xref: /freebsd/sys/cam/cam_xpt.c (revision c4c790949fbcba9f8425351896e3b3047eaae0b8)
1 /*-
2  * Implementation of the Common Access Method Transport (XPT) layer.
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
7  * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions, and the following disclaimer,
15  *    without modification, immediately at the beginning of the file.
16  * 2. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include "opt_printf.h"
33 
34 #include <sys/param.h>
35 #include <sys/bio.h>
36 #include <sys/bus.h>
37 #include <sys/systm.h>
38 #include <sys/types.h>
39 #include <sys/malloc.h>
40 #include <sys/kernel.h>
41 #include <sys/time.h>
42 #include <sys/conf.h>
43 #include <sys/fcntl.h>
44 #include <sys/proc.h>
45 #include <sys/sbuf.h>
46 #include <sys/smp.h>
47 #include <sys/stdarg.h>
48 #include <sys/taskqueue.h>
49 
50 #include <sys/lock.h>
51 #include <sys/mutex.h>
52 #include <sys/sysctl.h>
53 #include <sys/kthread.h>
54 
55 #include <cam/cam.h>
56 #include <cam/cam_ccb.h>
57 #include <cam/cam_iosched.h>
58 #include <cam/cam_periph.h>
59 #include <cam/cam_queue.h>
60 #include <cam/cam_sim.h>
61 #include <cam/cam_xpt.h>
62 #include <cam/cam_xpt_sim.h>
63 #include <cam/cam_xpt_periph.h>
64 #include <cam/cam_xpt_internal.h>
65 #include <cam/cam_debug.h>
66 #include <cam/cam_compat.h>
67 
68 #include <cam/scsi/scsi_all.h>
69 #include <cam/scsi/scsi_message.h>
70 #include <cam/scsi/scsi_pass.h>
71 
72 
73 /* SDT Probes */
74 SDT_PROBE_DEFINE1(cam, , xpt, action, "union ccb *");
75 SDT_PROBE_DEFINE1(cam, , xpt, done, "union ccb *");
76 SDT_PROBE_DEFINE4(cam, , xpt, async__cb, "void *", "uint32_t",
77     "struct cam_path *", "void *");
78 SDT_PROBE_DEFINE2(cam, , xpt, bus__register, "struct cam_sim *", "path_id_t");
79 SDT_PROBE_DEFINE1(cam, , xpt, hold__boot, "int");
80 SDT_PROBE_DEFINE1(cam, , xpt, release__boot, "int");
81 
82 /* Wild guess based on not wanting to grow the stack too much */
83 #define XPT_PRINT_MAXLEN	512
84 #ifdef PRINTF_BUFR_SIZE
85 #define XPT_PRINT_LEN	PRINTF_BUFR_SIZE
86 #else
87 #define XPT_PRINT_LEN	128
88 #endif
89 _Static_assert(XPT_PRINT_LEN <= XPT_PRINT_MAXLEN, "XPT_PRINT_LEN is too large");
90 
91 /*
92  * This sets a default for the the maximum number of high powered commands
93  * (e.g. start unit) that can be outstanding at a particular time.
94  */
95 #ifndef CAM_MAX_HIGHPOWER
96 #define CAM_MAX_HIGHPOWER  4
97 #endif
98 
99 /* Datastructures internal to the xpt layer */
100 MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
101 MALLOC_DEFINE(M_CAMDEV, "CAM DEV", "CAM devices");
102 MALLOC_DEFINE(M_CAMCCB, "CAM CCB", "CAM CCBs");
103 MALLOC_DEFINE(M_CAMPATH, "CAM path", "CAM paths");
104 
105 struct xpt_softc {
106 	uint32_t		xpt_generation;
107 
108 	/* number of high powered commands that can go through right now */
109 	struct mtx		xpt_highpower_lock;
110 	STAILQ_HEAD(highpowerlist, cam_ed)	highpowerq;
111 	int			num_highpower;
112 
113 	/* queue for handling async rescan requests. */
114 	TAILQ_HEAD(, ccb_hdr) ccb_scanq;
115 	int buses_to_config;
116 	int buses_config_done;
117 
118 	/*
119 	 * Registered buses
120 	 *
121 	 * N.B., "busses" is an archaic spelling of "buses".  In new code
122 	 * "buses" is preferred.
123 	 */
124 	TAILQ_HEAD(,cam_eb)	xpt_busses;
125 	u_int			bus_generation;
126 
127 	int			boot_delay;
128 	struct callout 		boot_callout;
129 	struct task		boot_task;
130 	struct root_hold_token	xpt_rootmount;
131 
132 	struct mtx		xpt_topo_lock;
133 	struct taskqueue	*xpt_taskq;
134 };
135 
136 typedef enum {
137 	DM_RET_COPY		= 0x01,
138 	DM_RET_FLAG_MASK	= 0x0f,
139 	DM_RET_NONE		= 0x00,
140 	DM_RET_STOP		= 0x10,
141 	DM_RET_DESCEND		= 0x20,
142 	DM_RET_ERROR		= 0x30,
143 	DM_RET_ACTION_MASK	= 0xf0
144 } dev_match_ret;
145 
146 typedef enum {
147 	XPT_DEPTH_BUS,
148 	XPT_DEPTH_TARGET,
149 	XPT_DEPTH_DEVICE,
150 	XPT_DEPTH_PERIPH
151 } xpt_traverse_depth;
152 
153 struct xpt_traverse_config {
154 	xpt_traverse_depth	depth;
155 	void			*tr_func;
156 	void			*tr_arg;
157 };
158 
159 typedef	int	xpt_busfunc_t (struct cam_eb *bus, void *arg);
160 typedef	int	xpt_targetfunc_t (struct cam_et *target, void *arg);
161 typedef	int	xpt_devicefunc_t (struct cam_ed *device, void *arg);
162 typedef	int	xpt_periphfunc_t (struct cam_periph *periph, void *arg);
163 typedef int	xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
164 
165 /* Transport layer configuration information */
166 static struct xpt_softc xsoftc;
167 
168 MTX_SYSINIT(xpt_topo_init, &xsoftc.xpt_topo_lock, "XPT topology lock", MTX_DEF);
169 
170 SYSCTL_INT(_kern_cam, OID_AUTO, boot_delay, CTLFLAG_RDTUN,
171            &xsoftc.boot_delay, 0, "Bus registration wait time");
172 SYSCTL_UINT(_kern_cam, OID_AUTO, xpt_generation, CTLFLAG_RD,
173 	    &xsoftc.xpt_generation, 0, "CAM peripheral generation count");
174 SYSCTL_INT(_kern_cam, OID_AUTO, max_high_power, CTLFLAG_RWTUN,
175            &xsoftc.num_highpower, 0,
176 	   "Max number of high power commands to be issued at once");
177 
178 struct cam_doneq {
179 	struct mtx_padalign	cam_doneq_mtx;
180 	STAILQ_HEAD(, ccb_hdr)	cam_doneq;
181 	int			cam_doneq_sleep;
182 };
183 
184 static struct cam_doneq cam_doneqs[MAXCPU];
185 static u_int __read_mostly cam_num_doneqs;
186 static struct proc *cam_proc;
187 static struct cam_doneq cam_async;
188 
189 SYSCTL_INT(_kern_cam, OID_AUTO, num_doneqs, CTLFLAG_RDTUN,
190            &cam_num_doneqs, 0, "Number of completion queues/threads");
191 
192 struct cam_periph *xpt_periph;
193 
194 static periph_init_t xpt_periph_init;
195 
196 static struct periph_driver xpt_driver =
197 {
198 	xpt_periph_init, "xpt",
199 	TAILQ_HEAD_INITIALIZER(xpt_driver.units), /* generation */ 0,
200 	CAM_PERIPH_DRV_EARLY
201 };
202 
203 PERIPHDRIVER_DECLARE(xpt, xpt_driver);
204 
205 static d_open_t xptopen;
206 static d_close_t xptclose;
207 static d_ioctl_t xptioctl;
208 static d_ioctl_t xptdoioctl;
209 
210 static struct cdevsw xpt_cdevsw = {
211 	.d_version =	D_VERSION,
212 	.d_flags =	0,
213 	.d_open =	xptopen,
214 	.d_close =	xptclose,
215 	.d_ioctl =	xptioctl,
216 	.d_name =	"xpt",
217 };
218 
219 /* Storage for debugging datastructures */
220 struct cam_path *cam_dpath;
221 uint32_t __read_mostly cam_dflags = CAM_DEBUG_FLAGS;
222 SYSCTL_UINT(_kern_cam, OID_AUTO, dflags, CTLFLAG_RWTUN,
223 	&cam_dflags, 0, "Enabled debug flags");
224 uint32_t cam_debug_delay = CAM_DEBUG_DELAY;
225 SYSCTL_UINT(_kern_cam, OID_AUTO, debug_delay, CTLFLAG_RWTUN,
226 	&cam_debug_delay, 0, "Delay in us after each debug message");
227 
228 /* Our boot-time initialization hook */
229 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
230 
231 static moduledata_t cam_moduledata = {
232 	"cam",
233 	cam_module_event_handler,
234 	NULL
235 };
236 
237 static int	xpt_init(void *);
238 
239 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
240 MODULE_VERSION(cam, 1);
241 
242 static void		xpt_async_bcast(struct async_list *async_head,
243 					uint32_t async_code,
244 					struct cam_path *path,
245 					void *async_arg);
246 static path_id_t xptnextfreepathid(void);
247 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
248 static union ccb *xpt_get_ccb(struct cam_periph *periph);
249 static union ccb *xpt_get_ccb_nowait(struct cam_periph *periph);
250 static void	 xpt_run_allocq(struct cam_periph *periph, int sleep);
251 static void	 xpt_run_allocq_task(void *context, int pending);
252 static void	 xpt_run_devq(struct cam_devq *devq);
253 static callout_func_t xpt_release_devq_timeout;
254 static void	 xpt_acquire_bus(struct cam_eb *bus);
255 static void	 xpt_release_bus(struct cam_eb *bus);
256 static uint32_t	 xpt_freeze_devq_device(struct cam_ed *dev, u_int count);
257 static int	 xpt_release_devq_device(struct cam_ed *dev, u_int count,
258 		    int run_queue);
259 static struct cam_et*
260 		 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
261 static void	 xpt_acquire_target(struct cam_et *target);
262 static void	 xpt_release_target(struct cam_et *target);
263 static struct cam_eb*
264 		 xpt_find_bus(path_id_t path_id);
265 static struct cam_et*
266 		 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
267 static struct cam_ed*
268 		 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
269 static void	 xpt_config(void *arg);
270 static void	 xpt_hold_boot_locked(void);
271 static int	 xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
272 				 uint32_t new_priority);
273 static xpt_devicefunc_t xptpassannouncefunc;
274 static void	 xptaction(struct cam_sim *sim, union ccb *work_ccb);
275 static void	 xptpoll(struct cam_sim *sim);
276 static void	 camisr_runqueue(void);
277 static void	 xpt_done_process(struct ccb_hdr *ccb_h);
278 static void	 xpt_done_td(void *);
279 static void	 xpt_async_td(void *);
280 static dev_match_ret	xptbusmatch(struct dev_match_pattern *patterns,
281 				    u_int num_patterns, struct cam_eb *bus);
282 static dev_match_ret	xptdevicematch(struct dev_match_pattern *patterns,
283 				       u_int num_patterns,
284 				       struct cam_ed *device);
285 static dev_match_ret	xptperiphmatch(struct dev_match_pattern *patterns,
286 				       u_int num_patterns,
287 				       struct cam_periph *periph);
288 static xpt_busfunc_t	xptedtbusfunc;
289 static xpt_targetfunc_t	xptedttargetfunc;
290 static xpt_devicefunc_t	xptedtdevicefunc;
291 static xpt_periphfunc_t	xptedtperiphfunc;
292 static xpt_pdrvfunc_t	xptplistpdrvfunc;
293 static xpt_periphfunc_t	xptplistperiphfunc;
294 static int		xptedtmatch(struct ccb_dev_match *cdm);
295 static int		xptperiphlistmatch(struct ccb_dev_match *cdm);
296 static int		xptbustraverse(struct cam_eb *start_bus,
297 				       xpt_busfunc_t *tr_func, void *arg);
298 static int		xpttargettraverse(struct cam_eb *bus,
299 					  struct cam_et *start_target,
300 					  xpt_targetfunc_t *tr_func, void *arg);
301 static int		xptdevicetraverse(struct cam_et *target,
302 					  struct cam_ed *start_device,
303 					  xpt_devicefunc_t *tr_func, void *arg);
304 static int		xptperiphtraverse(struct cam_ed *device,
305 					  struct cam_periph *start_periph,
306 					  xpt_periphfunc_t *tr_func, void *arg);
307 static int		xptpdrvtraverse(struct periph_driver **start_pdrv,
308 					xpt_pdrvfunc_t *tr_func, void *arg);
309 static int		xptpdperiphtraverse(struct periph_driver **pdrv,
310 					    struct cam_periph *start_periph,
311 					    xpt_periphfunc_t *tr_func,
312 					    void *arg);
313 static xpt_busfunc_t	xptdefbusfunc;
314 static xpt_targetfunc_t	xptdeftargetfunc;
315 static xpt_devicefunc_t	xptdefdevicefunc;
316 static xpt_periphfunc_t	xptdefperiphfunc;
317 static void		xpt_finishconfig_task(void *context, int pending);
318 static void		xpt_dev_async_default(uint32_t async_code,
319 					      struct cam_eb *bus,
320 					      struct cam_et *target,
321 					      struct cam_ed *device,
322 					      void *async_arg);
323 static struct cam_ed *	xpt_alloc_device_default(struct cam_eb *bus,
324 						 struct cam_et *target,
325 						 lun_id_t lun_id);
326 static xpt_devicefunc_t	xptsetasyncfunc;
327 static xpt_busfunc_t	xptsetasyncbusfunc;
328 static cam_status	xptregister(struct cam_periph *periph,
329 				    void *arg);
330 
331 static __inline int
332 xpt_schedule_devq(struct cam_devq *devq, struct cam_ed *dev)
333 {
334 	int	retval;
335 
336 	mtx_assert(&devq->send_mtx, MA_OWNED);
337 	if ((dev->ccbq.queue.entries > 0) &&
338 	    (dev->ccbq.dev_openings > 0) &&
339 	    (dev->ccbq.queue.qfrozen_cnt == 0)) {
340 		/*
341 		 * The priority of a device waiting for controller
342 		 * resources is that of the highest priority CCB
343 		 * enqueued.
344 		 */
345 		retval =
346 		    xpt_schedule_dev(&devq->send_queue,
347 				     &dev->devq_entry,
348 				     CAMQ_GET_PRIO(&dev->ccbq.queue));
349 	} else {
350 		retval = 0;
351 	}
352 	return (retval);
353 }
354 
355 static __inline int
356 device_is_queued(struct cam_ed *device)
357 {
358 	return (device->devq_entry.index != CAM_UNQUEUED_INDEX);
359 }
360 
361 static void
362 xpt_periph_init(void)
363 {
364 	make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
365 }
366 
367 static int
368 xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
369 {
370 
371 	/*
372 	 * Only allow read-write access.
373 	 */
374 	if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
375 		return(EPERM);
376 
377 	/*
378 	 * We don't allow nonblocking access.
379 	 */
380 	if ((flags & O_NONBLOCK) != 0) {
381 		printf("%s: can't do nonblocking access\n", devtoname(dev));
382 		return(ENODEV);
383 	}
384 
385 	return(0);
386 }
387 
388 static int
389 xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
390 {
391 
392 	return(0);
393 }
394 
395 /*
396  * Don't automatically grab the xpt softc lock here even though this is going
397  * through the xpt device.  The xpt device is really just a back door for
398  * accessing other devices and SIMs, so the right thing to do is to grab
399  * the appropriate SIM lock once the bus/SIM is located.
400  */
401 static int
402 xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
403 {
404 	int error;
405 
406 	if ((error = xptdoioctl(dev, cmd, addr, flag, td)) == ENOTTY) {
407 		error = cam_compat_ioctl(dev, cmd, addr, flag, td, xptdoioctl);
408 	}
409 	return (error);
410 }
411 
412 static int
413 xptdoioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
414 {
415 	int error;
416 
417 	error = 0;
418 
419 	switch(cmd) {
420 	/*
421 	 * For the transport layer CAMIOCOMMAND ioctl, we really only want
422 	 * to accept CCB types that don't quite make sense to send through a
423 	 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
424 	 * in the CAM spec.
425 	 */
426 	case CAMIOCOMMAND: {
427 		union ccb *ccb;
428 		union ccb *inccb;
429 		struct cam_eb *bus;
430 
431 		inccb = (union ccb *)addr;
432 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
433 		if (inccb->ccb_h.func_code == XPT_SCSI_IO)
434 			inccb->csio.bio = NULL;
435 #endif
436 
437 		if (inccb->ccb_h.flags & CAM_UNLOCKED)
438 			return (EINVAL);
439 
440 		bus = xpt_find_bus(inccb->ccb_h.path_id);
441 		if (bus == NULL)
442 			return (EINVAL);
443 
444 		switch (inccb->ccb_h.func_code) {
445 		case XPT_SCAN_BUS:
446 		case XPT_RESET_BUS:
447 			if (inccb->ccb_h.target_id != CAM_TARGET_WILDCARD ||
448 			    inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
449 				xpt_release_bus(bus);
450 				return (EINVAL);
451 			}
452 			break;
453 		case XPT_SCAN_TGT:
454 			if (inccb->ccb_h.target_id == CAM_TARGET_WILDCARD ||
455 			    inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
456 				xpt_release_bus(bus);
457 				return (EINVAL);
458 			}
459 			break;
460 		default:
461 			break;
462 		}
463 
464 		switch(inccb->ccb_h.func_code) {
465 		case XPT_SCAN_BUS:
466 		case XPT_RESET_BUS:
467 		case XPT_PATH_INQ:
468 		case XPT_ENG_INQ:
469 		case XPT_SCAN_LUN:
470 		case XPT_SCAN_TGT:
471 
472 			ccb = xpt_alloc_ccb();
473 
474 			/*
475 			 * Create a path using the bus, target, and lun the
476 			 * user passed in.
477 			 */
478 			if (xpt_create_path(&ccb->ccb_h.path, NULL,
479 					    inccb->ccb_h.path_id,
480 					    inccb->ccb_h.target_id,
481 					    inccb->ccb_h.target_lun) !=
482 					    CAM_REQ_CMP){
483 				error = EINVAL;
484 				xpt_free_ccb(ccb);
485 				break;
486 			}
487 			/* Ensure all of our fields are correct */
488 			xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
489 				      inccb->ccb_h.pinfo.priority);
490 			xpt_merge_ccb(ccb, inccb);
491 			xpt_path_lock(ccb->ccb_h.path);
492 			cam_periph_runccb(ccb, NULL, 0, 0, NULL);
493 			xpt_path_unlock(ccb->ccb_h.path);
494 			bcopy(ccb, inccb, sizeof(union ccb));
495 			xpt_free_path(ccb->ccb_h.path);
496 			xpt_free_ccb(ccb);
497 			break;
498 
499 		case XPT_DEBUG: {
500 			union ccb ccb;
501 
502 			/*
503 			 * This is an immediate CCB, so it's okay to
504 			 * allocate it on the stack.
505 			 */
506 			memset(&ccb, 0, sizeof(ccb));
507 
508 			/*
509 			 * Create a path using the bus, target, and lun the
510 			 * user passed in.
511 			 */
512 			if (xpt_create_path(&ccb.ccb_h.path, NULL,
513 					    inccb->ccb_h.path_id,
514 					    inccb->ccb_h.target_id,
515 					    inccb->ccb_h.target_lun) !=
516 					    CAM_REQ_CMP){
517 				error = EINVAL;
518 				break;
519 			}
520 			/* Ensure all of our fields are correct */
521 			xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
522 				      inccb->ccb_h.pinfo.priority);
523 			xpt_merge_ccb(&ccb, inccb);
524 			xpt_action(&ccb);
525 			bcopy(&ccb, inccb, sizeof(union ccb));
526 			xpt_free_path(ccb.ccb_h.path);
527 			break;
528 		}
529 		case XPT_DEV_MATCH: {
530 			struct cam_periph_map_info mapinfo;
531 			struct cam_path *old_path;
532 
533 			/*
534 			 * We can't deal with physical addresses for this
535 			 * type of transaction.
536 			 */
537 			if ((inccb->ccb_h.flags & CAM_DATA_MASK) !=
538 			    CAM_DATA_VADDR) {
539 				error = EINVAL;
540 				break;
541 			}
542 
543 			/*
544 			 * Save this in case the caller had it set to
545 			 * something in particular.
546 			 */
547 			old_path = inccb->ccb_h.path;
548 
549 			/*
550 			 * We really don't need a path for the matching
551 			 * code.  The path is needed because of the
552 			 * debugging statements in xpt_action().  They
553 			 * assume that the CCB has a valid path.
554 			 */
555 			inccb->ccb_h.path = xpt_periph->path;
556 
557 			bzero(&mapinfo, sizeof(mapinfo));
558 
559 			/*
560 			 * Map the pattern and match buffers into kernel
561 			 * virtual address space.
562 			 */
563 			error = cam_periph_mapmem(inccb, &mapinfo, maxphys);
564 
565 			if (error) {
566 				inccb->ccb_h.path = old_path;
567 				break;
568 			}
569 
570 			/*
571 			 * This is an immediate CCB, we can send it on directly.
572 			 */
573 			xpt_action(inccb);
574 
575 			/*
576 			 * Map the buffers back into user space.
577 			 */
578 			error = cam_periph_unmapmem(inccb, &mapinfo);
579 
580 			inccb->ccb_h.path = old_path;
581 			break;
582 		}
583 		default:
584 			error = ENOTSUP;
585 			break;
586 		}
587 		xpt_release_bus(bus);
588 		break;
589 	}
590 	/*
591 	 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
592 	 * with the periphal driver name and unit name filled in.  The other
593 	 * fields don't really matter as input.  The passthrough driver name
594 	 * ("pass"), and unit number are passed back in the ccb.  The current
595 	 * device generation number, and the index into the device peripheral
596 	 * driver list, and the status are also passed back.  Note that
597 	 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
598 	 * we never return a status of CAM_GDEVLIST_LIST_CHANGED.  It is
599 	 * (or rather should be) impossible for the device peripheral driver
600 	 * list to change since we look at the whole thing in one pass, and
601 	 * we do it with lock protection.
602 	 *
603 	 */
604 	case CAMGETPASSTHRU: {
605 		union ccb *ccb;
606 		struct cam_periph *periph;
607 		struct periph_driver **p_drv;
608 		char   *name;
609 		u_int unit;
610 		bool base_periph_found;
611 
612 		ccb = (union ccb *)addr;
613 		unit = ccb->cgdl.unit_number;
614 		name = ccb->cgdl.periph_name;
615 		base_periph_found = false;
616 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
617 		if (ccb->ccb_h.func_code == XPT_SCSI_IO)
618 			ccb->csio.bio = NULL;
619 #endif
620 
621 		/*
622 		 * Sanity check -- make sure we don't get a null peripheral
623 		 * driver name.
624 		 */
625 		if (*ccb->cgdl.periph_name == '\0') {
626 			error = EINVAL;
627 			break;
628 		}
629 
630 		/* Keep the list from changing while we traverse it */
631 		xpt_lock_buses();
632 
633 		/* first find our driver in the list of drivers */
634 		for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
635 			if (strcmp((*p_drv)->driver_name, name) == 0)
636 				break;
637 
638 		if (*p_drv == NULL) {
639 			xpt_unlock_buses();
640 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
641 			ccb->cgdl.status = CAM_GDEVLIST_ERROR;
642 			*ccb->cgdl.periph_name = '\0';
643 			ccb->cgdl.unit_number = 0;
644 			error = ENOENT;
645 			break;
646 		}
647 
648 		/*
649 		 * Run through every peripheral instance of this driver
650 		 * and check to see whether it matches the unit passed
651 		 * in by the user.  If it does, get out of the loops and
652 		 * find the passthrough driver associated with that
653 		 * peripheral driver.
654 		 */
655 		for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
656 		     periph = TAILQ_NEXT(periph, unit_links)) {
657 			if (periph->unit_number == unit)
658 				break;
659 		}
660 		/*
661 		 * If we found the peripheral driver that the user passed
662 		 * in, go through all of the peripheral drivers for that
663 		 * particular device and look for a passthrough driver.
664 		 */
665 		if (periph != NULL) {
666 			struct cam_ed *device;
667 			int i;
668 
669 			base_periph_found = true;
670 			device = periph->path->device;
671 			for (i = 0, periph = SLIST_FIRST(&device->periphs);
672 			     periph != NULL;
673 			     periph = SLIST_NEXT(periph, periph_links), i++) {
674 				/*
675 				 * Check to see whether we have a
676 				 * passthrough device or not.
677 				 */
678 				if (strcmp(periph->periph_name, "pass") == 0) {
679 					/*
680 					 * Fill in the getdevlist fields.
681 					 */
682 					strlcpy(ccb->cgdl.periph_name,
683 					       periph->periph_name,
684 					       sizeof(ccb->cgdl.periph_name));
685 					ccb->cgdl.unit_number =
686 						periph->unit_number;
687 					if (SLIST_NEXT(periph, periph_links))
688 						ccb->cgdl.status =
689 							CAM_GDEVLIST_MORE_DEVS;
690 					else
691 						ccb->cgdl.status =
692 						       CAM_GDEVLIST_LAST_DEVICE;
693 					ccb->cgdl.generation =
694 						device->generation;
695 					ccb->cgdl.index = i;
696 					/*
697 					 * Fill in some CCB header fields
698 					 * that the user may want.
699 					 */
700 					ccb->ccb_h.path_id =
701 						periph->path->bus->path_id;
702 					ccb->ccb_h.target_id =
703 						periph->path->target->target_id;
704 					ccb->ccb_h.target_lun =
705 						periph->path->device->lun_id;
706 					ccb->ccb_h.status = CAM_REQ_CMP;
707 					break;
708 				}
709 			}
710 		}
711 
712 		/*
713 		 * If the periph is null here, one of two things has
714 		 * happened.  The first possibility is that we couldn't
715 		 * find the unit number of the particular peripheral driver
716 		 * that the user is asking about.  e.g. the user asks for
717 		 * the passthrough driver for "da11".  We find the list of
718 		 * "da" peripherals all right, but there is no unit 11.
719 		 * The other possibility is that we went through the list
720 		 * of peripheral drivers attached to the device structure,
721 		 * but didn't find one with the name "pass".  Either way,
722 		 * we return ENOENT, since we couldn't find something.
723 		 */
724 		if (periph == NULL) {
725 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
726 			ccb->cgdl.status = CAM_GDEVLIST_ERROR;
727 			*ccb->cgdl.periph_name = '\0';
728 			ccb->cgdl.unit_number = 0;
729 			error = ENOENT;
730 			/*
731 			 * It is unfortunate that this is even necessary,
732 			 * but there are many, many clueless users out there.
733 			 * If this is true, the user is looking for the
734 			 * passthrough driver, but doesn't have one in his
735 			 * kernel.
736 			 */
737 			if (base_periph_found) {
738 				printf(
739 		"xptioctl: pass driver is not in the kernel\n"
740 		"xptioctl: put \"device pass\" in your kernel config file\n");
741 			}
742 		}
743 		xpt_unlock_buses();
744 		break;
745 		}
746 	default:
747 		error = ENOTTY;
748 		break;
749 	}
750 
751 	return(error);
752 }
753 
754 static int
755 cam_module_event_handler(module_t mod, int what, void *arg)
756 {
757 	int error;
758 
759 	switch (what) {
760 	case MOD_LOAD:
761 		if ((error = xpt_init(NULL)) != 0)
762 			return (error);
763 		break;
764 	case MOD_UNLOAD:
765 		return EBUSY;
766 	default:
767 		return EOPNOTSUPP;
768 	}
769 
770 	return 0;
771 }
772 
773 static struct xpt_proto *
774 xpt_proto_find(cam_proto proto)
775 {
776 	struct xpt_proto **pp;
777 
778 	SET_FOREACH(pp, cam_xpt_proto_set) {
779 		if ((*pp)->proto == proto)
780 			return *pp;
781 	}
782 
783 	return NULL;
784 }
785 
786 static void
787 xpt_rescan_done(struct cam_periph *periph, union ccb *done_ccb)
788 {
789 
790 	if (done_ccb->ccb_h.ppriv_ptr1 == NULL) {
791 		xpt_free_path(done_ccb->ccb_h.path);
792 		xpt_free_ccb(done_ccb);
793 	} else {
794 		done_ccb->ccb_h.cbfcnp = done_ccb->ccb_h.ppriv_ptr1;
795 		(*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
796 	}
797 	xpt_release_boot();
798 }
799 
800 /* thread to handle bus rescans */
801 static void
802 xpt_scanner_thread(void *dummy)
803 {
804 	union ccb	*ccb;
805 	struct mtx	*mtx;
806 	struct cam_ed	*device;
807 
808 	xpt_lock_buses();
809 	for (;;) {
810 		if (TAILQ_EMPTY(&xsoftc.ccb_scanq))
811 			msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO,
812 			       "-", 0);
813 		if ((ccb = (union ccb *)TAILQ_FIRST(&xsoftc.ccb_scanq)) != NULL) {
814 			TAILQ_REMOVE(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
815 			xpt_unlock_buses();
816 
817 			/*
818 			 * We need to lock the device's mutex which we use as
819 			 * the path mutex. We can't do it directly because the
820 			 * cam_path in the ccb may wind up going away because
821 			 * the path lock may be dropped and the path retired in
822 			 * the completion callback. We do this directly to keep
823 			 * the reference counts in cam_path sane. We also have
824 			 * to copy the device pointer because ccb_h.path may
825 			 * be freed in the callback.
826 			 */
827 			mtx = xpt_path_mtx(ccb->ccb_h.path);
828 			device = ccb->ccb_h.path->device;
829 			xpt_acquire_device(device);
830 			mtx_lock(mtx);
831 			xpt_action(ccb);
832 			mtx_unlock(mtx);
833 			xpt_release_device(device);
834 
835 			xpt_lock_buses();
836 		}
837 	}
838 }
839 
840 void
841 xpt_rescan(union ccb *ccb)
842 {
843 	struct ccb_hdr *hdr;
844 
845 	/* Prepare request */
846 	if (ccb->ccb_h.path->target->target_id == CAM_TARGET_WILDCARD &&
847 	    ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
848 		ccb->ccb_h.func_code = XPT_SCAN_BUS;
849 	else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
850 	    ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
851 		ccb->ccb_h.func_code = XPT_SCAN_TGT;
852 	else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
853 	    ccb->ccb_h.path->device->lun_id != CAM_LUN_WILDCARD)
854 		ccb->ccb_h.func_code = XPT_SCAN_LUN;
855 	else {
856 		xpt_print(ccb->ccb_h.path, "illegal scan path\n");
857 		xpt_free_path(ccb->ccb_h.path);
858 		xpt_free_ccb(ccb);
859 		return;
860 	}
861 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
862 	    ("xpt_rescan: func %#x %s\n", ccb->ccb_h.func_code,
863  		xpt_action_name(ccb->ccb_h.func_code)));
864 
865 	ccb->ccb_h.ppriv_ptr1 = ccb->ccb_h.cbfcnp;
866 	ccb->ccb_h.cbfcnp = xpt_rescan_done;
867 	xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, CAM_PRIORITY_XPT);
868 	/* Don't make duplicate entries for the same paths. */
869 	xpt_lock_buses();
870 	if (ccb->ccb_h.ppriv_ptr1 == NULL) {
871 		TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) {
872 			if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) {
873 				wakeup(&xsoftc.ccb_scanq);
874 				xpt_unlock_buses();
875 				xpt_print(ccb->ccb_h.path, "rescan already queued\n");
876 				xpt_free_path(ccb->ccb_h.path);
877 				xpt_free_ccb(ccb);
878 				return;
879 			}
880 		}
881 	}
882 	TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
883 	xpt_hold_boot_locked();
884 	wakeup(&xsoftc.ccb_scanq);
885 	xpt_unlock_buses();
886 }
887 
888 /* Functions accessed by the peripheral drivers */
889 static int
890 xpt_init(void *dummy)
891 {
892 	struct cam_sim *xpt_sim;
893 	struct cam_path *path;
894 	struct cam_devq *devq;
895 	cam_status status;
896 	int error, i;
897 
898 	TAILQ_INIT(&xsoftc.xpt_busses);
899 	TAILQ_INIT(&xsoftc.ccb_scanq);
900 	STAILQ_INIT(&xsoftc.highpowerq);
901 
902 	/* Fall back to a default if the kenv tunable isn't set */
903 	if (xsoftc.num_highpower == 0)
904 		xsoftc.num_highpower = CAM_MAX_HIGHPOWER;
905 
906 	mtx_init(&xsoftc.xpt_highpower_lock, "XPT highpower lock", NULL, MTX_DEF);
907 	xsoftc.xpt_taskq = taskqueue_create("CAM XPT task", M_WAITOK,
908 	    taskqueue_thread_enqueue, /*context*/&xsoftc.xpt_taskq);
909 
910 #ifdef CAM_BOOT_DELAY
911 	/*
912 	 * Override this value at compile time to assist our users
913 	 * who don't use loader to boot a kernel.
914 	 */
915 	xsoftc.boot_delay = CAM_BOOT_DELAY;
916 #endif
917 
918 	/*
919 	 * The xpt layer is, itself, the equivalent of a SIM.
920 	 * Allow 16 ccbs in the ccb pool for it.  This should
921 	 * give decent parallelism when we probe buses and
922 	 * perform other XPT functions.
923 	 */
924 	devq = cam_simq_alloc(16);
925 	if (devq == NULL)
926 		return (ENOMEM);
927 	xpt_sim = cam_sim_alloc(xptaction,
928 				xptpoll,
929 				"xpt",
930 				/*softc*/NULL,
931 				/*unit*/0,
932 				/*mtx*/NULL,
933 				/*max_dev_transactions*/0,
934 				/*max_tagged_dev_transactions*/0,
935 				devq);
936 	if (xpt_sim == NULL)
937 		return (ENOMEM);
938 
939 	if ((error = xpt_bus_register(xpt_sim, NULL, 0)) != CAM_SUCCESS) {
940 		printf(
941 		    "xpt_init: xpt_bus_register failed with errno %d, failing attach\n",
942 		    error);
943 		return (EINVAL);
944 	}
945 
946 	/*
947 	 * Looking at the XPT from the SIM layer, the XPT is
948 	 * the equivalent of a peripheral driver.  Allocate
949 	 * a peripheral driver entry for us.
950 	 */
951 	if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
952 				      CAM_TARGET_WILDCARD,
953 				      CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
954 		printf(
955 	"xpt_init: xpt_create_path failed with status %#x, failing attach\n",
956 		    status);
957 		return (EINVAL);
958 	}
959 	xpt_path_lock(path);
960 	cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
961 			 path, NULL, 0, xpt_sim);
962 	xpt_path_unlock(path);
963 	xpt_free_path(path);
964 
965 	if (cam_num_doneqs < 1)
966 		cam_num_doneqs = 1 + mp_ncpus / 6;
967 	else if (cam_num_doneqs > MAXCPU)
968 		cam_num_doneqs = MAXCPU;
969 	for (i = 0; i < cam_num_doneqs; i++) {
970 		mtx_init(&cam_doneqs[i].cam_doneq_mtx, "CAM doneq", NULL,
971 		    MTX_DEF);
972 		STAILQ_INIT(&cam_doneqs[i].cam_doneq);
973 		error = kproc_kthread_add(xpt_done_td, &cam_doneqs[i],
974 		    &cam_proc, NULL, 0, 0, "cam", "doneq%d", i);
975 		if (error != 0) {
976 			cam_num_doneqs = i;
977 			break;
978 		}
979 	}
980 	if (cam_num_doneqs < 1) {
981 		printf("xpt_init: Cannot init completion queues - failing attach\n");
982 		return (ENOMEM);
983 	}
984 
985 	mtx_init(&cam_async.cam_doneq_mtx, "CAM async", NULL, MTX_DEF);
986 	STAILQ_INIT(&cam_async.cam_doneq);
987 	if (kproc_kthread_add(xpt_async_td, &cam_async,
988 		&cam_proc, NULL, 0, 0, "cam", "async") != 0) {
989 		printf("xpt_init: Cannot init async thread - failing attach\n");
990 		return (ENOMEM);
991 	}
992 
993 	/*
994 	 * Register a callback for when interrupts are enabled.
995 	 */
996 	config_intrhook_oneshot(xpt_config, NULL);
997 
998 	return (0);
999 }
1000 
1001 static cam_status
1002 xptregister(struct cam_periph *periph, void *arg)
1003 {
1004 	struct cam_sim *xpt_sim;
1005 
1006 	if (periph == NULL) {
1007 		printf("xptregister: periph was NULL!!\n");
1008 		return(CAM_REQ_CMP_ERR);
1009 	}
1010 
1011 	xpt_sim = (struct cam_sim *)arg;
1012 	xpt_sim->softc = periph;
1013 	xpt_periph = periph;
1014 	periph->softc = NULL;
1015 
1016 	return(CAM_REQ_CMP);
1017 }
1018 
1019 int32_t
1020 xpt_add_periph(struct cam_periph *periph)
1021 {
1022 	struct cam_ed *device;
1023 	int32_t	 status;
1024 
1025 	TASK_INIT(&periph->periph_run_task, 0, xpt_run_allocq_task, periph);
1026 	device = periph->path->device;
1027 	status = CAM_REQ_CMP;
1028 	if (device != NULL) {
1029 		mtx_lock(&device->target->bus->eb_mtx);
1030 		device->generation++;
1031 		SLIST_INSERT_HEAD(&device->periphs, periph, periph_links);
1032 		mtx_unlock(&device->target->bus->eb_mtx);
1033 		atomic_add_32(&xsoftc.xpt_generation, 1);
1034 	}
1035 
1036 	return (status);
1037 }
1038 
1039 /*
1040  * Remove this peripheral from the list of peripherals the devices maintains.
1041  * Bump generation numbers to note topology changes.
1042  */
1043 void
1044 xpt_remove_periph(struct cam_periph *periph)
1045 {
1046 	struct cam_ed *device;
1047 
1048 	device = periph->path->device;
1049 	if (device != NULL) {
1050 		mtx_lock(&device->target->bus->eb_mtx);
1051 		device->generation++;
1052 		SLIST_REMOVE(&device->periphs, periph, cam_periph, periph_links);
1053 		mtx_unlock(&device->target->bus->eb_mtx);
1054 		atomic_add_32(&xsoftc.xpt_generation, 1);
1055 	}
1056 }
1057 
1058 void
1059 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1060 {
1061 	char buf[128];
1062 	struct sbuf sb;
1063 
1064 	(void)sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN | SBUF_INCLUDENUL);
1065 	sbuf_set_drain(&sb, sbuf_printf_drain, NULL);
1066 	xpt_announce_periph_sbuf(periph, &sb, announce_string);
1067 	(void)sbuf_finish(&sb);
1068 	(void)sbuf_delete(&sb);
1069 }
1070 
1071 void
1072 xpt_announce_periph_sbuf(struct cam_periph *periph, struct sbuf *sb,
1073     char *announce_string)
1074 {
1075 	struct	cam_path *path = periph->path;
1076 	struct  xpt_proto *proto;
1077 
1078 	cam_periph_assert(periph, MA_OWNED);
1079 	periph->flags |= CAM_PERIPH_ANNOUNCED;
1080 
1081 	sbuf_printf(sb, "%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
1082 	    periph->periph_name, periph->unit_number,
1083 	    path->bus->sim->sim_name,
1084 	    path->bus->sim->unit_number,
1085 	    path->bus->sim->bus_id,
1086 	    path->bus->path_id,
1087 	    path->target->target_id,
1088 	    (uintmax_t)path->device->lun_id);
1089 	sbuf_printf(sb, "%s%d: ", periph->periph_name, periph->unit_number);
1090 	proto = xpt_proto_find(path->device->protocol);
1091 	if (proto)
1092 		proto->ops->announce_sbuf(path->device, sb);
1093 	else
1094 		sbuf_printf(sb, "Unknown protocol device %d\n",
1095 		    path->device->protocol);
1096 	if (path->device->serial_num_len > 0) {
1097 		/* Don't wrap the screen  - print only the first 60 chars */
1098 		sbuf_printf(sb, "%s%d: Serial Number %.60s\n",
1099 		    periph->periph_name, periph->unit_number,
1100 		    path->device->serial_num);
1101 	}
1102 	/* Announce transport details. */
1103 	path->bus->xport->ops->announce_sbuf(periph, sb);
1104 	/* Announce command queueing. */
1105 	if (path->device->inq_flags & SID_CmdQue
1106 	 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1107 		sbuf_printf(sb, "%s%d: Command Queueing enabled\n",
1108 		    periph->periph_name, periph->unit_number);
1109 	}
1110 	/* Announce caller's details if they've passed in. */
1111 	if (announce_string != NULL)
1112 		sbuf_printf(sb, "%s%d: %s\n", periph->periph_name,
1113 		    periph->unit_number, announce_string);
1114 }
1115 
1116 void
1117 xpt_announce_quirks(struct cam_periph *periph, int quirks, char *bit_string)
1118 {
1119 	if (quirks != 0) {
1120 		printf("%s%d: quirks=0x%b\n", periph->periph_name,
1121 		    periph->unit_number, quirks, bit_string);
1122 	}
1123 }
1124 
1125 void
1126 xpt_announce_quirks_sbuf(struct cam_periph *periph, struct sbuf *sb,
1127 			 int quirks, char *bit_string)
1128 {
1129 	if (quirks != 0) {
1130 		sbuf_printf(sb, "%s%d: quirks=0x%b\n", periph->periph_name,
1131 		    periph->unit_number, quirks, bit_string);
1132 	}
1133 }
1134 
1135 void
1136 xpt_denounce_periph(struct cam_periph *periph)
1137 {
1138 	char buf[128];
1139 	struct sbuf sb;
1140 
1141 	(void)sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN | SBUF_INCLUDENUL);
1142 	sbuf_set_drain(&sb, sbuf_printf_drain, NULL);
1143 	xpt_denounce_periph_sbuf(periph, &sb);
1144 	(void)sbuf_finish(&sb);
1145 	(void)sbuf_delete(&sb);
1146 }
1147 
1148 void
1149 xpt_denounce_periph_sbuf(struct cam_periph *periph, struct sbuf *sb)
1150 {
1151 	struct cam_path *path = periph->path;
1152 	struct xpt_proto *proto;
1153 
1154 	cam_periph_assert(periph, MA_OWNED);
1155 
1156 	sbuf_printf(sb, "%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
1157 	    periph->periph_name, periph->unit_number,
1158 	    path->bus->sim->sim_name,
1159 	    path->bus->sim->unit_number,
1160 	    path->bus->sim->bus_id,
1161 	    path->bus->path_id,
1162 	    path->target->target_id,
1163 	    (uintmax_t)path->device->lun_id);
1164 	sbuf_printf(sb, "%s%d: ", periph->periph_name, periph->unit_number);
1165 	proto = xpt_proto_find(path->device->protocol);
1166 	if (proto)
1167 		proto->ops->denounce_sbuf(path->device, sb);
1168 	else
1169 		sbuf_printf(sb, "Unknown protocol device %d",
1170 		    path->device->protocol);
1171 	if (path->device->serial_num_len > 0)
1172 		sbuf_printf(sb, " s/n %.60s", path->device->serial_num);
1173 	sbuf_cat(sb, " detached\n");
1174 }
1175 
1176 int
1177 xpt_getattr(char *buf, size_t len, const char *attr, struct cam_path *path)
1178 {
1179 	int ret = -1, l, o;
1180 	struct ccb_dev_advinfo cdai;
1181 	struct scsi_vpd_device_id *did;
1182 	struct scsi_vpd_id_descriptor *idd;
1183 
1184 	xpt_path_assert(path, MA_OWNED);
1185 
1186 	memset(&cdai, 0, sizeof(cdai));
1187 	xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
1188 	cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1189 	cdai.flags = CDAI_FLAG_NONE;
1190 	cdai.bufsiz = len;
1191 	cdai.buf = buf;
1192 
1193 	if (!strcmp(attr, "GEOM::ident"))
1194 		cdai.buftype = CDAI_TYPE_SERIAL_NUM;
1195 	else if (!strcmp(attr, "GEOM::physpath"))
1196 		cdai.buftype = CDAI_TYPE_PHYS_PATH;
1197 	else if (strcmp(attr, "GEOM::lunid") == 0 ||
1198 		 strcmp(attr, "GEOM::lunname") == 0) {
1199 		cdai.buftype = CDAI_TYPE_SCSI_DEVID;
1200 		cdai.bufsiz = CAM_SCSI_DEVID_MAXLEN;
1201 		cdai.buf = malloc(cdai.bufsiz, M_CAMXPT, M_NOWAIT);
1202 		if (cdai.buf == NULL) {
1203 			ret = ENOMEM;
1204 			goto out;
1205 		}
1206 	} else
1207 		goto out;
1208 
1209 	xpt_action((union ccb *)&cdai); /* can only be synchronous */
1210 	if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1211 		cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1212 	if (cdai.provsiz == 0)
1213 		goto out;
1214 	switch(cdai.buftype) {
1215 	case CDAI_TYPE_SCSI_DEVID:
1216 		did = (struct scsi_vpd_device_id *)cdai.buf;
1217 		if (strcmp(attr, "GEOM::lunid") == 0) {
1218 			idd = scsi_get_devid(did, cdai.provsiz,
1219 			    scsi_devid_is_lun_naa);
1220 			if (idd == NULL)
1221 				idd = scsi_get_devid(did, cdai.provsiz,
1222 				    scsi_devid_is_lun_eui64);
1223 			if (idd == NULL)
1224 				idd = scsi_get_devid(did, cdai.provsiz,
1225 				    scsi_devid_is_lun_uuid);
1226 			if (idd == NULL)
1227 				idd = scsi_get_devid(did, cdai.provsiz,
1228 				    scsi_devid_is_lun_md5);
1229 		} else
1230 			idd = NULL;
1231 
1232 		if (idd == NULL)
1233 			idd = scsi_get_devid(did, cdai.provsiz,
1234 			    scsi_devid_is_lun_t10);
1235 		if (idd == NULL)
1236 			idd = scsi_get_devid(did, cdai.provsiz,
1237 			    scsi_devid_is_lun_name);
1238 		if (idd == NULL)
1239 			break;
1240 
1241 		ret = 0;
1242 		if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) ==
1243 		    SVPD_ID_CODESET_ASCII) {
1244 			if (idd->length < len) {
1245 				for (l = 0; l < idd->length; l++)
1246 					buf[l] = idd->identifier[l] ?
1247 					    idd->identifier[l] : ' ';
1248 				buf[l] = 0;
1249 			} else
1250 				ret = EFAULT;
1251 			break;
1252 		}
1253 		if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) ==
1254 		    SVPD_ID_CODESET_UTF8) {
1255 			l = strnlen(idd->identifier, idd->length);
1256 			if (l < len) {
1257 				bcopy(idd->identifier, buf, l);
1258 				buf[l] = 0;
1259 			} else
1260 				ret = EFAULT;
1261 			break;
1262 		}
1263 		if ((idd->id_type & SVPD_ID_TYPE_MASK) ==
1264 		    SVPD_ID_TYPE_UUID && idd->identifier[0] == 0x10) {
1265 			if ((idd->length - 2) * 2 + 4 >= len) {
1266 				ret = EFAULT;
1267 				break;
1268 			}
1269 			for (l = 2, o = 0; l < idd->length; l++) {
1270 				if (l == 6 || l == 8 || l == 10 || l == 12)
1271 				    o += sprintf(buf + o, "-");
1272 				o += sprintf(buf + o, "%02x",
1273 				    idd->identifier[l]);
1274 			}
1275 			break;
1276 		}
1277 		if (idd->length * 2 < len) {
1278 			for (l = 0; l < idd->length; l++)
1279 				sprintf(buf + l * 2, "%02x",
1280 				    idd->identifier[l]);
1281 		} else
1282 				ret = EFAULT;
1283 		break;
1284 	default:
1285 		if (cdai.provsiz < len) {
1286 			cdai.buf[cdai.provsiz] = 0;
1287 			ret = 0;
1288 		} else
1289 			ret = EFAULT;
1290 		break;
1291 	}
1292 
1293 out:
1294 	if ((char *)cdai.buf != buf)
1295 		free(cdai.buf, M_CAMXPT);
1296 	return ret;
1297 }
1298 
1299 static dev_match_ret
1300 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1301 	    struct cam_eb *bus)
1302 {
1303 	dev_match_ret retval;
1304 	u_int i;
1305 
1306 	retval = DM_RET_NONE;
1307 
1308 	/*
1309 	 * If we aren't given something to match against, that's an error.
1310 	 */
1311 	if (bus == NULL)
1312 		return(DM_RET_ERROR);
1313 
1314 	/*
1315 	 * If there are no match entries, then this bus matches no
1316 	 * matter what.
1317 	 */
1318 	if ((patterns == NULL) || (num_patterns == 0))
1319 		return(DM_RET_DESCEND | DM_RET_COPY);
1320 
1321 	for (i = 0; i < num_patterns; i++) {
1322 		struct bus_match_pattern *cur_pattern;
1323 		struct device_match_pattern *dp = &patterns[i].pattern.device_pattern;
1324 		struct periph_match_pattern *pp = &patterns[i].pattern.periph_pattern;
1325 
1326 		/*
1327 		 * If the pattern in question isn't for a bus node, we
1328 		 * aren't interested.  However, we do indicate to the
1329 		 * calling routine that we should continue descending the
1330 		 * tree, since the user wants to match against lower-level
1331 		 * EDT elements.
1332 		 */
1333 		if (patterns[i].type == DEV_MATCH_DEVICE &&
1334 		    (dp->flags & DEV_MATCH_PATH) != 0 &&
1335 		    dp->path_id != bus->path_id)
1336 			continue;
1337 		if (patterns[i].type == DEV_MATCH_PERIPH &&
1338 		    (pp->flags & PERIPH_MATCH_PATH) != 0 &&
1339 		    pp->path_id != bus->path_id)
1340 			continue;
1341 		if (patterns[i].type != DEV_MATCH_BUS) {
1342 			if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1343 				retval |= DM_RET_DESCEND;
1344 			continue;
1345 		}
1346 
1347 		cur_pattern = &patterns[i].pattern.bus_pattern;
1348 
1349 		if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1350 		 && (cur_pattern->path_id != bus->path_id))
1351 			continue;
1352 
1353 		if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1354 		 && (cur_pattern->bus_id != bus->sim->bus_id))
1355 			continue;
1356 
1357 		if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1358 		 && (cur_pattern->unit_number != bus->sim->unit_number))
1359 			continue;
1360 
1361 		if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1362 		 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1363 			     DEV_IDLEN) != 0))
1364 			continue;
1365 
1366 		/*
1367 		 * If we get to this point, the user definitely wants
1368 		 * information on this bus.  So tell the caller to copy the
1369 		 * data out.
1370 		 */
1371 		retval |= DM_RET_COPY;
1372 
1373 		/*
1374 		 * If the return action has been set to descend, then we
1375 		 * know that we've already seen a non-bus matching
1376 		 * expression, therefore we need to further descend the tree.
1377 		 * This won't change by continuing around the loop, so we
1378 		 * go ahead and return.  If we haven't seen a non-bus
1379 		 * matching expression, we keep going around the loop until
1380 		 * we exhaust the matching expressions.  We'll set the stop
1381 		 * flag once we fall out of the loop.
1382 		 */
1383 		if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1384 			return(retval);
1385 	}
1386 
1387 	/*
1388 	 * If the return action hasn't been set to descend yet, that means
1389 	 * we haven't seen anything other than bus matching patterns.  So
1390 	 * tell the caller to stop descending the tree -- the user doesn't
1391 	 * want to match against lower level tree elements.
1392 	 */
1393 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1394 		retval |= DM_RET_STOP;
1395 
1396 	return(retval);
1397 }
1398 
1399 static dev_match_ret
1400 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1401 	       struct cam_ed *device)
1402 {
1403 	dev_match_ret retval;
1404 	u_int i;
1405 
1406 	retval = DM_RET_NONE;
1407 
1408 	/*
1409 	 * If we aren't given something to match against, that's an error.
1410 	 */
1411 	if (device == NULL)
1412 		return(DM_RET_ERROR);
1413 
1414 	/*
1415 	 * If there are no match entries, then this device matches no
1416 	 * matter what.
1417 	 */
1418 	if ((patterns == NULL) || (num_patterns == 0))
1419 		return(DM_RET_DESCEND | DM_RET_COPY);
1420 
1421 	for (i = 0; i < num_patterns; i++) {
1422 		struct device_match_pattern *cur_pattern;
1423 		struct scsi_vpd_device_id *device_id_page;
1424 		struct periph_match_pattern *pp = &patterns[i].pattern.periph_pattern;
1425 
1426 		/*
1427 		 * If the pattern in question isn't for a device node, we
1428 		 * aren't interested.
1429 		 */
1430 		if (patterns[i].type == DEV_MATCH_PERIPH &&
1431 		    (pp->flags & PERIPH_MATCH_TARGET) != 0 &&
1432 		    pp->target_id != device->target->target_id)
1433 			continue;
1434 		if (patterns[i].type == DEV_MATCH_PERIPH &&
1435 		    (pp->flags & PERIPH_MATCH_LUN) != 0 &&
1436 		    pp->target_lun != device->lun_id)
1437 			continue;
1438 		if (patterns[i].type != DEV_MATCH_DEVICE) {
1439 			if ((patterns[i].type == DEV_MATCH_PERIPH)
1440 			 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1441 				retval |= DM_RET_DESCEND;
1442 			continue;
1443 		}
1444 
1445 		cur_pattern = &patterns[i].pattern.device_pattern;
1446 
1447 		/* Error out if mutually exclusive options are specified. */
1448 		if ((cur_pattern->flags & (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1449 		 == (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1450 			return(DM_RET_ERROR);
1451 
1452 		if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1453 		 && (cur_pattern->path_id != device->target->bus->path_id))
1454 			continue;
1455 
1456 		if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1457 		 && (cur_pattern->target_id != device->target->target_id))
1458 			continue;
1459 
1460 		if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1461 		 && (cur_pattern->target_lun != device->lun_id))
1462 			continue;
1463 
1464 		if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1465 		 && (cam_quirkmatch((caddr_t)&device->inq_data,
1466 				    (caddr_t)&cur_pattern->data.inq_pat,
1467 				    1, sizeof(cur_pattern->data.inq_pat),
1468 				    scsi_static_inquiry_match) == NULL))
1469 			continue;
1470 
1471 		device_id_page = (struct scsi_vpd_device_id *)device->device_id;
1472 		if (((cur_pattern->flags & DEV_MATCH_DEVID) != 0)
1473 		 && (device->device_id_len < SVPD_DEVICE_ID_HDR_LEN
1474 		  || scsi_devid_match((uint8_t *)device_id_page->desc_list,
1475 				      device->device_id_len
1476 				    - SVPD_DEVICE_ID_HDR_LEN,
1477 				      cur_pattern->data.devid_pat.id,
1478 				      cur_pattern->data.devid_pat.id_len) != 0))
1479 			continue;
1480 
1481 		/*
1482 		 * If we get to this point, the user definitely wants
1483 		 * information on this device.  So tell the caller to copy
1484 		 * the data out.
1485 		 */
1486 		retval |= DM_RET_COPY;
1487 
1488 		/*
1489 		 * If the return action has been set to descend, then we
1490 		 * know that we've already seen a peripheral matching
1491 		 * expression, therefore we need to further descend the tree.
1492 		 * This won't change by continuing around the loop, so we
1493 		 * go ahead and return.  If we haven't seen a peripheral
1494 		 * matching expression, we keep going around the loop until
1495 		 * we exhaust the matching expressions.  We'll set the stop
1496 		 * flag once we fall out of the loop.
1497 		 */
1498 		if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1499 			return(retval);
1500 	}
1501 
1502 	/*
1503 	 * If the return action hasn't been set to descend yet, that means
1504 	 * we haven't seen any peripheral matching patterns.  So tell the
1505 	 * caller to stop descending the tree -- the user doesn't want to
1506 	 * match against lower level tree elements.
1507 	 */
1508 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1509 		retval |= DM_RET_STOP;
1510 
1511 	return(retval);
1512 }
1513 
1514 /*
1515  * Match a single peripheral against any number of match patterns.
1516  */
1517 static dev_match_ret
1518 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1519 	       struct cam_periph *periph)
1520 {
1521 	dev_match_ret retval;
1522 	u_int i;
1523 
1524 	/*
1525 	 * If we aren't given something to match against, that's an error.
1526 	 */
1527 	if (periph == NULL)
1528 		return(DM_RET_ERROR);
1529 
1530 	/*
1531 	 * If there are no match entries, then this peripheral matches no
1532 	 * matter what.
1533 	 */
1534 	if ((patterns == NULL) || (num_patterns == 0))
1535 		return(DM_RET_STOP | DM_RET_COPY);
1536 
1537 	/*
1538 	 * There aren't any nodes below a peripheral node, so there's no
1539 	 * reason to descend the tree any further.
1540 	 */
1541 	retval = DM_RET_STOP;
1542 
1543 	for (i = 0; i < num_patterns; i++) {
1544 		struct periph_match_pattern *cur_pattern;
1545 
1546 		/*
1547 		 * If the pattern in question isn't for a peripheral, we
1548 		 * aren't interested.
1549 		 */
1550 		if (patterns[i].type != DEV_MATCH_PERIPH)
1551 			continue;
1552 
1553 		cur_pattern = &patterns[i].pattern.periph_pattern;
1554 
1555 		if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1556 		 && (cur_pattern->path_id != periph->path->bus->path_id))
1557 			continue;
1558 
1559 		/*
1560 		 * For the target and lun id's, we have to make sure the
1561 		 * target and lun pointers aren't NULL.  The xpt peripheral
1562 		 * has a wildcard target and device.
1563 		 */
1564 		if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1565 		 && ((periph->path->target == NULL)
1566 		 ||(cur_pattern->target_id != periph->path->target->target_id)))
1567 			continue;
1568 
1569 		if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1570 		 && ((periph->path->device == NULL)
1571 		 || (cur_pattern->target_lun != periph->path->device->lun_id)))
1572 			continue;
1573 
1574 		if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
1575 		 && (cur_pattern->unit_number != periph->unit_number))
1576 			continue;
1577 
1578 		if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
1579 		 && (strncmp(cur_pattern->periph_name, periph->periph_name,
1580 			     DEV_IDLEN) != 0))
1581 			continue;
1582 
1583 		/*
1584 		 * If we get to this point, the user definitely wants
1585 		 * information on this peripheral.  So tell the caller to
1586 		 * copy the data out.
1587 		 */
1588 		retval |= DM_RET_COPY;
1589 
1590 		/*
1591 		 * The return action has already been set to stop, since
1592 		 * peripherals don't have any nodes below them in the EDT.
1593 		 */
1594 		return(retval);
1595 	}
1596 
1597 	/*
1598 	 * If we get to this point, the peripheral that was passed in
1599 	 * doesn't match any of the patterns.
1600 	 */
1601 	return(retval);
1602 }
1603 
1604 static int
1605 xptedtbusfunc(struct cam_eb *bus, void *arg)
1606 {
1607 	struct ccb_dev_match *cdm;
1608 	struct cam_et *target;
1609 	dev_match_ret retval;
1610 
1611 	cdm = (struct ccb_dev_match *)arg;
1612 
1613 	/*
1614 	 * If our position is for something deeper in the tree, that means
1615 	 * that we've already seen this node.  So, we keep going down.
1616 	 */
1617 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1618 	 && (cdm->pos.cookie.bus == bus)
1619 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1620 	 && (cdm->pos.cookie.target != NULL))
1621 		retval = DM_RET_DESCEND;
1622 	else
1623 		retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
1624 
1625 	/*
1626 	 * If we got an error, bail out of the search.
1627 	 */
1628 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1629 		cdm->status = CAM_DEV_MATCH_ERROR;
1630 		return(0);
1631 	}
1632 
1633 	/*
1634 	 * If the copy flag is set, copy this bus out.
1635 	 */
1636 	if (retval & DM_RET_COPY) {
1637 		int spaceleft, j;
1638 
1639 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
1640 			sizeof(struct dev_match_result));
1641 
1642 		/*
1643 		 * If we don't have enough space to put in another
1644 		 * match result, save our position and tell the
1645 		 * user there are more devices to check.
1646 		 */
1647 		if (spaceleft < sizeof(struct dev_match_result)) {
1648 			bzero(&cdm->pos, sizeof(cdm->pos));
1649 			cdm->pos.position_type =
1650 				CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
1651 
1652 			cdm->pos.cookie.bus = bus;
1653 			cdm->pos.generations[CAM_BUS_GENERATION]=
1654 				xsoftc.bus_generation;
1655 			cdm->status = CAM_DEV_MATCH_MORE;
1656 			return(0);
1657 		}
1658 		j = cdm->num_matches;
1659 		cdm->num_matches++;
1660 		cdm->matches[j].type = DEV_MATCH_BUS;
1661 		cdm->matches[j].result.bus_result.path_id = bus->path_id;
1662 		cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
1663 		cdm->matches[j].result.bus_result.unit_number =
1664 			bus->sim->unit_number;
1665 		strlcpy(cdm->matches[j].result.bus_result.dev_name,
1666 			bus->sim->sim_name,
1667 			sizeof(cdm->matches[j].result.bus_result.dev_name));
1668 	}
1669 
1670 	/*
1671 	 * If the user is only interested in buses, there's no
1672 	 * reason to descend to the next level in the tree.
1673 	 */
1674 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1675 		return(1);
1676 
1677 	/*
1678 	 * If there is a target generation recorded, check it to
1679 	 * make sure the target list hasn't changed.
1680 	 */
1681 	mtx_lock(&bus->eb_mtx);
1682 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1683 	 && (cdm->pos.cookie.bus == bus)
1684 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1685 	 && (cdm->pos.cookie.target != NULL)) {
1686 		if ((cdm->pos.generations[CAM_TARGET_GENERATION] !=
1687 		    bus->generation)) {
1688 			mtx_unlock(&bus->eb_mtx);
1689 			cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1690 			return (0);
1691 		}
1692 		target = (struct cam_et *)cdm->pos.cookie.target;
1693 		target->refcount++;
1694 	} else
1695 		target = NULL;
1696 	mtx_unlock(&bus->eb_mtx);
1697 
1698 	return (xpttargettraverse(bus, target, xptedttargetfunc, arg));
1699 }
1700 
1701 static int
1702 xptedttargetfunc(struct cam_et *target, void *arg)
1703 {
1704 	struct ccb_dev_match *cdm;
1705 	struct cam_eb *bus;
1706 	struct cam_ed *device;
1707 
1708 	cdm = (struct ccb_dev_match *)arg;
1709 	bus = target->bus;
1710 
1711 	/*
1712 	 * If there is a device list generation recorded, check it to
1713 	 * make sure the device list hasn't changed.
1714 	 */
1715 	mtx_lock(&bus->eb_mtx);
1716 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1717 	 && (cdm->pos.cookie.bus == bus)
1718 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1719 	 && (cdm->pos.cookie.target == target)
1720 	 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1721 	 && (cdm->pos.cookie.device != NULL)) {
1722 		if (cdm->pos.generations[CAM_DEV_GENERATION] !=
1723 		    target->generation) {
1724 			mtx_unlock(&bus->eb_mtx);
1725 			cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1726 			return(0);
1727 		}
1728 		device = (struct cam_ed *)cdm->pos.cookie.device;
1729 		device->refcount++;
1730 	} else
1731 		device = NULL;
1732 	mtx_unlock(&bus->eb_mtx);
1733 
1734 	return (xptdevicetraverse(target, device, xptedtdevicefunc, arg));
1735 }
1736 
1737 static int
1738 xptedtdevicefunc(struct cam_ed *device, void *arg)
1739 {
1740 	struct cam_eb *bus;
1741 	struct cam_periph *periph;
1742 	struct ccb_dev_match *cdm;
1743 	dev_match_ret retval;
1744 
1745 	cdm = (struct ccb_dev_match *)arg;
1746 	bus = device->target->bus;
1747 
1748 	/*
1749 	 * If our position is for something deeper in the tree, that means
1750 	 * that we've already seen this node.  So, we keep going down.
1751 	 */
1752 	if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1753 	 && (cdm->pos.cookie.device == device)
1754 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1755 	 && (cdm->pos.cookie.periph != NULL))
1756 		retval = DM_RET_DESCEND;
1757 	else
1758 		retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
1759 					device);
1760 
1761 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1762 		cdm->status = CAM_DEV_MATCH_ERROR;
1763 		return(0);
1764 	}
1765 
1766 	/*
1767 	 * If the copy flag is set, copy this device out.
1768 	 */
1769 	if (retval & DM_RET_COPY) {
1770 		int spaceleft, j;
1771 
1772 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
1773 			sizeof(struct dev_match_result));
1774 
1775 		/*
1776 		 * If we don't have enough space to put in another
1777 		 * match result, save our position and tell the
1778 		 * user there are more devices to check.
1779 		 */
1780 		if (spaceleft < sizeof(struct dev_match_result)) {
1781 			bzero(&cdm->pos, sizeof(cdm->pos));
1782 			cdm->pos.position_type =
1783 				CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1784 				CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
1785 
1786 			cdm->pos.cookie.bus = device->target->bus;
1787 			cdm->pos.generations[CAM_BUS_GENERATION]=
1788 				xsoftc.bus_generation;
1789 			cdm->pos.cookie.target = device->target;
1790 			cdm->pos.generations[CAM_TARGET_GENERATION] =
1791 				device->target->bus->generation;
1792 			cdm->pos.cookie.device = device;
1793 			cdm->pos.generations[CAM_DEV_GENERATION] =
1794 				device->target->generation;
1795 			cdm->status = CAM_DEV_MATCH_MORE;
1796 			return(0);
1797 		}
1798 		j = cdm->num_matches;
1799 		cdm->num_matches++;
1800 		cdm->matches[j].type = DEV_MATCH_DEVICE;
1801 		cdm->matches[j].result.device_result.path_id =
1802 			device->target->bus->path_id;
1803 		cdm->matches[j].result.device_result.target_id =
1804 			device->target->target_id;
1805 		cdm->matches[j].result.device_result.target_lun =
1806 			device->lun_id;
1807 		cdm->matches[j].result.device_result.protocol =
1808 			device->protocol;
1809 		bcopy(&device->inq_data,
1810 		      &cdm->matches[j].result.device_result.inq_data,
1811 		      sizeof(struct scsi_inquiry_data));
1812 		bcopy(&device->ident_data,
1813 		      &cdm->matches[j].result.device_result.ident_data,
1814 		      sizeof(struct ata_params));
1815 
1816 		/* Let the user know whether this device is unconfigured */
1817 		if (device->flags & CAM_DEV_UNCONFIGURED)
1818 			cdm->matches[j].result.device_result.flags =
1819 				DEV_RESULT_UNCONFIGURED;
1820 		else
1821 			cdm->matches[j].result.device_result.flags =
1822 				DEV_RESULT_NOFLAG;
1823 	}
1824 
1825 	/*
1826 	 * If the user isn't interested in peripherals, don't descend
1827 	 * the tree any further.
1828 	 */
1829 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1830 		return(1);
1831 
1832 	/*
1833 	 * If there is a peripheral list generation recorded, make sure
1834 	 * it hasn't changed.
1835 	 */
1836 	xpt_lock_buses();
1837 	mtx_lock(&bus->eb_mtx);
1838 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1839 	 && (cdm->pos.cookie.bus == bus)
1840 	 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1841 	 && (cdm->pos.cookie.target == device->target)
1842 	 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1843 	 && (cdm->pos.cookie.device == device)
1844 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1845 	 && (cdm->pos.cookie.periph != NULL)) {
1846 		if (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1847 		    device->generation) {
1848 			mtx_unlock(&bus->eb_mtx);
1849 			xpt_unlock_buses();
1850 			cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1851 			return(0);
1852 		}
1853 		periph = (struct cam_periph *)cdm->pos.cookie.periph;
1854 		periph->refcount++;
1855 	} else
1856 		periph = NULL;
1857 	mtx_unlock(&bus->eb_mtx);
1858 	xpt_unlock_buses();
1859 
1860 	return (xptperiphtraverse(device, periph, xptedtperiphfunc, arg));
1861 }
1862 
1863 static int
1864 xptedtperiphfunc(struct cam_periph *periph, void *arg)
1865 {
1866 	struct ccb_dev_match *cdm;
1867 	dev_match_ret retval;
1868 
1869 	cdm = (struct ccb_dev_match *)arg;
1870 
1871 	retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1872 
1873 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1874 		cdm->status = CAM_DEV_MATCH_ERROR;
1875 		return(0);
1876 	}
1877 
1878 	/*
1879 	 * If the copy flag is set, copy this peripheral out.
1880 	 */
1881 	if (retval & DM_RET_COPY) {
1882 		int spaceleft, j;
1883 		size_t l;
1884 
1885 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
1886 			sizeof(struct dev_match_result));
1887 
1888 		/*
1889 		 * If we don't have enough space to put in another
1890 		 * match result, save our position and tell the
1891 		 * user there are more devices to check.
1892 		 */
1893 		if (spaceleft < sizeof(struct dev_match_result)) {
1894 			bzero(&cdm->pos, sizeof(cdm->pos));
1895 			cdm->pos.position_type =
1896 				CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1897 				CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
1898 				CAM_DEV_POS_PERIPH;
1899 
1900 			cdm->pos.cookie.bus = periph->path->bus;
1901 			cdm->pos.generations[CAM_BUS_GENERATION]=
1902 				xsoftc.bus_generation;
1903 			cdm->pos.cookie.target = periph->path->target;
1904 			cdm->pos.generations[CAM_TARGET_GENERATION] =
1905 				periph->path->bus->generation;
1906 			cdm->pos.cookie.device = periph->path->device;
1907 			cdm->pos.generations[CAM_DEV_GENERATION] =
1908 				periph->path->target->generation;
1909 			cdm->pos.cookie.periph = periph;
1910 			cdm->pos.generations[CAM_PERIPH_GENERATION] =
1911 				periph->path->device->generation;
1912 			cdm->status = CAM_DEV_MATCH_MORE;
1913 			return(0);
1914 		}
1915 
1916 		j = cdm->num_matches;
1917 		cdm->num_matches++;
1918 		cdm->matches[j].type = DEV_MATCH_PERIPH;
1919 		cdm->matches[j].result.periph_result.path_id =
1920 			periph->path->bus->path_id;
1921 		cdm->matches[j].result.periph_result.target_id =
1922 			periph->path->target->target_id;
1923 		cdm->matches[j].result.periph_result.target_lun =
1924 			periph->path->device->lun_id;
1925 		cdm->matches[j].result.periph_result.unit_number =
1926 			periph->unit_number;
1927 		l = sizeof(cdm->matches[j].result.periph_result.periph_name);
1928 		strlcpy(cdm->matches[j].result.periph_result.periph_name,
1929 			periph->periph_name, l);
1930 	}
1931 
1932 	return(1);
1933 }
1934 
1935 static int
1936 xptedtmatch(struct ccb_dev_match *cdm)
1937 {
1938 	struct cam_eb *bus;
1939 	int ret;
1940 
1941 	cdm->num_matches = 0;
1942 
1943 	/*
1944 	 * Check the bus list generation.  If it has changed, the user
1945 	 * needs to reset everything and start over.
1946 	 */
1947 	xpt_lock_buses();
1948 	if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1949 	 && (cdm->pos.cookie.bus != NULL)) {
1950 		if (cdm->pos.generations[CAM_BUS_GENERATION] !=
1951 		    xsoftc.bus_generation) {
1952 			xpt_unlock_buses();
1953 			cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1954 			return(0);
1955 		}
1956 		bus = (struct cam_eb *)cdm->pos.cookie.bus;
1957 		bus->refcount++;
1958 	} else
1959 		bus = NULL;
1960 	xpt_unlock_buses();
1961 
1962 	ret = xptbustraverse(bus, xptedtbusfunc, cdm);
1963 
1964 	/*
1965 	 * If we get back 0, that means that we had to stop before fully
1966 	 * traversing the EDT.  It also means that one of the subroutines
1967 	 * has set the status field to the proper value.  If we get back 1,
1968 	 * we've fully traversed the EDT and copied out any matching entries.
1969 	 */
1970 	if (ret == 1)
1971 		cdm->status = CAM_DEV_MATCH_LAST;
1972 
1973 	return(ret);
1974 }
1975 
1976 static int
1977 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
1978 {
1979 	struct cam_periph *periph;
1980 	struct ccb_dev_match *cdm;
1981 
1982 	cdm = (struct ccb_dev_match *)arg;
1983 
1984 	xpt_lock_buses();
1985 	if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1986 	 && (cdm->pos.cookie.pdrv == pdrv)
1987 	 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1988 	 && (cdm->pos.cookie.periph != NULL)) {
1989 		if (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1990 		    (*pdrv)->generation) {
1991 			xpt_unlock_buses();
1992 			cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1993 			return(0);
1994 		}
1995 		periph = (struct cam_periph *)cdm->pos.cookie.periph;
1996 		periph->refcount++;
1997 	} else
1998 		periph = NULL;
1999 	xpt_unlock_buses();
2000 
2001 	return (xptpdperiphtraverse(pdrv, periph, xptplistperiphfunc, arg));
2002 }
2003 
2004 static int
2005 xptplistperiphfunc(struct cam_periph *periph, void *arg)
2006 {
2007 	struct ccb_dev_match *cdm;
2008 	dev_match_ret retval;
2009 
2010 	cdm = (struct ccb_dev_match *)arg;
2011 
2012 	retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2013 
2014 	if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2015 		cdm->status = CAM_DEV_MATCH_ERROR;
2016 		return(0);
2017 	}
2018 
2019 	/*
2020 	 * If the copy flag is set, copy this peripheral out.
2021 	 */
2022 	if (retval & DM_RET_COPY) {
2023 		int spaceleft, j;
2024 		size_t l;
2025 
2026 		spaceleft = cdm->match_buf_len - (cdm->num_matches *
2027 			sizeof(struct dev_match_result));
2028 
2029 		/*
2030 		 * If we don't have enough space to put in another
2031 		 * match result, save our position and tell the
2032 		 * user there are more devices to check.
2033 		 */
2034 		if (spaceleft < sizeof(struct dev_match_result)) {
2035 			struct periph_driver **pdrv;
2036 
2037 			pdrv = NULL;
2038 			bzero(&cdm->pos, sizeof(cdm->pos));
2039 			cdm->pos.position_type =
2040 				CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
2041 				CAM_DEV_POS_PERIPH;
2042 
2043 			/*
2044 			 * This may look a bit non-sensical, but it is
2045 			 * actually quite logical.  There are very few
2046 			 * peripheral drivers, and bloating every peripheral
2047 			 * structure with a pointer back to its parent
2048 			 * peripheral driver linker set entry would cost
2049 			 * more in the long run than doing this quick lookup.
2050 			 */
2051 			for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
2052 				if (strcmp((*pdrv)->driver_name,
2053 				    periph->periph_name) == 0)
2054 					break;
2055 			}
2056 
2057 			if (*pdrv == NULL) {
2058 				cdm->status = CAM_DEV_MATCH_ERROR;
2059 				return(0);
2060 			}
2061 
2062 			cdm->pos.cookie.pdrv = pdrv;
2063 			/*
2064 			 * The periph generation slot does double duty, as
2065 			 * does the periph pointer slot.  They are used for
2066 			 * both edt and pdrv lookups and positioning.
2067 			 */
2068 			cdm->pos.cookie.periph = periph;
2069 			cdm->pos.generations[CAM_PERIPH_GENERATION] =
2070 				(*pdrv)->generation;
2071 			cdm->status = CAM_DEV_MATCH_MORE;
2072 			return(0);
2073 		}
2074 
2075 		j = cdm->num_matches;
2076 		cdm->num_matches++;
2077 		cdm->matches[j].type = DEV_MATCH_PERIPH;
2078 		cdm->matches[j].result.periph_result.path_id =
2079 			periph->path->bus->path_id;
2080 
2081 		/*
2082 		 * The transport layer peripheral doesn't have a target or
2083 		 * lun.
2084 		 */
2085 		if (periph->path->target)
2086 			cdm->matches[j].result.periph_result.target_id =
2087 				periph->path->target->target_id;
2088 		else
2089 			cdm->matches[j].result.periph_result.target_id =
2090 				CAM_TARGET_WILDCARD;
2091 
2092 		if (periph->path->device)
2093 			cdm->matches[j].result.periph_result.target_lun =
2094 				periph->path->device->lun_id;
2095 		else
2096 			cdm->matches[j].result.periph_result.target_lun =
2097 				CAM_LUN_WILDCARD;
2098 
2099 		cdm->matches[j].result.periph_result.unit_number =
2100 			periph->unit_number;
2101 		l = sizeof(cdm->matches[j].result.periph_result.periph_name);
2102 		strlcpy(cdm->matches[j].result.periph_result.periph_name,
2103 			periph->periph_name, l);
2104 	}
2105 
2106 	return(1);
2107 }
2108 
2109 static int
2110 xptperiphlistmatch(struct ccb_dev_match *cdm)
2111 {
2112 	int ret;
2113 
2114 	cdm->num_matches = 0;
2115 
2116 	/*
2117 	 * At this point in the edt traversal function, we check the bus
2118 	 * list generation to make sure that no buses have been added or
2119 	 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2120 	 * For the peripheral driver list traversal function, however, we
2121 	 * don't have to worry about new peripheral driver types coming or
2122 	 * going; they're in a linker set, and therefore can't change
2123 	 * without a recompile.
2124 	 */
2125 
2126 	if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2127 	 && (cdm->pos.cookie.pdrv != NULL))
2128 		ret = xptpdrvtraverse(
2129 				(struct periph_driver **)cdm->pos.cookie.pdrv,
2130 				xptplistpdrvfunc, cdm);
2131 	else
2132 		ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2133 
2134 	/*
2135 	 * If we get back 0, that means that we had to stop before fully
2136 	 * traversing the peripheral driver tree.  It also means that one of
2137 	 * the subroutines has set the status field to the proper value.  If
2138 	 * we get back 1, we've fully traversed the EDT and copied out any
2139 	 * matching entries.
2140 	 */
2141 	if (ret == 1)
2142 		cdm->status = CAM_DEV_MATCH_LAST;
2143 
2144 	return(ret);
2145 }
2146 
2147 static int
2148 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2149 {
2150 	struct cam_eb *bus, *next_bus;
2151 	int retval;
2152 
2153 	retval = 1;
2154 	if (start_bus)
2155 		bus = start_bus;
2156 	else {
2157 		xpt_lock_buses();
2158 		bus = TAILQ_FIRST(&xsoftc.xpt_busses);
2159 		if (bus == NULL) {
2160 			xpt_unlock_buses();
2161 			return (retval);
2162 		}
2163 		bus->refcount++;
2164 		xpt_unlock_buses();
2165 	}
2166 	for (; bus != NULL; bus = next_bus) {
2167 		retval = tr_func(bus, arg);
2168 		if (retval == 0) {
2169 			xpt_release_bus(bus);
2170 			break;
2171 		}
2172 		xpt_lock_buses();
2173 		next_bus = TAILQ_NEXT(bus, links);
2174 		if (next_bus)
2175 			next_bus->refcount++;
2176 		xpt_unlock_buses();
2177 		xpt_release_bus(bus);
2178 	}
2179 	return(retval);
2180 }
2181 
2182 static int
2183 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2184 		  xpt_targetfunc_t *tr_func, void *arg)
2185 {
2186 	struct cam_et *target, *next_target;
2187 	int retval;
2188 
2189 	retval = 1;
2190 	if (start_target)
2191 		target = start_target;
2192 	else {
2193 		mtx_lock(&bus->eb_mtx);
2194 		target = TAILQ_FIRST(&bus->et_entries);
2195 		if (target == NULL) {
2196 			mtx_unlock(&bus->eb_mtx);
2197 			return (retval);
2198 		}
2199 		target->refcount++;
2200 		mtx_unlock(&bus->eb_mtx);
2201 	}
2202 	for (; target != NULL; target = next_target) {
2203 		retval = tr_func(target, arg);
2204 		if (retval == 0) {
2205 			xpt_release_target(target);
2206 			break;
2207 		}
2208 		mtx_lock(&bus->eb_mtx);
2209 		next_target = TAILQ_NEXT(target, links);
2210 		if (next_target)
2211 			next_target->refcount++;
2212 		mtx_unlock(&bus->eb_mtx);
2213 		xpt_release_target(target);
2214 	}
2215 	return(retval);
2216 }
2217 
2218 static int
2219 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2220 		  xpt_devicefunc_t *tr_func, void *arg)
2221 {
2222 	struct cam_eb *bus;
2223 	struct cam_ed *device, *next_device;
2224 	int retval;
2225 
2226 	retval = 1;
2227 	bus = target->bus;
2228 	if (start_device)
2229 		device = start_device;
2230 	else {
2231 		mtx_lock(&bus->eb_mtx);
2232 		device = TAILQ_FIRST(&target->ed_entries);
2233 		if (device == NULL) {
2234 			mtx_unlock(&bus->eb_mtx);
2235 			return (retval);
2236 		}
2237 		device->refcount++;
2238 		mtx_unlock(&bus->eb_mtx);
2239 	}
2240 	for (; device != NULL; device = next_device) {
2241 		mtx_lock(&device->device_mtx);
2242 		retval = tr_func(device, arg);
2243 		mtx_unlock(&device->device_mtx);
2244 		if (retval == 0) {
2245 			xpt_release_device(device);
2246 			break;
2247 		}
2248 		mtx_lock(&bus->eb_mtx);
2249 		next_device = TAILQ_NEXT(device, links);
2250 		if (next_device)
2251 			next_device->refcount++;
2252 		mtx_unlock(&bus->eb_mtx);
2253 		xpt_release_device(device);
2254 	}
2255 	return(retval);
2256 }
2257 
2258 static int
2259 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2260 		  xpt_periphfunc_t *tr_func, void *arg)
2261 {
2262 	struct cam_eb *bus;
2263 	struct cam_periph *periph, *next_periph;
2264 	int retval;
2265 
2266 	retval = 1;
2267 
2268 	bus = device->target->bus;
2269 	if (start_periph)
2270 		periph = start_periph;
2271 	else {
2272 		xpt_lock_buses();
2273 		mtx_lock(&bus->eb_mtx);
2274 		periph = SLIST_FIRST(&device->periphs);
2275 		while (periph != NULL && (periph->flags & CAM_PERIPH_FREE) != 0)
2276 			periph = SLIST_NEXT(periph, periph_links);
2277 		if (periph == NULL) {
2278 			mtx_unlock(&bus->eb_mtx);
2279 			xpt_unlock_buses();
2280 			return (retval);
2281 		}
2282 		periph->refcount++;
2283 		mtx_unlock(&bus->eb_mtx);
2284 		xpt_unlock_buses();
2285 	}
2286 	for (; periph != NULL; periph = next_periph) {
2287 		retval = tr_func(periph, arg);
2288 		if (retval == 0) {
2289 			cam_periph_release_locked(periph);
2290 			break;
2291 		}
2292 		xpt_lock_buses();
2293 		mtx_lock(&bus->eb_mtx);
2294 		next_periph = SLIST_NEXT(periph, periph_links);
2295 		while (next_periph != NULL &&
2296 		    (next_periph->flags & CAM_PERIPH_FREE) != 0)
2297 			next_periph = SLIST_NEXT(next_periph, periph_links);
2298 		if (next_periph)
2299 			next_periph->refcount++;
2300 		mtx_unlock(&bus->eb_mtx);
2301 		xpt_unlock_buses();
2302 		cam_periph_release_locked(periph);
2303 	}
2304 	return(retval);
2305 }
2306 
2307 static int
2308 xptpdrvtraverse(struct periph_driver **start_pdrv,
2309 		xpt_pdrvfunc_t *tr_func, void *arg)
2310 {
2311 	struct periph_driver **pdrv;
2312 	int retval;
2313 
2314 	retval = 1;
2315 
2316 	/*
2317 	 * We don't traverse the peripheral driver list like we do the
2318 	 * other lists, because it is a linker set, and therefore cannot be
2319 	 * changed during runtime.  If the peripheral driver list is ever
2320 	 * re-done to be something other than a linker set (i.e. it can
2321 	 * change while the system is running), the list traversal should
2322 	 * be modified to work like the other traversal functions.
2323 	 */
2324 	for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2325 	     *pdrv != NULL; pdrv++) {
2326 		retval = tr_func(pdrv, arg);
2327 
2328 		if (retval == 0)
2329 			return(retval);
2330 	}
2331 
2332 	return(retval);
2333 }
2334 
2335 static int
2336 xptpdperiphtraverse(struct periph_driver **pdrv,
2337 		    struct cam_periph *start_periph,
2338 		    xpt_periphfunc_t *tr_func, void *arg)
2339 {
2340 	struct cam_periph *periph, *next_periph;
2341 	int retval;
2342 
2343 	retval = 1;
2344 
2345 	if (start_periph)
2346 		periph = start_periph;
2347 	else {
2348 		xpt_lock_buses();
2349 		periph = TAILQ_FIRST(&(*pdrv)->units);
2350 		while (periph != NULL && (periph->flags & CAM_PERIPH_FREE) != 0)
2351 			periph = TAILQ_NEXT(periph, unit_links);
2352 		if (periph == NULL) {
2353 			xpt_unlock_buses();
2354 			return (retval);
2355 		}
2356 		periph->refcount++;
2357 		xpt_unlock_buses();
2358 	}
2359 	for (; periph != NULL; periph = next_periph) {
2360 		cam_periph_lock(periph);
2361 		retval = tr_func(periph, arg);
2362 		cam_periph_unlock(periph);
2363 		if (retval == 0) {
2364 			cam_periph_release(periph);
2365 			break;
2366 		}
2367 		xpt_lock_buses();
2368 		next_periph = TAILQ_NEXT(periph, unit_links);
2369 		while (next_periph != NULL &&
2370 		    (next_periph->flags & CAM_PERIPH_FREE) != 0)
2371 			next_periph = TAILQ_NEXT(next_periph, unit_links);
2372 		if (next_periph)
2373 			next_periph->refcount++;
2374 		xpt_unlock_buses();
2375 		cam_periph_release(periph);
2376 	}
2377 	return(retval);
2378 }
2379 
2380 static int
2381 xptdefbusfunc(struct cam_eb *bus, void *arg)
2382 {
2383 	struct xpt_traverse_config *tr_config;
2384 
2385 	tr_config = (struct xpt_traverse_config *)arg;
2386 
2387 	if (tr_config->depth == XPT_DEPTH_BUS) {
2388 		xpt_busfunc_t *tr_func;
2389 
2390 		tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2391 
2392 		return(tr_func(bus, tr_config->tr_arg));
2393 	} else
2394 		return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2395 }
2396 
2397 static int
2398 xptdeftargetfunc(struct cam_et *target, void *arg)
2399 {
2400 	struct xpt_traverse_config *tr_config;
2401 
2402 	tr_config = (struct xpt_traverse_config *)arg;
2403 
2404 	if (tr_config->depth == XPT_DEPTH_TARGET) {
2405 		xpt_targetfunc_t *tr_func;
2406 
2407 		tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2408 
2409 		return(tr_func(target, tr_config->tr_arg));
2410 	} else
2411 		return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2412 }
2413 
2414 static int
2415 xptdefdevicefunc(struct cam_ed *device, void *arg)
2416 {
2417 	struct xpt_traverse_config *tr_config;
2418 
2419 	tr_config = (struct xpt_traverse_config *)arg;
2420 
2421 	if (tr_config->depth == XPT_DEPTH_DEVICE) {
2422 		xpt_devicefunc_t *tr_func;
2423 
2424 		tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2425 
2426 		return(tr_func(device, tr_config->tr_arg));
2427 	} else
2428 		return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2429 }
2430 
2431 static int
2432 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2433 {
2434 	struct xpt_traverse_config *tr_config;
2435 	xpt_periphfunc_t *tr_func;
2436 
2437 	tr_config = (struct xpt_traverse_config *)arg;
2438 
2439 	tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2440 
2441 	/*
2442 	 * Unlike the other default functions, we don't check for depth
2443 	 * here.  The peripheral driver level is the last level in the EDT,
2444 	 * so if we're here, we should execute the function in question.
2445 	 */
2446 	return(tr_func(periph, tr_config->tr_arg));
2447 }
2448 
2449 /*
2450  * Execute the given function for every bus in the EDT.
2451  */
2452 static int
2453 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2454 {
2455 	struct xpt_traverse_config tr_config;
2456 
2457 	tr_config.depth = XPT_DEPTH_BUS;
2458 	tr_config.tr_func = tr_func;
2459 	tr_config.tr_arg = arg;
2460 
2461 	return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2462 }
2463 
2464 /*
2465  * Execute the given function for every device in the EDT.
2466  */
2467 static int
2468 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2469 {
2470 	struct xpt_traverse_config tr_config;
2471 
2472 	tr_config.depth = XPT_DEPTH_DEVICE;
2473 	tr_config.tr_func = tr_func;
2474 	tr_config.tr_arg = arg;
2475 
2476 	return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2477 }
2478 
2479 static int
2480 xptsetasyncfunc(struct cam_ed *device, void *arg)
2481 {
2482 	struct cam_path path;
2483 	struct ccb_getdev cgd;
2484 	struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2485 
2486 	/*
2487 	 * Don't report unconfigured devices (Wildcard devs,
2488 	 * devices only for target mode, device instances
2489 	 * that have been invalidated but are waiting for
2490 	 * their last reference count to be released).
2491 	 */
2492 	if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2493 		return (1);
2494 
2495 	xpt_compile_path(&path,
2496 			 NULL,
2497 			 device->target->bus->path_id,
2498 			 device->target->target_id,
2499 			 device->lun_id);
2500 	xpt_gdev_type(&cgd, &path);
2501 	CAM_PROBE4(xpt, async__cb, csa->callback_arg,
2502 	    AC_FOUND_DEVICE, &path, &cgd);
2503 	csa->callback(csa->callback_arg,
2504 			    AC_FOUND_DEVICE,
2505 			    &path, &cgd);
2506 	xpt_release_path(&path);
2507 
2508 	return(1);
2509 }
2510 
2511 static int
2512 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2513 {
2514 	struct cam_path path;
2515 	struct ccb_pathinq cpi;
2516 	struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2517 
2518 	xpt_compile_path(&path, /*periph*/NULL,
2519 			 bus->path_id,
2520 			 CAM_TARGET_WILDCARD,
2521 			 CAM_LUN_WILDCARD);
2522 	xpt_path_lock(&path);
2523 	xpt_path_inq(&cpi, &path);
2524 	CAM_PROBE4(xpt, async__cb, csa->callback_arg,
2525 	    AC_PATH_REGISTERED, &path, &cpi);
2526 	csa->callback(csa->callback_arg,
2527 			    AC_PATH_REGISTERED,
2528 			    &path, &cpi);
2529 	xpt_path_unlock(&path);
2530 	xpt_release_path(&path);
2531 
2532 	return(1);
2533 }
2534 
2535 void
2536 xpt_action(union ccb *start_ccb)
2537 {
2538 
2539 	CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
2540 	    ("xpt_action: func %#x %s\n", start_ccb->ccb_h.func_code,
2541 		xpt_action_name(start_ccb->ccb_h.func_code)));
2542 
2543 	/*
2544 	 * Either it isn't queued, or it has a real priority. There still too
2545 	 * many places that reuse CCBs with a real priority to do immediate
2546 	 * queries to do the other side of this assert.
2547 	 */
2548 	KASSERT((start_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0 ||
2549 	    start_ccb->ccb_h.pinfo.priority != CAM_PRIORITY_NONE,
2550 	    ("%s: queued ccb and CAM_PRIORITY_NONE illegal.", __func__));
2551 
2552 	CAM_PROBE1(xpt, action, start_ccb);
2553 	start_ccb->ccb_h.status = CAM_REQ_INPROG;
2554 	(*(start_ccb->ccb_h.path->bus->xport->ops->action))(start_ccb);
2555 }
2556 
2557 void
2558 xpt_action_default(union ccb *start_ccb)
2559 {
2560 	struct cam_path *path;
2561 	struct cam_sim *sim;
2562 	struct mtx *mtx;
2563 
2564 	path = start_ccb->ccb_h.path;
2565 	CAM_DEBUG(path, CAM_DEBUG_TRACE,
2566 	    ("xpt_action_default: func %#x %s\n", start_ccb->ccb_h.func_code,
2567 		xpt_action_name(start_ccb->ccb_h.func_code)));
2568 
2569 	switch (start_ccb->ccb_h.func_code) {
2570 	case XPT_SCSI_IO:
2571 	{
2572 		struct cam_ed *device;
2573 
2574 		/*
2575 		 * For the sake of compatibility with SCSI-1
2576 		 * devices that may not understand the identify
2577 		 * message, we include lun information in the
2578 		 * second byte of all commands.  SCSI-1 specifies
2579 		 * that luns are a 3 bit value and reserves only 3
2580 		 * bits for lun information in the CDB.  Later
2581 		 * revisions of the SCSI spec allow for more than 8
2582 		 * luns, but have deprecated lun information in the
2583 		 * CDB.  So, if the lun won't fit, we must omit.
2584 		 *
2585 		 * Also be aware that during initial probing for devices,
2586 		 * the inquiry information is unknown but initialized to 0.
2587 		 * This means that this code will be exercised while probing
2588 		 * devices with an ANSI revision greater than 2.
2589 		 */
2590 		device = path->device;
2591 		if (device->protocol_version <= SCSI_REV_2
2592 		 && start_ccb->ccb_h.target_lun < 8
2593 		 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2594 			start_ccb->csio.cdb_io.cdb_bytes[1] |=
2595 			    start_ccb->ccb_h.target_lun << 5;
2596 		}
2597 		start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2598 	}
2599 	/* FALLTHROUGH */
2600 	case XPT_TARGET_IO:
2601 	case XPT_CONT_TARGET_IO:
2602 		start_ccb->csio.sense_resid = 0;
2603 		start_ccb->csio.resid = 0;
2604 		/* FALLTHROUGH */
2605 	case XPT_ATA_IO:
2606 		if (start_ccb->ccb_h.func_code == XPT_ATA_IO)
2607 			start_ccb->ataio.resid = 0;
2608 		/* FALLTHROUGH */
2609 	case XPT_NVME_IO:
2610 	case XPT_NVME_ADMIN:
2611 	case XPT_MMC_IO:
2612 	case XPT_MMC_GET_TRAN_SETTINGS:
2613 	case XPT_MMC_SET_TRAN_SETTINGS:
2614 	case XPT_RESET_DEV:
2615 	case XPT_ENG_EXEC:
2616 	case XPT_SMP_IO:
2617 	{
2618 		struct cam_devq *devq;
2619 
2620 		devq = path->bus->sim->devq;
2621 		mtx_lock(&devq->send_mtx);
2622 		cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2623 		if (xpt_schedule_devq(devq, path->device) != 0)
2624 			xpt_run_devq(devq);
2625 		mtx_unlock(&devq->send_mtx);
2626 		break;
2627 	}
2628 	case XPT_CALC_GEOMETRY:
2629 		/* Filter out garbage */
2630 		if (start_ccb->ccg.block_size == 0
2631 		 || start_ccb->ccg.volume_size == 0) {
2632 			start_ccb->ccg.cylinders = 0;
2633 			start_ccb->ccg.heads = 0;
2634 			start_ccb->ccg.secs_per_track = 0;
2635 			start_ccb->ccb_h.status = CAM_REQ_CMP;
2636 			break;
2637 		}
2638 		goto call_sim;
2639 	case XPT_ABORT:
2640 	{
2641 		union ccb* abort_ccb;
2642 
2643 		abort_ccb = start_ccb->cab.abort_ccb;
2644 		if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
2645 			struct cam_ed *device;
2646 			struct cam_devq *devq;
2647 
2648 			device = abort_ccb->ccb_h.path->device;
2649 			devq = device->sim->devq;
2650 
2651 			mtx_lock(&devq->send_mtx);
2652 			if (abort_ccb->ccb_h.pinfo.index > 0) {
2653 				cam_ccbq_remove_ccb(&device->ccbq, abort_ccb);
2654 				abort_ccb->ccb_h.status =
2655 				    CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2656 				xpt_freeze_devq_device(device, 1);
2657 				mtx_unlock(&devq->send_mtx);
2658 				xpt_done(abort_ccb);
2659 				start_ccb->ccb_h.status = CAM_REQ_CMP;
2660 				break;
2661 			}
2662 			mtx_unlock(&devq->send_mtx);
2663 
2664 			if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
2665 			 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
2666 				/*
2667 				 * We've caught this ccb en route to
2668 				 * the SIM.  Flag it for abort and the
2669 				 * SIM will do so just before starting
2670 				 * real work on the CCB.
2671 				 */
2672 				abort_ccb->ccb_h.status =
2673 				    CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2674 				xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2675 				start_ccb->ccb_h.status = CAM_REQ_CMP;
2676 				break;
2677 			}
2678 		}
2679 		if (XPT_FC_IS_QUEUED(abort_ccb)
2680 		 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
2681 			/*
2682 			 * It's already completed but waiting
2683 			 * for our SWI to get to it.
2684 			 */
2685 			start_ccb->ccb_h.status = CAM_UA_ABORT;
2686 			break;
2687 		}
2688 		/*
2689 		 * If we weren't able to take care of the abort request
2690 		 * in the XPT, pass the request down to the SIM for processing.
2691 		 */
2692 	}
2693 	/* FALLTHROUGH */
2694 	case XPT_ACCEPT_TARGET_IO:
2695 	case XPT_EN_LUN:
2696 	case XPT_IMMED_NOTIFY:
2697 	case XPT_NOTIFY_ACK:
2698 	case XPT_RESET_BUS:
2699 	case XPT_IMMEDIATE_NOTIFY:
2700 	case XPT_NOTIFY_ACKNOWLEDGE:
2701 	case XPT_GET_SIM_KNOB_OLD:
2702 	case XPT_GET_SIM_KNOB:
2703 	case XPT_SET_SIM_KNOB:
2704 	case XPT_GET_TRAN_SETTINGS:
2705 	case XPT_SET_TRAN_SETTINGS:
2706 	case XPT_PATH_INQ:
2707 call_sim:
2708 		sim = path->bus->sim;
2709 		mtx = sim->mtx;
2710 		if (mtx && !mtx_owned(mtx))
2711 			mtx_lock(mtx);
2712 		else
2713 			mtx = NULL;
2714 
2715 		CAM_DEBUG(path, CAM_DEBUG_TRACE,
2716 		    ("Calling sim->sim_action(): func=%#x\n", start_ccb->ccb_h.func_code));
2717 		(*(sim->sim_action))(sim, start_ccb);
2718 		CAM_DEBUG(path, CAM_DEBUG_TRACE,
2719 		    ("sim->sim_action returned: status=%#x\n", start_ccb->ccb_h.status));
2720 		if (mtx)
2721 			mtx_unlock(mtx);
2722 		break;
2723 	case XPT_PATH_STATS:
2724 		start_ccb->cpis.last_reset = path->bus->last_reset;
2725 		start_ccb->ccb_h.status = CAM_REQ_CMP;
2726 		break;
2727 	case XPT_GDEV_TYPE:
2728 	{
2729 		struct cam_ed *dev;
2730 
2731 		dev = path->device;
2732 		if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2733 			start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2734 		} else {
2735 			struct ccb_getdev *cgd;
2736 
2737 			cgd = &start_ccb->cgd;
2738 			cgd->protocol = dev->protocol;
2739 			cgd->inq_data = dev->inq_data;
2740 			cgd->ident_data = dev->ident_data;
2741 			cgd->inq_flags = dev->inq_flags;
2742 			cgd->ccb_h.status = CAM_REQ_CMP;
2743 			cgd->serial_num_len = dev->serial_num_len;
2744 			if ((dev->serial_num_len > 0)
2745 			 && (dev->serial_num != NULL))
2746 				bcopy(dev->serial_num, cgd->serial_num,
2747 				      dev->serial_num_len);
2748 		}
2749 		break;
2750 	}
2751 	case XPT_GDEV_STATS:
2752 	{
2753 		struct ccb_getdevstats *cgds = &start_ccb->cgds;
2754 		struct cam_ed *dev = path->device;
2755 		struct cam_eb *bus = path->bus;
2756 		struct cam_et *tar = path->target;
2757 		struct cam_devq *devq = bus->sim->devq;
2758 
2759 		mtx_lock(&devq->send_mtx);
2760 		cgds->dev_openings = dev->ccbq.dev_openings;
2761 		cgds->dev_active = dev->ccbq.dev_active;
2762 		cgds->allocated = dev->ccbq.allocated;
2763 		cgds->queued = cam_ccbq_pending_ccb_count(&dev->ccbq);
2764 		cgds->held = cgds->allocated - cgds->dev_active - cgds->queued;
2765 		cgds->last_reset = tar->last_reset;
2766 		cgds->maxtags = dev->maxtags;
2767 		cgds->mintags = dev->mintags;
2768 		if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
2769 			cgds->last_reset = bus->last_reset;
2770 		mtx_unlock(&devq->send_mtx);
2771 		cgds->ccb_h.status = CAM_REQ_CMP;
2772 		break;
2773 	}
2774 	case XPT_GDEVLIST:
2775 	{
2776 		struct cam_periph	*nperiph;
2777 		struct periph_list	*periph_head;
2778 		struct ccb_getdevlist	*cgdl;
2779 		u_int			i;
2780 		struct cam_ed		*device;
2781 		bool			found;
2782 
2783 		found = false;
2784 
2785 		/*
2786 		 * Don't want anyone mucking with our data.
2787 		 */
2788 		device = path->device;
2789 		periph_head = &device->periphs;
2790 		cgdl = &start_ccb->cgdl;
2791 		start_ccb->ccb_h.status = CAM_REQ_CMP;
2792 
2793 		/*
2794 		 * Check and see if the list has changed since the user
2795 		 * last requested a list member.  If so, tell them that the
2796 		 * list has changed, and therefore they need to start over
2797 		 * from the beginning.
2798 		 */
2799 		if ((cgdl->index != 0) &&
2800 		    (cgdl->generation != device->generation)) {
2801 			cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
2802 			break;
2803 		}
2804 
2805 		/*
2806 		 * Traverse the list of peripherals and attempt to find
2807 		 * the requested peripheral.
2808 		 */
2809 		for (nperiph = SLIST_FIRST(periph_head), i = 0;
2810 		     (nperiph != NULL) && (i <= cgdl->index);
2811 		     nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
2812 			if (i == cgdl->index) {
2813 				strlcpy(cgdl->periph_name,
2814 					nperiph->periph_name,
2815 					sizeof(cgdl->periph_name));
2816 				cgdl->unit_number = nperiph->unit_number;
2817 				found = true;
2818 			}
2819 		}
2820 		if (!found) {
2821 			cgdl->status = CAM_GDEVLIST_ERROR;
2822 			break;
2823 		}
2824 
2825 		if (nperiph == NULL)
2826 			cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
2827 		else
2828 			cgdl->status = CAM_GDEVLIST_MORE_DEVS;
2829 
2830 		cgdl->index++;
2831 		cgdl->generation = device->generation;
2832 
2833 		break;
2834 	}
2835 	case XPT_DEV_MATCH:
2836 	{
2837 		dev_pos_type position_type;
2838 		struct ccb_dev_match *cdm;
2839 
2840 		cdm = &start_ccb->cdm;
2841 
2842 		/*
2843 		 * There are two ways of getting at information in the EDT.
2844 		 * The first way is via the primary EDT tree.  It starts
2845 		 * with a list of buses, then a list of targets on a bus,
2846 		 * then devices/luns on a target, and then peripherals on a
2847 		 * device/lun.  The "other" way is by the peripheral driver
2848 		 * lists.  The peripheral driver lists are organized by
2849 		 * peripheral driver.  (obviously)  So it makes sense to
2850 		 * use the peripheral driver list if the user is looking
2851 		 * for something like "da1", or all "da" devices.  If the
2852 		 * user is looking for something on a particular bus/target
2853 		 * or lun, it's generally better to go through the EDT tree.
2854 		 */
2855 
2856 		if (cdm->pos.position_type != CAM_DEV_POS_NONE)
2857 			position_type = cdm->pos.position_type;
2858 		else {
2859 			u_int i;
2860 
2861 			position_type = CAM_DEV_POS_NONE;
2862 
2863 			for (i = 0; i < cdm->num_patterns; i++) {
2864 				if ((cdm->patterns[i].type == DEV_MATCH_BUS)
2865 				 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
2866 					position_type = CAM_DEV_POS_EDT;
2867 					break;
2868 				}
2869 			}
2870 
2871 			if (cdm->num_patterns == 0)
2872 				position_type = CAM_DEV_POS_EDT;
2873 			else if (position_type == CAM_DEV_POS_NONE)
2874 				position_type = CAM_DEV_POS_PDRV;
2875 		}
2876 
2877 		switch(position_type & CAM_DEV_POS_TYPEMASK) {
2878 		case CAM_DEV_POS_EDT:
2879 			xptedtmatch(cdm);
2880 			break;
2881 		case CAM_DEV_POS_PDRV:
2882 			xptperiphlistmatch(cdm);
2883 			break;
2884 		default:
2885 			cdm->status = CAM_DEV_MATCH_ERROR;
2886 			break;
2887 		}
2888 
2889 		if (cdm->status == CAM_DEV_MATCH_ERROR)
2890 			start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2891 		else
2892 			start_ccb->ccb_h.status = CAM_REQ_CMP;
2893 
2894 		break;
2895 	}
2896 	case XPT_SASYNC_CB:
2897 	{
2898 		struct ccb_setasync *csa;
2899 		struct async_node *cur_entry;
2900 		struct async_list *async_head;
2901 		uint32_t added;
2902 
2903 		csa = &start_ccb->csa;
2904 		added = csa->event_enable;
2905 		async_head = &path->device->asyncs;
2906 
2907 		/*
2908 		 * If there is already an entry for us, simply
2909 		 * update it.
2910 		 */
2911 		cur_entry = SLIST_FIRST(async_head);
2912 		while (cur_entry != NULL) {
2913 			if ((cur_entry->callback_arg == csa->callback_arg)
2914 			 && (cur_entry->callback == csa->callback))
2915 				break;
2916 			cur_entry = SLIST_NEXT(cur_entry, links);
2917 		}
2918 
2919 		if (cur_entry != NULL) {
2920 		 	/*
2921 			 * If the request has no flags set,
2922 			 * remove the entry.
2923 			 */
2924 			added &= ~cur_entry->event_enable;
2925 			if (csa->event_enable == 0) {
2926 				SLIST_REMOVE(async_head, cur_entry,
2927 					     async_node, links);
2928 				xpt_release_device(path->device);
2929 				free(cur_entry, M_CAMXPT);
2930 			} else {
2931 				cur_entry->event_enable = csa->event_enable;
2932 			}
2933 			csa->event_enable = added;
2934 		} else {
2935 			cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
2936 					   M_NOWAIT);
2937 			if (cur_entry == NULL) {
2938 				csa->ccb_h.status = CAM_RESRC_UNAVAIL;
2939 				break;
2940 			}
2941 			cur_entry->event_enable = csa->event_enable;
2942 			cur_entry->event_lock = (path->bus->sim->mtx &&
2943 			    mtx_owned(path->bus->sim->mtx)) ? 1 : 0;
2944 			cur_entry->callback_arg = csa->callback_arg;
2945 			cur_entry->callback = csa->callback;
2946 			SLIST_INSERT_HEAD(async_head, cur_entry, links);
2947 			xpt_acquire_device(path->device);
2948 		}
2949 		start_ccb->ccb_h.status = CAM_REQ_CMP;
2950 		break;
2951 	}
2952 	case XPT_REL_SIMQ:
2953 	{
2954 		struct ccb_relsim *crs;
2955 		struct cam_ed *dev;
2956 
2957 		crs = &start_ccb->crs;
2958 		dev = path->device;
2959 		if (dev == NULL) {
2960 			crs->ccb_h.status = CAM_DEV_NOT_THERE;
2961 			break;
2962 		}
2963 
2964 		if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
2965 			/* Don't ever go below one opening */
2966 			if (crs->openings > 0) {
2967 				xpt_dev_ccbq_resize(path, crs->openings);
2968 				if (bootverbose) {
2969 					xpt_print(path,
2970 					    "number of openings is now %d\n",
2971 					    crs->openings);
2972 				}
2973 			}
2974 		}
2975 
2976 		mtx_lock(&dev->sim->devq->send_mtx);
2977 		if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
2978 			if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
2979 				/*
2980 				 * Just extend the old timeout and decrement
2981 				 * the freeze count so that a single timeout
2982 				 * is sufficient for releasing the queue.
2983 				 */
2984 				start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2985 				callout_stop(&dev->callout);
2986 			} else {
2987 				start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2988 			}
2989 
2990 			callout_reset_sbt(&dev->callout,
2991 			    SBT_1MS * crs->release_timeout, SBT_1MS,
2992 			    xpt_release_devq_timeout, dev, 0);
2993 
2994 			dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
2995 		}
2996 
2997 		if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
2998 			if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
2999 				/*
3000 				 * Decrement the freeze count so that a single
3001 				 * completion is still sufficient to unfreeze
3002 				 * the queue.
3003 				 */
3004 				start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3005 			} else {
3006 				dev->flags |= CAM_DEV_REL_ON_COMPLETE;
3007 				start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3008 			}
3009 		}
3010 
3011 		if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
3012 			if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
3013 			 || (dev->ccbq.dev_active == 0)) {
3014 				start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3015 			} else {
3016 				dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
3017 				start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3018 			}
3019 		}
3020 		mtx_unlock(&dev->sim->devq->send_mtx);
3021 
3022 		if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0)
3023 			xpt_release_devq(path, /*count*/1, /*run_queue*/TRUE);
3024 		start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt;
3025 		start_ccb->ccb_h.status = CAM_REQ_CMP;
3026 		break;
3027 	}
3028 	case XPT_DEBUG: {
3029 		struct cam_path *oldpath;
3030 
3031 		/* Check that all request bits are supported. */
3032 		if (start_ccb->cdbg.flags & ~(CAM_DEBUG_COMPILE)) {
3033 			start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3034 			break;
3035 		}
3036 
3037 		cam_dflags = CAM_DEBUG_NONE;
3038 		if (cam_dpath != NULL) {
3039 			oldpath = cam_dpath;
3040 			cam_dpath = NULL;
3041 			xpt_free_path(oldpath);
3042 		}
3043 		if (start_ccb->cdbg.flags != CAM_DEBUG_NONE) {
3044 			if (xpt_create_path(&cam_dpath, NULL,
3045 					    start_ccb->ccb_h.path_id,
3046 					    start_ccb->ccb_h.target_id,
3047 					    start_ccb->ccb_h.target_lun) !=
3048 					    CAM_REQ_CMP) {
3049 				start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3050 			} else {
3051 				cam_dflags = start_ccb->cdbg.flags;
3052 				start_ccb->ccb_h.status = CAM_REQ_CMP;
3053 				xpt_print(cam_dpath, "debugging flags now %x\n",
3054 				    cam_dflags);
3055 			}
3056 		} else
3057 			start_ccb->ccb_h.status = CAM_REQ_CMP;
3058 		break;
3059 	}
3060 	case XPT_NOOP:
3061 		if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3062 			xpt_freeze_devq(path, 1);
3063 		start_ccb->ccb_h.status = CAM_REQ_CMP;
3064 		break;
3065 	case XPT_REPROBE_LUN:
3066 		xpt_async(AC_INQ_CHANGED, path, NULL);
3067 		start_ccb->ccb_h.status = CAM_REQ_CMP;
3068 		xpt_done(start_ccb);
3069 		break;
3070 	case XPT_ASYNC:
3071 		/*
3072 		 * Queue the async operation so it can be run from a sleepable
3073 		 * context.
3074 		 */
3075 		start_ccb->ccb_h.status = CAM_REQ_CMP;
3076 		mtx_lock(&cam_async.cam_doneq_mtx);
3077 		STAILQ_INSERT_TAIL(&cam_async.cam_doneq, &start_ccb->ccb_h, sim_links.stqe);
3078 		start_ccb->ccb_h.pinfo.index = CAM_ASYNC_INDEX;
3079 		mtx_unlock(&cam_async.cam_doneq_mtx);
3080 		wakeup(&cam_async.cam_doneq);
3081 		break;
3082 	default:
3083 	case XPT_SDEV_TYPE:
3084 	case XPT_TERM_IO:
3085 	case XPT_ENG_INQ:
3086 		/* XXX Implement */
3087 		xpt_print(start_ccb->ccb_h.path,
3088 		    "%s: CCB type %#x %s not supported\n", __func__,
3089 		    start_ccb->ccb_h.func_code,
3090 		    xpt_action_name(start_ccb->ccb_h.func_code));
3091 		start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3092 		if (start_ccb->ccb_h.func_code & XPT_FC_DEV_QUEUED) {
3093 			xpt_done(start_ccb);
3094 		}
3095 		break;
3096 	}
3097 	CAM_DEBUG(path, CAM_DEBUG_TRACE,
3098 	    ("xpt_action_default: func= %#x %s status %#x\n",
3099 		start_ccb->ccb_h.func_code,
3100  		xpt_action_name(start_ccb->ccb_h.func_code),
3101 		start_ccb->ccb_h.status));
3102 }
3103 
3104 /*
3105  * Call the sim poll routine to allow the sim to complete
3106  * any inflight requests, then call camisr_runqueue to
3107  * complete any CCB that the polling completed.
3108  */
3109 void
3110 xpt_sim_poll(struct cam_sim *sim)
3111 {
3112 	struct mtx *mtx;
3113 
3114 	KASSERT(cam_sim_pollable(sim), ("%s: non-pollable sim", __func__));
3115 	mtx = sim->mtx;
3116 	if (mtx)
3117 		mtx_lock(mtx);
3118 	(*(sim->sim_poll))(sim);
3119 	if (mtx)
3120 		mtx_unlock(mtx);
3121 	camisr_runqueue();
3122 }
3123 
3124 uint32_t
3125 xpt_poll_setup(union ccb *start_ccb)
3126 {
3127 	uint32_t timeout;
3128 	struct	  cam_sim *sim;
3129 	struct	  cam_devq *devq;
3130 	struct	  cam_ed *dev;
3131 
3132 	timeout = start_ccb->ccb_h.timeout * 10;
3133 	sim = start_ccb->ccb_h.path->bus->sim;
3134 	devq = sim->devq;
3135 	dev = start_ccb->ccb_h.path->device;
3136 
3137 	KASSERT(cam_sim_pollable(sim), ("%s: non-pollable sim", __func__));
3138 
3139 	/*
3140 	 * Steal an opening so that no other queued requests
3141 	 * can get it before us while we simulate interrupts.
3142 	 */
3143 	mtx_lock(&devq->send_mtx);
3144 	dev->ccbq.dev_openings--;
3145 	while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0) &&
3146 	    (--timeout > 0)) {
3147 		mtx_unlock(&devq->send_mtx);
3148 		DELAY(100);
3149 		xpt_sim_poll(sim);
3150 		mtx_lock(&devq->send_mtx);
3151 	}
3152 	dev->ccbq.dev_openings++;
3153 	mtx_unlock(&devq->send_mtx);
3154 
3155 	return (timeout);
3156 }
3157 
3158 void
3159 xpt_pollwait(union ccb *start_ccb, uint32_t timeout)
3160 {
3161 
3162 	KASSERT(cam_sim_pollable(start_ccb->ccb_h.path->bus->sim),
3163 	    ("%s: non-pollable sim", __func__));
3164 	while (--timeout > 0) {
3165 		xpt_sim_poll(start_ccb->ccb_h.path->bus->sim);
3166 		if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3167 		    != CAM_REQ_INPROG)
3168 			break;
3169 		DELAY(100);
3170 	}
3171 
3172 	if (timeout == 0) {
3173 		/*
3174 		 * XXX Is it worth adding a sim_timeout entry
3175 		 * point so we can attempt recovery?  If
3176 		 * this is only used for dumps, I don't think
3177 		 * it is.
3178 		 */
3179 		start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3180 	}
3181 }
3182 
3183 /*
3184  * Schedule a peripheral driver to receive a ccb when its
3185  * target device has space for more transactions.
3186  */
3187 void
3188 xpt_schedule(struct cam_periph *periph, uint32_t new_priority)
3189 {
3190 
3191 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3192 	cam_periph_assert(periph, MA_OWNED);
3193 	if (new_priority < periph->scheduled_priority) {
3194 		periph->scheduled_priority = new_priority;
3195 		xpt_run_allocq(periph, 0);
3196 	}
3197 }
3198 
3199 /*
3200  * Schedule a device to run on a given queue.
3201  * If the device was inserted as a new entry on the queue,
3202  * return 1 meaning the device queue should be run. If we
3203  * were already queued, implying someone else has already
3204  * started the queue, return 0 so the caller doesn't attempt
3205  * to run the queue.
3206  */
3207 static int
3208 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3209 		 uint32_t new_priority)
3210 {
3211 	int retval;
3212 	uint32_t old_priority;
3213 
3214 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3215 
3216 	old_priority = pinfo->priority;
3217 
3218 	/*
3219 	 * Are we already queued?
3220 	 */
3221 	if (pinfo->index != CAM_UNQUEUED_INDEX) {
3222 		/* Simply reorder based on new priority */
3223 		if (new_priority < old_priority) {
3224 			camq_change_priority(queue, pinfo->index,
3225 					     new_priority);
3226 			CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3227 					("changed priority to %d\n",
3228 					 new_priority));
3229 			retval = 1;
3230 		} else
3231 			retval = 0;
3232 	} else {
3233 		/* New entry on the queue */
3234 		if (new_priority < old_priority)
3235 			pinfo->priority = new_priority;
3236 
3237 		CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3238 				("Inserting onto queue\n"));
3239 		pinfo->generation = ++queue->generation;
3240 		camq_insert(queue, pinfo);
3241 		retval = 1;
3242 	}
3243 	return (retval);
3244 }
3245 
3246 static void
3247 xpt_run_allocq_task(void *context, int pending)
3248 {
3249 	struct cam_periph *periph = context;
3250 
3251 	cam_periph_lock(periph);
3252 	periph->flags &= ~CAM_PERIPH_RUN_TASK;
3253 	xpt_run_allocq(periph, 1);
3254 	cam_periph_unlock(periph);
3255 	cam_periph_release(periph);
3256 }
3257 
3258 static void
3259 xpt_run_allocq(struct cam_periph *periph, int sleep)
3260 {
3261 	struct cam_ed	*device;
3262 	union ccb	*ccb;
3263 	uint32_t	 prio;
3264 
3265 	cam_periph_assert(periph, MA_OWNED);
3266 	if (periph->periph_allocating)
3267 		return;
3268 	cam_periph_doacquire(periph);
3269 	periph->periph_allocating = 1;
3270 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_allocq(%p)\n", periph));
3271 	device = periph->path->device;
3272 	ccb = NULL;
3273 restart:
3274 	while ((prio = min(periph->scheduled_priority,
3275 	    periph->immediate_priority)) != CAM_PRIORITY_NONE &&
3276 	    (periph->periph_allocated - (ccb != NULL ? 1 : 0) <
3277 	     device->ccbq.total_openings || prio <= CAM_PRIORITY_OOB)) {
3278 		if (ccb == NULL &&
3279 		    (ccb = xpt_get_ccb_nowait(periph)) == NULL) {
3280 			if (sleep) {
3281 				ccb = xpt_get_ccb(periph);
3282 				goto restart;
3283 			}
3284 			if (periph->flags & CAM_PERIPH_RUN_TASK)
3285 				break;
3286 			cam_periph_doacquire(periph);
3287 			periph->flags |= CAM_PERIPH_RUN_TASK;
3288 			taskqueue_enqueue(xsoftc.xpt_taskq,
3289 			    &periph->periph_run_task);
3290 			break;
3291 		}
3292 		xpt_setup_ccb(&ccb->ccb_h, periph->path, prio);
3293 		if (prio == periph->immediate_priority) {
3294 			periph->immediate_priority = CAM_PRIORITY_NONE;
3295 			CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3296 					("waking cam_periph_getccb()\n"));
3297 			SLIST_INSERT_HEAD(&periph->ccb_list, &ccb->ccb_h,
3298 					  periph_links.sle);
3299 			wakeup(&periph->ccb_list);
3300 		} else {
3301 			periph->scheduled_priority = CAM_PRIORITY_NONE;
3302 			CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3303 					("calling periph_start()\n"));
3304 			periph->periph_start(periph, ccb);
3305 		}
3306 		ccb = NULL;
3307 	}
3308 	if (ccb != NULL)
3309 		xpt_release_ccb(ccb);
3310 	periph->periph_allocating = 0;
3311 	cam_periph_release_locked(periph);
3312 }
3313 
3314 static void
3315 xpt_run_devq(struct cam_devq *devq)
3316 {
3317 	struct mtx *mtx;
3318 
3319 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_devq\n"));
3320 
3321 	devq->send_queue.qfrozen_cnt++;
3322 	while ((devq->send_queue.entries > 0)
3323 	    && (devq->send_openings > 0)
3324 	    && (devq->send_queue.qfrozen_cnt <= 1)) {
3325 		struct	cam_ed *device;
3326 		union ccb *work_ccb;
3327 		struct	cam_sim *sim;
3328 		struct xpt_proto *proto;
3329 
3330 		device = (struct cam_ed *)camq_remove(&devq->send_queue,
3331 							   CAMQ_HEAD);
3332 		CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3333 				("running device %p\n", device));
3334 
3335 		work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3336 		if (work_ccb == NULL) {
3337 			printf("device on run queue with no ccbs???\n");
3338 			continue;
3339 		}
3340 
3341 		if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3342 			mtx_lock(&xsoftc.xpt_highpower_lock);
3343 		 	if (xsoftc.num_highpower <= 0) {
3344 				/*
3345 				 * We got a high power command, but we
3346 				 * don't have any available slots.  Freeze
3347 				 * the device queue until we have a slot
3348 				 * available.
3349 				 */
3350 				xpt_freeze_devq_device(device, 1);
3351 				STAILQ_INSERT_TAIL(&xsoftc.highpowerq, device,
3352 						   highpowerq_entry);
3353 
3354 				mtx_unlock(&xsoftc.xpt_highpower_lock);
3355 				continue;
3356 			} else {
3357 				/*
3358 				 * Consume a high power slot while
3359 				 * this ccb runs.
3360 				 */
3361 				xsoftc.num_highpower--;
3362 			}
3363 			mtx_unlock(&xsoftc.xpt_highpower_lock);
3364 		}
3365 		cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3366 		cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3367 		devq->send_openings--;
3368 		devq->send_active++;
3369 		xpt_schedule_devq(devq, device);
3370 		mtx_unlock(&devq->send_mtx);
3371 
3372 		if ((work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0) {
3373 			/*
3374 			 * The client wants to freeze the queue
3375 			 * after this CCB is sent.
3376 			 */
3377 			xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3378 		}
3379 
3380 		/* In Target mode, the peripheral driver knows best... */
3381 		if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3382 			if ((device->inq_flags & SID_CmdQue) != 0
3383 			 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3384 				work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3385 			else
3386 				/*
3387 				 * Clear this in case of a retried CCB that
3388 				 * failed due to a rejected tag.
3389 				 */
3390 				work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3391 		}
3392 
3393 		KASSERT(device == work_ccb->ccb_h.path->device,
3394 		    ("device (%p) / path->device (%p) mismatch",
3395 			device, work_ccb->ccb_h.path->device));
3396 		proto = xpt_proto_find(device->protocol);
3397 		if (proto && proto->ops->debug_out)
3398 			proto->ops->debug_out(work_ccb);
3399 
3400 		/*
3401 		 * Device queues can be shared among multiple SIM instances
3402 		 * that reside on different buses.  Use the SIM from the
3403 		 * queued device, rather than the one from the calling bus.
3404 		 */
3405 		sim = device->sim;
3406 		mtx = sim->mtx;
3407 		if (mtx && !mtx_owned(mtx))
3408 			mtx_lock(mtx);
3409 		else
3410 			mtx = NULL;
3411 		work_ccb->ccb_h.qos.periph_data = cam_iosched_now();
3412 		(*(sim->sim_action))(sim, work_ccb);
3413 		if (mtx)
3414 			mtx_unlock(mtx);
3415 		mtx_lock(&devq->send_mtx);
3416 	}
3417 	devq->send_queue.qfrozen_cnt--;
3418 }
3419 
3420 /*
3421  * This function merges stuff from the src ccb into the dst ccb, while keeping
3422  * important fields in the dst ccb constant.
3423  */
3424 void
3425 xpt_merge_ccb(union ccb *dst_ccb, union ccb *src_ccb)
3426 {
3427 
3428 	/*
3429 	 * Pull fields that are valid for peripheral drivers to set
3430 	 * into the dst CCB along with the CCB "payload".
3431 	 */
3432 	dst_ccb->ccb_h.retry_count = src_ccb->ccb_h.retry_count;
3433 	dst_ccb->ccb_h.func_code = src_ccb->ccb_h.func_code;
3434 	dst_ccb->ccb_h.timeout = src_ccb->ccb_h.timeout;
3435 	dst_ccb->ccb_h.flags = src_ccb->ccb_h.flags;
3436 	bcopy(&(&src_ccb->ccb_h)[1], &(&dst_ccb->ccb_h)[1],
3437 	      sizeof(union ccb) - sizeof(struct ccb_hdr));
3438 }
3439 
3440 void
3441 xpt_setup_ccb_flags(struct ccb_hdr *ccb_h, struct cam_path *path,
3442 		    uint32_t priority, uint32_t flags)
3443 {
3444 
3445 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3446 	ccb_h->pinfo.priority = priority;
3447 	ccb_h->path = path;
3448 	ccb_h->path_id = path->bus->path_id;
3449 	if (path->target)
3450 		ccb_h->target_id = path->target->target_id;
3451 	else
3452 		ccb_h->target_id = CAM_TARGET_WILDCARD;
3453 	if (path->device) {
3454 		ccb_h->target_lun = path->device->lun_id;
3455 		ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3456 	} else {
3457 		ccb_h->target_lun = CAM_TARGET_WILDCARD;
3458 	}
3459 	ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3460 	ccb_h->flags = flags;
3461 	ccb_h->xflags = 0;
3462 }
3463 
3464 void
3465 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, uint32_t priority)
3466 {
3467 	xpt_setup_ccb_flags(ccb_h, path, priority, /*flags*/ 0);
3468 }
3469 
3470 /* Path manipulation functions */
3471 cam_status
3472 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3473 		path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3474 {
3475 	struct	   cam_path *path;
3476 	cam_status status;
3477 
3478 	path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3479 
3480 	if (path == NULL) {
3481 		status = CAM_RESRC_UNAVAIL;
3482 		return(status);
3483 	}
3484 	status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3485 	if (status != CAM_REQ_CMP) {
3486 		free(path, M_CAMPATH);
3487 		path = NULL;
3488 	}
3489 	*new_path_ptr = path;
3490 	return (status);
3491 }
3492 
3493 cam_status
3494 xpt_create_path_unlocked(struct cam_path **new_path_ptr,
3495 			 struct cam_periph *periph, path_id_t path_id,
3496 			 target_id_t target_id, lun_id_t lun_id)
3497 {
3498 
3499 	return (xpt_create_path(new_path_ptr, periph, path_id, target_id,
3500 	    lun_id));
3501 }
3502 
3503 cam_status
3504 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3505 		 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3506 {
3507 	struct	     cam_eb *bus;
3508 	struct	     cam_et *target;
3509 	struct	     cam_ed *device;
3510 	cam_status   status;
3511 
3512 	status = CAM_REQ_CMP;	/* Completed without error */
3513 	target = NULL;		/* Wildcarded */
3514 	device = NULL;		/* Wildcarded */
3515 
3516 	/*
3517 	 * We will potentially modify the EDT, so block interrupts
3518 	 * that may attempt to create cam paths.
3519 	 */
3520 	bus = xpt_find_bus(path_id);
3521 	if (bus == NULL) {
3522 		status = CAM_PATH_INVALID;
3523 	} else {
3524 		xpt_lock_buses();
3525 		mtx_lock(&bus->eb_mtx);
3526 		target = xpt_find_target(bus, target_id);
3527 		if (target == NULL) {
3528 			/* Create one */
3529 			struct cam_et *new_target;
3530 
3531 			new_target = xpt_alloc_target(bus, target_id);
3532 			if (new_target == NULL) {
3533 				status = CAM_RESRC_UNAVAIL;
3534 			} else {
3535 				target = new_target;
3536 			}
3537 		}
3538 		xpt_unlock_buses();
3539 		if (target != NULL) {
3540 			device = xpt_find_device(target, lun_id);
3541 			if (device == NULL) {
3542 				/* Create one */
3543 				struct cam_ed *new_device;
3544 
3545 				new_device =
3546 				    (*(bus->xport->ops->alloc_device))(bus,
3547 								       target,
3548 								       lun_id);
3549 				if (new_device == NULL) {
3550 					status = CAM_RESRC_UNAVAIL;
3551 				} else {
3552 					device = new_device;
3553 				}
3554 			}
3555 		}
3556 		mtx_unlock(&bus->eb_mtx);
3557 	}
3558 
3559 	/*
3560 	 * Only touch the user's data if we are successful.
3561 	 */
3562 	if (status == CAM_REQ_CMP) {
3563 		new_path->periph = perph;
3564 		new_path->bus = bus;
3565 		new_path->target = target;
3566 		new_path->device = device;
3567 		CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
3568 	} else {
3569 		if (device != NULL)
3570 			xpt_release_device(device);
3571 		if (target != NULL)
3572 			xpt_release_target(target);
3573 		if (bus != NULL)
3574 			xpt_release_bus(bus);
3575 	}
3576 	return (status);
3577 }
3578 
3579 int
3580 xpt_clone_path(struct cam_path **new_path_ptr, struct cam_path *path)
3581 {
3582 	struct	   cam_path *new_path;
3583 
3584 	new_path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3585 	if (new_path == NULL)
3586 		return (ENOMEM);
3587 	*new_path = *path;
3588 	if (path->bus != NULL)
3589 		xpt_acquire_bus(path->bus);
3590 	if (path->target != NULL)
3591 		xpt_acquire_target(path->target);
3592 	if (path->device != NULL)
3593 		xpt_acquire_device(path->device);
3594 	*new_path_ptr = new_path;
3595 	return (0);
3596 }
3597 
3598 void
3599 xpt_release_path(struct cam_path *path)
3600 {
3601 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
3602 	if (path->device != NULL) {
3603 		xpt_release_device(path->device);
3604 		path->device = NULL;
3605 	}
3606 	if (path->target != NULL) {
3607 		xpt_release_target(path->target);
3608 		path->target = NULL;
3609 	}
3610 	if (path->bus != NULL) {
3611 		xpt_release_bus(path->bus);
3612 		path->bus = NULL;
3613 	}
3614 }
3615 
3616 void
3617 xpt_free_path(struct cam_path *path)
3618 {
3619 
3620 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
3621 	xpt_release_path(path);
3622 	free(path, M_CAMPATH);
3623 }
3624 
3625 void
3626 xpt_path_counts(struct cam_path *path, uint32_t *bus_ref,
3627     uint32_t *periph_ref, uint32_t *target_ref, uint32_t *device_ref)
3628 {
3629 
3630 	xpt_lock_buses();
3631 	if (bus_ref) {
3632 		if (path->bus)
3633 			*bus_ref = path->bus->refcount;
3634 		else
3635 			*bus_ref = 0;
3636 	}
3637 	if (periph_ref) {
3638 		if (path->periph)
3639 			*periph_ref = path->periph->refcount;
3640 		else
3641 			*periph_ref = 0;
3642 	}
3643 	xpt_unlock_buses();
3644 	if (target_ref) {
3645 		if (path->target)
3646 			*target_ref = path->target->refcount;
3647 		else
3648 			*target_ref = 0;
3649 	}
3650 	if (device_ref) {
3651 		if (path->device)
3652 			*device_ref = path->device->refcount;
3653 		else
3654 			*device_ref = 0;
3655 	}
3656 }
3657 
3658 /*
3659  * Return -1 for failure, 0 for exact match, 1 for match with wildcards
3660  * in path1, 2 for match with wildcards in path2.
3661  */
3662 int
3663 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
3664 {
3665 	int retval = 0;
3666 
3667 	if (path1->bus != path2->bus) {
3668 		if (path1->bus->path_id == CAM_BUS_WILDCARD)
3669 			retval = 1;
3670 		else if (path2->bus->path_id == CAM_BUS_WILDCARD)
3671 			retval = 2;
3672 		else
3673 			return (-1);
3674 	}
3675 	if (path1->target != path2->target) {
3676 		if (path1->target->target_id == CAM_TARGET_WILDCARD) {
3677 			if (retval == 0)
3678 				retval = 1;
3679 		} else if (path2->target->target_id == CAM_TARGET_WILDCARD)
3680 			retval = 2;
3681 		else
3682 			return (-1);
3683 	}
3684 	if (path1->device != path2->device) {
3685 		if (path1->device->lun_id == CAM_LUN_WILDCARD) {
3686 			if (retval == 0)
3687 				retval = 1;
3688 		} else if (path2->device->lun_id == CAM_LUN_WILDCARD)
3689 			retval = 2;
3690 		else
3691 			return (-1);
3692 	}
3693 	return (retval);
3694 }
3695 
3696 int
3697 xpt_path_comp_dev(struct cam_path *path, struct cam_ed *dev)
3698 {
3699 	int retval = 0;
3700 
3701 	if (path->bus != dev->target->bus) {
3702 		if (path->bus->path_id == CAM_BUS_WILDCARD)
3703 			retval = 1;
3704 		else if (dev->target->bus->path_id == CAM_BUS_WILDCARD)
3705 			retval = 2;
3706 		else
3707 			return (-1);
3708 	}
3709 	if (path->target != dev->target) {
3710 		if (path->target->target_id == CAM_TARGET_WILDCARD) {
3711 			if (retval == 0)
3712 				retval = 1;
3713 		} else if (dev->target->target_id == CAM_TARGET_WILDCARD)
3714 			retval = 2;
3715 		else
3716 			return (-1);
3717 	}
3718 	if (path->device != dev) {
3719 		if (path->device->lun_id == CAM_LUN_WILDCARD) {
3720 			if (retval == 0)
3721 				retval = 1;
3722 		} else if (dev->lun_id == CAM_LUN_WILDCARD)
3723 			retval = 2;
3724 		else
3725 			return (-1);
3726 	}
3727 	return (retval);
3728 }
3729 
3730 void
3731 xpt_print_path(struct cam_path *path)
3732 {
3733 	struct sbuf sb;
3734 	char buffer[XPT_PRINT_LEN];
3735 
3736 	sbuf_new(&sb, buffer, XPT_PRINT_LEN, SBUF_FIXEDLEN);
3737 	xpt_path_sbuf(path, &sb);
3738 	sbuf_finish(&sb);
3739 	printf("%s", sbuf_data(&sb));
3740 	sbuf_delete(&sb);
3741 }
3742 
3743 static void
3744 xpt_device_sbuf(struct cam_ed *device, struct sbuf *sb)
3745 {
3746 	if (device == NULL)
3747 		sbuf_cat(sb, "(nopath): ");
3748 	else {
3749 		sbuf_printf(sb, "(noperiph:%s%d:%d:%d:%jx): ",
3750 		    device->sim->sim_name,
3751 		    device->sim->unit_number,
3752 		    device->sim->bus_id,
3753 		    device->target->target_id,
3754 		    (uintmax_t)device->lun_id);
3755 	}
3756 }
3757 
3758 void
3759 xpt_print(struct cam_path *path, const char *fmt, ...)
3760 {
3761 	va_list ap;
3762 	struct sbuf sb;
3763 	char buffer[XPT_PRINT_LEN];
3764 
3765 	sbuf_new(&sb, buffer, XPT_PRINT_LEN, SBUF_FIXEDLEN);
3766 
3767 	xpt_path_sbuf(path, &sb);
3768 	va_start(ap, fmt);
3769 	sbuf_vprintf(&sb, fmt, ap);
3770 	va_end(ap);
3771 
3772 	sbuf_finish(&sb);
3773 	printf("%s", sbuf_data(&sb));
3774 	sbuf_delete(&sb);
3775 }
3776 
3777 char *
3778 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
3779 {
3780 	struct sbuf sb;
3781 
3782 	sbuf_new(&sb, str, str_len, 0);
3783 	xpt_path_sbuf(path, &sb);
3784 	sbuf_finish(&sb);
3785 	return (str);
3786 }
3787 
3788 void
3789 xpt_path_sbuf(struct cam_path *path, struct sbuf *sb)
3790 {
3791 
3792 	if (path == NULL)
3793 		sbuf_cat(sb, "(nopath): ");
3794 	else {
3795 		if (path->periph != NULL)
3796 			sbuf_printf(sb, "(%s%d:", path->periph->periph_name,
3797 				    path->periph->unit_number);
3798 		else
3799 			sbuf_cat(sb, "(noperiph:");
3800 
3801 		if (path->bus != NULL)
3802 			sbuf_printf(sb, "%s%d:%d:", path->bus->sim->sim_name,
3803 				    path->bus->sim->unit_number,
3804 				    path->bus->sim->bus_id);
3805 		else
3806 			sbuf_cat(sb, "nobus:");
3807 
3808 		if (path->target != NULL)
3809 			sbuf_printf(sb, "%d:", path->target->target_id);
3810 		else
3811 			sbuf_cat(sb, "X:");
3812 
3813 		if (path->device != NULL)
3814 			sbuf_printf(sb, "%jx): ",
3815 			    (uintmax_t)path->device->lun_id);
3816 		else
3817 			sbuf_cat(sb, "X): ");
3818 	}
3819 }
3820 
3821 path_id_t
3822 xpt_path_path_id(struct cam_path *path)
3823 {
3824 	return(path->bus->path_id);
3825 }
3826 
3827 target_id_t
3828 xpt_path_target_id(struct cam_path *path)
3829 {
3830 	if (path->target != NULL)
3831 		return (path->target->target_id);
3832 	else
3833 		return (CAM_TARGET_WILDCARD);
3834 }
3835 
3836 lun_id_t
3837 xpt_path_lun_id(struct cam_path *path)
3838 {
3839 	if (path->device != NULL)
3840 		return (path->device->lun_id);
3841 	else
3842 		return (CAM_LUN_WILDCARD);
3843 }
3844 
3845 struct cam_sim *
3846 xpt_path_sim(struct cam_path *path)
3847 {
3848 
3849 	return (path->bus->sim);
3850 }
3851 
3852 struct cam_periph*
3853 xpt_path_periph(struct cam_path *path)
3854 {
3855 
3856 	return (path->periph);
3857 }
3858 
3859 /*
3860  * Release a CAM control block for the caller.  Remit the cost of the structure
3861  * to the device referenced by the path.  If the this device had no 'credits'
3862  * and peripheral drivers have registered async callbacks for this notification
3863  * call them now.
3864  */
3865 void
3866 xpt_release_ccb(union ccb *free_ccb)
3867 {
3868 	struct	 cam_ed *device;
3869 	struct	 cam_periph *periph;
3870 
3871 	CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
3872 	xpt_path_assert(free_ccb->ccb_h.path, MA_OWNED);
3873 	device = free_ccb->ccb_h.path->device;
3874 	periph = free_ccb->ccb_h.path->periph;
3875 
3876 	xpt_free_ccb(free_ccb);
3877 	periph->periph_allocated--;
3878 	cam_ccbq_release_opening(&device->ccbq);
3879 	xpt_run_allocq(periph, 0);
3880 }
3881 
3882 /* Functions accessed by SIM drivers */
3883 
3884 static struct xpt_xport_ops xport_default_ops = {
3885 	.alloc_device = xpt_alloc_device_default,
3886 	.action = xpt_action_default,
3887 	.async = xpt_dev_async_default,
3888 };
3889 static struct xpt_xport xport_default = {
3890 	.xport = XPORT_UNKNOWN,
3891 	.name = "unknown",
3892 	.ops = &xport_default_ops,
3893 };
3894 
3895 CAM_XPT_XPORT(xport_default);
3896 
3897 /*
3898  * A sim structure, listing the SIM entry points and instance
3899  * identification info is passed to xpt_bus_register to hook the SIM
3900  * into the CAM framework.  xpt_bus_register creates a cam_eb entry
3901  * for this new bus and places it in the array of buses and assigns
3902  * it a path_id.  The path_id may be influenced by "hard wiring"
3903  * information specified by the user.  Once interrupt services are
3904  * available, the bus will be probed.
3905  */
3906 int
3907 xpt_bus_register(struct cam_sim *sim, device_t parent, uint32_t bus)
3908 {
3909 	struct cam_eb *new_bus;
3910 	struct cam_eb *old_bus;
3911 	struct ccb_pathinq cpi;
3912 	struct cam_path *path;
3913 	cam_status status;
3914 
3915 	sim->bus_id = bus;
3916 	new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
3917 					  M_CAMXPT, M_NOWAIT|M_ZERO);
3918 	if (new_bus == NULL) {
3919 		/* Couldn't satisfy request */
3920 		return (ENOMEM);
3921 	}
3922 
3923 	mtx_init(&new_bus->eb_mtx, "CAM bus lock", NULL, MTX_DEF);
3924 	TAILQ_INIT(&new_bus->et_entries);
3925 	cam_sim_hold(sim);
3926 	new_bus->sim = sim;
3927 	timevalclear(&new_bus->last_reset);
3928 	new_bus->flags = 0;
3929 	new_bus->refcount = 1;	/* Held until a bus_deregister event */
3930 	new_bus->generation = 0;
3931 	new_bus->parent_dev = parent;
3932 
3933 	xpt_lock_buses();
3934 	sim->path_id = new_bus->path_id =
3935 	    xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
3936 	old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3937 	while (old_bus != NULL
3938 	    && old_bus->path_id < new_bus->path_id)
3939 		old_bus = TAILQ_NEXT(old_bus, links);
3940 	if (old_bus != NULL)
3941 		TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
3942 	else
3943 		TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
3944 	xsoftc.bus_generation++;
3945 	xpt_unlock_buses();
3946 
3947 	CAM_PROBE2(xpt, bus__register, sim, new_bus->path_id);
3948 
3949 	/*
3950 	 * Set a default transport so that a PATH_INQ can be issued to
3951 	 * the SIM.  This will then allow for probing and attaching of
3952 	 * a more appropriate transport.
3953 	 */
3954 	new_bus->xport = &xport_default;
3955 
3956 	status = xpt_create_path(&path, /*periph*/NULL, sim->path_id,
3957 				  CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3958 	if (status != CAM_REQ_CMP) {
3959 		xpt_release_bus(new_bus);
3960 		return (ENOMEM);
3961 	}
3962 
3963 	xpt_path_inq(&cpi, path);
3964 
3965 	/*
3966 	 * Use the results of PATH_INQ to pick a transport.  Note that
3967 	 * the xpt bus (which uses XPORT_UNSPECIFIED) always uses
3968 	 * xport_default instead of a transport from
3969 	 * cam_xpt_port_set.
3970 	 */
3971 	if (cam_ccb_success((union ccb *)&cpi) &&
3972 	    cpi.transport != XPORT_UNSPECIFIED) {
3973 		struct xpt_xport **xpt;
3974 
3975 		SET_FOREACH(xpt, cam_xpt_xport_set) {
3976 			if ((*xpt)->xport == cpi.transport) {
3977 				new_bus->xport = *xpt;
3978 				break;
3979 			}
3980 		}
3981 		if (new_bus->xport == &xport_default) {
3982 			xpt_print(path,
3983 			    "No transport found for %d\n", cpi.transport);
3984 			xpt_release_bus(new_bus);
3985 			xpt_free_path(path);
3986 			return (EINVAL);
3987 		}
3988 	}
3989 
3990 	/* Notify interested parties */
3991 	if (sim->path_id != CAM_XPT_PATH_ID) {
3992 		xpt_async(AC_PATH_REGISTERED, path, &cpi);
3993 		if ((cpi.hba_misc & PIM_NOSCAN) == 0) {
3994 			union	ccb *scan_ccb;
3995 
3996 			/* Initiate bus rescan. */
3997 			scan_ccb = xpt_alloc_ccb_nowait();
3998 			if (scan_ccb != NULL) {
3999 				scan_ccb->ccb_h.path = path;
4000 				scan_ccb->ccb_h.func_code = XPT_SCAN_BUS;
4001 				scan_ccb->crcn.flags = 0;
4002 				xpt_rescan(scan_ccb);
4003 			} else {
4004 				xpt_print(path,
4005 					  "Can't allocate CCB to scan bus\n");
4006 				xpt_free_path(path);
4007 			}
4008 		} else
4009 			xpt_free_path(path);
4010 	} else
4011 		xpt_free_path(path);
4012 	return (CAM_SUCCESS);
4013 }
4014 
4015 int
4016 xpt_bus_deregister(path_id_t pathid)
4017 {
4018 	struct cam_path bus_path;
4019 	cam_status status;
4020 
4021 	status = xpt_compile_path(&bus_path, NULL, pathid,
4022 				  CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4023 	if (status != CAM_REQ_CMP)
4024 		return (ENOMEM);
4025 
4026 	xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4027 	xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4028 
4029 	/* Release the reference count held while registered. */
4030 	xpt_release_bus(bus_path.bus);
4031 	xpt_release_path(&bus_path);
4032 
4033 	return (CAM_SUCCESS);
4034 }
4035 
4036 static path_id_t
4037 xptnextfreepathid(void)
4038 {
4039 	struct cam_eb *bus;
4040 	path_id_t pathid;
4041 	const char *strval;
4042 
4043 	mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
4044 	pathid = 0;
4045 	bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4046 retry:
4047 	/* Find an unoccupied pathid */
4048 	while (bus != NULL && bus->path_id <= pathid) {
4049 		if (bus->path_id == pathid)
4050 			pathid++;
4051 		bus = TAILQ_NEXT(bus, links);
4052 	}
4053 
4054 	/*
4055 	 * Ensure that this pathid is not reserved for
4056 	 * a bus that may be registered in the future.
4057 	 */
4058 	if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4059 		++pathid;
4060 		/* Start the search over */
4061 		goto retry;
4062 	}
4063 	return (pathid);
4064 }
4065 
4066 static path_id_t
4067 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4068 {
4069 	path_id_t pathid;
4070 	int i, dunit, val;
4071 	char buf[32];
4072 	const char *dname;
4073 
4074 	pathid = CAM_XPT_PATH_ID;
4075 	snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4076 	if (strcmp(buf, "xpt0") == 0 && sim_bus == 0)
4077 		return (pathid);
4078 	i = 0;
4079 	while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
4080 		if (strcmp(dname, "scbus")) {
4081 			/* Avoid a bit of foot shooting. */
4082 			continue;
4083 		}
4084 		if (dunit < 0)		/* unwired?! */
4085 			continue;
4086 		if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4087 			if (sim_bus == val) {
4088 				pathid = dunit;
4089 				break;
4090 			}
4091 		} else if (sim_bus == 0) {
4092 			/* Unspecified matches bus 0 */
4093 			pathid = dunit;
4094 			break;
4095 		} else {
4096 			printf(
4097 "Ambiguous scbus configuration for %s%d bus %d, cannot wire down.  The kernel\n"
4098 "config entry for scbus%d should specify a controller bus.\n"
4099 "Scbus will be assigned dynamically.\n",
4100 			    sim_name, sim_unit, sim_bus, dunit);
4101 			break;
4102 		}
4103 	}
4104 
4105 	if (pathid == CAM_XPT_PATH_ID)
4106 		pathid = xptnextfreepathid();
4107 	return (pathid);
4108 }
4109 
4110 static const char *
4111 xpt_async_string(uint32_t async_code)
4112 {
4113 
4114 	switch (async_code) {
4115 	case AC_BUS_RESET: return ("AC_BUS_RESET");
4116 	case AC_UNSOL_RESEL: return ("AC_UNSOL_RESEL");
4117 	case AC_SCSI_AEN: return ("AC_SCSI_AEN");
4118 	case AC_SENT_BDR: return ("AC_SENT_BDR");
4119 	case AC_PATH_REGISTERED: return ("AC_PATH_REGISTERED");
4120 	case AC_PATH_DEREGISTERED: return ("AC_PATH_DEREGISTERED");
4121 	case AC_FOUND_DEVICE: return ("AC_FOUND_DEVICE");
4122 	case AC_LOST_DEVICE: return ("AC_LOST_DEVICE");
4123 	case AC_TRANSFER_NEG: return ("AC_TRANSFER_NEG");
4124 	case AC_INQ_CHANGED: return ("AC_INQ_CHANGED");
4125 	case AC_GETDEV_CHANGED: return ("AC_GETDEV_CHANGED");
4126 	case AC_CONTRACT: return ("AC_CONTRACT");
4127 	case AC_ADVINFO_CHANGED: return ("AC_ADVINFO_CHANGED");
4128 	case AC_UNIT_ATTENTION: return ("AC_UNIT_ATTENTION");
4129 	}
4130 	return ("AC_UNKNOWN");
4131 }
4132 
4133 static int
4134 xpt_async_size(uint32_t async_code)
4135 {
4136 
4137 	switch (async_code) {
4138 	case AC_BUS_RESET: return (0);
4139 	case AC_UNSOL_RESEL: return (0);
4140 	case AC_SCSI_AEN: return (0);
4141 	case AC_SENT_BDR: return (0);
4142 	case AC_PATH_REGISTERED: return (sizeof(struct ccb_pathinq));
4143 	case AC_PATH_DEREGISTERED: return (0);
4144 	case AC_FOUND_DEVICE: return (sizeof(struct ccb_getdev));
4145 	case AC_LOST_DEVICE: return (0);
4146 	case AC_TRANSFER_NEG: return (sizeof(struct ccb_trans_settings));
4147 	case AC_INQ_CHANGED: return (0);
4148 	case AC_GETDEV_CHANGED: return (0);
4149 	case AC_CONTRACT: return (sizeof(struct ac_contract));
4150 	case AC_ADVINFO_CHANGED: return (-1);
4151 	case AC_UNIT_ATTENTION: return (sizeof(struct ccb_scsiio));
4152 	}
4153 	return (0);
4154 }
4155 
4156 static int
4157 xpt_async_process_dev(struct cam_ed *device, void *arg)
4158 {
4159 	union ccb *ccb = arg;
4160 	struct cam_path *path = ccb->ccb_h.path;
4161 	void *async_arg = ccb->casync.async_arg_ptr;
4162 	uint32_t async_code = ccb->casync.async_code;
4163 	bool relock;
4164 
4165 	if (path->device != device
4166 	 && path->device->lun_id != CAM_LUN_WILDCARD
4167 	 && device->lun_id != CAM_LUN_WILDCARD)
4168 		return (1);
4169 
4170 	/*
4171 	 * The async callback could free the device.
4172 	 * If it is a broadcast async, it doesn't hold
4173 	 * device reference, so take our own reference.
4174 	 */
4175 	xpt_acquire_device(device);
4176 
4177 	/*
4178 	 * If async for specific device is to be delivered to
4179 	 * the wildcard client, take the specific device lock.
4180 	 * XXX: We may need a way for client to specify it.
4181 	 */
4182 	if ((device->lun_id == CAM_LUN_WILDCARD &&
4183 	     path->device->lun_id != CAM_LUN_WILDCARD) ||
4184 	    (device->target->target_id == CAM_TARGET_WILDCARD &&
4185 	     path->target->target_id != CAM_TARGET_WILDCARD) ||
4186 	    (device->target->bus->path_id == CAM_BUS_WILDCARD &&
4187 	     path->target->bus->path_id != CAM_BUS_WILDCARD)) {
4188 		mtx_unlock(&device->device_mtx);
4189 		xpt_path_lock(path);
4190 		relock = true;
4191 	} else
4192 		relock = false;
4193 
4194 	(*(device->target->bus->xport->ops->async))(async_code,
4195 	    device->target->bus, device->target, device, async_arg);
4196 	xpt_async_bcast(&device->asyncs, async_code, path, async_arg);
4197 
4198 	if (relock) {
4199 		xpt_path_unlock(path);
4200 		mtx_lock(&device->device_mtx);
4201 	}
4202 	xpt_release_device(device);
4203 	return (1);
4204 }
4205 
4206 static int
4207 xpt_async_process_tgt(struct cam_et *target, void *arg)
4208 {
4209 	union ccb *ccb = arg;
4210 	struct cam_path *path = ccb->ccb_h.path;
4211 
4212 	if (path->target != target
4213 	 && path->target->target_id != CAM_TARGET_WILDCARD
4214 	 && target->target_id != CAM_TARGET_WILDCARD)
4215 		return (1);
4216 
4217 	if (ccb->casync.async_code == AC_SENT_BDR) {
4218 		/* Update our notion of when the last reset occurred */
4219 		microtime(&target->last_reset);
4220 	}
4221 
4222 	return (xptdevicetraverse(target, NULL, xpt_async_process_dev, ccb));
4223 }
4224 
4225 static void
4226 xpt_async_process(struct cam_periph *periph, union ccb *ccb)
4227 {
4228 	struct cam_eb *bus;
4229 	struct cam_path *path;
4230 	void *async_arg;
4231 	uint32_t async_code;
4232 
4233 	path = ccb->ccb_h.path;
4234 	async_code = ccb->casync.async_code;
4235 	async_arg = ccb->casync.async_arg_ptr;
4236 	CAM_DEBUG(path, CAM_DEBUG_TRACE | CAM_DEBUG_INFO,
4237 	    ("xpt_async(%s)\n", xpt_async_string(async_code)));
4238 	bus = path->bus;
4239 
4240 	if (async_code == AC_BUS_RESET) {
4241 		/* Update our notion of when the last reset occurred */
4242 		microtime(&bus->last_reset);
4243 	}
4244 
4245 	xpttargettraverse(bus, NULL, xpt_async_process_tgt, ccb);
4246 
4247 	/*
4248 	 * If this wasn't a fully wildcarded async, tell all
4249 	 * clients that want all async events.
4250 	 */
4251 	if (bus != xpt_periph->path->bus) {
4252 		xpt_path_lock(xpt_periph->path);
4253 		xpt_async_process_dev(xpt_periph->path->device, ccb);
4254 		xpt_path_unlock(xpt_periph->path);
4255 	}
4256 
4257 	if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD)
4258 		xpt_release_devq(path, 1, TRUE);
4259 	else
4260 		xpt_release_simq(path->bus->sim, TRUE);
4261 	if (ccb->casync.async_arg_size > 0)
4262 		free(async_arg, M_CAMXPT);
4263 	xpt_free_path(path);
4264 	xpt_free_ccb(ccb);
4265 }
4266 
4267 static void
4268 xpt_async_bcast(struct async_list *async_head,
4269 		uint32_t async_code,
4270 		struct cam_path *path, void *async_arg)
4271 {
4272 	struct async_node *cur_entry;
4273 	struct mtx *mtx;
4274 
4275 	cur_entry = SLIST_FIRST(async_head);
4276 	while (cur_entry != NULL) {
4277 		struct async_node *next_entry;
4278 		/*
4279 		 * Grab the next list entry before we call the current
4280 		 * entry's callback.  This is because the callback function
4281 		 * can delete its async callback entry.
4282 		 */
4283 		next_entry = SLIST_NEXT(cur_entry, links);
4284 		if ((cur_entry->event_enable & async_code) != 0) {
4285 			mtx = cur_entry->event_lock ?
4286 			    path->device->sim->mtx : NULL;
4287 			if (mtx)
4288 				mtx_lock(mtx);
4289 			CAM_PROBE4(xpt, async__cb, cur_entry->callback_arg,
4290 			    async_code, path, async_arg);
4291 			cur_entry->callback(cur_entry->callback_arg,
4292 					    async_code, path,
4293 					    async_arg);
4294 			if (mtx)
4295 				mtx_unlock(mtx);
4296 		}
4297 		cur_entry = next_entry;
4298 	}
4299 }
4300 
4301 void
4302 xpt_async(uint32_t async_code, struct cam_path *path, void *async_arg)
4303 {
4304 	union ccb *ccb;
4305 	int size;
4306 
4307 	ccb = xpt_alloc_ccb_nowait();
4308 	if (ccb == NULL) {
4309 		xpt_print(path, "Can't allocate CCB to send %s\n",
4310 		    xpt_async_string(async_code));
4311 		return;
4312 	}
4313 
4314 	if (xpt_clone_path(&ccb->ccb_h.path, path) != 0) {
4315 		xpt_print(path, "Can't allocate path to send %s\n",
4316 		    xpt_async_string(async_code));
4317 		xpt_free_ccb(ccb);
4318 		return;
4319 	}
4320 	ccb->ccb_h.path->periph = NULL;
4321 	ccb->ccb_h.func_code = XPT_ASYNC;
4322 	ccb->ccb_h.cbfcnp = xpt_async_process;
4323 	ccb->ccb_h.flags |= CAM_UNLOCKED;
4324 	ccb->casync.async_code = async_code;
4325 	ccb->casync.async_arg_size = 0;
4326 	size = xpt_async_size(async_code);
4327 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
4328 	    ("xpt_async: func %#x %s aync_code %d %s\n",
4329 		ccb->ccb_h.func_code,
4330 		xpt_action_name(ccb->ccb_h.func_code),
4331 		async_code,
4332 		xpt_async_string(async_code)));
4333 	if (size > 0 && async_arg != NULL) {
4334 		ccb->casync.async_arg_ptr = malloc(size, M_CAMXPT, M_NOWAIT);
4335 		if (ccb->casync.async_arg_ptr == NULL) {
4336 			xpt_print(path, "Can't allocate argument to send %s\n",
4337 			    xpt_async_string(async_code));
4338 			xpt_free_path(ccb->ccb_h.path);
4339 			xpt_free_ccb(ccb);
4340 			return;
4341 		}
4342 		memcpy(ccb->casync.async_arg_ptr, async_arg, size);
4343 		ccb->casync.async_arg_size = size;
4344 	} else if (size < 0) {
4345 		ccb->casync.async_arg_ptr = async_arg;
4346 		ccb->casync.async_arg_size = size;
4347 	}
4348 	if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD)
4349 		xpt_freeze_devq(path, 1);
4350 	else
4351 		xpt_freeze_simq(path->bus->sim, 1);
4352 	xpt_action(ccb);
4353 }
4354 
4355 static void
4356 xpt_dev_async_default(uint32_t async_code, struct cam_eb *bus,
4357 		      struct cam_et *target, struct cam_ed *device,
4358 		      void *async_arg)
4359 {
4360 
4361 	/*
4362 	 * We only need to handle events for real devices.
4363 	 */
4364 	if (target->target_id == CAM_TARGET_WILDCARD
4365 	 || device->lun_id == CAM_LUN_WILDCARD)
4366 		return;
4367 
4368 	printf("%s called\n", __func__);
4369 }
4370 
4371 static uint32_t
4372 xpt_freeze_devq_device(struct cam_ed *dev, u_int count)
4373 {
4374 	struct cam_devq	*devq;
4375 	uint32_t freeze;
4376 
4377 	devq = dev->sim->devq;
4378 	mtx_assert(&devq->send_mtx, MA_OWNED);
4379 	CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE,
4380 	    ("xpt_freeze_devq_device(%d) %u->%u\n", count,
4381 	    dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt + count));
4382 	freeze = (dev->ccbq.queue.qfrozen_cnt += count);
4383 	/* Remove frozen device from sendq. */
4384 	if (device_is_queued(dev))
4385 		camq_remove(&devq->send_queue, dev->devq_entry.index);
4386 	return (freeze);
4387 }
4388 
4389 uint32_t
4390 xpt_freeze_devq(struct cam_path *path, u_int count)
4391 {
4392 	struct cam_ed	*dev = path->device;
4393 	struct cam_devq	*devq;
4394 	uint32_t	 freeze;
4395 
4396 	devq = dev->sim->devq;
4397 	mtx_lock(&devq->send_mtx);
4398 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_freeze_devq(%d)\n", count));
4399 	freeze = xpt_freeze_devq_device(dev, count);
4400 	mtx_unlock(&devq->send_mtx);
4401 	return (freeze);
4402 }
4403 
4404 uint32_t
4405 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4406 {
4407 	struct cam_devq	*devq;
4408 	uint32_t	 freeze;
4409 
4410 	devq = sim->devq;
4411 	mtx_lock(&devq->send_mtx);
4412 	freeze = (devq->send_queue.qfrozen_cnt += count);
4413 	mtx_unlock(&devq->send_mtx);
4414 	return (freeze);
4415 }
4416 
4417 static void
4418 xpt_release_devq_timeout(void *arg)
4419 {
4420 	struct cam_ed *dev;
4421 	struct cam_devq *devq;
4422 
4423 	dev = (struct cam_ed *)arg;
4424 	CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE, ("xpt_release_devq_timeout\n"));
4425 	devq = dev->sim->devq;
4426 	mtx_assert(&devq->send_mtx, MA_OWNED);
4427 	if (xpt_release_devq_device(dev, /*count*/1, /*run_queue*/TRUE))
4428 		xpt_run_devq(devq);
4429 }
4430 
4431 void
4432 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4433 {
4434 	struct cam_ed *dev;
4435 	struct cam_devq *devq;
4436 
4437 	CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_devq(%d, %d)\n",
4438 	    count, run_queue));
4439 	dev = path->device;
4440 	devq = dev->sim->devq;
4441 	mtx_lock(&devq->send_mtx);
4442 	if (xpt_release_devq_device(dev, count, run_queue))
4443 		xpt_run_devq(dev->sim->devq);
4444 	mtx_unlock(&devq->send_mtx);
4445 }
4446 
4447 static int
4448 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4449 {
4450 
4451 	mtx_assert(&dev->sim->devq->send_mtx, MA_OWNED);
4452 	CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE,
4453 	    ("xpt_release_devq_device(%d, %d) %u->%u\n", count, run_queue,
4454 	    dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt - count));
4455 	if (count > dev->ccbq.queue.qfrozen_cnt) {
4456 #ifdef INVARIANTS
4457 		printf("xpt_release_devq(): requested %u > present %u\n",
4458 		    count, dev->ccbq.queue.qfrozen_cnt);
4459 #endif
4460 		count = dev->ccbq.queue.qfrozen_cnt;
4461 	}
4462 	dev->ccbq.queue.qfrozen_cnt -= count;
4463 	if (dev->ccbq.queue.qfrozen_cnt == 0) {
4464 		/*
4465 		 * No longer need to wait for a successful
4466 		 * command completion.
4467 		 */
4468 		dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4469 		/*
4470 		 * Remove any timeouts that might be scheduled
4471 		 * to release this queue.
4472 		 */
4473 		if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4474 			callout_stop(&dev->callout);
4475 			dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4476 		}
4477 		/*
4478 		 * Now that we are unfrozen schedule the
4479 		 * device so any pending transactions are
4480 		 * run.
4481 		 */
4482 		xpt_schedule_devq(dev->sim->devq, dev);
4483 	} else
4484 		run_queue = 0;
4485 	return (run_queue);
4486 }
4487 
4488 void
4489 xpt_release_simq(struct cam_sim *sim, int run_queue)
4490 {
4491 	struct cam_devq	*devq;
4492 
4493 	devq = sim->devq;
4494 	mtx_lock(&devq->send_mtx);
4495 	if (devq->send_queue.qfrozen_cnt <= 0) {
4496 #ifdef INVARIANTS
4497 		printf("xpt_release_simq: requested 1 > present %u\n",
4498 		    devq->send_queue.qfrozen_cnt);
4499 #endif
4500 	} else
4501 		devq->send_queue.qfrozen_cnt--;
4502 	if (devq->send_queue.qfrozen_cnt == 0) {
4503 		if (run_queue) {
4504 			/*
4505 			 * Now that we are unfrozen run the send queue.
4506 			 */
4507 			xpt_run_devq(sim->devq);
4508 		}
4509 	}
4510 	mtx_unlock(&devq->send_mtx);
4511 }
4512 
4513 void
4514 xpt_done(union ccb *done_ccb)
4515 {
4516 	struct cam_doneq *queue;
4517 	int	run, hash;
4518 
4519 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
4520 	if (done_ccb->ccb_h.func_code == XPT_SCSI_IO &&
4521 	    done_ccb->csio.bio != NULL)
4522 		biotrack(done_ccb->csio.bio, __func__);
4523 #endif
4524 
4525 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
4526 	    ("xpt_done: func= %#x %s status %#x\n",
4527 		done_ccb->ccb_h.func_code,
4528 		xpt_action_name(done_ccb->ccb_h.func_code),
4529 		done_ccb->ccb_h.status));
4530 	if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0) {
4531 		CAM_PROBE1(xpt, done, done_ccb);
4532 		return;
4533 	}
4534 
4535 	/* Store the time the ccb was in the sim */
4536 	done_ccb->ccb_h.qos.periph_data = cam_iosched_delta_t(done_ccb->ccb_h.qos.periph_data);
4537 	done_ccb->ccb_h.status |= CAM_QOS_VALID;
4538 	hash = (u_int)(done_ccb->ccb_h.path_id + done_ccb->ccb_h.target_id +
4539 	    done_ccb->ccb_h.target_lun) % cam_num_doneqs;
4540 	queue = &cam_doneqs[hash];
4541 	mtx_lock(&queue->cam_doneq_mtx);
4542 	run = (queue->cam_doneq_sleep && STAILQ_EMPTY(&queue->cam_doneq));
4543 	STAILQ_INSERT_TAIL(&queue->cam_doneq, &done_ccb->ccb_h, sim_links.stqe);
4544 	done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4545 	mtx_unlock(&queue->cam_doneq_mtx);
4546 	if (run && !dumping)
4547 		wakeup(&queue->cam_doneq);
4548 }
4549 
4550 void
4551 xpt_done_direct(union ccb *done_ccb)
4552 {
4553 
4554 	CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
4555 	    ("xpt_done_direct: status %#x\n", done_ccb->ccb_h.status));
4556 	if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0)
4557 		return;
4558 
4559 	/* Store the time the ccb was in the sim */
4560 	done_ccb->ccb_h.qos.periph_data = cam_iosched_delta_t(done_ccb->ccb_h.qos.periph_data);
4561 	done_ccb->ccb_h.status |= CAM_QOS_VALID;
4562 	xpt_done_process(&done_ccb->ccb_h);
4563 }
4564 
4565 union ccb *
4566 xpt_alloc_ccb(void)
4567 {
4568 	union ccb *new_ccb;
4569 
4570 	new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4571 	return (new_ccb);
4572 }
4573 
4574 union ccb *
4575 xpt_alloc_ccb_nowait(void)
4576 {
4577 	union ccb *new_ccb;
4578 
4579 	new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
4580 	return (new_ccb);
4581 }
4582 
4583 void
4584 xpt_free_ccb(union ccb *free_ccb)
4585 {
4586 	struct cam_periph *periph;
4587 
4588 	if (free_ccb->ccb_h.alloc_flags & CAM_CCB_FROM_UMA) {
4589 		/*
4590 		 * Looks like a CCB allocated from a periph UMA zone.
4591 		 */
4592 		periph = free_ccb->ccb_h.path->periph;
4593 		uma_zfree(periph->ccb_zone, free_ccb);
4594 	} else {
4595 		free(free_ccb, M_CAMCCB);
4596 	}
4597 }
4598 
4599 /* Private XPT functions */
4600 
4601 /*
4602  * Get a CAM control block for the caller. Charge the structure to the device
4603  * referenced by the path.  If we don't have sufficient resources to allocate
4604  * more ccbs, we return NULL.
4605  */
4606 static union ccb *
4607 xpt_get_ccb_nowait(struct cam_periph *periph)
4608 {
4609 	union ccb *new_ccb;
4610 	int alloc_flags;
4611 
4612 	if (periph->ccb_zone != NULL) {
4613 		alloc_flags = CAM_CCB_FROM_UMA;
4614 		new_ccb = uma_zalloc(periph->ccb_zone, M_ZERO|M_NOWAIT);
4615 	} else {
4616 		alloc_flags = 0;
4617 		new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
4618 	}
4619 	if (new_ccb == NULL)
4620 		return (NULL);
4621 	new_ccb->ccb_h.alloc_flags = alloc_flags;
4622 	periph->periph_allocated++;
4623 	cam_ccbq_take_opening(&periph->path->device->ccbq);
4624 	return (new_ccb);
4625 }
4626 
4627 static union ccb *
4628 xpt_get_ccb(struct cam_periph *periph)
4629 {
4630 	union ccb *new_ccb;
4631 	int alloc_flags;
4632 
4633 	cam_periph_unlock(periph);
4634 	if (periph->ccb_zone != NULL) {
4635 		alloc_flags = CAM_CCB_FROM_UMA;
4636 		new_ccb = uma_zalloc(periph->ccb_zone, M_ZERO|M_WAITOK);
4637 	} else {
4638 		alloc_flags = 0;
4639 		new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4640 	}
4641 	new_ccb->ccb_h.alloc_flags = alloc_flags;
4642 	cam_periph_lock(periph);
4643 	periph->periph_allocated++;
4644 	cam_ccbq_take_opening(&periph->path->device->ccbq);
4645 	return (new_ccb);
4646 }
4647 
4648 union ccb *
4649 cam_periph_getccb(struct cam_periph *periph, uint32_t priority)
4650 {
4651 	struct ccb_hdr *ccb_h;
4652 
4653 	CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("cam_periph_getccb\n"));
4654 	cam_periph_assert(periph, MA_OWNED);
4655 	while ((ccb_h = SLIST_FIRST(&periph->ccb_list)) == NULL ||
4656 	    ccb_h->pinfo.priority != priority) {
4657 		if (priority < periph->immediate_priority) {
4658 			periph->immediate_priority = priority;
4659 			xpt_run_allocq(periph, 0);
4660 		} else
4661 			cam_periph_sleep(periph, &periph->ccb_list, PRIBIO,
4662 			    "cgticb", 0);
4663 	}
4664 	SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
4665 	return ((union ccb *)ccb_h);
4666 }
4667 
4668 static void
4669 xpt_acquire_bus(struct cam_eb *bus)
4670 {
4671 
4672 	xpt_lock_buses();
4673 	bus->refcount++;
4674 	xpt_unlock_buses();
4675 }
4676 
4677 static void
4678 xpt_release_bus(struct cam_eb *bus)
4679 {
4680 
4681 	xpt_lock_buses();
4682 	KASSERT(bus->refcount >= 1, ("bus->refcount >= 1"));
4683 	if (--bus->refcount > 0) {
4684 		xpt_unlock_buses();
4685 		return;
4686 	}
4687 	TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
4688 	xsoftc.bus_generation++;
4689 	xpt_unlock_buses();
4690 	KASSERT(TAILQ_EMPTY(&bus->et_entries),
4691 	    ("destroying bus, but target list is not empty"));
4692 	cam_sim_release(bus->sim);
4693 	mtx_destroy(&bus->eb_mtx);
4694 	free(bus, M_CAMXPT);
4695 }
4696 
4697 static struct cam_et *
4698 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4699 {
4700 	struct cam_et *cur_target, *target;
4701 
4702 	mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
4703 	mtx_assert(&bus->eb_mtx, MA_OWNED);
4704 	target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT,
4705 					 M_NOWAIT|M_ZERO);
4706 	if (target == NULL)
4707 		return (NULL);
4708 
4709 	TAILQ_INIT(&target->ed_entries);
4710 	target->bus = bus;
4711 	target->target_id = target_id;
4712 	target->refcount = 1;
4713 	target->generation = 0;
4714 	target->luns = NULL;
4715 	target->wluns = NULL;
4716 	mtx_init(&target->luns_mtx, "CAM LUNs lock", NULL, MTX_DEF);
4717 	timevalclear(&target->last_reset);
4718 	/*
4719 	 * Hold a reference to our parent bus so it
4720 	 * will not go away before we do.
4721 	 */
4722 	bus->refcount++;
4723 
4724 	/* Insertion sort into our bus's target list */
4725 	cur_target = TAILQ_FIRST(&bus->et_entries);
4726 	while (cur_target != NULL && cur_target->target_id < target_id)
4727 		cur_target = TAILQ_NEXT(cur_target, links);
4728 	if (cur_target != NULL) {
4729 		TAILQ_INSERT_BEFORE(cur_target, target, links);
4730 	} else {
4731 		TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4732 	}
4733 	bus->generation++;
4734 	return (target);
4735 }
4736 
4737 static void
4738 xpt_acquire_target(struct cam_et *target)
4739 {
4740 	struct cam_eb *bus = target->bus;
4741 
4742 	mtx_lock(&bus->eb_mtx);
4743 	target->refcount++;
4744 	mtx_unlock(&bus->eb_mtx);
4745 }
4746 
4747 static void
4748 xpt_release_target(struct cam_et *target)
4749 {
4750 	struct cam_eb *bus = target->bus;
4751 
4752 	mtx_lock(&bus->eb_mtx);
4753 	if (--target->refcount > 0) {
4754 		mtx_unlock(&bus->eb_mtx);
4755 		return;
4756 	}
4757 	TAILQ_REMOVE(&bus->et_entries, target, links);
4758 	bus->generation++;
4759 	mtx_unlock(&bus->eb_mtx);
4760 	KASSERT(TAILQ_EMPTY(&target->ed_entries),
4761 	    ("destroying target, but device list is not empty"));
4762 	xpt_release_bus(bus);
4763 	mtx_destroy(&target->luns_mtx);
4764 	if (target->luns)
4765 		free(target->luns, M_CAMXPT);
4766 	free(target, M_CAMXPT);
4767 }
4768 
4769 static struct cam_ed *
4770 xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target,
4771 			 lun_id_t lun_id)
4772 {
4773 	struct cam_ed *device;
4774 
4775 	device = xpt_alloc_device(bus, target, lun_id);
4776 	if (device == NULL)
4777 		return (NULL);
4778 
4779 	device->mintags = 1;
4780 	device->maxtags = 1;
4781 	return (device);
4782 }
4783 
4784 static void
4785 xpt_destroy_device(void *context, int pending)
4786 {
4787 	struct cam_ed	*device = context;
4788 
4789 	mtx_lock(&device->device_mtx);
4790 	mtx_destroy(&device->device_mtx);
4791 	free(device, M_CAMDEV);
4792 }
4793 
4794 struct cam_ed *
4795 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4796 {
4797 	struct cam_ed	*cur_device, *device;
4798 	struct cam_devq	*devq;
4799 	cam_status status;
4800 
4801 	mtx_assert(&bus->eb_mtx, MA_OWNED);
4802 	/* Make space for us in the device queue on our bus */
4803 	devq = bus->sim->devq;
4804 	mtx_lock(&devq->send_mtx);
4805 	status = cam_devq_resize(devq, devq->send_queue.array_size + 1);
4806 	mtx_unlock(&devq->send_mtx);
4807 	if (status != CAM_REQ_CMP)
4808 		return (NULL);
4809 
4810 	device = (struct cam_ed *)malloc(sizeof(*device),
4811 					 M_CAMDEV, M_NOWAIT|M_ZERO);
4812 	if (device == NULL)
4813 		return (NULL);
4814 
4815 	cam_init_pinfo(&device->devq_entry);
4816 	device->target = target;
4817 	device->lun_id = lun_id;
4818 	device->sim = bus->sim;
4819 	if (cam_ccbq_init(&device->ccbq,
4820 			  bus->sim->max_dev_openings) != 0) {
4821 		free(device, M_CAMDEV);
4822 		return (NULL);
4823 	}
4824 	SLIST_INIT(&device->asyncs);
4825 	SLIST_INIT(&device->periphs);
4826 	device->generation = 0;
4827 	device->flags = CAM_DEV_UNCONFIGURED;
4828 	device->tag_delay_count = 0;
4829 	device->tag_saved_openings = 0;
4830 	device->refcount = 1;
4831 	mtx_init(&device->device_mtx, "CAM device lock", NULL, MTX_DEF);
4832 	callout_init_mtx(&device->callout, &devq->send_mtx, 0);
4833 	TASK_INIT(&device->device_destroy_task, 0, xpt_destroy_device, device);
4834 	/*
4835 	 * Hold a reference to our parent bus so it
4836 	 * will not go away before we do.
4837 	 */
4838 	target->refcount++;
4839 
4840 	cur_device = TAILQ_FIRST(&target->ed_entries);
4841 	while (cur_device != NULL && cur_device->lun_id < lun_id)
4842 		cur_device = TAILQ_NEXT(cur_device, links);
4843 	if (cur_device != NULL)
4844 		TAILQ_INSERT_BEFORE(cur_device, device, links);
4845 	else
4846 		TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4847 	target->generation++;
4848 	return (device);
4849 }
4850 
4851 void
4852 xpt_acquire_device(struct cam_ed *device)
4853 {
4854 	struct cam_eb *bus = device->target->bus;
4855 
4856 	mtx_lock(&bus->eb_mtx);
4857 	device->refcount++;
4858 	mtx_unlock(&bus->eb_mtx);
4859 }
4860 
4861 void
4862 xpt_release_device(struct cam_ed *device)
4863 {
4864 	struct cam_eb *bus = device->target->bus;
4865 	struct cam_devq *devq;
4866 
4867 	mtx_lock(&bus->eb_mtx);
4868 	if (--device->refcount > 0) {
4869 		mtx_unlock(&bus->eb_mtx);
4870 		return;
4871 	}
4872 
4873 	TAILQ_REMOVE(&device->target->ed_entries, device,links);
4874 	device->target->generation++;
4875 	mtx_unlock(&bus->eb_mtx);
4876 
4877 	/* Release our slot in the devq */
4878 	devq = bus->sim->devq;
4879 	mtx_lock(&devq->send_mtx);
4880 	cam_devq_resize(devq, devq->send_queue.array_size - 1);
4881 
4882 	KASSERT(SLIST_EMPTY(&device->periphs),
4883 	    ("destroying device, but periphs list is not empty"));
4884 	KASSERT(device->devq_entry.index == CAM_UNQUEUED_INDEX,
4885 	    ("destroying device while still queued for ccbs"));
4886 
4887 	/* The send_mtx must be held when accessing the callout */
4888 	if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
4889 		callout_stop(&device->callout);
4890 
4891 	mtx_unlock(&devq->send_mtx);
4892 
4893 	xpt_release_target(device->target);
4894 
4895 	cam_ccbq_fini(&device->ccbq);
4896 	/*
4897 	 * Free allocated memory.  free(9) does nothing if the
4898 	 * supplied pointer is NULL, so it is safe to call without
4899 	 * checking.
4900 	 */
4901 	free(device->supported_vpds, M_CAMXPT);
4902 	free(device->device_id, M_CAMXPT);
4903 	free(device->ext_inq, M_CAMXPT);
4904 	free(device->physpath, M_CAMXPT);
4905 	free(device->rcap_buf, M_CAMXPT);
4906 	free(device->serial_num, M_CAMXPT);
4907 	free(device->nvme_data, M_CAMXPT);
4908 	free(device->nvme_cdata, M_CAMXPT);
4909 	taskqueue_enqueue(xsoftc.xpt_taskq, &device->device_destroy_task);
4910 }
4911 
4912 uint32_t
4913 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
4914 {
4915 	int	result;
4916 	struct	cam_ed *dev;
4917 
4918 	dev = path->device;
4919 	mtx_lock(&dev->sim->devq->send_mtx);
4920 	result = cam_ccbq_resize(&dev->ccbq, newopenings);
4921 	mtx_unlock(&dev->sim->devq->send_mtx);
4922 	if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
4923 	 || (dev->inq_flags & SID_CmdQue) != 0)
4924 		dev->tag_saved_openings = newopenings;
4925 	return (result);
4926 }
4927 
4928 static struct cam_eb *
4929 xpt_find_bus(path_id_t path_id)
4930 {
4931 	struct cam_eb *bus;
4932 
4933 	xpt_lock_buses();
4934 	for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4935 	     bus != NULL;
4936 	     bus = TAILQ_NEXT(bus, links)) {
4937 		if (bus->path_id == path_id) {
4938 			bus->refcount++;
4939 			break;
4940 		}
4941 	}
4942 	xpt_unlock_buses();
4943 	return (bus);
4944 }
4945 
4946 static struct cam_et *
4947 xpt_find_target(struct cam_eb *bus, target_id_t	target_id)
4948 {
4949 	struct cam_et *target;
4950 
4951 	mtx_assert(&bus->eb_mtx, MA_OWNED);
4952 	for (target = TAILQ_FIRST(&bus->et_entries);
4953 	     target != NULL;
4954 	     target = TAILQ_NEXT(target, links)) {
4955 		if (target->target_id == target_id) {
4956 			target->refcount++;
4957 			break;
4958 		}
4959 	}
4960 	return (target);
4961 }
4962 
4963 static struct cam_ed *
4964 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
4965 {
4966 	struct cam_ed *device;
4967 
4968 	mtx_assert(&target->bus->eb_mtx, MA_OWNED);
4969 	for (device = TAILQ_FIRST(&target->ed_entries);
4970 	     device != NULL;
4971 	     device = TAILQ_NEXT(device, links)) {
4972 		if (device->lun_id == lun_id) {
4973 			device->refcount++;
4974 			break;
4975 		}
4976 	}
4977 	return (device);
4978 }
4979 
4980 void
4981 xpt_start_tags(struct cam_path *path)
4982 {
4983 	struct ccb_relsim crs;
4984 	struct cam_ed *device;
4985 	struct cam_sim *sim;
4986 	int    newopenings;
4987 
4988 	device = path->device;
4989 	sim = path->bus->sim;
4990 	device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4991 	xpt_freeze_devq(path, /*count*/1);
4992 	device->inq_flags |= SID_CmdQue;
4993 	if (device->tag_saved_openings != 0)
4994 		newopenings = device->tag_saved_openings;
4995 	else
4996 		newopenings = min(device->maxtags,
4997 				  sim->max_tagged_dev_openings);
4998 	xpt_dev_ccbq_resize(path, newopenings);
4999 	xpt_async(AC_GETDEV_CHANGED, path, NULL);
5000 	memset(&crs, 0, sizeof(crs));
5001 	xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
5002 	crs.ccb_h.func_code = XPT_REL_SIMQ;
5003 	crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
5004 	crs.openings
5005 	    = crs.release_timeout
5006 	    = crs.qfrozen_cnt
5007 	    = 0;
5008 	xpt_action((union ccb *)&crs);
5009 }
5010 
5011 void
5012 xpt_stop_tags(struct cam_path *path)
5013 {
5014 	struct ccb_relsim crs;
5015 	struct cam_ed *device;
5016 	struct cam_sim *sim;
5017 
5018 	device = path->device;
5019 	sim = path->bus->sim;
5020 	device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
5021 	device->tag_delay_count = 0;
5022 	xpt_freeze_devq(path, /*count*/1);
5023 	device->inq_flags &= ~SID_CmdQue;
5024 	xpt_dev_ccbq_resize(path, sim->max_dev_openings);
5025 	xpt_async(AC_GETDEV_CHANGED, path, NULL);
5026 	memset(&crs, 0, sizeof(crs));
5027 	xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
5028 	crs.ccb_h.func_code = XPT_REL_SIMQ;
5029 	crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
5030 	crs.openings
5031 	    = crs.release_timeout
5032 	    = crs.qfrozen_cnt
5033 	    = 0;
5034 	xpt_action((union ccb *)&crs);
5035 }
5036 
5037 /*
5038  * Assume all possible buses are detected by this time, so allow boot
5039  * as soon as they all are scanned.
5040  */
5041 static void
5042 xpt_boot_delay(void *arg)
5043 {
5044 
5045 	xpt_release_boot();
5046 }
5047 
5048 /*
5049  * Now that all config hooks have completed, start boot_delay timer,
5050  * waiting for possibly still undetected buses (USB) to appear.
5051  */
5052 static void
5053 xpt_ch_done(void *arg)
5054 {
5055 
5056 	callout_init(&xsoftc.boot_callout, 1);
5057 	callout_reset_sbt(&xsoftc.boot_callout, SBT_1MS * xsoftc.boot_delay,
5058 	    SBT_1MS, xpt_boot_delay, NULL, 0);
5059 }
5060 SYSINIT(xpt_hw_delay, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_ANY, xpt_ch_done, NULL);
5061 
5062 /*
5063  * Now that interrupts are enabled, go find our devices
5064  */
5065 static void
5066 xpt_config(void *arg)
5067 {
5068 	if (taskqueue_start_threads(&xsoftc.xpt_taskq, 1, PRIBIO, "CAM taskq"))
5069 		printf("xpt_config: failed to create taskqueue thread.\n");
5070 
5071 	/* Setup debugging path */
5072 	if (cam_dflags != CAM_DEBUG_NONE) {
5073 		if (xpt_create_path(&cam_dpath, NULL,
5074 				    CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
5075 				    CAM_DEBUG_LUN) != CAM_REQ_CMP) {
5076 			printf(
5077 "xpt_config: xpt_create_path() failed for debug target %d:%d:%d, debugging disabled\n",
5078 			    CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
5079 			cam_dflags = CAM_DEBUG_NONE;
5080 		}
5081 	} else
5082 		cam_dpath = NULL;
5083 
5084 	periphdriver_init(1);
5085 	xpt_hold_boot();
5086 
5087 	/* Fire up rescan thread. */
5088 	if (kproc_kthread_add(xpt_scanner_thread, NULL, &cam_proc, NULL, 0, 0,
5089 	    "cam", "scanner")) {
5090 		printf("xpt_config: failed to create rescan thread.\n");
5091 	}
5092 }
5093 
5094 void
5095 xpt_hold_boot_locked(void)
5096 {
5097 
5098 	if (xsoftc.buses_to_config++ == 0)
5099 		root_mount_hold_token("CAM", &xsoftc.xpt_rootmount);
5100 	CAM_PROBE1(xpt, hold__boot, xsoftc.buses_to_config);
5101 }
5102 
5103 void
5104 xpt_hold_boot(void)
5105 {
5106 
5107 	xpt_lock_buses();
5108 	xpt_hold_boot_locked();
5109 	xpt_unlock_buses();
5110 }
5111 
5112 void
5113 xpt_release_boot(void)
5114 {
5115 
5116 	xpt_lock_buses();
5117 	--xsoftc.buses_to_config;
5118 	CAM_PROBE1(xpt, release__boot, xsoftc.buses_to_config);
5119 	if (xsoftc.buses_to_config == 0) {
5120 		if (xsoftc.buses_config_done == 0) {
5121 			xsoftc.buses_config_done = 1;
5122 			xsoftc.buses_to_config++;
5123 			CAM_PROBE1(xpt, hold__boot, xsoftc.buses_to_config);
5124 			TASK_INIT(&xsoftc.boot_task, 0, xpt_finishconfig_task,
5125 			    NULL);
5126 			taskqueue_enqueue(taskqueue_thread, &xsoftc.boot_task);
5127 		} else
5128 			root_mount_rel(&xsoftc.xpt_rootmount);
5129 	}
5130 	xpt_unlock_buses();
5131 }
5132 
5133 /*
5134  * If the given device only has one peripheral attached to it, and if that
5135  * peripheral is the passthrough driver, announce it.  This insures that the
5136  * user sees some sort of announcement for every peripheral in their system.
5137  */
5138 static int
5139 xptpassannouncefunc(struct cam_ed *device, void *arg)
5140 {
5141 	struct cam_periph *periph;
5142 	int i;
5143 
5144 	for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
5145 	     periph = SLIST_NEXT(periph, periph_links), i++);
5146 
5147 	periph = SLIST_FIRST(&device->periphs);
5148 	if ((i == 1)
5149 	 && (strncmp(periph->periph_name, "pass", 4) == 0))
5150 		xpt_announce_periph(periph, NULL);
5151 
5152 	return(1);
5153 }
5154 
5155 static void
5156 xpt_finishconfig_task(void *context, int pending)
5157 {
5158 
5159 	periphdriver_init(2);
5160 	/*
5161 	 * Check for devices with no "standard" peripheral driver
5162 	 * attached.  For any devices like that, announce the
5163 	 * passthrough driver so the user will see something.
5164 	 */
5165 	if (!bootverbose)
5166 		xpt_for_all_devices(xptpassannouncefunc, NULL);
5167 
5168 	xpt_release_boot();
5169 }
5170 
5171 cam_status
5172 xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
5173 		   struct cam_path *path)
5174 {
5175 	struct ccb_setasync csa;
5176 	cam_status status;
5177 	bool xptpath = false;
5178 
5179 	if (path == NULL) {
5180 		status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
5181 					 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
5182 		if (status != CAM_REQ_CMP)
5183 			return (status);
5184 		xpt_path_lock(path);
5185 		xptpath = true;
5186 	}
5187 
5188 	memset(&csa, 0, sizeof(csa));
5189 	xpt_setup_ccb(&csa.ccb_h, path, CAM_PRIORITY_NORMAL);
5190 	csa.ccb_h.func_code = XPT_SASYNC_CB;
5191 	csa.event_enable = event;
5192 	csa.callback = cbfunc;
5193 	csa.callback_arg = cbarg;
5194 	xpt_action((union ccb *)&csa);
5195 	status = csa.ccb_h.status;
5196 
5197 	CAM_DEBUG(csa.ccb_h.path, CAM_DEBUG_TRACE,
5198 	    ("xpt_register_async: func %p\n", cbfunc));
5199 
5200 	if (xptpath) {
5201 		xpt_path_unlock(path);
5202 		xpt_free_path(path);
5203 	}
5204 
5205 	if ((status == CAM_REQ_CMP) &&
5206 	    (csa.event_enable & AC_FOUND_DEVICE)) {
5207 		/*
5208 		 * Get this peripheral up to date with all
5209 		 * the currently existing devices.
5210 		 */
5211 		xpt_for_all_devices(xptsetasyncfunc, &csa);
5212 	}
5213 	if ((status == CAM_REQ_CMP) &&
5214 	    (csa.event_enable & AC_PATH_REGISTERED)) {
5215 		/*
5216 		 * Get this peripheral up to date with all
5217 		 * the currently existing buses.
5218 		 */
5219 		xpt_for_all_busses(xptsetasyncbusfunc, &csa);
5220 	}
5221 
5222 	return (status);
5223 }
5224 
5225 static void
5226 xptaction(struct cam_sim *sim, union ccb *work_ccb)
5227 {
5228 	CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
5229 
5230 	switch (work_ccb->ccb_h.func_code) {
5231 	/* Common cases first */
5232 	case XPT_PATH_INQ:		/* Path routing inquiry */
5233 	{
5234 		struct ccb_pathinq *cpi;
5235 
5236 		cpi = &work_ccb->cpi;
5237 		cpi->version_num = 1; /* XXX??? */
5238 		cpi->hba_inquiry = 0;
5239 		cpi->target_sprt = 0;
5240 		cpi->hba_misc = 0;
5241 		cpi->hba_eng_cnt = 0;
5242 		cpi->max_target = 0;
5243 		cpi->max_lun = 0;
5244 		cpi->initiator_id = 0;
5245 		strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
5246 		strlcpy(cpi->hba_vid, "", HBA_IDLEN);
5247 		strlcpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
5248 		cpi->unit_number = sim->unit_number;
5249 		cpi->bus_id = sim->bus_id;
5250 		cpi->base_transfer_speed = 0;
5251 		cpi->protocol = PROTO_UNSPECIFIED;
5252 		cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
5253 		cpi->transport = XPORT_UNSPECIFIED;
5254 		cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
5255 		cpi->ccb_h.status = CAM_REQ_CMP;
5256 		break;
5257 	}
5258 	default:
5259 		work_ccb->ccb_h.status = CAM_REQ_INVALID;
5260 		break;
5261 	}
5262 	xpt_done(work_ccb);
5263 }
5264 
5265 /*
5266  * The xpt as a "controller" has no interrupt sources, so polling
5267  * is a no-op.
5268  */
5269 static void
5270 xptpoll(struct cam_sim *sim)
5271 {
5272 }
5273 
5274 void
5275 xpt_lock_buses(void)
5276 {
5277 	mtx_lock(&xsoftc.xpt_topo_lock);
5278 }
5279 
5280 void
5281 xpt_unlock_buses(void)
5282 {
5283 	mtx_unlock(&xsoftc.xpt_topo_lock);
5284 }
5285 
5286 struct mtx *
5287 xpt_path_mtx(struct cam_path *path)
5288 {
5289 
5290 	return (&path->device->device_mtx);
5291 }
5292 
5293 static void
5294 xpt_done_process(struct ccb_hdr *ccb_h)
5295 {
5296 	struct cam_sim *sim = NULL;
5297 	struct cam_devq *devq = NULL;
5298 	struct mtx *mtx = NULL;
5299 
5300 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
5301 	struct ccb_scsiio *csio;
5302 
5303 	if (ccb_h->func_code == XPT_SCSI_IO) {
5304 		csio = &((union ccb *)ccb_h)->csio;
5305 		if (csio->bio != NULL)
5306 			biotrack(csio->bio, __func__);
5307 	}
5308 #endif
5309 
5310 	if (ccb_h->flags & CAM_HIGH_POWER) {
5311 		struct highpowerlist	*hphead;
5312 		struct cam_ed		*device;
5313 
5314 		mtx_lock(&xsoftc.xpt_highpower_lock);
5315 		hphead = &xsoftc.highpowerq;
5316 
5317 		device = STAILQ_FIRST(hphead);
5318 
5319 		/*
5320 		 * Increment the count since this command is done.
5321 		 */
5322 		xsoftc.num_highpower++;
5323 
5324 		/*
5325 		 * Any high powered commands queued up?
5326 		 */
5327 		if (device != NULL) {
5328 			STAILQ_REMOVE_HEAD(hphead, highpowerq_entry);
5329 			mtx_unlock(&xsoftc.xpt_highpower_lock);
5330 
5331 			mtx_lock(&device->sim->devq->send_mtx);
5332 			xpt_release_devq_device(device,
5333 					 /*count*/1, /*runqueue*/TRUE);
5334 			mtx_unlock(&device->sim->devq->send_mtx);
5335 		} else
5336 			mtx_unlock(&xsoftc.xpt_highpower_lock);
5337 	}
5338 
5339 	/*
5340 	 * Insulate against a race where the periph is destroyed but CCBs are
5341 	 * still not all processed. This shouldn't happen, but allows us better
5342 	 * bug diagnostic when it does.
5343 	 */
5344 	if (ccb_h->path->bus)
5345 		sim = ccb_h->path->bus->sim;
5346 
5347 	if (ccb_h->status & CAM_RELEASE_SIMQ) {
5348 		KASSERT(sim, ("sim missing for CAM_RELEASE_SIMQ request"));
5349 		xpt_release_simq(sim, /*run_queue*/FALSE);
5350 		ccb_h->status &= ~CAM_RELEASE_SIMQ;
5351 	}
5352 
5353 	if ((ccb_h->flags & CAM_DEV_QFRZDIS)
5354 	 && (ccb_h->status & CAM_DEV_QFRZN)) {
5355 		xpt_release_devq(ccb_h->path, /*count*/1, /*run_queue*/TRUE);
5356 		ccb_h->status &= ~CAM_DEV_QFRZN;
5357 	}
5358 
5359 	if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
5360 		struct cam_ed *dev = ccb_h->path->device;
5361 
5362 		if (sim)
5363 			devq = sim->devq;
5364 		KASSERT(devq, ("Periph disappeared with CCB %p %s request pending.",
5365 			ccb_h, xpt_action_name(ccb_h->func_code)));
5366 
5367 		mtx_lock(&devq->send_mtx);
5368 		devq->send_active--;
5369 		devq->send_openings++;
5370 		cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
5371 
5372 		if (((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
5373 		  && (dev->ccbq.dev_active == 0))) {
5374 			dev->flags &= ~CAM_DEV_REL_ON_QUEUE_EMPTY;
5375 			xpt_release_devq_device(dev, /*count*/1,
5376 					 /*run_queue*/FALSE);
5377 		}
5378 
5379 		if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
5380 		  && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)) {
5381 			dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
5382 			xpt_release_devq_device(dev, /*count*/1,
5383 					 /*run_queue*/FALSE);
5384 		}
5385 
5386 		if (!device_is_queued(dev))
5387 			(void)xpt_schedule_devq(devq, dev);
5388 		xpt_run_devq(devq);
5389 		mtx_unlock(&devq->send_mtx);
5390 
5391 		if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0) {
5392 			mtx = xpt_path_mtx(ccb_h->path);
5393 			mtx_lock(mtx);
5394 
5395 			if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5396 			 && (--dev->tag_delay_count == 0))
5397 				xpt_start_tags(ccb_h->path);
5398 		}
5399 	}
5400 
5401 	if ((ccb_h->flags & CAM_UNLOCKED) == 0) {
5402 		if (mtx == NULL) {
5403 			mtx = xpt_path_mtx(ccb_h->path);
5404 			mtx_lock(mtx);
5405 		}
5406 	} else {
5407 		if (mtx != NULL) {
5408 			mtx_unlock(mtx);
5409 			mtx = NULL;
5410 		}
5411 	}
5412 
5413 	/*
5414 	 * Call as late as possible. Do we want an early one too before the
5415 	 * unfreeze / releases above?
5416 	 */
5417 	CAM_PROBE1(xpt, done, (union ccb *)ccb_h);	/* container_of? */
5418 	/* Call the peripheral driver's callback */
5419 	ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
5420 	(*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
5421 	if (mtx != NULL)
5422 		mtx_unlock(mtx);
5423 }
5424 
5425 /*
5426  * Parameterize instead and use xpt_done_td?
5427  */
5428 static void
5429 xpt_async_td(void *arg)
5430 {
5431 	struct cam_doneq *queue = arg;
5432 	struct ccb_hdr *ccb_h;
5433 	STAILQ_HEAD(, ccb_hdr)	doneq;
5434 
5435 	STAILQ_INIT(&doneq);
5436 	mtx_lock(&queue->cam_doneq_mtx);
5437 	while (1) {
5438 		while (STAILQ_EMPTY(&queue->cam_doneq))
5439 			msleep(&queue->cam_doneq, &queue->cam_doneq_mtx,
5440 			    PRIBIO, "-", 0);
5441 		STAILQ_CONCAT(&doneq, &queue->cam_doneq);
5442 		mtx_unlock(&queue->cam_doneq_mtx);
5443 
5444 		while ((ccb_h = STAILQ_FIRST(&doneq)) != NULL) {
5445 			STAILQ_REMOVE_HEAD(&doneq, sim_links.stqe);
5446 			xpt_done_process(ccb_h);
5447 		}
5448 
5449 		mtx_lock(&queue->cam_doneq_mtx);
5450 	}
5451 }
5452 
5453 void
5454 xpt_done_td(void *arg)
5455 {
5456 	struct cam_doneq *queue = arg;
5457 	struct ccb_hdr *ccb_h;
5458 	STAILQ_HEAD(, ccb_hdr)	doneq;
5459 
5460 	STAILQ_INIT(&doneq);
5461 	mtx_lock(&queue->cam_doneq_mtx);
5462 	while (1) {
5463 		while (STAILQ_EMPTY(&queue->cam_doneq)) {
5464 			queue->cam_doneq_sleep = 1;
5465 			msleep(&queue->cam_doneq, &queue->cam_doneq_mtx,
5466 			    PRIBIO, "-", 0);
5467 			queue->cam_doneq_sleep = 0;
5468 		}
5469 		STAILQ_CONCAT(&doneq, &queue->cam_doneq);
5470 		mtx_unlock(&queue->cam_doneq_mtx);
5471 
5472 		THREAD_NO_SLEEPING();
5473 		while ((ccb_h = STAILQ_FIRST(&doneq)) != NULL) {
5474 			STAILQ_REMOVE_HEAD(&doneq, sim_links.stqe);
5475 			xpt_done_process(ccb_h);
5476 		}
5477 		THREAD_SLEEPING_OK();
5478 
5479 		mtx_lock(&queue->cam_doneq_mtx);
5480 	}
5481 }
5482 
5483 static void
5484 camisr_runqueue(void)
5485 {
5486 	struct	ccb_hdr *ccb_h;
5487 	struct cam_doneq *queue;
5488 	int i;
5489 
5490 	/* Process global queues. */
5491 	for (i = 0; i < cam_num_doneqs; i++) {
5492 		queue = &cam_doneqs[i];
5493 		mtx_lock(&queue->cam_doneq_mtx);
5494 		while ((ccb_h = STAILQ_FIRST(&queue->cam_doneq)) != NULL) {
5495 			STAILQ_REMOVE_HEAD(&queue->cam_doneq, sim_links.stqe);
5496 			mtx_unlock(&queue->cam_doneq_mtx);
5497 			xpt_done_process(ccb_h);
5498 			mtx_lock(&queue->cam_doneq_mtx);
5499 		}
5500 		mtx_unlock(&queue->cam_doneq_mtx);
5501 	}
5502 }
5503 
5504 /**
5505  * @brief Return the device_t associated with the path
5506  *
5507  * When a SIM is created, it registers a bus with a NEWBUS device_t. This is
5508  * stored in the internal cam_eb bus structure. There is no guarnatee any given
5509  * path will have a @c device_t associated with it (it's legal to call @c
5510  * xpt_bus_register with a @c NULL @c device_t.
5511  *
5512  * @param path		Path to return the device_t for.
5513  */
5514 device_t
5515 xpt_path_sim_device(const struct cam_path *path)
5516 {
5517 	return (path->bus->parent_dev);
5518 }
5519 
5520 struct kv
5521 {
5522 	uint32_t v;
5523 	const char *name;
5524 };
5525 
5526 static struct kv map[] = {
5527 	{ XPT_NOOP, "XPT_NOOP" },
5528 	{ XPT_SCSI_IO, "XPT_SCSI_IO" },
5529 	{ XPT_GDEV_TYPE, "XPT_GDEV_TYPE" },
5530 	{ XPT_GDEVLIST, "XPT_GDEVLIST" },
5531 	{ XPT_PATH_INQ, "XPT_PATH_INQ" },
5532 	{ XPT_REL_SIMQ, "XPT_REL_SIMQ" },
5533 	{ XPT_SASYNC_CB, "XPT_SASYNC_CB" },
5534 	{ XPT_SDEV_TYPE, "XPT_SDEV_TYPE" },
5535 	{ XPT_SCAN_BUS, "XPT_SCAN_BUS" },
5536 	{ XPT_DEV_MATCH, "XPT_DEV_MATCH" },
5537 	{ XPT_DEBUG, "XPT_DEBUG" },
5538 	{ XPT_PATH_STATS, "XPT_PATH_STATS" },
5539 	{ XPT_GDEV_STATS, "XPT_GDEV_STATS" },
5540 	{ XPT_DEV_ADVINFO, "XPT_DEV_ADVINFO" },
5541 	{ XPT_ASYNC, "XPT_ASYNC" },
5542 	{ XPT_ABORT, "XPT_ABORT" },
5543 	{ XPT_RESET_BUS, "XPT_RESET_BUS" },
5544 	{ XPT_RESET_DEV, "XPT_RESET_DEV" },
5545 	{ XPT_TERM_IO, "XPT_TERM_IO" },
5546 	{ XPT_SCAN_LUN, "XPT_SCAN_LUN" },
5547 	{ XPT_GET_TRAN_SETTINGS, "XPT_GET_TRAN_SETTINGS" },
5548 	{ XPT_SET_TRAN_SETTINGS, "XPT_SET_TRAN_SETTINGS" },
5549 	{ XPT_CALC_GEOMETRY, "XPT_CALC_GEOMETRY" },
5550 	{ XPT_ATA_IO, "XPT_ATA_IO" },
5551 	{ XPT_GET_SIM_KNOB, "XPT_GET_SIM_KNOB" },
5552 	{ XPT_SET_SIM_KNOB, "XPT_SET_SIM_KNOB" },
5553 	{ XPT_NVME_IO, "XPT_NVME_IO" },
5554 	{ XPT_MMC_IO, "XPT_MMC_IO" },
5555 	{ XPT_SMP_IO, "XPT_SMP_IO" },
5556 	{ XPT_SCAN_TGT, "XPT_SCAN_TGT" },
5557 	{ XPT_NVME_ADMIN, "XPT_NVME_ADMIN" },
5558 	{ XPT_ENG_INQ, "XPT_ENG_INQ" },
5559 	{ XPT_ENG_EXEC, "XPT_ENG_EXEC" },
5560 	{ XPT_EN_LUN, "XPT_EN_LUN" },
5561 	{ XPT_TARGET_IO, "XPT_TARGET_IO" },
5562 	{ XPT_ACCEPT_TARGET_IO, "XPT_ACCEPT_TARGET_IO" },
5563 	{ XPT_CONT_TARGET_IO, "XPT_CONT_TARGET_IO" },
5564 	{ XPT_IMMED_NOTIFY, "XPT_IMMED_NOTIFY" },
5565 	{ XPT_NOTIFY_ACK, "XPT_NOTIFY_ACK" },
5566 	{ XPT_IMMEDIATE_NOTIFY, "XPT_IMMEDIATE_NOTIFY" },
5567 	{ XPT_NOTIFY_ACKNOWLEDGE, "XPT_NOTIFY_ACKNOWLEDGE" },
5568 	{ 0, 0 }
5569 };
5570 
5571 const char *
5572 xpt_action_name(uint32_t action)
5573 {
5574 	static char buffer[32];	/* Only for unknown messages -- racy */
5575 	struct kv *walker = map;
5576 
5577 	while (walker->name != NULL) {
5578 		if (walker->v == action)
5579 			return (walker->name);
5580 		walker++;
5581 	}
5582 
5583 	snprintf(buffer, sizeof(buffer), "%#x", action);
5584 	return (buffer);
5585 }
5586 
5587 void
5588 xpt_cam_path_debug(struct cam_path *path, const char *fmt, ...)
5589 {
5590 	struct sbuf sbuf;
5591 	char buf[XPT_PRINT_LEN]; /* balance to not eat too much stack */
5592 	struct sbuf *sb = sbuf_new(&sbuf, buf, sizeof(buf), SBUF_FIXEDLEN | SBUF_INCLUDENUL);
5593 	va_list ap;
5594 
5595 	sbuf_set_drain(sb, sbuf_printf_drain, NULL);
5596 	xpt_path_sbuf(path, sb);
5597 	va_start(ap, fmt);
5598 	sbuf_vprintf(sb, fmt, ap);
5599 	va_end(ap);
5600 	sbuf_finish(sb);
5601 	sbuf_delete(sb);
5602 	if (cam_debug_delay != 0)
5603 		DELAY(cam_debug_delay);
5604 }
5605 
5606 void
5607 xpt_cam_dev_debug(struct cam_ed *dev, const char *fmt, ...)
5608 {
5609 	struct sbuf sbuf;
5610 	char buf[XPT_PRINT_LEN]; /* balance to not eat too much stack */
5611 	struct sbuf *sb = sbuf_new(&sbuf, buf, sizeof(buf), SBUF_FIXEDLEN | SBUF_INCLUDENUL);
5612 	va_list ap;
5613 
5614 	sbuf_set_drain(sb, sbuf_printf_drain, NULL);
5615 	xpt_device_sbuf(dev, sb);
5616 	va_start(ap, fmt);
5617 	sbuf_vprintf(sb, fmt, ap);
5618 	va_end(ap);
5619 	sbuf_finish(sb);
5620 	sbuf_delete(sb);
5621 	if (cam_debug_delay != 0)
5622 		DELAY(cam_debug_delay);
5623 }
5624 
5625 void
5626 xpt_cam_debug(const char *fmt, ...)
5627 {
5628 	struct sbuf sbuf;
5629 	char buf[XPT_PRINT_LEN]; /* balance to not eat too much stack */
5630 	struct sbuf *sb = sbuf_new(&sbuf, buf, sizeof(buf), SBUF_FIXEDLEN | SBUF_INCLUDENUL);
5631 	va_list ap;
5632 
5633 	sbuf_set_drain(sb, sbuf_printf_drain, NULL);
5634 	sbuf_cat(sb, "cam_debug: ");
5635 	va_start(ap, fmt);
5636 	sbuf_vprintf(sb, fmt, ap);
5637 	va_end(ap);
5638 	sbuf_finish(sb);
5639 	sbuf_delete(sb);
5640 	if (cam_debug_delay != 0)
5641 		DELAY(cam_debug_delay);
5642 }
5643