2 * Copyright (C) 1994-1998 Linus Torvalds & authors (see below)
6 * Mostly written by Mark Lord <mlord@pobox.com>
7 * and Gadi Oxman <gadio@netvision.net.il>
8 * and Andre Hedrick <andre@linux-ide.org>
10 * See linux/MAINTAINERS for address of current maintainer.
12 * This is the IDE probe module, as evolved from hd.c and ide.c.
14 * -- increase WAIT_PIDENTIFY to avoid CD-ROM locking at boot
18 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/string.h>
21 #include <linux/kernel.h>
22 #include <linux/timer.h>
24 #include <linux/interrupt.h>
25 #include <linux/major.h>
26 #include <linux/errno.h>
27 #include <linux/genhd.h>
28 #include <linux/slab.h>
29 #include <linux/delay.h>
30 #include <linux/ide.h>
31 #include <linux/spinlock.h>
32 #include <linux/kmod.h>
33 #include <linux/pci.h>
34 #include <linux/scatterlist.h>
36 #include <asm/byteorder.h>
38 #include <asm/uaccess.h>
42 * generic_id - add a generic drive id
43 * @drive: drive to make an ID block for
45 * Add a fake id field to the drive we are passed. This allows
46 * use to skip a ton of NULL checks (which people always miss)
47 * and make drive properties unconditional outside of this file
50 static void generic_id(ide_drive_t *drive)
52 drive->id->cyls = drive->cyl;
53 drive->id->heads = drive->head;
54 drive->id->sectors = drive->sect;
55 drive->id->cur_cyls = drive->cyl;
56 drive->id->cur_heads = drive->head;
57 drive->id->cur_sectors = drive->sect;
60 static void ide_disk_init_chs(ide_drive_t *drive)
62 struct hd_driveid *id = drive->id;
64 /* Extract geometry if we did not already have one for the drive */
65 if (!drive->cyl || !drive->head || !drive->sect) {
66 drive->cyl = drive->bios_cyl = id->cyls;
67 drive->head = drive->bios_head = id->heads;
68 drive->sect = drive->bios_sect = id->sectors;
71 /* Handle logical geometry translation by the drive */
72 if ((id->field_valid & 1) && id->cur_cyls &&
73 id->cur_heads && (id->cur_heads <= 16) && id->cur_sectors) {
74 drive->cyl = id->cur_cyls;
75 drive->head = id->cur_heads;
76 drive->sect = id->cur_sectors;
79 /* Use physical geometry if what we have still makes no sense */
80 if (drive->head > 16 && id->heads && id->heads <= 16) {
81 drive->cyl = id->cyls;
82 drive->head = id->heads;
83 drive->sect = id->sectors;
87 static void ide_disk_init_mult_count(ide_drive_t *drive)
89 struct hd_driveid *id = drive->id;
91 drive->mult_count = 0;
92 if (id->max_multsect) {
93 #ifdef CONFIG_IDEDISK_MULTI_MODE
94 id->multsect = ((id->max_multsect/2) > 1) ? id->max_multsect : 0;
95 id->multsect_valid = id->multsect ? 1 : 0;
96 drive->mult_req = id->multsect_valid ? id->max_multsect : 0;
97 drive->special.b.set_multmode = drive->mult_req ? 1 : 0;
98 #else /* original, pre IDE-NFG, per request of AC */
100 if (drive->mult_req > id->max_multsect)
101 drive->mult_req = id->max_multsect;
102 if (drive->mult_req || ((id->multsect_valid & 1) && id->multsect))
103 drive->special.b.set_multmode = 1;
109 * do_identify - identify a drive
110 * @drive: drive to identify
113 * Called when we have issued a drive identify command to
114 * read and parse the results. This function is run with
115 * interrupts disabled.
118 static inline void do_identify (ide_drive_t *drive, u8 cmd)
120 ide_hwif_t *hwif = HWIF(drive);
122 struct hd_driveid *id;
125 /* read 512 bytes of id info */
126 hwif->ata_input_data(drive, id, SECTOR_WORDS);
131 printk(KERN_INFO "%s: dumping identify data\n", drive->name);
132 ide_dump_identify((u8 *)id);
136 #if defined (CONFIG_SCSI_EATA_PIO) || defined (CONFIG_SCSI_EATA)
138 * EATA SCSI controllers do a hardware ATA emulation:
139 * Ignore them if there is a driver for them available.
141 if ((id->model[0] == 'P' && id->model[1] == 'M') ||
142 (id->model[0] == 'S' && id->model[1] == 'K')) {
143 printk("%s: EATA SCSI HBA %.10s\n", drive->name, id->model);
146 #endif /* CONFIG_SCSI_EATA || CONFIG_SCSI_EATA_PIO */
149 * WIN_IDENTIFY returns little-endian info,
150 * WIN_PIDENTIFY *usually* returns little-endian info.
152 if (cmd == WIN_PIDENTIFY) {
153 if ((id->model[0] == 'N' && id->model[1] == 'E') /* NEC */
154 || (id->model[0] == 'F' && id->model[1] == 'X') /* Mitsumi */
155 || (id->model[0] == 'P' && id->model[1] == 'i'))/* Pioneer */
156 /* Vertos drives may still be weird */
159 ide_fixstring(id->model, sizeof(id->model), bswap);
160 ide_fixstring(id->fw_rev, sizeof(id->fw_rev), bswap);
161 ide_fixstring(id->serial_no, sizeof(id->serial_no), bswap);
163 /* we depend on this a lot! */
164 id->model[sizeof(id->model)-1] = '\0';
166 if (strstr(id->model, "E X A B Y T E N E S T"))
169 printk("%s: %s, ", drive->name, id->model);
174 * Check for an ATAPI device
176 if (cmd == WIN_PIDENTIFY) {
177 u8 type = (id->config >> 8) & 0x1f;
181 if (!strstr(id->model, "CD-ROM")) {
182 if (!strstr(id->model, "oppy") &&
183 !strstr(id->model, "poyp") &&
184 !strstr(id->model, "ZIP"))
185 printk("cdrom or floppy?, assuming ");
186 if (drive->media != ide_cdrom) {
188 drive->removable = 1;
192 /* Early cdrom models used zero */
195 drive->removable = 1;
197 /* kludge for Apple PowerBook internal zip */
198 if (!strstr(id->model, "CD-ROM") &&
199 strstr(id->model, "ZIP")) {
205 printk ("CD/DVD-ROM");
212 drive->removable = 1;
215 printk("UNKNOWN (type %d)", type);
220 /* an ATAPI device ignores DRDY */
221 drive->ready_stat = 0;
226 * Not an ATAPI device: looks like a "regular" hard disk
230 * 0x848a = CompactFlash device
231 * These are *not* removable in Linux definition of the term
234 if ((id->config != 0x848a) && (id->config & (1<<7)))
235 drive->removable = 1;
237 drive->media = ide_disk;
238 printk("%s DISK drive\n", (id->config == 0x848a) ? "CFA" : "ATA" );
249 * actual_try_to_identify - send ata/atapi identify
250 * @drive: drive to identify
251 * @cmd: command to use
253 * try_to_identify() sends an ATA(PI) IDENTIFY request to a drive
254 * and waits for a response. It also monitors irqs while this is
255 * happening, in hope of automatically determining which one is
256 * being used by the interface.
258 * Returns: 0 device was identified
259 * 1 device timed-out (no response to identify request)
260 * 2 device aborted the command (refused to identify itself)
263 static int actual_try_to_identify (ide_drive_t *drive, u8 cmd)
265 ide_hwif_t *hwif = HWIF(drive);
267 unsigned long hd_status;
268 unsigned long timeout;
271 /* take a deep breath */
274 if (IDE_CONTROL_REG) {
275 a = hwif->INB(IDE_ALTSTATUS_REG);
276 s = hwif->INB(IDE_STATUS_REG);
277 if ((a ^ s) & ~INDEX_STAT) {
278 printk(KERN_INFO "%s: probing with STATUS(0x%02x) instead of "
279 "ALTSTATUS(0x%02x)\n", drive->name, s, a);
280 /* ancient Seagate drives, broken interfaces */
281 hd_status = IDE_STATUS_REG;
283 /* use non-intrusive polling */
284 hd_status = IDE_ALTSTATUS_REG;
287 hd_status = IDE_STATUS_REG;
289 /* set features register for atapi
290 * identify command to be sure of reply
292 if ((cmd == WIN_PIDENTIFY))
293 /* disable dma & overlap */
294 hwif->OUTB(0, IDE_FEATURE_REG);
296 /* ask drive for ID */
297 hwif->OUTB(cmd, IDE_COMMAND_REG);
299 timeout = ((cmd == WIN_IDENTIFY) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
302 if (time_after(jiffies, timeout)) {
303 /* drive timed-out */
306 /* give drive a breather */
308 } while ((hwif->INB(hd_status)) & BUSY_STAT);
310 /* wait for IRQ and DRQ_STAT */
312 if (OK_STAT((hwif->INB(IDE_STATUS_REG)), DRQ_STAT, BAD_R_STAT)) {
315 /* local CPU only; some systems need this */
316 local_irq_save(flags);
317 /* drive returned ID */
318 do_identify(drive, cmd);
319 /* drive responded with ID */
321 /* clear drive IRQ */
322 (void) hwif->INB(IDE_STATUS_REG);
323 local_irq_restore(flags);
325 /* drive refused ID */
332 * try_to_identify - try to identify a drive
333 * @drive: drive to probe
334 * @cmd: command to use
336 * Issue the identify command and then do IRQ probing to
337 * complete the identification when needed by finding the
338 * IRQ the drive is attached to
341 static int try_to_identify (ide_drive_t *drive, u8 cmd)
343 ide_hwif_t *hwif = HWIF(drive);
346 unsigned long cookie = 0;
349 * Disable device irq unless we need to
350 * probe for it. Otherwise we'll get spurious
351 * interrupts during the identify-phase that
352 * the irq handler isn't expecting.
354 if (IDE_CONTROL_REG) {
357 cookie = probe_irq_on();
359 ide_set_irq(drive, autoprobe);
362 retval = actual_try_to_identify(drive, cmd);
367 ide_set_irq(drive, 0);
368 /* clear drive IRQ */
369 (void) hwif->INB(IDE_STATUS_REG);
371 irq = probe_irq_off(cookie);
376 /* Mmmm.. multiple IRQs..
377 * don't know which was ours
379 printk("%s: IRQ probe failed (0x%lx)\n",
380 drive->name, cookie);
387 static int ide_busy_sleep(ide_hwif_t *hwif)
389 unsigned long timeout = jiffies + WAIT_WORSTCASE;
394 stat = hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
395 if ((stat & BUSY_STAT) == 0)
397 } while (time_before(jiffies, timeout));
403 * do_probe - probe an IDE device
404 * @drive: drive to probe
405 * @cmd: command to use
407 * do_probe() has the difficult job of finding a drive if it exists,
408 * without getting hung up if it doesn't exist, without trampling on
409 * ethernet cards, and without leaving any IRQs dangling to haunt us later.
411 * If a drive is "known" to exist (from CMOS or kernel parameters),
412 * but does not respond right away, the probe will "hang in there"
413 * for the maximum wait time (about 30 seconds), otherwise it will
414 * exit much more quickly.
416 * Returns: 0 device was identified
417 * 1 device timed-out (no response to identify request)
418 * 2 device aborted the command (refused to identify itself)
419 * 3 bad status from device (possible for ATAPI drives)
420 * 4 probe was not attempted because failure was obvious
423 static int do_probe (ide_drive_t *drive, u8 cmd)
426 ide_hwif_t *hwif = HWIF(drive);
428 if (drive->present) {
429 /* avoid waiting for inappropriate probes */
430 if ((drive->media != ide_disk) && (cmd == WIN_IDENTIFY))
434 printk("probing for %s: present=%d, media=%d, probetype=%s\n",
435 drive->name, drive->present, drive->media,
436 (cmd == WIN_IDENTIFY) ? "ATA" : "ATAPI");
439 /* needed for some systems
440 * (e.g. crw9624 as drive0 with disk as slave)
445 if (hwif->INB(IDE_SELECT_REG) != drive->select.all && !drive->present) {
446 if (drive->select.b.unit != 0) {
447 /* exit with drive0 selected */
448 SELECT_DRIVE(&hwif->drives[0]);
449 /* allow BUSY_STAT to assert & clear */
452 /* no i/f present: mmm.. this should be a 4 -ml */
456 if (OK_STAT((hwif->INB(IDE_STATUS_REG)), READY_STAT, BUSY_STAT) ||
457 drive->present || cmd == WIN_PIDENTIFY) {
458 /* send cmd and wait */
459 if ((rc = try_to_identify(drive, cmd))) {
460 /* failed: try again */
461 rc = try_to_identify(drive,cmd);
463 if (hwif->INB(IDE_STATUS_REG) == (BUSY_STAT|READY_STAT))
466 if ((rc == 1 && cmd == WIN_PIDENTIFY) &&
467 ((drive->autotune == IDE_TUNE_DEFAULT) ||
468 (drive->autotune == IDE_TUNE_AUTO))) {
469 printk("%s: no response (status = 0x%02x), "
470 "resetting drive\n", drive->name,
471 hwif->INB(IDE_STATUS_REG));
473 hwif->OUTB(drive->select.all, IDE_SELECT_REG);
475 hwif->OUTB(WIN_SRST, IDE_COMMAND_REG);
476 (void)ide_busy_sleep(hwif);
477 rc = try_to_identify(drive, cmd);
480 printk("%s: no response (status = 0x%02x)\n",
481 drive->name, hwif->INB(IDE_STATUS_REG));
482 /* ensure drive irq is clear */
483 (void) hwif->INB(IDE_STATUS_REG);
485 /* not present or maybe ATAPI */
488 if (drive->select.b.unit != 0) {
489 /* exit with drive0 selected */
490 SELECT_DRIVE(&hwif->drives[0]);
492 /* ensure drive irq is clear */
493 (void) hwif->INB(IDE_STATUS_REG);
501 static void enable_nest (ide_drive_t *drive)
503 ide_hwif_t *hwif = HWIF(drive);
505 printk("%s: enabling %s -- ", hwif->name, drive->id->model);
508 hwif->OUTB(EXABYTE_ENABLE_NEST, IDE_COMMAND_REG);
510 if (ide_busy_sleep(hwif)) {
511 printk(KERN_CONT "failed (timeout)\n");
517 if (!OK_STAT((hwif->INB(IDE_STATUS_REG)), 0, BAD_STAT)) {
518 printk("failed (status = 0x%02x)\n", hwif->INB(IDE_STATUS_REG));
523 /* if !(success||timed-out) */
524 if (do_probe(drive, WIN_IDENTIFY) >= 2) {
525 /* look for ATAPI device */
526 (void) do_probe(drive, WIN_PIDENTIFY);
531 * probe_for_drives - upper level drive probe
532 * @drive: drive to probe for
534 * probe_for_drive() tests for existence of a given drive using do_probe()
535 * and presents things to the user as needed.
537 * Returns: 0 no device was found
538 * 1 device was found (note: drive->present might
542 static inline u8 probe_for_drive (ide_drive_t *drive)
545 * In order to keep things simple we have an id
546 * block for all drives at all times. If the device
547 * is pre ATA or refuses ATA/ATAPI identify we
548 * will add faked data to this.
550 * Also note that 0 everywhere means "can't do X"
553 drive->id = kzalloc(SECTOR_WORDS *4, GFP_KERNEL);
555 if(drive->id == NULL)
557 printk(KERN_ERR "ide: out of memory for id data.\n");
560 strcpy(drive->id->model, "UNKNOWN");
565 /* if !(success||timed-out) */
566 if (do_probe(drive, WIN_IDENTIFY) >= 2) {
567 /* look for ATAPI device */
568 (void) do_probe(drive, WIN_PIDENTIFY);
571 /* drive not found */
573 if (strstr(drive->id->model, "E X A B Y T E N E S T"))
576 /* identification failed? */
577 if (!drive->id_read) {
578 if (drive->media == ide_disk) {
579 printk(KERN_INFO "%s: non-IDE drive, CHS=%d/%d/%d\n",
580 drive->name, drive->cyl,
581 drive->head, drive->sect);
582 } else if (drive->media == ide_cdrom) {
583 printk(KERN_INFO "%s: ATAPI cdrom (?)\n", drive->name);
586 printk(KERN_WARNING "%s: Unknown device on bus refused identification. Ignoring.\n", drive->name);
590 /* drive was found */
594 /* The drive wasn't being helpful. Add generic info only */
595 if (drive->id_read == 0) {
600 if (drive->media == ide_disk) {
601 ide_disk_init_chs(drive);
602 ide_disk_init_mult_count(drive);
605 return drive->present;
608 static void hwif_release_dev (struct device *dev)
610 ide_hwif_t *hwif = container_of(dev, ide_hwif_t, gendev);
612 complete(&hwif->gendev_rel_comp);
615 static void hwif_register (ide_hwif_t *hwif)
619 /* register with global device tree */
620 strlcpy(hwif->gendev.bus_id,hwif->name,BUS_ID_SIZE);
621 hwif->gendev.driver_data = hwif;
622 if (hwif->gendev.parent == NULL) {
624 hwif->gendev.parent = hwif->dev;
626 /* Would like to do = &device_legacy */
627 hwif->gendev.parent = NULL;
629 hwif->gendev.release = hwif_release_dev;
630 ret = device_register(&hwif->gendev);
632 printk(KERN_WARNING "IDE: %s: device_register error: %d\n",
636 static int wait_hwif_ready(ide_hwif_t *hwif)
640 printk(KERN_DEBUG "Probing IDE interface %s...\n", hwif->name);
642 /* Let HW settle down a bit from whatever init state we
646 /* Wait for BSY bit to go away, spec timeout is 30 seconds,
647 * I know of at least one disk who takes 31 seconds, I use 35
650 rc = ide_wait_not_busy(hwif, 35000);
654 /* Now make sure both master & slave are ready */
655 for (unit = 0; unit < MAX_DRIVES; unit++) {
656 ide_drive_t *drive = &hwif->drives[unit];
658 /* Ignore disks that we will not probe for later. */
659 if (!drive->noprobe || drive->present) {
661 ide_set_irq(drive, 1);
663 rc = ide_wait_not_busy(hwif, 35000);
667 printk(KERN_DEBUG "%s: ide_wait_not_busy() skipped\n",
671 /* Exit function with master reselected (let's be sane) */
673 SELECT_DRIVE(&hwif->drives[0]);
679 * ide_undecoded_slave - look for bad CF adapters
682 * Analyse the drives on the interface and attempt to decide if we
683 * have the same drive viewed twice. This occurs with crap CF adapters
684 * and PCMCIA sometimes.
687 void ide_undecoded_slave(ide_drive_t *drive1)
689 ide_drive_t *drive0 = &drive1->hwif->drives[0];
691 if ((drive1->dn & 1) == 0 || drive0->present == 0)
694 /* If the models don't match they are not the same product */
695 if (strcmp(drive0->id->model, drive1->id->model))
698 /* Serial numbers do not match */
699 if (strncmp(drive0->id->serial_no, drive1->id->serial_no, 20))
702 /* No serial number, thankfully very rare for CF */
703 if (drive0->id->serial_no[0] == 0)
706 /* Appears to be an IDE flash adapter with decode bugs */
707 printk(KERN_WARNING "ide-probe: ignoring undecoded slave\n");
712 EXPORT_SYMBOL_GPL(ide_undecoded_slave);
715 * This routine only knows how to look for drive units 0 and 1
716 * on an interface, so any setting of MAX_DRIVES > 2 won't work here.
718 static void probe_hwif(ide_hwif_t *hwif)
727 if ((hwif->chipset != ide_4drives || !hwif->mate || !hwif->mate->present) &&
728 (ide_hwif_request_regions(hwif))) {
730 for (unit = 0; unit < MAX_DRIVES; ++unit) {
731 ide_drive_t *drive = &hwif->drives[unit];
732 if (drive->present) {
734 printk(KERN_ERR "%s: ERROR, PORTS ALREADY IN USE\n",
740 printk(KERN_ERR "%s: ports already in use, skipping probe\n",
746 * We must always disable IRQ, as probe_for_drive will assert IRQ, but
747 * we'll install our IRQ driver much later...
751 disable_irq(hwif->irq);
753 local_irq_set(flags);
755 /* This is needed on some PPCs and a bunch of BIOS-less embedded
756 * platforms. Typical cases are:
758 * - The firmware hard reset the disk before booting the kernel,
759 * the drive is still doing it's poweron-reset sequence, that
760 * can take up to 30 seconds
761 * - The firmware does nothing (or no firmware), the device is
762 * still in POST state (same as above actually).
763 * - Some CD/DVD/Writer combo drives tend to drive the bus during
764 * their reset sequence even when they are non-selected slave
765 * devices, thus preventing discovery of the main HD
767 * Doing this wait-for-busy should not harm any existing configuration
768 * (at least things won't be worse than what current code does, that
769 * is blindly go & talk to the drive) and fix some issues like the
774 if (wait_hwif_ready(hwif) == -EBUSY)
775 printk(KERN_DEBUG "%s: Wait for ready failed before probe !\n", hwif->name);
778 * Need to probe slave device first to make it release PDIAG-.
780 for (unit = MAX_DRIVES - 1; unit >= 0; unit--) {
781 ide_drive_t *drive = &hwif->drives[unit];
782 drive->dn = (hwif->channel ? 2 : 0) + unit;
783 (void) probe_for_drive(drive);
784 if (drive->present && !hwif->present) {
786 if (hwif->chipset != ide_4drives ||
788 !hwif->mate->present) {
793 if (hwif->io_ports[IDE_CONTROL_OFFSET] && hwif->reset) {
794 printk(KERN_WARNING "%s: reset\n", hwif->name);
795 hwif->OUTB(12, hwif->io_ports[IDE_CONTROL_OFFSET]);
797 hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
798 (void)ide_busy_sleep(hwif);
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];
816 if (drive->present && hwif->quirkproc)
817 hwif->quirkproc(drive);
820 for (unit = 0; unit < MAX_DRIVES; ++unit) {
821 ide_drive_t *drive = &hwif->drives[unit];
823 if (drive->present) {
824 if (drive->autotune == IDE_TUNE_AUTO)
825 ide_set_max_pio(drive);
827 if (drive->autotune != IDE_TUNE_DEFAULT &&
828 drive->autotune != IDE_TUNE_AUTO)
833 if (hwif->dma_host_set)
838 for (unit = 0; unit < MAX_DRIVES; ++unit) {
839 ide_drive_t *drive = &hwif->drives[unit];
841 if (hwif->no_io_32bit)
842 drive->no_io_32bit = 1;
844 drive->no_io_32bit = drive->id->dword_io ? 1 : 0;
850 * save_match() is used to simplify logic in init_irq() below.
852 * A loophole here is that we may not know about a particular
853 * hwif's irq until after that hwif is actually probed/initialized..
854 * This could be a problem for the case where an hwif is on a
855 * dual interface that requires serialization (eg. cmd640) and another
856 * hwif using one of the same irqs is initialized beforehand.
858 * This routine detects and reports such situations, but does not fix them.
860 static void save_match(ide_hwif_t *hwif, ide_hwif_t *new, ide_hwif_t **match)
862 ide_hwif_t *m = *match;
864 if (m && m->hwgroup && m->hwgroup != new->hwgroup) {
867 printk("%s: potential irq problem with %s and %s\n",
868 hwif->name, new->name, m->name);
870 if (!m || m->irq != hwif->irq) /* don't undo a prior perfect match */
873 #endif /* MAX_HWIFS > 1 */
878 static int ide_init_queue(ide_drive_t *drive)
880 struct request_queue *q;
881 ide_hwif_t *hwif = HWIF(drive);
882 int max_sectors = 256;
883 int max_sg_entries = PRD_ENTRIES;
886 * Our default set up assumes the normal IDE case,
887 * that is 64K segmenting, standard PRD setup
888 * and LBA28. Some drivers then impose their own
889 * limits and LBA48 we could raise it but as yet
893 q = blk_init_queue_node(do_ide_request, &ide_lock, hwif_to_node(hwif));
897 q->queuedata = drive;
898 blk_queue_segment_boundary(q, 0xffff);
901 if ((hwif->host_flags & IDE_HFLAG_NO_LBA48) ||
902 (hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA))
905 hwif->rqsize = 65536;
907 if (hwif->rqsize < max_sectors)
908 max_sectors = hwif->rqsize;
909 blk_queue_max_sectors(q, max_sectors);
912 /* When we have an IOMMU, we may have a problem where pci_map_sg()
913 * creates segments that don't completely match our boundary
914 * requirements and thus need to be broken up again. Because it
915 * doesn't align properly either, we may actually have to break up
916 * to more segments than what was we got in the first place, a max
917 * worst case is twice as many.
918 * This will be fixed once we teach pci_map_sg() about our boundary
919 * requirements, hopefully soon. *FIXME*
921 if (!PCI_DMA_BUS_IS_PHYS)
922 max_sg_entries >>= 1;
923 #endif /* CONFIG_PCI */
925 blk_queue_max_hw_segments(q, max_sg_entries);
926 blk_queue_max_phys_segments(q, max_sg_entries);
928 /* assign drive queue */
931 /* needs drive->queue to be set */
932 ide_toggle_bounce(drive, 1);
938 * This routine sets up the irq for an ide interface, and creates a new
939 * hwgroup for the irq/hwif if none was previously assigned.
941 * Much of the code is for correctly detecting/handling irq sharing
942 * and irq serialization situations. This is somewhat complex because
943 * it handles static as well as dynamic (PCMCIA) IDE interfaces.
945 static int init_irq (ide_hwif_t *hwif)
948 ide_hwgroup_t *hwgroup;
949 ide_hwif_t *match = NULL;
952 BUG_ON(in_interrupt());
953 BUG_ON(irqs_disabled());
954 BUG_ON(hwif == NULL);
956 mutex_lock(&ide_cfg_mtx);
957 hwif->hwgroup = NULL;
960 * Group up with any other hwifs that share our irq(s).
962 for (index = 0; index < MAX_HWIFS; index++) {
963 ide_hwif_t *h = &ide_hwifs[index];
964 if (h->hwgroup) { /* scan only initialized hwif's */
965 if (hwif->irq == h->irq) {
966 hwif->sharing_irq = h->sharing_irq = 1;
967 if (hwif->chipset != ide_pci ||
968 h->chipset != ide_pci) {
969 save_match(hwif, h, &match);
972 if (hwif->serialized) {
973 if (hwif->mate && hwif->mate->irq == h->irq)
974 save_match(hwif, h, &match);
977 if (h->mate && hwif->irq == h->mate->irq)
978 save_match(hwif, h, &match);
982 #endif /* MAX_HWIFS > 1 */
984 * If we are still without a hwgroup, then form a new one
987 hwgroup = match->hwgroup;
988 hwif->hwgroup = hwgroup;
990 * Link us into the hwgroup.
991 * This must be done early, do ensure that unexpected_intr
992 * can find the hwif and prevent irq storms.
993 * No drives are attached to the new hwif, choose_drive
994 * can't do anything stupid (yet).
995 * Add ourself as the 2nd entry to the hwgroup->hwif
996 * linked list, the first entry is the hwif that owns
997 * hwgroup->handler - do not change that.
999 spin_lock_irq(&ide_lock);
1000 hwif->next = hwgroup->hwif->next;
1001 hwgroup->hwif->next = hwif;
1002 spin_unlock_irq(&ide_lock);
1004 hwgroup = kmalloc_node(sizeof(ide_hwgroup_t),
1005 GFP_KERNEL | __GFP_ZERO,
1006 hwif_to_node(hwif->drives[0].hwif));
1010 hwif->hwgroup = hwgroup;
1012 hwgroup->hwif = hwif->next = hwif;
1014 hwgroup->handler = NULL;
1015 hwgroup->drive = NULL;
1017 init_timer(&hwgroup->timer);
1018 hwgroup->timer.function = &ide_timer_expiry;
1019 hwgroup->timer.data = (unsigned long) hwgroup;
1023 * Allocate the irq, if not already obtained for another hwif
1025 if (!match || match->irq != hwif->irq) {
1027 #if defined(__mc68000__) || defined(CONFIG_APUS)
1029 #endif /* __mc68000__ || CONFIG_APUS */
1031 if (IDE_CHIPSET_IS_PCI(hwif->chipset))
1034 if (hwif->io_ports[IDE_CONTROL_OFFSET])
1036 hwif->OUTB(0x08, hwif->io_ports[IDE_CONTROL_OFFSET]);
1038 if (request_irq(hwif->irq,&ide_intr,sa,hwif->name,hwgroup))
1043 * For any present drive:
1044 * - allocate the block device queue
1045 * - link drive into the hwgroup
1047 for (index = 0; index < MAX_DRIVES; ++index) {
1048 ide_drive_t *drive = &hwif->drives[index];
1049 if (!drive->present)
1051 if (ide_init_queue(drive)) {
1052 printk(KERN_ERR "ide: failed to init %s\n",drive->name);
1055 spin_lock_irq(&ide_lock);
1056 if (!hwgroup->drive) {
1057 /* first drive for hwgroup. */
1058 drive->next = drive;
1059 hwgroup->drive = drive;
1060 hwgroup->hwif = HWIF(hwgroup->drive);
1062 drive->next = hwgroup->drive->next;
1063 hwgroup->drive->next = drive;
1065 spin_unlock_irq(&ide_lock);
1068 #if !defined(__mc68000__) && !defined(CONFIG_APUS)
1069 printk("%s at 0x%03lx-0x%03lx,0x%03lx on irq %d", hwif->name,
1070 hwif->io_ports[IDE_DATA_OFFSET],
1071 hwif->io_ports[IDE_DATA_OFFSET]+7,
1072 hwif->io_ports[IDE_CONTROL_OFFSET], hwif->irq);
1074 printk("%s at 0x%08lx on irq %d", hwif->name,
1075 hwif->io_ports[IDE_DATA_OFFSET], hwif->irq);
1076 #endif /* __mc68000__ && CONFIG_APUS */
1078 printk(" (%sed with %s)",
1079 hwif->sharing_irq ? "shar" : "serializ", match->name);
1081 mutex_unlock(&ide_cfg_mtx);
1084 spin_lock_irq(&ide_lock);
1085 if (hwif->next == hwif) {
1087 BUG_ON(hwgroup->hwif != hwif);
1092 while (g->next != hwif)
1094 g->next = hwif->next;
1095 if (hwgroup->hwif == hwif) {
1097 printk(KERN_ERR "Duh. Uninitialized hwif listed as active hwif.\n");
1100 BUG_ON(hwgroup->hwif == hwif);
1102 spin_unlock_irq(&ide_lock);
1104 mutex_unlock(&ide_cfg_mtx);
1108 static int ata_lock(dev_t dev, void *data)
1110 /* FIXME: we want to pin hwif down */
1114 static struct kobject *ata_probe(dev_t dev, int *part, void *data)
1116 ide_hwif_t *hwif = data;
1117 int unit = *part >> PARTN_BITS;
1118 ide_drive_t *drive = &hwif->drives[unit];
1119 if (!drive->present)
1122 if (drive->media == ide_disk)
1123 request_module("ide-disk");
1125 request_module("ide-scsi");
1126 if (drive->media == ide_cdrom || drive->media == ide_optical)
1127 request_module("ide-cd");
1128 if (drive->media == ide_tape)
1129 request_module("ide-tape");
1130 if (drive->media == ide_floppy)
1131 request_module("ide-floppy");
1136 static struct kobject *exact_match(dev_t dev, int *part, void *data)
1138 struct gendisk *p = data;
1139 *part &= (1 << PARTN_BITS) - 1;
1140 return &p->dev.kobj;
1143 static int exact_lock(dev_t dev, void *data)
1145 struct gendisk *p = data;
1152 void ide_register_region(struct gendisk *disk)
1154 blk_register_region(MKDEV(disk->major, disk->first_minor),
1155 disk->minors, NULL, exact_match, exact_lock, disk);
1158 EXPORT_SYMBOL_GPL(ide_register_region);
1160 void ide_unregister_region(struct gendisk *disk)
1162 blk_unregister_region(MKDEV(disk->major, disk->first_minor),
1166 EXPORT_SYMBOL_GPL(ide_unregister_region);
1168 void ide_init_disk(struct gendisk *disk, ide_drive_t *drive)
1170 ide_hwif_t *hwif = drive->hwif;
1171 unsigned int unit = (drive->select.all >> 4) & 1;
1173 disk->major = hwif->major;
1174 disk->first_minor = unit << PARTN_BITS;
1175 sprintf(disk->disk_name, "hd%c", 'a' + hwif->index * MAX_DRIVES + unit);
1176 disk->queue = drive->queue;
1179 EXPORT_SYMBOL_GPL(ide_init_disk);
1181 static void ide_remove_drive_from_hwgroup(ide_drive_t *drive)
1183 ide_hwgroup_t *hwgroup = drive->hwif->hwgroup;
1185 if (drive == drive->next) {
1186 /* special case: last drive from hwgroup. */
1187 BUG_ON(hwgroup->drive != drive);
1188 hwgroup->drive = NULL;
1192 walk = hwgroup->drive;
1193 while (walk->next != drive)
1195 walk->next = drive->next;
1196 if (hwgroup->drive == drive) {
1197 hwgroup->drive = drive->next;
1198 hwgroup->hwif = hwgroup->drive->hwif;
1201 BUG_ON(hwgroup->drive == drive);
1204 static void drive_release_dev (struct device *dev)
1206 ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
1208 spin_lock_irq(&ide_lock);
1209 ide_remove_drive_from_hwgroup(drive);
1213 /* Messed up locking ... */
1214 spin_unlock_irq(&ide_lock);
1215 blk_cleanup_queue(drive->queue);
1216 spin_lock_irq(&ide_lock);
1217 drive->queue = NULL;
1218 spin_unlock_irq(&ide_lock);
1220 complete(&drive->gendev_rel_comp);
1224 * init_gendisk() (as opposed to ide_geninit) is called for each major device,
1225 * after probing for drives, to allocate partition tables and other data
1226 * structures needed for the routines in genhd.c. ide_geninit() gets called
1227 * somewhat later, during the partition check.
1229 static void init_gendisk (ide_hwif_t *hwif)
1233 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1234 ide_drive_t * drive = &hwif->drives[unit];
1235 ide_add_generic_settings(drive);
1236 snprintf(drive->gendev.bus_id,BUS_ID_SIZE,"%u.%u",
1238 drive->gendev.parent = &hwif->gendev;
1239 drive->gendev.bus = &ide_bus_type;
1240 drive->gendev.driver_data = drive;
1241 drive->gendev.release = drive_release_dev;
1243 blk_register_region(MKDEV(hwif->major, 0), MAX_DRIVES << PARTN_BITS,
1244 THIS_MODULE, ata_probe, ata_lock, hwif);
1247 static int hwif_init(ide_hwif_t *hwif)
1251 /* Return success if no device is connected */
1256 if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET])))
1258 printk("%s: DISABLED, NO IRQ\n", hwif->name);
1259 return (hwif->present = 0);
1262 #ifdef CONFIG_BLK_DEV_HD
1263 if (hwif->irq == HD_IRQ && hwif->io_ports[IDE_DATA_OFFSET] != HD_DATA) {
1264 printk("%s: CANNOT SHARE IRQ WITH OLD "
1265 "HARDDISK DRIVER (hd.c)\n", hwif->name);
1266 return (hwif->present = 0);
1268 #endif /* CONFIG_BLK_DEV_HD */
1270 /* we set it back to 1 if all is ok below */
1273 if (register_blkdev(hwif->major, hwif->name))
1276 if (!hwif->sg_max_nents)
1277 hwif->sg_max_nents = PRD_ENTRIES;
1279 hwif->sg_table = kmalloc(sizeof(struct scatterlist)*hwif->sg_max_nents,
1281 if (!hwif->sg_table) {
1282 printk(KERN_ERR "%s: unable to allocate SG table.\n", hwif->name);
1286 sg_init_table(hwif->sg_table, hwif->sg_max_nents);
1288 if (init_irq(hwif) == 0)
1291 old_irq = hwif->irq;
1293 * It failed to initialise. Find the default IRQ for
1294 * this port and try that.
1296 if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET]))) {
1297 printk("%s: Disabled unable to get IRQ %d.\n",
1298 hwif->name, old_irq);
1301 if (init_irq(hwif)) {
1302 printk("%s: probed IRQ %d and default IRQ %d failed.\n",
1303 hwif->name, old_irq, hwif->irq);
1306 printk("%s: probed IRQ %d failed, using default.\n",
1307 hwif->name, hwif->irq);
1312 ide_acpi_init(hwif);
1314 hwif->present = 1; /* success */
1318 unregister_blkdev(hwif->major, hwif->name);
1322 static void hwif_register_devices(ide_hwif_t *hwif)
1326 for (i = 0; i < MAX_DRIVES; i++) {
1327 ide_drive_t *drive = &hwif->drives[i];
1329 if (drive->present) {
1330 int ret = device_register(&drive->gendev);
1333 printk(KERN_WARNING "IDE: %s: "
1334 "device_register error: %d\n",
1340 int ide_device_add_all(u8 *idx)
1345 for (i = 0; i < MAX_HWIFS; i++) {
1349 probe_hwif(&ide_hwifs[idx[i]]);
1352 for (i = 0; i < MAX_HWIFS; i++) {
1356 hwif = &ide_hwifs[idx[i]];
1358 if (hwif_init(hwif) == 0) {
1359 printk(KERN_INFO "%s: failed to initialize IDE "
1360 "interface\n", hwif->name);
1366 for (i = 0; i < MAX_HWIFS; i++) {
1370 hwif = &ide_hwifs[idx[i]];
1372 if (hwif->present) {
1373 if (hwif->chipset == ide_unknown ||
1374 hwif->chipset == ide_forced)
1375 hwif->chipset = ide_generic;
1376 hwif_register_devices(hwif);
1380 for (i = 0; i < MAX_HWIFS; i++) {
1382 ide_proc_register_port(&ide_hwifs[idx[i]]);
1387 EXPORT_SYMBOL_GPL(ide_device_add_all);
1389 int ide_device_add(u8 idx[4])
1391 u8 idx_all[MAX_HWIFS];
1394 for (i = 0; i < MAX_HWIFS; i++)
1395 idx_all[i] = (i < 4) ? idx[i] : 0xff;
1397 return ide_device_add_all(idx_all);
1399 EXPORT_SYMBOL_GPL(ide_device_add);