xref: /freebsd/sys/dev/cxgbe/tom/t4_tom.c (revision 96190b4fef3b4a0cc3ca0606b0c4e3e69a5e6717)
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_seq.h>
65 #include <netinet/tcp_timer.h>
66 #include <netinet/tcp_var.h>
67 #include <netinet/toecore.h>
68 #include <netinet/cc/cc.h>
69 
70 #ifdef TCP_OFFLOAD
71 #include "common/common.h"
72 #include "common/t4_msg.h"
73 #include "common/t4_regs.h"
74 #include "common/t4_regs_values.h"
75 #include "common/t4_tcb.h"
76 #include "t4_clip.h"
77 #include "tom/t4_tom_l2t.h"
78 #include "tom/t4_tom.h"
79 #include "tom/t4_tls.h"
80 
81 static struct protosw toe_protosw;
82 static struct protosw toe6_protosw;
83 
84 /* Module ops */
85 static int t4_tom_mod_load(void);
86 static int t4_tom_mod_unload(void);
87 static int t4_tom_modevent(module_t, int, void *);
88 
89 /* ULD ops and helpers */
90 static int t4_tom_activate(struct adapter *);
91 static int t4_tom_deactivate(struct adapter *);
92 static int t4_tom_stop(struct adapter *);
93 static int t4_tom_restart(struct adapter *);
94 
95 static struct uld_info tom_uld_info = {
96 	.uld_activate = t4_tom_activate,
97 	.uld_deactivate = t4_tom_deactivate,
98 	.uld_stop = t4_tom_stop,
99 	.uld_restart = t4_tom_restart,
100 };
101 
102 static void release_offload_resources(struct toepcb *);
103 static void done_with_toepcb(struct toepcb *);
104 static int alloc_tid_tabs(struct adapter *);
105 static void free_tid_tabs(struct adapter *);
106 static void free_tom_data(struct adapter *, struct tom_data *);
107 static void reclaim_wr_resources(void *, int);
108 static void cleanup_stranded_tids(void *, int);
109 
110 struct toepcb *
111 alloc_toepcb(struct vi_info *vi, int flags)
112 {
113 	struct port_info *pi = vi->pi;
114 	struct adapter *sc = pi->adapter;
115 	struct toepcb *toep;
116 	int tx_credits, txsd_total, len;
117 
118 	/*
119 	 * The firmware counts tx work request credits in units of 16 bytes
120 	 * each.  Reserve room for an ABORT_REQ so the driver never has to worry
121 	 * about tx credits if it wants to abort a connection.
122 	 */
123 	tx_credits = sc->params.ofldq_wr_cred;
124 	tx_credits -= howmany(sizeof(struct cpl_abort_req), 16);
125 
126 	/*
127 	 * Shortest possible tx work request is a fw_ofld_tx_data_wr + 1 byte
128 	 * immediate payload, and firmware counts tx work request credits in
129 	 * units of 16 byte.  Calculate the maximum work requests possible.
130 	 */
131 	txsd_total = tx_credits /
132 	    howmany(sizeof(struct fw_ofld_tx_data_wr) + 1, 16);
133 
134 	len = offsetof(struct toepcb, txsd) +
135 	    txsd_total * sizeof(struct ofld_tx_sdesc);
136 
137 	toep = malloc(len, M_CXGBE, M_ZERO | flags);
138 	if (toep == NULL)
139 		return (NULL);
140 
141 	refcount_init(&toep->refcount, 1);
142 	toep->td = sc->tom_softc;
143 	toep->incarnation = sc->incarnation;
144 	toep->vi = vi;
145 	toep->tid = -1;
146 	toep->tx_total = tx_credits;
147 	toep->tx_credits = tx_credits;
148 	mbufq_init(&toep->ulp_pduq, INT_MAX);
149 	mbufq_init(&toep->ulp_pdu_reclaimq, INT_MAX);
150 	toep->txsd_total = txsd_total;
151 	toep->txsd_avail = txsd_total;
152 	toep->txsd_pidx = 0;
153 	toep->txsd_cidx = 0;
154 	aiotx_init_toep(toep);
155 
156 	return (toep);
157 }
158 
159 /*
160  * Initialize a toepcb after its params have been filled out.
161  */
162 int
163 init_toepcb(struct vi_info *vi, struct toepcb *toep)
164 {
165 	struct conn_params *cp = &toep->params;
166 	struct port_info *pi = vi->pi;
167 	struct adapter *sc = pi->adapter;
168 	struct tx_cl_rl_params *tc;
169 
170 	if (cp->tc_idx >= 0 && cp->tc_idx < sc->params.nsched_cls) {
171 		tc = &pi->sched_params->cl_rl[cp->tc_idx];
172 		mtx_lock(&sc->tc_lock);
173 		if (tc->state != CS_HW_CONFIGURED) {
174 			CH_ERR(vi, "tid %d cannot be bound to traffic class %d "
175 			    "because it is not configured (its state is %d)\n",
176 			    toep->tid, cp->tc_idx, tc->state);
177 			cp->tc_idx = -1;
178 		} else {
179 			tc->refcount++;
180 		}
181 		mtx_unlock(&sc->tc_lock);
182 	}
183 	toep->ofld_txq = &sc->sge.ofld_txq[cp->txq_idx];
184 	toep->ofld_rxq = &sc->sge.ofld_rxq[cp->rxq_idx];
185 	toep->ctrlq = &sc->sge.ctrlq[pi->port_id];
186 
187 	tls_init_toep(toep);
188 	MPASS(ulp_mode(toep) != ULP_MODE_TCPDDP);
189 
190 	toep->flags |= TPF_INITIALIZED;
191 
192 	return (0);
193 }
194 
195 struct toepcb *
196 hold_toepcb(struct toepcb *toep)
197 {
198 
199 	refcount_acquire(&toep->refcount);
200 	return (toep);
201 }
202 
203 void
204 free_toepcb(struct toepcb *toep)
205 {
206 
207 	if (refcount_release(&toep->refcount) == 0)
208 		return;
209 
210 	KASSERT(!(toep->flags & TPF_ATTACHED),
211 	    ("%s: attached to an inpcb", __func__));
212 	KASSERT(!(toep->flags & TPF_CPL_PENDING),
213 	    ("%s: CPL pending", __func__));
214 
215 	if (toep->flags & TPF_INITIALIZED) {
216 		if (ulp_mode(toep) == ULP_MODE_TCPDDP)
217 			ddp_uninit_toep(toep);
218 		tls_uninit_toep(toep);
219 	}
220 	free(toep, M_CXGBE);
221 }
222 
223 /*
224  * Set up the socket for TCP offload.
225  */
226 void
227 offload_socket(struct socket *so, struct toepcb *toep)
228 {
229 	struct tom_data *td = toep->td;
230 	struct inpcb *inp = sotoinpcb(so);
231 	struct tcpcb *tp = intotcpcb(inp);
232 	struct sockbuf *sb;
233 
234 	INP_WLOCK_ASSERT(inp);
235 
236 	/* Update socket */
237 	sb = &so->so_snd;
238 	SOCKBUF_LOCK(sb);
239 	sb->sb_flags |= SB_NOCOALESCE;
240 	SOCKBUF_UNLOCK(sb);
241 	sb = &so->so_rcv;
242 	SOCKBUF_LOCK(sb);
243 	sb->sb_flags |= SB_NOCOALESCE;
244 	if (inp->inp_vflag & INP_IPV6)
245 		so->so_proto = &toe6_protosw;
246 	else
247 		so->so_proto = &toe_protosw;
248 	SOCKBUF_UNLOCK(sb);
249 
250 	/* Update TCP PCB */
251 	tp->tod = &td->tod;
252 	tp->t_toe = toep;
253 	tp->t_flags |= TF_TOE;
254 
255 	/* Install an extra hold on inp */
256 	toep->inp = inp;
257 	toep->flags |= TPF_ATTACHED;
258 	in_pcbref(inp);
259 }
260 
261 void
262 restore_so_proto(struct socket *so, bool v6)
263 {
264 	if (v6)
265 		so->so_proto = &tcp6_protosw;
266 	else
267 		so->so_proto = &tcp_protosw;
268 }
269 
270 /* This is _not_ the normal way to "unoffload" a socket. */
271 void
272 undo_offload_socket(struct socket *so)
273 {
274 	struct inpcb *inp = sotoinpcb(so);
275 	struct tcpcb *tp = intotcpcb(inp);
276 	struct toepcb *toep = tp->t_toe;
277 	struct sockbuf *sb;
278 
279 	INP_WLOCK_ASSERT(inp);
280 
281 	sb = &so->so_snd;
282 	SOCKBUF_LOCK(sb);
283 	sb->sb_flags &= ~SB_NOCOALESCE;
284 	SOCKBUF_UNLOCK(sb);
285 	sb = &so->so_rcv;
286 	SOCKBUF_LOCK(sb);
287 	sb->sb_flags &= ~SB_NOCOALESCE;
288 	restore_so_proto(so, inp->inp_vflag & INP_IPV6);
289 	SOCKBUF_UNLOCK(sb);
290 
291 	tp->tod = NULL;
292 	tp->t_toe = NULL;
293 	tp->t_flags &= ~TF_TOE;
294 
295 	toep->inp = NULL;
296 	toep->flags &= ~TPF_ATTACHED;
297 	if (in_pcbrele_wlocked(inp))
298 		panic("%s: inp freed.", __func__);
299 }
300 
301 static void
302 release_offload_resources(struct toepcb *toep)
303 {
304 	struct tom_data *td = toep->td;
305 	struct adapter *sc = td_adapter(td);
306 	int tid = toep->tid;
307 
308 	KASSERT(!(toep->flags & TPF_CPL_PENDING),
309 	    ("%s: %p has CPL pending.", __func__, toep));
310 
311 	CTR5(KTR_CXGBE, "%s: toep %p (tid %d, l2te %p, ce %p)",
312 	    __func__, toep, tid, toep->l2te, toep->ce);
313 
314 	if (toep->l2te) {
315 		t4_l2t_release(toep->l2te);
316 		toep->l2te = NULL;
317 	}
318 	if (tid >= 0) {
319 		remove_tid(sc, tid, toep->ce ? 2 : 1);
320 		release_tid(sc, tid, toep->ctrlq);
321 		toep->tid = -1;
322 		mtx_lock(&td->toep_list_lock);
323 		if (toep->flags & TPF_IN_TOEP_LIST) {
324 			toep->flags &= ~TPF_IN_TOEP_LIST;
325 			TAILQ_REMOVE(&td->toep_list, toep, link);
326 		}
327 		mtx_unlock(&td->toep_list_lock);
328 	}
329 	if (toep->ce) {
330 		t4_release_clip_entry(sc, toep->ce);
331 		toep->ce = NULL;
332 	}
333 	if (toep->params.tc_idx != -1)
334 		t4_release_cl_rl(sc, toep->vi->pi->port_id, toep->params.tc_idx);
335 }
336 
337 /*
338  * Both the driver and kernel are done with the toepcb.
339  */
340 static void
341 done_with_toepcb(struct toepcb *toep)
342 {
343 	KASSERT(!(toep->flags & TPF_CPL_PENDING),
344 	    ("%s: %p has CPL pending.", __func__, toep));
345 	KASSERT(!(toep->flags & TPF_ATTACHED),
346 	    ("%s: %p is still attached.", __func__, toep));
347 
348 	CTR(KTR_CXGBE, "%s: toep %p (0x%x)", __func__, toep, toep->flags);
349 
350 	/*
351 	 * These queues should have been emptied at approximately the same time
352 	 * that a normal connection's socket's so_snd would have been purged or
353 	 * drained.  Do _not_ clean up here.
354 	 */
355 	MPASS(mbufq_empty(&toep->ulp_pduq));
356 	MPASS(mbufq_empty(&toep->ulp_pdu_reclaimq));
357 #ifdef INVARIANTS
358 	if (ulp_mode(toep) == ULP_MODE_TCPDDP)
359 		ddp_assert_empty(toep);
360 #endif
361 	MPASS(TAILQ_EMPTY(&toep->aiotx_jobq));
362 	MPASS(toep->tid == -1);
363 	MPASS(toep->l2te == NULL);
364 	MPASS(toep->ce == NULL);
365 	MPASS((toep->flags & TPF_IN_TOEP_LIST) == 0);
366 
367 	free_toepcb(toep);
368 }
369 
370 /*
371  * The kernel is done with the TCP PCB and this is our opportunity to unhook the
372  * toepcb hanging off of it.  If the TOE driver is also done with the toepcb (no
373  * pending CPL) then it is time to release all resources tied to the toepcb.
374  *
375  * Also gets called when an offloaded active open fails and the TOM wants the
376  * kernel to take the TCP PCB back.
377  */
378 void
379 t4_pcb_detach(struct toedev *tod __unused, struct tcpcb *tp)
380 {
381 #if defined(KTR) || defined(INVARIANTS)
382 	struct inpcb *inp = tptoinpcb(tp);
383 #endif
384 	struct toepcb *toep = tp->t_toe;
385 
386 	INP_WLOCK_ASSERT(inp);
387 
388 	KASSERT(toep != NULL, ("%s: toep is NULL", __func__));
389 	KASSERT(toep->flags & TPF_ATTACHED,
390 	    ("%s: not attached", __func__));
391 
392 #ifdef KTR
393 	if (tp->t_state == TCPS_SYN_SENT) {
394 		CTR6(KTR_CXGBE, "%s: atid %d, toep %p (0x%x), inp %p (0x%x)",
395 		    __func__, toep->tid, toep, toep->flags, inp,
396 		    inp->inp_flags);
397 	} else {
398 		CTR6(KTR_CXGBE,
399 		    "t4_pcb_detach: tid %d (%s), toep %p (0x%x), inp %p (0x%x)",
400 		    toep->tid, tcpstates[tp->t_state], toep, toep->flags, inp,
401 		    inp->inp_flags);
402 	}
403 #endif
404 
405 	tp->tod = NULL;
406 	tp->t_toe = NULL;
407 	tp->t_flags &= ~TF_TOE;
408 	toep->flags &= ~TPF_ATTACHED;
409 
410 	if (!(toep->flags & TPF_CPL_PENDING))
411 		done_with_toepcb(toep);
412 }
413 
414 /*
415  * setsockopt handler.
416  */
417 static void
418 t4_ctloutput(struct toedev *tod, struct tcpcb *tp, int dir, int name)
419 {
420 	struct adapter *sc = tod->tod_softc;
421 	struct toepcb *toep = tp->t_toe;
422 
423 	if (dir == SOPT_GET)
424 		return;
425 
426 	CTR4(KTR_CXGBE, "%s: tp %p, dir %u, name %u", __func__, tp, dir, name);
427 
428 	switch (name) {
429 	case TCP_NODELAY:
430 		if (tp->t_state != TCPS_ESTABLISHED)
431 			break;
432 		toep->params.nagle = tp->t_flags & TF_NODELAY ? 0 : 1;
433 		t4_set_tcb_field(sc, toep->ctrlq, toep, W_TCB_T_FLAGS,
434 		    V_TF_NAGLE(1), V_TF_NAGLE(toep->params.nagle), 0, 0);
435 		break;
436 	default:
437 		break;
438 	}
439 }
440 
441 static inline uint64_t
442 get_tcb_tflags(const uint64_t *tcb)
443 {
444 
445 	return ((be64toh(tcb[14]) << 32) | (be64toh(tcb[15]) >> 32));
446 }
447 
448 static inline uint32_t
449 get_tcb_field(const uint64_t *tcb, u_int word, uint32_t mask, u_int shift)
450 {
451 #define LAST_WORD ((TCB_SIZE / 4) - 1)
452 	uint64_t t1, t2;
453 	int flit_idx;
454 
455 	MPASS(mask != 0);
456 	MPASS(word <= LAST_WORD);
457 	MPASS(shift < 32);
458 
459 	flit_idx = (LAST_WORD - word) / 2;
460 	if (word & 0x1)
461 		shift += 32;
462 	t1 = be64toh(tcb[flit_idx]) >> shift;
463 	t2 = 0;
464 	if (fls(mask) > 64 - shift) {
465 		/*
466 		 * Will spill over into the next logical flit, which is the flit
467 		 * before this one.  The flit_idx before this one must be valid.
468 		 */
469 		MPASS(flit_idx > 0);
470 		t2 = be64toh(tcb[flit_idx - 1]) << (64 - shift);
471 	}
472 	return ((t2 | t1) & mask);
473 #undef LAST_WORD
474 }
475 #define GET_TCB_FIELD(tcb, F) \
476     get_tcb_field(tcb, W_TCB_##F, M_TCB_##F, S_TCB_##F)
477 
478 /*
479  * Issues a CPL_GET_TCB to read the entire TCB for the tid.
480  */
481 static int
482 send_get_tcb(struct adapter *sc, u_int tid)
483 {
484 	struct cpl_get_tcb *cpl;
485 	struct wrq_cookie cookie;
486 
487 	MPASS(tid >= sc->tids.tid_base);
488 	MPASS(tid - sc->tids.tid_base < sc->tids.ntids);
489 
490 	cpl = start_wrq_wr(&sc->sge.ctrlq[0], howmany(sizeof(*cpl), 16),
491 	    &cookie);
492 	if (__predict_false(cpl == NULL))
493 		return (ENOMEM);
494 	bzero(cpl, sizeof(*cpl));
495 	INIT_TP_WR(cpl, tid);
496 	OPCODE_TID(cpl) = htobe32(MK_OPCODE_TID(CPL_GET_TCB, tid));
497 	cpl->reply_ctrl = htobe16(V_REPLY_CHAN(0) |
498 	    V_QUEUENO(sc->sge.ofld_rxq[0].iq.cntxt_id));
499 	cpl->cookie = 0xff;
500 	commit_wrq_wr(&sc->sge.ctrlq[0], cpl, &cookie);
501 
502 	return (0);
503 }
504 
505 static struct tcb_histent *
506 alloc_tcb_histent(struct adapter *sc, u_int tid, int flags)
507 {
508 	struct tcb_histent *te;
509 
510 	MPASS(flags == M_NOWAIT || flags == M_WAITOK);
511 
512 	te = malloc(sizeof(*te), M_CXGBE, M_ZERO | flags);
513 	if (te == NULL)
514 		return (NULL);
515 	mtx_init(&te->te_lock, "TCB entry", NULL, MTX_DEF);
516 	callout_init_mtx(&te->te_callout, &te->te_lock, 0);
517 	te->te_adapter = sc;
518 	te->te_tid = tid;
519 
520 	return (te);
521 }
522 
523 static void
524 free_tcb_histent(struct tcb_histent *te)
525 {
526 
527 	mtx_destroy(&te->te_lock);
528 	free(te, M_CXGBE);
529 }
530 
531 /*
532  * Start tracking the tid in the TCB history.
533  */
534 int
535 add_tid_to_history(struct adapter *sc, u_int tid)
536 {
537 	struct tcb_histent *te = NULL;
538 	struct tom_data *td = sc->tom_softc;
539 	int rc;
540 
541 	MPASS(tid >= sc->tids.tid_base);
542 	MPASS(tid - sc->tids.tid_base < sc->tids.ntids);
543 
544 	if (td->tcb_history == NULL)
545 		return (ENXIO);
546 
547 	rw_wlock(&td->tcb_history_lock);
548 	if (td->tcb_history[tid] != NULL) {
549 		rc = EEXIST;
550 		goto done;
551 	}
552 	te = alloc_tcb_histent(sc, tid, M_NOWAIT);
553 	if (te == NULL) {
554 		rc = ENOMEM;
555 		goto done;
556 	}
557 	mtx_lock(&te->te_lock);
558 	rc = send_get_tcb(sc, tid);
559 	if (rc == 0) {
560 		te->te_flags |= TE_RPL_PENDING;
561 		td->tcb_history[tid] = te;
562 	} else {
563 		free(te, M_CXGBE);
564 	}
565 	mtx_unlock(&te->te_lock);
566 done:
567 	rw_wunlock(&td->tcb_history_lock);
568 	return (rc);
569 }
570 
571 static void
572 remove_tcb_histent(struct tcb_histent *te)
573 {
574 	struct adapter *sc = te->te_adapter;
575 	struct tom_data *td = sc->tom_softc;
576 
577 	rw_assert(&td->tcb_history_lock, RA_WLOCKED);
578 	mtx_assert(&te->te_lock, MA_OWNED);
579 	MPASS(td->tcb_history[te->te_tid] == te);
580 
581 	td->tcb_history[te->te_tid] = NULL;
582 	free_tcb_histent(te);
583 	rw_wunlock(&td->tcb_history_lock);
584 }
585 
586 static inline struct tcb_histent *
587 lookup_tcb_histent(struct adapter *sc, u_int tid, bool addrem)
588 {
589 	struct tcb_histent *te;
590 	struct tom_data *td = sc->tom_softc;
591 
592 	MPASS(tid >= sc->tids.tid_base);
593 	MPASS(tid - sc->tids.tid_base < sc->tids.ntids);
594 
595 	if (td->tcb_history == NULL)
596 		return (NULL);
597 
598 	if (addrem)
599 		rw_wlock(&td->tcb_history_lock);
600 	else
601 		rw_rlock(&td->tcb_history_lock);
602 	te = td->tcb_history[tid];
603 	if (te != NULL) {
604 		mtx_lock(&te->te_lock);
605 		return (te);	/* with both locks held */
606 	}
607 	if (addrem)
608 		rw_wunlock(&td->tcb_history_lock);
609 	else
610 		rw_runlock(&td->tcb_history_lock);
611 
612 	return (te);
613 }
614 
615 static inline void
616 release_tcb_histent(struct tcb_histent *te)
617 {
618 	struct adapter *sc = te->te_adapter;
619 	struct tom_data *td = sc->tom_softc;
620 
621 	mtx_assert(&te->te_lock, MA_OWNED);
622 	mtx_unlock(&te->te_lock);
623 	rw_assert(&td->tcb_history_lock, RA_RLOCKED);
624 	rw_runlock(&td->tcb_history_lock);
625 }
626 
627 static void
628 request_tcb(void *arg)
629 {
630 	struct tcb_histent *te = arg;
631 
632 	mtx_assert(&te->te_lock, MA_OWNED);
633 
634 	/* Noone else is supposed to update the histent. */
635 	MPASS(!(te->te_flags & TE_RPL_PENDING));
636 	if (send_get_tcb(te->te_adapter, te->te_tid) == 0)
637 		te->te_flags |= TE_RPL_PENDING;
638 	else
639 		callout_schedule(&te->te_callout, hz / 100);
640 }
641 
642 static void
643 update_tcb_histent(struct tcb_histent *te, const uint64_t *tcb)
644 {
645 	struct tom_data *td = te->te_adapter->tom_softc;
646 	uint64_t tflags = get_tcb_tflags(tcb);
647 	uint8_t sample = 0;
648 
649 	if (GET_TCB_FIELD(tcb, SND_MAX_RAW) != GET_TCB_FIELD(tcb, SND_UNA_RAW)) {
650 		if (GET_TCB_FIELD(tcb, T_RXTSHIFT) != 0)
651 			sample |= TS_RTO;
652 		if (GET_TCB_FIELD(tcb, T_DUPACKS) != 0)
653 			sample |= TS_DUPACKS;
654 		if (GET_TCB_FIELD(tcb, T_DUPACKS) >= td->dupack_threshold)
655 			sample |= TS_FASTREXMT;
656 	}
657 
658 	if (GET_TCB_FIELD(tcb, SND_MAX_RAW) != 0) {
659 		uint32_t snd_wnd;
660 
661 		sample |= TS_SND_BACKLOGGED;	/* for whatever reason. */
662 
663 		snd_wnd = GET_TCB_FIELD(tcb, RCV_ADV);
664 		if (tflags & V_TF_RECV_SCALE(1))
665 			snd_wnd <<= GET_TCB_FIELD(tcb, RCV_SCALE);
666 		if (GET_TCB_FIELD(tcb, SND_CWND) < snd_wnd)
667 			sample |= TS_CWND_LIMITED;	/* maybe due to CWND */
668 	}
669 
670 	if (tflags & V_TF_CCTRL_ECN(1)) {
671 
672 		/*
673 		 * CE marker on incoming IP hdr, echoing ECE back in the TCP
674 		 * hdr.  Indicates congestion somewhere on the way from the peer
675 		 * to this node.
676 		 */
677 		if (tflags & V_TF_CCTRL_ECE(1))
678 			sample |= TS_ECN_ECE;
679 
680 		/*
681 		 * ECE seen and CWR sent (or about to be sent).  Might indicate
682 		 * congestion on the way to the peer.  This node is reducing its
683 		 * congestion window in response.
684 		 */
685 		if (tflags & (V_TF_CCTRL_CWR(1) | V_TF_CCTRL_RFR(1)))
686 			sample |= TS_ECN_CWR;
687 	}
688 
689 	te->te_sample[te->te_pidx] = sample;
690 	if (++te->te_pidx == nitems(te->te_sample))
691 		te->te_pidx = 0;
692 	memcpy(te->te_tcb, tcb, TCB_SIZE);
693 	te->te_flags |= TE_ACTIVE;
694 }
695 
696 static int
697 do_get_tcb_rpl(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
698 {
699 	struct adapter *sc = iq->adapter;
700 	const struct cpl_get_tcb_rpl *cpl = mtod(m, const void *);
701 	const uint64_t *tcb = (const uint64_t *)(const void *)(cpl + 1);
702 	struct tcb_histent *te;
703 	const u_int tid = GET_TID(cpl);
704 	bool remove;
705 
706 	remove = GET_TCB_FIELD(tcb, T_STATE) == TCPS_CLOSED;
707 	te = lookup_tcb_histent(sc, tid, remove);
708 	if (te == NULL) {
709 		/* Not in the history.  Who issued the GET_TCB for this? */
710 		device_printf(sc->dev, "tcb %u: flags 0x%016jx, state %u, "
711 		    "srtt %u, sscale %u, rscale %u, cookie 0x%x\n", tid,
712 		    (uintmax_t)get_tcb_tflags(tcb), GET_TCB_FIELD(tcb, T_STATE),
713 		    GET_TCB_FIELD(tcb, T_SRTT), GET_TCB_FIELD(tcb, SND_SCALE),
714 		    GET_TCB_FIELD(tcb, RCV_SCALE), cpl->cookie);
715 		goto done;
716 	}
717 
718 	MPASS(te->te_flags & TE_RPL_PENDING);
719 	te->te_flags &= ~TE_RPL_PENDING;
720 	if (remove) {
721 		remove_tcb_histent(te);
722 	} else {
723 		update_tcb_histent(te, tcb);
724 		callout_reset(&te->te_callout, hz / 10, request_tcb, te);
725 		release_tcb_histent(te);
726 	}
727 done:
728 	m_freem(m);
729 	return (0);
730 }
731 
732 static void
733 fill_tcp_info_from_tcb(struct adapter *sc, uint64_t *tcb, struct tcp_info *ti)
734 {
735 	uint32_t v;
736 
737 	ti->tcpi_state = GET_TCB_FIELD(tcb, T_STATE);
738 
739 	v = GET_TCB_FIELD(tcb, T_SRTT);
740 	ti->tcpi_rtt = tcp_ticks_to_us(sc, v);
741 
742 	v = GET_TCB_FIELD(tcb, T_RTTVAR);
743 	ti->tcpi_rttvar = tcp_ticks_to_us(sc, v);
744 
745 	ti->tcpi_snd_ssthresh = GET_TCB_FIELD(tcb, SND_SSTHRESH);
746 	ti->tcpi_snd_cwnd = GET_TCB_FIELD(tcb, SND_CWND);
747 	ti->tcpi_rcv_nxt = GET_TCB_FIELD(tcb, RCV_NXT);
748 	ti->tcpi_rcv_adv = GET_TCB_FIELD(tcb, RCV_ADV);
749 	ti->tcpi_dupacks = GET_TCB_FIELD(tcb, T_DUPACKS);
750 
751 	v = GET_TCB_FIELD(tcb, TX_MAX);
752 	ti->tcpi_snd_nxt = v - GET_TCB_FIELD(tcb, SND_NXT_RAW);
753 	ti->tcpi_snd_una = v - GET_TCB_FIELD(tcb, SND_UNA_RAW);
754 	ti->tcpi_snd_max = v - GET_TCB_FIELD(tcb, SND_MAX_RAW);
755 
756 	/* Receive window being advertised by us. */
757 	ti->tcpi_rcv_wscale = GET_TCB_FIELD(tcb, SND_SCALE);	/* Yes, SND. */
758 	ti->tcpi_rcv_space = GET_TCB_FIELD(tcb, RCV_WND);
759 
760 	/* Send window */
761 	ti->tcpi_snd_wscale = GET_TCB_FIELD(tcb, RCV_SCALE);	/* Yes, RCV. */
762 	ti->tcpi_snd_wnd = GET_TCB_FIELD(tcb, RCV_ADV);
763 	if (get_tcb_tflags(tcb) & V_TF_RECV_SCALE(1))
764 		ti->tcpi_snd_wnd <<= ti->tcpi_snd_wscale;
765 	else
766 		ti->tcpi_snd_wscale = 0;
767 
768 }
769 
770 static void
771 fill_tcp_info_from_history(struct adapter *sc, struct tcb_histent *te,
772     struct tcp_info *ti)
773 {
774 
775 	fill_tcp_info_from_tcb(sc, te->te_tcb, ti);
776 }
777 
778 /*
779  * Reads the TCB for the given tid using a memory window and copies it to 'buf'
780  * in the same format as CPL_GET_TCB_RPL.
781  */
782 static void
783 read_tcb_using_memwin(struct adapter *sc, u_int tid, uint64_t *buf)
784 {
785 	int i, j, k, rc;
786 	uint32_t addr;
787 	u_char *tcb, tmp;
788 
789 	MPASS(tid >= sc->tids.tid_base);
790 	MPASS(tid - sc->tids.tid_base < sc->tids.ntids);
791 
792 	addr = t4_read_reg(sc, A_TP_CMM_TCB_BASE) + tid * TCB_SIZE;
793 	rc = read_via_memwin(sc, 2, addr, (uint32_t *)buf, TCB_SIZE);
794 	if (rc != 0)
795 		return;
796 
797 	tcb = (u_char *)buf;
798 	for (i = 0, j = TCB_SIZE - 16; i < j; i += 16, j -= 16) {
799 		for (k = 0; k < 16; k++) {
800 			tmp = tcb[i + k];
801 			tcb[i + k] = tcb[j + k];
802 			tcb[j + k] = tmp;
803 		}
804 	}
805 }
806 
807 static void
808 fill_tcp_info(struct adapter *sc, u_int tid, struct tcp_info *ti)
809 {
810 	uint64_t tcb[TCB_SIZE / sizeof(uint64_t)];
811 	struct tcb_histent *te;
812 
813 	ti->tcpi_toe_tid = tid;
814 	te = lookup_tcb_histent(sc, tid, false);
815 	if (te != NULL) {
816 		fill_tcp_info_from_history(sc, te, ti);
817 		release_tcb_histent(te);
818 	} else {
819 		if (!(sc->debug_flags & DF_DISABLE_TCB_CACHE)) {
820 			/* XXX: tell firmware to flush TCB cache. */
821 		}
822 		read_tcb_using_memwin(sc, tid, tcb);
823 		fill_tcp_info_from_tcb(sc, tcb, ti);
824 	}
825 }
826 
827 /*
828  * Called by the kernel to allow the TOE driver to "refine" values filled up in
829  * the tcp_info for an offloaded connection.
830  */
831 static void
832 t4_tcp_info(struct toedev *tod, const struct tcpcb *tp, struct tcp_info *ti)
833 {
834 	struct adapter *sc = tod->tod_softc;
835 	struct toepcb *toep = tp->t_toe;
836 
837 	INP_LOCK_ASSERT(tptoinpcb(tp));
838 	MPASS(ti != NULL);
839 
840 	fill_tcp_info(sc, toep->tid, ti);
841 }
842 
843 #ifdef KERN_TLS
844 static int
845 t4_alloc_tls_session(struct toedev *tod, struct tcpcb *tp,
846     struct ktls_session *tls, int direction)
847 {
848 	struct toepcb *toep = tp->t_toe;
849 
850 	INP_WLOCK_ASSERT(tptoinpcb(tp));
851 	MPASS(tls != NULL);
852 
853 	return (tls_alloc_ktls(toep, tls, direction));
854 }
855 #endif
856 
857 static void
858 send_mss_flowc_wr(struct adapter *sc, struct toepcb *toep)
859 {
860 	struct wrq_cookie cookie;
861 	struct fw_flowc_wr *flowc;
862 	struct ofld_tx_sdesc *txsd;
863 	const int flowclen = sizeof(*flowc) + sizeof(struct fw_flowc_mnemval);
864 	const int flowclen16 = howmany(flowclen, 16);
865 
866 	if (toep->tx_credits < flowclen16 || toep->txsd_avail == 0) {
867 		CH_ERR(sc, "%s: tid %u out of tx credits (%d, %d).\n", __func__,
868 		    toep->tid, toep->tx_credits, toep->txsd_avail);
869 		return;
870 	}
871 
872 	flowc = start_wrq_wr(&toep->ofld_txq->wrq, flowclen16, &cookie);
873 	if (__predict_false(flowc == NULL)) {
874 		CH_ERR(sc, "ENOMEM in %s for tid %u.\n", __func__, toep->tid);
875 		return;
876 	}
877 	flowc->op_to_nparams = htobe32(V_FW_WR_OP(FW_FLOWC_WR) |
878 	    V_FW_FLOWC_WR_NPARAMS(1));
879 	flowc->flowid_len16 = htonl(V_FW_WR_LEN16(flowclen16) |
880 	    V_FW_WR_FLOWID(toep->tid));
881 	flowc->mnemval[0].mnemonic = FW_FLOWC_MNEM_MSS;
882 	flowc->mnemval[0].val = htobe32(toep->params.emss);
883 
884 	txsd = &toep->txsd[toep->txsd_pidx];
885 	txsd->tx_credits = flowclen16;
886 	txsd->plen = 0;
887 	toep->tx_credits -= txsd->tx_credits;
888 	if (__predict_false(++toep->txsd_pidx == toep->txsd_total))
889 		toep->txsd_pidx = 0;
890 	toep->txsd_avail--;
891 	commit_wrq_wr(&toep->ofld_txq->wrq, flowc, &cookie);
892 }
893 
894 static void
895 t4_pmtu_update(struct toedev *tod, struct tcpcb *tp, tcp_seq seq, int mtu)
896 {
897 	struct work_request_hdr *wrh;
898 	struct ulp_txpkt *ulpmc;
899 	int idx, len;
900 	struct wrq_cookie cookie;
901 	struct inpcb *inp = tptoinpcb(tp);
902 	struct toepcb *toep = tp->t_toe;
903 	struct adapter *sc = td_adapter(toep->td);
904 	unsigned short *mtus = &sc->params.mtus[0];
905 
906 	INP_WLOCK_ASSERT(inp);
907 	MPASS(mtu > 0);	/* kernel is supposed to provide something usable. */
908 
909 	/* tp->snd_una and snd_max are in host byte order too. */
910 	seq = be32toh(seq);
911 
912 	CTR6(KTR_CXGBE, "%s: tid %d, seq 0x%08x, mtu %u, mtu_idx %u (%d)",
913 	    __func__, toep->tid, seq, mtu, toep->params.mtu_idx,
914 	    mtus[toep->params.mtu_idx]);
915 
916 	if (ulp_mode(toep) == ULP_MODE_NONE &&	/* XXX: Read TCB otherwise? */
917 	    (SEQ_LT(seq, tp->snd_una) || SEQ_GEQ(seq, tp->snd_max))) {
918 		CTR5(KTR_CXGBE,
919 		    "%s: tid %d, seq 0x%08x not in range [0x%08x, 0x%08x).",
920 		    __func__, toep->tid, seq, tp->snd_una, tp->snd_max);
921 		return;
922 	}
923 
924 	/* Find the best mtu_idx for the suggested MTU. */
925 	for (idx = 0; idx < NMTUS - 1 && mtus[idx + 1] <= mtu; idx++)
926 		continue;
927 	if (idx >= toep->params.mtu_idx)
928 		return;	/* Never increase the PMTU (just like the kernel). */
929 
930 	/*
931 	 * We'll send a compound work request with 2 SET_TCB_FIELDs -- the first
932 	 * one updates the mtu_idx and the second one triggers a retransmit.
933 	 */
934 	len = sizeof(*wrh) + 2 * roundup2(LEN__SET_TCB_FIELD_ULP, 16);
935 	wrh = start_wrq_wr(toep->ctrlq, howmany(len, 16), &cookie);
936 	if (wrh == NULL) {
937 		CH_ERR(sc, "failed to change mtu_idx of tid %d (%u -> %u).\n",
938 		    toep->tid, toep->params.mtu_idx, idx);
939 		return;
940 	}
941 	INIT_ULPTX_WRH(wrh, len, 1, 0);	/* atomic */
942 	ulpmc = (struct ulp_txpkt *)(wrh + 1);
943 	ulpmc = mk_set_tcb_field_ulp(sc, ulpmc, toep->tid, W_TCB_T_MAXSEG,
944 	    V_TCB_T_MAXSEG(M_TCB_T_MAXSEG), V_TCB_T_MAXSEG(idx));
945 	ulpmc = mk_set_tcb_field_ulp(sc, ulpmc, toep->tid, W_TCB_TIMESTAMP,
946 	    V_TCB_TIMESTAMP(0x7FFFFULL << 11), 0);
947 	commit_wrq_wr(toep->ctrlq, wrh, &cookie);
948 
949 	/* Update the software toepcb and tcpcb. */
950 	toep->params.mtu_idx = idx;
951 	tp->t_maxseg = mtus[toep->params.mtu_idx];
952 	if (inp->inp_inc.inc_flags & INC_ISIPV6)
953 		tp->t_maxseg -= sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
954 	else
955 		tp->t_maxseg -= sizeof(struct ip) + sizeof(struct tcphdr);
956 	toep->params.emss = tp->t_maxseg;
957 	if (tp->t_flags & TF_RCVD_TSTMP)
958 		toep->params.emss -= TCPOLEN_TSTAMP_APPA;
959 
960 	/* Update the firmware flowc. */
961 	send_mss_flowc_wr(sc, toep);
962 
963 	/* Update the MTU in the kernel's hostcache. */
964 	if (sc->tt.update_hc_on_pmtu_change != 0) {
965 		struct in_conninfo inc = {0};
966 
967 		inc.inc_fibnum = inp->inp_inc.inc_fibnum;
968 		if (inp->inp_inc.inc_flags & INC_ISIPV6) {
969 			inc.inc_flags |= INC_ISIPV6;
970 			inc.inc6_faddr = inp->inp_inc.inc6_faddr;
971 		} else {
972 			inc.inc_faddr = inp->inp_inc.inc_faddr;
973 		}
974 		tcp_hc_updatemtu(&inc, mtu);
975 	}
976 
977 	CTR6(KTR_CXGBE, "%s: tid %d, mtu_idx %u (%u), t_maxseg %u, emss %u",
978 	    __func__, toep->tid, toep->params.mtu_idx,
979 	    mtus[toep->params.mtu_idx], tp->t_maxseg, toep->params.emss);
980 }
981 
982 /*
983  * The TOE driver will not receive any more CPLs for the tid associated with the
984  * toepcb; release the hold on the inpcb.
985  */
986 void
987 final_cpl_received(struct toepcb *toep)
988 {
989 	struct inpcb *inp = toep->inp;
990 	bool need_wakeup;
991 
992 	KASSERT(inp != NULL, ("%s: inp is NULL", __func__));
993 	INP_WLOCK_ASSERT(inp);
994 	KASSERT(toep->flags & TPF_CPL_PENDING,
995 	    ("%s: CPL not pending already?", __func__));
996 
997 	CTR6(KTR_CXGBE, "%s: tid %d, toep %p (0x%x), inp %p (0x%x)",
998 	    __func__, toep->tid, toep, toep->flags, inp, inp->inp_flags);
999 
1000 	if (ulp_mode(toep) == ULP_MODE_TCPDDP)
1001 		release_ddp_resources(toep);
1002 	toep->inp = NULL;
1003 	need_wakeup = (toep->flags & TPF_WAITING_FOR_FINAL) != 0;
1004 	toep->flags &= ~(TPF_CPL_PENDING | TPF_WAITING_FOR_FINAL);
1005 	mbufq_drain(&toep->ulp_pduq);
1006 	mbufq_drain(&toep->ulp_pdu_reclaimq);
1007 	release_offload_resources(toep);
1008 	if (!(toep->flags & TPF_ATTACHED))
1009 		done_with_toepcb(toep);
1010 
1011 	if (!in_pcbrele_wlocked(inp))
1012 		INP_WUNLOCK(inp);
1013 
1014 	if (need_wakeup) {
1015 		struct mtx *lock = mtx_pool_find(mtxpool_sleep, toep);
1016 
1017 		mtx_lock(lock);
1018 		wakeup(toep);
1019 		mtx_unlock(lock);
1020 	}
1021 }
1022 
1023 void
1024 insert_tid(struct adapter *sc, int tid, void *ctx, int ntids)
1025 {
1026 	struct tid_info *t = &sc->tids;
1027 
1028 	MPASS(tid >= t->tid_base);
1029 	MPASS(tid - t->tid_base < t->ntids);
1030 
1031 	t->tid_tab[tid - t->tid_base] = ctx;
1032 	atomic_add_int(&t->tids_in_use, ntids);
1033 }
1034 
1035 void *
1036 lookup_tid(struct adapter *sc, int tid)
1037 {
1038 	struct tid_info *t = &sc->tids;
1039 
1040 	return (t->tid_tab[tid - t->tid_base]);
1041 }
1042 
1043 void
1044 update_tid(struct adapter *sc, int tid, void *ctx)
1045 {
1046 	struct tid_info *t = &sc->tids;
1047 
1048 	t->tid_tab[tid - t->tid_base] = ctx;
1049 }
1050 
1051 void
1052 remove_tid(struct adapter *sc, int tid, int ntids)
1053 {
1054 	struct tid_info *t = &sc->tids;
1055 
1056 	t->tid_tab[tid - t->tid_base] = NULL;
1057 	atomic_subtract_int(&t->tids_in_use, ntids);
1058 }
1059 
1060 /*
1061  * What mtu_idx to use, given a 4-tuple.  Note that both s->mss and tcp_mssopt
1062  * have the MSS that we should advertise in our SYN.  Advertised MSS doesn't
1063  * account for any TCP options so the effective MSS (only payload, no headers or
1064  * options) could be different.
1065  */
1066 static int
1067 find_best_mtu_idx(struct adapter *sc, struct in_conninfo *inc,
1068     struct offload_settings *s)
1069 {
1070 	unsigned short *mtus = &sc->params.mtus[0];
1071 	int i, mss, mtu;
1072 
1073 	MPASS(inc != NULL);
1074 
1075 	mss = s->mss > 0 ? s->mss : tcp_mssopt(inc);
1076 	if (inc->inc_flags & INC_ISIPV6)
1077 		mtu = mss + sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
1078 	else
1079 		mtu = mss + sizeof(struct ip) + sizeof(struct tcphdr);
1080 
1081 	for (i = 0; i < NMTUS - 1 && mtus[i + 1] <= mtu; i++)
1082 		continue;
1083 
1084 	return (i);
1085 }
1086 
1087 /*
1088  * Determine the receive window size for a socket.
1089  */
1090 u_long
1091 select_rcv_wnd(struct socket *so)
1092 {
1093 	unsigned long wnd;
1094 
1095 	SOCKBUF_LOCK_ASSERT(&so->so_rcv);
1096 
1097 	wnd = sbspace(&so->so_rcv);
1098 	if (wnd < MIN_RCV_WND)
1099 		wnd = MIN_RCV_WND;
1100 
1101 	return min(wnd, MAX_RCV_WND);
1102 }
1103 
1104 int
1105 select_rcv_wscale(void)
1106 {
1107 	int wscale = 0;
1108 	unsigned long space = sb_max;
1109 
1110 	if (space > MAX_RCV_WND)
1111 		space = MAX_RCV_WND;
1112 
1113 	while (wscale < TCP_MAX_WINSHIFT && (TCP_MAXWIN << wscale) < space)
1114 		wscale++;
1115 
1116 	return (wscale);
1117 }
1118 
1119 __be64
1120 calc_options0(struct vi_info *vi, struct conn_params *cp)
1121 {
1122 	uint64_t opt0 = 0;
1123 
1124 	opt0 |= F_TCAM_BYPASS;
1125 
1126 	MPASS(cp->wscale >= 0 && cp->wscale <= M_WND_SCALE);
1127 	opt0 |= V_WND_SCALE(cp->wscale);
1128 
1129 	MPASS(cp->mtu_idx >= 0 && cp->mtu_idx < NMTUS);
1130 	opt0 |= V_MSS_IDX(cp->mtu_idx);
1131 
1132 	MPASS(cp->ulp_mode >= 0 && cp->ulp_mode <= M_ULP_MODE);
1133 	opt0 |= V_ULP_MODE(cp->ulp_mode);
1134 
1135 	MPASS(cp->opt0_bufsize >= 0 && cp->opt0_bufsize <= M_RCV_BUFSIZ);
1136 	opt0 |= V_RCV_BUFSIZ(cp->opt0_bufsize);
1137 
1138 	MPASS(cp->l2t_idx >= 0 && cp->l2t_idx < vi->adapter->vres.l2t.size);
1139 	opt0 |= V_L2T_IDX(cp->l2t_idx);
1140 
1141 	opt0 |= V_SMAC_SEL(vi->smt_idx);
1142 	opt0 |= V_TX_CHAN(vi->pi->tx_chan);
1143 
1144 	MPASS(cp->keepalive == 0 || cp->keepalive == 1);
1145 	opt0 |= V_KEEP_ALIVE(cp->keepalive);
1146 
1147 	MPASS(cp->nagle == 0 || cp->nagle == 1);
1148 	opt0 |= V_NAGLE(cp->nagle);
1149 
1150 	return (htobe64(opt0));
1151 }
1152 
1153 __be32
1154 calc_options2(struct vi_info *vi, struct conn_params *cp)
1155 {
1156 	uint32_t opt2 = 0;
1157 	struct port_info *pi = vi->pi;
1158 	struct adapter *sc = pi->adapter;
1159 
1160 	/*
1161 	 * rx flow control, rx coalesce, congestion control, and tx pace are all
1162 	 * explicitly set by the driver.  On T5+ the ISS is also set by the
1163 	 * driver to the value picked by the kernel.
1164 	 */
1165 	if (is_t4(sc)) {
1166 		opt2 |= F_RX_FC_VALID | F_RX_COALESCE_VALID;
1167 		opt2 |= F_CONG_CNTRL_VALID | F_PACE_VALID;
1168 	} else {
1169 		opt2 |= F_T5_OPT_2_VALID;	/* all 4 valid */
1170 		opt2 |= F_T5_ISS;		/* ISS provided in CPL */
1171 	}
1172 
1173 	MPASS(cp->sack == 0 || cp->sack == 1);
1174 	opt2 |= V_SACK_EN(cp->sack);
1175 
1176 	MPASS(cp->tstamp == 0 || cp->tstamp == 1);
1177 	opt2 |= V_TSTAMPS_EN(cp->tstamp);
1178 
1179 	if (cp->wscale > 0)
1180 		opt2 |= F_WND_SCALE_EN;
1181 
1182 	MPASS(cp->ecn == 0 || cp->ecn == 1);
1183 	opt2 |= V_CCTRL_ECN(cp->ecn);
1184 
1185 	opt2 |= V_TX_QUEUE(TX_MODQ(pi->tx_chan));
1186 	opt2 |= V_PACE(0);
1187 	opt2 |= F_RSS_QUEUE_VALID;
1188 	opt2 |= V_RSS_QUEUE(sc->sge.ofld_rxq[cp->rxq_idx].iq.abs_id);
1189 	if (chip_id(sc) <= CHELSIO_T6) {
1190 		MPASS(pi->rx_chan == 0 || pi->rx_chan == 1);
1191 		opt2 |= V_RX_CHANNEL(pi->rx_chan);
1192 	}
1193 
1194 	MPASS(cp->cong_algo >= 0 && cp->cong_algo <= M_CONG_CNTRL);
1195 	opt2 |= V_CONG_CNTRL(cp->cong_algo);
1196 
1197 	MPASS(cp->rx_coalesce == 0 || cp->rx_coalesce == 1);
1198 	if (cp->rx_coalesce == 1)
1199 		opt2 |= V_RX_COALESCE(M_RX_COALESCE);
1200 
1201 	opt2 |= V_RX_FC_DDP(0) | V_RX_FC_DISABLE(0);
1202 	MPASS(cp->ulp_mode != ULP_MODE_TCPDDP);
1203 
1204 	return (htobe32(opt2));
1205 }
1206 
1207 uint64_t
1208 select_ntuple(struct vi_info *vi, struct l2t_entry *e)
1209 {
1210 	struct adapter *sc = vi->adapter;
1211 	struct tp_params *tp = &sc->params.tp;
1212 	uint64_t ntuple = 0;
1213 
1214 	/*
1215 	 * Initialize each of the fields which we care about which are present
1216 	 * in the Compressed Filter Tuple.
1217 	 */
1218 	if (tp->vlan_shift >= 0 && EVL_VLANOFTAG(e->vlan) != CPL_L2T_VLAN_NONE)
1219 		ntuple |= (uint64_t)(F_FT_VLAN_VLD | e->vlan) << tp->vlan_shift;
1220 
1221 	if (tp->port_shift >= 0)
1222 		ntuple |= (uint64_t)e->lport << tp->port_shift;
1223 
1224 	if (tp->protocol_shift >= 0)
1225 		ntuple |= (uint64_t)IPPROTO_TCP << tp->protocol_shift;
1226 
1227 	if (tp->vnic_shift >= 0 && tp->vnic_mode == FW_VNIC_MODE_PF_VF) {
1228 		ntuple |= (uint64_t)(V_FT_VNID_ID_VF(vi->vin) |
1229 		    V_FT_VNID_ID_PF(sc->pf) | V_FT_VNID_ID_VLD(vi->vfvld)) <<
1230 		    tp->vnic_shift;
1231 	}
1232 
1233 	if (is_t4(sc))
1234 		return (htobe32((uint32_t)ntuple));
1235 	else
1236 		return (htobe64(V_FILTER_TUPLE(ntuple)));
1237 }
1238 
1239 /*
1240  * Initialize various connection parameters.
1241  */
1242 void
1243 init_conn_params(struct vi_info *vi , struct offload_settings *s,
1244     struct in_conninfo *inc, struct socket *so,
1245     const struct tcp_options *tcpopt, int16_t l2t_idx, struct conn_params *cp)
1246 {
1247 	struct port_info *pi = vi->pi;
1248 	struct adapter *sc = pi->adapter;
1249 	struct tom_tunables *tt = &sc->tt;
1250 	struct inpcb *inp = sotoinpcb(so);
1251 	struct tcpcb *tp = intotcpcb(inp);
1252 	u_long wnd;
1253 	u_int q_idx;
1254 
1255 	MPASS(s->offload != 0);
1256 
1257 	/* Congestion control algorithm */
1258 	if (s->cong_algo >= 0)
1259 		cp->cong_algo = s->cong_algo & M_CONG_CNTRL;
1260 	else if (sc->tt.cong_algorithm >= 0)
1261 		cp->cong_algo = tt->cong_algorithm & M_CONG_CNTRL;
1262 	else {
1263 		struct cc_algo *cc = CC_ALGO(tp);
1264 
1265 		if (strcasecmp(cc->name, "reno") == 0)
1266 			cp->cong_algo = CONG_ALG_RENO;
1267 		else if (strcasecmp(cc->name, "tahoe") == 0)
1268 			cp->cong_algo = CONG_ALG_TAHOE;
1269 		if (strcasecmp(cc->name, "newreno") == 0)
1270 			cp->cong_algo = CONG_ALG_NEWRENO;
1271 		if (strcasecmp(cc->name, "highspeed") == 0)
1272 			cp->cong_algo = CONG_ALG_HIGHSPEED;
1273 		else {
1274 			/*
1275 			 * Use newreno in case the algorithm selected by the
1276 			 * host stack is not supported by the hardware.
1277 			 */
1278 			cp->cong_algo = CONG_ALG_NEWRENO;
1279 		}
1280 	}
1281 
1282 	/* Tx traffic scheduling class. */
1283 	if (s->sched_class >= 0 && s->sched_class < sc->params.nsched_cls)
1284 		cp->tc_idx = s->sched_class;
1285 	else
1286 		cp->tc_idx = -1;
1287 
1288 	/* Nagle's algorithm. */
1289 	if (s->nagle >= 0)
1290 		cp->nagle = s->nagle > 0 ? 1 : 0;
1291 	else
1292 		cp->nagle = tp->t_flags & TF_NODELAY ? 0 : 1;
1293 
1294 	/* TCP Keepalive. */
1295 	if (V_tcp_always_keepalive || so_options_get(so) & SO_KEEPALIVE)
1296 		cp->keepalive = 1;
1297 	else
1298 		cp->keepalive = 0;
1299 
1300 	/* Optimization that's specific to T5 @ 40G. */
1301 	if (tt->tx_align >= 0)
1302 		cp->tx_align =  tt->tx_align > 0 ? 1 : 0;
1303 	else if (chip_id(sc) == CHELSIO_T5 &&
1304 	    (port_top_speed(pi) > 10 || sc->params.nports > 2))
1305 		cp->tx_align = 1;
1306 	else
1307 		cp->tx_align = 0;
1308 
1309 	/* ULP mode. */
1310 	cp->ulp_mode = ULP_MODE_NONE;
1311 
1312 	/* Rx coalescing. */
1313 	if (s->rx_coalesce >= 0)
1314 		cp->rx_coalesce = s->rx_coalesce > 0 ? 1 : 0;
1315 	else if (tt->rx_coalesce >= 0)
1316 		cp->rx_coalesce = tt->rx_coalesce > 0 ? 1 : 0;
1317 	else
1318 		cp->rx_coalesce = 1;	/* default */
1319 
1320 	/*
1321 	 * Index in the PMTU table.  This controls the MSS that we announce in
1322 	 * our SYN initially, but after ESTABLISHED it controls the MSS that we
1323 	 * use to send data.
1324 	 */
1325 	cp->mtu_idx = find_best_mtu_idx(sc, inc, s);
1326 
1327 	/* Tx queue for this connection. */
1328 	if (s->txq == QUEUE_RANDOM)
1329 		q_idx = arc4random();
1330 	else if (s->txq == QUEUE_ROUNDROBIN)
1331 		q_idx = atomic_fetchadd_int(&vi->txq_rr, 1);
1332 	else
1333 		q_idx = s->txq;
1334 	cp->txq_idx = vi->first_ofld_txq + q_idx % vi->nofldtxq;
1335 
1336 	/* Rx queue for this connection. */
1337 	if (s->rxq == QUEUE_RANDOM)
1338 		q_idx = arc4random();
1339 	else if (s->rxq == QUEUE_ROUNDROBIN)
1340 		q_idx = atomic_fetchadd_int(&vi->rxq_rr, 1);
1341 	else
1342 		q_idx = s->rxq;
1343 	cp->rxq_idx = vi->first_ofld_rxq + q_idx % vi->nofldrxq;
1344 
1345 	if (SOLISTENING(so)) {
1346 		/* Passive open */
1347 		MPASS(tcpopt != NULL);
1348 
1349 		/* TCP timestamp option */
1350 		if (tcpopt->tstamp &&
1351 		    (s->tstamp > 0 || (s->tstamp < 0 && V_tcp_do_rfc1323)))
1352 			cp->tstamp = 1;
1353 		else
1354 			cp->tstamp = 0;
1355 
1356 		/* SACK */
1357 		if (tcpopt->sack &&
1358 		    (s->sack > 0 || (s->sack < 0 && V_tcp_do_sack)))
1359 			cp->sack = 1;
1360 		else
1361 			cp->sack = 0;
1362 
1363 		/* Receive window scaling. */
1364 		if (tcpopt->wsf > 0 && tcpopt->wsf < 15 && V_tcp_do_rfc1323)
1365 			cp->wscale = select_rcv_wscale();
1366 		else
1367 			cp->wscale = 0;
1368 
1369 		/* ECN */
1370 		if (tcpopt->ecn &&	/* XXX: review. */
1371 		    (s->ecn > 0 || (s->ecn < 0 && V_tcp_do_ecn)))
1372 			cp->ecn = 1;
1373 		else
1374 			cp->ecn = 0;
1375 
1376 		wnd = max(so->sol_sbrcv_hiwat, MIN_RCV_WND);
1377 		cp->opt0_bufsize = min(wnd >> 10, M_RCV_BUFSIZ);
1378 
1379 		if (tt->sndbuf > 0)
1380 			cp->sndbuf = tt->sndbuf;
1381 		else if (so->sol_sbsnd_flags & SB_AUTOSIZE &&
1382 		    V_tcp_do_autosndbuf)
1383 			cp->sndbuf = 256 * 1024;
1384 		else
1385 			cp->sndbuf = so->sol_sbsnd_hiwat;
1386 	} else {
1387 		/* Active open */
1388 
1389 		/* TCP timestamp option */
1390 		if (s->tstamp > 0 ||
1391 		    (s->tstamp < 0 && (tp->t_flags & TF_REQ_TSTMP)))
1392 			cp->tstamp = 1;
1393 		else
1394 			cp->tstamp = 0;
1395 
1396 		/* SACK */
1397 		if (s->sack > 0 ||
1398 		    (s->sack < 0 && (tp->t_flags & TF_SACK_PERMIT)))
1399 			cp->sack = 1;
1400 		else
1401 			cp->sack = 0;
1402 
1403 		/* Receive window scaling */
1404 		if (tp->t_flags & TF_REQ_SCALE)
1405 			cp->wscale = select_rcv_wscale();
1406 		else
1407 			cp->wscale = 0;
1408 
1409 		/* ECN */
1410 		if (s->ecn > 0 || (s->ecn < 0 && V_tcp_do_ecn == 1))
1411 			cp->ecn = 1;
1412 		else
1413 			cp->ecn = 0;
1414 
1415 		SOCKBUF_LOCK(&so->so_rcv);
1416 		wnd = max(select_rcv_wnd(so), MIN_RCV_WND);
1417 		SOCKBUF_UNLOCK(&so->so_rcv);
1418 		cp->opt0_bufsize = min(wnd >> 10, M_RCV_BUFSIZ);
1419 
1420 		if (tt->sndbuf > 0)
1421 			cp->sndbuf = tt->sndbuf;
1422 		else {
1423 			SOCKBUF_LOCK(&so->so_snd);
1424 			if (so->so_snd.sb_flags & SB_AUTOSIZE &&
1425 			    V_tcp_do_autosndbuf)
1426 				cp->sndbuf = 256 * 1024;
1427 			else
1428 				cp->sndbuf = so->so_snd.sb_hiwat;
1429 			SOCKBUF_UNLOCK(&so->so_snd);
1430 		}
1431 	}
1432 
1433 	cp->l2t_idx = l2t_idx;
1434 
1435 	/* This will be initialized on ESTABLISHED. */
1436 	cp->emss = 0;
1437 }
1438 
1439 int
1440 negative_advice(int status)
1441 {
1442 
1443 	return (status == CPL_ERR_RTX_NEG_ADVICE ||
1444 	    status == CPL_ERR_PERSIST_NEG_ADVICE ||
1445 	    status == CPL_ERR_KEEPALV_NEG_ADVICE);
1446 }
1447 
1448 static int
1449 alloc_tid_tab(struct adapter *sc)
1450 {
1451 	struct tid_info *t = &sc->tids;
1452 
1453 	MPASS(t->ntids > 0);
1454 	MPASS(t->tid_tab == NULL);
1455 
1456 	t->tid_tab = malloc(t->ntids * sizeof(*t->tid_tab), M_CXGBE,
1457 	    M_ZERO | M_NOWAIT);
1458 	if (t->tid_tab == NULL)
1459 		return (ENOMEM);
1460 	atomic_store_rel_int(&t->tids_in_use, 0);
1461 
1462 	return (0);
1463 }
1464 
1465 static void
1466 free_tid_tab(struct adapter *sc)
1467 {
1468 	struct tid_info *t = &sc->tids;
1469 
1470 	KASSERT(t->tids_in_use == 0,
1471 	    ("%s: %d tids still in use.", __func__, t->tids_in_use));
1472 
1473 	free(t->tid_tab, M_CXGBE);
1474 	t->tid_tab = NULL;
1475 }
1476 
1477 static void
1478 free_tid_tabs(struct adapter *sc)
1479 {
1480 	free_tid_tab(sc);
1481 	free_stid_tab(sc);
1482 }
1483 
1484 static int
1485 alloc_tid_tabs(struct adapter *sc)
1486 {
1487 	int rc;
1488 
1489 	rc = alloc_tid_tab(sc);
1490 	if (rc != 0)
1491 		goto failed;
1492 
1493 	rc = alloc_stid_tab(sc);
1494 	if (rc != 0)
1495 		goto failed;
1496 
1497 	return (0);
1498 failed:
1499 	free_tid_tabs(sc);
1500 	return (rc);
1501 }
1502 
1503 static inline void
1504 alloc_tcb_history(struct adapter *sc, struct tom_data *td)
1505 {
1506 
1507 	if (sc->tids.ntids == 0 || sc->tids.ntids > 1024)
1508 		return;
1509 	rw_init(&td->tcb_history_lock, "TCB history");
1510 	td->tcb_history = malloc(sc->tids.ntids * sizeof(*td->tcb_history),
1511 	    M_CXGBE, M_ZERO | M_NOWAIT);
1512 	td->dupack_threshold = G_DUPACKTHRESH(t4_read_reg(sc, A_TP_PARA_REG0));
1513 }
1514 
1515 static inline void
1516 free_tcb_history(struct adapter *sc, struct tom_data *td)
1517 {
1518 #ifdef INVARIANTS
1519 	int i;
1520 
1521 	if (td->tcb_history != NULL) {
1522 		for (i = 0; i < sc->tids.ntids; i++) {
1523 			MPASS(td->tcb_history[i] == NULL);
1524 		}
1525 	}
1526 #endif
1527 	free(td->tcb_history, M_CXGBE);
1528 	if (rw_initialized(&td->tcb_history_lock))
1529 		rw_destroy(&td->tcb_history_lock);
1530 }
1531 
1532 static void
1533 free_tom_data(struct adapter *sc, struct tom_data *td)
1534 {
1535 
1536 	ASSERT_SYNCHRONIZED_OP(sc);
1537 
1538 	KASSERT(TAILQ_EMPTY(&td->toep_list),
1539 	    ("%s: TOE PCB list is not empty.", __func__));
1540 	KASSERT(td->lctx_count == 0,
1541 	    ("%s: lctx hash table is not empty.", __func__));
1542 
1543 	t4_free_ppod_region(&td->pr);
1544 
1545 	if (td->listen_mask != 0)
1546 		hashdestroy(td->listen_hash, M_CXGBE, td->listen_mask);
1547 
1548 	if (mtx_initialized(&td->unsent_wr_lock))
1549 		mtx_destroy(&td->unsent_wr_lock);
1550 	if (mtx_initialized(&td->lctx_hash_lock))
1551 		mtx_destroy(&td->lctx_hash_lock);
1552 	if (mtx_initialized(&td->toep_list_lock))
1553 		mtx_destroy(&td->toep_list_lock);
1554 
1555 	free_tcb_history(sc, td);
1556 	free_tid_tabs(sc);
1557 	free(td, M_CXGBE);
1558 }
1559 
1560 static char *
1561 prepare_pkt(int open_type, uint16_t vtag, struct inpcb *inp, int *pktlen,
1562     int *buflen)
1563 {
1564 	char *pkt;
1565 	struct tcphdr *th;
1566 	int ipv6, len;
1567 	const int maxlen =
1568 	    max(sizeof(struct ether_header), sizeof(struct ether_vlan_header)) +
1569 	    max(sizeof(struct ip), sizeof(struct ip6_hdr)) +
1570 	    sizeof(struct tcphdr);
1571 
1572 	MPASS(open_type == OPEN_TYPE_ACTIVE || open_type == OPEN_TYPE_LISTEN);
1573 
1574 	pkt = malloc(maxlen, M_CXGBE, M_ZERO | M_NOWAIT);
1575 	if (pkt == NULL)
1576 		return (NULL);
1577 
1578 	ipv6 = inp->inp_vflag & INP_IPV6;
1579 	len = 0;
1580 
1581 	if (EVL_VLANOFTAG(vtag) == 0xfff) {
1582 		struct ether_header *eh = (void *)pkt;
1583 
1584 		if (ipv6)
1585 			eh->ether_type = htons(ETHERTYPE_IPV6);
1586 		else
1587 			eh->ether_type = htons(ETHERTYPE_IP);
1588 
1589 		len += sizeof(*eh);
1590 	} else {
1591 		struct ether_vlan_header *evh = (void *)pkt;
1592 
1593 		evh->evl_encap_proto = htons(ETHERTYPE_VLAN);
1594 		evh->evl_tag = htons(vtag);
1595 		if (ipv6)
1596 			evh->evl_proto = htons(ETHERTYPE_IPV6);
1597 		else
1598 			evh->evl_proto = htons(ETHERTYPE_IP);
1599 
1600 		len += sizeof(*evh);
1601 	}
1602 
1603 	if (ipv6) {
1604 		struct ip6_hdr *ip6 = (void *)&pkt[len];
1605 
1606 		ip6->ip6_vfc = IPV6_VERSION;
1607 		ip6->ip6_plen = htons(sizeof(struct tcphdr));
1608 		ip6->ip6_nxt = IPPROTO_TCP;
1609 		if (open_type == OPEN_TYPE_ACTIVE) {
1610 			ip6->ip6_src = inp->in6p_laddr;
1611 			ip6->ip6_dst = inp->in6p_faddr;
1612 		} else if (open_type == OPEN_TYPE_LISTEN) {
1613 			ip6->ip6_src = inp->in6p_laddr;
1614 			ip6->ip6_dst = ip6->ip6_src;
1615 		}
1616 
1617 		len += sizeof(*ip6);
1618 	} else {
1619 		struct ip *ip = (void *)&pkt[len];
1620 
1621 		ip->ip_v = IPVERSION;
1622 		ip->ip_hl = sizeof(*ip) >> 2;
1623 		ip->ip_tos = inp->inp_ip_tos;
1624 		ip->ip_len = htons(sizeof(struct ip) + sizeof(struct tcphdr));
1625 		ip->ip_ttl = inp->inp_ip_ttl;
1626 		ip->ip_p = IPPROTO_TCP;
1627 		if (open_type == OPEN_TYPE_ACTIVE) {
1628 			ip->ip_src = inp->inp_laddr;
1629 			ip->ip_dst = inp->inp_faddr;
1630 		} else if (open_type == OPEN_TYPE_LISTEN) {
1631 			ip->ip_src = inp->inp_laddr;
1632 			ip->ip_dst = ip->ip_src;
1633 		}
1634 
1635 		len += sizeof(*ip);
1636 	}
1637 
1638 	th = (void *)&pkt[len];
1639 	if (open_type == OPEN_TYPE_ACTIVE) {
1640 		th->th_sport = inp->inp_lport;	/* network byte order already */
1641 		th->th_dport = inp->inp_fport;	/* ditto */
1642 	} else if (open_type == OPEN_TYPE_LISTEN) {
1643 		th->th_sport = inp->inp_lport;	/* network byte order already */
1644 		th->th_dport = th->th_sport;
1645 	}
1646 	len += sizeof(th);
1647 
1648 	*pktlen = *buflen = len;
1649 	return (pkt);
1650 }
1651 
1652 const struct offload_settings *
1653 lookup_offload_policy(struct adapter *sc, int open_type, struct mbuf *m,
1654     uint16_t vtag, struct inpcb *inp)
1655 {
1656 	const struct t4_offload_policy *op;
1657 	char *pkt;
1658 	struct offload_rule *r;
1659 	int i, matched, pktlen, buflen;
1660 	static const struct offload_settings allow_offloading_settings = {
1661 		.offload = 1,
1662 		.rx_coalesce = -1,
1663 		.cong_algo = -1,
1664 		.sched_class = -1,
1665 		.tstamp = -1,
1666 		.sack = -1,
1667 		.nagle = -1,
1668 		.ecn = -1,
1669 		.ddp = -1,
1670 		.tls = -1,
1671 		.txq = QUEUE_RANDOM,
1672 		.rxq = QUEUE_RANDOM,
1673 		.mss = -1,
1674 	};
1675 	static const struct offload_settings disallow_offloading_settings = {
1676 		.offload = 0,
1677 		/* rest is irrelevant when offload is off. */
1678 	};
1679 
1680 	rw_assert(&sc->policy_lock, RA_LOCKED);
1681 
1682 	/*
1683 	 * If there's no Connection Offloading Policy attached to the device
1684 	 * then we need to return a default static policy.  If
1685 	 * "cop_managed_offloading" is true, then we need to disallow
1686 	 * offloading until a COP is attached to the device.  Otherwise we
1687 	 * allow offloading ...
1688 	 */
1689 	op = sc->policy;
1690 	if (op == NULL) {
1691 		if (sc->tt.cop_managed_offloading)
1692 			return (&disallow_offloading_settings);
1693 		else
1694 			return (&allow_offloading_settings);
1695 	}
1696 
1697 	switch (open_type) {
1698 	case OPEN_TYPE_ACTIVE:
1699 	case OPEN_TYPE_LISTEN:
1700 		pkt = prepare_pkt(open_type, vtag, inp, &pktlen, &buflen);
1701 		break;
1702 	case OPEN_TYPE_PASSIVE:
1703 		MPASS(m != NULL);
1704 		pkt = mtod(m, char *);
1705 		MPASS(*pkt == CPL_PASS_ACCEPT_REQ);
1706 		pkt += sizeof(struct cpl_pass_accept_req);
1707 		pktlen = m->m_pkthdr.len - sizeof(struct cpl_pass_accept_req);
1708 		buflen = m->m_len - sizeof(struct cpl_pass_accept_req);
1709 		break;
1710 	default:
1711 		MPASS(0);
1712 		return (&disallow_offloading_settings);
1713 	}
1714 
1715 	if (pkt == NULL || pktlen == 0 || buflen == 0)
1716 		return (&disallow_offloading_settings);
1717 
1718 	matched = 0;
1719 	r = &op->rule[0];
1720 	for (i = 0; i < op->nrules; i++, r++) {
1721 		if (r->open_type != open_type &&
1722 		    r->open_type != OPEN_TYPE_DONTCARE) {
1723 			continue;
1724 		}
1725 		matched = bpf_filter(r->bpf_prog.bf_insns, pkt, pktlen, buflen);
1726 		if (matched)
1727 			break;
1728 	}
1729 
1730 	if (open_type == OPEN_TYPE_ACTIVE || open_type == OPEN_TYPE_LISTEN)
1731 		free(pkt, M_CXGBE);
1732 
1733 	return (matched ? &r->settings : &disallow_offloading_settings);
1734 }
1735 
1736 static void
1737 reclaim_wr_resources(void *arg, int count)
1738 {
1739 	struct tom_data *td = arg;
1740 	STAILQ_HEAD(, wrqe) twr_list = STAILQ_HEAD_INITIALIZER(twr_list);
1741 	struct cpl_act_open_req *cpl;
1742 	u_int opcode, atid, tid;
1743 	struct wrqe *wr;
1744 	struct adapter *sc = td_adapter(td);
1745 
1746 	mtx_lock(&td->unsent_wr_lock);
1747 	STAILQ_SWAP(&td->unsent_wr_list, &twr_list, wrqe);
1748 	mtx_unlock(&td->unsent_wr_lock);
1749 
1750 	while ((wr = STAILQ_FIRST(&twr_list)) != NULL) {
1751 		STAILQ_REMOVE_HEAD(&twr_list, link);
1752 
1753 		cpl = wrtod(wr);
1754 		opcode = GET_OPCODE(cpl);
1755 
1756 		switch (opcode) {
1757 		case CPL_ACT_OPEN_REQ:
1758 		case CPL_ACT_OPEN_REQ6:
1759 			atid = G_TID_TID(be32toh(OPCODE_TID(cpl)));
1760 			CTR2(KTR_CXGBE, "%s: atid %u ", __func__, atid);
1761 			act_open_failure_cleanup(sc, lookup_atid(sc, atid),
1762 						 EHOSTUNREACH);
1763 			free(wr, M_CXGBE);
1764 			break;
1765 		case CPL_PASS_ACCEPT_RPL:
1766 			tid = GET_TID(cpl);
1767 			CTR2(KTR_CXGBE, "%s: tid %u ", __func__, tid);
1768 			synack_failure_cleanup(sc, lookup_tid(sc, tid));
1769 			free(wr, M_CXGBE);
1770 			break;
1771 		default:
1772 			log(LOG_ERR, "%s: leaked work request %p, wr_len %d, "
1773 			    "opcode %x\n", __func__, wr, wr->wr_len, opcode);
1774 			/* WR not freed here; go look at it with a debugger.  */
1775 		}
1776 	}
1777 }
1778 
1779 /*
1780  * Based on do_abort_req.  We treat an abrupt hardware stop as a connection
1781  * abort from the hardware.
1782  */
1783 static void
1784 live_tid_failure_cleanup(struct adapter *sc, struct toepcb *toep, u_int status)
1785 {
1786 	struct inpcb *inp;
1787 	struct tcpcb *tp;
1788 	struct epoch_tracker et;
1789 
1790 	MPASS(!(toep->flags & TPF_SYNQE));
1791 
1792 	inp = toep->inp;
1793 	CURVNET_SET(toep->vnet);
1794 	NET_EPOCH_ENTER(et);	/* for tcp_close */
1795 	INP_WLOCK(inp);
1796 	tp = intotcpcb(inp);
1797 	toep->flags |= TPF_ABORT_SHUTDOWN;
1798 	if ((inp->inp_flags & INP_DROPPED) == 0) {
1799 		struct socket *so = inp->inp_socket;
1800 
1801 		if (so != NULL)
1802 			so_error_set(so, status);
1803 		tp = tcp_close(tp);
1804 		if (tp == NULL)
1805 			INP_WLOCK(inp);	/* re-acquire */
1806 	}
1807 	final_cpl_received(toep);
1808 	NET_EPOCH_EXIT(et);
1809 	CURVNET_RESTORE();
1810 }
1811 
1812 static void
1813 cleanup_stranded_tids(void *arg, int count)
1814 {
1815 	TAILQ_HEAD(, toepcb) tlist = TAILQ_HEAD_INITIALIZER(tlist);
1816 	TAILQ_HEAD(, synq_entry) slist = TAILQ_HEAD_INITIALIZER(slist);
1817 	struct tom_data *td = arg;
1818 	struct adapter *sc = td_adapter(td);
1819 	struct toepcb *toep;
1820 	struct synq_entry *synqe;
1821 
1822 	/* Clean up synq entries. */
1823 	mtx_lock(&td->toep_list_lock);
1824 	TAILQ_SWAP(&td->stranded_synqe, &slist, synq_entry, link);
1825 	mtx_unlock(&td->toep_list_lock);
1826 	while ((synqe = TAILQ_FIRST(&slist)) != NULL) {
1827 		TAILQ_REMOVE(&slist, synqe, link);
1828 		MPASS(synqe->tid >= 0);	/* stale, was kept around for debug */
1829 		synqe->tid = -1;
1830 		synack_failure_cleanup(sc, synqe);
1831 	}
1832 
1833 	/* Clean up in-flight active opens. */
1834 	mtx_lock(&td->toep_list_lock);
1835 	TAILQ_SWAP(&td->stranded_atids, &tlist, toepcb, link);
1836 	mtx_unlock(&td->toep_list_lock);
1837 	while ((toep = TAILQ_FIRST(&tlist)) != NULL) {
1838 		TAILQ_REMOVE(&tlist, toep, link);
1839 		MPASS(toep->tid >= 0);	/* stale, was kept around for debug */
1840 		toep->tid = -1;
1841 		act_open_failure_cleanup(sc, toep, EHOSTUNREACH);
1842 	}
1843 
1844 	/* Clean up live connections. */
1845 	mtx_lock(&td->toep_list_lock);
1846 	TAILQ_SWAP(&td->stranded_tids, &tlist, toepcb, link);
1847 	mtx_unlock(&td->toep_list_lock);
1848 	while ((toep = TAILQ_FIRST(&tlist)) != NULL) {
1849 		TAILQ_REMOVE(&tlist, toep, link);
1850 		MPASS(toep->tid >= 0);	/* stale, was kept around for debug */
1851 		toep->tid = -1;
1852 		live_tid_failure_cleanup(sc, toep, ECONNABORTED);
1853 	}
1854 }
1855 
1856 /*
1857  * Ground control to Major TOM
1858  * Commencing countdown, engines on
1859  */
1860 static int
1861 t4_tom_activate(struct adapter *sc)
1862 {
1863 	struct tom_data *td;
1864 	struct toedev *tod;
1865 	struct vi_info *vi;
1866 	int i, rc, v;
1867 
1868 	ASSERT_SYNCHRONIZED_OP(sc);
1869 
1870 	/* per-adapter softc for TOM */
1871 	td = malloc(sizeof(*td), M_CXGBE, M_ZERO | M_NOWAIT);
1872 	if (td == NULL)
1873 		return (ENOMEM);
1874 
1875 	/* List of TOE PCBs and associated lock */
1876 	mtx_init(&td->toep_list_lock, "PCB list lock", NULL, MTX_DEF);
1877 	TAILQ_INIT(&td->toep_list);
1878 	TAILQ_INIT(&td->synqe_list);
1879 	TAILQ_INIT(&td->stranded_atids);
1880 	TAILQ_INIT(&td->stranded_tids);
1881 	TASK_INIT(&td->cleanup_stranded_tids, 0, cleanup_stranded_tids, td);
1882 
1883 	/* Listen context */
1884 	mtx_init(&td->lctx_hash_lock, "lctx hash lock", NULL, MTX_DEF);
1885 	td->listen_hash = hashinit_flags(LISTEN_HASH_SIZE, M_CXGBE,
1886 	    &td->listen_mask, HASH_NOWAIT);
1887 
1888 	/* List of WRs for which L2 resolution failed */
1889 	mtx_init(&td->unsent_wr_lock, "Unsent WR list lock", NULL, MTX_DEF);
1890 	STAILQ_INIT(&td->unsent_wr_list);
1891 	TASK_INIT(&td->reclaim_wr_resources, 0, reclaim_wr_resources, td);
1892 
1893 	/* TID tables */
1894 	rc = alloc_tid_tabs(sc);
1895 	if (rc != 0)
1896 		goto done;
1897 
1898 	rc = t4_init_ppod_region(&td->pr, &sc->vres.ddp,
1899 	    t4_read_reg(sc, A_ULP_RX_TDDP_PSZ), "TDDP page pods");
1900 	if (rc != 0)
1901 		goto done;
1902 	t4_set_reg_field(sc, A_ULP_RX_TDDP_TAGMASK,
1903 	    V_TDDPTAGMASK(M_TDDPTAGMASK), td->pr.pr_tag_mask);
1904 
1905 	alloc_tcb_history(sc, td);
1906 
1907 	/* toedev ops */
1908 	tod = &td->tod;
1909 	init_toedev(tod);
1910 	tod->tod_softc = sc;
1911 	tod->tod_connect = t4_connect;
1912 	tod->tod_listen_start = t4_listen_start;
1913 	tod->tod_listen_stop = t4_listen_stop;
1914 	tod->tod_rcvd = t4_rcvd;
1915 	tod->tod_output = t4_tod_output;
1916 	tod->tod_send_rst = t4_send_rst;
1917 	tod->tod_send_fin = t4_send_fin;
1918 	tod->tod_pcb_detach = t4_pcb_detach;
1919 	tod->tod_l2_update = t4_l2_update;
1920 	tod->tod_syncache_added = t4_syncache_added;
1921 	tod->tod_syncache_removed = t4_syncache_removed;
1922 	tod->tod_syncache_respond = t4_syncache_respond;
1923 	tod->tod_offload_socket = t4_offload_socket;
1924 	tod->tod_ctloutput = t4_ctloutput;
1925 	tod->tod_tcp_info = t4_tcp_info;
1926 #ifdef KERN_TLS
1927 	tod->tod_alloc_tls_session = t4_alloc_tls_session;
1928 #endif
1929 	tod->tod_pmtu_update = t4_pmtu_update;
1930 
1931 	for_each_port(sc, i) {
1932 		for_each_vi(sc->port[i], v, vi) {
1933 			SETTOEDEV(vi->ifp, &td->tod);
1934 		}
1935 	}
1936 
1937 	sc->tom_softc = td;
1938 	register_toedev(sc->tom_softc);
1939 
1940 done:
1941 	if (rc != 0)
1942 		free_tom_data(sc, td);
1943 	return (rc);
1944 }
1945 
1946 static int
1947 t4_tom_deactivate(struct adapter *sc)
1948 {
1949 	int rc = 0, i, v;
1950 	struct tom_data *td = sc->tom_softc;
1951 	struct vi_info *vi;
1952 
1953 	ASSERT_SYNCHRONIZED_OP(sc);
1954 
1955 	if (td == NULL)
1956 		return (0);	/* XXX. KASSERT? */
1957 
1958 	if (uld_active(sc, ULD_IWARP) || uld_active(sc, ULD_ISCSI))
1959 		return (EBUSY);	/* both iWARP and iSCSI rely on the TOE. */
1960 
1961 	if (sc->offload_map != 0) {
1962 		for_each_port(sc, i) {
1963 			for_each_vi(sc->port[i], v, vi) {
1964 				toe_capability(vi, false);
1965 				if_setcapenablebit(vi->ifp, 0, IFCAP_TOE);
1966 				SETTOEDEV(vi->ifp, NULL);
1967 			}
1968 		}
1969 		MPASS(sc->offload_map == 0);
1970 	}
1971 
1972 	mtx_lock(&td->toep_list_lock);
1973 	if (!TAILQ_EMPTY(&td->toep_list))
1974 		rc = EBUSY;
1975 	MPASS(TAILQ_EMPTY(&td->synqe_list));
1976 	MPASS(TAILQ_EMPTY(&td->stranded_tids));
1977 	mtx_unlock(&td->toep_list_lock);
1978 
1979 	mtx_lock(&td->lctx_hash_lock);
1980 	if (td->lctx_count > 0)
1981 		rc = EBUSY;
1982 	mtx_unlock(&td->lctx_hash_lock);
1983 
1984 	taskqueue_drain(taskqueue_thread, &td->reclaim_wr_resources);
1985 	taskqueue_drain(taskqueue_thread, &td->cleanup_stranded_tids);
1986 	mtx_lock(&td->unsent_wr_lock);
1987 	if (!STAILQ_EMPTY(&td->unsent_wr_list))
1988 		rc = EBUSY;
1989 	mtx_unlock(&td->unsent_wr_lock);
1990 
1991 	if (rc == 0) {
1992 		unregister_toedev(sc->tom_softc);
1993 		free_tom_data(sc, td);
1994 		sc->tom_softc = NULL;
1995 	}
1996 
1997 	return (rc);
1998 }
1999 
2000 static void
2001 stop_atids(struct adapter *sc)
2002 {
2003 	struct tom_data *td = sc->tom_softc;
2004 	struct tid_info *t = &sc->tids;
2005 	struct toepcb *toep;
2006 	int atid;
2007 
2008 	/*
2009 	 * Hashfilters and T6-KTLS are the only other users of atids but they're
2010 	 * both mutually exclusive with TOE.  That means t4_tom owns all the
2011 	 * atids in the table.
2012 	 */
2013 	MPASS(!is_hashfilter(sc));
2014 	if (is_t6(sc))
2015 		MPASS(!(sc->flags & KERN_TLS_ON));
2016 
2017 	/* New atids are not being allocated. */
2018 #ifdef INVARIANTS
2019 	mtx_lock(&t->atid_lock);
2020 	MPASS(t->atid_alloc_stopped == true);
2021 	mtx_unlock(&t->atid_lock);
2022 #endif
2023 
2024 	/*
2025 	 * In-use atids fall in one of these two categories:
2026 	 * a) Those waiting for L2 resolution before being submitted to
2027 	 *    hardware.
2028 	 * b) Those that have been submitted to hardware and are awaiting
2029 	 *    replies that will never arrive because the LLD is stopped.
2030 	 */
2031 	for (atid = 0; atid < t->natids; atid++) {
2032 		toep = lookup_atid(sc, atid);
2033 		if ((uintptr_t)toep >= (uintptr_t)&t->atid_tab[0] &&
2034 		    (uintptr_t)toep < (uintptr_t)&t->atid_tab[t->natids])
2035 			continue;
2036 		if (__predict_false(toep == NULL))
2037 			continue;
2038 		MPASS(toep->tid == atid);
2039 		MPASS(toep->incarnation == sc->incarnation);
2040 		/*
2041 		 * Take the atid out of the lookup table.  toep->tid is stale
2042 		 * after this but useful for debug.
2043 		 */
2044 		CTR(KTR_CXGBE, "%s: atid %d@%d STRANDED, removed from table",
2045 		    __func__, atid, toep->incarnation);
2046 		free_atid(sc, toep->tid);
2047 #if 0
2048 		toep->tid = -1;
2049 #endif
2050 		mtx_lock(&td->toep_list_lock);
2051 		toep->flags &= ~TPF_IN_TOEP_LIST;
2052 		TAILQ_REMOVE(&td->toep_list, toep, link);
2053 		TAILQ_INSERT_TAIL(&td->stranded_atids, toep, link);
2054 		mtx_unlock(&td->toep_list_lock);
2055 	}
2056 	MPASS(atomic_load_int(&t->atids_in_use) == 0);
2057 }
2058 
2059 static void
2060 stop_tids(struct adapter *sc)
2061 {
2062 	struct tom_data *td = sc->tom_softc;
2063 	struct toepcb *toep;
2064 #ifdef INVARIANTS
2065 	struct tid_info *t = &sc->tids;
2066 #endif
2067 
2068 	/*
2069 	 * The LLD's offload queues are stopped so do_act_establish and
2070 	 * do_pass_accept_req cannot run and insert tids in parallel with this
2071 	 * thread.  stop_stid_tab has also run and removed the synq entries'
2072 	 * tids from the table.  The only tids in the table are for connections
2073 	 * at or beyond ESTABLISHED that are still waiting for the final CPL.
2074 	 */
2075 	mtx_lock(&td->toep_list_lock);
2076 	TAILQ_FOREACH(toep, &td->toep_list, link) {
2077 		MPASS(sc->incarnation == toep->incarnation);
2078 		MPASS(toep->tid >= 0);
2079 		MPASS(toep == lookup_tid(sc, toep->tid));
2080 		/* Remove tid from the lookup table immediately. */
2081 		CTR(KTR_CXGBE, "%s: tid %d@%d STRANDED, removed from table",
2082 		    __func__, toep->tid, toep->incarnation);
2083 		remove_tid(sc, toep->tid, toep->ce ? 2 : 1);
2084 #if 0
2085 		/* toep->tid is stale now but left alone for debug. */
2086 		toep->tid = -1;
2087 #endif
2088 		/* All toep in this list will get bulk moved to stranded_tids */
2089 		toep->flags &= ~TPF_IN_TOEP_LIST;
2090 	}
2091 	MPASS(TAILQ_EMPTY(&td->stranded_tids));
2092 	TAILQ_CONCAT(&td->stranded_tids, &td->toep_list, link);
2093 	MPASS(TAILQ_EMPTY(&td->toep_list));
2094 	mtx_unlock(&td->toep_list_lock);
2095 
2096 	MPASS(atomic_load_int(&t->tids_in_use) == 0);
2097 }
2098 
2099 /*
2100  * L2T is stable because
2101  * 1. stop_lld stopped all new allocations.
2102  * 2. stop_lld also stopped the tx wrq so nothing is enqueueing new WRs to the
2103  *    queue or to l2t_entry->wr_list.
2104  * 3. t4_l2t_update is ignoring all L2 updates.
2105  */
2106 static void
2107 stop_tom_l2t(struct adapter *sc)
2108 {
2109 	struct l2t_data *d = sc->l2t;
2110 	struct tom_data *td = sc->tom_softc;
2111 	struct l2t_entry *e;
2112 	struct wrqe *wr;
2113 	int i;
2114 
2115 	/*
2116 	 * This task cannot be enqueued because L2 state changes are not being
2117 	 * processed.  But if it's already scheduled or running then we need to
2118 	 * wait for it to cleanup the atids in the unsent_wr_list.
2119 	 */
2120 	taskqueue_drain(taskqueue_thread, &td->reclaim_wr_resources);
2121 	MPASS(STAILQ_EMPTY(&td->unsent_wr_list));
2122 
2123 	for (i = 0; i < d->l2t_size; i++) {
2124 		e = &d->l2tab[i];
2125 		mtx_lock(&e->lock);
2126 		if (e->state == L2T_STATE_VALID || e->state == L2T_STATE_STALE)
2127 			e->state = L2T_STATE_RESOLVING;
2128 		/*
2129 		 * stop_atids is going to clean up _all_ atids in use, including
2130 		 * these that were pending L2 resolution.  Just discard the WRs.
2131 		 */
2132 		while ((wr = STAILQ_FIRST(&e->wr_list)) != NULL) {
2133 			STAILQ_REMOVE_HEAD(&e->wr_list, link);
2134 			free(wr, M_CXGBE);
2135 		}
2136 		mtx_unlock(&e->lock);
2137 	}
2138 }
2139 
2140 static int
2141 t4_tom_stop(struct adapter *sc)
2142 {
2143 	struct tid_info *t = &sc->tids;
2144 	struct tom_data *td = sc->tom_softc;
2145 
2146 	ASSERT_SYNCHRONIZED_OP(sc);
2147 
2148 	stop_tom_l2t(sc);
2149 	if (atomic_load_int(&t->atids_in_use) > 0)
2150 		stop_atids(sc);
2151 	if (atomic_load_int(&t->stids_in_use) > 0)
2152 		stop_stid_tab(sc);
2153 	if (atomic_load_int(&t->tids_in_use) > 0)
2154 		stop_tids(sc);
2155 	taskqueue_enqueue(taskqueue_thread, &td->cleanup_stranded_tids);
2156 
2157 	/*
2158 	 * L2T and atid_tab are restarted before t4_tom_restart so this assert
2159 	 * is not valid in t4_tom_restart.  This is the next best place for it.
2160 	 */
2161 	MPASS(STAILQ_EMPTY(&td->unsent_wr_list));
2162 
2163 	return (0);
2164 }
2165 
2166 static int
2167 t4_tom_restart(struct adapter *sc)
2168 {
2169 	ASSERT_SYNCHRONIZED_OP(sc);
2170 
2171 	restart_stid_tab(sc);
2172 
2173 	return (0);
2174 }
2175 
2176 static int
2177 t4_ctloutput_tom(struct socket *so, struct sockopt *sopt)
2178 {
2179 	struct tcpcb *tp = sototcpcb(so);
2180 	struct toepcb *toep = tp->t_toe;
2181 	int error, optval;
2182 
2183 	if (sopt->sopt_level == IPPROTO_TCP && sopt->sopt_name == TCP_USE_DDP) {
2184 		if (sopt->sopt_dir != SOPT_SET)
2185 			return (EOPNOTSUPP);
2186 
2187 		if (sopt->sopt_td != NULL) {
2188 			/* Only settable by the kernel. */
2189 			return (EPERM);
2190 		}
2191 
2192 		error = sooptcopyin(sopt, &optval, sizeof(optval),
2193 		    sizeof(optval));
2194 		if (error != 0)
2195 			return (error);
2196 
2197 		if (optval != 0)
2198 			return (t4_enable_ddp_rcv(so, toep));
2199 		else
2200 			return (EOPNOTSUPP);
2201 	}
2202 	return (tcp_ctloutput(so, sopt));
2203 }
2204 
2205 static int
2206 t4_aio_queue_tom(struct socket *so, struct kaiocb *job)
2207 {
2208 	struct tcpcb *tp = sototcpcb(so);
2209 	struct toepcb *toep = tp->t_toe;
2210 	int error;
2211 
2212 	/*
2213 	 * No lock is needed as TOE sockets never change between
2214 	 * active and passive.
2215 	 */
2216 	if (SOLISTENING(so))
2217 		return (EINVAL);
2218 
2219 	if (ulp_mode(toep) == ULP_MODE_TCPDDP ||
2220 	    ulp_mode(toep) == ULP_MODE_NONE) {
2221 		error = t4_aio_queue_ddp(so, job);
2222 		if (error != EOPNOTSUPP)
2223 			return (error);
2224 	}
2225 
2226 	return (t4_aio_queue_aiotx(so, job));
2227 }
2228 
2229 static int
2230 t4_tom_mod_load(void)
2231 {
2232 	/* CPL handlers */
2233 	t4_register_cpl_handler(CPL_GET_TCB_RPL, do_get_tcb_rpl);
2234 	t4_register_shared_cpl_handler(CPL_L2T_WRITE_RPL, do_l2t_write_rpl2,
2235 	    CPL_COOKIE_TOM);
2236 	t4_init_connect_cpl_handlers();
2237 	t4_init_listen_cpl_handlers();
2238 	t4_init_cpl_io_handlers();
2239 
2240 	t4_ddp_mod_load();
2241 	t4_tls_mod_load();
2242 
2243 	bcopy(&tcp_protosw, &toe_protosw, sizeof(toe_protosw));
2244 	toe_protosw.pr_ctloutput = t4_ctloutput_tom;
2245 	toe_protosw.pr_aio_queue = t4_aio_queue_tom;
2246 
2247 	bcopy(&tcp6_protosw, &toe6_protosw, sizeof(toe6_protosw));
2248 	toe6_protosw.pr_ctloutput = t4_ctloutput_tom;
2249 	toe6_protosw.pr_aio_queue = t4_aio_queue_tom;
2250 
2251 	return (t4_register_uld(&tom_uld_info, ULD_TOM));
2252 }
2253 
2254 static void
2255 tom_uninit(struct adapter *sc, void *arg)
2256 {
2257 	bool *ok_to_unload = arg;
2258 
2259 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4tomun"))
2260 		return;
2261 
2262 	/* Try to free resources (works only if no port has IFCAP_TOE) */
2263 	if (uld_active(sc, ULD_TOM) && t4_deactivate_uld(sc, ULD_TOM) != 0)
2264 		*ok_to_unload = false;
2265 
2266 	end_synchronized_op(sc, 0);
2267 }
2268 
2269 static int
2270 t4_tom_mod_unload(void)
2271 {
2272 	bool ok_to_unload = true;
2273 
2274 	t4_iterate(tom_uninit, &ok_to_unload);
2275 	if (!ok_to_unload)
2276 		return (EBUSY);
2277 
2278 	if (t4_unregister_uld(&tom_uld_info, ULD_TOM) == EBUSY)
2279 		return (EBUSY);
2280 
2281 	t4_tls_mod_unload();
2282 	t4_ddp_mod_unload();
2283 
2284 	t4_uninit_connect_cpl_handlers();
2285 	t4_uninit_listen_cpl_handlers();
2286 	t4_uninit_cpl_io_handlers();
2287 	t4_register_shared_cpl_handler(CPL_L2T_WRITE_RPL, NULL, CPL_COOKIE_TOM);
2288 	t4_register_cpl_handler(CPL_GET_TCB_RPL, NULL);
2289 
2290 	return (0);
2291 }
2292 #endif	/* TCP_OFFLOAD */
2293 
2294 static int
2295 t4_tom_modevent(module_t mod, int cmd, void *arg)
2296 {
2297 	int rc = 0;
2298 
2299 #ifdef TCP_OFFLOAD
2300 	switch (cmd) {
2301 	case MOD_LOAD:
2302 		rc = t4_tom_mod_load();
2303 		break;
2304 
2305 	case MOD_UNLOAD:
2306 		rc = t4_tom_mod_unload();
2307 		break;
2308 
2309 	default:
2310 		rc = EINVAL;
2311 	}
2312 #else
2313 	printf("t4_tom: compiled without TCP_OFFLOAD support.\n");
2314 	rc = EOPNOTSUPP;
2315 #endif
2316 	return (rc);
2317 }
2318 
2319 static moduledata_t t4_tom_moddata= {
2320 	"t4_tom",
2321 	t4_tom_modevent,
2322 	0
2323 };
2324 
2325 MODULE_VERSION(t4_tom, 1);
2326 MODULE_DEPEND(t4_tom, toecore, 1, 1, 1);
2327 MODULE_DEPEND(t4_tom, t4nex, 1, 1, 1);
2328 DECLARE_MODULE(t4_tom, t4_tom_moddata, SI_SUB_EXEC, SI_ORDER_ANY);
2329