[ARP]: neigh_parms_put(destroy) are essentially local to core/neighbour.c.
[linux-2.6/libata-dev.git] / drivers / ide / ide-probe.c
blobedf650b20c67b87782b12a09488e612de03250f9
1 /*
2 * linux/drivers/ide/ide-probe.c Version 1.11 Mar 05, 2003
4 * Copyright (C) 1994-1998 Linus Torvalds & authors (see below)
5 */
7 /*
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 * -- increase WAIT_PIDENTIFY to avoid CD-ROM locking at boot
17 * by Andrea Arcangeli
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/string.h>
23 #include <linux/kernel.h>
24 #include <linux/timer.h>
25 #include <linux/mm.h>
26 #include <linux/interrupt.h>
27 #include <linux/major.h>
28 #include <linux/errno.h>
29 #include <linux/genhd.h>
30 #include <linux/slab.h>
31 #include <linux/delay.h>
32 #include <linux/ide.h>
33 #include <linux/spinlock.h>
34 #include <linux/kmod.h>
35 #include <linux/pci.h>
36 #include <linux/scatterlist.h>
38 #include <asm/byteorder.h>
39 #include <asm/irq.h>
40 #include <asm/uaccess.h>
41 #include <asm/io.h>
43 /**
44 * generic_id - add a generic drive id
45 * @drive: drive to make an ID block for
47 * Add a fake id field to the drive we are passed. This allows
48 * use to skip a ton of NULL checks (which people always miss)
49 * and make drive properties unconditional outside of this file
52 static void generic_id(ide_drive_t *drive)
54 drive->id->cyls = drive->cyl;
55 drive->id->heads = drive->head;
56 drive->id->sectors = drive->sect;
57 drive->id->cur_cyls = drive->cyl;
58 drive->id->cur_heads = drive->head;
59 drive->id->cur_sectors = drive->sect;
62 static void ide_disk_init_chs(ide_drive_t *drive)
64 struct hd_driveid *id = drive->id;
66 /* Extract geometry if we did not already have one for the drive */
67 if (!drive->cyl || !drive->head || !drive->sect) {
68 drive->cyl = drive->bios_cyl = id->cyls;
69 drive->head = drive->bios_head = id->heads;
70 drive->sect = drive->bios_sect = id->sectors;
73 /* Handle logical geometry translation by the drive */
74 if ((id->field_valid & 1) && id->cur_cyls &&
75 id->cur_heads && (id->cur_heads <= 16) && id->cur_sectors) {
76 drive->cyl = id->cur_cyls;
77 drive->head = id->cur_heads;
78 drive->sect = id->cur_sectors;
81 /* Use physical geometry if what we have still makes no sense */
82 if (drive->head > 16 && id->heads && id->heads <= 16) {
83 drive->cyl = id->cyls;
84 drive->head = id->heads;
85 drive->sect = id->sectors;
89 static void ide_disk_init_mult_count(ide_drive_t *drive)
91 struct hd_driveid *id = drive->id;
93 drive->mult_count = 0;
94 if (id->max_multsect) {
95 #ifdef CONFIG_IDEDISK_MULTI_MODE
96 id->multsect = ((id->max_multsect/2) > 1) ? id->max_multsect : 0;
97 id->multsect_valid = id->multsect ? 1 : 0;
98 drive->mult_req = id->multsect_valid ? id->max_multsect : 0;
99 drive->special.b.set_multmode = drive->mult_req ? 1 : 0;
100 #else /* original, pre IDE-NFG, per request of AC */
101 drive->mult_req = 0;
102 if (drive->mult_req > id->max_multsect)
103 drive->mult_req = id->max_multsect;
104 if (drive->mult_req || ((id->multsect_valid & 1) && id->multsect))
105 drive->special.b.set_multmode = 1;
106 #endif
111 * do_identify - identify a drive
112 * @drive: drive to identify
113 * @cmd: command used
115 * Called when we have issued a drive identify command to
116 * read and parse the results. This function is run with
117 * interrupts disabled.
120 static inline void do_identify (ide_drive_t *drive, u8 cmd)
122 ide_hwif_t *hwif = HWIF(drive);
123 int bswap = 1;
124 struct hd_driveid *id;
126 id = drive->id;
127 /* read 512 bytes of id info */
128 hwif->ata_input_data(drive, id, SECTOR_WORDS);
130 drive->id_read = 1;
131 local_irq_enable();
132 ide_fix_driveid(id);
134 #if defined (CONFIG_SCSI_EATA_PIO) || defined (CONFIG_SCSI_EATA)
136 * EATA SCSI controllers do a hardware ATA emulation:
137 * Ignore them if there is a driver for them available.
139 if ((id->model[0] == 'P' && id->model[1] == 'M') ||
140 (id->model[0] == 'S' && id->model[1] == 'K')) {
141 printk("%s: EATA SCSI HBA %.10s\n", drive->name, id->model);
142 goto err_misc;
144 #endif /* CONFIG_SCSI_EATA || CONFIG_SCSI_EATA_PIO */
147 * WIN_IDENTIFY returns little-endian info,
148 * WIN_PIDENTIFY *usually* returns little-endian info.
150 if (cmd == WIN_PIDENTIFY) {
151 if ((id->model[0] == 'N' && id->model[1] == 'E') /* NEC */
152 || (id->model[0] == 'F' && id->model[1] == 'X') /* Mitsumi */
153 || (id->model[0] == 'P' && id->model[1] == 'i'))/* Pioneer */
154 /* Vertos drives may still be weird */
155 bswap ^= 1;
157 ide_fixstring(id->model, sizeof(id->model), bswap);
158 ide_fixstring(id->fw_rev, sizeof(id->fw_rev), bswap);
159 ide_fixstring(id->serial_no, sizeof(id->serial_no), bswap);
161 /* we depend on this a lot! */
162 id->model[sizeof(id->model)-1] = '\0';
164 if (strstr(id->model, "E X A B Y T E N E S T"))
165 goto err_misc;
167 printk("%s: %s, ", drive->name, id->model);
168 drive->present = 1;
169 drive->dead = 0;
172 * Check for an ATAPI device
174 if (cmd == WIN_PIDENTIFY) {
175 u8 type = (id->config >> 8) & 0x1f;
176 printk("ATAPI ");
177 switch (type) {
178 case ide_floppy:
179 if (!strstr(id->model, "CD-ROM")) {
180 if (!strstr(id->model, "oppy") &&
181 !strstr(id->model, "poyp") &&
182 !strstr(id->model, "ZIP"))
183 printk("cdrom or floppy?, assuming ");
184 if (drive->media != ide_cdrom) {
185 printk ("FLOPPY");
186 drive->removable = 1;
187 break;
190 /* Early cdrom models used zero */
191 type = ide_cdrom;
192 case ide_cdrom:
193 drive->removable = 1;
194 #ifdef CONFIG_PPC
195 /* kludge for Apple PowerBook internal zip */
196 if (!strstr(id->model, "CD-ROM") &&
197 strstr(id->model, "ZIP")) {
198 printk ("FLOPPY");
199 type = ide_floppy;
200 break;
202 #endif
203 printk ("CD/DVD-ROM");
204 break;
205 case ide_tape:
206 printk ("TAPE");
207 break;
208 case ide_optical:
209 printk ("OPTICAL");
210 drive->removable = 1;
211 break;
212 default:
213 printk("UNKNOWN (type %d)", type);
214 break;
216 printk (" drive\n");
217 drive->media = type;
218 /* an ATAPI device ignores DRDY */
219 drive->ready_stat = 0;
220 return;
224 * Not an ATAPI device: looks like a "regular" hard disk
228 * 0x848a = CompactFlash device
229 * These are *not* removable in Linux definition of the term
232 if ((id->config != 0x848a) && (id->config & (1<<7)))
233 drive->removable = 1;
235 drive->media = ide_disk;
236 printk("%s DISK drive\n", (id->config == 0x848a) ? "CFA" : "ATA" );
238 return;
240 err_misc:
241 kfree(id);
242 drive->present = 0;
243 return;
247 * actual_try_to_identify - send ata/atapi identify
248 * @drive: drive to identify
249 * @cmd: command to use
251 * try_to_identify() sends an ATA(PI) IDENTIFY request to a drive
252 * and waits for a response. It also monitors irqs while this is
253 * happening, in hope of automatically determining which one is
254 * being used by the interface.
256 * Returns: 0 device was identified
257 * 1 device timed-out (no response to identify request)
258 * 2 device aborted the command (refused to identify itself)
261 static int actual_try_to_identify (ide_drive_t *drive, u8 cmd)
263 ide_hwif_t *hwif = HWIF(drive);
264 int rc;
265 unsigned long hd_status;
266 unsigned long timeout;
267 u8 s = 0, a = 0;
269 /* take a deep breath */
270 msleep(50);
272 if (IDE_CONTROL_REG) {
273 a = hwif->INB(IDE_ALTSTATUS_REG);
274 s = hwif->INB(IDE_STATUS_REG);
275 if ((a ^ s) & ~INDEX_STAT) {
276 printk(KERN_INFO "%s: probing with STATUS(0x%02x) instead of "
277 "ALTSTATUS(0x%02x)\n", drive->name, s, a);
278 /* ancient Seagate drives, broken interfaces */
279 hd_status = IDE_STATUS_REG;
280 } else {
281 /* use non-intrusive polling */
282 hd_status = IDE_ALTSTATUS_REG;
284 } else
285 hd_status = IDE_STATUS_REG;
287 /* set features register for atapi
288 * identify command to be sure of reply
290 if ((cmd == WIN_PIDENTIFY))
291 /* disable dma & overlap */
292 hwif->OUTB(0, IDE_FEATURE_REG);
294 /* ask drive for ID */
295 hwif->OUTB(cmd, IDE_COMMAND_REG);
297 timeout = ((cmd == WIN_IDENTIFY) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
298 timeout += jiffies;
299 do {
300 if (time_after(jiffies, timeout)) {
301 /* drive timed-out */
302 return 1;
304 /* give drive a breather */
305 msleep(50);
306 } while ((hwif->INB(hd_status)) & BUSY_STAT);
308 /* wait for IRQ and DRQ_STAT */
309 msleep(50);
310 if (OK_STAT((hwif->INB(IDE_STATUS_REG)), DRQ_STAT, BAD_R_STAT)) {
311 unsigned long flags;
313 /* local CPU only; some systems need this */
314 local_irq_save(flags);
315 /* drive returned ID */
316 do_identify(drive, cmd);
317 /* drive responded with ID */
318 rc = 0;
319 /* clear drive IRQ */
320 (void) hwif->INB(IDE_STATUS_REG);
321 local_irq_restore(flags);
322 } else {
323 /* drive refused ID */
324 rc = 2;
326 return rc;
330 * try_to_identify - try to identify a drive
331 * @drive: drive to probe
332 * @cmd: command to use
334 * Issue the identify command and then do IRQ probing to
335 * complete the identification when needed by finding the
336 * IRQ the drive is attached to
339 static int try_to_identify (ide_drive_t *drive, u8 cmd)
341 ide_hwif_t *hwif = HWIF(drive);
342 int retval;
343 int autoprobe = 0;
344 unsigned long cookie = 0;
347 * Disable device irq unless we need to
348 * probe for it. Otherwise we'll get spurious
349 * interrupts during the identify-phase that
350 * the irq handler isn't expecting.
352 if (IDE_CONTROL_REG) {
353 if (!hwif->irq) {
354 autoprobe = 1;
355 cookie = probe_irq_on();
357 ide_set_irq(drive, autoprobe);
360 retval = actual_try_to_identify(drive, cmd);
362 if (autoprobe) {
363 int irq;
365 ide_set_irq(drive, 0);
366 /* clear drive IRQ */
367 (void) hwif->INB(IDE_STATUS_REG);
368 udelay(5);
369 irq = probe_irq_off(cookie);
370 if (!hwif->irq) {
371 if (irq > 0) {
372 hwif->irq = irq;
373 } else {
374 /* Mmmm.. multiple IRQs..
375 * don't know which was ours
377 printk("%s: IRQ probe failed (0x%lx)\n",
378 drive->name, cookie);
382 return retval;
385 static int ide_busy_sleep(ide_hwif_t *hwif)
387 unsigned long timeout = jiffies + WAIT_WORSTCASE;
388 u8 stat;
390 do {
391 msleep(50);
392 stat = hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
393 if ((stat & BUSY_STAT) == 0)
394 return 0;
395 } while (time_before(jiffies, timeout));
397 return 1;
401 * do_probe - probe an IDE device
402 * @drive: drive to probe
403 * @cmd: command to use
405 * do_probe() has the difficult job of finding a drive if it exists,
406 * without getting hung up if it doesn't exist, without trampling on
407 * ethernet cards, and without leaving any IRQs dangling to haunt us later.
409 * If a drive is "known" to exist (from CMOS or kernel parameters),
410 * but does not respond right away, the probe will "hang in there"
411 * for the maximum wait time (about 30 seconds), otherwise it will
412 * exit much more quickly.
414 * Returns: 0 device was identified
415 * 1 device timed-out (no response to identify request)
416 * 2 device aborted the command (refused to identify itself)
417 * 3 bad status from device (possible for ATAPI drives)
418 * 4 probe was not attempted because failure was obvious
421 static int do_probe (ide_drive_t *drive, u8 cmd)
423 int rc;
424 ide_hwif_t *hwif = HWIF(drive);
426 if (drive->present) {
427 /* avoid waiting for inappropriate probes */
428 if ((drive->media != ide_disk) && (cmd == WIN_IDENTIFY))
429 return 4;
431 #ifdef DEBUG
432 printk("probing for %s: present=%d, media=%d, probetype=%s\n",
433 drive->name, drive->present, drive->media,
434 (cmd == WIN_IDENTIFY) ? "ATA" : "ATAPI");
435 #endif
437 /* needed for some systems
438 * (e.g. crw9624 as drive0 with disk as slave)
440 msleep(50);
441 SELECT_DRIVE(drive);
442 msleep(50);
443 if (hwif->INB(IDE_SELECT_REG) != drive->select.all && !drive->present) {
444 if (drive->select.b.unit != 0) {
445 /* exit with drive0 selected */
446 SELECT_DRIVE(&hwif->drives[0]);
447 /* allow BUSY_STAT to assert & clear */
448 msleep(50);
450 /* no i/f present: mmm.. this should be a 4 -ml */
451 return 3;
454 if (OK_STAT((hwif->INB(IDE_STATUS_REG)), READY_STAT, BUSY_STAT) ||
455 drive->present || cmd == WIN_PIDENTIFY) {
456 /* send cmd and wait */
457 if ((rc = try_to_identify(drive, cmd))) {
458 /* failed: try again */
459 rc = try_to_identify(drive,cmd);
461 if (hwif->INB(IDE_STATUS_REG) == (BUSY_STAT|READY_STAT))
462 return 4;
464 if ((rc == 1 && cmd == WIN_PIDENTIFY) &&
465 ((drive->autotune == IDE_TUNE_DEFAULT) ||
466 (drive->autotune == IDE_TUNE_AUTO))) {
467 printk("%s: no response (status = 0x%02x), "
468 "resetting drive\n", drive->name,
469 hwif->INB(IDE_STATUS_REG));
470 msleep(50);
471 hwif->OUTB(drive->select.all, IDE_SELECT_REG);
472 msleep(50);
473 hwif->OUTB(WIN_SRST, IDE_COMMAND_REG);
474 (void)ide_busy_sleep(hwif);
475 rc = try_to_identify(drive, cmd);
477 if (rc == 1)
478 printk("%s: no response (status = 0x%02x)\n",
479 drive->name, hwif->INB(IDE_STATUS_REG));
480 /* ensure drive irq is clear */
481 (void) hwif->INB(IDE_STATUS_REG);
482 } else {
483 /* not present or maybe ATAPI */
484 rc = 3;
486 if (drive->select.b.unit != 0) {
487 /* exit with drive0 selected */
488 SELECT_DRIVE(&hwif->drives[0]);
489 msleep(50);
490 /* ensure drive irq is clear */
491 (void) hwif->INB(IDE_STATUS_REG);
493 return rc;
499 static void enable_nest (ide_drive_t *drive)
501 ide_hwif_t *hwif = HWIF(drive);
503 printk("%s: enabling %s -- ", hwif->name, drive->id->model);
504 SELECT_DRIVE(drive);
505 msleep(50);
506 hwif->OUTB(EXABYTE_ENABLE_NEST, IDE_COMMAND_REG);
508 if (ide_busy_sleep(hwif)) {
509 printk(KERN_CONT "failed (timeout)\n");
510 return;
513 msleep(50);
515 if (!OK_STAT((hwif->INB(IDE_STATUS_REG)), 0, BAD_STAT)) {
516 printk("failed (status = 0x%02x)\n", hwif->INB(IDE_STATUS_REG));
517 } else {
518 printk("success\n");
521 /* if !(success||timed-out) */
522 if (do_probe(drive, WIN_IDENTIFY) >= 2) {
523 /* look for ATAPI device */
524 (void) do_probe(drive, WIN_PIDENTIFY);
529 * probe_for_drives - upper level drive probe
530 * @drive: drive to probe for
532 * probe_for_drive() tests for existence of a given drive using do_probe()
533 * and presents things to the user as needed.
535 * Returns: 0 no device was found
536 * 1 device was found (note: drive->present might
537 * still be 0)
540 static inline u8 probe_for_drive (ide_drive_t *drive)
543 * In order to keep things simple we have an id
544 * block for all drives at all times. If the device
545 * is pre ATA or refuses ATA/ATAPI identify we
546 * will add faked data to this.
548 * Also note that 0 everywhere means "can't do X"
551 drive->id = kzalloc(SECTOR_WORDS *4, GFP_KERNEL);
552 drive->id_read = 0;
553 if(drive->id == NULL)
555 printk(KERN_ERR "ide: out of memory for id data.\n");
556 return 0;
558 strcpy(drive->id->model, "UNKNOWN");
560 /* skip probing? */
561 if (!drive->noprobe)
563 /* if !(success||timed-out) */
564 if (do_probe(drive, WIN_IDENTIFY) >= 2) {
565 /* look for ATAPI device */
566 (void) do_probe(drive, WIN_PIDENTIFY);
568 if (!drive->present)
569 /* drive not found */
570 return 0;
571 if (strstr(drive->id->model, "E X A B Y T E N E S T"))
572 enable_nest(drive);
574 /* identification failed? */
575 if (!drive->id_read) {
576 if (drive->media == ide_disk) {
577 printk(KERN_INFO "%s: non-IDE drive, CHS=%d/%d/%d\n",
578 drive->name, drive->cyl,
579 drive->head, drive->sect);
580 } else if (drive->media == ide_cdrom) {
581 printk(KERN_INFO "%s: ATAPI cdrom (?)\n", drive->name);
582 } else {
583 /* nuke it */
584 printk(KERN_WARNING "%s: Unknown device on bus refused identification. Ignoring.\n", drive->name);
585 drive->present = 0;
588 /* drive was found */
590 if(!drive->present)
591 return 0;
592 /* The drive wasn't being helpful. Add generic info only */
593 if (drive->id_read == 0) {
594 generic_id(drive);
595 return 1;
598 if (drive->media == ide_disk) {
599 ide_disk_init_chs(drive);
600 ide_disk_init_mult_count(drive);
603 return drive->present;
606 static void hwif_release_dev (struct device *dev)
608 ide_hwif_t *hwif = container_of(dev, ide_hwif_t, gendev);
610 complete(&hwif->gendev_rel_comp);
613 static void hwif_register (ide_hwif_t *hwif)
615 int ret;
617 /* register with global device tree */
618 strlcpy(hwif->gendev.bus_id,hwif->name,BUS_ID_SIZE);
619 hwif->gendev.driver_data = hwif;
620 if (hwif->gendev.parent == NULL) {
621 if (hwif->pci_dev)
622 hwif->gendev.parent = &hwif->pci_dev->dev;
623 else
624 /* Would like to do = &device_legacy */
625 hwif->gendev.parent = NULL;
627 hwif->gendev.release = hwif_release_dev;
628 ret = device_register(&hwif->gendev);
629 if (ret < 0)
630 printk(KERN_WARNING "IDE: %s: device_register error: %d\n",
631 __FUNCTION__, ret);
634 static int wait_hwif_ready(ide_hwif_t *hwif)
636 int unit, rc;
638 printk(KERN_DEBUG "Probing IDE interface %s...\n", hwif->name);
640 /* Let HW settle down a bit from whatever init state we
641 * come from */
642 mdelay(2);
644 /* Wait for BSY bit to go away, spec timeout is 30 seconds,
645 * I know of at least one disk who takes 31 seconds, I use 35
646 * here to be safe
648 rc = ide_wait_not_busy(hwif, 35000);
649 if (rc)
650 return rc;
652 /* Now make sure both master & slave are ready */
653 for (unit = 0; unit < MAX_DRIVES; unit++) {
654 ide_drive_t *drive = &hwif->drives[unit];
656 /* Ignore disks that we will not probe for later. */
657 if (!drive->noprobe || drive->present) {
658 SELECT_DRIVE(drive);
659 ide_set_irq(drive, 1);
660 mdelay(2);
661 rc = ide_wait_not_busy(hwif, 35000);
662 if (rc)
663 goto out;
664 } else
665 printk(KERN_DEBUG "%s: ide_wait_not_busy() skipped\n",
666 drive->name);
668 out:
669 /* Exit function with master reselected (let's be sane) */
670 if (unit)
671 SELECT_DRIVE(&hwif->drives[0]);
673 return rc;
677 * ide_undecoded_slave - look for bad CF adapters
678 * @drive1: drive
680 * Analyse the drives on the interface and attempt to decide if we
681 * have the same drive viewed twice. This occurs with crap CF adapters
682 * and PCMCIA sometimes.
685 void ide_undecoded_slave(ide_drive_t *drive1)
687 ide_drive_t *drive0 = &drive1->hwif->drives[0];
689 if ((drive1->dn & 1) == 0 || drive0->present == 0)
690 return;
692 /* If the models don't match they are not the same product */
693 if (strcmp(drive0->id->model, drive1->id->model))
694 return;
696 /* Serial numbers do not match */
697 if (strncmp(drive0->id->serial_no, drive1->id->serial_no, 20))
698 return;
700 /* No serial number, thankfully very rare for CF */
701 if (drive0->id->serial_no[0] == 0)
702 return;
704 /* Appears to be an IDE flash adapter with decode bugs */
705 printk(KERN_WARNING "ide-probe: ignoring undecoded slave\n");
707 drive1->present = 0;
710 EXPORT_SYMBOL_GPL(ide_undecoded_slave);
713 * This routine only knows how to look for drive units 0 and 1
714 * on an interface, so any setting of MAX_DRIVES > 2 won't work here.
716 static void probe_hwif(ide_hwif_t *hwif)
718 unsigned long flags;
719 unsigned int irqd;
720 int unit;
722 if (hwif->noprobe)
723 return;
725 if ((hwif->chipset != ide_4drives || !hwif->mate || !hwif->mate->present) &&
726 (ide_hwif_request_regions(hwif))) {
727 u16 msgout = 0;
728 for (unit = 0; unit < MAX_DRIVES; ++unit) {
729 ide_drive_t *drive = &hwif->drives[unit];
730 if (drive->present) {
731 drive->present = 0;
732 printk(KERN_ERR "%s: ERROR, PORTS ALREADY IN USE\n",
733 drive->name);
734 msgout = 1;
737 if (!msgout)
738 printk(KERN_ERR "%s: ports already in use, skipping probe\n",
739 hwif->name);
740 return;
744 * We must always disable IRQ, as probe_for_drive will assert IRQ, but
745 * we'll install our IRQ driver much later...
747 irqd = hwif->irq;
748 if (irqd)
749 disable_irq(hwif->irq);
751 local_irq_set(flags);
753 /* This is needed on some PPCs and a bunch of BIOS-less embedded
754 * platforms. Typical cases are:
756 * - The firmware hard reset the disk before booting the kernel,
757 * the drive is still doing it's poweron-reset sequence, that
758 * can take up to 30 seconds
759 * - The firmware does nothing (or no firmware), the device is
760 * still in POST state (same as above actually).
761 * - Some CD/DVD/Writer combo drives tend to drive the bus during
762 * their reset sequence even when they are non-selected slave
763 * devices, thus preventing discovery of the main HD
765 * Doing this wait-for-busy should not harm any existing configuration
766 * (at least things won't be worse than what current code does, that
767 * is blindly go & talk to the drive) and fix some issues like the
768 * above.
770 * BenH.
772 if (wait_hwif_ready(hwif) == -EBUSY)
773 printk(KERN_DEBUG "%s: Wait for ready failed before probe !\n", hwif->name);
776 * Need to probe slave device first to make it release PDIAG-.
778 for (unit = MAX_DRIVES - 1; unit >= 0; unit--) {
779 ide_drive_t *drive = &hwif->drives[unit];
780 drive->dn = (hwif->channel ? 2 : 0) + unit;
781 (void) probe_for_drive(drive);
782 if (drive->present && !hwif->present) {
783 hwif->present = 1;
784 if (hwif->chipset != ide_4drives ||
785 !hwif->mate ||
786 !hwif->mate->present) {
787 hwif_register(hwif);
791 if (hwif->io_ports[IDE_CONTROL_OFFSET] && hwif->reset) {
792 printk(KERN_WARNING "%s: reset\n", hwif->name);
793 hwif->OUTB(12, hwif->io_ports[IDE_CONTROL_OFFSET]);
794 udelay(10);
795 hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
796 (void)ide_busy_sleep(hwif);
798 local_irq_restore(flags);
800 * Use cached IRQ number. It might be (and is...) changed by probe
801 * code above
803 if (irqd)
804 enable_irq(irqd);
806 if (!hwif->present) {
807 ide_hwif_release_regions(hwif);
808 return;
811 for (unit = 0; unit < MAX_DRIVES; unit++) {
812 ide_drive_t *drive = &hwif->drives[unit];
814 if (drive->present && hwif->quirkproc)
815 hwif->quirkproc(drive);
818 for (unit = 0; unit < MAX_DRIVES; ++unit) {
819 ide_drive_t *drive = &hwif->drives[unit];
821 if (drive->present) {
822 if (drive->autotune == IDE_TUNE_AUTO)
823 ide_set_max_pio(drive);
825 if (drive->autotune != IDE_TUNE_DEFAULT &&
826 drive->autotune != IDE_TUNE_AUTO)
827 continue;
829 drive->nice1 = 1;
831 if (hwif->dma_host_set)
832 ide_set_dma(drive);
836 for (unit = 0; unit < MAX_DRIVES; ++unit) {
837 ide_drive_t *drive = &hwif->drives[unit];
839 if (hwif->no_io_32bit)
840 drive->no_io_32bit = 1;
841 else
842 drive->no_io_32bit = drive->id->dword_io ? 1 : 0;
846 #if MAX_HWIFS > 1
848 * save_match() is used to simplify logic in init_irq() below.
850 * A loophole here is that we may not know about a particular
851 * hwif's irq until after that hwif is actually probed/initialized..
852 * This could be a problem for the case where an hwif is on a
853 * dual interface that requires serialization (eg. cmd640) and another
854 * hwif using one of the same irqs is initialized beforehand.
856 * This routine detects and reports such situations, but does not fix them.
858 static void save_match(ide_hwif_t *hwif, ide_hwif_t *new, ide_hwif_t **match)
860 ide_hwif_t *m = *match;
862 if (m && m->hwgroup && m->hwgroup != new->hwgroup) {
863 if (!new->hwgroup)
864 return;
865 printk("%s: potential irq problem with %s and %s\n",
866 hwif->name, new->name, m->name);
868 if (!m || m->irq != hwif->irq) /* don't undo a prior perfect match */
869 *match = new;
871 #endif /* MAX_HWIFS > 1 */
874 * init request queue
876 static int ide_init_queue(ide_drive_t *drive)
878 struct request_queue *q;
879 ide_hwif_t *hwif = HWIF(drive);
880 int max_sectors = 256;
881 int max_sg_entries = PRD_ENTRIES;
884 * Our default set up assumes the normal IDE case,
885 * that is 64K segmenting, standard PRD setup
886 * and LBA28. Some drivers then impose their own
887 * limits and LBA48 we could raise it but as yet
888 * do not.
891 q = blk_init_queue_node(do_ide_request, &ide_lock, hwif_to_node(hwif));
892 if (!q)
893 return 1;
895 q->queuedata = drive;
896 blk_queue_segment_boundary(q, 0xffff);
898 if (!hwif->rqsize) {
899 if ((hwif->host_flags & IDE_HFLAG_NO_LBA48) ||
900 (hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA))
901 hwif->rqsize = 256;
902 else
903 hwif->rqsize = 65536;
905 if (hwif->rqsize < max_sectors)
906 max_sectors = hwif->rqsize;
907 blk_queue_max_sectors(q, max_sectors);
909 #ifdef CONFIG_PCI
910 /* When we have an IOMMU, we may have a problem where pci_map_sg()
911 * creates segments that don't completely match our boundary
912 * requirements and thus need to be broken up again. Because it
913 * doesn't align properly either, we may actually have to break up
914 * to more segments than what was we got in the first place, a max
915 * worst case is twice as many.
916 * This will be fixed once we teach pci_map_sg() about our boundary
917 * requirements, hopefully soon. *FIXME*
919 if (!PCI_DMA_BUS_IS_PHYS)
920 max_sg_entries >>= 1;
921 #endif /* CONFIG_PCI */
923 blk_queue_max_hw_segments(q, max_sg_entries);
924 blk_queue_max_phys_segments(q, max_sg_entries);
926 /* assign drive queue */
927 drive->queue = q;
929 /* needs drive->queue to be set */
930 ide_toggle_bounce(drive, 1);
932 return 0;
936 * This routine sets up the irq for an ide interface, and creates a new
937 * hwgroup for the irq/hwif if none was previously assigned.
939 * Much of the code is for correctly detecting/handling irq sharing
940 * and irq serialization situations. This is somewhat complex because
941 * it handles static as well as dynamic (PCMCIA) IDE interfaces.
943 static int init_irq (ide_hwif_t *hwif)
945 unsigned int index;
946 ide_hwgroup_t *hwgroup;
947 ide_hwif_t *match = NULL;
950 BUG_ON(in_interrupt());
951 BUG_ON(irqs_disabled());
952 BUG_ON(hwif == NULL);
954 mutex_lock(&ide_cfg_mtx);
955 hwif->hwgroup = NULL;
956 #if MAX_HWIFS > 1
958 * Group up with any other hwifs that share our irq(s).
960 for (index = 0; index < MAX_HWIFS; index++) {
961 ide_hwif_t *h = &ide_hwifs[index];
962 if (h->hwgroup) { /* scan only initialized hwif's */
963 if (hwif->irq == h->irq) {
964 hwif->sharing_irq = h->sharing_irq = 1;
965 if (hwif->chipset != ide_pci ||
966 h->chipset != ide_pci) {
967 save_match(hwif, h, &match);
970 if (hwif->serialized) {
971 if (hwif->mate && hwif->mate->irq == h->irq)
972 save_match(hwif, h, &match);
974 if (h->serialized) {
975 if (h->mate && hwif->irq == h->mate->irq)
976 save_match(hwif, h, &match);
980 #endif /* MAX_HWIFS > 1 */
982 * If we are still without a hwgroup, then form a new one
984 if (match) {
985 hwgroup = match->hwgroup;
986 hwif->hwgroup = hwgroup;
988 * Link us into the hwgroup.
989 * This must be done early, do ensure that unexpected_intr
990 * can find the hwif and prevent irq storms.
991 * No drives are attached to the new hwif, choose_drive
992 * can't do anything stupid (yet).
993 * Add ourself as the 2nd entry to the hwgroup->hwif
994 * linked list, the first entry is the hwif that owns
995 * hwgroup->handler - do not change that.
997 spin_lock_irq(&ide_lock);
998 hwif->next = hwgroup->hwif->next;
999 hwgroup->hwif->next = hwif;
1000 spin_unlock_irq(&ide_lock);
1001 } else {
1002 hwgroup = kmalloc_node(sizeof(ide_hwgroup_t),
1003 GFP_KERNEL | __GFP_ZERO,
1004 hwif_to_node(hwif->drives[0].hwif));
1005 if (!hwgroup)
1006 goto out_up;
1008 hwif->hwgroup = hwgroup;
1010 hwgroup->hwif = hwif->next = hwif;
1011 hwgroup->rq = NULL;
1012 hwgroup->handler = NULL;
1013 hwgroup->drive = NULL;
1014 hwgroup->busy = 0;
1015 init_timer(&hwgroup->timer);
1016 hwgroup->timer.function = &ide_timer_expiry;
1017 hwgroup->timer.data = (unsigned long) hwgroup;
1021 * Allocate the irq, if not already obtained for another hwif
1023 if (!match || match->irq != hwif->irq) {
1024 int sa = 0;
1025 #if defined(__mc68000__) || defined(CONFIG_APUS)
1026 sa = IRQF_SHARED;
1027 #endif /* __mc68000__ || CONFIG_APUS */
1029 if (IDE_CHIPSET_IS_PCI(hwif->chipset))
1030 sa = IRQF_SHARED;
1032 if (hwif->io_ports[IDE_CONTROL_OFFSET])
1033 /* clear nIEN */
1034 hwif->OUTB(0x08, hwif->io_ports[IDE_CONTROL_OFFSET]);
1036 if (request_irq(hwif->irq,&ide_intr,sa,hwif->name,hwgroup))
1037 goto out_unlink;
1041 * For any present drive:
1042 * - allocate the block device queue
1043 * - link drive into the hwgroup
1045 for (index = 0; index < MAX_DRIVES; ++index) {
1046 ide_drive_t *drive = &hwif->drives[index];
1047 if (!drive->present)
1048 continue;
1049 if (ide_init_queue(drive)) {
1050 printk(KERN_ERR "ide: failed to init %s\n",drive->name);
1051 continue;
1053 spin_lock_irq(&ide_lock);
1054 if (!hwgroup->drive) {
1055 /* first drive for hwgroup. */
1056 drive->next = drive;
1057 hwgroup->drive = drive;
1058 hwgroup->hwif = HWIF(hwgroup->drive);
1059 } else {
1060 drive->next = hwgroup->drive->next;
1061 hwgroup->drive->next = drive;
1063 spin_unlock_irq(&ide_lock);
1066 #if !defined(__mc68000__) && !defined(CONFIG_APUS)
1067 printk("%s at 0x%03lx-0x%03lx,0x%03lx on irq %d", hwif->name,
1068 hwif->io_ports[IDE_DATA_OFFSET],
1069 hwif->io_ports[IDE_DATA_OFFSET]+7,
1070 hwif->io_ports[IDE_CONTROL_OFFSET], hwif->irq);
1071 #else
1072 printk("%s at 0x%08lx on irq %d", hwif->name,
1073 hwif->io_ports[IDE_DATA_OFFSET], hwif->irq);
1074 #endif /* __mc68000__ && CONFIG_APUS */
1075 if (match)
1076 printk(" (%sed with %s)",
1077 hwif->sharing_irq ? "shar" : "serializ", match->name);
1078 printk("\n");
1079 mutex_unlock(&ide_cfg_mtx);
1080 return 0;
1081 out_unlink:
1082 spin_lock_irq(&ide_lock);
1083 if (hwif->next == hwif) {
1084 BUG_ON(match);
1085 BUG_ON(hwgroup->hwif != hwif);
1086 kfree(hwgroup);
1087 } else {
1088 ide_hwif_t *g;
1089 g = hwgroup->hwif;
1090 while (g->next != hwif)
1091 g = g->next;
1092 g->next = hwif->next;
1093 if (hwgroup->hwif == hwif) {
1094 /* Impossible. */
1095 printk(KERN_ERR "Duh. Uninitialized hwif listed as active hwif.\n");
1096 hwgroup->hwif = g;
1098 BUG_ON(hwgroup->hwif == hwif);
1100 spin_unlock_irq(&ide_lock);
1101 out_up:
1102 mutex_unlock(&ide_cfg_mtx);
1103 return 1;
1106 static int ata_lock(dev_t dev, void *data)
1108 /* FIXME: we want to pin hwif down */
1109 return 0;
1112 static struct kobject *ata_probe(dev_t dev, int *part, void *data)
1114 ide_hwif_t *hwif = data;
1115 int unit = *part >> PARTN_BITS;
1116 ide_drive_t *drive = &hwif->drives[unit];
1117 if (!drive->present)
1118 return NULL;
1120 if (drive->media == ide_disk)
1121 request_module("ide-disk");
1122 if (drive->scsi)
1123 request_module("ide-scsi");
1124 if (drive->media == ide_cdrom || drive->media == ide_optical)
1125 request_module("ide-cd");
1126 if (drive->media == ide_tape)
1127 request_module("ide-tape");
1128 if (drive->media == ide_floppy)
1129 request_module("ide-floppy");
1131 return NULL;
1134 static struct kobject *exact_match(dev_t dev, int *part, void *data)
1136 struct gendisk *p = data;
1137 *part &= (1 << PARTN_BITS) - 1;
1138 return &p->dev.kobj;
1141 static int exact_lock(dev_t dev, void *data)
1143 struct gendisk *p = data;
1145 if (!get_disk(p))
1146 return -1;
1147 return 0;
1150 void ide_register_region(struct gendisk *disk)
1152 blk_register_region(MKDEV(disk->major, disk->first_minor),
1153 disk->minors, NULL, exact_match, exact_lock, disk);
1156 EXPORT_SYMBOL_GPL(ide_register_region);
1158 void ide_unregister_region(struct gendisk *disk)
1160 blk_unregister_region(MKDEV(disk->major, disk->first_minor),
1161 disk->minors);
1164 EXPORT_SYMBOL_GPL(ide_unregister_region);
1166 void ide_init_disk(struct gendisk *disk, ide_drive_t *drive)
1168 ide_hwif_t *hwif = drive->hwif;
1169 unsigned int unit = (drive->select.all >> 4) & 1;
1171 disk->major = hwif->major;
1172 disk->first_minor = unit << PARTN_BITS;
1173 sprintf(disk->disk_name, "hd%c", 'a' + hwif->index * MAX_DRIVES + unit);
1174 disk->queue = drive->queue;
1177 EXPORT_SYMBOL_GPL(ide_init_disk);
1179 static void ide_remove_drive_from_hwgroup(ide_drive_t *drive)
1181 ide_hwgroup_t *hwgroup = drive->hwif->hwgroup;
1183 if (drive == drive->next) {
1184 /* special case: last drive from hwgroup. */
1185 BUG_ON(hwgroup->drive != drive);
1186 hwgroup->drive = NULL;
1187 } else {
1188 ide_drive_t *walk;
1190 walk = hwgroup->drive;
1191 while (walk->next != drive)
1192 walk = walk->next;
1193 walk->next = drive->next;
1194 if (hwgroup->drive == drive) {
1195 hwgroup->drive = drive->next;
1196 hwgroup->hwif = hwgroup->drive->hwif;
1199 BUG_ON(hwgroup->drive == drive);
1202 static void drive_release_dev (struct device *dev)
1204 ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
1206 spin_lock_irq(&ide_lock);
1207 ide_remove_drive_from_hwgroup(drive);
1208 kfree(drive->id);
1209 drive->id = NULL;
1210 drive->present = 0;
1211 /* Messed up locking ... */
1212 spin_unlock_irq(&ide_lock);
1213 blk_cleanup_queue(drive->queue);
1214 spin_lock_irq(&ide_lock);
1215 drive->queue = NULL;
1216 spin_unlock_irq(&ide_lock);
1218 complete(&drive->gendev_rel_comp);
1222 * init_gendisk() (as opposed to ide_geninit) is called for each major device,
1223 * after probing for drives, to allocate partition tables and other data
1224 * structures needed for the routines in genhd.c. ide_geninit() gets called
1225 * somewhat later, during the partition check.
1227 static void init_gendisk (ide_hwif_t *hwif)
1229 unsigned int unit;
1231 for (unit = 0; unit < MAX_DRIVES; ++unit) {
1232 ide_drive_t * drive = &hwif->drives[unit];
1233 ide_add_generic_settings(drive);
1234 snprintf(drive->gendev.bus_id,BUS_ID_SIZE,"%u.%u",
1235 hwif->index,unit);
1236 drive->gendev.parent = &hwif->gendev;
1237 drive->gendev.bus = &ide_bus_type;
1238 drive->gendev.driver_data = drive;
1239 drive->gendev.release = drive_release_dev;
1241 blk_register_region(MKDEV(hwif->major, 0), MAX_DRIVES << PARTN_BITS,
1242 THIS_MODULE, ata_probe, ata_lock, hwif);
1245 static int hwif_init(ide_hwif_t *hwif)
1247 int old_irq;
1249 /* Return success if no device is connected */
1250 if (!hwif->present)
1251 return 1;
1253 if (!hwif->irq) {
1254 if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET])))
1256 printk("%s: DISABLED, NO IRQ\n", hwif->name);
1257 return (hwif->present = 0);
1260 #ifdef CONFIG_BLK_DEV_HD
1261 if (hwif->irq == HD_IRQ && hwif->io_ports[IDE_DATA_OFFSET] != HD_DATA) {
1262 printk("%s: CANNOT SHARE IRQ WITH OLD "
1263 "HARDDISK DRIVER (hd.c)\n", hwif->name);
1264 return (hwif->present = 0);
1266 #endif /* CONFIG_BLK_DEV_HD */
1268 /* we set it back to 1 if all is ok below */
1269 hwif->present = 0;
1271 if (register_blkdev(hwif->major, hwif->name))
1272 return 0;
1274 if (!hwif->sg_max_nents)
1275 hwif->sg_max_nents = PRD_ENTRIES;
1277 hwif->sg_table = kmalloc(sizeof(struct scatterlist)*hwif->sg_max_nents,
1278 GFP_KERNEL);
1279 if (!hwif->sg_table) {
1280 printk(KERN_ERR "%s: unable to allocate SG table.\n", hwif->name);
1281 goto out;
1284 sg_init_table(hwif->sg_table, hwif->sg_max_nents);
1286 if (init_irq(hwif) == 0)
1287 goto done;
1289 old_irq = hwif->irq;
1291 * It failed to initialise. Find the default IRQ for
1292 * this port and try that.
1294 if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET]))) {
1295 printk("%s: Disabled unable to get IRQ %d.\n",
1296 hwif->name, old_irq);
1297 goto out;
1299 if (init_irq(hwif)) {
1300 printk("%s: probed IRQ %d and default IRQ %d failed.\n",
1301 hwif->name, old_irq, hwif->irq);
1302 goto out;
1304 printk("%s: probed IRQ %d failed, using default.\n",
1305 hwif->name, hwif->irq);
1307 done:
1308 init_gendisk(hwif);
1310 ide_acpi_init(hwif);
1312 hwif->present = 1; /* success */
1313 return 1;
1315 out:
1316 unregister_blkdev(hwif->major, hwif->name);
1317 return 0;
1320 static void hwif_register_devices(ide_hwif_t *hwif)
1322 unsigned int i;
1324 for (i = 0; i < MAX_DRIVES; i++) {
1325 ide_drive_t *drive = &hwif->drives[i];
1327 if (drive->present) {
1328 int ret = device_register(&drive->gendev);
1330 if (ret < 0)
1331 printk(KERN_WARNING "IDE: %s: "
1332 "device_register error: %d\n",
1333 __FUNCTION__, ret);
1338 int ide_device_add_all(u8 *idx)
1340 ide_hwif_t *hwif;
1341 int i, rc = 0;
1343 for (i = 0; i < MAX_HWIFS; i++) {
1344 if (idx[i] == 0xff)
1345 continue;
1347 probe_hwif(&ide_hwifs[idx[i]]);
1350 for (i = 0; i < MAX_HWIFS; i++) {
1351 if (idx[i] == 0xff)
1352 continue;
1354 hwif = &ide_hwifs[idx[i]];
1356 if (hwif_init(hwif) == 0) {
1357 printk(KERN_INFO "%s: failed to initialize IDE "
1358 "interface\n", hwif->name);
1359 rc = -1;
1360 continue;
1364 for (i = 0; i < MAX_HWIFS; i++) {
1365 if (idx[i] == 0xff)
1366 continue;
1368 hwif = &ide_hwifs[idx[i]];
1370 if (hwif->present) {
1371 if (hwif->chipset == ide_unknown ||
1372 hwif->chipset == ide_forced)
1373 hwif->chipset = ide_generic;
1374 hwif_register_devices(hwif);
1378 for (i = 0; i < MAX_HWIFS; i++) {
1379 if (idx[i] != 0xff)
1380 ide_proc_register_port(&ide_hwifs[idx[i]]);
1383 return rc;
1385 EXPORT_SYMBOL_GPL(ide_device_add_all);
1387 int ide_device_add(u8 idx[4])
1389 u8 idx_all[MAX_HWIFS];
1390 int i;
1392 for (i = 0; i < MAX_HWIFS; i++)
1393 idx_all[i] = (i < 4) ? idx[i] : 0xff;
1395 return ide_device_add_all(idx_all);
1397 EXPORT_SYMBOL_GPL(ide_device_add);