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