linux-bk/drivers/ide/ide.c
<<
>>
Prefs
   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
 199extern ide_driver_t idedefault_driver;
 200static void setup_driver_defaults(ide_driver_t *driver);
 201
 202/*
 203 * Do not even *think* about calling this!
 204 */
 205static void init_hwif_data(ide_hwif_t *hwif, unsigned int index)
 206{
 207        unsigned int unit;
 208
 209        /* bulk initialize hwif & drive info with zeros */
 210        memset(hwif, 0, sizeof(ide_hwif_t));
 211
 212        /* fill in any non-zero initial values */
 213        hwif->index     = index;
 214        hwif->major     = ide_hwif_to_major[index];
 215
 216        hwif->name[0]   = 'i';
 217        hwif->name[1]   = 'd';
 218        hwif->name[2]   = 'e';
 219        hwif->name[3]   = '0' + index;
 220
 221        hwif->bus_state = BUSSTATE_ON;
 222
 223        hwif->atapi_dma = 0;            /* disable all atapi dma */ 
 224        hwif->ultra_mask = 0x80;        /* disable all ultra */
 225        hwif->mwdma_mask = 0x80;        /* disable all mwdma */
 226        hwif->swdma_mask = 0x80;        /* disable all swdma */
 227
 228        sema_init(&hwif->gendev_rel_sem, 0);
 229
 230        default_hwif_iops(hwif);
 231        default_hwif_transport(hwif);
 232        for (unit = 0; unit < MAX_DRIVES; ++unit) {
 233                ide_drive_t *drive = &hwif->drives[unit];
 234
 235                drive->media                    = ide_disk;
 236                drive->select.all               = (unit<<4)|0xa0;
 237                drive->hwif                     = hwif;
 238                drive->ctl                      = 0x08;
 239                drive->ready_stat               = READY_STAT;
 240                drive->bad_wstat                = BAD_W_STAT;
 241                drive->special.b.recalibrate    = 1;
 242                drive->special.b.set_geometry   = 1;
 243                drive->name[0]                  = 'h';
 244                drive->name[1]                  = 'd';
 245                drive->name[2]                  = 'a' + (index * MAX_DRIVES) + unit;
 246                drive->max_failures             = IDE_DEFAULT_MAX_FAILURES;
 247                drive->using_dma                = 0;
 248                drive->is_flash                 = 0;
 249                drive->driver                   = &idedefault_driver;
 250                drive->vdma                     = 0;
 251                INIT_LIST_HEAD(&drive->list);
 252                sema_init(&drive->gendev_rel_sem, 0);
 253        }
 254}
 255
 256static void init_hwif_default(ide_hwif_t *hwif, unsigned int index)
 257{
 258        hw_regs_t hw;
 259
 260        memset(&hw, 0, sizeof(hw_regs_t));
 261
 262        ide_init_hwif_ports(&hw, ide_default_io_base(index), 0, &hwif->irq);
 263
 264        memcpy(&hwif->hw, &hw, sizeof(hw));
 265        memcpy(hwif->io_ports, hw.io_ports, sizeof(hw.io_ports));
 266
 267        hwif->noprobe = !hwif->io_ports[IDE_DATA_OFFSET];
 268#ifdef CONFIG_BLK_DEV_HD
 269        if (hwif->io_ports[IDE_DATA_OFFSET] == HD_DATA)
 270                hwif->noprobe = 1;      /* may be overridden by ide_setup() */
 271#endif
 272}
 273
 274extern void ide_arm_init(void);
 275
 276/*
 277 * init_ide_data() sets reasonable default values into all fields
 278 * of all instances of the hwifs and drives, but only on the first call.
 279 * Subsequent calls have no effect (they don't wipe out anything).
 280 *
 281 * This routine is normally called at driver initialization time,
 282 * but may also be called MUCH earlier during kernel "command-line"
 283 * parameter processing.  As such, we cannot depend on any other parts
 284 * of the kernel (such as memory allocation) to be functioning yet.
 285 *
 286 * This is too bad, as otherwise we could dynamically allocate the
 287 * ide_drive_t structs as needed, rather than always consuming memory
 288 * for the max possible number (MAX_HWIFS * MAX_DRIVES) of them.
 289 *
 290 * FIXME: We should stuff the setup data into __init and copy the
 291 * relevant hwifs/allocate them properly during boot.
 292 */
 293#define MAGIC_COOKIE 0x12345678
 294static void __init init_ide_data (void)
 295{
 296        ide_hwif_t *hwif;
 297        unsigned int index;
 298        static unsigned long magic_cookie = MAGIC_COOKIE;
 299
 300        if (magic_cookie != MAGIC_COOKIE)
 301                return;         /* already initialized */
 302        magic_cookie = 0;
 303
 304        setup_driver_defaults(&idedefault_driver);
 305
 306        /* Initialise all interface structures */
 307        for (index = 0; index < MAX_HWIFS; ++index) {
 308                hwif = &ide_hwifs[index];
 309                init_hwif_data(hwif, index);
 310                init_hwif_default(hwif, index);
 311#if !defined(CONFIG_PPC32) || !defined(CONFIG_PCI)
 312                hwif->irq = hwif->hw.irq =
 313                        ide_init_default_irq(hwif->io_ports[IDE_DATA_OFFSET]);
 314#endif
 315        }
 316#ifdef CONFIG_IDE_ARM
 317        initializing = 1;
 318        ide_arm_init();
 319        initializing = 0;
 320#endif
 321}
 322
 323/**
 324 *      ide_system_bus_speed    -       guess bus speed
 325 *
 326 *      ide_system_bus_speed() returns what we think is the system VESA/PCI
 327 *      bus speed (in MHz). This is used for calculating interface PIO timings.
 328 *      The default is 40 for known PCI systems, 50 otherwise.
 329 *      The "idebus=xx" parameter can be used to override this value.
 330 *      The actual value to be used is computed/displayed the first time
 331 *      through. Drivers should only use this as a last resort.
 332 *
 333 *      Returns a guessed speed in MHz.
 334 */
 335
 336static int ide_system_bus_speed(void)
 337{
 338#ifdef CONFIG_PCI
 339        static struct pci_device_id pci_default[] = {
 340                { PCI_DEVICE(PCI_ANY_ID, PCI_ANY_ID) },
 341                { }
 342        };
 343#else
 344#define pci_default 0
 345#endif /* CONFIG_PCI */
 346
 347        if (!system_bus_speed) {
 348                if (idebus_parameter) {
 349                        /* user supplied value */
 350                        system_bus_speed = idebus_parameter;
 351                } else if (pci_dev_present(pci_default)) {
 352                        /* safe default value for PCI */
 353                        system_bus_speed = 33;
 354                } else {
 355                        /* safe default value for VESA and PCI */
 356                        system_bus_speed = 50;
 357                }
 358                printk(KERN_INFO "ide: Assuming %dMHz system bus speed "
 359                        "for PIO modes%s\n", system_bus_speed,
 360                        idebus_parameter ? "" : "; override with idebus=xx");
 361        }
 362        return system_bus_speed;
 363}
 364
 365static int ide_open (struct inode * inode, struct file * filp)
 366{
 367        return -ENXIO;
 368}
 369
 370/*
 371 *      drives_lock protects the list of drives, drivers_lock the
 372 *      list of drivers.  Currently nobody takes both at once.
 373 */
 374
 375static DEFINE_SPINLOCK(drives_lock);
 376static DEFINE_SPINLOCK(drivers_lock);
 377static LIST_HEAD(drivers);
 378
 379/* Iterator for the driver list. */
 380
 381static void *m_start(struct seq_file *m, loff_t *pos)
 382{
 383        struct list_head *p;
 384        loff_t l = *pos;
 385        spin_lock(&drivers_lock);
 386        list_for_each(p, &drivers)
 387                if (!l--)
 388                        return list_entry(p, ide_driver_t, drivers);
 389        return NULL;
 390}
 391
 392static void *m_next(struct seq_file *m, void *v, loff_t *pos)
 393{
 394        struct list_head *p = ((ide_driver_t *)v)->drivers.next;
 395        (*pos)++;
 396        return p==&drivers ? NULL : list_entry(p, ide_driver_t, drivers);
 397}
 398
 399static void m_stop(struct seq_file *m, void *v)
 400{
 401        spin_unlock(&drivers_lock);
 402}
 403
 404static int show_driver(struct seq_file *m, void *v)
 405{
 406        ide_driver_t *driver = v;
 407        seq_printf(m, "%s version %s\n", driver->name, driver->version);
 408        return 0;
 409}
 410
 411struct seq_operations ide_drivers_op = {
 412        .start  = m_start,
 413        .next   = m_next,
 414        .stop   = m_stop,
 415        .show   = show_driver
 416};
 417
 418#ifdef CONFIG_PROC_FS
 419struct proc_dir_entry *proc_ide_root;
 420
 421static ide_proc_entry_t generic_subdriver_entries[] = {
 422        { "capacity",   S_IFREG|S_IRUGO,        proc_ide_read_capacity, NULL },
 423        { NULL, 0, NULL, NULL }
 424};
 425#endif
 426
 427static struct resource* hwif_request_region(ide_hwif_t *hwif,
 428                                            unsigned long addr, int num)
 429{
 430        struct resource *res = request_region(addr, num, hwif->name);
 431
 432        if (!res)
 433                printk(KERN_ERR "%s: I/O resource 0x%lX-0x%lX not free.\n",
 434                                hwif->name, addr, addr+num-1);
 435        return res;
 436}
 437
 438/**
 439 *      ide_hwif_request_regions - request resources for IDE
 440 *      @hwif: interface to use
 441 *
 442 *      Requests all the needed resources for an interface.
 443 *      Right now core IDE code does this work which is deeply wrong.
 444 *      MMIO leaves it to the controller driver,
 445 *      PIO will migrate this way over time.
 446 */
 447
 448int ide_hwif_request_regions(ide_hwif_t *hwif)
 449{
 450        unsigned long addr;
 451        unsigned int i;
 452
 453        if (hwif->mmio == 2)
 454                return 0;
 455        BUG_ON(hwif->mmio == 1);
 456        addr = hwif->io_ports[IDE_CONTROL_OFFSET];
 457        if (addr && !hwif_request_region(hwif, addr, 1))
 458                goto control_region_busy;
 459        hwif->straight8 = 0;
 460        addr = hwif->io_ports[IDE_DATA_OFFSET];
 461        if ((addr | 7) == hwif->io_ports[IDE_STATUS_OFFSET]) {
 462                if (!hwif_request_region(hwif, addr, 8))
 463                        goto data_region_busy;
 464                hwif->straight8 = 1;
 465                return 0;
 466        }
 467        for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++) {
 468                addr = hwif->io_ports[i];
 469                if (!hwif_request_region(hwif, addr, 1)) {
 470                        while (--i)
 471                                release_region(addr, 1);
 472                        goto data_region_busy;
 473                }
 474        }
 475        return 0;
 476
 477data_region_busy:
 478        addr = hwif->io_ports[IDE_CONTROL_OFFSET];
 479        if (addr)
 480                release_region(addr, 1);
 481control_region_busy:
 482        /* If any errors are return, we drop the hwif interface. */
 483        return -EBUSY;
 484}
 485
 486/**
 487 *      ide_hwif_release_regions - free IDE resources
 488 *
 489 *      Note that we only release the standard ports,
 490 *      and do not even try to handle any extra ports
 491 *      allocated for weird IDE interface chipsets.
 492 *
 493 *      Note also that we don't yet handle mmio resources here. More
 494 *      importantly our caller should be doing this so we need to 
 495 *      restructure this as a helper function for drivers.
 496 */
 497
 498void ide_hwif_release_regions(ide_hwif_t *hwif)
 499{
 500        u32 i = 0;
 501
 502        if (hwif->mmio == 2)
 503                return;
 504        if (hwif->io_ports[IDE_CONTROL_OFFSET])
 505                release_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1);
 506        if (hwif->straight8) {
 507                release_region(hwif->io_ports[IDE_DATA_OFFSET], 8);
 508                return;
 509        }
 510        for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++)
 511                if (hwif->io_ports[i])
 512                        release_region(hwif->io_ports[i], 1);
 513}
 514
 515/**
 516 *      ide_hwif_restore        -       restore hwif to template
 517 *      @hwif: hwif to update
 518 *      @tmp_hwif: template
 519 *
 520 *      Restore hwif to a previous state by copying most settngs
 521 *      from the template.
 522 */
 523
 524static void ide_hwif_restore(ide_hwif_t *hwif, ide_hwif_t *tmp_hwif)
 525{
 526        hwif->hwgroup                   = tmp_hwif->hwgroup;
 527
 528        hwif->gendev.parent             = tmp_hwif->gendev.parent;
 529
 530        hwif->proc                      = tmp_hwif->proc;
 531
 532        hwif->major                     = tmp_hwif->major;
 533        hwif->straight8                 = tmp_hwif->straight8;
 534        hwif->bus_state                 = tmp_hwif->bus_state;
 535
 536        hwif->atapi_dma                 = tmp_hwif->atapi_dma;
 537        hwif->ultra_mask                = tmp_hwif->ultra_mask;
 538        hwif->mwdma_mask                = tmp_hwif->mwdma_mask;
 539        hwif->swdma_mask                = tmp_hwif->swdma_mask;
 540
 541        hwif->chipset                   = tmp_hwif->chipset;
 542        hwif->hold                      = tmp_hwif->hold;
 543
 544#ifdef CONFIG_BLK_DEV_IDEPCI
 545        hwif->pci_dev                   = tmp_hwif->pci_dev;
 546        hwif->cds                       = tmp_hwif->cds;
 547#endif
 548
 549        hwif->tuneproc                  = tmp_hwif->tuneproc;
 550        hwif->speedproc                 = tmp_hwif->speedproc;
 551        hwif->selectproc                = tmp_hwif->selectproc;
 552        hwif->reset_poll                = tmp_hwif->reset_poll;
 553        hwif->pre_reset                 = tmp_hwif->pre_reset;
 554        hwif->resetproc                 = tmp_hwif->resetproc;
 555        hwif->intrproc                  = tmp_hwif->intrproc;
 556        hwif->maskproc                  = tmp_hwif->maskproc;
 557        hwif->quirkproc                 = tmp_hwif->quirkproc;
 558        hwif->busproc                   = tmp_hwif->busproc;
 559
 560        hwif->ata_input_data            = tmp_hwif->ata_input_data;
 561        hwif->ata_output_data           = tmp_hwif->ata_output_data;
 562        hwif->atapi_input_bytes         = tmp_hwif->atapi_input_bytes;
 563        hwif->atapi_output_bytes        = tmp_hwif->atapi_output_bytes;
 564
 565        hwif->dma_setup                 = tmp_hwif->dma_setup;
 566        hwif->dma_exec_cmd              = tmp_hwif->dma_exec_cmd;
 567        hwif->dma_start                 = tmp_hwif->dma_start;
 568        hwif->ide_dma_end               = tmp_hwif->ide_dma_end;
 569        hwif->ide_dma_check             = tmp_hwif->ide_dma_check;
 570        hwif->ide_dma_on                = tmp_hwif->ide_dma_on;
 571        hwif->ide_dma_off_quietly       = tmp_hwif->ide_dma_off_quietly;
 572        hwif->ide_dma_test_irq          = tmp_hwif->ide_dma_test_irq;
 573        hwif->ide_dma_host_on           = tmp_hwif->ide_dma_host_on;
 574        hwif->ide_dma_host_off          = tmp_hwif->ide_dma_host_off;
 575        hwif->ide_dma_lostirq           = tmp_hwif->ide_dma_lostirq;
 576        hwif->ide_dma_timeout           = tmp_hwif->ide_dma_timeout;
 577
 578        hwif->OUTB                      = tmp_hwif->OUTB;
 579        hwif->OUTBSYNC                  = tmp_hwif->OUTBSYNC;
 580        hwif->OUTW                      = tmp_hwif->OUTW;
 581        hwif->OUTL                      = tmp_hwif->OUTL;
 582        hwif->OUTSW                     = tmp_hwif->OUTSW;
 583        hwif->OUTSL                     = tmp_hwif->OUTSL;
 584
 585        hwif->INB                       = tmp_hwif->INB;
 586        hwif->INW                       = tmp_hwif->INW;
 587        hwif->INL                       = tmp_hwif->INL;
 588        hwif->INSW                      = tmp_hwif->INSW;
 589        hwif->INSL                      = tmp_hwif->INSL;
 590
 591        hwif->sg_max_nents              = tmp_hwif->sg_max_nents;
 592
 593        hwif->mmio                      = tmp_hwif->mmio;
 594        hwif->rqsize                    = tmp_hwif->rqsize;
 595        hwif->no_lba48                  = tmp_hwif->no_lba48;
 596
 597#ifndef CONFIG_BLK_DEV_IDECS
 598        hwif->irq                       = tmp_hwif->irq;
 599#endif
 600
 601        hwif->dma_base                  = tmp_hwif->dma_base;
 602        hwif->dma_master                = tmp_hwif->dma_master;
 603        hwif->dma_command               = tmp_hwif->dma_command;
 604        hwif->dma_vendor1               = tmp_hwif->dma_vendor1;
 605        hwif->dma_status                = tmp_hwif->dma_status;
 606        hwif->dma_vendor3               = tmp_hwif->dma_vendor3;
 607        hwif->dma_prdtable              = tmp_hwif->dma_prdtable;
 608
 609        hwif->dma_extra                 = tmp_hwif->dma_extra;
 610        hwif->config_data               = tmp_hwif->config_data;
 611        hwif->select_data               = tmp_hwif->select_data;
 612        hwif->autodma                   = tmp_hwif->autodma;
 613        hwif->udma_four                 = tmp_hwif->udma_four;
 614        hwif->no_dsc                    = tmp_hwif->no_dsc;
 615
 616        hwif->hwif_data                 = tmp_hwif->hwif_data;
 617}
 618
 619/**
 620 *      ide_unregister          -       free an ide interface
 621 *      @index: index of interface (will change soon to a pointer)
 622 *
 623 *      Perform the final unregister of an IDE interface. At the moment
 624 *      we don't refcount interfaces so this will also get split up.
 625 *
 626 *      Locking:
 627 *      The caller must not hold the IDE locks
 628 *      The drive present/vanishing is not yet properly locked
 629 *      Take care with the callbacks. These have been split to avoid
 630 *      deadlocking the IDE layer. The shutdown callback is called
 631 *      before we take the lock and free resources. It is up to the
 632 *      caller to be sure there is no pending I/O here, and that
 633 *      the interfce will not be reopened (present/vanishing locking
 634 *      isnt yet done btw). After we commit to the final kill we
 635 *      call the cleanup callback with the ide locks held.
 636 *
 637 *      Unregister restores the hwif structures to the default state.
 638 *      This is raving bonkers.
 639 */
 640
 641void ide_unregister(unsigned int index)
 642{
 643        ide_drive_t *drive;
 644        ide_hwif_t *hwif, *g;
 645        static ide_hwif_t tmp_hwif; /* protected by ide_cfg_sem */
 646        ide_hwgroup_t *hwgroup;
 647        int irq_count = 0, unit, i;
 648
 649        BUG_ON(index >= MAX_HWIFS);
 650
 651        BUG_ON(in_interrupt());
 652        BUG_ON(irqs_disabled());
 653        down(&ide_cfg_sem);
 654        spin_lock_irq(&ide_lock);
 655        hwif = &ide_hwifs[index];
 656        if (!hwif->present)
 657                goto abort;
 658        for (unit = 0; unit < MAX_DRIVES; ++unit) {
 659                drive = &hwif->drives[unit];
 660                if (!drive->present)
 661                        continue;
 662                if (drive->usage || DRIVER(drive)->busy)
 663                        goto abort;
 664                drive->dead = 1;
 665        }
 666        hwif->present = 0;
 667
 668        spin_unlock_irq(&ide_lock);
 669
 670        for (unit = 0; unit < MAX_DRIVES; ++unit) {
 671                drive = &hwif->drives[unit];
 672                if (!drive->present)
 673                        continue;
 674                DRIVER(drive)->cleanup(drive);
 675        }
 676
 677        destroy_proc_ide_interface(hwif);
 678
 679        hwgroup = hwif->hwgroup;
 680        /*
 681         * free the irq if we were the only hwif using it
 682         */
 683        g = hwgroup->hwif;
 684        do {
 685                if (g->irq == hwif->irq)
 686                        ++irq_count;
 687                g = g->next;
 688        } while (g != hwgroup->hwif);
 689        if (irq_count == 1)
 690                free_irq(hwif->irq, hwgroup);
 691
 692        spin_lock_irq(&ide_lock);
 693        /*
 694         * Note that we only release the standard ports,
 695         * and do not even try to handle any extra ports
 696         * allocated for weird IDE interface chipsets.
 697         */
 698        ide_hwif_release_regions(hwif);
 699
 700        /*
 701         * Remove us from the hwgroup, and free
 702         * the hwgroup if we were the only member
 703         */
 704        for (i = 0; i < MAX_DRIVES; ++i) {
 705                drive = &hwif->drives[i];
 706                if (drive->devfs_name[0] != '\0') {
 707                        devfs_remove(drive->devfs_name);
 708                        drive->devfs_name[0] = '\0';
 709                }
 710                if (!drive->present)
 711                        continue;
 712                if (drive == drive->next) {
 713                        /* special case: last drive from hwgroup. */
 714                        BUG_ON(hwgroup->drive != drive);
 715                        hwgroup->drive = NULL;
 716                } else {
 717                        ide_drive_t *walk;
 718
 719                        walk = hwgroup->drive;
 720                        while (walk->next != drive)
 721                                walk = walk->next;
 722                        walk->next = drive->next;
 723                        if (hwgroup->drive == drive) {
 724                                hwgroup->drive = drive->next;
 725                                hwgroup->hwif = HWIF(hwgroup->drive);
 726                        }
 727                }
 728                BUG_ON(hwgroup->drive == drive);
 729                if (drive->id != NULL) {
 730                        kfree(drive->id);
 731                        drive->id = NULL;
 732                }
 733                drive->present = 0;
 734                /* Messed up locking ... */
 735                spin_unlock_irq(&ide_lock);
 736                blk_cleanup_queue(drive->queue);
 737                device_unregister(&drive->gendev);
 738                down(&drive->gendev_rel_sem);
 739                spin_lock_irq(&ide_lock);
 740                drive->queue = NULL;
 741        }
 742        if (hwif->next == hwif) {
 743                BUG_ON(hwgroup->hwif != hwif);
 744                kfree(hwgroup);
 745        } else {
 746                /* There is another interface in hwgroup.
 747                 * Unlink us, and set hwgroup->drive and ->hwif to
 748                 * something sane.
 749                 */
 750                g = hwgroup->hwif;
 751                while (g->next != hwif)
 752                        g = g->next;
 753                g->next = hwif->next;
 754                if (hwgroup->hwif == hwif) {
 755                        /* Chose a random hwif for hwgroup->hwif.
 756                         * It's guaranteed that there are no drives
 757                         * left in the hwgroup.
 758                         */
 759                        BUG_ON(hwgroup->drive != NULL);
 760                        hwgroup->hwif = g;
 761                }
 762                BUG_ON(hwgroup->hwif == hwif);
 763        }
 764
 765        /* More messed up locking ... */
 766        spin_unlock_irq(&ide_lock);
 767        device_unregister(&hwif->gendev);
 768        down(&hwif->gendev_rel_sem);
 769
 770        /*
 771         * Remove us from the kernel's knowledge
 772         */
 773        blk_unregister_region(MKDEV(hwif->major, 0), MAX_DRIVES<<PARTN_BITS);
 774        for (i = 0; i < MAX_DRIVES; i++) {
 775                struct gendisk *disk = hwif->drives[i].disk;
 776                hwif->drives[i].disk = NULL;
 777                put_disk(disk);
 778        }
 779        kfree(hwif->sg_table);
 780        unregister_blkdev(hwif->major, hwif->name);
 781        spin_lock_irq(&ide_lock);
 782
 783        if (hwif->dma_base) {
 784                (void) ide_release_dma(hwif);
 785
 786                hwif->dma_base = 0;
 787                hwif->dma_master = 0;
 788                hwif->dma_command = 0;
 789                hwif->dma_vendor1 = 0;
 790                hwif->dma_status = 0;
 791                hwif->dma_vendor3 = 0;
 792                hwif->dma_prdtable = 0;
 793        }
 794
 795        /* copy original settings */
 796        tmp_hwif = *hwif;
 797
 798        /* restore hwif data to pristine status */
 799        init_hwif_data(hwif, index);
 800        init_hwif_default(hwif, index);
 801
 802        ide_hwif_restore(hwif, &tmp_hwif);
 803
 804abort:
 805        spin_unlock_irq(&ide_lock);
 806        up(&ide_cfg_sem);
 807}
 808
 809EXPORT_SYMBOL(ide_unregister);
 810
 811
 812/**
 813 *      ide_setup_ports         -       set up IDE interface ports
 814 *      @hw: register descriptions
 815 *      @base: base register
 816 *      @offsets: table of register offsets
 817 *      @ctrl: control register
 818 *      @ack_irq: IRQ ack
 819 *      @irq: interrupt lie
 820 *
 821 *      Setup hw_regs_t structure described by parameters.  You
 822 *      may set up the hw structure yourself OR use this routine to
 823 *      do it for you. This is basically a helper
 824 *
 825 */
 826 
 827void ide_setup_ports (  hw_regs_t *hw,
 828                        unsigned long base, int *offsets,
 829                        unsigned long ctrl, unsigned long intr,
 830                        ide_ack_intr_t *ack_intr,
 831/*
 832 *                      ide_io_ops_t *iops,
 833 */
 834                        int irq)
 835{
 836        int i;
 837
 838        for (i = 0; i < IDE_NR_PORTS; i++) {
 839                if (offsets[i] == -1) {
 840                        switch(i) {
 841                                case IDE_CONTROL_OFFSET:
 842                                        hw->io_ports[i] = ctrl;
 843                                        break;
 844#if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
 845                                case IDE_IRQ_OFFSET:
 846                                        hw->io_ports[i] = intr;
 847                                        break;
 848#endif /* (CONFIG_AMIGA) || (CONFIG_MAC) */
 849                                default:
 850                                        hw->io_ports[i] = 0;
 851                                        break;
 852                        }
 853                } else {
 854                        hw->io_ports[i] = base + offsets[i];
 855                }
 856        }
 857        hw->irq = irq;
 858        hw->dma = NO_DMA;
 859        hw->ack_intr = ack_intr;
 860/*
 861 *      hw->iops = iops;
 862 */
 863}
 864
 865/**
 866 *      ide_register_hw_with_fixup      -       register IDE interface
 867 *      @hw: hardware registers
 868 *      @hwifp: pointer to returned hwif
 869 *      @fixup: fixup function
 870 *
 871 *      Register an IDE interface, specifying exactly the registers etc.
 872 *      Set init=1 iff calling before probes have taken place.
 873 *
 874 *      Returns -1 on error.
 875 */
 876
 877int ide_register_hw_with_fixup(hw_regs_t *hw, ide_hwif_t **hwifp, void(*fixup)(ide_hwif_t *hwif))
 878{
 879        int index, retry = 1;
 880        ide_hwif_t *hwif;
 881
 882        do {
 883                for (index = 0; index < MAX_HWIFS; ++index) {
 884                        hwif = &ide_hwifs[index];
 885                        if (hwif->hw.io_ports[IDE_DATA_OFFSET] == hw->io_ports[IDE_DATA_OFFSET])
 886                                goto found;
 887                }
 888                for (index = 0; index < MAX_HWIFS; ++index) {
 889                        hwif = &ide_hwifs[index];
 890                        if (hwif->hold)
 891                                continue;
 892                        if ((!hwif->present && !hwif->mate && !initializing) ||
 893                            (!hwif->hw.io_ports[IDE_DATA_OFFSET] && initializing))
 894                                goto found;
 895                }
 896                for (index = 0; index < MAX_HWIFS; index++)
 897                        ide_unregister(index);
 898        } while (retry--);
 899        return -1;
 900found:
 901        if (hwif->present)
 902                ide_unregister(index);
 903        else if (!hwif->hold) {
 904                init_hwif_data(hwif, index);
 905                init_hwif_default(hwif, index);
 906        }
 907        if (hwif->present)
 908                return -1;
 909        memcpy(&hwif->hw, hw, sizeof(*hw));
 910        memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->hw.io_ports));
 911        hwif->irq = hw->irq;
 912        hwif->noprobe = 0;
 913        hwif->chipset = hw->chipset;
 914
 915        if (!initializing) {
 916                probe_hwif_init_with_fixup(hwif, fixup);
 917                create_proc_ide_interfaces();
 918        }
 919
 920        if (hwifp)
 921                *hwifp = hwif;
 922
 923        return (initializing || hwif->present) ? index : -1;
 924}
 925
 926EXPORT_SYMBOL(ide_register_hw_with_fixup);
 927
 928int ide_register_hw(hw_regs_t *hw, ide_hwif_t **hwifp)
 929{
 930        return ide_register_hw_with_fixup(hw, hwifp, NULL);
 931}
 932
 933EXPORT_SYMBOL(ide_register_hw);
 934
 935/*
 936 *      Locks for IDE setting functionality
 937 */
 938
 939DECLARE_MUTEX(ide_setting_sem);
 940
 941/**
 942 *      ide_add_setting -       add an ide setting option
 943 *      @drive: drive to use
 944 *      @name: setting name
 945 *      @rw: true if the function is read write
 946 *      @read_ioctl: function to call on read
 947 *      @write_ioctl: function to call on write
 948 *      @data_type: type of data
 949 *      @min: range minimum
 950 *      @max: range maximum
 951 *      @mul_factor: multiplication scale
 952 *      @div_factor: divison scale
 953 *      @data: private data field
 954 *      @set: setting
 955 *
 956 *      Removes the setting named from the device if it is present.
 957 *      The function takes the settings_lock to protect against 
 958 *      parallel changes. This function must not be called from IRQ
 959 *      context. Returns 0 on success or -1 on failure.
 960 *
 961 *      BUGS: This code is seriously over-engineered. There is also
 962 *      magic about how the driver specific features are setup. If
 963 *      a driver is attached we assume the driver settings are auto
 964 *      remove.
 965 */
 966 
 967int 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)
 968{
 969        ide_settings_t **p = (ide_settings_t **) &drive->settings, *setting = NULL;
 970
 971        down(&ide_setting_sem);
 972        while ((*p) && strcmp((*p)->name, name) < 0)
 973                p = &((*p)->next);
 974        if ((setting = kmalloc(sizeof(*setting), GFP_KERNEL)) == NULL)
 975                goto abort;
 976        memset(setting, 0, sizeof(*setting));
 977        if ((setting->name = kmalloc(strlen(name) + 1, GFP_KERNEL)) == NULL)
 978                goto abort;
 979        strcpy(setting->name, name);
 980        setting->rw = rw;
 981        setting->read_ioctl = read_ioctl;
 982        setting->write_ioctl = write_ioctl;
 983        setting->data_type = data_type;
 984        setting->min = min;
 985        setting->max = max;
 986        setting->mul_factor = mul_factor;
 987        setting->div_factor = div_factor;
 988        setting->data = data;
 989        setting->set = set;
 990        
 991        setting->next = *p;
 992        if (drive->driver != &idedefault_driver)
 993                setting->auto_remove = 1;
 994        *p = setting;
 995        up(&ide_setting_sem);
 996        return 0;
 997abort:
 998        up(&ide_setting_sem);
 999        if (setting)
1000                kfree(setting);
1001        return -1;
1002}
1003
1004EXPORT_SYMBOL(ide_add_setting);
1005
1006/**
1007 *      __ide_remove_setting    -       remove an ide setting option
1008 *      @drive: drive to use
1009 *      @name: setting name
1010 *
1011 *      Removes the setting named from the device if it is present.
1012 *      The caller must hold the setting semaphore.
1013 */
1014 
1015static void __ide_remove_setting (ide_drive_t *drive, char *name)
1016{
1017        ide_settings_t **p, *setting;
1018
1019        p = (ide_settings_t **) &drive->settings;
1020
1021        while ((*p) && strcmp((*p)->name, name))
1022                p = &((*p)->next);
1023        if ((setting = (*p)) == NULL)
1024                return;
1025
1026        (*p) = setting->next;
1027        
1028        kfree(setting->name);
1029        kfree(setting);
1030}
1031
1032/**
1033 *      ide_find_setting_by_ioctl       -       find a drive specific ioctl
1034 *      @drive: drive to scan
1035 *      @cmd: ioctl command to handle
1036 *
1037 *      Scan's the device setting table for a matching entry and returns
1038 *      this or NULL if no entry is found. The caller must hold the
1039 *      setting semaphore
1040 */
1041 
1042static ide_settings_t *ide_find_setting_by_ioctl (ide_drive_t *drive, int cmd)
1043{
1044        ide_settings_t *setting = drive->settings;
1045
1046        while (setting) {
1047                if (setting->read_ioctl == cmd || setting->write_ioctl == cmd)
1048                        break;
1049                setting = setting->next;
1050        }
1051        
1052        return setting;
1053}
1054
1055/**
1056 *      ide_find_setting_by_name        -       find a drive specific setting
1057 *      @drive: drive to scan
1058 *      @name: setting name
1059 *
1060 *      Scan's the device setting table for a matching entry and returns
1061 *      this or NULL if no entry is found. The caller must hold the
1062 *      setting semaphore
1063 */
1064 
1065ide_settings_t *ide_find_setting_by_name (ide_drive_t *drive, char *name)
1066{
1067        ide_settings_t *setting = drive->settings;
1068
1069        while (setting) {
1070                if (strcmp(setting->name, name) == 0)
1071                        break;
1072                setting = setting->next;
1073        }
1074        return setting;
1075}
1076
1077/**
1078 *      auto_remove_settings    -       remove driver specific settings
1079 *      @drive: drive
1080 *
1081 *      Automatically remove all the driver specific settings for this
1082 *      drive. This function may sleep and must not be called from IRQ
1083 *      context. The caller must hold ide_setting_sem.
1084 */
1085 
1086static void auto_remove_settings (ide_drive_t *drive)
1087{
1088        ide_settings_t *setting;
1089repeat:
1090        setting = drive->settings;
1091        while (setting) {
1092                if (setting->auto_remove) {
1093                        __ide_remove_setting(drive, setting->name);
1094                        goto repeat;
1095                }
1096                setting = setting->next;
1097        }
1098}
1099
1100/**
1101 *      ide_read_setting        -       read an IDE setting
1102 *      @drive: drive to read from
1103 *      @setting: drive setting
1104 *
1105 *      Read a drive setting and return the value. The caller
1106 *      must hold the ide_setting_sem when making this call.
1107 *
1108 *      BUGS: the data return and error are the same return value
1109 *      so an error -EINVAL and true return of the same value cannot
1110 *      be told apart
1111 */
1112 
1113int ide_read_setting (ide_drive_t *drive, ide_settings_t *setting)
1114{
1115        int             val = -EINVAL;
1116        unsigned long   flags;
1117
1118        if ((setting->rw & SETTING_READ)) {
1119                spin_lock_irqsave(&ide_lock, flags);
1120                switch(setting->data_type) {
1121                        case TYPE_BYTE:
1122                                val = *((u8 *) setting->data);
1123                                break;
1124                        case TYPE_SHORT:
1125                                val = *((u16 *) setting->data);
1126                                break;
1127                        case TYPE_INT:
1128                        case TYPE_INTA:
1129                                val = *((u32 *) setting->data);
1130                                break;
1131                }
1132                spin_unlock_irqrestore(&ide_lock, flags);
1133        }
1134        return val;
1135}
1136
1137/**
1138 *      ide_spin_wait_hwgroup   -       wait for group
1139 *      @drive: drive in the group
1140 *
1141 *      Wait for an IDE device group to go non busy and then return
1142 *      holding the ide_lock which guards the hwgroup->busy status
1143 *      and right to use it.
1144 */
1145
1146int ide_spin_wait_hwgroup (ide_drive_t *drive)
1147{
1148        ide_hwgroup_t *hwgroup = HWGROUP(drive);
1149        unsigned long timeout = jiffies + (3 * HZ);
1150
1151        spin_lock_irq(&ide_lock);
1152
1153        while (hwgroup->busy) {
1154                unsigned long lflags;
1155                spin_unlock_irq(&ide_lock);
1156                local_irq_set(lflags);
1157                if (time_after(jiffies, timeout)) {
1158                        local_irq_restore(lflags);
1159                        printk(KERN_ERR "%s: channel busy\n", drive->name);
1160                        return -EBUSY;
1161                }
1162                local_irq_restore(lflags);
1163                spin_lock_irq(&ide_lock);
1164        }
1165        return 0;
1166}
1167
1168EXPORT_SYMBOL(ide_spin_wait_hwgroup);
1169
1170/**
1171 *      ide_write_setting       -       read an IDE setting
1172 *      @drive: drive to read from
1173 *      @setting: drive setting
1174 *      @val: value
1175 *
1176 *      Write a drive setting if it is possible. The caller
1177 *      must hold the ide_setting_sem when making this call.
1178 *
1179 *      BUGS: the data return and error are the same return value
1180 *      so an error -EINVAL and true return of the same value cannot
1181 *      be told apart
1182 *
1183 *      FIXME:  This should be changed to enqueue a special request
1184 *      to the driver to change settings, and then wait on a sema for completion.
1185 *      The current scheme of polling is kludgy, though safe enough.
1186 */
1187
1188int ide_write_setting (ide_drive_t *drive, ide_settings_t *setting, int val)
1189{
1190        int i;
1191        u32 *p;
1192
1193        if (!capable(CAP_SYS_ADMIN))
1194                return -EACCES;
1195        if (!(setting->rw & SETTING_WRITE))
1196                return -EPERM;
1197        if (val < setting->min || val > setting->max)
1198                return -EINVAL;
1199        if (setting->set)
1200                return setting->set(drive, val);
1201        if (ide_spin_wait_hwgroup(drive))
1202                return -EBUSY;
1203        switch (setting->data_type) {
1204                case TYPE_BYTE:
1205                        *((u8 *) setting->data) = val;
1206                        break;
1207                case TYPE_SHORT:
1208                        *((u16 *) setting->data) = val;
1209                        break;
1210                case TYPE_INT:
1211                        *((u32 *) setting->data) = val;
1212                        break;
1213                case TYPE_INTA:
1214                        p = (u32 *) setting->data;
1215                        for (i = 0; i < 1 << PARTN_BITS; i++, p++)
1216                                *p = val;
1217                        break;
1218        }
1219        spin_unlock_irq(&ide_lock);
1220        return 0;
1221}
1222
1223static int set_io_32bit(ide_drive_t *drive, int arg)
1224{
1225        drive->io_32bit = arg;
1226#ifdef CONFIG_BLK_DEV_DTC2278
1227        if (HWIF(drive)->chipset == ide_dtc2278)
1228                HWIF(drive)->drives[!drive->select.b.unit].io_32bit = arg;
1229#endif /* CONFIG_BLK_DEV_DTC2278 */
1230        return 0;
1231}
1232
1233static int set_using_dma (ide_drive_t *drive, int arg)
1234{
1235#ifdef CONFIG_BLK_DEV_IDEDMA
1236        if (!drive->id || !(drive->id->capability & 1))
1237                return -EPERM;
1238        if (HWIF(drive)->ide_dma_check == NULL)
1239                return -EPERM;
1240        if (arg) {
1241                if (HWIF(drive)->ide_dma_check(drive)) return -EIO;
1242                if (HWIF(drive)->ide_dma_on(drive)) return -EIO;
1243        } else {
1244                if (__ide_dma_off(drive))
1245                        return -EIO;
1246        }
1247        return 0;
1248#else
1249        return -EPERM;
1250#endif
1251}
1252
1253static int set_pio_mode (ide_drive_t *drive, int arg)
1254{
1255        struct request rq;
1256
1257        if (!HWIF(drive)->tuneproc)
1258                return -ENOSYS;
1259        if (drive->special.b.set_tune)
1260                return -EBUSY;
1261        ide_init_drive_cmd(&rq);
1262        drive->tune_req = (u8) arg;
1263        drive->special.b.set_tune = 1;
1264        (void) ide_do_drive_cmd(drive, &rq, ide_wait);
1265        return 0;
1266}
1267
1268static int set_xfer_rate (ide_drive_t *drive, int arg)
1269{
1270        int err = ide_wait_cmd(drive,
1271                        WIN_SETFEATURES, (u8) arg,
1272                        SETFEATURES_XFER, 0, NULL);
1273
1274        if (!err && arg) {
1275                ide_set_xfer_rate(drive, (u8) arg);
1276                ide_driveid_update(drive);
1277        }
1278        return err;
1279}
1280
1281/**
1282 *      ide_add_generic_settings        -       generic ide settings
1283 *      @drive: drive being configured
1284 *
1285 *      Add the generic parts of the system settings to the /proc files and
1286 *      ioctls for this IDE device. The caller must not be holding the
1287 *      ide_setting_sem.
1288 */
1289
1290void ide_add_generic_settings (ide_drive_t *drive)
1291{
1292/*
1293 *                      drive   setting name            read/write access                               read ioctl              write ioctl             data type       min     max                             mul_factor      div_factor      data pointer                    set function
1294 */
1295        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);
1296        ide_add_setting(drive,  "keepsettings",         SETTING_RW,                                     HDIO_GET_KEEPSETTINGS,  HDIO_SET_KEEPSETTINGS,  TYPE_BYTE,      0,      1,                              1,              1,              &drive->keep_settings,          NULL);
1297        ide_add_setting(drive,  "nice1",                SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      1,                              1,              1,              &drive->nice1,                  NULL);
1298        ide_add_setting(drive,  "pio_mode",             SETTING_WRITE,                                  -1,                     HDIO_SET_PIO_MODE,      TYPE_BYTE,      0,      255,                            1,              1,              NULL,                           set_pio_mode);
1299        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);
1300        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);
1301        ide_add_setting(drive,  "init_speed",           SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      70,                             1,              1,              &drive->init_speed,             NULL);
1302        ide_add_setting(drive,  "current_speed",        SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      70,                             1,              1,              &drive->current_speed,          set_xfer_rate);
1303        ide_add_setting(drive,  "number",               SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      3,                              1,              1,              &drive->dn,                     NULL);
1304}
1305
1306/**
1307 *      system_bus_clock        -       clock guess
1308 *
1309 *      External version of the bus clock guess used by very old IDE drivers
1310 *      for things like VLB timings. Should not be used.
1311 */
1312
1313int system_bus_clock (void)
1314{
1315        return((int) ((!system_bus_speed) ? ide_system_bus_speed() : system_bus_speed ));
1316}
1317
1318EXPORT_SYMBOL(system_bus_clock);
1319
1320/*
1321 *      Locking is badly broken here - since way back.  That sucker is
1322 * root-only, but that's not an excuse...  The real question is what
1323 * exclusion rules do we want here.
1324 */
1325int ide_replace_subdriver (ide_drive_t *drive, const char *driver)
1326{
1327        if (!drive->present || drive->usage || drive->dead)
1328                goto abort;
1329        if (DRIVER(drive)->cleanup(drive))
1330                goto abort;
1331        strlcpy(drive->driver_req, driver, sizeof(drive->driver_req));
1332        if (ata_attach(drive)) {
1333                spin_lock(&drives_lock);
1334                list_del_init(&drive->list);
1335                spin_unlock(&drives_lock);
1336                drive->driver_req[0] = 0;
1337                ata_attach(drive);
1338        } else {
1339                drive->driver_req[0] = 0;
1340        }
1341        if (DRIVER(drive)!= &idedefault_driver && !strcmp(DRIVER(drive)->name, driver))
1342                return 0;
1343abort:
1344        return 1;
1345}
1346
1347/**
1348 *      ata_attach              -       attach an ATA/ATAPI device
1349 *      @drive: drive to attach
1350 *
1351 *      Takes a drive that is as yet not assigned to any midlayer IDE
1352 *      driver (or is assigned to the default driver) and figures out
1353 *      which driver would like to own it. If nobody claims the drive
1354 *      then it is automatically attached to the default driver used for
1355 *      unclaimed objects.
1356 *
1357 *      A return of zero indicates attachment to a driver, of one
1358 *      attachment to the default driver.
1359 *
1360 *      Takes drivers_lock.
1361 */
1362
1363int ata_attach(ide_drive_t *drive)
1364{
1365        struct list_head *p;
1366        spin_lock(&drivers_lock);
1367        list_for_each(p, &drivers) {
1368                ide_driver_t *driver = list_entry(p, ide_driver_t, drivers);
1369                if (!try_module_get(driver->owner))
1370                        continue;
1371                spin_unlock(&drivers_lock);
1372                if (driver->attach(drive) == 0) {
1373                        module_put(driver->owner);
1374                        drive->gendev.driver = &driver->gen_driver;
1375                        return 0;
1376                }
1377                spin_lock(&drivers_lock);
1378                module_put(driver->owner);
1379        }
1380        drive->gendev.driver = &idedefault_driver.gen_driver;
1381        spin_unlock(&drivers_lock);
1382        if(idedefault_driver.attach(drive) != 0)
1383                panic("ide: default attach failed");
1384        return 1;
1385}
1386
1387static int generic_ide_suspend(struct device *dev, pm_message_t state)
1388{
1389        ide_drive_t *drive = dev->driver_data;
1390        struct request rq;
1391        struct request_pm_state rqpm;
1392        ide_task_t args;
1393
1394        memset(&rq, 0, sizeof(rq));
1395        memset(&rqpm, 0, sizeof(rqpm));
1396        memset(&args, 0, sizeof(args));
1397        rq.flags = REQ_PM_SUSPEND;
1398        rq.special = &args;
1399        rq.pm = &rqpm;
1400        rqpm.pm_step = ide_pm_state_start_suspend;
1401        rqpm.pm_state = state;
1402
1403        return ide_do_drive_cmd(drive, &rq, ide_wait);
1404}
1405
1406static int generic_ide_resume(struct device *dev)
1407{
1408        ide_drive_t *drive = dev->driver_data;
1409        struct request rq;
1410        struct request_pm_state rqpm;
1411        ide_task_t args;
1412
1413        memset(&rq, 0, sizeof(rq));
1414        memset(&rqpm, 0, sizeof(rqpm));
1415        memset(&args, 0, sizeof(args));
1416        rq.flags = REQ_PM_RESUME;
1417        rq.special = &args;
1418        rq.pm = &rqpm;
1419        rqpm.pm_step = ide_pm_state_start_resume;
1420        rqpm.pm_state = 0;
1421
1422        return ide_do_drive_cmd(drive, &rq, ide_head_wait);
1423}
1424
1425int generic_ide_ioctl(struct file *file, struct block_device *bdev,
1426                        unsigned int cmd, unsigned long arg)
1427{
1428        ide_drive_t *drive = bdev->bd_disk->private_data;
1429        ide_settings_t *setting;
1430        int err = 0;
1431        void __user *p = (void __user *)arg;
1432
1433        down(&ide_setting_sem);
1434        if ((setting = ide_find_setting_by_ioctl(drive, cmd)) != NULL) {
1435                if (cmd == setting->read_ioctl) {
1436                        err = ide_read_setting(drive, setting);
1437                        up(&ide_setting_sem);
1438                        return err >= 0 ? put_user(err, (long __user *)arg) : err;
1439                } else {
1440                        if (bdev != bdev->bd_contains)
1441                                err = -EINVAL;
1442                        else
1443                                err = ide_write_setting(drive, setting, arg);
1444                        up(&ide_setting_sem);
1445                        return err;
1446                }
1447        }
1448        up(&ide_setting_sem);
1449
1450        switch (cmd) {
1451                case HDIO_GETGEO:
1452                {
1453                        struct hd_geometry geom;
1454                        if (!p || (drive->media != ide_disk && drive->media != ide_floppy)) return -EINVAL;
1455                        geom.heads = drive->bios_head;
1456                        geom.sectors = drive->bios_sect;
1457                        geom.cylinders = (u16)drive->bios_cyl; /* truncate */
1458                        geom.start = get_start_sect(bdev);
1459                        if (copy_to_user(p, &geom, sizeof(struct hd_geometry)))
1460                                return -EFAULT;
1461                        return 0;
1462                }
1463
1464                case HDIO_OBSOLETE_IDENTITY:
1465                case HDIO_GET_IDENTITY:
1466                        if (bdev != bdev->bd_contains)
1467                                return -EINVAL;
1468                        if (drive->id_read == 0)
1469                                return -ENOMSG;
1470                        if (copy_to_user(p, drive->id, (cmd == HDIO_GET_IDENTITY) ? sizeof(*drive->id) : 142))
1471                                return -EFAULT;
1472                        return 0;
1473
1474                case HDIO_GET_NICE:
1475                        return put_user(drive->dsc_overlap      <<      IDE_NICE_DSC_OVERLAP    |
1476                                        drive->atapi_overlap    <<      IDE_NICE_ATAPI_OVERLAP  |
1477                                        drive->nice0            <<      IDE_NICE_0              |
1478                                        drive->nice1            <<      IDE_NICE_1              |
1479                                        drive->nice2            <<      IDE_NICE_2,
1480                                        (long __user *) arg);
1481
1482#ifdef CONFIG_IDE_TASK_IOCTL
1483                case HDIO_DRIVE_TASKFILE:
1484                        if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
1485                                return -EACCES;
1486                        switch(drive->media) {
1487                                case ide_disk:
1488                                        return ide_taskfile_ioctl(drive, cmd, arg);
1489                                default:
1490                                        return -ENOMSG;
1491                        }
1492#endif /* CONFIG_IDE_TASK_IOCTL */
1493
1494                case HDIO_DRIVE_CMD:
1495                        if (!capable(CAP_SYS_RAWIO))
1496                                return -EACCES;
1497                        return ide_cmd_ioctl(drive, cmd, arg);
1498
1499                case HDIO_DRIVE_TASK:
1500                        if (!capable(CAP_SYS_RAWIO))
1501                                return -EACCES;
1502                        return ide_task_ioctl(drive, cmd, arg);
1503
1504                case HDIO_SCAN_HWIF:
1505                {
1506                        hw_regs_t hw;
1507                        int args[3];
1508                        if (!capable(CAP_SYS_RAWIO)) return -EACCES;
1509                        if (copy_from_user(args, p, 3 * sizeof(int)))
1510                                return -EFAULT;
1511                        memset(&hw, 0, sizeof(hw));
1512                        ide_init_hwif_ports(&hw, (unsigned long) args[0],
1513                                            (unsigned long) args[1], NULL);
1514                        hw.irq = args[2];
1515                        if (ide_register_hw(&hw, NULL) == -1)
1516                                return -EIO;
1517                        return 0;
1518                }
1519                case HDIO_UNREGISTER_HWIF:
1520                        if (!capable(CAP_SYS_RAWIO)) return -EACCES;
1521                        /* (arg > MAX_HWIFS) checked in function */
1522                        ide_unregister(arg);
1523                        return 0;
1524                case HDIO_SET_NICE:
1525                        if (!capable(CAP_SYS_ADMIN)) return -EACCES;
1526                        if (arg != (arg & ((1 << IDE_NICE_DSC_OVERLAP) | (1 << IDE_NICE_1))))
1527                                return -EPERM;
1528                        drive->dsc_overlap = (arg >> IDE_NICE_DSC_OVERLAP) & 1;
1529                        if (drive->dsc_overlap && !DRIVER(drive)->supports_dsc_overlap) {
1530                                drive->dsc_overlap = 0;
1531                                return -EPERM;
1532                        }
1533                        drive->nice1 = (arg >> IDE_NICE_1) & 1;
1534                        return 0;
1535                case HDIO_DRIVE_RESET:
1536                {
1537                        unsigned long flags;
1538                        if (!capable(CAP_SYS_ADMIN)) return -EACCES;
1539                        
1540                        /*
1541                         *      Abort the current command on the
1542                         *      group if there is one, taking
1543                         *      care not to allow anything else
1544                         *      to be queued and to die on the
1545                         *      spot if we miss one somehow
1546                         */
1547
1548                        spin_lock_irqsave(&ide_lock, flags);
1549
1550                        ide_abort(drive, "drive reset");
1551
1552                        if(HWGROUP(drive)->handler)
1553                                BUG();
1554                                
1555                        /* Ensure nothing gets queued after we
1556                           drop the lock. Reset will clear the busy */
1557                   
1558                        HWGROUP(drive)->busy = 1;
1559                        spin_unlock_irqrestore(&ide_lock, flags);
1560                        (void) ide_do_reset(drive);
1561
1562                        return 0;
1563                }
1564
1565                case CDROMEJECT:
1566                case CDROMCLOSETRAY:
1567                        return scsi_cmd_ioctl(file, bdev->bd_disk, cmd, p);
1568
1569                case HDIO_GET_BUSSTATE:
1570                        if (!capable(CAP_SYS_ADMIN))
1571                                return -EACCES;
1572                        if (put_user(HWIF(drive)->bus_state, (long __user *)arg))
1573                                return -EFAULT;
1574                        return 0;
1575
1576                case HDIO_SET_BUSSTATE:
1577                        if (!capable(CAP_SYS_ADMIN))
1578                                return -EACCES;
1579                        if (HWIF(drive)->busproc)
1580                                return HWIF(drive)->busproc(drive, (int)arg);
1581                        return -EOPNOTSUPP;
1582                default:
1583                        return -EINVAL;
1584        }
1585}
1586
1587EXPORT_SYMBOL(generic_ide_ioctl);
1588
1589/*
1590 * stridx() returns the offset of c within s,
1591 * or -1 if c is '\0' or not found within s.
1592 */
1593static int __init stridx (const char *s, char c)
1594{
1595        char *i = strchr(s, c);
1596        return (i && c) ? i - s : -1;
1597}
1598
1599/*
1600 * match_parm() does parsing for ide_setup():
1601 *
1602 * 1. the first char of s must be '='.
1603 * 2. if the remainder matches one of the supplied keywords,
1604 *     the index (1 based) of the keyword is negated and returned.
1605 * 3. if the remainder is a series of no more than max_vals numbers
1606 *     separated by commas, the numbers are saved in vals[] and a
1607 *     count of how many were saved is returned.  Base10 is assumed,
1608 *     and base16 is allowed when prefixed with "0x".
1609 * 4. otherwise, zero is returned.
1610 */
1611static int __init match_parm (char *s, const char *keywords[], int vals[], int max_vals)
1612{
1613        static const char *decimal = "0123456789";
1614        static const char *hex = "0123456789abcdef";
1615        int i, n;
1616
1617        if (*s++ == '=') {
1618                /*
1619                 * Try matching against the supplied keywords,
1620                 * and return -(index+1) if we match one
1621                 */
1622                if (keywords != NULL) {
1623                        for (i = 0; *keywords != NULL; ++i) {
1624                                if (!strcmp(s, *keywords++))
1625                                        return -(i+1);
1626                        }
1627                }
1628                /*
1629                 * Look for a series of no more than "max_vals"
1630                 * numeric values separated by commas, in base10,
1631                 * or base16 when prefixed with "0x".
1632                 * Return a count of how many were found.
1633                 */
1634                for (n = 0; (i = stridx(decimal, *s)) >= 0;) {
1635                        vals[n] = i;
1636                        while ((i = stridx(decimal, *++s)) >= 0)
1637                                vals[n] = (vals[n] * 10) + i;
1638                        if (*s == 'x' && !vals[n]) {
1639                                while ((i = stridx(hex, *++s)) >= 0)
1640                                        vals[n] = (vals[n] * 0x10) + i;
1641                        }
1642                        if (++n == max_vals)
1643                                break;
1644                        if (*s == ',' || *s == ';')
1645                                ++s;
1646                }
1647                if (!*s)
1648                        return n;
1649        }
1650        return 0;       /* zero = nothing matched */
1651}
1652
1653#ifdef CONFIG_BLK_DEV_ALI14XX
1654static int __initdata probe_ali14xx;
1655extern int ali14xx_init(void);
1656#endif
1657#ifdef CONFIG_BLK_DEV_UMC8672
1658static int __initdata probe_umc8672;
1659extern int umc8672_init(void);
1660#endif
1661#ifdef CONFIG_BLK_DEV_DTC2278
1662static int __initdata probe_dtc2278;
1663extern int dtc2278_init(void);
1664#endif
1665#ifdef CONFIG_BLK_DEV_HT6560B
1666static int __initdata probe_ht6560b;
1667extern int ht6560b_init(void);
1668#endif
1669#ifdef CONFIG_BLK_DEV_QD65XX
1670static int __initdata probe_qd65xx;
1671extern int qd65xx_init(void);
1672#endif
1673
1674static int __initdata is_chipset_set[MAX_HWIFS];
1675
1676/*
1677 * ide_setup() gets called VERY EARLY during initialization,
1678 * to handle kernel "command line" strings beginning with "hdx=" or "ide".
1679 *
1680 * Remember to update Documentation/ide.txt if you change something here.
1681 */
1682static int __init ide_setup(char *s)
1683{
1684        int i, vals[3];
1685        ide_hwif_t *hwif;
1686        ide_drive_t *drive;
1687        unsigned int hw, unit;
1688        const char max_drive = 'a' + ((MAX_HWIFS * MAX_DRIVES) - 1);
1689        const char max_hwif  = '0' + (MAX_HWIFS - 1);
1690
1691        
1692        if (strncmp(s,"hd",2) == 0 && s[2] == '=')      /* hd= is for hd.c   */
1693                return 0;                               /* driver and not us */
1694
1695        if (strncmp(s,"ide",3) && strncmp(s,"idebus",6) && strncmp(s,"hd",2))
1696                return 0;
1697
1698        printk(KERN_INFO "ide_setup: %s", s);
1699        init_ide_data ();
1700
1701#ifdef CONFIG_BLK_DEV_IDEDOUBLER
1702        if (!strcmp(s, "ide=doubler")) {
1703                extern int ide_doubler;
1704
1705                printk(" : Enabled support for IDE doublers\n");
1706                ide_doubler = 1;
1707                return 1;
1708        }
1709#endif /* CONFIG_BLK_DEV_IDEDOUBLER */
1710
1711        if (!strcmp(s, "ide=nodma")) {
1712                printk(" : Prevented DMA\n");
1713                noautodma = 1;
1714                return 1;
1715        }
1716
1717#ifdef CONFIG_BLK_DEV_IDEPCI
1718        if (!strcmp(s, "ide=reverse")) {
1719                ide_scan_direction = 1;
1720                printk(" : Enabled support for IDE inverse scan order.\n");
1721                return 1;
1722        }
1723#endif /* CONFIG_BLK_DEV_IDEPCI */
1724
1725        /*
1726         * Look for drive options:  "hdx="
1727         */
1728        if (s[0] == 'h' && s[1] == 'd' && s[2] >= 'a' && s[2] <= max_drive) {
1729                const char *hd_words[] = {
1730                        "none", "noprobe", "nowerr", "cdrom", "serialize",
1731                        "autotune", "noautotune", "minus8", "swapdata", "bswap",
1732                        "minus11", "remap", "remap63", "scsi", NULL };
1733                unit = s[2] - 'a';
1734                hw   = unit / MAX_DRIVES;
1735                unit = unit % MAX_DRIVES;
1736                hwif = &ide_hwifs[hw];
1737                drive = &hwif->drives[unit];
1738                if (strncmp(s + 4, "ide-", 4) == 0) {
1739                        strlcpy(drive->driver_req, s + 4, sizeof(drive->driver_req));
1740                        goto done;
1741                }
1742                switch (match_parm(&s[3], hd_words, vals, 3)) {
1743                        case -1: /* "none" */
1744                        case -2: /* "noprobe" */
1745                                drive->noprobe = 1;
1746                                goto done;
1747                        case -3: /* "nowerr" */
1748                                drive->bad_wstat = BAD_R_STAT;
1749                                hwif->noprobe = 0;
1750                                goto done;
1751                        case -4: /* "cdrom" */
1752                                drive->present = 1;
1753                                drive->media = ide_cdrom;
1754                                hwif->noprobe = 0;
1755                                goto done;
1756                        case -5: /* "serialize" */
1757                                printk(" -- USE \"ide%d=serialize\" INSTEAD", hw);
1758                                goto do_serialize;
1759                        case -6: /* "autotune" */
1760                                drive->autotune = IDE_TUNE_AUTO;
1761                                goto obsolete_option;
1762                        case -7: /* "noautotune" */
1763                                drive->autotune = IDE_TUNE_NOAUTO;
1764                                goto obsolete_option;
1765                        case -9: /* "swapdata" */
1766                        case -10: /* "bswap" */
1767                                drive->bswap = 1;
1768                                goto done;
1769                        case -12: /* "remap" */
1770                                drive->remap_0_to_1 = 1;
1771                                goto done;
1772                        case -13: /* "remap63" */
1773                                drive->sect0 = 63;
1774                                goto done;
1775                        case -14: /* "scsi" */
1776                                drive->scsi = 1;
1777                                goto done;
1778                        case 3: /* cyl,head,sect */
1779                                drive->media    = ide_disk;
1780                                drive->cyl      = drive->bios_cyl  = vals[0];
1781                                drive->head     = drive->bios_head = vals[1];
1782                                drive->sect     = drive->bios_sect = vals[2];
1783                                drive->present  = 1;
1784                                drive->forced_geom = 1;
1785                                hwif->noprobe = 0;
1786                                goto done;
1787                        default:
1788                                goto bad_option;
1789                }
1790        }
1791
1792        if (s[0] != 'i' || s[1] != 'd' || s[2] != 'e')
1793                goto bad_option;
1794        /*
1795         * Look for bus speed option:  "idebus="
1796         */
1797        if (s[3] == 'b' && s[4] == 'u' && s[5] == 's') {
1798                if (match_parm(&s[6], NULL, vals, 1) != 1)
1799                        goto bad_option;
1800                if (vals[0] >= 20 && vals[0] <= 66) {
1801                        idebus_parameter = vals[0];
1802                } else
1803                        printk(" -- BAD BUS SPEED! Expected value from 20 to 66");
1804                goto done;
1805        }
1806        /*
1807         * Look for interface options:  "idex="
1808         */
1809        if (s[3] >= '0' && s[3] <= max_hwif) {
1810                /*
1811                 * Be VERY CAREFUL changing this: note hardcoded indexes below
1812                 * (-8, -9, -10) are reserved to ease the hardcoding.
1813                 */
1814                static const char *ide_words[] = {
1815                        "noprobe", "serialize", "autotune", "noautotune", 
1816                        "reset", "dma", "ata66", "minus8", "minus9",
1817                        "minus10", "four", "qd65xx", "ht6560b", "cmd640_vlb",
1818                        "dtc2278", "umc8672", "ali14xx", NULL };
1819                hw = s[3] - '0';
1820                hwif = &ide_hwifs[hw];
1821                i = match_parm(&s[4], ide_words, vals, 3);
1822
1823                /*
1824                 * Cryptic check to ensure chipset not already set for hwif.
1825                 * Note: we can't depend on hwif->chipset here.
1826                 */
1827                if ((i >= -18 && i <= -11) || (i > 0 && i <= 3)) {
1828                        /* chipset already specified */
1829                        if (is_chipset_set[hw])
1830                                goto bad_option;
1831                        if (i > -18 && i <= -11) {
1832                                /* these drivers are for "ide0=" only */
1833                                if (hw != 0)
1834                                        goto bad_hwif;
1835                                /* chipset already specified for 2nd port */
1836                                if (is_chipset_set[hw+1])
1837                                        goto bad_option;
1838                        }
1839                        is_chipset_set[hw] = 1;
1840                        printk("\n");
1841                }
1842
1843                switch (i) {
1844#ifdef CONFIG_BLK_DEV_ALI14XX
1845                        case -17: /* "ali14xx" */
1846                                probe_ali14xx = 1;
1847                                goto done;
1848#endif
1849#ifdef CONFIG_BLK_DEV_UMC8672
1850                        case -16: /* "umc8672" */
1851                                probe_umc8672 = 1;
1852                                goto done;
1853#endif
1854#ifdef CONFIG_BLK_DEV_DTC2278
1855                        case -15: /* "dtc2278" */
1856                                probe_dtc2278 = 1;
1857                                goto done;
1858#endif
1859#ifdef CONFIG_BLK_DEV_CMD640
1860                        case -14: /* "cmd640_vlb" */
1861                        {
1862                                extern int cmd640_vlb; /* flag for cmd640.c */
1863                                cmd640_vlb = 1;
1864                                goto done;
1865                        }
1866#endif
1867#ifdef CONFIG_BLK_DEV_HT6560B
1868                        case -13: /* "ht6560b" */
1869                                probe_ht6560b = 1;
1870                                goto done;
1871#endif
1872#ifdef CONFIG_BLK_DEV_QD65XX
1873                        case -12: /* "qd65xx" */
1874                                probe_qd65xx = 1;
1875                                goto done;
1876#endif
1877#ifdef CONFIG_BLK_DEV_4DRIVES
1878                        case -11: /* "four" drives on one set of ports */
1879                        {
1880                                ide_hwif_t *mate = &ide_hwifs[hw^1];
1881                                mate->drives[0].select.all ^= 0x20;
1882                                mate->drives[1].select.all ^= 0x20;
1883                                hwif->chipset = mate->chipset = ide_4drives;
1884                                mate->irq = hwif->irq;
1885                                memcpy(mate->io_ports, hwif->io_ports, sizeof(hwif->io_ports));
1886                                goto do_serialize;
1887                        }
1888#endif /* CONFIG_BLK_DEV_4DRIVES */
1889                        case -10: /* minus10 */
1890                        case -9: /* minus9 */
1891                        case -8: /* minus8 */
1892                                goto bad_option;
1893                        case -7: /* ata66 */
1894#ifdef CONFIG_BLK_DEV_IDEPCI
1895                                hwif->udma_four = 1;
1896                                goto obsolete_option;
1897#else
1898                                goto bad_hwif;
1899#endif
1900                        case -6: /* dma */
1901                                hwif->autodma = 1;
1902                                goto obsolete_option;
1903                        case -5: /* "reset" */
1904                                hwif->reset = 1;
1905                                goto obsolete_option;
1906                        case -4: /* "noautotune" */
1907                                hwif->drives[0].autotune = IDE_TUNE_NOAUTO;
1908                                hwif->drives[1].autotune = IDE_TUNE_NOAUTO;
1909                                goto obsolete_option;
1910                        case -3: /* "autotune" */
1911                                hwif->drives[0].autotune = IDE_TUNE_AUTO;
1912                                hwif->drives[1].autotune = IDE_TUNE_AUTO;
1913                                goto obsolete_option;
1914                        case -2: /* "serialize" */
1915                        do_serialize:
1916                                hwif->mate = &ide_hwifs[hw^1];
1917                                hwif->mate->mate = hwif;
1918                                hwif->serialized = hwif->mate->serialized = 1;
1919                                goto obsolete_option;
1920
1921                        case -1: /* "noprobe" */
1922                                hwif->noprobe = 1;
1923                                goto done;
1924
1925                        case 1: /* base */
1926                                vals[1] = vals[0] + 0x206; /* default ctl */
1927                        case 2: /* base,ctl */
1928                                vals[2] = 0;    /* default irq = probe for it */
1929                        case 3: /* base,ctl,irq */
1930                                hwif->hw.irq = vals[2];
1931                                ide_init_hwif_ports(&hwif->hw, (unsigned long) vals[0], (unsigned long) vals[1], &hwif->irq);
1932                                memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->io_ports));
1933                                hwif->irq      = vals[2];
1934                                hwif->noprobe  = 0;
1935                                hwif->chipset  = ide_forced;
1936                                goto obsolete_option;
1937
1938                        case 0: goto bad_option;
1939                        default:
1940                                printk(" -- SUPPORT NOT CONFIGURED IN THIS KERNEL\n");
1941                                return 1;
1942                }
1943        }
1944bad_option:
1945        printk(" -- BAD OPTION\n");
1946        return 1;
1947obsolete_option:
1948        printk(" -- OBSOLETE OPTION, WILL BE REMOVED SOON!\n");
1949        return 1;
1950bad_hwif:
1951        printk("-- NOT SUPPORTED ON ide%d", hw);
1952done:
1953        printk("\n");
1954        return 1;
1955}
1956
1957extern void pnpide_init(void);
1958extern void h8300_ide_init(void);
1959
1960/*
1961 * probe_for_hwifs() finds/initializes "known" IDE interfaces
1962 */
1963static void __init probe_for_hwifs (void)
1964{
1965#ifdef CONFIG_BLK_DEV_IDEPCI
1966        ide_scan_pcibus(ide_scan_direction);
1967#endif /* CONFIG_BLK_DEV_IDEPCI */
1968
1969#ifdef CONFIG_ETRAX_IDE
1970        {
1971                extern void init_e100_ide(void);
1972                init_e100_ide();
1973        }
1974#endif /* CONFIG_ETRAX_IDE */
1975#ifdef CONFIG_BLK_DEV_CMD640
1976        {
1977                extern void ide_probe_for_cmd640x(void);
1978                ide_probe_for_cmd640x();
1979        }
1980#endif /* CONFIG_BLK_DEV_CMD640 */
1981#ifdef CONFIG_BLK_DEV_IDE_PMAC
1982        {
1983                extern void pmac_ide_probe(void);
1984                pmac_ide_probe();
1985        }
1986#endif /* CONFIG_BLK_DEV_IDE_PMAC */
1987#ifdef CONFIG_BLK_DEV_GAYLE
1988        {
1989                extern void gayle_init(void);
1990                gayle_init();
1991        }
1992#endif /* CONFIG_BLK_DEV_GAYLE */
1993#ifdef CONFIG_BLK_DEV_FALCON_IDE
1994        {
1995                extern void falconide_init(void);
1996                falconide_init();
1997        }
1998#endif /* CONFIG_BLK_DEV_FALCON_IDE */
1999#ifdef CONFIG_BLK_DEV_MAC_IDE
2000        {
2001                extern void macide_init(void);
2002                macide_init();
2003        }
2004#endif /* CONFIG_BLK_DEV_MAC_IDE */
2005#ifdef CONFIG_BLK_DEV_Q40IDE
2006        {
2007                extern void q40ide_init(void);
2008                q40ide_init();
2009        }
2010#endif /* CONFIG_BLK_DEV_Q40IDE */
2011#ifdef CONFIG_BLK_DEV_BUDDHA
2012        {
2013                extern void buddha_init(void);
2014                buddha_init();
2015        }
2016#endif /* CONFIG_BLK_DEV_BUDDHA */
2017#ifdef CONFIG_BLK_DEV_IDEPNP
2018        pnpide_init();
2019#endif
2020#ifdef CONFIG_H8300
2021        h8300_ide_init();
2022#endif
2023}
2024
2025static ide_startstop_t default_do_request (ide_drive_t *drive, struct request *rq, sector_t block)
2026{
2027        ide_end_request(drive, 0, 0);
2028        return ide_stopped;
2029}
2030
2031static int default_end_request (ide_drive_t *drive, int uptodate, int nr_sects)
2032{
2033        return ide_end_request(drive, uptodate, nr_sects);
2034}
2035
2036static ide_startstop_t
2037default_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
2038{
2039        return __ide_error(drive, rq, stat, err);
2040}
2041
2042static void default_pre_reset (ide_drive_t *drive)
2043{
2044}
2045
2046static sector_t default_capacity (ide_drive_t *drive)
2047{
2048        return 0x7fffffff;
2049}
2050
2051static ide_startstop_t default_special (ide_drive_t *drive)
2052{
2053        special_t *s = &drive->special;
2054
2055        s->all = 0;
2056        drive->mult_req = 0;
2057        return ide_stopped;
2058}
2059
2060static ide_startstop_t default_abort(ide_drive_t *drive, struct request *rq)
2061{
2062        return __ide_abort(drive, rq);
2063}
2064
2065static ide_startstop_t default_start_power_step(ide_drive_t *drive,
2066                                                struct request *rq)
2067{
2068        rq->pm->pm_step = ide_pm_state_completed;
2069        return ide_stopped;
2070}
2071
2072static void setup_driver_defaults (ide_driver_t *d)
2073{
2074        BUG_ON(d->attach == NULL || d->cleanup == NULL);
2075
2076        if (d->do_request == NULL)      d->do_request = default_do_request;
2077        if (d->end_request == NULL)     d->end_request = default_end_request;
2078        if (d->error == NULL)           d->error = default_error;
2079        if (d->abort == NULL)           d->abort = default_abort;
2080        if (d->pre_reset == NULL)       d->pre_reset = default_pre_reset;
2081        if (d->capacity == NULL)        d->capacity = default_capacity;
2082        if (d->special == NULL)         d->special = default_special;
2083        if (d->start_power_step == NULL)
2084                d->start_power_step = default_start_power_step;
2085}
2086
2087int ide_register_subdriver(ide_drive_t *drive, ide_driver_t *driver)
2088{
2089        unsigned long flags;
2090
2091        BUG_ON(!drive->driver);
2092
2093        spin_lock_irqsave(&ide_lock, flags);
2094        if (!drive->present || drive->driver != &idedefault_driver ||
2095            drive->usage || drive->dead) {
2096                spin_unlock_irqrestore(&ide_lock, flags);
2097                return 1;
2098        }
2099        drive->driver = driver;
2100        spin_unlock_irqrestore(&ide_lock, flags);
2101        spin_lock(&drives_lock);
2102        list_add_tail(&drive->list, &driver->drives);
2103        spin_unlock(&drives_lock);
2104//      printk(KERN_INFO "%s: attached %s driver.\n", drive->name, driver->name);
2105        if ((drive->autotune == IDE_TUNE_DEFAULT) ||
2106                (drive->autotune == IDE_TUNE_AUTO)) {
2107                /* DMA timings and setup moved to ide-probe.c */
2108                drive->dsc_overlap = (drive->next != drive && driver->supports_dsc_overlap);
2109                drive->nice1 = 1;
2110        }
2111#ifdef CONFIG_PROC_FS
2112        if (drive->driver != &idedefault_driver) {
2113                ide_add_proc_entries(drive->proc, generic_subdriver_entries, drive);
2114                ide_add_proc_entries(drive->proc, driver->proc, drive);
2115        }
2116#endif
2117        return 0;
2118}
2119
2120EXPORT_SYMBOL(ide_register_subdriver);
2121
2122/**
2123 *      ide_unregister_subdriver        -       disconnect drive from driver
2124 *      @drive: drive to unplug
2125 *
2126 *      Disconnect a drive from the driver it was attached to and then
2127 *      clean up the various proc files and other objects attached to it.
2128 *
2129 *      Takes ide_setting_sem, ide_lock and drives_lock.
2130 *      Caller must hold none of the locks.
2131 *
2132 *      No locking versus subdriver unload because we are moving to the
2133 *      default driver anyway. Wants double checking.
2134 */
2135
2136int ide_unregister_subdriver (ide_drive_t *drive)
2137{
2138        unsigned long flags;
2139        
2140        down(&ide_setting_sem);
2141        spin_lock_irqsave(&ide_lock, flags);
2142        if (drive->usage || drive->driver == &idedefault_driver || DRIVER(drive)->busy) {
2143                spin_unlock_irqrestore(&ide_lock, flags);
2144                up(&ide_setting_sem);
2145                return 1;
2146        }
2147#ifdef CONFIG_PROC_FS
2148        ide_remove_proc_entries(drive->proc, DRIVER(drive)->proc);
2149        ide_remove_proc_entries(drive->proc, generic_subdriver_entries);
2150#endif
2151        auto_remove_settings(drive);
2152        drive->driver = &idedefault_driver;
2153        spin_unlock_irqrestore(&ide_lock, flags);
2154        up(&ide_setting_sem);
2155        spin_lock(&drives_lock);
2156        list_del_init(&drive->list);
2157        spin_unlock(&drives_lock);
2158        /* drive will be added to &idedefault_driver->drives in ata_attach() */
2159        return 0;
2160}
2161
2162EXPORT_SYMBOL(ide_unregister_subdriver);
2163
2164static int ide_drive_remove(struct device * dev)
2165{
2166        ide_drive_t * drive = container_of(dev,ide_drive_t,gendev);
2167        DRIVER(drive)->cleanup(drive);
2168        return 0;
2169}
2170
2171/**
2172 *      ide_register_driver     -       register IDE device driver
2173 *      @driver: the IDE device driver
2174 *
2175 *      Register a new device driver and then scan the devices
2176 *      on the IDE bus in case any should be attached to the
2177 *      driver we have just registered.  If so attach them.
2178 *
2179 *      Takes drivers_lock and drives_lock.
2180 */
2181
2182int ide_register_driver(ide_driver_t *driver)
2183{
2184        struct list_head list;
2185        struct list_head *list_loop;
2186        struct list_head *tmp_storage;
2187
2188        setup_driver_defaults(driver);
2189
2190        spin_lock(&drivers_lock);
2191        list_add(&driver->drivers, &drivers);
2192        spin_unlock(&drivers_lock);
2193
2194        INIT_LIST_HEAD(&list);
2195        spin_lock(&drives_lock);
2196        list_splice_init(&idedefault_driver.drives, &list);
2197        spin_unlock(&drives_lock);
2198
2199        list_for_each_safe(list_loop, tmp_storage, &list) {
2200                ide_drive_t *drive = container_of(list_loop, ide_drive_t, list);
2201                list_del_init(&drive->list);
2202                if (drive->present)
2203                        ata_attach(drive);
2204        }
2205        driver->gen_driver.name = (char *) driver->name;
2206        driver->gen_driver.bus = &ide_bus_type;
2207        driver->gen_driver.remove = ide_drive_remove;
2208        return driver_register(&driver->gen_driver);
2209}
2210
2211EXPORT_SYMBOL(ide_register_driver);
2212
2213/**
2214 *      ide_unregister_driver   -       unregister IDE device driver
2215 *      @driver: the IDE device driver
2216 *
2217 *      Called when a driver module is being unloaded. We reattach any
2218 *      devices to whatever driver claims them next (typically the default
2219 *      driver).
2220 *
2221 *      Takes drivers_lock and called functions will take ide_setting_sem.
2222 */
2223
2224void ide_unregister_driver(ide_driver_t *driver)
2225{
2226        ide_drive_t *drive;
2227
2228        spin_lock(&drivers_lock);
2229        list_del(&driver->drivers);
2230        spin_unlock(&drivers_lock);
2231
2232        driver_unregister(&driver->gen_driver);
2233
2234        while(!list_empty(&driver->drives)) {
2235                drive = list_entry(driver->drives.next, ide_drive_t, list);
2236                if (driver->cleanup(drive)) {
2237                        printk(KERN_ERR "%s: cleanup_module() called while still busy\n", drive->name);
2238                        BUG();
2239                }
2240                ata_attach(drive);
2241        }
2242}
2243
2244EXPORT_SYMBOL(ide_unregister_driver);
2245
2246struct block_device_operations ide_fops[] = {{
2247        .owner          = THIS_MODULE,
2248        .open           = ide_open,
2249}};
2250
2251EXPORT_SYMBOL(ide_fops);
2252
2253/*
2254 * Probe module
2255 */
2256
2257EXPORT_SYMBOL(ide_lock);
2258
2259struct bus_type ide_bus_type = {
2260        .name           = "ide",
2261        .suspend        = generic_ide_suspend,
2262        .resume         = generic_ide_resume,
2263};
2264
2265/*
2266 * This is gets invoked once during initialization, to set *everything* up
2267 */
2268static int __init ide_init(void)
2269{
2270        printk(KERN_INFO "Uniform Multi-Platform E-IDE driver " REVISION "\n");
2271        devfs_mk_dir("ide");
2272        system_bus_speed = ide_system_bus_speed();
2273
2274        bus_register(&ide_bus_type);
2275
2276        init_ide_data();
2277
2278#ifdef CONFIG_PROC_FS
2279        proc_ide_root = proc_mkdir("ide", NULL);
2280#endif
2281
2282#ifdef CONFIG_BLK_DEV_ALI14XX
2283        if (probe_ali14xx)
2284                (void)ali14xx_init();
2285#endif
2286#ifdef CONFIG_BLK_DEV_UMC8672
2287        if (probe_umc8672)
2288                (void)umc8672_init();
2289#endif
2290#ifdef CONFIG_BLK_DEV_DTC2278
2291        if (probe_dtc2278)
2292                (void)dtc2278_init();
2293#endif
2294#ifdef CONFIG_BLK_DEV_HT6560B
2295        if (probe_ht6560b)
2296                (void)ht6560b_init();
2297#endif
2298#ifdef CONFIG_BLK_DEV_QD65XX
2299        if (probe_qd65xx)
2300                (void)qd65xx_init();
2301#endif
2302
2303        initializing = 1;
2304        /* Probe for special PCI and other "known" interface chipsets. */
2305        probe_for_hwifs();
2306        initializing = 0;
2307
2308#ifdef CONFIG_PROC_FS
2309        proc_ide_create();
2310#endif
2311        return 0;
2312}
2313
2314#ifdef MODULE
2315static char *options = NULL;
2316module_param(options, charp, 0);
2317MODULE_LICENSE("GPL");
2318
2319static void __init parse_options (char *line)
2320{
2321        char *next = line;
2322
2323        if (line == NULL || !*line)
2324                return;
2325        while ((line = next) != NULL) {
2326                if ((next = strchr(line,' ')) != NULL)
2327                        *next++ = 0;
2328                if (!ide_setup(line))
2329                        printk (KERN_INFO "Unknown option '%s'\n", line);
2330        }
2331}
2332
2333int init_module (void)
2334{
2335        parse_options(options);
2336        return ide_init();
2337}
2338
2339void cleanup_module (void)
2340{
2341        int index;
2342
2343        for (index = 0; index < MAX_HWIFS; ++index)
2344                ide_unregister(index);
2345
2346#ifdef CONFIG_PROC_FS
2347        proc_ide_destroy();
2348#endif
2349        devfs_remove("ide");
2350
2351        bus_unregister(&ide_bus_type);
2352}
2353
2354#else /* !MODULE */
2355
2356__setup("", ide_setup);
2357
2358module_init(ide_init);
2359
2360#endif /* MODULE */
2361
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.