xref: /freebsd/sys/cam/scsi/scsi_pass.c (revision 094fc1ed0f2627525c7b0342efcbad5be7a8546a)
1 /*-
2  * Copyright (c) 1997, 1998, 2000 Justin T. Gibbs.
3  * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions, and the following disclaimer,
11  *    without modification, immediately at the beginning of the file.
12  * 2. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/conf.h>
35 #include <sys/types.h>
36 #include <sys/bio.h>
37 #include <sys/bus.h>
38 #include <sys/devicestat.h>
39 #include <sys/errno.h>
40 #include <sys/fcntl.h>
41 #include <sys/malloc.h>
42 #include <sys/proc.h>
43 #include <sys/poll.h>
44 #include <sys/selinfo.h>
45 #include <sys/sdt.h>
46 #include <sys/taskqueue.h>
47 #include <vm/uma.h>
48 #include <vm/vm.h>
49 #include <vm/vm_extern.h>
50 
51 #include <machine/bus.h>
52 
53 #include <cam/cam.h>
54 #include <cam/cam_ccb.h>
55 #include <cam/cam_periph.h>
56 #include <cam/cam_queue.h>
57 #include <cam/cam_xpt.h>
58 #include <cam/cam_xpt_periph.h>
59 #include <cam/cam_debug.h>
60 #include <cam/cam_compat.h>
61 #include <cam/cam_xpt_periph.h>
62 
63 #include <cam/scsi/scsi_all.h>
64 #include <cam/scsi/scsi_pass.h>
65 
66 typedef enum {
67 	PASS_FLAG_OPEN			= 0x01,
68 	PASS_FLAG_LOCKED		= 0x02,
69 	PASS_FLAG_INVALID		= 0x04,
70 	PASS_FLAG_INITIAL_PHYSPATH	= 0x08,
71 	PASS_FLAG_ZONE_INPROG		= 0x10,
72 	PASS_FLAG_ZONE_VALID		= 0x20,
73 	PASS_FLAG_UNMAPPED_CAPABLE	= 0x40,
74 	PASS_FLAG_ABANDONED_REF_SET	= 0x80
75 } pass_flags;
76 
77 typedef enum {
78 	PASS_STATE_NORMAL
79 } pass_state;
80 
81 typedef enum {
82 	PASS_CCB_BUFFER_IO,
83 	PASS_CCB_QUEUED_IO
84 } pass_ccb_types;
85 
86 #define ccb_type	ppriv_field0
87 #define ccb_ioreq	ppriv_ptr1
88 
89 /*
90  * The maximum number of memory segments we preallocate.
91  */
92 #define	PASS_MAX_SEGS	16
93 
94 typedef enum {
95 	PASS_IO_NONE		= 0x00,
96 	PASS_IO_USER_SEG_MALLOC	= 0x01,
97 	PASS_IO_KERN_SEG_MALLOC	= 0x02,
98 	PASS_IO_ABANDONED	= 0x04
99 } pass_io_flags;
100 
101 struct pass_io_req {
102 	union ccb			 ccb;
103 	union ccb			*alloced_ccb;
104 	union ccb			*user_ccb_ptr;
105 	camq_entry			 user_periph_links;
106 	ccb_ppriv_area			 user_periph_priv;
107 	struct cam_periph_map_info	 mapinfo;
108 	pass_io_flags			 flags;
109 	ccb_flags			 data_flags;
110 	int				 num_user_segs;
111 	bus_dma_segment_t		 user_segs[PASS_MAX_SEGS];
112 	int				 num_kern_segs;
113 	bus_dma_segment_t		 kern_segs[PASS_MAX_SEGS];
114 	bus_dma_segment_t		*user_segptr;
115 	bus_dma_segment_t		*kern_segptr;
116 	int				 num_bufs;
117 	uint32_t			 dirs[CAM_PERIPH_MAXMAPS];
118 	uint32_t			 lengths[CAM_PERIPH_MAXMAPS];
119 	uint8_t				*user_bufs[CAM_PERIPH_MAXMAPS];
120 	uint8_t				*kern_bufs[CAM_PERIPH_MAXMAPS];
121 	struct bintime			 start_time;
122 	TAILQ_ENTRY(pass_io_req)	 links;
123 };
124 
125 struct pass_softc {
126 	pass_state		  state;
127 	pass_flags		  flags;
128 	u_int8_t		  pd_type;
129 	union ccb		  saved_ccb;
130 	int			  open_count;
131 	u_int		 	  maxio;
132 	struct devstat		 *device_stats;
133 	struct cdev		 *dev;
134 	struct cdev		 *alias_dev;
135 	struct task		  add_physpath_task;
136 	struct task		  shutdown_kqueue_task;
137 	struct selinfo		  read_select;
138 	TAILQ_HEAD(, pass_io_req) incoming_queue;
139 	TAILQ_HEAD(, pass_io_req) active_queue;
140 	TAILQ_HEAD(, pass_io_req) abandoned_queue;
141 	TAILQ_HEAD(, pass_io_req) done_queue;
142 	struct cam_periph	 *periph;
143 	char			  zone_name[12];
144 	char			  io_zone_name[12];
145 	uma_zone_t		  pass_zone;
146 	uma_zone_t		  pass_io_zone;
147 	size_t			  io_zone_size;
148 };
149 
150 static	d_open_t	passopen;
151 static	d_close_t	passclose;
152 static	d_ioctl_t	passioctl;
153 static	d_ioctl_t	passdoioctl;
154 static	d_poll_t	passpoll;
155 static	d_kqfilter_t	passkqfilter;
156 static	void		passreadfiltdetach(struct knote *kn);
157 static	int		passreadfilt(struct knote *kn, long hint);
158 
159 static	periph_init_t	passinit;
160 static	periph_ctor_t	passregister;
161 static	periph_oninv_t	passoninvalidate;
162 static	periph_dtor_t	passcleanup;
163 static	periph_start_t	passstart;
164 static	void		pass_shutdown_kqueue(void *context, int pending);
165 static	void		pass_add_physpath(void *context, int pending);
166 static	void		passasync(void *callback_arg, u_int32_t code,
167 				  struct cam_path *path, void *arg);
168 static	void		passdone(struct cam_periph *periph,
169 				 union ccb *done_ccb);
170 static	int		passcreatezone(struct cam_periph *periph);
171 static	void		passiocleanup(struct pass_softc *softc,
172 				      struct pass_io_req *io_req);
173 static	int		passcopysglist(struct cam_periph *periph,
174 				       struct pass_io_req *io_req,
175 				       ccb_flags direction);
176 static	int		passmemsetup(struct cam_periph *periph,
177 				     struct pass_io_req *io_req);
178 static	int		passmemdone(struct cam_periph *periph,
179 				    struct pass_io_req *io_req);
180 static	int		passerror(union ccb *ccb, u_int32_t cam_flags,
181 				  u_int32_t sense_flags);
182 static 	int		passsendccb(struct cam_periph *periph, union ccb *ccb,
183 				    union ccb *inccb);
184 
185 static struct periph_driver passdriver =
186 {
187 	passinit, "pass",
188 	TAILQ_HEAD_INITIALIZER(passdriver.units), /* generation */ 0
189 };
190 
191 PERIPHDRIVER_DECLARE(pass, passdriver);
192 
193 static struct cdevsw pass_cdevsw = {
194 	.d_version =	D_VERSION,
195 	.d_flags =	D_TRACKCLOSE,
196 	.d_open =	passopen,
197 	.d_close =	passclose,
198 	.d_ioctl =	passioctl,
199 	.d_poll = 	passpoll,
200 	.d_kqfilter = 	passkqfilter,
201 	.d_name =	"pass",
202 };
203 
204 static struct filterops passread_filtops = {
205 	.f_isfd	=	1,
206 	.f_detach =	passreadfiltdetach,
207 	.f_event =	passreadfilt
208 };
209 
210 static MALLOC_DEFINE(M_SCSIPASS, "scsi_pass", "scsi passthrough buffers");
211 
212 static void
213 passinit(void)
214 {
215 	cam_status status;
216 
217 	/*
218 	 * Install a global async callback.  This callback will
219 	 * receive async callbacks like "new device found".
220 	 */
221 	status = xpt_register_async(AC_FOUND_DEVICE, passasync, NULL, NULL);
222 
223 	if (status != CAM_REQ_CMP) {
224 		printf("pass: Failed to attach master async callback "
225 		       "due to status 0x%x!\n", status);
226 	}
227 
228 }
229 
230 static void
231 passrejectios(struct cam_periph *periph)
232 {
233 	struct pass_io_req *io_req, *io_req2;
234 	struct pass_softc *softc;
235 
236 	softc = (struct pass_softc *)periph->softc;
237 
238 	/*
239 	 * The user can no longer get status for I/O on the done queue, so
240 	 * clean up all outstanding I/O on the done queue.
241 	 */
242 	TAILQ_FOREACH_SAFE(io_req, &softc->done_queue, links, io_req2) {
243 		TAILQ_REMOVE(&softc->done_queue, io_req, links);
244 		passiocleanup(softc, io_req);
245 		uma_zfree(softc->pass_zone, io_req);
246 	}
247 
248 	/*
249 	 * The underlying device is gone, so we can't issue these I/Os.
250 	 * The devfs node has been shut down, so we can't return status to
251 	 * the user.  Free any I/O left on the incoming queue.
252 	 */
253 	TAILQ_FOREACH_SAFE(io_req, &softc->incoming_queue, links, io_req2) {
254 		TAILQ_REMOVE(&softc->incoming_queue, io_req, links);
255 		passiocleanup(softc, io_req);
256 		uma_zfree(softc->pass_zone, io_req);
257 	}
258 
259 	/*
260 	 * Normally we would put I/Os on the abandoned queue and acquire a
261 	 * reference when we saw the final close.  But, the device went
262 	 * away and devfs may have moved everything off to deadfs by the
263 	 * time the I/O done callback is called; as a result, we won't see
264 	 * any more closes.  So, if we have any active I/Os, we need to put
265 	 * them on the abandoned queue.  When the abandoned queue is empty,
266 	 * we'll release the remaining reference (see below) to the peripheral.
267 	 */
268 	TAILQ_FOREACH_SAFE(io_req, &softc->active_queue, links, io_req2) {
269 		TAILQ_REMOVE(&softc->active_queue, io_req, links);
270 		io_req->flags |= PASS_IO_ABANDONED;
271 		TAILQ_INSERT_TAIL(&softc->abandoned_queue, io_req, links);
272 	}
273 
274 	/*
275 	 * If we put any I/O on the abandoned queue, acquire a reference.
276 	 */
277 	if ((!TAILQ_EMPTY(&softc->abandoned_queue))
278 	 && ((softc->flags & PASS_FLAG_ABANDONED_REF_SET) == 0)) {
279 		cam_periph_doacquire(periph);
280 		softc->flags |= PASS_FLAG_ABANDONED_REF_SET;
281 	}
282 }
283 
284 static void
285 passdevgonecb(void *arg)
286 {
287 	struct cam_periph *periph;
288 	struct mtx *mtx;
289 	struct pass_softc *softc;
290 	int i;
291 
292 	periph = (struct cam_periph *)arg;
293 	mtx = cam_periph_mtx(periph);
294 	mtx_lock(mtx);
295 
296 	softc = (struct pass_softc *)periph->softc;
297 	KASSERT(softc->open_count >= 0, ("Negative open count %d",
298 		softc->open_count));
299 
300 	/*
301 	 * When we get this callback, we will get no more close calls from
302 	 * devfs.  So if we have any dangling opens, we need to release the
303 	 * reference held for that particular context.
304 	 */
305 	for (i = 0; i < softc->open_count; i++)
306 		cam_periph_release_locked(periph);
307 
308 	softc->open_count = 0;
309 
310 	/*
311 	 * Release the reference held for the device node, it is gone now.
312 	 * Accordingly, inform all queued I/Os of their fate.
313 	 */
314 	cam_periph_release_locked(periph);
315 	passrejectios(periph);
316 
317 	/*
318 	 * We reference the SIM lock directly here, instead of using
319 	 * cam_periph_unlock().  The reason is that the final call to
320 	 * cam_periph_release_locked() above could result in the periph
321 	 * getting freed.  If that is the case, dereferencing the periph
322 	 * with a cam_periph_unlock() call would cause a page fault.
323 	 */
324 	mtx_unlock(mtx);
325 
326 	/*
327 	 * We have to remove our kqueue context from a thread because it
328 	 * may sleep.  It would be nice if we could get a callback from
329 	 * kqueue when it is done cleaning up resources.
330 	 */
331 	taskqueue_enqueue(taskqueue_thread, &softc->shutdown_kqueue_task);
332 }
333 
334 static void
335 passoninvalidate(struct cam_periph *periph)
336 {
337 	struct pass_softc *softc;
338 
339 	softc = (struct pass_softc *)periph->softc;
340 
341 	/*
342 	 * De-register any async callbacks.
343 	 */
344 	xpt_register_async(0, passasync, periph, periph->path);
345 
346 	softc->flags |= PASS_FLAG_INVALID;
347 
348 	/*
349 	 * Tell devfs this device has gone away, and ask for a callback
350 	 * when it has cleaned up its state.
351 	 */
352 	destroy_dev_sched_cb(softc->dev, passdevgonecb, periph);
353 }
354 
355 static void
356 passcleanup(struct cam_periph *periph)
357 {
358 	struct pass_softc *softc;
359 
360 	softc = (struct pass_softc *)periph->softc;
361 
362 	cam_periph_assert(periph, MA_OWNED);
363 	KASSERT(TAILQ_EMPTY(&softc->active_queue),
364 		("%s called when there are commands on the active queue!\n",
365 		__func__));
366 	KASSERT(TAILQ_EMPTY(&softc->abandoned_queue),
367 		("%s called when there are commands on the abandoned queue!\n",
368 		__func__));
369 	KASSERT(TAILQ_EMPTY(&softc->incoming_queue),
370 		("%s called when there are commands on the incoming queue!\n",
371 		__func__));
372 	KASSERT(TAILQ_EMPTY(&softc->done_queue),
373 		("%s called when there are commands on the done queue!\n",
374 		__func__));
375 
376 	devstat_remove_entry(softc->device_stats);
377 
378 	cam_periph_unlock(periph);
379 
380 	/*
381 	 * We call taskqueue_drain() for the physpath task to make sure it
382 	 * is complete.  We drop the lock because this can potentially
383 	 * sleep.  XXX KDM that is bad.  Need a way to get a callback when
384 	 * a taskqueue is drained.
385 	 *
386  	 * Note that we don't drain the kqueue shutdown task queue.  This
387 	 * is because we hold a reference on the periph for kqueue, and
388 	 * release that reference from the kqueue shutdown task queue.  So
389 	 * we cannot come into this routine unless we've released that
390 	 * reference.  Also, because that could be the last reference, we
391 	 * could be called from the cam_periph_release() call in
392 	 * pass_shutdown_kqueue().  In that case, the taskqueue_drain()
393 	 * would deadlock.  It would be preferable if we had a way to
394 	 * get a callback when a taskqueue is done.
395 	 */
396 	taskqueue_drain(taskqueue_thread, &softc->add_physpath_task);
397 
398 	cam_periph_lock(periph);
399 
400 	free(softc, M_DEVBUF);
401 }
402 
403 static void
404 pass_shutdown_kqueue(void *context, int pending)
405 {
406 	struct cam_periph *periph;
407 	struct pass_softc *softc;
408 
409 	periph = context;
410 	softc = periph->softc;
411 
412 	knlist_clear(&softc->read_select.si_note, /*is_locked*/ 0);
413 	knlist_destroy(&softc->read_select.si_note);
414 
415 	/*
416 	 * Release the reference we held for kqueue.
417 	 */
418 	cam_periph_release(periph);
419 }
420 
421 static void
422 pass_add_physpath(void *context, int pending)
423 {
424 	struct cam_periph *periph;
425 	struct pass_softc *softc;
426 	struct mtx *mtx;
427 	char *physpath;
428 
429 	/*
430 	 * If we have one, create a devfs alias for our
431 	 * physical path.
432 	 */
433 	periph = context;
434 	softc = periph->softc;
435 	physpath = malloc(MAXPATHLEN, M_DEVBUF, M_WAITOK);
436 	mtx = cam_periph_mtx(periph);
437 	mtx_lock(mtx);
438 
439 	if (periph->flags & CAM_PERIPH_INVALID)
440 		goto out;
441 
442 	if (xpt_getattr(physpath, MAXPATHLEN,
443 			"GEOM::physpath", periph->path) == 0
444 	 && strlen(physpath) != 0) {
445 
446 		mtx_unlock(mtx);
447 		make_dev_physpath_alias(MAKEDEV_WAITOK, &softc->alias_dev,
448 					softc->dev, softc->alias_dev, physpath);
449 		mtx_lock(mtx);
450 	}
451 
452 out:
453 	/*
454 	 * Now that we've made our alias, we no longer have to have a
455 	 * reference to the device.
456 	 */
457 	if ((softc->flags & PASS_FLAG_INITIAL_PHYSPATH) == 0)
458 		softc->flags |= PASS_FLAG_INITIAL_PHYSPATH;
459 
460 	/*
461 	 * We always acquire a reference to the periph before queueing this
462 	 * task queue function, so it won't go away before we run.
463 	 */
464 	while (pending-- > 0)
465 		cam_periph_release_locked(periph);
466 	mtx_unlock(mtx);
467 
468 	free(physpath, M_DEVBUF);
469 }
470 
471 static void
472 passasync(void *callback_arg, u_int32_t code,
473 	  struct cam_path *path, void *arg)
474 {
475 	struct cam_periph *periph;
476 
477 	periph = (struct cam_periph *)callback_arg;
478 
479 	switch (code) {
480 	case AC_FOUND_DEVICE:
481 	{
482 		struct ccb_getdev *cgd;
483 		cam_status status;
484 
485 		cgd = (struct ccb_getdev *)arg;
486 		if (cgd == NULL)
487 			break;
488 
489 		/*
490 		 * Allocate a peripheral instance for
491 		 * this device and start the probe
492 		 * process.
493 		 */
494 		status = cam_periph_alloc(passregister, passoninvalidate,
495 					  passcleanup, passstart, "pass",
496 					  CAM_PERIPH_BIO, path,
497 					  passasync, AC_FOUND_DEVICE, cgd);
498 
499 		if (status != CAM_REQ_CMP
500 		 && status != CAM_REQ_INPROG) {
501 			const struct cam_status_entry *entry;
502 
503 			entry = cam_fetch_status_entry(status);
504 
505 			printf("passasync: Unable to attach new device "
506 			       "due to status %#x: %s\n", status, entry ?
507 			       entry->status_text : "Unknown");
508 		}
509 
510 		break;
511 	}
512 	case AC_ADVINFO_CHANGED:
513 	{
514 		uintptr_t buftype;
515 
516 		buftype = (uintptr_t)arg;
517 		if (buftype == CDAI_TYPE_PHYS_PATH) {
518 			struct pass_softc *softc;
519 			cam_status status;
520 
521 			softc = (struct pass_softc *)periph->softc;
522 			/*
523 			 * Acquire a reference to the periph before we
524 			 * start the taskqueue, so that we don't run into
525 			 * a situation where the periph goes away before
526 			 * the task queue has a chance to run.
527 			 */
528 			status = cam_periph_acquire(periph);
529 			if (status != CAM_REQ_CMP)
530 				break;
531 
532 			taskqueue_enqueue(taskqueue_thread,
533 					  &softc->add_physpath_task);
534 		}
535 		break;
536 	}
537 	default:
538 		cam_periph_async(periph, code, path, arg);
539 		break;
540 	}
541 }
542 
543 static cam_status
544 passregister(struct cam_periph *periph, void *arg)
545 {
546 	struct pass_softc *softc;
547 	struct ccb_getdev *cgd;
548 	struct ccb_pathinq cpi;
549 	struct make_dev_args args;
550 	int error, no_tags;
551 
552 	cgd = (struct ccb_getdev *)arg;
553 	if (cgd == NULL) {
554 		printf("%s: no getdev CCB, can't register device\n", __func__);
555 		return(CAM_REQ_CMP_ERR);
556 	}
557 
558 	softc = (struct pass_softc *)malloc(sizeof(*softc),
559 					    M_DEVBUF, M_NOWAIT);
560 
561 	if (softc == NULL) {
562 		printf("%s: Unable to probe new device. "
563 		       "Unable to allocate softc\n", __func__);
564 		return(CAM_REQ_CMP_ERR);
565 	}
566 
567 	bzero(softc, sizeof(*softc));
568 	softc->state = PASS_STATE_NORMAL;
569 	if (cgd->protocol == PROTO_SCSI || cgd->protocol == PROTO_ATAPI)
570 		softc->pd_type = SID_TYPE(&cgd->inq_data);
571 	else if (cgd->protocol == PROTO_SATAPM)
572 		softc->pd_type = T_ENCLOSURE;
573 	else
574 		softc->pd_type = T_DIRECT;
575 
576 	periph->softc = softc;
577 	softc->periph = periph;
578 	TAILQ_INIT(&softc->incoming_queue);
579 	TAILQ_INIT(&softc->active_queue);
580 	TAILQ_INIT(&softc->abandoned_queue);
581 	TAILQ_INIT(&softc->done_queue);
582 	snprintf(softc->zone_name, sizeof(softc->zone_name), "%s%d",
583 		 periph->periph_name, periph->unit_number);
584 	snprintf(softc->io_zone_name, sizeof(softc->io_zone_name), "%s%dIO",
585 		 periph->periph_name, periph->unit_number);
586 	softc->io_zone_size = MAXPHYS;
587 	knlist_init_mtx(&softc->read_select.si_note, cam_periph_mtx(periph));
588 
589 	bzero(&cpi, sizeof(cpi));
590 	xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
591 	cpi.ccb_h.func_code = XPT_PATH_INQ;
592 	xpt_action((union ccb *)&cpi);
593 
594 	if (cpi.maxio == 0)
595 		softc->maxio = DFLTPHYS;	/* traditional default */
596 	else if (cpi.maxio > MAXPHYS)
597 		softc->maxio = MAXPHYS;		/* for safety */
598 	else
599 		softc->maxio = cpi.maxio;	/* real value */
600 
601 	if (cpi.hba_misc & PIM_UNMAPPED)
602 		softc->flags |= PASS_FLAG_UNMAPPED_CAPABLE;
603 
604 	/*
605 	 * We pass in 0 for a blocksize, since we don't
606 	 * know what the blocksize of this device is, if
607 	 * it even has a blocksize.
608 	 */
609 	cam_periph_unlock(periph);
610 	no_tags = (cgd->inq_data.flags & SID_CmdQue) == 0;
611 	softc->device_stats = devstat_new_entry("pass",
612 			  periph->unit_number, 0,
613 			  DEVSTAT_NO_BLOCKSIZE
614 			  | (no_tags ? DEVSTAT_NO_ORDERED_TAGS : 0),
615 			  softc->pd_type |
616 			  XPORT_DEVSTAT_TYPE(cpi.transport) |
617 			  DEVSTAT_TYPE_PASS,
618 			  DEVSTAT_PRIORITY_PASS);
619 
620 	/*
621 	 * Initialize the taskqueue handler for shutting down kqueue.
622 	 */
623 	TASK_INIT(&softc->shutdown_kqueue_task, /*priority*/ 0,
624 		  pass_shutdown_kqueue, periph);
625 
626 	/*
627 	 * Acquire a reference to the periph that we can release once we've
628 	 * cleaned up the kqueue.
629 	 */
630 	if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
631 		xpt_print(periph->path, "%s: lost periph during "
632 			  "registration!\n", __func__);
633 		cam_periph_lock(periph);
634 		return (CAM_REQ_CMP_ERR);
635 	}
636 
637 	/*
638 	 * Acquire a reference to the periph before we create the devfs
639 	 * instance for it.  We'll release this reference once the devfs
640 	 * instance has been freed.
641 	 */
642 	if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
643 		xpt_print(periph->path, "%s: lost periph during "
644 			  "registration!\n", __func__);
645 		cam_periph_lock(periph);
646 		return (CAM_REQ_CMP_ERR);
647 	}
648 
649 	/* Register the device */
650 	make_dev_args_init(&args);
651 	args.mda_devsw = &pass_cdevsw;
652 	args.mda_unit = periph->unit_number;
653 	args.mda_uid = UID_ROOT;
654 	args.mda_gid = GID_OPERATOR;
655 	args.mda_mode = 0600;
656 	args.mda_si_drv1 = periph;
657 	error = make_dev_s(&args, &softc->dev, "%s%d", periph->periph_name,
658 	    periph->unit_number);
659 	if (error != 0) {
660 		cam_periph_lock(periph);
661 		cam_periph_release_locked(periph);
662 		return (CAM_REQ_CMP_ERR);
663 	}
664 
665 	/*
666 	 * Hold a reference to the periph before we create the physical
667 	 * path alias so it can't go away.
668 	 */
669 	if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
670 		xpt_print(periph->path, "%s: lost periph during "
671 			  "registration!\n", __func__);
672 		cam_periph_lock(periph);
673 		return (CAM_REQ_CMP_ERR);
674 	}
675 
676 	cam_periph_lock(periph);
677 
678 	TASK_INIT(&softc->add_physpath_task, /*priority*/0,
679 		  pass_add_physpath, periph);
680 
681 	/*
682 	 * See if physical path information is already available.
683 	 */
684 	taskqueue_enqueue(taskqueue_thread, &softc->add_physpath_task);
685 
686 	/*
687 	 * Add an async callback so that we get notified if
688 	 * this device goes away or its physical path
689 	 * (stored in the advanced info data of the EDT) has
690 	 * changed.
691 	 */
692 	xpt_register_async(AC_LOST_DEVICE | AC_ADVINFO_CHANGED,
693 			   passasync, periph, periph->path);
694 
695 	if (bootverbose)
696 		xpt_announce_periph(periph, NULL);
697 
698 	return(CAM_REQ_CMP);
699 }
700 
701 static int
702 passopen(struct cdev *dev, int flags, int fmt, struct thread *td)
703 {
704 	struct cam_periph *periph;
705 	struct pass_softc *softc;
706 	int error;
707 
708 	periph = (struct cam_periph *)dev->si_drv1;
709 	if (cam_periph_acquire(periph) != CAM_REQ_CMP)
710 		return (ENXIO);
711 
712 	cam_periph_lock(periph);
713 
714 	softc = (struct pass_softc *)periph->softc;
715 
716 	if (softc->flags & PASS_FLAG_INVALID) {
717 		cam_periph_release_locked(periph);
718 		cam_periph_unlock(periph);
719 		return(ENXIO);
720 	}
721 
722 	/*
723 	 * Don't allow access when we're running at a high securelevel.
724 	 */
725 	error = securelevel_gt(td->td_ucred, 1);
726 	if (error) {
727 		cam_periph_release_locked(periph);
728 		cam_periph_unlock(periph);
729 		return(error);
730 	}
731 
732 	/*
733 	 * Only allow read-write access.
734 	 */
735 	if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0)) {
736 		cam_periph_release_locked(periph);
737 		cam_periph_unlock(periph);
738 		return(EPERM);
739 	}
740 
741 	/*
742 	 * We don't allow nonblocking access.
743 	 */
744 	if ((flags & O_NONBLOCK) != 0) {
745 		xpt_print(periph->path, "can't do nonblocking access\n");
746 		cam_periph_release_locked(periph);
747 		cam_periph_unlock(periph);
748 		return(EINVAL);
749 	}
750 
751 	softc->open_count++;
752 
753 	cam_periph_unlock(periph);
754 
755 	return (error);
756 }
757 
758 static int
759 passclose(struct cdev *dev, int flag, int fmt, struct thread *td)
760 {
761 	struct 	cam_periph *periph;
762 	struct  pass_softc *softc;
763 	struct mtx *mtx;
764 
765 	periph = (struct cam_periph *)dev->si_drv1;
766 	mtx = cam_periph_mtx(periph);
767 	mtx_lock(mtx);
768 
769 	softc = periph->softc;
770 	softc->open_count--;
771 
772 	if (softc->open_count == 0) {
773 		struct pass_io_req *io_req, *io_req2;
774 
775 		TAILQ_FOREACH_SAFE(io_req, &softc->done_queue, links, io_req2) {
776 			TAILQ_REMOVE(&softc->done_queue, io_req, links);
777 			passiocleanup(softc, io_req);
778 			uma_zfree(softc->pass_zone, io_req);
779 		}
780 
781 		TAILQ_FOREACH_SAFE(io_req, &softc->incoming_queue, links,
782 				   io_req2) {
783 			TAILQ_REMOVE(&softc->incoming_queue, io_req, links);
784 			passiocleanup(softc, io_req);
785 			uma_zfree(softc->pass_zone, io_req);
786 		}
787 
788 		/*
789 		 * If there are any active I/Os, we need to forcibly acquire a
790 		 * reference to the peripheral so that we don't go away
791 		 * before they complete.  We'll release the reference when
792 		 * the abandoned queue is empty.
793 		 */
794 		io_req = TAILQ_FIRST(&softc->active_queue);
795 		if ((io_req != NULL)
796 		 && (softc->flags & PASS_FLAG_ABANDONED_REF_SET) == 0) {
797 			cam_periph_doacquire(periph);
798 			softc->flags |= PASS_FLAG_ABANDONED_REF_SET;
799 		}
800 
801 		/*
802 		 * Since the I/O in the active queue is not under our
803 		 * control, just set a flag so that we can clean it up when
804 		 * it completes and put it on the abandoned queue.  This
805 		 * will prevent our sending spurious completions in the
806 		 * event that the device is opened again before these I/Os
807 		 * complete.
808 		 */
809 		TAILQ_FOREACH_SAFE(io_req, &softc->active_queue, links,
810 				   io_req2) {
811 			TAILQ_REMOVE(&softc->active_queue, io_req, links);
812 			io_req->flags |= PASS_IO_ABANDONED;
813 			TAILQ_INSERT_TAIL(&softc->abandoned_queue, io_req,
814 					  links);
815 		}
816 	}
817 
818 	cam_periph_release_locked(periph);
819 
820 	/*
821 	 * We reference the lock directly here, instead of using
822 	 * cam_periph_unlock().  The reason is that the call to
823 	 * cam_periph_release_locked() above could result in the periph
824 	 * getting freed.  If that is the case, dereferencing the periph
825 	 * with a cam_periph_unlock() call would cause a page fault.
826 	 *
827 	 * cam_periph_release() avoids this problem using the same method,
828 	 * but we're manually acquiring and dropping the lock here to
829 	 * protect the open count and avoid another lock acquisition and
830 	 * release.
831 	 */
832 	mtx_unlock(mtx);
833 
834 	return (0);
835 }
836 
837 
838 static void
839 passstart(struct cam_periph *periph, union ccb *start_ccb)
840 {
841 	struct pass_softc *softc;
842 
843 	softc = (struct pass_softc *)periph->softc;
844 
845 	switch (softc->state) {
846 	case PASS_STATE_NORMAL: {
847 		struct pass_io_req *io_req;
848 
849 		/*
850 		 * Check for any queued I/O requests that require an
851 		 * allocated slot.
852 		 */
853 		io_req = TAILQ_FIRST(&softc->incoming_queue);
854 		if (io_req == NULL) {
855 			xpt_release_ccb(start_ccb);
856 			break;
857 		}
858 		TAILQ_REMOVE(&softc->incoming_queue, io_req, links);
859 		TAILQ_INSERT_TAIL(&softc->active_queue, io_req, links);
860 		/*
861 		 * Merge the user's CCB into the allocated CCB.
862 		 */
863 		xpt_merge_ccb(start_ccb, &io_req->ccb);
864 		start_ccb->ccb_h.ccb_type = PASS_CCB_QUEUED_IO;
865 		start_ccb->ccb_h.ccb_ioreq = io_req;
866 		start_ccb->ccb_h.cbfcnp = passdone;
867 		io_req->alloced_ccb = start_ccb;
868 		binuptime(&io_req->start_time);
869 		devstat_start_transaction(softc->device_stats,
870 					  &io_req->start_time);
871 
872 		xpt_action(start_ccb);
873 
874 		/*
875 		 * If we have any more I/O waiting, schedule ourselves again.
876 		 */
877 		if (!TAILQ_EMPTY(&softc->incoming_queue))
878 			xpt_schedule(periph, CAM_PRIORITY_NORMAL);
879 		break;
880 	}
881 	default:
882 		break;
883 	}
884 }
885 
886 static void
887 passdone(struct cam_periph *periph, union ccb *done_ccb)
888 {
889 	struct pass_softc *softc;
890 	struct ccb_scsiio *csio;
891 
892 	softc = (struct pass_softc *)periph->softc;
893 
894 	cam_periph_assert(periph, MA_OWNED);
895 
896 	csio = &done_ccb->csio;
897 	switch (csio->ccb_h.ccb_type) {
898 	case PASS_CCB_QUEUED_IO: {
899 		struct pass_io_req *io_req;
900 
901 		io_req = done_ccb->ccb_h.ccb_ioreq;
902 #if 0
903 		xpt_print(periph->path, "%s: called for user CCB %p\n",
904 			  __func__, io_req->user_ccb_ptr);
905 #endif
906 		if (((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
907 		 && (done_ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER)
908 		 && ((io_req->flags & PASS_IO_ABANDONED) == 0)) {
909 			int error;
910 
911 			error = passerror(done_ccb, CAM_RETRY_SELTO,
912 					  SF_RETRY_UA | SF_NO_PRINT);
913 
914 			if (error == ERESTART) {
915 				/*
916 				 * A retry was scheduled, so
917  				 * just return.
918 				 */
919 				return;
920 			}
921 		}
922 
923 		/*
924 		 * Copy the allocated CCB contents back to the malloced CCB
925 		 * so we can give status back to the user when he requests it.
926 		 */
927 		bcopy(done_ccb, &io_req->ccb, sizeof(*done_ccb));
928 
929 		/*
930 		 * Log data/transaction completion with devstat(9).
931 		 */
932 		switch (done_ccb->ccb_h.func_code) {
933 		case XPT_SCSI_IO:
934 			devstat_end_transaction(softc->device_stats,
935 			    done_ccb->csio.dxfer_len - done_ccb->csio.resid,
936 			    done_ccb->csio.tag_action & 0x3,
937 			    ((done_ccb->ccb_h.flags & CAM_DIR_MASK) ==
938 			    CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
939 			    (done_ccb->ccb_h.flags & CAM_DIR_OUT) ?
940 			    DEVSTAT_WRITE : DEVSTAT_READ, NULL,
941 			    &io_req->start_time);
942 			break;
943 		case XPT_ATA_IO:
944 			devstat_end_transaction(softc->device_stats,
945 			    done_ccb->ataio.dxfer_len - done_ccb->ataio.resid,
946 			    0, /* Not used in ATA */
947 			    ((done_ccb->ccb_h.flags & CAM_DIR_MASK) ==
948 			    CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
949 			    (done_ccb->ccb_h.flags & CAM_DIR_OUT) ?
950 			    DEVSTAT_WRITE : DEVSTAT_READ, NULL,
951 			    &io_req->start_time);
952 			break;
953 		case XPT_SMP_IO:
954 			/*
955 			 * XXX KDM this isn't quite right, but there isn't
956 			 * currently an easy way to represent a bidirectional
957 			 * transfer in devstat.  The only way to do it
958 			 * and have the byte counts come out right would
959 			 * mean that we would have to record two
960 			 * transactions, one for the request and one for the
961 			 * response.  For now, so that we report something,
962 			 * just treat the entire thing as a read.
963 			 */
964 			devstat_end_transaction(softc->device_stats,
965 			    done_ccb->smpio.smp_request_len +
966 			    done_ccb->smpio.smp_response_len,
967 			    DEVSTAT_TAG_SIMPLE, DEVSTAT_READ, NULL,
968 			    &io_req->start_time);
969 			break;
970 		default:
971 			devstat_end_transaction(softc->device_stats, 0,
972 			    DEVSTAT_TAG_NONE, DEVSTAT_NO_DATA, NULL,
973 			    &io_req->start_time);
974 			break;
975 		}
976 
977 		/*
978 		 * In the normal case, take the completed I/O off of the
979 		 * active queue and put it on the done queue.  Notitfy the
980 		 * user that we have a completed I/O.
981 		 */
982 		if ((io_req->flags & PASS_IO_ABANDONED) == 0) {
983 			TAILQ_REMOVE(&softc->active_queue, io_req, links);
984 			TAILQ_INSERT_TAIL(&softc->done_queue, io_req, links);
985 			selwakeuppri(&softc->read_select, PRIBIO);
986 			KNOTE_LOCKED(&softc->read_select.si_note, 0);
987 		} else {
988 			/*
989 			 * In the case of an abandoned I/O (final close
990 			 * without fetching the I/O), take it off of the
991 			 * abandoned queue and free it.
992 			 */
993 			TAILQ_REMOVE(&softc->abandoned_queue, io_req, links);
994 			passiocleanup(softc, io_req);
995 			uma_zfree(softc->pass_zone, io_req);
996 
997 			/*
998 			 * Release the done_ccb here, since we may wind up
999 			 * freeing the peripheral when we decrement the
1000 			 * reference count below.
1001 			 */
1002 			xpt_release_ccb(done_ccb);
1003 
1004 			/*
1005 			 * If the abandoned queue is empty, we can release
1006 			 * our reference to the periph since we won't have
1007 			 * any more completions coming.
1008 			 */
1009 			if ((TAILQ_EMPTY(&softc->abandoned_queue))
1010 			 && (softc->flags & PASS_FLAG_ABANDONED_REF_SET)) {
1011 				softc->flags &= ~PASS_FLAG_ABANDONED_REF_SET;
1012 				cam_periph_release_locked(periph);
1013 			}
1014 
1015 			/*
1016 			 * We have already released the CCB, so we can
1017 			 * return.
1018 			 */
1019 			return;
1020 		}
1021 		break;
1022 	}
1023 	}
1024 	xpt_release_ccb(done_ccb);
1025 }
1026 
1027 static int
1028 passcreatezone(struct cam_periph *periph)
1029 {
1030 	struct pass_softc *softc;
1031 	int error;
1032 
1033 	error = 0;
1034 	softc = (struct pass_softc *)periph->softc;
1035 
1036 	cam_periph_assert(periph, MA_OWNED);
1037 	KASSERT(((softc->flags & PASS_FLAG_ZONE_VALID) == 0),
1038 		("%s called when the pass(4) zone is valid!\n", __func__));
1039 	KASSERT((softc->pass_zone == NULL),
1040 		("%s called when the pass(4) zone is allocated!\n", __func__));
1041 
1042 	if ((softc->flags & PASS_FLAG_ZONE_INPROG) == 0) {
1043 
1044 		/*
1045 		 * We're the first context through, so we need to create
1046 		 * the pass(4) UMA zone for I/O requests.
1047 		 */
1048 		softc->flags |= PASS_FLAG_ZONE_INPROG;
1049 
1050 		/*
1051 		 * uma_zcreate() does a blocking (M_WAITOK) allocation,
1052 		 * so we cannot hold a mutex while we call it.
1053 		 */
1054 		cam_periph_unlock(periph);
1055 
1056 		softc->pass_zone = uma_zcreate(softc->zone_name,
1057 		    sizeof(struct pass_io_req), NULL, NULL, NULL, NULL,
1058 		    /*align*/ 0, /*flags*/ 0);
1059 
1060 		softc->pass_io_zone = uma_zcreate(softc->io_zone_name,
1061 		    softc->io_zone_size, NULL, NULL, NULL, NULL,
1062 		    /*align*/ 0, /*flags*/ 0);
1063 
1064 		cam_periph_lock(periph);
1065 
1066 		if ((softc->pass_zone == NULL)
1067 		 || (softc->pass_io_zone == NULL)) {
1068 			if (softc->pass_zone == NULL)
1069 				xpt_print(periph->path, "unable to allocate "
1070 				    "IO Req UMA zone\n");
1071 			else
1072 				xpt_print(periph->path, "unable to allocate "
1073 				    "IO UMA zone\n");
1074 			softc->flags &= ~PASS_FLAG_ZONE_INPROG;
1075 			goto bailout;
1076 		}
1077 
1078 		/*
1079 		 * Set the flags appropriately and notify any other waiters.
1080 		 */
1081 		softc->flags &= PASS_FLAG_ZONE_INPROG;
1082 		softc->flags |= PASS_FLAG_ZONE_VALID;
1083 		wakeup(&softc->pass_zone);
1084 	} else {
1085 		/*
1086 		 * In this case, the UMA zone has not yet been created, but
1087 		 * another context is in the process of creating it.  We
1088 		 * need to sleep until the creation is either done or has
1089 		 * failed.
1090 		 */
1091 		while ((softc->flags & PASS_FLAG_ZONE_INPROG)
1092 		    && ((softc->flags & PASS_FLAG_ZONE_VALID) == 0)) {
1093 			error = msleep(&softc->pass_zone,
1094 				       cam_periph_mtx(periph), PRIBIO,
1095 				       "paszon", 0);
1096 			if (error != 0)
1097 				goto bailout;
1098 		}
1099 		/*
1100 		 * If the zone creation failed, no luck for the user.
1101 		 */
1102 		if ((softc->flags & PASS_FLAG_ZONE_VALID) == 0){
1103 			error = ENOMEM;
1104 			goto bailout;
1105 		}
1106 	}
1107 bailout:
1108 	return (error);
1109 }
1110 
1111 static void
1112 passiocleanup(struct pass_softc *softc, struct pass_io_req *io_req)
1113 {
1114 	union ccb *ccb;
1115 	u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
1116 	int i, numbufs;
1117 
1118 	ccb = &io_req->ccb;
1119 
1120 	switch (ccb->ccb_h.func_code) {
1121 	case XPT_DEV_MATCH:
1122 		numbufs = min(io_req->num_bufs, 2);
1123 
1124 		if (numbufs == 1) {
1125 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
1126 		} else {
1127 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
1128 			data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
1129 		}
1130 		break;
1131 	case XPT_SCSI_IO:
1132 	case XPT_CONT_TARGET_IO:
1133 		data_ptrs[0] = &ccb->csio.data_ptr;
1134 		numbufs = min(io_req->num_bufs, 1);
1135 		break;
1136 	case XPT_ATA_IO:
1137 		data_ptrs[0] = &ccb->ataio.data_ptr;
1138 		numbufs = min(io_req->num_bufs, 1);
1139 		break;
1140 	case XPT_SMP_IO:
1141 		numbufs = min(io_req->num_bufs, 2);
1142 		data_ptrs[0] = &ccb->smpio.smp_request;
1143 		data_ptrs[1] = &ccb->smpio.smp_response;
1144 		break;
1145 	case XPT_DEV_ADVINFO:
1146 		numbufs = min(io_req->num_bufs, 1);
1147 		data_ptrs[0] = (uint8_t **)&ccb->cdai.buf;
1148 		break;
1149 	case XPT_NVME_IO:
1150 	case XPT_NVME_ADMIN:
1151 		data_ptrs[0] = &ccb->nvmeio.data_ptr;
1152 		numbufs = min(io_req->num_bufs, 1);
1153 		break;
1154 	default:
1155 		/* allow ourselves to be swapped once again */
1156 		return;
1157 		break; /* NOTREACHED */
1158 	}
1159 
1160 	if (io_req->flags & PASS_IO_USER_SEG_MALLOC) {
1161 		free(io_req->user_segptr, M_SCSIPASS);
1162 		io_req->user_segptr = NULL;
1163 	}
1164 
1165 	/*
1166 	 * We only want to free memory we malloced.
1167 	 */
1168 	if (io_req->data_flags == CAM_DATA_VADDR) {
1169 		for (i = 0; i < io_req->num_bufs; i++) {
1170 			if (io_req->kern_bufs[i] == NULL)
1171 				continue;
1172 
1173 			free(io_req->kern_bufs[i], M_SCSIPASS);
1174 			io_req->kern_bufs[i] = NULL;
1175 		}
1176 	} else if (io_req->data_flags == CAM_DATA_SG) {
1177 		for (i = 0; i < io_req->num_kern_segs; i++) {
1178 			if ((uint8_t *)(uintptr_t)
1179 			    io_req->kern_segptr[i].ds_addr == NULL)
1180 				continue;
1181 
1182 			uma_zfree(softc->pass_io_zone, (uint8_t *)(uintptr_t)
1183 			    io_req->kern_segptr[i].ds_addr);
1184 			io_req->kern_segptr[i].ds_addr = 0;
1185 		}
1186 	}
1187 
1188 	if (io_req->flags & PASS_IO_KERN_SEG_MALLOC) {
1189 		free(io_req->kern_segptr, M_SCSIPASS);
1190 		io_req->kern_segptr = NULL;
1191 	}
1192 
1193 	if (io_req->data_flags != CAM_DATA_PADDR) {
1194 		for (i = 0; i < numbufs; i++) {
1195 			/*
1196 			 * Restore the user's buffer pointers to their
1197 			 * previous values.
1198 			 */
1199 			if (io_req->user_bufs[i] != NULL)
1200 				*data_ptrs[i] = io_req->user_bufs[i];
1201 		}
1202 	}
1203 
1204 }
1205 
1206 static int
1207 passcopysglist(struct cam_periph *periph, struct pass_io_req *io_req,
1208 	       ccb_flags direction)
1209 {
1210 	bus_size_t kern_watermark, user_watermark, len_copied, len_to_copy;
1211 	bus_dma_segment_t *user_sglist, *kern_sglist;
1212 	int i, j, error;
1213 
1214 	error = 0;
1215 	kern_watermark = 0;
1216 	user_watermark = 0;
1217 	len_to_copy = 0;
1218 	len_copied = 0;
1219 	user_sglist = io_req->user_segptr;
1220 	kern_sglist = io_req->kern_segptr;
1221 
1222 	for (i = 0, j = 0; i < io_req->num_user_segs &&
1223 	     j < io_req->num_kern_segs;) {
1224 		uint8_t *user_ptr, *kern_ptr;
1225 
1226 		len_to_copy = min(user_sglist[i].ds_len -user_watermark,
1227 		    kern_sglist[j].ds_len - kern_watermark);
1228 
1229 		user_ptr = (uint8_t *)(uintptr_t)user_sglist[i].ds_addr;
1230 		user_ptr = user_ptr + user_watermark;
1231 		kern_ptr = (uint8_t *)(uintptr_t)kern_sglist[j].ds_addr;
1232 		kern_ptr = kern_ptr + kern_watermark;
1233 
1234 		user_watermark += len_to_copy;
1235 		kern_watermark += len_to_copy;
1236 
1237 		if (!useracc(user_ptr, len_to_copy,
1238 		    (direction == CAM_DIR_IN) ? VM_PROT_WRITE : VM_PROT_READ)) {
1239 			xpt_print(periph->path, "%s: unable to access user "
1240 				  "S/G list element %p len %zu\n", __func__,
1241 				  user_ptr, len_to_copy);
1242 			error = EFAULT;
1243 			goto bailout;
1244 		}
1245 
1246 		if (direction == CAM_DIR_IN) {
1247 			error = copyout(kern_ptr, user_ptr, len_to_copy);
1248 			if (error != 0) {
1249 				xpt_print(periph->path, "%s: copyout of %u "
1250 					  "bytes from %p to %p failed with "
1251 					  "error %d\n", __func__, len_to_copy,
1252 					  kern_ptr, user_ptr, error);
1253 				goto bailout;
1254 			}
1255 		} else {
1256 			error = copyin(user_ptr, kern_ptr, len_to_copy);
1257 			if (error != 0) {
1258 				xpt_print(periph->path, "%s: copyin of %u "
1259 					  "bytes from %p to %p failed with "
1260 					  "error %d\n", __func__, len_to_copy,
1261 					  user_ptr, kern_ptr, error);
1262 				goto bailout;
1263 			}
1264 		}
1265 
1266 		len_copied += len_to_copy;
1267 
1268 		if (user_sglist[i].ds_len == user_watermark) {
1269 			i++;
1270 			user_watermark = 0;
1271 		}
1272 
1273 		if (kern_sglist[j].ds_len == kern_watermark) {
1274 			j++;
1275 			kern_watermark = 0;
1276 		}
1277 	}
1278 
1279 bailout:
1280 
1281 	return (error);
1282 }
1283 
1284 static int
1285 passmemsetup(struct cam_periph *periph, struct pass_io_req *io_req)
1286 {
1287 	union ccb *ccb;
1288 	struct pass_softc *softc;
1289 	int numbufs, i;
1290 	uint8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
1291 	uint32_t lengths[CAM_PERIPH_MAXMAPS];
1292 	uint32_t dirs[CAM_PERIPH_MAXMAPS];
1293 	uint32_t num_segs;
1294 	uint16_t *seg_cnt_ptr;
1295 	size_t maxmap;
1296 	int error;
1297 
1298 	cam_periph_assert(periph, MA_NOTOWNED);
1299 
1300 	softc = periph->softc;
1301 
1302 	error = 0;
1303 	ccb = &io_req->ccb;
1304 	maxmap = 0;
1305 	num_segs = 0;
1306 	seg_cnt_ptr = NULL;
1307 
1308 	switch(ccb->ccb_h.func_code) {
1309 	case XPT_DEV_MATCH:
1310 		if (ccb->cdm.match_buf_len == 0) {
1311 			printf("%s: invalid match buffer length 0\n", __func__);
1312 			return(EINVAL);
1313 		}
1314 		if (ccb->cdm.pattern_buf_len > 0) {
1315 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
1316 			lengths[0] = ccb->cdm.pattern_buf_len;
1317 			dirs[0] = CAM_DIR_OUT;
1318 			data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
1319 			lengths[1] = ccb->cdm.match_buf_len;
1320 			dirs[1] = CAM_DIR_IN;
1321 			numbufs = 2;
1322 		} else {
1323 			data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
1324 			lengths[0] = ccb->cdm.match_buf_len;
1325 			dirs[0] = CAM_DIR_IN;
1326 			numbufs = 1;
1327 		}
1328 		io_req->data_flags = CAM_DATA_VADDR;
1329 		break;
1330 	case XPT_SCSI_IO:
1331 	case XPT_CONT_TARGET_IO:
1332 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
1333 			return(0);
1334 
1335 		/*
1336 		 * The user shouldn't be able to supply a bio.
1337 		 */
1338 		if ((ccb->ccb_h.flags & CAM_DATA_MASK) == CAM_DATA_BIO)
1339 			return (EINVAL);
1340 
1341 		io_req->data_flags = ccb->ccb_h.flags & CAM_DATA_MASK;
1342 
1343 		data_ptrs[0] = &ccb->csio.data_ptr;
1344 		lengths[0] = ccb->csio.dxfer_len;
1345 		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
1346 		num_segs = ccb->csio.sglist_cnt;
1347 		seg_cnt_ptr = &ccb->csio.sglist_cnt;
1348 		numbufs = 1;
1349 		maxmap = softc->maxio;
1350 		break;
1351 	case XPT_ATA_IO:
1352 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
1353 			return(0);
1354 
1355 		/*
1356 		 * We only support a single virtual address for ATA I/O.
1357 		 */
1358 		if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
1359 			return (EINVAL);
1360 
1361 		io_req->data_flags = CAM_DATA_VADDR;
1362 
1363 		data_ptrs[0] = &ccb->ataio.data_ptr;
1364 		lengths[0] = ccb->ataio.dxfer_len;
1365 		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
1366 		numbufs = 1;
1367 		maxmap = softc->maxio;
1368 		break;
1369 	case XPT_SMP_IO:
1370 		io_req->data_flags = CAM_DATA_VADDR;
1371 
1372 		data_ptrs[0] = &ccb->smpio.smp_request;
1373 		lengths[0] = ccb->smpio.smp_request_len;
1374 		dirs[0] = CAM_DIR_OUT;
1375 		data_ptrs[1] = &ccb->smpio.smp_response;
1376 		lengths[1] = ccb->smpio.smp_response_len;
1377 		dirs[1] = CAM_DIR_IN;
1378 		numbufs = 2;
1379 		maxmap = softc->maxio;
1380 		break;
1381 	case XPT_DEV_ADVINFO:
1382 		if (ccb->cdai.bufsiz == 0)
1383 			return (0);
1384 
1385 		io_req->data_flags = CAM_DATA_VADDR;
1386 
1387 		data_ptrs[0] = (uint8_t **)&ccb->cdai.buf;
1388 		lengths[0] = ccb->cdai.bufsiz;
1389 		dirs[0] = CAM_DIR_IN;
1390 		numbufs = 1;
1391 		break;
1392 	case XPT_NVME_ADMIN:
1393 	case XPT_NVME_IO:
1394 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
1395 			return (0);
1396 
1397 		io_req->data_flags = ccb->ccb_h.flags & CAM_DATA_MASK;
1398 
1399 		data_ptrs[0] = &ccb->nvmeio.data_ptr;
1400 		lengths[0] = ccb->nvmeio.dxfer_len;
1401 		dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK;
1402 		num_segs = ccb->nvmeio.sglist_cnt;
1403 		seg_cnt_ptr = &ccb->nvmeio.sglist_cnt;
1404 		numbufs = 1;
1405 		maxmap = softc->maxio;
1406 		break;
1407 	default:
1408 		return(EINVAL);
1409 		break; /* NOTREACHED */
1410 	}
1411 
1412 	io_req->num_bufs = numbufs;
1413 
1414 	/*
1415 	 * If there is a maximum, check to make sure that the user's
1416 	 * request fits within the limit.  In general, we should only have
1417 	 * a maximum length for requests that go to hardware.  Otherwise it
1418 	 * is whatever we're able to malloc.
1419 	 */
1420 	for (i = 0; i < numbufs; i++) {
1421 		io_req->user_bufs[i] = *data_ptrs[i];
1422 		io_req->dirs[i] = dirs[i];
1423 		io_req->lengths[i] = lengths[i];
1424 
1425 		if (maxmap == 0)
1426 			continue;
1427 
1428 		if (lengths[i] <= maxmap)
1429 			continue;
1430 
1431 		xpt_print(periph->path, "%s: data length %u > max allowed %u "
1432 			  "bytes\n", __func__, lengths[i], maxmap);
1433 		error = EINVAL;
1434 		goto bailout;
1435 	}
1436 
1437 	switch (io_req->data_flags) {
1438 	case CAM_DATA_VADDR:
1439 		/* Map or copy the buffer into kernel address space */
1440 		for (i = 0; i < numbufs; i++) {
1441 			uint8_t *tmp_buf;
1442 
1443 			/*
1444 			 * If for some reason no length is specified, we
1445 			 * don't need to allocate anything.
1446 			 */
1447 			if (io_req->lengths[i] == 0)
1448 				continue;
1449 
1450 			/*
1451 			 * Make sure that the user's buffer is accessible
1452 			 * to that process.
1453 			 */
1454 			if (!useracc(io_req->user_bufs[i], io_req->lengths[i],
1455 			    (io_req->dirs[i] == CAM_DIR_IN) ? VM_PROT_WRITE :
1456 			     VM_PROT_READ)) {
1457 				xpt_print(periph->path, "%s: user address %p "
1458 				    "length %u is not accessible\n", __func__,
1459 				    io_req->user_bufs[i], io_req->lengths[i]);
1460 				error = EFAULT;
1461 				goto bailout;
1462 			}
1463 
1464 			tmp_buf = malloc(lengths[i], M_SCSIPASS,
1465 					 M_WAITOK | M_ZERO);
1466 			io_req->kern_bufs[i] = tmp_buf;
1467 			*data_ptrs[i] = tmp_buf;
1468 
1469 #if 0
1470 			xpt_print(periph->path, "%s: malloced %p len %u, user "
1471 				  "buffer %p, operation: %s\n", __func__,
1472 				  tmp_buf, lengths[i], io_req->user_bufs[i],
1473 				  (dirs[i] == CAM_DIR_IN) ? "read" : "write");
1474 #endif
1475 			/*
1476 			 * We only need to copy in if the user is writing.
1477 			 */
1478 			if (dirs[i] != CAM_DIR_OUT)
1479 				continue;
1480 
1481 			error = copyin(io_req->user_bufs[i],
1482 				       io_req->kern_bufs[i], lengths[i]);
1483 			if (error != 0) {
1484 				xpt_print(periph->path, "%s: copy of user "
1485 					  "buffer from %p to %p failed with "
1486 					  "error %d\n", __func__,
1487 					  io_req->user_bufs[i],
1488 					  io_req->kern_bufs[i], error);
1489 				goto bailout;
1490 			}
1491 		}
1492 		break;
1493 	case CAM_DATA_PADDR:
1494 		/* Pass down the pointer as-is */
1495 		break;
1496 	case CAM_DATA_SG: {
1497 		size_t sg_length, size_to_go, alloc_size;
1498 		uint32_t num_segs_needed;
1499 
1500 		/*
1501 		 * Copy the user S/G list in, and then copy in the
1502 		 * individual segments.
1503 		 */
1504 		/*
1505 		 * We shouldn't see this, but check just in case.
1506 		 */
1507 		if (numbufs != 1) {
1508 			xpt_print(periph->path, "%s: cannot currently handle "
1509 				  "more than one S/G list per CCB\n", __func__);
1510 			error = EINVAL;
1511 			goto bailout;
1512 		}
1513 
1514 		/*
1515 		 * We have to have at least one segment.
1516 		 */
1517 		if (num_segs == 0) {
1518 			xpt_print(periph->path, "%s: CAM_DATA_SG flag set, "
1519 				  "but sglist_cnt=0!\n", __func__);
1520 			error = EINVAL;
1521 			goto bailout;
1522 		}
1523 
1524 		/*
1525 		 * Make sure the user specified the total length and didn't
1526 		 * just leave it to us to decode the S/G list.
1527 		 */
1528 		if (lengths[0] == 0) {
1529 			xpt_print(periph->path, "%s: no dxfer_len specified, "
1530 				  "but CAM_DATA_SG flag is set!\n", __func__);
1531 			error = EINVAL;
1532 			goto bailout;
1533 		}
1534 
1535 		/*
1536 		 * We allocate buffers in io_zone_size increments for an
1537 		 * S/G list.  This will generally be MAXPHYS.
1538 		 */
1539 		if (lengths[0] <= softc->io_zone_size)
1540 			num_segs_needed = 1;
1541 		else {
1542 			num_segs_needed = lengths[0] / softc->io_zone_size;
1543 			if ((lengths[0] % softc->io_zone_size) != 0)
1544 				num_segs_needed++;
1545 		}
1546 
1547 		/* Figure out the size of the S/G list */
1548 		sg_length = num_segs * sizeof(bus_dma_segment_t);
1549 		io_req->num_user_segs = num_segs;
1550 		io_req->num_kern_segs = num_segs_needed;
1551 
1552 		/* Save the user's S/G list pointer for later restoration */
1553 		io_req->user_bufs[0] = *data_ptrs[0];
1554 
1555 		/*
1556 		 * If we have enough segments allocated by default to handle
1557 		 * the length of the user's S/G list,
1558 		 */
1559 		if (num_segs > PASS_MAX_SEGS) {
1560 			io_req->user_segptr = malloc(sizeof(bus_dma_segment_t) *
1561 			    num_segs, M_SCSIPASS, M_WAITOK | M_ZERO);
1562 			io_req->flags |= PASS_IO_USER_SEG_MALLOC;
1563 		} else
1564 			io_req->user_segptr = io_req->user_segs;
1565 
1566 		if (!useracc(*data_ptrs[0], sg_length, VM_PROT_READ)) {
1567 			xpt_print(periph->path, "%s: unable to access user "
1568 				  "S/G list at %p\n", __func__, *data_ptrs[0]);
1569 			error = EFAULT;
1570 			goto bailout;
1571 		}
1572 
1573 		error = copyin(*data_ptrs[0], io_req->user_segptr, sg_length);
1574 		if (error != 0) {
1575 			xpt_print(periph->path, "%s: copy of user S/G list "
1576 				  "from %p to %p failed with error %d\n",
1577 				  __func__, *data_ptrs[0], io_req->user_segptr,
1578 				  error);
1579 			goto bailout;
1580 		}
1581 
1582 		if (num_segs_needed > PASS_MAX_SEGS) {
1583 			io_req->kern_segptr = malloc(sizeof(bus_dma_segment_t) *
1584 			    num_segs_needed, M_SCSIPASS, M_WAITOK | M_ZERO);
1585 			io_req->flags |= PASS_IO_KERN_SEG_MALLOC;
1586 		} else {
1587 			io_req->kern_segptr = io_req->kern_segs;
1588 		}
1589 
1590 		/*
1591 		 * Allocate the kernel S/G list.
1592 		 */
1593 		for (size_to_go = lengths[0], i = 0;
1594 		     size_to_go > 0 && i < num_segs_needed;
1595 		     i++, size_to_go -= alloc_size) {
1596 			uint8_t *kern_ptr;
1597 
1598 			alloc_size = min(size_to_go, softc->io_zone_size);
1599 			kern_ptr = uma_zalloc(softc->pass_io_zone, M_WAITOK);
1600 			io_req->kern_segptr[i].ds_addr =
1601 			    (bus_addr_t)(uintptr_t)kern_ptr;
1602 			io_req->kern_segptr[i].ds_len = alloc_size;
1603 		}
1604 		if (size_to_go > 0) {
1605 			printf("%s: size_to_go = %zu, software error!\n",
1606 			       __func__, size_to_go);
1607 			error = EINVAL;
1608 			goto bailout;
1609 		}
1610 
1611 		*data_ptrs[0] = (uint8_t *)io_req->kern_segptr;
1612 		*seg_cnt_ptr = io_req->num_kern_segs;
1613 
1614 		/*
1615 		 * We only need to copy data here if the user is writing.
1616 		 */
1617 		if (dirs[0] == CAM_DIR_OUT)
1618 			error = passcopysglist(periph, io_req, dirs[0]);
1619 		break;
1620 	}
1621 	case CAM_DATA_SG_PADDR: {
1622 		size_t sg_length;
1623 
1624 		/*
1625 		 * We shouldn't see this, but check just in case.
1626 		 */
1627 		if (numbufs != 1) {
1628 			printf("%s: cannot currently handle more than one "
1629 			       "S/G list per CCB\n", __func__);
1630 			error = EINVAL;
1631 			goto bailout;
1632 		}
1633 
1634 		/*
1635 		 * We have to have at least one segment.
1636 		 */
1637 		if (num_segs == 0) {
1638 			xpt_print(periph->path, "%s: CAM_DATA_SG_PADDR flag "
1639 				  "set, but sglist_cnt=0!\n", __func__);
1640 			error = EINVAL;
1641 			goto bailout;
1642 		}
1643 
1644 		/*
1645 		 * Make sure the user specified the total length and didn't
1646 		 * just leave it to us to decode the S/G list.
1647 		 */
1648 		if (lengths[0] == 0) {
1649 			xpt_print(periph->path, "%s: no dxfer_len specified, "
1650 				  "but CAM_DATA_SG flag is set!\n", __func__);
1651 			error = EINVAL;
1652 			goto bailout;
1653 		}
1654 
1655 		/* Figure out the size of the S/G list */
1656 		sg_length = num_segs * sizeof(bus_dma_segment_t);
1657 		io_req->num_user_segs = num_segs;
1658 		io_req->num_kern_segs = io_req->num_user_segs;
1659 
1660 		/* Save the user's S/G list pointer for later restoration */
1661 		io_req->user_bufs[0] = *data_ptrs[0];
1662 
1663 		if (num_segs > PASS_MAX_SEGS) {
1664 			io_req->user_segptr = malloc(sizeof(bus_dma_segment_t) *
1665 			    num_segs, M_SCSIPASS, M_WAITOK | M_ZERO);
1666 			io_req->flags |= PASS_IO_USER_SEG_MALLOC;
1667 		} else
1668 			io_req->user_segptr = io_req->user_segs;
1669 
1670 		io_req->kern_segptr = io_req->user_segptr;
1671 
1672 		error = copyin(*data_ptrs[0], io_req->user_segptr, sg_length);
1673 		if (error != 0) {
1674 			xpt_print(periph->path, "%s: copy of user S/G list "
1675 				  "from %p to %p failed with error %d\n",
1676 				  __func__, *data_ptrs[0], io_req->user_segptr,
1677 				  error);
1678 			goto bailout;
1679 		}
1680 		break;
1681 	}
1682 	default:
1683 	case CAM_DATA_BIO:
1684 		/*
1685 		 * A user shouldn't be attaching a bio to the CCB.  It
1686 		 * isn't a user-accessible structure.
1687 		 */
1688 		error = EINVAL;
1689 		break;
1690 	}
1691 
1692 bailout:
1693 	if (error != 0)
1694 		passiocleanup(softc, io_req);
1695 
1696 	return (error);
1697 }
1698 
1699 static int
1700 passmemdone(struct cam_periph *periph, struct pass_io_req *io_req)
1701 {
1702 	struct pass_softc *softc;
1703 	union ccb *ccb;
1704 	int error;
1705 	int i;
1706 
1707 	error = 0;
1708 	softc = (struct pass_softc *)periph->softc;
1709 	ccb = &io_req->ccb;
1710 
1711 	switch (io_req->data_flags) {
1712 	case CAM_DATA_VADDR:
1713 		/*
1714 		 * Copy back to the user buffer if this was a read.
1715 		 */
1716 		for (i = 0; i < io_req->num_bufs; i++) {
1717 			if (io_req->dirs[i] != CAM_DIR_IN)
1718 				continue;
1719 
1720 			error = copyout(io_req->kern_bufs[i],
1721 			    io_req->user_bufs[i], io_req->lengths[i]);
1722 			if (error != 0) {
1723 				xpt_print(periph->path, "Unable to copy %u "
1724 					  "bytes from %p to user address %p\n",
1725 					  io_req->lengths[i],
1726 					  io_req->kern_bufs[i],
1727 					  io_req->user_bufs[i]);
1728 				goto bailout;
1729 			}
1730 
1731 		}
1732 		break;
1733 	case CAM_DATA_PADDR:
1734 		/* Do nothing.  The pointer is a physical address already */
1735 		break;
1736 	case CAM_DATA_SG:
1737 		/*
1738 		 * Copy back to the user buffer if this was a read.
1739 		 * Restore the user's S/G list buffer pointer.
1740 		 */
1741 		if (io_req->dirs[0] == CAM_DIR_IN)
1742 			error = passcopysglist(periph, io_req, io_req->dirs[0]);
1743 		break;
1744 	case CAM_DATA_SG_PADDR:
1745 		/*
1746 		 * Restore the user's S/G list buffer pointer.  No need to
1747 		 * copy.
1748 		 */
1749 		break;
1750 	default:
1751 	case CAM_DATA_BIO:
1752 		error = EINVAL;
1753 		break;
1754 	}
1755 
1756 bailout:
1757 	/*
1758 	 * Reset the user's pointers to their original values and free
1759 	 * allocated memory.
1760 	 */
1761 	passiocleanup(softc, io_req);
1762 
1763 	return (error);
1764 }
1765 
1766 static int
1767 passioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
1768 {
1769 	int error;
1770 
1771 	if ((error = passdoioctl(dev, cmd, addr, flag, td)) == ENOTTY) {
1772 		error = cam_compat_ioctl(dev, cmd, addr, flag, td, passdoioctl);
1773 	}
1774 	return (error);
1775 }
1776 
1777 static int
1778 passdoioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
1779 {
1780 	struct	cam_periph *periph;
1781 	struct	pass_softc *softc;
1782 	int	error;
1783 	uint32_t priority;
1784 
1785 	periph = (struct cam_periph *)dev->si_drv1;
1786 	cam_periph_lock(periph);
1787 	softc = (struct pass_softc *)periph->softc;
1788 
1789 	error = 0;
1790 
1791 	switch (cmd) {
1792 
1793 	case CAMIOCOMMAND:
1794 	{
1795 		union ccb *inccb;
1796 		union ccb *ccb;
1797 		int ccb_malloced;
1798 
1799 		inccb = (union ccb *)addr;
1800 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
1801 		if (inccb->ccb_h.func_code == XPT_SCSI_IO)
1802 			inccb->csio.bio = NULL;
1803 #endif
1804 
1805 		if (inccb->ccb_h.flags & CAM_UNLOCKED) {
1806 			error = EINVAL;
1807 			break;
1808 		}
1809 
1810 		/*
1811 		 * Some CCB types, like scan bus and scan lun can only go
1812 		 * through the transport layer device.
1813 		 */
1814 		if (inccb->ccb_h.func_code & XPT_FC_XPT_ONLY) {
1815 			xpt_print(periph->path, "CCB function code %#x is "
1816 			    "restricted to the XPT device\n",
1817 			    inccb->ccb_h.func_code);
1818 			error = ENODEV;
1819 			break;
1820 		}
1821 
1822 		/* Compatibility for RL/priority-unaware code. */
1823 		priority = inccb->ccb_h.pinfo.priority;
1824 		if (priority <= CAM_PRIORITY_OOB)
1825 		    priority += CAM_PRIORITY_OOB + 1;
1826 
1827 		/*
1828 		 * Non-immediate CCBs need a CCB from the per-device pool
1829 		 * of CCBs, which is scheduled by the transport layer.
1830 		 * Immediate CCBs and user-supplied CCBs should just be
1831 		 * malloced.
1832 		 */
1833 		if ((inccb->ccb_h.func_code & XPT_FC_QUEUED)
1834 		 && ((inccb->ccb_h.func_code & XPT_FC_USER_CCB) == 0)) {
1835 			ccb = cam_periph_getccb(periph, priority);
1836 			ccb_malloced = 0;
1837 		} else {
1838 			ccb = xpt_alloc_ccb_nowait();
1839 
1840 			if (ccb != NULL)
1841 				xpt_setup_ccb(&ccb->ccb_h, periph->path,
1842 					      priority);
1843 			ccb_malloced = 1;
1844 		}
1845 
1846 		if (ccb == NULL) {
1847 			xpt_print(periph->path, "unable to allocate CCB\n");
1848 			error = ENOMEM;
1849 			break;
1850 		}
1851 
1852 		error = passsendccb(periph, ccb, inccb);
1853 
1854 		if (ccb_malloced)
1855 			xpt_free_ccb(ccb);
1856 		else
1857 			xpt_release_ccb(ccb);
1858 
1859 		break;
1860 	}
1861 	case CAMIOQUEUE:
1862 	{
1863 		struct pass_io_req *io_req;
1864 		union ccb **user_ccb, *ccb;
1865 		xpt_opcode fc;
1866 
1867 		if ((softc->flags & PASS_FLAG_ZONE_VALID) == 0) {
1868 			error = passcreatezone(periph);
1869 			if (error != 0)
1870 				goto bailout;
1871 		}
1872 
1873 		/*
1874 		 * We're going to do a blocking allocation for this I/O
1875 		 * request, so we have to drop the lock.
1876 		 */
1877 		cam_periph_unlock(periph);
1878 
1879 		io_req = uma_zalloc(softc->pass_zone, M_WAITOK | M_ZERO);
1880 		ccb = &io_req->ccb;
1881 		user_ccb = (union ccb **)addr;
1882 
1883 		/*
1884 		 * Unlike the CAMIOCOMMAND ioctl above, we only have a
1885 		 * pointer to the user's CCB, so we have to copy the whole
1886 		 * thing in to a buffer we have allocated (above) instead
1887 		 * of allowing the ioctl code to malloc a buffer and copy
1888 		 * it in.
1889 		 *
1890 		 * This is an advantage for this asynchronous interface,
1891 		 * since we don't want the memory to get freed while the
1892 		 * CCB is outstanding.
1893 		 */
1894 #if 0
1895 		xpt_print(periph->path, "Copying user CCB %p to "
1896 			  "kernel address %p\n", *user_ccb, ccb);
1897 #endif
1898 		error = copyin(*user_ccb, ccb, sizeof(*ccb));
1899 		if (error != 0) {
1900 			xpt_print(periph->path, "Copy of user CCB %p to "
1901 				  "kernel address %p failed with error %d\n",
1902 				  *user_ccb, ccb, error);
1903 			goto camioqueue_error;
1904 		}
1905 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
1906 		if (ccb->ccb_h.func_code == XPT_SCSI_IO)
1907 			ccb->csio.bio = NULL;
1908 #endif
1909 
1910 		if (ccb->ccb_h.flags & CAM_UNLOCKED) {
1911 			error = EINVAL;
1912 			goto camioqueue_error;
1913 		}
1914 
1915 		if (ccb->ccb_h.flags & CAM_CDB_POINTER) {
1916 			if (ccb->csio.cdb_len > IOCDBLEN) {
1917 				error = EINVAL;
1918 				goto camioqueue_error;
1919 			}
1920 			error = copyin(ccb->csio.cdb_io.cdb_ptr,
1921 			    ccb->csio.cdb_io.cdb_bytes, ccb->csio.cdb_len);
1922 			if (error != 0)
1923 				goto camioqueue_error;
1924 			ccb->ccb_h.flags &= ~CAM_CDB_POINTER;
1925 		}
1926 
1927 		/*
1928 		 * Some CCB types, like scan bus and scan lun can only go
1929 		 * through the transport layer device.
1930 		 */
1931 		if (ccb->ccb_h.func_code & XPT_FC_XPT_ONLY) {
1932 			xpt_print(periph->path, "CCB function code %#x is "
1933 			    "restricted to the XPT device\n",
1934 			    ccb->ccb_h.func_code);
1935 			error = ENODEV;
1936 			goto camioqueue_error;
1937 		}
1938 
1939 		/*
1940 		 * Save the user's CCB pointer as well as his linked list
1941 		 * pointers and peripheral private area so that we can
1942 		 * restore these later.
1943 		 */
1944 		io_req->user_ccb_ptr = *user_ccb;
1945 		io_req->user_periph_links = ccb->ccb_h.periph_links;
1946 		io_req->user_periph_priv = ccb->ccb_h.periph_priv;
1947 
1948 		/*
1949 		 * Now that we've saved the user's values, we can set our
1950 		 * own peripheral private entry.
1951 		 */
1952 		ccb->ccb_h.ccb_ioreq = io_req;
1953 
1954 		/* Compatibility for RL/priority-unaware code. */
1955 		priority = ccb->ccb_h.pinfo.priority;
1956 		if (priority <= CAM_PRIORITY_OOB)
1957 		    priority += CAM_PRIORITY_OOB + 1;
1958 
1959 		/*
1960 		 * Setup fields in the CCB like the path and the priority.
1961 		 * The path in particular cannot be done in userland, since
1962 		 * it is a pointer to a kernel data structure.
1963 		 */
1964 		xpt_setup_ccb_flags(&ccb->ccb_h, periph->path, priority,
1965 				    ccb->ccb_h.flags);
1966 
1967 		/*
1968 		 * Setup our done routine.  There is no way for the user to
1969 		 * have a valid pointer here.
1970 		 */
1971 		ccb->ccb_h.cbfcnp = passdone;
1972 
1973 		fc = ccb->ccb_h.func_code;
1974 		/*
1975 		 * If this function code has memory that can be mapped in
1976 		 * or out, we need to call passmemsetup().
1977 		 */
1978 		if ((fc == XPT_SCSI_IO) || (fc == XPT_ATA_IO)
1979 		 || (fc == XPT_SMP_IO) || (fc == XPT_DEV_MATCH)
1980 		 || (fc == XPT_DEV_ADVINFO)
1981 		 || (fc == XPT_NVME_ADMIN) || (fc == XPT_NVME_IO)) {
1982 			error = passmemsetup(periph, io_req);
1983 			if (error != 0)
1984 				goto camioqueue_error;
1985 		} else
1986 			io_req->mapinfo.num_bufs_used = 0;
1987 
1988 		cam_periph_lock(periph);
1989 
1990 		/*
1991 		 * Everything goes on the incoming queue initially.
1992 		 */
1993 		TAILQ_INSERT_TAIL(&softc->incoming_queue, io_req, links);
1994 
1995 		/*
1996 		 * If the CCB is queued, and is not a user CCB, then
1997 		 * we need to allocate a slot for it.  Call xpt_schedule()
1998 		 * so that our start routine will get called when a CCB is
1999 		 * available.
2000 		 */
2001 		if ((fc & XPT_FC_QUEUED)
2002 		 && ((fc & XPT_FC_USER_CCB) == 0)) {
2003 			xpt_schedule(periph, priority);
2004 			break;
2005 		}
2006 
2007 		/*
2008 		 * At this point, the CCB in question is either an
2009 		 * immediate CCB (like XPT_DEV_ADVINFO) or it is a user CCB
2010 		 * and therefore should be malloced, not allocated via a slot.
2011 		 * Remove the CCB from the incoming queue and add it to the
2012 		 * active queue.
2013 		 */
2014 		TAILQ_REMOVE(&softc->incoming_queue, io_req, links);
2015 		TAILQ_INSERT_TAIL(&softc->active_queue, io_req, links);
2016 
2017 		xpt_action(ccb);
2018 
2019 		/*
2020 		 * If this is not a queued CCB (i.e. it is an immediate CCB),
2021 		 * then it is already done.  We need to put it on the done
2022 		 * queue for the user to fetch.
2023 		 */
2024 		if ((fc & XPT_FC_QUEUED) == 0) {
2025 			TAILQ_REMOVE(&softc->active_queue, io_req, links);
2026 			TAILQ_INSERT_TAIL(&softc->done_queue, io_req, links);
2027 		}
2028 		break;
2029 
2030 camioqueue_error:
2031 		uma_zfree(softc->pass_zone, io_req);
2032 		cam_periph_lock(periph);
2033 		break;
2034 	}
2035 	case CAMIOGET:
2036 	{
2037 		union ccb **user_ccb;
2038 		struct pass_io_req *io_req;
2039 		int old_error;
2040 
2041 		user_ccb = (union ccb **)addr;
2042 		old_error = 0;
2043 
2044 		io_req = TAILQ_FIRST(&softc->done_queue);
2045 		if (io_req == NULL) {
2046 			error = ENOENT;
2047 			break;
2048 		}
2049 
2050 		/*
2051 		 * Remove the I/O from the done queue.
2052 		 */
2053 		TAILQ_REMOVE(&softc->done_queue, io_req, links);
2054 
2055 		/*
2056 		 * We have to drop the lock during the copyout because the
2057 		 * copyout can result in VM faults that require sleeping.
2058 		 */
2059 		cam_periph_unlock(periph);
2060 
2061 		/*
2062 		 * Do any needed copies (e.g. for reads) and revert the
2063 		 * pointers in the CCB back to the user's pointers.
2064 		 */
2065 		error = passmemdone(periph, io_req);
2066 
2067 		old_error = error;
2068 
2069 		io_req->ccb.ccb_h.periph_links = io_req->user_periph_links;
2070 		io_req->ccb.ccb_h.periph_priv = io_req->user_periph_priv;
2071 
2072 #if 0
2073 		xpt_print(periph->path, "Copying to user CCB %p from "
2074 			  "kernel address %p\n", *user_ccb, &io_req->ccb);
2075 #endif
2076 
2077 		error = copyout(&io_req->ccb, *user_ccb, sizeof(union ccb));
2078 		if (error != 0) {
2079 			xpt_print(periph->path, "Copy to user CCB %p from "
2080 				  "kernel address %p failed with error %d\n",
2081 				  *user_ccb, &io_req->ccb, error);
2082 		}
2083 
2084 		/*
2085 		 * Prefer the first error we got back, and make sure we
2086 		 * don't overwrite bad status with good.
2087 		 */
2088 		if (old_error != 0)
2089 			error = old_error;
2090 
2091 		cam_periph_lock(periph);
2092 
2093 		/*
2094 		 * At this point, if there was an error, we could potentially
2095 		 * re-queue the I/O and try again.  But why?  The error
2096 		 * would almost certainly happen again.  We might as well
2097 		 * not leak memory.
2098 		 */
2099 		uma_zfree(softc->pass_zone, io_req);
2100 		break;
2101 	}
2102 	default:
2103 		error = cam_periph_ioctl(periph, cmd, addr, passerror);
2104 		break;
2105 	}
2106 
2107 bailout:
2108 	cam_periph_unlock(periph);
2109 
2110 	return(error);
2111 }
2112 
2113 static int
2114 passpoll(struct cdev *dev, int poll_events, struct thread *td)
2115 {
2116 	struct cam_periph *periph;
2117 	struct pass_softc *softc;
2118 	int revents;
2119 
2120 	periph = (struct cam_periph *)dev->si_drv1;
2121 	softc = (struct pass_softc *)periph->softc;
2122 
2123 	revents = poll_events & (POLLOUT | POLLWRNORM);
2124 	if ((poll_events & (POLLIN | POLLRDNORM)) != 0) {
2125 		cam_periph_lock(periph);
2126 
2127 		if (!TAILQ_EMPTY(&softc->done_queue)) {
2128 			revents |= poll_events & (POLLIN | POLLRDNORM);
2129 		}
2130 		cam_periph_unlock(periph);
2131 		if (revents == 0)
2132 			selrecord(td, &softc->read_select);
2133 	}
2134 
2135 	return (revents);
2136 }
2137 
2138 static int
2139 passkqfilter(struct cdev *dev, struct knote *kn)
2140 {
2141 	struct cam_periph *periph;
2142 	struct pass_softc *softc;
2143 
2144 	periph = (struct cam_periph *)dev->si_drv1;
2145 	softc = (struct pass_softc *)periph->softc;
2146 
2147 	kn->kn_hook = (caddr_t)periph;
2148 	kn->kn_fop = &passread_filtops;
2149 	knlist_add(&softc->read_select.si_note, kn, 0);
2150 
2151 	return (0);
2152 }
2153 
2154 static void
2155 passreadfiltdetach(struct knote *kn)
2156 {
2157 	struct cam_periph *periph;
2158 	struct pass_softc *softc;
2159 
2160 	periph = (struct cam_periph *)kn->kn_hook;
2161 	softc = (struct pass_softc *)periph->softc;
2162 
2163 	knlist_remove(&softc->read_select.si_note, kn, 0);
2164 }
2165 
2166 static int
2167 passreadfilt(struct knote *kn, long hint)
2168 {
2169 	struct cam_periph *periph;
2170 	struct pass_softc *softc;
2171 	int retval;
2172 
2173 	periph = (struct cam_periph *)kn->kn_hook;
2174 	softc = (struct pass_softc *)periph->softc;
2175 
2176 	cam_periph_assert(periph, MA_OWNED);
2177 
2178 	if (TAILQ_EMPTY(&softc->done_queue))
2179 		retval = 0;
2180 	else
2181 		retval = 1;
2182 
2183 	return (retval);
2184 }
2185 
2186 /*
2187  * Generally, "ccb" should be the CCB supplied by the kernel.  "inccb"
2188  * should be the CCB that is copied in from the user.
2189  */
2190 static int
2191 passsendccb(struct cam_periph *periph, union ccb *ccb, union ccb *inccb)
2192 {
2193 	struct pass_softc *softc;
2194 	struct cam_periph_map_info mapinfo;
2195 	uint8_t *cmd;
2196 	xpt_opcode fc;
2197 	int error;
2198 
2199 	softc = (struct pass_softc *)periph->softc;
2200 
2201 	/*
2202 	 * There are some fields in the CCB header that need to be
2203 	 * preserved, the rest we get from the user.
2204 	 */
2205 	xpt_merge_ccb(ccb, inccb);
2206 
2207 	if (ccb->ccb_h.flags & CAM_CDB_POINTER) {
2208 		cmd = __builtin_alloca(ccb->csio.cdb_len);
2209 		error = copyin(ccb->csio.cdb_io.cdb_ptr, cmd, ccb->csio.cdb_len);
2210 		if (error)
2211 			return (error);
2212 		ccb->csio.cdb_io.cdb_ptr = cmd;
2213 	}
2214 
2215 	/*
2216 	 */
2217 	ccb->ccb_h.cbfcnp = passdone;
2218 
2219 	/*
2220 	 * Let cam_periph_mapmem do a sanity check on the data pointer format.
2221 	 * Even if no data transfer is needed, it's a cheap check and it
2222 	 * simplifies the code.
2223 	 */
2224 	fc = ccb->ccb_h.func_code;
2225 	if ((fc == XPT_SCSI_IO) || (fc == XPT_ATA_IO) || (fc == XPT_SMP_IO)
2226             || (fc == XPT_DEV_MATCH) || (fc == XPT_DEV_ADVINFO) || (fc == XPT_MMC_IO)
2227             || (fc == XPT_NVME_ADMIN) || (fc == XPT_NVME_IO)) {
2228 
2229 		bzero(&mapinfo, sizeof(mapinfo));
2230 
2231 		/*
2232 		 * cam_periph_mapmem calls into proc and vm functions that can
2233 		 * sleep as well as trigger I/O, so we can't hold the lock.
2234 		 * Dropping it here is reasonably safe.
2235 		 */
2236 		cam_periph_unlock(periph);
2237 		error = cam_periph_mapmem(ccb, &mapinfo, softc->maxio);
2238 		cam_periph_lock(periph);
2239 
2240 		/*
2241 		 * cam_periph_mapmem returned an error, we can't continue.
2242 		 * Return the error to the user.
2243 		 */
2244 		if (error)
2245 			return(error);
2246 	} else
2247 		/* Ensure that the unmap call later on is a no-op. */
2248 		mapinfo.num_bufs_used = 0;
2249 
2250 	/*
2251 	 * If the user wants us to perform any error recovery, then honor
2252 	 * that request.  Otherwise, it's up to the user to perform any
2253 	 * error recovery.
2254 	 */
2255 	cam_periph_runccb(ccb, (ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) ?
2256 	    passerror : NULL, /* cam_flags */ CAM_RETRY_SELTO,
2257 	    /* sense_flags */ SF_RETRY_UA | SF_NO_PRINT,
2258 	    softc->device_stats);
2259 
2260 	cam_periph_unmapmem(ccb, &mapinfo);
2261 
2262 	ccb->ccb_h.cbfcnp = NULL;
2263 	ccb->ccb_h.periph_priv = inccb->ccb_h.periph_priv;
2264 	bcopy(ccb, inccb, sizeof(union ccb));
2265 
2266 	return(0);
2267 }
2268 
2269 static int
2270 passerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
2271 {
2272 	struct cam_periph *periph;
2273 	struct pass_softc *softc;
2274 
2275 	periph = xpt_path_periph(ccb->ccb_h.path);
2276 	softc = (struct pass_softc *)periph->softc;
2277 
2278 	return(cam_periph_error(ccb, cam_flags, sense_flags,
2279 				 &softc->saved_ccb));
2280 }
2281