2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 The FreeBSD Foundation
6 * This software was developed by Edward Tomasz Napierala under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
33 * iSCSI Common Layer. It's used by both the initiator and target to send
34 * and receive iSCSI PDUs.
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
41 #include <sys/condvar.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/mutex.h>
47 #include <sys/module.h>
48 #include <sys/queue.h>
50 #include <sys/socket.h>
51 #include <sys/sysctl.h>
52 #include <sys/systm.h>
55 #include <dev/iscsi/icl.h>
56 #include <icl_conn_if.h>
59 TAILQ_ENTRY(icl_module) im_next;
63 int (*im_limits)(struct icl_drv_limits *idl);
64 struct icl_conn *(*im_new_conn)(const char *name,
70 TAILQ_HEAD(, icl_module) sc_modules;
73 static int sysctl_kern_icl_offloads(SYSCTL_HANDLER_ARGS);
74 static MALLOC_DEFINE(M_ICL, "icl", "iSCSI Common Layer");
75 static struct icl_softc *sc;
77 SYSCTL_NODE(_kern, OID_AUTO, icl, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
78 "iSCSI Common Layer");
80 SYSCTL_INT(_kern_icl, OID_AUTO, debug, CTLFLAG_RWTUN,
81 &icl_debug, 0, "Enable debug messages");
82 SYSCTL_PROC(_kern_icl, OID_AUTO, offloads,
83 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
84 NULL, false, sysctl_kern_icl_offloads, "A",
85 "List of ICL modules");
86 SYSCTL_PROC(_kern_icl, OID_AUTO, iser_offloads,
87 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
88 NULL, true, sysctl_kern_icl_offloads, "A",
89 "List of iSER ICL modules");
92 sysctl_kern_icl_offloads(SYSCTL_HANDLER_ARGS)
94 const struct icl_module *im;
99 sbuf_new(&sb, NULL, 256, SBUF_AUTOEXTEND | SBUF_INCLUDENUL);
101 sx_slock(&sc->sc_lock);
102 TAILQ_FOREACH(im, &sc->sc_modules, im_next) {
103 if (im->im_iser != iser)
105 if (im != TAILQ_FIRST(&sc->sc_modules))
107 sbuf_printf(&sb, "%s", im->im_name);
109 sx_sunlock(&sc->sc_lock);
111 error = sbuf_finish(&sb);
113 error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb));
118 static struct icl_module *
119 icl_find(const char *name, bool iser, bool quiet)
121 struct icl_module *im, *im_max;
123 sx_assert(&sc->sc_lock, SA_LOCKED);
126 * If the name was not specified, pick a module with highest
129 if (name == NULL || name[0] == '\0') {
131 TAILQ_FOREACH(im, &sc->sc_modules, im_next) {
132 if (im->im_iser != iser)
134 if (im_max == NULL ||
135 im->im_priority > im_max->im_priority)
139 if (iser && im_max == NULL && !quiet)
140 ICL_WARN("no iSER-capable offload found");
145 TAILQ_FOREACH(im, &sc->sc_modules, im_next) {
146 if (strcasecmp(im->im_name, name) != 0)
149 if (!im->im_iser && iser && !quiet) {
150 ICL_WARN("offload \"%s\" is not iSER-capable", name);
153 if (im->im_iser && !iser && !quiet) {
154 ICL_WARN("offload \"%s\" is iSER-only", name);
162 ICL_WARN("offload \"%s\" not found", name);
168 icl_new_conn(const char *offload, bool iser, const char *name, struct mtx *lock)
170 struct icl_module *im;
173 sx_slock(&sc->sc_lock);
174 im = icl_find(offload, iser, false);
176 sx_sunlock(&sc->sc_lock);
180 ic = im->im_new_conn(name, lock);
181 sx_sunlock(&sc->sc_lock);
187 icl_limits(const char *offload, bool iser, struct icl_drv_limits *idl)
189 struct icl_module *im;
192 bzero(idl, sizeof(*idl));
193 sx_slock(&sc->sc_lock);
194 im = icl_find(offload, iser, false);
196 sx_sunlock(&sc->sc_lock);
200 error = im->im_limits(idl);
201 sx_sunlock(&sc->sc_lock);
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.
207 * Note that max_send_dsl is an internal implementation detail and not
210 #define OUT_OF_RANGE(x, lo, hi) ((x) != 0 && ((x) < (lo) || (x) > (hi)))
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))) {
221 * If both first_burst and max_burst are provided then first_burst must
222 * not exceed max_burst.
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) {
234 icl_register(const char *offload, bool iser, int priority,
235 int (*limits)(struct icl_drv_limits *),
236 struct icl_conn *(*new_conn)(const char *, struct mtx *))
238 struct icl_module *im;
240 sx_xlock(&sc->sc_lock);
241 im = icl_find(offload, iser, true);
243 ICL_WARN("offload \"%s\" already registered", offload);
244 sx_xunlock(&sc->sc_lock);
248 im = malloc(sizeof(*im), M_ICL, M_ZERO | M_WAITOK);
249 im->im_name = strdup(offload, M_ICL);
251 im->im_priority = priority;
252 im->im_limits = limits;
253 im->im_new_conn = new_conn;
255 TAILQ_INSERT_HEAD(&sc->sc_modules, im, im_next);
256 sx_xunlock(&sc->sc_lock);
258 ICL_DEBUG("offload \"%s\" registered", offload);
263 icl_unregister(const char *offload, bool rdma)
265 struct icl_module *im;
267 sx_xlock(&sc->sc_lock);
268 im = icl_find(offload, rdma, true);
270 ICL_WARN("offload \"%s\" not registered", offload);
271 sx_xunlock(&sc->sc_lock);
275 TAILQ_REMOVE(&sc->sc_modules, im, im_next);
276 sx_xunlock(&sc->sc_lock);
278 free(im->im_name, M_ICL);
281 ICL_DEBUG("offload \"%s\" unregistered", offload);
289 sc = malloc(sizeof(*sc), M_ICL, M_ZERO | M_WAITOK);
290 sx_init(&sc->sc_lock, "icl");
291 TAILQ_INIT(&sc->sc_modules);
300 sx_slock(&sc->sc_lock);
301 KASSERT(TAILQ_EMPTY(&sc->sc_modules), ("still have modules"));
302 sx_sunlock(&sc->sc_lock);
304 sx_destroy(&sc->sc_lock);
311 icl_modevent(module_t mod, int what, void *arg)
318 return (icl_unload());
324 moduledata_t icl_data = {
330 DECLARE_MODULE(icl, icl_data, SI_SUB_DRIVERS, SI_ORDER_FIRST);
331 MODULE_VERSION(icl, 1);