xref: /freebsd/sys/cam/scsi/scsi_pt.c (revision c5ff3b2fed69c49493902e97575b3e98dcad67da)
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  *
28c3aac50fSPeter Wemm  * $FreeBSD$
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>
369626b608SPoul-Henning Kamp #include <sys/bio.h>
3776babe50SJustin T. Gibbs #include <sys/devicestat.h>
3876babe50SJustin T. Gibbs #include <sys/malloc.h>
3976babe50SJustin T. Gibbs #include <sys/conf.h>
403ece1bd2SKenneth D. Merry #include <sys/ptio.h>
4176babe50SJustin T. Gibbs 
4276babe50SJustin T. Gibbs #include <cam/cam.h>
4376babe50SJustin T. Gibbs #include <cam/cam_ccb.h>
4476babe50SJustin T. Gibbs #include <cam/cam_extend.h>
4576babe50SJustin T. Gibbs #include <cam/cam_periph.h>
4676babe50SJustin T. Gibbs #include <cam/cam_xpt_periph.h>
4776babe50SJustin T. Gibbs #include <cam/cam_debug.h>
4876babe50SJustin T. Gibbs 
4976babe50SJustin T. Gibbs #include <cam/scsi/scsi_all.h>
5076babe50SJustin T. Gibbs #include <cam/scsi/scsi_message.h>
5176babe50SJustin T. Gibbs #include <cam/scsi/scsi_pt.h>
5276babe50SJustin T. Gibbs 
533ece1bd2SKenneth D. Merry #include "opt_pt.h"
543ece1bd2SKenneth D. Merry 
5576babe50SJustin T. Gibbs typedef enum {
5676babe50SJustin T. Gibbs 	PT_STATE_PROBE,
5776babe50SJustin T. Gibbs 	PT_STATE_NORMAL
5876babe50SJustin T. Gibbs } pt_state;
5976babe50SJustin T. Gibbs 
6076babe50SJustin T. Gibbs typedef enum {
6176babe50SJustin T. Gibbs 	PT_FLAG_NONE		= 0x00,
6276babe50SJustin T. Gibbs 	PT_FLAG_OPEN		= 0x01,
6376babe50SJustin T. Gibbs 	PT_FLAG_DEVICE_INVALID	= 0x02,
6476babe50SJustin T. Gibbs 	PT_FLAG_RETRY_UA	= 0x04
6576babe50SJustin T. Gibbs } pt_flags;
6676babe50SJustin T. Gibbs 
6776babe50SJustin T. Gibbs typedef enum {
6876babe50SJustin T. Gibbs 	PT_CCB_BUFFER_IO	= 0x01,
6976babe50SJustin T. Gibbs 	PT_CCB_WAITING		= 0x02,
7076babe50SJustin T. Gibbs 	PT_CCB_RETRY_UA		= 0x04,
7176babe50SJustin T. Gibbs 	PT_CCB_BUFFER_IO_UA	= PT_CCB_BUFFER_IO|PT_CCB_RETRY_UA
7276babe50SJustin T. Gibbs } pt_ccb_state;
7376babe50SJustin T. Gibbs 
7476babe50SJustin T. Gibbs /* Offsets into our private area for storing information */
7576babe50SJustin T. Gibbs #define ccb_state	ppriv_field0
7676babe50SJustin T. Gibbs #define ccb_bp		ppriv_ptr1
7776babe50SJustin T. Gibbs 
7876babe50SJustin T. Gibbs struct pt_softc {
798177437dSPoul-Henning Kamp 	struct	 bio_queue_head bio_queue;
8076babe50SJustin T. Gibbs 	struct	 devstat device_stats;
81e3975643SJake Burkholder 	LIST_HEAD(, ccb_hdr) pending_ccbs;
8276babe50SJustin T. Gibbs 	pt_state state;
8376babe50SJustin T. Gibbs 	pt_flags flags;
8476babe50SJustin T. Gibbs 	union	 ccb saved_ccb;
853ece1bd2SKenneth D. Merry 	int	 io_timeout;
8673d26919SKenneth D. Merry 	dev_t	 dev;
8776babe50SJustin T. Gibbs };
8876babe50SJustin T. Gibbs 
8976babe50SJustin T. Gibbs static	d_open_t	ptopen;
9076babe50SJustin T. Gibbs static	d_close_t	ptclose;
9176babe50SJustin T. Gibbs static	d_strategy_t	ptstrategy;
9276babe50SJustin T. Gibbs static	periph_init_t	ptinit;
9376babe50SJustin T. Gibbs static	void		ptasync(void *callback_arg, u_int32_t code,
9476babe50SJustin T. Gibbs 				struct cam_path *path, void *arg);
9576babe50SJustin T. Gibbs static	periph_ctor_t	ptctor;
96ee9c90c7SKenneth D. Merry static	periph_oninv_t	ptoninvalidate;
9776babe50SJustin T. Gibbs static	periph_dtor_t	ptdtor;
9876babe50SJustin T. Gibbs static	periph_start_t	ptstart;
9976babe50SJustin T. Gibbs static	void		ptdone(struct cam_periph *periph,
10076babe50SJustin T. Gibbs 			       union ccb *done_ccb);
1013ece1bd2SKenneth D. Merry static	d_ioctl_t	ptioctl;
10276babe50SJustin T. Gibbs static  int		pterror(union ccb *ccb, u_int32_t cam_flags,
10376babe50SJustin T. Gibbs 				u_int32_t sense_flags);
10476babe50SJustin T. Gibbs 
10576babe50SJustin T. Gibbs void	scsi_send_receive(struct ccb_scsiio *csio, u_int32_t retries,
10676babe50SJustin T. Gibbs 			  void (*cbfcnp)(struct cam_periph *, union ccb *),
10776babe50SJustin T. Gibbs 			  u_int tag_action, int readop, u_int byte2,
10876babe50SJustin T. Gibbs 			  u_int32_t xfer_len, u_int8_t *data_ptr,
10976babe50SJustin T. Gibbs 			  u_int8_t sense_len, u_int32_t timeout);
11076babe50SJustin T. Gibbs 
11176babe50SJustin T. Gibbs static struct periph_driver ptdriver =
11276babe50SJustin T. Gibbs {
11376babe50SJustin T. Gibbs 	ptinit, "pt",
11476babe50SJustin T. Gibbs 	TAILQ_HEAD_INITIALIZER(ptdriver.units), /* generation */ 0
11576babe50SJustin T. Gibbs };
11676babe50SJustin T. Gibbs 
1170b7c27b9SPeter Wemm PERIPHDRIVER_DECLARE(pt, ptdriver);
11876babe50SJustin T. Gibbs 
11976babe50SJustin T. Gibbs #define PT_CDEV_MAJOR 61
12076babe50SJustin T. Gibbs 
1214e2f199eSPoul-Henning Kamp static struct cdevsw pt_cdevsw = {
1224e2f199eSPoul-Henning Kamp 	/* open */	ptopen,
1234e2f199eSPoul-Henning Kamp 	/* close */	ptclose,
1244e2f199eSPoul-Henning Kamp 	/* read */	physread,
1254e2f199eSPoul-Henning Kamp 	/* write */	physwrite,
1263ece1bd2SKenneth D. Merry 	/* ioctl */	ptioctl,
1274e2f199eSPoul-Henning Kamp 	/* poll */	nopoll,
1284e2f199eSPoul-Henning Kamp 	/* mmap */	nommap,
1294e2f199eSPoul-Henning Kamp 	/* strategy */	ptstrategy,
1304e2f199eSPoul-Henning Kamp 	/* name */	"pt",
1314e2f199eSPoul-Henning Kamp 	/* maj */	PT_CDEV_MAJOR,
1324e2f199eSPoul-Henning Kamp 	/* dump */	nodump,
1334e2f199eSPoul-Henning Kamp 	/* psize */	nopsize,
1344e2f199eSPoul-Henning Kamp 	/* flags */	0,
13576babe50SJustin T. Gibbs };
13676babe50SJustin T. Gibbs 
13776babe50SJustin T. Gibbs static struct extend_array *ptperiphs;
13876babe50SJustin T. Gibbs 
1393ece1bd2SKenneth D. Merry #ifndef SCSI_PT_DEFAULT_TIMEOUT
1403ece1bd2SKenneth D. Merry #define SCSI_PT_DEFAULT_TIMEOUT		60
1413ece1bd2SKenneth D. Merry #endif
1423ece1bd2SKenneth D. Merry 
14376babe50SJustin T. Gibbs static int
14476babe50SJustin T. Gibbs ptopen(dev_t dev, int flags, int fmt, struct proc *p)
14576babe50SJustin T. Gibbs {
14676babe50SJustin T. Gibbs 	struct cam_periph *periph;
14776babe50SJustin T. Gibbs 	struct pt_softc *softc;
14876babe50SJustin T. Gibbs 	int unit;
14976babe50SJustin T. Gibbs 	int error;
150ee9c90c7SKenneth D. Merry 	int s;
15176babe50SJustin T. Gibbs 
15276babe50SJustin T. Gibbs 	unit = minor(dev);
15376babe50SJustin T. Gibbs 	periph = cam_extend_get(ptperiphs, unit);
15476babe50SJustin T. Gibbs 	if (periph == NULL)
15576babe50SJustin T. Gibbs 		return (ENXIO);
15676babe50SJustin T. Gibbs 
15776babe50SJustin T. Gibbs 	softc = (struct pt_softc *)periph->softc;
15876babe50SJustin T. Gibbs 
159ee9c90c7SKenneth D. Merry 	s = splsoftcam();
160ee9c90c7SKenneth D. Merry 	if (softc->flags & PT_FLAG_DEVICE_INVALID) {
161ee9c90c7SKenneth D. Merry 		splx(s);
162ee9c90c7SKenneth D. Merry 		return(ENXIO);
163ee9c90c7SKenneth D. Merry 	}
164ee9c90c7SKenneth D. Merry 
16576babe50SJustin T. Gibbs 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
166b8e49f68SBill Fumerola 	    ("ptopen: dev=%s (unit %d)\n", devtoname(dev), unit));
16776babe50SJustin T. Gibbs 
16822b9c86cSKenneth D. Merry 	if ((error = cam_periph_lock(periph, PRIBIO|PCATCH)) != 0) {
16922b9c86cSKenneth D. Merry 		splx(s);
17076babe50SJustin T. Gibbs 		return (error); /* error code from tsleep */
17122b9c86cSKenneth D. Merry 	}
17222b9c86cSKenneth D. Merry 
17322b9c86cSKenneth D. Merry 	splx(s);
17476babe50SJustin T. Gibbs 
17576babe50SJustin T. Gibbs 	if ((softc->flags & PT_FLAG_OPEN) == 0) {
17676babe50SJustin T. Gibbs 		if (cam_periph_acquire(periph) != CAM_REQ_CMP)
17776babe50SJustin T. Gibbs 			error = ENXIO;
17876babe50SJustin T. Gibbs 		else
17976babe50SJustin T. Gibbs 			softc->flags |= PT_FLAG_OPEN;
18076babe50SJustin T. Gibbs 	} else
18176babe50SJustin T. Gibbs 		error = EBUSY;
18276babe50SJustin T. Gibbs 
18376babe50SJustin T. Gibbs 	cam_periph_unlock(periph);
18476babe50SJustin T. Gibbs 	return (error);
18576babe50SJustin T. Gibbs }
18676babe50SJustin T. Gibbs 
18776babe50SJustin T. Gibbs static int
18876babe50SJustin T. Gibbs ptclose(dev_t dev, int flag, int fmt, struct proc *p)
18976babe50SJustin T. Gibbs {
19076babe50SJustin T. Gibbs 	struct	cam_periph *periph;
19176babe50SJustin T. Gibbs 	struct	pt_softc *softc;
19276babe50SJustin T. Gibbs 	int	unit;
19376babe50SJustin T. Gibbs 	int	error;
19476babe50SJustin T. Gibbs 
19576babe50SJustin T. Gibbs 	unit = minor(dev);
19676babe50SJustin T. Gibbs 	periph = cam_extend_get(ptperiphs, unit);
19776babe50SJustin T. Gibbs 	if (periph == NULL)
19876babe50SJustin T. Gibbs 		return (ENXIO);
19976babe50SJustin T. Gibbs 
20076babe50SJustin T. Gibbs 	softc = (struct pt_softc *)periph->softc;
20176babe50SJustin T. Gibbs 
20276babe50SJustin T. Gibbs 	if ((error = cam_periph_lock(periph, PRIBIO)) != 0)
20376babe50SJustin T. Gibbs 		return (error); /* error code from tsleep */
20476babe50SJustin T. Gibbs 
20576babe50SJustin T. Gibbs 	softc->flags &= ~PT_FLAG_OPEN;
20676babe50SJustin T. Gibbs 	cam_periph_unlock(periph);
20776babe50SJustin T. Gibbs 	cam_periph_release(periph);
20876babe50SJustin T. Gibbs 	return (0);
20976babe50SJustin T. Gibbs }
21076babe50SJustin T. Gibbs 
21176babe50SJustin T. Gibbs /*
21276babe50SJustin T. Gibbs  * Actually translate the requested transfer into one the physical driver
21376babe50SJustin T. Gibbs  * can understand.  The transfer is described by a buf and will include
21476babe50SJustin T. Gibbs  * only one physical transfer.
21576babe50SJustin T. Gibbs  */
21676babe50SJustin T. Gibbs static void
2178177437dSPoul-Henning Kamp ptstrategy(struct bio *bp)
21876babe50SJustin T. Gibbs {
21976babe50SJustin T. Gibbs 	struct cam_periph *periph;
22076babe50SJustin T. Gibbs 	struct pt_softc *softc;
22176babe50SJustin T. Gibbs 	u_int  unit;
22276babe50SJustin T. Gibbs 	int    s;
22376babe50SJustin T. Gibbs 
2248177437dSPoul-Henning Kamp 	unit = minor(bp->bio_dev);
22576babe50SJustin T. Gibbs 	periph = cam_extend_get(ptperiphs, unit);
226b63170f8SPoul-Henning Kamp 	bp->bio_resid = bp->bio_bcount;
22776babe50SJustin T. Gibbs 	if (periph == NULL) {
228b63170f8SPoul-Henning Kamp 		biofinish(bp, NULL, ENXIO);
229b63170f8SPoul-Henning Kamp 		return;
23076babe50SJustin T. Gibbs 	}
23176babe50SJustin T. Gibbs 	softc = (struct pt_softc *)periph->softc;
23276babe50SJustin T. Gibbs 
23376babe50SJustin T. Gibbs 	/*
23476babe50SJustin T. Gibbs 	 * Mask interrupts so that the pack cannot be invalidated until
23576babe50SJustin T. Gibbs 	 * after we are in the queue.  Otherwise, we might not properly
23676babe50SJustin T. Gibbs 	 * clean up one of the buffers.
23776babe50SJustin T. Gibbs 	 */
23876babe50SJustin T. Gibbs 	s = splbio();
23976babe50SJustin T. Gibbs 
24076babe50SJustin T. Gibbs 	/*
24176babe50SJustin T. Gibbs 	 * If the device has been made invalid, error out
24276babe50SJustin T. Gibbs 	 */
24376babe50SJustin T. Gibbs 	if ((softc->flags & PT_FLAG_DEVICE_INVALID)) {
24476babe50SJustin T. Gibbs 		splx(s);
245b63170f8SPoul-Henning Kamp 		biofinish(bp, NULL, ENXIO);
246b63170f8SPoul-Henning Kamp 		return;
24776babe50SJustin T. Gibbs 	}
24876babe50SJustin T. Gibbs 
24976babe50SJustin T. Gibbs 	/*
25076babe50SJustin T. Gibbs 	 * Place it in the queue of disk activities for this disk
25176babe50SJustin T. Gibbs 	 */
2528177437dSPoul-Henning Kamp 	bioq_insert_tail(&softc->bio_queue, bp);
25376babe50SJustin T. Gibbs 
25476babe50SJustin T. Gibbs 	splx(s);
25576babe50SJustin T. Gibbs 
25676babe50SJustin T. Gibbs 	/*
25776babe50SJustin T. Gibbs 	 * Schedule ourselves for performing the work.
25876babe50SJustin T. Gibbs 	 */
25976babe50SJustin T. Gibbs 	xpt_schedule(periph, /* XXX priority */1);
26076babe50SJustin T. Gibbs 
26176babe50SJustin T. Gibbs 	return;
26276babe50SJustin T. Gibbs }
26376babe50SJustin T. Gibbs 
26476babe50SJustin T. Gibbs static void
26576babe50SJustin T. Gibbs ptinit(void)
26676babe50SJustin T. Gibbs {
26776babe50SJustin T. Gibbs 	cam_status status;
26876babe50SJustin T. Gibbs 	struct cam_path *path;
26976babe50SJustin T. Gibbs 
27076babe50SJustin T. Gibbs 	/*
27176babe50SJustin T. Gibbs 	 * Create our extend array for storing the devices we attach to.
27276babe50SJustin T. Gibbs 	 */
27376babe50SJustin T. Gibbs 	ptperiphs = cam_extend_new();
27476babe50SJustin T. Gibbs 	if (ptperiphs == NULL) {
27576babe50SJustin T. Gibbs 		printf("pt: Failed to alloc extend array!\n");
27676babe50SJustin T. Gibbs 		return;
27776babe50SJustin T. Gibbs 	}
27876babe50SJustin T. Gibbs 
27976babe50SJustin T. Gibbs 	/*
28076babe50SJustin T. Gibbs 	 * Install a global async callback.  This callback will
28176babe50SJustin T. Gibbs 	 * receive async callbacks like "new device found".
28276babe50SJustin T. Gibbs 	 */
28376babe50SJustin T. Gibbs 	status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
28476babe50SJustin T. Gibbs 				 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
28576babe50SJustin T. Gibbs 
28676babe50SJustin T. Gibbs 	if (status == CAM_REQ_CMP) {
28776babe50SJustin T. Gibbs 		struct ccb_setasync csa;
28876babe50SJustin T. Gibbs 
28976babe50SJustin T. Gibbs                 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
29076babe50SJustin T. Gibbs                 csa.ccb_h.func_code = XPT_SASYNC_CB;
29176babe50SJustin T. Gibbs                 csa.event_enable = AC_FOUND_DEVICE;
29276babe50SJustin T. Gibbs                 csa.callback = ptasync;
29376babe50SJustin T. Gibbs                 csa.callback_arg = NULL;
29476babe50SJustin T. Gibbs                 xpt_action((union ccb *)&csa);
29576babe50SJustin T. Gibbs 		status = csa.ccb_h.status;
29676babe50SJustin T. Gibbs                 xpt_free_path(path);
29776babe50SJustin T. Gibbs         }
29876babe50SJustin T. Gibbs 
29976babe50SJustin T. Gibbs 	if (status != CAM_REQ_CMP) {
30076babe50SJustin T. Gibbs 		printf("pt: Failed to attach master async callback "
30176babe50SJustin T. Gibbs 		       "due to status 0x%x!\n", status);
30276babe50SJustin T. Gibbs 	}
30376babe50SJustin T. Gibbs }
30476babe50SJustin T. Gibbs 
30576babe50SJustin T. Gibbs static cam_status
30676babe50SJustin T. Gibbs ptctor(struct cam_periph *periph, void *arg)
30776babe50SJustin T. Gibbs {
30876babe50SJustin T. Gibbs 	struct pt_softc *softc;
30976babe50SJustin T. Gibbs 	struct ccb_setasync csa;
31076babe50SJustin T. Gibbs 	struct ccb_getdev *cgd;
31176babe50SJustin T. Gibbs 
31276babe50SJustin T. Gibbs 	cgd = (struct ccb_getdev *)arg;
31376babe50SJustin T. Gibbs 	if (periph == NULL) {
31476babe50SJustin T. Gibbs 		printf("ptregister: periph was NULL!!\n");
31576babe50SJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
31676babe50SJustin T. Gibbs 	}
31776babe50SJustin T. Gibbs 
31876babe50SJustin T. Gibbs 	if (cgd == NULL) {
31976babe50SJustin T. Gibbs 		printf("ptregister: no getdev CCB, can't register device\n");
32076babe50SJustin T. Gibbs 		return(CAM_REQ_CMP_ERR);
32176babe50SJustin T. Gibbs 	}
32276babe50SJustin T. Gibbs 
32376babe50SJustin T. Gibbs 	softc = (struct pt_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT);
32476babe50SJustin T. Gibbs 
32576babe50SJustin T. Gibbs 	if (softc == NULL) {
32676babe50SJustin T. Gibbs 		printf("daregister: 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 	LIST_INIT(&softc->pending_ccbs);
33376babe50SJustin T. Gibbs 	softc->state = PT_STATE_NORMAL;
3348177437dSPoul-Henning Kamp 	bioq_init(&softc->bio_queue);
33576babe50SJustin T. Gibbs 
3363ece1bd2SKenneth D. Merry 	softc->io_timeout = SCSI_PT_DEFAULT_TIMEOUT * 1000;
3373ece1bd2SKenneth D. Merry 
33876babe50SJustin T. Gibbs 	periph->softc = softc;
33976babe50SJustin T. Gibbs 
34076babe50SJustin T. Gibbs 	cam_extend_set(ptperiphs, periph->unit_number, periph);
34176babe50SJustin T. Gibbs 
34276babe50SJustin T. Gibbs 	devstat_add_entry(&softc->device_stats, "pt",
34376babe50SJustin T. Gibbs 			  periph->unit_number, 0,
34476babe50SJustin T. Gibbs 			  DEVSTAT_NO_BLOCKSIZE,
34510b6172aSMatt Jacob 			  SID_TYPE(&cgd->inq_data) | DEVSTAT_TYPE_IF_SCSI,
3462a888f93SKenneth D. Merry 			  DEVSTAT_PRIORITY_OTHER);
34776babe50SJustin T. Gibbs 
34873d26919SKenneth D. Merry 	softc->dev = make_dev(&pt_cdevsw, periph->unit_number, UID_ROOT,
34973d26919SKenneth D. Merry 			      GID_OPERATOR, 0600, "%s%d", periph->periph_name,
35073d26919SKenneth D. Merry 			      periph->unit_number);
35176babe50SJustin T. Gibbs 	/*
35276babe50SJustin T. Gibbs 	 * Add async callbacks for bus reset and
35376babe50SJustin T. Gibbs 	 * bus device reset calls.  I don't bother
35476babe50SJustin T. Gibbs 	 * checking if this fails as, in most cases,
35576babe50SJustin T. Gibbs 	 * the system will function just fine without
35676babe50SJustin T. Gibbs 	 * them and the only alternative would be to
35776babe50SJustin T. Gibbs 	 * not attach the device on failure.
35876babe50SJustin T. Gibbs 	 */
35976babe50SJustin T. Gibbs 	xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5);
36076babe50SJustin T. Gibbs 	csa.ccb_h.func_code = XPT_SASYNC_CB;
36176babe50SJustin T. Gibbs 	csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
36276babe50SJustin T. Gibbs 	csa.callback = ptasync;
36376babe50SJustin T. Gibbs 	csa.callback_arg = periph;
36476babe50SJustin T. Gibbs 	xpt_action((union ccb *)&csa);
36576babe50SJustin T. Gibbs 
36676babe50SJustin T. Gibbs 	/* Tell the user we've attached to the device */
36776babe50SJustin T. Gibbs 	xpt_announce_periph(periph, NULL);
36876babe50SJustin T. Gibbs 
36976babe50SJustin T. Gibbs 	return(CAM_REQ_CMP);
37076babe50SJustin T. Gibbs }
37176babe50SJustin T. Gibbs 
37276babe50SJustin T. Gibbs static void
373ee9c90c7SKenneth D. Merry ptoninvalidate(struct cam_periph *periph)
374ee9c90c7SKenneth D. Merry {
375ee9c90c7SKenneth D. Merry 	int s;
376ee9c90c7SKenneth D. Merry 	struct pt_softc *softc;
3778177437dSPoul-Henning Kamp 	struct bio *q_bp;
378ee9c90c7SKenneth D. Merry 	struct ccb_setasync csa;
379ee9c90c7SKenneth D. Merry 
380ee9c90c7SKenneth D. Merry 	softc = (struct pt_softc *)periph->softc;
381ee9c90c7SKenneth D. Merry 
382ee9c90c7SKenneth D. Merry 	/*
383ee9c90c7SKenneth D. Merry 	 * De-register any async callbacks.
384ee9c90c7SKenneth D. Merry 	 */
385ee9c90c7SKenneth D. Merry 	xpt_setup_ccb(&csa.ccb_h, periph->path,
386ee9c90c7SKenneth D. Merry 		      /* priority */ 5);
387ee9c90c7SKenneth D. Merry 	csa.ccb_h.func_code = XPT_SASYNC_CB;
388ee9c90c7SKenneth D. Merry 	csa.event_enable = 0;
389ee9c90c7SKenneth D. Merry 	csa.callback = ptasync;
390ee9c90c7SKenneth D. Merry 	csa.callback_arg = periph;
391ee9c90c7SKenneth D. Merry 	xpt_action((union ccb *)&csa);
392ee9c90c7SKenneth D. Merry 
393ee9c90c7SKenneth D. Merry 	softc->flags |= PT_FLAG_DEVICE_INVALID;
394ee9c90c7SKenneth D. Merry 
395ee9c90c7SKenneth D. Merry 	/*
396ee9c90c7SKenneth D. Merry 	 * Although the oninvalidate() routines are always called at
397ee9c90c7SKenneth D. Merry 	 * splsoftcam, we need to be at splbio() here to keep the buffer
398ee9c90c7SKenneth D. Merry 	 * queue from being modified while we traverse it.
399ee9c90c7SKenneth D. Merry 	 */
400ee9c90c7SKenneth D. Merry 	s = splbio();
401ee9c90c7SKenneth D. Merry 
402ee9c90c7SKenneth D. Merry 	/*
403ee9c90c7SKenneth D. Merry 	 * Return all queued I/O with ENXIO.
404ee9c90c7SKenneth D. Merry 	 * XXX Handle any transactions queued to the card
405ee9c90c7SKenneth D. Merry 	 *     with XPT_ABORT_CCB.
406ee9c90c7SKenneth D. Merry 	 */
4078177437dSPoul-Henning Kamp 	while ((q_bp = bioq_first(&softc->bio_queue)) != NULL){
4088177437dSPoul-Henning Kamp 		bioq_remove(&softc->bio_queue, q_bp);
4098177437dSPoul-Henning Kamp 		q_bp->bio_resid = q_bp->bio_bcount;
410a468031cSPoul-Henning Kamp 		biofinish(q_bp, NULL, ENXIO);
411ee9c90c7SKenneth D. Merry 	}
412ee9c90c7SKenneth D. Merry 
413ee9c90c7SKenneth D. Merry 	splx(s);
414ee9c90c7SKenneth D. Merry 
415ee9c90c7SKenneth D. Merry 	xpt_print_path(periph->path);
416ee9c90c7SKenneth D. Merry 	printf("lost device\n");
417ee9c90c7SKenneth D. Merry }
418ee9c90c7SKenneth D. Merry 
419ee9c90c7SKenneth D. Merry static void
42076babe50SJustin T. Gibbs ptdtor(struct cam_periph *periph)
42176babe50SJustin T. Gibbs {
422ee9c90c7SKenneth D. Merry 	struct pt_softc *softc;
423ee9c90c7SKenneth D. Merry 
424ee9c90c7SKenneth D. Merry 	softc = (struct pt_softc *)periph->softc;
425ee9c90c7SKenneth D. Merry 
426ee9c90c7SKenneth D. Merry 	devstat_remove_entry(&softc->device_stats);
427ee9c90c7SKenneth D. Merry 
42873d26919SKenneth D. Merry 	destroy_dev(softc->dev);
42973d26919SKenneth D. Merry 
43076babe50SJustin T. Gibbs 	cam_extend_release(ptperiphs, periph->unit_number);
43176babe50SJustin T. Gibbs 	xpt_print_path(periph->path);
43276babe50SJustin T. Gibbs 	printf("removing device entry\n");
433ee9c90c7SKenneth D. Merry 	free(softc, M_DEVBUF);
43476babe50SJustin T. Gibbs }
43576babe50SJustin T. Gibbs 
43676babe50SJustin T. Gibbs static void
43776babe50SJustin T. Gibbs ptasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
43876babe50SJustin T. Gibbs {
43976babe50SJustin T. Gibbs 	struct cam_periph *periph;
44076babe50SJustin T. Gibbs 
44176babe50SJustin T. Gibbs 	periph = (struct cam_periph *)callback_arg;
44276babe50SJustin T. Gibbs 	switch (code) {
44376babe50SJustin T. Gibbs 	case AC_FOUND_DEVICE:
44476babe50SJustin T. Gibbs 	{
44576babe50SJustin T. Gibbs 		struct ccb_getdev *cgd;
44676babe50SJustin T. Gibbs 		cam_status status;
44776babe50SJustin T. Gibbs 
44876babe50SJustin T. Gibbs 		cgd = (struct ccb_getdev *)arg;
449c5ff3b2fSMatt Jacob 		if (cgd == NULL)
450c5ff3b2fSMatt Jacob 			break;
45176babe50SJustin T. Gibbs 
45210b6172aSMatt Jacob 		if (SID_TYPE(&cgd->inq_data) != T_PROCESSOR)
45376babe50SJustin T. Gibbs 			break;
45476babe50SJustin T. Gibbs 
45576babe50SJustin T. Gibbs 		/*
45676babe50SJustin T. Gibbs 		 * Allocate a peripheral instance for
45776babe50SJustin T. Gibbs 		 * this device and start the probe
45876babe50SJustin T. Gibbs 		 * process.
45976babe50SJustin T. Gibbs 		 */
460ee9c90c7SKenneth D. Merry 		status = cam_periph_alloc(ptctor, ptoninvalidate, ptdtor,
461ee9c90c7SKenneth D. Merry 					  ptstart, "pt", CAM_PERIPH_BIO,
462ee9c90c7SKenneth D. Merry 					  cgd->ccb_h.path, ptasync,
463ee9c90c7SKenneth D. Merry 					  AC_FOUND_DEVICE, cgd);
46476babe50SJustin T. Gibbs 
46576babe50SJustin T. Gibbs 		if (status != CAM_REQ_CMP
46676babe50SJustin T. Gibbs 		 && status != CAM_REQ_INPROG)
46776babe50SJustin T. Gibbs 			printf("ptasync: Unable to attach to new device "
46876babe50SJustin T. Gibbs 				"due to status 0x%x\n", status);
46976babe50SJustin T. Gibbs 		break;
47076babe50SJustin T. Gibbs 	}
47176babe50SJustin T. Gibbs 	case AC_SENT_BDR:
47276babe50SJustin T. Gibbs 	case AC_BUS_RESET:
47376babe50SJustin T. Gibbs 	{
47476babe50SJustin T. Gibbs 		struct pt_softc *softc;
47576babe50SJustin T. Gibbs 		struct ccb_hdr *ccbh;
47676babe50SJustin T. Gibbs 		int s;
47776babe50SJustin T. Gibbs 
47876babe50SJustin T. Gibbs 		softc = (struct pt_softc *)periph->softc;
47976babe50SJustin T. Gibbs 		s = splsoftcam();
48076babe50SJustin T. Gibbs 		/*
48176babe50SJustin T. Gibbs 		 * Don't fail on the expected unit attention
48276babe50SJustin T. Gibbs 		 * that will occur.
48376babe50SJustin T. Gibbs 		 */
48476babe50SJustin T. Gibbs 		softc->flags |= PT_FLAG_RETRY_UA;
485fc2ffbe6SPoul-Henning Kamp 		LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
48676babe50SJustin T. Gibbs 			ccbh->ccb_state |= PT_CCB_RETRY_UA;
48776babe50SJustin T. Gibbs 		splx(s);
488516871c6SJustin T. Gibbs 		/* FALLTHROUGH */
48976babe50SJustin T. Gibbs 	}
49076babe50SJustin T. Gibbs 	default:
491516871c6SJustin T. Gibbs 		cam_periph_async(periph, code, path, arg);
49276babe50SJustin T. Gibbs 		break;
49376babe50SJustin T. Gibbs 	}
49476babe50SJustin T. Gibbs }
49576babe50SJustin T. Gibbs 
49676babe50SJustin T. Gibbs static void
49776babe50SJustin T. Gibbs ptstart(struct cam_periph *periph, union ccb *start_ccb)
49876babe50SJustin T. Gibbs {
49976babe50SJustin T. Gibbs 	struct pt_softc *softc;
5008177437dSPoul-Henning Kamp 	struct bio *bp;
50176babe50SJustin T. Gibbs 	int s;
50276babe50SJustin T. Gibbs 
50376babe50SJustin T. Gibbs 	softc = (struct pt_softc *)periph->softc;
50476babe50SJustin T. Gibbs 
50576babe50SJustin T. Gibbs 	/*
50676babe50SJustin T. Gibbs 	 * See if there is a buf with work for us to do..
50776babe50SJustin T. Gibbs 	 */
50876babe50SJustin T. Gibbs 	s = splbio();
5098177437dSPoul-Henning Kamp 	bp = bioq_first(&softc->bio_queue);
51076babe50SJustin T. Gibbs 	if (periph->immediate_priority <= periph->pinfo.priority) {
51176babe50SJustin T. Gibbs 		CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
51276babe50SJustin T. Gibbs 				("queuing for immediate ccb\n"));
51376babe50SJustin T. Gibbs 		start_ccb->ccb_h.ccb_state = PT_CCB_WAITING;
51476babe50SJustin T. Gibbs 		SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
51576babe50SJustin T. Gibbs 				  periph_links.sle);
51676babe50SJustin T. Gibbs 		periph->immediate_priority = CAM_PRIORITY_NONE;
51776babe50SJustin T. Gibbs 		splx(s);
51876babe50SJustin T. Gibbs 		wakeup(&periph->ccb_list);
51976babe50SJustin T. Gibbs 	} else if (bp == NULL) {
52076babe50SJustin T. Gibbs 		splx(s);
52176babe50SJustin T. Gibbs 		xpt_release_ccb(start_ccb);
52276babe50SJustin T. Gibbs 	} else {
52376babe50SJustin T. Gibbs 		int oldspl;
52476babe50SJustin T. Gibbs 
5258177437dSPoul-Henning Kamp 		bioq_remove(&softc->bio_queue, bp);
52676babe50SJustin T. Gibbs 
52776babe50SJustin T. Gibbs 		devstat_start_transaction(&softc->device_stats);
52876babe50SJustin T. Gibbs 
52976babe50SJustin T. Gibbs 		scsi_send_receive(&start_ccb->csio,
53076babe50SJustin T. Gibbs 				  /*retries*/4,
53176babe50SJustin T. Gibbs 				  ptdone,
53276babe50SJustin T. Gibbs 				  MSG_SIMPLE_Q_TAG,
5338177437dSPoul-Henning Kamp 				  bp->bio_cmd == BIO_READ,
53476babe50SJustin T. Gibbs 				  /*byte2*/0,
5358177437dSPoul-Henning Kamp 				  bp->bio_bcount,
5368177437dSPoul-Henning Kamp 				  bp->bio_data,
53776babe50SJustin T. Gibbs 				  /*sense_len*/SSD_FULL_SIZE,
5383ece1bd2SKenneth D. Merry 				  /*timeout*/softc->io_timeout);
53976babe50SJustin T. Gibbs 
5409c6a0920SKenneth D. Merry 		start_ccb->ccb_h.ccb_state = PT_CCB_BUFFER_IO_UA;
54176babe50SJustin T. Gibbs 
54276babe50SJustin T. Gibbs 		/*
54376babe50SJustin T. Gibbs 		 * Block out any asyncronous callbacks
54476babe50SJustin T. Gibbs 		 * while we touch the pending ccb list.
54576babe50SJustin T. Gibbs 		 */
54676babe50SJustin T. Gibbs 		oldspl = splcam();
54776babe50SJustin T. Gibbs 		LIST_INSERT_HEAD(&softc->pending_ccbs, &start_ccb->ccb_h,
54876babe50SJustin T. Gibbs 				 periph_links.le);
54976babe50SJustin T. Gibbs 		splx(oldspl);
55076babe50SJustin T. Gibbs 
55176babe50SJustin T. Gibbs 		start_ccb->ccb_h.ccb_bp = bp;
5528177437dSPoul-Henning Kamp 		bp = bioq_first(&softc->bio_queue);
55376babe50SJustin T. Gibbs 		splx(s);
55476babe50SJustin T. Gibbs 
55576babe50SJustin T. Gibbs 		xpt_action(start_ccb);
55676babe50SJustin T. Gibbs 
55776babe50SJustin T. Gibbs 		if (bp != NULL) {
55876babe50SJustin T. Gibbs 			/* Have more work to do, so ensure we stay scheduled */
55976babe50SJustin T. Gibbs 			xpt_schedule(periph, /* XXX priority */1);
56076babe50SJustin T. Gibbs 		}
56176babe50SJustin T. Gibbs 	}
56276babe50SJustin T. Gibbs }
56376babe50SJustin T. Gibbs 
56476babe50SJustin T. Gibbs static void
56576babe50SJustin T. Gibbs ptdone(struct cam_periph *periph, union ccb *done_ccb)
56676babe50SJustin T. Gibbs {
56776babe50SJustin T. Gibbs 	struct pt_softc *softc;
56876babe50SJustin T. Gibbs 	struct ccb_scsiio *csio;
56976babe50SJustin T. Gibbs 
57076babe50SJustin T. Gibbs 	softc = (struct pt_softc *)periph->softc;
57176babe50SJustin T. Gibbs 	csio = &done_ccb->csio;
57276babe50SJustin T. Gibbs 	switch (csio->ccb_h.ccb_state) {
57376babe50SJustin T. Gibbs 	case PT_CCB_BUFFER_IO:
57476babe50SJustin T. Gibbs 	case PT_CCB_BUFFER_IO_UA:
57576babe50SJustin T. Gibbs 	{
5768177437dSPoul-Henning Kamp 		struct bio *bp;
57776babe50SJustin T. Gibbs 		int    oldspl;
57876babe50SJustin T. Gibbs 
5798177437dSPoul-Henning Kamp 		bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
58076babe50SJustin T. Gibbs 		if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
58176babe50SJustin T. Gibbs 			int error;
58276babe50SJustin T. Gibbs 			int s;
58376babe50SJustin T. Gibbs 			int sf;
58476babe50SJustin T. Gibbs 
58576babe50SJustin T. Gibbs 			if ((csio->ccb_h.ccb_state & PT_CCB_RETRY_UA) != 0)
58676babe50SJustin T. Gibbs 				sf = SF_RETRY_UA;
58776babe50SJustin T. Gibbs 			else
58876babe50SJustin T. Gibbs 				sf = 0;
58976babe50SJustin T. Gibbs 
5903393f8daSKenneth D. Merry 			error = pterror(done_ccb, CAM_RETRY_SELTO, sf);
5913393f8daSKenneth D. Merry 			if (error == ERESTART) {
59276babe50SJustin T. Gibbs 				/*
59376babe50SJustin T. Gibbs 				 * A retry was scheuled, so
59476babe50SJustin T. Gibbs 				 * just return.
59576babe50SJustin T. Gibbs 				 */
59676babe50SJustin T. Gibbs 				return;
59776babe50SJustin T. Gibbs 			}
59876babe50SJustin T. Gibbs 			if (error != 0) {
5998177437dSPoul-Henning Kamp 				struct bio *q_bp;
60076babe50SJustin T. Gibbs 
60176babe50SJustin T. Gibbs 				s = splbio();
60276babe50SJustin T. Gibbs 
60376babe50SJustin T. Gibbs 				if (error == ENXIO) {
60476babe50SJustin T. Gibbs 					/*
60576babe50SJustin T. Gibbs 					 * Catastrophic error.  Mark our device
60676babe50SJustin T. Gibbs 					 * as invalid.
60776babe50SJustin T. Gibbs 					 */
60876babe50SJustin T. Gibbs 					xpt_print_path(periph->path);
60976babe50SJustin T. Gibbs 					printf("Invalidating device\n");
61076babe50SJustin T. Gibbs 					softc->flags |= PT_FLAG_DEVICE_INVALID;
61176babe50SJustin T. Gibbs 				}
61276babe50SJustin T. Gibbs 
61376babe50SJustin T. Gibbs 				/*
61476babe50SJustin T. Gibbs 				 * return all queued I/O with EIO, so that
61576babe50SJustin T. Gibbs 				 * the client can retry these I/Os in the
61676babe50SJustin T. Gibbs 				 * proper order should it attempt to recover.
61776babe50SJustin T. Gibbs 				 */
6188177437dSPoul-Henning Kamp 				while ((q_bp = bioq_first(&softc->bio_queue))
61976babe50SJustin T. Gibbs 					!= NULL) {
6208177437dSPoul-Henning Kamp 					bioq_remove(&softc->bio_queue, q_bp);
6218177437dSPoul-Henning Kamp 					q_bp->bio_resid = q_bp->bio_bcount;
622a468031cSPoul-Henning Kamp 					biofinish(q_bp, NULL, EIO);
62376babe50SJustin T. Gibbs 				}
62476babe50SJustin T. Gibbs 				splx(s);
6258177437dSPoul-Henning Kamp 				bp->bio_error = error;
6268177437dSPoul-Henning Kamp 				bp->bio_resid = bp->bio_bcount;
6278177437dSPoul-Henning Kamp 				bp->bio_flags |= BIO_ERROR;
62876babe50SJustin T. Gibbs 			} else {
6298177437dSPoul-Henning Kamp 				bp->bio_resid = csio->resid;
6308177437dSPoul-Henning Kamp 				bp->bio_error = 0;
6318177437dSPoul-Henning Kamp 				if (bp->bio_resid != 0) {
63276babe50SJustin T. Gibbs 					/* Short transfer ??? */
6338177437dSPoul-Henning Kamp 					bp->bio_flags |= BIO_ERROR;
63476babe50SJustin T. Gibbs 				}
63576babe50SJustin T. Gibbs 			}
63676babe50SJustin T. Gibbs 			if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
63776babe50SJustin T. Gibbs 				cam_release_devq(done_ccb->ccb_h.path,
63876babe50SJustin T. Gibbs 						 /*relsim_flags*/0,
63976babe50SJustin T. Gibbs 						 /*reduction*/0,
64076babe50SJustin T. Gibbs 						 /*timeout*/0,
64176babe50SJustin T. Gibbs 						 /*getcount_only*/0);
64276babe50SJustin T. Gibbs 		} else {
6438177437dSPoul-Henning Kamp 			bp->bio_resid = csio->resid;
6448177437dSPoul-Henning Kamp 			if (bp->bio_resid != 0)
6458177437dSPoul-Henning Kamp 				bp->bio_flags |= BIO_ERROR;
64676babe50SJustin T. Gibbs 		}
64776babe50SJustin T. Gibbs 
64876babe50SJustin T. Gibbs 		/*
64976babe50SJustin T. Gibbs 		 * Block out any asyncronous callbacks
65076babe50SJustin T. Gibbs 		 * while we touch the pending ccb list.
65176babe50SJustin T. Gibbs 		 */
65276babe50SJustin T. Gibbs 		oldspl = splcam();
65376babe50SJustin T. Gibbs 		LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
65476babe50SJustin T. Gibbs 		splx(oldspl);
65576babe50SJustin T. Gibbs 
656a468031cSPoul-Henning Kamp 		biofinish(bp, &softc->device_stats, 0);
65776babe50SJustin T. Gibbs 		break;
65876babe50SJustin T. Gibbs 	}
65976babe50SJustin T. Gibbs 	case PT_CCB_WAITING:
66076babe50SJustin T. Gibbs 		/* Caller will release the CCB */
66176babe50SJustin T. Gibbs 		wakeup(&done_ccb->ccb_h.cbfcnp);
66276babe50SJustin T. Gibbs 		return;
66376babe50SJustin T. Gibbs 	}
66476babe50SJustin T. Gibbs 	xpt_release_ccb(done_ccb);
66576babe50SJustin T. Gibbs }
66676babe50SJustin T. Gibbs 
66776babe50SJustin T. Gibbs static int
66876babe50SJustin T. Gibbs pterror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
66976babe50SJustin T. Gibbs {
67076babe50SJustin T. Gibbs 	struct pt_softc	  *softc;
67176babe50SJustin T. Gibbs 	struct cam_periph *periph;
67276babe50SJustin T. Gibbs 
67376babe50SJustin T. Gibbs 	periph = xpt_path_periph(ccb->ccb_h.path);
67476babe50SJustin T. Gibbs 	softc = (struct pt_softc *)periph->softc;
67576babe50SJustin T. Gibbs 
67676babe50SJustin T. Gibbs 	return(cam_periph_error(ccb, cam_flags, sense_flags,
67776babe50SJustin T. Gibbs 				&softc->saved_ccb));
67876babe50SJustin T. Gibbs }
67976babe50SJustin T. Gibbs 
6803ece1bd2SKenneth D. Merry static int
6813ece1bd2SKenneth D. Merry ptioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
6823ece1bd2SKenneth D. Merry {
6833ece1bd2SKenneth D. Merry 	struct cam_periph *periph;
6843ece1bd2SKenneth D. Merry 	struct pt_softc *softc;
6853ece1bd2SKenneth D. Merry 	int unit;
6863ece1bd2SKenneth D. Merry 	int error;
6873ece1bd2SKenneth D. Merry 
6883ece1bd2SKenneth D. Merry 	unit = minor(dev);
6893ece1bd2SKenneth D. Merry 	periph = cam_extend_get(ptperiphs, unit);
6903ece1bd2SKenneth D. Merry 
6913ece1bd2SKenneth D. Merry 	if (periph == NULL)
6923ece1bd2SKenneth D. Merry 		return(ENXIO);
6933ece1bd2SKenneth D. Merry 
6943ece1bd2SKenneth D. Merry 	softc = (struct pt_softc *)periph->softc;
6953ece1bd2SKenneth D. Merry 
6963ece1bd2SKenneth D. Merry 	if ((error = cam_periph_lock(periph, PRIBIO|PCATCH)) != 0) {
6973ece1bd2SKenneth D. Merry 		return (error); /* error code from tsleep */
6983ece1bd2SKenneth D. Merry 	}
6993ece1bd2SKenneth D. Merry 
7003ece1bd2SKenneth D. Merry 	switch(cmd) {
7013ece1bd2SKenneth D. Merry 	case PTIOCGETTIMEOUT:
7023ece1bd2SKenneth D. Merry 		if (softc->io_timeout >= 1000)
7033ece1bd2SKenneth D. Merry 			*(int *)addr = softc->io_timeout / 1000;
7043ece1bd2SKenneth D. Merry 		else
7053ece1bd2SKenneth D. Merry 			*(int *)addr = 0;
7063ece1bd2SKenneth D. Merry 		break;
7073ece1bd2SKenneth D. Merry 	case PTIOCSETTIMEOUT:
7083ece1bd2SKenneth D. Merry 	{
7093ece1bd2SKenneth D. Merry 		int s;
7103ece1bd2SKenneth D. Merry 
7113ece1bd2SKenneth D. Merry 		if (*(int *)addr < 1) {
7123ece1bd2SKenneth D. Merry 			error = EINVAL;
7133ece1bd2SKenneth D. Merry 			break;
7143ece1bd2SKenneth D. Merry 		}
7153ece1bd2SKenneth D. Merry 
7163ece1bd2SKenneth D. Merry 		s = splsoftcam();
7173ece1bd2SKenneth D. Merry 		softc->io_timeout = *(int *)addr * 1000;
7183ece1bd2SKenneth D. Merry 		splx(s);
7193ece1bd2SKenneth D. Merry 
7203ece1bd2SKenneth D. Merry 		break;
7213ece1bd2SKenneth D. Merry 	}
7223ece1bd2SKenneth D. Merry 	default:
7233ece1bd2SKenneth D. Merry 		error = cam_periph_ioctl(periph, cmd, addr, pterror);
7243ece1bd2SKenneth D. Merry 		break;
7253ece1bd2SKenneth D. Merry 	}
7263ece1bd2SKenneth D. Merry 
7273ece1bd2SKenneth D. Merry 	cam_periph_unlock(periph);
7283ece1bd2SKenneth D. Merry 
7293ece1bd2SKenneth D. Merry 	return(error);
7303ece1bd2SKenneth D. Merry }
7313ece1bd2SKenneth D. Merry 
73276babe50SJustin T. Gibbs void
73376babe50SJustin T. Gibbs scsi_send_receive(struct ccb_scsiio *csio, u_int32_t retries,
73476babe50SJustin T. Gibbs 		  void (*cbfcnp)(struct cam_periph *, union ccb *),
73576babe50SJustin T. Gibbs 		  u_int tag_action, int readop, u_int byte2,
73676babe50SJustin T. Gibbs 		  u_int32_t xfer_len, u_int8_t *data_ptr, u_int8_t sense_len,
73776babe50SJustin T. Gibbs 		  u_int32_t timeout)
73876babe50SJustin T. Gibbs {
73976babe50SJustin T. Gibbs 	struct scsi_send_receive *scsi_cmd;
74076babe50SJustin T. Gibbs 
74176babe50SJustin T. Gibbs 	scsi_cmd = (struct scsi_send_receive *)&csio->cdb_io.cdb_bytes;
74276babe50SJustin T. Gibbs 	scsi_cmd->opcode = readop ? RECEIVE : SEND;
74376babe50SJustin T. Gibbs 	scsi_cmd->byte2 = byte2;
74476babe50SJustin T. Gibbs 	scsi_ulto3b(xfer_len, scsi_cmd->xfer_len);
74576babe50SJustin T. Gibbs 	scsi_cmd->control = 0;
74676babe50SJustin T. Gibbs 
74776babe50SJustin T. Gibbs 	cam_fill_csio(csio,
74876babe50SJustin T. Gibbs 		      retries,
74976babe50SJustin T. Gibbs 		      cbfcnp,
75076babe50SJustin T. Gibbs 		      /*flags*/readop ? CAM_DIR_IN : CAM_DIR_OUT,
75176babe50SJustin T. Gibbs 		      tag_action,
75276babe50SJustin T. Gibbs 		      data_ptr,
75376babe50SJustin T. Gibbs 		      xfer_len,
75476babe50SJustin T. Gibbs 		      sense_len,
75576babe50SJustin T. Gibbs 		      sizeof(*scsi_cmd),
75676babe50SJustin T. Gibbs 		      timeout);
75776babe50SJustin T. Gibbs }
758