1/*
2 *  linux/drivers/ide/ide-probe.c	Version 1.11	Mar 05, 2003
3 *
4 *  Copyright (C) 1994-1998  Linus Torvalds & authors (see below)
5 */
6
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>
11 *
12 *  See linux/MAINTAINERS for address of current maintainer.
13 *
14 * This is the IDE probe module, as evolved from hd.c and ide.c.
15 *
16 * Version 1.00		move drive probing code from ide.c to ide-probe.c
17 * Version 1.01		fix compilation problem for m68k
18 * Version 1.02		increase WAIT_PIDENTIFY to avoid CD-ROM locking at boot
19 *			 by Andrea Arcangeli
20 * Version 1.03		fix for (hwif->chipset == ide_4drives)
21 * Version 1.04		fixed buggy treatments of known flash memory cards
22 *
23 * Version 1.05		fix for (hwif->chipset == ide_pdc4030)
24 *			added ide6/7/8/9
25 *			allowed for secondary flash card to be detectable
26 *			 with new flag : drive->ata_flash : 1;
27 * Version 1.06		stream line request queue and prep for cascade project.
28 * Version 1.07		max_sect <= 255; slower disks would get behind and
29 * 			then fall over when they get to 256.	Paul G.
30 * Version 1.10		Update set for new IDE. drive->id is now always
31 *			valid after probe time even with noprobe
32 */
33
34#include <linux/module.h>
35#include <linux/types.h>
36#include <linux/string.h>
37#include <linux/kernel.h>
38#include <linux/timer.h>
39#include <linux/mm.h>
40#include <linux/interrupt.h>
41#include <linux/major.h>
42#include <linux/errno.h>
43#include <linux/genhd.h>
44#include <linux/slab.h>
45#include <linux/delay.h>
46#include <linux/ide.h>
47#include <linux/spinlock.h>
48#include <linux/kmod.h>
49#include <linux/pci.h>
50
51#include <asm/byteorder.h>
52#include <asm/irq.h>
53#include <asm/uaccess.h>
54#include <asm/io.h>
55
56/**
57 *	generic_id		-	add a generic drive id
58 *	@drive:	drive to make an ID block for
59 *
60 *	Add a fake id field to the drive we are passed. This allows
61 *	use to skip a ton of NULL checks (which people always miss)
62 *	and make drive properties unconditional outside of this file
63 */
64
65static void generic_id(ide_drive_t *drive)
66{
67	drive->id->cyls = drive->cyl;
68	drive->id->heads = drive->head;
69	drive->id->sectors = drive->sect;
70	drive->id->cur_cyls = drive->cyl;
71	drive->id->cur_heads = drive->head;
72	drive->id->cur_sectors = drive->sect;
73}
74
75static void ide_disk_init_chs(ide_drive_t *drive)
76{
77	struct hd_driveid *id = drive->id;
78
79	/* Extract geometry if we did not already have one for the drive */
80	if (!drive->cyl || !drive->head || !drive->sect) {
81		drive->cyl  = drive->bios_cyl  = id->cyls;
82		drive->head = drive->bios_head = id->heads;
83		drive->sect = drive->bios_sect = id->sectors;
84	}
85
86	/* Handle logical geometry translation by the drive */
87	if ((id->field_valid & 1) && id->cur_cyls &&
88	    id->cur_heads && (id->cur_heads <= 16) && id->cur_sectors) {
89		drive->cyl  = id->cur_cyls;
90		drive->head = id->cur_heads;
91		drive->sect = id->cur_sectors;
92	}
93
94	/* Use physical geometry if what we have still makes no sense */
95	if (drive->head > 16 && id->heads && id->heads <= 16) {
96		drive->cyl  = id->cyls;
97		drive->head = id->heads;
98		drive->sect = id->sectors;
99	}
100}
101
102static void ide_disk_init_mult_count(ide_drive_t *drive)
103{
104	struct hd_driveid *id = drive->id;
105
106	drive->mult_count = 0;
107	if (id->max_multsect) {
108#ifdef CONFIG_IDEDISK_MULTI_MODE
109		id->multsect = ((id->max_multsect/2) > 1) ? id->max_multsect : 0;
110		id->multsect_valid = id->multsect ? 1 : 0;
111		drive->mult_req = id->multsect_valid ? id->max_multsect : INITIAL_MULT_COUNT;
112		drive->special.b.set_multmode = drive->mult_req ? 1 : 0;
113#else	/* original, pre IDE-NFG, per request of AC */
114		drive->mult_req = INITIAL_MULT_COUNT;
115		if (drive->mult_req > id->max_multsect)
116			drive->mult_req = id->max_multsect;
117		if (drive->mult_req || ((id->multsect_valid & 1) && id->multsect))
118			drive->special.b.set_multmode = 1;
119#endif
120	}
121}
122
123/**
124 *	do_identify	-	identify a drive
125 *	@drive: drive to identify
126 *	@cmd: command used
127 *
128 *	Called when we have issued a drive identify command to
129 *	read and parse the results. This function is run with
130 *	interrupts disabled.
131 */
132
133static inline void do_identify (ide_drive_t *drive, u8 cmd)
134{
135	ide_hwif_t *hwif = HWIF(drive);
136	int bswap = 1;
137	struct hd_driveid *id;
138
139	id = drive->id;
140	/* read 512 bytes of id info */
141	hwif->ata_input_data(drive, id, SECTOR_WORDS);
142
143	drive->id_read = 1;
144	local_irq_enable();
145	ide_fix_driveid(id);
146
147#if defined(CONFIG_SCSI_EATA_DMA) || defined(CONFIG_SCSI_EATA_PIO) || \
148	defined(CONFIG_SCSI_EATA)
149	/*
150	 * EATA SCSI controllers do a hardware ATA emulation:
151	 * Ignore them if there is a driver for them available.
152	 */
153	if ((id->model[0] == 'P' && id->model[1] == 'M') ||
154	    (id->model[0] == 'S' && id->model[1] == 'K')) {
155		printk("%s: EATA SCSI HBA %.10s\n", drive->name, id->model);
156		goto err_misc;
157	}
158#endif /* CONFIG_SCSI_EATA_DMA || CONFIG_SCSI_EATA_PIO */
159
160	/*
161	 *  WIN_IDENTIFY returns little-endian info,
162	 *  WIN_PIDENTIFY *usually* returns little-endian info.
163	 */
164	if (cmd == WIN_PIDENTIFY) {
165		if ((id->model[0] == 'N' && id->model[1] == 'E') /* NEC */
166		 || (id->model[0] == 'F' && id->model[1] == 'X') /* Mitsumi */
167		 || (id->model[0] == 'P' && id->model[1] == 'i'))/* Pioneer */
168			/* Vertos drives may still be weird */
169			bswap ^= 1;
170	}
171	ide_fixstring(id->model,     sizeof(id->model),     bswap);
172	ide_fixstring(id->fw_rev,    sizeof(id->fw_rev),    bswap);
173	ide_fixstring(id->serial_no, sizeof(id->serial_no), bswap);
174
175	if (strstr(id->model, "E X A B Y T E N E S T"))
176		goto err_misc;
177
178	/* we depend on this a lot! */
179	id->model[sizeof(id->model)-1] = '\0';
180	printk("%s: %s, ", drive->name, id->model);
181	drive->present = 1;
182	drive->dead = 0;
183
184	/*
185	 * Check for an ATAPI device
186	 */
187	if (cmd == WIN_PIDENTIFY) {
188		u8 type = (id->config >> 8) & 0x1f;
189		printk("ATAPI ");
190		switch (type) {
191			case ide_floppy:
192				if (!strstr(id->model, "CD-ROM")) {
193					if (!strstr(id->model, "oppy") &&
194					    !strstr(id->model, "poyp") &&
195					    !strstr(id->model, "ZIP"))
196						printk("cdrom or floppy?, assuming ");
197					if (drive->media != ide_cdrom) {
198						printk ("FLOPPY");
199						drive->removable = 1;
200						break;
201					}
202				}
203				/* Early cdrom models used zero */
204				type = ide_cdrom;
205			case ide_cdrom:
206				drive->removable = 1;
207#ifdef CONFIG_PPC
208				/* kludge for Apple PowerBook internal zip */
209				if (!strstr(id->model, "CD-ROM") &&
210				    strstr(id->model, "ZIP")) {
211					printk ("FLOPPY");
212					type = ide_floppy;
213					break;
214				}
215#endif
216				printk ("CD/DVD-ROM");
217				break;
218			case ide_tape:
219				printk ("TAPE");
220				break;
221			case ide_optical:
222				printk ("OPTICAL");
223				drive->removable = 1;
224				break;
225			default:
226				printk("UNKNOWN (type %d)", type);
227				break;
228		}
229		printk (" drive\n");
230		drive->media = type;
231		/* an ATAPI device ignores DRDY */
232		drive->ready_stat = 0;
233		return;
234	}
235
236	/*
237	 * Not an ATAPI device: looks like a "regular" hard disk
238	 */
239
240	/*
241	 * 0x848a = CompactFlash device
242	 * These are *not* removable in Linux definition of the term
243	 */
244
245	if ((id->config != 0x848a) && (id->config & (1<<7)))
246		drive->removable = 1;
247
248	drive->media = ide_disk;
249	printk("%s DISK drive\n", (id->config == 0x848a) ? "CFA" : "ATA" );
250	QUIRK_LIST(drive);
251	return;
252
253err_misc:
254	kfree(id);
255	drive->present = 0;
256	return;
257}
258
259/**
260 *	actual_try_to_identify	-	send ata/atapi identify
261 *	@drive: drive to identify
262 *	@cmd: command to use
263 *
264 *	try_to_identify() sends an ATA(PI) IDENTIFY request to a drive
265 *	and waits for a response.  It also monitors irqs while this is
266 *	happening, in hope of automatically determining which one is
267 *	being used by the interface.
268 *
269 *	Returns:	0  device was identified
270 *			1  device timed-out (no response to identify request)
271 *			2  device aborted the command (refused to identify itself)
272 */
273
274static int actual_try_to_identify (ide_drive_t *drive, u8 cmd)
275{
276	ide_hwif_t *hwif = HWIF(drive);
277	int rc;
278	unsigned long hd_status;
279	unsigned long timeout;
280	u8 s = 0, a = 0;
281
282	/* take a deep breath */
283	msleep(50);
284
285	if (IDE_CONTROL_REG) {
286		a = hwif->INB(IDE_ALTSTATUS_REG);
287		s = hwif->INB(IDE_STATUS_REG);
288		if ((a ^ s) & ~INDEX_STAT) {
289			printk(KERN_INFO "%s: probing with STATUS(0x%02x) instead of "
290				"ALTSTATUS(0x%02x)\n", drive->name, s, a);
291			/* ancient Seagate drives, broken interfaces */
292			hd_status = IDE_STATUS_REG;
293		} else {
294			/* use non-intrusive polling */
295			hd_status = IDE_ALTSTATUS_REG;
296		}
297	} else
298		hd_status = IDE_STATUS_REG;
299
300	/* set features register for atapi
301	 * identify command to be sure of reply
302	 */
303	if ((cmd == WIN_PIDENTIFY))
304		/* disable dma & overlap */
305		hwif->OUTB(0, IDE_FEATURE_REG);
306
307	/* ask drive for ID */
308	hwif->OUTB(cmd, IDE_COMMAND_REG);
309
310	timeout = ((cmd == WIN_IDENTIFY) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
311	timeout += jiffies;
312	do {
313		if (time_after(jiffies, timeout)) {
314			/* drive timed-out */
315			return 1;
316		}
317		/* give drive a breather */
318		msleep(50);
319	} while ((hwif->INB(hd_status)) & BUSY_STAT);
320
321	/* wait for IRQ and DRQ_STAT */
322	msleep(50);
323	if (OK_STAT((hwif->INB(IDE_STATUS_REG)), DRQ_STAT, BAD_R_STAT)) {
324		unsigned long flags;
325
326		/* local CPU only; some systems need this */
327		local_irq_save(flags);
328		/* drive returned ID */
329		do_identify(drive, cmd);
330		/* drive responded with ID */
331		rc = 0;
332		/* clear drive IRQ */
333		(void) hwif->INB(IDE_STATUS_REG);
334		local_irq_restore(flags);
335	} else {
336		/* drive refused ID */
337		rc = 2;
338	}
339	return rc;
340}
341
342/**
343 *	try_to_identify	-	try to identify a drive
344 *	@drive: drive to probe
345 *	@cmd: command to use
346 *
347 *	Issue the identify command and then do IRQ probing to
348 *	complete the identification when needed by finding the
349 *	IRQ the drive is attached to
350 */
351
352static int try_to_identify (ide_drive_t *drive, u8 cmd)
353{
354	ide_hwif_t *hwif = HWIF(drive);
355	int retval;
356	int autoprobe = 0;
357	unsigned long cookie = 0;
358
359	/*
360	 * Disable device irq unless we need to
361	 * probe for it. Otherwise we'll get spurious
362	 * interrupts during the identify-phase that
363	 * the irq handler isn't expecting.
364	 */
365	if (IDE_CONTROL_REG) {
366		u8 ctl = drive->ctl | 2;
367		if (!hwif->irq) {
368			autoprobe = 1;
369			cookie = probe_irq_on();
370			/* enable device irq */
371			ctl &= ~2;
372		}
373		hwif->OUTB(ctl, IDE_CONTROL_REG);
374	}
375
376	retval = actual_try_to_identify(drive, cmd);
377
378	if (autoprobe) {
379		int irq;
380		/* mask device irq */
381		hwif->OUTB(drive->ctl|2, IDE_CONTROL_REG);
382		/* clear drive IRQ */
383		(void) hwif->INB(IDE_STATUS_REG);
384		udelay(5);
385		irq = probe_irq_off(cookie);
386		if (!hwif->irq) {
387			if (irq > 0) {
388				hwif->irq = irq;
389			} else {
390				/* Mmmm.. multiple IRQs..
391				 * don't know which was ours
392				 */
393				printk("%s: IRQ probe failed (0x%lx)\n",
394					drive->name, cookie);
395			}
396		}
397	}
398	return retval;
399}
400
401
402/**
403 *	do_probe		-	probe an IDE device
404 *	@drive: drive to probe
405 *	@cmd: command to use
406 *
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.
410 *
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.
415 *
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
421 */
422
423static int do_probe (ide_drive_t *drive, u8 cmd)
424{
425	int rc;
426	ide_hwif_t *hwif = HWIF(drive);
427
428	if (drive->present) {
429		/* avoid waiting for inappropriate probes */
430		if ((drive->media != ide_disk) && (cmd == WIN_IDENTIFY))
431			return 4;
432	}
433#ifdef DEBUG
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");
437#endif
438
439	/* needed for some systems
440	 * (e.g. crw9624 as drive0 with disk as slave)
441	 */
442	msleep(50);
443	SELECT_DRIVE(drive);
444	msleep(50);
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 */
450			msleep(50);
451		}
452		/* no i/f present: mmm.. this should be a 4 -ml */
453		return 3;
454	}
455
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);
462		}
463		if (hwif->INB(IDE_STATUS_REG) == (BUSY_STAT|READY_STAT))
464			return 4;
465
466		if ((rc == 1 && cmd == WIN_PIDENTIFY) &&
467			((drive->autotune == IDE_TUNE_DEFAULT) ||
468			(drive->autotune == IDE_TUNE_AUTO))) {
469			unsigned long timeout;
470			printk("%s: no response (status = 0x%02x), "
471				"resetting drive\n", drive->name,
472				hwif->INB(IDE_STATUS_REG));
473			msleep(50);
474			hwif->OUTB(drive->select.all, IDE_SELECT_REG);
475			msleep(50);
476			hwif->OUTB(WIN_SRST, IDE_COMMAND_REG);
477			timeout = jiffies;
478			while (((hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) &&
479			       time_before(jiffies, timeout + WAIT_WORSTCASE))
480				msleep(50);
481			rc = try_to_identify(drive, cmd);
482		}
483		if (rc == 1)
484			printk("%s: no response (status = 0x%02x)\n",
485				drive->name, hwif->INB(IDE_STATUS_REG));
486		/* ensure drive irq is clear */
487		(void) hwif->INB(IDE_STATUS_REG);
488	} else {
489		/* not present or maybe ATAPI */
490		rc = 3;
491	}
492	if (drive->select.b.unit != 0) {
493		/* exit with drive0 selected */
494		SELECT_DRIVE(&hwif->drives[0]);
495		msleep(50);
496		/* ensure drive irq is clear */
497		(void) hwif->INB(IDE_STATUS_REG);
498	}
499	return rc;
500}
501
502/*
503 *
504 */
505static void enable_nest (ide_drive_t *drive)
506{
507	ide_hwif_t *hwif = HWIF(drive);
508	unsigned long timeout;
509
510	printk("%s: enabling %s -- ", hwif->name, drive->id->model);
511	SELECT_DRIVE(drive);
512	msleep(50);
513	hwif->OUTB(EXABYTE_ENABLE_NEST, IDE_COMMAND_REG);
514	timeout = jiffies + WAIT_WORSTCASE;
515	do {
516		if (time_after(jiffies, timeout)) {
517			printk("failed (timeout)\n");
518			return;
519		}
520		msleep(50);
521	} while ((hwif->INB(IDE_STATUS_REG)) & BUSY_STAT);
522
523	msleep(50);
524
525	if (!OK_STAT((hwif->INB(IDE_STATUS_REG)), 0, BAD_STAT)) {
526		printk("failed (status = 0x%02x)\n", hwif->INB(IDE_STATUS_REG));
527	} else {
528		printk("success\n");
529	}
530
531	/* if !(success||timed-out) */
532	if (do_probe(drive, WIN_IDENTIFY) >= 2) {
533		/* look for ATAPI device */
534		(void) do_probe(drive, WIN_PIDENTIFY);
535	}
536}
537
538/**
539 *	probe_for_drives	-	upper level drive probe
540 *	@drive: drive to probe for
541 *
542 *	probe_for_drive() tests for existence of a given drive using do_probe()
543 *	and presents things to the user as needed.
544 *
545 *	Returns:	0  no device was found
546 *			1  device was found (note: drive->present might
547 *			   still be 0)
548 */
549
550static inline u8 probe_for_drive (ide_drive_t *drive)
551{
552	/*
553	 *	In order to keep things simple we have an id
554	 *	block for all drives at all times. If the device
555	 *	is pre ATA or refuses ATA/ATAPI identify we
556	 *	will add faked data to this.
557	 *
558	 *	Also note that 0 everywhere means "can't do X"
559	 */
560
561	drive->id = kzalloc(SECTOR_WORDS *4, GFP_KERNEL);
562	drive->id_read = 0;
563	if(drive->id == NULL)
564	{
565		printk(KERN_ERR "ide: out of memory for id data.\n");
566		return 0;
567	}
568	strcpy(drive->id->model, "UNKNOWN");
569
570	/* skip probing? */
571	if (!drive->noprobe)
572	{
573		/* if !(success||timed-out) */
574		if (do_probe(drive, WIN_IDENTIFY) >= 2) {
575			/* look for ATAPI device */
576			(void) do_probe(drive, WIN_PIDENTIFY);
577		}
578		if (!drive->present)
579			/* drive not found */
580			return 0;
581		if (strstr(drive->id->model, "E X A B Y T E N E S T"))
582			enable_nest(drive);
583
584		/* identification failed? */
585		if (!drive->id_read) {
586			if (drive->media == ide_disk) {
587				printk(KERN_INFO "%s: non-IDE drive, CHS=%d/%d/%d\n",
588					drive->name, drive->cyl,
589					drive->head, drive->sect);
590			} else if (drive->media == ide_cdrom) {
591				printk(KERN_INFO "%s: ATAPI cdrom (?)\n", drive->name);
592			} else {
593				/* nuke it */
594				printk(KERN_WARNING "%s: Unknown device on bus refused identification. Ignoring.\n", drive->name);
595				drive->present = 0;
596			}
597		}
598		/* drive was found */
599	}
600	if(!drive->present)
601		return 0;
602	/* The drive wasn't being helpful. Add generic info only */
603	if (drive->id_read == 0) {
604		generic_id(drive);
605		return 1;
606	}
607
608	if (drive->media == ide_disk) {
609		ide_disk_init_chs(drive);
610		ide_disk_init_mult_count(drive);
611	}
612
613	return drive->present;
614}
615
616static void hwif_release_dev (struct device *dev)
617{
618	ide_hwif_t *hwif = container_of(dev, ide_hwif_t, gendev);
619
620	complete(&hwif->gendev_rel_comp);
621}
622
623static void hwif_register (ide_hwif_t *hwif)
624{
625	int ret;
626
627	/* register with global device tree */
628	strlcpy(hwif->gendev.bus_id,hwif->name,BUS_ID_SIZE);
629	hwif->gendev.driver_data = hwif;
630	if (hwif->gendev.parent == NULL) {
631		if (hwif->pci_dev)
632			hwif->gendev.parent = &hwif->pci_dev->dev;
633		else
634			/* Would like to do = &device_legacy */
635			hwif->gendev.parent = NULL;
636	}
637	hwif->gendev.release = hwif_release_dev;
638	ret = device_register(&hwif->gendev);
639	if (ret < 0)
640		printk(KERN_WARNING "IDE: %s: device_register error: %d\n",
641			__FUNCTION__, ret);
642}
643
644static int wait_hwif_ready(ide_hwif_t *hwif)
645{
646	int rc;
647
648	printk(KERN_DEBUG "Probing IDE interface %s...\n", hwif->name);
649
650	/* Let HW settle down a bit from whatever init state we
651	 * come from */
652	mdelay(2);
653
654	/* Wait for BSY bit to go away, spec timeout is 30 seconds,
655	 * I know of at least one disk who takes 31 seconds, I use 35
656	 * here to be safe
657	 */
658	rc = ide_wait_not_busy(hwif, 35000);
659	if (rc)
660		return rc;
661
662	/* Now make sure both master & slave are ready */
663	SELECT_DRIVE(&hwif->drives[0]);
664	hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
665	mdelay(2);
666	rc = ide_wait_not_busy(hwif, 35000);
667	if (rc)
668		return rc;
669	SELECT_DRIVE(&hwif->drives[1]);
670	hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
671	mdelay(2);
672	rc = ide_wait_not_busy(hwif, 35000);
673
674	/* Exit function with master reselected (let's be sane) */
675	SELECT_DRIVE(&hwif->drives[0]);
676
677	return rc;
678}
679
680/**
681 *	ide_undecoded_slave	-	look for bad CF adapters
682 *	@hwif: interface
683 *
684 *	Analyse the drives on the interface and attempt to decide if we
685 *	have the same drive viewed twice. This occurs with crap CF adapters
686 *	and PCMCIA sometimes.
687 */
688
689void ide_undecoded_slave(ide_hwif_t *hwif)
690{
691	ide_drive_t *drive0 = &hwif->drives[0];
692	ide_drive_t *drive1 = &hwif->drives[1];
693
694	if (drive0->present == 0 || drive1->present == 0)
695		return;
696
697	/* If the models don't match they are not the same product */
698	if (strcmp(drive0->id->model, drive1->id->model))
699		return;
700
701	/* Serial numbers do not match */
702	if (strncmp(drive0->id->serial_no, drive1->id->serial_no, 20))
703		return;
704
705	/* No serial number, thankfully very rare for CF */
706	if (drive0->id->serial_no[0] == 0)
707		return;
708
709	/* Appears to be an IDE flash adapter with decode bugs */
710	printk(KERN_WARNING "ide-probe: ignoring undecoded slave\n");
711
712	drive1->present = 0;
713}
714
715EXPORT_SYMBOL_GPL(ide_undecoded_slave);
716
717/*
718 * This routine only knows how to look for drive units 0 and 1
719 * on an interface, so any setting of MAX_DRIVES > 2 won't work here.
720 */
721static void probe_hwif(ide_hwif_t *hwif, void (*fixup)(ide_hwif_t *hwif))
722{
723	unsigned int unit;
724	unsigned long flags;
725	unsigned int irqd;
726
727	if (hwif->noprobe)
728		return;
729
730	if ((hwif->chipset != ide_4drives || !hwif->mate || !hwif->mate->present) &&
731	    (ide_hwif_request_regions(hwif))) {
732		u16 msgout = 0;
733		for (unit = 0; unit < MAX_DRIVES; ++unit) {
734			ide_drive_t *drive = &hwif->drives[unit];
735			if (drive->present) {
736				drive->present = 0;
737				printk(KERN_ERR "%s: ERROR, PORTS ALREADY IN USE\n",
738					drive->name);
739				msgout = 1;
740			}
741		}
742		if (!msgout)
743			printk(KERN_ERR "%s: ports already in use, skipping probe\n",
744				hwif->name);
745		return;
746	}
747
748	/*
749	 * We must always disable IRQ, as probe_for_drive will assert IRQ, but
750	 * we'll install our IRQ driver much later...
751	 */
752	irqd = hwif->irq;
753	if (irqd)
754		disable_irq(hwif->irq);
755
756	local_irq_set(flags);
757
758	/* This is needed on some PPCs and a bunch of BIOS-less embedded
759	 * platforms. Typical cases are:
760	 *
761	 *  - The firmware hard reset the disk before booting the kernel,
762	 *    the drive is still doing it's poweron-reset sequence, that
763	 *    can take up to 30 seconds
764	 *  - The firmware does nothing (or no firmware), the device is
765	 *    still in POST state (same as above actually).
766	 *  - Some CD/DVD/Writer combo drives tend to drive the bus during
767	 *    their reset sequence even when they are non-selected slave
768	 *    devices, thus preventing discovery of the main HD
769	 *
770	 *  Doing this wait-for-busy should not harm any existing configuration
771	 *  (at least things won't be worse than what current code does, that
772	 *  is blindly go & talk to the drive) and fix some issues like the
773	 *  above.
774	 *
775	 *  BenH.
776	 */
777	if (wait_hwif_ready(hwif) == -EBUSY)
778		printk(KERN_DEBUG "%s: Wait for ready failed before probe !\n", hwif->name);
779
780	/*
781	 * Second drive should only exist if first drive was found,
782	 * but a lot of cdrom drives are configured as single slaves.
783	 */
784	for (unit = 0; unit < MAX_DRIVES; ++unit) {
785		ide_drive_t *drive = &hwif->drives[unit];
786		drive->dn = (hwif->channel ? 2 : 0) + unit;
787		(void) probe_for_drive(drive);
788		if (drive->present && !hwif->present) {
789			hwif->present = 1;
790			if (hwif->chipset != ide_4drives ||
791			    !hwif->mate ||
792			    !hwif->mate->present) {
793				hwif_register(hwif);
794			}
795		}
796	}
797	if (hwif->io_ports[IDE_CONTROL_OFFSET] && hwif->reset) {
798		unsigned long timeout = jiffies + WAIT_WORSTCASE;
799		u8 stat;
800
801		printk(KERN_WARNING "%s: reset\n", hwif->name);
802		hwif->OUTB(12, hwif->io_ports[IDE_CONTROL_OFFSET]);
803		udelay(10);
804		hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
805		do {
806			msleep(50);
807			stat = hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
808		} while ((stat & BUSY_STAT) && time_after(timeout, jiffies));
809
810	}
811	local_irq_restore(flags);
812	/*
813	 * Use cached IRQ number. It might be (and is...) changed by probe
814	 * code above
815	 */
816	if (irqd)
817		enable_irq(irqd);
818
819	if (!hwif->present) {
820		ide_hwif_release_regions(hwif);
821		return;
822	}
823
824	if (fixup)
825		fixup(hwif);
826
827	for (unit = 0; unit < MAX_DRIVES; ++unit) {
828		ide_drive_t *drive = &hwif->drives[unit];
829
830		if (drive->present) {
831			if (hwif->tuneproc != NULL &&
832				drive->autotune == IDE_TUNE_AUTO)
833				/* auto-tune PIO mode */
834				hwif->tuneproc(drive, 255);
835
836			if (drive->autotune != IDE_TUNE_DEFAULT &&
837			    drive->autotune != IDE_TUNE_AUTO)
838				continue;
839
840			drive->nice1 = 1;
841
842			/*
843			 * Move here to prevent module loading clashing.
844			 */
845	//		drive->autodma = hwif->autodma;
846			if (hwif->ide_dma_check) {
847				/*
848				 * Force DMAing for the beginning of the check.
849				 * Some chipsets appear to do interesting
850				 * things, if not checked and cleared.
851				 *   PARANOIA!!!
852				 */
853				hwif->dma_off_quietly(drive);
854#ifdef CONFIG_IDEDMA_ONLYDISK
855				if (drive->media == ide_disk)
856#endif
857					ide_set_dma(drive);
858			}
859		}
860	}
861
862	for (unit = 0; unit < MAX_DRIVES; ++unit) {
863		ide_drive_t *drive = &hwif->drives[unit];
864
865		if (hwif->no_io_32bit)
866			drive->no_io_32bit = 1;
867		else
868			drive->no_io_32bit = drive->id->dword_io ? 1 : 0;
869	}
870}
871
872static int hwif_init(ide_hwif_t *hwif);
873
874int probe_hwif_init_with_fixup(ide_hwif_t *hwif, void (*fixup)(ide_hwif_t *hwif))
875{
876	probe_hwif(hwif, fixup);
877
878	if (!hwif_init(hwif)) {
879		printk(KERN_INFO "%s: failed to initialize IDE interface\n",
880				 hwif->name);
881		return -1;
882	}
883
884	if (hwif->present) {
885		u16 unit = 0;
886		int ret;
887
888		for (unit = 0; unit < MAX_DRIVES; ++unit) {
889			ide_drive_t *drive = &hwif->drives[unit];
890			/* For now don't attach absent drives, we may
891			   want them on default or a new "empty" class
892			   for hotplug reprobing ? */
893			if (drive->present) {
894				ret = device_register(&drive->gendev);
895				if (ret < 0)
896					printk(KERN_WARNING "IDE: %s: "
897						"device_register error: %d\n",
898						__FUNCTION__, ret);
899			}
900		}
901	}
902	return 0;
903}
904
905int probe_hwif_init(ide_hwif_t *hwif)
906{
907	return probe_hwif_init_with_fixup(hwif, NULL);
908}
909
910EXPORT_SYMBOL(probe_hwif_init);
911
912#if MAX_HWIFS > 1
913/*
914 * save_match() is used to simplify logic in init_irq() below.
915 *
916 * A loophole here is that we may not know about a particular
917 * hwif's irq until after that hwif is actually probed/initialized..
918 * This could be a problem for the case where an hwif is on a
919 * dual interface that requires serialization (eg. cmd640) and another
920 * hwif using one of the same irqs is initialized beforehand.
921 *
922 * This routine detects and reports such situations, but does not fix them.
923 */
924static void save_match(ide_hwif_t *hwif, ide_hwif_t *new, ide_hwif_t **match)
925{
926	ide_hwif_t *m = *match;
927
928	if (m && m->hwgroup && m->hwgroup != new->hwgroup) {
929		if (!new->hwgroup)
930			return;
931		printk("%s: potential irq problem with %s and %s\n",
932			hwif->name, new->name, m->name);
933	}
934	if (!m || m->irq != hwif->irq) /* don't undo a prior perfect match */
935		*match = new;
936}
937#endif /* MAX_HWIFS > 1 */
938
939/*
940 * init request queue
941 */
942static int ide_init_queue(ide_drive_t *drive)
943{
944	request_queue_t *q;
945	ide_hwif_t *hwif = HWIF(drive);
946	int max_sectors = 256;
947	int max_sg_entries = PRD_ENTRIES;
948
949	/*
950	 *	Our default set up assumes the normal IDE case,
951	 *	that is 64K segmenting, standard PRD setup
952	 *	and LBA28. Some drivers then impose their own
953	 *	limits and LBA48 we could raise it but as yet
954	 *	do not.
955	 */
956
957	q = blk_init_queue_node(do_ide_request, &ide_lock, hwif_to_node(hwif));
958	if (!q)
959		return 1;
960
961	q->queuedata = drive;
962	blk_queue_segment_boundary(q, 0xffff);
963
964	if (!hwif->rqsize) {
965		if (hwif->no_lba48 || hwif->no_lba48_dma)
966			hwif->rqsize = 256;
967		else
968			hwif->rqsize = 65536;
969	}
970	if (hwif->rqsize < max_sectors)
971		max_sectors = hwif->rqsize;
972	blk_queue_max_sectors(q, max_sectors);
973
974#ifdef CONFIG_PCI
975	if (!PCI_DMA_BUS_IS_PHYS)
976		max_sg_entries >>= 1;
977#endif /* CONFIG_PCI */
978
979	blk_queue_max_hw_segments(q, max_sg_entries);
980	blk_queue_max_phys_segments(q, max_sg_entries);
981
982	/* assign drive queue */
983	drive->queue = q;
984
985	/* needs drive->queue to be set */
986	ide_toggle_bounce(drive, 1);
987
988	return 0;
989}
990
991/*
992 * This routine sets up the irq for an ide interface, and creates a new
993 * hwgroup for the irq/hwif if none was previously assigned.
994 *
995 * Much of the code is for correctly detecting/handling irq sharing
996 * and irq serialization situations.  This is somewhat complex because
997 * it handles static as well as dynamic (PCMCIA) IDE interfaces.
998 *
999 * The IRQF_DISABLED in sa_flags means ide_intr() is always entered with
1000 * interrupts completely disabled.  This can be bad for interrupt latency,
1001 * but anything else has led to problems on some machines.  We re-enable
1002 * interrupts as much as we can safely do in most places.
1003 */
1004static int init_irq (ide_hwif_t *hwif)
1005{
1006	unsigned int index;
1007	ide_hwgroup_t *hwgroup;
1008	ide_hwif_t *match = NULL;
1009
1010
1011	BUG_ON(in_interrupt());
1012	BUG_ON(irqs_disabled());
1013	BUG_ON(hwif == NULL);
1014
1015	down(&ide_cfg_sem);
1016	hwif->hwgroup = NULL;
1017#if MAX_HWIFS > 1
1018	/*
1019	 * Group up with any other hwifs that share our irq(s).
1020	 */
1021	for (index = 0; index < MAX_HWIFS; index++) {
1022		ide_hwif_t *h = &ide_hwifs[index];
1023		if (h->hwgroup) {  /* scan only initialized hwif's */
1024			if (hwif->irq == h->irq) {
1025				hwif->sharing_irq = h->sharing_irq = 1;
1026				if (hwif->chipset != ide_pci ||
1027				    h->chipset != ide_pci) {
1028					save_match(hwif, h, &match);
1029				}
1030			}
1031			if (hwif->serialized) {
1032				if (hwif->mate && hwif->mate->irq == h->irq)
1033					save_match(hwif, h, &match);
1034			}
1035			if (h->serialized) {
1036				if (h->mate && hwif->irq == h->mate->irq)
1037					save_match(hwif, h, &match);
1038			}
1039		}
1040	}
1041#endif /* MAX_HWIFS > 1 */
1042	/*
1043	 * If we are still without a hwgroup, then form a new one
1044	 */
1045	if (match) {
1046		hwgroup = match->hwgroup;
1047		hwif->hwgroup = hwgroup;
1048		/*
1049		 * Link us into the hwgroup.
1050		 * This must be done early, do ensure that unexpected_intr
1051		 * can find the hwif and prevent irq storms.
1052		 * No drives are attached to the new hwif, choose_drive
1053		 * can't do anything stupid (yet).
1054		 * Add ourself as the 2nd entry to the hwgroup->hwif
1055		 * linked list, the first entry is the hwif that owns
1056		 * hwgroup->handler - do not change that.
1057		 */
1058		spin_lock_irq(&ide_lock);
1059		hwif->next = hwgroup->hwif->next;
1060		hwgroup->hwif->next = hwif;
1061		spin_unlock_irq(&ide_lock);
1062	} else {
1063		hwgroup = kmalloc_node(sizeof(ide_hwgroup_t), GFP_KERNEL,
1064					hwif_to_node(hwif->drives[0].hwif));
1065		if (!hwgroup)
1066	       		goto out_up;
1067
1068		hwif->hwgroup = hwgroup;
1069
1070		memset(hwgroup, 0, sizeof(ide_hwgroup_t));
1071		hwgroup->hwif     = hwif->next = hwif;
1072		hwgroup->rq       = NULL;
1073		hwgroup->handler  = NULL;
1074		hwgroup->drive    = NULL;
1075		hwgroup->busy     = 0;
1076		init_timer(&hwgroup->timer);
1077		hwgroup->timer.function = &ide_timer_expiry;
1078		hwgroup->timer.data = (unsigned long) hwgroup;
1079	}
1080
1081	/*
1082	 * Allocate the irq, if not already obtained for another hwif
1083	 */
1084	if (!match || match->irq != hwif->irq) {
1085		int sa = IRQF_DISABLED;
1086#if defined(__mc68000__) || defined(CONFIG_APUS)
1087		sa = IRQF_SHARED;
1088#endif /* __mc68000__ || CONFIG_APUS */
1089
1090		if (IDE_CHIPSET_IS_PCI(hwif->chipset)) {
1091			sa = IRQF_SHARED;
1092#ifndef CONFIG_IDEPCI_SHARE_IRQ
1093			sa |= IRQF_DISABLED;
1094#endif /* CONFIG_IDEPCI_SHARE_IRQ */
1095		}
1096
1097		if (hwif->io_ports[IDE_CONTROL_OFFSET])
1098			/* clear nIEN */
1099			hwif->OUTB(0x08, hwif->io_ports[IDE_CONTROL_OFFSET]);
1100
1101		if (request_irq(hwif->irq,&ide_intr,sa,hwif->name,hwgroup))
1102	       		goto out_unlink;
1103	}
1104
1105	/*
1106	 * For any present drive:
1107	 * - allocate the block device queue
1108	 * - link drive into the hwgroup
1109	 */
1110	for (index = 0; index < MAX_DRIVES; ++index) {
1111		ide_drive_t *drive = &hwif->drives[index];
1112		if (!drive->present)
1113			continue;
1114		if (ide_init_queue(drive)) {
1115			printk(KERN_ERR "ide: failed to init %s\n",drive->name);
1116			continue;
1117		}
1118		spin_lock_irq(&ide_lock);
1119		if (!hwgroup->drive) {
1120			/* first drive for hwgroup. */
1121			drive->next = drive;
1122			hwgroup->drive = drive;
1123			hwgroup->hwif = HWIF(hwgroup->drive);
1124		} else {
1125			drive->next = hwgroup->drive->next;
1126			hwgroup->drive->next = drive;
1127		}
1128		spin_unlock_irq(&ide_lock);
1129	}
1130
1131#if !defined(__mc68000__) && !defined(CONFIG_APUS)
1132	printk("%s at 0x%03lx-0x%03lx,0x%03lx on irq %d", hwif->name,
1133		hwif->io_ports[IDE_DATA_OFFSET],
1134		hwif->io_ports[IDE_DATA_OFFSET]+7,
1135		hwif->io_ports[IDE_CONTROL_OFFSET], hwif->irq);
1136#else
1137	printk("%s at 0x%08lx on irq %d", hwif->name,
1138		hwif->io_ports[IDE_DATA_OFFSET], hwif->irq);
1139#endif /* __mc68000__ && CONFIG_APUS */
1140	if (match)
1141		printk(" (%sed with %s)",
1142			hwif->sharing_irq ? "shar" : "serializ", match->name);
1143	printk("\n");
1144	up(&ide_cfg_sem);
1145	return 0;
1146out_unlink:
1147	spin_lock_irq(&ide_lock);
1148	if (hwif->next == hwif) {
1149		BUG_ON(match);
1150		BUG_ON(hwgroup->hwif != hwif);
1151		kfree(hwgroup);
1152	} else {
1153		ide_hwif_t *g;
1154		g = hwgroup->hwif;
1155		while (g->next != hwif)
1156			g = g->next;
1157		g->next = hwif->next;
1158		if (hwgroup->hwif == hwif) {
1159			/* Impossible. */
1160			printk(KERN_ERR "Duh. Uninitialized hwif listed as active hwif.\n");
1161			hwgroup->hwif = g;
1162		}
1163		BUG_ON(hwgroup->hwif == hwif);
1164	}
1165	spin_unlock_irq(&ide_lock);
1166out_up:
1167	up(&ide_cfg_sem);
1168	return 1;
1169}
1170
1171static int ata_lock(dev_t dev, void *data)
1172{
1173	return 0;
1174}
1175
1176static struct kobject *ata_probe(dev_t dev, int *part, void *data)
1177{
1178	ide_hwif_t *hwif = data;
1179	int unit = *part >> PARTN_BITS;
1180	ide_drive_t *drive = &hwif->drives[unit];
1181	if (!drive->present)
1182		return NULL;
1183
1184	if (drive->media == ide_disk)
1185		request_module("ide-disk");
1186	if (drive->scsi)
1187		request_module("ide-scsi");
1188	if (drive->media == ide_cdrom || drive->media == ide_optical)
1189		request_module("ide-cd");
1190	if (drive->media == ide_tape)
1191		request_module("ide-tape");
1192	if (drive->media == ide_floppy)
1193		request_module("ide-floppy");
1194
1195	return NULL;
1196}
1197
1198static struct kobject *exact_match(dev_t dev, int *part, void *data)
1199{
1200	struct gendisk *p = data;
1201	*part &= (1 << PARTN_BITS) - 1;
1202	return &p->kobj;
1203}
1204
1205static int exact_lock(dev_t dev, void *data)
1206{
1207	struct gendisk *p = data;
1208
1209	if (!get_disk(p))
1210		return -1;
1211	return 0;
1212}
1213
1214void ide_register_region(struct gendisk *disk)
1215{
1216	blk_register_region(MKDEV(disk->major, disk->first_minor),
1217			    disk->minors, NULL, exact_match, exact_lock, disk);
1218}
1219
1220EXPORT_SYMBOL_GPL(ide_register_region);
1221
1222void ide_unregister_region(struct gendisk *disk)
1223{
1224	blk_unregister_region(MKDEV(disk->major, disk->first_minor),
1225			      disk->minors);
1226}
1227
1228EXPORT_SYMBOL_GPL(ide_unregister_region);
1229
1230void ide_init_disk(struct gendisk *disk, ide_drive_t *drive)
1231{
1232	ide_hwif_t *hwif = drive->hwif;
1233	unsigned int unit = (drive->select.all >> 4) & 1;
1234
1235	disk->major = hwif->major;
1236	disk->first_minor = unit << PARTN_BITS;
1237	sprintf(disk->disk_name, "hd%c", 'a' + hwif->index * MAX_DRIVES + unit);
1238	disk->queue = drive->queue;
1239}
1240
1241EXPORT_SYMBOL_GPL(ide_init_disk);
1242
1243static void ide_remove_drive_from_hwgroup(ide_drive_t *drive)
1244{
1245	ide_hwgroup_t *hwgroup = drive->hwif->hwgroup;
1246
1247	if (drive == drive->next) {
1248		/* special case: last drive from hwgroup. */
1249		BUG_ON(hwgroup->drive != drive);
1250		hwgroup->drive = NULL;
1251	} else {
1252		ide_drive_t *walk;
1253
1254		walk = hwgroup->drive;
1255		while (walk->next != drive)
1256			walk = walk->next;
1257		walk->next = drive->next;
1258		if (hwgroup->drive == drive) {
1259			hwgroup->drive = drive->next;
1260			hwgroup->hwif = hwgroup->drive->hwif;
1261		}
1262	}
1263	BUG_ON(hwgroup->drive == drive);
1264}
1265
1266static void drive_release_dev (struct device *dev)
1267{
1268	ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
1269
1270	spin_lock_irq(&ide_lock);
1271	ide_remove_drive_from_hwgroup(drive);
1272	kfree(drive->id);
1273	drive->id = NULL;
1274	drive->present = 0;
1275	/* Messed up locking ... */
1276	spin_unlock_irq(&ide_lock);
1277	blk_cleanup_queue(drive->queue);
1278	spin_lock_irq(&ide_lock);
1279	drive->queue = NULL;
1280	spin_unlock_irq(&ide_lock);
1281
1282	complete(&drive->gendev_rel_comp);
1283}
1284
1285/*
1286 * init_gendisk() (as opposed to ide_geninit) is called for each major device,
1287 * after probing for drives, to allocate partition tables and other data
1288 * structures needed for the routines in genhd.c.  ide_geninit() gets called
1289 * somewhat later, during the partition check.
1290 */
1291static void init_gendisk (ide_hwif_t *hwif)
1292{
1293	unsigned int unit;
1294
1295	for (unit = 0; unit < MAX_DRIVES; ++unit) {
1296		ide_drive_t * drive = &hwif->drives[unit];
1297		ide_add_generic_settings(drive);
1298		snprintf(drive->gendev.bus_id,BUS_ID_SIZE,"%u.%u",
1299			 hwif->index,unit);
1300		drive->gendev.parent = &hwif->gendev;
1301		drive->gendev.bus = &ide_bus_type;
1302		drive->gendev.driver_data = drive;
1303		drive->gendev.release = drive_release_dev;
1304	}
1305	blk_register_region(MKDEV(hwif->major, 0), MAX_DRIVES << PARTN_BITS,
1306			THIS_MODULE, ata_probe, ata_lock, hwif);
1307}
1308
1309static int hwif_init(ide_hwif_t *hwif)
1310{
1311	int old_irq;
1312
1313	/* Return success if no device is connected */
1314	if (!hwif->present)
1315		return 1;
1316
1317	if (!hwif->irq) {
1318		if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET])))
1319		{
1320			printk("%s: DISABLED, NO IRQ\n", hwif->name);
1321			return (hwif->present = 0);
1322		}
1323	}
1324#ifdef CONFIG_BLK_DEV_HD
1325	if (hwif->irq == HD_IRQ && hwif->io_ports[IDE_DATA_OFFSET] != HD_DATA) {
1326		printk("%s: CANNOT SHARE IRQ WITH OLD "
1327			"HARDDISK DRIVER (hd.c)\n", hwif->name);
1328		return (hwif->present = 0);
1329	}
1330#endif /* CONFIG_BLK_DEV_HD */
1331
1332	/* we set it back to 1 if all is ok below */
1333	hwif->present = 0;
1334
1335	if (register_blkdev(hwif->major, hwif->name))
1336		return 0;
1337
1338	if (!hwif->sg_max_nents)
1339		hwif->sg_max_nents = PRD_ENTRIES;
1340
1341	hwif->sg_table = kmalloc(sizeof(struct scatterlist)*hwif->sg_max_nents,
1342				 GFP_KERNEL);
1343	if (!hwif->sg_table) {
1344		printk(KERN_ERR "%s: unable to allocate SG table.\n", hwif->name);
1345		goto out;
1346	}
1347
1348	if (init_irq(hwif) == 0)
1349		goto done;
1350
1351	old_irq = hwif->irq;
1352	/*
1353	 *	It failed to initialise. Find the default IRQ for
1354	 *	this port and try that.
1355	 */
1356	if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET]))) {
1357		printk("%s: Disabled unable to get IRQ %d.\n",
1358			hwif->name, old_irq);
1359		goto out;
1360	}
1361	if (init_irq(hwif)) {
1362		printk("%s: probed IRQ %d and default IRQ %d failed.\n",
1363			hwif->name, old_irq, hwif->irq);
1364		goto out;
1365	}
1366	printk("%s: probed IRQ %d failed, using default.\n",
1367		hwif->name, hwif->irq);
1368
1369done:
1370	init_gendisk(hwif);
1371
1372	ide_acpi_init(hwif);
1373
1374	hwif->present = 1;	/* success */
1375	return 1;
1376
1377out:
1378	unregister_blkdev(hwif->major, hwif->name);
1379	return 0;
1380}
1381
1382int ideprobe_init (void)
1383{
1384	unsigned int index;
1385	int probe[MAX_HWIFS];
1386
1387	memset(probe, 0, MAX_HWIFS * sizeof(int));
1388	for (index = 0; index < MAX_HWIFS; ++index)
1389		probe[index] = !ide_hwifs[index].present;
1390
1391	for (index = 0; index < MAX_HWIFS; ++index)
1392		if (probe[index])
1393			probe_hwif(&ide_hwifs[index], NULL);
1394	for (index = 0; index < MAX_HWIFS; ++index)
1395		if (probe[index])
1396			hwif_init(&ide_hwifs[index]);
1397	for (index = 0; index < MAX_HWIFS; ++index) {
1398		if (probe[index]) {
1399			ide_hwif_t *hwif = &ide_hwifs[index];
1400			int unit;
1401			if (!hwif->present)
1402				continue;
1403			if (hwif->chipset == ide_unknown || hwif->chipset == ide_forced)
1404				hwif->chipset = ide_generic;
1405			for (unit = 0; unit < MAX_DRIVES; ++unit)
1406				if (hwif->drives[unit].present) {
1407					int ret = device_register(
1408						&hwif->drives[unit].gendev);
1409					if (ret < 0)
1410						printk(KERN_WARNING "IDE: %s: "
1411							"device_register error: %d\n",
1412							__FUNCTION__, ret);
1413				}
1414		}
1415	}
1416	for (index = 0; index < MAX_HWIFS; ++index)
1417		if (probe[index])
1418			ide_proc_register_port(&ide_hwifs[index]);
1419	return 0;
1420}
1421
1422EXPORT_SYMBOL_GPL(ideprobe_init);
1423