xref: /freebsd/sys/dev/cxgbe/tom/t4_tom.c (revision 40427cca7a9ae77b095936fb1954417c290cfb17)
1 /*-
2  * Copyright (c) 2012 Chelsio Communications, Inc.
3  * All rights reserved.
4  * Written by: Navdeep Parhar <np@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include "opt_inet.h"
32 #include "opt_inet6.h"
33 #include "opt_ratelimit.h"
34 
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/ktr.h>
40 #include <sys/lock.h>
41 #include <sys/limits.h>
42 #include <sys/module.h>
43 #include <sys/protosw.h>
44 #include <sys/domain.h>
45 #include <sys/refcount.h>
46 #include <sys/rmlock.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/taskqueue.h>
50 #include <net/if.h>
51 #include <net/if_var.h>
52 #include <netinet/in.h>
53 #include <netinet/in_pcb.h>
54 #include <netinet/in_var.h>
55 #include <netinet/ip.h>
56 #include <netinet/ip6.h>
57 #include <netinet6/scope6_var.h>
58 #define TCPSTATES
59 #include <netinet/tcp_fsm.h>
60 #include <netinet/tcp_var.h>
61 #include <netinet/toecore.h>
62 
63 #ifdef TCP_OFFLOAD
64 #include "common/common.h"
65 #include "common/t4_msg.h"
66 #include "common/t4_regs.h"
67 #include "common/t4_regs_values.h"
68 #include "common/t4_tcb.h"
69 #include "tom/t4_tom_l2t.h"
70 #include "tom/t4_tom.h"
71 
72 static struct protosw toe_protosw;
73 static struct pr_usrreqs toe_usrreqs;
74 
75 static struct protosw toe6_protosw;
76 static struct pr_usrreqs toe6_usrreqs;
77 
78 /* Module ops */
79 static int t4_tom_mod_load(void);
80 static int t4_tom_mod_unload(void);
81 static int t4_tom_modevent(module_t, int, void *);
82 
83 /* ULD ops and helpers */
84 static int t4_tom_activate(struct adapter *);
85 static int t4_tom_deactivate(struct adapter *);
86 
87 static struct uld_info tom_uld_info = {
88 	.uld_id = ULD_TOM,
89 	.activate = t4_tom_activate,
90 	.deactivate = t4_tom_deactivate,
91 };
92 
93 static void queue_tid_release(struct adapter *, int);
94 static void release_offload_resources(struct toepcb *);
95 static int alloc_tid_tabs(struct tid_info *);
96 static void free_tid_tabs(struct tid_info *);
97 static int add_lip(struct adapter *, struct in6_addr *);
98 static int delete_lip(struct adapter *, struct in6_addr *);
99 static struct clip_entry *search_lip(struct tom_data *, struct in6_addr *);
100 static void init_clip_table(struct adapter *, struct tom_data *);
101 static void update_clip(struct adapter *, void *);
102 static void t4_clip_task(void *, int);
103 static void update_clip_table(struct adapter *, struct tom_data *);
104 static void destroy_clip_table(struct adapter *, struct tom_data *);
105 static void free_tom_data(struct adapter *, struct tom_data *);
106 static void reclaim_wr_resources(void *, int);
107 
108 static int in6_ifaddr_gen;
109 static eventhandler_tag ifaddr_evhandler;
110 static struct timeout_task clip_task;
111 
112 struct toepcb *
113 alloc_toepcb(struct vi_info *vi, int txqid, int rxqid, int flags)
114 {
115 	struct port_info *pi = vi->pi;
116 	struct adapter *sc = pi->adapter;
117 	struct toepcb *toep;
118 	int tx_credits, txsd_total, len;
119 
120 	/*
121 	 * The firmware counts tx work request credits in units of 16 bytes
122 	 * each.  Reserve room for an ABORT_REQ so the driver never has to worry
123 	 * about tx credits if it wants to abort a connection.
124 	 */
125 	tx_credits = sc->params.ofldq_wr_cred;
126 	tx_credits -= howmany(sizeof(struct cpl_abort_req), 16);
127 
128 	/*
129 	 * Shortest possible tx work request is a fw_ofld_tx_data_wr + 1 byte
130 	 * immediate payload, and firmware counts tx work request credits in
131 	 * units of 16 byte.  Calculate the maximum work requests possible.
132 	 */
133 	txsd_total = tx_credits /
134 	    howmany(sizeof(struct fw_ofld_tx_data_wr) + 1, 16);
135 
136 	if (txqid < 0)
137 		txqid = (arc4random() % vi->nofldtxq) + vi->first_ofld_txq;
138 	KASSERT(txqid >= vi->first_ofld_txq &&
139 	    txqid < vi->first_ofld_txq + vi->nofldtxq,
140 	    ("%s: txqid %d for vi %p (first %d, n %d)", __func__, txqid, vi,
141 		vi->first_ofld_txq, vi->nofldtxq));
142 
143 	if (rxqid < 0)
144 		rxqid = (arc4random() % vi->nofldrxq) + vi->first_ofld_rxq;
145 	KASSERT(rxqid >= vi->first_ofld_rxq &&
146 	    rxqid < vi->first_ofld_rxq + vi->nofldrxq,
147 	    ("%s: rxqid %d for vi %p (first %d, n %d)", __func__, rxqid, vi,
148 		vi->first_ofld_rxq, vi->nofldrxq));
149 
150 	len = offsetof(struct toepcb, txsd) +
151 	    txsd_total * sizeof(struct ofld_tx_sdesc);
152 
153 	toep = malloc(len, M_CXGBE, M_ZERO | flags);
154 	if (toep == NULL)
155 		return (NULL);
156 
157 	refcount_init(&toep->refcount, 1);
158 	toep->td = sc->tom_softc;
159 	toep->vi = vi;
160 	toep->tc_idx = -1;
161 	toep->tx_total = tx_credits;
162 	toep->tx_credits = tx_credits;
163 	toep->ofld_txq = &sc->sge.ofld_txq[txqid];
164 	toep->ofld_rxq = &sc->sge.ofld_rxq[rxqid];
165 	toep->ctrlq = &sc->sge.ctrlq[pi->port_id];
166 	mbufq_init(&toep->ulp_pduq, INT_MAX);
167 	mbufq_init(&toep->ulp_pdu_reclaimq, INT_MAX);
168 	toep->txsd_total = txsd_total;
169 	toep->txsd_avail = txsd_total;
170 	toep->txsd_pidx = 0;
171 	toep->txsd_cidx = 0;
172 	aiotx_init_toep(toep);
173 	ddp_init_toep(toep);
174 
175 	return (toep);
176 }
177 
178 struct toepcb *
179 hold_toepcb(struct toepcb *toep)
180 {
181 
182 	refcount_acquire(&toep->refcount);
183 	return (toep);
184 }
185 
186 void
187 free_toepcb(struct toepcb *toep)
188 {
189 
190 	if (refcount_release(&toep->refcount) == 0)
191 		return;
192 
193 	KASSERT(!(toep->flags & TPF_ATTACHED),
194 	    ("%s: attached to an inpcb", __func__));
195 	KASSERT(!(toep->flags & TPF_CPL_PENDING),
196 	    ("%s: CPL pending", __func__));
197 
198 	ddp_uninit_toep(toep);
199 	free(toep, M_CXGBE);
200 }
201 
202 /*
203  * Set up the socket for TCP offload.
204  */
205 void
206 offload_socket(struct socket *so, struct toepcb *toep)
207 {
208 	struct tom_data *td = toep->td;
209 	struct inpcb *inp = sotoinpcb(so);
210 	struct tcpcb *tp = intotcpcb(inp);
211 	struct sockbuf *sb;
212 
213 	INP_WLOCK_ASSERT(inp);
214 
215 	/* Update socket */
216 	sb = &so->so_snd;
217 	SOCKBUF_LOCK(sb);
218 	sb->sb_flags |= SB_NOCOALESCE;
219 	SOCKBUF_UNLOCK(sb);
220 	sb = &so->so_rcv;
221 	SOCKBUF_LOCK(sb);
222 	sb->sb_flags |= SB_NOCOALESCE;
223 	if (inp->inp_vflag & INP_IPV6)
224 		so->so_proto = &toe6_protosw;
225 	else
226 		so->so_proto = &toe_protosw;
227 	SOCKBUF_UNLOCK(sb);
228 
229 	/* Update TCP PCB */
230 	tp->tod = &td->tod;
231 	tp->t_toe = toep;
232 	tp->t_flags |= TF_TOE;
233 
234 	/* Install an extra hold on inp */
235 	toep->inp = inp;
236 	toep->flags |= TPF_ATTACHED;
237 	in_pcbref(inp);
238 
239 	/* Add the TOE PCB to the active list */
240 	mtx_lock(&td->toep_list_lock);
241 	TAILQ_INSERT_HEAD(&td->toep_list, toep, link);
242 	mtx_unlock(&td->toep_list_lock);
243 }
244 
245 /* This is _not_ the normal way to "unoffload" a socket. */
246 void
247 undo_offload_socket(struct socket *so)
248 {
249 	struct inpcb *inp = sotoinpcb(so);
250 	struct tcpcb *tp = intotcpcb(inp);
251 	struct toepcb *toep = tp->t_toe;
252 	struct tom_data *td = toep->td;
253 	struct sockbuf *sb;
254 
255 	INP_WLOCK_ASSERT(inp);
256 
257 	sb = &so->so_snd;
258 	SOCKBUF_LOCK(sb);
259 	sb->sb_flags &= ~SB_NOCOALESCE;
260 	SOCKBUF_UNLOCK(sb);
261 	sb = &so->so_rcv;
262 	SOCKBUF_LOCK(sb);
263 	sb->sb_flags &= ~SB_NOCOALESCE;
264 	SOCKBUF_UNLOCK(sb);
265 
266 	tp->tod = NULL;
267 	tp->t_toe = NULL;
268 	tp->t_flags &= ~TF_TOE;
269 
270 	toep->inp = NULL;
271 	toep->flags &= ~TPF_ATTACHED;
272 	if (in_pcbrele_wlocked(inp))
273 		panic("%s: inp freed.", __func__);
274 
275 	mtx_lock(&td->toep_list_lock);
276 	TAILQ_REMOVE(&td->toep_list, toep, link);
277 	mtx_unlock(&td->toep_list_lock);
278 }
279 
280 static void
281 release_offload_resources(struct toepcb *toep)
282 {
283 	struct tom_data *td = toep->td;
284 	struct adapter *sc = td_adapter(td);
285 	int tid = toep->tid;
286 
287 	KASSERT(!(toep->flags & TPF_CPL_PENDING),
288 	    ("%s: %p has CPL pending.", __func__, toep));
289 	KASSERT(!(toep->flags & TPF_ATTACHED),
290 	    ("%s: %p is still attached.", __func__, toep));
291 
292 	CTR5(KTR_CXGBE, "%s: toep %p (tid %d, l2te %p, ce %p)",
293 	    __func__, toep, tid, toep->l2te, toep->ce);
294 
295 	/*
296 	 * These queues should have been emptied at approximately the same time
297 	 * that a normal connection's socket's so_snd would have been purged or
298 	 * drained.  Do _not_ clean up here.
299 	 */
300 	MPASS(mbufq_len(&toep->ulp_pduq) == 0);
301 	MPASS(mbufq_len(&toep->ulp_pdu_reclaimq) == 0);
302 #ifdef INVARIANTS
303 	ddp_assert_empty(toep);
304 #endif
305 
306 	if (toep->l2te)
307 		t4_l2t_release(toep->l2te);
308 
309 	if (tid >= 0) {
310 		remove_tid(sc, tid, toep->ce ? 2 : 1);
311 		release_tid(sc, tid, toep->ctrlq);
312 	}
313 
314 	if (toep->ce)
315 		release_lip(td, toep->ce);
316 
317 #ifdef RATELIMIT
318 	if (toep->tc_idx != -1)
319 		t4_release_cl_rl_kbps(sc, toep->vi->pi->port_id, toep->tc_idx);
320 #endif
321 	mtx_lock(&td->toep_list_lock);
322 	TAILQ_REMOVE(&td->toep_list, toep, link);
323 	mtx_unlock(&td->toep_list_lock);
324 
325 	free_toepcb(toep);
326 }
327 
328 /*
329  * The kernel is done with the TCP PCB and this is our opportunity to unhook the
330  * toepcb hanging off of it.  If the TOE driver is also done with the toepcb (no
331  * pending CPL) then it is time to release all resources tied to the toepcb.
332  *
333  * Also gets called when an offloaded active open fails and the TOM wants the
334  * kernel to take the TCP PCB back.
335  */
336 static void
337 t4_pcb_detach(struct toedev *tod __unused, struct tcpcb *tp)
338 {
339 #if defined(KTR) || defined(INVARIANTS)
340 	struct inpcb *inp = tp->t_inpcb;
341 #endif
342 	struct toepcb *toep = tp->t_toe;
343 
344 	INP_WLOCK_ASSERT(inp);
345 
346 	KASSERT(toep != NULL, ("%s: toep is NULL", __func__));
347 	KASSERT(toep->flags & TPF_ATTACHED,
348 	    ("%s: not attached", __func__));
349 
350 #ifdef KTR
351 	if (tp->t_state == TCPS_SYN_SENT) {
352 		CTR6(KTR_CXGBE, "%s: atid %d, toep %p (0x%x), inp %p (0x%x)",
353 		    __func__, toep->tid, toep, toep->flags, inp,
354 		    inp->inp_flags);
355 	} else {
356 		CTR6(KTR_CXGBE,
357 		    "t4_pcb_detach: tid %d (%s), toep %p (0x%x), inp %p (0x%x)",
358 		    toep->tid, tcpstates[tp->t_state], toep, toep->flags, inp,
359 		    inp->inp_flags);
360 	}
361 #endif
362 
363 	tp->t_toe = NULL;
364 	tp->t_flags &= ~TF_TOE;
365 	toep->flags &= ~TPF_ATTACHED;
366 
367 	if (!(toep->flags & TPF_CPL_PENDING))
368 		release_offload_resources(toep);
369 }
370 
371 /*
372  * setsockopt handler.
373  */
374 static void
375 t4_ctloutput(struct toedev *tod, struct tcpcb *tp, int dir, int name)
376 {
377 	struct adapter *sc = tod->tod_softc;
378 	struct toepcb *toep = tp->t_toe;
379 
380 	if (dir == SOPT_GET)
381 		return;
382 
383 	CTR4(KTR_CXGBE, "%s: tp %p, dir %u, name %u", __func__, tp, dir, name);
384 
385 	switch (name) {
386 	case TCP_NODELAY:
387 		if (tp->t_state != TCPS_ESTABLISHED)
388 			break;
389 		t4_set_tcb_field(sc, toep->ctrlq, toep->tid, W_TCB_T_FLAGS,
390 		    V_TF_NAGLE(1), V_TF_NAGLE(tp->t_flags & TF_NODELAY ? 0 : 1),
391 		    0, 0, toep->ofld_rxq->iq.abs_id);
392 		break;
393 	default:
394 		break;
395 	}
396 }
397 
398 /*
399  * The TOE driver will not receive any more CPLs for the tid associated with the
400  * toepcb; release the hold on the inpcb.
401  */
402 void
403 final_cpl_received(struct toepcb *toep)
404 {
405 	struct inpcb *inp = toep->inp;
406 
407 	KASSERT(inp != NULL, ("%s: inp is NULL", __func__));
408 	INP_WLOCK_ASSERT(inp);
409 	KASSERT(toep->flags & TPF_CPL_PENDING,
410 	    ("%s: CPL not pending already?", __func__));
411 
412 	CTR6(KTR_CXGBE, "%s: tid %d, toep %p (0x%x), inp %p (0x%x)",
413 	    __func__, toep->tid, toep, toep->flags, inp, inp->inp_flags);
414 
415 	if (toep->ulp_mode == ULP_MODE_TCPDDP)
416 		release_ddp_resources(toep);
417 	toep->inp = NULL;
418 	toep->flags &= ~TPF_CPL_PENDING;
419 	mbufq_drain(&toep->ulp_pdu_reclaimq);
420 
421 	if (!(toep->flags & TPF_ATTACHED))
422 		release_offload_resources(toep);
423 
424 	if (!in_pcbrele_wlocked(inp))
425 		INP_WUNLOCK(inp);
426 }
427 
428 void
429 insert_tid(struct adapter *sc, int tid, void *ctx, int ntids)
430 {
431 	struct tid_info *t = &sc->tids;
432 
433 	t->tid_tab[tid] = ctx;
434 	atomic_add_int(&t->tids_in_use, ntids);
435 }
436 
437 void *
438 lookup_tid(struct adapter *sc, int tid)
439 {
440 	struct tid_info *t = &sc->tids;
441 
442 	return (t->tid_tab[tid]);
443 }
444 
445 void
446 update_tid(struct adapter *sc, int tid, void *ctx)
447 {
448 	struct tid_info *t = &sc->tids;
449 
450 	t->tid_tab[tid] = ctx;
451 }
452 
453 void
454 remove_tid(struct adapter *sc, int tid, int ntids)
455 {
456 	struct tid_info *t = &sc->tids;
457 
458 	t->tid_tab[tid] = NULL;
459 	atomic_subtract_int(&t->tids_in_use, ntids);
460 }
461 
462 void
463 release_tid(struct adapter *sc, int tid, struct sge_wrq *ctrlq)
464 {
465 	struct wrqe *wr;
466 	struct cpl_tid_release *req;
467 
468 	wr = alloc_wrqe(sizeof(*req), ctrlq);
469 	if (wr == NULL) {
470 		queue_tid_release(sc, tid);	/* defer */
471 		return;
472 	}
473 	req = wrtod(wr);
474 
475 	INIT_TP_WR_MIT_CPL(req, CPL_TID_RELEASE, tid);
476 
477 	t4_wrq_tx(sc, wr);
478 }
479 
480 static void
481 queue_tid_release(struct adapter *sc, int tid)
482 {
483 
484 	CXGBE_UNIMPLEMENTED("deferred tid release");
485 }
486 
487 /*
488  * What mtu_idx to use, given a 4-tuple and/or an MSS cap
489  */
490 int
491 find_best_mtu_idx(struct adapter *sc, struct in_conninfo *inc, int pmss)
492 {
493 	unsigned short *mtus = &sc->params.mtus[0];
494 	int i, mss, n;
495 
496 	KASSERT(inc != NULL || pmss > 0,
497 	    ("%s: at least one of inc/pmss must be specified", __func__));
498 
499 	mss = inc ? tcp_mssopt(inc) : pmss;
500 	if (pmss > 0 && mss > pmss)
501 		mss = pmss;
502 
503 	if (inc->inc_flags & INC_ISIPV6)
504 		n = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
505 	else
506 		n = sizeof(struct ip) + sizeof(struct tcphdr);
507 
508 	for (i = 0; i < NMTUS - 1 && mtus[i + 1] <= mss + n; i++)
509 		continue;
510 
511 	return (i);
512 }
513 
514 /*
515  * Determine the receive window size for a socket.
516  */
517 u_long
518 select_rcv_wnd(struct socket *so)
519 {
520 	unsigned long wnd;
521 
522 	SOCKBUF_LOCK_ASSERT(&so->so_rcv);
523 
524 	wnd = sbspace(&so->so_rcv);
525 	if (wnd < MIN_RCV_WND)
526 		wnd = MIN_RCV_WND;
527 
528 	return min(wnd, MAX_RCV_WND);
529 }
530 
531 int
532 select_rcv_wscale(void)
533 {
534 	int wscale = 0;
535 	unsigned long space = sb_max;
536 
537 	if (space > MAX_RCV_WND)
538 		space = MAX_RCV_WND;
539 
540 	while (wscale < TCP_MAX_WINSHIFT && (TCP_MAXWIN << wscale) < space)
541 		wscale++;
542 
543 	return (wscale);
544 }
545 
546 extern int always_keepalive;
547 
548 /*
549  * socket so could be a listening socket too.
550  */
551 uint64_t
552 calc_opt0(struct socket *so, struct vi_info *vi, struct l2t_entry *e,
553     int mtu_idx, int rscale, int rx_credits, int ulp_mode)
554 {
555 	uint64_t opt0;
556 
557 	KASSERT(rx_credits <= M_RCV_BUFSIZ,
558 	    ("%s: rcv_bufsiz too high", __func__));
559 
560 	opt0 = F_TCAM_BYPASS | V_WND_SCALE(rscale) | V_MSS_IDX(mtu_idx) |
561 	    V_ULP_MODE(ulp_mode) | V_RCV_BUFSIZ(rx_credits);
562 
563 	if (so != NULL) {
564 		struct inpcb *inp = sotoinpcb(so);
565 		struct tcpcb *tp = intotcpcb(inp);
566 		int keepalive = always_keepalive ||
567 		    so_options_get(so) & SO_KEEPALIVE;
568 
569 		opt0 |= V_NAGLE((tp->t_flags & TF_NODELAY) == 0);
570 		opt0 |= V_KEEP_ALIVE(keepalive != 0);
571 	}
572 
573 	if (e != NULL)
574 		opt0 |= V_L2T_IDX(e->idx);
575 
576 	if (vi != NULL) {
577 		opt0 |= V_SMAC_SEL(vi->smt_idx);
578 		opt0 |= V_TX_CHAN(vi->pi->tx_chan);
579 	}
580 
581 	return htobe64(opt0);
582 }
583 
584 uint64_t
585 select_ntuple(struct vi_info *vi, struct l2t_entry *e)
586 {
587 	struct adapter *sc = vi->pi->adapter;
588 	struct tp_params *tp = &sc->params.tp;
589 	uint16_t viid = vi->viid;
590 	uint64_t ntuple = 0;
591 
592 	/*
593 	 * Initialize each of the fields which we care about which are present
594 	 * in the Compressed Filter Tuple.
595 	 */
596 	if (tp->vlan_shift >= 0 && e->vlan != CPL_L2T_VLAN_NONE)
597 		ntuple |= (uint64_t)(F_FT_VLAN_VLD | e->vlan) << tp->vlan_shift;
598 
599 	if (tp->port_shift >= 0)
600 		ntuple |= (uint64_t)e->lport << tp->port_shift;
601 
602 	if (tp->protocol_shift >= 0)
603 		ntuple |= (uint64_t)IPPROTO_TCP << tp->protocol_shift;
604 
605 	if (tp->vnic_shift >= 0) {
606 		uint32_t vf = G_FW_VIID_VIN(viid);
607 		uint32_t pf = G_FW_VIID_PFN(viid);
608 		uint32_t vld = G_FW_VIID_VIVLD(viid);
609 
610 		ntuple |= (uint64_t)(V_FT_VNID_ID_VF(vf) | V_FT_VNID_ID_PF(pf) |
611 		    V_FT_VNID_ID_VLD(vld)) << tp->vnic_shift;
612 	}
613 
614 	if (is_t4(sc))
615 		return (htobe32((uint32_t)ntuple));
616 	else
617 		return (htobe64(V_FILTER_TUPLE(ntuple)));
618 }
619 
620 void
621 set_tcpddp_ulp_mode(struct toepcb *toep)
622 {
623 
624 	toep->ulp_mode = ULP_MODE_TCPDDP;
625 	toep->ddp_flags = DDP_OK;
626 }
627 
628 int
629 negative_advice(int status)
630 {
631 
632 	return (status == CPL_ERR_RTX_NEG_ADVICE ||
633 	    status == CPL_ERR_PERSIST_NEG_ADVICE ||
634 	    status == CPL_ERR_KEEPALV_NEG_ADVICE);
635 }
636 
637 static int
638 alloc_tid_tabs(struct tid_info *t)
639 {
640 	size_t size;
641 	unsigned int i;
642 
643 	size = t->ntids * sizeof(*t->tid_tab) +
644 	    t->natids * sizeof(*t->atid_tab) +
645 	    t->nstids * sizeof(*t->stid_tab);
646 
647 	t->tid_tab = malloc(size, M_CXGBE, M_ZERO | M_NOWAIT);
648 	if (t->tid_tab == NULL)
649 		return (ENOMEM);
650 
651 	mtx_init(&t->atid_lock, "atid lock", NULL, MTX_DEF);
652 	t->atid_tab = (union aopen_entry *)&t->tid_tab[t->ntids];
653 	t->afree = t->atid_tab;
654 	t->atids_in_use = 0;
655 	for (i = 1; i < t->natids; i++)
656 		t->atid_tab[i - 1].next = &t->atid_tab[i];
657 	t->atid_tab[t->natids - 1].next = NULL;
658 
659 	mtx_init(&t->stid_lock, "stid lock", NULL, MTX_DEF);
660 	t->stid_tab = (struct listen_ctx **)&t->atid_tab[t->natids];
661 	t->stids_in_use = 0;
662 	TAILQ_INIT(&t->stids);
663 	t->nstids_free_head = t->nstids;
664 
665 	atomic_store_rel_int(&t->tids_in_use, 0);
666 
667 	return (0);
668 }
669 
670 static void
671 free_tid_tabs(struct tid_info *t)
672 {
673 	KASSERT(t->tids_in_use == 0,
674 	    ("%s: %d tids still in use.", __func__, t->tids_in_use));
675 	KASSERT(t->atids_in_use == 0,
676 	    ("%s: %d atids still in use.", __func__, t->atids_in_use));
677 	KASSERT(t->stids_in_use == 0,
678 	    ("%s: %d tids still in use.", __func__, t->stids_in_use));
679 
680 	free(t->tid_tab, M_CXGBE);
681 	t->tid_tab = NULL;
682 
683 	if (mtx_initialized(&t->atid_lock))
684 		mtx_destroy(&t->atid_lock);
685 	if (mtx_initialized(&t->stid_lock))
686 		mtx_destroy(&t->stid_lock);
687 }
688 
689 static int
690 add_lip(struct adapter *sc, struct in6_addr *lip)
691 {
692         struct fw_clip_cmd c;
693 
694 	ASSERT_SYNCHRONIZED_OP(sc);
695 	/* mtx_assert(&td->clip_table_lock, MA_OWNED); */
696 
697         memset(&c, 0, sizeof(c));
698 	c.op_to_write = htonl(V_FW_CMD_OP(FW_CLIP_CMD) | F_FW_CMD_REQUEST |
699 	    F_FW_CMD_WRITE);
700         c.alloc_to_len16 = htonl(F_FW_CLIP_CMD_ALLOC | FW_LEN16(c));
701         c.ip_hi = *(uint64_t *)&lip->s6_addr[0];
702         c.ip_lo = *(uint64_t *)&lip->s6_addr[8];
703 
704 	return (-t4_wr_mbox_ns(sc, sc->mbox, &c, sizeof(c), &c));
705 }
706 
707 static int
708 delete_lip(struct adapter *sc, struct in6_addr *lip)
709 {
710 	struct fw_clip_cmd c;
711 
712 	ASSERT_SYNCHRONIZED_OP(sc);
713 	/* mtx_assert(&td->clip_table_lock, MA_OWNED); */
714 
715 	memset(&c, 0, sizeof(c));
716 	c.op_to_write = htonl(V_FW_CMD_OP(FW_CLIP_CMD) | F_FW_CMD_REQUEST |
717 	    F_FW_CMD_READ);
718         c.alloc_to_len16 = htonl(F_FW_CLIP_CMD_FREE | FW_LEN16(c));
719         c.ip_hi = *(uint64_t *)&lip->s6_addr[0];
720         c.ip_lo = *(uint64_t *)&lip->s6_addr[8];
721 
722 	return (-t4_wr_mbox_ns(sc, sc->mbox, &c, sizeof(c), &c));
723 }
724 
725 static struct clip_entry *
726 search_lip(struct tom_data *td, struct in6_addr *lip)
727 {
728 	struct clip_entry *ce;
729 
730 	mtx_assert(&td->clip_table_lock, MA_OWNED);
731 
732 	TAILQ_FOREACH(ce, &td->clip_table, link) {
733 		if (IN6_ARE_ADDR_EQUAL(&ce->lip, lip))
734 			return (ce);
735 	}
736 
737 	return (NULL);
738 }
739 
740 struct clip_entry *
741 hold_lip(struct tom_data *td, struct in6_addr *lip, struct clip_entry *ce)
742 {
743 
744 	mtx_lock(&td->clip_table_lock);
745 	if (ce == NULL)
746 		ce = search_lip(td, lip);
747 	if (ce != NULL)
748 		ce->refcount++;
749 	mtx_unlock(&td->clip_table_lock);
750 
751 	return (ce);
752 }
753 
754 void
755 release_lip(struct tom_data *td, struct clip_entry *ce)
756 {
757 
758 	mtx_lock(&td->clip_table_lock);
759 	KASSERT(search_lip(td, &ce->lip) == ce,
760 	    ("%s: CLIP entry %p p not in CLIP table.", __func__, ce));
761 	KASSERT(ce->refcount > 0,
762 	    ("%s: CLIP entry %p has refcount 0", __func__, ce));
763 	--ce->refcount;
764 	mtx_unlock(&td->clip_table_lock);
765 }
766 
767 static void
768 init_clip_table(struct adapter *sc, struct tom_data *td)
769 {
770 
771 	ASSERT_SYNCHRONIZED_OP(sc);
772 
773 	mtx_init(&td->clip_table_lock, "CLIP table lock", NULL, MTX_DEF);
774 	TAILQ_INIT(&td->clip_table);
775 	td->clip_gen = -1;
776 
777 	update_clip_table(sc, td);
778 }
779 
780 static void
781 update_clip(struct adapter *sc, void *arg __unused)
782 {
783 
784 	if (begin_synchronized_op(sc, NULL, HOLD_LOCK, "t4tomuc"))
785 		return;
786 
787 	if (uld_active(sc, ULD_TOM))
788 		update_clip_table(sc, sc->tom_softc);
789 
790 	end_synchronized_op(sc, LOCK_HELD);
791 }
792 
793 static void
794 t4_clip_task(void *arg, int count)
795 {
796 
797 	t4_iterate(update_clip, NULL);
798 }
799 
800 static void
801 update_clip_table(struct adapter *sc, struct tom_data *td)
802 {
803 	struct rm_priotracker in6_ifa_tracker;
804 	struct in6_ifaddr *ia;
805 	struct in6_addr *lip, tlip;
806 	struct clip_head stale;
807 	struct clip_entry *ce, *ce_temp;
808 	struct vi_info *vi;
809 	int rc, gen, i, j;
810 	uintptr_t last_vnet;
811 
812 	ASSERT_SYNCHRONIZED_OP(sc);
813 
814 	IN6_IFADDR_RLOCK(&in6_ifa_tracker);
815 	mtx_lock(&td->clip_table_lock);
816 
817 	gen = atomic_load_acq_int(&in6_ifaddr_gen);
818 	if (gen == td->clip_gen)
819 		goto done;
820 
821 	TAILQ_INIT(&stale);
822 	TAILQ_CONCAT(&stale, &td->clip_table, link);
823 
824 	/*
825 	 * last_vnet optimizes the common cases where all if_vnet = NULL (no
826 	 * VIMAGE) or all if_vnet = vnet0.
827 	 */
828 	last_vnet = (uintptr_t)(-1);
829 	for_each_port(sc, i)
830 	for_each_vi(sc->port[i], j, vi) {
831 		if (last_vnet == (uintptr_t)vi->ifp->if_vnet)
832 			continue;
833 
834 		/* XXX: races with if_vmove */
835 		CURVNET_SET(vi->ifp->if_vnet);
836 		TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
837 			lip = &ia->ia_addr.sin6_addr;
838 
839 			KASSERT(!IN6_IS_ADDR_MULTICAST(lip),
840 			    ("%s: mcast address in in6_ifaddr list", __func__));
841 
842 			if (IN6_IS_ADDR_LOOPBACK(lip))
843 				continue;
844 			if (IN6_IS_SCOPE_EMBED(lip)) {
845 				/* Remove the embedded scope */
846 				tlip = *lip;
847 				lip = &tlip;
848 				in6_clearscope(lip);
849 			}
850 			/*
851 			 * XXX: how to weed out the link local address for the
852 			 * loopback interface?  It's fe80::1 usually (always?).
853 			 */
854 
855 			/*
856 			 * If it's in the main list then we already know it's
857 			 * not stale.
858 			 */
859 			TAILQ_FOREACH(ce, &td->clip_table, link) {
860 				if (IN6_ARE_ADDR_EQUAL(&ce->lip, lip))
861 					goto next;
862 			}
863 
864 			/*
865 			 * If it's in the stale list we should move it to the
866 			 * main list.
867 			 */
868 			TAILQ_FOREACH(ce, &stale, link) {
869 				if (IN6_ARE_ADDR_EQUAL(&ce->lip, lip)) {
870 					TAILQ_REMOVE(&stale, ce, link);
871 					TAILQ_INSERT_TAIL(&td->clip_table, ce,
872 					    link);
873 					goto next;
874 				}
875 			}
876 
877 			/* A new IP6 address; add it to the CLIP table */
878 			ce = malloc(sizeof(*ce), M_CXGBE, M_NOWAIT);
879 			memcpy(&ce->lip, lip, sizeof(ce->lip));
880 			ce->refcount = 0;
881 			rc = add_lip(sc, lip);
882 			if (rc == 0)
883 				TAILQ_INSERT_TAIL(&td->clip_table, ce, link);
884 			else {
885 				char ip[INET6_ADDRSTRLEN];
886 
887 				inet_ntop(AF_INET6, &ce->lip, &ip[0],
888 				    sizeof(ip));
889 				log(LOG_ERR, "%s: could not add %s (%d)\n",
890 				    __func__, ip, rc);
891 				free(ce, M_CXGBE);
892 			}
893 next:
894 			continue;
895 		}
896 		CURVNET_RESTORE();
897 		last_vnet = (uintptr_t)vi->ifp->if_vnet;
898 	}
899 
900 	/*
901 	 * Remove stale addresses (those no longer in V_in6_ifaddrhead) that are
902 	 * no longer referenced by the driver.
903 	 */
904 	TAILQ_FOREACH_SAFE(ce, &stale, link, ce_temp) {
905 		if (ce->refcount == 0) {
906 			rc = delete_lip(sc, &ce->lip);
907 			if (rc == 0) {
908 				TAILQ_REMOVE(&stale, ce, link);
909 				free(ce, M_CXGBE);
910 			} else {
911 				char ip[INET6_ADDRSTRLEN];
912 
913 				inet_ntop(AF_INET6, &ce->lip, &ip[0],
914 				    sizeof(ip));
915 				log(LOG_ERR, "%s: could not delete %s (%d)\n",
916 				    __func__, ip, rc);
917 			}
918 		}
919 	}
920 	/* The ones that are still referenced need to stay in the CLIP table */
921 	TAILQ_CONCAT(&td->clip_table, &stale, link);
922 
923 	td->clip_gen = gen;
924 done:
925 	mtx_unlock(&td->clip_table_lock);
926 	IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
927 }
928 
929 static void
930 destroy_clip_table(struct adapter *sc, struct tom_data *td)
931 {
932 	struct clip_entry *ce, *ce_temp;
933 
934 	if (mtx_initialized(&td->clip_table_lock)) {
935 		mtx_lock(&td->clip_table_lock);
936 		TAILQ_FOREACH_SAFE(ce, &td->clip_table, link, ce_temp) {
937 			KASSERT(ce->refcount == 0,
938 			    ("%s: CLIP entry %p still in use (%d)", __func__,
939 			    ce, ce->refcount));
940 			TAILQ_REMOVE(&td->clip_table, ce, link);
941 			delete_lip(sc, &ce->lip);
942 			free(ce, M_CXGBE);
943 		}
944 		mtx_unlock(&td->clip_table_lock);
945 		mtx_destroy(&td->clip_table_lock);
946 	}
947 }
948 
949 static void
950 free_tom_data(struct adapter *sc, struct tom_data *td)
951 {
952 
953 	ASSERT_SYNCHRONIZED_OP(sc);
954 
955 	KASSERT(TAILQ_EMPTY(&td->toep_list),
956 	    ("%s: TOE PCB list is not empty.", __func__));
957 	KASSERT(td->lctx_count == 0,
958 	    ("%s: lctx hash table is not empty.", __func__));
959 
960 	t4_free_ppod_region(&td->pr);
961 	destroy_clip_table(sc, td);
962 
963 	if (td->listen_mask != 0)
964 		hashdestroy(td->listen_hash, M_CXGBE, td->listen_mask);
965 
966 	if (mtx_initialized(&td->unsent_wr_lock))
967 		mtx_destroy(&td->unsent_wr_lock);
968 	if (mtx_initialized(&td->lctx_hash_lock))
969 		mtx_destroy(&td->lctx_hash_lock);
970 	if (mtx_initialized(&td->toep_list_lock))
971 		mtx_destroy(&td->toep_list_lock);
972 
973 	free_tid_tabs(&sc->tids);
974 	free(td, M_CXGBE);
975 }
976 
977 static void
978 reclaim_wr_resources(void *arg, int count)
979 {
980 	struct tom_data *td = arg;
981 	STAILQ_HEAD(, wrqe) twr_list = STAILQ_HEAD_INITIALIZER(twr_list);
982 	struct cpl_act_open_req *cpl;
983 	u_int opcode, atid;
984 	struct wrqe *wr;
985 	struct adapter *sc;
986 
987 	mtx_lock(&td->unsent_wr_lock);
988 	STAILQ_SWAP(&td->unsent_wr_list, &twr_list, wrqe);
989 	mtx_unlock(&td->unsent_wr_lock);
990 
991 	while ((wr = STAILQ_FIRST(&twr_list)) != NULL) {
992 		STAILQ_REMOVE_HEAD(&twr_list, link);
993 
994 		cpl = wrtod(wr);
995 		opcode = GET_OPCODE(cpl);
996 
997 		switch (opcode) {
998 		case CPL_ACT_OPEN_REQ:
999 		case CPL_ACT_OPEN_REQ6:
1000 			atid = G_TID_TID(be32toh(OPCODE_TID(cpl)));
1001 			sc = td_adapter(td);
1002 
1003 			CTR2(KTR_CXGBE, "%s: atid %u ", __func__, atid);
1004 			act_open_failure_cleanup(sc, atid, EHOSTUNREACH);
1005 			free(wr, M_CXGBE);
1006 			break;
1007 		default:
1008 			log(LOG_ERR, "%s: leaked work request %p, wr_len %d, "
1009 			    "opcode %x\n", __func__, wr, wr->wr_len, opcode);
1010 			/* WR not freed here; go look at it with a debugger.  */
1011 		}
1012 	}
1013 }
1014 
1015 /*
1016  * Ground control to Major TOM
1017  * Commencing countdown, engines on
1018  */
1019 static int
1020 t4_tom_activate(struct adapter *sc)
1021 {
1022 	struct tom_data *td;
1023 	struct toedev *tod;
1024 	struct vi_info *vi;
1025 	struct sge_ofld_rxq *ofld_rxq;
1026 	int i, j, rc, v;
1027 
1028 	ASSERT_SYNCHRONIZED_OP(sc);
1029 
1030 	/* per-adapter softc for TOM */
1031 	td = malloc(sizeof(*td), M_CXGBE, M_ZERO | M_NOWAIT);
1032 	if (td == NULL)
1033 		return (ENOMEM);
1034 
1035 	/* List of TOE PCBs and associated lock */
1036 	mtx_init(&td->toep_list_lock, "PCB list lock", NULL, MTX_DEF);
1037 	TAILQ_INIT(&td->toep_list);
1038 
1039 	/* Listen context */
1040 	mtx_init(&td->lctx_hash_lock, "lctx hash lock", NULL, MTX_DEF);
1041 	td->listen_hash = hashinit_flags(LISTEN_HASH_SIZE, M_CXGBE,
1042 	    &td->listen_mask, HASH_NOWAIT);
1043 
1044 	/* List of WRs for which L2 resolution failed */
1045 	mtx_init(&td->unsent_wr_lock, "Unsent WR list lock", NULL, MTX_DEF);
1046 	STAILQ_INIT(&td->unsent_wr_list);
1047 	TASK_INIT(&td->reclaim_wr_resources, 0, reclaim_wr_resources, td);
1048 
1049 	/* TID tables */
1050 	rc = alloc_tid_tabs(&sc->tids);
1051 	if (rc != 0)
1052 		goto done;
1053 
1054 	rc = t4_init_ppod_region(&td->pr, &sc->vres.ddp,
1055 	    t4_read_reg(sc, A_ULP_RX_TDDP_PSZ), "TDDP page pods");
1056 	if (rc != 0)
1057 		goto done;
1058 	t4_set_reg_field(sc, A_ULP_RX_TDDP_TAGMASK,
1059 	    V_TDDPTAGMASK(M_TDDPTAGMASK), td->pr.pr_tag_mask);
1060 
1061 	/* CLIP table for IPv6 offload */
1062 	init_clip_table(sc, td);
1063 
1064 	/* toedev ops */
1065 	tod = &td->tod;
1066 	init_toedev(tod);
1067 	tod->tod_softc = sc;
1068 	tod->tod_connect = t4_connect;
1069 	tod->tod_listen_start = t4_listen_start;
1070 	tod->tod_listen_stop = t4_listen_stop;
1071 	tod->tod_rcvd = t4_rcvd;
1072 	tod->tod_output = t4_tod_output;
1073 	tod->tod_send_rst = t4_send_rst;
1074 	tod->tod_send_fin = t4_send_fin;
1075 	tod->tod_pcb_detach = t4_pcb_detach;
1076 	tod->tod_l2_update = t4_l2_update;
1077 	tod->tod_syncache_added = t4_syncache_added;
1078 	tod->tod_syncache_removed = t4_syncache_removed;
1079 	tod->tod_syncache_respond = t4_syncache_respond;
1080 	tod->tod_offload_socket = t4_offload_socket;
1081 	tod->tod_ctloutput = t4_ctloutput;
1082 
1083 	for_each_port(sc, i) {
1084 		for_each_vi(sc->port[i], v, vi) {
1085 			TOEDEV(vi->ifp) = &td->tod;
1086 			for_each_ofld_rxq(vi, j, ofld_rxq) {
1087 				ofld_rxq->iq.set_tcb_rpl = do_set_tcb_rpl;
1088 				ofld_rxq->iq.l2t_write_rpl = do_l2t_write_rpl2;
1089 			}
1090 		}
1091 	}
1092 
1093 	sc->tom_softc = td;
1094 	register_toedev(sc->tom_softc);
1095 
1096 done:
1097 	if (rc != 0)
1098 		free_tom_data(sc, td);
1099 	return (rc);
1100 }
1101 
1102 static int
1103 t4_tom_deactivate(struct adapter *sc)
1104 {
1105 	int rc = 0;
1106 	struct tom_data *td = sc->tom_softc;
1107 
1108 	ASSERT_SYNCHRONIZED_OP(sc);
1109 
1110 	if (td == NULL)
1111 		return (0);	/* XXX. KASSERT? */
1112 
1113 	if (sc->offload_map != 0)
1114 		return (EBUSY);	/* at least one port has IFCAP_TOE enabled */
1115 
1116 	if (uld_active(sc, ULD_IWARP) || uld_active(sc, ULD_ISCSI))
1117 		return (EBUSY);	/* both iWARP and iSCSI rely on the TOE. */
1118 
1119 	mtx_lock(&td->toep_list_lock);
1120 	if (!TAILQ_EMPTY(&td->toep_list))
1121 		rc = EBUSY;
1122 	mtx_unlock(&td->toep_list_lock);
1123 
1124 	mtx_lock(&td->lctx_hash_lock);
1125 	if (td->lctx_count > 0)
1126 		rc = EBUSY;
1127 	mtx_unlock(&td->lctx_hash_lock);
1128 
1129 	taskqueue_drain(taskqueue_thread, &td->reclaim_wr_resources);
1130 	mtx_lock(&td->unsent_wr_lock);
1131 	if (!STAILQ_EMPTY(&td->unsent_wr_list))
1132 		rc = EBUSY;
1133 	mtx_unlock(&td->unsent_wr_lock);
1134 
1135 	if (rc == 0) {
1136 		unregister_toedev(sc->tom_softc);
1137 		free_tom_data(sc, td);
1138 		sc->tom_softc = NULL;
1139 	}
1140 
1141 	return (rc);
1142 }
1143 
1144 static void
1145 t4_tom_ifaddr_event(void *arg __unused, struct ifnet *ifp)
1146 {
1147 
1148 	atomic_add_rel_int(&in6_ifaddr_gen, 1);
1149 	taskqueue_enqueue_timeout(taskqueue_thread, &clip_task, -hz / 4);
1150 }
1151 
1152 static int
1153 t4_aio_queue_tom(struct socket *so, struct kaiocb *job)
1154 {
1155 	struct tcpcb *tp = so_sototcpcb(so);
1156 	struct toepcb *toep = tp->t_toe;
1157 	int error;
1158 
1159 	if (toep->ulp_mode == ULP_MODE_TCPDDP) {
1160 		error = t4_aio_queue_ddp(so, job);
1161 		if (error != EOPNOTSUPP)
1162 			return (error);
1163 	}
1164 
1165 	return (t4_aio_queue_aiotx(so, job));
1166 }
1167 
1168 static int
1169 t4_tom_mod_load(void)
1170 {
1171 	int rc;
1172 	struct protosw *tcp_protosw, *tcp6_protosw;
1173 
1174 	/* CPL handlers */
1175 	t4_init_connect_cpl_handlers();
1176 	t4_init_listen_cpl_handlers();
1177 	t4_init_cpl_io_handlers();
1178 
1179 	rc = t4_ddp_mod_load();
1180 	if (rc != 0)
1181 		return (rc);
1182 
1183 	tcp_protosw = pffindproto(PF_INET, IPPROTO_TCP, SOCK_STREAM);
1184 	if (tcp_protosw == NULL)
1185 		return (ENOPROTOOPT);
1186 	bcopy(tcp_protosw, &toe_protosw, sizeof(toe_protosw));
1187 	bcopy(tcp_protosw->pr_usrreqs, &toe_usrreqs, sizeof(toe_usrreqs));
1188 	toe_usrreqs.pru_aio_queue = t4_aio_queue_tom;
1189 	toe_protosw.pr_usrreqs = &toe_usrreqs;
1190 
1191 	tcp6_protosw = pffindproto(PF_INET6, IPPROTO_TCP, SOCK_STREAM);
1192 	if (tcp6_protosw == NULL)
1193 		return (ENOPROTOOPT);
1194 	bcopy(tcp6_protosw, &toe6_protosw, sizeof(toe6_protosw));
1195 	bcopy(tcp6_protosw->pr_usrreqs, &toe6_usrreqs, sizeof(toe6_usrreqs));
1196 	toe6_usrreqs.pru_aio_queue = t4_aio_queue_tom;
1197 	toe6_protosw.pr_usrreqs = &toe6_usrreqs;
1198 
1199 	TIMEOUT_TASK_INIT(taskqueue_thread, &clip_task, 0, t4_clip_task, NULL);
1200 	ifaddr_evhandler = EVENTHANDLER_REGISTER(ifaddr_event,
1201 	    t4_tom_ifaddr_event, NULL, EVENTHANDLER_PRI_ANY);
1202 
1203 	rc = t4_register_uld(&tom_uld_info);
1204 	if (rc != 0)
1205 		t4_tom_mod_unload();
1206 
1207 	return (rc);
1208 }
1209 
1210 static void
1211 tom_uninit(struct adapter *sc, void *arg __unused)
1212 {
1213 	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4tomun"))
1214 		return;
1215 
1216 	/* Try to free resources (works only if no port has IFCAP_TOE) */
1217 	if (uld_active(sc, ULD_TOM))
1218 		t4_deactivate_uld(sc, ULD_TOM);
1219 
1220 	end_synchronized_op(sc, 0);
1221 }
1222 
1223 static int
1224 t4_tom_mod_unload(void)
1225 {
1226 	t4_iterate(tom_uninit, NULL);
1227 
1228 	if (t4_unregister_uld(&tom_uld_info) == EBUSY)
1229 		return (EBUSY);
1230 
1231 	if (ifaddr_evhandler) {
1232 		EVENTHANDLER_DEREGISTER(ifaddr_event, ifaddr_evhandler);
1233 		taskqueue_cancel_timeout(taskqueue_thread, &clip_task, NULL);
1234 	}
1235 
1236 	t4_ddp_mod_unload();
1237 
1238 	t4_uninit_connect_cpl_handlers();
1239 	t4_uninit_listen_cpl_handlers();
1240 	t4_uninit_cpl_io_handlers();
1241 
1242 	return (0);
1243 }
1244 #endif	/* TCP_OFFLOAD */
1245 
1246 static int
1247 t4_tom_modevent(module_t mod, int cmd, void *arg)
1248 {
1249 	int rc = 0;
1250 
1251 #ifdef TCP_OFFLOAD
1252 	switch (cmd) {
1253 	case MOD_LOAD:
1254 		rc = t4_tom_mod_load();
1255 		break;
1256 
1257 	case MOD_UNLOAD:
1258 		rc = t4_tom_mod_unload();
1259 		break;
1260 
1261 	default:
1262 		rc = EINVAL;
1263 	}
1264 #else
1265 	printf("t4_tom: compiled without TCP_OFFLOAD support.\n");
1266 	rc = EOPNOTSUPP;
1267 #endif
1268 	return (rc);
1269 }
1270 
1271 static moduledata_t t4_tom_moddata= {
1272 	"t4_tom",
1273 	t4_tom_modevent,
1274 	0
1275 };
1276 
1277 MODULE_VERSION(t4_tom, 1);
1278 MODULE_DEPEND(t4_tom, toecore, 1, 1, 1);
1279 MODULE_DEPEND(t4_tom, t4nex, 1, 1, 1);
1280 DECLARE_MODULE(t4_tom, t4_tom_moddata, SI_SUB_EXEC, SI_ORDER_ANY);
1281