t4_tom.c revision 331722
1214117Sjamie/*-
2223190Sjamie * Copyright (c) 2012 Chelsio Communications, Inc.
3214117Sjamie * All rights reserved.
4214117Sjamie * Written by: Navdeep Parhar <np@FreeBSD.org>
5214117Sjamie *
6214117Sjamie * Redistribution and use in source and binary forms, with or without
7214117Sjamie * modification, are permitted provided that the following conditions
8214117Sjamie * are met:
9214117Sjamie * 1. Redistributions of source code must retain the above copyright
10214117Sjamie *    notice, this list of conditions and the following disclaimer.
11214117Sjamie * 2. Redistributions in binary form must reproduce the above copyright
12214117Sjamie *    notice, this list of conditions and the following disclaimer in the
13214117Sjamie *    documentation and/or other materials provided with the distribution.
14214117Sjamie *
15214117Sjamie * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16214117Sjamie * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17214117Sjamie * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18214117Sjamie * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19214117Sjamie * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20214117Sjamie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21214117Sjamie * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22214117Sjamie * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23214117Sjamie * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24214117Sjamie * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25214117Sjamie * SUCH DAMAGE.
26214117Sjamie */
27214117Sjamie
28214117Sjamie#include <sys/cdefs.h>
29214117Sjamie__FBSDID("$FreeBSD: stable/11/sys/dev/cxgbe/tom/t4_tom.c 331722 2018-03-29 02:50:57Z eadler $");
30214117Sjamie
31214117Sjamie#include "opt_inet.h"
32214117Sjamie#include "opt_inet6.h"
33214117Sjamie
34214117Sjamie#include <sys/param.h>
35214117Sjamie#include <sys/types.h>
36214117Sjamie#include <sys/systm.h>
37214117Sjamie#include <sys/kernel.h>
38214117Sjamie#include <sys/ktr.h>
39214117Sjamie#include <sys/lock.h>
40214117Sjamie#include <sys/limits.h>
41214117Sjamie#include <sys/module.h>
42214117Sjamie#include <sys/protosw.h>
43214117Sjamie#include <sys/domain.h>
44214117Sjamie#include <sys/refcount.h>
45214117Sjamie#include <sys/rmlock.h>
46214117Sjamie#include <sys/socket.h>
47214117Sjamie#include <sys/socketvar.h>
48214117Sjamie#include <sys/taskqueue.h>
49214117Sjamie#include <net/if.h>
50214117Sjamie#include <net/if_var.h>
51214117Sjamie#include <netinet/in.h>
52214423Sjamie#include <netinet/in_pcb.h>
53248854Sjamie#include <netinet/in_var.h>
54214117Sjamie#include <netinet/ip.h>
55214117Sjamie#include <netinet/ip6.h>
56214117Sjamie#include <netinet6/scope6_var.h>
57214117Sjamie#define TCPSTATES
58214117Sjamie#include <netinet/tcp_fsm.h>
59214117Sjamie#include <netinet/tcp_timer.h>
60214117Sjamie#include <netinet/tcp_var.h>
61214649Sjamie#include <netinet/toecore.h>
62214649Sjamie
63223189Sjamie#ifdef TCP_OFFLOAD
64223189Sjamie#include "common/common.h"
65223189Sjamie#include "common/t4_msg.h"
66214117Sjamie#include "common/t4_regs.h"
67214117Sjamie#include "common/t4_regs_values.h"
68214117Sjamie#include "common/t4_tcb.h"
69214117Sjamie#include "tom/t4_tom_l2t.h"
70214117Sjamie#include "tom/t4_tom.h"
71214117Sjamie
72214117Sjamiestatic struct protosw toe_protosw;
73214117Sjamiestatic struct pr_usrreqs toe_usrreqs;
74214117Sjamie
75234988Sjamiestatic struct protosw toe6_protosw;
76234988Sjamiestatic struct pr_usrreqs toe6_usrreqs;
77214117Sjamie
78214117Sjamie/* Module ops */
79214117Sjamiestatic int t4_tom_mod_load(void);
80214117Sjamiestatic int t4_tom_mod_unload(void);
81214117Sjamiestatic int t4_tom_modevent(module_t, int, void *);
82214117Sjamie
83214117Sjamie/* ULD ops and helpers */
84214117Sjamiestatic int t4_tom_activate(struct adapter *);
85214117Sjamiestatic int t4_tom_deactivate(struct adapter *);
86214117Sjamie
87214117Sjamiestatic struct uld_info tom_uld_info = {
88214117Sjamie	.uld_id = ULD_TOM,
89214117Sjamie	.activate = t4_tom_activate,
90214117Sjamie	.deactivate = t4_tom_deactivate,
91214117Sjamie};
92223351Sjamie
93214117Sjamiestatic void queue_tid_release(struct adapter *, int);
94214117Sjamiestatic void release_offload_resources(struct toepcb *);
95223351Sjamiestatic int alloc_tid_tabs(struct tid_info *);
96214117Sjamiestatic void free_tid_tabs(struct tid_info *);
97214117Sjamiestatic int add_lip(struct adapter *, struct in6_addr *);
98256387Shrsstatic int delete_lip(struct adapter *, struct in6_addr *);
99214423Sjamiestatic struct clip_entry *search_lip(struct tom_data *, struct in6_addr *);
100214117Sjamiestatic void init_clip_table(struct adapter *, struct tom_data *);
101214423Sjamiestatic void update_clip(struct adapter *, void *);
102223351Sjamiestatic void t4_clip_task(void *, int);
103214117Sjamiestatic void update_clip_table(struct adapter *, struct tom_data *);
104223351Sjamiestatic void destroy_clip_table(struct adapter *, struct tom_data *);
105214117Sjamiestatic void free_tom_data(struct adapter *, struct tom_data *);
106214117Sjamiestatic void reclaim_wr_resources(void *, int);
107214117Sjamie
108214783Sjamiestatic int in6_ifaddr_gen;
109223189Sjamiestatic eventhandler_tag ifaddr_evhandler;
110214423Sjamiestatic struct timeout_task clip_task;
111214423Sjamie
112214423Sjamiestruct toepcb *
113214423Sjamiealloc_toepcb(struct vi_info *vi, int txqid, int rxqid, int flags)
114214423Sjamie{
115214423Sjamie	struct port_info *pi = vi->pi;
116232242Sjamie	struct adapter *sc = pi->adapter;
117214423Sjamie	struct toepcb *toep;
118214423Sjamie	int tx_credits, txsd_total, len;
119223351Sjamie
120214117Sjamie	/*
121223351Sjamie	 * The firmware counts tx work request credits in units of 16 bytes
122214117Sjamie	 * each.  Reserve room for an ABORT_REQ so the driver never has to worry
123214117Sjamie	 * about tx credits if it wants to abort a connection.
124214117Sjamie	 */
125214117Sjamie	tx_credits = sc->params.ofldq_wr_cred;
126214117Sjamie	tx_credits -= howmany(sizeof(struct cpl_abort_req), 16);
127214117Sjamie
128214117Sjamie	/*
129214423Sjamie	 * Shortest possible tx work request is a fw_ofld_tx_data_wr + 1 byte
130214117Sjamie	 * immediate payload, and firmware counts tx work request credits in
131214117Sjamie	 * units of 16 byte.  Calculate the maximum work requests possible.
132214117Sjamie	 */
133214117Sjamie	txsd_total = tx_credits /
134214117Sjamie	    howmany(sizeof(struct fw_ofld_tx_data_wr) + 1, 16);
135214117Sjamie
136214117Sjamie	if (txqid < 0)
137214117Sjamie		txqid = (arc4random() % vi->nofldtxq) + vi->first_ofld_txq;
138214117Sjamie	KASSERT(txqid >= vi->first_ofld_txq &&
139214117Sjamie	    txqid < vi->first_ofld_txq + vi->nofldtxq,
140214117Sjamie	    ("%s: txqid %d for vi %p (first %d, n %d)", __func__, txqid, vi,
141214117Sjamie		vi->first_ofld_txq, vi->nofldtxq));
142223188Sjamie
143214117Sjamie	if (rxqid < 0)
144214117Sjamie		rxqid = (arc4random() % vi->nofldrxq) + vi->first_ofld_rxq;
145223188Sjamie	KASSERT(rxqid >= vi->first_ofld_rxq &&
146214117Sjamie	    rxqid < vi->first_ofld_rxq + vi->nofldrxq,
147214117Sjamie	    ("%s: rxqid %d for vi %p (first %d, n %d)", __func__, rxqid, vi,
148214117Sjamie		vi->first_ofld_rxq, vi->nofldrxq));
149214117Sjamie
150214117Sjamie	len = offsetof(struct toepcb, txsd) +
151214117Sjamie	    txsd_total * sizeof(struct ofld_tx_sdesc);
152214117Sjamie
153214117Sjamie	toep = malloc(len, M_CXGBE, M_ZERO | flags);
154214117Sjamie	if (toep == NULL)
155214117Sjamie		return (NULL);
156214117Sjamie
157214117Sjamie	refcount_init(&toep->refcount, 1);
158214117Sjamie	toep->td = sc->tom_softc;
159214117Sjamie	toep->vi = vi;
160214117Sjamie	toep->tx_total = tx_credits;
161214117Sjamie	toep->tx_credits = tx_credits;
162214117Sjamie	toep->ofld_txq = &sc->sge.ofld_txq[txqid];
163214117Sjamie	toep->ofld_rxq = &sc->sge.ofld_rxq[rxqid];
164214117Sjamie	toep->ctrlq = &sc->sge.ctrlq[pi->port_id];
165214117Sjamie	mbufq_init(&toep->ulp_pduq, INT_MAX);
166214117Sjamie	mbufq_init(&toep->ulp_pdu_reclaimq, INT_MAX);
167214117Sjamie	toep->txsd_total = txsd_total;
168214117Sjamie	toep->txsd_avail = txsd_total;
169214117Sjamie	toep->txsd_pidx = 0;
170214117Sjamie	toep->txsd_cidx = 0;
171214117Sjamie	aiotx_init_toep(toep);
172214117Sjamie
173214117Sjamie	return (toep);
174214117Sjamie}
175223189Sjamie
176214117Sjamiestruct toepcb *
177214117Sjamiehold_toepcb(struct toepcb *toep)
178214117Sjamie{
179214117Sjamie
180214117Sjamie	refcount_acquire(&toep->refcount);
181214117Sjamie	return (toep);
182214117Sjamie}
183214117Sjamie
184214117Sjamievoid
185214117Sjamiefree_toepcb(struct toepcb *toep)
186214117Sjamie{
187214117Sjamie
188214117Sjamie	if (refcount_release(&toep->refcount) == 0)
189214117Sjamie		return;
190214117Sjamie
191214117Sjamie	KASSERT(!(toep->flags & TPF_ATTACHED),
192214117Sjamie	    ("%s: attached to an inpcb", __func__));
193214117Sjamie	KASSERT(!(toep->flags & TPF_CPL_PENDING),
194223263Sjamie	    ("%s: CPL pending", __func__));
195214117Sjamie
196214117Sjamie	if (toep->ulp_mode == ULP_MODE_TCPDDP)
197214117Sjamie		ddp_uninit_toep(toep);
198214117Sjamie	free(toep, M_CXGBE);
199223189Sjamie}
200216367Sjamie
201214117Sjamie/*
202214117Sjamie * Set up the socket for TCP offload.
203214117Sjamie */
204214117Sjamievoid
205214117Sjamieoffload_socket(struct socket *so, struct toepcb *toep)
206214423Sjamie{
207214117Sjamie	struct tom_data *td = toep->td;
208214117Sjamie	struct inpcb *inp = sotoinpcb(so);
209214117Sjamie	struct tcpcb *tp = intotcpcb(inp);
210214649Sjamie	struct sockbuf *sb;
211214117Sjamie
212214117Sjamie	INP_WLOCK_ASSERT(inp);
213214117Sjamie
214214117Sjamie	/* Update socket */
215214117Sjamie	sb = &so->so_snd;
216214117Sjamie	SOCKBUF_LOCK(sb);
217214117Sjamie	sb->sb_flags |= SB_NOCOALESCE;
218214117Sjamie	SOCKBUF_UNLOCK(sb);
219214117Sjamie	sb = &so->so_rcv;
220214117Sjamie	SOCKBUF_LOCK(sb);
221231238Sjamie	sb->sb_flags |= SB_NOCOALESCE;
222231238Sjamie	if (inp->inp_vflag & INP_IPV6)
223214117Sjamie		so->so_proto = &toe6_protosw;
224214117Sjamie	else
225214117Sjamie		so->so_proto = &toe_protosw;
226214117Sjamie	SOCKBUF_UNLOCK(sb);
227214117Sjamie
228214117Sjamie	/* Update TCP PCB */
229214117Sjamie	tp->tod = &td->tod;
230216367Sjamie	tp->t_toe = toep;
231214117Sjamie	tp->t_flags |= TF_TOE;
232236198Sjamie
233223263Sjamie	/* Install an extra hold on inp */
234216367Sjamie	toep->inp = inp;
235214117Sjamie	toep->flags |= TPF_ATTACHED;
236	in_pcbref(inp);
237
238	/* Add the TOE PCB to the active list */
239	mtx_lock(&td->toep_list_lock);
240	TAILQ_INSERT_HEAD(&td->toep_list, toep, link);
241	mtx_unlock(&td->toep_list_lock);
242}
243
244/* This is _not_ the normal way to "unoffload" a socket. */
245void
246undo_offload_socket(struct socket *so)
247{
248	struct inpcb *inp = sotoinpcb(so);
249	struct tcpcb *tp = intotcpcb(inp);
250	struct toepcb *toep = tp->t_toe;
251	struct tom_data *td = toep->td;
252	struct sockbuf *sb;
253
254	INP_WLOCK_ASSERT(inp);
255
256	sb = &so->so_snd;
257	SOCKBUF_LOCK(sb);
258	sb->sb_flags &= ~SB_NOCOALESCE;
259	SOCKBUF_UNLOCK(sb);
260	sb = &so->so_rcv;
261	SOCKBUF_LOCK(sb);
262	sb->sb_flags &= ~SB_NOCOALESCE;
263	SOCKBUF_UNLOCK(sb);
264
265	tp->tod = NULL;
266	tp->t_toe = NULL;
267	tp->t_flags &= ~TF_TOE;
268
269	toep->inp = NULL;
270	toep->flags &= ~TPF_ATTACHED;
271	if (in_pcbrele_wlocked(inp))
272		panic("%s: inp freed.", __func__);
273
274	mtx_lock(&td->toep_list_lock);
275	TAILQ_REMOVE(&td->toep_list, toep, link);
276	mtx_unlock(&td->toep_list_lock);
277}
278
279static void
280release_offload_resources(struct toepcb *toep)
281{
282	struct tom_data *td = toep->td;
283	struct adapter *sc = td_adapter(td);
284	int tid = toep->tid;
285
286	KASSERT(!(toep->flags & TPF_CPL_PENDING),
287	    ("%s: %p has CPL pending.", __func__, toep));
288	KASSERT(!(toep->flags & TPF_ATTACHED),
289	    ("%s: %p is still attached.", __func__, toep));
290
291	CTR5(KTR_CXGBE, "%s: toep %p (tid %d, l2te %p, ce %p)",
292	    __func__, toep, tid, toep->l2te, toep->ce);
293
294	/*
295	 * These queues should have been emptied at approximately the same time
296	 * that a normal connection's socket's so_snd would have been purged or
297	 * drained.  Do _not_ clean up here.
298	 */
299	MPASS(mbufq_len(&toep->ulp_pduq) == 0);
300	MPASS(mbufq_len(&toep->ulp_pdu_reclaimq) == 0);
301#ifdef INVARIANTS
302	if (toep->ulp_mode == ULP_MODE_TCPDDP)
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	mtx_lock(&td->toep_list_lock);
318	TAILQ_REMOVE(&td->toep_list, toep, link);
319	mtx_unlock(&td->toep_list_lock);
320
321	free_toepcb(toep);
322}
323
324/*
325 * The kernel is done with the TCP PCB and this is our opportunity to unhook the
326 * toepcb hanging off of it.  If the TOE driver is also done with the toepcb (no
327 * pending CPL) then it is time to release all resources tied to the toepcb.
328 *
329 * Also gets called when an offloaded active open fails and the TOM wants the
330 * kernel to take the TCP PCB back.
331 */
332static void
333t4_pcb_detach(struct toedev *tod __unused, struct tcpcb *tp)
334{
335#if defined(KTR) || defined(INVARIANTS)
336	struct inpcb *inp = tp->t_inpcb;
337#endif
338	struct toepcb *toep = tp->t_toe;
339
340	INP_WLOCK_ASSERT(inp);
341
342	KASSERT(toep != NULL, ("%s: toep is NULL", __func__));
343	KASSERT(toep->flags & TPF_ATTACHED,
344	    ("%s: not attached", __func__));
345
346#ifdef KTR
347	if (tp->t_state == TCPS_SYN_SENT) {
348		CTR6(KTR_CXGBE, "%s: atid %d, toep %p (0x%x), inp %p (0x%x)",
349		    __func__, toep->tid, toep, toep->flags, inp,
350		    inp->inp_flags);
351	} else {
352		CTR6(KTR_CXGBE,
353		    "t4_pcb_detach: tid %d (%s), toep %p (0x%x), inp %p (0x%x)",
354		    toep->tid, tcpstates[tp->t_state], toep, toep->flags, inp,
355		    inp->inp_flags);
356	}
357#endif
358
359	tp->t_toe = NULL;
360	tp->t_flags &= ~TF_TOE;
361	toep->flags &= ~TPF_ATTACHED;
362
363	if (!(toep->flags & TPF_CPL_PENDING))
364		release_offload_resources(toep);
365}
366
367/*
368 * setsockopt handler.
369 */
370static void
371t4_ctloutput(struct toedev *tod, struct tcpcb *tp, int dir, int name)
372{
373	struct adapter *sc = tod->tod_softc;
374	struct toepcb *toep = tp->t_toe;
375
376	if (dir == SOPT_GET)
377		return;
378
379	CTR4(KTR_CXGBE, "%s: tp %p, dir %u, name %u", __func__, tp, dir, name);
380
381	switch (name) {
382	case TCP_NODELAY:
383		if (tp->t_state != TCPS_ESTABLISHED)
384			break;
385		t4_set_tcb_field(sc, toep->ctrlq, toep->tid, W_TCB_T_FLAGS,
386		    V_TF_NAGLE(1), V_TF_NAGLE(tp->t_flags & TF_NODELAY ? 0 : 1),
387		    0, 0, toep->ofld_rxq->iq.abs_id);
388		break;
389	default:
390		break;
391	}
392}
393
394/*
395 * The TOE driver will not receive any more CPLs for the tid associated with the
396 * toepcb; release the hold on the inpcb.
397 */
398void
399final_cpl_received(struct toepcb *toep)
400{
401	struct inpcb *inp = toep->inp;
402
403	KASSERT(inp != NULL, ("%s: inp is NULL", __func__));
404	INP_WLOCK_ASSERT(inp);
405	KASSERT(toep->flags & TPF_CPL_PENDING,
406	    ("%s: CPL not pending already?", __func__));
407
408	CTR6(KTR_CXGBE, "%s: tid %d, toep %p (0x%x), inp %p (0x%x)",
409	    __func__, toep->tid, toep, toep->flags, inp, inp->inp_flags);
410
411	if (toep->ulp_mode == ULP_MODE_TCPDDP)
412		release_ddp_resources(toep);
413	toep->inp = NULL;
414	toep->flags &= ~TPF_CPL_PENDING;
415	mbufq_drain(&toep->ulp_pdu_reclaimq);
416
417	if (!(toep->flags & TPF_ATTACHED))
418		release_offload_resources(toep);
419
420	if (!in_pcbrele_wlocked(inp))
421		INP_WUNLOCK(inp);
422}
423
424void
425insert_tid(struct adapter *sc, int tid, void *ctx, int ntids)
426{
427	struct tid_info *t = &sc->tids;
428
429	t->tid_tab[tid] = ctx;
430	atomic_add_int(&t->tids_in_use, ntids);
431}
432
433void *
434lookup_tid(struct adapter *sc, int tid)
435{
436	struct tid_info *t = &sc->tids;
437
438	return (t->tid_tab[tid]);
439}
440
441void
442update_tid(struct adapter *sc, int tid, void *ctx)
443{
444	struct tid_info *t = &sc->tids;
445
446	t->tid_tab[tid] = ctx;
447}
448
449void
450remove_tid(struct adapter *sc, int tid, int ntids)
451{
452	struct tid_info *t = &sc->tids;
453
454	t->tid_tab[tid] = NULL;
455	atomic_subtract_int(&t->tids_in_use, ntids);
456}
457
458void
459release_tid(struct adapter *sc, int tid, struct sge_wrq *ctrlq)
460{
461	struct wrqe *wr;
462	struct cpl_tid_release *req;
463
464	wr = alloc_wrqe(sizeof(*req), ctrlq);
465	if (wr == NULL) {
466		queue_tid_release(sc, tid);	/* defer */
467		return;
468	}
469	req = wrtod(wr);
470
471	INIT_TP_WR_MIT_CPL(req, CPL_TID_RELEASE, tid);
472
473	t4_wrq_tx(sc, wr);
474}
475
476static void
477queue_tid_release(struct adapter *sc, int tid)
478{
479
480	CXGBE_UNIMPLEMENTED("deferred tid release");
481}
482
483/*
484 * What mtu_idx to use, given a 4-tuple and/or an MSS cap
485 */
486int
487find_best_mtu_idx(struct adapter *sc, struct in_conninfo *inc, int pmss)
488{
489	unsigned short *mtus = &sc->params.mtus[0];
490	int i, mss, n;
491
492	KASSERT(inc != NULL || pmss > 0,
493	    ("%s: at least one of inc/pmss must be specified", __func__));
494
495	mss = inc ? tcp_mssopt(inc) : pmss;
496	if (pmss > 0 && mss > pmss)
497		mss = pmss;
498
499	if (inc->inc_flags & INC_ISIPV6)
500		n = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
501	else
502		n = sizeof(struct ip) + sizeof(struct tcphdr);
503
504	for (i = 0; i < NMTUS - 1 && mtus[i + 1] <= mss + n; i++)
505		continue;
506
507	return (i);
508}
509
510/*
511 * Determine the receive window size for a socket.
512 */
513u_long
514select_rcv_wnd(struct socket *so)
515{
516	unsigned long wnd;
517
518	SOCKBUF_LOCK_ASSERT(&so->so_rcv);
519
520	wnd = sbspace(&so->so_rcv);
521	if (wnd < MIN_RCV_WND)
522		wnd = MIN_RCV_WND;
523
524	return min(wnd, MAX_RCV_WND);
525}
526
527int
528select_rcv_wscale(void)
529{
530	int wscale = 0;
531	unsigned long space = sb_max;
532
533	if (space > MAX_RCV_WND)
534		space = MAX_RCV_WND;
535
536	while (wscale < TCP_MAX_WINSHIFT && (TCP_MAXWIN << wscale) < space)
537		wscale++;
538
539	return (wscale);
540}
541
542/*
543 * socket so could be a listening socket too.
544 */
545uint64_t
546calc_opt0(struct socket *so, struct vi_info *vi, struct l2t_entry *e,
547    int mtu_idx, int rscale, int rx_credits, int ulp_mode)
548{
549	uint64_t opt0;
550
551	KASSERT(rx_credits <= M_RCV_BUFSIZ,
552	    ("%s: rcv_bufsiz too high", __func__));
553
554	opt0 = F_TCAM_BYPASS | V_WND_SCALE(rscale) | V_MSS_IDX(mtu_idx) |
555	    V_ULP_MODE(ulp_mode) | V_RCV_BUFSIZ(rx_credits);
556
557	if (so != NULL) {
558		struct inpcb *inp = sotoinpcb(so);
559		struct tcpcb *tp = intotcpcb(inp);
560		int keepalive = tcp_always_keepalive ||
561		    so_options_get(so) & SO_KEEPALIVE;
562
563		opt0 |= V_NAGLE((tp->t_flags & TF_NODELAY) == 0);
564		opt0 |= V_KEEP_ALIVE(keepalive != 0);
565	}
566
567	if (e != NULL)
568		opt0 |= V_L2T_IDX(e->idx);
569
570	if (vi != NULL) {
571		opt0 |= V_SMAC_SEL(vi->smt_idx);
572		opt0 |= V_TX_CHAN(vi->pi->tx_chan);
573	}
574
575	return htobe64(opt0);
576}
577
578uint64_t
579select_ntuple(struct vi_info *vi, struct l2t_entry *e)
580{
581	struct adapter *sc = vi->pi->adapter;
582	struct tp_params *tp = &sc->params.tp;
583	uint16_t viid = vi->viid;
584	uint64_t ntuple = 0;
585
586	/*
587	 * Initialize each of the fields which we care about which are present
588	 * in the Compressed Filter Tuple.
589	 */
590	if (tp->vlan_shift >= 0 && e->vlan != CPL_L2T_VLAN_NONE)
591		ntuple |= (uint64_t)(F_FT_VLAN_VLD | e->vlan) << tp->vlan_shift;
592
593	if (tp->port_shift >= 0)
594		ntuple |= (uint64_t)e->lport << tp->port_shift;
595
596	if (tp->protocol_shift >= 0)
597		ntuple |= (uint64_t)IPPROTO_TCP << tp->protocol_shift;
598
599	if (tp->vnic_shift >= 0) {
600		uint32_t vf = G_FW_VIID_VIN(viid);
601		uint32_t pf = G_FW_VIID_PFN(viid);
602		uint32_t vld = G_FW_VIID_VIVLD(viid);
603
604		ntuple |= (uint64_t)(V_FT_VNID_ID_VF(vf) | V_FT_VNID_ID_PF(pf) |
605		    V_FT_VNID_ID_VLD(vld)) << tp->vnic_shift;
606	}
607
608	if (is_t4(sc))
609		return (htobe32((uint32_t)ntuple));
610	else
611		return (htobe64(V_FILTER_TUPLE(ntuple)));
612}
613
614void
615set_tcpddp_ulp_mode(struct toepcb *toep)
616{
617
618	toep->ulp_mode = ULP_MODE_TCPDDP;
619	ddp_init_toep(toep);
620}
621
622int
623negative_advice(int status)
624{
625
626	return (status == CPL_ERR_RTX_NEG_ADVICE ||
627	    status == CPL_ERR_PERSIST_NEG_ADVICE ||
628	    status == CPL_ERR_KEEPALV_NEG_ADVICE);
629}
630
631static int
632alloc_tid_tabs(struct tid_info *t)
633{
634	size_t size;
635	unsigned int i;
636
637	size = t->ntids * sizeof(*t->tid_tab) +
638	    t->natids * sizeof(*t->atid_tab) +
639	    t->nstids * sizeof(*t->stid_tab);
640
641	t->tid_tab = malloc(size, M_CXGBE, M_ZERO | M_NOWAIT);
642	if (t->tid_tab == NULL)
643		return (ENOMEM);
644
645	mtx_init(&t->atid_lock, "atid lock", NULL, MTX_DEF);
646	t->atid_tab = (union aopen_entry *)&t->tid_tab[t->ntids];
647	t->afree = t->atid_tab;
648	t->atids_in_use = 0;
649	for (i = 1; i < t->natids; i++)
650		t->atid_tab[i - 1].next = &t->atid_tab[i];
651	t->atid_tab[t->natids - 1].next = NULL;
652
653	mtx_init(&t->stid_lock, "stid lock", NULL, MTX_DEF);
654	t->stid_tab = (struct listen_ctx **)&t->atid_tab[t->natids];
655	t->stids_in_use = 0;
656	TAILQ_INIT(&t->stids);
657	t->nstids_free_head = t->nstids;
658
659	atomic_store_rel_int(&t->tids_in_use, 0);
660
661	return (0);
662}
663
664static void
665free_tid_tabs(struct tid_info *t)
666{
667	KASSERT(t->tids_in_use == 0,
668	    ("%s: %d tids still in use.", __func__, t->tids_in_use));
669	KASSERT(t->atids_in_use == 0,
670	    ("%s: %d atids still in use.", __func__, t->atids_in_use));
671	KASSERT(t->stids_in_use == 0,
672	    ("%s: %d tids still in use.", __func__, t->stids_in_use));
673
674	free(t->tid_tab, M_CXGBE);
675	t->tid_tab = NULL;
676
677	if (mtx_initialized(&t->atid_lock))
678		mtx_destroy(&t->atid_lock);
679	if (mtx_initialized(&t->stid_lock))
680		mtx_destroy(&t->stid_lock);
681}
682
683static int
684add_lip(struct adapter *sc, struct in6_addr *lip)
685{
686        struct fw_clip_cmd c;
687
688	ASSERT_SYNCHRONIZED_OP(sc);
689	/* mtx_assert(&td->clip_table_lock, MA_OWNED); */
690
691        memset(&c, 0, sizeof(c));
692	c.op_to_write = htonl(V_FW_CMD_OP(FW_CLIP_CMD) | F_FW_CMD_REQUEST |
693	    F_FW_CMD_WRITE);
694        c.alloc_to_len16 = htonl(F_FW_CLIP_CMD_ALLOC | FW_LEN16(c));
695        c.ip_hi = *(uint64_t *)&lip->s6_addr[0];
696        c.ip_lo = *(uint64_t *)&lip->s6_addr[8];
697
698	return (-t4_wr_mbox_ns(sc, sc->mbox, &c, sizeof(c), &c));
699}
700
701static int
702delete_lip(struct adapter *sc, struct in6_addr *lip)
703{
704	struct fw_clip_cmd c;
705
706	ASSERT_SYNCHRONIZED_OP(sc);
707	/* mtx_assert(&td->clip_table_lock, MA_OWNED); */
708
709	memset(&c, 0, sizeof(c));
710	c.op_to_write = htonl(V_FW_CMD_OP(FW_CLIP_CMD) | F_FW_CMD_REQUEST |
711	    F_FW_CMD_READ);
712        c.alloc_to_len16 = htonl(F_FW_CLIP_CMD_FREE | FW_LEN16(c));
713        c.ip_hi = *(uint64_t *)&lip->s6_addr[0];
714        c.ip_lo = *(uint64_t *)&lip->s6_addr[8];
715
716	return (-t4_wr_mbox_ns(sc, sc->mbox, &c, sizeof(c), &c));
717}
718
719static struct clip_entry *
720search_lip(struct tom_data *td, struct in6_addr *lip)
721{
722	struct clip_entry *ce;
723
724	mtx_assert(&td->clip_table_lock, MA_OWNED);
725
726	TAILQ_FOREACH(ce, &td->clip_table, link) {
727		if (IN6_ARE_ADDR_EQUAL(&ce->lip, lip))
728			return (ce);
729	}
730
731	return (NULL);
732}
733
734struct clip_entry *
735hold_lip(struct tom_data *td, struct in6_addr *lip, struct clip_entry *ce)
736{
737
738	mtx_lock(&td->clip_table_lock);
739	if (ce == NULL)
740		ce = search_lip(td, lip);
741	if (ce != NULL)
742		ce->refcount++;
743	mtx_unlock(&td->clip_table_lock);
744
745	return (ce);
746}
747
748void
749release_lip(struct tom_data *td, struct clip_entry *ce)
750{
751
752	mtx_lock(&td->clip_table_lock);
753	KASSERT(search_lip(td, &ce->lip) == ce,
754	    ("%s: CLIP entry %p p not in CLIP table.", __func__, ce));
755	KASSERT(ce->refcount > 0,
756	    ("%s: CLIP entry %p has refcount 0", __func__, ce));
757	--ce->refcount;
758	mtx_unlock(&td->clip_table_lock);
759}
760
761static void
762init_clip_table(struct adapter *sc, struct tom_data *td)
763{
764
765	ASSERT_SYNCHRONIZED_OP(sc);
766
767	mtx_init(&td->clip_table_lock, "CLIP table lock", NULL, MTX_DEF);
768	TAILQ_INIT(&td->clip_table);
769	td->clip_gen = -1;
770
771	update_clip_table(sc, td);
772}
773
774static void
775update_clip(struct adapter *sc, void *arg __unused)
776{
777
778	if (begin_synchronized_op(sc, NULL, HOLD_LOCK, "t4tomuc"))
779		return;
780
781	if (uld_active(sc, ULD_TOM))
782		update_clip_table(sc, sc->tom_softc);
783
784	end_synchronized_op(sc, LOCK_HELD);
785}
786
787static void
788t4_clip_task(void *arg, int count)
789{
790
791	t4_iterate(update_clip, NULL);
792}
793
794static void
795update_clip_table(struct adapter *sc, struct tom_data *td)
796{
797	struct rm_priotracker in6_ifa_tracker;
798	struct in6_ifaddr *ia;
799	struct in6_addr *lip, tlip;
800	struct clip_head stale;
801	struct clip_entry *ce, *ce_temp;
802	struct vi_info *vi;
803	int rc, gen, i, j;
804	uintptr_t last_vnet;
805
806	ASSERT_SYNCHRONIZED_OP(sc);
807
808	IN6_IFADDR_RLOCK(&in6_ifa_tracker);
809	mtx_lock(&td->clip_table_lock);
810
811	gen = atomic_load_acq_int(&in6_ifaddr_gen);
812	if (gen == td->clip_gen)
813		goto done;
814
815	TAILQ_INIT(&stale);
816	TAILQ_CONCAT(&stale, &td->clip_table, link);
817
818	/*
819	 * last_vnet optimizes the common cases where all if_vnet = NULL (no
820	 * VIMAGE) or all if_vnet = vnet0.
821	 */
822	last_vnet = (uintptr_t)(-1);
823	for_each_port(sc, i)
824	for_each_vi(sc->port[i], j, vi) {
825		if (last_vnet == (uintptr_t)vi->ifp->if_vnet)
826			continue;
827
828		/* XXX: races with if_vmove */
829		CURVNET_SET(vi->ifp->if_vnet);
830		TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
831			lip = &ia->ia_addr.sin6_addr;
832
833			KASSERT(!IN6_IS_ADDR_MULTICAST(lip),
834			    ("%s: mcast address in in6_ifaddr list", __func__));
835
836			if (IN6_IS_ADDR_LOOPBACK(lip))
837				continue;
838			if (IN6_IS_SCOPE_EMBED(lip)) {
839				/* Remove the embedded scope */
840				tlip = *lip;
841				lip = &tlip;
842				in6_clearscope(lip);
843			}
844			/*
845			 * XXX: how to weed out the link local address for the
846			 * loopback interface?  It's fe80::1 usually (always?).
847			 */
848
849			/*
850			 * If it's in the main list then we already know it's
851			 * not stale.
852			 */
853			TAILQ_FOREACH(ce, &td->clip_table, link) {
854				if (IN6_ARE_ADDR_EQUAL(&ce->lip, lip))
855					goto next;
856			}
857
858			/*
859			 * If it's in the stale list we should move it to the
860			 * main list.
861			 */
862			TAILQ_FOREACH(ce, &stale, link) {
863				if (IN6_ARE_ADDR_EQUAL(&ce->lip, lip)) {
864					TAILQ_REMOVE(&stale, ce, link);
865					TAILQ_INSERT_TAIL(&td->clip_table, ce,
866					    link);
867					goto next;
868				}
869			}
870
871			/* A new IP6 address; add it to the CLIP table */
872			ce = malloc(sizeof(*ce), M_CXGBE, M_NOWAIT);
873			memcpy(&ce->lip, lip, sizeof(ce->lip));
874			ce->refcount = 0;
875			rc = add_lip(sc, lip);
876			if (rc == 0)
877				TAILQ_INSERT_TAIL(&td->clip_table, ce, link);
878			else {
879				char ip[INET6_ADDRSTRLEN];
880
881				inet_ntop(AF_INET6, &ce->lip, &ip[0],
882				    sizeof(ip));
883				log(LOG_ERR, "%s: could not add %s (%d)\n",
884				    __func__, ip, rc);
885				free(ce, M_CXGBE);
886			}
887next:
888			continue;
889		}
890		CURVNET_RESTORE();
891		last_vnet = (uintptr_t)vi->ifp->if_vnet;
892	}
893
894	/*
895	 * Remove stale addresses (those no longer in V_in6_ifaddrhead) that are
896	 * no longer referenced by the driver.
897	 */
898	TAILQ_FOREACH_SAFE(ce, &stale, link, ce_temp) {
899		if (ce->refcount == 0) {
900			rc = delete_lip(sc, &ce->lip);
901			if (rc == 0) {
902				TAILQ_REMOVE(&stale, ce, link);
903				free(ce, M_CXGBE);
904			} else {
905				char ip[INET6_ADDRSTRLEN];
906
907				inet_ntop(AF_INET6, &ce->lip, &ip[0],
908				    sizeof(ip));
909				log(LOG_ERR, "%s: could not delete %s (%d)\n",
910				    __func__, ip, rc);
911			}
912		}
913	}
914	/* The ones that are still referenced need to stay in the CLIP table */
915	TAILQ_CONCAT(&td->clip_table, &stale, link);
916
917	td->clip_gen = gen;
918done:
919	mtx_unlock(&td->clip_table_lock);
920	IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
921}
922
923static void
924destroy_clip_table(struct adapter *sc, struct tom_data *td)
925{
926	struct clip_entry *ce, *ce_temp;
927
928	if (mtx_initialized(&td->clip_table_lock)) {
929		mtx_lock(&td->clip_table_lock);
930		TAILQ_FOREACH_SAFE(ce, &td->clip_table, link, ce_temp) {
931			KASSERT(ce->refcount == 0,
932			    ("%s: CLIP entry %p still in use (%d)", __func__,
933			    ce, ce->refcount));
934			TAILQ_REMOVE(&td->clip_table, ce, link);
935			delete_lip(sc, &ce->lip);
936			free(ce, M_CXGBE);
937		}
938		mtx_unlock(&td->clip_table_lock);
939		mtx_destroy(&td->clip_table_lock);
940	}
941}
942
943static void
944free_tom_data(struct adapter *sc, struct tom_data *td)
945{
946
947	ASSERT_SYNCHRONIZED_OP(sc);
948
949	KASSERT(TAILQ_EMPTY(&td->toep_list),
950	    ("%s: TOE PCB list is not empty.", __func__));
951	KASSERT(td->lctx_count == 0,
952	    ("%s: lctx hash table is not empty.", __func__));
953
954	t4_free_ppod_region(&td->pr);
955	destroy_clip_table(sc, td);
956
957	if (td->listen_mask != 0)
958		hashdestroy(td->listen_hash, M_CXGBE, td->listen_mask);
959
960	if (mtx_initialized(&td->unsent_wr_lock))
961		mtx_destroy(&td->unsent_wr_lock);
962	if (mtx_initialized(&td->lctx_hash_lock))
963		mtx_destroy(&td->lctx_hash_lock);
964	if (mtx_initialized(&td->toep_list_lock))
965		mtx_destroy(&td->toep_list_lock);
966
967	free_tid_tabs(&sc->tids);
968	free(td, M_CXGBE);
969}
970
971static void
972reclaim_wr_resources(void *arg, int count)
973{
974	struct tom_data *td = arg;
975	STAILQ_HEAD(, wrqe) twr_list = STAILQ_HEAD_INITIALIZER(twr_list);
976	struct cpl_act_open_req *cpl;
977	u_int opcode, atid;
978	struct wrqe *wr;
979	struct adapter *sc;
980
981	mtx_lock(&td->unsent_wr_lock);
982	STAILQ_SWAP(&td->unsent_wr_list, &twr_list, wrqe);
983	mtx_unlock(&td->unsent_wr_lock);
984
985	while ((wr = STAILQ_FIRST(&twr_list)) != NULL) {
986		STAILQ_REMOVE_HEAD(&twr_list, link);
987
988		cpl = wrtod(wr);
989		opcode = GET_OPCODE(cpl);
990
991		switch (opcode) {
992		case CPL_ACT_OPEN_REQ:
993		case CPL_ACT_OPEN_REQ6:
994			atid = G_TID_TID(be32toh(OPCODE_TID(cpl)));
995			sc = td_adapter(td);
996
997			CTR2(KTR_CXGBE, "%s: atid %u ", __func__, atid);
998			act_open_failure_cleanup(sc, atid, EHOSTUNREACH);
999			free(wr, M_CXGBE);
1000			break;
1001		default:
1002			log(LOG_ERR, "%s: leaked work request %p, wr_len %d, "
1003			    "opcode %x\n", __func__, wr, wr->wr_len, opcode);
1004			/* WR not freed here; go look at it with a debugger.  */
1005		}
1006	}
1007}
1008
1009/*
1010 * Ground control to Major TOM
1011 * Commencing countdown, engines on
1012 */
1013static int
1014t4_tom_activate(struct adapter *sc)
1015{
1016	struct tom_data *td;
1017	struct toedev *tod;
1018	struct vi_info *vi;
1019	struct sge_ofld_rxq *ofld_rxq;
1020	int i, j, rc, v;
1021
1022	ASSERT_SYNCHRONIZED_OP(sc);
1023
1024	/* per-adapter softc for TOM */
1025	td = malloc(sizeof(*td), M_CXGBE, M_ZERO | M_NOWAIT);
1026	if (td == NULL)
1027		return (ENOMEM);
1028
1029	/* List of TOE PCBs and associated lock */
1030	mtx_init(&td->toep_list_lock, "PCB list lock", NULL, MTX_DEF);
1031	TAILQ_INIT(&td->toep_list);
1032
1033	/* Listen context */
1034	mtx_init(&td->lctx_hash_lock, "lctx hash lock", NULL, MTX_DEF);
1035	td->listen_hash = hashinit_flags(LISTEN_HASH_SIZE, M_CXGBE,
1036	    &td->listen_mask, HASH_NOWAIT);
1037
1038	/* List of WRs for which L2 resolution failed */
1039	mtx_init(&td->unsent_wr_lock, "Unsent WR list lock", NULL, MTX_DEF);
1040	STAILQ_INIT(&td->unsent_wr_list);
1041	TASK_INIT(&td->reclaim_wr_resources, 0, reclaim_wr_resources, td);
1042
1043	/* TID tables */
1044	rc = alloc_tid_tabs(&sc->tids);
1045	if (rc != 0)
1046		goto done;
1047
1048	rc = t4_init_ppod_region(&td->pr, &sc->vres.ddp,
1049	    t4_read_reg(sc, A_ULP_RX_TDDP_PSZ), "TDDP page pods");
1050	if (rc != 0)
1051		goto done;
1052	t4_set_reg_field(sc, A_ULP_RX_TDDP_TAGMASK,
1053	    V_TDDPTAGMASK(M_TDDPTAGMASK), td->pr.pr_tag_mask);
1054
1055	/* CLIP table for IPv6 offload */
1056	init_clip_table(sc, td);
1057
1058	/* toedev ops */
1059	tod = &td->tod;
1060	init_toedev(tod);
1061	tod->tod_softc = sc;
1062	tod->tod_connect = t4_connect;
1063	tod->tod_listen_start = t4_listen_start;
1064	tod->tod_listen_stop = t4_listen_stop;
1065	tod->tod_rcvd = t4_rcvd;
1066	tod->tod_output = t4_tod_output;
1067	tod->tod_send_rst = t4_send_rst;
1068	tod->tod_send_fin = t4_send_fin;
1069	tod->tod_pcb_detach = t4_pcb_detach;
1070	tod->tod_l2_update = t4_l2_update;
1071	tod->tod_syncache_added = t4_syncache_added;
1072	tod->tod_syncache_removed = t4_syncache_removed;
1073	tod->tod_syncache_respond = t4_syncache_respond;
1074	tod->tod_offload_socket = t4_offload_socket;
1075	tod->tod_ctloutput = t4_ctloutput;
1076
1077	for_each_port(sc, i) {
1078		for_each_vi(sc->port[i], v, vi) {
1079			TOEDEV(vi->ifp) = &td->tod;
1080			for_each_ofld_rxq(vi, j, ofld_rxq) {
1081				ofld_rxq->iq.set_tcb_rpl = do_set_tcb_rpl;
1082				ofld_rxq->iq.l2t_write_rpl = do_l2t_write_rpl2;
1083			}
1084		}
1085	}
1086
1087	sc->tom_softc = td;
1088	register_toedev(sc->tom_softc);
1089
1090done:
1091	if (rc != 0)
1092		free_tom_data(sc, td);
1093	return (rc);
1094}
1095
1096static int
1097t4_tom_deactivate(struct adapter *sc)
1098{
1099	int rc = 0;
1100	struct tom_data *td = sc->tom_softc;
1101
1102	ASSERT_SYNCHRONIZED_OP(sc);
1103
1104	if (td == NULL)
1105		return (0);	/* XXX. KASSERT? */
1106
1107	if (sc->offload_map != 0)
1108		return (EBUSY);	/* at least one port has IFCAP_TOE enabled */
1109
1110	if (uld_active(sc, ULD_IWARP) || uld_active(sc, ULD_ISCSI))
1111		return (EBUSY);	/* both iWARP and iSCSI rely on the TOE. */
1112
1113	mtx_lock(&td->toep_list_lock);
1114	if (!TAILQ_EMPTY(&td->toep_list))
1115		rc = EBUSY;
1116	mtx_unlock(&td->toep_list_lock);
1117
1118	mtx_lock(&td->lctx_hash_lock);
1119	if (td->lctx_count > 0)
1120		rc = EBUSY;
1121	mtx_unlock(&td->lctx_hash_lock);
1122
1123	taskqueue_drain(taskqueue_thread, &td->reclaim_wr_resources);
1124	mtx_lock(&td->unsent_wr_lock);
1125	if (!STAILQ_EMPTY(&td->unsent_wr_list))
1126		rc = EBUSY;
1127	mtx_unlock(&td->unsent_wr_lock);
1128
1129	if (rc == 0) {
1130		unregister_toedev(sc->tom_softc);
1131		free_tom_data(sc, td);
1132		sc->tom_softc = NULL;
1133	}
1134
1135	return (rc);
1136}
1137
1138static void
1139t4_tom_ifaddr_event(void *arg __unused, struct ifnet *ifp)
1140{
1141
1142	atomic_add_rel_int(&in6_ifaddr_gen, 1);
1143	taskqueue_enqueue_timeout(taskqueue_thread, &clip_task, -hz / 4);
1144}
1145
1146static int
1147t4_aio_queue_tom(struct socket *so, struct kaiocb *job)
1148{
1149	struct tcpcb *tp = so_sototcpcb(so);
1150	struct toepcb *toep = tp->t_toe;
1151	int error;
1152
1153	if (toep->ulp_mode == ULP_MODE_TCPDDP) {
1154		error = t4_aio_queue_ddp(so, job);
1155		if (error != EOPNOTSUPP)
1156			return (error);
1157	}
1158
1159	return (t4_aio_queue_aiotx(so, job));
1160}
1161
1162static int
1163t4_tom_mod_load(void)
1164{
1165	int rc;
1166	struct protosw *tcp_protosw, *tcp6_protosw;
1167
1168	/* CPL handlers */
1169	t4_init_connect_cpl_handlers();
1170	t4_init_listen_cpl_handlers();
1171	t4_init_cpl_io_handlers();
1172
1173	rc = t4_ddp_mod_load();
1174	if (rc != 0)
1175		return (rc);
1176
1177	tcp_protosw = pffindproto(PF_INET, IPPROTO_TCP, SOCK_STREAM);
1178	if (tcp_protosw == NULL)
1179		return (ENOPROTOOPT);
1180	bcopy(tcp_protosw, &toe_protosw, sizeof(toe_protosw));
1181	bcopy(tcp_protosw->pr_usrreqs, &toe_usrreqs, sizeof(toe_usrreqs));
1182	toe_usrreqs.pru_aio_queue = t4_aio_queue_tom;
1183	toe_protosw.pr_usrreqs = &toe_usrreqs;
1184
1185	tcp6_protosw = pffindproto(PF_INET6, IPPROTO_TCP, SOCK_STREAM);
1186	if (tcp6_protosw == NULL)
1187		return (ENOPROTOOPT);
1188	bcopy(tcp6_protosw, &toe6_protosw, sizeof(toe6_protosw));
1189	bcopy(tcp6_protosw->pr_usrreqs, &toe6_usrreqs, sizeof(toe6_usrreqs));
1190	toe6_usrreqs.pru_aio_queue = t4_aio_queue_tom;
1191	toe6_protosw.pr_usrreqs = &toe6_usrreqs;
1192
1193	TIMEOUT_TASK_INIT(taskqueue_thread, &clip_task, 0, t4_clip_task, NULL);
1194	ifaddr_evhandler = EVENTHANDLER_REGISTER(ifaddr_event,
1195	    t4_tom_ifaddr_event, NULL, EVENTHANDLER_PRI_ANY);
1196
1197	rc = t4_register_uld(&tom_uld_info);
1198	if (rc != 0)
1199		t4_tom_mod_unload();
1200
1201	return (rc);
1202}
1203
1204static void
1205tom_uninit(struct adapter *sc, void *arg __unused)
1206{
1207	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4tomun"))
1208		return;
1209
1210	/* Try to free resources (works only if no port has IFCAP_TOE) */
1211	if (uld_active(sc, ULD_TOM))
1212		t4_deactivate_uld(sc, ULD_TOM);
1213
1214	end_synchronized_op(sc, 0);
1215}
1216
1217static int
1218t4_tom_mod_unload(void)
1219{
1220	t4_iterate(tom_uninit, NULL);
1221
1222	if (t4_unregister_uld(&tom_uld_info) == EBUSY)
1223		return (EBUSY);
1224
1225	if (ifaddr_evhandler) {
1226		EVENTHANDLER_DEREGISTER(ifaddr_event, ifaddr_evhandler);
1227		taskqueue_cancel_timeout(taskqueue_thread, &clip_task, NULL);
1228	}
1229
1230	t4_ddp_mod_unload();
1231
1232	t4_uninit_connect_cpl_handlers();
1233	t4_uninit_listen_cpl_handlers();
1234	t4_uninit_cpl_io_handlers();
1235
1236	return (0);
1237}
1238#endif	/* TCP_OFFLOAD */
1239
1240static int
1241t4_tom_modevent(module_t mod, int cmd, void *arg)
1242{
1243	int rc = 0;
1244
1245#ifdef TCP_OFFLOAD
1246	switch (cmd) {
1247	case MOD_LOAD:
1248		rc = t4_tom_mod_load();
1249		break;
1250
1251	case MOD_UNLOAD:
1252		rc = t4_tom_mod_unload();
1253		break;
1254
1255	default:
1256		rc = EINVAL;
1257	}
1258#else
1259	printf("t4_tom: compiled without TCP_OFFLOAD support.\n");
1260	rc = EOPNOTSUPP;
1261#endif
1262	return (rc);
1263}
1264
1265static moduledata_t t4_tom_moddata= {
1266	"t4_tom",
1267	t4_tom_modevent,
1268	0
1269};
1270
1271MODULE_VERSION(t4_tom, 1);
1272MODULE_DEPEND(t4_tom, toecore, 1, 1, 1);
1273MODULE_DEPEND(t4_tom, t4nex, 1, 1, 1);
1274DECLARE_MODULE(t4_tom, t4_tom_moddata, SI_SUB_EXEC, SI_ORDER_ANY);
1275