cam_xpt.c

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

C
2,349 行
字号
		printf("xptioctl: got invalid xpt unit %d\n", unit);		return(ENXIO);	}	switch(cmd) {	/*	 * For the transport layer CAMIOCOMMAND ioctl, we really only want	 * to accept CCB types that don't quite make sense to send through a	 * passthrough driver.	 */	case CAMIOCOMMAND: {		union ccb *ccb;		union ccb *inccb;		inccb = (union ccb *)addr;		switch(inccb->ccb_h.func_code) {		case XPT_SCAN_BUS:		case XPT_RESET_BUS:			if ((inccb->ccb_h.target_id != CAM_TARGET_WILDCARD)			 || (inccb->ccb_h.target_lun != CAM_LUN_WILDCARD)) {				error = EINVAL;				break;			}			/* FALLTHROUGH */		case XPT_SCAN_LUN:		case XPT_RESET_DEV:		case XPT_ENG_INQ:  /* XXX not implemented yet */		case XPT_ENG_EXEC:			ccb = xpt_alloc_ccb();			/*			 * Create a path using the bus, target, and lun the			 * user passed in.			 */			if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,					    inccb->ccb_h.path_id,					    inccb->ccb_h.target_id,					    inccb->ccb_h.target_lun) !=					    CAM_REQ_CMP){				error = EINVAL;				xpt_free_ccb(ccb);				break;			}			/* Ensure all of our fields are correct */			xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,				      inccb->ccb_h.pinfo.priority);			xpt_merge_ccb(ccb, inccb);			ccb->ccb_h.cbfcnp = xptdone;			cam_periph_runccb(ccb, NULL, 0, 0, NULL);			bcopy(ccb, inccb, sizeof(union ccb));			xpt_free_path(ccb->ccb_h.path);			xpt_free_ccb(ccb);			break;		case XPT_DEBUG: {			union ccb ccb;			/*			 * This is an immediate CCB, so it's okay to			 * allocate it on the stack.			 */			/*			 * Create a path using the bus, target, and lun the			 * user passed in.			 */			if (xpt_create_path(&ccb.ccb_h.path, xpt_periph,					    inccb->ccb_h.path_id,					    inccb->ccb_h.target_id,					    inccb->ccb_h.target_lun) !=					    CAM_REQ_CMP){				error = EINVAL;				break;			}			/* Ensure all of our fields are correct */			xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,				      inccb->ccb_h.pinfo.priority);			xpt_merge_ccb(&ccb, inccb);			ccb.ccb_h.cbfcnp = xptdone;			xpt_action(&ccb);			bcopy(&ccb, inccb, sizeof(union ccb));			xpt_free_path(ccb.ccb_h.path);			break;		}		case XPT_DEV_MATCH: {			struct cam_periph_map_info mapinfo;			struct cam_path *old_path;			/*			 * We can't deal with physical addresses for this			 * type of transaction.			 */			if (inccb->ccb_h.flags & CAM_DATA_PHYS) {				error = EINVAL;				break;			}			/*			 * Save this in case the caller had it set to			 * something in particular.			 */			old_path = inccb->ccb_h.path;			/*			 * We really don't need a path for the matching			 * code.  The path is needed because of the			 * debugging statements in xpt_action().  They			 * assume that the CCB has a valid path.			 */			inccb->ccb_h.path = xpt_periph->path;			bzero(&mapinfo, sizeof(mapinfo));			/*			 * Map the pattern and match buffers into kernel			 * virtual address space.			 */			error = cam_periph_mapmem(inccb, &mapinfo);			if (error) {				inccb->ccb_h.path = old_path;				break;			}			/*			 * This is an immediate CCB, we can send it on directly.			 */			xpt_action(inccb);			/*			 * Map the buffers back into user space.			 */			cam_periph_unmapmem(inccb, &mapinfo);			inccb->ccb_h.path = old_path;			error = 0;			break;		}		default:			error = EINVAL;			break;		}		break;	}	/*	 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,	 * with the periphal driver name and unit name filled in.  The other	 * fields don't really matter as input.  The passthrough driver name	 * ("pass"), and unit number are passed back in the ccb.  The current	 * device generation number, and the index into the device peripheral	 * driver list, and the status are also passed back.  Note that	 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,	 * we never return a status of CAM_GDEVLIST_LIST_CHANGED.  It is	 * (or rather should be) impossible for the device peripheral driver	 * list to change since we look at the whole thing in one pass, and	 * we do it with splcam protection.	 * 	 */	case CAMGETPASSTHRU: {		union ccb *ccb;		struct cam_periph *periph;		struct periph_driver **p_drv;		char   *name;		int unit;		int cur_generation;		int base_periph_found;		int splbreaknum;		int s;		ccb = (union ccb *)addr;		unit = ccb->cgdl.unit_number;		name = ccb->cgdl.periph_name;		/*		 * Every 100 devices, we want to drop our spl protection to		 * give the software interrupt handler a chance to run.		 * Most systems won't run into this check, but this should		 * avoid starvation in the software interrupt handler in		 * large systems.		 */		splbreaknum = 100;		ccb = (union ccb *)addr;		base_periph_found = 0;		/*		 * Sanity check -- make sure we don't get a null peripheral		 * driver name.		 */		if (*ccb->cgdl.periph_name == '\0') {			error = EINVAL;			break;		}		/* Keep the list from changing while we traverse it */		s = splcam();ptstartover:		cur_generation = xsoftc.generation;		/* first find our driver in the list of drivers */		for (p_drv = (struct periph_driver **)periphdriver_set.ls_items;		     *p_drv != NULL; p_drv++)			if (strcmp((*p_drv)->driver_name, name) == 0)				break;		if (*p_drv == NULL) {			splx(s);			ccb->ccb_h.status = CAM_REQ_CMP_ERR;			ccb->cgdl.status = CAM_GDEVLIST_ERROR;			*ccb->cgdl.periph_name = '\0';			ccb->cgdl.unit_number = 0;			error = ENOENT;			break;		}			/*		 * Run through every peripheral instance of this driver		 * and check to see whether it matches the unit passed		 * in by the user.  If it does, get out of the loops and		 * find the passthrough driver associated with that		 * peripheral driver.		 */		for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;		     periph = TAILQ_NEXT(periph, unit_links)) {			if (periph->unit_number == unit) {				break;			} else if (--splbreaknum == 0) {				splx(s);				s = splcam();				splbreaknum = 100;				if (cur_generation != xsoftc.generation)				       goto ptstartover;			}		}		/*		 * If we found the peripheral driver that the user passed		 * in, go through all of the peripheral drivers for that		 * particular device and look for a passthrough driver.		 */		if (periph != NULL) {			struct cam_ed *device;			int i;			base_periph_found = 1;			device = periph->path->device;			for (i = 0, periph = device->periphs.slh_first;			     periph != NULL;			     periph = periph->periph_links.sle_next, i++) {				/*				 * Check to see whether we have a				 * passthrough device or not. 				 */				if (strcmp(periph->periph_name, "pass") == 0) {					/*					 * Fill in the getdevlist fields.					 */					strcpy(ccb->cgdl.periph_name,					       periph->periph_name);					ccb->cgdl.unit_number =						periph->unit_number;					if (periph->periph_links.sle_next)						ccb->cgdl.status =							CAM_GDEVLIST_MORE_DEVS;					else						ccb->cgdl.status =						       CAM_GDEVLIST_LAST_DEVICE;					ccb->cgdl.generation =						device->generation;					ccb->cgdl.index = i;					/*					 * Fill in some CCB header fields					 * that the user may want.					 */					ccb->ccb_h.path_id =						periph->path->bus->path_id;					ccb->ccb_h.target_id =						periph->path->target->target_id;					ccb->ccb_h.target_lun =						periph->path->device->lun_id;					ccb->ccb_h.status = CAM_REQ_CMP;					break;				}			}		}		/*		 * If the periph is null here, one of two things has		 * happened.  The first possibility is that we couldn't		 * find the unit number of the particular peripheral driver		 * that the user is asking about.  e.g. the user asks for		 * the passthrough driver for "da11".  We find the list of		 * "da" peripherals all right, but there is no unit 11.		 * The other possibility is that we went through the list		 * of peripheral drivers attached to the device structure,		 * but didn't find one with the name "pass".  Either way,		 * we return ENOENT, since we couldn't find something.		 */		if (periph == NULL) {			ccb->ccb_h.status = CAM_REQ_CMP_ERR;			ccb->cgdl.status = CAM_GDEVLIST_ERROR;			*ccb->cgdl.periph_name = '\0';			ccb->cgdl.unit_number = 0;			error = ENOENT;			/*			 * It is unfortunate that this is even necessary,			 * but there are many, many clueless users out there.			 * If this is true, the user is looking for the			 * passthrough driver, but doesn't have one in his			 * kernel.			 */			if (base_periph_found == 1) {				printf("xptioctl: pass driver is not in the "				       "kernel\n");				printf("xptioctl: put \"device pass0\" in "				       "your kernel config file\n");			}		}		splx(s);		break;		}	default:		error = ENOTTY;		break;	}	return(error);}/* Functions accessed by the peripheral drivers */static voidxpt_init(dummy)	void *dummy;{	struct cam_sim *xpt_sim;	struct cam_path *path;	struct cam_devq;	cam_status status;	TAILQ_INIT(&xpt_busses);	TAILQ_INIT(&cam_bioq);	TAILQ_INIT(&cam_netq);	SLIST_INIT(&ccb_freeq);	STAILQ_INIT(&highpowerq);	/*	 * The xpt layer is, itself, the equivelent of a SIM.	 * Allow 16 ccbs in the ccb pool for it.  This should	 * give decent parallelism when we probe busses and	 * perform other XPT functions.	 */	xpt_sim = (struct cam_sim *)malloc(sizeof(*xpt_sim),					   M_DEVBUF, M_WAITOK);	xpt_sim->sim_action = xptaction;	xpt_sim->sim_name = "xpt";	xpt_sim->path_id = CAM_XPT_PATH_ID;	xpt_sim->bus_id = 0;	xpt_sim->max_tagged_dev_openings = 0;	xpt_sim->max_dev_openings = 0;	xpt_sim->devq = cam_simq_alloc(16);	xpt_max_ccbs = 16;	xpt_bus_register(xpt_sim, 0);	/*	 * Looking at the XPT from the SIM layer, the XPT is	 * the equivelent of a peripheral driver.  Allocate	 * a peripheral driver entry for us.	 */	if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,				      CAM_TARGET_WILDCARD,				      CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {		printf("xpt_init: xpt_create_path failed with status %#x,"		       " failing attach\n", status);		return;	}	cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,			 path, NULL, 0, NULL);	xpt_free_path(path);	xpt_sim->softc = xpt_periph;	/*	 * Register a callback for when interrupts are enabled.	 */	xpt_config_hook =	    (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),					      M_TEMP, M_NOWAIT);	if (xpt_config_hook == NULL) {		printf("xpt_init: Cannot malloc config hook "		       "- failing attach\n");		return;	}	bzero(xpt_config_hook, sizeof(*xpt_config_hook));	xpt_config_hook->ich_func = xpt_config;	if (config_intrhook_establish(xpt_config_hook) != 0) {		free (xpt_config_hook, M_TEMP);		printf("xpt_init: config_intrhook_establish failed "		       "- failing attach\n");	}	/* Install our software interrupt handlers */	register_swi(SWI_CAMNET, swi_camnet);	register_swi(SWI_CAMBIO, swi_cambio);}static cam_statusxptregister(struct cam_periph *periph, void *arg){	if (periph == NULL) {		printf("xptregister: periph was NULL!!\n");		return(CAM_REQ_CMP_ERR);	}	periph->softc = NULL;	xpt_periph = periph;	return(CAM_REQ_CMP);}int32_txpt_add_periph(struct cam_periph *periph){	struct cam_ed *device;	int32_t	 status;	struct periph_list *periph_head;	device = periph->path->device;	periph_head = &device->periphs;	status = CAM_REQ_CMP;	if (device != NULL) {		int s;		/*		 * Make room for this peripheral		 * so it will fit in the queue		 * when it's scheduled to run		 */		s = splsoftcam();		status = camq_resize(&device->drvq,				     device->drvq.array_size + 1);		device->generation++;		SLIST_INSERT_HEAD(periph_head, periph, periph_links);		splx(s);	}	xsoftc.generation++;	return (status);}voidxpt_remove_periph(struct cam_periph *periph){	struct cam_ed *device;	device = periph->path->device;

⌨️ 快捷键说明

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