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