2 * Copyright (c) 2012 Chelsio Communications, Inc.
4 * Written by: Navdeep Parhar <np@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 #include "opt_inet6.h"
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
40 #include <sys/limits.h>
41 #include <sys/module.h>
42 #include <sys/protosw.h>
43 #include <sys/domain.h>
44 #include <sys/refcount.h>
45 #include <sys/rmlock.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/taskqueue.h>
50 #include <net/if_var.h>
51 #include <net/if_types.h>
52 #include <net/if_vlan_var.h>
53 #include <netinet/in.h>
54 #include <netinet/in_pcb.h>
55 #include <netinet/in_var.h>
56 #include <netinet/ip.h>
57 #include <netinet/ip6.h>
58 #include <netinet6/scope6_var.h>
60 #include <netinet/tcp_fsm.h>
61 #include <netinet/tcp_timer.h>
62 #include <netinet/tcp_var.h>
63 #include <netinet/toecore.h>
66 #include "common/common.h"
67 #include "common/t4_msg.h"
68 #include "common/t4_regs.h"
69 #include "common/t4_regs_values.h"
70 #include "common/t4_tcb.h"
71 #include "tom/t4_tom_l2t.h"
72 #include "tom/t4_tom.h"
73 #include "tom/t4_tls.h"
75 static struct protosw toe_protosw;
76 static struct pr_usrreqs toe_usrreqs;
78 static struct protosw toe6_protosw;
79 static struct pr_usrreqs toe6_usrreqs;
82 static int t4_tom_mod_load(void);
83 static int t4_tom_mod_unload(void);
84 static int t4_tom_modevent(module_t, int, void *);
86 /* ULD ops and helpers */
87 static int t4_tom_activate(struct adapter *);
88 static int t4_tom_deactivate(struct adapter *);
90 static struct uld_info tom_uld_info = {
92 .activate = t4_tom_activate,
93 .deactivate = t4_tom_deactivate,
96 static void queue_tid_release(struct adapter *, int);
97 static void release_offload_resources(struct toepcb *);
98 static int alloc_tid_tabs(struct tid_info *);
99 static void free_tid_tabs(struct tid_info *);
100 static int add_lip(struct adapter *, struct in6_addr *);
101 static int delete_lip(struct adapter *, struct in6_addr *);
102 static struct clip_entry *search_lip(struct tom_data *, struct in6_addr *);
103 static void init_clip_table(struct adapter *, struct tom_data *);
104 static void update_clip(struct adapter *, void *);
105 static void t4_clip_task(void *, int);
106 static void update_clip_table(struct adapter *, struct tom_data *);
107 static void destroy_clip_table(struct adapter *, struct tom_data *);
108 static void free_tom_data(struct adapter *, struct tom_data *);
109 static void reclaim_wr_resources(void *, int);
111 static int in6_ifaddr_gen;
112 static eventhandler_tag ifaddr_evhandler;
113 static struct timeout_task clip_task;
116 alloc_toepcb(struct vi_info *vi, int txqid, int rxqid, int flags)
118 struct port_info *pi = vi->pi;
119 struct adapter *sc = pi->adapter;
121 int tx_credits, txsd_total, len;
124 * The firmware counts tx work request credits in units of 16 bytes
125 * each. Reserve room for an ABORT_REQ so the driver never has to worry
126 * about tx credits if it wants to abort a connection.
128 tx_credits = sc->params.ofldq_wr_cred;
129 tx_credits -= howmany(sizeof(struct cpl_abort_req), 16);
132 * Shortest possible tx work request is a fw_ofld_tx_data_wr + 1 byte
133 * immediate payload, and firmware counts tx work request credits in
134 * units of 16 byte. Calculate the maximum work requests possible.
136 txsd_total = tx_credits /
137 howmany(sizeof(struct fw_ofld_tx_data_wr) + 1, 16);
139 KASSERT(txqid >= vi->first_ofld_txq &&
140 txqid < vi->first_ofld_txq + vi->nofldtxq,
141 ("%s: txqid %d for vi %p (first %d, n %d)", __func__, txqid, vi,
142 vi->first_ofld_txq, vi->nofldtxq));
144 KASSERT(rxqid >= vi->first_ofld_rxq &&
145 rxqid < vi->first_ofld_rxq + vi->nofldrxq,
146 ("%s: rxqid %d for vi %p (first %d, n %d)", __func__, rxqid, vi,
147 vi->first_ofld_rxq, vi->nofldrxq));
149 len = offsetof(struct toepcb, txsd) +
150 txsd_total * sizeof(struct ofld_tx_sdesc);
152 toep = malloc(len, M_CXGBE, M_ZERO | flags);
156 refcount_init(&toep->refcount, 1);
157 toep->td = sc->tom_softc;
160 toep->tx_total = tx_credits;
161 toep->tx_credits = tx_credits;
162 toep->ofld_txq = &sc->sge.ofld_txq[txqid];
163 toep->ofld_rxq = &sc->sge.ofld_rxq[rxqid];
164 toep->ctrlq = &sc->sge.ctrlq[pi->port_id];
165 mbufq_init(&toep->ulp_pduq, INT_MAX);
166 mbufq_init(&toep->ulp_pdu_reclaimq, INT_MAX);
167 toep->txsd_total = txsd_total;
168 toep->txsd_avail = txsd_total;
171 aiotx_init_toep(toep);
177 hold_toepcb(struct toepcb *toep)
180 refcount_acquire(&toep->refcount);
185 free_toepcb(struct toepcb *toep)
188 if (refcount_release(&toep->refcount) == 0)
191 KASSERT(!(toep->flags & TPF_ATTACHED),
192 ("%s: attached to an inpcb", __func__));
193 KASSERT(!(toep->flags & TPF_CPL_PENDING),
194 ("%s: CPL pending", __func__));
196 if (toep->ulp_mode == ULP_MODE_TCPDDP)
197 ddp_uninit_toep(toep);
198 tls_uninit_toep(toep);
203 * Set up the socket for TCP offload.
206 offload_socket(struct socket *so, struct toepcb *toep)
208 struct tom_data *td = toep->td;
209 struct inpcb *inp = sotoinpcb(so);
210 struct tcpcb *tp = intotcpcb(inp);
213 INP_WLOCK_ASSERT(inp);
218 sb->sb_flags |= SB_NOCOALESCE;
222 sb->sb_flags |= SB_NOCOALESCE;
223 if (inp->inp_vflag & INP_IPV6)
224 so->so_proto = &toe6_protosw;
226 so->so_proto = &toe_protosw;
232 tp->t_flags |= TF_TOE;
234 /* Install an extra hold on inp */
236 toep->flags |= TPF_ATTACHED;
239 /* Add the TOE PCB to the active list */
240 mtx_lock(&td->toep_list_lock);
241 TAILQ_INSERT_HEAD(&td->toep_list, toep, link);
242 mtx_unlock(&td->toep_list_lock);
245 /* This is _not_ the normal way to "unoffload" a socket. */
247 undo_offload_socket(struct socket *so)
249 struct inpcb *inp = sotoinpcb(so);
250 struct tcpcb *tp = intotcpcb(inp);
251 struct toepcb *toep = tp->t_toe;
252 struct tom_data *td = toep->td;
255 INP_WLOCK_ASSERT(inp);
259 sb->sb_flags &= ~SB_NOCOALESCE;
263 sb->sb_flags &= ~SB_NOCOALESCE;
268 tp->t_flags &= ~TF_TOE;
271 toep->flags &= ~TPF_ATTACHED;
272 if (in_pcbrele_wlocked(inp))
273 panic("%s: inp freed.", __func__);
275 mtx_lock(&td->toep_list_lock);
276 TAILQ_REMOVE(&td->toep_list, toep, link);
277 mtx_unlock(&td->toep_list_lock);
281 release_offload_resources(struct toepcb *toep)
283 struct tom_data *td = toep->td;
284 struct adapter *sc = td_adapter(td);
287 KASSERT(!(toep->flags & TPF_CPL_PENDING),
288 ("%s: %p has CPL pending.", __func__, toep));
289 KASSERT(!(toep->flags & TPF_ATTACHED),
290 ("%s: %p is still attached.", __func__, toep));
292 CTR5(KTR_CXGBE, "%s: toep %p (tid %d, l2te %p, ce %p)",
293 __func__, toep, tid, toep->l2te, toep->ce);
296 * These queues should have been emptied at approximately the same time
297 * that a normal connection's socket's so_snd would have been purged or
298 * drained. Do _not_ clean up here.
300 MPASS(mbufq_len(&toep->ulp_pduq) == 0);
301 MPASS(mbufq_len(&toep->ulp_pdu_reclaimq) == 0);
303 if (toep->ulp_mode == ULP_MODE_TCPDDP)
304 ddp_assert_empty(toep);
308 t4_l2t_release(toep->l2te);
311 remove_tid(sc, tid, toep->ce ? 2 : 1);
312 release_tid(sc, tid, toep->ctrlq);
316 release_lip(td, toep->ce);
319 if (toep->tc_idx != -1)
320 t4_release_cl_rl_kbps(sc, toep->vi->pi->port_id, toep->tc_idx);
322 mtx_lock(&td->toep_list_lock);
323 TAILQ_REMOVE(&td->toep_list, toep, link);
324 mtx_unlock(&td->toep_list_lock);
330 * The kernel is done with the TCP PCB and this is our opportunity to unhook the
331 * toepcb hanging off of it. If the TOE driver is also done with the toepcb (no
332 * pending CPL) then it is time to release all resources tied to the toepcb.
334 * Also gets called when an offloaded active open fails and the TOM wants the
335 * kernel to take the TCP PCB back.
338 t4_pcb_detach(struct toedev *tod __unused, struct tcpcb *tp)
340 #if defined(KTR) || defined(INVARIANTS)
341 struct inpcb *inp = tp->t_inpcb;
343 struct toepcb *toep = tp->t_toe;
345 INP_WLOCK_ASSERT(inp);
347 KASSERT(toep != NULL, ("%s: toep is NULL", __func__));
348 KASSERT(toep->flags & TPF_ATTACHED,
349 ("%s: not attached", __func__));
352 if (tp->t_state == TCPS_SYN_SENT) {
353 CTR6(KTR_CXGBE, "%s: atid %d, toep %p (0x%x), inp %p (0x%x)",
354 __func__, toep->tid, toep, toep->flags, inp,
358 "t4_pcb_detach: tid %d (%s), toep %p (0x%x), inp %p (0x%x)",
359 toep->tid, tcpstates[tp->t_state], toep, toep->flags, inp,
365 tp->t_flags &= ~TF_TOE;
366 toep->flags &= ~TPF_ATTACHED;
368 if (!(toep->flags & TPF_CPL_PENDING))
369 release_offload_resources(toep);
373 * setsockopt handler.
376 t4_ctloutput(struct toedev *tod, struct tcpcb *tp, int dir, int name)
378 struct adapter *sc = tod->tod_softc;
379 struct toepcb *toep = tp->t_toe;
384 CTR4(KTR_CXGBE, "%s: tp %p, dir %u, name %u", __func__, tp, dir, name);
388 if (tp->t_state != TCPS_ESTABLISHED)
390 t4_set_tcb_field(sc, toep->ctrlq, toep, W_TCB_T_FLAGS,
391 V_TF_NAGLE(1), V_TF_NAGLE(tp->t_flags & TF_NODELAY ? 0 : 1),
400 * The TOE driver will not receive any more CPLs for the tid associated with the
401 * toepcb; release the hold on the inpcb.
404 final_cpl_received(struct toepcb *toep)
406 struct inpcb *inp = toep->inp;
408 KASSERT(inp != NULL, ("%s: inp is NULL", __func__));
409 INP_WLOCK_ASSERT(inp);
410 KASSERT(toep->flags & TPF_CPL_PENDING,
411 ("%s: CPL not pending already?", __func__));
413 CTR6(KTR_CXGBE, "%s: tid %d, toep %p (0x%x), inp %p (0x%x)",
414 __func__, toep->tid, toep, toep->flags, inp, inp->inp_flags);
416 if (toep->ulp_mode == ULP_MODE_TCPDDP)
417 release_ddp_resources(toep);
419 toep->flags &= ~TPF_CPL_PENDING;
420 mbufq_drain(&toep->ulp_pdu_reclaimq);
422 if (!(toep->flags & TPF_ATTACHED))
423 release_offload_resources(toep);
425 if (!in_pcbrele_wlocked(inp))
430 insert_tid(struct adapter *sc, int tid, void *ctx, int ntids)
432 struct tid_info *t = &sc->tids;
434 t->tid_tab[tid] = ctx;
435 atomic_add_int(&t->tids_in_use, ntids);
439 lookup_tid(struct adapter *sc, int tid)
441 struct tid_info *t = &sc->tids;
443 return (t->tid_tab[tid]);
447 update_tid(struct adapter *sc, int tid, void *ctx)
449 struct tid_info *t = &sc->tids;
451 t->tid_tab[tid] = ctx;
455 remove_tid(struct adapter *sc, int tid, int ntids)
457 struct tid_info *t = &sc->tids;
459 t->tid_tab[tid] = NULL;
460 atomic_subtract_int(&t->tids_in_use, ntids);
464 release_tid(struct adapter *sc, int tid, struct sge_wrq *ctrlq)
467 struct cpl_tid_release *req;
469 wr = alloc_wrqe(sizeof(*req), ctrlq);
471 queue_tid_release(sc, tid); /* defer */
476 INIT_TP_WR_MIT_CPL(req, CPL_TID_RELEASE, tid);
482 queue_tid_release(struct adapter *sc, int tid)
485 CXGBE_UNIMPLEMENTED("deferred tid release");
489 * What mtu_idx to use, given a 4-tuple. Note that both s->mss and tcp_mssopt
490 * have the MSS that we should advertise in our SYN. Advertised MSS doesn't
491 * account for any TCP options so the effective MSS (only payload, no headers or
492 * options) could be different. We fill up tp->t_maxseg with the effective MSS
493 * at the end of the 3-way handshake.
496 find_best_mtu_idx(struct adapter *sc, struct in_conninfo *inc,
497 struct offload_settings *s)
499 unsigned short *mtus = &sc->params.mtus[0];
504 mss = s->mss > 0 ? s->mss : tcp_mssopt(inc);
505 if (inc->inc_flags & INC_ISIPV6)
506 mtu = mss + sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
508 mtu = mss + sizeof(struct ip) + sizeof(struct tcphdr);
510 for (i = 0; i < NMTUS - 1 && mtus[i + 1] <= mtu; i++)
517 * Determine the receive window size for a socket.
520 select_rcv_wnd(struct socket *so)
524 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
526 wnd = sbspace(&so->so_rcv);
527 if (wnd < MIN_RCV_WND)
530 return min(wnd, MAX_RCV_WND);
534 select_rcv_wscale(void)
537 unsigned long space = sb_max;
539 if (space > MAX_RCV_WND)
542 while (wscale < TCP_MAX_WINSHIFT && (TCP_MAXWIN << wscale) < space)
549 * socket so could be a listening socket too.
552 calc_opt0(struct socket *so, struct vi_info *vi, struct l2t_entry *e,
553 int mtu_idx, int rscale, int rx_credits, int ulp_mode,
554 struct offload_settings *s)
561 KASSERT(rx_credits <= M_RCV_BUFSIZ,
562 ("%s: rcv_bufsiz too high", __func__));
564 opt0 = F_TCAM_BYPASS | V_WND_SCALE(rscale) | V_MSS_IDX(mtu_idx) |
565 V_ULP_MODE(ulp_mode) | V_RCV_BUFSIZ(rx_credits) |
566 V_L2T_IDX(e->idx) | V_SMAC_SEL(vi->smt_idx) |
567 V_TX_CHAN(vi->pi->tx_chan);
569 keepalive = tcp_always_keepalive || so_options_get(so) & SO_KEEPALIVE;
570 opt0 |= V_KEEP_ALIVE(keepalive != 0);
573 struct inpcb *inp = sotoinpcb(so);
574 struct tcpcb *tp = intotcpcb(inp);
576 opt0 |= V_NAGLE((tp->t_flags & TF_NODELAY) == 0);
578 opt0 |= V_NAGLE(s->nagle != 0);
580 return htobe64(opt0);
584 select_ntuple(struct vi_info *vi, struct l2t_entry *e)
586 struct adapter *sc = vi->pi->adapter;
587 struct tp_params *tp = &sc->params.tp;
588 uint16_t viid = vi->viid;
592 * Initialize each of the fields which we care about which are present
593 * in the Compressed Filter Tuple.
595 if (tp->vlan_shift >= 0 && e->vlan != CPL_L2T_VLAN_NONE)
596 ntuple |= (uint64_t)(F_FT_VLAN_VLD | e->vlan) << tp->vlan_shift;
598 if (tp->port_shift >= 0)
599 ntuple |= (uint64_t)e->lport << tp->port_shift;
601 if (tp->protocol_shift >= 0)
602 ntuple |= (uint64_t)IPPROTO_TCP << tp->protocol_shift;
604 if (tp->vnic_shift >= 0) {
605 uint32_t vf = G_FW_VIID_VIN(viid);
606 uint32_t pf = G_FW_VIID_PFN(viid);
607 uint32_t vld = G_FW_VIID_VIVLD(viid);
609 ntuple |= (uint64_t)(V_FT_VNID_ID_VF(vf) | V_FT_VNID_ID_PF(pf) |
610 V_FT_VNID_ID_VLD(vld)) << tp->vnic_shift;
614 return (htobe32((uint32_t)ntuple));
616 return (htobe64(V_FILTER_TUPLE(ntuple)));
620 is_tls_sock(struct socket *so, struct adapter *sc)
622 struct inpcb *inp = sotoinpcb(so);
625 /* XXX: Eventually add a SO_WANT_TLS socket option perhaps? */
628 for (i = 0; i < sc->tt.num_tls_rx_ports; i++) {
629 if (inp->inp_lport == htons(sc->tt.tls_rx_ports[i]) ||
630 inp->inp_fport == htons(sc->tt.tls_rx_ports[i])) {
640 select_ulp_mode(struct socket *so, struct adapter *sc,
641 struct offload_settings *s)
644 if (can_tls_offload(sc) &&
645 (s->tls > 0 || (s->tls < 0 && is_tls_sock(so, sc))))
646 return (ULP_MODE_TLS);
647 else if (s->ddp > 0 ||
648 (s->ddp < 0 && sc->tt.ddp && (so->so_options & SO_NO_DDP) == 0))
649 return (ULP_MODE_TCPDDP);
651 return (ULP_MODE_NONE);
655 set_ulp_mode(struct toepcb *toep, int ulp_mode)
658 CTR4(KTR_CXGBE, "%s: toep %p (tid %d) ulp_mode %d",
659 __func__, toep, toep->tid, ulp_mode);
660 toep->ulp_mode = ulp_mode;
662 if (toep->ulp_mode == ULP_MODE_TCPDDP)
667 negative_advice(int status)
670 return (status == CPL_ERR_RTX_NEG_ADVICE ||
671 status == CPL_ERR_PERSIST_NEG_ADVICE ||
672 status == CPL_ERR_KEEPALV_NEG_ADVICE);
676 alloc_tid_tabs(struct tid_info *t)
681 size = t->ntids * sizeof(*t->tid_tab) +
682 t->natids * sizeof(*t->atid_tab) +
683 t->nstids * sizeof(*t->stid_tab);
685 t->tid_tab = malloc(size, M_CXGBE, M_ZERO | M_NOWAIT);
686 if (t->tid_tab == NULL)
689 mtx_init(&t->atid_lock, "atid lock", NULL, MTX_DEF);
690 t->atid_tab = (union aopen_entry *)&t->tid_tab[t->ntids];
691 t->afree = t->atid_tab;
693 for (i = 1; i < t->natids; i++)
694 t->atid_tab[i - 1].next = &t->atid_tab[i];
695 t->atid_tab[t->natids - 1].next = NULL;
697 mtx_init(&t->stid_lock, "stid lock", NULL, MTX_DEF);
698 t->stid_tab = (struct listen_ctx **)&t->atid_tab[t->natids];
700 TAILQ_INIT(&t->stids);
701 t->nstids_free_head = t->nstids;
703 atomic_store_rel_int(&t->tids_in_use, 0);
709 free_tid_tabs(struct tid_info *t)
711 KASSERT(t->tids_in_use == 0,
712 ("%s: %d tids still in use.", __func__, t->tids_in_use));
713 KASSERT(t->atids_in_use == 0,
714 ("%s: %d atids still in use.", __func__, t->atids_in_use));
715 KASSERT(t->stids_in_use == 0,
716 ("%s: %d tids still in use.", __func__, t->stids_in_use));
718 free(t->tid_tab, M_CXGBE);
721 if (mtx_initialized(&t->atid_lock))
722 mtx_destroy(&t->atid_lock);
723 if (mtx_initialized(&t->stid_lock))
724 mtx_destroy(&t->stid_lock);
728 add_lip(struct adapter *sc, struct in6_addr *lip)
730 struct fw_clip_cmd c;
732 ASSERT_SYNCHRONIZED_OP(sc);
733 /* mtx_assert(&td->clip_table_lock, MA_OWNED); */
735 memset(&c, 0, sizeof(c));
736 c.op_to_write = htonl(V_FW_CMD_OP(FW_CLIP_CMD) | F_FW_CMD_REQUEST |
738 c.alloc_to_len16 = htonl(F_FW_CLIP_CMD_ALLOC | FW_LEN16(c));
739 c.ip_hi = *(uint64_t *)&lip->s6_addr[0];
740 c.ip_lo = *(uint64_t *)&lip->s6_addr[8];
742 return (-t4_wr_mbox_ns(sc, sc->mbox, &c, sizeof(c), &c));
746 delete_lip(struct adapter *sc, struct in6_addr *lip)
748 struct fw_clip_cmd c;
750 ASSERT_SYNCHRONIZED_OP(sc);
751 /* mtx_assert(&td->clip_table_lock, MA_OWNED); */
753 memset(&c, 0, sizeof(c));
754 c.op_to_write = htonl(V_FW_CMD_OP(FW_CLIP_CMD) | F_FW_CMD_REQUEST |
756 c.alloc_to_len16 = htonl(F_FW_CLIP_CMD_FREE | FW_LEN16(c));
757 c.ip_hi = *(uint64_t *)&lip->s6_addr[0];
758 c.ip_lo = *(uint64_t *)&lip->s6_addr[8];
760 return (-t4_wr_mbox_ns(sc, sc->mbox, &c, sizeof(c), &c));
763 static struct clip_entry *
764 search_lip(struct tom_data *td, struct in6_addr *lip)
766 struct clip_entry *ce;
768 mtx_assert(&td->clip_table_lock, MA_OWNED);
770 TAILQ_FOREACH(ce, &td->clip_table, link) {
771 if (IN6_ARE_ADDR_EQUAL(&ce->lip, lip))
779 hold_lip(struct tom_data *td, struct in6_addr *lip, struct clip_entry *ce)
782 mtx_lock(&td->clip_table_lock);
784 ce = search_lip(td, lip);
787 mtx_unlock(&td->clip_table_lock);
793 release_lip(struct tom_data *td, struct clip_entry *ce)
796 mtx_lock(&td->clip_table_lock);
797 KASSERT(search_lip(td, &ce->lip) == ce,
798 ("%s: CLIP entry %p p not in CLIP table.", __func__, ce));
799 KASSERT(ce->refcount > 0,
800 ("%s: CLIP entry %p has refcount 0", __func__, ce));
802 mtx_unlock(&td->clip_table_lock);
806 init_clip_table(struct adapter *sc, struct tom_data *td)
809 ASSERT_SYNCHRONIZED_OP(sc);
811 mtx_init(&td->clip_table_lock, "CLIP table lock", NULL, MTX_DEF);
812 TAILQ_INIT(&td->clip_table);
815 update_clip_table(sc, td);
819 update_clip(struct adapter *sc, void *arg __unused)
822 if (begin_synchronized_op(sc, NULL, HOLD_LOCK, "t4tomuc"))
825 if (uld_active(sc, ULD_TOM))
826 update_clip_table(sc, sc->tom_softc);
828 end_synchronized_op(sc, LOCK_HELD);
832 t4_clip_task(void *arg, int count)
835 t4_iterate(update_clip, NULL);
839 update_clip_table(struct adapter *sc, struct tom_data *td)
841 struct rm_priotracker in6_ifa_tracker;
842 struct in6_ifaddr *ia;
843 struct in6_addr *lip, tlip;
844 struct clip_head stale;
845 struct clip_entry *ce, *ce_temp;
850 ASSERT_SYNCHRONIZED_OP(sc);
852 IN6_IFADDR_RLOCK(&in6_ifa_tracker);
853 mtx_lock(&td->clip_table_lock);
855 gen = atomic_load_acq_int(&in6_ifaddr_gen);
856 if (gen == td->clip_gen)
860 TAILQ_CONCAT(&stale, &td->clip_table, link);
863 * last_vnet optimizes the common cases where all if_vnet = NULL (no
864 * VIMAGE) or all if_vnet = vnet0.
866 last_vnet = (uintptr_t)(-1);
868 for_each_vi(sc->port[i], j, vi) {
869 if (last_vnet == (uintptr_t)vi->ifp->if_vnet)
872 /* XXX: races with if_vmove */
873 CURVNET_SET(vi->ifp->if_vnet);
874 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
875 lip = &ia->ia_addr.sin6_addr;
877 KASSERT(!IN6_IS_ADDR_MULTICAST(lip),
878 ("%s: mcast address in in6_ifaddr list", __func__));
880 if (IN6_IS_ADDR_LOOPBACK(lip))
882 if (IN6_IS_SCOPE_EMBED(lip)) {
883 /* Remove the embedded scope */
889 * XXX: how to weed out the link local address for the
890 * loopback interface? It's fe80::1 usually (always?).
894 * If it's in the main list then we already know it's
897 TAILQ_FOREACH(ce, &td->clip_table, link) {
898 if (IN6_ARE_ADDR_EQUAL(&ce->lip, lip))
903 * If it's in the stale list we should move it to the
906 TAILQ_FOREACH(ce, &stale, link) {
907 if (IN6_ARE_ADDR_EQUAL(&ce->lip, lip)) {
908 TAILQ_REMOVE(&stale, ce, link);
909 TAILQ_INSERT_TAIL(&td->clip_table, ce,
915 /* A new IP6 address; add it to the CLIP table */
916 ce = malloc(sizeof(*ce), M_CXGBE, M_NOWAIT);
917 memcpy(&ce->lip, lip, sizeof(ce->lip));
919 rc = add_lip(sc, lip);
921 TAILQ_INSERT_TAIL(&td->clip_table, ce, link);
923 char ip[INET6_ADDRSTRLEN];
925 inet_ntop(AF_INET6, &ce->lip, &ip[0],
927 log(LOG_ERR, "%s: could not add %s (%d)\n",
935 last_vnet = (uintptr_t)vi->ifp->if_vnet;
939 * Remove stale addresses (those no longer in V_in6_ifaddrhead) that are
940 * no longer referenced by the driver.
942 TAILQ_FOREACH_SAFE(ce, &stale, link, ce_temp) {
943 if (ce->refcount == 0) {
944 rc = delete_lip(sc, &ce->lip);
946 TAILQ_REMOVE(&stale, ce, link);
949 char ip[INET6_ADDRSTRLEN];
951 inet_ntop(AF_INET6, &ce->lip, &ip[0],
953 log(LOG_ERR, "%s: could not delete %s (%d)\n",
958 /* The ones that are still referenced need to stay in the CLIP table */
959 TAILQ_CONCAT(&td->clip_table, &stale, link);
963 mtx_unlock(&td->clip_table_lock);
964 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
968 destroy_clip_table(struct adapter *sc, struct tom_data *td)
970 struct clip_entry *ce, *ce_temp;
972 if (mtx_initialized(&td->clip_table_lock)) {
973 mtx_lock(&td->clip_table_lock);
974 TAILQ_FOREACH_SAFE(ce, &td->clip_table, link, ce_temp) {
975 KASSERT(ce->refcount == 0,
976 ("%s: CLIP entry %p still in use (%d)", __func__,
978 TAILQ_REMOVE(&td->clip_table, ce, link);
979 delete_lip(sc, &ce->lip);
982 mtx_unlock(&td->clip_table_lock);
983 mtx_destroy(&td->clip_table_lock);
988 free_tom_data(struct adapter *sc, struct tom_data *td)
991 ASSERT_SYNCHRONIZED_OP(sc);
993 KASSERT(TAILQ_EMPTY(&td->toep_list),
994 ("%s: TOE PCB list is not empty.", __func__));
995 KASSERT(td->lctx_count == 0,
996 ("%s: lctx hash table is not empty.", __func__));
998 t4_free_ppod_region(&td->pr);
999 destroy_clip_table(sc, td);
1001 if (td->listen_mask != 0)
1002 hashdestroy(td->listen_hash, M_CXGBE, td->listen_mask);
1004 if (mtx_initialized(&td->unsent_wr_lock))
1005 mtx_destroy(&td->unsent_wr_lock);
1006 if (mtx_initialized(&td->lctx_hash_lock))
1007 mtx_destroy(&td->lctx_hash_lock);
1008 if (mtx_initialized(&td->toep_list_lock))
1009 mtx_destroy(&td->toep_list_lock);
1011 free_tid_tabs(&sc->tids);
1016 prepare_pkt(int open_type, uint16_t vtag, struct inpcb *inp, int *pktlen,
1023 max(sizeof(struct ether_header), sizeof(struct ether_vlan_header)) +
1024 max(sizeof(struct ip), sizeof(struct ip6_hdr)) +
1025 sizeof(struct tcphdr);
1027 MPASS(open_type == OPEN_TYPE_ACTIVE || open_type == OPEN_TYPE_LISTEN);
1029 pkt = malloc(maxlen, M_CXGBE, M_ZERO | M_NOWAIT);
1033 ipv6 = inp->inp_vflag & INP_IPV6;
1036 if (vtag == 0xffff) {
1037 struct ether_header *eh = (void *)pkt;
1040 eh->ether_type = htons(ETHERTYPE_IPV6);
1042 eh->ether_type = htons(ETHERTYPE_IP);
1046 struct ether_vlan_header *evh = (void *)pkt;
1048 evh->evl_encap_proto = htons(ETHERTYPE_VLAN);
1049 evh->evl_tag = htons(vtag);
1051 evh->evl_proto = htons(ETHERTYPE_IPV6);
1053 evh->evl_proto = htons(ETHERTYPE_IP);
1055 len += sizeof(*evh);
1059 struct ip6_hdr *ip6 = (void *)&pkt[len];
1061 ip6->ip6_vfc = IPV6_VERSION;
1062 ip6->ip6_plen = htons(sizeof(struct tcphdr));
1063 ip6->ip6_nxt = IPPROTO_TCP;
1064 if (open_type == OPEN_TYPE_ACTIVE) {
1065 ip6->ip6_src = inp->in6p_laddr;
1066 ip6->ip6_dst = inp->in6p_faddr;
1067 } else if (open_type == OPEN_TYPE_LISTEN) {
1068 ip6->ip6_src = inp->in6p_laddr;
1069 ip6->ip6_dst = ip6->ip6_src;
1072 len += sizeof(*ip6);
1074 struct ip *ip = (void *)&pkt[len];
1076 ip->ip_v = IPVERSION;
1077 ip->ip_hl = sizeof(*ip) >> 2;
1078 ip->ip_tos = inp->inp_ip_tos;
1079 ip->ip_len = htons(sizeof(struct ip) + sizeof(struct tcphdr));
1080 ip->ip_ttl = inp->inp_ip_ttl;
1081 ip->ip_p = IPPROTO_TCP;
1082 if (open_type == OPEN_TYPE_ACTIVE) {
1083 ip->ip_src = inp->inp_laddr;
1084 ip->ip_dst = inp->inp_faddr;
1085 } else if (open_type == OPEN_TYPE_LISTEN) {
1086 ip->ip_src = inp->inp_laddr;
1087 ip->ip_dst = ip->ip_src;
1093 th = (void *)&pkt[len];
1094 if (open_type == OPEN_TYPE_ACTIVE) {
1095 th->th_sport = inp->inp_lport; /* network byte order already */
1096 th->th_dport = inp->inp_fport; /* ditto */
1097 } else if (open_type == OPEN_TYPE_LISTEN) {
1098 th->th_sport = inp->inp_lport; /* network byte order already */
1099 th->th_dport = th->th_sport;
1103 *pktlen = *buflen = len;
1107 const struct offload_settings *
1108 lookup_offload_policy(struct adapter *sc, int open_type, struct mbuf *m,
1109 uint16_t vtag, struct inpcb *inp)
1111 const struct t4_offload_policy *op;
1113 struct offload_rule *r;
1114 int i, matched, pktlen, buflen;
1115 static const struct offload_settings allow_offloading_settings = {
1130 static const struct offload_settings disallow_offloading_settings = {
1132 /* rest is irrelevant when offload is off. */
1135 rw_assert(&sc->policy_lock, RA_LOCKED);
1138 * If there's no Connection Offloading Policy attached to the device
1139 * then we need to return a default static policy. If
1140 * "cop_managed_offloading" is true, then we need to disallow
1141 * offloading until a COP is attached to the device. Otherwise we
1142 * allow offloading ...
1146 if (sc->tt.cop_managed_offloading)
1147 return (&disallow_offloading_settings);
1149 return (&allow_offloading_settings);
1152 switch (open_type) {
1153 case OPEN_TYPE_ACTIVE:
1154 case OPEN_TYPE_LISTEN:
1155 pkt = prepare_pkt(open_type, vtag, inp, &pktlen, &buflen);
1157 case OPEN_TYPE_PASSIVE:
1159 pkt = mtod(m, char *);
1160 MPASS(*pkt == CPL_PASS_ACCEPT_REQ);
1161 pkt += sizeof(struct cpl_pass_accept_req);
1162 pktlen = m->m_pkthdr.len - sizeof(struct cpl_pass_accept_req);
1163 buflen = m->m_len - sizeof(struct cpl_pass_accept_req);
1167 return (&disallow_offloading_settings);
1170 if (pkt == NULL || pktlen == 0 || buflen == 0)
1171 return (&disallow_offloading_settings);
1174 for (i = 0; i < op->nrules; i++, r++) {
1175 if (r->open_type != open_type &&
1176 r->open_type != OPEN_TYPE_DONTCARE) {
1179 matched = bpf_filter(r->bpf_prog.bf_insns, pkt, pktlen, buflen);
1184 if (open_type == OPEN_TYPE_ACTIVE || open_type == OPEN_TYPE_LISTEN)
1187 return (matched ? &r->settings : &disallow_offloading_settings);
1191 reclaim_wr_resources(void *arg, int count)
1193 struct tom_data *td = arg;
1194 STAILQ_HEAD(, wrqe) twr_list = STAILQ_HEAD_INITIALIZER(twr_list);
1195 struct cpl_act_open_req *cpl;
1200 mtx_lock(&td->unsent_wr_lock);
1201 STAILQ_SWAP(&td->unsent_wr_list, &twr_list, wrqe);
1202 mtx_unlock(&td->unsent_wr_lock);
1204 while ((wr = STAILQ_FIRST(&twr_list)) != NULL) {
1205 STAILQ_REMOVE_HEAD(&twr_list, link);
1208 opcode = GET_OPCODE(cpl);
1211 case CPL_ACT_OPEN_REQ:
1212 case CPL_ACT_OPEN_REQ6:
1213 atid = G_TID_TID(be32toh(OPCODE_TID(cpl)));
1214 sc = td_adapter(td);
1216 CTR2(KTR_CXGBE, "%s: atid %u ", __func__, atid);
1217 act_open_failure_cleanup(sc, atid, EHOSTUNREACH);
1221 log(LOG_ERR, "%s: leaked work request %p, wr_len %d, "
1222 "opcode %x\n", __func__, wr, wr->wr_len, opcode);
1223 /* WR not freed here; go look at it with a debugger. */
1229 * Ground control to Major TOM
1230 * Commencing countdown, engines on
1233 t4_tom_activate(struct adapter *sc)
1235 struct tom_data *td;
1238 struct sge_ofld_rxq *ofld_rxq;
1241 ASSERT_SYNCHRONIZED_OP(sc);
1243 /* per-adapter softc for TOM */
1244 td = malloc(sizeof(*td), M_CXGBE, M_ZERO | M_NOWAIT);
1248 /* List of TOE PCBs and associated lock */
1249 mtx_init(&td->toep_list_lock, "PCB list lock", NULL, MTX_DEF);
1250 TAILQ_INIT(&td->toep_list);
1252 /* Listen context */
1253 mtx_init(&td->lctx_hash_lock, "lctx hash lock", NULL, MTX_DEF);
1254 td->listen_hash = hashinit_flags(LISTEN_HASH_SIZE, M_CXGBE,
1255 &td->listen_mask, HASH_NOWAIT);
1257 /* List of WRs for which L2 resolution failed */
1258 mtx_init(&td->unsent_wr_lock, "Unsent WR list lock", NULL, MTX_DEF);
1259 STAILQ_INIT(&td->unsent_wr_list);
1260 TASK_INIT(&td->reclaim_wr_resources, 0, reclaim_wr_resources, td);
1263 rc = alloc_tid_tabs(&sc->tids);
1267 rc = t4_init_ppod_region(&td->pr, &sc->vres.ddp,
1268 t4_read_reg(sc, A_ULP_RX_TDDP_PSZ), "TDDP page pods");
1271 t4_set_reg_field(sc, A_ULP_RX_TDDP_TAGMASK,
1272 V_TDDPTAGMASK(M_TDDPTAGMASK), td->pr.pr_tag_mask);
1274 /* CLIP table for IPv6 offload */
1275 init_clip_table(sc, td);
1280 tod->tod_softc = sc;
1281 tod->tod_connect = t4_connect;
1282 tod->tod_listen_start = t4_listen_start;
1283 tod->tod_listen_stop = t4_listen_stop;
1284 tod->tod_rcvd = t4_rcvd;
1285 tod->tod_output = t4_tod_output;
1286 tod->tod_send_rst = t4_send_rst;
1287 tod->tod_send_fin = t4_send_fin;
1288 tod->tod_pcb_detach = t4_pcb_detach;
1289 tod->tod_l2_update = t4_l2_update;
1290 tod->tod_syncache_added = t4_syncache_added;
1291 tod->tod_syncache_removed = t4_syncache_removed;
1292 tod->tod_syncache_respond = t4_syncache_respond;
1293 tod->tod_offload_socket = t4_offload_socket;
1294 tod->tod_ctloutput = t4_ctloutput;
1296 for_each_port(sc, i) {
1297 for_each_vi(sc->port[i], v, vi) {
1298 TOEDEV(vi->ifp) = &td->tod;
1299 for_each_ofld_rxq(vi, j, ofld_rxq) {
1300 ofld_rxq->iq.set_tcb_rpl = do_set_tcb_rpl;
1301 ofld_rxq->iq.l2t_write_rpl = do_l2t_write_rpl2;
1307 register_toedev(sc->tom_softc);
1311 free_tom_data(sc, td);
1316 t4_tom_deactivate(struct adapter *sc)
1319 struct tom_data *td = sc->tom_softc;
1321 ASSERT_SYNCHRONIZED_OP(sc);
1324 return (0); /* XXX. KASSERT? */
1326 if (sc->offload_map != 0)
1327 return (EBUSY); /* at least one port has IFCAP_TOE enabled */
1329 if (uld_active(sc, ULD_IWARP) || uld_active(sc, ULD_ISCSI))
1330 return (EBUSY); /* both iWARP and iSCSI rely on the TOE. */
1332 mtx_lock(&td->toep_list_lock);
1333 if (!TAILQ_EMPTY(&td->toep_list))
1335 mtx_unlock(&td->toep_list_lock);
1337 mtx_lock(&td->lctx_hash_lock);
1338 if (td->lctx_count > 0)
1340 mtx_unlock(&td->lctx_hash_lock);
1342 taskqueue_drain(taskqueue_thread, &td->reclaim_wr_resources);
1343 mtx_lock(&td->unsent_wr_lock);
1344 if (!STAILQ_EMPTY(&td->unsent_wr_list))
1346 mtx_unlock(&td->unsent_wr_lock);
1349 unregister_toedev(sc->tom_softc);
1350 free_tom_data(sc, td);
1351 sc->tom_softc = NULL;
1358 t4_tom_ifaddr_event(void *arg __unused, struct ifnet *ifp)
1361 atomic_add_rel_int(&in6_ifaddr_gen, 1);
1362 taskqueue_enqueue_timeout(taskqueue_thread, &clip_task, -hz / 4);
1366 t4_aio_queue_tom(struct socket *so, struct kaiocb *job)
1368 struct tcpcb *tp = so_sototcpcb(so);
1369 struct toepcb *toep = tp->t_toe;
1372 if (toep->ulp_mode == ULP_MODE_TCPDDP) {
1373 error = t4_aio_queue_ddp(so, job);
1374 if (error != EOPNOTSUPP)
1378 return (t4_aio_queue_aiotx(so, job));
1382 t4_ctloutput_tom(struct socket *so, struct sockopt *sopt)
1385 if (sopt->sopt_level != IPPROTO_TCP)
1386 return (tcp_ctloutput(so, sopt));
1388 switch (sopt->sopt_name) {
1389 case TCP_TLSOM_SET_TLS_CONTEXT:
1390 case TCP_TLSOM_GET_TLS_TOM:
1391 case TCP_TLSOM_CLR_TLS_TOM:
1392 case TCP_TLSOM_CLR_QUIES:
1393 return (t4_ctloutput_tls(so, sopt));
1395 return (tcp_ctloutput(so, sopt));
1400 t4_tom_mod_load(void)
1402 struct protosw *tcp_protosw, *tcp6_protosw;
1405 t4_init_connect_cpl_handlers();
1406 t4_init_listen_cpl_handlers();
1407 t4_init_cpl_io_handlers();
1412 tcp_protosw = pffindproto(PF_INET, IPPROTO_TCP, SOCK_STREAM);
1413 if (tcp_protosw == NULL)
1414 return (ENOPROTOOPT);
1415 bcopy(tcp_protosw, &toe_protosw, sizeof(toe_protosw));
1416 bcopy(tcp_protosw->pr_usrreqs, &toe_usrreqs, sizeof(toe_usrreqs));
1417 toe_usrreqs.pru_aio_queue = t4_aio_queue_tom;
1418 toe_protosw.pr_ctloutput = t4_ctloutput_tom;
1419 toe_protosw.pr_usrreqs = &toe_usrreqs;
1421 tcp6_protosw = pffindproto(PF_INET6, IPPROTO_TCP, SOCK_STREAM);
1422 if (tcp6_protosw == NULL)
1423 return (ENOPROTOOPT);
1424 bcopy(tcp6_protosw, &toe6_protosw, sizeof(toe6_protosw));
1425 bcopy(tcp6_protosw->pr_usrreqs, &toe6_usrreqs, sizeof(toe6_usrreqs));
1426 toe6_usrreqs.pru_aio_queue = t4_aio_queue_tom;
1427 toe6_protosw.pr_ctloutput = t4_ctloutput_tom;
1428 toe6_protosw.pr_usrreqs = &toe6_usrreqs;
1430 TIMEOUT_TASK_INIT(taskqueue_thread, &clip_task, 0, t4_clip_task, NULL);
1431 ifaddr_evhandler = EVENTHANDLER_REGISTER(ifaddr_event,
1432 t4_tom_ifaddr_event, NULL, EVENTHANDLER_PRI_ANY);
1434 return (t4_register_uld(&tom_uld_info));
1438 tom_uninit(struct adapter *sc, void *arg __unused)
1440 if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4tomun"))
1443 /* Try to free resources (works only if no port has IFCAP_TOE) */
1444 if (uld_active(sc, ULD_TOM))
1445 t4_deactivate_uld(sc, ULD_TOM);
1447 end_synchronized_op(sc, 0);
1451 t4_tom_mod_unload(void)
1453 t4_iterate(tom_uninit, NULL);
1455 if (t4_unregister_uld(&tom_uld_info) == EBUSY)
1458 if (ifaddr_evhandler) {
1459 EVENTHANDLER_DEREGISTER(ifaddr_event, ifaddr_evhandler);
1460 taskqueue_cancel_timeout(taskqueue_thread, &clip_task, NULL);
1463 t4_tls_mod_unload();
1464 t4_ddp_mod_unload();
1466 t4_uninit_connect_cpl_handlers();
1467 t4_uninit_listen_cpl_handlers();
1468 t4_uninit_cpl_io_handlers();
1472 #endif /* TCP_OFFLOAD */
1475 t4_tom_modevent(module_t mod, int cmd, void *arg)
1482 rc = t4_tom_mod_load();
1486 rc = t4_tom_mod_unload();
1493 printf("t4_tom: compiled without TCP_OFFLOAD support.\n");
1499 static moduledata_t t4_tom_moddata= {
1505 MODULE_VERSION(t4_tom, 1);
1506 MODULE_DEPEND(t4_tom, toecore, 1, 1, 1);
1507 MODULE_DEPEND(t4_tom, t4nex, 1, 1, 1);
1508 DECLARE_MODULE(t4_tom, t4_tom_moddata, SI_SUB_EXEC, SI_ORDER_ANY);