More meth updates.
[linux-2.6/linux-mips.git] / drivers / ide / main.c
blob98f4a0ba585d215e0be931289dede51c6ab586f6
1 /**** vi:set ts=8 sts=8 sw=8:************************************************
3 * Copyright (C) 1994-1998,2002 Linus Torvalds and authors:
5 * Mark Lord <mlord@pobox.com>
6 * Gadi Oxman <gadio@netvision.net.il>
7 * Andre Hedrick <andre@linux-ide.org>
8 * Jens Axboe <axboe@suse.de>
9 * Marcin Dalecki <martin@dalecki.de>
11 * See linux/MAINTAINERS for address of current maintainer.
15 * Handle overall infrastructure of the driver
18 #define VERSION "7.0.0"
20 #include <linux/config.h>
21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/string.h>
24 #include <linux/kernel.h>
25 #include <linux/timer.h>
26 #include <linux/mm.h>
27 #include <linux/interrupt.h>
28 #include <linux/major.h>
29 #include <linux/errno.h>
30 #include <linux/genhd.h>
31 #include <linux/blkpg.h>
32 #include <linux/slab.h>
33 #ifndef MODULE
34 # include <linux/init.h>
35 #endif
36 #include <linux/pci.h>
37 #include <linux/delay.h>
38 #include <linux/devfs_fs_kernel.h>
39 #include <linux/reboot.h>
40 #include <linux/cdrom.h>
41 #include <linux/device.h>
42 #include <linux/kmod.h>
43 #include <linux/hdreg.h>
44 #include <linux/ide.h>
46 #include <asm/byteorder.h>
47 #include <asm/irq.h>
48 #include <asm/uaccess.h>
49 #include <asm/io.h>
50 #include <asm/bitops.h>
52 #include "timing.h"
53 #include "pcihost.h"
54 #include "ioctl.h"
57 MODULE_DESCRIPTION("ATA/ATAPI driver infrastructure");
58 MODULE_PARM(options,"s");
59 MODULE_LICENSE("GPL");
62 * Those will be moved into separate header files eventually.
64 #ifdef CONFIG_ETRAX_IDE
65 extern void init_e100_ide(void);
66 #endif
67 #ifdef CONFIG_BLK_DEV_CMD640
68 extern void ide_probe_for_cmd640x(void);
69 #endif
70 #ifdef CONFIG_BLK_DEV_PDC4030
71 extern int ide_probe_for_pdc4030(void);
72 #endif
73 #ifdef CONFIG_BLK_DEV_IDE_PMAC
74 extern void pmac_ide_probe(void);
75 #endif
76 #ifdef CONFIG_BLK_DEV_IDE_ICSIDE
77 extern void icside_init(void);
78 #endif
79 #ifdef CONFIG_BLK_DEV_IDE_RAPIDE
80 extern void rapide_init(void);
81 #endif
82 #ifdef CONFIG_BLK_DEV_GAYLE
83 extern void gayle_init(void);
84 #endif
85 #ifdef CONFIG_BLK_DEV_FALCON_IDE
86 extern void falconide_init(void);
87 #endif
88 #ifdef CONFIG_BLK_DEV_MAC_IDE
89 extern void macide_init(void);
90 #endif
91 #ifdef CONFIG_BLK_DEV_Q40IDE
92 extern void q40ide_init(void);
93 #endif
94 #ifdef CONFIG_BLK_DEV_BUDDHA
95 extern void buddha_init(void);
96 #endif
97 #if defined(CONFIG_BLK_DEV_ISAPNP) && defined(CONFIG_ISAPNP)
98 extern void pnpide_init(int);
99 #endif
101 /* default maximum number of failures */
102 #define IDE_DEFAULT_MAX_FAILURES 1
104 int system_bus_speed; /* holds what we think is VESA/PCI bus speed */
106 static int initializing; /* set while initializing built-in drivers */
107 static int idebus_parameter; /* the "idebus=" parameter */
110 * Protects access to global structures etc.
112 spinlock_t ide_lock __cacheline_aligned = SPIN_LOCK_UNLOCKED;
114 #ifdef CONFIG_PCI
115 static int ide_scan_direction; /* THIS was formerly 2.2.x pci=reverse */
116 #endif
118 #ifdef ATA_ARCH_LOCK
120 * This is used by the Atari code to obtain access to the IDE interrupt,
121 * which is shared between several drivers.
123 int ide_irq_lock;
124 #endif
126 int noautodma = 0;
128 /* Single linked list of sub device type drivers */
129 static struct ata_operations *ata_drivers; /* = NULL */
130 static spinlock_t ata_drivers_lock = SPIN_LOCK_UNLOCKED;
133 * This is declared extern in ide.h, for access by other IDE modules:
135 struct ata_channel ide_hwifs[MAX_HWIFS]; /* master data repository */
138 * FIXME: This function should be unrolled in the palces where it get's used,
139 * since in reality it's simple architecture specific initialization.
141 * Setup hw_regs_t structure described by parameters. You may set up the hw
142 * structure yourself OR use this routine to do it for you.
144 void ide_setup_ports(hw_regs_t *hw, ide_ioreg_t base, int *offsets,
145 ide_ioreg_t ctrl, ide_ioreg_t intr,
146 int (*ack_intr)(struct ata_channel *),
147 int irq)
149 int i;
151 for (i = 0; i < IDE_NR_PORTS; i++) {
152 if (offsets[i] != -1)
153 hw->io_ports[i] = base + offsets[i];
154 else
155 hw->io_ports[i] = 0;
157 if (offsets[IDE_CONTROL_OFFSET] == -1)
158 hw->io_ports[IDE_CONTROL_OFFSET] = ctrl;
160 /* FIMXE: check if we can remove this ifdef */
161 #if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
162 if (offsets[IDE_IRQ_OFFSET] == -1)
163 hw->io_ports[IDE_IRQ_OFFSET] = intr;
164 #endif
166 hw->irq = irq;
167 hw->dma = NO_DMA;
168 hw->ack_intr = ack_intr;
171 static void init_hwif_data(struct ata_channel *ch, unsigned int index)
173 static const unsigned int majors[] = {
174 IDE0_MAJOR, IDE1_MAJOR, IDE2_MAJOR, IDE3_MAJOR, IDE4_MAJOR,
175 IDE5_MAJOR, IDE6_MAJOR, IDE7_MAJOR, IDE8_MAJOR, IDE9_MAJOR
178 unsigned int unit;
180 /* bulk initialize channel & drive info with zeros */
181 memset(ch, 0, sizeof(struct ata_channel));
183 /* fill in any non-zero initial values */
184 ch->index = index;
185 ide_init_hwif_ports(&ch->hw, ide_default_io_base(index), 0, &ch->irq);
187 memcpy(ch->io_ports, ch->hw.io_ports, sizeof(ch->hw.io_ports));
189 /* Most controllers cannot do transfers across 64kB boundaries.
190 trm290 can do transfers within a 4GB boundary, so it changes
191 this mask accordingly. */
192 ch->seg_boundary_mask = 0xffff;
194 /* Some chipsets (cs5530, any others?) think a 64kB transfer
195 is 0 byte transfer, so set the limit one sector smaller.
196 In the future, we may default to 64kB transfers and let
197 invidual chipsets with this problem change ch->max_segment_size. */
198 ch->max_segment_size = (1<<16) - 512;
200 ch->noprobe = !ch->io_ports[IDE_DATA_OFFSET];
201 #ifdef CONFIG_BLK_DEV_HD
203 /* Ignore disks for which handling by the legacy driver was requested
204 * by the used.
206 if (ch->io_ports[IDE_DATA_OFFSET] == 0x1f0)
207 ch->noprobe = 1; /* may be overridden by ide_setup() */
208 #endif
210 ch->major = majors[index];
211 sprintf(ch->name, "ide%d", index);
212 ch->bus_state = BUSSTATE_ON;
214 for (unit = 0; unit < MAX_DRIVES; ++unit) {
215 struct ata_device *drive = &ch->drives[unit];
217 drive->type = ATA_DISK;
218 drive->select.all = (unit << 4) | 0xa0;
219 drive->channel = ch;
220 drive->ready_stat = READY_STAT;
221 drive->bad_wstat = BAD_W_STAT;
222 sprintf(drive->name, "hd%c", 'a' + (index * MAX_DRIVES) + unit);
223 drive->max_failures = IDE_DEFAULT_MAX_FAILURES;
225 init_waitqueue_head(&drive->wqueue);
229 extern struct block_device_operations ide_fops[];
232 * Returns the (struct ata_device *) for a given device number. Return
233 * NULL if the given device number does not match any present drives.
235 struct ata_device *get_info_ptr(kdev_t i_rdev)
237 unsigned int major = major(i_rdev);
238 int h;
240 for (h = 0; h < MAX_HWIFS; ++h) {
241 struct ata_channel *ch = &ide_hwifs[h];
242 if (ch->present && major == ch->major) {
243 int unit = DEVICE_NR(i_rdev);
244 if (unit < MAX_DRIVES) {
245 struct ata_device *drive = &ch->drives[unit];
246 if (drive->present)
247 return drive;
249 break;
252 return NULL;
256 * This routine is called to flush all partitions and partition tables
257 * for a changed disk, and then re-read the new partition table.
258 * If we are revalidating a disk because of a media change, then we
259 * enter with usage == 0. If we are using an ioctl, we automatically have
260 * usage == 1 (we need an open channel to use an ioctl :-), so this
261 * is our limit.
263 int ide_revalidate(kdev_t dev)
265 struct ata_device *drive;
266 struct ata_channel *channel;
267 struct gendisk *disk;
268 int unit;
270 if ((drive = get_info_ptr(dev)) == NULL)
271 return -ENODEV;
272 if (ata_ops(drive) && ata_ops(drive)->revalidate) {
273 ata_get(ata_ops(drive));
274 ata_ops(drive)->revalidate(drive);
275 ata_put(ata_ops(drive));
277 channel = drive->channel;
278 unit = drive - channel->drives;
279 disk = channel->gd[unit];
280 disk->part[0].nr_sects = ata_capacity(drive);
281 return 0;
284 void ide_driver_module(void)
286 int i;
288 /* Don't reinit the probe if there is already one channel detected. */
289 for (i = 0; i < MAX_HWIFS; ++i) {
290 if (ide_hwifs[i].present)
291 return;
294 ideprobe_init();
298 * Release the data associated with a channel.
300 void ide_unregister(struct ata_channel *ch)
302 struct gendisk *gd;
303 struct ata_device *d;
304 spinlock_t *lock;
305 int unit;
306 int i;
307 unsigned long flags;
308 unsigned int p, minor;
309 struct ata_channel old;
310 int n_irq;
311 int n_ch;
313 spin_lock_irqsave(&ide_lock, flags);
315 if (!ch->present)
316 goto abort;
318 put_device(&ch->dev);
319 for (unit = 0; unit < MAX_DRIVES; ++unit) {
320 struct ata_device * drive = &ch->drives[unit];
322 if (!drive->present)
323 continue;
325 if (drive->busy || drive->usage)
326 goto abort;
328 if (ata_ops(drive)) {
329 if (ata_ops(drive)->cleanup) {
330 if (ata_ops(drive)->cleanup(drive))
331 goto abort;
332 } else
333 ata_unregister_device(drive);
336 ch->present = 0;
339 * All clear? Then blow away the buffer cache
341 spin_unlock_irqrestore(&ide_lock, flags);
343 for (unit = 0; unit < MAX_DRIVES; ++unit) {
344 struct ata_device * drive = &ch->drives[unit];
346 if (!drive->present)
347 continue;
349 minor = drive->select.b.unit << PARTN_BITS;
350 for (p = 0; p < (1<<PARTN_BITS); ++p) {
351 if (drive->part[p].nr_sects > 0) {
352 kdev_t devp = mk_kdev(ch->major, minor+p);
353 invalidate_device(devp, 0);
358 spin_lock_irqsave(&ide_lock, flags);
361 * Note that we only release the standard ports, and do not even try to
362 * handle any extra ports allocated for weird IDE interface chipsets.
365 if (ch->straight8) {
366 release_region(ch->io_ports[IDE_DATA_OFFSET], 8);
367 } else {
368 for (i = 0; i < 8; i++)
369 if (ch->io_ports[i])
370 release_region(ch->io_ports[i], 1);
372 if (ch->io_ports[IDE_CONTROL_OFFSET])
373 release_region(ch->io_ports[IDE_CONTROL_OFFSET], 1);
374 /* FIXME: check if we can remove this ifdef */
375 #if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
376 if (ch->io_ports[IDE_IRQ_OFFSET])
377 release_region(ch->io_ports[IDE_IRQ_OFFSET], 1);
378 #endif
381 * Remove us from the lock group.
384 lock = ch->lock;
385 d = ch->drive;
386 for (i = 0; i < MAX_DRIVES; ++i) {
387 struct ata_device *drive = &ch->drives[i];
389 if (drive->de) {
390 devfs_unregister(drive->de);
391 drive->de = NULL;
393 if (!drive->present)
394 continue;
396 /* FIXME: possibly unneccessary */
397 if (ch->drive == drive)
398 ch->drive = NULL;
400 if (drive->id != NULL) {
401 kfree(drive->id);
402 drive->id = NULL;
404 drive->present = 0;
405 blk_cleanup_queue(&drive->queue);
407 if (d->present)
408 ch->drive = d;
412 * Free the irq if we were the only channel using it.
414 * Free the lock group if we were the only member.
416 n_irq = n_ch = 0;
417 for (i = 0; i < MAX_HWIFS; ++i) {
418 struct ata_channel *tmp = &ide_hwifs[i];
420 if (!tmp->present)
421 continue;
423 if (tmp->irq == ch->irq)
424 ++n_irq;
425 if (tmp->lock == ch->lock)
426 ++n_ch;
428 if (n_irq == 1)
429 free_irq(ch->irq, ch);
430 if (n_ch == 1) {
431 kfree(ch->lock);
432 kfree(ch->active);
433 ch->lock = NULL;
434 ch->active = NULL;
437 #ifdef CONFIG_BLK_DEV_IDEDMA
438 ide_release_dma(ch);
439 #endif
442 * Remove us from the kernel's knowledge.
444 unregister_blkdev(ch->major, ch->name);
445 blk_dev[ch->major].data = NULL;
446 blk_dev[ch->major].queue = NULL;
447 blk_clear(ch->major);
448 gd = ch->gd[0];
449 if (gd) {
450 int i;
451 for (i = 0; i < MAX_DRIVES; i++)
452 del_gendisk(gd + i);
453 kfree(gd->major_name);
454 kfree(gd->part);
455 if (gd->de_arr)
456 kfree (gd->de_arr);
457 if (gd->flags)
458 kfree (gd->flags);
459 kfree(gd);
460 for (i = 0; i < MAX_DRIVES; i++)
461 ch->gd[i] = NULL;
465 * Reinitialize the channel handler, but preserve any special methods for
466 * it.
469 old = *ch;
470 init_hwif_data(ch, ch->index);
471 ch->lock = old.lock;
472 ch->tuneproc = old.tuneproc;
473 ch->speedproc = old.speedproc;
474 ch->selectproc = old.selectproc;
475 ch->resetproc = old.resetproc;
476 ch->intrproc = old.intrproc;
477 ch->maskproc = old.maskproc;
478 ch->quirkproc = old.quirkproc;
479 ch->ata_read = old.ata_read;
480 ch->ata_write = old.ata_write;
481 ch->atapi_read = old.atapi_read;
482 ch->atapi_write = old.atapi_write;
483 ch->udma_setup = old.udma_setup;
484 ch->udma_enable = old.udma_enable;
485 ch->udma_start = old.udma_start;
486 ch->udma_stop = old.udma_stop;
487 ch->udma_init = old.udma_init;
488 ch->udma_irq_status = old.udma_irq_status;
489 ch->udma_timeout = old.udma_timeout;
490 ch->udma_irq_lost = old.udma_irq_lost;
491 ch->busproc = old.busproc;
492 ch->bus_state = old.bus_state;
493 ch->dma_base = old.dma_base;
494 ch->dma_extra = old.dma_extra;
495 ch->config_data = old.config_data;
496 ch->select_data = old.select_data;
497 ch->proc = old.proc;
498 /* FIXME: most propably this is always right:! */
499 #ifndef CONFIG_BLK_DEV_IDECS
500 ch->irq = old.irq;
501 #endif
502 ch->major = old.major;
503 ch->chipset = old.chipset;
504 ch->autodma = old.autodma;
505 ch->udma_four = old.udma_four;
506 #ifdef CONFIG_PCI
507 ch->pci_dev = old.pci_dev;
508 #endif
509 ch->straight8 = old.straight8;
511 abort:
512 spin_unlock_irqrestore(&ide_lock, flags);
515 static int subdriver_match(struct ata_channel *channel, struct ata_operations *ops)
517 int count, unit;
519 if (!channel->present)
520 return 0;
522 count = 0;
523 for (unit = 0; unit < MAX_DRIVES; ++unit) {
524 struct ata_device *drive = &channel->drives[unit];
525 if (drive->present && !drive->driver) {
526 (*ops->attach)(drive);
527 if (drive->driver != NULL)
528 ++count;
531 return count;
535 * Register an IDE interface, specifing exactly the registers etc
536 * Set initializing=1 iff calling before probes have taken place.
538 int ide_register_hw(hw_regs_t *hw)
540 int h;
541 int retry = 1;
542 struct ata_channel *ch;
544 do {
545 for (h = 0; h < MAX_HWIFS; ++h) {
546 ch = &ide_hwifs[h];
547 if (ch->hw.io_ports[IDE_DATA_OFFSET] == hw->io_ports[IDE_DATA_OFFSET])
548 goto found;
550 for (h = 0; h < MAX_HWIFS; ++h) {
551 ch = &ide_hwifs[h];
552 if ((!ch->present && (ch->unit == ATA_PRIMARY) && !initializing) ||
553 (!ch->hw.io_ports[IDE_DATA_OFFSET] && initializing))
554 goto found;
556 for (h = 0; h < MAX_HWIFS; ++h)
557 ide_unregister(&ide_hwifs[h]);
558 } while (retry--);
560 return -1;
562 found:
563 ide_unregister(ch);
564 if (ch->present)
565 return -1;
566 memcpy(&ch->hw, hw, sizeof(*hw));
567 memcpy(ch->io_ports, ch->hw.io_ports, sizeof(ch->hw.io_ports));
568 ch->irq = hw->irq;
569 ch->noprobe = 0;
570 ch->chipset = hw->chipset;
572 if (!initializing) {
573 ideprobe_init();
574 /* FIXME: Do we really have to call it second time here?! */
575 ide_driver_module();
578 /* Look up whatever there is a subdriver, which will serve this
579 * device and execute the attach method it is providing.
582 struct ata_operations *tmp;
583 unsigned long flags;
585 spin_lock_irqsave(&ata_drivers_lock, flags);
586 for(tmp = ata_drivers; tmp; tmp = tmp->next) {
587 if (subdriver_match(ch, tmp) > 0)
588 break;
590 spin_unlock_irqrestore(&ata_drivers_lock, flags);
593 return (initializing || ch->present) ? h : -1;
596 /****************************************************************************
597 * FIXME: rewrite the following crap:
601 * Parsing for ide_setup():
603 * 1. the first char of s must be '='.
604 * 2. if the remainder matches one of the supplied keywords,
605 * the index (1 based) of the keyword is negated and returned.
606 * 3. if the remainder is a series of no more than max_vals numbers
607 * separated by commas, the numbers are saved in vals[] and a
608 * count of how many were saved is returned. Base10 is assumed,
609 * and base16 is allowed when prefixed with "0x". The number of
610 * values read will be placed in vals[0], and the values read will
611 * placed in vals[1] to vals[max_vals].
612 * 4. otherwise, zero is returned.
614 static int __init match_parm (char *s, const char *keywords[], int vals[], int max_vals)
616 int i;
618 if (*s++ == '=') {
620 * Try matching against the supplied keywords,
621 * and return -(index+1) if we match one
623 if (keywords != NULL) {
624 for (i = 0; *keywords != NULL; ++i) {
625 if (!strcmp(s, *keywords++))
626 return -(i+1);
630 * Look for a series of no more than "max_vals"
631 * numeric values separated by commas, in base10,
632 * or base16 when prefixed with "0x".
633 * Return a count of how many were found.
635 get_options(s, max_vals+1, vals);
636 return vals[0];
638 return 0;
642 * This sets reasonable default values into all fields of all instances of the
643 * channles and drives, but only on the first call. Subsequent calls have no
644 * effect (they don't wipe out anything).
646 * This routine is normally called at driver initialization time, but may also
647 * be called MUCH earlier during kernel "command-line" parameter processing.
648 * As such, we cannot depend on any other parts of the kernel (such as memory
649 * allocation) to be functioning yet.
651 * This is too bad, as otherwise we could dynamically allocate the ata_device
652 * structs as needed, rather than always consuming memory for the max possible
653 * number (MAX_HWIFS * MAX_DRIVES) of them.
655 #define MAGIC_COOKIE 0x12345678
656 static void __init init_global_data(void)
658 unsigned int h;
659 static unsigned long magic_cookie = MAGIC_COOKIE;
661 if (magic_cookie != MAGIC_COOKIE)
662 return; /* already initialized */
663 magic_cookie = 0;
665 /* Initialize all interface structures */
666 for (h = 0; h < MAX_HWIFS; ++h)
667 init_hwif_data(&ide_hwifs[h], h);
669 /* Add default hw interfaces */
670 ide_init_default_hwifs();
674 * This gets called VERY EARLY during initialization, to handle kernel "command
675 * line" strings beginning with "hdx=". It gets called even before the actual
676 * module gets initialized.
678 * Please look at Documentation/ide.txt to see the complete list of supported
679 * options.
681 static int __init ata_hd_setup(char *s)
683 int vals[4];
684 struct ata_channel *ch; /* FIXME: Channel parms should not be accessed in ata_hd_setup */
685 struct ata_device *drive;
686 unsigned int hw, unit;
687 const char max_drive = 'a' + ((MAX_HWIFS * MAX_DRIVES) - 1);
689 if (s[0] == '=') /* hd= is for hd.c driver and not us */
690 return 0;
692 printk(KERN_INFO "hd%s", s);
693 init_global_data();
695 if (s[0] >= 'a' && s[0] <= max_drive) {
696 static const char *hd_words[] = {"none", "noprobe", "nowerr", "cdrom",
697 "serialize", "autotune", "noautotune",
698 "slow", "flash", "scsi", NULL};
699 unit = s[0] - 'a';
700 hw = unit / MAX_DRIVES;
701 unit = unit % MAX_DRIVES;
702 ch = &ide_hwifs[hw];
703 drive = &ch->drives[unit];
705 /* Look for hdx=ide-* */
706 if (!strncmp(s+1, "=ide-", 5)) {
707 strncpy(drive->driver_req, s+2, 9);
708 goto done;
711 * Look for last lun option: "hdxlun="
713 if (!strncmp(s+1, "lun=", 4)) {
714 if (*get_options(s+5, 2, vals) || vals[0]!=1)
715 goto bad_option;
716 if (vals[1] >= 0 && vals[1] <= 7) {
717 drive->last_lun = vals[1];
718 drive->forced_lun = 1;
719 } else
720 printk(" -- BAD LAST LUN! Expected value from 0 to 7");
721 goto done;
723 switch (match_parm(s+1, hd_words, vals, 3)) {
724 case -1: /* "none" */
725 drive->nobios = 1; /* drop into "noprobe" */
726 case -2: /* "noprobe" */
727 drive->noprobe = 1;
728 goto done;
729 case -3: /* "nowerr" */
730 drive->bad_wstat = BAD_R_STAT;
731 ch->noprobe = 0; /* FIXME: Channel parm */
732 goto done;
733 case -4: /* "cdrom" */
734 drive->present = 1;
735 drive->type = ATA_ROM;
736 ch->noprobe = 0; /* FIXME: Channel parm */
737 goto done;
738 case -5: /* "serialize" */
739 printk(" -- USE \"ide%d=serialize\" INSTEAD", hw);
740 goto bad_option;
741 case -6: /* "autotune" */
742 drive->autotune = 1;
743 goto done;
744 case -7: /* "noautotune" */
745 drive->autotune = 2;
746 goto done;
747 case -8: /* "slow" */
748 ch->slow = 1; /* FIXME: Channel parm */
749 goto done;
750 case -9: /* "flash" */
751 drive->ata_flash = 1;
752 goto done;
753 case -10: /* "scsi" */
754 #if defined(CONFIG_BLK_DEV_IDESCSI) && defined(CONFIG_SCSI)
755 drive->scsi = 1;
756 goto done;
757 #else
758 drive->scsi = 0;
759 goto bad_option;
760 #endif
761 case 3: /* cyl,head,sect */
762 drive->type = ATA_DISK;
763 drive->cyl = drive->bios_cyl = vals[1];
764 drive->head = drive->bios_head = vals[2];
765 drive->sect = drive->bios_sect = vals[3];
766 drive->present = 1;
767 drive->forced_geom = 1;
768 ch->noprobe = 0;
769 goto done;
770 default:
771 goto bad_option;
775 bad_option:
776 printk(" -- BAD OPTION\n");
777 return 1;
779 done:
780 printk("\n");
782 return 1;
786 * This gets called VERY EARLY during initialization, to handle kernel "command
787 * line" strings beginning with "ide". It gets called even before the actual
788 * module gets initialized.
790 * Please look at Documentation/ide.txt to see the complete list of supported
791 * options.
793 int __init ide_setup(char *s)
795 int i, vals[4];
796 struct ata_channel *ch;
797 unsigned int hw;
798 const char max_ch = '0' + (MAX_HWIFS - 1);
800 printk(KERN_INFO "ide%s", s);
801 init_global_data();
803 #ifdef CONFIG_BLK_DEV_IDEDOUBLER
804 if (!strcmp(s, "=doubler")) {
805 extern int ide_doubler;
807 printk(KERN_INFO" : Enabled support for ATA doublers\n");
808 ide_doubler = 1;
809 return 1;
811 #endif
813 if (!strcmp(s, "=nodma")) {
814 printk(KERN_INFO "ATA: Prevented DMA\n");
815 noautodma = 1;
816 return 1;
819 #ifdef CONFIG_PCI
820 if (!strcmp(s, "=reverse")) {
821 ide_scan_direction = 1;
822 printk(" : Enabled support for IDE inverse scan order.\n");
823 return 1;
825 #endif
828 * Look for bus speed option: "idebus="
830 if (!strncmp(s, "bus=", 4)) {
831 if (*get_options(s+4, 2, vals) || vals[0] != 1)
832 goto bad_option;
833 idebus_parameter = vals[1];
834 goto done;
838 * Look for interface options: "idex="
840 if (s[0] >= '0' && s[0] <= max_ch) {
842 * Be VERY CAREFUL changing this: note hardcoded indexes below
844 const char *ide_options[] = {
845 "noprobe", "serialize", "autotune", "noautotune", "reset", "dma", "ata66", NULL };
846 const char *ide_words[] = {
847 "qd65xx", "ht6560b", "cmd640_vlb", "dtc2278", "umc8672", "ali14xx", "dc4030", NULL };
848 hw = s[0] - '0';
849 ch = &ide_hwifs[hw];
852 switch (match_parm(s + 1, ide_options, vals, 1)) {
853 case -7: /* ata66 */
854 #ifdef CONFIG_PCI
855 ch->udma_four = 1;
856 goto done;
857 #else
858 ch->udma_four = 0;
859 goto bad_channel;
860 #endif
861 case -6: /* dma */
862 ch->autodma = 1;
863 goto done;
864 case -5: /* reset */
865 ch->reset = 1;
866 goto done;
867 case -4: /* noautotune */
868 ch->drives[0].autotune = 2;
869 ch->drives[1].autotune = 2;
870 goto done;
871 case -3: /* autotune */
872 ch->drives[0].autotune = 1;
873 ch->drives[1].autotune = 1;
874 goto done;
875 case -2: /* "serialize" */
877 struct ata_channel *mate;
879 mate = &ide_hwifs[hw ^ 1];
880 ch->serialized = 1;
881 mate->serialized = 1;
883 goto done;
885 case -1: /* "noprobe" */
886 ch->noprobe = 1;
887 goto done;
891 * Check for specific chipset name
893 i = match_parm(s + 1, ide_words, vals, 3);
896 * Cryptic check to ensure chipset not already set for a channel:
898 if (i) { /* is parameter a chipset name? */
899 if (ide_hwifs[hw].chipset != ide_unknown)
900 goto bad_option; /* chipset already specified */
901 if (i != -7 && hw != 0)
902 goto bad_channel; /* chipset drivers are for "ide0=" only */
903 if (i != -7 && ide_hwifs[1].chipset != ide_unknown)
904 goto bad_option; /* chipset for 2nd port already specified */
905 printk("\n");
908 switch (i) {
909 #ifdef CONFIG_BLK_DEV_PDC4030
910 case -7: /* "dc4030" */
912 extern void init_pdc4030(void);
913 init_pdc4030();
914 goto done;
916 #endif
917 #ifdef CONFIG_BLK_DEV_ALI14XX
918 case -6: /* "ali14xx" */
920 extern void init_ali14xx(void);
921 init_ali14xx();
922 goto done;
924 #endif
925 #ifdef CONFIG_BLK_DEV_UMC8672
926 case -5: /* "umc8672" */
928 extern void init_umc8672(void);
929 init_umc8672();
930 goto done;
932 #endif
933 #ifdef CONFIG_BLK_DEV_DTC2278
934 case -4: /* "dtc2278" */
936 extern void init_dtc2278(void);
937 init_dtc2278();
938 goto done;
940 #endif
941 #ifdef CONFIG_BLK_DEV_CMD640
942 case -3: /* "cmd640_vlb" */
944 extern int cmd640_vlb; /* flag for cmd640.c */
945 cmd640_vlb = 1;
946 goto done;
948 #endif
949 #ifdef CONFIG_BLK_DEV_HT6560B
950 case -2: /* "ht6560b" */
952 extern void init_ht6560b(void);
953 init_ht6560b();
954 goto done;
956 #endif
957 #if CONFIG_BLK_DEV_QD65XX
958 case -1: /* "qd65xx" */
960 extern void init_qd65xx(void);
961 init_qd65xx();
962 goto done;
964 #endif
965 case 1: /* base */
966 vals[2] = vals[1] + 0x206; /* default ctl */
967 case 2: /* base,ctl */
968 vals[3] = 0; /* default irq = probe for it */
969 case 3: /* base,ctl,irq */
970 ch->hw.irq = vals[3];
971 ide_init_hwif_ports(&ch->hw, (ide_ioreg_t) vals[1], (ide_ioreg_t) vals[2], &ch->irq);
972 memcpy(ch->io_ports, ch->hw.io_ports, sizeof(ch->io_ports));
973 ch->irq = vals[3];
974 ch->noprobe = 0;
975 ch->chipset = ide_generic;
976 goto done;
978 case 0:
979 goto bad_option;
980 default:
981 printk(" -- SUPPORT NOT CONFIGURED IN THIS KERNEL\n");
982 return 1;
986 bad_option:
987 printk(" -- BAD OPTION\n");
988 return 1;
990 bad_channel:
991 printk("-- NOT SUPPORTED ON ide%d", hw);
993 done:
994 printk("\n");
996 return 1;
999 /****************************************************************************/
1001 int ata_register_device(struct ata_device *drive, struct ata_operations *driver)
1003 unsigned long flags;
1005 /* FIXME: The locking here doesn't make the slightest sense! */
1006 spin_lock_irqsave(&ide_lock, flags);
1008 if (!drive->present || drive->driver != NULL || drive->busy || drive->usage) {
1009 spin_unlock_irqrestore(&ide_lock, flags);
1010 return 1;
1013 drive->driver = driver;
1014 spin_unlock_irqrestore(&ide_lock, flags);
1016 /* Default autotune or requested autotune */
1017 if (drive->autotune != 2) {
1018 struct ata_channel *ch = drive->channel;
1019 if (ch->udma_setup) {
1022 * Force DMAing for the beginning of the check. Some
1023 * chipsets appear to do interesting things, if not
1024 * checked and cleared.
1026 * PARANOIA!!!
1029 spin_lock_irqsave(ch->lock, flags);
1030 udma_enable(drive, 0, 0);
1031 ch->udma_setup(drive, ch->modes_map);
1032 #ifdef CONFIG_BLK_DEV_IDE_TCQ_DEFAULT
1033 udma_tcq_enable(drive, 1);
1034 #endif
1035 spin_unlock_irqrestore(ch->lock, flags);
1038 /* Only CD-ROMs and tape drives support DSC overlap. But only
1039 * if they are alone on a channel. */
1040 if (drive->type == ATA_ROM || drive->type == ATA_TAPE) {
1041 int single = 0;
1042 int unit;
1044 for (unit = 0; unit < MAX_DRIVES; ++unit)
1045 if (drive->channel->drives[unit].present)
1046 ++single;
1048 drive->dsc_overlap = (single == 1);
1049 } else
1050 drive->dsc_overlap = 0;
1053 return 0;
1057 * This is in fact the default cleanup routine.
1059 * FIXME: Check whatever we maybe don't call it twice!.
1061 int ata_unregister_device(struct ata_device *drive)
1063 if (drive->usage || drive->busy || !ata_ops(drive))
1064 return 1;
1066 #if defined(CONFIG_BLK_DEV_ISAPNP) && defined(CONFIG_ISAPNP) && defined(MODULE)
1067 pnpide_init(0);
1068 #endif
1069 drive->driver = NULL;
1071 return 0;
1076 * Register an ATA subdriver for a particular device type.
1078 int register_ata_driver(struct ata_operations *driver)
1080 unsigned long flags;
1081 int index;
1082 int count = 0;
1084 spin_lock_irqsave(&ata_drivers_lock, flags);
1085 driver->next = ata_drivers;
1086 ata_drivers = driver;
1087 spin_unlock_irqrestore(&ata_drivers_lock, flags);
1089 for (index = 0; index < MAX_HWIFS; ++index)
1090 count += subdriver_match(&ide_hwifs[index], driver);
1092 return count;
1095 EXPORT_SYMBOL(register_ata_driver);
1098 * Unregister an ATA sub-driver for a particular device type.
1100 void unregister_ata_driver(struct ata_operations *driver)
1102 struct ata_operations **tmp;
1103 unsigned long flags;
1104 int index;
1105 int unit;
1107 spin_lock_irqsave(&ata_drivers_lock, flags);
1108 for (tmp = &ata_drivers; *tmp != NULL; tmp = &(*tmp)->next) {
1109 if (*tmp == driver) {
1110 *tmp = driver->next;
1111 break;
1114 spin_unlock_irqrestore(&ata_drivers_lock, flags);
1116 for (index = 0; index < MAX_HWIFS; ++index) {
1117 struct ata_channel *ch = &ide_hwifs[index];
1118 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1119 struct ata_device *drive = &ch->drives[unit];
1121 if (drive->driver == driver)
1122 (*ata_ops(drive)->cleanup)(drive);
1127 EXPORT_SYMBOL(ide_hwifs);
1128 EXPORT_SYMBOL(ide_lock);
1130 devfs_handle_t ide_devfs_handle;
1132 EXPORT_SYMBOL(ata_register_device);
1133 EXPORT_SYMBOL(ata_unregister_device);
1134 EXPORT_SYMBOL(ide_register_hw);
1135 EXPORT_SYMBOL(ide_unregister);
1136 EXPORT_SYMBOL(get_info_ptr);
1139 * Handle power handling related events ths system informs us about.
1141 static int ata_sys_notify(struct notifier_block *this, unsigned long event, void *x)
1143 int i;
1145 switch (event) {
1146 case SYS_HALT:
1147 case SYS_POWER_OFF:
1148 case SYS_RESTART:
1149 break;
1150 default:
1151 return NOTIFY_DONE;
1154 printk(KERN_INFO "flushing ATA/ATAPI devices: ");
1156 for (i = 0; i < MAX_HWIFS; i++) {
1157 int unit;
1158 struct ata_channel *ch = &ide_hwifs[i];
1160 if (!ch->present)
1161 continue;
1163 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1164 struct ata_device *drive = &ch->drives[unit];
1166 if (!drive->present)
1167 continue;
1169 /* set the drive to standby */
1170 printk("%s ", drive->name);
1171 if (ata_ops(drive)) {
1172 if (event != SYS_RESTART) {
1173 if (ata_ops(drive)->standby && ata_ops(drive)->standby(drive))
1174 continue;
1177 if (ata_ops(drive)->cleanup)
1178 ata_ops(drive)->cleanup(drive);
1182 printk("\n");
1183 return NOTIFY_DONE;
1186 static struct notifier_block ata_notifier = {
1187 ata_sys_notify,
1188 NULL,
1193 * This is the global initialization entry point.
1195 static int __init ata_module_init(void)
1197 printk(KERN_INFO "ATA/ATAPI device driver v" VERSION "\n");
1199 ide_devfs_handle = devfs_mk_dir(NULL, "ide", NULL);
1202 * Because most of the ATA adapters represent the timings in unit of
1203 * bus clocks, and there is no known reliable way to detect the bus
1204 * clock frequency, we assume 50 MHz for non-PCI (VLB, EISA) and 33 MHz
1205 * for PCI based systems. Since assuming only hurts performance and not
1206 * stability, this is OK. The user can change this on the command line
1207 * by using the "idebus=XX" parameter. While the system_bus_speed
1208 * variable is in kHz units, we accept both MHz and kHz entry on the
1209 * command line for backward compatibility.
1212 system_bus_speed = 50000;
1214 if (pci_present())
1215 system_bus_speed = 33333;
1217 if (idebus_parameter >= 20 && idebus_parameter <= 80) {
1219 system_bus_speed = idebus_parameter * 1000;
1221 switch (system_bus_speed) {
1222 case 33000: system_bus_speed = 33333; break;
1223 case 37000: system_bus_speed = 37500; break;
1224 case 41000: system_bus_speed = 41666; break;
1225 case 66000: system_bus_speed = 66666; break;
1229 if (idebus_parameter >= 20000 && idebus_parameter <= 80000)
1230 system_bus_speed = idebus_parameter;
1232 printk(KERN_INFO "ATA: %s bus speed %d.%dMHz\n",
1233 pci_present() ? "PCI" : "System", system_bus_speed / 1000, system_bus_speed / 100 % 10);
1235 init_global_data();
1237 initializing = 1;
1239 #ifdef CONFIG_PCI
1241 * Register the host chip drivers.
1243 # ifdef CONFIG_BLK_DEV_PIIX
1244 init_piix();
1245 # endif
1246 # ifdef CONFIG_BLK_DEV_VIA82CXXX
1247 init_via82cxxx();
1248 # endif
1249 # ifdef CONFIG_BLK_DEV_PDC202XX
1250 init_pdc202xx();
1251 # endif
1252 # ifdef CONFIG_BLK_DEV_RZ1000
1253 init_rz1000();
1254 # endif
1255 # ifdef CONFIG_BLK_DEV_SIS5513
1256 init_sis5513();
1257 # endif
1258 # ifdef CONFIG_BLK_DEV_CMD64X
1259 init_cmd64x();
1260 # endif
1261 # ifdef CONFIG_BLK_DEV_OPTI621
1262 init_opti621();
1263 # endif
1264 # ifdef CONFIG_BLK_DEV_TRM290
1265 init_trm290();
1266 # endif
1267 # ifdef CONFIG_BLK_DEV_NS87415
1268 init_ns87415();
1269 # endif
1270 # ifdef CONFIG_BLK_DEV_AEC62XX
1271 init_aec62xx();
1272 # endif
1273 # ifdef CONFIG_BLK_DEV_SL82C105
1274 init_sl82c105();
1275 # endif
1276 # ifdef CONFIG_BLK_DEV_HPT34X
1277 init_hpt34x();
1278 # endif
1279 # ifdef CONFIG_BLK_DEV_HPT366
1280 init_hpt366();
1281 # endif
1282 # ifdef CONFIG_BLK_DEV_ALI15X3
1283 init_ali15x3();
1284 # endif
1285 # ifdef CONFIG_BLK_DEV_CY82C693
1286 init_cy82c693();
1287 # endif
1288 # ifdef CONFIG_BLK_DEV_CS5530
1289 init_cs5530();
1290 # endif
1291 # ifdef CONFIG_BLK_DEV_AMD74XX
1292 init_amd74xx();
1293 # endif
1294 # ifdef CONFIG_BLK_DEV_SVWKS
1295 init_svwks();
1296 # endif
1297 # ifdef CONFIG_BLK_DEV_IT8172
1298 init_it8172();
1299 # endif
1301 init_ata_pci_misc();
1304 * Detect and initialize "known" IDE host chip types.
1306 if (pci_present()) {
1307 # ifdef CONFIG_PCI
1308 ide_scan_pcibus(ide_scan_direction);
1309 # else
1310 # ifdef CONFIG_BLK_DEV_RZ1000
1311 ide_probe_for_rz100x();
1312 # endif
1313 # endif
1315 #endif
1317 #ifdef CONFIG_ETRAX_IDE
1318 init_e100_ide();
1319 #endif
1320 #ifdef CONFIG_BLK_DEV_CMD640
1321 ide_probe_for_cmd640x();
1322 #endif
1323 #ifdef CONFIG_BLK_DEV_PDC4030
1324 ide_probe_for_pdc4030();
1325 #endif
1326 #ifdef CONFIG_BLK_DEV_IDE_PMAC
1327 pmac_ide_probe();
1328 #endif
1329 #ifdef CONFIG_BLK_DEV_IDE_ICSIDE
1330 icside_init();
1331 #endif
1332 #ifdef CONFIG_BLK_DEV_IDE_RAPIDE
1333 rapide_init();
1334 #endif
1335 #ifdef CONFIG_BLK_DEV_GAYLE
1336 gayle_init();
1337 #endif
1338 #ifdef CONFIG_BLK_DEV_FALCON_IDE
1339 falconide_init();
1340 #endif
1341 #ifdef CONFIG_BLK_DEV_MAC_IDE
1342 macide_init();
1343 #endif
1344 #ifdef CONFIG_BLK_DEV_Q40IDE
1345 q40ide_init();
1346 #endif
1347 #ifdef CONFIG_BLK_DEV_BUDDHA
1348 buddha_init();
1349 #endif
1350 #if defined(CONFIG_BLK_DEV_ISAPNP) && defined(CONFIG_ISAPNP)
1351 pnpide_init(1);
1352 #endif
1354 #if defined(CONFIG_BLK_DEV_IDE) || defined(CONFIG_BLK_DEV_IDE_MODULE)
1355 # if defined(__mc68000__) || defined(CONFIG_APUS)
1356 if (ide_hwifs[0].io_ports[IDE_DATA_OFFSET]) {
1357 ide_get_lock(&ide_irq_lock, NULL, NULL);/* for atari only */
1358 disable_irq(ide_hwifs[0].irq); /* disable_irq_nosync ?? */
1359 // disable_irq_nosync(ide_hwifs[0].irq);
1361 # endif
1363 ideprobe_init();
1365 # if defined(__mc68000__) || defined(CONFIG_APUS)
1366 if (ide_hwifs[0].io_ports[IDE_DATA_OFFSET]) {
1367 enable_irq(ide_hwifs[0].irq);
1368 ide_release_lock(&ide_irq_lock);/* for atari only */
1370 # endif
1371 #endif
1374 * Initialize all device type driver modules.
1376 #ifdef CONFIG_BLK_DEV_IDEDISK
1377 idedisk_init();
1378 #endif
1379 #ifdef CONFIG_BLK_DEV_IDECD
1380 ide_cdrom_init();
1381 #endif
1382 #ifdef CONFIG_BLK_DEV_IDETAPE
1383 idetape_init();
1384 #endif
1385 #ifdef CONFIG_BLK_DEV_IDEFLOPPY
1386 idefloppy_init();
1387 #endif
1389 initializing = 0;
1391 register_reboot_notifier(&ata_notifier);
1393 return 0;
1396 static char *options = NULL;
1398 static int __init init_ata(void)
1401 if (options != NULL && *options) {
1402 char *next = options;
1404 while ((options = next) != NULL) {
1405 if ((next = strchr(options,' ')) != NULL)
1406 *next++ = 0;
1407 if (!strncmp(options,"hd",2)) {
1408 if (!ata_hd_setup(options+2))
1409 printk(KERN_ERR "Unknown option '%s'\n", options);
1411 else if (!strncmp(options,"ide",3)) {
1412 if (!ide_setup(options+3))
1413 printk(KERN_ERR "Unknown option '%s'\n", options);
1417 return ata_module_init();
1420 static void __exit cleanup_ata(void)
1422 int h;
1424 unregister_reboot_notifier(&ata_notifier);
1425 for (h = 0; h < MAX_HWIFS; ++h) {
1426 ide_unregister(&ide_hwifs[h]);
1429 devfs_unregister(ide_devfs_handle);
1432 module_init(init_ata);
1433 module_exit(cleanup_ata);
1435 #ifndef MODULE
1437 /* command line option parser */
1438 __setup("ide", ide_setup);
1439 __setup("hd", ata_hd_setup);
1441 #endif