scsi_cd.c

来自「基于组件方式开发操作系统的OSKIT源代码」· C语言 代码 · 共 2,447 行 · 第 1/5 页

C
2,447 行
字号
		softc->device_stats.block_size = softc->params.blksize;	} else {		if ((dsisopen(softc->cd_slices) == 0)		 && ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0))			cdprevent(periph, PR_ALLOW);	}	cam_periph_unlock(periph);	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("leaving cdopen\n"));	return (error);}static intcdclose(dev_t dev, int flag, int fmt, struct proc *p){	struct 	cam_periph *periph;	struct	cd_softc *softc;	int	unit, error;	unit = dkunit(dev);	periph = cam_extend_get(cdperiphs, unit);	if (periph == NULL)		return (ENXIO);		softc = (struct cd_softc *)periph->softc;	if ((error = cam_periph_lock(periph, PRIBIO)) != 0)		return (error);	dsclose(dev, fmt, softc->cd_slices);	if (dsisopen(softc->cd_slices)) {		cam_periph_unlock(periph);		return (0);	}	if ((softc->flags & CD_FLAG_DISC_REMOVABLE) != 0)		cdprevent(periph, PR_ALLOW);	/*	 * Since we're closing this CD, mark the blocksize as unavailable.	 * It will be marked as available whence the CD is opened again.	 */	softc->device_stats.flags |= DEVSTAT_BS_UNAVAILABLE;	softc->flags &= ~CD_FLAG_OPEN;	cam_periph_unlock(periph);	cam_periph_release(periph);	return (0);}static intcdread(dev_t dev, struct uio *uio, int ioflag){	return(physio(cdstrategy, NULL, dev, 1, minphys, uio));}static voidcdshorttimeout(void *arg){	struct cdchanger *changer;	int s;	s = splsoftcam();	changer = (struct cdchanger *)arg;	/* Always clear the short timeout flag, since that's what we're in */	changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;	/*	 * Check to see if there is any more pending or outstanding I/O for	 * this device.  If not, move it out of the active slot.	 */	if ((bufq_first(&changer->cur_device->buf_queue) == NULL)	 && (changer->cur_device->device_stats.busy_count == 0)) {		changer->flags |= CHANGER_MANUAL_CALL;		cdrunchangerqueue(changer);	}	splx(s);}/* * This is a wrapper for xpt_schedule.  It only applies to changers. */static voidcdschedule(struct cam_periph *periph, int priority){	struct cd_softc *softc;	int s;	s = splsoftcam();	softc = (struct cd_softc *)periph->softc;	/*	 * If this device isn't currently queued, and if it isn't	 * the active device, then we queue this device and run the	 * changer queue if there is no timeout scheduled to do it.	 * If this device is the active device, just schedule it	 * to run again.  If this device is queued, there should be	 * a timeout in place already that will make sure it runs.	 */	if ((softc->pinfo.index == CAM_UNQUEUED_INDEX) 	 && ((softc->flags & CD_FLAG_ACTIVE) == 0)) {		/*		 * We don't do anything with the priority here.		 * This is strictly a fifo queue.		 */		softc->pinfo.priority = 1;		softc->pinfo.generation = ++softc->changer->devq.generation;		camq_insert(&softc->changer->devq, (cam_pinfo *)softc);		/*		 * Since we just put a device in the changer queue,		 * check and see if there is a timeout scheduled for		 * this changer.  If so, let the timeout handle		 * switching this device into the active slot.  If		 * not, manually call the timeout routine to		 * bootstrap things.		 */		if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)		 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)		 && ((softc->changer->flags & CHANGER_SHORT_TMOUT_SCHED)==0)){			softc->changer->flags |= CHANGER_MANUAL_CALL;			cdrunchangerqueue(softc->changer);		}	} else if ((softc->flags & CD_FLAG_ACTIVE)		&& ((softc->flags & CD_FLAG_SCHED_ON_COMP) == 0))		xpt_schedule(periph, priority);	splx(s);}static voidcdrunchangerqueue(void *arg){	struct cd_softc *softc;	struct cdchanger *changer;	int called_from_timeout;	int s;	s = splsoftcam();	changer = (struct cdchanger *)arg;	/*	 * If we have NOT been called from cdstrategy() or cddone(), and	 * instead from a timeout routine, go ahead and clear the	 * timeout flag.	 */	if ((changer->flags & CHANGER_MANUAL_CALL) == 0) {		changer->flags &= ~CHANGER_TIMEOUT_SCHED;		called_from_timeout = 1;	} else		called_from_timeout = 0;	/* Always clear the manual call flag */	changer->flags &= ~CHANGER_MANUAL_CALL;	/* nothing to do if the queue is empty */	if (changer->devq.entries <= 0) {		splx(s);		return;	}	/*	 * If the changer queue is frozen, that means we have an active	 * device.	 */	if (changer->devq.qfrozen_cnt > 0) {		if (changer->cur_device->device_stats.busy_count > 0) {			changer->cur_device->flags |= CD_FLAG_SCHED_ON_COMP;			changer->cur_device->bufs_left = 				changer->cur_device->device_stats.busy_count;			if (called_from_timeout) {				changer->long_handle =					timeout(cdrunchangerqueue, changer,				        changer_max_busy_seconds * hz);				changer->flags |= CHANGER_TIMEOUT_SCHED;			}			splx(s);			return;		}		/*		 * We always need to reset the frozen count and clear the		 * active flag.		 */		changer->devq.qfrozen_cnt--;		changer->cur_device->flags &= ~CD_FLAG_ACTIVE;		changer->cur_device->flags &= ~CD_FLAG_SCHED_ON_COMP;		/*		 * Check to see whether the current device has any I/O left		 * to do.  If so, requeue it at the end of the queue.  If		 * not, there is no need to requeue it.		 */		if (bufq_first(&changer->cur_device->buf_queue) != NULL) {			changer->cur_device->pinfo.generation =				++changer->devq.generation;			camq_insert(&changer->devq,				(cam_pinfo *)changer->cur_device);		} 	}	softc = (struct cd_softc *)camq_remove(&changer->devq, CAMQ_HEAD);	changer->cur_device = softc;	changer->devq.qfrozen_cnt++;	softc->flags |= CD_FLAG_ACTIVE;	/* Just in case this device is waiting */	wakeup(&softc->changer);	xpt_schedule(softc->periph, /*priority*/ 1);	/*	 * Get rid of any pending timeouts, and set a flag to schedule new	 * ones so this device gets its full time quantum.	 */	if (changer->flags & CHANGER_TIMEOUT_SCHED) {		untimeout(cdrunchangerqueue, changer, changer->long_handle);		changer->flags &= ~CHANGER_TIMEOUT_SCHED;	}	if (changer->flags & CHANGER_SHORT_TMOUT_SCHED) {		untimeout(cdshorttimeout, changer, changer->short_handle);		changer->flags &= ~CHANGER_SHORT_TMOUT_SCHED;	}	/*	 * We need to schedule timeouts, but we only do this after the	 * first transaction has completed.  This eliminates the changer	 * switch time.	 */	changer->flags |= CHANGER_NEED_TIMEOUT;	splx(s);}static voidcdchangerschedule(struct cd_softc *softc){	struct cdchanger *changer;	int s;	s = splsoftcam();	changer = softc->changer;	/*	 * If this is a changer, and this is the current device,	 * and this device has at least the minimum time quantum to	 * run, see if we can switch it out.	 */	if ((softc->flags & CD_FLAG_ACTIVE) 	 && ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0)	 && ((changer->flags & CHANGER_NEED_TIMEOUT) == 0)) {		/*		 * We try three things here.  The first is that we		 * check to see whether the schedule on completion		 * flag is set.  If it is, we decrement the number		 * of buffers left, and if it's zero, we reschedule.		 * Next, we check to see whether the pending buffer		 * queue is empty and whether there are no		 * outstanding transactions.  If so, we reschedule.		 * Next, we see if the pending buffer queue is empty.		 * If it is, we set the number of buffers left to		 * the current active buffer count and set the		 * schedule on complete flag.		 */		if (softc->flags & CD_FLAG_SCHED_ON_COMP) {		 	if (--softc->bufs_left == 0) {				softc->changer->flags |=					CHANGER_MANUAL_CALL;				softc->flags &= ~CD_FLAG_SCHED_ON_COMP;				cdrunchangerqueue(softc->changer);			}		} else if ((bufq_first(&softc->buf_queue) == NULL)		        && (softc->device_stats.busy_count == 0)) {			softc->changer->flags |= CHANGER_MANUAL_CALL;			cdrunchangerqueue(softc->changer);		}	} else if ((softc->changer->flags & CHANGER_NEED_TIMEOUT) 		&& (softc->flags & CD_FLAG_ACTIVE)) {		/*		 * Now that the first transaction to this		 * particular device has completed, we can go ahead		 * and schedule our timeouts.		 */		if ((changer->flags & CHANGER_TIMEOUT_SCHED) == 0) {			changer->long_handle =			    timeout(cdrunchangerqueue, changer,				    changer_max_busy_seconds * hz);			changer->flags |= CHANGER_TIMEOUT_SCHED;		} else			printf("cdchangerschedule: already have a long"			       " timeout!\n");		if ((changer->flags & CHANGER_SHORT_TMOUT_SCHED) == 0) {			changer->short_handle =			    timeout(cdshorttimeout, changer,				    changer_min_busy_seconds * hz);			changer->flags |= CHANGER_SHORT_TMOUT_SCHED;		} else			printf("cdchangerschedule: already have a short "			       "timeout!\n");		/*		 * We just scheduled timeouts, no need to schedule		 * more.		 */		changer->flags &= ~CHANGER_NEED_TIMEOUT;	}	splx(s);}static intcdrunccb(union ccb *ccb, int (*error_routine)(union ccb *ccb,					      u_int32_t cam_flags,					      u_int32_t sense_flags),	 u_int32_t cam_flags, u_int32_t sense_flags){	struct cd_softc *softc;	struct cam_periph *periph;	int error;	periph = xpt_path_periph(ccb->ccb_h.path);	softc = (struct cd_softc *)periph->softc;	error = cam_periph_runccb(ccb, error_routine, cam_flags, sense_flags,				  &softc->device_stats);	if (softc->flags & CD_FLAG_CHANGER)		cdchangerschedule(softc);	return(error);}static union ccb *cdgetccb(struct cam_periph *periph, u_int32_t priority){	struct cd_softc *softc;	int s;	softc = (struct cd_softc *)periph->softc;	if (softc->flags & CD_FLAG_CHANGER) {		s = splsoftcam();		/*		 * This should work the first time this device is woken up,		 * but just in case it doesn't, we use a while loop.		 */		while ((softc->flags & CD_FLAG_ACTIVE) == 0) {			/*			 * If this changer isn't already queued, queue it up.			 */			if (softc->pinfo.index == CAM_UNQUEUED_INDEX) {				softc->pinfo.priority = 1;				softc->pinfo.generation =					++softc->changer->devq.generation;				camq_insert(&softc->changer->devq,					    (cam_pinfo *)softc);			}			if (((softc->changer->flags & CHANGER_TIMEOUT_SCHED)==0)			 && ((softc->changer->flags & CHANGER_NEED_TIMEOUT)==0)			 && ((softc->changer->flags			      & CHANGER_SHORT_TMOUT_SCHED)==0)) {				softc->changer->flags |= CHANGER_MANUAL_CALL;				cdrunchangerqueue(softc->changer);			} else				tsleep(&softc->changer, PRIBIO, "cgticb", 0);		}		splx(s);	}	return(cam_periph_getccb(periph, priority));}/* * Actually translate the requested transfer into one the physical driver * can understand.  The transfer is described by a buf and will include * only one physical transfer. */static voidcdstrategy(struct buf *bp){	struct cam_periph *periph;	struct cd_softc *softc;	u_int  unit, part;	int    s;	unit = dkunit(bp->b_dev);	part = dkpart(bp->b_dev);	periph = cam_extend_get(cdperiphs, unit);	if (periph == NULL) {		bp->b_error = ENXIO;		goto bad;	}	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstrategy\n"));	softc = (struct cd_softc *)periph->softc;	/*	 * Do bounds checking, adjust transfer, and set b_pbklno.	 */	if (dscheck(bp, softc->cd_slices) <= 0)		goto done;	/*	 * Mask interrupts so that the pack cannot be invalidated until	 * after we are in the queue.  Otherwise, we might not properly	 * clean up one of the buffers.	 */	s = splbio();		/*	 * If the device has been made invalid, error out	 */	if ((softc->flags & CD_FLAG_INVALID)) {		splx(s);		bp->b_error = ENXIO;		goto bad;	}	/*	 * Place it in the queue of disk activities for this disk	 */	bufqdisksort(&softc->buf_queue, bp);	splx(s);		/*	 * Schedule ourselves for performing the work.  We do things	 * differently for changers.	 */	if ((softc->flags & CD_FLAG_CHANGER) == 0)		xpt_schedule(periph, /* XXX priority */1);	else		cdschedule(periph, /* priority */ 1);	return;bad:	bp->b_flags |= B_ERROR;done:	/*	 * Correctly set the buf to indicate a completed xfer	 */	bp->b_resid = bp->b_bcount;	biodone(bp);	return;}static voidcdstrategy1(struct buf *bp){	/*	 * XXX - do something to make cdstrategy() but not this block while	 * we're doing dsopen() and dsioctl().	 */	cdstrategy(bp);}static voidcdstart(struct cam_periph *periph, union ccb *start_ccb){	struct cd_softc *softc;	struct buf *bp;	struct ccb_scsiio *csio;	struct scsi_read_capacity_data *rcap;	int s;	softc = (struct cd_softc *)periph->softc;	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdstart\n"));	switch (softc->state) {	case CD_STATE_NORMAL:

⌨️ 快捷键说明

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