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>
50 #include <linux/scatterlist.h>
52 #include <asm/byteorder.h>
54 #include <asm/uaccess.h>
58 * generic_id - add a generic drive id
59 * @drive: drive to make an ID block for
61 * Add a fake id field to the drive we are passed. This allows
62 * use to skip a ton of NULL checks (which people always miss)
63 * and make drive properties unconditional outside of this file
66 static void generic_id(ide_drive_t *drive)
68 drive->id->cyls = drive->cyl;
69 drive->id->heads = drive->head;
70 drive->id->sectors = drive->sect;
71 drive->id->cur_cyls = drive->cyl;
72 drive->id->cur_heads = drive->head;
73 drive->id->cur_sectors = drive->sect;
76 static void ide_disk_init_chs(ide_drive_t *drive)
78 struct hd_driveid *id = drive->id;
80 /* Extract geometry if we did not already have one for the drive */
81 if (!drive->cyl || !drive->head || !drive->sect) {
82 drive->cyl = drive->bios_cyl = id->cyls;
83 drive->head = drive->bios_head = id->heads;
84 drive->sect = drive->bios_sect = id->sectors;
87 /* Handle logical geometry translation by the drive */
88 if ((id->field_valid & 1) && id->cur_cyls &&
89 id->cur_heads && (id->cur_heads <= 16) && id->cur_sectors) {
90 drive->cyl = id->cur_cyls;
91 drive->head = id->cur_heads;
92 drive->sect = id->cur_sectors;
95 /* Use physical geometry if what we have still makes no sense */
96 if (drive->head > 16 && id->heads && id->heads <= 16) {
97 drive->cyl = id->cyls;
98 drive->head = id->heads;
99 drive->sect = id->sectors;
103 static void ide_disk_init_mult_count(ide_drive_t *drive)
105 struct hd_driveid *id = drive->id;
107 drive->mult_count = 0;
108 if (id->max_multsect) {
109 #ifdef CONFIG_IDEDISK_MULTI_MODE
110 id->multsect = ((id->max_multsect/2) > 1) ? id->max_multsect : 0;
111 id->multsect_valid = id->multsect ? 1 : 0;
112 drive->mult_req = id->multsect_valid ? id->max_multsect : INITIAL_MULT_COUNT;
113 drive->special.b.set_multmode = drive->mult_req ? 1 : 0;
114 #else /* original, pre IDE-NFG, per request of AC */
115 drive->mult_req = INITIAL_MULT_COUNT;
116 if (drive->mult_req > id->max_multsect)
117 drive->mult_req = id->max_multsect;
118 if (drive->mult_req || ((id->multsect_valid & 1) && id->multsect))
119 drive->special.b.set_multmode = 1;
125 * do_identify - identify a drive
126 * @drive: drive to identify
129 * Called when we have issued a drive identify command to
130 * read and parse the results. This function is run with
131 * interrupts disabled.
134 static inline void do_identify (ide_drive_t *drive, u8 cmd)
136 ide_hwif_t *hwif = HWIF(drive);
138 struct hd_driveid *id;
141 /* read 512 bytes of id info */
142 hwif->ata_input_data(drive, id, SECTOR_WORDS);
148 #if defined (CONFIG_SCSI_EATA_PIO) || defined (CONFIG_SCSI_EATA)
150 * EATA SCSI controllers do a hardware ATA emulation:
151 * Ignore them if there is a driver for them available.
153 if ((id->model[0] == 'P' && id->model[1] == 'M') ||
154 (id->model[0] == 'S' && id->model[1] == 'K')) {
155 printk("%s: EATA SCSI HBA %.10s\n", drive->name, id->model);
158 #endif /* CONFIG_SCSI_EATA || CONFIG_SCSI_EATA_PIO */
161 * WIN_IDENTIFY returns little-endian info,
162 * WIN_PIDENTIFY *usually* returns little-endian info.
164 if (cmd == WIN_PIDENTIFY) {
165 if ((id->model[0] == 'N' && id->model[1] == 'E') /* NEC */
166 || (id->model[0] == 'F' && id->model[1] == 'X') /* Mitsumi */
167 || (id->model[0] == 'P' && id->model[1] == 'i'))/* Pioneer */
168 /* Vertos drives may still be weird */
171 ide_fixstring(id->model, sizeof(id->model), bswap);
172 ide_fixstring(id->fw_rev, sizeof(id->fw_rev), bswap);
173 ide_fixstring(id->serial_no, sizeof(id->serial_no), bswap);
175 /* we depend on this a lot! */
176 id->model[sizeof(id->model)-1] = '\0';
178 if (strstr(id->model, "E X A B Y T E N E S T"))
181 printk("%s: %s, ", drive->name, id->model);
186 * Check for an ATAPI device
188 if (cmd == WIN_PIDENTIFY) {
189 u8 type = (id->config >> 8) & 0x1f;
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);
232 /* an ATAPI device ignores DRDY */
233 drive->ready_stat = 0;
238 * Not an ATAPI device: looks like a "regular" hard disk
242 * 0x848a = CompactFlash device
243 * These are *not* removable in Linux definition of the term
246 if ((id->config != 0x848a) && (id->config & (1<<7)))
247 drive->removable = 1;
249 drive->media = ide_disk;
250 printk("%s DISK drive\n", (id->config == 0x848a) ? "CFA" : "ATA" );
261 * actual_try_to_identify - send ata/atapi identify
262 * @drive: drive to identify
263 * @cmd: command to use
265 * try_to_identify() sends an ATA(PI) IDENTIFY request to a drive
266 * and waits for a response. It also monitors irqs while this is
267 * happening, in hope of automatically determining which one is
268 * being used by the interface.
270 * Returns: 0 device was identified
271 * 1 device timed-out (no response to identify request)
272 * 2 device aborted the command (refused to identify itself)
275 static int actual_try_to_identify (ide_drive_t *drive, u8 cmd)
277 ide_hwif_t *hwif = HWIF(drive);
279 unsigned long hd_status;
280 unsigned long timeout;
283 /* take a deep breath */
286 if (IDE_CONTROL_REG) {
287 a = hwif->INB(IDE_ALTSTATUS_REG);
288 s = hwif->INB(IDE_STATUS_REG);
289 if ((a ^ s) & ~INDEX_STAT) {
290 printk(KERN_INFO "%s: probing with STATUS(0x%02x) instead of "
291 "ALTSTATUS(0x%02x)\n", drive->name, s, a);
292 /* ancient Seagate drives, broken interfaces */
293 hd_status = IDE_STATUS_REG;
295 /* use non-intrusive polling */
296 hd_status = IDE_ALTSTATUS_REG;
299 hd_status = IDE_STATUS_REG;
301 /* set features register for atapi
302 * identify command to be sure of reply
304 if ((cmd == WIN_PIDENTIFY))
305 /* disable dma & overlap */
306 hwif->OUTB(0, IDE_FEATURE_REG);
308 /* ask drive for ID */
309 hwif->OUTB(cmd, IDE_COMMAND_REG);
311 timeout = ((cmd == WIN_IDENTIFY) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
314 if (time_after(jiffies, timeout)) {
315 /* drive timed-out */
318 /* give drive a breather */
320 } while ((hwif->INB(hd_status)) & BUSY_STAT);
322 /* wait for IRQ and DRQ_STAT */
324 if (OK_STAT((hwif->INB(IDE_STATUS_REG)), DRQ_STAT, BAD_R_STAT)) {
327 /* local CPU only; some systems need this */
328 local_irq_save(flags);
329 /* drive returned ID */
330 do_identify(drive, cmd);
331 /* drive responded with ID */
333 /* clear drive IRQ */
334 (void) hwif->INB(IDE_STATUS_REG);
335 local_irq_restore(flags);
337 /* drive refused ID */
344 * try_to_identify - try to identify a drive
345 * @drive: drive to probe
346 * @cmd: command to use
348 * Issue the identify command and then do IRQ probing to
349 * complete the identification when needed by finding the
350 * IRQ the drive is attached to
353 static int try_to_identify (ide_drive_t *drive, u8 cmd)
355 ide_hwif_t *hwif = HWIF(drive);
358 unsigned long cookie = 0;
361 * Disable device irq unless we need to
362 * probe for it. Otherwise we'll get spurious
363 * interrupts during the identify-phase that
364 * the irq handler isn't expecting.
366 if (IDE_CONTROL_REG) {
367 u8 ctl = drive->ctl | 2;
370 cookie = probe_irq_on();
371 /* enable device irq */
374 hwif->OUTB(ctl, IDE_CONTROL_REG);
377 retval = actual_try_to_identify(drive, cmd);
381 /* mask device irq */
382 hwif->OUTB(drive->ctl|2, IDE_CONTROL_REG);
383 /* clear drive IRQ */
384 (void) hwif->INB(IDE_STATUS_REG);
386 irq = probe_irq_off(cookie);
391 /* Mmmm.. multiple IRQs..
392 * don't know which was ours
394 printk("%s: IRQ probe failed (0x%lx)\n",
395 drive->name, cookie);
404 * do_probe - probe an IDE device
405 * @drive: drive to probe
406 * @cmd: command to use
408 * do_probe() has the difficult job of finding a drive if it exists,
409 * without getting hung up if it doesn't exist, without trampling on
410 * ethernet cards, and without leaving any IRQs dangling to haunt us later.
412 * If a drive is "known" to exist (from CMOS or kernel parameters),
413 * but does not respond right away, the probe will "hang in there"
414 * for the maximum wait time (about 30 seconds), otherwise it will
415 * exit much more quickly.
417 * Returns: 0 device was identified
418 * 1 device timed-out (no response to identify request)
419 * 2 device aborted the command (refused to identify itself)
420 * 3 bad status from device (possible for ATAPI drives)
421 * 4 probe was not attempted because failure was obvious
424 static int do_probe (ide_drive_t *drive, u8 cmd)
427 ide_hwif_t *hwif = HWIF(drive);
429 if (drive->present) {
430 /* avoid waiting for inappropriate probes */
431 if ((drive->media != ide_disk) && (cmd == WIN_IDENTIFY))
435 printk("probing for %s: present=%d, media=%d, probetype=%s\n",
436 drive->name, drive->present, drive->media,
437 (cmd == WIN_IDENTIFY) ? "ATA" : "ATAPI");
440 /* needed for some systems
441 * (e.g. crw9624 as drive0 with disk as slave)
446 if (hwif->INB(IDE_SELECT_REG) != drive->select.all && !drive->present) {
447 if (drive->select.b.unit != 0) {
448 /* exit with drive0 selected */
449 SELECT_DRIVE(&hwif->drives[0]);
450 /* allow BUSY_STAT to assert & clear */
453 /* no i/f present: mmm.. this should be a 4 -ml */
457 if (OK_STAT((hwif->INB(IDE_STATUS_REG)), READY_STAT, BUSY_STAT) ||
458 drive->present || cmd == WIN_PIDENTIFY) {
459 /* send cmd and wait */
460 if ((rc = try_to_identify(drive, cmd))) {
461 /* failed: try again */
462 rc = try_to_identify(drive,cmd);
464 if (hwif->INB(IDE_STATUS_REG) == (BUSY_STAT|READY_STAT))
467 if ((rc == 1 && cmd == WIN_PIDENTIFY) &&
468 ((drive->autotune == IDE_TUNE_DEFAULT) ||
469 (drive->autotune == IDE_TUNE_AUTO))) {
470 unsigned long timeout;
471 printk("%s: no response (status = 0x%02x), "
472 "resetting drive\n", drive->name,
473 hwif->INB(IDE_STATUS_REG));
475 hwif->OUTB(drive->select.all, IDE_SELECT_REG);
477 hwif->OUTB(WIN_SRST, IDE_COMMAND_REG);
479 while (((hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) &&
480 time_before(jiffies, timeout + WAIT_WORSTCASE))
482 rc = try_to_identify(drive, cmd);
485 printk("%s: no response (status = 0x%02x)\n",
486 drive->name, hwif->INB(IDE_STATUS_REG));
487 /* ensure drive irq is clear */
488 (void) hwif->INB(IDE_STATUS_REG);
490 /* not present or maybe ATAPI */
493 if (drive->select.b.unit != 0) {
494 /* exit with drive0 selected */
495 SELECT_DRIVE(&hwif->drives[0]);
497 /* ensure drive irq is clear */
498 (void) hwif->INB(IDE_STATUS_REG);
506 static void enable_nest (ide_drive_t *drive)
508 ide_hwif_t *hwif = HWIF(drive);
509 unsigned long timeout;
511 printk("%s: enabling %s -- ", hwif->name, drive->id->model);
514 hwif->OUTB(EXABYTE_ENABLE_NEST, IDE_COMMAND_REG);
515 timeout = jiffies + WAIT_WORSTCASE;
517 if (time_after(jiffies, timeout)) {
518 printk("failed (timeout)\n");
522 } while ((hwif->INB(IDE_STATUS_REG)) & BUSY_STAT);
526 if (!OK_STAT((hwif->INB(IDE_STATUS_REG)), 0, BAD_STAT)) {
527 printk("failed (status = 0x%02x)\n", hwif->INB(IDE_STATUS_REG));
532 /* if !(success||timed-out) */
533 if (do_probe(drive, WIN_IDENTIFY) >= 2) {
534 /* look for ATAPI device */
535 (void) do_probe(drive, WIN_PIDENTIFY);
540 * probe_for_drives - upper level drive probe
541 * @drive: drive to probe for
543 * probe_for_drive() tests for existence of a given drive using do_probe()
544 * and presents things to the user as needed.
546 * Returns: 0 no device was found
547 * 1 device was found (note: drive->present might
551 static inline u8 probe_for_drive (ide_drive_t *drive)
554 * In order to keep things simple we have an id
555 * block for all drives at all times. If the device
556 * is pre ATA or refuses ATA/ATAPI identify we
557 * will add faked data to this.
559 * Also note that 0 everywhere means "can't do X"
562 drive->id = kzalloc(SECTOR_WORDS *4, GFP_KERNEL);
564 if(drive->id == NULL)
566 printk(KERN_ERR "ide: out of memory for id data.\n");
569 strcpy(drive->id->model, "UNKNOWN");
574 /* if !(success||timed-out) */
575 if (do_probe(drive, WIN_IDENTIFY) >= 2) {
576 /* look for ATAPI device */
577 (void) do_probe(drive, WIN_PIDENTIFY);
580 /* drive not found */
582 if (strstr(drive->id->model, "E X A B Y T E N E S T"))
585 /* identification failed? */
586 if (!drive->id_read) {
587 if (drive->media == ide_disk) {
588 printk(KERN_INFO "%s: non-IDE drive, CHS=%d/%d/%d\n",
589 drive->name, drive->cyl,
590 drive->head, drive->sect);
591 } else if (drive->media == ide_cdrom) {
592 printk(KERN_INFO "%s: ATAPI cdrom (?)\n", drive->name);
595 printk(KERN_WARNING "%s: Unknown device on bus refused identification. Ignoring.\n", drive->name);
599 /* drive was found */
603 /* The drive wasn't being helpful. Add generic info only */
604 if (drive->id_read == 0) {
609 if (drive->media == ide_disk) {
610 ide_disk_init_chs(drive);
611 ide_disk_init_mult_count(drive);
614 return drive->present;
617 static void hwif_release_dev (struct device *dev)
619 ide_hwif_t *hwif = container_of(dev, ide_hwif_t, gendev);
621 complete(&hwif->gendev_rel_comp);
624 static void hwif_register (ide_hwif_t *hwif)
628 /* register with global device tree */
629 strlcpy(hwif->gendev.bus_id,hwif->name,BUS_ID_SIZE);
630 hwif->gendev.driver_data = hwif;
631 if (hwif->gendev.parent == NULL) {
633 hwif->gendev.parent = &hwif->pci_dev->dev;
635 /* Would like to do = &device_legacy */
636 hwif->gendev.parent = NULL;
638 hwif->gendev.release = hwif_release_dev;
639 ret = device_register(&hwif->gendev);
641 printk(KERN_WARNING "IDE: %s: device_register error: %d\n",
645 static int wait_hwif_ready(ide_hwif_t *hwif)
649 printk(KERN_DEBUG "Probing IDE interface %s...\n", hwif->name);
651 /* Let HW settle down a bit from whatever init state we
655 /* Wait for BSY bit to go away, spec timeout is 30 seconds,
656 * I know of at least one disk who takes 31 seconds, I use 35
659 rc = ide_wait_not_busy(hwif, 35000);
663 /* Now make sure both master & slave are ready */
664 for (unit = 0; unit < MAX_DRIVES; unit++) {
665 ide_drive_t *drive = &hwif->drives[unit];
667 /* Ignore disks that we will not probe for later. */
668 if (!drive->noprobe || drive->present) {
671 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
673 rc = ide_wait_not_busy(hwif, 35000);
677 printk(KERN_DEBUG "%s: ide_wait_not_busy() skipped\n",
681 /* Exit function with master reselected (let's be sane) */
683 SELECT_DRIVE(&hwif->drives[0]);
689 * ide_undecoded_slave - look for bad CF adapters
692 * Analyse the drives on the interface and attempt to decide if we
693 * have the same drive viewed twice. This occurs with crap CF adapters
694 * and PCMCIA sometimes.
697 void ide_undecoded_slave(ide_hwif_t *hwif)
699 ide_drive_t *drive0 = &hwif->drives[0];
700 ide_drive_t *drive1 = &hwif->drives[1];
702 if (drive0->present == 0 || drive1->present == 0)
705 /* If the models don't match they are not the same product */
706 if (strcmp(drive0->id->model, drive1->id->model))
709 /* Serial numbers do not match */
710 if (strncmp(drive0->id->serial_no, drive1->id->serial_no, 20))
713 /* No serial number, thankfully very rare for CF */
714 if (drive0->id->serial_no[0] == 0)
717 /* Appears to be an IDE flash adapter with decode bugs */
718 printk(KERN_WARNING "ide-probe: ignoring undecoded slave\n");
723 EXPORT_SYMBOL_GPL(ide_undecoded_slave);
726 * This routine only knows how to look for drive units 0 and 1
727 * on an interface, so any setting of MAX_DRIVES > 2 won't work here.
729 static void probe_hwif(ide_hwif_t *hwif)
738 if ((hwif->chipset != ide_4drives || !hwif->mate || !hwif->mate->present) &&
739 (ide_hwif_request_regions(hwif))) {
741 for (unit = 0; unit < MAX_DRIVES; ++unit) {
742 ide_drive_t *drive = &hwif->drives[unit];
743 if (drive->present) {
745 printk(KERN_ERR "%s: ERROR, PORTS ALREADY IN USE\n",
751 printk(KERN_ERR "%s: ports already in use, skipping probe\n",
757 * We must always disable IRQ, as probe_for_drive will assert IRQ, but
758 * we'll install our IRQ driver much later...
762 disable_irq(hwif->irq);
764 local_irq_set(flags);
766 /* This is needed on some PPCs and a bunch of BIOS-less embedded
767 * platforms. Typical cases are:
769 * - The firmware hard reset the disk before booting the kernel,
770 * the drive is still doing it's poweron-reset sequence, that
771 * can take up to 30 seconds
772 * - The firmware does nothing (or no firmware), the device is
773 * still in POST state (same as above actually).
774 * - Some CD/DVD/Writer combo drives tend to drive the bus during
775 * their reset sequence even when they are non-selected slave
776 * devices, thus preventing discovery of the main HD
778 * Doing this wait-for-busy should not harm any existing configuration
779 * (at least things won't be worse than what current code does, that
780 * is blindly go & talk to the drive) and fix some issues like the
785 if (wait_hwif_ready(hwif) == -EBUSY)
786 printk(KERN_DEBUG "%s: Wait for ready failed before probe !\n", hwif->name);
789 * Need to probe slave device first to make it release PDIAG-.
791 for (unit = MAX_DRIVES - 1; unit >= 0; unit--) {
792 ide_drive_t *drive = &hwif->drives[unit];
793 drive->dn = (hwif->channel ? 2 : 0) + unit;
794 (void) probe_for_drive(drive);
795 if (drive->present && !hwif->present) {
797 if (hwif->chipset != ide_4drives ||
799 !hwif->mate->present) {
804 if (hwif->io_ports[IDE_CONTROL_OFFSET] && hwif->reset) {
805 unsigned long timeout = jiffies + WAIT_WORSTCASE;
808 printk(KERN_WARNING "%s: reset\n", hwif->name);
809 hwif->OUTB(12, hwif->io_ports[IDE_CONTROL_OFFSET]);
811 hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
814 stat = hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
815 } while ((stat & BUSY_STAT) && time_after(timeout, jiffies));
818 local_irq_restore(flags);
820 * Use cached IRQ number. It might be (and is...) changed by probe
826 if (!hwif->present) {
827 ide_hwif_release_regions(hwif);
834 for (unit = 0; unit < MAX_DRIVES; ++unit) {
835 ide_drive_t *drive = &hwif->drives[unit];
837 if (drive->present) {
838 if (drive->autotune == IDE_TUNE_AUTO)
839 ide_set_max_pio(drive);
841 if (drive->autotune != IDE_TUNE_DEFAULT &&
842 drive->autotune != IDE_TUNE_AUTO)
847 if (hwif->ide_dma_on) {
849 * Force DMAing for the beginning of the check.
850 * Some chipsets appear to do interesting
851 * things, if not checked and cleared.
854 hwif->dma_off_quietly(drive);
860 for (unit = 0; unit < MAX_DRIVES; ++unit) {
861 ide_drive_t *drive = &hwif->drives[unit];
863 if (hwif->no_io_32bit)
864 drive->no_io_32bit = 1;
866 drive->no_io_32bit = drive->id->dword_io ? 1 : 0;
870 static int hwif_init(ide_hwif_t *hwif);
871 static void hwif_register_devices(ide_hwif_t *hwif);
873 static int probe_hwif_init(ide_hwif_t *hwif)
877 if (!hwif_init(hwif)) {
878 printk(KERN_INFO "%s: failed to initialize IDE interface\n",
884 hwif_register_devices(hwif);
891 * save_match() is used to simplify logic in init_irq() below.
893 * A loophole here is that we may not know about a particular
894 * hwif's irq until after that hwif is actually probed/initialized..
895 * This could be a problem for the case where an hwif is on a
896 * dual interface that requires serialization (eg. cmd640) and another
897 * hwif using one of the same irqs is initialized beforehand.
899 * This routine detects and reports such situations, but does not fix them.
901 static void save_match(ide_hwif_t *hwif, ide_hwif_t *new, ide_hwif_t **match)
903 ide_hwif_t *m = *match;
905 if (m && m->hwgroup && m->hwgroup != new->hwgroup) {
908 printk("%s: potential irq problem with %s and %s\n",
909 hwif->name, new->name, m->name);
911 if (!m || m->irq != hwif->irq) /* don't undo a prior perfect match */
914 #endif /* MAX_HWIFS > 1 */
919 static int ide_init_queue(ide_drive_t *drive)
921 struct request_queue *q;
922 ide_hwif_t *hwif = HWIF(drive);
923 int max_sectors = 256;
924 int max_sg_entries = PRD_ENTRIES;
927 * Our default set up assumes the normal IDE case,
928 * that is 64K segmenting, standard PRD setup
929 * and LBA28. Some drivers then impose their own
930 * limits and LBA48 we could raise it but as yet
934 q = blk_init_queue_node(do_ide_request, &ide_lock, hwif_to_node(hwif));
938 q->queuedata = drive;
939 blk_queue_segment_boundary(q, 0xffff);
942 if ((hwif->host_flags & IDE_HFLAG_NO_LBA48) ||
943 (hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA))
946 hwif->rqsize = 65536;
948 if (hwif->rqsize < max_sectors)
949 max_sectors = hwif->rqsize;
950 blk_queue_max_sectors(q, max_sectors);
953 /* When we have an IOMMU, we may have a problem where pci_map_sg()
954 * creates segments that don't completely match our boundary
955 * requirements and thus need to be broken up again. Because it
956 * doesn't align properly either, we may actually have to break up
957 * to more segments than what was we got in the first place, a max
958 * worst case is twice as many.
959 * This will be fixed once we teach pci_map_sg() about our boundary
960 * requirements, hopefully soon. *FIXME*
962 if (!PCI_DMA_BUS_IS_PHYS)
963 max_sg_entries >>= 1;
964 #endif /* CONFIG_PCI */
966 blk_queue_max_hw_segments(q, max_sg_entries);
967 blk_queue_max_phys_segments(q, max_sg_entries);
969 /* assign drive queue */
972 /* needs drive->queue to be set */
973 ide_toggle_bounce(drive, 1);
979 * This routine sets up the irq for an ide interface, and creates a new
980 * hwgroup for the irq/hwif if none was previously assigned.
982 * Much of the code is for correctly detecting/handling irq sharing
983 * and irq serialization situations. This is somewhat complex because
984 * it handles static as well as dynamic (PCMCIA) IDE interfaces.
986 * The IRQF_DISABLED in sa_flags means ide_intr() is always entered with
987 * interrupts completely disabled. This can be bad for interrupt latency,
988 * but anything else has led to problems on some machines. We re-enable
989 * interrupts as much as we can safely do in most places.
991 static int init_irq (ide_hwif_t *hwif)
994 ide_hwgroup_t *hwgroup;
995 ide_hwif_t *match = NULL;
998 BUG_ON(in_interrupt());
999 BUG_ON(irqs_disabled());
1000 BUG_ON(hwif == NULL);
1002 mutex_lock(&ide_cfg_mtx);
1003 hwif->hwgroup = NULL;
1006 * Group up with any other hwifs that share our irq(s).
1008 for (index = 0; index < MAX_HWIFS; index++) {
1009 ide_hwif_t *h = &ide_hwifs[index];
1010 if (h->hwgroup) { /* scan only initialized hwif's */
1011 if (hwif->irq == h->irq) {
1012 hwif->sharing_irq = h->sharing_irq = 1;
1013 if (hwif->chipset != ide_pci ||
1014 h->chipset != ide_pci) {
1015 save_match(hwif, h, &match);
1018 if (hwif->serialized) {
1019 if (hwif->mate && hwif->mate->irq == h->irq)
1020 save_match(hwif, h, &match);
1022 if (h->serialized) {
1023 if (h->mate && hwif->irq == h->mate->irq)
1024 save_match(hwif, h, &match);
1028 #endif /* MAX_HWIFS > 1 */
1030 * If we are still without a hwgroup, then form a new one
1033 hwgroup = match->hwgroup;
1034 hwif->hwgroup = hwgroup;
1036 * Link us into the hwgroup.
1037 * This must be done early, do ensure that unexpected_intr
1038 * can find the hwif and prevent irq storms.
1039 * No drives are attached to the new hwif, choose_drive
1040 * can't do anything stupid (yet).
1041 * Add ourself as the 2nd entry to the hwgroup->hwif
1042 * linked list, the first entry is the hwif that owns
1043 * hwgroup->handler - do not change that.
1045 spin_lock_irq(&ide_lock);
1046 hwif->next = hwgroup->hwif->next;
1047 hwgroup->hwif->next = hwif;
1048 spin_unlock_irq(&ide_lock);
1050 hwgroup = kmalloc_node(sizeof(ide_hwgroup_t),
1051 GFP_KERNEL | __GFP_ZERO,
1052 hwif_to_node(hwif->drives[0].hwif));
1056 hwif->hwgroup = hwgroup;
1058 hwgroup->hwif = hwif->next = hwif;
1060 hwgroup->handler = NULL;
1061 hwgroup->drive = NULL;
1063 init_timer(&hwgroup->timer);
1064 hwgroup->timer.function = &ide_timer_expiry;
1065 hwgroup->timer.data = (unsigned long) hwgroup;
1069 * Allocate the irq, if not already obtained for another hwif
1071 if (!match || match->irq != hwif->irq) {
1072 int sa = IRQF_DISABLED;
1073 #if defined(__mc68000__) || defined(CONFIG_APUS)
1075 #endif /* __mc68000__ || CONFIG_APUS */
1077 if (IDE_CHIPSET_IS_PCI(hwif->chipset)) {
1079 #ifndef CONFIG_IDEPCI_SHARE_IRQ
1080 sa |= IRQF_DISABLED;
1081 #endif /* CONFIG_IDEPCI_SHARE_IRQ */
1084 if (hwif->io_ports[IDE_CONTROL_OFFSET])
1086 hwif->OUTB(0x08, hwif->io_ports[IDE_CONTROL_OFFSET]);
1088 if (request_irq(hwif->irq,&ide_intr,sa,hwif->name,hwgroup))
1093 * For any present drive:
1094 * - allocate the block device queue
1095 * - link drive into the hwgroup
1097 for (index = 0; index < MAX_DRIVES; ++index) {
1098 ide_drive_t *drive = &hwif->drives[index];
1099 if (!drive->present)
1101 if (ide_init_queue(drive)) {
1102 printk(KERN_ERR "ide: failed to init %s\n",drive->name);
1105 spin_lock_irq(&ide_lock);
1106 if (!hwgroup->drive) {
1107 /* first drive for hwgroup. */
1108 drive->next = drive;
1109 hwgroup->drive = drive;
1110 hwgroup->hwif = HWIF(hwgroup->drive);
1112 drive->next = hwgroup->drive->next;
1113 hwgroup->drive->next = drive;
1115 spin_unlock_irq(&ide_lock);
1118 #if !defined(__mc68000__) && !defined(CONFIG_APUS)
1119 printk("%s at 0x%03lx-0x%03lx,0x%03lx on irq %d", hwif->name,
1120 hwif->io_ports[IDE_DATA_OFFSET],
1121 hwif->io_ports[IDE_DATA_OFFSET]+7,
1122 hwif->io_ports[IDE_CONTROL_OFFSET], hwif->irq);
1124 printk("%s at 0x%08lx on irq %d", hwif->name,
1125 hwif->io_ports[IDE_DATA_OFFSET], hwif->irq);
1126 #endif /* __mc68000__ && CONFIG_APUS */
1128 printk(" (%sed with %s)",
1129 hwif->sharing_irq ? "shar" : "serializ", match->name);
1131 mutex_unlock(&ide_cfg_mtx);
1134 spin_lock_irq(&ide_lock);
1135 if (hwif->next == hwif) {
1137 BUG_ON(hwgroup->hwif != hwif);
1142 while (g->next != hwif)
1144 g->next = hwif->next;
1145 if (hwgroup->hwif == hwif) {
1147 printk(KERN_ERR "Duh. Uninitialized hwif listed as active hwif.\n");
1150 BUG_ON(hwgroup->hwif == hwif);
1152 spin_unlock_irq(&ide_lock);
1154 mutex_unlock(&ide_cfg_mtx);
1158 static int ata_lock(dev_t dev, void *data)
1160 /* FIXME: we want to pin hwif down */
1164 static struct kobject *ata_probe(dev_t dev, int *part, void *data)
1166 ide_hwif_t *hwif = data;
1167 int unit = *part >> PARTN_BITS;
1168 ide_drive_t *drive = &hwif->drives[unit];
1169 if (!drive->present)
1172 if (drive->media == ide_disk)
1173 request_module("ide-disk");
1175 request_module("ide-scsi");
1176 if (drive->media == ide_cdrom || drive->media == ide_optical)
1177 request_module("ide-cd");
1178 if (drive->media == ide_tape)
1179 request_module("ide-tape");
1180 if (drive->media == ide_floppy)
1181 request_module("ide-floppy");
1186 static struct kobject *exact_match(dev_t dev, int *part, void *data)
1188 struct gendisk *p = data;
1189 *part &= (1 << PARTN_BITS) - 1;
1193 static int exact_lock(dev_t dev, void *data)
1195 struct gendisk *p = data;
1202 void ide_register_region(struct gendisk *disk)
1204 blk_register_region(MKDEV(disk->major, disk->first_minor),
1205 disk->minors, NULL, exact_match, exact_lock, disk);
1208 EXPORT_SYMBOL_GPL(ide_register_region);
1210 void ide_unregister_region(struct gendisk *disk)
1212 blk_unregister_region(MKDEV(disk->major, disk->first_minor),
1216 EXPORT_SYMBOL_GPL(ide_unregister_region);
1218 void ide_init_disk(struct gendisk *disk, ide_drive_t *drive)
1220 ide_hwif_t *hwif = drive->hwif;
1221 unsigned int unit = (drive->select.all >> 4) & 1;
1223 disk->major = hwif->major;
1224 disk->first_minor = unit << PARTN_BITS;
1225 sprintf(disk->disk_name, "hd%c", 'a' + hwif->index * MAX_DRIVES + unit);
1226 disk->queue = drive->queue;
1229 EXPORT_SYMBOL_GPL(ide_init_disk);
1231 static void ide_remove_drive_from_hwgroup(ide_drive_t *drive)
1233 ide_hwgroup_t *hwgroup = drive->hwif->hwgroup;
1235 if (drive == drive->next) {
1236 /* special case: last drive from hwgroup. */
1237 BUG_ON(hwgroup->drive != drive);
1238 hwgroup->drive = NULL;
1242 walk = hwgroup->drive;
1243 while (walk->next != drive)
1245 walk->next = drive->next;
1246 if (hwgroup->drive == drive) {
1247 hwgroup->drive = drive->next;
1248 hwgroup->hwif = hwgroup->drive->hwif;
1251 BUG_ON(hwgroup->drive == drive);
1254 static void drive_release_dev (struct device *dev)
1256 ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
1258 spin_lock_irq(&ide_lock);
1259 ide_remove_drive_from_hwgroup(drive);
1263 /* Messed up locking ... */
1264 spin_unlock_irq(&ide_lock);
1265 blk_cleanup_queue(drive->queue);
1266 spin_lock_irq(&ide_lock);
1267 drive->queue = NULL;
1268 spin_unlock_irq(&ide_lock);
1270 complete(&drive->gendev_rel_comp);
1274 * init_gendisk() (as opposed to ide_geninit) is called for each major device,
1275 * after probing for drives, to allocate partition tables and other data
1276 * structures needed for the routines in genhd.c. ide_geninit() gets called
1277 * somewhat later, during the partition check.
1279 static void init_gendisk (ide_hwif_t *hwif)
1283 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1284 ide_drive_t * drive = &hwif->drives[unit];
1285 ide_add_generic_settings(drive);
1286 snprintf(drive->gendev.bus_id,BUS_ID_SIZE,"%u.%u",
1288 drive->gendev.parent = &hwif->gendev;
1289 drive->gendev.bus = &ide_bus_type;
1290 drive->gendev.driver_data = drive;
1291 drive->gendev.release = drive_release_dev;
1293 blk_register_region(MKDEV(hwif->major, 0), MAX_DRIVES << PARTN_BITS,
1294 THIS_MODULE, ata_probe, ata_lock, hwif);
1297 static int hwif_init(ide_hwif_t *hwif)
1301 /* Return success if no device is connected */
1306 if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET])))
1308 printk("%s: DISABLED, NO IRQ\n", hwif->name);
1309 return (hwif->present = 0);
1312 #ifdef CONFIG_BLK_DEV_HD
1313 if (hwif->irq == HD_IRQ && hwif->io_ports[IDE_DATA_OFFSET] != HD_DATA) {
1314 printk("%s: CANNOT SHARE IRQ WITH OLD "
1315 "HARDDISK DRIVER (hd.c)\n", hwif->name);
1316 return (hwif->present = 0);
1318 #endif /* CONFIG_BLK_DEV_HD */
1320 /* we set it back to 1 if all is ok below */
1323 if (register_blkdev(hwif->major, hwif->name))
1326 if (!hwif->sg_max_nents)
1327 hwif->sg_max_nents = PRD_ENTRIES;
1329 hwif->sg_table = kmalloc(sizeof(struct scatterlist)*hwif->sg_max_nents,
1331 if (!hwif->sg_table) {
1332 printk(KERN_ERR "%s: unable to allocate SG table.\n", hwif->name);
1336 sg_init_table(hwif->sg_table, hwif->sg_max_nents);
1338 if (init_irq(hwif) == 0)
1341 old_irq = hwif->irq;
1343 * It failed to initialise. Find the default IRQ for
1344 * this port and try that.
1346 if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET]))) {
1347 printk("%s: Disabled unable to get IRQ %d.\n",
1348 hwif->name, old_irq);
1351 if (init_irq(hwif)) {
1352 printk("%s: probed IRQ %d and default IRQ %d failed.\n",
1353 hwif->name, old_irq, hwif->irq);
1356 printk("%s: probed IRQ %d failed, using default.\n",
1357 hwif->name, hwif->irq);
1362 ide_acpi_init(hwif);
1364 hwif->present = 1; /* success */
1368 unregister_blkdev(hwif->major, hwif->name);
1372 static void hwif_register_devices(ide_hwif_t *hwif)
1376 for (i = 0; i < MAX_DRIVES; i++) {
1377 ide_drive_t *drive = &hwif->drives[i];
1379 if (drive->present) {
1380 int ret = device_register(&drive->gendev);
1383 printk(KERN_WARNING "IDE: %s: "
1384 "device_register error: %d\n",
1390 int ideprobe_init (void)
1393 int probe[MAX_HWIFS];
1395 memset(probe, 0, MAX_HWIFS * sizeof(int));
1396 for (index = 0; index < MAX_HWIFS; ++index)
1397 probe[index] = !ide_hwifs[index].present;
1399 for (index = 0; index < MAX_HWIFS; ++index)
1401 probe_hwif(&ide_hwifs[index]);
1402 for (index = 0; index < MAX_HWIFS; ++index)
1404 hwif_init(&ide_hwifs[index]);
1405 for (index = 0; index < MAX_HWIFS; ++index) {
1407 ide_hwif_t *hwif = &ide_hwifs[index];
1410 if (hwif->chipset == ide_unknown || hwif->chipset == ide_forced)
1411 hwif->chipset = ide_generic;
1412 hwif_register_devices(hwif);
1415 for (index = 0; index < MAX_HWIFS; ++index)
1417 ide_proc_register_port(&ide_hwifs[index]);
1421 EXPORT_SYMBOL_GPL(ideprobe_init);
1423 int ide_device_add(u8 idx[4])
1427 for (i = 0; i < 4; i++) {
1429 rc |= probe_hwif_init(&ide_hwifs[idx[i]]);
1432 for (i = 0; i < 4; i++) {
1434 ide_proc_register_port(&ide_hwifs[idx[i]]);
1440 EXPORT_SYMBOL_GPL(ide_device_add);