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