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