FreeBSD kernel CAM code
cam_sim.c
Go to the documentation of this file.
1/*-
2 * Common functions for SCSI Interface Modules (SIMs).
3 *
4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 *
6 * Copyright (c) 1997 Justin T. Gibbs.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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.
17 *
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
28 * SUCH DAMAGE.
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD$");
33
34#include <sys/param.h>
35#include <sys/bus.h>
36#include <sys/kernel.h>
37#include <sys/lock.h>
38#include <sys/malloc.h>
39#include <sys/mutex.h>
40
41#include <cam/cam.h>
42#include <cam/cam_ccb.h>
43#include <cam/cam_queue.h>
44#include <cam/cam_sim.h>
45#include <cam/cam_xpt.h>
46
47#define CAM_PATH_ANY (u_int32_t)-1
48
49static MALLOC_DEFINE(M_CAMSIM, "CAM SIM", "CAM SIM buffers");
50
51static struct mtx cam_sim_free_mtx;
52MTX_SYSINIT(cam_sim_free_init, &cam_sim_free_mtx, "CAM SIM free lock", MTX_DEF);
53
54struct cam_devq *
55cam_simq_alloc(u_int32_t max_sim_transactions)
56{
57 return (cam_devq_alloc(/*size*/0, max_sim_transactions));
58}
59
60void
62{
63 cam_devq_free(devq);
64}
65
66
67
102struct cam_sim *
104 const char *sim_name, void *softc, u_int32_t unit,
105 struct mtx *mtx, int max_dev_transactions,
106 int max_tagged_dev_transactions, struct cam_devq *queue)
107{
108 struct cam_sim *sim;
109
110 sim = malloc(sizeof(struct cam_sim), M_CAMSIM, M_ZERO | M_NOWAIT);
111 if (sim == NULL)
112 return (NULL);
113
114 sim->sim_action = sim_action;
115 sim->sim_poll = sim_poll;
116 sim->sim_name = sim_name;
117 sim->softc = softc;
118 sim->path_id = CAM_PATH_ANY;
119 sim->unit_number = unit;
120 sim->bus_id = 0; /* set in xpt_bus_register */
121 sim->max_tagged_dev_openings = max_tagged_dev_transactions;
122 sim->max_dev_openings = max_dev_transactions;
123 sim->flags = 0;
124 sim->refcount = 1;
125 sim->devq = queue;
126 sim->mtx = mtx;
127 return (sim);
128}
129
143void
144cam_sim_free(struct cam_sim *sim, int free_devq)
145{
146 struct mtx *mtx;
147 int error __diagused;
148
149 if (sim->mtx == NULL) {
150 mtx = &cam_sim_free_mtx;
151 mtx_lock(mtx);
152 } else {
153 mtx = sim->mtx;
154 mtx_assert(mtx, MA_OWNED);
155 }
156 KASSERT(sim->refcount >= 1, ("sim->refcount >= 1"));
157 sim->refcount--;
158 if (sim->refcount > 0) {
159 error = msleep(sim, mtx, PRIBIO, "simfree", 0);
160 KASSERT(error == 0, ("invalid error value for msleep(9)"));
161 }
162 KASSERT(sim->refcount == 0, ("sim->refcount == 0"));
163 if (mtx == &cam_sim_free_mtx) /* sim->mtx == NULL */
164 mtx_unlock(mtx);
165
166 if (free_devq)
167 cam_simq_free(sim->devq);
168 free(sim, M_CAMSIM);
169}
170
171void
173{
174 struct mtx *mtx;
175
176 if (sim->mtx == NULL)
177 mtx = &cam_sim_free_mtx;
178 else if (!mtx_owned(sim->mtx))
179 mtx = sim->mtx;
180 else
181 mtx = NULL; /* We hold the lock. */
182 if (mtx)
183 mtx_lock(mtx);
184 KASSERT(sim->refcount >= 1, ("sim->refcount >= 1"));
185 sim->refcount--;
186 if (sim->refcount == 0)
187 wakeup(sim);
188 if (mtx)
189 mtx_unlock(mtx);
190}
191
192void
194{
195 struct mtx *mtx;
196
197 if (sim->mtx == NULL)
198 mtx = &cam_sim_free_mtx;
199 else if (!mtx_owned(sim->mtx))
200 mtx = sim->mtx;
201 else
202 mtx = NULL; /* We hold the lock. */
203 if (mtx)
204 mtx_lock(mtx);
205 KASSERT(sim->refcount >= 1, ("sim->refcount >= 1"));
206 sim->refcount++;
207 if (mtx)
208 mtx_unlock(mtx);
209}
210
211void
212cam_sim_set_path(struct cam_sim *sim, u_int32_t path_id)
213{
214 sim->path_id = path_id;
215}
struct cam_devq * cam_devq_alloc(int devices, int openings)
Definition: cam_queue.c:186
void cam_devq_free(struct cam_devq *devq)
Definition: cam_queue.c:216
struct cam_sim * cam_sim_alloc(sim_action_func sim_action, sim_poll_func sim_poll, const char *sim_name, void *softc, u_int32_t unit, struct mtx *mtx, int max_dev_transactions, int max_tagged_dev_transactions, struct cam_devq *queue)
allocate a new sim and fill in the details
Definition: cam_sim.c:103
struct cam_devq * cam_simq_alloc(u_int32_t max_sim_transactions)
Definition: cam_sim.c:55
static struct mtx cam_sim_free_mtx
Definition: cam_sim.c:51
void cam_sim_release(struct cam_sim *sim)
Definition: cam_sim.c:172
__FBSDID("$FreeBSD$")
void cam_sim_free(struct cam_sim *sim, int free_devq)
frees up the sim
Definition: cam_sim.c:144
void cam_sim_hold(struct cam_sim *sim)
Definition: cam_sim.c:193
void cam_sim_set_path(struct cam_sim *sim, u_int32_t path_id)
Definition: cam_sim.c:212
void cam_simq_free(struct cam_devq *devq)
Definition: cam_sim.c:61
MTX_SYSINIT(cam_sim_free_init, &cam_sim_free_mtx, "CAM SIM free lock", MTX_DEF)
static MALLOC_DEFINE(M_CAMSIM, "CAM SIM", "CAM SIM buffers")
#define CAM_PATH_ANY
Definition: cam_sim.c:47
void(* sim_poll_func)(struct cam_sim *sim)
Definition: cam_sim.h:51
void(* sim_action_func)(struct cam_sim *sim, union ccb *ccb)
Definition: cam_sim.h:50
sim_poll_func sim_poll
Definition: cam_sim.h:93
void * softc
Definition: cam_sim.h:95
u_int32_t unit_number
Definition: cam_sim.h:99
u_int32_t path_id
Definition: cam_sim.h:98
int max_tagged_dev_openings
Definition: cam_sim.h:101
struct mtx * mtx
Definition: cam_sim.h:96
u_int32_t bus_id
Definition: cam_sim.h:100
int refcount
Definition: cam_sim.h:105
sim_action_func sim_action
Definition: cam_sim.h:92
int max_dev_openings
Definition: cam_sim.h:102
u_int32_t flags
Definition: cam_sim.h:103
struct cam_devq * devq
Definition: cam_sim.h:104
const char * sim_name
Definition: cam_sim.h:94