1237263Snp/*- 2237263Snp * Copyright (c) 2012 Chelsio Communications, Inc. 3237263Snp * All rights reserved. 4237263Snp * Written by: Navdeep Parhar <np@FreeBSD.org> 5237263Snp * 6237263Snp * Redistribution and use in source and binary forms, with or without 7237263Snp * modification, are permitted provided that the following conditions 8237263Snp * are met: 9237263Snp * 1. Redistributions of source code must retain the above copyright 10237263Snp * notice, this list of conditions and the following disclaimer. 11237263Snp * 2. Redistributions in binary form must reproduce the above copyright 12237263Snp * notice, this list of conditions and the following disclaimer in the 13237263Snp * documentation and/or other materials provided with the distribution. 14237263Snp * 15237263Snp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16237263Snp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17237263Snp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18237263Snp * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19237263Snp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20237263Snp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21237263Snp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22237263Snp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23237263Snp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24237263Snp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25237263Snp * SUCH DAMAGE. 26237263Snp */ 27237263Snp 28237263Snp#include <sys/cdefs.h> 29237263Snp__FBSDID("$FreeBSD$"); 30237263Snp 31237263Snp#include "opt_inet.h" 32245441Snp#include "opt_inet6.h" 33237263Snp 34237263Snp#include <sys/param.h> 35237263Snp#include <sys/types.h> 36237263Snp#include <sys/systm.h> 37237263Snp#include <sys/kernel.h> 38237263Snp#include <sys/ktr.h> 39237263Snp#include <sys/module.h> 40237263Snp#include <sys/protosw.h> 41237263Snp#include <sys/domain.h> 42237263Snp#include <sys/socket.h> 43237263Snp#include <sys/socketvar.h> 44249627Snp#include <sys/taskqueue.h> 45245448Snp#include <net/if.h> 46237263Snp#include <netinet/in.h> 47237263Snp#include <netinet/in_pcb.h> 48245448Snp#include <netinet/in_var.h> 49237263Snp#include <netinet/ip.h> 50245441Snp#include <netinet/ip6.h> 51237263Snp#include <netinet/tcp_var.h> 52245448Snp#include <netinet6/scope6_var.h> 53237263Snp#define TCPSTATES 54237263Snp#include <netinet/tcp_fsm.h> 55237263Snp#include <netinet/toecore.h> 56237263Snp 57237263Snp#ifdef TCP_OFFLOAD 58237263Snp#include "common/common.h" 59237263Snp#include "common/t4_msg.h" 60237263Snp#include "common/t4_regs.h" 61252705Snp#include "common/t4_regs_values.h" 62252716Snp#include "common/t4_tcb.h" 63237263Snp#include "tom/t4_tom_l2t.h" 64237263Snp#include "tom/t4_tom.h" 65237263Snp 66239344Snpstatic struct protosw ddp_protosw; 67239344Snpstatic struct pr_usrreqs ddp_usrreqs; 68239344Snp 69245441Snpstatic struct protosw ddp6_protosw; 70245441Snpstatic struct pr_usrreqs ddp6_usrreqs; 71245441Snp 72237263Snp/* Module ops */ 73237263Snpstatic int t4_tom_mod_load(void); 74237263Snpstatic int t4_tom_mod_unload(void); 75237263Snpstatic int t4_tom_modevent(module_t, int, void *); 76237263Snp 77237263Snp/* ULD ops and helpers */ 78237263Snpstatic int t4_tom_activate(struct adapter *); 79237263Snpstatic int t4_tom_deactivate(struct adapter *); 80237263Snp 81237263Snpstatic struct uld_info tom_uld_info = { 82237263Snp .uld_id = ULD_TOM, 83237263Snp .activate = t4_tom_activate, 84237263Snp .deactivate = t4_tom_deactivate, 85237263Snp}; 86237263Snp 87237263Snpstatic void queue_tid_release(struct adapter *, int); 88237263Snpstatic void release_offload_resources(struct toepcb *); 89237263Snpstatic int alloc_tid_tabs(struct tid_info *); 90237263Snpstatic void free_tid_tabs(struct tid_info *); 91245448Snpstatic int add_lip(struct adapter *, struct in6_addr *); 92245448Snpstatic int delete_lip(struct adapter *, struct in6_addr *); 93245448Snpstatic struct clip_entry *search_lip(struct tom_data *, struct in6_addr *); 94245448Snpstatic void init_clip_table(struct adapter *, struct tom_data *); 95249627Snpstatic void update_clip(struct adapter *, void *); 96249627Snpstatic void t4_clip_task(void *, int); 97249627Snpstatic void update_clip_table(struct adapter *, struct tom_data *); 98245448Snpstatic void destroy_clip_table(struct adapter *, struct tom_data *); 99237263Snpstatic void free_tom_data(struct adapter *, struct tom_data *); 100237263Snp 101249627Snpstatic int in6_ifaddr_gen; 102249627Snpstatic eventhandler_tag ifaddr_evhandler; 103249627Snpstatic struct timeout_task clip_task; 104249627Snp 105237263Snpstruct toepcb * 106237263Snpalloc_toepcb(struct port_info *pi, int txqid, int rxqid, int flags) 107237263Snp{ 108237263Snp struct adapter *sc = pi->adapter; 109237263Snp struct toepcb *toep; 110237263Snp int tx_credits, txsd_total, len; 111237263Snp 112237263Snp /* 113237263Snp * The firmware counts tx work request credits in units of 16 bytes 114237263Snp * each. Reserve room for an ABORT_REQ so the driver never has to worry 115237263Snp * about tx credits if it wants to abort a connection. 116237263Snp */ 117237263Snp tx_credits = sc->params.ofldq_wr_cred; 118237263Snp tx_credits -= howmany(sizeof(struct cpl_abort_req), 16); 119237263Snp 120237263Snp /* 121237263Snp * Shortest possible tx work request is a fw_ofld_tx_data_wr + 1 byte 122237263Snp * immediate payload, and firmware counts tx work request credits in 123237263Snp * units of 16 byte. Calculate the maximum work requests possible. 124237263Snp */ 125237263Snp txsd_total = tx_credits / 126237263Snp howmany((sizeof(struct fw_ofld_tx_data_wr) + 1), 16); 127237263Snp 128237263Snp if (txqid < 0) 129237263Snp txqid = (arc4random() % pi->nofldtxq) + pi->first_ofld_txq; 130237263Snp KASSERT(txqid >= pi->first_ofld_txq && 131237263Snp txqid < pi->first_ofld_txq + pi->nofldtxq, 132237263Snp ("%s: txqid %d for port %p (first %d, n %d)", __func__, txqid, pi, 133237263Snp pi->first_ofld_txq, pi->nofldtxq)); 134237263Snp 135237263Snp if (rxqid < 0) 136237263Snp rxqid = (arc4random() % pi->nofldrxq) + pi->first_ofld_rxq; 137237263Snp KASSERT(rxqid >= pi->first_ofld_rxq && 138237263Snp rxqid < pi->first_ofld_rxq + pi->nofldrxq, 139237263Snp ("%s: rxqid %d for port %p (first %d, n %d)", __func__, rxqid, pi, 140237263Snp pi->first_ofld_rxq, pi->nofldrxq)); 141237263Snp 142237263Snp len = offsetof(struct toepcb, txsd) + 143237263Snp txsd_total * sizeof(struct ofld_tx_sdesc); 144237263Snp 145237263Snp toep = malloc(len, M_CXGBE, M_ZERO | flags); 146237263Snp if (toep == NULL) 147237263Snp return (NULL); 148237263Snp 149237263Snp toep->td = sc->tom_softc; 150237263Snp toep->port = pi; 151255411Snp toep->tx_total = tx_credits; 152237263Snp toep->tx_credits = tx_credits; 153237263Snp toep->ofld_txq = &sc->sge.ofld_txq[txqid]; 154237263Snp toep->ofld_rxq = &sc->sge.ofld_rxq[rxqid]; 155237263Snp toep->ctrlq = &sc->sge.ctrlq[pi->port_id]; 156237263Snp toep->txsd_total = txsd_total; 157237263Snp toep->txsd_avail = txsd_total; 158237263Snp toep->txsd_pidx = 0; 159237263Snp toep->txsd_cidx = 0; 160237263Snp 161237263Snp return (toep); 162237263Snp} 163237263Snp 164237263Snpvoid 165237263Snpfree_toepcb(struct toepcb *toep) 166237263Snp{ 167237263Snp 168239514Snp KASSERT(!(toep->flags & TPF_ATTACHED), 169237263Snp ("%s: attached to an inpcb", __func__)); 170239514Snp KASSERT(!(toep->flags & TPF_CPL_PENDING), 171237263Snp ("%s: CPL pending", __func__)); 172237263Snp 173237263Snp free(toep, M_CXGBE); 174237263Snp} 175237263Snp 176237263Snp/* 177237263Snp * Set up the socket for TCP offload. 178237263Snp */ 179237263Snpvoid 180237263Snpoffload_socket(struct socket *so, struct toepcb *toep) 181237263Snp{ 182237263Snp struct tom_data *td = toep->td; 183237263Snp struct inpcb *inp = sotoinpcb(so); 184237263Snp struct tcpcb *tp = intotcpcb(inp); 185237263Snp struct sockbuf *sb; 186237263Snp 187237263Snp INP_WLOCK_ASSERT(inp); 188237263Snp 189237263Snp /* Update socket */ 190237263Snp sb = &so->so_snd; 191237263Snp SOCKBUF_LOCK(sb); 192237263Snp sb->sb_flags |= SB_NOCOALESCE; 193237263Snp SOCKBUF_UNLOCK(sb); 194237263Snp sb = &so->so_rcv; 195237263Snp SOCKBUF_LOCK(sb); 196237263Snp sb->sb_flags |= SB_NOCOALESCE; 197245441Snp if (toep->ulp_mode == ULP_MODE_TCPDDP) { 198245441Snp if (inp->inp_vflag & INP_IPV6) 199245441Snp so->so_proto = &ddp6_protosw; 200245441Snp else 201245441Snp so->so_proto = &ddp_protosw; 202245441Snp } 203237263Snp SOCKBUF_UNLOCK(sb); 204237263Snp 205237263Snp /* Update TCP PCB */ 206237263Snp tp->tod = &td->tod; 207237263Snp tp->t_toe = toep; 208237263Snp tp->t_flags |= TF_TOE; 209237263Snp 210237263Snp /* Install an extra hold on inp */ 211237263Snp toep->inp = inp; 212239514Snp toep->flags |= TPF_ATTACHED; 213237263Snp in_pcbref(inp); 214237263Snp 215237263Snp /* Add the TOE PCB to the active list */ 216237263Snp mtx_lock(&td->toep_list_lock); 217237263Snp TAILQ_INSERT_HEAD(&td->toep_list, toep, link); 218237263Snp mtx_unlock(&td->toep_list_lock); 219237263Snp} 220237263Snp 221237263Snp/* This is _not_ the normal way to "unoffload" a socket. */ 222237263Snpvoid 223237263Snpundo_offload_socket(struct socket *so) 224237263Snp{ 225237263Snp struct inpcb *inp = sotoinpcb(so); 226237263Snp struct tcpcb *tp = intotcpcb(inp); 227237263Snp struct toepcb *toep = tp->t_toe; 228237263Snp struct tom_data *td = toep->td; 229237263Snp struct sockbuf *sb; 230237263Snp 231237263Snp INP_WLOCK_ASSERT(inp); 232237263Snp 233237263Snp sb = &so->so_snd; 234237263Snp SOCKBUF_LOCK(sb); 235237263Snp sb->sb_flags &= ~SB_NOCOALESCE; 236237263Snp SOCKBUF_UNLOCK(sb); 237237263Snp sb = &so->so_rcv; 238237263Snp SOCKBUF_LOCK(sb); 239237263Snp sb->sb_flags &= ~SB_NOCOALESCE; 240237263Snp SOCKBUF_UNLOCK(sb); 241237263Snp 242237263Snp tp->tod = NULL; 243237263Snp tp->t_toe = NULL; 244237263Snp tp->t_flags &= ~TF_TOE; 245237263Snp 246237263Snp toep->inp = NULL; 247239514Snp toep->flags &= ~TPF_ATTACHED; 248237263Snp if (in_pcbrele_wlocked(inp)) 249237263Snp panic("%s: inp freed.", __func__); 250237263Snp 251237263Snp mtx_lock(&td->toep_list_lock); 252237263Snp TAILQ_REMOVE(&td->toep_list, toep, link); 253237263Snp mtx_unlock(&td->toep_list_lock); 254237263Snp} 255237263Snp 256237263Snpstatic void 257237263Snprelease_offload_resources(struct toepcb *toep) 258237263Snp{ 259237263Snp struct tom_data *td = toep->td; 260237263Snp struct adapter *sc = td_adapter(td); 261237263Snp int tid = toep->tid; 262237263Snp 263239514Snp KASSERT(!(toep->flags & TPF_CPL_PENDING), 264237263Snp ("%s: %p has CPL pending.", __func__, toep)); 265239514Snp KASSERT(!(toep->flags & TPF_ATTACHED), 266237263Snp ("%s: %p is still attached.", __func__, toep)); 267237263Snp 268245448Snp CTR5(KTR_CXGBE, "%s: toep %p (tid %d, l2te %p, ce %p)", 269245448Snp __func__, toep, tid, toep->l2te, toep->ce); 270237263Snp 271239344Snp if (toep->ulp_mode == ULP_MODE_TCPDDP) 272239344Snp release_ddp_resources(toep); 273239344Snp 274237263Snp if (toep->l2te) 275237263Snp t4_l2t_release(toep->l2te); 276237263Snp 277237263Snp if (tid >= 0) { 278237263Snp remove_tid(sc, tid); 279237263Snp release_tid(sc, tid, toep->ctrlq); 280237263Snp } 281237263Snp 282245448Snp if (toep->ce) 283245448Snp release_lip(td, toep->ce); 284245448Snp 285237263Snp mtx_lock(&td->toep_list_lock); 286237263Snp TAILQ_REMOVE(&td->toep_list, toep, link); 287237263Snp mtx_unlock(&td->toep_list_lock); 288237263Snp 289237263Snp free_toepcb(toep); 290237263Snp} 291237263Snp 292237263Snp/* 293237263Snp * The kernel is done with the TCP PCB and this is our opportunity to unhook the 294237263Snp * toepcb hanging off of it. If the TOE driver is also done with the toepcb (no 295237263Snp * pending CPL) then it is time to release all resources tied to the toepcb. 296237263Snp * 297237263Snp * Also gets called when an offloaded active open fails and the TOM wants the 298237263Snp * kernel to take the TCP PCB back. 299237263Snp */ 300237263Snpstatic void 301237263Snpt4_pcb_detach(struct toedev *tod __unused, struct tcpcb *tp) 302237263Snp{ 303237263Snp#if defined(KTR) || defined(INVARIANTS) 304237263Snp struct inpcb *inp = tp->t_inpcb; 305237263Snp#endif 306237263Snp struct toepcb *toep = tp->t_toe; 307237263Snp 308237263Snp INP_WLOCK_ASSERT(inp); 309237263Snp 310237263Snp KASSERT(toep != NULL, ("%s: toep is NULL", __func__)); 311239514Snp KASSERT(toep->flags & TPF_ATTACHED, 312237263Snp ("%s: not attached", __func__)); 313237263Snp 314237263Snp#ifdef KTR 315237263Snp if (tp->t_state == TCPS_SYN_SENT) { 316237263Snp CTR6(KTR_CXGBE, "%s: atid %d, toep %p (0x%x), inp %p (0x%x)", 317237263Snp __func__, toep->tid, toep, toep->flags, inp, 318237263Snp inp->inp_flags); 319237263Snp } else { 320237263Snp CTR6(KTR_CXGBE, 321237263Snp "t4_pcb_detach: tid %d (%s), toep %p (0x%x), inp %p (0x%x)", 322237263Snp toep->tid, tcpstates[tp->t_state], toep, toep->flags, inp, 323237263Snp inp->inp_flags); 324237263Snp } 325237263Snp#endif 326237263Snp 327237263Snp tp->t_toe = NULL; 328237263Snp tp->t_flags &= ~TF_TOE; 329239514Snp toep->flags &= ~TPF_ATTACHED; 330237263Snp 331239514Snp if (!(toep->flags & TPF_CPL_PENDING)) 332237263Snp release_offload_resources(toep); 333237263Snp} 334237263Snp 335237263Snp/* 336252716Snp * setsockopt handler. 337252716Snp */ 338252716Snpstatic void 339252716Snpt4_ctloutput(struct toedev *tod, struct tcpcb *tp, int dir, int name) 340252716Snp{ 341252716Snp struct adapter *sc = tod->tod_softc; 342252716Snp struct toepcb *toep = tp->t_toe; 343252716Snp 344252716Snp if (dir == SOPT_GET) 345252716Snp return; 346252716Snp 347252716Snp CTR4(KTR_CXGBE, "%s: tp %p, dir %u, name %u", __func__, tp, dir, name); 348252716Snp 349252716Snp switch (name) { 350252716Snp case TCP_NODELAY: 351252716Snp t4_set_tcb_field(sc, toep, 1, W_TCB_T_FLAGS, V_TF_NAGLE(1), 352252716Snp V_TF_NAGLE(tp->t_flags & TF_NODELAY ? 0 : 1)); 353252716Snp break; 354252716Snp default: 355252716Snp break; 356252716Snp } 357252716Snp} 358252716Snp 359252716Snp/* 360237263Snp * The TOE driver will not receive any more CPLs for the tid associated with the 361237263Snp * toepcb; release the hold on the inpcb. 362237263Snp */ 363237263Snpvoid 364237263Snpfinal_cpl_received(struct toepcb *toep) 365237263Snp{ 366237263Snp struct inpcb *inp = toep->inp; 367237263Snp 368237263Snp KASSERT(inp != NULL, ("%s: inp is NULL", __func__)); 369237263Snp INP_WLOCK_ASSERT(inp); 370239514Snp KASSERT(toep->flags & TPF_CPL_PENDING, 371237263Snp ("%s: CPL not pending already?", __func__)); 372237263Snp 373237263Snp CTR6(KTR_CXGBE, "%s: tid %d, toep %p (0x%x), inp %p (0x%x)", 374237263Snp __func__, toep->tid, toep, toep->flags, inp, inp->inp_flags); 375237263Snp 376237263Snp toep->inp = NULL; 377239514Snp toep->flags &= ~TPF_CPL_PENDING; 378237263Snp 379239514Snp if (!(toep->flags & TPF_ATTACHED)) 380237263Snp release_offload_resources(toep); 381237263Snp 382237263Snp if (!in_pcbrele_wlocked(inp)) 383237263Snp INP_WUNLOCK(inp); 384237263Snp} 385237263Snp 386237263Snpvoid 387237263Snpinsert_tid(struct adapter *sc, int tid, void *ctx) 388237263Snp{ 389237263Snp struct tid_info *t = &sc->tids; 390237263Snp 391237263Snp t->tid_tab[tid] = ctx; 392237263Snp atomic_add_int(&t->tids_in_use, 1); 393237263Snp} 394237263Snp 395237263Snpvoid * 396237263Snplookup_tid(struct adapter *sc, int tid) 397237263Snp{ 398237263Snp struct tid_info *t = &sc->tids; 399237263Snp 400237263Snp return (t->tid_tab[tid]); 401237263Snp} 402237263Snp 403237263Snpvoid 404237263Snpupdate_tid(struct adapter *sc, int tid, void *ctx) 405237263Snp{ 406237263Snp struct tid_info *t = &sc->tids; 407237263Snp 408237263Snp t->tid_tab[tid] = ctx; 409237263Snp} 410237263Snp 411237263Snpvoid 412237263Snpremove_tid(struct adapter *sc, int tid) 413237263Snp{ 414237263Snp struct tid_info *t = &sc->tids; 415237263Snp 416237263Snp t->tid_tab[tid] = NULL; 417237263Snp atomic_subtract_int(&t->tids_in_use, 1); 418237263Snp} 419237263Snp 420237263Snpvoid 421237263Snprelease_tid(struct adapter *sc, int tid, struct sge_wrq *ctrlq) 422237263Snp{ 423237263Snp struct wrqe *wr; 424237263Snp struct cpl_tid_release *req; 425237263Snp 426237263Snp wr = alloc_wrqe(sizeof(*req), ctrlq); 427237263Snp if (wr == NULL) { 428237263Snp queue_tid_release(sc, tid); /* defer */ 429237263Snp return; 430237263Snp } 431237263Snp req = wrtod(wr); 432237263Snp 433237263Snp INIT_TP_WR_MIT_CPL(req, CPL_TID_RELEASE, tid); 434237263Snp 435237263Snp t4_wrq_tx(sc, wr); 436237263Snp} 437237263Snp 438237263Snpstatic void 439237263Snpqueue_tid_release(struct adapter *sc, int tid) 440237263Snp{ 441237263Snp 442237263Snp CXGBE_UNIMPLEMENTED("deferred tid release"); 443237263Snp} 444237263Snp 445237263Snp/* 446237263Snp * What mtu_idx to use, given a 4-tuple and/or an MSS cap 447237263Snp */ 448237263Snpint 449237263Snpfind_best_mtu_idx(struct adapter *sc, struct in_conninfo *inc, int pmss) 450237263Snp{ 451237263Snp unsigned short *mtus = &sc->params.mtus[0]; 452245441Snp int i, mss, n; 453237263Snp 454237263Snp KASSERT(inc != NULL || pmss > 0, 455237263Snp ("%s: at least one of inc/pmss must be specified", __func__)); 456237263Snp 457237263Snp mss = inc ? tcp_mssopt(inc) : pmss; 458237263Snp if (pmss > 0 && mss > pmss) 459237263Snp mss = pmss; 460237263Snp 461245441Snp if (inc->inc_flags & INC_ISIPV6) 462245441Snp n = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 463245441Snp else 464245441Snp n = sizeof(struct ip) + sizeof(struct tcphdr); 465237263Snp 466245441Snp for (i = 0; i < NMTUS - 1 && mtus[i + 1] <= mss + n; i++) 467245441Snp continue; 468245441Snp 469237263Snp return (i); 470237263Snp} 471237263Snp 472237263Snp/* 473237263Snp * Determine the receive window size for a socket. 474237263Snp */ 475237263Snpu_long 476237263Snpselect_rcv_wnd(struct socket *so) 477237263Snp{ 478237263Snp unsigned long wnd; 479237263Snp 480237263Snp SOCKBUF_LOCK_ASSERT(&so->so_rcv); 481237263Snp 482237263Snp wnd = sbspace(&so->so_rcv); 483237263Snp if (wnd < MIN_RCV_WND) 484237263Snp wnd = MIN_RCV_WND; 485237263Snp 486237263Snp return min(wnd, MAX_RCV_WND); 487237263Snp} 488237263Snp 489237263Snpint 490237263Snpselect_rcv_wscale(void) 491237263Snp{ 492237263Snp int wscale = 0; 493237263Snp unsigned long space = sb_max; 494237263Snp 495237263Snp if (space > MAX_RCV_WND) 496237263Snp space = MAX_RCV_WND; 497237263Snp 498237263Snp while (wscale < TCP_MAX_WINSHIFT && (TCP_MAXWIN << wscale) < space) 499237263Snp wscale++; 500237263Snp 501237263Snp return (wscale); 502237263Snp} 503237263Snp 504237263Snpextern int always_keepalive; 505237263Snp#define VIID_SMACIDX(v) (((unsigned int)(v) & 0x7f) << 1) 506237263Snp 507237263Snp/* 508237263Snp * socket so could be a listening socket too. 509237263Snp */ 510237263Snpuint64_t 511237263Snpcalc_opt0(struct socket *so, struct port_info *pi, struct l2t_entry *e, 512237263Snp int mtu_idx, int rscale, int rx_credits, int ulp_mode) 513237263Snp{ 514237263Snp uint64_t opt0; 515237263Snp 516237263Snp KASSERT(rx_credits <= M_RCV_BUFSIZ, 517237263Snp ("%s: rcv_bufsiz too high", __func__)); 518237263Snp 519237263Snp opt0 = F_TCAM_BYPASS | V_WND_SCALE(rscale) | V_MSS_IDX(mtu_idx) | 520237263Snp V_ULP_MODE(ulp_mode) | V_RCV_BUFSIZ(rx_credits); 521237263Snp 522237263Snp if (so != NULL) { 523237263Snp struct inpcb *inp = sotoinpcb(so); 524237263Snp struct tcpcb *tp = intotcpcb(inp); 525237263Snp int keepalive = always_keepalive || 526237263Snp so_options_get(so) & SO_KEEPALIVE; 527237263Snp 528237263Snp opt0 |= V_NAGLE((tp->t_flags & TF_NODELAY) == 0); 529237263Snp opt0 |= V_KEEP_ALIVE(keepalive != 0); 530237263Snp } 531237263Snp 532237263Snp if (e != NULL) 533237263Snp opt0 |= V_L2T_IDX(e->idx); 534237263Snp 535237263Snp if (pi != NULL) { 536237263Snp opt0 |= V_SMAC_SEL(VIID_SMACIDX(pi->viid)); 537237263Snp opt0 |= V_TX_CHAN(pi->tx_chan); 538237263Snp } 539237263Snp 540237263Snp return htobe64(opt0); 541237263Snp} 542237263Snp 543248925Snpuint64_t 544252705Snpselect_ntuple(struct port_info *pi, struct l2t_entry *e) 545237263Snp{ 546252705Snp struct adapter *sc = pi->adapter; 547252705Snp struct tp_params *tp = &sc->params.tp; 548237263Snp uint16_t viid = pi->viid; 549252705Snp uint64_t ntuple = 0; 550237263Snp 551252705Snp /* 552252705Snp * Initialize each of the fields which we care about which are present 553252705Snp * in the Compressed Filter Tuple. 554252705Snp */ 555252705Snp if (tp->vlan_shift >= 0 && e->vlan != CPL_L2T_VLAN_NONE) 556252705Snp ntuple |= (uint64_t)(F_FT_VLAN_VLD | e->vlan) << tp->vlan_shift; 557237263Snp 558252705Snp if (tp->port_shift >= 0) 559252705Snp ntuple |= (uint64_t)e->lport << tp->port_shift; 560252705Snp 561252705Snp if (tp->protocol_shift >= 0) 562252705Snp ntuple |= (uint64_t)IPPROTO_TCP << tp->protocol_shift; 563252705Snp 564252705Snp if (tp->vnic_shift >= 0) { 565252705Snp uint32_t vf = G_FW_VIID_VIN(viid); 566252705Snp uint32_t pf = G_FW_VIID_PFN(viid); 567252705Snp uint32_t vld = G_FW_VIID_VIVLD(viid); 568252705Snp 569252705Snp ntuple |= (uint64_t)(V_FT_VNID_ID_VF(vf) | V_FT_VNID_ID_PF(pf) | 570252705Snp V_FT_VNID_ID_VLD(vld)) << tp->vnic_shift; 571252705Snp } 572252705Snp 573252705Snp if (is_t4(sc)) 574252705Snp return (htobe32((uint32_t)ntuple)); 575248925Snp else 576248925Snp return (htobe64(V_FILTER_TUPLE(ntuple))); 577237263Snp} 578237263Snp 579245441Snpvoid 580245441Snpset_tcpddp_ulp_mode(struct toepcb *toep) 581245441Snp{ 582245441Snp 583245441Snp toep->ulp_mode = ULP_MODE_TCPDDP; 584245441Snp toep->ddp_flags = DDP_OK; 585245441Snp toep->ddp_score = DDP_LOW_SCORE; 586245441Snp} 587245441Snp 588245935Snpint 589245935Snpnegative_advice(int status) 590245935Snp{ 591245935Snp 592245935Snp return (status == CPL_ERR_RTX_NEG_ADVICE || 593245935Snp status == CPL_ERR_PERSIST_NEG_ADVICE || 594245935Snp status == CPL_ERR_KEEPALV_NEG_ADVICE); 595245935Snp} 596245935Snp 597237263Snpstatic int 598237263Snpalloc_tid_tabs(struct tid_info *t) 599237263Snp{ 600237263Snp size_t size; 601237263Snp unsigned int i; 602237263Snp 603237263Snp size = t->ntids * sizeof(*t->tid_tab) + 604237263Snp t->natids * sizeof(*t->atid_tab) + 605237263Snp t->nstids * sizeof(*t->stid_tab); 606237263Snp 607237263Snp t->tid_tab = malloc(size, M_CXGBE, M_ZERO | M_NOWAIT); 608237263Snp if (t->tid_tab == NULL) 609237263Snp return (ENOMEM); 610237263Snp 611237263Snp mtx_init(&t->atid_lock, "atid lock", NULL, MTX_DEF); 612237263Snp t->atid_tab = (union aopen_entry *)&t->tid_tab[t->ntids]; 613237263Snp t->afree = t->atid_tab; 614237263Snp t->atids_in_use = 0; 615237263Snp for (i = 1; i < t->natids; i++) 616237263Snp t->atid_tab[i - 1].next = &t->atid_tab[i]; 617237263Snp t->atid_tab[t->natids - 1].next = NULL; 618237263Snp 619237263Snp mtx_init(&t->stid_lock, "stid lock", NULL, MTX_DEF); 620245276Snp t->stid_tab = (struct listen_ctx **)&t->atid_tab[t->natids]; 621237263Snp t->stids_in_use = 0; 622245276Snp TAILQ_INIT(&t->stids); 623245276Snp t->nstids_free_head = t->nstids; 624237263Snp 625237263Snp atomic_store_rel_int(&t->tids_in_use, 0); 626237263Snp 627237263Snp return (0); 628237263Snp} 629237263Snp 630237263Snpstatic void 631237263Snpfree_tid_tabs(struct tid_info *t) 632237263Snp{ 633237263Snp KASSERT(t->tids_in_use == 0, 634237263Snp ("%s: %d tids still in use.", __func__, t->tids_in_use)); 635237263Snp KASSERT(t->atids_in_use == 0, 636237263Snp ("%s: %d atids still in use.", __func__, t->atids_in_use)); 637237263Snp KASSERT(t->stids_in_use == 0, 638237263Snp ("%s: %d tids still in use.", __func__, t->stids_in_use)); 639237263Snp 640237263Snp free(t->tid_tab, M_CXGBE); 641237263Snp t->tid_tab = NULL; 642237263Snp 643237263Snp if (mtx_initialized(&t->atid_lock)) 644237263Snp mtx_destroy(&t->atid_lock); 645237263Snp if (mtx_initialized(&t->stid_lock)) 646237263Snp mtx_destroy(&t->stid_lock); 647237263Snp} 648237263Snp 649245448Snpstatic int 650245448Snpadd_lip(struct adapter *sc, struct in6_addr *lip) 651245448Snp{ 652245448Snp struct fw_clip_cmd c; 653245448Snp 654245448Snp ASSERT_SYNCHRONIZED_OP(sc); 655245448Snp /* mtx_assert(&td->clip_table_lock, MA_OWNED); */ 656245448Snp 657245448Snp memset(&c, 0, sizeof(c)); 658245448Snp c.op_to_write = htonl(V_FW_CMD_OP(FW_CLIP_CMD) | F_FW_CMD_REQUEST | 659245448Snp F_FW_CMD_WRITE); 660245448Snp c.alloc_to_len16 = htonl(F_FW_CLIP_CMD_ALLOC | FW_LEN16(c)); 661245448Snp c.ip_hi = *(uint64_t *)&lip->s6_addr[0]; 662245448Snp c.ip_lo = *(uint64_t *)&lip->s6_addr[8]; 663245448Snp 664249627Snp return (-t4_wr_mbox_ns(sc, sc->mbox, &c, sizeof(c), &c)); 665245448Snp} 666245448Snp 667245448Snpstatic int 668245448Snpdelete_lip(struct adapter *sc, struct in6_addr *lip) 669245448Snp{ 670245448Snp struct fw_clip_cmd c; 671245448Snp 672245448Snp ASSERT_SYNCHRONIZED_OP(sc); 673245448Snp /* mtx_assert(&td->clip_table_lock, MA_OWNED); */ 674245448Snp 675245448Snp memset(&c, 0, sizeof(c)); 676245448Snp c.op_to_write = htonl(V_FW_CMD_OP(FW_CLIP_CMD) | F_FW_CMD_REQUEST | 677245448Snp F_FW_CMD_READ); 678245448Snp c.alloc_to_len16 = htonl(F_FW_CLIP_CMD_FREE | FW_LEN16(c)); 679245448Snp c.ip_hi = *(uint64_t *)&lip->s6_addr[0]; 680245448Snp c.ip_lo = *(uint64_t *)&lip->s6_addr[8]; 681245448Snp 682249627Snp return (-t4_wr_mbox_ns(sc, sc->mbox, &c, sizeof(c), &c)); 683245448Snp} 684245448Snp 685245448Snpstatic struct clip_entry * 686245448Snpsearch_lip(struct tom_data *td, struct in6_addr *lip) 687245448Snp{ 688245448Snp struct clip_entry *ce; 689245448Snp 690245448Snp mtx_assert(&td->clip_table_lock, MA_OWNED); 691245448Snp 692245448Snp TAILQ_FOREACH(ce, &td->clip_table, link) { 693245448Snp if (IN6_ARE_ADDR_EQUAL(&ce->lip, lip)) 694245448Snp return (ce); 695245448Snp } 696245448Snp 697245448Snp return (NULL); 698245448Snp} 699245448Snp 700245448Snpstruct clip_entry * 701245448Snphold_lip(struct tom_data *td, struct in6_addr *lip) 702245448Snp{ 703245448Snp struct clip_entry *ce; 704245448Snp 705245448Snp mtx_lock(&td->clip_table_lock); 706245448Snp ce = search_lip(td, lip); 707245448Snp if (ce != NULL) 708245448Snp ce->refcount++; 709245448Snp mtx_unlock(&td->clip_table_lock); 710245448Snp 711245448Snp return (ce); 712245448Snp} 713245448Snp 714245448Snpvoid 715245448Snprelease_lip(struct tom_data *td, struct clip_entry *ce) 716245448Snp{ 717245448Snp 718245448Snp mtx_lock(&td->clip_table_lock); 719245448Snp KASSERT(search_lip(td, &ce->lip) == ce, 720245448Snp ("%s: CLIP entry %p p not in CLIP table.", __func__, ce)); 721245448Snp KASSERT(ce->refcount > 0, 722245448Snp ("%s: CLIP entry %p has refcount 0", __func__, ce)); 723245448Snp --ce->refcount; 724245448Snp mtx_unlock(&td->clip_table_lock); 725245448Snp} 726245448Snp 727237263Snpstatic void 728245448Snpinit_clip_table(struct adapter *sc, struct tom_data *td) 729245448Snp{ 730245448Snp 731245448Snp ASSERT_SYNCHRONIZED_OP(sc); 732245448Snp 733245448Snp mtx_init(&td->clip_table_lock, "CLIP table lock", NULL, MTX_DEF); 734245448Snp TAILQ_INIT(&td->clip_table); 735249627Snp td->clip_gen = -1; 736245448Snp 737249627Snp update_clip_table(sc, td); 738249627Snp} 739249627Snp 740249627Snpstatic void 741249627Snpupdate_clip(struct adapter *sc, void *arg __unused) 742249627Snp{ 743249627Snp 744249627Snp if (begin_synchronized_op(sc, NULL, HOLD_LOCK, "t4tomuc")) 745249627Snp return; 746249627Snp 747249627Snp if (sc->flags & TOM_INIT_DONE) 748249627Snp update_clip_table(sc, sc->tom_softc); 749249627Snp 750249627Snp end_synchronized_op(sc, LOCK_HELD); 751249627Snp} 752249627Snp 753249627Snpstatic void 754249627Snpt4_clip_task(void *arg, int count) 755249627Snp{ 756249627Snp 757249627Snp t4_iterate(update_clip, NULL); 758249627Snp} 759249627Snp 760249627Snpstatic void 761249627Snpupdate_clip_table(struct adapter *sc, struct tom_data *td) 762249627Snp{ 763249627Snp struct in6_ifaddr *ia; 764249627Snp struct in6_addr *lip, tlip; 765249627Snp struct clip_head stale; 766249627Snp struct clip_entry *ce, *ce_temp; 767249627Snp int rc, gen = atomic_load_acq_int(&in6_ifaddr_gen); 768249627Snp 769249627Snp ASSERT_SYNCHRONIZED_OP(sc); 770249627Snp 771245448Snp IN6_IFADDR_RLOCK(); 772249627Snp mtx_lock(&td->clip_table_lock); 773249627Snp 774249627Snp if (gen == td->clip_gen) 775249627Snp goto done; 776249627Snp 777249627Snp TAILQ_INIT(&stale); 778249627Snp TAILQ_CONCAT(&stale, &td->clip_table, link); 779249627Snp 780245448Snp TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) { 781245448Snp lip = &ia->ia_addr.sin6_addr; 782245448Snp 783245448Snp KASSERT(!IN6_IS_ADDR_MULTICAST(lip), 784245448Snp ("%s: mcast address in in6_ifaddr list", __func__)); 785245448Snp 786245448Snp if (IN6_IS_ADDR_LOOPBACK(lip)) 787245448Snp continue; 788245448Snp if (IN6_IS_SCOPE_EMBED(lip)) { 789245448Snp /* Remove the embedded scope */ 790245448Snp tlip = *lip; 791245448Snp lip = &tlip; 792245448Snp in6_clearscope(lip); 793245448Snp } 794245448Snp /* 795245448Snp * XXX: how to weed out the link local address for the loopback 796245448Snp * interface? It's fe80::1 usually (always?). 797245448Snp */ 798245448Snp 799249627Snp /* 800249627Snp * If it's in the main list then we already know it's not stale. 801249627Snp */ 802249627Snp TAILQ_FOREACH(ce, &td->clip_table, link) { 803249627Snp if (IN6_ARE_ADDR_EQUAL(&ce->lip, lip)) 804249627Snp goto next; 805249627Snp } 806249627Snp 807249627Snp /* 808249627Snp * If it's in the stale list we should move it to the main list. 809249627Snp */ 810249627Snp TAILQ_FOREACH(ce, &stale, link) { 811249627Snp if (IN6_ARE_ADDR_EQUAL(&ce->lip, lip)) { 812249627Snp TAILQ_REMOVE(&stale, ce, link); 813245448Snp TAILQ_INSERT_TAIL(&td->clip_table, ce, link); 814249627Snp goto next; 815249627Snp } 816249627Snp } 817249627Snp 818249627Snp /* A new IP6 address; add it to the CLIP table */ 819249627Snp ce = malloc(sizeof(*ce), M_CXGBE, M_NOWAIT); 820249627Snp memcpy(&ce->lip, lip, sizeof(ce->lip)); 821249627Snp ce->refcount = 0; 822249627Snp rc = add_lip(sc, lip); 823249627Snp if (rc == 0) 824249627Snp TAILQ_INSERT_TAIL(&td->clip_table, ce, link); 825249627Snp else { 826249627Snp char ip[INET6_ADDRSTRLEN]; 827249627Snp 828249627Snp inet_ntop(AF_INET6, &ce->lip, &ip[0], sizeof(ip)); 829249627Snp log(LOG_ERR, "%s: could not add %s (%d)\n", 830249627Snp __func__, ip, rc); 831249627Snp free(ce, M_CXGBE); 832249627Snp } 833249627Snpnext: 834249627Snp continue; 835249627Snp } 836249627Snp 837249627Snp /* 838249627Snp * Remove stale addresses (those no longer in V_in6_ifaddrhead) that are 839249627Snp * no longer referenced by the driver. 840249627Snp */ 841249627Snp TAILQ_FOREACH_SAFE(ce, &stale, link, ce_temp) { 842249627Snp if (ce->refcount == 0) { 843249627Snp rc = delete_lip(sc, &ce->lip); 844249627Snp if (rc == 0) { 845249627Snp TAILQ_REMOVE(&stale, ce, link); 846245448Snp free(ce, M_CXGBE); 847249627Snp } else { 848249627Snp char ip[INET6_ADDRSTRLEN]; 849249627Snp 850249627Snp inet_ntop(AF_INET6, &ce->lip, &ip[0], 851249627Snp sizeof(ip)); 852249627Snp log(LOG_ERR, "%s: could not delete %s (%d)\n", 853249627Snp __func__, ip, rc); 854249627Snp } 855245448Snp } 856245448Snp } 857249627Snp /* The ones that are still referenced need to stay in the CLIP table */ 858249627Snp TAILQ_CONCAT(&td->clip_table, &stale, link); 859249627Snp 860249627Snp td->clip_gen = gen; 861249627Snpdone: 862249627Snp mtx_unlock(&td->clip_table_lock); 863245448Snp IN6_IFADDR_RUNLOCK(); 864245448Snp} 865245448Snp 866245448Snpstatic void 867245448Snpdestroy_clip_table(struct adapter *sc, struct tom_data *td) 868245448Snp{ 869245448Snp struct clip_entry *ce, *ce_temp; 870245448Snp 871245448Snp if (mtx_initialized(&td->clip_table_lock)) { 872245448Snp mtx_lock(&td->clip_table_lock); 873245448Snp TAILQ_FOREACH_SAFE(ce, &td->clip_table, link, ce_temp) { 874245448Snp KASSERT(ce->refcount == 0, 875245448Snp ("%s: CLIP entry %p still in use (%d)", __func__, 876245448Snp ce, ce->refcount)); 877245448Snp TAILQ_REMOVE(&td->clip_table, ce, link); 878245448Snp delete_lip(sc, &ce->lip); 879245448Snp free(ce, M_CXGBE); 880245448Snp } 881245448Snp mtx_unlock(&td->clip_table_lock); 882245448Snp mtx_destroy(&td->clip_table_lock); 883245448Snp } 884245448Snp} 885245448Snp 886245448Snpstatic void 887237263Snpfree_tom_data(struct adapter *sc, struct tom_data *td) 888237263Snp{ 889245448Snp 890245448Snp ASSERT_SYNCHRONIZED_OP(sc); 891245448Snp 892237263Snp KASSERT(TAILQ_EMPTY(&td->toep_list), 893237263Snp ("%s: TOE PCB list is not empty.", __func__)); 894237263Snp KASSERT(td->lctx_count == 0, 895237263Snp ("%s: lctx hash table is not empty.", __func__)); 896237263Snp 897237263Snp t4_uninit_l2t_cpl_handlers(sc); 898239344Snp t4_uninit_cpl_io_handlers(sc); 899239344Snp t4_uninit_ddp(sc, td); 900245448Snp destroy_clip_table(sc, td); 901237263Snp 902237263Snp if (td->listen_mask != 0) 903237263Snp hashdestroy(td->listen_hash, M_CXGBE, td->listen_mask); 904237263Snp 905237263Snp if (mtx_initialized(&td->lctx_hash_lock)) 906237263Snp mtx_destroy(&td->lctx_hash_lock); 907237263Snp if (mtx_initialized(&td->toep_list_lock)) 908237263Snp mtx_destroy(&td->toep_list_lock); 909237263Snp 910237263Snp free_tid_tabs(&sc->tids); 911237263Snp free(td, M_CXGBE); 912237263Snp} 913237263Snp 914237263Snp/* 915237263Snp * Ground control to Major TOM 916237263Snp * Commencing countdown, engines on 917237263Snp */ 918237263Snpstatic int 919237263Snpt4_tom_activate(struct adapter *sc) 920237263Snp{ 921237263Snp struct tom_data *td; 922237263Snp struct toedev *tod; 923237263Snp int i, rc; 924237263Snp 925245274Snp ASSERT_SYNCHRONIZED_OP(sc); 926237263Snp 927237263Snp /* per-adapter softc for TOM */ 928237263Snp td = malloc(sizeof(*td), M_CXGBE, M_ZERO | M_NOWAIT); 929237263Snp if (td == NULL) 930237263Snp return (ENOMEM); 931237263Snp 932237263Snp /* List of TOE PCBs and associated lock */ 933237263Snp mtx_init(&td->toep_list_lock, "PCB list lock", NULL, MTX_DEF); 934237263Snp TAILQ_INIT(&td->toep_list); 935237263Snp 936237263Snp /* Listen context */ 937237263Snp mtx_init(&td->lctx_hash_lock, "lctx hash lock", NULL, MTX_DEF); 938237263Snp td->listen_hash = hashinit_flags(LISTEN_HASH_SIZE, M_CXGBE, 939237263Snp &td->listen_mask, HASH_NOWAIT); 940237263Snp 941237263Snp /* TID tables */ 942237263Snp rc = alloc_tid_tabs(&sc->tids); 943237263Snp if (rc != 0) 944237263Snp goto done; 945237263Snp 946245448Snp /* DDP page pods and CPL handlers */ 947239344Snp t4_init_ddp(sc, td); 948239344Snp 949245448Snp /* CLIP table for IPv6 offload */ 950245448Snp init_clip_table(sc, td); 951245448Snp 952237263Snp /* CPL handlers */ 953237263Snp t4_init_connect_cpl_handlers(sc); 954237263Snp t4_init_l2t_cpl_handlers(sc); 955237263Snp t4_init_listen_cpl_handlers(sc); 956237263Snp t4_init_cpl_io_handlers(sc); 957237263Snp 958237263Snp /* toedev ops */ 959237263Snp tod = &td->tod; 960237263Snp init_toedev(tod); 961237263Snp tod->tod_softc = sc; 962237263Snp tod->tod_connect = t4_connect; 963237263Snp tod->tod_listen_start = t4_listen_start; 964237263Snp tod->tod_listen_stop = t4_listen_stop; 965237263Snp tod->tod_rcvd = t4_rcvd; 966237263Snp tod->tod_output = t4_tod_output; 967237263Snp tod->tod_send_rst = t4_send_rst; 968237263Snp tod->tod_send_fin = t4_send_fin; 969237263Snp tod->tod_pcb_detach = t4_pcb_detach; 970237263Snp tod->tod_l2_update = t4_l2_update; 971237263Snp tod->tod_syncache_added = t4_syncache_added; 972237263Snp tod->tod_syncache_removed = t4_syncache_removed; 973237263Snp tod->tod_syncache_respond = t4_syncache_respond; 974237263Snp tod->tod_offload_socket = t4_offload_socket; 975252716Snp tod->tod_ctloutput = t4_ctloutput; 976237263Snp 977237263Snp for_each_port(sc, i) 978237263Snp TOEDEV(sc->port[i]->ifp) = &td->tod; 979237263Snp 980237263Snp sc->tom_softc = td; 981237263Snp sc->flags |= TOM_INIT_DONE; 982237263Snp register_toedev(sc->tom_softc); 983237263Snp 984237263Snpdone: 985237263Snp if (rc != 0) 986237263Snp free_tom_data(sc, td); 987237263Snp return (rc); 988237263Snp} 989237263Snp 990237263Snpstatic int 991237263Snpt4_tom_deactivate(struct adapter *sc) 992237263Snp{ 993237263Snp int rc = 0; 994237263Snp struct tom_data *td = sc->tom_softc; 995237263Snp 996245274Snp ASSERT_SYNCHRONIZED_OP(sc); 997237263Snp 998237263Snp if (td == NULL) 999237263Snp return (0); /* XXX. KASSERT? */ 1000237263Snp 1001237263Snp if (sc->offload_map != 0) 1002237263Snp return (EBUSY); /* at least one port has IFCAP_TOE enabled */ 1003237263Snp 1004237263Snp mtx_lock(&td->toep_list_lock); 1005237263Snp if (!TAILQ_EMPTY(&td->toep_list)) 1006237263Snp rc = EBUSY; 1007237263Snp mtx_unlock(&td->toep_list_lock); 1008237263Snp 1009237263Snp mtx_lock(&td->lctx_hash_lock); 1010237263Snp if (td->lctx_count > 0) 1011237263Snp rc = EBUSY; 1012237263Snp mtx_unlock(&td->lctx_hash_lock); 1013237263Snp 1014237263Snp if (rc == 0) { 1015237263Snp unregister_toedev(sc->tom_softc); 1016237263Snp free_tom_data(sc, td); 1017237263Snp sc->tom_softc = NULL; 1018237263Snp sc->flags &= ~TOM_INIT_DONE; 1019237263Snp } 1020237263Snp 1021237263Snp return (rc); 1022237263Snp} 1023237263Snp 1024249627Snpstatic void 1025249627Snpt4_tom_ifaddr_event(void *arg __unused, struct ifnet *ifp) 1026249627Snp{ 1027249627Snp 1028249627Snp atomic_add_rel_int(&in6_ifaddr_gen, 1); 1029249627Snp taskqueue_enqueue_timeout(taskqueue_thread, &clip_task, -hz / 4); 1030249627Snp} 1031249627Snp 1032237263Snpstatic int 1033237263Snpt4_tom_mod_load(void) 1034237263Snp{ 1035237263Snp int rc; 1036245441Snp struct protosw *tcp_protosw, *tcp6_protosw; 1037237263Snp 1038239344Snp tcp_protosw = pffindproto(PF_INET, IPPROTO_TCP, SOCK_STREAM); 1039239344Snp if (tcp_protosw == NULL) 1040239344Snp return (ENOPROTOOPT); 1041239344Snp bcopy(tcp_protosw, &ddp_protosw, sizeof(ddp_protosw)); 1042239344Snp bcopy(tcp_protosw->pr_usrreqs, &ddp_usrreqs, sizeof(ddp_usrreqs)); 1043239344Snp ddp_usrreqs.pru_soreceive = t4_soreceive_ddp; 1044239344Snp ddp_protosw.pr_usrreqs = &ddp_usrreqs; 1045239344Snp 1046245441Snp tcp6_protosw = pffindproto(PF_INET6, IPPROTO_TCP, SOCK_STREAM); 1047245441Snp if (tcp6_protosw == NULL) 1048245441Snp return (ENOPROTOOPT); 1049245441Snp bcopy(tcp6_protosw, &ddp6_protosw, sizeof(ddp6_protosw)); 1050245441Snp bcopy(tcp6_protosw->pr_usrreqs, &ddp6_usrreqs, sizeof(ddp6_usrreqs)); 1051245441Snp ddp6_usrreqs.pru_soreceive = t4_soreceive_ddp; 1052245441Snp ddp6_protosw.pr_usrreqs = &ddp6_usrreqs; 1053245441Snp 1054249627Snp TIMEOUT_TASK_INIT(taskqueue_thread, &clip_task, 0, t4_clip_task, NULL); 1055249627Snp ifaddr_evhandler = EVENTHANDLER_REGISTER(ifaddr_event, 1056249627Snp t4_tom_ifaddr_event, NULL, EVENTHANDLER_PRI_ANY); 1057249627Snp 1058237263Snp rc = t4_register_uld(&tom_uld_info); 1059237263Snp if (rc != 0) 1060237263Snp t4_tom_mod_unload(); 1061237263Snp 1062237263Snp return (rc); 1063237263Snp} 1064237263Snp 1065237263Snpstatic void 1066237263Snptom_uninit(struct adapter *sc, void *arg __unused) 1067237263Snp{ 1068255006Snp if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4tomun")) 1069245274Snp return; 1070245274Snp 1071237263Snp /* Try to free resources (works only if no port has IFCAP_TOE) */ 1072237263Snp if (sc->flags & TOM_INIT_DONE) 1073237263Snp t4_deactivate_uld(sc, ULD_TOM); 1074245274Snp 1075255006Snp end_synchronized_op(sc, 0); 1076237263Snp} 1077237263Snp 1078237263Snpstatic int 1079237263Snpt4_tom_mod_unload(void) 1080237263Snp{ 1081237263Snp t4_iterate(tom_uninit, NULL); 1082237263Snp 1083237263Snp if (t4_unregister_uld(&tom_uld_info) == EBUSY) 1084237263Snp return (EBUSY); 1085237263Snp 1086249627Snp if (ifaddr_evhandler) { 1087249627Snp EVENTHANDLER_DEREGISTER(ifaddr_event, ifaddr_evhandler); 1088249627Snp taskqueue_cancel_timeout(taskqueue_thread, &clip_task, NULL); 1089249627Snp } 1090249627Snp 1091237263Snp return (0); 1092237263Snp} 1093237263Snp#endif /* TCP_OFFLOAD */ 1094237263Snp 1095237263Snpstatic int 1096237263Snpt4_tom_modevent(module_t mod, int cmd, void *arg) 1097237263Snp{ 1098237263Snp int rc = 0; 1099237263Snp 1100237263Snp#ifdef TCP_OFFLOAD 1101237263Snp switch (cmd) { 1102237263Snp case MOD_LOAD: 1103237263Snp rc = t4_tom_mod_load(); 1104237263Snp break; 1105237263Snp 1106237263Snp case MOD_UNLOAD: 1107237263Snp rc = t4_tom_mod_unload(); 1108237263Snp break; 1109237263Snp 1110237263Snp default: 1111237263Snp rc = EINVAL; 1112237263Snp } 1113237263Snp#else 1114237263Snp printf("t4_tom: compiled without TCP_OFFLOAD support.\n"); 1115237263Snp rc = EOPNOTSUPP; 1116237263Snp#endif 1117237263Snp return (rc); 1118237263Snp} 1119237263Snp 1120237263Snpstatic moduledata_t t4_tom_moddata= { 1121237263Snp "t4_tom", 1122237263Snp t4_tom_modevent, 1123241394Skevlo 0 1124237263Snp}; 1125237263Snp 1126237263SnpMODULE_VERSION(t4_tom, 1); 1127237263SnpMODULE_DEPEND(t4_tom, toecore, 1, 1, 1); 1128237263SnpMODULE_DEPEND(t4_tom, t4nex, 1, 1, 1); 1129237263SnpDECLARE_MODULE(t4_tom, t4_tom_moddata, SI_SUB_EXEC, SI_ORDER_ANY); 1130