File: /usr/src/linux/drivers/ide/ide-probe.c

1     /*
2      *  linux/drivers/ide/ide-probe.c	Version 1.07	March 18, 2001
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      */
31     
32     #undef REALLY_SLOW_IO		/* most systems can safely undef this */
33     
34     #include <linux/config.h>
35     #include <linux/module.h>
36     #include <linux/types.h>
37     #include <linux/string.h>
38     #include <linux/kernel.h>
39     #include <linux/timer.h>
40     #include <linux/mm.h>
41     #include <linux/interrupt.h>
42     #include <linux/major.h>
43     #include <linux/errno.h>
44     #include <linux/genhd.h>
45     #include <linux/slab.h>
46     #include <linux/delay.h>
47     #include <linux/ide.h>
48     #include <linux/spinlock.h>
49     
50     #include <asm/byteorder.h>
51     #include <asm/irq.h>
52     #include <asm/uaccess.h>
53     #include <asm/io.h>
54     
55     static inline void do_identify (ide_drive_t *drive, byte cmd)
56     {
57     	int bswap = 1;
58     	struct hd_driveid *id;
59     
60     	id = drive->id = kmalloc (SECTOR_WORDS*4, GFP_ATOMIC);	/* called with interrupts disabled! */
61     	ide_input_data(drive, id, SECTOR_WORDS);		/* read 512 bytes of id info */
62     	ide__sti();	/* local CPU only */
63     	ide_fix_driveid(id);
64     
65     	if (id->word156 == 0x4d42) {
66     		printk("%s: drive->id->word156 == 0x%04x \n", drive->name, drive->id->word156);
67     	}
68     
69     	if (!drive->forced_lun)
70     		drive->last_lun = id->last_lun & 0x7;
71     #if defined (CONFIG_SCSI_EATA_DMA) || defined (CONFIG_SCSI_EATA_PIO) || defined (CONFIG_SCSI_EATA)
72     	/*
73     	 * EATA SCSI controllers do a hardware ATA emulation:
74     	 * Ignore them if there is a driver for them available.
75     	 */
76     	if ((id->model[0] == 'P' && id->model[1] == 'M')
77     	 || (id->model[0] == 'S' && id->model[1] == 'K')) {
78     		printk("%s: EATA SCSI HBA %.10s\n", drive->name, id->model);
79     		drive->present = 0;
80     		return;
81     	}
82     #endif /* CONFIG_SCSI_EATA_DMA || CONFIG_SCSI_EATA_PIO */
83     
84     	/*
85     	 *  WIN_IDENTIFY returns little-endian info,
86     	 *  WIN_PIDENTIFY *usually* returns little-endian info.
87     	 */
88     	if (cmd == WIN_PIDENTIFY) {
89     		if ((id->model[0] == 'N' && id->model[1] == 'E') /* NEC */
90     		 || (id->model[0] == 'F' && id->model[1] == 'X') /* Mitsumi */
91     		 || (id->model[0] == 'P' && id->model[1] == 'i'))/* Pioneer */
92     			bswap ^= 1;	/* Vertos drives may still be weird */
93     	}
94     	ide_fixstring (id->model,     sizeof(id->model),     bswap);
95     	ide_fixstring (id->fw_rev,    sizeof(id->fw_rev),    bswap);
96     	ide_fixstring (id->serial_no, sizeof(id->serial_no), bswap);
97     
98     	if (strstr(id->model, "E X A B Y T E N E S T"))
99     		return;
100     
101     	id->model[sizeof(id->model)-1] = '\0';	/* we depend on this a lot! */
102     	printk("%s: %s, ", drive->name, id->model);
103     	drive->present = 1;
104     
105     	/*
106     	 * Check for an ATAPI device
107     	 */
108     	if (cmd == WIN_PIDENTIFY) {
109     		byte type = (id->config >> 8) & 0x1f;
110     		printk("ATAPI ");
111     #ifdef CONFIG_BLK_DEV_PDC4030
112     		if (HWIF(drive)->channel == 1 && HWIF(drive)->chipset == ide_pdc4030) {
113     			printk(" -- not supported on 2nd Promise port\n");
114     			drive->present = 0;
115     			return;
116     		}
117     #endif /* CONFIG_BLK_DEV_PDC4030 */
118     		switch (type) {
119     			case ide_floppy:
120     				if (!strstr(id->model, "CD-ROM")) {
121     					if (!strstr(id->model, "oppy") && !strstr(id->model, "poyp") && !strstr(id->model, "ZIP"))
122     						printk("cdrom or floppy?, assuming ");
123     					if (drive->media != ide_cdrom) {
124     						printk ("FLOPPY");
125     						break;
126     					}
127     				}
128     				type = ide_cdrom;	/* Early cdrom models used zero */
129     			case ide_cdrom:
130     				drive->removable = 1;
131     #ifdef CONFIG_PPC
132     				/* kludge for Apple PowerBook internal zip */
133     				if (!strstr(id->model, "CD-ROM") && strstr(id->model, "ZIP")) {
134     					printk ("FLOPPY");
135     					type = ide_floppy;
136     					break;
137     				}
138     #endif
139     				printk ("CD/DVD-ROM");
140     				break;
141     			case ide_tape:
142     				printk ("TAPE");
143     				break;
144     			case ide_optical:
145     				printk ("OPTICAL");
146     				drive->removable = 1;
147     				break;
148     			default:
149     				printk("UNKNOWN (type %d)", type);
150     				break;
151     		}
152     		printk (" drive\n");
153     		drive->media = type;
154     		return;
155     	}
156     
157     	/*
158     	 * Not an ATAPI device: looks like a "regular" hard disk
159     	 */
160     	if (id->config & (1<<7))
161     		drive->removable = 1;
162     	/*
163     	 * Prevent long system lockup probing later for non-existant
164     	 * slave drive if the hwif is actually a flash memory card of some variety:
165     	 */
166     	if (drive_is_flashcard(drive)) {
167     		ide_drive_t *mate = &HWIF(drive)->drives[1^drive->select.b.unit];
168     		if (!mate->ata_flash) {
169     			mate->present = 0;
170     			mate->noprobe = 1;
171     		}
172     	}
173     	drive->media = ide_disk;
174     	printk("ATA DISK drive\n");
175     	QUIRK_LIST(HWIF(drive),drive);
176     	return;
177     }
178     
179     /*
180      * try_to_identify() sends an ATA(PI) IDENTIFY request to a drive
181      * and waits for a response.  It also monitors irqs while this is
182      * happening, in hope of automatically determining which one is
183      * being used by the interface.
184      *
185      * Returns:	0  device was identified
186      *		1  device timed-out (no response to identify request)
187      *		2  device aborted the command (refused to identify itself)
188      */
189     static int actual_try_to_identify (ide_drive_t *drive, byte cmd)
190     {
191     	int rc;
192     	ide_ioreg_t hd_status;
193     	unsigned long timeout;
194     	byte s, a;
195     
196     	if (IDE_CONTROL_REG) {
197     		/* take a deep breath */
198     		ide_delay_50ms();
199     		a = IN_BYTE(IDE_ALTSTATUS_REG);
200     		s = IN_BYTE(IDE_STATUS_REG);
201     		if ((a ^ s) & ~INDEX_STAT) {
202     			printk("%s: probing with STATUS(0x%02x) instead of ALTSTATUS(0x%02x)\n", drive->name, s, a);
203     			hd_status = IDE_STATUS_REG;	/* ancient Seagate drives, broken interfaces */
204     		} else {
205     			hd_status = IDE_ALTSTATUS_REG;	/* use non-intrusive polling */
206     		}
207     	} else {
208     		ide_delay_50ms();
209     		hd_status = IDE_STATUS_REG;
210     	}
211     
212     	/* set features register for atapi identify command to be sure of reply */
213     	if ((cmd == WIN_PIDENTIFY))
214     		OUT_BYTE(0,IDE_FEATURE_REG);	/* disable dma & overlap */
215     
216     #if CONFIG_BLK_DEV_PDC4030
217     	if (HWIF(drive)->chipset == ide_pdc4030) {
218     		/* DC4030 hosted drives need their own identify... */
219     		extern int pdc4030_identify(ide_drive_t *);
220     		if (pdc4030_identify(drive)) {
221     			return 1;
222     		}
223     	} else
224     #endif /* CONFIG_BLK_DEV_PDC4030 */
225     		OUT_BYTE(cmd,IDE_COMMAND_REG);		/* ask drive for ID */
226     	timeout = ((cmd == WIN_IDENTIFY) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
227     	timeout += jiffies;
228     	do {
229     		if (0 < (signed long)(jiffies - timeout)) {
230     			return 1;	/* drive timed-out */
231     		}
232     		ide_delay_50ms();		/* give drive a breather */
233     	} while (IN_BYTE(hd_status) & BUSY_STAT);
234     
235     	ide_delay_50ms();		/* wait for IRQ and DRQ_STAT */
236     	if (OK_STAT(GET_STAT(),DRQ_STAT,BAD_R_STAT)) {
237     		unsigned long flags;
238     		__save_flags(flags);	/* local CPU only */
239     		__cli();		/* local CPU only; some systems need this */
240     		do_identify(drive, cmd); /* drive returned ID */
241     		rc = 0;			/* drive responded with ID */
242     		(void) GET_STAT();	/* clear drive IRQ */
243     		__restore_flags(flags);	/* local CPU only */
244     	} else
245     		rc = 2;			/* drive refused ID */
246     	return rc;
247     }
248     
249     static int try_to_identify (ide_drive_t *drive, byte cmd)
250     {
251     	int retval;
252     	int autoprobe = 0;
253     	unsigned long cookie = 0;
254     
255     	if (IDE_CONTROL_REG && !HWIF(drive)->irq) {
256     		autoprobe = 1;
257     		cookie = probe_irq_on();
258     		OUT_BYTE(drive->ctl,IDE_CONTROL_REG);	/* enable device irq */
259     	}
260     
261     	retval = actual_try_to_identify(drive, cmd);
262     
263     	if (autoprobe) {
264     		int irq;
265     		OUT_BYTE(drive->ctl|2,IDE_CONTROL_REG);	/* mask device irq */
266     		(void) GET_STAT();			/* clear drive IRQ */
267     		udelay(5);
268     		irq = probe_irq_off(cookie);
269     		if (!HWIF(drive)->irq) {
270     			if (irq > 0) {
271     				HWIF(drive)->irq = irq;
272     			} else {	/* Mmmm.. multiple IRQs.. don't know which was ours */
273     				printk("%s: IRQ probe failed (0x%lx)\n", drive->name, cookie);
274     #ifdef CONFIG_BLK_DEV_CMD640
275     #ifdef CMD640_DUMP_REGS
276     				if (HWIF(drive)->chipset == ide_cmd640) {
277     					printk("%s: Hmmm.. probably a driver problem.\n", drive->name);
278     					CMD640_DUMP_REGS;
279     				}
280     #endif /* CMD640_DUMP_REGS */
281     #endif /* CONFIG_BLK_DEV_CMD640 */
282     			}
283     		}
284     	}
285     	return retval;
286     }
287     
288     
289     /*
290      * do_probe() has the difficult job of finding a drive if it exists,
291      * without getting hung up if it doesn't exist, without trampling on
292      * ethernet cards, and without leaving any IRQs dangling to haunt us later.
293      *
294      * If a drive is "known" to exist (from CMOS or kernel parameters),
295      * but does not respond right away, the probe will "hang in there"
296      * for the maximum wait time (about 30 seconds), otherwise it will
297      * exit much more quickly.
298      *
299      * Returns:	0  device was identified
300      *		1  device timed-out (no response to identify request)
301      *		2  device aborted the command (refused to identify itself)
302      *		3  bad status from device (possible for ATAPI drives)
303      *		4  probe was not attempted because failure was obvious
304      */
305     static int do_probe (ide_drive_t *drive, byte cmd)
306     {
307     	int rc;
308     	ide_hwif_t *hwif = HWIF(drive);
309     	if (drive->present) {	/* avoid waiting for inappropriate probes */
310     		if ((drive->media != ide_disk) && (cmd == WIN_IDENTIFY))
311     			return 4;
312     	}
313     #ifdef DEBUG
314     	printk("probing for %s: present=%d, media=%d, probetype=%s\n",
315     		drive->name, drive->present, drive->media,
316     		(cmd == WIN_IDENTIFY) ? "ATA" : "ATAPI");
317     #endif
318     	ide_delay_50ms();	/* needed for some systems (e.g. crw9624 as drive0 with disk as slave) */
319     	SELECT_DRIVE(hwif,drive);
320     	ide_delay_50ms();
321     	if (IN_BYTE(IDE_SELECT_REG) != drive->select.all && !drive->present) {
322     		if (drive->select.b.unit != 0) {
323     			SELECT_DRIVE(hwif,&hwif->drives[0]);	/* exit with drive0 selected */
324     			ide_delay_50ms();		/* allow BUSY_STAT to assert & clear */
325     		}
326     		return 3;    /* no i/f present: mmm.. this should be a 4 -ml */
327     	}
328     
329     	if (OK_STAT(GET_STAT(),READY_STAT,BUSY_STAT)
330     	 || drive->present || cmd == WIN_PIDENTIFY)
331     	{
332     		if ((rc = try_to_identify(drive,cmd)))   /* send cmd and wait */
333     			rc = try_to_identify(drive,cmd); /* failed: try again */
334     		if (rc == 1 && cmd == WIN_PIDENTIFY && drive->autotune != 2) {
335     			unsigned long timeout;
336     			printk("%s: no response (status = 0x%02x), resetting drive\n", drive->name, GET_STAT());
337     			ide_delay_50ms();
338     			OUT_BYTE (drive->select.all, IDE_SELECT_REG);
339     			ide_delay_50ms();
340     			OUT_BYTE(WIN_SRST, IDE_COMMAND_REG);
341     			timeout = jiffies;
342     			while ((GET_STAT() & BUSY_STAT) && time_before(jiffies, timeout + WAIT_WORSTCASE))
343     				ide_delay_50ms();
344     			rc = try_to_identify(drive, cmd);
345     		}
346     		if (rc == 1)
347     			printk("%s: no response (status = 0x%02x)\n", drive->name, GET_STAT());
348     		(void) GET_STAT();		/* ensure drive irq is clear */
349     	} else {
350     		rc = 3;				/* not present or maybe ATAPI */
351     	}
352     	if (drive->select.b.unit != 0) {
353     		SELECT_DRIVE(hwif,&hwif->drives[0]);	/* exit with drive0 selected */
354     		ide_delay_50ms();
355     		(void) GET_STAT();		/* ensure drive irq is clear */
356     	}
357     	return rc;
358     }
359     
360     /*
361      *
362      */
363     static void enable_nest (ide_drive_t *drive)
364     {
365     	unsigned long timeout;
366     
367     	printk("%s: enabling %s -- ", HWIF(drive)->name, drive->id->model);
368     	SELECT_DRIVE(HWIF(drive), drive);
369     	ide_delay_50ms();
370     	OUT_BYTE(EXABYTE_ENABLE_NEST, IDE_COMMAND_REG);
371     	timeout = jiffies + WAIT_WORSTCASE;
372     	do {
373     		if (jiffies > timeout) {
374     			printk("failed (timeout)\n");
375     			return;
376     		}
377     		ide_delay_50ms();
378     	} while (GET_STAT() & BUSY_STAT);
379     	ide_delay_50ms();
380     	if (!OK_STAT(GET_STAT(), 0, BAD_STAT))
381     		printk("failed (status = 0x%02x)\n", GET_STAT());
382     	else
383     		printk("success\n");
384     	if (do_probe(drive, WIN_IDENTIFY) >= 2) {	/* if !(success||timed-out) */
385     		(void) do_probe(drive, WIN_PIDENTIFY);	/* look for ATAPI device */
386     	}
387     }
388     
389     /*
390      * probe_for_drive() tests for existence of a given drive using do_probe().
391      *
392      * Returns:	0  no device was found
393      *		1  device was found (note: drive->present might still be 0)
394      */
395     static inline byte probe_for_drive (ide_drive_t *drive)
396     {
397     	if (drive->noprobe)			/* skip probing? */
398     		return drive->present;
399     	if (do_probe(drive, WIN_IDENTIFY) >= 2) { /* if !(success||timed-out) */
400     		(void) do_probe(drive, WIN_PIDENTIFY); /* look for ATAPI device */
401     	}
402     	if (drive->id && strstr(drive->id->model, "E X A B Y T E N E S T"))
403     		enable_nest(drive);
404     	if (!drive->present)
405     		return 0;			/* drive not found */
406     	if (drive->id == NULL) {		/* identification failed? */
407     		if (drive->media == ide_disk) {
408     			printk ("%s: non-IDE drive, CHS=%d/%d/%d\n",
409     			 drive->name, drive->cyl, drive->head, drive->sect);
410     		} else if (drive->media == ide_cdrom) {
411     			printk("%s: ATAPI cdrom (?)\n", drive->name);
412     		} else {
413     			drive->present = 0;	/* nuke it */
414     		}
415     	}
416     	return 1;	/* drive was found */
417     }
418     
419     /*
420      * Calculate the region that this interface occupies,
421      * handling interfaces where the registers may not be
422      * ordered sanely.  We deal with the CONTROL register
423      * separately.
424      */
425     static int hwif_check_regions (ide_hwif_t *hwif)
426     {
427     	int region_errors = 0;
428     
429     	hwif->straight8 = 0;
430     	region_errors  = ide_check_region(hwif->io_ports[IDE_DATA_OFFSET], 1);
431     	region_errors += ide_check_region(hwif->io_ports[IDE_ERROR_OFFSET], 1);
432     	region_errors += ide_check_region(hwif->io_ports[IDE_NSECTOR_OFFSET], 1);
433     	region_errors += ide_check_region(hwif->io_ports[IDE_SECTOR_OFFSET], 1);
434     	region_errors += ide_check_region(hwif->io_ports[IDE_LCYL_OFFSET], 1);
435     	region_errors += ide_check_region(hwif->io_ports[IDE_HCYL_OFFSET], 1);
436     	region_errors += ide_check_region(hwif->io_ports[IDE_SELECT_OFFSET], 1);
437     	region_errors += ide_check_region(hwif->io_ports[IDE_STATUS_OFFSET], 1);
438     
439     	if (hwif->io_ports[IDE_CONTROL_OFFSET])
440     		region_errors += ide_check_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1);
441     #if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
442     	if (hwif->io_ports[IDE_IRQ_OFFSET])
443     		region_errors += ide_check_region(hwif->io_ports[IDE_IRQ_OFFSET], 1);
444     #endif /* (CONFIG_AMIGA) || (CONFIG_MAC) */
445     	/*
446     	 * If any errors are return, we drop the hwif interface.
447     	 */
448     	return(region_errors);
449     }
450     
451     static void hwif_register (ide_hwif_t *hwif)
452     {
453     	if (((unsigned long)hwif->io_ports[IDE_DATA_OFFSET] | 7) ==
454     	    ((unsigned long)hwif->io_ports[IDE_STATUS_OFFSET])) {
455     		ide_request_region(hwif->io_ports[IDE_DATA_OFFSET], 8, hwif->name);
456     		hwif->straight8 = 1;
457     		goto jump_straight8;
458     	}
459     
460     	if (hwif->io_ports[IDE_DATA_OFFSET])
461     		ide_request_region(hwif->io_ports[IDE_DATA_OFFSET], 1, hwif->name);
462     	if (hwif->io_ports[IDE_ERROR_OFFSET])
463     		ide_request_region(hwif->io_ports[IDE_ERROR_OFFSET], 1, hwif->name);
464     	if (hwif->io_ports[IDE_NSECTOR_OFFSET])
465     		ide_request_region(hwif->io_ports[IDE_NSECTOR_OFFSET], 1, hwif->name);
466     	if (hwif->io_ports[IDE_SECTOR_OFFSET])
467     		ide_request_region(hwif->io_ports[IDE_SECTOR_OFFSET], 1, hwif->name);
468     	if (hwif->io_ports[IDE_LCYL_OFFSET])
469     		ide_request_region(hwif->io_ports[IDE_LCYL_OFFSET], 1, hwif->name);
470     	if (hwif->io_ports[IDE_HCYL_OFFSET])
471     		ide_request_region(hwif->io_ports[IDE_HCYL_OFFSET], 1, hwif->name);
472     	if (hwif->io_ports[IDE_SELECT_OFFSET])
473     		ide_request_region(hwif->io_ports[IDE_SELECT_OFFSET], 1, hwif->name);
474     	if (hwif->io_ports[IDE_STATUS_OFFSET])
475     		ide_request_region(hwif->io_ports[IDE_STATUS_OFFSET], 1, hwif->name);
476     
477     jump_straight8:
478     	if (hwif->io_ports[IDE_CONTROL_OFFSET])
479     		ide_request_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1, hwif->name);
480     #if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
481     	if (hwif->io_ports[IDE_IRQ_OFFSET])
482     		ide_request_region(hwif->io_ports[IDE_IRQ_OFFSET], 1, hwif->name);
483     #endif /* (CONFIG_AMIGA) || (CONFIG_MAC) */
484     }
485     
486     /*
487      * This routine only knows how to look for drive units 0 and 1
488      * on an interface, so any setting of MAX_DRIVES > 2 won't work here.
489      */
490     static void probe_hwif (ide_hwif_t *hwif)
491     {
492     	unsigned int unit;
493     	unsigned long flags;
494     
495     	if (hwif->noprobe)
496     		return;
497     #ifdef CONFIG_BLK_DEV_IDE
498     	if (hwif->io_ports[IDE_DATA_OFFSET] == HD_DATA) {
499     		extern void probe_cmos_for_drives(ide_hwif_t *);
500     
501     		probe_cmos_for_drives (hwif);
502     	}
503     #endif
504     
505     	if ((hwif->chipset != ide_4drives || !hwif->mate->present) &&
506     #if CONFIG_BLK_DEV_PDC4030
507     	    (hwif->chipset != ide_pdc4030 || hwif->channel == 0) &&
508     #endif /* CONFIG_BLK_DEV_PDC4030 */
509     	    (hwif_check_regions(hwif))) {
510     		int msgout = 0;
511     		for (unit = 0; unit < MAX_DRIVES; ++unit) {
512     			ide_drive_t *drive = &hwif->drives[unit];
513     			if (drive->present) {
514     				drive->present = 0;
515     				printk("%s: ERROR, PORTS ALREADY IN USE\n", drive->name);
516     				msgout = 1;
517     			}
518     		}
519     		if (!msgout)
520     			printk("%s: ports already in use, skipping probe\n", hwif->name);
521     		return;	
522     	}
523     
524     	__save_flags(flags);	/* local CPU only */
525     	__sti();		/* local CPU only; needed for jiffies and irq probing */
526     	/*
527     	 * Second drive should only exist if first drive was found,
528     	 * but a lot of cdrom drives are configured as single slaves.
529     	 */
530     	for (unit = 0; unit < MAX_DRIVES; ++unit) {
531     		ide_drive_t *drive = &hwif->drives[unit];
532     		(void) probe_for_drive (drive);
533     		if (drive->present && !hwif->present) {
534     			hwif->present = 1;
535     			if (hwif->chipset != ide_4drives || !hwif->mate->present) {
536     				hwif_register(hwif);
537     			}
538     		}
539     	}
540     	if (hwif->io_ports[IDE_CONTROL_OFFSET] && hwif->reset) {
541     		unsigned long timeout = jiffies + WAIT_WORSTCASE;
542     		byte stat;
543     
544     		printk("%s: reset\n", hwif->name);
545     		OUT_BYTE(12, hwif->io_ports[IDE_CONTROL_OFFSET]);
546     		udelay(10);
547     		OUT_BYTE(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
548     		do {
549     			ide_delay_50ms();
550     			stat = IN_BYTE(hwif->io_ports[IDE_STATUS_OFFSET]);
551     		} while ((stat & BUSY_STAT) && 0 < (signed long)(timeout - jiffies));
552     
553     	}
554     	__restore_flags(flags);	/* local CPU only */
555     	for (unit = 0; unit < MAX_DRIVES; ++unit) {
556     		ide_drive_t *drive = &hwif->drives[unit];
557     		if (drive->present) {
558     			ide_tuneproc_t *tuneproc = HWIF(drive)->tuneproc;
559     			if (tuneproc != NULL && drive->autotune == 1)
560     				tuneproc(drive, 255);	/* auto-tune PIO mode */
561     		}
562     	}
563     }
564     
565     #if MAX_HWIFS > 1
566     /*
567      * save_match() is used to simplify logic in init_irq() below.
568      *
569      * A loophole here is that we may not know about a particular
570      * hwif's irq until after that hwif is actually probed/initialized..
571      * This could be a problem for the case where an hwif is on a
572      * dual interface that requires serialization (eg. cmd640) and another
573      * hwif using one of the same irqs is initialized beforehand.
574      *
575      * This routine detects and reports such situations, but does not fix them.
576      */
577     static void save_match (ide_hwif_t *hwif, ide_hwif_t *new, ide_hwif_t **match)
578     {
579     	ide_hwif_t *m = *match;
580     
581     	if (m && m->hwgroup && m->hwgroup != new->hwgroup) {
582     		if (!new->hwgroup)
583     			return;
584     		printk("%s: potential irq problem with %s and %s\n", hwif->name, new->name, m->name);
585     	}
586     	if (!m || m->irq != hwif->irq) /* don't undo a prior perfect match */
587     		*match = new;
588     }
589     #endif /* MAX_HWIFS > 1 */
590     
591     /*
592      * init request queue
593      */
594     static void ide_init_queue(ide_drive_t *drive)
595     {
596     	request_queue_t *q = &drive->queue;
597     
598     	q->queuedata = HWGROUP(drive);
599     	blk_init_queue(q, do_ide_request);
600     }
601     
602     /*
603      * This routine sets up the irq for an ide interface, and creates a new
604      * hwgroup for the irq/hwif if none was previously assigned.
605      *
606      * Much of the code is for correctly detecting/handling irq sharing
607      * and irq serialization situations.  This is somewhat complex because
608      * it handles static as well as dynamic (PCMCIA) IDE interfaces.
609      *
610      * The SA_INTERRUPT in sa_flags means ide_intr() is always entered with
611      * interrupts completely disabled.  This can be bad for interrupt latency,
612      * but anything else has led to problems on some machines.  We re-enable
613      * interrupts as much as we can safely do in most places.
614      */
615     static int init_irq (ide_hwif_t *hwif)
616     {
617     	unsigned long flags;
618     	unsigned int index;
619     	ide_hwgroup_t *hwgroup, *new_hwgroup;
620     	ide_hwif_t *match = NULL;
621     
622     	
623     	/* Allocate the buffer and potentially sleep first */
624     	
625     	new_hwgroup = kmalloc(sizeof(ide_hwgroup_t),GFP_KERNEL);
626     	
627     	save_flags(flags);	/* all CPUs */
628     	cli();			/* all CPUs */
629     
630     	hwif->hwgroup = NULL;
631     #if MAX_HWIFS > 1
632     	/*
633     	 * Group up with any other hwifs that share our irq(s).
634     	 */
635     	for (index = 0; index < MAX_HWIFS; index++) {
636     		ide_hwif_t *h = &ide_hwifs[index];
637     		if (h->hwgroup) {  /* scan only initialized hwif's */
638     			if (hwif->irq == h->irq) {
639     				hwif->sharing_irq = h->sharing_irq = 1;
640     				if (hwif->chipset != ide_pci || h->chipset != ide_pci) {
641     					save_match(hwif, h, &match);
642     				}
643     			}
644     			if (hwif->serialized) {
645     				if (hwif->mate && hwif->mate->irq == h->irq)
646     					save_match(hwif, h, &match);
647     			}
648     			if (h->serialized) {
649     				if (h->mate && hwif->irq == h->mate->irq)
650     					save_match(hwif, h, &match);
651     			}
652     		}
653     	}
654     #endif /* MAX_HWIFS > 1 */
655     	/*
656     	 * If we are still without a hwgroup, then form a new one
657     	 */
658     	if (match) {
659     		hwgroup = match->hwgroup;
660     		if(new_hwgroup)
661     			kfree(new_hwgroup);
662     	} else {
663     		hwgroup = new_hwgroup;
664     		if (!hwgroup) {
665     			restore_flags(flags);	/* all CPUs */
666     			return 1;
667     		}
668     		memset(hwgroup, 0, sizeof(ide_hwgroup_t));
669     		hwgroup->hwif     = hwif->next = hwif;
670     		hwgroup->rq       = NULL;
671     		hwgroup->handler  = NULL;
672     		hwgroup->drive    = NULL;
673     		hwgroup->busy     = 0;
674     		init_timer(&hwgroup->timer);
675     		hwgroup->timer.function = &ide_timer_expiry;
676     		hwgroup->timer.data = (unsigned long) hwgroup;
677     	}
678     
679     	/*
680     	 * Allocate the irq, if not already obtained for another hwif
681     	 */
682     	if (!match || match->irq != hwif->irq) {
683     #ifdef CONFIG_IDEPCI_SHARE_IRQ
684     		int sa = IDE_CHIPSET_IS_PCI(hwif->chipset) ? SA_SHIRQ : SA_INTERRUPT;
685     #else /* !CONFIG_IDEPCI_SHARE_IRQ */
686     		int sa = IDE_CHIPSET_IS_PCI(hwif->chipset) ? SA_INTERRUPT|SA_SHIRQ : SA_INTERRUPT;
687     #endif /* CONFIG_IDEPCI_SHARE_IRQ */
688     		if (ide_request_irq(hwif->irq, &ide_intr, sa, hwif->name, hwgroup)) {
689     			if (!match)
690     				kfree(hwgroup);
691     			restore_flags(flags);	/* all CPUs */
692     			return 1;
693     		}
694     	}
695     
696     	/*
697     	 * Everything is okay, so link us into the hwgroup
698     	 */
699     	hwif->hwgroup = hwgroup;
700     	hwif->next = hwgroup->hwif->next;
701     	hwgroup->hwif->next = hwif;
702     
703     	for (index = 0; index < MAX_DRIVES; ++index) {
704     		ide_drive_t *drive = &hwif->drives[index];
705     		if (!drive->present)
706     			continue;
707     		if (!hwgroup->drive)
708     			hwgroup->drive = drive;
709     		drive->next = hwgroup->drive->next;
710     		hwgroup->drive->next = drive;
711     		ide_init_queue(drive);
712     	}
713     	if (!hwgroup->hwif) {
714     		hwgroup->hwif = HWIF(hwgroup->drive);
715     #ifdef DEBUG
716     		printk("%s : Adding missed hwif to hwgroup!!\n", hwif->name);
717     #endif
718     	}
719     	restore_flags(flags);	/* all CPUs; safe now that hwif->hwgroup is set up */
720     
721     #if !defined(__mc68000__) && !defined(CONFIG_APUS) && !defined(__sparc__)
722     	printk("%s at 0x%03x-0x%03x,0x%03x on irq %d", hwif->name,
723     		hwif->io_ports[IDE_DATA_OFFSET],
724     		hwif->io_ports[IDE_DATA_OFFSET]+7,
725     		hwif->io_ports[IDE_CONTROL_OFFSET], hwif->irq);
726     #elif defined(__sparc__)
727     	printk("%s at 0x%03lx-0x%03lx,0x%03lx on irq %s", hwif->name,
728     		hwif->io_ports[IDE_DATA_OFFSET],
729     		hwif->io_ports[IDE_DATA_OFFSET]+7,
730     		hwif->io_ports[IDE_CONTROL_OFFSET], __irq_itoa(hwif->irq));
731     #else
732     	printk("%s at %p on irq 0x%08x", hwif->name,
733     		hwif->io_ports[IDE_DATA_OFFSET], hwif->irq);
734     #endif /* __mc68000__ && CONFIG_APUS */
735     	if (match)
736     		printk(" (%sed with %s)",
737     			hwif->sharing_irq ? "shar" : "serializ", match->name);
738     	printk("\n");
739     	return 0;
740     }
741     
742     /*
743      * init_gendisk() (as opposed to ide_geninit) is called for each major device,
744      * after probing for drives, to allocate partition tables and other data
745      * structures needed for the routines in genhd.c.  ide_geninit() gets called
746      * somewhat later, during the partition check.
747      */
748     static void init_gendisk (ide_hwif_t *hwif)
749     {
750     	struct gendisk *gd;
751     	unsigned int unit, units, minors;
752     	int *bs, *max_sect, *max_ra;
753     	extern devfs_handle_t ide_devfs_handle;
754     
755     	/* figure out maximum drive number on the interface */
756     	for (units = MAX_DRIVES; units > 0; --units) {
757     		if (hwif->drives[units-1].present)
758     			break;
759     	}
760     	minors    = units * (1<<PARTN_BITS);
761     	gd        = kmalloc (sizeof(struct gendisk), GFP_KERNEL);
762     	gd->sizes = kmalloc (minors * sizeof(int), GFP_KERNEL);
763     	gd->part  = kmalloc (minors * sizeof(struct hd_struct), GFP_KERNEL);
764     	bs        = kmalloc (minors*sizeof(int), GFP_KERNEL);
765     	max_sect  = kmalloc (minors*sizeof(int), GFP_KERNEL);
766     	max_ra    = kmalloc (minors*sizeof(int), GFP_KERNEL);
767     
768     	memset(gd->part, 0, minors * sizeof(struct hd_struct));
769     
770     	/* cdroms and msdos f/s are examples of non-1024 blocksizes */
771     	blksize_size[hwif->major] = bs;
772     	max_sectors[hwif->major] = max_sect;
773     	max_readahead[hwif->major] = max_ra;
774     	for (unit = 0; unit < minors; ++unit) {
775     		*bs++ = BLOCK_SIZE;
776     #ifdef CONFIG_BLK_DEV_PDC4030
777     		*max_sect++ = ((hwif->chipset == ide_pdc4030) ? 127 : 255);
778     #else
779     		/* IDE can do up to 128K per request. */
780     		*max_sect++ = 255;
781     #endif
782     		*max_ra++ = MAX_READAHEAD;
783     	}
784     
785     	for (unit = 0; unit < units; ++unit)
786     		hwif->drives[unit].part = &gd->part[unit << PARTN_BITS];
787     
788     	gd->major	= hwif->major;		/* our major device number */
789     	gd->major_name	= IDE_MAJOR_NAME;	/* treated special in genhd.c */
790     	gd->minor_shift	= PARTN_BITS;		/* num bits for partitions */
791     	gd->max_p	= 1<<PARTN_BITS;	/* 1 + max partitions / drive */
792     	gd->nr_real	= units;		/* current num real drives */
793     	gd->real_devices= hwif;			/* ptr to internal data */
794     	gd->next	= NULL;			/* linked list of major devs */
795     	gd->fops        = ide_fops;             /* file operations */
796     	gd->de_arr	= kmalloc (sizeof *gd->de_arr * units, GFP_KERNEL);
797     	gd->flags	= kmalloc (sizeof *gd->flags * units, GFP_KERNEL);
798     	if (gd->de_arr)
799     		memset (gd->de_arr, 0, sizeof *gd->de_arr * units);
800     	if (gd->flags)
801     		memset (gd->flags, 0, sizeof *gd->flags * units);
802     
803     	hwif->gd = gd;
804     	add_gendisk(gd);
805     
806     	for (unit = 0; unit < units; ++unit) {
807     		if (hwif->drives[unit].present) {
808     			char name[64];
809     
810     			ide_add_generic_settings(hwif->drives + unit);
811     			hwif->drives[unit].dn = ((hwif->channel ? 2 : 0) + unit);
812     			sprintf (name, "host%d/bus%d/target%d/lun%d",
813     				 (hwif->channel && hwif->mate) ? hwif->mate->index : hwif->index,
814     				 hwif->channel, unit, hwif->drives[unit].lun);
815     			hwif->drives[unit].de =
816     				devfs_mk_dir (ide_devfs_handle, name, NULL);
817     		}
818     	}
819     }
820     
821     static int hwif_init (ide_hwif_t *hwif)
822     {
823     	if (!hwif->present)
824     		return 0;
825     	if (!hwif->irq) {
826     		if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET])))
827     		{
828     			printk("%s: DISABLED, NO IRQ\n", hwif->name);
829     			return (hwif->present = 0);
830     		}
831     	}
832     #ifdef CONFIG_BLK_DEV_HD
833     	if (hwif->irq == HD_IRQ && hwif->io_ports[IDE_DATA_OFFSET] != HD_DATA) {
834     		printk("%s: CANNOT SHARE IRQ WITH OLD HARDDISK DRIVER (hd.c)\n", hwif->name);
835     		return (hwif->present = 0);
836     	}
837     #endif /* CONFIG_BLK_DEV_HD */
838     	
839     	hwif->present = 0; /* we set it back to 1 if all is ok below */
840     
841     	if (devfs_register_blkdev (hwif->major, hwif->name, ide_fops)) {
842     		printk("%s: UNABLE TO GET MAJOR NUMBER %d\n", hwif->name, hwif->major);
843     		return (hwif->present = 0);
844     	}
845     	
846     	if (init_irq(hwif)) {
847     		int i = hwif->irq;
848     		/*
849     		 *	It failed to initialise. Find the default IRQ for 
850     		 *	this port and try that.
851     		 */
852     		if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET]))) {
853     			printk("%s: Disabled unable to get IRQ %d.\n", hwif->name, i);
854     			(void) unregister_blkdev (hwif->major, hwif->name);
855     			return (hwif->present = 0);
856     		}
857     		if (init_irq(hwif)) {
858     			printk("%s: probed IRQ %d and default IRQ %d failed.\n",
859     				hwif->name, i, hwif->irq);
860     			(void) unregister_blkdev (hwif->major, hwif->name);
861     			return (hwif->present = 0);
862     		}
863     		printk("%s: probed IRQ %d failed, using default.\n",
864     			hwif->name, hwif->irq);
865     	}
866     	
867     	init_gendisk(hwif);
868     	blk_dev[hwif->major].data = hwif;
869     	blk_dev[hwif->major].queue = ide_get_queue;
870     	read_ahead[hwif->major] = 8;	/* (4kB) */
871     	hwif->present = 1;	/* success */
872     
873     #if (DEBUG_SPINLOCK > 0)
874     {
875     	static int done = 0;
876     	if (!done++)
877     		printk("io_request_lock is %p\n", &io_request_lock);    /* FIXME */
878     }
879     #endif
880     	return hwif->present;
881     }
882     
883     int ideprobe_init (void);
884     static ide_module_t ideprobe_module = {
885     	IDE_PROBE_MODULE,
886     	ideprobe_init,
887     	NULL
888     };
889     
890     int ideprobe_init (void)
891     {
892     	unsigned int index;
893     	int probe[MAX_HWIFS];
894     	
895     	MOD_INC_USE_COUNT;
896     	memset(probe, 0, MAX_HWIFS * sizeof(int));
897     	for (index = 0; index < MAX_HWIFS; ++index)
898     		probe[index] = !ide_hwifs[index].present;
899     
900     	/*
901     	 * Probe for drives in the usual way.. CMOS/BIOS, then poke at ports
902     	 */
903     	for (index = 0; index < MAX_HWIFS; ++index)
904     		if (probe[index])
905     			probe_hwif(&ide_hwifs[index]);
906     	for (index = 0; index < MAX_HWIFS; ++index)
907     		if (probe[index])
908     			hwif_init(&ide_hwifs[index]);
909     	if (!ide_probe)
910     		ide_probe = &ideprobe_module;
911     	MOD_DEC_USE_COUNT;
912     	return 0;
913     }
914     
915     #ifdef MODULE
916     int init_module (void)
917     {
918     	unsigned int index;
919     	
920     	for (index = 0; index < MAX_HWIFS; ++index)
921     		ide_unregister(index);
922     	ideprobe_init();
923     	create_proc_ide_interfaces();
924     	return 0;
925     }
926     
927     void cleanup_module (void)
928     {
929     	ide_probe = NULL;
930     }
931     #endif /* MODULE */
932