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