2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 Chelsio Communications, Inc.
6 * Written by: Navdeep Parhar <np@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 #include "opt_inet6.h"
35 #include "opt_kern_tls.h"
36 #include "opt_ratelimit.h"
38 #include <sys/param.h>
39 #include <sys/types.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
44 #include <sys/limits.h>
45 #include <sys/module.h>
46 #include <sys/protosw.h>
47 #include <sys/domain.h>
48 #include <sys/refcount.h>
49 #include <sys/rmlock.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/sysctl.h>
53 #include <sys/taskqueue.h>
55 #include <net/if_var.h>
56 #include <net/if_types.h>
57 #include <net/if_vlan_var.h>
58 #include <netinet/in.h>
59 #include <netinet/in_pcb.h>
60 #include <netinet/in_var.h>
61 #include <netinet/ip.h>
62 #include <netinet/ip6.h>
63 #include <netinet6/scope6_var.h>
65 #include <netinet/tcp_fsm.h>
66 #include <netinet/tcp_timer.h>
67 #include <netinet/tcp_var.h>
68 #include <netinet/toecore.h>
69 #include <netinet/cc/cc.h>
72 #include "common/common.h"
73 #include "common/t4_msg.h"
74 #include "common/t4_regs.h"
75 #include "common/t4_regs_values.h"
76 #include "common/t4_tcb.h"
78 #include "tom/t4_tom_l2t.h"
79 #include "tom/t4_tom.h"
80 #include "tom/t4_tls.h"
82 static struct protosw toe_protosw;
83 static struct pr_usrreqs toe_usrreqs;
85 static struct protosw toe6_protosw;
86 static struct pr_usrreqs toe6_usrreqs;
89 static int t4_tom_mod_load(void);
90 static int t4_tom_mod_unload(void);
91 static int t4_tom_modevent(module_t, int, void *);
93 /* ULD ops and helpers */
94 static int t4_tom_activate(struct adapter *);
95 static int t4_tom_deactivate(struct adapter *);
97 static struct uld_info tom_uld_info = {
99 .activate = t4_tom_activate,
100 .deactivate = t4_tom_deactivate,
103 static void release_offload_resources(struct toepcb *);
104 static int alloc_tid_tabs(struct tid_info *);
105 static void free_tid_tabs(struct tid_info *);
106 static void free_tom_data(struct adapter *, struct tom_data *);
107 static void reclaim_wr_resources(void *, int);
110 alloc_toepcb(struct vi_info *vi, int flags)
112 struct port_info *pi = vi->pi;
113 struct adapter *sc = pi->adapter;
115 int tx_credits, txsd_total, len;
118 * The firmware counts tx work request credits in units of 16 bytes
119 * each. Reserve room for an ABORT_REQ so the driver never has to worry
120 * about tx credits if it wants to abort a connection.
122 tx_credits = sc->params.ofldq_wr_cred;
123 tx_credits -= howmany(sizeof(struct cpl_abort_req), 16);
126 * Shortest possible tx work request is a fw_ofld_tx_data_wr + 1 byte
127 * immediate payload, and firmware counts tx work request credits in
128 * units of 16 byte. Calculate the maximum work requests possible.
130 txsd_total = tx_credits /
131 howmany(sizeof(struct fw_ofld_tx_data_wr) + 1, 16);
133 len = offsetof(struct toepcb, txsd) +
134 txsd_total * sizeof(struct ofld_tx_sdesc);
136 toep = malloc(len, M_CXGBE, M_ZERO | flags);
140 refcount_init(&toep->refcount, 1);
141 toep->td = sc->tom_softc;
144 toep->tx_total = tx_credits;
145 toep->tx_credits = tx_credits;
146 mbufq_init(&toep->ulp_pduq, INT_MAX);
147 mbufq_init(&toep->ulp_pdu_reclaimq, INT_MAX);
148 toep->txsd_total = txsd_total;
149 toep->txsd_avail = txsd_total;
152 aiotx_init_toep(toep);
158 * Initialize a toepcb after its params have been filled out.
161 init_toepcb(struct vi_info *vi, struct toepcb *toep)
163 struct conn_params *cp = &toep->params;
164 struct port_info *pi = vi->pi;
165 struct adapter *sc = pi->adapter;
166 struct tx_cl_rl_params *tc;
168 if (cp->tc_idx >= 0 && cp->tc_idx < sc->chip_params->nsched_cls) {
169 tc = &pi->sched_params->cl_rl[cp->tc_idx];
170 mtx_lock(&sc->tc_lock);
171 if (tc->flags & CLRL_ERR) {
173 "%s: failed to associate traffic class %u with tid %u\n",
174 device_get_nameunit(vi->dev), cp->tc_idx,
180 mtx_unlock(&sc->tc_lock);
182 toep->ofld_txq = &sc->sge.ofld_txq[cp->txq_idx];
183 toep->ofld_rxq = &sc->sge.ofld_rxq[cp->rxq_idx];
184 toep->ctrlq = &sc->sge.ctrlq[pi->port_id];
187 if (ulp_mode(toep) == ULP_MODE_TCPDDP)
194 hold_toepcb(struct toepcb *toep)
197 refcount_acquire(&toep->refcount);
202 free_toepcb(struct toepcb *toep)
205 if (refcount_release(&toep->refcount) == 0)
208 KASSERT(!(toep->flags & TPF_ATTACHED),
209 ("%s: attached to an inpcb", __func__));
210 KASSERT(!(toep->flags & TPF_CPL_PENDING),
211 ("%s: CPL pending", __func__));
213 if (ulp_mode(toep) == ULP_MODE_TCPDDP)
214 ddp_uninit_toep(toep);
215 tls_uninit_toep(toep);
220 * Set up the socket for TCP offload.
223 offload_socket(struct socket *so, struct toepcb *toep)
225 struct tom_data *td = toep->td;
226 struct inpcb *inp = sotoinpcb(so);
227 struct tcpcb *tp = intotcpcb(inp);
230 INP_WLOCK_ASSERT(inp);
235 sb->sb_flags |= SB_NOCOALESCE;
239 sb->sb_flags |= SB_NOCOALESCE;
240 if (inp->inp_vflag & INP_IPV6)
241 so->so_proto = &toe6_protosw;
243 so->so_proto = &toe_protosw;
249 tp->t_flags |= TF_TOE;
251 /* Install an extra hold on inp */
253 toep->flags |= TPF_ATTACHED;
256 /* Add the TOE PCB to the active list */
257 mtx_lock(&td->toep_list_lock);
258 TAILQ_INSERT_HEAD(&td->toep_list, toep, link);
259 mtx_unlock(&td->toep_list_lock);
262 /* This is _not_ the normal way to "unoffload" a socket. */
264 undo_offload_socket(struct socket *so)
266 struct inpcb *inp = sotoinpcb(so);
267 struct tcpcb *tp = intotcpcb(inp);
268 struct toepcb *toep = tp->t_toe;
269 struct tom_data *td = toep->td;
272 INP_WLOCK_ASSERT(inp);
276 sb->sb_flags &= ~SB_NOCOALESCE;
280 sb->sb_flags &= ~SB_NOCOALESCE;
285 tp->t_flags &= ~TF_TOE;
288 toep->flags &= ~TPF_ATTACHED;
289 if (in_pcbrele_wlocked(inp))
290 panic("%s: inp freed.", __func__);
292 mtx_lock(&td->toep_list_lock);
293 TAILQ_REMOVE(&td->toep_list, toep, link);
294 mtx_unlock(&td->toep_list_lock);
298 release_offload_resources(struct toepcb *toep)
300 struct tom_data *td = toep->td;
301 struct adapter *sc = td_adapter(td);
304 KASSERT(!(toep->flags & TPF_CPL_PENDING),
305 ("%s: %p has CPL pending.", __func__, toep));
306 KASSERT(!(toep->flags & TPF_ATTACHED),
307 ("%s: %p is still attached.", __func__, toep));
309 CTR5(KTR_CXGBE, "%s: toep %p (tid %d, l2te %p, ce %p)",
310 __func__, toep, tid, toep->l2te, toep->ce);
313 * These queues should have been emptied at approximately the same time
314 * that a normal connection's socket's so_snd would have been purged or
315 * drained. Do _not_ clean up here.
317 MPASS(mbufq_len(&toep->ulp_pduq) == 0);
318 MPASS(mbufq_len(&toep->ulp_pdu_reclaimq) == 0);
320 if (ulp_mode(toep) == ULP_MODE_TCPDDP)
321 ddp_assert_empty(toep);
323 MPASS(TAILQ_EMPTY(&toep->aiotx_jobq));
326 t4_l2t_release(toep->l2te);
329 remove_tid(sc, tid, toep->ce ? 2 : 1);
330 release_tid(sc, tid, toep->ctrlq);
334 t4_release_lip(sc, toep->ce);
336 if (toep->params.tc_idx != -1)
337 t4_release_cl_rl(sc, toep->vi->pi->port_id, toep->params.tc_idx);
339 mtx_lock(&td->toep_list_lock);
340 TAILQ_REMOVE(&td->toep_list, toep, link);
341 mtx_unlock(&td->toep_list_lock);
347 * The kernel is done with the TCP PCB and this is our opportunity to unhook the
348 * toepcb hanging off of it. If the TOE driver is also done with the toepcb (no
349 * pending CPL) then it is time to release all resources tied to the toepcb.
351 * Also gets called when an offloaded active open fails and the TOM wants the
352 * kernel to take the TCP PCB back.
355 t4_pcb_detach(struct toedev *tod __unused, struct tcpcb *tp)
357 #if defined(KTR) || defined(INVARIANTS)
358 struct inpcb *inp = tp->t_inpcb;
360 struct toepcb *toep = tp->t_toe;
362 INP_WLOCK_ASSERT(inp);
364 KASSERT(toep != NULL, ("%s: toep is NULL", __func__));
365 KASSERT(toep->flags & TPF_ATTACHED,
366 ("%s: not attached", __func__));
369 if (tp->t_state == TCPS_SYN_SENT) {
370 CTR6(KTR_CXGBE, "%s: atid %d, toep %p (0x%x), inp %p (0x%x)",
371 __func__, toep->tid, toep, toep->flags, inp,
375 "t4_pcb_detach: tid %d (%s), toep %p (0x%x), inp %p (0x%x)",
376 toep->tid, tcpstates[tp->t_state], toep, toep->flags, inp,
382 tp->t_flags &= ~TF_TOE;
383 toep->flags &= ~TPF_ATTACHED;
385 if (!(toep->flags & TPF_CPL_PENDING))
386 release_offload_resources(toep);
390 * setsockopt handler.
393 t4_ctloutput(struct toedev *tod, struct tcpcb *tp, int dir, int name)
395 struct adapter *sc = tod->tod_softc;
396 struct toepcb *toep = tp->t_toe;
401 CTR4(KTR_CXGBE, "%s: tp %p, dir %u, name %u", __func__, tp, dir, name);
405 if (tp->t_state != TCPS_ESTABLISHED)
407 toep->params.nagle = tp->t_flags & TF_NODELAY ? 0 : 1;
408 t4_set_tcb_field(sc, toep->ctrlq, toep, W_TCB_T_FLAGS,
409 V_TF_NAGLE(1), V_TF_NAGLE(toep->params.nagle), 0, 0);
416 static inline uint64_t
417 get_tcb_tflags(const uint64_t *tcb)
420 return ((be64toh(tcb[14]) << 32) | (be64toh(tcb[15]) >> 32));
423 static inline uint32_t
424 get_tcb_field(const uint64_t *tcb, u_int word, uint32_t mask, u_int shift)
426 #define LAST_WORD ((TCB_SIZE / 4) - 1)
431 MPASS(word <= LAST_WORD);
434 flit_idx = (LAST_WORD - word) / 2;
437 t1 = be64toh(tcb[flit_idx]) >> shift;
439 if (fls(mask) > 64 - shift) {
441 * Will spill over into the next logical flit, which is the flit
442 * before this one. The flit_idx before this one must be valid.
445 t2 = be64toh(tcb[flit_idx - 1]) << (64 - shift);
447 return ((t2 | t1) & mask);
450 #define GET_TCB_FIELD(tcb, F) \
451 get_tcb_field(tcb, W_TCB_##F, M_TCB_##F, S_TCB_##F)
454 * Issues a CPL_GET_TCB to read the entire TCB for the tid.
457 send_get_tcb(struct adapter *sc, u_int tid)
459 struct cpl_get_tcb *cpl;
460 struct wrq_cookie cookie;
462 MPASS(tid < sc->tids.ntids);
464 cpl = start_wrq_wr(&sc->sge.ctrlq[0], howmany(sizeof(*cpl), 16),
466 if (__predict_false(cpl == NULL))
468 bzero(cpl, sizeof(*cpl));
469 INIT_TP_WR(cpl, tid);
470 OPCODE_TID(cpl) = htobe32(MK_OPCODE_TID(CPL_GET_TCB, tid));
471 cpl->reply_ctrl = htobe16(V_REPLY_CHAN(0) |
472 V_QUEUENO(sc->sge.ofld_rxq[0].iq.cntxt_id));
474 commit_wrq_wr(&sc->sge.ctrlq[0], cpl, &cookie);
479 static struct tcb_histent *
480 alloc_tcb_histent(struct adapter *sc, u_int tid, int flags)
482 struct tcb_histent *te;
484 MPASS(flags == M_NOWAIT || flags == M_WAITOK);
486 te = malloc(sizeof(*te), M_CXGBE, M_ZERO | flags);
489 mtx_init(&te->te_lock, "TCB entry", NULL, MTX_DEF);
490 callout_init_mtx(&te->te_callout, &te->te_lock, 0);
498 free_tcb_histent(struct tcb_histent *te)
501 mtx_destroy(&te->te_lock);
506 * Start tracking the tid in the TCB history.
509 add_tid_to_history(struct adapter *sc, u_int tid)
511 struct tcb_histent *te = NULL;
512 struct tom_data *td = sc->tom_softc;
515 MPASS(tid < sc->tids.ntids);
517 if (td->tcb_history == NULL)
520 rw_wlock(&td->tcb_history_lock);
521 if (td->tcb_history[tid] != NULL) {
525 te = alloc_tcb_histent(sc, tid, M_NOWAIT);
530 mtx_lock(&te->te_lock);
531 rc = send_get_tcb(sc, tid);
533 te->te_flags |= TE_RPL_PENDING;
534 td->tcb_history[tid] = te;
538 mtx_unlock(&te->te_lock);
540 rw_wunlock(&td->tcb_history_lock);
545 remove_tcb_histent(struct tcb_histent *te)
547 struct adapter *sc = te->te_adapter;
548 struct tom_data *td = sc->tom_softc;
550 rw_assert(&td->tcb_history_lock, RA_WLOCKED);
551 mtx_assert(&te->te_lock, MA_OWNED);
552 MPASS(td->tcb_history[te->te_tid] == te);
554 td->tcb_history[te->te_tid] = NULL;
555 free_tcb_histent(te);
556 rw_wunlock(&td->tcb_history_lock);
559 static inline struct tcb_histent *
560 lookup_tcb_histent(struct adapter *sc, u_int tid, bool addrem)
562 struct tcb_histent *te;
563 struct tom_data *td = sc->tom_softc;
565 MPASS(tid < sc->tids.ntids);
567 if (td->tcb_history == NULL)
571 rw_wlock(&td->tcb_history_lock);
573 rw_rlock(&td->tcb_history_lock);
574 te = td->tcb_history[tid];
576 mtx_lock(&te->te_lock);
577 return (te); /* with both locks held */
580 rw_wunlock(&td->tcb_history_lock);
582 rw_runlock(&td->tcb_history_lock);
588 release_tcb_histent(struct tcb_histent *te)
590 struct adapter *sc = te->te_adapter;
591 struct tom_data *td = sc->tom_softc;
593 mtx_assert(&te->te_lock, MA_OWNED);
594 mtx_unlock(&te->te_lock);
595 rw_assert(&td->tcb_history_lock, RA_RLOCKED);
596 rw_runlock(&td->tcb_history_lock);
600 request_tcb(void *arg)
602 struct tcb_histent *te = arg;
604 mtx_assert(&te->te_lock, MA_OWNED);
606 /* Noone else is supposed to update the histent. */
607 MPASS(!(te->te_flags & TE_RPL_PENDING));
608 if (send_get_tcb(te->te_adapter, te->te_tid) == 0)
609 te->te_flags |= TE_RPL_PENDING;
611 callout_schedule(&te->te_callout, hz / 100);
615 update_tcb_histent(struct tcb_histent *te, const uint64_t *tcb)
617 struct tom_data *td = te->te_adapter->tom_softc;
618 uint64_t tflags = get_tcb_tflags(tcb);
621 if (GET_TCB_FIELD(tcb, SND_MAX_RAW) != GET_TCB_FIELD(tcb, SND_UNA_RAW)) {
622 if (GET_TCB_FIELD(tcb, T_RXTSHIFT) != 0)
624 if (GET_TCB_FIELD(tcb, T_DUPACKS) != 0)
625 sample |= TS_DUPACKS;
626 if (GET_TCB_FIELD(tcb, T_DUPACKS) >= td->dupack_threshold)
627 sample |= TS_FASTREXMT;
630 if (GET_TCB_FIELD(tcb, SND_MAX_RAW) != 0) {
633 sample |= TS_SND_BACKLOGGED; /* for whatever reason. */
635 snd_wnd = GET_TCB_FIELD(tcb, RCV_ADV);
636 if (tflags & V_TF_RECV_SCALE(1))
637 snd_wnd <<= GET_TCB_FIELD(tcb, RCV_SCALE);
638 if (GET_TCB_FIELD(tcb, SND_CWND) < snd_wnd)
639 sample |= TS_CWND_LIMITED; /* maybe due to CWND */
642 if (tflags & V_TF_CCTRL_ECN(1)) {
645 * CE marker on incoming IP hdr, echoing ECE back in the TCP
646 * hdr. Indicates congestion somewhere on the way from the peer
649 if (tflags & V_TF_CCTRL_ECE(1))
650 sample |= TS_ECN_ECE;
653 * ECE seen and CWR sent (or about to be sent). Might indicate
654 * congestion on the way to the peer. This node is reducing its
655 * congestion window in response.
657 if (tflags & (V_TF_CCTRL_CWR(1) | V_TF_CCTRL_RFR(1)))
658 sample |= TS_ECN_CWR;
661 te->te_sample[te->te_pidx] = sample;
662 if (++te->te_pidx == nitems(te->te_sample))
664 memcpy(te->te_tcb, tcb, TCB_SIZE);
665 te->te_flags |= TE_ACTIVE;
669 do_get_tcb_rpl(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
671 struct adapter *sc = iq->adapter;
672 const struct cpl_get_tcb_rpl *cpl = mtod(m, const void *);
673 const uint64_t *tcb = (const uint64_t *)(const void *)(cpl + 1);
674 struct tcb_histent *te;
675 const u_int tid = GET_TID(cpl);
678 remove = GET_TCB_FIELD(tcb, T_STATE) == TCPS_CLOSED;
679 te = lookup_tcb_histent(sc, tid, remove);
681 /* Not in the history. Who issued the GET_TCB for this? */
682 device_printf(sc->dev, "tcb %u: flags 0x%016jx, state %u, "
683 "srtt %u, sscale %u, rscale %u, cookie 0x%x\n", tid,
684 (uintmax_t)get_tcb_tflags(tcb), GET_TCB_FIELD(tcb, T_STATE),
685 GET_TCB_FIELD(tcb, T_SRTT), GET_TCB_FIELD(tcb, SND_SCALE),
686 GET_TCB_FIELD(tcb, RCV_SCALE), cpl->cookie);
690 MPASS(te->te_flags & TE_RPL_PENDING);
691 te->te_flags &= ~TE_RPL_PENDING;
693 remove_tcb_histent(te);
695 update_tcb_histent(te, tcb);
696 callout_reset(&te->te_callout, hz / 10, request_tcb, te);
697 release_tcb_histent(te);
705 fill_tcp_info_from_tcb(struct adapter *sc, uint64_t *tcb, struct tcp_info *ti)
709 ti->tcpi_state = GET_TCB_FIELD(tcb, T_STATE);
711 v = GET_TCB_FIELD(tcb, T_SRTT);
712 ti->tcpi_rtt = tcp_ticks_to_us(sc, v);
714 v = GET_TCB_FIELD(tcb, T_RTTVAR);
715 ti->tcpi_rttvar = tcp_ticks_to_us(sc, v);
717 ti->tcpi_snd_ssthresh = GET_TCB_FIELD(tcb, SND_SSTHRESH);
718 ti->tcpi_snd_cwnd = GET_TCB_FIELD(tcb, SND_CWND);
719 ti->tcpi_rcv_nxt = GET_TCB_FIELD(tcb, RCV_NXT);
721 v = GET_TCB_FIELD(tcb, TX_MAX);
722 ti->tcpi_snd_nxt = v - GET_TCB_FIELD(tcb, SND_NXT_RAW);
724 /* Receive window being advertised by us. */
725 ti->tcpi_rcv_wscale = GET_TCB_FIELD(tcb, SND_SCALE); /* Yes, SND. */
726 ti->tcpi_rcv_space = GET_TCB_FIELD(tcb, RCV_WND);
729 ti->tcpi_snd_wscale = GET_TCB_FIELD(tcb, RCV_SCALE); /* Yes, RCV. */
730 ti->tcpi_snd_wnd = GET_TCB_FIELD(tcb, RCV_ADV);
731 if (get_tcb_tflags(tcb) & V_TF_RECV_SCALE(1))
732 ti->tcpi_snd_wnd <<= ti->tcpi_snd_wscale;
734 ti->tcpi_snd_wscale = 0;
739 fill_tcp_info_from_history(struct adapter *sc, struct tcb_histent *te,
743 fill_tcp_info_from_tcb(sc, te->te_tcb, ti);
747 * Reads the TCB for the given tid using a memory window and copies it to 'buf'
748 * in the same format as CPL_GET_TCB_RPL.
751 read_tcb_using_memwin(struct adapter *sc, u_int tid, uint64_t *buf)
757 MPASS(tid < sc->tids.ntids);
759 addr = t4_read_reg(sc, A_TP_CMM_TCB_BASE) + tid * TCB_SIZE;
760 rc = read_via_memwin(sc, 2, addr, (uint32_t *)buf, TCB_SIZE);
765 for (i = 0, j = TCB_SIZE - 16; i < j; i += 16, j -= 16) {
766 for (k = 0; k < 16; k++) {
768 tcb[i + k] = tcb[j + k];
775 fill_tcp_info(struct adapter *sc, u_int tid, struct tcp_info *ti)
777 uint64_t tcb[TCB_SIZE / sizeof(uint64_t)];
778 struct tcb_histent *te;
780 ti->tcpi_toe_tid = tid;
781 te = lookup_tcb_histent(sc, tid, false);
783 fill_tcp_info_from_history(sc, te, ti);
784 release_tcb_histent(te);
786 if (!(sc->debug_flags & DF_DISABLE_TCB_CACHE)) {
787 /* XXX: tell firmware to flush TCB cache. */
789 read_tcb_using_memwin(sc, tid, tcb);
790 fill_tcp_info_from_tcb(sc, tcb, ti);
795 * Called by the kernel to allow the TOE driver to "refine" values filled up in
796 * the tcp_info for an offloaded connection.
799 t4_tcp_info(struct toedev *tod, struct tcpcb *tp, struct tcp_info *ti)
801 struct adapter *sc = tod->tod_softc;
802 struct toepcb *toep = tp->t_toe;
804 INP_WLOCK_ASSERT(tp->t_inpcb);
807 fill_tcp_info(sc, toep->tid, ti);
812 t4_alloc_tls_session(struct toedev *tod, struct tcpcb *tp,
813 struct ktls_session *tls)
815 struct toepcb *toep = tp->t_toe;
817 INP_WLOCK_ASSERT(tp->t_inpcb);
820 return (tls_alloc_ktls(toep, tls));
825 * The TOE driver will not receive any more CPLs for the tid associated with the
826 * toepcb; release the hold on the inpcb.
829 final_cpl_received(struct toepcb *toep)
831 struct inpcb *inp = toep->inp;
833 KASSERT(inp != NULL, ("%s: inp is NULL", __func__));
834 INP_WLOCK_ASSERT(inp);
835 KASSERT(toep->flags & TPF_CPL_PENDING,
836 ("%s: CPL not pending already?", __func__));
838 CTR6(KTR_CXGBE, "%s: tid %d, toep %p (0x%x), inp %p (0x%x)",
839 __func__, toep->tid, toep, toep->flags, inp, inp->inp_flags);
841 if (ulp_mode(toep) == ULP_MODE_TCPDDP)
842 release_ddp_resources(toep);
844 toep->flags &= ~TPF_CPL_PENDING;
845 mbufq_drain(&toep->ulp_pdu_reclaimq);
847 if (!(toep->flags & TPF_ATTACHED))
848 release_offload_resources(toep);
850 if (!in_pcbrele_wlocked(inp))
855 insert_tid(struct adapter *sc, int tid, void *ctx, int ntids)
857 struct tid_info *t = &sc->tids;
859 MPASS(tid >= t->tid_base);
860 MPASS(tid - t->tid_base < t->ntids);
862 t->tid_tab[tid - t->tid_base] = ctx;
863 atomic_add_int(&t->tids_in_use, ntids);
867 lookup_tid(struct adapter *sc, int tid)
869 struct tid_info *t = &sc->tids;
871 return (t->tid_tab[tid - t->tid_base]);
875 update_tid(struct adapter *sc, int tid, void *ctx)
877 struct tid_info *t = &sc->tids;
879 t->tid_tab[tid - t->tid_base] = ctx;
883 remove_tid(struct adapter *sc, int tid, int ntids)
885 struct tid_info *t = &sc->tids;
887 t->tid_tab[tid - t->tid_base] = NULL;
888 atomic_subtract_int(&t->tids_in_use, ntids);
892 * What mtu_idx to use, given a 4-tuple. Note that both s->mss and tcp_mssopt
893 * have the MSS that we should advertise in our SYN. Advertised MSS doesn't
894 * account for any TCP options so the effective MSS (only payload, no headers or
895 * options) could be different.
898 find_best_mtu_idx(struct adapter *sc, struct in_conninfo *inc,
899 struct offload_settings *s)
901 unsigned short *mtus = &sc->params.mtus[0];
906 mss = s->mss > 0 ? s->mss : tcp_mssopt(inc);
907 if (inc->inc_flags & INC_ISIPV6)
908 mtu = mss + sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
910 mtu = mss + sizeof(struct ip) + sizeof(struct tcphdr);
912 for (i = 0; i < NMTUS - 1 && mtus[i + 1] <= mtu; i++)
919 * Determine the receive window size for a socket.
922 select_rcv_wnd(struct socket *so)
926 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
928 wnd = sbspace(&so->so_rcv);
929 if (wnd < MIN_RCV_WND)
932 return min(wnd, MAX_RCV_WND);
936 select_rcv_wscale(void)
939 unsigned long space = sb_max;
941 if (space > MAX_RCV_WND)
944 while (wscale < TCP_MAX_WINSHIFT && (TCP_MAXWIN << wscale) < space)
951 calc_options0(struct vi_info *vi, struct conn_params *cp)
955 opt0 |= F_TCAM_BYPASS;
957 MPASS(cp->wscale >= 0 && cp->wscale <= M_WND_SCALE);
958 opt0 |= V_WND_SCALE(cp->wscale);
960 MPASS(cp->mtu_idx >= 0 && cp->mtu_idx < NMTUS);
961 opt0 |= V_MSS_IDX(cp->mtu_idx);
963 MPASS(cp->ulp_mode >= 0 && cp->ulp_mode <= M_ULP_MODE);
964 opt0 |= V_ULP_MODE(cp->ulp_mode);
966 MPASS(cp->opt0_bufsize >= 0 && cp->opt0_bufsize <= M_RCV_BUFSIZ);
967 opt0 |= V_RCV_BUFSIZ(cp->opt0_bufsize);
969 MPASS(cp->l2t_idx >= 0 && cp->l2t_idx < vi->pi->adapter->vres.l2t.size);
970 opt0 |= V_L2T_IDX(cp->l2t_idx);
972 opt0 |= V_SMAC_SEL(vi->smt_idx);
973 opt0 |= V_TX_CHAN(vi->pi->tx_chan);
975 MPASS(cp->keepalive == 0 || cp->keepalive == 1);
976 opt0 |= V_KEEP_ALIVE(cp->keepalive);
978 MPASS(cp->nagle == 0 || cp->nagle == 1);
979 opt0 |= V_NAGLE(cp->nagle);
981 return (htobe64(opt0));
985 calc_options2(struct vi_info *vi, struct conn_params *cp)
988 struct port_info *pi = vi->pi;
989 struct adapter *sc = pi->adapter;
992 * rx flow control, rx coalesce, congestion control, and tx pace are all
993 * explicitly set by the driver. On T5+ the ISS is also set by the
994 * driver to the value picked by the kernel.
997 opt2 |= F_RX_FC_VALID | F_RX_COALESCE_VALID;
998 opt2 |= F_CONG_CNTRL_VALID | F_PACE_VALID;
1000 opt2 |= F_T5_OPT_2_VALID; /* all 4 valid */
1001 opt2 |= F_T5_ISS; /* ISS provided in CPL */
1004 MPASS(cp->sack == 0 || cp->sack == 1);
1005 opt2 |= V_SACK_EN(cp->sack);
1007 MPASS(cp->tstamp == 0 || cp->tstamp == 1);
1008 opt2 |= V_TSTAMPS_EN(cp->tstamp);
1011 opt2 |= F_WND_SCALE_EN;
1013 MPASS(cp->ecn == 0 || cp->ecn == 1);
1014 opt2 |= V_CCTRL_ECN(cp->ecn);
1016 /* XXX: F_RX_CHANNEL for multiple rx c-chan support goes here. */
1018 opt2 |= V_TX_QUEUE(sc->params.tp.tx_modq[pi->tx_chan]);
1020 opt2 |= F_RSS_QUEUE_VALID;
1021 opt2 |= V_RSS_QUEUE(sc->sge.ofld_rxq[cp->rxq_idx].iq.abs_id);
1023 MPASS(cp->cong_algo >= 0 && cp->cong_algo <= M_CONG_CNTRL);
1024 opt2 |= V_CONG_CNTRL(cp->cong_algo);
1026 MPASS(cp->rx_coalesce == 0 || cp->rx_coalesce == 1);
1027 if (cp->rx_coalesce == 1)
1028 opt2 |= V_RX_COALESCE(M_RX_COALESCE);
1030 opt2 |= V_RX_FC_DDP(0) | V_RX_FC_DISABLE(0);
1031 #ifdef USE_DDP_RX_FLOW_CONTROL
1032 if (cp->ulp_mode == ULP_MODE_TCPDDP)
1033 opt2 |= F_RX_FC_DDP;
1035 if (cp->ulp_mode == ULP_MODE_TLS)
1036 opt2 |= F_RX_FC_DISABLE;
1038 return (htobe32(opt2));
1042 select_ntuple(struct vi_info *vi, struct l2t_entry *e)
1044 struct adapter *sc = vi->pi->adapter;
1045 struct tp_params *tp = &sc->params.tp;
1046 uint64_t ntuple = 0;
1049 * Initialize each of the fields which we care about which are present
1050 * in the Compressed Filter Tuple.
1052 if (tp->vlan_shift >= 0 && EVL_VLANOFTAG(e->vlan) != CPL_L2T_VLAN_NONE)
1053 ntuple |= (uint64_t)(F_FT_VLAN_VLD | e->vlan) << tp->vlan_shift;
1055 if (tp->port_shift >= 0)
1056 ntuple |= (uint64_t)e->lport << tp->port_shift;
1058 if (tp->protocol_shift >= 0)
1059 ntuple |= (uint64_t)IPPROTO_TCP << tp->protocol_shift;
1061 if (tp->vnic_shift >= 0 && tp->ingress_config & F_VNIC) {
1062 ntuple |= (uint64_t)(V_FT_VNID_ID_VF(vi->vin) |
1063 V_FT_VNID_ID_PF(sc->pf) | V_FT_VNID_ID_VLD(vi->vfvld)) <<
1068 return (htobe32((uint32_t)ntuple));
1070 return (htobe64(V_FILTER_TUPLE(ntuple)));
1074 is_tls_sock(struct socket *so, struct adapter *sc)
1076 struct inpcb *inp = sotoinpcb(so);
1079 /* XXX: Eventually add a SO_WANT_TLS socket option perhaps? */
1082 for (i = 0; i < sc->tt.num_tls_rx_ports; i++) {
1083 if (inp->inp_lport == htons(sc->tt.tls_rx_ports[i]) ||
1084 inp->inp_fport == htons(sc->tt.tls_rx_ports[i])) {
1094 * Initialize various connection parameters.
1097 init_conn_params(struct vi_info *vi , struct offload_settings *s,
1098 struct in_conninfo *inc, struct socket *so,
1099 const struct tcp_options *tcpopt, int16_t l2t_idx, struct conn_params *cp)
1101 struct port_info *pi = vi->pi;
1102 struct adapter *sc = pi->adapter;
1103 struct tom_tunables *tt = &sc->tt;
1104 struct inpcb *inp = sotoinpcb(so);
1105 struct tcpcb *tp = intotcpcb(inp);
1108 MPASS(s->offload != 0);
1110 /* Congestion control algorithm */
1111 if (s->cong_algo >= 0)
1112 cp->cong_algo = s->cong_algo & M_CONG_CNTRL;
1113 else if (sc->tt.cong_algorithm >= 0)
1114 cp->cong_algo = tt->cong_algorithm & M_CONG_CNTRL;
1116 struct cc_algo *cc = CC_ALGO(tp);
1118 if (strcasecmp(cc->name, "reno") == 0)
1119 cp->cong_algo = CONG_ALG_RENO;
1120 else if (strcasecmp(cc->name, "tahoe") == 0)
1121 cp->cong_algo = CONG_ALG_TAHOE;
1122 if (strcasecmp(cc->name, "newreno") == 0)
1123 cp->cong_algo = CONG_ALG_NEWRENO;
1124 if (strcasecmp(cc->name, "highspeed") == 0)
1125 cp->cong_algo = CONG_ALG_HIGHSPEED;
1128 * Use newreno in case the algorithm selected by the
1129 * host stack is not supported by the hardware.
1131 cp->cong_algo = CONG_ALG_NEWRENO;
1135 /* Tx traffic scheduling class. */
1136 if (s->sched_class >= 0 &&
1137 s->sched_class < sc->chip_params->nsched_cls) {
1138 cp->tc_idx = s->sched_class;
1142 /* Nagle's algorithm. */
1144 cp->nagle = s->nagle > 0 ? 1 : 0;
1146 cp->nagle = tp->t_flags & TF_NODELAY ? 0 : 1;
1148 /* TCP Keepalive. */
1149 if (tcp_always_keepalive || so_options_get(so) & SO_KEEPALIVE)
1154 /* Optimization that's specific to T5 @ 40G. */
1155 if (tt->tx_align >= 0)
1156 cp->tx_align = tt->tx_align > 0 ? 1 : 0;
1157 else if (chip_id(sc) == CHELSIO_T5 &&
1158 (port_top_speed(pi) > 10 || sc->params.nports > 2))
1164 if (can_tls_offload(sc) &&
1165 (s->tls > 0 || (s->tls < 0 && is_tls_sock(so, sc))))
1166 cp->ulp_mode = ULP_MODE_TLS;
1167 else if (s->ddp > 0 ||
1168 (s->ddp < 0 && sc->tt.ddp && (so_options_get(so) & SO_NO_DDP) == 0))
1169 cp->ulp_mode = ULP_MODE_TCPDDP;
1171 cp->ulp_mode = ULP_MODE_NONE;
1173 /* Rx coalescing. */
1174 if (s->rx_coalesce >= 0)
1175 cp->rx_coalesce = s->rx_coalesce > 0 ? 1 : 0;
1176 else if (cp->ulp_mode == ULP_MODE_TLS)
1177 cp->rx_coalesce = 0;
1178 else if (tt->rx_coalesce >= 0)
1179 cp->rx_coalesce = tt->rx_coalesce > 0 ? 1 : 0;
1181 cp->rx_coalesce = 1; /* default */
1184 * Index in the PMTU table. This controls the MSS that we announce in
1185 * our SYN initially, but after ESTABLISHED it controls the MSS that we
1188 cp->mtu_idx = find_best_mtu_idx(sc, inc, s);
1190 /* Tx queue for this connection. */
1191 if (s->txq >= 0 && s->txq < vi->nofldtxq)
1192 cp->txq_idx = s->txq;
1194 cp->txq_idx = arc4random() % vi->nofldtxq;
1195 cp->txq_idx += vi->first_ofld_txq;
1197 /* Rx queue for this connection. */
1198 if (s->rxq >= 0 && s->rxq < vi->nofldrxq)
1199 cp->rxq_idx = s->rxq;
1201 cp->rxq_idx = arc4random() % vi->nofldrxq;
1202 cp->rxq_idx += vi->first_ofld_rxq;
1204 if (SOLISTENING(so)) {
1206 MPASS(tcpopt != NULL);
1208 /* TCP timestamp option */
1209 if (tcpopt->tstamp &&
1210 (s->tstamp > 0 || (s->tstamp < 0 && V_tcp_do_rfc1323)))
1217 (s->sack > 0 || (s->sack < 0 && V_tcp_do_sack)))
1222 /* Receive window scaling. */
1223 if (tcpopt->wsf > 0 && tcpopt->wsf < 15 && V_tcp_do_rfc1323)
1224 cp->wscale = select_rcv_wscale();
1229 if (tcpopt->ecn && /* XXX: review. */
1230 (s->ecn > 0 || (s->ecn < 0 && V_tcp_do_ecn)))
1235 wnd = max(so->sol_sbrcv_hiwat, MIN_RCV_WND);
1236 cp->opt0_bufsize = min(wnd >> 10, M_RCV_BUFSIZ);
1239 cp->sndbuf = tt->sndbuf;
1240 else if (so->sol_sbsnd_flags & SB_AUTOSIZE &&
1241 V_tcp_do_autosndbuf)
1242 cp->sndbuf = 256 * 1024;
1244 cp->sndbuf = so->sol_sbsnd_hiwat;
1248 /* TCP timestamp option */
1249 if (s->tstamp > 0 ||
1250 (s->tstamp < 0 && (tp->t_flags & TF_REQ_TSTMP)))
1257 (s->sack < 0 && (tp->t_flags & TF_SACK_PERMIT)))
1262 /* Receive window scaling */
1263 if (tp->t_flags & TF_REQ_SCALE)
1264 cp->wscale = select_rcv_wscale();
1269 if (s->ecn > 0 || (s->ecn < 0 && V_tcp_do_ecn == 1))
1274 SOCKBUF_LOCK(&so->so_rcv);
1275 wnd = max(select_rcv_wnd(so), MIN_RCV_WND);
1276 SOCKBUF_UNLOCK(&so->so_rcv);
1277 cp->opt0_bufsize = min(wnd >> 10, M_RCV_BUFSIZ);
1280 cp->sndbuf = tt->sndbuf;
1282 SOCKBUF_LOCK(&so->so_snd);
1283 if (so->so_snd.sb_flags & SB_AUTOSIZE &&
1284 V_tcp_do_autosndbuf)
1285 cp->sndbuf = 256 * 1024;
1287 cp->sndbuf = so->so_snd.sb_hiwat;
1288 SOCKBUF_UNLOCK(&so->so_snd);
1292 cp->l2t_idx = l2t_idx;
1294 /* This will be initialized on ESTABLISHED. */
1299 negative_advice(int status)
1302 return (status == CPL_ERR_RTX_NEG_ADVICE ||
1303 status == CPL_ERR_PERSIST_NEG_ADVICE ||
1304 status == CPL_ERR_KEEPALV_NEG_ADVICE);
1308 alloc_tid_tab(struct tid_info *t, int flags)
1311 MPASS(t->ntids > 0);
1312 MPASS(t->tid_tab == NULL);
1314 t->tid_tab = malloc(t->ntids * sizeof(*t->tid_tab), M_CXGBE,
1316 if (t->tid_tab == NULL)
1318 atomic_store_rel_int(&t->tids_in_use, 0);
1324 free_tid_tab(struct tid_info *t)
1327 KASSERT(t->tids_in_use == 0,
1328 ("%s: %d tids still in use.", __func__, t->tids_in_use));
1330 free(t->tid_tab, M_CXGBE);
1335 alloc_stid_tab(struct tid_info *t, int flags)
1338 MPASS(t->nstids > 0);
1339 MPASS(t->stid_tab == NULL);
1341 t->stid_tab = malloc(t->nstids * sizeof(*t->stid_tab), M_CXGBE,
1343 if (t->stid_tab == NULL)
1345 mtx_init(&t->stid_lock, "stid lock", NULL, MTX_DEF);
1346 t->stids_in_use = 0;
1347 TAILQ_INIT(&t->stids);
1348 t->nstids_free_head = t->nstids;
1354 free_stid_tab(struct tid_info *t)
1357 KASSERT(t->stids_in_use == 0,
1358 ("%s: %d tids still in use.", __func__, t->stids_in_use));
1360 if (mtx_initialized(&t->stid_lock))
1361 mtx_destroy(&t->stid_lock);
1362 free(t->stid_tab, M_CXGBE);
1367 free_tid_tabs(struct tid_info *t)
1376 alloc_tid_tabs(struct tid_info *t)
1380 rc = alloc_tid_tab(t, M_NOWAIT);
1384 rc = alloc_atid_tab(t, M_NOWAIT);
1388 rc = alloc_stid_tab(t, M_NOWAIT);
1399 alloc_tcb_history(struct adapter *sc, struct tom_data *td)
1402 if (sc->tids.ntids == 0 || sc->tids.ntids > 1024)
1404 rw_init(&td->tcb_history_lock, "TCB history");
1405 td->tcb_history = malloc(sc->tids.ntids * sizeof(*td->tcb_history),
1406 M_CXGBE, M_ZERO | M_NOWAIT);
1407 td->dupack_threshold = G_DUPACKTHRESH(t4_read_reg(sc, A_TP_PARA_REG0));
1411 free_tcb_history(struct adapter *sc, struct tom_data *td)
1416 if (td->tcb_history != NULL) {
1417 for (i = 0; i < sc->tids.ntids; i++) {
1418 MPASS(td->tcb_history[i] == NULL);
1422 free(td->tcb_history, M_CXGBE);
1423 if (rw_initialized(&td->tcb_history_lock))
1424 rw_destroy(&td->tcb_history_lock);
1428 free_tom_data(struct adapter *sc, struct tom_data *td)
1431 ASSERT_SYNCHRONIZED_OP(sc);
1433 KASSERT(TAILQ_EMPTY(&td->toep_list),
1434 ("%s: TOE PCB list is not empty.", __func__));
1435 KASSERT(td->lctx_count == 0,
1436 ("%s: lctx hash table is not empty.", __func__));
1438 t4_free_ppod_region(&td->pr);
1440 if (td->listen_mask != 0)
1441 hashdestroy(td->listen_hash, M_CXGBE, td->listen_mask);
1443 if (mtx_initialized(&td->unsent_wr_lock))
1444 mtx_destroy(&td->unsent_wr_lock);
1445 if (mtx_initialized(&td->lctx_hash_lock))
1446 mtx_destroy(&td->lctx_hash_lock);
1447 if (mtx_initialized(&td->toep_list_lock))
1448 mtx_destroy(&td->toep_list_lock);
1450 free_tcb_history(sc, td);
1451 free_tid_tabs(&sc->tids);
1456 prepare_pkt(int open_type, uint16_t vtag, struct inpcb *inp, int *pktlen,
1463 max(sizeof(struct ether_header), sizeof(struct ether_vlan_header)) +
1464 max(sizeof(struct ip), sizeof(struct ip6_hdr)) +
1465 sizeof(struct tcphdr);
1467 MPASS(open_type == OPEN_TYPE_ACTIVE || open_type == OPEN_TYPE_LISTEN);
1469 pkt = malloc(maxlen, M_CXGBE, M_ZERO | M_NOWAIT);
1473 ipv6 = inp->inp_vflag & INP_IPV6;
1476 if (EVL_VLANOFTAG(vtag) == 0xfff) {
1477 struct ether_header *eh = (void *)pkt;
1480 eh->ether_type = htons(ETHERTYPE_IPV6);
1482 eh->ether_type = htons(ETHERTYPE_IP);
1486 struct ether_vlan_header *evh = (void *)pkt;
1488 evh->evl_encap_proto = htons(ETHERTYPE_VLAN);
1489 evh->evl_tag = htons(vtag);
1491 evh->evl_proto = htons(ETHERTYPE_IPV6);
1493 evh->evl_proto = htons(ETHERTYPE_IP);
1495 len += sizeof(*evh);
1499 struct ip6_hdr *ip6 = (void *)&pkt[len];
1501 ip6->ip6_vfc = IPV6_VERSION;
1502 ip6->ip6_plen = htons(sizeof(struct tcphdr));
1503 ip6->ip6_nxt = IPPROTO_TCP;
1504 if (open_type == OPEN_TYPE_ACTIVE) {
1505 ip6->ip6_src = inp->in6p_laddr;
1506 ip6->ip6_dst = inp->in6p_faddr;
1507 } else if (open_type == OPEN_TYPE_LISTEN) {
1508 ip6->ip6_src = inp->in6p_laddr;
1509 ip6->ip6_dst = ip6->ip6_src;
1512 len += sizeof(*ip6);
1514 struct ip *ip = (void *)&pkt[len];
1516 ip->ip_v = IPVERSION;
1517 ip->ip_hl = sizeof(*ip) >> 2;
1518 ip->ip_tos = inp->inp_ip_tos;
1519 ip->ip_len = htons(sizeof(struct ip) + sizeof(struct tcphdr));
1520 ip->ip_ttl = inp->inp_ip_ttl;
1521 ip->ip_p = IPPROTO_TCP;
1522 if (open_type == OPEN_TYPE_ACTIVE) {
1523 ip->ip_src = inp->inp_laddr;
1524 ip->ip_dst = inp->inp_faddr;
1525 } else if (open_type == OPEN_TYPE_LISTEN) {
1526 ip->ip_src = inp->inp_laddr;
1527 ip->ip_dst = ip->ip_src;
1533 th = (void *)&pkt[len];
1534 if (open_type == OPEN_TYPE_ACTIVE) {
1535 th->th_sport = inp->inp_lport; /* network byte order already */
1536 th->th_dport = inp->inp_fport; /* ditto */
1537 } else if (open_type == OPEN_TYPE_LISTEN) {
1538 th->th_sport = inp->inp_lport; /* network byte order already */
1539 th->th_dport = th->th_sport;
1543 *pktlen = *buflen = len;
1547 const struct offload_settings *
1548 lookup_offload_policy(struct adapter *sc, int open_type, struct mbuf *m,
1549 uint16_t vtag, struct inpcb *inp)
1551 const struct t4_offload_policy *op;
1553 struct offload_rule *r;
1554 int i, matched, pktlen, buflen;
1555 static const struct offload_settings allow_offloading_settings = {
1570 static const struct offload_settings disallow_offloading_settings = {
1572 /* rest is irrelevant when offload is off. */
1575 rw_assert(&sc->policy_lock, RA_LOCKED);
1578 * If there's no Connection Offloading Policy attached to the device
1579 * then we need to return a default static policy. If
1580 * "cop_managed_offloading" is true, then we need to disallow
1581 * offloading until a COP is attached to the device. Otherwise we
1582 * allow offloading ...
1586 if (sc->tt.cop_managed_offloading)
1587 return (&disallow_offloading_settings);
1589 return (&allow_offloading_settings);
1592 switch (open_type) {
1593 case OPEN_TYPE_ACTIVE:
1594 case OPEN_TYPE_LISTEN:
1595 pkt = prepare_pkt(open_type, vtag, inp, &pktlen, &buflen);
1597 case OPEN_TYPE_PASSIVE:
1599 pkt = mtod(m, char *);
1600 MPASS(*pkt == CPL_PASS_ACCEPT_REQ);
1601 pkt += sizeof(struct cpl_pass_accept_req);
1602 pktlen = m->m_pkthdr.len - sizeof(struct cpl_pass_accept_req);
1603 buflen = m->m_len - sizeof(struct cpl_pass_accept_req);
1607 return (&disallow_offloading_settings);
1610 if (pkt == NULL || pktlen == 0 || buflen == 0)
1611 return (&disallow_offloading_settings);
1615 for (i = 0; i < op->nrules; i++, r++) {
1616 if (r->open_type != open_type &&
1617 r->open_type != OPEN_TYPE_DONTCARE) {
1620 matched = bpf_filter(r->bpf_prog.bf_insns, pkt, pktlen, buflen);
1625 if (open_type == OPEN_TYPE_ACTIVE || open_type == OPEN_TYPE_LISTEN)
1628 return (matched ? &r->settings : &disallow_offloading_settings);
1632 reclaim_wr_resources(void *arg, int count)
1634 struct tom_data *td = arg;
1635 STAILQ_HEAD(, wrqe) twr_list = STAILQ_HEAD_INITIALIZER(twr_list);
1636 struct cpl_act_open_req *cpl;
1637 u_int opcode, atid, tid;
1639 struct adapter *sc = td_adapter(td);
1641 mtx_lock(&td->unsent_wr_lock);
1642 STAILQ_SWAP(&td->unsent_wr_list, &twr_list, wrqe);
1643 mtx_unlock(&td->unsent_wr_lock);
1645 while ((wr = STAILQ_FIRST(&twr_list)) != NULL) {
1646 STAILQ_REMOVE_HEAD(&twr_list, link);
1649 opcode = GET_OPCODE(cpl);
1652 case CPL_ACT_OPEN_REQ:
1653 case CPL_ACT_OPEN_REQ6:
1654 atid = G_TID_TID(be32toh(OPCODE_TID(cpl)));
1655 CTR2(KTR_CXGBE, "%s: atid %u ", __func__, atid);
1656 act_open_failure_cleanup(sc, atid, EHOSTUNREACH);
1659 case CPL_PASS_ACCEPT_RPL:
1661 CTR2(KTR_CXGBE, "%s: tid %u ", __func__, tid);
1662 synack_failure_cleanup(sc, tid);
1666 log(LOG_ERR, "%s: leaked work request %p, wr_len %d, "
1667 "opcode %x\n", __func__, wr, wr->wr_len, opcode);
1668 /* WR not freed here; go look at it with a debugger. */
1674 * Ground control to Major TOM
1675 * Commencing countdown, engines on
1678 t4_tom_activate(struct adapter *sc)
1680 struct tom_data *td;
1685 ASSERT_SYNCHRONIZED_OP(sc);
1687 /* per-adapter softc for TOM */
1688 td = malloc(sizeof(*td), M_CXGBE, M_ZERO | M_NOWAIT);
1692 /* List of TOE PCBs and associated lock */
1693 mtx_init(&td->toep_list_lock, "PCB list lock", NULL, MTX_DEF);
1694 TAILQ_INIT(&td->toep_list);
1696 /* Listen context */
1697 mtx_init(&td->lctx_hash_lock, "lctx hash lock", NULL, MTX_DEF);
1698 td->listen_hash = hashinit_flags(LISTEN_HASH_SIZE, M_CXGBE,
1699 &td->listen_mask, HASH_NOWAIT);
1701 /* List of WRs for which L2 resolution failed */
1702 mtx_init(&td->unsent_wr_lock, "Unsent WR list lock", NULL, MTX_DEF);
1703 STAILQ_INIT(&td->unsent_wr_list);
1704 TASK_INIT(&td->reclaim_wr_resources, 0, reclaim_wr_resources, td);
1707 rc = alloc_tid_tabs(&sc->tids);
1711 rc = t4_init_ppod_region(&td->pr, &sc->vres.ddp,
1712 t4_read_reg(sc, A_ULP_RX_TDDP_PSZ), "TDDP page pods");
1715 t4_set_reg_field(sc, A_ULP_RX_TDDP_TAGMASK,
1716 V_TDDPTAGMASK(M_TDDPTAGMASK), td->pr.pr_tag_mask);
1718 alloc_tcb_history(sc, td);
1723 tod->tod_softc = sc;
1724 tod->tod_connect = t4_connect;
1725 tod->tod_listen_start = t4_listen_start;
1726 tod->tod_listen_stop = t4_listen_stop;
1727 tod->tod_rcvd = t4_rcvd;
1728 tod->tod_output = t4_tod_output;
1729 tod->tod_send_rst = t4_send_rst;
1730 tod->tod_send_fin = t4_send_fin;
1731 tod->tod_pcb_detach = t4_pcb_detach;
1732 tod->tod_l2_update = t4_l2_update;
1733 tod->tod_syncache_added = t4_syncache_added;
1734 tod->tod_syncache_removed = t4_syncache_removed;
1735 tod->tod_syncache_respond = t4_syncache_respond;
1736 tod->tod_offload_socket = t4_offload_socket;
1737 tod->tod_ctloutput = t4_ctloutput;
1738 tod->tod_tcp_info = t4_tcp_info;
1740 tod->tod_alloc_tls_session = t4_alloc_tls_session;
1743 for_each_port(sc, i) {
1744 for_each_vi(sc->port[i], v, vi) {
1745 TOEDEV(vi->ifp) = &td->tod;
1750 register_toedev(sc->tom_softc);
1754 free_tom_data(sc, td);
1759 t4_tom_deactivate(struct adapter *sc)
1762 struct tom_data *td = sc->tom_softc;
1764 ASSERT_SYNCHRONIZED_OP(sc);
1767 return (0); /* XXX. KASSERT? */
1769 if (sc->offload_map != 0)
1770 return (EBUSY); /* at least one port has IFCAP_TOE enabled */
1772 if (uld_active(sc, ULD_IWARP) || uld_active(sc, ULD_ISCSI))
1773 return (EBUSY); /* both iWARP and iSCSI rely on the TOE. */
1775 mtx_lock(&td->toep_list_lock);
1776 if (!TAILQ_EMPTY(&td->toep_list))
1778 mtx_unlock(&td->toep_list_lock);
1780 mtx_lock(&td->lctx_hash_lock);
1781 if (td->lctx_count > 0)
1783 mtx_unlock(&td->lctx_hash_lock);
1785 taskqueue_drain(taskqueue_thread, &td->reclaim_wr_resources);
1786 mtx_lock(&td->unsent_wr_lock);
1787 if (!STAILQ_EMPTY(&td->unsent_wr_list))
1789 mtx_unlock(&td->unsent_wr_lock);
1792 unregister_toedev(sc->tom_softc);
1793 free_tom_data(sc, td);
1794 sc->tom_softc = NULL;
1801 t4_aio_queue_tom(struct socket *so, struct kaiocb *job)
1803 struct tcpcb *tp = so_sototcpcb(so);
1804 struct toepcb *toep = tp->t_toe;
1807 if (ulp_mode(toep) == ULP_MODE_TCPDDP) {
1808 error = t4_aio_queue_ddp(so, job);
1809 if (error != EOPNOTSUPP)
1813 return (t4_aio_queue_aiotx(so, job));
1817 t4_ctloutput_tom(struct socket *so, struct sockopt *sopt)
1820 if (sopt->sopt_level != IPPROTO_TCP)
1821 return (tcp_ctloutput(so, sopt));
1823 switch (sopt->sopt_name) {
1824 case TCP_TLSOM_SET_TLS_CONTEXT:
1825 case TCP_TLSOM_GET_TLS_TOM:
1826 case TCP_TLSOM_CLR_TLS_TOM:
1827 case TCP_TLSOM_CLR_QUIES:
1828 return (t4_ctloutput_tls(so, sopt));
1830 return (tcp_ctloutput(so, sopt));
1835 t4_tom_mod_load(void)
1837 struct protosw *tcp_protosw, *tcp6_protosw;
1840 t4_register_cpl_handler(CPL_GET_TCB_RPL, do_get_tcb_rpl);
1841 t4_register_shared_cpl_handler(CPL_L2T_WRITE_RPL, do_l2t_write_rpl2,
1843 t4_init_connect_cpl_handlers();
1844 t4_init_listen_cpl_handlers();
1845 t4_init_cpl_io_handlers();
1850 tcp_protosw = pffindproto(PF_INET, IPPROTO_TCP, SOCK_STREAM);
1851 if (tcp_protosw == NULL)
1852 return (ENOPROTOOPT);
1853 bcopy(tcp_protosw, &toe_protosw, sizeof(toe_protosw));
1854 bcopy(tcp_protosw->pr_usrreqs, &toe_usrreqs, sizeof(toe_usrreqs));
1855 toe_usrreqs.pru_aio_queue = t4_aio_queue_tom;
1856 toe_protosw.pr_ctloutput = t4_ctloutput_tom;
1857 toe_protosw.pr_usrreqs = &toe_usrreqs;
1859 tcp6_protosw = pffindproto(PF_INET6, IPPROTO_TCP, SOCK_STREAM);
1860 if (tcp6_protosw == NULL)
1861 return (ENOPROTOOPT);
1862 bcopy(tcp6_protosw, &toe6_protosw, sizeof(toe6_protosw));
1863 bcopy(tcp6_protosw->pr_usrreqs, &toe6_usrreqs, sizeof(toe6_usrreqs));
1864 toe6_usrreqs.pru_aio_queue = t4_aio_queue_tom;
1865 toe6_protosw.pr_ctloutput = t4_ctloutput_tom;
1866 toe6_protosw.pr_usrreqs = &toe6_usrreqs;
1868 return (t4_register_uld(&tom_uld_info));
1872 tom_uninit(struct adapter *sc, void *arg __unused)
1874 if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4tomun"))
1877 /* Try to free resources (works only if no port has IFCAP_TOE) */
1878 if (uld_active(sc, ULD_TOM))
1879 t4_deactivate_uld(sc, ULD_TOM);
1881 end_synchronized_op(sc, 0);
1885 t4_tom_mod_unload(void)
1887 t4_iterate(tom_uninit, NULL);
1889 if (t4_unregister_uld(&tom_uld_info) == EBUSY)
1892 t4_tls_mod_unload();
1893 t4_ddp_mod_unload();
1895 t4_uninit_connect_cpl_handlers();
1896 t4_uninit_listen_cpl_handlers();
1897 t4_uninit_cpl_io_handlers();
1898 t4_register_shared_cpl_handler(CPL_L2T_WRITE_RPL, NULL, CPL_COOKIE_TOM);
1902 #endif /* TCP_OFFLOAD */
1905 t4_tom_modevent(module_t mod, int cmd, void *arg)
1912 rc = t4_tom_mod_load();
1916 rc = t4_tom_mod_unload();
1923 printf("t4_tom: compiled without TCP_OFFLOAD support.\n");
1929 static moduledata_t t4_tom_moddata= {
1935 MODULE_VERSION(t4_tom, 1);
1936 MODULE_DEPEND(t4_tom, toecore, 1, 1, 1);
1937 MODULE_DEPEND(t4_tom, t4nex, 1, 1, 1);
1938 DECLARE_MODULE(t4_tom, t4_tom_moddata, SI_SUB_EXEC, SI_ORDER_ANY);