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)
190 toep->flags |= TPF_INITIALIZED;
196 hold_toepcb(struct toepcb *toep)
199 refcount_acquire(&toep->refcount);
204 free_toepcb(struct toepcb *toep)
207 if (refcount_release(&toep->refcount) == 0)
210 KASSERT(!(toep->flags & TPF_ATTACHED),
211 ("%s: attached to an inpcb", __func__));
212 KASSERT(!(toep->flags & TPF_CPL_PENDING),
213 ("%s: CPL pending", __func__));
215 if (toep->flags & TPF_INITIALIZED) {
216 if (ulp_mode(toep) == ULP_MODE_TCPDDP)
217 ddp_uninit_toep(toep);
218 tls_uninit_toep(toep);
224 * Set up the socket for TCP offload.
227 offload_socket(struct socket *so, struct toepcb *toep)
229 struct tom_data *td = toep->td;
230 struct inpcb *inp = sotoinpcb(so);
231 struct tcpcb *tp = intotcpcb(inp);
234 INP_WLOCK_ASSERT(inp);
239 sb->sb_flags |= SB_NOCOALESCE;
243 sb->sb_flags |= SB_NOCOALESCE;
244 if (inp->inp_vflag & INP_IPV6)
245 so->so_proto = &toe6_protosw;
247 so->so_proto = &toe_protosw;
253 tp->t_flags |= TF_TOE;
255 /* Install an extra hold on inp */
257 toep->flags |= TPF_ATTACHED;
260 /* Add the TOE PCB to the active list */
261 mtx_lock(&td->toep_list_lock);
262 TAILQ_INSERT_HEAD(&td->toep_list, toep, link);
263 mtx_unlock(&td->toep_list_lock);
266 /* This is _not_ the normal way to "unoffload" a socket. */
268 undo_offload_socket(struct socket *so)
270 struct inpcb *inp = sotoinpcb(so);
271 struct tcpcb *tp = intotcpcb(inp);
272 struct toepcb *toep = tp->t_toe;
273 struct tom_data *td = toep->td;
276 INP_WLOCK_ASSERT(inp);
280 sb->sb_flags &= ~SB_NOCOALESCE;
284 sb->sb_flags &= ~SB_NOCOALESCE;
289 tp->t_flags &= ~TF_TOE;
292 toep->flags &= ~TPF_ATTACHED;
293 if (in_pcbrele_wlocked(inp))
294 panic("%s: inp freed.", __func__);
296 mtx_lock(&td->toep_list_lock);
297 TAILQ_REMOVE(&td->toep_list, toep, link);
298 mtx_unlock(&td->toep_list_lock);
302 release_offload_resources(struct toepcb *toep)
304 struct tom_data *td = toep->td;
305 struct adapter *sc = td_adapter(td);
308 KASSERT(!(toep->flags & TPF_CPL_PENDING),
309 ("%s: %p has CPL pending.", __func__, toep));
310 KASSERT(!(toep->flags & TPF_ATTACHED),
311 ("%s: %p is still attached.", __func__, toep));
313 CTR5(KTR_CXGBE, "%s: toep %p (tid %d, l2te %p, ce %p)",
314 __func__, toep, tid, toep->l2te, toep->ce);
317 * These queues should have been emptied at approximately the same time
318 * that a normal connection's socket's so_snd would have been purged or
319 * drained. Do _not_ clean up here.
321 MPASS(mbufq_len(&toep->ulp_pduq) == 0);
322 MPASS(mbufq_len(&toep->ulp_pdu_reclaimq) == 0);
324 if (ulp_mode(toep) == ULP_MODE_TCPDDP)
325 ddp_assert_empty(toep);
327 MPASS(TAILQ_EMPTY(&toep->aiotx_jobq));
330 t4_l2t_release(toep->l2te);
333 remove_tid(sc, tid, toep->ce ? 2 : 1);
334 release_tid(sc, tid, toep->ctrlq);
338 t4_release_lip(sc, toep->ce);
340 if (toep->params.tc_idx != -1)
341 t4_release_cl_rl(sc, toep->vi->pi->port_id, toep->params.tc_idx);
343 mtx_lock(&td->toep_list_lock);
344 TAILQ_REMOVE(&td->toep_list, toep, link);
345 mtx_unlock(&td->toep_list_lock);
351 * The kernel is done with the TCP PCB and this is our opportunity to unhook the
352 * toepcb hanging off of it. If the TOE driver is also done with the toepcb (no
353 * pending CPL) then it is time to release all resources tied to the toepcb.
355 * Also gets called when an offloaded active open fails and the TOM wants the
356 * kernel to take the TCP PCB back.
359 t4_pcb_detach(struct toedev *tod __unused, struct tcpcb *tp)
361 #if defined(KTR) || defined(INVARIANTS)
362 struct inpcb *inp = tp->t_inpcb;
364 struct toepcb *toep = tp->t_toe;
366 INP_WLOCK_ASSERT(inp);
368 KASSERT(toep != NULL, ("%s: toep is NULL", __func__));
369 KASSERT(toep->flags & TPF_ATTACHED,
370 ("%s: not attached", __func__));
373 if (tp->t_state == TCPS_SYN_SENT) {
374 CTR6(KTR_CXGBE, "%s: atid %d, toep %p (0x%x), inp %p (0x%x)",
375 __func__, toep->tid, toep, toep->flags, inp,
379 "t4_pcb_detach: tid %d (%s), toep %p (0x%x), inp %p (0x%x)",
380 toep->tid, tcpstates[tp->t_state], toep, toep->flags, inp,
386 tp->t_flags &= ~TF_TOE;
387 toep->flags &= ~TPF_ATTACHED;
389 if (!(toep->flags & TPF_CPL_PENDING))
390 release_offload_resources(toep);
394 * setsockopt handler.
397 t4_ctloutput(struct toedev *tod, struct tcpcb *tp, int dir, int name)
399 struct adapter *sc = tod->tod_softc;
400 struct toepcb *toep = tp->t_toe;
405 CTR4(KTR_CXGBE, "%s: tp %p, dir %u, name %u", __func__, tp, dir, name);
409 if (tp->t_state != TCPS_ESTABLISHED)
411 toep->params.nagle = tp->t_flags & TF_NODELAY ? 0 : 1;
412 t4_set_tcb_field(sc, toep->ctrlq, toep, W_TCB_T_FLAGS,
413 V_TF_NAGLE(1), V_TF_NAGLE(toep->params.nagle), 0, 0);
420 static inline uint64_t
421 get_tcb_tflags(const uint64_t *tcb)
424 return ((be64toh(tcb[14]) << 32) | (be64toh(tcb[15]) >> 32));
427 static inline uint32_t
428 get_tcb_field(const uint64_t *tcb, u_int word, uint32_t mask, u_int shift)
430 #define LAST_WORD ((TCB_SIZE / 4) - 1)
435 MPASS(word <= LAST_WORD);
438 flit_idx = (LAST_WORD - word) / 2;
441 t1 = be64toh(tcb[flit_idx]) >> shift;
443 if (fls(mask) > 64 - shift) {
445 * Will spill over into the next logical flit, which is the flit
446 * before this one. The flit_idx before this one must be valid.
449 t2 = be64toh(tcb[flit_idx - 1]) << (64 - shift);
451 return ((t2 | t1) & mask);
454 #define GET_TCB_FIELD(tcb, F) \
455 get_tcb_field(tcb, W_TCB_##F, M_TCB_##F, S_TCB_##F)
458 * Issues a CPL_GET_TCB to read the entire TCB for the tid.
461 send_get_tcb(struct adapter *sc, u_int tid)
463 struct cpl_get_tcb *cpl;
464 struct wrq_cookie cookie;
466 MPASS(tid < sc->tids.ntids);
468 cpl = start_wrq_wr(&sc->sge.ctrlq[0], howmany(sizeof(*cpl), 16),
470 if (__predict_false(cpl == NULL))
472 bzero(cpl, sizeof(*cpl));
473 INIT_TP_WR(cpl, tid);
474 OPCODE_TID(cpl) = htobe32(MK_OPCODE_TID(CPL_GET_TCB, tid));
475 cpl->reply_ctrl = htobe16(V_REPLY_CHAN(0) |
476 V_QUEUENO(sc->sge.ofld_rxq[0].iq.cntxt_id));
478 commit_wrq_wr(&sc->sge.ctrlq[0], cpl, &cookie);
483 static struct tcb_histent *
484 alloc_tcb_histent(struct adapter *sc, u_int tid, int flags)
486 struct tcb_histent *te;
488 MPASS(flags == M_NOWAIT || flags == M_WAITOK);
490 te = malloc(sizeof(*te), M_CXGBE, M_ZERO | flags);
493 mtx_init(&te->te_lock, "TCB entry", NULL, MTX_DEF);
494 callout_init_mtx(&te->te_callout, &te->te_lock, 0);
502 free_tcb_histent(struct tcb_histent *te)
505 mtx_destroy(&te->te_lock);
510 * Start tracking the tid in the TCB history.
513 add_tid_to_history(struct adapter *sc, u_int tid)
515 struct tcb_histent *te = NULL;
516 struct tom_data *td = sc->tom_softc;
519 MPASS(tid < sc->tids.ntids);
521 if (td->tcb_history == NULL)
524 rw_wlock(&td->tcb_history_lock);
525 if (td->tcb_history[tid] != NULL) {
529 te = alloc_tcb_histent(sc, tid, M_NOWAIT);
534 mtx_lock(&te->te_lock);
535 rc = send_get_tcb(sc, tid);
537 te->te_flags |= TE_RPL_PENDING;
538 td->tcb_history[tid] = te;
542 mtx_unlock(&te->te_lock);
544 rw_wunlock(&td->tcb_history_lock);
549 remove_tcb_histent(struct tcb_histent *te)
551 struct adapter *sc = te->te_adapter;
552 struct tom_data *td = sc->tom_softc;
554 rw_assert(&td->tcb_history_lock, RA_WLOCKED);
555 mtx_assert(&te->te_lock, MA_OWNED);
556 MPASS(td->tcb_history[te->te_tid] == te);
558 td->tcb_history[te->te_tid] = NULL;
559 free_tcb_histent(te);
560 rw_wunlock(&td->tcb_history_lock);
563 static inline struct tcb_histent *
564 lookup_tcb_histent(struct adapter *sc, u_int tid, bool addrem)
566 struct tcb_histent *te;
567 struct tom_data *td = sc->tom_softc;
569 MPASS(tid < sc->tids.ntids);
571 if (td->tcb_history == NULL)
575 rw_wlock(&td->tcb_history_lock);
577 rw_rlock(&td->tcb_history_lock);
578 te = td->tcb_history[tid];
580 mtx_lock(&te->te_lock);
581 return (te); /* with both locks held */
584 rw_wunlock(&td->tcb_history_lock);
586 rw_runlock(&td->tcb_history_lock);
592 release_tcb_histent(struct tcb_histent *te)
594 struct adapter *sc = te->te_adapter;
595 struct tom_data *td = sc->tom_softc;
597 mtx_assert(&te->te_lock, MA_OWNED);
598 mtx_unlock(&te->te_lock);
599 rw_assert(&td->tcb_history_lock, RA_RLOCKED);
600 rw_runlock(&td->tcb_history_lock);
604 request_tcb(void *arg)
606 struct tcb_histent *te = arg;
608 mtx_assert(&te->te_lock, MA_OWNED);
610 /* Noone else is supposed to update the histent. */
611 MPASS(!(te->te_flags & TE_RPL_PENDING));
612 if (send_get_tcb(te->te_adapter, te->te_tid) == 0)
613 te->te_flags |= TE_RPL_PENDING;
615 callout_schedule(&te->te_callout, hz / 100);
619 update_tcb_histent(struct tcb_histent *te, const uint64_t *tcb)
621 struct tom_data *td = te->te_adapter->tom_softc;
622 uint64_t tflags = get_tcb_tflags(tcb);
625 if (GET_TCB_FIELD(tcb, SND_MAX_RAW) != GET_TCB_FIELD(tcb, SND_UNA_RAW)) {
626 if (GET_TCB_FIELD(tcb, T_RXTSHIFT) != 0)
628 if (GET_TCB_FIELD(tcb, T_DUPACKS) != 0)
629 sample |= TS_DUPACKS;
630 if (GET_TCB_FIELD(tcb, T_DUPACKS) >= td->dupack_threshold)
631 sample |= TS_FASTREXMT;
634 if (GET_TCB_FIELD(tcb, SND_MAX_RAW) != 0) {
637 sample |= TS_SND_BACKLOGGED; /* for whatever reason. */
639 snd_wnd = GET_TCB_FIELD(tcb, RCV_ADV);
640 if (tflags & V_TF_RECV_SCALE(1))
641 snd_wnd <<= GET_TCB_FIELD(tcb, RCV_SCALE);
642 if (GET_TCB_FIELD(tcb, SND_CWND) < snd_wnd)
643 sample |= TS_CWND_LIMITED; /* maybe due to CWND */
646 if (tflags & V_TF_CCTRL_ECN(1)) {
649 * CE marker on incoming IP hdr, echoing ECE back in the TCP
650 * hdr. Indicates congestion somewhere on the way from the peer
653 if (tflags & V_TF_CCTRL_ECE(1))
654 sample |= TS_ECN_ECE;
657 * ECE seen and CWR sent (or about to be sent). Might indicate
658 * congestion on the way to the peer. This node is reducing its
659 * congestion window in response.
661 if (tflags & (V_TF_CCTRL_CWR(1) | V_TF_CCTRL_RFR(1)))
662 sample |= TS_ECN_CWR;
665 te->te_sample[te->te_pidx] = sample;
666 if (++te->te_pidx == nitems(te->te_sample))
668 memcpy(te->te_tcb, tcb, TCB_SIZE);
669 te->te_flags |= TE_ACTIVE;
673 do_get_tcb_rpl(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
675 struct adapter *sc = iq->adapter;
676 const struct cpl_get_tcb_rpl *cpl = mtod(m, const void *);
677 const uint64_t *tcb = (const uint64_t *)(const void *)(cpl + 1);
678 struct tcb_histent *te;
679 const u_int tid = GET_TID(cpl);
682 remove = GET_TCB_FIELD(tcb, T_STATE) == TCPS_CLOSED;
683 te = lookup_tcb_histent(sc, tid, remove);
685 /* Not in the history. Who issued the GET_TCB for this? */
686 device_printf(sc->dev, "tcb %u: flags 0x%016jx, state %u, "
687 "srtt %u, sscale %u, rscale %u, cookie 0x%x\n", tid,
688 (uintmax_t)get_tcb_tflags(tcb), GET_TCB_FIELD(tcb, T_STATE),
689 GET_TCB_FIELD(tcb, T_SRTT), GET_TCB_FIELD(tcb, SND_SCALE),
690 GET_TCB_FIELD(tcb, RCV_SCALE), cpl->cookie);
694 MPASS(te->te_flags & TE_RPL_PENDING);
695 te->te_flags &= ~TE_RPL_PENDING;
697 remove_tcb_histent(te);
699 update_tcb_histent(te, tcb);
700 callout_reset(&te->te_callout, hz / 10, request_tcb, te);
701 release_tcb_histent(te);
709 fill_tcp_info_from_tcb(struct adapter *sc, uint64_t *tcb, struct tcp_info *ti)
713 ti->tcpi_state = GET_TCB_FIELD(tcb, T_STATE);
715 v = GET_TCB_FIELD(tcb, T_SRTT);
716 ti->tcpi_rtt = tcp_ticks_to_us(sc, v);
718 v = GET_TCB_FIELD(tcb, T_RTTVAR);
719 ti->tcpi_rttvar = tcp_ticks_to_us(sc, v);
721 ti->tcpi_snd_ssthresh = GET_TCB_FIELD(tcb, SND_SSTHRESH);
722 ti->tcpi_snd_cwnd = GET_TCB_FIELD(tcb, SND_CWND);
723 ti->tcpi_rcv_nxt = GET_TCB_FIELD(tcb, RCV_NXT);
725 v = GET_TCB_FIELD(tcb, TX_MAX);
726 ti->tcpi_snd_nxt = v - GET_TCB_FIELD(tcb, SND_NXT_RAW);
728 /* Receive window being advertised by us. */
729 ti->tcpi_rcv_wscale = GET_TCB_FIELD(tcb, SND_SCALE); /* Yes, SND. */
730 ti->tcpi_rcv_space = GET_TCB_FIELD(tcb, RCV_WND);
733 ti->tcpi_snd_wscale = GET_TCB_FIELD(tcb, RCV_SCALE); /* Yes, RCV. */
734 ti->tcpi_snd_wnd = GET_TCB_FIELD(tcb, RCV_ADV);
735 if (get_tcb_tflags(tcb) & V_TF_RECV_SCALE(1))
736 ti->tcpi_snd_wnd <<= ti->tcpi_snd_wscale;
738 ti->tcpi_snd_wscale = 0;
743 fill_tcp_info_from_history(struct adapter *sc, struct tcb_histent *te,
747 fill_tcp_info_from_tcb(sc, te->te_tcb, ti);
751 * Reads the TCB for the given tid using a memory window and copies it to 'buf'
752 * in the same format as CPL_GET_TCB_RPL.
755 read_tcb_using_memwin(struct adapter *sc, u_int tid, uint64_t *buf)
761 MPASS(tid < sc->tids.ntids);
763 addr = t4_read_reg(sc, A_TP_CMM_TCB_BASE) + tid * TCB_SIZE;
764 rc = read_via_memwin(sc, 2, addr, (uint32_t *)buf, TCB_SIZE);
769 for (i = 0, j = TCB_SIZE - 16; i < j; i += 16, j -= 16) {
770 for (k = 0; k < 16; k++) {
772 tcb[i + k] = tcb[j + k];
779 fill_tcp_info(struct adapter *sc, u_int tid, struct tcp_info *ti)
781 uint64_t tcb[TCB_SIZE / sizeof(uint64_t)];
782 struct tcb_histent *te;
784 ti->tcpi_toe_tid = tid;
785 te = lookup_tcb_histent(sc, tid, false);
787 fill_tcp_info_from_history(sc, te, ti);
788 release_tcb_histent(te);
790 if (!(sc->debug_flags & DF_DISABLE_TCB_CACHE)) {
791 /* XXX: tell firmware to flush TCB cache. */
793 read_tcb_using_memwin(sc, tid, tcb);
794 fill_tcp_info_from_tcb(sc, tcb, ti);
799 * Called by the kernel to allow the TOE driver to "refine" values filled up in
800 * the tcp_info for an offloaded connection.
803 t4_tcp_info(struct toedev *tod, struct tcpcb *tp, struct tcp_info *ti)
805 struct adapter *sc = tod->tod_softc;
806 struct toepcb *toep = tp->t_toe;
808 INP_WLOCK_ASSERT(tp->t_inpcb);
811 fill_tcp_info(sc, toep->tid, ti);
816 t4_alloc_tls_session(struct toedev *tod, struct tcpcb *tp,
817 struct ktls_session *tls)
819 struct toepcb *toep = tp->t_toe;
821 INP_WLOCK_ASSERT(tp->t_inpcb);
824 return (tls_alloc_ktls(toep, tls));
829 * The TOE driver will not receive any more CPLs for the tid associated with the
830 * toepcb; release the hold on the inpcb.
833 final_cpl_received(struct toepcb *toep)
835 struct inpcb *inp = toep->inp;
837 KASSERT(inp != NULL, ("%s: inp is NULL", __func__));
838 INP_WLOCK_ASSERT(inp);
839 KASSERT(toep->flags & TPF_CPL_PENDING,
840 ("%s: CPL not pending already?", __func__));
842 CTR6(KTR_CXGBE, "%s: tid %d, toep %p (0x%x), inp %p (0x%x)",
843 __func__, toep->tid, toep, toep->flags, inp, inp->inp_flags);
845 if (ulp_mode(toep) == ULP_MODE_TCPDDP)
846 release_ddp_resources(toep);
848 toep->flags &= ~TPF_CPL_PENDING;
849 mbufq_drain(&toep->ulp_pdu_reclaimq);
851 if (!(toep->flags & TPF_ATTACHED))
852 release_offload_resources(toep);
854 if (!in_pcbrele_wlocked(inp))
859 insert_tid(struct adapter *sc, int tid, void *ctx, int ntids)
861 struct tid_info *t = &sc->tids;
863 MPASS(tid >= t->tid_base);
864 MPASS(tid - t->tid_base < t->ntids);
866 t->tid_tab[tid - t->tid_base] = ctx;
867 atomic_add_int(&t->tids_in_use, ntids);
871 lookup_tid(struct adapter *sc, int tid)
873 struct tid_info *t = &sc->tids;
875 return (t->tid_tab[tid - t->tid_base]);
879 update_tid(struct adapter *sc, int tid, void *ctx)
881 struct tid_info *t = &sc->tids;
883 t->tid_tab[tid - t->tid_base] = ctx;
887 remove_tid(struct adapter *sc, int tid, int ntids)
889 struct tid_info *t = &sc->tids;
891 t->tid_tab[tid - t->tid_base] = NULL;
892 atomic_subtract_int(&t->tids_in_use, ntids);
896 * What mtu_idx to use, given a 4-tuple. Note that both s->mss and tcp_mssopt
897 * have the MSS that we should advertise in our SYN. Advertised MSS doesn't
898 * account for any TCP options so the effective MSS (only payload, no headers or
899 * options) could be different.
902 find_best_mtu_idx(struct adapter *sc, struct in_conninfo *inc,
903 struct offload_settings *s)
905 unsigned short *mtus = &sc->params.mtus[0];
910 mss = s->mss > 0 ? s->mss : tcp_mssopt(inc);
911 if (inc->inc_flags & INC_ISIPV6)
912 mtu = mss + sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
914 mtu = mss + sizeof(struct ip) + sizeof(struct tcphdr);
916 for (i = 0; i < NMTUS - 1 && mtus[i + 1] <= mtu; i++)
923 * Determine the receive window size for a socket.
926 select_rcv_wnd(struct socket *so)
930 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
932 wnd = sbspace(&so->so_rcv);
933 if (wnd < MIN_RCV_WND)
936 return min(wnd, MAX_RCV_WND);
940 select_rcv_wscale(void)
943 unsigned long space = sb_max;
945 if (space > MAX_RCV_WND)
948 while (wscale < TCP_MAX_WINSHIFT && (TCP_MAXWIN << wscale) < space)
955 calc_options0(struct vi_info *vi, struct conn_params *cp)
959 opt0 |= F_TCAM_BYPASS;
961 MPASS(cp->wscale >= 0 && cp->wscale <= M_WND_SCALE);
962 opt0 |= V_WND_SCALE(cp->wscale);
964 MPASS(cp->mtu_idx >= 0 && cp->mtu_idx < NMTUS);
965 opt0 |= V_MSS_IDX(cp->mtu_idx);
967 MPASS(cp->ulp_mode >= 0 && cp->ulp_mode <= M_ULP_MODE);
968 opt0 |= V_ULP_MODE(cp->ulp_mode);
970 MPASS(cp->opt0_bufsize >= 0 && cp->opt0_bufsize <= M_RCV_BUFSIZ);
971 opt0 |= V_RCV_BUFSIZ(cp->opt0_bufsize);
973 MPASS(cp->l2t_idx >= 0 && cp->l2t_idx < vi->pi->adapter->vres.l2t.size);
974 opt0 |= V_L2T_IDX(cp->l2t_idx);
976 opt0 |= V_SMAC_SEL(vi->smt_idx);
977 opt0 |= V_TX_CHAN(vi->pi->tx_chan);
979 MPASS(cp->keepalive == 0 || cp->keepalive == 1);
980 opt0 |= V_KEEP_ALIVE(cp->keepalive);
982 MPASS(cp->nagle == 0 || cp->nagle == 1);
983 opt0 |= V_NAGLE(cp->nagle);
985 return (htobe64(opt0));
989 calc_options2(struct vi_info *vi, struct conn_params *cp)
992 struct port_info *pi = vi->pi;
993 struct adapter *sc = pi->adapter;
996 * rx flow control, rx coalesce, congestion control, and tx pace are all
997 * explicitly set by the driver. On T5+ the ISS is also set by the
998 * driver to the value picked by the kernel.
1001 opt2 |= F_RX_FC_VALID | F_RX_COALESCE_VALID;
1002 opt2 |= F_CONG_CNTRL_VALID | F_PACE_VALID;
1004 opt2 |= F_T5_OPT_2_VALID; /* all 4 valid */
1005 opt2 |= F_T5_ISS; /* ISS provided in CPL */
1008 MPASS(cp->sack == 0 || cp->sack == 1);
1009 opt2 |= V_SACK_EN(cp->sack);
1011 MPASS(cp->tstamp == 0 || cp->tstamp == 1);
1012 opt2 |= V_TSTAMPS_EN(cp->tstamp);
1015 opt2 |= F_WND_SCALE_EN;
1017 MPASS(cp->ecn == 0 || cp->ecn == 1);
1018 opt2 |= V_CCTRL_ECN(cp->ecn);
1020 /* XXX: F_RX_CHANNEL for multiple rx c-chan support goes here. */
1022 opt2 |= V_TX_QUEUE(sc->params.tp.tx_modq[pi->tx_chan]);
1024 opt2 |= F_RSS_QUEUE_VALID;
1025 opt2 |= V_RSS_QUEUE(sc->sge.ofld_rxq[cp->rxq_idx].iq.abs_id);
1027 MPASS(cp->cong_algo >= 0 && cp->cong_algo <= M_CONG_CNTRL);
1028 opt2 |= V_CONG_CNTRL(cp->cong_algo);
1030 MPASS(cp->rx_coalesce == 0 || cp->rx_coalesce == 1);
1031 if (cp->rx_coalesce == 1)
1032 opt2 |= V_RX_COALESCE(M_RX_COALESCE);
1034 opt2 |= V_RX_FC_DDP(0) | V_RX_FC_DISABLE(0);
1035 #ifdef USE_DDP_RX_FLOW_CONTROL
1036 if (cp->ulp_mode == ULP_MODE_TCPDDP)
1037 opt2 |= F_RX_FC_DDP;
1039 if (cp->ulp_mode == ULP_MODE_TLS)
1040 opt2 |= F_RX_FC_DISABLE;
1042 return (htobe32(opt2));
1046 select_ntuple(struct vi_info *vi, struct l2t_entry *e)
1048 struct adapter *sc = vi->pi->adapter;
1049 struct tp_params *tp = &sc->params.tp;
1050 uint64_t ntuple = 0;
1053 * Initialize each of the fields which we care about which are present
1054 * in the Compressed Filter Tuple.
1056 if (tp->vlan_shift >= 0 && EVL_VLANOFTAG(e->vlan) != CPL_L2T_VLAN_NONE)
1057 ntuple |= (uint64_t)(F_FT_VLAN_VLD | e->vlan) << tp->vlan_shift;
1059 if (tp->port_shift >= 0)
1060 ntuple |= (uint64_t)e->lport << tp->port_shift;
1062 if (tp->protocol_shift >= 0)
1063 ntuple |= (uint64_t)IPPROTO_TCP << tp->protocol_shift;
1065 if (tp->vnic_shift >= 0 && tp->ingress_config & F_VNIC) {
1066 ntuple |= (uint64_t)(V_FT_VNID_ID_VF(vi->vin) |
1067 V_FT_VNID_ID_PF(sc->pf) | V_FT_VNID_ID_VLD(vi->vfvld)) <<
1072 return (htobe32((uint32_t)ntuple));
1074 return (htobe64(V_FILTER_TUPLE(ntuple)));
1078 is_tls_sock(struct socket *so, struct adapter *sc)
1080 struct inpcb *inp = sotoinpcb(so);
1083 /* XXX: Eventually add a SO_WANT_TLS socket option perhaps? */
1086 for (i = 0; i < sc->tt.num_tls_rx_ports; i++) {
1087 if (inp->inp_lport == htons(sc->tt.tls_rx_ports[i]) ||
1088 inp->inp_fport == htons(sc->tt.tls_rx_ports[i])) {
1098 * Initialize various connection parameters.
1101 init_conn_params(struct vi_info *vi , struct offload_settings *s,
1102 struct in_conninfo *inc, struct socket *so,
1103 const struct tcp_options *tcpopt, int16_t l2t_idx, struct conn_params *cp)
1105 struct port_info *pi = vi->pi;
1106 struct adapter *sc = pi->adapter;
1107 struct tom_tunables *tt = &sc->tt;
1108 struct inpcb *inp = sotoinpcb(so);
1109 struct tcpcb *tp = intotcpcb(inp);
1112 MPASS(s->offload != 0);
1114 /* Congestion control algorithm */
1115 if (s->cong_algo >= 0)
1116 cp->cong_algo = s->cong_algo & M_CONG_CNTRL;
1117 else if (sc->tt.cong_algorithm >= 0)
1118 cp->cong_algo = tt->cong_algorithm & M_CONG_CNTRL;
1120 struct cc_algo *cc = CC_ALGO(tp);
1122 if (strcasecmp(cc->name, "reno") == 0)
1123 cp->cong_algo = CONG_ALG_RENO;
1124 else if (strcasecmp(cc->name, "tahoe") == 0)
1125 cp->cong_algo = CONG_ALG_TAHOE;
1126 if (strcasecmp(cc->name, "newreno") == 0)
1127 cp->cong_algo = CONG_ALG_NEWRENO;
1128 if (strcasecmp(cc->name, "highspeed") == 0)
1129 cp->cong_algo = CONG_ALG_HIGHSPEED;
1132 * Use newreno in case the algorithm selected by the
1133 * host stack is not supported by the hardware.
1135 cp->cong_algo = CONG_ALG_NEWRENO;
1139 /* Tx traffic scheduling class. */
1140 if (s->sched_class >= 0 &&
1141 s->sched_class < sc->chip_params->nsched_cls) {
1142 cp->tc_idx = s->sched_class;
1146 /* Nagle's algorithm. */
1148 cp->nagle = s->nagle > 0 ? 1 : 0;
1150 cp->nagle = tp->t_flags & TF_NODELAY ? 0 : 1;
1152 /* TCP Keepalive. */
1153 if (V_tcp_always_keepalive || so_options_get(so) & SO_KEEPALIVE)
1158 /* Optimization that's specific to T5 @ 40G. */
1159 if (tt->tx_align >= 0)
1160 cp->tx_align = tt->tx_align > 0 ? 1 : 0;
1161 else if (chip_id(sc) == CHELSIO_T5 &&
1162 (port_top_speed(pi) > 10 || sc->params.nports > 2))
1168 if (can_tls_offload(sc) &&
1169 (s->tls > 0 || (s->tls < 0 && is_tls_sock(so, sc))))
1170 cp->ulp_mode = ULP_MODE_TLS;
1171 else if (s->ddp > 0 ||
1172 (s->ddp < 0 && sc->tt.ddp && (so_options_get(so) & SO_NO_DDP) == 0))
1173 cp->ulp_mode = ULP_MODE_TCPDDP;
1175 cp->ulp_mode = ULP_MODE_NONE;
1177 /* Rx coalescing. */
1178 if (s->rx_coalesce >= 0)
1179 cp->rx_coalesce = s->rx_coalesce > 0 ? 1 : 0;
1180 else if (cp->ulp_mode == ULP_MODE_TLS)
1181 cp->rx_coalesce = 0;
1182 else if (tt->rx_coalesce >= 0)
1183 cp->rx_coalesce = tt->rx_coalesce > 0 ? 1 : 0;
1185 cp->rx_coalesce = 1; /* default */
1188 * Index in the PMTU table. This controls the MSS that we announce in
1189 * our SYN initially, but after ESTABLISHED it controls the MSS that we
1192 cp->mtu_idx = find_best_mtu_idx(sc, inc, s);
1194 /* Tx queue for this connection. */
1195 if (s->txq >= 0 && s->txq < vi->nofldtxq)
1196 cp->txq_idx = s->txq;
1198 cp->txq_idx = arc4random() % vi->nofldtxq;
1199 cp->txq_idx += vi->first_ofld_txq;
1201 /* Rx queue for this connection. */
1202 if (s->rxq >= 0 && s->rxq < vi->nofldrxq)
1203 cp->rxq_idx = s->rxq;
1205 cp->rxq_idx = arc4random() % vi->nofldrxq;
1206 cp->rxq_idx += vi->first_ofld_rxq;
1208 if (SOLISTENING(so)) {
1210 MPASS(tcpopt != NULL);
1212 /* TCP timestamp option */
1213 if (tcpopt->tstamp &&
1214 (s->tstamp > 0 || (s->tstamp < 0 && V_tcp_do_rfc1323)))
1221 (s->sack > 0 || (s->sack < 0 && V_tcp_do_sack)))
1226 /* Receive window scaling. */
1227 if (tcpopt->wsf > 0 && tcpopt->wsf < 15 && V_tcp_do_rfc1323)
1228 cp->wscale = select_rcv_wscale();
1233 if (tcpopt->ecn && /* XXX: review. */
1234 (s->ecn > 0 || (s->ecn < 0 && V_tcp_do_ecn)))
1239 wnd = max(so->sol_sbrcv_hiwat, MIN_RCV_WND);
1240 cp->opt0_bufsize = min(wnd >> 10, M_RCV_BUFSIZ);
1243 cp->sndbuf = tt->sndbuf;
1244 else if (so->sol_sbsnd_flags & SB_AUTOSIZE &&
1245 V_tcp_do_autosndbuf)
1246 cp->sndbuf = 256 * 1024;
1248 cp->sndbuf = so->sol_sbsnd_hiwat;
1252 /* TCP timestamp option */
1253 if (s->tstamp > 0 ||
1254 (s->tstamp < 0 && (tp->t_flags & TF_REQ_TSTMP)))
1261 (s->sack < 0 && (tp->t_flags & TF_SACK_PERMIT)))
1266 /* Receive window scaling */
1267 if (tp->t_flags & TF_REQ_SCALE)
1268 cp->wscale = select_rcv_wscale();
1273 if (s->ecn > 0 || (s->ecn < 0 && V_tcp_do_ecn == 1))
1278 SOCKBUF_LOCK(&so->so_rcv);
1279 wnd = max(select_rcv_wnd(so), MIN_RCV_WND);
1280 SOCKBUF_UNLOCK(&so->so_rcv);
1281 cp->opt0_bufsize = min(wnd >> 10, M_RCV_BUFSIZ);
1284 cp->sndbuf = tt->sndbuf;
1286 SOCKBUF_LOCK(&so->so_snd);
1287 if (so->so_snd.sb_flags & SB_AUTOSIZE &&
1288 V_tcp_do_autosndbuf)
1289 cp->sndbuf = 256 * 1024;
1291 cp->sndbuf = so->so_snd.sb_hiwat;
1292 SOCKBUF_UNLOCK(&so->so_snd);
1296 cp->l2t_idx = l2t_idx;
1298 /* This will be initialized on ESTABLISHED. */
1303 negative_advice(int status)
1306 return (status == CPL_ERR_RTX_NEG_ADVICE ||
1307 status == CPL_ERR_PERSIST_NEG_ADVICE ||
1308 status == CPL_ERR_KEEPALV_NEG_ADVICE);
1312 alloc_tid_tab(struct tid_info *t, int flags)
1315 MPASS(t->ntids > 0);
1316 MPASS(t->tid_tab == NULL);
1318 t->tid_tab = malloc(t->ntids * sizeof(*t->tid_tab), M_CXGBE,
1320 if (t->tid_tab == NULL)
1322 atomic_store_rel_int(&t->tids_in_use, 0);
1328 free_tid_tab(struct tid_info *t)
1331 KASSERT(t->tids_in_use == 0,
1332 ("%s: %d tids still in use.", __func__, t->tids_in_use));
1334 free(t->tid_tab, M_CXGBE);
1339 alloc_stid_tab(struct tid_info *t, int flags)
1342 MPASS(t->nstids > 0);
1343 MPASS(t->stid_tab == NULL);
1345 t->stid_tab = malloc(t->nstids * sizeof(*t->stid_tab), M_CXGBE,
1347 if (t->stid_tab == NULL)
1349 mtx_init(&t->stid_lock, "stid lock", NULL, MTX_DEF);
1350 t->stids_in_use = 0;
1351 TAILQ_INIT(&t->stids);
1352 t->nstids_free_head = t->nstids;
1358 free_stid_tab(struct tid_info *t)
1361 KASSERT(t->stids_in_use == 0,
1362 ("%s: %d tids still in use.", __func__, t->stids_in_use));
1364 if (mtx_initialized(&t->stid_lock))
1365 mtx_destroy(&t->stid_lock);
1366 free(t->stid_tab, M_CXGBE);
1371 free_tid_tabs(struct tid_info *t)
1379 alloc_tid_tabs(struct tid_info *t)
1383 rc = alloc_tid_tab(t, M_NOWAIT);
1387 rc = alloc_stid_tab(t, M_NOWAIT);
1398 alloc_tcb_history(struct adapter *sc, struct tom_data *td)
1401 if (sc->tids.ntids == 0 || sc->tids.ntids > 1024)
1403 rw_init(&td->tcb_history_lock, "TCB history");
1404 td->tcb_history = malloc(sc->tids.ntids * sizeof(*td->tcb_history),
1405 M_CXGBE, M_ZERO | M_NOWAIT);
1406 td->dupack_threshold = G_DUPACKTHRESH(t4_read_reg(sc, A_TP_PARA_REG0));
1410 free_tcb_history(struct adapter *sc, struct tom_data *td)
1415 if (td->tcb_history != NULL) {
1416 for (i = 0; i < sc->tids.ntids; i++) {
1417 MPASS(td->tcb_history[i] == NULL);
1421 free(td->tcb_history, M_CXGBE);
1422 if (rw_initialized(&td->tcb_history_lock))
1423 rw_destroy(&td->tcb_history_lock);
1427 free_tom_data(struct adapter *sc, struct tom_data *td)
1430 ASSERT_SYNCHRONIZED_OP(sc);
1432 KASSERT(TAILQ_EMPTY(&td->toep_list),
1433 ("%s: TOE PCB list is not empty.", __func__));
1434 KASSERT(td->lctx_count == 0,
1435 ("%s: lctx hash table is not empty.", __func__));
1437 t4_free_ppod_region(&td->pr);
1439 if (td->listen_mask != 0)
1440 hashdestroy(td->listen_hash, M_CXGBE, td->listen_mask);
1442 if (mtx_initialized(&td->unsent_wr_lock))
1443 mtx_destroy(&td->unsent_wr_lock);
1444 if (mtx_initialized(&td->lctx_hash_lock))
1445 mtx_destroy(&td->lctx_hash_lock);
1446 if (mtx_initialized(&td->toep_list_lock))
1447 mtx_destroy(&td->toep_list_lock);
1449 free_tcb_history(sc, td);
1450 free_tid_tabs(&sc->tids);
1455 prepare_pkt(int open_type, uint16_t vtag, struct inpcb *inp, int *pktlen,
1462 max(sizeof(struct ether_header), sizeof(struct ether_vlan_header)) +
1463 max(sizeof(struct ip), sizeof(struct ip6_hdr)) +
1464 sizeof(struct tcphdr);
1466 MPASS(open_type == OPEN_TYPE_ACTIVE || open_type == OPEN_TYPE_LISTEN);
1468 pkt = malloc(maxlen, M_CXGBE, M_ZERO | M_NOWAIT);
1472 ipv6 = inp->inp_vflag & INP_IPV6;
1475 if (EVL_VLANOFTAG(vtag) == 0xfff) {
1476 struct ether_header *eh = (void *)pkt;
1479 eh->ether_type = htons(ETHERTYPE_IPV6);
1481 eh->ether_type = htons(ETHERTYPE_IP);
1485 struct ether_vlan_header *evh = (void *)pkt;
1487 evh->evl_encap_proto = htons(ETHERTYPE_VLAN);
1488 evh->evl_tag = htons(vtag);
1490 evh->evl_proto = htons(ETHERTYPE_IPV6);
1492 evh->evl_proto = htons(ETHERTYPE_IP);
1494 len += sizeof(*evh);
1498 struct ip6_hdr *ip6 = (void *)&pkt[len];
1500 ip6->ip6_vfc = IPV6_VERSION;
1501 ip6->ip6_plen = htons(sizeof(struct tcphdr));
1502 ip6->ip6_nxt = IPPROTO_TCP;
1503 if (open_type == OPEN_TYPE_ACTIVE) {
1504 ip6->ip6_src = inp->in6p_laddr;
1505 ip6->ip6_dst = inp->in6p_faddr;
1506 } else if (open_type == OPEN_TYPE_LISTEN) {
1507 ip6->ip6_src = inp->in6p_laddr;
1508 ip6->ip6_dst = ip6->ip6_src;
1511 len += sizeof(*ip6);
1513 struct ip *ip = (void *)&pkt[len];
1515 ip->ip_v = IPVERSION;
1516 ip->ip_hl = sizeof(*ip) >> 2;
1517 ip->ip_tos = inp->inp_ip_tos;
1518 ip->ip_len = htons(sizeof(struct ip) + sizeof(struct tcphdr));
1519 ip->ip_ttl = inp->inp_ip_ttl;
1520 ip->ip_p = IPPROTO_TCP;
1521 if (open_type == OPEN_TYPE_ACTIVE) {
1522 ip->ip_src = inp->inp_laddr;
1523 ip->ip_dst = inp->inp_faddr;
1524 } else if (open_type == OPEN_TYPE_LISTEN) {
1525 ip->ip_src = inp->inp_laddr;
1526 ip->ip_dst = ip->ip_src;
1532 th = (void *)&pkt[len];
1533 if (open_type == OPEN_TYPE_ACTIVE) {
1534 th->th_sport = inp->inp_lport; /* network byte order already */
1535 th->th_dport = inp->inp_fport; /* ditto */
1536 } else if (open_type == OPEN_TYPE_LISTEN) {
1537 th->th_sport = inp->inp_lport; /* network byte order already */
1538 th->th_dport = th->th_sport;
1542 *pktlen = *buflen = len;
1546 const struct offload_settings *
1547 lookup_offload_policy(struct adapter *sc, int open_type, struct mbuf *m,
1548 uint16_t vtag, struct inpcb *inp)
1550 const struct t4_offload_policy *op;
1552 struct offload_rule *r;
1553 int i, matched, pktlen, buflen;
1554 static const struct offload_settings allow_offloading_settings = {
1569 static const struct offload_settings disallow_offloading_settings = {
1571 /* rest is irrelevant when offload is off. */
1574 rw_assert(&sc->policy_lock, RA_LOCKED);
1577 * If there's no Connection Offloading Policy attached to the device
1578 * then we need to return a default static policy. If
1579 * "cop_managed_offloading" is true, then we need to disallow
1580 * offloading until a COP is attached to the device. Otherwise we
1581 * allow offloading ...
1585 if (sc->tt.cop_managed_offloading)
1586 return (&disallow_offloading_settings);
1588 return (&allow_offloading_settings);
1591 switch (open_type) {
1592 case OPEN_TYPE_ACTIVE:
1593 case OPEN_TYPE_LISTEN:
1594 pkt = prepare_pkt(open_type, vtag, inp, &pktlen, &buflen);
1596 case OPEN_TYPE_PASSIVE:
1598 pkt = mtod(m, char *);
1599 MPASS(*pkt == CPL_PASS_ACCEPT_REQ);
1600 pkt += sizeof(struct cpl_pass_accept_req);
1601 pktlen = m->m_pkthdr.len - sizeof(struct cpl_pass_accept_req);
1602 buflen = m->m_len - sizeof(struct cpl_pass_accept_req);
1606 return (&disallow_offloading_settings);
1609 if (pkt == NULL || pktlen == 0 || buflen == 0)
1610 return (&disallow_offloading_settings);
1614 for (i = 0; i < op->nrules; i++, r++) {
1615 if (r->open_type != open_type &&
1616 r->open_type != OPEN_TYPE_DONTCARE) {
1619 matched = bpf_filter(r->bpf_prog.bf_insns, pkt, pktlen, buflen);
1624 if (open_type == OPEN_TYPE_ACTIVE || open_type == OPEN_TYPE_LISTEN)
1627 return (matched ? &r->settings : &disallow_offloading_settings);
1631 reclaim_wr_resources(void *arg, int count)
1633 struct tom_data *td = arg;
1634 STAILQ_HEAD(, wrqe) twr_list = STAILQ_HEAD_INITIALIZER(twr_list);
1635 struct cpl_act_open_req *cpl;
1636 u_int opcode, atid, tid;
1638 struct adapter *sc = td_adapter(td);
1640 mtx_lock(&td->unsent_wr_lock);
1641 STAILQ_SWAP(&td->unsent_wr_list, &twr_list, wrqe);
1642 mtx_unlock(&td->unsent_wr_lock);
1644 while ((wr = STAILQ_FIRST(&twr_list)) != NULL) {
1645 STAILQ_REMOVE_HEAD(&twr_list, link);
1648 opcode = GET_OPCODE(cpl);
1651 case CPL_ACT_OPEN_REQ:
1652 case CPL_ACT_OPEN_REQ6:
1653 atid = G_TID_TID(be32toh(OPCODE_TID(cpl)));
1654 CTR2(KTR_CXGBE, "%s: atid %u ", __func__, atid);
1655 act_open_failure_cleanup(sc, atid, EHOSTUNREACH);
1658 case CPL_PASS_ACCEPT_RPL:
1660 CTR2(KTR_CXGBE, "%s: tid %u ", __func__, tid);
1661 synack_failure_cleanup(sc, tid);
1665 log(LOG_ERR, "%s: leaked work request %p, wr_len %d, "
1666 "opcode %x\n", __func__, wr, wr->wr_len, opcode);
1667 /* WR not freed here; go look at it with a debugger. */
1673 * Ground control to Major TOM
1674 * Commencing countdown, engines on
1677 t4_tom_activate(struct adapter *sc)
1679 struct tom_data *td;
1684 ASSERT_SYNCHRONIZED_OP(sc);
1686 /* per-adapter softc for TOM */
1687 td = malloc(sizeof(*td), M_CXGBE, M_ZERO | M_NOWAIT);
1691 /* List of TOE PCBs and associated lock */
1692 mtx_init(&td->toep_list_lock, "PCB list lock", NULL, MTX_DEF);
1693 TAILQ_INIT(&td->toep_list);
1695 /* Listen context */
1696 mtx_init(&td->lctx_hash_lock, "lctx hash lock", NULL, MTX_DEF);
1697 td->listen_hash = hashinit_flags(LISTEN_HASH_SIZE, M_CXGBE,
1698 &td->listen_mask, HASH_NOWAIT);
1700 /* List of WRs for which L2 resolution failed */
1701 mtx_init(&td->unsent_wr_lock, "Unsent WR list lock", NULL, MTX_DEF);
1702 STAILQ_INIT(&td->unsent_wr_list);
1703 TASK_INIT(&td->reclaim_wr_resources, 0, reclaim_wr_resources, td);
1706 rc = alloc_tid_tabs(&sc->tids);
1710 rc = t4_init_ppod_region(&td->pr, &sc->vres.ddp,
1711 t4_read_reg(sc, A_ULP_RX_TDDP_PSZ), "TDDP page pods");
1714 t4_set_reg_field(sc, A_ULP_RX_TDDP_TAGMASK,
1715 V_TDDPTAGMASK(M_TDDPTAGMASK), td->pr.pr_tag_mask);
1717 alloc_tcb_history(sc, td);
1722 tod->tod_softc = sc;
1723 tod->tod_connect = t4_connect;
1724 tod->tod_listen_start = t4_listen_start;
1725 tod->tod_listen_stop = t4_listen_stop;
1726 tod->tod_rcvd = t4_rcvd;
1727 tod->tod_output = t4_tod_output;
1728 tod->tod_send_rst = t4_send_rst;
1729 tod->tod_send_fin = t4_send_fin;
1730 tod->tod_pcb_detach = t4_pcb_detach;
1731 tod->tod_l2_update = t4_l2_update;
1732 tod->tod_syncache_added = t4_syncache_added;
1733 tod->tod_syncache_removed = t4_syncache_removed;
1734 tod->tod_syncache_respond = t4_syncache_respond;
1735 tod->tod_offload_socket = t4_offload_socket;
1736 tod->tod_ctloutput = t4_ctloutput;
1737 tod->tod_tcp_info = t4_tcp_info;
1739 tod->tod_alloc_tls_session = t4_alloc_tls_session;
1742 for_each_port(sc, i) {
1743 for_each_vi(sc->port[i], v, vi) {
1744 TOEDEV(vi->ifp) = &td->tod;
1749 register_toedev(sc->tom_softc);
1753 free_tom_data(sc, td);
1758 t4_tom_deactivate(struct adapter *sc)
1761 struct tom_data *td = sc->tom_softc;
1763 ASSERT_SYNCHRONIZED_OP(sc);
1766 return (0); /* XXX. KASSERT? */
1768 if (sc->offload_map != 0)
1769 return (EBUSY); /* at least one port has IFCAP_TOE enabled */
1771 if (uld_active(sc, ULD_IWARP) || uld_active(sc, ULD_ISCSI))
1772 return (EBUSY); /* both iWARP and iSCSI rely on the TOE. */
1774 mtx_lock(&td->toep_list_lock);
1775 if (!TAILQ_EMPTY(&td->toep_list))
1777 mtx_unlock(&td->toep_list_lock);
1779 mtx_lock(&td->lctx_hash_lock);
1780 if (td->lctx_count > 0)
1782 mtx_unlock(&td->lctx_hash_lock);
1784 taskqueue_drain(taskqueue_thread, &td->reclaim_wr_resources);
1785 mtx_lock(&td->unsent_wr_lock);
1786 if (!STAILQ_EMPTY(&td->unsent_wr_list))
1788 mtx_unlock(&td->unsent_wr_lock);
1791 unregister_toedev(sc->tom_softc);
1792 free_tom_data(sc, td);
1793 sc->tom_softc = NULL;
1800 t4_aio_queue_tom(struct socket *so, struct kaiocb *job)
1802 struct tcpcb *tp = so_sototcpcb(so);
1803 struct toepcb *toep = tp->t_toe;
1806 if (ulp_mode(toep) == ULP_MODE_TCPDDP) {
1807 error = t4_aio_queue_ddp(so, job);
1808 if (error != EOPNOTSUPP)
1812 return (t4_aio_queue_aiotx(so, job));
1816 t4_ctloutput_tom(struct socket *so, struct sockopt *sopt)
1819 if (sopt->sopt_level != IPPROTO_TCP)
1820 return (tcp_ctloutput(so, sopt));
1822 switch (sopt->sopt_name) {
1823 case TCP_TLSOM_SET_TLS_CONTEXT:
1824 case TCP_TLSOM_GET_TLS_TOM:
1825 case TCP_TLSOM_CLR_TLS_TOM:
1826 case TCP_TLSOM_CLR_QUIES:
1827 return (t4_ctloutput_tls(so, sopt));
1829 return (tcp_ctloutput(so, sopt));
1834 t4_tom_mod_load(void)
1836 struct protosw *tcp_protosw, *tcp6_protosw;
1839 t4_register_cpl_handler(CPL_GET_TCB_RPL, do_get_tcb_rpl);
1840 t4_register_shared_cpl_handler(CPL_L2T_WRITE_RPL, do_l2t_write_rpl2,
1842 t4_init_connect_cpl_handlers();
1843 t4_init_listen_cpl_handlers();
1844 t4_init_cpl_io_handlers();
1849 tcp_protosw = pffindproto(PF_INET, IPPROTO_TCP, SOCK_STREAM);
1850 if (tcp_protosw == NULL)
1851 return (ENOPROTOOPT);
1852 bcopy(tcp_protosw, &toe_protosw, sizeof(toe_protosw));
1853 bcopy(tcp_protosw->pr_usrreqs, &toe_usrreqs, sizeof(toe_usrreqs));
1854 toe_usrreqs.pru_aio_queue = t4_aio_queue_tom;
1855 toe_protosw.pr_ctloutput = t4_ctloutput_tom;
1856 toe_protosw.pr_usrreqs = &toe_usrreqs;
1858 tcp6_protosw = pffindproto(PF_INET6, IPPROTO_TCP, SOCK_STREAM);
1859 if (tcp6_protosw == NULL)
1860 return (ENOPROTOOPT);
1861 bcopy(tcp6_protosw, &toe6_protosw, sizeof(toe6_protosw));
1862 bcopy(tcp6_protosw->pr_usrreqs, &toe6_usrreqs, sizeof(toe6_usrreqs));
1863 toe6_usrreqs.pru_aio_queue = t4_aio_queue_tom;
1864 toe6_protosw.pr_ctloutput = t4_ctloutput_tom;
1865 toe6_protosw.pr_usrreqs = &toe6_usrreqs;
1867 return (t4_register_uld(&tom_uld_info));
1871 tom_uninit(struct adapter *sc, void *arg __unused)
1873 if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4tomun"))
1876 /* Try to free resources (works only if no port has IFCAP_TOE) */
1877 if (uld_active(sc, ULD_TOM))
1878 t4_deactivate_uld(sc, ULD_TOM);
1880 end_synchronized_op(sc, 0);
1884 t4_tom_mod_unload(void)
1886 t4_iterate(tom_uninit, NULL);
1888 if (t4_unregister_uld(&tom_uld_info) == EBUSY)
1891 t4_tls_mod_unload();
1892 t4_ddp_mod_unload();
1894 t4_uninit_connect_cpl_handlers();
1895 t4_uninit_listen_cpl_handlers();
1896 t4_uninit_cpl_io_handlers();
1897 t4_register_shared_cpl_handler(CPL_L2T_WRITE_RPL, NULL, CPL_COOKIE_TOM);
1898 t4_register_cpl_handler(CPL_GET_TCB_RPL, NULL);
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);