xref: /freebsd/sys/cam/scsi/scsi_pt.c (revision b8e49f681ba1043f50d42c84eb3eeb78a6d8e96e)
176babe50SJustin T. Gibbs /*
276babe50SJustin T. Gibbs  * Implementation of SCSI Processor Target Peripheral driver for CAM.
376babe50SJustin T. Gibbs  *
476babe50SJustin T. Gibbs  * Copyright (c) 1998 Justin T. Gibbs.
576babe50SJustin T. Gibbs  * All rights reserved.
676babe50SJustin T. Gibbs  *
776babe50SJustin T. Gibbs  * Redistribution and use in source and binary forms, with or without
876babe50SJustin T. Gibbs  * modification, are permitted provided that the following conditions
976babe50SJustin T. Gibbs  * are met:
1076babe50SJustin T. Gibbs  * 1. Redistributions of source code must retain the above copyright
1176babe50SJustin T. Gibbs  *    notice, this list of conditions, and the following disclaimer,
1276babe50SJustin T. Gibbs  *    without modification, immediately at the beginning of the file.
1376babe50SJustin T. Gibbs  * 2. The name of the author may not be used to endorse or promote products
1476babe50SJustin T. Gibbs  *    derived from this software without specific prior written permission.
1576babe50SJustin T. Gibbs  *
1676babe50SJustin T. Gibbs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1776babe50SJustin T. Gibbs  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1876babe50SJustin T. Gibbs  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1976babe50SJustin T. Gibbs  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
2076babe50SJustin T. Gibbs  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2176babe50SJustin T. Gibbs  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2276babe50SJustin T. Gibbs  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2376babe50SJustin T. Gibbs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2476babe50SJustin T. Gibbs  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2576babe50SJustin T. Gibbs  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2676babe50SJustin T. Gibbs  * SUCH DAMAGE.
2776babe50SJustin T. Gibbs  *
28b8e49f68SBill Fumerola  *      $Id: scsi_pt.c,v 1.10 1999/05/31 11:24:07 phk Exp $
2976babe50SJustin T. Gibbs  */
3076babe50SJustin T. Gibbs 
3176babe50SJustin T. Gibbs #include <sys/param.h>
3276babe50SJustin T. Gibbs #include <sys/queue.h>
3376babe50SJustin T. Gibbs #include <sys/systm.h>
3476babe50SJustin T. Gibbs #include <sys/kernel.h>
3576babe50SJustin T. Gibbs #include <sys/types.h>
3676babe50SJustin T. Gibbs #include <sys/buf.h>
3776babe50SJustin T. Gibbs #include <sys/devicestat.h>
3876babe50SJustin T. Gibbs #include <sys/malloc.h>
3976babe50SJustin T. Gibbs #include <sys/conf.h>
4076babe50SJustin T. Gibbs 
4176babe50SJustin T. Gibbs #include <cam/cam.h>
4276babe50SJustin T. Gibbs #include <cam/cam_ccb.h>
4376babe50SJustin T. Gibbs #include <cam/cam_extend.h>
4476babe50SJustin T. Gibbs #include <cam/cam_periph.h>
4576babe50SJustin T. Gibbs #include <cam/cam_xpt_periph.h>
4676babe50SJustin T. Gibbs #include <cam/cam_debug.h>
4776babe50SJustin T. Gibbs 
4876babe50SJustin T. Gibbs #include <cam/scsi/scsi_all.h>
4976babe50SJustin T. Gibbs #include <cam/scsi/scsi_message.h>
5076babe50SJustin T. Gibbs #include <cam/scsi/scsi_pt.h>
5176babe50SJustin T. Gibbs 
5276babe50SJustin T. Gibbs typedef enum {
5376babe50SJustin T. Gibbs 	PT_STATE_PROBE,
5476babe50SJustin T. Gibbs 	PT_STATE_NORMAL
5576babe50SJustin T. Gibbs } pt_state;
5676babe50SJustin T. Gibbs 
5776babe50SJustin T. Gibbs typedef enum {
5876babe50SJustin T. Gibbs 	PT_FLAG_NONE		= 0x00,
5976babe50SJustin T. Gibbs 	PT_FLAG_OPEN		= 0x01,
6076babe50SJustin T. Gibbs 	PT_FLAG_DEVICE_INVALID	= 0x02,
6176babe50SJustin T. Gibbs 	PT_FLAG_RETRY_UA	= 0x04
6276babe50SJustin T. Gibbs } pt_flags;
6376babe50SJustin T. Gibbs 
6476babe50SJustin T. Gibbs typedef enum {
6576babe50SJustin T. Gibbs 	PT_CCB_BUFFER_IO	= 0x01,
6676babe50SJustin T. Gibbs 	PT_CCB_WAITING		= 0x02,
6776babe50SJustin T. Gibbs 	PT_CCB_RETRY_UA		= 0x04,
6876babe50SJustin T. Gibbs 	PT_CCB_BUFFER_IO_UA	= PT_CCB_BUFFER_IO|PT_CCB_RETRY_UA
6976babe50SJustin T. Gibbs } pt_ccb_state;
7076babe50SJustin T. Gibbs 
7176babe50SJustin T. Gibbs /* Offsets into our private area for storing information */
7276babe50SJustin T. Gibbs #define ccb_state	ppriv_field0
7376babe50SJustin T. Gibbs #define ccb_bp		ppriv_ptr1
7476babe50SJustin T. Gibbs 
7576babe50SJustin T. Gibbs struct pt_softc {
7676babe50SJustin T. Gibbs 	struct	 buf_queue_head buf_queue;
7776babe50SJustin T. Gibbs 	struct	 devstat device_stats;
7876babe50SJustin T. Gibbs 	LIST_HEAD(, ccb_hdr) pending_ccbs;
7976babe50SJustin T. Gibbs 	pt_state state;
8076babe50SJustin T. Gibbs 	pt_flags flags;
8176babe50SJustin T. Gibbs 	union	 ccb saved_ccb;
8276babe50SJustin T. Gibbs };
8376babe50SJustin T. Gibbs 
8476babe50SJustin T. Gibbs static	d_open_t	ptopen;
8576babe50SJustin T. Gibbs static	d_close_t	ptclose;
8676babe50SJustin T. Gibbs static	d_strategy_t	ptstrategy;
8776babe50SJustin T. Gibbs static	periph_init_t	ptinit;
8876babe50SJustin T. Gibbs static	void		ptasync(void *callback_arg, u_int32_t code,
8976babe50SJustin T. Gibbs 				struct cam_path *path, void *arg);
9076babe50SJustin T. Gibbs static	periph_ctor_t	ptctor;
91ee9c90c7SKenneth D. Merry static	periph_oninv_t	ptoninvalidate;
9276babe50SJustin T. Gibbs static	periph_dtor_t	ptdtor;
9376babe50SJustin T. Gibbs static	periph_start_t	ptstart;
9476babe50SJustin T. Gibbs static	void		ptdone(struct cam_periph *periph,
9576babe50SJustin T. Gibbs 			       union ccb *done_ccb);
9676babe50SJustin T. Gibbs static  int		pterror(union ccb *ccb, u_int32_t cam_flags,
9776babe50SJustin T. Gibbs 				u_int32_t sense_flags);
9876babe50SJustin T. Gibbs 
9976babe50SJustin T. Gibbs void	scsi_send_receive(struct ccb_scsiio *csio, u_int32_t retries,
10076babe50SJustin T. Gibbs 			  void (*cbfcnp)(struct cam_periph *, union ccb *),
10176babe50SJustin T. Gibbs 			  u_int tag_action, int readop, u_int byte2,
10276babe50SJustin T. Gibbs 			  u_int32_t xfer_len, u_int8_t *data_ptr,
10376babe50SJustin T. Gibbs 			  u_int8_t sense_len, u_int32_t timeout);
10476babe50SJustin T. Gibbs 
10576babe50SJustin T. Gibbs static struct periph_driver ptdriver =
10676babe50SJustin T. Gibbs {
10776babe50SJustin T. Gibbs 	ptinit, "pt",
10876babe50SJustin T. Gibbs 	TAILQ_HEAD_INITIALIZER(ptdriver.units), /* generation */ 0
10976babe50SJustin T. Gibbs };
11076babe50SJustin T. Gibbs 
11176babe50SJustin T. Gibbs DATA_SET(periphdriver_set, ptdriver);
11276babe50SJustin T. Gibbs 
11376babe50SJustin T. Gibbs #define PT_CDEV_MAJOR 61
11476babe50SJustin T. Gibbs 
1154e2f199eSPoul-Henning Kamp static struct cdevsw pt_cdevsw = {
1164e2f199eSPoul-Henning Kamp 	/* open */	ptopen,
1174e2f199eSPoul-Henning Kamp 	/* close */	ptclose,
1184e2f199eSPoul-Henning Kamp 	/* read */	physread,
1194e2f199eSPoul-Henning Kamp 	/* write */	physwrite,
1204e2f199eSPoul-Henning Kamp 	/* ioctl */	noioctl,
1214e2f199eSPoul-Henning Kamp 	/* stop */	nostop,
1224e2f199eSPoul-Henning Kamp 	/* reset */	noreset,
1234e2f199eSPoul-Henning Kamp 	/* devtotty */	nodevtotty,
1244e2f199eSPoul-Henning Kamp 	/* poll */	nopoll,
1254e2f199eSPoul-Henning Kamp 	/* mmap */	nommap,
1264e2f199eSPoul-Henning Kamp 	/* strategy */	ptstrategy,
1274e2f199eSPoul-Henning Kamp 	/* name */	"pt",
1284e2f199eSPoul-Henning Kamp 	/* parms */	noparms,
1294e2f199eSPoul-Henning Kamp 	/* maj */	PT_CDEV_MAJOR,
1304e2f199eSPoul-Henning Kamp 	/* dump */	nodump,
1314e2f199eSPoul-Henning Kamp 	/* psize */	nopsize,
1324e2f199eSPoul-Henning Kamp 	/* flags */	0,
1334e2f199eSPoul-Henning Kamp 	/* maxio */	0,
1344e2f199eSPoul-Henning Kamp 	/* bmaj */	-1
13576babe50SJustin T. Gibbs };
13676babe50SJustin T. Gibbs 
13776babe50SJustin T. Gibbs static struct extend_array *ptperiphs;
13876babe50SJustin T. Gibbs 
13976babe50SJustin T. Gibbs static int
14076babe50SJustin T. Gibbs ptopen(dev_t dev, int flags, int fmt, struct proc *p)
14176babe50SJustin T. Gibbs {
14276babe50SJustin T. Gibbs 	struct cam_periph *periph;
14376babe50SJustin T. Gibbs 	struct pt_softc *softc;
14476babe50SJustin T. Gibbs 	int unit;
14576babe50SJustin T. Gibbs 	int error;
146ee9c90c7SKenneth D. Merry 	int s;
14776babe50SJustin T. Gibbs 
14876babe50SJustin T. Gibbs 	unit = minor(dev);
14976babe50SJustin T. Gibbs 	periph = cam_extend_get(ptperiphs, unit);
15076babe50SJustin T. Gibbs 	if (periph == NULL)
15176babe50SJustin T. Gibbs 		return (ENXIO);
15276babe50SJustin T. Gibbs 
15376babe50SJustin T. Gibbs 	softc = (struct pt_softc *)periph->softc;
15476babe50SJustin T. Gibbs 
155ee9c90c7SKenneth D. Merry 	s = splsoftcam();
156ee9c90c7SKenneth D. Merry 	if (softc->flags & PT_FLAG_DEVICE_INVALID) {
157ee9c90c7SKenneth D. Merry 		splx(s);
158ee9c90c7SKenneth D. Merry 		return(ENXIO);
159ee9c90c7SKenneth D. Merry 	}
160ee9c90c7SKenneth D. Merry 
16176babe50SJustin T. Gibbs 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
162b8e49f68SBill Fumerola 	    ("ptopen: dev=%s (unit %d)\n", devtoname(dev), unit));
16376babe50SJustin T. Gibbs 
16422b9c86cSKenneth D. Merry 	if ((error = cam_periph_lock(periph, PRIBIO|PCATCH)) != 0) {
16522b9c86cSKenneth D. Merry 		splx(s);
16676babe50SJustin T. Gibbs 		return (error); /* error code from tsleep */
16722b9c86cSKenneth D. Merry 	}
16822b9c86cSKenneth D. Merry 
16922b9c86cSKenneth D. Merry 	splx(s);
17076babe50SJustin T. Gibbs 
17176babe50SJustin T. Gibbs 	if ((softc->flags & PT_FLAG_OPEN) == 0) {
17276babe50SJustin T. Gibbs 		if (cam_periph_acquire(periph) != CAM_REQ_CMP)
17376babe50SJustin T. Gibbs 			error = ENXIO;
17476babe50SJustin T. Gibbs 		else
17576babe50SJustin T. Gibbs 			softc->flags |= PT_FLAG_OPEN;
17676babe50SJustin T. Gibbs 	} else
17776babe50SJustin T. Gibbs 		error = EBUSY;
17876babe50SJustin T. Gibbs 
17976babe50SJustin T. Gibbs 	cam_periph_unlock(periph);
18076babe50SJustin T. Gibbs 	return (error);
18176babe50SJustin T. Gibbs }
18276babe50SJustin T. Gibbs 
18376babe50SJustin T. Gibbs static int
18476babe50SJustin T. Gibbs ptclose(dev_t dev, int flag, int fmt, struct proc *p)
18576babe50SJustin T. Gibbs {
18676babe50SJustin T. Gibbs 	struct	cam_periph *periph;
18776babe50SJustin T. Gibbs 	struct	pt_softc *softc;
18876babe50SJustin T. Gibbs 	int	unit;
18976babe50SJustin T. Gibbs 	int	error;
19076babe50SJustin T. Gibbs 
19176babe50SJustin T. Gibbs 	unit = minor(dev);
19276babe50SJustin T. Gibbs 	periph = cam_extend_get(ptperiphs, unit);
19376babe50SJustin T. Gibbs 	if (periph == NULL)
19476babe50SJustin T. Gibbs 		return (ENXIO);
19576babe50SJustin T. Gibbs 
19676babe50SJustin T. Gibbs 	softc = (struct pt_softc *)periph->softc;
19776babe50SJustin T. Gibbs 
19876babe50SJustin T. Gibbs 	if ((error = cam_periph_lock(periph, PRIBIO)) != 0)
19976babe50SJustin T. Gibbs 		return (error); /* error code from tsleep */
20076babe50SJustin T. Gibbs 
20176babe50SJustin T. Gibbs 	softc->flags &= ~PT_FLAG_OPEN;
20276babe50SJustin T. Gibbs 	cam_periph_unlock(periph);
20376babe50SJustin T. Gibbs 	cam_periph_release(periph);
20476babe50SJustin T. Gibbs 	return (0);
20576babe50SJustin T. Gibbs }
20676babe50SJustin T. Gibbs 
20776babe50SJustin T. Gibbs /*
20876babe50SJustin T. Gibbs  * Actually translate the requested transfer into one the physical driver
20976babe50SJustin T. Gibbs  * can understand.  The transfer is described by a buf and will include
21076babe50SJustin T. Gibbs  * only one physical transfer.
21176babe50SJustin T. Gibbs  */
21276babe50SJustin T. Gibbs static void
21376babe50SJustin T. Gibbs ptstrategy(struct buf *bp)
21476babe50SJustin T. Gibbs {
21576babe50SJustin T. Gibbs 	struct cam_periph *periph;
21676babe50SJustin T. Gibbs 	struct pt_softc *softc;
21776babe50SJustin T. Gibbs 	u_int  unit;
21876babe50SJustin T. Gibbs 	int    s;
21976babe50SJustin T. Gibbs 
22076babe50SJustin T. Gibbs 	unit = minor(bp->b_dev);
22176babe50SJustin T. Gibbs 	periph = cam_extend_get(ptperiphs, unit);
22276babe50SJustin T. Gibbs 	if (periph == NULL) {
22376babe50SJustin T. Gibbs 		bp->b_error = ENXIO;
22476babe50SJustin T. Gibbs 		goto bad;
22576babe50SJustin T. Gibbs 	}
22676babe50SJustin T. Gibbs 	softc = (struct pt_softc *)periph->softc;
22776babe50SJustin T. Gibbs 
22876babe50SJustin T. Gibbs 	/*
22976babe50SJustin T. Gibbs 	 * Mask interrupts so that the pack cannot be invalidated until
23076babe50SJustin T. Gibbs 	 * after we are in the queue.  Otherwise, we might not properly
23176babe50SJustin T. Gibbs 	 * clean up one of the buffers.
23276babe50SJustin T. Gibbs 	 */
23376babe50SJustin T. Gibbs 	s = splbio();
23476babe50SJustin T. Gibbs 
23576babe50SJustin T. Gibbs 	/*
23676babe50SJustin T. Gibbs 	 * If the device has been made invalid, error out
23776babe50SJustin T. Gibbs 	 */
23876babe50SJustin T. Gibbs 	if ((softc->flags & PT_FLAG_DEVICE_INVALID)) {
23976babe50SJustin T. Gibbs 		splx(s);
24076babe50SJustin T. Gibbs 		bp->b_error = ENXIO;
24176babe50SJustin T. Gibbs 		goto bad;
24276babe50SJustin T. Gibbs 	}
24376babe50SJustin T. Gibbs 
24476babe50SJustin T. Gibbs 	/*
24576babe50SJustin T. Gibbs 	 * Place it in the queue of disk activities for this disk
24676babe50SJustin T. Gibbs 	 */
24776babe50SJustin T. Gibbs 	bufq_insert_tail(&softc->buf_queue, bp);
24876babe50SJustin T. Gibbs 
24976babe50SJustin T. Gibbs 	splx(s);
25076babe50SJustin T. Gibbs 
25176babe50SJustin T. Gibbs 	/*
25276babe50SJustin T. Gibbs 	 * Schedule ourselves for performing the work.
25376babe50SJustin T. Gibbs 	 */
25476babe50SJustin T. Gibbs 	xpt_schedule(periph, /* XXX priority */1);
25576babe50SJustin T. Gibbs 
25676babe50SJustin T. Gibbs 	return;
25776babe50SJustin T. Gibbs bad:
25876babe50SJustin T. Gibbs 	bp->b_flags |= B_ERROR;
25976babe50SJustin T. Gibbs 
26076babe50SJustin T. Gibbs 	/*
26176babe50SJustin T. Gibbs 	 * Correctly set the buf to indicate a completed xfer
26276babe50SJustin T. Gibbs 	 */
26376babe50SJustin T. Gibbs 	bp->b_resid = bp->b_bcount;
26476babe50SJustin T. Gibbs 	biodone(bp);
26576babe50SJustin T. Gibbs }
26676babe50SJustin T. Gibbs 
26776babe50SJustin T. Gibbs static void
26876babe50SJustin T. Gibbs ptinit(void)
26976babe50SJustin T. Gibbs {
27076babe50SJustin T. Gibbs 	cam_status status;
27176babe50SJustin T. Gibbs 	struct cam_path *path;
27276babe50SJustin T. Gibbs 
27376babe50SJustin T. Gibbs 	/*
27476babe50SJustin T. Gibbs 	 * Create our extend array for storing the devices we attach to.
27576babe50SJustin T. Gibbs 	 */
27676babe50SJustin T. Gibbs 	ptperiphs = cam_extend_new();
27776babe50SJustin T. Gibbs 	if (ptperiphs == NULL) {
27876babe50SJustin T. Gibbs 		printf("pt: Failed to alloc extend array!\n");
27976babe50SJustin T. Gibbs 		return;
28076babe50SJustin T. Gibbs 	}
28176babe50SJustin T. Gibbs 
28276babe50SJustin T. Gibbs 	/*
28376babe50SJustin T. Gibbs 	 * Install a global async callback.  This callback will
28476babe50SJustin T. Gibbs 	 * receive async callbacks like "new device found".
28576babe50SJustin T. Gibbs 	 */
28676babe50SJustin T. Gibbs 	status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
28776babe50SJustin T. Gibbs 				 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
28876babe50SJustin T. Gibbs 
28976babe50SJustin T. Gibbs 	if (status == CAM_REQ_CMP) {
29076babe50SJustin T. Gibbs 		struct ccb_setasync csa;
29176babe50SJustin T. Gibbs 
29276babe50SJustin T. Gibbs                 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
29376babe50SJustin T. Gibbs                 csa.ccb_h.func_code = XPT_SASYNC_CB;
29476babe50SJustin T. Gibbs                 csa.event_enable = AC_FOUND_DEVICE;
29576babe50SJustin T. Gibbs                 csa.callback = ptasync;
29676babe50SJustin T. Gibbs                 csa.callback_arg = NULL;
29776babe50SJustin T. Gibbs                 xpt_action((union ccb *)&csa);
29876babe50SJustin T. Gibbs 		status = csa.ccb_h.status;
29976babe50SJustin T. Gibbs                 xpt_free_path(path);
30076babe50SJustin T. Gibbs         }
30176babe50SJustin T. Gibbs 
30276babe50SJustin T. Gibbs 	if (status != CAM_REQ_CMP) {
30376babe50SJustin T. Gibbs 		printf("pt: Failed to attach master async callback "
30476babe50SJustin T. Gibbs 		       "due to status 0x%x!\n", status);
30576babe50SJustin T. Gibbs 	} else {
30676babe50SJustin T. Gibbs 		/* If we were successfull, register our devsw */
3072447bec8SPoul-Henning Kamp 		cdevsw_add(&pt_cdevsw);
30876babe50SJustin T. Gibbs 	}
30976babe50SJustin T. Gibbs }
31076babe50SJustin T. Gibbs 
31176babe50SJustin T. Gibbs static cam_status
31276babe50SJustin T. Gibbs ptctor(struct cam_periph *periph, void *arg)
31376babe50SJustin T. Gibbs {
31476babe50SJustin T. Gibbs 	struct pt_softc *softc;
31576babe50SJustin T. Gibbs 	struct ccb_setasync csa;
31676babe50SJustin T. Gibbs 	struct ccb_getdev *cgd;
31776babe50SJustin T. Gibbs 
31876babe50SJustin T. Gibbs 	cgd = (struct ccb_getdev *)arg;
31976babe50SJustin T. Gibbs 	if (periph == NULL) {
32076babe50SJustin T. Gibbs 		printf("ptregister: periph was NULL!!\n");
32176babe50SJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
32276babe50SJustin T. Gibbs 	}
32376babe50SJustin T. Gibbs 
32476babe50SJustin T. Gibbs 	if (cgd == NULL) {
32576babe50SJustin T. Gibbs 		printf("ptregister: no getdev CCB, can't register device\n");
32676babe50SJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
32776babe50SJustin T. Gibbs 	}
32876babe50SJustin T. Gibbs 
32976babe50SJustin T. Gibbs 	softc = (struct pt_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT);
33076babe50SJustin T. Gibbs 
33176babe50SJustin T. Gibbs 	if (softc == NULL) {
33276babe50SJustin T. Gibbs 		printf("daregister: Unable to probe new device. "
33376babe50SJustin T. Gibbs 		       "Unable to allocate softc\n");
33476babe50SJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
33576babe50SJustin T. Gibbs 	}
33676babe50SJustin T. Gibbs 
33776babe50SJustin T. Gibbs 	bzero(softc, sizeof(*softc));
33876babe50SJustin T. Gibbs 	LIST_INIT(&softc->pending_ccbs);
33976babe50SJustin T. Gibbs 	softc->state = PT_STATE_NORMAL;
34076babe50SJustin T. Gibbs 	bufq_init(&softc->buf_queue);
34176babe50SJustin T. Gibbs 
34276babe50SJustin T. Gibbs 	periph->softc = softc;
34376babe50SJustin T. Gibbs 
34476babe50SJustin T. Gibbs 	cam_extend_set(ptperiphs, periph->unit_number, periph);
34576babe50SJustin T. Gibbs 
34676babe50SJustin T. Gibbs 	/*
34776babe50SJustin T. Gibbs 	 * The DA driver supports a blocksize, but
34876babe50SJustin T. Gibbs 	 * we don't know the blocksize until we do
34976babe50SJustin T. Gibbs 	 * a read capacity.  So, set a flag to
35076babe50SJustin T. Gibbs 	 * indicate that the blocksize is
35176babe50SJustin T. Gibbs 	 * unavailable right now.  We'll clear the
35276babe50SJustin T. Gibbs 	 * flag as soon as we've done a read capacity.
35376babe50SJustin T. Gibbs 	 */
35476babe50SJustin T. Gibbs 	devstat_add_entry(&softc->device_stats, "pt",
35576babe50SJustin T. Gibbs 			  periph->unit_number, 0,
35676babe50SJustin T. Gibbs 			  DEVSTAT_NO_BLOCKSIZE,
3572a888f93SKenneth D. Merry 			  cgd->pd_type | DEVSTAT_TYPE_IF_SCSI,
3582a888f93SKenneth D. Merry 			  DEVSTAT_PRIORITY_OTHER);
35976babe50SJustin T. Gibbs 
36076babe50SJustin T. Gibbs 	/*
36176babe50SJustin T. Gibbs 	 * Add async callbacks for bus reset and
36276babe50SJustin T. Gibbs 	 * bus device reset calls.  I don't bother
36376babe50SJustin T. Gibbs 	 * checking if this fails as, in most cases,
36476babe50SJustin T. Gibbs 	 * the system will function just fine without
36576babe50SJustin T. Gibbs 	 * them and the only alternative would be to
36676babe50SJustin T. Gibbs 	 * not attach the device on failure.
36776babe50SJustin T. Gibbs 	 */
36876babe50SJustin T. Gibbs 	xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5);
36976babe50SJustin T. Gibbs 	csa.ccb_h.func_code = XPT_SASYNC_CB;
37076babe50SJustin T. Gibbs 	csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
37176babe50SJustin T. Gibbs 	csa.callback = ptasync;
37276babe50SJustin T. Gibbs 	csa.callback_arg = periph;
37376babe50SJustin T. Gibbs 	xpt_action((union ccb *)&csa);
37476babe50SJustin T. Gibbs 
37576babe50SJustin T. Gibbs 	/* Tell the user we've attached to the device */
37676babe50SJustin T. Gibbs 	xpt_announce_periph(periph, NULL);
37776babe50SJustin T. Gibbs 
37876babe50SJustin T. Gibbs 	return(CAM_REQ_CMP);
37976babe50SJustin T. Gibbs }
38076babe50SJustin T. Gibbs 
38176babe50SJustin T. Gibbs static void
382ee9c90c7SKenneth D. Merry ptoninvalidate(struct cam_periph *periph)
383ee9c90c7SKenneth D. Merry {
384ee9c90c7SKenneth D. Merry 	int s;
385ee9c90c7SKenneth D. Merry 	struct pt_softc *softc;
386ee9c90c7SKenneth D. Merry 	struct buf *q_bp;
387ee9c90c7SKenneth D. Merry 	struct ccb_setasync csa;
388ee9c90c7SKenneth D. Merry 
389ee9c90c7SKenneth D. Merry 	softc = (struct pt_softc *)periph->softc;
390ee9c90c7SKenneth D. Merry 
391ee9c90c7SKenneth D. Merry 	/*
392ee9c90c7SKenneth D. Merry 	 * De-register any async callbacks.
393ee9c90c7SKenneth D. Merry 	 */
394ee9c90c7SKenneth D. Merry 	xpt_setup_ccb(&csa.ccb_h, periph->path,
395ee9c90c7SKenneth D. Merry 		      /* priority */ 5);
396ee9c90c7SKenneth D. Merry 	csa.ccb_h.func_code = XPT_SASYNC_CB;
397ee9c90c7SKenneth D. Merry 	csa.event_enable = 0;
398ee9c90c7SKenneth D. Merry 	csa.callback = ptasync;
399ee9c90c7SKenneth D. Merry 	csa.callback_arg = periph;
400ee9c90c7SKenneth D. Merry 	xpt_action((union ccb *)&csa);
401ee9c90c7SKenneth D. Merry 
402ee9c90c7SKenneth D. Merry 	softc->flags |= PT_FLAG_DEVICE_INVALID;
403ee9c90c7SKenneth D. Merry 
404ee9c90c7SKenneth D. Merry 	/*
405ee9c90c7SKenneth D. Merry 	 * Although the oninvalidate() routines are always called at
406ee9c90c7SKenneth D. Merry 	 * splsoftcam, we need to be at splbio() here to keep the buffer
407ee9c90c7SKenneth D. Merry 	 * queue from being modified while we traverse it.
408ee9c90c7SKenneth D. Merry 	 */
409ee9c90c7SKenneth D. Merry 	s = splbio();
410ee9c90c7SKenneth D. Merry 
411ee9c90c7SKenneth D. Merry 	/*
412ee9c90c7SKenneth D. Merry 	 * Return all queued I/O with ENXIO.
413ee9c90c7SKenneth D. Merry 	 * XXX Handle any transactions queued to the card
414ee9c90c7SKenneth D. Merry 	 *     with XPT_ABORT_CCB.
415ee9c90c7SKenneth D. Merry 	 */
416ee9c90c7SKenneth D. Merry 	while ((q_bp = bufq_first(&softc->buf_queue)) != NULL){
417ee9c90c7SKenneth D. Merry 		bufq_remove(&softc->buf_queue, q_bp);
418ee9c90c7SKenneth D. Merry 		q_bp->b_resid = q_bp->b_bcount;
419ee9c90c7SKenneth D. Merry 		q_bp->b_error = ENXIO;
420ee9c90c7SKenneth D. Merry 		q_bp->b_flags |= B_ERROR;
421ee9c90c7SKenneth D. Merry 		biodone(q_bp);
422ee9c90c7SKenneth D. Merry 	}
423ee9c90c7SKenneth D. Merry 
424ee9c90c7SKenneth D. Merry 	splx(s);
425ee9c90c7SKenneth D. Merry 
426ee9c90c7SKenneth D. Merry 	xpt_print_path(periph->path);
427ee9c90c7SKenneth D. Merry 	printf("lost device\n");
428ee9c90c7SKenneth D. Merry }
429ee9c90c7SKenneth D. Merry 
430ee9c90c7SKenneth D. Merry static void
43176babe50SJustin T. Gibbs ptdtor(struct cam_periph *periph)
43276babe50SJustin T. Gibbs {
433ee9c90c7SKenneth D. Merry 	struct pt_softc *softc;
434ee9c90c7SKenneth D. Merry 
435ee9c90c7SKenneth D. Merry 	softc = (struct pt_softc *)periph->softc;
436ee9c90c7SKenneth D. Merry 
437ee9c90c7SKenneth D. Merry 	devstat_remove_entry(&softc->device_stats);
438ee9c90c7SKenneth D. Merry 
43976babe50SJustin T. Gibbs 	cam_extend_release(ptperiphs, periph->unit_number);
44076babe50SJustin T. Gibbs 	xpt_print_path(periph->path);
44176babe50SJustin T. Gibbs 	printf("removing device entry\n");
442ee9c90c7SKenneth D. Merry 	free(softc, M_DEVBUF);
44376babe50SJustin T. Gibbs }
44476babe50SJustin T. Gibbs 
44576babe50SJustin T. Gibbs static void
44676babe50SJustin T. Gibbs ptasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
44776babe50SJustin T. Gibbs {
44876babe50SJustin T. Gibbs 	struct cam_periph *periph;
44976babe50SJustin T. Gibbs 
45076babe50SJustin T. Gibbs 	periph = (struct cam_periph *)callback_arg;
45176babe50SJustin T. Gibbs 	switch (code) {
45276babe50SJustin T. Gibbs 	case AC_FOUND_DEVICE:
45376babe50SJustin T. Gibbs 	{
45476babe50SJustin T. Gibbs 		struct ccb_getdev *cgd;
45576babe50SJustin T. Gibbs 		cam_status status;
45676babe50SJustin T. Gibbs 
45776babe50SJustin T. Gibbs 		cgd = (struct ccb_getdev *)arg;
45876babe50SJustin T. Gibbs 
45976babe50SJustin T. Gibbs 		if (cgd->pd_type != T_PROCESSOR)
46076babe50SJustin T. Gibbs 			break;
46176babe50SJustin T. Gibbs 
46276babe50SJustin T. Gibbs 		/*
46376babe50SJustin T. Gibbs 		 * Allocate a peripheral instance for
46476babe50SJustin T. Gibbs 		 * this device and start the probe
46576babe50SJustin T. Gibbs 		 * process.
46676babe50SJustin T. Gibbs 		 */
467ee9c90c7SKenneth D. Merry 		status = cam_periph_alloc(ptctor, ptoninvalidate, ptdtor,
468ee9c90c7SKenneth D. Merry 					  ptstart, "pt", CAM_PERIPH_BIO,
469ee9c90c7SKenneth D. Merry 					  cgd->ccb_h.path, ptasync,
470ee9c90c7SKenneth D. Merry 					  AC_FOUND_DEVICE, cgd);
47176babe50SJustin T. Gibbs 
47276babe50SJustin T. Gibbs 		if (status != CAM_REQ_CMP
47376babe50SJustin T. Gibbs 		 && status != CAM_REQ_INPROG)
47476babe50SJustin T. Gibbs 			printf("ptasync: Unable to attach to new device "
47576babe50SJustin T. Gibbs 				"due to status 0x%x\n", status);
47676babe50SJustin T. Gibbs 		break;
47776babe50SJustin T. Gibbs 	}
47876babe50SJustin T. Gibbs 	case AC_SENT_BDR:
47976babe50SJustin T. Gibbs 	case AC_BUS_RESET:
48076babe50SJustin T. Gibbs 	{
48176babe50SJustin T. Gibbs 		struct pt_softc *softc;
48276babe50SJustin T. Gibbs 		struct ccb_hdr *ccbh;
48376babe50SJustin T. Gibbs 		int s;
48476babe50SJustin T. Gibbs 
48576babe50SJustin T. Gibbs 		softc = (struct pt_softc *)periph->softc;
48676babe50SJustin T. Gibbs 		s = splsoftcam();
48776babe50SJustin T. Gibbs 		/*
48876babe50SJustin T. Gibbs 		 * Don't fail on the expected unit attention
48976babe50SJustin T. Gibbs 		 * that will occur.
49076babe50SJustin T. Gibbs 		 */
49176babe50SJustin T. Gibbs 		softc->flags |= PT_FLAG_RETRY_UA;
49276babe50SJustin T. Gibbs 		for (ccbh = LIST_FIRST(&softc->pending_ccbs);
49376babe50SJustin T. Gibbs 		     ccbh != NULL; ccbh = LIST_NEXT(ccbh, periph_links.le))
49476babe50SJustin T. Gibbs 			ccbh->ccb_state |= PT_CCB_RETRY_UA;
49576babe50SJustin T. Gibbs 		splx(s);
496516871c6SJustin T. Gibbs 		/* FALLTHROUGH */
49776babe50SJustin T. Gibbs 	}
49876babe50SJustin T. Gibbs 	default:
499516871c6SJustin T. Gibbs 		cam_periph_async(periph, code, path, arg);
50076babe50SJustin T. Gibbs 		break;
50176babe50SJustin T. Gibbs 	}
50276babe50SJustin T. Gibbs }
50376babe50SJustin T. Gibbs 
50476babe50SJustin T. Gibbs static void
50576babe50SJustin T. Gibbs ptstart(struct cam_periph *periph, union ccb *start_ccb)
50676babe50SJustin T. Gibbs {
50776babe50SJustin T. Gibbs 	struct pt_softc *softc;
50876babe50SJustin T. Gibbs 	struct buf *bp;
50976babe50SJustin T. Gibbs 	int s;
51076babe50SJustin T. Gibbs 
51176babe50SJustin T. Gibbs 	softc = (struct pt_softc *)periph->softc;
51276babe50SJustin T. Gibbs 
51376babe50SJustin T. Gibbs 	/*
51476babe50SJustin T. Gibbs 	 * See if there is a buf with work for us to do..
51576babe50SJustin T. Gibbs 	 */
51676babe50SJustin T. Gibbs 	s = splbio();
51776babe50SJustin T. Gibbs 	bp = bufq_first(&softc->buf_queue);
51876babe50SJustin T. Gibbs 	if (periph->immediate_priority <= periph->pinfo.priority) {
51976babe50SJustin T. Gibbs 		CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
52076babe50SJustin T. Gibbs 				("queuing for immediate ccb\n"));
52176babe50SJustin T. Gibbs 		start_ccb->ccb_h.ccb_state = PT_CCB_WAITING;
52276babe50SJustin T. Gibbs 		SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
52376babe50SJustin T. Gibbs 				  periph_links.sle);
52476babe50SJustin T. Gibbs 		periph->immediate_priority = CAM_PRIORITY_NONE;
52576babe50SJustin T. Gibbs 		splx(s);
52676babe50SJustin T. Gibbs 		wakeup(&periph->ccb_list);
52776babe50SJustin T. Gibbs 	} else if (bp == NULL) {
52876babe50SJustin T. Gibbs 		splx(s);
52976babe50SJustin T. Gibbs 		xpt_release_ccb(start_ccb);
53076babe50SJustin T. Gibbs 	} else {
53176babe50SJustin T. Gibbs 		int oldspl;
53276babe50SJustin T. Gibbs 
53376babe50SJustin T. Gibbs 		bufq_remove(&softc->buf_queue, bp);
53476babe50SJustin T. Gibbs 
53576babe50SJustin T. Gibbs 		devstat_start_transaction(&softc->device_stats);
53676babe50SJustin T. Gibbs 
53776babe50SJustin T. Gibbs 		scsi_send_receive(&start_ccb->csio,
53876babe50SJustin T. Gibbs 				  /*retries*/4,
53976babe50SJustin T. Gibbs 				  ptdone,
54076babe50SJustin T. Gibbs 				  MSG_SIMPLE_Q_TAG,
54176babe50SJustin T. Gibbs 				  bp->b_flags & B_READ,
54276babe50SJustin T. Gibbs 				  /*byte2*/0,
54376babe50SJustin T. Gibbs 				  bp->b_bcount,
54476babe50SJustin T. Gibbs 				  bp->b_data,
54576babe50SJustin T. Gibbs 				  /*sense_len*/SSD_FULL_SIZE,
54676babe50SJustin T. Gibbs 				  /*timeout*/10000);
54776babe50SJustin T. Gibbs 
54876babe50SJustin T. Gibbs 		start_ccb->ccb_h.ccb_state = PT_CCB_BUFFER_IO;
54976babe50SJustin T. Gibbs 
55076babe50SJustin T. Gibbs 		/*
55176babe50SJustin T. Gibbs 		 * Block out any asyncronous callbacks
55276babe50SJustin T. Gibbs 		 * while we touch the pending ccb list.
55376babe50SJustin T. Gibbs 		 */
55476babe50SJustin T. Gibbs 		oldspl = splcam();
55576babe50SJustin T. Gibbs 		LIST_INSERT_HEAD(&softc->pending_ccbs, &start_ccb->ccb_h,
55676babe50SJustin T. Gibbs 				 periph_links.le);
55776babe50SJustin T. Gibbs 		splx(oldspl);
55876babe50SJustin T. Gibbs 
55976babe50SJustin T. Gibbs 		start_ccb->ccb_h.ccb_bp = bp;
56076babe50SJustin T. Gibbs 		bp = bufq_first(&softc->buf_queue);
56176babe50SJustin T. Gibbs 		splx(s);
56276babe50SJustin T. Gibbs 
56376babe50SJustin T. Gibbs 		xpt_action(start_ccb);
56476babe50SJustin T. Gibbs 
56576babe50SJustin T. Gibbs 		if (bp != NULL) {
56676babe50SJustin T. Gibbs 			/* Have more work to do, so ensure we stay scheduled */
56776babe50SJustin T. Gibbs 			xpt_schedule(periph, /* XXX priority */1);
56876babe50SJustin T. Gibbs 		}
56976babe50SJustin T. Gibbs 	}
57076babe50SJustin T. Gibbs }
57176babe50SJustin T. Gibbs 
57276babe50SJustin T. Gibbs static void
57376babe50SJustin T. Gibbs ptdone(struct cam_periph *periph, union ccb *done_ccb)
57476babe50SJustin T. Gibbs {
57576babe50SJustin T. Gibbs 	struct pt_softc *softc;
57676babe50SJustin T. Gibbs 	struct ccb_scsiio *csio;
57776babe50SJustin T. Gibbs 
57876babe50SJustin T. Gibbs 	softc = (struct pt_softc *)periph->softc;
57976babe50SJustin T. Gibbs 	csio = &done_ccb->csio;
58076babe50SJustin T. Gibbs 	switch (csio->ccb_h.ccb_state) {
58176babe50SJustin T. Gibbs 	case PT_CCB_BUFFER_IO:
58276babe50SJustin T. Gibbs 	case PT_CCB_BUFFER_IO_UA:
58376babe50SJustin T. Gibbs 	{
58476babe50SJustin T. Gibbs 		struct buf *bp;
58576babe50SJustin T. Gibbs 		int    oldspl;
58676babe50SJustin T. Gibbs 
58776babe50SJustin T. Gibbs 		bp = (struct buf *)done_ccb->ccb_h.ccb_bp;
58876babe50SJustin T. Gibbs 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
58976babe50SJustin T. Gibbs 			int error;
59076babe50SJustin T. Gibbs 			int s;
59176babe50SJustin T. Gibbs 			int sf;
59276babe50SJustin T. Gibbs 
59376babe50SJustin T. Gibbs 			if ((csio->ccb_h.ccb_state & PT_CCB_RETRY_UA) != 0)
59476babe50SJustin T. Gibbs 				sf = SF_RETRY_UA;
59576babe50SJustin T. Gibbs 			else
59676babe50SJustin T. Gibbs 				sf = 0;
59776babe50SJustin T. Gibbs 
59850711c71SKenneth D. Merry 			sf |= SF_RETRY_SELTO;
59950711c71SKenneth D. Merry 
60076babe50SJustin T. Gibbs 			if ((error = pterror(done_ccb, 0, sf)) == ERESTART) {
60176babe50SJustin T. Gibbs 				/*
60276babe50SJustin T. Gibbs 				 * A retry was scheuled, so
60376babe50SJustin T. Gibbs 				 * just return.
60476babe50SJustin T. Gibbs 				 */
60576babe50SJustin T. Gibbs 				return;
60676babe50SJustin T. Gibbs 			}
60776babe50SJustin T. Gibbs 			if (error != 0) {
60876babe50SJustin T. Gibbs 				struct buf *q_bp;
60976babe50SJustin T. Gibbs 
61076babe50SJustin T. Gibbs 				s = splbio();
61176babe50SJustin T. Gibbs 
61276babe50SJustin T. Gibbs 				if (error == ENXIO) {
61376babe50SJustin T. Gibbs 					/*
61476babe50SJustin T. Gibbs 					 * Catastrophic error.  Mark our device
61576babe50SJustin T. Gibbs 					 * as invalid.
61676babe50SJustin T. Gibbs 					 */
61776babe50SJustin T. Gibbs 					xpt_print_path(periph->path);
61876babe50SJustin T. Gibbs 					printf("Invalidating device\n");
61976babe50SJustin T. Gibbs 					softc->flags |= PT_FLAG_DEVICE_INVALID;
62076babe50SJustin T. Gibbs 				}
62176babe50SJustin T. Gibbs 
62276babe50SJustin T. Gibbs 				/*
62376babe50SJustin T. Gibbs 				 * return all queued I/O with EIO, so that
62476babe50SJustin T. Gibbs 				 * the client can retry these I/Os in the
62576babe50SJustin T. Gibbs 				 * proper order should it attempt to recover.
62676babe50SJustin T. Gibbs 				 */
62776babe50SJustin T. Gibbs 				while ((q_bp = bufq_first(&softc->buf_queue))
62876babe50SJustin T. Gibbs 					!= NULL) {
62976babe50SJustin T. Gibbs 					bufq_remove(&softc->buf_queue, q_bp);
63076babe50SJustin T. Gibbs 					q_bp->b_resid = q_bp->b_bcount;
63176babe50SJustin T. Gibbs 					q_bp->b_error = EIO;
63276babe50SJustin T. Gibbs 					q_bp->b_flags |= B_ERROR;
63376babe50SJustin T. Gibbs 					biodone(q_bp);
63476babe50SJustin T. Gibbs 				}
63576babe50SJustin T. Gibbs 				splx(s);
63676babe50SJustin T. Gibbs 				bp->b_error = error;
63776babe50SJustin T. Gibbs 				bp->b_resid = bp->b_bcount;
63876babe50SJustin T. Gibbs 				bp->b_flags |= B_ERROR;
63976babe50SJustin T. Gibbs 			} else {
64076babe50SJustin T. Gibbs 				bp->b_resid = csio->resid;
64176babe50SJustin T. Gibbs 				bp->b_error = 0;
64276babe50SJustin T. Gibbs 				if (bp->b_resid != 0) {
64376babe50SJustin T. Gibbs 					/* Short transfer ??? */
64476babe50SJustin T. Gibbs 					bp->b_flags |= B_ERROR;
64576babe50SJustin T. Gibbs 				}
64676babe50SJustin T. Gibbs 			}
64776babe50SJustin T. Gibbs 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
64876babe50SJustin T. Gibbs 				cam_release_devq(done_ccb->ccb_h.path,
64976babe50SJustin T. Gibbs 						 /*relsim_flags*/0,
65076babe50SJustin T. Gibbs 						 /*reduction*/0,
65176babe50SJustin T. Gibbs 						 /*timeout*/0,
65276babe50SJustin T. Gibbs 						 /*getcount_only*/0);
65376babe50SJustin T. Gibbs 		} else {
65476babe50SJustin T. Gibbs 			bp->b_resid = csio->resid;
65576babe50SJustin T. Gibbs 			if (bp->b_resid != 0)
65676babe50SJustin T. Gibbs 				bp->b_flags |= B_ERROR;
65776babe50SJustin T. Gibbs 		}
65876babe50SJustin T. Gibbs 
65976babe50SJustin T. Gibbs 		/*
66076babe50SJustin T. Gibbs 		 * Block out any asyncronous callbacks
66176babe50SJustin T. Gibbs 		 * while we touch the pending ccb list.
66276babe50SJustin T. Gibbs 		 */
66376babe50SJustin T. Gibbs 		oldspl = splcam();
66476babe50SJustin T. Gibbs 		LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
66576babe50SJustin T. Gibbs 		splx(oldspl);
66676babe50SJustin T. Gibbs 
66776babe50SJustin T. Gibbs 		devstat_end_transaction(&softc->device_stats,
66876babe50SJustin T. Gibbs 					bp->b_bcount - bp->b_resid,
66976babe50SJustin T. Gibbs 					done_ccb->csio.tag_action & 0xf,
67076babe50SJustin T. Gibbs 					(bp->b_flags & B_READ) ? DEVSTAT_READ
67176babe50SJustin T. Gibbs 							       : DEVSTAT_WRITE);
67276babe50SJustin T. Gibbs 
67376babe50SJustin T. Gibbs 		biodone(bp);
67476babe50SJustin T. Gibbs 		break;
67576babe50SJustin T. Gibbs 	}
67676babe50SJustin T. Gibbs 	case PT_CCB_WAITING:
67776babe50SJustin T. Gibbs 		/* Caller will release the CCB */
67876babe50SJustin T. Gibbs 		wakeup(&done_ccb->ccb_h.cbfcnp);
67976babe50SJustin T. Gibbs 		return;
68076babe50SJustin T. Gibbs 	}
68176babe50SJustin T. Gibbs 	xpt_release_ccb(done_ccb);
68276babe50SJustin T. Gibbs }
68376babe50SJustin T. Gibbs 
68476babe50SJustin T. Gibbs static int
68576babe50SJustin T. Gibbs pterror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
68676babe50SJustin T. Gibbs {
68776babe50SJustin T. Gibbs 	struct pt_softc	  *softc;
68876babe50SJustin T. Gibbs 	struct cam_periph *periph;
68976babe50SJustin T. Gibbs 
69076babe50SJustin T. Gibbs 	periph = xpt_path_periph(ccb->ccb_h.path);
69176babe50SJustin T. Gibbs 	softc = (struct pt_softc *)periph->softc;
69276babe50SJustin T. Gibbs 
69376babe50SJustin T. Gibbs 	return(cam_periph_error(ccb, cam_flags, sense_flags,
69476babe50SJustin T. Gibbs 				&softc->saved_ccb));
69576babe50SJustin T. Gibbs }
69676babe50SJustin T. Gibbs 
69776babe50SJustin T. Gibbs void
69876babe50SJustin T. Gibbs scsi_send_receive(struct ccb_scsiio *csio, u_int32_t retries,
69976babe50SJustin T. Gibbs 		  void (*cbfcnp)(struct cam_periph *, union ccb *),
70076babe50SJustin T. Gibbs 		  u_int tag_action, int readop, u_int byte2,
70176babe50SJustin T. Gibbs 		  u_int32_t xfer_len, u_int8_t *data_ptr, u_int8_t sense_len,
70276babe50SJustin T. Gibbs 		  u_int32_t timeout)
70376babe50SJustin T. Gibbs {
70476babe50SJustin T. Gibbs 	struct scsi_send_receive *scsi_cmd;
70576babe50SJustin T. Gibbs 
70676babe50SJustin T. Gibbs 	scsi_cmd = (struct scsi_send_receive *)&csio->cdb_io.cdb_bytes;
70776babe50SJustin T. Gibbs 	scsi_cmd->opcode = readop ? RECEIVE : SEND;
70876babe50SJustin T. Gibbs 	scsi_cmd->byte2 = byte2;
70976babe50SJustin T. Gibbs 	scsi_ulto3b(xfer_len, scsi_cmd->xfer_len);
71076babe50SJustin T. Gibbs 	scsi_cmd->control = 0;
71176babe50SJustin T. Gibbs 
71276babe50SJustin T. Gibbs 	cam_fill_csio(csio,
71376babe50SJustin T. Gibbs 		      retries,
71476babe50SJustin T. Gibbs 		      cbfcnp,
71576babe50SJustin T. Gibbs 		      /*flags*/readop ? CAM_DIR_IN : CAM_DIR_OUT,
71676babe50SJustin T. Gibbs 		      tag_action,
71776babe50SJustin T. Gibbs 		      data_ptr,
71876babe50SJustin T. Gibbs 		      xfer_len,
71976babe50SJustin T. Gibbs 		      sense_len,
72076babe50SJustin T. Gibbs 		      sizeof(*scsi_cmd),
72176babe50SJustin T. Gibbs 		      timeout);
72276babe50SJustin T. Gibbs }
723