1/*-
2 * Copyright (c) 2007 Scott Long
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions, and the following disclaimer,
10 *    without modification, immediately at the beginning of the file.
11 * 2. The name of the author may not be used to endorse or promote products
12 *    derived from this software without specific prior written permission.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27/*
28 * scsi_sg peripheral driver.  This driver is meant to implement the Linux
29 * SG passthrough interface for SCSI.
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: stable/11/sys/cam/scsi/scsi_sg.c 366297 2020-09-30 18:09:50Z jhb $");
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/kernel.h>
38#include <sys/types.h>
39#include <sys/bio.h>
40#include <sys/malloc.h>
41#include <sys/fcntl.h>
42#include <sys/ioccom.h>
43#include <sys/conf.h>
44#include <sys/errno.h>
45#include <sys/devicestat.h>
46#include <sys/proc.h>
47#include <sys/uio.h>
48
49#include <cam/cam.h>
50#include <cam/cam_ccb.h>
51#include <cam/cam_periph.h>
52#include <cam/cam_queue.h>
53#include <cam/cam_xpt_periph.h>
54#include <cam/cam_debug.h>
55#include <cam/cam_sim.h>
56
57#include <cam/scsi/scsi_all.h>
58#include <cam/scsi/scsi_message.h>
59#include <cam/scsi/scsi_sg.h>
60
61#include <compat/linux/linux_ioctl.h>
62
63typedef enum {
64	SG_FLAG_LOCKED		= 0x01,
65	SG_FLAG_INVALID		= 0x02
66} sg_flags;
67
68typedef enum {
69	SG_STATE_NORMAL
70} sg_state;
71
72typedef enum {
73	SG_RDWR_FREE,
74	SG_RDWR_INPROG,
75	SG_RDWR_DONE
76} sg_rdwr_state;
77
78typedef enum {
79	SG_CCB_RDWR_IO
80} sg_ccb_types;
81
82#define ccb_type	ppriv_field0
83#define ccb_rdwr	ppriv_ptr1
84
85struct sg_rdwr {
86	TAILQ_ENTRY(sg_rdwr)	rdwr_link;
87	int			tag;
88	int			state;
89	int			buf_len;
90	char			*buf;
91	union ccb		*ccb;
92	union {
93		struct sg_header hdr;
94		struct sg_io_hdr io_hdr;
95	} hdr;
96};
97
98struct sg_softc {
99	sg_state		state;
100	sg_flags		flags;
101	int			open_count;
102	u_int			maxio;
103	struct devstat		*device_stats;
104	TAILQ_HEAD(, sg_rdwr)	rdwr_done;
105	struct cdev		*dev;
106	int			sg_timeout;
107	int			sg_user_timeout;
108	uint8_t			pd_type;
109	union ccb		saved_ccb;
110};
111
112static d_open_t		sgopen;
113static d_close_t	sgclose;
114static d_ioctl_t	sgioctl;
115static d_write_t	sgwrite;
116static d_read_t		sgread;
117
118static periph_init_t	sginit;
119static periph_ctor_t	sgregister;
120static periph_oninv_t	sgoninvalidate;
121static periph_dtor_t	sgcleanup;
122static void		sgasync(void *callback_arg, uint32_t code,
123				struct cam_path *path, void *arg);
124static void		sgdone(struct cam_periph *periph, union ccb *done_ccb);
125static int		sgsendccb(struct cam_periph *periph, union ccb *ccb);
126static int		sgsendrdwr(struct cam_periph *periph, union ccb *ccb);
127static int		sgerror(union ccb *ccb, uint32_t cam_flags,
128				uint32_t sense_flags);
129static void		sg_scsiio_status(struct ccb_scsiio *csio,
130					 u_short *hoststat, u_short *drvstat);
131
132static int		scsi_group_len(u_char cmd);
133
134static struct periph_driver sgdriver =
135{
136	sginit, "sg",
137	TAILQ_HEAD_INITIALIZER(sgdriver.units), /* gen */ 0
138};
139PERIPHDRIVER_DECLARE(sg, sgdriver);
140
141static struct cdevsw sg_cdevsw = {
142	.d_version =	D_VERSION,
143	.d_flags =	D_NEEDGIANT | D_TRACKCLOSE,
144	.d_open =	sgopen,
145	.d_close =	sgclose,
146	.d_ioctl =	sgioctl,
147	.d_write =	sgwrite,
148	.d_read =	sgread,
149	.d_name =	"sg",
150};
151
152static int sg_version = 30125;
153
154static void
155sginit(void)
156{
157	cam_status status;
158
159	/*
160	 * Install a global async callback.  This callback will receive aync
161	 * callbacks like "new device found".
162	 */
163	status = xpt_register_async(AC_FOUND_DEVICE, sgasync, NULL, NULL);
164
165	if (status != CAM_REQ_CMP) {
166		printf("sg: Failed to attach master async callbac "
167			"due to status 0x%x!\n", status);
168	}
169}
170
171static void
172sgdevgonecb(void *arg)
173{
174	struct cam_periph *periph;
175	struct sg_softc *softc;
176	struct mtx *mtx;
177	int i;
178
179	periph = (struct cam_periph *)arg;
180	mtx = cam_periph_mtx(periph);
181	mtx_lock(mtx);
182
183	softc = (struct sg_softc *)periph->softc;
184	KASSERT(softc->open_count >= 0, ("Negative open count %d",
185		softc->open_count));
186
187	/*
188	 * When we get this callback, we will get no more close calls from
189	 * devfs.  So if we have any dangling opens, we need to release the
190	 * reference held for that particular context.
191	 */
192	for (i = 0; i < softc->open_count; i++)
193		cam_periph_release_locked(periph);
194
195	softc->open_count = 0;
196
197	/*
198	 * Release the reference held for the device node, it is gone now.
199	 */
200	cam_periph_release_locked(periph);
201
202	/*
203	 * We reference the lock directly here, instead of using
204	 * cam_periph_unlock().  The reason is that the final call to
205	 * cam_periph_release_locked() above could result in the periph
206	 * getting freed.  If that is the case, dereferencing the periph
207	 * with a cam_periph_unlock() call would cause a page fault.
208	 */
209	mtx_unlock(mtx);
210}
211
212
213static void
214sgoninvalidate(struct cam_periph *periph)
215{
216	struct sg_softc *softc;
217
218	softc = (struct sg_softc *)periph->softc;
219
220	/*
221	 * Deregister any async callbacks.
222	 */
223	xpt_register_async(0, sgasync, periph, periph->path);
224
225	softc->flags |= SG_FLAG_INVALID;
226
227	/*
228	 * Tell devfs this device has gone away, and ask for a callback
229	 * when it has cleaned up its state.
230	 */
231	destroy_dev_sched_cb(softc->dev, sgdevgonecb, periph);
232
233	/*
234	 * XXX Return all queued I/O with ENXIO.
235	 * XXX Handle any transactions queued to the card
236	 *     with XPT_ABORT_CCB.
237	 */
238
239}
240
241static void
242sgcleanup(struct cam_periph *periph)
243{
244	struct sg_softc *softc;
245
246	softc = (struct sg_softc *)periph->softc;
247
248	devstat_remove_entry(softc->device_stats);
249
250	free(softc, M_DEVBUF);
251}
252
253static void
254sgasync(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
255{
256	struct cam_periph *periph;
257
258	periph = (struct cam_periph *)callback_arg;
259
260	switch (code) {
261	case AC_FOUND_DEVICE:
262	{
263		struct ccb_getdev *cgd;
264		cam_status status;
265
266		cgd = (struct ccb_getdev *)arg;
267		if (cgd == NULL)
268			break;
269
270		if (cgd->protocol != PROTO_SCSI)
271			break;
272
273		/*
274		 * Allocate a peripheral instance for this device and
275		 * start the probe process.
276		 */
277		status = cam_periph_alloc(sgregister, sgoninvalidate,
278					  sgcleanup, NULL, "sg",
279					  CAM_PERIPH_BIO, path,
280					  sgasync, AC_FOUND_DEVICE, cgd);
281		if ((status != CAM_REQ_CMP) && (status != CAM_REQ_INPROG)) {
282			const struct cam_status_entry *entry;
283
284			entry = cam_fetch_status_entry(status);
285			printf("sgasync: Unable to attach new device "
286				"due to status %#x: %s\n", status, entry ?
287				entry->status_text : "Unknown");
288		}
289		break;
290	}
291	default:
292		cam_periph_async(periph, code, path, arg);
293		break;
294	}
295}
296
297static cam_status
298sgregister(struct cam_periph *periph, void *arg)
299{
300	struct sg_softc *softc;
301	struct ccb_getdev *cgd;
302	struct ccb_pathinq cpi;
303	struct make_dev_args args;
304	int no_tags, error;
305
306	cgd = (struct ccb_getdev *)arg;
307	if (cgd == NULL) {
308		printf("sgregister: no getdev CCB, can't register device\n");
309		return (CAM_REQ_CMP_ERR);
310	}
311
312	softc = malloc(sizeof(*softc), M_DEVBUF, M_ZERO | M_NOWAIT);
313	if (softc == NULL) {
314		printf("sgregister: Unable to allocate softc\n");
315		return (CAM_REQ_CMP_ERR);
316	}
317
318	softc->state = SG_STATE_NORMAL;
319	softc->pd_type = SID_TYPE(&cgd->inq_data);
320	softc->sg_timeout = SG_DEFAULT_TIMEOUT / SG_DEFAULT_HZ * hz;
321	softc->sg_user_timeout = SG_DEFAULT_TIMEOUT;
322	TAILQ_INIT(&softc->rdwr_done);
323	periph->softc = softc;
324
325	xpt_path_inq(&cpi, periph->path);
326
327	if (cpi.maxio == 0)
328		softc->maxio = DFLTPHYS;	/* traditional default */
329	else if (cpi.maxio > MAXPHYS)
330		softc->maxio = MAXPHYS;		/* for safety */
331	else
332		softc->maxio = cpi.maxio;	/* real value */
333
334	/*
335	 * We pass in 0 for all blocksize, since we don't know what the
336	 * blocksize of the device is, if it even has a blocksize.
337	 */
338	cam_periph_unlock(periph);
339	no_tags = (cgd->inq_data.flags & SID_CmdQue) == 0;
340	softc->device_stats = devstat_new_entry("sg",
341			periph->unit_number, 0,
342			DEVSTAT_NO_BLOCKSIZE
343			| (no_tags ? DEVSTAT_NO_ORDERED_TAGS : 0),
344			softc->pd_type |
345			XPORT_DEVSTAT_TYPE(cpi.transport) |
346			DEVSTAT_TYPE_PASS,
347			DEVSTAT_PRIORITY_PASS);
348
349	/*
350	 * Acquire a reference to the periph before we create the devfs
351	 * instance for it.  We'll release this reference once the devfs
352	 * instance has been freed.
353	 */
354	if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
355		xpt_print(periph->path, "%s: lost periph during "
356			  "registration!\n", __func__);
357		cam_periph_lock(periph);
358		return (CAM_REQ_CMP_ERR);
359	}
360
361	/* Register the device */
362	make_dev_args_init(&args);
363	args.mda_devsw = &sg_cdevsw;
364	args.mda_unit = periph->unit_number;
365	args.mda_uid = UID_ROOT;
366	args.mda_gid = GID_OPERATOR;
367	args.mda_mode = 0600;
368	args.mda_si_drv1 = periph;
369	error = make_dev_s(&args, &softc->dev, "%s%d",
370	    periph->periph_name, periph->unit_number);
371	if (error != 0) {
372		cam_periph_lock(periph);
373		cam_periph_release_locked(periph);
374		return (CAM_REQ_CMP_ERR);
375	}
376	if (periph->unit_number < 26) {
377		(void)make_dev_alias(softc->dev, "sg%c",
378		    periph->unit_number + 'a');
379	} else {
380		(void)make_dev_alias(softc->dev, "sg%c%c",
381		    ((periph->unit_number / 26) - 1) + 'a',
382		    (periph->unit_number % 26) + 'a');
383	}
384	cam_periph_lock(periph);
385
386	/*
387	 * Add as async callback so that we get
388	 * notified if this device goes away.
389	 */
390	xpt_register_async(AC_LOST_DEVICE, sgasync, periph, periph->path);
391
392	if (bootverbose)
393		xpt_announce_periph(periph, NULL);
394
395	return (CAM_REQ_CMP);
396}
397
398static void
399sgdone(struct cam_periph *periph, union ccb *done_ccb)
400{
401	struct sg_softc *softc;
402	struct ccb_scsiio *csio;
403
404	softc = (struct sg_softc *)periph->softc;
405	csio = &done_ccb->csio;
406	switch (csio->ccb_h.ccb_type) {
407	case SG_CCB_RDWR_IO:
408	{
409		struct sg_rdwr *rdwr;
410		int state;
411
412		devstat_end_transaction(softc->device_stats,
413					csio->dxfer_len,
414					csio->tag_action & 0xf,
415					((csio->ccb_h.flags & CAM_DIR_MASK) ==
416					CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
417					(csio->ccb_h.flags & CAM_DIR_OUT) ?
418					DEVSTAT_WRITE : DEVSTAT_READ,
419					NULL, NULL);
420
421		rdwr = done_ccb->ccb_h.ccb_rdwr;
422		state = rdwr->state;
423		rdwr->state = SG_RDWR_DONE;
424		wakeup(rdwr);
425		break;
426	}
427	default:
428		panic("unknown sg CCB type");
429	}
430}
431
432static int
433sgopen(struct cdev *dev, int flags, int fmt, struct thread *td)
434{
435	struct cam_periph *periph;
436	struct sg_softc *softc;
437	int error = 0;
438
439	periph = (struct cam_periph *)dev->si_drv1;
440	if (cam_periph_acquire(periph) != CAM_REQ_CMP)
441		return (ENXIO);
442
443	/*
444	 * Don't allow access when we're running at a high securelevel.
445	 */
446	error = securelevel_gt(td->td_ucred, 1);
447	if (error) {
448		cam_periph_release(periph);
449		return (error);
450	}
451
452	cam_periph_lock(periph);
453
454	softc = (struct sg_softc *)periph->softc;
455	if (softc->flags & SG_FLAG_INVALID) {
456		cam_periph_release_locked(periph);
457		cam_periph_unlock(periph);
458		return (ENXIO);
459	}
460
461	softc->open_count++;
462
463	cam_periph_unlock(periph);
464
465	return (error);
466}
467
468static int
469sgclose(struct cdev *dev, int flag, int fmt, struct thread *td)
470{
471	struct cam_periph *periph;
472	struct sg_softc   *softc;
473	struct mtx *mtx;
474
475	periph = (struct cam_periph *)dev->si_drv1;
476	mtx = cam_periph_mtx(periph);
477	mtx_lock(mtx);
478
479	softc = periph->softc;
480	softc->open_count--;
481
482	cam_periph_release_locked(periph);
483
484	/*
485	 * We reference the lock directly here, instead of using
486	 * cam_periph_unlock().  The reason is that the call to
487	 * cam_periph_release_locked() above could result in the periph
488	 * getting freed.  If that is the case, dereferencing the periph
489	 * with a cam_periph_unlock() call would cause a page fault.
490	 *
491	 * cam_periph_release() avoids this problem using the same method,
492	 * but we're manually acquiring and dropping the lock here to
493	 * protect the open count and avoid another lock acquisition and
494	 * release.
495	 */
496	mtx_unlock(mtx);
497
498	return (0);
499}
500
501static int
502sgioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
503{
504	union ccb *ccb;
505	struct ccb_scsiio *csio;
506	struct cam_periph *periph;
507	struct sg_softc *softc;
508	struct sg_io_hdr *req;
509	int dir, error;
510
511	periph = (struct cam_periph *)dev->si_drv1;
512	cam_periph_lock(periph);
513
514	softc = (struct sg_softc *)periph->softc;
515	error = 0;
516
517	switch (cmd) {
518	case SG_GET_VERSION_NUM:
519	{
520		int *version = (int *)arg;
521
522		*version = sg_version;
523		break;
524	}
525	case SG_SET_TIMEOUT:
526	{
527		u_int user_timeout = *(u_int *)arg;
528
529		softc->sg_user_timeout = user_timeout;
530		softc->sg_timeout = user_timeout / SG_DEFAULT_HZ * hz;
531		break;
532	}
533	case SG_GET_TIMEOUT:
534		/*
535		 * The value is returned directly to the syscall.
536		 */
537		td->td_retval[0] = softc->sg_user_timeout;
538		error = 0;
539		break;
540	case SG_IO:
541		req = (struct sg_io_hdr *)arg;
542
543		if (req->cmd_len > IOCDBLEN) {
544			error = EINVAL;
545			break;
546		}
547
548		if (req->iovec_count != 0) {
549			error = EOPNOTSUPP;
550			break;
551		}
552
553		ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
554		csio = &ccb->csio;
555
556		error = copyin(req->cmdp, &csio->cdb_io.cdb_bytes,
557		    req->cmd_len);
558		if (error) {
559			xpt_release_ccb(ccb);
560			break;
561		}
562
563		switch(req->dxfer_direction) {
564		case SG_DXFER_TO_DEV:
565			dir = CAM_DIR_OUT;
566			break;
567		case SG_DXFER_FROM_DEV:
568			dir = CAM_DIR_IN;
569			break;
570		case SG_DXFER_TO_FROM_DEV:
571			dir = CAM_DIR_BOTH;
572			break;
573		case SG_DXFER_NONE:
574		default:
575			dir = CAM_DIR_NONE;
576			break;
577		}
578
579		cam_fill_csio(csio,
580			      /*retries*/1,
581			      sgdone,
582			      dir|CAM_DEV_QFRZDIS,
583			      MSG_SIMPLE_Q_TAG,
584			      req->dxferp,
585			      req->dxfer_len,
586			      req->mx_sb_len,
587			      req->cmd_len,
588			      req->timeout);
589
590		error = sgsendccb(periph, ccb);
591		if (error) {
592			req->host_status = DID_ERROR;
593			req->driver_status = DRIVER_INVALID;
594			xpt_release_ccb(ccb);
595			break;
596		}
597
598		req->status = csio->scsi_status;
599		req->masked_status = (csio->scsi_status >> 1) & 0x7f;
600		sg_scsiio_status(csio, &req->host_status, &req->driver_status);
601		req->resid = csio->resid;
602		req->duration = csio->ccb_h.timeout;
603		req->info = 0;
604
605		if ((csio->ccb_h.status & CAM_AUTOSNS_VALID)
606		    && (req->sbp != NULL)) {
607			req->sb_len_wr = req->mx_sb_len - csio->sense_resid;
608			error = copyout(&csio->sense_data, req->sbp,
609					req->sb_len_wr);
610		}
611
612		xpt_release_ccb(ccb);
613		break;
614
615	case SG_GET_RESERVED_SIZE:
616	{
617		int *size = (int *)arg;
618		*size = DFLTPHYS;
619		break;
620	}
621
622	case SG_GET_SCSI_ID:
623	{
624		struct sg_scsi_id *id = (struct sg_scsi_id *)arg;
625
626		id->host_no = cam_sim_path(xpt_path_sim(periph->path));
627		id->channel = xpt_path_path_id(periph->path);
628		id->scsi_id = xpt_path_target_id(periph->path);
629		id->lun = xpt_path_lun_id(periph->path);
630		id->scsi_type = softc->pd_type;
631		id->h_cmd_per_lun = 1;
632		id->d_queue_depth = 1;
633		id->unused[0] = 0;
634		id->unused[1] = 0;
635		break;
636	}
637
638	case SG_GET_SG_TABLESIZE:
639	{
640		int *size = (int *)arg;
641		*size = 0;
642		break;
643	}
644
645	case SG_EMULATED_HOST:
646	case SG_SET_TRANSFORM:
647	case SG_GET_TRANSFORM:
648	case SG_GET_NUM_WAITING:
649	case SG_SCSI_RESET:
650	case SG_GET_REQUEST_TABLE:
651	case SG_SET_KEEP_ORPHAN:
652	case SG_GET_KEEP_ORPHAN:
653	case SG_GET_ACCESS_COUNT:
654	case SG_SET_FORCE_LOW_DMA:
655	case SG_GET_LOW_DMA:
656	case SG_SET_FORCE_PACK_ID:
657	case SG_GET_PACK_ID:
658	case SG_SET_RESERVED_SIZE:
659	case SG_GET_COMMAND_Q:
660	case SG_SET_COMMAND_Q:
661	case SG_SET_DEBUG:
662	case SG_NEXT_CMD_LEN:
663	default:
664#ifdef CAMDEBUG
665		printf("sgioctl: rejecting cmd 0x%lx\n", cmd);
666#endif
667		error = ENODEV;
668		break;
669	}
670
671	cam_periph_unlock(periph);
672	return (error);
673}
674
675static int
676sgwrite(struct cdev *dev, struct uio *uio, int ioflag)
677{
678	union ccb *ccb;
679	struct cam_periph *periph;
680	struct ccb_scsiio *csio;
681	struct sg_softc *sc;
682	struct sg_header *hdr;
683	struct sg_rdwr *rdwr;
684	u_char cdb_cmd;
685	char *buf;
686	int error = 0, cdb_len, buf_len, dir;
687
688	periph = dev->si_drv1;
689	rdwr = malloc(sizeof(*rdwr), M_DEVBUF, M_WAITOK | M_ZERO);
690	hdr = &rdwr->hdr.hdr;
691
692	/* Copy in the header block and sanity check it */
693	if (uio->uio_resid < sizeof(*hdr)) {
694		error = EINVAL;
695		goto out_hdr;
696	}
697	error = uiomove(hdr, sizeof(*hdr), uio);
698	if (error)
699		goto out_hdr;
700
701	/* XXX: We don't support SG 3.x read/write API. */
702	if (hdr->reply_len < 0) {
703		error = ENODEV;
704		goto out_hdr;
705	}
706
707	ccb = xpt_alloc_ccb();
708	if (ccb == NULL) {
709		error = ENOMEM;
710		goto out_hdr;
711	}
712	csio = &ccb->csio;
713
714	/*
715	 * Copy in the CDB block.  The designers of the interface didn't
716	 * bother to provide a size for this in the header, so we have to
717	 * figure it out ourselves.
718	 */
719	if (uio->uio_resid < 1)
720		goto out_ccb;
721	error = uiomove(&cdb_cmd, 1, uio);
722	if (error)
723		goto out_ccb;
724	if (hdr->twelve_byte)
725		cdb_len = 12;
726	else
727		cdb_len = scsi_group_len(cdb_cmd);
728	/*
729	 * We've already read the first byte of the CDB and advanced the uio
730	 * pointer.  Just read the rest.
731	 */
732	csio->cdb_io.cdb_bytes[0] = cdb_cmd;
733	error = uiomove(&csio->cdb_io.cdb_bytes[1], cdb_len - 1, uio);
734	if (error)
735		goto out_ccb;
736
737	/*
738	 * Now set up the data block.  Again, the designers didn't bother
739	 * to make this reliable.
740	 */
741	buf_len = uio->uio_resid;
742	if (buf_len != 0) {
743		buf = malloc(buf_len, M_DEVBUF, M_WAITOK | M_ZERO);
744		error = uiomove(buf, buf_len, uio);
745		if (error)
746			goto out_buf;
747		dir = CAM_DIR_OUT;
748	} else if (hdr->reply_len != 0) {
749		buf = malloc(hdr->reply_len, M_DEVBUF, M_WAITOK | M_ZERO);
750		buf_len = hdr->reply_len;
751		dir = CAM_DIR_IN;
752	} else {
753		buf = NULL;
754		buf_len = 0;
755		dir = CAM_DIR_NONE;
756	}
757
758	cam_periph_lock(periph);
759	sc = periph->softc;
760	xpt_setup_ccb(&ccb->ccb_h, periph->path, CAM_PRIORITY_NORMAL);
761	cam_fill_csio(csio,
762		      /*retries*/1,
763		      sgdone,
764		      dir|CAM_DEV_QFRZDIS,
765		      MSG_SIMPLE_Q_TAG,
766		      buf,
767		      buf_len,
768		      SG_MAX_SENSE,
769		      cdb_len,
770		      sc->sg_timeout);
771
772	/*
773	 * Send off the command and hope that it works. This path does not
774	 * go through sgstart because the I/O is supposed to be asynchronous.
775	 */
776	rdwr->buf = buf;
777	rdwr->buf_len = buf_len;
778	rdwr->tag = hdr->pack_id;
779	rdwr->ccb = ccb;
780	rdwr->state = SG_RDWR_INPROG;
781	ccb->ccb_h.ccb_rdwr = rdwr;
782	ccb->ccb_h.ccb_type = SG_CCB_RDWR_IO;
783	TAILQ_INSERT_TAIL(&sc->rdwr_done, rdwr, rdwr_link);
784	error = sgsendrdwr(periph, ccb);
785	cam_periph_unlock(periph);
786	return (error);
787
788out_buf:
789	free(buf, M_DEVBUF);
790out_ccb:
791	xpt_free_ccb(ccb);
792out_hdr:
793	free(rdwr, M_DEVBUF);
794	return (error);
795}
796
797static int
798sgread(struct cdev *dev, struct uio *uio, int ioflag)
799{
800	struct ccb_scsiio *csio;
801	struct cam_periph *periph;
802	struct sg_softc *sc;
803	struct sg_header *hdr;
804	struct sg_rdwr *rdwr;
805	u_short hstat, dstat;
806	int error, pack_len, reply_len, pack_id;
807
808	periph = dev->si_drv1;
809
810	/* XXX The pack len field needs to be updated and written out instead
811	 * of discarded.  Not sure how to do that.
812	 */
813	uio->uio_rw = UIO_WRITE;
814	if ((error = uiomove(&pack_len, 4, uio)) != 0)
815		return (error);
816	if ((error = uiomove(&reply_len, 4, uio)) != 0)
817		return (error);
818	if ((error = uiomove(&pack_id, 4, uio)) != 0)
819		return (error);
820	uio->uio_rw = UIO_READ;
821
822	cam_periph_lock(periph);
823	sc = periph->softc;
824search:
825	TAILQ_FOREACH(rdwr, &sc->rdwr_done, rdwr_link) {
826		if (rdwr->tag == pack_id)
827			break;
828	}
829	if ((rdwr == NULL) || (rdwr->state != SG_RDWR_DONE)) {
830		if (cam_periph_sleep(periph, rdwr, PCATCH, "sgread", 0) == ERESTART)
831			return (EAGAIN);
832		goto search;
833	}
834	TAILQ_REMOVE(&sc->rdwr_done, rdwr, rdwr_link);
835	cam_periph_unlock(periph);
836
837	hdr = &rdwr->hdr.hdr;
838	csio = &rdwr->ccb->csio;
839	sg_scsiio_status(csio, &hstat, &dstat);
840	hdr->host_status = hstat;
841	hdr->driver_status = dstat;
842	hdr->target_status = csio->scsi_status >> 1;
843
844	switch (hstat) {
845	case DID_OK:
846	case DID_PASSTHROUGH:
847	case DID_SOFT_ERROR:
848		hdr->result = 0;
849		break;
850	case DID_NO_CONNECT:
851	case DID_BUS_BUSY:
852	case DID_TIME_OUT:
853		hdr->result = EBUSY;
854		break;
855	case DID_BAD_TARGET:
856	case DID_ABORT:
857	case DID_PARITY:
858	case DID_RESET:
859	case DID_BAD_INTR:
860	case DID_ERROR:
861	default:
862		hdr->result = EIO;
863		break;
864	}
865
866	if (dstat == DRIVER_SENSE) {
867		bcopy(&csio->sense_data, hdr->sense_buffer,
868		      min(csio->sense_len, SG_MAX_SENSE));
869#ifdef CAMDEBUG
870		scsi_sense_print(csio);
871#endif
872	}
873
874	error = uiomove(&hdr->result, sizeof(*hdr) -
875			offsetof(struct sg_header, result), uio);
876	if ((error == 0) && (hdr->result == 0))
877		error = uiomove(rdwr->buf, rdwr->buf_len, uio);
878
879	cam_periph_lock(periph);
880	xpt_free_ccb(rdwr->ccb);
881	cam_periph_unlock(periph);
882	free(rdwr->buf, M_DEVBUF);
883	free(rdwr, M_DEVBUF);
884	return (error);
885}
886
887static int
888sgsendccb(struct cam_periph *periph, union ccb *ccb)
889{
890	struct sg_softc *softc;
891	struct cam_periph_map_info mapinfo;
892	int error;
893
894	softc = periph->softc;
895	bzero(&mapinfo, sizeof(mapinfo));
896
897	/*
898	 * cam_periph_mapmem calls into proc and vm functions that can
899	 * sleep as well as trigger I/O, so we can't hold the lock.
900	 * Dropping it here is reasonably safe.
901	 * The only CCB opcode that is possible here is XPT_SCSI_IO, no
902	 * need for additional checks.
903	 */
904	cam_periph_unlock(periph);
905	error = cam_periph_mapmem(ccb, &mapinfo, softc->maxio);
906	cam_periph_lock(periph);
907	if (error)
908		return (error);
909
910	error = cam_periph_runccb(ccb,
911				  sgerror,
912				  CAM_RETRY_SELTO,
913				  SF_RETRY_UA,
914				  softc->device_stats);
915
916	cam_periph_unlock(periph);
917	cam_periph_unmapmem(ccb, &mapinfo);
918	cam_periph_lock(periph);
919
920	return (error);
921}
922
923static int
924sgsendrdwr(struct cam_periph *periph, union ccb *ccb)
925{
926	struct sg_softc *softc;
927
928	softc = periph->softc;
929	devstat_start_transaction(softc->device_stats, NULL);
930	xpt_action(ccb);
931	return (0);
932}
933
934static int
935sgerror(union ccb *ccb, uint32_t cam_flags, uint32_t sense_flags)
936{
937	struct cam_periph *periph;
938	struct sg_softc *softc;
939
940	periph = xpt_path_periph(ccb->ccb_h.path);
941	softc = (struct sg_softc *)periph->softc;
942
943	return (cam_periph_error(ccb, cam_flags, sense_flags,
944				 &softc->saved_ccb));
945}
946
947static void
948sg_scsiio_status(struct ccb_scsiio *csio, u_short *hoststat, u_short *drvstat)
949{
950	int status;
951
952	status = csio->ccb_h.status;
953
954	switch (status & CAM_STATUS_MASK) {
955	case CAM_REQ_CMP:
956		*hoststat = DID_OK;
957		*drvstat = 0;
958		break;
959	case CAM_REQ_CMP_ERR:
960		*hoststat = DID_ERROR;
961		*drvstat = 0;
962		break;
963	case CAM_REQ_ABORTED:
964		*hoststat = DID_ABORT;
965		*drvstat = 0;
966		break;
967	case CAM_REQ_INVALID:
968		*hoststat = DID_ERROR;
969		*drvstat = DRIVER_INVALID;
970		break;
971	case CAM_DEV_NOT_THERE:
972		*hoststat = DID_BAD_TARGET;
973		*drvstat = 0;
974		break;
975	case CAM_SEL_TIMEOUT:
976		*hoststat = DID_NO_CONNECT;
977		*drvstat = 0;
978		break;
979	case CAM_CMD_TIMEOUT:
980		*hoststat = DID_TIME_OUT;
981		*drvstat = 0;
982		break;
983	case CAM_SCSI_STATUS_ERROR:
984		*hoststat = DID_ERROR;
985		*drvstat = 0;
986		break;
987	case CAM_SCSI_BUS_RESET:
988		*hoststat = DID_RESET;
989		*drvstat = 0;
990		break;
991	case CAM_UNCOR_PARITY:
992		*hoststat = DID_PARITY;
993		*drvstat = 0;
994		break;
995	case CAM_SCSI_BUSY:
996		*hoststat = DID_BUS_BUSY;
997		*drvstat = 0;
998		break;
999	default:
1000		*hoststat = DID_ERROR;
1001		*drvstat = DRIVER_ERROR;
1002	}
1003
1004	if (status & CAM_AUTOSNS_VALID)
1005		*drvstat = DRIVER_SENSE;
1006}
1007
1008static int
1009scsi_group_len(u_char cmd)
1010{
1011	int len[] = {6, 10, 10, 12, 12, 12, 10, 10};
1012	int group;
1013
1014	group = (cmd >> 5) & 0x7;
1015	return (len[group]);
1016}
1017
1018