ctl_frontend_iscsi.c revision 263740
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 * $FreeBSD: head/sys/cam/ctl/ctl_frontend_iscsi.c 263740 2014-03-25 18:30:57Z trasz $
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 263740 2014-03-25 18:30:57Z trasz $");
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>
46#include <sys/malloc.h>
47#include <sys/module.h>
48#include <sys/mutex.h>
49#include <sys/queue.h>
50#include <sys/sbuf.h>
51#include <sys/sysctl.h>
52#include <sys/systm.h>
53#include <sys/uio.h>
54#include <sys/unistd.h>
55#include <vm/uma.h>
56
57#include <cam/scsi/scsi_all.h>
58#include <cam/scsi/scsi_da.h>
59#include <cam/ctl/ctl_io.h>
60#include <cam/ctl/ctl.h>
61#include <cam/ctl/ctl_backend.h>
62#include <cam/ctl/ctl_error.h>
63#include <cam/ctl/ctl_frontend.h>
64#include <cam/ctl/ctl_frontend_internal.h>
65#include <cam/ctl/ctl_debug.h>
66#include <cam/ctl/ctl_ha.h>
67#include <cam/ctl/ctl_ioctl.h>
68#include <cam/ctl/ctl_private.h>
69
70#include "../../dev/iscsi/icl.h"
71#include "../../dev/iscsi/iscsi_proto.h"
72#include "ctl_frontend_iscsi.h"
73
74#ifdef ICL_KERNEL_PROXY
75#include <sys/socketvar.h>
76#endif
77
78static MALLOC_DEFINE(M_CFISCSI, "cfiscsi", "Memory used for CTL iSCSI frontend");
79static uma_zone_t cfiscsi_data_wait_zone;
80
81SYSCTL_NODE(_kern_cam_ctl, OID_AUTO, iscsi, CTLFLAG_RD, 0,
82    "CAM Target Layer iSCSI Frontend");
83static int debug = 3;
84TUNABLE_INT("kern.cam.ctl.iscsi.debug", &debug);
85SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, debug, CTLFLAG_RW,
86    &debug, 1, "Enable debug messages");
87static int ping_timeout = 5;
88TUNABLE_INT("kern.cam.ctl.iscsi.ping_timeout", &ping_timeout);
89SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, ping_timeout, CTLFLAG_RW,
90    &ping_timeout, 5, "Interval between ping (NOP-Out) requests, in seconds");
91static int login_timeout = 60;
92TUNABLE_INT("kern.cam.ctl.iscsi.login_timeout", &login_timeout);
93SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, login_timeout, CTLFLAG_RW,
94    &login_timeout, 60, "Time to wait for ctld(8) to finish Login Phase, in seconds");
95static int maxcmdsn_delta = 256;
96TUNABLE_INT("kern.cam.ctl.iscsi.maxcmdsn_delta", &maxcmdsn_delta);
97SYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, maxcmdsn_delta, CTLFLAG_RW,
98    &maxcmdsn_delta, 256, "Number of commands the initiator can send "
99    "without confirmation");
100
101#define	CFISCSI_DEBUG(X, ...)						\
102	do {								\
103		if (debug > 1) {					\
104			printf("%s: " X "\n",				\
105			    __func__, ## __VA_ARGS__);			\
106		}							\
107	} while (0)
108
109#define	CFISCSI_WARN(X, ...)						\
110	do {								\
111		if (debug > 0) {					\
112			printf("WARNING: %s: " X "\n",			\
113			    __func__, ## __VA_ARGS__);			\
114		}							\
115	} while (0)
116
117#define	CFISCSI_SESSION_DEBUG(S, X, ...)				\
118	do {								\
119		if (debug > 1) {					\
120			printf("%s: %s (%s): " X "\n",			\
121			    __func__, S->cs_initiator_addr,		\
122			    S->cs_initiator_name, ## __VA_ARGS__);	\
123		}							\
124	} while (0)
125
126#define	CFISCSI_SESSION_WARN(S, X, ...)					\
127	do  {								\
128		if (debug > 0) {					\
129			printf("WARNING: %s (%s): " X "\n",		\
130			    S->cs_initiator_addr,			\
131			    S->cs_initiator_name, ## __VA_ARGS__);	\
132		}							\
133	} while (0)
134
135#define CFISCSI_SESSION_LOCK(X)		mtx_lock(&X->cs_lock)
136#define CFISCSI_SESSION_UNLOCK(X)	mtx_unlock(&X->cs_lock)
137#define CFISCSI_SESSION_LOCK_ASSERT(X)	mtx_assert(&X->cs_lock, MA_OWNED)
138
139#define	CONN_SESSION(X)			((struct cfiscsi_session *)(X)->ic_prv0)
140#define	PDU_SESSION(X)			CONN_SESSION((X)->ip_conn)
141#define	PDU_EXPDATASN(X)		(X)->ip_prv0
142#define	PDU_TOTAL_TRANSFER_LEN(X)	(X)->ip_prv1
143#define	PDU_R2TSN(X)			(X)->ip_prv2
144
145int		cfiscsi_init(void);
146static void	cfiscsi_online(void *arg);
147static void	cfiscsi_offline(void *arg);
148static int	cfiscsi_targ_enable(void *arg, struct ctl_id targ_id);
149static int	cfiscsi_targ_disable(void *arg, struct ctl_id targ_id);
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 int	cfiscsi_ioctl(struct cdev *dev,
155		    u_long cmd, caddr_t addr, int flag, struct thread *td);
156static int	cfiscsi_devid(struct ctl_scsiio *ctsio, int alloc_len);
157static void	cfiscsi_datamove(union ctl_io *io);
158static void	cfiscsi_done(union ctl_io *io);
159static uint32_t	cfiscsi_map_lun(void *arg, uint32_t lun);
160static bool	cfiscsi_pdu_update_cmdsn(const struct icl_pdu *request);
161static void	cfiscsi_pdu_handle_nop_out(struct icl_pdu *request);
162static void	cfiscsi_pdu_handle_scsi_command(struct icl_pdu *request);
163static void	cfiscsi_pdu_handle_task_request(struct icl_pdu *request);
164static void	cfiscsi_pdu_handle_data_out(struct icl_pdu *request);
165static void	cfiscsi_pdu_handle_logout_request(struct icl_pdu *request);
166static void	cfiscsi_session_terminate(struct cfiscsi_session *cs);
167static struct cfiscsi_target	*cfiscsi_target_find(struct cfiscsi_softc
168		    *softc, const char *name);
169static void	cfiscsi_target_release(struct cfiscsi_target *ct);
170static void	cfiscsi_session_delete(struct cfiscsi_session *cs);
171
172static struct cfiscsi_softc cfiscsi_softc;
173extern struct ctl_softc *control_softc;
174
175static int cfiscsi_module_event_handler(module_t, int /*modeventtype_t*/, void *);
176
177static moduledata_t cfiscsi_moduledata = {
178	"ctlcfiscsi",
179	cfiscsi_module_event_handler,
180	NULL
181};
182
183DECLARE_MODULE(ctlcfiscsi, cfiscsi_moduledata, SI_SUB_CONFIGURE, SI_ORDER_FOURTH);
184MODULE_VERSION(ctlcfiscsi, 1);
185MODULE_DEPEND(ctlcfiscsi, ctl, 1, 1, 1);
186MODULE_DEPEND(ctlcfiscsi, icl, 1, 1, 1);
187
188static struct icl_pdu *
189cfiscsi_pdu_new_response(struct icl_pdu *request, int flags)
190{
191
192	return (icl_pdu_new_bhs(request->ip_conn, flags));
193}
194
195static bool
196cfiscsi_pdu_update_cmdsn(const struct icl_pdu *request)
197{
198	const struct iscsi_bhs_scsi_command *bhssc;
199	struct cfiscsi_session *cs;
200	uint32_t cmdsn, expstatsn;
201
202	cs = PDU_SESSION(request);
203
204	/*
205	 * Every incoming PDU - not just NOP-Out - resets the ping timer.
206	 * The purpose of the timeout is to reset the connection when it stalls;
207	 * we don't want this to happen when NOP-In or NOP-Out ends up delayed
208	 * in some queue.
209	 *
210	 * XXX: Locking?
211	 */
212	cs->cs_timeout = 0;
213
214	/*
215	 * Data-Out PDUs don't contain CmdSN.
216	 */
217	if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
218	    ISCSI_BHS_OPCODE_SCSI_DATA_OUT)
219		return (false);
220
221	/*
222	 * We're only using fields common for all the request
223	 * (initiator -> target) PDUs.
224	 */
225	bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
226	cmdsn = ntohl(bhssc->bhssc_cmdsn);
227	expstatsn = ntohl(bhssc->bhssc_expstatsn);
228
229	CFISCSI_SESSION_LOCK(cs);
230#if 0
231	if (expstatsn != cs->cs_statsn) {
232		CFISCSI_SESSION_DEBUG(cs, "received PDU with ExpStatSN %d, "
233		    "while current StatSN is %d", expstatsn,
234		    cs->cs_statsn);
235	}
236#endif
237
238	/*
239	 * The target MUST silently ignore any non-immediate command outside
240	 * of this range.
241	 */
242	if (cmdsn < cs->cs_cmdsn || cmdsn > cs->cs_cmdsn + maxcmdsn_delta) {
243		CFISCSI_SESSION_UNLOCK(cs);
244		CFISCSI_SESSION_WARN(cs, "received PDU with CmdSN %d, "
245		    "while expected CmdSN was %d", cmdsn, cs->cs_cmdsn);
246		return (true);
247	}
248
249	if ((request->ip_bhs->bhs_opcode & ISCSI_BHS_OPCODE_IMMEDIATE) == 0)
250		cs->cs_cmdsn++;
251
252	CFISCSI_SESSION_UNLOCK(cs);
253
254	return (false);
255}
256
257static void
258cfiscsi_pdu_handle(struct icl_pdu *request)
259{
260	struct cfiscsi_session *cs;
261	bool ignore;
262
263	cs = PDU_SESSION(request);
264
265	ignore = cfiscsi_pdu_update_cmdsn(request);
266	if (ignore) {
267		icl_pdu_free(request);
268		return;
269	}
270
271	/*
272	 * Handle the PDU; this includes e.g. receiving the remaining
273	 * part of PDU and submitting the SCSI command to CTL
274	 * or queueing a reply.  The handling routine is responsible
275	 * for freeing the PDU when it's no longer needed.
276	 */
277	switch (request->ip_bhs->bhs_opcode &
278	    ~ISCSI_BHS_OPCODE_IMMEDIATE) {
279	case ISCSI_BHS_OPCODE_NOP_OUT:
280		cfiscsi_pdu_handle_nop_out(request);
281		break;
282	case ISCSI_BHS_OPCODE_SCSI_COMMAND:
283		cfiscsi_pdu_handle_scsi_command(request);
284		break;
285	case ISCSI_BHS_OPCODE_TASK_REQUEST:
286		cfiscsi_pdu_handle_task_request(request);
287		break;
288	case ISCSI_BHS_OPCODE_SCSI_DATA_OUT:
289		cfiscsi_pdu_handle_data_out(request);
290		break;
291	case ISCSI_BHS_OPCODE_LOGOUT_REQUEST:
292		cfiscsi_pdu_handle_logout_request(request);
293		break;
294	default:
295		CFISCSI_SESSION_WARN(cs, "received PDU with unsupported "
296		    "opcode 0x%x; dropping connection",
297		    request->ip_bhs->bhs_opcode);
298		icl_pdu_free(request);
299		cfiscsi_session_terminate(cs);
300	}
301
302}
303
304static void
305cfiscsi_receive_callback(struct icl_pdu *request)
306{
307	struct cfiscsi_session *cs;
308
309	cs = PDU_SESSION(request);
310
311#ifdef ICL_KERNEL_PROXY
312	if (cs->cs_waiting_for_ctld || cs->cs_login_phase) {
313		if (cs->cs_login_pdu == NULL)
314			cs->cs_login_pdu = request;
315		else
316			icl_pdu_free(request);
317		cv_signal(&cs->cs_login_cv);
318		return;
319	}
320#endif
321
322	cfiscsi_pdu_handle(request);
323}
324
325static void
326cfiscsi_error_callback(struct icl_conn *ic)
327{
328	struct cfiscsi_session *cs;
329
330	cs = CONN_SESSION(ic);
331
332	CFISCSI_SESSION_WARN(cs, "connection error; dropping connection");
333	cfiscsi_session_terminate(cs);
334}
335
336static int
337cfiscsi_pdu_prepare(struct icl_pdu *response)
338{
339	struct cfiscsi_session *cs;
340	struct iscsi_bhs_scsi_response *bhssr;
341	bool advance_statsn = true;
342
343	cs = PDU_SESSION(response);
344
345	CFISCSI_SESSION_LOCK_ASSERT(cs);
346
347	/*
348	 * We're only using fields common for all the response
349	 * (target -> initiator) PDUs.
350	 */
351	bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
352
353	/*
354	 * 10.8.3: "The StatSN for this connection is not advanced
355	 * after this PDU is sent."
356	 */
357	if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_R2T)
358		advance_statsn = false;
359
360	/*
361	 * 10.19.2: "However, when the Initiator Task Tag is set to 0xffffffff,
362	 * StatSN for the connection is not advanced after this PDU is sent."
363	 */
364	if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_NOP_IN &&
365	    bhssr->bhssr_initiator_task_tag == 0xffffffff)
366		advance_statsn = false;
367
368	/*
369	 * See the comment below - StatSN is not meaningful and must
370	 * not be advanced.
371	 */
372	if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_SCSI_DATA_IN)
373		advance_statsn = false;
374
375	/*
376	 * 10.7.3: "The fields StatSN, Status, and Residual Count
377	 * only have meaningful content if the S bit is set to 1."
378	 */
379	if (bhssr->bhssr_opcode != ISCSI_BHS_OPCODE_SCSI_DATA_IN)
380		bhssr->bhssr_statsn = htonl(cs->cs_statsn);
381	bhssr->bhssr_expcmdsn = htonl(cs->cs_cmdsn);
382	bhssr->bhssr_maxcmdsn = htonl(cs->cs_cmdsn + maxcmdsn_delta);
383
384	if (advance_statsn)
385		cs->cs_statsn++;
386
387	return (0);
388}
389
390static void
391cfiscsi_pdu_queue(struct icl_pdu *response)
392{
393	struct cfiscsi_session *cs;
394
395	cs = PDU_SESSION(response);
396
397	CFISCSI_SESSION_LOCK(cs);
398	cfiscsi_pdu_prepare(response);
399	icl_pdu_queue(response);
400	CFISCSI_SESSION_UNLOCK(cs);
401}
402
403static uint32_t
404cfiscsi_decode_lun(uint64_t encoded)
405{
406	uint8_t lun[8];
407	uint32_t result;
408
409	/*
410	 * The LUN field in iSCSI PDUs may look like an ordinary 64 bit number,
411	 * but is in fact an evil, multidimensional structure defined
412	 * in SCSI Architecture Model 5 (SAM-5), section 4.6.
413	 */
414	memcpy(lun, &encoded, sizeof(lun));
415	switch (lun[0] & 0xC0) {
416	case 0x00:
417		if ((lun[0] & 0x3f) != 0 || lun[2] != 0 || lun[3] != 0 ||
418		    lun[4] != 0 || lun[5] != 0 || lun[6] != 0 || lun[7] != 0) {
419			CFISCSI_WARN("malformed LUN "
420			    "(peripheral device addressing method): 0x%jx",
421			    (uintmax_t)encoded);
422			result = 0xffffffff;
423			break;
424		}
425		result = lun[1];
426		break;
427	case 0x40:
428		if (lun[2] != 0 || lun[3] != 0 || lun[4] != 0 || lun[5] != 0 ||
429		    lun[6] != 0 || lun[7] != 0) {
430			CFISCSI_WARN("malformed LUN "
431			    "(flat address space addressing method): 0x%jx",
432			    (uintmax_t)encoded);
433			result = 0xffffffff;
434			break;
435		}
436		result = ((lun[0] & 0x3f) << 8) + lun[1];
437		break;
438	case 0xC0:
439		if (lun[0] != 0xD2 || lun[4] != 0 || lun[5] != 0 ||
440		    lun[6] != 0 || lun[7] != 0) {
441			CFISCSI_WARN("malformed LUN (extended flat "
442			    "address space addressing method): 0x%jx",
443			    (uintmax_t)encoded);
444			result = 0xffffffff;
445			break;
446		}
447		result = (lun[1] << 16) + (lun[2] << 8) + lun[3];
448	default:
449		CFISCSI_WARN("unsupported LUN format 0x%jx",
450		    (uintmax_t)encoded);
451		result = 0xffffffff;
452		break;
453	}
454
455	return (result);
456}
457
458static void
459cfiscsi_pdu_handle_nop_out(struct icl_pdu *request)
460{
461	struct cfiscsi_session *cs;
462	struct iscsi_bhs_nop_out *bhsno;
463	struct iscsi_bhs_nop_in *bhsni;
464	struct icl_pdu *response;
465	void *data = NULL;
466	size_t datasize;
467	int error;
468
469	cs = PDU_SESSION(request);
470	bhsno = (struct iscsi_bhs_nop_out *)request->ip_bhs;
471
472	if (bhsno->bhsno_initiator_task_tag == 0xffffffff) {
473		/*
474		 * Nothing to do, iscsi_pdu_update_statsn() already
475		 * zeroed the timeout.
476		 */
477		icl_pdu_free(request);
478		return;
479	}
480
481	datasize = icl_pdu_data_segment_length(request);
482	if (datasize > 0) {
483		data = malloc(datasize, M_CFISCSI, M_NOWAIT | M_ZERO);
484		if (data == NULL) {
485			CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
486			    "dropping connection");
487			icl_pdu_free(request);
488			cfiscsi_session_terminate(cs);
489			return;
490		}
491		icl_pdu_get_data(request, 0, data, datasize);
492	}
493
494	response = cfiscsi_pdu_new_response(request, M_NOWAIT);
495	if (response == NULL) {
496		CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
497		    "droppping connection");
498		free(data, M_CFISCSI);
499		icl_pdu_free(request);
500		cfiscsi_session_terminate(cs);
501		return;
502	}
503	bhsni = (struct iscsi_bhs_nop_in *)response->ip_bhs;
504	bhsni->bhsni_opcode = ISCSI_BHS_OPCODE_NOP_IN;
505	bhsni->bhsni_flags = 0x80;
506	bhsni->bhsni_initiator_task_tag = bhsno->bhsno_initiator_task_tag;
507	bhsni->bhsni_target_transfer_tag = 0xffffffff;
508	if (datasize > 0) {
509		error = icl_pdu_append_data(response, data, datasize, M_NOWAIT);
510		if (error != 0) {
511			CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
512			    "dropping connection");
513			free(data, M_CFISCSI);
514			icl_pdu_free(request);
515			icl_pdu_free(response);
516			cfiscsi_session_terminate(cs);
517			return;
518		}
519		free(data, M_CFISCSI);
520	}
521
522	icl_pdu_free(request);
523	cfiscsi_pdu_queue(response);
524}
525
526static void
527cfiscsi_pdu_handle_scsi_command(struct icl_pdu *request)
528{
529	struct iscsi_bhs_scsi_command *bhssc;
530	struct cfiscsi_session *cs;
531	union ctl_io *io;
532	int error;
533
534	cs = PDU_SESSION(request);
535	bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
536	//CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x",
537	//    bhssc->bhssc_initiator_task_tag);
538
539	if (request->ip_data_len > 0 && cs->cs_immediate_data == false) {
540		CFISCSI_SESSION_WARN(cs, "unsolicited data with "
541		    "ImmediateData=No; dropping connection");
542		icl_pdu_free(request);
543		cfiscsi_session_terminate(cs);
544		return;
545	}
546	io = ctl_alloc_io(cs->cs_target->ct_softc->fe.ctl_pool_ref);
547	if (io == NULL) {
548		CFISCSI_SESSION_WARN(cs, "can't allocate ctl_io; "
549		    "dropping connection");
550		icl_pdu_free(request);
551		cfiscsi_session_terminate(cs);
552		return;
553	}
554	ctl_zero_io(io);
555	io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = request;
556	io->io_hdr.io_type = CTL_IO_SCSI;
557	io->io_hdr.nexus.initid.id = cs->cs_ctl_initid;
558	io->io_hdr.nexus.targ_port = cs->cs_target->ct_softc->fe.targ_port;
559	io->io_hdr.nexus.targ_target.id = 0;
560	io->io_hdr.nexus.targ_lun = cfiscsi_decode_lun(bhssc->bhssc_lun);
561	io->io_hdr.nexus.lun_map_fn = cfiscsi_map_lun;
562	io->io_hdr.nexus.lun_map_arg = cs;
563	io->scsiio.tag_num = bhssc->bhssc_initiator_task_tag;
564	switch ((bhssc->bhssc_flags & BHSSC_FLAGS_ATTR)) {
565	case BHSSC_FLAGS_ATTR_UNTAGGED:
566		io->scsiio.tag_type = CTL_TAG_UNTAGGED;
567		break;
568	case BHSSC_FLAGS_ATTR_SIMPLE:
569		io->scsiio.tag_type = CTL_TAG_SIMPLE;
570		break;
571	case BHSSC_FLAGS_ATTR_ORDERED:
572        	io->scsiio.tag_type = CTL_TAG_ORDERED;
573		break;
574	case BHSSC_FLAGS_ATTR_HOQ:
575        	io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
576		break;
577	case BHSSC_FLAGS_ATTR_ACA:
578		io->scsiio.tag_type = CTL_TAG_ACA;
579		break;
580	default:
581		io->scsiio.tag_type = CTL_TAG_UNTAGGED;
582		CFISCSI_SESSION_WARN(cs, "unhandled tag type %d",
583		    bhssc->bhssc_flags & BHSSC_FLAGS_ATTR);
584		break;
585	}
586	io->scsiio.cdb_len = sizeof(bhssc->bhssc_cdb); /* Which is 16. */
587	memcpy(io->scsiio.cdb, bhssc->bhssc_cdb, sizeof(bhssc->bhssc_cdb));
588	refcount_acquire(&cs->cs_outstanding_ctl_pdus);
589	error = ctl_queue(io);
590	if (error != CTL_RETVAL_COMPLETE) {
591		CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d; "
592		    "dropping connection", error);
593		ctl_free_io(io);
594		refcount_release(&cs->cs_outstanding_ctl_pdus);
595		icl_pdu_free(request);
596		cfiscsi_session_terminate(cs);
597	}
598}
599
600static void
601cfiscsi_pdu_handle_task_request(struct icl_pdu *request)
602{
603	struct iscsi_bhs_task_management_request *bhstmr;
604	struct iscsi_bhs_task_management_response *bhstmr2;
605	struct icl_pdu *response;
606	struct cfiscsi_session *cs;
607	union ctl_io *io;
608	int error;
609
610	cs = PDU_SESSION(request);
611	bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
612	io = ctl_alloc_io(cs->cs_target->ct_softc->fe.ctl_pool_ref);
613	if (io == NULL) {
614		CFISCSI_SESSION_WARN(cs, "can't allocate ctl_io;"
615		    "dropping connection");
616		icl_pdu_free(request);
617		cfiscsi_session_terminate(cs);
618		return;
619	}
620	ctl_zero_io(io);
621	io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = request;
622	io->io_hdr.io_type = CTL_IO_TASK;
623	io->io_hdr.nexus.initid.id = cs->cs_ctl_initid;
624	io->io_hdr.nexus.targ_port = cs->cs_target->ct_softc->fe.targ_port;
625	io->io_hdr.nexus.targ_target.id = 0;
626	io->io_hdr.nexus.targ_lun = cfiscsi_decode_lun(bhstmr->bhstmr_lun);
627	io->io_hdr.nexus.lun_map_fn = cfiscsi_map_lun;
628	io->io_hdr.nexus.lun_map_arg = cs;
629	io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
630
631	switch (bhstmr->bhstmr_function & ~0x80) {
632	case BHSTMR_FUNCTION_ABORT_TASK:
633#if 0
634		CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_ABORT_TASK");
635#endif
636		io->taskio.task_action = CTL_TASK_ABORT_TASK;
637		io->taskio.tag_num = bhstmr->bhstmr_referenced_task_tag;
638		break;
639	case BHSTMR_FUNCTION_LOGICAL_UNIT_RESET:
640#if 0
641		CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_LOGICAL_UNIT_RESET");
642#endif
643		io->taskio.task_action = CTL_TASK_LUN_RESET;
644		break;
645	case BHSTMR_FUNCTION_TARGET_WARM_RESET:
646#if 0
647		CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_TARGET_WARM_RESET");
648#endif
649		io->taskio.task_action = CTL_TASK_TARGET_RESET;
650		break;
651	default:
652		CFISCSI_SESSION_DEBUG(cs, "unsupported function 0x%x",
653		    bhstmr->bhstmr_function & ~0x80);
654		ctl_free_io(io);
655
656		response = cfiscsi_pdu_new_response(request, M_NOWAIT);
657		if (response == NULL) {
658			CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
659			    "dropping connection");
660			icl_pdu_free(request);
661			cfiscsi_session_terminate(cs);
662			return;
663		}
664		bhstmr2 = (struct iscsi_bhs_task_management_response *)
665		    response->ip_bhs;
666		bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE;
667		bhstmr2->bhstmr_flags = 0x80;
668		bhstmr2->bhstmr_response =
669		    BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED;
670		bhstmr2->bhstmr_initiator_task_tag =
671		    bhstmr->bhstmr_initiator_task_tag;
672		icl_pdu_free(request);
673		cfiscsi_pdu_queue(response);
674		return;
675	}
676
677	refcount_acquire(&cs->cs_outstanding_ctl_pdus);
678	error = ctl_queue(io);
679	if (error != CTL_RETVAL_COMPLETE) {
680		CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d; "
681		    "dropping connection", error);
682		ctl_free_io(io);
683		refcount_release(&cs->cs_outstanding_ctl_pdus);
684		icl_pdu_free(request);
685		cfiscsi_session_terminate(cs);
686	}
687}
688
689static bool
690cfiscsi_handle_data_segment(struct icl_pdu *request, struct cfiscsi_data_wait *cdw)
691{
692	struct iscsi_bhs_data_out *bhsdo;
693	struct cfiscsi_session *cs;
694	struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
695	size_t copy_len, len, off, buffer_offset;
696	int ctl_sg_count;
697	union ctl_io *io;
698
699	cs = PDU_SESSION(request);
700
701	KASSERT((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
702	    ISCSI_BHS_OPCODE_SCSI_DATA_OUT ||
703	    (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
704	    ISCSI_BHS_OPCODE_SCSI_COMMAND,
705	    ("bad opcode 0x%x", request->ip_bhs->bhs_opcode));
706
707	/*
708	 * We're only using fields common for Data Out and SCSI Command PDUs.
709	 */
710	bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs;
711
712	io = cdw->cdw_ctl_io;
713	KASSERT((io->io_hdr.flags & CTL_FLAG_DATA_MASK) != CTL_FLAG_DATA_IN,
714	    ("CTL_FLAG_DATA_IN"));
715
716#if 0
717	CFISCSI_SESSION_DEBUG(cs, "received %zd bytes out of %d",
718	    request->ip_data_len, io->scsiio.kern_total_len);
719#endif
720
721	if (io->scsiio.kern_sg_entries > 0) {
722		ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
723		ctl_sg_count = io->scsiio.kern_sg_entries;
724	} else {
725		ctl_sglist = &ctl_sg_entry;
726		ctl_sglist->addr = io->scsiio.kern_data_ptr;
727		ctl_sglist->len = io->scsiio.kern_data_len;
728		ctl_sg_count = 1;
729	}
730
731	if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
732	    ISCSI_BHS_OPCODE_SCSI_DATA_OUT)
733		buffer_offset = ntohl(bhsdo->bhsdo_buffer_offset);
734	else
735		buffer_offset = 0;
736
737	/*
738	 * Make sure the offset, as sent by the initiator, matches the offset
739	 * we're supposed to be at in the scatter-gather list.
740	 */
741	if (buffer_offset != io->scsiio.ext_data_filled) {
742		CFISCSI_SESSION_WARN(cs, "received bad buffer offset %zd, "
743		    "expected %zd", buffer_offset,
744		    (size_t)io->scsiio.ext_data_filled);
745		cfiscsi_session_terminate(cs);
746		return (true);
747	}
748
749	/*
750	 * This is the offset within the PDU data segment, as opposed
751	 * to buffer_offset, which is the offset within the task (SCSI
752	 * command).
753	 */
754	off = 0;
755	len = icl_pdu_data_segment_length(request);
756
757	/*
758	 * Iterate over the scatter/gather segments, filling them with data
759	 * from the PDU data segment.  Note that this can get called multiple
760	 * times for one SCSI command; the cdw structure holds state for the
761	 * scatter/gather list.
762	 */
763	for (;;) {
764		KASSERT(cdw->cdw_sg_index < ctl_sg_count,
765		    ("cdw->cdw_sg_index >= ctl_sg_count"));
766		if (cdw->cdw_sg_len == 0) {
767			cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr;
768			cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len;
769		}
770		KASSERT(off <= len, ("len > off"));
771		copy_len = len - off;
772		if (copy_len > cdw->cdw_sg_len)
773			copy_len = cdw->cdw_sg_len;
774
775		icl_pdu_get_data(request, off, cdw->cdw_sg_addr, copy_len);
776		cdw->cdw_sg_addr += copy_len;
777		cdw->cdw_sg_len -= copy_len;
778		off += copy_len;
779		io->scsiio.ext_data_filled += copy_len;
780
781		if (cdw->cdw_sg_len == 0) {
782			/*
783			 * End of current segment.
784			 */
785			if (cdw->cdw_sg_index == ctl_sg_count - 1) {
786				/*
787				 * Last segment in scatter/gather list.
788				 */
789				break;
790			}
791			cdw->cdw_sg_index++;
792		}
793
794		if (off == len) {
795			/*
796			 * End of PDU payload.
797			 */
798			break;
799		}
800	}
801
802	if (len > off) {
803		CFISCSI_SESSION_WARN(cs, "received too much data: got %zd bytes, "
804		    "expected %zd", icl_pdu_data_segment_length(request), off);
805		cfiscsi_session_terminate(cs);
806		return (true);
807	}
808
809	if (bhsdo->bhsdo_flags & BHSDO_FLAGS_F ||
810	    io->scsiio.ext_data_filled == io->scsiio.kern_total_len) {
811		if ((bhsdo->bhsdo_flags & BHSDO_FLAGS_F) == 0) {
812			CFISCSI_SESSION_WARN(cs, "got the final packet without "
813			    "the F flag; flags = 0x%x; dropping connection",
814			    bhsdo->bhsdo_flags);
815			cfiscsi_session_terminate(cs);
816			return (true);
817		}
818
819		if (io->scsiio.ext_data_filled != io->scsiio.kern_total_len) {
820			if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
821			    ISCSI_BHS_OPCODE_SCSI_DATA_OUT) {
822				CFISCSI_SESSION_WARN(cs, "got the final packet, but the "
823				    "transmitted size was %zd bytes instead of %d; "
824				    "dropping connection",
825				    (size_t)io->scsiio.ext_data_filled,
826				    io->scsiio.kern_total_len);
827				cfiscsi_session_terminate(cs);
828				return (true);
829			} else {
830				/*
831				 * For SCSI Command PDU, this just means we need to
832				 * solicit more data by sending R2T.
833				 */
834				return (false);
835			}
836		}
837#if 0
838		CFISCSI_SESSION_DEBUG(cs, "no longer expecting Data-Out with target "
839		    "transfer tag 0x%x", cdw->cdw_target_transfer_tag);
840#endif
841
842		return (true);
843	}
844
845	return (false);
846}
847
848static void
849cfiscsi_pdu_handle_data_out(struct icl_pdu *request)
850{
851	struct iscsi_bhs_data_out *bhsdo;
852	struct cfiscsi_session *cs;
853	struct cfiscsi_data_wait *cdw = NULL;
854	union ctl_io *io;
855	bool done;
856
857	cs = PDU_SESSION(request);
858	bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs;
859
860	CFISCSI_SESSION_LOCK(cs);
861	TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next) {
862#if 0
863		CFISCSI_SESSION_DEBUG(cs, "have ttt 0x%x, itt 0x%x; looking for "
864		    "ttt 0x%x, itt 0x%x",
865		    bhsdo->bhsdo_target_transfer_tag,
866		    bhsdo->bhsdo_initiator_task_tag,
867		    cdw->cdw_target_transfer_tag, cdw->cdw_initiator_task_tag));
868#endif
869		if (bhsdo->bhsdo_target_transfer_tag ==
870		    cdw->cdw_target_transfer_tag)
871			break;
872	}
873	CFISCSI_SESSION_UNLOCK(cs);
874	if (cdw == NULL) {
875		CFISCSI_SESSION_WARN(cs, "data transfer tag 0x%x, initiator task tag "
876		    "0x%x, not found; dropping connection",
877		    bhsdo->bhsdo_target_transfer_tag, bhsdo->bhsdo_initiator_task_tag);
878		icl_pdu_free(request);
879		cfiscsi_session_terminate(cs);
880		return;
881	}
882
883	io = cdw->cdw_ctl_io;
884	KASSERT((io->io_hdr.flags & CTL_FLAG_DATA_MASK) != CTL_FLAG_DATA_IN,
885	    ("CTL_FLAG_DATA_IN"));
886
887	done = cfiscsi_handle_data_segment(request, cdw);
888	if (done) {
889		CFISCSI_SESSION_LOCK(cs);
890		TAILQ_REMOVE(&cs->cs_waiting_for_data_out, cdw, cdw_next);
891		CFISCSI_SESSION_UNLOCK(cs);
892		uma_zfree(cfiscsi_data_wait_zone, cdw);
893		io->scsiio.be_move_done(io);
894	}
895
896	icl_pdu_free(request);
897}
898
899static void
900cfiscsi_pdu_handle_logout_request(struct icl_pdu *request)
901{
902	struct iscsi_bhs_logout_request *bhslr;
903	struct iscsi_bhs_logout_response *bhslr2;
904	struct icl_pdu *response;
905	struct cfiscsi_session *cs;
906
907	cs = PDU_SESSION(request);
908	bhslr = (struct iscsi_bhs_logout_request *)request->ip_bhs;
909	switch (bhslr->bhslr_reason & 0x7f) {
910	case BHSLR_REASON_CLOSE_SESSION:
911	case BHSLR_REASON_CLOSE_CONNECTION:
912		response = cfiscsi_pdu_new_response(request, M_NOWAIT);
913		if (response == NULL) {
914			CFISCSI_SESSION_DEBUG(cs, "failed to allocate memory");
915			icl_pdu_free(request);
916			cfiscsi_session_terminate(cs);
917			return;
918		}
919		bhslr2 = (struct iscsi_bhs_logout_response *)response->ip_bhs;
920		bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
921		bhslr2->bhslr_flags = 0x80;
922		bhslr2->bhslr_response = BHSLR_RESPONSE_CLOSED_SUCCESSFULLY;
923		bhslr2->bhslr_initiator_task_tag =
924		    bhslr->bhslr_initiator_task_tag;
925		icl_pdu_free(request);
926		cfiscsi_pdu_queue(response);
927		cfiscsi_session_terminate(cs);
928		break;
929	case BHSLR_REASON_REMOVE_FOR_RECOVERY:
930		response = cfiscsi_pdu_new_response(request, M_NOWAIT);
931		if (response == NULL) {
932			CFISCSI_SESSION_WARN(cs,
933			    "failed to allocate memory; dropping connection");
934			icl_pdu_free(request);
935			cfiscsi_session_terminate(cs);
936			return;
937		}
938		bhslr2 = (struct iscsi_bhs_logout_response *)response->ip_bhs;
939		bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
940		bhslr2->bhslr_flags = 0x80;
941		bhslr2->bhslr_response = BHSLR_RESPONSE_RECOVERY_NOT_SUPPORTED;
942		bhslr2->bhslr_initiator_task_tag =
943		    bhslr->bhslr_initiator_task_tag;
944		icl_pdu_free(request);
945		cfiscsi_pdu_queue(response);
946		break;
947	default:
948		CFISCSI_SESSION_WARN(cs, "invalid reason 0%x; dropping connection",
949		    bhslr->bhslr_reason);
950		icl_pdu_free(request);
951		cfiscsi_session_terminate(cs);
952		break;
953	}
954}
955
956static void
957cfiscsi_callout(void *context)
958{
959	struct icl_pdu *cp;
960	struct iscsi_bhs_nop_in *bhsni;
961	struct cfiscsi_session *cs;
962
963	cs = context;
964
965	if (cs->cs_terminating)
966		return;
967
968	callout_schedule(&cs->cs_callout, 1 * hz);
969
970	CFISCSI_SESSION_LOCK(cs);
971	cs->cs_timeout++;
972	CFISCSI_SESSION_UNLOCK(cs);
973
974#ifdef ICL_KERNEL_PROXY
975	if (cs->cs_waiting_for_ctld || cs->cs_login_phase) {
976		if (cs->cs_timeout > login_timeout) {
977			CFISCSI_SESSION_WARN(cs, "login timed out after "
978			    "%d seconds; dropping connection", cs->cs_timeout);
979			cfiscsi_session_terminate(cs);
980		}
981		return;
982	}
983#endif
984
985	if (cs->cs_timeout >= ping_timeout) {
986		CFISCSI_SESSION_WARN(cs, "no ping reply (NOP-Out) after %d seconds; "
987		    "dropping connection",  ping_timeout);
988		cfiscsi_session_terminate(cs);
989		return;
990	}
991
992	/*
993	 * If the ping was reset less than one second ago - which means
994	 * that we've received some PDU during the last second - assume
995	 * the traffic flows correctly and don't bother sending a NOP-Out.
996	 *
997	 * (It's 2 - one for one second, and one for incrementing is_timeout
998	 * earlier in this routine.)
999	 */
1000	if (cs->cs_timeout < 2)
1001		return;
1002
1003	cp = icl_pdu_new_bhs(cs->cs_conn, M_NOWAIT);
1004	if (cp == NULL) {
1005		CFISCSI_SESSION_WARN(cs, "failed to allocate memory");
1006		return;
1007	}
1008	bhsni = (struct iscsi_bhs_nop_in *)cp->ip_bhs;
1009	bhsni->bhsni_opcode = ISCSI_BHS_OPCODE_NOP_IN;
1010	bhsni->bhsni_flags = 0x80;
1011	bhsni->bhsni_initiator_task_tag = 0xffffffff;
1012
1013	cfiscsi_pdu_queue(cp);
1014}
1015
1016static void
1017cfiscsi_session_terminate_tasks(struct cfiscsi_session *cs)
1018{
1019	struct cfiscsi_data_wait *cdw, *tmpcdw;
1020	union ctl_io *io;
1021	int error;
1022
1023#ifdef notyet
1024	io = ctl_alloc_io(cs->cs_target->ct_softc->fe.ctl_pool_ref);
1025	if (io == NULL) {
1026		CFISCSI_SESSION_WARN(cs, "can't allocate ctl_io");
1027		return;
1028	}
1029	ctl_zero_io(io);
1030	io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = NULL;
1031	io->io_hdr.io_type = CTL_IO_TASK;
1032	io->io_hdr.nexus.initid.id = cs->cs_ctl_initid;
1033	io->io_hdr.nexus.targ_port = cs->cs_target->ct_softc->fe.targ_port;
1034	io->io_hdr.nexus.targ_target.id = 0;
1035	io->io_hdr.nexus.targ_lun = lun;
1036	io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
1037	io->taskio.task_action = CTL_TASK_ABORT_TASK_SET;
1038	error = ctl_queue(io);
1039	if (error != CTL_RETVAL_COMPLETE) {
1040		CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d", error);
1041		ctl_free_io(io);
1042	}
1043#else
1044	/*
1045	 * CTL doesn't currently support CTL_TASK_ABORT_TASK_SET, so instead
1046	 * just iterate over tasks that are waiting for something - data - and
1047	 * terminate those.
1048	 */
1049	CFISCSI_SESSION_LOCK(cs);
1050	TAILQ_FOREACH_SAFE(cdw,
1051	    &cs->cs_waiting_for_data_out, cdw_next, tmpcdw) {
1052		io = ctl_alloc_io(cs->cs_target->ct_softc->fe.ctl_pool_ref);
1053		if (io == NULL) {
1054			CFISCSI_SESSION_WARN(cs, "can't allocate ctl_io");
1055			return;
1056		}
1057		ctl_zero_io(io);
1058		io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = NULL;
1059		io->io_hdr.io_type = CTL_IO_TASK;
1060		io->io_hdr.nexus.initid.id = cs->cs_ctl_initid;
1061		io->io_hdr.nexus.targ_port =
1062		    cs->cs_target->ct_softc->fe.targ_port;
1063		io->io_hdr.nexus.targ_target.id = 0;
1064		//io->io_hdr.nexus.targ_lun = lun; /* Not needed? */
1065		io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
1066		io->taskio.task_action = CTL_TASK_ABORT_TASK;
1067		io->taskio.tag_num = cdw->cdw_initiator_task_tag;
1068		error = ctl_queue(io);
1069		if (error != CTL_RETVAL_COMPLETE) {
1070			CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d", error);
1071			ctl_free_io(io);
1072			return;
1073		}
1074#if 0
1075		CFISCSI_SESSION_DEBUG(cs, "removing csw for initiator task tag "
1076		    "0x%x", cdw->cdw_initiator_task_tag);
1077#endif
1078		cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io);
1079		TAILQ_REMOVE(&cs->cs_waiting_for_data_out, cdw, cdw_next);
1080		uma_zfree(cfiscsi_data_wait_zone, cdw);
1081	}
1082	CFISCSI_SESSION_UNLOCK(cs);
1083#endif
1084}
1085
1086static void
1087cfiscsi_maintenance_thread(void *arg)
1088{
1089	struct cfiscsi_session *cs;
1090
1091	cs = arg;
1092
1093	for (;;) {
1094		CFISCSI_SESSION_LOCK(cs);
1095		if (cs->cs_terminating == false)
1096			cv_wait(&cs->cs_maintenance_cv, &cs->cs_lock);
1097		CFISCSI_SESSION_UNLOCK(cs);
1098
1099		if (cs->cs_terminating) {
1100			cfiscsi_session_terminate_tasks(cs);
1101			callout_drain(&cs->cs_callout);
1102
1103			icl_conn_shutdown(cs->cs_conn);
1104			icl_conn_close(cs->cs_conn);
1105
1106			cs->cs_terminating++;
1107
1108			/*
1109			 * XXX: We used to wait up to 30 seconds to deliver queued PDUs
1110			 * 	to the initiator.  We also tried hard to deliver SCSI Responses
1111			 * 	for the aborted PDUs.  We don't do that anymore.  We might need
1112			 * 	to revisit that.
1113			 */
1114
1115			cfiscsi_session_delete(cs);
1116			kthread_exit();
1117			return;
1118		}
1119		CFISCSI_SESSION_DEBUG(cs, "nothing to do");
1120	}
1121}
1122
1123static void
1124cfiscsi_session_terminate(struct cfiscsi_session *cs)
1125{
1126
1127	if (cs->cs_terminating != 0)
1128		return;
1129	cs->cs_terminating = 1;
1130	cv_signal(&cs->cs_maintenance_cv);
1131}
1132
1133static int
1134cfiscsi_session_register_initiator(struct cfiscsi_session *cs)
1135{
1136	int error, i;
1137	struct cfiscsi_softc *softc;
1138
1139	KASSERT(cs->cs_ctl_initid == -1, ("already registered"));
1140
1141	softc = &cfiscsi_softc;
1142
1143	mtx_lock(&softc->lock);
1144	for (i = 0; i < softc->max_initiators; i++) {
1145		if (softc->ctl_initids[i] == 0)
1146			break;
1147	}
1148	if (i == softc->max_initiators) {
1149		CFISCSI_SESSION_WARN(cs, "too many concurrent sessions (%d)",
1150		    softc->max_initiators);
1151		mtx_unlock(&softc->lock);
1152		return (1);
1153	}
1154	softc->ctl_initids[i] = 1;
1155	mtx_unlock(&softc->lock);
1156
1157#if 0
1158	CFISCSI_SESSION_DEBUG(cs, "adding initiator id %d, max %d",
1159	    i, softc->max_initiators);
1160#endif
1161	cs->cs_ctl_initid = i;
1162	error = ctl_add_initiator(0x0, softc->fe.targ_port, cs->cs_ctl_initid);
1163	if (error != 0) {
1164		CFISCSI_SESSION_WARN(cs, "ctl_add_initiator failed with error %d", error);
1165		mtx_lock(&softc->lock);
1166		softc->ctl_initids[cs->cs_ctl_initid] = 0;
1167		mtx_unlock(&softc->lock);
1168		cs->cs_ctl_initid = -1;
1169		return (1);
1170	}
1171
1172	return (0);
1173}
1174
1175static void
1176cfiscsi_session_unregister_initiator(struct cfiscsi_session *cs)
1177{
1178	int error;
1179	struct cfiscsi_softc *softc;
1180
1181	if (cs->cs_ctl_initid == -1)
1182		return;
1183
1184	softc = &cfiscsi_softc;
1185
1186	error = ctl_remove_initiator(softc->fe.targ_port, cs->cs_ctl_initid);
1187	if (error != 0) {
1188		CFISCSI_SESSION_WARN(cs, "ctl_remove_initiator failed with error %d",
1189		    error);
1190	}
1191	mtx_lock(&softc->lock);
1192	softc->ctl_initids[cs->cs_ctl_initid] = 0;
1193	mtx_unlock(&softc->lock);
1194	cs->cs_ctl_initid = -1;
1195}
1196
1197static struct cfiscsi_session *
1198cfiscsi_session_new(struct cfiscsi_softc *softc)
1199{
1200	struct cfiscsi_session *cs;
1201	int error;
1202
1203	cs = malloc(sizeof(*cs), M_CFISCSI, M_NOWAIT | M_ZERO);
1204	if (cs == NULL) {
1205		CFISCSI_WARN("malloc failed");
1206		return (NULL);
1207	}
1208	cs->cs_ctl_initid = -1;
1209
1210	refcount_init(&cs->cs_outstanding_ctl_pdus, 0);
1211	TAILQ_INIT(&cs->cs_waiting_for_data_out);
1212	mtx_init(&cs->cs_lock, "cfiscsi_lock", NULL, MTX_DEF);
1213	cv_init(&cs->cs_maintenance_cv, "cfiscsi_mt");
1214#ifdef ICL_KERNEL_PROXY
1215	cv_init(&cs->cs_login_cv, "cfiscsi_login");
1216#endif
1217
1218	cs->cs_conn = icl_conn_new();
1219	cs->cs_conn->ic_receive = cfiscsi_receive_callback;
1220	cs->cs_conn->ic_error = cfiscsi_error_callback;
1221	cs->cs_conn->ic_prv0 = cs;
1222
1223	error = kthread_add(cfiscsi_maintenance_thread, cs, NULL, NULL, 0, 0, "cfiscsimt");
1224	if (error != 0) {
1225		CFISCSI_SESSION_WARN(cs, "kthread_add(9) failed with error %d", error);
1226		free(cs, M_CFISCSI);
1227		return (NULL);
1228	}
1229
1230	mtx_lock(&softc->lock);
1231	cs->cs_id = softc->last_session_id + 1;
1232	softc->last_session_id++;
1233	mtx_unlock(&softc->lock);
1234
1235	mtx_lock(&softc->lock);
1236	TAILQ_INSERT_TAIL(&softc->sessions, cs, cs_next);
1237	mtx_unlock(&softc->lock);
1238
1239	/*
1240	 * Start pinging the initiator.
1241	 */
1242	callout_init(&cs->cs_callout, 1);
1243	callout_reset(&cs->cs_callout, 1 * hz, cfiscsi_callout, cs);
1244
1245	return (cs);
1246}
1247
1248static void
1249cfiscsi_session_delete(struct cfiscsi_session *cs)
1250{
1251	struct cfiscsi_softc *softc;
1252
1253	softc = &cfiscsi_softc;
1254
1255	KASSERT(cs->cs_outstanding_ctl_pdus == 0,
1256	    ("destroying session with outstanding CTL pdus"));
1257	KASSERT(TAILQ_EMPTY(&cs->cs_waiting_for_data_out),
1258	    ("destroying session with non-empty queue"));
1259
1260	cfiscsi_session_unregister_initiator(cs);
1261	if (cs->cs_target != NULL)
1262		cfiscsi_target_release(cs->cs_target);
1263	icl_conn_close(cs->cs_conn);
1264	icl_conn_free(cs->cs_conn);
1265
1266	mtx_lock(&softc->lock);
1267	TAILQ_REMOVE(&softc->sessions, cs, cs_next);
1268	mtx_unlock(&softc->lock);
1269
1270	free(cs, M_CFISCSI);
1271}
1272
1273int
1274cfiscsi_init(void)
1275{
1276	struct cfiscsi_softc *softc;
1277	struct ctl_frontend *fe;
1278	int retval;
1279
1280	softc = &cfiscsi_softc;
1281	retval = 0;
1282	bzero(softc, sizeof(*softc));
1283	mtx_init(&softc->lock, "cfiscsi", NULL, MTX_DEF);
1284
1285#ifdef ICL_KERNEL_PROXY
1286	cv_init(&softc->accept_cv, "cfiscsi_accept");
1287#endif
1288	TAILQ_INIT(&softc->sessions);
1289	TAILQ_INIT(&softc->targets);
1290
1291	fe = &softc->fe;
1292	fe->port_type = CTL_PORT_ISCSI;
1293	/* XXX KDM what should the real number be here? */
1294	fe->num_requested_ctl_io = 4096;
1295	snprintf(softc->port_name, sizeof(softc->port_name), "iscsi");
1296	fe->port_name = softc->port_name;
1297	fe->port_online = cfiscsi_online;
1298	fe->port_offline = cfiscsi_offline;
1299	fe->onoff_arg = softc;
1300	fe->targ_enable = cfiscsi_targ_enable;
1301	fe->targ_disable = cfiscsi_targ_disable;
1302	fe->lun_enable = cfiscsi_lun_enable;
1303	fe->lun_disable = cfiscsi_lun_disable;
1304	fe->targ_lun_arg = softc;
1305	fe->ioctl = cfiscsi_ioctl;
1306	fe->devid = cfiscsi_devid;
1307	fe->fe_datamove = cfiscsi_datamove;
1308	fe->fe_done = cfiscsi_done;
1309
1310	/* XXX KDM what should we report here? */
1311	/* XXX These should probably be fetched from CTL. */
1312	fe->max_targets = 1;
1313	fe->max_target_id = 15;
1314
1315	retval = ctl_frontend_register(fe, /*master_SC*/ 1);
1316	if (retval != 0) {
1317		CFISCSI_WARN("ctl_frontend_register() failed with error %d",
1318		    retval);
1319		retval = 1;
1320		goto bailout;
1321	}
1322
1323	softc->max_initiators = fe->max_initiators;
1324
1325	cfiscsi_data_wait_zone = uma_zcreate("cfiscsi_data_wait",
1326	    sizeof(struct cfiscsi_data_wait), NULL, NULL, NULL, NULL,
1327	    UMA_ALIGN_PTR, 0);
1328
1329	return (0);
1330
1331bailout:
1332	return (retval);
1333}
1334
1335static int
1336cfiscsi_module_event_handler(module_t mod, int what, void *arg)
1337{
1338
1339	switch (what) {
1340	case MOD_LOAD:
1341		return (cfiscsi_init());
1342	case MOD_UNLOAD:
1343		return (EBUSY);
1344	default:
1345		return (EOPNOTSUPP);
1346	}
1347}
1348
1349#ifdef ICL_KERNEL_PROXY
1350static void
1351cfiscsi_accept(struct socket *so)
1352{
1353	struct cfiscsi_session *cs;
1354
1355	cs = cfiscsi_session_new(&cfiscsi_softc);
1356	if (cs == NULL) {
1357		CFISCSI_WARN("failed to create session");
1358		return;
1359	}
1360
1361	icl_conn_handoff_sock(cs->cs_conn, so);
1362	cs->cs_waiting_for_ctld = true;
1363	cv_signal(&cfiscsi_softc.accept_cv);
1364}
1365#endif
1366
1367static void
1368cfiscsi_online(void *arg)
1369{
1370	struct cfiscsi_softc *softc;
1371
1372	softc = (struct cfiscsi_softc *)arg;
1373
1374	softc->online = 1;
1375#ifdef ICL_KERNEL_PROXY
1376	if (softc->listener != NULL)
1377		icl_listen_free(softc->listener);
1378	softc->listener = icl_listen_new(cfiscsi_accept);
1379#endif
1380}
1381
1382static void
1383cfiscsi_offline(void *arg)
1384{
1385	struct cfiscsi_softc *softc;
1386	struct cfiscsi_session *cs;
1387
1388	softc = (struct cfiscsi_softc *)arg;
1389
1390	softc->online = 0;
1391
1392	mtx_lock(&softc->lock);
1393	TAILQ_FOREACH(cs, &softc->sessions, cs_next)
1394		cfiscsi_session_terminate(cs);
1395	mtx_unlock(&softc->lock);
1396
1397#ifdef ICL_KERNEL_PROXY
1398	icl_listen_free(softc->listener);
1399	softc->listener = NULL;
1400#endif
1401}
1402
1403static int
1404cfiscsi_targ_enable(void *arg, struct ctl_id targ_id)
1405{
1406
1407	return (0);
1408}
1409
1410static int
1411cfiscsi_targ_disable(void *arg, struct ctl_id targ_id)
1412{
1413
1414	return (0);
1415}
1416
1417static void
1418cfiscsi_ioctl_handoff(struct ctl_iscsi *ci)
1419{
1420	struct cfiscsi_softc *softc;
1421	struct cfiscsi_session *cs;
1422	struct cfiscsi_target *ct;
1423	struct ctl_iscsi_handoff_params *cihp;
1424#ifndef ICL_KERNEL_PROXY
1425	int error;
1426#endif
1427
1428	cihp = (struct ctl_iscsi_handoff_params *)&(ci->data);
1429	softc = &cfiscsi_softc;
1430
1431	CFISCSI_DEBUG("new connection from %s (%s) to %s",
1432	    cihp->initiator_name, cihp->initiator_addr,
1433	    cihp->target_name);
1434
1435	if (softc->online == 0) {
1436		ci->status = CTL_ISCSI_ERROR;
1437		snprintf(ci->error_str, sizeof(ci->error_str),
1438		    "%s: port offline", __func__);
1439		return;
1440	}
1441
1442	ct = cfiscsi_target_find(softc, cihp->target_name);
1443	if (ct == NULL) {
1444		ci->status = CTL_ISCSI_ERROR;
1445		snprintf(ci->error_str, sizeof(ci->error_str),
1446		    "%s: target not found", __func__);
1447		return;
1448	}
1449
1450#ifdef ICL_KERNEL_PROXY
1451	mtx_lock(&cfiscsi_softc.lock);
1452	TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1453		if (cs->cs_id == cihp->socket)
1454			break;
1455	}
1456	if (cs == NULL) {
1457		mtx_unlock(&cfiscsi_softc.lock);
1458		snprintf(ci->error_str, sizeof(ci->error_str), "connection not found");
1459		ci->status = CTL_ISCSI_ERROR;
1460		return;
1461	}
1462	mtx_unlock(&cfiscsi_softc.lock);
1463#else
1464	cs = cfiscsi_session_new(softc);
1465	if (cs == NULL) {
1466		ci->status = CTL_ISCSI_ERROR;
1467		snprintf(ci->error_str, sizeof(ci->error_str),
1468		    "%s: cfiscsi_session_new failed", __func__);
1469		cfiscsi_target_release(ct);
1470		return;
1471	}
1472#endif
1473	cs->cs_target = ct;
1474
1475	/*
1476	 * First PDU of Full Feature phase has the same CmdSN as the last
1477	 * PDU from the Login Phase received from the initiator.  Thus,
1478	 * the -1 below.
1479	 */
1480	cs->cs_portal_group_tag = cihp->portal_group_tag;
1481	cs->cs_cmdsn = cihp->cmdsn;
1482	cs->cs_statsn = cihp->statsn;
1483	cs->cs_max_data_segment_length = cihp->max_recv_data_segment_length;
1484	cs->cs_max_burst_length = cihp->max_burst_length;
1485	cs->cs_immediate_data = !!cihp->immediate_data;
1486	if (cihp->header_digest == CTL_ISCSI_DIGEST_CRC32C)
1487		cs->cs_conn->ic_header_crc32c = true;
1488	if (cihp->data_digest == CTL_ISCSI_DIGEST_CRC32C)
1489		cs->cs_conn->ic_data_crc32c = true;
1490
1491	strlcpy(cs->cs_initiator_name,
1492	    cihp->initiator_name, sizeof(cs->cs_initiator_name));
1493	strlcpy(cs->cs_initiator_addr,
1494	    cihp->initiator_addr, sizeof(cs->cs_initiator_addr));
1495	strlcpy(cs->cs_initiator_alias,
1496	    cihp->initiator_alias, sizeof(cs->cs_initiator_alias));
1497
1498#ifdef ICL_KERNEL_PROXY
1499	cs->cs_login_phase = false;
1500#else
1501	error = icl_conn_handoff(cs->cs_conn, cihp->socket);
1502	if (error != 0) {
1503		cfiscsi_session_delete(cs);
1504		ci->status = CTL_ISCSI_ERROR;
1505		snprintf(ci->error_str, sizeof(ci->error_str),
1506		    "%s: icl_conn_handoff failed with error %d",
1507		    __func__, error);
1508		return;
1509	}
1510#endif
1511
1512	/*
1513	 * Register initiator with CTL.
1514	 */
1515	cfiscsi_session_register_initiator(cs);
1516
1517#ifdef ICL_KERNEL_PROXY
1518	/*
1519	 * First PDU of the Full Feature phase has likely already arrived.
1520	 * We have to pick it up and execute properly.
1521	 */
1522	if (cs->cs_login_pdu != NULL) {
1523		CFISCSI_SESSION_DEBUG(cs, "picking up first PDU");
1524		cfiscsi_pdu_handle(cs->cs_login_pdu);
1525		cs->cs_login_pdu = NULL;
1526	}
1527#endif
1528
1529	ci->status = CTL_ISCSI_OK;
1530}
1531
1532static void
1533cfiscsi_ioctl_list(struct ctl_iscsi *ci)
1534{
1535	struct ctl_iscsi_list_params *cilp;
1536	struct cfiscsi_session *cs;
1537	struct cfiscsi_softc *softc;
1538	struct sbuf *sb;
1539	int error;
1540
1541	cilp = (struct ctl_iscsi_list_params *)&(ci->data);
1542	softc = &cfiscsi_softc;
1543
1544	sb = sbuf_new(NULL, NULL, cilp->alloc_len, SBUF_FIXEDLEN);
1545	if (sb == NULL) {
1546		ci->status = CTL_ISCSI_ERROR;
1547		snprintf(ci->error_str, sizeof(ci->error_str),
1548		    "Unable to allocate %d bytes for iSCSI session list",
1549		    cilp->alloc_len);
1550		return;
1551	}
1552
1553	sbuf_printf(sb, "<ctlislist>\n");
1554	mtx_lock(&softc->lock);
1555	TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1556#ifdef ICL_KERNEL_PROXY
1557		if (cs->cs_target == NULL)
1558			continue;
1559#endif
1560		error = sbuf_printf(sb, "<connection id=\"%d\">"
1561		    "<initiator>%s</initiator>"
1562		    "<initiator_addr>%s</initiator_addr>"
1563		    "<initiator_alias>%s</initiator_alias>"
1564		    "<target>%s</target>"
1565		    "<target_alias>%s</target_alias>"
1566		    "<header_digest>%s</header_digest>"
1567		    "<data_digest>%s</data_digest>"
1568		    "<max_data_segment_length>%zd</max_data_segment_length>"
1569		    "<immediate_data>%d</immediate_data>"
1570		    "<iser>%d</iser>"
1571		    "</connection>\n",
1572		    cs->cs_id,
1573		    cs->cs_initiator_name, cs->cs_initiator_addr, cs->cs_initiator_alias,
1574		    cs->cs_target->ct_name, cs->cs_target->ct_alias,
1575		    cs->cs_conn->ic_header_crc32c ? "CRC32C" : "None",
1576		    cs->cs_conn->ic_data_crc32c ? "CRC32C" : "None",
1577		    cs->cs_max_data_segment_length,
1578		    cs->cs_immediate_data,
1579		    cs->cs_conn->ic_iser);
1580		if (error != 0)
1581			break;
1582	}
1583	mtx_unlock(&softc->lock);
1584	error = sbuf_printf(sb, "</ctlislist>\n");
1585	if (error != 0) {
1586		sbuf_delete(sb);
1587		ci->status = CTL_ISCSI_LIST_NEED_MORE_SPACE;
1588		snprintf(ci->error_str, sizeof(ci->error_str),
1589		    "Out of space, %d bytes is too small", cilp->alloc_len);
1590		return;
1591	}
1592	sbuf_finish(sb);
1593
1594	error = copyout(sbuf_data(sb), cilp->conn_xml, sbuf_len(sb) + 1);
1595	cilp->fill_len = sbuf_len(sb) + 1;
1596	ci->status = CTL_ISCSI_OK;
1597	sbuf_delete(sb);
1598}
1599
1600static void
1601cfiscsi_ioctl_terminate(struct ctl_iscsi *ci)
1602{
1603	struct icl_pdu *response;
1604	struct iscsi_bhs_asynchronous_message *bhsam;
1605	struct ctl_iscsi_terminate_params *citp;
1606	struct cfiscsi_session *cs;
1607	struct cfiscsi_softc *softc;
1608	int found = 0;
1609
1610	citp = (struct ctl_iscsi_terminate_params *)&(ci->data);
1611	softc = &cfiscsi_softc;
1612
1613	mtx_lock(&softc->lock);
1614	TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1615		if (citp->all == 0 && cs->cs_id != citp->connection_id &&
1616		    strcmp(cs->cs_initiator_name, citp->initiator_name) != 0 &&
1617		    strcmp(cs->cs_initiator_addr, citp->initiator_addr) != 0)
1618			continue;
1619
1620		response = icl_pdu_new_bhs(cs->cs_conn, M_NOWAIT);
1621		if (response == NULL) {
1622			/*
1623			 * Oh well.  Just terminate the connection.
1624			 */
1625		} else {
1626			bhsam = (struct iscsi_bhs_asynchronous_message *)
1627			    response->ip_bhs;
1628			bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE;
1629			bhsam->bhsam_flags = 0x80;
1630			bhsam->bhsam_0xffffffff = 0xffffffff;
1631			bhsam->bhsam_async_event =
1632			    BHSAM_EVENT_TARGET_TERMINATES_SESSION;
1633			cfiscsi_pdu_queue(response);
1634		}
1635		cfiscsi_session_terminate(cs);
1636		found++;
1637	}
1638	mtx_unlock(&softc->lock);
1639
1640	if (found == 0) {
1641		ci->status = CTL_ISCSI_SESSION_NOT_FOUND;
1642		snprintf(ci->error_str, sizeof(ci->error_str),
1643		    "No matching connections found");
1644		return;
1645	}
1646
1647	ci->status = CTL_ISCSI_OK;
1648}
1649
1650static void
1651cfiscsi_ioctl_logout(struct ctl_iscsi *ci)
1652{
1653	struct icl_pdu *response;
1654	struct iscsi_bhs_asynchronous_message *bhsam;
1655	struct ctl_iscsi_logout_params *cilp;
1656	struct cfiscsi_session *cs;
1657	struct cfiscsi_softc *softc;
1658	int found = 0;
1659
1660	cilp = (struct ctl_iscsi_logout_params *)&(ci->data);
1661	softc = &cfiscsi_softc;
1662
1663	mtx_lock(&softc->lock);
1664	TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1665		if (cilp->all == 0 && cs->cs_id != cilp->connection_id &&
1666		    strcmp(cs->cs_initiator_name, cilp->initiator_name) != 0 &&
1667		    strcmp(cs->cs_initiator_addr, cilp->initiator_addr) != 0)
1668			continue;
1669
1670		response = icl_pdu_new_bhs(cs->cs_conn, M_NOWAIT);
1671		if (response == NULL) {
1672			ci->status = CTL_ISCSI_ERROR;
1673			snprintf(ci->error_str, sizeof(ci->error_str),
1674			    "Unable to allocate memory");
1675			mtx_unlock(&softc->lock);
1676			return;
1677		}
1678		bhsam =
1679		    (struct iscsi_bhs_asynchronous_message *)response->ip_bhs;
1680		bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE;
1681		bhsam->bhsam_flags = 0x80;
1682		bhsam->bhsam_async_event = BHSAM_EVENT_TARGET_REQUESTS_LOGOUT;
1683		bhsam->bhsam_parameter3 = htons(10);
1684		cfiscsi_pdu_queue(response);
1685		found++;
1686	}
1687	mtx_unlock(&softc->lock);
1688
1689	if (found == 0) {
1690		ci->status = CTL_ISCSI_SESSION_NOT_FOUND;
1691		snprintf(ci->error_str, sizeof(ci->error_str),
1692		    "No matching connections found");
1693		return;
1694	}
1695
1696	ci->status = CTL_ISCSI_OK;
1697}
1698
1699#ifdef ICL_KERNEL_PROXY
1700static void
1701cfiscsi_ioctl_listen(struct ctl_iscsi *ci)
1702{
1703	struct ctl_iscsi_listen_params *cilp;
1704	struct sockaddr *sa;
1705	int error;
1706
1707	cilp = (struct ctl_iscsi_listen_params *)&(ci->data);
1708
1709	if (cfiscsi_softc.listener == NULL) {
1710		CFISCSI_DEBUG("no listener");
1711		snprintf(ci->error_str, sizeof(ci->error_str), "no listener");
1712		ci->status = CTL_ISCSI_ERROR;
1713		return;
1714	}
1715
1716	error = getsockaddr(&sa, (void *)cilp->addr, cilp->addrlen);
1717	if (error != 0) {
1718		CFISCSI_DEBUG("getsockaddr, error %d", error);
1719		snprintf(ci->error_str, sizeof(ci->error_str), "getsockaddr failed");
1720		ci->status = CTL_ISCSI_ERROR;
1721		return;
1722	}
1723
1724	error = icl_listen_add(cfiscsi_softc.listener, cilp->iser, cilp->domain,
1725	    cilp->socktype, cilp->protocol, sa);
1726	if (error != 0) {
1727		free(sa, M_SONAME);
1728		CFISCSI_DEBUG("icl_listen_add, error %d", error);
1729		snprintf(ci->error_str, sizeof(ci->error_str),
1730		    "icl_listen_add failed, error %d", error);
1731		ci->status = CTL_ISCSI_ERROR;
1732		return;
1733	}
1734
1735	ci->status = CTL_ISCSI_OK;
1736}
1737
1738static void
1739cfiscsi_ioctl_accept(struct ctl_iscsi *ci)
1740{
1741	struct ctl_iscsi_accept_params *ciap;
1742	struct cfiscsi_session *cs;
1743	int error;
1744
1745	ciap = (struct ctl_iscsi_accept_params *)&(ci->data);
1746
1747	mtx_lock(&cfiscsi_softc.lock);
1748	for (;;) {
1749		TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1750			if (cs->cs_waiting_for_ctld)
1751				break;
1752		}
1753		if (cs != NULL)
1754			break;
1755		error = cv_wait_sig(&cfiscsi_softc.accept_cv, &cfiscsi_softc.lock);
1756		if (error != 0) {
1757			mtx_unlock(&cfiscsi_softc.lock);
1758			snprintf(ci->error_str, sizeof(ci->error_str), "interrupted");
1759			ci->status = CTL_ISCSI_ERROR;
1760			return;
1761		}
1762	}
1763	mtx_unlock(&cfiscsi_softc.lock);
1764
1765	cs->cs_waiting_for_ctld = false;
1766	cs->cs_login_phase = true;
1767
1768	ciap->connection_id = cs->cs_id;
1769	ci->status = CTL_ISCSI_OK;
1770}
1771
1772static void
1773cfiscsi_ioctl_send(struct ctl_iscsi *ci)
1774{
1775	struct ctl_iscsi_send_params *cisp;
1776	struct cfiscsi_session *cs;
1777	struct icl_pdu *ip;
1778	size_t datalen;
1779	void *data;
1780	int error;
1781
1782	cisp = (struct ctl_iscsi_send_params *)&(ci->data);
1783
1784	mtx_lock(&cfiscsi_softc.lock);
1785	TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1786		if (cs->cs_id == cisp->connection_id)
1787			break;
1788	}
1789	if (cs == NULL) {
1790		mtx_unlock(&cfiscsi_softc.lock);
1791		snprintf(ci->error_str, sizeof(ci->error_str), "connection not found");
1792		ci->status = CTL_ISCSI_ERROR;
1793		return;
1794	}
1795	mtx_unlock(&cfiscsi_softc.lock);
1796
1797#if 0
1798	if (cs->cs_login_phase == false)
1799		return (EBUSY);
1800#endif
1801
1802	if (cs->cs_terminating) {
1803		snprintf(ci->error_str, sizeof(ci->error_str), "connection is terminating");
1804		ci->status = CTL_ISCSI_ERROR;
1805		return;
1806	}
1807
1808	datalen = cisp->data_segment_len;
1809	/*
1810	 * XXX
1811	 */
1812	//if (datalen > CFISCSI_MAX_DATA_SEGMENT_LENGTH) {
1813	if (datalen > 65535) {
1814		snprintf(ci->error_str, sizeof(ci->error_str), "data segment too big");
1815		ci->status = CTL_ISCSI_ERROR;
1816		return;
1817	}
1818	if (datalen > 0) {
1819		data = malloc(datalen, M_CFISCSI, M_WAITOK);
1820		error = copyin(cisp->data_segment, data, datalen);
1821		if (error != 0) {
1822			free(data, M_CFISCSI);
1823			snprintf(ci->error_str, sizeof(ci->error_str), "copyin error %d", error);
1824			ci->status = CTL_ISCSI_ERROR;
1825			return;
1826		}
1827	}
1828
1829	ip = icl_pdu_new_bhs(cs->cs_conn, M_WAITOK);
1830	memcpy(ip->ip_bhs, cisp->bhs, sizeof(*ip->ip_bhs));
1831	if (datalen > 0) {
1832		icl_pdu_append_data(ip, data, datalen, M_WAITOK);
1833		free(data, M_CFISCSI);
1834	}
1835	icl_pdu_queue(ip);
1836	ci->status = CTL_ISCSI_OK;
1837}
1838
1839static void
1840cfiscsi_ioctl_receive(struct ctl_iscsi *ci)
1841{
1842	struct ctl_iscsi_receive_params *cirp;
1843	struct cfiscsi_session *cs;
1844	struct icl_pdu *ip;
1845	void *data;
1846
1847	cirp = (struct ctl_iscsi_receive_params *)&(ci->data);
1848
1849	mtx_lock(&cfiscsi_softc.lock);
1850	TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1851		if (cs->cs_id == cirp->connection_id)
1852			break;
1853	}
1854	if (cs == NULL) {
1855		mtx_unlock(&cfiscsi_softc.lock);
1856		snprintf(ci->error_str, sizeof(ci->error_str), "connection not found");
1857		ci->status = CTL_ISCSI_ERROR;
1858		return;
1859	}
1860	mtx_unlock(&cfiscsi_softc.lock);
1861
1862#if 0
1863	if (is->is_login_phase == false)
1864		return (EBUSY);
1865#endif
1866
1867	CFISCSI_SESSION_LOCK(cs);
1868	while (cs->cs_login_pdu == NULL &&
1869	    cs->cs_terminating == false)
1870		cv_wait(&cs->cs_login_cv, &cs->cs_lock);
1871	if (cs->cs_terminating) {
1872		CFISCSI_SESSION_UNLOCK(cs);
1873		snprintf(ci->error_str, sizeof(ci->error_str), "connection terminating");
1874		ci->status = CTL_ISCSI_ERROR;
1875		return;
1876	}
1877	ip = cs->cs_login_pdu;
1878	cs->cs_login_pdu = NULL;
1879	CFISCSI_SESSION_UNLOCK(cs);
1880
1881	if (ip->ip_data_len > cirp->data_segment_len) {
1882		icl_pdu_free(ip);
1883		snprintf(ci->error_str, sizeof(ci->error_str), "data segment too big");
1884		ci->status = CTL_ISCSI_ERROR;
1885		return;
1886	}
1887
1888	copyout(ip->ip_bhs, cirp->bhs, sizeof(*ip->ip_bhs));
1889	if (ip->ip_data_len > 0) {
1890		data = malloc(ip->ip_data_len, M_CFISCSI, M_WAITOK);
1891		icl_pdu_get_data(ip, 0, data, ip->ip_data_len);
1892		copyout(data, cirp->data_segment, ip->ip_data_len);
1893		free(data, M_CFISCSI);
1894	}
1895
1896	icl_pdu_free(ip);
1897	ci->status = CTL_ISCSI_OK;
1898}
1899
1900static void
1901cfiscsi_ioctl_close(struct ctl_iscsi *ci)
1902{
1903	/*
1904	 * XXX
1905	 */
1906}
1907#endif /* !ICL_KERNEL_PROXY */
1908
1909static int
1910cfiscsi_ioctl(struct cdev *dev,
1911    u_long cmd, caddr_t addr, int flag, struct thread *td)
1912{
1913	struct ctl_iscsi *ci;
1914
1915	if (cmd != CTL_ISCSI)
1916		return (ENOTTY);
1917
1918	ci = (struct ctl_iscsi *)addr;
1919	switch (ci->type) {
1920	case CTL_ISCSI_HANDOFF:
1921		cfiscsi_ioctl_handoff(ci);
1922		break;
1923	case CTL_ISCSI_LIST:
1924		cfiscsi_ioctl_list(ci);
1925		break;
1926	case CTL_ISCSI_TERMINATE:
1927		cfiscsi_ioctl_terminate(ci);
1928		break;
1929	case CTL_ISCSI_LOGOUT:
1930		cfiscsi_ioctl_logout(ci);
1931		break;
1932#ifdef ICL_KERNEL_PROXY
1933	case CTL_ISCSI_LISTEN:
1934		cfiscsi_ioctl_listen(ci);
1935		break;
1936	case CTL_ISCSI_ACCEPT:
1937		cfiscsi_ioctl_accept(ci);
1938		break;
1939	case CTL_ISCSI_SEND:
1940		cfiscsi_ioctl_send(ci);
1941		break;
1942	case CTL_ISCSI_RECEIVE:
1943		cfiscsi_ioctl_receive(ci);
1944		break;
1945	case CTL_ISCSI_CLOSE:
1946		cfiscsi_ioctl_close(ci);
1947		break;
1948#endif /* ICL_KERNEL_PROXY */
1949	default:
1950		ci->status = CTL_ISCSI_ERROR;
1951		snprintf(ci->error_str, sizeof(ci->error_str),
1952		    "%s: invalid iSCSI request type %d", __func__, ci->type);
1953		break;
1954	}
1955
1956	return (0);
1957}
1958
1959static int
1960cfiscsi_devid(struct ctl_scsiio *ctsio, int alloc_len)
1961{
1962	struct cfiscsi_session *cs;
1963	struct scsi_vpd_device_id *devid_ptr;
1964	struct scsi_vpd_id_descriptor *desc, *desc1;
1965	struct scsi_vpd_id_descriptor *desc2, *desc3; /* for types 4h and 5h */
1966	struct scsi_vpd_id_t10 *t10id;
1967	struct ctl_lun *lun;
1968	const struct icl_pdu *request;
1969	size_t devid_len, wwpn_len;
1970
1971	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
1972	request = ctsio->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1973	cs = PDU_SESSION(request);
1974
1975	wwpn_len = strlen(cs->cs_target->ct_name);
1976	wwpn_len += strlen(",t,0x01");
1977	wwpn_len += 1; /* '\0' */
1978	if ((wwpn_len % 4) != 0)
1979		wwpn_len += (4 - (wwpn_len % 4));
1980
1981	devid_len = sizeof(struct scsi_vpd_device_id) +
1982		sizeof(struct scsi_vpd_id_descriptor) +
1983		sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN +
1984		sizeof(struct scsi_vpd_id_descriptor) + wwpn_len +
1985		sizeof(struct scsi_vpd_id_descriptor) +
1986		sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
1987		sizeof(struct scsi_vpd_id_descriptor) +
1988		sizeof(struct scsi_vpd_id_trgt_port_grp_id);
1989
1990	ctsio->kern_data_ptr = malloc(devid_len, M_CTL, M_WAITOK | M_ZERO);
1991	devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
1992	ctsio->kern_sg_entries = 0;
1993
1994	if (devid_len < alloc_len) {
1995		ctsio->residual = alloc_len - devid_len;
1996		ctsio->kern_data_len = devid_len;
1997		ctsio->kern_total_len = devid_len;
1998	} else {
1999		ctsio->residual = 0;
2000		ctsio->kern_data_len = alloc_len;
2001		ctsio->kern_total_len = alloc_len;
2002	}
2003	ctsio->kern_data_resid = 0;
2004	ctsio->kern_rel_offset = 0;
2005	ctsio->kern_sg_entries = 0;
2006
2007	desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
2008	t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
2009	desc1 = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
2010	    sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN);
2011	desc2 = (struct scsi_vpd_id_descriptor *)(&desc1->identifier[0] +
2012	    wwpn_len);
2013	desc3 = (struct scsi_vpd_id_descriptor *)(&desc2->identifier[0] +
2014	    sizeof(struct scsi_vpd_id_rel_trgt_port_id));
2015
2016	if (lun != NULL)
2017		devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
2018		    lun->be_lun->lun_type;
2019	else
2020		devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
2021
2022	devid_ptr->page_code = SVPD_DEVICE_ID;
2023
2024	scsi_ulto2b(devid_len - 4, devid_ptr->length);
2025
2026	/*
2027	 * We're using a LUN association here.  i.e., this device ID is a
2028	 * per-LUN identifier.
2029	 */
2030	desc->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_ASCII;
2031	desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
2032	desc->length = sizeof(*t10id) + CTL_DEVID_LEN;
2033	strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
2034
2035	/*
2036	 * If we've actually got a backend, copy the device id from the
2037	 * per-LUN data.  Otherwise, set it to all spaces.
2038	 */
2039	if (lun != NULL) {
2040		/*
2041		 * Copy the backend's LUN ID.
2042		 */
2043		strncpy((char *)t10id->vendor_spec_id,
2044		    (char *)lun->be_lun->device_id, CTL_DEVID_LEN);
2045	} else {
2046		/*
2047		 * No backend, set this to spaces.
2048		 */
2049		memset(t10id->vendor_spec_id, 0x20, CTL_DEVID_LEN);
2050	}
2051
2052	/*
2053	 * desc1 is for the WWPN which is a port asscociation.
2054	 */
2055       	desc1->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8;
2056	desc1->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
2057	    SVPD_ID_TYPE_SCSI_NAME;
2058	desc1->length = wwpn_len;
2059	snprintf(desc1->identifier, wwpn_len, "%s,t,0x%x",
2060	    cs->cs_target->ct_name, cs->cs_portal_group_tag);
2061
2062	/*
2063	 * desc2 is for the Relative Target Port(type 4h) identifier
2064	 */
2065       	desc2->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_BINARY;
2066	desc2->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
2067	    SVPD_ID_TYPE_RELTARG;
2068	desc2->length = 4;
2069	desc2->identifier[3] = 1;
2070
2071	/*
2072	 * desc3 is for the Target Port Group(type 5h) identifier
2073	 */
2074       	desc3->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_BINARY;
2075	desc3->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
2076	    SVPD_ID_TYPE_TPORTGRP;
2077	desc3->length = 4;
2078	desc3->identifier[3] = 1;
2079
2080	ctsio->scsi_status = SCSI_STATUS_OK;
2081
2082	ctsio->be_move_done = ctl_config_move_done;
2083	ctl_datamove((union ctl_io *)ctsio);
2084
2085	return (CTL_RETVAL_COMPLETE);
2086}
2087
2088static void
2089cfiscsi_target_hold(struct cfiscsi_target *ct)
2090{
2091
2092	refcount_acquire(&ct->ct_refcount);
2093}
2094
2095static void
2096cfiscsi_target_release(struct cfiscsi_target *ct)
2097{
2098	int old;
2099	struct cfiscsi_softc *softc;
2100
2101	softc = ct->ct_softc;
2102
2103	old = ct->ct_refcount;
2104	if (old > 1 && atomic_cmpset_int(&ct->ct_refcount, old, old - 1))
2105		return;
2106
2107	mtx_lock(&softc->lock);
2108	if (refcount_release(&ct->ct_refcount)) {
2109		TAILQ_REMOVE(&softc->targets, ct, ct_next);
2110		mtx_unlock(&softc->lock);
2111		free(ct, M_CFISCSI);
2112
2113		return;
2114	}
2115	mtx_unlock(&softc->lock);
2116}
2117
2118static struct cfiscsi_target *
2119cfiscsi_target_find(struct cfiscsi_softc *softc, const char *name)
2120{
2121	struct cfiscsi_target *ct;
2122
2123	mtx_lock(&softc->lock);
2124	TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2125		if (strcmp(name, ct->ct_name) != 0)
2126			continue;
2127		cfiscsi_target_hold(ct);
2128		mtx_unlock(&softc->lock);
2129		return (ct);
2130	}
2131	mtx_unlock(&softc->lock);
2132
2133	return (NULL);
2134}
2135
2136static struct cfiscsi_target *
2137cfiscsi_target_find_or_create(struct cfiscsi_softc *softc, const char *name,
2138    const char *alias)
2139{
2140	struct cfiscsi_target *ct, *newct;
2141	int i;
2142
2143	if (name[0] == '\0' || strlen(name) >= CTL_ISCSI_NAME_LEN)
2144		return (NULL);
2145
2146	newct = malloc(sizeof(*newct), M_CFISCSI, M_WAITOK | M_ZERO);
2147
2148	mtx_lock(&softc->lock);
2149	TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2150		if (strcmp(name, ct->ct_name) != 0)
2151			continue;
2152		cfiscsi_target_hold(ct);
2153		mtx_unlock(&softc->lock);
2154		free(newct, M_CFISCSI);
2155		return (ct);
2156	}
2157
2158	for (i = 0; i < CTL_MAX_LUNS; i++)
2159		newct->ct_luns[i] = -1;
2160
2161	strlcpy(newct->ct_name, name, sizeof(newct->ct_name));
2162	if (alias != NULL)
2163		strlcpy(newct->ct_alias, alias, sizeof(newct->ct_alias));
2164	refcount_init(&newct->ct_refcount, 1);
2165	newct->ct_softc = softc;
2166	TAILQ_INSERT_TAIL(&softc->targets, newct, ct_next);
2167	mtx_unlock(&softc->lock);
2168
2169	return (newct);
2170}
2171
2172/*
2173 * Takes LUN from the target space and returns LUN from the CTL space.
2174 */
2175static uint32_t
2176cfiscsi_map_lun(void *arg, uint32_t lun)
2177{
2178	struct cfiscsi_session *cs;
2179
2180	cs = arg;
2181
2182	if (lun >= CTL_MAX_LUNS) {
2183		CFISCSI_DEBUG("requested lun number %d is higher "
2184		    "than maximum %d", lun, CTL_MAX_LUNS - 1);
2185		return (0xffffffff);
2186	}
2187
2188	if (cs->cs_target->ct_luns[lun] < 0)
2189		return (0xffffffff);
2190
2191	return (cs->cs_target->ct_luns[lun]);
2192}
2193
2194static int
2195cfiscsi_target_set_lun(struct cfiscsi_target *ct,
2196    unsigned long lun_id, unsigned long ctl_lun_id)
2197{
2198
2199	if (lun_id >= CTL_MAX_LUNS) {
2200		CFISCSI_WARN("requested lun number %ld is higher "
2201		    "than maximum %d", lun_id, CTL_MAX_LUNS - 1);
2202		return (-1);
2203	}
2204
2205	if (ct->ct_luns[lun_id] >= 0) {
2206		/*
2207		 * CTL calls cfiscsi_lun_enable() twice for each LUN - once
2208		 * when the LUN is created, and a second time just before
2209		 * the port is brought online; don't emit warnings
2210		 * for that case.
2211		 */
2212		if (ct->ct_luns[lun_id] == ctl_lun_id)
2213			return (0);
2214		CFISCSI_WARN("lun %ld already allocated", lun_id);
2215		return (-1);
2216	}
2217
2218#if 0
2219	CFISCSI_DEBUG("adding mapping for lun %ld, target %s "
2220	    "to ctl lun %ld", lun_id, ct->ct_name, ctl_lun_id);
2221#endif
2222
2223	ct->ct_luns[lun_id] = ctl_lun_id;
2224	cfiscsi_target_hold(ct);
2225
2226	return (0);
2227}
2228
2229static int
2230cfiscsi_target_unset_lun(struct cfiscsi_target *ct, unsigned long lun_id)
2231{
2232
2233	if (ct->ct_luns[lun_id] < 0) {
2234		CFISCSI_WARN("lun %ld not allocated", lun_id);
2235		return (-1);
2236	}
2237
2238	ct->ct_luns[lun_id] = -1;
2239	cfiscsi_target_release(ct);
2240
2241	return (0);
2242}
2243
2244static int
2245cfiscsi_lun_enable(void *arg, struct ctl_id target_id, int lun_id)
2246{
2247	struct cfiscsi_softc *softc;
2248	struct cfiscsi_target *ct;
2249	struct ctl_be_lun_option *opt;
2250	const char *target = NULL, *target_alias = NULL;
2251	const char *lun = NULL;
2252	unsigned long tmp;
2253
2254	softc = (struct cfiscsi_softc *)arg;
2255
2256	STAILQ_FOREACH(opt,
2257	    &control_softc->ctl_luns[lun_id]->be_lun->options, links) {
2258		if (strcmp(opt->name, "cfiscsi_target") == 0)
2259			target = opt->value;
2260		else if (strcmp(opt->name, "cfiscsi_target_alias") == 0)
2261			target_alias = opt->value;
2262		else if (strcmp(opt->name, "cfiscsi_lun") == 0)
2263			lun = opt->value;
2264	}
2265
2266	if (target == NULL && lun == NULL)
2267		return (0);
2268
2269	if (target == NULL || lun == NULL) {
2270		CFISCSI_WARN("lun added with cfiscsi_target, but without "
2271		    "cfiscsi_lun, or the other way around; ignoring");
2272		return (0);
2273	}
2274
2275	ct = cfiscsi_target_find_or_create(softc, target, target_alias);
2276	if (ct == NULL) {
2277		CFISCSI_WARN("failed to create target \"%s\"", target);
2278		return (0);
2279	}
2280
2281	tmp = strtoul(lun, NULL, 10);
2282	cfiscsi_target_set_lun(ct, tmp, lun_id);
2283	return (0);
2284}
2285
2286static int
2287cfiscsi_lun_disable(void *arg, struct ctl_id target_id, int lun_id)
2288{
2289	struct cfiscsi_softc *softc;
2290	struct cfiscsi_target *ct;
2291	int i;
2292
2293	softc = (struct cfiscsi_softc *)arg;
2294
2295	mtx_lock(&softc->lock);
2296	TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2297		for (i = 0; i < CTL_MAX_LUNS; i++) {
2298			if (ct->ct_luns[i] < 0)
2299				continue;
2300			if (ct->ct_luns[i] != lun_id)
2301				continue;
2302			cfiscsi_target_unset_lun(ct, i);
2303			break;
2304		}
2305	}
2306	mtx_unlock(&softc->lock);
2307	return (0);
2308}
2309
2310static void
2311cfiscsi_datamove_in(union ctl_io *io)
2312{
2313	struct cfiscsi_session *cs;
2314	struct icl_pdu *request, *response;
2315	const struct iscsi_bhs_scsi_command *bhssc;
2316	struct iscsi_bhs_data_in *bhsdi;
2317	struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
2318	size_t copy_len, len, off;
2319	const char *addr;
2320	int ctl_sg_count, error, i;
2321
2322	request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2323	cs = PDU_SESSION(request);
2324
2325	bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
2326	KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2327	    ISCSI_BHS_OPCODE_SCSI_COMMAND,
2328	    ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND"));
2329
2330	if (io->scsiio.kern_sg_entries > 0) {
2331		ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
2332		ctl_sg_count = io->scsiio.kern_sg_entries;
2333	} else {
2334		ctl_sglist = &ctl_sg_entry;
2335		ctl_sglist->addr = io->scsiio.kern_data_ptr;
2336		ctl_sglist->len = io->scsiio.kern_data_len;
2337		ctl_sg_count = 1;
2338	}
2339
2340	/*
2341	 * We need to record it so that we can properly report
2342	 * underflow/underflow.
2343	 */
2344	PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len;
2345
2346	/*
2347	 * This is the offset within the current SCSI command;
2348	 * i.e. for the first call of datamove(), it will be 0,
2349	 * and for subsequent ones it will be the sum of lengths
2350	 * of previous ones.
2351	 */
2352	off = htonl(io->scsiio.kern_rel_offset);
2353
2354	i = 0;
2355	addr = NULL;
2356	len = 0;
2357	response = NULL;
2358	bhsdi = NULL;
2359	for (;;) {
2360		KASSERT(i < ctl_sg_count, ("i >= ctl_sg_count"));
2361		if (response == NULL) {
2362			response = cfiscsi_pdu_new_response(request, M_NOWAIT);
2363			if (response == NULL) {
2364				CFISCSI_SESSION_WARN(cs, "failed to "
2365				    "allocate memory; dropping connection");
2366				ctl_set_busy(&io->scsiio);
2367				io->scsiio.be_move_done(io);
2368				cfiscsi_session_terminate(cs);
2369				return;
2370			}
2371			bhsdi = (struct iscsi_bhs_data_in *)response->ip_bhs;
2372			bhsdi->bhsdi_opcode = ISCSI_BHS_OPCODE_SCSI_DATA_IN;
2373			bhsdi->bhsdi_initiator_task_tag =
2374			    bhssc->bhssc_initiator_task_tag;
2375			bhsdi->bhsdi_datasn = htonl(PDU_EXPDATASN(request));
2376			PDU_EXPDATASN(request)++;
2377			bhsdi->bhsdi_buffer_offset = htonl(off);
2378		}
2379
2380		if (len == 0) {
2381			addr = ctl_sglist[i].addr;
2382			len = ctl_sglist[i].len;
2383			KASSERT(len > 0, ("len <= 0"));
2384		}
2385
2386		copy_len = len;
2387		if (response->ip_data_len + copy_len >
2388		    cs->cs_max_data_segment_length)
2389			copy_len = cs->cs_max_data_segment_length -
2390			    response->ip_data_len;
2391		KASSERT(copy_len <= len, ("copy_len > len"));
2392		error = icl_pdu_append_data(response, addr, copy_len, M_NOWAIT);
2393		if (error != 0) {
2394			CFISCSI_SESSION_WARN(cs, "failed to "
2395			    "allocate memory; dropping connection");
2396			icl_pdu_free(response);
2397			ctl_set_busy(&io->scsiio);
2398			io->scsiio.be_move_done(io);
2399			cfiscsi_session_terminate(cs);
2400			return;
2401		}
2402		addr += copy_len;
2403		len -= copy_len;
2404		off += copy_len;
2405		io->scsiio.ext_data_filled += copy_len;
2406
2407		if (len == 0) {
2408			/*
2409			 * End of scatter-gather segment;
2410			 * proceed to the next one...
2411			 */
2412			if (i == ctl_sg_count - 1) {
2413				/*
2414				 * ... unless this was the last one.
2415				 */
2416				break;
2417			}
2418			i++;
2419		}
2420
2421		if (response->ip_data_len == cs->cs_max_data_segment_length) {
2422			/*
2423			 * Can't stuff more data into the current PDU;
2424			 * queue it.  Note that's not enough to check
2425			 * for kern_data_resid == 0 instead; there
2426			 * may be several Data-In PDUs for the final
2427			 * call to cfiscsi_datamove(), and we want
2428			 * to set the F flag only on the last of them.
2429			 */
2430			if (off == io->scsiio.kern_total_len)
2431				bhsdi->bhsdi_flags |= BHSDI_FLAGS_F;
2432			KASSERT(response->ip_data_len > 0,
2433			    ("sending empty Data-In"));
2434			cfiscsi_pdu_queue(response);
2435			response = NULL;
2436			bhsdi = NULL;
2437		}
2438	}
2439	KASSERT(i == ctl_sg_count - 1, ("missed SG segment"));
2440	KASSERT(len == 0, ("missed data from SG segment"));
2441	if (response != NULL) {
2442		if (off == io->scsiio.kern_total_len) {
2443			bhsdi->bhsdi_flags |= BHSDI_FLAGS_F;
2444#if 0
2445		} else {
2446			CFISCSI_SESSION_DEBUG(cs, "not setting the F flag; "
2447			    "have %zd, need %zd", off,
2448			    (size_t)io->scsiio.kern_total_len);
2449#endif
2450		}
2451		KASSERT(response->ip_data_len > 0, ("sending empty Data-In"));
2452		cfiscsi_pdu_queue(response);
2453	}
2454
2455	io->scsiio.be_move_done(io);
2456}
2457
2458static void
2459cfiscsi_datamove_out(union ctl_io *io)
2460{
2461	struct cfiscsi_session *cs;
2462	struct icl_pdu *request, *response;
2463	const struct iscsi_bhs_scsi_command *bhssc;
2464	struct iscsi_bhs_r2t *bhsr2t;
2465	struct cfiscsi_data_wait *cdw;
2466	uint32_t target_transfer_tag;
2467	bool done;
2468
2469	request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2470	cs = PDU_SESSION(request);
2471
2472	bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
2473	KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2474	    ISCSI_BHS_OPCODE_SCSI_COMMAND,
2475	    ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND"));
2476
2477	/*
2478	 * We need to record it so that we can properly report
2479	 * underflow/underflow.
2480	 */
2481	PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len;
2482
2483	CFISCSI_SESSION_LOCK(cs);
2484	target_transfer_tag = cs->cs_target_transfer_tag;
2485	cs->cs_target_transfer_tag++;
2486	CFISCSI_SESSION_UNLOCK(cs);
2487
2488#if 0
2489	CFISCSI_SESSION_DEBUG(cs, "expecting Data-Out with initiator "
2490	    "task tag 0x%x, target transfer tag 0x%x",
2491	    bhssc->bhssc_initiator_task_tag, target_transfer_tag);
2492#endif
2493	cdw = uma_zalloc(cfiscsi_data_wait_zone, M_NOWAIT | M_ZERO);
2494	if (cdw == NULL) {
2495		CFISCSI_SESSION_WARN(cs, "failed to "
2496		    "allocate memory; dropping connection");
2497		ctl_set_busy(&io->scsiio);
2498		io->scsiio.be_move_done(io);
2499		cfiscsi_session_terminate(cs);
2500		return;
2501	}
2502	cdw->cdw_ctl_io = io;
2503	cdw->cdw_target_transfer_tag = htonl(target_transfer_tag);
2504	cdw->cdw_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2505
2506	if (cs->cs_immediate_data && icl_pdu_data_segment_length(request) > 0) {
2507		done = cfiscsi_handle_data_segment(request, cdw);
2508		if (done) {
2509			uma_zfree(cfiscsi_data_wait_zone, cdw);
2510			io->scsiio.be_move_done(io);
2511			return;
2512		}
2513
2514#if 0
2515		if (io->scsiio.ext_data_filled != 0)
2516			CFISCSI_SESSION_DEBUG(cs, "got %zd bytes of immediate data, need %zd",
2517			    io->scsiio.ext_data_filled, io->scsiio.kern_data_len);
2518#endif
2519	}
2520
2521	CFISCSI_SESSION_LOCK(cs);
2522	TAILQ_INSERT_TAIL(&cs->cs_waiting_for_data_out, cdw, cdw_next);
2523	CFISCSI_SESSION_UNLOCK(cs);
2524
2525	/*
2526	 * XXX: We should limit the number of outstanding R2T PDUs
2527	 * 	per task to MaxOutstandingR2T.
2528	 */
2529	response = cfiscsi_pdu_new_response(request, M_NOWAIT);
2530	if (response == NULL) {
2531		CFISCSI_SESSION_WARN(cs, "failed to "
2532		    "allocate memory; dropping connection");
2533		ctl_set_busy(&io->scsiio);
2534		io->scsiio.be_move_done(io);
2535		cfiscsi_session_terminate(cs);
2536		return;
2537	}
2538	bhsr2t = (struct iscsi_bhs_r2t *)response->ip_bhs;
2539	bhsr2t->bhsr2t_opcode = ISCSI_BHS_OPCODE_R2T;
2540	bhsr2t->bhsr2t_flags = 0x80;
2541	bhsr2t->bhsr2t_lun = bhssc->bhssc_lun;
2542	bhsr2t->bhsr2t_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2543	bhsr2t->bhsr2t_target_transfer_tag = htonl(target_transfer_tag);
2544	/*
2545	 * XXX: Here we assume that cfiscsi_datamove() won't ever
2546	 *	be running concurrently on several CPUs for a given
2547	 *	command.
2548	 */
2549	bhsr2t->bhsr2t_r2tsn = htonl(PDU_R2TSN(request));
2550	PDU_R2TSN(request)++;
2551	/*
2552	 * This is the offset within the current SCSI command;
2553	 * i.e. for the first call of datamove(), it will be 0,
2554	 * and for subsequent ones it will be the sum of lengths
2555	 * of previous ones.
2556	 *
2557	 * The ext_data_filled is to account for unsolicited
2558	 * (immediate) data that might have already arrived.
2559	 */
2560	bhsr2t->bhsr2t_buffer_offset =
2561	    htonl(io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled);
2562	/*
2563	 * This is the total length (sum of S/G lengths) this call
2564	 * to cfiscsi_datamove() is supposed to handle.
2565	 *
2566	 * XXX: Limit it to MaxBurstLength.
2567	 */
2568	bhsr2t->bhsr2t_desired_data_transfer_length =
2569	    htonl(io->scsiio.kern_data_len - io->scsiio.ext_data_filled);
2570	cfiscsi_pdu_queue(response);
2571}
2572
2573static void
2574cfiscsi_datamove(union ctl_io *io)
2575{
2576
2577	if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
2578		cfiscsi_datamove_in(io);
2579	else
2580		cfiscsi_datamove_out(io);
2581}
2582
2583static void
2584cfiscsi_scsi_command_done(union ctl_io *io)
2585{
2586	struct icl_pdu *request, *response;
2587	struct iscsi_bhs_scsi_command *bhssc;
2588	struct iscsi_bhs_scsi_response *bhssr;
2589#ifdef DIAGNOSTIC
2590	struct cfiscsi_data_wait *cdw;
2591#endif
2592	struct cfiscsi_session *cs;
2593	uint16_t sense_length;
2594
2595	request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2596	cs = PDU_SESSION(request);
2597	bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
2598	KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2599	    ISCSI_BHS_OPCODE_SCSI_COMMAND,
2600	    ("replying to wrong opcode 0x%x", bhssc->bhssc_opcode));
2601
2602	//CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x",
2603	//    bhssc->bhssc_initiator_task_tag);
2604
2605#ifdef DIAGNOSTIC
2606	CFISCSI_SESSION_LOCK(cs);
2607	TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next)
2608		KASSERT(bhssc->bhssc_initiator_task_tag !=
2609		    cdw->cdw_initiator_task_tag, ("dangling cdw"));
2610	CFISCSI_SESSION_UNLOCK(cs);
2611#endif
2612
2613	response = cfiscsi_pdu_new_response(request, M_WAITOK);
2614	bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
2615	bhssr->bhssr_opcode = ISCSI_BHS_OPCODE_SCSI_RESPONSE;
2616	bhssr->bhssr_flags = 0x80;
2617	/*
2618	 * XXX: We don't deal with bidirectional under/overflows;
2619	 *	does anything actually support those?
2620	 */
2621	if (PDU_TOTAL_TRANSFER_LEN(request) <
2622	    ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2623		bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_UNDERFLOW;
2624		bhssr->bhssr_residual_count =
2625		    htonl(ntohl(bhssc->bhssc_expected_data_transfer_length) -
2626		    PDU_TOTAL_TRANSFER_LEN(request));
2627		//CFISCSI_SESSION_DEBUG(cs, "underflow; residual count %d",
2628		//    ntohl(bhssr->bhssr_residual_count));
2629	} else if (PDU_TOTAL_TRANSFER_LEN(request) >
2630	    ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2631		bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_OVERFLOW;
2632		bhssr->bhssr_residual_count =
2633		    htonl(PDU_TOTAL_TRANSFER_LEN(request) -
2634		    ntohl(bhssc->bhssc_expected_data_transfer_length));
2635		//CFISCSI_SESSION_DEBUG(cs, "overflow; residual count %d",
2636		//    ntohl(bhssr->bhssr_residual_count));
2637	}
2638	bhssr->bhssr_response = BHSSR_RESPONSE_COMMAND_COMPLETED;
2639	bhssr->bhssr_status = io->scsiio.scsi_status;
2640	bhssr->bhssr_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2641	bhssr->bhssr_expdatasn = htonl(PDU_EXPDATASN(request));
2642
2643	if (io->scsiio.sense_len > 0) {
2644#if 0
2645		CFISCSI_SESSION_DEBUG(cs, "returning %d bytes of sense data",
2646		    io->scsiio.sense_len);
2647#endif
2648		sense_length = htons(io->scsiio.sense_len);
2649		icl_pdu_append_data(response,
2650		    &sense_length, sizeof(sense_length), M_WAITOK);
2651		icl_pdu_append_data(response,
2652		    &io->scsiio.sense_data, io->scsiio.sense_len, M_WAITOK);
2653	}
2654
2655	ctl_free_io(io);
2656	icl_pdu_free(request);
2657	cfiscsi_pdu_queue(response);
2658}
2659
2660static void
2661cfiscsi_task_management_done(union ctl_io *io)
2662{
2663	struct icl_pdu *request, *response;
2664	struct iscsi_bhs_task_management_request *bhstmr;
2665	struct iscsi_bhs_task_management_response *bhstmr2;
2666	struct cfiscsi_data_wait *cdw, *tmpcdw;
2667	struct cfiscsi_session *cs;
2668
2669	request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2670	cs = PDU_SESSION(request);
2671	bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
2672	KASSERT((bhstmr->bhstmr_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2673	    ISCSI_BHS_OPCODE_TASK_REQUEST,
2674	    ("replying to wrong opcode 0x%x", bhstmr->bhstmr_opcode));
2675
2676#if 0
2677	CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x; referenced task tag 0x%x",
2678	    bhstmr->bhstmr_initiator_task_tag,
2679	    bhstmr->bhstmr_referenced_task_tag);
2680#endif
2681
2682	if ((bhstmr->bhstmr_function & ~0x80) ==
2683	    BHSTMR_FUNCTION_ABORT_TASK) {
2684		/*
2685		 * Make sure we no longer wait for Data-Out for this command.
2686		 */
2687		CFISCSI_SESSION_LOCK(cs);
2688		TAILQ_FOREACH_SAFE(cdw,
2689		    &cs->cs_waiting_for_data_out, cdw_next, tmpcdw) {
2690			if (bhstmr->bhstmr_referenced_task_tag !=
2691			    cdw->cdw_initiator_task_tag)
2692				continue;
2693
2694#if 0
2695			CFISCSI_SESSION_DEBUG(cs, "removing csw for initiator task "
2696			    "tag 0x%x", bhstmr->bhstmr_initiator_task_tag);
2697#endif
2698			TAILQ_REMOVE(&cs->cs_waiting_for_data_out,
2699			    cdw, cdw_next);
2700			cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io);
2701			uma_zfree(cfiscsi_data_wait_zone, cdw);
2702		}
2703		CFISCSI_SESSION_UNLOCK(cs);
2704	}
2705
2706	response = cfiscsi_pdu_new_response(request, M_WAITOK);
2707	bhstmr2 = (struct iscsi_bhs_task_management_response *)
2708	    response->ip_bhs;
2709	bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE;
2710	bhstmr2->bhstmr_flags = 0x80;
2711	if (io->io_hdr.status == CTL_SUCCESS) {
2712		bhstmr2->bhstmr_response = BHSTMR_RESPONSE_FUNCTION_COMPLETE;
2713	} else {
2714		/*
2715		 * XXX: How to figure out what exactly went wrong?  iSCSI spec
2716		 * 	expects us to provide detailed error, e.g. "Task does
2717		 * 	not exist" or "LUN does not exist".
2718		 */
2719		CFISCSI_SESSION_DEBUG(cs, "BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED");
2720		bhstmr2->bhstmr_response =
2721		    BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED;
2722	}
2723	bhstmr2->bhstmr_initiator_task_tag = bhstmr->bhstmr_initiator_task_tag;
2724
2725	ctl_free_io(io);
2726	icl_pdu_free(request);
2727	cfiscsi_pdu_queue(response);
2728}
2729
2730static void
2731cfiscsi_done(union ctl_io *io)
2732{
2733	struct icl_pdu *request;
2734	struct cfiscsi_session *cs;
2735
2736	KASSERT(((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE),
2737		("invalid CTL status %#x", io->io_hdr.status));
2738
2739	request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2740	if (request == NULL) {
2741		/*
2742		 * Implicit task termination has just completed; nothing to do.
2743		 */
2744		return;
2745	}
2746
2747	cs = PDU_SESSION(request);
2748	refcount_release(&cs->cs_outstanding_ctl_pdus);
2749
2750	switch (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) {
2751	case ISCSI_BHS_OPCODE_SCSI_COMMAND:
2752		cfiscsi_scsi_command_done(io);
2753		break;
2754	case ISCSI_BHS_OPCODE_TASK_REQUEST:
2755		cfiscsi_task_management_done(io);
2756		break;
2757	default:
2758		panic("cfiscsi_done called with wrong opcode 0x%x",
2759		    request->ip_bhs->bhs_opcode);
2760	}
2761}
2762