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