ctl_frontend_iscsi.c revision 268554
1255570Strasz/*-
2255570Strasz * Copyright (c) 2012 The FreeBSD Foundation
3255570Strasz * All rights reserved.
4255570Strasz *
5255570Strasz * This software was developed by Edward Tomasz Napierala under sponsorship
6255570Strasz * from the FreeBSD Foundation.
7255570Strasz *
8255570Strasz * Redistribution and use in source and binary forms, with or without
9255570Strasz * modification, are permitted provided that the following conditions
10255570Strasz * are met:
11255570Strasz * 1. Redistributions of source code must retain the above copyright
12255570Strasz *    notice, this list of conditions and the following disclaimer.
13255570Strasz * 2. Redistributions in binary form must reproduce the above copyright
14255570Strasz *    notice, this list of conditions and the following disclaimer in the
15255570Strasz *    documentation and/or other materials provided with the distribution.
16255570Strasz *
17255570Strasz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18255570Strasz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19255570Strasz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20255570Strasz * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21255570Strasz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22255570Strasz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23255570Strasz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24255570Strasz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25255570Strasz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26255570Strasz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27255570Strasz * SUCH DAMAGE.
28255570Strasz *
29255570Strasz * $FreeBSD: stable/10/sys/cam/ctl/ctl_frontend_iscsi.c 268554 2014-07-12 02:33:03Z mav $
30255570Strasz */
31255570Strasz
32255570Strasz/*
33255570Strasz * CTL frontend for the iSCSI protocol.
34255570Strasz */
35255570Strasz
36255570Strasz#include <sys/cdefs.h>
37255570Strasz__FBSDID("$FreeBSD: stable/10/sys/cam/ctl/ctl_frontend_iscsi.c 268554 2014-07-12 02:33:03Z mav $");
38255570Strasz
39255570Strasz#include <sys/param.h>
40255570Strasz#include <sys/capability.h>
41255570Strasz#include <sys/condvar.h>
42255570Strasz#include <sys/file.h>
43255570Strasz#include <sys/kernel.h>
44255570Strasz#include <sys/kthread.h>
45255570Strasz#include <sys/lock.h>
46255570Strasz#include <sys/malloc.h>
47255570Strasz#include <sys/module.h>
48255570Strasz#include <sys/mutex.h>
49255570Strasz#include <sys/queue.h>
50255570Strasz#include <sys/sbuf.h>
51255570Strasz#include <sys/sysctl.h>
52255570Strasz#include <sys/systm.h>
53255570Strasz#include <sys/uio.h>
54255570Strasz#include <sys/unistd.h>
55255570Strasz#include <vm/uma.h>
56255570Strasz
57255570Strasz#include <cam/scsi/scsi_all.h>
58255570Strasz#include <cam/scsi/scsi_da.h>
59255570Strasz#include <cam/ctl/ctl_io.h>
60255570Strasz#include <cam/ctl/ctl.h>
61255570Strasz#include <cam/ctl/ctl_backend.h>
62255570Strasz#include <cam/ctl/ctl_error.h>
63255570Strasz#include <cam/ctl/ctl_frontend.h>
64255570Strasz#include <cam/ctl/ctl_frontend_internal.h>
65255570Strasz#include <cam/ctl/ctl_debug.h>
66255570Strasz#include <cam/ctl/ctl_ha.h>
67255570Strasz#include <cam/ctl/ctl_ioctl.h>
68255570Strasz#include <cam/ctl/ctl_private.h>
69255570Strasz
70255570Strasz#include "../../dev/iscsi/icl.h"
71255570Strasz#include "../../dev/iscsi/iscsi_proto.h"
72255570Strasz#include "ctl_frontend_iscsi.h"
73255570Strasz
74255570Strasz#ifdef ICL_KERNEL_PROXY
75255570Strasz#include <sys/socketvar.h>
76255570Strasz#endif
77255570Strasz
78255570Strasz#ifdef ICL_KERNEL_PROXY
79255570StraszFEATURE(cfiscsi_kernel_proxy, "iSCSI target built with ICL_KERNEL_PROXY");
80255570Strasz#endif
81255570Strasz
82255570Straszstatic MALLOC_DEFINE(M_CFISCSI, "cfiscsi", "Memory used for CTL iSCSI frontend");
83255570Straszstatic uma_zone_t cfiscsi_data_wait_zone;
84255570Strasz
85255570StraszSYSCTL_NODE(_kern_cam_ctl, OID_AUTO, iscsi, CTLFLAG_RD, 0,
86255570Strasz    "CAM Target Layer iSCSI Frontend");
87255570Straszstatic int debug = 3;
88255570StraszTUNABLE_INT("kern.cam.ctl.iscsi.debug", &debug);
89255570StraszSYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, debug, CTLFLAG_RWTUN,
90255570Strasz    &debug, 1, "Enable debug messages");
91255570Straszstatic int ping_timeout = 5;
92255570StraszTUNABLE_INT("kern.cam.ctl.iscsi.ping_timeout", &ping_timeout);
93255570StraszSYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, ping_timeout, CTLFLAG_RWTUN,
94255570Strasz    &ping_timeout, 5, "Interval between ping (NOP-Out) requests, in seconds");
95255570Straszstatic int login_timeout = 60;
96255570StraszTUNABLE_INT("kern.cam.ctl.iscsi.login_timeout", &login_timeout);
97255570StraszSYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, login_timeout, CTLFLAG_RWTUN,
98255570Strasz    &login_timeout, 60, "Time to wait for ctld(8) to finish Login Phase, in seconds");
99255570Straszstatic int maxcmdsn_delta = 256;
100255570StraszTUNABLE_INT("kern.cam.ctl.iscsi.maxcmdsn_delta", &maxcmdsn_delta);
101255570StraszSYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, maxcmdsn_delta, CTLFLAG_RWTUN,
102255570Strasz    &maxcmdsn_delta, 256, "Number of commands the initiator can send "
103255570Strasz    "without confirmation");
104255570Strasz
105255570Strasz#define	CFISCSI_DEBUG(X, ...)						\
106255570Strasz	do {								\
107255570Strasz		if (debug > 1) {					\
108255570Strasz			printf("%s: " X "\n",				\
109255570Strasz			    __func__, ## __VA_ARGS__);			\
110255570Strasz		}							\
111255570Strasz	} while (0)
112255570Strasz
113255570Strasz#define	CFISCSI_WARN(X, ...)						\
114255570Strasz	do {								\
115255570Strasz		if (debug > 0) {					\
116255570Strasz			printf("WARNING: %s: " X "\n",			\
117255570Strasz			    __func__, ## __VA_ARGS__);			\
118255570Strasz		}							\
119255570Strasz	} while (0)
120255570Strasz
121255570Strasz#define	CFISCSI_SESSION_DEBUG(S, X, ...)				\
122255570Strasz	do {								\
123255570Strasz		if (debug > 1) {					\
124255570Strasz			printf("%s: %s (%s): " X "\n",			\
125255570Strasz			    __func__, S->cs_initiator_addr,		\
126255570Strasz			    S->cs_initiator_name, ## __VA_ARGS__);	\
127255570Strasz		}							\
128255570Strasz	} while (0)
129255570Strasz
130255570Strasz#define	CFISCSI_SESSION_WARN(S, X, ...)					\
131255570Strasz	do  {								\
132255570Strasz		if (debug > 0) {					\
133255570Strasz			printf("WARNING: %s (%s): " X "\n",		\
134255570Strasz			    S->cs_initiator_addr,			\
135255570Strasz			    S->cs_initiator_name, ## __VA_ARGS__);	\
136255570Strasz		}							\
137255570Strasz	} while (0)
138255570Strasz
139255570Strasz#define CFISCSI_SESSION_LOCK(X)		mtx_lock(&X->cs_lock)
140255570Strasz#define CFISCSI_SESSION_UNLOCK(X)	mtx_unlock(&X->cs_lock)
141255570Strasz#define CFISCSI_SESSION_LOCK_ASSERT(X)	mtx_assert(&X->cs_lock, MA_OWNED)
142255570Strasz
143255570Strasz#define	CONN_SESSION(X)			((struct cfiscsi_session *)(X)->ic_prv0)
144255570Strasz#define	PDU_SESSION(X)			CONN_SESSION((X)->ip_conn)
145255570Strasz#define	PDU_EXPDATASN(X)		(X)->ip_prv0
146255570Strasz#define	PDU_TOTAL_TRANSFER_LEN(X)	(X)->ip_prv1
147255570Strasz#define	PDU_R2TSN(X)			(X)->ip_prv2
148255570Strasz
149255570Straszint		cfiscsi_init(void);
150255848Straszstatic void	cfiscsi_online(void *arg);
151255570Straszstatic void	cfiscsi_offline(void *arg);
152255570Straszstatic int	cfiscsi_targ_enable(void *arg, struct ctl_id targ_id);
153255570Straszstatic int	cfiscsi_targ_disable(void *arg, struct ctl_id targ_id);
154255570Straszstatic int	cfiscsi_lun_enable(void *arg,
155255570Strasz		    struct ctl_id target_id, int lun_id);
156255570Straszstatic int	cfiscsi_lun_disable(void *arg,
157255570Strasz		    struct ctl_id target_id, int lun_id);
158255570Straszstatic int	cfiscsi_ioctl(struct cdev *dev,
159255570Strasz		    u_long cmd, caddr_t addr, int flag, struct thread *td);
160255570Straszstatic int	cfiscsi_devid(struct ctl_scsiio *ctsio, int alloc_len);
161255570Straszstatic void	cfiscsi_datamove(union ctl_io *io);
162255570Straszstatic void	cfiscsi_done(union ctl_io *io);
163255570Straszstatic uint32_t	cfiscsi_map_lun(void *arg, uint32_t lun);
164255570Straszstatic bool	cfiscsi_pdu_update_cmdsn(const struct icl_pdu *request);
165255570Straszstatic void	cfiscsi_pdu_handle_nop_out(struct icl_pdu *request);
166255570Straszstatic void	cfiscsi_pdu_handle_scsi_command(struct icl_pdu *request);
167255570Straszstatic void	cfiscsi_pdu_handle_task_request(struct icl_pdu *request);
168255570Straszstatic void	cfiscsi_pdu_handle_data_out(struct icl_pdu *request);
169255570Straszstatic void	cfiscsi_pdu_handle_logout_request(struct icl_pdu *request);
170255570Straszstatic void	cfiscsi_session_terminate(struct cfiscsi_session *cs);
171255570Straszstatic struct cfiscsi_target	*cfiscsi_target_find(struct cfiscsi_softc
172255570Strasz		    *softc, const char *name);
173255570Straszstatic void	cfiscsi_target_release(struct cfiscsi_target *ct);
174255570Straszstatic void	cfiscsi_session_delete(struct cfiscsi_session *cs);
175255570Strasz
176255570Straszstatic struct cfiscsi_softc cfiscsi_softc;
177255570Straszextern struct ctl_softc *control_softc;
178255570Strasz
179255570Straszstatic int cfiscsi_module_event_handler(module_t, int /*modeventtype_t*/, void *);
180255570Strasz
181255570Straszstatic moduledata_t cfiscsi_moduledata = {
182255570Strasz	"ctlcfiscsi",
183255570Strasz	cfiscsi_module_event_handler,
184255570Strasz	NULL
185255848Strasz};
186255570Strasz
187255570StraszDECLARE_MODULE(ctlcfiscsi, cfiscsi_moduledata, SI_SUB_CONFIGURE, SI_ORDER_FOURTH);
188255570StraszMODULE_VERSION(ctlcfiscsi, 1);
189255570StraszMODULE_DEPEND(ctlcfiscsi, ctl, 1, 1, 1);
190255570StraszMODULE_DEPEND(ctlcfiscsi, icl, 1, 1, 1);
191255570Strasz
192255570Straszstatic struct icl_pdu *
193255570Straszcfiscsi_pdu_new_response(struct icl_pdu *request, int flags)
194255570Strasz{
195255570Strasz
196255570Strasz	return (icl_pdu_new_bhs(request->ip_conn, flags));
197255570Strasz}
198255570Strasz
199255570Straszstatic bool
200255570Straszcfiscsi_pdu_update_cmdsn(const struct icl_pdu *request)
201255570Strasz{
202255570Strasz	const struct iscsi_bhs_scsi_command *bhssc;
203255570Strasz	struct cfiscsi_session *cs;
204255570Strasz	uint32_t cmdsn, expstatsn;
205255570Strasz
206255570Strasz	cs = PDU_SESSION(request);
207255570Strasz
208255570Strasz	/*
209255848Strasz	 * Every incoming PDU - not just NOP-Out - resets the ping timer.
210255570Strasz	 * The purpose of the timeout is to reset the connection when it stalls;
211255570Strasz	 * we don't want this to happen when NOP-In or NOP-Out ends up delayed
212255570Strasz	 * in some queue.
213255570Strasz	 *
214255570Strasz	 * XXX: Locking?
215255570Strasz	 */
216255570Strasz	cs->cs_timeout = 0;
217255570Strasz
218255570Strasz	/*
219255570Strasz	 * Data-Out PDUs don't contain CmdSN.
220255570Strasz	 */
221255570Strasz	if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
222255570Strasz	    ISCSI_BHS_OPCODE_SCSI_DATA_OUT)
223255570Strasz		return (false);
224255570Strasz
225255570Strasz	/*
226255570Strasz	 * We're only using fields common for all the request
227255570Strasz	 * (initiator -> target) PDUs.
228255570Strasz	 */
229255848Strasz	bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
230255848Strasz	cmdsn = ntohl(bhssc->bhssc_cmdsn);
231255848Strasz	expstatsn = ntohl(bhssc->bhssc_expstatsn);
232255848Strasz
233255570Strasz	CFISCSI_SESSION_LOCK(cs);
234255848Strasz#if 0
235255848Strasz	if (expstatsn != cs->cs_statsn) {
236255570Strasz		CFISCSI_SESSION_DEBUG(cs, "received PDU with ExpStatSN %d, "
237255570Strasz		    "while current StatSN is %d", expstatsn,
238255848Strasz		    cs->cs_statsn);
239255570Strasz	}
240255570Strasz#endif
241255570Strasz
242255570Strasz	/*
243255570Strasz	 * The target MUST silently ignore any non-immediate command outside
244255570Strasz	 * of this range.
245255848Strasz	 */
246255848Strasz	if (cmdsn < cs->cs_cmdsn || cmdsn > cs->cs_cmdsn + maxcmdsn_delta) {
247255570Strasz		CFISCSI_SESSION_UNLOCK(cs);
248255570Strasz		CFISCSI_SESSION_WARN(cs, "received PDU with CmdSN %d, "
249255570Strasz		    "while expected CmdSN was %d", cmdsn, cs->cs_cmdsn);
250255570Strasz		return (true);
251255570Strasz	}
252255570Strasz
253255848Strasz	if ((request->ip_bhs->bhs_opcode & ISCSI_BHS_OPCODE_IMMEDIATE) == 0)
254255570Strasz		cs->cs_cmdsn++;
255255570Strasz
256255570Strasz	CFISCSI_SESSION_UNLOCK(cs);
257255848Strasz
258255848Strasz	return (false);
259255848Strasz}
260255848Strasz
261255848Straszstatic void
262255570Straszcfiscsi_pdu_handle(struct icl_pdu *request)
263255570Strasz{
264255570Strasz	struct cfiscsi_session *cs;
265255570Strasz	bool ignore;
266255570Strasz
267255570Strasz	cs = PDU_SESSION(request);
268255570Strasz
269255570Strasz	ignore = cfiscsi_pdu_update_cmdsn(request);
270255570Strasz	if (ignore) {
271255570Strasz		icl_pdu_free(request);
272255570Strasz		return;
273255570Strasz	}
274255570Strasz
275255570Strasz	/*
276255570Strasz	 * Handle the PDU; this includes e.g. receiving the remaining
277255570Strasz	 * part of PDU and submitting the SCSI command to CTL
278255570Strasz	 * or queueing a reply.  The handling routine is responsible
279255570Strasz	 * for freeing the PDU when it's no longer needed.
280255570Strasz	 */
281255570Strasz	switch (request->ip_bhs->bhs_opcode &
282255570Strasz	    ~ISCSI_BHS_OPCODE_IMMEDIATE) {
283255570Strasz	case ISCSI_BHS_OPCODE_NOP_OUT:
284255570Strasz		cfiscsi_pdu_handle_nop_out(request);
285255570Strasz		break;
286255570Strasz	case ISCSI_BHS_OPCODE_SCSI_COMMAND:
287255570Strasz		cfiscsi_pdu_handle_scsi_command(request);
288255570Strasz		break;
289255570Strasz	case ISCSI_BHS_OPCODE_TASK_REQUEST:
290255570Strasz		cfiscsi_pdu_handle_task_request(request);
291255570Strasz		break;
292255570Strasz	case ISCSI_BHS_OPCODE_SCSI_DATA_OUT:
293255570Strasz		cfiscsi_pdu_handle_data_out(request);
294255570Strasz		break;
295255570Strasz	case ISCSI_BHS_OPCODE_LOGOUT_REQUEST:
296255570Strasz		cfiscsi_pdu_handle_logout_request(request);
297255570Strasz		break;
298255570Strasz	default:
299255570Strasz		CFISCSI_SESSION_WARN(cs, "received PDU with unsupported "
300255570Strasz		    "opcode 0x%x; dropping connection",
301255570Strasz		    request->ip_bhs->bhs_opcode);
302255570Strasz		icl_pdu_free(request);
303255570Strasz		cfiscsi_session_terminate(cs);
304255570Strasz	}
305255570Strasz
306255570Strasz}
307255570Strasz
308255570Straszstatic void
309255570Straszcfiscsi_receive_callback(struct icl_pdu *request)
310255570Strasz{
311255570Strasz	struct cfiscsi_session *cs;
312255570Strasz
313255570Strasz	cs = PDU_SESSION(request);
314255570Strasz
315255570Strasz#ifdef ICL_KERNEL_PROXY
316255570Strasz	if (cs->cs_waiting_for_ctld || cs->cs_login_phase) {
317255570Strasz		if (cs->cs_login_pdu == NULL)
318255570Strasz			cs->cs_login_pdu = request;
319255570Strasz		else
320255570Strasz			icl_pdu_free(request);
321255570Strasz		cv_signal(&cs->cs_login_cv);
322255570Strasz		return;
323255570Strasz	}
324255570Strasz#endif
325255570Strasz
326255570Strasz	cfiscsi_pdu_handle(request);
327255570Strasz}
328255570Strasz
329255570Straszstatic void
330255570Straszcfiscsi_error_callback(struct icl_conn *ic)
331255570Strasz{
332255570Strasz	struct cfiscsi_session *cs;
333255570Strasz
334255570Strasz	cs = CONN_SESSION(ic);
335255570Strasz
336255570Strasz	CFISCSI_SESSION_WARN(cs, "connection error; dropping connection");
337255570Strasz	cfiscsi_session_terminate(cs);
338255570Strasz}
339255570Strasz
340255570Straszstatic int
341255570Straszcfiscsi_pdu_prepare(struct icl_pdu *response)
342255570Strasz{
343255570Strasz	struct cfiscsi_session *cs;
344255570Strasz	struct iscsi_bhs_scsi_response *bhssr;
345255570Strasz	bool advance_statsn = true;
346255570Strasz
347255570Strasz	cs = PDU_SESSION(response);
348255570Strasz
349255570Strasz	CFISCSI_SESSION_LOCK_ASSERT(cs);
350255570Strasz
351255570Strasz	/*
352255570Strasz	 * We're only using fields common for all the response
353255570Strasz	 * (target -> initiator) PDUs.
354255570Strasz	 */
355255570Strasz	bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
356255570Strasz
357255570Strasz	/*
358255570Strasz	 * 10.8.3: "The StatSN for this connection is not advanced
359255570Strasz	 * after this PDU is sent."
360255570Strasz	 */
361255570Strasz	if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_R2T)
362255570Strasz		advance_statsn = false;
363255570Strasz
364255570Strasz	/*
365255570Strasz	 * 10.19.2: "However, when the Initiator Task Tag is set to 0xffffffff,
366255570Strasz	 * StatSN for the connection is not advanced after this PDU is sent."
367255570Strasz	 */
368255570Strasz	if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_NOP_IN &&
369255570Strasz	    bhssr->bhssr_initiator_task_tag == 0xffffffff)
370255570Strasz		advance_statsn = false;
371255570Strasz
372255570Strasz	/*
373255570Strasz	 * See the comment below - StatSN is not meaningful and must
374255570Strasz	 * not be advanced.
375255570Strasz	 */
376255570Strasz	if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_SCSI_DATA_IN)
377255570Strasz		advance_statsn = false;
378255570Strasz
379255570Strasz	/*
380255570Strasz	 * 10.7.3: "The fields StatSN, Status, and Residual Count
381255570Strasz	 * only have meaningful content if the S bit is set to 1."
382255570Strasz	 */
383255570Strasz	if (bhssr->bhssr_opcode != ISCSI_BHS_OPCODE_SCSI_DATA_IN)
384255570Strasz		bhssr->bhssr_statsn = htonl(cs->cs_statsn);
385255570Strasz	bhssr->bhssr_expcmdsn = htonl(cs->cs_cmdsn);
386255570Strasz	bhssr->bhssr_maxcmdsn = htonl(cs->cs_cmdsn + maxcmdsn_delta);
387255570Strasz
388255570Strasz	if (advance_statsn)
389255570Strasz		cs->cs_statsn++;
390255570Strasz
391255570Strasz	return (0);
392255570Strasz}
393255570Strasz
394255570Straszstatic void
395255570Straszcfiscsi_pdu_queue(struct icl_pdu *response)
396255570Strasz{
397255570Strasz	struct cfiscsi_session *cs;
398255570Strasz
399255570Strasz	cs = PDU_SESSION(response);
400255570Strasz
401255570Strasz	CFISCSI_SESSION_LOCK(cs);
402255570Strasz	cfiscsi_pdu_prepare(response);
403255570Strasz	icl_pdu_queue(response);
404255570Strasz	CFISCSI_SESSION_UNLOCK(cs);
405255570Strasz}
406255570Strasz
407255570Straszstatic uint32_t
408255570Straszcfiscsi_decode_lun(uint64_t encoded)
409255570Strasz{
410255570Strasz	uint8_t lun[8];
411255570Strasz	uint32_t result;
412255570Strasz
413255570Strasz	/*
414255570Strasz	 * The LUN field in iSCSI PDUs may look like an ordinary 64 bit number,
415255570Strasz	 * but is in fact an evil, multidimensional structure defined
416255570Strasz	 * in SCSI Architecture Model 5 (SAM-5), section 4.6.
417255570Strasz	 */
418255570Strasz	memcpy(lun, &encoded, sizeof(lun));
419255570Strasz	switch (lun[0] & 0xC0) {
420255570Strasz	case 0x00:
421255570Strasz		if ((lun[0] & 0x3f) != 0 || lun[2] != 0 || lun[3] != 0 ||
422255570Strasz		    lun[4] != 0 || lun[5] != 0 || lun[6] != 0 || lun[7] != 0) {
423255570Strasz			CFISCSI_WARN("malformed LUN "
424255570Strasz			    "(peripheral device addressing method): 0x%jx",
425255570Strasz			    (uintmax_t)encoded);
426255570Strasz			result = 0xffffffff;
427255570Strasz			break;
428255570Strasz		}
429255570Strasz		result = lun[1];
430255570Strasz		break;
431255570Strasz	case 0x40:
432255570Strasz		if (lun[2] != 0 || lun[3] != 0 || lun[4] != 0 || lun[5] != 0 ||
433255570Strasz		    lun[6] != 0 || lun[7] != 0) {
434255570Strasz			CFISCSI_WARN("malformed LUN "
435255570Strasz			    "(flat address space addressing method): 0x%jx",
436255570Strasz			    (uintmax_t)encoded);
437255570Strasz			result = 0xffffffff;
438255570Strasz			break;
439255570Strasz		}
440255570Strasz		result = ((lun[0] & 0x3f) << 8) + lun[1];
441255570Strasz		break;
442255570Strasz	case 0xC0:
443255570Strasz		if (lun[0] != 0xD2 || lun[4] != 0 || lun[5] != 0 ||
444255570Strasz		    lun[6] != 0 || lun[7] != 0) {
445255570Strasz			CFISCSI_WARN("malformed LUN (extended flat "
446255570Strasz			    "address space addressing method): 0x%jx",
447255570Strasz			    (uintmax_t)encoded);
448255570Strasz			result = 0xffffffff;
449255570Strasz			break;
450255570Strasz		}
451255570Strasz		result = (lun[1] << 16) + (lun[2] << 8) + lun[3];
452255570Strasz	default:
453255570Strasz		CFISCSI_WARN("unsupported LUN format 0x%jx",
454255570Strasz		    (uintmax_t)encoded);
455255570Strasz		result = 0xffffffff;
456255570Strasz		break;
457255570Strasz	}
458255570Strasz
459255570Strasz	return (result);
460255570Strasz}
461255570Strasz
462255570Straszstatic void
463255570Straszcfiscsi_pdu_handle_nop_out(struct icl_pdu *request)
464255570Strasz{
465255570Strasz	struct cfiscsi_session *cs;
466255570Strasz	struct iscsi_bhs_nop_out *bhsno;
467255570Strasz	struct iscsi_bhs_nop_in *bhsni;
468255570Strasz	struct icl_pdu *response;
469255570Strasz	void *data = NULL;
470255570Strasz	size_t datasize;
471255570Strasz	int error;
472255570Strasz
473255570Strasz	cs = PDU_SESSION(request);
474255570Strasz	bhsno = (struct iscsi_bhs_nop_out *)request->ip_bhs;
475255570Strasz
476255570Strasz	if (bhsno->bhsno_initiator_task_tag == 0xffffffff) {
477255570Strasz		/*
478255570Strasz		 * Nothing to do, iscsi_pdu_update_statsn() already
479255570Strasz		 * zeroed the timeout.
480255570Strasz		 */
481255570Strasz		icl_pdu_free(request);
482255570Strasz		return;
483255570Strasz	}
484255570Strasz
485255570Strasz	datasize = icl_pdu_data_segment_length(request);
486255570Strasz	if (datasize > 0) {
487255570Strasz		data = malloc(datasize, M_CFISCSI, M_NOWAIT | M_ZERO);
488255570Strasz		if (data == NULL) {
489255570Strasz			CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
490255570Strasz			    "dropping connection");
491255570Strasz			icl_pdu_free(request);
492255570Strasz			cfiscsi_session_terminate(cs);
493255570Strasz			return;
494255570Strasz		}
495255570Strasz		icl_pdu_get_data(request, 0, data, datasize);
496255570Strasz	}
497255570Strasz
498255570Strasz	response = cfiscsi_pdu_new_response(request, M_NOWAIT);
499255570Strasz	if (response == NULL) {
500255570Strasz		CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
501255570Strasz		    "droppping connection");
502255570Strasz		free(data, M_CFISCSI);
503255570Strasz		icl_pdu_free(request);
504255570Strasz		cfiscsi_session_terminate(cs);
505255570Strasz		return;
506255570Strasz	}
507255570Strasz	bhsni = (struct iscsi_bhs_nop_in *)response->ip_bhs;
508255570Strasz	bhsni->bhsni_opcode = ISCSI_BHS_OPCODE_NOP_IN;
509255570Strasz	bhsni->bhsni_flags = 0x80;
510255570Strasz	bhsni->bhsni_initiator_task_tag = bhsno->bhsno_initiator_task_tag;
511255570Strasz	bhsni->bhsni_target_transfer_tag = 0xffffffff;
512255570Strasz	if (datasize > 0) {
513255570Strasz		error = icl_pdu_append_data(response, data, datasize, M_NOWAIT);
514255570Strasz		if (error != 0) {
515255570Strasz			CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
516255570Strasz			    "dropping connection");
517255570Strasz			free(data, M_CFISCSI);
518255570Strasz			icl_pdu_free(request);
519255570Strasz			icl_pdu_free(response);
520255570Strasz			cfiscsi_session_terminate(cs);
521255570Strasz			return;
522255570Strasz		}
523255570Strasz		free(data, M_CFISCSI);
524255570Strasz	}
525255570Strasz
526255570Strasz	icl_pdu_free(request);
527255570Strasz	cfiscsi_pdu_queue(response);
528255570Strasz}
529255570Strasz
530255570Straszstatic void
531255570Straszcfiscsi_pdu_handle_scsi_command(struct icl_pdu *request)
532255570Strasz{
533255570Strasz	struct iscsi_bhs_scsi_command *bhssc;
534255570Strasz	struct cfiscsi_session *cs;
535255570Strasz	union ctl_io *io;
536255570Strasz	int error;
537255570Strasz
538255570Strasz	cs = PDU_SESSION(request);
539255570Strasz	bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
540255570Strasz	//CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x",
541255570Strasz	//    bhssc->bhssc_initiator_task_tag);
542255570Strasz
543255570Strasz	if (request->ip_data_len > 0 && cs->cs_immediate_data == false) {
544255570Strasz		CFISCSI_SESSION_WARN(cs, "unsolicited data with "
545255570Strasz		    "ImmediateData=No; dropping connection");
546255570Strasz		icl_pdu_free(request);
547255570Strasz		cfiscsi_session_terminate(cs);
548255570Strasz		return;
549255570Strasz	}
550255570Strasz	io = ctl_alloc_io(cs->cs_target->ct_softc->fe.ctl_pool_ref);
551255570Strasz	if (io == NULL) {
552255570Strasz		CFISCSI_SESSION_WARN(cs, "can't allocate ctl_io; "
553255570Strasz		    "dropping connection");
554255570Strasz		icl_pdu_free(request);
555255570Strasz		cfiscsi_session_terminate(cs);
556255570Strasz		return;
557255570Strasz	}
558255570Strasz	ctl_zero_io(io);
559255570Strasz	io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = request;
560255570Strasz	io->io_hdr.io_type = CTL_IO_SCSI;
561255570Strasz	io->io_hdr.nexus.initid.id = cs->cs_ctl_initid;
562255570Strasz	io->io_hdr.nexus.targ_port = cs->cs_target->ct_softc->fe.targ_port;
563255570Strasz	io->io_hdr.nexus.targ_target.id = 0;
564255570Strasz	io->io_hdr.nexus.targ_lun = cfiscsi_decode_lun(bhssc->bhssc_lun);
565255570Strasz	io->io_hdr.nexus.lun_map_fn = cfiscsi_map_lun;
566255570Strasz	io->io_hdr.nexus.lun_map_arg = cs;
567255570Strasz	io->scsiio.tag_num = bhssc->bhssc_initiator_task_tag;
568255570Strasz	switch ((bhssc->bhssc_flags & BHSSC_FLAGS_ATTR)) {
569255570Strasz	case BHSSC_FLAGS_ATTR_UNTAGGED:
570255570Strasz		io->scsiio.tag_type = CTL_TAG_UNTAGGED;
571255570Strasz		break;
572255570Strasz	case BHSSC_FLAGS_ATTR_SIMPLE:
573255570Strasz		io->scsiio.tag_type = CTL_TAG_SIMPLE;
574255570Strasz		break;
575255570Strasz	case BHSSC_FLAGS_ATTR_ORDERED:
576255570Strasz        	io->scsiio.tag_type = CTL_TAG_ORDERED;
577255570Strasz		break;
578255570Strasz	case BHSSC_FLAGS_ATTR_HOQ:
579255570Strasz        	io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
580255570Strasz		break;
581255570Strasz	case BHSSC_FLAGS_ATTR_ACA:
582255570Strasz		io->scsiio.tag_type = CTL_TAG_ACA;
583255570Strasz		break;
584255570Strasz	default:
585255570Strasz		io->scsiio.tag_type = CTL_TAG_UNTAGGED;
586255570Strasz		CFISCSI_SESSION_WARN(cs, "unhandled tag type %d",
587255570Strasz		    bhssc->bhssc_flags & BHSSC_FLAGS_ATTR);
588255570Strasz		break;
589255570Strasz	}
590255570Strasz	io->scsiio.cdb_len = sizeof(bhssc->bhssc_cdb); /* Which is 16. */
591255570Strasz	memcpy(io->scsiio.cdb, bhssc->bhssc_cdb, sizeof(bhssc->bhssc_cdb));
592255570Strasz	refcount_acquire(&cs->cs_outstanding_ctl_pdus);
593255570Strasz	error = ctl_queue(io);
594255570Strasz	if (error != CTL_RETVAL_COMPLETE) {
595255570Strasz		CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d; "
596255570Strasz		    "dropping connection", error);
597255570Strasz		ctl_free_io(io);
598255570Strasz		refcount_release(&cs->cs_outstanding_ctl_pdus);
599255570Strasz		icl_pdu_free(request);
600255570Strasz		cfiscsi_session_terminate(cs);
601255570Strasz	}
602255570Strasz}
603255570Strasz
604255570Straszstatic void
605255570Straszcfiscsi_pdu_handle_task_request(struct icl_pdu *request)
606255570Strasz{
607255570Strasz	struct iscsi_bhs_task_management_request *bhstmr;
608255570Strasz	struct iscsi_bhs_task_management_response *bhstmr2;
609255570Strasz	struct icl_pdu *response;
610255570Strasz	struct cfiscsi_session *cs;
611255570Strasz	union ctl_io *io;
612255570Strasz	int error;
613255570Strasz
614255570Strasz	cs = PDU_SESSION(request);
615255570Strasz	bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
616255570Strasz	io = ctl_alloc_io(cs->cs_target->ct_softc->fe.ctl_pool_ref);
617255570Strasz	if (io == NULL) {
618255570Strasz		CFISCSI_SESSION_WARN(cs, "can't allocate ctl_io;"
619255570Strasz		    "dropping connection");
620255570Strasz		icl_pdu_free(request);
621255570Strasz		cfiscsi_session_terminate(cs);
622255570Strasz		return;
623255570Strasz	}
624255570Strasz	ctl_zero_io(io);
625255570Strasz	io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = request;
626255570Strasz	io->io_hdr.io_type = CTL_IO_TASK;
627255570Strasz	io->io_hdr.nexus.initid.id = cs->cs_ctl_initid;
628255570Strasz	io->io_hdr.nexus.targ_port = cs->cs_target->ct_softc->fe.targ_port;
629255570Strasz	io->io_hdr.nexus.targ_target.id = 0;
630255570Strasz	io->io_hdr.nexus.targ_lun = cfiscsi_decode_lun(bhstmr->bhstmr_lun);
631255570Strasz	io->io_hdr.nexus.lun_map_fn = cfiscsi_map_lun;
632255570Strasz	io->io_hdr.nexus.lun_map_arg = cs;
633255570Strasz	io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
634255570Strasz
635255570Strasz	switch (bhstmr->bhstmr_function & ~0x80) {
636255570Strasz	case BHSTMR_FUNCTION_ABORT_TASK:
637255570Strasz#if 0
638255570Strasz		CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_ABORT_TASK");
639255570Strasz#endif
640255570Strasz		io->taskio.task_action = CTL_TASK_ABORT_TASK;
641255570Strasz		io->taskio.tag_num = bhstmr->bhstmr_referenced_task_tag;
642255570Strasz		break;
643255570Strasz	case BHSTMR_FUNCTION_LOGICAL_UNIT_RESET:
644255570Strasz#if 0
645255570Strasz		CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_LOGICAL_UNIT_RESET");
646255570Strasz#endif
647255570Strasz		io->taskio.task_action = CTL_TASK_LUN_RESET;
648255570Strasz		break;
649255570Strasz	case BHSTMR_FUNCTION_TARGET_WARM_RESET:
650255570Strasz#if 0
651255570Strasz		CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_TARGET_WARM_RESET");
652255570Strasz#endif
653255570Strasz		io->taskio.task_action = CTL_TASK_TARGET_RESET;
654255570Strasz		break;
655255570Strasz	default:
656255570Strasz		CFISCSI_SESSION_DEBUG(cs, "unsupported function 0x%x",
657255570Strasz		    bhstmr->bhstmr_function & ~0x80);
658255570Strasz		ctl_free_io(io);
659255570Strasz
660255570Strasz		response = cfiscsi_pdu_new_response(request, M_NOWAIT);
661255570Strasz		if (response == NULL) {
662255570Strasz			CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
663255570Strasz			    "dropping connection");
664255570Strasz			icl_pdu_free(request);
665255570Strasz			cfiscsi_session_terminate(cs);
666255570Strasz			return;
667255570Strasz		}
668255570Strasz		bhstmr2 = (struct iscsi_bhs_task_management_response *)
669255570Strasz		    response->ip_bhs;
670255570Strasz		bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE;
671255570Strasz		bhstmr2->bhstmr_flags = 0x80;
672255570Strasz		bhstmr2->bhstmr_response =
673255570Strasz		    BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED;
674255570Strasz		bhstmr2->bhstmr_initiator_task_tag =
675255570Strasz		    bhstmr->bhstmr_initiator_task_tag;
676255570Strasz		icl_pdu_free(request);
677255570Strasz		cfiscsi_pdu_queue(response);
678255570Strasz		return;
679255570Strasz	}
680255570Strasz
681255570Strasz	refcount_acquire(&cs->cs_outstanding_ctl_pdus);
682255570Strasz	error = ctl_queue(io);
683255570Strasz	if (error != CTL_RETVAL_COMPLETE) {
684255570Strasz		CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d; "
685255570Strasz		    "dropping connection", error);
686255570Strasz		ctl_free_io(io);
687255570Strasz		refcount_release(&cs->cs_outstanding_ctl_pdus);
688255570Strasz		icl_pdu_free(request);
689255570Strasz		cfiscsi_session_terminate(cs);
690255570Strasz	}
691255570Strasz}
692255570Strasz
693255570Straszstatic bool
694255570Straszcfiscsi_handle_data_segment(struct icl_pdu *request, struct cfiscsi_data_wait *cdw)
695255570Strasz{
696255570Strasz	struct iscsi_bhs_data_out *bhsdo;
697255570Strasz	struct cfiscsi_session *cs;
698255570Strasz	struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
699255570Strasz	size_t copy_len, len, off, buffer_offset;
700255570Strasz	int ctl_sg_count;
701255570Strasz	union ctl_io *io;
702255570Strasz
703255570Strasz	cs = PDU_SESSION(request);
704255570Strasz
705255570Strasz	KASSERT((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
706255570Strasz	    ISCSI_BHS_OPCODE_SCSI_DATA_OUT ||
707255570Strasz	    (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
708255570Strasz	    ISCSI_BHS_OPCODE_SCSI_COMMAND,
709255570Strasz	    ("bad opcode 0x%x", request->ip_bhs->bhs_opcode));
710255570Strasz
711255570Strasz	/*
712255570Strasz	 * We're only using fields common for Data-Out and SCSI Command PDUs.
713255570Strasz	 */
714255570Strasz	bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs;
715255570Strasz
716255570Strasz	io = cdw->cdw_ctl_io;
717255570Strasz	KASSERT((io->io_hdr.flags & CTL_FLAG_DATA_MASK) != CTL_FLAG_DATA_IN,
718255570Strasz	    ("CTL_FLAG_DATA_IN"));
719255570Strasz
720255570Strasz#if 0
721255570Strasz	CFISCSI_SESSION_DEBUG(cs, "received %zd bytes out of %d",
722255570Strasz	    request->ip_data_len, io->scsiio.kern_total_len);
723255570Strasz#endif
724255570Strasz
725255570Strasz	if (io->scsiio.kern_sg_entries > 0) {
726255570Strasz		ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
727255570Strasz		ctl_sg_count = io->scsiio.kern_sg_entries;
728255570Strasz	} else {
729255570Strasz		ctl_sglist = &ctl_sg_entry;
730255570Strasz		ctl_sglist->addr = io->scsiio.kern_data_ptr;
731255570Strasz		ctl_sglist->len = io->scsiio.kern_data_len;
732255570Strasz		ctl_sg_count = 1;
733255570Strasz	}
734255570Strasz
735255570Strasz	if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
736255570Strasz	    ISCSI_BHS_OPCODE_SCSI_DATA_OUT)
737255570Strasz		buffer_offset = ntohl(bhsdo->bhsdo_buffer_offset);
738255570Strasz	else
739255570Strasz		buffer_offset = 0;
740255570Strasz	len = icl_pdu_data_segment_length(request);
741255570Strasz
742255570Strasz	/*
743255570Strasz	 * Make sure the offset, as sent by the initiator, matches the offset
744255570Strasz	 * we're supposed to be at in the scatter-gather list.
745255570Strasz	 */
746255570Strasz	if (buffer_offset >
747255570Strasz	    io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled ||
748255570Strasz	    buffer_offset + len <=
749255570Strasz	    io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled) {
750255570Strasz		CFISCSI_SESSION_WARN(cs, "received bad buffer offset %zd, "
751255570Strasz		    "expected %zd; dropping connection", buffer_offset,
752255570Strasz		    (size_t)io->scsiio.kern_rel_offset +
753255570Strasz		    (size_t)io->scsiio.ext_data_filled);
754255570Strasz		ctl_set_data_phase_error(&io->scsiio);
755255570Strasz		cfiscsi_session_terminate(cs);
756255570Strasz		return (true);
757255570Strasz	}
758255570Strasz
759255570Strasz	/*
760255570Strasz	 * This is the offset within the PDU data segment, as opposed
761255570Strasz	 * to buffer_offset, which is the offset within the task (SCSI
762255570Strasz	 * command).
763255570Strasz	 */
764255570Strasz	off = io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled -
765255570Strasz	    buffer_offset;
766255570Strasz
767255570Strasz	/*
768255570Strasz	 * Iterate over the scatter/gather segments, filling them with data
769255570Strasz	 * from the PDU data segment.  Note that this can get called multiple
770255570Strasz	 * times for one SCSI command; the cdw structure holds state for the
771255570Strasz	 * scatter/gather list.
772255570Strasz	 */
773255570Strasz	for (;;) {
774255570Strasz		KASSERT(cdw->cdw_sg_index < ctl_sg_count,
775255570Strasz		    ("cdw->cdw_sg_index >= ctl_sg_count"));
776255570Strasz		if (cdw->cdw_sg_len == 0) {
777255570Strasz			cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr;
778255570Strasz			cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len;
779255570Strasz		}
780255570Strasz		KASSERT(off <= len, ("len > off"));
781255570Strasz		copy_len = len - off;
782255570Strasz		if (copy_len > cdw->cdw_sg_len)
783255570Strasz			copy_len = cdw->cdw_sg_len;
784255570Strasz
785255570Strasz		icl_pdu_get_data(request, off, cdw->cdw_sg_addr, copy_len);
786255570Strasz		cdw->cdw_sg_addr += copy_len;
787255570Strasz		cdw->cdw_sg_len -= copy_len;
788255570Strasz		off += copy_len;
789255570Strasz		io->scsiio.ext_data_filled += copy_len;
790255570Strasz
791255570Strasz		if (cdw->cdw_sg_len == 0) {
792255570Strasz			/*
793255570Strasz			 * End of current segment.
794255570Strasz			 */
795255570Strasz			if (cdw->cdw_sg_index == ctl_sg_count - 1) {
796255570Strasz				/*
797255570Strasz				 * Last segment in scatter/gather list.
798255570Strasz				 */
799255570Strasz				break;
800255570Strasz			}
801255570Strasz			cdw->cdw_sg_index++;
802255570Strasz		}
803255570Strasz
804255570Strasz		if (off == len) {
805255570Strasz			/*
806255570Strasz			 * End of PDU payload.
807255570Strasz			 */
808255570Strasz			break;
809255570Strasz		}
810255570Strasz	}
811255570Strasz
812255570Strasz	if (len > off) {
813255570Strasz		/*
814255570Strasz		 * In case of unsolicited data, it's possible that the buffer
815255570Strasz		 * provided by CTL is smaller than negotiated FirstBurstLength.
816255570Strasz		 * Just ignore the superfluous data; will ask for them with R2T
817255570Strasz		 * on next call to cfiscsi_datamove().
818255570Strasz		 *
819255570Strasz		 * This obviously can only happen with SCSI Command PDU.
820255570Strasz		 */
821255570Strasz		if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
822255570Strasz		    ISCSI_BHS_OPCODE_SCSI_COMMAND)
823255570Strasz			return (true);
824255570Strasz
825255570Strasz		CFISCSI_SESSION_WARN(cs, "received too much data: got %zd bytes, "
826255570Strasz		    "expected %zd; dropping connection",
827255570Strasz		    icl_pdu_data_segment_length(request), off);
828255570Strasz		ctl_set_data_phase_error(&io->scsiio);
829255570Strasz		cfiscsi_session_terminate(cs);
830255570Strasz		return (true);
831255570Strasz	}
832255570Strasz
833255570Strasz	if (io->scsiio.ext_data_filled == io->scsiio.kern_data_len &&
834255570Strasz	    (bhsdo->bhsdo_flags & BHSDO_FLAGS_F) == 0) {
835255570Strasz		CFISCSI_SESSION_WARN(cs, "got the final packet without "
836255570Strasz		    "the F flag; flags = 0x%x; dropping connection",
837255570Strasz		    bhsdo->bhsdo_flags);
838255570Strasz		ctl_set_data_phase_error(&io->scsiio);
839255570Strasz		cfiscsi_session_terminate(cs);
840255570Strasz		return (true);
841255570Strasz	}
842255570Strasz
843255570Strasz	if (io->scsiio.ext_data_filled != io->scsiio.kern_data_len &&
844255570Strasz	    (bhsdo->bhsdo_flags & BHSDO_FLAGS_F) != 0) {
845255570Strasz		if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
846255570Strasz		    ISCSI_BHS_OPCODE_SCSI_DATA_OUT) {
847255570Strasz			CFISCSI_SESSION_WARN(cs, "got the final packet, but the "
848255570Strasz			    "transmitted size was %zd bytes instead of %d; "
849255570Strasz			    "dropping connection",
850255570Strasz			    (size_t)io->scsiio.ext_data_filled,
851255570Strasz			    io->scsiio.kern_data_len);
852255570Strasz			ctl_set_data_phase_error(&io->scsiio);
853255570Strasz			cfiscsi_session_terminate(cs);
854255570Strasz			return (true);
855255570Strasz		} else {
856255570Strasz			/*
857255570Strasz			 * For SCSI Command PDU, this just means we need to
858255570Strasz			 * solicit more data by sending R2T.
859255570Strasz			 */
860255570Strasz			return (false);
861255570Strasz		}
862255570Strasz	}
863255570Strasz
864255570Strasz	if (io->scsiio.ext_data_filled == io->scsiio.kern_data_len) {
865255570Strasz#if 0
866255570Strasz		CFISCSI_SESSION_DEBUG(cs, "no longer expecting Data-Out with target "
867255570Strasz		    "transfer tag 0x%x", cdw->cdw_target_transfer_tag);
868255570Strasz#endif
869255570Strasz
870255570Strasz		return (true);
871255570Strasz	}
872255570Strasz
873255570Strasz	return (false);
874255570Strasz}
875255570Strasz
876255570Straszstatic void
877255570Straszcfiscsi_pdu_handle_data_out(struct icl_pdu *request)
878255570Strasz{
879255570Strasz	struct iscsi_bhs_data_out *bhsdo;
880255570Strasz	struct cfiscsi_session *cs;
881255570Strasz	struct cfiscsi_data_wait *cdw = NULL;
882255570Strasz	union ctl_io *io;
883255570Strasz	bool done;
884255570Strasz
885255570Strasz	cs = PDU_SESSION(request);
886255570Strasz	bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs;
887255570Strasz
888255570Strasz	CFISCSI_SESSION_LOCK(cs);
889255570Strasz	TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next) {
890255570Strasz#if 0
891255570Strasz		CFISCSI_SESSION_DEBUG(cs, "have ttt 0x%x, itt 0x%x; looking for "
892255570Strasz		    "ttt 0x%x, itt 0x%x",
893255570Strasz		    bhsdo->bhsdo_target_transfer_tag,
894255570Strasz		    bhsdo->bhsdo_initiator_task_tag,
895255570Strasz		    cdw->cdw_target_transfer_tag, cdw->cdw_initiator_task_tag));
896255570Strasz#endif
897255570Strasz		if (bhsdo->bhsdo_target_transfer_tag ==
898255570Strasz		    cdw->cdw_target_transfer_tag)
899255570Strasz			break;
900255570Strasz	}
901255570Strasz	CFISCSI_SESSION_UNLOCK(cs);
902255570Strasz	if (cdw == NULL) {
903255570Strasz		CFISCSI_SESSION_WARN(cs, "data transfer tag 0x%x, initiator task tag "
904255570Strasz		    "0x%x, not found; dropping connection",
905255570Strasz		    bhsdo->bhsdo_target_transfer_tag, bhsdo->bhsdo_initiator_task_tag);
906255570Strasz		icl_pdu_free(request);
907255570Strasz		cfiscsi_session_terminate(cs);
908255570Strasz		return;
909255570Strasz	}
910255570Strasz
911255570Strasz	io = cdw->cdw_ctl_io;
912255570Strasz	KASSERT((io->io_hdr.flags & CTL_FLAG_DATA_MASK) != CTL_FLAG_DATA_IN,
913255570Strasz	    ("CTL_FLAG_DATA_IN"));
914255570Strasz
915255570Strasz	done = cfiscsi_handle_data_segment(request, cdw);
916255570Strasz	if (done) {
917255570Strasz		CFISCSI_SESSION_LOCK(cs);
918255570Strasz		TAILQ_REMOVE(&cs->cs_waiting_for_data_out, cdw, cdw_next);
919255570Strasz		CFISCSI_SESSION_UNLOCK(cs);
920255570Strasz		uma_zfree(cfiscsi_data_wait_zone, cdw);
921255570Strasz		io->scsiio.be_move_done(io);
922255570Strasz	}
923255570Strasz
924255570Strasz	icl_pdu_free(request);
925255570Strasz}
926255570Strasz
927255570Straszstatic void
928255570Straszcfiscsi_pdu_handle_logout_request(struct icl_pdu *request)
929255570Strasz{
930255570Strasz	struct iscsi_bhs_logout_request *bhslr;
931255570Strasz	struct iscsi_bhs_logout_response *bhslr2;
932255570Strasz	struct icl_pdu *response;
933255570Strasz	struct cfiscsi_session *cs;
934255824Strasz
935255824Strasz	cs = PDU_SESSION(request);
936255824Strasz	bhslr = (struct iscsi_bhs_logout_request *)request->ip_bhs;
937255824Strasz	switch (bhslr->bhslr_reason & 0x7f) {
938255824Strasz	case BHSLR_REASON_CLOSE_SESSION:
939255570Strasz	case BHSLR_REASON_CLOSE_CONNECTION:
940255570Strasz		response = cfiscsi_pdu_new_response(request, M_NOWAIT);
941255570Strasz		if (response == NULL) {
942255570Strasz			CFISCSI_SESSION_DEBUG(cs, "failed to allocate memory");
943255570Strasz			icl_pdu_free(request);
944255570Strasz			cfiscsi_session_terminate(cs);
945255570Strasz			return;
946255570Strasz		}
947255570Strasz		bhslr2 = (struct iscsi_bhs_logout_response *)response->ip_bhs;
948255570Strasz		bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
949255570Strasz		bhslr2->bhslr_flags = 0x80;
950255570Strasz		bhslr2->bhslr_response = BHSLR_RESPONSE_CLOSED_SUCCESSFULLY;
951255570Strasz		bhslr2->bhslr_initiator_task_tag =
952255570Strasz		    bhslr->bhslr_initiator_task_tag;
953255570Strasz		icl_pdu_free(request);
954255570Strasz		cfiscsi_pdu_queue(response);
955255570Strasz		cfiscsi_session_terminate(cs);
956255570Strasz		break;
957255570Strasz	case BHSLR_REASON_REMOVE_FOR_RECOVERY:
958255570Strasz		response = cfiscsi_pdu_new_response(request, M_NOWAIT);
959255570Strasz		if (response == NULL) {
960255570Strasz			CFISCSI_SESSION_WARN(cs,
961255570Strasz			    "failed to allocate memory; dropping connection");
962255570Strasz			icl_pdu_free(request);
963255570Strasz			cfiscsi_session_terminate(cs);
964255570Strasz			return;
965255570Strasz		}
966255570Strasz		bhslr2 = (struct iscsi_bhs_logout_response *)response->ip_bhs;
967255570Strasz		bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
968255570Strasz		bhslr2->bhslr_flags = 0x80;
969255570Strasz		bhslr2->bhslr_response = BHSLR_RESPONSE_RECOVERY_NOT_SUPPORTED;
970255570Strasz		bhslr2->bhslr_initiator_task_tag =
971255570Strasz		    bhslr->bhslr_initiator_task_tag;
972255570Strasz		icl_pdu_free(request);
973255570Strasz		cfiscsi_pdu_queue(response);
974255570Strasz		break;
975255570Strasz	default:
976255570Strasz		CFISCSI_SESSION_WARN(cs, "invalid reason 0%x; dropping connection",
977255570Strasz		    bhslr->bhslr_reason);
978255570Strasz		icl_pdu_free(request);
979255570Strasz		cfiscsi_session_terminate(cs);
980255570Strasz		break;
981255570Strasz	}
982255570Strasz}
983255570Strasz
984255570Straszstatic void
985255570Straszcfiscsi_callout(void *context)
986255570Strasz{
987255570Strasz	struct icl_pdu *cp;
988255570Strasz	struct iscsi_bhs_nop_in *bhsni;
989255570Strasz	struct cfiscsi_session *cs;
990255570Strasz
991255570Strasz	cs = context;
992255570Strasz
993255570Strasz	if (cs->cs_terminating)
994255570Strasz		return;
995255570Strasz
996255570Strasz	callout_schedule(&cs->cs_callout, 1 * hz);
997255570Strasz
998255570Strasz	atomic_add_int(&cs->cs_timeout, 1);
999255570Strasz
1000255570Strasz#ifdef ICL_KERNEL_PROXY
1001255570Strasz	if (cs->cs_waiting_for_ctld || cs->cs_login_phase) {
1002255570Strasz		if (cs->cs_timeout > login_timeout) {
1003255570Strasz			CFISCSI_SESSION_WARN(cs, "login timed out after "
1004255570Strasz			    "%d seconds; dropping connection", cs->cs_timeout);
1005255570Strasz			cfiscsi_session_terminate(cs);
1006255570Strasz		}
1007255570Strasz		return;
1008255570Strasz	}
1009255570Strasz#endif
1010255570Strasz
1011255570Strasz	if (cs->cs_timeout >= ping_timeout) {
1012255570Strasz		CFISCSI_SESSION_WARN(cs, "no ping reply (NOP-Out) after %d seconds; "
1013255570Strasz		    "dropping connection",  ping_timeout);
1014255570Strasz		cfiscsi_session_terminate(cs);
1015255570Strasz		return;
1016255570Strasz	}
1017255570Strasz
1018255570Strasz	/*
1019255570Strasz	 * If the ping was reset less than one second ago - which means
1020255570Strasz	 * that we've received some PDU during the last second - assume
1021255570Strasz	 * the traffic flows correctly and don't bother sending a NOP-Out.
1022255570Strasz	 *
1023255570Strasz	 * (It's 2 - one for one second, and one for incrementing is_timeout
1024255570Strasz	 * earlier in this routine.)
1025255570Strasz	 */
1026255570Strasz	if (cs->cs_timeout < 2)
1027255570Strasz		return;
1028255570Strasz
1029255570Strasz	cp = icl_pdu_new_bhs(cs->cs_conn, M_NOWAIT);
1030255570Strasz	if (cp == NULL) {
1031255570Strasz		CFISCSI_SESSION_WARN(cs, "failed to allocate memory");
1032255570Strasz		return;
1033255570Strasz	}
1034255570Strasz	bhsni = (struct iscsi_bhs_nop_in *)cp->ip_bhs;
1035255570Strasz	bhsni->bhsni_opcode = ISCSI_BHS_OPCODE_NOP_IN;
1036255570Strasz	bhsni->bhsni_flags = 0x80;
1037255570Strasz	bhsni->bhsni_initiator_task_tag = 0xffffffff;
1038255570Strasz
1039255570Strasz	cfiscsi_pdu_queue(cp);
1040255570Strasz}
1041255570Strasz
1042255570Straszstatic void
1043255570Straszcfiscsi_session_terminate_tasks(struct cfiscsi_session *cs)
1044255570Strasz{
1045255570Strasz	struct cfiscsi_data_wait *cdw, *tmpcdw;
1046255570Strasz	union ctl_io *io;
1047255570Strasz	int error, last;
1048255570Strasz
1049255570Strasz#ifdef notyet
1050255570Strasz	io = ctl_alloc_io(cs->cs_target->ct_softc->fe.ctl_pool_ref);
1051255570Strasz	if (io == NULL) {
1052255570Strasz		CFISCSI_SESSION_WARN(cs, "can't allocate ctl_io");
1053255570Strasz		return;
1054255570Strasz	}
1055255570Strasz	ctl_zero_io(io);
1056255570Strasz	io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = NULL;
1057255570Strasz	io->io_hdr.io_type = CTL_IO_TASK;
1058255570Strasz	io->io_hdr.nexus.initid.id = cs->cs_ctl_initid;
1059255570Strasz	io->io_hdr.nexus.targ_port = cs->cs_target->ct_softc->fe.targ_port;
1060255570Strasz	io->io_hdr.nexus.targ_target.id = 0;
1061255570Strasz	io->io_hdr.nexus.targ_lun = lun;
1062255570Strasz	io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
1063255570Strasz	io->taskio.task_action = CTL_TASK_ABORT_TASK_SET;
1064255570Strasz	error = ctl_queue(io);
1065255570Strasz	if (error != CTL_RETVAL_COMPLETE) {
1066255570Strasz		CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d", error);
1067255570Strasz		ctl_free_io(io);
1068255570Strasz	}
1069255570Strasz#else
1070255570Strasz	/*
1071255570Strasz	 * CTL doesn't currently support CTL_TASK_ABORT_TASK_SET, so instead
1072255570Strasz	 * just iterate over tasks that are waiting for something - data - and
1073255570Strasz	 * terminate those.
1074255570Strasz	 */
1075255570Strasz	CFISCSI_SESSION_LOCK(cs);
1076255570Strasz	TAILQ_FOREACH_SAFE(cdw,
1077255570Strasz	    &cs->cs_waiting_for_data_out, cdw_next, tmpcdw) {
1078255570Strasz		io = ctl_alloc_io(cs->cs_target->ct_softc->fe.ctl_pool_ref);
1079255570Strasz		if (io == NULL) {
1080255570Strasz			CFISCSI_SESSION_WARN(cs, "can't allocate ctl_io");
1081255570Strasz			return;
1082255570Strasz		}
1083255570Strasz		ctl_zero_io(io);
1084255570Strasz		io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = NULL;
1085255570Strasz		io->io_hdr.io_type = CTL_IO_TASK;
1086255570Strasz		io->io_hdr.nexus.initid.id = cs->cs_ctl_initid;
1087255570Strasz		io->io_hdr.nexus.targ_port =
1088255570Strasz		    cs->cs_target->ct_softc->fe.targ_port;
1089255570Strasz		io->io_hdr.nexus.targ_target.id = 0;
1090255570Strasz		//io->io_hdr.nexus.targ_lun = lun; /* Not needed? */
1091255570Strasz		io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
1092255570Strasz		io->taskio.task_action = CTL_TASK_ABORT_TASK;
1093255570Strasz		io->taskio.tag_num = cdw->cdw_initiator_task_tag;
1094255570Strasz		error = ctl_queue(io);
1095255570Strasz		if (error != CTL_RETVAL_COMPLETE) {
1096255570Strasz			CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d", error);
1097255570Strasz			ctl_free_io(io);
1098255570Strasz			return;
1099255570Strasz		}
1100255570Strasz#if 0
1101255570Strasz		CFISCSI_SESSION_DEBUG(cs, "removing csw for initiator task tag "
1102255570Strasz		    "0x%x", cdw->cdw_initiator_task_tag);
1103255570Strasz#endif
1104255570Strasz		/*
1105255570Strasz		 * Set nonzero port status; this prevents backends from
1106255570Strasz		 * assuming that the data transfer actually succeeded
1107255570Strasz		 * and writing uninitialized data to disk.
1108255570Strasz		 */
1109255570Strasz		cdw->cdw_ctl_io->scsiio.io_hdr.port_status = 42;
1110255570Strasz		cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io);
1111255570Strasz		TAILQ_REMOVE(&cs->cs_waiting_for_data_out, cdw, cdw_next);
1112255570Strasz		uma_zfree(cfiscsi_data_wait_zone, cdw);
1113255570Strasz	}
1114255570Strasz	CFISCSI_SESSION_UNLOCK(cs);
1115255570Strasz#endif
1116255570Strasz
1117255570Strasz	/*
1118255570Strasz	 * Wait for CTL to terminate all the tasks.
1119255570Strasz	 */
1120255570Strasz	for (;;) {
1121255570Strasz		refcount_acquire(&cs->cs_outstanding_ctl_pdus);
1122255570Strasz		last = refcount_release(&cs->cs_outstanding_ctl_pdus);
1123255570Strasz		if (last != 0)
1124255570Strasz			break;
1125255570Strasz		CFISCSI_SESSION_WARN(cs, "waiting for CTL to terminate tasks, "
1126255570Strasz		    "%d remaining", cs->cs_outstanding_ctl_pdus);
1127255570Strasz		pause("cfiscsi_terminate", 1);
1128255570Strasz	}
1129255570Strasz}
1130255570Strasz
1131255570Straszstatic void
1132255570Straszcfiscsi_maintenance_thread(void *arg)
1133255570Strasz{
1134255570Strasz	struct cfiscsi_session *cs;
1135255570Strasz
1136255570Strasz	cs = arg;
1137255570Strasz
1138255570Strasz	for (;;) {
1139255570Strasz		CFISCSI_SESSION_LOCK(cs);
1140255570Strasz		if (cs->cs_terminating == false)
1141255570Strasz			cv_wait(&cs->cs_maintenance_cv, &cs->cs_lock);
1142255570Strasz		CFISCSI_SESSION_UNLOCK(cs);
1143255570Strasz
1144255570Strasz		if (cs->cs_terminating) {
1145255570Strasz
1146255570Strasz			/*
1147255570Strasz			 * We used to wait up to 30 seconds to deliver queued
1148255570Strasz			 * PDUs to the initiator.  We also tried hard to deliver
1149255570Strasz			 * SCSI Responses for the aborted PDUs.  We don't do
1150255570Strasz			 * that anymore.  We might need to revisit that.
1151255570Strasz			 */
1152255570Strasz			callout_drain(&cs->cs_callout);
1153255570Strasz			icl_conn_shutdown(cs->cs_conn);
1154255570Strasz			icl_conn_close(cs->cs_conn);
1155255570Strasz
1156255570Strasz			/*
1157255570Strasz			 * At this point ICL receive thread is no longer
1158255570Strasz			 * running; no new tasks can be queued.
1159255570Strasz			 */
1160255570Strasz			cfiscsi_session_terminate_tasks(cs);
1161255570Strasz			cfiscsi_session_delete(cs);
1162255570Strasz			kthread_exit();
1163255570Strasz			return;
1164255570Strasz		}
1165255570Strasz		CFISCSI_SESSION_DEBUG(cs, "nothing to do");
1166255570Strasz	}
1167255570Strasz}
1168255570Strasz
1169255570Straszstatic void
1170255570Straszcfiscsi_session_terminate(struct cfiscsi_session *cs)
1171255570Strasz{
1172255570Strasz
1173255570Strasz	if (cs->cs_terminating)
1174255570Strasz		return;
1175255570Strasz	cs->cs_terminating = true;
1176255570Strasz	cv_signal(&cs->cs_maintenance_cv);
1177255570Strasz#ifdef ICL_KERNEL_PROXY
1178255570Strasz	cv_signal(&cs->cs_login_cv);
1179255570Strasz#endif
1180255570Strasz}
1181255570Strasz
1182255570Straszstatic int
1183255570Straszcfiscsi_session_register_initiator(struct cfiscsi_session *cs)
1184255570Strasz{
1185255570Strasz	int error, i;
1186255570Strasz	struct cfiscsi_softc *softc;
1187255570Strasz
1188255570Strasz	KASSERT(cs->cs_ctl_initid == -1, ("already registered"));
1189255570Strasz
1190255570Strasz	softc = &cfiscsi_softc;
1191255570Strasz
1192255570Strasz	mtx_lock(&softc->lock);
1193255570Strasz	for (i = 0; i < softc->max_initiators; i++) {
1194255570Strasz		if (softc->ctl_initids[i] == 0)
1195255570Strasz			break;
1196255570Strasz	}
1197255570Strasz	if (i == softc->max_initiators) {
1198255570Strasz		CFISCSI_SESSION_WARN(cs, "too many concurrent sessions (%d)",
1199255570Strasz		    softc->max_initiators);
1200255570Strasz		mtx_unlock(&softc->lock);
1201255570Strasz		return (1);
1202255570Strasz	}
1203255570Strasz	softc->ctl_initids[i] = 1;
1204255570Strasz	mtx_unlock(&softc->lock);
1205255570Strasz
1206255570Strasz#if 0
1207255570Strasz	CFISCSI_SESSION_DEBUG(cs, "adding initiator id %d, max %d",
1208255570Strasz	    i, softc->max_initiators);
1209255570Strasz#endif
1210255570Strasz	cs->cs_ctl_initid = i;
1211255570Strasz	error = ctl_add_initiator(0x0, softc->fe.targ_port, cs->cs_ctl_initid);
1212255570Strasz	if (error != 0) {
1213255570Strasz		CFISCSI_SESSION_WARN(cs, "ctl_add_initiator failed with error %d", error);
1214255570Strasz		mtx_lock(&softc->lock);
1215255570Strasz		softc->ctl_initids[cs->cs_ctl_initid] = 0;
1216255570Strasz		mtx_unlock(&softc->lock);
1217255570Strasz		cs->cs_ctl_initid = -1;
1218255570Strasz		return (1);
1219255570Strasz	}
1220255570Strasz
1221255570Strasz	return (0);
1222255570Strasz}
1223255570Strasz
1224255570Straszstatic void
1225255570Straszcfiscsi_session_unregister_initiator(struct cfiscsi_session *cs)
1226255570Strasz{
1227255570Strasz	int error;
1228255570Strasz	struct cfiscsi_softc *softc;
1229255570Strasz
1230255570Strasz	if (cs->cs_ctl_initid == -1)
1231255570Strasz		return;
1232255570Strasz
1233255570Strasz	softc = &cfiscsi_softc;
1234255570Strasz
1235255570Strasz	error = ctl_remove_initiator(softc->fe.targ_port, cs->cs_ctl_initid);
1236255570Strasz	if (error != 0) {
1237255570Strasz		CFISCSI_SESSION_WARN(cs, "ctl_remove_initiator failed with error %d",
1238255570Strasz		    error);
1239255570Strasz	}
1240255570Strasz	mtx_lock(&softc->lock);
1241255570Strasz	softc->ctl_initids[cs->cs_ctl_initid] = 0;
1242255570Strasz	mtx_unlock(&softc->lock);
1243255570Strasz	cs->cs_ctl_initid = -1;
1244255570Strasz}
1245255570Strasz
1246255570Straszstatic struct cfiscsi_session *
1247255570Straszcfiscsi_session_new(struct cfiscsi_softc *softc)
1248255570Strasz{
1249255570Strasz	struct cfiscsi_session *cs;
1250255570Strasz	int error;
1251255570Strasz
1252255570Strasz	cs = malloc(sizeof(*cs), M_CFISCSI, M_NOWAIT | M_ZERO);
1253255570Strasz	if (cs == NULL) {
1254255570Strasz		CFISCSI_WARN("malloc failed");
1255255570Strasz		return (NULL);
1256255570Strasz	}
1257255570Strasz	cs->cs_ctl_initid = -1;
1258256058Strasz
1259255570Strasz	refcount_init(&cs->cs_outstanding_ctl_pdus, 0);
1260255570Strasz	TAILQ_INIT(&cs->cs_waiting_for_data_out);
1261255570Strasz	mtx_init(&cs->cs_lock, "cfiscsi_lock", NULL, MTX_DEF);
1262255570Strasz	cv_init(&cs->cs_maintenance_cv, "cfiscsi_mt");
1263255570Strasz#ifdef ICL_KERNEL_PROXY
1264255570Strasz	cv_init(&cs->cs_login_cv, "cfiscsi_login");
1265255570Strasz#endif
1266255570Strasz
1267255570Strasz	cs->cs_conn = icl_conn_new("cfiscsi", &cs->cs_lock);
1268255570Strasz	cs->cs_conn->ic_receive = cfiscsi_receive_callback;
1269255570Strasz	cs->cs_conn->ic_error = cfiscsi_error_callback;
1270255570Strasz	cs->cs_conn->ic_prv0 = cs;
1271255570Strasz
1272255570Strasz	error = kthread_add(cfiscsi_maintenance_thread, cs, NULL, NULL, 0, 0, "cfiscsimt");
1273255570Strasz	if (error != 0) {
1274255570Strasz		CFISCSI_SESSION_WARN(cs, "kthread_add(9) failed with error %d", error);
1275255570Strasz		free(cs, M_CFISCSI);
1276255570Strasz		return (NULL);
1277255570Strasz	}
1278255570Strasz
1279255570Strasz	mtx_lock(&softc->lock);
1280255570Strasz	cs->cs_id = softc->last_session_id + 1;
1281255570Strasz	softc->last_session_id++;
1282255570Strasz	mtx_unlock(&softc->lock);
1283255570Strasz
1284255570Strasz	mtx_lock(&softc->lock);
1285255570Strasz	TAILQ_INSERT_TAIL(&softc->sessions, cs, cs_next);
1286255570Strasz	mtx_unlock(&softc->lock);
1287255570Strasz
1288255570Strasz	/*
1289255570Strasz	 * Start pinging the initiator.
1290255570Strasz	 */
1291255570Strasz	callout_init(&cs->cs_callout, 1);
1292255570Strasz	callout_reset(&cs->cs_callout, 1 * hz, cfiscsi_callout, cs);
1293255570Strasz
1294255570Strasz	return (cs);
1295255570Strasz}
1296255570Strasz
1297255570Straszstatic void
1298255570Straszcfiscsi_session_delete(struct cfiscsi_session *cs)
1299255570Strasz{
1300255570Strasz	struct cfiscsi_softc *softc;
1301255570Strasz
1302255570Strasz	softc = &cfiscsi_softc;
1303255570Strasz
1304255570Strasz	KASSERT(cs->cs_outstanding_ctl_pdus == 0,
1305255570Strasz	    ("destroying session with outstanding CTL pdus"));
1306255570Strasz	KASSERT(TAILQ_EMPTY(&cs->cs_waiting_for_data_out),
1307255570Strasz	    ("destroying session with non-empty queue"));
1308255570Strasz
1309255570Strasz	cfiscsi_session_unregister_initiator(cs);
1310255570Strasz	if (cs->cs_target != NULL)
1311255570Strasz		cfiscsi_target_release(cs->cs_target);
1312255570Strasz	icl_conn_close(cs->cs_conn);
1313255570Strasz	icl_conn_free(cs->cs_conn);
1314255570Strasz
1315255570Strasz	mtx_lock(&softc->lock);
1316255570Strasz	TAILQ_REMOVE(&softc->sessions, cs, cs_next);
1317255570Strasz	mtx_unlock(&softc->lock);
1318255570Strasz
1319255570Strasz	free(cs, M_CFISCSI);
1320255570Strasz}
1321255570Strasz
1322255570Straszint
1323255570Straszcfiscsi_init(void)
1324255570Strasz{
1325255570Strasz	struct cfiscsi_softc *softc;
1326255570Strasz	struct ctl_frontend *fe;
1327255570Strasz	int retval;
1328255570Strasz
1329255570Strasz	softc = &cfiscsi_softc;
1330255570Strasz	retval = 0;
1331255570Strasz	bzero(softc, sizeof(*softc));
1332255570Strasz	mtx_init(&softc->lock, "cfiscsi", NULL, MTX_DEF);
1333255570Strasz
1334255570Strasz#ifdef ICL_KERNEL_PROXY
1335255570Strasz	cv_init(&softc->accept_cv, "cfiscsi_accept");
1336255570Strasz#endif
1337255570Strasz	TAILQ_INIT(&softc->sessions);
1338255570Strasz	TAILQ_INIT(&softc->targets);
1339255570Strasz
1340255570Strasz	fe = &softc->fe;
1341255570Strasz	fe->port_type = CTL_PORT_ISCSI;
1342255570Strasz	/* XXX KDM what should the real number be here? */
1343255570Strasz	fe->num_requested_ctl_io = 4096;
1344255570Strasz	snprintf(softc->port_name, sizeof(softc->port_name), "iscsi");
1345255570Strasz	fe->port_name = softc->port_name;
1346255570Strasz	fe->port_online = cfiscsi_online;
1347255570Strasz	fe->port_offline = cfiscsi_offline;
1348255570Strasz	fe->onoff_arg = softc;
1349255570Strasz	fe->targ_enable = cfiscsi_targ_enable;
1350255570Strasz	fe->targ_disable = cfiscsi_targ_disable;
1351255570Strasz	fe->lun_enable = cfiscsi_lun_enable;
1352255570Strasz	fe->lun_disable = cfiscsi_lun_disable;
1353255570Strasz	fe->targ_lun_arg = softc;
1354255570Strasz	fe->ioctl = cfiscsi_ioctl;
1355255570Strasz	fe->devid = cfiscsi_devid;
1356255570Strasz	fe->fe_datamove = cfiscsi_datamove;
1357255570Strasz	fe->fe_done = cfiscsi_done;
1358255570Strasz
1359255570Strasz	/* XXX KDM what should we report here? */
1360255570Strasz	/* XXX These should probably be fetched from CTL. */
1361255570Strasz	fe->max_targets = 1;
1362255570Strasz	fe->max_target_id = 15;
1363255570Strasz
1364255570Strasz	retval = ctl_frontend_register(fe, /*master_SC*/ 1);
1365255570Strasz	if (retval != 0) {
1366255570Strasz		CFISCSI_WARN("ctl_frontend_register() failed with error %d",
1367255570Strasz		    retval);
1368255570Strasz		retval = 1;
1369255570Strasz		goto bailout;
1370255570Strasz	}
1371255570Strasz
1372255570Strasz	softc->max_initiators = fe->max_initiators;
1373255570Strasz
1374255570Strasz	cfiscsi_data_wait_zone = uma_zcreate("cfiscsi_data_wait",
1375255570Strasz	    sizeof(struct cfiscsi_data_wait), NULL, NULL, NULL, NULL,
1376255570Strasz	    UMA_ALIGN_PTR, 0);
1377255570Strasz
1378255570Strasz	return (0);
1379255570Strasz
1380255570Straszbailout:
1381255570Strasz	return (retval);
1382255570Strasz}
1383255570Strasz
1384255570Straszstatic int
1385255570Straszcfiscsi_module_event_handler(module_t mod, int what, void *arg)
1386255570Strasz{
1387255570Strasz
1388255570Strasz	switch (what) {
1389255570Strasz	case MOD_LOAD:
1390255570Strasz		return (cfiscsi_init());
1391255570Strasz	case MOD_UNLOAD:
1392255570Strasz		return (EBUSY);
1393255570Strasz	default:
1394255570Strasz		return (EOPNOTSUPP);
1395255570Strasz	}
1396255570Strasz}
1397255570Strasz
1398255570Strasz#ifdef ICL_KERNEL_PROXY
1399255570Straszstatic void
1400255570Straszcfiscsi_accept(struct socket *so, struct sockaddr *sa, int portal_id)
1401255570Strasz{
1402255570Strasz	struct cfiscsi_session *cs;
1403255570Strasz
1404255570Strasz	cs = cfiscsi_session_new(&cfiscsi_softc);
1405255570Strasz	if (cs == NULL) {
1406255570Strasz		CFISCSI_WARN("failed to create session");
1407255570Strasz		return;
1408255570Strasz	}
1409255570Strasz
1410255570Strasz	icl_conn_handoff_sock(cs->cs_conn, so);
1411255570Strasz	cs->cs_initiator_sa = sa;
1412255570Strasz	cs->cs_portal_id = portal_id;
1413255570Strasz	cs->cs_waiting_for_ctld = true;
1414255570Strasz	cv_signal(&cfiscsi_softc.accept_cv);
1415255570Strasz}
1416255570Strasz#endif
1417255570Strasz
1418255570Straszstatic void
1419255570Straszcfiscsi_online(void *arg)
1420255570Strasz{
1421255570Strasz	struct cfiscsi_softc *softc;
1422255570Strasz
1423255570Strasz	softc = (struct cfiscsi_softc *)arg;
1424255570Strasz
1425255570Strasz	softc->online = 1;
1426255570Strasz#ifdef ICL_KERNEL_PROXY
1427255570Strasz	if (softc->listener != NULL)
1428255570Strasz		icl_listen_free(softc->listener);
1429255570Strasz	softc->listener = icl_listen_new(cfiscsi_accept);
1430255570Strasz#endif
1431255570Strasz}
1432255570Strasz
1433255570Straszstatic void
1434255570Straszcfiscsi_offline(void *arg)
1435255570Strasz{
1436255570Strasz	struct cfiscsi_softc *softc;
1437255570Strasz	struct cfiscsi_session *cs;
1438255570Strasz
1439255570Strasz	softc = (struct cfiscsi_softc *)arg;
1440255570Strasz
1441255570Strasz	softc->online = 0;
1442255570Strasz
1443255570Strasz	mtx_lock(&softc->lock);
1444255570Strasz	TAILQ_FOREACH(cs, &softc->sessions, cs_next)
1445255570Strasz		cfiscsi_session_terminate(cs);
1446255570Strasz	mtx_unlock(&softc->lock);
1447255570Strasz
1448255570Strasz#ifdef ICL_KERNEL_PROXY
1449255570Strasz	icl_listen_free(softc->listener);
1450255570Strasz	softc->listener = NULL;
1451255570Strasz#endif
1452255570Strasz}
1453255570Strasz
1454255570Straszstatic int
1455255570Straszcfiscsi_targ_enable(void *arg, struct ctl_id targ_id)
1456255570Strasz{
1457255570Strasz
1458255570Strasz	return (0);
1459255570Strasz}
1460255570Strasz
1461255570Straszstatic int
1462255570Straszcfiscsi_targ_disable(void *arg, struct ctl_id targ_id)
1463255570Strasz{
1464255570Strasz
1465255570Strasz	return (0);
1466255570Strasz}
1467255570Strasz
1468255570Straszstatic void
1469255570Straszcfiscsi_ioctl_handoff(struct ctl_iscsi *ci)
1470255570Strasz{
1471255570Strasz	struct cfiscsi_softc *softc;
1472255570Strasz	struct cfiscsi_session *cs;
1473255570Strasz	struct cfiscsi_target *ct;
1474255570Strasz	struct ctl_iscsi_handoff_params *cihp;
1475255570Strasz	int error;
1476255570Strasz
1477255570Strasz	cihp = (struct ctl_iscsi_handoff_params *)&(ci->data);
1478255570Strasz	softc = &cfiscsi_softc;
1479255570Strasz
1480255570Strasz	CFISCSI_DEBUG("new connection from %s (%s) to %s",
1481255570Strasz	    cihp->initiator_name, cihp->initiator_addr,
1482255570Strasz	    cihp->target_name);
1483255570Strasz
1484255570Strasz	if (softc->online == 0) {
1485255570Strasz		ci->status = CTL_ISCSI_ERROR;
1486255570Strasz		snprintf(ci->error_str, sizeof(ci->error_str),
1487255570Strasz		    "%s: port offline", __func__);
1488255570Strasz		return;
1489255570Strasz	}
1490255570Strasz
1491255570Strasz	ct = cfiscsi_target_find(softc, cihp->target_name);
1492255570Strasz	if (ct == NULL) {
1493255570Strasz		ci->status = CTL_ISCSI_ERROR;
1494255570Strasz		snprintf(ci->error_str, sizeof(ci->error_str),
1495255570Strasz		    "%s: target not found", __func__);
1496255570Strasz		return;
1497255570Strasz	}
1498255570Strasz
1499255570Strasz#ifdef ICL_KERNEL_PROXY
1500255570Strasz	if (cihp->socket > 0 && cihp->connection_id > 0) {
1501255570Strasz		snprintf(ci->error_str, sizeof(ci->error_str),
1502255570Strasz		    "both socket and connection_id set");
1503255570Strasz		ci->status = CTL_ISCSI_ERROR;
1504255570Strasz		cfiscsi_target_release(ct);
1505255570Strasz		return;
1506255570Strasz	}
1507255570Strasz	if (cihp->socket == 0) {
1508255570Strasz		mtx_lock(&cfiscsi_softc.lock);
1509255570Strasz		TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1510255570Strasz			if (cs->cs_id == cihp->socket)
1511255570Strasz				break;
1512255570Strasz		}
1513255570Strasz		if (cs == NULL) {
1514255570Strasz			mtx_unlock(&cfiscsi_softc.lock);
1515255570Strasz			snprintf(ci->error_str, sizeof(ci->error_str),
1516255570Strasz			    "connection not found");
1517255570Strasz			ci->status = CTL_ISCSI_ERROR;
1518255570Strasz			cfiscsi_target_release(ct);
1519255570Strasz			return;
1520255570Strasz		}
1521255570Strasz		mtx_unlock(&cfiscsi_softc.lock);
1522255570Strasz	} else {
1523255570Strasz#endif
1524255570Strasz		cs = cfiscsi_session_new(softc);
1525255570Strasz		if (cs == NULL) {
1526255570Strasz			ci->status = CTL_ISCSI_ERROR;
1527255570Strasz			snprintf(ci->error_str, sizeof(ci->error_str),
1528255570Strasz			    "%s: cfiscsi_session_new failed", __func__);
1529255570Strasz			cfiscsi_target_release(ct);
1530255570Strasz			return;
1531255570Strasz		}
1532255570Strasz#ifdef ICL_KERNEL_PROXY
1533255570Strasz	}
1534255570Strasz#endif
1535255570Strasz	cs->cs_target = ct;
1536255570Strasz
1537255570Strasz	/*
1538255570Strasz	 * First PDU of Full Feature phase has the same CmdSN as the last
1539255570Strasz	 * PDU from the Login Phase received from the initiator.  Thus,
1540255570Strasz	 * the -1 below.
1541255570Strasz	 */
1542255570Strasz	cs->cs_portal_group_tag = cihp->portal_group_tag;
1543255570Strasz	cs->cs_cmdsn = cihp->cmdsn;
1544255570Strasz	cs->cs_statsn = cihp->statsn;
1545255570Strasz	cs->cs_max_data_segment_length = cihp->max_recv_data_segment_length;
1546255570Strasz	cs->cs_max_burst_length = cihp->max_burst_length;
1547255570Strasz	cs->cs_immediate_data = !!cihp->immediate_data;
1548255570Strasz	if (cihp->header_digest == CTL_ISCSI_DIGEST_CRC32C)
1549255570Strasz		cs->cs_conn->ic_header_crc32c = true;
1550255570Strasz	if (cihp->data_digest == CTL_ISCSI_DIGEST_CRC32C)
1551255570Strasz		cs->cs_conn->ic_data_crc32c = true;
1552255570Strasz
1553255570Strasz	strlcpy(cs->cs_initiator_name,
1554255570Strasz	    cihp->initiator_name, sizeof(cs->cs_initiator_name));
1555255570Strasz	strlcpy(cs->cs_initiator_addr,
1556255570Strasz	    cihp->initiator_addr, sizeof(cs->cs_initiator_addr));
1557255570Strasz	strlcpy(cs->cs_initiator_alias,
1558255570Strasz	    cihp->initiator_alias, sizeof(cs->cs_initiator_alias));
1559255570Strasz
1560255570Strasz#ifdef ICL_KERNEL_PROXY
1561255570Strasz	if (cihp->socket > 0) {
1562255570Strasz#endif
1563255570Strasz		error = icl_conn_handoff(cs->cs_conn, cihp->socket);
1564255570Strasz		if (error != 0) {
1565255570Strasz			cfiscsi_session_delete(cs);
1566255570Strasz			ci->status = CTL_ISCSI_ERROR;
1567255570Strasz			snprintf(ci->error_str, sizeof(ci->error_str),
1568255570Strasz			    "%s: icl_conn_handoff failed with error %d",
1569255570Strasz			    __func__, error);
1570255570Strasz			return;
1571255570Strasz		}
1572255570Strasz#ifdef ICL_KERNEL_PROXY
1573255570Strasz	}
1574255570Strasz#endif
1575255570Strasz
1576255570Strasz	/*
1577255570Strasz	 * Register initiator with CTL.
1578255570Strasz	 */
1579255570Strasz	cfiscsi_session_register_initiator(cs);
1580255570Strasz
1581255570Strasz#ifdef ICL_KERNEL_PROXY
1582255570Strasz	cs->cs_login_phase = false;
1583255570Strasz
1584255570Strasz	/*
1585255570Strasz	 * First PDU of the Full Feature phase has likely already arrived.
1586255570Strasz	 * We have to pick it up and execute properly.
1587255570Strasz	 */
1588255570Strasz	if (cs->cs_login_pdu != NULL) {
1589255570Strasz		CFISCSI_SESSION_DEBUG(cs, "picking up first PDU");
1590255570Strasz		cfiscsi_pdu_handle(cs->cs_login_pdu);
1591255570Strasz		cs->cs_login_pdu = NULL;
1592255570Strasz	}
1593255570Strasz#endif
1594255570Strasz
1595255570Strasz	ci->status = CTL_ISCSI_OK;
1596255570Strasz}
1597255570Strasz
1598255570Straszstatic void
1599255570Straszcfiscsi_ioctl_list(struct ctl_iscsi *ci)
1600255570Strasz{
1601255570Strasz	struct ctl_iscsi_list_params *cilp;
1602255570Strasz	struct cfiscsi_session *cs;
1603255570Strasz	struct cfiscsi_softc *softc;
1604255570Strasz	struct sbuf *sb;
1605255570Strasz	int error;
1606255570Strasz
1607255570Strasz	cilp = (struct ctl_iscsi_list_params *)&(ci->data);
1608255570Strasz	softc = &cfiscsi_softc;
1609255570Strasz
1610255570Strasz	sb = sbuf_new(NULL, NULL, cilp->alloc_len, SBUF_FIXEDLEN);
1611255570Strasz	if (sb == NULL) {
1612255570Strasz		ci->status = CTL_ISCSI_ERROR;
1613255570Strasz		snprintf(ci->error_str, sizeof(ci->error_str),
1614255570Strasz		    "Unable to allocate %d bytes for iSCSI session list",
1615255570Strasz		    cilp->alloc_len);
1616255570Strasz		return;
1617255570Strasz	}
1618255570Strasz
1619255570Strasz	sbuf_printf(sb, "<ctlislist>\n");
1620255570Strasz	mtx_lock(&softc->lock);
1621255570Strasz	TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1622255570Strasz#ifdef ICL_KERNEL_PROXY
1623255570Strasz		if (cs->cs_target == NULL)
1624255570Strasz			continue;
1625255570Strasz#endif
1626255570Strasz		error = sbuf_printf(sb, "<connection id=\"%d\">"
1627255570Strasz		    "<initiator>%s</initiator>"
1628255570Strasz		    "<initiator_addr>%s</initiator_addr>"
1629255570Strasz		    "<initiator_alias>%s</initiator_alias>"
1630255570Strasz		    "<target>%s</target>"
1631255570Strasz		    "<target_alias>%s</target_alias>"
1632255570Strasz		    "<header_digest>%s</header_digest>"
1633255570Strasz		    "<data_digest>%s</data_digest>"
1634255570Strasz		    "<max_data_segment_length>%zd</max_data_segment_length>"
1635255570Strasz		    "<immediate_data>%d</immediate_data>"
1636255570Strasz		    "<iser>%d</iser>"
1637255570Strasz		    "</connection>\n",
1638255570Strasz		    cs->cs_id,
1639255570Strasz		    cs->cs_initiator_name, cs->cs_initiator_addr, cs->cs_initiator_alias,
1640255570Strasz		    cs->cs_target->ct_name, cs->cs_target->ct_alias,
1641255570Strasz		    cs->cs_conn->ic_header_crc32c ? "CRC32C" : "None",
1642255570Strasz		    cs->cs_conn->ic_data_crc32c ? "CRC32C" : "None",
1643255570Strasz		    cs->cs_max_data_segment_length,
1644255570Strasz		    cs->cs_immediate_data,
1645255570Strasz		    cs->cs_conn->ic_iser);
1646255570Strasz		if (error != 0)
1647255570Strasz			break;
1648255570Strasz	}
1649255570Strasz	mtx_unlock(&softc->lock);
1650255570Strasz	error = sbuf_printf(sb, "</ctlislist>\n");
1651255570Strasz	if (error != 0) {
1652255570Strasz		sbuf_delete(sb);
1653255570Strasz		ci->status = CTL_ISCSI_LIST_NEED_MORE_SPACE;
1654255570Strasz		snprintf(ci->error_str, sizeof(ci->error_str),
1655255570Strasz		    "Out of space, %d bytes is too small", cilp->alloc_len);
1656255570Strasz		return;
1657255570Strasz	}
1658255570Strasz	sbuf_finish(sb);
1659255570Strasz
1660255570Strasz	error = copyout(sbuf_data(sb), cilp->conn_xml, sbuf_len(sb) + 1);
1661255570Strasz	cilp->fill_len = sbuf_len(sb) + 1;
1662255570Strasz	ci->status = CTL_ISCSI_OK;
1663255570Strasz	sbuf_delete(sb);
1664255570Strasz}
1665255570Strasz
1666255570Straszstatic void
1667255570Straszcfiscsi_ioctl_terminate(struct ctl_iscsi *ci)
1668255570Strasz{
1669255570Strasz	struct icl_pdu *response;
1670255570Strasz	struct iscsi_bhs_asynchronous_message *bhsam;
1671255570Strasz	struct ctl_iscsi_terminate_params *citp;
1672255570Strasz	struct cfiscsi_session *cs;
1673255570Strasz	struct cfiscsi_softc *softc;
1674255570Strasz	int found = 0;
1675255570Strasz
1676255570Strasz	citp = (struct ctl_iscsi_terminate_params *)&(ci->data);
1677255570Strasz	softc = &cfiscsi_softc;
1678255570Strasz
1679255570Strasz	mtx_lock(&softc->lock);
1680255570Strasz	TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1681255570Strasz		if (citp->all == 0 && cs->cs_id != citp->connection_id &&
1682255570Strasz		    strcmp(cs->cs_initiator_name, citp->initiator_name) != 0 &&
1683255570Strasz		    strcmp(cs->cs_initiator_addr, citp->initiator_addr) != 0)
1684255570Strasz			continue;
1685255570Strasz
1686255570Strasz		response = icl_pdu_new_bhs(cs->cs_conn, M_NOWAIT);
1687255570Strasz		if (response == NULL) {
1688255570Strasz			/*
1689255570Strasz			 * Oh well.  Just terminate the connection.
1690255570Strasz			 */
1691255570Strasz		} else {
1692255570Strasz			bhsam = (struct iscsi_bhs_asynchronous_message *)
1693255570Strasz			    response->ip_bhs;
1694255570Strasz			bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE;
1695255570Strasz			bhsam->bhsam_flags = 0x80;
1696255570Strasz			bhsam->bhsam_0xffffffff = 0xffffffff;
1697255570Strasz			bhsam->bhsam_async_event =
1698255570Strasz			    BHSAM_EVENT_TARGET_TERMINATES_SESSION;
1699255570Strasz			cfiscsi_pdu_queue(response);
1700255570Strasz		}
1701255570Strasz		cfiscsi_session_terminate(cs);
1702255570Strasz		found++;
1703255570Strasz	}
1704255570Strasz	mtx_unlock(&softc->lock);
1705255570Strasz
1706255570Strasz	if (found == 0) {
1707255570Strasz		ci->status = CTL_ISCSI_SESSION_NOT_FOUND;
1708255570Strasz		snprintf(ci->error_str, sizeof(ci->error_str),
1709255570Strasz		    "No matching connections found");
1710255570Strasz		return;
1711255570Strasz	}
1712255570Strasz
1713255570Strasz	ci->status = CTL_ISCSI_OK;
1714255570Strasz}
1715255570Strasz
1716255570Straszstatic void
1717255570Straszcfiscsi_ioctl_logout(struct ctl_iscsi *ci)
1718255570Strasz{
1719255570Strasz	struct icl_pdu *response;
1720255570Strasz	struct iscsi_bhs_asynchronous_message *bhsam;
1721255570Strasz	struct ctl_iscsi_logout_params *cilp;
1722255570Strasz	struct cfiscsi_session *cs;
1723255570Strasz	struct cfiscsi_softc *softc;
1724255570Strasz	int found = 0;
1725255570Strasz
1726255570Strasz	cilp = (struct ctl_iscsi_logout_params *)&(ci->data);
1727255570Strasz	softc = &cfiscsi_softc;
1728255570Strasz
1729255570Strasz	mtx_lock(&softc->lock);
1730255570Strasz	TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1731255570Strasz		if (cilp->all == 0 && cs->cs_id != cilp->connection_id &&
1732255570Strasz		    strcmp(cs->cs_initiator_name, cilp->initiator_name) != 0 &&
1733255570Strasz		    strcmp(cs->cs_initiator_addr, cilp->initiator_addr) != 0)
1734255570Strasz			continue;
1735255570Strasz
1736255570Strasz		response = icl_pdu_new_bhs(cs->cs_conn, M_NOWAIT);
1737255570Strasz		if (response == NULL) {
1738255570Strasz			ci->status = CTL_ISCSI_ERROR;
1739255570Strasz			snprintf(ci->error_str, sizeof(ci->error_str),
1740255570Strasz			    "Unable to allocate memory");
1741255570Strasz			mtx_unlock(&softc->lock);
1742255570Strasz			return;
1743255570Strasz		}
1744255570Strasz		bhsam =
1745255570Strasz		    (struct iscsi_bhs_asynchronous_message *)response->ip_bhs;
1746255570Strasz		bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE;
1747255570Strasz		bhsam->bhsam_flags = 0x80;
1748255570Strasz		bhsam->bhsam_async_event = BHSAM_EVENT_TARGET_REQUESTS_LOGOUT;
1749255570Strasz		bhsam->bhsam_parameter3 = htons(10);
1750255570Strasz		cfiscsi_pdu_queue(response);
1751255570Strasz		found++;
1752255570Strasz	}
1753255570Strasz	mtx_unlock(&softc->lock);
1754255570Strasz
1755255570Strasz	if (found == 0) {
1756255570Strasz		ci->status = CTL_ISCSI_SESSION_NOT_FOUND;
1757255570Strasz		snprintf(ci->error_str, sizeof(ci->error_str),
1758255570Strasz		    "No matching connections found");
1759255570Strasz		return;
1760255570Strasz	}
1761255570Strasz
1762255570Strasz	ci->status = CTL_ISCSI_OK;
1763255570Strasz}
1764255570Strasz
1765255570Strasz#ifdef ICL_KERNEL_PROXY
1766255570Straszstatic void
1767255570Straszcfiscsi_ioctl_listen(struct ctl_iscsi *ci)
1768255570Strasz{
1769255570Strasz	struct ctl_iscsi_listen_params *cilp;
1770255570Strasz	struct sockaddr *sa;
1771255570Strasz	int error;
1772255570Strasz
1773255570Strasz	cilp = (struct ctl_iscsi_listen_params *)&(ci->data);
1774255570Strasz
1775255570Strasz	if (cfiscsi_softc.listener == NULL) {
1776255570Strasz		CFISCSI_DEBUG("no listener");
1777255570Strasz		snprintf(ci->error_str, sizeof(ci->error_str), "no listener");
1778255570Strasz		ci->status = CTL_ISCSI_ERROR;
1779255570Strasz		return;
1780255570Strasz	}
1781255570Strasz
1782255570Strasz	error = getsockaddr(&sa, (void *)cilp->addr, cilp->addrlen);
1783255570Strasz	if (error != 0) {
1784255570Strasz		CFISCSI_DEBUG("getsockaddr, error %d", error);
1785255570Strasz		snprintf(ci->error_str, sizeof(ci->error_str), "getsockaddr failed");
1786255570Strasz		ci->status = CTL_ISCSI_ERROR;
1787255570Strasz		return;
1788255570Strasz	}
1789255570Strasz
1790255570Strasz	error = icl_listen_add(cfiscsi_softc.listener, cilp->iser, cilp->domain,
1791255570Strasz	    cilp->socktype, cilp->protocol, sa, cilp->portal_id);
1792255570Strasz	if (error != 0) {
1793255570Strasz		free(sa, M_SONAME);
1794255570Strasz		CFISCSI_DEBUG("icl_listen_add, error %d", error);
1795255570Strasz		snprintf(ci->error_str, sizeof(ci->error_str),
1796255570Strasz		    "icl_listen_add failed, error %d", error);
1797255570Strasz		ci->status = CTL_ISCSI_ERROR;
1798255570Strasz		return;
1799255570Strasz	}
1800255570Strasz
1801255570Strasz	ci->status = CTL_ISCSI_OK;
1802255570Strasz}
1803255570Strasz
1804255570Straszstatic void
1805255570Straszcfiscsi_ioctl_accept(struct ctl_iscsi *ci)
1806255570Strasz{
1807255570Strasz	struct ctl_iscsi_accept_params *ciap;
1808255570Strasz	struct cfiscsi_session *cs;
1809255570Strasz	int error;
1810255570Strasz
1811255570Strasz	ciap = (struct ctl_iscsi_accept_params *)&(ci->data);
1812255570Strasz
1813255570Strasz	mtx_lock(&cfiscsi_softc.lock);
1814255570Strasz	for (;;) {
1815255570Strasz		TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1816255570Strasz			if (cs->cs_waiting_for_ctld)
1817255570Strasz				break;
1818255570Strasz		}
1819255570Strasz		if (cs != NULL)
1820255570Strasz			break;
1821255570Strasz		error = cv_wait_sig(&cfiscsi_softc.accept_cv, &cfiscsi_softc.lock);
1822255570Strasz		if (error != 0) {
1823255570Strasz			mtx_unlock(&cfiscsi_softc.lock);
1824255570Strasz			snprintf(ci->error_str, sizeof(ci->error_str), "interrupted");
1825255570Strasz			ci->status = CTL_ISCSI_ERROR;
1826255570Strasz			return;
1827255570Strasz		}
1828255570Strasz	}
1829255570Strasz	mtx_unlock(&cfiscsi_softc.lock);
1830255570Strasz
1831255570Strasz	cs->cs_waiting_for_ctld = false;
1832255570Strasz	cs->cs_login_phase = true;
1833255570Strasz
1834255570Strasz	ciap->connection_id = cs->cs_id;
1835255570Strasz	ciap->portal_id = cs->cs_portal_id;
1836255570Strasz	ciap->initiator_addrlen = cs->cs_initiator_sa->sa_len;
1837255570Strasz	error = copyout(cs->cs_initiator_sa, ciap->initiator_addr,
1838255570Strasz	    cs->cs_initiator_sa->sa_len);
1839255570Strasz	if (error != 0) {
1840255570Strasz		snprintf(ci->error_str, sizeof(ci->error_str),
1841255570Strasz		    "copyout failed with error %d", error);
1842255570Strasz		ci->status = CTL_ISCSI_ERROR;
1843255570Strasz		return;
1844255570Strasz	}
1845255570Strasz
1846255570Strasz	ci->status = CTL_ISCSI_OK;
1847255570Strasz}
1848255570Strasz
1849255570Straszstatic void
1850255570Straszcfiscsi_ioctl_send(struct ctl_iscsi *ci)
1851255570Strasz{
1852255570Strasz	struct ctl_iscsi_send_params *cisp;
1853255570Strasz	struct cfiscsi_session *cs;
1854255570Strasz	struct icl_pdu *ip;
1855255570Strasz	size_t datalen;
1856255570Strasz	void *data;
1857255570Strasz	int error;
1858255570Strasz
1859255570Strasz	cisp = (struct ctl_iscsi_send_params *)&(ci->data);
1860255570Strasz
1861255570Strasz	mtx_lock(&cfiscsi_softc.lock);
1862255570Strasz	TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1863255570Strasz		if (cs->cs_id == cisp->connection_id)
1864255570Strasz			break;
1865255570Strasz	}
1866255570Strasz	if (cs == NULL) {
1867255570Strasz		mtx_unlock(&cfiscsi_softc.lock);
1868255570Strasz		snprintf(ci->error_str, sizeof(ci->error_str), "connection not found");
1869255570Strasz		ci->status = CTL_ISCSI_ERROR;
1870255570Strasz		return;
1871255570Strasz	}
1872255570Strasz	mtx_unlock(&cfiscsi_softc.lock);
1873255570Strasz
1874255570Strasz#if 0
1875255570Strasz	if (cs->cs_login_phase == false)
1876255570Strasz		return (EBUSY);
1877255570Strasz#endif
1878255570Strasz
1879255570Strasz	if (cs->cs_terminating) {
1880255570Strasz		snprintf(ci->error_str, sizeof(ci->error_str), "connection is terminating");
1881255570Strasz		ci->status = CTL_ISCSI_ERROR;
1882255570Strasz		return;
1883255570Strasz	}
1884255570Strasz
1885255570Strasz	datalen = cisp->data_segment_len;
1886255570Strasz	/*
1887255570Strasz	 * XXX
1888255570Strasz	 */
1889255570Strasz	//if (datalen > CFISCSI_MAX_DATA_SEGMENT_LENGTH) {
1890255570Strasz	if (datalen > 65535) {
1891255570Strasz		snprintf(ci->error_str, sizeof(ci->error_str), "data segment too big");
1892255570Strasz		ci->status = CTL_ISCSI_ERROR;
1893255570Strasz		return;
1894255570Strasz	}
1895255570Strasz	if (datalen > 0) {
1896255570Strasz		data = malloc(datalen, M_CFISCSI, M_WAITOK);
1897255570Strasz		error = copyin(cisp->data_segment, data, datalen);
1898255570Strasz		if (error != 0) {
1899255570Strasz			free(data, M_CFISCSI);
1900255570Strasz			snprintf(ci->error_str, sizeof(ci->error_str), "copyin error %d", error);
1901255570Strasz			ci->status = CTL_ISCSI_ERROR;
1902255570Strasz			return;
1903255570Strasz		}
1904255570Strasz	}
1905255570Strasz
1906255570Strasz	ip = icl_pdu_new_bhs(cs->cs_conn, M_WAITOK);
1907255570Strasz	memcpy(ip->ip_bhs, cisp->bhs, sizeof(*ip->ip_bhs));
1908255570Strasz	if (datalen > 0) {
1909255570Strasz		icl_pdu_append_data(ip, data, datalen, M_WAITOK);
1910255570Strasz		free(data, M_CFISCSI);
1911255570Strasz	}
1912255570Strasz	CFISCSI_SESSION_LOCK(cs);
1913255570Strasz	icl_pdu_queue(ip);
1914255570Strasz	CFISCSI_SESSION_UNLOCK(cs);
1915255570Strasz	ci->status = CTL_ISCSI_OK;
1916255570Strasz}
1917255570Strasz
1918255570Straszstatic void
1919255570Straszcfiscsi_ioctl_receive(struct ctl_iscsi *ci)
1920255570Strasz{
1921255570Strasz	struct ctl_iscsi_receive_params *cirp;
1922255570Strasz	struct cfiscsi_session *cs;
1923255570Strasz	struct icl_pdu *ip;
1924255570Strasz	void *data;
1925255570Strasz	int error;
1926255570Strasz
1927255570Strasz	cirp = (struct ctl_iscsi_receive_params *)&(ci->data);
1928255570Strasz
1929255570Strasz	mtx_lock(&cfiscsi_softc.lock);
1930255570Strasz	TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1931255570Strasz		if (cs->cs_id == cirp->connection_id)
1932255570Strasz			break;
1933255570Strasz	}
1934255570Strasz	if (cs == NULL) {
1935255570Strasz		mtx_unlock(&cfiscsi_softc.lock);
1936255570Strasz		snprintf(ci->error_str, sizeof(ci->error_str),
1937255570Strasz		    "connection not found");
1938255570Strasz		ci->status = CTL_ISCSI_ERROR;
1939255570Strasz		return;
1940255570Strasz	}
1941255570Strasz	mtx_unlock(&cfiscsi_softc.lock);
1942255570Strasz
1943255570Strasz#if 0
1944255570Strasz	if (is->is_login_phase == false)
1945255570Strasz		return (EBUSY);
1946255570Strasz#endif
1947255570Strasz
1948255570Strasz	CFISCSI_SESSION_LOCK(cs);
1949255570Strasz	while (cs->cs_login_pdu == NULL && cs->cs_terminating == false) {
1950255570Strasz		error = cv_wait_sig(&cs->cs_login_cv, &cs->cs_lock);
1951255570Strasz		if (error != 0) {
1952255570Strasz			CFISCSI_SESSION_UNLOCK(cs);
1953255570Strasz			snprintf(ci->error_str, sizeof(ci->error_str),
1954255570Strasz			    "interrupted by signal");
1955255570Strasz			ci->status = CTL_ISCSI_ERROR;
1956255570Strasz			return;
1957255570Strasz		}
1958255570Strasz	}
1959255570Strasz
1960255570Strasz	if (cs->cs_terminating) {
1961255570Strasz		CFISCSI_SESSION_UNLOCK(cs);
1962255570Strasz		snprintf(ci->error_str, sizeof(ci->error_str),
1963255570Strasz		    "connection terminating");
1964255570Strasz		ci->status = CTL_ISCSI_ERROR;
1965255570Strasz		return;
1966255570Strasz	}
1967255570Strasz	ip = cs->cs_login_pdu;
1968255570Strasz	cs->cs_login_pdu = NULL;
1969255570Strasz	CFISCSI_SESSION_UNLOCK(cs);
1970255570Strasz
1971255570Strasz	if (ip->ip_data_len > cirp->data_segment_len) {
1972255570Strasz		icl_pdu_free(ip);
1973255570Strasz		snprintf(ci->error_str, sizeof(ci->error_str),
1974255570Strasz		    "data segment too big");
1975255570Strasz		ci->status = CTL_ISCSI_ERROR;
1976255570Strasz		return;
1977255570Strasz	}
1978255570Strasz
1979255570Strasz	copyout(ip->ip_bhs, cirp->bhs, sizeof(*ip->ip_bhs));
1980255570Strasz	if (ip->ip_data_len > 0) {
1981255570Strasz		data = malloc(ip->ip_data_len, M_CFISCSI, M_WAITOK);
1982255570Strasz		icl_pdu_get_data(ip, 0, data, ip->ip_data_len);
1983255570Strasz		copyout(data, cirp->data_segment, ip->ip_data_len);
1984255570Strasz		free(data, M_CFISCSI);
1985255570Strasz	}
1986255570Strasz
1987255570Strasz	icl_pdu_free(ip);
1988255570Strasz	ci->status = CTL_ISCSI_OK;
1989255570Strasz}
1990255570Strasz
1991255570Strasz#endif /* !ICL_KERNEL_PROXY */
1992255570Strasz
1993255570Straszstatic int
1994255570Straszcfiscsi_ioctl(struct cdev *dev,
1995255570Strasz    u_long cmd, caddr_t addr, int flag, struct thread *td)
1996255570Strasz{
1997255570Strasz	struct ctl_iscsi *ci;
1998255570Strasz
1999255570Strasz	if (cmd != CTL_ISCSI)
2000255570Strasz		return (ENOTTY);
2001255570Strasz
2002255570Strasz	ci = (struct ctl_iscsi *)addr;
2003255570Strasz	switch (ci->type) {
2004255570Strasz	case CTL_ISCSI_HANDOFF:
2005255570Strasz		cfiscsi_ioctl_handoff(ci);
2006255570Strasz		break;
2007255570Strasz	case CTL_ISCSI_LIST:
2008255570Strasz		cfiscsi_ioctl_list(ci);
2009255570Strasz		break;
2010255570Strasz	case CTL_ISCSI_TERMINATE:
2011255570Strasz		cfiscsi_ioctl_terminate(ci);
2012255570Strasz		break;
2013255570Strasz	case CTL_ISCSI_LOGOUT:
2014255570Strasz		cfiscsi_ioctl_logout(ci);
2015255570Strasz		break;
2016255570Strasz#ifdef ICL_KERNEL_PROXY
2017255570Strasz	case CTL_ISCSI_LISTEN:
2018255570Strasz		cfiscsi_ioctl_listen(ci);
2019255570Strasz		break;
2020255570Strasz	case CTL_ISCSI_ACCEPT:
2021255570Strasz		cfiscsi_ioctl_accept(ci);
2022255570Strasz		break;
2023255570Strasz	case CTL_ISCSI_SEND:
2024255570Strasz		cfiscsi_ioctl_send(ci);
2025255570Strasz		break;
2026255570Strasz	case CTL_ISCSI_RECEIVE:
2027255570Strasz		cfiscsi_ioctl_receive(ci);
2028255570Strasz		break;
2029255570Strasz#else
2030255570Strasz	case CTL_ISCSI_LISTEN:
2031255570Strasz	case CTL_ISCSI_ACCEPT:
2032255570Strasz	case CTL_ISCSI_SEND:
2033255570Strasz	case CTL_ISCSI_RECEIVE:
2034255570Strasz		ci->status = CTL_ISCSI_ERROR;
2035255570Strasz		snprintf(ci->error_str, sizeof(ci->error_str),
2036255570Strasz		    "%s: CTL compiled without ICL_KERNEL_PROXY",
2037255570Strasz		    __func__);
2038255570Strasz		break;
2039255570Strasz#endif /* !ICL_KERNEL_PROXY */
2040255570Strasz	default:
2041255570Strasz		ci->status = CTL_ISCSI_ERROR;
2042255570Strasz		snprintf(ci->error_str, sizeof(ci->error_str),
2043255570Strasz		    "%s: invalid iSCSI request type %d", __func__, ci->type);
2044255570Strasz		break;
2045255570Strasz	}
2046255570Strasz
2047255570Strasz	return (0);
2048255570Strasz}
2049255570Strasz
2050255570Straszstatic int
2051255570Straszcfiscsi_devid(struct ctl_scsiio *ctsio, int alloc_len)
2052255570Strasz{
2053255570Strasz	struct cfiscsi_session *cs;
2054255570Strasz	struct scsi_vpd_device_id *devid_ptr;
2055255570Strasz	struct scsi_vpd_id_descriptor *desc, *desc1, *desc2, *desc3, *desc4;
2056255570Strasz	struct scsi_vpd_id_descriptor *desc5;
2057255570Strasz	struct scsi_vpd_id_t10 *t10id;
2058255570Strasz	struct ctl_lun *lun;
2059255570Strasz	const struct icl_pdu *request;
2060255570Strasz	int i, ret;
2061255570Strasz	char *val;
2062255570Strasz	size_t devid_len, wwnn_len, wwpn_len, lun_name_len;
2063255570Strasz
2064255570Strasz	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
2065255570Strasz	request = ctsio->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2066255570Strasz	cs = PDU_SESSION(request);
2067255570Strasz
2068255570Strasz	wwpn_len = strlen(cs->cs_target->ct_name);
2069255570Strasz	wwpn_len += strlen(",t,0x0001");
2070255570Strasz	wwpn_len += 1; /* '\0' */
2071255570Strasz	if ((wwpn_len % 4) != 0)
2072255570Strasz		wwpn_len += (4 - (wwpn_len % 4));
2073255570Strasz
2074255570Strasz	wwnn_len = strlen(cs->cs_target->ct_name);
2075255570Strasz	wwnn_len += 1; /* '\0' */
2076255570Strasz	if ((wwnn_len % 4) != 0)
2077255570Strasz		wwnn_len += (4 - (wwnn_len % 4));
2078255570Strasz
2079255570Strasz	if (lun == NULL) {
2080255570Strasz		lun_name_len = 0;
2081255570Strasz	} else {
2082255570Strasz		lun_name_len = strlen(cs->cs_target->ct_name);
2083255570Strasz		lun_name_len += strlen(",lun,XXXXXXXX");
2084255570Strasz		lun_name_len += 1; /* '\0' */
2085255570Strasz		if ((lun_name_len % 4) != 0)
2086255570Strasz			lun_name_len += (4 - (lun_name_len % 4));
2087255570Strasz	}
2088255570Strasz
2089255570Strasz	devid_len = sizeof(struct scsi_vpd_device_id) +
2090255570Strasz		sizeof(struct scsi_vpd_id_descriptor) +
2091255570Strasz		sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN +
2092255570Strasz		sizeof(struct scsi_vpd_id_descriptor) + lun_name_len +
2093255570Strasz		sizeof(struct scsi_vpd_id_descriptor) + wwnn_len +
2094255570Strasz		sizeof(struct scsi_vpd_id_descriptor) + wwpn_len +
2095255570Strasz		sizeof(struct scsi_vpd_id_descriptor) +
2096255570Strasz		sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
2097255570Strasz		sizeof(struct scsi_vpd_id_descriptor) +
2098255570Strasz		sizeof(struct scsi_vpd_id_trgt_port_grp_id);
2099255570Strasz
2100255570Strasz	ctsio->kern_data_ptr = malloc(devid_len, M_CTL, M_WAITOK | M_ZERO);
2101255570Strasz	devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
2102255570Strasz	ctsio->kern_sg_entries = 0;
2103255570Strasz
2104255570Strasz	if (devid_len < alloc_len) {
2105255570Strasz		ctsio->residual = alloc_len - devid_len;
2106255570Strasz		ctsio->kern_data_len = devid_len;
2107255570Strasz		ctsio->kern_total_len = devid_len;
2108255570Strasz	} else {
2109255570Strasz		ctsio->residual = 0;
2110255570Strasz		ctsio->kern_data_len = alloc_len;
2111255570Strasz		ctsio->kern_total_len = alloc_len;
2112255570Strasz	}
2113255570Strasz	ctsio->kern_data_resid = 0;
2114255570Strasz	ctsio->kern_rel_offset = 0;
2115255570Strasz	ctsio->kern_sg_entries = 0;
2116255570Strasz
2117255570Strasz	desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
2118255570Strasz	t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
2119255570Strasz	desc1 = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
2120255570Strasz	    sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN);
2121255570Strasz	desc2 = (struct scsi_vpd_id_descriptor *)(&desc1->identifier[0] +
2122255570Strasz	    lun_name_len);
2123255570Strasz	desc3 = (struct scsi_vpd_id_descriptor *)(&desc2->identifier[0] +
2124255570Strasz	    wwnn_len);
2125255570Strasz	desc4 = (struct scsi_vpd_id_descriptor *)(&desc3->identifier[0] +
2126255570Strasz	    wwpn_len);
2127255570Strasz	desc5 = (struct scsi_vpd_id_descriptor *)(&desc4->identifier[0] +
2128255570Strasz	    sizeof(struct scsi_vpd_id_rel_trgt_port_id));
2129255570Strasz
2130255570Strasz	if (lun != NULL)
2131255570Strasz		devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
2132255570Strasz		    lun->be_lun->lun_type;
2133255570Strasz	else
2134255570Strasz		devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
2135255570Strasz
2136255570Strasz	devid_ptr->page_code = SVPD_DEVICE_ID;
2137255570Strasz
2138255570Strasz	scsi_ulto2b(devid_len - 4, devid_ptr->length);
2139255570Strasz
2140255570Strasz	/*
2141255570Strasz	 * We're using a LUN association here.  i.e., this device ID is a
2142255570Strasz	 * per-LUN identifier.
2143255570Strasz	 */
2144255570Strasz	desc->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_ASCII;
2145255570Strasz	desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
2146255570Strasz	desc->length = sizeof(*t10id) + CTL_DEVID_LEN;
2147255570Strasz	if (lun == NULL || (val = ctl_get_opt(lun->be_lun, "vendor")) == NULL) {
2148255570Strasz		strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
2149255570Strasz	} else {
2150255570Strasz		memset(t10id->vendor, ' ', sizeof(t10id->vendor));
2151255570Strasz		strncpy(t10id->vendor, val,
2152255570Strasz		    min(sizeof(t10id->vendor), strlen(val)));
2153255570Strasz	}
2154255570Strasz
2155255570Strasz	/*
2156255570Strasz	 * If we've actually got a backend, copy the device id from the
2157255570Strasz	 * per-LUN data.  Otherwise, set it to all spaces.
2158255570Strasz	 */
2159255570Strasz	if (lun != NULL) {
2160255570Strasz		/*
2161255570Strasz		 * Copy the backend's LUN ID.
2162255570Strasz		 */
2163255570Strasz		strncpy((char *)t10id->vendor_spec_id,
2164255570Strasz		    (char *)lun->be_lun->device_id, CTL_DEVID_LEN);
2165255570Strasz	} else {
2166255570Strasz		/*
2167255570Strasz		 * No backend, set this to spaces.
2168255570Strasz		 */
2169255570Strasz		memset(t10id->vendor_spec_id, 0x20, CTL_DEVID_LEN);
2170255570Strasz	}
2171255570Strasz
2172255570Strasz	/*
2173255570Strasz	 * desc1 is for the unique LUN name.
2174255570Strasz	 *
2175255570Strasz	 * XXX: According to SPC-3, LUN must report the same ID through
2176255570Strasz	 *      all the ports.  The code below, however, reports the
2177255570Strasz	 *      ID only via iSCSI.
2178255570Strasz	 */
2179255570Strasz	desc1->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8;
2180255570Strasz	desc1->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
2181255570Strasz		SVPD_ID_TYPE_SCSI_NAME;
2182255570Strasz	desc1->length = lun_name_len;
2183255570Strasz	if (lun != NULL) {
2184255570Strasz		/*
2185255570Strasz		 * Find the per-target LUN number.
2186255570Strasz		 */
2187255570Strasz		for (i = 0; i < CTL_MAX_LUNS; i++) {
2188255570Strasz			if (cs->cs_target->ct_luns[i] == lun->lun)
2189255570Strasz				break;
2190255570Strasz		}
2191255570Strasz		KASSERT(i < CTL_MAX_LUNS,
2192255570Strasz		    ("lun %jd not found", (uintmax_t)lun->lun));
2193255570Strasz		ret = snprintf(desc1->identifier, lun_name_len, "%s,lun,%d",
2194255570Strasz		    cs->cs_target->ct_name, i);
2195255570Strasz		KASSERT(ret > 0 && ret <= lun_name_len, ("bad snprintf"));
2196255570Strasz	} else {
2197255570Strasz		KASSERT(lun_name_len == 0, ("no lun, but lun_name_len != 0"));
2198255570Strasz	}
2199255570Strasz
2200255570Strasz	/*
2201255570Strasz	 * desc2 is for the Target Name.
2202255570Strasz	 */
2203255570Strasz	desc2->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8;
2204255570Strasz	desc2->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_TARGET |
2205255570Strasz	    SVPD_ID_TYPE_SCSI_NAME;
2206255570Strasz	desc2->length = wwnn_len;
2207255570Strasz	snprintf(desc2->identifier, wwnn_len, "%s", cs->cs_target->ct_name);
2208255570Strasz
2209255570Strasz	/*
2210255570Strasz	 * desc3 is for the WWPN which is a port asscociation.
2211255570Strasz	 */
2212255570Strasz	desc3->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8;
2213255570Strasz	desc3->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
2214255570Strasz	    SVPD_ID_TYPE_SCSI_NAME;
2215255570Strasz	desc3->length = wwpn_len;
2216255570Strasz	snprintf(desc3->identifier, wwpn_len, "%s,t,0x%4.4x",
2217255570Strasz	    cs->cs_target->ct_name, cs->cs_portal_group_tag);
2218255570Strasz
2219255570Strasz	/*
2220255570Strasz	 * desc3 is for the Relative Target Port(type 4h) identifier
2221255570Strasz	 */
2222255570Strasz	desc4->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_BINARY;
2223255570Strasz	desc4->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
2224255570Strasz	    SVPD_ID_TYPE_RELTARG;
2225255570Strasz	desc4->length = 4;
2226255570Strasz	desc4->identifier[3] = 1;
2227255570Strasz
2228255570Strasz	/*
2229255570Strasz	 * desc4 is for the Target Port Group(type 5h) identifier
2230255570Strasz	 */
2231255570Strasz	desc5->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_BINARY;
2232255570Strasz	desc5->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
2233255570Strasz	    SVPD_ID_TYPE_TPORTGRP;
2234255570Strasz	desc5->length = 4;
2235255570Strasz	desc5->identifier[3] = 1;
2236255570Strasz
2237255570Strasz	ctsio->scsi_status = SCSI_STATUS_OK;
2238255570Strasz
2239255570Strasz	ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
2240255570Strasz	ctsio->be_move_done = ctl_config_move_done;
2241255570Strasz	ctl_datamove((union ctl_io *)ctsio);
2242255570Strasz
2243255570Strasz	return (CTL_RETVAL_COMPLETE);
2244255570Strasz}
2245255570Strasz
2246255570Straszstatic void
2247255570Straszcfiscsi_target_hold(struct cfiscsi_target *ct)
2248255570Strasz{
2249255570Strasz
2250255570Strasz	refcount_acquire(&ct->ct_refcount);
2251255570Strasz}
2252255570Strasz
2253255824Straszstatic void
2254255570Straszcfiscsi_target_release(struct cfiscsi_target *ct)
2255255570Strasz{
2256255570Strasz	struct cfiscsi_softc *softc;
2257255570Strasz
2258255570Strasz	softc = ct->ct_softc;
2259255570Strasz	mtx_lock(&softc->lock);
2260255570Strasz	if (refcount_release(&ct->ct_refcount)) {
2261255570Strasz		TAILQ_REMOVE(&softc->targets, ct, ct_next);
2262255570Strasz		mtx_unlock(&softc->lock);
2263255570Strasz		free(ct, M_CFISCSI);
2264255570Strasz
2265255570Strasz		return;
2266255570Strasz	}
2267255570Strasz	mtx_unlock(&softc->lock);
2268255570Strasz}
2269255570Strasz
2270255570Straszstatic struct cfiscsi_target *
2271255570Straszcfiscsi_target_find(struct cfiscsi_softc *softc, const char *name)
2272255570Strasz{
2273255570Strasz	struct cfiscsi_target *ct;
2274255570Strasz
2275255570Strasz	mtx_lock(&softc->lock);
2276255570Strasz	TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2277255570Strasz		if (strcmp(name, ct->ct_name) != 0)
2278255570Strasz			continue;
2279255570Strasz		cfiscsi_target_hold(ct);
2280255570Strasz		mtx_unlock(&softc->lock);
2281255570Strasz		return (ct);
2282255570Strasz	}
2283255570Strasz	mtx_unlock(&softc->lock);
2284255570Strasz
2285255570Strasz	return (NULL);
2286255570Strasz}
2287255570Strasz
2288255570Straszstatic struct cfiscsi_target *
2289255570Straszcfiscsi_target_find_or_create(struct cfiscsi_softc *softc, const char *name,
2290255570Strasz    const char *alias)
2291255570Strasz{
2292255570Strasz	struct cfiscsi_target *ct, *newct;
2293255570Strasz	int i;
2294255570Strasz
2295255570Strasz	if (name[0] == '\0' || strlen(name) >= CTL_ISCSI_NAME_LEN)
2296255570Strasz		return (NULL);
2297255570Strasz
2298255570Strasz	newct = malloc(sizeof(*newct), M_CFISCSI, M_WAITOK | M_ZERO);
2299255570Strasz
2300255570Strasz	mtx_lock(&softc->lock);
2301255570Strasz	TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2302255570Strasz		if (strcmp(name, ct->ct_name) != 0)
2303255570Strasz			continue;
2304255570Strasz		cfiscsi_target_hold(ct);
2305255570Strasz		mtx_unlock(&softc->lock);
2306255824Strasz		free(newct, M_CFISCSI);
2307255824Strasz		return (ct);
2308255824Strasz	}
2309255824Strasz
2310255837Strasz	for (i = 0; i < CTL_MAX_LUNS; i++)
2311255824Strasz		newct->ct_luns[i] = -1;
2312255824Strasz
2313255824Strasz	strlcpy(newct->ct_name, name, sizeof(newct->ct_name));
2314255570Strasz	if (alias != NULL)
2315255570Strasz		strlcpy(newct->ct_alias, alias, sizeof(newct->ct_alias));
2316255570Strasz	refcount_init(&newct->ct_refcount, 1);
2317255570Strasz	newct->ct_softc = softc;
2318255570Strasz	TAILQ_INSERT_TAIL(&softc->targets, newct, ct_next);
2319255570Strasz	mtx_unlock(&softc->lock);
2320255570Strasz
2321255570Strasz	return (newct);
2322255570Strasz}
2323255570Strasz
2324255570Strasz/*
2325255570Strasz * Takes LUN from the target space and returns LUN from the CTL space.
2326255570Strasz */
2327255570Straszstatic uint32_t
2328255570Straszcfiscsi_map_lun(void *arg, uint32_t lun)
2329255570Strasz{
2330255570Strasz	struct cfiscsi_session *cs;
2331255570Strasz
2332255570Strasz	cs = arg;
2333255570Strasz
2334255570Strasz	if (lun >= CTL_MAX_LUNS) {
2335255570Strasz		CFISCSI_DEBUG("requested lun number %d is higher "
2336255570Strasz		    "than maximum %d", lun, CTL_MAX_LUNS - 1);
2337255570Strasz		return (0xffffffff);
2338255824Strasz	}
2339255824Strasz
2340255824Strasz	if (cs->cs_target->ct_luns[lun] < 0)
2341255824Strasz		return (0xffffffff);
2342255837Strasz
2343255824Strasz	return (cs->cs_target->ct_luns[lun]);
2344255824Strasz}
2345255824Strasz
2346255824Straszstatic int
2347255570Straszcfiscsi_target_set_lun(struct cfiscsi_target *ct,
2348255570Strasz    unsigned long lun_id, unsigned long ctl_lun_id)
2349255570Strasz{
2350255570Strasz
2351255570Strasz	if (lun_id >= CTL_MAX_LUNS) {
2352255570Strasz		CFISCSI_WARN("requested lun number %ld is higher "
2353255570Strasz		    "than maximum %d", lun_id, CTL_MAX_LUNS - 1);
2354255570Strasz		return (-1);
2355255570Strasz	}
2356255570Strasz
2357255570Strasz	if (ct->ct_luns[lun_id] >= 0) {
2358255570Strasz		/*
2359255570Strasz		 * CTL calls cfiscsi_lun_enable() twice for each LUN - once
2360255570Strasz		 * when the LUN is created, and a second time just before
2361255570Strasz		 * the port is brought online; don't emit warnings
2362255570Strasz		 * for that case.
2363255570Strasz		 */
2364255570Strasz		if (ct->ct_luns[lun_id] == ctl_lun_id)
2365255570Strasz			return (0);
2366255570Strasz		CFISCSI_WARN("lun %ld already allocated", lun_id);
2367255570Strasz		return (-1);
2368255570Strasz	}
2369255570Strasz
2370255570Strasz#if 0
2371255570Strasz	CFISCSI_DEBUG("adding mapping for lun %ld, target %s "
2372255570Strasz	    "to ctl lun %ld", lun_id, ct->ct_name, ctl_lun_id);
2373255570Strasz#endif
2374255570Strasz
2375255570Strasz	ct->ct_luns[lun_id] = ctl_lun_id;
2376255570Strasz	cfiscsi_target_hold(ct);
2377255570Strasz
2378255570Strasz	return (0);
2379255570Strasz}
2380255570Strasz
2381255570Straszstatic int
2382255570Straszcfiscsi_target_unset_lun(struct cfiscsi_target *ct, unsigned long lun_id)
2383255570Strasz{
2384255570Strasz
2385255570Strasz	if (ct->ct_luns[lun_id] < 0) {
2386255570Strasz		CFISCSI_WARN("lun %ld not allocated", lun_id);
2387255570Strasz		return (-1);
2388255570Strasz	}
2389255570Strasz
2390255570Strasz	ct->ct_luns[lun_id] = -1;
2391255570Strasz	cfiscsi_target_release(ct);
2392255570Strasz
2393255570Strasz	return (0);
2394255570Strasz}
2395255570Strasz
2396255570Straszstatic int
2397255570Straszcfiscsi_lun_enable(void *arg, struct ctl_id target_id, int lun_id)
2398255570Strasz{
2399255570Strasz	struct cfiscsi_softc *softc;
2400255570Strasz	struct cfiscsi_target *ct;
2401255570Strasz	const char *target = NULL, *target_alias = NULL;
2402255570Strasz	const char *lun = NULL;
2403255570Strasz	unsigned long tmp;
2404255570Strasz
2405255570Strasz	softc = (struct cfiscsi_softc *)arg;
2406255570Strasz
2407255570Strasz	target = ctl_get_opt(control_softc->ctl_luns[lun_id]->be_lun,
2408255570Strasz	    "cfiscsi_target");
2409255570Strasz	target_alias = ctl_get_opt(control_softc->ctl_luns[lun_id]->be_lun,
2410255570Strasz	    "cfiscsi_target_alias");
2411255570Strasz	lun = ctl_get_opt(control_softc->ctl_luns[lun_id]->be_lun,
2412255837Strasz	    "cfiscsi_lun");
2413255837Strasz
2414255837Strasz	if (target == NULL && lun == NULL)
2415255837Strasz		return (0);
2416255837Strasz
2417255837Strasz	if (target == NULL || lun == NULL) {
2418255837Strasz		CFISCSI_WARN("lun added with cfiscsi_target, but without "
2419255570Strasz		    "cfiscsi_lun, or the other way around; ignoring");
2420255570Strasz		return (0);
2421255570Strasz	}
2422255570Strasz
2423255570Strasz	ct = cfiscsi_target_find_or_create(softc, target, target_alias);
2424255570Strasz	if (ct == NULL) {
2425255570Strasz		CFISCSI_WARN("failed to create target \"%s\"", target);
2426255570Strasz		return (0);
2427255570Strasz	}
2428255570Strasz
2429255570Strasz	tmp = strtoul(lun, NULL, 10);
2430255570Strasz	cfiscsi_target_set_lun(ct, tmp, lun_id);
2431255570Strasz	cfiscsi_target_release(ct);
2432255570Strasz	return (0);
2433255570Strasz}
2434255570Strasz
2435255570Straszstatic int
2436255570Straszcfiscsi_lun_disable(void *arg, struct ctl_id target_id, int lun_id)
2437255570Strasz{
2438255570Strasz	struct cfiscsi_softc *softc;
2439255570Strasz	struct cfiscsi_target *ct;
2440255570Strasz	int i;
2441255570Strasz
2442255570Strasz	softc = (struct cfiscsi_softc *)arg;
2443255570Strasz
2444255570Strasz	mtx_lock(&softc->lock);
2445255570Strasz	TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2446255570Strasz		for (i = 0; i < CTL_MAX_LUNS; i++) {
2447255837Strasz			if (ct->ct_luns[i] < 0)
2448255837Strasz				continue;
2449255837Strasz			if (ct->ct_luns[i] != lun_id)
2450255837Strasz				continue;
2451255837Strasz			mtx_unlock(&softc->lock);
2452255837Strasz			cfiscsi_target_unset_lun(ct, i);
2453255837Strasz			return (0);
2454255570Strasz		}
2455255570Strasz	}
2456255570Strasz	mtx_unlock(&softc->lock);
2457255570Strasz	return (0);
2458255570Strasz}
2459255570Strasz
2460255570Straszstatic void
2461255570Straszcfiscsi_datamove_in(union ctl_io *io)
2462255570Strasz{
2463255570Strasz	struct cfiscsi_session *cs;
2464255570Strasz	struct icl_pdu *request, *response;
2465255570Strasz	const struct iscsi_bhs_scsi_command *bhssc;
2466255570Strasz	struct iscsi_bhs_data_in *bhsdi;
2467255570Strasz	struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
2468255570Strasz	size_t len, expected_len, sg_len, buffer_offset;
2469255570Strasz	const char *sg_addr;
2470255570Strasz	int ctl_sg_count, error, i;
2471255570Strasz
2472255570Strasz	request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2473255570Strasz	cs = PDU_SESSION(request);
2474255570Strasz
2475255570Strasz	bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
2476255570Strasz	KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2477255570Strasz	    ISCSI_BHS_OPCODE_SCSI_COMMAND,
2478255570Strasz	    ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND"));
2479255570Strasz
2480255570Strasz	if (io->scsiio.kern_sg_entries > 0) {
2481255570Strasz		ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
2482255570Strasz		ctl_sg_count = io->scsiio.kern_sg_entries;
2483255570Strasz	} else {
2484255570Strasz		ctl_sglist = &ctl_sg_entry;
2485255570Strasz		ctl_sglist->addr = io->scsiio.kern_data_ptr;
2486255570Strasz		ctl_sglist->len = io->scsiio.kern_data_len;
2487255570Strasz		ctl_sg_count = 1;
2488255570Strasz	}
2489255570Strasz
2490255570Strasz	/*
2491255570Strasz	 * This is the total amount of data to be transferred within the current
2492255570Strasz	 * SCSI command.  We need to record it so that we can properly report
2493255570Strasz	 * underflow/underflow.
2494255570Strasz	 */
2495255570Strasz	PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len;
2496255570Strasz
2497255570Strasz	/*
2498255570Strasz	 * This is the offset within the current SCSI command; for the first
2499255570Strasz	 * call to cfiscsi_datamove() it will be 0, and for subsequent ones
2500255570Strasz	 * it will be the sum of lengths of previous ones.
2501255570Strasz	 */
2502255570Strasz	buffer_offset = io->scsiio.kern_rel_offset;
2503255570Strasz
2504255570Strasz	/*
2505255570Strasz	 * This is the transfer length expected by the initiator.  In theory,
2506255570Strasz	 * it could be different from the correct amount of data from the SCSI
2507255570Strasz	 * point of view, even if that doesn't make any sense.
2508255570Strasz	 */
2509255570Strasz	expected_len = ntohl(bhssc->bhssc_expected_data_transfer_length);
2510255570Strasz#if 0
2511255570Strasz	if (expected_len != io->scsiio.kern_total_len) {
2512255570Strasz		CFISCSI_SESSION_DEBUG(cs, "expected transfer length %zd, "
2513255570Strasz		    "actual length %zd", expected_len,
2514255570Strasz		    (size_t)io->scsiio.kern_total_len);
2515255570Strasz	}
2516255570Strasz#endif
2517255570Strasz
2518255570Strasz	if (buffer_offset >= expected_len) {
2519255570Strasz#if 0
2520255570Strasz		CFISCSI_SESSION_DEBUG(cs, "buffer_offset = %zd, "
2521255570Strasz		    "already sent the expected len", buffer_offset);
2522255570Strasz#endif
2523255570Strasz		io->scsiio.be_move_done(io);
2524255570Strasz		return;
2525255570Strasz	}
2526255570Strasz
2527255570Strasz	i = 0;
2528255570Strasz	sg_addr = NULL;
2529255570Strasz	sg_len = 0;
2530255570Strasz	response = NULL;
2531255570Strasz	bhsdi = NULL;
2532255570Strasz	for (;;) {
2533255570Strasz		if (response == NULL) {
2534255570Strasz			response = cfiscsi_pdu_new_response(request, M_NOWAIT);
2535255570Strasz			if (response == NULL) {
2536255570Strasz				CFISCSI_SESSION_WARN(cs, "failed to "
2537255570Strasz				    "allocate memory; dropping connection");
2538255570Strasz				ctl_set_busy(&io->scsiio);
2539255570Strasz				io->scsiio.be_move_done(io);
2540255570Strasz				cfiscsi_session_terminate(cs);
2541255570Strasz				return;
2542255570Strasz			}
2543255570Strasz			bhsdi = (struct iscsi_bhs_data_in *)response->ip_bhs;
2544255570Strasz			bhsdi->bhsdi_opcode = ISCSI_BHS_OPCODE_SCSI_DATA_IN;
2545255570Strasz			bhsdi->bhsdi_initiator_task_tag =
2546255570Strasz			    bhssc->bhssc_initiator_task_tag;
2547255570Strasz			bhsdi->bhsdi_datasn = htonl(PDU_EXPDATASN(request));
2548255570Strasz			PDU_EXPDATASN(request)++;
2549255570Strasz			bhsdi->bhsdi_buffer_offset = htonl(buffer_offset);
2550255570Strasz		}
2551255570Strasz
2552255570Strasz		KASSERT(i < ctl_sg_count, ("i >= ctl_sg_count"));
2553255570Strasz		if (sg_len == 0) {
2554255570Strasz			sg_addr = ctl_sglist[i].addr;
2555255570Strasz			sg_len = ctl_sglist[i].len;
2556255570Strasz			KASSERT(sg_len > 0, ("sg_len <= 0"));
2557255570Strasz		}
2558255570Strasz
2559255570Strasz		len = sg_len;
2560255570Strasz
2561255570Strasz		/*
2562255570Strasz		 * Truncate to maximum data segment length.
2563255570Strasz		 */
2564255570Strasz		KASSERT(response->ip_data_len < cs->cs_max_data_segment_length,
2565255570Strasz		    ("ip_data_len %zd >= max_data_segment_length %zd",
2566255570Strasz		    response->ip_data_len, cs->cs_max_data_segment_length));
2567255570Strasz		if (response->ip_data_len + len >
2568255570Strasz		    cs->cs_max_data_segment_length) {
2569255570Strasz			len = cs->cs_max_data_segment_length -
2570255570Strasz			    response->ip_data_len;
2571255570Strasz			KASSERT(len <= sg_len, ("len %zd > sg_len %zd",
2572255570Strasz			    len, sg_len));
2573255570Strasz		}
2574255570Strasz
2575255570Strasz		/*
2576255570Strasz		 * Truncate to expected data transfer length.
2577255570Strasz		 */
2578255570Strasz		KASSERT(buffer_offset + response->ip_data_len < expected_len,
2579255570Strasz		    ("buffer_offset %zd + ip_data_len %zd >= expected_len %zd",
2580255570Strasz		    buffer_offset, response->ip_data_len, expected_len));
2581255570Strasz		if (buffer_offset + response->ip_data_len + len > expected_len) {
2582255570Strasz			CFISCSI_SESSION_DEBUG(cs, "truncating from %zd "
2583255570Strasz			    "to expected data transfer length %zd",
2584255570Strasz			    buffer_offset + response->ip_data_len + len, expected_len);
2585255570Strasz			len = expected_len - (buffer_offset + response->ip_data_len);
2586255570Strasz			KASSERT(len <= sg_len, ("len %zd > sg_len %zd",
2587255570Strasz			    len, sg_len));
2588255570Strasz		}
2589255570Strasz
2590255570Strasz		error = icl_pdu_append_data(response, sg_addr, len, M_NOWAIT);
2591255570Strasz		if (error != 0) {
2592255570Strasz			CFISCSI_SESSION_WARN(cs, "failed to "
2593255570Strasz			    "allocate memory; dropping connection");
2594255570Strasz			icl_pdu_free(response);
2595255570Strasz			ctl_set_busy(&io->scsiio);
2596255570Strasz			io->scsiio.be_move_done(io);
2597255570Strasz			cfiscsi_session_terminate(cs);
2598255570Strasz			return;
2599255570Strasz		}
2600255570Strasz		sg_addr += len;
2601255570Strasz		sg_len -= len;
2602255570Strasz
2603255570Strasz		KASSERT(buffer_offset + request->ip_data_len <= expected_len,
2604255570Strasz		    ("buffer_offset %zd + ip_data_len %zd > expected_len %zd",
2605255570Strasz		    buffer_offset, request->ip_data_len, expected_len));
2606255570Strasz		if (buffer_offset + request->ip_data_len == expected_len) {
2607255570Strasz			/*
2608255570Strasz			 * Already have the amount of data the initiator wanted.
2609255570Strasz			 */
2610255570Strasz			break;
2611255570Strasz		}
2612255570Strasz
2613255570Strasz		if (sg_len == 0) {
2614255570Strasz			/*
2615255570Strasz			 * End of scatter-gather segment;
2616255570Strasz			 * proceed to the next one...
2617255570Strasz			 */
2618255570Strasz			if (i == ctl_sg_count - 1) {
2619255570Strasz				/*
2620255570Strasz				 * ... unless this was the last one.
2621255570Strasz				 */
2622255570Strasz				break;
2623255570Strasz			}
2624255570Strasz			i++;
2625255570Strasz		}
2626255570Strasz
2627255570Strasz		if (response->ip_data_len == cs->cs_max_data_segment_length) {
2628255570Strasz			/*
2629255570Strasz			 * Can't stuff more data into the current PDU;
2630255570Strasz			 * queue it.  Note that's not enough to check
2631255570Strasz			 * for kern_data_resid == 0 instead; there
2632255570Strasz			 * may be several Data-In PDUs for the final
2633255570Strasz			 * call to cfiscsi_datamove(), and we want
2634255570Strasz			 * to set the F flag only on the last of them.
2635255570Strasz			 */
2636255570Strasz			buffer_offset += response->ip_data_len;
2637255570Strasz			if (buffer_offset == io->scsiio.kern_total_len ||
2638255570Strasz			    buffer_offset == expected_len)
2639255570Strasz				bhsdi->bhsdi_flags |= BHSDI_FLAGS_F;
2640255570Strasz			cfiscsi_pdu_queue(response);
2641255570Strasz			response = NULL;
2642255570Strasz			bhsdi = NULL;
2643255570Strasz		}
2644255570Strasz	}
2645255570Strasz	if (response != NULL) {
2646255570Strasz		buffer_offset += response->ip_data_len;
2647255570Strasz		if (buffer_offset == io->scsiio.kern_total_len ||
2648255570Strasz		    buffer_offset == expected_len)
2649255570Strasz			bhsdi->bhsdi_flags |= BHSDI_FLAGS_F;
2650255570Strasz		KASSERT(response->ip_data_len > 0, ("sending empty Data-In"));
2651255570Strasz		cfiscsi_pdu_queue(response);
2652255570Strasz	}
2653255570Strasz
2654255570Strasz	io->scsiio.be_move_done(io);
2655255570Strasz}
2656255570Strasz
2657255570Straszstatic void
2658255570Straszcfiscsi_datamove_out(union ctl_io *io)
2659255570Strasz{
2660255570Strasz	struct cfiscsi_session *cs;
2661255570Strasz	struct icl_pdu *request, *response;
2662255570Strasz	const struct iscsi_bhs_scsi_command *bhssc;
2663255570Strasz	struct iscsi_bhs_r2t *bhsr2t;
2664255570Strasz	struct cfiscsi_data_wait *cdw;
2665255570Strasz	uint32_t target_transfer_tag;
2666255570Strasz	bool done;
2667255570Strasz
2668255570Strasz	request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2669255570Strasz	cs = PDU_SESSION(request);
2670255570Strasz
2671	bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
2672	KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2673	    ISCSI_BHS_OPCODE_SCSI_COMMAND,
2674	    ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND"));
2675
2676	/*
2677	 * We need to record it so that we can properly report
2678	 * underflow/underflow.
2679	 */
2680	PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len;
2681
2682	/*
2683	 * We hadn't received anything during this datamove yet.
2684	 */
2685	io->scsiio.ext_data_filled = 0;
2686
2687	target_transfer_tag =
2688	    atomic_fetchadd_32(&cs->cs_target_transfer_tag, 1);
2689
2690#if 0
2691	CFISCSI_SESSION_DEBUG(cs, "expecting Data-Out with initiator "
2692	    "task tag 0x%x, target transfer tag 0x%x",
2693	    bhssc->bhssc_initiator_task_tag, target_transfer_tag);
2694#endif
2695	cdw = uma_zalloc(cfiscsi_data_wait_zone, M_NOWAIT | M_ZERO);
2696	if (cdw == NULL) {
2697		CFISCSI_SESSION_WARN(cs, "failed to "
2698		    "allocate memory; dropping connection");
2699		ctl_set_busy(&io->scsiio);
2700		io->scsiio.be_move_done(io);
2701		cfiscsi_session_terminate(cs);
2702		return;
2703	}
2704	cdw->cdw_ctl_io = io;
2705	cdw->cdw_target_transfer_tag = target_transfer_tag;
2706	cdw->cdw_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2707
2708	if (cs->cs_immediate_data && io->scsiio.kern_rel_offset <
2709	    icl_pdu_data_segment_length(request)) {
2710		done = cfiscsi_handle_data_segment(request, cdw);
2711		if (done) {
2712			uma_zfree(cfiscsi_data_wait_zone, cdw);
2713			io->scsiio.be_move_done(io);
2714			return;
2715		}
2716	}
2717
2718	CFISCSI_SESSION_LOCK(cs);
2719	TAILQ_INSERT_TAIL(&cs->cs_waiting_for_data_out, cdw, cdw_next);
2720	CFISCSI_SESSION_UNLOCK(cs);
2721
2722	/*
2723	 * XXX: We should limit the number of outstanding R2T PDUs
2724	 * 	per task to MaxOutstandingR2T.
2725	 */
2726	response = cfiscsi_pdu_new_response(request, M_NOWAIT);
2727	if (response == NULL) {
2728		CFISCSI_SESSION_WARN(cs, "failed to "
2729		    "allocate memory; dropping connection");
2730		ctl_set_busy(&io->scsiio);
2731		io->scsiio.be_move_done(io);
2732		cfiscsi_session_terminate(cs);
2733		return;
2734	}
2735	bhsr2t = (struct iscsi_bhs_r2t *)response->ip_bhs;
2736	bhsr2t->bhsr2t_opcode = ISCSI_BHS_OPCODE_R2T;
2737	bhsr2t->bhsr2t_flags = 0x80;
2738	bhsr2t->bhsr2t_lun = bhssc->bhssc_lun;
2739	bhsr2t->bhsr2t_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2740	bhsr2t->bhsr2t_target_transfer_tag = target_transfer_tag;
2741	/*
2742	 * XXX: Here we assume that cfiscsi_datamove() won't ever
2743	 *	be running concurrently on several CPUs for a given
2744	 *	command.
2745	 */
2746	bhsr2t->bhsr2t_r2tsn = htonl(PDU_R2TSN(request));
2747	PDU_R2TSN(request)++;
2748	/*
2749	 * This is the offset within the current SCSI command;
2750	 * i.e. for the first call of datamove(), it will be 0,
2751	 * and for subsequent ones it will be the sum of lengths
2752	 * of previous ones.
2753	 *
2754	 * The ext_data_filled is to account for unsolicited
2755	 * (immediate) data that might have already arrived.
2756	 */
2757	bhsr2t->bhsr2t_buffer_offset =
2758	    htonl(io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled);
2759	/*
2760	 * This is the total length (sum of S/G lengths) this call
2761	 * to cfiscsi_datamove() is supposed to handle.
2762	 *
2763	 * XXX: Limit it to MaxBurstLength.
2764	 */
2765	bhsr2t->bhsr2t_desired_data_transfer_length =
2766	    htonl(io->scsiio.kern_data_len - io->scsiio.ext_data_filled);
2767	cfiscsi_pdu_queue(response);
2768}
2769
2770static void
2771cfiscsi_datamove(union ctl_io *io)
2772{
2773
2774	if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
2775		cfiscsi_datamove_in(io);
2776	else
2777		cfiscsi_datamove_out(io);
2778}
2779
2780static void
2781cfiscsi_scsi_command_done(union ctl_io *io)
2782{
2783	struct icl_pdu *request, *response;
2784	struct iscsi_bhs_scsi_command *bhssc;
2785	struct iscsi_bhs_scsi_response *bhssr;
2786#ifdef DIAGNOSTIC
2787	struct cfiscsi_data_wait *cdw;
2788#endif
2789	struct cfiscsi_session *cs;
2790	uint16_t sense_length;
2791
2792	request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2793	cs = PDU_SESSION(request);
2794	bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
2795	KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2796	    ISCSI_BHS_OPCODE_SCSI_COMMAND,
2797	    ("replying to wrong opcode 0x%x", bhssc->bhssc_opcode));
2798
2799	//CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x",
2800	//    bhssc->bhssc_initiator_task_tag);
2801
2802#ifdef DIAGNOSTIC
2803	CFISCSI_SESSION_LOCK(cs);
2804	TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next)
2805		KASSERT(bhssc->bhssc_initiator_task_tag !=
2806		    cdw->cdw_initiator_task_tag, ("dangling cdw"));
2807	CFISCSI_SESSION_UNLOCK(cs);
2808#endif
2809
2810	/*
2811	 * Do not return status for aborted commands.
2812	 * There are exceptions, but none supported by CTL yet.
2813	 */
2814	if (io->io_hdr.status == CTL_CMD_ABORTED) {
2815		ctl_free_io(io);
2816		icl_pdu_free(request);
2817		return;
2818	}
2819
2820	response = cfiscsi_pdu_new_response(request, M_WAITOK);
2821	bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
2822	bhssr->bhssr_opcode = ISCSI_BHS_OPCODE_SCSI_RESPONSE;
2823	bhssr->bhssr_flags = 0x80;
2824	/*
2825	 * XXX: We don't deal with bidirectional under/overflows;
2826	 *	does anything actually support those?
2827	 */
2828	if (PDU_TOTAL_TRANSFER_LEN(request) <
2829	    ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2830		bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_UNDERFLOW;
2831		bhssr->bhssr_residual_count =
2832		    htonl(ntohl(bhssc->bhssc_expected_data_transfer_length) -
2833		    PDU_TOTAL_TRANSFER_LEN(request));
2834		//CFISCSI_SESSION_DEBUG(cs, "underflow; residual count %d",
2835		//    ntohl(bhssr->bhssr_residual_count));
2836	} else if (PDU_TOTAL_TRANSFER_LEN(request) >
2837	    ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2838		bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_OVERFLOW;
2839		bhssr->bhssr_residual_count =
2840		    htonl(PDU_TOTAL_TRANSFER_LEN(request) -
2841		    ntohl(bhssc->bhssc_expected_data_transfer_length));
2842		//CFISCSI_SESSION_DEBUG(cs, "overflow; residual count %d",
2843		//    ntohl(bhssr->bhssr_residual_count));
2844	}
2845	bhssr->bhssr_response = BHSSR_RESPONSE_COMMAND_COMPLETED;
2846	bhssr->bhssr_status = io->scsiio.scsi_status;
2847	bhssr->bhssr_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2848	bhssr->bhssr_expdatasn = htonl(PDU_EXPDATASN(request));
2849
2850	if (io->scsiio.sense_len > 0) {
2851#if 0
2852		CFISCSI_SESSION_DEBUG(cs, "returning %d bytes of sense data",
2853		    io->scsiio.sense_len);
2854#endif
2855		sense_length = htons(io->scsiio.sense_len);
2856		icl_pdu_append_data(response,
2857		    &sense_length, sizeof(sense_length), M_WAITOK);
2858		icl_pdu_append_data(response,
2859		    &io->scsiio.sense_data, io->scsiio.sense_len, M_WAITOK);
2860	}
2861
2862	ctl_free_io(io);
2863	icl_pdu_free(request);
2864	cfiscsi_pdu_queue(response);
2865}
2866
2867static void
2868cfiscsi_task_management_done(union ctl_io *io)
2869{
2870	struct icl_pdu *request, *response;
2871	struct iscsi_bhs_task_management_request *bhstmr;
2872	struct iscsi_bhs_task_management_response *bhstmr2;
2873	struct cfiscsi_data_wait *cdw, *tmpcdw;
2874	struct cfiscsi_session *cs;
2875
2876	request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2877	cs = PDU_SESSION(request);
2878	bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
2879	KASSERT((bhstmr->bhstmr_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2880	    ISCSI_BHS_OPCODE_TASK_REQUEST,
2881	    ("replying to wrong opcode 0x%x", bhstmr->bhstmr_opcode));
2882
2883#if 0
2884	CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x; referenced task tag 0x%x",
2885	    bhstmr->bhstmr_initiator_task_tag,
2886	    bhstmr->bhstmr_referenced_task_tag);
2887#endif
2888
2889	if ((bhstmr->bhstmr_function & ~0x80) ==
2890	    BHSTMR_FUNCTION_ABORT_TASK) {
2891		/*
2892		 * Make sure we no longer wait for Data-Out for this command.
2893		 */
2894		CFISCSI_SESSION_LOCK(cs);
2895		TAILQ_FOREACH_SAFE(cdw,
2896		    &cs->cs_waiting_for_data_out, cdw_next, tmpcdw) {
2897			if (bhstmr->bhstmr_referenced_task_tag !=
2898			    cdw->cdw_initiator_task_tag)
2899				continue;
2900
2901#if 0
2902			CFISCSI_SESSION_DEBUG(cs, "removing csw for initiator task "
2903			    "tag 0x%x", bhstmr->bhstmr_initiator_task_tag);
2904#endif
2905			TAILQ_REMOVE(&cs->cs_waiting_for_data_out,
2906			    cdw, cdw_next);
2907			cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io);
2908			uma_zfree(cfiscsi_data_wait_zone, cdw);
2909		}
2910		CFISCSI_SESSION_UNLOCK(cs);
2911	}
2912
2913	response = cfiscsi_pdu_new_response(request, M_WAITOK);
2914	bhstmr2 = (struct iscsi_bhs_task_management_response *)
2915	    response->ip_bhs;
2916	bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE;
2917	bhstmr2->bhstmr_flags = 0x80;
2918	if (io->io_hdr.status == CTL_SUCCESS) {
2919		bhstmr2->bhstmr_response = BHSTMR_RESPONSE_FUNCTION_COMPLETE;
2920	} else {
2921		/*
2922		 * XXX: How to figure out what exactly went wrong?  iSCSI spec
2923		 * 	expects us to provide detailed error, e.g. "Task does
2924		 * 	not exist" or "LUN does not exist".
2925		 */
2926		CFISCSI_SESSION_DEBUG(cs, "BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED");
2927		bhstmr2->bhstmr_response =
2928		    BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED;
2929	}
2930	bhstmr2->bhstmr_initiator_task_tag = bhstmr->bhstmr_initiator_task_tag;
2931
2932	ctl_free_io(io);
2933	icl_pdu_free(request);
2934	cfiscsi_pdu_queue(response);
2935}
2936
2937static void
2938cfiscsi_done(union ctl_io *io)
2939{
2940	struct icl_pdu *request;
2941	struct cfiscsi_session *cs;
2942
2943	KASSERT(((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE),
2944		("invalid CTL status %#x", io->io_hdr.status));
2945
2946	request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2947	if (request == NULL) {
2948		/*
2949		 * Implicit task termination has just completed; nothing to do.
2950		 */
2951		return;
2952	}
2953
2954	cs = PDU_SESSION(request);
2955	refcount_release(&cs->cs_outstanding_ctl_pdus);
2956
2957	switch (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) {
2958	case ISCSI_BHS_OPCODE_SCSI_COMMAND:
2959		cfiscsi_scsi_command_done(io);
2960		break;
2961	case ISCSI_BHS_OPCODE_TASK_REQUEST:
2962		cfiscsi_task_management_done(io);
2963		break;
2964	default:
2965		panic("cfiscsi_done called with wrong opcode 0x%x",
2966		    request->ip_bhs->bhs_opcode);
2967	}
2968}
2969