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