2 * linux/drivers/ide/ide-probe.c Version 1.07 March 18, 2001
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.
32 #undef REALLY_SLOW_IO /* most systems can safely undef this */
34 #include <linux/config.h>
35 #include <linux/module.h>
36 #include <linux/types.h>
37 #include <linux/string.h>
38 #include <linux/kernel.h>
39 #include <linux/timer.h>
41 #include <linux/interrupt.h>
42 #include <linux/major.h>
43 #include <linux/errno.h>
44 #include <linux/genhd.h>
45 #include <linux/slab.h>
46 #include <linux/delay.h>
47 #include <linux/ide.h>
48 #include <linux/spinlock.h>
49 #include <linux/pci.h>
50 #include <linux/kmod.h>
52 #include <asm/byteorder.h>
54 #include <asm/uaccess.h>
58 * CompactFlash cards and their brethern pretend to be removable
60 * (1) they never have a slave unit, and
61 * (2) they don't have doorlock mechanisms.
62 * This test catches them, and is invoked elsewhere when setting
63 * appropriate config bits.
65 * FIXME: This treatment is probably applicable for *all* PCMCIA (PC CARD)
66 * devices, so in linux 2.3.x we should change this to just treat all PCMCIA
67 * drives this way, and get rid of the model-name tests below
68 * (too big of an interface change for 2.2.x).
69 * At that time, we might also consider parameterizing the timeouts and retries,
70 * since these are MUCH faster than mechanical drives. -M.Lord
72 inline int drive_is_flashcard (ide_drive_t *drive)
74 struct hd_driveid *id = drive->id;
76 if (drive->removable && id != NULL) {
77 if (id->config == 0x848a) return 1; /* CompactFlash */
78 if (!strncmp(id->model, "KODAK ATA_FLASH", 15) /* Kodak */
79 || !strncmp(id->model, "Hitachi CV", 10) /* Hitachi */
80 || !strncmp(id->model, "SunDisk SDCFB", 13) /* SunDisk */
81 || !strncmp(id->model, "HAGIWARA HPC", 12) /* Hagiwara */
82 || !strncmp(id->model, "LEXAR ATA_FLASH", 15) /* Lexar */
83 || !strncmp(id->model, "ATA_FLASH", 9)) /* Simple Tech */
85 return 1; /* yes, it is a flash memory card */
88 return 0; /* no, it is not a flash memory card */
91 static inline void do_identify (ide_drive_t *drive, u8 cmd)
93 ide_hwif_t *hwif = HWIF(drive);
95 struct hd_driveid *id;
97 /* called with interrupts disabled! */
98 id = drive->id = kmalloc(SECTOR_WORDS*4, GFP_ATOMIC);
100 printk(KERN_WARNING "(ide-probe::do_identify) "
104 /* read 512 bytes of id info */
105 hwif->ata_input_data(drive, id, SECTOR_WORDS);
110 if (!drive->forced_lun)
111 drive->last_lun = id->last_lun & 0x7;
113 #if defined (CONFIG_SCSI_EATA_DMA) || defined (CONFIG_SCSI_EATA_PIO) || defined (CONFIG_SCSI_EATA)
115 * EATA SCSI controllers do a hardware ATA emulation:
116 * Ignore them if there is a driver for them available.
118 if ((id->model[0] == 'P' && id->model[1] == 'M') ||
119 (id->model[0] == 'S' && id->model[1] == 'K')) {
120 printk("%s: EATA SCSI HBA %.10s\n", drive->name, id->model);
123 #endif /* CONFIG_SCSI_EATA_DMA || CONFIG_SCSI_EATA_PIO */
126 * WIN_IDENTIFY returns little-endian info,
127 * WIN_PIDENTIFY *usually* returns little-endian info.
129 if (cmd == WIN_PIDENTIFY) {
130 if ((id->model[0] == 'N' && id->model[1] == 'E') /* NEC */
131 || (id->model[0] == 'F' && id->model[1] == 'X') /* Mitsumi */
132 || (id->model[0] == 'P' && id->model[1] == 'i'))/* Pioneer */
133 /* Vertos drives may still be weird */
136 ide_fixstring(id->model, sizeof(id->model), bswap);
137 ide_fixstring(id->fw_rev, sizeof(id->fw_rev), bswap);
138 ide_fixstring(id->serial_no, sizeof(id->serial_no), bswap);
140 if (strstr(id->model, "E X A B Y T E N E S T"))
143 /* we depend on this a lot! */
144 id->model[sizeof(id->model)-1] = '\0';
145 printk("%s: %s, ", drive->name, id->model);
150 * Check for an ATAPI device
152 if (cmd == WIN_PIDENTIFY) {
153 u8 type = (id->config >> 8) & 0x1f;
155 #ifdef CONFIG_BLK_DEV_PDC4030
156 if (hwif->channel == 1 && hwif->chipset == ide_pdc4030) {
157 printk(" -- not supported on 2nd Promise port\n");
160 #endif /* CONFIG_BLK_DEV_PDC4030 */
163 if (!strstr(id->model, "CD-ROM")) {
164 if (!strstr(id->model, "oppy") &&
165 !strstr(id->model, "poyp") &&
166 !strstr(id->model, "ZIP"))
167 printk("cdrom or floppy?, assuming ");
168 if (drive->media != ide_cdrom) {
170 drive->removable = 1;
174 /* Early cdrom models used zero */
177 drive->removable = 1;
179 /* kludge for Apple PowerBook internal zip */
180 if (!strstr(id->model, "CD-ROM") &&
181 strstr(id->model, "ZIP")) {
187 printk ("CD/DVD-ROM");
194 drive->removable = 1;
197 printk("UNKNOWN (type %d)", type);
206 * Not an ATAPI device: looks like a "regular" hard disk
208 if (id->config & (1<<7))
209 drive->removable = 1;
211 * Prevent long system lockup probing later for non-existant
212 * slave drive if the hwif is actually a flash memory card of
216 if (drive_is_flashcard(drive)) {
217 ide_drive_t *mate = &hwif->drives[1^drive->select.b.unit];
218 if (!mate->ata_flash) {
224 drive->media = ide_disk;
225 printk("%s DISK drive\n", (drive->is_flash) ? "CFA" : "ATA" );
237 * try_to_identify() sends an ATA(PI) IDENTIFY request to a drive
238 * and waits for a response. It also monitors irqs while this is
239 * happening, in hope of automatically determining which one is
240 * being used by the interface.
242 * Returns: 0 device was identified
243 * 1 device timed-out (no response to identify request)
244 * 2 device aborted the command (refused to identify itself)
246 static int actual_try_to_identify (ide_drive_t *drive, u8 cmd)
248 ide_hwif_t *hwif = HWIF(drive);
250 ide_ioreg_t hd_status;
251 unsigned long timeout;
254 if (IDE_CONTROL_REG) {
255 /* take a deep breath */
257 a = hwif->INB(IDE_ALTSTATUS_REG);
258 s = hwif->INB(IDE_STATUS_REG);
259 if ((a ^ s) & ~INDEX_STAT) {
260 printk("%s: probing with STATUS(0x%02x) instead of "
261 "ALTSTATUS(0x%02x)\n", drive->name, s, a);
262 /* ancient Seagate drives, broken interfaces */
263 hd_status = IDE_STATUS_REG;
265 /* use non-intrusive polling */
266 hd_status = IDE_ALTSTATUS_REG;
270 hd_status = IDE_STATUS_REG;
273 /* set features register for atapi
274 * identify command to be sure of reply
276 if ((cmd == WIN_PIDENTIFY))
277 /* disable dma & overlap */
278 hwif->OUTB(0, IDE_FEATURE_REG);
280 if (hwif->identify != NULL) {
281 if (hwif->identify(drive))
284 /* ask drive for ID */
285 hwif->OUTB(cmd, IDE_COMMAND_REG);
287 timeout = ((cmd == WIN_IDENTIFY) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
290 if (time_after(jiffies, timeout)) {
291 /* drive timed-out */
294 /* give drive a breather */
296 } while ((hwif->INB(hd_status)) & BUSY_STAT);
298 /* wait for IRQ and DRQ_STAT */
300 if (OK_STAT((hwif->INB(IDE_STATUS_REG)), DRQ_STAT, BAD_R_STAT)) {
303 /* local CPU only; some systems need this */
304 local_irq_save(flags);
305 /* drive returned ID */
306 do_identify(drive, cmd);
307 /* drive responded with ID */
309 /* clear drive IRQ */
310 (void) hwif->INB(IDE_STATUS_REG);
311 local_irq_restore(flags);
313 /* drive refused ID */
319 static int try_to_identify (ide_drive_t *drive, u8 cmd)
321 ide_hwif_t *hwif = HWIF(drive);
324 unsigned long cookie = 0;
326 if (IDE_CONTROL_REG && !hwif->irq) {
328 cookie = probe_irq_on();
329 /* enable device irq */
330 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
333 retval = actual_try_to_identify(drive, cmd);
337 /* mask device irq */
338 hwif->OUTB(drive->ctl|2, IDE_CONTROL_REG);
339 /* clear drive IRQ */
340 (void) hwif->INB(IDE_STATUS_REG);
342 irq = probe_irq_off(cookie);
347 /* Mmmm.. multiple IRQs..
348 * don't know which was ours
350 printk("%s: IRQ probe failed (0x%lx)\n",
351 drive->name, cookie);
352 #ifdef CONFIG_BLK_DEV_CMD640
353 #ifdef CMD640_DUMP_REGS
354 if (hwif->chipset == ide_cmd640) {
355 printk("%s: Hmmm.. probably a driver "
356 "problem.\n", drive->name);
359 #endif /* CMD640_DUMP_REGS */
360 #endif /* CONFIG_BLK_DEV_CMD640 */
369 * do_probe() has the difficult job of finding a drive if it exists,
370 * without getting hung up if it doesn't exist, without trampling on
371 * ethernet cards, and without leaving any IRQs dangling to haunt us later.
373 * If a drive is "known" to exist (from CMOS or kernel parameters),
374 * but does not respond right away, the probe will "hang in there"
375 * for the maximum wait time (about 30 seconds), otherwise it will
376 * exit much more quickly.
378 * Returns: 0 device was identified
379 * 1 device timed-out (no response to identify request)
380 * 2 device aborted the command (refused to identify itself)
381 * 3 bad status from device (possible for ATAPI drives)
382 * 4 probe was not attempted because failure was obvious
384 static int do_probe (ide_drive_t *drive, u8 cmd)
387 ide_hwif_t *hwif = HWIF(drive);
389 if (drive->present) {
390 /* avoid waiting for inappropriate probes */
391 if ((drive->media != ide_disk) && (cmd == WIN_IDENTIFY))
395 printk("probing for %s: present=%d, media=%d, probetype=%s\n",
396 drive->name, drive->present, drive->media,
397 (cmd == WIN_IDENTIFY) ? "ATA" : "ATAPI");
400 /* needed for some systems
401 * (e.g. crw9624 as drive0 with disk as slave)
406 if (hwif->INB(IDE_SELECT_REG) != drive->select.all && !drive->present) {
407 if (drive->select.b.unit != 0) {
408 /* exit with drive0 selected */
409 SELECT_DRIVE(&hwif->drives[0]);
410 /* allow BUSY_STAT to assert & clear */
413 /* no i/f present: mmm.. this should be a 4 -ml */
417 if (OK_STAT((hwif->INB(IDE_STATUS_REG)), READY_STAT, BUSY_STAT) ||
418 drive->present || cmd == WIN_PIDENTIFY) {
419 /* send cmd and wait */
420 if ((rc = try_to_identify(drive, cmd))) {
421 /* failed: try again */
422 rc = try_to_identify(drive,cmd);
424 if (hwif->INB(IDE_STATUS_REG) == (BUSY_STAT|READY_STAT))
427 if ((rc == 1 && cmd == WIN_PIDENTIFY) &&
428 ((drive->autotune == IDE_TUNE_DEFAULT) ||
429 (drive->autotune == IDE_TUNE_AUTO))) {
430 unsigned long timeout;
431 printk("%s: no response (status = 0x%02x), "
432 "resetting drive\n", drive->name,
433 hwif->INB(IDE_STATUS_REG));
435 hwif->OUTB(drive->select.all, IDE_SELECT_REG);
437 hwif->OUTB(WIN_SRST, IDE_COMMAND_REG);
439 while (((hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) &&
440 time_before(jiffies, timeout + WAIT_WORSTCASE))
442 rc = try_to_identify(drive, cmd);
445 printk("%s: no response (status = 0x%02x)\n",
446 drive->name, hwif->INB(IDE_STATUS_REG));
447 /* ensure drive irq is clear */
448 (void) hwif->INB(IDE_STATUS_REG);
450 /* not present or maybe ATAPI */
453 if (drive->select.b.unit != 0) {
454 /* exit with drive0 selected */
455 SELECT_DRIVE(&hwif->drives[0]);
457 /* ensure drive irq is clear */
458 (void) hwif->INB(IDE_STATUS_REG);
466 static void enable_nest (ide_drive_t *drive)
468 ide_hwif_t *hwif = HWIF(drive);
469 unsigned long timeout;
471 printk("%s: enabling %s -- ", hwif->name, drive->id->model);
474 hwif->OUTB(EXABYTE_ENABLE_NEST, IDE_COMMAND_REG);
475 timeout = jiffies + WAIT_WORSTCASE;
477 if (time_after(jiffies, timeout)) {
478 printk("failed (timeout)\n");
482 } while ((hwif->INB(IDE_STATUS_REG)) & BUSY_STAT);
486 if (!OK_STAT((hwif->INB(IDE_STATUS_REG)), 0, BAD_STAT)) {
487 printk("failed (status = 0x%02x)\n", hwif->INB(IDE_STATUS_REG));
492 /* if !(success||timed-out) */
493 if (do_probe(drive, WIN_IDENTIFY) >= 2) {
494 /* look for ATAPI device */
495 (void) do_probe(drive, WIN_PIDENTIFY);
500 * probe_for_drive() tests for existence of a given drive using do_probe().
502 * Returns: 0 no device was found
503 * 1 device was found (note: drive->present might still be 0)
505 static inline u8 probe_for_drive (ide_drive_t *drive)
509 return drive->present;
511 /* if !(success||timed-out) */
512 if (do_probe(drive, WIN_IDENTIFY) >= 2) {
513 /* look for ATAPI device */
514 (void) do_probe(drive, WIN_PIDENTIFY);
516 if (drive->id && strstr(drive->id->model, "E X A B Y T E N E S T"))
519 /* drive not found */
522 /* identification failed? */
523 if (drive->id == NULL) {
524 if (drive->media == ide_disk) {
525 printk("%s: non-IDE drive, CHS=%d/%d/%d\n",
526 drive->name, drive->cyl,
527 drive->head, drive->sect);
528 } else if (drive->media == ide_cdrom) {
529 printk("%s: ATAPI cdrom (?)\n", drive->name);
535 /* drive was found */
539 #define hwif_check_region(addr, num) \
540 ((hwif->mmio) ? check_mem_region((addr),(num)) : check_region((addr),(num)))
542 static int hwif_check_regions (ide_hwif_t *hwif)
549 addr_errs = hwif_check_region(hwif->io_ports[IDE_DATA_OFFSET], 1);
550 for (i = IDE_ERROR_OFFSET; i <= IDE_STATUS_OFFSET; i++)
551 addr_errs += hwif_check_region(hwif->io_ports[i], 1);
552 if (hwif->io_ports[IDE_CONTROL_OFFSET])
553 addr_errs += hwif_check_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1);
554 #if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
555 if (hwif->io_ports[IDE_IRQ_OFFSET])
556 addr_errs += hwif_check_region(hwif->io_ports[IDE_IRQ_OFFSET], 1);
557 #endif /* (CONFIG_AMIGA) || (CONFIG_MAC) */
558 /* If any errors are return, we drop the hwif interface. */
563 //EXPORT_SYMBOL(hwif_check_regions);
565 #define hwif_request_region(addr, num, name) \
566 ((hwif->mmio) ? request_mem_region((addr),(num),(name)) : request_region((addr),(num),(name)))
568 static void hwif_register (ide_hwif_t *hwif)
574 if (hwif->io_ports[IDE_CONTROL_OFFSET])
575 hwif_request_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1, hwif->name);
576 #if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
577 if (hwif->io_ports[IDE_IRQ_OFFSET])
578 hwif_request_region(hwif->io_ports[IDE_IRQ_OFFSET], 1, hwif->name);
579 #endif /* (CONFIG_AMIGA) || (CONFIG_MAC) */
580 if (((unsigned long)hwif->io_ports[IDE_DATA_OFFSET] | 7) ==
581 ((unsigned long)hwif->io_ports[IDE_STATUS_OFFSET])) {
582 hwif_request_region(hwif->io_ports[IDE_DATA_OFFSET], 8, hwif->name);
587 for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++)
588 hwif_request_region(hwif->io_ports[i], 1, hwif->name);
591 //EXPORT_SYMBOL(hwif_register);
594 * This function waits for the hwif to report a non-busy status
595 * see comments in probe_hwif()
597 static int wait_not_busy(ide_hwif_t *hwif, unsigned long timeout)
602 /* Turn this into a schedule() sleep once I'm sure
603 * about locking issues (2.5 work ?)
606 stat = hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
607 if ((stat & BUSY_STAT) == 0)
609 /* Assume a value of 0xff means nothing is connected to
610 * the interface and it doesn't implement the pull-down
616 return ((stat & BUSY_STAT) == 0) ? 0 : -EBUSY;
619 static int wait_hwif_ready(ide_hwif_t *hwif)
623 printk("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 = 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 = wait_not_busy(hwif, 10000);
644 SELECT_DRIVE(&hwif->drives[1]);
645 hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
647 rc = wait_not_busy(hwif, 10000);
649 /* Exit function with master reselected (let's be sane) */
650 SELECT_DRIVE(&hwif->drives[0]);
656 * This routine only knows how to look for drive units 0 and 1
657 * on an interface, so any setting of MAX_DRIVES > 2 won't work here.
659 void probe_hwif (ide_hwif_t *hwif)
667 #ifdef CONFIG_BLK_DEV_IDE
668 if (hwif->io_ports[IDE_DATA_OFFSET] == HD_DATA) {
669 extern void probe_cmos_for_drives(ide_hwif_t *);
670 probe_cmos_for_drives(hwif);
673 if ((hwif->chipset != ide_4drives || !hwif->mate || !hwif->mate->present) &&
674 #if CONFIG_BLK_DEV_PDC4030
675 (hwif->chipset != ide_pdc4030 || hwif->channel == 0) &&
676 #endif /* CONFIG_BLK_DEV_PDC4030 */
677 (hwif_check_regions(hwif))) {
679 for (unit = 0; unit < MAX_DRIVES; ++unit) {
680 ide_drive_t *drive = &hwif->drives[unit];
681 if (drive->present) {
683 printk("%s: ERROR, PORTS ALREADY IN USE\n",
689 printk("%s: ports already in use, skipping probe\n",
695 * We must always disable IRQ, as probe_for_drive will assert IRQ, but
696 * we'll install our IRQ driver much later...
700 disable_irq(hwif->irq);
702 local_irq_set(flags);
704 /* This is needed on some PPCs and a bunch of BIOS-less embedded
705 * platforms. Typical cases are:
707 * - The firmware hard reset the disk before booting the kernel,
708 * the drive is still doing it's poweron-reset sequence, that
709 * can take up to 30 seconds
710 * - The firmware does nothing (or no firmware), the device is
711 * still in POST state (same as above actually).
712 * - Some CD/DVD/Writer combo drives tend to drive the bus during
713 * their reset sequence even when they are non-selected slave
714 * devices, thus preventing discovery of the main HD
716 * Doing this wait-for-busy should not harm any existing configuration
717 * (at least things won't be worse than what current code does, that
718 * is blindly go & talk to the drive) and fix some issues like the
723 if (wait_hwif_ready(hwif))
724 printk(KERN_WARNING "%s: Wait for ready failed before probe !\n", hwif->name);
727 * Second drive should only exist if first drive was found,
728 * but a lot of cdrom drives are configured as single slaves.
730 for (unit = 0; unit < MAX_DRIVES; ++unit) {
731 ide_drive_t *drive = &hwif->drives[unit];
732 drive->dn = ((hwif->channel ? 2 : 0) + unit);
733 hwif->drives[unit].dn = ((hwif->channel ? 2 : 0) + unit);
734 (void) probe_for_drive(drive);
735 if (drive->present && !hwif->present) {
737 if (hwif->chipset != ide_4drives ||
739 !hwif->mate->present) {
744 if (hwif->io_ports[IDE_CONTROL_OFFSET] && hwif->reset) {
745 unsigned long timeout = jiffies + WAIT_WORSTCASE;
748 printk("%s: reset\n", hwif->name);
749 hwif->OUTB(12, hwif->io_ports[IDE_CONTROL_OFFSET]);
751 hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
754 stat = hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
755 } while ((stat & BUSY_STAT) && time_after(timeout, jiffies));
758 local_irq_restore(flags);
760 * Use cached IRQ number. It might be (and is...) changed by probe
766 for (unit = 0; unit < MAX_DRIVES; ++unit) {
767 ide_drive_t *drive = &hwif->drives[unit];
770 if (drive->present) {
771 if (hwif->tuneproc != NULL &&
772 drive->autotune == IDE_TUNE_AUTO)
773 /* auto-tune PIO mode */
774 hwif->tuneproc(drive, 255);
776 #ifdef CONFIG_IDEDMA_ONLYDISK
777 if (drive->media != ide_disk)
781 * MAJOR HACK BARF :-/
783 * FIXME: chipsets own this cruft!
786 * Move here to prevent module loading clashing.
788 // drive->autodma = hwif->autodma;
789 if ((hwif->ide_dma_check) &&
790 ((drive->autotune == IDE_TUNE_DEFAULT) ||
791 (drive->autotune == IDE_TUNE_AUTO))) {
793 * Force DMAing for the beginning of the check.
794 * Some chipsets appear to do interesting
795 * things, if not checked and cleared.
798 hwif->ide_dma_off_quietly(drive);
800 hwif->ide_dma_check(drive);
806 EXPORT_SYMBOL(probe_hwif);
809 int hwif_init (ide_hwif_t *hwif);
810 int probe_hwif_init (ide_hwif_t *hwif)
812 hwif->initializing = 1;
819 for (unit = 0; unit < MAX_DRIVES; ++unit) {
820 ide_drive_t *drive = &hwif->drives[unit];
821 if (drive->present) {
827 hwif->initializing = 0;
831 EXPORT_SYMBOL(probe_hwif_init);
836 * save_match() is used to simplify logic in init_irq() below.
838 * A loophole here is that we may not know about a particular
839 * hwif's irq until after that hwif is actually probed/initialized..
840 * This could be a problem for the case where an hwif is on a
841 * dual interface that requires serialization (eg. cmd640) and another
842 * hwif using one of the same irqs is initialized beforehand.
844 * This routine detects and reports such situations, but does not fix them.
846 void save_match (ide_hwif_t *hwif, ide_hwif_t *new, ide_hwif_t **match)
848 ide_hwif_t *m = *match;
850 if (m && m->hwgroup && m->hwgroup != new->hwgroup) {
853 printk("%s: potential irq problem with %s and %s\n",
854 hwif->name, new->name, m->name);
856 if (!m || m->irq != hwif->irq) /* don't undo a prior perfect match */
859 EXPORT_SYMBOL(save_match);
860 #endif /* MAX_HWIFS > 1 */
865 static void ide_init_queue(ide_drive_t *drive)
867 request_queue_t *q = &drive->queue;
869 q->queuedata = HWGROUP(drive);
870 blk_init_queue(q, do_ide_request);
872 if (drive->media == ide_disk) {
873 #ifdef CONFIG_BLK_DEV_ELEVATOR_NOOP
874 elevator_init(&q->elevator, ELEVATOR_NOOP);
879 #undef __IRQ_HELL_SPIN
881 * This routine sets up the irq for an ide interface, and creates a new
882 * hwgroup for the irq/hwif if none was previously assigned.
884 * Much of the code is for correctly detecting/handling irq sharing
885 * and irq serialization situations. This is somewhat complex because
886 * it handles static as well as dynamic (PCMCIA) IDE interfaces.
888 * The SA_INTERRUPT in sa_flags means ide_intr() is always entered with
889 * interrupts completely disabled. This can be bad for interrupt latency,
890 * but anything else has led to problems on some machines. We re-enable
891 * interrupts as much as we can safely do in most places.
893 int init_irq (ide_hwif_t *hwif)
897 ide_hwgroup_t *hwgroup, *new_hwgroup;
898 ide_hwif_t *match = NULL;
901 /* Allocate the buffer and no sleep allowed */
902 new_hwgroup = kmalloc(sizeof(ide_hwgroup_t),GFP_ATOMIC);
904 /* Allocate the buffer and potentially sleep first */
905 new_hwgroup = kmalloc(sizeof(ide_hwgroup_t),GFP_KERNEL);
908 #ifndef __IRQ_HELL_SPIN
911 spin_lock_irqsave(&io_request_lock, flags);
914 hwif->hwgroup = NULL;
917 * Group up with any other hwifs that share our irq(s).
919 for (index = 0; index < MAX_HWIFS; index++) {
920 ide_hwif_t *h = &ide_hwifs[index];
921 if (h->hwgroup) { /* scan only initialized hwif's */
922 if (hwif->irq == h->irq) {
923 hwif->sharing_irq = h->sharing_irq = 1;
924 if (hwif->chipset != ide_pci ||
925 h->chipset != ide_pci) {
926 save_match(hwif, h, &match);
929 if (hwif->serialized) {
930 if (hwif->mate && hwif->mate->irq == h->irq)
931 save_match(hwif, h, &match);
934 if (h->mate && hwif->irq == h->mate->irq)
935 save_match(hwif, h, &match);
939 #endif /* MAX_HWIFS > 1 */
941 * If we are still without a hwgroup, then form a new one
944 hwgroup = match->hwgroup;
948 hwgroup = new_hwgroup;
950 #ifndef __IRQ_HELL_SPIN
951 restore_flags(flags);
953 spin_unlock_irqrestore(&io_request_lock, flags);
957 memset(hwgroup, 0, sizeof(ide_hwgroup_t));
958 hwgroup->hwif = hwif->next = hwif;
960 hwgroup->handler = NULL;
961 hwgroup->drive = NULL;
963 init_timer(&hwgroup->timer);
964 hwgroup->timer.function = &ide_timer_expiry;
965 hwgroup->timer.data = (unsigned long) hwgroup;
969 * Allocate the irq, if not already obtained for another hwif
971 if (!match || match->irq != hwif->irq) {
972 int sa = SA_INTERRUPT;
973 #if defined(__mc68000__) || defined(CONFIG_APUS)
975 #endif /* __mc68000__ || CONFIG_APUS */
977 if (IDE_CHIPSET_IS_PCI(hwif->chipset)) {
979 #ifndef CONFIG_IDEPCI_SHARE_IRQ
981 #endif /* CONFIG_IDEPCI_SHARE_IRQ */
984 if (hwif->io_ports[IDE_CONTROL_OFFSET])
986 hwif->OUTB(0x08, hwif->io_ports[IDE_CONTROL_OFFSET]);
988 if (request_irq(hwif->irq,&ide_intr,sa,hwif->name,hwgroup)) {
991 #ifndef __IRQ_HELL_SPIN
992 restore_flags(flags);
994 spin_unlock_irqrestore(&io_request_lock, flags);
1001 * Everything is okay, so link us into the hwgroup
1003 hwif->hwgroup = hwgroup;
1004 hwif->next = hwgroup->hwif->next;
1005 hwgroup->hwif->next = hwif;
1007 for (index = 0; index < MAX_DRIVES; ++index) {
1008 ide_drive_t *drive = &hwif->drives[index];
1009 if (!drive->present)
1011 if (!hwgroup->drive)
1012 hwgroup->drive = drive;
1013 drive->next = hwgroup->drive->next;
1014 hwgroup->drive->next = drive;
1015 ide_init_queue(drive);
1017 if (!hwgroup->hwif) {
1018 hwgroup->hwif = HWIF(hwgroup->drive);
1020 printk("%s : Adding missed hwif to hwgroup!!\n", hwif->name);
1024 /* all CPUs; safe now that hwif->hwgroup is set up */
1025 #ifndef __IRQ_HELL_SPIN
1026 restore_flags(flags);
1028 spin_unlock_irqrestore(&io_request_lock, flags);
1031 #if !defined(__mc68000__) && !defined(CONFIG_APUS) && !defined(__sparc__)
1032 printk("%s at 0x%03lx-0x%03lx,0x%03lx on irq %d", hwif->name,
1033 hwif->io_ports[IDE_DATA_OFFSET],
1034 hwif->io_ports[IDE_DATA_OFFSET]+7,
1035 hwif->io_ports[IDE_CONTROL_OFFSET], hwif->irq);
1036 #elif defined(__sparc__)
1037 printk("%s at 0x%03lx-0x%03lx,0x%03lx on irq %s", hwif->name,
1038 hwif->io_ports[IDE_DATA_OFFSET],
1039 hwif->io_ports[IDE_DATA_OFFSET]+7,
1040 hwif->io_ports[IDE_CONTROL_OFFSET], __irq_itoa(hwif->irq));
1042 printk("%s at %x on irq 0x%08x", hwif->name,
1043 hwif->io_ports[IDE_DATA_OFFSET], hwif->irq);
1044 #endif /* __mc68000__ && CONFIG_APUS */
1046 printk(" (%sed with %s)",
1047 hwif->sharing_irq ? "shar" : "serializ", match->name);
1052 EXPORT_SYMBOL(init_irq);
1055 * init_gendisk() (as opposed to ide_geninit) is called for each major device,
1056 * after probing for drives, to allocate partition tables and other data
1057 * structures needed for the routines in genhd.c. ide_geninit() gets called
1058 * somewhat later, during the partition check.
1060 static void init_gendisk (ide_hwif_t *hwif)
1063 unsigned int unit, units, minors;
1064 int *bs, *max_sect, *max_ra;
1065 extern devfs_handle_t ide_devfs_handle;
1069 minors = units * (1<<PARTN_BITS);
1070 gd = kmalloc(sizeof(struct gendisk), GFP_KERNEL);
1072 goto err_kmalloc_gd;
1073 memset(gd, 0, sizeof(struct gendisk));
1075 gd->sizes = kmalloc(minors * sizeof(int), GFP_KERNEL);
1077 goto err_kmalloc_gd_sizes;
1078 gd->part = kmalloc(minors * sizeof(struct hd_struct), GFP_KERNEL);
1080 goto err_kmalloc_gd_part;
1081 bs = kmalloc(minors*sizeof(int), GFP_KERNEL);
1083 goto err_kmalloc_bs;
1084 max_sect = kmalloc(minors*sizeof(int), GFP_KERNEL);
1086 goto err_kmalloc_max_sect;
1087 max_ra = kmalloc(minors*sizeof(int), GFP_KERNEL);
1089 goto err_kmalloc_max_ra;
1091 memset(gd->part, 0, minors * sizeof(struct hd_struct));
1093 /* cdroms and msdos f/s are examples of non-1024 blocksizes */
1094 blksize_size[hwif->major] = bs;
1095 max_sectors[hwif->major] = max_sect;
1096 max_readahead[hwif->major] = max_ra;
1097 for (unit = 0; unit < minors; ++unit) {
1100 * IDE can do up to 128K per request == 256
1102 *max_sect++ = ((hwif->rqsize) ? hwif->rqsize : 128);
1103 *max_ra++ = vm_max_readahead;
1106 for (unit = 0; unit < units; ++unit)
1107 hwif->drives[unit].part = &gd->part[unit << PARTN_BITS];
1109 /* our major device number */
1110 gd->major = hwif->major;
1111 /* treated special in genhd.c */
1112 gd->major_name = IDE_MAJOR_NAME;
1113 /* num bits for partitions */
1114 gd->minor_shift = PARTN_BITS;
1115 /* 1 + max partitions / drive */
1116 gd->max_p = 1<<PARTN_BITS;
1117 /* current num real drives */
1118 gd->nr_real = units;
1119 /* ptr to internal data */
1120 gd->real_devices= hwif;
1121 /* linked list of major devs */
1123 /* file operations */
1124 gd->fops = ide_fops;
1125 gd->de_arr = kmalloc(sizeof *gd->de_arr * units, GFP_KERNEL);
1126 gd->flags = kmalloc(sizeof *gd->flags * units, GFP_KERNEL);
1128 memset(gd->de_arr, 0, sizeof *gd->de_arr * units);
1130 memset(gd->flags, 0, sizeof *gd->flags * units);
1135 for (unit = 0; unit < units; ++unit) {
1137 ide_add_generic_settings(hwif->drives + unit);
1138 // hwif->drives[unit].dn = ((hwif->channel ? 2 : 0) + unit);
1139 sprintf (name, "host%d/bus%d/target%d/lun%d",
1140 (hwif->channel && hwif->mate) ?
1141 hwif->mate->index : hwif->index,
1142 hwif->channel, unit, hwif->drives[unit].lun);
1143 if (hwif->drives[unit].present)
1144 hwif->drives[unit].de = devfs_mk_dir(ide_devfs_handle, name, NULL);
1150 err_kmalloc_max_sect:
1154 err_kmalloc_gd_part:
1156 err_kmalloc_gd_sizes:
1159 printk(KERN_WARNING "(ide::init_gendisk) Out of memory\n");
1163 EXPORT_SYMBOL(init_gendisk);
1165 int hwif_init (ide_hwif_t *hwif)
1170 if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET])))
1172 printk("%s: DISABLED, NO IRQ\n", hwif->name);
1173 return (hwif->present = 0);
1176 #ifdef CONFIG_BLK_DEV_HD
1177 if (hwif->irq == HD_IRQ && hwif->io_ports[IDE_DATA_OFFSET] != HD_DATA) {
1178 printk("%s: CANNOT SHARE IRQ WITH OLD "
1179 "HARDDISK DRIVER (hd.c)\n", hwif->name);
1180 return (hwif->present = 0);
1182 #endif /* CONFIG_BLK_DEV_HD */
1184 /* we set it back to 1 if all is ok below */
1187 if (devfs_register_blkdev (hwif->major, hwif->name, ide_fops)) {
1188 printk("%s: UNABLE TO GET MAJOR NUMBER %d\n",
1189 hwif->name, hwif->major);
1190 return (hwif->present = 0);
1193 if (init_irq(hwif)) {
1196 * It failed to initialise. Find the default IRQ for
1197 * this port and try that.
1199 if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET]))) {
1200 printk("%s: Disabled unable to get IRQ %d.\n",
1202 (void) unregister_blkdev(hwif->major, hwif->name);
1203 return (hwif->present = 0);
1205 if (init_irq(hwif)) {
1206 printk("%s: probed IRQ %d and default IRQ %d failed.\n",
1207 hwif->name, i, hwif->irq);
1208 (void) unregister_blkdev(hwif->major, hwif->name);
1209 return (hwif->present = 0);
1211 printk("%s: probed IRQ %d failed, using default.\n",
1212 hwif->name, hwif->irq);
1216 blk_dev[hwif->major].data = hwif;
1217 blk_dev[hwif->major].queue = ide_get_queue;
1218 read_ahead[hwif->major] = 8; /* (4kB) */
1219 hwif->present = 1; /* success */
1221 #if (DEBUG_SPINLOCK > 0)
1223 static int done = 0;
1225 printk("io_request_lock is %p\n", &io_request_lock); /* FIXME */
1228 return hwif->present;
1231 EXPORT_SYMBOL(hwif_init);
1233 void export_ide_init_queue (ide_drive_t *drive)
1235 ide_init_queue(drive);
1238 EXPORT_SYMBOL(export_ide_init_queue);
1240 u8 export_probe_for_drive (ide_drive_t *drive)
1242 return probe_for_drive(drive);
1245 EXPORT_SYMBOL(export_probe_for_drive);
1247 #ifndef HWIF_PROBE_CLASSIC_METHOD
1248 int probe_hwif_init (ide_hwif_t *hwif)
1250 hwif->initializing = 1;
1254 #ifndef CLASSIC_BUILTINS_METHOD
1255 # ifndef FAKE_CLASSIC_ATTACH_METHOD
1256 # ifdef DIRECT_HWIF_PROBE_ATTACH_METHOD
1257 if (hwif->present) {
1259 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1260 ide_drive_t *drive = &hwif->drives[unit];
1262 ide_attach_drive(drive);
1265 # endif /* DIRECT_HWIF_PROBE_ATTACH_METHOD */
1266 # endif /* FAKE_CLASSIC_ATTACH_METHOD */
1267 #endif /* CLASSIC_BUILTINS_METHOD */
1268 hwif->initializing = 0;
1272 EXPORT_SYMBOL(probe_hwif_init);
1274 #endif /* HWIF_PROBE_CLASSIC_METHOD */
1276 int ideprobe_init (void);
1277 static ide_module_t ideprobe_module = {
1283 int ideprobe_init (void)
1286 int probe[MAX_HWIFS];
1289 memset(probe, 0, MAX_HWIFS * sizeof(int));
1290 for (index = 0; index < MAX_HWIFS; ++index)
1291 probe[index] = !ide_hwifs[index].present;
1294 * Probe for drives in the usual way.. CMOS/BIOS, then poke at ports
1296 #ifdef HWIF_PROBE_CLASSIC_METHOD
1297 for (index = 0; index < MAX_HWIFS; ++index)
1299 probe_hwif(&ide_hwifs[index]);
1301 for (index = 0; index < MAX_HWIFS; ++index)
1303 hwif_init(&ide_hwifs[index]);
1304 #else /* HWIF_PROBE_CLASSIC_METHOD */
1305 for (index = 0; index < MAX_HWIFS; ++index)
1307 probe_hwif_init(&ide_hwifs[index]);
1308 #endif /* HWIF_PROBE_CLASSIC_METHOD */
1311 ide_probe = &ideprobe_module;
1317 extern int (*ide_xlate_1024_hook)(kdev_t, int, int, const char *);
1319 int init_module (void)
1323 for (index = 0; index < MAX_HWIFS; ++index)
1324 ide_unregister(index);
1326 create_proc_ide_interfaces();
1327 ide_xlate_1024_hook = ide_xlate_1024;
1331 void cleanup_module (void)
1334 ide_xlate_1024_hook = 0;
1336 MODULE_LICENSE("GPL");