xref: /freebsd/sys/dev/cxgbe/tom/t4_tom.c (revision 0957b409)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
32 
33 #include "opt_inet.h"
34 #include "opt_inet6.h"
35 #include "opt_ratelimit.h"
36 
37 #include <sys/param.h>
38 #include <sys/types.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/ktr.h>
42 #include <sys/lock.h>
43 #include <sys/limits.h>
44 #include <sys/module.h>
45 #include <sys/protosw.h>
46 #include <sys/domain.h>
47 #include <sys/refcount.h>
48 #include <sys/rmlock.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.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 
68 #ifdef TCP_OFFLOAD
69 #include "common/common.h"
70 #include "common/t4_msg.h"
71 #include "common/t4_regs.h"
72 #include "common/t4_regs_values.h"
73 #include "common/t4_tcb.h"
74 #include "t4_clip.h"
75 #include "tom/t4_tom_l2t.h"
76 #include "tom/t4_tom.h"
77 #include "tom/t4_tls.h"
78 
79 static struct protosw toe_protosw;
80 static struct pr_usrreqs toe_usrreqs;
81 
82 static struct protosw toe6_protosw;
83 static struct pr_usrreqs toe6_usrreqs;
84 
85 /* Module ops */
86 static int t4_tom_mod_load(void);
87 static int t4_tom_mod_unload(void);
88 static int t4_tom_modevent(module_t, int, void *);
89 
90 /* ULD ops and helpers */
91 static int t4_tom_activate(struct adapter *);
92 static int t4_tom_deactivate(struct adapter *);
93 
94 static struct uld_info tom_uld_info = {
95 	.uld_id = ULD_TOM,
96 	.activate = t4_tom_activate,
97 	.deactivate = t4_tom_deactivate,
98 };
99 
100 static void release_offload_resources(struct toepcb *);
101 static int alloc_tid_tabs(struct tid_info *);
102 static void free_tid_tabs(struct tid_info *);
103 static void free_tom_data(struct adapter *, struct tom_data *);
104 static void reclaim_wr_resources(void *, int);
105 
106 struct toepcb *
107 alloc_toepcb(struct vi_info *vi, int txqid, int rxqid, int flags)
108 {
109 	struct port_info *pi = vi->pi;
110 	struct adapter *sc = pi->adapter;
111 	struct toepcb *toep;
112 	int tx_credits, txsd_total, len;
113 
114 	/*
115 	 * The firmware counts tx work request credits in units of 16 bytes
116 	 * each.  Reserve room for an ABORT_REQ so the driver never has to worry
117 	 * about tx credits if it wants to abort a connection.
118 	 */
119 	tx_credits = sc->params.ofldq_wr_cred;
120 	tx_credits -= howmany(sizeof(struct cpl_abort_req), 16);
121 
122 	/*
123 	 * Shortest possible tx work request is a fw_ofld_tx_data_wr + 1 byte
124 	 * immediate payload, and firmware counts tx work request credits in
125 	 * units of 16 byte.  Calculate the maximum work requests possible.
126 	 */
127 	txsd_total = tx_credits /
128 	    howmany(sizeof(struct fw_ofld_tx_data_wr) + 1, 16);
129 
130 	KASSERT(txqid >= vi->first_ofld_txq &&
131 	    txqid < vi->first_ofld_txq + vi->nofldtxq,
132 	    ("%s: txqid %d for vi %p (first %d, n %d)", __func__, txqid, vi,
133 		vi->first_ofld_txq, vi->nofldtxq));
134 
135 	KASSERT(rxqid >= vi->first_ofld_rxq &&
136 	    rxqid < vi->first_ofld_rxq + vi->nofldrxq,
137 	    ("%s: rxqid %d for vi %p (first %d, n %d)", __func__, rxqid, vi,
138 		vi->first_ofld_rxq, vi->nofldrxq));
139 
140 	len = offsetof(struct toepcb, txsd) +
141 	    txsd_total * sizeof(struct ofld_tx_sdesc);
142 
143 	toep = malloc(len, M_CXGBE, M_ZERO | flags);
144 	if (toep == NULL)
145 		return (NULL);
146 
147 	refcount_init(&toep->refcount, 1);
148 	toep->td = sc->tom_softc;
149 	toep->vi = vi;
150 	toep->tc_idx = -1;
151 	toep->tx_total = tx_credits;
152 	toep->tx_credits = tx_credits;
153 	toep->ofld_txq = &sc->sge.ofld_txq[txqid];
154 	toep->ofld_rxq = &sc->sge.ofld_rxq[rxqid];
155 	toep->ctrlq = &sc->sge.ctrlq[pi->port_id];
156 	mbufq_init(&toep->ulp_pduq, INT_MAX);
157 	mbufq_init(&toep->ulp_pdu_reclaimq, INT_MAX);
158 	toep->txsd_total = txsd_total;
159 	toep->txsd_avail = txsd_total;
160 	toep->txsd_pidx = 0;
161 	toep->txsd_cidx = 0;
162 	aiotx_init_toep(toep);
163 
164 	return (toep);
165 }
166 
167 struct toepcb *
168 hold_toepcb(struct toepcb *toep)
169 {
170 
171 	refcount_acquire(&toep->refcount);
172 	return (toep);
173 }
174 
175 void
176 free_toepcb(struct toepcb *toep)
177 {
178 
179 	if (refcount_release(&toep->refcount) == 0)
180 		return;
181 
182 	KASSERT(!(toep->flags & TPF_ATTACHED),
183 	    ("%s: attached to an inpcb", __func__));
184 	KASSERT(!(toep->flags & TPF_CPL_PENDING),
185 	    ("%s: CPL pending", __func__));
186 
187 	if (toep->ulp_mode == ULP_MODE_TCPDDP)
188 		ddp_uninit_toep(toep);
189 	tls_uninit_toep(toep);
190 	free(toep, M_CXGBE);
191 }
192 
193 /*
194  * Set up the socket for TCP offload.
195  */
196 void
197 offload_socket(struct socket *so, struct toepcb *toep)
198 {
199 	struct tom_data *td = toep->td;
200 	struct inpcb *inp = sotoinpcb(so);
201 	struct tcpcb *tp = intotcpcb(inp);
202 	struct sockbuf *sb;
203 
204 	INP_WLOCK_ASSERT(inp);
205 
206 	/* Update socket */
207 	sb = &so->so_snd;
208 	SOCKBUF_LOCK(sb);
209 	sb->sb_flags |= SB_NOCOALESCE;
210 	SOCKBUF_UNLOCK(sb);
211 	sb = &so->so_rcv;
212 	SOCKBUF_LOCK(sb);
213 	sb->sb_flags |= SB_NOCOALESCE;
214 	if (inp->inp_vflag & INP_IPV6)
215 		so->so_proto = &toe6_protosw;
216 	else
217 		so->so_proto = &toe_protosw;
218 	SOCKBUF_UNLOCK(sb);
219 
220 	/* Update TCP PCB */
221 	tp->tod = &td->tod;
222 	tp->t_toe = toep;
223 	tp->t_flags |= TF_TOE;
224 
225 	/* Install an extra hold on inp */
226 	toep->inp = inp;
227 	toep->flags |= TPF_ATTACHED;
228 	in_pcbref(inp);
229 
230 	/* Add the TOE PCB to the active list */
231 	mtx_lock(&td->toep_list_lock);
232 	TAILQ_INSERT_HEAD(&td->toep_list, toep, link);
233 	mtx_unlock(&td->toep_list_lock);
234 }
235 
236 /* This is _not_ the normal way to "unoffload" a socket. */
237 void
238 undo_offload_socket(struct socket *so)
239 {
240 	struct inpcb *inp = sotoinpcb(so);
241 	struct tcpcb *tp = intotcpcb(inp);
242 	struct toepcb *toep = tp->t_toe;
243 	struct tom_data *td = toep->td;
244 	struct sockbuf *sb;
245 
246 	INP_WLOCK_ASSERT(inp);
247 
248 	sb = &so->so_snd;
249 	SOCKBUF_LOCK(sb);
250 	sb->sb_flags &= ~SB_NOCOALESCE;
251 	SOCKBUF_UNLOCK(sb);
252 	sb = &so->so_rcv;
253 	SOCKBUF_LOCK(sb);
254 	sb->sb_flags &= ~SB_NOCOALESCE;
255 	SOCKBUF_UNLOCK(sb);
256 
257 	tp->tod = NULL;
258 	tp->t_toe = NULL;
259 	tp->t_flags &= ~TF_TOE;
260 
261 	toep->inp = NULL;
262 	toep->flags &= ~TPF_ATTACHED;
263 	if (in_pcbrele_wlocked(inp))
264 		panic("%s: inp freed.", __func__);
265 
266 	mtx_lock(&td->toep_list_lock);
267 	TAILQ_REMOVE(&td->toep_list, toep, link);
268 	mtx_unlock(&td->toep_list_lock);
269 }
270 
271 static void
272 release_offload_resources(struct toepcb *toep)
273 {
274 	struct tom_data *td = toep->td;
275 	struct adapter *sc = td_adapter(td);
276 	int tid = toep->tid;
277 
278 	KASSERT(!(toep->flags & TPF_CPL_PENDING),
279 	    ("%s: %p has CPL pending.", __func__, toep));
280 	KASSERT(!(toep->flags & TPF_ATTACHED),
281 	    ("%s: %p is still attached.", __func__, toep));
282 
283 	CTR5(KTR_CXGBE, "%s: toep %p (tid %d, l2te %p, ce %p)",
284 	    __func__, toep, tid, toep->l2te, toep->ce);
285 
286 	/*
287 	 * These queues should have been emptied at approximately the same time
288 	 * that a normal connection's socket's so_snd would have been purged or
289 	 * drained.  Do _not_ clean up here.
290 	 */
291 	MPASS(mbufq_len(&toep->ulp_pduq) == 0);
292 	MPASS(mbufq_len(&toep->ulp_pdu_reclaimq) == 0);
293 #ifdef INVARIANTS
294 	if (toep->ulp_mode == ULP_MODE_TCPDDP)
295 		ddp_assert_empty(toep);
296 #endif
297 
298 	if (toep->l2te)
299 		t4_l2t_release(toep->l2te);
300 
301 	if (tid >= 0) {
302 		remove_tid(sc, tid, toep->ce ? 2 : 1);
303 		release_tid(sc, tid, toep->ctrlq);
304 	}
305 
306 	if (toep->ce)
307 		t4_release_lip(sc, toep->ce);
308 
309 	if (toep->tc_idx != -1)
310 		t4_release_cl_rl(sc, toep->vi->pi->port_id, toep->tc_idx);
311 
312 	mtx_lock(&td->toep_list_lock);
313 	TAILQ_REMOVE(&td->toep_list, toep, link);
314 	mtx_unlock(&td->toep_list_lock);
315 
316 	free_toepcb(toep);
317 }
318 
319 /*
320  * The kernel is done with the TCP PCB and this is our opportunity to unhook the
321  * toepcb hanging off of it.  If the TOE driver is also done with the toepcb (no
322  * pending CPL) then it is time to release all resources tied to the toepcb.
323  *
324  * Also gets called when an offloaded active open fails and the TOM wants the
325  * kernel to take the TCP PCB back.
326  */
327 static void
328 t4_pcb_detach(struct toedev *tod __unused, struct tcpcb *tp)
329 {
330 #if defined(KTR) || defined(INVARIANTS)
331 	struct inpcb *inp = tp->t_inpcb;
332 #endif
333 	struct toepcb *toep = tp->t_toe;
334 
335 	INP_WLOCK_ASSERT(inp);
336 
337 	KASSERT(toep != NULL, ("%s: toep is NULL", __func__));
338 	KASSERT(toep->flags & TPF_ATTACHED,
339 	    ("%s: not attached", __func__));
340 
341 #ifdef KTR
342 	if (tp->t_state == TCPS_SYN_SENT) {
343 		CTR6(KTR_CXGBE, "%s: atid %d, toep %p (0x%x), inp %p (0x%x)",
344 		    __func__, toep->tid, toep, toep->flags, inp,
345 		    inp->inp_flags);
346 	} else {
347 		CTR6(KTR_CXGBE,
348 		    "t4_pcb_detach: tid %d (%s), toep %p (0x%x), inp %p (0x%x)",
349 		    toep->tid, tcpstates[tp->t_state], toep, toep->flags, inp,
350 		    inp->inp_flags);
351 	}
352 #endif
353 
354 	tp->t_toe = NULL;
355 	tp->t_flags &= ~TF_TOE;
356 	toep->flags &= ~TPF_ATTACHED;
357 
358 	if (!(toep->flags & TPF_CPL_PENDING))
359 		release_offload_resources(toep);
360 }
361 
362 /*
363  * setsockopt handler.
364  */
365 static void
366 t4_ctloutput(struct toedev *tod, struct tcpcb *tp, int dir, int name)
367 {
368 	struct adapter *sc = tod->tod_softc;
369 	struct toepcb *toep = tp->t_toe;
370 
371 	if (dir == SOPT_GET)
372 		return;
373 
374 	CTR4(KTR_CXGBE, "%s: tp %p, dir %u, name %u", __func__, tp, dir, name);
375 
376 	switch (name) {
377 	case TCP_NODELAY:
378 		if (tp->t_state != TCPS_ESTABLISHED)
379 			break;
380 		t4_set_tcb_field(sc, toep->ctrlq, toep, W_TCB_T_FLAGS,
381 		    V_TF_NAGLE(1), V_TF_NAGLE(tp->t_flags & TF_NODELAY ? 0 : 1),
382 		    0, 0);
383 		break;
384 	default:
385 		break;
386 	}
387 }
388 
389 static inline int
390 get_tcb_bit(u_char *tcb, int bit)
391 {
392 	int ix, shift;
393 
394 	ix = 127 - (bit >> 3);
395 	shift = bit & 0x7;
396 
397 	return ((tcb[ix] >> shift) & 1);
398 }
399 
400 static inline uint64_t
401 get_tcb_bits(u_char *tcb, int hi, int lo)
402 {
403 	uint64_t rc = 0;
404 
405 	while (hi >= lo) {
406 		rc = (rc << 1) | get_tcb_bit(tcb, hi);
407 		--hi;
408 	}
409 
410 	return (rc);
411 }
412 
413 /*
414  * Called by the kernel to allow the TOE driver to "refine" values filled up in
415  * the tcp_info for an offloaded connection.
416  */
417 static void
418 t4_tcp_info(struct toedev *tod, struct tcpcb *tp, struct tcp_info *ti)
419 {
420 	int i, j, k, rc;
421 	struct adapter *sc = tod->tod_softc;
422 	struct toepcb *toep = tp->t_toe;
423 	uint32_t addr, v;
424 	uint32_t buf[TCB_SIZE / sizeof(uint32_t)];
425 	u_char *tcb, tmp;
426 
427 	INP_WLOCK_ASSERT(tp->t_inpcb);
428 	MPASS(ti != NULL);
429 
430 	ti->tcpi_toe_tid = toep->tid;
431 
432 	addr = t4_read_reg(sc, A_TP_CMM_TCB_BASE) + toep->tid * TCB_SIZE;
433 	rc = read_via_memwin(sc, 2, addr, &buf[0], TCB_SIZE);
434 	if (rc != 0)
435 		return;
436 
437 	tcb = (u_char *)&buf[0];
438 	for (i = 0, j = TCB_SIZE - 16; i < j; i += 16, j -= 16) {
439 		for (k = 0; k < 16; k++) {
440 			tmp = tcb[i + k];
441 			tcb[i + k] = tcb[j + k];
442 			tcb[j + k] = tmp;
443 		}
444 	}
445 
446 	ti->tcpi_state = get_tcb_bits(tcb, 115, 112);
447 
448 	v = get_tcb_bits(tcb, 271, 256);
449 	ti->tcpi_rtt = tcp_ticks_to_us(sc, v);
450 
451 	v = get_tcb_bits(tcb, 287, 272);
452 	ti->tcpi_rttvar = tcp_ticks_to_us(sc, v);
453 
454 	ti->tcpi_snd_ssthresh = get_tcb_bits(tcb, 487, 460);
455 	ti->tcpi_snd_cwnd = get_tcb_bits(tcb, 459, 432);
456 	ti->tcpi_rcv_nxt = get_tcb_bits(tcb, 553, 522);
457 
458 	ti->tcpi_snd_nxt = get_tcb_bits(tcb, 319, 288) -
459 	    get_tcb_bits(tcb, 375, 348);
460 
461 	/* Receive window being advertised by us. */
462 	ti->tcpi_rcv_space = get_tcb_bits(tcb, 581, 554);
463 
464 	/* Send window ceiling. */
465 	v = get_tcb_bits(tcb, 159, 144) << get_tcb_bits(tcb, 131, 128);
466 	ti->tcpi_snd_wnd = min(v, ti->tcpi_snd_cwnd);
467 }
468 
469 /*
470  * The TOE driver will not receive any more CPLs for the tid associated with the
471  * toepcb; release the hold on the inpcb.
472  */
473 void
474 final_cpl_received(struct toepcb *toep)
475 {
476 	struct inpcb *inp = toep->inp;
477 
478 	KASSERT(inp != NULL, ("%s: inp is NULL", __func__));
479 	INP_WLOCK_ASSERT(inp);
480 	KASSERT(toep->flags & TPF_CPL_PENDING,
481 	    ("%s: CPL not pending already?", __func__));
482 
483 	CTR6(KTR_CXGBE, "%s: tid %d, toep %p (0x%x), inp %p (0x%x)",
484 	    __func__, toep->tid, toep, toep->flags, inp, inp->inp_flags);
485 
486 	if (toep->ulp_mode == ULP_MODE_TCPDDP)
487 		release_ddp_resources(toep);
488 	toep->inp = NULL;
489 	toep->flags &= ~TPF_CPL_PENDING;
490 	mbufq_drain(&toep->ulp_pdu_reclaimq);
491 
492 	if (!(toep->flags & TPF_ATTACHED))
493 		release_offload_resources(toep);
494 
495 	if (!in_pcbrele_wlocked(inp))
496 		INP_WUNLOCK(inp);
497 }
498 
499 void
500 insert_tid(struct adapter *sc, int tid, void *ctx, int ntids)
501 {
502 	struct tid_info *t = &sc->tids;
503 
504 	MPASS(tid >= t->tid_base);
505 	MPASS(tid - t->tid_base < t->ntids);
506 
507 	t->tid_tab[tid - t->tid_base] = ctx;
508 	atomic_add_int(&t->tids_in_use, ntids);
509 }
510 
511 void *
512 lookup_tid(struct adapter *sc, int tid)
513 {
514 	struct tid_info *t = &sc->tids;
515 
516 	return (t->tid_tab[tid - t->tid_base]);
517 }
518 
519 void
520 update_tid(struct adapter *sc, int tid, void *ctx)
521 {
522 	struct tid_info *t = &sc->tids;
523 
524 	t->tid_tab[tid - t->tid_base] = ctx;
525 }
526 
527 void
528 remove_tid(struct adapter *sc, int tid, int ntids)
529 {
530 	struct tid_info *t = &sc->tids;
531 
532 	t->tid_tab[tid - t->tid_base] = NULL;
533 	atomic_subtract_int(&t->tids_in_use, ntids);
534 }
535 
536 /*
537  * What mtu_idx to use, given a 4-tuple.  Note that both s->mss and tcp_mssopt
538  * have the MSS that we should advertise in our SYN.  Advertised MSS doesn't
539  * account for any TCP options so the effective MSS (only payload, no headers or
540  * options) could be different.  We fill up tp->t_maxseg with the effective MSS
541  * at the end of the 3-way handshake.
542  */
543 int
544 find_best_mtu_idx(struct adapter *sc, struct in_conninfo *inc,
545     struct offload_settings *s)
546 {
547 	unsigned short *mtus = &sc->params.mtus[0];
548 	int i, mss, mtu;
549 
550 	MPASS(inc != NULL);
551 
552 	mss = s->mss > 0 ? s->mss : tcp_mssopt(inc);
553 	if (inc->inc_flags & INC_ISIPV6)
554 		mtu = mss + sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
555 	else
556 		mtu = mss + sizeof(struct ip) + sizeof(struct tcphdr);
557 
558 	for (i = 0; i < NMTUS - 1 && mtus[i + 1] <= mtu; i++)
559 		continue;
560 
561 	return (i);
562 }
563 
564 /*
565  * Determine the receive window size for a socket.
566  */
567 u_long
568 select_rcv_wnd(struct socket *so)
569 {
570 	unsigned long wnd;
571 
572 	SOCKBUF_LOCK_ASSERT(&so->so_rcv);
573 
574 	wnd = sbspace(&so->so_rcv);
575 	if (wnd < MIN_RCV_WND)
576 		wnd = MIN_RCV_WND;
577 
578 	return min(wnd, MAX_RCV_WND);
579 }
580 
581 int
582 select_rcv_wscale(void)
583 {
584 	int wscale = 0;
585 	unsigned long space = sb_max;
586 
587 	if (space > MAX_RCV_WND)
588 		space = MAX_RCV_WND;
589 
590 	while (wscale < TCP_MAX_WINSHIFT && (TCP_MAXWIN << wscale) < space)
591 		wscale++;
592 
593 	return (wscale);
594 }
595 
596 /*
597  * socket so could be a listening socket too.
598  */
599 uint64_t
600 calc_opt0(struct socket *so, struct vi_info *vi, struct l2t_entry *e,
601     int mtu_idx, int rscale, int rx_credits, int ulp_mode,
602     struct offload_settings *s)
603 {
604 	int keepalive;
605 	uint64_t opt0;
606 
607 	MPASS(so != NULL);
608 	MPASS(vi != NULL);
609 	KASSERT(rx_credits <= M_RCV_BUFSIZ,
610 	    ("%s: rcv_bufsiz too high", __func__));
611 
612 	opt0 = F_TCAM_BYPASS | V_WND_SCALE(rscale) | V_MSS_IDX(mtu_idx) |
613 	    V_ULP_MODE(ulp_mode) | V_RCV_BUFSIZ(rx_credits) |
614 	    V_L2T_IDX(e->idx) | V_SMAC_SEL(vi->smt_idx) |
615 	    V_TX_CHAN(vi->pi->tx_chan);
616 
617 	keepalive = tcp_always_keepalive || so_options_get(so) & SO_KEEPALIVE;
618 	opt0 |= V_KEEP_ALIVE(keepalive != 0);
619 
620 	if (s->nagle < 0) {
621 		struct inpcb *inp = sotoinpcb(so);
622 		struct tcpcb *tp = intotcpcb(inp);
623 
624 		opt0 |= V_NAGLE((tp->t_flags & TF_NODELAY) == 0);
625 	} else
626 		opt0 |= V_NAGLE(s->nagle != 0);
627 
628 	return htobe64(opt0);
629 }
630 
631 uint64_t
632 select_ntuple(struct vi_info *vi, struct l2t_entry *e)
633 {
634 	struct adapter *sc = vi->pi->adapter;
635 	struct tp_params *tp = &sc->params.tp;
636 	uint16_t viid = vi->viid;
637 	uint64_t ntuple = 0;
638 
639 	/*
640 	 * Initialize each of the fields which we care about which are present
641 	 * in the Compressed Filter Tuple.
642 	 */
643 	if (tp->vlan_shift >= 0 && EVL_VLANOFTAG(e->vlan) != CPL_L2T_VLAN_NONE)
644 		ntuple |= (uint64_t)(F_FT_VLAN_VLD | e->vlan) << tp->vlan_shift;
645 
646 	if (tp->port_shift >= 0)
647 		ntuple |= (uint64_t)e->lport << tp->port_shift;
648 
649 	if (tp->protocol_shift >= 0)
650 		ntuple |= (uint64_t)IPPROTO_TCP << tp->protocol_shift;
651 
652 	if (tp->vnic_shift >= 0 && tp->ingress_config & F_VNIC) {
653 		uint32_t vf = G_FW_VIID_VIN(viid);
654 		uint32_t pf = G_FW_VIID_PFN(viid);
655 		uint32_t vld = G_FW_VIID_VIVLD(viid);
656 
657 		ntuple |= (uint64_t)(V_FT_VNID_ID_VF(vf) | V_FT_VNID_ID_PF(pf) |
658 		    V_FT_VNID_ID_VLD(vld)) << tp->vnic_shift;
659 	}
660 
661 	if (is_t4(sc))
662 		return (htobe32((uint32_t)ntuple));
663 	else
664 		return (htobe64(V_FILTER_TUPLE(ntuple)));
665 }
666 
667 static int
668 is_tls_sock(struct socket *so, struct adapter *sc)
669 {
670 	struct inpcb *inp = sotoinpcb(so);
671 	int i, rc;
672 
673 	/* XXX: Eventually add a SO_WANT_TLS socket option perhaps? */
674 	rc = 0;
675 	ADAPTER_LOCK(sc);
676 	for (i = 0; i < sc->tt.num_tls_rx_ports; i++) {
677 		if (inp->inp_lport == htons(sc->tt.tls_rx_ports[i]) ||
678 		    inp->inp_fport == htons(sc->tt.tls_rx_ports[i])) {
679 			rc = 1;
680 			break;
681 		}
682 	}
683 	ADAPTER_UNLOCK(sc);
684 	return (rc);
685 }
686 
687 int
688 select_ulp_mode(struct socket *so, struct adapter *sc,
689     struct offload_settings *s)
690 {
691 
692 	if (can_tls_offload(sc) &&
693 	    (s->tls > 0 || (s->tls < 0 && is_tls_sock(so, sc))))
694 		return (ULP_MODE_TLS);
695 	else if (s->ddp > 0 ||
696 	    (s->ddp < 0 && sc->tt.ddp && (so->so_options & SO_NO_DDP) == 0))
697 		return (ULP_MODE_TCPDDP);
698 	else
699 		return (ULP_MODE_NONE);
700 }
701 
702 void
703 set_ulp_mode(struct toepcb *toep, int ulp_mode)
704 {
705 
706 	CTR4(KTR_CXGBE, "%s: toep %p (tid %d) ulp_mode %d",
707 	    __func__, toep, toep->tid, ulp_mode);
708 	toep->ulp_mode = ulp_mode;
709 	tls_init_toep(toep);
710 	if (toep->ulp_mode == ULP_MODE_TCPDDP)
711 		ddp_init_toep(toep);
712 }
713 
714 int
715 negative_advice(int status)
716 {
717 
718 	return (status == CPL_ERR_RTX_NEG_ADVICE ||
719 	    status == CPL_ERR_PERSIST_NEG_ADVICE ||
720 	    status == CPL_ERR_KEEPALV_NEG_ADVICE);
721 }
722 
723 static int
724 alloc_tid_tab(struct tid_info *t, int flags)
725 {
726 
727 	MPASS(t->ntids > 0);
728 	MPASS(t->tid_tab == NULL);
729 
730 	t->tid_tab = malloc(t->ntids * sizeof(*t->tid_tab), M_CXGBE,
731 	    M_ZERO | flags);
732 	if (t->tid_tab == NULL)
733 		return (ENOMEM);
734 	atomic_store_rel_int(&t->tids_in_use, 0);
735 
736 	return (0);
737 }
738 
739 static void
740 free_tid_tab(struct tid_info *t)
741 {
742 
743 	KASSERT(t->tids_in_use == 0,
744 	    ("%s: %d tids still in use.", __func__, t->tids_in_use));
745 
746 	free(t->tid_tab, M_CXGBE);
747 	t->tid_tab = NULL;
748 }
749 
750 static int
751 alloc_stid_tab(struct tid_info *t, int flags)
752 {
753 
754 	MPASS(t->nstids > 0);
755 	MPASS(t->stid_tab == NULL);
756 
757 	t->stid_tab = malloc(t->nstids * sizeof(*t->stid_tab), M_CXGBE,
758 	    M_ZERO | flags);
759 	if (t->stid_tab == NULL)
760 		return (ENOMEM);
761 	mtx_init(&t->stid_lock, "stid lock", NULL, MTX_DEF);
762 	t->stids_in_use = 0;
763 	TAILQ_INIT(&t->stids);
764 	t->nstids_free_head = t->nstids;
765 
766 	return (0);
767 }
768 
769 static void
770 free_stid_tab(struct tid_info *t)
771 {
772 
773 	KASSERT(t->stids_in_use == 0,
774 	    ("%s: %d tids still in use.", __func__, t->stids_in_use));
775 
776 	if (mtx_initialized(&t->stid_lock))
777 		mtx_destroy(&t->stid_lock);
778 	free(t->stid_tab, M_CXGBE);
779 	t->stid_tab = NULL;
780 }
781 
782 static void
783 free_tid_tabs(struct tid_info *t)
784 {
785 
786 	free_tid_tab(t);
787 	free_atid_tab(t);
788 	free_stid_tab(t);
789 }
790 
791 static int
792 alloc_tid_tabs(struct tid_info *t)
793 {
794 	int rc;
795 
796 	rc = alloc_tid_tab(t, M_NOWAIT);
797 	if (rc != 0)
798 		goto failed;
799 
800 	rc = alloc_atid_tab(t, M_NOWAIT);
801 	if (rc != 0)
802 		goto failed;
803 
804 	rc = alloc_stid_tab(t, M_NOWAIT);
805 	if (rc != 0)
806 		goto failed;
807 
808 	return (0);
809 failed:
810 	free_tid_tabs(t);
811 	return (rc);
812 }
813 
814 static void
815 free_tom_data(struct adapter *sc, struct tom_data *td)
816 {
817 
818 	ASSERT_SYNCHRONIZED_OP(sc);
819 
820 	KASSERT(TAILQ_EMPTY(&td->toep_list),
821 	    ("%s: TOE PCB list is not empty.", __func__));
822 	KASSERT(td->lctx_count == 0,
823 	    ("%s: lctx hash table is not empty.", __func__));
824 
825 	t4_free_ppod_region(&td->pr);
826 
827 	if (td->listen_mask != 0)
828 		hashdestroy(td->listen_hash, M_CXGBE, td->listen_mask);
829 
830 	if (mtx_initialized(&td->unsent_wr_lock))
831 		mtx_destroy(&td->unsent_wr_lock);
832 	if (mtx_initialized(&td->lctx_hash_lock))
833 		mtx_destroy(&td->lctx_hash_lock);
834 	if (mtx_initialized(&td->toep_list_lock))
835 		mtx_destroy(&td->toep_list_lock);
836 
837 	free_tid_tabs(&sc->tids);
838 	free(td, M_CXGBE);
839 }
840 
841 static char *
842 prepare_pkt(int open_type, uint16_t vtag, struct inpcb *inp, int *pktlen,
843     int *buflen)
844 {
845 	char *pkt;
846 	struct tcphdr *th;
847 	int ipv6, len;
848 	const int maxlen =
849 	    max(sizeof(struct ether_header), sizeof(struct ether_vlan_header)) +
850 	    max(sizeof(struct ip), sizeof(struct ip6_hdr)) +
851 	    sizeof(struct tcphdr);
852 
853 	MPASS(open_type == OPEN_TYPE_ACTIVE || open_type == OPEN_TYPE_LISTEN);
854 
855 	pkt = malloc(maxlen, M_CXGBE, M_ZERO | M_NOWAIT);
856 	if (pkt == NULL)
857 		return (NULL);
858 
859 	ipv6 = inp->inp_vflag & INP_IPV6;
860 	len = 0;
861 
862 	if (EVL_VLANOFTAG(vtag) == 0xfff) {
863 		struct ether_header *eh = (void *)pkt;
864 
865 		if (ipv6)
866 			eh->ether_type = htons(ETHERTYPE_IPV6);
867 		else
868 			eh->ether_type = htons(ETHERTYPE_IP);
869 
870 		len += sizeof(*eh);
871 	} else {
872 		struct ether_vlan_header *evh = (void *)pkt;
873 
874 		evh->evl_encap_proto = htons(ETHERTYPE_VLAN);
875 		evh->evl_tag = htons(vtag);
876 		if (ipv6)
877 			evh->evl_proto = htons(ETHERTYPE_IPV6);
878 		else
879 			evh->evl_proto = htons(ETHERTYPE_IP);
880 
881 		len += sizeof(*evh);
882 	}
883 
884 	if (ipv6) {
885 		struct ip6_hdr *ip6 = (void *)&pkt[len];
886 
887 		ip6->ip6_vfc = IPV6_VERSION;
888 		ip6->ip6_plen = htons(sizeof(struct tcphdr));
889 		ip6->ip6_nxt = IPPROTO_TCP;
890 		if (open_type == OPEN_TYPE_ACTIVE) {
891 			ip6->ip6_src = inp->in6p_laddr;
892 			ip6->ip6_dst = inp->in6p_faddr;
893 		} else if (open_type == OPEN_TYPE_LISTEN) {
894 			ip6->ip6_src = inp->in6p_laddr;
895 			ip6->ip6_dst = ip6->ip6_src;
896 		}
897 
898 		len += sizeof(*ip6);
899 	} else {
900 		struct ip *ip = (void *)&pkt[len];
901 
902 		ip->ip_v = IPVERSION;
903 		ip->ip_hl = sizeof(*ip) >> 2;
904 		ip->ip_tos = inp->inp_ip_tos;
905 		ip->ip_len = htons(sizeof(struct ip) + sizeof(struct tcphdr));
906 		ip->ip_ttl = inp->inp_ip_ttl;
907 		ip->ip_p = IPPROTO_TCP;
908 		if (open_type == OPEN_TYPE_ACTIVE) {
909 			ip->ip_src = inp->inp_laddr;
910 			ip->ip_dst = inp->inp_faddr;
911 		} else if (open_type == OPEN_TYPE_LISTEN) {
912 			ip->ip_src = inp->inp_laddr;
913 			ip->ip_dst = ip->ip_src;
914 		}
915 
916 		len += sizeof(*ip);
917 	}
918 
919 	th = (void *)&pkt[len];
920 	if (open_type == OPEN_TYPE_ACTIVE) {
921 		th->th_sport = inp->inp_lport;	/* network byte order already */
922 		th->th_dport = inp->inp_fport;	/* ditto */
923 	} else if (open_type == OPEN_TYPE_LISTEN) {
924 		th->th_sport = inp->inp_lport;	/* network byte order already */
925 		th->th_dport = th->th_sport;
926 	}
927 	len += sizeof(th);
928 
929 	*pktlen = *buflen = len;
930 	return (pkt);
931 }
932 
933 const struct offload_settings *
934 lookup_offload_policy(struct adapter *sc, int open_type, struct mbuf *m,
935     uint16_t vtag, struct inpcb *inp)
936 {
937 	const struct t4_offload_policy *op;
938 	char *pkt;
939 	struct offload_rule *r;
940 	int i, matched, pktlen, buflen;
941 	static const struct offload_settings allow_offloading_settings = {
942 		.offload = 1,
943 		.rx_coalesce = -1,
944 		.cong_algo = -1,
945 		.sched_class = -1,
946 		.tstamp = -1,
947 		.sack = -1,
948 		.nagle = -1,
949 		.ecn = -1,
950 		.ddp = -1,
951 		.tls = -1,
952 		.txq = -1,
953 		.rxq = -1,
954 		.mss = -1,
955 	};
956 	static const struct offload_settings disallow_offloading_settings = {
957 		.offload = 0,
958 		/* rest is irrelevant when offload is off. */
959 	};
960 
961 	rw_assert(&sc->policy_lock, RA_LOCKED);
962 
963 	/*
964 	 * If there's no Connection Offloading Policy attached to the device
965 	 * then we need to return a default static policy.  If
966 	 * "cop_managed_offloading" is true, then we need to disallow
967 	 * offloading until a COP is attached to the device.  Otherwise we
968 	 * allow offloading ...
969 	 */
970 	op = sc->policy;
971 	if (op == NULL) {
972 		if (sc->tt.cop_managed_offloading)
973 			return (&disallow_offloading_settings);
974 		else
975 			return (&allow_offloading_settings);
976 	}
977 
978 	switch (open_type) {
979 	case OPEN_TYPE_ACTIVE:
980 	case OPEN_TYPE_LISTEN:
981 		pkt = prepare_pkt(open_type, vtag, inp, &pktlen, &buflen);
982 		break;
983 	case OPEN_TYPE_PASSIVE:
984 		MPASS(m != NULL);
985 		pkt = mtod(m, char *);
986 		MPASS(*pkt == CPL_PASS_ACCEPT_REQ);
987 		pkt += sizeof(struct cpl_pass_accept_req);
988 		pktlen = m->m_pkthdr.len - sizeof(struct cpl_pass_accept_req);
989 		buflen = m->m_len - sizeof(struct cpl_pass_accept_req);
990 		break;
991 	default:
992 		MPASS(0);
993 		return (&disallow_offloading_settings);
994 	}
995 
996 	if (pkt == NULL || pktlen == 0 || buflen == 0)
997 		return (&disallow_offloading_settings);
998 
999 	matched = 0;
1000 	r = &op->rule[0];
1001 	for (i = 0; i < op->nrules; i++, r++) {
1002 		if (r->open_type != open_type &&
1003 		    r->open_type != OPEN_TYPE_DONTCARE) {
1004 			continue;
1005 		}
1006 		matched = bpf_filter(r->bpf_prog.bf_insns, pkt, pktlen, buflen);
1007 		if (matched)
1008 			break;
1009 	}
1010 
1011 	if (open_type == OPEN_TYPE_ACTIVE || open_type == OPEN_TYPE_LISTEN)
1012 		free(pkt, M_CXGBE);
1013 
1014 	return (matched ? &r->settings : &disallow_offloading_settings);
1015 }
1016 
1017 static void
1018 reclaim_wr_resources(void *arg, int count)
1019 {
1020 	struct tom_data *td = arg;
1021 	STAILQ_HEAD(, wrqe) twr_list = STAILQ_HEAD_INITIALIZER(twr_list);
1022 	struct cpl_act_open_req *cpl;
1023 	u_int opcode, atid, tid;
1024 	struct wrqe *wr;
1025 	struct adapter *sc = td_adapter(td);
1026 
1027 	mtx_lock(&td->unsent_wr_lock);
1028 	STAILQ_SWAP(&td->unsent_wr_list, &twr_list, wrqe);
1029 	mtx_unlock(&td->unsent_wr_lock);
1030 
1031 	while ((wr = STAILQ_FIRST(&twr_list)) != NULL) {
1032 		STAILQ_REMOVE_HEAD(&twr_list, link);
1033 
1034 		cpl = wrtod(wr);
1035 		opcode = GET_OPCODE(cpl);
1036 
1037 		switch (opcode) {
1038 		case CPL_ACT_OPEN_REQ:
1039 		case CPL_ACT_OPEN_REQ6:
1040 			atid = G_TID_TID(be32toh(OPCODE_TID(cpl)));
1041 			CTR2(KTR_CXGBE, "%s: atid %u ", __func__, atid);
1042 			act_open_failure_cleanup(sc, atid, EHOSTUNREACH);
1043 			free(wr, M_CXGBE);
1044 			break;
1045 		case CPL_PASS_ACCEPT_RPL:
1046 			tid = GET_TID(cpl);
1047 			CTR2(KTR_CXGBE, "%s: tid %u ", __func__, tid);
1048 			synack_failure_cleanup(sc, tid);
1049 			free(wr, M_CXGBE);
1050 			break;
1051 		default:
1052 			log(LOG_ERR, "%s: leaked work request %p, wr_len %d, "
1053 			    "opcode %x\n", __func__, wr, wr->wr_len, opcode);
1054 			/* WR not freed here; go look at it with a debugger.  */
1055 		}
1056 	}
1057 }
1058 
1059 /*
1060  * Ground control to Major TOM
1061  * Commencing countdown, engines on
1062  */
1063 static int
1064 t4_tom_activate(struct adapter *sc)
1065 {
1066 	struct tom_data *td;
1067 	struct toedev *tod;
1068 	struct vi_info *vi;
1069 	int i, rc, v;
1070 
1071 	ASSERT_SYNCHRONIZED_OP(sc);
1072 
1073 	/* per-adapter softc for TOM */
1074 	td = malloc(sizeof(*td), M_CXGBE, M_ZERO | M_NOWAIT);
1075 	if (td == NULL)
1076 		return (ENOMEM);
1077 
1078 	/* List of TOE PCBs and associated lock */
1079 	mtx_init(&td->toep_list_lock, "PCB list lock", NULL, MTX_DEF);
1080 	TAILQ_INIT(&td->toep_list);
1081 
1082 	/* Listen context */
1083 	mtx_init(&td->lctx_hash_lock, "lctx hash lock", NULL, MTX_DEF);
1084 	td->listen_hash = hashinit_flags(LISTEN_HASH_SIZE, M_CXGBE,
1085 	    &td->listen_mask, HASH_NOWAIT);
1086 
1087 	/* List of WRs for which L2 resolution failed */
1088 	mtx_init(&td->unsent_wr_lock, "Unsent WR list lock", NULL, MTX_DEF);
1089 	STAILQ_INIT(&td->unsent_wr_list);
1090 	TASK_INIT(&td->reclaim_wr_resources, 0, reclaim_wr_resources, td);
1091 
1092 	/* TID tables */
1093 	rc = alloc_tid_tabs(&sc->tids);
1094 	if (rc != 0)
1095 		goto done;
1096 
1097 	rc = t4_init_ppod_region(&td->pr, &sc->vres.ddp,
1098 	    t4_read_reg(sc, A_ULP_RX_TDDP_PSZ), "TDDP page pods");
1099 	if (rc != 0)
1100 		goto done;
1101 	t4_set_reg_field(sc, A_ULP_RX_TDDP_TAGMASK,
1102 	    V_TDDPTAGMASK(M_TDDPTAGMASK), td->pr.pr_tag_mask);
1103 
1104 	/* toedev ops */
1105 	tod = &td->tod;
1106 	init_toedev(tod);
1107 	tod->tod_softc = sc;
1108 	tod->tod_connect = t4_connect;
1109 	tod->tod_listen_start = t4_listen_start;
1110 	tod->tod_listen_stop = t4_listen_stop;
1111 	tod->tod_rcvd = t4_rcvd;
1112 	tod->tod_output = t4_tod_output;
1113 	tod->tod_send_rst = t4_send_rst;
1114 	tod->tod_send_fin = t4_send_fin;
1115 	tod->tod_pcb_detach = t4_pcb_detach;
1116 	tod->tod_l2_update = t4_l2_update;
1117 	tod->tod_syncache_added = t4_syncache_added;
1118 	tod->tod_syncache_removed = t4_syncache_removed;
1119 	tod->tod_syncache_respond = t4_syncache_respond;
1120 	tod->tod_offload_socket = t4_offload_socket;
1121 	tod->tod_ctloutput = t4_ctloutput;
1122 	tod->tod_tcp_info = t4_tcp_info;
1123 
1124 	for_each_port(sc, i) {
1125 		for_each_vi(sc->port[i], v, vi) {
1126 			TOEDEV(vi->ifp) = &td->tod;
1127 		}
1128 	}
1129 
1130 	sc->tom_softc = td;
1131 	register_toedev(sc->tom_softc);
1132 
1133 done:
1134 	if (rc != 0)
1135 		free_tom_data(sc, td);
1136 	return (rc);
1137 }
1138 
1139 static int
1140 t4_tom_deactivate(struct adapter *sc)
1141 {
1142 	int rc = 0;
1143 	struct tom_data *td = sc->tom_softc;
1144 
1145 	ASSERT_SYNCHRONIZED_OP(sc);
1146 
1147 	if (td == NULL)
1148 		return (0);	/* XXX. KASSERT? */
1149 
1150 	if (sc->offload_map != 0)
1151 		return (EBUSY);	/* at least one port has IFCAP_TOE enabled */
1152 
1153 	if (uld_active(sc, ULD_IWARP) || uld_active(sc, ULD_ISCSI))
1154 		return (EBUSY);	/* both iWARP and iSCSI rely on the TOE. */
1155 
1156 	mtx_lock(&td->toep_list_lock);
1157 	if (!TAILQ_EMPTY(&td->toep_list))
1158 		rc = EBUSY;
1159 	mtx_unlock(&td->toep_list_lock);
1160 
1161 	mtx_lock(&td->lctx_hash_lock);
1162 	if (td->lctx_count > 0)
1163 		rc = EBUSY;
1164 	mtx_unlock(&td->lctx_hash_lock);
1165 
1166 	taskqueue_drain(taskqueue_thread, &td->reclaim_wr_resources);
1167 	mtx_lock(&td->unsent_wr_lock);
1168 	if (!STAILQ_EMPTY(&td->unsent_wr_list))
1169 		rc = EBUSY;
1170 	mtx_unlock(&td->unsent_wr_lock);
1171 
1172 	if (rc == 0) {
1173 		unregister_toedev(sc->tom_softc);
1174 		free_tom_data(sc, td);
1175 		sc->tom_softc = NULL;
1176 	}
1177 
1178 	return (rc);
1179 }
1180 
1181 static int
1182 t4_aio_queue_tom(struct socket *so, struct kaiocb *job)
1183 {
1184 	struct tcpcb *tp = so_sototcpcb(so);
1185 	struct toepcb *toep = tp->t_toe;
1186 	int error;
1187 
1188 	if (toep->ulp_mode == ULP_MODE_TCPDDP) {
1189 		error = t4_aio_queue_ddp(so, job);
1190 		if (error != EOPNOTSUPP)
1191 			return (error);
1192 	}
1193 
1194 	return (t4_aio_queue_aiotx(so, job));
1195 }
1196 
1197 static int
1198 t4_ctloutput_tom(struct socket *so, struct sockopt *sopt)
1199 {
1200 
1201 	if (sopt->sopt_level != IPPROTO_TCP)
1202 		return (tcp_ctloutput(so, sopt));
1203 
1204 	switch (sopt->sopt_name) {
1205 	case TCP_TLSOM_SET_TLS_CONTEXT:
1206 	case TCP_TLSOM_GET_TLS_TOM:
1207 	case TCP_TLSOM_CLR_TLS_TOM:
1208 	case TCP_TLSOM_CLR_QUIES:
1209 		return (t4_ctloutput_tls(so, sopt));
1210 	default:
1211 		return (tcp_ctloutput(so, sopt));
1212 	}
1213 }
1214 
1215 static int
1216 t4_tom_mod_load(void)
1217 {
1218 	struct protosw *tcp_protosw, *tcp6_protosw;
1219 
1220 	/* CPL handlers */
1221 	t4_register_shared_cpl_handler(CPL_L2T_WRITE_RPL, do_l2t_write_rpl2,
1222 	    CPL_COOKIE_TOM);
1223 	t4_init_connect_cpl_handlers();
1224 	t4_init_listen_cpl_handlers();
1225 	t4_init_cpl_io_handlers();
1226 
1227 	t4_ddp_mod_load();
1228 	t4_tls_mod_load();
1229 
1230 	tcp_protosw = pffindproto(PF_INET, IPPROTO_TCP, SOCK_STREAM);
1231 	if (tcp_protosw == NULL)
1232 		return (ENOPROTOOPT);
1233 	bcopy(tcp_protosw, &toe_protosw, sizeof(toe_protosw));
1234 	bcopy(tcp_protosw->pr_usrreqs, &toe_usrreqs, sizeof(toe_usrreqs));
1235 	toe_usrreqs.pru_aio_queue = t4_aio_queue_tom;
1236 	toe_protosw.pr_ctloutput = t4_ctloutput_tom;
1237 	toe_protosw.pr_usrreqs = &toe_usrreqs;
1238 
1239 	tcp6_protosw = pffindproto(PF_INET6, IPPROTO_TCP, SOCK_STREAM);
1240 	if (tcp6_protosw == NULL)
1241 		return (ENOPROTOOPT);
1242 	bcopy(tcp6_protosw, &toe6_protosw, sizeof(toe6_protosw));
1243 	bcopy(tcp6_protosw->pr_usrreqs, &toe6_usrreqs, sizeof(toe6_usrreqs));
1244 	toe6_usrreqs.pru_aio_queue = t4_aio_queue_tom;
1245 	toe6_protosw.pr_ctloutput = t4_ctloutput_tom;
1246 	toe6_protosw.pr_usrreqs = &toe6_usrreqs;
1247 
1248 	return (t4_register_uld(&tom_uld_info));
1249 }
1250 
1251 static void
1252 tom_uninit(struct adapter *sc, void *arg __unused)
1253 {
1254 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4tomun"))
1255 		return;
1256 
1257 	/* Try to free resources (works only if no port has IFCAP_TOE) */
1258 	if (uld_active(sc, ULD_TOM))
1259 		t4_deactivate_uld(sc, ULD_TOM);
1260 
1261 	end_synchronized_op(sc, 0);
1262 }
1263 
1264 static int
1265 t4_tom_mod_unload(void)
1266 {
1267 	t4_iterate(tom_uninit, NULL);
1268 
1269 	if (t4_unregister_uld(&tom_uld_info) == EBUSY)
1270 		return (EBUSY);
1271 
1272 	t4_tls_mod_unload();
1273 	t4_ddp_mod_unload();
1274 
1275 	t4_uninit_connect_cpl_handlers();
1276 	t4_uninit_listen_cpl_handlers();
1277 	t4_uninit_cpl_io_handlers();
1278 	t4_register_shared_cpl_handler(CPL_L2T_WRITE_RPL, NULL, CPL_COOKIE_TOM);
1279 
1280 	return (0);
1281 }
1282 #endif	/* TCP_OFFLOAD */
1283 
1284 static int
1285 t4_tom_modevent(module_t mod, int cmd, void *arg)
1286 {
1287 	int rc = 0;
1288 
1289 #ifdef TCP_OFFLOAD
1290 	switch (cmd) {
1291 	case MOD_LOAD:
1292 		rc = t4_tom_mod_load();
1293 		break;
1294 
1295 	case MOD_UNLOAD:
1296 		rc = t4_tom_mod_unload();
1297 		break;
1298 
1299 	default:
1300 		rc = EINVAL;
1301 	}
1302 #else
1303 	printf("t4_tom: compiled without TCP_OFFLOAD support.\n");
1304 	rc = EOPNOTSUPP;
1305 #endif
1306 	return (rc);
1307 }
1308 
1309 static moduledata_t t4_tom_moddata= {
1310 	"t4_tom",
1311 	t4_tom_modevent,
1312 	0
1313 };
1314 
1315 MODULE_VERSION(t4_tom, 1);
1316 MODULE_DEPEND(t4_tom, toecore, 1, 1, 1);
1317 MODULE_DEPEND(t4_tom, t4nex, 1, 1, 1);
1318 DECLARE_MODULE(t4_tom, t4_tom_moddata, SI_SUB_EXEC, SI_ORDER_ANY);
1319