2 * Implementation of the Common Access Method Transport (XPT) layer.
4 * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
5 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/types.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
41 #include <sys/fcntl.h>
42 #include <sys/interrupt.h>
44 #include <sys/taskqueue.h>
47 #include <sys/mutex.h>
48 #include <sys/sysctl.h>
49 #include <sys/kthread.h>
52 #include <cam/cam_ccb.h>
53 #include <cam/cam_periph.h>
54 #include <cam/cam_queue.h>
55 #include <cam/cam_sim.h>
56 #include <cam/cam_xpt.h>
57 #include <cam/cam_xpt_sim.h>
58 #include <cam/cam_xpt_periph.h>
59 #include <cam/cam_xpt_internal.h>
60 #include <cam/cam_debug.h>
62 #include <cam/scsi/scsi_all.h>
63 #include <cam/scsi/scsi_message.h>
64 #include <cam/scsi/scsi_pass.h>
66 #include <machine/md_var.h> /* geometry translation */
67 #include <machine/stdarg.h> /* for xpt_print below */
72 * This is the maximum number of high powered commands (e.g. start unit)
73 * that can be outstanding at a particular time.
75 #ifndef CAM_MAX_HIGHPOWER
76 #define CAM_MAX_HIGHPOWER 4
79 /* Datastructures internal to the xpt layer */
80 MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
81 MALLOC_DEFINE(M_CAMDEV, "CAM DEV", "CAM devices");
82 MALLOC_DEFINE(M_CAMCCB, "CAM CCB", "CAM CCBs");
83 MALLOC_DEFINE(M_CAMPATH, "CAM path", "CAM paths");
85 /* Object for defering XPT actions to a taskqueue */
98 u_int32_t xpt_generation;
100 /* number of high powered commands that can go through right now */
101 STAILQ_HEAD(highpowerlist, ccb_hdr) highpowerq;
104 /* queue for handling async rescan requests. */
105 TAILQ_HEAD(, ccb_hdr) ccb_scanq;
107 int buses_config_done;
109 /* Registered busses */
110 TAILQ_HEAD(,cam_eb) xpt_busses;
111 u_int bus_generation;
113 struct intr_config_hook *xpt_config_hook;
116 struct callout boot_callout;
118 struct mtx xpt_topo_lock;
124 DM_RET_FLAG_MASK = 0x0f,
127 DM_RET_DESCEND = 0x20,
129 DM_RET_ACTION_MASK = 0xf0
137 } xpt_traverse_depth;
139 struct xpt_traverse_config {
140 xpt_traverse_depth depth;
145 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
146 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
147 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
148 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
149 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
151 /* Transport layer configuration information */
152 static struct xpt_softc xsoftc;
154 TUNABLE_INT("kern.cam.boot_delay", &xsoftc.boot_delay);
155 SYSCTL_INT(_kern_cam, OID_AUTO, boot_delay, CTLFLAG_RDTUN,
156 &xsoftc.boot_delay, 0, "Bus registration wait time");
158 /* Queues for our software interrupt handler */
159 typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
160 typedef TAILQ_HEAD(cam_simq, cam_sim) cam_simq_t;
161 static cam_simq_t cam_simq;
162 static struct mtx cam_simq_lock;
164 /* Pointers to software interrupt handlers */
165 static void *cambio_ih;
167 struct cam_periph *xpt_periph;
169 static periph_init_t xpt_periph_init;
171 static struct periph_driver xpt_driver =
173 xpt_periph_init, "xpt",
174 TAILQ_HEAD_INITIALIZER(xpt_driver.units), /* generation */ 0,
178 PERIPHDRIVER_DECLARE(xpt, xpt_driver);
180 static d_open_t xptopen;
181 static d_close_t xptclose;
182 static d_ioctl_t xptioctl;
184 static struct cdevsw xpt_cdevsw = {
185 .d_version = D_VERSION,
193 /* Storage for debugging datastructures */
194 struct cam_path *cam_dpath;
195 u_int32_t cam_dflags = CAM_DEBUG_FLAGS;
196 TUNABLE_INT("kern.cam.dflags", &cam_dflags);
197 SYSCTL_UINT(_kern_cam, OID_AUTO, dflags, CTLFLAG_RW,
198 &cam_dflags, 0, "Enabled debug flags");
199 u_int32_t cam_debug_delay = CAM_DEBUG_DELAY;
200 TUNABLE_INT("kern.cam.debug_delay", &cam_debug_delay);
201 SYSCTL_UINT(_kern_cam, OID_AUTO, debug_delay, CTLFLAG_RW,
202 &cam_debug_delay, 0, "Delay in us after each debug message");
204 /* Our boot-time initialization hook */
205 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
207 static moduledata_t cam_moduledata = {
209 cam_module_event_handler,
213 static int xpt_init(void *);
215 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
216 MODULE_VERSION(cam, 1);
219 static void xpt_async_bcast(struct async_list *async_head,
220 u_int32_t async_code,
221 struct cam_path *path,
223 static path_id_t xptnextfreepathid(void);
224 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
225 static union ccb *xpt_get_ccb(struct cam_ed *device);
226 static void xpt_run_dev_allocq(struct cam_eb *bus);
227 static void xpt_run_dev_sendq(struct cam_eb *bus);
228 static timeout_t xpt_release_devq_timeout;
229 static void xpt_release_simq_timeout(void *arg) __unused;
230 static void xpt_release_bus(struct cam_eb *bus);
231 static void xpt_release_devq_device(struct cam_ed *dev, cam_rl rl,
232 u_int count, int run_queue);
233 static struct cam_et*
234 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
235 static void xpt_release_target(struct cam_et *target);
236 static struct cam_eb*
237 xpt_find_bus(path_id_t path_id);
238 static struct cam_et*
239 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
240 static struct cam_ed*
241 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
242 static void xpt_config(void *arg);
243 static xpt_devicefunc_t xptpassannouncefunc;
244 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
245 static void xptpoll(struct cam_sim *sim);
246 static void camisr(void *);
247 static void camisr_runqueue(void *);
248 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
249 u_int num_patterns, struct cam_eb *bus);
250 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
252 struct cam_ed *device);
253 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
255 struct cam_periph *periph);
256 static xpt_busfunc_t xptedtbusfunc;
257 static xpt_targetfunc_t xptedttargetfunc;
258 static xpt_devicefunc_t xptedtdevicefunc;
259 static xpt_periphfunc_t xptedtperiphfunc;
260 static xpt_pdrvfunc_t xptplistpdrvfunc;
261 static xpt_periphfunc_t xptplistperiphfunc;
262 static int xptedtmatch(struct ccb_dev_match *cdm);
263 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
264 static int xptbustraverse(struct cam_eb *start_bus,
265 xpt_busfunc_t *tr_func, void *arg);
266 static int xpttargettraverse(struct cam_eb *bus,
267 struct cam_et *start_target,
268 xpt_targetfunc_t *tr_func, void *arg);
269 static int xptdevicetraverse(struct cam_et *target,
270 struct cam_ed *start_device,
271 xpt_devicefunc_t *tr_func, void *arg);
272 static int xptperiphtraverse(struct cam_ed *device,
273 struct cam_periph *start_periph,
274 xpt_periphfunc_t *tr_func, void *arg);
275 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
276 xpt_pdrvfunc_t *tr_func, void *arg);
277 static int xptpdperiphtraverse(struct periph_driver **pdrv,
278 struct cam_periph *start_periph,
279 xpt_periphfunc_t *tr_func,
281 static xpt_busfunc_t xptdefbusfunc;
282 static xpt_targetfunc_t xptdeftargetfunc;
283 static xpt_devicefunc_t xptdefdevicefunc;
284 static xpt_periphfunc_t xptdefperiphfunc;
285 static void xpt_finishconfig_task(void *context, int pending);
286 static void xpt_dev_async_default(u_int32_t async_code,
288 struct cam_et *target,
289 struct cam_ed *device,
291 static struct cam_ed * xpt_alloc_device_default(struct cam_eb *bus,
292 struct cam_et *target,
294 static xpt_devicefunc_t xptsetasyncfunc;
295 static xpt_busfunc_t xptsetasyncbusfunc;
296 static cam_status xptregister(struct cam_periph *periph,
298 static __inline int periph_is_queued(struct cam_periph *periph);
299 static __inline int device_is_alloc_queued(struct cam_ed *device);
300 static __inline int device_is_send_queued(struct cam_ed *device);
303 xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev)
307 if ((dev->drvq.entries > 0) &&
308 (dev->ccbq.devq_openings > 0) &&
309 (cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL(
310 CAMQ_GET_PRIO(&dev->drvq))) == 0)) {
312 * The priority of a device waiting for CCB resources
313 * is that of the highest priority peripheral driver
316 retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue,
317 &dev->alloc_ccb_entry.pinfo,
318 CAMQ_GET_PRIO(&dev->drvq));
327 xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev)
331 if ((dev->ccbq.queue.entries > 0) &&
332 (dev->ccbq.dev_openings > 0) &&
333 (cam_ccbq_frozen_top(&dev->ccbq) == 0)) {
335 * The priority of a device waiting for controller
336 * resources is that of the highest priority CCB
340 xpt_schedule_dev(&bus->sim->devq->send_queue,
341 &dev->send_ccb_entry.pinfo,
342 CAMQ_GET_PRIO(&dev->ccbq.queue));
350 periph_is_queued(struct cam_periph *periph)
352 return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
356 device_is_alloc_queued(struct cam_ed *device)
358 return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
362 device_is_send_queued(struct cam_ed *device)
364 return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
370 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
374 xptdone(struct cam_periph *periph, union ccb *done_ccb)
376 /* Caller will release the CCB */
377 wakeup(&done_ccb->ccb_h.cbfcnp);
381 xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
385 * Only allow read-write access.
387 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
391 * We don't allow nonblocking access.
393 if ((flags & O_NONBLOCK) != 0) {
394 printf("%s: can't do nonblocking access\n", devtoname(dev));
398 /* Mark ourselves open */
399 mtx_lock(&xsoftc.xpt_lock);
400 xsoftc.flags |= XPT_FLAG_OPEN;
401 mtx_unlock(&xsoftc.xpt_lock);
407 xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
410 /* Mark ourselves closed */
411 mtx_lock(&xsoftc.xpt_lock);
412 xsoftc.flags &= ~XPT_FLAG_OPEN;
413 mtx_unlock(&xsoftc.xpt_lock);
419 * Don't automatically grab the xpt softc lock here even though this is going
420 * through the xpt device. The xpt device is really just a back door for
421 * accessing other devices and SIMs, so the right thing to do is to grab
422 * the appropriate SIM lock once the bus/SIM is located.
425 xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
433 * For the transport layer CAMIOCOMMAND ioctl, we really only want
434 * to accept CCB types that don't quite make sense to send through a
435 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
443 inccb = (union ccb *)addr;
445 bus = xpt_find_bus(inccb->ccb_h.path_id);
449 switch (inccb->ccb_h.func_code) {
452 if (inccb->ccb_h.target_id != CAM_TARGET_WILDCARD ||
453 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
454 xpt_release_bus(bus);
459 if (inccb->ccb_h.target_id == CAM_TARGET_WILDCARD ||
460 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
461 xpt_release_bus(bus);
469 switch(inccb->ccb_h.func_code) {
477 ccb = xpt_alloc_ccb();
479 CAM_SIM_LOCK(bus->sim);
482 * Create a path using the bus, target, and lun the
485 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
486 inccb->ccb_h.path_id,
487 inccb->ccb_h.target_id,
488 inccb->ccb_h.target_lun) !=
491 CAM_SIM_UNLOCK(bus->sim);
495 /* Ensure all of our fields are correct */
496 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
497 inccb->ccb_h.pinfo.priority);
498 xpt_merge_ccb(ccb, inccb);
499 ccb->ccb_h.cbfcnp = xptdone;
500 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
501 bcopy(ccb, inccb, sizeof(union ccb));
502 xpt_free_path(ccb->ccb_h.path);
504 CAM_SIM_UNLOCK(bus->sim);
511 * This is an immediate CCB, so it's okay to
512 * allocate it on the stack.
515 CAM_SIM_LOCK(bus->sim);
518 * Create a path using the bus, target, and lun the
521 if (xpt_create_path(&ccb.ccb_h.path, xpt_periph,
522 inccb->ccb_h.path_id,
523 inccb->ccb_h.target_id,
524 inccb->ccb_h.target_lun) !=
527 CAM_SIM_UNLOCK(bus->sim);
530 /* Ensure all of our fields are correct */
531 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
532 inccb->ccb_h.pinfo.priority);
533 xpt_merge_ccb(&ccb, inccb);
534 ccb.ccb_h.cbfcnp = xptdone;
536 bcopy(&ccb, inccb, sizeof(union ccb));
537 xpt_free_path(ccb.ccb_h.path);
538 CAM_SIM_UNLOCK(bus->sim);
542 case XPT_DEV_MATCH: {
543 struct cam_periph_map_info mapinfo;
544 struct cam_path *old_path;
547 * We can't deal with physical addresses for this
548 * type of transaction.
550 if (inccb->ccb_h.flags & CAM_DATA_PHYS) {
556 * Save this in case the caller had it set to
557 * something in particular.
559 old_path = inccb->ccb_h.path;
562 * We really don't need a path for the matching
563 * code. The path is needed because of the
564 * debugging statements in xpt_action(). They
565 * assume that the CCB has a valid path.
567 inccb->ccb_h.path = xpt_periph->path;
569 bzero(&mapinfo, sizeof(mapinfo));
572 * Map the pattern and match buffers into kernel
573 * virtual address space.
575 error = cam_periph_mapmem(inccb, &mapinfo);
578 inccb->ccb_h.path = old_path;
583 * This is an immediate CCB, we can send it on directly.
585 CAM_SIM_LOCK(xpt_path_sim(xpt_periph->path));
587 CAM_SIM_UNLOCK(xpt_path_sim(xpt_periph->path));
590 * Map the buffers back into user space.
592 cam_periph_unmapmem(inccb, &mapinfo);
594 inccb->ccb_h.path = old_path;
603 xpt_release_bus(bus);
607 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
608 * with the periphal driver name and unit name filled in. The other
609 * fields don't really matter as input. The passthrough driver name
610 * ("pass"), and unit number are passed back in the ccb. The current
611 * device generation number, and the index into the device peripheral
612 * driver list, and the status are also passed back. Note that
613 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
614 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
615 * (or rather should be) impossible for the device peripheral driver
616 * list to change since we look at the whole thing in one pass, and
617 * we do it with lock protection.
620 case CAMGETPASSTHRU: {
622 struct cam_periph *periph;
623 struct periph_driver **p_drv;
626 u_int cur_generation;
627 int base_periph_found;
630 ccb = (union ccb *)addr;
631 unit = ccb->cgdl.unit_number;
632 name = ccb->cgdl.periph_name;
634 * Every 100 devices, we want to drop our lock protection to
635 * give the software interrupt handler a chance to run.
636 * Most systems won't run into this check, but this should
637 * avoid starvation in the software interrupt handler in
642 ccb = (union ccb *)addr;
644 base_periph_found = 0;
647 * Sanity check -- make sure we don't get a null peripheral
650 if (*ccb->cgdl.periph_name == '\0') {
655 /* Keep the list from changing while we traverse it */
658 cur_generation = xsoftc.xpt_generation;
660 /* first find our driver in the list of drivers */
661 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
662 if (strcmp((*p_drv)->driver_name, name) == 0)
665 if (*p_drv == NULL) {
667 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
668 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
669 *ccb->cgdl.periph_name = '\0';
670 ccb->cgdl.unit_number = 0;
676 * Run through every peripheral instance of this driver
677 * and check to see whether it matches the unit passed
678 * in by the user. If it does, get out of the loops and
679 * find the passthrough driver associated with that
682 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
683 periph = TAILQ_NEXT(periph, unit_links)) {
685 if (periph->unit_number == unit) {
687 } else if (--splbreaknum == 0) {
691 if (cur_generation != xsoftc.xpt_generation)
696 * If we found the peripheral driver that the user passed
697 * in, go through all of the peripheral drivers for that
698 * particular device and look for a passthrough driver.
700 if (periph != NULL) {
701 struct cam_ed *device;
704 base_periph_found = 1;
705 device = periph->path->device;
706 for (i = 0, periph = SLIST_FIRST(&device->periphs);
708 periph = SLIST_NEXT(periph, periph_links), i++) {
710 * Check to see whether we have a
711 * passthrough device or not.
713 if (strcmp(periph->periph_name, "pass") == 0) {
715 * Fill in the getdevlist fields.
717 strcpy(ccb->cgdl.periph_name,
718 periph->periph_name);
719 ccb->cgdl.unit_number =
721 if (SLIST_NEXT(periph, periph_links))
723 CAM_GDEVLIST_MORE_DEVS;
726 CAM_GDEVLIST_LAST_DEVICE;
727 ccb->cgdl.generation =
731 * Fill in some CCB header fields
732 * that the user may want.
735 periph->path->bus->path_id;
736 ccb->ccb_h.target_id =
737 periph->path->target->target_id;
738 ccb->ccb_h.target_lun =
739 periph->path->device->lun_id;
740 ccb->ccb_h.status = CAM_REQ_CMP;
747 * If the periph is null here, one of two things has
748 * happened. The first possibility is that we couldn't
749 * find the unit number of the particular peripheral driver
750 * that the user is asking about. e.g. the user asks for
751 * the passthrough driver for "da11". We find the list of
752 * "da" peripherals all right, but there is no unit 11.
753 * The other possibility is that we went through the list
754 * of peripheral drivers attached to the device structure,
755 * but didn't find one with the name "pass". Either way,
756 * we return ENOENT, since we couldn't find something.
758 if (periph == NULL) {
759 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
760 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
761 *ccb->cgdl.periph_name = '\0';
762 ccb->cgdl.unit_number = 0;
765 * It is unfortunate that this is even necessary,
766 * but there are many, many clueless users out there.
767 * If this is true, the user is looking for the
768 * passthrough driver, but doesn't have one in his
771 if (base_periph_found == 1) {
772 printf("xptioctl: pass driver is not in the "
774 printf("xptioctl: put \"device pass\" in "
775 "your kernel config file\n");
790 cam_module_event_handler(module_t mod, int what, void *arg)
796 if ((error = xpt_init(NULL)) != 0)
809 xpt_rescan_done(struct cam_periph *periph, union ccb *done_ccb)
812 if (done_ccb->ccb_h.ppriv_ptr1 == NULL) {
813 xpt_free_path(done_ccb->ccb_h.path);
814 xpt_free_ccb(done_ccb);
816 done_ccb->ccb_h.cbfcnp = done_ccb->ccb_h.ppriv_ptr1;
817 (*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
822 /* thread to handle bus rescans */
824 xpt_scanner_thread(void *dummy)
831 if (TAILQ_EMPTY(&xsoftc.ccb_scanq))
832 msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO,
834 if ((ccb = (union ccb *)TAILQ_FIRST(&xsoftc.ccb_scanq)) != NULL) {
835 TAILQ_REMOVE(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
838 sim = ccb->ccb_h.path->bus->sim;
849 xpt_rescan(union ccb *ccb)
853 /* Prepare request */
854 if (ccb->ccb_h.path->target->target_id == CAM_TARGET_WILDCARD &&
855 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
856 ccb->ccb_h.func_code = XPT_SCAN_BUS;
857 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
858 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
859 ccb->ccb_h.func_code = XPT_SCAN_TGT;
860 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
861 ccb->ccb_h.path->device->lun_id != CAM_LUN_WILDCARD)
862 ccb->ccb_h.func_code = XPT_SCAN_LUN;
864 xpt_print(ccb->ccb_h.path, "illegal scan path\n");
865 xpt_free_path(ccb->ccb_h.path);
869 ccb->ccb_h.ppriv_ptr1 = ccb->ccb_h.cbfcnp;
870 ccb->ccb_h.cbfcnp = xpt_rescan_done;
871 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, CAM_PRIORITY_XPT);
872 /* Don't make duplicate entries for the same paths. */
874 if (ccb->ccb_h.ppriv_ptr1 == NULL) {
875 TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) {
876 if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) {
877 wakeup(&xsoftc.ccb_scanq);
879 xpt_print(ccb->ccb_h.path, "rescan already queued\n");
880 xpt_free_path(ccb->ccb_h.path);
886 TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
887 xsoftc.buses_to_config++;
888 wakeup(&xsoftc.ccb_scanq);
892 /* Functions accessed by the peripheral drivers */
894 xpt_init(void *dummy)
896 struct cam_sim *xpt_sim;
897 struct cam_path *path;
898 struct cam_devq *devq;
901 TAILQ_INIT(&xsoftc.xpt_busses);
902 TAILQ_INIT(&cam_simq);
903 TAILQ_INIT(&xsoftc.ccb_scanq);
904 STAILQ_INIT(&xsoftc.highpowerq);
905 xsoftc.num_highpower = CAM_MAX_HIGHPOWER;
907 mtx_init(&cam_simq_lock, "CAM SIMQ lock", NULL, MTX_DEF);
908 mtx_init(&xsoftc.xpt_lock, "XPT lock", NULL, MTX_DEF);
909 mtx_init(&xsoftc.xpt_topo_lock, "XPT topology lock", NULL, MTX_DEF);
911 #ifdef CAM_BOOT_DELAY
913 * Override this value at compile time to assist our users
914 * who don't use loader to boot a kernel.
916 xsoftc.boot_delay = CAM_BOOT_DELAY;
919 * The xpt layer is, itself, the equivelent of a SIM.
920 * Allow 16 ccbs in the ccb pool for it. This should
921 * give decent parallelism when we probe busses and
922 * perform other XPT functions.
924 devq = cam_simq_alloc(16);
925 xpt_sim = cam_sim_alloc(xptaction,
930 /*mtx*/&xsoftc.xpt_lock,
931 /*max_dev_transactions*/0,
932 /*max_tagged_dev_transactions*/0,
937 mtx_lock(&xsoftc.xpt_lock);
938 if ((status = xpt_bus_register(xpt_sim, NULL, 0)) != CAM_SUCCESS) {
939 mtx_unlock(&xsoftc.xpt_lock);
940 printf("xpt_init: xpt_bus_register failed with status %#x,"
941 " failing attach\n", status);
946 * Looking at the XPT from the SIM layer, the XPT is
947 * the equivelent of a peripheral driver. Allocate
948 * a peripheral driver entry for us.
950 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
952 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
953 mtx_unlock(&xsoftc.xpt_lock);
954 printf("xpt_init: xpt_create_path failed with status %#x,"
955 " failing attach\n", status);
959 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
960 path, NULL, 0, xpt_sim);
962 mtx_unlock(&xsoftc.xpt_lock);
963 /* Install our software interrupt handlers */
964 swi_add(NULL, "cambio", camisr, NULL, SWI_CAMBIO, INTR_MPSAFE, &cambio_ih);
966 * Register a callback for when interrupts are enabled.
968 xsoftc.xpt_config_hook =
969 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
970 M_CAMXPT, M_NOWAIT | M_ZERO);
971 if (xsoftc.xpt_config_hook == NULL) {
972 printf("xpt_init: Cannot malloc config hook "
973 "- failing attach\n");
976 xsoftc.xpt_config_hook->ich_func = xpt_config;
977 if (config_intrhook_establish(xsoftc.xpt_config_hook) != 0) {
978 free (xsoftc.xpt_config_hook, M_CAMXPT);
979 printf("xpt_init: config_intrhook_establish failed "
980 "- failing attach\n");
987 xptregister(struct cam_periph *periph, void *arg)
989 struct cam_sim *xpt_sim;
991 if (periph == NULL) {
992 printf("xptregister: periph was NULL!!\n");
993 return(CAM_REQ_CMP_ERR);
996 xpt_sim = (struct cam_sim *)arg;
997 xpt_sim->softc = periph;
999 periph->softc = NULL;
1001 return(CAM_REQ_CMP);
1005 xpt_add_periph(struct cam_periph *periph)
1007 struct cam_ed *device;
1009 struct periph_list *periph_head;
1011 mtx_assert(periph->sim->mtx, MA_OWNED);
1013 device = periph->path->device;
1015 periph_head = &device->periphs;
1017 status = CAM_REQ_CMP;
1019 if (device != NULL) {
1021 * Make room for this peripheral
1022 * so it will fit in the queue
1023 * when it's scheduled to run
1025 status = camq_resize(&device->drvq,
1026 device->drvq.array_size + 1);
1028 device->generation++;
1030 SLIST_INSERT_HEAD(periph_head, periph, periph_links);
1034 xsoftc.xpt_generation++;
1041 xpt_remove_periph(struct cam_periph *periph, int topology_lock_held)
1043 struct cam_ed *device;
1045 mtx_assert(periph->sim->mtx, MA_OWNED);
1047 device = periph->path->device;
1049 if (device != NULL) {
1050 struct periph_list *periph_head;
1052 periph_head = &device->periphs;
1054 /* Release the slot for this peripheral */
1055 camq_resize(&device->drvq, device->drvq.array_size - 1);
1057 device->generation++;
1059 SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
1062 if (topology_lock_held == 0)
1065 xsoftc.xpt_generation++;
1067 if (topology_lock_held == 0)
1073 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1075 struct cam_path *path = periph->path;
1077 mtx_assert(periph->sim->mtx, MA_OWNED);
1079 printf("%s%d at %s%d bus %d scbus%d target %d lun %d\n",
1080 periph->periph_name, periph->unit_number,
1081 path->bus->sim->sim_name,
1082 path->bus->sim->unit_number,
1083 path->bus->sim->bus_id,
1085 path->target->target_id,
1086 path->device->lun_id);
1087 printf("%s%d: ", periph->periph_name, periph->unit_number);
1088 if (path->device->protocol == PROTO_SCSI)
1089 scsi_print_inquiry(&path->device->inq_data);
1090 else if (path->device->protocol == PROTO_ATA ||
1091 path->device->protocol == PROTO_SATAPM)
1092 ata_print_ident(&path->device->ident_data);
1093 else if (path->device->protocol == PROTO_SEMB)
1095 (struct sep_identify_data *)&path->device->ident_data);
1097 printf("Unknown protocol device\n");
1098 if (bootverbose && path->device->serial_num_len > 0) {
1099 /* Don't wrap the screen - print only the first 60 chars */
1100 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1101 periph->unit_number, path->device->serial_num);
1103 /* Announce transport details. */
1104 (*(path->bus->xport->announce))(periph);
1105 /* Announce command queueing. */
1106 if (path->device->inq_flags & SID_CmdQue
1107 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1108 printf("%s%d: Command Queueing enabled\n",
1109 periph->periph_name, periph->unit_number);
1111 /* Announce caller's details if they've passed in. */
1112 if (announce_string != NULL)
1113 printf("%s%d: %s\n", periph->periph_name,
1114 periph->unit_number, announce_string);
1118 xpt_getattr(char *buf, size_t len, const char *attr, struct cam_path *path)
1121 struct ccb_dev_advinfo cdai;
1123 mtx_assert(path->bus->sim->mtx, MA_OWNED);
1125 memset(&cdai, 0, sizeof(cdai));
1126 xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
1127 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1130 if (!strcmp(attr, "GEOM::ident"))
1131 cdai.buftype = CDAI_TYPE_SERIAL_NUM;
1132 else if (!strcmp(attr, "GEOM::physpath"))
1133 cdai.buftype = CDAI_TYPE_PHYS_PATH;
1137 cdai.buf = malloc(cdai.bufsiz, M_CAMXPT, M_NOWAIT|M_ZERO);
1138 if (cdai.buf == NULL) {
1142 xpt_action((union ccb *)&cdai); /* can only be synchronous */
1143 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1144 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1145 if (cdai.provsiz == 0)
1148 if (strlcpy(buf, cdai.buf, len) >= len)
1152 if (cdai.buf != NULL)
1153 free(cdai.buf, M_CAMXPT);
1157 static dev_match_ret
1158 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1161 dev_match_ret retval;
1164 retval = DM_RET_NONE;
1167 * If we aren't given something to match against, that's an error.
1170 return(DM_RET_ERROR);
1173 * If there are no match entries, then this bus matches no
1176 if ((patterns == NULL) || (num_patterns == 0))
1177 return(DM_RET_DESCEND | DM_RET_COPY);
1179 for (i = 0; i < num_patterns; i++) {
1180 struct bus_match_pattern *cur_pattern;
1183 * If the pattern in question isn't for a bus node, we
1184 * aren't interested. However, we do indicate to the
1185 * calling routine that we should continue descending the
1186 * tree, since the user wants to match against lower-level
1189 if (patterns[i].type != DEV_MATCH_BUS) {
1190 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1191 retval |= DM_RET_DESCEND;
1195 cur_pattern = &patterns[i].pattern.bus_pattern;
1198 * If they want to match any bus node, we give them any
1201 if (cur_pattern->flags == BUS_MATCH_ANY) {
1202 /* set the copy flag */
1203 retval |= DM_RET_COPY;
1206 * If we've already decided on an action, go ahead
1209 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1214 * Not sure why someone would do this...
1216 if (cur_pattern->flags == BUS_MATCH_NONE)
1219 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1220 && (cur_pattern->path_id != bus->path_id))
1223 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1224 && (cur_pattern->bus_id != bus->sim->bus_id))
1227 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1228 && (cur_pattern->unit_number != bus->sim->unit_number))
1231 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1232 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1237 * If we get to this point, the user definitely wants
1238 * information on this bus. So tell the caller to copy the
1241 retval |= DM_RET_COPY;
1244 * If the return action has been set to descend, then we
1245 * know that we've already seen a non-bus matching
1246 * expression, therefore we need to further descend the tree.
1247 * This won't change by continuing around the loop, so we
1248 * go ahead and return. If we haven't seen a non-bus
1249 * matching expression, we keep going around the loop until
1250 * we exhaust the matching expressions. We'll set the stop
1251 * flag once we fall out of the loop.
1253 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1258 * If the return action hasn't been set to descend yet, that means
1259 * we haven't seen anything other than bus matching patterns. So
1260 * tell the caller to stop descending the tree -- the user doesn't
1261 * want to match against lower level tree elements.
1263 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1264 retval |= DM_RET_STOP;
1269 static dev_match_ret
1270 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1271 struct cam_ed *device)
1273 dev_match_ret retval;
1276 retval = DM_RET_NONE;
1279 * If we aren't given something to match against, that's an error.
1282 return(DM_RET_ERROR);
1285 * If there are no match entries, then this device matches no
1288 if ((patterns == NULL) || (num_patterns == 0))
1289 return(DM_RET_DESCEND | DM_RET_COPY);
1291 for (i = 0; i < num_patterns; i++) {
1292 struct device_match_pattern *cur_pattern;
1293 struct scsi_vpd_device_id *device_id_page;
1296 * If the pattern in question isn't for a device node, we
1297 * aren't interested.
1299 if (patterns[i].type != DEV_MATCH_DEVICE) {
1300 if ((patterns[i].type == DEV_MATCH_PERIPH)
1301 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1302 retval |= DM_RET_DESCEND;
1306 cur_pattern = &patterns[i].pattern.device_pattern;
1308 /* Error out if mutually exclusive options are specified. */
1309 if ((cur_pattern->flags & (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1310 == (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1311 return(DM_RET_ERROR);
1314 * If they want to match any device node, we give them any
1317 if (cur_pattern->flags == DEV_MATCH_ANY)
1321 * Not sure why someone would do this...
1323 if (cur_pattern->flags == DEV_MATCH_NONE)
1326 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1327 && (cur_pattern->path_id != device->target->bus->path_id))
1330 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1331 && (cur_pattern->target_id != device->target->target_id))
1334 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1335 && (cur_pattern->target_lun != device->lun_id))
1338 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1339 && (cam_quirkmatch((caddr_t)&device->inq_data,
1340 (caddr_t)&cur_pattern->data.inq_pat,
1341 1, sizeof(cur_pattern->data.inq_pat),
1342 scsi_static_inquiry_match) == NULL))
1345 device_id_page = (struct scsi_vpd_device_id *)device->device_id;
1346 if (((cur_pattern->flags & DEV_MATCH_DEVID) != 0)
1347 && (device->device_id_len < SVPD_DEVICE_ID_HDR_LEN
1348 || scsi_devid_match((uint8_t *)device_id_page->desc_list,
1349 device->device_id_len
1350 - SVPD_DEVICE_ID_HDR_LEN,
1351 cur_pattern->data.devid_pat.id,
1352 cur_pattern->data.devid_pat.id_len) != 0))
1357 * If we get to this point, the user definitely wants
1358 * information on this device. So tell the caller to copy
1361 retval |= DM_RET_COPY;
1364 * If the return action has been set to descend, then we
1365 * know that we've already seen a peripheral matching
1366 * expression, therefore we need to further descend the tree.
1367 * This won't change by continuing around the loop, so we
1368 * go ahead and return. If we haven't seen a peripheral
1369 * matching expression, we keep going around the loop until
1370 * we exhaust the matching expressions. We'll set the stop
1371 * flag once we fall out of the loop.
1373 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1378 * If the return action hasn't been set to descend yet, that means
1379 * we haven't seen any peripheral matching patterns. So tell the
1380 * caller to stop descending the tree -- the user doesn't want to
1381 * match against lower level tree elements.
1383 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1384 retval |= DM_RET_STOP;
1390 * Match a single peripheral against any number of match patterns.
1392 static dev_match_ret
1393 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1394 struct cam_periph *periph)
1396 dev_match_ret retval;
1400 * If we aren't given something to match against, that's an error.
1403 return(DM_RET_ERROR);
1406 * If there are no match entries, then this peripheral matches no
1409 if ((patterns == NULL) || (num_patterns == 0))
1410 return(DM_RET_STOP | DM_RET_COPY);
1413 * There aren't any nodes below a peripheral node, so there's no
1414 * reason to descend the tree any further.
1416 retval = DM_RET_STOP;
1418 for (i = 0; i < num_patterns; i++) {
1419 struct periph_match_pattern *cur_pattern;
1422 * If the pattern in question isn't for a peripheral, we
1423 * aren't interested.
1425 if (patterns[i].type != DEV_MATCH_PERIPH)
1428 cur_pattern = &patterns[i].pattern.periph_pattern;
1431 * If they want to match on anything, then we will do so.
1433 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1434 /* set the copy flag */
1435 retval |= DM_RET_COPY;
1438 * We've already set the return action to stop,
1439 * since there are no nodes below peripherals in
1446 * Not sure why someone would do this...
1448 if (cur_pattern->flags == PERIPH_MATCH_NONE)
1451 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1452 && (cur_pattern->path_id != periph->path->bus->path_id))
1456 * For the target and lun id's, we have to make sure the
1457 * target and lun pointers aren't NULL. The xpt peripheral
1458 * has a wildcard target and device.
1460 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1461 && ((periph->path->target == NULL)
1462 ||(cur_pattern->target_id != periph->path->target->target_id)))
1465 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1466 && ((periph->path->device == NULL)
1467 || (cur_pattern->target_lun != periph->path->device->lun_id)))
1470 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
1471 && (cur_pattern->unit_number != periph->unit_number))
1474 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
1475 && (strncmp(cur_pattern->periph_name, periph->periph_name,
1480 * If we get to this point, the user definitely wants
1481 * information on this peripheral. So tell the caller to
1482 * copy the data out.
1484 retval |= DM_RET_COPY;
1487 * The return action has already been set to stop, since
1488 * peripherals don't have any nodes below them in the EDT.
1494 * If we get to this point, the peripheral that was passed in
1495 * doesn't match any of the patterns.
1501 xptedtbusfunc(struct cam_eb *bus, void *arg)
1503 struct ccb_dev_match *cdm;
1504 dev_match_ret retval;
1506 cdm = (struct ccb_dev_match *)arg;
1509 * If our position is for something deeper in the tree, that means
1510 * that we've already seen this node. So, we keep going down.
1512 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1513 && (cdm->pos.cookie.bus == bus)
1514 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1515 && (cdm->pos.cookie.target != NULL))
1516 retval = DM_RET_DESCEND;
1518 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
1521 * If we got an error, bail out of the search.
1523 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1524 cdm->status = CAM_DEV_MATCH_ERROR;
1529 * If the copy flag is set, copy this bus out.
1531 if (retval & DM_RET_COPY) {
1534 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1535 sizeof(struct dev_match_result));
1538 * If we don't have enough space to put in another
1539 * match result, save our position and tell the
1540 * user there are more devices to check.
1542 if (spaceleft < sizeof(struct dev_match_result)) {
1543 bzero(&cdm->pos, sizeof(cdm->pos));
1544 cdm->pos.position_type =
1545 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
1547 cdm->pos.cookie.bus = bus;
1548 cdm->pos.generations[CAM_BUS_GENERATION]=
1549 xsoftc.bus_generation;
1550 cdm->status = CAM_DEV_MATCH_MORE;
1553 j = cdm->num_matches;
1555 cdm->matches[j].type = DEV_MATCH_BUS;
1556 cdm->matches[j].result.bus_result.path_id = bus->path_id;
1557 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
1558 cdm->matches[j].result.bus_result.unit_number =
1559 bus->sim->unit_number;
1560 strncpy(cdm->matches[j].result.bus_result.dev_name,
1561 bus->sim->sim_name, DEV_IDLEN);
1565 * If the user is only interested in busses, there's no
1566 * reason to descend to the next level in the tree.
1568 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1572 * If there is a target generation recorded, check it to
1573 * make sure the target list hasn't changed.
1575 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1576 && (bus == cdm->pos.cookie.bus)
1577 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1578 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
1579 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
1581 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1585 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1586 && (cdm->pos.cookie.bus == bus)
1587 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1588 && (cdm->pos.cookie.target != NULL))
1589 return(xpttargettraverse(bus,
1590 (struct cam_et *)cdm->pos.cookie.target,
1591 xptedttargetfunc, arg));
1593 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
1597 xptedttargetfunc(struct cam_et *target, void *arg)
1599 struct ccb_dev_match *cdm;
1601 cdm = (struct ccb_dev_match *)arg;
1604 * If there is a device list generation recorded, check it to
1605 * make sure the device list hasn't changed.
1607 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1608 && (cdm->pos.cookie.bus == target->bus)
1609 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1610 && (cdm->pos.cookie.target == target)
1611 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1612 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
1613 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
1614 target->generation)) {
1615 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1619 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1620 && (cdm->pos.cookie.bus == target->bus)
1621 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1622 && (cdm->pos.cookie.target == target)
1623 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1624 && (cdm->pos.cookie.device != NULL))
1625 return(xptdevicetraverse(target,
1626 (struct cam_ed *)cdm->pos.cookie.device,
1627 xptedtdevicefunc, arg));
1629 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
1633 xptedtdevicefunc(struct cam_ed *device, void *arg)
1636 struct ccb_dev_match *cdm;
1637 dev_match_ret retval;
1639 cdm = (struct ccb_dev_match *)arg;
1642 * If our position is for something deeper in the tree, that means
1643 * that we've already seen this node. So, we keep going down.
1645 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1646 && (cdm->pos.cookie.device == device)
1647 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1648 && (cdm->pos.cookie.periph != NULL))
1649 retval = DM_RET_DESCEND;
1651 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
1654 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1655 cdm->status = CAM_DEV_MATCH_ERROR;
1660 * If the copy flag is set, copy this device out.
1662 if (retval & DM_RET_COPY) {
1665 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1666 sizeof(struct dev_match_result));
1669 * If we don't have enough space to put in another
1670 * match result, save our position and tell the
1671 * user there are more devices to check.
1673 if (spaceleft < sizeof(struct dev_match_result)) {
1674 bzero(&cdm->pos, sizeof(cdm->pos));
1675 cdm->pos.position_type =
1676 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1677 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
1679 cdm->pos.cookie.bus = device->target->bus;
1680 cdm->pos.generations[CAM_BUS_GENERATION]=
1681 xsoftc.bus_generation;
1682 cdm->pos.cookie.target = device->target;
1683 cdm->pos.generations[CAM_TARGET_GENERATION] =
1684 device->target->bus->generation;
1685 cdm->pos.cookie.device = device;
1686 cdm->pos.generations[CAM_DEV_GENERATION] =
1687 device->target->generation;
1688 cdm->status = CAM_DEV_MATCH_MORE;
1691 j = cdm->num_matches;
1693 cdm->matches[j].type = DEV_MATCH_DEVICE;
1694 cdm->matches[j].result.device_result.path_id =
1695 device->target->bus->path_id;
1696 cdm->matches[j].result.device_result.target_id =
1697 device->target->target_id;
1698 cdm->matches[j].result.device_result.target_lun =
1700 cdm->matches[j].result.device_result.protocol =
1702 bcopy(&device->inq_data,
1703 &cdm->matches[j].result.device_result.inq_data,
1704 sizeof(struct scsi_inquiry_data));
1705 bcopy(&device->ident_data,
1706 &cdm->matches[j].result.device_result.ident_data,
1707 sizeof(struct ata_params));
1709 /* Let the user know whether this device is unconfigured */
1710 if (device->flags & CAM_DEV_UNCONFIGURED)
1711 cdm->matches[j].result.device_result.flags =
1712 DEV_RESULT_UNCONFIGURED;
1714 cdm->matches[j].result.device_result.flags =
1719 * If the user isn't interested in peripherals, don't descend
1720 * the tree any further.
1722 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1726 * If there is a peripheral list generation recorded, make sure
1727 * it hasn't changed.
1729 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1730 && (device->target->bus == cdm->pos.cookie.bus)
1731 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1732 && (device->target == cdm->pos.cookie.target)
1733 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1734 && (device == cdm->pos.cookie.device)
1735 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1736 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
1737 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1738 device->generation)){
1739 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1743 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1744 && (cdm->pos.cookie.bus == device->target->bus)
1745 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1746 && (cdm->pos.cookie.target == device->target)
1747 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1748 && (cdm->pos.cookie.device == device)
1749 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1750 && (cdm->pos.cookie.periph != NULL))
1751 return(xptperiphtraverse(device,
1752 (struct cam_periph *)cdm->pos.cookie.periph,
1753 xptedtperiphfunc, arg));
1755 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
1759 xptedtperiphfunc(struct cam_periph *periph, void *arg)
1761 struct ccb_dev_match *cdm;
1762 dev_match_ret retval;
1764 cdm = (struct ccb_dev_match *)arg;
1766 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1768 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1769 cdm->status = CAM_DEV_MATCH_ERROR;
1774 * If the copy flag is set, copy this peripheral out.
1776 if (retval & DM_RET_COPY) {
1779 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1780 sizeof(struct dev_match_result));
1783 * If we don't have enough space to put in another
1784 * match result, save our position and tell the
1785 * user there are more devices to check.
1787 if (spaceleft < sizeof(struct dev_match_result)) {
1788 bzero(&cdm->pos, sizeof(cdm->pos));
1789 cdm->pos.position_type =
1790 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1791 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
1794 cdm->pos.cookie.bus = periph->path->bus;
1795 cdm->pos.generations[CAM_BUS_GENERATION]=
1796 xsoftc.bus_generation;
1797 cdm->pos.cookie.target = periph->path->target;
1798 cdm->pos.generations[CAM_TARGET_GENERATION] =
1799 periph->path->bus->generation;
1800 cdm->pos.cookie.device = periph->path->device;
1801 cdm->pos.generations[CAM_DEV_GENERATION] =
1802 periph->path->target->generation;
1803 cdm->pos.cookie.periph = periph;
1804 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1805 periph->path->device->generation;
1806 cdm->status = CAM_DEV_MATCH_MORE;
1810 j = cdm->num_matches;
1812 cdm->matches[j].type = DEV_MATCH_PERIPH;
1813 cdm->matches[j].result.periph_result.path_id =
1814 periph->path->bus->path_id;
1815 cdm->matches[j].result.periph_result.target_id =
1816 periph->path->target->target_id;
1817 cdm->matches[j].result.periph_result.target_lun =
1818 periph->path->device->lun_id;
1819 cdm->matches[j].result.periph_result.unit_number =
1820 periph->unit_number;
1821 strncpy(cdm->matches[j].result.periph_result.periph_name,
1822 periph->periph_name, DEV_IDLEN);
1829 xptedtmatch(struct ccb_dev_match *cdm)
1833 cdm->num_matches = 0;
1836 * Check the bus list generation. If it has changed, the user
1837 * needs to reset everything and start over.
1839 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1840 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
1841 && (cdm->pos.generations[CAM_BUS_GENERATION] != xsoftc.bus_generation)) {
1842 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1846 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1847 && (cdm->pos.cookie.bus != NULL))
1848 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
1849 xptedtbusfunc, cdm);
1851 ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
1854 * If we get back 0, that means that we had to stop before fully
1855 * traversing the EDT. It also means that one of the subroutines
1856 * has set the status field to the proper value. If we get back 1,
1857 * we've fully traversed the EDT and copied out any matching entries.
1860 cdm->status = CAM_DEV_MATCH_LAST;
1866 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
1868 struct ccb_dev_match *cdm;
1870 cdm = (struct ccb_dev_match *)arg;
1872 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1873 && (cdm->pos.cookie.pdrv == pdrv)
1874 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1875 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
1876 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1877 (*pdrv)->generation)) {
1878 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1882 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1883 && (cdm->pos.cookie.pdrv == pdrv)
1884 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1885 && (cdm->pos.cookie.periph != NULL))
1886 return(xptpdperiphtraverse(pdrv,
1887 (struct cam_periph *)cdm->pos.cookie.periph,
1888 xptplistperiphfunc, arg));
1890 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
1894 xptplistperiphfunc(struct cam_periph *periph, void *arg)
1896 struct ccb_dev_match *cdm;
1897 dev_match_ret retval;
1899 cdm = (struct ccb_dev_match *)arg;
1901 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1903 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1904 cdm->status = CAM_DEV_MATCH_ERROR;
1909 * If the copy flag is set, copy this peripheral out.
1911 if (retval & DM_RET_COPY) {
1914 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1915 sizeof(struct dev_match_result));
1918 * If we don't have enough space to put in another
1919 * match result, save our position and tell the
1920 * user there are more devices to check.
1922 if (spaceleft < sizeof(struct dev_match_result)) {
1923 struct periph_driver **pdrv;
1926 bzero(&cdm->pos, sizeof(cdm->pos));
1927 cdm->pos.position_type =
1928 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
1932 * This may look a bit non-sensical, but it is
1933 * actually quite logical. There are very few
1934 * peripheral drivers, and bloating every peripheral
1935 * structure with a pointer back to its parent
1936 * peripheral driver linker set entry would cost
1937 * more in the long run than doing this quick lookup.
1939 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
1940 if (strcmp((*pdrv)->driver_name,
1941 periph->periph_name) == 0)
1945 if (*pdrv == NULL) {
1946 cdm->status = CAM_DEV_MATCH_ERROR;
1950 cdm->pos.cookie.pdrv = pdrv;
1952 * The periph generation slot does double duty, as
1953 * does the periph pointer slot. They are used for
1954 * both edt and pdrv lookups and positioning.
1956 cdm->pos.cookie.periph = periph;
1957 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1958 (*pdrv)->generation;
1959 cdm->status = CAM_DEV_MATCH_MORE;
1963 j = cdm->num_matches;
1965 cdm->matches[j].type = DEV_MATCH_PERIPH;
1966 cdm->matches[j].result.periph_result.path_id =
1967 periph->path->bus->path_id;
1970 * The transport layer peripheral doesn't have a target or
1973 if (periph->path->target)
1974 cdm->matches[j].result.periph_result.target_id =
1975 periph->path->target->target_id;
1977 cdm->matches[j].result.periph_result.target_id = -1;
1979 if (periph->path->device)
1980 cdm->matches[j].result.periph_result.target_lun =
1981 periph->path->device->lun_id;
1983 cdm->matches[j].result.periph_result.target_lun = -1;
1985 cdm->matches[j].result.periph_result.unit_number =
1986 periph->unit_number;
1987 strncpy(cdm->matches[j].result.periph_result.periph_name,
1988 periph->periph_name, DEV_IDLEN);
1995 xptperiphlistmatch(struct ccb_dev_match *cdm)
1999 cdm->num_matches = 0;
2002 * At this point in the edt traversal function, we check the bus
2003 * list generation to make sure that no busses have been added or
2004 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2005 * For the peripheral driver list traversal function, however, we
2006 * don't have to worry about new peripheral driver types coming or
2007 * going; they're in a linker set, and therefore can't change
2008 * without a recompile.
2011 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2012 && (cdm->pos.cookie.pdrv != NULL))
2013 ret = xptpdrvtraverse(
2014 (struct periph_driver **)cdm->pos.cookie.pdrv,
2015 xptplistpdrvfunc, cdm);
2017 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2020 * If we get back 0, that means that we had to stop before fully
2021 * traversing the peripheral driver tree. It also means that one of
2022 * the subroutines has set the status field to the proper value. If
2023 * we get back 1, we've fully traversed the EDT and copied out any
2027 cdm->status = CAM_DEV_MATCH_LAST;
2033 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2035 struct cam_eb *bus, *next_bus;
2041 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xsoftc.xpt_busses));
2048 * XXX The locking here is obviously very complex. We
2049 * should work to simplify it.
2052 CAM_SIM_LOCK(bus->sim);
2053 retval = tr_func(bus, arg);
2054 CAM_SIM_UNLOCK(bus->sim);
2057 next_bus = TAILQ_NEXT(bus, links);
2060 xpt_release_bus(bus);
2072 xpt_sim_opened(struct cam_sim *sim)
2075 struct cam_et *target;
2076 struct cam_ed *device;
2077 struct cam_periph *periph;
2079 KASSERT(sim->refcount >= 1, ("sim->refcount >= 1"));
2080 mtx_assert(sim->mtx, MA_OWNED);
2083 TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) {
2084 if (bus->sim != sim)
2087 TAILQ_FOREACH(target, &bus->et_entries, links) {
2088 TAILQ_FOREACH(device, &target->ed_entries, links) {
2089 SLIST_FOREACH(periph, &device->periphs,
2091 if (periph->refcount > 0) {
2105 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2106 xpt_targetfunc_t *tr_func, void *arg)
2108 struct cam_et *target, *next_target;
2111 mtx_assert(bus->sim->mtx, MA_OWNED);
2113 for (target = (start_target ? start_target :
2114 TAILQ_FIRST(&bus->et_entries));
2115 target != NULL; target = next_target) {
2119 retval = tr_func(target, arg);
2121 next_target = TAILQ_NEXT(target, links);
2123 xpt_release_target(target);
2133 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2134 xpt_devicefunc_t *tr_func, void *arg)
2136 struct cam_ed *device, *next_device;
2139 mtx_assert(target->bus->sim->mtx, MA_OWNED);
2141 for (device = (start_device ? start_device :
2142 TAILQ_FIRST(&target->ed_entries));
2144 device = next_device) {
2147 * Hold a reference so the current device does not go away
2152 retval = tr_func(device, arg);
2155 * Grab our next pointer before we release the current
2158 next_device = TAILQ_NEXT(device, links);
2160 xpt_release_device(device);
2170 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2171 xpt_periphfunc_t *tr_func, void *arg)
2173 struct cam_periph *periph, *next_periph;
2178 mtx_assert(device->sim->mtx, MA_OWNED);
2180 for (periph = (start_periph ? start_periph :
2181 SLIST_FIRST(&device->periphs));
2183 periph = next_periph) {
2187 * In this case, we want to show peripherals that have been
2188 * invalidated, but not peripherals that are scheduled to
2189 * be freed. So instead of calling cam_periph_acquire(),
2190 * which will fail if the periph has been invalidated, we
2191 * just check for the free flag here. If it is in the
2192 * process of being freed, we skip to the next periph.
2194 if (periph->flags & CAM_PERIPH_FREE) {
2195 next_periph = SLIST_NEXT(periph, periph_links);
2200 * Acquire a reference to this periph while we call the
2201 * traversal function, so it can't go away.
2205 retval = tr_func(periph, arg);
2208 * Grab the next peripheral before we release this one, so
2209 * our next pointer is still valid.
2211 next_periph = SLIST_NEXT(periph, periph_links);
2213 cam_periph_release_locked_buses(periph);
2227 xptpdrvtraverse(struct periph_driver **start_pdrv,
2228 xpt_pdrvfunc_t *tr_func, void *arg)
2230 struct periph_driver **pdrv;
2236 * We don't traverse the peripheral driver list like we do the
2237 * other lists, because it is a linker set, and therefore cannot be
2238 * changed during runtime. If the peripheral driver list is ever
2239 * re-done to be something other than a linker set (i.e. it can
2240 * change while the system is running), the list traversal should
2241 * be modified to work like the other traversal functions.
2243 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2244 *pdrv != NULL; pdrv++) {
2245 retval = tr_func(pdrv, arg);
2255 xptpdperiphtraverse(struct periph_driver **pdrv,
2256 struct cam_periph *start_periph,
2257 xpt_periphfunc_t *tr_func, void *arg)
2259 struct cam_periph *periph, *next_periph;
2260 struct cam_sim *sim;
2266 for (periph = (start_periph ? start_periph :
2267 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2268 periph = next_periph) {
2272 * In this case, we want to show peripherals that have been
2273 * invalidated, but not peripherals that are scheduled to
2274 * be freed. So instead of calling cam_periph_acquire(),
2275 * which will fail if the periph has been invalidated, we
2276 * just check for the free flag here. If it is free, we
2277 * skip to the next periph.
2279 if (periph->flags & CAM_PERIPH_FREE) {
2280 next_periph = TAILQ_NEXT(periph, unit_links);
2285 * Acquire a reference to this periph while we call the
2286 * traversal function, so it can't go away.
2293 retval = tr_func(periph, arg);
2296 * Grab the next peripheral before we release this one, so
2297 * our next pointer is still valid.
2299 next_periph = TAILQ_NEXT(periph, unit_links);
2301 cam_periph_release_locked_buses(periph);
2302 CAM_SIM_UNLOCK(sim);
2315 xptdefbusfunc(struct cam_eb *bus, void *arg)
2317 struct xpt_traverse_config *tr_config;
2319 tr_config = (struct xpt_traverse_config *)arg;
2321 if (tr_config->depth == XPT_DEPTH_BUS) {
2322 xpt_busfunc_t *tr_func;
2324 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2326 return(tr_func(bus, tr_config->tr_arg));
2328 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2332 xptdeftargetfunc(struct cam_et *target, void *arg)
2334 struct xpt_traverse_config *tr_config;
2336 tr_config = (struct xpt_traverse_config *)arg;
2338 if (tr_config->depth == XPT_DEPTH_TARGET) {
2339 xpt_targetfunc_t *tr_func;
2341 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2343 return(tr_func(target, tr_config->tr_arg));
2345 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2349 xptdefdevicefunc(struct cam_ed *device, void *arg)
2351 struct xpt_traverse_config *tr_config;
2353 tr_config = (struct xpt_traverse_config *)arg;
2355 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2356 xpt_devicefunc_t *tr_func;
2358 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2360 return(tr_func(device, tr_config->tr_arg));
2362 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2366 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2368 struct xpt_traverse_config *tr_config;
2369 xpt_periphfunc_t *tr_func;
2371 tr_config = (struct xpt_traverse_config *)arg;
2373 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2376 * Unlike the other default functions, we don't check for depth
2377 * here. The peripheral driver level is the last level in the EDT,
2378 * so if we're here, we should execute the function in question.
2380 return(tr_func(periph, tr_config->tr_arg));
2384 * Execute the given function for every bus in the EDT.
2387 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2389 struct xpt_traverse_config tr_config;
2391 tr_config.depth = XPT_DEPTH_BUS;
2392 tr_config.tr_func = tr_func;
2393 tr_config.tr_arg = arg;
2395 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2399 * Execute the given function for every device in the EDT.
2402 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2404 struct xpt_traverse_config tr_config;
2406 tr_config.depth = XPT_DEPTH_DEVICE;
2407 tr_config.tr_func = tr_func;
2408 tr_config.tr_arg = arg;
2410 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2414 xptsetasyncfunc(struct cam_ed *device, void *arg)
2416 struct cam_path path;
2417 struct ccb_getdev cgd;
2418 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2421 * Don't report unconfigured devices (Wildcard devs,
2422 * devices only for target mode, device instances
2423 * that have been invalidated but are waiting for
2424 * their last reference count to be released).
2426 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2429 xpt_compile_path(&path,
2431 device->target->bus->path_id,
2432 device->target->target_id,
2434 xpt_setup_ccb(&cgd.ccb_h, &path, CAM_PRIORITY_NORMAL);
2435 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2436 xpt_action((union ccb *)&cgd);
2437 csa->callback(csa->callback_arg,
2440 xpt_release_path(&path);
2446 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2448 struct cam_path path;
2449 struct ccb_pathinq cpi;
2450 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2452 xpt_compile_path(&path, /*periph*/NULL,
2454 CAM_TARGET_WILDCARD,
2456 xpt_setup_ccb(&cpi.ccb_h, &path, CAM_PRIORITY_NORMAL);
2457 cpi.ccb_h.func_code = XPT_PATH_INQ;
2458 xpt_action((union ccb *)&cpi);
2459 csa->callback(csa->callback_arg,
2462 xpt_release_path(&path);
2468 xpt_action(union ccb *start_ccb)
2471 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2473 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2474 (*(start_ccb->ccb_h.path->bus->xport->action))(start_ccb);
2478 xpt_action_default(union ccb *start_ccb)
2480 struct cam_path *path;
2482 path = start_ccb->ccb_h.path;
2483 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_action_default\n"));
2485 switch (start_ccb->ccb_h.func_code) {
2488 struct cam_ed *device;
2491 * For the sake of compatibility with SCSI-1
2492 * devices that may not understand the identify
2493 * message, we include lun information in the
2494 * second byte of all commands. SCSI-1 specifies
2495 * that luns are a 3 bit value and reserves only 3
2496 * bits for lun information in the CDB. Later
2497 * revisions of the SCSI spec allow for more than 8
2498 * luns, but have deprecated lun information in the
2499 * CDB. So, if the lun won't fit, we must omit.
2501 * Also be aware that during initial probing for devices,
2502 * the inquiry information is unknown but initialized to 0.
2503 * This means that this code will be exercised while probing
2504 * devices with an ANSI revision greater than 2.
2506 device = path->device;
2507 if (device->protocol_version <= SCSI_REV_2
2508 && start_ccb->ccb_h.target_lun < 8
2509 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2511 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2512 start_ccb->ccb_h.target_lun << 5;
2514 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2518 case XPT_CONT_TARGET_IO:
2519 start_ccb->csio.sense_resid = 0;
2520 start_ccb->csio.resid = 0;
2523 if (start_ccb->ccb_h.func_code == XPT_ATA_IO)
2524 start_ccb->ataio.resid = 0;
2532 frozen = cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2533 path->device->sim->devq->alloc_openings += frozen;
2535 xpt_run_dev_allocq(path->bus);
2536 if (xpt_schedule_dev_sendq(path->bus, path->device))
2537 xpt_run_dev_sendq(path->bus);
2540 case XPT_CALC_GEOMETRY:
2542 struct cam_sim *sim;
2544 /* Filter out garbage */
2545 if (start_ccb->ccg.block_size == 0
2546 || start_ccb->ccg.volume_size == 0) {
2547 start_ccb->ccg.cylinders = 0;
2548 start_ccb->ccg.heads = 0;
2549 start_ccb->ccg.secs_per_track = 0;
2550 start_ccb->ccb_h.status = CAM_REQ_CMP;
2553 #if defined(PC98) || defined(__sparc64__)
2555 * In a PC-98 system, geometry translation depens on
2556 * the "real" device geometry obtained from mode page 4.
2557 * SCSI geometry translation is performed in the
2558 * initialization routine of the SCSI BIOS and the result
2559 * stored in host memory. If the translation is available
2560 * in host memory, use it. If not, rely on the default
2561 * translation the device driver performs.
2562 * For sparc64, we may need adjust the geometry of large
2563 * disks in order to fit the limitations of the 16-bit
2564 * fields of the VTOC8 disk label.
2566 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
2567 start_ccb->ccb_h.status = CAM_REQ_CMP;
2571 sim = path->bus->sim;
2572 (*(sim->sim_action))(sim, start_ccb);
2577 union ccb* abort_ccb;
2579 abort_ccb = start_ccb->cab.abort_ccb;
2580 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
2582 if (abort_ccb->ccb_h.pinfo.index >= 0) {
2583 struct cam_ccbq *ccbq;
2584 struct cam_ed *device;
2586 device = abort_ccb->ccb_h.path->device;
2587 ccbq = &device->ccbq;
2588 device->sim->devq->alloc_openings -=
2589 cam_ccbq_remove_ccb(ccbq, abort_ccb);
2590 abort_ccb->ccb_h.status =
2591 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2592 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2593 xpt_done(abort_ccb);
2594 start_ccb->ccb_h.status = CAM_REQ_CMP;
2597 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
2598 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
2600 * We've caught this ccb en route to
2601 * the SIM. Flag it for abort and the
2602 * SIM will do so just before starting
2603 * real work on the CCB.
2605 abort_ccb->ccb_h.status =
2606 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2607 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2608 start_ccb->ccb_h.status = CAM_REQ_CMP;
2612 if (XPT_FC_IS_QUEUED(abort_ccb)
2613 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
2615 * It's already completed but waiting
2616 * for our SWI to get to it.
2618 start_ccb->ccb_h.status = CAM_UA_ABORT;
2622 * If we weren't able to take care of the abort request
2623 * in the XPT, pass the request down to the SIM for processing.
2627 case XPT_ACCEPT_TARGET_IO:
2629 case XPT_IMMED_NOTIFY:
2630 case XPT_NOTIFY_ACK:
2632 case XPT_IMMEDIATE_NOTIFY:
2633 case XPT_NOTIFY_ACKNOWLEDGE:
2634 case XPT_GET_SIM_KNOB:
2635 case XPT_SET_SIM_KNOB:
2637 struct cam_sim *sim;
2639 sim = path->bus->sim;
2640 (*(sim->sim_action))(sim, start_ccb);
2645 struct cam_sim *sim;
2647 sim = path->bus->sim;
2648 (*(sim->sim_action))(sim, start_ccb);
2651 case XPT_PATH_STATS:
2652 start_ccb->cpis.last_reset = path->bus->last_reset;
2653 start_ccb->ccb_h.status = CAM_REQ_CMP;
2660 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2661 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2663 struct ccb_getdev *cgd;
2665 cgd = &start_ccb->cgd;
2666 cgd->protocol = dev->protocol;
2667 cgd->inq_data = dev->inq_data;
2668 cgd->ident_data = dev->ident_data;
2669 cgd->inq_flags = dev->inq_flags;
2670 cgd->ccb_h.status = CAM_REQ_CMP;
2671 cgd->serial_num_len = dev->serial_num_len;
2672 if ((dev->serial_num_len > 0)
2673 && (dev->serial_num != NULL))
2674 bcopy(dev->serial_num, cgd->serial_num,
2675 dev->serial_num_len);
2679 case XPT_GDEV_STATS:
2684 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2685 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2687 struct ccb_getdevstats *cgds;
2691 cgds = &start_ccb->cgds;
2694 cgds->dev_openings = dev->ccbq.dev_openings;
2695 cgds->dev_active = dev->ccbq.dev_active;
2696 cgds->devq_openings = dev->ccbq.devq_openings;
2697 cgds->devq_queued = dev->ccbq.queue.entries;
2698 cgds->held = dev->ccbq.held;
2699 cgds->last_reset = tar->last_reset;
2700 cgds->maxtags = dev->maxtags;
2701 cgds->mintags = dev->mintags;
2702 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
2703 cgds->last_reset = bus->last_reset;
2704 cgds->ccb_h.status = CAM_REQ_CMP;
2710 struct cam_periph *nperiph;
2711 struct periph_list *periph_head;
2712 struct ccb_getdevlist *cgdl;
2714 struct cam_ed *device;
2721 * Don't want anyone mucking with our data.
2723 device = path->device;
2724 periph_head = &device->periphs;
2725 cgdl = &start_ccb->cgdl;
2728 * Check and see if the list has changed since the user
2729 * last requested a list member. If so, tell them that the
2730 * list has changed, and therefore they need to start over
2731 * from the beginning.
2733 if ((cgdl->index != 0) &&
2734 (cgdl->generation != device->generation)) {
2735 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
2740 * Traverse the list of peripherals and attempt to find
2741 * the requested peripheral.
2743 for (nperiph = SLIST_FIRST(periph_head), i = 0;
2744 (nperiph != NULL) && (i <= cgdl->index);
2745 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
2746 if (i == cgdl->index) {
2747 strncpy(cgdl->periph_name,
2748 nperiph->periph_name,
2750 cgdl->unit_number = nperiph->unit_number;
2755 cgdl->status = CAM_GDEVLIST_ERROR;
2759 if (nperiph == NULL)
2760 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
2762 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
2765 cgdl->generation = device->generation;
2767 cgdl->ccb_h.status = CAM_REQ_CMP;
2772 dev_pos_type position_type;
2773 struct ccb_dev_match *cdm;
2775 cdm = &start_ccb->cdm;
2778 * There are two ways of getting at information in the EDT.
2779 * The first way is via the primary EDT tree. It starts
2780 * with a list of busses, then a list of targets on a bus,
2781 * then devices/luns on a target, and then peripherals on a
2782 * device/lun. The "other" way is by the peripheral driver
2783 * lists. The peripheral driver lists are organized by
2784 * peripheral driver. (obviously) So it makes sense to
2785 * use the peripheral driver list if the user is looking
2786 * for something like "da1", or all "da" devices. If the
2787 * user is looking for something on a particular bus/target
2788 * or lun, it's generally better to go through the EDT tree.
2791 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
2792 position_type = cdm->pos.position_type;
2796 position_type = CAM_DEV_POS_NONE;
2798 for (i = 0; i < cdm->num_patterns; i++) {
2799 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
2800 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
2801 position_type = CAM_DEV_POS_EDT;
2806 if (cdm->num_patterns == 0)
2807 position_type = CAM_DEV_POS_EDT;
2808 else if (position_type == CAM_DEV_POS_NONE)
2809 position_type = CAM_DEV_POS_PDRV;
2813 * Note that we drop the SIM lock here, because the EDT
2814 * traversal code needs to do its own locking.
2816 CAM_SIM_UNLOCK(xpt_path_sim(cdm->ccb_h.path));
2817 switch(position_type & CAM_DEV_POS_TYPEMASK) {
2818 case CAM_DEV_POS_EDT:
2821 case CAM_DEV_POS_PDRV:
2822 xptperiphlistmatch(cdm);
2825 cdm->status = CAM_DEV_MATCH_ERROR;
2828 CAM_SIM_LOCK(xpt_path_sim(cdm->ccb_h.path));
2830 if (cdm->status == CAM_DEV_MATCH_ERROR)
2831 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2833 start_ccb->ccb_h.status = CAM_REQ_CMP;
2839 struct ccb_setasync *csa;
2840 struct async_node *cur_entry;
2841 struct async_list *async_head;
2844 csa = &start_ccb->csa;
2845 added = csa->event_enable;
2846 async_head = &path->device->asyncs;
2849 * If there is already an entry for us, simply
2852 cur_entry = SLIST_FIRST(async_head);
2853 while (cur_entry != NULL) {
2854 if ((cur_entry->callback_arg == csa->callback_arg)
2855 && (cur_entry->callback == csa->callback))
2857 cur_entry = SLIST_NEXT(cur_entry, links);
2860 if (cur_entry != NULL) {
2862 * If the request has no flags set,
2865 added &= ~cur_entry->event_enable;
2866 if (csa->event_enable == 0) {
2867 SLIST_REMOVE(async_head, cur_entry,
2869 xpt_release_device(path->device);
2870 free(cur_entry, M_CAMXPT);
2872 cur_entry->event_enable = csa->event_enable;
2874 csa->event_enable = added;
2876 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
2878 if (cur_entry == NULL) {
2879 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
2882 cur_entry->event_enable = csa->event_enable;
2883 cur_entry->callback_arg = csa->callback_arg;
2884 cur_entry->callback = csa->callback;
2885 SLIST_INSERT_HEAD(async_head, cur_entry, links);
2886 xpt_acquire_device(path->device);
2888 start_ccb->ccb_h.status = CAM_REQ_CMP;
2893 struct ccb_relsim *crs;
2896 crs = &start_ccb->crs;
2900 crs->ccb_h.status = CAM_DEV_NOT_THERE;
2904 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
2906 /* Don't ever go below one opening */
2907 if (crs->openings > 0) {
2908 xpt_dev_ccbq_resize(path, crs->openings);
2911 "number of openings is now %d\n",
2917 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
2919 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
2922 * Just extend the old timeout and decrement
2923 * the freeze count so that a single timeout
2924 * is sufficient for releasing the queue.
2926 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2927 callout_stop(&dev->callout);
2930 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2933 callout_reset(&dev->callout,
2934 (crs->release_timeout * hz) / 1000,
2935 xpt_release_devq_timeout, dev);
2937 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
2941 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
2943 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
2945 * Decrement the freeze count so that a single
2946 * completion is still sufficient to unfreeze
2949 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2952 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
2953 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2957 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
2959 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
2960 || (dev->ccbq.dev_active == 0)) {
2962 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2965 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
2966 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2970 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
2971 xpt_release_devq_rl(path, /*runlevel*/
2972 (crs->release_flags & RELSIM_RELEASE_RUNLEVEL) ?
2973 crs->release_timeout : 0,
2974 /*count*/1, /*run_queue*/TRUE);
2976 start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt[0];
2977 start_ccb->ccb_h.status = CAM_REQ_CMP;
2981 struct cam_path *oldpath;
2982 struct cam_sim *oldsim;
2984 /* Check that all request bits are supported. */
2985 if (start_ccb->cdbg.flags & ~(CAM_DEBUG_COMPILE)) {
2986 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2990 cam_dflags = CAM_DEBUG_NONE;
2991 if (cam_dpath != NULL) {
2992 /* To release the old path we must hold proper lock. */
2993 oldpath = cam_dpath;
2995 oldsim = xpt_path_sim(oldpath);
2996 CAM_SIM_UNLOCK(xpt_path_sim(start_ccb->ccb_h.path));
2997 CAM_SIM_LOCK(oldsim);
2998 xpt_free_path(oldpath);
2999 CAM_SIM_UNLOCK(oldsim);
3000 CAM_SIM_LOCK(xpt_path_sim(start_ccb->ccb_h.path));
3002 if (start_ccb->cdbg.flags != CAM_DEBUG_NONE) {
3003 if (xpt_create_path(&cam_dpath, xpt_periph,
3004 start_ccb->ccb_h.path_id,
3005 start_ccb->ccb_h.target_id,
3006 start_ccb->ccb_h.target_lun) !=
3008 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3010 cam_dflags = start_ccb->cdbg.flags;
3011 start_ccb->ccb_h.status = CAM_REQ_CMP;
3012 xpt_print(cam_dpath, "debugging flags now %x\n",
3016 start_ccb->ccb_h.status = CAM_REQ_CMP;
3019 case XPT_FREEZE_QUEUE:
3021 struct ccb_relsim *crs = &start_ccb->crs;
3023 xpt_freeze_devq_rl(path, /*runlevel*/
3024 (crs->release_flags & RELSIM_RELEASE_RUNLEVEL) ?
3025 crs->release_timeout : 0, /*count*/1);
3026 start_ccb->ccb_h.status = CAM_REQ_CMP;
3030 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3031 xpt_freeze_devq(path, 1);
3032 start_ccb->ccb_h.status = CAM_REQ_CMP;
3039 printf("%s: CCB type %#x not supported\n", __func__,
3040 start_ccb->ccb_h.func_code);
3041 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3042 if (start_ccb->ccb_h.func_code & XPT_FC_DEV_QUEUED) {
3043 xpt_done(start_ccb);
3050 xpt_polled_action(union ccb *start_ccb)
3053 struct cam_sim *sim;
3054 struct cam_devq *devq;
3058 timeout = start_ccb->ccb_h.timeout * 10;
3059 sim = start_ccb->ccb_h.path->bus->sim;
3061 dev = start_ccb->ccb_h.path->device;
3063 mtx_assert(sim->mtx, MA_OWNED);
3065 /* Don't use ISR for this SIM while polling. */
3066 sim->flags |= CAM_SIM_POLLED;
3069 * Steal an opening so that no other queued requests
3070 * can get it before us while we simulate interrupts.
3072 dev->ccbq.devq_openings--;
3073 dev->ccbq.dev_openings--;
3075 while(((devq != NULL && devq->send_openings <= 0) ||
3076 dev->ccbq.dev_openings < 0) && (--timeout > 0)) {
3078 (*(sim->sim_poll))(sim);
3079 camisr_runqueue(&sim->sim_doneq);
3082 dev->ccbq.devq_openings++;
3083 dev->ccbq.dev_openings++;
3086 xpt_action(start_ccb);
3087 while(--timeout > 0) {
3088 (*(sim->sim_poll))(sim);
3089 camisr_runqueue(&sim->sim_doneq);
3090 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3097 * XXX Is it worth adding a sim_timeout entry
3098 * point so we can attempt recovery? If
3099 * this is only used for dumps, I don't think
3102 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3105 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3108 /* We will use CAM ISR for this SIM again. */
3109 sim->flags &= ~CAM_SIM_POLLED;
3113 * Schedule a peripheral driver to receive a ccb when it's
3114 * target device has space for more transactions.
3117 xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3119 struct cam_ed *device;
3122 mtx_assert(perph->sim->mtx, MA_OWNED);
3124 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3125 device = perph->path->device;
3126 if (periph_is_queued(perph)) {
3127 /* Simply reorder based on new priority */
3128 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3129 (" change priority to %d\n", new_priority));
3130 if (new_priority < perph->pinfo.priority) {
3131 camq_change_priority(&device->drvq,
3134 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3137 /* New entry on the queue */
3138 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3139 (" added periph to queue\n"));
3140 perph->pinfo.priority = new_priority;
3141 perph->pinfo.generation = ++device->drvq.generation;
3142 camq_insert(&device->drvq, &perph->pinfo);
3143 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3146 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3147 (" calling xpt_run_devq\n"));
3148 xpt_run_dev_allocq(perph->path->bus);
3154 * Schedule a device to run on a given queue.
3155 * If the device was inserted as a new entry on the queue,
3156 * return 1 meaning the device queue should be run. If we
3157 * were already queued, implying someone else has already
3158 * started the queue, return 0 so the caller doesn't attempt
3162 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3163 u_int32_t new_priority)
3166 u_int32_t old_priority;
3168 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3170 old_priority = pinfo->priority;
3173 * Are we already queued?
3175 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3176 /* Simply reorder based on new priority */
3177 if (new_priority < old_priority) {
3178 camq_change_priority(queue, pinfo->index,
3180 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3181 ("changed priority to %d\n",
3187 /* New entry on the queue */
3188 if (new_priority < old_priority)
3189 pinfo->priority = new_priority;
3191 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3192 ("Inserting onto queue\n"));
3193 pinfo->generation = ++queue->generation;
3194 camq_insert(queue, pinfo);
3201 xpt_run_dev_allocq(struct cam_eb *bus)
3203 struct cam_devq *devq;
3205 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
3206 devq = bus->sim->devq;
3208 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3209 (" qfrozen_cnt == 0x%x, entries == %d, "
3210 "openings == %d, active == %d\n",
3211 devq->alloc_queue.qfrozen_cnt[0],
3212 devq->alloc_queue.entries,
3213 devq->alloc_openings,
3214 devq->alloc_active));
3216 devq->alloc_queue.qfrozen_cnt[0]++;
3217 while ((devq->alloc_queue.entries > 0)
3218 && (devq->alloc_openings > 0)
3219 && (devq->alloc_queue.qfrozen_cnt[0] <= 1)) {
3220 struct cam_ed_qinfo *qinfo;
3221 struct cam_ed *device;
3222 union ccb *work_ccb;
3223 struct cam_periph *drv;
3226 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3228 device = qinfo->device;
3229 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3230 ("running device %p\n", device));
3232 drvq = &device->drvq;
3233 KASSERT(drvq->entries > 0, ("xpt_run_dev_allocq: "
3234 "Device on queue without any work to do"));
3235 if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3236 devq->alloc_openings--;
3237 devq->alloc_active++;
3238 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3239 xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3240 drv->pinfo.priority);
3241 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3242 ("calling periph start\n"));
3243 drv->periph_start(drv, work_ccb);
3246 * Malloc failure in alloc_ccb
3249 * XXX add us to a list to be run from free_ccb
3250 * if we don't have any ccbs active on this
3251 * device queue otherwise we may never get run
3257 /* We may have more work. Attempt to reschedule. */
3258 xpt_schedule_dev_allocq(bus, device);
3260 devq->alloc_queue.qfrozen_cnt[0]--;
3264 xpt_run_dev_sendq(struct cam_eb *bus)
3266 struct cam_devq *devq;
3267 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
3269 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
3271 devq = bus->sim->devq;
3273 devq->send_queue.qfrozen_cnt[0]++;
3274 while ((devq->send_queue.entries > 0)
3275 && (devq->send_openings > 0)
3276 && (devq->send_queue.qfrozen_cnt[0] <= 1)) {
3277 struct cam_ed_qinfo *qinfo;
3278 struct cam_ed *device;
3279 union ccb *work_ccb;
3280 struct cam_sim *sim;
3282 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3284 device = qinfo->device;
3285 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3286 ("running device %p\n", device));
3288 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3289 if (work_ccb == NULL) {
3290 printf("device on run queue with no ccbs???\n");
3294 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3296 mtx_lock(&xsoftc.xpt_lock);
3297 if (xsoftc.num_highpower <= 0) {
3299 * We got a high power command, but we
3300 * don't have any available slots. Freeze
3301 * the device queue until we have a slot
3304 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3305 STAILQ_INSERT_TAIL(&xsoftc.highpowerq,
3309 mtx_unlock(&xsoftc.xpt_lock);
3313 * Consume a high power slot while
3316 xsoftc.num_highpower--;
3318 mtx_unlock(&xsoftc.xpt_lock);
3320 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3321 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3323 devq->send_openings--;
3324 devq->send_active++;
3326 xpt_schedule_dev_sendq(bus, device);
3328 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
3330 * The client wants to freeze the queue
3331 * after this CCB is sent.
3333 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3336 /* In Target mode, the peripheral driver knows best... */
3337 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3338 if ((device->inq_flags & SID_CmdQue) != 0
3339 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3340 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3343 * Clear this in case of a retried CCB that
3344 * failed due to a rejected tag.
3346 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3349 switch (work_ccb->ccb_h.func_code) {
3351 CAM_DEBUG(work_ccb->ccb_h.path,
3352 CAM_DEBUG_CDB,("%s. CDB: %s\n",
3353 scsi_op_desc(work_ccb->csio.cdb_io.cdb_bytes[0],
3355 scsi_cdb_string(work_ccb->csio.cdb_io.cdb_bytes,
3356 cdb_str, sizeof(cdb_str))));
3359 CAM_DEBUG(work_ccb->ccb_h.path,
3360 CAM_DEBUG_CDB,("%s. ACB: %s\n",
3361 ata_op_string(&work_ccb->ataio.cmd),
3362 ata_cmd_string(&work_ccb->ataio.cmd,
3363 cdb_str, sizeof(cdb_str))));
3370 * Device queues can be shared among multiple sim instances
3371 * that reside on different busses. Use the SIM in the queue
3372 * CCB's path, rather than the one in the bus that was passed
3373 * into this function.
3375 sim = work_ccb->ccb_h.path->bus->sim;
3376 (*(sim->sim_action))(sim, work_ccb);
3378 devq->send_queue.qfrozen_cnt[0]--;
3382 * This function merges stuff from the slave ccb into the master ccb, while
3383 * keeping important fields in the master ccb constant.
3386 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3390 * Pull fields that are valid for peripheral drivers to set
3391 * into the master CCB along with the CCB "payload".
3393 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3394 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3395 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3396 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3397 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3398 sizeof(union ccb) - sizeof(struct ccb_hdr));
3402 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3405 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3406 ccb_h->pinfo.priority = priority;
3408 ccb_h->path_id = path->bus->path_id;
3410 ccb_h->target_id = path->target->target_id;
3412 ccb_h->target_id = CAM_TARGET_WILDCARD;
3414 ccb_h->target_lun = path->device->lun_id;
3415 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3417 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3419 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3423 /* Path manipulation functions */
3425 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3426 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3428 struct cam_path *path;
3431 path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3434 status = CAM_RESRC_UNAVAIL;
3437 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3438 if (status != CAM_REQ_CMP) {
3439 free(path, M_CAMPATH);
3442 *new_path_ptr = path;
3447 xpt_create_path_unlocked(struct cam_path **new_path_ptr,
3448 struct cam_periph *periph, path_id_t path_id,
3449 target_id_t target_id, lun_id_t lun_id)
3451 struct cam_path *path;
3452 struct cam_eb *bus = NULL;
3455 path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_WAITOK);
3457 bus = xpt_find_bus(path_id);
3459 CAM_SIM_LOCK(bus->sim);
3460 status = xpt_compile_path(path, periph, path_id, target_id, lun_id);
3462 CAM_SIM_UNLOCK(bus->sim);
3463 xpt_release_bus(bus);
3465 if (status != CAM_REQ_CMP) {
3466 free(path, M_CAMPATH);
3469 *new_path_ptr = path;
3474 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3475 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3478 struct cam_et *target;
3479 struct cam_ed *device;
3482 status = CAM_REQ_CMP; /* Completed without error */
3483 target = NULL; /* Wildcarded */
3484 device = NULL; /* Wildcarded */
3487 * We will potentially modify the EDT, so block interrupts
3488 * that may attempt to create cam paths.
3490 bus = xpt_find_bus(path_id);
3492 status = CAM_PATH_INVALID;
3494 target = xpt_find_target(bus, target_id);
3495 if (target == NULL) {
3497 struct cam_et *new_target;
3499 new_target = xpt_alloc_target(bus, target_id);
3500 if (new_target == NULL) {
3501 status = CAM_RESRC_UNAVAIL;
3503 target = new_target;
3506 if (target != NULL) {
3507 device = xpt_find_device(target, lun_id);
3508 if (device == NULL) {
3510 struct cam_ed *new_device;
3513 (*(bus->xport->alloc_device))(bus,
3516 if (new_device == NULL) {
3517 status = CAM_RESRC_UNAVAIL;
3519 device = new_device;
3526 * Only touch the user's data if we are successful.
3528 if (status == CAM_REQ_CMP) {
3529 new_path->periph = perph;
3530 new_path->bus = bus;
3531 new_path->target = target;
3532 new_path->device = device;
3533 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
3536 xpt_release_device(device);
3538 xpt_release_target(target);
3540 xpt_release_bus(bus);
3546 xpt_release_path(struct cam_path *path)
3548 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
3549 if (path->device != NULL) {
3550 xpt_release_device(path->device);
3551 path->device = NULL;
3553 if (path->target != NULL) {
3554 xpt_release_target(path->target);
3555 path->target = NULL;
3557 if (path->bus != NULL) {
3558 xpt_release_bus(path->bus);
3564 xpt_free_path(struct cam_path *path)
3567 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
3568 xpt_release_path(path);
3569 free(path, M_CAMPATH);
3573 xpt_path_counts(struct cam_path *path, uint32_t *bus_ref,
3574 uint32_t *periph_ref, uint32_t *target_ref, uint32_t *device_ref)
3580 *bus_ref = path->bus->refcount;
3586 *periph_ref = path->periph->refcount;
3593 *target_ref = path->target->refcount;
3599 *device_ref = path->device->refcount;
3606 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
3607 * in path1, 2 for match with wildcards in path2.
3610 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
3614 if (path1->bus != path2->bus) {
3615 if (path1->bus->path_id == CAM_BUS_WILDCARD)
3617 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
3622 if (path1->target != path2->target) {
3623 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
3626 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
3631 if (path1->device != path2->device) {
3632 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
3635 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
3644 xpt_print_path(struct cam_path *path)
3648 printf("(nopath): ");
3650 if (path->periph != NULL)
3651 printf("(%s%d:", path->periph->periph_name,
3652 path->periph->unit_number);
3654 printf("(noperiph:");
3656 if (path->bus != NULL)
3657 printf("%s%d:%d:", path->bus->sim->sim_name,
3658 path->bus->sim->unit_number,
3659 path->bus->sim->bus_id);
3663 if (path->target != NULL)
3664 printf("%d:", path->target->target_id);
3668 if (path->device != NULL)
3669 printf("%d): ", path->device->lun_id);
3676 xpt_print(struct cam_path *path, const char *fmt, ...)
3679 xpt_print_path(path);
3686 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
3691 if (path != NULL && path->bus != NULL)
3692 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3695 sbuf_new(&sb, str, str_len, 0);
3698 sbuf_printf(&sb, "(nopath): ");
3700 if (path->periph != NULL)
3701 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
3702 path->periph->unit_number);
3704 sbuf_printf(&sb, "(noperiph:");
3706 if (path->bus != NULL)
3707 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
3708 path->bus->sim->unit_number,
3709 path->bus->sim->bus_id);
3711 sbuf_printf(&sb, "nobus:");
3713 if (path->target != NULL)
3714 sbuf_printf(&sb, "%d:", path->target->target_id);
3716 sbuf_printf(&sb, "X:");
3718 if (path->device != NULL)
3719 sbuf_printf(&sb, "%d): ", path->device->lun_id);
3721 sbuf_printf(&sb, "X): ");
3725 return(sbuf_len(&sb));
3729 xpt_path_path_id(struct cam_path *path)
3731 return(path->bus->path_id);
3735 xpt_path_target_id(struct cam_path *path)
3737 if (path->target != NULL)
3738 return (path->target->target_id);
3740 return (CAM_TARGET_WILDCARD);
3744 xpt_path_lun_id(struct cam_path *path)
3746 if (path->device != NULL)
3747 return (path->device->lun_id);
3749 return (CAM_LUN_WILDCARD);
3753 xpt_path_sim(struct cam_path *path)
3756 return (path->bus->sim);
3760 xpt_path_periph(struct cam_path *path)
3762 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3764 return (path->periph);
3768 xpt_path_legacy_ata_id(struct cam_path *path)
3773 if ((strcmp(path->bus->sim->sim_name, "ata") != 0) &&
3774 strcmp(path->bus->sim->sim_name, "ahcich") != 0 &&
3775 strcmp(path->bus->sim->sim_name, "mvsch") != 0 &&
3776 strcmp(path->bus->sim->sim_name, "siisch") != 0)
3779 if (strcmp(path->bus->sim->sim_name, "ata") == 0 &&
3780 path->bus->sim->unit_number < 2) {
3781 bus_id = path->bus->sim->unit_number;
3785 TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) {
3786 if (bus == path->bus)
3788 if ((strcmp(bus->sim->sim_name, "ata") == 0 &&
3789 bus->sim->unit_number >= 2) ||
3790 strcmp(bus->sim->sim_name, "ahcich") == 0 ||
3791 strcmp(bus->sim->sim_name, "mvsch") == 0 ||
3792 strcmp(bus->sim->sim_name, "siisch") == 0)
3797 if (path->target != NULL) {
3798 if (path->target->target_id < 2)
3799 return (bus_id * 2 + path->target->target_id);
3803 return (bus_id * 2);
3807 * Release a CAM control block for the caller. Remit the cost of the structure
3808 * to the device referenced by the path. If the this device had no 'credits'
3809 * and peripheral drivers have registered async callbacks for this notification
3813 xpt_release_ccb(union ccb *free_ccb)
3815 struct cam_path *path;
3816 struct cam_ed *device;
3818 struct cam_sim *sim;
3820 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
3821 path = free_ccb->ccb_h.path;
3822 device = path->device;
3826 mtx_assert(sim->mtx, MA_OWNED);
3828 cam_ccbq_release_opening(&device->ccbq);
3829 if (device->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) {
3830 device->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
3831 cam_ccbq_resize(&device->ccbq,
3832 device->ccbq.dev_openings + device->ccbq.dev_active);
3834 if (sim->ccb_count > sim->max_ccbs) {
3835 xpt_free_ccb(free_ccb);
3838 SLIST_INSERT_HEAD(&sim->ccb_freeq, &free_ccb->ccb_h,
3841 if (sim->devq == NULL) {
3844 sim->devq->alloc_openings++;
3845 sim->devq->alloc_active--;
3846 if (device_is_alloc_queued(device) == 0)
3847 xpt_schedule_dev_allocq(bus, device);
3848 xpt_run_dev_allocq(bus);
3851 /* Functions accessed by SIM drivers */
3853 static struct xpt_xport xport_default = {
3854 .alloc_device = xpt_alloc_device_default,
3855 .action = xpt_action_default,
3856 .async = xpt_dev_async_default,
3860 * A sim structure, listing the SIM entry points and instance
3861 * identification info is passed to xpt_bus_register to hook the SIM
3862 * into the CAM framework. xpt_bus_register creates a cam_eb entry
3863 * for this new bus and places it in the array of busses and assigns
3864 * it a path_id. The path_id may be influenced by "hard wiring"
3865 * information specified by the user. Once interrupt services are
3866 * available, the bus will be probed.
3869 xpt_bus_register(struct cam_sim *sim, device_t parent, u_int32_t bus)
3871 struct cam_eb *new_bus;
3872 struct cam_eb *old_bus;
3873 struct ccb_pathinq cpi;
3874 struct cam_path *path;
3877 mtx_assert(sim->mtx, MA_OWNED);
3880 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
3881 M_CAMXPT, M_NOWAIT);
3882 if (new_bus == NULL) {
3883 /* Couldn't satisfy request */
3884 return (CAM_RESRC_UNAVAIL);
3886 if (strcmp(sim->sim_name, "xpt") != 0) {
3888 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
3891 TAILQ_INIT(&new_bus->et_entries);
3892 new_bus->path_id = sim->path_id;
3895 timevalclear(&new_bus->last_reset);
3897 new_bus->refcount = 1; /* Held until a bus_deregister event */
3898 new_bus->generation = 0;
3901 old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3902 while (old_bus != NULL
3903 && old_bus->path_id < new_bus->path_id)
3904 old_bus = TAILQ_NEXT(old_bus, links);
3905 if (old_bus != NULL)
3906 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
3908 TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
3909 xsoftc.bus_generation++;
3913 * Set a default transport so that a PATH_INQ can be issued to
3914 * the SIM. This will then allow for probing and attaching of
3915 * a more appropriate transport.
3917 new_bus->xport = &xport_default;
3919 status = xpt_create_path(&path, /*periph*/NULL, sim->path_id,
3920 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3921 if (status != CAM_REQ_CMP) {
3922 xpt_release_bus(new_bus);
3923 free(path, M_CAMXPT);
3924 return (CAM_RESRC_UNAVAIL);
3927 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
3928 cpi.ccb_h.func_code = XPT_PATH_INQ;
3929 xpt_action((union ccb *)&cpi);
3931 if (cpi.ccb_h.status == CAM_REQ_CMP) {
3932 switch (cpi.transport) {
3939 new_bus->xport = scsi_get_xport();
3943 new_bus->xport = ata_get_xport();
3946 new_bus->xport = &xport_default;
3951 /* Notify interested parties */
3952 if (sim->path_id != CAM_XPT_PATH_ID) {
3953 union ccb *scan_ccb;
3955 xpt_async(AC_PATH_REGISTERED, path, &cpi);
3956 /* Initiate bus rescan. */
3957 scan_ccb = xpt_alloc_ccb_nowait();
3958 scan_ccb->ccb_h.path = path;
3959 scan_ccb->ccb_h.func_code = XPT_SCAN_BUS;
3960 scan_ccb->crcn.flags = 0;
3961 xpt_rescan(scan_ccb);
3963 xpt_free_path(path);
3964 return (CAM_SUCCESS);
3968 xpt_bus_deregister(path_id_t pathid)
3970 struct cam_path bus_path;
3973 status = xpt_compile_path(&bus_path, NULL, pathid,
3974 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3975 if (status != CAM_REQ_CMP)
3978 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
3979 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
3981 /* Release the reference count held while registered. */
3982 xpt_release_bus(bus_path.bus);
3983 xpt_release_path(&bus_path);
3985 return (CAM_REQ_CMP);
3989 xptnextfreepathid(void)
3997 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3999 /* Find an unoccupied pathid */
4000 while (bus != NULL && bus->path_id <= pathid) {
4001 if (bus->path_id == pathid)
4003 bus = TAILQ_NEXT(bus, links);
4008 * Ensure that this pathid is not reserved for
4009 * a bus that may be registered in the future.
4011 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4013 /* Start the search over */
4021 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4028 pathid = CAM_XPT_PATH_ID;
4029 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4031 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
4032 if (strcmp(dname, "scbus")) {
4033 /* Avoid a bit of foot shooting. */
4036 if (dunit < 0) /* unwired?! */
4038 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4039 if (sim_bus == val) {
4043 } else if (sim_bus == 0) {
4044 /* Unspecified matches bus 0 */
4048 printf("Ambiguous scbus configuration for %s%d "
4049 "bus %d, cannot wire down. The kernel "
4050 "config entry for scbus%d should "
4051 "specify a controller bus.\n"
4052 "Scbus will be assigned dynamically.\n",
4053 sim_name, sim_unit, sim_bus, dunit);
4058 if (pathid == CAM_XPT_PATH_ID)
4059 pathid = xptnextfreepathid();
4064 xpt_async_string(u_int32_t async_code)
4067 switch (async_code) {
4068 case AC_BUS_RESET: return ("AC_BUS_RESET");
4069 case AC_UNSOL_RESEL: return ("AC_UNSOL_RESEL");
4070 case AC_SCSI_AEN: return ("AC_SCSI_AEN");
4071 case AC_SENT_BDR: return ("AC_SENT_BDR");
4072 case AC_PATH_REGISTERED: return ("AC_PATH_REGISTERED");
4073 case AC_PATH_DEREGISTERED: return ("AC_PATH_DEREGISTERED");
4074 case AC_FOUND_DEVICE: return ("AC_FOUND_DEVICE");
4075 case AC_LOST_DEVICE: return ("AC_LOST_DEVICE");
4076 case AC_TRANSFER_NEG: return ("AC_TRANSFER_NEG");
4077 case AC_INQ_CHANGED: return ("AC_INQ_CHANGED");
4078 case AC_GETDEV_CHANGED: return ("AC_GETDEV_CHANGED");
4079 case AC_CONTRACT: return ("AC_CONTRACT");
4080 case AC_ADVINFO_CHANGED: return ("AC_ADVINFO_CHANGED");
4081 case AC_UNIT_ATTENTION: return ("AC_UNIT_ATTENTION");
4083 return ("AC_UNKNOWN");
4087 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4090 struct cam_et *target, *next_target;
4091 struct cam_ed *device, *next_device;
4093 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4094 CAM_DEBUG(path, CAM_DEBUG_TRACE | CAM_DEBUG_INFO,
4095 ("xpt_async(%s)\n", xpt_async_string(async_code)));
4098 * Most async events come from a CAM interrupt context. In
4099 * a few cases, the error recovery code at the peripheral layer,
4100 * which may run from our SWI or a process context, may signal
4101 * deferred events with a call to xpt_async.
4106 if (async_code == AC_BUS_RESET) {
4107 /* Update our notion of when the last reset occurred */
4108 microtime(&bus->last_reset);
4111 for (target = TAILQ_FIRST(&bus->et_entries);
4113 target = next_target) {
4115 next_target = TAILQ_NEXT(target, links);
4117 if (path->target != target
4118 && path->target->target_id != CAM_TARGET_WILDCARD
4119 && target->target_id != CAM_TARGET_WILDCARD)
4122 if (async_code == AC_SENT_BDR) {
4123 /* Update our notion of when the last reset occurred */
4124 microtime(&path->target->last_reset);
4127 for (device = TAILQ_FIRST(&target->ed_entries);
4129 device = next_device) {
4131 next_device = TAILQ_NEXT(device, links);
4133 if (path->device != device
4134 && path->device->lun_id != CAM_LUN_WILDCARD
4135 && device->lun_id != CAM_LUN_WILDCARD)
4138 * The async callback could free the device.
4139 * If it is a broadcast async, it doesn't hold
4140 * device reference, so take our own reference.
4142 xpt_acquire_device(device);
4143 (*(bus->xport->async))(async_code, bus,
4147 xpt_async_bcast(&device->asyncs, async_code,
4149 xpt_release_device(device);
4154 * If this wasn't a fully wildcarded async, tell all
4155 * clients that want all async events.
4157 if (bus != xpt_periph->path->bus)
4158 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4163 xpt_async_bcast(struct async_list *async_head,
4164 u_int32_t async_code,
4165 struct cam_path *path, void *async_arg)
4167 struct async_node *cur_entry;
4169 cur_entry = SLIST_FIRST(async_head);
4170 while (cur_entry != NULL) {
4171 struct async_node *next_entry;
4173 * Grab the next list entry before we call the current
4174 * entry's callback. This is because the callback function
4175 * can delete its async callback entry.
4177 next_entry = SLIST_NEXT(cur_entry, links);
4178 if ((cur_entry->event_enable & async_code) != 0)
4179 cur_entry->callback(cur_entry->callback_arg,
4182 cur_entry = next_entry;
4187 xpt_dev_async_default(u_int32_t async_code, struct cam_eb *bus,
4188 struct cam_et *target, struct cam_ed *device,
4191 printf("%s called\n", __func__);
4195 xpt_freeze_devq_rl(struct cam_path *path, cam_rl rl, u_int count)
4197 struct cam_ed *dev = path->device;
4199 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4200 dev->sim->devq->alloc_openings +=
4201 cam_ccbq_freeze(&dev->ccbq, rl, count);
4202 /* Remove frozen device from allocq. */
4203 if (device_is_alloc_queued(dev) &&
4204 cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL(
4205 CAMQ_GET_PRIO(&dev->drvq)))) {
4206 camq_remove(&dev->sim->devq->alloc_queue,
4207 dev->alloc_ccb_entry.pinfo.index);
4209 /* Remove frozen device from sendq. */
4210 if (device_is_send_queued(dev) &&
4211 cam_ccbq_frozen_top(&dev->ccbq)) {
4212 camq_remove(&dev->sim->devq->send_queue,
4213 dev->send_ccb_entry.pinfo.index);
4215 return (dev->ccbq.queue.qfrozen_cnt[rl]);
4219 xpt_freeze_devq(struct cam_path *path, u_int count)
4222 return (xpt_freeze_devq_rl(path, 0, count));
4226 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4229 mtx_assert(sim->mtx, MA_OWNED);
4230 sim->devq->send_queue.qfrozen_cnt[0] += count;
4231 return (sim->devq->send_queue.qfrozen_cnt[0]);
4235 xpt_release_devq_timeout(void *arg)
4237 struct cam_ed *device;
4239 device = (struct cam_ed *)arg;
4241 xpt_release_devq_device(device, /*rl*/0, /*count*/1, /*run_queue*/TRUE);
4245 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4247 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4249 xpt_release_devq_device(path->device, /*rl*/0, count, run_queue);
4253 xpt_release_devq_rl(struct cam_path *path, cam_rl rl, u_int count, int run_queue)
4255 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4257 xpt_release_devq_device(path->device, rl, count, run_queue);
4261 xpt_release_devq_device(struct cam_ed *dev, cam_rl rl, u_int count, int run_queue)
4264 if (count > dev->ccbq.queue.qfrozen_cnt[rl]) {
4266 printf("xpt_release_devq(%d): requested %u > present %u\n",
4267 rl, count, dev->ccbq.queue.qfrozen_cnt[rl]);
4269 count = dev->ccbq.queue.qfrozen_cnt[rl];
4271 dev->sim->devq->alloc_openings -=
4272 cam_ccbq_release(&dev->ccbq, rl, count);
4273 if (cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL(
4274 CAMQ_GET_PRIO(&dev->drvq))) == 0) {
4275 if (xpt_schedule_dev_allocq(dev->target->bus, dev))
4276 xpt_run_dev_allocq(dev->target->bus);
4278 if (cam_ccbq_frozen_top(&dev->ccbq) == 0) {
4280 * No longer need to wait for a successful
4281 * command completion.
4283 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4285 * Remove any timeouts that might be scheduled
4286 * to release this queue.
4288 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4289 callout_stop(&dev->callout);
4290 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4295 * Now that we are unfrozen schedule the
4296 * device so any pending transactions are
4299 if (xpt_schedule_dev_sendq(dev->target->bus, dev))
4300 xpt_run_dev_sendq(dev->target->bus);
4305 xpt_release_simq(struct cam_sim *sim, int run_queue)
4309 mtx_assert(sim->mtx, MA_OWNED);
4310 sendq = &(sim->devq->send_queue);
4311 if (sendq->qfrozen_cnt[0] <= 0) {
4313 printf("xpt_release_simq: requested 1 > present %u\n",
4314 sendq->qfrozen_cnt[0]);
4317 sendq->qfrozen_cnt[0]--;
4318 if (sendq->qfrozen_cnt[0] == 0) {
4320 * If there is a timeout scheduled to release this
4321 * sim queue, remove it. The queue frozen count is
4324 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4325 callout_stop(&sim->callout);
4326 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4332 * Now that we are unfrozen run the send queue.
4334 bus = xpt_find_bus(sim->path_id);
4335 xpt_run_dev_sendq(bus);
4336 xpt_release_bus(bus);
4342 * XXX Appears to be unused.
4345 xpt_release_simq_timeout(void *arg)
4347 struct cam_sim *sim;
4349 sim = (struct cam_sim *)arg;
4350 xpt_release_simq(sim, /* run_queue */ TRUE);
4354 xpt_done(union ccb *done_ccb)
4356 struct cam_sim *sim;
4359 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4360 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4362 * Queue up the request for handling by our SWI handler
4363 * any of the "non-immediate" type of ccbs.
4365 sim = done_ccb->ccb_h.path->bus->sim;
4366 TAILQ_INSERT_TAIL(&sim->sim_doneq, &done_ccb->ccb_h,
4368 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4369 if ((sim->flags & (CAM_SIM_ON_DONEQ | CAM_SIM_POLLED |
4370 CAM_SIM_BATCH)) == 0) {
4371 mtx_lock(&cam_simq_lock);
4372 first = TAILQ_EMPTY(&cam_simq);
4373 TAILQ_INSERT_TAIL(&cam_simq, sim, links);
4374 mtx_unlock(&cam_simq_lock);
4375 sim->flags |= CAM_SIM_ON_DONEQ;
4377 swi_sched(cambio_ih, 0);
4383 xpt_batch_start(struct cam_sim *sim)
4386 KASSERT((sim->flags & CAM_SIM_BATCH) == 0, ("Batch flag already set"));
4387 sim->flags |= CAM_SIM_BATCH;
4391 xpt_batch_done(struct cam_sim *sim)
4394 KASSERT((sim->flags & CAM_SIM_BATCH) != 0, ("Batch flag was not set"));
4395 sim->flags &= ~CAM_SIM_BATCH;
4396 if (!TAILQ_EMPTY(&sim->sim_doneq) &&
4397 (sim->flags & CAM_SIM_ON_DONEQ) == 0)
4398 camisr_runqueue(&sim->sim_doneq);
4406 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4411 xpt_alloc_ccb_nowait()
4415 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
4420 xpt_free_ccb(union ccb *free_ccb)
4422 free(free_ccb, M_CAMCCB);
4427 /* Private XPT functions */
4430 * Get a CAM control block for the caller. Charge the structure to the device
4431 * referenced by the path. If the this device has no 'credits' then the
4432 * device already has the maximum number of outstanding operations under way
4433 * and we return NULL. If we don't have sufficient resources to allocate more
4434 * ccbs, we also return NULL.
4437 xpt_get_ccb(struct cam_ed *device)
4440 struct cam_sim *sim;
4443 if ((new_ccb = (union ccb *)SLIST_FIRST(&sim->ccb_freeq)) == NULL) {
4444 new_ccb = xpt_alloc_ccb_nowait();
4445 if (new_ccb == NULL) {
4448 if ((sim->flags & CAM_SIM_MPSAFE) == 0)
4449 callout_handle_init(&new_ccb->ccb_h.timeout_ch);
4450 SLIST_INSERT_HEAD(&sim->ccb_freeq, &new_ccb->ccb_h,
4454 cam_ccbq_take_opening(&device->ccbq);
4455 SLIST_REMOVE_HEAD(&sim->ccb_freeq, xpt_links.sle);
4460 xpt_release_bus(struct cam_eb *bus)
4464 KASSERT(bus->refcount >= 1, ("bus->refcount >= 1"));
4465 if (--bus->refcount > 0) {
4469 KASSERT(TAILQ_EMPTY(&bus->et_entries),
4470 ("refcount is zero, but target list is not empty"));
4471 TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
4472 xsoftc.bus_generation++;
4474 cam_sim_release(bus->sim);
4475 free(bus, M_CAMXPT);
4478 static struct cam_et *
4479 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4481 struct cam_et *cur_target, *target;
4483 mtx_assert(bus->sim->mtx, MA_OWNED);
4484 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT,
4489 TAILQ_INIT(&target->ed_entries);
4491 target->target_id = target_id;
4492 target->refcount = 1;
4493 target->generation = 0;
4494 target->luns = NULL;
4495 timevalclear(&target->last_reset);
4497 * Hold a reference to our parent bus so it
4498 * will not go away before we do.
4504 /* Insertion sort into our bus's target list */
4505 cur_target = TAILQ_FIRST(&bus->et_entries);
4506 while (cur_target != NULL && cur_target->target_id < target_id)
4507 cur_target = TAILQ_NEXT(cur_target, links);
4508 if (cur_target != NULL) {
4509 TAILQ_INSERT_BEFORE(cur_target, target, links);
4511 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4518 xpt_release_target(struct cam_et *target)
4521 mtx_assert(target->bus->sim->mtx, MA_OWNED);
4522 if (--target->refcount > 0)
4524 KASSERT(TAILQ_EMPTY(&target->ed_entries),
4525 ("refcount is zero, but device list is not empty"));
4526 TAILQ_REMOVE(&target->bus->et_entries, target, links);
4527 target->bus->generation++;
4528 xpt_release_bus(target->bus);
4530 free(target->luns, M_CAMXPT);
4531 free(target, M_CAMXPT);
4534 static struct cam_ed *
4535 xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target,
4538 struct cam_ed *device;
4540 device = xpt_alloc_device(bus, target, lun_id);
4544 device->mintags = 1;
4545 device->maxtags = 1;
4546 bus->sim->max_ccbs += device->ccbq.devq_openings;
4551 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4553 struct cam_ed *cur_device, *device;
4554 struct cam_devq *devq;
4557 mtx_assert(target->bus->sim->mtx, MA_OWNED);
4558 /* Make space for us in the device queue on our bus */
4559 devq = bus->sim->devq;
4560 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
4561 if (status != CAM_REQ_CMP)
4564 device = (struct cam_ed *)malloc(sizeof(*device),
4565 M_CAMDEV, M_NOWAIT|M_ZERO);
4569 cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
4570 device->alloc_ccb_entry.device = device;
4571 cam_init_pinfo(&device->send_ccb_entry.pinfo);
4572 device->send_ccb_entry.device = device;
4573 device->target = target;
4574 device->lun_id = lun_id;
4575 device->sim = bus->sim;
4576 /* Initialize our queues */
4577 if (camq_init(&device->drvq, 0) != 0) {
4578 free(device, M_CAMDEV);
4581 if (cam_ccbq_init(&device->ccbq,
4582 bus->sim->max_dev_openings) != 0) {
4583 camq_fini(&device->drvq);
4584 free(device, M_CAMDEV);
4587 SLIST_INIT(&device->asyncs);
4588 SLIST_INIT(&device->periphs);
4589 device->generation = 0;
4590 device->owner = NULL;
4591 device->flags = CAM_DEV_UNCONFIGURED;
4592 device->tag_delay_count = 0;
4593 device->tag_saved_openings = 0;
4594 device->refcount = 1;
4595 callout_init_mtx(&device->callout, bus->sim->mtx, 0);
4597 cur_device = TAILQ_FIRST(&target->ed_entries);
4598 while (cur_device != NULL && cur_device->lun_id < lun_id)
4599 cur_device = TAILQ_NEXT(cur_device, links);
4600 if (cur_device != NULL)
4601 TAILQ_INSERT_BEFORE(cur_device, device, links);
4603 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4605 target->generation++;
4610 xpt_acquire_device(struct cam_ed *device)
4613 mtx_assert(device->sim->mtx, MA_OWNED);
4618 xpt_release_device(struct cam_ed *device)
4620 struct cam_devq *devq;
4622 mtx_assert(device->sim->mtx, MA_OWNED);
4623 if (--device->refcount > 0)
4626 KASSERT(SLIST_EMPTY(&device->periphs),
4627 ("refcount is zero, but periphs list is not empty"));
4628 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
4629 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
4630 panic("Removing device while still queued for ccbs");
4632 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
4633 callout_stop(&device->callout);
4635 TAILQ_REMOVE(&device->target->ed_entries, device,links);
4636 device->target->generation++;
4637 device->target->bus->sim->max_ccbs -= device->ccbq.devq_openings;
4638 /* Release our slot in the devq */
4639 devq = device->target->bus->sim->devq;
4640 cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
4641 camq_fini(&device->drvq);
4642 cam_ccbq_fini(&device->ccbq);
4644 * Free allocated memory. free(9) does nothing if the
4645 * supplied pointer is NULL, so it is safe to call without
4648 free(device->supported_vpds, M_CAMXPT);
4649 free(device->device_id, M_CAMXPT);
4650 free(device->physpath, M_CAMXPT);
4651 free(device->rcap_buf, M_CAMXPT);
4652 free(device->serial_num, M_CAMXPT);
4654 xpt_release_target(device->target);
4655 free(device, M_CAMDEV);
4659 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
4667 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
4668 result = cam_ccbq_resize(&dev->ccbq, newopenings);
4669 if (result == CAM_REQ_CMP && (diff < 0)) {
4670 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
4672 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
4673 || (dev->inq_flags & SID_CmdQue) != 0)
4674 dev->tag_saved_openings = newopenings;
4675 /* Adjust the global limit */
4676 dev->sim->max_ccbs += diff;
4680 static struct cam_eb *
4681 xpt_find_bus(path_id_t path_id)
4686 for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4688 bus = TAILQ_NEXT(bus, links)) {
4689 if (bus->path_id == path_id) {
4698 static struct cam_et *
4699 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
4701 struct cam_et *target;
4703 mtx_assert(bus->sim->mtx, MA_OWNED);
4704 for (target = TAILQ_FIRST(&bus->et_entries);
4706 target = TAILQ_NEXT(target, links)) {
4707 if (target->target_id == target_id) {
4715 static struct cam_ed *
4716 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
4718 struct cam_ed *device;
4720 mtx_assert(target->bus->sim->mtx, MA_OWNED);
4721 for (device = TAILQ_FIRST(&target->ed_entries);
4723 device = TAILQ_NEXT(device, links)) {
4724 if (device->lun_id == lun_id) {
4733 xpt_start_tags(struct cam_path *path)
4735 struct ccb_relsim crs;
4736 struct cam_ed *device;
4737 struct cam_sim *sim;
4740 device = path->device;
4741 sim = path->bus->sim;
4742 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4743 xpt_freeze_devq(path, /*count*/1);
4744 device->inq_flags |= SID_CmdQue;
4745 if (device->tag_saved_openings != 0)
4746 newopenings = device->tag_saved_openings;
4748 newopenings = min(device->maxtags,
4749 sim->max_tagged_dev_openings);
4750 xpt_dev_ccbq_resize(path, newopenings);
4751 xpt_async(AC_GETDEV_CHANGED, path, NULL);
4752 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4753 crs.ccb_h.func_code = XPT_REL_SIMQ;
4754 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4756 = crs.release_timeout
4759 xpt_action((union ccb *)&crs);
4763 xpt_stop_tags(struct cam_path *path)
4765 struct ccb_relsim crs;
4766 struct cam_ed *device;
4767 struct cam_sim *sim;
4769 device = path->device;
4770 sim = path->bus->sim;
4771 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4772 device->tag_delay_count = 0;
4773 xpt_freeze_devq(path, /*count*/1);
4774 device->inq_flags &= ~SID_CmdQue;
4775 xpt_dev_ccbq_resize(path, sim->max_dev_openings);
4776 xpt_async(AC_GETDEV_CHANGED, path, NULL);
4777 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4778 crs.ccb_h.func_code = XPT_REL_SIMQ;
4779 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4781 = crs.release_timeout
4784 xpt_action((union ccb *)&crs);
4788 xpt_boot_delay(void *arg)
4795 xpt_config(void *arg)
4798 * Now that interrupts are enabled, go find our devices
4801 /* Setup debugging path */
4802 if (cam_dflags != CAM_DEBUG_NONE) {
4803 if (xpt_create_path_unlocked(&cam_dpath, xpt_periph,
4804 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
4805 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
4806 printf("xpt_config: xpt_create_path() failed for debug"
4807 " target %d:%d:%d, debugging disabled\n",
4808 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
4809 cam_dflags = CAM_DEBUG_NONE;
4814 periphdriver_init(1);
4816 callout_init(&xsoftc.boot_callout, 1);
4817 callout_reset(&xsoftc.boot_callout, hz * xsoftc.boot_delay / 1000,
4818 xpt_boot_delay, NULL);
4819 /* Fire up rescan thread. */
4820 if (kproc_create(xpt_scanner_thread, NULL, NULL, 0, 0, "xpt_thrd")) {
4821 printf("xpt_config: failed to create rescan thread.\n");
4829 xsoftc.buses_to_config++;
4834 xpt_release_boot(void)
4837 xsoftc.buses_to_config--;
4838 if (xsoftc.buses_to_config == 0 && xsoftc.buses_config_done == 0) {
4839 struct xpt_task *task;
4841 xsoftc.buses_config_done = 1;
4843 /* Call manually because we don't have any busses */
4844 task = malloc(sizeof(struct xpt_task), M_CAMXPT, M_NOWAIT);
4846 TASK_INIT(&task->task, 0, xpt_finishconfig_task, task);
4847 taskqueue_enqueue(taskqueue_thread, &task->task);
4854 * If the given device only has one peripheral attached to it, and if that
4855 * peripheral is the passthrough driver, announce it. This insures that the
4856 * user sees some sort of announcement for every peripheral in their system.
4859 xptpassannouncefunc(struct cam_ed *device, void *arg)
4861 struct cam_periph *periph;
4864 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
4865 periph = SLIST_NEXT(periph, periph_links), i++);
4867 periph = SLIST_FIRST(&device->periphs);
4869 && (strncmp(periph->periph_name, "pass", 4) == 0))
4870 xpt_announce_periph(periph, NULL);
4876 xpt_finishconfig_task(void *context, int pending)
4879 periphdriver_init(2);
4881 * Check for devices with no "standard" peripheral driver
4882 * attached. For any devices like that, announce the
4883 * passthrough driver so the user will see something.
4886 xpt_for_all_devices(xptpassannouncefunc, NULL);
4888 /* Release our hook so that the boot can continue. */
4889 config_intrhook_disestablish(xsoftc.xpt_config_hook);
4890 free(xsoftc.xpt_config_hook, M_CAMXPT);
4891 xsoftc.xpt_config_hook = NULL;
4893 free(context, M_CAMXPT);
4897 xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
4898 struct cam_path *path)
4900 struct ccb_setasync csa;
4905 mtx_lock(&xsoftc.xpt_lock);
4906 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
4907 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4908 if (status != CAM_REQ_CMP) {
4909 mtx_unlock(&xsoftc.xpt_lock);
4915 xpt_setup_ccb(&csa.ccb_h, path, CAM_PRIORITY_NORMAL);
4916 csa.ccb_h.func_code = XPT_SASYNC_CB;
4917 csa.event_enable = event;
4918 csa.callback = cbfunc;
4919 csa.callback_arg = cbarg;
4920 xpt_action((union ccb *)&csa);
4921 status = csa.ccb_h.status;
4924 xpt_free_path(path);
4925 mtx_unlock(&xsoftc.xpt_lock);
4928 if ((status == CAM_REQ_CMP) &&
4929 (csa.event_enable & AC_FOUND_DEVICE)) {
4931 * Get this peripheral up to date with all
4932 * the currently existing devices.
4934 xpt_for_all_devices(xptsetasyncfunc, &csa);
4936 if ((status == CAM_REQ_CMP) &&
4937 (csa.event_enable & AC_PATH_REGISTERED)) {
4939 * Get this peripheral up to date with all
4940 * the currently existing busses.
4942 xpt_for_all_busses(xptsetasyncbusfunc, &csa);
4949 xptaction(struct cam_sim *sim, union ccb *work_ccb)
4951 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
4953 switch (work_ccb->ccb_h.func_code) {
4954 /* Common cases first */
4955 case XPT_PATH_INQ: /* Path routing inquiry */
4957 struct ccb_pathinq *cpi;
4959 cpi = &work_ccb->cpi;
4960 cpi->version_num = 1; /* XXX??? */
4961 cpi->hba_inquiry = 0;
4962 cpi->target_sprt = 0;
4964 cpi->hba_eng_cnt = 0;
4965 cpi->max_target = 0;
4967 cpi->initiator_id = 0;
4968 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
4969 strncpy(cpi->hba_vid, "", HBA_IDLEN);
4970 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
4971 cpi->unit_number = sim->unit_number;
4972 cpi->bus_id = sim->bus_id;
4973 cpi->base_transfer_speed = 0;
4974 cpi->protocol = PROTO_UNSPECIFIED;
4975 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
4976 cpi->transport = XPORT_UNSPECIFIED;
4977 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
4978 cpi->ccb_h.status = CAM_REQ_CMP;
4983 work_ccb->ccb_h.status = CAM_REQ_INVALID;
4990 * The xpt as a "controller" has no interrupt sources, so polling
4994 xptpoll(struct cam_sim *sim)
4999 xpt_lock_buses(void)
5001 mtx_lock(&xsoftc.xpt_topo_lock);
5005 xpt_unlock_buses(void)
5007 mtx_unlock(&xsoftc.xpt_topo_lock);
5014 struct cam_sim *sim;
5016 mtx_lock(&cam_simq_lock);
5018 while (!TAILQ_EMPTY(&cam_simq)) {
5019 TAILQ_CONCAT(&queue, &cam_simq, links);
5020 mtx_unlock(&cam_simq_lock);
5022 while ((sim = TAILQ_FIRST(&queue)) != NULL) {
5023 TAILQ_REMOVE(&queue, sim, links);
5025 camisr_runqueue(&sim->sim_doneq);
5026 sim->flags &= ~CAM_SIM_ON_DONEQ;
5027 CAM_SIM_UNLOCK(sim);
5029 mtx_lock(&cam_simq_lock);
5031 mtx_unlock(&cam_simq_lock);
5035 camisr_runqueue(void *V_queue)
5037 cam_isrq_t *queue = V_queue;
5038 struct ccb_hdr *ccb_h;
5040 while ((ccb_h = TAILQ_FIRST(queue)) != NULL) {
5043 TAILQ_REMOVE(queue, ccb_h, sim_links.tqe);
5044 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
5046 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
5051 if (ccb_h->flags & CAM_HIGH_POWER) {
5052 struct highpowerlist *hphead;
5053 union ccb *send_ccb;
5055 mtx_lock(&xsoftc.xpt_lock);
5056 hphead = &xsoftc.highpowerq;
5058 send_ccb = (union ccb *)STAILQ_FIRST(hphead);
5061 * Increment the count since this command is done.
5063 xsoftc.num_highpower++;
5066 * Any high powered commands queued up?
5068 if (send_ccb != NULL) {
5070 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
5071 mtx_unlock(&xsoftc.xpt_lock);
5073 xpt_release_devq(send_ccb->ccb_h.path,
5074 /*count*/1, /*runqueue*/TRUE);
5076 mtx_unlock(&xsoftc.xpt_lock);
5079 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
5082 dev = ccb_h->path->device;
5084 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
5085 ccb_h->path->bus->sim->devq->send_active--;
5086 ccb_h->path->bus->sim->devq->send_openings++;
5089 if (((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
5090 && (dev->ccbq.dev_active == 0))) {
5091 dev->flags &= ~CAM_DEV_REL_ON_QUEUE_EMPTY;
5092 xpt_release_devq(ccb_h->path, /*count*/1,
5093 /*run_queue*/FALSE);
5096 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
5097 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)) {
5098 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
5099 xpt_release_devq(ccb_h->path, /*count*/1,
5100 /*run_queue*/FALSE);
5103 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5104 && (--dev->tag_delay_count == 0))
5105 xpt_start_tags(ccb_h->path);
5106 if (!device_is_send_queued(dev)) {
5107 (void)xpt_schedule_dev_sendq(ccb_h->path->bus,
5112 if (ccb_h->status & CAM_RELEASE_SIMQ) {
5113 xpt_release_simq(ccb_h->path->bus->sim,
5115 ccb_h->status &= ~CAM_RELEASE_SIMQ;
5119 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
5120 && (ccb_h->status & CAM_DEV_QFRZN)) {
5121 xpt_release_devq(ccb_h->path, /*count*/1,
5123 ccb_h->status &= ~CAM_DEV_QFRZN;
5125 xpt_run_dev_sendq(ccb_h->path->bus);
5128 /* Call the peripheral driver's callback */
5129 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);