Deleted Added
full compact
ctl_frontend_iscsi.c (277963) ctl_frontend_iscsi.c (278037)
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

--- 12 unchanged lines hidden (view full) ---

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 *
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

--- 12 unchanged lines hidden (view full) ---

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 * $FreeBSD: head/sys/cam/ctl/ctl_frontend_iscsi.c 277963 2015-01-31 07:49:50Z trasz $
29 * $FreeBSD: head/sys/cam/ctl/ctl_frontend_iscsi.c 278037 2015-02-01 21:50:28Z mav $
30 */
31
32/*
33 * CTL frontend for the iSCSI protocol.
34 */
35
36#include <sys/cdefs.h>
30 */
31
32/*
33 * CTL frontend for the iSCSI protocol.
34 */
35
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD: head/sys/cam/ctl/ctl_frontend_iscsi.c 277963 2015-01-31 07:49:50Z trasz $");
37__FBSDID("$FreeBSD: head/sys/cam/ctl/ctl_frontend_iscsi.c 278037 2015-02-01 21:50:28Z mav $");
38
39#include <sys/param.h>
40#include <sys/capsicum.h>
41#include <sys/condvar.h>
42#include <sys/file.h>
43#include <sys/kernel.h>
44#include <sys/kthread.h>
45#include <sys/lock.h>

--- 100 unchanged lines hidden (view full) ---

146int cfiscsi_init(void);
147static void cfiscsi_online(void *arg);
148static void cfiscsi_offline(void *arg);
149static int cfiscsi_info(void *arg, struct sbuf *sb);
150static int cfiscsi_lun_enable(void *arg,
151 struct ctl_id target_id, int lun_id);
152static int cfiscsi_lun_disable(void *arg,
153 struct ctl_id target_id, int lun_id);
38
39#include <sys/param.h>
40#include <sys/capsicum.h>
41#include <sys/condvar.h>
42#include <sys/file.h>
43#include <sys/kernel.h>
44#include <sys/kthread.h>
45#include <sys/lock.h>

--- 100 unchanged lines hidden (view full) ---

146int cfiscsi_init(void);
147static void cfiscsi_online(void *arg);
148static void cfiscsi_offline(void *arg);
149static int cfiscsi_info(void *arg, struct sbuf *sb);
150static int cfiscsi_lun_enable(void *arg,
151 struct ctl_id target_id, int lun_id);
152static int cfiscsi_lun_disable(void *arg,
153 struct ctl_id target_id, int lun_id);
154static uint32_t cfiscsi_lun_map(void *arg, uint32_t lun);
155static int cfiscsi_ioctl(struct cdev *dev,
156 u_long cmd, caddr_t addr, int flag, struct thread *td);
157static void cfiscsi_datamove(union ctl_io *io);
158static void cfiscsi_datamove_in(union ctl_io *io);
159static void cfiscsi_datamove_out(union ctl_io *io);
160static void cfiscsi_done(union ctl_io *io);
161static bool cfiscsi_pdu_update_cmdsn(const struct icl_pdu *request);
162static void cfiscsi_pdu_handle_nop_out(struct icl_pdu *request);

--- 1863 unchanged lines hidden (view full) ---

2026 port->physical_port = strtoul(tag, NULL, 0);
2027 port->virtual_port = ct->ct_target_id;
2028 port->port_online = cfiscsi_online;
2029 port->port_offline = cfiscsi_offline;
2030 port->port_info = cfiscsi_info;
2031 port->onoff_arg = ct;
2032 port->lun_enable = cfiscsi_lun_enable;
2033 port->lun_disable = cfiscsi_lun_disable;
154static int cfiscsi_ioctl(struct cdev *dev,
155 u_long cmd, caddr_t addr, int flag, struct thread *td);
156static void cfiscsi_datamove(union ctl_io *io);
157static void cfiscsi_datamove_in(union ctl_io *io);
158static void cfiscsi_datamove_out(union ctl_io *io);
159static void cfiscsi_done(union ctl_io *io);
160static bool cfiscsi_pdu_update_cmdsn(const struct icl_pdu *request);
161static void cfiscsi_pdu_handle_nop_out(struct icl_pdu *request);

--- 1863 unchanged lines hidden (view full) ---

2025 port->physical_port = strtoul(tag, NULL, 0);
2026 port->virtual_port = ct->ct_target_id;
2027 port->port_online = cfiscsi_online;
2028 port->port_offline = cfiscsi_offline;
2029 port->port_info = cfiscsi_info;
2030 port->onoff_arg = ct;
2031 port->lun_enable = cfiscsi_lun_enable;
2032 port->lun_disable = cfiscsi_lun_disable;
2034 port->lun_map = cfiscsi_lun_map;
2035 port->targ_lun_arg = ct;
2036 port->fe_datamove = cfiscsi_datamove;
2037 port->fe_done = cfiscsi_done;
2038
2039 /* XXX KDM what should we report here? */
2040 /* XXX These should probably be fetched from CTL. */
2041 port->max_targets = 1;
2042 port->max_target_id = 15;

