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