sys/bus/cam/cam_sim.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | /* * Common functions for SCSI Interface Modules (SIMs). * * Copyright (c) 1997 Justin T. Gibbs. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions, and the following disclaimer, * without modification, immediately at the beginning of the file. * 2. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD: src/sys/cam/cam_sim.c,v 1.3 1999/08/28 00:40:42 peter Exp $ */ #include <sys/param.h> #include <sys/systm.h> #include <sys/malloc.h> #include <sys/kernel.h> #include <sys/lock.h> #include <sys/spinlock.h> #include <sys/spinlock2.h> #include <sys/mplock2.h> #include "cam.h" #include "cam_ccb.h" #include "cam_sim.h" #include "cam_queue.h" #include "cam_xpt_sim.h" #define CAM_PATH_ANY (u_int32_t)-1 MALLOC_DEFINE(M_CAMSIM, "CAM SIM", "CAM SIM buffers"); /* Drivers will use sim_mplock if they need the BGL */ sim_lock sim_mplock; void cam_sim_lock(sim_lock *lock) { if (lock == &sim_mplock) get_mplock(); else lockmgr(lock, LK_EXCLUSIVE); } void cam_sim_unlock(sim_lock *lock) { if (lock == &sim_mplock) rel_mplock(); else lockmgr(lock, LK_RELEASE); } int cam_sim_cond_lock(sim_lock *lock) { if (lock == &sim_mplock) { get_mplock(); return(1); } else if (lockstatus(lock, curthread) != LK_EXCLUSIVE) { lockmgr(lock, LK_EXCLUSIVE); return(1); } return(0); } void cam_sim_cond_unlock(sim_lock *lock, int doun) { if (doun) { if (lock == &sim_mplock) rel_mplock(); else lockmgr(lock, LK_RELEASE); } } /* * lock can be NULL if sim was &dead_sim */ void sim_lock_assert_owned(sim_lock *lock) { if (lock) { if (lock == &sim_mplock) ASSERT_MP_LOCK_HELD(); else KKASSERT(lockowned(lock)); } } void sim_lock_assert_unowned(sim_lock *lock) { if (lock) { if (lock != &sim_mplock) KKASSERT(!lockowned(lock)); } } int sim_lock_sleep(void *ident, int flags, const char *wmesg, int timo, sim_lock *lock) { int retval; if (lock != &sim_mplock) { /* lock should be held already */ KKASSERT(lockowned(lock)); tsleep_interlock(ident, flags); lockmgr(lock, LK_RELEASE); retval = tsleep(ident, flags | PINTERLOCKED, wmesg, timo); } else { retval = tsleep(ident, flags, wmesg, timo); } if (lock != &sim_mplock) { lockmgr(lock, LK_EXCLUSIVE); } return (retval); } struct cam_devq * cam_simq_alloc(u_int32_t max_sim_transactions) { return (cam_devq_alloc(/*size*/0, max_sim_transactions)); } void cam_simq_release(struct cam_devq *devq) { cam_devq_release(devq); } /* * cam_sim_alloc() may potentially be called from an interrupt (?) but * unexpected things happen to the system if malloc() returns NULL so we * use M_INTWAIT anyway. */ 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, sim_lock *lock, int max_dev_transactions, int max_tagged_dev_transactions, struct cam_devq *queue) { struct cam_sim *sim; KKASSERT(lock != NULL); /* * XXX ahd was limited to 256 instead of 512 for unknown reasons, * move that to a global limit here. We may be able to remove this * code, needs testing. */ if (max_dev_transactions > 256) max_dev_transactions = 256; if (max_tagged_dev_transactions > 256) max_tagged_dev_transactions = 256; /* * Allocate a simq or use the supplied (possibly shared) simq. */ if (queue == NULL) queue = cam_simq_alloc(max_tagged_dev_transactions); else cam_devq_reference(queue); sim = kmalloc(sizeof(struct cam_sim), M_CAMSIM, M_INTWAIT | M_ZERO); sim->sim_action = sim_action; sim->sim_poll = sim_poll; sim->sim_name = sim_name; sim->softc = softc; sim->path_id = CAM_PATH_ANY; sim->unit_number = unit; sim->bus_id = 0; /* set in xpt_bus_register */ sim->max_tagged_dev_openings = max_tagged_dev_transactions; sim->max_dev_openings = max_dev_transactions; sim->flags = 0; sim->refcount = 1; sim->devq = queue; sim->lock = lock; if (lock == &sim_mplock) { sim->flags |= 0; callout_init(&sim->callout); } else { sim->flags |= CAM_SIM_MPSAFE; callout_init_mp(&sim->callout); } SLIST_INIT(&sim->ccb_freeq); TAILQ_INIT(&sim->sim_doneq); spin_init(&sim->sim_spin, "cam_sim_alloc"); return (sim); } void cam_sim_set_max_tags(struct cam_sim *sim, int max_tags) { if (max_tags > 256) max_tags = 256; sim->max_tagged_dev_openings = max_tags; cam_devq_set_openings(sim->devq, max_tags); } static void deadsim_poll(struct cam_sim *sim); static void deadsim_action(struct cam_sim *sim, union ccb *ccb); void cam_sim_free(struct cam_sim *sim) { sim->sim_action = deadsim_action; sim->sim_poll = deadsim_poll; cam_sim_release(sim, CAM_SIM_SOFTC); } /* * Note: the devq is still used by individual peripherals even if the * backend sim disappears, so do not destroy it until the sim itself * is no longer needed. */ void cam_sim_release(struct cam_sim *sim, int flags) { if (flags & CAM_SIM_SOFTC) sim->softc = NULL; if (sim->refcount == 1) { if (sim->devq) { cam_simq_release(sim->devq); sim->devq = NULL; } sim->refcount = 0; kfree(sim, M_CAMSIM); } else { --sim->refcount; } } void cam_sim_set_path(struct cam_sim *sim, u_int32_t path_id) { sim->path_id = path_id; } /* * Dead sim poll and action functions. The backend to the sim has gone * away, aka usb, scsi device, etc... deal with it. */ static void deadsim_poll(struct cam_sim *sim) { /* empty */ } static void deadsim_action(struct cam_sim *sim, union ccb *ccb) { ccb->ccb_h.status = CAM_TID_INVALID; xpt_done(ccb); } |