xref: /freebsd/sys/cam/scsi/scsi_pass.c (revision 9626b608de4a43ec9984c3ee95b2ce624b3c0924)
176babe50SJustin T. Gibbs /*
276babe50SJustin T. Gibbs  * Copyright (c) 1997, 1998 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  *
27c3aac50fSPeter Wemm  * $FreeBSD$
2876babe50SJustin T. Gibbs  */
2976babe50SJustin T. Gibbs 
3076babe50SJustin T. Gibbs #include <sys/param.h>
3176babe50SJustin T. Gibbs #include <sys/systm.h>
3276babe50SJustin T. Gibbs #include <sys/kernel.h>
3376babe50SJustin T. Gibbs #include <sys/types.h>
349626b608SPoul-Henning Kamp #include <sys/bio.h>
3576babe50SJustin T. Gibbs #include <sys/malloc.h>
3676babe50SJustin T. Gibbs #include <sys/fcntl.h>
3776babe50SJustin T. Gibbs #include <sys/stat.h>
3876babe50SJustin T. Gibbs #include <sys/conf.h>
3976babe50SJustin T. Gibbs #include <sys/proc.h>
4076babe50SJustin T. Gibbs #include <sys/errno.h>
4176babe50SJustin T. Gibbs #include <sys/devicestat.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_extend.h>
4676babe50SJustin T. Gibbs #include <cam/cam_periph.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_message.h>
5276babe50SJustin T. Gibbs #include <cam/scsi/scsi_da.h>
5376babe50SJustin T. Gibbs #include <cam/scsi/scsi_pass.h>
5476babe50SJustin T. Gibbs 
5576babe50SJustin T. Gibbs typedef enum {
5676babe50SJustin T. Gibbs 	PASS_FLAG_OPEN			= 0x01,
5776babe50SJustin T. Gibbs 	PASS_FLAG_LOCKED		= 0x02,
5876babe50SJustin T. Gibbs 	PASS_FLAG_INVALID		= 0x04
5976babe50SJustin T. Gibbs } pass_flags;
6076babe50SJustin T. Gibbs 
6176babe50SJustin T. Gibbs typedef enum {
6276babe50SJustin T. Gibbs 	PASS_STATE_NORMAL
6376babe50SJustin T. Gibbs } pass_state;
6476babe50SJustin T. Gibbs 
6576babe50SJustin T. Gibbs typedef enum {
6676babe50SJustin T. Gibbs 	PASS_CCB_BUFFER_IO,
6776babe50SJustin T. Gibbs 	PASS_CCB_WAITING
6876babe50SJustin T. Gibbs } pass_ccb_types;
6976babe50SJustin T. Gibbs 
7076babe50SJustin T. Gibbs #define ccb_type	ppriv_field0
7176babe50SJustin T. Gibbs #define ccb_bp		ppriv_ptr1
7276babe50SJustin T. Gibbs 
7376babe50SJustin T. Gibbs struct pass_softc {
7476babe50SJustin T. Gibbs 	pass_state	state;
7576babe50SJustin T. Gibbs 	pass_flags	flags;
7676babe50SJustin T. Gibbs 	u_int8_t	pd_type;
778177437dSPoul-Henning Kamp 	struct		bio_queue_head bio_queue;
7876babe50SJustin T. Gibbs 	union ccb	saved_ccb;
7976babe50SJustin T. Gibbs 	struct devstat	device_stats;
8073d26919SKenneth D. Merry 	dev_t		dev;
8176babe50SJustin T. Gibbs };
8276babe50SJustin T. Gibbs 
8376babe50SJustin T. Gibbs #ifndef MIN
8476babe50SJustin T. Gibbs #define MIN(x,y) ((x<y) ? x : y)
8576babe50SJustin T. Gibbs #endif
8676babe50SJustin T. Gibbs 
8776babe50SJustin T. Gibbs #define PASS_CDEV_MAJOR 31
8876babe50SJustin T. Gibbs 
8976babe50SJustin T. Gibbs static	d_open_t	passopen;
9076babe50SJustin T. Gibbs static	d_close_t	passclose;
9176babe50SJustin T. Gibbs static	d_ioctl_t	passioctl;
9276babe50SJustin T. Gibbs static	d_strategy_t	passstrategy;
9376babe50SJustin T. Gibbs 
9476babe50SJustin T. Gibbs static	periph_init_t	passinit;
9576babe50SJustin T. Gibbs static	periph_ctor_t	passregister;
96ee9c90c7SKenneth D. Merry static	periph_oninv_t	passoninvalidate;
9776babe50SJustin T. Gibbs static	periph_dtor_t	passcleanup;
9876babe50SJustin T. Gibbs static	periph_start_t	passstart;
9976babe50SJustin T. Gibbs static	void		passasync(void *callback_arg, u_int32_t code,
10076babe50SJustin T. Gibbs 				  struct cam_path *path, void *arg);
10176babe50SJustin T. Gibbs static	void		passdone(struct cam_periph *periph,
10276babe50SJustin T. Gibbs 				 union ccb *done_ccb);
10376babe50SJustin T. Gibbs static	int		passerror(union ccb *ccb, u_int32_t cam_flags,
10476babe50SJustin T. Gibbs 				  u_int32_t sense_flags);
10576babe50SJustin T. Gibbs static 	int		passsendccb(struct cam_periph *periph, union ccb *ccb,
10676babe50SJustin T. Gibbs 				    union ccb *inccb);
10776babe50SJustin T. Gibbs 
10876babe50SJustin T. Gibbs static struct periph_driver passdriver =
10976babe50SJustin T. Gibbs {
11076babe50SJustin T. Gibbs 	passinit, "pass",
11176babe50SJustin T. Gibbs 	TAILQ_HEAD_INITIALIZER(passdriver.units), /* generation */ 0
11276babe50SJustin T. Gibbs };
11376babe50SJustin T. Gibbs 
11476babe50SJustin T. Gibbs DATA_SET(periphdriver_set, passdriver);
11576babe50SJustin T. Gibbs 
1164e2f199eSPoul-Henning Kamp static struct cdevsw pass_cdevsw = {
1174e2f199eSPoul-Henning Kamp 	/* open */	passopen,
1184e2f199eSPoul-Henning Kamp 	/* close */	passclose,
1194e2f199eSPoul-Henning Kamp 	/* read */	physread,
1204e2f199eSPoul-Henning Kamp 	/* write */	physwrite,
1214e2f199eSPoul-Henning Kamp 	/* ioctl */	passioctl,
1224e2f199eSPoul-Henning Kamp 	/* poll */	nopoll,
1234e2f199eSPoul-Henning Kamp 	/* mmap */	nommap,
1244e2f199eSPoul-Henning Kamp 	/* strategy */	passstrategy,
1254e2f199eSPoul-Henning Kamp 	/* name */	"pass",
1264e2f199eSPoul-Henning Kamp 	/* maj */	PASS_CDEV_MAJOR,
1274e2f199eSPoul-Henning Kamp 	/* dump */	nodump,
1284e2f199eSPoul-Henning Kamp 	/* psize */	nopsize,
1294e2f199eSPoul-Henning Kamp 	/* flags */	0,
1304e2f199eSPoul-Henning Kamp 	/* bmaj */	-1
13176babe50SJustin T. Gibbs };
13276babe50SJustin T. Gibbs 
13376babe50SJustin T. Gibbs static struct extend_array *passperiphs;
13476babe50SJustin T. Gibbs 
13576babe50SJustin T. Gibbs static void
13676babe50SJustin T. Gibbs passinit(void)
13776babe50SJustin T. Gibbs {
13876babe50SJustin T. Gibbs 	cam_status status;
13976babe50SJustin T. Gibbs 	struct cam_path *path;
14076babe50SJustin T. Gibbs 
14176babe50SJustin T. Gibbs 	/*
14276babe50SJustin T. Gibbs 	 * Create our extend array for storing the devices we attach to.
14376babe50SJustin T. Gibbs 	 */
14476babe50SJustin T. Gibbs 	passperiphs = cam_extend_new();
14576babe50SJustin T. Gibbs 	if (passperiphs == NULL) {
14676babe50SJustin T. Gibbs 		printf("passm: Failed to alloc extend array!\n");
14776babe50SJustin T. Gibbs 		return;
14876babe50SJustin T. Gibbs 	}
14976babe50SJustin T. Gibbs 
15076babe50SJustin T. Gibbs 	/*
15176babe50SJustin T. Gibbs 	 * Install a global async callback.  This callback will
15276babe50SJustin T. Gibbs 	 * receive async callbacks like "new device found".
15376babe50SJustin T. Gibbs 	 */
15476babe50SJustin T. Gibbs 	status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
15576babe50SJustin T. Gibbs 				 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
15676babe50SJustin T. Gibbs 
15776babe50SJustin T. Gibbs 	if (status == CAM_REQ_CMP) {
15876babe50SJustin T. Gibbs 		struct ccb_setasync csa;
15976babe50SJustin T. Gibbs 
16076babe50SJustin T. Gibbs                 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
16176babe50SJustin T. Gibbs                 csa.ccb_h.func_code = XPT_SASYNC_CB;
16276babe50SJustin T. Gibbs                 csa.event_enable = AC_FOUND_DEVICE;
16376babe50SJustin T. Gibbs                 csa.callback = passasync;
16476babe50SJustin T. Gibbs                 csa.callback_arg = NULL;
16576babe50SJustin T. Gibbs                 xpt_action((union ccb *)&csa);
16676babe50SJustin T. Gibbs 		status = csa.ccb_h.status;
16776babe50SJustin T. Gibbs                 xpt_free_path(path);
16876babe50SJustin T. Gibbs         }
16976babe50SJustin T. Gibbs 
17076babe50SJustin T. Gibbs 	if (status != CAM_REQ_CMP) {
17176babe50SJustin T. Gibbs 		printf("pass: Failed to attach master async callback "
17276babe50SJustin T. Gibbs 		       "due to status 0x%x!\n", status);
17376babe50SJustin T. Gibbs 	}
17476babe50SJustin T. Gibbs 
17576babe50SJustin T. Gibbs }
17676babe50SJustin T. Gibbs 
17776babe50SJustin T. Gibbs static void
178ee9c90c7SKenneth D. Merry passoninvalidate(struct cam_periph *periph)
179ee9c90c7SKenneth D. Merry {
180ee9c90c7SKenneth D. Merry 	int s;
181ee9c90c7SKenneth D. Merry 	struct pass_softc *softc;
1828177437dSPoul-Henning Kamp 	struct bio *q_bp;
183ee9c90c7SKenneth D. Merry 	struct ccb_setasync csa;
184ee9c90c7SKenneth D. Merry 
185ee9c90c7SKenneth D. Merry 	softc = (struct pass_softc *)periph->softc;
186ee9c90c7SKenneth D. Merry 
187ee9c90c7SKenneth D. Merry 	/*
188ee9c90c7SKenneth D. Merry 	 * De-register any async callbacks.
189ee9c90c7SKenneth D. Merry 	 */
190ee9c90c7SKenneth D. Merry 	xpt_setup_ccb(&csa.ccb_h, periph->path,
191ee9c90c7SKenneth D. Merry 		      /* priority */ 5);
192ee9c90c7SKenneth D. Merry 	csa.ccb_h.func_code = XPT_SASYNC_CB;
193ee9c90c7SKenneth D. Merry 	csa.event_enable = 0;
194ee9c90c7SKenneth D. Merry 	csa.callback = passasync;
195ee9c90c7SKenneth D. Merry 	csa.callback_arg = periph;
196ee9c90c7SKenneth D. Merry 	xpt_action((union ccb *)&csa);
197ee9c90c7SKenneth D. Merry 
198ee9c90c7SKenneth D. Merry 	softc->flags |= PASS_FLAG_INVALID;
199ee9c90c7SKenneth D. Merry 
200ee9c90c7SKenneth D. Merry 	/*
201ee9c90c7SKenneth D. Merry 	 * Although the oninvalidate() routines are always called at
202ee9c90c7SKenneth D. Merry 	 * splsoftcam, we need to be at splbio() here to keep the buffer
203ee9c90c7SKenneth D. Merry 	 * queue from being modified while we traverse it.
204ee9c90c7SKenneth D. Merry 	 */
205ee9c90c7SKenneth D. Merry 	s = splbio();
206ee9c90c7SKenneth D. Merry 
207ee9c90c7SKenneth D. Merry 	/*
208ee9c90c7SKenneth D. Merry 	 * Return all queued I/O with ENXIO.
209ee9c90c7SKenneth D. Merry 	 * XXX Handle any transactions queued to the card
210ee9c90c7SKenneth D. Merry 	 *     with XPT_ABORT_CCB.
211ee9c90c7SKenneth D. Merry 	 */
2128177437dSPoul-Henning Kamp 	while ((q_bp = bioq_first(&softc->bio_queue)) != NULL){
2138177437dSPoul-Henning Kamp 		bioq_remove(&softc->bio_queue, q_bp);
2148177437dSPoul-Henning Kamp 		q_bp->bio_resid = q_bp->bio_bcount;
2158177437dSPoul-Henning Kamp 		q_bp->bio_error = ENXIO;
2168177437dSPoul-Henning Kamp 		q_bp->bio_flags |= BIO_ERROR;
217ee9c90c7SKenneth D. Merry 		biodone(q_bp);
218ee9c90c7SKenneth D. Merry 	}
219ee9c90c7SKenneth D. Merry 	splx(s);
220ee9c90c7SKenneth D. Merry 
221ee9c90c7SKenneth D. Merry 	if (bootverbose) {
222ee9c90c7SKenneth D. Merry 		xpt_print_path(periph->path);
223ee9c90c7SKenneth D. Merry 		printf("lost device\n");
224ee9c90c7SKenneth D. Merry 	}
225ee9c90c7SKenneth D. Merry 
226ee9c90c7SKenneth D. Merry }
227ee9c90c7SKenneth D. Merry 
228ee9c90c7SKenneth D. Merry static void
22976babe50SJustin T. Gibbs passcleanup(struct cam_periph *periph)
23076babe50SJustin T. Gibbs {
231ee9c90c7SKenneth D. Merry 	struct pass_softc *softc;
232ee9c90c7SKenneth D. Merry 
233ee9c90c7SKenneth D. Merry 	softc = (struct pass_softc *)periph->softc;
234ee9c90c7SKenneth D. Merry 
235ee9c90c7SKenneth D. Merry 	devstat_remove_entry(&softc->device_stats);
236ee9c90c7SKenneth D. Merry 
23773d26919SKenneth D. Merry 	destroy_dev(softc->dev);
23873d26919SKenneth D. Merry 
23976babe50SJustin T. Gibbs 	cam_extend_release(passperiphs, periph->unit_number);
24076babe50SJustin T. Gibbs 
24176babe50SJustin T. Gibbs 	if (bootverbose) {
24276babe50SJustin T. Gibbs 		xpt_print_path(periph->path);
24376babe50SJustin T. Gibbs 		printf("removing device entry\n");
24476babe50SJustin T. Gibbs 	}
245ee9c90c7SKenneth D. Merry 	free(softc, M_DEVBUF);
24676babe50SJustin T. Gibbs }
24776babe50SJustin T. Gibbs 
24876babe50SJustin T. Gibbs static void
24976babe50SJustin T. Gibbs passasync(void *callback_arg, u_int32_t code,
25076babe50SJustin T. Gibbs 	  struct cam_path *path, void *arg)
25176babe50SJustin T. Gibbs {
25276babe50SJustin T. Gibbs 	struct cam_periph *periph;
25376babe50SJustin T. Gibbs 
25476babe50SJustin T. Gibbs 	periph = (struct cam_periph *)callback_arg;
25576babe50SJustin T. Gibbs 
25676babe50SJustin T. Gibbs 	switch (code) {
25776babe50SJustin T. Gibbs 	case AC_FOUND_DEVICE:
25876babe50SJustin T. Gibbs 	{
25976babe50SJustin T. Gibbs 		struct ccb_getdev *cgd;
26076babe50SJustin T. Gibbs 		cam_status status;
26176babe50SJustin T. Gibbs 
26276babe50SJustin T. Gibbs 		cgd = (struct ccb_getdev *)arg;
26376babe50SJustin T. Gibbs 
26476babe50SJustin T. Gibbs 		/*
26576babe50SJustin T. Gibbs 		 * Allocate a peripheral instance for
26676babe50SJustin T. Gibbs 		 * this device and start the probe
26776babe50SJustin T. Gibbs 		 * process.
26876babe50SJustin T. Gibbs 		 */
269ee9c90c7SKenneth D. Merry 		status = cam_periph_alloc(passregister, passoninvalidate,
270ee9c90c7SKenneth D. Merry 					  passcleanup, passstart, "pass",
271ee9c90c7SKenneth D. Merry 					  CAM_PERIPH_BIO, cgd->ccb_h.path,
272ee9c90c7SKenneth D. Merry 					  passasync, AC_FOUND_DEVICE, cgd);
27376babe50SJustin T. Gibbs 
27476babe50SJustin T. Gibbs 		if (status != CAM_REQ_CMP
27576babe50SJustin T. Gibbs 		 && status != CAM_REQ_INPROG)
27676babe50SJustin T. Gibbs 			printf("passasync: Unable to attach new device "
27776babe50SJustin T. Gibbs 				"due to status 0x%x\n", status);
27876babe50SJustin T. Gibbs 
27976babe50SJustin T. Gibbs 		break;
28076babe50SJustin T. Gibbs 	}
28176babe50SJustin T. Gibbs 	default:
282516871c6SJustin T. Gibbs 		cam_periph_async(periph, code, path, arg);
28376babe50SJustin T. Gibbs 		break;
28476babe50SJustin T. Gibbs 	}
28576babe50SJustin T. Gibbs }
28676babe50SJustin T. Gibbs 
28776babe50SJustin T. Gibbs static cam_status
28876babe50SJustin T. Gibbs passregister(struct cam_periph *periph, void *arg)
28976babe50SJustin T. Gibbs {
29076babe50SJustin T. Gibbs 	struct pass_softc *softc;
29176babe50SJustin T. Gibbs 	struct ccb_setasync csa;
29276babe50SJustin T. Gibbs 	struct ccb_getdev *cgd;
29376babe50SJustin T. Gibbs 
29476babe50SJustin T. Gibbs 	cgd = (struct ccb_getdev *)arg;
29576babe50SJustin T. Gibbs 	if (periph == NULL) {
29676babe50SJustin T. Gibbs 		printf("passregister: periph was NULL!!\n");
29776babe50SJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
29876babe50SJustin T. Gibbs 	}
29976babe50SJustin T. Gibbs 
30076babe50SJustin T. Gibbs 	if (cgd == NULL) {
30176babe50SJustin T. Gibbs 		printf("passregister: no getdev CCB, can't register device\n");
30276babe50SJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
30376babe50SJustin T. Gibbs 	}
30476babe50SJustin T. Gibbs 
30576babe50SJustin T. Gibbs 	softc = (struct pass_softc *)malloc(sizeof(*softc),
30676babe50SJustin T. Gibbs 					    M_DEVBUF, M_NOWAIT);
30776babe50SJustin T. Gibbs 
30876babe50SJustin T. Gibbs 	if (softc == NULL) {
30976babe50SJustin T. Gibbs 		printf("passregister: Unable to probe new device. "
31076babe50SJustin T. Gibbs 		       "Unable to allocate softc\n");
31176babe50SJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
31276babe50SJustin T. Gibbs 	}
31376babe50SJustin T. Gibbs 
31476babe50SJustin T. Gibbs 	bzero(softc, sizeof(*softc));
31576babe50SJustin T. Gibbs 	softc->state = PASS_STATE_NORMAL;
31610b6172aSMatt Jacob 	softc->pd_type = SID_TYPE(&cgd->inq_data);
3178177437dSPoul-Henning Kamp 	bioq_init(&softc->bio_queue);
31876babe50SJustin T. Gibbs 
31976babe50SJustin T. Gibbs 	periph->softc = softc;
32076babe50SJustin T. Gibbs 
32176babe50SJustin T. Gibbs 	cam_extend_set(passperiphs, periph->unit_number, periph);
32276babe50SJustin T. Gibbs 	/*
32376babe50SJustin T. Gibbs 	 * We pass in 0 for a blocksize, since we don't
32476babe50SJustin T. Gibbs 	 * know what the blocksize of this device is, if
32576babe50SJustin T. Gibbs 	 * it even has a blocksize.
32676babe50SJustin T. Gibbs 	 */
32776babe50SJustin T. Gibbs 	devstat_add_entry(&softc->device_stats, "pass", periph->unit_number,
32876babe50SJustin T. Gibbs 			  0, DEVSTAT_NO_BLOCKSIZE | DEVSTAT_NO_ORDERED_TAGS,
32910b6172aSMatt Jacob 			  softc->pd_type |
33076babe50SJustin T. Gibbs 			  DEVSTAT_TYPE_IF_SCSI |
3312a888f93SKenneth D. Merry 			  DEVSTAT_TYPE_PASS,
3322a888f93SKenneth D. Merry 			  DEVSTAT_PRIORITY_PASS);
33373d26919SKenneth D. Merry 
33473d26919SKenneth D. Merry 	/* Register the device */
33573d26919SKenneth D. Merry 	softc->dev = make_dev(&pass_cdevsw, periph->unit_number, UID_ROOT,
33673d26919SKenneth D. Merry 			      GID_OPERATOR, 0600, "%s%d", periph->periph_name,
33773d26919SKenneth D. Merry 			      periph->unit_number);
33873d26919SKenneth D. Merry 
33976babe50SJustin T. Gibbs 	/*
34076babe50SJustin T. Gibbs 	 * Add an async callback so that we get
34176babe50SJustin T. Gibbs 	 * notified if this device goes away.
34276babe50SJustin T. Gibbs 	 */
34376babe50SJustin T. Gibbs 	xpt_setup_ccb(&csa.ccb_h, periph->path, /* priority */ 5);
34476babe50SJustin T. Gibbs 	csa.ccb_h.func_code = XPT_SASYNC_CB;
34576babe50SJustin T. Gibbs 	csa.event_enable = AC_LOST_DEVICE;
34676babe50SJustin T. Gibbs 	csa.callback = passasync;
34776babe50SJustin T. Gibbs 	csa.callback_arg = periph;
34876babe50SJustin T. Gibbs 	xpt_action((union ccb *)&csa);
34976babe50SJustin T. Gibbs 
35076babe50SJustin T. Gibbs 	if (bootverbose)
35176babe50SJustin T. Gibbs 		xpt_announce_periph(periph, NULL);
35276babe50SJustin T. Gibbs 
35376babe50SJustin T. Gibbs 	return(CAM_REQ_CMP);
35476babe50SJustin T. Gibbs }
35576babe50SJustin T. Gibbs 
35676babe50SJustin T. Gibbs static int
35776babe50SJustin T. Gibbs passopen(dev_t dev, int flags, int fmt, struct proc *p)
35876babe50SJustin T. Gibbs {
35976babe50SJustin T. Gibbs 	struct cam_periph *periph;
36076babe50SJustin T. Gibbs 	struct pass_softc *softc;
36176babe50SJustin T. Gibbs 	int unit, error;
362ee9c90c7SKenneth D. Merry 	int s;
36376babe50SJustin T. Gibbs 
36476babe50SJustin T. Gibbs 	error = 0; /* default to no error */
36576babe50SJustin T. Gibbs 
36676babe50SJustin T. Gibbs 	/* unit = dkunit(dev); */
36776babe50SJustin T. Gibbs 	/* XXX KDM fix this */
36876babe50SJustin T. Gibbs 	unit = minor(dev) & 0xff;
36976babe50SJustin T. Gibbs 
37076babe50SJustin T. Gibbs 	periph = cam_extend_get(passperiphs, unit);
37176babe50SJustin T. Gibbs 
37276babe50SJustin T. Gibbs 	if (periph == NULL)
37376babe50SJustin T. Gibbs 		return (ENXIO);
37476babe50SJustin T. Gibbs 
37576babe50SJustin T. Gibbs 	softc = (struct pass_softc *)periph->softc;
37676babe50SJustin T. Gibbs 
377ee9c90c7SKenneth D. Merry 	s = splsoftcam();
378ee9c90c7SKenneth D. Merry 	if (softc->flags & PASS_FLAG_INVALID) {
379ee9c90c7SKenneth D. Merry 		splx(s);
38076babe50SJustin T. Gibbs 		return(ENXIO);
381ee9c90c7SKenneth D. Merry 	}
38222b9c86cSKenneth D. Merry 
38322b9c86cSKenneth D. Merry 	/*
38422b9c86cSKenneth D. Merry 	 * Don't allow access when we're running at a high securelvel.
38522b9c86cSKenneth D. Merry 	 */
38622b9c86cSKenneth D. Merry 	if (securelevel > 1) {
387ee9c90c7SKenneth D. Merry 		splx(s);
38822b9c86cSKenneth D. Merry 		return(EPERM);
38922b9c86cSKenneth D. Merry 	}
39076babe50SJustin T. Gibbs 
39176babe50SJustin T. Gibbs 	/*
39266a0780eSKenneth D. Merry 	 * Only allow read-write access.
39366a0780eSKenneth D. Merry 	 */
39422b9c86cSKenneth D. Merry 	if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0)) {
39522b9c86cSKenneth D. Merry 		splx(s);
39666a0780eSKenneth D. Merry 		return(EPERM);
39722b9c86cSKenneth D. Merry 	}
39866a0780eSKenneth D. Merry 
39966a0780eSKenneth D. Merry 	/*
40076babe50SJustin T. Gibbs 	 * We don't allow nonblocking access.
40176babe50SJustin T. Gibbs 	 */
40276babe50SJustin T. Gibbs 	if ((flags & O_NONBLOCK) != 0) {
40322b9c86cSKenneth D. Merry 		xpt_print_path(periph->path);
40422b9c86cSKenneth D. Merry 		printf("can't do nonblocking accesss\n");
40522b9c86cSKenneth D. Merry 		splx(s);
40622b9c86cSKenneth D. Merry 		return(EINVAL);
40776babe50SJustin T. Gibbs 	}
40876babe50SJustin T. Gibbs 
40922b9c86cSKenneth D. Merry 	if ((error = cam_periph_lock(periph, PRIBIO | PCATCH)) != 0) {
41022b9c86cSKenneth D. Merry 		splx(s);
41176babe50SJustin T. Gibbs 		return (error);
41222b9c86cSKenneth D. Merry 	}
41322b9c86cSKenneth D. Merry 
41422b9c86cSKenneth D. Merry 	splx(s);
41576babe50SJustin T. Gibbs 
41676babe50SJustin T. Gibbs 	if ((softc->flags & PASS_FLAG_OPEN) == 0) {
41776babe50SJustin T. Gibbs 		if (cam_periph_acquire(periph) != CAM_REQ_CMP)
41876babe50SJustin T. Gibbs 			return(ENXIO);
41976babe50SJustin T. Gibbs 		softc->flags |= PASS_FLAG_OPEN;
42076babe50SJustin T. Gibbs 	}
42176babe50SJustin T. Gibbs 
42276babe50SJustin T. Gibbs 	cam_periph_unlock(periph);
42376babe50SJustin T. Gibbs 
42476babe50SJustin T. Gibbs 	return (error);
42576babe50SJustin T. Gibbs }
42676babe50SJustin T. Gibbs 
42776babe50SJustin T. Gibbs static int
42876babe50SJustin T. Gibbs passclose(dev_t dev, int flag, int fmt, struct proc *p)
42976babe50SJustin T. Gibbs {
43076babe50SJustin T. Gibbs 	struct 	cam_periph *periph;
43176babe50SJustin T. Gibbs 	struct	pass_softc *softc;
43276babe50SJustin T. Gibbs 	int	unit, error;
43376babe50SJustin T. Gibbs 
43476babe50SJustin T. Gibbs 	/* unit = dkunit(dev); */
43576babe50SJustin T. Gibbs 	/* XXX KDM fix this */
43676babe50SJustin T. Gibbs 	unit = minor(dev) & 0xff;
43776babe50SJustin T. Gibbs 
43876babe50SJustin T. Gibbs 	periph = cam_extend_get(passperiphs, unit);
43976babe50SJustin T. Gibbs 	if (periph == NULL)
44076babe50SJustin T. Gibbs 		return (ENXIO);
44176babe50SJustin T. Gibbs 
44276babe50SJustin T. Gibbs 	softc = (struct pass_softc *)periph->softc;
44376babe50SJustin T. Gibbs 
44476babe50SJustin T. Gibbs 	if ((error = cam_periph_lock(periph, PRIBIO)) != 0)
44576babe50SJustin T. Gibbs 		return (error);
44676babe50SJustin T. Gibbs 
44776babe50SJustin T. Gibbs 	softc->flags &= ~PASS_FLAG_OPEN;
44876babe50SJustin T. Gibbs 
44976babe50SJustin T. Gibbs 	cam_periph_unlock(periph);
45076babe50SJustin T. Gibbs 	cam_periph_release(periph);
45176babe50SJustin T. Gibbs 
45276babe50SJustin T. Gibbs 	return (0);
45376babe50SJustin T. Gibbs }
45476babe50SJustin T. Gibbs 
45576babe50SJustin T. Gibbs /*
45676babe50SJustin T. Gibbs  * Actually translate the requested transfer into one the physical driver
45776babe50SJustin T. Gibbs  * can understand.  The transfer is described by a buf and will include
45876babe50SJustin T. Gibbs  * only one physical transfer.
45976babe50SJustin T. Gibbs  */
46076babe50SJustin T. Gibbs static void
4618177437dSPoul-Henning Kamp passstrategy(struct bio *bp)
46276babe50SJustin T. Gibbs {
46376babe50SJustin T. Gibbs 	struct cam_periph *periph;
46476babe50SJustin T. Gibbs 	struct pass_softc *softc;
46576babe50SJustin T. Gibbs 	u_int  unit;
46676babe50SJustin T. Gibbs 	int    s;
46776babe50SJustin T. Gibbs 
46876babe50SJustin T. Gibbs 	/*
46976babe50SJustin T. Gibbs 	 * The read/write interface for the passthrough driver doesn't
47076babe50SJustin T. Gibbs 	 * really work right now.  So, we just pass back EINVAL to tell the
47176babe50SJustin T. Gibbs 	 * user to go away.
47276babe50SJustin T. Gibbs 	 */
4738177437dSPoul-Henning Kamp 	bp->bio_error = EINVAL;
47476babe50SJustin T. Gibbs 	goto bad;
47576babe50SJustin T. Gibbs 
4768177437dSPoul-Henning Kamp 	/* unit = dkunit(bp->bio_dev); */
47776babe50SJustin T. Gibbs 	/* XXX KDM fix this */
4788177437dSPoul-Henning Kamp 	unit = minor(bp->bio_dev) & 0xff;
47976babe50SJustin T. Gibbs 
48076babe50SJustin T. Gibbs 	periph = cam_extend_get(passperiphs, unit);
48176babe50SJustin T. Gibbs 	if (periph == NULL) {
4828177437dSPoul-Henning Kamp 		bp->bio_error = ENXIO;
48376babe50SJustin T. Gibbs 		goto bad;
48476babe50SJustin T. Gibbs 	}
48576babe50SJustin T. Gibbs 	softc = (struct pass_softc *)periph->softc;
48676babe50SJustin T. Gibbs 
48776babe50SJustin T. Gibbs 	/*
48876babe50SJustin T. Gibbs 	 * Odd number of bytes or negative offset
48976babe50SJustin T. Gibbs 	 */
49076babe50SJustin T. Gibbs 	/* valid request?  */
4918177437dSPoul-Henning Kamp 	if (bp->bio_blkno < 0) {
4928177437dSPoul-Henning Kamp 		bp->bio_error = EINVAL;
49376babe50SJustin T. Gibbs 		goto bad;
49476babe50SJustin T. Gibbs         }
49576babe50SJustin T. Gibbs 
49676babe50SJustin T. Gibbs 	/*
49776babe50SJustin T. Gibbs 	 * Mask interrupts so that the pack cannot be invalidated until
49876babe50SJustin T. Gibbs 	 * after we are in the queue.  Otherwise, we might not properly
49976babe50SJustin T. Gibbs 	 * clean up one of the buffers.
50076babe50SJustin T. Gibbs 	 */
50176babe50SJustin T. Gibbs 	s = splbio();
50276babe50SJustin T. Gibbs 
5038177437dSPoul-Henning Kamp 	bioq_insert_tail(&softc->bio_queue, bp);
50476babe50SJustin T. Gibbs 
50576babe50SJustin T. Gibbs 	splx(s);
50676babe50SJustin T. Gibbs 
50776babe50SJustin T. Gibbs 	/*
50876babe50SJustin T. Gibbs 	 * Schedule ourselves for performing the work.
50976babe50SJustin T. Gibbs 	 */
51076babe50SJustin T. Gibbs 	xpt_schedule(periph, /* XXX priority */1);
51176babe50SJustin T. Gibbs 
51276babe50SJustin T. Gibbs 	return;
51376babe50SJustin T. Gibbs bad:
5148177437dSPoul-Henning Kamp 	bp->bio_flags |= BIO_ERROR;
51576babe50SJustin T. Gibbs 
51676babe50SJustin T. Gibbs 	/*
51776babe50SJustin T. Gibbs 	 * Correctly set the buf to indicate a completed xfer
51876babe50SJustin T. Gibbs 	 */
5198177437dSPoul-Henning Kamp 	bp->bio_resid = bp->bio_bcount;
52076babe50SJustin T. Gibbs 	biodone(bp);
52176babe50SJustin T. Gibbs 	return;
52276babe50SJustin T. Gibbs }
52376babe50SJustin T. Gibbs 
52476babe50SJustin T. Gibbs static void
52576babe50SJustin T. Gibbs passstart(struct cam_periph *periph, union ccb *start_ccb)
52676babe50SJustin T. Gibbs {
52776babe50SJustin T. Gibbs 	struct pass_softc *softc;
52876babe50SJustin T. Gibbs 	int s;
52976babe50SJustin T. Gibbs 
53076babe50SJustin T. Gibbs 	softc = (struct pass_softc *)periph->softc;
53176babe50SJustin T. Gibbs 
53276babe50SJustin T. Gibbs 	switch (softc->state) {
53376babe50SJustin T. Gibbs 	case PASS_STATE_NORMAL:
53476babe50SJustin T. Gibbs 	{
5358177437dSPoul-Henning Kamp 		struct bio *bp;
53676babe50SJustin T. Gibbs 
53776babe50SJustin T. Gibbs 		s = splbio();
5388177437dSPoul-Henning Kamp 		bp = bioq_first(&softc->bio_queue);
53976babe50SJustin T. Gibbs 		if (periph->immediate_priority <= periph->pinfo.priority) {
54076babe50SJustin T. Gibbs 			start_ccb->ccb_h.ccb_type = PASS_CCB_WAITING;
54176babe50SJustin T. Gibbs 			SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
54276babe50SJustin T. Gibbs 					  periph_links.sle);
54376babe50SJustin T. Gibbs 			periph->immediate_priority = CAM_PRIORITY_NONE;
54476babe50SJustin T. Gibbs 			splx(s);
54576babe50SJustin T. Gibbs 			wakeup(&periph->ccb_list);
54676babe50SJustin T. Gibbs 		} else if (bp == NULL) {
54776babe50SJustin T. Gibbs 			splx(s);
54876babe50SJustin T. Gibbs 			xpt_release_ccb(start_ccb);
54976babe50SJustin T. Gibbs 		} else {
55076babe50SJustin T. Gibbs 
5518177437dSPoul-Henning Kamp 			bioq_remove(&softc->bio_queue, bp);
55276babe50SJustin T. Gibbs 
55376babe50SJustin T. Gibbs 			devstat_start_transaction(&softc->device_stats);
55476babe50SJustin T. Gibbs 
55576babe50SJustin T. Gibbs 			/*
55676babe50SJustin T. Gibbs 			 * XXX JGibbs -
55776babe50SJustin T. Gibbs 			 * Interpret the contents of the bp as a CCB
55876babe50SJustin T. Gibbs 			 * and pass it to a routine shared by our ioctl
55976babe50SJustin T. Gibbs 			 * code and passtart.
56076babe50SJustin T. Gibbs 			 * For now, just biodone it with EIO so we don't
56176babe50SJustin T. Gibbs 			 * hang.
56276babe50SJustin T. Gibbs 			 */
5638177437dSPoul-Henning Kamp 			bp->bio_error = EIO;
5648177437dSPoul-Henning Kamp 			bp->bio_flags |= BIO_ERROR;
5658177437dSPoul-Henning Kamp 			bp->bio_resid = bp->bio_bcount;
56676babe50SJustin T. Gibbs 			biodone(bp);
5678177437dSPoul-Henning Kamp 			bp = bioq_first(&softc->bio_queue);
56876babe50SJustin T. Gibbs 			splx(s);
56976babe50SJustin T. Gibbs 
57076babe50SJustin T. Gibbs 			xpt_action(start_ccb);
57176babe50SJustin T. Gibbs 
57276babe50SJustin T. Gibbs 		}
57376babe50SJustin T. Gibbs 		if (bp != NULL) {
57476babe50SJustin T. Gibbs 			/* Have more work to do, so ensure we stay scheduled */
57576babe50SJustin T. Gibbs 			xpt_schedule(periph, /* XXX priority */1);
57676babe50SJustin T. Gibbs 		}
57776babe50SJustin T. Gibbs 		break;
57876babe50SJustin T. Gibbs 	}
57976babe50SJustin T. Gibbs 	}
58076babe50SJustin T. Gibbs }
58176babe50SJustin T. Gibbs static void
58276babe50SJustin T. Gibbs passdone(struct cam_periph *periph, union ccb *done_ccb)
58376babe50SJustin T. Gibbs {
58476babe50SJustin T. Gibbs 	struct pass_softc *softc;
58576babe50SJustin T. Gibbs 	struct ccb_scsiio *csio;
58676babe50SJustin T. Gibbs 
58776babe50SJustin T. Gibbs 	softc = (struct pass_softc *)periph->softc;
58876babe50SJustin T. Gibbs 	csio = &done_ccb->csio;
58976babe50SJustin T. Gibbs 	switch (csio->ccb_h.ccb_type) {
59076babe50SJustin T. Gibbs 	case PASS_CCB_BUFFER_IO:
59176babe50SJustin T. Gibbs 	{
5928177437dSPoul-Henning Kamp 		struct bio		*bp;
59376babe50SJustin T. Gibbs 		cam_status		status;
59476babe50SJustin T. Gibbs 		u_int8_t		scsi_status;
59576babe50SJustin T. Gibbs 		devstat_trans_flags	ds_flags;
59676babe50SJustin T. Gibbs 
59776babe50SJustin T. Gibbs 		status = done_ccb->ccb_h.status;
59876babe50SJustin T. Gibbs 		scsi_status = done_ccb->csio.scsi_status;
5998177437dSPoul-Henning Kamp 		bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
60076babe50SJustin T. Gibbs 		/* XXX handle errors */
60176babe50SJustin T. Gibbs 		if (!(((status & CAM_STATUS_MASK) == CAM_REQ_CMP)
60276babe50SJustin T. Gibbs 		  && (scsi_status == SCSI_STATUS_OK))) {
60376babe50SJustin T. Gibbs 			int error;
60476babe50SJustin T. Gibbs 
60576babe50SJustin T. Gibbs 			if ((error = passerror(done_ccb, 0, 0)) == ERESTART) {
60676babe50SJustin T. Gibbs 				/*
60776babe50SJustin T. Gibbs 				 * A retry was scheuled, so
60876babe50SJustin T. Gibbs 				 * just return.
60976babe50SJustin T. Gibbs 				 */
61076babe50SJustin T. Gibbs 				return;
61176babe50SJustin T. Gibbs 			}
61276babe50SJustin T. Gibbs 
61376babe50SJustin T. Gibbs 			/*
61476babe50SJustin T. Gibbs 			 * XXX unfreeze the queue after we complete
61576babe50SJustin T. Gibbs 			 * the abort process
61676babe50SJustin T. Gibbs 			 */
6178177437dSPoul-Henning Kamp 			bp->bio_error = error;
6188177437dSPoul-Henning Kamp 			bp->bio_flags |= BIO_ERROR;
61976babe50SJustin T. Gibbs 		}
62076babe50SJustin T. Gibbs 
62176babe50SJustin T. Gibbs 		if ((done_ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
62276babe50SJustin T. Gibbs 			ds_flags = DEVSTAT_READ;
62376babe50SJustin T. Gibbs 		else if ((done_ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
62476babe50SJustin T. Gibbs 			ds_flags = DEVSTAT_WRITE;
62576babe50SJustin T. Gibbs 		else
62676babe50SJustin T. Gibbs 			ds_flags = DEVSTAT_NO_DATA;
62776babe50SJustin T. Gibbs 
6288177437dSPoul-Henning Kamp 		devstat_end_transaction_bio(&softc->device_stats, bp);
62976babe50SJustin T. Gibbs 		biodone(bp);
63076babe50SJustin T. Gibbs 		break;
63176babe50SJustin T. Gibbs 	}
63276babe50SJustin T. Gibbs 	case PASS_CCB_WAITING:
63376babe50SJustin T. Gibbs 	{
63476babe50SJustin T. Gibbs 		/* Caller will release the CCB */
63576babe50SJustin T. Gibbs 		wakeup(&done_ccb->ccb_h.cbfcnp);
63676babe50SJustin T. Gibbs 		return;
63776babe50SJustin T. Gibbs 	}
63876babe50SJustin T. Gibbs 	}
63976babe50SJustin T. Gibbs 	xpt_release_ccb(done_ccb);
64076babe50SJustin T. Gibbs }
64176babe50SJustin T. Gibbs 
64276babe50SJustin T. Gibbs static int
64376babe50SJustin T. Gibbs passioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
64476babe50SJustin T. Gibbs {
64576babe50SJustin T. Gibbs 	struct 	cam_periph *periph;
64676babe50SJustin T. Gibbs 	struct	pass_softc *softc;
64776babe50SJustin T. Gibbs 	u_int8_t unit;
64876babe50SJustin T. Gibbs 	int      error;
64976babe50SJustin T. Gibbs 
65076babe50SJustin T. Gibbs 
65176babe50SJustin T. Gibbs 	/* unit = dkunit(dev); */
65276babe50SJustin T. Gibbs 	/* XXX KDM fix this */
65376babe50SJustin T. Gibbs 	unit = minor(dev) & 0xff;
65476babe50SJustin T. Gibbs 
65576babe50SJustin T. Gibbs 	periph = cam_extend_get(passperiphs, unit);
65676babe50SJustin T. Gibbs 
65776babe50SJustin T. Gibbs 	if (periph == NULL)
65876babe50SJustin T. Gibbs 		return(ENXIO);
65976babe50SJustin T. Gibbs 
66076babe50SJustin T. Gibbs 	softc = (struct pass_softc *)periph->softc;
66176babe50SJustin T. Gibbs 
66276babe50SJustin T. Gibbs 	error = 0;
66376babe50SJustin T. Gibbs 
66476babe50SJustin T. Gibbs 	switch (cmd) {
66576babe50SJustin T. Gibbs 
66676babe50SJustin T. Gibbs 	case CAMIOCOMMAND:
66776babe50SJustin T. Gibbs 	{
66876babe50SJustin T. Gibbs 		union ccb *inccb;
66976babe50SJustin T. Gibbs 		union ccb *ccb;
6709deea857SKenneth D. Merry 		int ccb_malloced;
67176babe50SJustin T. Gibbs 
67276babe50SJustin T. Gibbs 		inccb = (union ccb *)addr;
6739deea857SKenneth D. Merry 
6749deea857SKenneth D. Merry 		/*
6759deea857SKenneth D. Merry 		 * Some CCB types, like scan bus and scan lun can only go
6769deea857SKenneth D. Merry 		 * through the transport layer device.
6779deea857SKenneth D. Merry 		 */
6789deea857SKenneth D. Merry 		if (inccb->ccb_h.func_code & XPT_FC_XPT_ONLY) {
6799deea857SKenneth D. Merry 			xpt_print_path(periph->path);
6809deea857SKenneth D. Merry 			printf("CCB function code %#x is restricted to the "
6819deea857SKenneth D. Merry 			       "XPT device\n", inccb->ccb_h.func_code);
6829deea857SKenneth D. Merry 			error = ENODEV;
6839deea857SKenneth D. Merry 			break;
6849deea857SKenneth D. Merry 		}
6859deea857SKenneth D. Merry 
6869deea857SKenneth D. Merry 		/*
6879deea857SKenneth D. Merry 		 * Non-immediate CCBs need a CCB from the per-device pool
6889deea857SKenneth D. Merry 		 * of CCBs, which is scheduled by the transport layer.
6899deea857SKenneth D. Merry 		 * Immediate CCBs and user-supplied CCBs should just be
6909deea857SKenneth D. Merry 		 * malloced.
6919deea857SKenneth D. Merry 		 */
6929deea857SKenneth D. Merry 		if ((inccb->ccb_h.func_code & XPT_FC_QUEUED)
6939deea857SKenneth D. Merry 		 && ((inccb->ccb_h.func_code & XPT_FC_USER_CCB) == 0)) {
6949deea857SKenneth D. Merry 			ccb = cam_periph_getccb(periph,
6959deea857SKenneth D. Merry 						inccb->ccb_h.pinfo.priority);
6969deea857SKenneth D. Merry 			ccb_malloced = 0;
6979deea857SKenneth D. Merry 		} else {
6989deea857SKenneth D. Merry 			ccb = xpt_alloc_ccb();
6999deea857SKenneth D. Merry 
7009deea857SKenneth D. Merry 			if (ccb != NULL)
7019deea857SKenneth D. Merry 				xpt_setup_ccb(&ccb->ccb_h, periph->path,
7029deea857SKenneth D. Merry 					      inccb->ccb_h.pinfo.priority);
7039deea857SKenneth D. Merry 			ccb_malloced = 1;
7049deea857SKenneth D. Merry 		}
7059deea857SKenneth D. Merry 
7069deea857SKenneth D. Merry 		if (ccb == NULL) {
7079deea857SKenneth D. Merry 			xpt_print_path(periph->path);
7089deea857SKenneth D. Merry 			printf("unable to allocate CCB\n");
7099deea857SKenneth D. Merry 			error = ENOMEM;
7109deea857SKenneth D. Merry 			break;
7119deea857SKenneth D. Merry 		}
71276babe50SJustin T. Gibbs 
71376babe50SJustin T. Gibbs 		error = passsendccb(periph, ccb, inccb);
71476babe50SJustin T. Gibbs 
7159deea857SKenneth D. Merry 		if (ccb_malloced)
7169deea857SKenneth D. Merry 			xpt_free_ccb(ccb);
7179deea857SKenneth D. Merry 		else
71876babe50SJustin T. Gibbs 			xpt_release_ccb(ccb);
71976babe50SJustin T. Gibbs 
72076babe50SJustin T. Gibbs 		break;
72176babe50SJustin T. Gibbs 	}
72276babe50SJustin T. Gibbs 	default:
72376babe50SJustin T. Gibbs 		error = cam_periph_ioctl(periph, cmd, addr, passerror);
72476babe50SJustin T. Gibbs 		break;
72576babe50SJustin T. Gibbs 	}
72676babe50SJustin T. Gibbs 
72776babe50SJustin T. Gibbs 	return(error);
72876babe50SJustin T. Gibbs }
72976babe50SJustin T. Gibbs 
73076babe50SJustin T. Gibbs /*
73176babe50SJustin T. Gibbs  * Generally, "ccb" should be the CCB supplied by the kernel.  "inccb"
73276babe50SJustin T. Gibbs  * should be the CCB that is copied in from the user.
73376babe50SJustin T. Gibbs  */
73476babe50SJustin T. Gibbs static int
73576babe50SJustin T. Gibbs passsendccb(struct cam_periph *periph, union ccb *ccb, union ccb *inccb)
73676babe50SJustin T. Gibbs {
73776babe50SJustin T. Gibbs 	struct pass_softc *softc;
73876babe50SJustin T. Gibbs 	struct cam_periph_map_info mapinfo;
73976babe50SJustin T. Gibbs 	int error, need_unmap;
74076babe50SJustin T. Gibbs 
74176babe50SJustin T. Gibbs 	softc = (struct pass_softc *)periph->softc;
74276babe50SJustin T. Gibbs 
74376babe50SJustin T. Gibbs 	need_unmap = 0;
74476babe50SJustin T. Gibbs 
74576babe50SJustin T. Gibbs 	/*
74676babe50SJustin T. Gibbs 	 * There are some fields in the CCB header that need to be
74776babe50SJustin T. Gibbs 	 * preserved, the rest we get from the user.
74876babe50SJustin T. Gibbs 	 */
74976babe50SJustin T. Gibbs 	xpt_merge_ccb(ccb, inccb);
75076babe50SJustin T. Gibbs 
75176babe50SJustin T. Gibbs 	/*
75276babe50SJustin T. Gibbs 	 * There's no way for the user to have a completion
75376babe50SJustin T. Gibbs 	 * function, so we put our own completion function in here.
75476babe50SJustin T. Gibbs 	 */
75576babe50SJustin T. Gibbs 	ccb->ccb_h.cbfcnp = passdone;
75676babe50SJustin T. Gibbs 
75776babe50SJustin T. Gibbs 	/*
75876babe50SJustin T. Gibbs 	 * We only attempt to map the user memory into kernel space
75976babe50SJustin T. Gibbs 	 * if they haven't passed in a physical memory pointer,
76076babe50SJustin T. Gibbs 	 * and if there is actually an I/O operation to perform.
76176babe50SJustin T. Gibbs 	 * Right now cam_periph_mapmem() only supports SCSI and device
76276babe50SJustin T. Gibbs 	 * match CCBs.  For the SCSI CCBs, we only pass the CCB in if
76376babe50SJustin T. Gibbs 	 * there's actually data to map.  cam_periph_mapmem() will do the
76476babe50SJustin T. Gibbs 	 * right thing, even if there isn't data to map, but since CCBs
76576babe50SJustin T. Gibbs 	 * without data are a reasonably common occurance (e.g. test unit
76676babe50SJustin T. Gibbs 	 * ready), it will save a few cycles if we check for it here.
76776babe50SJustin T. Gibbs 	 */
76876babe50SJustin T. Gibbs 	if (((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0)
76976babe50SJustin T. Gibbs 	 && (((ccb->ccb_h.func_code == XPT_SCSI_IO)
77076babe50SJustin T. Gibbs 	    && ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE))
77176babe50SJustin T. Gibbs 	  || (ccb->ccb_h.func_code == XPT_DEV_MATCH))) {
77276babe50SJustin T. Gibbs 
77376babe50SJustin T. Gibbs 		bzero(&mapinfo, sizeof(mapinfo));
77476babe50SJustin T. Gibbs 
77576babe50SJustin T. Gibbs 		error = cam_periph_mapmem(ccb, &mapinfo);
77676babe50SJustin T. Gibbs 
77776babe50SJustin T. Gibbs 		/*
77876babe50SJustin T. Gibbs 		 * cam_periph_mapmem returned an error, we can't continue.
77976babe50SJustin T. Gibbs 		 * Return the error to the user.
78076babe50SJustin T. Gibbs 		 */
78176babe50SJustin T. Gibbs 		if (error)
78276babe50SJustin T. Gibbs 			return(error);
78376babe50SJustin T. Gibbs 
78476babe50SJustin T. Gibbs 		/*
78576babe50SJustin T. Gibbs 		 * We successfully mapped the memory in, so we need to
78676babe50SJustin T. Gibbs 		 * unmap it when the transaction is done.
78776babe50SJustin T. Gibbs 		 */
78876babe50SJustin T. Gibbs 		need_unmap = 1;
78976babe50SJustin T. Gibbs 	}
79076babe50SJustin T. Gibbs 
79176babe50SJustin T. Gibbs 	/*
79276babe50SJustin T. Gibbs 	 * If the user wants us to perform any error recovery, then honor
79376babe50SJustin T. Gibbs 	 * that request.  Otherwise, it's up to the user to perform any
79476babe50SJustin T. Gibbs 	 * error recovery.
79576babe50SJustin T. Gibbs 	 */
79676babe50SJustin T. Gibbs 	error = cam_periph_runccb(ccb,
79776babe50SJustin T. Gibbs 				  (ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) ?
79876babe50SJustin T. Gibbs 				  passerror : NULL,
79976babe50SJustin T. Gibbs 				  /* cam_flags */ 0,
80050711c71SKenneth D. Merry 				  /* sense_flags */SF_RETRY_UA | SF_RETRY_SELTO,
80176babe50SJustin T. Gibbs 				  &softc->device_stats);
80276babe50SJustin T. Gibbs 
80376babe50SJustin T. Gibbs 	if (need_unmap != 0)
80476babe50SJustin T. Gibbs 		cam_periph_unmapmem(ccb, &mapinfo);
80576babe50SJustin T. Gibbs 
80676babe50SJustin T. Gibbs 	ccb->ccb_h.cbfcnp = NULL;
80776babe50SJustin T. Gibbs 	ccb->ccb_h.periph_priv = inccb->ccb_h.periph_priv;
80876babe50SJustin T. Gibbs 	bcopy(ccb, inccb, sizeof(union ccb));
80976babe50SJustin T. Gibbs 
81076babe50SJustin T. Gibbs 	return(error);
81176babe50SJustin T. Gibbs }
81276babe50SJustin T. Gibbs 
81376babe50SJustin T. Gibbs static int
81476babe50SJustin T. Gibbs passerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
81576babe50SJustin T. Gibbs {
81676babe50SJustin T. Gibbs 	struct cam_periph *periph;
81776babe50SJustin T. Gibbs 	struct pass_softc *softc;
81876babe50SJustin T. Gibbs 
81976babe50SJustin T. Gibbs 	periph = xpt_path_periph(ccb->ccb_h.path);
82076babe50SJustin T. Gibbs 	softc = (struct pass_softc *)periph->softc;
82176babe50SJustin T. Gibbs 
82276babe50SJustin T. Gibbs 	return(cam_periph_error(ccb, cam_flags, sense_flags,
82376babe50SJustin T. Gibbs 				 &softc->saved_ccb));
82476babe50SJustin T. Gibbs }
825