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