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/pci.h>
52 #include <linux/kmod.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) /* SunDisk */
106 || !strncmp(id->model, "HAGIWARA HPC", 12) /* Hagiwara */
107 || !strncmp(id->model, "LEXAR ATA_FLASH", 15) /* Lexar */
108 || !strncmp(id->model, "ATA_FLASH", 9)) /* Simple Tech */
110 return 1; /* yes, it is a flash memory card */
113 return 0; /* no, it is not a flash memory card */
117 * do_identify - identify a drive
118 * @drive: drive to identify
121 * Called when we have issued a drive identify command to
122 * read and parse the results. This function is run with
123 * interrupts disabled.
126 static inline void do_identify (ide_drive_t *drive, u8 cmd)
128 ide_hwif_t *hwif = HWIF(drive);
130 struct hd_driveid *id;
133 /* read 512 bytes of id info */
134 hwif->ata_input_data(drive, id, SECTOR_WORDS);
140 if (!drive->forced_lun)
141 drive->last_lun = id->last_lun & 0x7;
143 #if defined (CONFIG_SCSI_EATA_DMA) || defined (CONFIG_SCSI_EATA_PIO) || defined (CONFIG_SCSI_EATA)
145 * EATA SCSI controllers do a hardware ATA emulation:
146 * Ignore them if there is a driver for them available.
148 if ((id->model[0] == 'P' && id->model[1] == 'M') ||
149 (id->model[0] == 'S' && id->model[1] == 'K')) {
150 printk("%s: EATA SCSI HBA %.10s\n", drive->name, id->model);
153 #endif /* CONFIG_SCSI_EATA_DMA || CONFIG_SCSI_EATA_PIO */
156 * WIN_IDENTIFY returns little-endian info,
157 * WIN_PIDENTIFY *usually* returns little-endian info.
159 if (cmd == WIN_PIDENTIFY) {
160 if ((id->model[0] == 'N' && id->model[1] == 'E') /* NEC */
161 || (id->model[0] == 'F' && id->model[1] == 'X') /* Mitsumi */
162 || (id->model[0] == 'P' && id->model[1] == 'i'))/* Pioneer */
163 /* Vertos drives may still be weird */
166 ide_fixstring(id->model, sizeof(id->model), bswap);
167 ide_fixstring(id->fw_rev, sizeof(id->fw_rev), bswap);
168 ide_fixstring(id->serial_no, sizeof(id->serial_no), bswap);
170 if (strstr(id->model, "E X A B Y T E N E S T"))
173 /* we depend on this a lot! */
174 id->model[sizeof(id->model)-1] = '\0';
175 printk("%s: %s, ", drive->name, id->model);
180 * Check for an ATAPI device
182 if (cmd == WIN_PIDENTIFY) {
183 u8 type = (id->config >> 8) & 0x1f;
185 #ifdef CONFIG_BLK_DEV_PDC4030
186 if (hwif->channel == 1 && hwif->chipset == ide_pdc4030) {
187 printk(" -- not supported on 2nd Promise port\n");
190 #endif /* CONFIG_BLK_DEV_PDC4030 */
193 if (!strstr(id->model, "CD-ROM")) {
194 if (!strstr(id->model, "oppy") &&
195 !strstr(id->model, "poyp") &&
196 !strstr(id->model, "ZIP"))
197 printk("cdrom or floppy?, assuming ");
198 if (drive->media != ide_cdrom) {
200 drive->removable = 1;
204 /* Early cdrom models used zero */
207 drive->removable = 1;
209 /* kludge for Apple PowerBook internal zip */
210 if (!strstr(id->model, "CD-ROM") &&
211 strstr(id->model, "ZIP")) {
217 printk ("CD/DVD-ROM");
224 drive->removable = 1;
227 printk("UNKNOWN (type %d)", type);
236 * Not an ATAPI device: looks like a "regular" hard disk
238 if (id->config & (1<<7))
239 drive->removable = 1;
242 * Prevent long system lockup probing later for non-existant
243 * slave drive if the hwif is actually a flash memory card of
247 if (drive_is_flashcard(drive)) {
249 /* The new IDE adapter widgets don't follow this heuristic
250 so we must nowdays just bite the bullet and take the
252 ide_drive_t *mate = &hwif->drives[1^drive->select.b.unit];
253 ide_drive_t *mate = &hwif->drives[1^drive->select.b.unit];
254 if (!mate->ata_flash) {
261 drive->media = ide_disk;
262 printk("%s DISK drive\n", (drive->is_flash) ? "CFA" : "ATA" );
265 /* Initialize queue depth settings */
266 drive->queue_depth = 1;
267 #ifdef CONFIG_BLK_DEV_IDE_TCQ_DEPTH
268 drive->queue_depth = CONFIG_BLK_DEV_IDE_TCQ_DEPTH;
270 drive->queue_depth = drive->id->queue_depth + 1;
272 if (drive->queue_depth < 1 || drive->queue_depth > IDE_MAX_TAG)
273 drive->queue_depth = IDE_MAX_TAG;
284 * actual_try_to_identify - send ata/atapi identify
285 * @drive: drive to identify
286 * @cmd: command to use
288 * try_to_identify() sends an ATA(PI) IDENTIFY request to a drive
289 * and waits for a response. It also monitors irqs while this is
290 * happening, in hope of automatically determining which one is
291 * being used by the interface.
293 * Returns: 0 device was identified
294 * 1 device timed-out (no response to identify request)
295 * 2 device aborted the command (refused to identify itself)
298 static int actual_try_to_identify (ide_drive_t *drive, u8 cmd)
300 ide_hwif_t *hwif = HWIF(drive);
302 unsigned long hd_status;
303 unsigned long timeout;
306 if (IDE_CONTROL_REG) {
307 /* take a deep breath */
309 a = hwif->INB(IDE_ALTSTATUS_REG);
310 s = hwif->INB(IDE_STATUS_REG);
311 if ((a ^ s) & ~INDEX_STAT) {
312 printk(KERN_INFO "%s: probing with STATUS(0x%02x) instead of "
313 "ALTSTATUS(0x%02x)\n", drive->name, s, a);
314 /* ancient Seagate drives, broken interfaces */
315 hd_status = IDE_STATUS_REG;
317 /* use non-intrusive polling */
318 hd_status = IDE_ALTSTATUS_REG;
322 hd_status = IDE_STATUS_REG;
325 /* set features register for atapi
326 * identify command to be sure of reply
328 if ((cmd == WIN_PIDENTIFY))
329 /* disable dma & overlap */
330 hwif->OUTB(0, IDE_FEATURE_REG);
332 if (hwif->identify != NULL) {
333 if (hwif->identify(drive))
336 /* ask drive for ID */
337 hwif->OUTB(cmd, IDE_COMMAND_REG);
339 timeout = ((cmd == WIN_IDENTIFY) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
342 if (time_after(jiffies, timeout)) {
343 /* drive timed-out */
346 /* give drive a breather */
348 } while ((hwif->INB(hd_status)) & BUSY_STAT);
350 /* wait for IRQ and DRQ_STAT */
352 if (OK_STAT((hwif->INB(IDE_STATUS_REG)), DRQ_STAT, BAD_R_STAT)) {
355 /* local CPU only; some systems need this */
356 local_irq_save(flags);
357 /* drive returned ID */
358 do_identify(drive, cmd);
359 /* drive responded with ID */
361 /* clear drive IRQ */
362 (void) hwif->INB(IDE_STATUS_REG);
363 local_irq_restore(flags);
365 /* drive refused ID */
372 * try_to_identify - try to identify a drive
373 * @drive: drive to probe
374 * @cmd: command to use
376 * Issue the identify command and then do IRQ probing to
377 * complete the identification when needed by finding the
378 * IRQ the drive is attached to
381 static int try_to_identify (ide_drive_t *drive, u8 cmd)
383 ide_hwif_t *hwif = HWIF(drive);
386 unsigned long cookie = 0;
389 * Disable device irq unless we need to
390 * probe for it. Otherwise we'll get spurious
391 * interrupts during the identify-phase that
392 * the irq handler isn't expecting.
394 if (IDE_CONTROL_REG) {
395 u8 ctl = drive->ctl | 2;
398 cookie = probe_irq_on();
399 /* enable device irq */
402 hwif->OUTB(ctl, IDE_CONTROL_REG);
405 retval = actual_try_to_identify(drive, cmd);
409 /* mask device irq */
410 hwif->OUTB(drive->ctl|2, IDE_CONTROL_REG);
411 /* clear drive IRQ */
412 (void) hwif->INB(IDE_STATUS_REG);
414 irq = probe_irq_off(cookie);
419 /* Mmmm.. multiple IRQs..
420 * don't know which was ours
422 printk("%s: IRQ probe failed (0x%lx)\n",
423 drive->name, cookie);
424 #ifdef CONFIG_BLK_DEV_CMD640
425 #ifdef CMD640_DUMP_REGS
426 if (hwif->chipset == ide_cmd640) {
427 printk("%s: Hmmm.. probably a driver "
428 "problem.\n", drive->name);
431 #endif /* CMD640_DUMP_REGS */
432 #endif /* CONFIG_BLK_DEV_CMD640 */
441 * do_probe - probe an IDE device
442 * @drive: drive to probe
443 * @cmd: command to use
445 * do_probe() has the difficult job of finding a drive if it exists,
446 * without getting hung up if it doesn't exist, without trampling on
447 * ethernet cards, and without leaving any IRQs dangling to haunt us later.
449 * If a drive is "known" to exist (from CMOS or kernel parameters),
450 * but does not respond right away, the probe will "hang in there"
451 * for the maximum wait time (about 30 seconds), otherwise it will
452 * exit much more quickly.
454 * Returns: 0 device was identified
455 * 1 device timed-out (no response to identify request)
456 * 2 device aborted the command (refused to identify itself)
457 * 3 bad status from device (possible for ATAPI drives)
458 * 4 probe was not attempted because failure was obvious
461 static int do_probe (ide_drive_t *drive, u8 cmd)
464 ide_hwif_t *hwif = HWIF(drive);
466 if (drive->present) {
467 /* avoid waiting for inappropriate probes */
468 if ((drive->media != ide_disk) && (cmd == WIN_IDENTIFY))
472 printk("probing for %s: present=%d, media=%d, probetype=%s\n",
473 drive->name, drive->present, drive->media,
474 (cmd == WIN_IDENTIFY) ? "ATA" : "ATAPI");
477 /* needed for some systems
478 * (e.g. crw9624 as drive0 with disk as slave)
483 if (hwif->INB(IDE_SELECT_REG) != drive->select.all && !drive->present) {
484 if (drive->select.b.unit != 0) {
485 /* exit with drive0 selected */
486 SELECT_DRIVE(&hwif->drives[0]);
487 /* allow BUSY_STAT to assert & clear */
490 /* no i/f present: mmm.. this should be a 4 -ml */
494 if (OK_STAT((hwif->INB(IDE_STATUS_REG)), READY_STAT, BUSY_STAT) ||
495 drive->present || cmd == WIN_PIDENTIFY) {
496 /* send cmd and wait */
497 if ((rc = try_to_identify(drive, cmd))) {
498 /* failed: try again */
499 rc = try_to_identify(drive,cmd);
501 if (hwif->INB(IDE_STATUS_REG) == (BUSY_STAT|READY_STAT))
504 if ((rc == 1 && cmd == WIN_PIDENTIFY) &&
505 ((drive->autotune == IDE_TUNE_DEFAULT) ||
506 (drive->autotune == IDE_TUNE_AUTO))) {
507 unsigned long timeout;
508 printk("%s: no response (status = 0x%02x), "
509 "resetting drive\n", drive->name,
510 hwif->INB(IDE_STATUS_REG));
512 hwif->OUTB(drive->select.all, IDE_SELECT_REG);
514 hwif->OUTB(WIN_SRST, IDE_COMMAND_REG);
516 while (((hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) &&
517 time_before(jiffies, timeout + WAIT_WORSTCASE))
519 rc = try_to_identify(drive, cmd);
522 printk("%s: no response (status = 0x%02x)\n",
523 drive->name, hwif->INB(IDE_STATUS_REG));
524 /* ensure drive irq is clear */
525 (void) hwif->INB(IDE_STATUS_REG);
527 /* not present or maybe ATAPI */
530 if (drive->select.b.unit != 0) {
531 /* exit with drive0 selected */
532 SELECT_DRIVE(&hwif->drives[0]);
534 /* ensure drive irq is clear */
535 (void) hwif->INB(IDE_STATUS_REG);
543 static void enable_nest (ide_drive_t *drive)
545 ide_hwif_t *hwif = HWIF(drive);
546 unsigned long timeout;
548 printk("%s: enabling %s -- ", hwif->name, drive->id->model);
551 hwif->OUTB(EXABYTE_ENABLE_NEST, IDE_COMMAND_REG);
552 timeout = jiffies + WAIT_WORSTCASE;
554 if (time_after(jiffies, timeout)) {
555 printk("failed (timeout)\n");
559 } while ((hwif->INB(IDE_STATUS_REG)) & BUSY_STAT);
563 if (!OK_STAT((hwif->INB(IDE_STATUS_REG)), 0, BAD_STAT)) {
564 printk("failed (status = 0x%02x)\n", hwif->INB(IDE_STATUS_REG));
569 /* if !(success||timed-out) */
570 if (do_probe(drive, WIN_IDENTIFY) >= 2) {
571 /* look for ATAPI device */
572 (void) do_probe(drive, WIN_PIDENTIFY);
577 * probe_for_drives - upper level drive probe
578 * @drive: drive to probe for
580 * probe_for_drive() tests for existence of a given drive using do_probe()
581 * and presents things to the user as needed.
583 * Returns: 0 no device was found
584 * 1 device was found (note: drive->present might
588 static inline u8 probe_for_drive (ide_drive_t *drive)
591 * In order to keep things simple we have an id
592 * block for all drives at all times. If the device
593 * is pre ATA or refuses ATA/ATAPI identify we
594 * will add faked data to this.
596 * Also note that 0 everywhere means "can't do X"
599 drive->id = kmalloc(SECTOR_WORDS *4, GFP_KERNEL);
601 if(drive->id == NULL)
603 printk(KERN_ERR "ide: out of memory for id data.\n");
606 memset(drive->id, 0, SECTOR_WORDS * 4);
607 strcpy(drive->id->model, "UNKNOWN");
612 /* if !(success||timed-out) */
613 if (do_probe(drive, WIN_IDENTIFY) >= 2) {
614 /* look for ATAPI device */
615 (void) do_probe(drive, WIN_PIDENTIFY);
617 if (strstr(drive->id->model, "E X A B Y T E N E S T"))
620 /* drive not found */
623 /* identification failed? */
624 if (!drive->id_read) {
625 if (drive->media == ide_disk) {
626 printk(KERN_INFO "%s: non-IDE drive, CHS=%d/%d/%d\n",
627 drive->name, drive->cyl,
628 drive->head, drive->sect);
629 } else if (drive->media == ide_cdrom) {
630 printk(KERN_INFO "%s: ATAPI cdrom (?)\n", drive->name);
633 printk(KERN_WARNING "%s: Unknown device on bus refused identification. Ignoring.\n", drive->name);
637 /* drive was found */
641 /* The drive wasn't being helpful. Add generic info only */
644 return drive->present;
647 static void hwif_register (ide_hwif_t *hwif)
649 /* register with global device tree */
650 strlcpy(hwif->gendev.bus_id,hwif->name,BUS_ID_SIZE);
651 snprintf(hwif->gendev.name,DEVICE_NAME_SIZE,"IDE Controller");
652 hwif->gendev.driver_data = hwif;
654 hwif->gendev.parent = &hwif->pci_dev->dev;
656 hwif->gendev.parent = NULL; /* Would like to do = &device_legacy */
657 device_register(&hwif->gendev);
660 //EXPORT_SYMBOL(hwif_register);
663 static int wait_hwif_ready(ide_hwif_t *hwif)
667 printk(KERN_INFO "Probing IDE interface %s...\n", hwif->name);
669 /* Let HW settle down a bit from whatever init state we
673 /* Wait for BSY bit to go away, spec timeout is 30 seconds,
674 * I know of at least one disk who takes 31 seconds, I use 35
677 rc = ide_wait_not_busy(hwif, 35000);
681 /* Now make sure both master & slave are ready */
682 SELECT_DRIVE(&hwif->drives[0]);
683 hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
685 rc = ide_wait_not_busy(hwif, 10000);
688 SELECT_DRIVE(&hwif->drives[1]);
689 hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
691 rc = ide_wait_not_busy(hwif, 10000);
693 /* Exit function with master reselected (let's be sane) */
694 SELECT_DRIVE(&hwif->drives[0]);
701 * This routine only knows how to look for drive units 0 and 1
702 * on an interface, so any setting of MAX_DRIVES > 2 won't work here.
704 void probe_hwif (ide_hwif_t *hwif)
713 if ((hwif->chipset != ide_4drives || !hwif->mate || !hwif->mate->present) &&
714 #ifdef CONFIG_BLK_DEV_PDC4030
715 (hwif->chipset != ide_pdc4030 || hwif->channel == 0) &&
716 #endif /* CONFIG_BLK_DEV_PDC4030 */
717 (ide_hwif_request_regions(hwif))) {
719 for (unit = 0; unit < MAX_DRIVES; ++unit) {
720 ide_drive_t *drive = &hwif->drives[unit];
721 if (drive->present) {
723 printk(KERN_ERR "%s: ERROR, PORTS ALREADY IN USE\n",
729 printk(KERN_ERR "%s: ports already in use, skipping probe\n",
735 * We must always disable IRQ, as probe_for_drive will assert IRQ, but
736 * we'll install our IRQ driver much later...
740 disable_irq(hwif->irq);
742 local_irq_set(flags);
745 /* This is needed on some PPCs and a bunch of BIOS-less embedded
746 * platforms. Typical cases are:
748 * - The firmware hard reset the disk before booting the kernel,
749 * the drive is still doing it's poweron-reset sequence, that
750 * can take up to 30 seconds
751 * - The firmware does nothing (or no firmware), the device is
752 * still in POST state (same as above actually).
753 * - Some CD/DVD/Writer combo drives tend to drive the bus during
754 * their reset sequence even when they are non-selected slave
755 * devices, thus preventing discovery of the main HD
757 * Doing this wait-for-busy should not harm any existing configuration
758 * (at least things won't be worse than what current code does, that
759 * is blindly go & talk to the drive) and fix some issues like the
764 if (wait_hwif_ready(hwif))
765 printk(KERN_WARNING "%s: Wait for ready failed before probe !\n", hwif->name);
766 #endif /* CONFIG_PPC */
769 * Second drive should only exist if first drive was found,
770 * but a lot of cdrom drives are configured as single slaves.
772 for (unit = 0; unit < MAX_DRIVES; ++unit) {
773 ide_drive_t *drive = &hwif->drives[unit];
774 drive->dn = ((hwif->channel ? 2 : 0) + unit);
775 hwif->drives[unit].dn = ((hwif->channel ? 2 : 0) + unit);
776 (void) probe_for_drive(drive);
777 if (drive->present && !hwif->present) {
779 if (hwif->chipset != ide_4drives ||
781 !hwif->mate->present) {
786 if (hwif->io_ports[IDE_CONTROL_OFFSET] && hwif->reset) {
787 unsigned long timeout = jiffies + WAIT_WORSTCASE;
790 printk(KERN_WARNING "%s: reset\n", hwif->name);
791 hwif->OUTB(12, hwif->io_ports[IDE_CONTROL_OFFSET]);
793 hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
796 stat = hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
797 } while ((stat & BUSY_STAT) && time_after(timeout, jiffies));
800 local_irq_restore(flags);
802 * Use cached IRQ number. It might be (and is...) changed by probe
808 if (!hwif->present) {
809 ide_hwif_release_regions(hwif);
813 for (unit = 0; unit < MAX_DRIVES; ++unit) {
814 ide_drive_t *drive = &hwif->drives[unit];
817 if (drive->present) {
818 if (hwif->tuneproc != NULL &&
819 drive->autotune == IDE_TUNE_AUTO)
820 /* auto-tune PIO mode */
821 hwif->tuneproc(drive, 255);
823 #ifdef CONFIG_IDEDMA_ONLYDISK
824 if (drive->media != ide_disk)
828 * MAJOR HACK BARF :-/
830 * FIXME: chipsets own this cruft!
833 * Move here to prevent module loading clashing.
835 // drive->autodma = hwif->autodma;
836 if ((hwif->ide_dma_check) &&
837 ((drive->autotune == IDE_TUNE_DEFAULT) ||
838 (drive->autotune == IDE_TUNE_AUTO))) {
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->ide_dma_off_quietly(drive);
847 hwif->ide_dma_check(drive);
853 EXPORT_SYMBOL(probe_hwif);
855 int hwif_init (ide_hwif_t *hwif);
856 int probe_hwif_init (ide_hwif_t *hwif)
858 hwif->initializing = 1;
864 for (unit = 0; unit < MAX_DRIVES; ++unit) {
865 ide_drive_t *drive = &hwif->drives[unit];
866 /* For now don't attach absent drives, we may
867 want them on default or a new "empty" class
868 for hotplug reprobing ? */
869 if (drive->present) {
874 hwif->initializing = 0;
878 EXPORT_SYMBOL(probe_hwif_init);
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 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 EXPORT_SYMBOL(save_match);
906 #endif /* MAX_HWIFS > 1 */
911 static void ide_init_queue(ide_drive_t *drive)
913 request_queue_t *q = &drive->queue;
914 ide_hwif_t *hwif = HWIF(drive);
915 int max_sectors = 256;
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 blk_init_queue(q, do_ide_request, &ide_lock);
926 q->queuedata = HWGROUP(drive);
927 blk_queue_segment_boundary(q, 0xffff);
930 hwif->rqsize = hwif->addressing ? 256 : 65536;
931 if (hwif->rqsize < max_sectors)
932 max_sectors = hwif->rqsize;
933 blk_queue_max_sectors(q, max_sectors);
935 /* IDE DMA can do PRD_ENTRIES number of segments. */
936 blk_queue_max_hw_segments(q, PRD_ENTRIES);
938 /* This is a driver limit and could be eliminated. */
939 blk_queue_max_phys_segments(q, PRD_ENTRIES);
943 * Setup the drive for request handling.
945 static void ide_init_drive(ide_drive_t *drive)
947 ide_toggle_bounce(drive, 1);
951 * This routine sets up the irq for an ide interface, and creates a new
952 * hwgroup for the irq/hwif if none was previously assigned.
954 * Much of the code is for correctly detecting/handling irq sharing
955 * and irq serialization situations. This is somewhat complex because
956 * it handles static as well as dynamic (PCMCIA) IDE interfaces.
958 * The SA_INTERRUPT in sa_flags means ide_intr() is always entered with
959 * interrupts completely disabled. This can be bad for interrupt latency,
960 * but anything else has led to problems on some machines. We re-enable
961 * interrupts as much as we can safely do in most places.
963 static int init_irq (ide_hwif_t *hwif)
966 ide_hwgroup_t *hwgroup;
967 ide_hwif_t *match = NULL;
970 BUG_ON(in_interrupt());
971 BUG_ON(irqs_disabled());
973 hwif->hwgroup = NULL;
976 * Group up with any other hwifs that share our irq(s).
978 for (index = 0; index < MAX_HWIFS; index++) {
979 ide_hwif_t *h = &ide_hwifs[index];
980 if (h->hwgroup) { /* scan only initialized hwif's */
981 if (hwif->irq == h->irq) {
982 hwif->sharing_irq = h->sharing_irq = 1;
983 if (hwif->chipset != ide_pci ||
984 h->chipset != ide_pci) {
985 save_match(hwif, h, &match);
988 if (hwif->serialized) {
989 if (hwif->mate && hwif->mate->irq == h->irq)
990 save_match(hwif, h, &match);
993 if (h->mate && hwif->irq == h->mate->irq)
994 save_match(hwif, h, &match);
998 #endif /* MAX_HWIFS > 1 */
1000 * If we are still without a hwgroup, then form a new one
1003 hwgroup = match->hwgroup;
1004 hwif->hwgroup = hwgroup;
1006 * Link us into the hwgroup.
1007 * This must be done early, do ensure that unexpected_intr
1008 * can find the hwif and prevent irq storms.
1009 * No drives are attached to the new hwif, choose_drive
1010 * can't do anything stupid (yet).
1011 * Add ourself as the 2nd entry to the hwgroup->hwif
1012 * linked list, the first entry is the hwif that owns
1013 * hwgroup->handler - do not change that.
1015 spin_lock_irq(&ide_lock);
1016 hwif->next = hwgroup->hwif->next;
1017 hwgroup->hwif->next = hwif;
1018 spin_unlock_irq(&ide_lock);
1020 hwgroup = kmalloc(sizeof(ide_hwgroup_t),GFP_KERNEL);
1024 hwif->hwgroup = hwgroup;
1026 memset(hwgroup, 0, sizeof(ide_hwgroup_t));
1027 hwgroup->hwif = hwif->next = hwif;
1029 hwgroup->handler = NULL;
1030 hwgroup->drive = NULL;
1032 init_timer(&hwgroup->timer);
1033 hwgroup->timer.function = &ide_timer_expiry;
1034 hwgroup->timer.data = (unsigned long) hwgroup;
1038 * Allocate the irq, if not already obtained for another hwif
1040 if (!match || match->irq != hwif->irq) {
1041 int sa = SA_INTERRUPT;
1042 #if defined(__mc68000__) || defined(CONFIG_APUS)
1044 #endif /* __mc68000__ || CONFIG_APUS */
1046 if (IDE_CHIPSET_IS_PCI(hwif->chipset)) {
1048 #ifndef CONFIG_IDEPCI_SHARE_IRQ
1050 #endif /* CONFIG_IDEPCI_SHARE_IRQ */
1053 if (hwif->io_ports[IDE_CONTROL_OFFSET])
1055 hwif->OUTB(0x08, hwif->io_ports[IDE_CONTROL_OFFSET]);
1057 if (request_irq(hwif->irq,&ide_intr,sa,hwif->name,hwgroup))
1062 * Link any new drives into the hwgroup, allocate
1063 * the block device queue and initialize the drive.
1064 * Note that ide_init_drive sends commands to the new
1067 for (index = 0; index < MAX_DRIVES; ++index) {
1068 ide_drive_t *drive = &hwif->drives[index];
1069 if (!drive->present)
1071 ide_init_queue(drive);
1072 spin_lock_irq(&ide_lock);
1073 if (!hwgroup->drive) {
1074 /* first drive for hwgroup. */
1075 drive->next = drive;
1076 hwgroup->drive = drive;
1077 hwgroup->hwif = HWIF(hwgroup->drive);
1079 drive->next = hwgroup->drive->next;
1080 hwgroup->drive->next = drive;
1082 spin_unlock_irq(&ide_lock);
1083 ide_init_drive(drive);
1086 #if !defined(__mc68000__) && !defined(CONFIG_APUS) && !defined(__sparc__)
1087 printk("%s at 0x%03lx-0x%03lx,0x%03lx on irq %d", hwif->name,
1088 hwif->io_ports[IDE_DATA_OFFSET],
1089 hwif->io_ports[IDE_DATA_OFFSET]+7,
1090 hwif->io_ports[IDE_CONTROL_OFFSET], hwif->irq);
1091 #elif defined(__sparc__)
1092 printk("%s at 0x%03lx-0x%03lx,0x%03lx on irq %s", hwif->name,
1093 hwif->io_ports[IDE_DATA_OFFSET],
1094 hwif->io_ports[IDE_DATA_OFFSET]+7,
1095 hwif->io_ports[IDE_CONTROL_OFFSET], __irq_itoa(hwif->irq));
1097 printk("%s at 0x%08lx on irq %d", hwif->name,
1098 hwif->io_ports[IDE_DATA_OFFSET], hwif->irq);
1099 #endif /* __mc68000__ && CONFIG_APUS */
1101 printk(" (%sed with %s)",
1102 hwif->sharing_irq ? "shar" : "serializ", match->name);
1107 spin_lock_irq(&ide_lock);
1108 if (hwif->next == hwif) {
1110 BUG_ON(hwgroup->hwif != hwif);
1115 while (g->next != hwif)
1117 g->next = hwif->next;
1118 if (hwgroup->hwif == hwif) {
1120 printk(KERN_ERR "Duh. Uninitialized hwif listed as active hwif.\n");
1123 BUG_ON(hwgroup->hwif == hwif);
1125 spin_unlock_irq(&ide_lock);
1131 static int ata_lock(dev_t dev, void *data)
1133 /* FIXME: we want to pin hwif down */
1137 struct kobject *ata_probe(dev_t dev, int *part, void *data)
1139 ide_hwif_t *hwif = data;
1140 int unit = *part >> PARTN_BITS;
1141 ide_drive_t *drive = &hwif->drives[unit];
1142 if (!drive->present)
1144 if (!drive->driver) {
1145 if (drive->media == ide_disk)
1146 (void) request_module("ide-disk");
1148 (void) request_module("ide-scsi");
1149 if (drive->media == ide_cdrom || drive->media == ide_optical)
1150 (void) request_module("ide-cd");
1151 if (drive->media == ide_tape)
1152 (void) request_module("ide-tape");
1153 if (drive->media == ide_floppy)
1154 (void) request_module("ide-floppy");
1158 *part &= (1 << PARTN_BITS) - 1;
1159 return get_disk(drive->disk);
1162 static int alloc_disks(ide_hwif_t *hwif)
1165 struct gendisk *disks[MAX_DRIVES];
1167 for (unit = 0; unit < MAX_DRIVES; unit++) {
1168 disks[unit] = alloc_disk(1 << PARTN_BITS);
1172 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1173 ide_drive_t *drive = &hwif->drives[unit];
1174 struct gendisk *disk = disks[unit];
1175 disk->major = hwif->major;
1176 disk->first_minor = unit << PARTN_BITS;
1177 sprintf(disk->disk_name,"hd%c",'a'+hwif->index*MAX_DRIVES+unit);
1178 disk->fops = ide_fops;
1179 disk->private_data = drive;
1180 disk->queue = &drive->queue;
1185 printk(KERN_WARNING "(ide::init_gendisk) Out of memory\n");
1187 put_disk(disks[unit]);
1192 * init_gendisk() (as opposed to ide_geninit) is called for each major device,
1193 * after probing for drives, to allocate partition tables and other data
1194 * structures needed for the routines in genhd.c. ide_geninit() gets called
1195 * somewhat later, during the partition check.
1197 static void init_gendisk (ide_hwif_t *hwif)
1201 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1202 ide_drive_t * drive = &hwif->drives[unit];
1203 ide_add_generic_settings(drive);
1204 snprintf(drive->gendev.bus_id,BUS_ID_SIZE,"%u.%u",
1206 snprintf(drive->gendev.name,DEVICE_NAME_SIZE,
1208 drive->gendev.parent = &hwif->gendev;
1209 drive->gendev.bus = &ide_bus_type;
1210 drive->gendev.driver_data = drive;
1211 if (drive->present) {
1212 device_register(&drive->gendev);
1213 sprintf(drive->devfs_name, "ide/host%d/bus%d/target%d/lun%d",
1214 (hwif->channel && hwif->mate) ?
1215 hwif->mate->index : hwif->index,
1216 hwif->channel, unit, drive->lun);
1219 blk_register_region(MKDEV(hwif->major, 0), MAX_DRIVES << PARTN_BITS,
1220 THIS_MODULE, ata_probe, ata_lock, hwif);
1223 EXPORT_SYMBOL(init_gendisk);
1225 int hwif_init (ide_hwif_t *hwif)
1233 if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET])))
1235 printk("%s: DISABLED, NO IRQ\n", hwif->name);
1236 return (hwif->present = 0);
1239 #ifdef CONFIG_BLK_DEV_HD
1240 if (hwif->irq == HD_IRQ && hwif->io_ports[IDE_DATA_OFFSET] != HD_DATA) {
1241 printk("%s: CANNOT SHARE IRQ WITH OLD "
1242 "HARDDISK DRIVER (hd.c)\n", hwif->name);
1243 return (hwif->present = 0);
1245 #endif /* CONFIG_BLK_DEV_HD */
1247 /* we set it back to 1 if all is ok below */
1250 if (register_blkdev(hwif->major, hwif->name))
1253 if (alloc_disks(hwif) < 0)
1256 if (init_irq(hwif) == 0)
1259 old_irq = hwif->irq;
1261 * It failed to initialise. Find the default IRQ for
1262 * this port and try that.
1264 if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET]))) {
1265 printk("%s: Disabled unable to get IRQ %d.\n",
1266 hwif->name, old_irq);
1269 if (init_irq(hwif)) {
1270 printk("%s: probed IRQ %d and default IRQ %d failed.\n",
1271 hwif->name, old_irq, hwif->irq);
1274 printk("%s: probed IRQ %d failed, using default.\n",
1275 hwif->name, hwif->irq);
1279 hwif->present = 1; /* success */
1283 for (unit = 0; unit < MAX_DRIVES; unit++) {
1284 struct gendisk *disk = hwif->drives[unit].disk;
1285 hwif->drives[unit].disk = NULL;
1289 unregister_blkdev(hwif->major, hwif->name);
1293 EXPORT_SYMBOL(hwif_init);
1295 void export_ide_init_queue (ide_drive_t *drive)
1297 ide_init_queue(drive);
1298 ide_init_drive(drive);
1301 EXPORT_SYMBOL(export_ide_init_queue);
1303 u8 export_probe_for_drive (ide_drive_t *drive)
1305 return probe_for_drive(drive);
1308 EXPORT_SYMBOL(export_probe_for_drive);
1310 int ideprobe_init (void);
1311 static ide_module_t ideprobe_module = {
1317 int ideprobe_init (void)
1320 int probe[MAX_HWIFS];
1323 memset(probe, 0, MAX_HWIFS * sizeof(int));
1324 for (index = 0; index < MAX_HWIFS; ++index)
1325 probe[index] = !ide_hwifs[index].present;
1328 * Probe for drives in the usual way.. CMOS/BIOS, then poke at ports
1330 for (index = 0; index < MAX_HWIFS; ++index)
1332 probe_hwif(&ide_hwifs[index]);
1333 for (index = 0; index < MAX_HWIFS; ++index)
1335 hwif_init(&ide_hwifs[index]);
1336 for (index = 0; index < MAX_HWIFS; ++index) {
1338 ide_hwif_t *hwif = &ide_hwifs[index];
1342 for (unit = 0; unit < MAX_DRIVES; ++unit)
1343 if (hwif->drives[unit].present)
1344 ata_attach(&hwif->drives[unit]);
1348 ide_probe = &ideprobe_module;
1354 int init_module (void)
1358 for (index = 0; index < MAX_HWIFS; ++index)
1359 ide_unregister(index);
1361 create_proc_ide_interfaces();
1365 void cleanup_module (void)
1369 MODULE_LICENSE("GPL");