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