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