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