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 struct cam_sim * 68 cam_sim_alloc(sim_action_func sim_action, sim_poll_func sim_poll, 69 const char *sim_name, void *softc, u_int32_t unit, 70 struct mtx *mtx, int max_dev_transactions, 71 int max_tagged_dev_transactions, struct cam_devq *queue) 72 { 73 struct cam_sim *sim; 74 75 sim = malloc(sizeof(struct cam_sim), M_CAMSIM, M_ZERO | M_NOWAIT); 76 if (sim == NULL) 77 return (NULL); 78 79 sim->sim_action = sim_action; 80 sim->sim_poll = sim_poll; 81 sim->sim_name = sim_name; 82 sim->softc = softc; 83 sim->path_id = CAM_PATH_ANY; 84 sim->sim_dev = NULL; /* set only by cam_sim_alloc_dev */ 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 callout_init(&sim->callout, 1); 94 return (sim); 95 } 96 97 struct cam_sim * 98 cam_sim_alloc_dev(sim_action_func sim_action, sim_poll_func sim_poll, 99 const char *sim_name, void *softc, device_t dev, 100 struct mtx *mtx, int max_dev_transactions, 101 int max_tagged_dev_transactions, struct cam_devq *queue) 102 { 103 struct cam_sim *sim; 104 105 KASSERT(dev != NULL, ("%s: dev is null for sim_name %s softc %p\n", 106 __func__, sim_name, softc)); 107 108 sim = cam_sim_alloc(sim_action, sim_poll, sim_name, softc, 109 device_get_unit(dev), mtx, max_dev_transactions, 110 max_tagged_dev_transactions, queue); 111 if (sim != NULL) 112 sim->sim_dev = dev; 113 return (sim); 114 } 115 116 void 117 cam_sim_free(struct cam_sim *sim, int free_devq) 118 { 119 struct mtx *mtx; 120 int error; 121 122 if (sim->mtx == NULL) { 123 mtx = &cam_sim_free_mtx; 124 mtx_lock(mtx); 125 } else { 126 mtx = sim->mtx; 127 mtx_assert(mtx, MA_OWNED); 128 } 129 KASSERT(sim->refcount >= 1, ("sim->refcount >= 1")); 130 sim->refcount--; 131 if (sim->refcount > 0) { 132 error = msleep(sim, mtx, PRIBIO, "simfree", 0); 133 KASSERT(error == 0, ("invalid error value for msleep(9)")); 134 } 135 KASSERT(sim->refcount == 0, ("sim->refcount == 0")); 136 if (mtx == &cam_sim_free_mtx) /* sim->mtx == NULL */ 137 mtx_unlock(mtx); 138 139 if (free_devq) 140 cam_simq_free(sim->devq); 141 free(sim, M_CAMSIM); 142 } 143 144 void 145 cam_sim_release(struct cam_sim *sim) 146 { 147 struct mtx *mtx; 148 149 if (sim->mtx == NULL) 150 mtx = &cam_sim_free_mtx; 151 else if (!mtx_owned(sim->mtx)) 152 mtx = sim->mtx; 153 else 154 mtx = NULL; /* We hold the lock. */ 155 if (mtx) 156 mtx_lock(mtx); 157 KASSERT(sim->refcount >= 1, ("sim->refcount >= 1")); 158 sim->refcount--; 159 if (sim->refcount == 0) 160 wakeup(sim); 161 if (mtx) 162 mtx_unlock(mtx); 163 } 164 165 void 166 cam_sim_hold(struct cam_sim *sim) 167 { 168 struct mtx *mtx; 169 170 if (sim->mtx == NULL) 171 mtx = &cam_sim_free_mtx; 172 else if (!mtx_owned(sim->mtx)) 173 mtx = sim->mtx; 174 else 175 mtx = NULL; /* We hold the lock. */ 176 if (mtx) 177 mtx_lock(mtx); 178 KASSERT(sim->refcount >= 1, ("sim->refcount >= 1")); 179 sim->refcount++; 180 if (mtx) 181 mtx_unlock(mtx); 182 } 183 184 void 185 cam_sim_set_path(struct cam_sim *sim, u_int32_t path_id) 186 { 187 sim->path_id = path_id; 188 } 189