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