PageRenderTime 122ms CodeModel.GetById 13ms app.highlight 95ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/ide/ide.c

https://bitbucket.org/evzijst/gittest
C | 2269 lines | 1497 code | 248 blank | 524 comment | 288 complexity | ad6648959e9d0aa0c596b8a7bec46749 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

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

Large files files are truncated, but you can click here to view the full file