xref: /freebsd/sys/cam/cam_sim.c (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1 /*-
2  * Common functions for SCSI Interface Modules (SIMs).
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
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 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/kernel.h>
35 #include <sys/lock.h>
36 #include <sys/malloc.h>
37 #include <sys/mutex.h>
38 
39 #include <cam/cam.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>
44 
45 #define CAM_PATH_ANY (uint32_t)-1
46 
47 static MALLOC_DEFINE(M_CAMSIM, "CAM SIM", "CAM SIM buffers");
48 
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);
51 
52 struct cam_devq *
53 cam_simq_alloc(uint32_t max_sim_transactions)
54 {
55 	return (cam_devq_alloc(/*size*/0, max_sim_transactions));
56 }
57 
58 void
59 cam_simq_free(struct cam_devq *devq)
60 {
61 	cam_devq_free(devq);
62 }
63 
64 
65 
66 /**
67  * @brief allocate a new sim and fill in the details
68  *
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().
74  *
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
79  * lock.
80  *
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.
85  *
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
93  *			queue support.
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.
99  */
100 struct cam_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)
105 {
106 	struct cam_sim *sim;
107 
108 	sim = malloc(sizeof(struct cam_sim), M_CAMSIM, M_ZERO | M_NOWAIT);
109 	if (sim == NULL)
110 		return (NULL);
111 
112 	sim->sim_action = sim_action;
113 	sim->sim_poll = sim_poll;
114 	sim->sim_name = sim_name;
115 	sim->softc = softc;
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;
121 	sim->flags = 0;
122 	sim->refcount = 1;
123 	sim->devq = queue;
124 	sim->mtx = mtx;
125 	return (sim);
126 }
127 
128 /**
129  * @brief frees up the sim
130  *
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
133  * return.
134  *
135  * This function will wait for all outstanding reference to the sim to clear
136  * before returning.
137  *
138  * @param sim          The sim to free
139  * @param free_devq    Free the devq associated with the sim at creation.
140  */
141 void
142 cam_sim_free(struct cam_sim *sim, int free_devq)
143 {
144 	struct mtx *mtx;
145 	int error __diagused;
146 
147 	if (sim->mtx == NULL) {
148 		mtx = &cam_sim_free_mtx;
149 		mtx_lock(mtx);
150 	} else {
151 		mtx = sim->mtx;
152 		mtx_assert(mtx, MA_OWNED);
153 	}
154 	KASSERT(sim->refcount >= 1, ("sim->refcount >= 1"));
155 	sim->refcount--;
156 	if (sim->refcount > 0) {
157 		error = msleep(sim, mtx, PRIBIO, "simfree", 0);
158 		KASSERT(error == 0, ("invalid error value for msleep(9)"));
159 	}
160 	KASSERT(sim->refcount == 0, ("sim->refcount == 0"));
161 	if (mtx == &cam_sim_free_mtx)	/* sim->mtx == NULL */
162 		mtx_unlock(mtx);
163 
164 	if (free_devq)
165 		cam_simq_free(sim->devq);
166 	free(sim, M_CAMSIM);
167 }
168 
169 void
170 cam_sim_release(struct cam_sim *sim)
171 {
172 	struct mtx *mtx;
173 
174 	if (sim->mtx == NULL)
175 		mtx = &cam_sim_free_mtx;
176 	else if (!mtx_owned(sim->mtx))
177 		mtx = sim->mtx;
178 	else
179 		mtx = NULL;	/* We hold the lock. */
180 	if (mtx)
181 		mtx_lock(mtx);
182 	KASSERT(sim->refcount >= 1, ("sim->refcount >= 1"));
183 	sim->refcount--;
184 	if (sim->refcount == 0)
185 		wakeup(sim);
186 	if (mtx)
187 		mtx_unlock(mtx);
188 }
189 
190 void
191 cam_sim_hold(struct cam_sim *sim)
192 {
193 	struct mtx *mtx;
194 
195 	if (sim->mtx == NULL)
196 		mtx = &cam_sim_free_mtx;
197 	else if (!mtx_owned(sim->mtx))
198 		mtx = sim->mtx;
199 	else
200 		mtx = NULL;	/* We hold the lock. */
201 	if (mtx)
202 		mtx_lock(mtx);
203 	KASSERT(sim->refcount >= 1, ("sim->refcount >= 1"));
204 	sim->refcount++;
205 	if (mtx)
206 		mtx_unlock(mtx);
207 }
208 
209 void
210 cam_sim_set_path(struct cam_sim *sim, uint32_t path_id)
211 {
212 	sim->path_id = path_id;
213 }
214