xref: /freebsd/sys/cam/scsi/scsi_pass.c (revision 4e2f199e0c9ada1b226f685a848abb1420a1ff8f)
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  *
274e2f199eSPoul-Henning Kamp  *      $Id: scsi_pass.c,v 1.10 1999/05/22 22:00:21 gibbs Exp $
2876babe50SJustin T. Gibbs  */
2976babe50SJustin T. Gibbs 
3076babe50SJustin T. Gibbs #include <sys/param.h>
3176babe50SJustin T. Gibbs #include <sys/queue.h>
3276babe50SJustin T. Gibbs #include <sys/systm.h>
3376babe50SJustin T. Gibbs #include <sys/kernel.h>
3476babe50SJustin T. Gibbs #include <sys/types.h>
3576babe50SJustin T. Gibbs #include <sys/buf.h>
3676babe50SJustin T. Gibbs #include <sys/dkbad.h>
3776babe50SJustin T. Gibbs #include <sys/disklabel.h>
3876babe50SJustin T. Gibbs #include <sys/diskslice.h>
3976babe50SJustin T. Gibbs #include <sys/malloc.h>
4076babe50SJustin T. Gibbs #include <sys/fcntl.h>
4176babe50SJustin T. Gibbs #include <sys/stat.h>
4276babe50SJustin T. Gibbs #include <sys/conf.h>
4376babe50SJustin T. Gibbs #include <sys/buf.h>
4476babe50SJustin T. Gibbs #include <sys/proc.h>
4576babe50SJustin T. Gibbs #include <sys/errno.h>
4676babe50SJustin T. Gibbs #include <sys/devicestat.h>
4776babe50SJustin T. Gibbs 
4876babe50SJustin T. Gibbs #include <cam/cam.h>
4976babe50SJustin T. Gibbs #include <cam/cam_ccb.h>
5076babe50SJustin T. Gibbs #include <cam/cam_extend.h>
5176babe50SJustin T. Gibbs #include <cam/cam_periph.h>
5276babe50SJustin T. Gibbs #include <cam/cam_xpt_periph.h>
5376babe50SJustin T. Gibbs #include <cam/cam_debug.h>
5476babe50SJustin T. Gibbs 
5576babe50SJustin T. Gibbs #include <cam/scsi/scsi_all.h>
5676babe50SJustin T. Gibbs #include <cam/scsi/scsi_message.h>
5776babe50SJustin T. Gibbs #include <cam/scsi/scsi_da.h>
5876babe50SJustin T. Gibbs #include <cam/scsi/scsi_pass.h>
5976babe50SJustin T. Gibbs 
6076babe50SJustin T. Gibbs typedef enum {
6176babe50SJustin T. Gibbs 	PASS_FLAG_OPEN			= 0x01,
6276babe50SJustin T. Gibbs 	PASS_FLAG_LOCKED		= 0x02,
6376babe50SJustin T. Gibbs 	PASS_FLAG_INVALID		= 0x04
6476babe50SJustin T. Gibbs } pass_flags;
6576babe50SJustin T. Gibbs 
6676babe50SJustin T. Gibbs typedef enum {
6776babe50SJustin T. Gibbs 	PASS_STATE_NORMAL
6876babe50SJustin T. Gibbs } pass_state;
6976babe50SJustin T. Gibbs 
7076babe50SJustin T. Gibbs typedef enum {
7176babe50SJustin T. Gibbs 	PASS_CCB_BUFFER_IO,
7276babe50SJustin T. Gibbs 	PASS_CCB_WAITING
7376babe50SJustin T. Gibbs } pass_ccb_types;
7476babe50SJustin T. Gibbs 
7576babe50SJustin T. Gibbs #define ccb_type	ppriv_field0
7676babe50SJustin T. Gibbs #define ccb_bp		ppriv_ptr1
7776babe50SJustin T. Gibbs 
7876babe50SJustin T. Gibbs struct pass_softc {
7976babe50SJustin T. Gibbs 	pass_state	state;
8076babe50SJustin T. Gibbs 	pass_flags	flags;
8176babe50SJustin T. Gibbs 	u_int8_t	pd_type;
8276babe50SJustin T. Gibbs 	struct		buf_queue_head buf_queue;
8376babe50SJustin T. Gibbs 	union ccb	saved_ccb;
8476babe50SJustin T. Gibbs 	struct devstat	device_stats;
8576babe50SJustin T. Gibbs #ifdef DEVFS
8676babe50SJustin T. Gibbs 	void		*pass_devfs_token;
8776babe50SJustin T. Gibbs 	void		*ctl_devfs_token;
8876babe50SJustin T. Gibbs #endif
8976babe50SJustin T. Gibbs };
9076babe50SJustin T. Gibbs 
9176babe50SJustin T. Gibbs #ifndef MIN
9276babe50SJustin T. Gibbs #define MIN(x,y) ((x<y) ? x : y)
9376babe50SJustin T. Gibbs #endif
9476babe50SJustin T. Gibbs 
9576babe50SJustin T. Gibbs #define PASS_CDEV_MAJOR 31
9676babe50SJustin T. Gibbs 
9776babe50SJustin T. Gibbs static	d_open_t	passopen;
9876babe50SJustin T. Gibbs static	d_close_t	passclose;
9976babe50SJustin T. Gibbs static	d_ioctl_t	passioctl;
10076babe50SJustin T. Gibbs static	d_strategy_t	passstrategy;
10176babe50SJustin T. Gibbs 
10276babe50SJustin T. Gibbs static	periph_init_t	passinit;
10376babe50SJustin T. Gibbs static	periph_ctor_t	passregister;
104ee9c90c7SKenneth D. Merry static	periph_oninv_t	passoninvalidate;
10576babe50SJustin T. Gibbs static	periph_dtor_t	passcleanup;
10676babe50SJustin T. Gibbs static	periph_start_t	passstart;
10776babe50SJustin T. Gibbs static	void		passasync(void *callback_arg, u_int32_t code,
10876babe50SJustin T. Gibbs 				  struct cam_path *path, void *arg);
10976babe50SJustin T. Gibbs static	void		passdone(struct cam_periph *periph,
11076babe50SJustin T. Gibbs 				 union ccb *done_ccb);
11176babe50SJustin T. Gibbs static	int		passerror(union ccb *ccb, u_int32_t cam_flags,
11276babe50SJustin T. Gibbs 				  u_int32_t sense_flags);
11376babe50SJustin T. Gibbs static 	int		passsendccb(struct cam_periph *periph, union ccb *ccb,
11476babe50SJustin T. Gibbs 				    union ccb *inccb);
11576babe50SJustin T. Gibbs 
11676babe50SJustin T. Gibbs static struct periph_driver passdriver =
11776babe50SJustin T. Gibbs {
11876babe50SJustin T. Gibbs 	passinit, "pass",
11976babe50SJustin T. Gibbs 	TAILQ_HEAD_INITIALIZER(passdriver.units), /* generation */ 0
12076babe50SJustin T. Gibbs };
12176babe50SJustin T. Gibbs 
12276babe50SJustin T. Gibbs DATA_SET(periphdriver_set, passdriver);
12376babe50SJustin T. Gibbs 
1244e2f199eSPoul-Henning Kamp static struct cdevsw pass_cdevsw = {
1254e2f199eSPoul-Henning Kamp 	/* open */	passopen,
1264e2f199eSPoul-Henning Kamp 	/* close */	passclose,
1274e2f199eSPoul-Henning Kamp 	/* read */	physread,
1284e2f199eSPoul-Henning Kamp 	/* write */	physwrite,
1294e2f199eSPoul-Henning Kamp 	/* ioctl */	passioctl,
1304e2f199eSPoul-Henning Kamp 	/* stop */	nostop,
1314e2f199eSPoul-Henning Kamp 	/* reset */	noreset,
1324e2f199eSPoul-Henning Kamp 	/* devtotty */	nodevtotty,
1334e2f199eSPoul-Henning Kamp 	/* poll */	nopoll,
1344e2f199eSPoul-Henning Kamp 	/* mmap */	nommap,
1354e2f199eSPoul-Henning Kamp 	/* strategy */	passstrategy,
1364e2f199eSPoul-Henning Kamp 	/* name */	"pass",
1374e2f199eSPoul-Henning Kamp 	/* parms */	noparms,
1384e2f199eSPoul-Henning Kamp 	/* maj */	PASS_CDEV_MAJOR,
1394e2f199eSPoul-Henning Kamp 	/* dump */	nodump,
1404e2f199eSPoul-Henning Kamp 	/* psize */	nopsize,
1414e2f199eSPoul-Henning Kamp 	/* flags */	0,
1424e2f199eSPoul-Henning Kamp 	/* maxio */	0,
1434e2f199eSPoul-Henning Kamp 	/* bmaj */	-1
14476babe50SJustin T. Gibbs };
14576babe50SJustin T. Gibbs 
14676babe50SJustin T. Gibbs static struct extend_array *passperiphs;
14776babe50SJustin T. Gibbs 
14876babe50SJustin T. Gibbs static void
14976babe50SJustin T. Gibbs passinit(void)
15076babe50SJustin T. Gibbs {
15176babe50SJustin T. Gibbs 	cam_status status;
15276babe50SJustin T. Gibbs 	struct cam_path *path;
15376babe50SJustin T. Gibbs 
15476babe50SJustin T. Gibbs 	/*
15576babe50SJustin T. Gibbs 	 * Create our extend array for storing the devices we attach to.
15676babe50SJustin T. Gibbs 	 */
15776babe50SJustin T. Gibbs 	passperiphs = cam_extend_new();
15876babe50SJustin T. Gibbs 	if (passperiphs == NULL) {
15976babe50SJustin T. Gibbs 		printf("passm: Failed to alloc extend array!\n");
16076babe50SJustin T. Gibbs 		return;
16176babe50SJustin T. Gibbs 	}
16276babe50SJustin T. Gibbs 
16376babe50SJustin T. Gibbs 	/*
16476babe50SJustin T. Gibbs 	 * Install a global async callback.  This callback will
16576babe50SJustin T. Gibbs 	 * receive async callbacks like "new device found".
16676babe50SJustin T. Gibbs 	 */
16776babe50SJustin T. Gibbs 	status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
16876babe50SJustin T. Gibbs 				 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
16976babe50SJustin T. Gibbs 
17076babe50SJustin T. Gibbs 	if (status == CAM_REQ_CMP) {
17176babe50SJustin T. Gibbs 		struct ccb_setasync csa;
17276babe50SJustin T. Gibbs 
17376babe50SJustin T. Gibbs                 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
17476babe50SJustin T. Gibbs                 csa.ccb_h.func_code = XPT_SASYNC_CB;
17576babe50SJustin T. Gibbs                 csa.event_enable = AC_FOUND_DEVICE;
17676babe50SJustin T. Gibbs                 csa.callback = passasync;
17776babe50SJustin T. Gibbs                 csa.callback_arg = NULL;
17876babe50SJustin T. Gibbs                 xpt_action((union ccb *)&csa);
17976babe50SJustin T. Gibbs 		status = csa.ccb_h.status;
18076babe50SJustin T. Gibbs                 xpt_free_path(path);
18176babe50SJustin T. Gibbs         }
18276babe50SJustin T. Gibbs 
18376babe50SJustin T. Gibbs 	if (status != CAM_REQ_CMP) {
18476babe50SJustin T. Gibbs 		printf("pass: Failed to attach master async callback "
18576babe50SJustin T. Gibbs 		       "due to status 0x%x!\n", status);
18676babe50SJustin T. Gibbs 	} else {
18776babe50SJustin T. Gibbs 		dev_t dev;
18876babe50SJustin T. Gibbs 
18976babe50SJustin T. Gibbs 		/* If we were successfull, register our devsw */
19076babe50SJustin T. Gibbs 		dev = makedev(PASS_CDEV_MAJOR, 0);
19176babe50SJustin T. Gibbs 		cdevsw_add(&dev, &pass_cdevsw, NULL);
19276babe50SJustin T. Gibbs 	}
19376babe50SJustin T. Gibbs 
19476babe50SJustin T. Gibbs }
19576babe50SJustin T. Gibbs 
19676babe50SJustin T. Gibbs static void
197ee9c90c7SKenneth D. Merry passoninvalidate(struct cam_periph *periph)
198ee9c90c7SKenneth D. Merry {
199ee9c90c7SKenneth D. Merry 	int s;
200ee9c90c7SKenneth D. Merry 	struct pass_softc *softc;
201ee9c90c7SKenneth D. Merry 	struct buf *q_bp;
202ee9c90c7SKenneth D. Merry 	struct ccb_setasync csa;
203ee9c90c7SKenneth D. Merry 
204ee9c90c7SKenneth D. Merry 	softc = (struct pass_softc *)periph->softc;
205ee9c90c7SKenneth D. Merry 
206ee9c90c7SKenneth D. Merry 	/*
207ee9c90c7SKenneth D. Merry 	 * De-register any async callbacks.
208ee9c90c7SKenneth D. Merry 	 */
209ee9c90c7SKenneth D. Merry 	xpt_setup_ccb(&csa.ccb_h, periph->path,
210ee9c90c7SKenneth D. Merry 		      /* priority */ 5);
211ee9c90c7SKenneth D. Merry 	csa.ccb_h.func_code = XPT_SASYNC_CB;
212ee9c90c7SKenneth D. Merry 	csa.event_enable = 0;
213ee9c90c7SKenneth D. Merry 	csa.callback = passasync;
214ee9c90c7SKenneth D. Merry 	csa.callback_arg = periph;
215ee9c90c7SKenneth D. Merry 	xpt_action((union ccb *)&csa);
216ee9c90c7SKenneth D. Merry 
217ee9c90c7SKenneth D. Merry 	softc->flags |= PASS_FLAG_INVALID;
218ee9c90c7SKenneth D. Merry 
219ee9c90c7SKenneth D. Merry 	/*
220ee9c90c7SKenneth D. Merry 	 * Although the oninvalidate() routines are always called at
221ee9c90c7SKenneth D. Merry 	 * splsoftcam, we need to be at splbio() here to keep the buffer
222ee9c90c7SKenneth D. Merry 	 * queue from being modified while we traverse it.
223ee9c90c7SKenneth D. Merry 	 */
224ee9c90c7SKenneth D. Merry 	s = splbio();
225ee9c90c7SKenneth D. Merry 
226ee9c90c7SKenneth D. Merry 	/*
227ee9c90c7SKenneth D. Merry 	 * Return all queued I/O with ENXIO.
228ee9c90c7SKenneth D. Merry 	 * XXX Handle any transactions queued to the card
229ee9c90c7SKenneth D. Merry 	 *     with XPT_ABORT_CCB.
230ee9c90c7SKenneth D. Merry 	 */
231ee9c90c7SKenneth D. Merry 	while ((q_bp = bufq_first(&softc->buf_queue)) != NULL){
232ee9c90c7SKenneth D. Merry 		bufq_remove(&softc->buf_queue, q_bp);
233ee9c90c7SKenneth D. Merry 		q_bp->b_resid = q_bp->b_bcount;
234ee9c90c7SKenneth D. Merry 		q_bp->b_error = ENXIO;
235ee9c90c7SKenneth D. Merry 		q_bp->b_flags |= B_ERROR;
236ee9c90c7SKenneth D. Merry 		biodone(q_bp);
237ee9c90c7SKenneth D. Merry 	}
238ee9c90c7SKenneth D. Merry 	splx(s);
239ee9c90c7SKenneth D. Merry 
240ee9c90c7SKenneth D. Merry 	if (bootverbose) {
241ee9c90c7SKenneth D. Merry 		xpt_print_path(periph->path);
242ee9c90c7SKenneth D. Merry 		printf("lost device\n");
243ee9c90c7SKenneth D. Merry 	}
244ee9c90c7SKenneth D. Merry 
245ee9c90c7SKenneth D. Merry }
246ee9c90c7SKenneth D. Merry 
247ee9c90c7SKenneth D. Merry static void
24876babe50SJustin T. Gibbs passcleanup(struct cam_periph *periph)
24976babe50SJustin T. Gibbs {
250ee9c90c7SKenneth D. Merry 	struct pass_softc *softc;
251ee9c90c7SKenneth D. Merry 
252ee9c90c7SKenneth D. Merry 	softc = (struct pass_softc *)periph->softc;
253ee9c90c7SKenneth D. Merry 
254ee9c90c7SKenneth D. Merry 	devstat_remove_entry(&softc->device_stats);
255ee9c90c7SKenneth D. Merry 
25676babe50SJustin T. Gibbs 	cam_extend_release(passperiphs, periph->unit_number);
25776babe50SJustin T. Gibbs 
25876babe50SJustin T. Gibbs 	if (bootverbose) {
25976babe50SJustin T. Gibbs 		xpt_print_path(periph->path);
26076babe50SJustin T. Gibbs 		printf("removing device entry\n");
26176babe50SJustin T. Gibbs 	}
262ee9c90c7SKenneth D. Merry 	free(softc, M_DEVBUF);
26376babe50SJustin T. Gibbs }
26476babe50SJustin T. Gibbs 
26576babe50SJustin T. Gibbs static void
26676babe50SJustin T. Gibbs passasync(void *callback_arg, u_int32_t code,
26776babe50SJustin T. Gibbs 	  struct cam_path *path, void *arg)
26876babe50SJustin T. Gibbs {
26976babe50SJustin T. Gibbs 	struct cam_periph *periph;
27076babe50SJustin T. Gibbs 
27176babe50SJustin T. Gibbs 	periph = (struct cam_periph *)callback_arg;
27276babe50SJustin T. Gibbs 
27376babe50SJustin T. Gibbs 	switch (code) {
27476babe50SJustin T. Gibbs 	case AC_FOUND_DEVICE:
27576babe50SJustin T. Gibbs 	{
27676babe50SJustin T. Gibbs 		struct ccb_getdev *cgd;
27776babe50SJustin T. Gibbs 		cam_status status;
27876babe50SJustin T. Gibbs 
27976babe50SJustin T. Gibbs 		cgd = (struct ccb_getdev *)arg;
28076babe50SJustin T. Gibbs 
28176babe50SJustin T. Gibbs 		/*
28276babe50SJustin T. Gibbs 		 * Allocate a peripheral instance for
28376babe50SJustin T. Gibbs 		 * this device and start the probe
28476babe50SJustin T. Gibbs 		 * process.
28576babe50SJustin T. Gibbs 		 */
286ee9c90c7SKenneth D. Merry 		status = cam_periph_alloc(passregister, passoninvalidate,
287ee9c90c7SKenneth D. Merry 					  passcleanup, passstart, "pass",
288ee9c90c7SKenneth D. Merry 					  CAM_PERIPH_BIO, cgd->ccb_h.path,
289ee9c90c7SKenneth D. Merry 					  passasync, AC_FOUND_DEVICE, cgd);
29076babe50SJustin T. Gibbs 
29176babe50SJustin T. Gibbs 		if (status != CAM_REQ_CMP
29276babe50SJustin T. Gibbs 		 && status != CAM_REQ_INPROG)
29376babe50SJustin T. Gibbs 			printf("passasync: Unable to attach new device "
29476babe50SJustin T. Gibbs 				"due to status 0x%x\n", status);
29576babe50SJustin T. Gibbs 
29676babe50SJustin T. Gibbs 		break;
29776babe50SJustin T. Gibbs 	}
29876babe50SJustin T. Gibbs 	default:
299516871c6SJustin T. Gibbs 		cam_periph_async(periph, code, path, arg);
30076babe50SJustin T. Gibbs 		break;
30176babe50SJustin T. Gibbs 	}
30276babe50SJustin T. Gibbs }
30376babe50SJustin T. Gibbs 
30476babe50SJustin T. Gibbs static cam_status
30576babe50SJustin T. Gibbs passregister(struct cam_periph *periph, void *arg)
30676babe50SJustin T. Gibbs {
30776babe50SJustin T. Gibbs 	struct pass_softc *softc;
30876babe50SJustin T. Gibbs 	struct ccb_setasync csa;
30976babe50SJustin T. Gibbs 	struct ccb_getdev *cgd;
31076babe50SJustin T. Gibbs 
31176babe50SJustin T. Gibbs 	cgd = (struct ccb_getdev *)arg;
31276babe50SJustin T. Gibbs 	if (periph == NULL) {
31376babe50SJustin T. Gibbs 		printf("passregister: periph was NULL!!\n");
31476babe50SJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
31576babe50SJustin T. Gibbs 	}
31676babe50SJustin T. Gibbs 
31776babe50SJustin T. Gibbs 	if (cgd == NULL) {
31876babe50SJustin T. Gibbs 		printf("passregister: no getdev CCB, can't register device\n");
31976babe50SJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
32076babe50SJustin T. Gibbs 	}
32176babe50SJustin T. Gibbs 
32276babe50SJustin T. Gibbs 	softc = (struct pass_softc *)malloc(sizeof(*softc),
32376babe50SJustin T. Gibbs 					    M_DEVBUF, M_NOWAIT);
32476babe50SJustin T. Gibbs 
32576babe50SJustin T. Gibbs 	if (softc == NULL) {
32676babe50SJustin T. Gibbs 		printf("passregister: Unable to probe new device. "
32776babe50SJustin T. Gibbs 		       "Unable to allocate softc\n");
32876babe50SJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
32976babe50SJustin T. Gibbs 	}
33076babe50SJustin T. Gibbs 
33176babe50SJustin T. Gibbs 	bzero(softc, sizeof(*softc));
33276babe50SJustin T. Gibbs 	softc->state = PASS_STATE_NORMAL;
33376babe50SJustin T. Gibbs 	softc->pd_type = cgd->pd_type;
33476babe50SJustin T. Gibbs 	bufq_init(&softc->buf_queue);
33576babe50SJustin T. Gibbs 
33676babe50SJustin T. Gibbs 	periph->softc = softc;
33776babe50SJustin T. Gibbs 
33876babe50SJustin T. Gibbs 	cam_extend_set(passperiphs, periph->unit_number, periph);
33976babe50SJustin T. Gibbs 	/*
34076babe50SJustin T. Gibbs 	 * We pass in 0 for a blocksize, since we don't
34176babe50SJustin T. Gibbs 	 * know what the blocksize of this device is, if
34276babe50SJustin T. Gibbs 	 * it even has a blocksize.
34376babe50SJustin T. Gibbs 	 */
34476babe50SJustin T. Gibbs 	devstat_add_entry(&softc->device_stats, "pass", periph->unit_number,
34576babe50SJustin T. Gibbs 			  0, DEVSTAT_NO_BLOCKSIZE | DEVSTAT_NO_ORDERED_TAGS,
34676babe50SJustin T. Gibbs 			  cgd->pd_type |
34776babe50SJustin T. Gibbs 			  DEVSTAT_TYPE_IF_SCSI |
3482a888f93SKenneth D. Merry 			  DEVSTAT_TYPE_PASS,
3492a888f93SKenneth D. Merry 			  DEVSTAT_PRIORITY_PASS);
35076babe50SJustin T. Gibbs 	/*
35176babe50SJustin T. Gibbs 	 * Add an async callback so that we get
35276babe50SJustin T. Gibbs 	 * notified if this device goes away.
35376babe50SJustin T. Gibbs 	 */
35476babe50SJustin T. Gibbs 	xpt_setup_ccb(&csa.ccb_h, periph->path, /* priority */ 5);
35576babe50SJustin T. Gibbs 	csa.ccb_h.func_code = XPT_SASYNC_CB;
35676babe50SJustin T. Gibbs 	csa.event_enable = AC_LOST_DEVICE;
35776babe50SJustin T. Gibbs 	csa.callback = passasync;
35876babe50SJustin T. Gibbs 	csa.callback_arg = periph;
35976babe50SJustin T. Gibbs 	xpt_action((union ccb *)&csa);
36076babe50SJustin T. Gibbs 
36176babe50SJustin T. Gibbs 	if (bootverbose)
36276babe50SJustin T. Gibbs 		xpt_announce_periph(periph, NULL);
36376babe50SJustin T. Gibbs 
36476babe50SJustin T. Gibbs 	return(CAM_REQ_CMP);
36576babe50SJustin T. Gibbs }
36676babe50SJustin T. Gibbs 
36776babe50SJustin T. Gibbs static int
36876babe50SJustin T. Gibbs passopen(dev_t dev, int flags, int fmt, struct proc *p)
36976babe50SJustin T. Gibbs {
37076babe50SJustin T. Gibbs 	struct cam_periph *periph;
37176babe50SJustin T. Gibbs 	struct pass_softc *softc;
37276babe50SJustin T. Gibbs 	int unit, error;
373ee9c90c7SKenneth D. Merry 	int s;
37476babe50SJustin T. Gibbs 
37576babe50SJustin T. Gibbs 	error = 0; /* default to no error */
37676babe50SJustin T. Gibbs 
37776babe50SJustin T. Gibbs 	/* unit = dkunit(dev); */
37876babe50SJustin T. Gibbs 	/* XXX KDM fix this */
37976babe50SJustin T. Gibbs 	unit = minor(dev) & 0xff;
38076babe50SJustin T. Gibbs 
38176babe50SJustin T. Gibbs 	periph = cam_extend_get(passperiphs, unit);
38276babe50SJustin T. Gibbs 
38376babe50SJustin T. Gibbs 	if (periph == NULL)
38476babe50SJustin T. Gibbs 		return (ENXIO);
38576babe50SJustin T. Gibbs 
38676babe50SJustin T. Gibbs 	softc = (struct pass_softc *)periph->softc;
38776babe50SJustin T. Gibbs 
388ee9c90c7SKenneth D. Merry 	s = splsoftcam();
389ee9c90c7SKenneth D. Merry 	if (softc->flags & PASS_FLAG_INVALID) {
390ee9c90c7SKenneth D. Merry 		splx(s);
39176babe50SJustin T. Gibbs 		return(ENXIO);
392ee9c90c7SKenneth D. Merry 	}
39322b9c86cSKenneth D. Merry 
39422b9c86cSKenneth D. Merry 	/*
39522b9c86cSKenneth D. Merry 	 * Don't allow access when we're running at a high securelvel.
39622b9c86cSKenneth D. Merry 	 */
39722b9c86cSKenneth D. Merry 	if (securelevel > 1) {
398ee9c90c7SKenneth D. Merry 		splx(s);
39922b9c86cSKenneth D. Merry 		return(EPERM);
40022b9c86cSKenneth D. Merry 	}
40176babe50SJustin T. Gibbs 
40276babe50SJustin T. Gibbs 	/*
40366a0780eSKenneth D. Merry 	 * Only allow read-write access.
40466a0780eSKenneth D. Merry 	 */
40522b9c86cSKenneth D. Merry 	if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0)) {
40622b9c86cSKenneth D. Merry 		splx(s);
40766a0780eSKenneth D. Merry 		return(EPERM);
40822b9c86cSKenneth D. Merry 	}
40966a0780eSKenneth D. Merry 
41066a0780eSKenneth D. Merry 	/*
41176babe50SJustin T. Gibbs 	 * We don't allow nonblocking access.
41276babe50SJustin T. Gibbs 	 */
41376babe50SJustin T. Gibbs 	if ((flags & O_NONBLOCK) != 0) {
41422b9c86cSKenneth D. Merry 		xpt_print_path(periph->path);
41522b9c86cSKenneth D. Merry 		printf("can't do nonblocking accesss\n");
41622b9c86cSKenneth D. Merry 		splx(s);
41722b9c86cSKenneth D. Merry 		return(EINVAL);
41876babe50SJustin T. Gibbs 	}
41976babe50SJustin T. Gibbs 
42022b9c86cSKenneth D. Merry 	if ((error = cam_periph_lock(periph, PRIBIO | PCATCH)) != 0) {
42122b9c86cSKenneth D. Merry 		splx(s);
42276babe50SJustin T. Gibbs 		return (error);
42322b9c86cSKenneth D. Merry 	}
42422b9c86cSKenneth D. Merry 
42522b9c86cSKenneth D. Merry 	splx(s);
42676babe50SJustin T. Gibbs 
42776babe50SJustin T. Gibbs 	if ((softc->flags & PASS_FLAG_OPEN) == 0) {
42876babe50SJustin T. Gibbs 		if (cam_periph_acquire(periph) != CAM_REQ_CMP)
42976babe50SJustin T. Gibbs 			return(ENXIO);
43076babe50SJustin T. Gibbs 		softc->flags |= PASS_FLAG_OPEN;
43176babe50SJustin T. Gibbs 	}
43276babe50SJustin T. Gibbs 
43376babe50SJustin T. Gibbs 	cam_periph_unlock(periph);
43476babe50SJustin T. Gibbs 
43576babe50SJustin T. Gibbs 	return (error);
43676babe50SJustin T. Gibbs }
43776babe50SJustin T. Gibbs 
43876babe50SJustin T. Gibbs static int
43976babe50SJustin T. Gibbs passclose(dev_t dev, int flag, int fmt, struct proc *p)
44076babe50SJustin T. Gibbs {
44176babe50SJustin T. Gibbs 	struct 	cam_periph *periph;
44276babe50SJustin T. Gibbs 	struct	pass_softc *softc;
44376babe50SJustin T. Gibbs 	int	unit, error;
44476babe50SJustin T. Gibbs 
44576babe50SJustin T. Gibbs 	/* unit = dkunit(dev); */
44676babe50SJustin T. Gibbs 	/* XXX KDM fix this */
44776babe50SJustin T. Gibbs 	unit = minor(dev) & 0xff;
44876babe50SJustin T. Gibbs 
44976babe50SJustin T. Gibbs 	periph = cam_extend_get(passperiphs, unit);
45076babe50SJustin T. Gibbs 	if (periph == NULL)
45176babe50SJustin T. Gibbs 		return (ENXIO);
45276babe50SJustin T. Gibbs 
45376babe50SJustin T. Gibbs 	softc = (struct pass_softc *)periph->softc;
45476babe50SJustin T. Gibbs 
45576babe50SJustin T. Gibbs 	if ((error = cam_periph_lock(periph, PRIBIO)) != 0)
45676babe50SJustin T. Gibbs 		return (error);
45776babe50SJustin T. Gibbs 
45876babe50SJustin T. Gibbs 	softc->flags &= ~PASS_FLAG_OPEN;
45976babe50SJustin T. Gibbs 
46076babe50SJustin T. Gibbs 	cam_periph_unlock(periph);
46176babe50SJustin T. Gibbs 	cam_periph_release(periph);
46276babe50SJustin T. Gibbs 
46376babe50SJustin T. Gibbs 	return (0);
46476babe50SJustin T. Gibbs }
46576babe50SJustin T. Gibbs 
46676babe50SJustin T. Gibbs /*
46776babe50SJustin T. Gibbs  * Actually translate the requested transfer into one the physical driver
46876babe50SJustin T. Gibbs  * can understand.  The transfer is described by a buf and will include
46976babe50SJustin T. Gibbs  * only one physical transfer.
47076babe50SJustin T. Gibbs  */
47176babe50SJustin T. Gibbs static void
47276babe50SJustin T. Gibbs passstrategy(struct buf *bp)
47376babe50SJustin T. Gibbs {
47476babe50SJustin T. Gibbs 	struct cam_periph *periph;
47576babe50SJustin T. Gibbs 	struct pass_softc *softc;
47676babe50SJustin T. Gibbs 	u_int  unit;
47776babe50SJustin T. Gibbs 	int    s;
47876babe50SJustin T. Gibbs 
47976babe50SJustin T. Gibbs 	/*
48076babe50SJustin T. Gibbs 	 * The read/write interface for the passthrough driver doesn't
48176babe50SJustin T. Gibbs 	 * really work right now.  So, we just pass back EINVAL to tell the
48276babe50SJustin T. Gibbs 	 * user to go away.
48376babe50SJustin T. Gibbs 	 */
48476babe50SJustin T. Gibbs 	bp->b_error = EINVAL;
48576babe50SJustin T. Gibbs 	goto bad;
48676babe50SJustin T. Gibbs 
48776babe50SJustin T. Gibbs 	/* unit = dkunit(bp->b_dev); */
48876babe50SJustin T. Gibbs 	/* XXX KDM fix this */
48976babe50SJustin T. Gibbs 	unit = minor(bp->b_dev) & 0xff;
49076babe50SJustin T. Gibbs 
49176babe50SJustin T. Gibbs 	periph = cam_extend_get(passperiphs, unit);
49276babe50SJustin T. Gibbs 	if (periph == NULL) {
49376babe50SJustin T. Gibbs 		bp->b_error = ENXIO;
49476babe50SJustin T. Gibbs 		goto bad;
49576babe50SJustin T. Gibbs 	}
49676babe50SJustin T. Gibbs 	softc = (struct pass_softc *)periph->softc;
49776babe50SJustin T. Gibbs 
49876babe50SJustin T. Gibbs 	/*
49976babe50SJustin T. Gibbs 	 * Odd number of bytes or negative offset
50076babe50SJustin T. Gibbs 	 */
50176babe50SJustin T. Gibbs 	/* valid request?  */
50276babe50SJustin T. Gibbs 	if (bp->b_blkno < 0) {
50376babe50SJustin T. Gibbs 		bp->b_error = EINVAL;
50476babe50SJustin T. Gibbs 		goto bad;
50576babe50SJustin T. Gibbs         }
50676babe50SJustin T. Gibbs 
50776babe50SJustin T. Gibbs 	/*
50876babe50SJustin T. Gibbs 	 * Mask interrupts so that the pack cannot be invalidated until
50976babe50SJustin T. Gibbs 	 * after we are in the queue.  Otherwise, we might not properly
51076babe50SJustin T. Gibbs 	 * clean up one of the buffers.
51176babe50SJustin T. Gibbs 	 */
51276babe50SJustin T. Gibbs 	s = splbio();
51376babe50SJustin T. Gibbs 
51476babe50SJustin T. Gibbs 	bufq_insert_tail(&softc->buf_queue, bp);
51576babe50SJustin T. Gibbs 
51676babe50SJustin T. Gibbs 	splx(s);
51776babe50SJustin T. Gibbs 
51876babe50SJustin T. Gibbs 	/*
51976babe50SJustin T. Gibbs 	 * Schedule ourselves for performing the work.
52076babe50SJustin T. Gibbs 	 */
52176babe50SJustin T. Gibbs 	xpt_schedule(periph, /* XXX priority */1);
52276babe50SJustin T. Gibbs 
52376babe50SJustin T. Gibbs 	return;
52476babe50SJustin T. Gibbs bad:
52576babe50SJustin T. Gibbs 	bp->b_flags |= B_ERROR;
52676babe50SJustin T. Gibbs 
52776babe50SJustin T. Gibbs 	/*
52876babe50SJustin T. Gibbs 	 * Correctly set the buf to indicate a completed xfer
52976babe50SJustin T. Gibbs 	 */
53076babe50SJustin T. Gibbs 	bp->b_resid = bp->b_bcount;
53176babe50SJustin T. Gibbs 	biodone(bp);
53276babe50SJustin T. Gibbs 	return;
53376babe50SJustin T. Gibbs }
53476babe50SJustin T. Gibbs 
53576babe50SJustin T. Gibbs static void
53676babe50SJustin T. Gibbs passstart(struct cam_periph *periph, union ccb *start_ccb)
53776babe50SJustin T. Gibbs {
53876babe50SJustin T. Gibbs 	struct pass_softc *softc;
53976babe50SJustin T. Gibbs 	int s;
54076babe50SJustin T. Gibbs 
54176babe50SJustin T. Gibbs 	softc = (struct pass_softc *)periph->softc;
54276babe50SJustin T. Gibbs 
54376babe50SJustin T. Gibbs 	switch (softc->state) {
54476babe50SJustin T. Gibbs 	case PASS_STATE_NORMAL:
54576babe50SJustin T. Gibbs 	{
54676babe50SJustin T. Gibbs 		struct buf *bp;
54776babe50SJustin T. Gibbs 
54876babe50SJustin T. Gibbs 		s = splbio();
54976babe50SJustin T. Gibbs 		bp = bufq_first(&softc->buf_queue);
55076babe50SJustin T. Gibbs 		if (periph->immediate_priority <= periph->pinfo.priority) {
55176babe50SJustin T. Gibbs 			start_ccb->ccb_h.ccb_type = PASS_CCB_WAITING;
55276babe50SJustin T. Gibbs 			SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
55376babe50SJustin T. Gibbs 					  periph_links.sle);
55476babe50SJustin T. Gibbs 			periph->immediate_priority = CAM_PRIORITY_NONE;
55576babe50SJustin T. Gibbs 			splx(s);
55676babe50SJustin T. Gibbs 			wakeup(&periph->ccb_list);
55776babe50SJustin T. Gibbs 		} else if (bp == NULL) {
55876babe50SJustin T. Gibbs 			splx(s);
55976babe50SJustin T. Gibbs 			xpt_release_ccb(start_ccb);
56076babe50SJustin T. Gibbs 		} else {
56176babe50SJustin T. Gibbs 
56276babe50SJustin T. Gibbs 			bufq_remove(&softc->buf_queue, bp);
56376babe50SJustin T. Gibbs 
56476babe50SJustin T. Gibbs 			devstat_start_transaction(&softc->device_stats);
56576babe50SJustin T. Gibbs 
56676babe50SJustin T. Gibbs 			/*
56776babe50SJustin T. Gibbs 			 * XXX JGibbs -
56876babe50SJustin T. Gibbs 			 * Interpret the contents of the bp as a CCB
56976babe50SJustin T. Gibbs 			 * and pass it to a routine shared by our ioctl
57076babe50SJustin T. Gibbs 			 * code and passtart.
57176babe50SJustin T. Gibbs 			 * For now, just biodone it with EIO so we don't
57276babe50SJustin T. Gibbs 			 * hang.
57376babe50SJustin T. Gibbs 			 */
57476babe50SJustin T. Gibbs 			bp->b_error = EIO;
57576babe50SJustin T. Gibbs 			bp->b_flags |= B_ERROR;
57676babe50SJustin T. Gibbs 			bp->b_resid = bp->b_bcount;
57776babe50SJustin T. Gibbs 			biodone(bp);
57876babe50SJustin T. Gibbs 			bp = bufq_first(&softc->buf_queue);
57976babe50SJustin T. Gibbs 			splx(s);
58076babe50SJustin T. Gibbs 
58176babe50SJustin T. Gibbs 			xpt_action(start_ccb);
58276babe50SJustin T. Gibbs 
58376babe50SJustin T. Gibbs 		}
58476babe50SJustin T. Gibbs 		if (bp != NULL) {
58576babe50SJustin T. Gibbs 			/* Have more work to do, so ensure we stay scheduled */
58676babe50SJustin T. Gibbs 			xpt_schedule(periph, /* XXX priority */1);
58776babe50SJustin T. Gibbs 		}
58876babe50SJustin T. Gibbs 		break;
58976babe50SJustin T. Gibbs 	}
59076babe50SJustin T. Gibbs 	}
59176babe50SJustin T. Gibbs }
59276babe50SJustin T. Gibbs static void
59376babe50SJustin T. Gibbs passdone(struct cam_periph *periph, union ccb *done_ccb)
59476babe50SJustin T. Gibbs {
59576babe50SJustin T. Gibbs 	struct pass_softc *softc;
59676babe50SJustin T. Gibbs 	struct ccb_scsiio *csio;
59776babe50SJustin T. Gibbs 
59876babe50SJustin T. Gibbs 	softc = (struct pass_softc *)periph->softc;
59976babe50SJustin T. Gibbs 	csio = &done_ccb->csio;
60076babe50SJustin T. Gibbs 	switch (csio->ccb_h.ccb_type) {
60176babe50SJustin T. Gibbs 	case PASS_CCB_BUFFER_IO:
60276babe50SJustin T. Gibbs 	{
60376babe50SJustin T. Gibbs 		struct buf		*bp;
60476babe50SJustin T. Gibbs 		cam_status		status;
60576babe50SJustin T. Gibbs 		u_int8_t		scsi_status;
60676babe50SJustin T. Gibbs 		devstat_trans_flags	ds_flags;
60776babe50SJustin T. Gibbs 
60876babe50SJustin T. Gibbs 		status = done_ccb->ccb_h.status;
60976babe50SJustin T. Gibbs 		scsi_status = done_ccb->csio.scsi_status;
61076babe50SJustin T. Gibbs 		bp = (struct buf *)done_ccb->ccb_h.ccb_bp;
61176babe50SJustin T. Gibbs 		/* XXX handle errors */
61276babe50SJustin T. Gibbs 		if (!(((status & CAM_STATUS_MASK) == CAM_REQ_CMP)
61376babe50SJustin T. Gibbs 		  && (scsi_status == SCSI_STATUS_OK))) {
61476babe50SJustin T. Gibbs 			int error;
61576babe50SJustin T. Gibbs 
61676babe50SJustin T. Gibbs 			if ((error = passerror(done_ccb, 0, 0)) == ERESTART) {
61776babe50SJustin T. Gibbs 				/*
61876babe50SJustin T. Gibbs 				 * A retry was scheuled, so
61976babe50SJustin T. Gibbs 				 * just return.
62076babe50SJustin T. Gibbs 				 */
62176babe50SJustin T. Gibbs 				return;
62276babe50SJustin T. Gibbs 			}
62376babe50SJustin T. Gibbs 
62476babe50SJustin T. Gibbs 			/*
62576babe50SJustin T. Gibbs 			 * XXX unfreeze the queue after we complete
62676babe50SJustin T. Gibbs 			 * the abort process
62776babe50SJustin T. Gibbs 			 */
62876babe50SJustin T. Gibbs 			bp->b_error = error;
62976babe50SJustin T. Gibbs 			bp->b_flags |= B_ERROR;
63076babe50SJustin T. Gibbs 		}
63176babe50SJustin T. Gibbs 
63276babe50SJustin T. Gibbs 		if ((done_ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
63376babe50SJustin T. Gibbs 			ds_flags = DEVSTAT_READ;
63476babe50SJustin T. Gibbs 		else if ((done_ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
63576babe50SJustin T. Gibbs 			ds_flags = DEVSTAT_WRITE;
63676babe50SJustin T. Gibbs 		else
63776babe50SJustin T. Gibbs 			ds_flags = DEVSTAT_NO_DATA;
63876babe50SJustin T. Gibbs 
63976babe50SJustin T. Gibbs 		devstat_end_transaction(&softc->device_stats, bp->b_bcount,
64076babe50SJustin T. Gibbs 					done_ccb->csio.tag_action & 0xf,
64176babe50SJustin T. Gibbs 					ds_flags);
64276babe50SJustin T. Gibbs 
64376babe50SJustin T. Gibbs 		biodone(bp);
64476babe50SJustin T. Gibbs 		break;
64576babe50SJustin T. Gibbs 	}
64676babe50SJustin T. Gibbs 	case PASS_CCB_WAITING:
64776babe50SJustin T. Gibbs 	{
64876babe50SJustin T. Gibbs 		/* Caller will release the CCB */
64976babe50SJustin T. Gibbs 		wakeup(&done_ccb->ccb_h.cbfcnp);
65076babe50SJustin T. Gibbs 		return;
65176babe50SJustin T. Gibbs 	}
65276babe50SJustin T. Gibbs 	}
65376babe50SJustin T. Gibbs 	xpt_release_ccb(done_ccb);
65476babe50SJustin T. Gibbs }
65576babe50SJustin T. Gibbs 
65676babe50SJustin T. Gibbs static int
65776babe50SJustin T. Gibbs passioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
65876babe50SJustin T. Gibbs {
65976babe50SJustin T. Gibbs 	struct 	cam_periph *periph;
66076babe50SJustin T. Gibbs 	struct	pass_softc *softc;
66176babe50SJustin T. Gibbs 	u_int8_t unit;
66276babe50SJustin T. Gibbs 	int      error;
66376babe50SJustin T. Gibbs 
66476babe50SJustin T. Gibbs 
66576babe50SJustin T. Gibbs 	/* unit = dkunit(dev); */
66676babe50SJustin T. Gibbs 	/* XXX KDM fix this */
66776babe50SJustin T. Gibbs 	unit = minor(dev) & 0xff;
66876babe50SJustin T. Gibbs 
66976babe50SJustin T. Gibbs 	periph = cam_extend_get(passperiphs, unit);
67076babe50SJustin T. Gibbs 
67176babe50SJustin T. Gibbs 	if (periph == NULL)
67276babe50SJustin T. Gibbs 		return(ENXIO);
67376babe50SJustin T. Gibbs 
67476babe50SJustin T. Gibbs 	softc = (struct pass_softc *)periph->softc;
67576babe50SJustin T. Gibbs 
67676babe50SJustin T. Gibbs 	error = 0;
67776babe50SJustin T. Gibbs 
67876babe50SJustin T. Gibbs 	switch (cmd) {
67976babe50SJustin T. Gibbs 
68076babe50SJustin T. Gibbs 	case CAMIOCOMMAND:
68176babe50SJustin T. Gibbs 	{
68276babe50SJustin T. Gibbs 		union ccb *inccb;
68376babe50SJustin T. Gibbs 		union ccb *ccb;
6849deea857SKenneth D. Merry 		int ccb_malloced;
68576babe50SJustin T. Gibbs 
68676babe50SJustin T. Gibbs 		inccb = (union ccb *)addr;
6879deea857SKenneth D. Merry 
6889deea857SKenneth D. Merry 		/*
6899deea857SKenneth D. Merry 		 * Some CCB types, like scan bus and scan lun can only go
6909deea857SKenneth D. Merry 		 * through the transport layer device.
6919deea857SKenneth D. Merry 		 */
6929deea857SKenneth D. Merry 		if (inccb->ccb_h.func_code & XPT_FC_XPT_ONLY) {
6939deea857SKenneth D. Merry 			xpt_print_path(periph->path);
6949deea857SKenneth D. Merry 			printf("CCB function code %#x is restricted to the "
6959deea857SKenneth D. Merry 			       "XPT device\n", inccb->ccb_h.func_code);
6969deea857SKenneth D. Merry 			error = ENODEV;
6979deea857SKenneth D. Merry 			break;
6989deea857SKenneth D. Merry 		}
6999deea857SKenneth D. Merry 
7009deea857SKenneth D. Merry 		/*
7019deea857SKenneth D. Merry 		 * Non-immediate CCBs need a CCB from the per-device pool
7029deea857SKenneth D. Merry 		 * of CCBs, which is scheduled by the transport layer.
7039deea857SKenneth D. Merry 		 * Immediate CCBs and user-supplied CCBs should just be
7049deea857SKenneth D. Merry 		 * malloced.
7059deea857SKenneth D. Merry 		 */
7069deea857SKenneth D. Merry 		if ((inccb->ccb_h.func_code & XPT_FC_QUEUED)
7079deea857SKenneth D. Merry 		 && ((inccb->ccb_h.func_code & XPT_FC_USER_CCB) == 0)) {
7089deea857SKenneth D. Merry 			ccb = cam_periph_getccb(periph,
7099deea857SKenneth D. Merry 						inccb->ccb_h.pinfo.priority);
7109deea857SKenneth D. Merry 			ccb_malloced = 0;
7119deea857SKenneth D. Merry 		} else {
7129deea857SKenneth D. Merry 			ccb = xpt_alloc_ccb();
7139deea857SKenneth D. Merry 
7149deea857SKenneth D. Merry 			if (ccb != NULL)
7159deea857SKenneth D. Merry 				xpt_setup_ccb(&ccb->ccb_h, periph->path,
7169deea857SKenneth D. Merry 					      inccb->ccb_h.pinfo.priority);
7179deea857SKenneth D. Merry 			ccb_malloced = 1;
7189deea857SKenneth D. Merry 		}
7199deea857SKenneth D. Merry 
7209deea857SKenneth D. Merry 		if (ccb == NULL) {
7219deea857SKenneth D. Merry 			xpt_print_path(periph->path);
7229deea857SKenneth D. Merry 			printf("unable to allocate CCB\n");
7239deea857SKenneth D. Merry 			error = ENOMEM;
7249deea857SKenneth D. Merry 			break;
7259deea857SKenneth D. Merry 		}
72676babe50SJustin T. Gibbs 
72776babe50SJustin T. Gibbs 		error = passsendccb(periph, ccb, inccb);
72876babe50SJustin T. Gibbs 
7299deea857SKenneth D. Merry 		if (ccb_malloced)
7309deea857SKenneth D. Merry 			xpt_free_ccb(ccb);
7319deea857SKenneth D. Merry 		else
73276babe50SJustin T. Gibbs 			xpt_release_ccb(ccb);
73376babe50SJustin T. Gibbs 
73476babe50SJustin T. Gibbs 		break;
73576babe50SJustin T. Gibbs 	}
73676babe50SJustin T. Gibbs 	default:
73776babe50SJustin T. Gibbs 		error = cam_periph_ioctl(periph, cmd, addr, passerror);
73876babe50SJustin T. Gibbs 		break;
73976babe50SJustin T. Gibbs 	}
74076babe50SJustin T. Gibbs 
74176babe50SJustin T. Gibbs 	return(error);
74276babe50SJustin T. Gibbs }
74376babe50SJustin T. Gibbs 
74476babe50SJustin T. Gibbs /*
74576babe50SJustin T. Gibbs  * Generally, "ccb" should be the CCB supplied by the kernel.  "inccb"
74676babe50SJustin T. Gibbs  * should be the CCB that is copied in from the user.
74776babe50SJustin T. Gibbs  */
74876babe50SJustin T. Gibbs static int
74976babe50SJustin T. Gibbs passsendccb(struct cam_periph *periph, union ccb *ccb, union ccb *inccb)
75076babe50SJustin T. Gibbs {
75176babe50SJustin T. Gibbs 	struct pass_softc *softc;
75276babe50SJustin T. Gibbs 	struct cam_periph_map_info mapinfo;
75376babe50SJustin T. Gibbs 	int error, need_unmap;
75476babe50SJustin T. Gibbs 
75576babe50SJustin T. Gibbs 	softc = (struct pass_softc *)periph->softc;
75676babe50SJustin T. Gibbs 
75776babe50SJustin T. Gibbs 	need_unmap = 0;
75876babe50SJustin T. Gibbs 
75976babe50SJustin T. Gibbs 	/*
76076babe50SJustin T. Gibbs 	 * There are some fields in the CCB header that need to be
76176babe50SJustin T. Gibbs 	 * preserved, the rest we get from the user.
76276babe50SJustin T. Gibbs 	 */
76376babe50SJustin T. Gibbs 	xpt_merge_ccb(ccb, inccb);
76476babe50SJustin T. Gibbs 
76576babe50SJustin T. Gibbs 	/*
76676babe50SJustin T. Gibbs 	 * There's no way for the user to have a completion
76776babe50SJustin T. Gibbs 	 * function, so we put our own completion function in here.
76876babe50SJustin T. Gibbs 	 */
76976babe50SJustin T. Gibbs 	ccb->ccb_h.cbfcnp = passdone;
77076babe50SJustin T. Gibbs 
77176babe50SJustin T. Gibbs 	/*
77276babe50SJustin T. Gibbs 	 * We only attempt to map the user memory into kernel space
77376babe50SJustin T. Gibbs 	 * if they haven't passed in a physical memory pointer,
77476babe50SJustin T. Gibbs 	 * and if there is actually an I/O operation to perform.
77576babe50SJustin T. Gibbs 	 * Right now cam_periph_mapmem() only supports SCSI and device
77676babe50SJustin T. Gibbs 	 * match CCBs.  For the SCSI CCBs, we only pass the CCB in if
77776babe50SJustin T. Gibbs 	 * there's actually data to map.  cam_periph_mapmem() will do the
77876babe50SJustin T. Gibbs 	 * right thing, even if there isn't data to map, but since CCBs
77976babe50SJustin T. Gibbs 	 * without data are a reasonably common occurance (e.g. test unit
78076babe50SJustin T. Gibbs 	 * ready), it will save a few cycles if we check for it here.
78176babe50SJustin T. Gibbs 	 */
78276babe50SJustin T. Gibbs 	if (((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0)
78376babe50SJustin T. Gibbs 	 && (((ccb->ccb_h.func_code == XPT_SCSI_IO)
78476babe50SJustin T. Gibbs 	    && ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE))
78576babe50SJustin T. Gibbs 	  || (ccb->ccb_h.func_code == XPT_DEV_MATCH))) {
78676babe50SJustin T. Gibbs 
78776babe50SJustin T. Gibbs 		bzero(&mapinfo, sizeof(mapinfo));
78876babe50SJustin T. Gibbs 
78976babe50SJustin T. Gibbs 		error = cam_periph_mapmem(ccb, &mapinfo);
79076babe50SJustin T. Gibbs 
79176babe50SJustin T. Gibbs 		/*
79276babe50SJustin T. Gibbs 		 * cam_periph_mapmem returned an error, we can't continue.
79376babe50SJustin T. Gibbs 		 * Return the error to the user.
79476babe50SJustin T. Gibbs 		 */
79576babe50SJustin T. Gibbs 		if (error)
79676babe50SJustin T. Gibbs 			return(error);
79776babe50SJustin T. Gibbs 
79876babe50SJustin T. Gibbs 		/*
79976babe50SJustin T. Gibbs 		 * We successfully mapped the memory in, so we need to
80076babe50SJustin T. Gibbs 		 * unmap it when the transaction is done.
80176babe50SJustin T. Gibbs 		 */
80276babe50SJustin T. Gibbs 		need_unmap = 1;
80376babe50SJustin T. Gibbs 	}
80476babe50SJustin T. Gibbs 
80576babe50SJustin T. Gibbs 	/*
80676babe50SJustin T. Gibbs 	 * If the user wants us to perform any error recovery, then honor
80776babe50SJustin T. Gibbs 	 * that request.  Otherwise, it's up to the user to perform any
80876babe50SJustin T. Gibbs 	 * error recovery.
80976babe50SJustin T. Gibbs 	 */
81076babe50SJustin T. Gibbs 	error = cam_periph_runccb(ccb,
81176babe50SJustin T. Gibbs 				  (ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) ?
81276babe50SJustin T. Gibbs 				  passerror : NULL,
81376babe50SJustin T. Gibbs 				  /* cam_flags */ 0,
81450711c71SKenneth D. Merry 				  /* sense_flags */SF_RETRY_UA | SF_RETRY_SELTO,
81576babe50SJustin T. Gibbs 				  &softc->device_stats);
81676babe50SJustin T. Gibbs 
81776babe50SJustin T. Gibbs 	if (need_unmap != 0)
81876babe50SJustin T. Gibbs 		cam_periph_unmapmem(ccb, &mapinfo);
81976babe50SJustin T. Gibbs 
82076babe50SJustin T. Gibbs 	ccb->ccb_h.cbfcnp = NULL;
82176babe50SJustin T. Gibbs 	ccb->ccb_h.periph_priv = inccb->ccb_h.periph_priv;
82276babe50SJustin T. Gibbs 	bcopy(ccb, inccb, sizeof(union ccb));
82376babe50SJustin T. Gibbs 
82476babe50SJustin T. Gibbs 	return(error);
82576babe50SJustin T. Gibbs }
82676babe50SJustin T. Gibbs 
82776babe50SJustin T. Gibbs static int
82876babe50SJustin T. Gibbs passerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
82976babe50SJustin T. Gibbs {
83076babe50SJustin T. Gibbs 	struct cam_periph *periph;
83176babe50SJustin T. Gibbs 	struct pass_softc *softc;
83276babe50SJustin T. Gibbs 
83376babe50SJustin T. Gibbs 	periph = xpt_path_periph(ccb->ccb_h.path);
83476babe50SJustin T. Gibbs 	softc = (struct pass_softc *)periph->softc;
83576babe50SJustin T. Gibbs 
83676babe50SJustin T. Gibbs 	return(cam_periph_error(ccb, cam_flags, sense_flags,
83776babe50SJustin T. Gibbs 				 &softc->saved_ccb));
83876babe50SJustin T. Gibbs }
839