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: stable/12/sys/cam/cam_sim.c 367367 2020-11-05 11:19:31Z bz $");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/kernel.h>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 
41 #include <cam/cam.h>
42 #include <cam/cam_ccb.h>
43 #include <cam/cam_sim.h>
44 #include <cam/cam_queue.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 *
cam_simq_alloc(u_int32_t max_sim_transactions)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
cam_simq_free(struct cam_devq * devq)61 cam_simq_free(struct cam_devq *devq)
62 {
63 	cam_devq_free(devq);
64 }
65 
66 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)67 cam_sim_alloc(sim_action_func sim_action, sim_poll_func sim_poll,
68 	      const char *sim_name, void *softc, u_int32_t unit,
69 	      struct mtx *mtx, int max_dev_transactions,
70 	      int max_tagged_dev_transactions, struct cam_devq *queue)
71 {
72 	struct cam_sim *sim;
73 
74 	sim = (struct cam_sim *)malloc(sizeof(struct cam_sim),
75 	    M_CAMSIM, M_ZERO | M_NOWAIT);
76 
77 	if (sim == NULL)
78 		return (NULL);
79 
80 	sim->sim_action = sim_action;
81 	sim->sim_poll = sim_poll;
82 	sim->sim_name = sim_name;
83 	sim->softc = softc;
84 	sim->path_id = CAM_PATH_ANY;
85 	sim->unit_number = unit;
86 	sim->bus_id = 0;	/* set in xpt_bus_register */
87 	sim->max_tagged_dev_openings = max_tagged_dev_transactions;
88 	sim->max_dev_openings = max_dev_transactions;
89 	sim->flags = 0;
90 	sim->refcount = 1;
91 	sim->devq = queue;
92 	sim->mtx = mtx;
93 	if (mtx == &Giant) {
94 		sim->flags |= 0;
95 		callout_init(&sim->callout, 0);
96 	} else {
97 		sim->flags |= CAM_SIM_MPSAFE;
98 		callout_init(&sim->callout, 1);
99 	}
100 	return (sim);
101 }
102 
103 void
cam_sim_free(struct cam_sim * sim,int free_devq)104 cam_sim_free(struct cam_sim *sim, int free_devq)
105 {
106 	struct mtx *mtx;
107 	int error;
108 
109 	if (sim->mtx == NULL) {
110 		mtx = &cam_sim_free_mtx;
111 		mtx_lock(mtx);
112 	} else {
113 		mtx = sim->mtx;
114 		mtx_assert(mtx, MA_OWNED);
115 	}
116 	sim->refcount--;
117 	if (sim->refcount > 0) {
118 		error = msleep(sim, mtx, PRIBIO, "simfree", 0);
119 		KASSERT(error == 0, ("invalid error value for msleep(9)"));
120 	}
121 	KASSERT(sim->refcount == 0, ("sim->refcount == 0"));
122 	if (mtx == &cam_sim_free_mtx)	/* sim->mtx == NULL */
123 		mtx_unlock(mtx);
124 
125 	if (free_devq)
126 		cam_simq_free(sim->devq);
127 	free(sim, M_CAMSIM);
128 }
129 
130 void
cam_sim_release(struct cam_sim * sim)131 cam_sim_release(struct cam_sim *sim)
132 {
133 	struct mtx *mtx;
134 
135 	if (sim->mtx == NULL)
136 		mtx = &cam_sim_free_mtx;
137 	else if (!mtx_owned(sim->mtx))
138 		mtx = sim->mtx;
139 	else
140 		mtx = NULL;	/* We hold the lock. */
141 	if (mtx)
142 		mtx_lock(mtx);
143 	KASSERT(sim->refcount >= 1, ("sim->refcount >= 1"));
144 	sim->refcount--;
145 	if (sim->refcount == 0)
146 		wakeup(sim);
147 	if (mtx)
148 		mtx_unlock(mtx);
149 }
150 
151 void
cam_sim_hold(struct cam_sim * sim)152 cam_sim_hold(struct cam_sim *sim)
153 {
154 	struct mtx *mtx;
155 
156 	if (sim->mtx == NULL)
157 		mtx = &cam_sim_free_mtx;
158 	else if (!mtx_owned(sim->mtx))
159 		mtx = sim->mtx;
160 	else
161 		mtx = NULL;	/* We hold the lock. */
162 	if (mtx)
163 		mtx_lock(mtx);
164 	KASSERT(sim->refcount >= 1, ("sim->refcount >= 1"));
165 	sim->refcount++;
166 	if (mtx)
167 		mtx_unlock(mtx);
168 }
169 
170 void
cam_sim_set_path(struct cam_sim * sim,u_int32_t path_id)171 cam_sim_set_path(struct cam_sim *sim, u_int32_t path_id)
172 {
173 	sim->path_id = path_id;
174 }
175