xref: /freebsd/sys/cam/scsi/scsi_sg.c (revision ee1988938c413b3173b5d537f5d8f2021c7fbd24)
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 {
64c552ebe1SKenneth D. Merry 	SG_FLAG_LOCKED		= 0x01,
65c552ebe1SKenneth D. Merry 	SG_FLAG_INVALID		= 0x02
661eba4c79SScott Long } sg_flags;
671eba4c79SScott Long 
681eba4c79SScott Long typedef enum {
691eba4c79SScott Long 	SG_STATE_NORMAL
701eba4c79SScott Long } sg_state;
711eba4c79SScott Long 
721eba4c79SScott Long typedef enum {
73472cdbefSScott Long 	SG_RDWR_FREE,
741eba4c79SScott Long 	SG_RDWR_INPROG,
751eba4c79SScott Long 	SG_RDWR_DONE
761eba4c79SScott Long } sg_rdwr_state;
771eba4c79SScott Long 
781eba4c79SScott Long typedef enum {
79227d67aaSAlexander Motin 	SG_CCB_RDWR_IO
801eba4c79SScott Long } sg_ccb_types;
811eba4c79SScott Long 
821eba4c79SScott Long #define ccb_type	ppriv_field0
831eba4c79SScott Long #define ccb_rdwr	ppriv_ptr1
841eba4c79SScott Long 
851eba4c79SScott Long struct sg_rdwr {
861eba4c79SScott Long 	TAILQ_ENTRY(sg_rdwr)	rdwr_link;
871eba4c79SScott Long 	int			tag;
881eba4c79SScott Long 	int			state;
891eba4c79SScott Long 	int			buf_len;
901eba4c79SScott Long 	char			*buf;
911eba4c79SScott Long 	union ccb		*ccb;
921eba4c79SScott Long 	union {
931eba4c79SScott Long 		struct sg_header hdr;
941eba4c79SScott Long 		struct sg_io_hdr io_hdr;
951eba4c79SScott Long 	} hdr;
961eba4c79SScott Long };
971eba4c79SScott Long 
981eba4c79SScott Long struct sg_softc {
991eba4c79SScott Long 	sg_state		state;
1001eba4c79SScott Long 	sg_flags		flags;
10186d45c7fSKenneth D. Merry 	int			open_count;
102de239312SAlexander Motin 	u_int			maxio;
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 void		sgasync(void *callback_arg, uint32_t code,
1231eba4c79SScott Long 				struct cam_path *path, void *arg);
1241eba4c79SScott Long static void		sgdone(struct cam_periph *periph, union ccb *done_ccb);
1251eba4c79SScott Long static int		sgsendccb(struct cam_periph *periph, union ccb *ccb);
1261eba4c79SScott Long static int		sgsendrdwr(struct cam_periph *periph, union ccb *ccb);
1271eba4c79SScott Long static int		sgerror(union ccb *ccb, uint32_t cam_flags,
1281eba4c79SScott Long 				uint32_t sense_flags);
1291eba4c79SScott Long static void		sg_scsiio_status(struct ccb_scsiio *csio,
1301eba4c79SScott Long 					 u_short *hoststat, u_short *drvstat);
1311eba4c79SScott Long 
1321eba4c79SScott Long static int		scsi_group_len(u_char cmd);
1331eba4c79SScott Long 
1341eba4c79SScott Long static struct periph_driver sgdriver =
1351eba4c79SScott Long {
1361eba4c79SScott Long 	sginit, "sg",
1371eba4c79SScott Long 	TAILQ_HEAD_INITIALIZER(sgdriver.units), /* gen */ 0
1381eba4c79SScott Long };
1391eba4c79SScott Long PERIPHDRIVER_DECLARE(sg, sgdriver);
1401eba4c79SScott Long 
1411eba4c79SScott Long static struct cdevsw sg_cdevsw = {
1421eba4c79SScott Long 	.d_version =	D_VERSION,
143c552ebe1SKenneth D. Merry 	.d_flags =	D_NEEDGIANT | D_TRACKCLOSE,
1441eba4c79SScott Long 	.d_open =	sgopen,
1451eba4c79SScott Long 	.d_close =	sgclose,
1461eba4c79SScott Long 	.d_ioctl =	sgioctl,
1471eba4c79SScott Long 	.d_write =	sgwrite,
1481eba4c79SScott Long 	.d_read =	sgread,
1491eba4c79SScott Long 	.d_name =	"sg",
1501eba4c79SScott Long };
1511eba4c79SScott Long 
1521eba4c79SScott Long static int sg_version = 30125;
1531eba4c79SScott Long 
1541eba4c79SScott Long static void
1551eba4c79SScott Long sginit(void)
1561eba4c79SScott Long {
1571eba4c79SScott Long 	cam_status status;
1581eba4c79SScott Long 
1591eba4c79SScott Long 	/*
1601eba4c79SScott Long 	 * Install a global async callback.  This callback will receive aync
1611eba4c79SScott Long 	 * callbacks like "new device found".
1621eba4c79SScott Long 	 */
16385d92640SScott Long 	status = xpt_register_async(AC_FOUND_DEVICE, sgasync, NULL, NULL);
1641eba4c79SScott Long 
1651eba4c79SScott Long 	if (status != CAM_REQ_CMP) {
1661eba4c79SScott Long 		printf("sg: Failed to attach master async callbac "
1671eba4c79SScott Long 			"due to status 0x%x!\n", status);
1681eba4c79SScott Long 	}
1691eba4c79SScott Long }
1701eba4c79SScott Long 
1711eba4c79SScott Long static void
17286d45c7fSKenneth D. Merry sgdevgonecb(void *arg)
17386d45c7fSKenneth D. Merry {
17486d45c7fSKenneth D. Merry 	struct cam_periph *periph;
17586d45c7fSKenneth D. Merry 	struct sg_softc *softc;
176227d67aaSAlexander Motin 	struct mtx *mtx;
17786d45c7fSKenneth D. Merry 	int i;
17886d45c7fSKenneth D. Merry 
17986d45c7fSKenneth D. Merry 	periph = (struct cam_periph *)arg;
180227d67aaSAlexander Motin 	mtx = cam_periph_mtx(periph);
181227d67aaSAlexander Motin 	mtx_lock(mtx);
18286d45c7fSKenneth D. Merry 
183227d67aaSAlexander Motin 	softc = (struct sg_softc *)periph->softc;
18486d45c7fSKenneth D. Merry 	KASSERT(softc->open_count >= 0, ("Negative open count %d",
18586d45c7fSKenneth D. Merry 		softc->open_count));
18686d45c7fSKenneth D. Merry 
18786d45c7fSKenneth D. Merry 	/*
18886d45c7fSKenneth D. Merry 	 * When we get this callback, we will get no more close calls from
18986d45c7fSKenneth D. Merry 	 * devfs.  So if we have any dangling opens, we need to release the
19086d45c7fSKenneth D. Merry 	 * reference held for that particular context.
19186d45c7fSKenneth D. Merry 	 */
19286d45c7fSKenneth D. Merry 	for (i = 0; i < softc->open_count; i++)
19386d45c7fSKenneth D. Merry 		cam_periph_release_locked(periph);
19486d45c7fSKenneth D. Merry 
19586d45c7fSKenneth D. Merry 	softc->open_count = 0;
19686d45c7fSKenneth D. Merry 
19786d45c7fSKenneth D. Merry 	/*
19886d45c7fSKenneth D. Merry 	 * Release the reference held for the device node, it is gone now.
19986d45c7fSKenneth D. Merry 	 */
20086d45c7fSKenneth D. Merry 	cam_periph_release_locked(periph);
20186d45c7fSKenneth D. Merry 
20286d45c7fSKenneth D. Merry 	/*
203227d67aaSAlexander Motin 	 * We reference the lock directly here, instead of using
20486d45c7fSKenneth D. Merry 	 * cam_periph_unlock().  The reason is that the final call to
20586d45c7fSKenneth D. Merry 	 * cam_periph_release_locked() above could result in the periph
20686d45c7fSKenneth D. Merry 	 * getting freed.  If that is the case, dereferencing the periph
20786d45c7fSKenneth D. Merry 	 * with a cam_periph_unlock() call would cause a page fault.
20886d45c7fSKenneth D. Merry 	 */
209227d67aaSAlexander Motin 	mtx_unlock(mtx);
21086d45c7fSKenneth D. Merry }
21186d45c7fSKenneth D. Merry 
21286d45c7fSKenneth D. Merry 
21386d45c7fSKenneth D. Merry static void
2141eba4c79SScott Long sgoninvalidate(struct cam_periph *periph)
2151eba4c79SScott Long {
2161eba4c79SScott Long 	struct sg_softc *softc;
2171eba4c79SScott Long 
2181eba4c79SScott Long 	softc = (struct sg_softc *)periph->softc;
2191eba4c79SScott Long 
2201eba4c79SScott Long 	/*
2211eba4c79SScott Long 	 * Deregister any async callbacks.
2221eba4c79SScott Long 	 */
22385d92640SScott Long 	xpt_register_async(0, sgasync, periph, periph->path);
2241eba4c79SScott Long 
2251eba4c79SScott Long 	softc->flags |= SG_FLAG_INVALID;
2261eba4c79SScott Long 
2271eba4c79SScott Long 	/*
22886d45c7fSKenneth D. Merry 	 * Tell devfs this device has gone away, and ask for a callback
22986d45c7fSKenneth D. Merry 	 * when it has cleaned up its state.
23086d45c7fSKenneth D. Merry 	 */
23186d45c7fSKenneth D. Merry 	destroy_dev_sched_cb(softc->dev, sgdevgonecb, periph);
23286d45c7fSKenneth D. Merry 
23386d45c7fSKenneth D. Merry 	/*
2341eba4c79SScott Long 	 * XXX Return all queued I/O with ENXIO.
2351eba4c79SScott Long 	 * XXX Handle any transactions queued to the card
2361eba4c79SScott Long 	 *     with XPT_ABORT_CCB.
2371eba4c79SScott Long 	 */
2381eba4c79SScott Long 
2391eba4c79SScott Long }
2401eba4c79SScott Long 
2411eba4c79SScott Long static void
2421eba4c79SScott Long sgcleanup(struct cam_periph *periph)
2431eba4c79SScott Long {
2441eba4c79SScott Long 	struct sg_softc *softc;
2451eba4c79SScott Long 
2461eba4c79SScott Long 	softc = (struct sg_softc *)periph->softc;
24786d45c7fSKenneth D. Merry 
2485f3fed85SEdward Tomasz Napierala 	devstat_remove_entry(softc->device_stats);
24986d45c7fSKenneth D. Merry 
2501eba4c79SScott Long 	free(softc, M_DEVBUF);
2511eba4c79SScott Long }
2521eba4c79SScott Long 
2531eba4c79SScott Long static void
2541eba4c79SScott Long sgasync(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
2551eba4c79SScott Long {
2561eba4c79SScott Long 	struct cam_periph *periph;
2571eba4c79SScott Long 
2581eba4c79SScott Long 	periph = (struct cam_periph *)callback_arg;
2591eba4c79SScott Long 
2601eba4c79SScott Long 	switch (code) {
2611eba4c79SScott Long 	case AC_FOUND_DEVICE:
2621eba4c79SScott Long 	{
2631eba4c79SScott Long 		struct ccb_getdev *cgd;
2641eba4c79SScott Long 		cam_status status;
2651eba4c79SScott Long 
2661eba4c79SScott Long 		cgd = (struct ccb_getdev *)arg;
2671eba4c79SScott Long 		if (cgd == NULL)
2681eba4c79SScott Long 			break;
2691eba4c79SScott Long 
27052c9ce25SScott Long 		if (cgd->protocol != PROTO_SCSI)
27152c9ce25SScott Long 			break;
27252c9ce25SScott Long 
2731eba4c79SScott Long 		/*
2741eba4c79SScott Long 		 * Allocate a peripheral instance for this device and
2751eba4c79SScott Long 		 * start the probe process.
2761eba4c79SScott Long 		 */
2771eba4c79SScott Long 		status = cam_periph_alloc(sgregister, sgoninvalidate,
278227d67aaSAlexander Motin 					  sgcleanup, NULL, "sg",
279227d67aaSAlexander Motin 					  CAM_PERIPH_BIO, path,
2801eba4c79SScott Long 					  sgasync, AC_FOUND_DEVICE, cgd);
2811eba4c79SScott Long 		if ((status != CAM_REQ_CMP) && (status != CAM_REQ_INPROG)) {
2821eba4c79SScott Long 			const struct cam_status_entry *entry;
2831eba4c79SScott Long 
2841eba4c79SScott Long 			entry = cam_fetch_status_entry(status);
2851eba4c79SScott Long 			printf("sgasync: Unable to attach new device "
2861eba4c79SScott Long 				"due to status %#x: %s\n", status, entry ?
2871eba4c79SScott Long 				entry->status_text : "Unknown");
2881eba4c79SScott Long 		}
2891eba4c79SScott Long 		break;
2901eba4c79SScott Long 	}
2911eba4c79SScott Long 	default:
2921eba4c79SScott Long 		cam_periph_async(periph, code, path, arg);
2931eba4c79SScott Long 		break;
2941eba4c79SScott Long 	}
2951eba4c79SScott Long }
2961eba4c79SScott Long 
2971eba4c79SScott Long static cam_status
2981eba4c79SScott Long sgregister(struct cam_periph *periph, void *arg)
2991eba4c79SScott Long {
3001eba4c79SScott Long 	struct sg_softc *softc;
3011eba4c79SScott Long 	struct ccb_getdev *cgd;
302b8b6b5d3SAlexander Motin 	struct ccb_pathinq cpi;
303*ee198893SKonstantin Belousov 	struct make_dev_args args;
304*ee198893SKonstantin Belousov 	int no_tags, error;
3051eba4c79SScott Long 
3061eba4c79SScott Long 	cgd = (struct ccb_getdev *)arg;
3071eba4c79SScott Long 	if (cgd == NULL) {
3081eba4c79SScott Long 		printf("sgregister: no getdev CCB, can't register device\n");
3091eba4c79SScott Long 		return (CAM_REQ_CMP_ERR);
3101eba4c79SScott Long 	}
3111eba4c79SScott Long 
3124400b36dSScott Long 	softc = malloc(sizeof(*softc), M_DEVBUF, M_ZERO | M_NOWAIT);
3131eba4c79SScott Long 	if (softc == NULL) {
3141eba4c79SScott Long 		printf("sgregister: Unable to allocate softc\n");
3151eba4c79SScott Long 		return (CAM_REQ_CMP_ERR);
3161eba4c79SScott Long 	}
3171eba4c79SScott Long 
3181eba4c79SScott Long 	softc->state = SG_STATE_NORMAL;
3191eba4c79SScott Long 	softc->pd_type = SID_TYPE(&cgd->inq_data);
320715ab212SScott Long 	softc->sg_timeout = SG_DEFAULT_TIMEOUT / SG_DEFAULT_HZ * hz;
321715ab212SScott Long 	softc->sg_user_timeout = SG_DEFAULT_TIMEOUT;
3221eba4c79SScott Long 	TAILQ_INIT(&softc->rdwr_done);
3231eba4c79SScott Long 	periph->softc = softc;
3241eba4c79SScott Long 
325b8b6b5d3SAlexander Motin 	bzero(&cpi, sizeof(cpi));
326b8b6b5d3SAlexander Motin 	xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
327b8b6b5d3SAlexander Motin 	cpi.ccb_h.func_code = XPT_PATH_INQ;
328b8b6b5d3SAlexander Motin 	xpt_action((union ccb *)&cpi);
329b8b6b5d3SAlexander Motin 
330de239312SAlexander Motin 	if (cpi.maxio == 0)
331de239312SAlexander Motin 		softc->maxio = DFLTPHYS;	/* traditional default */
332de239312SAlexander Motin 	else if (cpi.maxio > MAXPHYS)
333de239312SAlexander Motin 		softc->maxio = MAXPHYS;		/* for safety */
334de239312SAlexander Motin 	else
335de239312SAlexander Motin 		softc->maxio = cpi.maxio;	/* real value */
336de239312SAlexander Motin 
3371eba4c79SScott Long 	/*
3381eba4c79SScott Long 	 * We pass in 0 for all blocksize, since we don't know what the
3391eba4c79SScott Long 	 * blocksize of the device is, if it even has a blocksize.
3401eba4c79SScott Long 	 */
34185d92640SScott Long 	cam_periph_unlock(periph);
3421eba4c79SScott Long 	no_tags = (cgd->inq_data.flags & SID_CmdQue) == 0;
3431eba4c79SScott Long 	softc->device_stats = devstat_new_entry("sg",
344d3ce8327SEd Schouten 			periph->unit_number, 0,
3451eba4c79SScott Long 			DEVSTAT_NO_BLOCKSIZE
3461eba4c79SScott Long 			| (no_tags ? DEVSTAT_NO_ORDERED_TAGS : 0),
3471eba4c79SScott Long 			softc->pd_type |
348b8b6b5d3SAlexander Motin 			XPORT_DEVSTAT_TYPE(cpi.transport) |
3491eba4c79SScott Long 			DEVSTAT_TYPE_PASS,
3501eba4c79SScott Long 			DEVSTAT_PRIORITY_PASS);
3511eba4c79SScott Long 
35286d45c7fSKenneth D. Merry 	/*
35386d45c7fSKenneth D. Merry 	 * Acquire a reference to the periph before we create the devfs
35486d45c7fSKenneth D. Merry 	 * instance for it.  We'll release this reference once the devfs
35586d45c7fSKenneth D. Merry 	 * instance has been freed.
35686d45c7fSKenneth D. Merry 	 */
35786d45c7fSKenneth D. Merry 	if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
35886d45c7fSKenneth D. Merry 		xpt_print(periph->path, "%s: lost periph during "
35986d45c7fSKenneth D. Merry 			  "registration!\n", __func__);
36086d45c7fSKenneth D. Merry 		cam_periph_lock(periph);
36186d45c7fSKenneth D. Merry 		return (CAM_REQ_CMP_ERR);
36286d45c7fSKenneth D. Merry 	}
36386d45c7fSKenneth D. Merry 
3641eba4c79SScott Long 	/* Register the device */
365*ee198893SKonstantin Belousov 	make_dev_args_init(&args);
366*ee198893SKonstantin Belousov 	args.mda_devsw = &sg_cdevsw;
367*ee198893SKonstantin Belousov 	args.mda_unit = periph->unit_number;
368*ee198893SKonstantin Belousov 	args.mda_uid = UID_ROOT;
369*ee198893SKonstantin Belousov 	args.mda_gid = GID_OPERATOR;
370*ee198893SKonstantin Belousov 	args.mda_mode = 0600;
371*ee198893SKonstantin Belousov 	args.mda_si_drv1 = periph;
372*ee198893SKonstantin Belousov 	error = make_dev_s(&args, &softc->dev, "%s%d",
3731eba4c79SScott Long 	    periph->periph_name, periph->unit_number);
374*ee198893SKonstantin Belousov 	if (error != 0) {
375*ee198893SKonstantin Belousov 		cam_periph_lock(periph);
376*ee198893SKonstantin Belousov 		cam_periph_release_locked(periph);
377*ee198893SKonstantin Belousov 		return (CAM_REQ_CMP_ERR);
378*ee198893SKonstantin Belousov 	}
379cf454e30SMatt Jacob 	if (periph->unit_number < 26) {
380c59b4dcdSMatt Jacob 		(void)make_dev_alias(softc->dev, "sg%c",
381c59b4dcdSMatt Jacob 		    periph->unit_number + 'a');
382cf454e30SMatt Jacob 	} else {
383cf454e30SMatt Jacob 		(void)make_dev_alias(softc->dev, "sg%c%c",
384c59b4dcdSMatt Jacob 		    ((periph->unit_number / 26) - 1) + 'a',
385c59b4dcdSMatt Jacob 		    (periph->unit_number % 26) + 'a');
386cf454e30SMatt Jacob 	}
3872b83592fSScott Long 	cam_periph_lock(periph);
3881eba4c79SScott Long 
3891eba4c79SScott Long 	/*
3901eba4c79SScott Long 	 * Add as async callback so that we get
3911eba4c79SScott Long 	 * notified if this device goes away.
3921eba4c79SScott Long 	 */
39385d92640SScott Long 	xpt_register_async(AC_LOST_DEVICE, sgasync, periph, periph->path);
3941eba4c79SScott Long 
3951eba4c79SScott Long 	if (bootverbose)
3961eba4c79SScott Long 		xpt_announce_periph(periph, NULL);
3971eba4c79SScott Long 
3981eba4c79SScott Long 	return (CAM_REQ_CMP);
3991eba4c79SScott Long }
4001eba4c79SScott Long 
4011eba4c79SScott Long static void
4021eba4c79SScott Long sgdone(struct cam_periph *periph, union ccb *done_ccb)
4031eba4c79SScott Long {
4041eba4c79SScott Long 	struct sg_softc *softc;
4051eba4c79SScott Long 	struct ccb_scsiio *csio;
4061eba4c79SScott Long 
4071eba4c79SScott Long 	softc = (struct sg_softc *)periph->softc;
4081eba4c79SScott Long 	csio = &done_ccb->csio;
4091eba4c79SScott Long 	switch (csio->ccb_h.ccb_type) {
4101eba4c79SScott Long 	case SG_CCB_RDWR_IO:
4111eba4c79SScott Long 	{
4121eba4c79SScott Long 		struct sg_rdwr *rdwr;
4131eba4c79SScott Long 		int state;
4141eba4c79SScott Long 
4151eba4c79SScott Long 		devstat_end_transaction(softc->device_stats,
4161eba4c79SScott Long 					csio->dxfer_len,
4171eba4c79SScott Long 					csio->tag_action & 0xf,
4181eba4c79SScott Long 					((csio->ccb_h.flags & CAM_DIR_MASK) ==
4191eba4c79SScott Long 					CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
4201eba4c79SScott Long 					(csio->ccb_h.flags & CAM_DIR_OUT) ?
4211eba4c79SScott Long 					DEVSTAT_WRITE : DEVSTAT_READ,
4221eba4c79SScott Long 					NULL, NULL);
4231eba4c79SScott Long 
4241eba4c79SScott Long 		rdwr = done_ccb->ccb_h.ccb_rdwr;
4251eba4c79SScott Long 		state = rdwr->state;
4261eba4c79SScott Long 		rdwr->state = SG_RDWR_DONE;
4271eba4c79SScott Long 		wakeup(rdwr);
4281eba4c79SScott Long 		break;
4291eba4c79SScott Long 	}
4301eba4c79SScott Long 	default:
4311eba4c79SScott Long 		panic("unknown sg CCB type");
4321eba4c79SScott Long 	}
4331eba4c79SScott Long }
4341eba4c79SScott Long 
4351eba4c79SScott Long static int
4361eba4c79SScott Long sgopen(struct cdev *dev, int flags, int fmt, struct thread *td)
4371eba4c79SScott Long {
4381eba4c79SScott Long 	struct cam_periph *periph;
4391eba4c79SScott Long 	struct sg_softc *softc;
4401eba4c79SScott Long 	int error = 0;
4411eba4c79SScott Long 
4421eba4c79SScott Long 	periph = (struct cam_periph *)dev->si_drv1;
4438900f4b8SKenneth D. Merry 	if (cam_periph_acquire(periph) != CAM_REQ_CMP)
4448900f4b8SKenneth D. Merry 		return (ENXIO);
4458900f4b8SKenneth D. Merry 
4461eba4c79SScott Long 	/*
4471eba4c79SScott Long 	 * Don't allow access when we're running at a high securelevel.
4481eba4c79SScott Long 	 */
4491eba4c79SScott Long 	error = securelevel_gt(td->td_ucred, 1);
4508900f4b8SKenneth D. Merry 	if (error) {
4518900f4b8SKenneth D. Merry 		cam_periph_release(periph);
4521eba4c79SScott Long 		return (error);
4538900f4b8SKenneth D. Merry 	}
4541eba4c79SScott Long 
4552b83592fSScott Long 	cam_periph_lock(periph);
4562b83592fSScott Long 
4572b83592fSScott Long 	softc = (struct sg_softc *)periph->softc;
4582b83592fSScott Long 	if (softc->flags & SG_FLAG_INVALID) {
459c552ebe1SKenneth D. Merry 		cam_periph_release_locked(periph);
4602b83592fSScott Long 		cam_periph_unlock(periph);
4612b83592fSScott Long 		return (ENXIO);
4622b83592fSScott Long 	}
4631eba4c79SScott Long 
46486d45c7fSKenneth D. Merry 	softc->open_count++;
46586d45c7fSKenneth D. Merry 
466835187bfSScott Long 	cam_periph_unlock(periph);
4671eba4c79SScott Long 
4681eba4c79SScott Long 	return (error);
4691eba4c79SScott Long }
4701eba4c79SScott Long 
4711eba4c79SScott Long static int
4721eba4c79SScott Long sgclose(struct cdev *dev, int flag, int fmt, struct thread *td)
4731eba4c79SScott Long {
4741eba4c79SScott Long 	struct cam_periph *periph;
47586d45c7fSKenneth D. Merry 	struct sg_softc   *softc;
476227d67aaSAlexander Motin 	struct mtx *mtx;
4771eba4c79SScott Long 
4781eba4c79SScott Long 	periph = (struct cam_periph *)dev->si_drv1;
479227d67aaSAlexander Motin 	mtx = cam_periph_mtx(periph);
480227d67aaSAlexander Motin 	mtx_lock(mtx);
4811eba4c79SScott Long 
48286d45c7fSKenneth D. Merry 	softc = periph->softc;
48386d45c7fSKenneth D. Merry 	softc->open_count--;
48486d45c7fSKenneth D. Merry 
48586d45c7fSKenneth D. Merry 	cam_periph_release_locked(periph);
48686d45c7fSKenneth D. Merry 
48786d45c7fSKenneth D. Merry 	/*
488227d67aaSAlexander Motin 	 * We reference the lock directly here, instead of using
48986d45c7fSKenneth D. Merry 	 * cam_periph_unlock().  The reason is that the call to
49086d45c7fSKenneth D. Merry 	 * cam_periph_release_locked() above could result in the periph
49186d45c7fSKenneth D. Merry 	 * getting freed.  If that is the case, dereferencing the periph
49286d45c7fSKenneth D. Merry 	 * with a cam_periph_unlock() call would cause a page fault.
49386d45c7fSKenneth D. Merry 	 *
49486d45c7fSKenneth D. Merry 	 * cam_periph_release() avoids this problem using the same method,
49586d45c7fSKenneth D. Merry 	 * but we're manually acquiring and dropping the lock here to
49686d45c7fSKenneth D. Merry 	 * protect the open count and avoid another lock acquisition and
49786d45c7fSKenneth D. Merry 	 * release.
49886d45c7fSKenneth D. Merry 	 */
499227d67aaSAlexander Motin 	mtx_unlock(mtx);
5001eba4c79SScott Long 
5011eba4c79SScott Long 	return (0);
5021eba4c79SScott Long }
5031eba4c79SScott Long 
5041eba4c79SScott Long static int
5051eba4c79SScott Long sgioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
5061eba4c79SScott Long {
5071eba4c79SScott Long 	union ccb *ccb;
5081eba4c79SScott Long 	struct ccb_scsiio *csio;
5091eba4c79SScott Long 	struct cam_periph *periph;
5101eba4c79SScott Long 	struct sg_softc *softc;
511fcaf473cSAlexander Motin 	struct sg_io_hdr *req;
5121eba4c79SScott Long 	int dir, error;
5131eba4c79SScott Long 
5141eba4c79SScott Long 	periph = (struct cam_periph *)dev->si_drv1;
5152b83592fSScott Long 	cam_periph_lock(periph);
5162b83592fSScott Long 
5171eba4c79SScott Long 	softc = (struct sg_softc *)periph->softc;
5181eba4c79SScott Long 	error = 0;
5191eba4c79SScott Long 
5201eba4c79SScott Long 	switch (cmd) {
5211eba4c79SScott Long 	case SG_GET_VERSION_NUM:
522fcaf473cSAlexander Motin 	{
523fcaf473cSAlexander Motin 		int *version = (int *)arg;
524715ab212SScott Long 
525fcaf473cSAlexander Motin 		*version = sg_version;
526fcaf473cSAlexander Motin 		break;
527fcaf473cSAlexander Motin 	}
528fcaf473cSAlexander Motin 	case SG_SET_TIMEOUT:
529fcaf473cSAlexander Motin 	{
530fcaf473cSAlexander Motin 		u_int user_timeout = *(u_int *)arg;
531fcaf473cSAlexander Motin 
532715ab212SScott Long 		softc->sg_user_timeout = user_timeout;
533715ab212SScott Long 		softc->sg_timeout = user_timeout / SG_DEFAULT_HZ * hz;
5341eba4c79SScott Long 		break;
535715ab212SScott Long 	}
5361eba4c79SScott Long 	case SG_GET_TIMEOUT:
5371eba4c79SScott Long 		/*
538715ab212SScott Long 		 * The value is returned directly to the syscall.
5391eba4c79SScott Long 		 */
540715ab212SScott Long 		td->td_retval[0] = softc->sg_user_timeout;
5411eba4c79SScott Long 		error = 0;
5421eba4c79SScott Long 		break;
5431eba4c79SScott Long 	case SG_IO:
544fcaf473cSAlexander Motin 		req = (struct sg_io_hdr *)arg;
5451eba4c79SScott Long 
546fcaf473cSAlexander Motin 		if (req->cmd_len > IOCDBLEN) {
5471eba4c79SScott Long 			error = EINVAL;
5481eba4c79SScott Long 			break;
5491eba4c79SScott Long 		}
5501eba4c79SScott Long 
551fcaf473cSAlexander Motin 		if (req->iovec_count != 0) {
5521eba4c79SScott Long 			error = EOPNOTSUPP;
5531eba4c79SScott Long 			break;
5541eba4c79SScott Long 		}
5551eba4c79SScott Long 
5561e637ba6SAlexander Motin 		ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
5571eba4c79SScott Long 		csio = &ccb->csio;
5581eba4c79SScott Long 
559fcaf473cSAlexander Motin 		error = copyin(req->cmdp, &csio->cdb_io.cdb_bytes,
560fcaf473cSAlexander Motin 		    req->cmd_len);
5611eba4c79SScott Long 		if (error) {
5621eba4c79SScott Long 			xpt_release_ccb(ccb);
5631eba4c79SScott Long 			break;
5641eba4c79SScott Long 		}
5651eba4c79SScott Long 
566fcaf473cSAlexander Motin 		switch(req->dxfer_direction) {
5671eba4c79SScott Long 		case SG_DXFER_TO_DEV:
5681eba4c79SScott Long 			dir = CAM_DIR_OUT;
5691eba4c79SScott Long 			break;
5701eba4c79SScott Long 		case SG_DXFER_FROM_DEV:
5711eba4c79SScott Long 			dir = CAM_DIR_IN;
5721eba4c79SScott Long 			break;
5731eba4c79SScott Long 		case SG_DXFER_TO_FROM_DEV:
5741eba4c79SScott Long 			dir = CAM_DIR_IN | CAM_DIR_OUT;
5751eba4c79SScott Long 			break;
5761eba4c79SScott Long 		case SG_DXFER_NONE:
5771eba4c79SScott Long 		default:
5781eba4c79SScott Long 			dir = CAM_DIR_NONE;
5791eba4c79SScott Long 			break;
5801eba4c79SScott Long 		}
5811eba4c79SScott Long 
5821eba4c79SScott Long 		cam_fill_csio(csio,
5831eba4c79SScott Long 			      /*retries*/1,
5841eba4c79SScott Long 			      sgdone,
5851eba4c79SScott Long 			      dir|CAM_DEV_QFRZDIS,
5861eba4c79SScott Long 			      MSG_SIMPLE_Q_TAG,
587fcaf473cSAlexander Motin 			      req->dxferp,
588fcaf473cSAlexander Motin 			      req->dxfer_len,
589fcaf473cSAlexander Motin 			      req->mx_sb_len,
590fcaf473cSAlexander Motin 			      req->cmd_len,
591fcaf473cSAlexander Motin 			      req->timeout);
5921eba4c79SScott Long 
5931eba4c79SScott Long 		error = sgsendccb(periph, ccb);
5941eba4c79SScott Long 		if (error) {
595fcaf473cSAlexander Motin 			req->host_status = DID_ERROR;
596fcaf473cSAlexander Motin 			req->driver_status = DRIVER_INVALID;
5971eba4c79SScott Long 			xpt_release_ccb(ccb);
5981eba4c79SScott Long 			break;
5991eba4c79SScott Long 		}
6001eba4c79SScott Long 
601fcaf473cSAlexander Motin 		req->status = csio->scsi_status;
602fcaf473cSAlexander Motin 		req->masked_status = (csio->scsi_status >> 1) & 0x7f;
603fcaf473cSAlexander Motin 		sg_scsiio_status(csio, &req->host_status, &req->driver_status);
604fcaf473cSAlexander Motin 		req->resid = csio->resid;
605fcaf473cSAlexander Motin 		req->duration = csio->ccb_h.timeout;
606fcaf473cSAlexander Motin 		req->info = 0;
6071eba4c79SScott Long 
608fcaf473cSAlexander Motin 		if ((csio->ccb_h.status & CAM_AUTOSNS_VALID)
609fcaf473cSAlexander Motin 		    && (req->sbp != NULL)) {
610fcaf473cSAlexander Motin 			req->sb_len_wr = req->mx_sb_len - csio->sense_resid;
611fcaf473cSAlexander Motin 			error = copyout(&csio->sense_data, req->sbp,
612fcaf473cSAlexander Motin 					req->sb_len_wr);
6131eba4c79SScott Long 		}
6141eba4c79SScott Long 
6151eba4c79SScott Long 		xpt_release_ccb(ccb);
6161eba4c79SScott Long 		break;
6171eba4c79SScott Long 
6181eba4c79SScott Long 	case SG_GET_RESERVED_SIZE:
619fcaf473cSAlexander Motin 	{
620fcaf473cSAlexander Motin 		int *size = (int *)arg;
621fcaf473cSAlexander Motin 		*size = DFLTPHYS;
6221eba4c79SScott Long 		break;
6231eba4c79SScott Long 	}
6241eba4c79SScott Long 
6251eba4c79SScott Long 	case SG_GET_SCSI_ID:
6261eba4c79SScott Long 	{
627fcaf473cSAlexander Motin 		struct sg_scsi_id *id = (struct sg_scsi_id *)arg;
6281eba4c79SScott Long 
629fcaf473cSAlexander Motin 		id->host_no = cam_sim_path(xpt_path_sim(periph->path));
630fcaf473cSAlexander Motin 		id->channel = xpt_path_path_id(periph->path);
631fcaf473cSAlexander Motin 		id->scsi_id = xpt_path_target_id(periph->path);
632fcaf473cSAlexander Motin 		id->lun = xpt_path_lun_id(periph->path);
633fcaf473cSAlexander Motin 		id->scsi_type = softc->pd_type;
634fcaf473cSAlexander Motin 		id->h_cmd_per_lun = 1;
635fcaf473cSAlexander Motin 		id->d_queue_depth = 1;
636fcaf473cSAlexander Motin 		id->unused[0] = 0;
637fcaf473cSAlexander Motin 		id->unused[1] = 0;
6381eba4c79SScott Long 		break;
6391eba4c79SScott Long 	}
6401eba4c79SScott Long 
64194fe9f95SAlexander Motin 	case SG_GET_SG_TABLESIZE:
64294fe9f95SAlexander Motin 	{
64394fe9f95SAlexander Motin 		int *size = (int *)arg;
64494fe9f95SAlexander Motin 		*size = 0;
64594fe9f95SAlexander Motin 		break;
64694fe9f95SAlexander Motin 	}
64794fe9f95SAlexander Motin 
6481eba4c79SScott Long 	case SG_EMULATED_HOST:
6491eba4c79SScott Long 	case SG_SET_TRANSFORM:
6501eba4c79SScott Long 	case SG_GET_TRANSFORM:
6511eba4c79SScott Long 	case SG_GET_NUM_WAITING:
6521eba4c79SScott Long 	case SG_SCSI_RESET:
6531eba4c79SScott Long 	case SG_GET_REQUEST_TABLE:
6541eba4c79SScott Long 	case SG_SET_KEEP_ORPHAN:
6551eba4c79SScott Long 	case SG_GET_KEEP_ORPHAN:
6561eba4c79SScott Long 	case SG_GET_ACCESS_COUNT:
6571eba4c79SScott Long 	case SG_SET_FORCE_LOW_DMA:
6581eba4c79SScott Long 	case SG_GET_LOW_DMA:
6591eba4c79SScott Long 	case SG_SET_FORCE_PACK_ID:
6601eba4c79SScott Long 	case SG_GET_PACK_ID:
6611eba4c79SScott Long 	case SG_SET_RESERVED_SIZE:
6621eba4c79SScott Long 	case SG_GET_COMMAND_Q:
6631eba4c79SScott Long 	case SG_SET_COMMAND_Q:
6641eba4c79SScott Long 	case SG_SET_DEBUG:
6651eba4c79SScott Long 	case SG_NEXT_CMD_LEN:
6661eba4c79SScott Long 	default:
6671eba4c79SScott Long #ifdef CAMDEBUG
6681eba4c79SScott Long 		printf("sgioctl: rejecting cmd 0x%lx\n", cmd);
6691eba4c79SScott Long #endif
6701eba4c79SScott Long 		error = ENODEV;
6711eba4c79SScott Long 		break;
6721eba4c79SScott Long 	}
6731eba4c79SScott Long 
6742b83592fSScott Long 	cam_periph_unlock(periph);
6751eba4c79SScott Long 	return (error);
6761eba4c79SScott Long }
6771eba4c79SScott Long 
6781eba4c79SScott Long static int
6791eba4c79SScott Long sgwrite(struct cdev *dev, struct uio *uio, int ioflag)
6801eba4c79SScott Long {
6811eba4c79SScott Long 	union ccb *ccb;
6821eba4c79SScott Long 	struct cam_periph *periph;
6831eba4c79SScott Long 	struct ccb_scsiio *csio;
6841eba4c79SScott Long 	struct sg_softc *sc;
6851eba4c79SScott Long 	struct sg_header *hdr;
6861eba4c79SScott Long 	struct sg_rdwr *rdwr;
6871eba4c79SScott Long 	u_char cdb_cmd;
6881eba4c79SScott Long 	char *buf;
6891eba4c79SScott Long 	int error = 0, cdb_len, buf_len, dir;
6901eba4c79SScott Long 
6911eba4c79SScott Long 	periph = dev->si_drv1;
6924400b36dSScott Long 	rdwr = malloc(sizeof(*rdwr), M_DEVBUF, M_WAITOK | M_ZERO);
6931eba4c79SScott Long 	hdr = &rdwr->hdr.hdr;
6941eba4c79SScott Long 
6951eba4c79SScott Long 	/* Copy in the header block and sanity check it */
6961eba4c79SScott Long 	if (uio->uio_resid < sizeof(*hdr)) {
6971eba4c79SScott Long 		error = EINVAL;
6981eba4c79SScott Long 		goto out_hdr;
6991eba4c79SScott Long 	}
7001eba4c79SScott Long 	error = uiomove(hdr, sizeof(*hdr), uio);
7011eba4c79SScott Long 	if (error)
7021eba4c79SScott Long 		goto out_hdr;
7031eba4c79SScott Long 
70494fe9f95SAlexander Motin 	/* XXX: We don't support SG 3.x read/write API. */
70594fe9f95SAlexander Motin 	if (hdr->reply_len < 0) {
70694fe9f95SAlexander Motin 		error = ENODEV;
70794fe9f95SAlexander Motin 		goto out_hdr;
70894fe9f95SAlexander Motin 	}
70994fe9f95SAlexander Motin 
7108008a935SScott Long 	ccb = xpt_alloc_ccb();
7111eba4c79SScott Long 	if (ccb == NULL) {
7121eba4c79SScott Long 		error = ENOMEM;
7131eba4c79SScott Long 		goto out_hdr;
7141eba4c79SScott Long 	}
7151eba4c79SScott Long 	csio = &ccb->csio;
7161eba4c79SScott Long 
7171eba4c79SScott Long 	/*
7181eba4c79SScott Long 	 * Copy in the CDB block.  The designers of the interface didn't
7191eba4c79SScott Long 	 * bother to provide a size for this in the header, so we have to
7201eba4c79SScott Long 	 * figure it out ourselves.
7211eba4c79SScott Long 	 */
7221eba4c79SScott Long 	if (uio->uio_resid < 1)
7231eba4c79SScott Long 		goto out_ccb;
7241eba4c79SScott Long 	error = uiomove(&cdb_cmd, 1, uio);
7251eba4c79SScott Long 	if (error)
7261eba4c79SScott Long 		goto out_ccb;
7271eba4c79SScott Long 	if (hdr->twelve_byte)
7281eba4c79SScott Long 		cdb_len = 12;
7291eba4c79SScott Long 	else
7301eba4c79SScott Long 		cdb_len = scsi_group_len(cdb_cmd);
7311eba4c79SScott Long 	/*
7321eba4c79SScott Long 	 * We've already read the first byte of the CDB and advanced the uio
7331eba4c79SScott Long 	 * pointer.  Just read the rest.
7341eba4c79SScott Long 	 */
7351eba4c79SScott Long 	csio->cdb_io.cdb_bytes[0] = cdb_cmd;
7361eba4c79SScott Long 	error = uiomove(&csio->cdb_io.cdb_bytes[1], cdb_len - 1, uio);
7371eba4c79SScott Long 	if (error)
7381eba4c79SScott Long 		goto out_ccb;
7391eba4c79SScott Long 
7401eba4c79SScott Long 	/*
7411eba4c79SScott Long 	 * Now set up the data block.  Again, the designers didn't bother
7421eba4c79SScott Long 	 * to make this reliable.
7431eba4c79SScott Long 	 */
7441eba4c79SScott Long 	buf_len = uio->uio_resid;
7451eba4c79SScott Long 	if (buf_len != 0) {
7464400b36dSScott Long 		buf = malloc(buf_len, M_DEVBUF, M_WAITOK | M_ZERO);
7471eba4c79SScott Long 		error = uiomove(buf, buf_len, uio);
7481eba4c79SScott Long 		if (error)
7491eba4c79SScott Long 			goto out_buf;
7501eba4c79SScott Long 		dir = CAM_DIR_OUT;
7511eba4c79SScott Long 	} else if (hdr->reply_len != 0) {
7524400b36dSScott Long 		buf = malloc(hdr->reply_len, M_DEVBUF, M_WAITOK | M_ZERO);
7531eba4c79SScott Long 		buf_len = hdr->reply_len;
7541eba4c79SScott Long 		dir = CAM_DIR_IN;
7551eba4c79SScott Long 	} else {
7561eba4c79SScott Long 		buf = NULL;
7571eba4c79SScott Long 		buf_len = 0;
7581eba4c79SScott Long 		dir = CAM_DIR_NONE;
7591eba4c79SScott Long 	}
7601eba4c79SScott Long 
7612b83592fSScott Long 	cam_periph_lock(periph);
7622b83592fSScott Long 	sc = periph->softc;
7631e637ba6SAlexander Motin 	xpt_setup_ccb(&ccb->ccb_h, periph->path, CAM_PRIORITY_NORMAL);
7641eba4c79SScott Long 	cam_fill_csio(csio,
7651eba4c79SScott Long 		      /*retries*/1,
7661eba4c79SScott Long 		      sgdone,
7671eba4c79SScott Long 		      dir|CAM_DEV_QFRZDIS,
7681eba4c79SScott Long 		      MSG_SIMPLE_Q_TAG,
7691eba4c79SScott Long 		      buf,
7701eba4c79SScott Long 		      buf_len,
7711eba4c79SScott Long 		      SG_MAX_SENSE,
7721eba4c79SScott Long 		      cdb_len,
773715ab212SScott Long 		      sc->sg_timeout);
7741eba4c79SScott Long 
7751eba4c79SScott Long 	/*
7761eba4c79SScott Long 	 * Send off the command and hope that it works. This path does not
7771eba4c79SScott Long 	 * go through sgstart because the I/O is supposed to be asynchronous.
7781eba4c79SScott Long 	 */
7791eba4c79SScott Long 	rdwr->buf = buf;
7801eba4c79SScott Long 	rdwr->buf_len = buf_len;
7811eba4c79SScott Long 	rdwr->tag = hdr->pack_id;
7821eba4c79SScott Long 	rdwr->ccb = ccb;
7831eba4c79SScott Long 	rdwr->state = SG_RDWR_INPROG;
7841eba4c79SScott Long 	ccb->ccb_h.ccb_rdwr = rdwr;
7851eba4c79SScott Long 	ccb->ccb_h.ccb_type = SG_CCB_RDWR_IO;
7861eba4c79SScott Long 	TAILQ_INSERT_TAIL(&sc->rdwr_done, rdwr, rdwr_link);
7872b83592fSScott Long 	error = sgsendrdwr(periph, ccb);
7882b83592fSScott Long 	cam_periph_unlock(periph);
7892b83592fSScott Long 	return (error);
7901eba4c79SScott Long 
7911eba4c79SScott Long out_buf:
7921eba4c79SScott Long 	free(buf, M_DEVBUF);
7931eba4c79SScott Long out_ccb:
7941eba4c79SScott Long 	xpt_free_ccb(ccb);
7951eba4c79SScott Long out_hdr:
7961eba4c79SScott Long 	free(rdwr, M_DEVBUF);
7971eba4c79SScott Long 	return (error);
7981eba4c79SScott Long }
7991eba4c79SScott Long 
8001eba4c79SScott Long static int
8011eba4c79SScott Long sgread(struct cdev *dev, struct uio *uio, int ioflag)
8021eba4c79SScott Long {
8031eba4c79SScott Long 	struct ccb_scsiio *csio;
8041eba4c79SScott Long 	struct cam_periph *periph;
8051eba4c79SScott Long 	struct sg_softc *sc;
8061eba4c79SScott Long 	struct sg_header *hdr;
8071eba4c79SScott Long 	struct sg_rdwr *rdwr;
8081eba4c79SScott Long 	u_short hstat, dstat;
8091eba4c79SScott Long 	int error, pack_len, reply_len, pack_id;
8101eba4c79SScott Long 
8111eba4c79SScott Long 	periph = dev->si_drv1;
8121eba4c79SScott Long 
8131eba4c79SScott Long 	/* XXX The pack len field needs to be updated and written out instead
8141eba4c79SScott Long 	 * of discarded.  Not sure how to do that.
8151eba4c79SScott Long 	 */
8161eba4c79SScott Long 	uio->uio_rw = UIO_WRITE;
8171eba4c79SScott Long 	if ((error = uiomove(&pack_len, 4, uio)) != 0)
8181eba4c79SScott Long 		return (error);
8191eba4c79SScott Long 	if ((error = uiomove(&reply_len, 4, uio)) != 0)
8201eba4c79SScott Long 		return (error);
8211eba4c79SScott Long 	if ((error = uiomove(&pack_id, 4, uio)) != 0)
8221eba4c79SScott Long 		return (error);
8231eba4c79SScott Long 	uio->uio_rw = UIO_READ;
8241eba4c79SScott Long 
8252b83592fSScott Long 	cam_periph_lock(periph);
8262b83592fSScott Long 	sc = periph->softc;
8271eba4c79SScott Long search:
8281eba4c79SScott Long 	TAILQ_FOREACH(rdwr, &sc->rdwr_done, rdwr_link) {
8291eba4c79SScott Long 		if (rdwr->tag == pack_id)
8301eba4c79SScott Long 			break;
8311eba4c79SScott Long 	}
8321eba4c79SScott Long 	if ((rdwr == NULL) || (rdwr->state != SG_RDWR_DONE)) {
833227d67aaSAlexander Motin 		if (cam_periph_sleep(periph, rdwr, PCATCH, "sgread", 0) == ERESTART)
8341eba4c79SScott Long 			return (EAGAIN);
8351eba4c79SScott Long 		goto search;
8361eba4c79SScott Long 	}
8371eba4c79SScott Long 	TAILQ_REMOVE(&sc->rdwr_done, rdwr, rdwr_link);
8382b83592fSScott Long 	cam_periph_unlock(periph);
8391eba4c79SScott Long 
8401eba4c79SScott Long 	hdr = &rdwr->hdr.hdr;
8411eba4c79SScott Long 	csio = &rdwr->ccb->csio;
8421eba4c79SScott Long 	sg_scsiio_status(csio, &hstat, &dstat);
8431eba4c79SScott Long 	hdr->host_status = hstat;
8441eba4c79SScott Long 	hdr->driver_status = dstat;
8451eba4c79SScott Long 	hdr->target_status = csio->scsi_status >> 1;
8461eba4c79SScott Long 
8471eba4c79SScott Long 	switch (hstat) {
8481eba4c79SScott Long 	case DID_OK:
8491eba4c79SScott Long 	case DID_PASSTHROUGH:
8501eba4c79SScott Long 	case DID_SOFT_ERROR:
8511eba4c79SScott Long 		hdr->result = 0;
8521eba4c79SScott Long 		break;
8531eba4c79SScott Long 	case DID_NO_CONNECT:
8541eba4c79SScott Long 	case DID_BUS_BUSY:
8551eba4c79SScott Long 	case DID_TIME_OUT:
8561eba4c79SScott Long 		hdr->result = EBUSY;
8571eba4c79SScott Long 		break;
8581eba4c79SScott Long 	case DID_BAD_TARGET:
8591eba4c79SScott Long 	case DID_ABORT:
8601eba4c79SScott Long 	case DID_PARITY:
8611eba4c79SScott Long 	case DID_RESET:
8621eba4c79SScott Long 	case DID_BAD_INTR:
8631eba4c79SScott Long 	case DID_ERROR:
8641eba4c79SScott Long 	default:
8651eba4c79SScott Long 		hdr->result = EIO;
8661eba4c79SScott Long 		break;
8671eba4c79SScott Long 	}
8681eba4c79SScott Long 
8691eba4c79SScott Long 	if (dstat == DRIVER_SENSE) {
8701eba4c79SScott Long 		bcopy(&csio->sense_data, hdr->sense_buffer,
8711eba4c79SScott Long 		      min(csio->sense_len, SG_MAX_SENSE));
8721eba4c79SScott Long #ifdef CAMDEBUG
8731eba4c79SScott Long 		scsi_sense_print(csio);
8741eba4c79SScott Long #endif
8751eba4c79SScott Long 	}
8761eba4c79SScott Long 
8771eba4c79SScott Long 	error = uiomove(&hdr->result, sizeof(*hdr) -
8781eba4c79SScott Long 			offsetof(struct sg_header, result), uio);
8791eba4c79SScott Long 	if ((error == 0) && (hdr->result == 0))
8801eba4c79SScott Long 		error = uiomove(rdwr->buf, rdwr->buf_len, uio);
8811eba4c79SScott Long 
8822b83592fSScott Long 	cam_periph_lock(periph);
8831eba4c79SScott Long 	xpt_free_ccb(rdwr->ccb);
8842b83592fSScott Long 	cam_periph_unlock(periph);
8851eba4c79SScott Long 	free(rdwr->buf, M_DEVBUF);
8861eba4c79SScott Long 	free(rdwr, M_DEVBUF);
8871eba4c79SScott Long 	return (error);
8881eba4c79SScott Long }
8891eba4c79SScott Long 
8901eba4c79SScott Long static int
8911eba4c79SScott Long sgsendccb(struct cam_periph *periph, union ccb *ccb)
8921eba4c79SScott Long {
8931eba4c79SScott Long 	struct sg_softc *softc;
8941eba4c79SScott Long 	struct cam_periph_map_info mapinfo;
89595fbded6SScott Long 	int error;
8961eba4c79SScott Long 
8971eba4c79SScott Long 	softc = periph->softc;
8981eba4c79SScott Long 	bzero(&mapinfo, sizeof(mapinfo));
8992b83592fSScott Long 
9002b83592fSScott Long 	/*
9012b83592fSScott Long 	 * cam_periph_mapmem calls into proc and vm functions that can
9022b83592fSScott Long 	 * sleep as well as trigger I/O, so we can't hold the lock.
9032b83592fSScott Long 	 * Dropping it here is reasonably safe.
90495fbded6SScott Long 	 * The only CCB opcode that is possible here is XPT_SCSI_IO, no
90595fbded6SScott Long 	 * need for additional checks.
9062b83592fSScott Long 	 */
9072b83592fSScott Long 	cam_periph_unlock(periph);
908de239312SAlexander Motin 	error = cam_periph_mapmem(ccb, &mapinfo, softc->maxio);
9092b83592fSScott Long 	cam_periph_lock(periph);
9101eba4c79SScott Long 	if (error)
9111eba4c79SScott Long 		return (error);
9121eba4c79SScott Long 
9131eba4c79SScott Long 	error = cam_periph_runccb(ccb,
9141eba4c79SScott Long 				  sgerror,
9151eba4c79SScott Long 				  CAM_RETRY_SELTO,
9161eba4c79SScott Long 				  SF_RETRY_UA,
9171eba4c79SScott Long 				  softc->device_stats);
9181eba4c79SScott Long 
9191eba4c79SScott Long 	cam_periph_unmapmem(ccb, &mapinfo);
9201eba4c79SScott Long 
9211eba4c79SScott Long 	return (error);
9221eba4c79SScott Long }
9231eba4c79SScott Long 
9241eba4c79SScott Long static int
9251eba4c79SScott Long sgsendrdwr(struct cam_periph *periph, union ccb *ccb)
9261eba4c79SScott Long {
9271eba4c79SScott Long 	struct sg_softc *softc;
9281eba4c79SScott Long 
9291eba4c79SScott Long 	softc = periph->softc;
9301eba4c79SScott Long 	devstat_start_transaction(softc->device_stats, NULL);
9311eba4c79SScott Long 	xpt_action(ccb);
9321eba4c79SScott Long 	return (0);
9331eba4c79SScott Long }
9341eba4c79SScott Long 
9351eba4c79SScott Long static int
9361eba4c79SScott Long sgerror(union ccb *ccb, uint32_t cam_flags, uint32_t sense_flags)
9371eba4c79SScott Long {
9381eba4c79SScott Long 	struct cam_periph *periph;
9391eba4c79SScott Long 	struct sg_softc *softc;
9401eba4c79SScott Long 
9411eba4c79SScott Long 	periph = xpt_path_periph(ccb->ccb_h.path);
9421eba4c79SScott Long 	softc = (struct sg_softc *)periph->softc;
9431eba4c79SScott Long 
9441eba4c79SScott Long 	return (cam_periph_error(ccb, cam_flags, sense_flags,
9451eba4c79SScott Long 				 &softc->saved_ccb));
9461eba4c79SScott Long }
9471eba4c79SScott Long 
9481eba4c79SScott Long static void
9491eba4c79SScott Long sg_scsiio_status(struct ccb_scsiio *csio, u_short *hoststat, u_short *drvstat)
9501eba4c79SScott Long {
9511eba4c79SScott Long 	int status;
9521eba4c79SScott Long 
9531eba4c79SScott Long 	status = csio->ccb_h.status;
9541eba4c79SScott Long 
9551eba4c79SScott Long 	switch (status & CAM_STATUS_MASK) {
9561eba4c79SScott Long 	case CAM_REQ_CMP:
9571eba4c79SScott Long 		*hoststat = DID_OK;
9581eba4c79SScott Long 		*drvstat = 0;
9591eba4c79SScott Long 		break;
9601eba4c79SScott Long 	case CAM_REQ_CMP_ERR:
9611eba4c79SScott Long 		*hoststat = DID_ERROR;
9621eba4c79SScott Long 		*drvstat = 0;
9631eba4c79SScott Long 		break;
9641eba4c79SScott Long 	case CAM_REQ_ABORTED:
9651eba4c79SScott Long 		*hoststat = DID_ABORT;
9661eba4c79SScott Long 		*drvstat = 0;
9671eba4c79SScott Long 		break;
9681eba4c79SScott Long 	case CAM_REQ_INVALID:
9691eba4c79SScott Long 		*hoststat = DID_ERROR;
9701eba4c79SScott Long 		*drvstat = DRIVER_INVALID;
9711eba4c79SScott Long 		break;
9721eba4c79SScott Long 	case CAM_DEV_NOT_THERE:
9731eba4c79SScott Long 		*hoststat = DID_BAD_TARGET;
9741eba4c79SScott Long 		*drvstat = 0;
9754fee613eSEdward Tomasz Napierala 		break;
9761eba4c79SScott Long 	case CAM_SEL_TIMEOUT:
9771eba4c79SScott Long 		*hoststat = DID_NO_CONNECT;
9781eba4c79SScott Long 		*drvstat = 0;
9791eba4c79SScott Long 		break;
9801eba4c79SScott Long 	case CAM_CMD_TIMEOUT:
9811eba4c79SScott Long 		*hoststat = DID_TIME_OUT;
9821eba4c79SScott Long 		*drvstat = 0;
9831eba4c79SScott Long 		break;
9841eba4c79SScott Long 	case CAM_SCSI_STATUS_ERROR:
9851eba4c79SScott Long 		*hoststat = DID_ERROR;
9861eba4c79SScott Long 		*drvstat = 0;
9870c70e307SEdward Tomasz Napierala 		break;
9881eba4c79SScott Long 	case CAM_SCSI_BUS_RESET:
9891eba4c79SScott Long 		*hoststat = DID_RESET;
9901eba4c79SScott Long 		*drvstat = 0;
9911eba4c79SScott Long 		break;
9921eba4c79SScott Long 	case CAM_UNCOR_PARITY:
9931eba4c79SScott Long 		*hoststat = DID_PARITY;
9941eba4c79SScott Long 		*drvstat = 0;
9951eba4c79SScott Long 		break;
9961eba4c79SScott Long 	case CAM_SCSI_BUSY:
9971eba4c79SScott Long 		*hoststat = DID_BUS_BUSY;
9981eba4c79SScott Long 		*drvstat = 0;
9990c70e307SEdward Tomasz Napierala 		break;
10001eba4c79SScott Long 	default:
10011eba4c79SScott Long 		*hoststat = DID_ERROR;
10021eba4c79SScott Long 		*drvstat = DRIVER_ERROR;
10031eba4c79SScott Long 	}
10041eba4c79SScott Long 
10051eba4c79SScott Long 	if (status & CAM_AUTOSNS_VALID)
10061eba4c79SScott Long 		*drvstat = DRIVER_SENSE;
10071eba4c79SScott Long }
10081eba4c79SScott Long 
10091eba4c79SScott Long static int
10101eba4c79SScott Long scsi_group_len(u_char cmd)
10111eba4c79SScott Long {
10121eba4c79SScott Long 	int len[] = {6, 10, 10, 12, 12, 12, 10, 10};
10131eba4c79SScott Long 	int group;
10141eba4c79SScott Long 
10151eba4c79SScott Long 	group = (cmd >> 5) & 0x7;
10161eba4c79SScott Long 	return (len[group]);
10171eba4c79SScott Long }
10181eba4c79SScott Long 
1019