xref: /freebsd/sys/dev/iscsi/icl.c (revision 3416500aef140042c64bc149cb1ec6620483bc44)
1 /*-
2  * Copyright (c) 2012 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Edward Tomasz Napierala under sponsorship
6  * from the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  *
29  */
30 
31 /*
32  * iSCSI Common Layer.  It's used by both the initiator and target to send
33  * and receive iSCSI PDUs.
34  */
35 
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 #include <sys/param.h>
40 #include <sys/condvar.h>
41 #include <sys/conf.h>
42 #include <sys/lock.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/sbuf.h>
49 #include <sys/socket.h>
50 #include <sys/sysctl.h>
51 #include <sys/systm.h>
52 #include <sys/sx.h>
53 
54 #include <dev/iscsi/icl.h>
55 #include <icl_conn_if.h>
56 
57 struct icl_module {
58 	TAILQ_ENTRY(icl_module)		im_next;
59 	char				*im_name;
60 	bool				im_iser;
61 	int				im_priority;
62 	int				(*im_limits)(struct icl_drv_limits *idl);
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, 0, "iSCSI Common Layer");
77 int icl_debug = 1;
78 SYSCTL_INT(_kern_icl, OID_AUTO, debug, CTLFLAG_RWTUN,
79     &icl_debug, 0, "Enable debug messages");
80 SYSCTL_PROC(_kern_icl, OID_AUTO, offloads,
81     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
82     NULL, false, sysctl_kern_icl_offloads, "A",
83     "List of ICL modules");
84 SYSCTL_PROC(_kern_icl, OID_AUTO, iser_offloads,
85     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
86     NULL, true, sysctl_kern_icl_offloads, "A",
87     "List of iSER ICL modules");
88 
89 static int
90 sysctl_kern_icl_offloads(SYSCTL_HANDLER_ARGS)
91 {
92 	const struct icl_module *im;
93 	struct sbuf sb;
94 	bool iser = arg2;
95 	int error;
96 
97 	sbuf_new(&sb, NULL, 256, SBUF_AUTOEXTEND | SBUF_INCLUDENUL);
98 
99 	sx_slock(&sc->sc_lock);
100 	TAILQ_FOREACH(im, &sc->sc_modules, im_next) {
101 		if (im->im_iser != iser)
102 			continue;
103 		if (im != TAILQ_FIRST(&sc->sc_modules))
104 			sbuf_putc(&sb, ' ');
105 		sbuf_printf(&sb, "%s", im->im_name);
106 	}
107 	sx_sunlock(&sc->sc_lock);
108 
109 	error = sbuf_finish(&sb);
110 	if (error == 0)
111 		error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb));
112 	sbuf_delete(&sb);
113 	return (error);
114 }
115 
116 static struct icl_module *
117 icl_find(const char *name, bool iser, bool quiet)
118 {
119 	struct icl_module *im, *im_max;
120 
121 	sx_assert(&sc->sc_lock, SA_LOCKED);
122 
123 	/*
124 	 * If the name was not specified, pick a module with highest
125 	 * priority.
126 	 */
127 	if (name == NULL || name[0] == '\0') {
128 		im_max = NULL;
129 		TAILQ_FOREACH(im, &sc->sc_modules, im_next) {
130 			if (im->im_iser != iser)
131 				continue;
132 			if (im_max == NULL ||
133 			    im->im_priority > im_max->im_priority)
134 				im_max = im;
135 		}
136 
137 		if (iser && im_max == NULL && !quiet)
138 			ICL_WARN("no iSER-capable offload found");
139 
140 		return (im_max);
141 	}
142 
143 	TAILQ_FOREACH(im, &sc->sc_modules, im_next) {
144 		if (strcasecmp(im->im_name, name) != 0)
145 			continue;
146 
147 		if (!im->im_iser && iser && !quiet) {
148 			ICL_WARN("offload \"%s\" is not iSER-capable", name);
149 			return (NULL);
150 		}
151 		if (im->im_iser && !iser && !quiet) {
152 			ICL_WARN("offload \"%s\" is iSER-only", name);
153 			return (NULL);
154 		}
155 
156 		return (im);
157 	}
158 
159 	if (!quiet)
160 		ICL_WARN("offload \"%s\" not found", name);
161 
162 	return (NULL);
163 }
164 
165 struct icl_conn *
166 icl_new_conn(const char *offload, bool iser, const char *name, struct mtx *lock)
167 {
168 	struct icl_module *im;
169 	struct icl_conn *ic;
170 
171 	sx_slock(&sc->sc_lock);
172 	im = icl_find(offload, iser, false);
173 	if (im == NULL) {
174 		sx_sunlock(&sc->sc_lock);
175 		return (NULL);
176 	}
177 
178 	ic = im->im_new_conn(name, lock);
179 	sx_sunlock(&sc->sc_lock);
180 
181 	return (ic);
182 }
183 
184 int
185 icl_limits(const char *offload, bool iser, struct icl_drv_limits *idl)
186 {
187 	struct icl_module *im;
188 	int error;
189 
190 	bzero(idl, sizeof(*idl));
191 	sx_slock(&sc->sc_lock);
192 	im = icl_find(offload, iser, false);
193 	if (im == NULL) {
194 		sx_sunlock(&sc->sc_lock);
195 		return (ENXIO);
196 	}
197 
198 	error = im->im_limits(idl);
199 	sx_sunlock(&sc->sc_lock);
200 
201 	/*
202 	 * Validate the limits provided by the driver against values allowed by
203 	 * the iSCSI RFC.  0 means iscsid/ctld should pick a reasonable value.
204 	 *
205 	 * Note that max_send_dsl is an internal implementation detail and not
206 	 * part of the RFC.
207 	 */
208 #define OUT_OF_RANGE(x, lo, hi) ((x) != 0 && ((x) < (lo) || (x) > (hi)))
209 	if (error == 0 &&
210 	    (OUT_OF_RANGE(idl->idl_max_recv_data_segment_length, 512, 16777215) ||
211 	    OUT_OF_RANGE(idl->idl_max_send_data_segment_length, 512, 16777215) ||
212 	    OUT_OF_RANGE(idl->idl_max_burst_length, 512, 16777215) ||
213 	    OUT_OF_RANGE(idl->idl_first_burst_length, 512, 16777215))) {
214 		error = EINVAL;
215 	}
216 #undef OUT_OF_RANGE
217 
218 	/*
219 	 * If both first_burst and max_burst are provided then first_burst must
220 	 * not exceed max_burst.
221 	 */
222 	if (error == 0 && idl->idl_first_burst_length > 0 &&
223 	    idl->idl_max_burst_length > 0 &&
224 	    idl->idl_first_burst_length > idl->idl_max_burst_length) {
225 		error = EINVAL;
226 	}
227 
228 	return (error);
229 }
230 
231 int
232 icl_register(const char *offload, bool iser, int priority,
233     int (*limits)(struct icl_drv_limits *),
234     struct icl_conn *(*new_conn)(const char *, struct mtx *))
235 {
236 	struct icl_module *im;
237 
238 	sx_xlock(&sc->sc_lock);
239 	im = icl_find(offload, iser, true);
240 	if (im != NULL) {
241 		ICL_WARN("offload \"%s\" already registered", offload);
242 		sx_xunlock(&sc->sc_lock);
243 		return (EBUSY);
244 	}
245 
246 	im = malloc(sizeof(*im), M_ICL, M_ZERO | M_WAITOK);
247 	im->im_name = strdup(offload, M_ICL);
248 	im->im_iser = iser;
249 	im->im_priority = priority;
250 	im->im_limits = limits;
251 	im->im_new_conn = new_conn;
252 
253 	TAILQ_INSERT_HEAD(&sc->sc_modules, im, im_next);
254 	sx_xunlock(&sc->sc_lock);
255 
256 	ICL_DEBUG("offload \"%s\" registered", offload);
257 	return (0);
258 }
259 
260 int
261 icl_unregister(const char *offload, bool rdma)
262 {
263 	struct icl_module *im;
264 
265 	sx_xlock(&sc->sc_lock);
266 	im = icl_find(offload, rdma, true);
267 	if (im == NULL) {
268 		ICL_WARN("offload \"%s\" not registered", offload);
269 		sx_xunlock(&sc->sc_lock);
270 		return (ENXIO);
271 	}
272 
273 	TAILQ_REMOVE(&sc->sc_modules, im, im_next);
274 	sx_xunlock(&sc->sc_lock);
275 
276 	free(im->im_name, M_ICL);
277 	free(im, M_ICL);
278 
279 	ICL_DEBUG("offload \"%s\" unregistered", offload);
280 	return (0);
281 }
282 
283 static int
284 icl_load(void)
285 {
286 
287 	sc = malloc(sizeof(*sc), M_ICL, M_ZERO | M_WAITOK);
288 	sx_init(&sc->sc_lock, "icl");
289 	TAILQ_INIT(&sc->sc_modules);
290 
291 	return (0);
292 }
293 
294 static int
295 icl_unload(void)
296 {
297 
298 	sx_slock(&sc->sc_lock);
299 	KASSERT(TAILQ_EMPTY(&sc->sc_modules), ("still have modules"));
300 	sx_sunlock(&sc->sc_lock);
301 
302 	sx_destroy(&sc->sc_lock);
303 	free(sc, M_ICL);
304 
305 	return (0);
306 }
307 
308 static int
309 icl_modevent(module_t mod, int what, void *arg)
310 {
311 
312 	switch (what) {
313 	case MOD_LOAD:
314 		return (icl_load());
315 	case MOD_UNLOAD:
316 		return (icl_unload());
317 	default:
318 		return (EINVAL);
319 	}
320 }
321 
322 moduledata_t icl_data = {
323 	"icl",
324 	icl_modevent,
325 	0
326 };
327 
328 DECLARE_MODULE(icl, icl_data, SI_SUB_DRIVERS, SI_ORDER_FIRST);
329 MODULE_VERSION(icl, 1);
330