ide-io.c

来自「Linux Kernel 2.6.9 for OMAP1710」· C语言 代码 · 共 1,604 行 · 第 1/4 页

C
1,604
字号
		goto kill_rq;	block    = rq->sector;	if (blk_fs_request(rq) &&	    (drive->media == ide_disk || drive->media == ide_floppy)) {		block += drive->sect0;	}	/* Yecch - this will shift the entire interval,	   possibly killing some innocent following sector */	if (block == 0 && drive->remap_0_to_1 == 1)		block = 1;  /* redirect MBR access to EZ-Drive partn table */	if (blk_pm_suspend_request(rq) &&	    rq->pm->pm_step == ide_pm_state_start_suspend)		/* Mark drive blocked when starting the suspend sequence. */		drive->blocked = 1;	else if (blk_pm_resume_request(rq) &&		 rq->pm->pm_step == ide_pm_state_start_resume) {		/* 		 * The first thing we do on wakeup is to wait for BSY bit to		 * go away (with a looong timeout) as a drive on this hwif may		 * just be POSTing itself.		 * We do that before even selecting as the "other" device on		 * the bus may be broken enough to walk on our toes at this		 * point.		 */		int rc;#ifdef DEBUG_PM		printk("%s: Wakeup request inited, waiting for !BSY...\n", drive->name);#endif		rc = ide_wait_not_busy(HWIF(drive), 35000);		if (rc)			printk(KERN_WARNING "%s: bus not ready on wakeup\n", drive->name);		SELECT_DRIVE(drive);		HWIF(drive)->OUTB(8, HWIF(drive)->io_ports[IDE_CONTROL_OFFSET]);		rc = ide_wait_not_busy(HWIF(drive), 10000);		if (rc)			printk(KERN_WARNING "%s: drive not ready on wakeup\n", drive->name);	}	SELECT_DRIVE(drive);	if (ide_wait_stat(&startstop, drive, drive->ready_stat, BUSY_STAT|DRQ_STAT, WAIT_READY)) {		printk(KERN_ERR "%s: drive not ready for command\n", drive->name);		return startstop;	}	if (!drive->special.all) {		if (rq->flags & (REQ_DRIVE_CMD | REQ_DRIVE_TASK))			return execute_drive_cmd(drive, rq);		else if (rq->flags & REQ_DRIVE_TASKFILE)			return execute_drive_cmd(drive, rq);		else if (blk_pm_request(rq)) {#ifdef DEBUG_PM			printk("%s: start_power_step(step: %d)\n",				drive->name, rq->pm->pm_step);#endif			startstop = DRIVER(drive)->start_power_step(drive, rq);			if (startstop == ide_stopped &&			    rq->pm->pm_step == ide_pm_state_completed)				ide_complete_pm_request(drive, rq);			return startstop;		}		return (DRIVER(drive)->do_request(drive, rq, block));	}	return do_special(drive);kill_rq:	DRIVER(drive)->end_request(drive, 0, 0);	return ide_stopped;}EXPORT_SYMBOL(start_request);/** *	ide_stall_queue		-	pause an IDE device *	@drive: drive to stall *	@timeout: time to stall for (jiffies) * *	ide_stall_queue() can be used by a drive to give excess bandwidth back *	to the hwgroup by sleeping for timeout jiffies. */ void ide_stall_queue (ide_drive_t *drive, unsigned long timeout){	if (timeout > WAIT_WORSTCASE)		timeout = WAIT_WORSTCASE;	drive->sleep = timeout + jiffies;}EXPORT_SYMBOL(ide_stall_queue);#define WAKEUP(drive)	((drive)->service_start + 2 * (drive)->service_time)/** *	choose_drive		-	select a drive to service *	@hwgroup: hardware group to select on * *	choose_drive() selects the next drive which will be serviced. *	This is necessary because the IDE layer can't issue commands *	to both drives on the same cable, unlike SCSI. */ static inline ide_drive_t *choose_drive (ide_hwgroup_t *hwgroup){	ide_drive_t *drive, *best;repeat:		best = NULL;	drive = hwgroup->drive;	/*	 * drive is doing pre-flush, ordered write, post-flush sequence. even	 * though that is 3 requests, it must be seen as a single transaction.	 * we must not preempt this drive until that is complete	 */	if (drive->doing_barrier) {		/*		 * small race where queue could get replugged during		 * the 3-request flush cycle, just yank the plug since		 * we want it to finish asap		 */		blk_remove_plug(drive->queue);		return drive;	}	do {		if ((!drive->sleep || time_after_eq(jiffies, drive->sleep))		    && !elv_queue_empty(drive->queue)) {			if (!best			 || (drive->sleep && (!best->sleep || 0 < (signed long)(best->sleep - drive->sleep)))			 || (!best->sleep && 0 < (signed long)(WAKEUP(best) - WAKEUP(drive))))			{				if (!blk_queue_plugged(drive->queue))					best = drive;			}		}	} while ((drive = drive->next) != hwgroup->drive);	if (best && best->nice1 && !best->sleep && best != hwgroup->drive && best->service_time > WAIT_MIN_SLEEP) {		long t = (signed long)(WAKEUP(best) - jiffies);		if (t >= WAIT_MIN_SLEEP) {		/*		 * We *may* have some time to spare, but first let's see if		 * someone can potentially benefit from our nice mood today..		 */			drive = best->next;			do {				if (!drive->sleep				/* FIXME: use time_before */				 && 0 < (signed long)(WAKEUP(drive) - (jiffies - best->service_time))				 && 0 < (signed long)((jiffies + t) - WAKEUP(drive)))				{					ide_stall_queue(best, min_t(long, t, 10 * WAIT_MIN_SLEEP));					goto repeat;				}			} while ((drive = drive->next) != best);		}	}	return best;}/* * Issue a new request to a drive from hwgroup * Caller must have already done spin_lock_irqsave(&ide_lock, ..); * * A hwgroup is a serialized group of IDE interfaces.  Usually there is * exactly one hwif (interface) per hwgroup, but buggy controllers (eg. CMD640) * may have both interfaces in a single hwgroup to "serialize" access. * Or possibly multiple ISA interfaces can share a common IRQ by being grouped * together into one hwgroup for serialized access. * * Note also that several hwgroups can end up sharing a single IRQ, * possibly along with many other devices.  This is especially common in * PCI-based systems with off-board IDE controller cards. * * The IDE driver uses the single global ide_lock spinlock to protect * access to the request queues, and to protect the hwgroup->busy flag. * * The first thread into the driver for a particular hwgroup sets the * hwgroup->busy flag to indicate that this hwgroup is now active, * and then initiates processing of the top request from the request queue. * * Other threads attempting entry notice the busy setting, and will simply * queue their new requests and exit immediately.  Note that hwgroup->busy * remains set even when the driver is merely awaiting the next interrupt. * Thus, the meaning is "this hwgroup is busy processing a request". * * When processing of a request completes, the completing thread or IRQ-handler * will start the next request from the queue.  If no more work remains, * the driver will clear the hwgroup->busy flag and exit. * * The ide_lock (spinlock) is used to protect all access to the * hwgroup->busy flag, but is otherwise not needed for most processing in * the driver.  This makes the driver much more friendlier to shared IRQs * than previous designs, while remaining 100% (?) SMP safe and capable. *//* --BenH: made non-static as ide-pmac.c uses it to kick the hwgroup back *         into life on wakeup from machine sleep. */ void ide_do_request (ide_hwgroup_t *hwgroup, int masked_irq){	ide_drive_t	*drive;	ide_hwif_t	*hwif;	struct request	*rq;	ide_startstop_t	startstop;	/* for atari only: POSSIBLY BROKEN HERE(?) */	ide_get_lock(ide_intr, hwgroup);	/* caller must own ide_lock */	BUG_ON(!irqs_disabled());	while (!hwgroup->busy) {		hwgroup->busy = 1;		drive = choose_drive(hwgroup);		if (drive == NULL) {			unsigned long sleep = 0;			hwgroup->rq = NULL;			drive = hwgroup->drive;			do {				if (drive->sleep && (!sleep || 0 < (signed long)(sleep - drive->sleep)))					sleep = drive->sleep;			} while ((drive = drive->next) != hwgroup->drive);			if (sleep) {		/*		 * Take a short snooze, and then wake up this hwgroup again.		 * This gives other hwgroups on the same a chance to		 * play fairly with us, just in case there are big differences		 * in relative throughputs.. don't want to hog the cpu too much.		 */				if (time_before(sleep, jiffies + WAIT_MIN_SLEEP))					sleep = jiffies + WAIT_MIN_SLEEP;#if 1				if (timer_pending(&hwgroup->timer))					printk(KERN_CRIT "ide_set_handler: timer already active\n");#endif				/* so that ide_timer_expiry knows what to do */				hwgroup->sleeping = 1;				mod_timer(&hwgroup->timer, sleep);				/* we purposely leave hwgroup->busy==1				 * while sleeping */			} else {				/* Ugly, but how can we sleep for the lock				 * otherwise? perhaps from tq_disk?				 */				/* for atari only */				ide_release_lock();				hwgroup->busy = 0;			}			/* no more work for this hwgroup (for now) */			return;		}		hwif = HWIF(drive);		if (hwgroup->hwif->sharing_irq &&		    hwif != hwgroup->hwif &&		    hwif->io_ports[IDE_CONTROL_OFFSET]) {			/* set nIEN for previous hwif */			SELECT_INTERRUPT(drive);		}		hwgroup->hwif = hwif;		hwgroup->drive = drive;		drive->sleep = 0;		drive->service_start = jiffies;		if (blk_queue_plugged(drive->queue)) {			printk(KERN_ERR "ide: huh? queue was plugged!\n");			break;		}		/*		 * we know that the queue isn't empty, but this can happen		 * if the q->prep_rq_fn() decides to kill a request		 */		rq = elv_next_request(drive->queue);		if (!rq) {			hwgroup->busy = 0;			break;		}		/*		 * if rq is a barrier write, issue pre cache flush if not		 * already done		 */		if (blk_barrier_rq(rq) && !blk_barrier_preflush(rq))			rq = ide_queue_flush_cmd(drive, rq, 0);		/*		 * Sanity: don't accept a request that isn't a PM request		 * if we are currently power managed. This is very important as		 * blk_stop_queue() doesn't prevent the elv_next_request()		 * above to return us whatever is in the queue. Since we call		 * ide_do_request() ourselves, we end up taking requests while		 * the queue is blocked...		 * 		 * We let requests forced at head of queue with ide-preempt		 * though. I hope that doesn't happen too much, hopefully not		 * unless the subdriver triggers such a thing in its own PM		 * state machine.		 */		if (drive->blocked && !blk_pm_request(rq) && !(rq->flags & REQ_PREEMPT)) {			/* We clear busy, there should be no pending ATA command at this point. */			hwgroup->busy = 0;			break;		}		hwgroup->rq = rq;		/*		 * Some systems have trouble with IDE IRQs arriving while		 * the driver is still setting things up.  So, here we disable		 * the IRQ used by this interface while the request is being started.		 * This may look bad at first, but pretty much the same thing		 * happens anyway when any interrupt comes in, IDE or otherwise		 *  -- the kernel masks the IRQ while it is being handled.		 */		if (hwif->irq != masked_irq)			disable_irq_nosync(hwif->irq);		spin_unlock(&ide_lock);		local_irq_enable();			/* allow other IRQs while we start this request */		startstop = start_request(drive, rq);		spin_lock_irq(&ide_lock);		if (hwif->irq != masked_irq)			enable_irq(hwif->irq);		if (startstop == ide_stopped)			hwgroup->busy = 0;	}}EXPORT_SYMBOL(ide_do_request);/* * Passes the stuff to ide_do_request */void do_ide_request(request_queue_t *q){	ide_drive_t *drive = q->queuedata;	ide_do_request(HWGROUP(drive), IDE_NO_IRQ);}/* * un-busy the hwgroup etc, and clear any pending DMA status. we want to * retry the current request in pio mode instead of risking tossing it * all away */static ide_startstop_t ide_dma_timeout_retry(ide_drive_t *drive, int error){	ide_hwif_t *hwif = HWIF(drive);	struct request *rq;	ide_startstop_t ret = ide_stopped;	/*	 * end current dma transaction	 */	if (error < 0) {		printk(KERN_WARNING "%s: DMA timeout error\n", drive->name);		(void)HWIF(drive)->ide_dma_end(drive);		ret = DRIVER(drive)->error(drive, "dma timeout error",						hwif->INB(IDE_STATUS_REG));	} else {		printk(KERN_WARNING "%s: DMA timeout retry\n", drive->name);		(void) hwif->ide_dma_timeout(drive);	}	/*	 * disable dma for now, but remember that we did so because of	 * a timeout -- we'll reenable after we finish this next request	 * (or rather the first chunk of it) in pio.	 */	drive->retry_pio++;	drive->state = DMA_PIO_RETRY;	(void) hwif->ide_dma_off_quietly(drive);	/*	 * un-busy drive etc (hwgroup->busy is cleared on return) and	 * make sure request is sane	 */	rq = HWGROUP(drive)->rq;	HWGROUP(drive)->rq = NULL;	rq->errors = 0;	rq->sector = rq->bio->bi_sector;	rq->current_nr_sectors = bio_iovec(rq->bio)->bv_len >> 9;	rq->hard_cur_sectors = rq->current_nr_sectors;	if (rq->bio)		rq->buffer = NULL;	return ret;}/** *	ide_timer_expiry	-	handle lack of an IDE interrupt *	@data: timer callback magic (hwgroup) * *	An IDE command has timed out before the expected drive return *	occurred. At this point we attempt to clean up the current *	mess. If the current handler includes an expiry handler then *	we invoke the expiry handler, and providing it is happy the *	work is done. If that fails we apply generic recovery rules *	invoking the handler and checking the drive DMA status. We

⌨️ 快捷键说明

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