1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2012 The FreeBSD Foundation 5 * All rights reserved. 6 * 7 * This software was developed by Edward Tomasz Napierala under sponsorship 8 * from the FreeBSD Foundation. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 */ 32 33 /* 34 * iSCSI Common Layer. It's used by both the initiator and target to send 35 * and receive iSCSI PDUs. 36 */ 37 38 #include <sys/cdefs.h> 39 __FBSDID("$FreeBSD$"); 40 41 #include <sys/param.h> 42 #include <sys/condvar.h> 43 #include <sys/conf.h> 44 #include <sys/lock.h> 45 #include <sys/kernel.h> 46 #include <sys/malloc.h> 47 #include <sys/mutex.h> 48 #include <sys/module.h> 49 #include <sys/queue.h> 50 #include <sys/sbuf.h> 51 #include <sys/socket.h> 52 #include <sys/sysctl.h> 53 #include <sys/systm.h> 54 #include <sys/sx.h> 55 56 #include <dev/iscsi/icl.h> 57 #include <icl_conn_if.h> 58 59 struct icl_module { 60 TAILQ_ENTRY(icl_module) im_next; 61 char *im_name; 62 bool im_iser; 63 int im_priority; 64 int (*im_limits)(struct icl_drv_limits *idl); 65 struct icl_conn *(*im_new_conn)(const char *name, 66 struct mtx *lock); 67 }; 68 69 struct icl_softc { 70 struct sx sc_lock; 71 TAILQ_HEAD(, icl_module) sc_modules; 72 }; 73 74 static int sysctl_kern_icl_offloads(SYSCTL_HANDLER_ARGS); 75 static MALLOC_DEFINE(M_ICL, "icl", "iSCSI Common Layer"); 76 static struct icl_softc *sc; 77 78 SYSCTL_NODE(_kern, OID_AUTO, icl, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 79 "iSCSI Common Layer"); 80 int icl_debug = 1; 81 SYSCTL_INT(_kern_icl, OID_AUTO, debug, CTLFLAG_RWTUN, 82 &icl_debug, 0, "Enable debug messages"); 83 SYSCTL_PROC(_kern_icl, OID_AUTO, offloads, 84 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 85 NULL, false, sysctl_kern_icl_offloads, "A", 86 "List of ICL modules"); 87 SYSCTL_PROC(_kern_icl, OID_AUTO, iser_offloads, 88 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 89 NULL, true, sysctl_kern_icl_offloads, "A", 90 "List of iSER ICL modules"); 91 92 static int 93 sysctl_kern_icl_offloads(SYSCTL_HANDLER_ARGS) 94 { 95 const struct icl_module *im; 96 struct sbuf sb; 97 bool iser = arg2; 98 int error; 99 100 sbuf_new(&sb, NULL, 256, SBUF_AUTOEXTEND | SBUF_INCLUDENUL); 101 102 sx_slock(&sc->sc_lock); 103 TAILQ_FOREACH(im, &sc->sc_modules, im_next) { 104 if (im->im_iser != iser) 105 continue; 106 if (im != TAILQ_FIRST(&sc->sc_modules)) 107 sbuf_putc(&sb, ' '); 108 sbuf_printf(&sb, "%s", im->im_name); 109 } 110 sx_sunlock(&sc->sc_lock); 111 112 error = sbuf_finish(&sb); 113 if (error == 0) 114 error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb)); 115 sbuf_delete(&sb); 116 return (error); 117 } 118 119 static struct icl_module * 120 icl_find(const char *name, bool iser, bool quiet) 121 { 122 struct icl_module *im, *im_max; 123 124 sx_assert(&sc->sc_lock, SA_LOCKED); 125 126 /* 127 * If the name was not specified, pick a module with highest 128 * priority. 129 */ 130 if (name == NULL || name[0] == '\0') { 131 im_max = NULL; 132 TAILQ_FOREACH(im, &sc->sc_modules, im_next) { 133 if (im->im_iser != iser) 134 continue; 135 if (im_max == NULL || 136 im->im_priority > im_max->im_priority) 137 im_max = im; 138 } 139 140 if (iser && im_max == NULL && !quiet) 141 ICL_WARN("no iSER-capable offload found"); 142 143 return (im_max); 144 } 145 146 TAILQ_FOREACH(im, &sc->sc_modules, im_next) { 147 if (strcasecmp(im->im_name, name) != 0) 148 continue; 149 150 if (!im->im_iser && iser && !quiet) { 151 ICL_WARN("offload \"%s\" is not iSER-capable", name); 152 return (NULL); 153 } 154 if (im->im_iser && !iser && !quiet) { 155 ICL_WARN("offload \"%s\" is iSER-only", name); 156 return (NULL); 157 } 158 159 return (im); 160 } 161 162 if (!quiet) 163 ICL_WARN("offload \"%s\" not found", name); 164 165 return (NULL); 166 } 167 168 struct icl_conn * 169 icl_new_conn(const char *offload, bool iser, const char *name, struct mtx *lock) 170 { 171 struct icl_module *im; 172 struct icl_conn *ic; 173 174 sx_slock(&sc->sc_lock); 175 im = icl_find(offload, iser, false); 176 if (im == NULL) { 177 sx_sunlock(&sc->sc_lock); 178 return (NULL); 179 } 180 181 ic = im->im_new_conn(name, lock); 182 sx_sunlock(&sc->sc_lock); 183 184 return (ic); 185 } 186 187 int 188 icl_limits(const char *offload, bool iser, struct icl_drv_limits *idl) 189 { 190 struct icl_module *im; 191 int error; 192 193 bzero(idl, sizeof(*idl)); 194 sx_slock(&sc->sc_lock); 195 im = icl_find(offload, iser, false); 196 if (im == NULL) { 197 sx_sunlock(&sc->sc_lock); 198 return (ENXIO); 199 } 200 201 error = im->im_limits(idl); 202 sx_sunlock(&sc->sc_lock); 203 204 /* 205 * Validate the limits provided by the driver against values allowed by 206 * the iSCSI RFC. 0 means iscsid/ctld should pick a reasonable value. 207 * 208 * Note that max_send_dsl is an internal implementation detail and not 209 * part of the RFC. 210 */ 211 #define OUT_OF_RANGE(x, lo, hi) ((x) != 0 && ((x) < (lo) || (x) > (hi))) 212 if (error == 0 && 213 (OUT_OF_RANGE(idl->idl_max_recv_data_segment_length, 512, 16777215) || 214 OUT_OF_RANGE(idl->idl_max_send_data_segment_length, 512, 16777215) || 215 OUT_OF_RANGE(idl->idl_max_burst_length, 512, 16777215) || 216 OUT_OF_RANGE(idl->idl_first_burst_length, 512, 16777215))) { 217 error = EINVAL; 218 } 219 #undef OUT_OF_RANGE 220 221 /* 222 * If both first_burst and max_burst are provided then first_burst must 223 * not exceed max_burst. 224 */ 225 if (error == 0 && idl->idl_first_burst_length > 0 && 226 idl->idl_max_burst_length > 0 && 227 idl->idl_first_burst_length > idl->idl_max_burst_length) { 228 error = EINVAL; 229 } 230 231 return (error); 232 } 233 234 int 235 icl_register(const char *offload, bool iser, int priority, 236 int (*limits)(struct icl_drv_limits *), 237 struct icl_conn *(*new_conn)(const char *, struct mtx *)) 238 { 239 struct icl_module *im; 240 241 sx_xlock(&sc->sc_lock); 242 im = icl_find(offload, iser, true); 243 if (im != NULL) { 244 ICL_WARN("offload \"%s\" already registered", offload); 245 sx_xunlock(&sc->sc_lock); 246 return (EBUSY); 247 } 248 249 im = malloc(sizeof(*im), M_ICL, M_ZERO | M_WAITOK); 250 im->im_name = strdup(offload, M_ICL); 251 im->im_iser = iser; 252 im->im_priority = priority; 253 im->im_limits = limits; 254 im->im_new_conn = new_conn; 255 256 TAILQ_INSERT_HEAD(&sc->sc_modules, im, im_next); 257 sx_xunlock(&sc->sc_lock); 258 259 ICL_DEBUG("offload \"%s\" registered", offload); 260 return (0); 261 } 262 263 int 264 icl_unregister(const char *offload, bool rdma) 265 { 266 struct icl_module *im; 267 268 sx_xlock(&sc->sc_lock); 269 im = icl_find(offload, rdma, true); 270 if (im == NULL) { 271 ICL_WARN("offload \"%s\" not registered", offload); 272 sx_xunlock(&sc->sc_lock); 273 return (ENXIO); 274 } 275 276 TAILQ_REMOVE(&sc->sc_modules, im, im_next); 277 sx_xunlock(&sc->sc_lock); 278 279 free(im->im_name, M_ICL); 280 free(im, M_ICL); 281 282 ICL_DEBUG("offload \"%s\" unregistered", offload); 283 return (0); 284 } 285 286 static int 287 icl_load(void) 288 { 289 290 sc = malloc(sizeof(*sc), M_ICL, M_ZERO | M_WAITOK); 291 sx_init(&sc->sc_lock, "icl"); 292 TAILQ_INIT(&sc->sc_modules); 293 294 return (0); 295 } 296 297 static int 298 icl_unload(void) 299 { 300 301 sx_slock(&sc->sc_lock); 302 KASSERT(TAILQ_EMPTY(&sc->sc_modules), ("still have modules")); 303 sx_sunlock(&sc->sc_lock); 304 305 sx_destroy(&sc->sc_lock); 306 free(sc, M_ICL); 307 308 return (0); 309 } 310 311 static int 312 icl_modevent(module_t mod, int what, void *arg) 313 { 314 315 switch (what) { 316 case MOD_LOAD: 317 return (icl_load()); 318 case MOD_UNLOAD: 319 return (icl_unload()); 320 default: 321 return (EINVAL); 322 } 323 } 324 325 moduledata_t icl_data = { 326 "icl", 327 icl_modevent, 328 0 329 }; 330 331 DECLARE_MODULE(icl, icl_data, SI_SUB_DRIVERS, SI_ORDER_FIRST); 332 MODULE_VERSION(icl, 1); 333