xref: /freebsd/sys/cam/scsi/scsi_pass.c (revision 8008a935a77868d1b703a4aec2e8d7d4d33144d2)
1898b0535SWarner Losh /*-
23393f8daSKenneth D. Merry  * Copyright (c) 1997, 1998, 2000 Justin T. Gibbs.
32a888f93SKenneth D. Merry  * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
476babe50SJustin T. Gibbs  * All rights reserved.
576babe50SJustin T. Gibbs  *
676babe50SJustin T. Gibbs  * Redistribution and use in source and binary forms, with or without
776babe50SJustin T. Gibbs  * modification, are permitted provided that the following conditions
876babe50SJustin T. Gibbs  * are met:
976babe50SJustin T. Gibbs  * 1. Redistributions of source code must retain the above copyright
1076babe50SJustin T. Gibbs  *    notice, this list of conditions, and the following disclaimer,
1176babe50SJustin T. Gibbs  *    without modification, immediately at the beginning of the file.
1276babe50SJustin T. Gibbs  * 2. The name of the author may not be used to endorse or promote products
1376babe50SJustin T. Gibbs  *    derived from this software without specific prior written permission.
1476babe50SJustin T. Gibbs  *
1576babe50SJustin T. Gibbs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1676babe50SJustin T. Gibbs  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1776babe50SJustin T. Gibbs  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1876babe50SJustin T. Gibbs  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
1976babe50SJustin T. Gibbs  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2076babe50SJustin T. Gibbs  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2176babe50SJustin T. Gibbs  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2276babe50SJustin T. Gibbs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2376babe50SJustin T. Gibbs  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2476babe50SJustin T. Gibbs  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2576babe50SJustin T. Gibbs  * SUCH DAMAGE.
2676babe50SJustin T. Gibbs  */
2776babe50SJustin T. Gibbs 
28ee709e70SDavid E. O'Brien #include <sys/cdefs.h>
29ee709e70SDavid E. O'Brien __FBSDID("$FreeBSD$");
30ee709e70SDavid E. O'Brien 
3176babe50SJustin T. Gibbs #include <sys/param.h>
3276babe50SJustin T. Gibbs #include <sys/systm.h>
3376babe50SJustin T. Gibbs #include <sys/kernel.h>
3476babe50SJustin T. Gibbs #include <sys/types.h>
359626b608SPoul-Henning Kamp #include <sys/bio.h>
3676babe50SJustin T. Gibbs #include <sys/malloc.h>
3776babe50SJustin T. Gibbs #include <sys/fcntl.h>
3876babe50SJustin T. Gibbs #include <sys/conf.h>
3976babe50SJustin T. Gibbs #include <sys/errno.h>
4076babe50SJustin T. Gibbs #include <sys/devicestat.h>
41f7312ca2SRobert Watson #include <sys/proc.h>
4276babe50SJustin T. Gibbs 
4376babe50SJustin T. Gibbs #include <cam/cam.h>
4476babe50SJustin T. Gibbs #include <cam/cam_ccb.h>
4576babe50SJustin T. Gibbs #include <cam/cam_periph.h>
463393f8daSKenneth D. Merry #include <cam/cam_queue.h>
4776babe50SJustin T. Gibbs #include <cam/cam_xpt_periph.h>
4876babe50SJustin T. Gibbs #include <cam/cam_debug.h>
492b83592fSScott Long #include <cam/cam_sim.h>
5076babe50SJustin T. Gibbs 
5176babe50SJustin T. Gibbs #include <cam/scsi/scsi_all.h>
5276babe50SJustin T. Gibbs #include <cam/scsi/scsi_pass.h>
5376babe50SJustin T. Gibbs 
5476babe50SJustin T. Gibbs typedef enum {
5576babe50SJustin T. Gibbs 	PASS_FLAG_OPEN			= 0x01,
5676babe50SJustin T. Gibbs 	PASS_FLAG_LOCKED		= 0x02,
5776babe50SJustin T. Gibbs 	PASS_FLAG_INVALID		= 0x04
5876babe50SJustin T. Gibbs } pass_flags;
5976babe50SJustin T. Gibbs 
6076babe50SJustin T. Gibbs typedef enum {
6176babe50SJustin T. Gibbs 	PASS_STATE_NORMAL
6276babe50SJustin T. Gibbs } pass_state;
6376babe50SJustin T. Gibbs 
6476babe50SJustin T. Gibbs typedef enum {
6576babe50SJustin T. Gibbs 	PASS_CCB_BUFFER_IO,
6676babe50SJustin T. Gibbs 	PASS_CCB_WAITING
6776babe50SJustin T. Gibbs } pass_ccb_types;
6876babe50SJustin T. Gibbs 
6976babe50SJustin T. Gibbs #define ccb_type	ppriv_field0
7076babe50SJustin T. Gibbs #define ccb_bp		ppriv_ptr1
7176babe50SJustin T. Gibbs 
7276babe50SJustin T. Gibbs struct pass_softc {
7376babe50SJustin T. Gibbs 	pass_state		state;
7476babe50SJustin T. Gibbs 	pass_flags		flags;
7576babe50SJustin T. Gibbs 	u_int8_t		pd_type;
7676babe50SJustin T. Gibbs 	union ccb		saved_ccb;
77a9d2245eSPoul-Henning Kamp 	struct devstat		*device_stats;
7889c9c53dSPoul-Henning Kamp 	struct cdev *dev;
7976babe50SJustin T. Gibbs };
8076babe50SJustin T. Gibbs 
8176babe50SJustin T. Gibbs 
8276babe50SJustin T. Gibbs static	d_open_t	passopen;
8376babe50SJustin T. Gibbs static	d_close_t	passclose;
8476babe50SJustin T. Gibbs static	d_ioctl_t	passioctl;
8576babe50SJustin T. Gibbs 
8676babe50SJustin T. Gibbs static	periph_init_t	passinit;
8776babe50SJustin T. Gibbs static	periph_ctor_t	passregister;
88ee9c90c7SKenneth D. Merry static	periph_oninv_t	passoninvalidate;
8976babe50SJustin T. Gibbs static	periph_dtor_t	passcleanup;
9076babe50SJustin T. Gibbs static	periph_start_t	passstart;
9176babe50SJustin T. Gibbs static	void		passasync(void *callback_arg, u_int32_t code,
9276babe50SJustin T. Gibbs 				  struct cam_path *path, void *arg);
9376babe50SJustin T. Gibbs static	void		passdone(struct cam_periph *periph,
9476babe50SJustin T. Gibbs 				 union ccb *done_ccb);
9576babe50SJustin T. Gibbs static	int		passerror(union ccb *ccb, u_int32_t cam_flags,
9676babe50SJustin T. Gibbs 				  u_int32_t sense_flags);
9776babe50SJustin T. Gibbs static 	int		passsendccb(struct cam_periph *periph, union ccb *ccb,
9876babe50SJustin T. Gibbs 				    union ccb *inccb);
9976babe50SJustin T. Gibbs 
10076babe50SJustin T. Gibbs static struct periph_driver passdriver =
10176babe50SJustin T. Gibbs {
10276babe50SJustin T. Gibbs 	passinit, "pass",
10376babe50SJustin T. Gibbs 	TAILQ_HEAD_INITIALIZER(passdriver.units), /* generation */ 0
10476babe50SJustin T. Gibbs };
10576babe50SJustin T. Gibbs 
1060b7c27b9SPeter Wemm PERIPHDRIVER_DECLARE(pass, passdriver);
10776babe50SJustin T. Gibbs 
1084e2f199eSPoul-Henning Kamp static struct cdevsw pass_cdevsw = {
109dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
1102b83592fSScott Long 	.d_flags =	0,
1117ac40f5fSPoul-Henning Kamp 	.d_open =	passopen,
1127ac40f5fSPoul-Henning Kamp 	.d_close =	passclose,
1137ac40f5fSPoul-Henning Kamp 	.d_ioctl =	passioctl,
1147ac40f5fSPoul-Henning Kamp 	.d_name =	"pass",
11576babe50SJustin T. Gibbs };
11676babe50SJustin T. Gibbs 
11776babe50SJustin T. Gibbs static void
11876babe50SJustin T. Gibbs passinit(void)
11976babe50SJustin T. Gibbs {
12076babe50SJustin T. Gibbs 	cam_status status;
12176babe50SJustin T. Gibbs 	struct cam_path *path;
12276babe50SJustin T. Gibbs 
12376babe50SJustin T. Gibbs 	/*
12476babe50SJustin T. Gibbs 	 * Install a global async callback.  This callback will
12576babe50SJustin T. Gibbs 	 * receive async callbacks like "new device found".
12676babe50SJustin T. Gibbs 	 */
12776babe50SJustin T. Gibbs 	status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
12876babe50SJustin T. Gibbs 				 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
12976babe50SJustin T. Gibbs 
13076babe50SJustin T. Gibbs 	if (status == CAM_REQ_CMP) {
13176babe50SJustin T. Gibbs 		struct ccb_setasync csa;
13276babe50SJustin T. Gibbs 
13376babe50SJustin T. Gibbs                 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
13476babe50SJustin T. Gibbs                 csa.ccb_h.func_code = XPT_SASYNC_CB;
13576babe50SJustin T. Gibbs                 csa.event_enable = AC_FOUND_DEVICE;
13676babe50SJustin T. Gibbs                 csa.callback = passasync;
13776babe50SJustin T. Gibbs                 csa.callback_arg = NULL;
13876babe50SJustin T. Gibbs                 xpt_action((union ccb *)&csa);
13976babe50SJustin T. Gibbs 		status = csa.ccb_h.status;
14076babe50SJustin T. Gibbs                 xpt_free_path(path);
14176babe50SJustin T. Gibbs         }
14276babe50SJustin T. Gibbs 
14376babe50SJustin T. Gibbs 	if (status != CAM_REQ_CMP) {
14476babe50SJustin T. Gibbs 		printf("pass: Failed to attach master async callback "
14576babe50SJustin T. Gibbs 		       "due to status 0x%x!\n", status);
14676babe50SJustin T. Gibbs 	}
14776babe50SJustin T. Gibbs 
14876babe50SJustin T. Gibbs }
14976babe50SJustin T. Gibbs 
15076babe50SJustin T. Gibbs static void
151ee9c90c7SKenneth D. Merry passoninvalidate(struct cam_periph *periph)
152ee9c90c7SKenneth D. Merry {
153ee9c90c7SKenneth D. Merry 	struct pass_softc *softc;
154ee9c90c7SKenneth D. Merry 	struct ccb_setasync csa;
155ee9c90c7SKenneth D. Merry 
156ee9c90c7SKenneth D. Merry 	softc = (struct pass_softc *)periph->softc;
157ee9c90c7SKenneth D. Merry 
158ee9c90c7SKenneth D. Merry 	/*
159ee9c90c7SKenneth D. Merry 	 * De-register any async callbacks.
160ee9c90c7SKenneth D. Merry 	 */
161ee9c90c7SKenneth D. Merry 	xpt_setup_ccb(&csa.ccb_h, periph->path,
162ee9c90c7SKenneth D. Merry 		      /* priority */ 5);
163ee9c90c7SKenneth D. Merry 	csa.ccb_h.func_code = XPT_SASYNC_CB;
164ee9c90c7SKenneth D. Merry 	csa.event_enable = 0;
165ee9c90c7SKenneth D. Merry 	csa.callback = passasync;
166ee9c90c7SKenneth D. Merry 	csa.callback_arg = periph;
167ee9c90c7SKenneth D. Merry 	xpt_action((union ccb *)&csa);
168ee9c90c7SKenneth D. Merry 
169ee9c90c7SKenneth D. Merry 	softc->flags |= PASS_FLAG_INVALID;
170ee9c90c7SKenneth D. Merry 
171ee9c90c7SKenneth D. Merry 	/*
1723393f8daSKenneth D. Merry 	 * XXX Return all queued I/O with ENXIO.
173ee9c90c7SKenneth D. Merry 	 * XXX Handle any transactions queued to the card
174ee9c90c7SKenneth D. Merry 	 *     with XPT_ABORT_CCB.
175ee9c90c7SKenneth D. Merry 	 */
176ee9c90c7SKenneth D. Merry 
177ee9c90c7SKenneth D. Merry 	if (bootverbose) {
178f0d9af51SMatt Jacob 		xpt_print(periph->path, "lost device\n");
179ee9c90c7SKenneth D. Merry 	}
180ee9c90c7SKenneth D. Merry 
181ee9c90c7SKenneth D. Merry }
182ee9c90c7SKenneth D. Merry 
183ee9c90c7SKenneth D. Merry static void
18476babe50SJustin T. Gibbs passcleanup(struct cam_periph *periph)
18576babe50SJustin T. Gibbs {
186ee9c90c7SKenneth D. Merry 	struct pass_softc *softc;
187ee9c90c7SKenneth D. Merry 
188ee9c90c7SKenneth D. Merry 	softc = (struct pass_softc *)periph->softc;
189ee9c90c7SKenneth D. Merry 
190a9d2245eSPoul-Henning Kamp 	devstat_remove_entry(softc->device_stats);
191ee9c90c7SKenneth D. Merry 
19273d26919SKenneth D. Merry 	destroy_dev(softc->dev);
19373d26919SKenneth D. Merry 
19476babe50SJustin T. Gibbs 	if (bootverbose) {
195f0d9af51SMatt Jacob 		xpt_print(periph->path, "removing device entry\n");
19676babe50SJustin T. Gibbs 	}
197ee9c90c7SKenneth D. Merry 	free(softc, M_DEVBUF);
19876babe50SJustin T. Gibbs }
19976babe50SJustin T. Gibbs 
20076babe50SJustin T. Gibbs static void
20176babe50SJustin T. Gibbs passasync(void *callback_arg, u_int32_t code,
20276babe50SJustin T. Gibbs 	  struct cam_path *path, void *arg)
20376babe50SJustin T. Gibbs {
20476babe50SJustin T. Gibbs 	struct cam_periph *periph;
2052b83592fSScott Long 	struct cam_sim *sim;
20676babe50SJustin T. Gibbs 
20776babe50SJustin T. Gibbs 	periph = (struct cam_periph *)callback_arg;
20876babe50SJustin T. Gibbs 
20976babe50SJustin T. Gibbs 	switch (code) {
21076babe50SJustin T. Gibbs 	case AC_FOUND_DEVICE:
21176babe50SJustin T. Gibbs 	{
21276babe50SJustin T. Gibbs 		struct ccb_getdev *cgd;
21376babe50SJustin T. Gibbs 		cam_status status;
21476babe50SJustin T. Gibbs 
21576babe50SJustin T. Gibbs 		cgd = (struct ccb_getdev *)arg;
216c5ff3b2fSMatt Jacob 		if (cgd == NULL)
217c5ff3b2fSMatt Jacob 			break;
21876babe50SJustin T. Gibbs 
21976babe50SJustin T. Gibbs 		/*
22076babe50SJustin T. Gibbs 		 * Allocate a peripheral instance for
22176babe50SJustin T. Gibbs 		 * this device and start the probe
22276babe50SJustin T. Gibbs 		 * process.
22376babe50SJustin T. Gibbs 		 */
2242b83592fSScott Long 		sim = xpt_path_sim(cgd->ccb_h.path);
225ee9c90c7SKenneth D. Merry 		status = cam_periph_alloc(passregister, passoninvalidate,
226ee9c90c7SKenneth D. Merry 					  passcleanup, passstart, "pass",
227ee9c90c7SKenneth D. Merry 					  CAM_PERIPH_BIO, cgd->ccb_h.path,
228ee9c90c7SKenneth D. Merry 					  passasync, AC_FOUND_DEVICE, cgd);
22976babe50SJustin T. Gibbs 
23076babe50SJustin T. Gibbs 		if (status != CAM_REQ_CMP
2313393f8daSKenneth D. Merry 		 && status != CAM_REQ_INPROG) {
2323393f8daSKenneth D. Merry 			const struct cam_status_entry *entry;
2333393f8daSKenneth D. Merry 
2343393f8daSKenneth D. Merry 			entry = cam_fetch_status_entry(status);
2353393f8daSKenneth D. Merry 
23676babe50SJustin T. Gibbs 			printf("passasync: Unable to attach new device "
2373393f8daSKenneth D. Merry 			       "due to status %#x: %s\n", status, entry ?
2383393f8daSKenneth D. Merry 			       entry->status_text : "Unknown");
2393393f8daSKenneth D. Merry 		}
24076babe50SJustin T. Gibbs 
24176babe50SJustin T. Gibbs 		break;
24276babe50SJustin T. Gibbs 	}
24376babe50SJustin T. Gibbs 	default:
244516871c6SJustin T. Gibbs 		cam_periph_async(periph, code, path, arg);
24576babe50SJustin T. Gibbs 		break;
24676babe50SJustin T. Gibbs 	}
24776babe50SJustin T. Gibbs }
24876babe50SJustin T. Gibbs 
24976babe50SJustin T. Gibbs static cam_status
25076babe50SJustin T. Gibbs passregister(struct cam_periph *periph, void *arg)
25176babe50SJustin T. Gibbs {
25276babe50SJustin T. Gibbs 	struct pass_softc *softc;
25376babe50SJustin T. Gibbs 	struct ccb_setasync csa;
25476babe50SJustin T. Gibbs 	struct ccb_getdev *cgd;
2553393f8daSKenneth D. Merry 	int    no_tags;
25676babe50SJustin T. Gibbs 
25776babe50SJustin T. Gibbs 	cgd = (struct ccb_getdev *)arg;
25876babe50SJustin T. Gibbs 	if (periph == NULL) {
25976babe50SJustin T. Gibbs 		printf("passregister: periph was NULL!!\n");
26076babe50SJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
26176babe50SJustin T. Gibbs 	}
26276babe50SJustin T. Gibbs 
26376babe50SJustin T. Gibbs 	if (cgd == NULL) {
26476babe50SJustin T. Gibbs 		printf("passregister: no getdev CCB, can't register device\n");
26576babe50SJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
26676babe50SJustin T. Gibbs 	}
26776babe50SJustin T. Gibbs 
26876babe50SJustin T. Gibbs 	softc = (struct pass_softc *)malloc(sizeof(*softc),
26976babe50SJustin T. Gibbs 					    M_DEVBUF, M_NOWAIT);
27076babe50SJustin T. Gibbs 
27176babe50SJustin T. Gibbs 	if (softc == NULL) {
27276babe50SJustin T. Gibbs 		printf("passregister: Unable to probe new device. "
27376babe50SJustin T. Gibbs 		       "Unable to allocate softc\n");
27476babe50SJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
27576babe50SJustin T. Gibbs 	}
27676babe50SJustin T. Gibbs 
27776babe50SJustin T. Gibbs 	bzero(softc, sizeof(*softc));
27876babe50SJustin T. Gibbs 	softc->state = PASS_STATE_NORMAL;
27910b6172aSMatt Jacob 	softc->pd_type = SID_TYPE(&cgd->inq_data);
28076babe50SJustin T. Gibbs 
28176babe50SJustin T. Gibbs 	periph->softc = softc;
2823393f8daSKenneth D. Merry 
28376babe50SJustin T. Gibbs 	/*
28476babe50SJustin T. Gibbs 	 * We pass in 0 for a blocksize, since we don't
28576babe50SJustin T. Gibbs 	 * know what the blocksize of this device is, if
28676babe50SJustin T. Gibbs 	 * it even has a blocksize.
28776babe50SJustin T. Gibbs 	 */
2883393f8daSKenneth D. Merry 	no_tags = (cgd->inq_data.flags & SID_CmdQue) == 0;
289c81d2c74SMatt Jacob 	softc->device_stats = devstat_new_entry("pass",
290c81d2c74SMatt Jacob 			  unit2minor(periph->unit_number), 0,
2913393f8daSKenneth D. Merry 			  DEVSTAT_NO_BLOCKSIZE
2923393f8daSKenneth D. Merry 			  | (no_tags ? DEVSTAT_NO_ORDERED_TAGS : 0),
29310b6172aSMatt Jacob 			  softc->pd_type |
29476babe50SJustin T. Gibbs 			  DEVSTAT_TYPE_IF_SCSI |
2952a888f93SKenneth D. Merry 			  DEVSTAT_TYPE_PASS,
2962a888f93SKenneth D. Merry 			  DEVSTAT_PRIORITY_PASS);
29773d26919SKenneth D. Merry 
29873d26919SKenneth D. Merry 	/* Register the device */
2992b83592fSScott Long 	mtx_unlock(periph->sim->mtx);
300c81d2c74SMatt Jacob 	softc->dev = make_dev(&pass_cdevsw, unit2minor(periph->unit_number),
301c81d2c74SMatt Jacob 			      UID_ROOT, GID_OPERATOR, 0600, "%s%d",
302c81d2c74SMatt Jacob 			      periph->periph_name, periph->unit_number);
3032b83592fSScott Long 	mtx_lock(periph->sim->mtx);
304e2a5fdf9SNate Lawson 	softc->dev->si_drv1 = periph;
30573d26919SKenneth D. Merry 
30676babe50SJustin T. Gibbs 	/*
30776babe50SJustin T. Gibbs 	 * Add an async callback so that we get
30876babe50SJustin T. Gibbs 	 * notified if this device goes away.
30976babe50SJustin T. Gibbs 	 */
31076babe50SJustin T. Gibbs 	xpt_setup_ccb(&csa.ccb_h, periph->path, /* priority */ 5);
31176babe50SJustin T. Gibbs 	csa.ccb_h.func_code = XPT_SASYNC_CB;
31276babe50SJustin T. Gibbs 	csa.event_enable = AC_LOST_DEVICE;
31376babe50SJustin T. Gibbs 	csa.callback = passasync;
31476babe50SJustin T. Gibbs 	csa.callback_arg = periph;
31576babe50SJustin T. Gibbs 	xpt_action((union ccb *)&csa);
31676babe50SJustin T. Gibbs 
31776babe50SJustin T. Gibbs 	if (bootverbose)
31876babe50SJustin T. Gibbs 		xpt_announce_periph(periph, NULL);
31976babe50SJustin T. Gibbs 
32076babe50SJustin T. Gibbs 	return(CAM_REQ_CMP);
32176babe50SJustin T. Gibbs }
32276babe50SJustin T. Gibbs 
32376babe50SJustin T. Gibbs static int
32489c9c53dSPoul-Henning Kamp passopen(struct cdev *dev, int flags, int fmt, struct thread *td)
32576babe50SJustin T. Gibbs {
32676babe50SJustin T. Gibbs 	struct cam_periph *periph;
32776babe50SJustin T. Gibbs 	struct pass_softc *softc;
328e2a5fdf9SNate Lawson 	int error;
32976babe50SJustin T. Gibbs 
33076babe50SJustin T. Gibbs 	error = 0; /* default to no error */
33176babe50SJustin T. Gibbs 
332e2a5fdf9SNate Lawson 	periph = (struct cam_periph *)dev->si_drv1;
3332b83592fSScott Long 	if (cam_periph_acquire(periph) != CAM_REQ_CMP)
33476babe50SJustin T. Gibbs 		return (ENXIO);
33576babe50SJustin T. Gibbs 
3362b83592fSScott Long 	cam_periph_lock(periph);
3372b83592fSScott Long 
33876babe50SJustin T. Gibbs 	softc = (struct pass_softc *)periph->softc;
33976babe50SJustin T. Gibbs 
340ee9c90c7SKenneth D. Merry 	if (softc->flags & PASS_FLAG_INVALID) {
3412b83592fSScott Long 		cam_periph_unlock(periph);
3422b83592fSScott Long 		cam_periph_release(periph);
34376babe50SJustin T. Gibbs 		return(ENXIO);
344ee9c90c7SKenneth D. Merry 	}
34522b9c86cSKenneth D. Merry 
34622b9c86cSKenneth D. Merry 	/*
347f5ef42beSRobert Watson 	 * Don't allow access when we're running at a high securelevel.
34822b9c86cSKenneth D. Merry 	 */
349a854ed98SJohn Baldwin 	error = securelevel_gt(td->td_ucred, 1);
350f7312ca2SRobert Watson 	if (error) {
3512b83592fSScott Long 		cam_periph_unlock(periph);
3522b83592fSScott Long 		cam_periph_release(periph);
353f7312ca2SRobert Watson 		return(error);
35422b9c86cSKenneth D. Merry 	}
35576babe50SJustin T. Gibbs 
35676babe50SJustin T. Gibbs 	/*
35766a0780eSKenneth D. Merry 	 * Only allow read-write access.
35866a0780eSKenneth D. Merry 	 */
35922b9c86cSKenneth D. Merry 	if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0)) {
3602b83592fSScott Long 		cam_periph_unlock(periph);
3612b83592fSScott Long 		cam_periph_release(periph);
36266a0780eSKenneth D. Merry 		return(EPERM);
36322b9c86cSKenneth D. Merry 	}
36466a0780eSKenneth D. Merry 
36566a0780eSKenneth D. Merry 	/*
36676babe50SJustin T. Gibbs 	 * We don't allow nonblocking access.
36776babe50SJustin T. Gibbs 	 */
36876babe50SJustin T. Gibbs 	if ((flags & O_NONBLOCK) != 0) {
369f0d9af51SMatt Jacob 		xpt_print(periph->path, "can't do nonblocking access\n");
3702b83592fSScott Long 		cam_periph_unlock(periph);
3712b83592fSScott Long 		cam_periph_release(periph);
37222b9c86cSKenneth D. Merry 		return(EINVAL);
37376babe50SJustin T. Gibbs 	}
37476babe50SJustin T. Gibbs 
37576babe50SJustin T. Gibbs 	if ((softc->flags & PASS_FLAG_OPEN) == 0) {
37676babe50SJustin T. Gibbs 		softc->flags |= PASS_FLAG_OPEN;
3772b83592fSScott Long 	} else {
3782b83592fSScott Long 		/* Device closes aren't symmertical, so fix up the refcount */
3792b83592fSScott Long 		cam_periph_release(periph);
38076babe50SJustin T. Gibbs 	}
38176babe50SJustin T. Gibbs 
38276babe50SJustin T. Gibbs 	cam_periph_unlock(periph);
38376babe50SJustin T. Gibbs 
38476babe50SJustin T. Gibbs 	return (error);
38576babe50SJustin T. Gibbs }
38676babe50SJustin T. Gibbs 
38776babe50SJustin T. Gibbs static int
38889c9c53dSPoul-Henning Kamp passclose(struct cdev *dev, int flag, int fmt, struct thread *td)
38976babe50SJustin T. Gibbs {
39076babe50SJustin T. Gibbs 	struct 	cam_periph *periph;
39176babe50SJustin T. Gibbs 	struct	pass_softc *softc;
39276babe50SJustin T. Gibbs 
393e2a5fdf9SNate Lawson 	periph = (struct cam_periph *)dev->si_drv1;
39476babe50SJustin T. Gibbs 	if (periph == NULL)
39576babe50SJustin T. Gibbs 		return (ENXIO);
39676babe50SJustin T. Gibbs 
3972b83592fSScott Long 	cam_periph_lock(periph);
3982b83592fSScott Long 
39976babe50SJustin T. Gibbs 	softc = (struct pass_softc *)periph->softc;
40076babe50SJustin T. Gibbs 	softc->flags &= ~PASS_FLAG_OPEN;
40176babe50SJustin T. Gibbs 
40276babe50SJustin T. Gibbs 	cam_periph_unlock(periph);
40376babe50SJustin T. Gibbs 	cam_periph_release(periph);
40476babe50SJustin T. Gibbs 
40576babe50SJustin T. Gibbs 	return (0);
40676babe50SJustin T. Gibbs }
40776babe50SJustin T. Gibbs 
40876babe50SJustin T. Gibbs static void
40976babe50SJustin T. Gibbs passstart(struct cam_periph *periph, union ccb *start_ccb)
41076babe50SJustin T. Gibbs {
41176babe50SJustin T. Gibbs 	struct pass_softc *softc;
41276babe50SJustin T. Gibbs 
41376babe50SJustin T. Gibbs 	softc = (struct pass_softc *)periph->softc;
41476babe50SJustin T. Gibbs 
41576babe50SJustin T. Gibbs 	switch (softc->state) {
41676babe50SJustin T. Gibbs 	case PASS_STATE_NORMAL:
41776babe50SJustin T. Gibbs 		start_ccb->ccb_h.ccb_type = PASS_CCB_WAITING;
41876babe50SJustin T. Gibbs 		SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
41976babe50SJustin T. Gibbs 				  periph_links.sle);
42076babe50SJustin T. Gibbs 		periph->immediate_priority = CAM_PRIORITY_NONE;
42176babe50SJustin T. Gibbs 		wakeup(&periph->ccb_list);
42276babe50SJustin T. Gibbs 		break;
42376babe50SJustin T. Gibbs 	}
42476babe50SJustin T. Gibbs }
4253393f8daSKenneth D. Merry 
42676babe50SJustin T. Gibbs static void
42776babe50SJustin T. Gibbs passdone(struct cam_periph *periph, union ccb *done_ccb)
42876babe50SJustin T. Gibbs {
42976babe50SJustin T. Gibbs 	struct pass_softc *softc;
43076babe50SJustin T. Gibbs 	struct ccb_scsiio *csio;
43176babe50SJustin T. Gibbs 
43276babe50SJustin T. Gibbs 	softc = (struct pass_softc *)periph->softc;
43376babe50SJustin T. Gibbs 	csio = &done_ccb->csio;
43476babe50SJustin T. Gibbs 	switch (csio->ccb_h.ccb_type) {
43576babe50SJustin T. Gibbs 	case PASS_CCB_WAITING:
43676babe50SJustin T. Gibbs 		/* Caller will release the CCB */
43776babe50SJustin T. Gibbs 		wakeup(&done_ccb->ccb_h.cbfcnp);
43876babe50SJustin T. Gibbs 		return;
43976babe50SJustin T. Gibbs 	}
44076babe50SJustin T. Gibbs 	xpt_release_ccb(done_ccb);
44176babe50SJustin T. Gibbs }
44276babe50SJustin T. Gibbs 
44376babe50SJustin T. Gibbs static int
44489c9c53dSPoul-Henning Kamp passioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
44576babe50SJustin T. Gibbs {
44676babe50SJustin T. Gibbs 	struct	cam_periph *periph;
44776babe50SJustin T. Gibbs 	struct	pass_softc *softc;
44876babe50SJustin T. Gibbs 	int	error;
44976babe50SJustin T. Gibbs 
450e2a5fdf9SNate Lawson 	periph = (struct cam_periph *)dev->si_drv1;
45176babe50SJustin T. Gibbs 	if (periph == NULL)
45276babe50SJustin T. Gibbs 		return(ENXIO);
45376babe50SJustin T. Gibbs 
4542b83592fSScott Long 	cam_periph_lock(periph);
45576babe50SJustin T. Gibbs 	softc = (struct pass_softc *)periph->softc;
45676babe50SJustin T. Gibbs 
45776babe50SJustin T. Gibbs 	error = 0;
45876babe50SJustin T. Gibbs 
45976babe50SJustin T. Gibbs 	switch (cmd) {
46076babe50SJustin T. Gibbs 
46176babe50SJustin T. Gibbs 	case CAMIOCOMMAND:
46276babe50SJustin T. Gibbs 	{
46376babe50SJustin T. Gibbs 		union ccb *inccb;
46476babe50SJustin T. Gibbs 		union ccb *ccb;
4659deea857SKenneth D. Merry 		int ccb_malloced;
46676babe50SJustin T. Gibbs 
46776babe50SJustin T. Gibbs 		inccb = (union ccb *)addr;
4689deea857SKenneth D. Merry 
4699deea857SKenneth D. Merry 		/*
4709deea857SKenneth D. Merry 		 * Some CCB types, like scan bus and scan lun can only go
4719deea857SKenneth D. Merry 		 * through the transport layer device.
4729deea857SKenneth D. Merry 		 */
4739deea857SKenneth D. Merry 		if (inccb->ccb_h.func_code & XPT_FC_XPT_ONLY) {
474f0d9af51SMatt Jacob 			xpt_print(periph->path, "CCB function code %#x is "
475f0d9af51SMatt Jacob 			    "restricted to the XPT device\n",
476f0d9af51SMatt Jacob 			    inccb->ccb_h.func_code);
4779deea857SKenneth D. Merry 			error = ENODEV;
4789deea857SKenneth D. Merry 			break;
4799deea857SKenneth D. Merry 		}
4809deea857SKenneth D. Merry 
4819deea857SKenneth D. Merry 		/*
4829deea857SKenneth D. Merry 		 * Non-immediate CCBs need a CCB from the per-device pool
4839deea857SKenneth D. Merry 		 * of CCBs, which is scheduled by the transport layer.
4849deea857SKenneth D. Merry 		 * Immediate CCBs and user-supplied CCBs should just be
4859deea857SKenneth D. Merry 		 * malloced.
4869deea857SKenneth D. Merry 		 */
4879deea857SKenneth D. Merry 		if ((inccb->ccb_h.func_code & XPT_FC_QUEUED)
4889deea857SKenneth D. Merry 		 && ((inccb->ccb_h.func_code & XPT_FC_USER_CCB) == 0)) {
4899deea857SKenneth D. Merry 			ccb = cam_periph_getccb(periph,
4909deea857SKenneth D. Merry 						inccb->ccb_h.pinfo.priority);
4919deea857SKenneth D. Merry 			ccb_malloced = 0;
4929deea857SKenneth D. Merry 		} else {
4938008a935SScott Long 			ccb = xpt_alloc_ccb_nowait();
4949deea857SKenneth D. Merry 
4959deea857SKenneth D. Merry 			if (ccb != NULL)
4969deea857SKenneth D. Merry 				xpt_setup_ccb(&ccb->ccb_h, periph->path,
4979deea857SKenneth D. Merry 					      inccb->ccb_h.pinfo.priority);
4989deea857SKenneth D. Merry 			ccb_malloced = 1;
4999deea857SKenneth D. Merry 		}
5009deea857SKenneth D. Merry 
5019deea857SKenneth D. Merry 		if (ccb == NULL) {
502f0d9af51SMatt Jacob 			xpt_print(periph->path, "unable to allocate CCB\n");
5039deea857SKenneth D. Merry 			error = ENOMEM;
5049deea857SKenneth D. Merry 			break;
5059deea857SKenneth D. Merry 		}
50676babe50SJustin T. Gibbs 
50776babe50SJustin T. Gibbs 		error = passsendccb(periph, ccb, inccb);
50876babe50SJustin T. Gibbs 
5099deea857SKenneth D. Merry 		if (ccb_malloced)
5109deea857SKenneth D. Merry 			xpt_free_ccb(ccb);
5119deea857SKenneth D. Merry 		else
51276babe50SJustin T. Gibbs 			xpt_release_ccb(ccb);
51376babe50SJustin T. Gibbs 
51476babe50SJustin T. Gibbs 		break;
51576babe50SJustin T. Gibbs 	}
51676babe50SJustin T. Gibbs 	default:
51776babe50SJustin T. Gibbs 		error = cam_periph_ioctl(periph, cmd, addr, passerror);
51876babe50SJustin T. Gibbs 		break;
51976babe50SJustin T. Gibbs 	}
52076babe50SJustin T. Gibbs 
5212b83592fSScott Long 	cam_periph_unlock(periph);
52276babe50SJustin T. Gibbs 	return(error);
52376babe50SJustin T. Gibbs }
52476babe50SJustin T. Gibbs 
52576babe50SJustin T. Gibbs /*
52676babe50SJustin T. Gibbs  * Generally, "ccb" should be the CCB supplied by the kernel.  "inccb"
52776babe50SJustin T. Gibbs  * should be the CCB that is copied in from the user.
52876babe50SJustin T. Gibbs  */
52976babe50SJustin T. Gibbs static int
53076babe50SJustin T. Gibbs passsendccb(struct cam_periph *periph, union ccb *ccb, union ccb *inccb)
53176babe50SJustin T. Gibbs {
53276babe50SJustin T. Gibbs 	struct pass_softc *softc;
53376babe50SJustin T. Gibbs 	struct cam_periph_map_info mapinfo;
53476babe50SJustin T. Gibbs 	int error, need_unmap;
53576babe50SJustin T. Gibbs 
53676babe50SJustin T. Gibbs 	softc = (struct pass_softc *)periph->softc;
53776babe50SJustin T. Gibbs 
53876babe50SJustin T. Gibbs 	need_unmap = 0;
53976babe50SJustin T. Gibbs 
54076babe50SJustin T. Gibbs 	/*
54176babe50SJustin T. Gibbs 	 * There are some fields in the CCB header that need to be
54276babe50SJustin T. Gibbs 	 * preserved, the rest we get from the user.
54376babe50SJustin T. Gibbs 	 */
54476babe50SJustin T. Gibbs 	xpt_merge_ccb(ccb, inccb);
54576babe50SJustin T. Gibbs 
54676babe50SJustin T. Gibbs 	/*
54776babe50SJustin T. Gibbs 	 * There's no way for the user to have a completion
54876babe50SJustin T. Gibbs 	 * function, so we put our own completion function in here.
54976babe50SJustin T. Gibbs 	 */
55076babe50SJustin T. Gibbs 	ccb->ccb_h.cbfcnp = passdone;
55176babe50SJustin T. Gibbs 
55276babe50SJustin T. Gibbs 	/*
55376babe50SJustin T. Gibbs 	 * We only attempt to map the user memory into kernel space
55476babe50SJustin T. Gibbs 	 * if they haven't passed in a physical memory pointer,
55576babe50SJustin T. Gibbs 	 * and if there is actually an I/O operation to perform.
55676babe50SJustin T. Gibbs 	 * Right now cam_periph_mapmem() only supports SCSI and device
55776babe50SJustin T. Gibbs 	 * match CCBs.  For the SCSI CCBs, we only pass the CCB in if
55876babe50SJustin T. Gibbs 	 * there's actually data to map.  cam_periph_mapmem() will do the
55976babe50SJustin T. Gibbs 	 * right thing, even if there isn't data to map, but since CCBs
56076babe50SJustin T. Gibbs 	 * without data are a reasonably common occurance (e.g. test unit
56176babe50SJustin T. Gibbs 	 * ready), it will save a few cycles if we check for it here.
56276babe50SJustin T. Gibbs 	 */
56376babe50SJustin T. Gibbs 	if (((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0)
56476babe50SJustin T. Gibbs 	 && (((ccb->ccb_h.func_code == XPT_SCSI_IO)
56576babe50SJustin T. Gibbs 	    && ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE))
56676babe50SJustin T. Gibbs 	  || (ccb->ccb_h.func_code == XPT_DEV_MATCH))) {
56776babe50SJustin T. Gibbs 
56876babe50SJustin T. Gibbs 		bzero(&mapinfo, sizeof(mapinfo));
56976babe50SJustin T. Gibbs 
5702b83592fSScott Long 		/*
5712b83592fSScott Long 		 * cam_periph_mapmem calls into proc and vm functions that can
5722b83592fSScott Long 		 * sleep as well as trigger I/O, so we can't hold the lock.
5732b83592fSScott Long 		 * Dropping it here is reasonably safe.
5742b83592fSScott Long 		 */
5752b83592fSScott Long 		cam_periph_unlock(periph);
57676babe50SJustin T. Gibbs 		error = cam_periph_mapmem(ccb, &mapinfo);
5772b83592fSScott Long 		cam_periph_lock(periph);
57876babe50SJustin T. Gibbs 
57976babe50SJustin T. Gibbs 		/*
58076babe50SJustin T. Gibbs 		 * cam_periph_mapmem returned an error, we can't continue.
58176babe50SJustin T. Gibbs 		 * Return the error to the user.
58276babe50SJustin T. Gibbs 		 */
58376babe50SJustin T. Gibbs 		if (error)
58476babe50SJustin T. Gibbs 			return(error);
58576babe50SJustin T. Gibbs 
58676babe50SJustin T. Gibbs 		/*
58776babe50SJustin T. Gibbs 		 * We successfully mapped the memory in, so we need to
58876babe50SJustin T. Gibbs 		 * unmap it when the transaction is done.
58976babe50SJustin T. Gibbs 		 */
59076babe50SJustin T. Gibbs 		need_unmap = 1;
59176babe50SJustin T. Gibbs 	}
59276babe50SJustin T. Gibbs 
59376babe50SJustin T. Gibbs 	/*
59476babe50SJustin T. Gibbs 	 * If the user wants us to perform any error recovery, then honor
59576babe50SJustin T. Gibbs 	 * that request.  Otherwise, it's up to the user to perform any
59676babe50SJustin T. Gibbs 	 * error recovery.
59776babe50SJustin T. Gibbs 	 */
59876babe50SJustin T. Gibbs 	error = cam_periph_runccb(ccb,
59976babe50SJustin T. Gibbs 				  (ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) ?
60076babe50SJustin T. Gibbs 				  passerror : NULL,
6013393f8daSKenneth D. Merry 				  /* cam_flags */ CAM_RETRY_SELTO,
6023393f8daSKenneth D. Merry 				  /* sense_flags */SF_RETRY_UA,
603a9d2245eSPoul-Henning Kamp 				  softc->device_stats);
60476babe50SJustin T. Gibbs 
60576babe50SJustin T. Gibbs 	if (need_unmap != 0)
60676babe50SJustin T. Gibbs 		cam_periph_unmapmem(ccb, &mapinfo);
60776babe50SJustin T. Gibbs 
60876babe50SJustin T. Gibbs 	ccb->ccb_h.cbfcnp = NULL;
60976babe50SJustin T. Gibbs 	ccb->ccb_h.periph_priv = inccb->ccb_h.periph_priv;
61076babe50SJustin T. Gibbs 	bcopy(ccb, inccb, sizeof(union ccb));
61176babe50SJustin T. Gibbs 
61276babe50SJustin T. Gibbs 	return(error);
61376babe50SJustin T. Gibbs }
61476babe50SJustin T. Gibbs 
61576babe50SJustin T. Gibbs static int
61676babe50SJustin T. Gibbs passerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
61776babe50SJustin T. Gibbs {
61876babe50SJustin T. Gibbs 	struct cam_periph *periph;
61976babe50SJustin T. Gibbs 	struct pass_softc *softc;
62076babe50SJustin T. Gibbs 
62176babe50SJustin T. Gibbs 	periph = xpt_path_periph(ccb->ccb_h.path);
62276babe50SJustin T. Gibbs 	softc = (struct pass_softc *)periph->softc;
62376babe50SJustin T. Gibbs 
62476babe50SJustin T. Gibbs 	return(cam_periph_error(ccb, cam_flags, sense_flags,
62576babe50SJustin T. Gibbs 				 &softc->saved_ccb));
62676babe50SJustin T. Gibbs }
627