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 #undef REALLY_SLOW_IO		/* most systems can safely undef this */
35 
36 #include <linux/config.h>
37 #include <linux/module.h>
38 #include <linux/types.h>
39 #include <linux/string.h>
40 #include <linux/kernel.h>
41 #include <linux/timer.h>
42 #include <linux/mm.h>
43 #include <linux/interrupt.h>
44 #include <linux/major.h>
45 #include <linux/errno.h>
46 #include <linux/genhd.h>
47 #include <linux/slab.h>
48 #include <linux/delay.h>
49 #include <linux/ide.h>
50 #include <linux/spinlock.h>
51 #include <linux/pci.h>
52 #include <linux/kmod.h>
53 
54 #include <asm/byteorder.h>
55 #include <asm/irq.h>
56 #include <asm/uaccess.h>
57 #include <asm/io.h>
58 
59 /**
60  *	generic_id		-	add a generic drive id
61  *	@drive:	drive to make an ID block for
62  *
63  *	Add a fake id field to the drive we are passed. This allows
64  *	use to skip a ton of NULL checks (which people always miss)
65  *	and make drive properties unconditional outside of this file
66  */
67 
generic_id(ide_drive_t * drive)68 static void generic_id(ide_drive_t *drive)
69 {
70 	drive->id->cyls = drive->cyl;
71 	drive->id->heads = drive->head;
72 	drive->id->sectors = drive->sect;
73 	drive->id->cur_cyls = drive->cyl;
74 	drive->id->cur_heads = drive->head;
75 	drive->id->cur_sectors = drive->sect;
76 }
77 
78 /**
79  *	drive_is_flashcard	-	check for compact flash
80  *	@drive: drive to check
81  *
82  *	CompactFlash cards and their brethern pretend to be removable
83  *	hard disks, except:
84  * 		(1) they never have a slave unit, and
85  *		(2) they don't have doorlock mechanisms.
86  *	This test catches them, and is invoked elsewhere when setting
87  *	appropriate config bits.
88  *
89  *	FIXME: This treatment is probably applicable for *all* PCMCIA (PC CARD)
90  *	devices, so in linux 2.3.x we should change this to just treat all
91  *	PCMCIA  drives this way, and get rid of the model-name tests below
92  *	(too big of an interface change for 2.4.x).
93  *	At that time, we might also consider parameterizing the timeouts and
94  *	retries, since these are MUCH faster than mechanical drives. -M.Lord
95  */
96 
drive_is_flashcard(ide_drive_t * drive)97 static inline int drive_is_flashcard (ide_drive_t *drive)
98 {
99 	struct hd_driveid *id = drive->id;
100 
101 	if (drive->removable && id != NULL) {
102 		if (id->config == 0x848a) return 1;	/* CompactFlash */
103 		if (!strncmp(id->model, "KODAK ATA_FLASH", 15)	/* Kodak */
104 		 || !strncmp(id->model, "Hitachi CV", 10)	/* Hitachi */
105 		 || !strncmp(id->model, "SunDisk SDCFB", 13)	/* old SanDisk */
106 		 || !strncmp(id->model, "SanDisk SDCFB", 13)	/* SanDisk */
107 		 || !strncmp(id->model, "HAGIWARA HPC", 12)	/* Hagiwara */
108 		 || !strncmp(id->model, "LEXAR ATA_FLASH", 15)	/* Lexar */
109 		 || !strncmp(id->model, "ATA_FLASH", 9))	/* Simple Tech */
110 		{
111 			return 1;	/* yes, it is a flash memory card */
112 		}
113 	}
114 	return 0;	/* no, it is not a flash memory card */
115 }
116 
117 /**
118  *	do_identify	-	identify a drive
119  *	@drive: drive to identify
120  *	@cmd: command used
121  *
122  *	Called when we have issued a drive identify command to
123  *	read and parse the results. This function is run with
124  *	interrupts disabled.
125  */
126 
do_identify(ide_drive_t * drive,u8 cmd)127 static inline void do_identify (ide_drive_t *drive, u8 cmd)
128 {
129 	ide_hwif_t *hwif = HWIF(drive);
130 	int bswap = 1;
131 	struct hd_driveid *id;
132 
133 	/* called with interrupts disabled! */
134 	id = drive->id;
135 	/* read 512 bytes of id info */
136 	hwif->ata_input_data(drive, id, SECTOR_WORDS);
137 	drive->id_read = 1;
138 	local_irq_enable();
139 
140 	ide_fix_driveid(id);
141 
142 	if (!drive->forced_lun)
143 		drive->last_lun = id->last_lun & 0x7;
144 
145 #if defined (CONFIG_SCSI_EATA_DMA) || defined (CONFIG_SCSI_EATA_PIO) || defined (CONFIG_SCSI_EATA)
146 	/*
147 	 * EATA SCSI controllers do a hardware ATA emulation:
148 	 * Ignore them if there is a driver for them available.
149 	 */
150 	if ((id->model[0] == 'P' && id->model[1] == 'M') ||
151 	    (id->model[0] == 'S' && id->model[1] == 'K')) {
152 		printk("%s: EATA SCSI HBA %.10s\n", drive->name, id->model);
153 		goto err_misc;
154 	}
155 #endif /* CONFIG_SCSI_EATA_DMA || CONFIG_SCSI_EATA_PIO */
156 
157 	/*
158 	 *  WIN_IDENTIFY returns little-endian info,
159 	 *  WIN_PIDENTIFY *usually* returns little-endian info.
160 	 */
161 	if (cmd == WIN_PIDENTIFY) {
162 		if ((id->model[0] == 'N' && id->model[1] == 'E') /* NEC */
163 		 || (id->model[0] == 'F' && id->model[1] == 'X') /* Mitsumi */
164 		 || (id->model[0] == 'P' && id->model[1] == 'i'))/* Pioneer */
165 			/* Vertos drives may still be weird */
166 			bswap ^= 1;
167 	}
168 	ide_fixstring(id->model,     sizeof(id->model),     bswap);
169 	ide_fixstring(id->fw_rev,    sizeof(id->fw_rev),    bswap);
170 	ide_fixstring(id->serial_no, sizeof(id->serial_no), bswap);
171 
172 	if (strstr(id->model, "E X A B Y T E N E S T"))
173 		goto err_misc;
174 
175 	/* we depend on this a lot! */
176 	id->model[sizeof(id->model)-1] = '\0';
177 	printk("%s: %s, ", drive->name, id->model);
178 	drive->present = 1;
179 	drive->dead = 0;
180 
181 	/*
182 	 * Check for an ATAPI device
183 	 */
184 	if (cmd == WIN_PIDENTIFY) {
185 		u8 type = (id->config >> 8) & 0x1f;
186 		printk("ATAPI ");
187 #ifdef CONFIG_BLK_DEV_PDC4030
188 		if (hwif->channel == 1 && hwif->chipset == ide_pdc4030) {
189 			printk(" -- not supported on 2nd Promise port\n");
190 			goto err_misc;
191 		}
192 #endif /* CONFIG_BLK_DEV_PDC4030 */
193 		switch (type) {
194 			case ide_floppy:
195 				if (!strstr(id->model, "CD-ROM")) {
196 					if (!strstr(id->model, "oppy") &&
197 					    !strstr(id->model, "poyp") &&
198 					    !strstr(id->model, "ZIP"))
199 						printk("cdrom or floppy?, assuming ");
200 					if (drive->media != ide_cdrom) {
201 						printk ("FLOPPY");
202 						drive->removable = 1;
203 						break;
204 					}
205 				}
206 				/* Early cdrom models used zero */
207 				type = ide_cdrom;
208 			case ide_cdrom:
209 				drive->removable = 1;
210 #ifdef CONFIG_PPC
211 				/* kludge for Apple PowerBook internal zip */
212 				if (!strstr(id->model, "CD-ROM") &&
213 				    strstr(id->model, "ZIP")) {
214 					printk ("FLOPPY");
215 					type = ide_floppy;
216 					break;
217 				}
218 #endif
219 				printk ("CD/DVD-ROM");
220 				break;
221 			case ide_tape:
222 				printk ("TAPE");
223 				break;
224 			case ide_optical:
225 				printk ("OPTICAL");
226 				drive->removable = 1;
227 				break;
228 			default:
229 				printk("UNKNOWN (type %d)", type);
230 				break;
231 		}
232 		printk (" drive\n");
233 		drive->media = type;
234 		return;
235 	}
236 
237 	/*
238 	 * Not an ATAPI device: looks like a "regular" hard disk
239 	 */
240 	if (id->config & (1<<7))
241 		drive->removable = 1;
242 
243 	/*
244 	 * Prevent long system lockup probing later for non-existant
245 	 * slave drive if the hwif is actually a flash memory card of
246 	 * some variety:
247 	 */
248 	drive->is_flash = 0;
249 	if (drive_is_flashcard(drive)) {
250 #if 0
251 		/* The new IDE adapter widgets don't follow this heuristic
252 		   so we must nowdays just bite the bullet and take the
253 		   probe hit */
254 		ide_drive_t *mate = &hwif->drives[1^drive->select.b.unit];
255 		if (!mate->ata_flash) {
256 			mate->present = 0;
257 			mate->noprobe = 1;
258 		}
259 #endif
260 		drive->is_flash = 1;
261 	}
262 	drive->media = ide_disk;
263 	printk("%s DISK drive\n", (drive->is_flash) ? "CFA" : "ATA" );
264 	QUIRK_LIST(drive);
265 	return;
266 
267 err_misc:
268 	kfree(id);
269 	drive->present = 0;
270 	return;
271 }
272 
273 /**
274  *	actual_try_to_identify	-	send ata/atapi identify
275  *	@drive: drive to identify
276  *	@cmd: comamnd to use
277  *
278  *	try_to_identify() sends an ATA(PI) IDENTIFY request to a drive
279  *	and waits for a response.  It also monitors irqs while this is
280  *	happening, in hope of automatically determining which one is
281  *	being used by the interface.
282  *
283  *	Returns:	0  device was identified
284  *			1  device timed-out (no response to identify request)
285  *			2  device aborted the command (refused to identify itself)
286  */
287 
actual_try_to_identify(ide_drive_t * drive,u8 cmd)288 static int actual_try_to_identify (ide_drive_t *drive, u8 cmd)
289 {
290 	ide_hwif_t *hwif = HWIF(drive);
291 	int rc;
292 	ide_ioreg_t hd_status;
293 	unsigned long timeout;
294 	u8 s = 0, a = 0;
295 
296 	if (IDE_CONTROL_REG) {
297 		/* take a deep breath */
298 		ide_delay_50ms();
299 		a = hwif->INB(IDE_ALTSTATUS_REG);
300 		s = hwif->INB(IDE_STATUS_REG);
301 		if ((a ^ s) & ~INDEX_STAT) {
302 			printk(KERN_INFO "%s: probing with STATUS(0x%02x) instead of "
303 				"ALTSTATUS(0x%02x)\n", drive->name, s, a);
304 			/* ancient Seagate drives, broken interfaces */
305 			hd_status = IDE_STATUS_REG;
306 		} else {
307 			/* use non-intrusive polling */
308 			hd_status = IDE_ALTSTATUS_REG;
309 		}
310 	} else {
311 		ide_delay_50ms();
312 		hd_status = IDE_STATUS_REG;
313 	}
314 
315 	/* set features register for atapi
316 	 * identify command to be sure of reply
317 	 */
318 	if ((cmd == WIN_PIDENTIFY))
319 		/* disable dma & overlap */
320 		hwif->OUTB(0, IDE_FEATURE_REG);
321 
322 	if (hwif->identify != NULL) {
323 		if (hwif->identify(drive))
324 			return 1;
325 	} else {
326 		/* ask drive for ID */
327 		hwif->OUTB(cmd, IDE_COMMAND_REG);
328 	}
329 	timeout = ((cmd == WIN_IDENTIFY) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
330 	timeout += jiffies;
331 	do {
332 		if (time_after(jiffies, timeout)) {
333 			/* drive timed-out */
334 			return 1;
335 		}
336 		/* give drive a breather */
337 		ide_delay_50ms();
338 	} while ((hwif->INB(hd_status)) & BUSY_STAT);
339 
340 	/* wait for IRQ and DRQ_STAT */
341 	ide_delay_50ms();
342 	if (OK_STAT((hwif->INB(IDE_STATUS_REG)), DRQ_STAT, BAD_R_STAT)) {
343 		unsigned long flags;
344 
345 		/* local CPU only; some systems need this */
346 		local_irq_save(flags);
347 		/* drive returned ID */
348 		do_identify(drive, cmd);
349 		/* drive responded with ID */
350 		rc = 0;
351 		/* clear drive IRQ */
352 		(void) hwif->INB(IDE_STATUS_REG);
353 		local_irq_restore(flags);
354 	} else {
355 		/* drive refused ID */
356 		rc = 2;
357 	}
358 	return rc;
359 }
360 
361 /**
362  *	try_to_identify	-	try to identify a drive
363  *	@drive: drive to probe
364  *	@cmd: comamnd to use
365  *
366  *	Issue the identify command and then do IRQ probing to
367  *	complete the identification when needed by finding the
368  *	IRQ the drive is attached to
369  */
370 
try_to_identify(ide_drive_t * drive,u8 cmd)371 static int try_to_identify (ide_drive_t *drive, u8 cmd)
372 {
373 	ide_hwif_t *hwif = HWIF(drive);
374 	int retval;
375 	int autoprobe = 0;
376 	unsigned long cookie = 0;
377 
378 	if (IDE_CONTROL_REG && !hwif->irq) {
379 		autoprobe = 1;
380 		cookie = probe_irq_on();
381 		/* enable device irq */
382 		hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
383 	}
384 
385 	retval = actual_try_to_identify(drive, cmd);
386 
387 	if (autoprobe) {
388 		int irq;
389 		/* mask device irq */
390 		hwif->OUTB(drive->ctl|2, IDE_CONTROL_REG);
391 		/* clear drive IRQ */
392 		(void) hwif->INB(IDE_STATUS_REG);
393 		udelay(5);
394 		irq = probe_irq_off(cookie);
395 		if (!hwif->irq) {
396 			if (irq > 0) {
397 				hwif->irq = irq;
398 			} else {
399 				/* Mmmm.. multiple IRQs..
400 				 * don't know which was ours
401 				 */
402 				printk("%s: IRQ probe failed (0x%lx)\n",
403 					drive->name, cookie);
404 #ifdef CONFIG_BLK_DEV_CMD640
405 #ifdef CMD640_DUMP_REGS
406 				if (hwif->chipset == ide_cmd640) {
407 					printk("%s: Hmmm.. probably a driver "
408 						"problem.\n", drive->name);
409 					CMD640_DUMP_REGS;
410 				}
411 #endif /* CMD640_DUMP_REGS */
412 #endif /* CONFIG_BLK_DEV_CMD640 */
413 			}
414 		}
415 	}
416 	return retval;
417 }
418 
419 
420 /**
421  *	do_probe		-	probe an IDE device
422  *	@drive: drive to probe
423  *	@cmd: command to use
424  *
425  *	do_probe() has the difficult job of finding a drive if it exists,
426  *	without getting hung up if it doesn't exist, without trampling on
427  *	ethernet cards, and without leaving any IRQs dangling to haunt us later.
428  *
429  *	If a drive is "known" to exist (from CMOS or kernel parameters),
430  *	but does not respond right away, the probe will "hang in there"
431  *	for the maximum wait time (about 30 seconds), otherwise it will
432  *	exit much more quickly.
433  *
434  * Returns:	0  device was identified
435  *		1  device timed-out (no response to identify request)
436  *		2  device aborted the command (refused to identify itself)
437  *		3  bad status from device (possible for ATAPI drives)
438  *		4  probe was not attempted because failure was obvious
439  */
440 
do_probe(ide_drive_t * drive,u8 cmd)441 static int do_probe (ide_drive_t *drive, u8 cmd)
442 {
443 	int rc;
444 	ide_hwif_t *hwif = HWIF(drive);
445 
446 	if (drive->present) {
447 		/* avoid waiting for inappropriate probes */
448 		if ((drive->media != ide_disk) && (cmd == WIN_IDENTIFY))
449 			return 4;
450 	}
451 #ifdef DEBUG
452 	printk("probing for %s: present=%d, media=%d, probetype=%s\n",
453 		drive->name, drive->present, drive->media,
454 		(cmd == WIN_IDENTIFY) ? "ATA" : "ATAPI");
455 #endif
456 
457 	/* needed for some systems
458 	 * (e.g. crw9624 as drive0 with disk as slave)
459 	 */
460 	ide_delay_50ms();
461 	SELECT_DRIVE(drive);
462 	ide_delay_50ms();
463 	if (hwif->INB(IDE_SELECT_REG) != drive->select.all && !drive->present) {
464 		if (drive->select.b.unit != 0) {
465 			/* exit with drive0 selected */
466 			SELECT_DRIVE(&hwif->drives[0]);
467 			/* allow BUSY_STAT to assert & clear */
468 			ide_delay_50ms();
469 		}
470 		/* no i/f present: mmm.. this should be a 4 -ml */
471 		return 3;
472 	}
473 
474 	if (OK_STAT((hwif->INB(IDE_STATUS_REG)), READY_STAT, BUSY_STAT) ||
475 	    drive->present || cmd == WIN_PIDENTIFY) {
476 		/* send cmd and wait */
477 		if ((rc = try_to_identify(drive, cmd))) {
478 			/* failed: try again */
479 			rc = try_to_identify(drive,cmd);
480 		}
481 		if (hwif->INB(IDE_STATUS_REG) == (BUSY_STAT|READY_STAT))
482 			return 4;
483 
484 		if ((rc == 1 && cmd == WIN_PIDENTIFY) &&
485 			((drive->autotune == IDE_TUNE_DEFAULT) ||
486 			(drive->autotune == IDE_TUNE_AUTO))) {
487 			unsigned long timeout;
488 			printk("%s: no response (status = 0x%02x), "
489 				"resetting drive\n", drive->name,
490 				hwif->INB(IDE_STATUS_REG));
491 			ide_delay_50ms();
492 			hwif->OUTB(drive->select.all, IDE_SELECT_REG);
493 			ide_delay_50ms();
494 			hwif->OUTB(WIN_SRST, IDE_COMMAND_REG);
495 			timeout = jiffies;
496 			while (((hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) &&
497 			       time_before(jiffies, timeout + WAIT_WORSTCASE))
498 				ide_delay_50ms();
499 			rc = try_to_identify(drive, cmd);
500 		}
501 		if (rc == 1)
502 			printk("%s: no response (status = 0x%02x)\n",
503 				drive->name, hwif->INB(IDE_STATUS_REG));
504 		/* ensure drive irq is clear */
505 		(void) hwif->INB(IDE_STATUS_REG);
506 	} else {
507 		/* not present or maybe ATAPI */
508 		rc = 3;
509 	}
510 	if (drive->select.b.unit != 0) {
511 		/* exit with drive0 selected */
512 		SELECT_DRIVE(&hwif->drives[0]);
513 		ide_delay_50ms();
514 		/* ensure drive irq is clear */
515 		(void) hwif->INB(IDE_STATUS_REG);
516 	}
517 	return rc;
518 }
519 
520 /*
521  *
522  */
enable_nest(ide_drive_t * drive)523 static void enable_nest (ide_drive_t *drive)
524 {
525 	ide_hwif_t *hwif = HWIF(drive);
526 	unsigned long timeout;
527 
528 	printk("%s: enabling %s -- ", hwif->name, drive->id->model);
529 	SELECT_DRIVE(drive);
530 	ide_delay_50ms();
531 	hwif->OUTB(EXABYTE_ENABLE_NEST, IDE_COMMAND_REG);
532 	timeout = jiffies + WAIT_WORSTCASE;
533 	do {
534 		if (time_after(jiffies, timeout)) {
535 			printk("failed (timeout)\n");
536 			return;
537 		}
538 		ide_delay_50ms();
539 	} while ((hwif->INB(IDE_STATUS_REG)) & BUSY_STAT);
540 
541 	ide_delay_50ms();
542 
543 	if (!OK_STAT((hwif->INB(IDE_STATUS_REG)), 0, BAD_STAT)) {
544 		printk("failed (status = 0x%02x)\n", hwif->INB(IDE_STATUS_REG));
545 	} else {
546 		printk("success\n");
547 	}
548 
549 	/* if !(success||timed-out) */
550 	if (do_probe(drive, WIN_IDENTIFY) >= 2) {
551 		/* look for ATAPI device */
552 		(void) do_probe(drive, WIN_PIDENTIFY);
553 	}
554 }
555 
556 /**
557  *	ide_probe_for_drives	-	upper level drive probe
558  *	@drive: drive to probe for
559  *
560  *	probe_for_drive() tests for existence of a given drive using do_probe()
561  *	and presents things to the user as needed.
562  *
563  *	Returns:	0  no device was found
564  *			1  device was found (note: drive->present might
565  *			   still be 0)
566  */
567 
ide_probe_for_drive(ide_drive_t * drive)568 u8 ide_probe_for_drive (ide_drive_t *drive)
569 {
570 	/*
571 	 *	In order to keep things simple we have an id
572 	 *	block for all drives at all times. If the device
573 	 *	is pre ATA or refuses ATA/ATAPI identify we
574 	 *	will add faked data to this.
575 	 *
576 	 *	Also note that 0 everywhere means "can't do X"
577 	 */
578 
579 	drive->id = kmalloc(SECTOR_WORDS *4, GFP_KERNEL);
580 	drive->id_read = 0;
581 	if(drive->id == NULL)
582 	{
583 		printk(KERN_ERR "ide: out of memory for id data.\n");
584 		return 0;
585 	}
586 	memset(drive->id, 0, SECTOR_WORDS * 4);
587 	strcpy(drive->id->model, "UNKNOWN");
588 
589 	/* skip probing? */
590 	if (!drive->noprobe)
591 	{
592 		/* if !(success||timed-out) */
593 		if (do_probe(drive, WIN_IDENTIFY) >= 2) {
594 			/* look for ATAPI device */
595 			(void) do_probe(drive, WIN_PIDENTIFY);
596 		}
597 		if (strstr(drive->id->model, "E X A B Y T E N E S T"))
598 			enable_nest(drive);
599 		if (!drive->present)
600 			/* drive not found */
601 			return 0;
602 
603 		/* identification failed? */
604 		if (!drive->id_read) {
605 			if (drive->media == ide_disk) {
606 				printk(KERN_INFO "%s: non-IDE drive, CHS=%d/%d/%d\n",
607 					drive->name, drive->cyl,
608 					drive->head, drive->sect);
609 			} else if (drive->media == ide_cdrom) {
610 				printk(KERN_INFO "%s: ATAPI cdrom (?)\n", drive->name);
611 			} else {
612 				/* nuke it */
613 				printk(KERN_WARNING "%s: Unknown device on bus refused identification. Ignoring.\n", drive->name);
614 				drive->present = 0;
615 			}
616 		}
617 		/* drive was found */
618 	}
619 	if(!drive->present)
620 		return 0;
621 	/* The drive wasn't being helpful. Add generic info only */
622 	if(!drive->id_read)
623 		generic_id(drive);
624 	return drive->present;
625 }
626 
627 #define hwif_check_region(addr, num) \
628 	((hwif->mmio) ? check_mem_region((addr),(num)) : check_region((addr),(num)))
629 
630 /**
631  *	hwif_check_regions	-	check resources for IDE
632  *	@hwif: interface to use
633  *
634  *	Checks if all the needed resources for an interface are free
635  *	providing the interface is PIO. Right now core IDE code does
636  *	this work which is deeply wrong. MMIO leaves it to the controller
637  *	driver, PIO will migrate this way over time
638  */
639 
hwif_check_regions(ide_hwif_t * hwif)640 static int hwif_check_regions (ide_hwif_t *hwif)
641 {
642 	u32 i		= 0;
643 	int addr_errs	= 0;
644 
645 	if (hwif->mmio == 2)
646 		return 0;
647 	addr_errs  = hwif_check_region(hwif->io_ports[IDE_DATA_OFFSET], 1);
648 	for (i = IDE_ERROR_OFFSET; i <= IDE_STATUS_OFFSET; i++)
649 		addr_errs += hwif_check_region(hwif->io_ports[i], 1);
650 	if (hwif->io_ports[IDE_CONTROL_OFFSET])
651 		addr_errs += hwif_check_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1);
652 #if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
653 	if (hwif->io_ports[IDE_IRQ_OFFSET])
654 		addr_errs += hwif_check_region(hwif->io_ports[IDE_IRQ_OFFSET], 1);
655 #endif /* (CONFIG_AMIGA) || (CONFIG_MAC) */
656 	/* If any errors are return, we drop the hwif interface. */
657 	hwif->straight8 = 0;
658 	return(addr_errs);
659 }
660 
661 //EXPORT_SYMBOL(hwif_check_regions);
662 
663 #define hwif_request_region(addr, num, name)	\
664 	((hwif->mmio) ? request_mem_region((addr),(num),(name)) : request_region((addr),(num),(name)))
665 
hwif_register(ide_hwif_t * hwif)666 static void hwif_register (ide_hwif_t *hwif)
667 {
668 	u32 i = 0;
669 
670 	if (hwif->mmio == 2)
671 		return;
672 	if (hwif->io_ports[IDE_CONTROL_OFFSET])
673 		hwif_request_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1, hwif->name);
674 #if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
675 	if (hwif->io_ports[IDE_IRQ_OFFSET])
676 		hwif_request_region(hwif->io_ports[IDE_IRQ_OFFSET], 1, hwif->name);
677 #endif /* (CONFIG_AMIGA) || (CONFIG_MAC) */
678 	if (((unsigned long)hwif->io_ports[IDE_DATA_OFFSET] | 7) ==
679 	    ((unsigned long)hwif->io_ports[IDE_STATUS_OFFSET])) {
680 		hwif_request_region(hwif->io_ports[IDE_DATA_OFFSET], 8, hwif->name);
681 		hwif->straight8 = 1;
682 		return;
683 	}
684 
685 	for (i = IDE_DATA_OFFSET; i <= IDE_STATUS_OFFSET; i++)
686 		hwif_request_region(hwif->io_ports[i], 1, hwif->name);
687 }
688 
689 //EXPORT_SYMBOL(hwif_register);
690 
691 /*
692  * This function waits for the hwif to report a non-busy status
693  * see comments in probe_hwif()
694  */
wait_not_busy(ide_hwif_t * hwif,unsigned long timeout)695 static int wait_not_busy(ide_hwif_t *hwif, unsigned long timeout)
696 {
697 	u8 stat = 0;
698 
699 	while(timeout--) {
700 		/* Turn this into a schedule() sleep once I'm sure
701 		 * about locking issues (2.5 work ?)
702 		 */
703 		mdelay(1);
704 		stat = hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
705 		if ((stat & BUSY_STAT) == 0)
706 			break;
707 		/* Assume a value of 0xff means nothing is connected to
708 		 * the interface and it doesn't implement the pull-down
709 		 * resistor on D7
710 		 */
711 		if (stat == 0xff)
712 			break;
713 	}
714 	return ((stat & BUSY_STAT) == 0) ? 0 : -EBUSY;
715 }
716 
ide_wait_hwif_ready(ide_hwif_t * hwif)717 int ide_wait_hwif_ready(ide_hwif_t *hwif)
718 {
719 	int rc;
720 
721 	printk(KERN_INFO "Probing IDE interface %s...\n", hwif->name);
722 
723 	/* Let HW settle down a bit from whatever init state we
724 	 * come from */
725 	mdelay(2);
726 
727 	/* Wait for BSY bit to go away, spec timeout is 30 seconds,
728 	 * I know of at least one disk who takes 31 seconds, I use 35
729 	 * here to be safe
730 	 */
731 	rc = wait_not_busy(hwif, 35000);
732 	if (rc)
733 		return rc;
734 
735 	/* Now make sure both master & slave are ready */
736 	SELECT_DRIVE(&hwif->drives[0]);
737 	hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
738 	mdelay(2);
739 	rc = wait_not_busy(hwif, 10000);
740 	if (rc)
741 		return rc;
742 	SELECT_DRIVE(&hwif->drives[1]);
743 	hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
744 	mdelay(2);
745 	rc = wait_not_busy(hwif, 10000);
746 
747 	/* Exit function with master reselected (let's be sane) */
748 	SELECT_DRIVE(&hwif->drives[0]);
749 
750 	return rc;
751 }
752 
ide_probe_reset(ide_hwif_t * hwif)753 void ide_probe_reset(ide_hwif_t *hwif)
754 {
755 	if (hwif->io_ports[IDE_CONTROL_OFFSET] && hwif->reset) {
756 		unsigned long timeout = jiffies + WAIT_WORSTCASE;
757 		u8 stat;
758 
759 		printk(KERN_WARNING "%s: reset\n", hwif->name);
760 		hwif->OUTB(12, hwif->io_ports[IDE_CONTROL_OFFSET]);
761 		udelay(10);
762 		hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
763 		do {
764 			ide_delay_50ms();
765 			stat = hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
766 		} while ((stat & BUSY_STAT) && time_after(timeout, jiffies));
767 	}
768 }
769 
ide_tune_drives(ide_hwif_t * hwif)770 void ide_tune_drives(ide_hwif_t *hwif)
771 {
772 	int unit;
773 
774 	for (unit = 0; unit < MAX_DRIVES; ++unit) {
775 		ide_drive_t *drive = &hwif->drives[unit];
776 		int enable_dma = 1;
777 
778 		if (drive->present) {
779 			if (hwif->tuneproc != NULL &&
780 				drive->autotune == IDE_TUNE_AUTO)
781 				/* auto-tune PIO mode */
782 				hwif->tuneproc(drive, 255);
783 
784 #ifdef CONFIG_IDEDMA_ONLYDISK
785 			if (drive->media != ide_disk)
786 				enable_dma = 0;
787 #endif
788 			/*
789 			 * MAJOR HACK BARF :-/
790 			 *
791 			 * FIXME: chipsets own this cruft!
792 			 */
793 			/*
794 			 * Move here to prevent module loading clashing.
795 			 */
796 	//		drive->autodma = hwif->autodma;
797 			if ((hwif->ide_dma_check) &&
798 				((drive->autotune == IDE_TUNE_DEFAULT) ||
799 				(drive->autotune == IDE_TUNE_AUTO))) {
800 				/*
801 				 * Force DMAing for the beginning of the check.
802 				 * Some chipsets appear to do interesting
803 				 * things, if not checked and cleared.
804 				 *   PARANOIA!!!
805 				 */
806 				hwif->ide_dma_off_quietly(drive);
807 				if (enable_dma)
808 					hwif->ide_dma_check(drive);
809 			}
810 		}
811 	}
812 }
813 
814 /*
815  * This routine only knows how to look for drive units 0 and 1
816  * on an interface, so any setting of MAX_DRIVES > 2 won't work here.
817  */
probe_hwif(ide_hwif_t * hwif)818 void probe_hwif (ide_hwif_t *hwif)
819 {
820 	unsigned int unit;
821 	unsigned long flags;
822 	int irqd;
823 
824 	if (hwif->noprobe)
825 		return;
826 #ifdef CONFIG_BLK_DEV_IDE
827 	if (hwif->io_ports[IDE_DATA_OFFSET] == HD_DATA) {
828 		extern void probe_cmos_for_drives(ide_hwif_t *);
829 		probe_cmos_for_drives(hwif);
830 	}
831 #endif
832 	if ((hwif->chipset != ide_4drives || !hwif->mate || !hwif->mate->present) &&
833 #if CONFIG_BLK_DEV_PDC4030
834 	    (hwif->chipset != ide_pdc4030 || hwif->channel == 0) &&
835 #endif /* CONFIG_BLK_DEV_PDC4030 */
836 	    (hwif_check_regions(hwif))) {
837 		u16 msgout = 0;
838 		for (unit = 0; unit < MAX_DRIVES; ++unit) {
839 			ide_drive_t *drive = &hwif->drives[unit];
840 			if (drive->present) {
841 				drive->present = 0;
842 				printk(KERN_ERR "%s: ERROR, PORTS ALREADY IN USE\n",
843 					drive->name);
844 				msgout = 1;
845 			}
846 		}
847 		if (!msgout)
848 			printk(KERN_ERR "%s: ports already in use, skipping probe\n",
849 				hwif->name);
850 		return;
851 	}
852 
853 	/*
854 	 * We must always disable IRQ, as probe_for_drive will assert IRQ, but
855 	 * we'll install our IRQ driver much later...
856 	 */
857 	irqd = hwif->irq;
858 	if (irqd > 0)
859 		disable_irq(irqd);
860 
861 	local_irq_set(flags);
862 
863 #ifdef CONFIG_PPC
864 	/* This is needed on some PPCs and a bunch of BIOS-less embedded
865 	 * platforms. Typical cases are:
866 	 *
867 	 *  - The firmware hard reset the disk before booting the kernel,
868 	 *    the drive is still doing it's poweron-reset sequence, that
869 	 *    can take up to 30 seconds
870 	 *  - The firmware does nothing (or no firmware), the device is
871 	 *    still in POST state (same as above actually).
872 	 *  - Some CD/DVD/Writer combo drives tend to drive the bus during
873 	 *    their reset sequence even when they are non-selected slave
874 	 *    devices, thus preventing discovery of the main HD
875 	 *
876 	 *  Doing this wait-for-busy should not harm any existing configuration
877 	 *  (at least things won't be worse than what current code does, that
878 	 *  is blindly go & talk to the drive) and fix some issues like the
879 	 *  above.
880 	 *
881 	 *  BenH.
882 	 */
883 	if (ide_wait_hwif_ready(hwif))
884 		printk(KERN_WARNING "%s: Wait for ready failed before probe !\n", hwif->name);
885 #endif /* CONFIG_PPC */
886 
887 	/*
888 	 * Second drive should only exist if first drive was found,
889 	 * but a lot of cdrom drives are configured as single slaves.
890 	 */
891 	for (unit = 0; unit < MAX_DRIVES; ++unit) {
892 		ide_drive_t *drive = &hwif->drives[unit];
893 		drive->dn = ((hwif->channel ? 2 : 0) + unit);
894 		hwif->drives[unit].dn = ((hwif->channel ? 2 : 0) + unit);
895 		(void) ide_probe_for_drive(drive);
896 		if (drive->present && !hwif->present) {
897 			hwif->present = 1;
898 			if (hwif->chipset != ide_4drives ||
899 			    !hwif->mate ||
900 			    !hwif->mate->present) {
901 				hwif_register(hwif);
902 			}
903 		}
904 	}
905 
906 	ide_probe_reset(hwif);
907 	local_irq_restore(flags);
908 	/*
909 	 * Use cached IRQ number. It might be (and is...) changed by probe
910 	 * code above
911 	 */
912 	if (irqd > 0)
913 		enable_irq(irqd);
914 
915 	ide_tune_drives(hwif);
916 
917 }
918 
919 EXPORT_SYMBOL(probe_hwif);
920 
921 #if 0
922 int hwif_init (ide_hwif_t *hwif);
923 int probe_hwif_init (ide_hwif_t *hwif)
924 {
925 	hwif->initializing = 1;
926 	probe_hwif(hwif);
927 	hwif_init(hwif);
928 
929 #if 1
930 	if (hwif->present) {
931 		u16 unit = 0;
932 		for (unit = 0; unit < MAX_DRIVES; ++unit) {
933 			ide_drive_t *drive = &hwif->drives[unit];
934 			if (drive->present) {
935 				ata_attach(drive);
936 			}
937 		}
938 	}
939 #endif
940 	hwif->initializing = 0;
941 	return 0;
942 }
943 
944 EXPORT_SYMBOL(probe_hwif_init);
945 #endif
946 
947 #if MAX_HWIFS > 1
948 /*
949  * save_match() is used to simplify logic in init_irq() below.
950  *
951  * A loophole here is that we may not know about a particular
952  * hwif's irq until after that hwif is actually probed/initialized..
953  * This could be a problem for the case where an hwif is on a
954  * dual interface that requires serialization (eg. cmd640) and another
955  * hwif using one of the same irqs is initialized beforehand.
956  *
957  * This routine detects and reports such situations, but does not fix them.
958  */
save_match(ide_hwif_t * hwif,ide_hwif_t * new,ide_hwif_t ** match)959 void save_match (ide_hwif_t *hwif, ide_hwif_t *new, ide_hwif_t **match)
960 {
961 	ide_hwif_t *m = *match;
962 
963 	if (m && m->hwgroup && m->hwgroup != new->hwgroup) {
964 		if (!new->hwgroup)
965 			return;
966 		printk("%s: potential irq problem with %s and %s\n",
967 			hwif->name, new->name, m->name);
968 	}
969 	if (!m || m->irq != hwif->irq) /* don't undo a prior perfect match */
970 		*match = new;
971 }
972 EXPORT_SYMBOL(save_match);
973 #endif /* MAX_HWIFS > 1 */
974 
975 /*
976  * init request queue
977  */
ide_init_queue(ide_drive_t * drive)978 static void ide_init_queue(ide_drive_t *drive)
979 {
980 	request_queue_t *q = &drive->queue;
981 
982 	q->queuedata = HWGROUP(drive);
983 	blk_init_queue(q, do_ide_request);
984 	blk_queue_throttle_sectors(q, 1);
985 }
986 
987 #undef __IRQ_HELL_SPIN
988 /*
989  * This routine sets up the irq for an ide interface, and creates a new
990  * hwgroup for the irq/hwif if none was previously assigned.
991  *
992  * Much of the code is for correctly detecting/handling irq sharing
993  * and irq serialization situations.  This is somewhat complex because
994  * it handles static as well as dynamic (PCMCIA) IDE interfaces.
995  *
996  * The SA_INTERRUPT in sa_flags means ide_intr() is always entered with
997  * interrupts completely disabled.  This can be bad for interrupt latency,
998  * but anything else has led to problems on some machines.  We re-enable
999  * interrupts as much as we can safely do in most places.
1000  */
init_irq(ide_hwif_t * hwif)1001 int init_irq (ide_hwif_t *hwif)
1002 {
1003 	unsigned long flags;
1004 	unsigned int index;
1005 	ide_hwgroup_t *hwgroup, *new_hwgroup;
1006 	ide_hwif_t *match = NULL;
1007 
1008 #if 0
1009 	/* Allocate the buffer and no sleep allowed */
1010 	new_hwgroup = kmalloc(sizeof(ide_hwgroup_t),GFP_ATOMIC);
1011 #else
1012 	/* Allocate the buffer and potentially sleep first */
1013 	new_hwgroup = kmalloc(sizeof(ide_hwgroup_t),GFP_KERNEL);
1014 #endif
1015 
1016 #ifndef __IRQ_HELL_SPIN
1017 	save_and_cli(flags);
1018 #else
1019 	spin_lock_irqsave(&io_request_lock, flags);
1020 #endif
1021 
1022 	hwif->hwgroup = NULL;
1023 #if MAX_HWIFS > 1
1024 	/*
1025 	 * Group up with any other hwifs that share our irq(s).
1026 	 */
1027 	for (index = 0; index < MAX_HWIFS; index++) {
1028 		ide_hwif_t *h = &ide_hwifs[index];
1029 		if (h->hwgroup) {  /* scan only initialized hwif's */
1030 			if (hwif->irq == h->irq) {
1031 				hwif->sharing_irq = h->sharing_irq = 1;
1032 				if (hwif->chipset != ide_pci ||
1033 				    h->chipset != ide_pci) {
1034 					save_match(hwif, h, &match);
1035 				}
1036 			}
1037 			if (hwif->serialized) {
1038 				if (hwif->mate && hwif->mate->irq == h->irq)
1039 					save_match(hwif, h, &match);
1040 			}
1041 			if (h->serialized) {
1042 				if (h->mate && hwif->irq == h->mate->irq)
1043 					save_match(hwif, h, &match);
1044 			}
1045 		}
1046 	}
1047 #endif /* MAX_HWIFS > 1 */
1048 	/*
1049 	 * If we are still without a hwgroup, then form a new one
1050 	 */
1051 	if (match) {
1052 		hwgroup = match->hwgroup;
1053 		if(new_hwgroup)
1054 			kfree(new_hwgroup);
1055 	} else {
1056 		hwgroup = new_hwgroup;
1057 		if (!hwgroup) {
1058 #ifndef __IRQ_HELL_SPIN
1059 			restore_flags(flags);
1060 #else
1061 			spin_unlock_irqrestore(&io_request_lock, flags);
1062 #endif
1063 			return 1;
1064 		}
1065 		memset(hwgroup, 0, sizeof(ide_hwgroup_t));
1066 		hwgroup->hwif     = hwif->next = hwif;
1067 		hwgroup->rq       = NULL;
1068 		hwgroup->handler  = NULL;
1069 		hwgroup->drive    = NULL;
1070 		hwgroup->busy     = 0;
1071 		init_timer(&hwgroup->timer);
1072 		hwgroup->timer.function = &ide_timer_expiry;
1073 		hwgroup->timer.data = (unsigned long) hwgroup;
1074 	}
1075 
1076 	/*
1077 	 * Allocate the irq, if not already obtained for another hwif
1078 	 */
1079 	if (!match || match->irq != hwif->irq) {
1080 		int sa = SA_INTERRUPT;
1081 #if defined(__mc68000__) || defined(CONFIG_APUS)
1082 		sa = SA_SHIRQ;
1083 #endif /* __mc68000__ || CONFIG_APUS */
1084 
1085 		if (IDE_CHIPSET_IS_PCI(hwif->chipset)) {
1086 			sa = SA_SHIRQ;
1087 #ifndef CONFIG_IDEPCI_SHARE_IRQ
1088 			sa |= SA_INTERRUPT;
1089 #endif /* CONFIG_IDEPCI_SHARE_IRQ */
1090 		}
1091 
1092 		if (hwif->io_ports[IDE_CONTROL_OFFSET])
1093 			/* clear nIEN */
1094 			hwif->OUTB(0x08, hwif->io_ports[IDE_CONTROL_OFFSET]);
1095 
1096 		if (request_irq(hwif->irq,&ide_intr,sa,hwif->name,hwgroup)) {
1097 			if (!match)
1098 				kfree(hwgroup);
1099 #ifndef __IRQ_HELL_SPIN
1100 			restore_flags(flags);
1101 #else
1102 			spin_unlock_irqrestore(&io_request_lock, flags);
1103 #endif
1104 			return 1;
1105 		}
1106 	}
1107 
1108 	/*
1109 	 * Everything is okay, so link us into the hwgroup
1110 	 */
1111 	hwif->hwgroup = hwgroup;
1112 	hwif->next = hwgroup->hwif->next;
1113 	hwgroup->hwif->next = hwif;
1114 
1115 	for (index = 0; index < MAX_DRIVES; ++index) {
1116 		ide_drive_t *drive = &hwif->drives[index];
1117 		if (!drive->present)
1118 			continue;
1119 		if (!hwgroup->drive)
1120 			hwgroup->drive = drive;
1121 		drive->next = hwgroup->drive->next;
1122 		hwgroup->drive->next = drive;
1123 		ide_init_queue(drive);
1124 	}
1125 	if (!hwgroup->hwif) {
1126 		hwgroup->hwif = HWIF(hwgroup->drive);
1127 #ifdef DEBUG
1128 		printk("%s : Adding missed hwif to hwgroup!!\n", hwif->name);
1129 #endif
1130 	}
1131 
1132 	/* all CPUs; safe now that hwif->hwgroup is set up */
1133 #ifndef __IRQ_HELL_SPIN
1134 	restore_flags(flags);
1135 #else
1136 	spin_unlock_irqrestore(&io_request_lock, flags);
1137 #endif
1138 
1139 #if !defined(__mc68000__) && !defined(CONFIG_APUS) && !defined(__sparc__)
1140 	printk("%s at 0x%03lx-0x%03lx,0x%03lx on irq %d", hwif->name,
1141 		hwif->io_ports[IDE_DATA_OFFSET],
1142 		hwif->io_ports[IDE_DATA_OFFSET]+7,
1143 		hwif->io_ports[IDE_CONTROL_OFFSET], hwif->irq);
1144 #elif defined(__sparc__)
1145 	printk("%s at 0x%03lx-0x%03lx,0x%03lx on irq %s", hwif->name,
1146 		hwif->io_ports[IDE_DATA_OFFSET],
1147 		hwif->io_ports[IDE_DATA_OFFSET]+7,
1148 		hwif->io_ports[IDE_CONTROL_OFFSET], __irq_itoa(hwif->irq));
1149 #else
1150 	printk("%s at 0x%08lx on irq %d", hwif->name,
1151 		hwif->io_ports[IDE_DATA_OFFSET], hwif->irq);
1152 #endif /* __mc68000__ && CONFIG_APUS */
1153 	if (match)
1154 		printk(" (%sed with %s)",
1155 			hwif->sharing_irq ? "shar" : "serializ", match->name);
1156 	printk("\n");
1157 	return 0;
1158 }
1159 
1160 EXPORT_SYMBOL(init_irq);
1161 
1162 /*
1163  * init_gendisk() (as opposed to ide_geninit) is called for each major device,
1164  * after probing for drives, to allocate partition tables and other data
1165  * structures needed for the routines in genhd.c.  ide_geninit() gets called
1166  * somewhat later, during the partition check.
1167  */
init_gendisk(ide_hwif_t * hwif)1168 static void init_gendisk (ide_hwif_t *hwif)
1169 {
1170 	struct gendisk *gd;
1171 	unsigned int unit, units, minors;
1172 	int *bs, *max_sect, *max_ra;
1173 	extern devfs_handle_t ide_devfs_handle;
1174 
1175 	units = MAX_DRIVES;
1176 
1177 	minors    = units * (1<<PARTN_BITS);
1178 	gd        = kmalloc(sizeof(struct gendisk), GFP_KERNEL);
1179 	if (!gd)
1180 		goto err_kmalloc_gd;
1181 	memset(gd, 0, sizeof(struct gendisk));
1182 
1183 	gd->sizes = kmalloc(minors * sizeof(int), GFP_KERNEL);
1184 	if (!gd->sizes)
1185 		goto err_kmalloc_gd_sizes;
1186 	gd->part  = kmalloc(minors * sizeof(struct hd_struct), GFP_KERNEL);
1187 	if (!gd->part)
1188 		goto err_kmalloc_gd_part;
1189 	bs        = kmalloc(minors*sizeof(int), GFP_KERNEL);
1190 	if (!bs)
1191 		goto err_kmalloc_bs;
1192 	max_sect  = kmalloc(minors*sizeof(int), GFP_KERNEL);
1193 	if (!max_sect)
1194 		goto err_kmalloc_max_sect;
1195 	max_ra    = kmalloc(minors*sizeof(int), GFP_KERNEL);
1196 	if (!max_ra)
1197 		goto err_kmalloc_max_ra;
1198 
1199 	memset(gd->part, 0, minors * sizeof(struct hd_struct));
1200 
1201 	/* cdroms and msdos f/s are examples of non-1024 blocksizes */
1202 	blksize_size[hwif->major] = bs;
1203 	max_sectors[hwif->major] = max_sect;
1204 	max_readahead[hwif->major] = max_ra;
1205 	for (unit = 0; unit < minors; ++unit) {
1206 		*bs++ = BLOCK_SIZE;
1207 		/*
1208 		 * IDE can do up to 128K per request == 256
1209 		 */
1210 		*max_sect++ = ((hwif->rqsize) ? hwif->rqsize : 128);
1211 		*max_ra++ = vm_max_readahead;
1212 	}
1213 
1214 	for (unit = 0; unit < units; ++unit)
1215 		hwif->drives[unit].part = &gd->part[unit << PARTN_BITS];
1216 
1217 	/* our major device number */
1218 	gd->major	= hwif->major;
1219 	/* treated special in genhd.c */
1220 	gd->major_name	= IDE_MAJOR_NAME;
1221 	/* num bits for partitions */
1222 	gd->minor_shift	= PARTN_BITS;
1223 	/* 1 + max partitions / drive */
1224 	gd->max_p	= 1<<PARTN_BITS;
1225 	/* current num real drives */
1226 	gd->nr_real	= units;
1227 	/* ptr to internal data */
1228 	gd->real_devices= hwif;
1229 	/* linked list of major devs */
1230 	gd->next	= NULL;
1231 	/* file operations */
1232 	gd->fops        = ide_fops;
1233 	gd->de_arr	= kmalloc(sizeof *gd->de_arr * units, GFP_KERNEL);
1234 	gd->flags	= kmalloc(sizeof *gd->flags * units, GFP_KERNEL);
1235 	if (gd->de_arr)
1236 		memset(gd->de_arr, 0, sizeof *gd->de_arr * units);
1237 	if (gd->flags)
1238 		memset(gd->flags, 0, sizeof *gd->flags * units);
1239 
1240 	hwif->gd = gd;
1241 	add_gendisk(gd);
1242 
1243 	for (unit = 0; unit < units; ++unit) {
1244 		char name[64];
1245 		ide_add_generic_settings(hwif->drives + unit);
1246 //		hwif->drives[unit].dn = ((hwif->channel ? 2 : 0) + unit);
1247 		sprintf (name, "host%d/bus%d/target%d/lun%d",
1248 			(hwif->channel && hwif->mate) ?
1249 			hwif->mate->index : hwif->index,
1250 			hwif->channel, unit, hwif->drives[unit].lun);
1251 		if (hwif->drives[unit].present)
1252 			hwif->drives[unit].de = devfs_mk_dir(ide_devfs_handle, name, NULL);
1253 	}
1254 	return;
1255 
1256 err_kmalloc_max_ra:
1257 	kfree(max_sect);
1258 err_kmalloc_max_sect:
1259 	kfree(bs);
1260 err_kmalloc_bs:
1261 	kfree(gd->part);
1262 err_kmalloc_gd_part:
1263 	kfree(gd->sizes);
1264 err_kmalloc_gd_sizes:
1265 	kfree(gd);
1266 err_kmalloc_gd:
1267 	printk(KERN_WARNING "(ide::init_gendisk) Out of memory\n");
1268 	return;
1269 }
1270 
1271 EXPORT_SYMBOL(init_gendisk);
1272 
hwif_init(ide_hwif_t * hwif)1273 int hwif_init (ide_hwif_t *hwif)
1274 {
1275 	if (!hwif->present)
1276 		return 0;
1277 	if (!hwif->irq) {
1278 		if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET])))
1279 		{
1280 			printk("%s: DISABLED, NO IRQ\n", hwif->name);
1281 			return (hwif->present = 0);
1282 		}
1283 	}
1284 #ifdef CONFIG_BLK_DEV_HD
1285 	if (hwif->irq == HD_IRQ && hwif->io_ports[IDE_DATA_OFFSET] != HD_DATA) {
1286 		printk("%s: CANNOT SHARE IRQ WITH OLD "
1287 			"HARDDISK DRIVER (hd.c)\n", hwif->name);
1288 		return (hwif->present = 0);
1289 	}
1290 #endif /* CONFIG_BLK_DEV_HD */
1291 
1292 	/* we set it back to 1 if all is ok below */
1293 	hwif->present = 0;
1294 
1295 	if (devfs_register_blkdev (hwif->major, hwif->name, ide_fops)) {
1296 		printk("%s: UNABLE TO GET MAJOR NUMBER %d\n",
1297 			hwif->name, hwif->major);
1298 		return (hwif->present = 0);
1299 	}
1300 
1301 	if (init_irq(hwif)) {
1302 		int i = hwif->irq;
1303 		/*
1304 		 *	It failed to initialise. Find the default IRQ for
1305 		 *	this port and try that.
1306 		 */
1307 		if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET]))) {
1308 			printk("%s: Disabled unable to get IRQ %d.\n",
1309 				hwif->name, i);
1310 			(void) unregister_blkdev(hwif->major, hwif->name);
1311 			return (hwif->present = 0);
1312 		}
1313 		if (init_irq(hwif)) {
1314 			printk("%s: probed IRQ %d and default IRQ %d failed.\n",
1315 				hwif->name, i, hwif->irq);
1316 			(void) unregister_blkdev(hwif->major, hwif->name);
1317 			return (hwif->present = 0);
1318 		}
1319 		printk("%s: probed IRQ %d failed, using default.\n",
1320 			hwif->name, hwif->irq);
1321 	}
1322 
1323 	init_gendisk(hwif);
1324 	blk_dev[hwif->major].data = hwif;
1325 	blk_dev[hwif->major].queue = ide_get_queue;
1326 	read_ahead[hwif->major] = 8;	/* (4kB) */
1327 	hwif->present = 1;	/* success */
1328 
1329 #if (DEBUG_SPINLOCK > 0)
1330 {
1331 	static int done = 0;
1332 	if (!done++)
1333 		printk("io_request_lock is %p\n", &io_request_lock);    /* FIXME */
1334 }
1335 #endif
1336 	return hwif->present;
1337 }
1338 
1339 EXPORT_SYMBOL(hwif_init);
1340 
export_ide_init_queue(ide_drive_t * drive)1341 void export_ide_init_queue (ide_drive_t *drive)
1342 {
1343 	ide_init_queue(drive);
1344 }
1345 
1346 EXPORT_SYMBOL(export_ide_init_queue);
1347 
1348 #ifndef HWIF_PROBE_CLASSIC_METHOD
probe_hwif_init(ide_hwif_t * hwif)1349 int probe_hwif_init (ide_hwif_t *hwif)
1350 {
1351 	hwif->initializing = 1;
1352 	probe_hwif(hwif);
1353 	hwif_init(hwif);
1354 
1355 #ifndef CLASSIC_BUILTINS_METHOD
1356 #  ifndef FAKE_CLASSIC_ATTACH_METHOD
1357 #    ifdef DIRECT_HWIF_PROBE_ATTACH_METHOD
1358 	if (hwif->present) {
1359 		u16 unit = 0;
1360 		for (unit = 0; unit < MAX_DRIVES; ++unit) {
1361 			ide_drive_t *drive = &hwif->drives[unit];
1362 			if (drive->present)
1363 				ide_attach_drive(drive);
1364 		}
1365 	}
1366 #    endif /* DIRECT_HWIF_PROBE_ATTACH_METHOD */
1367 #  endif /* FAKE_CLASSIC_ATTACH_METHOD */
1368 #endif /* CLASSIC_BUILTINS_METHOD */
1369 	hwif->initializing = 0;
1370 	return 0;
1371 }
1372 
1373 EXPORT_SYMBOL(probe_hwif_init);
1374 
1375 #endif /* HWIF_PROBE_CLASSIC_METHOD */
1376 
1377 int ideprobe_init (void);
1378 static ide_module_t ideprobe_module = {
1379 	IDE_PROBE_MODULE,
1380 	ideprobe_init,
1381 	NULL
1382 };
1383 
ideprobe_init(void)1384 int ideprobe_init (void)
1385 {
1386 	unsigned int index;
1387 	int probe[MAX_HWIFS];
1388 
1389 	MOD_INC_USE_COUNT;
1390 	memset(probe, 0, MAX_HWIFS * sizeof(int));
1391 	for (index = 0; index < MAX_HWIFS; ++index)
1392 		probe[index] = !ide_hwifs[index].present;
1393 
1394 	/*
1395 	 * Probe for drives in the usual way.. CMOS/BIOS, then poke at ports
1396 	 */
1397 #ifdef HWIF_PROBE_CLASSIC_METHOD
1398 	for (index = 0; index < MAX_HWIFS; ++index)
1399 		if (probe[index])
1400 			probe_hwif(&ide_hwifs[index]);
1401 
1402 	for (index = 0; index < MAX_HWIFS; ++index)
1403 		if (probe[index])
1404 			hwif_init(&ide_hwifs[index]);
1405 #else /* HWIF_PROBE_CLASSIC_METHOD */
1406 	for (index = 0; index < MAX_HWIFS; ++index)
1407 		if (probe[index])
1408 			probe_hwif_init(&ide_hwifs[index]);
1409 #endif /* HWIF_PROBE_CLASSIC_METHOD */
1410 
1411 	if (!ide_probe)
1412 		ide_probe = &ideprobe_module;
1413 	MOD_DEC_USE_COUNT;
1414 	return 0;
1415 }
1416 
1417 #ifdef MODULE
1418 extern int (*ide_xlate_1024_hook)(kdev_t, int, int, const char *);
1419 
1420 static int ideprobe_done = 0;
1421 
ideprobe_init_module(void)1422 int ideprobe_init_module (void)
1423 {
1424 	unsigned int index;
1425 
1426 	if (ideprobe_done)
1427     		return -EBUSY;
1428 
1429 	for (index = 0; index < MAX_HWIFS; ++index)
1430 		ide_unregister(index);
1431 	ideprobe_init();
1432 	create_proc_ide_interfaces();
1433 	ide_xlate_1024_hook = ide_xlate_1024;
1434 	ideprobe_done++;
1435 	return 0;
1436 }
1437 
ideprobe_cleanup_module(void)1438 void ideprobe_cleanup_module (void)
1439 {
1440 	ide_probe = NULL;
1441 	ide_xlate_1024_hook = 0;
1442 }
1443 EXPORT_SYMBOL(ideprobe_init_module);
1444 EXPORT_SYMBOL(ideprobe_cleanup_module);
1445 MODULE_LICENSE("GPL");
1446 #endif /* MODULE */
1447