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 #include <linux/module.h>
35 #include <linux/types.h>
36 #include <linux/string.h>
37 #include <linux/kernel.h>
38 #include <linux/timer.h>
40 #include <linux/interrupt.h>
41 #include <linux/major.h>
42 #include <linux/errno.h>
43 #include <linux/genhd.h>
44 #include <linux/slab.h>
45 #include <linux/delay.h>
46 #include <linux/ide.h>
47 #include <linux/spinlock.h>
48 #include <linux/kmod.h>
49 #include <linux/pci.h>
51 #include <asm/byteorder.h>
53 #include <asm/uaccess.h>
57 * generic_id - add a generic drive id
58 * @drive: drive to make an ID block for
60 * Add a fake id field to the drive we are passed. This allows
61 * use to skip a ton of NULL checks (which people always miss)
62 * and make drive properties unconditional outside of this file
65 static void generic_id(ide_drive_t *drive)
67 drive->id->cyls = drive->cyl;
68 drive->id->heads = drive->head;
69 drive->id->sectors = drive->sect;
70 drive->id->cur_cyls = drive->cyl;
71 drive->id->cur_heads = drive->head;
72 drive->id->cur_sectors = drive->sect;
75 static void ide_disk_init_chs(ide_drive_t *drive)
77 struct hd_driveid *id = drive->id;
79 /* Extract geometry if we did not already have one for the drive */
80 if (!drive->cyl || !drive->head || !drive->sect) {
81 drive->cyl = drive->bios_cyl = id->cyls;
82 drive->head = drive->bios_head = id->heads;
83 drive->sect = drive->bios_sect = id->sectors;
86 /* Handle logical geometry translation by the drive */
87 if ((id->field_valid & 1) && id->cur_cyls &&
88 id->cur_heads && (id->cur_heads <= 16) && id->cur_sectors) {
89 drive->cyl = id->cur_cyls;
90 drive->head = id->cur_heads;
91 drive->sect = id->cur_sectors;
94 /* Use physical geometry if what we have still makes no sense */
95 if (drive->head > 16 && id->heads && id->heads <= 16) {
96 drive->cyl = id->cyls;
97 drive->head = id->heads;
98 drive->sect = id->sectors;
102 static void ide_disk_init_mult_count(ide_drive_t *drive)
104 struct hd_driveid *id = drive->id;
106 drive->mult_count = 0;
107 if (id->max_multsect) {
108 #ifdef CONFIG_IDEDISK_MULTI_MODE
109 id->multsect = ((id->max_multsect/2) > 1) ? id->max_multsect : 0;
110 id->multsect_valid = id->multsect ? 1 : 0;
111 drive->mult_req = id->multsect_valid ? id->max_multsect : INITIAL_MULT_COUNT;
112 drive->special.b.set_multmode = drive->mult_req ? 1 : 0;
113 #else /* original, pre IDE-NFG, per request of AC */
114 drive->mult_req = INITIAL_MULT_COUNT;
115 if (drive->mult_req > id->max_multsect)
116 drive->mult_req = id->max_multsect;
117 if (drive->mult_req || ((id->multsect_valid & 1) && id->multsect))
118 drive->special.b.set_multmode = 1;
124 * do_identify - identify a drive
125 * @drive: drive to identify
128 * Called when we have issued a drive identify command to
129 * read and parse the results. This function is run with
130 * interrupts disabled.
133 static inline void do_identify (ide_drive_t *drive, u8 cmd)
135 ide_hwif_t *hwif = HWIF(drive);
137 struct hd_driveid *id;
140 /* read 512 bytes of id info */
141 hwif->ata_input_data(drive, id, SECTOR_WORDS);
147 #if defined (CONFIG_SCSI_EATA_PIO) || defined (CONFIG_SCSI_EATA)
149 * EATA SCSI controllers do a hardware ATA emulation:
150 * Ignore them if there is a driver for them available.
152 if ((id->model[0] == 'P' && id->model[1] == 'M') ||
153 (id->model[0] == 'S' && id->model[1] == 'K')) {
154 printk("%s: EATA SCSI HBA %.10s\n", drive->name, id->model);
157 #endif /* CONFIG_SCSI_EATA || CONFIG_SCSI_EATA_PIO */
160 * WIN_IDENTIFY returns little-endian info,
161 * WIN_PIDENTIFY *usually* returns little-endian info.
163 if (cmd == WIN_PIDENTIFY) {
164 if ((id->model[0] == 'N' && id->model[1] == 'E') /* NEC */
165 || (id->model[0] == 'F' && id->model[1] == 'X') /* Mitsumi */
166 || (id->model[0] == 'P' && id->model[1] == 'i'))/* Pioneer */
167 /* Vertos drives may still be weird */
170 ide_fixstring(id->model, sizeof(id->model), bswap);
171 ide_fixstring(id->fw_rev, sizeof(id->fw_rev), bswap);
172 ide_fixstring(id->serial_no, sizeof(id->serial_no), bswap);
174 if (strstr(id->model, "E X A B Y T E N E S T"))
177 /* we depend on this a lot! */
178 id->model[sizeof(id->model)-1] = '\0';
179 printk("%s: %s, ", drive->name, id->model);
184 * Check for an ATAPI device
186 if (cmd == WIN_PIDENTIFY) {
187 u8 type = (id->config >> 8) & 0x1f;
191 if (!strstr(id->model, "CD-ROM")) {
192 if (!strstr(id->model, "oppy") &&
193 !strstr(id->model, "poyp") &&
194 !strstr(id->model, "ZIP"))
195 printk("cdrom or floppy?, assuming ");
196 if (drive->media != ide_cdrom) {
198 drive->removable = 1;
202 /* Early cdrom models used zero */
205 drive->removable = 1;
207 /* kludge for Apple PowerBook internal zip */
208 if (!strstr(id->model, "CD-ROM") &&
209 strstr(id->model, "ZIP")) {
215 printk ("CD/DVD-ROM");
222 drive->removable = 1;
225 printk("UNKNOWN (type %d)", type);
230 /* an ATAPI device ignores DRDY */
231 drive->ready_stat = 0;
236 * Not an ATAPI device: looks like a "regular" hard disk
240 * 0x848a = CompactFlash device
241 * These are *not* removable in Linux definition of the term
244 if ((id->config != 0x848a) && (id->config & (1<<7)))
245 drive->removable = 1;
247 drive->media = ide_disk;
248 printk("%s DISK drive\n", (id->config == 0x848a) ? "CFA" : "ATA" );
259 * actual_try_to_identify - send ata/atapi identify
260 * @drive: drive to identify
261 * @cmd: command to use
263 * try_to_identify() sends an ATA(PI) IDENTIFY request to a drive
264 * and waits for a response. It also monitors irqs while this is
265 * happening, in hope of automatically determining which one is
266 * being used by the interface.
268 * Returns: 0 device was identified
269 * 1 device timed-out (no response to identify request)
270 * 2 device aborted the command (refused to identify itself)
273 static int actual_try_to_identify (ide_drive_t *drive, u8 cmd)
275 ide_hwif_t *hwif = HWIF(drive);
277 unsigned long hd_status;
278 unsigned long timeout;
281 /* take a deep breath */
284 if (IDE_CONTROL_REG) {
285 a = hwif->INB(IDE_ALTSTATUS_REG);
286 s = hwif->INB(IDE_STATUS_REG);
287 if ((a ^ s) & ~INDEX_STAT) {
288 printk(KERN_INFO "%s: probing with STATUS(0x%02x) instead of "
289 "ALTSTATUS(0x%02x)\n", drive->name, s, a);
290 /* ancient Seagate drives, broken interfaces */
291 hd_status = IDE_STATUS_REG;
293 /* use non-intrusive polling */
294 hd_status = IDE_ALTSTATUS_REG;
297 hd_status = IDE_STATUS_REG;
299 /* set features register for atapi
300 * identify command to be sure of reply
302 if ((cmd == WIN_PIDENTIFY))
303 /* disable dma & overlap */
304 hwif->OUTB(0, IDE_FEATURE_REG);
306 /* ask drive for ID */
307 hwif->OUTB(cmd, IDE_COMMAND_REG);
309 timeout = ((cmd == WIN_IDENTIFY) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
312 if (time_after(jiffies, timeout)) {
313 /* drive timed-out */
316 /* give drive a breather */
318 } while ((hwif->INB(hd_status)) & BUSY_STAT);
320 /* wait for IRQ and DRQ_STAT */
322 if (OK_STAT((hwif->INB(IDE_STATUS_REG)), DRQ_STAT, BAD_R_STAT)) {
325 /* local CPU only; some systems need this */
326 local_irq_save(flags);
327 /* drive returned ID */
328 do_identify(drive, cmd);
329 /* drive responded with ID */
331 /* clear drive IRQ */
332 (void) hwif->INB(IDE_STATUS_REG);
333 local_irq_restore(flags);
335 /* drive refused ID */
342 * try_to_identify - try to identify a drive
343 * @drive: drive to probe
344 * @cmd: command to use
346 * Issue the identify command and then do IRQ probing to
347 * complete the identification when needed by finding the
348 * IRQ the drive is attached to
351 static int try_to_identify (ide_drive_t *drive, u8 cmd)
353 ide_hwif_t *hwif = HWIF(drive);
356 unsigned long cookie = 0;
359 * Disable device irq unless we need to
360 * probe for it. Otherwise we'll get spurious
361 * interrupts during the identify-phase that
362 * the irq handler isn't expecting.
364 if (IDE_CONTROL_REG) {
365 u8 ctl = drive->ctl | 2;
368 cookie = probe_irq_on();
369 /* enable device irq */
372 hwif->OUTB(ctl, IDE_CONTROL_REG);
375 retval = actual_try_to_identify(drive, cmd);
379 /* mask device irq */
380 hwif->OUTB(drive->ctl|2, IDE_CONTROL_REG);
381 /* clear drive IRQ */
382 (void) hwif->INB(IDE_STATUS_REG);
384 irq = probe_irq_off(cookie);
389 /* Mmmm.. multiple IRQs..
390 * don't know which was ours
392 printk("%s: IRQ probe failed (0x%lx)\n",
393 drive->name, cookie);
402 * do_probe - probe an IDE device
403 * @drive: drive to probe
404 * @cmd: command to use
406 * do_probe() has the difficult job of finding a drive if it exists,
407 * without getting hung up if it doesn't exist, without trampling on
408 * ethernet cards, and without leaving any IRQs dangling to haunt us later.
410 * If a drive is "known" to exist (from CMOS or kernel parameters),
411 * but does not respond right away, the probe will "hang in there"
412 * for the maximum wait time (about 30 seconds), otherwise it will
413 * exit much more quickly.
415 * Returns: 0 device was identified
416 * 1 device timed-out (no response to identify request)
417 * 2 device aborted the command (refused to identify itself)
418 * 3 bad status from device (possible for ATAPI drives)
419 * 4 probe was not attempted because failure was obvious
422 static int do_probe (ide_drive_t *drive, u8 cmd)
425 ide_hwif_t *hwif = HWIF(drive);
427 if (drive->present) {
428 /* avoid waiting for inappropriate probes */
429 if ((drive->media != ide_disk) && (cmd == WIN_IDENTIFY))
433 printk("probing for %s: present=%d, media=%d, probetype=%s\n",
434 drive->name, drive->present, drive->media,
435 (cmd == WIN_IDENTIFY) ? "ATA" : "ATAPI");
438 /* needed for some systems
439 * (e.g. crw9624 as drive0 with disk as slave)
444 if (hwif->INB(IDE_SELECT_REG) != drive->select.all && !drive->present) {
445 if (drive->select.b.unit != 0) {
446 /* exit with drive0 selected */
447 SELECT_DRIVE(&hwif->drives[0]);
448 /* allow BUSY_STAT to assert & clear */
451 /* no i/f present: mmm.. this should be a 4 -ml */
455 if (OK_STAT((hwif->INB(IDE_STATUS_REG)), READY_STAT, BUSY_STAT) ||
456 drive->present || cmd == WIN_PIDENTIFY) {
457 /* send cmd and wait */
458 if ((rc = try_to_identify(drive, cmd))) {
459 /* failed: try again */
460 rc = try_to_identify(drive,cmd);
462 if (hwif->INB(IDE_STATUS_REG) == (BUSY_STAT|READY_STAT))
465 if ((rc == 1 && cmd == WIN_PIDENTIFY) &&
466 ((drive->autotune == IDE_TUNE_DEFAULT) ||
467 (drive->autotune == IDE_TUNE_AUTO))) {
468 unsigned long timeout;
469 printk("%s: no response (status = 0x%02x), "
470 "resetting drive\n", drive->name,
471 hwif->INB(IDE_STATUS_REG));
473 hwif->OUTB(drive->select.all, IDE_SELECT_REG);
475 hwif->OUTB(WIN_SRST, IDE_COMMAND_REG);
477 while (((hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) &&
478 time_before(jiffies, timeout + WAIT_WORSTCASE))
480 rc = try_to_identify(drive, cmd);
483 printk("%s: no response (status = 0x%02x)\n",
484 drive->name, hwif->INB(IDE_STATUS_REG));
485 /* ensure drive irq is clear */
486 (void) hwif->INB(IDE_STATUS_REG);
488 /* not present or maybe ATAPI */
491 if (drive->select.b.unit != 0) {
492 /* exit with drive0 selected */
493 SELECT_DRIVE(&hwif->drives[0]);
495 /* ensure drive irq is clear */
496 (void) hwif->INB(IDE_STATUS_REG);
504 static void enable_nest (ide_drive_t *drive)
506 ide_hwif_t *hwif = HWIF(drive);
507 unsigned long timeout;
509 printk("%s: enabling %s -- ", hwif->name, drive->id->model);
512 hwif->OUTB(EXABYTE_ENABLE_NEST, IDE_COMMAND_REG);
513 timeout = jiffies + WAIT_WORSTCASE;
515 if (time_after(jiffies, timeout)) {
516 printk("failed (timeout)\n");
520 } while ((hwif->INB(IDE_STATUS_REG)) & BUSY_STAT);
524 if (!OK_STAT((hwif->INB(IDE_STATUS_REG)), 0, BAD_STAT)) {
525 printk("failed (status = 0x%02x)\n", hwif->INB(IDE_STATUS_REG));
530 /* if !(success||timed-out) */
531 if (do_probe(drive, WIN_IDENTIFY) >= 2) {
532 /* look for ATAPI device */
533 (void) do_probe(drive, WIN_PIDENTIFY);
538 * probe_for_drives - upper level drive probe
539 * @drive: drive to probe for
541 * probe_for_drive() tests for existence of a given drive using do_probe()
542 * and presents things to the user as needed.
544 * Returns: 0 no device was found
545 * 1 device was found (note: drive->present might
549 static inline u8 probe_for_drive (ide_drive_t *drive)
552 * In order to keep things simple we have an id
553 * block for all drives at all times. If the device
554 * is pre ATA or refuses ATA/ATAPI identify we
555 * will add faked data to this.
557 * Also note that 0 everywhere means "can't do X"
560 drive->id = kzalloc(SECTOR_WORDS *4, GFP_KERNEL);
562 if(drive->id == NULL)
564 printk(KERN_ERR "ide: out of memory for id data.\n");
567 strcpy(drive->id->model, "UNKNOWN");
572 /* if !(success||timed-out) */
573 if (do_probe(drive, WIN_IDENTIFY) >= 2) {
574 /* look for ATAPI device */
575 (void) do_probe(drive, WIN_PIDENTIFY);
578 /* drive not found */
580 if (strstr(drive->id->model, "E X A B Y T E N E S T"))
583 /* identification failed? */
584 if (!drive->id_read) {
585 if (drive->media == ide_disk) {
586 printk(KERN_INFO "%s: non-IDE drive, CHS=%d/%d/%d\n",
587 drive->name, drive->cyl,
588 drive->head, drive->sect);
589 } else if (drive->media == ide_cdrom) {
590 printk(KERN_INFO "%s: ATAPI cdrom (?)\n", drive->name);
593 printk(KERN_WARNING "%s: Unknown device on bus refused identification. Ignoring.\n", drive->name);
597 /* drive was found */
601 /* The drive wasn't being helpful. Add generic info only */
602 if (drive->id_read == 0) {
607 if (drive->media == ide_disk) {
608 ide_disk_init_chs(drive);
609 ide_disk_init_mult_count(drive);
612 return drive->present;
615 static void hwif_release_dev (struct device *dev)
617 ide_hwif_t *hwif = container_of(dev, ide_hwif_t, gendev);
619 complete(&hwif->gendev_rel_comp);
622 static void hwif_register (ide_hwif_t *hwif)
626 /* register with global device tree */
627 strlcpy(hwif->gendev.bus_id,hwif->name,BUS_ID_SIZE);
628 hwif->gendev.driver_data = hwif;
629 if (hwif->gendev.parent == NULL) {
631 hwif->gendev.parent = &hwif->pci_dev->dev;
633 /* Would like to do = &device_legacy */
634 hwif->gendev.parent = NULL;
636 hwif->gendev.release = hwif_release_dev;
637 ret = device_register(&hwif->gendev);
639 printk(KERN_WARNING "IDE: %s: device_register error: %d\n",
643 static int wait_hwif_ready(ide_hwif_t *hwif)
647 printk(KERN_DEBUG "Probing IDE interface %s...\n", hwif->name);
649 /* Let HW settle down a bit from whatever init state we
653 /* Wait for BSY bit to go away, spec timeout is 30 seconds,
654 * I know of at least one disk who takes 31 seconds, I use 35
657 rc = ide_wait_not_busy(hwif, 35000);
661 /* Now make sure both master & slave are ready */
662 SELECT_DRIVE(&hwif->drives[0]);
663 hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
665 rc = ide_wait_not_busy(hwif, 35000);
668 SELECT_DRIVE(&hwif->drives[1]);
669 hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
671 rc = ide_wait_not_busy(hwif, 35000);
673 /* Exit function with master reselected (let's be sane) */
674 SELECT_DRIVE(&hwif->drives[0]);
680 * ide_undecoded_slave - look for bad CF adapters
683 * Analyse the drives on the interface and attempt to decide if we
684 * have the same drive viewed twice. This occurs with crap CF adapters
685 * and PCMCIA sometimes.
688 void ide_undecoded_slave(ide_hwif_t *hwif)
690 ide_drive_t *drive0 = &hwif->drives[0];
691 ide_drive_t *drive1 = &hwif->drives[1];
693 if (drive0->present == 0 || drive1->present == 0)
696 /* If the models don't match they are not the same product */
697 if (strcmp(drive0->id->model, drive1->id->model))
700 /* Serial numbers do not match */
701 if (strncmp(drive0->id->serial_no, drive1->id->serial_no, 20))
704 /* No serial number, thankfully very rare for CF */
705 if (drive0->id->serial_no[0] == 0)
708 /* Appears to be an IDE flash adapter with decode bugs */
709 printk(KERN_WARNING "ide-probe: ignoring undecoded slave\n");
714 EXPORT_SYMBOL_GPL(ide_undecoded_slave);
717 * This routine only knows how to look for drive units 0 and 1
718 * on an interface, so any setting of MAX_DRIVES > 2 won't work here.
720 static void probe_hwif(ide_hwif_t *hwif)
729 if ((hwif->chipset != ide_4drives || !hwif->mate || !hwif->mate->present) &&
730 (ide_hwif_request_regions(hwif))) {
732 for (unit = 0; unit < MAX_DRIVES; ++unit) {
733 ide_drive_t *drive = &hwif->drives[unit];
734 if (drive->present) {
736 printk(KERN_ERR "%s: ERROR, PORTS ALREADY IN USE\n",
742 printk(KERN_ERR "%s: ports already in use, skipping probe\n",
748 * We must always disable IRQ, as probe_for_drive will assert IRQ, but
749 * we'll install our IRQ driver much later...
753 disable_irq(hwif->irq);
755 local_irq_set(flags);
757 /* This is needed on some PPCs and a bunch of BIOS-less embedded
758 * platforms. Typical cases are:
760 * - The firmware hard reset the disk before booting the kernel,
761 * the drive is still doing it's poweron-reset sequence, that
762 * can take up to 30 seconds
763 * - The firmware does nothing (or no firmware), the device is
764 * still in POST state (same as above actually).
765 * - Some CD/DVD/Writer combo drives tend to drive the bus during
766 * their reset sequence even when they are non-selected slave
767 * devices, thus preventing discovery of the main HD
769 * Doing this wait-for-busy should not harm any existing configuration
770 * (at least things won't be worse than what current code does, that
771 * is blindly go & talk to the drive) and fix some issues like the
776 if (wait_hwif_ready(hwif) == -EBUSY)
777 printk(KERN_DEBUG "%s: Wait for ready failed before probe !\n", hwif->name);
780 * Need to probe slave device first to make it release PDIAG-.
782 for (unit = MAX_DRIVES - 1; unit >= 0; unit--) {
783 ide_drive_t *drive = &hwif->drives[unit];
784 drive->dn = (hwif->channel ? 2 : 0) + unit;
785 (void) probe_for_drive(drive);
786 if (drive->present && !hwif->present) {
788 if (hwif->chipset != ide_4drives ||
790 !hwif->mate->present) {
795 if (hwif->io_ports[IDE_CONTROL_OFFSET] && hwif->reset) {
796 unsigned long timeout = jiffies + WAIT_WORSTCASE;
799 printk(KERN_WARNING "%s: reset\n", hwif->name);
800 hwif->OUTB(12, hwif->io_ports[IDE_CONTROL_OFFSET]);
802 hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
805 stat = hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
806 } while ((stat & BUSY_STAT) && time_after(timeout, jiffies));
809 local_irq_restore(flags);
811 * Use cached IRQ number. It might be (and is...) changed by probe
817 if (!hwif->present) {
818 ide_hwif_release_regions(hwif);
825 for (unit = 0; unit < MAX_DRIVES; ++unit) {
826 ide_drive_t *drive = &hwif->drives[unit];
828 if (drive->present) {
829 if (drive->autotune == IDE_TUNE_AUTO)
830 ide_set_max_pio(drive);
832 if (drive->autotune != IDE_TUNE_DEFAULT &&
833 drive->autotune != IDE_TUNE_AUTO)
838 if (hwif->ide_dma_on) {
840 * Force DMAing for the beginning of the check.
841 * Some chipsets appear to do interesting
842 * things, if not checked and cleared.
845 hwif->dma_off_quietly(drive);
851 for (unit = 0; unit < MAX_DRIVES; ++unit) {
852 ide_drive_t *drive = &hwif->drives[unit];
854 if (hwif->no_io_32bit)
855 drive->no_io_32bit = 1;
857 drive->no_io_32bit = drive->id->dword_io ? 1 : 0;
861 static int hwif_init(ide_hwif_t *hwif);
862 static void hwif_register_devices(ide_hwif_t *hwif);
864 static int probe_hwif_init(ide_hwif_t *hwif)
868 if (!hwif_init(hwif)) {
869 printk(KERN_INFO "%s: failed to initialize IDE interface\n",
875 hwif_register_devices(hwif);
882 * save_match() is used to simplify logic in init_irq() below.
884 * A loophole here is that we may not know about a particular
885 * hwif's irq until after that hwif is actually probed/initialized..
886 * This could be a problem for the case where an hwif is on a
887 * dual interface that requires serialization (eg. cmd640) and another
888 * hwif using one of the same irqs is initialized beforehand.
890 * This routine detects and reports such situations, but does not fix them.
892 static void save_match(ide_hwif_t *hwif, ide_hwif_t *new, ide_hwif_t **match)
894 ide_hwif_t *m = *match;
896 if (m && m->hwgroup && m->hwgroup != new->hwgroup) {
899 printk("%s: potential irq problem with %s and %s\n",
900 hwif->name, new->name, m->name);
902 if (!m || m->irq != hwif->irq) /* don't undo a prior perfect match */
905 #endif /* MAX_HWIFS > 1 */
910 static int ide_init_queue(ide_drive_t *drive)
912 struct request_queue *q;
913 ide_hwif_t *hwif = HWIF(drive);
914 int max_sectors = 256;
915 int max_sg_entries = PRD_ENTRIES;
918 * Our default set up assumes the normal IDE case,
919 * that is 64K segmenting, standard PRD setup
920 * and LBA28. Some drivers then impose their own
921 * limits and LBA48 we could raise it but as yet
925 q = blk_init_queue_node(do_ide_request, &ide_lock, hwif_to_node(hwif));
929 q->queuedata = drive;
930 blk_queue_segment_boundary(q, 0xffff);
933 if ((hwif->host_flags & IDE_HFLAG_NO_LBA48) ||
934 (hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA))
937 hwif->rqsize = 65536;
939 if (hwif->rqsize < max_sectors)
940 max_sectors = hwif->rqsize;
941 blk_queue_max_sectors(q, max_sectors);
944 /* When we have an IOMMU, we may have a problem where pci_map_sg()
945 * creates segments that don't completely match our boundary
946 * requirements and thus need to be broken up again. Because it
947 * doesn't align properly either, we may actually have to break up
948 * to more segments than what was we got in the first place, a max
949 * worst case is twice as many.
950 * This will be fixed once we teach pci_map_sg() about our boundary
951 * requirements, hopefully soon. *FIXME*
953 if (!PCI_DMA_BUS_IS_PHYS)
954 max_sg_entries >>= 1;
955 #endif /* CONFIG_PCI */
957 blk_queue_max_hw_segments(q, max_sg_entries);
958 blk_queue_max_phys_segments(q, max_sg_entries);
960 /* assign drive queue */
963 /* needs drive->queue to be set */
964 ide_toggle_bounce(drive, 1);
970 * This routine sets up the irq for an ide interface, and creates a new
971 * hwgroup for the irq/hwif if none was previously assigned.
973 * Much of the code is for correctly detecting/handling irq sharing
974 * and irq serialization situations. This is somewhat complex because
975 * it handles static as well as dynamic (PCMCIA) IDE interfaces.
977 * The IRQF_DISABLED in sa_flags means ide_intr() is always entered with
978 * interrupts completely disabled. This can be bad for interrupt latency,
979 * but anything else has led to problems on some machines. We re-enable
980 * interrupts as much as we can safely do in most places.
982 static int init_irq (ide_hwif_t *hwif)
985 ide_hwgroup_t *hwgroup;
986 ide_hwif_t *match = NULL;
989 BUG_ON(in_interrupt());
990 BUG_ON(irqs_disabled());
991 BUG_ON(hwif == NULL);
993 mutex_lock(&ide_cfg_mtx);
994 hwif->hwgroup = NULL;
997 * Group up with any other hwifs that share our irq(s).
999 for (index = 0; index < MAX_HWIFS; index++) {
1000 ide_hwif_t *h = &ide_hwifs[index];
1001 if (h->hwgroup) { /* scan only initialized hwif's */
1002 if (hwif->irq == h->irq) {
1003 hwif->sharing_irq = h->sharing_irq = 1;
1004 if (hwif->chipset != ide_pci ||
1005 h->chipset != ide_pci) {
1006 save_match(hwif, h, &match);
1009 if (hwif->serialized) {
1010 if (hwif->mate && hwif->mate->irq == h->irq)
1011 save_match(hwif, h, &match);
1013 if (h->serialized) {
1014 if (h->mate && hwif->irq == h->mate->irq)
1015 save_match(hwif, h, &match);
1019 #endif /* MAX_HWIFS > 1 */
1021 * If we are still without a hwgroup, then form a new one
1024 hwgroup = match->hwgroup;
1025 hwif->hwgroup = hwgroup;
1027 * Link us into the hwgroup.
1028 * This must be done early, do ensure that unexpected_intr
1029 * can find the hwif and prevent irq storms.
1030 * No drives are attached to the new hwif, choose_drive
1031 * can't do anything stupid (yet).
1032 * Add ourself as the 2nd entry to the hwgroup->hwif
1033 * linked list, the first entry is the hwif that owns
1034 * hwgroup->handler - do not change that.
1036 spin_lock_irq(&ide_lock);
1037 hwif->next = hwgroup->hwif->next;
1038 hwgroup->hwif->next = hwif;
1039 spin_unlock_irq(&ide_lock);
1041 hwgroup = kmalloc_node(sizeof(ide_hwgroup_t),
1042 GFP_KERNEL | __GFP_ZERO,
1043 hwif_to_node(hwif->drives[0].hwif));
1047 hwif->hwgroup = hwgroup;
1049 hwgroup->hwif = hwif->next = hwif;
1051 hwgroup->handler = NULL;
1052 hwgroup->drive = NULL;
1054 init_timer(&hwgroup->timer);
1055 hwgroup->timer.function = &ide_timer_expiry;
1056 hwgroup->timer.data = (unsigned long) hwgroup;
1060 * Allocate the irq, if not already obtained for another hwif
1062 if (!match || match->irq != hwif->irq) {
1063 int sa = IRQF_DISABLED;
1064 #if defined(__mc68000__) || defined(CONFIG_APUS)
1066 #endif /* __mc68000__ || CONFIG_APUS */
1068 if (IDE_CHIPSET_IS_PCI(hwif->chipset)) {
1070 #ifndef CONFIG_IDEPCI_SHARE_IRQ
1071 sa |= IRQF_DISABLED;
1072 #endif /* CONFIG_IDEPCI_SHARE_IRQ */
1075 if (hwif->io_ports[IDE_CONTROL_OFFSET])
1077 hwif->OUTB(0x08, hwif->io_ports[IDE_CONTROL_OFFSET]);
1079 if (request_irq(hwif->irq,&ide_intr,sa,hwif->name,hwgroup))
1084 * For any present drive:
1085 * - allocate the block device queue
1086 * - link drive into the hwgroup
1088 for (index = 0; index < MAX_DRIVES; ++index) {
1089 ide_drive_t *drive = &hwif->drives[index];
1090 if (!drive->present)
1092 if (ide_init_queue(drive)) {
1093 printk(KERN_ERR "ide: failed to init %s\n",drive->name);
1096 spin_lock_irq(&ide_lock);
1097 if (!hwgroup->drive) {
1098 /* first drive for hwgroup. */
1099 drive->next = drive;
1100 hwgroup->drive = drive;
1101 hwgroup->hwif = HWIF(hwgroup->drive);
1103 drive->next = hwgroup->drive->next;
1104 hwgroup->drive->next = drive;
1106 spin_unlock_irq(&ide_lock);
1109 #if !defined(__mc68000__) && !defined(CONFIG_APUS)
1110 printk("%s at 0x%03lx-0x%03lx,0x%03lx on irq %d", hwif->name,
1111 hwif->io_ports[IDE_DATA_OFFSET],
1112 hwif->io_ports[IDE_DATA_OFFSET]+7,
1113 hwif->io_ports[IDE_CONTROL_OFFSET], hwif->irq);
1115 printk("%s at 0x%08lx on irq %d", hwif->name,
1116 hwif->io_ports[IDE_DATA_OFFSET], hwif->irq);
1117 #endif /* __mc68000__ && CONFIG_APUS */
1119 printk(" (%sed with %s)",
1120 hwif->sharing_irq ? "shar" : "serializ", match->name);
1122 mutex_unlock(&ide_cfg_mtx);
1125 spin_lock_irq(&ide_lock);
1126 if (hwif->next == hwif) {
1128 BUG_ON(hwgroup->hwif != hwif);
1133 while (g->next != hwif)
1135 g->next = hwif->next;
1136 if (hwgroup->hwif == hwif) {
1138 printk(KERN_ERR "Duh. Uninitialized hwif listed as active hwif.\n");
1141 BUG_ON(hwgroup->hwif == hwif);
1143 spin_unlock_irq(&ide_lock);
1145 mutex_unlock(&ide_cfg_mtx);
1149 static int ata_lock(dev_t dev, void *data)
1151 /* FIXME: we want to pin hwif down */
1155 static struct kobject *ata_probe(dev_t dev, int *part, void *data)
1157 ide_hwif_t *hwif = data;
1158 int unit = *part >> PARTN_BITS;
1159 ide_drive_t *drive = &hwif->drives[unit];
1160 if (!drive->present)
1163 if (drive->media == ide_disk)
1164 request_module("ide-disk");
1166 request_module("ide-scsi");
1167 if (drive->media == ide_cdrom || drive->media == ide_optical)
1168 request_module("ide-cd");
1169 if (drive->media == ide_tape)
1170 request_module("ide-tape");
1171 if (drive->media == ide_floppy)
1172 request_module("ide-floppy");
1177 static struct kobject *exact_match(dev_t dev, int *part, void *data)
1179 struct gendisk *p = data;
1180 *part &= (1 << PARTN_BITS) - 1;
1184 static int exact_lock(dev_t dev, void *data)
1186 struct gendisk *p = data;
1193 void ide_register_region(struct gendisk *disk)
1195 blk_register_region(MKDEV(disk->major, disk->first_minor),
1196 disk->minors, NULL, exact_match, exact_lock, disk);
1199 EXPORT_SYMBOL_GPL(ide_register_region);
1201 void ide_unregister_region(struct gendisk *disk)
1203 blk_unregister_region(MKDEV(disk->major, disk->first_minor),
1207 EXPORT_SYMBOL_GPL(ide_unregister_region);
1209 void ide_init_disk(struct gendisk *disk, ide_drive_t *drive)
1211 ide_hwif_t *hwif = drive->hwif;
1212 unsigned int unit = (drive->select.all >> 4) & 1;
1214 disk->major = hwif->major;
1215 disk->first_minor = unit << PARTN_BITS;
1216 sprintf(disk->disk_name, "hd%c", 'a' + hwif->index * MAX_DRIVES + unit);
1217 disk->queue = drive->queue;
1220 EXPORT_SYMBOL_GPL(ide_init_disk);
1222 static void ide_remove_drive_from_hwgroup(ide_drive_t *drive)
1224 ide_hwgroup_t *hwgroup = drive->hwif->hwgroup;
1226 if (drive == drive->next) {
1227 /* special case: last drive from hwgroup. */
1228 BUG_ON(hwgroup->drive != drive);
1229 hwgroup->drive = NULL;
1233 walk = hwgroup->drive;
1234 while (walk->next != drive)
1236 walk->next = drive->next;
1237 if (hwgroup->drive == drive) {
1238 hwgroup->drive = drive->next;
1239 hwgroup->hwif = hwgroup->drive->hwif;
1242 BUG_ON(hwgroup->drive == drive);
1245 static void drive_release_dev (struct device *dev)
1247 ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
1249 spin_lock_irq(&ide_lock);
1250 ide_remove_drive_from_hwgroup(drive);
1254 /* Messed up locking ... */
1255 spin_unlock_irq(&ide_lock);
1256 blk_cleanup_queue(drive->queue);
1257 spin_lock_irq(&ide_lock);
1258 drive->queue = NULL;
1259 spin_unlock_irq(&ide_lock);
1261 complete(&drive->gendev_rel_comp);
1265 * init_gendisk() (as opposed to ide_geninit) is called for each major device,
1266 * after probing for drives, to allocate partition tables and other data
1267 * structures needed for the routines in genhd.c. ide_geninit() gets called
1268 * somewhat later, during the partition check.
1270 static void init_gendisk (ide_hwif_t *hwif)
1274 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1275 ide_drive_t * drive = &hwif->drives[unit];
1276 ide_add_generic_settings(drive);
1277 snprintf(drive->gendev.bus_id,BUS_ID_SIZE,"%u.%u",
1279 drive->gendev.parent = &hwif->gendev;
1280 drive->gendev.bus = &ide_bus_type;
1281 drive->gendev.driver_data = drive;
1282 drive->gendev.release = drive_release_dev;
1284 blk_register_region(MKDEV(hwif->major, 0), MAX_DRIVES << PARTN_BITS,
1285 THIS_MODULE, ata_probe, ata_lock, hwif);
1288 static int hwif_init(ide_hwif_t *hwif)
1292 /* Return success if no device is connected */
1297 if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET])))
1299 printk("%s: DISABLED, NO IRQ\n", hwif->name);
1300 return (hwif->present = 0);
1303 #ifdef CONFIG_BLK_DEV_HD
1304 if (hwif->irq == HD_IRQ && hwif->io_ports[IDE_DATA_OFFSET] != HD_DATA) {
1305 printk("%s: CANNOT SHARE IRQ WITH OLD "
1306 "HARDDISK DRIVER (hd.c)\n", hwif->name);
1307 return (hwif->present = 0);
1309 #endif /* CONFIG_BLK_DEV_HD */
1311 /* we set it back to 1 if all is ok below */
1314 if (register_blkdev(hwif->major, hwif->name))
1317 if (!hwif->sg_max_nents)
1318 hwif->sg_max_nents = PRD_ENTRIES;
1320 hwif->sg_table = kmalloc(sizeof(struct scatterlist)*hwif->sg_max_nents,
1322 if (!hwif->sg_table) {
1323 printk(KERN_ERR "%s: unable to allocate SG table.\n", hwif->name);
1327 sg_init_table(hwif->sg_table, hwif->sg_max_nents);
1329 if (init_irq(hwif) == 0)
1332 old_irq = hwif->irq;
1334 * It failed to initialise. Find the default IRQ for
1335 * this port and try that.
1337 if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET]))) {
1338 printk("%s: Disabled unable to get IRQ %d.\n",
1339 hwif->name, old_irq);
1342 if (init_irq(hwif)) {
1343 printk("%s: probed IRQ %d and default IRQ %d failed.\n",
1344 hwif->name, old_irq, hwif->irq);
1347 printk("%s: probed IRQ %d failed, using default.\n",
1348 hwif->name, hwif->irq);
1353 ide_acpi_init(hwif);
1355 hwif->present = 1; /* success */
1359 unregister_blkdev(hwif->major, hwif->name);
1363 static void hwif_register_devices(ide_hwif_t *hwif)
1367 for (i = 0; i < MAX_DRIVES; i++) {
1368 ide_drive_t *drive = &hwif->drives[i];
1370 if (drive->present) {
1371 int ret = device_register(&drive->gendev);
1374 printk(KERN_WARNING "IDE: %s: "
1375 "device_register error: %d\n",
1381 int ideprobe_init (void)
1384 int probe[MAX_HWIFS];
1386 memset(probe, 0, MAX_HWIFS * sizeof(int));
1387 for (index = 0; index < MAX_HWIFS; ++index)
1388 probe[index] = !ide_hwifs[index].present;
1390 for (index = 0; index < MAX_HWIFS; ++index)
1392 probe_hwif(&ide_hwifs[index]);
1393 for (index = 0; index < MAX_HWIFS; ++index)
1395 hwif_init(&ide_hwifs[index]);
1396 for (index = 0; index < MAX_HWIFS; ++index) {
1398 ide_hwif_t *hwif = &ide_hwifs[index];
1401 if (hwif->chipset == ide_unknown || hwif->chipset == ide_forced)
1402 hwif->chipset = ide_generic;
1403 hwif_register_devices(hwif);
1406 for (index = 0; index < MAX_HWIFS; ++index)
1408 ide_proc_register_port(&ide_hwifs[index]);
1412 EXPORT_SYMBOL_GPL(ideprobe_init);
1414 int ide_device_add(u8 idx[4])
1418 for (i = 0; i < 4; i++) {
1420 rc |= probe_hwif_init(&ide_hwifs[idx[i]]);
1423 for (i = 0; i < 4; i++) {
1425 ide_proc_register_port(&ide_hwifs[idx[i]]);
1431 EXPORT_SYMBOL_GPL(ide_device_add);