ctl_frontend_iscsi.c revision 264110
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 264110 2014-04-04 08:48:55Z 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 264110 2014-04-04 08:48:55Z 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_RWTUN,
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_RWTUN,
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_RWTUN,
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_RWTUN,
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	atomic_add_int(&cs->cs_timeout, 1);
971
972#ifdef ICL_KERNEL_PROXY
973	if (cs->cs_waiting_for_ctld || cs->cs_login_phase) {
974		if (cs->cs_timeout > login_timeout) {
975			CFISCSI_SESSION_WARN(cs, "login timed out after "
976			    "%d seconds; dropping connection", cs->cs_timeout);
977			cfiscsi_session_terminate(cs);
978		}
979		return;
980	}
981#endif
982
983	if (cs->cs_timeout >= ping_timeout) {
984		CFISCSI_SESSION_WARN(cs, "no ping reply (NOP-Out) after %d seconds; "
985		    "dropping connection",  ping_timeout);
986		cfiscsi_session_terminate(cs);
987		return;
988	}
989
990	/*
991	 * If the ping was reset less than one second ago - which means
992	 * that we've received some PDU during the last second - assume
993	 * the traffic flows correctly and don't bother sending a NOP-Out.
994	 *
995	 * (It's 2 - one for one second, and one for incrementing is_timeout
996	 * earlier in this routine.)
997	 */
998	if (cs->cs_timeout < 2)
999		return;
1000
1001	cp = icl_pdu_new_bhs(cs->cs_conn, M_NOWAIT);
1002	if (cp == NULL) {
1003		CFISCSI_SESSION_WARN(cs, "failed to allocate memory");
1004		return;
1005	}
1006	bhsni = (struct iscsi_bhs_nop_in *)cp->ip_bhs;
1007	bhsni->bhsni_opcode = ISCSI_BHS_OPCODE_NOP_IN;
1008	bhsni->bhsni_flags = 0x80;
1009	bhsni->bhsni_initiator_task_tag = 0xffffffff;
1010
1011	cfiscsi_pdu_queue(cp);
1012}
1013
1014static void
1015cfiscsi_session_terminate_tasks(struct cfiscsi_session *cs)
1016{
1017	struct cfiscsi_data_wait *cdw, *tmpcdw;
1018	union ctl_io *io;
1019	int error;
1020
1021#ifdef notyet
1022	io = ctl_alloc_io(cs->cs_target->ct_softc->fe.ctl_pool_ref);
1023	if (io == NULL) {
1024		CFISCSI_SESSION_WARN(cs, "can't allocate ctl_io");
1025		return;
1026	}
1027	ctl_zero_io(io);
1028	io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = NULL;
1029	io->io_hdr.io_type = CTL_IO_TASK;
1030	io->io_hdr.nexus.initid.id = cs->cs_ctl_initid;
1031	io->io_hdr.nexus.targ_port = cs->cs_target->ct_softc->fe.targ_port;
1032	io->io_hdr.nexus.targ_target.id = 0;
1033	io->io_hdr.nexus.targ_lun = lun;
1034	io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
1035	io->taskio.task_action = CTL_TASK_ABORT_TASK_SET;
1036	error = ctl_queue(io);
1037	if (error != CTL_RETVAL_COMPLETE) {
1038		CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d", error);
1039		ctl_free_io(io);
1040	}
1041#else
1042	/*
1043	 * CTL doesn't currently support CTL_TASK_ABORT_TASK_SET, so instead
1044	 * just iterate over tasks that are waiting for something - data - and
1045	 * terminate those.
1046	 */
1047	CFISCSI_SESSION_LOCK(cs);
1048	TAILQ_FOREACH_SAFE(cdw,
1049	    &cs->cs_waiting_for_data_out, cdw_next, tmpcdw) {
1050		io = ctl_alloc_io(cs->cs_target->ct_softc->fe.ctl_pool_ref);
1051		if (io == NULL) {
1052			CFISCSI_SESSION_WARN(cs, "can't allocate ctl_io");
1053			return;
1054		}
1055		ctl_zero_io(io);
1056		io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = NULL;
1057		io->io_hdr.io_type = CTL_IO_TASK;
1058		io->io_hdr.nexus.initid.id = cs->cs_ctl_initid;
1059		io->io_hdr.nexus.targ_port =
1060		    cs->cs_target->ct_softc->fe.targ_port;
1061		io->io_hdr.nexus.targ_target.id = 0;
1062		//io->io_hdr.nexus.targ_lun = lun; /* Not needed? */
1063		io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
1064		io->taskio.task_action = CTL_TASK_ABORT_TASK;
1065		io->taskio.tag_num = cdw->cdw_initiator_task_tag;
1066		error = ctl_queue(io);
1067		if (error != CTL_RETVAL_COMPLETE) {
1068			CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d", error);
1069			ctl_free_io(io);
1070			return;
1071		}
1072#if 0
1073		CFISCSI_SESSION_DEBUG(cs, "removing csw for initiator task tag "
1074		    "0x%x", cdw->cdw_initiator_task_tag);
1075#endif
1076		cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io);
1077		TAILQ_REMOVE(&cs->cs_waiting_for_data_out, cdw, cdw_next);
1078		uma_zfree(cfiscsi_data_wait_zone, cdw);
1079	}
1080	CFISCSI_SESSION_UNLOCK(cs);
1081#endif
1082}
1083
1084static void
1085cfiscsi_maintenance_thread(void *arg)
1086{
1087	struct cfiscsi_session *cs;
1088
1089	cs = arg;
1090
1091	for (;;) {
1092		CFISCSI_SESSION_LOCK(cs);
1093		if (cs->cs_terminating == false)
1094			cv_wait(&cs->cs_maintenance_cv, &cs->cs_lock);
1095		CFISCSI_SESSION_UNLOCK(cs);
1096
1097		if (cs->cs_terminating) {
1098			cfiscsi_session_terminate_tasks(cs);
1099			callout_drain(&cs->cs_callout);
1100
1101			icl_conn_shutdown(cs->cs_conn);
1102			icl_conn_close(cs->cs_conn);
1103
1104			cs->cs_terminating++;
1105
1106			/*
1107			 * XXX: We used to wait up to 30 seconds to deliver queued PDUs
1108			 * 	to the initiator.  We also tried hard to deliver SCSI Responses
1109			 * 	for the aborted PDUs.  We don't do that anymore.  We might need
1110			 * 	to revisit that.
1111			 */
1112
1113			cfiscsi_session_delete(cs);
1114			kthread_exit();
1115			return;
1116		}
1117		CFISCSI_SESSION_DEBUG(cs, "nothing to do");
1118	}
1119}
1120
1121static void
1122cfiscsi_session_terminate(struct cfiscsi_session *cs)
1123{
1124
1125	if (cs->cs_terminating != 0)
1126		return;
1127	cs->cs_terminating = 1;
1128	cv_signal(&cs->cs_maintenance_cv);
1129}
1130
1131static int
1132cfiscsi_session_register_initiator(struct cfiscsi_session *cs)
1133{
1134	int error, i;
1135	struct cfiscsi_softc *softc;
1136
1137	KASSERT(cs->cs_ctl_initid == -1, ("already registered"));
1138
1139	softc = &cfiscsi_softc;
1140
1141	mtx_lock(&softc->lock);
1142	for (i = 0; i < softc->max_initiators; i++) {
1143		if (softc->ctl_initids[i] == 0)
1144			break;
1145	}
1146	if (i == softc->max_initiators) {
1147		CFISCSI_SESSION_WARN(cs, "too many concurrent sessions (%d)",
1148		    softc->max_initiators);
1149		mtx_unlock(&softc->lock);
1150		return (1);
1151	}
1152	softc->ctl_initids[i] = 1;
1153	mtx_unlock(&softc->lock);
1154
1155#if 0
1156	CFISCSI_SESSION_DEBUG(cs, "adding initiator id %d, max %d",
1157	    i, softc->max_initiators);
1158#endif
1159	cs->cs_ctl_initid = i;
1160	error = ctl_add_initiator(0x0, softc->fe.targ_port, cs->cs_ctl_initid);
1161	if (error != 0) {
1162		CFISCSI_SESSION_WARN(cs, "ctl_add_initiator failed with error %d", error);
1163		mtx_lock(&softc->lock);
1164		softc->ctl_initids[cs->cs_ctl_initid] = 0;
1165		mtx_unlock(&softc->lock);
1166		cs->cs_ctl_initid = -1;
1167		return (1);
1168	}
1169
1170	return (0);
1171}
1172
1173static void
1174cfiscsi_session_unregister_initiator(struct cfiscsi_session *cs)
1175{
1176	int error;
1177	struct cfiscsi_softc *softc;
1178
1179	if (cs->cs_ctl_initid == -1)
1180		return;
1181
1182	softc = &cfiscsi_softc;
1183
1184	error = ctl_remove_initiator(softc->fe.targ_port, cs->cs_ctl_initid);
1185	if (error != 0) {
1186		CFISCSI_SESSION_WARN(cs, "ctl_remove_initiator failed with error %d",
1187		    error);
1188	}
1189	mtx_lock(&softc->lock);
1190	softc->ctl_initids[cs->cs_ctl_initid] = 0;
1191	mtx_unlock(&softc->lock);
1192	cs->cs_ctl_initid = -1;
1193}
1194
1195static struct cfiscsi_session *
1196cfiscsi_session_new(struct cfiscsi_softc *softc)
1197{
1198	struct cfiscsi_session *cs;
1199	int error;
1200
1201	cs = malloc(sizeof(*cs), M_CFISCSI, M_NOWAIT | M_ZERO);
1202	if (cs == NULL) {
1203		CFISCSI_WARN("malloc failed");
1204		return (NULL);
1205	}
1206	cs->cs_ctl_initid = -1;
1207
1208	refcount_init(&cs->cs_outstanding_ctl_pdus, 0);
1209	TAILQ_INIT(&cs->cs_waiting_for_data_out);
1210	mtx_init(&cs->cs_lock, "cfiscsi_lock", NULL, MTX_DEF);
1211	cv_init(&cs->cs_maintenance_cv, "cfiscsi_mt");
1212#ifdef ICL_KERNEL_PROXY
1213	cv_init(&cs->cs_login_cv, "cfiscsi_login");
1214#endif
1215
1216	cs->cs_conn = icl_conn_new("cfiscsi", &cs->cs_lock);
1217	cs->cs_conn->ic_receive = cfiscsi_receive_callback;
1218	cs->cs_conn->ic_error = cfiscsi_error_callback;
1219	cs->cs_conn->ic_prv0 = cs;
1220
1221	error = kthread_add(cfiscsi_maintenance_thread, cs, NULL, NULL, 0, 0, "cfiscsimt");
1222	if (error != 0) {
1223		CFISCSI_SESSION_WARN(cs, "kthread_add(9) failed with error %d", error);
1224		free(cs, M_CFISCSI);
1225		return (NULL);
1226	}
1227
1228	mtx_lock(&softc->lock);
1229	cs->cs_id = softc->last_session_id + 1;
1230	softc->last_session_id++;
1231	mtx_unlock(&softc->lock);
1232
1233	mtx_lock(&softc->lock);
1234	TAILQ_INSERT_TAIL(&softc->sessions, cs, cs_next);
1235	mtx_unlock(&softc->lock);
1236
1237	/*
1238	 * Start pinging the initiator.
1239	 */
1240	callout_init(&cs->cs_callout, 1);
1241	callout_reset(&cs->cs_callout, 1 * hz, cfiscsi_callout, cs);
1242
1243	return (cs);
1244}
1245
1246static void
1247cfiscsi_session_delete(struct cfiscsi_session *cs)
1248{
1249	struct cfiscsi_softc *softc;
1250
1251	softc = &cfiscsi_softc;
1252
1253	KASSERT(cs->cs_outstanding_ctl_pdus == 0,
1254	    ("destroying session with outstanding CTL pdus"));
1255	KASSERT(TAILQ_EMPTY(&cs->cs_waiting_for_data_out),
1256	    ("destroying session with non-empty queue"));
1257
1258	cfiscsi_session_unregister_initiator(cs);
1259	if (cs->cs_target != NULL)
1260		cfiscsi_target_release(cs->cs_target);
1261	icl_conn_close(cs->cs_conn);
1262	icl_conn_free(cs->cs_conn);
1263
1264	mtx_lock(&softc->lock);
1265	TAILQ_REMOVE(&softc->sessions, cs, cs_next);
1266	mtx_unlock(&softc->lock);
1267
1268	free(cs, M_CFISCSI);
1269}
1270
1271int
1272cfiscsi_init(void)
1273{
1274	struct cfiscsi_softc *softc;
1275	struct ctl_frontend *fe;
1276	int retval;
1277
1278	softc = &cfiscsi_softc;
1279	retval = 0;
1280	bzero(softc, sizeof(*softc));
1281	mtx_init(&softc->lock, "cfiscsi", NULL, MTX_DEF);
1282
1283#ifdef ICL_KERNEL_PROXY
1284	cv_init(&softc->accept_cv, "cfiscsi_accept");
1285#endif
1286	TAILQ_INIT(&softc->sessions);
1287	TAILQ_INIT(&softc->targets);
1288
1289	fe = &softc->fe;
1290	fe->port_type = CTL_PORT_ISCSI;
1291	/* XXX KDM what should the real number be here? */
1292	fe->num_requested_ctl_io = 4096;
1293	snprintf(softc->port_name, sizeof(softc->port_name), "iscsi");
1294	fe->port_name = softc->port_name;
1295	fe->port_online = cfiscsi_online;
1296	fe->port_offline = cfiscsi_offline;
1297	fe->onoff_arg = softc;
1298	fe->targ_enable = cfiscsi_targ_enable;
1299	fe->targ_disable = cfiscsi_targ_disable;
1300	fe->lun_enable = cfiscsi_lun_enable;
1301	fe->lun_disable = cfiscsi_lun_disable;
1302	fe->targ_lun_arg = softc;
1303	fe->ioctl = cfiscsi_ioctl;
1304	fe->devid = cfiscsi_devid;
1305	fe->fe_datamove = cfiscsi_datamove;
1306	fe->fe_done = cfiscsi_done;
1307
1308	/* XXX KDM what should we report here? */
1309	/* XXX These should probably be fetched from CTL. */
1310	fe->max_targets = 1;
1311	fe->max_target_id = 15;
1312
1313	retval = ctl_frontend_register(fe, /*master_SC*/ 1);
1314	if (retval != 0) {
1315		CFISCSI_WARN("ctl_frontend_register() failed with error %d",
1316		    retval);
1317		retval = 1;
1318		goto bailout;
1319	}
1320
1321	softc->max_initiators = fe->max_initiators;
1322
1323	cfiscsi_data_wait_zone = uma_zcreate("cfiscsi_data_wait",
1324	    sizeof(struct cfiscsi_data_wait), NULL, NULL, NULL, NULL,
1325	    UMA_ALIGN_PTR, 0);
1326
1327	return (0);
1328
1329bailout:
1330	return (retval);
1331}
1332
1333static int
1334cfiscsi_module_event_handler(module_t mod, int what, void *arg)
1335{
1336
1337	switch (what) {
1338	case MOD_LOAD:
1339		return (cfiscsi_init());
1340	case MOD_UNLOAD:
1341		return (EBUSY);
1342	default:
1343		return (EOPNOTSUPP);
1344	}
1345}
1346
1347#ifdef ICL_KERNEL_PROXY
1348static void
1349cfiscsi_accept(struct socket *so)
1350{
1351	struct cfiscsi_session *cs;
1352
1353	cs = cfiscsi_session_new(&cfiscsi_softc);
1354	if (cs == NULL) {
1355		CFISCSI_WARN("failed to create session");
1356		return;
1357	}
1358
1359	icl_conn_handoff_sock(cs->cs_conn, so);
1360	cs->cs_waiting_for_ctld = true;
1361	cv_signal(&cfiscsi_softc.accept_cv);
1362}
1363#endif
1364
1365static void
1366cfiscsi_online(void *arg)
1367{
1368	struct cfiscsi_softc *softc;
1369
1370	softc = (struct cfiscsi_softc *)arg;
1371
1372	softc->online = 1;
1373#ifdef ICL_KERNEL_PROXY
1374	if (softc->listener != NULL)
1375		icl_listen_free(softc->listener);
1376	softc->listener = icl_listen_new(cfiscsi_accept);
1377#endif
1378}
1379
1380static void
1381cfiscsi_offline(void *arg)
1382{
1383	struct cfiscsi_softc *softc;
1384	struct cfiscsi_session *cs;
1385
1386	softc = (struct cfiscsi_softc *)arg;
1387
1388	softc->online = 0;
1389
1390	mtx_lock(&softc->lock);
1391	TAILQ_FOREACH(cs, &softc->sessions, cs_next)
1392		cfiscsi_session_terminate(cs);
1393	mtx_unlock(&softc->lock);
1394
1395#ifdef ICL_KERNEL_PROXY
1396	icl_listen_free(softc->listener);
1397	softc->listener = NULL;
1398#endif
1399}
1400
1401static int
1402cfiscsi_targ_enable(void *arg, struct ctl_id targ_id)
1403{
1404
1405	return (0);
1406}
1407
1408static int
1409cfiscsi_targ_disable(void *arg, struct ctl_id targ_id)
1410{
1411
1412	return (0);
1413}
1414
1415static void
1416cfiscsi_ioctl_handoff(struct ctl_iscsi *ci)
1417{
1418	struct cfiscsi_softc *softc;
1419	struct cfiscsi_session *cs;
1420	struct cfiscsi_target *ct;
1421	struct ctl_iscsi_handoff_params *cihp;
1422#ifndef ICL_KERNEL_PROXY
1423	int error;
1424#endif
1425
1426	cihp = (struct ctl_iscsi_handoff_params *)&(ci->data);
1427	softc = &cfiscsi_softc;
1428
1429	CFISCSI_DEBUG("new connection from %s (%s) to %s",
1430	    cihp->initiator_name, cihp->initiator_addr,
1431	    cihp->target_name);
1432
1433	if (softc->online == 0) {
1434		ci->status = CTL_ISCSI_ERROR;
1435		snprintf(ci->error_str, sizeof(ci->error_str),
1436		    "%s: port offline", __func__);
1437		return;
1438	}
1439
1440	ct = cfiscsi_target_find(softc, cihp->target_name);
1441	if (ct == NULL) {
1442		ci->status = CTL_ISCSI_ERROR;
1443		snprintf(ci->error_str, sizeof(ci->error_str),
1444		    "%s: target not found", __func__);
1445		return;
1446	}
1447
1448#ifdef ICL_KERNEL_PROXY
1449	mtx_lock(&cfiscsi_softc.lock);
1450	TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1451		if (cs->cs_id == cihp->socket)
1452			break;
1453	}
1454	if (cs == NULL) {
1455		mtx_unlock(&cfiscsi_softc.lock);
1456		snprintf(ci->error_str, sizeof(ci->error_str), "connection not found");
1457		ci->status = CTL_ISCSI_ERROR;
1458		return;
1459	}
1460	mtx_unlock(&cfiscsi_softc.lock);
1461#else
1462	cs = cfiscsi_session_new(softc);
1463	if (cs == NULL) {
1464		ci->status = CTL_ISCSI_ERROR;
1465		snprintf(ci->error_str, sizeof(ci->error_str),
1466		    "%s: cfiscsi_session_new failed", __func__);
1467		cfiscsi_target_release(ct);
1468		return;
1469	}
1470#endif
1471	cs->cs_target = ct;
1472
1473	/*
1474	 * First PDU of Full Feature phase has the same CmdSN as the last
1475	 * PDU from the Login Phase received from the initiator.  Thus,
1476	 * the -1 below.
1477	 */
1478	cs->cs_portal_group_tag = cihp->portal_group_tag;
1479	cs->cs_cmdsn = cihp->cmdsn;
1480	cs->cs_statsn = cihp->statsn;
1481	cs->cs_max_data_segment_length = cihp->max_recv_data_segment_length;
1482	cs->cs_max_burst_length = cihp->max_burst_length;
1483	cs->cs_immediate_data = !!cihp->immediate_data;
1484	if (cihp->header_digest == CTL_ISCSI_DIGEST_CRC32C)
1485		cs->cs_conn->ic_header_crc32c = true;
1486	if (cihp->data_digest == CTL_ISCSI_DIGEST_CRC32C)
1487		cs->cs_conn->ic_data_crc32c = true;
1488
1489	strlcpy(cs->cs_initiator_name,
1490	    cihp->initiator_name, sizeof(cs->cs_initiator_name));
1491	strlcpy(cs->cs_initiator_addr,
1492	    cihp->initiator_addr, sizeof(cs->cs_initiator_addr));
1493	strlcpy(cs->cs_initiator_alias,
1494	    cihp->initiator_alias, sizeof(cs->cs_initiator_alias));
1495
1496#ifdef ICL_KERNEL_PROXY
1497	cs->cs_login_phase = false;
1498#else
1499	error = icl_conn_handoff(cs->cs_conn, cihp->socket);
1500	if (error != 0) {
1501		cfiscsi_session_delete(cs);
1502		ci->status = CTL_ISCSI_ERROR;
1503		snprintf(ci->error_str, sizeof(ci->error_str),
1504		    "%s: icl_conn_handoff failed with error %d",
1505		    __func__, error);
1506		return;
1507	}
1508#endif
1509
1510	/*
1511	 * Register initiator with CTL.
1512	 */
1513	cfiscsi_session_register_initiator(cs);
1514
1515#ifdef ICL_KERNEL_PROXY
1516	/*
1517	 * First PDU of the Full Feature phase has likely already arrived.
1518	 * We have to pick it up and execute properly.
1519	 */
1520	if (cs->cs_login_pdu != NULL) {
1521		CFISCSI_SESSION_DEBUG(cs, "picking up first PDU");
1522		cfiscsi_pdu_handle(cs->cs_login_pdu);
1523		cs->cs_login_pdu = NULL;
1524	}
1525#endif
1526
1527	ci->status = CTL_ISCSI_OK;
1528}
1529
1530static void
1531cfiscsi_ioctl_list(struct ctl_iscsi *ci)
1532{
1533	struct ctl_iscsi_list_params *cilp;
1534	struct cfiscsi_session *cs;
1535	struct cfiscsi_softc *softc;
1536	struct sbuf *sb;
1537	int error;
1538
1539	cilp = (struct ctl_iscsi_list_params *)&(ci->data);
1540	softc = &cfiscsi_softc;
1541
1542	sb = sbuf_new(NULL, NULL, cilp->alloc_len, SBUF_FIXEDLEN);
1543	if (sb == NULL) {
1544		ci->status = CTL_ISCSI_ERROR;
1545		snprintf(ci->error_str, sizeof(ci->error_str),
1546		    "Unable to allocate %d bytes for iSCSI session list",
1547		    cilp->alloc_len);
1548		return;
1549	}
1550
1551	sbuf_printf(sb, "<ctlislist>\n");
1552	mtx_lock(&softc->lock);
1553	TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1554#ifdef ICL_KERNEL_PROXY
1555		if (cs->cs_target == NULL)
1556			continue;
1557#endif
1558		error = sbuf_printf(sb, "<connection id=\"%d\">"
1559		    "<initiator>%s</initiator>"
1560		    "<initiator_addr>%s</initiator_addr>"
1561		    "<initiator_alias>%s</initiator_alias>"
1562		    "<target>%s</target>"
1563		    "<target_alias>%s</target_alias>"
1564		    "<header_digest>%s</header_digest>"
1565		    "<data_digest>%s</data_digest>"
1566		    "<max_data_segment_length>%zd</max_data_segment_length>"
1567		    "<immediate_data>%d</immediate_data>"
1568		    "<iser>%d</iser>"
1569		    "</connection>\n",
1570		    cs->cs_id,
1571		    cs->cs_initiator_name, cs->cs_initiator_addr, cs->cs_initiator_alias,
1572		    cs->cs_target->ct_name, cs->cs_target->ct_alias,
1573		    cs->cs_conn->ic_header_crc32c ? "CRC32C" : "None",
1574		    cs->cs_conn->ic_data_crc32c ? "CRC32C" : "None",
1575		    cs->cs_max_data_segment_length,
1576		    cs->cs_immediate_data,
1577		    cs->cs_conn->ic_iser);
1578		if (error != 0)
1579			break;
1580	}
1581	mtx_unlock(&softc->lock);
1582	error = sbuf_printf(sb, "</ctlislist>\n");
1583	if (error != 0) {
1584		sbuf_delete(sb);
1585		ci->status = CTL_ISCSI_LIST_NEED_MORE_SPACE;
1586		snprintf(ci->error_str, sizeof(ci->error_str),
1587		    "Out of space, %d bytes is too small", cilp->alloc_len);
1588		return;
1589	}
1590	sbuf_finish(sb);
1591
1592	error = copyout(sbuf_data(sb), cilp->conn_xml, sbuf_len(sb) + 1);
1593	cilp->fill_len = sbuf_len(sb) + 1;
1594	ci->status = CTL_ISCSI_OK;
1595	sbuf_delete(sb);
1596}
1597
1598static void
1599cfiscsi_ioctl_terminate(struct ctl_iscsi *ci)
1600{
1601	struct icl_pdu *response;
1602	struct iscsi_bhs_asynchronous_message *bhsam;
1603	struct ctl_iscsi_terminate_params *citp;
1604	struct cfiscsi_session *cs;
1605	struct cfiscsi_softc *softc;
1606	int found = 0;
1607
1608	citp = (struct ctl_iscsi_terminate_params *)&(ci->data);
1609	softc = &cfiscsi_softc;
1610
1611	mtx_lock(&softc->lock);
1612	TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1613		if (citp->all == 0 && cs->cs_id != citp->connection_id &&
1614		    strcmp(cs->cs_initiator_name, citp->initiator_name) != 0 &&
1615		    strcmp(cs->cs_initiator_addr, citp->initiator_addr) != 0)
1616			continue;
1617
1618		response = icl_pdu_new_bhs(cs->cs_conn, M_NOWAIT);
1619		if (response == NULL) {
1620			/*
1621			 * Oh well.  Just terminate the connection.
1622			 */
1623		} else {
1624			bhsam = (struct iscsi_bhs_asynchronous_message *)
1625			    response->ip_bhs;
1626			bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE;
1627			bhsam->bhsam_flags = 0x80;
1628			bhsam->bhsam_0xffffffff = 0xffffffff;
1629			bhsam->bhsam_async_event =
1630			    BHSAM_EVENT_TARGET_TERMINATES_SESSION;
1631			cfiscsi_pdu_queue(response);
1632		}
1633		cfiscsi_session_terminate(cs);
1634		found++;
1635	}
1636	mtx_unlock(&softc->lock);
1637
1638	if (found == 0) {
1639		ci->status = CTL_ISCSI_SESSION_NOT_FOUND;
1640		snprintf(ci->error_str, sizeof(ci->error_str),
1641		    "No matching connections found");
1642		return;
1643	}
1644
1645	ci->status = CTL_ISCSI_OK;
1646}
1647
1648static void
1649cfiscsi_ioctl_logout(struct ctl_iscsi *ci)
1650{
1651	struct icl_pdu *response;
1652	struct iscsi_bhs_asynchronous_message *bhsam;
1653	struct ctl_iscsi_logout_params *cilp;
1654	struct cfiscsi_session *cs;
1655	struct cfiscsi_softc *softc;
1656	int found = 0;
1657
1658	cilp = (struct ctl_iscsi_logout_params *)&(ci->data);
1659	softc = &cfiscsi_softc;
1660
1661	mtx_lock(&softc->lock);
1662	TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1663		if (cilp->all == 0 && cs->cs_id != cilp->connection_id &&
1664		    strcmp(cs->cs_initiator_name, cilp->initiator_name) != 0 &&
1665		    strcmp(cs->cs_initiator_addr, cilp->initiator_addr) != 0)
1666			continue;
1667
1668		response = icl_pdu_new_bhs(cs->cs_conn, M_NOWAIT);
1669		if (response == NULL) {
1670			ci->status = CTL_ISCSI_ERROR;
1671			snprintf(ci->error_str, sizeof(ci->error_str),
1672			    "Unable to allocate memory");
1673			mtx_unlock(&softc->lock);
1674			return;
1675		}
1676		bhsam =
1677		    (struct iscsi_bhs_asynchronous_message *)response->ip_bhs;
1678		bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE;
1679		bhsam->bhsam_flags = 0x80;
1680		bhsam->bhsam_async_event = BHSAM_EVENT_TARGET_REQUESTS_LOGOUT;
1681		bhsam->bhsam_parameter3 = htons(10);
1682		cfiscsi_pdu_queue(response);
1683		found++;
1684	}
1685	mtx_unlock(&softc->lock);
1686
1687	if (found == 0) {
1688		ci->status = CTL_ISCSI_SESSION_NOT_FOUND;
1689		snprintf(ci->error_str, sizeof(ci->error_str),
1690		    "No matching connections found");
1691		return;
1692	}
1693
1694	ci->status = CTL_ISCSI_OK;
1695}
1696
1697#ifdef ICL_KERNEL_PROXY
1698static void
1699cfiscsi_ioctl_listen(struct ctl_iscsi *ci)
1700{
1701	struct ctl_iscsi_listen_params *cilp;
1702	struct sockaddr *sa;
1703	int error;
1704
1705	cilp = (struct ctl_iscsi_listen_params *)&(ci->data);
1706
1707	if (cfiscsi_softc.listener == NULL) {
1708		CFISCSI_DEBUG("no listener");
1709		snprintf(ci->error_str, sizeof(ci->error_str), "no listener");
1710		ci->status = CTL_ISCSI_ERROR;
1711		return;
1712	}
1713
1714	error = getsockaddr(&sa, (void *)cilp->addr, cilp->addrlen);
1715	if (error != 0) {
1716		CFISCSI_DEBUG("getsockaddr, error %d", error);
1717		snprintf(ci->error_str, sizeof(ci->error_str), "getsockaddr failed");
1718		ci->status = CTL_ISCSI_ERROR;
1719		return;
1720	}
1721
1722	error = icl_listen_add(cfiscsi_softc.listener, cilp->iser, cilp->domain,
1723	    cilp->socktype, cilp->protocol, sa);
1724	if (error != 0) {
1725		free(sa, M_SONAME);
1726		CFISCSI_DEBUG("icl_listen_add, error %d", error);
1727		snprintf(ci->error_str, sizeof(ci->error_str),
1728		    "icl_listen_add failed, error %d", error);
1729		ci->status = CTL_ISCSI_ERROR;
1730		return;
1731	}
1732
1733	ci->status = CTL_ISCSI_OK;
1734}
1735
1736static void
1737cfiscsi_ioctl_accept(struct ctl_iscsi *ci)
1738{
1739	struct ctl_iscsi_accept_params *ciap;
1740	struct cfiscsi_session *cs;
1741	int error;
1742
1743	ciap = (struct ctl_iscsi_accept_params *)&(ci->data);
1744
1745	mtx_lock(&cfiscsi_softc.lock);
1746	for (;;) {
1747		TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1748			if (cs->cs_waiting_for_ctld)
1749				break;
1750		}
1751		if (cs != NULL)
1752			break;
1753		error = cv_wait_sig(&cfiscsi_softc.accept_cv, &cfiscsi_softc.lock);
1754		if (error != 0) {
1755			mtx_unlock(&cfiscsi_softc.lock);
1756			snprintf(ci->error_str, sizeof(ci->error_str), "interrupted");
1757			ci->status = CTL_ISCSI_ERROR;
1758			return;
1759		}
1760	}
1761	mtx_unlock(&cfiscsi_softc.lock);
1762
1763	cs->cs_waiting_for_ctld = false;
1764	cs->cs_login_phase = true;
1765
1766	ciap->connection_id = cs->cs_id;
1767	ci->status = CTL_ISCSI_OK;
1768}
1769
1770static void
1771cfiscsi_ioctl_send(struct ctl_iscsi *ci)
1772{
1773	struct ctl_iscsi_send_params *cisp;
1774	struct cfiscsi_session *cs;
1775	struct icl_pdu *ip;
1776	size_t datalen;
1777	void *data;
1778	int error;
1779
1780	cisp = (struct ctl_iscsi_send_params *)&(ci->data);
1781
1782	mtx_lock(&cfiscsi_softc.lock);
1783	TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1784		if (cs->cs_id == cisp->connection_id)
1785			break;
1786	}
1787	if (cs == NULL) {
1788		mtx_unlock(&cfiscsi_softc.lock);
1789		snprintf(ci->error_str, sizeof(ci->error_str), "connection not found");
1790		ci->status = CTL_ISCSI_ERROR;
1791		return;
1792	}
1793	mtx_unlock(&cfiscsi_softc.lock);
1794
1795#if 0
1796	if (cs->cs_login_phase == false)
1797		return (EBUSY);
1798#endif
1799
1800	if (cs->cs_terminating) {
1801		snprintf(ci->error_str, sizeof(ci->error_str), "connection is terminating");
1802		ci->status = CTL_ISCSI_ERROR;
1803		return;
1804	}
1805
1806	datalen = cisp->data_segment_len;
1807	/*
1808	 * XXX
1809	 */
1810	//if (datalen > CFISCSI_MAX_DATA_SEGMENT_LENGTH) {
1811	if (datalen > 65535) {
1812		snprintf(ci->error_str, sizeof(ci->error_str), "data segment too big");
1813		ci->status = CTL_ISCSI_ERROR;
1814		return;
1815	}
1816	if (datalen > 0) {
1817		data = malloc(datalen, M_CFISCSI, M_WAITOK);
1818		error = copyin(cisp->data_segment, data, datalen);
1819		if (error != 0) {
1820			free(data, M_CFISCSI);
1821			snprintf(ci->error_str, sizeof(ci->error_str), "copyin error %d", error);
1822			ci->status = CTL_ISCSI_ERROR;
1823			return;
1824		}
1825	}
1826
1827	ip = icl_pdu_new_bhs(cs->cs_conn, M_WAITOK);
1828	memcpy(ip->ip_bhs, cisp->bhs, sizeof(*ip->ip_bhs));
1829	if (datalen > 0) {
1830		icl_pdu_append_data(ip, data, datalen, M_WAITOK);
1831		free(data, M_CFISCSI);
1832	}
1833	icl_pdu_queue(ip);
1834	ci->status = CTL_ISCSI_OK;
1835}
1836
1837static void
1838cfiscsi_ioctl_receive(struct ctl_iscsi *ci)
1839{
1840	struct ctl_iscsi_receive_params *cirp;
1841	struct cfiscsi_session *cs;
1842	struct icl_pdu *ip;
1843	void *data;
1844
1845	cirp = (struct ctl_iscsi_receive_params *)&(ci->data);
1846
1847	mtx_lock(&cfiscsi_softc.lock);
1848	TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1849		if (cs->cs_id == cirp->connection_id)
1850			break;
1851	}
1852	if (cs == NULL) {
1853		mtx_unlock(&cfiscsi_softc.lock);
1854		snprintf(ci->error_str, sizeof(ci->error_str), "connection not found");
1855		ci->status = CTL_ISCSI_ERROR;
1856		return;
1857	}
1858	mtx_unlock(&cfiscsi_softc.lock);
1859
1860#if 0
1861	if (is->is_login_phase == false)
1862		return (EBUSY);
1863#endif
1864
1865	CFISCSI_SESSION_LOCK(cs);
1866	while (cs->cs_login_pdu == NULL &&
1867	    cs->cs_terminating == false)
1868		cv_wait(&cs->cs_login_cv, &cs->cs_lock);
1869	if (cs->cs_terminating) {
1870		CFISCSI_SESSION_UNLOCK(cs);
1871		snprintf(ci->error_str, sizeof(ci->error_str), "connection terminating");
1872		ci->status = CTL_ISCSI_ERROR;
1873		return;
1874	}
1875	ip = cs->cs_login_pdu;
1876	cs->cs_login_pdu = NULL;
1877	CFISCSI_SESSION_UNLOCK(cs);
1878
1879	if (ip->ip_data_len > cirp->data_segment_len) {
1880		icl_pdu_free(ip);
1881		snprintf(ci->error_str, sizeof(ci->error_str), "data segment too big");
1882		ci->status = CTL_ISCSI_ERROR;
1883		return;
1884	}
1885
1886	copyout(ip->ip_bhs, cirp->bhs, sizeof(*ip->ip_bhs));
1887	if (ip->ip_data_len > 0) {
1888		data = malloc(ip->ip_data_len, M_CFISCSI, M_WAITOK);
1889		icl_pdu_get_data(ip, 0, data, ip->ip_data_len);
1890		copyout(data, cirp->data_segment, ip->ip_data_len);
1891		free(data, M_CFISCSI);
1892	}
1893
1894	icl_pdu_free(ip);
1895	ci->status = CTL_ISCSI_OK;
1896}
1897
1898static void
1899cfiscsi_ioctl_close(struct ctl_iscsi *ci)
1900{
1901	/*
1902	 * XXX
1903	 */
1904}
1905#endif /* !ICL_KERNEL_PROXY */
1906
1907static int
1908cfiscsi_ioctl(struct cdev *dev,
1909    u_long cmd, caddr_t addr, int flag, struct thread *td)
1910{
1911	struct ctl_iscsi *ci;
1912
1913	if (cmd != CTL_ISCSI)
1914		return (ENOTTY);
1915
1916	ci = (struct ctl_iscsi *)addr;
1917	switch (ci->type) {
1918	case CTL_ISCSI_HANDOFF:
1919		cfiscsi_ioctl_handoff(ci);
1920		break;
1921	case CTL_ISCSI_LIST:
1922		cfiscsi_ioctl_list(ci);
1923		break;
1924	case CTL_ISCSI_TERMINATE:
1925		cfiscsi_ioctl_terminate(ci);
1926		break;
1927	case CTL_ISCSI_LOGOUT:
1928		cfiscsi_ioctl_logout(ci);
1929		break;
1930#ifdef ICL_KERNEL_PROXY
1931	case CTL_ISCSI_LISTEN:
1932		cfiscsi_ioctl_listen(ci);
1933		break;
1934	case CTL_ISCSI_ACCEPT:
1935		cfiscsi_ioctl_accept(ci);
1936		break;
1937	case CTL_ISCSI_SEND:
1938		cfiscsi_ioctl_send(ci);
1939		break;
1940	case CTL_ISCSI_RECEIVE:
1941		cfiscsi_ioctl_receive(ci);
1942		break;
1943	case CTL_ISCSI_CLOSE:
1944		cfiscsi_ioctl_close(ci);
1945		break;
1946#endif /* ICL_KERNEL_PROXY */
1947	default:
1948		ci->status = CTL_ISCSI_ERROR;
1949		snprintf(ci->error_str, sizeof(ci->error_str),
1950		    "%s: invalid iSCSI request type %d", __func__, ci->type);
1951		break;
1952	}
1953
1954	return (0);
1955}
1956
1957static int
1958cfiscsi_devid(struct ctl_scsiio *ctsio, int alloc_len)
1959{
1960	struct cfiscsi_session *cs;
1961	struct scsi_vpd_device_id *devid_ptr;
1962	struct scsi_vpd_id_descriptor *desc, *desc1;
1963	struct scsi_vpd_id_descriptor *desc2, *desc3; /* for types 4h and 5h */
1964	struct scsi_vpd_id_t10 *t10id;
1965	struct ctl_lun *lun;
1966	const struct icl_pdu *request;
1967	size_t devid_len, wwpn_len;
1968
1969	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
1970	request = ctsio->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1971	cs = PDU_SESSION(request);
1972
1973	wwpn_len = strlen(cs->cs_target->ct_name);
1974	wwpn_len += strlen(",t,0x01");
1975	wwpn_len += 1; /* '\0' */
1976	if ((wwpn_len % 4) != 0)
1977		wwpn_len += (4 - (wwpn_len % 4));
1978
1979	devid_len = sizeof(struct scsi_vpd_device_id) +
1980		sizeof(struct scsi_vpd_id_descriptor) +
1981		sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN +
1982		sizeof(struct scsi_vpd_id_descriptor) + wwpn_len +
1983		sizeof(struct scsi_vpd_id_descriptor) +
1984		sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
1985		sizeof(struct scsi_vpd_id_descriptor) +
1986		sizeof(struct scsi_vpd_id_trgt_port_grp_id);
1987
1988	ctsio->kern_data_ptr = malloc(devid_len, M_CTL, M_WAITOK | M_ZERO);
1989	devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
1990	ctsio->kern_sg_entries = 0;
1991
1992	if (devid_len < alloc_len) {
1993		ctsio->residual = alloc_len - devid_len;
1994		ctsio->kern_data_len = devid_len;
1995		ctsio->kern_total_len = devid_len;
1996	} else {
1997		ctsio->residual = 0;
1998		ctsio->kern_data_len = alloc_len;
1999		ctsio->kern_total_len = alloc_len;
2000	}
2001	ctsio->kern_data_resid = 0;
2002	ctsio->kern_rel_offset = 0;
2003	ctsio->kern_sg_entries = 0;
2004
2005	desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
2006	t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
2007	desc1 = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
2008	    sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN);
2009	desc2 = (struct scsi_vpd_id_descriptor *)(&desc1->identifier[0] +
2010	    wwpn_len);
2011	desc3 = (struct scsi_vpd_id_descriptor *)(&desc2->identifier[0] +
2012	    sizeof(struct scsi_vpd_id_rel_trgt_port_id));
2013
2014	if (lun != NULL)
2015		devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
2016		    lun->be_lun->lun_type;
2017	else
2018		devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
2019
2020	devid_ptr->page_code = SVPD_DEVICE_ID;
2021
2022	scsi_ulto2b(devid_len - 4, devid_ptr->length);
2023
2024	/*
2025	 * We're using a LUN association here.  i.e., this device ID is a
2026	 * per-LUN identifier.
2027	 */
2028	desc->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_ASCII;
2029	desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
2030	desc->length = sizeof(*t10id) + CTL_DEVID_LEN;
2031	strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
2032
2033	/*
2034	 * If we've actually got a backend, copy the device id from the
2035	 * per-LUN data.  Otherwise, set it to all spaces.
2036	 */
2037	if (lun != NULL) {
2038		/*
2039		 * Copy the backend's LUN ID.
2040		 */
2041		strncpy((char *)t10id->vendor_spec_id,
2042		    (char *)lun->be_lun->device_id, CTL_DEVID_LEN);
2043	} else {
2044		/*
2045		 * No backend, set this to spaces.
2046		 */
2047		memset(t10id->vendor_spec_id, 0x20, CTL_DEVID_LEN);
2048	}
2049
2050	/*
2051	 * desc1 is for the WWPN which is a port asscociation.
2052	 */
2053       	desc1->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8;
2054	desc1->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
2055	    SVPD_ID_TYPE_SCSI_NAME;
2056	desc1->length = wwpn_len;
2057	snprintf(desc1->identifier, wwpn_len, "%s,t,0x%x",
2058	    cs->cs_target->ct_name, cs->cs_portal_group_tag);
2059
2060	/*
2061	 * desc2 is for the Relative Target Port(type 4h) identifier
2062	 */
2063       	desc2->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_BINARY;
2064	desc2->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
2065	    SVPD_ID_TYPE_RELTARG;
2066	desc2->length = 4;
2067	desc2->identifier[3] = 1;
2068
2069	/*
2070	 * desc3 is for the Target Port Group(type 5h) identifier
2071	 */
2072       	desc3->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_BINARY;
2073	desc3->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
2074	    SVPD_ID_TYPE_TPORTGRP;
2075	desc3->length = 4;
2076	desc3->identifier[3] = 1;
2077
2078	ctsio->scsi_status = SCSI_STATUS_OK;
2079
2080	ctsio->be_move_done = ctl_config_move_done;
2081	ctl_datamove((union ctl_io *)ctsio);
2082
2083	return (CTL_RETVAL_COMPLETE);
2084}
2085
2086static void
2087cfiscsi_target_hold(struct cfiscsi_target *ct)
2088{
2089
2090	refcount_acquire(&ct->ct_refcount);
2091}
2092
2093static void
2094cfiscsi_target_release(struct cfiscsi_target *ct)
2095{
2096	int old;
2097	struct cfiscsi_softc *softc;
2098
2099	softc = ct->ct_softc;
2100
2101	old = ct->ct_refcount;
2102	if (old > 1 && atomic_cmpset_int(&ct->ct_refcount, old, old - 1))
2103		return;
2104
2105	mtx_lock(&softc->lock);
2106	if (refcount_release(&ct->ct_refcount)) {
2107		TAILQ_REMOVE(&softc->targets, ct, ct_next);
2108		mtx_unlock(&softc->lock);
2109		free(ct, M_CFISCSI);
2110
2111		return;
2112	}
2113	mtx_unlock(&softc->lock);
2114}
2115
2116static struct cfiscsi_target *
2117cfiscsi_target_find(struct cfiscsi_softc *softc, const char *name)
2118{
2119	struct cfiscsi_target *ct;
2120
2121	mtx_lock(&softc->lock);
2122	TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2123		if (strcmp(name, ct->ct_name) != 0)
2124			continue;
2125		cfiscsi_target_hold(ct);
2126		mtx_unlock(&softc->lock);
2127		return (ct);
2128	}
2129	mtx_unlock(&softc->lock);
2130
2131	return (NULL);
2132}
2133
2134static struct cfiscsi_target *
2135cfiscsi_target_find_or_create(struct cfiscsi_softc *softc, const char *name,
2136    const char *alias)
2137{
2138	struct cfiscsi_target *ct, *newct;
2139	int i;
2140
2141	if (name[0] == '\0' || strlen(name) >= CTL_ISCSI_NAME_LEN)
2142		return (NULL);
2143
2144	newct = malloc(sizeof(*newct), M_CFISCSI, M_WAITOK | M_ZERO);
2145
2146	mtx_lock(&softc->lock);
2147	TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2148		if (strcmp(name, ct->ct_name) != 0)
2149			continue;
2150		cfiscsi_target_hold(ct);
2151		mtx_unlock(&softc->lock);
2152		free(newct, M_CFISCSI);
2153		return (ct);
2154	}
2155
2156	for (i = 0; i < CTL_MAX_LUNS; i++)
2157		newct->ct_luns[i] = -1;
2158
2159	strlcpy(newct->ct_name, name, sizeof(newct->ct_name));
2160	if (alias != NULL)
2161		strlcpy(newct->ct_alias, alias, sizeof(newct->ct_alias));
2162	refcount_init(&newct->ct_refcount, 1);
2163	newct->ct_softc = softc;
2164	TAILQ_INSERT_TAIL(&softc->targets, newct, ct_next);
2165	mtx_unlock(&softc->lock);
2166
2167	return (newct);
2168}
2169
2170/*
2171 * Takes LUN from the target space and returns LUN from the CTL space.
2172 */
2173static uint32_t
2174cfiscsi_map_lun(void *arg, uint32_t lun)
2175{
2176	struct cfiscsi_session *cs;
2177
2178	cs = arg;
2179
2180	if (lun >= CTL_MAX_LUNS) {
2181		CFISCSI_DEBUG("requested lun number %d is higher "
2182		    "than maximum %d", lun, CTL_MAX_LUNS - 1);
2183		return (0xffffffff);
2184	}
2185
2186	if (cs->cs_target->ct_luns[lun] < 0)
2187		return (0xffffffff);
2188
2189	return (cs->cs_target->ct_luns[lun]);
2190}
2191
2192static int
2193cfiscsi_target_set_lun(struct cfiscsi_target *ct,
2194    unsigned long lun_id, unsigned long ctl_lun_id)
2195{
2196
2197	if (lun_id >= CTL_MAX_LUNS) {
2198		CFISCSI_WARN("requested lun number %ld is higher "
2199		    "than maximum %d", lun_id, CTL_MAX_LUNS - 1);
2200		return (-1);
2201	}
2202
2203	if (ct->ct_luns[lun_id] >= 0) {
2204		/*
2205		 * CTL calls cfiscsi_lun_enable() twice for each LUN - once
2206		 * when the LUN is created, and a second time just before
2207		 * the port is brought online; don't emit warnings
2208		 * for that case.
2209		 */
2210		if (ct->ct_luns[lun_id] == ctl_lun_id)
2211			return (0);
2212		CFISCSI_WARN("lun %ld already allocated", lun_id);
2213		return (-1);
2214	}
2215
2216#if 0
2217	CFISCSI_DEBUG("adding mapping for lun %ld, target %s "
2218	    "to ctl lun %ld", lun_id, ct->ct_name, ctl_lun_id);
2219#endif
2220
2221	ct->ct_luns[lun_id] = ctl_lun_id;
2222	cfiscsi_target_hold(ct);
2223
2224	return (0);
2225}
2226
2227static int
2228cfiscsi_target_unset_lun(struct cfiscsi_target *ct, unsigned long lun_id)
2229{
2230
2231	if (ct->ct_luns[lun_id] < 0) {
2232		CFISCSI_WARN("lun %ld not allocated", lun_id);
2233		return (-1);
2234	}
2235
2236	ct->ct_luns[lun_id] = -1;
2237	cfiscsi_target_release(ct);
2238
2239	return (0);
2240}
2241
2242static int
2243cfiscsi_lun_enable(void *arg, struct ctl_id target_id, int lun_id)
2244{
2245	struct cfiscsi_softc *softc;
2246	struct cfiscsi_target *ct;
2247	struct ctl_be_lun_option *opt;
2248	const char *target = NULL, *target_alias = NULL;
2249	const char *lun = NULL;
2250	unsigned long tmp;
2251
2252	softc = (struct cfiscsi_softc *)arg;
2253
2254	STAILQ_FOREACH(opt,
2255	    &control_softc->ctl_luns[lun_id]->be_lun->options, links) {
2256		if (strcmp(opt->name, "cfiscsi_target") == 0)
2257			target = opt->value;
2258		else if (strcmp(opt->name, "cfiscsi_target_alias") == 0)
2259			target_alias = opt->value;
2260		else if (strcmp(opt->name, "cfiscsi_lun") == 0)
2261			lun = opt->value;
2262	}
2263
2264	if (target == NULL && lun == NULL)
2265		return (0);
2266
2267	if (target == NULL || lun == NULL) {
2268		CFISCSI_WARN("lun added with cfiscsi_target, but without "
2269		    "cfiscsi_lun, or the other way around; ignoring");
2270		return (0);
2271	}
2272
2273	ct = cfiscsi_target_find_or_create(softc, target, target_alias);
2274	if (ct == NULL) {
2275		CFISCSI_WARN("failed to create target \"%s\"", target);
2276		return (0);
2277	}
2278
2279	tmp = strtoul(lun, NULL, 10);
2280	cfiscsi_target_set_lun(ct, tmp, lun_id);
2281	return (0);
2282}
2283
2284static int
2285cfiscsi_lun_disable(void *arg, struct ctl_id target_id, int lun_id)
2286{
2287	struct cfiscsi_softc *softc;
2288	struct cfiscsi_target *ct;
2289	int i;
2290
2291	softc = (struct cfiscsi_softc *)arg;
2292
2293	mtx_lock(&softc->lock);
2294	TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2295		for (i = 0; i < CTL_MAX_LUNS; i++) {
2296			if (ct->ct_luns[i] < 0)
2297				continue;
2298			if (ct->ct_luns[i] != lun_id)
2299				continue;
2300			cfiscsi_target_unset_lun(ct, i);
2301			break;
2302		}
2303	}
2304	mtx_unlock(&softc->lock);
2305	return (0);
2306}
2307
2308static void
2309cfiscsi_datamove_in(union ctl_io *io)
2310{
2311	struct cfiscsi_session *cs;
2312	struct icl_pdu *request, *response;
2313	const struct iscsi_bhs_scsi_command *bhssc;
2314	struct iscsi_bhs_data_in *bhsdi;
2315	struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
2316	size_t len, expected_len, sg_len, buffer_offset;
2317	const char *sg_addr;
2318	int ctl_sg_count, error, i;
2319
2320	request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2321	cs = PDU_SESSION(request);
2322
2323	bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
2324	KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2325	    ISCSI_BHS_OPCODE_SCSI_COMMAND,
2326	    ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND"));
2327
2328	if (io->scsiio.kern_sg_entries > 0) {
2329		ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
2330		ctl_sg_count = io->scsiio.kern_sg_entries;
2331	} else {
2332		ctl_sglist = &ctl_sg_entry;
2333		ctl_sglist->addr = io->scsiio.kern_data_ptr;
2334		ctl_sglist->len = io->scsiio.kern_data_len;
2335		ctl_sg_count = 1;
2336	}
2337
2338	/*
2339	 * This is the total amount of data to be transferred within the current
2340	 * SCSI command.  We need to record it so that we can properly report
2341	 * underflow/underflow.
2342	 */
2343	PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len;
2344
2345	/*
2346	 * This is the offset within the current SCSI command; for the first
2347	 * call to cfiscsi_datamove() it will be 0, and for subsequent ones
2348	 * it will be the sum of lengths of previous ones.  It's being
2349	 * incremented as we append data to the data segment.
2350	 */
2351	buffer_offset = io->scsiio.kern_rel_offset;
2352
2353	/*
2354	 * This is the transfer length expected by the initiator.  In theory,
2355	 * it could be different from the correct amount of data from the SCSI
2356	 * point of view, even if that doesn't make any sense.
2357	 */
2358	expected_len = ntohl(bhssc->bhssc_expected_data_transfer_length);
2359#if 0
2360	if (expected_len != io->scsiio.kern_total_len)
2361		CFISCSI_SESSION_DEBUG(cs, "expected transfer length = %zd, "
2362		    "actual length = %zd", expected_len,
2363		    io->scsiio.kern_total_len);
2364#endif
2365
2366	if (buffer_offset >= expected_len) {
2367#if 0
2368		CFISCSI_SESSION_DEBUG(cs, "buffer_offset = %zd, "
2369		    "already sent the expected len", buffer_offset);
2370#endif
2371		io->scsiio.ext_data_filled = io->scsiio.kern_total_len;
2372		io->scsiio.be_move_done(io);
2373		return;
2374	}
2375
2376	i = 0;
2377	sg_addr = NULL;
2378	sg_len = 0;
2379	response = NULL;
2380	bhsdi = NULL;
2381	for (;;) {
2382		if (response == NULL) {
2383			response = cfiscsi_pdu_new_response(request, M_NOWAIT);
2384			if (response == NULL) {
2385				CFISCSI_SESSION_WARN(cs, "failed to "
2386				    "allocate memory; dropping connection");
2387				ctl_set_busy(&io->scsiio);
2388				io->scsiio.be_move_done(io);
2389				cfiscsi_session_terminate(cs);
2390				return;
2391			}
2392			bhsdi = (struct iscsi_bhs_data_in *)response->ip_bhs;
2393			bhsdi->bhsdi_opcode = ISCSI_BHS_OPCODE_SCSI_DATA_IN;
2394			bhsdi->bhsdi_initiator_task_tag =
2395			    bhssc->bhssc_initiator_task_tag;
2396			bhsdi->bhsdi_datasn = htonl(PDU_EXPDATASN(request));
2397			PDU_EXPDATASN(request)++;
2398			bhsdi->bhsdi_buffer_offset = htonl(buffer_offset);
2399		}
2400
2401		KASSERT(i < ctl_sg_count, ("i >= ctl_sg_count"));
2402		if (sg_len == 0) {
2403			sg_addr = ctl_sglist[i].addr;
2404			sg_len = ctl_sglist[i].len;
2405			KASSERT(sg_len > 0, ("sg_len <= 0"));
2406		}
2407
2408		len = sg_len;
2409
2410		/*
2411		 * Truncate to maximum data segment length.
2412		 */
2413		KASSERT(response->ip_data_len < cs->cs_max_data_segment_length,
2414		    ("max_data_segment_length %zd >= ip_data_len %zd",
2415		    response->ip_data_len, cs->cs_max_data_segment_length));
2416		if (response->ip_data_len + len >
2417		    cs->cs_max_data_segment_length)
2418			len = cs->cs_max_data_segment_length -
2419			    response->ip_data_len;
2420
2421		/*
2422		 * Truncate to expected data transfer length.
2423		 */
2424		KASSERT(buffer_offset + response->ip_data_len < expected_len,
2425		    ("%zd >= %zd", buffer_offset + response->ip_data_len, expected_len));
2426		if (buffer_offset + response->ip_data_len + len > expected_len) {
2427			CFISCSI_SESSION_DEBUG(cs, "truncating from %zd "
2428			    "to expected data transfer length %zd",
2429			    buffer_offset + response->ip_data_len + len, expected_len);
2430			len = expected_len - (buffer_offset + response->ip_data_len);
2431		}
2432
2433		KASSERT(len <= sg_len, ("len > sg_len"));
2434		error = icl_pdu_append_data(response, sg_addr, len, M_NOWAIT);
2435		if (error != 0) {
2436			CFISCSI_SESSION_WARN(cs, "failed to "
2437			    "allocate memory; dropping connection");
2438			icl_pdu_free(response);
2439			ctl_set_busy(&io->scsiio);
2440			io->scsiio.be_move_done(io);
2441			cfiscsi_session_terminate(cs);
2442			return;
2443		}
2444		sg_addr += len;
2445		sg_len -= len;
2446		buffer_offset += len;
2447		io->scsiio.ext_data_filled += len;
2448
2449		if (buffer_offset == expected_len) {
2450			/*
2451			 * Already have the amount of data the initiator wanted.
2452			 */
2453			break;
2454		}
2455
2456		if (sg_len == 0) {
2457			/*
2458			 * End of scatter-gather segment;
2459			 * proceed to the next one...
2460			 */
2461			if (i == ctl_sg_count - 1) {
2462				/*
2463				 * ... unless this was the last one.
2464				 */
2465				break;
2466			}
2467			i++;
2468		}
2469
2470		if (response->ip_data_len == cs->cs_max_data_segment_length) {
2471			/*
2472			 * Can't stuff more data into the current PDU;
2473			 * queue it.  Note that's not enough to check
2474			 * for kern_data_resid == 0 instead; there
2475			 * may be several Data-In PDUs for the final
2476			 * call to cfiscsi_datamove(), and we want
2477			 * to set the F flag only on the last of them.
2478			 */
2479			if (buffer_offset == io->scsiio.kern_total_len ||
2480			    buffer_offset == expected_len)
2481				bhsdi->bhsdi_flags |= BHSDI_FLAGS_F;
2482			cfiscsi_pdu_queue(response);
2483			response = NULL;
2484			bhsdi = NULL;
2485		}
2486	}
2487	if (response != NULL) {
2488		if (buffer_offset == io->scsiio.kern_total_len ||
2489		    buffer_offset == expected_len)
2490			bhsdi->bhsdi_flags |= BHSDI_FLAGS_F;
2491		KASSERT(response->ip_data_len > 0, ("sending empty Data-In"));
2492		cfiscsi_pdu_queue(response);
2493	}
2494
2495	io->scsiio.be_move_done(io);
2496}
2497
2498static void
2499cfiscsi_datamove_out(union ctl_io *io)
2500{
2501	struct cfiscsi_session *cs;
2502	struct icl_pdu *request, *response;
2503	const struct iscsi_bhs_scsi_command *bhssc;
2504	struct iscsi_bhs_r2t *bhsr2t;
2505	struct cfiscsi_data_wait *cdw;
2506	uint32_t target_transfer_tag;
2507	bool done;
2508
2509	request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2510	cs = PDU_SESSION(request);
2511
2512	bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
2513	KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2514	    ISCSI_BHS_OPCODE_SCSI_COMMAND,
2515	    ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND"));
2516
2517	/*
2518	 * We need to record it so that we can properly report
2519	 * underflow/underflow.
2520	 */
2521	PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len;
2522
2523	target_transfer_tag =
2524	    atomic_fetchadd_32(&cs->cs_target_transfer_tag, 1);
2525
2526#if 0
2527	CFISCSI_SESSION_DEBUG(cs, "expecting Data-Out with initiator "
2528	    "task tag 0x%x, target transfer tag 0x%x",
2529	    bhssc->bhssc_initiator_task_tag, target_transfer_tag);
2530#endif
2531	cdw = uma_zalloc(cfiscsi_data_wait_zone, M_NOWAIT | M_ZERO);
2532	if (cdw == NULL) {
2533		CFISCSI_SESSION_WARN(cs, "failed to "
2534		    "allocate memory; dropping connection");
2535		ctl_set_busy(&io->scsiio);
2536		io->scsiio.be_move_done(io);
2537		cfiscsi_session_terminate(cs);
2538		return;
2539	}
2540	cdw->cdw_ctl_io = io;
2541	cdw->cdw_target_transfer_tag = target_transfer_tag;
2542	cdw->cdw_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2543
2544	if (cs->cs_immediate_data && icl_pdu_data_segment_length(request) > 0) {
2545		done = cfiscsi_handle_data_segment(request, cdw);
2546		if (done) {
2547			uma_zfree(cfiscsi_data_wait_zone, cdw);
2548			io->scsiio.be_move_done(io);
2549			return;
2550		}
2551
2552#if 0
2553		if (io->scsiio.ext_data_filled != 0)
2554			CFISCSI_SESSION_DEBUG(cs, "got %zd bytes of immediate data, need %zd",
2555			    io->scsiio.ext_data_filled, io->scsiio.kern_data_len);
2556#endif
2557	}
2558
2559	CFISCSI_SESSION_LOCK(cs);
2560	TAILQ_INSERT_TAIL(&cs->cs_waiting_for_data_out, cdw, cdw_next);
2561	CFISCSI_SESSION_UNLOCK(cs);
2562
2563	/*
2564	 * XXX: We should limit the number of outstanding R2T PDUs
2565	 * 	per task to MaxOutstandingR2T.
2566	 */
2567	response = cfiscsi_pdu_new_response(request, M_NOWAIT);
2568	if (response == NULL) {
2569		CFISCSI_SESSION_WARN(cs, "failed to "
2570		    "allocate memory; dropping connection");
2571		ctl_set_busy(&io->scsiio);
2572		io->scsiio.be_move_done(io);
2573		cfiscsi_session_terminate(cs);
2574		return;
2575	}
2576	bhsr2t = (struct iscsi_bhs_r2t *)response->ip_bhs;
2577	bhsr2t->bhsr2t_opcode = ISCSI_BHS_OPCODE_R2T;
2578	bhsr2t->bhsr2t_flags = 0x80;
2579	bhsr2t->bhsr2t_lun = bhssc->bhssc_lun;
2580	bhsr2t->bhsr2t_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2581	bhsr2t->bhsr2t_target_transfer_tag = target_transfer_tag;
2582	/*
2583	 * XXX: Here we assume that cfiscsi_datamove() won't ever
2584	 *	be running concurrently on several CPUs for a given
2585	 *	command.
2586	 */
2587	bhsr2t->bhsr2t_r2tsn = htonl(PDU_R2TSN(request));
2588	PDU_R2TSN(request)++;
2589	/*
2590	 * This is the offset within the current SCSI command;
2591	 * i.e. for the first call of datamove(), it will be 0,
2592	 * and for subsequent ones it will be the sum of lengths
2593	 * of previous ones.
2594	 *
2595	 * The ext_data_filled is to account for unsolicited
2596	 * (immediate) data that might have already arrived.
2597	 */
2598	bhsr2t->bhsr2t_buffer_offset =
2599	    htonl(io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled);
2600	/*
2601	 * This is the total length (sum of S/G lengths) this call
2602	 * to cfiscsi_datamove() is supposed to handle.
2603	 *
2604	 * XXX: Limit it to MaxBurstLength.
2605	 */
2606	bhsr2t->bhsr2t_desired_data_transfer_length =
2607	    htonl(io->scsiio.kern_data_len - io->scsiio.ext_data_filled);
2608	cfiscsi_pdu_queue(response);
2609}
2610
2611static void
2612cfiscsi_datamove(union ctl_io *io)
2613{
2614
2615	if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
2616		cfiscsi_datamove_in(io);
2617	else
2618		cfiscsi_datamove_out(io);
2619}
2620
2621static void
2622cfiscsi_scsi_command_done(union ctl_io *io)
2623{
2624	struct icl_pdu *request, *response;
2625	struct iscsi_bhs_scsi_command *bhssc;
2626	struct iscsi_bhs_scsi_response *bhssr;
2627#ifdef DIAGNOSTIC
2628	struct cfiscsi_data_wait *cdw;
2629#endif
2630	struct cfiscsi_session *cs;
2631	uint16_t sense_length;
2632
2633	request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2634	cs = PDU_SESSION(request);
2635	bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
2636	KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2637	    ISCSI_BHS_OPCODE_SCSI_COMMAND,
2638	    ("replying to wrong opcode 0x%x", bhssc->bhssc_opcode));
2639
2640	//CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x",
2641	//    bhssc->bhssc_initiator_task_tag);
2642
2643#ifdef DIAGNOSTIC
2644	CFISCSI_SESSION_LOCK(cs);
2645	TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next)
2646		KASSERT(bhssc->bhssc_initiator_task_tag !=
2647		    cdw->cdw_initiator_task_tag, ("dangling cdw"));
2648	CFISCSI_SESSION_UNLOCK(cs);
2649#endif
2650
2651	response = cfiscsi_pdu_new_response(request, M_WAITOK);
2652	bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
2653	bhssr->bhssr_opcode = ISCSI_BHS_OPCODE_SCSI_RESPONSE;
2654	bhssr->bhssr_flags = 0x80;
2655	/*
2656	 * XXX: We don't deal with bidirectional under/overflows;
2657	 *	does anything actually support those?
2658	 */
2659	if (PDU_TOTAL_TRANSFER_LEN(request) <
2660	    ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2661		bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_UNDERFLOW;
2662		bhssr->bhssr_residual_count =
2663		    htonl(ntohl(bhssc->bhssc_expected_data_transfer_length) -
2664		    PDU_TOTAL_TRANSFER_LEN(request));
2665		//CFISCSI_SESSION_DEBUG(cs, "underflow; residual count %d",
2666		//    ntohl(bhssr->bhssr_residual_count));
2667	} else if (PDU_TOTAL_TRANSFER_LEN(request) >
2668	    ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2669		bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_OVERFLOW;
2670		bhssr->bhssr_residual_count =
2671		    htonl(PDU_TOTAL_TRANSFER_LEN(request) -
2672		    ntohl(bhssc->bhssc_expected_data_transfer_length));
2673		//CFISCSI_SESSION_DEBUG(cs, "overflow; residual count %d",
2674		//    ntohl(bhssr->bhssr_residual_count));
2675	}
2676	bhssr->bhssr_response = BHSSR_RESPONSE_COMMAND_COMPLETED;
2677	bhssr->bhssr_status = io->scsiio.scsi_status;
2678	bhssr->bhssr_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2679	bhssr->bhssr_expdatasn = htonl(PDU_EXPDATASN(request));
2680
2681	if (io->scsiio.sense_len > 0) {
2682#if 0
2683		CFISCSI_SESSION_DEBUG(cs, "returning %d bytes of sense data",
2684		    io->scsiio.sense_len);
2685#endif
2686		sense_length = htons(io->scsiio.sense_len);
2687		icl_pdu_append_data(response,
2688		    &sense_length, sizeof(sense_length), M_WAITOK);
2689		icl_pdu_append_data(response,
2690		    &io->scsiio.sense_data, io->scsiio.sense_len, M_WAITOK);
2691	}
2692
2693	ctl_free_io(io);
2694	icl_pdu_free(request);
2695	cfiscsi_pdu_queue(response);
2696}
2697
2698static void
2699cfiscsi_task_management_done(union ctl_io *io)
2700{
2701	struct icl_pdu *request, *response;
2702	struct iscsi_bhs_task_management_request *bhstmr;
2703	struct iscsi_bhs_task_management_response *bhstmr2;
2704	struct cfiscsi_data_wait *cdw, *tmpcdw;
2705	struct cfiscsi_session *cs;
2706
2707	request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2708	cs = PDU_SESSION(request);
2709	bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
2710	KASSERT((bhstmr->bhstmr_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2711	    ISCSI_BHS_OPCODE_TASK_REQUEST,
2712	    ("replying to wrong opcode 0x%x", bhstmr->bhstmr_opcode));
2713
2714#if 0
2715	CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x; referenced task tag 0x%x",
2716	    bhstmr->bhstmr_initiator_task_tag,
2717	    bhstmr->bhstmr_referenced_task_tag);
2718#endif
2719
2720	if ((bhstmr->bhstmr_function & ~0x80) ==
2721	    BHSTMR_FUNCTION_ABORT_TASK) {
2722		/*
2723		 * Make sure we no longer wait for Data-Out for this command.
2724		 */
2725		CFISCSI_SESSION_LOCK(cs);
2726		TAILQ_FOREACH_SAFE(cdw,
2727		    &cs->cs_waiting_for_data_out, cdw_next, tmpcdw) {
2728			if (bhstmr->bhstmr_referenced_task_tag !=
2729			    cdw->cdw_initiator_task_tag)
2730				continue;
2731
2732#if 0
2733			CFISCSI_SESSION_DEBUG(cs, "removing csw for initiator task "
2734			    "tag 0x%x", bhstmr->bhstmr_initiator_task_tag);
2735#endif
2736			TAILQ_REMOVE(&cs->cs_waiting_for_data_out,
2737			    cdw, cdw_next);
2738			cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io);
2739			uma_zfree(cfiscsi_data_wait_zone, cdw);
2740		}
2741		CFISCSI_SESSION_UNLOCK(cs);
2742	}
2743
2744	response = cfiscsi_pdu_new_response(request, M_WAITOK);
2745	bhstmr2 = (struct iscsi_bhs_task_management_response *)
2746	    response->ip_bhs;
2747	bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE;
2748	bhstmr2->bhstmr_flags = 0x80;
2749	if (io->io_hdr.status == CTL_SUCCESS) {
2750		bhstmr2->bhstmr_response = BHSTMR_RESPONSE_FUNCTION_COMPLETE;
2751	} else {
2752		/*
2753		 * XXX: How to figure out what exactly went wrong?  iSCSI spec
2754		 * 	expects us to provide detailed error, e.g. "Task does
2755		 * 	not exist" or "LUN does not exist".
2756		 */
2757		CFISCSI_SESSION_DEBUG(cs, "BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED");
2758		bhstmr2->bhstmr_response =
2759		    BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED;
2760	}
2761	bhstmr2->bhstmr_initiator_task_tag = bhstmr->bhstmr_initiator_task_tag;
2762
2763	ctl_free_io(io);
2764	icl_pdu_free(request);
2765	cfiscsi_pdu_queue(response);
2766}
2767
2768static void
2769cfiscsi_done(union ctl_io *io)
2770{
2771	struct icl_pdu *request;
2772	struct cfiscsi_session *cs;
2773
2774	KASSERT(((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE),
2775		("invalid CTL status %#x", io->io_hdr.status));
2776
2777	request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2778	if (request == NULL) {
2779		/*
2780		 * Implicit task termination has just completed; nothing to do.
2781		 */
2782		return;
2783	}
2784
2785	cs = PDU_SESSION(request);
2786	refcount_release(&cs->cs_outstanding_ctl_pdus);
2787
2788	switch (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) {
2789	case ISCSI_BHS_OPCODE_SCSI_COMMAND:
2790		cfiscsi_scsi_command_done(io);
2791		break;
2792	case ISCSI_BHS_OPCODE_TASK_REQUEST:
2793		cfiscsi_task_management_done(io);
2794		break;
2795	default:
2796		panic("cfiscsi_done called with wrong opcode 0x%x",
2797		    request->ip_bhs->bhs_opcode);
2798	}
2799}
2800