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