--- 33 unchanged lines hidden (view full) ---

2076 retval = ctl_port_register(port);
2077 if (retval != 0) {
2078 ctl_free_opts(&port->options);
2079 cfiscsi_target_release(ct);
2080 free(port->port_devid, M_CFISCSI);
2081 free(port->target_devid, M_CFISCSI);
2082 req->status = CTL_LUN_ERROR;
2083 snprintf(req->error_str, sizeof(req->error_str),
2033 port->targ_lun_arg = ct;
2034 port->fe_datamove = cfiscsi_datamove;
2035 port->fe_done = cfiscsi_done;
2036
2037 /* XXX KDM what should we report here? */
2038 /* XXX These should probably be fetched from CTL. */
2039 port->max_targets = 1;
2040 port->max_target_id = 15;

--- 33 unchanged lines hidden (view full) ---

2074 retval = ctl_port_register(port);
2075 if (retval != 0) {
2076 ctl_free_opts(&port->options);
2077 cfiscsi_target_release(ct);
2078 free(port->port_devid, M_CFISCSI);
2079 free(port->target_devid, M_CFISCSI);
2080 req->status = CTL_LUN_ERROR;
2081 snprintf(req->error_str, sizeof(req->error_str),
2084 "ctl_frontend_register() failed with error %d", retval);
2082 "ctl_port_register() failed with error %d", retval);
2085 return;
2086 }
2087done:
2088 ct->ct_state = CFISCSI_TARGET_STATE_ACTIVE;
2089 req->status = CTL_LUN_OK;
2090 memcpy(req->kern_args[0].kvalue, &port->targ_port,
2091 sizeof(port->targ_port)); //XXX
2092}

--- 161 unchanged lines hidden (view full) ---

2254 return (NULL);
2255}
2256
2257static struct cfiscsi_target *
2258cfiscsi_target_find_or_create(struct cfiscsi_softc *softc, const char *name,
2259 const char *alias)
2260{
2261 struct cfiscsi_target *ct, *newct;
2083 return;
2084 }
2085done:
2086 ct->ct_state = CFISCSI_TARGET_STATE_ACTIVE;
2087 req->status = CTL_LUN_OK;
2088 memcpy(req->kern_args[0].kvalue, &port->targ_port,
2089 sizeof(port->targ_port)); //XXX
2090}

--- 161 unchanged lines hidden (view full) ---

