2 * Copyright (c) 2012 The FreeBSD Foundation
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
32 * iSCSI Common Layer. It's used by both the initiator and target to send
33 * and receive iSCSI PDUs.
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #include <sys/condvar.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/sysctl.h>
49 #include <sys/systm.h>
52 #include <dev/iscsi/icl.h>
53 #include <icl_conn_if.h>
56 TAILQ_ENTRY(icl_module) im_next;
59 int (*im_limits)(size_t *limitp);
60 struct icl_conn *(*im_new_conn)(const char *name,
66 TAILQ_HEAD(, icl_module) sc_modules;
69 SYSCTL_NODE(_kern, OID_AUTO, icl, CTLFLAG_RD, 0, "iSCSI Common Layer");
71 SYSCTL_INT(_kern_icl, OID_AUTO, debug, CTLFLAG_RWTUN,
72 &icl_debug, 0, "Enable debug messages");
74 static MALLOC_DEFINE(M_ICL, "icl", "iSCSI Common Layer");
75 static struct icl_softc *sc;
77 static struct icl_module *
78 icl_find(const char *name)
80 struct icl_module *im, *im_max;
82 sx_assert(&sc->sc_lock, SA_LOCKED);
85 * If the name was not specified, pick a module with highest
88 if (name == NULL || name[0] == '\0') {
89 im_max = TAILQ_FIRST(&sc->sc_modules);
90 TAILQ_FOREACH(im, &sc->sc_modules, im_next) {
91 if (im->im_priority > im_max->im_priority)
98 TAILQ_FOREACH(im, &sc->sc_modules, im_next) {
99 if (strcasecmp(im->im_name, name) == 0)
107 icl_new_conn(const char *offload, const char *name, struct mtx *lock)
109 struct icl_module *im;
112 sx_slock(&sc->sc_lock);
113 im = icl_find(offload);
116 ICL_WARN("offload \"%s\" not found", offload);
117 sx_sunlock(&sc->sc_lock);
121 ic = im->im_new_conn(name, lock);
122 sx_sunlock(&sc->sc_lock);
128 icl_limits(const char *offload, size_t *limitp)
130 struct icl_module *im;
133 sx_slock(&sc->sc_lock);
134 im = icl_find(offload);
137 ICL_WARN("offload \"%s\" not found", offload);
138 sx_sunlock(&sc->sc_lock);
142 error = im->im_limits(limitp);
143 sx_sunlock(&sc->sc_lock);
150 icl_register(const char *offload, int priority, int (*limits)(size_t *),
151 struct icl_conn *(*new_conn)(const char *, struct mtx *))
153 struct icl_module *im;
155 sx_xlock(&sc->sc_lock);
156 im = icl_find(offload);
159 ICL_WARN("offload \"%s\" already registered", offload);
160 sx_xunlock(&sc->sc_lock);
164 im = malloc(sizeof(*im), M_ICL, M_ZERO | M_WAITOK);
165 im->im_name = strdup(offload, M_ICL);
166 im->im_priority = priority;
167 im->im_limits = limits;
168 im->im_new_conn = new_conn;
170 TAILQ_INSERT_HEAD(&sc->sc_modules, im, im_next);
171 sx_xunlock(&sc->sc_lock);
173 ICL_DEBUG("offload \"%s\" registered", offload);
178 icl_unregister(const char *offload)
180 struct icl_module *im;
182 sx_xlock(&sc->sc_lock);
183 im = icl_find(offload);
186 ICL_WARN("offload \"%s\" not registered", offload);
187 sx_xunlock(&sc->sc_lock);
191 TAILQ_REMOVE(&sc->sc_modules, im, im_next);
192 sx_xunlock(&sc->sc_lock);
194 free(im->im_name, M_ICL);
197 ICL_DEBUG("offload \"%s\" unregistered", offload);
205 sc = malloc(sizeof(*sc), M_ICL, M_ZERO | M_WAITOK);
206 sx_init(&sc->sc_lock, "icl");
207 TAILQ_INIT(&sc->sc_modules);
216 sx_slock(&sc->sc_lock);
217 KASSERT(TAILQ_EMPTY(&sc->sc_modules), ("still have modules"));
218 sx_sunlock(&sc->sc_lock);
220 sx_destroy(&sc->sc_lock);
227 icl_modevent(module_t mod, int what, void *arg)
234 return (icl_unload());
240 moduledata_t icl_data = {
246 DECLARE_MODULE(icl, icl_data, SI_SUB_DRIVERS, SI_ORDER_FIRST);
247 MODULE_VERSION(icl, 1);