xref: /freebsd/sys/cam/scsi/scsi_sg.c (revision cf454e30b64cfe4ec620adef09561f292de6acd3)
11eba4c79SScott Long /*-
21eba4c79SScott Long  * Copyright (c) 2007 Scott Long
31eba4c79SScott Long  * All rights reserved.
41eba4c79SScott Long  *
51eba4c79SScott Long  * Redistribution and use in source and binary forms, with or without
61eba4c79SScott Long  * modification, are permitted provided that the following conditions
71eba4c79SScott Long  * are met:
81eba4c79SScott Long  * 1. Redistributions of source code must retain the above copyright
91eba4c79SScott Long  *    notice, this list of conditions, and the following disclaimer,
101eba4c79SScott Long  *    without modification, immediately at the beginning of the file.
111eba4c79SScott Long  * 2. The name of the author may not be used to endorse or promote products
121eba4c79SScott Long  *    derived from this software without specific prior written permission.
131eba4c79SScott Long  *
141eba4c79SScott Long  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
151eba4c79SScott Long  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
161eba4c79SScott Long  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
171eba4c79SScott Long  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
181eba4c79SScott Long  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
191eba4c79SScott Long  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
201eba4c79SScott Long  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
211eba4c79SScott Long  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
221eba4c79SScott Long  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
231eba4c79SScott Long  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
241eba4c79SScott Long  * SUCH DAMAGE.
251eba4c79SScott Long  */
261eba4c79SScott Long 
271eba4c79SScott Long /*
281eba4c79SScott Long  * scsi_sg peripheral driver.  This driver is meant to implement the Linux
291eba4c79SScott Long  * SG passthrough interface for SCSI.
301eba4c79SScott Long  */
311eba4c79SScott Long 
321eba4c79SScott Long #include <sys/cdefs.h>
331eba4c79SScott Long __FBSDID("$FreeBSD$");
341eba4c79SScott Long 
351eba4c79SScott Long #include <sys/param.h>
361eba4c79SScott Long #include <sys/systm.h>
371eba4c79SScott Long #include <sys/kernel.h>
381eba4c79SScott Long #include <sys/types.h>
391eba4c79SScott Long #include <sys/bio.h>
401eba4c79SScott Long #include <sys/malloc.h>
411eba4c79SScott Long #include <sys/fcntl.h>
421eba4c79SScott Long #include <sys/ioccom.h>
431eba4c79SScott Long #include <sys/conf.h>
441eba4c79SScott Long #include <sys/errno.h>
451eba4c79SScott Long #include <sys/devicestat.h>
461eba4c79SScott Long #include <sys/proc.h>
471eba4c79SScott Long #include <sys/uio.h>
481eba4c79SScott Long 
491eba4c79SScott Long #include <cam/cam.h>
501eba4c79SScott Long #include <cam/cam_ccb.h>
511eba4c79SScott Long #include <cam/cam_periph.h>
521eba4c79SScott Long #include <cam/cam_queue.h>
531eba4c79SScott Long #include <cam/cam_xpt_periph.h>
541eba4c79SScott Long #include <cam/cam_debug.h>
551eba4c79SScott Long #include <cam/cam_sim.h>
561eba4c79SScott Long 
571eba4c79SScott Long #include <cam/scsi/scsi_all.h>
581eba4c79SScott Long #include <cam/scsi/scsi_message.h>
591eba4c79SScott Long #include <cam/scsi/scsi_sg.h>
601eba4c79SScott Long 
611eba4c79SScott Long #include <compat/linux/linux_ioctl.h>
621eba4c79SScott Long 
631eba4c79SScott Long typedef enum {
641eba4c79SScott Long 	SG_FLAG_OPEN		= 0x01,
651eba4c79SScott Long 	SG_FLAG_LOCKED		= 0x02,
661eba4c79SScott Long 	SG_FLAG_INVALID		= 0x04
671eba4c79SScott Long } sg_flags;
681eba4c79SScott Long 
691eba4c79SScott Long typedef enum {
701eba4c79SScott Long 	SG_STATE_NORMAL
711eba4c79SScott Long } sg_state;
721eba4c79SScott Long 
731eba4c79SScott Long typedef enum {
74472cdbefSScott Long 	SG_RDWR_FREE,
751eba4c79SScott Long 	SG_RDWR_INPROG,
761eba4c79SScott Long 	SG_RDWR_DONE
771eba4c79SScott Long } sg_rdwr_state;
781eba4c79SScott Long 
791eba4c79SScott Long typedef enum {
801eba4c79SScott Long 	SG_CCB_RDWR_IO,
811eba4c79SScott Long 	SG_CCB_WAITING
821eba4c79SScott Long } sg_ccb_types;
831eba4c79SScott Long 
841eba4c79SScott Long #define ccb_type	ppriv_field0
851eba4c79SScott Long #define ccb_rdwr	ppriv_ptr1
861eba4c79SScott Long 
871eba4c79SScott Long struct sg_rdwr {
881eba4c79SScott Long 	TAILQ_ENTRY(sg_rdwr)	rdwr_link;
891eba4c79SScott Long 	int			tag;
901eba4c79SScott Long 	int			state;
911eba4c79SScott Long 	int			buf_len;
921eba4c79SScott Long 	char			*buf;
931eba4c79SScott Long 	union ccb		*ccb;
941eba4c79SScott Long 	union {
951eba4c79SScott Long 		struct sg_header hdr;
961eba4c79SScott Long 		struct sg_io_hdr io_hdr;
971eba4c79SScott Long 	} hdr;
981eba4c79SScott Long };
991eba4c79SScott Long 
1001eba4c79SScott Long struct sg_softc {
1011eba4c79SScott Long 	sg_state		state;
1021eba4c79SScott Long 	sg_flags		flags;
1031eba4c79SScott Long 	struct devstat		*device_stats;
1041eba4c79SScott Long 	TAILQ_HEAD(, sg_rdwr)	rdwr_done;
1051eba4c79SScott Long 	struct cdev		*dev;
106715ab212SScott Long 	int			sg_timeout;
107715ab212SScott Long 	int			sg_user_timeout;
108715ab212SScott Long 	uint8_t			pd_type;
1091eba4c79SScott Long 	union ccb		saved_ccb;
1101eba4c79SScott Long };
1111eba4c79SScott Long 
1121eba4c79SScott Long static d_open_t		sgopen;
1131eba4c79SScott Long static d_close_t	sgclose;
1141eba4c79SScott Long static d_ioctl_t	sgioctl;
1151eba4c79SScott Long static d_write_t	sgwrite;
1161eba4c79SScott Long static d_read_t		sgread;
1171eba4c79SScott Long 
1181eba4c79SScott Long static periph_init_t	sginit;
1191eba4c79SScott Long static periph_ctor_t	sgregister;
1201eba4c79SScott Long static periph_oninv_t	sgoninvalidate;
1211eba4c79SScott Long static periph_dtor_t	sgcleanup;
1221eba4c79SScott Long static periph_start_t	sgstart;
1231eba4c79SScott Long static void		sgasync(void *callback_arg, uint32_t code,
1241eba4c79SScott Long 				struct cam_path *path, void *arg);
1251eba4c79SScott Long static void		sgdone(struct cam_periph *periph, union ccb *done_ccb);
1261eba4c79SScott Long static int		sgsendccb(struct cam_periph *periph, union ccb *ccb);
1271eba4c79SScott Long static int		sgsendrdwr(struct cam_periph *periph, union ccb *ccb);
1281eba4c79SScott Long static int		sgerror(union ccb *ccb, uint32_t cam_flags,
1291eba4c79SScott Long 				uint32_t sense_flags);
1301eba4c79SScott Long static void		sg_scsiio_status(struct ccb_scsiio *csio,
1311eba4c79SScott Long 					 u_short *hoststat, u_short *drvstat);
1321eba4c79SScott Long 
1331eba4c79SScott Long static int		scsi_group_len(u_char cmd);
1341eba4c79SScott Long 
1351eba4c79SScott Long static struct periph_driver sgdriver =
1361eba4c79SScott Long {
1371eba4c79SScott Long 	sginit, "sg",
1381eba4c79SScott Long 	TAILQ_HEAD_INITIALIZER(sgdriver.units), /* gen */ 0
1391eba4c79SScott Long };
1401eba4c79SScott Long PERIPHDRIVER_DECLARE(sg, sgdriver);
1411eba4c79SScott Long 
1421eba4c79SScott Long static struct cdevsw sg_cdevsw = {
1431eba4c79SScott Long 	.d_version =	D_VERSION,
1441eba4c79SScott Long 	.d_flags =	D_NEEDGIANT,
1451eba4c79SScott Long 	.d_open =	sgopen,
1461eba4c79SScott Long 	.d_close =	sgclose,
1471eba4c79SScott Long 	.d_ioctl =	sgioctl,
1481eba4c79SScott Long 	.d_write =	sgwrite,
1491eba4c79SScott Long 	.d_read =	sgread,
1501eba4c79SScott Long 	.d_name =	"sg",
1511eba4c79SScott Long };
1521eba4c79SScott Long 
1531eba4c79SScott Long static int sg_version = 30125;
1541eba4c79SScott Long 
1551eba4c79SScott Long static void
1561eba4c79SScott Long sginit(void)
1571eba4c79SScott Long {
1581eba4c79SScott Long 	cam_status status;
1591eba4c79SScott Long 
1601eba4c79SScott Long 	/*
1611eba4c79SScott Long 	 * Install a global async callback.  This callback will receive aync
1621eba4c79SScott Long 	 * callbacks like "new device found".
1631eba4c79SScott Long 	 */
16485d92640SScott Long 	status = xpt_register_async(AC_FOUND_DEVICE, sgasync, NULL, NULL);
1651eba4c79SScott Long 
1661eba4c79SScott Long 	if (status != CAM_REQ_CMP) {
1671eba4c79SScott Long 		printf("sg: Failed to attach master async callbac "
1681eba4c79SScott Long 			"due to status 0x%x!\n", status);
1691eba4c79SScott Long 	}
1701eba4c79SScott Long }
1711eba4c79SScott Long 
1721eba4c79SScott Long static void
1731eba4c79SScott Long sgoninvalidate(struct cam_periph *periph)
1741eba4c79SScott Long {
1751eba4c79SScott Long 	struct sg_softc *softc;
1761eba4c79SScott Long 
1771eba4c79SScott Long 	softc = (struct sg_softc *)periph->softc;
1781eba4c79SScott Long 
1791eba4c79SScott Long 	/*
1801eba4c79SScott Long 	 * Deregister any async callbacks.
1811eba4c79SScott Long 	 */
18285d92640SScott Long 	xpt_register_async(0, sgasync, periph, periph->path);
1831eba4c79SScott Long 
1841eba4c79SScott Long 	softc->flags |= SG_FLAG_INVALID;
1851eba4c79SScott Long 
1861eba4c79SScott Long 	/*
1871eba4c79SScott Long 	 * XXX Return all queued I/O with ENXIO.
1881eba4c79SScott Long 	 * XXX Handle any transactions queued to the card
1891eba4c79SScott Long 	 *     with XPT_ABORT_CCB.
1901eba4c79SScott Long 	 */
1911eba4c79SScott Long 
1921eba4c79SScott Long 	if (bootverbose) {
1931eba4c79SScott Long 		xpt_print(periph->path, "lost device\n");
1941eba4c79SScott Long 	}
1951eba4c79SScott Long }
1961eba4c79SScott Long 
1971eba4c79SScott Long static void
1981eba4c79SScott Long sgcleanup(struct cam_periph *periph)
1991eba4c79SScott Long {
2001eba4c79SScott Long 	struct sg_softc *softc;
2011eba4c79SScott Long 
2021eba4c79SScott Long 	softc = (struct sg_softc *)periph->softc;
2035f3fed85SEdward Tomasz Napierala 	if (bootverbose)
2041eba4c79SScott Long 		xpt_print(periph->path, "removing device entry\n");
2055f3fed85SEdward Tomasz Napierala 	devstat_remove_entry(softc->device_stats);
2065f3fed85SEdward Tomasz Napierala 	cam_periph_unlock(periph);
2075f3fed85SEdward Tomasz Napierala 	destroy_dev(softc->dev);
2085f3fed85SEdward Tomasz Napierala 	cam_periph_lock(periph);
2091eba4c79SScott Long 	free(softc, M_DEVBUF);
2101eba4c79SScott Long }
2111eba4c79SScott Long 
2121eba4c79SScott Long static void
2131eba4c79SScott Long sgasync(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
2141eba4c79SScott Long {
2151eba4c79SScott Long 	struct cam_periph *periph;
2161eba4c79SScott Long 
2171eba4c79SScott Long 	periph = (struct cam_periph *)callback_arg;
2181eba4c79SScott Long 
2191eba4c79SScott Long 	switch (code) {
2201eba4c79SScott Long 	case AC_FOUND_DEVICE:
2211eba4c79SScott Long 	{
2221eba4c79SScott Long 		struct ccb_getdev *cgd;
2231eba4c79SScott Long 		cam_status status;
2241eba4c79SScott Long 
2251eba4c79SScott Long 		cgd = (struct ccb_getdev *)arg;
2261eba4c79SScott Long 		if (cgd == NULL)
2271eba4c79SScott Long 			break;
2281eba4c79SScott Long 
22952c9ce25SScott Long 		if (cgd->protocol != PROTO_SCSI)
23052c9ce25SScott Long 			break;
23152c9ce25SScott Long 
2321eba4c79SScott Long 		/*
2331eba4c79SScott Long 		 * Allocate a peripheral instance for this device and
2341eba4c79SScott Long 		 * start the probe process.
2351eba4c79SScott Long 		 */
2361eba4c79SScott Long 		status = cam_periph_alloc(sgregister, sgoninvalidate,
2371eba4c79SScott Long 					  sgcleanup, sgstart, "sg",
2381eba4c79SScott Long 					  CAM_PERIPH_BIO, cgd->ccb_h.path,
2391eba4c79SScott Long 					  sgasync, AC_FOUND_DEVICE, cgd);
2401eba4c79SScott Long 		if ((status != CAM_REQ_CMP) && (status != CAM_REQ_INPROG)) {
2411eba4c79SScott Long 			const struct cam_status_entry *entry;
2421eba4c79SScott Long 
2431eba4c79SScott Long 			entry = cam_fetch_status_entry(status);
2441eba4c79SScott Long 			printf("sgasync: Unable to attach new device "
2451eba4c79SScott Long 				"due to status %#x: %s\n", status, entry ?
2461eba4c79SScott Long 				entry->status_text : "Unknown");
2471eba4c79SScott Long 		}
2481eba4c79SScott Long 		break;
2491eba4c79SScott Long 	}
2501eba4c79SScott Long 	default:
2511eba4c79SScott Long 		cam_periph_async(periph, code, path, arg);
2521eba4c79SScott Long 		break;
2531eba4c79SScott Long 	}
2541eba4c79SScott Long }
2551eba4c79SScott Long 
2561eba4c79SScott Long static cam_status
2571eba4c79SScott Long sgregister(struct cam_periph *periph, void *arg)
2581eba4c79SScott Long {
2591eba4c79SScott Long 	struct sg_softc *softc;
2601eba4c79SScott Long 	struct ccb_getdev *cgd;
2611eba4c79SScott Long 	int no_tags;
2621eba4c79SScott Long 
2631eba4c79SScott Long 	cgd = (struct ccb_getdev *)arg;
2641eba4c79SScott Long 	if (periph == NULL) {
2651eba4c79SScott Long 		printf("sgregister: periph was NULL!!\n");
2661eba4c79SScott Long 		return (CAM_REQ_CMP_ERR);
2671eba4c79SScott Long 	}
2681eba4c79SScott Long 
2691eba4c79SScott Long 	if (cgd == NULL) {
2701eba4c79SScott Long 		printf("sgregister: no getdev CCB, can't register device\n");
2711eba4c79SScott Long 		return (CAM_REQ_CMP_ERR);
2721eba4c79SScott Long 	}
2731eba4c79SScott Long 
2744400b36dSScott Long 	softc = malloc(sizeof(*softc), M_DEVBUF, M_ZERO | M_NOWAIT);
2751eba4c79SScott Long 	if (softc == NULL) {
2761eba4c79SScott Long 		printf("sgregister: Unable to allocate softc\n");
2771eba4c79SScott Long 		return (CAM_REQ_CMP_ERR);
2781eba4c79SScott Long 	}
2791eba4c79SScott Long 
2801eba4c79SScott Long 	softc->state = SG_STATE_NORMAL;
2811eba4c79SScott Long 	softc->pd_type = SID_TYPE(&cgd->inq_data);
282715ab212SScott Long 	softc->sg_timeout = SG_DEFAULT_TIMEOUT / SG_DEFAULT_HZ * hz;
283715ab212SScott Long 	softc->sg_user_timeout = SG_DEFAULT_TIMEOUT;
2841eba4c79SScott Long 	TAILQ_INIT(&softc->rdwr_done);
2851eba4c79SScott Long 	periph->softc = softc;
2861eba4c79SScott Long 
2871eba4c79SScott Long 	/*
2881eba4c79SScott Long 	 * We pass in 0 for all blocksize, since we don't know what the
2891eba4c79SScott Long 	 * blocksize of the device is, if it even has a blocksize.
2901eba4c79SScott Long 	 */
29185d92640SScott Long 	cam_periph_unlock(periph);
2921eba4c79SScott Long 	no_tags = (cgd->inq_data.flags & SID_CmdQue) == 0;
2931eba4c79SScott Long 	softc->device_stats = devstat_new_entry("sg",
294d3ce8327SEd Schouten 			periph->unit_number, 0,
2951eba4c79SScott Long 			DEVSTAT_NO_BLOCKSIZE
2961eba4c79SScott Long 			| (no_tags ? DEVSTAT_NO_ORDERED_TAGS : 0),
2971eba4c79SScott Long 			softc->pd_type |
2981eba4c79SScott Long 			DEVSTAT_TYPE_IF_SCSI |
2991eba4c79SScott Long 			DEVSTAT_TYPE_PASS,
3001eba4c79SScott Long 			DEVSTAT_PRIORITY_PASS);
3011eba4c79SScott Long 
3021eba4c79SScott Long 	/* Register the device */
303d3ce8327SEd Schouten 	softc->dev = make_dev(&sg_cdevsw, periph->unit_number,
3041eba4c79SScott Long 			      UID_ROOT, GID_OPERATOR, 0600, "%s%d",
3051eba4c79SScott Long 			      periph->periph_name, periph->unit_number);
306*cf454e30SMatt Jacob 	if (periph->unit_number < 26) {
307*cf454e30SMatt Jacob 		(void)make_dev_alias(softc->dev, "sg%c", periph->unit_number + 'a');
308*cf454e30SMatt Jacob 	} else {
309*cf454e30SMatt Jacob 		(void)make_dev_alias(softc->dev, "sg%c%c",
310*cf454e30SMatt Jacob 		    ((periph->unit_number / 26) - 1) + 'a', periph->unit_number + 'a');
311*cf454e30SMatt Jacob 	}
3122b83592fSScott Long 	cam_periph_lock(periph);
3131eba4c79SScott Long 	softc->dev->si_drv1 = periph;
3141eba4c79SScott Long 
3151eba4c79SScott Long 	/*
3161eba4c79SScott Long 	 * Add as async callback so that we get
3171eba4c79SScott Long 	 * notified if this device goes away.
3181eba4c79SScott Long 	 */
31985d92640SScott Long 	xpt_register_async(AC_LOST_DEVICE, sgasync, periph, periph->path);
3201eba4c79SScott Long 
3211eba4c79SScott Long 	if (bootverbose)
3221eba4c79SScott Long 		xpt_announce_periph(periph, NULL);
3231eba4c79SScott Long 
3241eba4c79SScott Long 	return (CAM_REQ_CMP);
3251eba4c79SScott Long }
3261eba4c79SScott Long 
3271eba4c79SScott Long static void
3281eba4c79SScott Long sgstart(struct cam_periph *periph, union ccb *start_ccb)
3291eba4c79SScott Long {
3301eba4c79SScott Long 	struct sg_softc *softc;
3311eba4c79SScott Long 
3321eba4c79SScott Long 	softc = (struct sg_softc *)periph->softc;
3331eba4c79SScott Long 
3341eba4c79SScott Long 	switch (softc->state) {
3351eba4c79SScott Long 	case SG_STATE_NORMAL:
3361eba4c79SScott Long 		start_ccb->ccb_h.ccb_type = SG_CCB_WAITING;
3371eba4c79SScott Long 		SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
3381eba4c79SScott Long 				  periph_links.sle);
3391eba4c79SScott Long 		periph->immediate_priority = CAM_PRIORITY_NONE;
3401eba4c79SScott Long 		wakeup(&periph->ccb_list);
3411eba4c79SScott Long 		break;
3421eba4c79SScott Long 	}
3431eba4c79SScott Long }
3441eba4c79SScott Long 
3451eba4c79SScott Long static void
3461eba4c79SScott Long sgdone(struct cam_periph *periph, union ccb *done_ccb)
3471eba4c79SScott Long {
3481eba4c79SScott Long 	struct sg_softc *softc;
3491eba4c79SScott Long 	struct ccb_scsiio *csio;
3501eba4c79SScott Long 
3511eba4c79SScott Long 	softc = (struct sg_softc *)periph->softc;
3521eba4c79SScott Long 	csio = &done_ccb->csio;
3531eba4c79SScott Long 	switch (csio->ccb_h.ccb_type) {
3541eba4c79SScott Long 	case SG_CCB_WAITING:
3551eba4c79SScott Long 		/* Caller will release the CCB */
3561eba4c79SScott Long 		wakeup(&done_ccb->ccb_h.cbfcnp);
3571eba4c79SScott Long 		return;
3581eba4c79SScott Long 	case SG_CCB_RDWR_IO:
3591eba4c79SScott Long 	{
3601eba4c79SScott Long 		struct sg_rdwr *rdwr;
3611eba4c79SScott Long 		int state;
3621eba4c79SScott Long 
3631eba4c79SScott Long 		devstat_end_transaction(softc->device_stats,
3641eba4c79SScott Long 					csio->dxfer_len,
3651eba4c79SScott Long 					csio->tag_action & 0xf,
3661eba4c79SScott Long 					((csio->ccb_h.flags & CAM_DIR_MASK) ==
3671eba4c79SScott Long 					CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
3681eba4c79SScott Long 					(csio->ccb_h.flags & CAM_DIR_OUT) ?
3691eba4c79SScott Long 					DEVSTAT_WRITE : DEVSTAT_READ,
3701eba4c79SScott Long 					NULL, NULL);
3711eba4c79SScott Long 
3721eba4c79SScott Long 		rdwr = done_ccb->ccb_h.ccb_rdwr;
3731eba4c79SScott Long 		state = rdwr->state;
3741eba4c79SScott Long 		rdwr->state = SG_RDWR_DONE;
3751eba4c79SScott Long 		wakeup(rdwr);
3761eba4c79SScott Long 		break;
3771eba4c79SScott Long 	}
3781eba4c79SScott Long 	default:
3791eba4c79SScott Long 		panic("unknown sg CCB type");
3801eba4c79SScott Long 	}
3811eba4c79SScott Long }
3821eba4c79SScott Long 
3831eba4c79SScott Long static int
3841eba4c79SScott Long sgopen(struct cdev *dev, int flags, int fmt, struct thread *td)
3851eba4c79SScott Long {
3861eba4c79SScott Long 	struct cam_periph *periph;
3871eba4c79SScott Long 	struct sg_softc *softc;
3881eba4c79SScott Long 	int error = 0;
3891eba4c79SScott Long 
3901eba4c79SScott Long 	periph = (struct cam_periph *)dev->si_drv1;
3911eba4c79SScott Long 	if (periph == NULL)
3921eba4c79SScott Long 		return (ENXIO);
3931eba4c79SScott Long 
3941eba4c79SScott Long 	/*
3951eba4c79SScott Long 	 * Don't allow access when we're running at a high securelevel.
3961eba4c79SScott Long 	 */
3971eba4c79SScott Long 	error = securelevel_gt(td->td_ucred, 1);
3981eba4c79SScott Long 	if (error)
3991eba4c79SScott Long 		return (error);
4001eba4c79SScott Long 
4012b83592fSScott Long 	cam_periph_lock(periph);
4022b83592fSScott Long 
4032b83592fSScott Long 	softc = (struct sg_softc *)periph->softc;
4042b83592fSScott Long 	if (softc->flags & SG_FLAG_INVALID) {
4052b83592fSScott Long 		cam_periph_unlock(periph);
4062b83592fSScott Long 		return (ENXIO);
4072b83592fSScott Long 	}
4081eba4c79SScott Long 
4091eba4c79SScott Long 	if ((softc->flags & SG_FLAG_OPEN) == 0) {
4101eba4c79SScott Long 		softc->flags |= SG_FLAG_OPEN;
411835187bfSScott Long 		cam_periph_unlock(periph);
4122b83592fSScott Long 	} else {
4132b83592fSScott Long 		/* Device closes aren't symmetrical, fix up the refcount. */
414835187bfSScott Long 		cam_periph_unlock(periph);
4152b83592fSScott Long 		cam_periph_release(periph);
4161eba4c79SScott Long 	}
4171eba4c79SScott Long 
4181eba4c79SScott Long 	return (error);
4191eba4c79SScott Long }
4201eba4c79SScott Long 
4211eba4c79SScott Long static int
4221eba4c79SScott Long sgclose(struct cdev *dev, int flag, int fmt, struct thread *td)
4231eba4c79SScott Long {
4241eba4c79SScott Long 	struct cam_periph *periph;
4251eba4c79SScott Long 	struct sg_softc *softc;
4261eba4c79SScott Long 
4271eba4c79SScott Long 	periph = (struct cam_periph *)dev->si_drv1;
4281eba4c79SScott Long 	if (periph == NULL)
4291eba4c79SScott Long 		return (ENXIO);
4301eba4c79SScott Long 
4312b83592fSScott Long 	cam_periph_lock(periph);
4322b83592fSScott Long 
4331eba4c79SScott Long 	softc = (struct sg_softc *)periph->softc;
4341eba4c79SScott Long 	softc->flags &= ~SG_FLAG_OPEN;
4351eba4c79SScott Long 
4361eba4c79SScott Long 	cam_periph_unlock(periph);
4371eba4c79SScott Long 	cam_periph_release(periph);
4381eba4c79SScott Long 
4391eba4c79SScott Long 	return (0);
4401eba4c79SScott Long }
4411eba4c79SScott Long 
4421eba4c79SScott Long static int
4431eba4c79SScott Long sgioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
4441eba4c79SScott Long {
4451eba4c79SScott Long 	union ccb *ccb;
4461eba4c79SScott Long 	struct ccb_scsiio *csio;
4471eba4c79SScott Long 	struct cam_periph *periph;
4481eba4c79SScott Long 	struct sg_softc *softc;
4491eba4c79SScott Long 	struct sg_io_hdr req;
4501eba4c79SScott Long 	int dir, error;
4511eba4c79SScott Long 
4521eba4c79SScott Long 	periph = (struct cam_periph *)dev->si_drv1;
4531eba4c79SScott Long 	if (periph == NULL)
4541eba4c79SScott Long 		return (ENXIO);
4551eba4c79SScott Long 
4562b83592fSScott Long 	cam_periph_lock(periph);
4572b83592fSScott Long 
4581eba4c79SScott Long 	softc = (struct sg_softc *)periph->softc;
4591eba4c79SScott Long 	error = 0;
4601eba4c79SScott Long 
4611eba4c79SScott Long 	switch (cmd) {
4621eba4c79SScott Long 	case LINUX_SCSI_GET_BUS_NUMBER: {
4631eba4c79SScott Long 		int busno;
4641eba4c79SScott Long 
4651eba4c79SScott Long 		busno = xpt_path_path_id(periph->path);
4661eba4c79SScott Long 		error = copyout(&busno, arg, sizeof(busno));
4671eba4c79SScott Long 		break;
4681eba4c79SScott Long 	}
4691eba4c79SScott Long 	case LINUX_SCSI_GET_IDLUN: {
4701eba4c79SScott Long 		struct scsi_idlun idlun;
4711eba4c79SScott Long 		struct cam_sim *sim;
4721eba4c79SScott Long 
4731eba4c79SScott Long 		idlun.dev_id = xpt_path_target_id(periph->path);
4741eba4c79SScott Long 		sim = xpt_path_sim(periph->path);
4751eba4c79SScott Long 		idlun.host_unique_id = sim->unit_number;
4761eba4c79SScott Long 		error = copyout(&idlun, arg, sizeof(idlun));
4771eba4c79SScott Long 		break;
4781eba4c79SScott Long 	}
4791eba4c79SScott Long 	case SG_GET_VERSION_NUM:
4801eba4c79SScott Long 	case LINUX_SG_GET_VERSION_NUM:
4811eba4c79SScott Long 		error = copyout(&sg_version, arg, sizeof(sg_version));
4821eba4c79SScott Long 		break;
4831eba4c79SScott Long 	case SG_SET_TIMEOUT:
484715ab212SScott Long 	case LINUX_SG_SET_TIMEOUT: {
485715ab212SScott Long 		u_int user_timeout;
486715ab212SScott Long 
487715ab212SScott Long 		error = copyin(arg, &user_timeout, sizeof(u_int));
488715ab212SScott Long 		if (error == 0) {
489715ab212SScott Long 			softc->sg_user_timeout = user_timeout;
490715ab212SScott Long 			softc->sg_timeout = user_timeout / SG_DEFAULT_HZ * hz;
491715ab212SScott Long 		}
4921eba4c79SScott Long 		break;
493715ab212SScott Long 	}
4941eba4c79SScott Long 	case SG_GET_TIMEOUT:
4951eba4c79SScott Long 	case LINUX_SG_GET_TIMEOUT:
4961eba4c79SScott Long 		/*
497715ab212SScott Long 		 * The value is returned directly to the syscall.
4981eba4c79SScott Long 		 */
499715ab212SScott Long 		td->td_retval[0] = softc->sg_user_timeout;
5001eba4c79SScott Long 		error = 0;
5011eba4c79SScott Long 		break;
5021eba4c79SScott Long 	case SG_IO:
5031eba4c79SScott Long 	case LINUX_SG_IO:
5041eba4c79SScott Long 		error = copyin(arg, &req, sizeof(req));
5051eba4c79SScott Long 		if (error)
5061eba4c79SScott Long 			break;
5071eba4c79SScott Long 
5081eba4c79SScott Long 		if (req.cmd_len > IOCDBLEN) {
5091eba4c79SScott Long 			error = EINVAL;
5101eba4c79SScott Long 			break;
5111eba4c79SScott Long 		}
5121eba4c79SScott Long 
5131eba4c79SScott Long 		if (req.iovec_count != 0) {
5141eba4c79SScott Long 			error = EOPNOTSUPP;
5151eba4c79SScott Long 			break;
5161eba4c79SScott Long 		}
5171eba4c79SScott Long 
5181e637ba6SAlexander Motin 		ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
5191eba4c79SScott Long 		csio = &ccb->csio;
5201eba4c79SScott Long 
5211eba4c79SScott Long 		error = copyin(req.cmdp, &csio->cdb_io.cdb_bytes,
5221eba4c79SScott Long 		    req.cmd_len);
5231eba4c79SScott Long 		if (error) {
5241eba4c79SScott Long 			xpt_release_ccb(ccb);
5251eba4c79SScott Long 			break;
5261eba4c79SScott Long 		}
5271eba4c79SScott Long 
5281eba4c79SScott Long 		switch(req.dxfer_direction) {
5291eba4c79SScott Long 		case SG_DXFER_TO_DEV:
5301eba4c79SScott Long 			dir = CAM_DIR_OUT;
5311eba4c79SScott Long 			break;
5321eba4c79SScott Long 		case SG_DXFER_FROM_DEV:
5331eba4c79SScott Long 			dir = CAM_DIR_IN;
5341eba4c79SScott Long 			break;
5351eba4c79SScott Long 		case SG_DXFER_TO_FROM_DEV:
5361eba4c79SScott Long 			dir = CAM_DIR_IN | CAM_DIR_OUT;
5371eba4c79SScott Long 			break;
5381eba4c79SScott Long 		case SG_DXFER_NONE:
5391eba4c79SScott Long 		default:
5401eba4c79SScott Long 			dir = CAM_DIR_NONE;
5411eba4c79SScott Long 			break;
5421eba4c79SScott Long 		}
5431eba4c79SScott Long 
5441eba4c79SScott Long 		cam_fill_csio(csio,
5451eba4c79SScott Long 			      /*retries*/1,
5461eba4c79SScott Long 			      sgdone,
5471eba4c79SScott Long 			      dir|CAM_DEV_QFRZDIS,
5481eba4c79SScott Long 			      MSG_SIMPLE_Q_TAG,
5491eba4c79SScott Long 			      req.dxferp,
5501eba4c79SScott Long 			      req.dxfer_len,
5511eba4c79SScott Long 			      req.mx_sb_len,
5521eba4c79SScott Long 			      req.cmd_len,
5531eba4c79SScott Long 			      req.timeout);
5541eba4c79SScott Long 
5551eba4c79SScott Long 		error = sgsendccb(periph, ccb);
5561eba4c79SScott Long 		if (error) {
5571eba4c79SScott Long 			req.host_status = DID_ERROR;
5581eba4c79SScott Long 			req.driver_status = DRIVER_INVALID;
5591eba4c79SScott Long 			xpt_release_ccb(ccb);
5601eba4c79SScott Long 			break;
5611eba4c79SScott Long 		}
5621eba4c79SScott Long 
5631eba4c79SScott Long 		req.status = csio->scsi_status;
5641eba4c79SScott Long 		req.masked_status = (csio->scsi_status >> 1) & 0x7f;
5651eba4c79SScott Long 		sg_scsiio_status(csio, &req.host_status, &req.driver_status);
5661eba4c79SScott Long 		req.resid = csio->resid;
5671eba4c79SScott Long 		req.duration = csio->ccb_h.timeout;
5681eba4c79SScott Long 		req.info = 0;
5691eba4c79SScott Long 
5701eba4c79SScott Long 		error = copyout(&req, arg, sizeof(req));
5711eba4c79SScott Long 		if ((error == 0) && (csio->ccb_h.status & CAM_AUTOSNS_VALID)
5721eba4c79SScott Long 		    && (req.sbp != NULL)) {
5731eba4c79SScott Long 			req.sb_len_wr = req.mx_sb_len - csio->sense_resid;
5741eba4c79SScott Long 			error = copyout(&csio->sense_data, req.sbp,
5751eba4c79SScott Long 					req.sb_len_wr);
5761eba4c79SScott Long 		}
5771eba4c79SScott Long 
5781eba4c79SScott Long 		xpt_release_ccb(ccb);
5791eba4c79SScott Long 		break;
5801eba4c79SScott Long 
5811eba4c79SScott Long 	case SG_GET_RESERVED_SIZE:
5821eba4c79SScott Long 	case LINUX_SG_GET_RESERVED_SIZE: {
5831eba4c79SScott Long 		int size = 32768;
5841eba4c79SScott Long 
5851eba4c79SScott Long 		error = copyout(&size, arg, sizeof(size));
5861eba4c79SScott Long 		break;
5871eba4c79SScott Long 	}
5881eba4c79SScott Long 
5891eba4c79SScott Long 	case SG_GET_SCSI_ID:
5901eba4c79SScott Long 	case LINUX_SG_GET_SCSI_ID:
5911eba4c79SScott Long 	{
5921eba4c79SScott Long 		struct sg_scsi_id id;
5931eba4c79SScott Long 
59475b06c87SMatt Jacob 		id.host_no = cam_sim_path(xpt_path_sim(periph->path));
5951eba4c79SScott Long 		id.channel = xpt_path_path_id(periph->path);
5961eba4c79SScott Long 		id.scsi_id = xpt_path_target_id(periph->path);
5971eba4c79SScott Long 		id.lun = xpt_path_lun_id(periph->path);
5981eba4c79SScott Long 		id.scsi_type = softc->pd_type;
5991eba4c79SScott Long 		id.h_cmd_per_lun = 1;
6001eba4c79SScott Long 		id.d_queue_depth = 1;
6011eba4c79SScott Long 		id.unused[0] = 0;
6021eba4c79SScott Long 		id.unused[1] = 0;
6031eba4c79SScott Long 
6041eba4c79SScott Long 		error = copyout(&id, arg, sizeof(id));
6051eba4c79SScott Long 		break;
6061eba4c79SScott Long 	}
6071eba4c79SScott Long 
6081eba4c79SScott Long 	case SG_EMULATED_HOST:
6091eba4c79SScott Long 	case SG_SET_TRANSFORM:
6101eba4c79SScott Long 	case SG_GET_TRANSFORM:
6111eba4c79SScott Long 	case SG_GET_NUM_WAITING:
6121eba4c79SScott Long 	case SG_SCSI_RESET:
6131eba4c79SScott Long 	case SG_GET_REQUEST_TABLE:
6141eba4c79SScott Long 	case SG_SET_KEEP_ORPHAN:
6151eba4c79SScott Long 	case SG_GET_KEEP_ORPHAN:
6161eba4c79SScott Long 	case SG_GET_ACCESS_COUNT:
6171eba4c79SScott Long 	case SG_SET_FORCE_LOW_DMA:
6181eba4c79SScott Long 	case SG_GET_LOW_DMA:
6191eba4c79SScott Long 	case SG_GET_SG_TABLESIZE:
6201eba4c79SScott Long 	case SG_SET_FORCE_PACK_ID:
6211eba4c79SScott Long 	case SG_GET_PACK_ID:
6221eba4c79SScott Long 	case SG_SET_RESERVED_SIZE:
6231eba4c79SScott Long 	case SG_GET_COMMAND_Q:
6241eba4c79SScott Long 	case SG_SET_COMMAND_Q:
6251eba4c79SScott Long 	case SG_SET_DEBUG:
6261eba4c79SScott Long 	case SG_NEXT_CMD_LEN:
6271eba4c79SScott Long 	case LINUX_SG_EMULATED_HOST:
6281eba4c79SScott Long 	case LINUX_SG_SET_TRANSFORM:
6291eba4c79SScott Long 	case LINUX_SG_GET_TRANSFORM:
6301eba4c79SScott Long 	case LINUX_SG_GET_NUM_WAITING:
6311eba4c79SScott Long 	case LINUX_SG_SCSI_RESET:
6321eba4c79SScott Long 	case LINUX_SG_GET_REQUEST_TABLE:
6331eba4c79SScott Long 	case LINUX_SG_SET_KEEP_ORPHAN:
6341eba4c79SScott Long 	case LINUX_SG_GET_KEEP_ORPHAN:
6351eba4c79SScott Long 	case LINUX_SG_GET_ACCESS_COUNT:
6361eba4c79SScott Long 	case LINUX_SG_SET_FORCE_LOW_DMA:
6371eba4c79SScott Long 	case LINUX_SG_GET_LOW_DMA:
6381eba4c79SScott Long 	case LINUX_SG_GET_SG_TABLESIZE:
6391eba4c79SScott Long 	case LINUX_SG_SET_FORCE_PACK_ID:
6401eba4c79SScott Long 	case LINUX_SG_GET_PACK_ID:
6411eba4c79SScott Long 	case LINUX_SG_SET_RESERVED_SIZE:
6421eba4c79SScott Long 	case LINUX_SG_GET_COMMAND_Q:
6431eba4c79SScott Long 	case LINUX_SG_SET_COMMAND_Q:
6441eba4c79SScott Long 	case LINUX_SG_SET_DEBUG:
6451eba4c79SScott Long 	case LINUX_SG_NEXT_CMD_LEN:
6461eba4c79SScott Long 	default:
6471eba4c79SScott Long #ifdef CAMDEBUG
6481eba4c79SScott Long 		printf("sgioctl: rejecting cmd 0x%lx\n", cmd);
6491eba4c79SScott Long #endif
6501eba4c79SScott Long 		error = ENODEV;
6511eba4c79SScott Long 		break;
6521eba4c79SScott Long 	}
6531eba4c79SScott Long 
6542b83592fSScott Long 	cam_periph_unlock(periph);
6551eba4c79SScott Long 	return (error);
6561eba4c79SScott Long }
6571eba4c79SScott Long 
6581eba4c79SScott Long static int
6591eba4c79SScott Long sgwrite(struct cdev *dev, struct uio *uio, int ioflag)
6601eba4c79SScott Long {
6611eba4c79SScott Long 	union ccb *ccb;
6621eba4c79SScott Long 	struct cam_periph *periph;
6631eba4c79SScott Long 	struct ccb_scsiio *csio;
6641eba4c79SScott Long 	struct sg_softc *sc;
6651eba4c79SScott Long 	struct sg_header *hdr;
6661eba4c79SScott Long 	struct sg_rdwr *rdwr;
6671eba4c79SScott Long 	u_char cdb_cmd;
6681eba4c79SScott Long 	char *buf;
6691eba4c79SScott Long 	int error = 0, cdb_len, buf_len, dir;
6701eba4c79SScott Long 
6711eba4c79SScott Long 	periph = dev->si_drv1;
6724400b36dSScott Long 	rdwr = malloc(sizeof(*rdwr), M_DEVBUF, M_WAITOK | M_ZERO);
6731eba4c79SScott Long 	hdr = &rdwr->hdr.hdr;
6741eba4c79SScott Long 
6751eba4c79SScott Long 	/* Copy in the header block and sanity check it */
6761eba4c79SScott Long 	if (uio->uio_resid < sizeof(*hdr)) {
6771eba4c79SScott Long 		error = EINVAL;
6781eba4c79SScott Long 		goto out_hdr;
6791eba4c79SScott Long 	}
6801eba4c79SScott Long 	error = uiomove(hdr, sizeof(*hdr), uio);
6811eba4c79SScott Long 	if (error)
6821eba4c79SScott Long 		goto out_hdr;
6831eba4c79SScott Long 
6848008a935SScott Long 	ccb = xpt_alloc_ccb();
6851eba4c79SScott Long 	if (ccb == NULL) {
6861eba4c79SScott Long 		error = ENOMEM;
6871eba4c79SScott Long 		goto out_hdr;
6881eba4c79SScott Long 	}
6891eba4c79SScott Long 	csio = &ccb->csio;
6901eba4c79SScott Long 
6911eba4c79SScott Long 	/*
6921eba4c79SScott Long 	 * Copy in the CDB block.  The designers of the interface didn't
6931eba4c79SScott Long 	 * bother to provide a size for this in the header, so we have to
6941eba4c79SScott Long 	 * figure it out ourselves.
6951eba4c79SScott Long 	 */
6961eba4c79SScott Long 	if (uio->uio_resid < 1)
6971eba4c79SScott Long 		goto out_ccb;
6981eba4c79SScott Long 	error = uiomove(&cdb_cmd, 1, uio);
6991eba4c79SScott Long 	if (error)
7001eba4c79SScott Long 		goto out_ccb;
7011eba4c79SScott Long 	if (hdr->twelve_byte)
7021eba4c79SScott Long 		cdb_len = 12;
7031eba4c79SScott Long 	else
7041eba4c79SScott Long 		cdb_len = scsi_group_len(cdb_cmd);
7051eba4c79SScott Long 	/*
7061eba4c79SScott Long 	 * We've already read the first byte of the CDB and advanced the uio
7071eba4c79SScott Long 	 * pointer.  Just read the rest.
7081eba4c79SScott Long 	 */
7091eba4c79SScott Long 	csio->cdb_io.cdb_bytes[0] = cdb_cmd;
7101eba4c79SScott Long 	error = uiomove(&csio->cdb_io.cdb_bytes[1], cdb_len - 1, uio);
7111eba4c79SScott Long 	if (error)
7121eba4c79SScott Long 		goto out_ccb;
7131eba4c79SScott Long 
7141eba4c79SScott Long 	/*
7151eba4c79SScott Long 	 * Now set up the data block.  Again, the designers didn't bother
7161eba4c79SScott Long 	 * to make this reliable.
7171eba4c79SScott Long 	 */
7181eba4c79SScott Long 	buf_len = uio->uio_resid;
7191eba4c79SScott Long 	if (buf_len != 0) {
7204400b36dSScott Long 		buf = malloc(buf_len, M_DEVBUF, M_WAITOK | M_ZERO);
7211eba4c79SScott Long 		error = uiomove(buf, buf_len, uio);
7221eba4c79SScott Long 		if (error)
7231eba4c79SScott Long 			goto out_buf;
7241eba4c79SScott Long 		dir = CAM_DIR_OUT;
7251eba4c79SScott Long 	} else if (hdr->reply_len != 0) {
7264400b36dSScott Long 		buf = malloc(hdr->reply_len, M_DEVBUF, M_WAITOK | M_ZERO);
7271eba4c79SScott Long 		buf_len = hdr->reply_len;
7281eba4c79SScott Long 		dir = CAM_DIR_IN;
7291eba4c79SScott Long 	} else {
7301eba4c79SScott Long 		buf = NULL;
7311eba4c79SScott Long 		buf_len = 0;
7321eba4c79SScott Long 		dir = CAM_DIR_NONE;
7331eba4c79SScott Long 	}
7341eba4c79SScott Long 
7352b83592fSScott Long 	cam_periph_lock(periph);
7362b83592fSScott Long 	sc = periph->softc;
7371e637ba6SAlexander Motin 	xpt_setup_ccb(&ccb->ccb_h, periph->path, CAM_PRIORITY_NORMAL);
7381eba4c79SScott Long 	cam_fill_csio(csio,
7391eba4c79SScott Long 		      /*retries*/1,
7401eba4c79SScott Long 		      sgdone,
7411eba4c79SScott Long 		      dir|CAM_DEV_QFRZDIS,
7421eba4c79SScott Long 		      MSG_SIMPLE_Q_TAG,
7431eba4c79SScott Long 		      buf,
7441eba4c79SScott Long 		      buf_len,
7451eba4c79SScott Long 		      SG_MAX_SENSE,
7461eba4c79SScott Long 		      cdb_len,
747715ab212SScott Long 		      sc->sg_timeout);
7481eba4c79SScott Long 
7491eba4c79SScott Long 	/*
7501eba4c79SScott Long 	 * Send off the command and hope that it works. This path does not
7511eba4c79SScott Long 	 * go through sgstart because the I/O is supposed to be asynchronous.
7521eba4c79SScott Long 	 */
7531eba4c79SScott Long 	rdwr->buf = buf;
7541eba4c79SScott Long 	rdwr->buf_len = buf_len;
7551eba4c79SScott Long 	rdwr->tag = hdr->pack_id;
7561eba4c79SScott Long 	rdwr->ccb = ccb;
7571eba4c79SScott Long 	rdwr->state = SG_RDWR_INPROG;
7581eba4c79SScott Long 	ccb->ccb_h.ccb_rdwr = rdwr;
7591eba4c79SScott Long 	ccb->ccb_h.ccb_type = SG_CCB_RDWR_IO;
7601eba4c79SScott Long 	TAILQ_INSERT_TAIL(&sc->rdwr_done, rdwr, rdwr_link);
7612b83592fSScott Long 	error = sgsendrdwr(periph, ccb);
7622b83592fSScott Long 	cam_periph_unlock(periph);
7632b83592fSScott Long 	return (error);
7641eba4c79SScott Long 
7651eba4c79SScott Long out_buf:
7661eba4c79SScott Long 	free(buf, M_DEVBUF);
7671eba4c79SScott Long out_ccb:
7681eba4c79SScott Long 	xpt_free_ccb(ccb);
7691eba4c79SScott Long out_hdr:
7701eba4c79SScott Long 	free(rdwr, M_DEVBUF);
7711eba4c79SScott Long 	return (error);
7721eba4c79SScott Long }
7731eba4c79SScott Long 
7741eba4c79SScott Long static int
7751eba4c79SScott Long sgread(struct cdev *dev, struct uio *uio, int ioflag)
7761eba4c79SScott Long {
7771eba4c79SScott Long 	struct ccb_scsiio *csio;
7781eba4c79SScott Long 	struct cam_periph *periph;
7791eba4c79SScott Long 	struct sg_softc *sc;
7801eba4c79SScott Long 	struct sg_header *hdr;
7811eba4c79SScott Long 	struct sg_rdwr *rdwr;
7821eba4c79SScott Long 	u_short hstat, dstat;
7831eba4c79SScott Long 	int error, pack_len, reply_len, pack_id;
7841eba4c79SScott Long 
7851eba4c79SScott Long 	periph = dev->si_drv1;
7861eba4c79SScott Long 
7871eba4c79SScott Long 	/* XXX The pack len field needs to be updated and written out instead
7881eba4c79SScott Long 	 * of discarded.  Not sure how to do that.
7891eba4c79SScott Long 	 */
7901eba4c79SScott Long 	uio->uio_rw = UIO_WRITE;
7911eba4c79SScott Long 	if ((error = uiomove(&pack_len, 4, uio)) != 0)
7921eba4c79SScott Long 		return (error);
7931eba4c79SScott Long 	if ((error = uiomove(&reply_len, 4, uio)) != 0)
7941eba4c79SScott Long 		return (error);
7951eba4c79SScott Long 	if ((error = uiomove(&pack_id, 4, uio)) != 0)
7961eba4c79SScott Long 		return (error);
7971eba4c79SScott Long 	uio->uio_rw = UIO_READ;
7981eba4c79SScott Long 
7992b83592fSScott Long 	cam_periph_lock(periph);
8002b83592fSScott Long 	sc = periph->softc;
8011eba4c79SScott Long search:
8021eba4c79SScott Long 	TAILQ_FOREACH(rdwr, &sc->rdwr_done, rdwr_link) {
8031eba4c79SScott Long 		if (rdwr->tag == pack_id)
8041eba4c79SScott Long 			break;
8051eba4c79SScott Long 	}
8061eba4c79SScott Long 	if ((rdwr == NULL) || (rdwr->state != SG_RDWR_DONE)) {
8072b83592fSScott Long 		if (msleep(rdwr, periph->sim->mtx, PCATCH, "sgread", 0) == ERESTART)
8081eba4c79SScott Long 			return (EAGAIN);
8091eba4c79SScott Long 		goto search;
8101eba4c79SScott Long 	}
8111eba4c79SScott Long 	TAILQ_REMOVE(&sc->rdwr_done, rdwr, rdwr_link);
8122b83592fSScott Long 	cam_periph_unlock(periph);
8131eba4c79SScott Long 
8141eba4c79SScott Long 	hdr = &rdwr->hdr.hdr;
8151eba4c79SScott Long 	csio = &rdwr->ccb->csio;
8161eba4c79SScott Long 	sg_scsiio_status(csio, &hstat, &dstat);
8171eba4c79SScott Long 	hdr->host_status = hstat;
8181eba4c79SScott Long 	hdr->driver_status = dstat;
8191eba4c79SScott Long 	hdr->target_status = csio->scsi_status >> 1;
8201eba4c79SScott Long 
8211eba4c79SScott Long 	switch (hstat) {
8221eba4c79SScott Long 	case DID_OK:
8231eba4c79SScott Long 	case DID_PASSTHROUGH:
8241eba4c79SScott Long 	case DID_SOFT_ERROR:
8251eba4c79SScott Long 		hdr->result = 0;
8261eba4c79SScott Long 		break;
8271eba4c79SScott Long 	case DID_NO_CONNECT:
8281eba4c79SScott Long 	case DID_BUS_BUSY:
8291eba4c79SScott Long 	case DID_TIME_OUT:
8301eba4c79SScott Long 		hdr->result = EBUSY;
8311eba4c79SScott Long 		break;
8321eba4c79SScott Long 	case DID_BAD_TARGET:
8331eba4c79SScott Long 	case DID_ABORT:
8341eba4c79SScott Long 	case DID_PARITY:
8351eba4c79SScott Long 	case DID_RESET:
8361eba4c79SScott Long 	case DID_BAD_INTR:
8371eba4c79SScott Long 	case DID_ERROR:
8381eba4c79SScott Long 	default:
8391eba4c79SScott Long 		hdr->result = EIO;
8401eba4c79SScott Long 		break;
8411eba4c79SScott Long 	}
8421eba4c79SScott Long 
8431eba4c79SScott Long 	if (dstat == DRIVER_SENSE) {
8441eba4c79SScott Long 		bcopy(&csio->sense_data, hdr->sense_buffer,
8451eba4c79SScott Long 		      min(csio->sense_len, SG_MAX_SENSE));
8461eba4c79SScott Long #ifdef CAMDEBUG
8471eba4c79SScott Long 		scsi_sense_print(csio);
8481eba4c79SScott Long #endif
8491eba4c79SScott Long 	}
8501eba4c79SScott Long 
8511eba4c79SScott Long 	error = uiomove(&hdr->result, sizeof(*hdr) -
8521eba4c79SScott Long 			offsetof(struct sg_header, result), uio);
8531eba4c79SScott Long 	if ((error == 0) && (hdr->result == 0))
8541eba4c79SScott Long 		error = uiomove(rdwr->buf, rdwr->buf_len, uio);
8551eba4c79SScott Long 
8562b83592fSScott Long 	cam_periph_lock(periph);
8571eba4c79SScott Long 	xpt_free_ccb(rdwr->ccb);
8582b83592fSScott Long 	cam_periph_unlock(periph);
8591eba4c79SScott Long 	free(rdwr->buf, M_DEVBUF);
8601eba4c79SScott Long 	free(rdwr, M_DEVBUF);
8611eba4c79SScott Long 	return (error);
8621eba4c79SScott Long }
8631eba4c79SScott Long 
8641eba4c79SScott Long static int
8651eba4c79SScott Long sgsendccb(struct cam_periph *periph, union ccb *ccb)
8661eba4c79SScott Long {
8671eba4c79SScott Long 	struct sg_softc *softc;
8681eba4c79SScott Long 	struct cam_periph_map_info mapinfo;
8691eba4c79SScott Long 	int error, need_unmap = 0;
8701eba4c79SScott Long 
8711eba4c79SScott Long 	softc = periph->softc;
8721eba4c79SScott Long 	if (((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE)
8731eba4c79SScott Long 	    && (ccb->csio.data_ptr != NULL)) {
8741eba4c79SScott Long 		bzero(&mapinfo, sizeof(mapinfo));
8752b83592fSScott Long 
8762b83592fSScott Long 		/*
8772b83592fSScott Long 		 * cam_periph_mapmem calls into proc and vm functions that can
8782b83592fSScott Long 		 * sleep as well as trigger I/O, so we can't hold the lock.
8792b83592fSScott Long 		 * Dropping it here is reasonably safe.
8802b83592fSScott Long 		 */
8812b83592fSScott Long 		cam_periph_unlock(periph);
8821eba4c79SScott Long 		error = cam_periph_mapmem(ccb, &mapinfo);
8832b83592fSScott Long 		cam_periph_lock(periph);
8841eba4c79SScott Long 		if (error)
8851eba4c79SScott Long 			return (error);
8861eba4c79SScott Long 		need_unmap = 1;
8871eba4c79SScott Long 	}
8881eba4c79SScott Long 
8891eba4c79SScott Long 	error = cam_periph_runccb(ccb,
8901eba4c79SScott Long 				  sgerror,
8911eba4c79SScott Long 				  CAM_RETRY_SELTO,
8921eba4c79SScott Long 				  SF_RETRY_UA,
8931eba4c79SScott Long 				  softc->device_stats);
8941eba4c79SScott Long 
8951eba4c79SScott Long 	if (need_unmap)
8961eba4c79SScott Long 		cam_periph_unmapmem(ccb, &mapinfo);
8971eba4c79SScott Long 
8981eba4c79SScott Long 	return (error);
8991eba4c79SScott Long }
9001eba4c79SScott Long 
9011eba4c79SScott Long static int
9021eba4c79SScott Long sgsendrdwr(struct cam_periph *periph, union ccb *ccb)
9031eba4c79SScott Long {
9041eba4c79SScott Long 	struct sg_softc *softc;
9051eba4c79SScott Long 
9061eba4c79SScott Long 	softc = periph->softc;
9071eba4c79SScott Long 	devstat_start_transaction(softc->device_stats, NULL);
9081eba4c79SScott Long 	xpt_action(ccb);
9091eba4c79SScott Long 	return (0);
9101eba4c79SScott Long }
9111eba4c79SScott Long 
9121eba4c79SScott Long static int
9131eba4c79SScott Long sgerror(union ccb *ccb, uint32_t cam_flags, uint32_t sense_flags)
9141eba4c79SScott Long {
9151eba4c79SScott Long 	struct cam_periph *periph;
9161eba4c79SScott Long 	struct sg_softc *softc;
9171eba4c79SScott Long 
9181eba4c79SScott Long 	periph = xpt_path_periph(ccb->ccb_h.path);
9191eba4c79SScott Long 	softc = (struct sg_softc *)periph->softc;
9201eba4c79SScott Long 
9211eba4c79SScott Long 	return (cam_periph_error(ccb, cam_flags, sense_flags,
9221eba4c79SScott Long 				 &softc->saved_ccb));
9231eba4c79SScott Long }
9241eba4c79SScott Long 
9251eba4c79SScott Long static void
9261eba4c79SScott Long sg_scsiio_status(struct ccb_scsiio *csio, u_short *hoststat, u_short *drvstat)
9271eba4c79SScott Long {
9281eba4c79SScott Long 	int status;
9291eba4c79SScott Long 
9301eba4c79SScott Long 	status = csio->ccb_h.status;
9311eba4c79SScott Long 
9321eba4c79SScott Long 	switch (status & CAM_STATUS_MASK) {
9331eba4c79SScott Long 	case CAM_REQ_CMP:
9341eba4c79SScott Long 		*hoststat = DID_OK;
9351eba4c79SScott Long 		*drvstat = 0;
9361eba4c79SScott Long 		break;
9371eba4c79SScott Long 	case CAM_REQ_CMP_ERR:
9381eba4c79SScott Long 		*hoststat = DID_ERROR;
9391eba4c79SScott Long 		*drvstat = 0;
9401eba4c79SScott Long 		break;
9411eba4c79SScott Long 	case CAM_REQ_ABORTED:
9421eba4c79SScott Long 		*hoststat = DID_ABORT;
9431eba4c79SScott Long 		*drvstat = 0;
9441eba4c79SScott Long 		break;
9451eba4c79SScott Long 	case CAM_REQ_INVALID:
9461eba4c79SScott Long 		*hoststat = DID_ERROR;
9471eba4c79SScott Long 		*drvstat = DRIVER_INVALID;
9481eba4c79SScott Long 		break;
9491eba4c79SScott Long 	case CAM_DEV_NOT_THERE:
9501eba4c79SScott Long 		*hoststat = DID_BAD_TARGET;
9511eba4c79SScott Long 		*drvstat = 0;
9524fee613eSEdward Tomasz Napierala 		break;
9531eba4c79SScott Long 	case CAM_SEL_TIMEOUT:
9541eba4c79SScott Long 		*hoststat = DID_NO_CONNECT;
9551eba4c79SScott Long 		*drvstat = 0;
9561eba4c79SScott Long 		break;
9571eba4c79SScott Long 	case CAM_CMD_TIMEOUT:
9581eba4c79SScott Long 		*hoststat = DID_TIME_OUT;
9591eba4c79SScott Long 		*drvstat = 0;
9601eba4c79SScott Long 		break;
9611eba4c79SScott Long 	case CAM_SCSI_STATUS_ERROR:
9621eba4c79SScott Long 		*hoststat = DID_ERROR;
9631eba4c79SScott Long 		*drvstat = 0;
9640c70e307SEdward Tomasz Napierala 		break;
9651eba4c79SScott Long 	case CAM_SCSI_BUS_RESET:
9661eba4c79SScott Long 		*hoststat = DID_RESET;
9671eba4c79SScott Long 		*drvstat = 0;
9681eba4c79SScott Long 		break;
9691eba4c79SScott Long 	case CAM_UNCOR_PARITY:
9701eba4c79SScott Long 		*hoststat = DID_PARITY;
9711eba4c79SScott Long 		*drvstat = 0;
9721eba4c79SScott Long 		break;
9731eba4c79SScott Long 	case CAM_SCSI_BUSY:
9741eba4c79SScott Long 		*hoststat = DID_BUS_BUSY;
9751eba4c79SScott Long 		*drvstat = 0;
9760c70e307SEdward Tomasz Napierala 		break;
9771eba4c79SScott Long 	default:
9781eba4c79SScott Long 		*hoststat = DID_ERROR;
9791eba4c79SScott Long 		*drvstat = DRIVER_ERROR;
9801eba4c79SScott Long 	}
9811eba4c79SScott Long 
9821eba4c79SScott Long 	if (status & CAM_AUTOSNS_VALID)
9831eba4c79SScott Long 		*drvstat = DRIVER_SENSE;
9841eba4c79SScott Long }
9851eba4c79SScott Long 
9861eba4c79SScott Long static int
9871eba4c79SScott Long scsi_group_len(u_char cmd)
9881eba4c79SScott Long {
9891eba4c79SScott Long 	int len[] = {6, 10, 10, 12, 12, 12, 10, 10};
9901eba4c79SScott Long 	int group;
9911eba4c79SScott Long 
9921eba4c79SScott Long 	group = (cmd >> 5) & 0x7;
9931eba4c79SScott Long 	return (len[group]);
9941eba4c79SScott Long }
9951eba4c79SScott Long 
996