t4_tom.c revision 346850
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: stable/11/sys/dev/cxgbe/tom/t4_tom.c 346850 2019-04-28 18:44:29Z np $");
30
31#include "opt_inet.h"
32#include "opt_inet6.h"
33
34#include <sys/param.h>
35#include <sys/types.h>
36#include <sys/systm.h>
37#include <sys/kernel.h>
38#include <sys/ktr.h>
39#include <sys/lock.h>
40#include <sys/limits.h>
41#include <sys/module.h>
42#include <sys/protosw.h>
43#include <sys/domain.h>
44#include <sys/refcount.h>
45#include <sys/rmlock.h>
46#include <sys/socket.h>
47#include <sys/socketvar.h>
48#include <sys/taskqueue.h>
49#include <net/if.h>
50#include <net/if_var.h>
51#include <net/if_types.h>
52#include <net/if_vlan_var.h>
53#include <netinet/in.h>
54#include <netinet/in_pcb.h>
55#include <netinet/in_var.h>
56#include <netinet/ip.h>
57#include <netinet/ip6.h>
58#include <netinet6/scope6_var.h>
59#define TCPSTATES
60#include <netinet/tcp_fsm.h>
61#include <netinet/tcp_timer.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#include "tom/t4_tls.h"
74
75static struct protosw toe_protosw;
76static struct pr_usrreqs toe_usrreqs;
77
78static struct protosw toe6_protosw;
79static struct pr_usrreqs toe6_usrreqs;
80
81/* Module ops */
82static int t4_tom_mod_load(void);
83static int t4_tom_mod_unload(void);
84static int t4_tom_modevent(module_t, int, void *);
85
86/* ULD ops and helpers */
87static int t4_tom_activate(struct adapter *);
88static int t4_tom_deactivate(struct adapter *);
89
90static struct uld_info tom_uld_info = {
91	.uld_id = ULD_TOM,
92	.activate = t4_tom_activate,
93	.deactivate = t4_tom_deactivate,
94};
95
96static void release_offload_resources(struct toepcb *);
97static int alloc_tid_tabs(struct tid_info *);
98static void free_tid_tabs(struct tid_info *);
99static int add_lip(struct adapter *, struct in6_addr *);
100static int delete_lip(struct adapter *, struct in6_addr *);
101static struct clip_entry *search_lip(struct tom_data *, struct in6_addr *);
102static void init_clip_table(struct adapter *, struct tom_data *);
103static void update_clip(struct adapter *, void *);
104static void t4_clip_task(void *, int);
105static void update_clip_table(struct adapter *, struct tom_data *);
106static void destroy_clip_table(struct adapter *, struct tom_data *);
107static void free_tom_data(struct adapter *, struct tom_data *);
108static void reclaim_wr_resources(void *, int);
109
110static int in6_ifaddr_gen;
111static eventhandler_tag ifaddr_evhandler;
112static struct timeout_task clip_task;
113
114struct toepcb *
115alloc_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	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	KASSERT(rxqid >= vi->first_ofld_rxq &&
144	    rxqid < vi->first_ofld_rxq + vi->nofldrxq,
145	    ("%s: rxqid %d for vi %p (first %d, n %d)", __func__, rxqid, vi,
146		vi->first_ofld_rxq, vi->nofldrxq));
147
148	len = offsetof(struct toepcb, txsd) +
149	    txsd_total * sizeof(struct ofld_tx_sdesc);
150
151	toep = malloc(len, M_CXGBE, M_ZERO | flags);
152	if (toep == NULL)
153		return (NULL);
154
155	refcount_init(&toep->refcount, 1);
156	toep->td = sc->tom_softc;
157	toep->vi = vi;
158	toep->tc_idx = -1;
159	toep->tx_total = tx_credits;
160	toep->tx_credits = tx_credits;
161	toep->ofld_txq = &sc->sge.ofld_txq[txqid];
162	toep->ofld_rxq = &sc->sge.ofld_rxq[rxqid];
163	toep->ctrlq = &sc->sge.ctrlq[pi->port_id];
164	mbufq_init(&toep->ulp_pduq, INT_MAX);
165	mbufq_init(&toep->ulp_pdu_reclaimq, INT_MAX);
166	toep->txsd_total = txsd_total;
167	toep->txsd_avail = txsd_total;
168	toep->txsd_pidx = 0;
169	toep->txsd_cidx = 0;
170	aiotx_init_toep(toep);
171
172	return (toep);
173}
174
175struct toepcb *
176hold_toepcb(struct toepcb *toep)
177{
178
179	refcount_acquire(&toep->refcount);
180	return (toep);
181}
182
183void
184free_toepcb(struct toepcb *toep)
185{
186
187	if (refcount_release(&toep->refcount) == 0)
188		return;
189
190	KASSERT(!(toep->flags & TPF_ATTACHED),
191	    ("%s: attached to an inpcb", __func__));
192	KASSERT(!(toep->flags & TPF_CPL_PENDING),
193	    ("%s: CPL pending", __func__));
194
195	if (toep->ulp_mode == ULP_MODE_TCPDDP)
196		ddp_uninit_toep(toep);
197	tls_uninit_toep(toep);
198	free(toep, M_CXGBE);
199}
200
201/*
202 * Set up the socket for TCP offload.
203 */
204void
205offload_socket(struct socket *so, struct toepcb *toep)
206{
207	struct tom_data *td = toep->td;
208	struct inpcb *inp = sotoinpcb(so);
209	struct tcpcb *tp = intotcpcb(inp);
210	struct sockbuf *sb;
211
212	INP_WLOCK_ASSERT(inp);
213
214	/* Update socket */
215	sb = &so->so_snd;
216	SOCKBUF_LOCK(sb);
217	sb->sb_flags |= SB_NOCOALESCE;
218	SOCKBUF_UNLOCK(sb);
219	sb = &so->so_rcv;
220	SOCKBUF_LOCK(sb);
221	sb->sb_flags |= SB_NOCOALESCE;
222	if (inp->inp_vflag & INP_IPV6)
223		so->so_proto = &toe6_protosw;
224	else
225		so->so_proto = &toe_protosw;
226	SOCKBUF_UNLOCK(sb);
227
228	/* Update TCP PCB */
229	tp->tod = &td->tod;
230	tp->t_toe = toep;
231	tp->t_flags |= TF_TOE;
232
233	/* Install an extra hold on inp */
234	toep->inp = inp;
235	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#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 */
336static void
337t4_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 */
374static void
375t4_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, W_TCB_T_FLAGS,
390		    V_TF_NAGLE(1), V_TF_NAGLE(tp->t_flags & TF_NODELAY ? 0 : 1),
391		    0, 0);
392		break;
393	default:
394		break;
395	}
396}
397
398static inline int
399get_tcb_bit(u_char *tcb, int bit)
400{
401	int ix, shift;
402
403	ix = 127 - (bit >> 3);
404	shift = bit & 0x7;
405
406	return ((tcb[ix] >> shift) & 1);
407}
408
409static inline uint64_t
410get_tcb_bits(u_char *tcb, int hi, int lo)
411{
412	uint64_t rc = 0;
413
414	while (hi >= lo) {
415		rc = (rc << 1) | get_tcb_bit(tcb, hi);
416		--hi;
417	}
418
419	return (rc);
420}
421
422/*
423 * Called by the kernel to allow the TOE driver to "refine" values filled up in
424 * the tcp_info for an offloaded connection.
425 */
426static void
427t4_tcp_info(struct toedev *tod, struct tcpcb *tp, struct tcp_info *ti)
428{
429	int i, j, k, rc;
430	struct adapter *sc = tod->tod_softc;
431	struct toepcb *toep = tp->t_toe;
432	uint32_t addr, v;
433	uint32_t buf[TCB_SIZE / sizeof(uint32_t)];
434	u_char *tcb, tmp;
435
436	INP_WLOCK_ASSERT(tp->t_inpcb);
437	MPASS(ti != NULL);
438
439	addr = t4_read_reg(sc, A_TP_CMM_TCB_BASE) + toep->tid * TCB_SIZE;
440	rc = read_via_memwin(sc, 2, addr, &buf[0], TCB_SIZE);
441	if (rc != 0)
442		return;
443
444	tcb = (u_char *)&buf[0];
445	for (i = 0, j = TCB_SIZE - 16; i < j; i += 16, j -= 16) {
446		for (k = 0; k < 16; k++) {
447			tmp = tcb[i + k];
448			tcb[i + k] = tcb[j + k];
449			tcb[j + k] = tmp;
450		}
451	}
452
453	ti->tcpi_state = get_tcb_bits(tcb, 115, 112);
454
455	v = get_tcb_bits(tcb, 271, 256);
456	ti->tcpi_rtt = tcp_ticks_to_us(sc, v);
457
458	v = get_tcb_bits(tcb, 287, 272);
459	ti->tcpi_rttvar = tcp_ticks_to_us(sc, v);
460
461	ti->tcpi_snd_ssthresh = get_tcb_bits(tcb, 487, 460);
462	ti->tcpi_snd_cwnd = get_tcb_bits(tcb, 459, 432);
463	ti->tcpi_rcv_nxt = get_tcb_bits(tcb, 553, 522);
464
465	ti->tcpi_snd_nxt = get_tcb_bits(tcb, 319, 288) -
466	    get_tcb_bits(tcb, 375, 348);
467
468	/* Receive window being advertised by us. */
469	ti->tcpi_rcv_space = get_tcb_bits(tcb, 581, 554);
470
471	/* Send window ceiling. */
472	v = get_tcb_bits(tcb, 159, 144) << get_tcb_bits(tcb, 131, 128);
473	ti->tcpi_snd_wnd = min(v, ti->tcpi_snd_cwnd);
474}
475
476/*
477 * The TOE driver will not receive any more CPLs for the tid associated with the
478 * toepcb; release the hold on the inpcb.
479 */
480void
481final_cpl_received(struct toepcb *toep)
482{
483	struct inpcb *inp = toep->inp;
484
485	KASSERT(inp != NULL, ("%s: inp is NULL", __func__));
486	INP_WLOCK_ASSERT(inp);
487	KASSERT(toep->flags & TPF_CPL_PENDING,
488	    ("%s: CPL not pending already?", __func__));
489
490	CTR6(KTR_CXGBE, "%s: tid %d, toep %p (0x%x), inp %p (0x%x)",
491	    __func__, toep->tid, toep, toep->flags, inp, inp->inp_flags);
492
493	if (toep->ulp_mode == ULP_MODE_TCPDDP)
494		release_ddp_resources(toep);
495	toep->inp = NULL;
496	toep->flags &= ~TPF_CPL_PENDING;
497	mbufq_drain(&toep->ulp_pdu_reclaimq);
498
499	if (!(toep->flags & TPF_ATTACHED))
500		release_offload_resources(toep);
501
502	if (!in_pcbrele_wlocked(inp))
503		INP_WUNLOCK(inp);
504}
505
506void
507insert_tid(struct adapter *sc, int tid, void *ctx, int ntids)
508{
509	struct tid_info *t = &sc->tids;
510
511	t->tid_tab[tid] = ctx;
512	atomic_add_int(&t->tids_in_use, ntids);
513}
514
515void *
516lookup_tid(struct adapter *sc, int tid)
517{
518	struct tid_info *t = &sc->tids;
519
520	return (t->tid_tab[tid]);
521}
522
523void
524update_tid(struct adapter *sc, int tid, void *ctx)
525{
526	struct tid_info *t = &sc->tids;
527
528	t->tid_tab[tid] = ctx;
529}
530
531void
532remove_tid(struct adapter *sc, int tid, int ntids)
533{
534	struct tid_info *t = &sc->tids;
535
536	t->tid_tab[tid] = NULL;
537	atomic_subtract_int(&t->tids_in_use, ntids);
538}
539
540/*
541 * What mtu_idx to use, given a 4-tuple.  Note that both s->mss and tcp_mssopt
542 * have the MSS that we should advertise in our SYN.  Advertised MSS doesn't
543 * account for any TCP options so the effective MSS (only payload, no headers or
544 * options) could be different.  We fill up tp->t_maxseg with the effective MSS
545 * at the end of the 3-way handshake.
546 */
547int
548find_best_mtu_idx(struct adapter *sc, struct in_conninfo *inc,
549    struct offload_settings *s)
550{
551	unsigned short *mtus = &sc->params.mtus[0];
552	int i, mss, mtu;
553
554	MPASS(inc != NULL);
555
556	mss = s->mss > 0 ? s->mss : tcp_mssopt(inc);
557	if (inc->inc_flags & INC_ISIPV6)
558		mtu = mss + sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
559	else
560		mtu = mss + sizeof(struct ip) + sizeof(struct tcphdr);
561
562	for (i = 0; i < NMTUS - 1 && mtus[i + 1] <= mtu; i++)
563		continue;
564
565	return (i);
566}
567
568/*
569 * Determine the receive window size for a socket.
570 */
571u_long
572select_rcv_wnd(struct socket *so)
573{
574	unsigned long wnd;
575
576	SOCKBUF_LOCK_ASSERT(&so->so_rcv);
577
578	wnd = sbspace(&so->so_rcv);
579	if (wnd < MIN_RCV_WND)
580		wnd = MIN_RCV_WND;
581
582	return min(wnd, MAX_RCV_WND);
583}
584
585int
586select_rcv_wscale(void)
587{
588	int wscale = 0;
589	unsigned long space = sb_max;
590
591	if (space > MAX_RCV_WND)
592		space = MAX_RCV_WND;
593
594	while (wscale < TCP_MAX_WINSHIFT && (TCP_MAXWIN << wscale) < space)
595		wscale++;
596
597	return (wscale);
598}
599
600/*
601 * socket so could be a listening socket too.
602 */
603uint64_t
604calc_opt0(struct socket *so, struct vi_info *vi, struct l2t_entry *e,
605    int mtu_idx, int rscale, int rx_credits, int ulp_mode,
606    struct offload_settings *s)
607{
608	int keepalive;
609	uint64_t opt0;
610
611	MPASS(so != NULL);
612	MPASS(vi != NULL);
613	KASSERT(rx_credits <= M_RCV_BUFSIZ,
614	    ("%s: rcv_bufsiz too high", __func__));
615
616	opt0 = F_TCAM_BYPASS | V_WND_SCALE(rscale) | V_MSS_IDX(mtu_idx) |
617	    V_ULP_MODE(ulp_mode) | V_RCV_BUFSIZ(rx_credits) |
618	    V_L2T_IDX(e->idx) | V_SMAC_SEL(vi->smt_idx) |
619	    V_TX_CHAN(vi->pi->tx_chan);
620
621	keepalive = tcp_always_keepalive || so_options_get(so) & SO_KEEPALIVE;
622	opt0 |= V_KEEP_ALIVE(keepalive != 0);
623
624	if (s->nagle < 0) {
625		struct inpcb *inp = sotoinpcb(so);
626		struct tcpcb *tp = intotcpcb(inp);
627
628		opt0 |= V_NAGLE((tp->t_flags & TF_NODELAY) == 0);
629	} else
630		opt0 |= V_NAGLE(s->nagle != 0);
631
632	return htobe64(opt0);
633}
634
635uint64_t
636select_ntuple(struct vi_info *vi, struct l2t_entry *e)
637{
638	struct adapter *sc = vi->pi->adapter;
639	struct tp_params *tp = &sc->params.tp;
640	uint16_t viid = vi->viid;
641	uint64_t ntuple = 0;
642
643	/*
644	 * Initialize each of the fields which we care about which are present
645	 * in the Compressed Filter Tuple.
646	 */
647	if (tp->vlan_shift >= 0 && e->vlan != CPL_L2T_VLAN_NONE)
648		ntuple |= (uint64_t)(F_FT_VLAN_VLD | e->vlan) << tp->vlan_shift;
649
650	if (tp->port_shift >= 0)
651		ntuple |= (uint64_t)e->lport << tp->port_shift;
652
653	if (tp->protocol_shift >= 0)
654		ntuple |= (uint64_t)IPPROTO_TCP << tp->protocol_shift;
655
656	if (tp->vnic_shift >= 0) {
657		uint32_t vf = G_FW_VIID_VIN(viid);
658		uint32_t pf = G_FW_VIID_PFN(viid);
659		uint32_t vld = G_FW_VIID_VIVLD(viid);
660
661		ntuple |= (uint64_t)(V_FT_VNID_ID_VF(vf) | V_FT_VNID_ID_PF(pf) |
662		    V_FT_VNID_ID_VLD(vld)) << tp->vnic_shift;
663	}
664
665	if (is_t4(sc))
666		return (htobe32((uint32_t)ntuple));
667	else
668		return (htobe64(V_FILTER_TUPLE(ntuple)));
669}
670
671static int
672is_tls_sock(struct socket *so, struct adapter *sc)
673{
674	struct inpcb *inp = sotoinpcb(so);
675	int i, rc;
676
677	/* XXX: Eventually add a SO_WANT_TLS socket option perhaps? */
678	rc = 0;
679	ADAPTER_LOCK(sc);
680	for (i = 0; i < sc->tt.num_tls_rx_ports; i++) {
681		if (inp->inp_lport == htons(sc->tt.tls_rx_ports[i]) ||
682		    inp->inp_fport == htons(sc->tt.tls_rx_ports[i])) {
683			rc = 1;
684			break;
685		}
686	}
687	ADAPTER_UNLOCK(sc);
688	return (rc);
689}
690
691int
692select_ulp_mode(struct socket *so, struct adapter *sc,
693    struct offload_settings *s)
694{
695
696	if (can_tls_offload(sc) &&
697	    (s->tls > 0 || (s->tls < 0 && is_tls_sock(so, sc))))
698		return (ULP_MODE_TLS);
699	else if (s->ddp > 0 ||
700	    (s->ddp < 0 && sc->tt.ddp && (so->so_options & SO_NO_DDP) == 0))
701		return (ULP_MODE_TCPDDP);
702	else
703		return (ULP_MODE_NONE);
704}
705
706void
707set_ulp_mode(struct toepcb *toep, int ulp_mode)
708{
709
710	CTR4(KTR_CXGBE, "%s: toep %p (tid %d) ulp_mode %d",
711	    __func__, toep, toep->tid, ulp_mode);
712	toep->ulp_mode = ulp_mode;
713	tls_init_toep(toep);
714	if (toep->ulp_mode == ULP_MODE_TCPDDP)
715		ddp_init_toep(toep);
716}
717
718int
719negative_advice(int status)
720{
721
722	return (status == CPL_ERR_RTX_NEG_ADVICE ||
723	    status == CPL_ERR_PERSIST_NEG_ADVICE ||
724	    status == CPL_ERR_KEEPALV_NEG_ADVICE);
725}
726
727static int
728alloc_tid_tab(struct tid_info *t, int flags)
729{
730
731	MPASS(t->ntids > 0);
732	MPASS(t->tid_tab == NULL);
733
734	t->tid_tab = malloc(t->ntids * sizeof(*t->tid_tab), M_CXGBE,
735	    M_ZERO | flags);
736	if (t->tid_tab == NULL)
737		return (ENOMEM);
738	atomic_store_rel_int(&t->tids_in_use, 0);
739
740	return (0);
741}
742
743static void
744free_tid_tab(struct tid_info *t)
745{
746
747	KASSERT(t->tids_in_use == 0,
748	    ("%s: %d tids still in use.", __func__, t->tids_in_use));
749
750	free(t->tid_tab, M_CXGBE);
751	t->tid_tab = NULL;
752}
753
754static int
755alloc_stid_tab(struct tid_info *t, int flags)
756{
757
758	MPASS(t->nstids > 0);
759	MPASS(t->stid_tab == NULL);
760
761	t->stid_tab = malloc(t->nstids * sizeof(*t->stid_tab), M_CXGBE,
762	    M_ZERO | flags);
763	if (t->stid_tab == NULL)
764		return (ENOMEM);
765	mtx_init(&t->stid_lock, "stid lock", NULL, MTX_DEF);
766	t->stids_in_use = 0;
767	TAILQ_INIT(&t->stids);
768	t->nstids_free_head = t->nstids;
769
770	return (0);
771}
772
773static void
774free_stid_tab(struct tid_info *t)
775{
776
777	KASSERT(t->stids_in_use == 0,
778	    ("%s: %d tids still in use.", __func__, t->stids_in_use));
779
780	if (mtx_initialized(&t->stid_lock))
781		mtx_destroy(&t->stid_lock);
782	free(t->stid_tab, M_CXGBE);
783	t->stid_tab = NULL;
784}
785
786static void
787free_tid_tabs(struct tid_info *t)
788{
789
790	free_tid_tab(t);
791	free_atid_tab(t);
792	free_stid_tab(t);
793}
794
795static int
796alloc_tid_tabs(struct tid_info *t)
797{
798	int rc;
799
800	rc = alloc_tid_tab(t, M_NOWAIT);
801	if (rc != 0)
802		goto failed;
803
804	rc = alloc_atid_tab(t, M_NOWAIT);
805	if (rc != 0)
806		goto failed;
807
808	rc = alloc_stid_tab(t, M_NOWAIT);
809	if (rc != 0)
810		goto failed;
811
812	return (0);
813failed:
814	free_tid_tabs(t);
815	return (rc);
816}
817
818static int
819add_lip(struct adapter *sc, struct in6_addr *lip)
820{
821        struct fw_clip_cmd c;
822
823	ASSERT_SYNCHRONIZED_OP(sc);
824	/* mtx_assert(&td->clip_table_lock, MA_OWNED); */
825
826        memset(&c, 0, sizeof(c));
827	c.op_to_write = htonl(V_FW_CMD_OP(FW_CLIP_CMD) | F_FW_CMD_REQUEST |
828	    F_FW_CMD_WRITE);
829        c.alloc_to_len16 = htonl(F_FW_CLIP_CMD_ALLOC | FW_LEN16(c));
830        c.ip_hi = *(uint64_t *)&lip->s6_addr[0];
831        c.ip_lo = *(uint64_t *)&lip->s6_addr[8];
832
833	return (-t4_wr_mbox_ns(sc, sc->mbox, &c, sizeof(c), &c));
834}
835
836static int
837delete_lip(struct adapter *sc, struct in6_addr *lip)
838{
839	struct fw_clip_cmd c;
840
841	ASSERT_SYNCHRONIZED_OP(sc);
842	/* mtx_assert(&td->clip_table_lock, MA_OWNED); */
843
844	memset(&c, 0, sizeof(c));
845	c.op_to_write = htonl(V_FW_CMD_OP(FW_CLIP_CMD) | F_FW_CMD_REQUEST |
846	    F_FW_CMD_READ);
847        c.alloc_to_len16 = htonl(F_FW_CLIP_CMD_FREE | FW_LEN16(c));
848        c.ip_hi = *(uint64_t *)&lip->s6_addr[0];
849        c.ip_lo = *(uint64_t *)&lip->s6_addr[8];
850
851	return (-t4_wr_mbox_ns(sc, sc->mbox, &c, sizeof(c), &c));
852}
853
854static struct clip_entry *
855search_lip(struct tom_data *td, struct in6_addr *lip)
856{
857	struct clip_entry *ce;
858
859	mtx_assert(&td->clip_table_lock, MA_OWNED);
860
861	TAILQ_FOREACH(ce, &td->clip_table, link) {
862		if (IN6_ARE_ADDR_EQUAL(&ce->lip, lip))
863			return (ce);
864	}
865
866	return (NULL);
867}
868
869struct clip_entry *
870hold_lip(struct tom_data *td, struct in6_addr *lip, struct clip_entry *ce)
871{
872
873	mtx_lock(&td->clip_table_lock);
874	if (ce == NULL)
875		ce = search_lip(td, lip);
876	if (ce != NULL)
877		ce->refcount++;
878	mtx_unlock(&td->clip_table_lock);
879
880	return (ce);
881}
882
883void
884release_lip(struct tom_data *td, struct clip_entry *ce)
885{
886
887	mtx_lock(&td->clip_table_lock);
888	KASSERT(search_lip(td, &ce->lip) == ce,
889	    ("%s: CLIP entry %p p not in CLIP table.", __func__, ce));
890	KASSERT(ce->refcount > 0,
891	    ("%s: CLIP entry %p has refcount 0", __func__, ce));
892	--ce->refcount;
893	mtx_unlock(&td->clip_table_lock);
894}
895
896static void
897init_clip_table(struct adapter *sc, struct tom_data *td)
898{
899
900	ASSERT_SYNCHRONIZED_OP(sc);
901
902	mtx_init(&td->clip_table_lock, "CLIP table lock", NULL, MTX_DEF);
903	TAILQ_INIT(&td->clip_table);
904	td->clip_gen = -1;
905
906	update_clip_table(sc, td);
907}
908
909static void
910update_clip(struct adapter *sc, void *arg __unused)
911{
912
913	if (begin_synchronized_op(sc, NULL, HOLD_LOCK, "t4tomuc"))
914		return;
915
916	if (uld_active(sc, ULD_TOM))
917		update_clip_table(sc, sc->tom_softc);
918
919	end_synchronized_op(sc, LOCK_HELD);
920}
921
922static void
923t4_clip_task(void *arg, int count)
924{
925
926	t4_iterate(update_clip, NULL);
927}
928
929static void
930update_clip_table(struct adapter *sc, struct tom_data *td)
931{
932	struct rm_priotracker in6_ifa_tracker;
933	struct in6_ifaddr *ia;
934	struct in6_addr *lip, tlip;
935	struct clip_head stale;
936	struct clip_entry *ce, *ce_temp;
937	struct vi_info *vi;
938	int rc, gen, i, j;
939	uintptr_t last_vnet;
940
941	ASSERT_SYNCHRONIZED_OP(sc);
942
943	IN6_IFADDR_RLOCK(&in6_ifa_tracker);
944	mtx_lock(&td->clip_table_lock);
945
946	gen = atomic_load_acq_int(&in6_ifaddr_gen);
947	if (gen == td->clip_gen)
948		goto done;
949
950	TAILQ_INIT(&stale);
951	TAILQ_CONCAT(&stale, &td->clip_table, link);
952
953	/*
954	 * last_vnet optimizes the common cases where all if_vnet = NULL (no
955	 * VIMAGE) or all if_vnet = vnet0.
956	 */
957	last_vnet = (uintptr_t)(-1);
958	for_each_port(sc, i)
959	for_each_vi(sc->port[i], j, vi) {
960		if (last_vnet == (uintptr_t)vi->ifp->if_vnet)
961			continue;
962
963		/* XXX: races with if_vmove */
964		CURVNET_SET(vi->ifp->if_vnet);
965		TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
966			lip = &ia->ia_addr.sin6_addr;
967
968			KASSERT(!IN6_IS_ADDR_MULTICAST(lip),
969			    ("%s: mcast address in in6_ifaddr list", __func__));
970
971			if (IN6_IS_ADDR_LOOPBACK(lip))
972				continue;
973			if (IN6_IS_SCOPE_EMBED(lip)) {
974				/* Remove the embedded scope */
975				tlip = *lip;
976				lip = &tlip;
977				in6_clearscope(lip);
978			}
979			/*
980			 * XXX: how to weed out the link local address for the
981			 * loopback interface?  It's fe80::1 usually (always?).
982			 */
983
984			/*
985			 * If it's in the main list then we already know it's
986			 * not stale.
987			 */
988			TAILQ_FOREACH(ce, &td->clip_table, link) {
989				if (IN6_ARE_ADDR_EQUAL(&ce->lip, lip))
990					goto next;
991			}
992
993			/*
994			 * If it's in the stale list we should move it to the
995			 * main list.
996			 */
997			TAILQ_FOREACH(ce, &stale, link) {
998				if (IN6_ARE_ADDR_EQUAL(&ce->lip, lip)) {
999					TAILQ_REMOVE(&stale, ce, link);
1000					TAILQ_INSERT_TAIL(&td->clip_table, ce,
1001					    link);
1002					goto next;
1003				}
1004			}
1005
1006			/* A new IP6 address; add it to the CLIP table */
1007			ce = malloc(sizeof(*ce), M_CXGBE, M_NOWAIT);
1008			memcpy(&ce->lip, lip, sizeof(ce->lip));
1009			ce->refcount = 0;
1010			rc = add_lip(sc, lip);
1011			if (rc == 0)
1012				TAILQ_INSERT_TAIL(&td->clip_table, ce, link);
1013			else {
1014				char ip[INET6_ADDRSTRLEN];
1015
1016				inet_ntop(AF_INET6, &ce->lip, &ip[0],
1017				    sizeof(ip));
1018				log(LOG_ERR, "%s: could not add %s (%d)\n",
1019				    __func__, ip, rc);
1020				free(ce, M_CXGBE);
1021			}
1022next:
1023			continue;
1024		}
1025		CURVNET_RESTORE();
1026		last_vnet = (uintptr_t)vi->ifp->if_vnet;
1027	}
1028
1029	/*
1030	 * Remove stale addresses (those no longer in V_in6_ifaddrhead) that are
1031	 * no longer referenced by the driver.
1032	 */
1033	TAILQ_FOREACH_SAFE(ce, &stale, link, ce_temp) {
1034		if (ce->refcount == 0) {
1035			rc = delete_lip(sc, &ce->lip);
1036			if (rc == 0) {
1037				TAILQ_REMOVE(&stale, ce, link);
1038				free(ce, M_CXGBE);
1039			} else {
1040				char ip[INET6_ADDRSTRLEN];
1041
1042				inet_ntop(AF_INET6, &ce->lip, &ip[0],
1043				    sizeof(ip));
1044				log(LOG_ERR, "%s: could not delete %s (%d)\n",
1045				    __func__, ip, rc);
1046			}
1047		}
1048	}
1049	/* The ones that are still referenced need to stay in the CLIP table */
1050	TAILQ_CONCAT(&td->clip_table, &stale, link);
1051
1052	td->clip_gen = gen;
1053done:
1054	mtx_unlock(&td->clip_table_lock);
1055	IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1056}
1057
1058static void
1059destroy_clip_table(struct adapter *sc, struct tom_data *td)
1060{
1061	struct clip_entry *ce, *ce_temp;
1062
1063	if (mtx_initialized(&td->clip_table_lock)) {
1064		mtx_lock(&td->clip_table_lock);
1065		TAILQ_FOREACH_SAFE(ce, &td->clip_table, link, ce_temp) {
1066			KASSERT(ce->refcount == 0,
1067			    ("%s: CLIP entry %p still in use (%d)", __func__,
1068			    ce, ce->refcount));
1069			TAILQ_REMOVE(&td->clip_table, ce, link);
1070			delete_lip(sc, &ce->lip);
1071			free(ce, M_CXGBE);
1072		}
1073		mtx_unlock(&td->clip_table_lock);
1074		mtx_destroy(&td->clip_table_lock);
1075	}
1076}
1077
1078static void
1079free_tom_data(struct adapter *sc, struct tom_data *td)
1080{
1081
1082	ASSERT_SYNCHRONIZED_OP(sc);
1083
1084	KASSERT(TAILQ_EMPTY(&td->toep_list),
1085	    ("%s: TOE PCB list is not empty.", __func__));
1086	KASSERT(td->lctx_count == 0,
1087	    ("%s: lctx hash table is not empty.", __func__));
1088
1089	t4_free_ppod_region(&td->pr);
1090	destroy_clip_table(sc, td);
1091
1092	if (td->listen_mask != 0)
1093		hashdestroy(td->listen_hash, M_CXGBE, td->listen_mask);
1094
1095	if (mtx_initialized(&td->unsent_wr_lock))
1096		mtx_destroy(&td->unsent_wr_lock);
1097	if (mtx_initialized(&td->lctx_hash_lock))
1098		mtx_destroy(&td->lctx_hash_lock);
1099	if (mtx_initialized(&td->toep_list_lock))
1100		mtx_destroy(&td->toep_list_lock);
1101
1102	free_tid_tabs(&sc->tids);
1103	free(td, M_CXGBE);
1104}
1105
1106static char *
1107prepare_pkt(int open_type, uint16_t vtag, struct inpcb *inp, int *pktlen,
1108    int *buflen)
1109{
1110	char *pkt;
1111	struct tcphdr *th;
1112	int ipv6, len;
1113	const int maxlen =
1114	    max(sizeof(struct ether_header), sizeof(struct ether_vlan_header)) +
1115	    max(sizeof(struct ip), sizeof(struct ip6_hdr)) +
1116	    sizeof(struct tcphdr);
1117
1118	MPASS(open_type == OPEN_TYPE_ACTIVE || open_type == OPEN_TYPE_LISTEN);
1119
1120	pkt = malloc(maxlen, M_CXGBE, M_ZERO | M_NOWAIT);
1121	if (pkt == NULL)
1122		return (NULL);
1123
1124	ipv6 = inp->inp_vflag & INP_IPV6;
1125	len = 0;
1126
1127	if (vtag == 0xffff) {
1128		struct ether_header *eh = (void *)pkt;
1129
1130		if (ipv6)
1131			eh->ether_type = htons(ETHERTYPE_IPV6);
1132		else
1133			eh->ether_type = htons(ETHERTYPE_IP);
1134
1135		len += sizeof(*eh);
1136	} else {
1137		struct ether_vlan_header *evh = (void *)pkt;
1138
1139		evh->evl_encap_proto = htons(ETHERTYPE_VLAN);
1140		evh->evl_tag = htons(vtag);
1141		if (ipv6)
1142			evh->evl_proto = htons(ETHERTYPE_IPV6);
1143		else
1144			evh->evl_proto = htons(ETHERTYPE_IP);
1145
1146		len += sizeof(*evh);
1147	}
1148
1149	if (ipv6) {
1150		struct ip6_hdr *ip6 = (void *)&pkt[len];
1151
1152		ip6->ip6_vfc = IPV6_VERSION;
1153		ip6->ip6_plen = htons(sizeof(struct tcphdr));
1154		ip6->ip6_nxt = IPPROTO_TCP;
1155		if (open_type == OPEN_TYPE_ACTIVE) {
1156			ip6->ip6_src = inp->in6p_laddr;
1157			ip6->ip6_dst = inp->in6p_faddr;
1158		} else if (open_type == OPEN_TYPE_LISTEN) {
1159			ip6->ip6_src = inp->in6p_laddr;
1160			ip6->ip6_dst = ip6->ip6_src;
1161		}
1162
1163		len += sizeof(*ip6);
1164	} else {
1165		struct ip *ip = (void *)&pkt[len];
1166
1167		ip->ip_v = IPVERSION;
1168		ip->ip_hl = sizeof(*ip) >> 2;
1169		ip->ip_tos = inp->inp_ip_tos;
1170		ip->ip_len = htons(sizeof(struct ip) + sizeof(struct tcphdr));
1171		ip->ip_ttl = inp->inp_ip_ttl;
1172		ip->ip_p = IPPROTO_TCP;
1173		if (open_type == OPEN_TYPE_ACTIVE) {
1174			ip->ip_src = inp->inp_laddr;
1175			ip->ip_dst = inp->inp_faddr;
1176		} else if (open_type == OPEN_TYPE_LISTEN) {
1177			ip->ip_src = inp->inp_laddr;
1178			ip->ip_dst = ip->ip_src;
1179		}
1180
1181		len += sizeof(*ip);
1182	}
1183
1184	th = (void *)&pkt[len];
1185	if (open_type == OPEN_TYPE_ACTIVE) {
1186		th->th_sport = inp->inp_lport;	/* network byte order already */
1187		th->th_dport = inp->inp_fport;	/* ditto */
1188	} else if (open_type == OPEN_TYPE_LISTEN) {
1189		th->th_sport = inp->inp_lport;	/* network byte order already */
1190		th->th_dport = th->th_sport;
1191	}
1192	len += sizeof(th);
1193
1194	*pktlen = *buflen = len;
1195	return (pkt);
1196}
1197
1198const struct offload_settings *
1199lookup_offload_policy(struct adapter *sc, int open_type, struct mbuf *m,
1200    uint16_t vtag, struct inpcb *inp)
1201{
1202	const struct t4_offload_policy *op;
1203	char *pkt;
1204	struct offload_rule *r;
1205	int i, matched, pktlen, buflen;
1206	static const struct offload_settings allow_offloading_settings = {
1207		.offload = 1,
1208		.rx_coalesce = -1,
1209		.cong_algo = -1,
1210		.sched_class = -1,
1211		.tstamp = -1,
1212		.sack = -1,
1213		.nagle = -1,
1214		.ecn = -1,
1215		.ddp = -1,
1216		.tls = -1,
1217		.txq = -1,
1218		.rxq = -1,
1219		.mss = -1,
1220	};
1221	static const struct offload_settings disallow_offloading_settings = {
1222		.offload = 0,
1223		/* rest is irrelevant when offload is off. */
1224	};
1225
1226	rw_assert(&sc->policy_lock, RA_LOCKED);
1227
1228	/*
1229	 * If there's no Connection Offloading Policy attached to the device
1230	 * then we need to return a default static policy.  If
1231	 * "cop_managed_offloading" is true, then we need to disallow
1232	 * offloading until a COP is attached to the device.  Otherwise we
1233	 * allow offloading ...
1234	 */
1235	op = sc->policy;
1236	if (op == NULL) {
1237		if (sc->tt.cop_managed_offloading)
1238			return (&disallow_offloading_settings);
1239		else
1240			return (&allow_offloading_settings);
1241	}
1242
1243	switch (open_type) {
1244	case OPEN_TYPE_ACTIVE:
1245	case OPEN_TYPE_LISTEN:
1246		pkt = prepare_pkt(open_type, vtag, inp, &pktlen, &buflen);
1247		break;
1248	case OPEN_TYPE_PASSIVE:
1249		MPASS(m != NULL);
1250		pkt = mtod(m, char *);
1251		MPASS(*pkt == CPL_PASS_ACCEPT_REQ);
1252		pkt += sizeof(struct cpl_pass_accept_req);
1253		pktlen = m->m_pkthdr.len - sizeof(struct cpl_pass_accept_req);
1254		buflen = m->m_len - sizeof(struct cpl_pass_accept_req);
1255		break;
1256	default:
1257		MPASS(0);
1258		return (&disallow_offloading_settings);
1259	}
1260
1261	if (pkt == NULL || pktlen == 0 || buflen == 0)
1262		return (&disallow_offloading_settings);
1263
1264	r = &op->rule[0];
1265	for (i = 0; i < op->nrules; i++, r++) {
1266		if (r->open_type != open_type &&
1267		    r->open_type != OPEN_TYPE_DONTCARE) {
1268			continue;
1269		}
1270		matched = bpf_filter(r->bpf_prog.bf_insns, pkt, pktlen, buflen);
1271		if (matched)
1272			break;
1273	}
1274
1275	if (open_type == OPEN_TYPE_ACTIVE || open_type == OPEN_TYPE_LISTEN)
1276		free(pkt, M_CXGBE);
1277
1278	return (matched ? &r->settings : &disallow_offloading_settings);
1279}
1280
1281static void
1282reclaim_wr_resources(void *arg, int count)
1283{
1284	struct tom_data *td = arg;
1285	STAILQ_HEAD(, wrqe) twr_list = STAILQ_HEAD_INITIALIZER(twr_list);
1286	struct cpl_act_open_req *cpl;
1287	u_int opcode, atid;
1288	struct wrqe *wr;
1289	struct adapter *sc;
1290
1291	mtx_lock(&td->unsent_wr_lock);
1292	STAILQ_SWAP(&td->unsent_wr_list, &twr_list, wrqe);
1293	mtx_unlock(&td->unsent_wr_lock);
1294
1295	while ((wr = STAILQ_FIRST(&twr_list)) != NULL) {
1296		STAILQ_REMOVE_HEAD(&twr_list, link);
1297
1298		cpl = wrtod(wr);
1299		opcode = GET_OPCODE(cpl);
1300
1301		switch (opcode) {
1302		case CPL_ACT_OPEN_REQ:
1303		case CPL_ACT_OPEN_REQ6:
1304			atid = G_TID_TID(be32toh(OPCODE_TID(cpl)));
1305			sc = td_adapter(td);
1306
1307			CTR2(KTR_CXGBE, "%s: atid %u ", __func__, atid);
1308			act_open_failure_cleanup(sc, atid, EHOSTUNREACH);
1309			free(wr, M_CXGBE);
1310			break;
1311		default:
1312			log(LOG_ERR, "%s: leaked work request %p, wr_len %d, "
1313			    "opcode %x\n", __func__, wr, wr->wr_len, opcode);
1314			/* WR not freed here; go look at it with a debugger.  */
1315		}
1316	}
1317}
1318
1319/*
1320 * Ground control to Major TOM
1321 * Commencing countdown, engines on
1322 */
1323static int
1324t4_tom_activate(struct adapter *sc)
1325{
1326	struct tom_data *td;
1327	struct toedev *tod;
1328	struct vi_info *vi;
1329	struct sge_ofld_rxq *ofld_rxq;
1330	int i, j, rc, v;
1331
1332	ASSERT_SYNCHRONIZED_OP(sc);
1333
1334	/* per-adapter softc for TOM */
1335	td = malloc(sizeof(*td), M_CXGBE, M_ZERO | M_NOWAIT);
1336	if (td == NULL)
1337		return (ENOMEM);
1338
1339	/* List of TOE PCBs and associated lock */
1340	mtx_init(&td->toep_list_lock, "PCB list lock", NULL, MTX_DEF);
1341	TAILQ_INIT(&td->toep_list);
1342
1343	/* Listen context */
1344	mtx_init(&td->lctx_hash_lock, "lctx hash lock", NULL, MTX_DEF);
1345	td->listen_hash = hashinit_flags(LISTEN_HASH_SIZE, M_CXGBE,
1346	    &td->listen_mask, HASH_NOWAIT);
1347
1348	/* List of WRs for which L2 resolution failed */
1349	mtx_init(&td->unsent_wr_lock, "Unsent WR list lock", NULL, MTX_DEF);
1350	STAILQ_INIT(&td->unsent_wr_list);
1351	TASK_INIT(&td->reclaim_wr_resources, 0, reclaim_wr_resources, td);
1352
1353	/* TID tables */
1354	rc = alloc_tid_tabs(&sc->tids);
1355	if (rc != 0)
1356		goto done;
1357
1358	rc = t4_init_ppod_region(&td->pr, &sc->vres.ddp,
1359	    t4_read_reg(sc, A_ULP_RX_TDDP_PSZ), "TDDP page pods");
1360	if (rc != 0)
1361		goto done;
1362	t4_set_reg_field(sc, A_ULP_RX_TDDP_TAGMASK,
1363	    V_TDDPTAGMASK(M_TDDPTAGMASK), td->pr.pr_tag_mask);
1364
1365	/* CLIP table for IPv6 offload */
1366	init_clip_table(sc, td);
1367
1368	/* toedev ops */
1369	tod = &td->tod;
1370	init_toedev(tod);
1371	tod->tod_softc = sc;
1372	tod->tod_connect = t4_connect;
1373	tod->tod_listen_start = t4_listen_start;
1374	tod->tod_listen_stop = t4_listen_stop;
1375	tod->tod_rcvd = t4_rcvd;
1376	tod->tod_output = t4_tod_output;
1377	tod->tod_send_rst = t4_send_rst;
1378	tod->tod_send_fin = t4_send_fin;
1379	tod->tod_pcb_detach = t4_pcb_detach;
1380	tod->tod_l2_update = t4_l2_update;
1381	tod->tod_syncache_added = t4_syncache_added;
1382	tod->tod_syncache_removed = t4_syncache_removed;
1383	tod->tod_syncache_respond = t4_syncache_respond;
1384	tod->tod_offload_socket = t4_offload_socket;
1385	tod->tod_ctloutput = t4_ctloutput;
1386#if 0
1387	tod->tod_tcp_info = t4_tcp_info;
1388#else
1389	(void)&t4_tcp_info;
1390#endif
1391
1392	for_each_port(sc, i) {
1393		for_each_vi(sc->port[i], v, vi) {
1394			TOEDEV(vi->ifp) = &td->tod;
1395			for_each_ofld_rxq(vi, j, ofld_rxq) {
1396				ofld_rxq->iq.set_tcb_rpl = do_set_tcb_rpl;
1397				ofld_rxq->iq.l2t_write_rpl = do_l2t_write_rpl2;
1398			}
1399		}
1400	}
1401
1402	sc->tom_softc = td;
1403	register_toedev(sc->tom_softc);
1404
1405done:
1406	if (rc != 0)
1407		free_tom_data(sc, td);
1408	return (rc);
1409}
1410
1411static int
1412t4_tom_deactivate(struct adapter *sc)
1413{
1414	int rc = 0;
1415	struct tom_data *td = sc->tom_softc;
1416
1417	ASSERT_SYNCHRONIZED_OP(sc);
1418
1419	if (td == NULL)
1420		return (0);	/* XXX. KASSERT? */
1421
1422	if (sc->offload_map != 0)
1423		return (EBUSY);	/* at least one port has IFCAP_TOE enabled */
1424
1425	if (uld_active(sc, ULD_IWARP) || uld_active(sc, ULD_ISCSI))
1426		return (EBUSY);	/* both iWARP and iSCSI rely on the TOE. */
1427
1428	mtx_lock(&td->toep_list_lock);
1429	if (!TAILQ_EMPTY(&td->toep_list))
1430		rc = EBUSY;
1431	mtx_unlock(&td->toep_list_lock);
1432
1433	mtx_lock(&td->lctx_hash_lock);
1434	if (td->lctx_count > 0)
1435		rc = EBUSY;
1436	mtx_unlock(&td->lctx_hash_lock);
1437
1438	taskqueue_drain(taskqueue_thread, &td->reclaim_wr_resources);
1439	mtx_lock(&td->unsent_wr_lock);
1440	if (!STAILQ_EMPTY(&td->unsent_wr_list))
1441		rc = EBUSY;
1442	mtx_unlock(&td->unsent_wr_lock);
1443
1444	if (rc == 0) {
1445		unregister_toedev(sc->tom_softc);
1446		free_tom_data(sc, td);
1447		sc->tom_softc = NULL;
1448	}
1449
1450	return (rc);
1451}
1452
1453static void
1454t4_tom_ifaddr_event(void *arg __unused, struct ifnet *ifp)
1455{
1456
1457	atomic_add_rel_int(&in6_ifaddr_gen, 1);
1458	taskqueue_enqueue_timeout(taskqueue_thread, &clip_task, -hz / 4);
1459}
1460
1461static int
1462t4_aio_queue_tom(struct socket *so, struct kaiocb *job)
1463{
1464	struct tcpcb *tp = so_sototcpcb(so);
1465	struct toepcb *toep = tp->t_toe;
1466	int error;
1467
1468	if (toep->ulp_mode == ULP_MODE_TCPDDP) {
1469		error = t4_aio_queue_ddp(so, job);
1470		if (error != EOPNOTSUPP)
1471			return (error);
1472	}
1473
1474	return (t4_aio_queue_aiotx(so, job));
1475}
1476
1477static int
1478t4_ctloutput_tom(struct socket *so, struct sockopt *sopt)
1479{
1480
1481	if (sopt->sopt_level != IPPROTO_TCP)
1482		return (tcp_ctloutput(so, sopt));
1483
1484	switch (sopt->sopt_name) {
1485	case TCP_TLSOM_SET_TLS_CONTEXT:
1486	case TCP_TLSOM_GET_TLS_TOM:
1487	case TCP_TLSOM_CLR_TLS_TOM:
1488	case TCP_TLSOM_CLR_QUIES:
1489		return (t4_ctloutput_tls(so, sopt));
1490	default:
1491		return (tcp_ctloutput(so, sopt));
1492	}
1493}
1494
1495static int
1496t4_tom_mod_load(void)
1497{
1498	struct protosw *tcp_protosw, *tcp6_protosw;
1499
1500	/* CPL handlers */
1501	t4_init_connect_cpl_handlers();
1502	t4_init_listen_cpl_handlers();
1503	t4_init_cpl_io_handlers();
1504
1505	t4_ddp_mod_load();
1506	t4_tls_mod_load();
1507
1508	tcp_protosw = pffindproto(PF_INET, IPPROTO_TCP, SOCK_STREAM);
1509	if (tcp_protosw == NULL)
1510		return (ENOPROTOOPT);
1511	bcopy(tcp_protosw, &toe_protosw, sizeof(toe_protosw));
1512	bcopy(tcp_protosw->pr_usrreqs, &toe_usrreqs, sizeof(toe_usrreqs));
1513	toe_usrreqs.pru_aio_queue = t4_aio_queue_tom;
1514	toe_protosw.pr_ctloutput = t4_ctloutput_tom;
1515	toe_protosw.pr_usrreqs = &toe_usrreqs;
1516
1517	tcp6_protosw = pffindproto(PF_INET6, IPPROTO_TCP, SOCK_STREAM);
1518	if (tcp6_protosw == NULL)
1519		return (ENOPROTOOPT);
1520	bcopy(tcp6_protosw, &toe6_protosw, sizeof(toe6_protosw));
1521	bcopy(tcp6_protosw->pr_usrreqs, &toe6_usrreqs, sizeof(toe6_usrreqs));
1522	toe6_usrreqs.pru_aio_queue = t4_aio_queue_tom;
1523	toe6_protosw.pr_ctloutput = t4_ctloutput_tom;
1524	toe6_protosw.pr_usrreqs = &toe6_usrreqs;
1525
1526	TIMEOUT_TASK_INIT(taskqueue_thread, &clip_task, 0, t4_clip_task, NULL);
1527	ifaddr_evhandler = EVENTHANDLER_REGISTER(ifaddr_event,
1528	    t4_tom_ifaddr_event, NULL, EVENTHANDLER_PRI_ANY);
1529
1530	return (t4_register_uld(&tom_uld_info));
1531}
1532
1533static void
1534tom_uninit(struct adapter *sc, void *arg __unused)
1535{
1536	if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4tomun"))
1537		return;
1538
1539	/* Try to free resources (works only if no port has IFCAP_TOE) */
1540	if (uld_active(sc, ULD_TOM))
1541		t4_deactivate_uld(sc, ULD_TOM);
1542
1543	end_synchronized_op(sc, 0);
1544}
1545
1546static int
1547t4_tom_mod_unload(void)
1548{
1549	t4_iterate(tom_uninit, NULL);
1550
1551	if (t4_unregister_uld(&tom_uld_info) == EBUSY)
1552		return (EBUSY);
1553
1554	if (ifaddr_evhandler) {
1555		EVENTHANDLER_DEREGISTER(ifaddr_event, ifaddr_evhandler);
1556		taskqueue_cancel_timeout(taskqueue_thread, &clip_task, NULL);
1557	}
1558
1559	t4_tls_mod_unload();
1560	t4_ddp_mod_unload();
1561
1562	t4_uninit_connect_cpl_handlers();
1563	t4_uninit_listen_cpl_handlers();
1564	t4_uninit_cpl_io_handlers();
1565
1566	return (0);
1567}
1568#endif	/* TCP_OFFLOAD */
1569
1570static int
1571t4_tom_modevent(module_t mod, int cmd, void *arg)
1572{
1573	int rc = 0;
1574
1575#ifdef TCP_OFFLOAD
1576	switch (cmd) {
1577	case MOD_LOAD:
1578		rc = t4_tom_mod_load();
1579		break;
1580
1581	case MOD_UNLOAD:
1582		rc = t4_tom_mod_unload();
1583		break;
1584
1585	default:
1586		rc = EINVAL;
1587	}
1588#else
1589	printf("t4_tom: compiled without TCP_OFFLOAD support.\n");
1590	rc = EOPNOTSUPP;
1591#endif
1592	return (rc);
1593}
1594
1595static moduledata_t t4_tom_moddata= {
1596	"t4_tom",
1597	t4_tom_modevent,
1598	0
1599};
1600
1601MODULE_VERSION(t4_tom, 1);
1602MODULE_DEPEND(t4_tom, toecore, 1, 1, 1);
1603MODULE_DEPEND(t4_tom, t4nex, 1, 1, 1);
1604DECLARE_MODULE(t4_tom, t4_tom_moddata, SI_SUB_EXEC, SI_ORDER_ANY);
1605