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/sbuf.h> 49 #include <sys/socket.h> 50 #include <sys/sysctl.h> 51 #include <sys/systm.h> 52 #include <sys/sx.h> 53 54 #include <dev/iscsi/icl.h> 55 #include <icl_conn_if.h> 56 57 struct icl_module { 58 TAILQ_ENTRY(icl_module) im_next; 59 char *im_name; 60 int im_priority; 61 int (*im_limits)(size_t *limitp); 62 struct icl_conn *(*im_new_conn)(const char *name, 63 struct mtx *lock); 64 }; 65 66 struct icl_softc { 67 struct sx sc_lock; 68 TAILQ_HEAD(, icl_module) sc_modules; 69 }; 70 71 static int sysctl_kern_icl_drivers(SYSCTL_HANDLER_ARGS); 72 static MALLOC_DEFINE(M_ICL, "icl", "iSCSI Common Layer"); 73 static struct icl_softc *sc; 74 75 SYSCTL_NODE(_kern, OID_AUTO, icl, CTLFLAG_RD, 0, "iSCSI Common Layer"); 76 int icl_debug = 1; 77 SYSCTL_INT(_kern_icl, OID_AUTO, debug, CTLFLAG_RWTUN, 78 &icl_debug, 0, "Enable debug messages"); 79 SYSCTL_PROC(_kern_icl, OID_AUTO, drivers, 80 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 81 NULL, 0, sysctl_kern_icl_drivers, "A", 82 "List of ICL drivers"); 83 84 static int 85 sysctl_kern_icl_drivers(SYSCTL_HANDLER_ARGS) 86 { 87 const struct icl_module *im; 88 struct sbuf sb; 89 int error; 90 91 sbuf_new(&sb, NULL, 256, SBUF_AUTOEXTEND | SBUF_INCLUDENUL); 92 93 sx_slock(&sc->sc_lock); 94 TAILQ_FOREACH(im, &sc->sc_modules, im_next) { 95 if (im != TAILQ_FIRST(&sc->sc_modules)) 96 sbuf_putc(&sb, ' '); 97 sbuf_printf(&sb, "%s", im->im_name); 98 } 99 sx_sunlock(&sc->sc_lock); 100 101 error = sbuf_finish(&sb); 102 if (error == 0) 103 error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb)); 104 sbuf_delete(&sb); 105 return (error); 106 } 107 108 static struct icl_module * 109 icl_find(const char *name) 110 { 111 struct icl_module *im, *im_max; 112 113 sx_assert(&sc->sc_lock, SA_LOCKED); 114 115 /* 116 * If the name was not specified, pick a module with highest 117 * priority. 118 */ 119 if (name == NULL || name[0] == '\0') { 120 im_max = TAILQ_FIRST(&sc->sc_modules); 121 TAILQ_FOREACH(im, &sc->sc_modules, im_next) { 122 if (im->im_priority > im_max->im_priority) 123 im_max = im; 124 } 125 126 return (im_max); 127 } 128 129 TAILQ_FOREACH(im, &sc->sc_modules, im_next) { 130 if (strcasecmp(im->im_name, name) == 0) 131 return (im); 132 } 133 134 return (NULL); 135 } 136 137 struct icl_conn * 138 icl_new_conn(const char *offload, const char *name, struct mtx *lock) 139 { 140 struct icl_module *im; 141 struct icl_conn *ic; 142 143 sx_slock(&sc->sc_lock); 144 im = icl_find(offload); 145 146 if (im == NULL) { 147 ICL_WARN("offload \"%s\" not found", offload); 148 sx_sunlock(&sc->sc_lock); 149 return (NULL); 150 } 151 152 ic = im->im_new_conn(name, lock); 153 sx_sunlock(&sc->sc_lock); 154 155 return (ic); 156 } 157 158 int 159 icl_limits(const char *offload, size_t *limitp) 160 { 161 struct icl_module *im; 162 int error; 163 164 sx_slock(&sc->sc_lock); 165 im = icl_find(offload); 166 167 if (im == NULL) { 168 ICL_WARN("offload \"%s\" not found", offload); 169 sx_sunlock(&sc->sc_lock); 170 return (ENXIO); 171 } 172 173 error = im->im_limits(limitp); 174 sx_sunlock(&sc->sc_lock); 175 176 return (error); 177 } 178 179 180 int 181 icl_register(const char *offload, int priority, int (*limits)(size_t *), 182 struct icl_conn *(*new_conn)(const char *, struct mtx *)) 183 { 184 struct icl_module *im; 185 186 sx_xlock(&sc->sc_lock); 187 im = icl_find(offload); 188 189 if (im != NULL) { 190 ICL_WARN("offload \"%s\" already registered", offload); 191 sx_xunlock(&sc->sc_lock); 192 return (EBUSY); 193 } 194 195 im = malloc(sizeof(*im), M_ICL, M_ZERO | M_WAITOK); 196 im->im_name = strdup(offload, M_ICL); 197 im->im_priority = priority; 198 im->im_limits = limits; 199 im->im_new_conn = new_conn; 200 201 TAILQ_INSERT_HEAD(&sc->sc_modules, im, im_next); 202 sx_xunlock(&sc->sc_lock); 203 204 ICL_DEBUG("offload \"%s\" registered", offload); 205 return (0); 206 } 207 208 int 209 icl_unregister(const char *offload) 210 { 211 struct icl_module *im; 212 213 sx_xlock(&sc->sc_lock); 214 im = icl_find(offload); 215 216 if (im == NULL) { 217 ICL_WARN("offload \"%s\" not registered", offload); 218 sx_xunlock(&sc->sc_lock); 219 return (ENXIO); 220 } 221 222 TAILQ_REMOVE(&sc->sc_modules, im, im_next); 223 sx_xunlock(&sc->sc_lock); 224 225 free(im->im_name, M_ICL); 226 free(im, M_ICL); 227 228 ICL_DEBUG("offload \"%s\" unregistered", offload); 229 return (0); 230 } 231 232 static int 233 icl_load(void) 234 { 235 236 sc = malloc(sizeof(*sc), M_ICL, M_ZERO | M_WAITOK); 237 sx_init(&sc->sc_lock, "icl"); 238 TAILQ_INIT(&sc->sc_modules); 239 240 return (0); 241 } 242 243 static int 244 icl_unload(void) 245 { 246 247 sx_slock(&sc->sc_lock); 248 KASSERT(TAILQ_EMPTY(&sc->sc_modules), ("still have modules")); 249 sx_sunlock(&sc->sc_lock); 250 251 sx_destroy(&sc->sc_lock); 252 free(sc, M_ICL); 253 254 return (0); 255 } 256 257 static int 258 icl_modevent(module_t mod, int what, void *arg) 259 { 260 261 switch (what) { 262 case MOD_LOAD: 263 return (icl_load()); 264 case MOD_UNLOAD: 265 return (icl_unload()); 266 default: 267 return (EINVAL); 268 } 269 } 270 271 moduledata_t icl_data = { 272 "icl", 273 icl_modevent, 274 0 275 }; 276 277 DECLARE_MODULE(icl, icl_data, SI_SUB_DRIVERS, SI_ORDER_FIRST); 278 MODULE_VERSION(icl, 1); 279