2252 return (NULL);
2253}
2254
2255static struct cfiscsi_target *
2256cfiscsi_target_find_or_create(struct cfiscsi_softc *softc, const char *name,
2257 const char *alias)
2258{
2259 struct cfiscsi_target *ct, *newct;
2262 int i;
2263
2264 if (name[0] == '\0' || strlen(name) >= CTL_ISCSI_NAME_LEN)
2265 return (NULL);
2266
2267 newct = malloc(sizeof(*newct), M_CFISCSI, M_WAITOK | M_ZERO);
2268
2269 mtx_lock(&softc->lock);
2270 TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2271 if (strcmp(name, ct->ct_name) != 0 ||
2272 ct->ct_state == CFISCSI_TARGET_STATE_INVALID)
2273 continue;
2274 cfiscsi_target_hold(ct);
2275 mtx_unlock(&softc->lock);
2276 free(newct, M_CFISCSI);
2277 return (ct);
2278 }
2279
2260
2261 if (name[0] == '\0' || strlen(name) >= CTL_ISCSI_NAME_LEN)
2262 return (NULL);
2263
2264 newct = malloc(sizeof(*newct), M_CFISCSI, M_WAITOK | M_ZERO);
2265
2266 mtx_lock(&softc->lock);
2267 TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2268 if (strcmp(name, ct->ct_name) != 0 ||
2269 ct->ct_state == CFISCSI_TARGET_STATE_INVALID)
2270 continue;
2271 cfiscsi_target_hold(ct);
2272 mtx_unlock(&softc->lock);
2273 free(newct, M_CFISCSI);
2274 return (ct);
2275 }
2276
2280 for (i = 0; i < CTL_MAX_LUNS; i++)
2281 newct->ct_luns[i] = UINT32_MAX;
2282
2283 strlcpy(newct->ct_name, name, sizeof(newct->ct_name));
2284 if (alias != NULL)
2285 strlcpy(newct->ct_alias, alias, sizeof(newct->ct_alias));
2286 refcount_init(&newct->ct_refcount, 1);
2287 newct->ct_softc = softc;
2288 if (TAILQ_EMPTY(&softc->targets))
2289 softc->last_target_id = 0;
2290 newct->ct_target_id = ++softc->last_target_id;
2291 TAILQ_INSERT_TAIL(&softc->targets, newct, ct_next);
2292 mtx_unlock(&softc->lock);
2293
2294 return (newct);
2295}
2296
2277 strlcpy(newct->ct_name, name, sizeof(newct->ct_name));
2278 if (alias != NULL)
2279 strlcpy(newct->ct_alias, alias, sizeof(newct->ct_alias));
2280 refcount_init(&newct->ct_refcount, 1);
2281 newct->ct_softc = softc;
2282 if (TAILQ_EMPTY(&softc->targets))
2283 softc->last_target_id = 0;
2284 newct->ct_target_id = ++softc->last_target_id;
2285 TAILQ_INSERT_TAIL(&softc->targets, newct, ct_next);
2286 mtx_unlock(&softc->lock);
2287
2288 return (newct);
2289}
2290
2297/*
2298 * Takes LUN from the target space and returns LUN from the CTL space.
2299 */
2300static uint32_t
2301cfiscsi_lun_map(void *arg, uint32_t lun)
2302{
2303 struct cfiscsi_target *ct = arg;
2304
2305 if (lun >= CTL_MAX_LUNS) {
2306 CFISCSI_DEBUG("requested lun number %d is higher "
2307 "than maximum %d", lun, CTL_MAX_LUNS - 1);
2308 return (UINT32_MAX);
2309 }
2310 return (ct->ct_luns[lun]);
2311}
2312
2313static int
2291static int
2314cfiscsi_target_set_lun(struct cfiscsi_target *ct,
2315 unsigned long lun_id, unsigned long ctl_lun_id)
2316{
2317
2318 if (lun_id >= CTL_MAX_LUNS) {
2319 CFISCSI_WARN("requested lun number %ld is higher "
2320 "than maximum %d", lun_id, CTL_MAX_LUNS - 1);
2321 return (-1);
2322 }
2323
2324 if (ct->ct_luns[lun_id] < CTL_MAX_LUNS) {
2325 /*
2326 * CTL calls cfiscsi_lun_enable() twice for each LUN - once
2327 * when the LUN is created, and a second time just before
2328 * the port is brought online; don't emit warnings
2329 * for that case.
2330 */
2331 if (ct->ct_luns[lun_id] == ctl_lun_id)
2332 return (0);
2333 CFISCSI_WARN("lun %ld already allocated", lun_id);
2334 return (-1);
2335 }
2336
2337#if 0
2338 CFISCSI_DEBUG("adding mapping for lun %ld, target %s "
2339 "to ctl lun %ld", lun_id, ct->ct_name, ctl_lun_id);
2340#endif
2341
2342 ct->ct_luns[lun_id] = ctl_lun_id;
2343
2344 return (0);
2345}
2346
2347static int
2348cfiscsi_lun_enable(void *arg, struct ctl_id target_id, int lun_id)
2349{
2292cfiscsi_lun_enable(void *arg, struct ctl_id target_id, int lun_id)
2293{
2350 struct cfiscsi_softc *softc;
2351 struct cfiscsi_target *ct;
2352 const char *target = NULL;
2353 const char *lun = NULL;
2354 unsigned long tmp;
2355
2294
2356 ct = (struct cfiscsi_target *)arg;
2357 softc = ct->ct_softc;
2358
2359 target = ctl_get_opt(&control_softc->ctl_luns[lun_id]->be_lun->options,
2360 "cfiscsi_target");
2361 lun = ctl_get_opt(&control_softc->ctl_luns[lun_id]->be_lun->options,
2362 "cfiscsi_lun");
2363
2364 if (target == NULL && lun == NULL)
2365 return (0);
2366
2367 if (target == NULL || lun == NULL) {
2368 CFISCSI_WARN("lun added with cfiscsi_target, but without "
2369 "cfiscsi_lun, or the other way around; ignoring");
2370 return (0);
2371 }
2372
2373 if (strcmp(target, ct->ct_name) != 0)
2374 return (0);
2375
2376 tmp = strtoul(lun, NULL, 10);
2377 cfiscsi_target_set_lun(ct, tmp, lun_id);
2378 return (0);
2379}
2380
2381static int
2382cfiscsi_lun_disable(void *arg, struct ctl_id target_id, int lun_id)
2383{
2295 return (0);
2296}
2297
2298static int
2299cfiscsi_lun_disable(void *arg, struct ctl_id target_id, int lun_id)
2300{
2384 struct cfiscsi_softc *softc;
2385 struct cfiscsi_target *ct;
2386 int i;
2387
2301
2388 ct = (struct cfiscsi_target *)arg;
2389 softc = ct->ct_softc;
2390
2391 mtx_lock(&softc->lock);
2392 for (i = 0; i < CTL_MAX_LUNS; i++) {
2393 if (ct->ct_luns[i] != lun_id)
2394 continue;
2395 ct->ct_luns[i] = UINT32_MAX;
2396 break;
2397 }
2398 mtx_unlock(&softc->lock);
2399 return (0);
2400}
2401
2402static void
2403cfiscsi_datamove_in(union ctl_io *io)
2404{
2405 struct cfiscsi_session *cs;
2406 struct icl_pdu *request, *response;

--- 575 unchanged lines hidden ---
2302 return (0);
2303}
2304
2305static void
2306cfiscsi_datamove_in(union ctl_io *io)
2307{
2308 struct cfiscsi_session *cs;
2309 struct icl_pdu *request, *response;

--- 575 unchanged lines hidden ---