2 * Common functions for SCSI Interface Modules (SIMs).
4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6 * Copyright (c) 1997 Justin T. Gibbs.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification, immediately at the beginning of the file.
15 * 2. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/kernel.h>
39 #include <sys/mutex.h>
43 #include <cam/cam_ccb.h>
44 #include <cam/cam_sim.h>
45 #include <cam/cam_queue.h>
46 #include <cam/cam_xpt.h>
48 #define CAM_PATH_ANY (u_int32_t)-1
50 static MALLOC_DEFINE(M_CAMSIM, "CAM SIM", "CAM SIM buffers");
52 static struct mtx cam_sim_free_mtx;
53 MTX_SYSINIT(cam_sim_free_init, &cam_sim_free_mtx, "CAM SIM free lock", MTX_DEF);
56 cam_simq_alloc(u_int32_t max_sim_transactions)
58 return (cam_devq_alloc(/*size*/0, max_sim_transactions));
62 cam_simq_free(struct cam_devq *devq)
68 cam_sim_alloc(sim_action_func sim_action, sim_poll_func sim_poll,
69 const char *sim_name, void *softc, u_int32_t unit,
70 struct mtx *mtx, int max_dev_transactions,
71 int max_tagged_dev_transactions, struct cam_devq *queue)
75 sim = (struct cam_sim *)malloc(sizeof(struct cam_sim),
76 M_CAMSIM, M_ZERO | M_NOWAIT);
81 sim->sim_action = sim_action;
82 sim->sim_poll = sim_poll;
83 sim->sim_name = sim_name;
85 sim->path_id = CAM_PATH_ANY;
86 sim->sim_dev = NULL; /* set only by cam_sim_alloc_dev */
87 sim->unit_number = unit;
88 sim->bus_id = 0; /* set in xpt_bus_register */
89 sim->max_tagged_dev_openings = max_tagged_dev_transactions;
90 sim->max_dev_openings = max_dev_transactions;
97 callout_init(&sim->callout, 0);
99 sim->flags |= CAM_SIM_MPSAFE;
100 callout_init(&sim->callout, 1);
106 cam_sim_alloc_dev(sim_action_func sim_action, sim_poll_func sim_poll,
107 const char *sim_name, void *softc, device_t dev,
108 struct mtx *mtx, int max_dev_transactions,
109 int max_tagged_dev_transactions, struct cam_devq *queue)
113 KASSERT(dev != NULL, ("%s: dev is null for sim_name %s softc %p\n",
114 __func__, sim_name, softc));
116 sim = cam_sim_alloc(sim_action, sim_poll, sim_name, softc,
117 device_get_unit(dev), mtx, max_dev_transactions,
118 max_tagged_dev_transactions, queue);
125 cam_sim_free(struct cam_sim *sim, int free_devq)
127 struct mtx *mtx = sim->mtx;
131 mtx_assert(mtx, MA_OWNED);
133 mtx = &cam_sim_free_mtx;
137 if (sim->refcount > 0) {
138 error = msleep(sim, mtx, PRIBIO, "simfree", 0);
139 KASSERT(error == 0, ("invalid error value for msleep(9)"));
141 KASSERT(sim->refcount == 0, ("sim->refcount == 0"));
142 if (sim->mtx == NULL)
146 cam_simq_free(sim->devq);
151 cam_sim_release(struct cam_sim *sim)
153 struct mtx *mtx = sim->mtx;
161 mtx = &cam_sim_free_mtx;
164 KASSERT(sim->refcount >= 1, ("sim->refcount >= 1"));
166 if (sim->refcount == 0)
173 cam_sim_hold(struct cam_sim *sim)
175 struct mtx *mtx = sim->mtx;
183 mtx = &cam_sim_free_mtx;
186 KASSERT(sim->refcount >= 1, ("sim->refcount >= 1"));
193 cam_sim_set_path(struct cam_sim *sim, u_int32_t path_id)
195 sim->path_id = path_id;