1 /*- 2 * Copyright (c) 2012 The FreeBSD Foundation 3 * All rights reserved. 4 * 5 * This software was developed by Edward Tomasz Napierala under sponsorship 6 * from the FreeBSD Foundation. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 */ 30 31 /* 32 * iSCSI Common Layer. It's used by both the initiator and target to send 33 * and receive iSCSI PDUs. 34 */ 35 36 #include <sys/cdefs.h> 37 __FBSDID("$FreeBSD$"); 38 39 #include <sys/param.h> 40 #include <sys/condvar.h> 41 #include <sys/conf.h> 42 #include <sys/lock.h> 43 #include <sys/kernel.h> 44 #include <sys/malloc.h> 45 #include <sys/mutex.h> 46 #include <sys/module.h> 47 #include <sys/queue.h> 48 #include <sys/sysctl.h> 49 #include <sys/systm.h> 50 #include <sys/sx.h> 51 52 #include <dev/iscsi/icl.h> 53 #include <icl_conn_if.h> 54 55 struct icl_module { 56 TAILQ_ENTRY(icl_module) im_next; 57 char *im_name; 58 int im_priority; 59 int (*im_limits)(size_t *limitp); 60 struct icl_conn *(*im_new_conn)(const char *name, 61 struct mtx *lock); 62 }; 63 64 struct icl_softc { 65 struct sx sc_lock; 66 TAILQ_HEAD(, icl_module) sc_modules; 67 }; 68 69 SYSCTL_NODE(_kern, OID_AUTO, icl, CTLFLAG_RD, 0, "iSCSI Common Layer"); 70 int icl_debug = 1; 71 SYSCTL_INT(_kern_icl, OID_AUTO, debug, CTLFLAG_RWTUN, 72 &icl_debug, 0, "Enable debug messages"); 73 74 static MALLOC_DEFINE(M_ICL, "icl", "iSCSI Common Layer"); 75 static struct icl_softc *sc; 76 77 static struct icl_module * 78 icl_find(const char *name) 79 { 80 struct icl_module *im, *im_max; 81 82 sx_assert(&sc->sc_lock, SA_LOCKED); 83 84 /* 85 * If the name was not specified, pick a module with highest 86 * priority. 87 */ 88 if (name == NULL || name[0] == '\0') { 89 im_max = TAILQ_FIRST(&sc->sc_modules); 90 TAILQ_FOREACH(im, &sc->sc_modules, im_next) { 91 if (im->im_priority > im_max->im_priority) 92 im_max = im; 93 } 94 95 return (im_max); 96 } 97 98 TAILQ_FOREACH(im, &sc->sc_modules, im_next) { 99 if (strcasecmp(im->im_name, name) == 0) 100 return (im); 101 } 102 103 return (NULL); 104 } 105 106 struct icl_conn * 107 icl_new_conn(const char *offload, const char *name, struct mtx *lock) 108 { 109 struct icl_module *im; 110 struct icl_conn *ic; 111 112 sx_slock(&sc->sc_lock); 113 im = icl_find(offload); 114 115 if (im == NULL) { 116 ICL_WARN("offload \"%s\" not found", offload); 117 sx_sunlock(&sc->sc_lock); 118 return (NULL); 119 } 120 121 ic = im->im_new_conn(name, lock); 122 sx_sunlock(&sc->sc_lock); 123 124 return (ic); 125 } 126 127 int 128 icl_limits(const char *offload, size_t *limitp) 129 { 130 struct icl_module *im; 131 int error; 132 133 sx_slock(&sc->sc_lock); 134 im = icl_find(offload); 135 136 if (im == NULL) { 137 ICL_WARN("offload \"%s\" not found", offload); 138 sx_sunlock(&sc->sc_lock); 139 return (ENXIO); 140 } 141 142 error = im->im_limits(limitp); 143 sx_sunlock(&sc->sc_lock); 144 145 return (error); 146 } 147 148 149 int 150 icl_register(const char *offload, int priority, int (*limits)(size_t *), 151 struct icl_conn *(*new_conn)(const char *, struct mtx *)) 152 { 153 struct icl_module *im; 154 155 sx_xlock(&sc->sc_lock); 156 im = icl_find(offload); 157 158 if (im != NULL) { 159 ICL_WARN("offload \"%s\" already registered", offload); 160 sx_xunlock(&sc->sc_lock); 161 return (EBUSY); 162 } 163 164 im = malloc(sizeof(*im), M_ICL, M_ZERO | M_WAITOK); 165 im->im_name = strdup(offload, M_ICL); 166 im->im_priority = priority; 167 im->im_limits = limits; 168 im->im_new_conn = new_conn; 169 170 TAILQ_INSERT_HEAD(&sc->sc_modules, im, im_next); 171 sx_xunlock(&sc->sc_lock); 172 173 ICL_DEBUG("offload \"%s\" registered", offload); 174 return (0); 175 } 176 177 int 178 icl_unregister(const char *offload) 179 { 180 struct icl_module *im; 181 182 sx_xlock(&sc->sc_lock); 183 im = icl_find(offload); 184 185 if (im == NULL) { 186 ICL_WARN("offload \"%s\" not registered", offload); 187 sx_xunlock(&sc->sc_lock); 188 return (ENXIO); 189 } 190 191 TAILQ_REMOVE(&sc->sc_modules, im, im_next); 192 sx_xunlock(&sc->sc_lock); 193 194 free(im->im_name, M_ICL); 195 free(im, M_ICL); 196 197 ICL_DEBUG("offload \"%s\" unregistered", offload); 198 return (0); 199 } 200 201 static int 202 icl_load(void) 203 { 204 205 sc = malloc(sizeof(*sc), M_ICL, M_ZERO | M_WAITOK); 206 sx_init(&sc->sc_lock, "icl"); 207 TAILQ_INIT(&sc->sc_modules); 208 209 return (0); 210 } 211 212 static int 213 icl_unload(void) 214 { 215 216 sx_slock(&sc->sc_lock); 217 KASSERT(TAILQ_EMPTY(&sc->sc_modules), ("still have modules")); 218 sx_sunlock(&sc->sc_lock); 219 220 sx_destroy(&sc->sc_lock); 221 free(sc, M_ICL); 222 223 return (0); 224 } 225 226 static int 227 icl_modevent(module_t mod, int what, void *arg) 228 { 229 230 switch (what) { 231 case MOD_LOAD: 232 return (icl_load()); 233 case MOD_UNLOAD: 234 return (icl_unload()); 235 default: 236 return (EINVAL); 237 } 238 } 239 240 moduledata_t icl_data = { 241 "icl", 242 icl_modevent, 243 0 244 }; 245 246 DECLARE_MODULE(icl, icl_data, SI_SUB_DRIVERS, SI_ORDER_FIRST); 247 MODULE_VERSION(icl, 1); 248