xref: /freebsd/sys/cam/cam_sim.c (revision 3ff01b231dfa83d518854c63e7c9cd1debd1139e)
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 
49 static MALLOC_DEFINE(M_CAMSIM, "CAM SIM", "CAM SIM buffers");
50 
51 static struct mtx cam_sim_free_mtx;
52 MTX_SYSINIT(cam_sim_free_init, &cam_sim_free_mtx, "CAM SIM free lock", MTX_DEF);
53 
54 struct cam_devq *
55 cam_simq_alloc(u_int32_t max_sim_transactions)
56 {
57 	return (cam_devq_alloc(/*size*/0, max_sim_transactions));
58 }
59 
60 void
61 cam_simq_free(struct cam_devq *devq)
62 {
63 	cam_devq_free(devq);
64 }
65 
66 
67 
68 /**
69  * @brief allocate a new sim and fill in the details
70  *
71  * A Storage Interface Module (SIM) is the interface between CAM and
72  * hardware. SIM receives CCBs from CAM via @p sim_action callback and
73  * translates them into DMA or other hardware transactions.  During system
74  * dumps, it can be polled with the @p sim_poll callback. CCB processing is
75  * terminated by calling @c xpt_done().
76  *
77  * The @p mtx acts as a perimeter lock for the SIM. All calls into the SIM's
78  * @p sim_action are made with this lock held. It is also used to hold/release
79  * a SIM, managing its reference count. When the lock is NULL, the SIM is 100%
80  * responsible for locking (and the reference counting is done with a shared
81  * lock.
82  *
83  * The cam_devq passed in (@c queue) is used to arbitrate the number of
84  * outstanding transactions to the SIM. For HBAs that have global limits shared
85  * between the different buses, the same devq should be specified for each bus
86  * attached to the SIM.
87  *
88  * @param sim_action	Function to call to process CCBs
89  * @param sim_poll	Function to poll the hardware for completions
90  * @param sim_name	Name of SIM class
91  * @param softc		Software context associated with the SIM
92  * @param unit		Unit number of SIM
93  * @param mtx		Mutex to lock while interacting with the SIM, or NULL
94  *			for a SIM that handle its own locking to enable multi
95  *			queue support.
96  * @param max_dev_transactions Maximum number of concurrent untagged
97  *			transactions possible
98  * @param max_tagged_dev_transactions Maximum number of concurrent tagged
99  *			transactions possible.
100  * @param queue		The cam_devq to use for this SIM.
101  */
102 struct cam_sim *
103 cam_sim_alloc(sim_action_func sim_action, sim_poll_func sim_poll,
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->sim_dev = NULL;	/* set only by cam_sim_alloc_dev */
120 	sim->unit_number = unit;
121 	sim->bus_id = 0;	/* set in xpt_bus_register */
122 	sim->max_tagged_dev_openings = max_tagged_dev_transactions;
123 	sim->max_dev_openings = max_dev_transactions;
124 	sim->flags = 0;
125 	sim->refcount = 1;
126 	sim->devq = queue;
127 	sim->mtx = mtx;
128 	return (sim);
129 }
130 
131 /**
132  * @brief allocate a new sim and fill in the details with a device_t
133  *
134  * Just like @c cam_sim_alloc, but with an additional paramter.
135  *
136  * @param dev		A newbus device that's associated with the
137  *			sim. Must be non-NULL.
138  */
139 struct cam_sim *
140 cam_sim_alloc_dev(sim_action_func sim_action, sim_poll_func sim_poll,
141     const char *sim_name, void *softc, device_t dev, struct mtx *mtx,
142     int max_dev_transactions, int max_tagged_dev_transactions,
143     struct cam_devq *queue)
144 {
145 	struct cam_sim *sim;
146 
147 	KASSERT(dev != NULL, ("%s: dev is null for sim_name %s softc %p\n",
148 	    __func__, sim_name, softc));
149 
150 	sim = cam_sim_alloc(sim_action, sim_poll, sim_name, softc,
151 	    device_get_unit(dev), mtx, max_dev_transactions,
152 	    max_tagged_dev_transactions, queue);
153 	if (sim != NULL)
154 		sim->sim_dev = dev;
155 	return (sim);
156 }
157 
158 void
159 cam_sim_free(struct cam_sim *sim, int free_devq)
160 {
161 	struct mtx *mtx;
162 	int error;
163 
164 	if (sim->mtx == NULL) {
165 		mtx = &cam_sim_free_mtx;
166 		mtx_lock(mtx);
167 	} else {
168 		mtx = sim->mtx;
169 		mtx_assert(mtx, MA_OWNED);
170 	}
171 	KASSERT(sim->refcount >= 1, ("sim->refcount >= 1"));
172 	sim->refcount--;
173 	if (sim->refcount > 0) {
174 		error = msleep(sim, mtx, PRIBIO, "simfree", 0);
175 		KASSERT(error == 0, ("invalid error value for msleep(9)"));
176 	}
177 	KASSERT(sim->refcount == 0, ("sim->refcount == 0"));
178 	if (mtx == &cam_sim_free_mtx)	/* sim->mtx == NULL */
179 		mtx_unlock(mtx);
180 
181 	if (free_devq)
182 		cam_simq_free(sim->devq);
183 	free(sim, M_CAMSIM);
184 }
185 
186 void
187 cam_sim_release(struct cam_sim *sim)
188 {
189 	struct mtx *mtx;
190 
191 	if (sim->mtx == NULL)
192 		mtx = &cam_sim_free_mtx;
193 	else if (!mtx_owned(sim->mtx))
194 		mtx = sim->mtx;
195 	else
196 		mtx = NULL;	/* We hold the lock. */
197 	if (mtx)
198 		mtx_lock(mtx);
199 	KASSERT(sim->refcount >= 1, ("sim->refcount >= 1"));
200 	sim->refcount--;
201 	if (sim->refcount == 0)
202 		wakeup(sim);
203 	if (mtx)
204 		mtx_unlock(mtx);
205 }
206 
207 void
208 cam_sim_hold(struct cam_sim *sim)
209 {
210 	struct mtx *mtx;
211 
212 	if (sim->mtx == NULL)
213 		mtx = &cam_sim_free_mtx;
214 	else if (!mtx_owned(sim->mtx))
215 		mtx = sim->mtx;
216 	else
217 		mtx = NULL;	/* We hold the lock. */
218 	if (mtx)
219 		mtx_lock(mtx);
220 	KASSERT(sim->refcount >= 1, ("sim->refcount >= 1"));
221 	sim->refcount++;
222 	if (mtx)
223 		mtx_unlock(mtx);
224 }
225 
226 void
227 cam_sim_set_path(struct cam_sim *sim, u_int32_t path_id)
228 {
229 	sim->path_id = path_id;
230 }
231