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