xref: /freebsd-13-stable/sys/dev/cxgbe/tom/t4_tom.c (revision 7f864af3e648fc3fcc27d3f2b840f111deac1600)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2012 Chelsio Communications, Inc.
5  * All rights reserved.
6  * Written by: Navdeep Parhar <np@FreeBSD.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 #include "opt_inet.h"
32 #include "opt_inet6.h"
33 #include "opt_kern_tls.h"
34 #include "opt_ratelimit.h"
35 
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/ktr.h>
41 #include <sys/lock.h>
42 #include <sys/limits.h>
43 #include <sys/module.h>
44 #include <sys/protosw.h>
45 #include <sys/domain.h>
46 #include <sys/refcount.h>
47 #include <sys/rmlock.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/sysctl.h>
51 #include <sys/taskqueue.h>
52 #include <net/if.h>
53 #include <net/if_var.h>
54 #include <net/if_types.h>
55 #include <net/if_vlan_var.h>
56 #include <netinet/in.h>
57 #include <netinet/in_pcb.h>
58 #include <netinet/in_var.h>
59 #include <netinet/ip.h>
60 #include <netinet/ip6.h>
61 #include <netinet6/scope6_var.h>
62 #define TCPSTATES
63 #include <netinet/tcp_fsm.h>
64 #include <netinet/tcp_timer.h>
65 #include <netinet/tcp_var.h>
66 #include <netinet/toecore.h>
67 #include <netinet/cc/cc.h>
68 
69 #ifdef TCP_OFFLOAD
70 #include "common/common.h"
71 #include "common/t4_msg.h"
72 #include "common/t4_regs.h"
73 #include "common/t4_regs_values.h"
74 #include "common/t4_tcb.h"
75 #include "t4_clip.h"
76 #include "tom/t4_tom_l2t.h"
77 #include "tom/t4_tom.h"
78 #include "tom/t4_tls.h"
79 
80 static struct protosw *tcp_protosw;
81 static struct protosw toe_protosw;
82 static struct pr_usrreqs toe_usrreqs;
83 
84 static struct protosw *tcp6_protosw;
85 static struct protosw toe6_protosw;
86 static struct pr_usrreqs toe6_usrreqs;
87 
88 /* Module ops */
89 static int t4_tom_mod_load(void);
90 static int t4_tom_mod_unload(void);
91 static int t4_tom_modevent(module_t, int, void *);
92 
93 /* ULD ops and helpers */
94 static int t4_tom_activate(struct adapter *);
95 static int t4_tom_deactivate(struct adapter *);
96 
97 static struct uld_info tom_uld_info = {
98 	.uld_id = ULD_TOM,
99 	.activate = t4_tom_activate,
100 	.deactivate = t4_tom_deactivate,
101 };
102 
103 static void release_offload_resources(struct toepcb *);
104 static int alloc_tid_tabs(struct tid_info *);
105 static void free_tid_tabs(struct tid_info *);
106 static void free_tom_data(struct adapter *, struct tom_data *);
107 static void reclaim_wr_resources(void *, int);
108 
109 struct toepcb *
alloc_toepcb(struct vi_info * vi,int flags)110 alloc_toepcb(struct vi_info *vi, int flags)
111 {
112 	struct port_info *pi = vi->pi;
113 	struct adapter *sc = pi->adapter;
114 	struct toepcb *toep;
115 	int tx_credits, txsd_total, len;
116 
117 	/*
118 	 * The firmware counts tx work request credits in units of 16 bytes
119 	 * each.  Reserve room for an ABORT_REQ so the driver never has to worry
120 	 * about tx credits if it wants to abort a connection.
121 	 */
122 	tx_credits = sc->params.ofldq_wr_cred;
123 	tx_credits -= howmany(sizeof(struct cpl_abort_req), 16);
124 
125 	/*
126 	 * Shortest possible tx work request is a fw_ofld_tx_data_wr + 1 byte
127 	 * immediate payload, and firmware counts tx work request credits in
128 	 * units of 16 byte.  Calculate the maximum work requests possible.
129 	 */
130 	txsd_total = tx_credits /
131 	    howmany(sizeof(struct fw_ofld_tx_data_wr) + 1, 16);
132 
133 	len = offsetof(struct toepcb, txsd) +
134 	    txsd_total * sizeof(struct ofld_tx_sdesc);
135 
136 	toep = malloc(len, M_CXGBE, M_ZERO | flags);
137 	if (toep == NULL)
138 		return (NULL);
139 
140 	refcount_init(&toep->refcount, 1);
141 	toep->td = sc->tom_softc;
142 	toep->vi = vi;
143 	toep->tid = -1;
144 	toep->tx_total = tx_credits;
145 	toep->tx_credits = tx_credits;
146 	mbufq_init(&toep->ulp_pduq, INT_MAX);
147 	mbufq_init(&toep->ulp_pdu_reclaimq, INT_MAX);
148 	toep->txsd_total = txsd_total;
149 	toep->txsd_avail = txsd_total;
150 	toep->txsd_pidx = 0;
151 	toep->txsd_cidx = 0;
152 	aiotx_init_toep(toep);
153 
154 	return (toep);
155 }
156 
157 /*
158  * Initialize a toepcb after its params have been filled out.
159  */
160 int
init_toepcb(struct vi_info * vi,struct toepcb * toep)161 init_toepcb(struct vi_info *vi, struct toepcb *toep)
162 {
163 	struct conn_params *cp = &toep->params;
164 	struct port_info *pi = vi->pi;
165 	struct adapter *sc = pi->adapter;
166 	struct tx_cl_rl_params *tc;
167 
168 	if (cp->tc_idx >= 0 && cp->tc_idx < sc->params.nsched_cls) {
169 		tc = &pi->sched_params->cl_rl[cp->tc_idx];
170 		mtx_lock(&sc->tc_lock);
171 		if (tc->state != CS_HW_CONFIGURED) {
172 			CH_ERR(vi, "tid %d cannot be bound to traffic class %d "
173 			    "because it is not configured (its state is %d)\n",
174 			    toep->tid, cp->tc_idx, tc->state);
175 			cp->tc_idx = -1;
176 		} else {
177 			tc->refcount++;
178 		}
179 		mtx_unlock(&sc->tc_lock);
180 	}
181 	toep->ofld_txq = &sc->sge.ofld_txq[cp->txq_idx];
182 	toep->ofld_rxq = &sc->sge.ofld_rxq[cp->rxq_idx];
183 	toep->ctrlq = &sc->sge.ctrlq[pi->port_id];
184 
185 	tls_init_toep(toep);
186 	if (ulp_mode(toep) == ULP_MODE_TCPDDP)
187 		ddp_init_toep(toep);
188 
189 	toep->flags |= TPF_INITIALIZED;
190 
191 	return (0);
192 }
193 
194 struct toepcb *
hold_toepcb(struct toepcb * toep)195 hold_toepcb(struct toepcb *toep)
196 {
197 
198 	refcount_acquire(&toep->refcount);
199 	return (toep);
200 }
201 
202 void
free_toepcb(struct toepcb * toep)203 free_toepcb(struct toepcb *toep)
204 {
205 
206 	if (refcount_release(&toep->refcount) == 0)
207 		return;
208 
209 	KASSERT(!(toep->flags & TPF_ATTACHED),
210 	    ("%s: attached to an inpcb", __func__));
211 	KASSERT(!(toep->flags & TPF_CPL_PENDING),
212 	    ("%s: CPL pending", __func__));
213 
214 	if (toep->flags & TPF_INITIALIZED) {
215 		if (ulp_mode(toep) == ULP_MODE_TCPDDP)
216 			ddp_uninit_toep(toep);
217 		tls_uninit_toep(toep);
218 	}
219 	free(toep, M_CXGBE);
220 }
221 
222 /*
223  * Set up the socket for TCP offload.
224  */
225 void
offload_socket(struct socket * so,struct toepcb * toep)226 offload_socket(struct socket *so, struct toepcb *toep)
227 {
228 	struct tom_data *td = toep->td;
229 	struct inpcb *inp = sotoinpcb(so);
230 	struct tcpcb *tp = intotcpcb(inp);
231 	struct sockbuf *sb;
232 
233 	INP_WLOCK_ASSERT(inp);
234 
235 	/* Update socket */
236 	sb = &so->so_snd;
237 	SOCKBUF_LOCK(sb);
238 	sb->sb_flags |= SB_NOCOALESCE;
239 	SOCKBUF_UNLOCK(sb);
240 	sb = &so->so_rcv;
241 	SOCKBUF_LOCK(sb);
242 	sb->sb_flags |= SB_NOCOALESCE;
243 	if (inp->inp_vflag & INP_IPV6)
244 		so->so_proto = &toe6_protosw;
245 	else
246 		so->so_proto = &toe_protosw;
247 	SOCKBUF_UNLOCK(sb);
248 
249 	/* Update TCP PCB */
250 	tp->tod = &td->tod;
251 	tp->t_toe = toep;
252 	tp->t_flags |= TF_TOE;
253 
254 	/* Install an extra hold on inp */
255 	toep->inp = inp;
256 	toep->flags |= TPF_ATTACHED;
257 	in_pcbref(inp);
258 
259 	/* Add the TOE PCB to the active list */
260 	mtx_lock(&td->toep_list_lock);
261 	TAILQ_INSERT_HEAD(&td->toep_list, toep, link);
262 	mtx_unlock(&td->toep_list_lock);
263 }
264 
265 void
restore_so_proto(struct socket * so,bool v6)266 restore_so_proto(struct socket *so, bool v6)
267 {
268 	if (v6)
269 		so->so_proto = tcp6_protosw;
270 	else
271 		so->so_proto = tcp_protosw;
272 }
273 
274 /* This is _not_ the normal way to "unoffload" a socket. */
275 void
undo_offload_socket(struct socket * so)276 undo_offload_socket(struct socket *so)
277 {
278 	struct inpcb *inp = sotoinpcb(so);
279 	struct tcpcb *tp = intotcpcb(inp);
280 	struct toepcb *toep = tp->t_toe;
281 	struct tom_data *td = toep->td;
282 	struct sockbuf *sb;
283 
284 	INP_WLOCK_ASSERT(inp);
285 
286 	sb = &so->so_snd;
287 	SOCKBUF_LOCK(sb);
288 	sb->sb_flags &= ~SB_NOCOALESCE;
289 	SOCKBUF_UNLOCK(sb);
290 	sb = &so->so_rcv;
291 	SOCKBUF_LOCK(sb);
292 	sb->sb_flags &= ~SB_NOCOALESCE;
293 	restore_so_proto(so, inp->inp_vflag & INP_IPV6);
294 	SOCKBUF_UNLOCK(sb);
295 
296 	tp->tod = NULL;
297 	tp->t_toe = NULL;
298 	tp->t_flags &= ~TF_TOE;
299 
300 	toep->inp = NULL;
301 	toep->flags &= ~TPF_ATTACHED;
302 	if (in_pcbrele_wlocked(inp))
303 		panic("%s: inp freed.", __func__);
304 
305 	mtx_lock(&td->toep_list_lock);
306 	TAILQ_REMOVE(&td->toep_list, toep, link);
307 	mtx_unlock(&td->toep_list_lock);
308 }
309 
310 static void
release_offload_resources(struct toepcb * toep)311 release_offload_resources(struct toepcb *toep)
312 {
313 	struct tom_data *td = toep->td;
314 	struct adapter *sc = td_adapter(td);
315 	int tid = toep->tid;
316 
317 	KASSERT(!(toep->flags & TPF_CPL_PENDING),
318 	    ("%s: %p has CPL pending.", __func__, toep));
319 	KASSERT(!(toep->flags & TPF_ATTACHED),
320 	    ("%s: %p is still attached.", __func__, toep));
321 
322 	CTR5(KTR_CXGBE, "%s: toep %p (tid %d, l2te %p, ce %p)",
323 	    __func__, toep, tid, toep->l2te, toep->ce);
324 
325 	/*
326 	 * These queues should have been emptied at approximately the same time
327 	 * that a normal connection's socket's so_snd would have been purged or
328 	 * drained.  Do _not_ clean up here.
329 	 */
330 	MPASS(mbufq_empty(&toep->ulp_pduq));
331 	MPASS(mbufq_empty(&toep->ulp_pdu_reclaimq));
332 #ifdef INVARIANTS
333 	if (ulp_mode(toep) == ULP_MODE_TCPDDP)
334 		ddp_assert_empty(toep);
335 #endif
336 	MPASS(TAILQ_EMPTY(&toep->aiotx_jobq));
337 
338 	if (toep->l2te)
339 		t4_l2t_release(toep->l2te);
340 
341 	if (tid >= 0) {
342 		remove_tid(sc, tid, toep->ce ? 2 : 1);
343 		release_tid(sc, tid, toep->ctrlq);
344 	}
345 
346 	if (toep->ce)
347 		t4_release_clip_entry(sc, toep->ce);
348 
349 	if (toep->params.tc_idx != -1)
350 		t4_release_cl_rl(sc, toep->vi->pi->port_id, toep->params.tc_idx);
351 
352 	mtx_lock(&td->toep_list_lock);
353 	TAILQ_REMOVE(&td->toep_list, toep, link);
354 	mtx_unlock(&td->toep_list_lock);
355 
356 	free_toepcb(toep);
357 }
358 
359 /*
360  * The kernel is done with the TCP PCB and this is our opportunity to unhook the
361  * toepcb hanging off of it.  If the TOE driver is also done with the toepcb (no
362  * pending CPL) then it is time to release all resources tied to the toepcb.
363  *
364  * Also gets called when an offloaded active open fails and the TOM wants the
365  * kernel to take the TCP PCB back.
366  */
367 static void
t4_pcb_detach(struct toedev * tod __unused,struct tcpcb * tp)368 t4_pcb_detach(struct toedev *tod __unused, struct tcpcb *tp)
369 {
370 #if defined(KTR) || defined(INVARIANTS)
371 	struct inpcb *inp = tp->t_inpcb;
372 #endif
373 	struct toepcb *toep = tp->t_toe;
374 
375 	INP_WLOCK_ASSERT(inp);
376 
377 	KASSERT(toep != NULL, ("%s: toep is NULL", __func__));
378 	KASSERT(toep->flags & TPF_ATTACHED,
379 	    ("%s: not attached", __func__));
380 
381 #ifdef KTR
382 	if (tp->t_state == TCPS_SYN_SENT) {
383 		CTR6(KTR_CXGBE, "%s: atid %d, toep %p (0x%x), inp %p (0x%x)",
384 		    __func__, toep->tid, toep, toep->flags, inp,
385 		    inp->inp_flags);
386 	} else {
387 		CTR6(KTR_CXGBE,
388 		    "t4_pcb_detach: tid %d (%s), toep %p (0x%x), inp %p (0x%x)",
389 		    toep->tid, tcpstates[tp->t_state], toep, toep->flags, inp,
390 		    inp->inp_flags);
391 	}
392 #endif
393 
394 	if (ulp_mode(toep) == ULP_MODE_TLS)
395 		tls_detach(toep);
396 
397 	tp->tod = NULL;
398 	tp->t_toe = NULL;
399 	tp->t_flags &= ~TF_TOE;
400 	toep->flags &= ~TPF_ATTACHED;
401 
402 	if (!(toep->flags & TPF_CPL_PENDING))
403 		release_offload_resources(toep);
404 }
405 
406 /*
407  * setsockopt handler.
408  */
409 static void
t4_ctloutput(struct toedev * tod,struct tcpcb * tp,int dir,int name)410 t4_ctloutput(struct toedev *tod, struct tcpcb *tp, int dir, int name)
411 {
412 	struct adapter *sc = tod->tod_softc;
413 	struct toepcb *toep = tp->t_toe;
414 
415 	if (dir == SOPT_GET)
416 		return;
417 
418 	CTR4(KTR_CXGBE, "%s: tp %p, dir %u, name %u", __func__, tp, dir, name);
419 
420 	switch (name) {
421 	case TCP_NODELAY:
422 		if (tp->t_state != TCPS_ESTABLISHED)
423 			break;
424 		toep->params.nagle = tp->t_flags & TF_NODELAY ? 0 : 1;
425 		t4_set_tcb_field(sc, toep->ctrlq, toep, W_TCB_T_FLAGS,
426 		    V_TF_NAGLE(1), V_TF_NAGLE(toep->params.nagle), 0, 0);
427 		break;
428 	default:
429 		break;
430 	}
431 }
432 
433 static inline uint64_t
get_tcb_tflags(const uint64_t * tcb)434 get_tcb_tflags(const uint64_t *tcb)
435 {
436 
437 	return ((be64toh(tcb[14]) << 32) | (be64toh(tcb[15]) >> 32));
438 }
439 
440 static inline uint32_t
get_tcb_field(const uint64_t * tcb,u_int word,uint32_t mask,u_int shift)441 get_tcb_field(const uint64_t *tcb, u_int word, uint32_t mask, u_int shift)
442 {
443 #define LAST_WORD ((TCB_SIZE / 4) - 1)
444 	uint64_t t1, t2;
445 	int flit_idx;
446 
447 	MPASS(mask != 0);
448 	MPASS(word <= LAST_WORD);
449 	MPASS(shift < 32);
450 
451 	flit_idx = (LAST_WORD - word) / 2;
452 	if (word & 0x1)
453 		shift += 32;
454 	t1 = be64toh(tcb[flit_idx]) >> shift;
455 	t2 = 0;
456 	if (fls(mask) > 64 - shift) {
457 		/*
458 		 * Will spill over into the next logical flit, which is the flit
459 		 * before this one.  The flit_idx before this one must be valid.
460 		 */
461 		MPASS(flit_idx > 0);
462 		t2 = be64toh(tcb[flit_idx - 1]) << (64 - shift);
463 	}
464 	return ((t2 | t1) & mask);
465 #undef LAST_WORD
466 }
467 #define GET_TCB_FIELD(tcb, F) \
468     get_tcb_field(tcb, W_TCB_##F, M_TCB_##F, S_TCB_##F)
469 
470 /*
471  * Issues a CPL_GET_TCB to read the entire TCB for the tid.
472  */
473 static int
send_get_tcb(struct adapter * sc,u_int tid)474 send_get_tcb(struct adapter *sc, u_int tid)
475 {
476 	struct cpl_get_tcb *cpl;
477 	struct wrq_cookie cookie;
478 
479 	MPASS(tid >= sc->tids.tid_base);
480 	MPASS(tid - sc->tids.tid_base < sc->tids.ntids);
481 
482 	cpl = start_wrq_wr(&sc->sge.ctrlq[0], howmany(sizeof(*cpl), 16),
483 	    &cookie);
484 	if (__predict_false(cpl == NULL))
485 		return (ENOMEM);
486 	bzero(cpl, sizeof(*cpl));
487 	INIT_TP_WR(cpl, tid);
488 	OPCODE_TID(cpl) = htobe32(MK_OPCODE_TID(CPL_GET_TCB, tid));
489 	cpl->reply_ctrl = htobe16(V_REPLY_CHAN(0) |
490 	    V_QUEUENO(sc->sge.ofld_rxq[0].iq.cntxt_id));
491 	cpl->cookie = 0xff;
492 	commit_wrq_wr(&sc->sge.ctrlq[0], cpl, &cookie);
493 
494 	return (0);
495 }
496 
497 static struct tcb_histent *
alloc_tcb_histent(struct adapter * sc,u_int tid,int flags)498 alloc_tcb_histent(struct adapter *sc, u_int tid, int flags)
499 {
500 	struct tcb_histent *te;
501 
502 	MPASS(flags == M_NOWAIT || flags == M_WAITOK);
503 
504 	te = malloc(sizeof(*te), M_CXGBE, M_ZERO | flags);
505 	if (te == NULL)
506 		return (NULL);
507 	mtx_init(&te->te_lock, "TCB entry", NULL, MTX_DEF);
508 	callout_init_mtx(&te->te_callout, &te->te_lock, 0);
509 	te->te_adapter = sc;
510 	te->te_tid = tid;
511 
512 	return (te);
513 }
514 
515 static void
free_tcb_histent(struct tcb_histent * te)516 free_tcb_histent(struct tcb_histent *te)
517 {
518 
519 	mtx_destroy(&te->te_lock);
520 	free(te, M_CXGBE);
521 }
522 
523 /*
524  * Start tracking the tid in the TCB history.
525  */
526 int
add_tid_to_history(struct adapter * sc,u_int tid)527 add_tid_to_history(struct adapter *sc, u_int tid)
528 {
529 	struct tcb_histent *te = NULL;
530 	struct tom_data *td = sc->tom_softc;
531 	int rc;
532 
533 	MPASS(tid >= sc->tids.tid_base);
534 	MPASS(tid - sc->tids.tid_base < sc->tids.ntids);
535 
536 	if (td->tcb_history == NULL)
537 		return (ENXIO);
538 
539 	rw_wlock(&td->tcb_history_lock);
540 	if (td->tcb_history[tid] != NULL) {
541 		rc = EEXIST;
542 		goto done;
543 	}
544 	te = alloc_tcb_histent(sc, tid, M_NOWAIT);
545 	if (te == NULL) {
546 		rc = ENOMEM;
547 		goto done;
548 	}
549 	mtx_lock(&te->te_lock);
550 	rc = send_get_tcb(sc, tid);
551 	if (rc == 0) {
552 		te->te_flags |= TE_RPL_PENDING;
553 		td->tcb_history[tid] = te;
554 	} else {
555 		free(te, M_CXGBE);
556 	}
557 	mtx_unlock(&te->te_lock);
558 done:
559 	rw_wunlock(&td->tcb_history_lock);
560 	return (rc);
561 }
562 
563 static void
remove_tcb_histent(struct tcb_histent * te)564 remove_tcb_histent(struct tcb_histent *te)
565 {
566 	struct adapter *sc = te->te_adapter;
567 	struct tom_data *td = sc->tom_softc;
568 
569 	rw_assert(&td->tcb_history_lock, RA_WLOCKED);
570 	mtx_assert(&te->te_lock, MA_OWNED);
571 	MPASS(td->tcb_history[te->te_tid] == te);
572 
573 	td->tcb_history[te->te_tid] = NULL;
574 	free_tcb_histent(te);
575 	rw_wunlock(&td->tcb_history_lock);
576 }
577 
578 static inline struct tcb_histent *
lookup_tcb_histent(struct adapter * sc,u_int tid,bool addrem)579 lookup_tcb_histent(struct adapter *sc, u_int tid, bool addrem)
580 {
581 	struct tcb_histent *te;
582 	struct tom_data *td = sc->tom_softc;
583 
584 	MPASS(tid >= sc->tids.tid_base);
585 	MPASS(tid - sc->tids.tid_base < sc->tids.ntids);
586 
587 	if (td->tcb_history == NULL)
588 		return (NULL);
589 
590 	if (addrem)
591 		rw_wlock(&td->tcb_history_lock);
592 	else
593 		rw_rlock(&td->tcb_history_lock);
594 	te = td->tcb_history[tid];
595 	if (te != NULL) {
596 		mtx_lock(&te->te_lock);
597 		return (te);	/* with both locks held */
598 	}
599 	if (addrem)
600 		rw_wunlock(&td->tcb_history_lock);
601 	else
602 		rw_runlock(&td->tcb_history_lock);
603 
604 	return (te);
605 }
606 
607 static inline void
release_tcb_histent(struct tcb_histent * te)608 release_tcb_histent(struct tcb_histent *te)
609 {
610 	struct adapter *sc = te->te_adapter;
611 	struct tom_data *td = sc->tom_softc;
612 
613 	mtx_assert(&te->te_lock, MA_OWNED);
614 	mtx_unlock(&te->te_lock);
615 	rw_assert(&td->tcb_history_lock, RA_RLOCKED);
616 	rw_runlock(&td->tcb_history_lock);
617 }
618 
619 static void
request_tcb(void * arg)620 request_tcb(void *arg)
621 {
622 	struct tcb_histent *te = arg;
623 
624 	mtx_assert(&te->te_lock, MA_OWNED);
625 
626 	/* Noone else is supposed to update the histent. */
627 	MPASS(!(te->te_flags & TE_RPL_PENDING));
628 	if (send_get_tcb(te->te_adapter, te->te_tid) == 0)
629 		te->te_flags |= TE_RPL_PENDING;
630 	else
631 		callout_schedule(&te->te_callout, hz / 100);
632 }
633 
634 static void
update_tcb_histent(struct tcb_histent * te,const uint64_t * tcb)635 update_tcb_histent(struct tcb_histent *te, const uint64_t *tcb)
636 {
637 	struct tom_data *td = te->te_adapter->tom_softc;
638 	uint64_t tflags = get_tcb_tflags(tcb);
639 	uint8_t sample = 0;
640 
641 	if (GET_TCB_FIELD(tcb, SND_MAX_RAW) != GET_TCB_FIELD(tcb, SND_UNA_RAW)) {
642 		if (GET_TCB_FIELD(tcb, T_RXTSHIFT) != 0)
643 			sample |= TS_RTO;
644 		if (GET_TCB_FIELD(tcb, T_DUPACKS) != 0)
645 			sample |= TS_DUPACKS;
646 		if (GET_TCB_FIELD(tcb, T_DUPACKS) >= td->dupack_threshold)
647 			sample |= TS_FASTREXMT;
648 	}
649 
650 	if (GET_TCB_FIELD(tcb, SND_MAX_RAW) != 0) {
651 		uint32_t snd_wnd;
652 
653 		sample |= TS_SND_BACKLOGGED;	/* for whatever reason. */
654 
655 		snd_wnd = GET_TCB_FIELD(tcb, RCV_ADV);
656 		if (tflags & V_TF_RECV_SCALE(1))
657 			snd_wnd <<= GET_TCB_FIELD(tcb, RCV_SCALE);
658 		if (GET_TCB_FIELD(tcb, SND_CWND) < snd_wnd)
659 			sample |= TS_CWND_LIMITED;	/* maybe due to CWND */
660 	}
661 
662 	if (tflags & V_TF_CCTRL_ECN(1)) {
663 
664 		/*
665 		 * CE marker on incoming IP hdr, echoing ECE back in the TCP
666 		 * hdr.  Indicates congestion somewhere on the way from the peer
667 		 * to this node.
668 		 */
669 		if (tflags & V_TF_CCTRL_ECE(1))
670 			sample |= TS_ECN_ECE;
671 
672 		/*
673 		 * ECE seen and CWR sent (or about to be sent).  Might indicate
674 		 * congestion on the way to the peer.  This node is reducing its
675 		 * congestion window in response.
676 		 */
677 		if (tflags & (V_TF_CCTRL_CWR(1) | V_TF_CCTRL_RFR(1)))
678 			sample |= TS_ECN_CWR;
679 	}
680 
681 	te->te_sample[te->te_pidx] = sample;
682 	if (++te->te_pidx == nitems(te->te_sample))
683 		te->te_pidx = 0;
684 	memcpy(te->te_tcb, tcb, TCB_SIZE);
685 	te->te_flags |= TE_ACTIVE;
686 }
687 
688 static int
do_get_tcb_rpl(struct sge_iq * iq,const struct rss_header * rss,struct mbuf * m)689 do_get_tcb_rpl(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
690 {
691 	struct adapter *sc = iq->adapter;
692 	const struct cpl_get_tcb_rpl *cpl = mtod(m, const void *);
693 	const uint64_t *tcb = (const uint64_t *)(const void *)(cpl + 1);
694 	struct tcb_histent *te;
695 	const u_int tid = GET_TID(cpl);
696 	bool remove;
697 
698 	remove = GET_TCB_FIELD(tcb, T_STATE) == TCPS_CLOSED;
699 	te = lookup_tcb_histent(sc, tid, remove);
700 	if (te == NULL) {
701 		/* Not in the history.  Who issued the GET_TCB for this? */
702 		device_printf(sc->dev, "tcb %u: flags 0x%016jx, state %u, "
703 		    "srtt %u, sscale %u, rscale %u, cookie 0x%x\n", tid,
704 		    (uintmax_t)get_tcb_tflags(tcb), GET_TCB_FIELD(tcb, T_STATE),
705 		    GET_TCB_FIELD(tcb, T_SRTT), GET_TCB_FIELD(tcb, SND_SCALE),
706 		    GET_TCB_FIELD(tcb, RCV_SCALE), cpl->cookie);
707 		goto done;
708 	}
709 
710 	MPASS(te->te_flags & TE_RPL_PENDING);
711 	te->te_flags &= ~TE_RPL_PENDING;
712 	if (remove) {
713 		remove_tcb_histent(te);
714 	} else {
715 		update_tcb_histent(te, tcb);
716 		callout_reset(&te->te_callout, hz / 10, request_tcb, te);
717 		release_tcb_histent(te);
718 	}
719 done:
720 	m_freem(m);
721 	return (0);
722 }
723 
724 static void
fill_tcp_info_from_tcb(struct adapter * sc,uint64_t * tcb,struct tcp_info * ti)725 fill_tcp_info_from_tcb(struct adapter *sc, uint64_t *tcb, struct tcp_info *ti)
726 {
727 	uint32_t v;
728 
729 	ti->tcpi_state = GET_TCB_FIELD(tcb, T_STATE);
730 
731 	v = GET_TCB_FIELD(tcb, T_SRTT);
732 	ti->tcpi_rtt = tcp_ticks_to_us(sc, v);
733 
734 	v = GET_TCB_FIELD(tcb, T_RTTVAR);
735 	ti->tcpi_rttvar = tcp_ticks_to_us(sc, v);
736 
737 	ti->tcpi_snd_ssthresh = GET_TCB_FIELD(tcb, SND_SSTHRESH);
738 	ti->tcpi_snd_cwnd = GET_TCB_FIELD(tcb, SND_CWND);
739 	ti->tcpi_rcv_nxt = GET_TCB_FIELD(tcb, RCV_NXT);
740 	ti->tcpi_rcv_adv = GET_TCB_FIELD(tcb, RCV_ADV);
741 	ti->tcpi_dupacks = GET_TCB_FIELD(tcb, T_DUPACKS);
742 
743 	v = GET_TCB_FIELD(tcb, TX_MAX);
744 	ti->tcpi_snd_nxt = v - GET_TCB_FIELD(tcb, SND_NXT_RAW);
745 	ti->tcpi_snd_una = v - GET_TCB_FIELD(tcb, SND_UNA_RAW);
746 	ti->tcpi_snd_max = v - GET_TCB_FIELD(tcb, SND_MAX_RAW);
747 
748 	/* Receive window being advertised by us. */
749 	ti->tcpi_rcv_wscale = GET_TCB_FIELD(tcb, SND_SCALE);	/* Yes, SND. */
750 	ti->tcpi_rcv_space = GET_TCB_FIELD(tcb, RCV_WND);
751 
752 	/* Send window */
753 	ti->tcpi_snd_wscale = GET_TCB_FIELD(tcb, RCV_SCALE);	/* Yes, RCV. */
754 	ti->tcpi_snd_wnd = GET_TCB_FIELD(tcb, RCV_ADV);
755 	if (get_tcb_tflags(tcb) & V_TF_RECV_SCALE(1))
756 		ti->tcpi_snd_wnd <<= ti->tcpi_snd_wscale;
757 	else
758 		ti->tcpi_snd_wscale = 0;
759 
760 }
761 
762 static void
fill_tcp_info_from_history(struct adapter * sc,struct tcb_histent * te,struct tcp_info * ti)763 fill_tcp_info_from_history(struct adapter *sc, struct tcb_histent *te,
764     struct tcp_info *ti)
765 {
766 
767 	fill_tcp_info_from_tcb(sc, te->te_tcb, ti);
768 }
769 
770 /*
771  * Reads the TCB for the given tid using a memory window and copies it to 'buf'
772  * in the same format as CPL_GET_TCB_RPL.
773  */
774 static void
read_tcb_using_memwin(struct adapter * sc,u_int tid,uint64_t * buf)775 read_tcb_using_memwin(struct adapter *sc, u_int tid, uint64_t *buf)
776 {
777 	int i, j, k, rc;
778 	uint32_t addr;
779 	u_char *tcb, tmp;
780 
781 	MPASS(tid >= sc->tids.tid_base);
782 	MPASS(tid - sc->tids.tid_base < sc->tids.ntids);
783 
784 	addr = t4_read_reg(sc, A_TP_CMM_TCB_BASE) + tid * TCB_SIZE;
785 	rc = read_via_memwin(sc, 2, addr, (uint32_t *)buf, TCB_SIZE);
786 	if (rc != 0)
787 		return;
788 
789 	tcb = (u_char *)buf;
790 	for (i = 0, j = TCB_SIZE - 16; i < j; i += 16, j -= 16) {
791 		for (k = 0; k < 16; k++) {
792 			tmp = tcb[i + k];
793 			tcb[i + k] = tcb[j + k];
794 			tcb[j + k] = tmp;
795 		}
796 	}
797 }
798 
799 static void
fill_tcp_info(struct adapter * sc,u_int tid,struct tcp_info * ti)800 fill_tcp_info(struct adapter *sc, u_int tid, struct tcp_info *ti)
801 {
802 	uint64_t tcb[TCB_SIZE / sizeof(uint64_t)];
803 	struct tcb_histent *te;
804 
805 	ti->tcpi_toe_tid = tid;
806 	te = lookup_tcb_histent(sc, tid, false);
807 	if (te != NULL) {
808 		fill_tcp_info_from_history(sc, te, ti);
809 		release_tcb_histent(te);
810 	} else {
811 		if (!(sc->debug_flags & DF_DISABLE_TCB_CACHE)) {
812 			/* XXX: tell firmware to flush TCB cache. */
813 		}
814 		read_tcb_using_memwin(sc, tid, tcb);
815 		fill_tcp_info_from_tcb(sc, tcb, ti);
816 	}
817 }
818 
819 /*
820  * Called by the kernel to allow the TOE driver to "refine" values filled up in
821  * the tcp_info for an offloaded connection.
822  */
823 static void
t4_tcp_info(struct toedev * tod,const struct tcpcb * tp,struct tcp_info * ti)824 t4_tcp_info(struct toedev *tod, const struct tcpcb *tp, struct tcp_info *ti)
825 {
826 	struct adapter *sc = tod->tod_softc;
827 	struct toepcb *toep = tp->t_toe;
828 
829 	INP_LOCK_ASSERT(tp->t_inpcb);
830 	MPASS(ti != NULL);
831 
832 	fill_tcp_info(sc, toep->tid, ti);
833 }
834 
835 #ifdef KERN_TLS
836 static int
t4_alloc_tls_session(struct toedev * tod,struct tcpcb * tp,struct ktls_session * tls,int direction)837 t4_alloc_tls_session(struct toedev *tod, struct tcpcb *tp,
838     struct ktls_session *tls, int direction)
839 {
840 	struct toepcb *toep = tp->t_toe;
841 
842 	INP_WLOCK_ASSERT(tp->t_inpcb);
843 	MPASS(tls != NULL);
844 
845 	return (tls_alloc_ktls(toep, tls, direction));
846 }
847 #endif
848 
849 /*
850  * The TOE driver will not receive any more CPLs for the tid associated with the
851  * toepcb; release the hold on the inpcb.
852  */
853 void
final_cpl_received(struct toepcb * toep)854 final_cpl_received(struct toepcb *toep)
855 {
856 	struct inpcb *inp = toep->inp;
857 	bool need_wakeup;
858 
859 	KASSERT(inp != NULL, ("%s: inp is NULL", __func__));
860 	INP_WLOCK_ASSERT(inp);
861 	KASSERT(toep->flags & TPF_CPL_PENDING,
862 	    ("%s: CPL not pending already?", __func__));
863 
864 	CTR6(KTR_CXGBE, "%s: tid %d, toep %p (0x%x), inp %p (0x%x)",
865 	    __func__, toep->tid, toep, toep->flags, inp, inp->inp_flags);
866 
867 	if (ulp_mode(toep) == ULP_MODE_TCPDDP)
868 		release_ddp_resources(toep);
869 	else if (ulp_mode(toep) == ULP_MODE_TLS)
870 		tls_detach(toep);
871 	toep->inp = NULL;
872 	need_wakeup = (toep->flags & TPF_WAITING_FOR_FINAL) != 0;
873 	toep->flags &= ~(TPF_CPL_PENDING | TPF_WAITING_FOR_FINAL);
874 	mbufq_drain(&toep->ulp_pduq);
875 	mbufq_drain(&toep->ulp_pdu_reclaimq);
876 
877 	if (!(toep->flags & TPF_ATTACHED))
878 		release_offload_resources(toep);
879 
880 	if (!in_pcbrele_wlocked(inp))
881 		INP_WUNLOCK(inp);
882 
883 	if (need_wakeup) {
884 		struct mtx *lock = mtx_pool_find(mtxpool_sleep, toep);
885 
886 		mtx_lock(lock);
887 		wakeup(toep);
888 		mtx_unlock(lock);
889 	}
890 }
891 
892 void
insert_tid(struct adapter * sc,int tid,void * ctx,int ntids)893 insert_tid(struct adapter *sc, int tid, void *ctx, int ntids)
894 {
895 	struct tid_info *t = &sc->tids;
896 
897 	MPASS(tid >= t->tid_base);
898 	MPASS(tid - t->tid_base < t->ntids);
899 
900 	t->tid_tab[tid - t->tid_base] = ctx;
901 	atomic_add_int(&t->tids_in_use, ntids);
902 }
903 
904 void *
lookup_tid(struct adapter * sc,int tid)905 lookup_tid(struct adapter *sc, int tid)
906 {
907 	struct tid_info *t = &sc->tids;
908 
909 	return (t->tid_tab[tid - t->tid_base]);
910 }
911 
912 void
update_tid(struct adapter * sc,int tid,void * ctx)913 update_tid(struct adapter *sc, int tid, void *ctx)
914 {
915 	struct tid_info *t = &sc->tids;
916 
917 	t->tid_tab[tid - t->tid_base] = ctx;
918 }
919 
920 void
remove_tid(struct adapter * sc,int tid,int ntids)921 remove_tid(struct adapter *sc, int tid, int ntids)
922 {
923 	struct tid_info *t = &sc->tids;
924 
925 	t->tid_tab[tid - t->tid_base] = NULL;
926 	atomic_subtract_int(&t->tids_in_use, ntids);
927 }
928 
929 /*
930  * What mtu_idx to use, given a 4-tuple.  Note that both s->mss and tcp_mssopt
931  * have the MSS that we should advertise in our SYN.  Advertised MSS doesn't
932  * account for any TCP options so the effective MSS (only payload, no headers or
933  * options) could be different.
934  */
935 static int
find_best_mtu_idx(struct adapter * sc,struct in_conninfo * inc,struct offload_settings * s)936 find_best_mtu_idx(struct adapter *sc, struct in_conninfo *inc,
937     struct offload_settings *s)
938 {
939 	unsigned short *mtus = &sc->params.mtus[0];
940 	int i, mss, mtu;
941 
942 	MPASS(inc != NULL);
943 
944 	mss = s->mss > 0 ? s->mss : tcp_mssopt(inc);
945 	if (inc->inc_flags & INC_ISIPV6)
946 		mtu = mss + sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
947 	else
948 		mtu = mss + sizeof(struct ip) + sizeof(struct tcphdr);
949 
950 	for (i = 0; i < NMTUS - 1 && mtus[i + 1] <= mtu; i++)
951 		continue;
952 
953 	return (i);
954 }
955 
956 /*
957  * Determine the receive window size for a socket.
958  */
959 u_long
select_rcv_wnd(struct socket * so)960 select_rcv_wnd(struct socket *so)
961 {
962 	unsigned long wnd;
963 
964 	SOCKBUF_LOCK_ASSERT(&so->so_rcv);
965 
966 	wnd = sbspace(&so->so_rcv);
967 	if (wnd < MIN_RCV_WND)
968 		wnd = MIN_RCV_WND;
969 
970 	return min(wnd, MAX_RCV_WND);
971 }
972 
973 int
select_rcv_wscale(void)974 select_rcv_wscale(void)
975 {
976 	int wscale = 0;
977 	unsigned long space = sb_max;
978 
979 	if (space > MAX_RCV_WND)
980 		space = MAX_RCV_WND;
981 
982 	while (wscale < TCP_MAX_WINSHIFT && (TCP_MAXWIN << wscale) < space)
983 		wscale++;
984 
985 	return (wscale);
986 }
987 
988 __be64
calc_options0(struct vi_info * vi,struct conn_params * cp)989 calc_options0(struct vi_info *vi, struct conn_params *cp)
990 {
991 	uint64_t opt0 = 0;
992 
993 	opt0 |= F_TCAM_BYPASS;
994 
995 	MPASS(cp->wscale >= 0 && cp->wscale <= M_WND_SCALE);
996 	opt0 |= V_WND_SCALE(cp->wscale);
997 
998 	MPASS(cp->mtu_idx >= 0 && cp->mtu_idx < NMTUS);
999 	opt0 |= V_MSS_IDX(cp->mtu_idx);
1000 
1001 	MPASS(cp->ulp_mode >= 0 && cp->ulp_mode <= M_ULP_MODE);
1002 	opt0 |= V_ULP_MODE(cp->ulp_mode);
1003 
1004 	MPASS(cp->opt0_bufsize >= 0 && cp->opt0_bufsize <= M_RCV_BUFSIZ);
1005 	opt0 |= V_RCV_BUFSIZ(cp->opt0_bufsize);
1006 
1007 	MPASS(cp->l2t_idx >= 0 && cp->l2t_idx < vi->adapter->vres.l2t.size);
1008 	opt0 |= V_L2T_IDX(cp->l2t_idx);
1009 
1010 	opt0 |= V_SMAC_SEL(vi->smt_idx);
1011 	opt0 |= V_TX_CHAN(vi->pi->tx_chan);
1012 
1013 	MPASS(cp->keepalive == 0 || cp->keepalive == 1);
1014 	opt0 |= V_KEEP_ALIVE(cp->keepalive);
1015 
1016 	MPASS(cp->nagle == 0 || cp->nagle == 1);
1017 	opt0 |= V_NAGLE(cp->nagle);
1018 
1019 	return (htobe64(opt0));
1020 }
1021 
1022 __be32
calc_options2(struct vi_info * vi,struct conn_params * cp)1023 calc_options2(struct vi_info *vi, struct conn_params *cp)
1024 {
1025 	uint32_t opt2 = 0;
1026 	struct port_info *pi = vi->pi;
1027 	struct adapter *sc = pi->adapter;
1028 
1029 	/*
1030 	 * rx flow control, rx coalesce, congestion control, and tx pace are all
1031 	 * explicitly set by the driver.  On T5+ the ISS is also set by the
1032 	 * driver to the value picked by the kernel.
1033 	 */
1034 	if (is_t4(sc)) {
1035 		opt2 |= F_RX_FC_VALID | F_RX_COALESCE_VALID;
1036 		opt2 |= F_CONG_CNTRL_VALID | F_PACE_VALID;
1037 	} else {
1038 		opt2 |= F_T5_OPT_2_VALID;	/* all 4 valid */
1039 		opt2 |= F_T5_ISS;		/* ISS provided in CPL */
1040 	}
1041 
1042 	MPASS(cp->sack == 0 || cp->sack == 1);
1043 	opt2 |= V_SACK_EN(cp->sack);
1044 
1045 	MPASS(cp->tstamp == 0 || cp->tstamp == 1);
1046 	opt2 |= V_TSTAMPS_EN(cp->tstamp);
1047 
1048 	if (cp->wscale > 0)
1049 		opt2 |= F_WND_SCALE_EN;
1050 
1051 	MPASS(cp->ecn == 0 || cp->ecn == 1);
1052 	opt2 |= V_CCTRL_ECN(cp->ecn);
1053 
1054 	opt2 |= V_TX_QUEUE(TX_MODQ(pi->tx_chan));
1055 	opt2 |= V_PACE(0);
1056 	opt2 |= F_RSS_QUEUE_VALID;
1057 	opt2 |= V_RSS_QUEUE(sc->sge.ofld_rxq[cp->rxq_idx].iq.abs_id);
1058 	if (chip_id(sc) <= CHELSIO_T6) {
1059 		MPASS(pi->rx_chan == 0 || pi->rx_chan == 1);
1060 		opt2 |= V_RX_CHANNEL(pi->rx_chan);
1061 	}
1062 
1063 	MPASS(cp->cong_algo >= 0 && cp->cong_algo <= M_CONG_CNTRL);
1064 	opt2 |= V_CONG_CNTRL(cp->cong_algo);
1065 
1066 	MPASS(cp->rx_coalesce == 0 || cp->rx_coalesce == 1);
1067 	if (cp->rx_coalesce == 1)
1068 		opt2 |= V_RX_COALESCE(M_RX_COALESCE);
1069 
1070 	opt2 |= V_RX_FC_DDP(0) | V_RX_FC_DISABLE(0);
1071 #ifdef USE_DDP_RX_FLOW_CONTROL
1072 	if (cp->ulp_mode == ULP_MODE_TCPDDP)
1073 		opt2 |= F_RX_FC_DDP;
1074 #endif
1075 
1076 	return (htobe32(opt2));
1077 }
1078 
1079 uint64_t
select_ntuple(struct vi_info * vi,struct l2t_entry * e)1080 select_ntuple(struct vi_info *vi, struct l2t_entry *e)
1081 {
1082 	struct adapter *sc = vi->adapter;
1083 	struct tp_params *tp = &sc->params.tp;
1084 	uint64_t ntuple = 0;
1085 
1086 	/*
1087 	 * Initialize each of the fields which we care about which are present
1088 	 * in the Compressed Filter Tuple.
1089 	 */
1090 	if (tp->vlan_shift >= 0 && EVL_VLANOFTAG(e->vlan) != CPL_L2T_VLAN_NONE)
1091 		ntuple |= (uint64_t)(F_FT_VLAN_VLD | e->vlan) << tp->vlan_shift;
1092 
1093 	if (tp->port_shift >= 0)
1094 		ntuple |= (uint64_t)e->lport << tp->port_shift;
1095 
1096 	if (tp->protocol_shift >= 0)
1097 		ntuple |= (uint64_t)IPPROTO_TCP << tp->protocol_shift;
1098 
1099 	if (tp->vnic_shift >= 0 && tp->vnic_mode == FW_VNIC_MODE_PF_VF) {
1100 		ntuple |= (uint64_t)(V_FT_VNID_ID_VF(vi->vin) |
1101 		    V_FT_VNID_ID_PF(sc->pf) | V_FT_VNID_ID_VLD(vi->vfvld)) <<
1102 		    tp->vnic_shift;
1103 	}
1104 
1105 	if (is_t4(sc))
1106 		return (htobe32((uint32_t)ntuple));
1107 	else
1108 		return (htobe64(V_FILTER_TUPLE(ntuple)));
1109 }
1110 
1111 static int
is_tls_sock(struct socket * so,struct adapter * sc)1112 is_tls_sock(struct socket *so, struct adapter *sc)
1113 {
1114 	struct inpcb *inp = sotoinpcb(so);
1115 	int i, rc;
1116 
1117 	/* XXX: Eventually add a SO_WANT_TLS socket option perhaps? */
1118 	rc = 0;
1119 	ADAPTER_LOCK(sc);
1120 	for (i = 0; i < sc->tt.num_tls_rx_ports; i++) {
1121 		if (inp->inp_lport == htons(sc->tt.tls_rx_ports[i]) ||
1122 		    inp->inp_fport == htons(sc->tt.tls_rx_ports[i])) {
1123 			rc = 1;
1124 			break;
1125 		}
1126 	}
1127 	ADAPTER_UNLOCK(sc);
1128 	return (rc);
1129 }
1130 
1131 /*
1132  * Initialize various connection parameters.
1133  */
1134 void
init_conn_params(struct vi_info * vi,struct offload_settings * s,struct in_conninfo * inc,struct socket * so,const struct tcp_options * tcpopt,int16_t l2t_idx,struct conn_params * cp)1135 init_conn_params(struct vi_info *vi , struct offload_settings *s,
1136     struct in_conninfo *inc, struct socket *so,
1137     const struct tcp_options *tcpopt, int16_t l2t_idx, struct conn_params *cp)
1138 {
1139 	struct port_info *pi = vi->pi;
1140 	struct adapter *sc = pi->adapter;
1141 	struct tom_tunables *tt = &sc->tt;
1142 	struct inpcb *inp = sotoinpcb(so);
1143 	struct tcpcb *tp = intotcpcb(inp);
1144 	u_long wnd;
1145 	u_int q_idx;
1146 
1147 	MPASS(s->offload != 0);
1148 
1149 	/* Congestion control algorithm */
1150 	if (s->cong_algo >= 0)
1151 		cp->cong_algo = s->cong_algo & M_CONG_CNTRL;
1152 	else if (sc->tt.cong_algorithm >= 0)
1153 		cp->cong_algo = tt->cong_algorithm & M_CONG_CNTRL;
1154 	else {
1155 		struct cc_algo *cc = CC_ALGO(tp);
1156 
1157 		if (strcasecmp(cc->name, "reno") == 0)
1158 			cp->cong_algo = CONG_ALG_RENO;
1159 		else if (strcasecmp(cc->name, "tahoe") == 0)
1160 			cp->cong_algo = CONG_ALG_TAHOE;
1161 		if (strcasecmp(cc->name, "newreno") == 0)
1162 			cp->cong_algo = CONG_ALG_NEWRENO;
1163 		if (strcasecmp(cc->name, "highspeed") == 0)
1164 			cp->cong_algo = CONG_ALG_HIGHSPEED;
1165 		else {
1166 			/*
1167 			 * Use newreno in case the algorithm selected by the
1168 			 * host stack is not supported by the hardware.
1169 			 */
1170 			cp->cong_algo = CONG_ALG_NEWRENO;
1171 		}
1172 	}
1173 
1174 	/* Tx traffic scheduling class. */
1175 	if (s->sched_class >= 0 && s->sched_class < sc->params.nsched_cls)
1176 		cp->tc_idx = s->sched_class;
1177 	else
1178 		cp->tc_idx = -1;
1179 
1180 	/* Nagle's algorithm. */
1181 	if (s->nagle >= 0)
1182 		cp->nagle = s->nagle > 0 ? 1 : 0;
1183 	else
1184 		cp->nagle = tp->t_flags & TF_NODELAY ? 0 : 1;
1185 
1186 	/* TCP Keepalive. */
1187 	if (V_tcp_always_keepalive || so_options_get(so) & SO_KEEPALIVE)
1188 		cp->keepalive = 1;
1189 	else
1190 		cp->keepalive = 0;
1191 
1192 	/* Optimization that's specific to T5 @ 40G. */
1193 	if (tt->tx_align >= 0)
1194 		cp->tx_align =  tt->tx_align > 0 ? 1 : 0;
1195 	else if (chip_id(sc) == CHELSIO_T5 &&
1196 	    (port_top_speed(pi) > 10 || sc->params.nports > 2))
1197 		cp->tx_align = 1;
1198 	else
1199 		cp->tx_align = 0;
1200 
1201 	/* ULP mode. */
1202 	if (can_tls_offload(sc) &&
1203 	    (s->tls > 0 || (s->tls < 0 && is_tls_sock(so, sc))))
1204 		cp->ulp_mode = ULP_MODE_TLS;
1205 	else if (s->ddp > 0 ||
1206 	    (s->ddp < 0 && sc->tt.ddp && (so_options_get(so) & SO_NO_DDP) == 0))
1207 		cp->ulp_mode = ULP_MODE_TCPDDP;
1208 	else
1209 		cp->ulp_mode = ULP_MODE_NONE;
1210 
1211 	/* Rx coalescing. */
1212 	if (s->rx_coalesce >= 0)
1213 		cp->rx_coalesce = s->rx_coalesce > 0 ? 1 : 0;
1214 	else if (cp->ulp_mode == ULP_MODE_TLS)
1215 		cp->rx_coalesce = 0;
1216 	else if (tt->rx_coalesce >= 0)
1217 		cp->rx_coalesce = tt->rx_coalesce > 0 ? 1 : 0;
1218 	else
1219 		cp->rx_coalesce = 1;	/* default */
1220 
1221 	/*
1222 	 * Index in the PMTU table.  This controls the MSS that we announce in
1223 	 * our SYN initially, but after ESTABLISHED it controls the MSS that we
1224 	 * use to send data.
1225 	 */
1226 	cp->mtu_idx = find_best_mtu_idx(sc, inc, s);
1227 
1228 	/* Tx queue for this connection. */
1229 	if (s->txq == QUEUE_RANDOM)
1230 		q_idx = arc4random();
1231 	else if (s->txq == QUEUE_ROUNDROBIN)
1232 		q_idx = atomic_fetchadd_int(&vi->txq_rr, 1);
1233 	else
1234 		q_idx = s->txq;
1235 	cp->txq_idx = vi->first_ofld_txq + q_idx % vi->nofldtxq;
1236 
1237 	/* Rx queue for this connection. */
1238 	if (s->rxq == QUEUE_RANDOM)
1239 		q_idx = arc4random();
1240 	else if (s->rxq == QUEUE_ROUNDROBIN)
1241 		q_idx = atomic_fetchadd_int(&vi->rxq_rr, 1);
1242 	else
1243 		q_idx = s->rxq;
1244 	cp->rxq_idx = vi->first_ofld_rxq + q_idx % vi->nofldrxq;
1245 
1246 	if (SOLISTENING(so)) {
1247 		/* Passive open */
1248 		MPASS(tcpopt != NULL);
1249 
1250 		/* TCP timestamp option */
1251 		if (tcpopt->tstamp &&
1252 		    (s->tstamp > 0 || (s->tstamp < 0 && V_tcp_do_rfc1323)))
1253 			cp->tstamp = 1;
1254 		else
1255 			cp->tstamp = 0;
1256 
1257 		/* SACK */
1258 		if (tcpopt->sack &&
1259 		    (s->sack > 0 || (s->sack < 0 && V_tcp_do_sack)))
1260 			cp->sack = 1;
1261 		else
1262 			cp->sack = 0;
1263 
1264 		/* Receive window scaling. */
1265 		if (tcpopt->wsf > 0 && tcpopt->wsf < 15 && V_tcp_do_rfc1323)
1266 			cp->wscale = select_rcv_wscale();
1267 		else
1268 			cp->wscale = 0;
1269 
1270 		/* ECN */
1271 		if (tcpopt->ecn &&	/* XXX: review. */
1272 		    (s->ecn > 0 || (s->ecn < 0 && V_tcp_do_ecn)))
1273 			cp->ecn = 1;
1274 		else
1275 			cp->ecn = 0;
1276 
1277 		wnd = max(so->sol_sbrcv_hiwat, MIN_RCV_WND);
1278 		cp->opt0_bufsize = min(wnd >> 10, M_RCV_BUFSIZ);
1279 
1280 		if (tt->sndbuf > 0)
1281 			cp->sndbuf = tt->sndbuf;
1282 		else if (so->sol_sbsnd_flags & SB_AUTOSIZE &&
1283 		    V_tcp_do_autosndbuf)
1284 			cp->sndbuf = 256 * 1024;
1285 		else
1286 			cp->sndbuf = so->sol_sbsnd_hiwat;
1287 	} else {
1288 		/* Active open */
1289 
1290 		/* TCP timestamp option */
1291 		if (s->tstamp > 0 ||
1292 		    (s->tstamp < 0 && (tp->t_flags & TF_REQ_TSTMP)))
1293 			cp->tstamp = 1;
1294 		else
1295 			cp->tstamp = 0;
1296 
1297 		/* SACK */
1298 		if (s->sack > 0 ||
1299 		    (s->sack < 0 && (tp->t_flags & TF_SACK_PERMIT)))
1300 			cp->sack = 1;
1301 		else
1302 			cp->sack = 0;
1303 
1304 		/* Receive window scaling */
1305 		if (tp->t_flags & TF_REQ_SCALE)
1306 			cp->wscale = select_rcv_wscale();
1307 		else
1308 			cp->wscale = 0;
1309 
1310 		/* ECN */
1311 		if (s->ecn > 0 || (s->ecn < 0 && V_tcp_do_ecn == 1))
1312 			cp->ecn = 1;
1313 		else
1314 			cp->ecn = 0;
1315 
1316 		SOCKBUF_LOCK(&so->so_rcv);
1317 		wnd = max(select_rcv_wnd(so), MIN_RCV_WND);
1318 		SOCKBUF_UNLOCK(&so->so_rcv);
1319 		cp->opt0_bufsize = min(wnd >> 10, M_RCV_BUFSIZ);
1320 
1321 		if (tt->sndbuf > 0)
1322 			cp->sndbuf = tt->sndbuf;
1323 		else {
1324 			SOCKBUF_LOCK(&so->so_snd);
1325 			if (so->so_snd.sb_flags & SB_AUTOSIZE &&
1326 			    V_tcp_do_autosndbuf)
1327 				cp->sndbuf = 256 * 1024;
1328 			else
1329 				cp->sndbuf = so->so_snd.sb_hiwat;
1330 			SOCKBUF_UNLOCK(&so->so_snd);
1331 		}
1332 	}
1333 
1334 	cp->l2t_idx = l2t_idx;
1335 
1336 	/* This will be initialized on ESTABLISHED. */
1337 	cp->emss = 0;
1338 }
1339 
1340 int
negative_advice(int status)1341 negative_advice(int status)
1342 {
1343 
1344 	return (status == CPL_ERR_RTX_NEG_ADVICE ||
1345 	    status == CPL_ERR_PERSIST_NEG_ADVICE ||
1346 	    status == CPL_ERR_KEEPALV_NEG_ADVICE);
1347 }
1348 
1349 static int
alloc_tid_tab(struct tid_info * t,int flags)1350 alloc_tid_tab(struct tid_info *t, int flags)
1351 {
1352 
1353 	MPASS(t->ntids > 0);
1354 	MPASS(t->tid_tab == NULL);
1355 
1356 	t->tid_tab = malloc(t->ntids * sizeof(*t->tid_tab), M_CXGBE,
1357 	    M_ZERO | flags);
1358 	if (t->tid_tab == NULL)
1359 		return (ENOMEM);
1360 	atomic_store_rel_int(&t->tids_in_use, 0);
1361 
1362 	return (0);
1363 }
1364 
1365 static void
free_tid_tab(struct tid_info * t)1366 free_tid_tab(struct tid_info *t)
1367 {
1368 
1369 	KASSERT(t->tids_in_use == 0,
1370 	    ("%s: %d tids still in use.", __func__, t->tids_in_use));
1371 
1372 	free(t->tid_tab, M_CXGBE);
1373 	t->tid_tab = NULL;
1374 }
1375 
1376 static int
alloc_stid_tab(struct tid_info * t,int flags)1377 alloc_stid_tab(struct tid_info *t, int flags)
1378 {
1379 
1380 	MPASS(t->nstids > 0);
1381 	MPASS(t->stid_tab == NULL);
1382 
1383 	t->stid_tab = malloc(t->nstids * sizeof(*t->stid_tab), M_CXGBE,
1384 	    M_ZERO | flags);
1385 	if (t->stid_tab == NULL)
1386 		return (ENOMEM);
1387 	mtx_init(&t->stid_lock, "stid lock", NULL, MTX_DEF);
1388 	t->stids_in_use = 0;
1389 	TAILQ_INIT(&t->stids);
1390 	t->nstids_free_head = t->nstids;
1391 
1392 	return (0);
1393 }
1394 
1395 static void
free_stid_tab(struct tid_info * t)1396 free_stid_tab(struct tid_info *t)
1397 {
1398 
1399 	KASSERT(t->stids_in_use == 0,
1400 	    ("%s: %d tids still in use.", __func__, t->stids_in_use));
1401 
1402 	if (mtx_initialized(&t->stid_lock))
1403 		mtx_destroy(&t->stid_lock);
1404 	free(t->stid_tab, M_CXGBE);
1405 	t->stid_tab = NULL;
1406 }
1407 
1408 static void
free_tid_tabs(struct tid_info * t)1409 free_tid_tabs(struct tid_info *t)
1410 {
1411 
1412 	free_tid_tab(t);
1413 	free_stid_tab(t);
1414 }
1415 
1416 static int
alloc_tid_tabs(struct tid_info * t)1417 alloc_tid_tabs(struct tid_info *t)
1418 {
1419 	int rc;
1420 
1421 	rc = alloc_tid_tab(t, M_NOWAIT);
1422 	if (rc != 0)
1423 		goto failed;
1424 
1425 	rc = alloc_stid_tab(t, M_NOWAIT);
1426 	if (rc != 0)
1427 		goto failed;
1428 
1429 	return (0);
1430 failed:
1431 	free_tid_tabs(t);
1432 	return (rc);
1433 }
1434 
1435 static inline void
alloc_tcb_history(struct adapter * sc,struct tom_data * td)1436 alloc_tcb_history(struct adapter *sc, struct tom_data *td)
1437 {
1438 
1439 	if (sc->tids.ntids == 0 || sc->tids.ntids > 1024)
1440 		return;
1441 	rw_init(&td->tcb_history_lock, "TCB history");
1442 	td->tcb_history = malloc(sc->tids.ntids * sizeof(*td->tcb_history),
1443 	    M_CXGBE, M_ZERO | M_NOWAIT);
1444 	td->dupack_threshold = G_DUPACKTHRESH(t4_read_reg(sc, A_TP_PARA_REG0));
1445 }
1446 
1447 static inline void
free_tcb_history(struct adapter * sc,struct tom_data * td)1448 free_tcb_history(struct adapter *sc, struct tom_data *td)
1449 {
1450 #ifdef INVARIANTS
1451 	int i;
1452 
1453 	if (td->tcb_history != NULL) {
1454 		for (i = 0; i < sc->tids.ntids; i++) {
1455 			MPASS(td->tcb_history[i] == NULL);
1456 		}
1457 	}
1458 #endif
1459 	free(td->tcb_history, M_CXGBE);
1460 	if (rw_initialized(&td->tcb_history_lock))
1461 		rw_destroy(&td->tcb_history_lock);
1462 }
1463 
1464 static void
free_tom_data(struct adapter * sc,struct tom_data * td)1465 free_tom_data(struct adapter *sc, struct tom_data *td)
1466 {
1467 
1468 	ASSERT_SYNCHRONIZED_OP(sc);
1469 
1470 	KASSERT(TAILQ_EMPTY(&td->toep_list),
1471 	    ("%s: TOE PCB list is not empty.", __func__));
1472 	KASSERT(td->lctx_count == 0,
1473 	    ("%s: lctx hash table is not empty.", __func__));
1474 
1475 	t4_free_ppod_region(&td->pr);
1476 
1477 	if (td->listen_mask != 0)
1478 		hashdestroy(td->listen_hash, M_CXGBE, td->listen_mask);
1479 
1480 	if (mtx_initialized(&td->unsent_wr_lock))
1481 		mtx_destroy(&td->unsent_wr_lock);
1482 	if (mtx_initialized(&td->lctx_hash_lock))
1483 		mtx_destroy(&td->lctx_hash_lock);
1484 	if (mtx_initialized(&td->toep_list_lock))
1485 		mtx_destroy(&td->toep_list_lock);
1486 
1487 	free_tcb_history(sc, td);
1488 	free_tid_tabs(&sc->tids);
1489 	free(td, M_CXGBE);
1490 }
1491 
1492 static char *
prepare_pkt(int open_type,uint16_t vtag,struct inpcb * inp,int * pktlen,int * buflen)1493 prepare_pkt(int open_type, uint16_t vtag, struct inpcb *inp, int *pktlen,
1494     int *buflen)
1495 {
1496 	char *pkt;
1497 	struct tcphdr *th;
1498 	int ipv6, len;
1499 	const int maxlen =
1500 	    max(sizeof(struct ether_header), sizeof(struct ether_vlan_header)) +
1501 	    max(sizeof(struct ip), sizeof(struct ip6_hdr)) +
1502 	    sizeof(struct tcphdr);
1503 
1504 	MPASS(open_type == OPEN_TYPE_ACTIVE || open_type == OPEN_TYPE_LISTEN);
1505 
1506 	pkt = malloc(maxlen, M_CXGBE, M_ZERO | M_NOWAIT);
1507 	if (pkt == NULL)
1508 		return (NULL);
1509 
1510 	ipv6 = inp->inp_vflag & INP_IPV6;
1511 	len = 0;
1512 
1513 	if (EVL_VLANOFTAG(vtag) == 0xfff) {
1514 		struct ether_header *eh = (void *)pkt;
1515 
1516 		if (ipv6)
1517 			eh->ether_type = htons(ETHERTYPE_IPV6);
1518 		else
1519 			eh->ether_type = htons(ETHERTYPE_IP);
1520 
1521 		len += sizeof(*eh);
1522 	} else {
1523 		struct ether_vlan_header *evh = (void *)pkt;
1524 
1525 		evh->evl_encap_proto = htons(ETHERTYPE_VLAN);
1526 		evh->evl_tag = htons(vtag);
1527 		if (ipv6)
1528 			evh->evl_proto = htons(ETHERTYPE_IPV6);
1529 		else
1530 			evh->evl_proto = htons(ETHERTYPE_IP);
1531 
1532 		len += sizeof(*evh);
1533 	}
1534 
1535 	if (ipv6) {
1536 		struct ip6_hdr *ip6 = (void *)&pkt[len];
1537 
1538 		ip6->ip6_vfc = IPV6_VERSION;
1539 		ip6->ip6_plen = htons(sizeof(struct tcphdr));
1540 		ip6->ip6_nxt = IPPROTO_TCP;
1541 		if (open_type == OPEN_TYPE_ACTIVE) {
1542 			ip6->ip6_src = inp->in6p_laddr;
1543 			ip6->ip6_dst = inp->in6p_faddr;
1544 		} else if (open_type == OPEN_TYPE_LISTEN) {
1545 			ip6->ip6_src = inp->in6p_laddr;
1546 			ip6->ip6_dst = ip6->ip6_src;
1547 		}
1548 
1549 		len += sizeof(*ip6);
1550 	} else {
1551 		struct ip *ip = (void *)&pkt[len];
1552 
1553 		ip->ip_v = IPVERSION;
1554 		ip->ip_hl = sizeof(*ip) >> 2;
1555 		ip->ip_tos = inp->inp_ip_tos;
1556 		ip->ip_len = htons(sizeof(struct ip) + sizeof(struct tcphdr));
1557 		ip->ip_ttl = inp->inp_ip_ttl;
1558 		ip->ip_p = IPPROTO_TCP;
1559 		if (open_type == OPEN_TYPE_ACTIVE) {
1560 			ip->ip_src = inp->inp_laddr;
1561 			ip->ip_dst = inp->inp_faddr;
1562 		} else if (open_type == OPEN_TYPE_LISTEN) {
1563 			ip->ip_src = inp->inp_laddr;
1564 			ip->ip_dst = ip->ip_src;
1565 		}
1566 
1567 		len += sizeof(*ip);
1568 	}
1569 
1570 	th = (void *)&pkt[len];
1571 	if (open_type == OPEN_TYPE_ACTIVE) {
1572 		th->th_sport = inp->inp_lport;	/* network byte order already */
1573 		th->th_dport = inp->inp_fport;	/* ditto */
1574 	} else if (open_type == OPEN_TYPE_LISTEN) {
1575 		th->th_sport = inp->inp_lport;	/* network byte order already */
1576 		th->th_dport = th->th_sport;
1577 	}
1578 	len += sizeof(th);
1579 
1580 	*pktlen = *buflen = len;
1581 	return (pkt);
1582 }
1583 
1584 const struct offload_settings *
lookup_offload_policy(struct adapter * sc,int open_type,struct mbuf * m,uint16_t vtag,struct inpcb * inp)1585 lookup_offload_policy(struct adapter *sc, int open_type, struct mbuf *m,
1586     uint16_t vtag, struct inpcb *inp)
1587 {
1588 	const struct t4_offload_policy *op;
1589 	char *pkt;
1590 	struct offload_rule *r;
1591 	int i, matched, pktlen, buflen;
1592 	static const struct offload_settings allow_offloading_settings = {
1593 		.offload = 1,
1594 		.rx_coalesce = -1,
1595 		.cong_algo = -1,
1596 		.sched_class = -1,
1597 		.tstamp = -1,
1598 		.sack = -1,
1599 		.nagle = -1,
1600 		.ecn = -1,
1601 		.ddp = -1,
1602 		.tls = -1,
1603 		.txq = QUEUE_RANDOM,
1604 		.rxq = QUEUE_RANDOM,
1605 		.mss = -1,
1606 	};
1607 	static const struct offload_settings disallow_offloading_settings = {
1608 		.offload = 0,
1609 		/* rest is irrelevant when offload is off. */
1610 	};
1611 
1612 	rw_assert(&sc->policy_lock, RA_LOCKED);
1613 
1614 	/*
1615 	 * If there's no Connection Offloading Policy attached to the device
1616 	 * then we need to return a default static policy.  If
1617 	 * "cop_managed_offloading" is true, then we need to disallow
1618 	 * offloading until a COP is attached to the device.  Otherwise we
1619 	 * allow offloading ...
1620 	 */
1621 	op = sc->policy;
1622 	if (op == NULL) {
1623 		if (sc->tt.cop_managed_offloading)
1624 			return (&disallow_offloading_settings);
1625 		else
1626 			return (&allow_offloading_settings);
1627 	}
1628 
1629 	switch (open_type) {
1630 	case OPEN_TYPE_ACTIVE:
1631 	case OPEN_TYPE_LISTEN:
1632 		pkt = prepare_pkt(open_type, vtag, inp, &pktlen, &buflen);
1633 		break;
1634 	case OPEN_TYPE_PASSIVE:
1635 		MPASS(m != NULL);
1636 		pkt = mtod(m, char *);
1637 		MPASS(*pkt == CPL_PASS_ACCEPT_REQ);
1638 		pkt += sizeof(struct cpl_pass_accept_req);
1639 		pktlen = m->m_pkthdr.len - sizeof(struct cpl_pass_accept_req);
1640 		buflen = m->m_len - sizeof(struct cpl_pass_accept_req);
1641 		break;
1642 	default:
1643 		MPASS(0);
1644 		return (&disallow_offloading_settings);
1645 	}
1646 
1647 	if (pkt == NULL || pktlen == 0 || buflen == 0)
1648 		return (&disallow_offloading_settings);
1649 
1650 	matched = 0;
1651 	r = &op->rule[0];
1652 	for (i = 0; i < op->nrules; i++, r++) {
1653 		if (r->open_type != open_type &&
1654 		    r->open_type != OPEN_TYPE_DONTCARE) {
1655 			continue;
1656 		}
1657 		matched = bpf_filter(r->bpf_prog.bf_insns, pkt, pktlen, buflen);
1658 		if (matched)
1659 			break;
1660 	}
1661 
1662 	if (open_type == OPEN_TYPE_ACTIVE || open_type == OPEN_TYPE_LISTEN)
1663 		free(pkt, M_CXGBE);
1664 
1665 	return (matched ? &r->settings : &disallow_offloading_settings);
1666 }
1667 
1668 static void
reclaim_wr_resources(void * arg,int count)1669 reclaim_wr_resources(void *arg, int count)
1670 {
1671 	struct tom_data *td = arg;
1672 	STAILQ_HEAD(, wrqe) twr_list = STAILQ_HEAD_INITIALIZER(twr_list);
1673 	struct cpl_act_open_req *cpl;
1674 	u_int opcode, atid, tid;
1675 	struct wrqe *wr;
1676 	struct adapter *sc = td_adapter(td);
1677 
1678 	mtx_lock(&td->unsent_wr_lock);
1679 	STAILQ_SWAP(&td->unsent_wr_list, &twr_list, wrqe);
1680 	mtx_unlock(&td->unsent_wr_lock);
1681 
1682 	while ((wr = STAILQ_FIRST(&twr_list)) != NULL) {
1683 		STAILQ_REMOVE_HEAD(&twr_list, link);
1684 
1685 		cpl = wrtod(wr);
1686 		opcode = GET_OPCODE(cpl);
1687 
1688 		switch (opcode) {
1689 		case CPL_ACT_OPEN_REQ:
1690 		case CPL_ACT_OPEN_REQ6:
1691 			atid = G_TID_TID(be32toh(OPCODE_TID(cpl)));
1692 			CTR2(KTR_CXGBE, "%s: atid %u ", __func__, atid);
1693 			act_open_failure_cleanup(sc, atid, EHOSTUNREACH);
1694 			free(wr, M_CXGBE);
1695 			break;
1696 		case CPL_PASS_ACCEPT_RPL:
1697 			tid = GET_TID(cpl);
1698 			CTR2(KTR_CXGBE, "%s: tid %u ", __func__, tid);
1699 			synack_failure_cleanup(sc, tid);
1700 			free(wr, M_CXGBE);
1701 			break;
1702 		default:
1703 			log(LOG_ERR, "%s: leaked work request %p, wr_len %d, "
1704 			    "opcode %x\n", __func__, wr, wr->wr_len, opcode);
1705 			/* WR not freed here; go look at it with a debugger.  */
1706 		}
1707 	}
1708 }
1709 
1710 /*
1711  * Ground control to Major TOM
1712  * Commencing countdown, engines on
1713  */
1714 static int
t4_tom_activate(struct adapter * sc)1715 t4_tom_activate(struct adapter *sc)
1716 {
1717 	struct tom_data *td;
1718 	struct toedev *tod;
1719 	struct vi_info *vi;
1720 	int i, rc, v;
1721 
1722 	ASSERT_SYNCHRONIZED_OP(sc);
1723 
1724 	/* per-adapter softc for TOM */
1725 	td = malloc(sizeof(*td), M_CXGBE, M_ZERO | M_NOWAIT);
1726 	if (td == NULL)
1727 		return (ENOMEM);
1728 
1729 	/* List of TOE PCBs and associated lock */
1730 	mtx_init(&td->toep_list_lock, "PCB list lock", NULL, MTX_DEF);
1731 	TAILQ_INIT(&td->toep_list);
1732 
1733 	/* Listen context */
1734 	mtx_init(&td->lctx_hash_lock, "lctx hash lock", NULL, MTX_DEF);
1735 	td->listen_hash = hashinit_flags(LISTEN_HASH_SIZE, M_CXGBE,
1736 	    &td->listen_mask, HASH_NOWAIT);
1737 
1738 	/* List of WRs for which L2 resolution failed */
1739 	mtx_init(&td->unsent_wr_lock, "Unsent WR list lock", NULL, MTX_DEF);
1740 	STAILQ_INIT(&td->unsent_wr_list);
1741 	TASK_INIT(&td->reclaim_wr_resources, 0, reclaim_wr_resources, td);
1742 
1743 	/* TID tables */
1744 	rc = alloc_tid_tabs(&sc->tids);
1745 	if (rc != 0)
1746 		goto done;
1747 
1748 	rc = t4_init_ppod_region(&td->pr, &sc->vres.ddp,
1749 	    t4_read_reg(sc, A_ULP_RX_TDDP_PSZ), "TDDP page pods");
1750 	if (rc != 0)
1751 		goto done;
1752 	t4_set_reg_field(sc, A_ULP_RX_TDDP_TAGMASK,
1753 	    V_TDDPTAGMASK(M_TDDPTAGMASK), td->pr.pr_tag_mask);
1754 
1755 	alloc_tcb_history(sc, td);
1756 
1757 	/* toedev ops */
1758 	tod = &td->tod;
1759 	init_toedev(tod);
1760 	tod->tod_softc = sc;
1761 	tod->tod_connect = t4_connect;
1762 	tod->tod_listen_start = t4_listen_start;
1763 	tod->tod_listen_stop = t4_listen_stop;
1764 	tod->tod_rcvd = t4_rcvd;
1765 	tod->tod_output = t4_tod_output;
1766 	tod->tod_send_rst = t4_send_rst;
1767 	tod->tod_send_fin = t4_send_fin;
1768 	tod->tod_pcb_detach = t4_pcb_detach;
1769 	tod->tod_l2_update = t4_l2_update;
1770 	tod->tod_syncache_added = t4_syncache_added;
1771 	tod->tod_syncache_removed = t4_syncache_removed;
1772 	tod->tod_syncache_respond = t4_syncache_respond;
1773 	tod->tod_offload_socket = t4_offload_socket;
1774 	tod->tod_ctloutput = t4_ctloutput;
1775 	tod->tod_tcp_info = t4_tcp_info;
1776 #ifdef KERN_TLS
1777 	tod->tod_alloc_tls_session = t4_alloc_tls_session;
1778 #endif
1779 
1780 	for_each_port(sc, i) {
1781 		for_each_vi(sc->port[i], v, vi) {
1782 			TOEDEV(vi->ifp) = &td->tod;
1783 		}
1784 	}
1785 
1786 	sc->tom_softc = td;
1787 	register_toedev(sc->tom_softc);
1788 
1789 done:
1790 	if (rc != 0)
1791 		free_tom_data(sc, td);
1792 	return (rc);
1793 }
1794 
1795 static int
t4_tom_deactivate(struct adapter * sc)1796 t4_tom_deactivate(struct adapter *sc)
1797 {
1798 	int rc = 0;
1799 	struct tom_data *td = sc->tom_softc;
1800 
1801 	ASSERT_SYNCHRONIZED_OP(sc);
1802 
1803 	if (td == NULL)
1804 		return (0);	/* XXX. KASSERT? */
1805 
1806 	if (sc->offload_map != 0)
1807 		return (EBUSY);	/* at least one port has IFCAP_TOE enabled */
1808 
1809 	if (uld_active(sc, ULD_IWARP) || uld_active(sc, ULD_ISCSI))
1810 		return (EBUSY);	/* both iWARP and iSCSI rely on the TOE. */
1811 
1812 	mtx_lock(&td->toep_list_lock);
1813 	if (!TAILQ_EMPTY(&td->toep_list))
1814 		rc = EBUSY;
1815 	mtx_unlock(&td->toep_list_lock);
1816 
1817 	mtx_lock(&td->lctx_hash_lock);
1818 	if (td->lctx_count > 0)
1819 		rc = EBUSY;
1820 	mtx_unlock(&td->lctx_hash_lock);
1821 
1822 	taskqueue_drain(taskqueue_thread, &td->reclaim_wr_resources);
1823 	mtx_lock(&td->unsent_wr_lock);
1824 	if (!STAILQ_EMPTY(&td->unsent_wr_list))
1825 		rc = EBUSY;
1826 	mtx_unlock(&td->unsent_wr_lock);
1827 
1828 	if (rc == 0) {
1829 		unregister_toedev(sc->tom_softc);
1830 		free_tom_data(sc, td);
1831 		sc->tom_softc = NULL;
1832 	}
1833 
1834 	return (rc);
1835 }
1836 
1837 static int
t4_aio_queue_tom(struct socket * so,struct kaiocb * job)1838 t4_aio_queue_tom(struct socket *so, struct kaiocb *job)
1839 {
1840 	struct tcpcb *tp = so_sototcpcb(so);
1841 	struct toepcb *toep = tp->t_toe;
1842 	int error;
1843 
1844 	if (ulp_mode(toep) == ULP_MODE_TCPDDP) {
1845 		error = t4_aio_queue_ddp(so, job);
1846 		if (error != EOPNOTSUPP)
1847 			return (error);
1848 	}
1849 
1850 	return (t4_aio_queue_aiotx(so, job));
1851 }
1852 
1853 static int
t4_tom_mod_load(void)1854 t4_tom_mod_load(void)
1855 {
1856 	/* CPL handlers */
1857 	t4_register_cpl_handler(CPL_GET_TCB_RPL, do_get_tcb_rpl);
1858 	t4_register_shared_cpl_handler(CPL_L2T_WRITE_RPL, do_l2t_write_rpl2,
1859 	    CPL_COOKIE_TOM);
1860 	t4_init_connect_cpl_handlers();
1861 	t4_init_listen_cpl_handlers();
1862 	t4_init_cpl_io_handlers();
1863 
1864 	t4_ddp_mod_load();
1865 	t4_tls_mod_load();
1866 
1867 	tcp_protosw = pffindproto(PF_INET, IPPROTO_TCP, SOCK_STREAM);
1868 	if (tcp_protosw == NULL)
1869 		return (ENOPROTOOPT);
1870 	bcopy(tcp_protosw, &toe_protosw, sizeof(toe_protosw));
1871 	bcopy(tcp_protosw->pr_usrreqs, &toe_usrreqs, sizeof(toe_usrreqs));
1872 	toe_usrreqs.pru_aio_queue = t4_aio_queue_tom;
1873 	toe_protosw.pr_usrreqs = &toe_usrreqs;
1874 
1875 	tcp6_protosw = pffindproto(PF_INET6, IPPROTO_TCP, SOCK_STREAM);
1876 	if (tcp6_protosw == NULL)
1877 		return (ENOPROTOOPT);
1878 	bcopy(tcp6_protosw, &toe6_protosw, sizeof(toe6_protosw));
1879 	bcopy(tcp6_protosw->pr_usrreqs, &toe6_usrreqs, sizeof(toe6_usrreqs));
1880 	toe6_usrreqs.pru_aio_queue = t4_aio_queue_tom;
1881 	toe6_protosw.pr_usrreqs = &toe6_usrreqs;
1882 
1883 	return (t4_register_uld(&tom_uld_info));
1884 }
1885 
1886 static void
tom_uninit(struct adapter * sc,void * arg __unused)1887 tom_uninit(struct adapter *sc, void *arg __unused)
1888 {
1889 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4tomun"))
1890 		return;
1891 
1892 	/* Try to free resources (works only if no port has IFCAP_TOE) */
1893 	if (uld_active(sc, ULD_TOM))
1894 		t4_deactivate_uld(sc, ULD_TOM);
1895 
1896 	end_synchronized_op(sc, 0);
1897 }
1898 
1899 static int
t4_tom_mod_unload(void)1900 t4_tom_mod_unload(void)
1901 {
1902 	t4_iterate(tom_uninit, NULL);
1903 
1904 	if (t4_unregister_uld(&tom_uld_info) == EBUSY)
1905 		return (EBUSY);
1906 
1907 	t4_tls_mod_unload();
1908 	t4_ddp_mod_unload();
1909 
1910 	t4_uninit_connect_cpl_handlers();
1911 	t4_uninit_listen_cpl_handlers();
1912 	t4_uninit_cpl_io_handlers();
1913 	t4_register_shared_cpl_handler(CPL_L2T_WRITE_RPL, NULL, CPL_COOKIE_TOM);
1914 	t4_register_cpl_handler(CPL_GET_TCB_RPL, NULL);
1915 
1916 	return (0);
1917 }
1918 #endif	/* TCP_OFFLOAD */
1919 
1920 static int
t4_tom_modevent(module_t mod,int cmd,void * arg)1921 t4_tom_modevent(module_t mod, int cmd, void *arg)
1922 {
1923 	int rc = 0;
1924 
1925 #ifdef TCP_OFFLOAD
1926 	switch (cmd) {
1927 	case MOD_LOAD:
1928 		rc = t4_tom_mod_load();
1929 		break;
1930 
1931 	case MOD_UNLOAD:
1932 		rc = t4_tom_mod_unload();
1933 		break;
1934 
1935 	default:
1936 		rc = EINVAL;
1937 	}
1938 #else
1939 	printf("t4_tom: compiled without TCP_OFFLOAD support.\n");
1940 	rc = EOPNOTSUPP;
1941 #endif
1942 	return (rc);
1943 }
1944 
1945 static moduledata_t t4_tom_moddata= {
1946 	"t4_tom",
1947 	t4_tom_modevent,
1948 	0
1949 };
1950 
1951 MODULE_VERSION(t4_tom, 1);
1952 MODULE_DEPEND(t4_tom, toecore, 1, 1, 1);
1953 MODULE_DEPEND(t4_tom, t4nex, 1, 1, 1);
1954 DECLARE_MODULE(t4_tom, t4_tom_moddata, SI_SUB_EXEC, SI_ORDER_ANY);
1955