ctl_frontend_iscsi.c revision 265490
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 265490 2014-05-07 06:18:03Z trasz $
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 265490 2014-05-07 06:18:03Z trasz $");
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>
62256163Strasz#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
78255570Straszstatic MALLOC_DEFINE(M_CFISCSI, "cfiscsi", "Memory used for CTL iSCSI frontend");
79255570Straszstatic uma_zone_t cfiscsi_data_wait_zone;
80255570Strasz
81255570StraszSYSCTL_NODE(_kern_cam_ctl, OID_AUTO, iscsi, CTLFLAG_RD, 0,
82255570Strasz    "CAM Target Layer iSCSI Frontend");
83255570Straszstatic int debug = 3;
84255570StraszTUNABLE_INT("kern.cam.ctl.iscsi.debug", &debug);
85255570StraszSYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, debug, CTLFLAG_RW,
86255570Strasz    &debug, 1, "Enable debug messages");
87255570Straszstatic int ping_timeout = 5;
88255570StraszTUNABLE_INT("kern.cam.ctl.iscsi.ping_timeout", &ping_timeout);
89255570StraszSYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, ping_timeout, CTLFLAG_RW,
90255570Strasz    &ping_timeout, 5, "Interval between ping (NOP-Out) requests, in seconds");
91255570Straszstatic int login_timeout = 60;
92255570StraszTUNABLE_INT("kern.cam.ctl.iscsi.login_timeout", &login_timeout);
93255570StraszSYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, login_timeout, CTLFLAG_RW,
94255570Strasz    &login_timeout, 60, "Time to wait for ctld(8) to finish Login Phase, in seconds");
95255570Straszstatic int maxcmdsn_delta = 256;
96255570StraszTUNABLE_INT("kern.cam.ctl.iscsi.maxcmdsn_delta", &maxcmdsn_delta);
97255570StraszSYSCTL_INT(_kern_cam_ctl_iscsi, OID_AUTO, maxcmdsn_delta, CTLFLAG_RW,
98255570Strasz    &maxcmdsn_delta, 256, "Number of commands the initiator can send "
99255570Strasz    "without confirmation");
100255570Strasz
101255570Strasz#define	CFISCSI_DEBUG(X, ...)					\
102255570Strasz	if (debug > 1) {					\
103255570Strasz		printf("%s: " X "\n", __func__, ## __VA_ARGS__);\
104255570Strasz	} while (0)
105255570Strasz
106255570Strasz#define	CFISCSI_WARN(X, ...)					\
107255570Strasz	if (debug > 0) {					\
108255570Strasz		printf("WARNING: %s: " X "\n",			\
109255570Strasz		    __func__, ## __VA_ARGS__);			\
110255570Strasz	} while (0)
111255570Strasz
112255570Strasz#define	CFISCSI_SESSION_DEBUG(S, X, ...)			\
113255570Strasz	if (debug > 1) {					\
114255570Strasz		printf("%s: %s (%s): " X "\n",			\
115255570Strasz		    __func__, S->cs_initiator_addr,		\
116255570Strasz		    S->cs_initiator_name, ## __VA_ARGS__);	\
117255570Strasz	} while (0)
118255570Strasz
119255570Strasz#define	CFISCSI_SESSION_WARN(S, X, ...)				\
120255570Strasz	if (debug > 0) {					\
121255570Strasz		printf("WARNING: %s (%s): " X "\n",		\
122255570Strasz		    S->cs_initiator_addr,			\
123255570Strasz		    S->cs_initiator_name, ## __VA_ARGS__);	\
124255570Strasz	} while (0)
125255570Strasz
126255570Strasz#define CFISCSI_SESSION_LOCK(X)		mtx_lock(&X->cs_lock)
127255570Strasz#define CFISCSI_SESSION_UNLOCK(X)	mtx_unlock(&X->cs_lock)
128255570Strasz#define CFISCSI_SESSION_LOCK_ASSERT(X)	mtx_assert(&X->cs_lock, MA_OWNED)
129255570Strasz
130255570Strasz#define	CONN_SESSION(X)			((struct cfiscsi_session *)(X)->ic_prv0)
131255570Strasz#define	PDU_SESSION(X)			CONN_SESSION((X)->ip_conn)
132255570Strasz#define	PDU_EXPDATASN(X)		(X)->ip_prv0
133255570Strasz#define	PDU_TOTAL_TRANSFER_LEN(X)	(X)->ip_prv1
134255570Strasz#define	PDU_R2TSN(X)			(X)->ip_prv2
135255570Strasz
136255570Straszint		cfiscsi_init(void);
137255570Straszstatic void	cfiscsi_online(void *arg);
138255570Straszstatic void	cfiscsi_offline(void *arg);
139255570Straszstatic int	cfiscsi_targ_enable(void *arg, struct ctl_id targ_id);
140255570Straszstatic int	cfiscsi_targ_disable(void *arg, struct ctl_id targ_id);
141255570Straszstatic int	cfiscsi_lun_enable(void *arg,
142255570Strasz		    struct ctl_id target_id, int lun_id);
143255570Straszstatic int	cfiscsi_lun_disable(void *arg,
144255570Strasz		    struct ctl_id target_id, int lun_id);
145255570Straszstatic int	cfiscsi_ioctl(struct cdev *dev,
146255570Strasz		    u_long cmd, caddr_t addr, int flag, struct thread *td);
147255570Straszstatic int	cfiscsi_devid(struct ctl_scsiio *ctsio, int alloc_len);
148255570Straszstatic void	cfiscsi_datamove(union ctl_io *io);
149255570Straszstatic void	cfiscsi_done(union ctl_io *io);
150255570Straszstatic uint32_t	cfiscsi_map_lun(void *arg, uint32_t lun);
151255848Straszstatic bool	cfiscsi_pdu_update_cmdsn(const struct icl_pdu *request);
152255570Straszstatic void	cfiscsi_pdu_handle_nop_out(struct icl_pdu *request);
153255570Straszstatic void	cfiscsi_pdu_handle_scsi_command(struct icl_pdu *request);
154255570Straszstatic void	cfiscsi_pdu_handle_task_request(struct icl_pdu *request);
155255570Straszstatic void	cfiscsi_pdu_handle_data_out(struct icl_pdu *request);
156255570Straszstatic void	cfiscsi_pdu_handle_logout_request(struct icl_pdu *request);
157255570Straszstatic void	cfiscsi_session_terminate(struct cfiscsi_session *cs);
158255570Straszstatic struct cfiscsi_target	*cfiscsi_target_find(struct cfiscsi_softc
159255570Strasz		    *softc, const char *name);
160255570Straszstatic void	cfiscsi_target_release(struct cfiscsi_target *ct);
161255570Straszstatic void	cfiscsi_session_delete(struct cfiscsi_session *cs);
162255570Strasz
163255570Straszstatic struct cfiscsi_softc cfiscsi_softc;
164255570Straszextern struct ctl_softc *control_softc;
165255570Strasz
166255570Straszstatic int cfiscsi_module_event_handler(module_t, int /*modeventtype_t*/, void *);
167255570Strasz
168255570Straszstatic moduledata_t cfiscsi_moduledata = {
169255570Strasz	"ctlcfiscsi",
170255570Strasz	cfiscsi_module_event_handler,
171255570Strasz	NULL
172255570Strasz};
173255570Strasz
174255570StraszDECLARE_MODULE(ctlcfiscsi, cfiscsi_moduledata, SI_SUB_CONFIGURE, SI_ORDER_FOURTH);
175255570StraszMODULE_VERSION(ctlcfiscsi, 1);
176255570StraszMODULE_DEPEND(ctlcfiscsi, ctl, 1, 1, 1);
177255570StraszMODULE_DEPEND(ctlcfiscsi, icl, 1, 1, 1);
178255570Strasz
179255570Straszstatic struct icl_pdu *
180255570Straszcfiscsi_pdu_new_response(struct icl_pdu *request, int flags)
181255570Strasz{
182255570Strasz
183255570Strasz	return (icl_pdu_new_bhs(request->ip_conn, flags));
184255570Strasz}
185255570Strasz
186255848Straszstatic bool
187255570Straszcfiscsi_pdu_update_cmdsn(const struct icl_pdu *request)
188255570Strasz{
189255570Strasz	const struct iscsi_bhs_scsi_command *bhssc;
190255570Strasz	struct cfiscsi_session *cs;
191255570Strasz	uint32_t cmdsn, expstatsn;
192255570Strasz
193255570Strasz	cs = PDU_SESSION(request);
194255570Strasz
195255570Strasz	/*
196255570Strasz	 * Every incoming PDU - not just NOP-Out - resets the ping timer.
197255570Strasz	 * The purpose of the timeout is to reset the connection when it stalls;
198255570Strasz	 * we don't want this to happen when NOP-In or NOP-Out ends up delayed
199255570Strasz	 * in some queue.
200255570Strasz	 *
201255570Strasz	 * XXX: Locking?
202255570Strasz	 */
203255570Strasz	cs->cs_timeout = 0;
204255570Strasz
205255570Strasz	/*
206255570Strasz	 * Data-Out PDUs don't contain CmdSN.
207255570Strasz	 */
208255570Strasz	if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
209255570Strasz	    ISCSI_BHS_OPCODE_SCSI_DATA_OUT)
210255848Strasz		return (false);
211255570Strasz
212255570Strasz	/*
213255570Strasz	 * We're only using fields common for all the request
214255570Strasz	 * (initiator -> target) PDUs.
215255570Strasz	 */
216255570Strasz	bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
217255570Strasz	cmdsn = ntohl(bhssc->bhssc_cmdsn);
218255570Strasz	expstatsn = ntohl(bhssc->bhssc_expstatsn);
219255570Strasz
220255570Strasz	CFISCSI_SESSION_LOCK(cs);
221255570Strasz#if 0
222255570Strasz	if (expstatsn != cs->cs_statsn) {
223255570Strasz		CFISCSI_SESSION_DEBUG(cs, "received PDU with ExpStatSN %d, "
224255570Strasz		    "while current StatSN is %d", expstatsn,
225255570Strasz		    cs->cs_statsn);
226255570Strasz	}
227255570Strasz#endif
228255570Strasz
229255570Strasz	/*
230255848Strasz	 * The target MUST silently ignore any non-immediate command outside
231255848Strasz	 * of this range.
232255570Strasz	 */
233255848Strasz	if (cmdsn < cs->cs_cmdsn || cmdsn > cs->cs_cmdsn + maxcmdsn_delta) {
234255848Strasz		CFISCSI_SESSION_UNLOCK(cs);
235255570Strasz		CFISCSI_SESSION_WARN(cs, "received PDU with CmdSN %d, "
236255570Strasz		    "while expected CmdSN was %d", cmdsn, cs->cs_cmdsn);
237255848Strasz		return (true);
238255570Strasz	}
239255570Strasz
240255570Strasz	if ((request->ip_bhs->bhs_opcode & ISCSI_BHS_OPCODE_IMMEDIATE) == 0)
241255570Strasz		cs->cs_cmdsn++;
242255570Strasz
243255570Strasz	CFISCSI_SESSION_UNLOCK(cs);
244255848Strasz
245255848Strasz	return (false);
246255570Strasz}
247255570Strasz
248255570Straszstatic void
249255570Straszcfiscsi_pdu_handle(struct icl_pdu *request)
250255570Strasz{
251255570Strasz	struct cfiscsi_session *cs;
252255848Strasz	bool ignore;
253255570Strasz
254255570Strasz	cs = PDU_SESSION(request);
255255570Strasz
256255848Strasz	ignore = cfiscsi_pdu_update_cmdsn(request);
257255848Strasz	if (ignore) {
258255848Strasz		icl_pdu_free(request);
259255848Strasz		return;
260255848Strasz	}
261255570Strasz
262255570Strasz	/*
263255570Strasz	 * Handle the PDU; this includes e.g. receiving the remaining
264255570Strasz	 * part of PDU and submitting the SCSI command to CTL
265255570Strasz	 * or queueing a reply.  The handling routine is responsible
266255570Strasz	 * for freeing the PDU when it's no longer needed.
267255570Strasz	 */
268255570Strasz	switch (request->ip_bhs->bhs_opcode &
269255570Strasz	    ~ISCSI_BHS_OPCODE_IMMEDIATE) {
270255570Strasz	case ISCSI_BHS_OPCODE_NOP_OUT:
271255570Strasz		cfiscsi_pdu_handle_nop_out(request);
272255570Strasz		break;
273255570Strasz	case ISCSI_BHS_OPCODE_SCSI_COMMAND:
274255570Strasz		cfiscsi_pdu_handle_scsi_command(request);
275255570Strasz		break;
276255570Strasz	case ISCSI_BHS_OPCODE_TASK_REQUEST:
277255570Strasz		cfiscsi_pdu_handle_task_request(request);
278255570Strasz		break;
279255570Strasz	case ISCSI_BHS_OPCODE_SCSI_DATA_OUT:
280255570Strasz		cfiscsi_pdu_handle_data_out(request);
281255570Strasz		break;
282255570Strasz	case ISCSI_BHS_OPCODE_LOGOUT_REQUEST:
283255570Strasz		cfiscsi_pdu_handle_logout_request(request);
284255570Strasz		break;
285255570Strasz	default:
286255570Strasz		CFISCSI_SESSION_WARN(cs, "received PDU with unsupported "
287255570Strasz		    "opcode 0x%x; dropping connection",
288255570Strasz		    request->ip_bhs->bhs_opcode);
289256197Strasz		icl_pdu_free(request);
290255570Strasz		cfiscsi_session_terminate(cs);
291255570Strasz	}
292255570Strasz
293255570Strasz}
294255570Strasz
295255570Straszstatic void
296255570Straszcfiscsi_receive_callback(struct icl_pdu *request)
297255570Strasz{
298255570Strasz	struct cfiscsi_session *cs;
299255570Strasz
300255570Strasz	cs = PDU_SESSION(request);
301255570Strasz
302255570Strasz#ifdef ICL_KERNEL_PROXY
303255570Strasz	if (cs->cs_waiting_for_ctld || cs->cs_login_phase) {
304255570Strasz		if (cs->cs_login_pdu == NULL)
305255570Strasz			cs->cs_login_pdu = request;
306255570Strasz		else
307255570Strasz			icl_pdu_free(request);
308255570Strasz		cv_signal(&cs->cs_login_cv);
309255570Strasz		return;
310255570Strasz	}
311255570Strasz#endif
312255570Strasz
313255570Strasz	cfiscsi_pdu_handle(request);
314255570Strasz}
315255570Strasz
316255570Straszstatic void
317255570Straszcfiscsi_error_callback(struct icl_conn *ic)
318255570Strasz{
319255570Strasz	struct cfiscsi_session *cs;
320255570Strasz
321255570Strasz	cs = CONN_SESSION(ic);
322255570Strasz
323255570Strasz	CFISCSI_SESSION_WARN(cs, "connection error; dropping connection");
324255570Strasz	cfiscsi_session_terminate(cs);
325255570Strasz}
326255570Strasz
327255570Straszstatic int
328255570Straszcfiscsi_pdu_prepare(struct icl_pdu *response)
329255570Strasz{
330255570Strasz	struct cfiscsi_session *cs;
331255570Strasz	struct iscsi_bhs_scsi_response *bhssr;
332255570Strasz	bool advance_statsn = true;
333255570Strasz
334255570Strasz	cs = PDU_SESSION(response);
335255570Strasz
336255570Strasz	CFISCSI_SESSION_LOCK_ASSERT(cs);
337255570Strasz
338255570Strasz	/*
339255570Strasz	 * We're only using fields common for all the response
340255570Strasz	 * (target -> initiator) PDUs.
341255570Strasz	 */
342255570Strasz	bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
343255570Strasz
344255570Strasz	/*
345255570Strasz	 * 10.8.3: "The StatSN for this connection is not advanced
346255570Strasz	 * after this PDU is sent."
347255570Strasz	 */
348255570Strasz	if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_R2T)
349255570Strasz		advance_statsn = false;
350255570Strasz
351255570Strasz	/*
352255570Strasz	 * 10.19.2: "However, when the Initiator Task Tag is set to 0xffffffff,
353255570Strasz	 * StatSN for the connection is not advanced after this PDU is sent."
354255570Strasz	 */
355255570Strasz	if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_NOP_IN &&
356255570Strasz	    bhssr->bhssr_initiator_task_tag == 0xffffffff)
357255570Strasz		advance_statsn = false;
358255570Strasz
359255570Strasz	/*
360255570Strasz	 * See the comment below - StatSN is not meaningful and must
361255570Strasz	 * not be advanced.
362255570Strasz	 */
363255570Strasz	if (bhssr->bhssr_opcode == ISCSI_BHS_OPCODE_SCSI_DATA_IN)
364255570Strasz		advance_statsn = false;
365255570Strasz
366255570Strasz	/*
367255570Strasz	 * 10.7.3: "The fields StatSN, Status, and Residual Count
368255570Strasz	 * only have meaningful content if the S bit is set to 1."
369255570Strasz	 */
370255570Strasz	if (bhssr->bhssr_opcode != ISCSI_BHS_OPCODE_SCSI_DATA_IN)
371255570Strasz		bhssr->bhssr_statsn = htonl(cs->cs_statsn);
372255570Strasz	bhssr->bhssr_expcmdsn = htonl(cs->cs_cmdsn);
373255570Strasz	bhssr->bhssr_maxcmdsn = htonl(cs->cs_cmdsn + maxcmdsn_delta);
374255570Strasz
375255570Strasz	if (advance_statsn)
376255570Strasz		cs->cs_statsn++;
377255570Strasz
378255570Strasz	return (0);
379255570Strasz}
380255570Strasz
381255570Straszstatic void
382255570Straszcfiscsi_pdu_queue(struct icl_pdu *response)
383255570Strasz{
384255570Strasz	struct cfiscsi_session *cs;
385255570Strasz
386255570Strasz	cs = PDU_SESSION(response);
387255570Strasz
388255570Strasz	CFISCSI_SESSION_LOCK(cs);
389255570Strasz	cfiscsi_pdu_prepare(response);
390255570Strasz	icl_pdu_queue(response);
391255570Strasz	CFISCSI_SESSION_UNLOCK(cs);
392255570Strasz}
393255570Strasz
394255570Straszstatic uint32_t
395255570Straszcfiscsi_decode_lun(uint64_t encoded)
396255570Strasz{
397255570Strasz	uint8_t lun[8];
398255570Strasz	uint32_t result;
399255570Strasz
400255570Strasz	/*
401255570Strasz	 * The LUN field in iSCSI PDUs may look like an ordinary 64 bit number,
402255570Strasz	 * but is in fact an evil, multidimensional structure defined
403255570Strasz	 * in SCSI Architecture Model 5 (SAM-5), section 4.6.
404255570Strasz	 */
405255570Strasz	memcpy(lun, &encoded, sizeof(lun));
406255570Strasz	switch (lun[0] & 0xC0) {
407255570Strasz	case 0x00:
408255570Strasz		if ((lun[0] & 0x3f) != 0 || lun[2] != 0 || lun[3] != 0 ||
409255570Strasz		    lun[4] != 0 || lun[5] != 0 || lun[6] != 0 || lun[7] != 0) {
410255570Strasz			CFISCSI_WARN("malformed LUN "
411255570Strasz			    "(peripheral device addressing method): 0x%jx",
412255570Strasz			    (uintmax_t)encoded);
413255570Strasz			result = 0xffffffff;
414255570Strasz			break;
415255570Strasz		}
416255570Strasz		result = lun[1];
417255570Strasz		break;
418255570Strasz	case 0x40:
419255570Strasz		if (lun[2] != 0 || lun[3] != 0 || lun[4] != 0 || lun[5] != 0 ||
420255570Strasz		    lun[6] != 0 || lun[7] != 0) {
421255570Strasz			CFISCSI_WARN("malformed LUN "
422255570Strasz			    "(flat address space addressing method): 0x%jx",
423255570Strasz			    (uintmax_t)encoded);
424255570Strasz			result = 0xffffffff;
425255570Strasz			break;
426255570Strasz		}
427255570Strasz		result = ((lun[0] & 0x3f) << 8) + lun[1];
428255570Strasz		break;
429255570Strasz	case 0xC0:
430255570Strasz		if (lun[0] != 0xD2 || lun[4] != 0 || lun[5] != 0 ||
431255570Strasz		    lun[6] != 0 || lun[7] != 0) {
432255570Strasz			CFISCSI_WARN("malformed LUN (extended flat "
433255570Strasz			    "address space addressing method): 0x%jx",
434255570Strasz			    (uintmax_t)encoded);
435255570Strasz			result = 0xffffffff;
436255570Strasz			break;
437255570Strasz		}
438255570Strasz		result = (lun[1] << 16) + (lun[2] << 8) + lun[3];
439255570Strasz	default:
440255570Strasz		CFISCSI_WARN("unsupported LUN format 0x%jx",
441255570Strasz		    (uintmax_t)encoded);
442255570Strasz		result = 0xffffffff;
443255570Strasz		break;
444255570Strasz	}
445255570Strasz
446255570Strasz	return (result);
447255570Strasz}
448255570Strasz
449255570Straszstatic void
450255570Straszcfiscsi_pdu_handle_nop_out(struct icl_pdu *request)
451255570Strasz{
452255570Strasz	struct cfiscsi_session *cs;
453255570Strasz	struct iscsi_bhs_nop_out *bhsno;
454255570Strasz	struct iscsi_bhs_nop_in *bhsni;
455255570Strasz	struct icl_pdu *response;
456256187Strasz	void *data = NULL;
457256187Strasz	size_t datasize;
458256187Strasz	int error;
459255570Strasz
460255570Strasz	cs = PDU_SESSION(request);
461255570Strasz	bhsno = (struct iscsi_bhs_nop_out *)request->ip_bhs;
462255570Strasz
463255570Strasz	if (bhsno->bhsno_initiator_task_tag == 0xffffffff) {
464255570Strasz		/*
465255570Strasz		 * Nothing to do, iscsi_pdu_update_statsn() already
466255570Strasz		 * zeroed the timeout.
467255570Strasz		 */
468255570Strasz		icl_pdu_free(request);
469255570Strasz		return;
470255570Strasz	}
471255570Strasz
472256187Strasz	datasize = icl_pdu_data_segment_length(request);
473256187Strasz	if (datasize > 0) {
474256187Strasz		data = malloc(datasize, M_CFISCSI, M_NOWAIT | M_ZERO);
475256187Strasz		if (data == NULL) {
476256187Strasz			CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
477256187Strasz			    "dropping connection");
478256187Strasz			icl_pdu_free(request);
479256187Strasz			cfiscsi_session_terminate(cs);
480256187Strasz			return;
481256187Strasz		}
482256187Strasz		icl_pdu_get_data(request, 0, data, datasize);
483256187Strasz	}
484256187Strasz
485255570Strasz	response = cfiscsi_pdu_new_response(request, M_NOWAIT);
486255570Strasz	if (response == NULL) {
487256187Strasz		CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
488256187Strasz		    "droppping connection");
489256187Strasz		free(data, M_CFISCSI);
490255570Strasz		icl_pdu_free(request);
491256187Strasz		cfiscsi_session_terminate(cs);
492255570Strasz		return;
493255570Strasz	}
494255570Strasz	bhsni = (struct iscsi_bhs_nop_in *)response->ip_bhs;
495255570Strasz	bhsni->bhsni_opcode = ISCSI_BHS_OPCODE_NOP_IN;
496255570Strasz	bhsni->bhsni_flags = 0x80;
497255570Strasz	bhsni->bhsni_initiator_task_tag = bhsno->bhsno_initiator_task_tag;
498255570Strasz	bhsni->bhsni_target_transfer_tag = 0xffffffff;
499256187Strasz	if (datasize > 0) {
500256187Strasz		error = icl_pdu_append_data(response, data, datasize, M_NOWAIT);
501256187Strasz		if (error != 0) {
502256187Strasz			CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
503256187Strasz			    "dropping connection");
504256187Strasz			free(data, M_CFISCSI);
505256187Strasz			icl_pdu_free(request);
506256187Strasz			icl_pdu_free(response);
507256187Strasz			cfiscsi_session_terminate(cs);
508256187Strasz			return;
509256187Strasz		}
510256187Strasz		free(data, M_CFISCSI);
511256187Strasz	}
512255570Strasz
513255570Strasz	icl_pdu_free(request);
514255570Strasz	cfiscsi_pdu_queue(response);
515255570Strasz}
516255570Strasz
517255570Straszstatic void
518255570Straszcfiscsi_pdu_handle_scsi_command(struct icl_pdu *request)
519255570Strasz{
520255570Strasz	struct iscsi_bhs_scsi_command *bhssc;
521255570Strasz	struct cfiscsi_session *cs;
522255570Strasz	union ctl_io *io;
523255570Strasz	int error;
524255570Strasz
525255570Strasz	cs = PDU_SESSION(request);
526255570Strasz	bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
527255570Strasz	//CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x",
528255570Strasz	//    bhssc->bhssc_initiator_task_tag);
529255570Strasz
530255570Strasz	if (request->ip_data_len > 0 && cs->cs_immediate_data == false) {
531255570Strasz		CFISCSI_SESSION_WARN(cs, "unsolicited data with "
532255570Strasz		    "ImmediateData=No; dropping connection");
533256197Strasz		icl_pdu_free(request);
534255570Strasz		cfiscsi_session_terminate(cs);
535255570Strasz		return;
536255570Strasz	}
537255570Strasz	io = ctl_alloc_io(cs->cs_target->ct_softc->fe.ctl_pool_ref);
538255570Strasz	if (io == NULL) {
539256197Strasz		CFISCSI_SESSION_WARN(cs, "can't allocate ctl_io; "
540256197Strasz		    "dropping connection");
541255570Strasz		icl_pdu_free(request);
542256197Strasz		cfiscsi_session_terminate(cs);
543255570Strasz		return;
544255570Strasz	}
545255570Strasz	ctl_zero_io(io);
546255570Strasz	io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = request;
547255570Strasz	io->io_hdr.io_type = CTL_IO_SCSI;
548255570Strasz	io->io_hdr.nexus.initid.id = cs->cs_ctl_initid;
549255570Strasz	io->io_hdr.nexus.targ_port = cs->cs_target->ct_softc->fe.targ_port;
550255570Strasz	io->io_hdr.nexus.targ_target.id = 0;
551255570Strasz	io->io_hdr.nexus.targ_lun = cfiscsi_decode_lun(bhssc->bhssc_lun);
552255570Strasz	io->io_hdr.nexus.lun_map_fn = cfiscsi_map_lun;
553255570Strasz	io->io_hdr.nexus.lun_map_arg = cs;
554255570Strasz	io->scsiio.tag_num = bhssc->bhssc_initiator_task_tag;
555255570Strasz	switch ((bhssc->bhssc_flags & BHSSC_FLAGS_ATTR)) {
556255570Strasz	case BHSSC_FLAGS_ATTR_UNTAGGED:
557255570Strasz		io->scsiio.tag_type = CTL_TAG_UNTAGGED;
558255570Strasz		break;
559255570Strasz	case BHSSC_FLAGS_ATTR_SIMPLE:
560255570Strasz		io->scsiio.tag_type = CTL_TAG_SIMPLE;
561255570Strasz		break;
562255570Strasz	case BHSSC_FLAGS_ATTR_ORDERED:
563255570Strasz        	io->scsiio.tag_type = CTL_TAG_ORDERED;
564255570Strasz		break;
565255570Strasz	case BHSSC_FLAGS_ATTR_HOQ:
566255570Strasz        	io->scsiio.tag_type = CTL_TAG_HEAD_OF_QUEUE;
567255570Strasz		break;
568255570Strasz	case BHSSC_FLAGS_ATTR_ACA:
569255570Strasz		io->scsiio.tag_type = CTL_TAG_ACA;
570255570Strasz		break;
571255570Strasz	default:
572255570Strasz		io->scsiio.tag_type = CTL_TAG_UNTAGGED;
573255570Strasz		CFISCSI_SESSION_WARN(cs, "unhandled tag type %d",
574255570Strasz		    bhssc->bhssc_flags & BHSSC_FLAGS_ATTR);
575255570Strasz		break;
576255570Strasz	}
577255570Strasz	io->scsiio.cdb_len = sizeof(bhssc->bhssc_cdb); /* Which is 16. */
578255570Strasz	memcpy(io->scsiio.cdb, bhssc->bhssc_cdb, sizeof(bhssc->bhssc_cdb));
579255570Strasz	refcount_acquire(&cs->cs_outstanding_ctl_pdus);
580255570Strasz	error = ctl_queue(io);
581255570Strasz	if (error != CTL_RETVAL_COMPLETE) {
582256197Strasz		CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d; "
583256197Strasz		    "dropping connection", error);
584255570Strasz		ctl_free_io(io);
585255570Strasz		refcount_release(&cs->cs_outstanding_ctl_pdus);
586255570Strasz		icl_pdu_free(request);
587256197Strasz		cfiscsi_session_terminate(cs);
588255570Strasz	}
589255570Strasz}
590255570Strasz
591255570Straszstatic void
592255570Straszcfiscsi_pdu_handle_task_request(struct icl_pdu *request)
593255570Strasz{
594255570Strasz	struct iscsi_bhs_task_management_request *bhstmr;
595255570Strasz	struct iscsi_bhs_task_management_response *bhstmr2;
596255570Strasz	struct icl_pdu *response;
597255570Strasz	struct cfiscsi_session *cs;
598255570Strasz	union ctl_io *io;
599255570Strasz	int error;
600255570Strasz
601255570Strasz	cs = PDU_SESSION(request);
602255570Strasz	bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
603255570Strasz	io = ctl_alloc_io(cs->cs_target->ct_softc->fe.ctl_pool_ref);
604255570Strasz	if (io == NULL) {
605256197Strasz		CFISCSI_SESSION_WARN(cs, "can't allocate ctl_io;"
606256197Strasz		    "dropping connection");
607255570Strasz		icl_pdu_free(request);
608256197Strasz		cfiscsi_session_terminate(cs);
609255570Strasz		return;
610255570Strasz	}
611255570Strasz	ctl_zero_io(io);
612255570Strasz	io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = request;
613255570Strasz	io->io_hdr.io_type = CTL_IO_TASK;
614255570Strasz	io->io_hdr.nexus.initid.id = cs->cs_ctl_initid;
615255570Strasz	io->io_hdr.nexus.targ_port = cs->cs_target->ct_softc->fe.targ_port;
616255570Strasz	io->io_hdr.nexus.targ_target.id = 0;
617255570Strasz	io->io_hdr.nexus.targ_lun = cfiscsi_decode_lun(bhstmr->bhstmr_lun);
618255570Strasz	io->io_hdr.nexus.lun_map_fn = cfiscsi_map_lun;
619255570Strasz	io->io_hdr.nexus.lun_map_arg = cs;
620255570Strasz	io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
621255570Strasz
622255570Strasz	switch (bhstmr->bhstmr_function & ~0x80) {
623255570Strasz	case BHSTMR_FUNCTION_ABORT_TASK:
624255570Strasz#if 0
625255570Strasz		CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_ABORT_TASK");
626255570Strasz#endif
627255570Strasz		io->taskio.task_action = CTL_TASK_ABORT_TASK;
628255570Strasz		io->taskio.tag_num = bhstmr->bhstmr_referenced_task_tag;
629255570Strasz		break;
630255570Strasz	case BHSTMR_FUNCTION_LOGICAL_UNIT_RESET:
631255570Strasz#if 0
632255570Strasz		CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_LOGICAL_UNIT_RESET");
633255570Strasz#endif
634255570Strasz		io->taskio.task_action = CTL_TASK_LUN_RESET;
635255570Strasz		break;
636264602Strasz	case BHSTMR_FUNCTION_TARGET_WARM_RESET:
637255570Strasz#if 0
638264602Strasz		CFISCSI_SESSION_DEBUG(cs, "BHSTMR_FUNCTION_TARGET_WARM_RESET");
639255570Strasz#endif
640264602Strasz		io->taskio.task_action = CTL_TASK_TARGET_RESET;
641255570Strasz		break;
642255570Strasz	default:
643255570Strasz		CFISCSI_SESSION_DEBUG(cs, "unsupported function 0x%x",
644255570Strasz		    bhstmr->bhstmr_function & ~0x80);
645255570Strasz		ctl_free_io(io);
646255570Strasz
647255570Strasz		response = cfiscsi_pdu_new_response(request, M_NOWAIT);
648255570Strasz		if (response == NULL) {
649256197Strasz			CFISCSI_SESSION_WARN(cs, "failed to allocate memory; "
650256197Strasz			    "dropping connection");
651255570Strasz			icl_pdu_free(request);
652256197Strasz			cfiscsi_session_terminate(cs);
653255570Strasz			return;
654255570Strasz		}
655255570Strasz		bhstmr2 = (struct iscsi_bhs_task_management_response *)
656255570Strasz		    response->ip_bhs;
657255570Strasz		bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE;
658255570Strasz		bhstmr2->bhstmr_flags = 0x80;
659255570Strasz		bhstmr2->bhstmr_response =
660255570Strasz		    BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED;
661255570Strasz		bhstmr2->bhstmr_initiator_task_tag =
662255570Strasz		    bhstmr->bhstmr_initiator_task_tag;
663255570Strasz		icl_pdu_free(request);
664255570Strasz		cfiscsi_pdu_queue(response);
665255570Strasz		return;
666255570Strasz	}
667255570Strasz
668255570Strasz	refcount_acquire(&cs->cs_outstanding_ctl_pdus);
669255570Strasz	error = ctl_queue(io);
670255570Strasz	if (error != CTL_RETVAL_COMPLETE) {
671256197Strasz		CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d; "
672256197Strasz		    "dropping connection", error);
673255570Strasz		ctl_free_io(io);
674255570Strasz		refcount_release(&cs->cs_outstanding_ctl_pdus);
675255570Strasz		icl_pdu_free(request);
676256197Strasz		cfiscsi_session_terminate(cs);
677255570Strasz	}
678255570Strasz}
679255570Strasz
680255570Straszstatic bool
681255570Straszcfiscsi_handle_data_segment(struct icl_pdu *request, struct cfiscsi_data_wait *cdw)
682255570Strasz{
683255570Strasz	struct iscsi_bhs_data_out *bhsdo;
684255570Strasz	struct cfiscsi_session *cs;
685255570Strasz	struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
686256195Strasz	size_t copy_len, len, off, buffer_offset;
687255570Strasz	int ctl_sg_count;
688255570Strasz	union ctl_io *io;
689255570Strasz
690255570Strasz	cs = PDU_SESSION(request);
691255570Strasz
692255570Strasz	KASSERT((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
693255570Strasz	    ISCSI_BHS_OPCODE_SCSI_DATA_OUT ||
694255570Strasz	    (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
695255570Strasz	    ISCSI_BHS_OPCODE_SCSI_COMMAND,
696255570Strasz	    ("bad opcode 0x%x", request->ip_bhs->bhs_opcode));
697255570Strasz
698255570Strasz	/*
699255570Strasz	 * We're only using fields common for Data Out and SCSI Command PDUs.
700255570Strasz	 */
701255570Strasz	bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs;
702255570Strasz
703255570Strasz	io = cdw->cdw_ctl_io;
704255570Strasz	KASSERT((io->io_hdr.flags & CTL_FLAG_DATA_MASK) != CTL_FLAG_DATA_IN,
705255570Strasz	    ("CTL_FLAG_DATA_IN"));
706255570Strasz
707255570Strasz#if 0
708255570Strasz	CFISCSI_SESSION_DEBUG(cs, "received %zd bytes out of %d",
709255570Strasz	    request->ip_data_len, io->scsiio.kern_total_len);
710255570Strasz#endif
711255570Strasz
712255570Strasz	if (io->scsiio.kern_sg_entries > 0) {
713255570Strasz		ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
714255570Strasz		ctl_sg_count = io->scsiio.kern_sg_entries;
715255570Strasz	} else {
716255570Strasz		ctl_sglist = &ctl_sg_entry;
717255570Strasz		ctl_sglist->addr = io->scsiio.kern_data_ptr;
718255570Strasz		ctl_sglist->len = io->scsiio.kern_data_len;
719255570Strasz		ctl_sg_count = 1;
720255570Strasz	}
721255570Strasz
722255570Strasz	if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
723255570Strasz	    ISCSI_BHS_OPCODE_SCSI_DATA_OUT)
724255570Strasz		buffer_offset = ntohl(bhsdo->bhsdo_buffer_offset);
725255570Strasz	else
726255570Strasz		buffer_offset = 0;
727255570Strasz
728255570Strasz	/*
729255570Strasz	 * Make sure the offset, as sent by the initiator, matches the offset
730255570Strasz	 * we're supposed to be at in the scatter-gather list.
731255570Strasz	 */
732255570Strasz	if (buffer_offset != io->scsiio.ext_data_filled) {
733255570Strasz		CFISCSI_SESSION_WARN(cs, "received bad buffer offset %zd, "
734255570Strasz		    "expected %zd", buffer_offset,
735255570Strasz		    (size_t)io->scsiio.ext_data_filled);
736255570Strasz		cfiscsi_session_terminate(cs);
737255570Strasz		return (true);
738255570Strasz	}
739255570Strasz
740256195Strasz	/*
741256195Strasz	 * This is the offset within the PDU data segment, as opposed
742256195Strasz	 * to buffer_offset, which is the offset within the task (SCSI
743256195Strasz	 * command).
744256195Strasz	 */
745255570Strasz	off = 0;
746256195Strasz	len = icl_pdu_data_segment_length(request);
747256195Strasz
748256195Strasz	/*
749256195Strasz	 * Iterate over the scatter/gather segments, filling them with data
750256195Strasz	 * from the PDU data segment.  Note that this can get called multiple
751256195Strasz	 * times for one SCSI command; the cdw structure holds state for the
752256195Strasz	 * scatter/gather list.
753256195Strasz	 */
754255570Strasz	for (;;) {
755255570Strasz		KASSERT(cdw->cdw_sg_index < ctl_sg_count,
756255570Strasz		    ("cdw->cdw_sg_index >= ctl_sg_count"));
757255570Strasz		if (cdw->cdw_sg_len == 0) {
758255570Strasz			cdw->cdw_sg_addr = ctl_sglist[cdw->cdw_sg_index].addr;
759255570Strasz			cdw->cdw_sg_len = ctl_sglist[cdw->cdw_sg_index].len;
760255570Strasz		}
761256195Strasz		KASSERT(off <= len, ("len > off"));
762256195Strasz		copy_len = len - off;
763255570Strasz		if (copy_len > cdw->cdw_sg_len)
764255570Strasz			copy_len = cdw->cdw_sg_len;
765255570Strasz
766255570Strasz		icl_pdu_get_data(request, off, cdw->cdw_sg_addr, copy_len);
767255570Strasz		cdw->cdw_sg_addr += copy_len;
768255570Strasz		cdw->cdw_sg_len -= copy_len;
769255570Strasz		off += copy_len;
770255570Strasz		io->scsiio.ext_data_filled += copy_len;
771255570Strasz
772255570Strasz		if (cdw->cdw_sg_len == 0) {
773256195Strasz			/*
774256195Strasz			 * End of current segment.
775256195Strasz			 */
776256195Strasz			if (cdw->cdw_sg_index == ctl_sg_count - 1) {
777256195Strasz				/*
778256195Strasz				 * Last segment in scatter/gather list.
779256195Strasz				 */
780255570Strasz				break;
781256195Strasz			}
782255570Strasz			cdw->cdw_sg_index++;
783255570Strasz		}
784256195Strasz
785256195Strasz		if (off == len) {
786256195Strasz			/*
787256195Strasz			 * End of PDU payload.
788256195Strasz			 */
789255570Strasz			break;
790256195Strasz		}
791255570Strasz	}
792255570Strasz
793256195Strasz	if (len > off) {
794255570Strasz		CFISCSI_SESSION_WARN(cs, "received too much data: got %zd bytes, "
795255570Strasz		    "expected %zd", icl_pdu_data_segment_length(request), off);
796255570Strasz		cfiscsi_session_terminate(cs);
797255570Strasz		return (true);
798255570Strasz	}
799255570Strasz
800255570Strasz	if (bhsdo->bhsdo_flags & BHSDO_FLAGS_F ||
801255570Strasz	    io->scsiio.ext_data_filled == io->scsiio.kern_total_len) {
802255570Strasz		if ((bhsdo->bhsdo_flags & BHSDO_FLAGS_F) == 0) {
803255570Strasz			CFISCSI_SESSION_WARN(cs, "got the final packet without "
804255570Strasz			    "the F flag; flags = 0x%x; dropping connection",
805255570Strasz			    bhsdo->bhsdo_flags);
806255570Strasz			cfiscsi_session_terminate(cs);
807255570Strasz			return (true);
808255570Strasz		}
809255570Strasz
810255570Strasz		if (io->scsiio.ext_data_filled != io->scsiio.kern_total_len) {
811255570Strasz			if ((request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
812255570Strasz			    ISCSI_BHS_OPCODE_SCSI_DATA_OUT) {
813255570Strasz				CFISCSI_SESSION_WARN(cs, "got the final packet, but the "
814255570Strasz				    "transmitted size was %zd bytes instead of %d; "
815255570Strasz				    "dropping connection",
816255570Strasz				    (size_t)io->scsiio.ext_data_filled,
817255570Strasz				    io->scsiio.kern_total_len);
818255570Strasz				cfiscsi_session_terminate(cs);
819255570Strasz				return (true);
820255570Strasz			} else {
821255570Strasz				/*
822255570Strasz				 * For SCSI Command PDU, this just means we need to
823255570Strasz				 * solicit more data by sending R2T.
824255570Strasz				 */
825255570Strasz				return (false);
826255570Strasz			}
827255570Strasz		}
828255570Strasz#if 0
829255570Strasz		CFISCSI_SESSION_DEBUG(cs, "no longer expecting Data-Out with target "
830255570Strasz		    "transfer tag 0x%x", cdw->cdw_target_transfer_tag);
831255570Strasz#endif
832255570Strasz
833255570Strasz		return (true);
834255570Strasz	}
835255570Strasz
836255570Strasz	return (false);
837255570Strasz}
838255570Strasz
839255570Straszstatic void
840255570Straszcfiscsi_pdu_handle_data_out(struct icl_pdu *request)
841255570Strasz{
842255570Strasz	struct iscsi_bhs_data_out *bhsdo;
843255570Strasz	struct cfiscsi_session *cs;
844255570Strasz	struct cfiscsi_data_wait *cdw = NULL;
845255570Strasz	union ctl_io *io;
846255570Strasz	bool done;
847255570Strasz
848255570Strasz	cs = PDU_SESSION(request);
849255570Strasz	bhsdo = (struct iscsi_bhs_data_out *)request->ip_bhs;
850255570Strasz
851255570Strasz	CFISCSI_SESSION_LOCK(cs);
852255570Strasz	TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next) {
853255570Strasz#if 0
854255570Strasz		CFISCSI_SESSION_DEBUG(cs, "have ttt 0x%x, itt 0x%x; looking for "
855255570Strasz		    "ttt 0x%x, itt 0x%x",
856255570Strasz		    bhsdo->bhsdo_target_transfer_tag,
857255570Strasz		    bhsdo->bhsdo_initiator_task_tag,
858255570Strasz		    cdw->cdw_target_transfer_tag, cdw->cdw_initiator_task_tag));
859255570Strasz#endif
860255570Strasz		if (bhsdo->bhsdo_target_transfer_tag ==
861255570Strasz		    cdw->cdw_target_transfer_tag)
862255570Strasz			break;
863255570Strasz	}
864255570Strasz	CFISCSI_SESSION_UNLOCK(cs);
865255570Strasz	if (cdw == NULL) {
866255570Strasz		CFISCSI_SESSION_WARN(cs, "data transfer tag 0x%x, initiator task tag "
867256197Strasz		    "0x%x, not found; dropping connection",
868256197Strasz		    bhsdo->bhsdo_target_transfer_tag, bhsdo->bhsdo_initiator_task_tag);
869255570Strasz		icl_pdu_free(request);
870255570Strasz		cfiscsi_session_terminate(cs);
871255570Strasz		return;
872255570Strasz	}
873255570Strasz
874255570Strasz	io = cdw->cdw_ctl_io;
875255570Strasz	KASSERT((io->io_hdr.flags & CTL_FLAG_DATA_MASK) != CTL_FLAG_DATA_IN,
876255570Strasz	    ("CTL_FLAG_DATA_IN"));
877255570Strasz
878255570Strasz	done = cfiscsi_handle_data_segment(request, cdw);
879255570Strasz	if (done) {
880255570Strasz		CFISCSI_SESSION_LOCK(cs);
881255570Strasz		TAILQ_REMOVE(&cs->cs_waiting_for_data_out, cdw, cdw_next);
882255570Strasz		CFISCSI_SESSION_UNLOCK(cs);
883255570Strasz		uma_zfree(cfiscsi_data_wait_zone, cdw);
884255570Strasz		io->scsiio.be_move_done(io);
885255570Strasz	}
886255570Strasz
887255570Strasz	icl_pdu_free(request);
888255570Strasz}
889255570Strasz
890255570Straszstatic void
891255570Straszcfiscsi_pdu_handle_logout_request(struct icl_pdu *request)
892255570Strasz{
893255570Strasz	struct iscsi_bhs_logout_request *bhslr;
894255570Strasz	struct iscsi_bhs_logout_response *bhslr2;
895255570Strasz	struct icl_pdu *response;
896255570Strasz	struct cfiscsi_session *cs;
897255570Strasz
898255570Strasz	cs = PDU_SESSION(request);
899255570Strasz	bhslr = (struct iscsi_bhs_logout_request *)request->ip_bhs;
900255570Strasz	switch (bhslr->bhslr_reason & 0x7f) {
901255570Strasz	case BHSLR_REASON_CLOSE_SESSION:
902255570Strasz	case BHSLR_REASON_CLOSE_CONNECTION:
903255570Strasz		response = cfiscsi_pdu_new_response(request, M_NOWAIT);
904255570Strasz		if (response == NULL) {
905256197Strasz			CFISCSI_SESSION_DEBUG(cs, "failed to allocate memory");
906255570Strasz			icl_pdu_free(request);
907255570Strasz			cfiscsi_session_terminate(cs);
908255570Strasz			return;
909255570Strasz		}
910255570Strasz		bhslr2 = (struct iscsi_bhs_logout_response *)response->ip_bhs;
911255570Strasz		bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
912255570Strasz		bhslr2->bhslr_flags = 0x80;
913255570Strasz		bhslr2->bhslr_response = BHSLR_RESPONSE_CLOSED_SUCCESSFULLY;
914255570Strasz		bhslr2->bhslr_initiator_task_tag =
915255570Strasz		    bhslr->bhslr_initiator_task_tag;
916255570Strasz		icl_pdu_free(request);
917255570Strasz		cfiscsi_pdu_queue(response);
918255570Strasz		cfiscsi_session_terminate(cs);
919255570Strasz		break;
920255570Strasz	case BHSLR_REASON_REMOVE_FOR_RECOVERY:
921255570Strasz		response = cfiscsi_pdu_new_response(request, M_NOWAIT);
922255570Strasz		if (response == NULL) {
923256197Strasz			CFISCSI_SESSION_WARN(cs,
924256197Strasz			    "failed to allocate memory; dropping connection");
925255570Strasz			icl_pdu_free(request);
926255570Strasz			cfiscsi_session_terminate(cs);
927255570Strasz			return;
928255570Strasz		}
929255570Strasz		bhslr2 = (struct iscsi_bhs_logout_response *)response->ip_bhs;
930255570Strasz		bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
931255570Strasz		bhslr2->bhslr_flags = 0x80;
932255570Strasz		bhslr2->bhslr_response = BHSLR_RESPONSE_RECOVERY_NOT_SUPPORTED;
933255570Strasz		bhslr2->bhslr_initiator_task_tag =
934255570Strasz		    bhslr->bhslr_initiator_task_tag;
935255570Strasz		icl_pdu_free(request);
936255570Strasz		cfiscsi_pdu_queue(response);
937255570Strasz		break;
938255570Strasz	default:
939255570Strasz		CFISCSI_SESSION_WARN(cs, "invalid reason 0%x; dropping connection",
940255570Strasz		    bhslr->bhslr_reason);
941255570Strasz		icl_pdu_free(request);
942255570Strasz		cfiscsi_session_terminate(cs);
943255570Strasz		break;
944255570Strasz	}
945255570Strasz}
946255570Strasz
947255570Straszstatic void
948255570Straszcfiscsi_callout(void *context)
949255570Strasz{
950255570Strasz	struct icl_pdu *cp;
951255570Strasz	struct iscsi_bhs_nop_in *bhsni;
952255570Strasz	struct cfiscsi_session *cs;
953255570Strasz
954255570Strasz	cs = context;
955255570Strasz
956255570Strasz	if (cs->cs_terminating)
957255570Strasz		return;
958255570Strasz
959255570Strasz	callout_schedule(&cs->cs_callout, 1 * hz);
960255570Strasz
961255570Strasz	CFISCSI_SESSION_LOCK(cs);
962255570Strasz	cs->cs_timeout++;
963255570Strasz	CFISCSI_SESSION_UNLOCK(cs);
964255570Strasz
965255570Strasz#ifdef ICL_KERNEL_PROXY
966255570Strasz	if (cs->cs_waiting_for_ctld || cs->cs_login_phase) {
967255570Strasz		if (cs->cs_timeout > login_timeout) {
968255570Strasz			CFISCSI_SESSION_WARN(cs, "login timed out after "
969255570Strasz			    "%d seconds; dropping connection", cs->cs_timeout);
970255570Strasz			cfiscsi_session_terminate(cs);
971255570Strasz		}
972255570Strasz		return;
973255570Strasz	}
974255570Strasz#endif
975255570Strasz
976255570Strasz	if (cs->cs_timeout >= ping_timeout) {
977255570Strasz		CFISCSI_SESSION_WARN(cs, "no ping reply (NOP-Out) after %d seconds; "
978255570Strasz		    "dropping connection",  ping_timeout);
979255570Strasz		cfiscsi_session_terminate(cs);
980255570Strasz		return;
981255570Strasz	}
982255570Strasz
983255570Strasz	/*
984255570Strasz	 * If the ping was reset less than one second ago - which means
985255570Strasz	 * that we've received some PDU during the last second - assume
986255570Strasz	 * the traffic flows correctly and don't bother sending a NOP-Out.
987255570Strasz	 *
988255570Strasz	 * (It's 2 - one for one second, and one for incrementing is_timeout
989255570Strasz	 * earlier in this routine.)
990255570Strasz	 */
991255570Strasz	if (cs->cs_timeout < 2)
992255570Strasz		return;
993255570Strasz
994255824Strasz	cp = icl_pdu_new_bhs(cs->cs_conn, M_NOWAIT);
995255824Strasz	if (cp == NULL) {
996256197Strasz		CFISCSI_SESSION_WARN(cs, "failed to allocate memory");
997255824Strasz		return;
998255824Strasz	}
999255570Strasz	bhsni = (struct iscsi_bhs_nop_in *)cp->ip_bhs;
1000255570Strasz	bhsni->bhsni_opcode = ISCSI_BHS_OPCODE_NOP_IN;
1001255570Strasz	bhsni->bhsni_flags = 0x80;
1002255570Strasz	bhsni->bhsni_initiator_task_tag = 0xffffffff;
1003255570Strasz
1004255570Strasz	cfiscsi_pdu_queue(cp);
1005255570Strasz}
1006255570Strasz
1007255570Straszstatic void
1008255570Straszcfiscsi_session_terminate_tasks(struct cfiscsi_session *cs)
1009255570Strasz{
1010255570Strasz	struct cfiscsi_data_wait *cdw, *tmpcdw;
1011255570Strasz	union ctl_io *io;
1012255570Strasz	int error;
1013255570Strasz
1014255570Strasz#ifdef notyet
1015255570Strasz	io = ctl_alloc_io(cs->cs_target->ct_softc->fe.ctl_pool_ref);
1016255570Strasz	if (io == NULL) {
1017255570Strasz		CFISCSI_SESSION_WARN(cs, "can't allocate ctl_io");
1018255570Strasz		return;
1019255570Strasz	}
1020255570Strasz	ctl_zero_io(io);
1021255570Strasz	io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = NULL;
1022255570Strasz	io->io_hdr.io_type = CTL_IO_TASK;
1023255570Strasz	io->io_hdr.nexus.initid.id = cs->cs_ctl_initid;
1024255570Strasz	io->io_hdr.nexus.targ_port = cs->cs_target->ct_softc->fe.targ_port;
1025255570Strasz	io->io_hdr.nexus.targ_target.id = 0;
1026255570Strasz	io->io_hdr.nexus.targ_lun = lun;
1027255570Strasz	io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
1028255570Strasz	io->taskio.task_action = CTL_TASK_ABORT_TASK_SET;
1029255570Strasz	error = ctl_queue(io);
1030255570Strasz	if (error != CTL_RETVAL_COMPLETE) {
1031255570Strasz		CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d", error);
1032255570Strasz		ctl_free_io(io);
1033255570Strasz	}
1034255570Strasz#else
1035255570Strasz	/*
1036255570Strasz	 * CTL doesn't currently support CTL_TASK_ABORT_TASK_SET, so instead
1037255570Strasz	 * just iterate over tasks that are waiting for something - data - and
1038255570Strasz	 * terminate those.
1039255570Strasz	 */
1040255570Strasz	CFISCSI_SESSION_LOCK(cs);
1041255570Strasz	TAILQ_FOREACH_SAFE(cdw,
1042255570Strasz	    &cs->cs_waiting_for_data_out, cdw_next, tmpcdw) {
1043255570Strasz		io = ctl_alloc_io(cs->cs_target->ct_softc->fe.ctl_pool_ref);
1044255570Strasz		if (io == NULL) {
1045255570Strasz			CFISCSI_SESSION_WARN(cs, "can't allocate ctl_io");
1046255570Strasz			return;
1047255570Strasz		}
1048255570Strasz		ctl_zero_io(io);
1049255570Strasz		io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = NULL;
1050255570Strasz		io->io_hdr.io_type = CTL_IO_TASK;
1051255570Strasz		io->io_hdr.nexus.initid.id = cs->cs_ctl_initid;
1052255570Strasz		io->io_hdr.nexus.targ_port =
1053255570Strasz		    cs->cs_target->ct_softc->fe.targ_port;
1054255570Strasz		io->io_hdr.nexus.targ_target.id = 0;
1055255570Strasz		//io->io_hdr.nexus.targ_lun = lun; /* Not needed? */
1056255570Strasz		io->taskio.tag_type = CTL_TAG_SIMPLE; /* XXX */
1057255570Strasz		io->taskio.task_action = CTL_TASK_ABORT_TASK;
1058255570Strasz		io->taskio.tag_num = cdw->cdw_initiator_task_tag;
1059255570Strasz		error = ctl_queue(io);
1060255570Strasz		if (error != CTL_RETVAL_COMPLETE) {
1061255570Strasz			CFISCSI_SESSION_WARN(cs, "ctl_queue() failed; error %d", error);
1062255570Strasz			ctl_free_io(io);
1063255570Strasz			return;
1064255570Strasz		}
1065255570Strasz#if 0
1066255570Strasz		CFISCSI_SESSION_DEBUG(cs, "removing csw for initiator task tag "
1067255570Strasz		    "0x%x", cdw->cdw_initiator_task_tag);
1068255570Strasz#endif
1069255570Strasz		cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io);
1070255570Strasz		TAILQ_REMOVE(&cs->cs_waiting_for_data_out, cdw, cdw_next);
1071255570Strasz		uma_zfree(cfiscsi_data_wait_zone, cdw);
1072255570Strasz	}
1073255570Strasz	CFISCSI_SESSION_UNLOCK(cs);
1074255570Strasz#endif
1075255570Strasz}
1076255570Strasz
1077255570Straszstatic void
1078255570Straszcfiscsi_maintenance_thread(void *arg)
1079255570Strasz{
1080255570Strasz	struct cfiscsi_session *cs;
1081255570Strasz
1082255570Strasz	cs = arg;
1083255570Strasz
1084255570Strasz	for (;;) {
1085255570Strasz		CFISCSI_SESSION_LOCK(cs);
1086255570Strasz		if (cs->cs_terminating == false)
1087255570Strasz			cv_wait(&cs->cs_maintenance_cv, &cs->cs_lock);
1088255570Strasz		CFISCSI_SESSION_UNLOCK(cs);
1089255570Strasz
1090255570Strasz		if (cs->cs_terminating) {
1091255570Strasz			cfiscsi_session_terminate_tasks(cs);
1092255570Strasz			callout_drain(&cs->cs_callout);
1093255570Strasz
1094255570Strasz			icl_conn_shutdown(cs->cs_conn);
1095255570Strasz			icl_conn_close(cs->cs_conn);
1096255570Strasz
1097255570Strasz			cs->cs_terminating++;
1098255570Strasz
1099255570Strasz			/*
1100255570Strasz			 * XXX: We used to wait up to 30 seconds to deliver queued PDUs
1101255570Strasz			 * 	to the initiator.  We also tried hard to deliver SCSI Responses
1102255570Strasz			 * 	for the aborted PDUs.  We don't do that anymore.  We might need
1103255570Strasz			 * 	to revisit that.
1104255570Strasz			 */
1105255570Strasz
1106255570Strasz			cfiscsi_session_delete(cs);
1107255570Strasz			kthread_exit();
1108255570Strasz			return;
1109255570Strasz		}
1110255570Strasz		CFISCSI_SESSION_DEBUG(cs, "nothing to do");
1111255570Strasz	}
1112255570Strasz}
1113255570Strasz
1114255570Straszstatic void
1115255570Straszcfiscsi_session_terminate(struct cfiscsi_session *cs)
1116255570Strasz{
1117255570Strasz
1118255570Strasz	if (cs->cs_terminating != 0)
1119255570Strasz		return;
1120255570Strasz	cs->cs_terminating = 1;
1121255570Strasz	cv_signal(&cs->cs_maintenance_cv);
1122255570Strasz}
1123255570Strasz
1124255570Straszstatic int
1125255570Straszcfiscsi_session_register_initiator(struct cfiscsi_session *cs)
1126255570Strasz{
1127255570Strasz	int error, i;
1128255570Strasz	struct cfiscsi_softc *softc;
1129255570Strasz
1130255570Strasz	KASSERT(cs->cs_ctl_initid == -1, ("already registered"));
1131255570Strasz
1132255570Strasz	softc = &cfiscsi_softc;
1133255570Strasz
1134255570Strasz	mtx_lock(&softc->lock);
1135255570Strasz	for (i = 0; i < softc->max_initiators; i++) {
1136255570Strasz		if (softc->ctl_initids[i] == 0)
1137255570Strasz			break;
1138255570Strasz	}
1139255570Strasz	if (i == softc->max_initiators) {
1140255570Strasz		CFISCSI_SESSION_WARN(cs, "too many concurrent sessions (%d)",
1141255570Strasz		    softc->max_initiators);
1142255570Strasz		mtx_unlock(&softc->lock);
1143255570Strasz		return (1);
1144255570Strasz	}
1145255570Strasz	softc->ctl_initids[i] = 1;
1146255570Strasz	mtx_unlock(&softc->lock);
1147255570Strasz
1148255570Strasz#if 0
1149255570Strasz	CFISCSI_SESSION_DEBUG(cs, "adding initiator id %d, max %d",
1150255570Strasz	    i, softc->max_initiators);
1151255570Strasz#endif
1152255570Strasz	cs->cs_ctl_initid = i;
1153255570Strasz	error = ctl_add_initiator(0x0, softc->fe.targ_port, cs->cs_ctl_initid);
1154255570Strasz	if (error != 0) {
1155255570Strasz		CFISCSI_SESSION_WARN(cs, "ctl_add_initiator failed with error %d", error);
1156255570Strasz		mtx_lock(&softc->lock);
1157255570Strasz		softc->ctl_initids[cs->cs_ctl_initid] = 0;
1158255570Strasz		mtx_unlock(&softc->lock);
1159255570Strasz		cs->cs_ctl_initid = -1;
1160255570Strasz		return (1);
1161255570Strasz	}
1162255570Strasz
1163255570Strasz	return (0);
1164255570Strasz}
1165255570Strasz
1166255570Straszstatic void
1167255570Straszcfiscsi_session_unregister_initiator(struct cfiscsi_session *cs)
1168255570Strasz{
1169255570Strasz	int error;
1170255570Strasz	struct cfiscsi_softc *softc;
1171255570Strasz
1172255570Strasz	if (cs->cs_ctl_initid == -1)
1173255570Strasz		return;
1174255570Strasz
1175255570Strasz	softc = &cfiscsi_softc;
1176255570Strasz
1177255570Strasz	error = ctl_remove_initiator(softc->fe.targ_port, cs->cs_ctl_initid);
1178255570Strasz	if (error != 0) {
1179255570Strasz		CFISCSI_SESSION_WARN(cs, "ctl_remove_initiator failed with error %d",
1180255570Strasz		    error);
1181255570Strasz	}
1182255570Strasz	mtx_lock(&softc->lock);
1183255570Strasz	softc->ctl_initids[cs->cs_ctl_initid] = 0;
1184255570Strasz	mtx_unlock(&softc->lock);
1185255570Strasz	cs->cs_ctl_initid = -1;
1186255570Strasz}
1187255570Strasz
1188255570Straszstatic struct cfiscsi_session *
1189255570Straszcfiscsi_session_new(struct cfiscsi_softc *softc)
1190255570Strasz{
1191255570Strasz	struct cfiscsi_session *cs;
1192255570Strasz	int error;
1193255570Strasz
1194255570Strasz	cs = malloc(sizeof(*cs), M_CFISCSI, M_NOWAIT | M_ZERO);
1195255570Strasz	if (cs == NULL) {
1196255570Strasz		CFISCSI_WARN("malloc failed");
1197255570Strasz		return (NULL);
1198255570Strasz	}
1199255570Strasz	cs->cs_ctl_initid = -1;
1200255570Strasz
1201255570Strasz	refcount_init(&cs->cs_outstanding_ctl_pdus, 0);
1202255570Strasz	TAILQ_INIT(&cs->cs_waiting_for_data_out);
1203255570Strasz	mtx_init(&cs->cs_lock, "cfiscsi_lock", NULL, MTX_DEF);
1204255570Strasz	cv_init(&cs->cs_maintenance_cv, "cfiscsi_mt");
1205255570Strasz#ifdef ICL_KERNEL_PROXY
1206255570Strasz	cv_init(&cs->cs_login_cv, "cfiscsi_login");
1207255570Strasz#endif
1208255570Strasz
1209255570Strasz	cs->cs_conn = icl_conn_new();
1210255570Strasz	cs->cs_conn->ic_receive = cfiscsi_receive_callback;
1211255570Strasz	cs->cs_conn->ic_error = cfiscsi_error_callback;
1212255570Strasz	cs->cs_conn->ic_prv0 = cs;
1213255570Strasz
1214255570Strasz	error = kthread_add(cfiscsi_maintenance_thread, cs, NULL, NULL, 0, 0, "cfiscsimt");
1215255570Strasz	if (error != 0) {
1216255570Strasz		CFISCSI_SESSION_WARN(cs, "kthread_add(9) failed with error %d", error);
1217255570Strasz		free(cs, M_CFISCSI);
1218255570Strasz		return (NULL);
1219255570Strasz	}
1220255570Strasz
1221255570Strasz	mtx_lock(&softc->lock);
1222255570Strasz	cs->cs_id = softc->last_session_id + 1;
1223255570Strasz	softc->last_session_id++;
1224255570Strasz	mtx_unlock(&softc->lock);
1225255570Strasz
1226255570Strasz	mtx_lock(&softc->lock);
1227255570Strasz	TAILQ_INSERT_TAIL(&softc->sessions, cs, cs_next);
1228255570Strasz	mtx_unlock(&softc->lock);
1229255570Strasz
1230255570Strasz	/*
1231255570Strasz	 * Start pinging the initiator.
1232255570Strasz	 */
1233255570Strasz	callout_init(&cs->cs_callout, 1);
1234255570Strasz	callout_reset(&cs->cs_callout, 1 * hz, cfiscsi_callout, cs);
1235255570Strasz
1236255570Strasz	return (cs);
1237255570Strasz}
1238255570Strasz
1239255570Straszstatic void
1240255570Straszcfiscsi_session_delete(struct cfiscsi_session *cs)
1241255570Strasz{
1242255570Strasz	struct cfiscsi_softc *softc;
1243255570Strasz
1244255570Strasz	softc = &cfiscsi_softc;
1245255570Strasz
1246255570Strasz	KASSERT(cs->cs_outstanding_ctl_pdus == 0,
1247255570Strasz	    ("destroying session with outstanding CTL pdus"));
1248255570Strasz	KASSERT(TAILQ_EMPTY(&cs->cs_waiting_for_data_out),
1249255570Strasz	    ("destroying session with non-empty queue"));
1250255570Strasz
1251255570Strasz	cfiscsi_session_unregister_initiator(cs);
1252255570Strasz	if (cs->cs_target != NULL)
1253255570Strasz		cfiscsi_target_release(cs->cs_target);
1254255570Strasz	icl_conn_close(cs->cs_conn);
1255255570Strasz	icl_conn_free(cs->cs_conn);
1256255570Strasz
1257255570Strasz	mtx_lock(&softc->lock);
1258255570Strasz	TAILQ_REMOVE(&softc->sessions, cs, cs_next);
1259255570Strasz	mtx_unlock(&softc->lock);
1260255570Strasz
1261255570Strasz	free(cs, M_CFISCSI);
1262255570Strasz}
1263255570Strasz
1264255570Straszint
1265255570Straszcfiscsi_init(void)
1266255570Strasz{
1267255570Strasz	struct cfiscsi_softc *softc;
1268255570Strasz	struct ctl_frontend *fe;
1269255570Strasz	int retval;
1270255570Strasz
1271255570Strasz	softc = &cfiscsi_softc;
1272255570Strasz	retval = 0;
1273255570Strasz	bzero(softc, sizeof(*softc));
1274255570Strasz	mtx_init(&softc->lock, "cfiscsi", NULL, MTX_DEF);
1275255570Strasz
1276255570Strasz#ifdef ICL_KERNEL_PROXY
1277255570Strasz	cv_init(&softc->accept_cv, "cfiscsi_accept");
1278255570Strasz#endif
1279255570Strasz	TAILQ_INIT(&softc->sessions);
1280255570Strasz	TAILQ_INIT(&softc->targets);
1281255570Strasz
1282255570Strasz	fe = &softc->fe;
1283255570Strasz	fe->port_type = CTL_PORT_ISCSI;
1284255570Strasz	/* XXX KDM what should the real number be here? */
1285255570Strasz	fe->num_requested_ctl_io = 4096;
1286255570Strasz	snprintf(softc->port_name, sizeof(softc->port_name), "iscsi");
1287255570Strasz	fe->port_name = softc->port_name;
1288255570Strasz	fe->port_online = cfiscsi_online;
1289255570Strasz	fe->port_offline = cfiscsi_offline;
1290255570Strasz	fe->onoff_arg = softc;
1291255570Strasz	fe->targ_enable = cfiscsi_targ_enable;
1292255570Strasz	fe->targ_disable = cfiscsi_targ_disable;
1293255570Strasz	fe->lun_enable = cfiscsi_lun_enable;
1294255570Strasz	fe->lun_disable = cfiscsi_lun_disable;
1295255570Strasz	fe->targ_lun_arg = softc;
1296255570Strasz	fe->ioctl = cfiscsi_ioctl;
1297255570Strasz	fe->devid = cfiscsi_devid;
1298255570Strasz	fe->fe_datamove = cfiscsi_datamove;
1299255570Strasz	fe->fe_done = cfiscsi_done;
1300255570Strasz
1301255570Strasz	/* XXX KDM what should we report here? */
1302255570Strasz	/* XXX These should probably be fetched from CTL. */
1303255570Strasz	fe->max_targets = 1;
1304255570Strasz	fe->max_target_id = 15;
1305255570Strasz
1306255570Strasz	retval = ctl_frontend_register(fe, /*master_SC*/ 1);
1307255570Strasz	if (retval != 0) {
1308255570Strasz		CFISCSI_WARN("ctl_frontend_register() failed with error %d",
1309255570Strasz		    retval);
1310255570Strasz		retval = 1;
1311255570Strasz		goto bailout;
1312255570Strasz	}
1313255570Strasz
1314255570Strasz	softc->max_initiators = fe->max_initiators;
1315255570Strasz
1316255570Strasz	cfiscsi_data_wait_zone = uma_zcreate("cfiscsi_data_wait",
1317255570Strasz	    sizeof(struct cfiscsi_data_wait), NULL, NULL, NULL, NULL,
1318256058Strasz	    UMA_ALIGN_PTR, 0);
1319255570Strasz
1320255570Strasz	return (0);
1321255570Strasz
1322255570Straszbailout:
1323255570Strasz	return (retval);
1324255570Strasz}
1325255570Strasz
1326255570Straszstatic int
1327255570Straszcfiscsi_module_event_handler(module_t mod, int what, void *arg)
1328255570Strasz{
1329255570Strasz
1330255570Strasz	switch (what) {
1331255570Strasz	case MOD_LOAD:
1332255570Strasz		return (cfiscsi_init());
1333255570Strasz	case MOD_UNLOAD:
1334255570Strasz		return (EBUSY);
1335255570Strasz	default:
1336255570Strasz		return (EOPNOTSUPP);
1337255570Strasz	}
1338255570Strasz}
1339255570Strasz
1340255570Strasz#ifdef ICL_KERNEL_PROXY
1341255570Straszstatic void
1342255570Straszcfiscsi_accept(struct socket *so)
1343255570Strasz{
1344255570Strasz	struct cfiscsi_session *cs;
1345255570Strasz
1346255570Strasz	cs = cfiscsi_session_new(&cfiscsi_softc);
1347255570Strasz	if (cs == NULL) {
1348255570Strasz		CFISCSI_WARN("failed to create session");
1349255570Strasz		return;
1350255570Strasz	}
1351255570Strasz
1352255570Strasz	icl_conn_handoff_sock(cs->cs_conn, so);
1353255570Strasz	cs->cs_waiting_for_ctld = true;
1354255570Strasz	cv_signal(&cfiscsi_softc.accept_cv);
1355255570Strasz}
1356255570Strasz#endif
1357255570Strasz
1358255570Straszstatic void
1359255570Straszcfiscsi_online(void *arg)
1360255570Strasz{
1361255570Strasz	struct cfiscsi_softc *softc;
1362255570Strasz
1363255570Strasz	softc = (struct cfiscsi_softc *)arg;
1364255570Strasz
1365255570Strasz	softc->online = 1;
1366255570Strasz#ifdef ICL_KERNEL_PROXY
1367255570Strasz	if (softc->listener != NULL)
1368255570Strasz		icl_listen_free(softc->listener);
1369255570Strasz	softc->listener = icl_listen_new(cfiscsi_accept);
1370255570Strasz#endif
1371255570Strasz}
1372255570Strasz
1373255570Straszstatic void
1374255570Straszcfiscsi_offline(void *arg)
1375255570Strasz{
1376255570Strasz	struct cfiscsi_softc *softc;
1377255570Strasz	struct cfiscsi_session *cs;
1378255570Strasz
1379255570Strasz	softc = (struct cfiscsi_softc *)arg;
1380255570Strasz
1381255570Strasz	softc->online = 0;
1382255570Strasz
1383255570Strasz	mtx_lock(&softc->lock);
1384255570Strasz	TAILQ_FOREACH(cs, &softc->sessions, cs_next)
1385255570Strasz		cfiscsi_session_terminate(cs);
1386255570Strasz	mtx_unlock(&softc->lock);
1387255570Strasz
1388255570Strasz#ifdef ICL_KERNEL_PROXY
1389255570Strasz	icl_listen_free(softc->listener);
1390255570Strasz	softc->listener = NULL;
1391255570Strasz#endif
1392255570Strasz}
1393255570Strasz
1394255570Straszstatic int
1395255570Straszcfiscsi_targ_enable(void *arg, struct ctl_id targ_id)
1396255570Strasz{
1397255570Strasz
1398255570Strasz	return (0);
1399255570Strasz}
1400255570Strasz
1401255570Straszstatic int
1402255570Straszcfiscsi_targ_disable(void *arg, struct ctl_id targ_id)
1403255570Strasz{
1404255570Strasz
1405255570Strasz	return (0);
1406255570Strasz}
1407255570Strasz
1408255570Straszstatic void
1409255570Straszcfiscsi_ioctl_handoff(struct ctl_iscsi *ci)
1410255570Strasz{
1411255570Strasz	struct cfiscsi_softc *softc;
1412255570Strasz	struct cfiscsi_session *cs;
1413255570Strasz	struct cfiscsi_target *ct;
1414255570Strasz	struct ctl_iscsi_handoff_params *cihp;
1415255570Strasz#ifndef ICL_KERNEL_PROXY
1416255570Strasz	int error;
1417255570Strasz#endif
1418255570Strasz
1419255570Strasz	cihp = (struct ctl_iscsi_handoff_params *)&(ci->data);
1420255570Strasz	softc = &cfiscsi_softc;
1421255570Strasz
1422255570Strasz	CFISCSI_DEBUG("new connection from %s (%s) to %s",
1423255570Strasz	    cihp->initiator_name, cihp->initiator_addr,
1424255570Strasz	    cihp->target_name);
1425255570Strasz
1426255570Strasz	if (softc->online == 0) {
1427255570Strasz		ci->status = CTL_ISCSI_ERROR;
1428255570Strasz		snprintf(ci->error_str, sizeof(ci->error_str),
1429255570Strasz		    "%s: port offline", __func__);
1430255570Strasz		return;
1431255570Strasz	}
1432255570Strasz
1433255570Strasz	ct = cfiscsi_target_find(softc, cihp->target_name);
1434255570Strasz	if (ct == NULL) {
1435255570Strasz		ci->status = CTL_ISCSI_ERROR;
1436255570Strasz		snprintf(ci->error_str, sizeof(ci->error_str),
1437255570Strasz		    "%s: target not found", __func__);
1438255570Strasz		return;
1439255570Strasz	}
1440255570Strasz
1441255570Strasz#ifdef ICL_KERNEL_PROXY
1442255570Strasz	mtx_lock(&cfiscsi_softc.lock);
1443255570Strasz	TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1444255570Strasz		if (cs->cs_id == cihp->socket)
1445255570Strasz			break;
1446255570Strasz	}
1447255570Strasz	if (cs == NULL) {
1448255570Strasz		mtx_unlock(&cfiscsi_softc.lock);
1449255570Strasz		snprintf(ci->error_str, sizeof(ci->error_str), "connection not found");
1450255570Strasz		ci->status = CTL_ISCSI_ERROR;
1451264523Smav		cfiscsi_target_release(ct);
1452255570Strasz		return;
1453255570Strasz	}
1454255570Strasz	mtx_unlock(&cfiscsi_softc.lock);
1455255570Strasz#else
1456255570Strasz	cs = cfiscsi_session_new(softc);
1457255570Strasz	if (cs == NULL) {
1458255570Strasz		ci->status = CTL_ISCSI_ERROR;
1459255570Strasz		snprintf(ci->error_str, sizeof(ci->error_str),
1460255570Strasz		    "%s: cfiscsi_session_new failed", __func__);
1461255570Strasz		cfiscsi_target_release(ct);
1462255570Strasz		return;
1463255570Strasz	}
1464255570Strasz#endif
1465255570Strasz	cs->cs_target = ct;
1466255570Strasz
1467255570Strasz	/*
1468255570Strasz	 * First PDU of Full Feature phase has the same CmdSN as the last
1469255570Strasz	 * PDU from the Login Phase received from the initiator.  Thus,
1470255570Strasz	 * the -1 below.
1471255570Strasz	 */
1472255570Strasz	cs->cs_portal_group_tag = cihp->portal_group_tag;
1473255570Strasz	cs->cs_cmdsn = cihp->cmdsn;
1474255570Strasz	cs->cs_statsn = cihp->statsn;
1475255570Strasz	cs->cs_max_data_segment_length = cihp->max_recv_data_segment_length;
1476255570Strasz	cs->cs_max_burst_length = cihp->max_burst_length;
1477255570Strasz	cs->cs_immediate_data = !!cihp->immediate_data;
1478255570Strasz	if (cihp->header_digest == CTL_ISCSI_DIGEST_CRC32C)
1479255570Strasz		cs->cs_conn->ic_header_crc32c = true;
1480255570Strasz	if (cihp->data_digest == CTL_ISCSI_DIGEST_CRC32C)
1481255570Strasz		cs->cs_conn->ic_data_crc32c = true;
1482255570Strasz
1483255570Strasz	strlcpy(cs->cs_initiator_name,
1484255570Strasz	    cihp->initiator_name, sizeof(cs->cs_initiator_name));
1485255570Strasz	strlcpy(cs->cs_initiator_addr,
1486255570Strasz	    cihp->initiator_addr, sizeof(cs->cs_initiator_addr));
1487255570Strasz	strlcpy(cs->cs_initiator_alias,
1488255570Strasz	    cihp->initiator_alias, sizeof(cs->cs_initiator_alias));
1489255570Strasz
1490255570Strasz#ifdef ICL_KERNEL_PROXY
1491255570Strasz	cs->cs_login_phase = false;
1492255570Strasz#else
1493255570Strasz	error = icl_conn_handoff(cs->cs_conn, cihp->socket);
1494255570Strasz	if (error != 0) {
1495255570Strasz		cfiscsi_session_delete(cs);
1496255570Strasz		ci->status = CTL_ISCSI_ERROR;
1497255570Strasz		snprintf(ci->error_str, sizeof(ci->error_str),
1498255570Strasz		    "%s: icl_conn_handoff failed with error %d",
1499255570Strasz		    __func__, error);
1500255570Strasz		return;
1501255570Strasz	}
1502255570Strasz#endif
1503255570Strasz
1504255570Strasz	/*
1505255570Strasz	 * Register initiator with CTL.
1506255570Strasz	 */
1507255570Strasz	cfiscsi_session_register_initiator(cs);
1508255570Strasz
1509255570Strasz#ifdef ICL_KERNEL_PROXY
1510255570Strasz	/*
1511255570Strasz	 * First PDU of the Full Feature phase has likely already arrived.
1512255570Strasz	 * We have to pick it up and execute properly.
1513255570Strasz	 */
1514255570Strasz	if (cs->cs_login_pdu != NULL) {
1515255570Strasz		CFISCSI_SESSION_DEBUG(cs, "picking up first PDU");
1516255570Strasz		cfiscsi_pdu_handle(cs->cs_login_pdu);
1517255570Strasz		cs->cs_login_pdu = NULL;
1518255570Strasz	}
1519255570Strasz#endif
1520255570Strasz
1521255570Strasz	ci->status = CTL_ISCSI_OK;
1522255570Strasz}
1523255570Strasz
1524255570Straszstatic void
1525255570Straszcfiscsi_ioctl_list(struct ctl_iscsi *ci)
1526255570Strasz{
1527255570Strasz	struct ctl_iscsi_list_params *cilp;
1528255570Strasz	struct cfiscsi_session *cs;
1529255570Strasz	struct cfiscsi_softc *softc;
1530255570Strasz	struct sbuf *sb;
1531255570Strasz	int error;
1532255570Strasz
1533255570Strasz	cilp = (struct ctl_iscsi_list_params *)&(ci->data);
1534255570Strasz	softc = &cfiscsi_softc;
1535255570Strasz
1536255570Strasz	sb = sbuf_new(NULL, NULL, cilp->alloc_len, SBUF_FIXEDLEN);
1537255570Strasz	if (sb == NULL) {
1538255570Strasz		ci->status = CTL_ISCSI_ERROR;
1539255570Strasz		snprintf(ci->error_str, sizeof(ci->error_str),
1540255570Strasz		    "Unable to allocate %d bytes for iSCSI session list",
1541255570Strasz		    cilp->alloc_len);
1542255570Strasz		return;
1543255570Strasz	}
1544255570Strasz
1545255570Strasz	sbuf_printf(sb, "<ctlislist>\n");
1546255570Strasz	mtx_lock(&softc->lock);
1547255570Strasz	TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1548255570Strasz#ifdef ICL_KERNEL_PROXY
1549255570Strasz		if (cs->cs_target == NULL)
1550255570Strasz			continue;
1551255570Strasz#endif
1552255570Strasz		error = sbuf_printf(sb, "<connection id=\"%d\">"
1553255570Strasz		    "<initiator>%s</initiator>"
1554255570Strasz		    "<initiator_addr>%s</initiator_addr>"
1555255570Strasz		    "<initiator_alias>%s</initiator_alias>"
1556255570Strasz		    "<target>%s</target>"
1557255570Strasz		    "<target_alias>%s</target_alias>"
1558255570Strasz		    "<header_digest>%s</header_digest>"
1559255570Strasz		    "<data_digest>%s</data_digest>"
1560255570Strasz		    "<max_data_segment_length>%zd</max_data_segment_length>"
1561255570Strasz		    "<immediate_data>%d</immediate_data>"
1562255570Strasz		    "<iser>%d</iser>"
1563255570Strasz		    "</connection>\n",
1564255570Strasz		    cs->cs_id,
1565255570Strasz		    cs->cs_initiator_name, cs->cs_initiator_addr, cs->cs_initiator_alias,
1566255570Strasz		    cs->cs_target->ct_name, cs->cs_target->ct_alias,
1567255570Strasz		    cs->cs_conn->ic_header_crc32c ? "CRC32C" : "None",
1568255570Strasz		    cs->cs_conn->ic_data_crc32c ? "CRC32C" : "None",
1569255570Strasz		    cs->cs_max_data_segment_length,
1570255570Strasz		    cs->cs_immediate_data,
1571255570Strasz		    cs->cs_conn->ic_iser);
1572255570Strasz		if (error != 0)
1573255570Strasz			break;
1574255570Strasz	}
1575255570Strasz	mtx_unlock(&softc->lock);
1576255570Strasz	error = sbuf_printf(sb, "</ctlislist>\n");
1577255570Strasz	if (error != 0) {
1578255570Strasz		sbuf_delete(sb);
1579255570Strasz		ci->status = CTL_ISCSI_LIST_NEED_MORE_SPACE;
1580255570Strasz		snprintf(ci->error_str, sizeof(ci->error_str),
1581255570Strasz		    "Out of space, %d bytes is too small", cilp->alloc_len);
1582255570Strasz		return;
1583255570Strasz	}
1584255570Strasz	sbuf_finish(sb);
1585255570Strasz
1586255570Strasz	error = copyout(sbuf_data(sb), cilp->conn_xml, sbuf_len(sb) + 1);
1587255570Strasz	cilp->fill_len = sbuf_len(sb) + 1;
1588255570Strasz	ci->status = CTL_ISCSI_OK;
1589255570Strasz	sbuf_delete(sb);
1590255570Strasz}
1591255570Strasz
1592255570Straszstatic void
1593255570Straszcfiscsi_ioctl_terminate(struct ctl_iscsi *ci)
1594255570Strasz{
1595255570Strasz	struct icl_pdu *response;
1596255570Strasz	struct iscsi_bhs_asynchronous_message *bhsam;
1597255570Strasz	struct ctl_iscsi_terminate_params *citp;
1598255570Strasz	struct cfiscsi_session *cs;
1599255570Strasz	struct cfiscsi_softc *softc;
1600255570Strasz	int found = 0;
1601255570Strasz
1602255570Strasz	citp = (struct ctl_iscsi_terminate_params *)&(ci->data);
1603255570Strasz	softc = &cfiscsi_softc;
1604255570Strasz
1605255570Strasz	mtx_lock(&softc->lock);
1606255570Strasz	TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1607255570Strasz		if (citp->all == 0 && cs->cs_id != citp->connection_id &&
1608255570Strasz		    strcmp(cs->cs_initiator_name, citp->initiator_name) != 0 &&
1609255570Strasz		    strcmp(cs->cs_initiator_addr, citp->initiator_addr) != 0)
1610255570Strasz			continue;
1611255570Strasz
1612255570Strasz		response = icl_pdu_new_bhs(cs->cs_conn, M_NOWAIT);
1613255570Strasz		if (response == NULL) {
1614255570Strasz			/*
1615255570Strasz			 * Oh well.  Just terminate the connection.
1616255570Strasz			 */
1617255570Strasz		} else {
1618255570Strasz			bhsam = (struct iscsi_bhs_asynchronous_message *)
1619255570Strasz			    response->ip_bhs;
1620255570Strasz			bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE;
1621255570Strasz			bhsam->bhsam_flags = 0x80;
1622255570Strasz			bhsam->bhsam_0xffffffff = 0xffffffff;
1623255570Strasz			bhsam->bhsam_async_event =
1624255570Strasz			    BHSAM_EVENT_TARGET_TERMINATES_SESSION;
1625255570Strasz			cfiscsi_pdu_queue(response);
1626255570Strasz		}
1627255570Strasz		cfiscsi_session_terminate(cs);
1628255570Strasz		found++;
1629255570Strasz	}
1630255570Strasz	mtx_unlock(&softc->lock);
1631255570Strasz
1632255570Strasz	if (found == 0) {
1633255570Strasz		ci->status = CTL_ISCSI_SESSION_NOT_FOUND;
1634255570Strasz		snprintf(ci->error_str, sizeof(ci->error_str),
1635255570Strasz		    "No matching connections found");
1636255570Strasz		return;
1637255570Strasz	}
1638255570Strasz
1639255570Strasz	ci->status = CTL_ISCSI_OK;
1640255570Strasz}
1641255570Strasz
1642255570Straszstatic void
1643255570Straszcfiscsi_ioctl_logout(struct ctl_iscsi *ci)
1644255570Strasz{
1645255570Strasz	struct icl_pdu *response;
1646255570Strasz	struct iscsi_bhs_asynchronous_message *bhsam;
1647255570Strasz	struct ctl_iscsi_logout_params *cilp;
1648255570Strasz	struct cfiscsi_session *cs;
1649255570Strasz	struct cfiscsi_softc *softc;
1650255570Strasz	int found = 0;
1651255570Strasz
1652255570Strasz	cilp = (struct ctl_iscsi_logout_params *)&(ci->data);
1653255570Strasz	softc = &cfiscsi_softc;
1654255570Strasz
1655255570Strasz	mtx_lock(&softc->lock);
1656255570Strasz	TAILQ_FOREACH(cs, &softc->sessions, cs_next) {
1657255570Strasz		if (cilp->all == 0 && cs->cs_id != cilp->connection_id &&
1658255570Strasz		    strcmp(cs->cs_initiator_name, cilp->initiator_name) != 0 &&
1659255570Strasz		    strcmp(cs->cs_initiator_addr, cilp->initiator_addr) != 0)
1660255570Strasz			continue;
1661255570Strasz
1662255570Strasz		response = icl_pdu_new_bhs(cs->cs_conn, M_NOWAIT);
1663255570Strasz		if (response == NULL) {
1664255570Strasz			ci->status = CTL_ISCSI_ERROR;
1665255570Strasz			snprintf(ci->error_str, sizeof(ci->error_str),
1666255570Strasz			    "Unable to allocate memory");
1667255570Strasz			mtx_unlock(&softc->lock);
1668255570Strasz			return;
1669255570Strasz		}
1670255570Strasz		bhsam =
1671255570Strasz		    (struct iscsi_bhs_asynchronous_message *)response->ip_bhs;
1672255570Strasz		bhsam->bhsam_opcode = ISCSI_BHS_OPCODE_ASYNC_MESSAGE;
1673255570Strasz		bhsam->bhsam_flags = 0x80;
1674255570Strasz		bhsam->bhsam_async_event = BHSAM_EVENT_TARGET_REQUESTS_LOGOUT;
1675255570Strasz		bhsam->bhsam_parameter3 = htons(10);
1676255570Strasz		cfiscsi_pdu_queue(response);
1677255570Strasz		found++;
1678255570Strasz	}
1679255570Strasz	mtx_unlock(&softc->lock);
1680255570Strasz
1681255570Strasz	if (found == 0) {
1682255570Strasz		ci->status = CTL_ISCSI_SESSION_NOT_FOUND;
1683255570Strasz		snprintf(ci->error_str, sizeof(ci->error_str),
1684255570Strasz		    "No matching connections found");
1685255570Strasz		return;
1686255570Strasz	}
1687255570Strasz
1688255570Strasz	ci->status = CTL_ISCSI_OK;
1689255570Strasz}
1690255570Strasz
1691255570Strasz#ifdef ICL_KERNEL_PROXY
1692255570Straszstatic void
1693255570Straszcfiscsi_ioctl_listen(struct ctl_iscsi *ci)
1694255570Strasz{
1695255570Strasz	struct ctl_iscsi_listen_params *cilp;
1696255570Strasz	struct sockaddr *sa;
1697255570Strasz	int error;
1698255570Strasz
1699255570Strasz	cilp = (struct ctl_iscsi_listen_params *)&(ci->data);
1700255570Strasz
1701255570Strasz	if (cfiscsi_softc.listener == NULL) {
1702255570Strasz		CFISCSI_DEBUG("no listener");
1703255570Strasz		snprintf(ci->error_str, sizeof(ci->error_str), "no listener");
1704255570Strasz		ci->status = CTL_ISCSI_ERROR;
1705255570Strasz		return;
1706255570Strasz	}
1707255570Strasz
1708255570Strasz	error = getsockaddr(&sa, (void *)cilp->addr, cilp->addrlen);
1709255570Strasz	if (error != 0) {
1710255570Strasz		CFISCSI_DEBUG("getsockaddr, error %d", error);
1711255570Strasz		snprintf(ci->error_str, sizeof(ci->error_str), "getsockaddr failed");
1712255570Strasz		ci->status = CTL_ISCSI_ERROR;
1713255570Strasz		return;
1714255570Strasz	}
1715255570Strasz
1716255570Strasz	error = icl_listen_add(cfiscsi_softc.listener, cilp->iser, cilp->domain,
1717255570Strasz	    cilp->socktype, cilp->protocol, sa);
1718255570Strasz	if (error != 0) {
1719255570Strasz		free(sa, M_SONAME);
1720255570Strasz		CFISCSI_DEBUG("icl_listen_add, error %d", error);
1721255570Strasz		snprintf(ci->error_str, sizeof(ci->error_str),
1722255570Strasz		    "icl_listen_add failed, error %d", error);
1723255570Strasz		ci->status = CTL_ISCSI_ERROR;
1724255570Strasz		return;
1725255570Strasz	}
1726255570Strasz
1727255570Strasz	ci->status = CTL_ISCSI_OK;
1728255570Strasz}
1729255570Strasz
1730255570Straszstatic void
1731255570Straszcfiscsi_ioctl_accept(struct ctl_iscsi *ci)
1732255570Strasz{
1733255570Strasz	struct ctl_iscsi_accept_params *ciap;
1734255570Strasz	struct cfiscsi_session *cs;
1735255570Strasz	int error;
1736255570Strasz
1737255570Strasz	ciap = (struct ctl_iscsi_accept_params *)&(ci->data);
1738255570Strasz
1739255570Strasz	mtx_lock(&cfiscsi_softc.lock);
1740255570Strasz	for (;;) {
1741255570Strasz		TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1742255570Strasz			if (cs->cs_waiting_for_ctld)
1743255570Strasz				break;
1744255570Strasz		}
1745255570Strasz		if (cs != NULL)
1746255570Strasz			break;
1747255570Strasz		error = cv_wait_sig(&cfiscsi_softc.accept_cv, &cfiscsi_softc.lock);
1748255570Strasz		if (error != 0) {
1749255570Strasz			mtx_unlock(&cfiscsi_softc.lock);
1750255570Strasz			snprintf(ci->error_str, sizeof(ci->error_str), "interrupted");
1751255570Strasz			ci->status = CTL_ISCSI_ERROR;
1752255570Strasz			return;
1753255570Strasz		}
1754255570Strasz	}
1755255570Strasz	mtx_unlock(&cfiscsi_softc.lock);
1756255570Strasz
1757255570Strasz	cs->cs_waiting_for_ctld = false;
1758255570Strasz	cs->cs_login_phase = true;
1759255570Strasz
1760255570Strasz	ciap->connection_id = cs->cs_id;
1761255570Strasz	ci->status = CTL_ISCSI_OK;
1762255570Strasz}
1763255570Strasz
1764255570Straszstatic void
1765255570Straszcfiscsi_ioctl_send(struct ctl_iscsi *ci)
1766255570Strasz{
1767255570Strasz	struct ctl_iscsi_send_params *cisp;
1768255570Strasz	struct cfiscsi_session *cs;
1769255570Strasz	struct icl_pdu *ip;
1770255570Strasz	size_t datalen;
1771255570Strasz	void *data;
1772255570Strasz	int error;
1773255570Strasz
1774255570Strasz	cisp = (struct ctl_iscsi_send_params *)&(ci->data);
1775255570Strasz
1776255570Strasz	mtx_lock(&cfiscsi_softc.lock);
1777255570Strasz	TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1778255570Strasz		if (cs->cs_id == cisp->connection_id)
1779255570Strasz			break;
1780255570Strasz	}
1781255570Strasz	if (cs == NULL) {
1782255570Strasz		mtx_unlock(&cfiscsi_softc.lock);
1783255570Strasz		snprintf(ci->error_str, sizeof(ci->error_str), "connection not found");
1784255570Strasz		ci->status = CTL_ISCSI_ERROR;
1785255570Strasz		return;
1786255570Strasz	}
1787255570Strasz	mtx_unlock(&cfiscsi_softc.lock);
1788255570Strasz
1789255570Strasz#if 0
1790255570Strasz	if (cs->cs_login_phase == false)
1791255570Strasz		return (EBUSY);
1792255570Strasz#endif
1793255570Strasz
1794255570Strasz	if (cs->cs_terminating) {
1795255570Strasz		snprintf(ci->error_str, sizeof(ci->error_str), "connection is terminating");
1796255570Strasz		ci->status = CTL_ISCSI_ERROR;
1797255570Strasz		return;
1798255570Strasz	}
1799255570Strasz
1800255570Strasz	datalen = cisp->data_segment_len;
1801255570Strasz	/*
1802255570Strasz	 * XXX
1803255570Strasz	 */
1804255570Strasz	//if (datalen > CFISCSI_MAX_DATA_SEGMENT_LENGTH) {
1805255570Strasz	if (datalen > 65535) {
1806255570Strasz		snprintf(ci->error_str, sizeof(ci->error_str), "data segment too big");
1807255570Strasz		ci->status = CTL_ISCSI_ERROR;
1808255570Strasz		return;
1809255570Strasz	}
1810255570Strasz	if (datalen > 0) {
1811255570Strasz		data = malloc(datalen, M_CFISCSI, M_WAITOK);
1812255570Strasz		error = copyin(cisp->data_segment, data, datalen);
1813255570Strasz		if (error != 0) {
1814255570Strasz			free(data, M_CFISCSI);
1815255570Strasz			snprintf(ci->error_str, sizeof(ci->error_str), "copyin error %d", error);
1816255570Strasz			ci->status = CTL_ISCSI_ERROR;
1817255570Strasz			return;
1818255570Strasz		}
1819255570Strasz	}
1820255570Strasz
1821255570Strasz	ip = icl_pdu_new_bhs(cs->cs_conn, M_WAITOK);
1822255570Strasz	memcpy(ip->ip_bhs, cisp->bhs, sizeof(*ip->ip_bhs));
1823255570Strasz	if (datalen > 0) {
1824255570Strasz		icl_pdu_append_data(ip, data, datalen, M_WAITOK);
1825255570Strasz		free(data, M_CFISCSI);
1826255570Strasz	}
1827255570Strasz	icl_pdu_queue(ip);
1828255570Strasz	ci->status = CTL_ISCSI_OK;
1829255570Strasz}
1830255570Strasz
1831255570Straszstatic void
1832255570Straszcfiscsi_ioctl_receive(struct ctl_iscsi *ci)
1833255570Strasz{
1834255570Strasz	struct ctl_iscsi_receive_params *cirp;
1835255570Strasz	struct cfiscsi_session *cs;
1836255570Strasz	struct icl_pdu *ip;
1837255570Strasz	void *data;
1838255570Strasz
1839255570Strasz	cirp = (struct ctl_iscsi_receive_params *)&(ci->data);
1840255570Strasz
1841255570Strasz	mtx_lock(&cfiscsi_softc.lock);
1842255570Strasz	TAILQ_FOREACH(cs, &cfiscsi_softc.sessions, cs_next) {
1843255570Strasz		if (cs->cs_id == cirp->connection_id)
1844255570Strasz			break;
1845255570Strasz	}
1846255570Strasz	if (cs == NULL) {
1847255570Strasz		mtx_unlock(&cfiscsi_softc.lock);
1848255570Strasz		snprintf(ci->error_str, sizeof(ci->error_str), "connection not found");
1849255570Strasz		ci->status = CTL_ISCSI_ERROR;
1850255570Strasz		return;
1851255570Strasz	}
1852255570Strasz	mtx_unlock(&cfiscsi_softc.lock);
1853255570Strasz
1854255570Strasz#if 0
1855255570Strasz	if (is->is_login_phase == false)
1856255570Strasz		return (EBUSY);
1857255570Strasz#endif
1858255570Strasz
1859255570Strasz	CFISCSI_SESSION_LOCK(cs);
1860255570Strasz	while (cs->cs_login_pdu == NULL &&
1861255570Strasz	    cs->cs_terminating == false)
1862255570Strasz		cv_wait(&cs->cs_login_cv, &cs->cs_lock);
1863255570Strasz	if (cs->cs_terminating) {
1864255570Strasz		CFISCSI_SESSION_UNLOCK(cs);
1865255570Strasz		snprintf(ci->error_str, sizeof(ci->error_str), "connection terminating");
1866255570Strasz		ci->status = CTL_ISCSI_ERROR;
1867255570Strasz		return;
1868255570Strasz	}
1869255570Strasz	ip = cs->cs_login_pdu;
1870255570Strasz	cs->cs_login_pdu = NULL;
1871255570Strasz	CFISCSI_SESSION_UNLOCK(cs);
1872255570Strasz
1873255570Strasz	if (ip->ip_data_len > cirp->data_segment_len) {
1874255570Strasz		icl_pdu_free(ip);
1875255570Strasz		snprintf(ci->error_str, sizeof(ci->error_str), "data segment too big");
1876255570Strasz		ci->status = CTL_ISCSI_ERROR;
1877255570Strasz		return;
1878255570Strasz	}
1879255570Strasz
1880255570Strasz	copyout(ip->ip_bhs, cirp->bhs, sizeof(*ip->ip_bhs));
1881255570Strasz	if (ip->ip_data_len > 0) {
1882255570Strasz		data = malloc(ip->ip_data_len, M_CFISCSI, M_WAITOK);
1883255570Strasz		icl_pdu_get_data(ip, 0, data, ip->ip_data_len);
1884255570Strasz		copyout(data, cirp->data_segment, ip->ip_data_len);
1885255570Strasz		free(data, M_CFISCSI);
1886255570Strasz	}
1887255570Strasz
1888255570Strasz	icl_pdu_free(ip);
1889255570Strasz	ci->status = CTL_ISCSI_OK;
1890255570Strasz}
1891255570Strasz
1892255570Straszstatic void
1893255570Straszcfiscsi_ioctl_close(struct ctl_iscsi *ci)
1894255570Strasz{
1895255570Strasz	/*
1896255570Strasz	 * XXX
1897255570Strasz	 */
1898255570Strasz}
1899255570Strasz#endif /* !ICL_KERNEL_PROXY */
1900255570Strasz
1901255570Straszstatic int
1902255570Straszcfiscsi_ioctl(struct cdev *dev,
1903255570Strasz    u_long cmd, caddr_t addr, int flag, struct thread *td)
1904255570Strasz{
1905255570Strasz	struct ctl_iscsi *ci;
1906255570Strasz
1907255570Strasz	if (cmd != CTL_ISCSI)
1908255570Strasz		return (ENOTTY);
1909255570Strasz
1910255570Strasz	ci = (struct ctl_iscsi *)addr;
1911255570Strasz	switch (ci->type) {
1912255570Strasz	case CTL_ISCSI_HANDOFF:
1913255570Strasz		cfiscsi_ioctl_handoff(ci);
1914255570Strasz		break;
1915255570Strasz	case CTL_ISCSI_LIST:
1916255570Strasz		cfiscsi_ioctl_list(ci);
1917255570Strasz		break;
1918255570Strasz	case CTL_ISCSI_TERMINATE:
1919255570Strasz		cfiscsi_ioctl_terminate(ci);
1920255570Strasz		break;
1921255570Strasz	case CTL_ISCSI_LOGOUT:
1922255570Strasz		cfiscsi_ioctl_logout(ci);
1923255570Strasz		break;
1924255570Strasz#ifdef ICL_KERNEL_PROXY
1925255570Strasz	case CTL_ISCSI_LISTEN:
1926255570Strasz		cfiscsi_ioctl_listen(ci);
1927255570Strasz		break;
1928255570Strasz	case CTL_ISCSI_ACCEPT:
1929255570Strasz		cfiscsi_ioctl_accept(ci);
1930255570Strasz		break;
1931255570Strasz	case CTL_ISCSI_SEND:
1932255570Strasz		cfiscsi_ioctl_send(ci);
1933255570Strasz		break;
1934255570Strasz	case CTL_ISCSI_RECEIVE:
1935255570Strasz		cfiscsi_ioctl_receive(ci);
1936255570Strasz		break;
1937255570Strasz	case CTL_ISCSI_CLOSE:
1938255570Strasz		cfiscsi_ioctl_close(ci);
1939255570Strasz		break;
1940255570Strasz#endif /* ICL_KERNEL_PROXY */
1941255570Strasz	default:
1942255570Strasz		ci->status = CTL_ISCSI_ERROR;
1943255570Strasz		snprintf(ci->error_str, sizeof(ci->error_str),
1944255570Strasz		    "%s: invalid iSCSI request type %d", __func__, ci->type);
1945255570Strasz		break;
1946255570Strasz	}
1947255570Strasz
1948255570Strasz	return (0);
1949255570Strasz}
1950255570Strasz
1951255570Straszstatic int
1952255570Straszcfiscsi_devid(struct ctl_scsiio *ctsio, int alloc_len)
1953255570Strasz{
1954255570Strasz	struct cfiscsi_session *cs;
1955255570Strasz	struct scsi_vpd_device_id *devid_ptr;
1956255570Strasz	struct scsi_vpd_id_descriptor *desc, *desc1;
1957255570Strasz	struct scsi_vpd_id_descriptor *desc2, *desc3; /* for types 4h and 5h */
1958255570Strasz	struct scsi_vpd_id_t10 *t10id;
1959255570Strasz	struct ctl_lun *lun;
1960255570Strasz	const struct icl_pdu *request;
1961255570Strasz	size_t devid_len, wwpn_len;
1962255570Strasz
1963255570Strasz	lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
1964255570Strasz	request = ctsio->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1965255570Strasz	cs = PDU_SESSION(request);
1966255570Strasz
1967255570Strasz	wwpn_len = strlen(cs->cs_target->ct_name);
1968255570Strasz	wwpn_len += strlen(",t,0x01");
1969255570Strasz	wwpn_len += 1; /* '\0' */
1970255570Strasz	if ((wwpn_len % 4) != 0)
1971255570Strasz		wwpn_len += (4 - (wwpn_len % 4));
1972255570Strasz
1973255570Strasz	devid_len = sizeof(struct scsi_vpd_device_id) +
1974255570Strasz		sizeof(struct scsi_vpd_id_descriptor) +
1975255570Strasz		sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN +
1976255570Strasz		sizeof(struct scsi_vpd_id_descriptor) + wwpn_len +
1977255570Strasz		sizeof(struct scsi_vpd_id_descriptor) +
1978255570Strasz		sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
1979255570Strasz		sizeof(struct scsi_vpd_id_descriptor) +
1980255570Strasz		sizeof(struct scsi_vpd_id_trgt_port_grp_id);
1981255570Strasz
1982255570Strasz	ctsio->kern_data_ptr = malloc(devid_len, M_CTL, M_WAITOK | M_ZERO);
1983255570Strasz	devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
1984255570Strasz	ctsio->kern_sg_entries = 0;
1985255570Strasz
1986255570Strasz	if (devid_len < alloc_len) {
1987255570Strasz		ctsio->residual = alloc_len - devid_len;
1988255570Strasz		ctsio->kern_data_len = devid_len;
1989255570Strasz		ctsio->kern_total_len = devid_len;
1990255570Strasz	} else {
1991255570Strasz		ctsio->residual = 0;
1992255570Strasz		ctsio->kern_data_len = alloc_len;
1993255570Strasz		ctsio->kern_total_len = alloc_len;
1994255570Strasz	}
1995255570Strasz	ctsio->kern_data_resid = 0;
1996255570Strasz	ctsio->kern_rel_offset = 0;
1997255570Strasz	ctsio->kern_sg_entries = 0;
1998255570Strasz
1999255570Strasz	desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
2000255570Strasz	t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
2001255570Strasz	desc1 = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
2002255570Strasz	    sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN);
2003255570Strasz	desc2 = (struct scsi_vpd_id_descriptor *)(&desc1->identifier[0] +
2004255570Strasz	    wwpn_len);
2005255570Strasz	desc3 = (struct scsi_vpd_id_descriptor *)(&desc2->identifier[0] +
2006255570Strasz	    sizeof(struct scsi_vpd_id_rel_trgt_port_id));
2007255570Strasz
2008255570Strasz	if (lun != NULL)
2009255570Strasz		devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
2010255570Strasz		    lun->be_lun->lun_type;
2011255570Strasz	else
2012255570Strasz		devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
2013255570Strasz
2014255570Strasz	devid_ptr->page_code = SVPD_DEVICE_ID;
2015255570Strasz
2016255570Strasz	scsi_ulto2b(devid_len - 4, devid_ptr->length);
2017255570Strasz
2018255570Strasz	/*
2019255570Strasz	 * We're using a LUN association here.  i.e., this device ID is a
2020255570Strasz	 * per-LUN identifier.
2021255570Strasz	 */
2022255570Strasz	desc->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_ASCII;
2023255570Strasz	desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
2024255570Strasz	desc->length = sizeof(*t10id) + CTL_DEVID_LEN;
2025255570Strasz	strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
2026255570Strasz
2027255570Strasz	/*
2028255570Strasz	 * If we've actually got a backend, copy the device id from the
2029255570Strasz	 * per-LUN data.  Otherwise, set it to all spaces.
2030255570Strasz	 */
2031255570Strasz	if (lun != NULL) {
2032255570Strasz		/*
2033255570Strasz		 * Copy the backend's LUN ID.
2034255570Strasz		 */
2035255570Strasz		strncpy((char *)t10id->vendor_spec_id,
2036255570Strasz		    (char *)lun->be_lun->device_id, CTL_DEVID_LEN);
2037255570Strasz	} else {
2038255570Strasz		/*
2039255570Strasz		 * No backend, set this to spaces.
2040255570Strasz		 */
2041255570Strasz		memset(t10id->vendor_spec_id, 0x20, CTL_DEVID_LEN);
2042255570Strasz	}
2043255570Strasz
2044255570Strasz	/*
2045255570Strasz	 * desc1 is for the WWPN which is a port asscociation.
2046255570Strasz	 */
2047255570Strasz       	desc1->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_UTF8;
2048255570Strasz	desc1->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
2049255570Strasz	    SVPD_ID_TYPE_SCSI_NAME;
2050255570Strasz	desc1->length = wwpn_len;
2051255570Strasz	snprintf(desc1->identifier, wwpn_len, "%s,t,0x%x",
2052255570Strasz	    cs->cs_target->ct_name, cs->cs_portal_group_tag);
2053255570Strasz
2054255570Strasz	/*
2055255570Strasz	 * desc2 is for the Relative Target Port(type 4h) identifier
2056255570Strasz	 */
2057255570Strasz       	desc2->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_BINARY;
2058255570Strasz	desc2->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
2059255570Strasz	    SVPD_ID_TYPE_RELTARG;
2060255570Strasz	desc2->length = 4;
2061255570Strasz	desc2->identifier[3] = 1;
2062255570Strasz
2063255570Strasz	/*
2064255570Strasz	 * desc3 is for the Target Port Group(type 5h) identifier
2065255570Strasz	 */
2066255570Strasz       	desc3->proto_codeset = (SCSI_PROTO_ISCSI << 4) | SVPD_ID_CODESET_BINARY;
2067255570Strasz	desc3->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
2068255570Strasz	    SVPD_ID_TYPE_TPORTGRP;
2069255570Strasz	desc3->length = 4;
2070255570Strasz	desc3->identifier[3] = 1;
2071255570Strasz
2072255570Strasz	ctsio->scsi_status = SCSI_STATUS_OK;
2073255570Strasz
2074255570Strasz	ctsio->be_move_done = ctl_config_move_done;
2075255570Strasz	ctl_datamove((union ctl_io *)ctsio);
2076255570Strasz
2077255570Strasz	return (CTL_RETVAL_COMPLETE);
2078255570Strasz}
2079255570Strasz
2080255570Straszstatic void
2081255570Straszcfiscsi_target_hold(struct cfiscsi_target *ct)
2082255570Strasz{
2083255570Strasz
2084255570Strasz	refcount_acquire(&ct->ct_refcount);
2085255570Strasz}
2086255570Strasz
2087255570Straszstatic void
2088255570Straszcfiscsi_target_release(struct cfiscsi_target *ct)
2089255570Strasz{
2090255570Strasz	struct cfiscsi_softc *softc;
2091255570Strasz
2092255570Strasz	softc = ct->ct_softc;
2093255570Strasz	mtx_lock(&softc->lock);
2094255570Strasz	if (refcount_release(&ct->ct_refcount)) {
2095255570Strasz		TAILQ_REMOVE(&softc->targets, ct, ct_next);
2096255570Strasz		mtx_unlock(&softc->lock);
2097255570Strasz		free(ct, M_CFISCSI);
2098255570Strasz
2099255570Strasz		return;
2100255570Strasz	}
2101255570Strasz	mtx_unlock(&softc->lock);
2102255570Strasz}
2103255570Strasz
2104255570Straszstatic struct cfiscsi_target *
2105255570Straszcfiscsi_target_find(struct cfiscsi_softc *softc, const char *name)
2106255570Strasz{
2107255570Strasz	struct cfiscsi_target *ct;
2108255570Strasz
2109255570Strasz	mtx_lock(&softc->lock);
2110255570Strasz	TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2111255570Strasz		if (strcmp(name, ct->ct_name) != 0)
2112255570Strasz			continue;
2113255570Strasz		cfiscsi_target_hold(ct);
2114255570Strasz		mtx_unlock(&softc->lock);
2115255570Strasz		return (ct);
2116255570Strasz	}
2117255570Strasz	mtx_unlock(&softc->lock);
2118255570Strasz
2119255570Strasz	return (NULL);
2120255570Strasz}
2121255570Strasz
2122255570Straszstatic struct cfiscsi_target *
2123255570Straszcfiscsi_target_find_or_create(struct cfiscsi_softc *softc, const char *name,
2124255570Strasz    const char *alias)
2125255570Strasz{
2126255570Strasz	struct cfiscsi_target *ct, *newct;
2127255570Strasz	int i;
2128255570Strasz
2129255570Strasz	if (name[0] == '\0' || strlen(name) >= CTL_ISCSI_NAME_LEN)
2130255570Strasz		return (NULL);
2131255570Strasz
2132255570Strasz	newct = malloc(sizeof(*newct), M_CFISCSI, M_WAITOK | M_ZERO);
2133255570Strasz
2134255570Strasz	mtx_lock(&softc->lock);
2135255570Strasz	TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2136255570Strasz		if (strcmp(name, ct->ct_name) != 0)
2137255570Strasz			continue;
2138255570Strasz		cfiscsi_target_hold(ct);
2139255570Strasz		mtx_unlock(&softc->lock);
2140255570Strasz		free(newct, M_CFISCSI);
2141255570Strasz		return (ct);
2142255570Strasz	}
2143255570Strasz
2144255570Strasz	for (i = 0; i < CTL_MAX_LUNS; i++)
2145255570Strasz		newct->ct_luns[i] = -1;
2146255570Strasz
2147255570Strasz	strlcpy(newct->ct_name, name, sizeof(newct->ct_name));
2148255570Strasz	if (alias != NULL)
2149255570Strasz		strlcpy(newct->ct_alias, alias, sizeof(newct->ct_alias));
2150255570Strasz	refcount_init(&newct->ct_refcount, 1);
2151255570Strasz	newct->ct_softc = softc;
2152255570Strasz	TAILQ_INSERT_TAIL(&softc->targets, newct, ct_next);
2153255570Strasz	mtx_unlock(&softc->lock);
2154255570Strasz
2155255570Strasz	return (newct);
2156255570Strasz}
2157255570Strasz
2158255570Strasz/*
2159255570Strasz * Takes LUN from the target space and returns LUN from the CTL space.
2160255570Strasz */
2161255570Straszstatic uint32_t
2162255570Straszcfiscsi_map_lun(void *arg, uint32_t lun)
2163255570Strasz{
2164255570Strasz	struct cfiscsi_session *cs;
2165255570Strasz
2166255570Strasz	cs = arg;
2167255570Strasz
2168255570Strasz	if (lun >= CTL_MAX_LUNS) {
2169255570Strasz		CFISCSI_DEBUG("requested lun number %d is higher "
2170255570Strasz		    "than maximum %d", lun, CTL_MAX_LUNS - 1);
2171255570Strasz		return (0xffffffff);
2172255570Strasz	}
2173255570Strasz
2174255570Strasz	if (cs->cs_target->ct_luns[lun] < 0)
2175255570Strasz		return (0xffffffff);
2176255570Strasz
2177255570Strasz	return (cs->cs_target->ct_luns[lun]);
2178255570Strasz}
2179255570Strasz
2180255570Straszstatic int
2181255570Straszcfiscsi_target_set_lun(struct cfiscsi_target *ct,
2182255570Strasz    unsigned long lun_id, unsigned long ctl_lun_id)
2183255570Strasz{
2184255570Strasz
2185255570Strasz	if (lun_id >= CTL_MAX_LUNS) {
2186255570Strasz		CFISCSI_WARN("requested lun number %ld is higher "
2187255570Strasz		    "than maximum %d", lun_id, CTL_MAX_LUNS - 1);
2188255570Strasz		return (-1);
2189255570Strasz	}
2190255570Strasz
2191255570Strasz	if (ct->ct_luns[lun_id] >= 0) {
2192255570Strasz		/*
2193255570Strasz		 * CTL calls cfiscsi_lun_enable() twice for each LUN - once
2194255570Strasz		 * when the LUN is created, and a second time just before
2195255570Strasz		 * the port is brought online; don't emit warnings
2196255570Strasz		 * for that case.
2197255570Strasz		 */
2198255570Strasz		if (ct->ct_luns[lun_id] == ctl_lun_id)
2199255570Strasz			return (0);
2200255570Strasz		CFISCSI_WARN("lun %ld already allocated", lun_id);
2201255570Strasz		return (-1);
2202255570Strasz	}
2203255570Strasz
2204255570Strasz#if 0
2205255570Strasz	CFISCSI_DEBUG("adding mapping for lun %ld, target %s "
2206255570Strasz	    "to ctl lun %ld", lun_id, ct->ct_name, ctl_lun_id);
2207255570Strasz#endif
2208255570Strasz
2209255570Strasz	ct->ct_luns[lun_id] = ctl_lun_id;
2210255570Strasz	cfiscsi_target_hold(ct);
2211255570Strasz
2212255570Strasz	return (0);
2213255570Strasz}
2214255570Strasz
2215255570Straszstatic int
2216255570Straszcfiscsi_target_unset_lun(struct cfiscsi_target *ct, unsigned long lun_id)
2217255570Strasz{
2218255570Strasz
2219255570Strasz	if (ct->ct_luns[lun_id] < 0) {
2220255570Strasz		CFISCSI_WARN("lun %ld not allocated", lun_id);
2221255570Strasz		return (-1);
2222255570Strasz	}
2223255570Strasz
2224255570Strasz	ct->ct_luns[lun_id] = -1;
2225255570Strasz	cfiscsi_target_release(ct);
2226255570Strasz
2227255570Strasz	return (0);
2228255570Strasz}
2229255570Strasz
2230255570Straszstatic int
2231255570Straszcfiscsi_lun_enable(void *arg, struct ctl_id target_id, int lun_id)
2232255570Strasz{
2233255570Strasz	struct cfiscsi_softc *softc;
2234255570Strasz	struct cfiscsi_target *ct;
2235255570Strasz	struct ctl_be_lun_option *opt;
2236255570Strasz	const char *target = NULL, *target_alias = NULL;
2237255570Strasz	const char *lun = NULL;
2238255570Strasz	unsigned long tmp;
2239255570Strasz
2240255570Strasz	softc = (struct cfiscsi_softc *)arg;
2241255570Strasz
2242255570Strasz	STAILQ_FOREACH(opt,
2243255570Strasz	    &control_softc->ctl_luns[lun_id]->be_lun->options, links) {
2244255570Strasz		if (strcmp(opt->name, "cfiscsi_target") == 0)
2245255570Strasz			target = opt->value;
2246255570Strasz		else if (strcmp(opt->name, "cfiscsi_target_alias") == 0)
2247255570Strasz			target_alias = opt->value;
2248255570Strasz		else if (strcmp(opt->name, "cfiscsi_lun") == 0)
2249255570Strasz			lun = opt->value;
2250255570Strasz	}
2251255570Strasz
2252255570Strasz	if (target == NULL && lun == NULL)
2253255570Strasz		return (0);
2254255570Strasz
2255255570Strasz	if (target == NULL || lun == NULL) {
2256255570Strasz		CFISCSI_WARN("lun added with cfiscsi_target, but without "
2257255570Strasz		    "cfiscsi_lun, or the other way around; ignoring");
2258255570Strasz		return (0);
2259255570Strasz	}
2260255570Strasz
2261255570Strasz	ct = cfiscsi_target_find_or_create(softc, target, target_alias);
2262255570Strasz	if (ct == NULL) {
2263255570Strasz		CFISCSI_WARN("failed to create target \"%s\"", target);
2264255570Strasz		return (0);
2265255570Strasz	}
2266255570Strasz
2267255570Strasz	tmp = strtoul(lun, NULL, 10);
2268255570Strasz	cfiscsi_target_set_lun(ct, tmp, lun_id);
2269264523Smav	cfiscsi_target_release(ct);
2270255570Strasz	return (0);
2271255570Strasz}
2272255570Strasz
2273255570Straszstatic int
2274255570Straszcfiscsi_lun_disable(void *arg, struct ctl_id target_id, int lun_id)
2275255570Strasz{
2276255570Strasz	struct cfiscsi_softc *softc;
2277255570Strasz	struct cfiscsi_target *ct;
2278255570Strasz	int i;
2279255570Strasz
2280255570Strasz	softc = (struct cfiscsi_softc *)arg;
2281255570Strasz
2282255570Strasz	mtx_lock(&softc->lock);
2283255570Strasz	TAILQ_FOREACH(ct, &softc->targets, ct_next) {
2284255570Strasz		for (i = 0; i < CTL_MAX_LUNS; i++) {
2285255570Strasz			if (ct->ct_luns[i] < 0)
2286255570Strasz				continue;
2287255570Strasz			if (ct->ct_luns[i] != lun_id)
2288255570Strasz				continue;
2289264523Smav			mtx_unlock(&softc->lock);
2290255570Strasz			cfiscsi_target_unset_lun(ct, i);
2291264523Smav			return (0);
2292255570Strasz		}
2293255570Strasz	}
2294255570Strasz	mtx_unlock(&softc->lock);
2295255570Strasz	return (0);
2296255570Strasz}
2297255570Strasz
2298255570Straszstatic void
2299256065Straszcfiscsi_datamove_in(union ctl_io *io)
2300255570Strasz{
2301255570Strasz	struct cfiscsi_session *cs;
2302255570Strasz	struct icl_pdu *request, *response;
2303255570Strasz	const struct iscsi_bhs_scsi_command *bhssc;
2304255570Strasz	struct iscsi_bhs_data_in *bhsdi;
2305255570Strasz	struct ctl_sg_entry ctl_sg_entry, *ctl_sglist;
2306265490Strasz	size_t len, expected_len, sg_len, buffer_offset;
2307265490Strasz	const char *sg_addr;
2308255824Strasz	int ctl_sg_count, error, i;
2309255570Strasz
2310255570Strasz	request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2311255570Strasz	cs = PDU_SESSION(request);
2312255570Strasz
2313255570Strasz	bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
2314255570Strasz	KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2315255570Strasz	    ISCSI_BHS_OPCODE_SCSI_COMMAND,
2316255570Strasz	    ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND"));
2317255570Strasz
2318255570Strasz	if (io->scsiio.kern_sg_entries > 0) {
2319255570Strasz		ctl_sglist = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
2320255570Strasz		ctl_sg_count = io->scsiio.kern_sg_entries;
2321255570Strasz	} else {
2322255570Strasz		ctl_sglist = &ctl_sg_entry;
2323255570Strasz		ctl_sglist->addr = io->scsiio.kern_data_ptr;
2324255570Strasz		ctl_sglist->len = io->scsiio.kern_data_len;
2325255570Strasz		ctl_sg_count = 1;
2326255570Strasz	}
2327255570Strasz
2328255570Strasz	/*
2329265490Strasz	 * This is the total amount of data to be transferred within the current
2330265490Strasz	 * SCSI command.  We need to record it so that we can properly report
2331255570Strasz	 * underflow/underflow.
2332255570Strasz	 */
2333255570Strasz	PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len;
2334255570Strasz
2335256065Strasz	/*
2336265490Strasz	 * This is the offset within the current SCSI command; for the first
2337265490Strasz	 * call to cfiscsi_datamove() it will be 0, and for subsequent ones
2338265490Strasz	 * it will be the sum of lengths of previous ones.  It's being
2339265490Strasz	 * incremented as we append data to the data segment.
2340256065Strasz	 */
2341265490Strasz	buffer_offset = io->scsiio.kern_rel_offset;
2342255570Strasz
2343265490Strasz	/*
2344265490Strasz	 * This is the transfer length expected by the initiator.  In theory,
2345265490Strasz	 * it could be different from the correct amount of data from the SCSI
2346265490Strasz	 * point of view, even if that doesn't make any sense.
2347265490Strasz	 */
2348265490Strasz	expected_len = ntohl(bhssc->bhssc_expected_data_transfer_length);
2349265490Strasz#if 0
2350265490Strasz	if (expected_len != io->scsiio.kern_total_len)
2351265490Strasz		CFISCSI_SESSION_DEBUG(cs, "expected transfer length = %zd, "
2352265490Strasz		    "actual length = %zd", expected_len,
2353265490Strasz		    io->scsiio.kern_total_len);
2354265490Strasz#endif
2355265490Strasz
2356265490Strasz	if (buffer_offset >= expected_len) {
2357265490Strasz#if 0
2358265490Strasz		CFISCSI_SESSION_DEBUG(cs, "buffer_offset = %zd, "
2359265490Strasz		    "already sent the expected len", buffer_offset);
2360265490Strasz#endif
2361265490Strasz		io->scsiio.ext_data_filled = io->scsiio.kern_total_len;
2362265490Strasz		io->scsiio.be_move_done(io);
2363265490Strasz		return;
2364265490Strasz	}
2365265490Strasz
2366256065Strasz	i = 0;
2367265490Strasz	sg_addr = NULL;
2368265490Strasz	sg_len = 0;
2369256065Strasz	response = NULL;
2370256065Strasz	bhsdi = NULL;
2371256065Strasz	for (;;) {
2372256065Strasz		if (response == NULL) {
2373256065Strasz			response = cfiscsi_pdu_new_response(request, M_NOWAIT);
2374255570Strasz			if (response == NULL) {
2375255824Strasz				CFISCSI_SESSION_WARN(cs, "failed to "
2376255824Strasz				    "allocate memory; dropping connection");
2377256163Strasz				ctl_set_busy(&io->scsiio);
2378256163Strasz				io->scsiio.be_move_done(io);
2379255824Strasz				cfiscsi_session_terminate(cs);
2380255824Strasz				return;
2381255824Strasz			}
2382256065Strasz			bhsdi = (struct iscsi_bhs_data_in *)response->ip_bhs;
2383256065Strasz			bhsdi->bhsdi_opcode = ISCSI_BHS_OPCODE_SCSI_DATA_IN;
2384256065Strasz			bhsdi->bhsdi_initiator_task_tag =
2385256065Strasz			    bhssc->bhssc_initiator_task_tag;
2386256065Strasz			bhsdi->bhsdi_datasn = htonl(PDU_EXPDATASN(request));
2387256065Strasz			PDU_EXPDATASN(request)++;
2388265490Strasz			bhsdi->bhsdi_buffer_offset = htonl(buffer_offset);
2389256065Strasz		}
2390255570Strasz
2391265490Strasz		KASSERT(i < ctl_sg_count, ("i >= ctl_sg_count"));
2392265490Strasz		if (sg_len == 0) {
2393265490Strasz			sg_addr = ctl_sglist[i].addr;
2394265490Strasz			sg_len = ctl_sglist[i].len;
2395265490Strasz			KASSERT(sg_len > 0, ("sg_len <= 0"));
2396256065Strasz		}
2397255570Strasz
2398265490Strasz		len = sg_len;
2399265490Strasz
2400265490Strasz		/*
2401265490Strasz		 * Truncate to maximum data segment length.
2402265490Strasz		 */
2403265490Strasz		KASSERT(response->ip_data_len < cs->cs_max_data_segment_length,
2404265490Strasz		    ("max_data_segment_length %zd >= ip_data_len %zd",
2405265490Strasz		    response->ip_data_len, cs->cs_max_data_segment_length));
2406265490Strasz		if (response->ip_data_len + len >
2407256065Strasz		    cs->cs_max_data_segment_length)
2408265490Strasz			len = cs->cs_max_data_segment_length -
2409256065Strasz			    response->ip_data_len;
2410265490Strasz
2411265490Strasz		/*
2412265490Strasz		 * Truncate to expected data transfer length.
2413265490Strasz		 */
2414265490Strasz		KASSERT(buffer_offset + response->ip_data_len < expected_len,
2415265490Strasz		    ("%zd >= %zd", buffer_offset + response->ip_data_len, expected_len));
2416265490Strasz		if (buffer_offset + response->ip_data_len + len > expected_len) {
2417265490Strasz			CFISCSI_SESSION_DEBUG(cs, "truncating from %zd "
2418265490Strasz			    "to expected data transfer length %zd",
2419265490Strasz			    buffer_offset + response->ip_data_len + len, expected_len);
2420265490Strasz			len = expected_len - (buffer_offset + response->ip_data_len);
2421265490Strasz		}
2422265490Strasz
2423265490Strasz		KASSERT(len <= sg_len, ("len > sg_len"));
2424265490Strasz		error = icl_pdu_append_data(response, sg_addr, len, M_NOWAIT);
2425256065Strasz		if (error != 0) {
2426256065Strasz			CFISCSI_SESSION_WARN(cs, "failed to "
2427256065Strasz			    "allocate memory; dropping connection");
2428256065Strasz			icl_pdu_free(response);
2429256163Strasz			ctl_set_busy(&io->scsiio);
2430256163Strasz			io->scsiio.be_move_done(io);
2431256065Strasz			cfiscsi_session_terminate(cs);
2432256065Strasz			return;
2433256065Strasz		}
2434265490Strasz		sg_addr += len;
2435265490Strasz		sg_len -= len;
2436265490Strasz		buffer_offset += len;
2437265490Strasz		io->scsiio.ext_data_filled += len;
2438256065Strasz
2439265490Strasz		if (buffer_offset == expected_len) {
2440256065Strasz			/*
2441265490Strasz			 * Already have the amount of data the initiator wanted.
2442265490Strasz			 */
2443265490Strasz			break;
2444265490Strasz		}
2445265490Strasz
2446265490Strasz		if (sg_len == 0) {
2447265490Strasz			/*
2448256065Strasz			 * End of scatter-gather segment;
2449256065Strasz			 * proceed to the next one...
2450256065Strasz			 */
2451256065Strasz			if (i == ctl_sg_count - 1) {
2452255570Strasz				/*
2453256065Strasz				 * ... unless this was the last one.
2454255570Strasz				 */
2455256065Strasz				break;
2456255570Strasz			}
2457256065Strasz			i++;
2458255570Strasz		}
2459256065Strasz
2460256065Strasz		if (response->ip_data_len == cs->cs_max_data_segment_length) {
2461256065Strasz			/*
2462256065Strasz			 * Can't stuff more data into the current PDU;
2463256065Strasz			 * queue it.  Note that's not enough to check
2464256195Strasz			 * for kern_data_resid == 0 instead; there
2465256065Strasz			 * may be several Data-In PDUs for the final
2466256065Strasz			 * call to cfiscsi_datamove(), and we want
2467256065Strasz			 * to set the F flag only on the last of them.
2468256065Strasz			 */
2469265490Strasz			if (buffer_offset == io->scsiio.kern_total_len ||
2470265490Strasz			    buffer_offset == expected_len)
2471255570Strasz				bhsdi->bhsdi_flags |= BHSDI_FLAGS_F;
2472255570Strasz			cfiscsi_pdu_queue(response);
2473256065Strasz			response = NULL;
2474256065Strasz			bhsdi = NULL;
2475255570Strasz		}
2476256065Strasz	}
2477256065Strasz	if (response != NULL) {
2478265490Strasz		if (buffer_offset == io->scsiio.kern_total_len ||
2479265490Strasz		    buffer_offset == expected_len)
2480256065Strasz			bhsdi->bhsdi_flags |= BHSDI_FLAGS_F;
2481256065Strasz		KASSERT(response->ip_data_len > 0, ("sending empty Data-In"));
2482256065Strasz		cfiscsi_pdu_queue(response);
2483256065Strasz	}
2484255570Strasz
2485256065Strasz	io->scsiio.be_move_done(io);
2486256065Strasz}
2487255570Strasz
2488256065Straszstatic void
2489256065Straszcfiscsi_datamove_out(union ctl_io *io)
2490256065Strasz{
2491256065Strasz	struct cfiscsi_session *cs;
2492256065Strasz	struct icl_pdu *request, *response;
2493256065Strasz	const struct iscsi_bhs_scsi_command *bhssc;
2494256065Strasz	struct iscsi_bhs_r2t *bhsr2t;
2495256065Strasz	struct cfiscsi_data_wait *cdw;
2496256065Strasz	uint32_t target_transfer_tag;
2497256065Strasz	bool done;
2498256065Strasz
2499256065Strasz	request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2500256065Strasz	cs = PDU_SESSION(request);
2501256065Strasz
2502256065Strasz	bhssc = (const struct iscsi_bhs_scsi_command *)request->ip_bhs;
2503256065Strasz	KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2504256065Strasz	    ISCSI_BHS_OPCODE_SCSI_COMMAND,
2505256065Strasz	    ("bhssc->bhssc_opcode != ISCSI_BHS_OPCODE_SCSI_COMMAND"));
2506256065Strasz
2507256065Strasz	/*
2508256065Strasz	 * We need to record it so that we can properly report
2509256065Strasz	 * underflow/underflow.
2510256065Strasz	 */
2511256065Strasz	PDU_TOTAL_TRANSFER_LEN(request) = io->scsiio.kern_total_len;
2512256065Strasz
2513256065Strasz	CFISCSI_SESSION_LOCK(cs);
2514256065Strasz	target_transfer_tag = cs->cs_target_transfer_tag;
2515256065Strasz	cs->cs_target_transfer_tag++;
2516256065Strasz	CFISCSI_SESSION_UNLOCK(cs);
2517256065Strasz
2518255570Strasz#if 0
2519256065Strasz	CFISCSI_SESSION_DEBUG(cs, "expecting Data-Out with initiator "
2520256065Strasz	    "task tag 0x%x, target transfer tag 0x%x",
2521256065Strasz	    bhssc->bhssc_initiator_task_tag, target_transfer_tag);
2522255570Strasz#endif
2523256065Strasz	cdw = uma_zalloc(cfiscsi_data_wait_zone, M_NOWAIT | M_ZERO);
2524256065Strasz	if (cdw == NULL) {
2525256065Strasz		CFISCSI_SESSION_WARN(cs, "failed to "
2526256065Strasz		    "allocate memory; dropping connection");
2527256163Strasz		ctl_set_busy(&io->scsiio);
2528256163Strasz		io->scsiio.be_move_done(io);
2529256065Strasz		cfiscsi_session_terminate(cs);
2530256163Strasz		return;
2531256065Strasz	}
2532256065Strasz	cdw->cdw_ctl_io = io;
2533265489Strasz	cdw->cdw_target_transfer_tag = target_transfer_tag;
2534256065Strasz	cdw->cdw_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2535256065Strasz
2536256065Strasz	if (cs->cs_immediate_data && icl_pdu_data_segment_length(request) > 0) {
2537256065Strasz		done = cfiscsi_handle_data_segment(request, cdw);
2538256065Strasz		if (done) {
2539256065Strasz			uma_zfree(cfiscsi_data_wait_zone, cdw);
2540256065Strasz			io->scsiio.be_move_done(io);
2541256065Strasz			return;
2542255837Strasz		}
2543255570Strasz
2544255570Strasz#if 0
2545256065Strasz		if (io->scsiio.ext_data_filled != 0)
2546256065Strasz			CFISCSI_SESSION_DEBUG(cs, "got %zd bytes of immediate data, need %zd",
2547256065Strasz			    io->scsiio.ext_data_filled, io->scsiio.kern_data_len);
2548255570Strasz#endif
2549256065Strasz	}
2550255570Strasz
2551256065Strasz	CFISCSI_SESSION_LOCK(cs);
2552256065Strasz	TAILQ_INSERT_TAIL(&cs->cs_waiting_for_data_out, cdw, cdw_next);
2553256065Strasz	CFISCSI_SESSION_UNLOCK(cs);
2554255570Strasz
2555256065Strasz	/*
2556256065Strasz	 * XXX: We should limit the number of outstanding R2T PDUs
2557256065Strasz	 * 	per task to MaxOutstandingR2T.
2558256065Strasz	 */
2559256065Strasz	response = cfiscsi_pdu_new_response(request, M_NOWAIT);
2560256065Strasz	if (response == NULL) {
2561256065Strasz		CFISCSI_SESSION_WARN(cs, "failed to "
2562256065Strasz		    "allocate memory; dropping connection");
2563256163Strasz		ctl_set_busy(&io->scsiio);
2564256163Strasz		io->scsiio.be_move_done(io);
2565256065Strasz		cfiscsi_session_terminate(cs);
2566256163Strasz		return;
2567255570Strasz	}
2568256065Strasz	bhsr2t = (struct iscsi_bhs_r2t *)response->ip_bhs;
2569256065Strasz	bhsr2t->bhsr2t_opcode = ISCSI_BHS_OPCODE_R2T;
2570256065Strasz	bhsr2t->bhsr2t_flags = 0x80;
2571256065Strasz	bhsr2t->bhsr2t_lun = bhssc->bhssc_lun;
2572256065Strasz	bhsr2t->bhsr2t_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2573265489Strasz	bhsr2t->bhsr2t_target_transfer_tag = target_transfer_tag;
2574256065Strasz	/*
2575256065Strasz	 * XXX: Here we assume that cfiscsi_datamove() won't ever
2576256065Strasz	 *	be running concurrently on several CPUs for a given
2577256065Strasz	 *	command.
2578256065Strasz	 */
2579256065Strasz	bhsr2t->bhsr2t_r2tsn = htonl(PDU_R2TSN(request));
2580256065Strasz	PDU_R2TSN(request)++;
2581256065Strasz	/*
2582256065Strasz	 * This is the offset within the current SCSI command;
2583256065Strasz	 * i.e. for the first call of datamove(), it will be 0,
2584256065Strasz	 * and for subsequent ones it will be the sum of lengths
2585256065Strasz	 * of previous ones.
2586256065Strasz	 *
2587256065Strasz	 * The ext_data_filled is to account for unsolicited
2588256065Strasz	 * (immediate) data that might have already arrived.
2589256065Strasz	 */
2590256065Strasz	bhsr2t->bhsr2t_buffer_offset =
2591256065Strasz	    htonl(io->scsiio.kern_rel_offset + io->scsiio.ext_data_filled);
2592256065Strasz	/*
2593256065Strasz	 * This is the total length (sum of S/G lengths) this call
2594256065Strasz	 * to cfiscsi_datamove() is supposed to handle.
2595256065Strasz	 *
2596256065Strasz	 * XXX: Limit it to MaxBurstLength.
2597256065Strasz	 */
2598256065Strasz	bhsr2t->bhsr2t_desired_data_transfer_length =
2599256065Strasz	    htonl(io->scsiio.kern_data_len - io->scsiio.ext_data_filled);
2600256065Strasz	cfiscsi_pdu_queue(response);
2601255570Strasz}
2602255570Strasz
2603255570Straszstatic void
2604256065Straszcfiscsi_datamove(union ctl_io *io)
2605256065Strasz{
2606256065Strasz
2607256065Strasz	if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
2608256065Strasz		cfiscsi_datamove_in(io);
2609256065Strasz	else
2610256065Strasz		cfiscsi_datamove_out(io);
2611256065Strasz}
2612256065Strasz
2613256065Straszstatic void
2614255570Straszcfiscsi_scsi_command_done(union ctl_io *io)
2615255570Strasz{
2616255570Strasz	struct icl_pdu *request, *response;
2617255570Strasz	struct iscsi_bhs_scsi_command *bhssc;
2618255570Strasz	struct iscsi_bhs_scsi_response *bhssr;
2619255570Strasz#ifdef DIAGNOSTIC
2620255570Strasz	struct cfiscsi_data_wait *cdw;
2621255570Strasz#endif
2622255570Strasz	struct cfiscsi_session *cs;
2623255570Strasz	uint16_t sense_length;
2624255570Strasz
2625255570Strasz	request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2626255570Strasz	cs = PDU_SESSION(request);
2627255570Strasz	bhssc = (struct iscsi_bhs_scsi_command *)request->ip_bhs;
2628255570Strasz	KASSERT((bhssc->bhssc_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2629255570Strasz	    ISCSI_BHS_OPCODE_SCSI_COMMAND,
2630255570Strasz	    ("replying to wrong opcode 0x%x", bhssc->bhssc_opcode));
2631255570Strasz
2632255570Strasz	//CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x",
2633255570Strasz	//    bhssc->bhssc_initiator_task_tag);
2634255570Strasz
2635255570Strasz#ifdef DIAGNOSTIC
2636255570Strasz	CFISCSI_SESSION_LOCK(cs);
2637255570Strasz	TAILQ_FOREACH(cdw, &cs->cs_waiting_for_data_out, cdw_next)
2638255570Strasz		KASSERT(bhssc->bhssc_initiator_task_tag !=
2639255570Strasz		    cdw->cdw_initiator_task_tag, ("dangling cdw"));
2640255570Strasz	CFISCSI_SESSION_UNLOCK(cs);
2641255570Strasz#endif
2642255570Strasz
2643255570Strasz	response = cfiscsi_pdu_new_response(request, M_WAITOK);
2644255570Strasz	bhssr = (struct iscsi_bhs_scsi_response *)response->ip_bhs;
2645255570Strasz	bhssr->bhssr_opcode = ISCSI_BHS_OPCODE_SCSI_RESPONSE;
2646255570Strasz	bhssr->bhssr_flags = 0x80;
2647255570Strasz	/*
2648255570Strasz	 * XXX: We don't deal with bidirectional under/overflows;
2649255570Strasz	 *	does anything actually support those?
2650255570Strasz	 */
2651255570Strasz	if (PDU_TOTAL_TRANSFER_LEN(request) <
2652255570Strasz	    ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2653255570Strasz		bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_UNDERFLOW;
2654255570Strasz		bhssr->bhssr_residual_count =
2655255570Strasz		    htonl(ntohl(bhssc->bhssc_expected_data_transfer_length) -
2656255570Strasz		    PDU_TOTAL_TRANSFER_LEN(request));
2657255570Strasz		//CFISCSI_SESSION_DEBUG(cs, "underflow; residual count %d",
2658255570Strasz		//    ntohl(bhssr->bhssr_residual_count));
2659255570Strasz	} else if (PDU_TOTAL_TRANSFER_LEN(request) >
2660255570Strasz	    ntohl(bhssc->bhssc_expected_data_transfer_length)) {
2661255570Strasz		bhssr->bhssr_flags |= BHSSR_FLAGS_RESIDUAL_OVERFLOW;
2662255570Strasz		bhssr->bhssr_residual_count =
2663255570Strasz		    htonl(PDU_TOTAL_TRANSFER_LEN(request) -
2664255570Strasz		    ntohl(bhssc->bhssc_expected_data_transfer_length));
2665255570Strasz		//CFISCSI_SESSION_DEBUG(cs, "overflow; residual count %d",
2666255570Strasz		//    ntohl(bhssr->bhssr_residual_count));
2667255570Strasz	}
2668255570Strasz	bhssr->bhssr_response = BHSSR_RESPONSE_COMMAND_COMPLETED;
2669255570Strasz	bhssr->bhssr_status = io->scsiio.scsi_status;
2670255570Strasz	bhssr->bhssr_initiator_task_tag = bhssc->bhssc_initiator_task_tag;
2671255570Strasz	bhssr->bhssr_expdatasn = htonl(PDU_EXPDATASN(request));
2672255570Strasz
2673255570Strasz	if (io->scsiio.sense_len > 0) {
2674255570Strasz#if 0
2675255570Strasz		CFISCSI_SESSION_DEBUG(cs, "returning %d bytes of sense data",
2676255570Strasz		    io->scsiio.sense_len);
2677255570Strasz#endif
2678255570Strasz		sense_length = htons(io->scsiio.sense_len);
2679255570Strasz		icl_pdu_append_data(response,
2680255570Strasz		    &sense_length, sizeof(sense_length), M_WAITOK);
2681255570Strasz		icl_pdu_append_data(response,
2682255570Strasz		    &io->scsiio.sense_data, io->scsiio.sense_len, M_WAITOK);
2683255570Strasz	}
2684255570Strasz
2685255570Strasz	ctl_free_io(io);
2686255570Strasz	icl_pdu_free(request);
2687255570Strasz	cfiscsi_pdu_queue(response);
2688255570Strasz}
2689255570Strasz
2690255570Straszstatic void
2691255570Straszcfiscsi_task_management_done(union ctl_io *io)
2692255570Strasz{
2693255570Strasz	struct icl_pdu *request, *response;
2694255570Strasz	struct iscsi_bhs_task_management_request *bhstmr;
2695255570Strasz	struct iscsi_bhs_task_management_response *bhstmr2;
2696255570Strasz	struct cfiscsi_data_wait *cdw, *tmpcdw;
2697255570Strasz	struct cfiscsi_session *cs;
2698255570Strasz
2699255570Strasz	request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2700255570Strasz	cs = PDU_SESSION(request);
2701255570Strasz	bhstmr = (struct iscsi_bhs_task_management_request *)request->ip_bhs;
2702255570Strasz	KASSERT((bhstmr->bhstmr_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) ==
2703255570Strasz	    ISCSI_BHS_OPCODE_TASK_REQUEST,
2704255570Strasz	    ("replying to wrong opcode 0x%x", bhstmr->bhstmr_opcode));
2705255570Strasz
2706255570Strasz#if 0
2707255570Strasz	CFISCSI_SESSION_DEBUG(cs, "initiator task tag 0x%x; referenced task tag 0x%x",
2708255570Strasz	    bhstmr->bhstmr_initiator_task_tag,
2709255570Strasz	    bhstmr->bhstmr_referenced_task_tag);
2710255570Strasz#endif
2711255570Strasz
2712255570Strasz	if ((bhstmr->bhstmr_function & ~0x80) ==
2713255570Strasz	    BHSTMR_FUNCTION_ABORT_TASK) {
2714255570Strasz		/*
2715255570Strasz		 * Make sure we no longer wait for Data-Out for this command.
2716255570Strasz		 */
2717255570Strasz		CFISCSI_SESSION_LOCK(cs);
2718255570Strasz		TAILQ_FOREACH_SAFE(cdw,
2719255570Strasz		    &cs->cs_waiting_for_data_out, cdw_next, tmpcdw) {
2720255570Strasz			if (bhstmr->bhstmr_referenced_task_tag !=
2721255570Strasz			    cdw->cdw_initiator_task_tag)
2722255570Strasz				continue;
2723255570Strasz
2724255570Strasz#if 0
2725255570Strasz			CFISCSI_SESSION_DEBUG(cs, "removing csw for initiator task "
2726255570Strasz			    "tag 0x%x", bhstmr->bhstmr_initiator_task_tag);
2727255570Strasz#endif
2728255570Strasz			TAILQ_REMOVE(&cs->cs_waiting_for_data_out,
2729255570Strasz			    cdw, cdw_next);
2730255570Strasz			cdw->cdw_ctl_io->scsiio.be_move_done(cdw->cdw_ctl_io);
2731255570Strasz			uma_zfree(cfiscsi_data_wait_zone, cdw);
2732255570Strasz		}
2733255570Strasz		CFISCSI_SESSION_UNLOCK(cs);
2734255570Strasz	}
2735255570Strasz
2736255570Strasz	response = cfiscsi_pdu_new_response(request, M_WAITOK);
2737255570Strasz	bhstmr2 = (struct iscsi_bhs_task_management_response *)
2738255570Strasz	    response->ip_bhs;
2739255570Strasz	bhstmr2->bhstmr_opcode = ISCSI_BHS_OPCODE_TASK_RESPONSE;
2740255570Strasz	bhstmr2->bhstmr_flags = 0x80;
2741255570Strasz	if (io->io_hdr.status == CTL_SUCCESS) {
2742255570Strasz		bhstmr2->bhstmr_response = BHSTMR_RESPONSE_FUNCTION_COMPLETE;
2743255570Strasz	} else {
2744255570Strasz		/*
2745255570Strasz		 * XXX: How to figure out what exactly went wrong?  iSCSI spec
2746255570Strasz		 * 	expects us to provide detailed error, e.g. "Task does
2747255570Strasz		 * 	not exist" or "LUN does not exist".
2748255570Strasz		 */
2749255570Strasz		CFISCSI_SESSION_DEBUG(cs, "BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED");
2750255570Strasz		bhstmr2->bhstmr_response =
2751255570Strasz		    BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED;
2752255570Strasz	}
2753255570Strasz	bhstmr2->bhstmr_initiator_task_tag = bhstmr->bhstmr_initiator_task_tag;
2754255570Strasz
2755255570Strasz	ctl_free_io(io);
2756255570Strasz	icl_pdu_free(request);
2757255570Strasz	cfiscsi_pdu_queue(response);
2758255570Strasz}
2759255570Strasz
2760255570Straszstatic void
2761255570Straszcfiscsi_done(union ctl_io *io)
2762255570Strasz{
2763255570Strasz	struct icl_pdu *request;
2764255570Strasz	struct cfiscsi_session *cs;
2765255570Strasz
2766255570Strasz	KASSERT(((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE),
2767255570Strasz		("invalid CTL status %#x", io->io_hdr.status));
2768255570Strasz
2769255570Strasz	request = io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2770255570Strasz	if (request == NULL) {
2771255570Strasz		/*
2772255570Strasz		 * Implicit task termination has just completed; nothing to do.
2773255570Strasz		 */
2774255570Strasz		return;
2775255570Strasz	}
2776255570Strasz
2777255570Strasz	cs = PDU_SESSION(request);
2778255570Strasz	refcount_release(&cs->cs_outstanding_ctl_pdus);
2779255570Strasz
2780255570Strasz	switch (request->ip_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) {
2781255570Strasz	case ISCSI_BHS_OPCODE_SCSI_COMMAND:
2782255570Strasz		cfiscsi_scsi_command_done(io);
2783255570Strasz		break;
2784255570Strasz	case ISCSI_BHS_OPCODE_TASK_REQUEST:
2785255570Strasz		cfiscsi_task_management_done(io);
2786255570Strasz		break;
2787255570Strasz	default:
2788255570Strasz		panic("cfiscsi_done called with wrong opcode 0x%x",
2789255570Strasz		    request->ip_bhs->bhs_opcode);
2790255570Strasz	}
2791255570Strasz}
2792