2 * Common functions for SCSI Interface Modules (SIMs).
4 * SPDX-License-Identifier: BSD-2-Clause
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 #include <sys/param.h>
34 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/mutex.h>
40 #include <cam/cam_ccb.h>
41 #include <cam/cam_queue.h>
42 #include <cam/cam_sim.h>
43 #include <cam/cam_xpt.h>
45 #define CAM_PATH_ANY (uint32_t)-1
47 static MALLOC_DEFINE(M_CAMSIM, "CAM SIM", "CAM SIM buffers");
49 static struct mtx cam_sim_free_mtx;
50 MTX_SYSINIT(cam_sim_free_init, &cam_sim_free_mtx, "CAM SIM free lock", MTX_DEF);
53 cam_simq_alloc(uint32_t max_sim_transactions)
55 return (cam_devq_alloc(/*size*/0, max_sim_transactions));
59 cam_simq_free(struct cam_devq *devq)
67 * @brief allocate a new sim and fill in the details
69 * A Storage Interface Module (SIM) is the interface between CAM and
70 * hardware. SIM receives CCBs from CAM via @p sim_action callback and
71 * translates them into DMA or other hardware transactions. During system
72 * dumps, it can be polled with the @p sim_poll callback. CCB processing is
73 * terminated by calling @c xpt_done().
75 * The @p mtx acts as a perimeter lock for the SIM. All calls into the SIM's
76 * @p sim_action are made with this lock held. It is also used to hold/release
77 * a SIM, managing its reference count. When the lock is NULL, the SIM is 100%
78 * responsible for locking (and the reference counting is done with a shared
81 * The cam_devq passed in (@c queue) is used to arbitrate the number of
82 * outstanding transactions to the SIM. For HBAs that have global limits shared
83 * between the different buses, the same devq should be specified for each bus
84 * attached to the SIM.
86 * @param sim_action Function to call to process CCBs
87 * @param sim_poll Function to poll the hardware for completions
88 * @param sim_name Name of SIM class
89 * @param softc Software context associated with the SIM
90 * @param unit Unit number of SIM
91 * @param mtx Mutex to lock while interacting with the SIM, or NULL
92 * for a SIM that handle its own locking to enable multi
94 * @param max_dev_transactions Maximum number of concurrent untagged
95 * transactions possible
96 * @param max_tagged_dev_transactions Maximum number of concurrent tagged
97 * transactions possible.
98 * @param queue The cam_devq to use for this SIM.
101 cam_sim_alloc(sim_action_func sim_action, sim_poll_func sim_poll,
102 const char *sim_name, void *softc, uint32_t unit,
103 struct mtx *mtx, int max_dev_transactions,
104 int max_tagged_dev_transactions, struct cam_devq *queue)
108 sim = malloc(sizeof(struct cam_sim), M_CAMSIM, M_ZERO | M_NOWAIT);
112 sim->sim_action = sim_action;
113 sim->sim_poll = sim_poll;
114 sim->sim_name = sim_name;
116 sim->path_id = CAM_PATH_ANY;
117 sim->unit_number = unit;
118 sim->bus_id = 0; /* set in xpt_bus_register */
119 sim->max_tagged_dev_openings = max_tagged_dev_transactions;
120 sim->max_dev_openings = max_dev_transactions;
129 * @brief frees up the sim
131 * Frees up the CAM @c sim and optionally the devq. If a mutex is associated
132 * with the sim, it must be locked on entry. It will remain locked on
135 * This function will wait for all outstanding reference to the sim to clear
138 * @param sim The sim to free
139 * @param free_devq Free the devq associated with the sim at creation.
142 cam_sim_free(struct cam_sim *sim, int free_devq)
145 int error __diagused;
147 if (sim->mtx == NULL) {
148 mtx = &cam_sim_free_mtx;
152 mtx_assert(mtx, MA_OWNED);
154 KASSERT(sim->refcount >= 1, ("sim->refcount >= 1"));
156 if (sim->refcount > 0) {
157 error = msleep(sim, mtx, PRIBIO, "simfree", 0);
158 KASSERT(error == 0, ("invalid error value for msleep(9)"));
160 KASSERT(sim->refcount == 0, ("sim->refcount == 0"));
161 if (mtx == &cam_sim_free_mtx) /* sim->mtx == NULL */
165 cam_simq_free(sim->devq);
170 cam_sim_release(struct cam_sim *sim)
174 if (sim->mtx == NULL)
175 mtx = &cam_sim_free_mtx;
176 else if (!mtx_owned(sim->mtx))
179 mtx = NULL; /* We hold the lock. */
182 KASSERT(sim->refcount >= 1, ("sim->refcount >= 1"));
184 if (sim->refcount == 0)
191 cam_sim_hold(struct cam_sim *sim)
195 if (sim->mtx == NULL)
196 mtx = &cam_sim_free_mtx;
197 else if (!mtx_owned(sim->mtx))
200 mtx = NULL; /* We hold the lock. */
203 KASSERT(sim->refcount >= 1, ("sim->refcount >= 1"));
210 cam_sim_set_path(struct cam_sim *sim, uint32_t path_id)
212 sim->path_id = path_id;