ide.c revision bf64b7a9ddc604883a1f41535d3d7a62bca9ee81
1/*
2 *  Copyright (C) 1994-1998	    Linus Torvalds & authors (see below)
3 *  Copyrifht (C) 2003-2005, 2007   Bartlomiej Zolnierkiewicz
4 */
5
6/*
7 *  Mostly written by Mark Lord  <mlord@pobox.com>
8 *                and Gadi Oxman <gadio@netvision.net.il>
9 *                and Andre Hedrick <andre@linux-ide.org>
10 *
11 *  See linux/MAINTAINERS for address of current maintainer.
12 *
13 * This is the multiple IDE interface driver, as evolved from hd.c.
14 * It supports up to MAX_HWIFS IDE interfaces, on one or more IRQs
15 *   (usually 14 & 15).
16 * There can be up to two drives per interface, as per the ATA-2 spec.
17 *
18 * ...
19 *
20 *  From hd.c:
21 *  |
22 *  | It traverses the request-list, using interrupts to jump between functions.
23 *  | As nearly all functions can be called within interrupts, we may not sleep.
24 *  | Special care is recommended.  Have Fun!
25 *  |
26 *  | modified by Drew Eckhardt to check nr of hd's from the CMOS.
27 *  |
28 *  | Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
29 *  | in the early extended-partition checks and added DM partitions.
30 *  |
31 *  | Early work on error handling by Mika Liljeberg (liljeber@cs.Helsinki.FI).
32 *  |
33 *  | IRQ-unmask, drive-id, multiple-mode, support for ">16 heads",
34 *  | and general streamlining by Mark Lord (mlord@pobox.com).
35 *
36 *  October, 1994 -- Complete line-by-line overhaul for linux 1.1.x, by:
37 *
38 *	Mark Lord	(mlord@pobox.com)		(IDE Perf.Pkg)
39 *	Delman Lee	(delman@ieee.org)		("Mr. atdisk2")
40 *	Scott Snyder	(snyder@fnald0.fnal.gov)	(ATAPI IDE cd-rom)
41 *
42 *  This was a rewrite of just about everything from hd.c, though some original
43 *  code is still sprinkled about.  Think of it as a major evolution, with
44 *  inspiration from lots of linux users, esp.  hamish@zot.apana.org.au
45 */
46
47#define _IDE_C			/* Tell ide.h it's really us */
48
49#include <linux/module.h>
50#include <linux/types.h>
51#include <linux/string.h>
52#include <linux/kernel.h>
53#include <linux/timer.h>
54#include <linux/mm.h>
55#include <linux/interrupt.h>
56#include <linux/major.h>
57#include <linux/errno.h>
58#include <linux/genhd.h>
59#include <linux/blkpg.h>
60#include <linux/slab.h>
61#include <linux/init.h>
62#include <linux/pci.h>
63#include <linux/delay.h>
64#include <linux/ide.h>
65#include <linux/completion.h>
66#include <linux/reboot.h>
67#include <linux/cdrom.h>
68#include <linux/seq_file.h>
69#include <linux/device.h>
70#include <linux/bitops.h>
71
72#include <asm/byteorder.h>
73#include <asm/irq.h>
74#include <asm/uaccess.h>
75#include <asm/io.h>
76
77
78/* default maximum number of failures */
79#define IDE_DEFAULT_MAX_FAILURES 	1
80
81struct class *ide_port_class;
82
83static const u8 ide_hwif_to_major[] = { IDE0_MAJOR, IDE1_MAJOR,
84					IDE2_MAJOR, IDE3_MAJOR,
85					IDE4_MAJOR, IDE5_MAJOR,
86					IDE6_MAJOR, IDE7_MAJOR,
87					IDE8_MAJOR, IDE9_MAJOR };
88
89static int idebus_parameter;	/* holds the "idebus=" parameter */
90static int system_bus_speed;	/* holds what we think is VESA/PCI bus speed */
91
92DEFINE_MUTEX(ide_cfg_mtx);
93 __cacheline_aligned_in_smp DEFINE_SPINLOCK(ide_lock);
94
95int noautodma = 0;
96
97ide_hwif_t ide_hwifs[MAX_HWIFS];	/* master data repository */
98
99static void ide_port_init_devices_data(ide_hwif_t *);
100
101/*
102 * Do not even *think* about calling this!
103 */
104void ide_init_port_data(ide_hwif_t *hwif, unsigned int index)
105{
106	/* bulk initialize hwif & drive info with zeros */
107	memset(hwif, 0, sizeof(ide_hwif_t));
108
109	/* fill in any non-zero initial values */
110	hwif->index	= index;
111	hwif->major	= ide_hwif_to_major[index];
112
113	hwif->name[0]	= 'i';
114	hwif->name[1]	= 'd';
115	hwif->name[2]	= 'e';
116	hwif->name[3]	= '0' + index;
117
118	hwif->bus_state	= BUSSTATE_ON;
119
120	init_completion(&hwif->gendev_rel_comp);
121
122	default_hwif_iops(hwif);
123	default_hwif_transport(hwif);
124
125	ide_port_init_devices_data(hwif);
126}
127EXPORT_SYMBOL_GPL(ide_init_port_data);
128
129static void ide_port_init_devices_data(ide_hwif_t *hwif)
130{
131	int unit;
132
133	for (unit = 0; unit < MAX_DRIVES; ++unit) {
134		ide_drive_t *drive = &hwif->drives[unit];
135		u8 j = (hwif->index * MAX_DRIVES) + unit;
136
137		memset(drive, 0, sizeof(*drive));
138
139		drive->media			= ide_disk;
140		drive->select.all		= (unit<<4)|0xa0;
141		drive->hwif			= hwif;
142		drive->ctl			= 0x08;
143		drive->ready_stat		= READY_STAT;
144		drive->bad_wstat		= BAD_W_STAT;
145		drive->special.b.recalibrate	= 1;
146		drive->special.b.set_geometry	= 1;
147		drive->name[0]			= 'h';
148		drive->name[1]			= 'd';
149		drive->name[2]			= 'a' + j;
150		drive->max_failures		= IDE_DEFAULT_MAX_FAILURES;
151
152		INIT_LIST_HEAD(&drive->list);
153		init_completion(&drive->gendev_rel_comp);
154	}
155}
156
157/*
158 * init_ide_data() sets reasonable default values into all fields
159 * of all instances of the hwifs and drives, but only on the first call.
160 * Subsequent calls have no effect (they don't wipe out anything).
161 *
162 * This routine is normally called at driver initialization time,
163 * but may also be called MUCH earlier during kernel "command-line"
164 * parameter processing.  As such, we cannot depend on any other parts
165 * of the kernel (such as memory allocation) to be functioning yet.
166 *
167 * This is too bad, as otherwise we could dynamically allocate the
168 * ide_drive_t structs as needed, rather than always consuming memory
169 * for the max possible number (MAX_HWIFS * MAX_DRIVES) of them.
170 *
171 * FIXME: We should stuff the setup data into __init and copy the
172 * relevant hwifs/allocate them properly during boot.
173 */
174#define MAGIC_COOKIE 0x12345678
175static void __init init_ide_data (void)
176{
177	unsigned int index;
178	static unsigned long magic_cookie = MAGIC_COOKIE;
179
180	if (magic_cookie != MAGIC_COOKIE)
181		return;		/* already initialized */
182	magic_cookie = 0;
183
184	/* Initialise all interface structures */
185	for (index = 0; index < MAX_HWIFS; ++index) {
186		ide_hwif_t *hwif = &ide_hwifs[index];
187
188		ide_init_port_data(hwif, index);
189	}
190}
191
192/**
193 *	ide_system_bus_speed	-	guess bus speed
194 *
195 *	ide_system_bus_speed() returns what we think is the system VESA/PCI
196 *	bus speed (in MHz). This is used for calculating interface PIO timings.
197 *	The default is 40 for known PCI systems, 50 otherwise.
198 *	The "idebus=xx" parameter can be used to override this value.
199 *	The actual value to be used is computed/displayed the first time
200 *	through. Drivers should only use this as a last resort.
201 *
202 *	Returns a guessed speed in MHz.
203 */
204
205static int ide_system_bus_speed(void)
206{
207#ifdef CONFIG_PCI
208	static struct pci_device_id pci_default[] = {
209		{ PCI_DEVICE(PCI_ANY_ID, PCI_ANY_ID) },
210		{ }
211	};
212#else
213#define pci_default 0
214#endif /* CONFIG_PCI */
215
216	/* user supplied value */
217	if (idebus_parameter)
218		return idebus_parameter;
219
220	/* safe default value for PCI or VESA and PCI*/
221	return pci_dev_present(pci_default) ? 33 : 50;
222}
223
224void ide_remove_port_from_hwgroup(ide_hwif_t *hwif)
225{
226	ide_hwgroup_t *hwgroup = hwif->hwgroup;
227
228	spin_lock_irq(&ide_lock);
229	/*
230	 * Remove us from the hwgroup, and free
231	 * the hwgroup if we were the only member
232	 */
233	if (hwif->next == hwif) {
234		BUG_ON(hwgroup->hwif != hwif);
235		kfree(hwgroup);
236	} else {
237		/* There is another interface in hwgroup.
238		 * Unlink us, and set hwgroup->drive and ->hwif to
239		 * something sane.
240		 */
241		ide_hwif_t *g = hwgroup->hwif;
242
243		while (g->next != hwif)
244			g = g->next;
245		g->next = hwif->next;
246		if (hwgroup->hwif == hwif) {
247			/* Chose a random hwif for hwgroup->hwif.
248			 * It's guaranteed that there are no drives
249			 * left in the hwgroup.
250			 */
251			BUG_ON(hwgroup->drive != NULL);
252			hwgroup->hwif = g;
253		}
254		BUG_ON(hwgroup->hwif == hwif);
255	}
256	spin_unlock_irq(&ide_lock);
257}
258
259/* Called with ide_lock held. */
260static void __ide_port_unregister_devices(ide_hwif_t *hwif)
261{
262	int i;
263
264	for (i = 0; i < MAX_DRIVES; i++) {
265		ide_drive_t *drive = &hwif->drives[i];
266
267		if (drive->present) {
268			spin_unlock_irq(&ide_lock);
269			device_unregister(&drive->gendev);
270			wait_for_completion(&drive->gendev_rel_comp);
271			spin_lock_irq(&ide_lock);
272		}
273	}
274}
275
276void ide_port_unregister_devices(ide_hwif_t *hwif)
277{
278	mutex_lock(&ide_cfg_mtx);
279	spin_lock_irq(&ide_lock);
280	__ide_port_unregister_devices(hwif);
281	hwif->present = 0;
282	ide_port_init_devices_data(hwif);
283	spin_unlock_irq(&ide_lock);
284	mutex_unlock(&ide_cfg_mtx);
285}
286EXPORT_SYMBOL_GPL(ide_port_unregister_devices);
287
288/**
289 *	ide_unregister		-	free an IDE interface
290 *	@index: index of interface (will change soon to a pointer)
291 *
292 *	Perform the final unregister of an IDE interface. At the moment
293 *	we don't refcount interfaces so this will also get split up.
294 *
295 *	Locking:
296 *	The caller must not hold the IDE locks
297 *	The drive present/vanishing is not yet properly locked
298 *	Take care with the callbacks. These have been split to avoid
299 *	deadlocking the IDE layer. The shutdown callback is called
300 *	before we take the lock and free resources. It is up to the
301 *	caller to be sure there is no pending I/O here, and that
302 *	the interface will not be reopened (present/vanishing locking
303 *	isn't yet done BTW). After we commit to the final kill we
304 *	call the cleanup callback with the ide locks held.
305 *
306 *	Unregister restores the hwif structures to the default state.
307 *	This is raving bonkers.
308 */
309
310void ide_unregister(unsigned int index)
311{
312	ide_hwif_t *hwif, *g;
313	ide_hwgroup_t *hwgroup;
314	int irq_count = 0;
315
316	BUG_ON(index >= MAX_HWIFS);
317
318	BUG_ON(in_interrupt());
319	BUG_ON(irqs_disabled());
320	mutex_lock(&ide_cfg_mtx);
321	spin_lock_irq(&ide_lock);
322	hwif = &ide_hwifs[index];
323	if (!hwif->present)
324		goto abort;
325	__ide_port_unregister_devices(hwif);
326	hwif->present = 0;
327
328	spin_unlock_irq(&ide_lock);
329
330	ide_proc_unregister_port(hwif);
331
332	hwgroup = hwif->hwgroup;
333	/*
334	 * free the irq if we were the only hwif using it
335	 */
336	g = hwgroup->hwif;
337	do {
338		if (g->irq == hwif->irq)
339			++irq_count;
340		g = g->next;
341	} while (g != hwgroup->hwif);
342	if (irq_count == 1)
343		free_irq(hwif->irq, hwgroup);
344
345	ide_remove_port_from_hwgroup(hwif);
346
347	device_unregister(hwif->portdev);
348	device_unregister(&hwif->gendev);
349	wait_for_completion(&hwif->gendev_rel_comp);
350
351	/*
352	 * Remove us from the kernel's knowledge
353	 */
354	blk_unregister_region(MKDEV(hwif->major, 0), MAX_DRIVES<<PARTN_BITS);
355	kfree(hwif->sg_table);
356	unregister_blkdev(hwif->major, hwif->name);
357	spin_lock_irq(&ide_lock);
358
359	if (hwif->dma_base)
360		ide_release_dma_engine(hwif);
361
362	/* restore hwif data to pristine status */
363	ide_init_port_data(hwif, index);
364
365abort:
366	spin_unlock_irq(&ide_lock);
367	mutex_unlock(&ide_cfg_mtx);
368}
369
370EXPORT_SYMBOL(ide_unregister);
371
372void ide_init_port_hw(ide_hwif_t *hwif, hw_regs_t *hw)
373{
374	memcpy(hwif->io_ports, hw->io_ports, sizeof(hwif->io_ports));
375	hwif->irq = hw->irq;
376	hwif->chipset = hw->chipset;
377	hwif->gendev.parent = hw->dev;
378	hwif->ack_intr = hw->ack_intr;
379}
380EXPORT_SYMBOL_GPL(ide_init_port_hw);
381
382/*
383 *	Locks for IDE setting functionality
384 */
385
386DEFINE_MUTEX(ide_setting_mtx);
387
388EXPORT_SYMBOL_GPL(ide_setting_mtx);
389
390/**
391 *	ide_spin_wait_hwgroup	-	wait for group
392 *	@drive: drive in the group
393 *
394 *	Wait for an IDE device group to go non busy and then return
395 *	holding the ide_lock which guards the hwgroup->busy status
396 *	and right to use it.
397 */
398
399int ide_spin_wait_hwgroup (ide_drive_t *drive)
400{
401	ide_hwgroup_t *hwgroup = HWGROUP(drive);
402	unsigned long timeout = jiffies + (3 * HZ);
403
404	spin_lock_irq(&ide_lock);
405
406	while (hwgroup->busy) {
407		unsigned long lflags;
408		spin_unlock_irq(&ide_lock);
409		local_irq_set(lflags);
410		if (time_after(jiffies, timeout)) {
411			local_irq_restore(lflags);
412			printk(KERN_ERR "%s: channel busy\n", drive->name);
413			return -EBUSY;
414		}
415		local_irq_restore(lflags);
416		spin_lock_irq(&ide_lock);
417	}
418	return 0;
419}
420
421EXPORT_SYMBOL(ide_spin_wait_hwgroup);
422
423int set_io_32bit(ide_drive_t *drive, int arg)
424{
425	if (drive->no_io_32bit)
426		return -EPERM;
427
428	if (arg < 0 || arg > 1 + (SUPPORT_VLB_SYNC << 1))
429		return -EINVAL;
430
431	if (ide_spin_wait_hwgroup(drive))
432		return -EBUSY;
433
434	drive->io_32bit = arg;
435
436	spin_unlock_irq(&ide_lock);
437
438	return 0;
439}
440
441static int set_ksettings(ide_drive_t *drive, int arg)
442{
443	if (arg < 0 || arg > 1)
444		return -EINVAL;
445
446	if (ide_spin_wait_hwgroup(drive))
447		return -EBUSY;
448	drive->keep_settings = arg;
449	spin_unlock_irq(&ide_lock);
450
451	return 0;
452}
453
454int set_using_dma(ide_drive_t *drive, int arg)
455{
456#ifdef CONFIG_BLK_DEV_IDEDMA
457	ide_hwif_t *hwif = drive->hwif;
458	int err = -EPERM;
459
460	if (arg < 0 || arg > 1)
461		return -EINVAL;
462
463	if (!drive->id || !(drive->id->capability & 1))
464		goto out;
465
466	if (hwif->dma_ops == NULL)
467		goto out;
468
469	err = -EBUSY;
470	if (ide_spin_wait_hwgroup(drive))
471		goto out;
472	/*
473	 * set ->busy flag, unlock and let it ride
474	 */
475	hwif->hwgroup->busy = 1;
476	spin_unlock_irq(&ide_lock);
477
478	err = 0;
479
480	if (arg) {
481		if (ide_set_dma(drive))
482			err = -EIO;
483	} else
484		ide_dma_off(drive);
485
486	/*
487	 * lock, clear ->busy flag and unlock before leaving
488	 */
489	spin_lock_irq(&ide_lock);
490	hwif->hwgroup->busy = 0;
491	spin_unlock_irq(&ide_lock);
492out:
493	return err;
494#else
495	if (arg < 0 || arg > 1)
496		return -EINVAL;
497
498	return -EPERM;
499#endif
500}
501
502int set_pio_mode(ide_drive_t *drive, int arg)
503{
504	struct request rq;
505	ide_hwif_t *hwif = drive->hwif;
506	const struct ide_port_ops *port_ops = hwif->port_ops;
507
508	if (arg < 0 || arg > 255)
509		return -EINVAL;
510
511	if (port_ops == NULL || port_ops->set_pio_mode == NULL ||
512	    (hwif->host_flags & IDE_HFLAG_NO_SET_MODE))
513		return -ENOSYS;
514
515	if (drive->special.b.set_tune)
516		return -EBUSY;
517
518	ide_init_drive_cmd(&rq);
519	rq.cmd_type = REQ_TYPE_ATA_TASKFILE;
520
521	drive->tune_req = (u8) arg;
522	drive->special.b.set_tune = 1;
523	(void) ide_do_drive_cmd(drive, &rq, ide_wait);
524	return 0;
525}
526
527static int set_unmaskirq(ide_drive_t *drive, int arg)
528{
529	if (drive->no_unmask)
530		return -EPERM;
531
532	if (arg < 0 || arg > 1)
533		return -EINVAL;
534
535	if (ide_spin_wait_hwgroup(drive))
536		return -EBUSY;
537	drive->unmask = arg;
538	spin_unlock_irq(&ide_lock);
539
540	return 0;
541}
542
543/**
544 *	system_bus_clock	-	clock guess
545 *
546 *	External version of the bus clock guess used by very old IDE drivers
547 *	for things like VLB timings. Should not be used.
548 */
549
550int system_bus_clock (void)
551{
552	return system_bus_speed;
553}
554
555EXPORT_SYMBOL(system_bus_clock);
556
557static int generic_ide_suspend(struct device *dev, pm_message_t mesg)
558{
559	ide_drive_t *drive = dev->driver_data;
560	ide_hwif_t *hwif = HWIF(drive);
561	struct request rq;
562	struct request_pm_state rqpm;
563	ide_task_t args;
564	int ret;
565
566	/* Call ACPI _GTM only once */
567	if (!(drive->dn % 2))
568		ide_acpi_get_timing(hwif);
569
570	memset(&rq, 0, sizeof(rq));
571	memset(&rqpm, 0, sizeof(rqpm));
572	memset(&args, 0, sizeof(args));
573	rq.cmd_type = REQ_TYPE_PM_SUSPEND;
574	rq.special = &args;
575	rq.data = &rqpm;
576	rqpm.pm_step = ide_pm_state_start_suspend;
577	if (mesg.event == PM_EVENT_PRETHAW)
578		mesg.event = PM_EVENT_FREEZE;
579	rqpm.pm_state = mesg.event;
580
581	ret = ide_do_drive_cmd(drive, &rq, ide_wait);
582	/* only call ACPI _PS3 after both drivers are suspended */
583	if (!ret && (((drive->dn % 2) && hwif->drives[0].present
584		 && hwif->drives[1].present)
585		 || !hwif->drives[0].present
586		 || !hwif->drives[1].present))
587		ide_acpi_set_state(hwif, 0);
588	return ret;
589}
590
591static int generic_ide_resume(struct device *dev)
592{
593	ide_drive_t *drive = dev->driver_data;
594	ide_hwif_t *hwif = HWIF(drive);
595	struct request rq;
596	struct request_pm_state rqpm;
597	ide_task_t args;
598	int err;
599
600	/* Call ACPI _STM only once */
601	if (!(drive->dn % 2)) {
602		ide_acpi_set_state(hwif, 1);
603		ide_acpi_push_timing(hwif);
604	}
605
606	ide_acpi_exec_tfs(drive);
607
608	memset(&rq, 0, sizeof(rq));
609	memset(&rqpm, 0, sizeof(rqpm));
610	memset(&args, 0, sizeof(args));
611	rq.cmd_type = REQ_TYPE_PM_RESUME;
612	rq.special = &args;
613	rq.data = &rqpm;
614	rqpm.pm_step = ide_pm_state_start_resume;
615	rqpm.pm_state = PM_EVENT_ON;
616
617	err = ide_do_drive_cmd(drive, &rq, ide_head_wait);
618
619	if (err == 0 && dev->driver) {
620		ide_driver_t *drv = to_ide_driver(dev->driver);
621
622		if (drv->resume)
623			drv->resume(drive);
624	}
625
626	return err;
627}
628
629int generic_ide_ioctl(ide_drive_t *drive, struct file *file, struct block_device *bdev,
630			unsigned int cmd, unsigned long arg)
631{
632	unsigned long flags;
633	ide_driver_t *drv;
634	void __user *p = (void __user *)arg;
635	int err = 0, (*setfunc)(ide_drive_t *, int);
636	u8 *val;
637
638	switch (cmd) {
639	case HDIO_GET_32BIT:	    val = &drive->io_32bit;	 goto read_val;
640	case HDIO_GET_KEEPSETTINGS: val = &drive->keep_settings; goto read_val;
641	case HDIO_GET_UNMASKINTR:   val = &drive->unmask;	 goto read_val;
642	case HDIO_GET_DMA:	    val = &drive->using_dma;	 goto read_val;
643	case HDIO_SET_32BIT:	    setfunc = set_io_32bit;	 goto set_val;
644	case HDIO_SET_KEEPSETTINGS: setfunc = set_ksettings;	 goto set_val;
645	case HDIO_SET_PIO_MODE:	    setfunc = set_pio_mode;	 goto set_val;
646	case HDIO_SET_UNMASKINTR:   setfunc = set_unmaskirq;	 goto set_val;
647	case HDIO_SET_DMA:	    setfunc = set_using_dma;	 goto set_val;
648	}
649
650	switch (cmd) {
651		case HDIO_OBSOLETE_IDENTITY:
652		case HDIO_GET_IDENTITY:
653			if (bdev != bdev->bd_contains)
654				return -EINVAL;
655			if (drive->id_read == 0)
656				return -ENOMSG;
657			if (copy_to_user(p, drive->id, (cmd == HDIO_GET_IDENTITY) ? sizeof(*drive->id) : 142))
658				return -EFAULT;
659			return 0;
660
661		case HDIO_GET_NICE:
662			return put_user(drive->dsc_overlap	<<	IDE_NICE_DSC_OVERLAP	|
663					drive->atapi_overlap	<<	IDE_NICE_ATAPI_OVERLAP	|
664					drive->nice1 << IDE_NICE_1,
665					(long __user *) arg);
666#ifdef CONFIG_IDE_TASK_IOCTL
667		case HDIO_DRIVE_TASKFILE:
668		        if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
669				return -EACCES;
670			switch(drive->media) {
671				case ide_disk:
672					return ide_taskfile_ioctl(drive, cmd, arg);
673				default:
674					return -ENOMSG;
675			}
676#endif /* CONFIG_IDE_TASK_IOCTL */
677
678		case HDIO_DRIVE_CMD:
679			if (!capable(CAP_SYS_RAWIO))
680				return -EACCES;
681			return ide_cmd_ioctl(drive, cmd, arg);
682
683		case HDIO_DRIVE_TASK:
684			if (!capable(CAP_SYS_RAWIO))
685				return -EACCES;
686			return ide_task_ioctl(drive, cmd, arg);
687		case HDIO_SET_NICE:
688			if (!capable(CAP_SYS_ADMIN)) return -EACCES;
689			if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
690				return -EPERM;
691			drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1;
692			drv = *(ide_driver_t **)bdev->bd_disk->private_data;
693			if (drive->dsc_overlap && !drv->supports_dsc_overlap) {
694				drive->dsc_overlap = 0;
695				return -EPERM;
696			}
697			drive->nice1 = (arg >> IDE_NICE_1) & 1;
698			return 0;
699		case HDIO_DRIVE_RESET:
700			if (!capable(CAP_SYS_ADMIN))
701				return -EACCES;
702
703			/*
704			 *	Abort the current command on the
705			 *	group if there is one, taking
706			 *	care not to allow anything else
707			 *	to be queued and to die on the
708			 *	spot if we miss one somehow
709			 */
710
711			spin_lock_irqsave(&ide_lock, flags);
712
713			if (HWGROUP(drive)->resetting) {
714				spin_unlock_irqrestore(&ide_lock, flags);
715				return -EBUSY;
716			}
717
718			ide_abort(drive, "drive reset");
719
720			BUG_ON(HWGROUP(drive)->handler);
721
722			/* Ensure nothing gets queued after we
723			   drop the lock. Reset will clear the busy */
724
725			HWGROUP(drive)->busy = 1;
726			spin_unlock_irqrestore(&ide_lock, flags);
727			(void) ide_do_reset(drive);
728
729			return 0;
730		case HDIO_GET_BUSSTATE:
731			if (!capable(CAP_SYS_ADMIN))
732				return -EACCES;
733			if (put_user(HWIF(drive)->bus_state, (long __user *)arg))
734				return -EFAULT;
735			return 0;
736
737		case HDIO_SET_BUSSTATE:
738			if (!capable(CAP_SYS_ADMIN))
739				return -EACCES;
740			return -EOPNOTSUPP;
741		default:
742			return -EINVAL;
743	}
744
745read_val:
746	mutex_lock(&ide_setting_mtx);
747	spin_lock_irqsave(&ide_lock, flags);
748	err = *val;
749	spin_unlock_irqrestore(&ide_lock, flags);
750	mutex_unlock(&ide_setting_mtx);
751	return err >= 0 ? put_user(err, (long __user *)arg) : err;
752
753set_val:
754	if (bdev != bdev->bd_contains)
755		err = -EINVAL;
756	else {
757		if (!capable(CAP_SYS_ADMIN))
758			err = -EACCES;
759		else {
760			mutex_lock(&ide_setting_mtx);
761			err = setfunc(drive, arg);
762			mutex_unlock(&ide_setting_mtx);
763		}
764	}
765	return err;
766}
767
768EXPORT_SYMBOL(generic_ide_ioctl);
769
770/*
771 * stridx() returns the offset of c within s,
772 * or -1 if c is '\0' or not found within s.
773 */
774static int __init stridx (const char *s, char c)
775{
776	char *i = strchr(s, c);
777	return (i && c) ? i - s : -1;
778}
779
780/*
781 * match_parm() does parsing for ide_setup():
782 *
783 * 1. the first char of s must be '='.
784 * 2. if the remainder matches one of the supplied keywords,
785 *     the index (1 based) of the keyword is negated and returned.
786 * 3. if the remainder is a series of no more than max_vals numbers
787 *     separated by commas, the numbers are saved in vals[] and a
788 *     count of how many were saved is returned.  Base10 is assumed,
789 *     and base16 is allowed when prefixed with "0x".
790 * 4. otherwise, zero is returned.
791 */
792static int __init match_parm (char *s, const char *keywords[], int vals[], int max_vals)
793{
794	static const char *decimal = "0123456789";
795	static const char *hex = "0123456789abcdef";
796	int i, n;
797
798	if (*s++ == '=') {
799		/*
800		 * Try matching against the supplied keywords,
801		 * and return -(index+1) if we match one
802		 */
803		if (keywords != NULL) {
804			for (i = 0; *keywords != NULL; ++i) {
805				if (!strcmp(s, *keywords++))
806					return -(i+1);
807			}
808		}
809		/*
810		 * Look for a series of no more than "max_vals"
811		 * numeric values separated by commas, in base10,
812		 * or base16 when prefixed with "0x".
813		 * Return a count of how many were found.
814		 */
815		for (n = 0; (i = stridx(decimal, *s)) >= 0;) {
816			vals[n] = i;
817			while ((i = stridx(decimal, *++s)) >= 0)
818				vals[n] = (vals[n] * 10) + i;
819			if (*s == 'x' && !vals[n]) {
820				while ((i = stridx(hex, *++s)) >= 0)
821					vals[n] = (vals[n] * 0x10) + i;
822			}
823			if (++n == max_vals)
824				break;
825			if (*s == ',' || *s == ';')
826				++s;
827		}
828		if (!*s)
829			return n;
830	}
831	return 0;	/* zero = nothing matched */
832}
833
834/*
835 * ide_setup() gets called VERY EARLY during initialization,
836 * to handle kernel "command line" strings beginning with "hdx=" or "ide".
837 *
838 * Remember to update Documentation/ide/ide.txt if you change something here.
839 */
840static int __init ide_setup(char *s)
841{
842	ide_hwif_t *hwif;
843	ide_drive_t *drive;
844	unsigned int hw, unit;
845	int vals[3];
846	const char max_drive = 'a' + ((MAX_HWIFS * MAX_DRIVES) - 1);
847
848	if (strncmp(s,"hd",2) == 0 && s[2] == '=')	/* hd= is for hd.c   */
849		return 0;				/* driver and not us */
850
851	if (strncmp(s,"ide",3) && strncmp(s,"idebus",6) && strncmp(s,"hd",2))
852		return 0;
853
854	printk(KERN_INFO "ide_setup: %s", s);
855	init_ide_data ();
856
857#ifdef CONFIG_BLK_DEV_IDEDOUBLER
858	if (!strcmp(s, "ide=doubler")) {
859		extern int ide_doubler;
860
861		printk(" : Enabled support for IDE doublers\n");
862		ide_doubler = 1;
863		goto obsolete_option;
864	}
865#endif /* CONFIG_BLK_DEV_IDEDOUBLER */
866
867	if (!strcmp(s, "ide=nodma")) {
868		printk(" : Prevented DMA\n");
869		noautodma = 1;
870		goto obsolete_option;
871	}
872
873#ifdef CONFIG_BLK_DEV_IDEACPI
874	if (!strcmp(s, "ide=noacpi")) {
875		//printk(" : Disable IDE ACPI support.\n");
876		ide_noacpi = 1;
877		goto obsolete_option;
878	}
879	if (!strcmp(s, "ide=acpigtf")) {
880		//printk(" : Enable IDE ACPI _GTF support.\n");
881		ide_acpigtf = 1;
882		goto obsolete_option;
883	}
884	if (!strcmp(s, "ide=acpionboot")) {
885		//printk(" : Call IDE ACPI methods on boot.\n");
886		ide_acpionboot = 1;
887		goto obsolete_option;
888	}
889#endif /* CONFIG_BLK_DEV_IDEACPI */
890
891	/*
892	 * Look for drive options:  "hdx="
893	 */
894	if (s[0] == 'h' && s[1] == 'd' && s[2] >= 'a' && s[2] <= max_drive) {
895		const char *hd_words[] = {
896			"none", "noprobe", "nowerr", "cdrom", "nodma",
897			"-6", "-7", "-8", "-9", "-10",
898			"noflush", "remap", "remap63", "scsi", NULL };
899		unit = s[2] - 'a';
900		hw   = unit / MAX_DRIVES;
901		unit = unit % MAX_DRIVES;
902		hwif = &ide_hwifs[hw];
903		drive = &hwif->drives[unit];
904		if (strncmp(s + 4, "ide-", 4) == 0) {
905			strlcpy(drive->driver_req, s + 4, sizeof(drive->driver_req));
906			goto obsolete_option;
907		}
908		switch (match_parm(&s[3], hd_words, vals, 3)) {
909			case -1: /* "none" */
910			case -2: /* "noprobe" */
911				drive->noprobe = 1;
912				goto obsolete_option;
913			case -3: /* "nowerr" */
914				drive->bad_wstat = BAD_R_STAT;
915				goto obsolete_option;
916			case -4: /* "cdrom" */
917				drive->present = 1;
918				drive->media = ide_cdrom;
919				/* an ATAPI device ignores DRDY */
920				drive->ready_stat = 0;
921				goto obsolete_option;
922			case -5: /* nodma */
923				drive->nodma = 1;
924				goto obsolete_option;
925			case -11: /* noflush */
926				drive->noflush = 1;
927				goto obsolete_option;
928			case -12: /* "remap" */
929				drive->remap_0_to_1 = 1;
930				goto obsolete_option;
931			case -13: /* "remap63" */
932				drive->sect0 = 63;
933				goto obsolete_option;
934			case -14: /* "scsi" */
935				drive->scsi = 1;
936				goto obsolete_option;
937			case 3: /* cyl,head,sect */
938				drive->media	= ide_disk;
939				drive->ready_stat = READY_STAT;
940				drive->cyl	= drive->bios_cyl  = vals[0];
941				drive->head	= drive->bios_head = vals[1];
942				drive->sect	= drive->bios_sect = vals[2];
943				drive->present	= 1;
944				drive->forced_geom = 1;
945				goto obsolete_option;
946			default:
947				goto bad_option;
948		}
949	}
950
951	if (s[0] != 'i' || s[1] != 'd' || s[2] != 'e')
952		goto bad_option;
953	/*
954	 * Look for bus speed option:  "idebus="
955	 */
956	if (s[3] == 'b' && s[4] == 'u' && s[5] == 's') {
957		if (match_parm(&s[6], NULL, vals, 1) != 1)
958			goto bad_option;
959		if (vals[0] >= 20 && vals[0] <= 66) {
960			idebus_parameter = vals[0];
961		} else
962			printk(" -- BAD BUS SPEED! Expected value from 20 to 66");
963		goto obsolete_option;
964	}
965
966bad_option:
967	printk(" -- BAD OPTION\n");
968	return 1;
969obsolete_option:
970	printk(" -- OBSOLETE OPTION, WILL BE REMOVED SOON!\n");
971	return 1;
972}
973
974EXPORT_SYMBOL(ide_lock);
975
976static int ide_bus_match(struct device *dev, struct device_driver *drv)
977{
978	return 1;
979}
980
981static char *media_string(ide_drive_t *drive)
982{
983	switch (drive->media) {
984	case ide_disk:
985		return "disk";
986	case ide_cdrom:
987		return "cdrom";
988	case ide_tape:
989		return "tape";
990	case ide_floppy:
991		return "floppy";
992	case ide_optical:
993		return "optical";
994	default:
995		return "UNKNOWN";
996	}
997}
998
999static ssize_t media_show(struct device *dev, struct device_attribute *attr, char *buf)
1000{
1001	ide_drive_t *drive = to_ide_device(dev);
1002	return sprintf(buf, "%s\n", media_string(drive));
1003}
1004
1005static ssize_t drivename_show(struct device *dev, struct device_attribute *attr, char *buf)
1006{
1007	ide_drive_t *drive = to_ide_device(dev);
1008	return sprintf(buf, "%s\n", drive->name);
1009}
1010
1011static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
1012{
1013	ide_drive_t *drive = to_ide_device(dev);
1014	return sprintf(buf, "ide:m-%s\n", media_string(drive));
1015}
1016
1017static ssize_t model_show(struct device *dev, struct device_attribute *attr,
1018			  char *buf)
1019{
1020	ide_drive_t *drive = to_ide_device(dev);
1021	return sprintf(buf, "%s\n", drive->id->model);
1022}
1023
1024static ssize_t firmware_show(struct device *dev, struct device_attribute *attr,
1025			     char *buf)
1026{
1027	ide_drive_t *drive = to_ide_device(dev);
1028	return sprintf(buf, "%s\n", drive->id->fw_rev);
1029}
1030
1031static ssize_t serial_show(struct device *dev, struct device_attribute *attr,
1032			   char *buf)
1033{
1034	ide_drive_t *drive = to_ide_device(dev);
1035	return sprintf(buf, "%s\n", drive->id->serial_no);
1036}
1037
1038static struct device_attribute ide_dev_attrs[] = {
1039	__ATTR_RO(media),
1040	__ATTR_RO(drivename),
1041	__ATTR_RO(modalias),
1042	__ATTR_RO(model),
1043	__ATTR_RO(firmware),
1044	__ATTR(serial, 0400, serial_show, NULL),
1045	__ATTR_NULL
1046};
1047
1048static int ide_uevent(struct device *dev, struct kobj_uevent_env *env)
1049{
1050	ide_drive_t *drive = to_ide_device(dev);
1051
1052	add_uevent_var(env, "MEDIA=%s", media_string(drive));
1053	add_uevent_var(env, "DRIVENAME=%s", drive->name);
1054	add_uevent_var(env, "MODALIAS=ide:m-%s", media_string(drive));
1055	return 0;
1056}
1057
1058static int generic_ide_probe(struct device *dev)
1059{
1060	ide_drive_t *drive = to_ide_device(dev);
1061	ide_driver_t *drv = to_ide_driver(dev->driver);
1062
1063	return drv->probe ? drv->probe(drive) : -ENODEV;
1064}
1065
1066static int generic_ide_remove(struct device *dev)
1067{
1068	ide_drive_t *drive = to_ide_device(dev);
1069	ide_driver_t *drv = to_ide_driver(dev->driver);
1070
1071	if (drv->remove)
1072		drv->remove(drive);
1073
1074	return 0;
1075}
1076
1077static void generic_ide_shutdown(struct device *dev)
1078{
1079	ide_drive_t *drive = to_ide_device(dev);
1080	ide_driver_t *drv = to_ide_driver(dev->driver);
1081
1082	if (dev->driver && drv->shutdown)
1083		drv->shutdown(drive);
1084}
1085
1086struct bus_type ide_bus_type = {
1087	.name		= "ide",
1088	.match		= ide_bus_match,
1089	.uevent		= ide_uevent,
1090	.probe		= generic_ide_probe,
1091	.remove		= generic_ide_remove,
1092	.shutdown	= generic_ide_shutdown,
1093	.dev_attrs	= ide_dev_attrs,
1094	.suspend	= generic_ide_suspend,
1095	.resume		= generic_ide_resume,
1096};
1097
1098EXPORT_SYMBOL_GPL(ide_bus_type);
1099
1100static void ide_port_class_release(struct device *portdev)
1101{
1102	ide_hwif_t *hwif = dev_get_drvdata(portdev);
1103
1104	put_device(&hwif->gendev);
1105}
1106
1107int ide_vlb_clk;
1108EXPORT_SYMBOL_GPL(ide_vlb_clk);
1109
1110module_param_named(vlb_clock, ide_vlb_clk, int, 0);
1111MODULE_PARM_DESC(vlb_clock, "VLB clock frequency (in MHz)");
1112
1113int ide_pci_clk;
1114EXPORT_SYMBOL_GPL(ide_pci_clk);
1115
1116module_param_named(pci_clock, ide_pci_clk, int, 0);
1117MODULE_PARM_DESC(pci_clock, "PCI bus clock frequency (in MHz)");
1118
1119static int ide_set_dev_param_mask(const char *s, struct kernel_param *kp)
1120{
1121	int a, b, i, j = 1;
1122	unsigned int *dev_param_mask = (unsigned int *)kp->arg;
1123
1124	if (sscanf(s, "%d.%d:%d", &a, &b, &j) != 3 &&
1125	    sscanf(s, "%d.%d", &a, &b) != 2)
1126		return -EINVAL;
1127
1128	i = a * MAX_DRIVES + b;
1129
1130	if (i >= MAX_HWIFS * MAX_DRIVES || j < 0 || j > 1)
1131		return -EINVAL;
1132
1133	if (j)
1134		*dev_param_mask |= (1 << i);
1135	else
1136		*dev_param_mask &= (1 << i);
1137
1138	return 0;
1139}
1140
1141static unsigned int ide_nodma;
1142
1143module_param_call(nodma, ide_set_dev_param_mask, NULL, &ide_nodma, 0);
1144MODULE_PARM_DESC(nodma, "disallow DMA for a device");
1145
1146static unsigned int ide_noflush;
1147
1148module_param_call(noflush, ide_set_dev_param_mask, NULL, &ide_noflush, 0);
1149MODULE_PARM_DESC(noflush, "disable flush requests for a device");
1150
1151static unsigned int ide_noprobe;
1152
1153module_param_call(noprobe, ide_set_dev_param_mask, NULL, &ide_noprobe, 0);
1154MODULE_PARM_DESC(noprobe, "skip probing for a device");
1155
1156static unsigned int ide_nowerr;
1157
1158module_param_call(nowerr, ide_set_dev_param_mask, NULL, &ide_nowerr, 0);
1159MODULE_PARM_DESC(nowerr, "ignore the WRERR_STAT bit for a device");
1160
1161static unsigned int ide_cdroms;
1162
1163module_param_call(cdrom, ide_set_dev_param_mask, NULL, &ide_cdroms, 0);
1164MODULE_PARM_DESC(cdrom, "force device as a CD-ROM");
1165
1166struct chs_geom {
1167	unsigned int	cyl;
1168	u8		head;
1169	u8		sect;
1170};
1171
1172static unsigned int ide_disks;
1173static struct chs_geom ide_disks_chs[MAX_HWIFS * MAX_DRIVES];
1174
1175static int ide_set_disk_chs(const char *str, struct kernel_param *kp)
1176{
1177	int a, b, c = 0, h = 0, s = 0, i, j = 1;
1178
1179	if (sscanf(str, "%d.%d:%d,%d,%d", &a, &b, &c, &h, &s) != 5 &&
1180	    sscanf(str, "%d.%d:%d", &a, &b, &j) != 3)
1181		return -EINVAL;
1182
1183	i = a * MAX_DRIVES + b;
1184
1185	if (i >= MAX_HWIFS * MAX_DRIVES || j < 0 || j > 1)
1186		return -EINVAL;
1187
1188	if (c > INT_MAX || h > 255 || s > 255)
1189		return -EINVAL;
1190
1191	if (j)
1192		ide_disks |= (1 << i);
1193	else
1194		ide_disks &= (1 << i);
1195
1196	ide_disks_chs[i].cyl  = c;
1197	ide_disks_chs[i].head = h;
1198	ide_disks_chs[i].sect = s;
1199
1200	return 0;
1201}
1202
1203module_param_call(chs, ide_set_disk_chs, NULL, NULL, 0);
1204MODULE_PARM_DESC(chs, "force device as a disk (using CHS)");
1205
1206static void ide_dev_apply_params(ide_drive_t *drive)
1207{
1208	int i = drive->hwif->index * MAX_DRIVES + drive->select.b.unit;
1209
1210	if (ide_nodma & (1 << i)) {
1211		printk(KERN_INFO "ide: disallowing DMA for %s\n", drive->name);
1212		drive->nodma = 1;
1213	}
1214	if (ide_noflush & (1 << i)) {
1215		printk(KERN_INFO "ide: disabling flush requests for %s\n",
1216				 drive->name);
1217		drive->noflush = 1;
1218	}
1219	if (ide_noprobe & (1 << i)) {
1220		printk(KERN_INFO "ide: skipping probe for %s\n", drive->name);
1221		drive->noprobe = 1;
1222	}
1223	if (ide_nowerr & (1 << i)) {
1224		printk(KERN_INFO "ide: ignoring the WRERR_STAT bit for %s\n",
1225				 drive->name);
1226		drive->bad_wstat = BAD_R_STAT;
1227	}
1228	if (ide_cdroms & (1 << i)) {
1229		printk(KERN_INFO "ide: forcing %s as a CD-ROM\n", drive->name);
1230		drive->present = 1;
1231		drive->media = ide_cdrom;
1232		/* an ATAPI device ignores DRDY */
1233		drive->ready_stat = 0;
1234	}
1235	if (ide_disks & (1 << i)) {
1236		drive->cyl  = drive->bios_cyl  = ide_disks_chs[i].cyl;
1237		drive->head = drive->bios_head = ide_disks_chs[i].head;
1238		drive->sect = drive->bios_sect = ide_disks_chs[i].sect;
1239		drive->forced_geom = 1;
1240		printk(KERN_INFO "ide: forcing %s as a disk (%d/%d/%d)\n",
1241				 drive->name,
1242				 drive->cyl, drive->head, drive->sect);
1243		drive->present = 1;
1244		drive->media = ide_disk;
1245		drive->ready_stat = READY_STAT;
1246	}
1247}
1248
1249static unsigned int ide_ignore_cable;
1250
1251static int ide_set_ignore_cable(const char *s, struct kernel_param *kp)
1252{
1253	int i, j = 1;
1254
1255	if (sscanf(s, "%d:%d", &i, &j) != 2 && sscanf(s, "%d", &i) != 1)
1256		return -EINVAL;
1257
1258	if (i >= MAX_HWIFS || j < 0 || j > 1)
1259		return -EINVAL;
1260
1261	if (j)
1262		ide_ignore_cable |= (1 << i);
1263	else
1264		ide_ignore_cable &= (1 << i);
1265
1266	return 0;
1267}
1268
1269module_param_call(ignore_cable, ide_set_ignore_cable, NULL, NULL, 0);
1270MODULE_PARM_DESC(ignore_cable, "ignore cable detection");
1271
1272void ide_port_apply_params(ide_hwif_t *hwif)
1273{
1274	int i;
1275
1276	if (ide_ignore_cable & (1 << hwif->index)) {
1277		printk(KERN_INFO "ide: ignoring cable detection for %s\n",
1278				 hwif->name);
1279		hwif->cbl = ATA_CBL_PATA40_SHORT;
1280	}
1281
1282	for (i = 0; i < MAX_DRIVES; i++)
1283		ide_dev_apply_params(&hwif->drives[i]);
1284}
1285
1286/*
1287 * This is gets invoked once during initialization, to set *everything* up
1288 */
1289static int __init ide_init(void)
1290{
1291	int ret;
1292
1293	printk(KERN_INFO "Uniform Multi-Platform E-IDE driver\n");
1294	system_bus_speed = ide_system_bus_speed();
1295
1296	printk(KERN_INFO "ide: Assuming %dMHz system bus speed "
1297			 "for PIO modes%s\n", system_bus_speed,
1298			idebus_parameter ? "" : "; override with idebus=xx");
1299
1300	ret = bus_register(&ide_bus_type);
1301	if (ret < 0) {
1302		printk(KERN_WARNING "IDE: bus_register error: %d\n", ret);
1303		return ret;
1304	}
1305
1306	ide_port_class = class_create(THIS_MODULE, "ide_port");
1307	if (IS_ERR(ide_port_class)) {
1308		ret = PTR_ERR(ide_port_class);
1309		goto out_port_class;
1310	}
1311	ide_port_class->dev_release = ide_port_class_release;
1312
1313	init_ide_data();
1314
1315	proc_ide_create();
1316
1317	return 0;
1318
1319out_port_class:
1320	bus_unregister(&ide_bus_type);
1321
1322	return ret;
1323}
1324
1325#ifdef MODULE
1326static char *options = NULL;
1327module_param(options, charp, 0);
1328MODULE_LICENSE("GPL");
1329
1330static void __init parse_options (char *line)
1331{
1332	char *next = line;
1333
1334	if (line == NULL || !*line)
1335		return;
1336	while ((line = next) != NULL) {
1337 		if ((next = strchr(line,' ')) != NULL)
1338			*next++ = 0;
1339		if (!ide_setup(line))
1340			printk (KERN_INFO "Unknown option '%s'\n", line);
1341	}
1342}
1343
1344int __init init_module (void)
1345{
1346	parse_options(options);
1347	return ide_init();
1348}
1349
1350void __exit cleanup_module (void)
1351{
1352	proc_ide_destroy();
1353
1354	class_destroy(ide_port_class);
1355
1356	bus_unregister(&ide_bus_type);
1357}
1358
1359#else /* !MODULE */
1360
1361__setup("", ide_setup);
1362
1363module_init(ide_init);
1364
1365#endif /* MODULE */
1366