ide.c revision 9232c14bff36d65de254f34386c00b732c5b6099
1/*
2 *  Copyright (C) 1994-1998	    Linus Torvalds & authors (see below)
3 *  Copyright (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#include <linux/module.h>
48#include <linux/types.h>
49#include <linux/string.h>
50#include <linux/kernel.h>
51#include <linux/interrupt.h>
52#include <linux/major.h>
53#include <linux/errno.h>
54#include <linux/genhd.h>
55#include <linux/slab.h>
56#include <linux/init.h>
57#include <linux/pci.h>
58#include <linux/ide.h>
59#include <linux/hdreg.h>
60#include <linux/completion.h>
61#include <linux/device.h>
62
63
64/* default maximum number of failures */
65#define IDE_DEFAULT_MAX_FAILURES 	1
66
67struct class *ide_port_class;
68
69static const u8 ide_hwif_to_major[] = { IDE0_MAJOR, IDE1_MAJOR,
70					IDE2_MAJOR, IDE3_MAJOR,
71					IDE4_MAJOR, IDE5_MAJOR,
72					IDE6_MAJOR, IDE7_MAJOR,
73					IDE8_MAJOR, IDE9_MAJOR };
74
75DEFINE_MUTEX(ide_cfg_mtx);
76
77__cacheline_aligned_in_smp DEFINE_SPINLOCK(ide_lock);
78EXPORT_SYMBOL(ide_lock);
79
80static void ide_port_init_devices_data(ide_hwif_t *);
81
82/*
83 * Do not even *think* about calling this!
84 */
85void ide_init_port_data(ide_hwif_t *hwif, unsigned int index)
86{
87	/* bulk initialize hwif & drive info with zeros */
88	memset(hwif, 0, sizeof(ide_hwif_t));
89
90	/* fill in any non-zero initial values */
91	hwif->index	= index;
92	hwif->major	= ide_hwif_to_major[index];
93
94	hwif->name[0]	= 'i';
95	hwif->name[1]	= 'd';
96	hwif->name[2]	= 'e';
97	hwif->name[3]	= '0' + index;
98
99	init_completion(&hwif->gendev_rel_comp);
100
101	hwif->tp_ops = &default_tp_ops;
102
103	ide_port_init_devices_data(hwif);
104}
105
106static void ide_port_init_devices_data(ide_hwif_t *hwif)
107{
108	int unit;
109
110	for (unit = 0; unit < MAX_DRIVES; ++unit) {
111		ide_drive_t *drive = &hwif->drives[unit];
112		u8 j = (hwif->index * MAX_DRIVES) + unit;
113
114		memset(drive, 0, sizeof(*drive));
115
116		drive->media			= ide_disk;
117		drive->select.all		= (unit<<4)|0xa0;
118		drive->hwif			= hwif;
119		drive->ready_stat		= ATA_DRDY;
120		drive->bad_wstat		= BAD_W_STAT;
121		drive->special.b.recalibrate	= 1;
122		drive->special.b.set_geometry	= 1;
123		drive->name[0]			= 'h';
124		drive->name[1]			= 'd';
125		drive->name[2]			= 'a' + j;
126		drive->max_failures		= IDE_DEFAULT_MAX_FAILURES;
127
128		INIT_LIST_HEAD(&drive->list);
129		init_completion(&drive->gendev_rel_comp);
130	}
131}
132
133/* Called with ide_lock held. */
134static void __ide_port_unregister_devices(ide_hwif_t *hwif)
135{
136	int i;
137
138	for (i = 0; i < MAX_DRIVES; i++) {
139		ide_drive_t *drive = &hwif->drives[i];
140
141		if (drive->present) {
142			spin_unlock_irq(&ide_lock);
143			device_unregister(&drive->gendev);
144			wait_for_completion(&drive->gendev_rel_comp);
145			spin_lock_irq(&ide_lock);
146		}
147	}
148}
149
150void ide_port_unregister_devices(ide_hwif_t *hwif)
151{
152	mutex_lock(&ide_cfg_mtx);
153	spin_lock_irq(&ide_lock);
154	__ide_port_unregister_devices(hwif);
155	hwif->present = 0;
156	ide_port_init_devices_data(hwif);
157	spin_unlock_irq(&ide_lock);
158	mutex_unlock(&ide_cfg_mtx);
159}
160EXPORT_SYMBOL_GPL(ide_port_unregister_devices);
161
162/**
163 *	ide_unregister		-	free an IDE interface
164 *	@hwif: IDE interface
165 *
166 *	Perform the final unregister of an IDE interface. At the moment
167 *	we don't refcount interfaces so this will also get split up.
168 *
169 *	Locking:
170 *	The caller must not hold the IDE locks
171 *	The drive present/vanishing is not yet properly locked
172 *	Take care with the callbacks. These have been split to avoid
173 *	deadlocking the IDE layer. The shutdown callback is called
174 *	before we take the lock and free resources. It is up to the
175 *	caller to be sure there is no pending I/O here, and that
176 *	the interface will not be reopened (present/vanishing locking
177 *	isn't yet done BTW). After we commit to the final kill we
178 *	call the cleanup callback with the ide locks held.
179 *
180 *	Unregister restores the hwif structures to the default state.
181 *	This is raving bonkers.
182 */
183
184void ide_unregister(ide_hwif_t *hwif)
185{
186	ide_hwif_t *g;
187	ide_hwgroup_t *hwgroup;
188	int irq_count = 0;
189
190	BUG_ON(in_interrupt());
191	BUG_ON(irqs_disabled());
192
193	mutex_lock(&ide_cfg_mtx);
194
195	spin_lock_irq(&ide_lock);
196	if (hwif->present) {
197		__ide_port_unregister_devices(hwif);
198		hwif->present = 0;
199	}
200	spin_unlock_irq(&ide_lock);
201
202	ide_proc_unregister_port(hwif);
203
204	hwgroup = hwif->hwgroup;
205	/*
206	 * free the irq if we were the only hwif using it
207	 */
208	g = hwgroup->hwif;
209	do {
210		if (g->irq == hwif->irq)
211			++irq_count;
212		g = g->next;
213	} while (g != hwgroup->hwif);
214	if (irq_count == 1)
215		free_irq(hwif->irq, hwgroup);
216
217	ide_remove_port_from_hwgroup(hwif);
218
219	device_unregister(hwif->portdev);
220	device_unregister(&hwif->gendev);
221	wait_for_completion(&hwif->gendev_rel_comp);
222
223	/*
224	 * Remove us from the kernel's knowledge
225	 */
226	blk_unregister_region(MKDEV(hwif->major, 0), MAX_DRIVES<<PARTN_BITS);
227	kfree(hwif->sg_table);
228	unregister_blkdev(hwif->major, hwif->name);
229
230	if (hwif->dma_base)
231		ide_release_dma_engine(hwif);
232
233	mutex_unlock(&ide_cfg_mtx);
234}
235
236void ide_init_port_hw(ide_hwif_t *hwif, hw_regs_t *hw)
237{
238	memcpy(&hwif->io_ports, &hw->io_ports, sizeof(hwif->io_ports));
239	hwif->irq = hw->irq;
240	hwif->chipset = hw->chipset;
241	hwif->dev = hw->dev;
242	hwif->gendev.parent = hw->parent ? hw->parent : hw->dev;
243	hwif->ack_intr = hw->ack_intr;
244	hwif->config_data = hw->config;
245}
246
247/*
248 *	Locks for IDE setting functionality
249 */
250
251DEFINE_MUTEX(ide_setting_mtx);
252
253EXPORT_SYMBOL_GPL(ide_setting_mtx);
254
255/**
256 *	ide_spin_wait_hwgroup	-	wait for group
257 *	@drive: drive in the group
258 *
259 *	Wait for an IDE device group to go non busy and then return
260 *	holding the ide_lock which guards the hwgroup->busy status
261 *	and right to use it.
262 */
263
264int ide_spin_wait_hwgroup (ide_drive_t *drive)
265{
266	ide_hwgroup_t *hwgroup = HWGROUP(drive);
267	unsigned long timeout = jiffies + (3 * HZ);
268
269	spin_lock_irq(&ide_lock);
270
271	while (hwgroup->busy) {
272		unsigned long lflags;
273		spin_unlock_irq(&ide_lock);
274		local_irq_set(lflags);
275		if (time_after(jiffies, timeout)) {
276			local_irq_restore(lflags);
277			printk(KERN_ERR "%s: channel busy\n", drive->name);
278			return -EBUSY;
279		}
280		local_irq_restore(lflags);
281		spin_lock_irq(&ide_lock);
282	}
283	return 0;
284}
285
286EXPORT_SYMBOL(ide_spin_wait_hwgroup);
287
288ide_devset_get(io_32bit, io_32bit);
289
290int set_io_32bit(ide_drive_t *drive, int arg)
291{
292	if (drive->no_io_32bit)
293		return -EPERM;
294
295	if (arg < 0 || arg > 1 + (SUPPORT_VLB_SYNC << 1))
296		return -EINVAL;
297
298	if (ide_spin_wait_hwgroup(drive))
299		return -EBUSY;
300
301	drive->io_32bit = arg;
302
303	spin_unlock_irq(&ide_lock);
304
305	return 0;
306}
307
308ide_devset_get(ksettings, keep_settings);
309
310int set_ksettings(ide_drive_t *drive, int arg)
311{
312	if (arg < 0 || arg > 1)
313		return -EINVAL;
314
315	if (ide_spin_wait_hwgroup(drive))
316		return -EBUSY;
317	drive->keep_settings = arg;
318	spin_unlock_irq(&ide_lock);
319
320	return 0;
321}
322
323ide_devset_get(using_dma, using_dma);
324
325int set_using_dma(ide_drive_t *drive, int arg)
326{
327#ifdef CONFIG_BLK_DEV_IDEDMA
328	ide_hwif_t *hwif = drive->hwif;
329	int err = -EPERM;
330
331	if (arg < 0 || arg > 1)
332		return -EINVAL;
333
334	if (ata_id_has_dma(drive->id) == 0)
335		goto out;
336
337	if (hwif->dma_ops == NULL)
338		goto out;
339
340	err = -EBUSY;
341	if (ide_spin_wait_hwgroup(drive))
342		goto out;
343	/*
344	 * set ->busy flag, unlock and let it ride
345	 */
346	hwif->hwgroup->busy = 1;
347	spin_unlock_irq(&ide_lock);
348
349	err = 0;
350
351	if (arg) {
352		if (ide_set_dma(drive))
353			err = -EIO;
354	} else
355		ide_dma_off(drive);
356
357	/*
358	 * lock, clear ->busy flag and unlock before leaving
359	 */
360	spin_lock_irq(&ide_lock);
361	hwif->hwgroup->busy = 0;
362	spin_unlock_irq(&ide_lock);
363out:
364	return err;
365#else
366	if (arg < 0 || arg > 1)
367		return -EINVAL;
368
369	return -EPERM;
370#endif
371}
372
373int set_pio_mode(ide_drive_t *drive, int arg)
374{
375	struct request *rq;
376	ide_hwif_t *hwif = drive->hwif;
377	const struct ide_port_ops *port_ops = hwif->port_ops;
378
379	if (arg < 0 || arg > 255)
380		return -EINVAL;
381
382	if (port_ops == NULL || port_ops->set_pio_mode == NULL ||
383	    (hwif->host_flags & IDE_HFLAG_NO_SET_MODE))
384		return -ENOSYS;
385
386	if (drive->special.b.set_tune)
387		return -EBUSY;
388
389	rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
390	rq->cmd_type = REQ_TYPE_ATA_TASKFILE;
391
392	drive->tune_req = (u8) arg;
393	drive->special.b.set_tune = 1;
394
395	blk_execute_rq(drive->queue, NULL, rq, 0);
396	blk_put_request(rq);
397
398	return 0;
399}
400
401ide_devset_get(unmaskirq, unmask);
402
403int set_unmaskirq(ide_drive_t *drive, int arg)
404{
405	if (drive->no_unmask)
406		return -EPERM;
407
408	if (arg < 0 || arg > 1)
409		return -EINVAL;
410
411	if (ide_spin_wait_hwgroup(drive))
412		return -EBUSY;
413	drive->unmask = arg;
414	spin_unlock_irq(&ide_lock);
415
416	return 0;
417}
418
419static int generic_ide_suspend(struct device *dev, pm_message_t mesg)
420{
421	ide_drive_t *drive = dev->driver_data;
422	ide_hwif_t *hwif = HWIF(drive);
423	struct request *rq;
424	struct request_pm_state rqpm;
425	ide_task_t args;
426	int ret;
427
428	/* Call ACPI _GTM only once */
429	if (!(drive->dn % 2))
430		ide_acpi_get_timing(hwif);
431
432	memset(&rqpm, 0, sizeof(rqpm));
433	memset(&args, 0, sizeof(args));
434	rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
435	rq->cmd_type = REQ_TYPE_PM_SUSPEND;
436	rq->special = &args;
437	rq->data = &rqpm;
438	rqpm.pm_step = ide_pm_state_start_suspend;
439	if (mesg.event == PM_EVENT_PRETHAW)
440		mesg.event = PM_EVENT_FREEZE;
441	rqpm.pm_state = mesg.event;
442
443	ret = blk_execute_rq(drive->queue, NULL, rq, 0);
444	blk_put_request(rq);
445	/* only call ACPI _PS3 after both drivers are suspended */
446	if (!ret && (((drive->dn % 2) && hwif->drives[0].present
447		 && hwif->drives[1].present)
448		 || !hwif->drives[0].present
449		 || !hwif->drives[1].present))
450		ide_acpi_set_state(hwif, 0);
451	return ret;
452}
453
454static int generic_ide_resume(struct device *dev)
455{
456	ide_drive_t *drive = dev->driver_data;
457	ide_hwif_t *hwif = HWIF(drive);
458	struct request *rq;
459	struct request_pm_state rqpm;
460	ide_task_t args;
461	int err;
462
463	/* Call ACPI _STM only once */
464	if (!(drive->dn % 2)) {
465		ide_acpi_set_state(hwif, 1);
466		ide_acpi_push_timing(hwif);
467	}
468
469	ide_acpi_exec_tfs(drive);
470
471	memset(&rqpm, 0, sizeof(rqpm));
472	memset(&args, 0, sizeof(args));
473	rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
474	rq->cmd_type = REQ_TYPE_PM_RESUME;
475	rq->cmd_flags |= REQ_PREEMPT;
476	rq->special = &args;
477	rq->data = &rqpm;
478	rqpm.pm_step = ide_pm_state_start_resume;
479	rqpm.pm_state = PM_EVENT_ON;
480
481	err = blk_execute_rq(drive->queue, NULL, rq, 1);
482	blk_put_request(rq);
483
484	if (err == 0 && dev->driver) {
485		ide_driver_t *drv = to_ide_driver(dev->driver);
486
487		if (drv->resume)
488			drv->resume(drive);
489	}
490
491	return err;
492}
493
494static int generic_drive_reset(ide_drive_t *drive)
495{
496	struct request *rq;
497	int ret = 0;
498
499	rq = blk_get_request(drive->queue, READ, __GFP_WAIT);
500	rq->cmd_type = REQ_TYPE_SPECIAL;
501	rq->cmd_len = 1;
502	rq->cmd[0] = REQ_DRIVE_RESET;
503	rq->cmd_flags |= REQ_SOFTBARRIER;
504	if (blk_execute_rq(drive->queue, NULL, rq, 1))
505		ret = rq->errors;
506	blk_put_request(rq);
507	return ret;
508}
509
510static int ide_get_identity_ioctl(ide_drive_t *drive, unsigned int cmd,
511				  unsigned long arg)
512{
513	u16 *id = NULL;
514	int size = (cmd == HDIO_GET_IDENTITY) ? (ATA_ID_WORDS * 2) : 142;
515	int rc = 0;
516
517	if (drive->id_read == 0) {
518		rc = -ENOMSG;
519		goto out;
520	}
521
522	id = kmalloc(size, GFP_KERNEL);
523	if (id == NULL) {
524		rc = -ENOMEM;
525		goto out;
526	}
527
528	memcpy(id, drive->id, size);
529	ata_id_to_hd_driveid(id);
530
531	if (copy_to_user((void __user *)arg, id, size))
532		rc = -EFAULT;
533
534	kfree(id);
535out:
536	return rc;
537}
538
539static int ide_get_nice_ioctl(ide_drive_t *drive, unsigned long arg)
540{
541	return put_user((drive->dsc_overlap << IDE_NICE_DSC_OVERLAP) |
542			(drive->nice1 << IDE_NICE_1), (long __user *)arg);
543}
544
545static int ide_set_nice_ioctl(ide_drive_t *drive, unsigned long arg)
546{
547	if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
548		return -EPERM;
549
550	if (((arg >> IDE_NICE_DSC_OVERLAP) & 1) &&
551	    (drive->media == ide_disk || drive->media == ide_floppy ||
552	     drive->scsi))
553		return -EPERM;
554
555	drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1;
556	drive->nice1 = (arg >> IDE_NICE_1) & 1;
557
558	return 0;
559}
560
561int generic_ide_ioctl(ide_drive_t *drive, struct file *file, struct block_device *bdev,
562			unsigned int cmd, unsigned long arg)
563{
564	unsigned long flags;
565	int err = 0, (*getfunc)(ide_drive_t *), (*setfunc)(ide_drive_t *, int);
566
567	switch (cmd) {
568	case HDIO_GET_32BIT:	    getfunc = get_io_32bit;	 goto read_val;
569	case HDIO_GET_KEEPSETTINGS: getfunc = get_ksettings;	 goto read_val;
570	case HDIO_GET_UNMASKINTR:   getfunc = get_unmaskirq;	 goto read_val;
571	case HDIO_GET_DMA:	    getfunc = get_using_dma;	 goto read_val;
572	case HDIO_SET_32BIT:	    setfunc = set_io_32bit;	 goto set_val;
573	case HDIO_SET_KEEPSETTINGS: setfunc = set_ksettings;	 goto set_val;
574	case HDIO_SET_PIO_MODE:	    setfunc = set_pio_mode;	 goto set_val;
575	case HDIO_SET_UNMASKINTR:   setfunc = set_unmaskirq;	 goto set_val;
576	case HDIO_SET_DMA:	    setfunc = set_using_dma;	 goto set_val;
577	}
578
579	switch (cmd) {
580		case HDIO_OBSOLETE_IDENTITY:
581		case HDIO_GET_IDENTITY:
582			if (bdev != bdev->bd_contains)
583				return -EINVAL;
584			return ide_get_identity_ioctl(drive, cmd, arg);
585		case HDIO_GET_NICE:
586			return ide_get_nice_ioctl(drive, arg);
587#ifdef CONFIG_IDE_TASK_IOCTL
588		case HDIO_DRIVE_TASKFILE:
589		        if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
590				return -EACCES;
591			switch(drive->media) {
592				case ide_disk:
593					return ide_taskfile_ioctl(drive, cmd, arg);
594				default:
595					return -ENOMSG;
596			}
597#endif /* CONFIG_IDE_TASK_IOCTL */
598
599		case HDIO_DRIVE_CMD:
600			if (!capable(CAP_SYS_RAWIO))
601				return -EACCES;
602			return ide_cmd_ioctl(drive, cmd, arg);
603
604		case HDIO_DRIVE_TASK:
605			if (!capable(CAP_SYS_RAWIO))
606				return -EACCES;
607			return ide_task_ioctl(drive, cmd, arg);
608		case HDIO_SET_NICE:
609			if (!capable(CAP_SYS_ADMIN))
610				return -EACCES;
611			return ide_set_nice_ioctl(drive, arg);
612		case HDIO_DRIVE_RESET:
613			if (!capable(CAP_SYS_ADMIN))
614				return -EACCES;
615
616			return generic_drive_reset(drive);
617
618		case HDIO_GET_BUSSTATE:
619			if (!capable(CAP_SYS_ADMIN))
620				return -EACCES;
621			if (put_user(BUSSTATE_ON, (long __user *)arg))
622				return -EFAULT;
623			return 0;
624
625		case HDIO_SET_BUSSTATE:
626			if (!capable(CAP_SYS_ADMIN))
627				return -EACCES;
628			return -EOPNOTSUPP;
629		default:
630			return -EINVAL;
631	}
632
633read_val:
634	mutex_lock(&ide_setting_mtx);
635	spin_lock_irqsave(&ide_lock, flags);
636	err = getfunc(drive);
637	spin_unlock_irqrestore(&ide_lock, flags);
638	mutex_unlock(&ide_setting_mtx);
639	return err >= 0 ? put_user(err, (long __user *)arg) : err;
640
641set_val:
642	if (bdev != bdev->bd_contains)
643		err = -EINVAL;
644	else {
645		if (!capable(CAP_SYS_ADMIN))
646			err = -EACCES;
647		else {
648			mutex_lock(&ide_setting_mtx);
649			err = setfunc(drive, arg);
650			mutex_unlock(&ide_setting_mtx);
651		}
652	}
653	return err;
654}
655
656EXPORT_SYMBOL(generic_ide_ioctl);
657
658/**
659 * ide_device_get	-	get an additional reference to a ide_drive_t
660 * @drive:	device to get a reference to
661 *
662 * Gets a reference to the ide_drive_t and increments the use count of the
663 * underlying LLDD module.
664 */
665int ide_device_get(ide_drive_t *drive)
666{
667	struct device *host_dev;
668	struct module *module;
669
670	if (!get_device(&drive->gendev))
671		return -ENXIO;
672
673	host_dev = drive->hwif->host->dev[0];
674	module = host_dev ? host_dev->driver->owner : NULL;
675
676	if (module && !try_module_get(module)) {
677		put_device(&drive->gendev);
678		return -ENXIO;
679	}
680
681	return 0;
682}
683EXPORT_SYMBOL_GPL(ide_device_get);
684
685/**
686 * ide_device_put	-	release a reference to a ide_drive_t
687 * @drive:	device to release a reference on
688 *
689 * Release a reference to the ide_drive_t and decrements the use count of
690 * the underlying LLDD module.
691 */
692void ide_device_put(ide_drive_t *drive)
693{
694#ifdef CONFIG_MODULE_UNLOAD
695	struct device *host_dev = drive->hwif->host->dev[0];
696	struct module *module = host_dev ? host_dev->driver->owner : NULL;
697
698	if (module)
699		module_put(module);
700#endif
701	put_device(&drive->gendev);
702}
703EXPORT_SYMBOL_GPL(ide_device_put);
704
705static int ide_bus_match(struct device *dev, struct device_driver *drv)
706{
707	return 1;
708}
709
710static char *media_string(ide_drive_t *drive)
711{
712	switch (drive->media) {
713	case ide_disk:
714		return "disk";
715	case ide_cdrom:
716		return "cdrom";
717	case ide_tape:
718		return "tape";
719	case ide_floppy:
720		return "floppy";
721	case ide_optical:
722		return "optical";
723	default:
724		return "UNKNOWN";
725	}
726}
727
728static ssize_t media_show(struct device *dev, struct device_attribute *attr, char *buf)
729{
730	ide_drive_t *drive = to_ide_device(dev);
731	return sprintf(buf, "%s\n", media_string(drive));
732}
733
734static ssize_t drivename_show(struct device *dev, struct device_attribute *attr, char *buf)
735{
736	ide_drive_t *drive = to_ide_device(dev);
737	return sprintf(buf, "%s\n", drive->name);
738}
739
740static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
741{
742	ide_drive_t *drive = to_ide_device(dev);
743	return sprintf(buf, "ide:m-%s\n", media_string(drive));
744}
745
746static ssize_t model_show(struct device *dev, struct device_attribute *attr,
747			  char *buf)
748{
749	ide_drive_t *drive = to_ide_device(dev);
750	return sprintf(buf, "%s\n", (char *)&drive->id[ATA_ID_PROD]);
751}
752
753static ssize_t firmware_show(struct device *dev, struct device_attribute *attr,
754			     char *buf)
755{
756	ide_drive_t *drive = to_ide_device(dev);
757	return sprintf(buf, "%s\n", (char *)&drive->id[ATA_ID_FW_REV]);
758}
759
760static ssize_t serial_show(struct device *dev, struct device_attribute *attr,
761			   char *buf)
762{
763	ide_drive_t *drive = to_ide_device(dev);
764	return sprintf(buf, "%s\n", (char *)&drive->id[ATA_ID_SERNO]);
765}
766
767static struct device_attribute ide_dev_attrs[] = {
768	__ATTR_RO(media),
769	__ATTR_RO(drivename),
770	__ATTR_RO(modalias),
771	__ATTR_RO(model),
772	__ATTR_RO(firmware),
773	__ATTR(serial, 0400, serial_show, NULL),
774	__ATTR_NULL
775};
776
777static int ide_uevent(struct device *dev, struct kobj_uevent_env *env)
778{
779	ide_drive_t *drive = to_ide_device(dev);
780
781	add_uevent_var(env, "MEDIA=%s", media_string(drive));
782	add_uevent_var(env, "DRIVENAME=%s", drive->name);
783	add_uevent_var(env, "MODALIAS=ide:m-%s", media_string(drive));
784	return 0;
785}
786
787static int generic_ide_probe(struct device *dev)
788{
789	ide_drive_t *drive = to_ide_device(dev);
790	ide_driver_t *drv = to_ide_driver(dev->driver);
791
792	return drv->probe ? drv->probe(drive) : -ENODEV;
793}
794
795static int generic_ide_remove(struct device *dev)
796{
797	ide_drive_t *drive = to_ide_device(dev);
798	ide_driver_t *drv = to_ide_driver(dev->driver);
799
800	if (drv->remove)
801		drv->remove(drive);
802
803	return 0;
804}
805
806static void generic_ide_shutdown(struct device *dev)
807{
808	ide_drive_t *drive = to_ide_device(dev);
809	ide_driver_t *drv = to_ide_driver(dev->driver);
810
811	if (dev->driver && drv->shutdown)
812		drv->shutdown(drive);
813}
814
815struct bus_type ide_bus_type = {
816	.name		= "ide",
817	.match		= ide_bus_match,
818	.uevent		= ide_uevent,
819	.probe		= generic_ide_probe,
820	.remove		= generic_ide_remove,
821	.shutdown	= generic_ide_shutdown,
822	.dev_attrs	= ide_dev_attrs,
823	.suspend	= generic_ide_suspend,
824	.resume		= generic_ide_resume,
825};
826
827EXPORT_SYMBOL_GPL(ide_bus_type);
828
829int ide_vlb_clk;
830EXPORT_SYMBOL_GPL(ide_vlb_clk);
831
832module_param_named(vlb_clock, ide_vlb_clk, int, 0);
833MODULE_PARM_DESC(vlb_clock, "VLB clock frequency (in MHz)");
834
835int ide_pci_clk;
836EXPORT_SYMBOL_GPL(ide_pci_clk);
837
838module_param_named(pci_clock, ide_pci_clk, int, 0);
839MODULE_PARM_DESC(pci_clock, "PCI bus clock frequency (in MHz)");
840
841static int ide_set_dev_param_mask(const char *s, struct kernel_param *kp)
842{
843	int a, b, i, j = 1;
844	unsigned int *dev_param_mask = (unsigned int *)kp->arg;
845
846	if (sscanf(s, "%d.%d:%d", &a, &b, &j) != 3 &&
847	    sscanf(s, "%d.%d", &a, &b) != 2)
848		return -EINVAL;
849
850	i = a * MAX_DRIVES + b;
851
852	if (i >= MAX_HWIFS * MAX_DRIVES || j < 0 || j > 1)
853		return -EINVAL;
854
855	if (j)
856		*dev_param_mask |= (1 << i);
857	else
858		*dev_param_mask &= (1 << i);
859
860	return 0;
861}
862
863static unsigned int ide_nodma;
864
865module_param_call(nodma, ide_set_dev_param_mask, NULL, &ide_nodma, 0);
866MODULE_PARM_DESC(nodma, "disallow DMA for a device");
867
868static unsigned int ide_noflush;
869
870module_param_call(noflush, ide_set_dev_param_mask, NULL, &ide_noflush, 0);
871MODULE_PARM_DESC(noflush, "disable flush requests for a device");
872
873static unsigned int ide_noprobe;
874
875module_param_call(noprobe, ide_set_dev_param_mask, NULL, &ide_noprobe, 0);
876MODULE_PARM_DESC(noprobe, "skip probing for a device");
877
878static unsigned int ide_nowerr;
879
880module_param_call(nowerr, ide_set_dev_param_mask, NULL, &ide_nowerr, 0);
881MODULE_PARM_DESC(nowerr, "ignore the ATA_DF bit for a device");
882
883static unsigned int ide_cdroms;
884
885module_param_call(cdrom, ide_set_dev_param_mask, NULL, &ide_cdroms, 0);
886MODULE_PARM_DESC(cdrom, "force device as a CD-ROM");
887
888struct chs_geom {
889	unsigned int	cyl;
890	u8		head;
891	u8		sect;
892};
893
894static unsigned int ide_disks;
895static struct chs_geom ide_disks_chs[MAX_HWIFS * MAX_DRIVES];
896
897static int ide_set_disk_chs(const char *str, struct kernel_param *kp)
898{
899	int a, b, c = 0, h = 0, s = 0, i, j = 1;
900
901	if (sscanf(str, "%d.%d:%d,%d,%d", &a, &b, &c, &h, &s) != 5 &&
902	    sscanf(str, "%d.%d:%d", &a, &b, &j) != 3)
903		return -EINVAL;
904
905	i = a * MAX_DRIVES + b;
906
907	if (i >= MAX_HWIFS * MAX_DRIVES || j < 0 || j > 1)
908		return -EINVAL;
909
910	if (c > INT_MAX || h > 255 || s > 255)
911		return -EINVAL;
912
913	if (j)
914		ide_disks |= (1 << i);
915	else
916		ide_disks &= (1 << i);
917
918	ide_disks_chs[i].cyl  = c;
919	ide_disks_chs[i].head = h;
920	ide_disks_chs[i].sect = s;
921
922	return 0;
923}
924
925module_param_call(chs, ide_set_disk_chs, NULL, NULL, 0);
926MODULE_PARM_DESC(chs, "force device as a disk (using CHS)");
927
928static void ide_dev_apply_params(ide_drive_t *drive)
929{
930	int i = drive->hwif->index * MAX_DRIVES + drive->select.b.unit;
931
932	if (ide_nodma & (1 << i)) {
933		printk(KERN_INFO "ide: disallowing DMA for %s\n", drive->name);
934		drive->nodma = 1;
935	}
936	if (ide_noflush & (1 << i)) {
937		printk(KERN_INFO "ide: disabling flush requests for %s\n",
938				 drive->name);
939		drive->noflush = 1;
940	}
941	if (ide_noprobe & (1 << i)) {
942		printk(KERN_INFO "ide: skipping probe for %s\n", drive->name);
943		drive->noprobe = 1;
944	}
945	if (ide_nowerr & (1 << i)) {
946		printk(KERN_INFO "ide: ignoring the ATA_DF bit for %s\n",
947				 drive->name);
948		drive->bad_wstat = BAD_R_STAT;
949	}
950	if (ide_cdroms & (1 << i)) {
951		printk(KERN_INFO "ide: forcing %s as a CD-ROM\n", drive->name);
952		drive->present = 1;
953		drive->media = ide_cdrom;
954		/* an ATAPI device ignores DRDY */
955		drive->ready_stat = 0;
956	}
957	if (ide_disks & (1 << i)) {
958		drive->cyl  = drive->bios_cyl  = ide_disks_chs[i].cyl;
959		drive->head = drive->bios_head = ide_disks_chs[i].head;
960		drive->sect = drive->bios_sect = ide_disks_chs[i].sect;
961		drive->forced_geom = 1;
962		printk(KERN_INFO "ide: forcing %s as a disk (%d/%d/%d)\n",
963				 drive->name,
964				 drive->cyl, drive->head, drive->sect);
965		drive->present = 1;
966		drive->media = ide_disk;
967		drive->ready_stat = ATA_DRDY;
968	}
969}
970
971static unsigned int ide_ignore_cable;
972
973static int ide_set_ignore_cable(const char *s, struct kernel_param *kp)
974{
975	int i, j = 1;
976
977	if (sscanf(s, "%d:%d", &i, &j) != 2 && sscanf(s, "%d", &i) != 1)
978		return -EINVAL;
979
980	if (i >= MAX_HWIFS || j < 0 || j > 1)
981		return -EINVAL;
982
983	if (j)
984		ide_ignore_cable |= (1 << i);
985	else
986		ide_ignore_cable &= (1 << i);
987
988	return 0;
989}
990
991module_param_call(ignore_cable, ide_set_ignore_cable, NULL, NULL, 0);
992MODULE_PARM_DESC(ignore_cable, "ignore cable detection");
993
994void ide_port_apply_params(ide_hwif_t *hwif)
995{
996	int i;
997
998	if (ide_ignore_cable & (1 << hwif->index)) {
999		printk(KERN_INFO "ide: ignoring cable detection for %s\n",
1000				 hwif->name);
1001		hwif->cbl = ATA_CBL_PATA40_SHORT;
1002	}
1003
1004	for (i = 0; i < MAX_DRIVES; i++)
1005		ide_dev_apply_params(&hwif->drives[i]);
1006}
1007
1008/*
1009 * This is gets invoked once during initialization, to set *everything* up
1010 */
1011static int __init ide_init(void)
1012{
1013	int ret;
1014
1015	printk(KERN_INFO "Uniform Multi-Platform E-IDE driver\n");
1016
1017	ret = bus_register(&ide_bus_type);
1018	if (ret < 0) {
1019		printk(KERN_WARNING "IDE: bus_register error: %d\n", ret);
1020		return ret;
1021	}
1022
1023	ide_port_class = class_create(THIS_MODULE, "ide_port");
1024	if (IS_ERR(ide_port_class)) {
1025		ret = PTR_ERR(ide_port_class);
1026		goto out_port_class;
1027	}
1028
1029	proc_ide_create();
1030
1031	return 0;
1032
1033out_port_class:
1034	bus_unregister(&ide_bus_type);
1035
1036	return ret;
1037}
1038
1039static void __exit ide_exit(void)
1040{
1041	proc_ide_destroy();
1042
1043	class_destroy(ide_port_class);
1044
1045	bus_unregister(&ide_bus_type);
1046}
1047
1048module_init(ide_init);
1049module_exit(ide_exit);
1050
1051MODULE_LICENSE("GPL");
1052