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