xref: /freebsd/sys/cam/scsi/scsi_sg.c (revision a4dc509f723944821bcfcc52005ff87c9a5dee5b)
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$");
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 
63 typedef enum {
64 	SG_FLAG_LOCKED		= 0x01,
65 	SG_FLAG_INVALID		= 0x02
66 } sg_flags;
67 
68 typedef enum {
69 	SG_STATE_NORMAL
70 } sg_state;
71 
72 typedef enum {
73 	SG_RDWR_FREE,
74 	SG_RDWR_INPROG,
75 	SG_RDWR_DONE
76 } sg_rdwr_state;
77 
78 typedef enum {
79 	SG_CCB_RDWR_IO
80 } sg_ccb_types;
81 
82 #define ccb_type	ppriv_field0
83 #define ccb_rdwr	ppriv_ptr1
84 
85 struct 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 
98 struct 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 
112 static d_open_t		sgopen;
113 static d_close_t	sgclose;
114 static d_ioctl_t	sgioctl;
115 static d_write_t	sgwrite;
116 static d_read_t		sgread;
117 
118 static periph_init_t	sginit;
119 static periph_ctor_t	sgregister;
120 static periph_oninv_t	sgoninvalidate;
121 static periph_dtor_t	sgcleanup;
122 static void		sgasync(void *callback_arg, uint32_t code,
123 				struct cam_path *path, void *arg);
124 static void		sgdone(struct cam_periph *periph, union ccb *done_ccb);
125 static int		sgsendccb(struct cam_periph *periph, union ccb *ccb);
126 static int		sgsendrdwr(struct cam_periph *periph, union ccb *ccb);
127 static int		sgerror(union ccb *ccb, uint32_t cam_flags,
128 				uint32_t sense_flags);
129 static void		sg_scsiio_status(struct ccb_scsiio *csio,
130 					 u_short *hoststat, u_short *drvstat);
131 
132 static int		scsi_group_len(u_char cmd);
133 
134 static struct periph_driver sgdriver =
135 {
136 	sginit, "sg",
137 	TAILQ_HEAD_INITIALIZER(sgdriver.units), /* gen */ 0
138 };
139 PERIPHDRIVER_DECLARE(sg, sgdriver);
140 
141 static 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 
152 static int sg_version = 30125;
153 
154 static void
155 sginit(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 
171 static void
172 sgdevgonecb(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 
213 static void
214 sgoninvalidate(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 
241 static void
242 sgcleanup(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 
253 static void
254 sgasync(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 
297 static cam_status
298 sgregister(struct cam_periph *periph, void *arg)
299 {
300 	struct sg_softc *softc;
301 	struct ccb_getdev *cgd;
302 	struct ccb_pathinq cpi;
303 	int no_tags;
304 
305 	cgd = (struct ccb_getdev *)arg;
306 	if (cgd == NULL) {
307 		printf("sgregister: no getdev CCB, can't register device\n");
308 		return (CAM_REQ_CMP_ERR);
309 	}
310 
311 	softc = malloc(sizeof(*softc), M_DEVBUF, M_ZERO | M_NOWAIT);
312 	if (softc == NULL) {
313 		printf("sgregister: Unable to allocate softc\n");
314 		return (CAM_REQ_CMP_ERR);
315 	}
316 
317 	softc->state = SG_STATE_NORMAL;
318 	softc->pd_type = SID_TYPE(&cgd->inq_data);
319 	softc->sg_timeout = SG_DEFAULT_TIMEOUT / SG_DEFAULT_HZ * hz;
320 	softc->sg_user_timeout = SG_DEFAULT_TIMEOUT;
321 	TAILQ_INIT(&softc->rdwr_done);
322 	periph->softc = softc;
323 
324 	bzero(&cpi, sizeof(cpi));
325 	xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
326 	cpi.ccb_h.func_code = XPT_PATH_INQ;
327 	xpt_action((union ccb *)&cpi);
328 
329 	if (cpi.maxio == 0)
330 		softc->maxio = DFLTPHYS;	/* traditional default */
331 	else if (cpi.maxio > MAXPHYS)
332 		softc->maxio = MAXPHYS;		/* for safety */
333 	else
334 		softc->maxio = cpi.maxio;	/* real value */
335 
336 	/*
337 	 * We pass in 0 for all blocksize, since we don't know what the
338 	 * blocksize of the device is, if it even has a blocksize.
339 	 */
340 	cam_periph_unlock(periph);
341 	no_tags = (cgd->inq_data.flags & SID_CmdQue) == 0;
342 	softc->device_stats = devstat_new_entry("sg",
343 			periph->unit_number, 0,
344 			DEVSTAT_NO_BLOCKSIZE
345 			| (no_tags ? DEVSTAT_NO_ORDERED_TAGS : 0),
346 			softc->pd_type |
347 			XPORT_DEVSTAT_TYPE(cpi.transport) |
348 			DEVSTAT_TYPE_PASS,
349 			DEVSTAT_PRIORITY_PASS);
350 
351 	/*
352 	 * Acquire a reference to the periph before we create the devfs
353 	 * instance for it.  We'll release this reference once the devfs
354 	 * instance has been freed.
355 	 */
356 	if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
357 		xpt_print(periph->path, "%s: lost periph during "
358 			  "registration!\n", __func__);
359 		cam_periph_lock(periph);
360 		return (CAM_REQ_CMP_ERR);
361 	}
362 
363 	/* Register the device */
364 	softc->dev = make_dev(&sg_cdevsw, periph->unit_number,
365 			      UID_ROOT, GID_OPERATOR, 0600, "%s%d",
366 			      periph->periph_name, periph->unit_number);
367 	if (periph->unit_number < 26) {
368 		(void)make_dev_alias(softc->dev, "sg%c",
369 		    periph->unit_number + 'a');
370 	} else {
371 		(void)make_dev_alias(softc->dev, "sg%c%c",
372 		    ((periph->unit_number / 26) - 1) + 'a',
373 		    (periph->unit_number % 26) + 'a');
374 	}
375 	cam_periph_lock(periph);
376 	softc->dev->si_drv1 = periph;
377 
378 	/*
379 	 * Add as async callback so that we get
380 	 * notified if this device goes away.
381 	 */
382 	xpt_register_async(AC_LOST_DEVICE, sgasync, periph, periph->path);
383 
384 	if (bootverbose)
385 		xpt_announce_periph(periph, NULL);
386 
387 	return (CAM_REQ_CMP);
388 }
389 
390 static void
391 sgdone(struct cam_periph *periph, union ccb *done_ccb)
392 {
393 	struct sg_softc *softc;
394 	struct ccb_scsiio *csio;
395 
396 	softc = (struct sg_softc *)periph->softc;
397 	csio = &done_ccb->csio;
398 	switch (csio->ccb_h.ccb_type) {
399 	case SG_CCB_RDWR_IO:
400 	{
401 		struct sg_rdwr *rdwr;
402 		int state;
403 
404 		devstat_end_transaction(softc->device_stats,
405 					csio->dxfer_len,
406 					csio->tag_action & 0xf,
407 					((csio->ccb_h.flags & CAM_DIR_MASK) ==
408 					CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
409 					(csio->ccb_h.flags & CAM_DIR_OUT) ?
410 					DEVSTAT_WRITE : DEVSTAT_READ,
411 					NULL, NULL);
412 
413 		rdwr = done_ccb->ccb_h.ccb_rdwr;
414 		state = rdwr->state;
415 		rdwr->state = SG_RDWR_DONE;
416 		wakeup(rdwr);
417 		break;
418 	}
419 	default:
420 		panic("unknown sg CCB type");
421 	}
422 }
423 
424 static int
425 sgopen(struct cdev *dev, int flags, int fmt, struct thread *td)
426 {
427 	struct cam_periph *periph;
428 	struct sg_softc *softc;
429 	int error = 0;
430 
431 	periph = (struct cam_periph *)dev->si_drv1;
432 	if (periph == NULL)
433 		return (ENXIO);
434 
435 	if (cam_periph_acquire(periph) != CAM_REQ_CMP)
436 		return (ENXIO);
437 
438 	/*
439 	 * Don't allow access when we're running at a high securelevel.
440 	 */
441 	error = securelevel_gt(td->td_ucred, 1);
442 	if (error) {
443 		cam_periph_release(periph);
444 		return (error);
445 	}
446 
447 	cam_periph_lock(periph);
448 
449 	softc = (struct sg_softc *)periph->softc;
450 	if (softc->flags & SG_FLAG_INVALID) {
451 		cam_periph_release_locked(periph);
452 		cam_periph_unlock(periph);
453 		return (ENXIO);
454 	}
455 
456 	softc->open_count++;
457 
458 	cam_periph_unlock(periph);
459 
460 	return (error);
461 }
462 
463 static int
464 sgclose(struct cdev *dev, int flag, int fmt, struct thread *td)
465 {
466 	struct cam_periph *periph;
467 	struct sg_softc   *softc;
468 	struct mtx *mtx;
469 
470 	periph = (struct cam_periph *)dev->si_drv1;
471 	if (periph == NULL)
472 		return (ENXIO);
473 	mtx = cam_periph_mtx(periph);
474 	mtx_lock(mtx);
475 
476 	softc = periph->softc;
477 	softc->open_count--;
478 
479 	cam_periph_release_locked(periph);
480 
481 	/*
482 	 * We reference the lock directly here, instead of using
483 	 * cam_periph_unlock().  The reason is that the call to
484 	 * cam_periph_release_locked() above could result in the periph
485 	 * getting freed.  If that is the case, dereferencing the periph
486 	 * with a cam_periph_unlock() call would cause a page fault.
487 	 *
488 	 * cam_periph_release() avoids this problem using the same method,
489 	 * but we're manually acquiring and dropping the lock here to
490 	 * protect the open count and avoid another lock acquisition and
491 	 * release.
492 	 */
493 	mtx_unlock(mtx);
494 
495 	return (0);
496 }
497 
498 static int
499 sgioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
500 {
501 	union ccb *ccb;
502 	struct ccb_scsiio *csio;
503 	struct cam_periph *periph;
504 	struct sg_softc *softc;
505 	struct sg_io_hdr *req;
506 	int dir, error;
507 
508 	periph = (struct cam_periph *)dev->si_drv1;
509 	if (periph == NULL)
510 		return (ENXIO);
511 
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_IN | CAM_DIR_OUT;
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 
675 static int
676 sgwrite(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 
788 out_buf:
789 	free(buf, M_DEVBUF);
790 out_ccb:
791 	xpt_free_ccb(ccb);
792 out_hdr:
793 	free(rdwr, M_DEVBUF);
794 	return (error);
795 }
796 
797 static int
798 sgread(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;
824 search:
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 
887 static int
888 sgsendccb(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_unmapmem(ccb, &mapinfo);
917 
918 	return (error);
919 }
920 
921 static int
922 sgsendrdwr(struct cam_periph *periph, union ccb *ccb)
923 {
924 	struct sg_softc *softc;
925 
926 	softc = periph->softc;
927 	devstat_start_transaction(softc->device_stats, NULL);
928 	xpt_action(ccb);
929 	return (0);
930 }
931 
932 static int
933 sgerror(union ccb *ccb, uint32_t cam_flags, uint32_t sense_flags)
934 {
935 	struct cam_periph *periph;
936 	struct sg_softc *softc;
937 
938 	periph = xpt_path_periph(ccb->ccb_h.path);
939 	softc = (struct sg_softc *)periph->softc;
940 
941 	return (cam_periph_error(ccb, cam_flags, sense_flags,
942 				 &softc->saved_ccb));
943 }
944 
945 static void
946 sg_scsiio_status(struct ccb_scsiio *csio, u_short *hoststat, u_short *drvstat)
947 {
948 	int status;
949 
950 	status = csio->ccb_h.status;
951 
952 	switch (status & CAM_STATUS_MASK) {
953 	case CAM_REQ_CMP:
954 		*hoststat = DID_OK;
955 		*drvstat = 0;
956 		break;
957 	case CAM_REQ_CMP_ERR:
958 		*hoststat = DID_ERROR;
959 		*drvstat = 0;
960 		break;
961 	case CAM_REQ_ABORTED:
962 		*hoststat = DID_ABORT;
963 		*drvstat = 0;
964 		break;
965 	case CAM_REQ_INVALID:
966 		*hoststat = DID_ERROR;
967 		*drvstat = DRIVER_INVALID;
968 		break;
969 	case CAM_DEV_NOT_THERE:
970 		*hoststat = DID_BAD_TARGET;
971 		*drvstat = 0;
972 		break;
973 	case CAM_SEL_TIMEOUT:
974 		*hoststat = DID_NO_CONNECT;
975 		*drvstat = 0;
976 		break;
977 	case CAM_CMD_TIMEOUT:
978 		*hoststat = DID_TIME_OUT;
979 		*drvstat = 0;
980 		break;
981 	case CAM_SCSI_STATUS_ERROR:
982 		*hoststat = DID_ERROR;
983 		*drvstat = 0;
984 		break;
985 	case CAM_SCSI_BUS_RESET:
986 		*hoststat = DID_RESET;
987 		*drvstat = 0;
988 		break;
989 	case CAM_UNCOR_PARITY:
990 		*hoststat = DID_PARITY;
991 		*drvstat = 0;
992 		break;
993 	case CAM_SCSI_BUSY:
994 		*hoststat = DID_BUS_BUSY;
995 		*drvstat = 0;
996 		break;
997 	default:
998 		*hoststat = DID_ERROR;
999 		*drvstat = DRIVER_ERROR;
1000 	}
1001 
1002 	if (status & CAM_AUTOSNS_VALID)
1003 		*drvstat = DRIVER_SENSE;
1004 }
1005 
1006 static int
1007 scsi_group_len(u_char cmd)
1008 {
1009 	int len[] = {6, 10, 10, 12, 12, 12, 10, 10};
1010 	int group;
1011 
1012 	group = (cmd >> 5) & 0x7;
1013 	return (len[group]);
1014 }
1015 
1016