xref: /freebsd/sys/cam/scsi/scsi_pass.c (revision 898b0535b73b62baaf1d386b169e4d215194a012)
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>
4976babe50SJustin T. Gibbs 
5076babe50SJustin T. Gibbs #include <cam/scsi/scsi_all.h>
5176babe50SJustin T. Gibbs #include <cam/scsi/scsi_pass.h>
5276babe50SJustin T. Gibbs 
5376babe50SJustin T. Gibbs typedef enum {
5476babe50SJustin T. Gibbs 	PASS_FLAG_OPEN			= 0x01,
5576babe50SJustin T. Gibbs 	PASS_FLAG_LOCKED		= 0x02,
5676babe50SJustin T. Gibbs 	PASS_FLAG_INVALID		= 0x04
5776babe50SJustin T. Gibbs } pass_flags;
5876babe50SJustin T. Gibbs 
5976babe50SJustin T. Gibbs typedef enum {
6076babe50SJustin T. Gibbs 	PASS_STATE_NORMAL
6176babe50SJustin T. Gibbs } pass_state;
6276babe50SJustin T. Gibbs 
6376babe50SJustin T. Gibbs typedef enum {
6476babe50SJustin T. Gibbs 	PASS_CCB_BUFFER_IO,
6576babe50SJustin T. Gibbs 	PASS_CCB_WAITING
6676babe50SJustin T. Gibbs } pass_ccb_types;
6776babe50SJustin T. Gibbs 
6876babe50SJustin T. Gibbs #define ccb_type	ppriv_field0
6976babe50SJustin T. Gibbs #define ccb_bp		ppriv_ptr1
7076babe50SJustin T. Gibbs 
7176babe50SJustin T. Gibbs struct pass_softc {
7276babe50SJustin T. Gibbs 	pass_state		state;
7376babe50SJustin T. Gibbs 	pass_flags		flags;
7476babe50SJustin T. Gibbs 	u_int8_t		pd_type;
7576babe50SJustin T. Gibbs 	union ccb		saved_ccb;
76a9d2245eSPoul-Henning Kamp 	struct devstat		*device_stats;
7789c9c53dSPoul-Henning Kamp 	struct cdev *dev;
7876babe50SJustin T. Gibbs };
7976babe50SJustin T. Gibbs 
8076babe50SJustin T. Gibbs 
8176babe50SJustin T. Gibbs static	d_open_t	passopen;
8276babe50SJustin T. Gibbs static	d_close_t	passclose;
8376babe50SJustin T. Gibbs static	d_ioctl_t	passioctl;
8476babe50SJustin T. Gibbs 
8576babe50SJustin T. Gibbs static	periph_init_t	passinit;
8676babe50SJustin T. Gibbs static	periph_ctor_t	passregister;
87ee9c90c7SKenneth D. Merry static	periph_oninv_t	passoninvalidate;
8876babe50SJustin T. Gibbs static	periph_dtor_t	passcleanup;
8976babe50SJustin T. Gibbs static	periph_start_t	passstart;
9076babe50SJustin T. Gibbs static	void		passasync(void *callback_arg, u_int32_t code,
9176babe50SJustin T. Gibbs 				  struct cam_path *path, void *arg);
9276babe50SJustin T. Gibbs static	void		passdone(struct cam_periph *periph,
9376babe50SJustin T. Gibbs 				 union ccb *done_ccb);
9476babe50SJustin T. Gibbs static	int		passerror(union ccb *ccb, u_int32_t cam_flags,
9576babe50SJustin T. Gibbs 				  u_int32_t sense_flags);
9676babe50SJustin T. Gibbs static 	int		passsendccb(struct cam_periph *periph, union ccb *ccb,
9776babe50SJustin T. Gibbs 				    union ccb *inccb);
9876babe50SJustin T. Gibbs 
9976babe50SJustin T. Gibbs static struct periph_driver passdriver =
10076babe50SJustin T. Gibbs {
10176babe50SJustin T. Gibbs 	passinit, "pass",
10276babe50SJustin T. Gibbs 	TAILQ_HEAD_INITIALIZER(passdriver.units), /* generation */ 0
10376babe50SJustin T. Gibbs };
10476babe50SJustin T. Gibbs 
1050b7c27b9SPeter Wemm PERIPHDRIVER_DECLARE(pass, passdriver);
10676babe50SJustin T. Gibbs 
1074e2f199eSPoul-Henning Kamp static struct cdevsw pass_cdevsw = {
108dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
109dc08ffecSPoul-Henning Kamp 	.d_flags =	D_NEEDGIANT,
1107ac40f5fSPoul-Henning Kamp 	.d_open =	passopen,
1117ac40f5fSPoul-Henning Kamp 	.d_close =	passclose,
1127ac40f5fSPoul-Henning Kamp 	.d_ioctl =	passioctl,
1137ac40f5fSPoul-Henning Kamp 	.d_name =	"pass",
11476babe50SJustin T. Gibbs };
11576babe50SJustin T. Gibbs 
11676babe50SJustin T. Gibbs static void
11776babe50SJustin T. Gibbs passinit(void)
11876babe50SJustin T. Gibbs {
11976babe50SJustin T. Gibbs 	cam_status status;
12076babe50SJustin T. Gibbs 	struct cam_path *path;
12176babe50SJustin T. Gibbs 
12276babe50SJustin T. Gibbs 	/*
12376babe50SJustin T. Gibbs 	 * Install a global async callback.  This callback will
12476babe50SJustin T. Gibbs 	 * receive async callbacks like "new device found".
12576babe50SJustin T. Gibbs 	 */
12676babe50SJustin T. Gibbs 	status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
12776babe50SJustin T. Gibbs 				 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
12876babe50SJustin T. Gibbs 
12976babe50SJustin T. Gibbs 	if (status == CAM_REQ_CMP) {
13076babe50SJustin T. Gibbs 		struct ccb_setasync csa;
13176babe50SJustin T. Gibbs 
13276babe50SJustin T. Gibbs                 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
13376babe50SJustin T. Gibbs                 csa.ccb_h.func_code = XPT_SASYNC_CB;
13476babe50SJustin T. Gibbs                 csa.event_enable = AC_FOUND_DEVICE;
13576babe50SJustin T. Gibbs                 csa.callback = passasync;
13676babe50SJustin T. Gibbs                 csa.callback_arg = NULL;
13776babe50SJustin T. Gibbs                 xpt_action((union ccb *)&csa);
13876babe50SJustin T. Gibbs 		status = csa.ccb_h.status;
13976babe50SJustin T. Gibbs                 xpt_free_path(path);
14076babe50SJustin T. Gibbs         }
14176babe50SJustin T. Gibbs 
14276babe50SJustin T. Gibbs 	if (status != CAM_REQ_CMP) {
14376babe50SJustin T. Gibbs 		printf("pass: Failed to attach master async callback "
14476babe50SJustin T. Gibbs 		       "due to status 0x%x!\n", status);
14576babe50SJustin T. Gibbs 	}
14676babe50SJustin T. Gibbs 
14776babe50SJustin T. Gibbs }
14876babe50SJustin T. Gibbs 
14976babe50SJustin T. Gibbs static void
150ee9c90c7SKenneth D. Merry passoninvalidate(struct cam_periph *periph)
151ee9c90c7SKenneth D. Merry {
152ee9c90c7SKenneth D. Merry 	struct pass_softc *softc;
153ee9c90c7SKenneth D. Merry 	struct ccb_setasync csa;
154ee9c90c7SKenneth D. Merry 
155ee9c90c7SKenneth D. Merry 	softc = (struct pass_softc *)periph->softc;
156ee9c90c7SKenneth D. Merry 
157ee9c90c7SKenneth D. Merry 	/*
158ee9c90c7SKenneth D. Merry 	 * De-register any async callbacks.
159ee9c90c7SKenneth D. Merry 	 */
160ee9c90c7SKenneth D. Merry 	xpt_setup_ccb(&csa.ccb_h, periph->path,
161ee9c90c7SKenneth D. Merry 		      /* priority */ 5);
162ee9c90c7SKenneth D. Merry 	csa.ccb_h.func_code = XPT_SASYNC_CB;
163ee9c90c7SKenneth D. Merry 	csa.event_enable = 0;
164ee9c90c7SKenneth D. Merry 	csa.callback = passasync;
165ee9c90c7SKenneth D. Merry 	csa.callback_arg = periph;
166ee9c90c7SKenneth D. Merry 	xpt_action((union ccb *)&csa);
167ee9c90c7SKenneth D. Merry 
168ee9c90c7SKenneth D. Merry 	softc->flags |= PASS_FLAG_INVALID;
169ee9c90c7SKenneth D. Merry 
170ee9c90c7SKenneth D. Merry 	/*
1713393f8daSKenneth D. Merry 	 * XXX Return all queued I/O with ENXIO.
172ee9c90c7SKenneth D. Merry 	 * XXX Handle any transactions queued to the card
173ee9c90c7SKenneth D. Merry 	 *     with XPT_ABORT_CCB.
174ee9c90c7SKenneth D. Merry 	 */
175ee9c90c7SKenneth D. Merry 
176ee9c90c7SKenneth D. Merry 	if (bootverbose) {
177ee9c90c7SKenneth D. Merry 		xpt_print_path(periph->path);
178ee9c90c7SKenneth D. Merry 		printf("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) {
19576babe50SJustin T. Gibbs 		xpt_print_path(periph->path);
19676babe50SJustin T. Gibbs 		printf("removing device entry\n");
19776babe50SJustin T. Gibbs 	}
198ee9c90c7SKenneth D. Merry 	free(softc, M_DEVBUF);
19976babe50SJustin T. Gibbs }
20076babe50SJustin T. Gibbs 
20176babe50SJustin T. Gibbs static void
20276babe50SJustin T. Gibbs passasync(void *callback_arg, u_int32_t code,
20376babe50SJustin T. Gibbs 	  struct cam_path *path, void *arg)
20476babe50SJustin T. Gibbs {
20576babe50SJustin T. Gibbs 	struct cam_periph *periph;
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 		 */
224ee9c90c7SKenneth D. Merry 		status = cam_periph_alloc(passregister, passoninvalidate,
225ee9c90c7SKenneth D. Merry 					  passcleanup, passstart, "pass",
226ee9c90c7SKenneth D. Merry 					  CAM_PERIPH_BIO, cgd->ccb_h.path,
227ee9c90c7SKenneth D. Merry 					  passasync, AC_FOUND_DEVICE, cgd);
22876babe50SJustin T. Gibbs 
22976babe50SJustin T. Gibbs 		if (status != CAM_REQ_CMP
2303393f8daSKenneth D. Merry 		 && status != CAM_REQ_INPROG) {
2313393f8daSKenneth D. Merry 			const struct cam_status_entry *entry;
2323393f8daSKenneth D. Merry 
2333393f8daSKenneth D. Merry 			entry = cam_fetch_status_entry(status);
2343393f8daSKenneth D. Merry 
23576babe50SJustin T. Gibbs 			printf("passasync: Unable to attach new device "
2363393f8daSKenneth D. Merry 			       "due to status %#x: %s\n", status, entry ?
2373393f8daSKenneth D. Merry 			       entry->status_text : "Unknown");
2383393f8daSKenneth D. Merry 		}
23976babe50SJustin T. Gibbs 
24076babe50SJustin T. Gibbs 		break;
24176babe50SJustin T. Gibbs 	}
24276babe50SJustin T. Gibbs 	default:
243516871c6SJustin T. Gibbs 		cam_periph_async(periph, code, path, arg);
24476babe50SJustin T. Gibbs 		break;
24576babe50SJustin T. Gibbs 	}
24676babe50SJustin T. Gibbs }
24776babe50SJustin T. Gibbs 
24876babe50SJustin T. Gibbs static cam_status
24976babe50SJustin T. Gibbs passregister(struct cam_periph *periph, void *arg)
25076babe50SJustin T. Gibbs {
25176babe50SJustin T. Gibbs 	struct pass_softc *softc;
25276babe50SJustin T. Gibbs 	struct ccb_setasync csa;
25376babe50SJustin T. Gibbs 	struct ccb_getdev *cgd;
2543393f8daSKenneth D. Merry 	int    no_tags;
25576babe50SJustin T. Gibbs 
25676babe50SJustin T. Gibbs 	cgd = (struct ccb_getdev *)arg;
25776babe50SJustin T. Gibbs 	if (periph == NULL) {
25876babe50SJustin T. Gibbs 		printf("passregister: periph was NULL!!\n");
25976babe50SJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
26076babe50SJustin T. Gibbs 	}
26176babe50SJustin T. Gibbs 
26276babe50SJustin T. Gibbs 	if (cgd == NULL) {
26376babe50SJustin T. Gibbs 		printf("passregister: no getdev CCB, can't register device\n");
26476babe50SJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
26576babe50SJustin T. Gibbs 	}
26676babe50SJustin T. Gibbs 
26776babe50SJustin T. Gibbs 	softc = (struct pass_softc *)malloc(sizeof(*softc),
26876babe50SJustin T. Gibbs 					    M_DEVBUF, M_NOWAIT);
26976babe50SJustin T. Gibbs 
27076babe50SJustin T. Gibbs 	if (softc == NULL) {
27176babe50SJustin T. Gibbs 		printf("passregister: Unable to probe new device. "
27276babe50SJustin T. Gibbs 		       "Unable to allocate softc\n");
27376babe50SJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
27476babe50SJustin T. Gibbs 	}
27576babe50SJustin T. Gibbs 
27676babe50SJustin T. Gibbs 	bzero(softc, sizeof(*softc));
27776babe50SJustin T. Gibbs 	softc->state = PASS_STATE_NORMAL;
27810b6172aSMatt Jacob 	softc->pd_type = SID_TYPE(&cgd->inq_data);
27976babe50SJustin T. Gibbs 
28076babe50SJustin T. Gibbs 	periph->softc = softc;
2813393f8daSKenneth D. Merry 
28276babe50SJustin T. Gibbs 	/*
28376babe50SJustin T. Gibbs 	 * We pass in 0 for a blocksize, since we don't
28476babe50SJustin T. Gibbs 	 * know what the blocksize of this device is, if
28576babe50SJustin T. Gibbs 	 * it even has a blocksize.
28676babe50SJustin T. Gibbs 	 */
2873393f8daSKenneth D. Merry 	no_tags = (cgd->inq_data.flags & SID_CmdQue) == 0;
288a9d2245eSPoul-Henning Kamp 	softc->device_stats = devstat_new_entry("pass", periph->unit_number, 0,
2893393f8daSKenneth D. Merry 			  DEVSTAT_NO_BLOCKSIZE
2903393f8daSKenneth D. Merry 			  | (no_tags ? DEVSTAT_NO_ORDERED_TAGS : 0),
29110b6172aSMatt Jacob 			  softc->pd_type |
29276babe50SJustin T. Gibbs 			  DEVSTAT_TYPE_IF_SCSI |
2932a888f93SKenneth D. Merry 			  DEVSTAT_TYPE_PASS,
2942a888f93SKenneth D. Merry 			  DEVSTAT_PRIORITY_PASS);
29573d26919SKenneth D. Merry 
29673d26919SKenneth D. Merry 	/* Register the device */
29773d26919SKenneth D. Merry 	softc->dev = make_dev(&pass_cdevsw, periph->unit_number, UID_ROOT,
29873d26919SKenneth D. Merry 			      GID_OPERATOR, 0600, "%s%d", periph->periph_name,
29973d26919SKenneth D. Merry 			      periph->unit_number);
300e2a5fdf9SNate Lawson 	softc->dev->si_drv1 = periph;
30173d26919SKenneth D. Merry 
30276babe50SJustin T. Gibbs 	/*
30376babe50SJustin T. Gibbs 	 * Add an async callback so that we get
30476babe50SJustin T. Gibbs 	 * notified if this device goes away.
30576babe50SJustin T. Gibbs 	 */
30676babe50SJustin T. Gibbs 	xpt_setup_ccb(&csa.ccb_h, periph->path, /* priority */ 5);
30776babe50SJustin T. Gibbs 	csa.ccb_h.func_code = XPT_SASYNC_CB;
30876babe50SJustin T. Gibbs 	csa.event_enable = AC_LOST_DEVICE;
30976babe50SJustin T. Gibbs 	csa.callback = passasync;
31076babe50SJustin T. Gibbs 	csa.callback_arg = periph;
31176babe50SJustin T. Gibbs 	xpt_action((union ccb *)&csa);
31276babe50SJustin T. Gibbs 
31376babe50SJustin T. Gibbs 	if (bootverbose)
31476babe50SJustin T. Gibbs 		xpt_announce_periph(periph, NULL);
31576babe50SJustin T. Gibbs 
31676babe50SJustin T. Gibbs 	return(CAM_REQ_CMP);
31776babe50SJustin T. Gibbs }
31876babe50SJustin T. Gibbs 
31976babe50SJustin T. Gibbs static int
32089c9c53dSPoul-Henning Kamp passopen(struct cdev *dev, int flags, int fmt, struct thread *td)
32176babe50SJustin T. Gibbs {
32276babe50SJustin T. Gibbs 	struct cam_periph *periph;
32376babe50SJustin T. Gibbs 	struct pass_softc *softc;
324e2a5fdf9SNate Lawson 	int error;
325ee9c90c7SKenneth D. Merry 	int s;
32676babe50SJustin T. Gibbs 
32776babe50SJustin T. Gibbs 	error = 0; /* default to no error */
32876babe50SJustin T. Gibbs 
329e2a5fdf9SNate Lawson 	periph = (struct cam_periph *)dev->si_drv1;
33076babe50SJustin T. Gibbs 	if (periph == NULL)
33176babe50SJustin T. Gibbs 		return (ENXIO);
33276babe50SJustin T. Gibbs 
33376babe50SJustin T. Gibbs 	softc = (struct pass_softc *)periph->softc;
33476babe50SJustin T. Gibbs 
335ee9c90c7SKenneth D. Merry 	s = splsoftcam();
336ee9c90c7SKenneth D. Merry 	if (softc->flags & PASS_FLAG_INVALID) {
337ee9c90c7SKenneth D. Merry 		splx(s);
33876babe50SJustin T. Gibbs 		return(ENXIO);
339ee9c90c7SKenneth D. Merry 	}
34022b9c86cSKenneth D. Merry 
34122b9c86cSKenneth D. Merry 	/*
342f5ef42beSRobert Watson 	 * Don't allow access when we're running at a high securelevel.
34322b9c86cSKenneth D. Merry 	 */
344a854ed98SJohn Baldwin 	error = securelevel_gt(td->td_ucred, 1);
345f7312ca2SRobert Watson 	if (error) {
346ee9c90c7SKenneth D. Merry 		splx(s);
347f7312ca2SRobert Watson 		return(error);
34822b9c86cSKenneth D. Merry 	}
34976babe50SJustin T. Gibbs 
35076babe50SJustin T. Gibbs 	/*
35166a0780eSKenneth D. Merry 	 * Only allow read-write access.
35266a0780eSKenneth D. Merry 	 */
35322b9c86cSKenneth D. Merry 	if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0)) {
35422b9c86cSKenneth D. Merry 		splx(s);
35566a0780eSKenneth D. Merry 		return(EPERM);
35622b9c86cSKenneth D. Merry 	}
35766a0780eSKenneth D. Merry 
35866a0780eSKenneth D. Merry 	/*
35976babe50SJustin T. Gibbs 	 * We don't allow nonblocking access.
36076babe50SJustin T. Gibbs 	 */
36176babe50SJustin T. Gibbs 	if ((flags & O_NONBLOCK) != 0) {
36222b9c86cSKenneth D. Merry 		xpt_print_path(periph->path);
36322b9c86cSKenneth D. Merry 		printf("can't do nonblocking accesss\n");
36422b9c86cSKenneth D. Merry 		splx(s);
36522b9c86cSKenneth D. Merry 		return(EINVAL);
36676babe50SJustin T. Gibbs 	}
36776babe50SJustin T. Gibbs 
36822b9c86cSKenneth D. Merry 	if ((error = cam_periph_lock(periph, PRIBIO | PCATCH)) != 0) {
36922b9c86cSKenneth D. Merry 		splx(s);
37076babe50SJustin T. Gibbs 		return (error);
37122b9c86cSKenneth D. Merry 	}
37222b9c86cSKenneth D. Merry 
37322b9c86cSKenneth D. Merry 	splx(s);
37476babe50SJustin T. Gibbs 
37576babe50SJustin T. Gibbs 	if ((softc->flags & PASS_FLAG_OPEN) == 0) {
37676babe50SJustin T. Gibbs 		if (cam_periph_acquire(periph) != CAM_REQ_CMP)
37776babe50SJustin T. Gibbs 			return(ENXIO);
37876babe50SJustin T. Gibbs 		softc->flags |= PASS_FLAG_OPEN;
37976babe50SJustin T. Gibbs 	}
38076babe50SJustin T. Gibbs 
38176babe50SJustin T. Gibbs 	cam_periph_unlock(periph);
38276babe50SJustin T. Gibbs 
38376babe50SJustin T. Gibbs 	return (error);
38476babe50SJustin T. Gibbs }
38576babe50SJustin T. Gibbs 
38676babe50SJustin T. Gibbs static int
38789c9c53dSPoul-Henning Kamp passclose(struct cdev *dev, int flag, int fmt, struct thread *td)
38876babe50SJustin T. Gibbs {
38976babe50SJustin T. Gibbs 	struct 	cam_periph *periph;
39076babe50SJustin T. Gibbs 	struct	pass_softc *softc;
391e2a5fdf9SNate Lawson 	int	error;
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 
39776babe50SJustin T. Gibbs 	softc = (struct pass_softc *)periph->softc;
39876babe50SJustin T. Gibbs 
39976babe50SJustin T. Gibbs 	if ((error = cam_periph_lock(periph, PRIBIO)) != 0)
40076babe50SJustin T. Gibbs 		return (error);
40176babe50SJustin T. Gibbs 
40276babe50SJustin T. Gibbs 	softc->flags &= ~PASS_FLAG_OPEN;
40376babe50SJustin T. Gibbs 
40476babe50SJustin T. Gibbs 	cam_periph_unlock(periph);
40576babe50SJustin T. Gibbs 	cam_periph_release(periph);
40676babe50SJustin T. Gibbs 
40776babe50SJustin T. Gibbs 	return (0);
40876babe50SJustin T. Gibbs }
40976babe50SJustin T. Gibbs 
41076babe50SJustin T. Gibbs static void
41176babe50SJustin T. Gibbs passstart(struct cam_periph *periph, union ccb *start_ccb)
41276babe50SJustin T. Gibbs {
41376babe50SJustin T. Gibbs 	struct pass_softc *softc;
41476babe50SJustin T. Gibbs 	int s;
41576babe50SJustin T. Gibbs 
41676babe50SJustin T. Gibbs 	softc = (struct pass_softc *)periph->softc;
41776babe50SJustin T. Gibbs 
41876babe50SJustin T. Gibbs 	switch (softc->state) {
41976babe50SJustin T. Gibbs 	case PASS_STATE_NORMAL:
42076babe50SJustin T. Gibbs 		s = splbio();
42176babe50SJustin T. Gibbs 		start_ccb->ccb_h.ccb_type = PASS_CCB_WAITING;
42276babe50SJustin T. Gibbs 		SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
42376babe50SJustin T. Gibbs 				  periph_links.sle);
42476babe50SJustin T. Gibbs 		periph->immediate_priority = CAM_PRIORITY_NONE;
42576babe50SJustin T. Gibbs 		splx(s);
42676babe50SJustin T. Gibbs 		wakeup(&periph->ccb_list);
42776babe50SJustin T. Gibbs 		break;
42876babe50SJustin T. Gibbs 	}
42976babe50SJustin T. Gibbs }
4303393f8daSKenneth D. Merry 
43176babe50SJustin T. Gibbs static void
43276babe50SJustin T. Gibbs passdone(struct cam_periph *periph, union ccb *done_ccb)
43376babe50SJustin T. Gibbs {
43476babe50SJustin T. Gibbs 	struct pass_softc *softc;
43576babe50SJustin T. Gibbs 	struct ccb_scsiio *csio;
43676babe50SJustin T. Gibbs 
43776babe50SJustin T. Gibbs 	softc = (struct pass_softc *)periph->softc;
43876babe50SJustin T. Gibbs 	csio = &done_ccb->csio;
43976babe50SJustin T. Gibbs 	switch (csio->ccb_h.ccb_type) {
44076babe50SJustin T. Gibbs 	case PASS_CCB_WAITING:
44176babe50SJustin T. Gibbs 		/* Caller will release the CCB */
44276babe50SJustin T. Gibbs 		wakeup(&done_ccb->ccb_h.cbfcnp);
44376babe50SJustin T. Gibbs 		return;
44476babe50SJustin T. Gibbs 	}
44576babe50SJustin T. Gibbs 	xpt_release_ccb(done_ccb);
44676babe50SJustin T. Gibbs }
44776babe50SJustin T. Gibbs 
44876babe50SJustin T. Gibbs static int
44989c9c53dSPoul-Henning Kamp passioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
45076babe50SJustin T. Gibbs {
45176babe50SJustin T. Gibbs 	struct	cam_periph *periph;
45276babe50SJustin T. Gibbs 	struct	pass_softc *softc;
45376babe50SJustin T. Gibbs 	int	error;
45476babe50SJustin T. Gibbs 
455e2a5fdf9SNate Lawson 	periph = (struct cam_periph *)dev->si_drv1;
45676babe50SJustin T. Gibbs 	if (periph == NULL)
45776babe50SJustin T. Gibbs 		return(ENXIO);
45876babe50SJustin T. Gibbs 
45976babe50SJustin T. Gibbs 	softc = (struct pass_softc *)periph->softc;
46076babe50SJustin T. Gibbs 
46176babe50SJustin T. Gibbs 	error = 0;
46276babe50SJustin T. Gibbs 
46376babe50SJustin T. Gibbs 	switch (cmd) {
46476babe50SJustin T. Gibbs 
46576babe50SJustin T. Gibbs 	case CAMIOCOMMAND:
46676babe50SJustin T. Gibbs 	{
46776babe50SJustin T. Gibbs 		union ccb *inccb;
46876babe50SJustin T. Gibbs 		union ccb *ccb;
4699deea857SKenneth D. Merry 		int ccb_malloced;
47076babe50SJustin T. Gibbs 
47176babe50SJustin T. Gibbs 		inccb = (union ccb *)addr;
4729deea857SKenneth D. Merry 
4739deea857SKenneth D. Merry 		/*
4749deea857SKenneth D. Merry 		 * Some CCB types, like scan bus and scan lun can only go
4759deea857SKenneth D. Merry 		 * through the transport layer device.
4769deea857SKenneth D. Merry 		 */
4779deea857SKenneth D. Merry 		if (inccb->ccb_h.func_code & XPT_FC_XPT_ONLY) {
4789deea857SKenneth D. Merry 			xpt_print_path(periph->path);
4799deea857SKenneth D. Merry 			printf("CCB function code %#x is restricted to the "
4809deea857SKenneth D. Merry 			       "XPT device\n", inccb->ccb_h.func_code);
4819deea857SKenneth D. Merry 			error = ENODEV;
4829deea857SKenneth D. Merry 			break;
4839deea857SKenneth D. Merry 		}
4849deea857SKenneth D. Merry 
4859deea857SKenneth D. Merry 		/*
4869deea857SKenneth D. Merry 		 * Non-immediate CCBs need a CCB from the per-device pool
4879deea857SKenneth D. Merry 		 * of CCBs, which is scheduled by the transport layer.
4889deea857SKenneth D. Merry 		 * Immediate CCBs and user-supplied CCBs should just be
4899deea857SKenneth D. Merry 		 * malloced.
4909deea857SKenneth D. Merry 		 */
4919deea857SKenneth D. Merry 		if ((inccb->ccb_h.func_code & XPT_FC_QUEUED)
4929deea857SKenneth D. Merry 		 && ((inccb->ccb_h.func_code & XPT_FC_USER_CCB) == 0)) {
4939deea857SKenneth D. Merry 			ccb = cam_periph_getccb(periph,
4949deea857SKenneth D. Merry 						inccb->ccb_h.pinfo.priority);
4959deea857SKenneth D. Merry 			ccb_malloced = 0;
4969deea857SKenneth D. Merry 		} else {
4979deea857SKenneth D. Merry 			ccb = xpt_alloc_ccb();
4989deea857SKenneth D. Merry 
4999deea857SKenneth D. Merry 			if (ccb != NULL)
5009deea857SKenneth D. Merry 				xpt_setup_ccb(&ccb->ccb_h, periph->path,
5019deea857SKenneth D. Merry 					      inccb->ccb_h.pinfo.priority);
5029deea857SKenneth D. Merry 			ccb_malloced = 1;
5039deea857SKenneth D. Merry 		}
5049deea857SKenneth D. Merry 
5059deea857SKenneth D. Merry 		if (ccb == NULL) {
5069deea857SKenneth D. Merry 			xpt_print_path(periph->path);
5079deea857SKenneth D. Merry 			printf("unable to allocate CCB\n");
5089deea857SKenneth D. Merry 			error = ENOMEM;
5099deea857SKenneth D. Merry 			break;
5109deea857SKenneth D. Merry 		}
51176babe50SJustin T. Gibbs 
51276babe50SJustin T. Gibbs 		error = passsendccb(periph, ccb, inccb);
51376babe50SJustin T. Gibbs 
5149deea857SKenneth D. Merry 		if (ccb_malloced)
5159deea857SKenneth D. Merry 			xpt_free_ccb(ccb);
5169deea857SKenneth D. Merry 		else
51776babe50SJustin T. Gibbs 			xpt_release_ccb(ccb);
51876babe50SJustin T. Gibbs 
51976babe50SJustin T. Gibbs 		break;
52076babe50SJustin T. Gibbs 	}
52176babe50SJustin T. Gibbs 	default:
52276babe50SJustin T. Gibbs 		error = cam_periph_ioctl(periph, cmd, addr, passerror);
52376babe50SJustin T. Gibbs 		break;
52476babe50SJustin T. Gibbs 	}
52576babe50SJustin T. Gibbs 
52676babe50SJustin T. Gibbs 	return(error);
52776babe50SJustin T. Gibbs }
52876babe50SJustin T. Gibbs 
52976babe50SJustin T. Gibbs /*
53076babe50SJustin T. Gibbs  * Generally, "ccb" should be the CCB supplied by the kernel.  "inccb"
53176babe50SJustin T. Gibbs  * should be the CCB that is copied in from the user.
53276babe50SJustin T. Gibbs  */
53376babe50SJustin T. Gibbs static int
53476babe50SJustin T. Gibbs passsendccb(struct cam_periph *periph, union ccb *ccb, union ccb *inccb)
53576babe50SJustin T. Gibbs {
53676babe50SJustin T. Gibbs 	struct pass_softc *softc;
53776babe50SJustin T. Gibbs 	struct cam_periph_map_info mapinfo;
53876babe50SJustin T. Gibbs 	int error, need_unmap;
53976babe50SJustin T. Gibbs 
54076babe50SJustin T. Gibbs 	softc = (struct pass_softc *)periph->softc;
54176babe50SJustin T. Gibbs 
54276babe50SJustin T. Gibbs 	need_unmap = 0;
54376babe50SJustin T. Gibbs 
54476babe50SJustin T. Gibbs 	/*
54576babe50SJustin T. Gibbs 	 * There are some fields in the CCB header that need to be
54676babe50SJustin T. Gibbs 	 * preserved, the rest we get from the user.
54776babe50SJustin T. Gibbs 	 */
54876babe50SJustin T. Gibbs 	xpt_merge_ccb(ccb, inccb);
54976babe50SJustin T. Gibbs 
55076babe50SJustin T. Gibbs 	/*
55176babe50SJustin T. Gibbs 	 * There's no way for the user to have a completion
55276babe50SJustin T. Gibbs 	 * function, so we put our own completion function in here.
55376babe50SJustin T. Gibbs 	 */
55476babe50SJustin T. Gibbs 	ccb->ccb_h.cbfcnp = passdone;
55576babe50SJustin T. Gibbs 
55676babe50SJustin T. Gibbs 	/*
55776babe50SJustin T. Gibbs 	 * We only attempt to map the user memory into kernel space
55876babe50SJustin T. Gibbs 	 * if they haven't passed in a physical memory pointer,
55976babe50SJustin T. Gibbs 	 * and if there is actually an I/O operation to perform.
56076babe50SJustin T. Gibbs 	 * Right now cam_periph_mapmem() only supports SCSI and device
56176babe50SJustin T. Gibbs 	 * match CCBs.  For the SCSI CCBs, we only pass the CCB in if
56276babe50SJustin T. Gibbs 	 * there's actually data to map.  cam_periph_mapmem() will do the
56376babe50SJustin T. Gibbs 	 * right thing, even if there isn't data to map, but since CCBs
56476babe50SJustin T. Gibbs 	 * without data are a reasonably common occurance (e.g. test unit
56576babe50SJustin T. Gibbs 	 * ready), it will save a few cycles if we check for it here.
56676babe50SJustin T. Gibbs 	 */
56776babe50SJustin T. Gibbs 	if (((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0)
56876babe50SJustin T. Gibbs 	 && (((ccb->ccb_h.func_code == XPT_SCSI_IO)
56976babe50SJustin T. Gibbs 	    && ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE))
57076babe50SJustin T. Gibbs 	  || (ccb->ccb_h.func_code == XPT_DEV_MATCH))) {
57176babe50SJustin T. Gibbs 
57276babe50SJustin T. Gibbs 		bzero(&mapinfo, sizeof(mapinfo));
57376babe50SJustin T. Gibbs 
57476babe50SJustin T. Gibbs 		error = cam_periph_mapmem(ccb, &mapinfo);
57576babe50SJustin T. Gibbs 
57676babe50SJustin T. Gibbs 		/*
57776babe50SJustin T. Gibbs 		 * cam_periph_mapmem returned an error, we can't continue.
57876babe50SJustin T. Gibbs 		 * Return the error to the user.
57976babe50SJustin T. Gibbs 		 */
58076babe50SJustin T. Gibbs 		if (error)
58176babe50SJustin T. Gibbs 			return(error);
58276babe50SJustin T. Gibbs 
58376babe50SJustin T. Gibbs 		/*
58476babe50SJustin T. Gibbs 		 * We successfully mapped the memory in, so we need to
58576babe50SJustin T. Gibbs 		 * unmap it when the transaction is done.
58676babe50SJustin T. Gibbs 		 */
58776babe50SJustin T. Gibbs 		need_unmap = 1;
58876babe50SJustin T. Gibbs 	}
58976babe50SJustin T. Gibbs 
59076babe50SJustin T. Gibbs 	/*
59176babe50SJustin T. Gibbs 	 * If the user wants us to perform any error recovery, then honor
59276babe50SJustin T. Gibbs 	 * that request.  Otherwise, it's up to the user to perform any
59376babe50SJustin T. Gibbs 	 * error recovery.
59476babe50SJustin T. Gibbs 	 */
59576babe50SJustin T. Gibbs 	error = cam_periph_runccb(ccb,
59676babe50SJustin T. Gibbs 				  (ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) ?
59776babe50SJustin T. Gibbs 				  passerror : NULL,
5983393f8daSKenneth D. Merry 				  /* cam_flags */ CAM_RETRY_SELTO,
5993393f8daSKenneth D. Merry 				  /* sense_flags */SF_RETRY_UA,
600a9d2245eSPoul-Henning Kamp 				  softc->device_stats);
60176babe50SJustin T. Gibbs 
60276babe50SJustin T. Gibbs 	if (need_unmap != 0)
60376babe50SJustin T. Gibbs 		cam_periph_unmapmem(ccb, &mapinfo);
60476babe50SJustin T. Gibbs 
60576babe50SJustin T. Gibbs 	ccb->ccb_h.cbfcnp = NULL;
60676babe50SJustin T. Gibbs 	ccb->ccb_h.periph_priv = inccb->ccb_h.periph_priv;
60776babe50SJustin T. Gibbs 	bcopy(ccb, inccb, sizeof(union ccb));
60876babe50SJustin T. Gibbs 
60976babe50SJustin T. Gibbs 	return(error);
61076babe50SJustin T. Gibbs }
61176babe50SJustin T. Gibbs 
61276babe50SJustin T. Gibbs static int
61376babe50SJustin T. Gibbs passerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
61476babe50SJustin T. Gibbs {
61576babe50SJustin T. Gibbs 	struct cam_periph *periph;
61676babe50SJustin T. Gibbs 	struct pass_softc *softc;
61776babe50SJustin T. Gibbs 
61876babe50SJustin T. Gibbs 	periph = xpt_path_periph(ccb->ccb_h.path);
61976babe50SJustin T. Gibbs 	softc = (struct pass_softc *)periph->softc;
62076babe50SJustin T. Gibbs 
62176babe50SJustin T. Gibbs 	return(cam_periph_error(ccb, cam_flags, sense_flags,
62276babe50SJustin T. Gibbs 				 &softc->saved_ccb));
62376babe50SJustin T. Gibbs }
624