2 * linux/drivers/ide/ide-probe.c Version 1.11 Mar 05, 2003
4 * Copyright (C) 1994-1998 Linus Torvalds & authors (see below)
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>
12 * See linux/MAINTAINERS for address of current maintainer.
14 * This is the IDE probe module, as evolved from hd.c and ide.c.
16 * Version 1.00 move drive probing code from ide.c to ide-probe.c
17 * Version 1.01 fix compilation problem for m68k
18 * Version 1.02 increase WAIT_PIDENTIFY to avoid CD-ROM locking at boot
20 * Version 1.03 fix for (hwif->chipset == ide_4drives)
21 * Version 1.04 fixed buggy treatments of known flash memory cards
23 * Version 1.05 fix for (hwif->chipset == ide_pdc4030)
25 * allowed for secondary flash card to be detectable
26 * with new flag : drive->ata_flash : 1;
27 * Version 1.06 stream line request queue and prep for cascade project.
28 * Version 1.07 max_sect <= 255; slower disks would get behind and
29 * then fall over when they get to 256. Paul G.
30 * Version 1.10 Update set for new IDE. drive->id is now always
31 * valid after probe time even with noprobe
34 #undef REALLY_SLOW_IO /* most systems can safely undef this */
36 #include <linux/config.h>
37 #include <linux/module.h>
38 #include <linux/types.h>
39 #include <linux/string.h>
40 #include <linux/kernel.h>
41 #include <linux/timer.h>
43 #include <linux/interrupt.h>
44 #include <linux/major.h>
45 #include <linux/errno.h>
46 #include <linux/genhd.h>
47 #include <linux/slab.h>
48 #include <linux/delay.h>
49 #include <linux/ide.h>
50 #include <linux/spinlock.h>
51 #include <linux/kmod.h>
52 #include <linux/pci.h>
54 #include <asm/byteorder.h>
56 #include <asm/uaccess.h>
60 * generic_id - add a generic drive id
61 * @drive: drive to make an ID block for
63 * Add a fake id field to the drive we are passed. This allows
64 * use to skip a ton of NULL checks (which people always miss)
65 * and make drive properties unconditional outside of this file
68 static void generic_id(ide_drive_t *drive)
70 drive->id->cyls = drive->cyl;
71 drive->id->heads = drive->head;
72 drive->id->sectors = drive->sect;
73 drive->id->cur_cyls = drive->cyl;
74 drive->id->cur_heads = drive->head;
75 drive->id->cur_sectors = drive->sect;
79 * drive_is_flashcard - check for compact flash
80 * @drive: drive to check
82 * CompactFlash cards and their brethern pretend to be removable
84 * (1) they never have a slave unit, and
85 * (2) they don't have doorlock mechanisms.
86 * This test catches them, and is invoked elsewhere when setting
87 * appropriate config bits.
89 * FIXME: This treatment is probably applicable for *all* PCMCIA (PC CARD)
90 * devices, so in linux 2.3.x we should change this to just treat all
91 * PCMCIA drives this way, and get rid of the model-name tests below
92 * (too big of an interface change for 2.4.x).
93 * At that time, we might also consider parameterizing the timeouts and
94 * retries, since these are MUCH faster than mechanical drives. -M.Lord
97 static inline int drive_is_flashcard (ide_drive_t *drive)
99 struct hd_driveid *id = drive->id;
101 if (drive->removable) {
102 if (id->config == 0x848a) return 1; /* CompactFlash */
103 if (!strncmp(id->model, "KODAK ATA_FLASH", 15) /* Kodak */
104 || !strncmp(id->model, "Hitachi CV", 10) /* Hitachi */
105 || !strncmp(id->model, "SunDisk SDCFB", 13) /* old SanDisk */
106 || !strncmp(id->model, "SanDisk SDCFB", 13) /* SanDisk */
107 || !strncmp(id->model, "HAGIWARA HPC", 12) /* Hagiwara */
108 || !strncmp(id->model, "LEXAR ATA_FLASH", 15) /* Lexar */
109 || !strncmp(id->model, "ATA_FLASH", 9)) /* Simple Tech */
111 return 1; /* yes, it is a flash memory card */
114 return 0; /* no, it is not a flash memory card */
118 * do_identify - identify a drive
119 * @drive: drive to identify
122 * Called when we have issued a drive identify command to
123 * read and parse the results. This function is run with
124 * interrupts disabled.
127 static inline void do_identify (ide_drive_t *drive, u8 cmd)
129 ide_hwif_t *hwif = HWIF(drive);
131 struct hd_driveid *id;
134 /* read 512 bytes of id info */
135 hwif->ata_input_data(drive, id, SECTOR_WORDS);
141 #if defined (CONFIG_SCSI_EATA_DMA) || defined (CONFIG_SCSI_EATA_PIO) || defined (CONFIG_SCSI_EATA)
143 * EATA SCSI controllers do a hardware ATA emulation:
144 * Ignore them if there is a driver for them available.
146 if ((id->model[0] == 'P' && id->model[1] == 'M') ||
147 (id->model[0] == 'S' && id->model[1] == 'K')) {
148 printk("%s: EATA SCSI HBA %.10s\n", drive->name, id->model);
151 #endif /* CONFIG_SCSI_EATA_DMA || CONFIG_SCSI_EATA_PIO */
154 * WIN_IDENTIFY returns little-endian info,
155 * WIN_PIDENTIFY *usually* returns little-endian info.
157 if (cmd == WIN_PIDENTIFY) {
158 if ((id->model[0] == 'N' && id->model[1] == 'E') /* NEC */
159 || (id->model[0] == 'F' && id->model[1] == 'X') /* Mitsumi */
160 || (id->model[0] == 'P' && id->model[1] == 'i'))/* Pioneer */
161 /* Vertos drives may still be weird */
164 ide_fixstring(id->model, sizeof(id->model), bswap);
165 ide_fixstring(id->fw_rev, sizeof(id->fw_rev), bswap);
166 ide_fixstring(id->serial_no, sizeof(id->serial_no), bswap);
168 if (strstr(id->model, "E X A B Y T E N E S T"))
171 /* we depend on this a lot! */
172 id->model[sizeof(id->model)-1] = '\0';
173 printk("%s: %s, ", drive->name, id->model);
178 * Check for an ATAPI device
180 if (cmd == WIN_PIDENTIFY) {
181 u8 type = (id->config >> 8) & 0x1f;
185 if (!strstr(id->model, "CD-ROM")) {
186 if (!strstr(id->model, "oppy") &&
187 !strstr(id->model, "poyp") &&
188 !strstr(id->model, "ZIP"))
189 printk("cdrom or floppy?, assuming ");
190 if (drive->media != ide_cdrom) {
192 drive->removable = 1;
196 /* Early cdrom models used zero */
199 drive->removable = 1;
201 /* kludge for Apple PowerBook internal zip */
202 if (!strstr(id->model, "CD-ROM") &&
203 strstr(id->model, "ZIP")) {
209 printk ("CD/DVD-ROM");
216 drive->removable = 1;
219 printk("UNKNOWN (type %d)", type);
228 * Not an ATAPI device: looks like a "regular" hard disk
230 if (id->config & (1<<7))
231 drive->removable = 1;
233 if (drive_is_flashcard(drive))
235 drive->media = ide_disk;
236 printk("%s DISK drive\n", (drive->is_flash) ? "CFA" : "ATA" );
247 * actual_try_to_identify - send ata/atapi identify
248 * @drive: drive to identify
249 * @cmd: command to use
251 * try_to_identify() sends an ATA(PI) IDENTIFY request to a drive
252 * and waits for a response. It also monitors irqs while this is
253 * happening, in hope of automatically determining which one is
254 * being used by the interface.
256 * Returns: 0 device was identified
257 * 1 device timed-out (no response to identify request)
258 * 2 device aborted the command (refused to identify itself)
261 static int actual_try_to_identify (ide_drive_t *drive, u8 cmd)
263 ide_hwif_t *hwif = HWIF(drive);
265 unsigned long hd_status;
266 unsigned long timeout;
269 /* take a deep breath */
272 if (IDE_CONTROL_REG) {
273 a = hwif->INB(IDE_ALTSTATUS_REG);
274 s = hwif->INB(IDE_STATUS_REG);
275 if ((a ^ s) & ~INDEX_STAT) {
276 printk(KERN_INFO "%s: probing with STATUS(0x%02x) instead of "
277 "ALTSTATUS(0x%02x)\n", drive->name, s, a);
278 /* ancient Seagate drives, broken interfaces */
279 hd_status = IDE_STATUS_REG;
281 /* use non-intrusive polling */
282 hd_status = IDE_ALTSTATUS_REG;
285 hd_status = IDE_STATUS_REG;
287 /* set features register for atapi
288 * identify command to be sure of reply
290 if ((cmd == WIN_PIDENTIFY))
291 /* disable dma & overlap */
292 hwif->OUTB(0, IDE_FEATURE_REG);
294 /* ask drive for ID */
295 hwif->OUTB(cmd, IDE_COMMAND_REG);
297 timeout = ((cmd == WIN_IDENTIFY) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
300 if (time_after(jiffies, timeout)) {
301 /* drive timed-out */
304 /* give drive a breather */
306 } while ((hwif->INB(hd_status)) & BUSY_STAT);
308 /* wait for IRQ and DRQ_STAT */
310 if (OK_STAT((hwif->INB(IDE_STATUS_REG)), DRQ_STAT, BAD_R_STAT)) {
313 /* local CPU only; some systems need this */
314 local_irq_save(flags);
315 /* drive returned ID */
316 do_identify(drive, cmd);
317 /* drive responded with ID */
319 /* clear drive IRQ */
320 (void) hwif->INB(IDE_STATUS_REG);
321 local_irq_restore(flags);
323 /* drive refused ID */
330 * try_to_identify - try to identify a drive
331 * @drive: drive to probe
332 * @cmd: command to use
334 * Issue the identify command and then do IRQ probing to
335 * complete the identification when needed by finding the
336 * IRQ the drive is attached to
339 static int try_to_identify (ide_drive_t *drive, u8 cmd)
341 ide_hwif_t *hwif = HWIF(drive);
344 unsigned long cookie = 0;
347 * Disable device irq unless we need to
348 * probe for it. Otherwise we'll get spurious
349 * interrupts during the identify-phase that
350 * the irq handler isn't expecting.
352 if (IDE_CONTROL_REG) {
353 u8 ctl = drive->ctl | 2;
356 cookie = probe_irq_on();
357 /* enable device irq */
360 hwif->OUTB(ctl, IDE_CONTROL_REG);
363 retval = actual_try_to_identify(drive, cmd);
367 /* mask device irq */
368 hwif->OUTB(drive->ctl|2, IDE_CONTROL_REG);
369 /* clear drive IRQ */
370 (void) hwif->INB(IDE_STATUS_REG);
372 irq = probe_irq_off(cookie);
377 /* Mmmm.. multiple IRQs..
378 * don't know which was ours
380 printk("%s: IRQ probe failed (0x%lx)\n",
381 drive->name, cookie);
390 * do_probe - probe an IDE device
391 * @drive: drive to probe
392 * @cmd: command to use
394 * do_probe() has the difficult job of finding a drive if it exists,
395 * without getting hung up if it doesn't exist, without trampling on
396 * ethernet cards, and without leaving any IRQs dangling to haunt us later.
398 * If a drive is "known" to exist (from CMOS or kernel parameters),
399 * but does not respond right away, the probe will "hang in there"
400 * for the maximum wait time (about 30 seconds), otherwise it will
401 * exit much more quickly.
403 * Returns: 0 device was identified
404 * 1 device timed-out (no response to identify request)
405 * 2 device aborted the command (refused to identify itself)
406 * 3 bad status from device (possible for ATAPI drives)
407 * 4 probe was not attempted because failure was obvious
410 static int do_probe (ide_drive_t *drive, u8 cmd)
413 ide_hwif_t *hwif = HWIF(drive);
415 if (drive->present) {
416 /* avoid waiting for inappropriate probes */
417 if ((drive->media != ide_disk) && (cmd == WIN_IDENTIFY))
421 printk("probing for %s: present=%d, media=%d, probetype=%s\n",
422 drive->name, drive->present, drive->media,
423 (cmd == WIN_IDENTIFY) ? "ATA" : "ATAPI");
426 /* needed for some systems
427 * (e.g. crw9624 as drive0 with disk as slave)
432 if (hwif->INB(IDE_SELECT_REG) != drive->select.all && !drive->present) {
433 if (drive->select.b.unit != 0) {
434 /* exit with drive0 selected */
435 SELECT_DRIVE(&hwif->drives[0]);
436 /* allow BUSY_STAT to assert & clear */
439 /* no i/f present: mmm.. this should be a 4 -ml */
443 if (OK_STAT((hwif->INB(IDE_STATUS_REG)), READY_STAT, BUSY_STAT) ||
444 drive->present || cmd == WIN_PIDENTIFY) {
445 /* send cmd and wait */
446 if ((rc = try_to_identify(drive, cmd))) {
447 /* failed: try again */
448 rc = try_to_identify(drive,cmd);
450 if (hwif->INB(IDE_STATUS_REG) == (BUSY_STAT|READY_STAT))
453 if ((rc == 1 && cmd == WIN_PIDENTIFY) &&
454 ((drive->autotune == IDE_TUNE_DEFAULT) ||
455 (drive->autotune == IDE_TUNE_AUTO))) {
456 unsigned long timeout;
457 printk("%s: no response (status = 0x%02x), "
458 "resetting drive\n", drive->name,
459 hwif->INB(IDE_STATUS_REG));
461 hwif->OUTB(drive->select.all, IDE_SELECT_REG);
463 hwif->OUTB(WIN_SRST, IDE_COMMAND_REG);
465 while (((hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) &&
466 time_before(jiffies, timeout + WAIT_WORSTCASE))
468 rc = try_to_identify(drive, cmd);
471 printk("%s: no response (status = 0x%02x)\n",
472 drive->name, hwif->INB(IDE_STATUS_REG));
473 /* ensure drive irq is clear */
474 (void) hwif->INB(IDE_STATUS_REG);
476 /* not present or maybe ATAPI */
479 if (drive->select.b.unit != 0) {
480 /* exit with drive0 selected */
481 SELECT_DRIVE(&hwif->drives[0]);
483 /* ensure drive irq is clear */
484 (void) hwif->INB(IDE_STATUS_REG);
492 static void enable_nest (ide_drive_t *drive)
494 ide_hwif_t *hwif = HWIF(drive);
495 unsigned long timeout;
497 printk("%s: enabling %s -- ", hwif->name, drive->id->model);
500 hwif->OUTB(EXABYTE_ENABLE_NEST, IDE_COMMAND_REG);
501 timeout = jiffies + WAIT_WORSTCASE;
503 if (time_after(jiffies, timeout)) {
504 printk("failed (timeout)\n");
508 } while ((hwif->INB(IDE_STATUS_REG)) & BUSY_STAT);
512 if (!OK_STAT((hwif->INB(IDE_STATUS_REG)), 0, BAD_STAT)) {
513 printk("failed (status = 0x%02x)\n", hwif->INB(IDE_STATUS_REG));
518 /* if !(success||timed-out) */
519 if (do_probe(drive, WIN_IDENTIFY) >= 2) {
520 /* look for ATAPI device */
521 (void) do_probe(drive, WIN_PIDENTIFY);
526 * probe_for_drives - upper level drive probe
527 * @drive: drive to probe for
529 * probe_for_drive() tests for existence of a given drive using do_probe()
530 * and presents things to the user as needed.
532 * Returns: 0 no device was found
533 * 1 device was found (note: drive->present might
537 static inline u8 probe_for_drive (ide_drive_t *drive)
540 * In order to keep things simple we have an id
541 * block for all drives at all times. If the device
542 * is pre ATA or refuses ATA/ATAPI identify we
543 * will add faked data to this.
545 * Also note that 0 everywhere means "can't do X"
548 drive->id = kmalloc(SECTOR_WORDS *4, GFP_KERNEL);
550 if(drive->id == NULL)
552 printk(KERN_ERR "ide: out of memory for id data.\n");
555 memset(drive->id, 0, SECTOR_WORDS * 4);
556 strcpy(drive->id->model, "UNKNOWN");
561 /* if !(success||timed-out) */
562 if (do_probe(drive, WIN_IDENTIFY) >= 2) {
563 /* look for ATAPI device */
564 (void) do_probe(drive, WIN_PIDENTIFY);
566 if (strstr(drive->id->model, "E X A B Y T E N E S T"))
569 /* drive not found */
572 /* identification failed? */
573 if (!drive->id_read) {
574 if (drive->media == ide_disk) {
575 printk(KERN_INFO "%s: non-IDE drive, CHS=%d/%d/%d\n",
576 drive->name, drive->cyl,
577 drive->head, drive->sect);
578 } else if (drive->media == ide_cdrom) {
579 printk(KERN_INFO "%s: ATAPI cdrom (?)\n", drive->name);
582 printk(KERN_WARNING "%s: Unknown device on bus refused identification. Ignoring.\n", drive->name);
586 /* drive was found */
590 /* The drive wasn't being helpful. Add generic info only */
593 return drive->present;
596 static void hwif_release_dev (struct device *dev)
598 ide_hwif_t *hwif = container_of(dev, ide_hwif_t, gendev);
600 up(&hwif->gendev_rel_sem);
603 static void hwif_register (ide_hwif_t *hwif)
605 /* register with global device tree */
606 strlcpy(hwif->gendev.bus_id,hwif->name,BUS_ID_SIZE);
607 hwif->gendev.driver_data = hwif;
608 if (hwif->gendev.parent == NULL) {
610 hwif->gendev.parent = &hwif->pci_dev->dev;
612 /* Would like to do = &device_legacy */
613 hwif->gendev.parent = NULL;
615 hwif->gendev.release = hwif_release_dev;
616 device_register(&hwif->gendev);
619 static int wait_hwif_ready(ide_hwif_t *hwif)
623 printk(KERN_DEBUG "Probing IDE interface %s...\n", hwif->name);
625 /* Let HW settle down a bit from whatever init state we
629 /* Wait for BSY bit to go away, spec timeout is 30 seconds,
630 * I know of at least one disk who takes 31 seconds, I use 35
633 rc = ide_wait_not_busy(hwif, 35000);
637 /* Now make sure both master & slave are ready */
638 SELECT_DRIVE(&hwif->drives[0]);
639 hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
641 rc = ide_wait_not_busy(hwif, 10000);
644 SELECT_DRIVE(&hwif->drives[1]);
645 hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
647 rc = ide_wait_not_busy(hwif, 10000);
649 /* Exit function with master reselected (let's be sane) */
650 SELECT_DRIVE(&hwif->drives[0]);
656 * ide_undecoded_slave - look for bad CF adapters
659 * Analyse the drives on the interface and attempt to decide if we
660 * have the same drive viewed twice. This occurs with crap CF adapters
661 * and PCMCIA sometimes.
664 void ide_undecoded_slave(ide_hwif_t *hwif)
666 ide_drive_t *drive0 = &hwif->drives[0];
667 ide_drive_t *drive1 = &hwif->drives[1];
669 if (drive0->present == 0 || drive1->present == 0)
672 /* If the models don't match they are not the same product */
673 if (strcmp(drive0->id->model, drive1->id->model))
676 /* Serial numbers do not match */
677 if (strncmp(drive0->id->serial_no, drive1->id->serial_no, 20))
680 /* No serial number, thankfully very rare for CF */
681 if (drive0->id->serial_no[0] == 0)
684 /* Appears to be an IDE flash adapter with decode bugs */
685 printk(KERN_WARNING "ide-probe: ignoring undecoded slave\n");
690 EXPORT_SYMBOL_GPL(ide_undecoded_slave);
693 * This routine only knows how to look for drive units 0 and 1
694 * on an interface, so any setting of MAX_DRIVES > 2 won't work here.
696 static void probe_hwif(ide_hwif_t *hwif)
705 if ((hwif->chipset != ide_4drives || !hwif->mate || !hwif->mate->present) &&
706 (ide_hwif_request_regions(hwif))) {
708 for (unit = 0; unit < MAX_DRIVES; ++unit) {
709 ide_drive_t *drive = &hwif->drives[unit];
710 if (drive->present) {
712 printk(KERN_ERR "%s: ERROR, PORTS ALREADY IN USE\n",
718 printk(KERN_ERR "%s: ports already in use, skipping probe\n",
724 * We must always disable IRQ, as probe_for_drive will assert IRQ, but
725 * we'll install our IRQ driver much later...
729 disable_irq(hwif->irq);
731 local_irq_set(flags);
733 /* This is needed on some PPCs and a bunch of BIOS-less embedded
734 * platforms. Typical cases are:
736 * - The firmware hard reset the disk before booting the kernel,
737 * the drive is still doing it's poweron-reset sequence, that
738 * can take up to 30 seconds
739 * - The firmware does nothing (or no firmware), the device is
740 * still in POST state (same as above actually).
741 * - Some CD/DVD/Writer combo drives tend to drive the bus during
742 * their reset sequence even when they are non-selected slave
743 * devices, thus preventing discovery of the main HD
745 * Doing this wait-for-busy should not harm any existing configuration
746 * (at least things won't be worse than what current code does, that
747 * is blindly go & talk to the drive) and fix some issues like the
752 if (wait_hwif_ready(hwif) == -EBUSY)
753 printk(KERN_DEBUG "%s: Wait for ready failed before probe !\n", hwif->name);
756 * Second drive should only exist if first drive was found,
757 * but a lot of cdrom drives are configured as single slaves.
759 for (unit = 0; unit < MAX_DRIVES; ++unit) {
760 ide_drive_t *drive = &hwif->drives[unit];
761 drive->dn = (hwif->channel ? 2 : 0) + unit;
762 (void) probe_for_drive(drive);
763 if (drive->present && !hwif->present) {
765 if (hwif->chipset != ide_4drives ||
767 !hwif->mate->present) {
772 if (hwif->io_ports[IDE_CONTROL_OFFSET] && hwif->reset) {
773 unsigned long timeout = jiffies + WAIT_WORSTCASE;
776 printk(KERN_WARNING "%s: reset\n", hwif->name);
777 hwif->OUTB(12, hwif->io_ports[IDE_CONTROL_OFFSET]);
779 hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
782 stat = hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
783 } while ((stat & BUSY_STAT) && time_after(timeout, jiffies));
786 local_irq_restore(flags);
788 * Use cached IRQ number. It might be (and is...) changed by probe
794 if (!hwif->present) {
795 ide_hwif_release_regions(hwif);
799 for (unit = 0; unit < MAX_DRIVES; ++unit) {
800 ide_drive_t *drive = &hwif->drives[unit];
802 if (drive->present) {
803 if (hwif->tuneproc != NULL &&
804 drive->autotune == IDE_TUNE_AUTO)
805 /* auto-tune PIO mode */
806 hwif->tuneproc(drive, 255);
808 * MAJOR HACK BARF :-/
810 * FIXME: chipsets own this cruft!
813 * Move here to prevent module loading clashing.
815 // drive->autodma = hwif->autodma;
816 if ((hwif->ide_dma_check) &&
817 ((drive->autotune == IDE_TUNE_DEFAULT) ||
818 (drive->autotune == IDE_TUNE_AUTO))) {
820 * Force DMAing for the beginning of the check.
821 * Some chipsets appear to do interesting
822 * things, if not checked and cleared.
825 hwif->ide_dma_off_quietly(drive);
826 #ifdef CONFIG_IDEDMA_ONLYDISK
827 if (drive->media == ide_disk)
829 hwif->ide_dma_check(drive);
835 static int hwif_init(ide_hwif_t *hwif);
837 int probe_hwif_init_with_fixup(ide_hwif_t *hwif, void (*fixup)(ide_hwif_t *hwif))
844 if (!hwif_init(hwif)) {
845 printk(KERN_INFO "%s: failed to initialize IDE interface\n",
852 for (unit = 0; unit < MAX_DRIVES; ++unit) {
853 ide_drive_t *drive = &hwif->drives[unit];
854 /* For now don't attach absent drives, we may
855 want them on default or a new "empty" class
856 for hotplug reprobing ? */
857 if (drive->present) {
865 int probe_hwif_init(ide_hwif_t *hwif)
867 return probe_hwif_init_with_fixup(hwif, NULL);
870 EXPORT_SYMBOL(probe_hwif_init);
874 * save_match() is used to simplify logic in init_irq() below.
876 * A loophole here is that we may not know about a particular
877 * hwif's irq until after that hwif is actually probed/initialized..
878 * This could be a problem for the case where an hwif is on a
879 * dual interface that requires serialization (eg. cmd640) and another
880 * hwif using one of the same irqs is initialized beforehand.
882 * This routine detects and reports such situations, but does not fix them.
884 static void save_match(ide_hwif_t *hwif, ide_hwif_t *new, ide_hwif_t **match)
886 ide_hwif_t *m = *match;
888 if (m && m->hwgroup && m->hwgroup != new->hwgroup) {
891 printk("%s: potential irq problem with %s and %s\n",
892 hwif->name, new->name, m->name);
894 if (!m || m->irq != hwif->irq) /* don't undo a prior perfect match */
897 #endif /* MAX_HWIFS > 1 */
902 static int ide_init_queue(ide_drive_t *drive)
905 ide_hwif_t *hwif = HWIF(drive);
906 int max_sectors = 256;
907 int max_sg_entries = PRD_ENTRIES;
910 * Our default set up assumes the normal IDE case,
911 * that is 64K segmenting, standard PRD setup
912 * and LBA28. Some drivers then impose their own
913 * limits and LBA48 we could raise it but as yet
917 q = blk_init_queue(do_ide_request, &ide_lock);
921 q->queuedata = drive;
922 blk_queue_segment_boundary(q, 0xffff);
925 if (hwif->no_lba48 || hwif->no_lba48_dma)
928 hwif->rqsize = 65536;
930 if (hwif->rqsize < max_sectors)
931 max_sectors = hwif->rqsize;
932 blk_queue_max_sectors(q, max_sectors);
935 /* When we have an IOMMU, we may have a problem where pci_map_sg()
936 * creates segments that don't completely match our boundary
937 * requirements and thus need to be broken up again. Because it
938 * doesn't align properly either, we may actually have to break up
939 * to more segments than what was we got in the first place, a max
940 * worst case is twice as many.
941 * This will be fixed once we teach pci_map_sg() about our boundary
942 * requirements, hopefully soon. *FIXME*
944 if (!PCI_DMA_BUS_IS_PHYS)
945 max_sg_entries >>= 1;
946 #endif /* CONFIG_PCI */
948 blk_queue_max_hw_segments(q, max_sg_entries);
949 blk_queue_max_phys_segments(q, max_sg_entries);
951 /* assign drive and gendisk queue */
954 drive->disk->queue = drive->queue;
956 /* needs drive->queue to be set */
957 ide_toggle_bounce(drive, 1);
959 /* enable led activity for disk drives only */
960 if (drive->media == ide_disk && hwif->led_act)
961 blk_queue_activity_fn(q, hwif->led_act, drive);
967 * This routine sets up the irq for an ide interface, and creates a new
968 * hwgroup for the irq/hwif if none was previously assigned.
970 * Much of the code is for correctly detecting/handling irq sharing
971 * and irq serialization situations. This is somewhat complex because
972 * it handles static as well as dynamic (PCMCIA) IDE interfaces.
974 * The SA_INTERRUPT in sa_flags means ide_intr() is always entered with
975 * interrupts completely disabled. This can be bad for interrupt latency,
976 * but anything else has led to problems on some machines. We re-enable
977 * interrupts as much as we can safely do in most places.
979 static int init_irq (ide_hwif_t *hwif)
982 ide_hwgroup_t *hwgroup;
983 ide_hwif_t *match = NULL;
986 BUG_ON(in_interrupt());
987 BUG_ON(irqs_disabled());
989 hwif->hwgroup = NULL;
992 * Group up with any other hwifs that share our irq(s).
994 for (index = 0; index < MAX_HWIFS; index++) {
995 ide_hwif_t *h = &ide_hwifs[index];
996 if (h->hwgroup) { /* scan only initialized hwif's */
997 if (hwif->irq == h->irq) {
998 hwif->sharing_irq = h->sharing_irq = 1;
999 if (hwif->chipset != ide_pci ||
1000 h->chipset != ide_pci) {
1001 save_match(hwif, h, &match);
1004 if (hwif->serialized) {
1005 if (hwif->mate && hwif->mate->irq == h->irq)
1006 save_match(hwif, h, &match);
1008 if (h->serialized) {
1009 if (h->mate && hwif->irq == h->mate->irq)
1010 save_match(hwif, h, &match);
1014 #endif /* MAX_HWIFS > 1 */
1016 * If we are still without a hwgroup, then form a new one
1019 hwgroup = match->hwgroup;
1020 hwif->hwgroup = hwgroup;
1022 * Link us into the hwgroup.
1023 * This must be done early, do ensure that unexpected_intr
1024 * can find the hwif and prevent irq storms.
1025 * No drives are attached to the new hwif, choose_drive
1026 * can't do anything stupid (yet).
1027 * Add ourself as the 2nd entry to the hwgroup->hwif
1028 * linked list, the first entry is the hwif that owns
1029 * hwgroup->handler - do not change that.
1031 spin_lock_irq(&ide_lock);
1032 hwif->next = hwgroup->hwif->next;
1033 hwgroup->hwif->next = hwif;
1034 spin_unlock_irq(&ide_lock);
1036 hwgroup = kmalloc(sizeof(ide_hwgroup_t),GFP_KERNEL);
1040 hwif->hwgroup = hwgroup;
1042 memset(hwgroup, 0, sizeof(ide_hwgroup_t));
1043 hwgroup->hwif = hwif->next = hwif;
1045 hwgroup->handler = NULL;
1046 hwgroup->drive = NULL;
1048 init_timer(&hwgroup->timer);
1049 hwgroup->timer.function = &ide_timer_expiry;
1050 hwgroup->timer.data = (unsigned long) hwgroup;
1054 * Allocate the irq, if not already obtained for another hwif
1056 if (!match || match->irq != hwif->irq) {
1057 int sa = SA_INTERRUPT;
1058 #if defined(__mc68000__) || defined(CONFIG_APUS)
1060 #endif /* __mc68000__ || CONFIG_APUS */
1062 if (IDE_CHIPSET_IS_PCI(hwif->chipset)) {
1064 #ifndef CONFIG_IDEPCI_SHARE_IRQ
1066 #endif /* CONFIG_IDEPCI_SHARE_IRQ */
1069 if (hwif->io_ports[IDE_CONTROL_OFFSET])
1071 hwif->OUTB(0x08, hwif->io_ports[IDE_CONTROL_OFFSET]);
1073 if (request_irq(hwif->irq,&ide_intr,sa,hwif->name,hwgroup))
1078 * For any present drive:
1079 * - allocate the block device queue
1080 * - link drive into the hwgroup
1082 for (index = 0; index < MAX_DRIVES; ++index) {
1083 ide_drive_t *drive = &hwif->drives[index];
1084 if (!drive->present)
1086 if (ide_init_queue(drive)) {
1087 printk(KERN_ERR "ide: failed to init %s\n",drive->name);
1090 spin_lock_irq(&ide_lock);
1091 if (!hwgroup->drive) {
1092 /* first drive for hwgroup. */
1093 drive->next = drive;
1094 hwgroup->drive = drive;
1095 hwgroup->hwif = HWIF(hwgroup->drive);
1097 drive->next = hwgroup->drive->next;
1098 hwgroup->drive->next = drive;
1100 spin_unlock_irq(&ide_lock);
1103 #if !defined(__mc68000__) && !defined(CONFIG_APUS) && !defined(__sparc__)
1104 printk("%s at 0x%03lx-0x%03lx,0x%03lx on irq %d", hwif->name,
1105 hwif->io_ports[IDE_DATA_OFFSET],
1106 hwif->io_ports[IDE_DATA_OFFSET]+7,
1107 hwif->io_ports[IDE_CONTROL_OFFSET], hwif->irq);
1108 #elif defined(__sparc__)
1109 printk("%s at 0x%03lx-0x%03lx,0x%03lx on irq %s", hwif->name,
1110 hwif->io_ports[IDE_DATA_OFFSET],
1111 hwif->io_ports[IDE_DATA_OFFSET]+7,
1112 hwif->io_ports[IDE_CONTROL_OFFSET], __irq_itoa(hwif->irq));
1114 printk("%s at 0x%08lx on irq %d", hwif->name,
1115 hwif->io_ports[IDE_DATA_OFFSET], hwif->irq);
1116 #endif /* __mc68000__ && CONFIG_APUS */
1118 printk(" (%sed with %s)",
1119 hwif->sharing_irq ? "shar" : "serializ", match->name);
1124 spin_lock_irq(&ide_lock);
1125 if (hwif->next == hwif) {
1127 BUG_ON(hwgroup->hwif != hwif);
1132 while (g->next != hwif)
1134 g->next = hwif->next;
1135 if (hwgroup->hwif == hwif) {
1137 printk(KERN_ERR "Duh. Uninitialized hwif listed as active hwif.\n");
1140 BUG_ON(hwgroup->hwif == hwif);
1142 spin_unlock_irq(&ide_lock);
1148 static int ata_lock(dev_t dev, void *data)
1150 /* FIXME: we want to pin hwif down */
1154 extern ide_driver_t idedefault_driver;
1156 static struct kobject *ata_probe(dev_t dev, int *part, void *data)
1158 ide_hwif_t *hwif = data;
1159 int unit = *part >> PARTN_BITS;
1160 ide_drive_t *drive = &hwif->drives[unit];
1161 if (!drive->present)
1163 if (drive->driver == &idedefault_driver) {
1164 if (drive->media == ide_disk)
1165 (void) request_module("ide-disk");
1167 (void) request_module("ide-scsi");
1168 if (drive->media == ide_cdrom || drive->media == ide_optical)
1169 (void) request_module("ide-cd");
1170 if (drive->media == ide_tape)
1171 (void) request_module("ide-tape");
1172 if (drive->media == ide_floppy)
1173 (void) request_module("ide-floppy");
1175 if (drive->driver == &idedefault_driver)
1177 *part &= (1 << PARTN_BITS) - 1;
1178 return get_disk(drive->disk);
1181 static int alloc_disks(ide_hwif_t *hwif)
1184 struct gendisk *disks[MAX_DRIVES];
1186 for (unit = 0; unit < MAX_DRIVES; unit++) {
1187 disks[unit] = alloc_disk(1 << PARTN_BITS);
1191 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1192 ide_drive_t *drive = &hwif->drives[unit];
1193 struct gendisk *disk = disks[unit];
1194 disk->major = hwif->major;
1195 disk->first_minor = unit << PARTN_BITS;
1196 sprintf(disk->disk_name,"hd%c",'a'+hwif->index*MAX_DRIVES+unit);
1197 disk->fops = ide_fops;
1198 disk->private_data = drive;
1203 printk(KERN_WARNING "(ide::init_gendisk) Out of memory\n");
1205 put_disk(disks[unit]);
1209 static void drive_release_dev (struct device *dev)
1211 ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
1213 up(&drive->gendev_rel_sem);
1217 * init_gendisk() (as opposed to ide_geninit) is called for each major device,
1218 * after probing for drives, to allocate partition tables and other data
1219 * structures needed for the routines in genhd.c. ide_geninit() gets called
1220 * somewhat later, during the partition check.
1222 static void init_gendisk (ide_hwif_t *hwif)
1226 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1227 ide_drive_t * drive = &hwif->drives[unit];
1228 ide_add_generic_settings(drive);
1229 snprintf(drive->gendev.bus_id,BUS_ID_SIZE,"%u.%u",
1231 drive->gendev.parent = &hwif->gendev;
1232 drive->gendev.bus = &ide_bus_type;
1233 drive->gendev.driver_data = drive;
1234 drive->gendev.release = drive_release_dev;
1235 if (drive->present) {
1236 device_register(&drive->gendev);
1237 sprintf(drive->devfs_name, "ide/host%d/bus%d/target%d/lun%d",
1238 (hwif->channel && hwif->mate) ?
1239 hwif->mate->index : hwif->index,
1240 hwif->channel, unit, drive->lun);
1243 blk_register_region(MKDEV(hwif->major, 0), MAX_DRIVES << PARTN_BITS,
1244 THIS_MODULE, ata_probe, ata_lock, hwif);
1247 static int hwif_init(ide_hwif_t *hwif)
1251 /* Return success if no device is connected */
1256 if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET])))
1258 printk("%s: DISABLED, NO IRQ\n", hwif->name);
1259 return (hwif->present = 0);
1262 #ifdef CONFIG_BLK_DEV_HD
1263 if (hwif->irq == HD_IRQ && hwif->io_ports[IDE_DATA_OFFSET] != HD_DATA) {
1264 printk("%s: CANNOT SHARE IRQ WITH OLD "
1265 "HARDDISK DRIVER (hd.c)\n", hwif->name);
1266 return (hwif->present = 0);
1268 #endif /* CONFIG_BLK_DEV_HD */
1270 /* we set it back to 1 if all is ok below */
1273 if (register_blkdev(hwif->major, hwif->name))
1276 if (!hwif->sg_max_nents)
1277 hwif->sg_max_nents = PRD_ENTRIES;
1279 hwif->sg_table = kmalloc(sizeof(struct scatterlist)*hwif->sg_max_nents,
1281 if (!hwif->sg_table) {
1282 printk(KERN_ERR "%s: unable to allocate SG table.\n", hwif->name);
1286 if (alloc_disks(hwif) < 0)
1289 if (init_irq(hwif) == 0)
1292 old_irq = hwif->irq;
1294 * It failed to initialise. Find the default IRQ for
1295 * this port and try that.
1297 if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET]))) {
1298 printk("%s: Disabled unable to get IRQ %d.\n",
1299 hwif->name, old_irq);
1302 if (init_irq(hwif)) {
1303 printk("%s: probed IRQ %d and default IRQ %d failed.\n",
1304 hwif->name, old_irq, hwif->irq);
1307 printk("%s: probed IRQ %d failed, using default.\n",
1308 hwif->name, hwif->irq);
1312 hwif->present = 1; /* success */
1316 for (unit = 0; unit < MAX_DRIVES; unit++) {
1317 struct gendisk *disk = hwif->drives[unit].disk;
1318 hwif->drives[unit].disk = NULL;
1322 unregister_blkdev(hwif->major, hwif->name);
1326 int ideprobe_init (void)
1329 int probe[MAX_HWIFS];
1331 memset(probe, 0, MAX_HWIFS * sizeof(int));
1332 for (index = 0; index < MAX_HWIFS; ++index)
1333 probe[index] = !ide_hwifs[index].present;
1335 for (index = 0; index < MAX_HWIFS; ++index)
1337 probe_hwif(&ide_hwifs[index]);
1338 for (index = 0; index < MAX_HWIFS; ++index)
1340 hwif_init(&ide_hwifs[index]);
1341 for (index = 0; index < MAX_HWIFS; ++index) {
1343 ide_hwif_t *hwif = &ide_hwifs[index];
1347 if (hwif->chipset == ide_unknown || hwif->chipset == ide_forced)
1348 hwif->chipset = ide_generic;
1349 for (unit = 0; unit < MAX_DRIVES; ++unit)
1350 if (hwif->drives[unit].present)
1351 ata_attach(&hwif->drives[unit]);
1357 EXPORT_SYMBOL_GPL(ideprobe_init);