ide-lib.c

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

C
620
字号
/** *	ide_get_best_pio_mode	-	get PIO mode from drive *	@driver: drive to consider *	@mode_wanted: preferred mode *	@max_mode: highest allowed *	@d: pio data * *	This routine returns the recommended PIO settings for a given drive, *	based on the drive->id information and the ide_pio_blacklist[]. *	This is used by most chipset support modules when "auto-tuning". * *	Drive PIO mode auto selection */u8 ide_get_best_pio_mode (ide_drive_t *drive, u8 mode_wanted, u8 max_mode, ide_pio_data_t *d){	int pio_mode;	int cycle_time = 0;	int use_iordy = 0;	struct hd_driveid* id = drive->id;	int overridden  = 0;	int blacklisted = 0;	if (mode_wanted != 255) {		pio_mode = mode_wanted;	} else if (!drive->id) {		pio_mode = 0;	} else if ((pio_mode = ide_scan_pio_blacklist(id->model)) != -1) {		overridden = 1;		blacklisted = 1;		use_iordy = (pio_mode > 2);	} else {		pio_mode = id->tPIO;		if (pio_mode > 2) {	/* 2 is maximum allowed tPIO value */			pio_mode = 2;			overridden = 1;		}		if (id->field_valid & 2) {	  /* drive implements ATA2? */			if (id->capability & 8) { /* drive supports use_iordy? */				use_iordy = 1;				cycle_time = id->eide_pio_iordy;				if (id->eide_pio_modes & 7) {					overridden = 0;					if (id->eide_pio_modes & 4)						pio_mode = 5;					else if (id->eide_pio_modes & 2)						pio_mode = 4;					else						pio_mode = 3;				}			} else {				cycle_time = id->eide_pio;			}		}#if 0		if (drive->id->major_rev_num & 0x0004) printk("ATA-2 ");#endif		/*		 * Conservative "downgrade" for all pre-ATA2 drives		 */		if (pio_mode && pio_mode < 4) {			pio_mode--;			overridden = 1;#if 0			use_iordy = (pio_mode > 2);#endif			if (cycle_time && cycle_time < ide_pio_timings[pio_mode].cycle_time)				cycle_time = 0; /* use standard timing */		}	}	if (pio_mode > max_mode) {		pio_mode = max_mode;		cycle_time = 0;	}	if (d) {		d->pio_mode = pio_mode;		d->cycle_time = cycle_time ? cycle_time : ide_pio_timings[pio_mode].cycle_time;		d->use_iordy = use_iordy;		d->overridden = overridden;		d->blacklisted = blacklisted;	}	return pio_mode;}EXPORT_SYMBOL_GPL(ide_get_best_pio_mode);/** *	ide_toggle_bounce	-	handle bounce buffering *	@drive: drive to update *	@on: on/off boolean * *	Enable or disable bounce buffering for the device. Drives move *	between PIO and DMA and that changes the rules we need. */ void ide_toggle_bounce(ide_drive_t *drive, int on){	u64 addr = BLK_BOUNCE_HIGH;	/* dma64_addr_t */	if (on && drive->media == ide_disk) {		if (!PCI_DMA_BUS_IS_PHYS)			addr = BLK_BOUNCE_ANY;		else if (HWIF(drive)->pci_dev)			addr = HWIF(drive)->pci_dev->dma_mask;	}	if (drive->queue)		blk_queue_bounce_limit(drive->queue, addr);}/** *	ide_set_xfer_rate	-	set transfer rate *	@drive: drive to set *	@speed: speed to attempt to set *	 *	General helper for setting the speed of an IDE device. This *	function knows about user enforced limits from the configuration *	which speedproc() does not.  High level drivers should never *	invoke speedproc() directly. */ int ide_set_xfer_rate(ide_drive_t *drive, u8 rate){#ifndef CONFIG_BLK_DEV_IDEDMA	rate = min(rate, (u8) XFER_PIO_4);#endif	if(HWIF(drive)->speedproc)		return HWIF(drive)->speedproc(drive, rate);	else		return -1;}EXPORT_SYMBOL_GPL(ide_set_xfer_rate);static void ide_dump_opcode(ide_drive_t *drive){	struct request *rq;	u8 opcode = 0;	int found = 0;	spin_lock(&ide_lock);	rq = NULL;	if (HWGROUP(drive))		rq = HWGROUP(drive)->rq;	spin_unlock(&ide_lock);	if (!rq)		return;	if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK)) {		char *args = rq->buffer;		if (args) {			opcode = args[0];			found = 1;		}	} else if (rq->flags & REQ_DRIVE_TASKFILE) {		ide_task_t *args = rq->special;		if (args) {			task_struct_t *tf = (task_struct_t *) args->tfRegister;			opcode = tf->command;			found = 1;		}	}	printk("ide: failed opcode was: ");	if (!found)		printk("unknown\n");	else		printk("0x%02x\n", opcode);}static u8 ide_dump_ata_status(ide_drive_t *drive, const char *msg, u8 stat){	ide_hwif_t *hwif = HWIF(drive);	unsigned long flags;	u8 err = 0;	local_irq_set(flags);	printk("%s: %s: status=0x%02x { ", drive->name, msg, stat);	if (stat & BUSY_STAT)		printk("Busy ");	else {		if (stat & READY_STAT)	printk("DriveReady ");		if (stat & WRERR_STAT)	printk("DeviceFault ");		if (stat & SEEK_STAT)	printk("SeekComplete ");		if (stat & DRQ_STAT)	printk("DataRequest ");		if (stat & ECC_STAT)	printk("CorrectedError ");		if (stat & INDEX_STAT)	printk("Index ");		if (stat & ERR_STAT)	printk("Error ");	}	printk("}\n");	if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {		err = hwif->INB(IDE_ERROR_REG);		printk("%s: %s: error=0x%02x { ", drive->name, msg, err);		if (err & ABRT_ERR)	printk("DriveStatusError ");		if (err & ICRC_ERR)			printk((err & ABRT_ERR) ? "BadCRC " : "BadSector ");		if (err & ECC_ERR)	printk("UncorrectableError ");		if (err & ID_ERR)	printk("SectorIdNotFound ");		if (err & TRK0_ERR)	printk("TrackZeroNotFound ");		if (err & MARK_ERR)	printk("AddrMarkNotFound ");		printk("}");		if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR ||		    (err & (ECC_ERR|ID_ERR|MARK_ERR))) {			if (drive->addressing == 1) {				__u64 sectors = 0;				u32 low = 0, high = 0;				low = ide_read_24(drive);				hwif->OUTB(drive->ctl|0x80, IDE_CONTROL_REG);				high = ide_read_24(drive);				sectors = ((__u64)high << 24) | low;				printk(", LBAsect=%llu, high=%d, low=%d",				       (unsigned long long) sectors,				       high, low);			} else {				u8 cur = hwif->INB(IDE_SELECT_REG);				if (cur & 0x40) {	/* using LBA? */					printk(", LBAsect=%ld", (unsigned long)					 ((cur&0xf)<<24)					 |(hwif->INB(IDE_HCYL_REG)<<16)					 |(hwif->INB(IDE_LCYL_REG)<<8)					 | hwif->INB(IDE_SECTOR_REG));				} else {					printk(", CHS=%d/%d/%d",					 (hwif->INB(IDE_HCYL_REG)<<8) +					  hwif->INB(IDE_LCYL_REG),					  cur & 0xf,					  hwif->INB(IDE_SECTOR_REG));				}			}			if (HWGROUP(drive) && HWGROUP(drive)->rq)				printk(", sector=%llu",					(unsigned long long)HWGROUP(drive)->rq->sector);		}		printk("\n");	}	ide_dump_opcode(drive);	local_irq_restore(flags);	return err;}/** *	ide_dump_atapi_status       -       print human readable atapi status *	@drive: drive that status applies to *	@msg: text message to print *	@stat: status byte to decode * *	Error reporting, in human readable form (luxurious, but a memory hog). */static u8 ide_dump_atapi_status(ide_drive_t *drive, const char *msg, u8 stat){	unsigned long flags;	atapi_status_t status;	atapi_error_t error;	status.all = stat;	error.all = 0;	local_irq_set(flags);	printk("%s: %s: status=0x%02x { ", drive->name, msg, stat);	if (status.b.bsy)		printk("Busy ");	else {		if (status.b.drdy)	printk("DriveReady ");		if (status.b.df)	printk("DeviceFault ");		if (status.b.dsc)	printk("SeekComplete ");		if (status.b.drq)	printk("DataRequest ");		if (status.b.corr)	printk("CorrectedError ");		if (status.b.idx)	printk("Index ");		if (status.b.check)	printk("Error ");	}	printk("}\n");	if (status.b.check && !status.b.bsy) {		error.all = HWIF(drive)->INB(IDE_ERROR_REG);		printk("%s: %s: error=0x%02x { ", drive->name, msg, error.all);		if (error.b.ili)	printk("IllegalLengthIndication ");		if (error.b.eom)	printk("EndOfMedia ");		if (error.b.abrt)	printk("AbortedCommand ");		if (error.b.mcr)	printk("MediaChangeRequested ");		if (error.b.sense_key)	printk("LastFailedSense=0x%02x ",						error.b.sense_key);		printk("}\n");	}	ide_dump_opcode(drive);	local_irq_restore(flags);	return error.all;}/** *	ide_dump_status		-	translate ATA/ATAPI error *	@drive: drive the error occured on *	@msg: information string *	@stat: status byte * *	Error reporting, in human readable form (luxurious, but a memory hog). *	Combines the drive name, message and status byte to provide a *	user understandable explanation of the device error. */u8 ide_dump_status(ide_drive_t *drive, const char *msg, u8 stat){	if (drive->media == ide_disk)		return ide_dump_ata_status(drive, msg, stat);	return ide_dump_atapi_status(drive, msg, stat);}EXPORT_SYMBOL(ide_dump_status);

⌨️ 快捷键说明

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