2 * Copyright (C) 1994-1998 Linus Torvalds & authors (see below)
3 * Copyright (C) 2005, 2007 Bartlomiej Zolnierkiewicz
7 * Mostly written by Mark Lord <mlord@pobox.com>
8 * and Gadi Oxman <gadio@netvision.net.il>
9 * and Andre Hedrick <andre@linux-ide.org>
11 * See linux/MAINTAINERS for address of current maintainer.
13 * This is the IDE probe module, as evolved from hd.c and ide.c.
15 * -- increase WAIT_PIDENTIFY to avoid CD-ROM locking at boot
19 #include <linux/module.h>
20 #include <linux/types.h>
21 #include <linux/string.h>
22 #include <linux/kernel.h>
23 #include <linux/timer.h>
25 #include <linux/interrupt.h>
26 #include <linux/major.h>
27 #include <linux/errno.h>
28 #include <linux/genhd.h>
29 #include <linux/slab.h>
30 #include <linux/delay.h>
31 #include <linux/ide.h>
32 #include <linux/spinlock.h>
33 #include <linux/kmod.h>
34 #include <linux/pci.h>
35 #include <linux/scatterlist.h>
37 #include <asm/byteorder.h>
39 #include <asm/uaccess.h>
43 * generic_id - add a generic drive id
44 * @drive: drive to make an ID block for
46 * Add a fake id field to the drive we are passed. This allows
47 * use to skip a ton of NULL checks (which people always miss)
48 * and make drive properties unconditional outside of this file
51 static void generic_id(ide_drive_t *drive)
53 drive->id->cyls = drive->cyl;
54 drive->id->heads = drive->head;
55 drive->id->sectors = drive->sect;
56 drive->id->cur_cyls = drive->cyl;
57 drive->id->cur_heads = drive->head;
58 drive->id->cur_sectors = drive->sect;
61 static void ide_disk_init_chs(ide_drive_t *drive)
63 struct hd_driveid *id = drive->id;
65 /* Extract geometry if we did not already have one for the drive */
66 if (!drive->cyl || !drive->head || !drive->sect) {
67 drive->cyl = drive->bios_cyl = id->cyls;
68 drive->head = drive->bios_head = id->heads;
69 drive->sect = drive->bios_sect = id->sectors;
72 /* Handle logical geometry translation by the drive */
73 if ((id->field_valid & 1) && id->cur_cyls &&
74 id->cur_heads && (id->cur_heads <= 16) && id->cur_sectors) {
75 drive->cyl = id->cur_cyls;
76 drive->head = id->cur_heads;
77 drive->sect = id->cur_sectors;
80 /* Use physical geometry if what we have still makes no sense */
81 if (drive->head > 16 && id->heads && id->heads <= 16) {
82 drive->cyl = id->cyls;
83 drive->head = id->heads;
84 drive->sect = id->sectors;
88 static void ide_disk_init_mult_count(ide_drive_t *drive)
90 struct hd_driveid *id = drive->id;
92 if (id->max_multsect) {
93 #ifdef CONFIG_IDEDISK_MULTI_MODE
94 if ((id->max_multsect / 2) > 1) {
95 id->multsect = id->max_multsect;
96 id->multsect_valid = 1;
99 id->multsect_valid = 0;
101 drive->mult_req = id->multsect;
103 if ((id->multsect_valid & 1) && id->multsect)
104 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->tp_ops->input_data(drive, NULL, id, SECTOR_SIZE);
131 printk(KERN_INFO "%s: dumping identify data\n", drive->name);
132 ide_dump_identify((u8 *)id);
137 * WIN_IDENTIFY returns little-endian info,
138 * WIN_PIDENTIFY *usually* returns little-endian info.
140 if (cmd == WIN_PIDENTIFY) {
141 if ((id->model[0] == 'N' && id->model[1] == 'E') /* NEC */
142 || (id->model[0] == 'F' && id->model[1] == 'X') /* Mitsumi */
143 || (id->model[0] == 'P' && id->model[1] == 'i'))/* Pioneer */
144 /* Vertos drives may still be weird */
147 ide_fixstring(id->model, sizeof(id->model), bswap);
148 ide_fixstring(id->fw_rev, sizeof(id->fw_rev), bswap);
149 ide_fixstring(id->serial_no, sizeof(id->serial_no), bswap);
151 /* we depend on this a lot! */
152 id->model[sizeof(id->model)-1] = '\0';
154 if (strstr(id->model, "E X A B Y T E N E S T"))
157 printk(KERN_INFO "%s: %s, ", drive->name, id->model);
163 * Check for an ATAPI device
165 if (cmd == WIN_PIDENTIFY) {
166 u8 type = (id->config >> 8) & 0x1f;
168 printk(KERN_CONT "ATAPI ");
171 if (!strstr(id->model, "CD-ROM")) {
172 if (!strstr(id->model, "oppy") &&
173 !strstr(id->model, "poyp") &&
174 !strstr(id->model, "ZIP"))
175 printk(KERN_CONT "cdrom or floppy?, assuming ");
176 if (drive->media != ide_cdrom) {
177 printk(KERN_CONT "FLOPPY");
178 drive->removable = 1;
182 /* Early cdrom models used zero */
185 drive->removable = 1;
187 /* kludge for Apple PowerBook internal zip */
188 if (!strstr(id->model, "CD-ROM") &&
189 strstr(id->model, "ZIP")) {
190 printk(KERN_CONT "FLOPPY");
195 printk(KERN_CONT "CD/DVD-ROM");
198 printk(KERN_CONT "TAPE");
201 printk(KERN_CONT "OPTICAL");
202 drive->removable = 1;
205 printk(KERN_CONT "UNKNOWN (type %d)", type);
208 printk(KERN_CONT " drive\n");
210 /* an ATAPI device ignores DRDY */
211 drive->ready_stat = 0;
216 * Not an ATAPI device: looks like a "regular" hard disk
220 * 0x848a = CompactFlash device
221 * These are *not* removable in Linux definition of the term
224 if ((id->config != 0x848a) && (id->config & (1<<7)))
225 drive->removable = 1;
227 drive->media = ide_disk;
229 printk(KERN_CONT "%s DISK drive\n",
230 (id->config == 0x848a) ? "CFA" : "ATA");
241 * actual_try_to_identify - send ata/atapi identify
242 * @drive: drive to identify
243 * @cmd: command to use
245 * try_to_identify() sends an ATA(PI) IDENTIFY request to a drive
246 * and waits for a response. It also monitors irqs while this is
247 * happening, in hope of automatically determining which one is
248 * being used by the interface.
250 * Returns: 0 device was identified
251 * 1 device timed-out (no response to identify request)
252 * 2 device aborted the command (refused to identify itself)
255 static int actual_try_to_identify (ide_drive_t *drive, u8 cmd)
257 ide_hwif_t *hwif = HWIF(drive);
258 struct ide_io_ports *io_ports = &hwif->io_ports;
259 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
260 int use_altstatus = 0, rc;
261 unsigned long timeout;
264 /* take a deep breath */
267 if (io_ports->ctl_addr) {
268 a = tp_ops->read_altstatus(hwif);
269 s = tp_ops->read_status(hwif);
270 if ((a ^ s) & ~INDEX_STAT)
271 /* ancient Seagate drives, broken interfaces */
272 printk(KERN_INFO "%s: probing with STATUS(0x%02x) "
273 "instead of ALTSTATUS(0x%02x)\n",
276 /* use non-intrusive polling */
280 /* set features register for atapi
281 * identify command to be sure of reply
283 if (cmd == WIN_PIDENTIFY) {
286 memset(&task, 0, sizeof(task));
287 /* disable DMA & overlap */
288 task.tf_flags = IDE_TFLAG_OUT_FEATURE;
290 tp_ops->tf_load(drive, &task);
293 /* ask drive for ID */
294 tp_ops->exec_command(hwif, cmd);
296 timeout = ((cmd == WIN_IDENTIFY) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
299 if (time_after(jiffies, timeout)) {
300 /* drive timed-out */
303 /* give drive a breather */
305 s = use_altstatus ? tp_ops->read_altstatus(hwif)
306 : tp_ops->read_status(hwif);
307 } while (s & BUSY_STAT);
309 /* wait for IRQ and DRQ_STAT */
311 s = tp_ops->read_status(hwif);
313 if (OK_STAT(s, DRQ_STAT, BAD_R_STAT)) {
316 /* local CPU only; some systems need this */
317 local_irq_save(flags);
318 /* drive returned ID */
319 do_identify(drive, cmd);
320 /* drive responded with ID */
322 /* clear drive IRQ */
323 (void)tp_ops->read_status(hwif);
324 local_irq_restore(flags);
326 /* drive refused ID */
333 * try_to_identify - try to identify a drive
334 * @drive: drive to probe
335 * @cmd: command to use
337 * Issue the identify command and then do IRQ probing to
338 * complete the identification when needed by finding the
339 * IRQ the drive is attached to
342 static int try_to_identify (ide_drive_t *drive, u8 cmd)
344 ide_hwif_t *hwif = HWIF(drive);
345 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
348 unsigned long cookie = 0;
351 * Disable device irq unless we need to
352 * probe for it. Otherwise we'll get spurious
353 * interrupts during the identify-phase that
354 * the irq handler isn't expecting.
356 if (hwif->io_ports.ctl_addr) {
359 cookie = probe_irq_on();
361 tp_ops->set_irq(hwif, autoprobe);
364 retval = actual_try_to_identify(drive, cmd);
369 tp_ops->set_irq(hwif, 0);
370 /* clear drive IRQ */
371 (void)tp_ops->read_status(hwif);
373 irq = probe_irq_off(cookie);
378 /* Mmmm.. multiple IRQs..
379 * don't know which was ours
381 printk(KERN_ERR "%s: IRQ probe failed (0x%lx)\n",
382 drive->name, cookie);
389 static int ide_busy_sleep(ide_hwif_t *hwif)
391 unsigned long timeout = jiffies + WAIT_WORSTCASE;
396 stat = hwif->tp_ops->read_status(hwif);
397 if ((stat & BUSY_STAT) == 0)
399 } while (time_before(jiffies, timeout));
404 static u8 ide_read_device(ide_drive_t *drive)
408 memset(&task, 0, sizeof(task));
409 task.tf_flags = IDE_TFLAG_IN_DEVICE;
411 drive->hwif->tp_ops->tf_read(drive, &task);
413 return task.tf.device;
417 * do_probe - probe an IDE device
418 * @drive: drive to probe
419 * @cmd: command to use
421 * do_probe() has the difficult job of finding a drive if it exists,
422 * without getting hung up if it doesn't exist, without trampling on
423 * ethernet cards, and without leaving any IRQs dangling to haunt us later.
425 * If a drive is "known" to exist (from CMOS or kernel parameters),
426 * but does not respond right away, the probe will "hang in there"
427 * for the maximum wait time (about 30 seconds), otherwise it will
428 * exit much more quickly.
430 * Returns: 0 device was identified
431 * 1 device timed-out (no response to identify request)
432 * 2 device aborted the command (refused to identify itself)
433 * 3 bad status from device (possible for ATAPI drives)
434 * 4 probe was not attempted because failure was obvious
437 static int do_probe (ide_drive_t *drive, u8 cmd)
439 ide_hwif_t *hwif = HWIF(drive);
440 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
444 if (drive->present) {
445 /* avoid waiting for inappropriate probes */
446 if ((drive->media != ide_disk) && (cmd == WIN_IDENTIFY))
450 printk(KERN_INFO "probing for %s: present=%d, media=%d, probetype=%s\n",
451 drive->name, drive->present, drive->media,
452 (cmd == WIN_IDENTIFY) ? "ATA" : "ATAPI");
455 /* needed for some systems
456 * (e.g. crw9624 as drive0 with disk as slave)
462 if (ide_read_device(drive) != drive->select.all && !drive->present) {
463 if (drive->select.b.unit != 0) {
464 /* exit with drive0 selected */
465 SELECT_DRIVE(&hwif->drives[0]);
466 /* allow BUSY_STAT to assert & clear */
469 /* no i/f present: mmm.. this should be a 4 -ml */
473 stat = tp_ops->read_status(hwif);
475 if (OK_STAT(stat, READY_STAT, BUSY_STAT) ||
476 drive->present || cmd == WIN_PIDENTIFY) {
477 /* send cmd and wait */
478 if ((rc = try_to_identify(drive, cmd))) {
479 /* failed: try again */
480 rc = try_to_identify(drive,cmd);
483 stat = tp_ops->read_status(hwif);
485 if (stat == (BUSY_STAT | READY_STAT))
488 if (rc == 1 && cmd == WIN_PIDENTIFY) {
489 printk(KERN_ERR "%s: no response (status = 0x%02x), "
490 "resetting drive\n", drive->name, stat);
494 tp_ops->exec_command(hwif, WIN_SRST);
495 (void)ide_busy_sleep(hwif);
496 rc = try_to_identify(drive, cmd);
499 /* ensure drive IRQ is clear */
500 stat = tp_ops->read_status(hwif);
503 printk(KERN_ERR "%s: no response (status = 0x%02x)\n",
506 /* not present or maybe ATAPI */
509 if (drive->select.b.unit != 0) {
510 /* exit with drive0 selected */
511 SELECT_DRIVE(&hwif->drives[0]);
513 /* ensure drive irq is clear */
514 (void)tp_ops->read_status(hwif);
522 static void enable_nest (ide_drive_t *drive)
524 ide_hwif_t *hwif = HWIF(drive);
525 const struct ide_tp_ops *tp_ops = hwif->tp_ops;
528 printk(KERN_INFO "%s: enabling %s -- ", hwif->name, drive->id->model);
532 tp_ops->exec_command(hwif, EXABYTE_ENABLE_NEST);
534 if (ide_busy_sleep(hwif)) {
535 printk(KERN_CONT "failed (timeout)\n");
541 stat = tp_ops->read_status(hwif);
543 if (!OK_STAT(stat, 0, BAD_STAT))
544 printk(KERN_CONT "failed (status = 0x%02x)\n", stat);
546 printk(KERN_CONT "success\n");
548 /* if !(success||timed-out) */
549 if (do_probe(drive, WIN_IDENTIFY) >= 2) {
550 /* look for ATAPI device */
551 (void) do_probe(drive, WIN_PIDENTIFY);
556 * probe_for_drives - upper level drive probe
557 * @drive: drive to probe for
559 * probe_for_drive() tests for existence of a given drive using do_probe()
560 * and presents things to the user as needed.
562 * Returns: 0 no device was found
563 * 1 device was found (note: drive->present might
567 static inline u8 probe_for_drive (ide_drive_t *drive)
570 * In order to keep things simple we have an id
571 * block for all drives at all times. If the device
572 * is pre ATA or refuses ATA/ATAPI identify we
573 * will add faked data to this.
575 * Also note that 0 everywhere means "can't do X"
578 drive->id = kzalloc(SECTOR_WORDS *4, GFP_KERNEL);
580 if(drive->id == NULL)
582 printk(KERN_ERR "ide: out of memory for id data.\n");
585 strcpy(drive->id->model, "UNKNOWN");
590 /* if !(success||timed-out) */
591 if (do_probe(drive, WIN_IDENTIFY) >= 2) {
592 /* look for ATAPI device */
593 (void) do_probe(drive, WIN_PIDENTIFY);
596 /* drive not found */
598 if (strstr(drive->id->model, "E X A B Y T E N E S T"))
601 /* identification failed? */
602 if (!drive->id_read) {
603 if (drive->media == ide_disk) {
604 printk(KERN_INFO "%s: non-IDE drive, CHS=%d/%d/%d\n",
605 drive->name, drive->cyl,
606 drive->head, drive->sect);
607 } else if (drive->media == ide_cdrom) {
608 printk(KERN_INFO "%s: ATAPI cdrom (?)\n", drive->name);
611 printk(KERN_WARNING "%s: Unknown device on bus refused identification. Ignoring.\n", drive->name);
615 /* drive was found */
619 /* The drive wasn't being helpful. Add generic info only */
620 if (drive->id_read == 0) {
625 if (drive->media == ide_disk) {
626 ide_disk_init_chs(drive);
627 ide_disk_init_mult_count(drive);
630 return drive->present;
633 static void hwif_release_dev(struct device *dev)
635 ide_hwif_t *hwif = container_of(dev, ide_hwif_t, gendev);
637 complete(&hwif->gendev_rel_comp);
640 static int ide_register_port(ide_hwif_t *hwif)
644 /* register with global device tree */
645 strlcpy(hwif->gendev.bus_id,hwif->name,BUS_ID_SIZE);
646 hwif->gendev.driver_data = hwif;
647 if (hwif->gendev.parent == NULL) {
649 hwif->gendev.parent = hwif->dev;
651 /* Would like to do = &device_legacy */
652 hwif->gendev.parent = NULL;
654 hwif->gendev.release = hwif_release_dev;
655 ret = device_register(&hwif->gendev);
657 printk(KERN_WARNING "IDE: %s: device_register error: %d\n",
662 hwif->portdev = device_create_drvdata(ide_port_class, &hwif->gendev,
663 MKDEV(0, 0), hwif, hwif->name);
664 if (IS_ERR(hwif->portdev)) {
665 ret = PTR_ERR(hwif->portdev);
666 device_unregister(&hwif->gendev);
673 * ide_port_wait_ready - wait for port to become ready
676 * This is needed on some PPCs and a bunch of BIOS-less embedded
677 * platforms. Typical cases are:
679 * - The firmware hard reset the disk before booting the kernel,
680 * the drive is still doing it's poweron-reset sequence, that
681 * can take up to 30 seconds.
683 * - The firmware does nothing (or no firmware), the device is
684 * still in POST state (same as above actually).
686 * - Some CD/DVD/Writer combo drives tend to drive the bus during
687 * their reset sequence even when they are non-selected slave
688 * devices, thus preventing discovery of the main HD.
690 * Doing this wait-for-non-busy should not harm any existing
691 * configuration and fix some issues like the above.
695 * Returns 0 on success, error code (< 0) otherwise.
698 static int ide_port_wait_ready(ide_hwif_t *hwif)
702 printk(KERN_DEBUG "Probing IDE interface %s...\n", hwif->name);
704 /* Let HW settle down a bit from whatever init state we
708 /* Wait for BSY bit to go away, spec timeout is 30 seconds,
709 * I know of at least one disk who takes 31 seconds, I use 35
712 rc = ide_wait_not_busy(hwif, 35000);
716 /* Now make sure both master & slave are ready */
717 for (unit = 0; unit < MAX_DRIVES; unit++) {
718 ide_drive_t *drive = &hwif->drives[unit];
720 /* Ignore disks that we will not probe for later. */
721 if (!drive->noprobe || drive->present) {
723 hwif->tp_ops->set_irq(hwif, 1);
725 rc = ide_wait_not_busy(hwif, 35000);
729 printk(KERN_DEBUG "%s: ide_wait_not_busy() skipped\n",
733 /* Exit function with master reselected (let's be sane) */
735 SELECT_DRIVE(&hwif->drives[0]);
741 * ide_undecoded_slave - look for bad CF adapters
744 * Analyse the drives on the interface and attempt to decide if we
745 * have the same drive viewed twice. This occurs with crap CF adapters
746 * and PCMCIA sometimes.
749 void ide_undecoded_slave(ide_drive_t *drive1)
751 ide_drive_t *drive0 = &drive1->hwif->drives[0];
753 if ((drive1->dn & 1) == 0 || drive0->present == 0)
756 /* If the models don't match they are not the same product */
757 if (strcmp(drive0->id->model, drive1->id->model))
760 /* Serial numbers do not match */
761 if (strncmp(drive0->id->serial_no, drive1->id->serial_no, 20))
764 /* No serial number, thankfully very rare for CF */
765 if (drive0->id->serial_no[0] == 0)
768 /* Appears to be an IDE flash adapter with decode bugs */
769 printk(KERN_WARNING "ide-probe: ignoring undecoded slave\n");
774 EXPORT_SYMBOL_GPL(ide_undecoded_slave);
776 static int ide_probe_port(ide_hwif_t *hwif)
780 int unit, rc = -ENODEV;
782 BUG_ON(hwif->present);
784 if (hwif->drives[0].noprobe && hwif->drives[1].noprobe)
788 * We must always disable IRQ, as probe_for_drive will assert IRQ, but
789 * we'll install our IRQ driver much later...
793 disable_irq(hwif->irq);
795 local_irq_set(flags);
797 if (ide_port_wait_ready(hwif) == -EBUSY)
798 printk(KERN_DEBUG "%s: Wait for ready failed before probe !\n", hwif->name);
801 * Second drive should only exist if first drive was found,
802 * but a lot of cdrom drives are configured as single slaves.
804 for (unit = 0; unit < MAX_DRIVES; ++unit) {
805 ide_drive_t *drive = &hwif->drives[unit];
806 drive->dn = (hwif->channel ? 2 : 0) + unit;
807 (void) probe_for_drive(drive);
812 local_irq_restore(flags);
815 * Use cached IRQ number. It might be (and is...) changed by probe
824 static void ide_port_tune_devices(ide_hwif_t *hwif)
826 const struct ide_port_ops *port_ops = hwif->port_ops;
829 for (unit = 0; unit < MAX_DRIVES; unit++) {
830 ide_drive_t *drive = &hwif->drives[unit];
832 if (drive->present && port_ops && port_ops->quirkproc)
833 port_ops->quirkproc(drive);
836 for (unit = 0; unit < MAX_DRIVES; ++unit) {
837 ide_drive_t *drive = &hwif->drives[unit];
839 if (drive->present) {
840 ide_set_max_pio(drive);
849 for (unit = 0; unit < MAX_DRIVES; ++unit) {
850 ide_drive_t *drive = &hwif->drives[unit];
852 if (hwif->host_flags & IDE_HFLAG_NO_IO_32BIT)
853 drive->no_io_32bit = 1;
855 drive->no_io_32bit = drive->id->dword_io ? 1 : 0;
861 * save_match() is used to simplify logic in init_irq() below.
863 * A loophole here is that we may not know about a particular
864 * hwif's irq until after that hwif is actually probed/initialized..
865 * This could be a problem for the case where an hwif is on a
866 * dual interface that requires serialization (eg. cmd640) and another
867 * hwif using one of the same irqs is initialized beforehand.
869 * This routine detects and reports such situations, but does not fix them.
871 static void save_match(ide_hwif_t *hwif, ide_hwif_t *new, ide_hwif_t **match)
873 ide_hwif_t *m = *match;
875 if (m && m->hwgroup && m->hwgroup != new->hwgroup) {
878 printk(KERN_WARNING "%s: potential IRQ problem with %s and %s\n",
879 hwif->name, new->name, m->name);
881 if (!m || m->irq != hwif->irq) /* don't undo a prior perfect match */
884 #endif /* MAX_HWIFS > 1 */
889 static int ide_init_queue(ide_drive_t *drive)
891 struct request_queue *q;
892 ide_hwif_t *hwif = HWIF(drive);
893 int max_sectors = 256;
894 int max_sg_entries = PRD_ENTRIES;
897 * Our default set up assumes the normal IDE case,
898 * that is 64K segmenting, standard PRD setup
899 * and LBA28. Some drivers then impose their own
900 * limits and LBA48 we could raise it but as yet
904 q = blk_init_queue_node(do_ide_request, &ide_lock, hwif_to_node(hwif));
908 q->queuedata = drive;
909 blk_queue_segment_boundary(q, 0xffff);
911 if (hwif->rqsize < max_sectors)
912 max_sectors = hwif->rqsize;
913 blk_queue_max_sectors(q, max_sectors);
916 /* When we have an IOMMU, we may have a problem where pci_map_sg()
917 * creates segments that don't completely match our boundary
918 * requirements and thus need to be broken up again. Because it
919 * doesn't align properly either, we may actually have to break up
920 * to more segments than what was we got in the first place, a max
921 * worst case is twice as many.
922 * This will be fixed once we teach pci_map_sg() about our boundary
923 * requirements, hopefully soon. *FIXME*
925 if (!PCI_DMA_BUS_IS_PHYS)
926 max_sg_entries >>= 1;
927 #endif /* CONFIG_PCI */
929 blk_queue_max_hw_segments(q, max_sg_entries);
930 blk_queue_max_phys_segments(q, max_sg_entries);
932 /* assign drive queue */
935 /* needs drive->queue to be set */
936 ide_toggle_bounce(drive, 1);
941 static void ide_add_drive_to_hwgroup(ide_drive_t *drive)
943 ide_hwgroup_t *hwgroup = drive->hwif->hwgroup;
945 spin_lock_irq(&ide_lock);
946 if (!hwgroup->drive) {
947 /* first drive for hwgroup. */
949 hwgroup->drive = drive;
950 hwgroup->hwif = HWIF(hwgroup->drive);
952 drive->next = hwgroup->drive->next;
953 hwgroup->drive->next = drive;
955 spin_unlock_irq(&ide_lock);
959 * For any present drive:
960 * - allocate the block device queue
961 * - link drive into the hwgroup
963 static void ide_port_setup_devices(ide_hwif_t *hwif)
967 mutex_lock(&ide_cfg_mtx);
968 for (i = 0; i < MAX_DRIVES; i++) {
969 ide_drive_t *drive = &hwif->drives[i];
974 if (ide_init_queue(drive)) {
975 printk(KERN_ERR "ide: failed to init %s\n",
980 ide_add_drive_to_hwgroup(drive);
982 mutex_unlock(&ide_cfg_mtx);
985 static ide_hwif_t *ide_ports[MAX_HWIFS];
987 void ide_remove_port_from_hwgroup(ide_hwif_t *hwif)
989 ide_hwgroup_t *hwgroup = hwif->hwgroup;
991 ide_ports[hwif->index] = NULL;
993 spin_lock_irq(&ide_lock);
995 * Remove us from the hwgroup, and free
996 * the hwgroup if we were the only member
998 if (hwif->next == hwif) {
999 BUG_ON(hwgroup->hwif != hwif);
1002 /* There is another interface in hwgroup.
1003 * Unlink us, and set hwgroup->drive and ->hwif to
1006 ide_hwif_t *g = hwgroup->hwif;
1008 while (g->next != hwif)
1010 g->next = hwif->next;
1011 if (hwgroup->hwif == hwif) {
1012 /* Chose a random hwif for hwgroup->hwif.
1013 * It's guaranteed that there are no drives
1014 * left in the hwgroup.
1016 BUG_ON(hwgroup->drive != NULL);
1019 BUG_ON(hwgroup->hwif == hwif);
1021 spin_unlock_irq(&ide_lock);
1025 * This routine sets up the irq for an ide interface, and creates a new
1026 * hwgroup for the irq/hwif if none was previously assigned.
1028 * Much of the code is for correctly detecting/handling irq sharing
1029 * and irq serialization situations. This is somewhat complex because
1030 * it handles static as well as dynamic (PCMCIA) IDE interfaces.
1032 static int init_irq (ide_hwif_t *hwif)
1034 struct ide_io_ports *io_ports = &hwif->io_ports;
1036 ide_hwgroup_t *hwgroup;
1037 ide_hwif_t *match = NULL;
1040 BUG_ON(in_interrupt());
1041 BUG_ON(irqs_disabled());
1042 BUG_ON(hwif == NULL);
1044 mutex_lock(&ide_cfg_mtx);
1045 hwif->hwgroup = NULL;
1048 * Group up with any other hwifs that share our irq(s).
1050 for (index = 0; index < MAX_HWIFS; index++) {
1051 ide_hwif_t *h = ide_ports[index];
1053 if (h && h->hwgroup) { /* scan only initialized ports */
1054 if (hwif->irq == h->irq) {
1055 hwif->sharing_irq = h->sharing_irq = 1;
1056 if (hwif->chipset != ide_pci ||
1057 h->chipset != ide_pci) {
1058 save_match(hwif, h, &match);
1061 if (hwif->serialized) {
1062 if (hwif->mate && hwif->mate->irq == h->irq)
1063 save_match(hwif, h, &match);
1065 if (h->serialized) {
1066 if (h->mate && hwif->irq == h->mate->irq)
1067 save_match(hwif, h, &match);
1071 #endif /* MAX_HWIFS > 1 */
1073 * If we are still without a hwgroup, then form a new one
1076 hwgroup = match->hwgroup;
1077 hwif->hwgroup = hwgroup;
1079 * Link us into the hwgroup.
1080 * This must be done early, do ensure that unexpected_intr
1081 * can find the hwif and prevent irq storms.
1082 * No drives are attached to the new hwif, choose_drive
1083 * can't do anything stupid (yet).
1084 * Add ourself as the 2nd entry to the hwgroup->hwif
1085 * linked list, the first entry is the hwif that owns
1086 * hwgroup->handler - do not change that.
1088 spin_lock_irq(&ide_lock);
1089 hwif->next = hwgroup->hwif->next;
1090 hwgroup->hwif->next = hwif;
1091 BUG_ON(hwif->next == hwif);
1092 spin_unlock_irq(&ide_lock);
1094 hwgroup = kmalloc_node(sizeof(*hwgroup), GFP_KERNEL|__GFP_ZERO,
1095 hwif_to_node(hwif));
1096 if (hwgroup == NULL)
1099 hwif->hwgroup = hwgroup;
1100 hwgroup->hwif = hwif->next = hwif;
1102 init_timer(&hwgroup->timer);
1103 hwgroup->timer.function = &ide_timer_expiry;
1104 hwgroup->timer.data = (unsigned long) hwgroup;
1107 ide_ports[hwif->index] = hwif;
1110 * Allocate the irq, if not already obtained for another hwif
1112 if (!match || match->irq != hwif->irq) {
1114 #if defined(__mc68000__)
1116 #endif /* __mc68000__ */
1118 if (IDE_CHIPSET_IS_PCI(hwif->chipset))
1121 if (io_ports->ctl_addr)
1122 hwif->tp_ops->set_irq(hwif, 1);
1124 if (request_irq(hwif->irq,&ide_intr,sa,hwif->name,hwgroup))
1128 if (!hwif->rqsize) {
1129 if ((hwif->host_flags & IDE_HFLAG_NO_LBA48) ||
1130 (hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA))
1133 hwif->rqsize = 65536;
1136 #if !defined(__mc68000__)
1137 printk(KERN_INFO "%s at 0x%03lx-0x%03lx,0x%03lx on irq %d", hwif->name,
1138 io_ports->data_addr, io_ports->status_addr,
1139 io_ports->ctl_addr, hwif->irq);
1141 printk(KERN_INFO "%s at 0x%08lx on irq %d", hwif->name,
1142 io_ports->data_addr, hwif->irq);
1143 #endif /* __mc68000__ */
1145 printk(KERN_CONT " (%sed with %s)",
1146 hwif->sharing_irq ? "shar" : "serializ", match->name);
1147 printk(KERN_CONT "\n");
1149 mutex_unlock(&ide_cfg_mtx);
1152 ide_remove_port_from_hwgroup(hwif);
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;
1190 return &disk_to_dev(p)->kobj;
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 ide_proc_unregister_device(drive);
1260 spin_lock_irq(&ide_lock);
1261 ide_remove_drive_from_hwgroup(drive);
1265 /* Messed up locking ... */
1266 spin_unlock_irq(&ide_lock);
1267 blk_cleanup_queue(drive->queue);
1268 spin_lock_irq(&ide_lock);
1269 drive->queue = NULL;
1270 spin_unlock_irq(&ide_lock);
1272 complete(&drive->gendev_rel_comp);
1275 static int hwif_init(ide_hwif_t *hwif)
1280 hwif->irq = __ide_default_irq(hwif->io_ports.data_addr);
1282 printk(KERN_ERR "%s: disabled, no IRQ\n", hwif->name);
1287 if (register_blkdev(hwif->major, hwif->name))
1290 if (!hwif->sg_max_nents)
1291 hwif->sg_max_nents = PRD_ENTRIES;
1293 hwif->sg_table = kmalloc(sizeof(struct scatterlist)*hwif->sg_max_nents,
1295 if (!hwif->sg_table) {
1296 printk(KERN_ERR "%s: unable to allocate SG table.\n", hwif->name);
1300 sg_init_table(hwif->sg_table, hwif->sg_max_nents);
1302 if (init_irq(hwif) == 0)
1305 old_irq = hwif->irq;
1307 * It failed to initialise. Find the default IRQ for
1308 * this port and try that.
1310 hwif->irq = __ide_default_irq(hwif->io_ports.data_addr);
1312 printk(KERN_ERR "%s: disabled, unable to get IRQ %d\n",
1313 hwif->name, old_irq);
1316 if (init_irq(hwif)) {
1317 printk(KERN_ERR "%s: probed IRQ %d and default IRQ %d failed\n",
1318 hwif->name, old_irq, hwif->irq);
1321 printk(KERN_WARNING "%s: probed IRQ %d failed, using default\n",
1322 hwif->name, hwif->irq);
1325 blk_register_region(MKDEV(hwif->major, 0), MAX_DRIVES << PARTN_BITS,
1326 THIS_MODULE, ata_probe, ata_lock, hwif);
1330 unregister_blkdev(hwif->major, hwif->name);
1334 static void hwif_register_devices(ide_hwif_t *hwif)
1338 for (i = 0; i < MAX_DRIVES; i++) {
1339 ide_drive_t *drive = &hwif->drives[i];
1340 struct device *dev = &drive->gendev;
1343 if (!drive->present)
1346 ide_add_generic_settings(drive);
1348 snprintf(dev->bus_id, BUS_ID_SIZE, "%u.%u", hwif->index, i);
1349 dev->parent = &hwif->gendev;
1350 dev->bus = &ide_bus_type;
1351 dev->driver_data = drive;
1352 dev->release = drive_release_dev;
1354 ret = device_register(dev);
1356 printk(KERN_WARNING "IDE: %s: device_register error: "
1357 "%d\n", __func__, ret);
1361 static void ide_port_init_devices(ide_hwif_t *hwif)
1363 const struct ide_port_ops *port_ops = hwif->port_ops;
1366 for (i = 0; i < MAX_DRIVES; i++) {
1367 ide_drive_t *drive = &hwif->drives[i];
1369 if (hwif->host_flags & IDE_HFLAG_IO_32BIT)
1370 drive->io_32bit = 1;
1371 if (hwif->host_flags & IDE_HFLAG_UNMASK_IRQS)
1373 if (hwif->host_flags & IDE_HFLAG_NO_UNMASK_IRQS)
1374 drive->no_unmask = 1;
1376 if (port_ops && port_ops->init_dev)
1377 port_ops->init_dev(drive);
1381 static void ide_init_port(ide_hwif_t *hwif, unsigned int port,
1382 const struct ide_port_info *d)
1384 hwif->channel = port;
1387 hwif->chipset = d->chipset;
1392 if ((!hwif->irq && (d->host_flags & IDE_HFLAG_LEGACY_IRQS)) ||
1393 (d->host_flags & IDE_HFLAG_FORCE_LEGACY_IRQS))
1394 hwif->irq = port ? 15 : 14;
1396 /* ->host_flags may be set by ->init_iops (or even earlier...) */
1397 hwif->host_flags |= d->host_flags;
1398 hwif->pio_mask = d->pio_mask;
1401 hwif->tp_ops = d->tp_ops;
1403 /* ->set_pio_mode for DTC2278 is currently limited to port 0 */
1404 if (hwif->chipset != ide_dtc2278 || hwif->channel == 0)
1405 hwif->port_ops = d->port_ops;
1407 hwif->swdma_mask = d->swdma_mask;
1408 hwif->mwdma_mask = d->mwdma_mask;
1409 hwif->ultra_mask = d->udma_mask;
1411 if ((d->host_flags & IDE_HFLAG_NO_DMA) == 0) {
1415 rc = d->init_dma(hwif, d);
1417 rc = ide_hwif_setup_dma(hwif, d);
1420 printk(KERN_INFO "%s: DMA disabled\n", hwif->name);
1422 hwif->swdma_mask = 0;
1423 hwif->mwdma_mask = 0;
1424 hwif->ultra_mask = 0;
1425 } else if (d->dma_ops)
1426 hwif->dma_ops = d->dma_ops;
1429 if ((d->host_flags & IDE_HFLAG_SERIALIZE) ||
1430 ((d->host_flags & IDE_HFLAG_SERIALIZE_DMA) && hwif->dma_base)) {
1432 hwif->mate->serialized = hwif->serialized = 1;
1435 if (d->host_flags & IDE_HFLAG_RQSIZE_256)
1438 /* call chipset specific routine for each enabled port */
1443 static void ide_port_cable_detect(ide_hwif_t *hwif)
1445 const struct ide_port_ops *port_ops = hwif->port_ops;
1447 if (port_ops && port_ops->cable_detect && (hwif->ultra_mask & 0x78)) {
1448 if (hwif->cbl != ATA_CBL_PATA40_SHORT)
1449 hwif->cbl = port_ops->cable_detect(hwif);
1453 static ssize_t store_delete_devices(struct device *portdev,
1454 struct device_attribute *attr,
1455 const char *buf, size_t n)
1457 ide_hwif_t *hwif = dev_get_drvdata(portdev);
1459 if (strncmp(buf, "1", n))
1462 ide_port_unregister_devices(hwif);
1467 static DEVICE_ATTR(delete_devices, S_IWUSR, NULL, store_delete_devices);
1469 static ssize_t store_scan(struct device *portdev,
1470 struct device_attribute *attr,
1471 const char *buf, size_t n)
1473 ide_hwif_t *hwif = dev_get_drvdata(portdev);
1475 if (strncmp(buf, "1", n))
1478 ide_port_unregister_devices(hwif);
1479 ide_port_scan(hwif);
1484 static DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
1486 static struct device_attribute *ide_port_attrs[] = {
1487 &dev_attr_delete_devices,
1492 static int ide_sysfs_register_port(ide_hwif_t *hwif)
1494 int i, uninitialized_var(rc);
1496 for (i = 0; ide_port_attrs[i]; i++) {
1497 rc = device_create_file(hwif->portdev, ide_port_attrs[i]);
1505 static unsigned int ide_indexes;
1508 * ide_find_port_slot - find free port slot
1511 * Return the new port slot index or -ENOENT if we are out of free slots.
1514 static int ide_find_port_slot(const struct ide_port_info *d)
1517 u8 bootable = (d && (d->host_flags & IDE_HFLAG_NON_BOOTABLE)) ? 0 : 1;
1518 u8 i = (d && (d->host_flags & IDE_HFLAG_QD_2ND_PORT)) ? 1 : 0;;
1521 * Claim an unassigned slot.
1523 * Give preference to claiming other slots before claiming ide0/ide1,
1524 * just in case there's another interface yet-to-be-scanned
1525 * which uses ports 0x1f0/0x170 (the ide0/ide1 defaults).
1527 * Unless there is a bootable card that does not use the standard
1528 * ports 0x1f0/0x170 (the ide0/ide1 defaults).
1530 mutex_lock(&ide_cfg_mtx);
1531 if (MAX_HWIFS == 1) {
1532 if (ide_indexes == 0 && i == 0)
1536 if ((ide_indexes | i) != (1 << MAX_HWIFS) - 1)
1537 idx = ffz(ide_indexes | i);
1539 if ((ide_indexes | 3) != (1 << MAX_HWIFS) - 1)
1540 idx = ffz(ide_indexes | 3);
1541 else if ((ide_indexes & 3) != 3)
1542 idx = ffz(ide_indexes);
1546 ide_indexes |= (1 << idx);
1547 mutex_unlock(&ide_cfg_mtx);
1552 static void ide_free_port_slot(int idx)
1554 mutex_lock(&ide_cfg_mtx);
1555 ide_indexes &= ~(1 << idx);
1556 mutex_unlock(&ide_cfg_mtx);
1559 struct ide_host *ide_host_alloc_all(const struct ide_port_info *d,
1562 struct ide_host *host;
1565 host = kzalloc(sizeof(*host), GFP_KERNEL);
1569 for (i = 0; i < MAX_HWIFS; i++) {
1576 hwif = kzalloc(sizeof(*hwif), GFP_KERNEL);
1580 idx = ide_find_port_slot(d);
1582 printk(KERN_ERR "%s: no free slot for interface\n",
1583 d ? d->name : "ide");
1588 ide_init_port_data(hwif, idx);
1592 host->ports[i] = hwif;
1596 if (host->n_ports == 0) {
1602 host->dev[0] = hws[0]->dev;
1605 host->host_flags = d->host_flags;
1609 EXPORT_SYMBOL_GPL(ide_host_alloc_all);
1611 struct ide_host *ide_host_alloc(const struct ide_port_info *d, hw_regs_t **hws)
1613 hw_regs_t *hws_all[MAX_HWIFS];
1616 for (i = 0; i < MAX_HWIFS; i++)
1617 hws_all[i] = (i < 4) ? hws[i] : NULL;
1619 return ide_host_alloc_all(d, hws_all);
1621 EXPORT_SYMBOL_GPL(ide_host_alloc);
1623 int ide_host_register(struct ide_host *host, const struct ide_port_info *d,
1626 ide_hwif_t *hwif, *mate = NULL;
1629 for (i = 0; i < MAX_HWIFS; i++) {
1630 hwif = host->ports[i];
1637 ide_init_port_hw(hwif, hws[i]);
1638 ide_port_apply_params(hwif);
1645 if ((i & 1) && mate) {
1650 mate = (i & 1) ? NULL : hwif;
1652 ide_init_port(hwif, i & 1, d);
1653 ide_port_cable_detect(hwif);
1654 ide_port_init_devices(hwif);
1657 for (i = 0; i < MAX_HWIFS; i++) {
1658 hwif = host->ports[i];
1663 if (ide_probe_port(hwif) == 0)
1666 if (hwif->chipset != ide_4drives || !hwif->mate ||
1667 !hwif->mate->present)
1668 ide_register_port(hwif);
1671 ide_port_tune_devices(hwif);
1674 for (i = 0; i < MAX_HWIFS; i++) {
1675 hwif = host->ports[i];
1680 if (hwif_init(hwif) == 0) {
1681 printk(KERN_INFO "%s: failed to initialize IDE "
1682 "interface\n", hwif->name);
1690 ide_port_setup_devices(hwif);
1692 ide_acpi_init(hwif);
1695 ide_acpi_port_init_devices(hwif);
1698 for (i = 0; i < MAX_HWIFS; i++) {
1699 hwif = host->ports[i];
1704 if (hwif->chipset == ide_unknown)
1705 hwif->chipset = ide_generic;
1708 hwif_register_devices(hwif);
1711 for (i = 0; i < MAX_HWIFS; i++) {
1712 hwif = host->ports[i];
1717 ide_sysfs_register_port(hwif);
1718 ide_proc_register_port(hwif);
1721 ide_proc_port_register_devices(hwif);
1726 EXPORT_SYMBOL_GPL(ide_host_register);
1728 int ide_host_add(const struct ide_port_info *d, hw_regs_t **hws,
1729 struct ide_host **hostp)
1731 struct ide_host *host;
1734 host = ide_host_alloc(d, hws);
1738 rc = ide_host_register(host, d, hws);
1740 ide_host_free(host);
1749 EXPORT_SYMBOL_GPL(ide_host_add);
1751 void ide_host_free(struct ide_host *host)
1756 for (i = 0; i < MAX_HWIFS; i++) {
1757 hwif = host->ports[i];
1762 ide_free_port_slot(hwif->index);
1768 EXPORT_SYMBOL_GPL(ide_host_free);
1770 void ide_host_remove(struct ide_host *host)
1774 for (i = 0; i < MAX_HWIFS; i++) {
1776 ide_unregister(host->ports[i]);
1779 ide_host_free(host);
1781 EXPORT_SYMBOL_GPL(ide_host_remove);
1783 void ide_port_scan(ide_hwif_t *hwif)
1785 ide_port_apply_params(hwif);
1786 ide_port_cable_detect(hwif);
1787 ide_port_init_devices(hwif);
1789 if (ide_probe_port(hwif) < 0)
1794 ide_port_tune_devices(hwif);
1795 ide_acpi_port_init_devices(hwif);
1796 ide_port_setup_devices(hwif);
1797 hwif_register_devices(hwif);
1798 ide_proc_port_register_devices(hwif);
1800 EXPORT_SYMBOL_GPL(ide_port_scan);
1802 static void ide_legacy_init_one(hw_regs_t **hws, hw_regs_t *hw,
1803 u8 port_no, const struct ide_port_info *d,
1804 unsigned long config)
1806 unsigned long base, ctl;
1819 if (!request_region(base, 8, d->name)) {
1820 printk(KERN_ERR "%s: I/O resource 0x%lX-0x%lX not free.\n",
1821 d->name, base, base + 7);
1825 if (!request_region(ctl, 1, d->name)) {
1826 printk(KERN_ERR "%s: I/O resource 0x%lX not free.\n",
1828 release_region(base, 8);
1832 ide_std_init_ports(hw, base, ctl);
1834 hw->chipset = d->chipset;
1835 hw->config = config;
1840 int ide_legacy_device_add(const struct ide_port_info *d, unsigned long config)
1842 hw_regs_t hw[2], *hws[] = { NULL, NULL, NULL, NULL };
1844 memset(&hw, 0, sizeof(hw));
1846 if ((d->host_flags & IDE_HFLAG_QD_2ND_PORT) == 0)
1847 ide_legacy_init_one(hws, &hw[0], 0, d, config);
1848 ide_legacy_init_one(hws, &hw[1], 1, d, config);
1850 if (hws[0] == NULL && hws[1] == NULL &&
1851 (d->host_flags & IDE_HFLAG_SINGLE))
1854 return ide_host_add(d, hws, NULL);
1856 EXPORT_SYMBOL_GPL(ide_legacy_device_add);