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