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