xref: /freebsd/sys/dev/cxgbe/cxgbei/icl_cxgbei.c (revision 77013d11e6483b970af25e13c9b892075742f7e5)
1 /*-
2  * Copyright (c) 2012 The FreeBSD Foundation
3  * Copyright (c) 2015 Chelsio Communications, Inc.
4  * All rights reserved.
5  *
6  * This software was developed by Edward Tomasz Napierala under sponsorship
7  * from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  */
31 
32 /*
33  * cxgbei implementation of iSCSI Common Layer kobj(9) interface.
34  */
35 
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 #include "opt_inet.h"
40 #include "opt_inet6.h"
41 
42 #ifdef TCP_OFFLOAD
43 #include <sys/param.h>
44 #include <sys/capsicum.h>
45 #include <sys/condvar.h>
46 #include <sys/conf.h>
47 #include <sys/file.h>
48 #include <sys/kernel.h>
49 #include <sys/kthread.h>
50 #include <sys/ktr.h>
51 #include <sys/lock.h>
52 #include <sys/mbuf.h>
53 #include <sys/mutex.h>
54 #include <sys/module.h>
55 #include <sys/protosw.h>
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
58 #include <sys/sysctl.h>
59 #include <sys/systm.h>
60 #include <sys/sx.h>
61 #include <sys/uio.h>
62 #include <machine/bus.h>
63 #include <vm/vm.h>
64 #include <vm/pmap.h>
65 #include <netinet/in.h>
66 #include <netinet/in_pcb.h>
67 #include <netinet/tcp.h>
68 #include <netinet/tcp_var.h>
69 #include <netinet/toecore.h>
70 
71 #include <dev/iscsi/icl.h>
72 #include <dev/iscsi/iscsi_proto.h>
73 #include <icl_conn_if.h>
74 
75 #include <cam/scsi/scsi_all.h>
76 #include <cam/scsi/scsi_da.h>
77 #include <cam/ctl/ctl_io.h>
78 #include <cam/ctl/ctl.h>
79 #include <cam/ctl/ctl_backend.h>
80 #include <cam/ctl/ctl_error.h>
81 #include <cam/ctl/ctl_frontend.h>
82 #include <cam/ctl/ctl_debug.h>
83 #include <cam/ctl/ctl_ha.h>
84 #include <cam/ctl/ctl_ioctl.h>
85 
86 #include <cam/cam.h>
87 #include <cam/cam_ccb.h>
88 #include <cam/cam_xpt.h>
89 #include <cam/cam_debug.h>
90 #include <cam/cam_sim.h>
91 #include <cam/cam_xpt_sim.h>
92 #include <cam/cam_xpt_periph.h>
93 #include <cam/cam_periph.h>
94 #include <cam/cam_compat.h>
95 #include <cam/scsi/scsi_message.h>
96 
97 #include "common/common.h"
98 #include "common/t4_tcb.h"
99 #include "tom/t4_tom.h"
100 #include "cxgbei.h"
101 
102 /*
103  * Use the page pod tag for the TT hash.
104  */
105 #define	TT_HASH(icc, tt)	(G_PPOD_TAG(tt) & (icc)->cmp_hash_mask)
106 
107 struct cxgbei_ddp_state {
108 	struct ppod_reservation prsv;
109 	struct cxgbei_cmp cmp;
110 };
111 
112 static MALLOC_DEFINE(M_CXGBEI, "cxgbei", "cxgbei(4)");
113 
114 SYSCTL_NODE(_kern_icl, OID_AUTO, cxgbei, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
115     "Chelsio iSCSI offload");
116 static int first_burst_length = 8192;
117 SYSCTL_INT(_kern_icl_cxgbei, OID_AUTO, first_burst_length, CTLFLAG_RWTUN,
118     &first_burst_length, 0, "First burst length");
119 static int max_burst_length = 2 * 1024 * 1024;
120 SYSCTL_INT(_kern_icl_cxgbei, OID_AUTO, max_burst_length, CTLFLAG_RWTUN,
121     &max_burst_length, 0, "Maximum burst length");
122 static int sendspace = 1048576;
123 SYSCTL_INT(_kern_icl_cxgbei, OID_AUTO, sendspace, CTLFLAG_RWTUN,
124     &sendspace, 0, "Default send socket buffer size");
125 static int recvspace = 1048576;
126 SYSCTL_INT(_kern_icl_cxgbei, OID_AUTO, recvspace, CTLFLAG_RWTUN,
127     &recvspace, 0, "Default receive socket buffer size");
128 
129 static volatile u_int icl_cxgbei_ncons;
130 
131 #define ICL_CONN_LOCK(X)		mtx_lock(X->ic_lock)
132 #define ICL_CONN_UNLOCK(X)		mtx_unlock(X->ic_lock)
133 #define ICL_CONN_LOCK_ASSERT(X)		mtx_assert(X->ic_lock, MA_OWNED)
134 #define ICL_CONN_LOCK_ASSERT_NOT(X)	mtx_assert(X->ic_lock, MA_NOTOWNED)
135 
136 static icl_conn_new_pdu_t	icl_cxgbei_conn_new_pdu;
137 static icl_conn_pdu_data_segment_length_t
138 				    icl_cxgbei_conn_pdu_data_segment_length;
139 static icl_conn_pdu_append_data_t	icl_cxgbei_conn_pdu_append_data;
140 static icl_conn_pdu_get_data_t	icl_cxgbei_conn_pdu_get_data;
141 static icl_conn_pdu_queue_t	icl_cxgbei_conn_pdu_queue;
142 static icl_conn_pdu_queue_cb_t	icl_cxgbei_conn_pdu_queue_cb;
143 static icl_conn_handoff_t	icl_cxgbei_conn_handoff;
144 static icl_conn_free_t		icl_cxgbei_conn_free;
145 static icl_conn_close_t		icl_cxgbei_conn_close;
146 static icl_conn_task_setup_t	icl_cxgbei_conn_task_setup;
147 static icl_conn_task_done_t	icl_cxgbei_conn_task_done;
148 static icl_conn_transfer_setup_t	icl_cxgbei_conn_transfer_setup;
149 static icl_conn_transfer_done_t	icl_cxgbei_conn_transfer_done;
150 
151 static kobj_method_t icl_cxgbei_methods[] = {
152 	KOBJMETHOD(icl_conn_new_pdu, icl_cxgbei_conn_new_pdu),
153 	KOBJMETHOD(icl_conn_pdu_free, icl_cxgbei_conn_pdu_free),
154 	KOBJMETHOD(icl_conn_pdu_data_segment_length,
155 	    icl_cxgbei_conn_pdu_data_segment_length),
156 	KOBJMETHOD(icl_conn_pdu_append_data, icl_cxgbei_conn_pdu_append_data),
157 	KOBJMETHOD(icl_conn_pdu_get_data, icl_cxgbei_conn_pdu_get_data),
158 	KOBJMETHOD(icl_conn_pdu_queue, icl_cxgbei_conn_pdu_queue),
159 	KOBJMETHOD(icl_conn_pdu_queue_cb, icl_cxgbei_conn_pdu_queue_cb),
160 	KOBJMETHOD(icl_conn_handoff, icl_cxgbei_conn_handoff),
161 	KOBJMETHOD(icl_conn_free, icl_cxgbei_conn_free),
162 	KOBJMETHOD(icl_conn_close, icl_cxgbei_conn_close),
163 	KOBJMETHOD(icl_conn_task_setup, icl_cxgbei_conn_task_setup),
164 	KOBJMETHOD(icl_conn_task_done, icl_cxgbei_conn_task_done),
165 	KOBJMETHOD(icl_conn_transfer_setup, icl_cxgbei_conn_transfer_setup),
166 	KOBJMETHOD(icl_conn_transfer_done, icl_cxgbei_conn_transfer_done),
167 	{ 0, 0 }
168 };
169 
170 DEFINE_CLASS(icl_cxgbei, icl_cxgbei_methods, sizeof(struct icl_cxgbei_conn));
171 
172 void
173 icl_cxgbei_conn_pdu_free(struct icl_conn *ic, struct icl_pdu *ip)
174 {
175 	struct icl_cxgbei_pdu *icp = ip_to_icp(ip);
176 
177 	KASSERT(icp->ref_cnt != 0, ("freeing deleted PDU"));
178 	MPASS(icp->icp_signature == CXGBEI_PDU_SIGNATURE);
179 	MPASS(ic == ip->ip_conn);
180 
181 	m_freem(ip->ip_ahs_mbuf);
182 	m_freem(ip->ip_data_mbuf);
183 	m_freem(ip->ip_bhs_mbuf);
184 
185 	KASSERT(ic != NULL || icp->ref_cnt == 1,
186 	    ("orphaned PDU has oustanding references"));
187 
188 	if (atomic_fetchadd_int(&icp->ref_cnt, -1) != 1)
189 		return;
190 
191 	free(icp, M_CXGBEI);
192 #ifdef DIAGNOSTIC
193 	if (__predict_true(ic != NULL))
194 		refcount_release(&ic->ic_outstanding_pdus);
195 #endif
196 }
197 
198 static void
199 icl_cxgbei_pdu_call_cb(struct icl_pdu *ip)
200 {
201 	struct icl_cxgbei_pdu *icp = ip_to_icp(ip);
202 
203 	MPASS(icp->icp_signature == CXGBEI_PDU_SIGNATURE);
204 
205 	if (icp->cb != NULL)
206 		icp->cb(ip, icp->error);
207 #ifdef DIAGNOSTIC
208 	if (__predict_true(ip->ip_conn != NULL))
209 		refcount_release(&ip->ip_conn->ic_outstanding_pdus);
210 #endif
211 	free(icp, M_CXGBEI);
212 }
213 
214 static void
215 icl_cxgbei_pdu_done(struct icl_pdu *ip, int error)
216 {
217 	struct icl_cxgbei_pdu *icp = ip_to_icp(ip);
218 
219 	if (error != 0)
220 		icp->error = error;
221 
222 	m_freem(ip->ip_ahs_mbuf);
223 	ip->ip_ahs_mbuf = NULL;
224 	m_freem(ip->ip_data_mbuf);
225 	ip->ip_data_mbuf = NULL;
226 	m_freem(ip->ip_bhs_mbuf);
227 	ip->ip_bhs_mbuf = NULL;
228 
229 	/*
230 	 * All other references to this PDU should have been dropped
231 	 * by the m_freem() of ip_data_mbuf.
232 	 */
233 	if (atomic_fetchadd_int(&icp->ref_cnt, -1) == 1)
234 		icl_cxgbei_pdu_call_cb(ip);
235 	else
236 		__assert_unreachable();
237 }
238 
239 static void
240 icl_cxgbei_mbuf_done(struct mbuf *mb)
241 {
242 
243 	struct icl_cxgbei_pdu *icp = (struct icl_cxgbei_pdu *)mb->m_ext.ext_arg1;
244 
245 	/*
246 	 * NB: mb_free_mext() might leave ref_cnt as 1 without
247 	 * decrementing it if it hits the fast path in the ref_cnt
248 	 * check.
249 	 */
250 	icl_cxgbei_pdu_call_cb(&icp->ip);
251 }
252 
253 struct icl_pdu *
254 icl_cxgbei_new_pdu(int flags)
255 {
256 	struct icl_cxgbei_pdu *icp;
257 	struct icl_pdu *ip;
258 	struct mbuf *m;
259 
260 	icp = malloc(sizeof(*icp), M_CXGBEI, flags | M_ZERO);
261 	if (__predict_false(icp == NULL))
262 		return (NULL);
263 
264 	icp->icp_signature = CXGBEI_PDU_SIGNATURE;
265 	icp->ref_cnt = 1;
266 	ip = &icp->ip;
267 
268 	m = m_gethdr(flags, MT_DATA);
269 	if (__predict_false(m == NULL)) {
270 		free(icp, M_CXGBEI);
271 		return (NULL);
272 	}
273 
274 	ip->ip_bhs_mbuf = m;
275 	ip->ip_bhs = mtod(m, struct iscsi_bhs *);
276 	memset(ip->ip_bhs, 0, sizeof(*ip->ip_bhs));
277 	m->m_len = sizeof(struct iscsi_bhs);
278 	m->m_pkthdr.len = m->m_len;
279 
280 	return (ip);
281 }
282 
283 void
284 icl_cxgbei_new_pdu_set_conn(struct icl_pdu *ip, struct icl_conn *ic)
285 {
286 
287 	ip->ip_conn = ic;
288 #ifdef DIAGNOSTIC
289 	refcount_acquire(&ic->ic_outstanding_pdus);
290 #endif
291 }
292 
293 /*
294  * Allocate icl_pdu with empty BHS to fill up by the caller.
295  */
296 static struct icl_pdu *
297 icl_cxgbei_conn_new_pdu(struct icl_conn *ic, int flags)
298 {
299 	struct icl_pdu *ip;
300 
301 	ip = icl_cxgbei_new_pdu(flags);
302 	if (__predict_false(ip == NULL))
303 		return (NULL);
304 	icl_cxgbei_new_pdu_set_conn(ip, ic);
305 
306 	return (ip);
307 }
308 
309 static size_t
310 icl_pdu_data_segment_length(const struct icl_pdu *request)
311 {
312 	uint32_t len = 0;
313 
314 	len += request->ip_bhs->bhs_data_segment_len[0];
315 	len <<= 8;
316 	len += request->ip_bhs->bhs_data_segment_len[1];
317 	len <<= 8;
318 	len += request->ip_bhs->bhs_data_segment_len[2];
319 
320 	return (len);
321 }
322 
323 size_t
324 icl_cxgbei_conn_pdu_data_segment_length(struct icl_conn *ic,
325     const struct icl_pdu *request)
326 {
327 
328 	return (icl_pdu_data_segment_length(request));
329 }
330 
331 static struct mbuf *
332 finalize_pdu(struct icl_cxgbei_conn *icc, struct icl_cxgbei_pdu *icp)
333 {
334 	struct icl_pdu *ip = &icp->ip;
335 	uint8_t ulp_submode, padding;
336 	struct mbuf *m, *last;
337 	struct iscsi_bhs *bhs;
338 
339 	/*
340 	 * Fix up the data segment mbuf first.
341 	 */
342 	m = ip->ip_data_mbuf;
343 	ulp_submode = icc->ulp_submode;
344 	if (m) {
345 		last = m_last(m);
346 
347 		/*
348 		 * Round up the data segment to a 4B boundary.	Pad with 0 if
349 		 * necessary.  There will definitely be room in the mbuf.
350 		 */
351 		padding = roundup2(ip->ip_data_len, 4) - ip->ip_data_len;
352 		if (padding) {
353 			bzero(mtod(last, uint8_t *) + last->m_len, padding);
354 			last->m_len += padding;
355 		}
356 	} else {
357 		MPASS(ip->ip_data_len == 0);
358 		ulp_submode &= ~ULP_CRC_DATA;
359 		padding = 0;
360 	}
361 
362 	/*
363 	 * Now the header mbuf that has the BHS.
364 	 */
365 	m = ip->ip_bhs_mbuf;
366 	MPASS(m->m_pkthdr.len == sizeof(struct iscsi_bhs));
367 	MPASS(m->m_len == sizeof(struct iscsi_bhs));
368 
369 	bhs = ip->ip_bhs;
370 	bhs->bhs_data_segment_len[2] = ip->ip_data_len;
371 	bhs->bhs_data_segment_len[1] = ip->ip_data_len >> 8;
372 	bhs->bhs_data_segment_len[0] = ip->ip_data_len >> 16;
373 
374 	/*
375 	 * Extract mbuf chain from PDU.
376 	 */
377 	m->m_pkthdr.len += ip->ip_data_len + padding;
378 	m->m_next = ip->ip_data_mbuf;
379 	set_mbuf_ulp_submode(m, ulp_submode);
380 	ip->ip_bhs_mbuf = NULL;
381 	ip->ip_data_mbuf = NULL;
382 	ip->ip_bhs = NULL;
383 
384 	/*
385 	 * Drop PDU reference on icp.  Additional references might
386 	 * still be held by zero-copy PDU buffers (ICL_NOCOPY).
387 	 */
388 	if (atomic_fetchadd_int(&icp->ref_cnt, -1) == 1)
389 		icl_cxgbei_pdu_call_cb(ip);
390 
391 	return (m);
392 }
393 
394 int
395 icl_cxgbei_conn_pdu_append_data(struct icl_conn *ic, struct icl_pdu *ip,
396     const void *addr, size_t len, int flags)
397 {
398 	struct icl_cxgbei_pdu *icp = ip_to_icp(ip);
399 	struct mbuf *m, *m_tail;
400 	const char *src;
401 
402 	MPASS(icp->icp_signature == CXGBEI_PDU_SIGNATURE);
403 	MPASS(ic == ip->ip_conn);
404 	KASSERT(len > 0, ("%s: len is %jd", __func__, (intmax_t)len));
405 
406 	m_tail = ip->ip_data_mbuf;
407 	if (m_tail != NULL)
408 		for (; m_tail->m_next != NULL; m_tail = m_tail->m_next)
409 			;
410 
411 	if (flags & ICL_NOCOPY) {
412 		m = m_get(flags & ~ICL_NOCOPY, MT_DATA);
413 		if (m == NULL) {
414 			ICL_WARN("failed to allocate mbuf");
415 			return (ENOMEM);
416 		}
417 
418 		m->m_flags |= M_RDONLY;
419 		m_extaddref(m, __DECONST(char *, addr), len, &icp->ref_cnt,
420 		    icl_cxgbei_mbuf_done, icp, NULL);
421 		m->m_len = len;
422 		if (ip->ip_data_mbuf == NULL) {
423 			ip->ip_data_mbuf = m;
424 			ip->ip_data_len = len;
425 		} else {
426 			m_tail->m_next = m;
427 			m_tail = m_tail->m_next;
428 			ip->ip_data_len += len;
429 		}
430 
431 		return (0);
432 	}
433 
434 	src = (const char *)addr;
435 
436 	/* Allocate as jumbo mbufs of size MJUM16BYTES. */
437 	while (len >= MJUM16BYTES) {
438 		m = m_getjcl(M_NOWAIT, MT_DATA, 0, MJUM16BYTES);
439 		if (__predict_false(m == NULL)) {
440 			if ((flags & M_WAITOK) != 0) {
441 				/* Fall back to non-jumbo mbufs. */
442 				break;
443 			}
444 			return (ENOMEM);
445 		}
446 		memcpy(mtod(m, void *), src, MJUM16BYTES);
447 		m->m_len = MJUM16BYTES;
448 		if (ip->ip_data_mbuf == NULL) {
449 			ip->ip_data_mbuf = m_tail = m;
450 			ip->ip_data_len = MJUM16BYTES;
451 		} else {
452 			m_tail->m_next = m;
453 			m_tail = m_tail->m_next;
454 			ip->ip_data_len += MJUM16BYTES;
455 		}
456 		src += MJUM16BYTES;
457 		len -= MJUM16BYTES;
458 	}
459 
460 	/* Allocate mbuf chain for the remaining data. */
461 	if (len != 0) {
462 		m = m_getm2(NULL, len, flags, MT_DATA, 0);
463 		if (__predict_false(m == NULL))
464 			return (ENOMEM);
465 		if (ip->ip_data_mbuf == NULL) {
466 			ip->ip_data_mbuf = m;
467 			ip->ip_data_len = len;
468 		} else {
469 			m_tail->m_next = m;
470 			ip->ip_data_len += len;
471 		}
472 		for (; m != NULL; m = m->m_next) {
473 			m->m_len = min(len, M_SIZE(m));
474 			memcpy(mtod(m, void *), src, m->m_len);
475 			src += m->m_len;
476 			len -= m->m_len;
477 		}
478 		MPASS(len == 0);
479 	}
480 	MPASS(ip->ip_data_len <= ic->ic_max_send_data_segment_length);
481 
482 	return (0);
483 }
484 
485 void
486 icl_cxgbei_conn_pdu_get_data(struct icl_conn *ic, struct icl_pdu *ip,
487     size_t off, void *addr, size_t len)
488 {
489 	struct icl_cxgbei_pdu *icp = ip_to_icp(ip);
490 
491 	if (icp->icp_flags & ICPF_RX_DDP)
492 		return; /* data is DDP'ed, no need to copy */
493 	m_copydata(ip->ip_data_mbuf, off, len, addr);
494 }
495 
496 void
497 icl_cxgbei_conn_pdu_queue(struct icl_conn *ic, struct icl_pdu *ip)
498 {
499 	icl_cxgbei_conn_pdu_queue_cb(ic, ip, NULL);
500 }
501 
502 void
503 icl_cxgbei_conn_pdu_queue_cb(struct icl_conn *ic, struct icl_pdu *ip,
504 			     icl_pdu_cb cb)
505 {
506 	struct epoch_tracker et;
507 	struct icl_cxgbei_conn *icc = ic_to_icc(ic);
508 	struct icl_cxgbei_pdu *icp = ip_to_icp(ip);
509 	struct socket *so = ic->ic_socket;
510 	struct toepcb *toep = icc->toep;
511 	struct inpcb *inp;
512 	struct mbuf *m;
513 
514 	MPASS(ic == ip->ip_conn);
515 	MPASS(ip->ip_bhs_mbuf != NULL);
516 	/* The kernel doesn't generate PDUs with AHS. */
517 	MPASS(ip->ip_ahs_mbuf == NULL && ip->ip_ahs_len == 0);
518 
519 	ICL_CONN_LOCK_ASSERT(ic);
520 
521 	icp->cb = cb;
522 
523 	/* NOTE: sowriteable without so_snd lock is a mostly harmless race. */
524 	if (ic->ic_disconnecting || so == NULL || !sowriteable(so)) {
525 		icl_cxgbei_pdu_done(ip, ENOTCONN);
526 		return;
527 	}
528 
529 	m = finalize_pdu(icc, icp);
530 	M_ASSERTPKTHDR(m);
531 	MPASS((m->m_pkthdr.len & 3) == 0);
532 
533 	/*
534 	 * Do not get inp from toep->inp as the toepcb might have detached
535 	 * already.
536 	 */
537 	inp = sotoinpcb(so);
538 	CURVNET_SET(toep->vnet);
539 	NET_EPOCH_ENTER(et);
540 	INP_WLOCK(inp);
541 	if (__predict_false(inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT)) ||
542 	    __predict_false((toep->flags & TPF_ATTACHED) == 0))
543 		m_freem(m);
544 	else {
545 		mbufq_enqueue(&toep->ulp_pduq, m);
546 		t4_push_pdus(icc->sc, toep, 0);
547 	}
548 	INP_WUNLOCK(inp);
549 	NET_EPOCH_EXIT(et);
550 	CURVNET_RESTORE();
551 }
552 
553 static struct icl_conn *
554 icl_cxgbei_new_conn(const char *name, struct mtx *lock)
555 {
556 	struct icl_cxgbei_conn *icc;
557 	struct icl_conn *ic;
558 
559 	refcount_acquire(&icl_cxgbei_ncons);
560 
561 	icc = (struct icl_cxgbei_conn *)kobj_create(&icl_cxgbei_class, M_CXGBE,
562 	    M_WAITOK | M_ZERO);
563 	icc->icc_signature = CXGBEI_CONN_SIGNATURE;
564 	STAILQ_INIT(&icc->rcvd_pdus);
565 
566 	icc->cmp_table = hashinit(64, M_CXGBEI, &icc->cmp_hash_mask);
567 	mtx_init(&icc->cmp_lock, "cxgbei_cmp", NULL, MTX_DEF);
568 
569 	ic = &icc->ic;
570 	ic->ic_lock = lock;
571 
572 	/* XXXNP: review.  Most of these icl_conn fields aren't really used */
573 	STAILQ_INIT(&ic->ic_to_send);
574 	cv_init(&ic->ic_send_cv, "icl_cxgbei_tx");
575 	cv_init(&ic->ic_receive_cv, "icl_cxgbei_rx");
576 #ifdef DIAGNOSTIC
577 	refcount_init(&ic->ic_outstanding_pdus, 0);
578 #endif
579 	ic->ic_name = name;
580 	ic->ic_offload = "cxgbei";
581 	ic->ic_unmapped = false;
582 
583 	CTR2(KTR_CXGBE, "%s: icc %p", __func__, icc);
584 
585 	return (ic);
586 }
587 
588 void
589 icl_cxgbei_conn_free(struct icl_conn *ic)
590 {
591 	struct icl_cxgbei_conn *icc = ic_to_icc(ic);
592 
593 	MPASS(icc->icc_signature == CXGBEI_CONN_SIGNATURE);
594 
595 	CTR2(KTR_CXGBE, "%s: icc %p", __func__, icc);
596 
597 	cv_destroy(&ic->ic_send_cv);
598 	cv_destroy(&ic->ic_receive_cv);
599 
600 	mtx_destroy(&icc->cmp_lock);
601 	hashdestroy(icc->cmp_table, M_CXGBEI, icc->cmp_hash_mask);
602 	kobj_delete((struct kobj *)icc, M_CXGBE);
603 	refcount_release(&icl_cxgbei_ncons);
604 }
605 
606 static int
607 icl_cxgbei_setsockopt(struct icl_conn *ic, struct socket *so, int sspace,
608     int rspace)
609 {
610 	struct sockopt opt;
611 	int error, one = 1, ss, rs;
612 
613 	ss = max(sendspace, sspace);
614 	rs = max(recvspace, rspace);
615 
616 	error = soreserve(so, ss, rs);
617 	if (error != 0) {
618 		icl_cxgbei_conn_close(ic);
619 		return (error);
620 	}
621 	SOCKBUF_LOCK(&so->so_snd);
622 	so->so_snd.sb_flags |= SB_AUTOSIZE;
623 	SOCKBUF_UNLOCK(&so->so_snd);
624 	SOCKBUF_LOCK(&so->so_rcv);
625 	so->so_rcv.sb_flags |= SB_AUTOSIZE;
626 	SOCKBUF_UNLOCK(&so->so_rcv);
627 
628 	/*
629 	 * Disable Nagle.
630 	 */
631 	bzero(&opt, sizeof(opt));
632 	opt.sopt_dir = SOPT_SET;
633 	opt.sopt_level = IPPROTO_TCP;
634 	opt.sopt_name = TCP_NODELAY;
635 	opt.sopt_val = &one;
636 	opt.sopt_valsize = sizeof(one);
637 	error = sosetopt(so, &opt);
638 	if (error != 0) {
639 		icl_cxgbei_conn_close(ic);
640 		return (error);
641 	}
642 
643 	return (0);
644 }
645 
646 /*
647  * Request/response structure used to find out the adapter offloading a socket.
648  */
649 struct find_ofld_adapter_rr {
650 	struct socket *so;
651 	struct adapter *sc;	/* result */
652 };
653 
654 static void
655 find_offload_adapter(struct adapter *sc, void *arg)
656 {
657 	struct find_ofld_adapter_rr *fa = arg;
658 	struct socket *so = fa->so;
659 	struct tom_data *td = sc->tom_softc;
660 	struct tcpcb *tp;
661 	struct inpcb *inp;
662 
663 	/* Non-TCP were filtered out earlier. */
664 	MPASS(so->so_proto->pr_protocol == IPPROTO_TCP);
665 
666 	if (fa->sc != NULL)
667 		return;	/* Found already. */
668 
669 	if (td == NULL)
670 		return;	/* TOE not enabled on this adapter. */
671 
672 	inp = sotoinpcb(so);
673 	INP_WLOCK(inp);
674 	if ((inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT)) == 0) {
675 		tp = intotcpcb(inp);
676 		if (tp->t_flags & TF_TOE && tp->tod == &td->tod)
677 			fa->sc = sc;	/* Found. */
678 	}
679 	INP_WUNLOCK(inp);
680 }
681 
682 /* XXXNP: move this to t4_tom. */
683 static void
684 send_iscsi_flowc_wr(struct adapter *sc, struct toepcb *toep, int maxlen)
685 {
686 	struct wrqe *wr;
687 	struct fw_flowc_wr *flowc;
688 	const u_int nparams = 1;
689 	u_int flowclen;
690 	struct ofld_tx_sdesc *txsd = &toep->txsd[toep->txsd_pidx];
691 
692 	flowclen = sizeof(*flowc) + nparams * sizeof(struct fw_flowc_mnemval);
693 
694 	wr = alloc_wrqe(roundup2(flowclen, 16), &toep->ofld_txq->wrq);
695 	if (wr == NULL) {
696 		/* XXX */
697 		panic("%s: allocation failure.", __func__);
698 	}
699 	flowc = wrtod(wr);
700 	memset(flowc, 0, wr->wr_len);
701 
702 	flowc->op_to_nparams = htobe32(V_FW_WR_OP(FW_FLOWC_WR) |
703 	    V_FW_FLOWC_WR_NPARAMS(nparams));
704 	flowc->flowid_len16 = htonl(V_FW_WR_LEN16(howmany(flowclen, 16)) |
705 	    V_FW_WR_FLOWID(toep->tid));
706 
707 	flowc->mnemval[0].mnemonic = FW_FLOWC_MNEM_TXDATAPLEN_MAX;
708 	flowc->mnemval[0].val = htobe32(maxlen);
709 
710 	txsd->tx_credits = howmany(flowclen, 16);
711 	txsd->plen = 0;
712 	KASSERT(toep->tx_credits >= txsd->tx_credits && toep->txsd_avail > 0,
713 	    ("%s: not enough credits (%d)", __func__, toep->tx_credits));
714 	toep->tx_credits -= txsd->tx_credits;
715 	if (__predict_false(++toep->txsd_pidx == toep->txsd_total))
716 		toep->txsd_pidx = 0;
717 	toep->txsd_avail--;
718 
719 	t4_wrq_tx(sc, wr);
720 }
721 
722 static void
723 set_ulp_mode_iscsi(struct adapter *sc, struct toepcb *toep, u_int ulp_submode)
724 {
725 	uint64_t val;
726 
727 	CTR3(KTR_CXGBE, "%s: tid %u, ULP_MODE_ISCSI, submode=%#x",
728 	    __func__, toep->tid, ulp_submode);
729 
730 	val = V_TCB_ULP_TYPE(ULP_MODE_ISCSI) | V_TCB_ULP_RAW(ulp_submode);
731 	t4_set_tcb_field(sc, toep->ctrlq, toep, W_TCB_ULP_TYPE,
732 	    V_TCB_ULP_TYPE(M_TCB_ULP_TYPE) | V_TCB_ULP_RAW(M_TCB_ULP_RAW), val,
733 	    0, 0);
734 
735 	val = V_TF_RX_FLOW_CONTROL_DISABLE(1ULL);
736 	t4_set_tcb_field(sc, toep->ctrlq, toep, W_TCB_T_FLAGS, val, val, 0, 0);
737 }
738 
739 /*
740  * XXXNP: Who is responsible for cleaning up the socket if this returns with an
741  * error?  Review all error paths.
742  *
743  * XXXNP: What happens to the socket's fd reference if the operation is
744  * successful, and how does that affect the socket's life cycle?
745  */
746 int
747 icl_cxgbei_conn_handoff(struct icl_conn *ic, int fd)
748 {
749 	struct icl_cxgbei_conn *icc = ic_to_icc(ic);
750 	struct cxgbei_data *ci;
751 	struct find_ofld_adapter_rr fa;
752 	struct file *fp;
753 	struct socket *so;
754 	struct inpcb *inp;
755 	struct tcpcb *tp;
756 	struct toepcb *toep;
757 	cap_rights_t rights;
758 	int error;
759 
760 	MPASS(icc->icc_signature == CXGBEI_CONN_SIGNATURE);
761 	ICL_CONN_LOCK_ASSERT_NOT(ic);
762 
763 	/*
764 	 * Steal the socket from userland.
765 	 */
766 	error = fget(curthread, fd,
767 	    cap_rights_init_one(&rights, CAP_SOCK_CLIENT), &fp);
768 	if (error != 0)
769 		return (error);
770 	if (fp->f_type != DTYPE_SOCKET) {
771 		fdrop(fp, curthread);
772 		return (EINVAL);
773 	}
774 	so = fp->f_data;
775 	if (so->so_type != SOCK_STREAM ||
776 	    so->so_proto->pr_protocol != IPPROTO_TCP) {
777 		fdrop(fp, curthread);
778 		return (EINVAL);
779 	}
780 
781 	ICL_CONN_LOCK(ic);
782 	if (ic->ic_socket != NULL) {
783 		ICL_CONN_UNLOCK(ic);
784 		fdrop(fp, curthread);
785 		return (EBUSY);
786 	}
787 	ic->ic_disconnecting = false;
788 	ic->ic_socket = so;
789 	fp->f_ops = &badfileops;
790 	fp->f_data = NULL;
791 	fdrop(fp, curthread);
792 	ICL_CONN_UNLOCK(ic);
793 
794 	/* Find the adapter offloading this socket. */
795 	fa.sc = NULL;
796 	fa.so = so;
797 	t4_iterate(find_offload_adapter, &fa);
798 	if (fa.sc == NULL)
799 		return (EINVAL);
800 	icc->sc = fa.sc;
801 	ci = icc->sc->iscsi_ulp_softc;
802 
803 	inp = sotoinpcb(so);
804 	INP_WLOCK(inp);
805 	tp = intotcpcb(inp);
806 	if (inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT))
807 		error = EBUSY;
808 	else {
809 		/*
810 		 * socket could not have been "unoffloaded" if here.
811 		 */
812 		MPASS(tp->t_flags & TF_TOE);
813 		MPASS(tp->tod != NULL);
814 		MPASS(tp->t_toe != NULL);
815 		toep = tp->t_toe;
816 		MPASS(toep->vi->adapter == icc->sc);
817 		icc->toep = toep;
818 		icc->cwt = cxgbei_select_worker_thread(icc);
819 
820 		icc->ulp_submode = 0;
821 		if (ic->ic_header_crc32c)
822 			icc->ulp_submode |= ULP_CRC_HEADER;
823 		if (ic->ic_data_crc32c)
824 			icc->ulp_submode |= ULP_CRC_DATA;
825 		so->so_options |= SO_NO_DDP;
826 		toep->params.ulp_mode = ULP_MODE_ISCSI;
827 		toep->ulpcb = icc;
828 
829 		send_iscsi_flowc_wr(icc->sc, toep, roundup(ci->max_tx_pdu_len,
830 		    tp->t_maxseg));
831 		set_ulp_mode_iscsi(icc->sc, toep, icc->ulp_submode);
832 		error = 0;
833 	}
834 	INP_WUNLOCK(inp);
835 
836 	if (error == 0) {
837 		error = icl_cxgbei_setsockopt(ic, so, ci->max_tx_pdu_len,
838 		    ci->max_rx_pdu_len);
839 	}
840 
841 	return (error);
842 }
843 
844 void
845 icl_cxgbei_conn_close(struct icl_conn *ic)
846 {
847 	struct icl_cxgbei_conn *icc = ic_to_icc(ic);
848 	struct icl_pdu *ip;
849 	struct socket *so;
850 	struct sockbuf *sb;
851 	struct inpcb *inp;
852 	struct toepcb *toep = icc->toep;
853 
854 	MPASS(icc->icc_signature == CXGBEI_CONN_SIGNATURE);
855 	ICL_CONN_LOCK_ASSERT_NOT(ic);
856 
857 	ICL_CONN_LOCK(ic);
858 	so = ic->ic_socket;
859 	if (ic->ic_disconnecting || so == NULL) {
860 		CTR4(KTR_CXGBE, "%s: icc %p (disconnecting = %d), so %p",
861 		    __func__, icc, ic->ic_disconnecting, so);
862 		ICL_CONN_UNLOCK(ic);
863 		return;
864 	}
865 	ic->ic_disconnecting = true;
866 
867 	/* These are unused in this driver right now. */
868 	MPASS(STAILQ_EMPTY(&ic->ic_to_send));
869 	MPASS(ic->ic_receive_pdu == NULL);
870 
871 #ifdef DIAGNOSTIC
872 	KASSERT(ic->ic_outstanding_pdus == 0,
873 	    ("destroying session with %d outstanding PDUs",
874 	     ic->ic_outstanding_pdus));
875 #endif
876 	ICL_CONN_UNLOCK(ic);
877 
878 	CTR3(KTR_CXGBE, "%s: tid %d, icc %p", __func__, toep ? toep->tid : -1,
879 	    icc);
880 	inp = sotoinpcb(so);
881 	sb = &so->so_rcv;
882 	INP_WLOCK(inp);
883 	if (toep != NULL) {	/* NULL if connection was never offloaded. */
884 		toep->ulpcb = NULL;
885 
886 		/* Discard PDUs queued for TX. */
887 		mbufq_drain(&toep->ulp_pduq);
888 
889 		/*
890 		 * Wait for the cwt threads to stop processing this
891 		 * connection.
892 		 */
893 		SOCKBUF_LOCK(sb);
894 		if (icc->rx_flags & RXF_ACTIVE) {
895 			volatile u_int *p = &icc->rx_flags;
896 
897 			SOCKBUF_UNLOCK(sb);
898 			INP_WUNLOCK(inp);
899 
900 			while (*p & RXF_ACTIVE)
901 				pause("conclo", 1);
902 
903 			INP_WLOCK(inp);
904 			SOCKBUF_LOCK(sb);
905 		}
906 
907 		/*
908 		 * Discard received PDUs not passed to the iSCSI
909 		 * layer.
910 		 */
911 		while (!STAILQ_EMPTY(&icc->rcvd_pdus)) {
912 			ip = STAILQ_FIRST(&icc->rcvd_pdus);
913 			STAILQ_REMOVE_HEAD(&icc->rcvd_pdus, ip_next);
914 			icl_cxgbei_pdu_done(ip, ENOTCONN);
915 		}
916 		SOCKBUF_UNLOCK(sb);
917 	}
918 	INP_WUNLOCK(inp);
919 
920 	ICL_CONN_LOCK(ic);
921 	ic->ic_socket = NULL;
922 	ICL_CONN_UNLOCK(ic);
923 
924 	/*
925 	 * XXXNP: we should send RST instead of FIN when PDUs held in various
926 	 * queues were purged instead of delivered reliably but soabort isn't
927 	 * really general purpose and wouldn't do the right thing here.
928 	 */
929 	soref(so);
930 	soclose(so);
931 
932 	/*
933 	 * Wait for the socket to fully close.  This ensures any
934 	 * pending received data has been received (and in particular,
935 	 * any data that would be received by DDP has been handled).
936 	 * Callers assume that it is safe to free buffers for tasks
937 	 * and transfers after this function returns.
938 	 */
939 	SOCK_LOCK(so);
940 	while ((so->so_state & SS_ISDISCONNECTED) == 0)
941 		mtx_sleep(&so->so_timeo, SOCK_MTX(so), PSOCK, "conclo2", 0);
942 	CURVNET_SET(so->so_vnet);
943 	sorele(so);
944 	CURVNET_RESTORE();
945 }
946 
947 static void
948 cxgbei_insert_cmp(struct icl_cxgbei_conn *icc, struct cxgbei_cmp *cmp,
949     uint32_t tt)
950 {
951 #ifdef INVARIANTS
952 	struct cxgbei_cmp *cmp2;
953 #endif
954 
955 	cmp->tt = tt;
956 
957 	mtx_lock(&icc->cmp_lock);
958 #ifdef INVARIANTS
959 	LIST_FOREACH(cmp2, &icc->cmp_table[TT_HASH(icc, tt)], link) {
960 		KASSERT(cmp2->tt != tt, ("%s: duplicate cmp", __func__));
961 	}
962 #endif
963 	LIST_INSERT_HEAD(&icc->cmp_table[TT_HASH(icc, tt)], cmp, link);
964 	mtx_unlock(&icc->cmp_lock);
965 }
966 
967 struct cxgbei_cmp *
968 cxgbei_find_cmp(struct icl_cxgbei_conn *icc, uint32_t tt)
969 {
970 	struct cxgbei_cmp *cmp;
971 
972 	mtx_lock(&icc->cmp_lock);
973 	LIST_FOREACH(cmp, &icc->cmp_table[TT_HASH(icc, tt)], link) {
974 		if (cmp->tt == tt)
975 			break;
976 	}
977 	mtx_unlock(&icc->cmp_lock);
978 	return (cmp);
979 }
980 
981 static void
982 cxgbei_rm_cmp(struct icl_cxgbei_conn *icc, struct cxgbei_cmp *cmp)
983 {
984 #ifdef INVARIANTS
985 	struct cxgbei_cmp *cmp2;
986 #endif
987 
988 	mtx_lock(&icc->cmp_lock);
989 
990 #ifdef INVARIANTS
991 	LIST_FOREACH(cmp2, &icc->cmp_table[TT_HASH(icc, cmp->tt)], link) {
992 		if (cmp2 == cmp)
993 			goto found;
994 	}
995 	panic("%s: could not find cmp", __func__);
996 found:
997 #endif
998 	LIST_REMOVE(cmp, link);
999 	mtx_unlock(&icc->cmp_lock);
1000 }
1001 
1002 int
1003 icl_cxgbei_conn_task_setup(struct icl_conn *ic, struct icl_pdu *ip,
1004     struct ccb_scsiio *csio, uint32_t *ittp, void **arg)
1005 {
1006 	struct icl_cxgbei_conn *icc = ic_to_icc(ic);
1007 	struct toepcb *toep = icc->toep;
1008 	struct adapter *sc = icc->sc;
1009 	struct cxgbei_data *ci = sc->iscsi_ulp_softc;
1010 	struct ppod_region *pr = &ci->pr;
1011 	struct cxgbei_ddp_state *ddp;
1012 	struct ppod_reservation *prsv;
1013 	struct inpcb *inp;
1014 	struct mbufq mq;
1015 	uint32_t itt;
1016 	int rc = 0;
1017 
1018 	ICL_CONN_LOCK_ASSERT(ic);
1019 
1020 	/* This is for the offload driver's state.  Must not be set already. */
1021 	MPASS(arg != NULL);
1022 	MPASS(*arg == NULL);
1023 
1024 	if (ic->ic_disconnecting || ic->ic_socket == NULL)
1025 		return (ECONNRESET);
1026 
1027 	if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_IN ||
1028 	    csio->dxfer_len < ci->ddp_threshold) {
1029 no_ddp:
1030 		/*
1031 		 * No DDP for this I/O.	 Allocate an ITT (based on the one
1032 		 * passed in) that cannot be a valid hardware DDP tag in the
1033 		 * iSCSI region.
1034 		 */
1035 		itt = *ittp & M_PPOD_TAG;
1036 		itt = V_PPOD_TAG(itt) | pr->pr_invalid_bit;
1037 		*ittp = htobe32(itt);
1038 		MPASS(*arg == NULL);	/* State is maintained for DDP only. */
1039 		if (rc != 0)
1040 			counter_u64_add(
1041 			    toep->ofld_rxq->rx_iscsi_ddp_setup_error, 1);
1042 		return (0);
1043 	}
1044 
1045 	/*
1046 	 * Reserve resources for DDP, update the itt that should be used in the
1047 	 * PDU, and save DDP specific state for this I/O in *arg.
1048 	 */
1049 	ddp = malloc(sizeof(*ddp), M_CXGBEI, M_NOWAIT | M_ZERO);
1050 	if (ddp == NULL) {
1051 		rc = ENOMEM;
1052 		goto no_ddp;
1053 	}
1054 	prsv = &ddp->prsv;
1055 
1056 	/* XXX add support for all CAM_DATA_ types */
1057 	MPASS((csio->ccb_h.flags & CAM_DATA_MASK) == CAM_DATA_VADDR);
1058 	rc = t4_alloc_page_pods_for_buf(pr, (vm_offset_t)csio->data_ptr,
1059 	    csio->dxfer_len, prsv);
1060 	if (rc != 0) {
1061 		free(ddp, M_CXGBEI);
1062 		goto no_ddp;
1063 	}
1064 
1065 	mbufq_init(&mq, INT_MAX);
1066 	rc = t4_write_page_pods_for_buf(sc, toep, prsv,
1067 	    (vm_offset_t)csio->data_ptr, csio->dxfer_len, &mq);
1068 	if (__predict_false(rc != 0)) {
1069 		mbufq_drain(&mq);
1070 		t4_free_page_pods(prsv);
1071 		free(ddp, M_CXGBEI);
1072 		goto no_ddp;
1073 	}
1074 
1075 	/*
1076 	 * Do not get inp from toep->inp as the toepcb might have
1077 	 * detached already.
1078 	 */
1079 	inp = sotoinpcb(ic->ic_socket);
1080 	INP_WLOCK(inp);
1081 	if ((inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT)) != 0) {
1082 		INP_WUNLOCK(inp);
1083 		mbufq_drain(&mq);
1084 		t4_free_page_pods(prsv);
1085 		free(ddp, M_CXGBEI);
1086 		return (ECONNRESET);
1087 	}
1088 	mbufq_concat(&toep->ulp_pduq, &mq);
1089 	INP_WUNLOCK(inp);
1090 
1091 	ddp->cmp.last_datasn = -1;
1092 	cxgbei_insert_cmp(icc, &ddp->cmp, prsv->prsv_tag);
1093 	*ittp = htobe32(prsv->prsv_tag);
1094 	*arg = prsv;
1095 	counter_u64_add(toep->ofld_rxq->rx_iscsi_ddp_setup_ok, 1);
1096 	return (0);
1097 }
1098 
1099 void
1100 icl_cxgbei_conn_task_done(struct icl_conn *ic, void *arg)
1101 {
1102 
1103 	if (arg != NULL) {
1104 		struct cxgbei_ddp_state *ddp = arg;
1105 
1106 		cxgbei_rm_cmp(ic_to_icc(ic), &ddp->cmp);
1107 		t4_free_page_pods(&ddp->prsv);
1108 		free(ddp, M_CXGBEI);
1109 	}
1110 }
1111 
1112 static inline bool
1113 ddp_sgl_check(struct ctl_sg_entry *sg, int entries, int xferlen)
1114 {
1115 	int total_len = 0;
1116 
1117 	MPASS(entries > 0);
1118 	if (((vm_offset_t)sg[--entries].addr & 3U) != 0)
1119 		return (false);
1120 
1121 	total_len += sg[entries].len;
1122 
1123 	while (--entries >= 0) {
1124 		if (((vm_offset_t)sg[entries].addr & PAGE_MASK) != 0 ||
1125 		    (sg[entries].len % PAGE_SIZE) != 0)
1126 			return (false);
1127 		total_len += sg[entries].len;
1128 	}
1129 
1130 	MPASS(total_len == xferlen);
1131 	return (true);
1132 }
1133 
1134 /* XXXNP: PDU should be passed in as parameter, like on the initiator. */
1135 #define io_to_request_pdu(io) ((io)->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr)
1136 #define io_to_ddp_state(io) ((io)->io_hdr.ctl_private[CTL_PRIV_FRONTEND2].ptr)
1137 
1138 int
1139 icl_cxgbei_conn_transfer_setup(struct icl_conn *ic, union ctl_io *io,
1140     uint32_t *tttp, void **arg)
1141 {
1142 	struct icl_cxgbei_conn *icc = ic_to_icc(ic);
1143 	struct toepcb *toep = icc->toep;
1144 	struct ctl_scsiio *ctsio = &io->scsiio;
1145 	struct adapter *sc = icc->sc;
1146 	struct cxgbei_data *ci = sc->iscsi_ulp_softc;
1147 	struct ppod_region *pr = &ci->pr;
1148 	struct cxgbei_ddp_state *ddp;
1149 	struct ppod_reservation *prsv;
1150 	struct ctl_sg_entry *sgl, sg_entry;
1151 	struct inpcb *inp;
1152 	struct mbufq mq;
1153 	int sg_entries = ctsio->kern_sg_entries;
1154 	uint32_t ttt;
1155 	int xferlen, rc = 0, alias;
1156 
1157 	/* This is for the offload driver's state.  Must not be set already. */
1158 	MPASS(arg != NULL);
1159 	MPASS(*arg == NULL);
1160 
1161 	if (ctsio->ext_data_filled == 0) {
1162 		int first_burst;
1163 		struct icl_pdu *ip = io_to_request_pdu(io);
1164 #ifdef INVARIANTS
1165 		struct icl_cxgbei_pdu *icp = ip_to_icp(ip);
1166 
1167 		MPASS(icp->icp_signature == CXGBEI_PDU_SIGNATURE);
1168 		MPASS(ic == ip->ip_conn);
1169 		MPASS(ip->ip_bhs_mbuf != NULL);
1170 #endif
1171 		first_burst = icl_pdu_data_segment_length(ip);
1172 
1173 		/*
1174 		 * Note that ICL calls conn_transfer_setup even if the first
1175 		 * burst had everything and there's nothing left to transfer.
1176 		 *
1177 		 * NB: The CTL frontend might have provided a buffer
1178 		 * whose length (kern_data_len) is smaller than the
1179 		 * FirstBurstLength of unsolicited data.  Treat those
1180 		 * as an empty transfer.
1181 		 */
1182 		xferlen = ctsio->kern_data_len;
1183 		if (xferlen < first_burst ||
1184 		    xferlen - first_burst < ci->ddp_threshold) {
1185 no_ddp:
1186 			/*
1187 			 * No DDP for this transfer.  Allocate a TTT (based on
1188 			 * the one passed in) that cannot be a valid hardware
1189 			 * DDP tag in the iSCSI region.
1190 			 */
1191 			ttt = *tttp & M_PPOD_TAG;
1192 			ttt = V_PPOD_TAG(ttt) | pr->pr_invalid_bit;
1193 			*tttp = htobe32(ttt);
1194 			MPASS(io_to_ddp_state(io) == NULL);
1195 			if (rc != 0)
1196 				counter_u64_add(
1197 				    toep->ofld_rxq->rx_iscsi_ddp_setup_error, 1);
1198 			return (0);
1199 		}
1200 
1201 		if (sg_entries == 0) {
1202 			sgl = &sg_entry;
1203 			sgl->len = xferlen;
1204 			sgl->addr = (void *)ctsio->kern_data_ptr;
1205 			sg_entries = 1;
1206 		} else
1207 			sgl = (void *)ctsio->kern_data_ptr;
1208 
1209 		if (!ddp_sgl_check(sgl, sg_entries, xferlen))
1210 			goto no_ddp;
1211 
1212 		/*
1213 		 * Reserve resources for DDP, update the ttt that should be used
1214 		 * in the PDU, and save DDP specific state for this I/O.
1215 		 */
1216 		MPASS(io_to_ddp_state(io) == NULL);
1217 		ddp = malloc(sizeof(*ddp), M_CXGBEI, M_NOWAIT | M_ZERO);
1218 		if (ddp == NULL) {
1219 			rc = ENOMEM;
1220 			goto no_ddp;
1221 		}
1222 		prsv = &ddp->prsv;
1223 
1224 		rc = t4_alloc_page_pods_for_sgl(pr, sgl, sg_entries, prsv);
1225 		if (rc != 0) {
1226 			free(ddp, M_CXGBEI);
1227 			goto no_ddp;
1228 		}
1229 
1230 		mbufq_init(&mq, INT_MAX);
1231 		rc = t4_write_page_pods_for_sgl(sc, toep, prsv, sgl, sg_entries,
1232 		    xferlen, &mq);
1233 		if (__predict_false(rc != 0)) {
1234 			mbufq_drain(&mq);
1235 			t4_free_page_pods(prsv);
1236 			free(ddp, M_CXGBEI);
1237 			goto no_ddp;
1238 		}
1239 
1240 		/*
1241 		 * Do not get inp from toep->inp as the toepcb might
1242 		 * have detached already.
1243 		 */
1244 		ICL_CONN_LOCK(ic);
1245 		if (ic->ic_disconnecting || ic->ic_socket == NULL) {
1246 			ICL_CONN_UNLOCK(ic);
1247 			mbufq_drain(&mq);
1248 			t4_free_page_pods(prsv);
1249 			free(ddp, M_CXGBEI);
1250 			return (ECONNRESET);
1251 		}
1252 		inp = sotoinpcb(ic->ic_socket);
1253 		INP_WLOCK(inp);
1254 		ICL_CONN_UNLOCK(ic);
1255 		if ((inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT)) != 0) {
1256 			INP_WUNLOCK(inp);
1257 			mbufq_drain(&mq);
1258 			t4_free_page_pods(prsv);
1259 			free(ddp, M_CXGBEI);
1260 			return (ECONNRESET);
1261 		}
1262 		mbufq_concat(&toep->ulp_pduq, &mq);
1263 		INP_WUNLOCK(inp);
1264 
1265 		ddp->cmp.next_buffer_offset = ctsio->kern_rel_offset +
1266 		    first_burst;
1267 		ddp->cmp.last_datasn = -1;
1268 		cxgbei_insert_cmp(icc, &ddp->cmp, prsv->prsv_tag);
1269 		*tttp = htobe32(prsv->prsv_tag);
1270 		io_to_ddp_state(io) = ddp;
1271 		*arg = ctsio;
1272 		counter_u64_add(toep->ofld_rxq->rx_iscsi_ddp_setup_ok, 1);
1273 		return (0);
1274 	}
1275 
1276 	/*
1277 	 * In the middle of an I/O.  A non-NULL page pod reservation indicates
1278 	 * that a DDP buffer is being used for the I/O.
1279 	 */
1280 	ddp = io_to_ddp_state(ctsio);
1281 	if (ddp == NULL)
1282 		goto no_ddp;
1283 	prsv = &ddp->prsv;
1284 
1285 	alias = (prsv->prsv_tag & pr->pr_alias_mask) >> pr->pr_alias_shift;
1286 	alias++;
1287 	prsv->prsv_tag &= ~pr->pr_alias_mask;
1288 	prsv->prsv_tag |= alias << pr->pr_alias_shift & pr->pr_alias_mask;
1289 
1290 	ddp->cmp.next_datasn = 0;
1291 	ddp->cmp.last_datasn = -1;
1292 	cxgbei_insert_cmp(icc, &ddp->cmp, prsv->prsv_tag);
1293 	*tttp = htobe32(prsv->prsv_tag);
1294 	*arg = ctsio;
1295 
1296 	return (0);
1297 }
1298 
1299 void
1300 icl_cxgbei_conn_transfer_done(struct icl_conn *ic, void *arg)
1301 {
1302 	struct ctl_scsiio *ctsio = arg;
1303 
1304 	if (ctsio != NULL) {
1305 		struct cxgbei_ddp_state *ddp;
1306 
1307 		ddp = io_to_ddp_state(ctsio);
1308 		MPASS(ddp != NULL);
1309 
1310 		cxgbei_rm_cmp(ic_to_icc(ic), &ddp->cmp);
1311 		if (ctsio->kern_data_len == ctsio->ext_data_filled ||
1312 		    ic->ic_disconnecting) {
1313 			t4_free_page_pods(&ddp->prsv);
1314 			free(ddp, M_CXGBEI);
1315 			io_to_ddp_state(ctsio) = NULL;
1316 		}
1317 	}
1318 }
1319 
1320 static void
1321 cxgbei_limits(struct adapter *sc, void *arg)
1322 {
1323 	struct icl_drv_limits *idl = arg;
1324 	struct cxgbei_data *ci;
1325 	int max_dsl;
1326 
1327 	if (begin_synchronized_op(sc, NULL, HOLD_LOCK, "t4lims") != 0)
1328 		return;
1329 
1330 	if (uld_active(sc, ULD_ISCSI)) {
1331 		ci = sc->iscsi_ulp_softc;
1332 		MPASS(ci != NULL);
1333 
1334 		/*
1335 		 * AHS is not supported by the kernel so we'll not account for
1336 		 * it either in our PDU len -> data segment len conversions.
1337 		 */
1338 
1339 		max_dsl = ci->max_rx_pdu_len - ISCSI_BHS_SIZE -
1340 		    ISCSI_HEADER_DIGEST_SIZE - ISCSI_DATA_DIGEST_SIZE;
1341 		if (idl->idl_max_recv_data_segment_length > max_dsl)
1342 			idl->idl_max_recv_data_segment_length = max_dsl;
1343 
1344 		max_dsl = ci->max_tx_pdu_len - ISCSI_BHS_SIZE -
1345 		    ISCSI_HEADER_DIGEST_SIZE - ISCSI_DATA_DIGEST_SIZE;
1346 		if (idl->idl_max_send_data_segment_length > max_dsl)
1347 			idl->idl_max_send_data_segment_length = max_dsl;
1348 	}
1349 
1350 	end_synchronized_op(sc, LOCK_HELD);
1351 }
1352 
1353 static int
1354 icl_cxgbei_limits(struct icl_drv_limits *idl)
1355 {
1356 
1357 	/* Maximum allowed by the RFC.	cxgbei_limits will clip them. */
1358 	idl->idl_max_recv_data_segment_length = (1 << 24) - 1;
1359 	idl->idl_max_send_data_segment_length = (1 << 24) - 1;
1360 
1361 	/* These are somewhat arbitrary. */
1362 	idl->idl_max_burst_length = max_burst_length;
1363 	idl->idl_first_burst_length = first_burst_length;
1364 
1365 	t4_iterate(cxgbei_limits, idl);
1366 
1367 	return (0);
1368 }
1369 
1370 int
1371 icl_cxgbei_mod_load(void)
1372 {
1373 	int rc;
1374 
1375 	refcount_init(&icl_cxgbei_ncons, 0);
1376 
1377 	rc = icl_register("cxgbei", false, -100, icl_cxgbei_limits,
1378 	    icl_cxgbei_new_conn);
1379 
1380 	return (rc);
1381 }
1382 
1383 int
1384 icl_cxgbei_mod_unload(void)
1385 {
1386 
1387 	if (icl_cxgbei_ncons != 0)
1388 		return (EBUSY);
1389 
1390 	icl_unregister("cxgbei", false);
1391 
1392 	return (0);
1393 }
1394 #endif
1395