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