2013-09-14 15:29:06 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2012 The FreeBSD Foundation
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This software was developed by Edward Tomasz Napierala under sponsorship
|
|
|
|
* from the FreeBSD Foundation.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* iSCSI Common Layer. It's used by both the initiator and target to send
|
|
|
|
* and receive iSCSI PDUs.
|
|
|
|
*/
|
|
|
|
|
2014-08-21 15:32:38 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2013-09-14 15:29:06 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/condvar.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/lock.h>
|
2015-01-31 07:49:50 +00:00
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/malloc.h>
|
2013-09-14 15:29:06 +00:00
|
|
|
#include <sys/mutex.h>
|
|
|
|
#include <sys/module.h>
|
2015-01-31 07:49:50 +00:00
|
|
|
#include <sys/queue.h>
|
2013-09-14 15:29:06 +00:00
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/sx.h>
|
|
|
|
|
2014-08-21 16:08:17 +00:00
|
|
|
#include <dev/iscsi/icl.h>
|
2015-01-31 07:49:50 +00:00
|
|
|
#include <icl_conn_if.h>
|
|
|
|
|
|
|
|
struct icl_module {
|
|
|
|
TAILQ_ENTRY(icl_module) im_next;
|
|
|
|
char *im_name;
|
|
|
|
int im_priority;
|
|
|
|
int (*im_limits)(size_t *limitp);
|
|
|
|
struct icl_conn *(*im_new_conn)(const char *name,
|
|
|
|
struct mtx *lock);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct icl_softc {
|
|
|
|
struct sx sc_lock;
|
|
|
|
TAILQ_HEAD(, icl_module) sc_modules;
|
|
|
|
};
|
2013-09-14 15:29:06 +00:00
|
|
|
|
|
|
|
SYSCTL_NODE(_kern, OID_AUTO, icl, CTLFLAG_RD, 0, "iSCSI Common Layer");
|
2015-01-31 07:49:50 +00:00
|
|
|
int icl_debug = 1;
|
2014-04-04 08:48:55 +00:00
|
|
|
SYSCTL_INT(_kern_icl, OID_AUTO, debug, CTLFLAG_RWTUN,
|
2015-01-31 07:49:50 +00:00
|
|
|
&icl_debug, 0, "Enable debug messages");
|
2013-09-14 15:29:06 +00:00
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
static MALLOC_DEFINE(M_ICL, "icl", "iSCSI Common Layer");
|
|
|
|
static struct icl_softc *sc;
|
2013-09-14 15:29:06 +00:00
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
static struct icl_module *
|
|
|
|
icl_find(const char *name)
|
2013-09-14 15:29:06 +00:00
|
|
|
{
|
2015-01-31 07:49:50 +00:00
|
|
|
struct icl_module *im, *im_max;
|
2013-09-14 15:29:06 +00:00
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
sx_assert(&sc->sc_lock, SA_LOCKED);
|
2013-09-14 15:29:06 +00:00
|
|
|
|
|
|
|
/*
|
2015-01-31 07:49:50 +00:00
|
|
|
* If the name was not specified, pick a module with highest
|
|
|
|
* priority.
|
2013-09-14 15:29:06 +00:00
|
|
|
*/
|
2015-01-31 07:49:50 +00:00
|
|
|
if (name == NULL || name[0] == '\0') {
|
|
|
|
im_max = TAILQ_FIRST(&sc->sc_modules);
|
|
|
|
TAILQ_FOREACH(im, &sc->sc_modules, im_next) {
|
|
|
|
if (im->im_priority > im_max->im_priority)
|
|
|
|
im_max = im;
|
2013-09-14 15:29:06 +00:00
|
|
|
}
|
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
return (im_max);
|
2013-09-14 15:29:06 +00:00
|
|
|
}
|
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
TAILQ_FOREACH(im, &sc->sc_modules, im_next) {
|
2015-02-08 10:58:25 +00:00
|
|
|
if (strcasecmp(im->im_name, name) == 0)
|
2015-01-31 07:49:50 +00:00
|
|
|
return (im);
|
2013-09-14 15:29:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
struct icl_conn *
|
|
|
|
icl_new_conn(const char *offload, const char *name, struct mtx *lock)
|
2013-09-14 15:29:06 +00:00
|
|
|
{
|
2015-01-31 07:49:50 +00:00
|
|
|
struct icl_module *im;
|
2013-09-14 15:29:06 +00:00
|
|
|
struct icl_conn *ic;
|
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
sx_slock(&sc->sc_lock);
|
|
|
|
im = icl_find(offload);
|
2013-09-14 15:29:06 +00:00
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
if (im == NULL) {
|
|
|
|
ICL_WARN("offload \"%s\" not found", offload);
|
|
|
|
sx_sunlock(&sc->sc_lock);
|
|
|
|
return (NULL);
|
2013-09-14 15:29:06 +00:00
|
|
|
}
|
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
ic = im->im_new_conn(name, lock);
|
|
|
|
sx_sunlock(&sc->sc_lock);
|
2013-09-14 15:29:06 +00:00
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
return (ic);
|
2013-09-14 15:29:06 +00:00
|
|
|
}
|
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
int
|
|
|
|
icl_limits(const char *offload, size_t *limitp)
|
2013-09-14 15:29:06 +00:00
|
|
|
{
|
2015-01-31 07:49:50 +00:00
|
|
|
struct icl_module *im;
|
|
|
|
int error;
|
2014-04-04 15:49:37 +00:00
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
sx_slock(&sc->sc_lock);
|
|
|
|
im = icl_find(offload);
|
2014-10-09 09:12:08 +00:00
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
if (im == NULL) {
|
|
|
|
ICL_WARN("offload \"%s\" not found", offload);
|
|
|
|
sx_sunlock(&sc->sc_lock);
|
|
|
|
return (ENXIO);
|
2013-09-14 15:29:06 +00:00
|
|
|
}
|
2013-10-24 15:54:06 +00:00
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
error = im->im_limits(limitp);
|
|
|
|
sx_sunlock(&sc->sc_lock);
|
2014-04-04 15:49:37 +00:00
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
return (error);
|
2013-09-14 15:29:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2015-01-31 07:49:50 +00:00
|
|
|
icl_register(const char *offload, int priority, int (*limits)(size_t *),
|
|
|
|
struct icl_conn *(*new_conn)(const char *, struct mtx *))
|
2013-09-14 15:29:06 +00:00
|
|
|
{
|
2015-01-31 07:49:50 +00:00
|
|
|
struct icl_module *im;
|
2013-09-14 15:29:06 +00:00
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
sx_xlock(&sc->sc_lock);
|
|
|
|
im = icl_find(offload);
|
2013-09-14 15:29:06 +00:00
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
if (im != NULL) {
|
|
|
|
ICL_WARN("offload \"%s\" already registered", offload);
|
|
|
|
sx_xunlock(&sc->sc_lock);
|
|
|
|
return (EBUSY);
|
2013-09-14 15:29:06 +00:00
|
|
|
}
|
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
im = malloc(sizeof(*im), M_ICL, M_ZERO | M_WAITOK);
|
|
|
|
im->im_name = strdup(offload, M_ICL);
|
|
|
|
im->im_priority = priority;
|
|
|
|
im->im_limits = limits;
|
|
|
|
im->im_new_conn = new_conn;
|
2013-09-14 15:29:06 +00:00
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
TAILQ_INSERT_HEAD(&sc->sc_modules, im, im_next);
|
|
|
|
sx_xunlock(&sc->sc_lock);
|
2013-09-14 15:29:06 +00:00
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
ICL_DEBUG("offload \"%s\" registered", offload);
|
2013-09-14 15:29:06 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
int
|
|
|
|
icl_unregister(const char *offload)
|
2013-09-14 15:29:06 +00:00
|
|
|
{
|
2015-01-31 07:49:50 +00:00
|
|
|
struct icl_module *im;
|
2013-09-14 15:29:06 +00:00
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
sx_xlock(&sc->sc_lock);
|
|
|
|
im = icl_find(offload);
|
2014-04-04 15:49:37 +00:00
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
if (im == NULL) {
|
|
|
|
ICL_WARN("offload \"%s\" not registered", offload);
|
|
|
|
sx_xunlock(&sc->sc_lock);
|
|
|
|
return (ENXIO);
|
2014-04-04 15:49:37 +00:00
|
|
|
}
|
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
TAILQ_REMOVE(&sc->sc_modules, im, im_next);
|
|
|
|
sx_xunlock(&sc->sc_lock);
|
2013-09-14 15:29:06 +00:00
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
free(im->im_name, M_ICL);
|
|
|
|
free(im, M_ICL);
|
2013-09-14 15:29:06 +00:00
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
ICL_DEBUG("offload \"%s\" unregistered", offload);
|
|
|
|
return (0);
|
2013-09-14 15:29:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-01-31 07:49:50 +00:00
|
|
|
icl_load(void)
|
2013-09-14 15:29:06 +00:00
|
|
|
{
|
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
sc = malloc(sizeof(*sc), M_ICL, M_ZERO | M_WAITOK);
|
|
|
|
sx_init(&sc->sc_lock, "icl");
|
|
|
|
TAILQ_INIT(&sc->sc_modules);
|
2013-09-14 15:29:06 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
icl_unload(void)
|
|
|
|
{
|
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
sx_slock(&sc->sc_lock);
|
|
|
|
KASSERT(TAILQ_EMPTY(&sc->sc_modules), ("still have modules"));
|
|
|
|
sx_sunlock(&sc->sc_lock);
|
2013-09-14 15:29:06 +00:00
|
|
|
|
2015-01-31 07:49:50 +00:00
|
|
|
sx_destroy(&sc->sc_lock);
|
|
|
|
free(sc, M_ICL);
|
2013-09-14 15:29:06 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
icl_modevent(module_t mod, int what, void *arg)
|
|
|
|
{
|
|
|
|
|
|
|
|
switch (what) {
|
|
|
|
case MOD_LOAD:
|
2015-01-31 07:49:50 +00:00
|
|
|
return (icl_load());
|
2013-09-14 15:29:06 +00:00
|
|
|
case MOD_UNLOAD:
|
|
|
|
return (icl_unload());
|
|
|
|
default:
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
moduledata_t icl_data = {
|
|
|
|
"icl",
|
|
|
|
icl_modevent,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
DECLARE_MODULE(icl, icl_data, SI_SUB_DRIVERS, SI_ORDER_FIRST);
|
|
|
|
MODULE_VERSION(icl, 1);
|