ctl_frontend_iscsi.c revision 264524
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 264524 2014-04-16 10:06:37Z 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 264524 2014-04-16 10:06:37Z 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	int error;
1423
1424	cihp = (struct ctl_iscsi_handoff_params *)&(ci->data);
1425	softc = &cfiscsi_softc;
1426
1427	CFISCSI_DEBUG("new connection from %s (%s) to %s",
1428	    cihp->initiator_name, cihp->initiator_addr,
1429	    cihp->target_name);
1430
1431	if (softc->online == 0) {
1432		ci->status = CTL_ISCSI_ERROR;
1433		snprintf(ci->error_str, sizeof(ci->error_str),
1434		    "%s: port offline", __func__);
1435		return;
1436	}
1437
1438	ct = cfiscsi_target_find(softc, cihp->target_name);
1439	if (ct == NULL) {
1440		ci->status = CTL_ISCSI_ERROR;
1441		snprintf(ci->error_str, sizeof(ci->error_str),
1442		    "%s: target not found", __func__);
1443		return;
1444	}
1445
1446#ifdef ICL_KERNEL_PROXY
1447	if (cihp->socket > 0 && cihp->connection_id > 0) {
1448		snprintf(ci->error_str, sizeof(ci->error_str),
1449		    "both socket and connection_id set");
1450		ci->status = CTL_ISCSI_ERROR;
1451		cfiscsi_target_release(ct);
1452		return;
1453	}
1454	if (cihp->socket == 0) {
1455		mtx_lock(&cfiscsi_softc.lock);
1456		TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1457			if (cs->cs_id == cihp->socket)
1458				break;
1459		}
1460		if (cs == NULL) {
1461			mtx_unlock(&cfiscsi_softc.lock);
1462			snprintf(ci->error_str, sizeof(ci->error_str),
1463			    "connection not found");
1464			ci->status = CTL_ISCSI_ERROR;
1465			cfiscsi_target_release(ct);
1466			return;
1467		}
1468		mtx_unlock(&cfiscsi_softc.lock);
1469	} else {
1470#endif
1471		cs = cfiscsi_session_new(softc);
1472		if (cs == NULL) {
1473			ci->status = CTL_ISCSI_ERROR;
1474			snprintf(ci->error_str, sizeof(ci->error_str),
1475			    "%s: cfiscsi_session_new failed", __func__);
1476			cfiscsi_target_release(ct);
1477			return;
1478		}
1479#ifdef ICL_KERNEL_PROXY
1480	}
1481#endif
1482	cs->cs_target = ct;
1483
1484	/*
1485	 * First PDU of Full Feature phase has the same CmdSN as the last
1486	 * PDU from the Login Phase received from the initiator.  Thus,
1487	 * the -1 below.
1488	 */
1489	cs->cs_portal_group_tag = cihp->portal_group_tag;
1490	cs->cs_cmdsn = cihp->cmdsn;
1491	cs->cs_statsn = cihp->statsn;
1492	cs->cs_max_data_segment_length = cihp->max_recv_data_segment_length;
1493	cs->cs_max_burst_length = cihp->max_burst_length;
1494	cs->cs_immediate_data = !!cihp->immediate_data;
1495	if (cihp->header_digest == CTL_ISCSI_DIGEST_CRC32C)
1496		cs->cs_conn->ic_header_crc32c = true;
1497	if (cihp->data_digest == CTL_ISCSI_DIGEST_CRC32C)
1498		cs->cs_conn->ic_data_crc32c = true;
1499
1500	strlcpy(cs->cs_initiator_name,
1501	    cihp->initiator_name, sizeof(cs->cs_initiator_name));
1502	strlcpy(cs->cs_initiator_addr,
1503	    cihp->initiator_addr, sizeof(cs->cs_initiator_addr));
1504	strlcpy(cs->cs_initiator_alias,
1505	    cihp->initiator_alias, sizeof(cs->cs_initiator_alias));
1506
1507#ifdef ICL_KERNEL_PROXY
1508	if (cihp->socket > 0) {
1509#endif
1510		error = icl_conn_handoff(cs->cs_conn, cihp->socket);
1511		if (error != 0) {
1512			cfiscsi_session_delete(cs);
1513			ci->status = CTL_ISCSI_ERROR;
1514			snprintf(ci->error_str, sizeof(ci->error_str),
1515			    "%s: icl_conn_handoff failed with error %d",
1516			    __func__, error);
1517			return;
1518		}
1519#ifdef ICL_KERNEL_PROXY
1520	}
1521#endif
1522
1523	/*
1524	 * Register initiator with CTL.
1525	 */
1526	cfiscsi_session_register_initiator(cs);
1527
1528#ifdef ICL_KERNEL_PROXY
1529	cs->cs_login_phase = false;
1530
1531	/*
1532	 * First PDU of the Full Feature phase has likely already arrived.
1533	 * We have to pick it up and execute properly.
1534	 */
1535	if (cs->cs_login_pdu != NULL) {
1536		CFISCSI_SESSION_DEBUG(cs, "picking up first PDU");
1537		cfiscsi_pdu_handle(cs->cs_login_pdu);
1538		cs->cs_login_pdu = NULL;
1539	}
1540#endif
1541
1542	ci->status = CTL_ISCSI_OK;
1543}
1544
1545static void
1546cfiscsi_ioctl_list(struct ctl_iscsi *ci)
1547{
1548	struct ctl_iscsi_list_params *cilp;
1549	struct cfiscsi_session *cs;
1550	struct cfiscsi_softc *softc;
1551	struct sbuf *sb;
1552	int error;
1553
1554	cilp = (struct ctl_iscsi_list_params *)&(ci->data);
1555	softc = &cfiscsi_softc;
1556
1557	sb = sbuf_new(NULL, NULL, cilp->alloc_len, SBUF_FIXEDLEN);
1558	if (sb == NULL) {
1559		ci->status = CTL_ISCSI_ERROR;
1560		snprintf(ci->error_str, sizeof(ci->error_str),
1561		    "Unable to allocate %d bytes for iSCSI session list",
1562		    cilp->alloc_len);
1563		return;
1564	}
1565
1566	sbuf_printf(sb, "<ctlislist>\n");
1567	mtx_lock(&softc->lock);
1568	TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1569#ifdef ICL_KERNEL_PROXY
1570		if (cs->cs_target == NULL)
1571			continue;
1572#endif
1573		error = sbuf_printf(sb, "<connection id=\"%d\">"
1574		    "<initiator>%s</initiator>"
1575		    "<initiator_addr>%s</initiator_addr>"
1576		    "<initiator_alias>%s</initiator_alias>"
1577		    "<target>%s</target>"
1578		    "<target_alias>%s</target_alias>"
1579		    "<header_digest>%s</header_digest>"
1580		    "<data_digest>%s</data_digest>"
1581		    "<max_data_segment_length>%zd</max_data_segment_length>"
1582		    "<immediate_data>%d</immediate_data>"
1583		    "<iser>%d</iser>"
1584		    "</connection>\n",
1585		    cs->cs_id,
1586		    cs->cs_initiator_name, cs->cs_initiator_addr, cs->cs_initiator_alias,
1587		    cs->cs_target->ct_name, cs->cs_target->ct_alias,
1588		    cs->cs_conn->ic_header_crc32c ? "CRC32C" : "None",
1589		    cs->cs_conn->ic_data_crc32c ? "CRC32C" : "None",
1590		    cs->cs_max_data_segment_length,
1591		    cs->cs_immediate_data,
1592		    cs->cs_conn->ic_iser);
1593		if (error != 0)
1594			break;
1595	}
1596	mtx_unlock(&softc->lock);
1597	error = sbuf_printf(sb, "</ctlislist>\n");
1598	if (error != 0) {
1599		sbuf_delete(sb);
1600		ci->status = CTL_ISCSI_LIST_NEED_MORE_SPACE;
1601		snprintf(ci->error_str, sizeof(ci->error_str),
1602		    "Out of space, %d bytes is too small", cilp->alloc_len);
1603		return;
1604	}
1605	sbuf_finish(sb);
1606
1607	error = copyout(sbuf_data(sb), cilp->conn_xml, sbuf_len(sb) + 1);
1608	cilp->fill_len = sbuf_len(sb) + 1;
1609	ci->status = CTL_ISCSI_OK;
1610	sbuf_delete(sb);
1611}
1612
1613static void
1614cfiscsi_ioctl_terminate(struct ctl_iscsi *ci)
1615{
1616	struct icl_pdu *response;
1617	struct iscsi_bhs_asynchronous_message *bhsam;
1618	struct ctl_iscsi_terminate_params *citp;
1619	struct cfiscsi_session *cs;
1620	struct cfiscsi_softc *softc;
1621	int found = 0;
1622
1623	citp = (struct ctl_iscsi_terminate_params *)&(ci->data);
1624	softc = &cfiscsi_softc;
1625
1626	mtx_lock(&softc->lock);
1627	TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1628		if (citp->all == 0 && cs->cs_id != citp->connection_id &&
1629		    strcmp(cs->cs_initiator_name, citp->initiator_name) != 0 &&
1630		    strcmp(cs->cs_initiator_addr, citp->initiator_addr) != 0)
1631			continue;
1632
1633		response = icl_pdu_new_bhs(cs->cs_conn, M_NOWAIT);
1634		if (response == NULL) {
1635			/*
1636			 * Oh well.  Just terminate the connection.
1637			 */
1638		} else {
1639			bhsam = (struct iscsi_bhs_asynchronous_message *)
1640			    response->ip_bhs;
1641			bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE;
1642			bhsam->bhsam_flags = 0x80;
1643			bhsam->bhsam_0xffffffff = 0xffffffff;
1644			bhsam->bhsam_async_event =
1645			    BHSAM_EVENT_TARGET_TERMINATES_SESSION;
1646			cfiscsi_pdu_queue(response);
1647		}
1648		cfiscsi_session_terminate(cs);
1649		found++;
1650	}
1651	mtx_unlock(&softc->lock);
1652
1653	if (found == 0) {
1654		ci->status = CTL_ISCSI_SESSION_NOT_FOUND;
1655		snprintf(ci->error_str, sizeof(ci->error_str),
1656		    "No matching connections found");
1657		return;
1658	}
1659
1660	ci->status = CTL_ISCSI_OK;
1661}
1662
1663static void
1664cfiscsi_ioctl_logout(struct ctl_iscsi *ci)
1665{
1666	struct icl_pdu *response;
1667	struct iscsi_bhs_asynchronous_message *bhsam;
1668	struct ctl_iscsi_logout_params *cilp;
1669	struct cfiscsi_session *cs;
1670	struct cfiscsi_softc *softc;
1671	int found = 0;
1672
1673	cilp = (struct ctl_iscsi_logout_params *)&(ci->data);
1674	softc = &cfiscsi_softc;
1675
1676	mtx_lock(&softc->lock);
1677	TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1678		if (cilp->all == 0 && cs->cs_id != cilp->connection_id &&
1679		    strcmp(cs->cs_initiator_name, cilp->initiator_name) != 0 &&
1680		    strcmp(cs->cs_initiator_addr, cilp->initiator_addr) != 0)
1681			continue;
1682
1683		response = icl_pdu_new_bhs(cs->cs_conn, M_NOWAIT);
1684		if (response == NULL) {
1685			ci->status = CTL_ISCSI_ERROR;
1686			snprintf(ci->error_str, sizeof(ci->error_str),
1687			    "Unable to allocate memory");
1688			mtx_unlock(&softc->lock);
1689			return;
1690		}
1691		bhsam =
1692		    (struct iscsi_bhs_asynchronous_message *)response->ip_bhs;
1693		bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE;
1694		bhsam->bhsam_flags = 0x80;
1695		bhsam->bhsam_async_event = BHSAM_EVENT_TARGET_REQUESTS_LOGOUT;
1696		bhsam->bhsam_parameter3 = htons(10);
1697		cfiscsi_pdu_queue(response);
1698		found++;
1699	}
1700	mtx_unlock(&softc->lock);
1701
1702	if (found == 0) {
1703		ci->status = CTL_ISCSI_SESSION_NOT_FOUND;
1704		snprintf(ci->error_str, sizeof(ci->error_str),
1705		    "No matching connections found");
1706		return;
1707	}
1708
1709	ci->status = CTL_ISCSI_OK;
1710}
1711
1712#ifdef ICL_KERNEL_PROXY
1713static void
1714cfiscsi_ioctl_listen(struct ctl_iscsi *ci)
1715{
1716	struct ctl_iscsi_listen_params *cilp;
1717	struct sockaddr *sa;
1718	int error;
1719
1720	cilp = (struct ctl_iscsi_listen_params *)&(ci->data);
1721
1722	if (cfiscsi_softc.listener == NULL) {
1723		CFISCSI_DEBUG("no listener");
1724		snprintf(ci->error_str, sizeof(ci->error_str), "no listener");
1725		ci->status = CTL_ISCSI_ERROR;
1726		return;
1727	}
1728
1729	error = getsockaddr(&sa, (void *)cilp->addr, cilp->addrlen);
1730	if (error != 0) {
1731		CFISCSI_DEBUG("getsockaddr, error %d", error);
1732		snprintf(ci->error_str, sizeof(ci->error_str), "getsockaddr failed");
1733		ci->status = CTL_ISCSI_ERROR;
1734		return;
1735	}
1736
1737	error = icl_listen_add(cfiscsi_softc.listener, cilp->iser, cilp->domain,
1738	    cilp->socktype, cilp->protocol, sa);
1739	if (error != 0) {
1740		free(sa, M_SONAME);
1741		CFISCSI_DEBUG("icl_listen_add, error %d", error);
1742		snprintf(ci->error_str, sizeof(ci->error_str),
1743		    "icl_listen_add failed, error %d", error);
1744		ci->status = CTL_ISCSI_ERROR;
1745		return;
1746	}
1747
1748	ci->status = CTL_ISCSI_OK;
1749}
1750
1751static void
1752cfiscsi_ioctl_accept(struct ctl_iscsi *ci)
1753{
1754	struct ctl_iscsi_accept_params *ciap;
1755	struct cfiscsi_session *cs;
1756	int error;
1757
1758	ciap = (struct ctl_iscsi_accept_params *)&(ci->data);
1759
1760	mtx_lock(&cfiscsi_softc.lock);
1761	for (;;) {
1762		TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1763			if (cs->cs_waiting_for_ctld)
1764				break;
1765		}
1766		if (cs != NULL)
1767			break;
1768		error = cv_wait_sig(&cfiscsi_softc.accept_cv, &cfiscsi_softc.lock);
1769		if (error != 0) {
1770			mtx_unlock(&cfiscsi_softc.lock);
1771			snprintf(ci->error_str, sizeof(ci->error_str), "interrupted");
1772			ci->status = CTL_ISCSI_ERROR;
1773			return;
1774		}
1775	}
1776	mtx_unlock(&cfiscsi_softc.lock);
1777
1778	cs->cs_waiting_for_ctld = false;
1779	cs->cs_login_phase = true;
1780
1781	ciap->connection_id = cs->cs_id;
1782	ci->status = CTL_ISCSI_OK;
1783}
1784
1785static void
1786cfiscsi_ioctl_send(struct ctl_iscsi *ci)
1787{
1788	struct ctl_iscsi_send_params *cisp;
1789	struct cfiscsi_session *cs;
1790	struct icl_pdu *ip;
1791	size_t datalen;
1792	void *data;
1793	int error;
1794
1795	cisp = (struct ctl_iscsi_send_params *)&(ci->data);
1796
1797	mtx_lock(&cfiscsi_softc.lock);
1798	TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1799		if (cs->cs_id == cisp->connection_id)
1800			break;
1801	}
1802	if (cs == NULL) {
1803		mtx_unlock(&cfiscsi_softc.lock);
1804		snprintf(ci->error_str, sizeof(ci->error_str), "connection not found");
1805		ci->status = CTL_ISCSI_ERROR;
1806		return;
1807	}
1808	mtx_unlock(&cfiscsi_softc.lock);
1809
1810#if 0
1811	if (cs->cs_login_phase == false)
1812		return (EBUSY);
1813#endif
1814
1815	if (cs->cs_terminating) {
1816		snprintf(ci->error_str, sizeof(ci->error_str), "connection is terminating");
1817		ci->status = CTL_ISCSI_ERROR;
1818		return;
1819	}
1820
1821	datalen = cisp->data_segment_len;
1822	/*
1823	 * XXX
1824	 */
1825	//if (datalen > CFISCSI_MAX_DATA_SEGMENT_LENGTH) {
1826	if (datalen > 65535) {
1827		snprintf(ci->error_str, sizeof(ci->error_str), "data segment too big");
1828		ci->status = CTL_ISCSI_ERROR;
1829		return;
1830	}
1831	if (datalen > 0) {
1832		data = malloc(datalen, M_CFISCSI, M_WAITOK);
1833		error = copyin(cisp->data_segment, data, datalen);
1834		if (error != 0) {
1835			free(data, M_CFISCSI);
1836			snprintf(ci->error_str, sizeof(ci->error_str), "copyin error %d", error);
1837			ci->status = CTL_ISCSI_ERROR;
1838			return;
1839		}
1840	}
1841
1842	ip = icl_pdu_new_bhs(cs->cs_conn, M_WAITOK);
1843	memcpy(ip->ip_bhs, cisp->bhs, sizeof(*ip->ip_bhs));
1844	if (datalen > 0) {
1845		icl_pdu_append_data(ip, data, datalen, M_WAITOK);
1846		free(data, M_CFISCSI);
1847	}
1848	CFISCSI_SESSION_LOCK(cs);
1849	icl_pdu_queue(ip);
1850	CFISCSI_SESSION_UNLOCK(cs);
1851	ci->status = CTL_ISCSI_OK;
1852}
1853
1854static void
1855cfiscsi_ioctl_receive(struct ctl_iscsi *ci)
1856{
1857	struct ctl_iscsi_receive_params *cirp;
1858	struct cfiscsi_session *cs;
1859	struct icl_pdu *ip;
1860	void *data;
1861
1862	cirp = (struct ctl_iscsi_receive_params *)&(ci->data);
1863
1864	mtx_lock(&cfiscsi_softc.lock);
1865	TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1866		if (cs->cs_id == cirp->connection_id)
1867			break;
1868	}
1869	if (cs == NULL) {
1870		mtx_unlock(&cfiscsi_softc.lock);
1871		snprintf(ci->error_str, sizeof(ci->error_str), "connection not found");
1872		ci->status = CTL_ISCSI_ERROR;
1873		return;
1874	}
1875	mtx_unlock(&cfiscsi_softc.lock);
1876
1877#if 0
1878	if (is->is_login_phase == false)
1879		return (EBUSY);
1880#endif
1881
1882	CFISCSI_SESSION_LOCK(cs);
1883	while (cs->cs_login_pdu == NULL &&
1884	    cs->cs_terminating == false)
1885		cv_wait(&cs->cs_login_cv, &cs->cs_lock);
1886	if (cs->cs_terminating) {
1887		CFISCSI_SESSION_UNLOCK(cs);
1888		snprintf(ci->error_str, sizeof(ci->error_str), "connection terminating");
1889		ci->status = CTL_ISCSI_ERROR;
1890		return;
1891	}
1892	ip = cs->cs_login_pdu;
1893	cs->cs_login_pdu = NULL;
1894	CFISCSI_SESSION_UNLOCK(cs);
1895
1896	if (ip->ip_data_len > cirp->data_segment_len) {
1897		icl_pdu_free(ip);
1898		snprintf(ci->error_str, sizeof(ci->error_str), "data segment too big");
1899		ci->status = CTL_ISCSI_ERROR;
1900		return;
1901	}
1902
1903	copyout(ip->ip_bhs, cirp->bhs, sizeof(*ip->ip_bhs));
1904	if (ip->ip_data_len > 0) {
1905		data = malloc(ip->ip_data_len, M_CFISCSI, M_WAITOK);
1906		icl_pdu_get_data(ip, 0, data, ip->ip_data_len);
1907		copyout(data, cirp->data_segment, ip->ip_data_len);
1908		free(data, M_CFISCSI);
1909	}
1910
1911	icl_pdu_free(ip);
1912	ci->status = CTL_ISCSI_OK;
1913}
1914
1915static void
1916cfiscsi_ioctl_close(struct ctl_iscsi *ci)
1917{
1918	/*
1919	 * XXX
1920	 */
1921}
1922#endif /* !ICL_KERNEL_PROXY */
1923
1924static int
1925cfiscsi_ioctl(struct cdev *dev,
1926    u_long cmd, caddr_t addr, int flag, struct thread *td)
1927{
1928	struct ctl_iscsi *ci;
1929
1930	if (cmd != CTL_ISCSI)
1931		return (ENOTTY);
1932
1933	ci = (struct ctl_iscsi *)addr;
1934	switch (ci->type) {
1935	case CTL_ISCSI_HANDOFF:
1936		cfiscsi_ioctl_handoff(ci);
1937		break;
1938	case CTL_ISCSI_LIST:
1939		cfiscsi_ioctl_list(ci);
1940		break;
1941	case CTL_ISCSI_TERMINATE:
1942		cfiscsi_ioctl_terminate(ci);
1943		break;
1944	case CTL_ISCSI_LOGOUT:
1945		cfiscsi_ioctl_logout(ci);
1946		break;
1947#ifdef ICL_KERNEL_PROXY
1948	case CTL_ISCSI_LISTEN:
1949		cfiscsi_ioctl_listen(ci);
1950		break;
1951	case CTL_ISCSI_ACCEPT:
1952		cfiscsi_ioctl_accept(ci);
1953		break;
1954	case CTL_ISCSI_SEND:
1955		cfiscsi_ioctl_send(ci);
1956		break;
1957	case CTL_ISCSI_RECEIVE:
1958		cfiscsi_ioctl_receive(ci);
1959		break;
1960	case CTL_ISCSI_CLOSE:
1961		cfiscsi_ioctl_close(ci);
1962		break;
1963#endif /* ICL_KERNEL_PROXY */
1964	default:
1965		ci->status = CTL_ISCSI_ERROR;
1966		snprintf(ci->error_str, sizeof(ci->error_str),
1967		    "%s: invalid iSCSI request type %d", __func__, ci->type);
1968		break;
1969	}
1970
1971	return (0);
1972}
1973
1974static int
1975cfiscsi_devid(struct ctl_scsiio *ctsio, int alloc_len)
1976{
1977	struct cfiscsi_session *cs;
1978	struct scsi_vpd_device_id *devid_ptr;
1979	struct scsi_vpd_id_descriptor *desc, *desc1;
1980	struct scsi_vpd_id_descriptor *desc2, *desc3; /* for types 4h and 5h */
1981	struct scsi_vpd_id_t10 *t10id;
1982	struct ctl_lun *lun;
1983	const struct icl_pdu *request;
1984	size_t devid_len, wwpn_len;
1985
1986	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
1987	request = ctsio->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1988	cs = PDU_SESSION(request);
1989
1990	wwpn_len = strlen(cs->cs_target->ct_name);
1991	wwpn_len += strlen(",t,0x01");
1992	wwpn_len += 1; /* '\0' */
1993	if ((wwpn_len % 4) != 0)
1994		wwpn_len += (4 - (wwpn_len % 4));
1995
1996	devid_len = sizeof(struct scsi_vpd_device_id) +
1997		sizeof(struct scsi_vpd_id_descriptor) +
1998		sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN +
1999		sizeof(struct scsi_vpd_id_descriptor) + wwpn_len +
2000		sizeof(struct scsi_vpd_id_descriptor) +
2001		sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
2002		sizeof(struct scsi_vpd_id_descriptor) +
2003		sizeof(struct scsi_vpd_id_trgt_port_grp_id);
2004
2005	ctsio->kern_data_ptr = malloc(devid_len, M_CTL, M_WAITOK | M_ZERO);
2006	devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
2007	ctsio->kern_sg_entries = 0;
2008
2009	if (devid_len < alloc_len) {
2010		ctsio->residual = alloc_len - devid_len;
2011		ctsio->kern_data_len = devid_len;
2012		ctsio->kern_total_len = devid_len;
2013	} else {
2014		ctsio->residual = 0;
2015		ctsio->kern_data_len = alloc_len;
2016		ctsio->kern_total_len = alloc_len;
2017	}
2018	ctsio->kern_data_resid = 0;
2019	ctsio->kern_rel_offset = 0;
2020	ctsio->kern_sg_entries = 0;
2021
2022	desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
2023	t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
2024	desc1 = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
2025	    sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN);
2026	desc2 = (struct scsi_vpd_id_descriptor *)(&desc1->identifier[0] +
2027	    wwpn_len);
2028	desc3 = (struct scsi_vpd_id_descriptor *)(&desc2->identifier[0] +
2029	    sizeof(struct scsi_vpd_id_rel_trgt_port_id));
2030
2031	if (lun != NULL)
2032		devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
2033		    lun->be_lun->lun_type;
2034	else
2035		devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
2036
2037	devid_ptr->page_code = SVPD_DEVICE_ID;
2038
2039	scsi_ulto2b(devid_len - 4, devid_ptr->length);
2040
2041	/*
2042	 * We're using a LUN association here.  i.e., this device ID is a
2043	 * per-LUN identifier.
2044	 */
2045	desc->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_ASCII;
2046	desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
2047	desc->length = sizeof(*t10id) + CTL_DEVID_LEN;
2048	strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
2049
2050	/*
2051	 * If we've actually got a backend, copy the device id from the
2052	 * per-LUN data.  Otherwise, set it to all spaces.
2053	 */
2054	if (lun != NULL) {
2055		/*
2056		 * Copy the backend's LUN ID.
2057		 */
2058		strncpy((char *)t10id->vendor_spec_id,
2059		    (char *)lun->be_lun->device_id, CTL_DEVID_LEN);
2060	} else {
2061		/*
2062		 * No backend, set this to spaces.
2063		 */
2064		memset(t10id->vendor_spec_id, 0x20, CTL_DEVID_LEN);
2065	}
2066
2067	/*
2068	 * desc1 is for the WWPN which is a port asscociation.
2069	 */
2070       	desc1->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8;
2071	desc1->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
2072	    SVPD_ID_TYPE_SCSI_NAME;
2073	desc1->length = wwpn_len;
2074	snprintf(desc1->identifier, wwpn_len, "%s,t,0x%x",
2075	    cs->cs_target->ct_name, cs->cs_portal_group_tag);
2076
2077	/*
2078	 * desc2 is for the Relative Target Port(type 4h) identifier
2079	 */
2080       	desc2->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_BINARY;
2081	desc2->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
2082	    SVPD_ID_TYPE_RELTARG;
2083	desc2->length = 4;
2084	desc2->identifier[3] = 1;
2085
2086	/*
2087	 * desc3 is for the Target Port Group(type 5h) identifier
2088	 */
2089       	desc3->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_BINARY;
2090	desc3->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
2091	    SVPD_ID_TYPE_TPORTGRP;
2092	desc3->length = 4;
2093	desc3->identifier[3] = 1;
2094
2095	ctsio->scsi_status = SCSI_STATUS_OK;
2096
2097	ctsio->be_move_done = ctl_config_move_done;
2098	ctl_datamove((union ctl_io *)ctsio);
2099
2100	return (CTL_RETVAL_COMPLETE);
2101}
2102
2103static void
2104cfiscsi_target_hold(struct cfiscsi_target *ct)
2105{
2106
2107	refcount_acquire(&ct->ct_refcount);
2108}
2109
2110static void
2111cfiscsi_target_release(struct cfiscsi_target *ct)
2112{
2113	struct cfiscsi_softc *softc;
2114
2115	softc = ct->ct_softc;
2116	mtx_lock(&softc->lock);
2117	if (refcount_release(&ct->ct_refcount)) {
2118		TAILQ_REMOVE(&softc->targets, ct, ct_next);
2119		mtx_unlock(&softc->lock);
2120		free(ct, M_CFISCSI);
2121
2122		return;
2123	}
2124	mtx_unlock(&softc->lock);
2125}
2126
2127static struct cfiscsi_target *
2128cfiscsi_target_find(struct cfiscsi_softc *softc, const char *name)
2129{
2130	struct cfiscsi_target *ct;
2131
2132	mtx_lock(&softc->lock);
2133	TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2134		if (strcmp(name, ct->ct_name) != 0)
2135			continue;
2136		cfiscsi_target_hold(ct);
2137		mtx_unlock(&softc->lock);
2138		return (ct);
2139	}
2140	mtx_unlock(&softc->lock);
2141
2142	return (NULL);
2143}
2144
2145static struct cfiscsi_target *
2146cfiscsi_target_find_or_create(struct cfiscsi_softc *softc, const char *name,
2147    const char *alias)
2148{
2149	struct cfiscsi_target *ct, *newct;
2150	int i;
2151
2152	if (name[0] == '\0' || strlen(name) >= CTL_ISCSI_NAME_LEN)
2153		return (NULL);
2154
2155	newct = malloc(sizeof(*newct), M_CFISCSI, M_WAITOK | M_ZERO);
2156
2157	mtx_lock(&softc->lock);
2158	TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2159		if (strcmp(name, ct->ct_name) != 0)
2160			continue;
2161		cfiscsi_target_hold(ct);
2162		mtx_unlock(&softc->lock);
2163		free(newct, M_CFISCSI);
2164		return (ct);
2165	}
2166
2167	for (i = 0; i < CTL_MAX_LUNS; i++)
2168		newct->ct_luns[i] = -1;
2169
2170	strlcpy(newct->ct_name, name, sizeof(newct->ct_name));
2171	if (alias != NULL)
2172		strlcpy(newct->ct_alias, alias, sizeof(newct->ct_alias));
2173	refcount_init(&newct->ct_refcount, 1);
2174	newct->ct_softc = softc;
2175	TAILQ_INSERT_TAIL(&softc->targets, newct, ct_next);
2176	mtx_unlock(&softc->lock);
2177
2178	return (newct);
2179}
2180
2181/*
2182 * Takes LUN from the target space and returns LUN from the CTL space.
2183 */
2184static uint32_t
2185cfiscsi_map_lun(void *arg, uint32_t lun)
2186{
2187	struct cfiscsi_session *cs;
2188
2189	cs = arg;
2190
2191	if (lun >= CTL_MAX_LUNS) {
2192		CFISCSI_DEBUG("requested lun number %d is higher "
2193		    "than maximum %d", lun, CTL_MAX_LUNS - 1);
2194		return (0xffffffff);
2195	}
2196
2197	if (cs->cs_target->ct_luns[lun] < 0)
2198		return (0xffffffff);
2199
2200	return (cs->cs_target->ct_luns[lun]);
2201}
2202
2203static int
2204cfiscsi_target_set_lun(struct cfiscsi_target *ct,
2205    unsigned long lun_id, unsigned long ctl_lun_id)
2206{
2207
2208	if (lun_id >= CTL_MAX_LUNS) {
2209		CFISCSI_WARN("requested lun number %ld is higher "
2210		    "than maximum %d", lun_id, CTL_MAX_LUNS - 1);
2211		return (-1);
2212	}
2213
2214	if (ct->ct_luns[lun_id] >= 0) {
2215		/*
2216		 * CTL calls cfiscsi_lun_enable() twice for each LUN - once
2217		 * when the LUN is created, and a second time just before
2218		 * the port is brought online; don't emit warnings
2219		 * for that case.
2220		 */
2221		if (ct->ct_luns[lun_id] == ctl_lun_id)
2222			return (0);
2223		CFISCSI_WARN("lun %ld already allocated", lun_id);
2224		return (-1);
2225	}
2226
2227#if 0
2228	CFISCSI_DEBUG("adding mapping for lun %ld, target %s "
2229	    "to ctl lun %ld", lun_id, ct->ct_name, ctl_lun_id);
2230#endif
2231
2232	ct->ct_luns[lun_id] = ctl_lun_id;
2233	cfiscsi_target_hold(ct);
2234
2235	return (0);
2236}
2237
2238static int
2239cfiscsi_target_unset_lun(struct cfiscsi_target *ct, unsigned long lun_id)
2240{
2241
2242	if (ct->ct_luns[lun_id] < 0) {
2243		CFISCSI_WARN("lun %ld not allocated", lun_id);
2244		return (-1);
2245	}
2246
2247	ct->ct_luns[lun_id] = -1;
2248	cfiscsi_target_release(ct);
2249
2250	return (0);
2251}
2252
2253static int
2254cfiscsi_lun_enable(void *arg, struct ctl_id target_id, int lun_id)
2255{
2256	struct cfiscsi_softc *softc;
2257	struct cfiscsi_target *ct;
2258	struct ctl_be_lun_option *opt;
2259	const char *target = NULL, *target_alias = NULL;
2260	const char *lun = NULL;
2261	unsigned long tmp;
2262
2263	softc = (struct cfiscsi_softc *)arg;
2264
2265	STAILQ_FOREACH(opt,
2266	    &control_softc->ctl_luns[lun_id]->be_lun->options, links) {
2267		if (strcmp(opt->name, "cfiscsi_target") == 0)
2268			target = opt->value;
2269		else if (strcmp(opt->name, "cfiscsi_target_alias") == 0)
2270			target_alias = opt->value;
2271		else if (strcmp(opt->name, "cfiscsi_lun") == 0)
2272			lun = opt->value;
2273	}
2274
2275	if (target == NULL && lun == NULL)
2276		return (0);
2277
2278	if (target == NULL || lun == NULL) {
2279		CFISCSI_WARN("lun added with cfiscsi_target, but without "
2280		    "cfiscsi_lun, or the other way around; ignoring");
2281		return (0);
2282	}
2283
2284	ct = cfiscsi_target_find_or_create(softc, target, target_alias);
2285	if (ct == NULL) {
2286		CFISCSI_WARN("failed to create target \"%s\"", target);
2287		return (0);
2288	}
2289
2290	tmp = strtoul(lun, NULL, 10);
2291	cfiscsi_target_set_lun(ct, tmp, lun_id);
2292	cfiscsi_target_release(ct);
2293	return (0);
2294}
2295
2296static int
2297cfiscsi_lun_disable(void *arg, struct ctl_id target_id, int lun_id)
2298{
2299	struct cfiscsi_softc *softc;
2300	struct cfiscsi_target *ct;
2301	int i;
2302
2303	softc = (struct cfiscsi_softc *)arg;
2304
2305	mtx_lock(&softc->lock);
2306	TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2307		for (i = 0; i < CTL_MAX_LUNS; i++) {
2308			if (ct->ct_luns[i] < 0)
2309				continue;
2310			if (ct->ct_luns[i] != lun_id)
2311				continue;
2312			mtx_unlock(&softc->lock);
2313			cfiscsi_target_unset_lun(ct, i);
2314			return (0);
2315		}
2316	}
2317	mtx_unlock(&softc->lock);
2318	return (0);
2319}
2320
2321static void
2322cfiscsi_datamove_in(union ctl_io *io)
2323{
2324	struct cfiscsi_session *cs;
2325	struct icl_pdu *request, *response;
2326	const struct iscsi_bhs_scsi_command *bhssc;
2327	struct iscsi_bhs_data_in *bhsdi;
2328	struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
2329	size_t len, expected_len, sg_len, buffer_offset;
2330	const char *sg_addr;
2331	int ctl_sg_count, error, i;
2332
2333	request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2334	cs = PDU_SESSION(request);
2335
2336	bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
2337	KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2338	    ISCSI_BHS_OPCODE_SCSI_COMMAND,
2339	    ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND"));
2340
2341	if (io->scsiio.kern_sg_entries > 0) {
2342		ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
2343		ctl_sg_count = io->scsiio.kern_sg_entries;
2344	} else {
2345		ctl_sglist = &ctl_sg_entry;
2346		ctl_sglist->addr = io->scsiio.kern_data_ptr;
2347		ctl_sglist->len = io->scsiio.kern_data_len;
2348		ctl_sg_count = 1;
2349	}
2350
2351	/*
2352	 * This is the total amount of data to be transferred within the current
2353	 * SCSI command.  We need to record it so that we can properly report
2354	 * underflow/underflow.
2355	 */
2356	PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len;
2357
2358	/*
2359	 * This is the offset within the current SCSI command; for the first
2360	 * call to cfiscsi_datamove() it will be 0, and for subsequent ones
2361	 * it will be the sum of lengths of previous ones.  It's being
2362	 * incremented as we append data to the data segment.
2363	 */
2364	buffer_offset = io->scsiio.kern_rel_offset;
2365
2366	/*
2367	 * This is the transfer length expected by the initiator.  In theory,
2368	 * it could be different from the correct amount of data from the SCSI
2369	 * point of view, even if that doesn't make any sense.
2370	 */
2371	expected_len = ntohl(bhssc->bhssc_expected_data_transfer_length);
2372#if 0
2373	if (expected_len != io->scsiio.kern_total_len)
2374		CFISCSI_SESSION_DEBUG(cs, "expected transfer length = %zd, "
2375		    "actual length = %zd", expected_len,
2376		    io->scsiio.kern_total_len);
2377#endif
2378
2379	if (buffer_offset >= expected_len) {
2380#if 0
2381		CFISCSI_SESSION_DEBUG(cs, "buffer_offset = %zd, "
2382		    "already sent the expected len", buffer_offset);
2383#endif
2384		io->scsiio.ext_data_filled = io->scsiio.kern_total_len;
2385		io->scsiio.be_move_done(io);
2386		return;
2387	}
2388
2389	i = 0;
2390	sg_addr = NULL;
2391	sg_len = 0;
2392	response = NULL;
2393	bhsdi = NULL;
2394	for (;;) {
2395		if (response == NULL) {
2396			response = cfiscsi_pdu_new_response(request, M_NOWAIT);
2397			if (response == NULL) {
2398				CFISCSI_SESSION_WARN(cs, "failed to "
2399				    "allocate memory; dropping connection");
2400				ctl_set_busy(&io->scsiio);
2401				io->scsiio.be_move_done(io);
2402				cfiscsi_session_terminate(cs);
2403				return;
2404			}
2405			bhsdi = (struct iscsi_bhs_data_in *)response->ip_bhs;
2406			bhsdi->bhsdi_opcode = ISCSI_BHS_OPCODE_SCSI_DATA_IN;
2407			bhsdi->bhsdi_initiator_task_tag =
2408			    bhssc->bhssc_initiator_task_tag;
2409			bhsdi->bhsdi_datasn = htonl(PDU_EXPDATASN(request));
2410			PDU_EXPDATASN(request)++;
2411			bhsdi->bhsdi_buffer_offset = htonl(buffer_offset);
2412		}
2413
2414		KASSERT(i < ctl_sg_count, ("i >= ctl_sg_count"));
2415		if (sg_len == 0) {
2416			sg_addr = ctl_sglist[i].addr;
2417			sg_len = ctl_sglist[i].len;
2418			KASSERT(sg_len > 0, ("sg_len <= 0"));
2419		}
2420
2421		len = sg_len;
2422
2423		/*
2424		 * Truncate to maximum data segment length.
2425		 */
2426		KASSERT(response->ip_data_len < cs->cs_max_data_segment_length,
2427		    ("max_data_segment_length %zd >= ip_data_len %zd",
2428		    response->ip_data_len, cs->cs_max_data_segment_length));
2429		if (response->ip_data_len + len >
2430		    cs->cs_max_data_segment_length)
2431			len = cs->cs_max_data_segment_length -
2432			    response->ip_data_len;
2433
2434		/*
2435		 * Truncate to expected data transfer length.
2436		 */
2437		KASSERT(buffer_offset + response->ip_data_len < expected_len,
2438		    ("%zd >= %zd", buffer_offset + response->ip_data_len, expected_len));
2439		if (buffer_offset + response->ip_data_len + len > expected_len) {
2440			CFISCSI_SESSION_DEBUG(cs, "truncating from %zd "
2441			    "to expected data transfer length %zd",
2442			    buffer_offset + response->ip_data_len + len, expected_len);
2443			len = expected_len - (buffer_offset + response->ip_data_len);
2444		}
2445
2446		KASSERT(len <= sg_len, ("len > sg_len"));
2447		error = icl_pdu_append_data(response, sg_addr, len, M_NOWAIT);
2448		if (error != 0) {
2449			CFISCSI_SESSION_WARN(cs, "failed to "
2450			    "allocate memory; dropping connection");
2451			icl_pdu_free(response);
2452			ctl_set_busy(&io->scsiio);
2453			io->scsiio.be_move_done(io);
2454			cfiscsi_session_terminate(cs);
2455			return;
2456		}
2457		sg_addr += len;
2458		sg_len -= len;
2459		buffer_offset += len;
2460		io->scsiio.ext_data_filled += len;
2461
2462		if (buffer_offset == expected_len) {
2463			/*
2464			 * Already have the amount of data the initiator wanted.
2465			 */
2466			break;
2467		}
2468
2469		if (sg_len == 0) {
2470			/*
2471			 * End of scatter-gather segment;
2472			 * proceed to the next one...
2473			 */
2474			if (i == ctl_sg_count - 1) {
2475				/*
2476				 * ... unless this was the last one.
2477				 */
2478				break;
2479			}
2480			i++;
2481		}
2482
2483		if (response->ip_data_len == cs->cs_max_data_segment_length) {
2484			/*
2485			 * Can't stuff more data into the current PDU;
2486			 * queue it.  Note that's not enough to check
2487			 * for kern_data_resid == 0 instead; there
2488			 * may be several Data-In PDUs for the final
2489			 * call to cfiscsi_datamove(), and we want
2490			 * to set the F flag only on the last of them.
2491			 */
2492			if (buffer_offset == io->scsiio.kern_total_len ||
2493			    buffer_offset == expected_len)
2494				bhsdi->bhsdi_flags |= BHSDI_FLAGS_F;
2495			cfiscsi_pdu_queue(response);
2496			response = NULL;
2497			bhsdi = NULL;
2498		}
2499	}
2500	if (response != NULL) {
2501		if (buffer_offset == io->scsiio.kern_total_len ||
2502		    buffer_offset == expected_len)
2503			bhsdi->bhsdi_flags |= BHSDI_FLAGS_F;
2504		KASSERT(response->ip_data_len > 0, ("sending empty Data-In"));
2505		cfiscsi_pdu_queue(response);
2506	}
2507
2508	io->scsiio.be_move_done(io);
2509}
2510
2511static void
2512cfiscsi_datamove_out(union ctl_io *io)
2513{
2514	struct cfiscsi_session *cs;
2515	struct icl_pdu *request, *response;
2516	const struct iscsi_bhs_scsi_command *bhssc;
2517	struct iscsi_bhs_r2t *bhsr2t;
2518	struct cfiscsi_data_wait *cdw;
2519	uint32_t target_transfer_tag;
2520	bool done;
2521
2522	request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2523	cs = PDU_SESSION(request);
2524
2525	bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
2526	KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2527	    ISCSI_BHS_OPCODE_SCSI_COMMAND,
2528	    ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND"));
2529
2530	/*
2531	 * We need to record it so that we can properly report
2532	 * underflow/underflow.
2533	 */
2534	PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len;
2535
2536	target_transfer_tag =
2537	    atomic_fetchadd_32(&cs->cs_target_transfer_tag, 1);
2538
2539#if 0
2540	CFISCSI_SESSION_DEBUG(cs, "expecting Data-Out with initiator "
2541	    "task tag 0x%x, target transfer tag 0x%x",
2542	    bhssc->bhssc_initiator_task_tag, target_transfer_tag);
2543#endif
2544	cdw = uma_zalloc(cfiscsi_data_wait_zone, M_NOWAIT | M_ZERO);
2545	if (cdw == NULL) {
2546		CFISCSI_SESSION_WARN(cs, "failed to "
2547		    "allocate memory; dropping connection");
2548		ctl_set_busy(&io->scsiio);
2549		io->scsiio.be_move_done(io);
2550		cfiscsi_session_terminate(cs);
2551		return;
2552	}
2553	cdw->cdw_ctl_io = io;
2554	cdw->cdw_target_transfer_tag = target_transfer_tag;
2555	cdw->cdw_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2556
2557	if (cs->cs_immediate_data && icl_pdu_data_segment_length(request) > 0) {
2558		done = cfiscsi_handle_data_segment(request, cdw);
2559		if (done) {
2560			uma_zfree(cfiscsi_data_wait_zone, cdw);
2561			io->scsiio.be_move_done(io);
2562			return;
2563		}
2564
2565#if 0
2566		if (io->scsiio.ext_data_filled != 0)
2567			CFISCSI_SESSION_DEBUG(cs, "got %zd bytes of immediate data, need %zd",
2568			    io->scsiio.ext_data_filled, io->scsiio.kern_data_len);
2569#endif
2570	}
2571
2572	CFISCSI_SESSION_LOCK(cs);
2573	TAILQ_INSERT_TAIL(&cs->cs_waiting_for_data_out, cdw, cdw_next);
2574	CFISCSI_SESSION_UNLOCK(cs);
2575
2576	/*
2577	 * XXX: We should limit the number of outstanding R2T PDUs
2578	 * 	per task to MaxOutstandingR2T.
2579	 */
2580	response = cfiscsi_pdu_new_response(request, M_NOWAIT);
2581	if (response == NULL) {
2582		CFISCSI_SESSION_WARN(cs, "failed to "
2583		    "allocate memory; dropping connection");
2584		ctl_set_busy(&io->scsiio);
2585		io->scsiio.be_move_done(io);
2586		cfiscsi_session_terminate(cs);
2587		return;
2588	}
2589	bhsr2t = (struct iscsi_bhs_r2t *)response->ip_bhs;
2590	bhsr2t->bhsr2t_opcode = ISCSI_BHS_OPCODE_R2T;
2591	bhsr2t->bhsr2t_flags = 0x80;
2592	bhsr2t->bhsr2t_lun = bhssc->bhssc_lun;
2593	bhsr2t->bhsr2t_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2594	bhsr2t->bhsr2t_target_transfer_tag = target_transfer_tag;
2595	/*
2596	 * XXX: Here we assume that cfiscsi_datamove() won't ever
2597	 *	be running concurrently on several CPUs for a given
2598	 *	command.
2599	 */
2600	bhsr2t->bhsr2t_r2tsn = htonl(PDU_R2TSN(request));
2601	PDU_R2TSN(request)++;
2602	/*
2603	 * This is the offset within the current SCSI command;
2604	 * i.e. for the first call of datamove(), it will be 0,
2605	 * and for subsequent ones it will be the sum of lengths
2606	 * of previous ones.
2607	 *
2608	 * The ext_data_filled is to account for unsolicited
2609	 * (immediate) data that might have already arrived.
2610	 */
2611	bhsr2t->bhsr2t_buffer_offset =
2612	    htonl(io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled);
2613	/*
2614	 * This is the total length (sum of S/G lengths) this call
2615	 * to cfiscsi_datamove() is supposed to handle.
2616	 *
2617	 * XXX: Limit it to MaxBurstLength.
2618	 */
2619	bhsr2t->bhsr2t_desired_data_transfer_length =
2620	    htonl(io->scsiio.kern_data_len - io->scsiio.ext_data_filled);
2621	cfiscsi_pdu_queue(response);
2622}
2623
2624static void
2625cfiscsi_datamove(union ctl_io *io)
2626{
2627
2628	if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
2629		cfiscsi_datamove_in(io);
2630	else
2631		cfiscsi_datamove_out(io);
2632}
2633
2634static void
2635cfiscsi_scsi_command_done(union ctl_io *io)
2636{
2637	struct icl_pdu *request, *response;
2638	struct iscsi_bhs_scsi_command *bhssc;
2639	struct iscsi_bhs_scsi_response *bhssr;
2640#ifdef DIAGNOSTIC
2641	struct cfiscsi_data_wait *cdw;
2642#endif
2643	struct cfiscsi_session *cs;
2644	uint16_t sense_length;
2645
2646	request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2647	cs = PDU_SESSION(request);
2648	bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
2649	KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2650	    ISCSI_BHS_OPCODE_SCSI_COMMAND,
2651	    ("replying to wrong opcode 0x%x", bhssc->bhssc_opcode));
2652
2653	//CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x",
2654	//    bhssc->bhssc_initiator_task_tag);
2655
2656#ifdef DIAGNOSTIC
2657	CFISCSI_SESSION_LOCK(cs);
2658	TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next)
2659		KASSERT(bhssc->bhssc_initiator_task_tag !=
2660		    cdw->cdw_initiator_task_tag, ("dangling cdw"));
2661	CFISCSI_SESSION_UNLOCK(cs);
2662#endif
2663
2664	response = cfiscsi_pdu_new_response(request, M_WAITOK);
2665	bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
2666	bhssr->bhssr_opcode = ISCSI_BHS_OPCODE_SCSI_RESPONSE;
2667	bhssr->bhssr_flags = 0x80;
2668	/*
2669	 * XXX: We don't deal with bidirectional under/overflows;
2670	 *	does anything actually support those?
2671	 */
2672	if (PDU_TOTAL_TRANSFER_LEN(request) <
2673	    ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2674		bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_UNDERFLOW;
2675		bhssr->bhssr_residual_count =
2676		    htonl(ntohl(bhssc->bhssc_expected_data_transfer_length) -
2677		    PDU_TOTAL_TRANSFER_LEN(request));
2678		//CFISCSI_SESSION_DEBUG(cs, "underflow; residual count %d",
2679		//    ntohl(bhssr->bhssr_residual_count));
2680	} else if (PDU_TOTAL_TRANSFER_LEN(request) >
2681	    ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2682		bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_OVERFLOW;
2683		bhssr->bhssr_residual_count =
2684		    htonl(PDU_TOTAL_TRANSFER_LEN(request) -
2685		    ntohl(bhssc->bhssc_expected_data_transfer_length));
2686		//CFISCSI_SESSION_DEBUG(cs, "overflow; residual count %d",
2687		//    ntohl(bhssr->bhssr_residual_count));
2688	}
2689	bhssr->bhssr_response = BHSSR_RESPONSE_COMMAND_COMPLETED;
2690	bhssr->bhssr_status = io->scsiio.scsi_status;
2691	bhssr->bhssr_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2692	bhssr->bhssr_expdatasn = htonl(PDU_EXPDATASN(request));
2693
2694	if (io->scsiio.sense_len > 0) {
2695#if 0
2696		CFISCSI_SESSION_DEBUG(cs, "returning %d bytes of sense data",
2697		    io->scsiio.sense_len);
2698#endif
2699		sense_length = htons(io->scsiio.sense_len);
2700		icl_pdu_append_data(response,
2701		    &sense_length, sizeof(sense_length), M_WAITOK);
2702		icl_pdu_append_data(response,
2703		    &io->scsiio.sense_data, io->scsiio.sense_len, M_WAITOK);
2704	}
2705
2706	ctl_free_io(io);
2707	icl_pdu_free(request);
2708	cfiscsi_pdu_queue(response);
2709}
2710
2711static void
2712cfiscsi_task_management_done(union ctl_io *io)
2713{
2714	struct icl_pdu *request, *response;
2715	struct iscsi_bhs_task_management_request *bhstmr;
2716	struct iscsi_bhs_task_management_response *bhstmr2;
2717	struct cfiscsi_data_wait *cdw, *tmpcdw;
2718	struct cfiscsi_session *cs;
2719
2720	request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2721	cs = PDU_SESSION(request);
2722	bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
2723	KASSERT((bhstmr->bhstmr_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2724	    ISCSI_BHS_OPCODE_TASK_REQUEST,
2725	    ("replying to wrong opcode 0x%x", bhstmr->bhstmr_opcode));
2726
2727#if 0
2728	CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x; referenced task tag 0x%x",
2729	    bhstmr->bhstmr_initiator_task_tag,
2730	    bhstmr->bhstmr_referenced_task_tag);
2731#endif
2732
2733	if ((bhstmr->bhstmr_function & ~0x80) ==
2734	    BHSTMR_FUNCTION_ABORT_TASK) {
2735		/*
2736		 * Make sure we no longer wait for Data-Out for this command.
2737		 */
2738		CFISCSI_SESSION_LOCK(cs);
2739		TAILQ_FOREACH_SAFE(cdw,
2740		    &cs->cs_waiting_for_data_out, cdw_next, tmpcdw) {
2741			if (bhstmr->bhstmr_referenced_task_tag !=
2742			    cdw->cdw_initiator_task_tag)
2743				continue;
2744
2745#if 0
2746			CFISCSI_SESSION_DEBUG(cs, "removing csw for initiator task "
2747			    "tag 0x%x", bhstmr->bhstmr_initiator_task_tag);
2748#endif
2749			TAILQ_REMOVE(&cs->cs_waiting_for_data_out,
2750			    cdw, cdw_next);
2751			cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io);
2752			uma_zfree(cfiscsi_data_wait_zone, cdw);
2753		}
2754		CFISCSI_SESSION_UNLOCK(cs);
2755	}
2756
2757	response = cfiscsi_pdu_new_response(request, M_WAITOK);
2758	bhstmr2 = (struct iscsi_bhs_task_management_response *)
2759	    response->ip_bhs;
2760	bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE;
2761	bhstmr2->bhstmr_flags = 0x80;
2762	if (io->io_hdr.status == CTL_SUCCESS) {
2763		bhstmr2->bhstmr_response = BHSTMR_RESPONSE_FUNCTION_COMPLETE;
2764	} else {
2765		/*
2766		 * XXX: How to figure out what exactly went wrong?  iSCSI spec
2767		 * 	expects us to provide detailed error, e.g. "Task does
2768		 * 	not exist" or "LUN does not exist".
2769		 */
2770		CFISCSI_SESSION_DEBUG(cs, "BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED");
2771		bhstmr2->bhstmr_response =
2772		    BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED;
2773	}
2774	bhstmr2->bhstmr_initiator_task_tag = bhstmr->bhstmr_initiator_task_tag;
2775
2776	ctl_free_io(io);
2777	icl_pdu_free(request);
2778	cfiscsi_pdu_queue(response);
2779}
2780
2781static void
2782cfiscsi_done(union ctl_io *io)
2783{
2784	struct icl_pdu *request;
2785	struct cfiscsi_session *cs;
2786
2787	KASSERT(((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE),
2788		("invalid CTL status %#x", io->io_hdr.status));
2789
2790	request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2791	if (request == NULL) {
2792		/*
2793		 * Implicit task termination has just completed; nothing to do.
2794		 */
2795		return;
2796	}
2797
2798	cs = PDU_SESSION(request);
2799	refcount_release(&cs->cs_outstanding_ctl_pdus);
2800
2801	switch (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) {
2802	case ISCSI_BHS_OPCODE_SCSI_COMMAND:
2803		cfiscsi_scsi_command_done(io);
2804		break;
2805	case ISCSI_BHS_OPCODE_TASK_REQUEST:
2806		cfiscsi_task_management_done(io);
2807		break;
2808	default:
2809		panic("cfiscsi_done called with wrong opcode 0x%x",
2810		    request->ip_bhs->bhs_opcode);
2811	}
2812}
2813