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