ide-probe.c

来自「2410的硬盘块设备源码」· C语言 代码 · 共 1,466 行 · 第 1/3 页

C
1,466
字号
		/* no i/f present: mmm.. this should be a 4 -ml */		return 3;	}	if (OK_STAT((hwif->INB(IDE_STATUS_REG)), READY_STAT, BUSY_STAT) ||	    drive->present || cmd == WIN_PIDENTIFY) {		/* send cmd and wait */		if ((rc = try_to_identify(drive, cmd))) {			/* failed: try again */			rc = try_to_identify(drive,cmd);		}		if (hwif->INB(IDE_STATUS_REG) == (BUSY_STAT|READY_STAT))			return 4;		if ((rc == 1 && cmd == WIN_PIDENTIFY) &&			((drive->autotune == IDE_TUNE_DEFAULT) ||			(drive->autotune == IDE_TUNE_AUTO))) {			unsigned long timeout;			printk("%s: no response (status = 0x%02x), "				"resetting drive\n", drive->name,				hwif->INB(IDE_STATUS_REG));			msleep(50);			hwif->OUTB(drive->select.all, IDE_SELECT_REG);			msleep(50);			hwif->OUTB(WIN_SRST, IDE_COMMAND_REG);			timeout = jiffies;			while (((hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) &&			       time_before(jiffies, timeout + WAIT_WORSTCASE))				msleep(50);			rc = try_to_identify(drive, cmd);		}		if (rc == 1)			printk("%s: no response (status = 0x%02x)\n",				drive->name, hwif->INB(IDE_STATUS_REG));		/* ensure drive irq is clear */		(void) hwif->INB(IDE_STATUS_REG);	} else {		/* not present or maybe ATAPI */		rc = 3;	}	if (drive->select.b.unit != 0) {		/* exit with drive0 selected */		SELECT_DRIVE(&hwif->drives[0]);		msleep(50);		/* ensure drive irq is clear */		(void) hwif->INB(IDE_STATUS_REG);	}	return rc;}/* * */static void enable_nest (ide_drive_t *drive){	ide_hwif_t *hwif = HWIF(drive);	unsigned long timeout;	printk("%s: enabling %s -- ", hwif->name, drive->id->model);	SELECT_DRIVE(drive);	msleep(50);	hwif->OUTB(EXABYTE_ENABLE_NEST, IDE_COMMAND_REG);	timeout = jiffies + WAIT_WORSTCASE;	do {		if (time_after(jiffies, timeout)) {			printk("failed (timeout)\n");			return;		}		msleep(50);	} while ((hwif->INB(IDE_STATUS_REG)) & BUSY_STAT);	msleep(50);	if (!OK_STAT((hwif->INB(IDE_STATUS_REG)), 0, BAD_STAT)) {		printk("failed (status = 0x%02x)\n", hwif->INB(IDE_STATUS_REG));	} else {		printk("success\n");	}	/* if !(success||timed-out) */	if (do_probe(drive, WIN_IDENTIFY) >= 2) {		/* look for ATAPI device */		(void) do_probe(drive, WIN_PIDENTIFY);	}}/** *	probe_for_drives	-	upper level drive probe *	@drive: drive to probe for * *	probe_for_drive() tests for existence of a given drive using do_probe() *	and presents things to the user as needed. * *	Returns:	0  no device was found *			1  device was found (note: drive->present might *			   still be 0) */ static inline u8 probe_for_drive (ide_drive_t *drive){	/*	 *	In order to keep things simple we have an id	 *	block for all drives at all times. If the device	 *	is pre ATA or refuses ATA/ATAPI identify we	 *	will add faked data to this.	 *	 *	Also note that 0 everywhere means "can't do X"	 */ 	drive->id = kmalloc(SECTOR_WORDS *4, GFP_KERNEL);	drive->id_read = 0;	if(drive->id == NULL)	{		printk(KERN_ERR "ide: out of memory for id data.\n");		return 0;	}	memset(drive->id, 0, SECTOR_WORDS * 4);	strcpy(drive->id->model, "UNKNOWN");		/* skip probing? */	if (!drive->noprobe)	{		/* if !(success||timed-out) */		if (do_probe(drive, WIN_IDENTIFY) >= 2) {			/* look for ATAPI device */			(void) do_probe(drive, WIN_PIDENTIFY);		}		if (strstr(drive->id->model, "E X A B Y T E N E S T"))			enable_nest(drive);		if (!drive->present)			/* drive not found */			return 0;			/* identification failed? */		if (!drive->id_read) {			if (drive->media == ide_disk) {				printk(KERN_INFO "%s: non-IDE drive, CHS=%d/%d/%d\n",					drive->name, drive->cyl,					drive->head, drive->sect);			} else if (drive->media == ide_cdrom) {				printk(KERN_INFO "%s: ATAPI cdrom (?)\n", drive->name);			} else {				/* nuke it */				printk(KERN_WARNING "%s: Unknown device on bus refused identification. Ignoring.\n", drive->name);				drive->present = 0;			}		}		/* drive was found */	}	if(!drive->present)		return 0;	/* The drive wasn't being helpful. Add generic info only */	if (drive->id_read == 0) {		generic_id(drive);		return 1;	}	if (drive->media == ide_disk) {		ide_disk_init_chs(drive);		ide_disk_init_mult_count(drive);	}	return drive->present;}static void hwif_release_dev (struct device *dev){	ide_hwif_t *hwif = container_of(dev, ide_hwif_t, gendev);	up(&hwif->gendev_rel_sem);}static void hwif_register (ide_hwif_t *hwif){	/* register with global device tree */	strlcpy(hwif->gendev.bus_id,hwif->name,BUS_ID_SIZE);	hwif->gendev.driver_data = hwif;	if (hwif->gendev.parent == NULL) {		if (hwif->pci_dev)			hwif->gendev.parent = &hwif->pci_dev->dev;		else			/* Would like to do = &device_legacy */			hwif->gendev.parent = NULL;	}	hwif->gendev.release = hwif_release_dev;	device_register(&hwif->gendev);}static int wait_hwif_ready(ide_hwif_t *hwif){	int rc;	printk(KERN_DEBUG "Probing IDE interface %s...\n", hwif->name);	/* Let HW settle down a bit from whatever init state we	 * come from */	mdelay(2);	/* Wait for BSY bit to go away, spec timeout is 30 seconds,	 * I know of at least one disk who takes 31 seconds, I use 35	 * here to be safe	 */	rc = ide_wait_not_busy(hwif, 35000);	if (rc)		return rc;	/* Now make sure both master & slave are ready */	SELECT_DRIVE(&hwif->drives[0]);	hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);	mdelay(2);	rc = ide_wait_not_busy(hwif, 35000);	if (rc)		return rc;	SELECT_DRIVE(&hwif->drives[1]);	hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);	mdelay(2);	rc = ide_wait_not_busy(hwif, 35000);	/* Exit function with master reselected (let's be sane) */	SELECT_DRIVE(&hwif->drives[0]);		return rc;}/** *	ide_undecoded_slave	-	look for bad CF adapters *	@hwif: interface * *	Analyse the drives on the interface and attempt to decide if we *	have the same drive viewed twice. This occurs with crap CF adapters *	and PCMCIA sometimes. */void ide_undecoded_slave(ide_hwif_t *hwif){	ide_drive_t *drive0 = &hwif->drives[0];	ide_drive_t *drive1 = &hwif->drives[1];	if (drive0->present == 0 || drive1->present == 0)		return;	/* If the models don't match they are not the same product */	if (strcmp(drive0->id->model, drive1->id->model))		return;	/* Serial numbers do not match */	if (strncmp(drive0->id->serial_no, drive1->id->serial_no, 20))		return;	/* No serial number, thankfully very rare for CF */	if (drive0->id->serial_no[0] == 0)		return;	/* Appears to be an IDE flash adapter with decode bugs */	printk(KERN_WARNING "ide-probe: ignoring undecoded slave\n");	drive1->present = 0;}EXPORT_SYMBOL_GPL(ide_undecoded_slave);/* * This routine only knows how to look for drive units 0 and 1 * on an interface, so any setting of MAX_DRIVES > 2 won't work here. */static void probe_hwif(ide_hwif_t *hwif){	unsigned int unit;	unsigned long flags;	unsigned int irqd;	if (hwif->noprobe)		return;	if ((hwif->chipset != ide_4drives || !hwif->mate || !hwif->mate->present) &&	    (ide_hwif_request_regions(hwif))) {		u16 msgout = 0;		for (unit = 0; unit < MAX_DRIVES; ++unit) {			ide_drive_t *drive = &hwif->drives[unit];			if (drive->present) {				drive->present = 0;				printk(KERN_ERR "%s: ERROR, PORTS ALREADY IN USE\n",					drive->name);				msgout = 1;			}		}		if (!msgout)			printk(KERN_ERR "%s: ports already in use, skipping probe\n",				hwif->name);		return;		}	/*	 * We must always disable IRQ, as probe_for_drive will assert IRQ, but	 * we'll install our IRQ driver much later...	 */	irqd = hwif->irq;	if (irqd)		disable_irq(hwif->irq);	local_irq_set(flags);	/* This is needed on some PPCs and a bunch of BIOS-less embedded	 * platforms. Typical cases are:	 * 	 *  - The firmware hard reset the disk before booting the kernel,	 *    the drive is still doing it's poweron-reset sequence, that	 *    can take up to 30 seconds	 *  - The firmware does nothing (or no firmware), the device is	 *    still in POST state (same as above actually).	 *  - Some CD/DVD/Writer combo drives tend to drive the bus during	 *    their reset sequence even when they are non-selected slave	 *    devices, thus preventing discovery of the main HD	 *    	 *  Doing this wait-for-busy should not harm any existing configuration	 *  (at least things won't be worse than what current code does, that	 *  is blindly go & talk to the drive) and fix some issues like the	 *  above.	 *  	 *  BenH.	 */	if (wait_hwif_ready(hwif) == -EBUSY)		printk(KERN_DEBUG "%s: Wait for ready failed before probe !\n", hwif->name);	/*	 * Second drive should only exist if first drive was found,	 * but a lot of cdrom drives are configured as single slaves.	 */	for (unit = 0; unit < MAX_DRIVES; ++unit) {		ide_drive_t *drive = &hwif->drives[unit];		drive->dn = (hwif->channel ? 2 : 0) + unit;		(void) probe_for_drive(drive);		if (drive->present && !hwif->present) {			hwif->present = 1;			if (hwif->chipset != ide_4drives ||			    !hwif->mate || 			    !hwif->mate->present) {				hwif_register(hwif);			}		}	}	if (hwif->io_ports[IDE_CONTROL_OFFSET] && hwif->reset) {		unsigned long timeout = jiffies + WAIT_WORSTCASE;		u8 stat;		printk(KERN_WARNING "%s: reset\n", hwif->name);		hwif->OUTB(12, hwif->io_ports[IDE_CONTROL_OFFSET]);		udelay(10);		hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);		do {			msleep(50);			stat = hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);		} while ((stat & BUSY_STAT) && time_after(timeout, jiffies));	}	local_irq_restore(flags);	/*	 * Use cached IRQ number. It might be (and is...) changed by probe	 * code above	 */	if (irqd)		enable_irq(irqd);	if (!hwif->present) {		ide_hwif_release_regions(hwif);		return;	}	for (unit = 0; unit < MAX_DRIVES; ++unit) {		ide_drive_t *drive = &hwif->drives[unit];		if (drive->present) {			if (hwif->tuneproc != NULL && 				drive->autotune == IDE_TUNE_AUTO)				/* auto-tune PIO mode */				hwif->tuneproc(drive, 255);			if (drive->autotune != IDE_TUNE_DEFAULT &&			    drive->autotune != IDE_TUNE_AUTO)				continue;			drive->nice1 = 1;			/*			 * MAJOR HACK BARF :-/			 *			 * FIXME: chipsets own this cruft!			 */			/*			 * Move here to prevent module loading clashing.			 */	//		drive->autodma = hwif->autodma;			if (hwif->ide_dma_check) {				/*				 * Force DMAing for the beginning of the check.				 * Some chipsets appear to do interesting				 * things, if not checked and cleared.				 *   PARANOIA!!!				 */				hwif->ide_dma_off_quietly(drive);#ifdef CONFIG_IDEDMA_ONLYDISK				if (drive->media == ide_disk)#endif					hwif->ide_dma_check(drive);			}		}	}}static int hwif_init(ide_hwif_t *hwif);int probe_hwif_init_with_fixup(ide_hwif_t *hwif, void (*fixup)(ide_hwif_t *hwif)){	probe_hwif(hwif);	if (fixup)		fixup(hwif);	if (!hwif_init(hwif)) {		printk(KERN_INFO "%s: failed to initialize IDE interface\n",				 hwif->name);		return -1;	}	if (hwif->present) {		u16 unit = 0;		for (unit = 0; unit < MAX_DRIVES; ++unit) {			ide_drive_t *drive = &hwif->drives[unit];			/* For now don't attach absent drives, we may			   want them on default or a new "empty" class			   for hotplug reprobing ? */			if (drive->present) {				device_register(&drive->gendev);			}		}	}	return 0;}int probe_hwif_init(ide_hwif_t *hwif){	return probe_hwif_init_with_fixup(hwif, NULL);}EXPORT_SYMBOL(probe_hwif_init);#if MAX_HWIFS > 1/* * save_match() is used to simplify logic in init_irq() below. * * A loophole here is that we may not know about a particular * hwif's irq until after that hwif is actually probed/initialized.. * This could be a problem for the case where an hwif is on a * dual interface that requires serialization (eg. cmd640) and another * hwif using one of the same irqs is initialized beforehand. * * This routine detects and reports such situations, but does not fix them. */static void save_match(ide_hwif_t *hwif, ide_hwif_t *new, ide_hwif_t **match){	ide_hwif_t *m = *match;	if (m && m->hwgroup && m->hwgroup != new->hwgroup) {		if (!new->hwgroup)			return;		printk("%s: potential irq problem with %s and %s\n",			hwif->name, new->name, m->name);	}	if (!m || m->irq != hwif->irq) /* don't undo a prior perfect match */		*match = new;}#endif /* MAX_HWIFS > 1 *//* * init request queue */static int ide_init_queue(ide_drive_t *drive){	request_queue_t *q;	ide_hwif_t *hwif = HWIF(drive);	int max_sectors = 256;	int max_sg_entries = PRD_ENTRIES;	/*	 *	Our default set up assumes the normal IDE case,	 *	that is 64K segmenting, standard PRD setup	 *	and LBA28. Some drivers then impose their own	 *	limits and LBA48 we could raise it but as yet	 *	do not.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?