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_ratelimit.h"
37 #include <sys/param.h>
38 #include <sys/types.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
43 #include <sys/limits.h>
44 #include <sys/module.h>
45 #include <sys/protosw.h>
46 #include <sys/domain.h>
47 #include <sys/refcount.h>
48 #include <sys/rmlock.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/taskqueue.h>
53 #include <net/if_var.h>
54 #include <net/if_types.h>
55 #include <net/if_vlan_var.h>
56 #include <netinet/in.h>
57 #include <netinet/in_pcb.h>
58 #include <netinet/in_var.h>
59 #include <netinet/ip.h>
60 #include <netinet/ip6.h>
61 #include <netinet6/scope6_var.h>
63 #include <netinet/tcp_fsm.h>
64 #include <netinet/tcp_timer.h>
65 #include <netinet/tcp_var.h>
66 #include <netinet/toecore.h>
69 #include "common/common.h"
70 #include "common/t4_msg.h"
71 #include "common/t4_regs.h"
72 #include "common/t4_regs_values.h"
73 #include "common/t4_tcb.h"
74 #include "tom/t4_tom_l2t.h"
75 #include "tom/t4_tom.h"
76 #include "tom/t4_tls.h"
78 static struct protosw toe_protosw;
79 static struct pr_usrreqs toe_usrreqs;
81 static struct protosw toe6_protosw;
82 static struct pr_usrreqs toe6_usrreqs;
85 static int t4_tom_mod_load(void);
86 static int t4_tom_mod_unload(void);
87 static int t4_tom_modevent(module_t, int, void *);
89 /* ULD ops and helpers */
90 static int t4_tom_activate(struct adapter *);
91 static int t4_tom_deactivate(struct adapter *);
93 static struct uld_info tom_uld_info = {
95 .activate = t4_tom_activate,
96 .deactivate = t4_tom_deactivate,
99 static void release_offload_resources(struct toepcb *);
100 static int alloc_tid_tabs(struct tid_info *);
101 static void free_tid_tabs(struct tid_info *);
102 static int add_lip(struct adapter *, struct in6_addr *);
103 static int delete_lip(struct adapter *, struct in6_addr *);
104 static struct clip_entry *search_lip(struct tom_data *, struct in6_addr *);
105 static void init_clip_table(struct adapter *, struct tom_data *);
106 static void update_clip(struct adapter *, void *);
107 static void t4_clip_task(void *, int);
108 static void update_clip_table(struct adapter *, struct tom_data *);
109 static void destroy_clip_table(struct adapter *, struct tom_data *);
110 static void free_tom_data(struct adapter *, struct tom_data *);
111 static void reclaim_wr_resources(void *, int);
113 static int in6_ifaddr_gen;
114 static eventhandler_tag ifaddr_evhandler;
115 static struct timeout_task clip_task;
118 alloc_toepcb(struct vi_info *vi, int txqid, int rxqid, int flags)
120 struct port_info *pi = vi->pi;
121 struct adapter *sc = pi->adapter;
123 int tx_credits, txsd_total, len;
126 * The firmware counts tx work request credits in units of 16 bytes
127 * each. Reserve room for an ABORT_REQ so the driver never has to worry
128 * about tx credits if it wants to abort a connection.
130 tx_credits = sc->params.ofldq_wr_cred;
131 tx_credits -= howmany(sizeof(struct cpl_abort_req), 16);
134 * Shortest possible tx work request is a fw_ofld_tx_data_wr + 1 byte
135 * immediate payload, and firmware counts tx work request credits in
136 * units of 16 byte. Calculate the maximum work requests possible.
138 txsd_total = tx_credits /
139 howmany(sizeof(struct fw_ofld_tx_data_wr) + 1, 16);
141 KASSERT(txqid >= vi->first_ofld_txq &&
142 txqid < vi->first_ofld_txq + vi->nofldtxq,
143 ("%s: txqid %d for vi %p (first %d, n %d)", __func__, txqid, vi,
144 vi->first_ofld_txq, vi->nofldtxq));
146 KASSERT(rxqid >= vi->first_ofld_rxq &&
147 rxqid < vi->first_ofld_rxq + vi->nofldrxq,
148 ("%s: rxqid %d for vi %p (first %d, n %d)", __func__, rxqid, vi,
149 vi->first_ofld_rxq, vi->nofldrxq));
151 len = offsetof(struct toepcb, txsd) +
152 txsd_total * sizeof(struct ofld_tx_sdesc);
154 toep = malloc(len, M_CXGBE, M_ZERO | flags);
158 refcount_init(&toep->refcount, 1);
159 toep->td = sc->tom_softc;
162 toep->tx_total = tx_credits;
163 toep->tx_credits = tx_credits;
164 toep->ofld_txq = &sc->sge.ofld_txq[txqid];
165 toep->ofld_rxq = &sc->sge.ofld_rxq[rxqid];
166 toep->ctrlq = &sc->sge.ctrlq[pi->port_id];
167 mbufq_init(&toep->ulp_pduq, INT_MAX);
168 mbufq_init(&toep->ulp_pdu_reclaimq, INT_MAX);
169 toep->txsd_total = txsd_total;
170 toep->txsd_avail = txsd_total;
173 aiotx_init_toep(toep);
179 hold_toepcb(struct toepcb *toep)
182 refcount_acquire(&toep->refcount);
187 free_toepcb(struct toepcb *toep)
190 if (refcount_release(&toep->refcount) == 0)
193 KASSERT(!(toep->flags & TPF_ATTACHED),
194 ("%s: attached to an inpcb", __func__));
195 KASSERT(!(toep->flags & TPF_CPL_PENDING),
196 ("%s: CPL pending", __func__));
198 if (toep->ulp_mode == ULP_MODE_TCPDDP)
199 ddp_uninit_toep(toep);
200 tls_uninit_toep(toep);
205 * Set up the socket for TCP offload.
208 offload_socket(struct socket *so, struct toepcb *toep)
210 struct tom_data *td = toep->td;
211 struct inpcb *inp = sotoinpcb(so);
212 struct tcpcb *tp = intotcpcb(inp);
215 INP_WLOCK_ASSERT(inp);
220 sb->sb_flags |= SB_NOCOALESCE;
224 sb->sb_flags |= SB_NOCOALESCE;
225 if (inp->inp_vflag & INP_IPV6)
226 so->so_proto = &toe6_protosw;
228 so->so_proto = &toe_protosw;
234 tp->t_flags |= TF_TOE;
236 /* Install an extra hold on inp */
238 toep->flags |= TPF_ATTACHED;
241 /* Add the TOE PCB to the active list */
242 mtx_lock(&td->toep_list_lock);
243 TAILQ_INSERT_HEAD(&td->toep_list, toep, link);
244 mtx_unlock(&td->toep_list_lock);
247 /* This is _not_ the normal way to "unoffload" a socket. */
249 undo_offload_socket(struct socket *so)
251 struct inpcb *inp = sotoinpcb(so);
252 struct tcpcb *tp = intotcpcb(inp);
253 struct toepcb *toep = tp->t_toe;
254 struct tom_data *td = toep->td;
257 INP_WLOCK_ASSERT(inp);
261 sb->sb_flags &= ~SB_NOCOALESCE;
265 sb->sb_flags &= ~SB_NOCOALESCE;
270 tp->t_flags &= ~TF_TOE;
273 toep->flags &= ~TPF_ATTACHED;
274 if (in_pcbrele_wlocked(inp))
275 panic("%s: inp freed.", __func__);
277 mtx_lock(&td->toep_list_lock);
278 TAILQ_REMOVE(&td->toep_list, toep, link);
279 mtx_unlock(&td->toep_list_lock);
283 release_offload_resources(struct toepcb *toep)
285 struct tom_data *td = toep->td;
286 struct adapter *sc = td_adapter(td);
289 KASSERT(!(toep->flags & TPF_CPL_PENDING),
290 ("%s: %p has CPL pending.", __func__, toep));
291 KASSERT(!(toep->flags & TPF_ATTACHED),
292 ("%s: %p is still attached.", __func__, toep));
294 CTR5(KTR_CXGBE, "%s: toep %p (tid %d, l2te %p, ce %p)",
295 __func__, toep, tid, toep->l2te, toep->ce);
298 * These queues should have been emptied at approximately the same time
299 * that a normal connection's socket's so_snd would have been purged or
300 * drained. Do _not_ clean up here.
302 MPASS(mbufq_len(&toep->ulp_pduq) == 0);
303 MPASS(mbufq_len(&toep->ulp_pdu_reclaimq) == 0);
305 if (toep->ulp_mode == ULP_MODE_TCPDDP)
306 ddp_assert_empty(toep);
310 t4_l2t_release(toep->l2te);
313 remove_tid(sc, tid, toep->ce ? 2 : 1);
314 release_tid(sc, tid, toep->ctrlq);
318 release_lip(td, toep->ce);
321 if (toep->tc_idx != -1)
322 t4_release_cl_rl_kbps(sc, toep->vi->pi->port_id, toep->tc_idx);
324 mtx_lock(&td->toep_list_lock);
325 TAILQ_REMOVE(&td->toep_list, toep, link);
326 mtx_unlock(&td->toep_list_lock);
332 * The kernel is done with the TCP PCB and this is our opportunity to unhook the
333 * toepcb hanging off of it. If the TOE driver is also done with the toepcb (no
334 * pending CPL) then it is time to release all resources tied to the toepcb.
336 * Also gets called when an offloaded active open fails and the TOM wants the
337 * kernel to take the TCP PCB back.
340 t4_pcb_detach(struct toedev *tod __unused, struct tcpcb *tp)
342 #if defined(KTR) || defined(INVARIANTS)
343 struct inpcb *inp = tp->t_inpcb;
345 struct toepcb *toep = tp->t_toe;
347 INP_WLOCK_ASSERT(inp);
349 KASSERT(toep != NULL, ("%s: toep is NULL", __func__));
350 KASSERT(toep->flags & TPF_ATTACHED,
351 ("%s: not attached", __func__));
354 if (tp->t_state == TCPS_SYN_SENT) {
355 CTR6(KTR_CXGBE, "%s: atid %d, toep %p (0x%x), inp %p (0x%x)",
356 __func__, toep->tid, toep, toep->flags, inp,
360 "t4_pcb_detach: tid %d (%s), toep %p (0x%x), inp %p (0x%x)",
361 toep->tid, tcpstates[tp->t_state], toep, toep->flags, inp,
367 tp->t_flags &= ~TF_TOE;
368 toep->flags &= ~TPF_ATTACHED;
370 if (!(toep->flags & TPF_CPL_PENDING))
371 release_offload_resources(toep);
375 * setsockopt handler.
378 t4_ctloutput(struct toedev *tod, struct tcpcb *tp, int dir, int name)
380 struct adapter *sc = tod->tod_softc;
381 struct toepcb *toep = tp->t_toe;
386 CTR4(KTR_CXGBE, "%s: tp %p, dir %u, name %u", __func__, tp, dir, name);
390 if (tp->t_state != TCPS_ESTABLISHED)
392 t4_set_tcb_field(sc, toep->ctrlq, toep, W_TCB_T_FLAGS,
393 V_TF_NAGLE(1), V_TF_NAGLE(tp->t_flags & TF_NODELAY ? 0 : 1),
402 get_tcb_bit(u_char *tcb, int bit)
406 ix = 127 - (bit >> 3);
409 return ((tcb[ix] >> shift) & 1);
412 static inline uint64_t
413 get_tcb_bits(u_char *tcb, int hi, int lo)
418 rc = (rc << 1) | get_tcb_bit(tcb, hi);
426 * Called by the kernel to allow the TOE driver to "refine" values filled up in
427 * the tcp_info for an offloaded connection.
430 t4_tcp_info(struct toedev *tod, struct tcpcb *tp, struct tcp_info *ti)
433 struct adapter *sc = tod->tod_softc;
434 struct toepcb *toep = tp->t_toe;
436 uint32_t buf[TCB_SIZE / sizeof(uint32_t)];
439 INP_WLOCK_ASSERT(tp->t_inpcb);
442 addr = t4_read_reg(sc, A_TP_CMM_TCB_BASE) + toep->tid * TCB_SIZE;
443 rc = read_via_memwin(sc, 2, addr, &buf[0], TCB_SIZE);
447 tcb = (u_char *)&buf[0];
448 for (i = 0, j = TCB_SIZE - 16; i < j; i += 16, j -= 16) {
449 for (k = 0; k < 16; k++) {
451 tcb[i + k] = tcb[j + k];
456 ti->tcpi_state = get_tcb_bits(tcb, 115, 112);
458 v = get_tcb_bits(tcb, 271, 256);
459 ti->tcpi_rtt = tcp_ticks_to_us(sc, v);
461 v = get_tcb_bits(tcb, 287, 272);
462 ti->tcpi_rttvar = tcp_ticks_to_us(sc, v);
464 ti->tcpi_snd_ssthresh = get_tcb_bits(tcb, 487, 460);
465 ti->tcpi_snd_cwnd = get_tcb_bits(tcb, 459, 432);
466 ti->tcpi_rcv_nxt = get_tcb_bits(tcb, 553, 522);
468 ti->tcpi_snd_nxt = get_tcb_bits(tcb, 319, 288) -
469 get_tcb_bits(tcb, 375, 348);
471 /* Receive window being advertised by us. */
472 ti->tcpi_rcv_space = get_tcb_bits(tcb, 581, 554);
474 /* Send window ceiling. */
475 v = get_tcb_bits(tcb, 159, 144) << get_tcb_bits(tcb, 131, 128);
476 ti->tcpi_snd_wnd = min(v, ti->tcpi_snd_cwnd);
480 * The TOE driver will not receive any more CPLs for the tid associated with the
481 * toepcb; release the hold on the inpcb.
484 final_cpl_received(struct toepcb *toep)
486 struct inpcb *inp = toep->inp;
488 KASSERT(inp != NULL, ("%s: inp is NULL", __func__));
489 INP_WLOCK_ASSERT(inp);
490 KASSERT(toep->flags & TPF_CPL_PENDING,
491 ("%s: CPL not pending already?", __func__));
493 CTR6(KTR_CXGBE, "%s: tid %d, toep %p (0x%x), inp %p (0x%x)",
494 __func__, toep->tid, toep, toep->flags, inp, inp->inp_flags);
496 if (toep->ulp_mode == ULP_MODE_TCPDDP)
497 release_ddp_resources(toep);
499 toep->flags &= ~TPF_CPL_PENDING;
500 mbufq_drain(&toep->ulp_pdu_reclaimq);
502 if (!(toep->flags & TPF_ATTACHED))
503 release_offload_resources(toep);
505 if (!in_pcbrele_wlocked(inp))
510 insert_tid(struct adapter *sc, int tid, void *ctx, int ntids)
512 struct tid_info *t = &sc->tids;
514 t->tid_tab[tid] = ctx;
515 atomic_add_int(&t->tids_in_use, ntids);
519 lookup_tid(struct adapter *sc, int tid)
521 struct tid_info *t = &sc->tids;
523 return (t->tid_tab[tid]);
527 update_tid(struct adapter *sc, int tid, void *ctx)
529 struct tid_info *t = &sc->tids;
531 t->tid_tab[tid] = ctx;
535 remove_tid(struct adapter *sc, int tid, int ntids)
537 struct tid_info *t = &sc->tids;
539 t->tid_tab[tid] = NULL;
540 atomic_subtract_int(&t->tids_in_use, ntids);
544 * What mtu_idx to use, given a 4-tuple. Note that both s->mss and tcp_mssopt
545 * have the MSS that we should advertise in our SYN. Advertised MSS doesn't
546 * account for any TCP options so the effective MSS (only payload, no headers or
547 * options) could be different. We fill up tp->t_maxseg with the effective MSS
548 * at the end of the 3-way handshake.
551 find_best_mtu_idx(struct adapter *sc, struct in_conninfo *inc,
552 struct offload_settings *s)
554 unsigned short *mtus = &sc->params.mtus[0];
559 mss = s->mss > 0 ? s->mss : tcp_mssopt(inc);
560 if (inc->inc_flags & INC_ISIPV6)
561 mtu = mss + sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
563 mtu = mss + sizeof(struct ip) + sizeof(struct tcphdr);
565 for (i = 0; i < NMTUS - 1 && mtus[i + 1] <= mtu; i++)
572 * Determine the receive window size for a socket.
575 select_rcv_wnd(struct socket *so)
579 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
581 wnd = sbspace(&so->so_rcv);
582 if (wnd < MIN_RCV_WND)
585 return min(wnd, MAX_RCV_WND);
589 select_rcv_wscale(void)
592 unsigned long space = sb_max;
594 if (space > MAX_RCV_WND)
597 while (wscale < TCP_MAX_WINSHIFT && (TCP_MAXWIN << wscale) < space)
604 * socket so could be a listening socket too.
607 calc_opt0(struct socket *so, struct vi_info *vi, struct l2t_entry *e,
608 int mtu_idx, int rscale, int rx_credits, int ulp_mode,
609 struct offload_settings *s)
616 KASSERT(rx_credits <= M_RCV_BUFSIZ,
617 ("%s: rcv_bufsiz too high", __func__));
619 opt0 = F_TCAM_BYPASS | V_WND_SCALE(rscale) | V_MSS_IDX(mtu_idx) |
620 V_ULP_MODE(ulp_mode) | V_RCV_BUFSIZ(rx_credits) |
621 V_L2T_IDX(e->idx) | V_SMAC_SEL(vi->smt_idx) |
622 V_TX_CHAN(vi->pi->tx_chan);
624 keepalive = tcp_always_keepalive || so_options_get(so) & SO_KEEPALIVE;
625 opt0 |= V_KEEP_ALIVE(keepalive != 0);
628 struct inpcb *inp = sotoinpcb(so);
629 struct tcpcb *tp = intotcpcb(inp);
631 opt0 |= V_NAGLE((tp->t_flags & TF_NODELAY) == 0);
633 opt0 |= V_NAGLE(s->nagle != 0);
635 return htobe64(opt0);
639 select_ntuple(struct vi_info *vi, struct l2t_entry *e)
641 struct adapter *sc = vi->pi->adapter;
642 struct tp_params *tp = &sc->params.tp;
643 uint16_t viid = vi->viid;
647 * Initialize each of the fields which we care about which are present
648 * in the Compressed Filter Tuple.
650 if (tp->vlan_shift >= 0 && e->vlan != CPL_L2T_VLAN_NONE)
651 ntuple |= (uint64_t)(F_FT_VLAN_VLD | e->vlan) << tp->vlan_shift;
653 if (tp->port_shift >= 0)
654 ntuple |= (uint64_t)e->lport << tp->port_shift;
656 if (tp->protocol_shift >= 0)
657 ntuple |= (uint64_t)IPPROTO_TCP << tp->protocol_shift;
659 if (tp->vnic_shift >= 0) {
660 uint32_t vf = G_FW_VIID_VIN(viid);
661 uint32_t pf = G_FW_VIID_PFN(viid);
662 uint32_t vld = G_FW_VIID_VIVLD(viid);
664 ntuple |= (uint64_t)(V_FT_VNID_ID_VF(vf) | V_FT_VNID_ID_PF(pf) |
665 V_FT_VNID_ID_VLD(vld)) << tp->vnic_shift;
669 return (htobe32((uint32_t)ntuple));
671 return (htobe64(V_FILTER_TUPLE(ntuple)));
675 is_tls_sock(struct socket *so, struct adapter *sc)
677 struct inpcb *inp = sotoinpcb(so);
680 /* XXX: Eventually add a SO_WANT_TLS socket option perhaps? */
683 for (i = 0; i < sc->tt.num_tls_rx_ports; i++) {
684 if (inp->inp_lport == htons(sc->tt.tls_rx_ports[i]) ||
685 inp->inp_fport == htons(sc->tt.tls_rx_ports[i])) {
695 select_ulp_mode(struct socket *so, struct adapter *sc,
696 struct offload_settings *s)
699 if (can_tls_offload(sc) &&
700 (s->tls > 0 || (s->tls < 0 && is_tls_sock(so, sc))))
701 return (ULP_MODE_TLS);
702 else if (s->ddp > 0 ||
703 (s->ddp < 0 && sc->tt.ddp && (so->so_options & SO_NO_DDP) == 0))
704 return (ULP_MODE_TCPDDP);
706 return (ULP_MODE_NONE);
710 set_ulp_mode(struct toepcb *toep, int ulp_mode)
713 CTR4(KTR_CXGBE, "%s: toep %p (tid %d) ulp_mode %d",
714 __func__, toep, toep->tid, ulp_mode);
715 toep->ulp_mode = ulp_mode;
717 if (toep->ulp_mode == ULP_MODE_TCPDDP)
722 negative_advice(int status)
725 return (status == CPL_ERR_RTX_NEG_ADVICE ||
726 status == CPL_ERR_PERSIST_NEG_ADVICE ||
727 status == CPL_ERR_KEEPALV_NEG_ADVICE);
731 alloc_tid_tab(struct tid_info *t, int flags)
735 MPASS(t->tid_tab == NULL);
737 t->tid_tab = malloc(t->ntids * sizeof(*t->tid_tab), M_CXGBE,
739 if (t->tid_tab == NULL)
741 atomic_store_rel_int(&t->tids_in_use, 0);
747 free_tid_tab(struct tid_info *t)
750 KASSERT(t->tids_in_use == 0,
751 ("%s: %d tids still in use.", __func__, t->tids_in_use));
753 free(t->tid_tab, M_CXGBE);
758 alloc_stid_tab(struct tid_info *t, int flags)
761 MPASS(t->nstids > 0);
762 MPASS(t->stid_tab == NULL);
764 t->stid_tab = malloc(t->nstids * sizeof(*t->stid_tab), M_CXGBE,
766 if (t->stid_tab == NULL)
768 mtx_init(&t->stid_lock, "stid lock", NULL, MTX_DEF);
770 TAILQ_INIT(&t->stids);
771 t->nstids_free_head = t->nstids;
777 free_stid_tab(struct tid_info *t)
780 KASSERT(t->stids_in_use == 0,
781 ("%s: %d tids still in use.", __func__, t->stids_in_use));
783 if (mtx_initialized(&t->stid_lock))
784 mtx_destroy(&t->stid_lock);
785 free(t->stid_tab, M_CXGBE);
790 free_tid_tabs(struct tid_info *t)
799 alloc_tid_tabs(struct tid_info *t)
803 rc = alloc_tid_tab(t, M_NOWAIT);
807 rc = alloc_atid_tab(t, M_NOWAIT);
811 rc = alloc_stid_tab(t, M_NOWAIT);
822 add_lip(struct adapter *sc, struct in6_addr *lip)
824 struct fw_clip_cmd c;
826 ASSERT_SYNCHRONIZED_OP(sc);
827 /* mtx_assert(&td->clip_table_lock, MA_OWNED); */
829 memset(&c, 0, sizeof(c));
830 c.op_to_write = htonl(V_FW_CMD_OP(FW_CLIP_CMD) | F_FW_CMD_REQUEST |
832 c.alloc_to_len16 = htonl(F_FW_CLIP_CMD_ALLOC | FW_LEN16(c));
833 c.ip_hi = *(uint64_t *)&lip->s6_addr[0];
834 c.ip_lo = *(uint64_t *)&lip->s6_addr[8];
836 return (-t4_wr_mbox_ns(sc, sc->mbox, &c, sizeof(c), &c));
840 delete_lip(struct adapter *sc, struct in6_addr *lip)
842 struct fw_clip_cmd c;
844 ASSERT_SYNCHRONIZED_OP(sc);
845 /* mtx_assert(&td->clip_table_lock, MA_OWNED); */
847 memset(&c, 0, sizeof(c));
848 c.op_to_write = htonl(V_FW_CMD_OP(FW_CLIP_CMD) | F_FW_CMD_REQUEST |
850 c.alloc_to_len16 = htonl(F_FW_CLIP_CMD_FREE | FW_LEN16(c));
851 c.ip_hi = *(uint64_t *)&lip->s6_addr[0];
852 c.ip_lo = *(uint64_t *)&lip->s6_addr[8];
854 return (-t4_wr_mbox_ns(sc, sc->mbox, &c, sizeof(c), &c));
857 static struct clip_entry *
858 search_lip(struct tom_data *td, struct in6_addr *lip)
860 struct clip_entry *ce;
862 mtx_assert(&td->clip_table_lock, MA_OWNED);
864 TAILQ_FOREACH(ce, &td->clip_table, link) {
865 if (IN6_ARE_ADDR_EQUAL(&ce->lip, lip))
873 hold_lip(struct tom_data *td, struct in6_addr *lip, struct clip_entry *ce)
876 mtx_lock(&td->clip_table_lock);
878 ce = search_lip(td, lip);
881 mtx_unlock(&td->clip_table_lock);
887 release_lip(struct tom_data *td, struct clip_entry *ce)
890 mtx_lock(&td->clip_table_lock);
891 KASSERT(search_lip(td, &ce->lip) == ce,
892 ("%s: CLIP entry %p p not in CLIP table.", __func__, ce));
893 KASSERT(ce->refcount > 0,
894 ("%s: CLIP entry %p has refcount 0", __func__, ce));
896 mtx_unlock(&td->clip_table_lock);
900 init_clip_table(struct adapter *sc, struct tom_data *td)
903 ASSERT_SYNCHRONIZED_OP(sc);
905 mtx_init(&td->clip_table_lock, "CLIP table lock", NULL, MTX_DEF);
906 TAILQ_INIT(&td->clip_table);
909 update_clip_table(sc, td);
913 update_clip(struct adapter *sc, void *arg __unused)
916 if (begin_synchronized_op(sc, NULL, HOLD_LOCK, "t4tomuc"))
919 if (uld_active(sc, ULD_TOM))
920 update_clip_table(sc, sc->tom_softc);
922 end_synchronized_op(sc, LOCK_HELD);
926 t4_clip_task(void *arg, int count)
929 t4_iterate(update_clip, NULL);
933 update_clip_table(struct adapter *sc, struct tom_data *td)
935 struct rm_priotracker in6_ifa_tracker;
936 struct in6_ifaddr *ia;
937 struct in6_addr *lip, tlip;
938 struct clip_head stale;
939 struct clip_entry *ce, *ce_temp;
944 ASSERT_SYNCHRONIZED_OP(sc);
946 IN6_IFADDR_RLOCK(&in6_ifa_tracker);
947 mtx_lock(&td->clip_table_lock);
949 gen = atomic_load_acq_int(&in6_ifaddr_gen);
950 if (gen == td->clip_gen)
954 TAILQ_CONCAT(&stale, &td->clip_table, link);
957 * last_vnet optimizes the common cases where all if_vnet = NULL (no
958 * VIMAGE) or all if_vnet = vnet0.
960 last_vnet = (uintptr_t)(-1);
962 for_each_vi(sc->port[i], j, vi) {
963 if (last_vnet == (uintptr_t)vi->ifp->if_vnet)
966 /* XXX: races with if_vmove */
967 CURVNET_SET(vi->ifp->if_vnet);
968 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
969 lip = &ia->ia_addr.sin6_addr;
971 KASSERT(!IN6_IS_ADDR_MULTICAST(lip),
972 ("%s: mcast address in in6_ifaddr list", __func__));
974 if (IN6_IS_ADDR_LOOPBACK(lip))
976 if (IN6_IS_SCOPE_EMBED(lip)) {
977 /* Remove the embedded scope */
983 * XXX: how to weed out the link local address for the
984 * loopback interface? It's fe80::1 usually (always?).
988 * If it's in the main list then we already know it's
991 TAILQ_FOREACH(ce, &td->clip_table, link) {
992 if (IN6_ARE_ADDR_EQUAL(&ce->lip, lip))
997 * If it's in the stale list we should move it to the
1000 TAILQ_FOREACH(ce, &stale, link) {
1001 if (IN6_ARE_ADDR_EQUAL(&ce->lip, lip)) {
1002 TAILQ_REMOVE(&stale, ce, link);
1003 TAILQ_INSERT_TAIL(&td->clip_table, ce,
1009 /* A new IP6 address; add it to the CLIP table */
1010 ce = malloc(sizeof(*ce), M_CXGBE, M_NOWAIT);
1011 memcpy(&ce->lip, lip, sizeof(ce->lip));
1013 rc = add_lip(sc, lip);
1015 TAILQ_INSERT_TAIL(&td->clip_table, ce, link);
1017 char ip[INET6_ADDRSTRLEN];
1019 inet_ntop(AF_INET6, &ce->lip, &ip[0],
1021 log(LOG_ERR, "%s: could not add %s (%d)\n",
1029 last_vnet = (uintptr_t)vi->ifp->if_vnet;
1033 * Remove stale addresses (those no longer in V_in6_ifaddrhead) that are
1034 * no longer referenced by the driver.
1036 TAILQ_FOREACH_SAFE(ce, &stale, link, ce_temp) {
1037 if (ce->refcount == 0) {
1038 rc = delete_lip(sc, &ce->lip);
1040 TAILQ_REMOVE(&stale, ce, link);
1043 char ip[INET6_ADDRSTRLEN];
1045 inet_ntop(AF_INET6, &ce->lip, &ip[0],
1047 log(LOG_ERR, "%s: could not delete %s (%d)\n",
1052 /* The ones that are still referenced need to stay in the CLIP table */
1053 TAILQ_CONCAT(&td->clip_table, &stale, link);
1057 mtx_unlock(&td->clip_table_lock);
1058 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
1062 destroy_clip_table(struct adapter *sc, struct tom_data *td)
1064 struct clip_entry *ce, *ce_temp;
1066 if (mtx_initialized(&td->clip_table_lock)) {
1067 mtx_lock(&td->clip_table_lock);
1068 TAILQ_FOREACH_SAFE(ce, &td->clip_table, link, ce_temp) {
1069 KASSERT(ce->refcount == 0,
1070 ("%s: CLIP entry %p still in use (%d)", __func__,
1072 TAILQ_REMOVE(&td->clip_table, ce, link);
1073 delete_lip(sc, &ce->lip);
1076 mtx_unlock(&td->clip_table_lock);
1077 mtx_destroy(&td->clip_table_lock);
1082 free_tom_data(struct adapter *sc, struct tom_data *td)
1085 ASSERT_SYNCHRONIZED_OP(sc);
1087 KASSERT(TAILQ_EMPTY(&td->toep_list),
1088 ("%s: TOE PCB list is not empty.", __func__));
1089 KASSERT(td->lctx_count == 0,
1090 ("%s: lctx hash table is not empty.", __func__));
1093 t4_free_ppod_region(&td->pr);
1094 destroy_clip_table(sc, td);
1096 if (td->listen_mask != 0)
1097 hashdestroy(td->listen_hash, M_CXGBE, td->listen_mask);
1099 if (mtx_initialized(&td->unsent_wr_lock))
1100 mtx_destroy(&td->unsent_wr_lock);
1101 if (mtx_initialized(&td->lctx_hash_lock))
1102 mtx_destroy(&td->lctx_hash_lock);
1103 if (mtx_initialized(&td->toep_list_lock))
1104 mtx_destroy(&td->toep_list_lock);
1106 free_tid_tabs(&sc->tids);
1111 prepare_pkt(int open_type, uint16_t vtag, struct inpcb *inp, int *pktlen,
1118 max(sizeof(struct ether_header), sizeof(struct ether_vlan_header)) +
1119 max(sizeof(struct ip), sizeof(struct ip6_hdr)) +
1120 sizeof(struct tcphdr);
1122 MPASS(open_type == OPEN_TYPE_ACTIVE || open_type == OPEN_TYPE_LISTEN);
1124 pkt = malloc(maxlen, M_CXGBE, M_ZERO | M_NOWAIT);
1128 ipv6 = inp->inp_vflag & INP_IPV6;
1131 if (vtag == 0xffff) {
1132 struct ether_header *eh = (void *)pkt;
1135 eh->ether_type = htons(ETHERTYPE_IPV6);
1137 eh->ether_type = htons(ETHERTYPE_IP);
1141 struct ether_vlan_header *evh = (void *)pkt;
1143 evh->evl_encap_proto = htons(ETHERTYPE_VLAN);
1144 evh->evl_tag = htons(vtag);
1146 evh->evl_proto = htons(ETHERTYPE_IPV6);
1148 evh->evl_proto = htons(ETHERTYPE_IP);
1150 len += sizeof(*evh);
1154 struct ip6_hdr *ip6 = (void *)&pkt[len];
1156 ip6->ip6_vfc = IPV6_VERSION;
1157 ip6->ip6_plen = htons(sizeof(struct tcphdr));
1158 ip6->ip6_nxt = IPPROTO_TCP;
1159 if (open_type == OPEN_TYPE_ACTIVE) {
1160 ip6->ip6_src = inp->in6p_laddr;
1161 ip6->ip6_dst = inp->in6p_faddr;
1162 } else if (open_type == OPEN_TYPE_LISTEN) {
1163 ip6->ip6_src = inp->in6p_laddr;
1164 ip6->ip6_dst = ip6->ip6_src;
1167 len += sizeof(*ip6);
1169 struct ip *ip = (void *)&pkt[len];
1171 ip->ip_v = IPVERSION;
1172 ip->ip_hl = sizeof(*ip) >> 2;
1173 ip->ip_tos = inp->inp_ip_tos;
1174 ip->ip_len = htons(sizeof(struct ip) + sizeof(struct tcphdr));
1175 ip->ip_ttl = inp->inp_ip_ttl;
1176 ip->ip_p = IPPROTO_TCP;
1177 if (open_type == OPEN_TYPE_ACTIVE) {
1178 ip->ip_src = inp->inp_laddr;
1179 ip->ip_dst = inp->inp_faddr;
1180 } else if (open_type == OPEN_TYPE_LISTEN) {
1181 ip->ip_src = inp->inp_laddr;
1182 ip->ip_dst = ip->ip_src;
1188 th = (void *)&pkt[len];
1189 if (open_type == OPEN_TYPE_ACTIVE) {
1190 th->th_sport = inp->inp_lport; /* network byte order already */
1191 th->th_dport = inp->inp_fport; /* ditto */
1192 } else if (open_type == OPEN_TYPE_LISTEN) {
1193 th->th_sport = inp->inp_lport; /* network byte order already */
1194 th->th_dport = th->th_sport;
1198 *pktlen = *buflen = len;
1202 const struct offload_settings *
1203 lookup_offload_policy(struct adapter *sc, int open_type, struct mbuf *m,
1204 uint16_t vtag, struct inpcb *inp)
1206 const struct t4_offload_policy *op;
1208 struct offload_rule *r;
1209 int i, matched, pktlen, buflen;
1210 static const struct offload_settings allow_offloading_settings = {
1225 static const struct offload_settings disallow_offloading_settings = {
1227 /* rest is irrelevant when offload is off. */
1230 rw_assert(&sc->policy_lock, RA_LOCKED);
1233 * If there's no Connection Offloading Policy attached to the device
1234 * then we need to return a default static policy. If
1235 * "cop_managed_offloading" is true, then we need to disallow
1236 * offloading until a COP is attached to the device. Otherwise we
1237 * allow offloading ...
1241 if (sc->tt.cop_managed_offloading)
1242 return (&disallow_offloading_settings);
1244 return (&allow_offloading_settings);
1247 switch (open_type) {
1248 case OPEN_TYPE_ACTIVE:
1249 case OPEN_TYPE_LISTEN:
1250 pkt = prepare_pkt(open_type, vtag, inp, &pktlen, &buflen);
1252 case OPEN_TYPE_PASSIVE:
1254 pkt = mtod(m, char *);
1255 MPASS(*pkt == CPL_PASS_ACCEPT_REQ);
1256 pkt += sizeof(struct cpl_pass_accept_req);
1257 pktlen = m->m_pkthdr.len - sizeof(struct cpl_pass_accept_req);
1258 buflen = m->m_len - sizeof(struct cpl_pass_accept_req);
1262 return (&disallow_offloading_settings);
1265 if (pkt == NULL || pktlen == 0 || buflen == 0)
1266 return (&disallow_offloading_settings);
1269 for (i = 0; i < op->nrules; i++, r++) {
1270 if (r->open_type != open_type &&
1271 r->open_type != OPEN_TYPE_DONTCARE) {
1274 matched = bpf_filter(r->bpf_prog.bf_insns, pkt, pktlen, buflen);
1279 if (open_type == OPEN_TYPE_ACTIVE || open_type == OPEN_TYPE_LISTEN)
1282 return (matched ? &r->settings : &disallow_offloading_settings);
1286 reclaim_wr_resources(void *arg, int count)
1288 struct tom_data *td = arg;
1289 STAILQ_HEAD(, wrqe) twr_list = STAILQ_HEAD_INITIALIZER(twr_list);
1290 struct cpl_act_open_req *cpl;
1295 mtx_lock(&td->unsent_wr_lock);
1296 STAILQ_SWAP(&td->unsent_wr_list, &twr_list, wrqe);
1297 mtx_unlock(&td->unsent_wr_lock);
1299 while ((wr = STAILQ_FIRST(&twr_list)) != NULL) {
1300 STAILQ_REMOVE_HEAD(&twr_list, link);
1303 opcode = GET_OPCODE(cpl);
1306 case CPL_ACT_OPEN_REQ:
1307 case CPL_ACT_OPEN_REQ6:
1308 atid = G_TID_TID(be32toh(OPCODE_TID(cpl)));
1309 sc = td_adapter(td);
1311 CTR2(KTR_CXGBE, "%s: atid %u ", __func__, atid);
1312 act_open_failure_cleanup(sc, atid, EHOSTUNREACH);
1316 log(LOG_ERR, "%s: leaked work request %p, wr_len %d, "
1317 "opcode %x\n", __func__, wr, wr->wr_len, opcode);
1318 /* WR not freed here; go look at it with a debugger. */
1324 * Ground control to Major TOM
1325 * Commencing countdown, engines on
1328 t4_tom_activate(struct adapter *sc)
1330 struct tom_data *td;
1335 ASSERT_SYNCHRONIZED_OP(sc);
1337 /* per-adapter softc for TOM */
1338 td = malloc(sizeof(*td), M_CXGBE, M_ZERO | M_NOWAIT);
1342 /* List of TOE PCBs and associated lock */
1343 mtx_init(&td->toep_list_lock, "PCB list lock", NULL, MTX_DEF);
1344 TAILQ_INIT(&td->toep_list);
1346 /* Listen context */
1347 mtx_init(&td->lctx_hash_lock, "lctx hash lock", NULL, MTX_DEF);
1348 td->listen_hash = hashinit_flags(LISTEN_HASH_SIZE, M_CXGBE,
1349 &td->listen_mask, HASH_NOWAIT);
1351 /* List of WRs for which L2 resolution failed */
1352 mtx_init(&td->unsent_wr_lock, "Unsent WR list lock", NULL, MTX_DEF);
1353 STAILQ_INIT(&td->unsent_wr_list);
1354 TASK_INIT(&td->reclaim_wr_resources, 0, reclaim_wr_resources, td);
1357 rc = alloc_tid_tabs(&sc->tids);
1361 rc = t4_init_ppod_region(&td->pr, &sc->vres.ddp,
1362 t4_read_reg(sc, A_ULP_RX_TDDP_PSZ), "TDDP page pods");
1365 t4_set_reg_field(sc, A_ULP_RX_TDDP_TAGMASK,
1366 V_TDDPTAGMASK(M_TDDPTAGMASK), td->pr.pr_tag_mask);
1368 /* CLIP table for IPv6 offload */
1369 init_clip_table(sc, td);
1371 if (sc->vres.key.size != 0) {
1372 rc = tls_init_kmap(sc, td);
1380 tod->tod_softc = sc;
1381 tod->tod_connect = t4_connect;
1382 tod->tod_listen_start = t4_listen_start;
1383 tod->tod_listen_stop = t4_listen_stop;
1384 tod->tod_rcvd = t4_rcvd;
1385 tod->tod_output = t4_tod_output;
1386 tod->tod_send_rst = t4_send_rst;
1387 tod->tod_send_fin = t4_send_fin;
1388 tod->tod_pcb_detach = t4_pcb_detach;
1389 tod->tod_l2_update = t4_l2_update;
1390 tod->tod_syncache_added = t4_syncache_added;
1391 tod->tod_syncache_removed = t4_syncache_removed;
1392 tod->tod_syncache_respond = t4_syncache_respond;
1393 tod->tod_offload_socket = t4_offload_socket;
1394 tod->tod_ctloutput = t4_ctloutput;
1395 tod->tod_tcp_info = t4_tcp_info;
1397 for_each_port(sc, i) {
1398 for_each_vi(sc->port[i], v, vi) {
1399 TOEDEV(vi->ifp) = &td->tod;
1404 register_toedev(sc->tom_softc);
1408 free_tom_data(sc, td);
1413 t4_tom_deactivate(struct adapter *sc)
1416 struct tom_data *td = sc->tom_softc;
1418 ASSERT_SYNCHRONIZED_OP(sc);
1421 return (0); /* XXX. KASSERT? */
1423 if (sc->offload_map != 0)
1424 return (EBUSY); /* at least one port has IFCAP_TOE enabled */
1426 if (uld_active(sc, ULD_IWARP) || uld_active(sc, ULD_ISCSI))
1427 return (EBUSY); /* both iWARP and iSCSI rely on the TOE. */
1429 mtx_lock(&td->toep_list_lock);
1430 if (!TAILQ_EMPTY(&td->toep_list))
1432 mtx_unlock(&td->toep_list_lock);
1434 mtx_lock(&td->lctx_hash_lock);
1435 if (td->lctx_count > 0)
1437 mtx_unlock(&td->lctx_hash_lock);
1439 taskqueue_drain(taskqueue_thread, &td->reclaim_wr_resources);
1440 mtx_lock(&td->unsent_wr_lock);
1441 if (!STAILQ_EMPTY(&td->unsent_wr_list))
1443 mtx_unlock(&td->unsent_wr_lock);
1446 unregister_toedev(sc->tom_softc);
1447 free_tom_data(sc, td);
1448 sc->tom_softc = NULL;
1455 t4_tom_ifaddr_event(void *arg __unused, struct ifnet *ifp)
1458 atomic_add_rel_int(&in6_ifaddr_gen, 1);
1459 taskqueue_enqueue_timeout(taskqueue_thread, &clip_task, -hz / 4);
1463 t4_aio_queue_tom(struct socket *so, struct kaiocb *job)
1465 struct tcpcb *tp = so_sototcpcb(so);
1466 struct toepcb *toep = tp->t_toe;
1469 if (toep->ulp_mode == ULP_MODE_TCPDDP) {
1470 error = t4_aio_queue_ddp(so, job);
1471 if (error != EOPNOTSUPP)
1475 return (t4_aio_queue_aiotx(so, job));
1479 t4_ctloutput_tom(struct socket *so, struct sockopt *sopt)
1482 if (sopt->sopt_level != IPPROTO_TCP)
1483 return (tcp_ctloutput(so, sopt));
1485 switch (sopt->sopt_name) {
1486 case TCP_TLSOM_SET_TLS_CONTEXT:
1487 case TCP_TLSOM_GET_TLS_TOM:
1488 case TCP_TLSOM_CLR_TLS_TOM:
1489 case TCP_TLSOM_CLR_QUIES:
1490 return (t4_ctloutput_tls(so, sopt));
1492 return (tcp_ctloutput(so, sopt));
1497 t4_tom_mod_load(void)
1499 struct protosw *tcp_protosw, *tcp6_protosw;
1502 t4_register_shared_cpl_handler(CPL_L2T_WRITE_RPL, do_l2t_write_rpl2,
1504 t4_init_connect_cpl_handlers();
1505 t4_init_listen_cpl_handlers();
1506 t4_init_cpl_io_handlers();
1511 tcp_protosw = pffindproto(PF_INET, IPPROTO_TCP, SOCK_STREAM);
1512 if (tcp_protosw == NULL)
1513 return (ENOPROTOOPT);
1514 bcopy(tcp_protosw, &toe_protosw, sizeof(toe_protosw));
1515 bcopy(tcp_protosw->pr_usrreqs, &toe_usrreqs, sizeof(toe_usrreqs));
1516 toe_usrreqs.pru_aio_queue = t4_aio_queue_tom;
1517 toe_protosw.pr_ctloutput = t4_ctloutput_tom;
1518 toe_protosw.pr_usrreqs = &toe_usrreqs;
1520 tcp6_protosw = pffindproto(PF_INET6, IPPROTO_TCP, SOCK_STREAM);
1521 if (tcp6_protosw == NULL)
1522 return (ENOPROTOOPT);
1523 bcopy(tcp6_protosw, &toe6_protosw, sizeof(toe6_protosw));
1524 bcopy(tcp6_protosw->pr_usrreqs, &toe6_usrreqs, sizeof(toe6_usrreqs));
1525 toe6_usrreqs.pru_aio_queue = t4_aio_queue_tom;
1526 toe6_protosw.pr_ctloutput = t4_ctloutput_tom;
1527 toe6_protosw.pr_usrreqs = &toe6_usrreqs;
1529 TIMEOUT_TASK_INIT(taskqueue_thread, &clip_task, 0, t4_clip_task, NULL);
1530 ifaddr_evhandler = EVENTHANDLER_REGISTER(ifaddr_event,
1531 t4_tom_ifaddr_event, NULL, EVENTHANDLER_PRI_ANY);
1533 return (t4_register_uld(&tom_uld_info));
1537 tom_uninit(struct adapter *sc, void *arg __unused)
1539 if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4tomun"))
1542 /* Try to free resources (works only if no port has IFCAP_TOE) */
1543 if (uld_active(sc, ULD_TOM))
1544 t4_deactivate_uld(sc, ULD_TOM);
1546 end_synchronized_op(sc, 0);
1550 t4_tom_mod_unload(void)
1552 t4_iterate(tom_uninit, NULL);
1554 if (t4_unregister_uld(&tom_uld_info) == EBUSY)
1557 if (ifaddr_evhandler) {
1558 EVENTHANDLER_DEREGISTER(ifaddr_event, ifaddr_evhandler);
1559 taskqueue_cancel_timeout(taskqueue_thread, &clip_task, NULL);
1562 t4_tls_mod_unload();
1563 t4_ddp_mod_unload();
1565 t4_uninit_connect_cpl_handlers();
1566 t4_uninit_listen_cpl_handlers();
1567 t4_uninit_cpl_io_handlers();
1568 t4_register_shared_cpl_handler(CPL_L2T_WRITE_RPL, NULL, CPL_COOKIE_TOM);
1572 #endif /* TCP_OFFLOAD */
1575 t4_tom_modevent(module_t mod, int cmd, void *arg)
1582 rc = t4_tom_mod_load();
1586 rc = t4_tom_mod_unload();
1593 printf("t4_tom: compiled without TCP_OFFLOAD support.\n");
1599 static moduledata_t t4_tom_moddata= {
1605 MODULE_VERSION(t4_tom, 1);
1606 MODULE_DEPEND(t4_tom, toecore, 1, 1, 1);
1607 MODULE_DEPEND(t4_tom, t4nex, 1, 1, 1);
1608 DECLARE_MODULE(t4_tom, t4_tom_moddata, SI_SUB_EXEC, SI_ORDER_ANY);