2 * Implementation of the Common Access Method Transport (XPT) layer.
4 * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
5 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the following disclaimer,
13 * without modification, immediately at the beginning of the file.
14 * 2. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/types.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
41 #include <sys/fcntl.h>
42 #include <sys/reboot.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 <cam/cam_ccb.h>
54 #include <cam/cam_periph.h>
55 #include <cam/cam_queue.h>
56 #include <cam/cam_sim.h>
57 #include <cam/cam_xpt.h>
58 #include <cam/cam_xpt_sim.h>
59 #include <cam/cam_xpt_periph.h>
60 #include <cam/cam_xpt_internal.h>
61 #include <cam/cam_debug.h>
63 #include <cam/scsi/scsi_all.h>
64 #include <cam/scsi/scsi_message.h>
65 #include <cam/scsi/scsi_pass.h>
67 #include <machine/md_var.h> /* geometry translation */
68 #include <machine/stdarg.h> /* for xpt_print below */
73 * This is the maximum number of high powered commands (e.g. start unit)
74 * that can be outstanding at a particular time.
76 #ifndef CAM_MAX_HIGHPOWER
77 #define CAM_MAX_HIGHPOWER 4
80 /* Datastructures internal to the xpt layer */
81 MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
83 /* Object for defering XPT actions to a taskqueue */
96 u_int32_t xpt_generation;
98 /* number of high powered commands that can go through right now */
99 STAILQ_HEAD(highpowerlist, ccb_hdr) highpowerq;
102 /* queue for handling async rescan requests. */
103 TAILQ_HEAD(, ccb_hdr) ccb_scanq;
105 int buses_config_done;
107 /* Registered busses */
108 TAILQ_HEAD(,cam_eb) xpt_busses;
109 u_int bus_generation;
111 struct intr_config_hook *xpt_config_hook;
114 struct callout boot_callout;
116 struct mtx xpt_topo_lock;
122 DM_RET_FLAG_MASK = 0x0f,
125 DM_RET_DESCEND = 0x20,
127 DM_RET_ACTION_MASK = 0xf0
135 } xpt_traverse_depth;
137 struct xpt_traverse_config {
138 xpt_traverse_depth depth;
143 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
144 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
145 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
146 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
147 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
149 /* Transport layer configuration information */
150 static struct xpt_softc xsoftc;
152 TUNABLE_INT("kern.cam.boot_delay", &xsoftc.boot_delay);
153 SYSCTL_INT(_kern_cam, OID_AUTO, boot_delay, CTLFLAG_RDTUN,
154 &xsoftc.boot_delay, 0, "Bus registration wait time");
156 /* Queues for our software interrupt handler */
157 typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
158 typedef TAILQ_HEAD(cam_simq, cam_sim) cam_simq_t;
159 static cam_simq_t cam_simq;
160 static struct mtx cam_simq_lock;
162 /* Pointers to software interrupt handlers */
163 static void *cambio_ih;
165 struct cam_periph *xpt_periph;
167 static periph_init_t xpt_periph_init;
169 static struct periph_driver xpt_driver =
171 xpt_periph_init, "xpt",
172 TAILQ_HEAD_INITIALIZER(xpt_driver.units), /* generation */ 0,
176 PERIPHDRIVER_DECLARE(xpt, xpt_driver);
178 static d_open_t xptopen;
179 static d_close_t xptclose;
180 static d_ioctl_t xptioctl;
182 static struct cdevsw xpt_cdevsw = {
183 .d_version = D_VERSION,
191 /* Storage for debugging datastructures */
193 struct cam_path *cam_dpath;
194 u_int32_t cam_dflags;
195 u_int32_t cam_debug_delay;
198 /* Our boot-time initialization hook */
199 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
201 static moduledata_t cam_moduledata = {
203 cam_module_event_handler,
207 static int xpt_init(void *);
209 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
210 MODULE_VERSION(cam, 1);
213 static void xpt_async_bcast(struct async_list *async_head,
214 u_int32_t async_code,
215 struct cam_path *path,
217 static path_id_t xptnextfreepathid(void);
218 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
219 static union ccb *xpt_get_ccb(struct cam_ed *device);
220 static void xpt_run_dev_allocq(struct cam_eb *bus);
221 static void xpt_run_dev_sendq(struct cam_eb *bus);
222 static timeout_t xpt_release_devq_timeout;
223 static void xpt_release_simq_timeout(void *arg) __unused;
224 static void xpt_release_bus(struct cam_eb *bus);
225 static void xpt_release_devq_device(struct cam_ed *dev, cam_rl rl,
226 u_int count, int run_queue);
227 static struct cam_et*
228 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
229 static void xpt_release_target(struct cam_et *target);
230 static struct cam_eb*
231 xpt_find_bus(path_id_t path_id);
232 static struct cam_et*
233 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
234 static struct cam_ed*
235 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
236 static void xpt_config(void *arg);
237 static xpt_devicefunc_t xptpassannouncefunc;
238 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
239 static void xptpoll(struct cam_sim *sim);
240 static void camisr(void *);
241 static void camisr_runqueue(void *);
242 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
243 u_int num_patterns, struct cam_eb *bus);
244 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
246 struct cam_ed *device);
247 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
249 struct cam_periph *periph);
250 static xpt_busfunc_t xptedtbusfunc;
251 static xpt_targetfunc_t xptedttargetfunc;
252 static xpt_devicefunc_t xptedtdevicefunc;
253 static xpt_periphfunc_t xptedtperiphfunc;
254 static xpt_pdrvfunc_t xptplistpdrvfunc;
255 static xpt_periphfunc_t xptplistperiphfunc;
256 static int xptedtmatch(struct ccb_dev_match *cdm);
257 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
258 static int xptbustraverse(struct cam_eb *start_bus,
259 xpt_busfunc_t *tr_func, void *arg);
260 static int xpttargettraverse(struct cam_eb *bus,
261 struct cam_et *start_target,
262 xpt_targetfunc_t *tr_func, void *arg);
263 static int xptdevicetraverse(struct cam_et *target,
264 struct cam_ed *start_device,
265 xpt_devicefunc_t *tr_func, void *arg);
266 static int xptperiphtraverse(struct cam_ed *device,
267 struct cam_periph *start_periph,
268 xpt_periphfunc_t *tr_func, void *arg);
269 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
270 xpt_pdrvfunc_t *tr_func, void *arg);
271 static int xptpdperiphtraverse(struct periph_driver **pdrv,
272 struct cam_periph *start_periph,
273 xpt_periphfunc_t *tr_func,
275 static xpt_busfunc_t xptdefbusfunc;
276 static xpt_targetfunc_t xptdeftargetfunc;
277 static xpt_devicefunc_t xptdefdevicefunc;
278 static xpt_periphfunc_t xptdefperiphfunc;
279 static void xpt_finishconfig_task(void *context, int pending);
280 static int xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg);
281 static int xpt_for_all_devices(xpt_devicefunc_t *tr_func,
283 static void xpt_dev_async_default(u_int32_t async_code,
285 struct cam_et *target,
286 struct cam_ed *device,
288 static struct cam_ed * xpt_alloc_device_default(struct cam_eb *bus,
289 struct cam_et *target,
291 static xpt_devicefunc_t xptsetasyncfunc;
292 static xpt_busfunc_t xptsetasyncbusfunc;
293 static cam_status xptregister(struct cam_periph *periph,
295 static __inline int periph_is_queued(struct cam_periph *periph);
296 static __inline int device_is_alloc_queued(struct cam_ed *device);
297 static __inline int device_is_send_queued(struct cam_ed *device);
300 xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev)
304 if ((dev->drvq.entries > 0) &&
305 (dev->ccbq.devq_openings > 0) &&
306 (cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL(
307 CAMQ_GET_PRIO(&dev->drvq))) == 0)) {
309 * The priority of a device waiting for CCB resources
310 * is that of the highest priority peripheral driver
313 retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue,
314 &dev->alloc_ccb_entry.pinfo,
315 CAMQ_GET_PRIO(&dev->drvq));
324 xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev)
328 if ((dev->ccbq.queue.entries > 0) &&
329 (dev->ccbq.dev_openings > 0) &&
330 (cam_ccbq_frozen_top(&dev->ccbq) == 0)) {
332 * The priority of a device waiting for controller
333 * resources is that of the highest priority CCB
337 xpt_schedule_dev(&bus->sim->devq->send_queue,
338 &dev->send_ccb_entry.pinfo,
339 CAMQ_GET_PRIO(&dev->ccbq.queue));
347 periph_is_queued(struct cam_periph *periph)
349 return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
353 device_is_alloc_queued(struct cam_ed *device)
355 return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
359 device_is_send_queued(struct cam_ed *device)
361 return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
367 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
371 xptdone(struct cam_periph *periph, union ccb *done_ccb)
373 /* Caller will release the CCB */
374 wakeup(&done_ccb->ccb_h.cbfcnp);
378 xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
382 * Only allow read-write access.
384 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
388 * We don't allow nonblocking access.
390 if ((flags & O_NONBLOCK) != 0) {
391 printf("%s: can't do nonblocking access\n", devtoname(dev));
395 /* Mark ourselves open */
396 mtx_lock(&xsoftc.xpt_lock);
397 xsoftc.flags |= XPT_FLAG_OPEN;
398 mtx_unlock(&xsoftc.xpt_lock);
404 xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
407 /* Mark ourselves closed */
408 mtx_lock(&xsoftc.xpt_lock);
409 xsoftc.flags &= ~XPT_FLAG_OPEN;
410 mtx_unlock(&xsoftc.xpt_lock);
416 * Don't automatically grab the xpt softc lock here even though this is going
417 * through the xpt device. The xpt device is really just a back door for
418 * accessing other devices and SIMs, so the right thing to do is to grab
419 * the appropriate SIM lock once the bus/SIM is located.
422 xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
430 * For the transport layer CAMIOCOMMAND ioctl, we really only want
431 * to accept CCB types that don't quite make sense to send through a
432 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
440 inccb = (union ccb *)addr;
442 bus = xpt_find_bus(inccb->ccb_h.path_id);
446 switch (inccb->ccb_h.func_code) {
449 if (inccb->ccb_h.target_id != CAM_TARGET_WILDCARD ||
450 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
451 xpt_release_bus(bus);
456 if (inccb->ccb_h.target_id == CAM_TARGET_WILDCARD ||
457 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
458 xpt_release_bus(bus);
466 switch(inccb->ccb_h.func_code) {
474 ccb = xpt_alloc_ccb();
476 CAM_SIM_LOCK(bus->sim);
479 * Create a path using the bus, target, and lun the
482 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
483 inccb->ccb_h.path_id,
484 inccb->ccb_h.target_id,
485 inccb->ccb_h.target_lun) !=
488 CAM_SIM_UNLOCK(bus->sim);
492 /* Ensure all of our fields are correct */
493 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
494 inccb->ccb_h.pinfo.priority);
495 xpt_merge_ccb(ccb, inccb);
496 ccb->ccb_h.cbfcnp = xptdone;
497 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
498 bcopy(ccb, inccb, sizeof(union ccb));
499 xpt_free_path(ccb->ccb_h.path);
501 CAM_SIM_UNLOCK(bus->sim);
508 * This is an immediate CCB, so it's okay to
509 * allocate it on the stack.
512 CAM_SIM_LOCK(bus->sim);
515 * Create a path using the bus, target, and lun the
518 if (xpt_create_path(&ccb.ccb_h.path, xpt_periph,
519 inccb->ccb_h.path_id,
520 inccb->ccb_h.target_id,
521 inccb->ccb_h.target_lun) !=
524 CAM_SIM_UNLOCK(bus->sim);
527 /* Ensure all of our fields are correct */
528 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
529 inccb->ccb_h.pinfo.priority);
530 xpt_merge_ccb(&ccb, inccb);
531 ccb.ccb_h.cbfcnp = xptdone;
533 CAM_SIM_UNLOCK(bus->sim);
534 bcopy(&ccb, inccb, sizeof(union ccb));
535 xpt_free_path(ccb.ccb_h.path);
539 case XPT_DEV_MATCH: {
540 struct cam_periph_map_info mapinfo;
541 struct cam_path *old_path;
544 * We can't deal with physical addresses for this
545 * type of transaction.
547 if (inccb->ccb_h.flags & CAM_DATA_PHYS) {
553 * Save this in case the caller had it set to
554 * something in particular.
556 old_path = inccb->ccb_h.path;
559 * We really don't need a path for the matching
560 * code. The path is needed because of the
561 * debugging statements in xpt_action(). They
562 * assume that the CCB has a valid path.
564 inccb->ccb_h.path = xpt_periph->path;
566 bzero(&mapinfo, sizeof(mapinfo));
569 * Map the pattern and match buffers into kernel
570 * virtual address space.
572 error = cam_periph_mapmem(inccb, &mapinfo);
575 inccb->ccb_h.path = old_path;
580 * This is an immediate CCB, we can send it on directly.
585 * Map the buffers back into user space.
587 cam_periph_unmapmem(inccb, &mapinfo);
589 inccb->ccb_h.path = old_path;
598 xpt_release_bus(bus);
602 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
603 * with the periphal driver name and unit name filled in. The other
604 * fields don't really matter as input. The passthrough driver name
605 * ("pass"), and unit number are passed back in the ccb. The current
606 * device generation number, and the index into the device peripheral
607 * driver list, and the status are also passed back. Note that
608 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
609 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
610 * (or rather should be) impossible for the device peripheral driver
611 * list to change since we look at the whole thing in one pass, and
612 * we do it with lock protection.
615 case CAMGETPASSTHRU: {
617 struct cam_periph *periph;
618 struct periph_driver **p_drv;
621 u_int cur_generation;
622 int base_periph_found;
625 ccb = (union ccb *)addr;
626 unit = ccb->cgdl.unit_number;
627 name = ccb->cgdl.periph_name;
629 * Every 100 devices, we want to drop our lock protection to
630 * give the software interrupt handler a chance to run.
631 * Most systems won't run into this check, but this should
632 * avoid starvation in the software interrupt handler in
637 ccb = (union ccb *)addr;
639 base_periph_found = 0;
642 * Sanity check -- make sure we don't get a null peripheral
645 if (*ccb->cgdl.periph_name == '\0') {
650 /* Keep the list from changing while we traverse it */
651 mtx_lock(&xsoftc.xpt_topo_lock);
653 cur_generation = xsoftc.xpt_generation;
655 /* first find our driver in the list of drivers */
656 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
657 if (strcmp((*p_drv)->driver_name, name) == 0)
660 if (*p_drv == NULL) {
661 mtx_unlock(&xsoftc.xpt_topo_lock);
662 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
663 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
664 *ccb->cgdl.periph_name = '\0';
665 ccb->cgdl.unit_number = 0;
671 * Run through every peripheral instance of this driver
672 * and check to see whether it matches the unit passed
673 * in by the user. If it does, get out of the loops and
674 * find the passthrough driver associated with that
677 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
678 periph = TAILQ_NEXT(periph, unit_links)) {
680 if (periph->unit_number == unit) {
682 } else if (--splbreaknum == 0) {
683 mtx_unlock(&xsoftc.xpt_topo_lock);
684 mtx_lock(&xsoftc.xpt_topo_lock);
686 if (cur_generation != xsoftc.xpt_generation)
691 * If we found the peripheral driver that the user passed
692 * in, go through all of the peripheral drivers for that
693 * particular device and look for a passthrough driver.
695 if (periph != NULL) {
696 struct cam_ed *device;
699 base_periph_found = 1;
700 device = periph->path->device;
701 for (i = 0, periph = SLIST_FIRST(&device->periphs);
703 periph = SLIST_NEXT(periph, periph_links), i++) {
705 * Check to see whether we have a
706 * passthrough device or not.
708 if (strcmp(periph->periph_name, "pass") == 0) {
710 * Fill in the getdevlist fields.
712 strcpy(ccb->cgdl.periph_name,
713 periph->periph_name);
714 ccb->cgdl.unit_number =
716 if (SLIST_NEXT(periph, periph_links))
718 CAM_GDEVLIST_MORE_DEVS;
721 CAM_GDEVLIST_LAST_DEVICE;
722 ccb->cgdl.generation =
726 * Fill in some CCB header fields
727 * that the user may want.
730 periph->path->bus->path_id;
731 ccb->ccb_h.target_id =
732 periph->path->target->target_id;
733 ccb->ccb_h.target_lun =
734 periph->path->device->lun_id;
735 ccb->ccb_h.status = CAM_REQ_CMP;
742 * If the periph is null here, one of two things has
743 * happened. The first possibility is that we couldn't
744 * find the unit number of the particular peripheral driver
745 * that the user is asking about. e.g. the user asks for
746 * the passthrough driver for "da11". We find the list of
747 * "da" peripherals all right, but there is no unit 11.
748 * The other possibility is that we went through the list
749 * of peripheral drivers attached to the device structure,
750 * but didn't find one with the name "pass". Either way,
751 * we return ENOENT, since we couldn't find something.
753 if (periph == NULL) {
754 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
755 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
756 *ccb->cgdl.periph_name = '\0';
757 ccb->cgdl.unit_number = 0;
760 * It is unfortunate that this is even necessary,
761 * but there are many, many clueless users out there.
762 * If this is true, the user is looking for the
763 * passthrough driver, but doesn't have one in his
766 if (base_periph_found == 1) {
767 printf("xptioctl: pass driver is not in the "
769 printf("xptioctl: put \"device pass\" in "
770 "your kernel config file\n");
773 mtx_unlock(&xsoftc.xpt_topo_lock);
785 cam_module_event_handler(module_t mod, int what, void *arg)
791 if ((error = xpt_init(NULL)) != 0)
804 xpt_rescan_done(struct cam_periph *periph, union ccb *done_ccb)
807 if (done_ccb->ccb_h.ppriv_ptr1 == NULL) {
808 xpt_free_path(done_ccb->ccb_h.path);
809 xpt_free_ccb(done_ccb);
811 done_ccb->ccb_h.cbfcnp = done_ccb->ccb_h.ppriv_ptr1;
812 (*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
817 /* thread to handle bus rescans */
819 xpt_scanner_thread(void *dummy)
826 if (TAILQ_EMPTY(&xsoftc.ccb_scanq))
827 msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO,
829 if ((ccb = (union ccb *)TAILQ_FIRST(&xsoftc.ccb_scanq)) != NULL) {
830 TAILQ_REMOVE(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
833 sim = ccb->ccb_h.path->bus->sim;
844 xpt_rescan(union ccb *ccb)
848 /* Prepare request */
849 if (ccb->ccb_h.path->target->target_id == CAM_TARGET_WILDCARD &&
850 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
851 ccb->ccb_h.func_code = XPT_SCAN_BUS;
852 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
853 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
854 ccb->ccb_h.func_code = XPT_SCAN_TGT;
855 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
856 ccb->ccb_h.path->device->lun_id != CAM_LUN_WILDCARD)
857 ccb->ccb_h.func_code = XPT_SCAN_LUN;
859 xpt_print(ccb->ccb_h.path, "illegal scan path\n");
860 xpt_free_path(ccb->ccb_h.path);
864 ccb->ccb_h.ppriv_ptr1 = ccb->ccb_h.cbfcnp;
865 ccb->ccb_h.cbfcnp = xpt_rescan_done;
866 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, CAM_PRIORITY_XPT);
867 /* Don't make duplicate entries for the same paths. */
869 if (ccb->ccb_h.ppriv_ptr1 == NULL) {
870 TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) {
871 if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) {
872 wakeup(&xsoftc.ccb_scanq);
874 xpt_print(ccb->ccb_h.path, "rescan already queued\n");
875 xpt_free_path(ccb->ccb_h.path);
881 TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
882 xsoftc.buses_to_config++;
883 wakeup(&xsoftc.ccb_scanq);
887 /* Functions accessed by the peripheral drivers */
889 xpt_init(void *dummy)
891 struct cam_sim *xpt_sim;
892 struct cam_path *path;
893 struct cam_devq *devq;
896 TAILQ_INIT(&xsoftc.xpt_busses);
897 TAILQ_INIT(&cam_simq);
898 TAILQ_INIT(&xsoftc.ccb_scanq);
899 STAILQ_INIT(&xsoftc.highpowerq);
900 xsoftc.num_highpower = CAM_MAX_HIGHPOWER;
902 mtx_init(&cam_simq_lock, "CAM SIMQ lock", NULL, MTX_DEF);
903 mtx_init(&xsoftc.xpt_lock, "XPT lock", NULL, MTX_DEF);
904 mtx_init(&xsoftc.xpt_topo_lock, "XPT topology lock", NULL, MTX_DEF);
907 * The xpt layer is, itself, the equivelent of a SIM.
908 * Allow 16 ccbs in the ccb pool for it. This should
909 * give decent parallelism when we probe busses and
910 * perform other XPT functions.
912 devq = cam_simq_alloc(16);
913 xpt_sim = cam_sim_alloc(xptaction,
918 /*mtx*/&xsoftc.xpt_lock,
919 /*max_dev_transactions*/0,
920 /*max_tagged_dev_transactions*/0,
925 mtx_lock(&xsoftc.xpt_lock);
926 if ((status = xpt_bus_register(xpt_sim, NULL, 0)) != CAM_SUCCESS) {
927 mtx_unlock(&xsoftc.xpt_lock);
928 printf("xpt_init: xpt_bus_register failed with status %#x,"
929 " failing attach\n", status);
934 * Looking at the XPT from the SIM layer, the XPT is
935 * the equivelent of a peripheral driver. Allocate
936 * a peripheral driver entry for us.
938 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
940 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
941 mtx_unlock(&xsoftc.xpt_lock);
942 printf("xpt_init: xpt_create_path failed with status %#x,"
943 " failing attach\n", status);
947 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
948 path, NULL, 0, xpt_sim);
950 mtx_unlock(&xsoftc.xpt_lock);
951 /* Install our software interrupt handlers */
952 swi_add(NULL, "cambio", camisr, NULL, SWI_CAMBIO, INTR_MPSAFE, &cambio_ih);
954 * Register a callback for when interrupts are enabled.
956 xsoftc.xpt_config_hook =
957 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
958 M_CAMXPT, M_NOWAIT | M_ZERO);
959 if (xsoftc.xpt_config_hook == NULL) {
960 printf("xpt_init: Cannot malloc config hook "
961 "- failing attach\n");
964 xsoftc.xpt_config_hook->ich_func = xpt_config;
965 if (config_intrhook_establish(xsoftc.xpt_config_hook) != 0) {
966 free (xsoftc.xpt_config_hook, M_CAMXPT);
967 printf("xpt_init: config_intrhook_establish failed "
968 "- failing attach\n");
975 xptregister(struct cam_periph *periph, void *arg)
977 struct cam_sim *xpt_sim;
979 if (periph == NULL) {
980 printf("xptregister: periph was NULL!!\n");
981 return(CAM_REQ_CMP_ERR);
984 xpt_sim = (struct cam_sim *)arg;
985 xpt_sim->softc = periph;
987 periph->softc = NULL;
993 xpt_add_periph(struct cam_periph *periph)
995 struct cam_ed *device;
997 struct periph_list *periph_head;
999 mtx_assert(periph->sim->mtx, MA_OWNED);
1001 device = periph->path->device;
1003 periph_head = &device->periphs;
1005 status = CAM_REQ_CMP;
1007 if (device != NULL) {
1009 * Make room for this peripheral
1010 * so it will fit in the queue
1011 * when it's scheduled to run
1013 status = camq_resize(&device->drvq,
1014 device->drvq.array_size + 1);
1016 device->generation++;
1018 SLIST_INSERT_HEAD(periph_head, periph, periph_links);
1021 mtx_lock(&xsoftc.xpt_topo_lock);
1022 xsoftc.xpt_generation++;
1023 mtx_unlock(&xsoftc.xpt_topo_lock);
1029 xpt_remove_periph(struct cam_periph *periph)
1031 struct cam_ed *device;
1033 mtx_assert(periph->sim->mtx, MA_OWNED);
1035 device = periph->path->device;
1037 if (device != NULL) {
1038 struct periph_list *periph_head;
1040 periph_head = &device->periphs;
1042 /* Release the slot for this peripheral */
1043 camq_resize(&device->drvq, device->drvq.array_size - 1);
1045 device->generation++;
1047 SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
1050 mtx_lock(&xsoftc.xpt_topo_lock);
1051 xsoftc.xpt_generation++;
1052 mtx_unlock(&xsoftc.xpt_topo_lock);
1057 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1059 struct cam_path *path = periph->path;
1061 mtx_assert(periph->sim->mtx, MA_OWNED);
1063 printf("%s%d at %s%d bus %d scbus%d target %d lun %d\n",
1064 periph->periph_name, periph->unit_number,
1065 path->bus->sim->sim_name,
1066 path->bus->sim->unit_number,
1067 path->bus->sim->bus_id,
1069 path->target->target_id,
1070 path->device->lun_id);
1071 printf("%s%d: ", periph->periph_name, periph->unit_number);
1072 if (path->device->protocol == PROTO_SCSI)
1073 scsi_print_inquiry(&path->device->inq_data);
1074 else if (path->device->protocol == PROTO_ATA ||
1075 path->device->protocol == PROTO_SATAPM)
1076 ata_print_ident(&path->device->ident_data);
1078 printf("Unknown protocol device\n");
1079 if (bootverbose && path->device->serial_num_len > 0) {
1080 /* Don't wrap the screen - print only the first 60 chars */
1081 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1082 periph->unit_number, path->device->serial_num);
1084 /* Announce transport details. */
1085 (*(path->bus->xport->announce))(periph);
1086 /* Announce command queueing. */
1087 if (path->device->inq_flags & SID_CmdQue
1088 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1089 printf("%s%d: Command Queueing enabled\n",
1090 periph->periph_name, periph->unit_number);
1092 /* Announce caller's details if they've passed in. */
1093 if (announce_string != NULL)
1094 printf("%s%d: %s\n", periph->periph_name,
1095 periph->unit_number, announce_string);
1098 static dev_match_ret
1099 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1102 dev_match_ret retval;
1105 retval = DM_RET_NONE;
1108 * If we aren't given something to match against, that's an error.
1111 return(DM_RET_ERROR);
1114 * If there are no match entries, then this bus matches no
1117 if ((patterns == NULL) || (num_patterns == 0))
1118 return(DM_RET_DESCEND | DM_RET_COPY);
1120 for (i = 0; i < num_patterns; i++) {
1121 struct bus_match_pattern *cur_pattern;
1124 * If the pattern in question isn't for a bus node, we
1125 * aren't interested. However, we do indicate to the
1126 * calling routine that we should continue descending the
1127 * tree, since the user wants to match against lower-level
1130 if (patterns[i].type != DEV_MATCH_BUS) {
1131 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1132 retval |= DM_RET_DESCEND;
1136 cur_pattern = &patterns[i].pattern.bus_pattern;
1139 * If they want to match any bus node, we give them any
1142 if (cur_pattern->flags == BUS_MATCH_ANY) {
1143 /* set the copy flag */
1144 retval |= DM_RET_COPY;
1147 * If we've already decided on an action, go ahead
1150 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1155 * Not sure why someone would do this...
1157 if (cur_pattern->flags == BUS_MATCH_NONE)
1160 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1161 && (cur_pattern->path_id != bus->path_id))
1164 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1165 && (cur_pattern->bus_id != bus->sim->bus_id))
1168 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1169 && (cur_pattern->unit_number != bus->sim->unit_number))
1172 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1173 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1178 * If we get to this point, the user definitely wants
1179 * information on this bus. So tell the caller to copy the
1182 retval |= DM_RET_COPY;
1185 * If the return action has been set to descend, then we
1186 * know that we've already seen a non-bus matching
1187 * expression, therefore we need to further descend the tree.
1188 * This won't change by continuing around the loop, so we
1189 * go ahead and return. If we haven't seen a non-bus
1190 * matching expression, we keep going around the loop until
1191 * we exhaust the matching expressions. We'll set the stop
1192 * flag once we fall out of the loop.
1194 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1199 * If the return action hasn't been set to descend yet, that means
1200 * we haven't seen anything other than bus matching patterns. So
1201 * tell the caller to stop descending the tree -- the user doesn't
1202 * want to match against lower level tree elements.
1204 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1205 retval |= DM_RET_STOP;
1210 static dev_match_ret
1211 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1212 struct cam_ed *device)
1214 dev_match_ret retval;
1217 retval = DM_RET_NONE;
1220 * If we aren't given something to match against, that's an error.
1223 return(DM_RET_ERROR);
1226 * If there are no match entries, then this device matches no
1229 if ((patterns == NULL) || (num_patterns == 0))
1230 return(DM_RET_DESCEND | DM_RET_COPY);
1232 for (i = 0; i < num_patterns; i++) {
1233 struct device_match_pattern *cur_pattern;
1236 * If the pattern in question isn't for a device node, we
1237 * aren't interested.
1239 if (patterns[i].type != DEV_MATCH_DEVICE) {
1240 if ((patterns[i].type == DEV_MATCH_PERIPH)
1241 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1242 retval |= DM_RET_DESCEND;
1246 cur_pattern = &patterns[i].pattern.device_pattern;
1249 * If they want to match any device node, we give them any
1252 if (cur_pattern->flags == DEV_MATCH_ANY) {
1253 /* set the copy flag */
1254 retval |= DM_RET_COPY;
1258 * If we've already decided on an action, go ahead
1261 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1266 * Not sure why someone would do this...
1268 if (cur_pattern->flags == DEV_MATCH_NONE)
1271 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1272 && (cur_pattern->path_id != device->target->bus->path_id))
1275 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1276 && (cur_pattern->target_id != device->target->target_id))
1279 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1280 && (cur_pattern->target_lun != device->lun_id))
1283 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1284 && (cam_quirkmatch((caddr_t)&device->inq_data,
1285 (caddr_t)&cur_pattern->inq_pat,
1286 1, sizeof(cur_pattern->inq_pat),
1287 scsi_static_inquiry_match) == NULL))
1291 * If we get to this point, the user definitely wants
1292 * information on this device. So tell the caller to copy
1295 retval |= DM_RET_COPY;
1298 * If the return action has been set to descend, then we
1299 * know that we've already seen a peripheral matching
1300 * expression, therefore we need to further descend the tree.
1301 * This won't change by continuing around the loop, so we
1302 * go ahead and return. If we haven't seen a peripheral
1303 * matching expression, we keep going around the loop until
1304 * we exhaust the matching expressions. We'll set the stop
1305 * flag once we fall out of the loop.
1307 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1312 * If the return action hasn't been set to descend yet, that means
1313 * we haven't seen any peripheral matching patterns. So tell the
1314 * caller to stop descending the tree -- the user doesn't want to
1315 * match against lower level tree elements.
1317 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1318 retval |= DM_RET_STOP;
1324 * Match a single peripheral against any number of match patterns.
1326 static dev_match_ret
1327 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1328 struct cam_periph *periph)
1330 dev_match_ret retval;
1334 * If we aren't given something to match against, that's an error.
1337 return(DM_RET_ERROR);
1340 * If there are no match entries, then this peripheral matches no
1343 if ((patterns == NULL) || (num_patterns == 0))
1344 return(DM_RET_STOP | DM_RET_COPY);
1347 * There aren't any nodes below a peripheral node, so there's no
1348 * reason to descend the tree any further.
1350 retval = DM_RET_STOP;
1352 for (i = 0; i < num_patterns; i++) {
1353 struct periph_match_pattern *cur_pattern;
1356 * If the pattern in question isn't for a peripheral, we
1357 * aren't interested.
1359 if (patterns[i].type != DEV_MATCH_PERIPH)
1362 cur_pattern = &patterns[i].pattern.periph_pattern;
1365 * If they want to match on anything, then we will do so.
1367 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1368 /* set the copy flag */
1369 retval |= DM_RET_COPY;
1372 * We've already set the return action to stop,
1373 * since there are no nodes below peripherals in
1380 * Not sure why someone would do this...
1382 if (cur_pattern->flags == PERIPH_MATCH_NONE)
1385 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1386 && (cur_pattern->path_id != periph->path->bus->path_id))
1390 * For the target and lun id's, we have to make sure the
1391 * target and lun pointers aren't NULL. The xpt peripheral
1392 * has a wildcard target and device.
1394 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1395 && ((periph->path->target == NULL)
1396 ||(cur_pattern->target_id != periph->path->target->target_id)))
1399 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1400 && ((periph->path->device == NULL)
1401 || (cur_pattern->target_lun != periph->path->device->lun_id)))
1404 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
1405 && (cur_pattern->unit_number != periph->unit_number))
1408 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
1409 && (strncmp(cur_pattern->periph_name, periph->periph_name,
1414 * If we get to this point, the user definitely wants
1415 * information on this peripheral. So tell the caller to
1416 * copy the data out.
1418 retval |= DM_RET_COPY;
1421 * The return action has already been set to stop, since
1422 * peripherals don't have any nodes below them in the EDT.
1428 * If we get to this point, the peripheral that was passed in
1429 * doesn't match any of the patterns.
1435 xptedtbusfunc(struct cam_eb *bus, void *arg)
1437 struct ccb_dev_match *cdm;
1438 dev_match_ret retval;
1440 cdm = (struct ccb_dev_match *)arg;
1443 * If our position is for something deeper in the tree, that means
1444 * that we've already seen this node. So, we keep going down.
1446 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1447 && (cdm->pos.cookie.bus == bus)
1448 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1449 && (cdm->pos.cookie.target != NULL))
1450 retval = DM_RET_DESCEND;
1452 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
1455 * If we got an error, bail out of the search.
1457 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1458 cdm->status = CAM_DEV_MATCH_ERROR;
1463 * If the copy flag is set, copy this bus out.
1465 if (retval & DM_RET_COPY) {
1468 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1469 sizeof(struct dev_match_result));
1472 * If we don't have enough space to put in another
1473 * match result, save our position and tell the
1474 * user there are more devices to check.
1476 if (spaceleft < sizeof(struct dev_match_result)) {
1477 bzero(&cdm->pos, sizeof(cdm->pos));
1478 cdm->pos.position_type =
1479 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
1481 cdm->pos.cookie.bus = bus;
1482 cdm->pos.generations[CAM_BUS_GENERATION]=
1483 xsoftc.bus_generation;
1484 cdm->status = CAM_DEV_MATCH_MORE;
1487 j = cdm->num_matches;
1489 cdm->matches[j].type = DEV_MATCH_BUS;
1490 cdm->matches[j].result.bus_result.path_id = bus->path_id;
1491 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
1492 cdm->matches[j].result.bus_result.unit_number =
1493 bus->sim->unit_number;
1494 strncpy(cdm->matches[j].result.bus_result.dev_name,
1495 bus->sim->sim_name, DEV_IDLEN);
1499 * If the user is only interested in busses, there's no
1500 * reason to descend to the next level in the tree.
1502 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1506 * If there is a target generation recorded, check it to
1507 * make sure the target list hasn't changed.
1509 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1510 && (bus == cdm->pos.cookie.bus)
1511 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1512 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
1513 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
1515 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1519 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1520 && (cdm->pos.cookie.bus == bus)
1521 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1522 && (cdm->pos.cookie.target != NULL))
1523 return(xpttargettraverse(bus,
1524 (struct cam_et *)cdm->pos.cookie.target,
1525 xptedttargetfunc, arg));
1527 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
1531 xptedttargetfunc(struct cam_et *target, void *arg)
1533 struct ccb_dev_match *cdm;
1535 cdm = (struct ccb_dev_match *)arg;
1538 * If there is a device list generation recorded, check it to
1539 * make sure the device list hasn't changed.
1541 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1542 && (cdm->pos.cookie.bus == target->bus)
1543 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1544 && (cdm->pos.cookie.target == target)
1545 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1546 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
1547 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
1548 target->generation)) {
1549 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1553 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1554 && (cdm->pos.cookie.bus == target->bus)
1555 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1556 && (cdm->pos.cookie.target == target)
1557 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1558 && (cdm->pos.cookie.device != NULL))
1559 return(xptdevicetraverse(target,
1560 (struct cam_ed *)cdm->pos.cookie.device,
1561 xptedtdevicefunc, arg));
1563 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
1567 xptedtdevicefunc(struct cam_ed *device, void *arg)
1570 struct ccb_dev_match *cdm;
1571 dev_match_ret retval;
1573 cdm = (struct ccb_dev_match *)arg;
1576 * If our position is for something deeper in the tree, that means
1577 * that we've already seen this node. So, we keep going down.
1579 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1580 && (cdm->pos.cookie.device == device)
1581 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1582 && (cdm->pos.cookie.periph != NULL))
1583 retval = DM_RET_DESCEND;
1585 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
1588 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1589 cdm->status = CAM_DEV_MATCH_ERROR;
1594 * If the copy flag is set, copy this device out.
1596 if (retval & DM_RET_COPY) {
1599 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1600 sizeof(struct dev_match_result));
1603 * If we don't have enough space to put in another
1604 * match result, save our position and tell the
1605 * user there are more devices to check.
1607 if (spaceleft < sizeof(struct dev_match_result)) {
1608 bzero(&cdm->pos, sizeof(cdm->pos));
1609 cdm->pos.position_type =
1610 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1611 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
1613 cdm->pos.cookie.bus = device->target->bus;
1614 cdm->pos.generations[CAM_BUS_GENERATION]=
1615 xsoftc.bus_generation;
1616 cdm->pos.cookie.target = device->target;
1617 cdm->pos.generations[CAM_TARGET_GENERATION] =
1618 device->target->bus->generation;
1619 cdm->pos.cookie.device = device;
1620 cdm->pos.generations[CAM_DEV_GENERATION] =
1621 device->target->generation;
1622 cdm->status = CAM_DEV_MATCH_MORE;
1625 j = cdm->num_matches;
1627 cdm->matches[j].type = DEV_MATCH_DEVICE;
1628 cdm->matches[j].result.device_result.path_id =
1629 device->target->bus->path_id;
1630 cdm->matches[j].result.device_result.target_id =
1631 device->target->target_id;
1632 cdm->matches[j].result.device_result.target_lun =
1634 cdm->matches[j].result.device_result.protocol =
1636 bcopy(&device->inq_data,
1637 &cdm->matches[j].result.device_result.inq_data,
1638 sizeof(struct scsi_inquiry_data));
1639 bcopy(&device->ident_data,
1640 &cdm->matches[j].result.device_result.ident_data,
1641 sizeof(struct ata_params));
1643 /* Let the user know whether this device is unconfigured */
1644 if (device->flags & CAM_DEV_UNCONFIGURED)
1645 cdm->matches[j].result.device_result.flags =
1646 DEV_RESULT_UNCONFIGURED;
1648 cdm->matches[j].result.device_result.flags =
1653 * If the user isn't interested in peripherals, don't descend
1654 * the tree any further.
1656 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1660 * If there is a peripheral list generation recorded, make sure
1661 * it hasn't changed.
1663 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1664 && (device->target->bus == cdm->pos.cookie.bus)
1665 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1666 && (device->target == cdm->pos.cookie.target)
1667 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1668 && (device == cdm->pos.cookie.device)
1669 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1670 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
1671 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1672 device->generation)){
1673 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1677 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1678 && (cdm->pos.cookie.bus == device->target->bus)
1679 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1680 && (cdm->pos.cookie.target == device->target)
1681 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1682 && (cdm->pos.cookie.device == device)
1683 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1684 && (cdm->pos.cookie.periph != NULL))
1685 return(xptperiphtraverse(device,
1686 (struct cam_periph *)cdm->pos.cookie.periph,
1687 xptedtperiphfunc, arg));
1689 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
1693 xptedtperiphfunc(struct cam_periph *periph, void *arg)
1695 struct ccb_dev_match *cdm;
1696 dev_match_ret retval;
1698 cdm = (struct ccb_dev_match *)arg;
1700 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1702 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1703 cdm->status = CAM_DEV_MATCH_ERROR;
1708 * If the copy flag is set, copy this peripheral out.
1710 if (retval & DM_RET_COPY) {
1713 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1714 sizeof(struct dev_match_result));
1717 * If we don't have enough space to put in another
1718 * match result, save our position and tell the
1719 * user there are more devices to check.
1721 if (spaceleft < sizeof(struct dev_match_result)) {
1722 bzero(&cdm->pos, sizeof(cdm->pos));
1723 cdm->pos.position_type =
1724 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1725 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
1728 cdm->pos.cookie.bus = periph->path->bus;
1729 cdm->pos.generations[CAM_BUS_GENERATION]=
1730 xsoftc.bus_generation;
1731 cdm->pos.cookie.target = periph->path->target;
1732 cdm->pos.generations[CAM_TARGET_GENERATION] =
1733 periph->path->bus->generation;
1734 cdm->pos.cookie.device = periph->path->device;
1735 cdm->pos.generations[CAM_DEV_GENERATION] =
1736 periph->path->target->generation;
1737 cdm->pos.cookie.periph = periph;
1738 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1739 periph->path->device->generation;
1740 cdm->status = CAM_DEV_MATCH_MORE;
1744 j = cdm->num_matches;
1746 cdm->matches[j].type = DEV_MATCH_PERIPH;
1747 cdm->matches[j].result.periph_result.path_id =
1748 periph->path->bus->path_id;
1749 cdm->matches[j].result.periph_result.target_id =
1750 periph->path->target->target_id;
1751 cdm->matches[j].result.periph_result.target_lun =
1752 periph->path->device->lun_id;
1753 cdm->matches[j].result.periph_result.unit_number =
1754 periph->unit_number;
1755 strncpy(cdm->matches[j].result.periph_result.periph_name,
1756 periph->periph_name, DEV_IDLEN);
1763 xptedtmatch(struct ccb_dev_match *cdm)
1767 cdm->num_matches = 0;
1770 * Check the bus list generation. If it has changed, the user
1771 * needs to reset everything and start over.
1773 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1774 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
1775 && (cdm->pos.generations[CAM_BUS_GENERATION] != xsoftc.bus_generation)) {
1776 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1780 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1781 && (cdm->pos.cookie.bus != NULL))
1782 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
1783 xptedtbusfunc, cdm);
1785 ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
1788 * If we get back 0, that means that we had to stop before fully
1789 * traversing the EDT. It also means that one of the subroutines
1790 * has set the status field to the proper value. If we get back 1,
1791 * we've fully traversed the EDT and copied out any matching entries.
1794 cdm->status = CAM_DEV_MATCH_LAST;
1800 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
1802 struct ccb_dev_match *cdm;
1804 cdm = (struct ccb_dev_match *)arg;
1806 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1807 && (cdm->pos.cookie.pdrv == pdrv)
1808 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1809 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
1810 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1811 (*pdrv)->generation)) {
1812 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1816 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1817 && (cdm->pos.cookie.pdrv == pdrv)
1818 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1819 && (cdm->pos.cookie.periph != NULL))
1820 return(xptpdperiphtraverse(pdrv,
1821 (struct cam_periph *)cdm->pos.cookie.periph,
1822 xptplistperiphfunc, arg));
1824 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
1828 xptplistperiphfunc(struct cam_periph *periph, void *arg)
1830 struct ccb_dev_match *cdm;
1831 dev_match_ret retval;
1833 cdm = (struct ccb_dev_match *)arg;
1835 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1837 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1838 cdm->status = CAM_DEV_MATCH_ERROR;
1843 * If the copy flag is set, copy this peripheral out.
1845 if (retval & DM_RET_COPY) {
1848 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1849 sizeof(struct dev_match_result));
1852 * If we don't have enough space to put in another
1853 * match result, save our position and tell the
1854 * user there are more devices to check.
1856 if (spaceleft < sizeof(struct dev_match_result)) {
1857 struct periph_driver **pdrv;
1860 bzero(&cdm->pos, sizeof(cdm->pos));
1861 cdm->pos.position_type =
1862 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
1866 * This may look a bit non-sensical, but it is
1867 * actually quite logical. There are very few
1868 * peripheral drivers, and bloating every peripheral
1869 * structure with a pointer back to its parent
1870 * peripheral driver linker set entry would cost
1871 * more in the long run than doing this quick lookup.
1873 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
1874 if (strcmp((*pdrv)->driver_name,
1875 periph->periph_name) == 0)
1879 if (*pdrv == NULL) {
1880 cdm->status = CAM_DEV_MATCH_ERROR;
1884 cdm->pos.cookie.pdrv = pdrv;
1886 * The periph generation slot does double duty, as
1887 * does the periph pointer slot. They are used for
1888 * both edt and pdrv lookups and positioning.
1890 cdm->pos.cookie.periph = periph;
1891 cdm->pos.generations[CAM_PERIPH_GENERATION] =
1892 (*pdrv)->generation;
1893 cdm->status = CAM_DEV_MATCH_MORE;
1897 j = cdm->num_matches;
1899 cdm->matches[j].type = DEV_MATCH_PERIPH;
1900 cdm->matches[j].result.periph_result.path_id =
1901 periph->path->bus->path_id;
1904 * The transport layer peripheral doesn't have a target or
1907 if (periph->path->target)
1908 cdm->matches[j].result.periph_result.target_id =
1909 periph->path->target->target_id;
1911 cdm->matches[j].result.periph_result.target_id = -1;
1913 if (periph->path->device)
1914 cdm->matches[j].result.periph_result.target_lun =
1915 periph->path->device->lun_id;
1917 cdm->matches[j].result.periph_result.target_lun = -1;
1919 cdm->matches[j].result.periph_result.unit_number =
1920 periph->unit_number;
1921 strncpy(cdm->matches[j].result.periph_result.periph_name,
1922 periph->periph_name, DEV_IDLEN);
1929 xptperiphlistmatch(struct ccb_dev_match *cdm)
1933 cdm->num_matches = 0;
1936 * At this point in the edt traversal function, we check the bus
1937 * list generation to make sure that no busses have been added or
1938 * removed since the user last sent a XPT_DEV_MATCH ccb through.
1939 * For the peripheral driver list traversal function, however, we
1940 * don't have to worry about new peripheral driver types coming or
1941 * going; they're in a linker set, and therefore can't change
1942 * without a recompile.
1945 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
1946 && (cdm->pos.cookie.pdrv != NULL))
1947 ret = xptpdrvtraverse(
1948 (struct periph_driver **)cdm->pos.cookie.pdrv,
1949 xptplistpdrvfunc, cdm);
1951 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
1954 * If we get back 0, that means that we had to stop before fully
1955 * traversing the peripheral driver tree. It also means that one of
1956 * the subroutines has set the status field to the proper value. If
1957 * we get back 1, we've fully traversed the EDT and copied out any
1961 cdm->status = CAM_DEV_MATCH_LAST;
1967 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
1969 struct cam_eb *bus, *next_bus;
1974 mtx_lock(&xsoftc.xpt_topo_lock);
1975 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xsoftc.xpt_busses));
1982 * XXX The locking here is obviously very complex. We
1983 * should work to simplify it.
1985 mtx_unlock(&xsoftc.xpt_topo_lock);
1986 CAM_SIM_LOCK(bus->sim);
1987 retval = tr_func(bus, arg);
1988 CAM_SIM_UNLOCK(bus->sim);
1990 mtx_lock(&xsoftc.xpt_topo_lock);
1991 next_bus = TAILQ_NEXT(bus, links);
1992 mtx_unlock(&xsoftc.xpt_topo_lock);
1994 xpt_release_bus(bus);
1998 mtx_lock(&xsoftc.xpt_topo_lock);
2000 mtx_unlock(&xsoftc.xpt_topo_lock);
2006 xpt_sim_opened(struct cam_sim *sim)
2009 struct cam_et *target;
2010 struct cam_ed *device;
2011 struct cam_periph *periph;
2013 KASSERT(sim->refcount >= 1, ("sim->refcount >= 1"));
2014 mtx_assert(sim->mtx, MA_OWNED);
2016 mtx_lock(&xsoftc.xpt_topo_lock);
2017 TAILQ_FOREACH(bus, &xsoftc.xpt_busses, links) {
2018 if (bus->sim != sim)
2021 TAILQ_FOREACH(target, &bus->et_entries, links) {
2022 TAILQ_FOREACH(device, &target->ed_entries, links) {
2023 SLIST_FOREACH(periph, &device->periphs,
2025 if (periph->refcount > 0) {
2026 mtx_unlock(&xsoftc.xpt_topo_lock);
2034 mtx_unlock(&xsoftc.xpt_topo_lock);
2039 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2040 xpt_targetfunc_t *tr_func, void *arg)
2042 struct cam_et *target, *next_target;
2046 for (target = (start_target ? start_target :
2047 TAILQ_FIRST(&bus->et_entries));
2048 target != NULL; target = next_target) {
2052 retval = tr_func(target, arg);
2054 next_target = TAILQ_NEXT(target, links);
2056 xpt_release_target(target);
2066 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2067 xpt_devicefunc_t *tr_func, void *arg)
2069 struct cam_ed *device, *next_device;
2073 for (device = (start_device ? start_device :
2074 TAILQ_FIRST(&target->ed_entries));
2076 device = next_device) {
2079 * Hold a reference so the current device does not go away
2084 retval = tr_func(device, arg);
2087 * Grab our next pointer before we release the current
2090 next_device = TAILQ_NEXT(device, links);
2092 xpt_release_device(device);
2102 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2103 xpt_periphfunc_t *tr_func, void *arg)
2105 struct cam_periph *periph, *next_periph;
2111 for (periph = (start_periph ? start_periph :
2112 SLIST_FIRST(&device->periphs));
2114 periph = next_periph) {
2118 * In this case, we want to show peripherals that have been
2119 * invalidated, but not peripherals that are scheduled to
2120 * be freed. So instead of calling cam_periph_acquire(),
2121 * which will fail if the periph has been invalidated, we
2122 * just check for the free flag here. If it is free, we
2123 * skip to the next periph.
2125 if (periph->flags & CAM_PERIPH_FREE) {
2126 next_periph = SLIST_NEXT(periph, periph_links);
2131 * Acquire a reference to this periph while we call the
2132 * traversal function, so it can't go away.
2138 retval = tr_func(periph, arg);
2141 * We need the lock for list traversal.
2146 * Grab the next peripheral before we release this one, so
2147 * our next pointer is still valid.
2149 next_periph = SLIST_NEXT(periph, periph_links);
2151 cam_periph_release_locked_buses(periph);
2165 xptpdrvtraverse(struct periph_driver **start_pdrv,
2166 xpt_pdrvfunc_t *tr_func, void *arg)
2168 struct periph_driver **pdrv;
2174 * We don't traverse the peripheral driver list like we do the
2175 * other lists, because it is a linker set, and therefore cannot be
2176 * changed during runtime. If the peripheral driver list is ever
2177 * re-done to be something other than a linker set (i.e. it can
2178 * change while the system is running), the list traversal should
2179 * be modified to work like the other traversal functions.
2181 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2182 *pdrv != NULL; pdrv++) {
2183 retval = tr_func(pdrv, arg);
2193 xptpdperiphtraverse(struct periph_driver **pdrv,
2194 struct cam_periph *start_periph,
2195 xpt_periphfunc_t *tr_func, void *arg)
2197 struct cam_periph *periph, *next_periph;
2203 for (periph = (start_periph ? start_periph :
2204 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2205 periph = next_periph) {
2209 * In this case, we want to show peripherals that have been
2210 * invalidated, but not peripherals that are scheduled to
2211 * be freed. So instead of calling cam_periph_acquire(),
2212 * which will fail if the periph has been invalidated, we
2213 * just check for the free flag here. If it is free, we
2214 * skip to the next periph.
2216 if (periph->flags & CAM_PERIPH_FREE) {
2217 next_periph = TAILQ_NEXT(periph, unit_links);
2222 * Acquire a reference to this periph while we call the
2223 * traversal function, so it can't go away.
2228 * XXX KDM we have the toplogy lock here, but in
2229 * xptperiphtraverse(), we drop it before calling the
2230 * traversal function. Which is correct?
2232 retval = tr_func(periph, arg);
2235 * Grab the next peripheral before we release this one, so
2236 * our next pointer is still valid.
2238 next_periph = TAILQ_NEXT(periph, unit_links);
2240 cam_periph_release_locked_buses(periph);
2253 xptdefbusfunc(struct cam_eb *bus, void *arg)
2255 struct xpt_traverse_config *tr_config;
2257 tr_config = (struct xpt_traverse_config *)arg;
2259 if (tr_config->depth == XPT_DEPTH_BUS) {
2260 xpt_busfunc_t *tr_func;
2262 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2264 return(tr_func(bus, tr_config->tr_arg));
2266 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2270 xptdeftargetfunc(struct cam_et *target, void *arg)
2272 struct xpt_traverse_config *tr_config;
2274 tr_config = (struct xpt_traverse_config *)arg;
2276 if (tr_config->depth == XPT_DEPTH_TARGET) {
2277 xpt_targetfunc_t *tr_func;
2279 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2281 return(tr_func(target, tr_config->tr_arg));
2283 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2287 xptdefdevicefunc(struct cam_ed *device, void *arg)
2289 struct xpt_traverse_config *tr_config;
2291 tr_config = (struct xpt_traverse_config *)arg;
2293 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2294 xpt_devicefunc_t *tr_func;
2296 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2298 return(tr_func(device, tr_config->tr_arg));
2300 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2304 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2306 struct xpt_traverse_config *tr_config;
2307 xpt_periphfunc_t *tr_func;
2309 tr_config = (struct xpt_traverse_config *)arg;
2311 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2314 * Unlike the other default functions, we don't check for depth
2315 * here. The peripheral driver level is the last level in the EDT,
2316 * so if we're here, we should execute the function in question.
2318 return(tr_func(periph, tr_config->tr_arg));
2322 * Execute the given function for every bus in the EDT.
2325 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2327 struct xpt_traverse_config tr_config;
2329 tr_config.depth = XPT_DEPTH_BUS;
2330 tr_config.tr_func = tr_func;
2331 tr_config.tr_arg = arg;
2333 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2337 * Execute the given function for every device in the EDT.
2340 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2342 struct xpt_traverse_config tr_config;
2344 tr_config.depth = XPT_DEPTH_DEVICE;
2345 tr_config.tr_func = tr_func;
2346 tr_config.tr_arg = arg;
2348 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2352 xptsetasyncfunc(struct cam_ed *device, void *arg)
2354 struct cam_path path;
2355 struct ccb_getdev cgd;
2356 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2359 * Don't report unconfigured devices (Wildcard devs,
2360 * devices only for target mode, device instances
2361 * that have been invalidated but are waiting for
2362 * their last reference count to be released).
2364 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2367 xpt_compile_path(&path,
2369 device->target->bus->path_id,
2370 device->target->target_id,
2372 xpt_setup_ccb(&cgd.ccb_h, &path, CAM_PRIORITY_NORMAL);
2373 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2374 xpt_action((union ccb *)&cgd);
2375 csa->callback(csa->callback_arg,
2378 xpt_release_path(&path);
2384 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2386 struct cam_path path;
2387 struct ccb_pathinq cpi;
2388 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2390 xpt_compile_path(&path, /*periph*/NULL,
2392 CAM_TARGET_WILDCARD,
2394 xpt_setup_ccb(&cpi.ccb_h, &path, CAM_PRIORITY_NORMAL);
2395 cpi.ccb_h.func_code = XPT_PATH_INQ;
2396 xpt_action((union ccb *)&cpi);
2397 csa->callback(csa->callback_arg,
2400 xpt_release_path(&path);
2406 xpt_action(union ccb *start_ccb)
2409 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2411 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2412 /* Compatibility for RL-unaware code. */
2413 if (CAM_PRIORITY_TO_RL(start_ccb->ccb_h.pinfo.priority) == 0)
2414 start_ccb->ccb_h.pinfo.priority += CAM_PRIORITY_NORMAL - 1;
2415 (*(start_ccb->ccb_h.path->bus->xport->action))(start_ccb);
2419 xpt_action_default(union ccb *start_ccb)
2422 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
2424 struct cam_path *path;
2426 path = start_ccb->ccb_h.path;
2427 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_action_default\n"));
2429 switch (start_ccb->ccb_h.func_code) {
2432 struct cam_ed *device;
2435 * For the sake of compatibility with SCSI-1
2436 * devices that may not understand the identify
2437 * message, we include lun information in the
2438 * second byte of all commands. SCSI-1 specifies
2439 * that luns are a 3 bit value and reserves only 3
2440 * bits for lun information in the CDB. Later
2441 * revisions of the SCSI spec allow for more than 8
2442 * luns, but have deprecated lun information in the
2443 * CDB. So, if the lun won't fit, we must omit.
2445 * Also be aware that during initial probing for devices,
2446 * the inquiry information is unknown but initialized to 0.
2447 * This means that this code will be exercised while probing
2448 * devices with an ANSI revision greater than 2.
2450 device = path->device;
2451 if (device->protocol_version <= SCSI_REV_2
2452 && start_ccb->ccb_h.target_lun < 8
2453 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2455 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2456 start_ccb->ccb_h.target_lun << 5;
2458 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2459 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n",
2460 scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0],
2461 &path->device->inq_data),
2462 scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes,
2463 cdb_str, sizeof(cdb_str))));
2467 case XPT_CONT_TARGET_IO:
2468 start_ccb->csio.sense_resid = 0;
2469 start_ccb->csio.resid = 0;
2472 if (start_ccb->ccb_h.func_code == XPT_ATA_IO) {
2473 start_ccb->ataio.resid = 0;
2474 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. ACB: %s\n",
2475 ata_op_string(&start_ccb->ataio.cmd),
2476 ata_cmd_string(&start_ccb->ataio.cmd,
2477 cdb_str, sizeof(cdb_str))));
2485 frozen = cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2486 path->device->sim->devq->alloc_openings += frozen;
2488 xpt_run_dev_allocq(path->bus);
2489 if (xpt_schedule_dev_sendq(path->bus, path->device))
2490 xpt_run_dev_sendq(path->bus);
2493 case XPT_CALC_GEOMETRY:
2495 struct cam_sim *sim;
2497 /* Filter out garbage */
2498 if (start_ccb->ccg.block_size == 0
2499 || start_ccb->ccg.volume_size == 0) {
2500 start_ccb->ccg.cylinders = 0;
2501 start_ccb->ccg.heads = 0;
2502 start_ccb->ccg.secs_per_track = 0;
2503 start_ccb->ccb_h.status = CAM_REQ_CMP;
2506 #if defined(PC98) || defined(__sparc64__)
2508 * In a PC-98 system, geometry translation depens on
2509 * the "real" device geometry obtained from mode page 4.
2510 * SCSI geometry translation is performed in the
2511 * initialization routine of the SCSI BIOS and the result
2512 * stored in host memory. If the translation is available
2513 * in host memory, use it. If not, rely on the default
2514 * translation the device driver performs.
2515 * For sparc64, we may need adjust the geometry of large
2516 * disks in order to fit the limitations of the 16-bit
2517 * fields of the VTOC8 disk label.
2519 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
2520 start_ccb->ccb_h.status = CAM_REQ_CMP;
2524 sim = path->bus->sim;
2525 (*(sim->sim_action))(sim, start_ccb);
2530 union ccb* abort_ccb;
2532 abort_ccb = start_ccb->cab.abort_ccb;
2533 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
2535 if (abort_ccb->ccb_h.pinfo.index >= 0) {
2536 struct cam_ccbq *ccbq;
2537 struct cam_ed *device;
2539 device = abort_ccb->ccb_h.path->device;
2540 ccbq = &device->ccbq;
2541 device->sim->devq->alloc_openings -=
2542 cam_ccbq_remove_ccb(ccbq, abort_ccb);
2543 abort_ccb->ccb_h.status =
2544 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2545 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2546 xpt_done(abort_ccb);
2547 start_ccb->ccb_h.status = CAM_REQ_CMP;
2550 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
2551 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
2553 * We've caught this ccb en route to
2554 * the SIM. Flag it for abort and the
2555 * SIM will do so just before starting
2556 * real work on the CCB.
2558 abort_ccb->ccb_h.status =
2559 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2560 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2561 start_ccb->ccb_h.status = CAM_REQ_CMP;
2565 if (XPT_FC_IS_QUEUED(abort_ccb)
2566 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
2568 * It's already completed but waiting
2569 * for our SWI to get to it.
2571 start_ccb->ccb_h.status = CAM_UA_ABORT;
2575 * If we weren't able to take care of the abort request
2576 * in the XPT, pass the request down to the SIM for processing.
2580 case XPT_ACCEPT_TARGET_IO:
2582 case XPT_IMMED_NOTIFY:
2583 case XPT_NOTIFY_ACK:
2585 case XPT_IMMEDIATE_NOTIFY:
2586 case XPT_NOTIFY_ACKNOWLEDGE:
2587 case XPT_GET_SIM_KNOB:
2588 case XPT_SET_SIM_KNOB:
2590 struct cam_sim *sim;
2592 sim = path->bus->sim;
2593 (*(sim->sim_action))(sim, start_ccb);
2598 struct cam_sim *sim;
2600 sim = path->bus->sim;
2601 (*(sim->sim_action))(sim, start_ccb);
2604 case XPT_PATH_STATS:
2605 start_ccb->cpis.last_reset = path->bus->last_reset;
2606 start_ccb->ccb_h.status = CAM_REQ_CMP;
2613 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2614 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2616 struct ccb_getdev *cgd;
2618 cgd = &start_ccb->cgd;
2619 cgd->protocol = dev->protocol;
2620 cgd->inq_data = dev->inq_data;
2621 cgd->ident_data = dev->ident_data;
2622 cgd->inq_flags = dev->inq_flags;
2623 cgd->ccb_h.status = CAM_REQ_CMP;
2624 cgd->serial_num_len = dev->serial_num_len;
2625 if ((dev->serial_num_len > 0)
2626 && (dev->serial_num != NULL))
2627 bcopy(dev->serial_num, cgd->serial_num,
2628 dev->serial_num_len);
2632 case XPT_GDEV_STATS:
2637 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2638 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2640 struct ccb_getdevstats *cgds;
2644 cgds = &start_ccb->cgds;
2647 cgds->dev_openings = dev->ccbq.dev_openings;
2648 cgds->dev_active = dev->ccbq.dev_active;
2649 cgds->devq_openings = dev->ccbq.devq_openings;
2650 cgds->devq_queued = dev->ccbq.queue.entries;
2651 cgds->held = dev->ccbq.held;
2652 cgds->last_reset = tar->last_reset;
2653 cgds->maxtags = dev->maxtags;
2654 cgds->mintags = dev->mintags;
2655 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
2656 cgds->last_reset = bus->last_reset;
2657 cgds->ccb_h.status = CAM_REQ_CMP;
2663 struct cam_periph *nperiph;
2664 struct periph_list *periph_head;
2665 struct ccb_getdevlist *cgdl;
2667 struct cam_ed *device;
2674 * Don't want anyone mucking with our data.
2676 device = path->device;
2677 periph_head = &device->periphs;
2678 cgdl = &start_ccb->cgdl;
2681 * Check and see if the list has changed since the user
2682 * last requested a list member. If so, tell them that the
2683 * list has changed, and therefore they need to start over
2684 * from the beginning.
2686 if ((cgdl->index != 0) &&
2687 (cgdl->generation != device->generation)) {
2688 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
2693 * Traverse the list of peripherals and attempt to find
2694 * the requested peripheral.
2696 for (nperiph = SLIST_FIRST(periph_head), i = 0;
2697 (nperiph != NULL) && (i <= cgdl->index);
2698 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
2699 if (i == cgdl->index) {
2700 strncpy(cgdl->periph_name,
2701 nperiph->periph_name,
2703 cgdl->unit_number = nperiph->unit_number;
2708 cgdl->status = CAM_GDEVLIST_ERROR;
2712 if (nperiph == NULL)
2713 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
2715 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
2718 cgdl->generation = device->generation;
2720 cgdl->ccb_h.status = CAM_REQ_CMP;
2725 dev_pos_type position_type;
2726 struct ccb_dev_match *cdm;
2728 cdm = &start_ccb->cdm;
2731 * There are two ways of getting at information in the EDT.
2732 * The first way is via the primary EDT tree. It starts
2733 * with a list of busses, then a list of targets on a bus,
2734 * then devices/luns on a target, and then peripherals on a
2735 * device/lun. The "other" way is by the peripheral driver
2736 * lists. The peripheral driver lists are organized by
2737 * peripheral driver. (obviously) So it makes sense to
2738 * use the peripheral driver list if the user is looking
2739 * for something like "da1", or all "da" devices. If the
2740 * user is looking for something on a particular bus/target
2741 * or lun, it's generally better to go through the EDT tree.
2744 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
2745 position_type = cdm->pos.position_type;
2749 position_type = CAM_DEV_POS_NONE;
2751 for (i = 0; i < cdm->num_patterns; i++) {
2752 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
2753 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
2754 position_type = CAM_DEV_POS_EDT;
2759 if (cdm->num_patterns == 0)
2760 position_type = CAM_DEV_POS_EDT;
2761 else if (position_type == CAM_DEV_POS_NONE)
2762 position_type = CAM_DEV_POS_PDRV;
2765 switch(position_type & CAM_DEV_POS_TYPEMASK) {
2766 case CAM_DEV_POS_EDT:
2769 case CAM_DEV_POS_PDRV:
2770 xptperiphlistmatch(cdm);
2773 cdm->status = CAM_DEV_MATCH_ERROR;
2777 if (cdm->status == CAM_DEV_MATCH_ERROR)
2778 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2780 start_ccb->ccb_h.status = CAM_REQ_CMP;
2786 struct ccb_setasync *csa;
2787 struct async_node *cur_entry;
2788 struct async_list *async_head;
2791 csa = &start_ccb->csa;
2792 added = csa->event_enable;
2793 async_head = &path->device->asyncs;
2796 * If there is already an entry for us, simply
2799 cur_entry = SLIST_FIRST(async_head);
2800 while (cur_entry != NULL) {
2801 if ((cur_entry->callback_arg == csa->callback_arg)
2802 && (cur_entry->callback == csa->callback))
2804 cur_entry = SLIST_NEXT(cur_entry, links);
2807 if (cur_entry != NULL) {
2809 * If the request has no flags set,
2812 added &= ~cur_entry->event_enable;
2813 if (csa->event_enable == 0) {
2814 SLIST_REMOVE(async_head, cur_entry,
2816 xpt_release_device(path->device);
2817 free(cur_entry, M_CAMXPT);
2819 cur_entry->event_enable = csa->event_enable;
2821 csa->event_enable = added;
2823 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
2825 if (cur_entry == NULL) {
2826 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
2829 cur_entry->event_enable = csa->event_enable;
2830 cur_entry->callback_arg = csa->callback_arg;
2831 cur_entry->callback = csa->callback;
2832 SLIST_INSERT_HEAD(async_head, cur_entry, links);
2833 xpt_acquire_device(path->device);
2835 start_ccb->ccb_h.status = CAM_REQ_CMP;
2840 struct ccb_relsim *crs;
2843 crs = &start_ccb->crs;
2847 crs->ccb_h.status = CAM_DEV_NOT_THERE;
2851 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
2853 if (INQ_DATA_TQ_ENABLED(&dev->inq_data)) {
2854 /* Don't ever go below one opening */
2855 if (crs->openings > 0) {
2856 xpt_dev_ccbq_resize(path,
2861 "tagged openings now %d\n",
2868 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
2870 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
2873 * Just extend the old timeout and decrement
2874 * the freeze count so that a single timeout
2875 * is sufficient for releasing the queue.
2877 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2878 callout_stop(&dev->callout);
2881 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2884 callout_reset(&dev->callout,
2885 (crs->release_timeout * hz) / 1000,
2886 xpt_release_devq_timeout, dev);
2888 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
2892 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
2894 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
2896 * Decrement the freeze count so that a single
2897 * completion is still sufficient to unfreeze
2900 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2903 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
2904 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2908 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
2910 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
2911 || (dev->ccbq.dev_active == 0)) {
2913 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
2916 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
2917 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
2921 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
2922 xpt_release_devq_rl(path, /*runlevel*/
2923 (crs->release_flags & RELSIM_RELEASE_RUNLEVEL) ?
2924 crs->release_timeout : 0,
2925 /*count*/1, /*run_queue*/TRUE);
2927 start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt[0];
2928 start_ccb->ccb_h.status = CAM_REQ_CMP;
2933 #ifdef CAM_DEBUG_DELAY
2934 cam_debug_delay = CAM_DEBUG_DELAY;
2936 cam_dflags = start_ccb->cdbg.flags;
2937 if (cam_dpath != NULL) {
2938 xpt_free_path(cam_dpath);
2942 if (cam_dflags != CAM_DEBUG_NONE) {
2943 if (xpt_create_path(&cam_dpath, xpt_periph,
2944 start_ccb->ccb_h.path_id,
2945 start_ccb->ccb_h.target_id,
2946 start_ccb->ccb_h.target_lun) !=
2948 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2949 cam_dflags = CAM_DEBUG_NONE;
2951 start_ccb->ccb_h.status = CAM_REQ_CMP;
2952 xpt_print(cam_dpath, "debugging flags now %x\n",
2957 start_ccb->ccb_h.status = CAM_REQ_CMP;
2959 #else /* !CAMDEBUG */
2960 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2961 #endif /* CAMDEBUG */
2964 case XPT_FREEZE_QUEUE:
2966 struct ccb_relsim *crs = &start_ccb->crs;
2968 xpt_freeze_devq_rl(path, /*runlevel*/
2969 (crs->release_flags & RELSIM_RELEASE_RUNLEVEL) ?
2970 crs->release_timeout : 0, /*count*/1);
2971 start_ccb->ccb_h.status = CAM_REQ_CMP;
2975 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
2976 xpt_freeze_devq(path, 1);
2977 start_ccb->ccb_h.status = CAM_REQ_CMP;
2984 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
2985 if (start_ccb->ccb_h.func_code & XPT_FC_DEV_QUEUED) {
2986 xpt_done(start_ccb);
2993 xpt_polled_action(union ccb *start_ccb)
2996 struct cam_sim *sim;
2997 struct cam_devq *devq;
3001 timeout = start_ccb->ccb_h.timeout * 10;
3002 sim = start_ccb->ccb_h.path->bus->sim;
3004 dev = start_ccb->ccb_h.path->device;
3006 mtx_assert(sim->mtx, MA_OWNED);
3008 /* Don't use ISR for this SIM while polling. */
3009 sim->flags |= CAM_SIM_POLLED;
3012 * Steal an opening so that no other queued requests
3013 * can get it before us while we simulate interrupts.
3015 dev->ccbq.devq_openings--;
3016 dev->ccbq.dev_openings--;
3018 while(((devq != NULL && devq->send_openings <= 0) ||
3019 dev->ccbq.dev_openings < 0) && (--timeout > 0)) {
3021 (*(sim->sim_poll))(sim);
3022 camisr_runqueue(&sim->sim_doneq);
3025 dev->ccbq.devq_openings++;
3026 dev->ccbq.dev_openings++;
3029 xpt_action(start_ccb);
3030 while(--timeout > 0) {
3031 (*(sim->sim_poll))(sim);
3032 camisr_runqueue(&sim->sim_doneq);
3033 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3040 * XXX Is it worth adding a sim_timeout entry
3041 * point so we can attempt recovery? If
3042 * this is only used for dumps, I don't think
3045 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3048 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3051 /* We will use CAM ISR for this SIM again. */
3052 sim->flags &= ~CAM_SIM_POLLED;
3056 * Schedule a peripheral driver to receive a ccb when it's
3057 * target device has space for more transactions.
3060 xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3062 struct cam_ed *device;
3065 mtx_assert(perph->sim->mtx, MA_OWNED);
3067 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3068 device = perph->path->device;
3069 if (periph_is_queued(perph)) {
3070 /* Simply reorder based on new priority */
3071 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3072 (" change priority to %d\n", new_priority));
3073 if (new_priority < perph->pinfo.priority) {
3074 camq_change_priority(&device->drvq,
3077 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3080 /* New entry on the queue */
3081 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3082 (" added periph to queue\n"));
3083 perph->pinfo.priority = new_priority;
3084 perph->pinfo.generation = ++device->drvq.generation;
3085 camq_insert(&device->drvq, &perph->pinfo);
3086 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3089 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3090 (" calling xpt_run_devq\n"));
3091 xpt_run_dev_allocq(perph->path->bus);
3097 * Schedule a device to run on a given queue.
3098 * If the device was inserted as a new entry on the queue,
3099 * return 1 meaning the device queue should be run. If we
3100 * were already queued, implying someone else has already
3101 * started the queue, return 0 so the caller doesn't attempt
3105 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3106 u_int32_t new_priority)
3109 u_int32_t old_priority;
3111 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3113 old_priority = pinfo->priority;
3116 * Are we already queued?
3118 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3119 /* Simply reorder based on new priority */
3120 if (new_priority < old_priority) {
3121 camq_change_priority(queue, pinfo->index,
3123 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3124 ("changed priority to %d\n",
3130 /* New entry on the queue */
3131 if (new_priority < old_priority)
3132 pinfo->priority = new_priority;
3134 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3135 ("Inserting onto queue\n"));
3136 pinfo->generation = ++queue->generation;
3137 camq_insert(queue, pinfo);
3144 xpt_run_dev_allocq(struct cam_eb *bus)
3146 struct cam_devq *devq;
3148 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
3149 devq = bus->sim->devq;
3151 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3152 (" qfrozen_cnt == 0x%x, entries == %d, "
3153 "openings == %d, active == %d\n",
3154 devq->alloc_queue.qfrozen_cnt[0],
3155 devq->alloc_queue.entries,
3156 devq->alloc_openings,
3157 devq->alloc_active));
3159 devq->alloc_queue.qfrozen_cnt[0]++;
3160 while ((devq->alloc_queue.entries > 0)
3161 && (devq->alloc_openings > 0)
3162 && (devq->alloc_queue.qfrozen_cnt[0] <= 1)) {
3163 struct cam_ed_qinfo *qinfo;
3164 struct cam_ed *device;
3165 union ccb *work_ccb;
3166 struct cam_periph *drv;
3169 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3171 device = qinfo->device;
3172 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3173 ("running device %p\n", device));
3175 drvq = &device->drvq;
3178 if (drvq->entries <= 0) {
3179 panic("xpt_run_dev_allocq: "
3180 "Device on queue without any work to do");
3183 if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3184 devq->alloc_openings--;
3185 devq->alloc_active++;
3186 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3187 xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3188 drv->pinfo.priority);
3189 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3190 ("calling periph start\n"));
3191 drv->periph_start(drv, work_ccb);
3194 * Malloc failure in alloc_ccb
3197 * XXX add us to a list to be run from free_ccb
3198 * if we don't have any ccbs active on this
3199 * device queue otherwise we may never get run
3205 /* We may have more work. Attempt to reschedule. */
3206 xpt_schedule_dev_allocq(bus, device);
3208 devq->alloc_queue.qfrozen_cnt[0]--;
3212 xpt_run_dev_sendq(struct cam_eb *bus)
3214 struct cam_devq *devq;
3216 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
3218 devq = bus->sim->devq;
3220 devq->send_queue.qfrozen_cnt[0]++;
3221 while ((devq->send_queue.entries > 0)
3222 && (devq->send_openings > 0)
3223 && (devq->send_queue.qfrozen_cnt[0] <= 1)) {
3224 struct cam_ed_qinfo *qinfo;
3225 struct cam_ed *device;
3226 union ccb *work_ccb;
3227 struct cam_sim *sim;
3229 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3231 device = qinfo->device;
3232 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3233 ("running device %p\n", device));
3235 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3236 if (work_ccb == NULL) {
3237 printf("device on run queue with no ccbs???\n");
3241 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3243 mtx_lock(&xsoftc.xpt_lock);
3244 if (xsoftc.num_highpower <= 0) {
3246 * We got a high power command, but we
3247 * don't have any available slots. Freeze
3248 * the device queue until we have a slot
3251 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3252 STAILQ_INSERT_TAIL(&xsoftc.highpowerq,
3256 mtx_unlock(&xsoftc.xpt_lock);
3260 * Consume a high power slot while
3263 xsoftc.num_highpower--;
3265 mtx_unlock(&xsoftc.xpt_lock);
3267 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3268 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3270 devq->send_openings--;
3271 devq->send_active++;
3273 xpt_schedule_dev_sendq(bus, device);
3275 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
3277 * The client wants to freeze the queue
3278 * after this CCB is sent.
3280 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3283 /* In Target mode, the peripheral driver knows best... */
3284 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3285 if ((device->inq_flags & SID_CmdQue) != 0
3286 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3287 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3290 * Clear this in case of a retried CCB that
3291 * failed due to a rejected tag.
3293 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3297 * Device queues can be shared among multiple sim instances
3298 * that reside on different busses. Use the SIM in the queue
3299 * CCB's path, rather than the one in the bus that was passed
3300 * into this function.
3302 sim = work_ccb->ccb_h.path->bus->sim;
3303 (*(sim->sim_action))(sim, work_ccb);
3305 devq->send_queue.qfrozen_cnt[0]--;
3309 * This function merges stuff from the slave ccb into the master ccb, while
3310 * keeping important fields in the master ccb constant.
3313 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3317 * Pull fields that are valid for peripheral drivers to set
3318 * into the master CCB along with the CCB "payload".
3320 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3321 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3322 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3323 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3324 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3325 sizeof(union ccb) - sizeof(struct ccb_hdr));
3329 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3332 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3333 ccb_h->pinfo.priority = priority;
3335 ccb_h->path_id = path->bus->path_id;
3337 ccb_h->target_id = path->target->target_id;
3339 ccb_h->target_id = CAM_TARGET_WILDCARD;
3341 ccb_h->target_lun = path->device->lun_id;
3342 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3344 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3346 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3350 /* Path manipulation functions */
3352 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3353 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3355 struct cam_path *path;
3358 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_NOWAIT);
3361 status = CAM_RESRC_UNAVAIL;
3364 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3365 if (status != CAM_REQ_CMP) {
3366 free(path, M_CAMXPT);
3369 *new_path_ptr = path;
3374 xpt_create_path_unlocked(struct cam_path **new_path_ptr,
3375 struct cam_periph *periph, path_id_t path_id,
3376 target_id_t target_id, lun_id_t lun_id)
3378 struct cam_path *path;
3379 struct cam_eb *bus = NULL;
3381 int need_unlock = 0;
3383 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_WAITOK);
3385 if (path_id != CAM_BUS_WILDCARD) {
3386 bus = xpt_find_bus(path_id);
3389 CAM_SIM_LOCK(bus->sim);
3392 status = xpt_compile_path(path, periph, path_id, target_id, lun_id);
3394 CAM_SIM_UNLOCK(bus->sim);
3395 if (status != CAM_REQ_CMP) {
3396 free(path, M_CAMXPT);
3399 *new_path_ptr = path;
3404 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3405 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3408 struct cam_et *target;
3409 struct cam_ed *device;
3412 status = CAM_REQ_CMP; /* Completed without error */
3413 target = NULL; /* Wildcarded */
3414 device = NULL; /* Wildcarded */
3417 * We will potentially modify the EDT, so block interrupts
3418 * that may attempt to create cam paths.
3420 bus = xpt_find_bus(path_id);
3422 status = CAM_PATH_INVALID;
3424 target = xpt_find_target(bus, target_id);
3425 if (target == NULL) {
3427 struct cam_et *new_target;
3429 new_target = xpt_alloc_target(bus, target_id);
3430 if (new_target == NULL) {
3431 status = CAM_RESRC_UNAVAIL;
3433 target = new_target;
3436 if (target != NULL) {
3437 device = xpt_find_device(target, lun_id);
3438 if (device == NULL) {
3440 struct cam_ed *new_device;
3443 (*(bus->xport->alloc_device))(bus,
3446 if (new_device == NULL) {
3447 status = CAM_RESRC_UNAVAIL;
3449 device = new_device;
3456 * Only touch the user's data if we are successful.
3458 if (status == CAM_REQ_CMP) {
3459 new_path->periph = perph;
3460 new_path->bus = bus;
3461 new_path->target = target;
3462 new_path->device = device;
3463 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
3466 xpt_release_device(device);
3468 xpt_release_target(target);
3470 xpt_release_bus(bus);
3476 xpt_release_path(struct cam_path *path)
3478 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
3479 if (path->device != NULL) {
3480 xpt_release_device(path->device);
3481 path->device = NULL;
3483 if (path->target != NULL) {
3484 xpt_release_target(path->target);
3485 path->target = NULL;
3487 if (path->bus != NULL) {
3488 xpt_release_bus(path->bus);
3494 xpt_free_path(struct cam_path *path)
3497 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
3498 xpt_release_path(path);
3499 free(path, M_CAMXPT);
3504 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
3505 * in path1, 2 for match with wildcards in path2.
3508 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
3512 if (path1->bus != path2->bus) {
3513 if (path1->bus->path_id == CAM_BUS_WILDCARD)
3515 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
3520 if (path1->target != path2->target) {
3521 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
3524 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
3529 if (path1->device != path2->device) {
3530 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
3533 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
3542 xpt_print_path(struct cam_path *path)
3546 printf("(nopath): ");
3548 if (path->periph != NULL)
3549 printf("(%s%d:", path->periph->periph_name,
3550 path->periph->unit_number);
3552 printf("(noperiph:");
3554 if (path->bus != NULL)
3555 printf("%s%d:%d:", path->bus->sim->sim_name,
3556 path->bus->sim->unit_number,
3557 path->bus->sim->bus_id);
3561 if (path->target != NULL)
3562 printf("%d:", path->target->target_id);
3566 if (path->device != NULL)
3567 printf("%d): ", path->device->lun_id);
3574 xpt_print(struct cam_path *path, const char *fmt, ...)
3577 xpt_print_path(path);
3584 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
3589 if (path != NULL && path->bus != NULL)
3590 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3593 sbuf_new(&sb, str, str_len, 0);
3596 sbuf_printf(&sb, "(nopath): ");
3598 if (path->periph != NULL)
3599 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
3600 path->periph->unit_number);
3602 sbuf_printf(&sb, "(noperiph:");
3604 if (path->bus != NULL)
3605 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
3606 path->bus->sim->unit_number,
3607 path->bus->sim->bus_id);
3609 sbuf_printf(&sb, "nobus:");
3611 if (path->target != NULL)
3612 sbuf_printf(&sb, "%d:", path->target->target_id);
3614 sbuf_printf(&sb, "X:");
3616 if (path->device != NULL)
3617 sbuf_printf(&sb, "%d): ", path->device->lun_id);
3619 sbuf_printf(&sb, "X): ");
3623 return(sbuf_len(&sb));
3627 xpt_path_path_id(struct cam_path *path)
3629 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3631 return(path->bus->path_id);
3635 xpt_path_target_id(struct cam_path *path)
3637 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3639 if (path->target != NULL)
3640 return (path->target->target_id);
3642 return (CAM_TARGET_WILDCARD);
3646 xpt_path_lun_id(struct cam_path *path)
3648 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3650 if (path->device != NULL)
3651 return (path->device->lun_id);
3653 return (CAM_LUN_WILDCARD);
3657 xpt_path_sim(struct cam_path *path)
3660 return (path->bus->sim);
3664 xpt_path_periph(struct cam_path *path)
3666 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3668 return (path->periph);
3672 * Release a CAM control block for the caller. Remit the cost of the structure
3673 * to the device referenced by the path. If the this device had no 'credits'
3674 * and peripheral drivers have registered async callbacks for this notification
3678 xpt_release_ccb(union ccb *free_ccb)
3680 struct cam_path *path;
3681 struct cam_ed *device;
3683 struct cam_sim *sim;
3685 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
3686 path = free_ccb->ccb_h.path;
3687 device = path->device;
3691 mtx_assert(sim->mtx, MA_OWNED);
3693 cam_ccbq_release_opening(&device->ccbq);
3694 if (device->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) {
3695 device->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
3696 cam_ccbq_resize(&device->ccbq,
3697 device->ccbq.dev_openings + device->ccbq.dev_active);
3699 if (sim->ccb_count > sim->max_ccbs) {
3700 xpt_free_ccb(free_ccb);
3703 SLIST_INSERT_HEAD(&sim->ccb_freeq, &free_ccb->ccb_h,
3706 if (sim->devq == NULL) {
3709 sim->devq->alloc_openings++;
3710 sim->devq->alloc_active--;
3711 if (device_is_alloc_queued(device) == 0)
3712 xpt_schedule_dev_allocq(bus, device);
3713 xpt_run_dev_allocq(bus);
3716 /* Functions accessed by SIM drivers */
3718 static struct xpt_xport xport_default = {
3719 .alloc_device = xpt_alloc_device_default,
3720 .action = xpt_action_default,
3721 .async = xpt_dev_async_default,
3725 * A sim structure, listing the SIM entry points and instance
3726 * identification info is passed to xpt_bus_register to hook the SIM
3727 * into the CAM framework. xpt_bus_register creates a cam_eb entry
3728 * for this new bus and places it in the array of busses and assigns
3729 * it a path_id. The path_id may be influenced by "hard wiring"
3730 * information specified by the user. Once interrupt services are
3731 * available, the bus will be probed.
3734 xpt_bus_register(struct cam_sim *sim, device_t parent, u_int32_t bus)
3736 struct cam_eb *new_bus;
3737 struct cam_eb *old_bus;
3738 struct ccb_pathinq cpi;
3739 struct cam_path *path;
3742 mtx_assert(sim->mtx, MA_OWNED);
3745 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
3746 M_CAMXPT, M_NOWAIT);
3747 if (new_bus == NULL) {
3748 /* Couldn't satisfy request */
3749 return (CAM_RESRC_UNAVAIL);
3751 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_NOWAIT);
3753 free(new_bus, M_CAMXPT);
3754 return (CAM_RESRC_UNAVAIL);
3757 if (strcmp(sim->sim_name, "xpt") != 0) {
3759 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
3762 TAILQ_INIT(&new_bus->et_entries);
3763 new_bus->path_id = sim->path_id;
3766 timevalclear(&new_bus->last_reset);
3768 new_bus->refcount = 1; /* Held until a bus_deregister event */
3769 new_bus->generation = 0;
3771 mtx_lock(&xsoftc.xpt_topo_lock);
3772 old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3773 while (old_bus != NULL
3774 && old_bus->path_id < new_bus->path_id)
3775 old_bus = TAILQ_NEXT(old_bus, links);
3776 if (old_bus != NULL)
3777 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
3779 TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
3780 xsoftc.bus_generation++;
3781 mtx_unlock(&xsoftc.xpt_topo_lock);
3784 * Set a default transport so that a PATH_INQ can be issued to
3785 * the SIM. This will then allow for probing and attaching of
3786 * a more appropriate transport.
3788 new_bus->xport = &xport_default;
3790 status = xpt_compile_path(path, /*periph*/NULL, sim->path_id,
3791 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3792 if (status != CAM_REQ_CMP)
3793 printf("xpt_compile_path returned %d\n", status);
3795 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
3796 cpi.ccb_h.func_code = XPT_PATH_INQ;
3797 xpt_action((union ccb *)&cpi);
3799 if (cpi.ccb_h.status == CAM_REQ_CMP) {
3800 switch (cpi.transport) {
3807 new_bus->xport = scsi_get_xport();
3811 new_bus->xport = ata_get_xport();
3814 new_bus->xport = &xport_default;
3819 /* Notify interested parties */
3820 if (sim->path_id != CAM_XPT_PATH_ID) {
3821 union ccb *scan_ccb;
3823 xpt_async(AC_PATH_REGISTERED, path, &cpi);
3824 /* Initiate bus rescan. */
3825 scan_ccb = xpt_alloc_ccb_nowait();
3826 scan_ccb->ccb_h.path = path;
3827 scan_ccb->ccb_h.func_code = XPT_SCAN_BUS;
3828 scan_ccb->crcn.flags = 0;
3829 xpt_rescan(scan_ccb);
3831 xpt_free_path(path);
3832 return (CAM_SUCCESS);
3836 xpt_bus_deregister(path_id_t pathid)
3838 struct cam_path bus_path;
3841 status = xpt_compile_path(&bus_path, NULL, pathid,
3842 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
3843 if (status != CAM_REQ_CMP)
3846 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
3847 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
3849 /* Release the reference count held while registered. */
3850 xpt_release_bus(bus_path.bus);
3851 xpt_release_path(&bus_path);
3853 return (CAM_REQ_CMP);
3857 xptnextfreepathid(void)
3864 mtx_lock(&xsoftc.xpt_topo_lock);
3865 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
3867 /* Find an unoccupied pathid */
3868 while (bus != NULL && bus->path_id <= pathid) {
3869 if (bus->path_id == pathid)
3871 bus = TAILQ_NEXT(bus, links);
3873 mtx_unlock(&xsoftc.xpt_topo_lock);
3876 * Ensure that this pathid is not reserved for
3877 * a bus that may be registered in the future.
3879 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
3881 /* Start the search over */
3882 mtx_lock(&xsoftc.xpt_topo_lock);
3889 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
3896 pathid = CAM_XPT_PATH_ID;
3897 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
3899 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
3900 if (strcmp(dname, "scbus")) {
3901 /* Avoid a bit of foot shooting. */
3904 if (dunit < 0) /* unwired?! */
3906 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
3907 if (sim_bus == val) {
3911 } else if (sim_bus == 0) {
3912 /* Unspecified matches bus 0 */
3916 printf("Ambiguous scbus configuration for %s%d "
3917 "bus %d, cannot wire down. The kernel "
3918 "config entry for scbus%d should "
3919 "specify a controller bus.\n"
3920 "Scbus will be assigned dynamically.\n",
3921 sim_name, sim_unit, sim_bus, dunit);
3926 if (pathid == CAM_XPT_PATH_ID)
3927 pathid = xptnextfreepathid();
3932 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
3935 struct cam_et *target, *next_target;
3936 struct cam_ed *device, *next_device;
3938 mtx_assert(path->bus->sim->mtx, MA_OWNED);
3940 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n"));
3943 * Most async events come from a CAM interrupt context. In
3944 * a few cases, the error recovery code at the peripheral layer,
3945 * which may run from our SWI or a process context, may signal
3946 * deferred events with a call to xpt_async.
3951 if (async_code == AC_BUS_RESET) {
3952 /* Update our notion of when the last reset occurred */
3953 microtime(&bus->last_reset);
3956 for (target = TAILQ_FIRST(&bus->et_entries);
3958 target = next_target) {
3960 next_target = TAILQ_NEXT(target, links);
3962 if (path->target != target
3963 && path->target->target_id != CAM_TARGET_WILDCARD
3964 && target->target_id != CAM_TARGET_WILDCARD)
3967 if (async_code == AC_SENT_BDR) {
3968 /* Update our notion of when the last reset occurred */
3969 microtime(&path->target->last_reset);
3972 for (device = TAILQ_FIRST(&target->ed_entries);
3974 device = next_device) {
3976 next_device = TAILQ_NEXT(device, links);
3978 if (path->device != device
3979 && path->device->lun_id != CAM_LUN_WILDCARD
3980 && device->lun_id != CAM_LUN_WILDCARD)
3983 * The async callback could free the device.
3984 * If it is a broadcast async, it doesn't hold
3985 * device reference, so take our own reference.
3987 xpt_acquire_device(device);
3988 (*(bus->xport->async))(async_code, bus,
3992 xpt_async_bcast(&device->asyncs, async_code,
3994 xpt_release_device(device);
3999 * If this wasn't a fully wildcarded async, tell all
4000 * clients that want all async events.
4002 if (bus != xpt_periph->path->bus)
4003 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4008 xpt_async_bcast(struct async_list *async_head,
4009 u_int32_t async_code,
4010 struct cam_path *path, void *async_arg)
4012 struct async_node *cur_entry;
4014 cur_entry = SLIST_FIRST(async_head);
4015 while (cur_entry != NULL) {
4016 struct async_node *next_entry;
4018 * Grab the next list entry before we call the current
4019 * entry's callback. This is because the callback function
4020 * can delete its async callback entry.
4022 next_entry = SLIST_NEXT(cur_entry, links);
4023 if ((cur_entry->event_enable & async_code) != 0)
4024 cur_entry->callback(cur_entry->callback_arg,
4027 cur_entry = next_entry;
4032 xpt_dev_async_default(u_int32_t async_code, struct cam_eb *bus,
4033 struct cam_et *target, struct cam_ed *device,
4036 printf("%s called\n", __func__);
4040 xpt_freeze_devq_rl(struct cam_path *path, cam_rl rl, u_int count)
4042 struct cam_ed *dev = path->device;
4044 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4045 dev->sim->devq->alloc_openings +=
4046 cam_ccbq_freeze(&dev->ccbq, rl, count);
4047 /* Remove frozen device from allocq. */
4048 if (device_is_alloc_queued(dev) &&
4049 cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL(
4050 CAMQ_GET_PRIO(&dev->drvq)))) {
4051 camq_remove(&dev->sim->devq->alloc_queue,
4052 dev->alloc_ccb_entry.pinfo.index);
4054 /* Remove frozen device from sendq. */
4055 if (device_is_send_queued(dev) &&
4056 cam_ccbq_frozen_top(&dev->ccbq)) {
4057 camq_remove(&dev->sim->devq->send_queue,
4058 dev->send_ccb_entry.pinfo.index);
4060 return (dev->ccbq.queue.qfrozen_cnt[rl]);
4064 xpt_freeze_devq(struct cam_path *path, u_int count)
4067 return (xpt_freeze_devq_rl(path, 0, count));
4071 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4074 mtx_assert(sim->mtx, MA_OWNED);
4075 sim->devq->send_queue.qfrozen_cnt[0] += count;
4076 return (sim->devq->send_queue.qfrozen_cnt[0]);
4080 xpt_release_devq_timeout(void *arg)
4082 struct cam_ed *device;
4084 device = (struct cam_ed *)arg;
4086 xpt_release_devq_device(device, /*rl*/0, /*count*/1, /*run_queue*/TRUE);
4090 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4092 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4094 xpt_release_devq_device(path->device, /*rl*/0, count, run_queue);
4098 xpt_release_devq_rl(struct cam_path *path, cam_rl rl, u_int count, int run_queue)
4100 mtx_assert(path->bus->sim->mtx, MA_OWNED);
4102 xpt_release_devq_device(path->device, rl, count, run_queue);
4106 xpt_release_devq_device(struct cam_ed *dev, cam_rl rl, u_int count, int run_queue)
4109 if (count > dev->ccbq.queue.qfrozen_cnt[rl]) {
4111 printf("xpt_release_devq(%d): requested %u > present %u\n",
4112 rl, count, dev->ccbq.queue.qfrozen_cnt[rl]);
4114 count = dev->ccbq.queue.qfrozen_cnt[rl];
4116 dev->sim->devq->alloc_openings -=
4117 cam_ccbq_release(&dev->ccbq, rl, count);
4118 if (cam_ccbq_frozen(&dev->ccbq, CAM_PRIORITY_TO_RL(
4119 CAMQ_GET_PRIO(&dev->drvq))) == 0) {
4120 if (xpt_schedule_dev_allocq(dev->target->bus, dev))
4121 xpt_run_dev_allocq(dev->target->bus);
4123 if (cam_ccbq_frozen_top(&dev->ccbq) == 0) {
4125 * No longer need to wait for a successful
4126 * command completion.
4128 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4130 * Remove any timeouts that might be scheduled
4131 * to release this queue.
4133 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4134 callout_stop(&dev->callout);
4135 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4140 * Now that we are unfrozen schedule the
4141 * device so any pending transactions are
4144 if (xpt_schedule_dev_sendq(dev->target->bus, dev))
4145 xpt_run_dev_sendq(dev->target->bus);
4150 xpt_release_simq(struct cam_sim *sim, int run_queue)
4154 mtx_assert(sim->mtx, MA_OWNED);
4155 sendq = &(sim->devq->send_queue);
4156 if (sendq->qfrozen_cnt[0] <= 0) {
4158 printf("xpt_release_simq: requested 1 > present %u\n",
4159 sendq->qfrozen_cnt[0]);
4162 sendq->qfrozen_cnt[0]--;
4163 if (sendq->qfrozen_cnt[0] == 0) {
4165 * If there is a timeout scheduled to release this
4166 * sim queue, remove it. The queue frozen count is
4169 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4170 callout_stop(&sim->callout);
4171 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4177 * Now that we are unfrozen run the send queue.
4179 bus = xpt_find_bus(sim->path_id);
4180 xpt_run_dev_sendq(bus);
4181 xpt_release_bus(bus);
4187 * XXX Appears to be unused.
4190 xpt_release_simq_timeout(void *arg)
4192 struct cam_sim *sim;
4194 sim = (struct cam_sim *)arg;
4195 xpt_release_simq(sim, /* run_queue */ TRUE);
4199 xpt_done(union ccb *done_ccb)
4201 struct cam_sim *sim;
4204 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4205 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4207 * Queue up the request for handling by our SWI handler
4208 * any of the "non-immediate" type of ccbs.
4210 sim = done_ccb->ccb_h.path->bus->sim;
4211 TAILQ_INSERT_TAIL(&sim->sim_doneq, &done_ccb->ccb_h,
4213 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4214 if ((sim->flags & (CAM_SIM_ON_DONEQ | CAM_SIM_POLLED)) == 0) {
4215 mtx_lock(&cam_simq_lock);
4216 first = TAILQ_EMPTY(&cam_simq);
4217 TAILQ_INSERT_TAIL(&cam_simq, sim, links);
4218 mtx_unlock(&cam_simq_lock);
4219 sim->flags |= CAM_SIM_ON_DONEQ;
4221 swi_sched(cambio_ih, 0);
4231 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_ZERO|M_WAITOK);
4236 xpt_alloc_ccb_nowait()
4240 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_ZERO|M_NOWAIT);
4245 xpt_free_ccb(union ccb *free_ccb)
4247 free(free_ccb, M_CAMXPT);
4252 /* Private XPT functions */
4255 * Get a CAM control block for the caller. Charge the structure to the device
4256 * referenced by the path. If the this device has no 'credits' then the
4257 * device already has the maximum number of outstanding operations under way
4258 * and we return NULL. If we don't have sufficient resources to allocate more
4259 * ccbs, we also return NULL.
4262 xpt_get_ccb(struct cam_ed *device)
4265 struct cam_sim *sim;
4268 if ((new_ccb = (union ccb *)SLIST_FIRST(&sim->ccb_freeq)) == NULL) {
4269 new_ccb = xpt_alloc_ccb_nowait();
4270 if (new_ccb == NULL) {
4273 if ((sim->flags & CAM_SIM_MPSAFE) == 0)
4274 callout_handle_init(&new_ccb->ccb_h.timeout_ch);
4275 SLIST_INSERT_HEAD(&sim->ccb_freeq, &new_ccb->ccb_h,
4279 cam_ccbq_take_opening(&device->ccbq);
4280 SLIST_REMOVE_HEAD(&sim->ccb_freeq, xpt_links.sle);
4285 xpt_release_bus(struct cam_eb *bus)
4288 if ((--bus->refcount == 0)
4289 && (TAILQ_FIRST(&bus->et_entries) == NULL)) {
4290 mtx_lock(&xsoftc.xpt_topo_lock);
4291 TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
4292 xsoftc.bus_generation++;
4293 mtx_unlock(&xsoftc.xpt_topo_lock);
4294 cam_sim_release(bus->sim);
4295 free(bus, M_CAMXPT);
4299 static struct cam_et *
4300 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4302 struct cam_et *target;
4304 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT, M_NOWAIT);
4305 if (target != NULL) {
4306 struct cam_et *cur_target;
4308 TAILQ_INIT(&target->ed_entries);
4310 target->target_id = target_id;
4311 target->refcount = 1;
4312 target->generation = 0;
4313 timevalclear(&target->last_reset);
4315 * Hold a reference to our parent bus so it
4316 * will not go away before we do.
4320 /* Insertion sort into our bus's target list */
4321 cur_target = TAILQ_FIRST(&bus->et_entries);
4322 while (cur_target != NULL && cur_target->target_id < target_id)
4323 cur_target = TAILQ_NEXT(cur_target, links);
4325 if (cur_target != NULL) {
4326 TAILQ_INSERT_BEFORE(cur_target, target, links);
4328 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4336 xpt_release_target(struct cam_et *target)
4339 if ((--target->refcount == 0)
4340 && (TAILQ_FIRST(&target->ed_entries) == NULL)) {
4341 TAILQ_REMOVE(&target->bus->et_entries, target, links);
4342 target->bus->generation++;
4343 xpt_release_bus(target->bus);
4344 free(target, M_CAMXPT);
4348 static struct cam_ed *
4349 xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target,
4352 struct cam_ed *device, *cur_device;
4354 device = xpt_alloc_device(bus, target, lun_id);
4358 device->mintags = 1;
4359 device->maxtags = 1;
4360 bus->sim->max_ccbs += device->ccbq.devq_openings;
4361 cur_device = TAILQ_FIRST(&target->ed_entries);
4362 while (cur_device != NULL && cur_device->lun_id < lun_id)
4363 cur_device = TAILQ_NEXT(cur_device, links);
4364 if (cur_device != NULL) {
4365 TAILQ_INSERT_BEFORE(cur_device, device, links);
4367 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4369 target->generation++;
4375 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4377 struct cam_ed *device;
4378 struct cam_devq *devq;
4381 /* Make space for us in the device queue on our bus */
4382 devq = bus->sim->devq;
4383 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
4385 if (status != CAM_REQ_CMP) {
4388 device = (struct cam_ed *)malloc(sizeof(*device),
4389 M_CAMXPT, M_NOWAIT);
4392 if (device != NULL) {
4393 cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
4394 device->alloc_ccb_entry.device = device;
4395 cam_init_pinfo(&device->send_ccb_entry.pinfo);
4396 device->send_ccb_entry.device = device;
4397 device->target = target;
4398 device->lun_id = lun_id;
4399 device->sim = bus->sim;
4400 /* Initialize our queues */
4401 if (camq_init(&device->drvq, 0) != 0) {
4402 free(device, M_CAMXPT);
4405 if (cam_ccbq_init(&device->ccbq,
4406 bus->sim->max_dev_openings) != 0) {
4407 camq_fini(&device->drvq);
4408 free(device, M_CAMXPT);
4411 SLIST_INIT(&device->asyncs);
4412 SLIST_INIT(&device->periphs);
4413 device->generation = 0;
4414 device->owner = NULL;
4415 device->flags = CAM_DEV_UNCONFIGURED;
4416 device->tag_delay_count = 0;
4417 device->tag_saved_openings = 0;
4418 device->refcount = 1;
4419 callout_init_mtx(&device->callout, bus->sim->mtx, 0);
4422 * Hold a reference to our parent target so it
4423 * will not go away before we do.
4432 xpt_acquire_device(struct cam_ed *device)
4439 xpt_release_device(struct cam_ed *device)
4442 if (--device->refcount == 0) {
4443 struct cam_devq *devq;
4445 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
4446 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
4447 panic("Removing device while still queued for ccbs");
4449 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
4450 callout_stop(&device->callout);
4452 TAILQ_REMOVE(&device->target->ed_entries, device,links);
4453 device->target->generation++;
4454 device->target->bus->sim->max_ccbs -= device->ccbq.devq_openings;
4455 /* Release our slot in the devq */
4456 devq = device->target->bus->sim->devq;
4457 cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
4458 camq_fini(&device->drvq);
4459 cam_ccbq_fini(&device->ccbq);
4460 xpt_release_target(device->target);
4461 free(device, M_CAMXPT);
4466 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
4474 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
4475 result = cam_ccbq_resize(&dev->ccbq, newopenings);
4476 if (result == CAM_REQ_CMP && (diff < 0)) {
4477 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
4479 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
4480 || (dev->inq_flags & SID_CmdQue) != 0)
4481 dev->tag_saved_openings = newopenings;
4482 /* Adjust the global limit */
4483 dev->sim->max_ccbs += diff;
4487 static struct cam_eb *
4488 xpt_find_bus(path_id_t path_id)
4492 mtx_lock(&xsoftc.xpt_topo_lock);
4493 for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4495 bus = TAILQ_NEXT(bus, links)) {
4496 if (bus->path_id == path_id) {
4501 mtx_unlock(&xsoftc.xpt_topo_lock);
4505 static struct cam_et *
4506 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
4508 struct cam_et *target;
4510 for (target = TAILQ_FIRST(&bus->et_entries);
4512 target = TAILQ_NEXT(target, links)) {
4513 if (target->target_id == target_id) {
4521 static struct cam_ed *
4522 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
4524 struct cam_ed *device;
4526 for (device = TAILQ_FIRST(&target->ed_entries);
4528 device = TAILQ_NEXT(device, links)) {
4529 if (device->lun_id == lun_id) {
4538 xpt_start_tags(struct cam_path *path)
4540 struct ccb_relsim crs;
4541 struct cam_ed *device;
4542 struct cam_sim *sim;
4545 device = path->device;
4546 sim = path->bus->sim;
4547 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4548 xpt_freeze_devq(path, /*count*/1);
4549 device->inq_flags |= SID_CmdQue;
4550 if (device->tag_saved_openings != 0)
4551 newopenings = device->tag_saved_openings;
4553 newopenings = min(device->maxtags,
4554 sim->max_tagged_dev_openings);
4555 xpt_dev_ccbq_resize(path, newopenings);
4556 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4557 crs.ccb_h.func_code = XPT_REL_SIMQ;
4558 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4560 = crs.release_timeout
4563 xpt_action((union ccb *)&crs);
4567 xpt_stop_tags(struct cam_path *path)
4569 struct ccb_relsim crs;
4570 struct cam_ed *device;
4571 struct cam_sim *sim;
4573 device = path->device;
4574 sim = path->bus->sim;
4575 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
4576 device->tag_delay_count = 0;
4577 xpt_freeze_devq(path, /*count*/1);
4578 device->inq_flags &= ~SID_CmdQue;
4579 xpt_dev_ccbq_resize(path, sim->max_dev_openings);
4580 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
4581 crs.ccb_h.func_code = XPT_REL_SIMQ;
4582 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
4584 = crs.release_timeout
4587 xpt_action((union ccb *)&crs);
4591 xpt_boot_delay(void *arg)
4598 xpt_config(void *arg)
4601 * Now that interrupts are enabled, go find our devices
4605 /* Setup debugging flags and path */
4606 #ifdef CAM_DEBUG_FLAGS
4607 cam_dflags = CAM_DEBUG_FLAGS;
4608 #else /* !CAM_DEBUG_FLAGS */
4609 cam_dflags = CAM_DEBUG_NONE;
4610 #endif /* CAM_DEBUG_FLAGS */
4611 #ifdef CAM_DEBUG_BUS
4612 if (cam_dflags != CAM_DEBUG_NONE) {
4614 * Locking is specifically omitted here. No SIMs have
4615 * registered yet, so xpt_create_path will only be searching
4616 * empty lists of targets and devices.
4618 if (xpt_create_path(&cam_dpath, xpt_periph,
4619 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
4620 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
4621 printf("xpt_config: xpt_create_path() failed for debug"
4622 " target %d:%d:%d, debugging disabled\n",
4623 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
4624 cam_dflags = CAM_DEBUG_NONE;
4628 #else /* !CAM_DEBUG_BUS */
4630 #endif /* CAM_DEBUG_BUS */
4631 #endif /* CAMDEBUG */
4633 periphdriver_init(1);
4635 callout_init(&xsoftc.boot_callout, 1);
4636 callout_reset(&xsoftc.boot_callout, hz * xsoftc.boot_delay / 1000,
4637 xpt_boot_delay, NULL);
4638 /* Fire up rescan thread. */
4639 if (kproc_create(xpt_scanner_thread, NULL, NULL, 0, 0, "xpt_thrd")) {
4640 printf("xpt_init: failed to create rescan thread\n");
4648 xsoftc.buses_to_config++;
4653 xpt_release_boot(void)
4656 xsoftc.buses_to_config--;
4657 if (xsoftc.buses_to_config == 0 && xsoftc.buses_config_done == 0) {
4658 struct xpt_task *task;
4660 xsoftc.buses_config_done = 1;
4662 /* Call manually because we don't have any busses */
4663 task = malloc(sizeof(struct xpt_task), M_CAMXPT, M_NOWAIT);
4665 TASK_INIT(&task->task, 0, xpt_finishconfig_task, task);
4666 taskqueue_enqueue(taskqueue_thread, &task->task);
4673 * If the given device only has one peripheral attached to it, and if that
4674 * peripheral is the passthrough driver, announce it. This insures that the
4675 * user sees some sort of announcement for every peripheral in their system.
4678 xptpassannouncefunc(struct cam_ed *device, void *arg)
4680 struct cam_periph *periph;
4683 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
4684 periph = SLIST_NEXT(periph, periph_links), i++);
4686 periph = SLIST_FIRST(&device->periphs);
4688 && (strncmp(periph->periph_name, "pass", 4) == 0))
4689 xpt_announce_periph(periph, NULL);
4695 xpt_finishconfig_task(void *context, int pending)
4698 periphdriver_init(2);
4700 * Check for devices with no "standard" peripheral driver
4701 * attached. For any devices like that, announce the
4702 * passthrough driver so the user will see something.
4704 xpt_for_all_devices(xptpassannouncefunc, NULL);
4706 /* Release our hook so that the boot can continue. */
4707 config_intrhook_disestablish(xsoftc.xpt_config_hook);
4708 free(xsoftc.xpt_config_hook, M_CAMXPT);
4709 xsoftc.xpt_config_hook = NULL;
4711 free(context, M_CAMXPT);
4715 xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
4716 struct cam_path *path)
4718 struct ccb_setasync csa;
4723 mtx_lock(&xsoftc.xpt_lock);
4724 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
4725 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4726 if (status != CAM_REQ_CMP) {
4727 mtx_unlock(&xsoftc.xpt_lock);
4733 xpt_setup_ccb(&csa.ccb_h, path, CAM_PRIORITY_NORMAL);
4734 csa.ccb_h.func_code = XPT_SASYNC_CB;
4735 csa.event_enable = event;
4736 csa.callback = cbfunc;
4737 csa.callback_arg = cbarg;
4738 xpt_action((union ccb *)&csa);
4739 status = csa.ccb_h.status;
4741 xpt_free_path(path);
4742 mtx_unlock(&xsoftc.xpt_lock);
4744 if ((status == CAM_REQ_CMP) &&
4745 (csa.event_enable & AC_FOUND_DEVICE)) {
4747 * Get this peripheral up to date with all
4748 * the currently existing devices.
4750 xpt_for_all_devices(xptsetasyncfunc, &csa);
4752 if ((status == CAM_REQ_CMP) &&
4753 (csa.event_enable & AC_PATH_REGISTERED)) {
4755 * Get this peripheral up to date with all
4756 * the currently existing busses.
4758 xpt_for_all_busses(xptsetasyncbusfunc, &csa);
4765 xptaction(struct cam_sim *sim, union ccb *work_ccb)
4767 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
4769 switch (work_ccb->ccb_h.func_code) {
4770 /* Common cases first */
4771 case XPT_PATH_INQ: /* Path routing inquiry */
4773 struct ccb_pathinq *cpi;
4775 cpi = &work_ccb->cpi;
4776 cpi->version_num = 1; /* XXX??? */
4777 cpi->hba_inquiry = 0;
4778 cpi->target_sprt = 0;
4780 cpi->hba_eng_cnt = 0;
4781 cpi->max_target = 0;
4783 cpi->initiator_id = 0;
4784 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
4785 strncpy(cpi->hba_vid, "", HBA_IDLEN);
4786 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
4787 cpi->unit_number = sim->unit_number;
4788 cpi->bus_id = sim->bus_id;
4789 cpi->base_transfer_speed = 0;
4790 cpi->protocol = PROTO_UNSPECIFIED;
4791 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
4792 cpi->transport = XPORT_UNSPECIFIED;
4793 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
4794 cpi->ccb_h.status = CAM_REQ_CMP;
4799 work_ccb->ccb_h.status = CAM_REQ_INVALID;
4806 * The xpt as a "controller" has no interrupt sources, so polling
4810 xptpoll(struct cam_sim *sim)
4815 xpt_lock_buses(void)
4817 mtx_lock(&xsoftc.xpt_topo_lock);
4821 xpt_unlock_buses(void)
4823 mtx_unlock(&xsoftc.xpt_topo_lock);
4830 struct cam_sim *sim;
4832 mtx_lock(&cam_simq_lock);
4834 while (!TAILQ_EMPTY(&cam_simq)) {
4835 TAILQ_CONCAT(&queue, &cam_simq, links);
4836 mtx_unlock(&cam_simq_lock);
4838 while ((sim = TAILQ_FIRST(&queue)) != NULL) {
4839 TAILQ_REMOVE(&queue, sim, links);
4841 sim->flags &= ~CAM_SIM_ON_DONEQ;
4842 camisr_runqueue(&sim->sim_doneq);
4843 CAM_SIM_UNLOCK(sim);
4845 mtx_lock(&cam_simq_lock);
4847 mtx_unlock(&cam_simq_lock);
4851 camisr_runqueue(void *V_queue)
4853 cam_isrq_t *queue = V_queue;
4854 struct ccb_hdr *ccb_h;
4856 while ((ccb_h = TAILQ_FIRST(queue)) != NULL) {
4859 TAILQ_REMOVE(queue, ccb_h, sim_links.tqe);
4860 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
4862 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
4867 if (ccb_h->flags & CAM_HIGH_POWER) {
4868 struct highpowerlist *hphead;
4869 union ccb *send_ccb;
4871 mtx_lock(&xsoftc.xpt_lock);
4872 hphead = &xsoftc.highpowerq;
4874 send_ccb = (union ccb *)STAILQ_FIRST(hphead);
4877 * Increment the count since this command is done.
4879 xsoftc.num_highpower++;
4882 * Any high powered commands queued up?
4884 if (send_ccb != NULL) {
4886 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
4887 mtx_unlock(&xsoftc.xpt_lock);
4889 xpt_release_devq(send_ccb->ccb_h.path,
4890 /*count*/1, /*runqueue*/TRUE);
4892 mtx_unlock(&xsoftc.xpt_lock);
4895 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
4898 dev = ccb_h->path->device;
4900 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
4901 ccb_h->path->bus->sim->devq->send_active--;
4902 ccb_h->path->bus->sim->devq->send_openings++;
4905 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
4906 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)
4907 || ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
4908 && (dev->ccbq.dev_active == 0))) {
4909 xpt_release_devq(ccb_h->path, /*count*/1,
4910 /*run_queue*/FALSE);
4913 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
4914 && (--dev->tag_delay_count == 0))
4915 xpt_start_tags(ccb_h->path);
4916 if (!device_is_send_queued(dev))
4917 xpt_schedule_dev_sendq(ccb_h->path->bus, dev);
4920 if (ccb_h->status & CAM_RELEASE_SIMQ) {
4921 xpt_release_simq(ccb_h->path->bus->sim,
4923 ccb_h->status &= ~CAM_RELEASE_SIMQ;
4927 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
4928 && (ccb_h->status & CAM_DEV_QFRZN)) {
4929 xpt_release_devq(ccb_h->path, /*count*/1,
4931 ccb_h->status &= ~CAM_DEV_QFRZN;
4933 xpt_run_dev_sendq(ccb_h->path->bus);
4936 /* Call the peripheral driver's callback */
4937 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);