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>
43 #include <sys/interrupt.h>
45 #include <sys/taskqueue.h>
48 #include <sys/mutex.h>
49 #include <sys/sysctl.h>
50 #include <sys/kthread.h>
53 #include <pc98/pc98/pc98_machdep.h> /* geometry translation */
57 #include <cam/cam_ccb.h>
58 #include <cam/cam_periph.h>
59 #include <cam/cam_queue.h>
60 #include <cam/cam_sim.h>
61 #include <cam/cam_xpt.h>
62 #include <cam/cam_xpt_sim.h>
63 #include <cam/cam_xpt_periph.h>
64 #include <cam/cam_xpt_internal.h>
65 #include <cam/cam_debug.h>
67 #include <cam/scsi/scsi_all.h>
68 #include <cam/scsi/scsi_message.h>
69 #include <cam/scsi/scsi_pass.h>
70 #include <machine/stdarg.h> /* for xpt_print below */
74 * This is the maximum number of high powered commands (e.g. start unit)
75 * that can be outstanding at a particular time.
77 #ifndef CAM_MAX_HIGHPOWER
78 #define CAM_MAX_HIGHPOWER 4
81 /* Datastructures internal to the xpt layer */
82 MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
84 /* Object for defering XPT actions to a taskqueue */
97 u_int32_t xpt_generation;
99 /* number of high powered commands that can go through right now */
100 STAILQ_HEAD(highpowerlist, ccb_hdr) highpowerq;
103 /* queue for handling async rescan requests. */
104 TAILQ_HEAD(, ccb_hdr) ccb_scanq;
106 int buses_config_done;
108 /* Registered busses */
109 TAILQ_HEAD(,cam_eb) xpt_busses;
110 u_int bus_generation;
112 struct intr_config_hook *xpt_config_hook;
115 struct callout boot_callout;
117 struct mtx xpt_topo_lock;
123 DM_RET_FLAG_MASK = 0x0f,
126 DM_RET_DESCEND = 0x20,
128 DM_RET_ACTION_MASK = 0xf0
136 } xpt_traverse_depth;
138 struct xpt_traverse_config {
139 xpt_traverse_depth depth;
144 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
145 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
146 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
147 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
148 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
150 /* Transport layer configuration information */
151 static struct xpt_softc xsoftc;
153 TUNABLE_INT("kern.cam.boot_delay", &xsoftc.boot_delay);
154 SYSCTL_INT(_kern_cam, OID_AUTO, boot_delay, CTLFLAG_RDTUN,
155 &xsoftc.boot_delay, 0, "Bus registration wait time");
157 /* Queues for our software interrupt handler */
158 typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
159 typedef TAILQ_HEAD(cam_simq, cam_sim) cam_simq_t;
160 static cam_simq_t cam_simq;
161 static struct mtx cam_simq_lock;
163 /* Pointers to software interrupt handlers */
164 static void *cambio_ih;
166 struct cam_periph *xpt_periph;
168 static periph_init_t xpt_periph_init;
170 static struct periph_driver xpt_driver =
172 xpt_periph_init, "xpt",
173 TAILQ_HEAD_INITIALIZER(xpt_driver.units), /* generation */ 0,
177 PERIPHDRIVER_DECLARE(xpt, xpt_driver);
179 static d_open_t xptopen;
180 static d_close_t xptclose;
181 static d_ioctl_t xptioctl;
183 static struct cdevsw xpt_cdevsw = {
184 .d_version = D_VERSION,
192 /* Storage for debugging datastructures */
194 struct cam_path *cam_dpath;
195 u_int32_t cam_dflags;
196 u_int32_t cam_debug_delay;
199 /* Our boot-time initialization hook */
200 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
202 static moduledata_t cam_moduledata = {
204 cam_module_event_handler,
208 static int xpt_init(void *);
210 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
211 MODULE_VERSION(cam, 1);
214 static void xpt_async_bcast(struct async_list *async_head,
215 u_int32_t async_code,
216 struct cam_path *path,
218 static path_id_t xptnextfreepathid(void);
219 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
220 static union ccb *xpt_get_ccb(struct cam_ed *device);
221 static void xpt_run_dev_allocq(struct cam_eb *bus);
222 static void xpt_run_dev_sendq(struct cam_eb *bus);
223 static timeout_t xpt_release_devq_timeout;
224 static void xpt_release_simq_timeout(void *arg) __unused;
225 static void xpt_release_bus(struct cam_eb *bus);
226 static void xpt_release_devq_device(struct cam_ed *dev, cam_rl rl,
227 u_int count, int run_queue);
228 static struct cam_et*
229 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
230 static void xpt_release_target(struct cam_et *target);
231 static struct cam_eb*
232 xpt_find_bus(path_id_t path_id);
233 static struct cam_et*
234 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
235 static struct cam_ed*
236 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
237 static void xpt_config(void *arg);
238 static xpt_devicefunc_t xptpassannouncefunc;
239 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
240 static void xptpoll(struct cam_sim *sim);
241 static void camisr(void *);
242 static void camisr_runqueue(void *);
243 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
244 u_int num_patterns, struct cam_eb *bus);
245 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
247 struct cam_ed *device);
248 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
250 struct cam_periph *periph);
251 static xpt_busfunc_t xptedtbusfunc;
252 static xpt_targetfunc_t xptedttargetfunc;
253 static xpt_devicefunc_t xptedtdevicefunc;
254 static xpt_periphfunc_t xptedtperiphfunc;
255 static xpt_pdrvfunc_t xptplistpdrvfunc;
256 static xpt_periphfunc_t xptplistperiphfunc;
257 static int xptedtmatch(struct ccb_dev_match *cdm);
258 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
259 static int xptbustraverse(struct cam_eb *start_bus,
260 xpt_busfunc_t *tr_func, void *arg);
261 static int xpttargettraverse(struct cam_eb *bus,
262 struct cam_et *start_target,
263 xpt_targetfunc_t *tr_func, void *arg);
264 static int xptdevicetraverse(struct cam_et *target,
265 struct cam_ed *start_device,
266 xpt_devicefunc_t *tr_func, void *arg);
267 static int xptperiphtraverse(struct cam_ed *device,
268 struct cam_periph *start_periph,
269 xpt_periphfunc_t *tr_func, void *arg);
270 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
271 xpt_pdrvfunc_t *tr_func, void *arg);
272 static int xptpdperiphtraverse(struct periph_driver **pdrv,
273 struct cam_periph *start_periph,
274 xpt_periphfunc_t *tr_func,
276 static xpt_busfunc_t xptdefbusfunc;
277 static xpt_targetfunc_t xptdeftargetfunc;
278 static xpt_devicefunc_t xptdefdevicefunc;
279 static xpt_periphfunc_t xptdefperiphfunc;
280 static void xpt_finishconfig_task(void *context, int pending);
281 static int xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg);
282 static int xpt_for_all_devices(xpt_devicefunc_t *tr_func,
284 static void xpt_dev_async_default(u_int32_t async_code,
286 struct cam_et *target,
287 struct cam_ed *device,
289 static struct cam_ed * xpt_alloc_device_default(struct cam_eb *bus,
290 struct cam_et *target,
292 static xpt_devicefunc_t xptsetasyncfunc;
293 static xpt_busfunc_t xptsetasyncbusfunc;
294 static cam_status xptregister(struct cam_periph *periph,
296 static __inline int periph_is_queued(struct cam_periph *periph);
297 static __inline int device_is_alloc_queued(struct cam_ed *device);
298 static __inline int device_is_send_queued(struct cam_ed *device);
301 xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev)
305 if ((dev->drvq.entries > 0) &&
306 (dev->ccbq.devq_openings > 0) &&
307 (cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL(
308 CAMQ_GET_PRIO(&dev->drvq))) == 0)) {
310 * The priority of a device waiting for CCB resources
311 * is that of the the highest priority peripheral driver
314 retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue,
315 &dev->alloc_ccb_entry.pinfo,
316 CAMQ_GET_PRIO(&dev->drvq));
325 xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev)
329 if ((dev->ccbq.queue.entries > 0) &&
330 (dev->ccbq.dev_openings > 0) &&
331 (cam_ccbq_frozen_top(&dev->ccbq) == 0)) {
333 * The priority of a device waiting for controller
334 * resources is that of the the highest priority CCB
338 xpt_schedule_dev(&bus->sim->devq->send_queue,
339 &dev->send_ccb_entry.pinfo,
340 CAMQ_GET_PRIO(&dev->ccbq.queue));
348 periph_is_queued(struct cam_periph *periph)
350 return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
354 device_is_alloc_queued(struct cam_ed *device)
356 return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
360 device_is_send_queued(struct cam_ed *device)
362 return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
368 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
372 xptdone(struct cam_periph *periph, union ccb *done_ccb)
374 /* Caller will release the CCB */
375 wakeup(&done_ccb->ccb_h.cbfcnp);
379 xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
383 * Only allow read-write access.
385 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
389 * We don't allow nonblocking access.
391 if ((flags & O_NONBLOCK) != 0) {
392 printf("%s: can't do nonblocking access\n", devtoname(dev));
396 /* Mark ourselves open */
397 mtx_lock(&xsoftc.xpt_lock);
398 xsoftc.flags |= XPT_FLAG_OPEN;
399 mtx_unlock(&xsoftc.xpt_lock);
405 xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
408 /* Mark ourselves closed */
409 mtx_lock(&xsoftc.xpt_lock);
410 xsoftc.flags &= ~XPT_FLAG_OPEN;
411 mtx_unlock(&xsoftc.xpt_lock);
417 * Don't automatically grab the xpt softc lock here even though this is going
418 * through the xpt device. The xpt device is really just a back door for
419 * accessing other devices and SIMs, so the right thing to do is to grab
420 * the appropriate SIM lock once the bus/SIM is located.
423 xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
431 * For the transport layer CAMIOCOMMAND ioctl, we really only want
432 * to accept CCB types that don't quite make sense to send through a
433 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
441 inccb = (union ccb *)addr;
443 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)) {
462 ccb = xpt_alloc_ccb();
464 CAM_SIM_LOCK(bus->sim);
467 * Create a path using the bus, target, and lun the
470 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
471 inccb->ccb_h.path_id,
472 inccb->ccb_h.target_id,
473 inccb->ccb_h.target_lun) !=
476 CAM_SIM_UNLOCK(bus->sim);
480 /* Ensure all of our fields are correct */
481 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
482 inccb->ccb_h.pinfo.priority);
483 xpt_merge_ccb(ccb, inccb);
484 ccb->ccb_h.cbfcnp = xptdone;
485 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
486 bcopy(ccb, inccb, sizeof(union ccb));
487 xpt_free_path(ccb->ccb_h.path);
489 CAM_SIM_UNLOCK(bus->sim);
496 * This is an immediate CCB, so it's okay to
497 * allocate it on the stack.
500 CAM_SIM_LOCK(bus->sim);
503 * Create a path using the bus, target, and lun the
506 if (xpt_create_path(&ccb.ccb_h.path, xpt_periph,
507 inccb->ccb_h.path_id,
508 inccb->ccb_h.target_id,
509 inccb->ccb_h.target_lun) !=
512 CAM_SIM_UNLOCK(bus->sim);
515 /* Ensure all of our fields are correct */
516 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
517 inccb->ccb_h.pinfo.priority);
518 xpt_merge_ccb(&ccb, inccb);
519 ccb.ccb_h.cbfcnp = xptdone;
521 CAM_SIM_UNLOCK(bus->sim);
522 bcopy(&ccb, inccb, sizeof(union ccb));
523 xpt_free_path(ccb.ccb_h.path);
527 case XPT_DEV_MATCH: {
528 struct cam_periph_map_info mapinfo;
529 struct cam_path *old_path;
532 * We can't deal with physical addresses for this
533 * type of transaction.
535 if (inccb->ccb_h.flags & CAM_DATA_PHYS) {
541 * Save this in case the caller had it set to
542 * something in particular.
544 old_path = inccb->ccb_h.path;
547 * We really don't need a path for the matching
548 * code. The path is needed because of the
549 * debugging statements in xpt_action(). They
550 * assume that the CCB has a valid path.
552 inccb->ccb_h.path = xpt_periph->path;
554 bzero(&mapinfo, sizeof(mapinfo));
557 * Map the pattern and match buffers into kernel
558 * virtual address space.
560 error = cam_periph_mapmem(inccb, &mapinfo);
563 inccb->ccb_h.path = old_path;
568 * This is an immediate CCB, we can send it on directly.
573 * Map the buffers back into user space.
575 cam_periph_unmapmem(inccb, &mapinfo);
577 inccb->ccb_h.path = old_path;
586 xpt_release_bus(bus);
590 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
591 * with the periphal driver name and unit name filled in. The other
592 * fields don't really matter as input. The passthrough driver name
593 * ("pass"), and unit number are passed back in the ccb. The current
594 * device generation number, and the index into the device peripheral
595 * driver list, and the status are also passed back. Note that
596 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
597 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
598 * (or rather should be) impossible for the device peripheral driver
599 * list to change since we look at the whole thing in one pass, and
600 * we do it with lock protection.
603 case CAMGETPASSTHRU: {
605 struct cam_periph *periph;
606 struct periph_driver **p_drv;
609 u_int cur_generation;
610 int base_periph_found;
613 ccb = (union ccb *)addr;
614 unit = ccb->cgdl.unit_number;
615 name = ccb->cgdl.periph_name;
617 * Every 100 devices, we want to drop our lock protection to
618 * give the software interrupt handler a chance to run.
619 * Most systems won't run into this check, but this should
620 * avoid starvation in the software interrupt handler in
625 ccb = (union ccb *)addr;
627 base_periph_found = 0;
630 * Sanity check -- make sure we don't get a null peripheral
633 if (*ccb->cgdl.periph_name == '\0') {
638 /* Keep the list from changing while we traverse it */
639 mtx_lock(&xsoftc.xpt_topo_lock);
641 cur_generation = xsoftc.xpt_generation;
643 /* first find our driver in the list of drivers */
644 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
645 if (strcmp((*p_drv)->driver_name, name) == 0)
648 if (*p_drv == NULL) {
649 mtx_unlock(&xsoftc.xpt_topo_lock);
650 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
651 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
652 *ccb->cgdl.periph_name = '\0';
653 ccb->cgdl.unit_number = 0;
659 * Run through every peripheral instance of this driver
660 * and check to see whether it matches the unit passed
661 * in by the user. If it does, get out of the loops and
662 * find the passthrough driver associated with that
665 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
666 periph = TAILQ_NEXT(periph, unit_links)) {
668 if (periph->unit_number == unit) {
670 } else if (--splbreaknum == 0) {
671 mtx_unlock(&xsoftc.xpt_topo_lock);
672 mtx_lock(&xsoftc.xpt_topo_lock);
674 if (cur_generation != xsoftc.xpt_generation)
679 * If we found the peripheral driver that the user passed
680 * in, go through all of the peripheral drivers for that
681 * particular device and look for a passthrough driver.
683 if (periph != NULL) {
684 struct cam_ed *device;
687 base_periph_found = 1;
688 device = periph->path->device;
689 for (i = 0, periph = SLIST_FIRST(&device->periphs);
691 periph = SLIST_NEXT(periph, periph_links), i++) {
693 * Check to see whether we have a
694 * passthrough device or not.
696 if (strcmp(periph->periph_name, "pass") == 0) {
698 * Fill in the getdevlist fields.
700 strcpy(ccb->cgdl.periph_name,
701 periph->periph_name);
702 ccb->cgdl.unit_number =
704 if (SLIST_NEXT(periph, periph_links))
706 CAM_GDEVLIST_MORE_DEVS;
709 CAM_GDEVLIST_LAST_DEVICE;
710 ccb->cgdl.generation =
714 * Fill in some CCB header fields
715 * that the user may want.
718 periph->path->bus->path_id;
719 ccb->ccb_h.target_id =
720 periph->path->target->target_id;
721 ccb->ccb_h.target_lun =
722 periph->path->device->lun_id;
723 ccb->ccb_h.status = CAM_REQ_CMP;
730 * If the periph is null here, one of two things has
731 * happened. The first possibility is that we couldn't
732 * find the unit number of the particular peripheral driver
733 * that the user is asking about. e.g. the user asks for
734 * the passthrough driver for "da11". We find the list of
735 * "da" peripherals all right, but there is no unit 11.
736 * The other possibility is that we went through the list
737 * of peripheral drivers attached to the device structure,
738 * but didn't find one with the name "pass". Either way,
739 * we return ENOENT, since we couldn't find something.
741 if (periph == NULL) {
742 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
743 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
744 *ccb->cgdl.periph_name = '\0';
745 ccb->cgdl.unit_number = 0;
748 * It is unfortunate that this is even necessary,
749 * but there are many, many clueless users out there.
750 * If this is true, the user is looking for the
751 * passthrough driver, but doesn't have one in his
754 if (base_periph_found == 1) {
755 printf("xptioctl: pass driver is not in the "
757 printf("xptioctl: put \"device pass\" in "
758 "your kernel config file\n");
761 mtx_unlock(&xsoftc.xpt_topo_lock);
773 cam_module_event_handler(module_t mod, int what, void *arg)
779 if ((error = xpt_init(NULL)) != 0)
792 xpt_rescan_done(struct cam_periph *periph, union ccb *done_ccb)
795 if (done_ccb->ccb_h.ppriv_ptr1 == NULL) {
796 xpt_free_path(done_ccb->ccb_h.path);
797 xpt_free_ccb(done_ccb);
799 done_ccb->ccb_h.cbfcnp = done_ccb->ccb_h.ppriv_ptr1;
800 (*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
805 /* thread to handle bus rescans */
807 xpt_scanner_thread(void *dummy)
814 if (TAILQ_EMPTY(&xsoftc.ccb_scanq))
815 msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO,
817 if ((ccb = (union ccb *)TAILQ_FIRST(&xsoftc.ccb_scanq)) != NULL) {
818 TAILQ_REMOVE(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
821 sim = ccb->ccb_h.path->bus->sim;
832 xpt_rescan(union ccb *ccb)
836 /* Prepare request */
837 if (ccb->ccb_h.path->target->target_id == CAM_TARGET_WILDCARD ||
838 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
839 ccb->ccb_h.func_code = XPT_SCAN_BUS;
841 ccb->ccb_h.func_code = XPT_SCAN_LUN;
842 ccb->ccb_h.ppriv_ptr1 = ccb->ccb_h.cbfcnp;
843 ccb->ccb_h.cbfcnp = xpt_rescan_done;
844 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, CAM_PRIORITY_XPT);
845 /* Don't make duplicate entries for the same paths. */
847 if (ccb->ccb_h.ppriv_ptr1 == NULL) {
848 TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) {
849 if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) {
850 wakeup(&xsoftc.ccb_scanq);
852 xpt_print(ccb->ccb_h.path, "rescan already queued\n");
853 xpt_free_path(ccb->ccb_h.path);
859 TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
860 xsoftc.buses_to_config++;
861 wakeup(&xsoftc.ccb_scanq);
865 /* Functions accessed by the peripheral drivers */
867 xpt_init(void *dummy)
869 struct cam_sim *xpt_sim;
870 struct cam_path *path;
871 struct cam_devq *devq;
874 TAILQ_INIT(&xsoftc.xpt_busses);
875 TAILQ_INIT(&cam_simq);
876 TAILQ_INIT(&xsoftc.ccb_scanq);
877 STAILQ_INIT(&xsoftc.highpowerq);
878 xsoftc.num_highpower = CAM_MAX_HIGHPOWER;
880 mtx_init(&cam_simq_lock, "CAM SIMQ lock", NULL, MTX_DEF);
881 mtx_init(&xsoftc.xpt_lock, "XPT lock", NULL, MTX_DEF);
882 mtx_init(&xsoftc.xpt_topo_lock, "XPT topology lock", NULL, MTX_DEF);
885 * The xpt layer is, itself, the equivelent of a SIM.
886 * Allow 16 ccbs in the ccb pool for it. This should
887 * give decent parallelism when we probe busses and
888 * perform other XPT functions.
890 devq = cam_simq_alloc(16);
891 xpt_sim = cam_sim_alloc(xptaction,
896 /*mtx*/&xsoftc.xpt_lock,
897 /*max_dev_transactions*/0,
898 /*max_tagged_dev_transactions*/0,
903 mtx_lock(&xsoftc.xpt_lock);
904 if ((status = xpt_bus_register(xpt_sim, NULL, 0)) != CAM_SUCCESS) {
905 mtx_unlock(&xsoftc.xpt_lock);
906 printf("xpt_init: xpt_bus_register failed with status %#x,"
907 " failing attach\n", status);
912 * Looking at the XPT from the SIM layer, the XPT is
913 * the equivelent of a peripheral driver. Allocate
914 * a peripheral driver entry for us.
916 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
918 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
919 mtx_unlock(&xsoftc.xpt_lock);
920 printf("xpt_init: xpt_create_path failed with status %#x,"
921 " failing attach\n", status);
925 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
926 path, NULL, 0, xpt_sim);
928 mtx_unlock(&xsoftc.xpt_lock);
929 /* Install our software interrupt handlers */
930 swi_add(NULL, "cambio", camisr, NULL, SWI_CAMBIO, INTR_MPSAFE, &cambio_ih);
932 * Register a callback for when interrupts are enabled.
934 xsoftc.xpt_config_hook =
935 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
936 M_CAMXPT, M_NOWAIT | M_ZERO);
937 if (xsoftc.xpt_config_hook == NULL) {
938 printf("xpt_init: Cannot malloc config hook "
939 "- failing attach\n");
942 xsoftc.xpt_config_hook->ich_func = xpt_config;
943 if (config_intrhook_establish(xsoftc.xpt_config_hook) != 0) {
944 free (xsoftc.xpt_config_hook, M_CAMXPT);
945 printf("xpt_init: config_intrhook_establish failed "
946 "- failing attach\n");
953 xptregister(struct cam_periph *periph, void *arg)
955 struct cam_sim *xpt_sim;
957 if (periph == NULL) {
958 printf("xptregister: periph was NULL!!\n");
959 return(CAM_REQ_CMP_ERR);
962 xpt_sim = (struct cam_sim *)arg;
963 xpt_sim->softc = periph;
965 periph->softc = NULL;
971 xpt_add_periph(struct cam_periph *periph)
973 struct cam_ed *device;
975 struct periph_list *periph_head;
977 mtx_assert(periph->sim->mtx, MA_OWNED);
979 device = periph->path->device;
981 periph_head = &device->periphs;
983 status = CAM_REQ_CMP;
985 if (device != NULL) {
987 * Make room for this peripheral
988 * so it will fit in the queue
989 * when it's scheduled to run
991 status = camq_resize(&device->drvq,
992 device->drvq.array_size + 1);
994 device->generation++;
996 SLIST_INSERT_HEAD(periph_head, periph, periph_links);
999 mtx_lock(&xsoftc.xpt_topo_lock);
1000 xsoftc.xpt_generation++;
1001 mtx_unlock(&xsoftc.xpt_topo_lock);
1007 xpt_remove_periph(struct cam_periph *periph)
1009 struct cam_ed *device;
1011 mtx_assert(periph->sim->mtx, MA_OWNED);
1013 device = periph->path->device;
1015 if (device != NULL) {
1016 struct periph_list *periph_head;
1018 periph_head = &device->periphs;
1020 /* Release the slot for this peripheral */
1021 camq_resize(&device->drvq, device->drvq.array_size - 1);
1023 device->generation++;
1025 SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
1028 mtx_lock(&xsoftc.xpt_topo_lock);
1029 xsoftc.xpt_generation++;
1030 mtx_unlock(&xsoftc.xpt_topo_lock);
1035 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1037 struct cam_path *path = periph->path;
1039 mtx_assert(periph->sim->mtx, MA_OWNED);
1041 printf("%s%d at %s%d bus %d scbus%d target %d lun %d\n",
1042 periph->periph_name, periph->unit_number,
1043 path->bus->sim->sim_name,
1044 path->bus->sim->unit_number,
1045 path->bus->sim->bus_id,
1047 path->target->target_id,
1048 path->device->lun_id);
1049 printf("%s%d: ", periph->periph_name, periph->unit_number);
1050 if (path->device->protocol == PROTO_SCSI)
1051 scsi_print_inquiry(&path->device->inq_data);
1052 else if (path->device->protocol == PROTO_ATA ||
1053 path->device->protocol == PROTO_SATAPM)
1054 ata_print_ident(&path->device->ident_data);
1056 printf("Unknown protocol device\n");
1057 if (bootverbose && path->device->serial_num_len > 0) {
1058 /* Don't wrap the screen - print only the first 60 chars */
1059 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1060 periph->unit_number, path->device->serial_num);
1062 /* Announce transport details. */
1063 (*(path->bus->xport->announce))(periph);
1064 /* Announce command queueing. */
1065 if (path->device->inq_flags & SID_CmdQue
1066 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1067 printf("%s%d: Command Queueing enabled\n",
1068 periph->periph_name, periph->unit_number);
1070 /* Announce caller's details if they've passed in. */
1071 if (announce_string != NULL)
1072 printf("%s%d: %s\n", periph->periph_name,
1073 periph->unit_number, announce_string);
1076 static dev_match_ret
1077 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1080 dev_match_ret retval;
1083 retval = DM_RET_NONE;
1086 * If we aren't given something to match against, that's an error.
1089 return(DM_RET_ERROR);
1092 * If there are no match entries, then this bus matches no
1095 if ((patterns == NULL) || (num_patterns == 0))
1096 return(DM_RET_DESCEND | DM_RET_COPY);
1098 for (i = 0; i < num_patterns; i++) {
1099 struct bus_match_pattern *cur_pattern;
1102 * If the pattern in question isn't for a bus node, we
1103 * aren't interested. However, we do indicate to the
1104 * calling routine that we should continue descending the
1105 * tree, since the user wants to match against lower-level
1108 if (patterns[i].type != DEV_MATCH_BUS) {
1109 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1110 retval |= DM_RET_DESCEND;
1114 cur_pattern = &patterns[i].pattern.bus_pattern;
1117 * If they want to match any bus node, we give them any
1120 if (cur_pattern->flags == BUS_MATCH_ANY) {
1121 /* set the copy flag */
1122 retval |= DM_RET_COPY;
1125 * If we've already decided on an action, go ahead
1128 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1133 * Not sure why someone would do this...
1135 if (cur_pattern->flags == BUS_MATCH_NONE)
1138 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1139 && (cur_pattern->path_id != bus->path_id))
1142 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1143 && (cur_pattern->bus_id != bus->sim->bus_id))
1146 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1147 && (cur_pattern->unit_number != bus->sim->unit_number))
1150 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1151 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1156 * If we get to this point, the user definitely wants
1157 * information on this bus. So tell the caller to copy the
1160 retval |= DM_RET_COPY;
1163 * If the return action has been set to descend, then we
1164 * know that we've already seen a non-bus matching
1165 * expression, therefore we need to further descend the tree.
1166 * This won't change by continuing around the loop, so we
1167 * go ahead and return. If we haven't seen a non-bus
1168 * matching expression, we keep going around the loop until
1169 * we exhaust the matching expressions. We'll set the stop
1170 * flag once we fall out of the loop.
1172 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1177 * If the return action hasn't been set to descend yet, that means
1178 * we haven't seen anything other than bus matching patterns. So
1179 * tell the caller to stop descending the tree -- the user doesn't
1180 * want to match against lower level tree elements.
1182 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1183 retval |= DM_RET_STOP;
1188 static dev_match_ret
1189 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1190 struct cam_ed *device)
1192 dev_match_ret retval;
1195 retval = DM_RET_NONE;
1198 * If we aren't given something to match against, that's an error.
1201 return(DM_RET_ERROR);
1204 * If there are no match entries, then this device matches no
1207 if ((patterns == NULL) || (num_patterns == 0))
1208 return(DM_RET_DESCEND | DM_RET_COPY);
1210 for (i = 0; i < num_patterns; i++) {
1211 struct device_match_pattern *cur_pattern;
1214 * If the pattern in question isn't for a device node, we
1215 * aren't interested.
1217 if (patterns[i].type != DEV_MATCH_DEVICE) {
1218 if ((patterns[i].type == DEV_MATCH_PERIPH)
1219 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1220 retval |= DM_RET_DESCEND;
1224 cur_pattern = &patterns[i].pattern.device_pattern;
1227 * If they want to match any device node, we give them any
1230 if (cur_pattern->flags == DEV_MATCH_ANY) {
1231 /* set the copy flag */
1232 retval |= DM_RET_COPY;
1236 * If we've already decided on an action, go ahead
1239 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1244 * Not sure why someone would do this...
1246 if (cur_pattern->flags == DEV_MATCH_NONE)
1249 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1250 && (cur_pattern->path_id != device->target->bus->path_id))
1253 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1254 && (cur_pattern->target_id != device->target->target_id))
1257 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1258 && (cur_pattern->target_lun != device->lun_id))
1261 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1262 && (cam_quirkmatch((caddr_t)&device->inq_data,
1263 (caddr_t)&cur_pattern->inq_pat,
1264 1, sizeof(cur_pattern->inq_pat),
1265 scsi_static_inquiry_match) == NULL))
1269 * If we get to this point, the user definitely wants
1270 * information on this device. So tell the caller to copy
1273 retval |= DM_RET_COPY;
1276 * If the return action has been set to descend, then we
1277 * know that we've already seen a peripheral matching
1278 * expression, therefore we need to further descend the tree.
1279 * This won't change by continuing around the loop, so we
1280 * go ahead and return. If we haven't seen a peripheral
1281 * matching expression, we keep going around the loop until
1282 * we exhaust the matching expressions. We'll set the stop
1283 * flag once we fall out of the loop.
1285 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1290 * If the return action hasn't been set to descend yet, that means
1291 * we haven't seen any peripheral matching patterns. So tell the
1292 * caller to stop descending the tree -- the user doesn't want to
1293 * match against lower level tree elements.
1295 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1296 retval |= DM_RET_STOP;
1302 * Match a single peripheral against any number of match patterns.
1304 static dev_match_ret
1305 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1306 struct cam_periph *periph)
1308 dev_match_ret retval;
1312 * If we aren't given something to match against, that's an error.
1315 return(DM_RET_ERROR);
1318 * If there are no match entries, then this peripheral matches no
1321 if ((patterns == NULL) || (num_patterns == 0))
1322 return(DM_RET_STOP | DM_RET_COPY);
1325 * There aren't any nodes below a peripheral node, so there's no
1326 * reason to descend the tree any further.
1328 retval = DM_RET_STOP;
1330 for (i = 0; i < num_patterns; i++) {
1331 struct periph_match_pattern *cur_pattern;
1334 * If the pattern in question isn't for a peripheral, we
1335 * aren't interested.
1337 if (patterns[i].type != DEV_MATCH_PERIPH)
1340 cur_pattern = &patterns[i].pattern.periph_pattern;
1343 * If they want to match on anything, then we will do so.
1345 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1346 /* set the copy flag */
1347 retval |= DM_RET_COPY;
1350 * We've already set the return action to stop,
1351 * since there are no nodes below peripherals in
1358 * Not sure why someone would do this...
1360 if (cur_pattern->flags == PERIPH_MATCH_NONE)
1363 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1364 && (cur_pattern->path_id != periph->path->bus->path_id))
1368 * For the target and lun id's, we have to make sure the
1369 * target and lun pointers aren't NULL. The xpt peripheral
1370 * has a wildcard target and device.
1372 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1373 && ((periph->path->target == NULL)
1374 ||(cur_pattern->target_id != periph->path->target->target_id)))
1377 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1378 && ((periph->path->device == NULL)
1379 || (cur_pattern->target_lun != periph->path->device->lun_id)))
1382 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
1383 && (cur_pattern->unit_number != periph->unit_number))
1386 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
1387 && (strncmp(cur_pattern->periph_name, periph->periph_name,
1392 * If we get to this point, the user definitely wants
1393 * information on this peripheral. So tell the caller to
1394 * copy the data out.
1396 retval |= DM_RET_COPY;
1399 * The return action has already been set to stop, since
1400 * peripherals don't have any nodes below them in the EDT.
1406 * If we get to this point, the peripheral that was passed in
1407 * doesn't match any of the patterns.
1413 xptedtbusfunc(struct cam_eb *bus, void *arg)
1415 struct ccb_dev_match *cdm;
1416 dev_match_ret retval;
1418 cdm = (struct ccb_dev_match *)arg;
1421 * If our position is for something deeper in the tree, that means
1422 * that we've already seen this node. So, we keep going down.
1424 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1425 && (cdm->pos.cookie.bus == bus)
1426 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1427 && (cdm->pos.cookie.target != NULL))
1428 retval = DM_RET_DESCEND;
1430 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
1433 * If we got an error, bail out of the search.
1435 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1436 cdm->status = CAM_DEV_MATCH_ERROR;
1441 * If the copy flag is set, copy this bus out.
1443 if (retval & DM_RET_COPY) {
1446 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1447 sizeof(struct dev_match_result));
1450 * If we don't have enough space to put in another
1451 * match result, save our position and tell the
1452 * user there are more devices to check.
1454 if (spaceleft < sizeof(struct dev_match_result)) {
1455 bzero(&cdm->pos, sizeof(cdm->pos));
1456 cdm->pos.position_type =
1457 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
1459 cdm->pos.cookie.bus = bus;
1460 cdm->pos.generations[CAM_BUS_GENERATION]=
1461 xsoftc.bus_generation;
1462 cdm->status = CAM_DEV_MATCH_MORE;
1465 j = cdm->num_matches;
1467 cdm->matches[j].type = DEV_MATCH_BUS;
1468 cdm->matches[j].result.bus_result.path_id = bus->path_id;
1469 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
1470 cdm->matches[j].result.bus_result.unit_number =
1471 bus->sim->unit_number;
1472 strncpy(cdm->matches[j].result.bus_result.dev_name,
1473 bus->sim->sim_name, DEV_IDLEN);
1477 * If the user is only interested in busses, there's no
1478 * reason to descend to the next level in the tree.
1480 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1484 * If there is a target generation recorded, check it to
1485 * make sure the target list hasn't changed.
1487 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1488 && (bus == cdm->pos.cookie.bus)
1489 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1490 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
1491 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
1493 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1497 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1498 && (cdm->pos.cookie.bus == bus)
1499 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1500 && (cdm->pos.cookie.target != NULL))
1501 return(xpttargettraverse(bus,
1502 (struct cam_et *)cdm->pos.cookie.target,
1503 xptedttargetfunc, arg));
1505 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
1509 xptedttargetfunc(struct cam_et *target, void *arg)
1511 struct ccb_dev_match *cdm;
1513 cdm = (struct ccb_dev_match *)arg;
1516 * If there is a device list generation recorded, check it to
1517 * make sure the device list hasn't changed.
1519 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1520 && (cdm->pos.cookie.bus == target->bus)
1521 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1522 && (cdm->pos.cookie.target == target)
1523 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1524 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
1525 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
1526 target->generation)) {
1527 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1531 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1532 && (cdm->pos.cookie.bus == target->bus)
1533 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1534 && (cdm->pos.cookie.target == target)
1535 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1536 && (cdm->pos.cookie.device != NULL))
1537 return(xptdevicetraverse(target,
1538 (struct cam_ed *)cdm->pos.cookie.device,
1539 xptedtdevicefunc, arg));
1541 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
1545 xptedtdevicefunc(struct cam_ed *device, void *arg)
1548 struct ccb_dev_match *cdm;
1549 dev_match_ret retval;
1551 cdm = (struct ccb_dev_match *)arg;
1554 * If our position is for something deeper in the tree, that means
1555 * that we've already seen this node. So, we keep going down.
1557 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1558 && (cdm->pos.cookie.device == device)
1559 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1560 && (cdm->pos.cookie.periph != NULL))
1561 retval = DM_RET_DESCEND;
1563 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
1566 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1567 cdm->status = CAM_DEV_MATCH_ERROR;
1572 * If the copy flag is set, copy this device out.
1574 if (retval & DM_RET_COPY) {
1577 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1578 sizeof(struct dev_match_result));
1581 * If we don't have enough space to put in another
1582 * match result, save our position and tell the
1583 * user there are more devices to check.
1585 if (spaceleft < sizeof(struct dev_match_result)) {
1586 bzero(&cdm->pos, sizeof(cdm->pos));
1587 cdm->pos.position_type =
1588 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1589 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
1591 cdm->pos.cookie.bus = device->target->bus;
1592 cdm->pos.generations[CAM_BUS_GENERATION]=
1593 xsoftc.bus_generation;
1594 cdm->pos.cookie.target = device->target;
1595 cdm->pos.generations[CAM_TARGET_GENERATION] =
1596 device->target->bus->generation;
1597 cdm->pos.cookie.device = device;
1598 cdm->pos.generations[CAM_DEV_GENERATION] =
1599 device->target->generation;
1600 cdm->status = CAM_DEV_MATCH_MORE;
1603 j = cdm->num_matches;
1605 cdm->matches[j].type = DEV_MATCH_DEVICE;
1606 cdm->matches[j].result.device_result.path_id =
1607 device->target->bus->path_id;
1608 cdm->matches[j].result.device_result.target_id =
1609 device->target->target_id;
1610 cdm->matches[j].result.device_result.target_lun =
1612 cdm->matches[j].result.device_result.protocol =
1614 bcopy(&device->inq_data,
1615 &cdm->matches[j].result.device_result.inq_data,
1616 sizeof(struct scsi_inquiry_data));
1617 bcopy(&device->ident_data,
1618 &cdm->matches[j].result.device_result.ident_data,
1619 sizeof(struct ata_params));
1621 /* Let the user know whether this device is unconfigured */
1622 if (device->flags & CAM_DEV_UNCONFIGURED)
1623 cdm->matches[j].result.device_result.flags =
1624 DEV_RESULT_UNCONFIGURED;
1626 cdm->matches[j].result.device_result.flags =
1631 * If the user isn't interested in peripherals, don't descend
1632 * the tree any further.
1634 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1638 * If there is a peripheral list generation recorded, make sure
1639 * it hasn't changed.
1641 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1642 && (device->target->bus == cdm->pos.cookie.bus)
1643 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1644 && (device->target == cdm->pos.cookie.target)
1645 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1646 && (device == cdm->pos.cookie.device)
1647 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1648 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
1649 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1650 device->generation)){
1651 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1655 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1656 && (cdm->pos.cookie.bus == device->target->bus)
1657 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1658 && (cdm->pos.cookie.target == device->target)
1659 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1660 && (cdm->pos.cookie.device == device)
1661 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1662 && (cdm->pos.cookie.periph != NULL))
1663 return(xptperiphtraverse(device,
1664 (struct cam_periph *)cdm->pos.cookie.periph,
1665 xptedtperiphfunc, arg));
1667 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
1671 xptedtperiphfunc(struct cam_periph *periph, void *arg)
1673 struct ccb_dev_match *cdm;
1674 dev_match_ret retval;
1676 cdm = (struct ccb_dev_match *)arg;
1678 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1680 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1681 cdm->status = CAM_DEV_MATCH_ERROR;
1686 * If the copy flag is set, copy this peripheral out.
1688 if (retval & DM_RET_COPY) {
1691 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1692 sizeof(struct dev_match_result));
1695 * If we don't have enough space to put in another
1696 * match result, save our position and tell the
1697 * user there are more devices to check.
1699 if (spaceleft < sizeof(struct dev_match_result)) {
1700 bzero(&cdm->pos, sizeof(cdm->pos));
1701 cdm->pos.position_type =
1702 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1703 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
1706 cdm->pos.cookie.bus = periph->path->bus;
1707 cdm->pos.generations[CAM_BUS_GENERATION]=
1708 xsoftc.bus_generation;
1709 cdm->pos.cookie.target = periph->path->target;
1710 cdm->pos.generations[CAM_TARGET_GENERATION] =
1711 periph->path->bus->generation;
1712 cdm->pos.cookie.device = periph->path->device;
1713 cdm->pos.generations[CAM_DEV_GENERATION] =
1714 periph->path->target->generation;
1715 cdm->pos.cookie.periph = periph;
1716 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1717 periph->path->device->generation;
1718 cdm->status = CAM_DEV_MATCH_MORE;
1722 j = cdm->num_matches;
1724 cdm->matches[j].type = DEV_MATCH_PERIPH;
1725 cdm->matches[j].result.periph_result.path_id =
1726 periph->path->bus->path_id;
1727 cdm->matches[j].result.periph_result.target_id =
1728 periph->path->target->target_id;
1729 cdm->matches[j].result.periph_result.target_lun =
1730 periph->path->device->lun_id;
1731 cdm->matches[j].result.periph_result.unit_number =
1732 periph->unit_number;
1733 strncpy(cdm->matches[j].result.periph_result.periph_name,
1734 periph->periph_name, DEV_IDLEN);
1741 xptedtmatch(struct ccb_dev_match *cdm)
1745 cdm->num_matches = 0;
1748 * Check the bus list generation. If it has changed, the user
1749 * needs to reset everything and start over.
1751 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1752 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
1753 && (cdm->pos.generations[CAM_BUS_GENERATION] != xsoftc.bus_generation)) {
1754 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1758 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1759 && (cdm->pos.cookie.bus != NULL))
1760 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
1761 xptedtbusfunc, cdm);
1763 ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
1766 * If we get back 0, that means that we had to stop before fully
1767 * traversing the EDT. It also means that one of the subroutines
1768 * has set the status field to the proper value. If we get back 1,
1769 * we've fully traversed the EDT and copied out any matching entries.
1772 cdm->status = CAM_DEV_MATCH_LAST;
1778 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
1780 struct ccb_dev_match *cdm;
1782 cdm = (struct ccb_dev_match *)arg;
1784 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1785 && (cdm->pos.cookie.pdrv == pdrv)
1786 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1787 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
1788 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1789 (*pdrv)->generation)) {
1790 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1794 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1795 && (cdm->pos.cookie.pdrv == pdrv)
1796 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1797 && (cdm->pos.cookie.periph != NULL))
1798 return(xptpdperiphtraverse(pdrv,
1799 (struct cam_periph *)cdm->pos.cookie.periph,
1800 xptplistperiphfunc, arg));
1802 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
1806 xptplistperiphfunc(struct cam_periph *periph, void *arg)
1808 struct ccb_dev_match *cdm;
1809 dev_match_ret retval;
1811 cdm = (struct ccb_dev_match *)arg;
1813 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1815 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1816 cdm->status = CAM_DEV_MATCH_ERROR;
1821 * If the copy flag is set, copy this peripheral out.
1823 if (retval & DM_RET_COPY) {
1826 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1827 sizeof(struct dev_match_result));
1830 * If we don't have enough space to put in another
1831 * match result, save our position and tell the
1832 * user there are more devices to check.
1834 if (spaceleft < sizeof(struct dev_match_result)) {
1835 struct periph_driver **pdrv;
1838 bzero(&cdm->pos, sizeof(cdm->pos));
1839 cdm->pos.position_type =
1840 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
1844 * This may look a bit non-sensical, but it is
1845 * actually quite logical. There are very few
1846 * peripheral drivers, and bloating every peripheral
1847 * structure with a pointer back to its parent
1848 * peripheral driver linker set entry would cost
1849 * more in the long run than doing this quick lookup.
1851 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
1852 if (strcmp((*pdrv)->driver_name,
1853 periph->periph_name) == 0)
1857 if (*pdrv == NULL) {
1858 cdm->status = CAM_DEV_MATCH_ERROR;
1862 cdm->pos.cookie.pdrv = pdrv;
1864 * The periph generation slot does double duty, as
1865 * does the periph pointer slot. They are used for
1866 * both edt and pdrv lookups and positioning.
1868 cdm->pos.cookie.periph = periph;
1869 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1870 (*pdrv)->generation;
1871 cdm->status = CAM_DEV_MATCH_MORE;
1875 j = cdm->num_matches;
1877 cdm->matches[j].type = DEV_MATCH_PERIPH;
1878 cdm->matches[j].result.periph_result.path_id =
1879 periph->path->bus->path_id;
1882 * The transport layer peripheral doesn't have a target or
1885 if (periph->path->target)
1886 cdm->matches[j].result.periph_result.target_id =
1887 periph->path->target->target_id;
1889 cdm->matches[j].result.periph_result.target_id = -1;
1891 if (periph->path->device)
1892 cdm->matches[j].result.periph_result.target_lun =
1893 periph->path->device->lun_id;
1895 cdm->matches[j].result.periph_result.target_lun = -1;
1897 cdm->matches[j].result.periph_result.unit_number =
1898 periph->unit_number;
1899 strncpy(cdm->matches[j].result.periph_result.periph_name,
1900 periph->periph_name, DEV_IDLEN);
1907 xptperiphlistmatch(struct ccb_dev_match *cdm)
1911 cdm->num_matches = 0;
1914 * At this point in the edt traversal function, we check the bus
1915 * list generation to make sure that no busses have been added or
1916 * removed since the user last sent a XPT_DEV_MATCH ccb through.
1917 * For the peripheral driver list traversal function, however, we
1918 * don't have to worry about new peripheral driver types coming or
1919 * going; they're in a linker set, and therefore can't change
1920 * without a recompile.
1923 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1924 && (cdm->pos.cookie.pdrv != NULL))
1925 ret = xptpdrvtraverse(
1926 (struct periph_driver **)cdm->pos.cookie.pdrv,
1927 xptplistpdrvfunc, cdm);
1929 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
1932 * If we get back 0, that means that we had to stop before fully
1933 * traversing the peripheral driver tree. It also means that one of
1934 * the subroutines has set the status field to the proper value. If
1935 * we get back 1, we've fully traversed the EDT and copied out any
1939 cdm->status = CAM_DEV_MATCH_LAST;
1945 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
1947 struct cam_eb *bus, *next_bus;
1952 mtx_lock(&xsoftc.xpt_topo_lock);
1953 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xsoftc.xpt_busses));
1956 next_bus = TAILQ_NEXT(bus, links);
1958 mtx_unlock(&xsoftc.xpt_topo_lock);
1959 CAM_SIM_LOCK(bus->sim);
1960 retval = tr_func(bus, arg);
1961 CAM_SIM_UNLOCK(bus->sim);
1964 mtx_lock(&xsoftc.xpt_topo_lock);
1966 mtx_unlock(&xsoftc.xpt_topo_lock);
1972 xpt_sim_opened(struct cam_sim *sim)
1975 struct cam_et *target;
1976 struct cam_ed *device;
1977 struct cam_periph *periph;
1979 KASSERT(sim->refcount >= 1, ("sim->refcount >= 1"));
1980 mtx_assert(sim->mtx, MA_OWNED);
1982 mtx_lock(&xsoftc.xpt_topo_lock);
1983 TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) {
1984 if (bus->sim != sim)
1987 TAILQ_FOREACH(target, &bus->et_entries, links) {
1988 TAILQ_FOREACH(device, &target->ed_entries, links) {
1989 SLIST_FOREACH(periph, &device->periphs,
1991 if (periph->refcount > 0) {
1992 mtx_unlock(&xsoftc.xpt_topo_lock);
2000 mtx_unlock(&xsoftc.xpt_topo_lock);
2005 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2006 xpt_targetfunc_t *tr_func, void *arg)
2008 struct cam_et *target, *next_target;
2012 for (target = (start_target ? start_target :
2013 TAILQ_FIRST(&bus->et_entries));
2014 target != NULL; target = next_target) {
2016 next_target = TAILQ_NEXT(target, links);
2018 retval = tr_func(target, arg);
2028 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2029 xpt_devicefunc_t *tr_func, void *arg)
2031 struct cam_ed *device, *next_device;
2035 for (device = (start_device ? start_device :
2036 TAILQ_FIRST(&target->ed_entries));
2038 device = next_device) {
2040 next_device = TAILQ_NEXT(device, links);
2042 retval = tr_func(device, arg);
2052 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2053 xpt_periphfunc_t *tr_func, void *arg)
2055 struct cam_periph *periph, *next_periph;
2060 for (periph = (start_periph ? start_periph :
2061 SLIST_FIRST(&device->periphs));
2063 periph = next_periph) {
2065 next_periph = SLIST_NEXT(periph, periph_links);
2067 retval = tr_func(periph, arg);
2076 xptpdrvtraverse(struct periph_driver **start_pdrv,
2077 xpt_pdrvfunc_t *tr_func, void *arg)
2079 struct periph_driver **pdrv;
2085 * We don't traverse the peripheral driver list like we do the
2086 * other lists, because it is a linker set, and therefore cannot be
2087 * changed during runtime. If the peripheral driver list is ever
2088 * re-done to be something other than a linker set (i.e. it can
2089 * change while the system is running), the list traversal should
2090 * be modified to work like the other traversal functions.
2092 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2093 *pdrv != NULL; pdrv++) {
2094 retval = tr_func(pdrv, arg);
2104 xptpdperiphtraverse(struct periph_driver **pdrv,
2105 struct cam_periph *start_periph,
2106 xpt_periphfunc_t *tr_func, void *arg)
2108 struct cam_periph *periph, *next_periph;
2113 for (periph = (start_periph ? start_periph :
2114 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2115 periph = next_periph) {
2117 next_periph = TAILQ_NEXT(periph, unit_links);
2119 retval = tr_func(periph, arg);
2127 xptdefbusfunc(struct cam_eb *bus, void *arg)
2129 struct xpt_traverse_config *tr_config;
2131 tr_config = (struct xpt_traverse_config *)arg;
2133 if (tr_config->depth == XPT_DEPTH_BUS) {
2134 xpt_busfunc_t *tr_func;
2136 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2138 return(tr_func(bus, tr_config->tr_arg));
2140 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2144 xptdeftargetfunc(struct cam_et *target, void *arg)
2146 struct xpt_traverse_config *tr_config;
2148 tr_config = (struct xpt_traverse_config *)arg;
2150 if (tr_config->depth == XPT_DEPTH_TARGET) {
2151 xpt_targetfunc_t *tr_func;
2153 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2155 return(tr_func(target, tr_config->tr_arg));
2157 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2161 xptdefdevicefunc(struct cam_ed *device, void *arg)
2163 struct xpt_traverse_config *tr_config;
2165 tr_config = (struct xpt_traverse_config *)arg;
2167 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2168 xpt_devicefunc_t *tr_func;
2170 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2172 return(tr_func(device, tr_config->tr_arg));
2174 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2178 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2180 struct xpt_traverse_config *tr_config;
2181 xpt_periphfunc_t *tr_func;
2183 tr_config = (struct xpt_traverse_config *)arg;
2185 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2188 * Unlike the other default functions, we don't check for depth
2189 * here. The peripheral driver level is the last level in the EDT,
2190 * so if we're here, we should execute the function in question.
2192 return(tr_func(periph, tr_config->tr_arg));
2196 * Execute the given function for every bus in the EDT.
2199 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2201 struct xpt_traverse_config tr_config;
2203 tr_config.depth = XPT_DEPTH_BUS;
2204 tr_config.tr_func = tr_func;
2205 tr_config.tr_arg = arg;
2207 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2211 * Execute the given function for every device in the EDT.
2214 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2216 struct xpt_traverse_config tr_config;
2218 tr_config.depth = XPT_DEPTH_DEVICE;
2219 tr_config.tr_func = tr_func;
2220 tr_config.tr_arg = arg;
2222 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2226 xptsetasyncfunc(struct cam_ed *device, void *arg)
2228 struct cam_path path;
2229 struct ccb_getdev cgd;
2230 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2233 * Don't report unconfigured devices (Wildcard devs,
2234 * devices only for target mode, device instances
2235 * that have been invalidated but are waiting for
2236 * their last reference count to be released).
2238 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2241 xpt_compile_path(&path,
2243 device->target->bus->path_id,
2244 device->target->target_id,
2246 xpt_setup_ccb(&cgd.ccb_h, &path, CAM_PRIORITY_NORMAL);
2247 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2248 xpt_action((union ccb *)&cgd);
2249 csa->callback(csa->callback_arg,
2252 xpt_release_path(&path);
2258 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2260 struct cam_path path;
2261 struct ccb_pathinq cpi;
2262 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2264 xpt_compile_path(&path, /*periph*/NULL,
2266 CAM_TARGET_WILDCARD,
2268 xpt_setup_ccb(&cpi.ccb_h, &path, CAM_PRIORITY_NORMAL);
2269 cpi.ccb_h.func_code = XPT_PATH_INQ;
2270 xpt_action((union ccb *)&cpi);
2271 csa->callback(csa->callback_arg,
2274 xpt_release_path(&path);
2280 xpt_action(union ccb *start_ccb)
2283 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2285 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2286 /* Compatibility for RL-unaware code. */
2287 if (CAM_PRIORITY_TO_RL(start_ccb->ccb_h.pinfo.priority) == 0)
2288 start_ccb->ccb_h.pinfo.priority += CAM_PRIORITY_NORMAL - 1;
2289 (*(start_ccb->ccb_h.path->bus->xport->action))(start_ccb);
2293 xpt_action_default(union ccb *start_ccb)
2296 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action_default\n"));
2299 switch (start_ccb->ccb_h.func_code) {
2302 struct cam_ed *device;
2304 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
2305 struct cam_path *path;
2307 path = start_ccb->ccb_h.path;
2311 * For the sake of compatibility with SCSI-1
2312 * devices that may not understand the identify
2313 * message, we include lun information in the
2314 * second byte of all commands. SCSI-1 specifies
2315 * that luns are a 3 bit value and reserves only 3
2316 * bits for lun information in the CDB. Later
2317 * revisions of the SCSI spec allow for more than 8
2318 * luns, but have deprecated lun information in the
2319 * CDB. So, if the lun won't fit, we must omit.
2321 * Also be aware that during initial probing for devices,
2322 * the inquiry information is unknown but initialized to 0.
2323 * This means that this code will be exercised while probing
2324 * devices with an ANSI revision greater than 2.
2326 device = start_ccb->ccb_h.path->device;
2327 if (device->protocol_version <= SCSI_REV_2
2328 && start_ccb->ccb_h.target_lun < 8
2329 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2331 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2332 start_ccb->ccb_h.target_lun << 5;
2334 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2335 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n",
2336 scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0],
2337 &path->device->inq_data),
2338 scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes,
2339 cdb_str, sizeof(cdb_str))));
2343 case XPT_CONT_TARGET_IO:
2344 start_ccb->csio.sense_resid = 0;
2345 start_ccb->csio.resid = 0;
2348 if (start_ccb->ccb_h.func_code == XPT_ATA_IO) {
2349 start_ccb->ataio.resid = 0;
2355 struct cam_path *path = start_ccb->ccb_h.path;
2358 frozen = cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2359 path->device->sim->devq->alloc_openings += frozen;
2361 xpt_run_dev_allocq(path->bus);
2362 if (xpt_schedule_dev_sendq(path->bus, path->device))
2363 xpt_run_dev_sendq(path->bus);
2366 case XPT_CALC_GEOMETRY:
2368 struct cam_sim *sim;
2370 /* Filter out garbage */
2371 if (start_ccb->ccg.block_size == 0
2372 || start_ccb->ccg.volume_size == 0) {
2373 start_ccb->ccg.cylinders = 0;
2374 start_ccb->ccg.heads = 0;
2375 start_ccb->ccg.secs_per_track = 0;
2376 start_ccb->ccb_h.status = CAM_REQ_CMP;
2381 * In a PC-98 system, geometry translation depens on
2382 * the "real" device geometry obtained from mode page 4.
2383 * SCSI geometry translation is performed in the
2384 * initialization routine of the SCSI BIOS and the result
2385 * stored in host memory. If the translation is available
2386 * in host memory, use it. If not, rely on the default
2387 * translation the device driver performs.
2389 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
2390 start_ccb->ccb_h.status = CAM_REQ_CMP;
2394 sim = start_ccb->ccb_h.path->bus->sim;
2395 (*(sim->sim_action))(sim, start_ccb);
2400 union ccb* abort_ccb;
2402 abort_ccb = start_ccb->cab.abort_ccb;
2403 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
2405 if (abort_ccb->ccb_h.pinfo.index >= 0) {
2406 struct cam_ccbq *ccbq;
2407 struct cam_ed *device;
2409 device = abort_ccb->ccb_h.path->device;
2410 ccbq = &device->ccbq;
2411 device->sim->devq->alloc_openings -=
2412 cam_ccbq_remove_ccb(ccbq, abort_ccb);
2413 abort_ccb->ccb_h.status =
2414 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2415 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2416 xpt_done(abort_ccb);
2417 start_ccb->ccb_h.status = CAM_REQ_CMP;
2420 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
2421 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
2423 * We've caught this ccb en route to
2424 * the SIM. Flag it for abort and the
2425 * SIM will do so just before starting
2426 * real work on the CCB.
2428 abort_ccb->ccb_h.status =
2429 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2430 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2431 start_ccb->ccb_h.status = CAM_REQ_CMP;
2435 if (XPT_FC_IS_QUEUED(abort_ccb)
2436 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
2438 * It's already completed but waiting
2439 * for our SWI to get to it.
2441 start_ccb->ccb_h.status = CAM_UA_ABORT;
2445 * If we weren't able to take care of the abort request
2446 * in the XPT, pass the request down to the SIM for processing.
2450 case XPT_ACCEPT_TARGET_IO:
2452 case XPT_IMMED_NOTIFY:
2453 case XPT_NOTIFY_ACK:
2455 case XPT_IMMEDIATE_NOTIFY:
2456 case XPT_NOTIFY_ACKNOWLEDGE:
2457 case XPT_GET_SIM_KNOB:
2458 case XPT_SET_SIM_KNOB:
2460 struct cam_sim *sim;
2462 sim = start_ccb->ccb_h.path->bus->sim;
2463 (*(sim->sim_action))(sim, start_ccb);
2468 struct cam_sim *sim;
2470 sim = start_ccb->ccb_h.path->bus->sim;
2471 (*(sim->sim_action))(sim, start_ccb);
2474 case XPT_PATH_STATS:
2475 start_ccb->cpis.last_reset =
2476 start_ccb->ccb_h.path->bus->last_reset;
2477 start_ccb->ccb_h.status = CAM_REQ_CMP;
2483 dev = start_ccb->ccb_h.path->device;
2484 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2485 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2487 struct ccb_getdev *cgd;
2491 cgd = &start_ccb->cgd;
2492 bus = cgd->ccb_h.path->bus;
2493 tar = cgd->ccb_h.path->target;
2494 cgd->protocol = dev->protocol;
2495 cgd->inq_data = dev->inq_data;
2496 cgd->ident_data = dev->ident_data;
2497 cgd->inq_flags = dev->inq_flags;
2498 cgd->ccb_h.status = CAM_REQ_CMP;
2499 cgd->serial_num_len = dev->serial_num_len;
2500 if ((dev->serial_num_len > 0)
2501 && (dev->serial_num != NULL))
2502 bcopy(dev->serial_num, cgd->serial_num,
2503 dev->serial_num_len);
2507 case XPT_GDEV_STATS:
2511 dev = start_ccb->ccb_h.path->device;
2512 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2513 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2515 struct ccb_getdevstats *cgds;
2519 cgds = &start_ccb->cgds;
2520 bus = cgds->ccb_h.path->bus;
2521 tar = cgds->ccb_h.path->target;
2522 cgds->dev_openings = dev->ccbq.dev_openings;
2523 cgds->dev_active = dev->ccbq.dev_active;
2524 cgds->devq_openings = dev->ccbq.devq_openings;
2525 cgds->devq_queued = dev->ccbq.queue.entries;
2526 cgds->held = dev->ccbq.held;
2527 cgds->last_reset = tar->last_reset;
2528 cgds->maxtags = dev->maxtags;
2529 cgds->mintags = dev->mintags;
2530 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
2531 cgds->last_reset = bus->last_reset;
2532 cgds->ccb_h.status = CAM_REQ_CMP;
2538 struct cam_periph *nperiph;
2539 struct periph_list *periph_head;
2540 struct ccb_getdevlist *cgdl;
2542 struct cam_ed *device;
2549 * Don't want anyone mucking with our data.
2551 device = start_ccb->ccb_h.path->device;
2552 periph_head = &device->periphs;
2553 cgdl = &start_ccb->cgdl;
2556 * Check and see if the list has changed since the user
2557 * last requested a list member. If so, tell them that the
2558 * list has changed, and therefore they need to start over
2559 * from the beginning.
2561 if ((cgdl->index != 0) &&
2562 (cgdl->generation != device->generation)) {
2563 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
2568 * Traverse the list of peripherals and attempt to find
2569 * the requested peripheral.
2571 for (nperiph = SLIST_FIRST(periph_head), i = 0;
2572 (nperiph != NULL) && (i <= cgdl->index);
2573 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
2574 if (i == cgdl->index) {
2575 strncpy(cgdl->periph_name,
2576 nperiph->periph_name,
2578 cgdl->unit_number = nperiph->unit_number;
2583 cgdl->status = CAM_GDEVLIST_ERROR;
2587 if (nperiph == NULL)
2588 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
2590 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
2593 cgdl->generation = device->generation;
2595 cgdl->ccb_h.status = CAM_REQ_CMP;
2600 dev_pos_type position_type;
2601 struct ccb_dev_match *cdm;
2603 cdm = &start_ccb->cdm;
2606 * There are two ways of getting at information in the EDT.
2607 * The first way is via the primary EDT tree. It starts
2608 * with a list of busses, then a list of targets on a bus,
2609 * then devices/luns on a target, and then peripherals on a
2610 * device/lun. The "other" way is by the peripheral driver
2611 * lists. The peripheral driver lists are organized by
2612 * peripheral driver. (obviously) So it makes sense to
2613 * use the peripheral driver list if the user is looking
2614 * for something like "da1", or all "da" devices. If the
2615 * user is looking for something on a particular bus/target
2616 * or lun, it's generally better to go through the EDT tree.
2619 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
2620 position_type = cdm->pos.position_type;
2624 position_type = CAM_DEV_POS_NONE;
2626 for (i = 0; i < cdm->num_patterns; i++) {
2627 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
2628 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
2629 position_type = CAM_DEV_POS_EDT;
2634 if (cdm->num_patterns == 0)
2635 position_type = CAM_DEV_POS_EDT;
2636 else if (position_type == CAM_DEV_POS_NONE)
2637 position_type = CAM_DEV_POS_PDRV;
2640 switch(position_type & CAM_DEV_POS_TYPEMASK) {
2641 case CAM_DEV_POS_EDT:
2644 case CAM_DEV_POS_PDRV:
2645 xptperiphlistmatch(cdm);
2648 cdm->status = CAM_DEV_MATCH_ERROR;
2652 if (cdm->status == CAM_DEV_MATCH_ERROR)
2653 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2655 start_ccb->ccb_h.status = CAM_REQ_CMP;
2661 struct ccb_setasync *csa;
2662 struct async_node *cur_entry;
2663 struct async_list *async_head;
2666 csa = &start_ccb->csa;
2667 added = csa->event_enable;
2668 async_head = &csa->ccb_h.path->device->asyncs;
2671 * If there is already an entry for us, simply
2674 cur_entry = SLIST_FIRST(async_head);
2675 while (cur_entry != NULL) {
2676 if ((cur_entry->callback_arg == csa->callback_arg)
2677 && (cur_entry->callback == csa->callback))
2679 cur_entry = SLIST_NEXT(cur_entry, links);
2682 if (cur_entry != NULL) {
2684 * If the request has no flags set,
2687 added &= ~cur_entry->event_enable;
2688 if (csa->event_enable == 0) {
2689 SLIST_REMOVE(async_head, cur_entry,
2691 xpt_release_device(csa->ccb_h.path->device);
2692 free(cur_entry, M_CAMXPT);
2694 cur_entry->event_enable = csa->event_enable;
2696 csa->event_enable = added;
2698 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
2700 if (cur_entry == NULL) {
2701 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
2704 cur_entry->event_enable = csa->event_enable;
2705 cur_entry->callback_arg = csa->callback_arg;
2706 cur_entry->callback = csa->callback;
2707 SLIST_INSERT_HEAD(async_head, cur_entry, links);
2708 xpt_acquire_device(csa->ccb_h.path->device);
2710 start_ccb->ccb_h.status = CAM_REQ_CMP;
2715 struct ccb_relsim *crs;
2718 crs = &start_ccb->crs;
2719 dev = crs->ccb_h.path->device;
2722 crs->ccb_h.status = CAM_DEV_NOT_THERE;
2726 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
2728 if (INQ_DATA_TQ_ENABLED(&dev->inq_data)) {
2729 /* Don't ever go below one opening */
2730 if (crs->openings > 0) {
2731 xpt_dev_ccbq_resize(crs->ccb_h.path,
2735 xpt_print(crs->ccb_h.path,
2736 "tagged openings now %d\n",
2743 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
2745 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
2748 * Just extend the old timeout and decrement
2749 * the freeze count so that a single timeout
2750 * is sufficient for releasing the queue.
2752 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2753 callout_stop(&dev->callout);
2756 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2759 callout_reset(&dev->callout,
2760 (crs->release_timeout * hz) / 1000,
2761 xpt_release_devq_timeout, dev);
2763 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
2767 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
2769 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
2771 * Decrement the freeze count so that a single
2772 * completion is still sufficient to unfreeze
2775 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2778 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
2779 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2783 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
2785 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
2786 || (dev->ccbq.dev_active == 0)) {
2788 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2791 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
2792 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2796 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
2797 xpt_release_devq_rl(crs->ccb_h.path, /*runlevel*/
2798 (crs->release_flags & RELSIM_RELEASE_RUNLEVEL) ?
2799 crs->release_timeout : 0,
2800 /*count*/1, /*run_queue*/TRUE);
2802 start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt[0];
2803 start_ccb->ccb_h.status = CAM_REQ_CMP;
2808 #ifdef CAM_DEBUG_DELAY
2809 cam_debug_delay = CAM_DEBUG_DELAY;
2811 cam_dflags = start_ccb->cdbg.flags;
2812 if (cam_dpath != NULL) {
2813 xpt_free_path(cam_dpath);
2817 if (cam_dflags != CAM_DEBUG_NONE) {
2818 if (xpt_create_path(&cam_dpath, xpt_periph,
2819 start_ccb->ccb_h.path_id,
2820 start_ccb->ccb_h.target_id,
2821 start_ccb->ccb_h.target_lun) !=
2823 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2824 cam_dflags = CAM_DEBUG_NONE;
2826 start_ccb->ccb_h.status = CAM_REQ_CMP;
2827 xpt_print(cam_dpath, "debugging flags now %x\n",
2832 start_ccb->ccb_h.status = CAM_REQ_CMP;
2834 #else /* !CAMDEBUG */
2835 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2836 #endif /* CAMDEBUG */
2839 case XPT_FREEZE_QUEUE:
2841 struct ccb_relsim *crs = &start_ccb->crs;
2843 xpt_freeze_devq_rl(crs->ccb_h.path, /*runlevel*/
2844 (crs->release_flags & RELSIM_RELEASE_RUNLEVEL) ?
2845 crs->release_timeout : 0, /*count*/1);
2846 start_ccb->ccb_h.status = CAM_REQ_CMP;
2850 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
2851 xpt_freeze_devq(start_ccb->ccb_h.path, 1);
2852 start_ccb->ccb_h.status = CAM_REQ_CMP;
2859 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
2860 if (start_ccb->ccb_h.func_code & XPT_FC_DEV_QUEUED) {
2861 xpt_done(start_ccb);
2868 xpt_polled_action(union ccb *start_ccb)
2871 struct cam_sim *sim;
2872 struct cam_devq *devq;
2876 timeout = start_ccb->ccb_h.timeout;
2877 sim = start_ccb->ccb_h.path->bus->sim;
2879 dev = start_ccb->ccb_h.path->device;
2881 mtx_assert(sim->mtx, MA_OWNED);
2884 * Steal an opening so that no other queued requests
2885 * can get it before us while we simulate interrupts.
2887 dev->ccbq.devq_openings--;
2888 dev->ccbq.dev_openings--;
2890 while(((devq != NULL && devq->send_openings <= 0) ||
2891 dev->ccbq.dev_openings < 0) && (--timeout > 0)) {
2893 (*(sim->sim_poll))(sim);
2894 camisr_runqueue(&sim->sim_doneq);
2897 dev->ccbq.devq_openings++;
2898 dev->ccbq.dev_openings++;
2901 xpt_action(start_ccb);
2902 while(--timeout > 0) {
2903 (*(sim->sim_poll))(sim);
2904 camisr_runqueue(&sim->sim_doneq);
2905 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
2912 * XXX Is it worth adding a sim_timeout entry
2913 * point so we can attempt recovery? If
2914 * this is only used for dumps, I don't think
2917 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
2920 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2925 * Schedule a peripheral driver to receive a ccb when it's
2926 * target device has space for more transactions.
2929 xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
2931 struct cam_ed *device;
2934 mtx_assert(perph->sim->mtx, MA_OWNED);
2936 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
2937 device = perph->path->device;
2938 if (periph_is_queued(perph)) {
2939 /* Simply reorder based on new priority */
2940 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
2941 (" change priority to %d\n", new_priority));
2942 if (new_priority < perph->pinfo.priority) {
2943 camq_change_priority(&device->drvq,
2946 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
2949 /* New entry on the queue */
2950 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
2951 (" added periph to queue\n"));
2952 perph->pinfo.priority = new_priority;
2953 perph->pinfo.generation = ++device->drvq.generation;
2954 camq_insert(&device->drvq, &perph->pinfo);
2955 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
2958 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
2959 (" calling xpt_run_devq\n"));
2960 xpt_run_dev_allocq(perph->path->bus);
2966 * Schedule a device to run on a given queue.
2967 * If the device was inserted as a new entry on the queue,
2968 * return 1 meaning the device queue should be run. If we
2969 * were already queued, implying someone else has already
2970 * started the queue, return 0 so the caller doesn't attempt
2974 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
2975 u_int32_t new_priority)
2978 u_int32_t old_priority;
2980 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
2982 old_priority = pinfo->priority;
2985 * Are we already queued?
2987 if (pinfo->index != CAM_UNQUEUED_INDEX) {
2988 /* Simply reorder based on new priority */
2989 if (new_priority < old_priority) {
2990 camq_change_priority(queue, pinfo->index,
2992 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
2993 ("changed priority to %d\n",
2999 /* New entry on the queue */
3000 if (new_priority < old_priority)
3001 pinfo->priority = new_priority;
3003 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3004 ("Inserting onto queue\n"));
3005 pinfo->generation = ++queue->generation;
3006 camq_insert(queue, pinfo);
3013 xpt_run_dev_allocq(struct cam_eb *bus)
3015 struct cam_devq *devq;
3017 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
3018 devq = bus->sim->devq;
3020 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3021 (" qfrozen_cnt == 0x%x, entries == %d, "
3022 "openings == %d, active == %d\n",
3023 devq->alloc_queue.qfrozen_cnt[0],
3024 devq->alloc_queue.entries,
3025 devq->alloc_openings,
3026 devq->alloc_active));
3028 devq->alloc_queue.qfrozen_cnt[0]++;
3029 while ((devq->alloc_queue.entries > 0)
3030 && (devq->alloc_openings > 0)
3031 && (devq->alloc_queue.qfrozen_cnt[0] <= 1)) {
3032 struct cam_ed_qinfo *qinfo;
3033 struct cam_ed *device;
3034 union ccb *work_ccb;
3035 struct cam_periph *drv;
3038 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3040 device = qinfo->device;
3041 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3042 ("running device %p\n", device));
3044 drvq = &device->drvq;
3047 if (drvq->entries <= 0) {
3048 panic("xpt_run_dev_allocq: "
3049 "Device on queue without any work to do");
3052 if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3053 devq->alloc_openings--;
3054 devq->alloc_active++;
3055 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3056 xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3057 drv->pinfo.priority);
3058 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3059 ("calling periph start\n"));
3060 drv->periph_start(drv, work_ccb);
3063 * Malloc failure in alloc_ccb
3066 * XXX add us to a list to be run from free_ccb
3067 * if we don't have any ccbs active on this
3068 * device queue otherwise we may never get run
3074 /* We may have more work. Attempt to reschedule. */
3075 xpt_schedule_dev_allocq(bus, device);
3077 devq->alloc_queue.qfrozen_cnt[0]--;
3081 xpt_run_dev_sendq(struct cam_eb *bus)
3083 struct cam_devq *devq;
3085 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
3087 devq = bus->sim->devq;
3089 devq->send_queue.qfrozen_cnt[0]++;
3090 while ((devq->send_queue.entries > 0)
3091 && (devq->send_openings > 0)
3092 && (devq->send_queue.qfrozen_cnt[0] <= 1)) {
3093 struct cam_ed_qinfo *qinfo;
3094 struct cam_ed *device;
3095 union ccb *work_ccb;
3096 struct cam_sim *sim;
3098 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3100 device = qinfo->device;
3101 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3102 ("running device %p\n", device));
3104 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3105 if (work_ccb == NULL) {
3106 printf("device on run queue with no ccbs???\n");
3110 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3112 mtx_lock(&xsoftc.xpt_lock);
3113 if (xsoftc.num_highpower <= 0) {
3115 * We got a high power command, but we
3116 * don't have any available slots. Freeze
3117 * the device queue until we have a slot
3120 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3121 STAILQ_INSERT_TAIL(&xsoftc.highpowerq,
3125 mtx_unlock(&xsoftc.xpt_lock);
3129 * Consume a high power slot while
3132 xsoftc.num_highpower--;
3134 mtx_unlock(&xsoftc.xpt_lock);
3136 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3137 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3139 devq->send_openings--;
3140 devq->send_active++;
3142 xpt_schedule_dev_sendq(bus, device);
3144 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
3146 * The client wants to freeze the queue
3147 * after this CCB is sent.
3149 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3152 /* In Target mode, the peripheral driver knows best... */
3153 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3154 if ((device->inq_flags & SID_CmdQue) != 0
3155 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3156 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3159 * Clear this in case of a retried CCB that
3160 * failed due to a rejected tag.
3162 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3166 * Device queues can be shared among multiple sim instances
3167 * that reside on different busses. Use the SIM in the queue
3168 * CCB's path, rather than the one in the bus that was passed
3169 * into this function.
3171 sim = work_ccb->ccb_h.path->bus->sim;
3172 (*(sim->sim_action))(sim, work_ccb);
3174 devq->send_queue.qfrozen_cnt[0]--;
3178 * This function merges stuff from the slave ccb into the master ccb, while
3179 * keeping important fields in the master ccb constant.
3182 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3186 * Pull fields that are valid for peripheral drivers to set
3187 * into the master CCB along with the CCB "payload".
3189 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3190 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3191 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3192 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3193 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3194 sizeof(union ccb) - sizeof(struct ccb_hdr));
3198 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3201 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3202 ccb_h->pinfo.priority = priority;
3204 ccb_h->path_id = path->bus->path_id;
3206 ccb_h->target_id = path->target->target_id;
3208 ccb_h->target_id = CAM_TARGET_WILDCARD;
3210 ccb_h->target_lun = path->device->lun_id;
3211 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3213 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3215 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3219 /* Path manipulation functions */
3221 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3222 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3224 struct cam_path *path;
3227 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_NOWAIT);
3230 status = CAM_RESRC_UNAVAIL;
3233 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3234 if (status != CAM_REQ_CMP) {
3235 free(path, M_CAMXPT);
3238 *new_path_ptr = path;
3243 xpt_create_path_unlocked(struct cam_path **new_path_ptr,
3244 struct cam_periph *periph, path_id_t path_id,
3245 target_id_t target_id, lun_id_t lun_id)
3247 struct cam_path *path;
3248 struct cam_eb *bus = NULL;
3250 int need_unlock = 0;
3252 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_WAITOK);
3254 if (path_id != CAM_BUS_WILDCARD) {
3255 bus = xpt_find_bus(path_id);
3258 CAM_SIM_LOCK(bus->sim);
3261 status = xpt_compile_path(path, periph, path_id, target_id, lun_id);
3263 CAM_SIM_UNLOCK(bus->sim);
3264 if (status != CAM_REQ_CMP) {
3265 free(path, M_CAMXPT);
3268 *new_path_ptr = path;
3273 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3274 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3277 struct cam_et *target;
3278 struct cam_ed *device;
3281 status = CAM_REQ_CMP; /* Completed without error */
3282 target = NULL; /* Wildcarded */
3283 device = NULL; /* Wildcarded */
3286 * We will potentially modify the EDT, so block interrupts
3287 * that may attempt to create cam paths.
3289 bus = xpt_find_bus(path_id);
3291 status = CAM_PATH_INVALID;
3293 target = xpt_find_target(bus, target_id);
3294 if (target == NULL) {
3296 struct cam_et *new_target;
3298 new_target = xpt_alloc_target(bus, target_id);
3299 if (new_target == NULL) {
3300 status = CAM_RESRC_UNAVAIL;
3302 target = new_target;
3305 if (target != NULL) {
3306 device = xpt_find_device(target, lun_id);
3307 if (device == NULL) {
3309 struct cam_ed *new_device;
3312 (*(bus->xport->alloc_device))(bus,
3315 if (new_device == NULL) {
3316 status = CAM_RESRC_UNAVAIL;
3318 device = new_device;
3325 * Only touch the user's data if we are successful.
3327 if (status == CAM_REQ_CMP) {
3328 new_path->periph = perph;
3329 new_path->bus = bus;
3330 new_path->target = target;
3331 new_path->device = device;
3332 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
3335 xpt_release_device(device);
3337 xpt_release_target(target);
3339 xpt_release_bus(bus);
3345 xpt_release_path(struct cam_path *path)
3347 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
3348 if (path->device != NULL) {
3349 xpt_release_device(path->device);
3350 path->device = NULL;
3352 if (path->target != NULL) {
3353 xpt_release_target(path->target);
3354 path->target = NULL;
3356 if (path->bus != NULL) {
3357 xpt_release_bus(path->bus);
3363 xpt_free_path(struct cam_path *path)
3366 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
3367 xpt_release_path(path);
3368 free(path, M_CAMXPT);
3373 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
3374 * in path1, 2 for match with wildcards in path2.
3377 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
3381 if (path1->bus != path2->bus) {
3382 if (path1->bus->path_id == CAM_BUS_WILDCARD)
3384 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
3389 if (path1->target != path2->target) {
3390 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
3393 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
3398 if (path1->device != path2->device) {
3399 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
3402 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
3411 xpt_print_path(struct cam_path *path)
3415 printf("(nopath): ");
3417 if (path->periph != NULL)
3418 printf("(%s%d:", path->periph->periph_name,
3419 path->periph->unit_number);
3421 printf("(noperiph:");
3423 if (path->bus != NULL)
3424 printf("%s%d:%d:", path->bus->sim->sim_name,
3425 path->bus->sim->unit_number,
3426 path->bus->sim->bus_id);
3430 if (path->target != NULL)
3431 printf("%d:", path->target->target_id);
3435 if (path->device != NULL)
3436 printf("%d): ", path->device->lun_id);
3443 xpt_print(struct cam_path *path, const char *fmt, ...)
3446 xpt_print_path(path);
3453 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
3458 if (path != NULL && path->bus != NULL)
3459 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3462 sbuf_new(&sb, str, str_len, 0);
3465 sbuf_printf(&sb, "(nopath): ");
3467 if (path->periph != NULL)
3468 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
3469 path->periph->unit_number);
3471 sbuf_printf(&sb, "(noperiph:");
3473 if (path->bus != NULL)
3474 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
3475 path->bus->sim->unit_number,
3476 path->bus->sim->bus_id);
3478 sbuf_printf(&sb, "nobus:");
3480 if (path->target != NULL)
3481 sbuf_printf(&sb, "%d:", path->target->target_id);
3483 sbuf_printf(&sb, "X:");
3485 if (path->device != NULL)
3486 sbuf_printf(&sb, "%d): ", path->device->lun_id);
3488 sbuf_printf(&sb, "X): ");
3492 return(sbuf_len(&sb));
3496 xpt_path_path_id(struct cam_path *path)
3498 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3500 return(path->bus->path_id);
3504 xpt_path_target_id(struct cam_path *path)
3506 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3508 if (path->target != NULL)
3509 return (path->target->target_id);
3511 return (CAM_TARGET_WILDCARD);
3515 xpt_path_lun_id(struct cam_path *path)
3517 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3519 if (path->device != NULL)
3520 return (path->device->lun_id);
3522 return (CAM_LUN_WILDCARD);
3526 xpt_path_sim(struct cam_path *path)
3529 return (path->bus->sim);
3533 xpt_path_periph(struct cam_path *path)
3535 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3537 return (path->periph);
3541 * Release a CAM control block for the caller. Remit the cost of the structure
3542 * to the device referenced by the path. If the this device had no 'credits'
3543 * and peripheral drivers have registered async callbacks for this notification
3547 xpt_release_ccb(union ccb *free_ccb)
3549 struct cam_path *path;
3550 struct cam_ed *device;
3552 struct cam_sim *sim;
3554 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
3555 path = free_ccb->ccb_h.path;
3556 device = path->device;
3560 mtx_assert(sim->mtx, MA_OWNED);
3562 cam_ccbq_release_opening(&device->ccbq);
3563 if (device->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) {
3564 device->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
3565 cam_ccbq_resize(&device->ccbq,
3566 device->ccbq.dev_openings + device->ccbq.dev_active);
3568 if (sim->ccb_count > sim->max_ccbs) {
3569 xpt_free_ccb(free_ccb);
3572 SLIST_INSERT_HEAD(&sim->ccb_freeq, &free_ccb->ccb_h,
3575 if (sim->devq == NULL) {
3578 sim->devq->alloc_openings++;
3579 sim->devq->alloc_active--;
3580 if (device_is_alloc_queued(device) == 0)
3581 xpt_schedule_dev_allocq(bus, device);
3582 xpt_run_dev_allocq(bus);
3585 /* Functions accessed by SIM drivers */
3587 static struct xpt_xport xport_default = {
3588 .alloc_device = xpt_alloc_device_default,
3589 .action = xpt_action_default,
3590 .async = xpt_dev_async_default,
3594 * A sim structure, listing the SIM entry points and instance
3595 * identification info is passed to xpt_bus_register to hook the SIM
3596 * into the CAM framework. xpt_bus_register creates a cam_eb entry
3597 * for this new bus and places it in the array of busses and assigns
3598 * it a path_id. The path_id may be influenced by "hard wiring"
3599 * information specified by the user. Once interrupt services are
3600 * available, the bus will be probed.
3603 xpt_bus_register(struct cam_sim *sim, device_t parent, u_int32_t bus)
3605 struct cam_eb *new_bus;
3606 struct cam_eb *old_bus;
3607 struct ccb_pathinq cpi;
3608 struct cam_path *path;
3611 mtx_assert(sim->mtx, MA_OWNED);
3614 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
3615 M_CAMXPT, M_NOWAIT);
3616 if (new_bus == NULL) {
3617 /* Couldn't satisfy request */
3618 return (CAM_RESRC_UNAVAIL);
3620 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_NOWAIT);
3622 free(new_bus, M_CAMXPT);
3623 return (CAM_RESRC_UNAVAIL);
3626 if (strcmp(sim->sim_name, "xpt") != 0) {
3628 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
3631 TAILQ_INIT(&new_bus->et_entries);
3632 new_bus->path_id = sim->path_id;
3635 timevalclear(&new_bus->last_reset);
3637 new_bus->refcount = 1; /* Held until a bus_deregister event */
3638 new_bus->generation = 0;
3640 mtx_lock(&xsoftc.xpt_topo_lock);
3641 old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3642 while (old_bus != NULL
3643 && old_bus->path_id < new_bus->path_id)
3644 old_bus = TAILQ_NEXT(old_bus, links);
3645 if (old_bus != NULL)
3646 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
3648 TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
3649 xsoftc.bus_generation++;
3650 mtx_unlock(&xsoftc.xpt_topo_lock);
3653 * Set a default transport so that a PATH_INQ can be issued to
3654 * the SIM. This will then allow for probing and attaching of
3655 * a more appropriate transport.
3657 new_bus->xport = &xport_default;
3659 status = xpt_compile_path(path, /*periph*/NULL, sim->path_id,
3660 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3661 if (status != CAM_REQ_CMP)
3662 printf("xpt_compile_path returned %d\n", status);
3664 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
3665 cpi.ccb_h.func_code = XPT_PATH_INQ;
3666 xpt_action((union ccb *)&cpi);
3668 if (cpi.ccb_h.status == CAM_REQ_CMP) {
3669 switch (cpi.transport) {
3676 new_bus->xport = scsi_get_xport();
3680 new_bus->xport = ata_get_xport();
3683 new_bus->xport = &xport_default;
3688 /* Notify interested parties */
3689 if (sim->path_id != CAM_XPT_PATH_ID) {
3690 union ccb *scan_ccb;
3692 xpt_async(AC_PATH_REGISTERED, path, &cpi);
3693 /* Initiate bus rescan. */
3694 scan_ccb = xpt_alloc_ccb_nowait();
3695 scan_ccb->ccb_h.path = path;
3696 scan_ccb->ccb_h.func_code = XPT_SCAN_BUS;
3697 scan_ccb->crcn.flags = 0;
3698 xpt_rescan(scan_ccb);
3700 xpt_free_path(path);
3701 return (CAM_SUCCESS);
3705 xpt_bus_deregister(path_id_t pathid)
3707 struct cam_path bus_path;
3710 status = xpt_compile_path(&bus_path, NULL, pathid,
3711 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3712 if (status != CAM_REQ_CMP)
3715 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
3716 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
3718 /* Release the reference count held while registered. */
3719 xpt_release_bus(bus_path.bus);
3720 xpt_release_path(&bus_path);
3722 return (CAM_REQ_CMP);
3726 xptnextfreepathid(void)
3733 mtx_lock(&xsoftc.xpt_topo_lock);
3734 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3736 /* Find an unoccupied pathid */
3737 while (bus != NULL && bus->path_id <= pathid) {
3738 if (bus->path_id == pathid)
3740 bus = TAILQ_NEXT(bus, links);
3742 mtx_unlock(&xsoftc.xpt_topo_lock);
3745 * Ensure that this pathid is not reserved for
3746 * a bus that may be registered in the future.
3748 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
3750 /* Start the search over */
3751 mtx_lock(&xsoftc.xpt_topo_lock);
3758 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
3765 pathid = CAM_XPT_PATH_ID;
3766 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
3768 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
3769 if (strcmp(dname, "scbus")) {
3770 /* Avoid a bit of foot shooting. */
3773 if (dunit < 0) /* unwired?! */
3775 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
3776 if (sim_bus == val) {
3780 } else if (sim_bus == 0) {
3781 /* Unspecified matches bus 0 */
3785 printf("Ambiguous scbus configuration for %s%d "
3786 "bus %d, cannot wire down. The kernel "
3787 "config entry for scbus%d should "
3788 "specify a controller bus.\n"
3789 "Scbus will be assigned dynamically.\n",
3790 sim_name, sim_unit, sim_bus, dunit);
3795 if (pathid == CAM_XPT_PATH_ID)
3796 pathid = xptnextfreepathid();
3801 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
3804 struct cam_et *target, *next_target;
3805 struct cam_ed *device, *next_device;
3807 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3809 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n"));
3812 * Most async events come from a CAM interrupt context. In
3813 * a few cases, the error recovery code at the peripheral layer,
3814 * which may run from our SWI or a process context, may signal
3815 * deferred events with a call to xpt_async.
3820 if (async_code == AC_BUS_RESET) {
3821 /* Update our notion of when the last reset occurred */
3822 microtime(&bus->last_reset);
3825 for (target = TAILQ_FIRST(&bus->et_entries);
3827 target = next_target) {
3829 next_target = TAILQ_NEXT(target, links);
3831 if (path->target != target
3832 && path->target->target_id != CAM_TARGET_WILDCARD
3833 && target->target_id != CAM_TARGET_WILDCARD)
3836 if (async_code == AC_SENT_BDR) {
3837 /* Update our notion of when the last reset occurred */
3838 microtime(&path->target->last_reset);
3841 for (device = TAILQ_FIRST(&target->ed_entries);
3843 device = next_device) {
3845 next_device = TAILQ_NEXT(device, links);
3847 if (path->device != device
3848 && path->device->lun_id != CAM_LUN_WILDCARD
3849 && device->lun_id != CAM_LUN_WILDCARD)
3852 * The async callback could free the device.
3853 * If it is a broadcast async, it doesn't hold
3854 * device reference, so take our own reference.
3856 xpt_acquire_device(device);
3857 (*(bus->xport->async))(async_code, bus,
3861 xpt_async_bcast(&device->asyncs, async_code,
3863 xpt_release_device(device);
3868 * If this wasn't a fully wildcarded async, tell all
3869 * clients that want all async events.
3871 if (bus != xpt_periph->path->bus)
3872 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
3877 xpt_async_bcast(struct async_list *async_head,
3878 u_int32_t async_code,
3879 struct cam_path *path, void *async_arg)
3881 struct async_node *cur_entry;
3883 cur_entry = SLIST_FIRST(async_head);
3884 while (cur_entry != NULL) {
3885 struct async_node *next_entry;
3887 * Grab the next list entry before we call the current
3888 * entry's callback. This is because the callback function
3889 * can delete its async callback entry.
3891 next_entry = SLIST_NEXT(cur_entry, links);
3892 if ((cur_entry->event_enable & async_code) != 0)
3893 cur_entry->callback(cur_entry->callback_arg,
3896 cur_entry = next_entry;
3901 xpt_dev_async_default(u_int32_t async_code, struct cam_eb *bus,
3902 struct cam_et *target, struct cam_ed *device,
3905 printf("%s called\n", __func__);
3909 xpt_freeze_devq_rl(struct cam_path *path, cam_rl rl, u_int count)
3911 struct cam_ed *dev = path->device;
3913 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3914 dev->sim->devq->alloc_openings +=
3915 cam_ccbq_freeze(&dev->ccbq, rl, count);
3916 /* Remove frozen device from allocq. */
3917 if (device_is_alloc_queued(dev) &&
3918 cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL(
3919 CAMQ_GET_PRIO(&dev->drvq)))) {
3920 camq_remove(&dev->sim->devq->alloc_queue,
3921 dev->alloc_ccb_entry.pinfo.index);
3923 /* Remove frozen device from sendq. */
3924 if (device_is_send_queued(dev) &&
3925 cam_ccbq_frozen_top(&dev->ccbq)) {
3926 camq_remove(&dev->sim->devq->send_queue,
3927 dev->send_ccb_entry.pinfo.index);
3929 return (dev->ccbq.queue.qfrozen_cnt[rl]);
3933 xpt_freeze_devq(struct cam_path *path, u_int count)
3936 return (xpt_freeze_devq_rl(path, 0, count));
3940 xpt_freeze_simq(struct cam_sim *sim, u_int count)
3943 mtx_assert(sim->mtx, MA_OWNED);
3944 sim->devq->send_queue.qfrozen_cnt[0] += count;
3945 return (sim->devq->send_queue.qfrozen_cnt[0]);
3949 xpt_release_devq_timeout(void *arg)
3951 struct cam_ed *device;
3953 device = (struct cam_ed *)arg;
3955 xpt_release_devq_device(device, /*rl*/0, /*count*/1, /*run_queue*/TRUE);
3959 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
3961 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3963 xpt_release_devq_device(path->device, /*rl*/0, count, run_queue);
3967 xpt_release_devq_rl(struct cam_path *path, cam_rl rl, u_int count, int run_queue)
3969 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3971 xpt_release_devq_device(path->device, rl, count, run_queue);
3975 xpt_release_devq_device(struct cam_ed *dev, cam_rl rl, u_int count, int run_queue)
3978 if (count > dev->ccbq.queue.qfrozen_cnt[rl]) {
3980 printf("xpt_release_devq(%d): requested %u > present %u\n",
3981 rl, count, dev->ccbq.queue.qfrozen_cnt[rl]);
3983 count = dev->ccbq.queue.qfrozen_cnt[rl];
3985 dev->sim->devq->alloc_openings -=
3986 cam_ccbq_release(&dev->ccbq, rl, count);
3987 if (cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL(
3988 CAMQ_GET_PRIO(&dev->drvq))) == 0) {
3989 if (xpt_schedule_dev_allocq(dev->target->bus, dev))
3990 xpt_run_dev_allocq(dev->target->bus);
3992 if (cam_ccbq_frozen_top(&dev->ccbq) == 0) {
3994 * No longer need to wait for a successful
3995 * command completion.
3997 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
3999 * Remove any timeouts that might be scheduled
4000 * to release this queue.
4002 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4003 callout_stop(&dev->callout);
4004 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4009 * Now that we are unfrozen schedule the
4010 * device so any pending transactions are
4013 if (xpt_schedule_dev_sendq(dev->target->bus, dev))
4014 xpt_run_dev_sendq(dev->target->bus);
4019 xpt_release_simq(struct cam_sim *sim, int run_queue)
4023 mtx_assert(sim->mtx, MA_OWNED);
4024 sendq = &(sim->devq->send_queue);
4025 if (sendq->qfrozen_cnt[0] <= 0) {
4027 printf("xpt_release_simq: requested 1 > present %u\n",
4028 sendq->qfrozen_cnt[0]);
4031 sendq->qfrozen_cnt[0]--;
4032 if (sendq->qfrozen_cnt[0] == 0) {
4034 * If there is a timeout scheduled to release this
4035 * sim queue, remove it. The queue frozen count is
4038 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4039 callout_stop(&sim->callout);
4040 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4046 * Now that we are unfrozen run the send queue.
4048 bus = xpt_find_bus(sim->path_id);
4049 xpt_run_dev_sendq(bus);
4050 xpt_release_bus(bus);
4056 * XXX Appears to be unused.
4059 xpt_release_simq_timeout(void *arg)
4061 struct cam_sim *sim;
4063 sim = (struct cam_sim *)arg;
4064 xpt_release_simq(sim, /* run_queue */ TRUE);
4068 xpt_done(union ccb *done_ccb)
4070 struct cam_sim *sim;
4073 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4074 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4076 * Queue up the request for handling by our SWI handler
4077 * any of the "non-immediate" type of ccbs.
4079 sim = done_ccb->ccb_h.path->bus->sim;
4080 TAILQ_INSERT_TAIL(&sim->sim_doneq, &done_ccb->ccb_h,
4082 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4083 if ((sim->flags & CAM_SIM_ON_DONEQ) == 0) {
4084 mtx_lock(&cam_simq_lock);
4085 first = TAILQ_EMPTY(&cam_simq);
4086 TAILQ_INSERT_TAIL(&cam_simq, sim, links);
4087 mtx_unlock(&cam_simq_lock);
4088 sim->flags |= CAM_SIM_ON_DONEQ;
4090 swi_sched(cambio_ih, 0);
4100 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_ZERO|M_WAITOK);
4105 xpt_alloc_ccb_nowait()
4109 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_ZERO|M_NOWAIT);
4114 xpt_free_ccb(union ccb *free_ccb)
4116 free(free_ccb, M_CAMXPT);
4121 /* Private XPT functions */
4124 * Get a CAM control block for the caller. Charge the structure to the device
4125 * referenced by the path. If the this device has no 'credits' then the
4126 * device already has the maximum number of outstanding operations under way
4127 * and we return NULL. If we don't have sufficient resources to allocate more
4128 * ccbs, we also return NULL.
4131 xpt_get_ccb(struct cam_ed *device)
4134 struct cam_sim *sim;
4137 if ((new_ccb = (union ccb *)SLIST_FIRST(&sim->ccb_freeq)) == NULL) {
4138 new_ccb = xpt_alloc_ccb_nowait();
4139 if (new_ccb == NULL) {
4142 if ((sim->flags & CAM_SIM_MPSAFE) == 0)
4143 callout_handle_init(&new_ccb->ccb_h.timeout_ch);
4144 SLIST_INSERT_HEAD(&sim->ccb_freeq, &new_ccb->ccb_h,
4148 cam_ccbq_take_opening(&device->ccbq);
4149 SLIST_REMOVE_HEAD(&sim->ccb_freeq, xpt_links.sle);
4154 xpt_release_bus(struct cam_eb *bus)
4157 if ((--bus->refcount == 0)
4158 && (TAILQ_FIRST(&bus->et_entries) == NULL)) {
4159 mtx_lock(&xsoftc.xpt_topo_lock);
4160 TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
4161 xsoftc.bus_generation++;
4162 mtx_unlock(&xsoftc.xpt_topo_lock);
4163 cam_sim_release(bus->sim);
4164 free(bus, M_CAMXPT);
4168 static struct cam_et *
4169 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4171 struct cam_et *target;
4173 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT, M_NOWAIT);
4174 if (target != NULL) {
4175 struct cam_et *cur_target;
4177 TAILQ_INIT(&target->ed_entries);
4179 target->target_id = target_id;
4180 target->refcount = 1;
4181 target->generation = 0;
4182 timevalclear(&target->last_reset);
4184 * Hold a reference to our parent bus so it
4185 * will not go away before we do.
4189 /* Insertion sort into our bus's target list */
4190 cur_target = TAILQ_FIRST(&bus->et_entries);
4191 while (cur_target != NULL && cur_target->target_id < target_id)
4192 cur_target = TAILQ_NEXT(cur_target, links);
4194 if (cur_target != NULL) {
4195 TAILQ_INSERT_BEFORE(cur_target, target, links);
4197 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4205 xpt_release_target(struct cam_et *target)
4208 if ((--target->refcount == 0)
4209 && (TAILQ_FIRST(&target->ed_entries) == NULL)) {
4210 TAILQ_REMOVE(&target->bus->et_entries, target, links);
4211 target->bus->generation++;
4212 xpt_release_bus(target->bus);
4213 free(target, M_CAMXPT);
4217 static struct cam_ed *
4218 xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target,
4221 struct cam_ed *device, *cur_device;
4223 device = xpt_alloc_device(bus, target, lun_id);
4227 device->mintags = 1;
4228 device->maxtags = 1;
4229 bus->sim->max_ccbs += device->ccbq.devq_openings;
4230 cur_device = TAILQ_FIRST(&target->ed_entries);
4231 while (cur_device != NULL && cur_device->lun_id < lun_id)
4232 cur_device = TAILQ_NEXT(cur_device, links);
4233 if (cur_device != NULL) {
4234 TAILQ_INSERT_BEFORE(cur_device, device, links);
4236 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4238 target->generation++;
4244 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4246 struct cam_ed *device;
4247 struct cam_devq *devq;
4250 /* Make space for us in the device queue on our bus */
4251 devq = bus->sim->devq;
4252 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
4254 if (status != CAM_REQ_CMP) {
4257 device = (struct cam_ed *)malloc(sizeof(*device),
4258 M_CAMXPT, M_NOWAIT);
4261 if (device != NULL) {
4262 cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
4263 device->alloc_ccb_entry.device = device;
4264 cam_init_pinfo(&device->send_ccb_entry.pinfo);
4265 device->send_ccb_entry.device = device;
4266 device->target = target;
4267 device->lun_id = lun_id;
4268 device->sim = bus->sim;
4269 /* Initialize our queues */
4270 if (camq_init(&device->drvq, 0) != 0) {
4271 free(device, M_CAMXPT);
4274 if (cam_ccbq_init(&device->ccbq,
4275 bus->sim->max_dev_openings) != 0) {
4276 camq_fini(&device->drvq);
4277 free(device, M_CAMXPT);
4280 SLIST_INIT(&device->asyncs);
4281 SLIST_INIT(&device->periphs);
4282 device->generation = 0;
4283 device->owner = NULL;
4284 device->flags = CAM_DEV_UNCONFIGURED;
4285 device->tag_delay_count = 0;
4286 device->tag_saved_openings = 0;
4287 device->refcount = 1;
4288 callout_init_mtx(&device->callout, bus->sim->mtx, 0);
4291 * Hold a reference to our parent target so it
4292 * will not go away before we do.
4301 xpt_acquire_device(struct cam_ed *device)
4308 xpt_release_device(struct cam_ed *device)
4311 if (--device->refcount == 0) {
4312 struct cam_devq *devq;
4314 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
4315 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
4316 panic("Removing device while still queued for ccbs");
4318 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
4319 callout_stop(&device->callout);
4321 TAILQ_REMOVE(&device->target->ed_entries, device,links);
4322 device->target->generation++;
4323 device->target->bus->sim->max_ccbs -= device->ccbq.devq_openings;
4324 /* Release our slot in the devq */
4325 devq = device->target->bus->sim->devq;
4326 cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
4327 camq_fini(&device->drvq);
4328 cam_ccbq_fini(&device->ccbq);
4329 xpt_release_target(device->target);
4330 free(device, M_CAMXPT);
4335 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
4343 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
4344 result = cam_ccbq_resize(&dev->ccbq, newopenings);
4345 if (result == CAM_REQ_CMP && (diff < 0)) {
4346 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
4348 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
4349 || (dev->inq_flags & SID_CmdQue) != 0)
4350 dev->tag_saved_openings = newopenings;
4351 /* Adjust the global limit */
4352 dev->sim->max_ccbs += diff;
4356 static struct cam_eb *
4357 xpt_find_bus(path_id_t path_id)
4361 mtx_lock(&xsoftc.xpt_topo_lock);
4362 for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4364 bus = TAILQ_NEXT(bus, links)) {
4365 if (bus->path_id == path_id) {
4370 mtx_unlock(&xsoftc.xpt_topo_lock);
4374 static struct cam_et *
4375 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
4377 struct cam_et *target;
4379 for (target = TAILQ_FIRST(&bus->et_entries);
4381 target = TAILQ_NEXT(target, links)) {
4382 if (target->target_id == target_id) {
4390 static struct cam_ed *
4391 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
4393 struct cam_ed *device;
4395 for (device = TAILQ_FIRST(&target->ed_entries);
4397 device = TAILQ_NEXT(device, links)) {
4398 if (device->lun_id == lun_id) {
4407 xpt_start_tags(struct cam_path *path)
4409 struct ccb_relsim crs;
4410 struct cam_ed *device;
4411 struct cam_sim *sim;
4414 device = path->device;
4415 sim = path->bus->sim;
4416 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4417 xpt_freeze_devq(path, /*count*/1);
4418 device->inq_flags |= SID_CmdQue;
4419 if (device->tag_saved_openings != 0)
4420 newopenings = device->tag_saved_openings;
4422 newopenings = min(device->maxtags,
4423 sim->max_tagged_dev_openings);
4424 xpt_dev_ccbq_resize(path, newopenings);
4425 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4426 crs.ccb_h.func_code = XPT_REL_SIMQ;
4427 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4429 = crs.release_timeout
4432 xpt_action((union ccb *)&crs);
4436 xpt_stop_tags(struct cam_path *path)
4438 struct ccb_relsim crs;
4439 struct cam_ed *device;
4440 struct cam_sim *sim;
4442 device = path->device;
4443 sim = path->bus->sim;
4444 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4445 device->tag_delay_count = 0;
4446 xpt_freeze_devq(path, /*count*/1);
4447 device->inq_flags &= ~SID_CmdQue;
4448 xpt_dev_ccbq_resize(path, sim->max_dev_openings);
4449 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4450 crs.ccb_h.func_code = XPT_REL_SIMQ;
4451 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4453 = crs.release_timeout
4456 xpt_action((union ccb *)&crs);
4460 xpt_boot_delay(void *arg)
4467 xpt_config(void *arg)
4470 * Now that interrupts are enabled, go find our devices
4474 /* Setup debugging flags and path */
4475 #ifdef CAM_DEBUG_FLAGS
4476 cam_dflags = CAM_DEBUG_FLAGS;
4477 #else /* !CAM_DEBUG_FLAGS */
4478 cam_dflags = CAM_DEBUG_NONE;
4479 #endif /* CAM_DEBUG_FLAGS */
4480 #ifdef CAM_DEBUG_BUS
4481 if (cam_dflags != CAM_DEBUG_NONE) {
4483 * Locking is specifically omitted here. No SIMs have
4484 * registered yet, so xpt_create_path will only be searching
4485 * empty lists of targets and devices.
4487 if (xpt_create_path(&cam_dpath, xpt_periph,
4488 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
4489 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
4490 printf("xpt_config: xpt_create_path() failed for debug"
4491 " target %d:%d:%d, debugging disabled\n",
4492 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
4493 cam_dflags = CAM_DEBUG_NONE;
4497 #else /* !CAM_DEBUG_BUS */
4499 #endif /* CAM_DEBUG_BUS */
4500 #endif /* CAMDEBUG */
4502 periphdriver_init(1);
4504 callout_init(&xsoftc.boot_callout, 1);
4505 callout_reset(&xsoftc.boot_callout, hz * xsoftc.boot_delay / 1000,
4506 xpt_boot_delay, NULL);
4507 /* Fire up rescan thread. */
4508 if (kproc_create(xpt_scanner_thread, NULL, NULL, 0, 0, "xpt_thrd")) {
4509 printf("xpt_init: failed to create rescan thread\n");
4517 xsoftc.buses_to_config++;
4522 xpt_release_boot(void)
4525 xsoftc.buses_to_config--;
4526 if (xsoftc.buses_to_config == 0 && xsoftc.buses_config_done == 0) {
4527 struct xpt_task *task;
4529 xsoftc.buses_config_done = 1;
4531 /* Call manually because we don't have any busses */
4532 task = malloc(sizeof(struct xpt_task), M_CAMXPT, M_NOWAIT);
4534 TASK_INIT(&task->task, 0, xpt_finishconfig_task, task);
4535 taskqueue_enqueue(taskqueue_thread, &task->task);
4542 * If the given device only has one peripheral attached to it, and if that
4543 * peripheral is the passthrough driver, announce it. This insures that the
4544 * user sees some sort of announcement for every peripheral in their system.
4547 xptpassannouncefunc(struct cam_ed *device, void *arg)
4549 struct cam_periph *periph;
4552 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
4553 periph = SLIST_NEXT(periph, periph_links), i++);
4555 periph = SLIST_FIRST(&device->periphs);
4557 && (strncmp(periph->periph_name, "pass", 4) == 0))
4558 xpt_announce_periph(periph, NULL);
4564 xpt_finishconfig_task(void *context, int pending)
4567 periphdriver_init(2);
4569 * Check for devices with no "standard" peripheral driver
4570 * attached. For any devices like that, announce the
4571 * passthrough driver so the user will see something.
4573 xpt_for_all_devices(xptpassannouncefunc, NULL);
4575 /* Release our hook so that the boot can continue. */
4576 config_intrhook_disestablish(xsoftc.xpt_config_hook);
4577 free(xsoftc.xpt_config_hook, M_CAMXPT);
4578 xsoftc.xpt_config_hook = NULL;
4580 free(context, M_CAMXPT);
4584 xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
4585 struct cam_path *path)
4587 struct ccb_setasync csa;
4592 mtx_lock(&xsoftc.xpt_lock);
4593 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
4594 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4595 if (status != CAM_REQ_CMP) {
4596 mtx_unlock(&xsoftc.xpt_lock);
4602 xpt_setup_ccb(&csa.ccb_h, path, CAM_PRIORITY_NORMAL);
4603 csa.ccb_h.func_code = XPT_SASYNC_CB;
4604 csa.event_enable = event;
4605 csa.callback = cbfunc;
4606 csa.callback_arg = cbarg;
4607 xpt_action((union ccb *)&csa);
4608 status = csa.ccb_h.status;
4610 xpt_free_path(path);
4611 mtx_unlock(&xsoftc.xpt_lock);
4613 if ((status == CAM_REQ_CMP) &&
4614 (csa.event_enable & AC_FOUND_DEVICE)) {
4616 * Get this peripheral up to date with all
4617 * the currently existing devices.
4619 xpt_for_all_devices(xptsetasyncfunc, &csa);
4621 if ((status == CAM_REQ_CMP) &&
4622 (csa.event_enable & AC_PATH_REGISTERED)) {
4624 * Get this peripheral up to date with all
4625 * the currently existing busses.
4627 xpt_for_all_busses(xptsetasyncbusfunc, &csa);
4634 xptaction(struct cam_sim *sim, union ccb *work_ccb)
4636 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
4638 switch (work_ccb->ccb_h.func_code) {
4639 /* Common cases first */
4640 case XPT_PATH_INQ: /* Path routing inquiry */
4642 struct ccb_pathinq *cpi;
4644 cpi = &work_ccb->cpi;
4645 cpi->version_num = 1; /* XXX??? */
4646 cpi->hba_inquiry = 0;
4647 cpi->target_sprt = 0;
4649 cpi->hba_eng_cnt = 0;
4650 cpi->max_target = 0;
4652 cpi->initiator_id = 0;
4653 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
4654 strncpy(cpi->hba_vid, "", HBA_IDLEN);
4655 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
4656 cpi->unit_number = sim->unit_number;
4657 cpi->bus_id = sim->bus_id;
4658 cpi->base_transfer_speed = 0;
4659 cpi->protocol = PROTO_UNSPECIFIED;
4660 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
4661 cpi->transport = XPORT_UNSPECIFIED;
4662 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
4663 cpi->ccb_h.status = CAM_REQ_CMP;
4668 work_ccb->ccb_h.status = CAM_REQ_INVALID;
4675 * The xpt as a "controller" has no interrupt sources, so polling
4679 xptpoll(struct cam_sim *sim)
4684 xpt_lock_buses(void)
4686 mtx_lock(&xsoftc.xpt_topo_lock);
4690 xpt_unlock_buses(void)
4692 mtx_unlock(&xsoftc.xpt_topo_lock);
4699 struct cam_sim *sim;
4701 mtx_lock(&cam_simq_lock);
4703 while (!TAILQ_EMPTY(&cam_simq)) {
4704 TAILQ_CONCAT(&queue, &cam_simq, links);
4705 mtx_unlock(&cam_simq_lock);
4707 while ((sim = TAILQ_FIRST(&queue)) != NULL) {
4708 TAILQ_REMOVE(&queue, sim, links);
4710 sim->flags &= ~CAM_SIM_ON_DONEQ;
4711 camisr_runqueue(&sim->sim_doneq);
4712 CAM_SIM_UNLOCK(sim);
4714 mtx_lock(&cam_simq_lock);
4716 mtx_unlock(&cam_simq_lock);
4720 camisr_runqueue(void *V_queue)
4722 cam_isrq_t *queue = V_queue;
4723 struct ccb_hdr *ccb_h;
4725 while ((ccb_h = TAILQ_FIRST(queue)) != NULL) {
4728 TAILQ_REMOVE(queue, ccb_h, sim_links.tqe);
4729 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
4731 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
4736 if (ccb_h->flags & CAM_HIGH_POWER) {
4737 struct highpowerlist *hphead;
4738 union ccb *send_ccb;
4740 mtx_lock(&xsoftc.xpt_lock);
4741 hphead = &xsoftc.highpowerq;
4743 send_ccb = (union ccb *)STAILQ_FIRST(hphead);
4746 * Increment the count since this command is done.
4748 xsoftc.num_highpower++;
4751 * Any high powered commands queued up?
4753 if (send_ccb != NULL) {
4755 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
4756 mtx_unlock(&xsoftc.xpt_lock);
4758 xpt_release_devq(send_ccb->ccb_h.path,
4759 /*count*/1, /*runqueue*/TRUE);
4761 mtx_unlock(&xsoftc.xpt_lock);
4764 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
4767 dev = ccb_h->path->device;
4769 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
4770 ccb_h->path->bus->sim->devq->send_active--;
4771 ccb_h->path->bus->sim->devq->send_openings++;
4774 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
4775 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)
4776 || ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
4777 && (dev->ccbq.dev_active == 0))) {
4778 xpt_release_devq(ccb_h->path, /*count*/1,
4779 /*run_queue*/FALSE);
4782 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
4783 && (--dev->tag_delay_count == 0))
4784 xpt_start_tags(ccb_h->path);
4785 if (!device_is_send_queued(dev))
4786 xpt_schedule_dev_sendq(ccb_h->path->bus, dev);
4789 if (ccb_h->status & CAM_RELEASE_SIMQ) {
4790 xpt_release_simq(ccb_h->path->bus->sim,
4792 ccb_h->status &= ~CAM_RELEASE_SIMQ;
4796 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
4797 && (ccb_h->status & CAM_DEV_QFRZN)) {
4798 xpt_release_devq(ccb_h->path, /*count*/1,
4800 ccb_h->status &= ~CAM_DEV_QFRZN;
4802 xpt_run_dev_sendq(ccb_h->path->bus);
4805 /* Call the peripheral driver's callback */
4806 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);