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