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