1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2012 Chelsio Communications, Inc. 5 * All rights reserved. 6 * Written by: Navdeep Parhar <np@FreeBSD.org> 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 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. 16 * 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 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include "opt_inet.h" 34 #include "opt_inet6.h" 35 #include "opt_ratelimit.h" 36 37 #include <sys/param.h> 38 #include <sys/types.h> 39 #include <sys/systm.h> 40 #include <sys/kernel.h> 41 #include <sys/ktr.h> 42 #include <sys/lock.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> 52 #include <net/if.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> 62 #define TCPSTATES 63 #include <netinet/tcp_fsm.h> 64 #include <netinet/tcp_timer.h> 65 #include <netinet/tcp_var.h> 66 #include <netinet/toecore.h> 67 68 #ifdef TCP_OFFLOAD 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" 77 78 static struct protosw toe_protosw; 79 static struct pr_usrreqs toe_usrreqs; 80 81 static struct protosw toe6_protosw; 82 static struct pr_usrreqs toe6_usrreqs; 83 84 /* Module ops */ 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 *); 88 89 /* ULD ops and helpers */ 90 static int t4_tom_activate(struct adapter *); 91 static int t4_tom_deactivate(struct adapter *); 92 93 static struct uld_info tom_uld_info = { 94 .uld_id = ULD_TOM, 95 .activate = t4_tom_activate, 96 .deactivate = t4_tom_deactivate, 97 }; 98 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); 112 113 static int in6_ifaddr_gen; 114 static eventhandler_tag ifaddr_evhandler; 115 static struct timeout_task clip_task; 116 117 struct toepcb * 118 alloc_toepcb(struct vi_info *vi, int txqid, int rxqid, int flags) 119 { 120 struct port_info *pi = vi->pi; 121 struct adapter *sc = pi->adapter; 122 struct toepcb *toep; 123 int tx_credits, txsd_total, len; 124 125 /* 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. 129 */ 130 tx_credits = sc->params.ofldq_wr_cred; 131 tx_credits -= howmany(sizeof(struct cpl_abort_req), 16); 132 133 /* 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. 137 */ 138 txsd_total = tx_credits / 139 howmany(sizeof(struct fw_ofld_tx_data_wr) + 1, 16); 140 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)); 145 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)); 150 151 len = offsetof(struct toepcb, txsd) + 152 txsd_total * sizeof(struct ofld_tx_sdesc); 153 154 toep = malloc(len, M_CXGBE, M_ZERO | flags); 155 if (toep == NULL) 156 return (NULL); 157 158 refcount_init(&toep->refcount, 1); 159 toep->td = sc->tom_softc; 160 toep->vi = vi; 161 toep->tc_idx = -1; 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; 171 toep->txsd_pidx = 0; 172 toep->txsd_cidx = 0; 173 aiotx_init_toep(toep); 174 175 return (toep); 176 } 177 178 struct toepcb * 179 hold_toepcb(struct toepcb *toep) 180 { 181 182 refcount_acquire(&toep->refcount); 183 return (toep); 184 } 185 186 void 187 free_toepcb(struct toepcb *toep) 188 { 189 190 if (refcount_release(&toep->refcount) == 0) 191 return; 192 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__)); 197 198 if (toep->ulp_mode == ULP_MODE_TCPDDP) 199 ddp_uninit_toep(toep); 200 tls_uninit_toep(toep); 201 free(toep, M_CXGBE); 202 } 203 204 /* 205 * Set up the socket for TCP offload. 206 */ 207 void 208 offload_socket(struct socket *so, struct toepcb *toep) 209 { 210 struct tom_data *td = toep->td; 211 struct inpcb *inp = sotoinpcb(so); 212 struct tcpcb *tp = intotcpcb(inp); 213 struct sockbuf *sb; 214 215 INP_WLOCK_ASSERT(inp); 216 217 /* Update socket */ 218 sb = &so->so_snd; 219 SOCKBUF_LOCK(sb); 220 sb->sb_flags |= SB_NOCOALESCE; 221 SOCKBUF_UNLOCK(sb); 222 sb = &so->so_rcv; 223 SOCKBUF_LOCK(sb); 224 sb->sb_flags |= SB_NOCOALESCE; 225 if (inp->inp_vflag & INP_IPV6) 226 so->so_proto = &toe6_protosw; 227 else 228 so->so_proto = &toe_protosw; 229 SOCKBUF_UNLOCK(sb); 230 231 /* Update TCP PCB */ 232 tp->tod = &td->tod; 233 tp->t_toe = toep; 234 tp->t_flags |= TF_TOE; 235 236 /* Install an extra hold on inp */ 237 toep->inp = inp; 238 toep->flags |= TPF_ATTACHED; 239 in_pcbref(inp); 240 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); 245 } 246 247 /* This is _not_ the normal way to "unoffload" a socket. */ 248 void 249 undo_offload_socket(struct socket *so) 250 { 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; 255 struct sockbuf *sb; 256 257 INP_WLOCK_ASSERT(inp); 258 259 sb = &so->so_snd; 260 SOCKBUF_LOCK(sb); 261 sb->sb_flags &= ~SB_NOCOALESCE; 262 SOCKBUF_UNLOCK(sb); 263 sb = &so->so_rcv; 264 SOCKBUF_LOCK(sb); 265 sb->sb_flags &= ~SB_NOCOALESCE; 266 SOCKBUF_UNLOCK(sb); 267 268 tp->tod = NULL; 269 tp->t_toe = NULL; 270 tp->t_flags &= ~TF_TOE; 271 272 toep->inp = NULL; 273 toep->flags &= ~TPF_ATTACHED; 274 if (in_pcbrele_wlocked(inp)) 275 panic("%s: inp freed.", __func__); 276 277 mtx_lock(&td->toep_list_lock); 278 TAILQ_REMOVE(&td->toep_list, toep, link); 279 mtx_unlock(&td->toep_list_lock); 280 } 281 282 static void 283 release_offload_resources(struct toepcb *toep) 284 { 285 struct tom_data *td = toep->td; 286 struct adapter *sc = td_adapter(td); 287 int tid = toep->tid; 288 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)); 293 294 CTR5(KTR_CXGBE, "%s: toep %p (tid %d, l2te %p, ce %p)", 295 __func__, toep, tid, toep->l2te, toep->ce); 296 297 /* 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. 301 */ 302 MPASS(mbufq_len(&toep->ulp_pduq) == 0); 303 MPASS(mbufq_len(&toep->ulp_pdu_reclaimq) == 0); 304 #ifdef INVARIANTS 305 if (toep->ulp_mode == ULP_MODE_TCPDDP) 306 ddp_assert_empty(toep); 307 #endif 308 309 if (toep->l2te) 310 t4_l2t_release(toep->l2te); 311 312 if (tid >= 0) { 313 remove_tid(sc, tid, toep->ce ? 2 : 1); 314 release_tid(sc, tid, toep->ctrlq); 315 } 316 317 if (toep->ce) 318 release_lip(td, toep->ce); 319 320 #ifdef RATELIMIT 321 if (toep->tc_idx != -1) 322 t4_release_cl_rl_kbps(sc, toep->vi->pi->port_id, toep->tc_idx); 323 #endif 324 mtx_lock(&td->toep_list_lock); 325 TAILQ_REMOVE(&td->toep_list, toep, link); 326 mtx_unlock(&td->toep_list_lock); 327 328 free_toepcb(toep); 329 } 330 331 /* 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. 335 * 336 * Also gets called when an offloaded active open fails and the TOM wants the 337 * kernel to take the TCP PCB back. 338 */ 339 static void 340 t4_pcb_detach(struct toedev *tod __unused, struct tcpcb *tp) 341 { 342 #if defined(KTR) || defined(INVARIANTS) 343 struct inpcb *inp = tp->t_inpcb; 344 #endif 345 struct toepcb *toep = tp->t_toe; 346 347 INP_WLOCK_ASSERT(inp); 348 349 KASSERT(toep != NULL, ("%s: toep is NULL", __func__)); 350 KASSERT(toep->flags & TPF_ATTACHED, 351 ("%s: not attached", __func__)); 352 353 #ifdef KTR 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, 357 inp->inp_flags); 358 } else { 359 CTR6(KTR_CXGBE, 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, 362 inp->inp_flags); 363 } 364 #endif 365 366 tp->t_toe = NULL; 367 tp->t_flags &= ~TF_TOE; 368 toep->flags &= ~TPF_ATTACHED; 369 370 if (!(toep->flags & TPF_CPL_PENDING)) 371 release_offload_resources(toep); 372 } 373 374 /* 375 * setsockopt handler. 376 */ 377 static void 378 t4_ctloutput(struct toedev *tod, struct tcpcb *tp, int dir, int name) 379 { 380 struct adapter *sc = tod->tod_softc; 381 struct toepcb *toep = tp->t_toe; 382 383 if (dir == SOPT_GET) 384 return; 385 386 CTR4(KTR_CXGBE, "%s: tp %p, dir %u, name %u", __func__, tp, dir, name); 387 388 switch (name) { 389 case TCP_NODELAY: 390 if (tp->t_state != TCPS_ESTABLISHED) 391 break; 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), 394 0, 0); 395 break; 396 default: 397 break; 398 } 399 } 400 401 static inline int 402 get_tcb_bit(u_char *tcb, int bit) 403 { 404 int ix, shift; 405 406 ix = 127 - (bit >> 3); 407 shift = bit & 0x7; 408 409 return ((tcb[ix] >> shift) & 1); 410 } 411 412 static inline uint64_t 413 get_tcb_bits(u_char *tcb, int hi, int lo) 414 { 415 uint64_t rc = 0; 416 417 while (hi >= lo) { 418 rc = (rc << 1) | get_tcb_bit(tcb, hi); 419 --hi; 420 } 421 422 return (rc); 423 } 424 425 /* 426 * Called by the kernel to allow the TOE driver to "refine" values filled up in 427 * the tcp_info for an offloaded connection. 428 */ 429 static void 430 t4_tcp_info(struct toedev *tod, struct tcpcb *tp, struct tcp_info *ti) 431 { 432 int i, j, k, rc; 433 struct adapter *sc = tod->tod_softc; 434 struct toepcb *toep = tp->t_toe; 435 uint32_t addr, v; 436 uint32_t buf[TCB_SIZE / sizeof(uint32_t)]; 437 u_char *tcb, tmp; 438 439 INP_WLOCK_ASSERT(tp->t_inpcb); 440 MPASS(ti != NULL); 441 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); 444 if (rc != 0) 445 return; 446 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++) { 450 tmp = tcb[i + k]; 451 tcb[i + k] = tcb[j + k]; 452 tcb[j + k] = tmp; 453 } 454 } 455 456 ti->tcpi_state = get_tcb_bits(tcb, 115, 112); 457 458 v = get_tcb_bits(tcb, 271, 256); 459 ti->tcpi_rtt = tcp_ticks_to_us(sc, v); 460 461 v = get_tcb_bits(tcb, 287, 272); 462 ti->tcpi_rttvar = tcp_ticks_to_us(sc, v); 463 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); 467 468 ti->tcpi_snd_nxt = get_tcb_bits(tcb, 319, 288) - 469 get_tcb_bits(tcb, 375, 348); 470 471 /* Receive window being advertised by us. */ 472 ti->tcpi_rcv_space = get_tcb_bits(tcb, 581, 554); 473 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); 477 } 478 479 /* 480 * The TOE driver will not receive any more CPLs for the tid associated with the 481 * toepcb; release the hold on the inpcb. 482 */ 483 void 484 final_cpl_received(struct toepcb *toep) 485 { 486 struct inpcb *inp = toep->inp; 487 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__)); 492 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); 495 496 if (toep->ulp_mode == ULP_MODE_TCPDDP) 497 release_ddp_resources(toep); 498 toep->inp = NULL; 499 toep->flags &= ~TPF_CPL_PENDING; 500 mbufq_drain(&toep->ulp_pdu_reclaimq); 501 502 if (!(toep->flags & TPF_ATTACHED)) 503 release_offload_resources(toep); 504 505 if (!in_pcbrele_wlocked(inp)) 506 INP_WUNLOCK(inp); 507 } 508 509 void 510 insert_tid(struct adapter *sc, int tid, void *ctx, int ntids) 511 { 512 struct tid_info *t = &sc->tids; 513 514 t->tid_tab[tid] = ctx; 515 atomic_add_int(&t->tids_in_use, ntids); 516 } 517 518 void * 519 lookup_tid(struct adapter *sc, int tid) 520 { 521 struct tid_info *t = &sc->tids; 522 523 return (t->tid_tab[tid]); 524 } 525 526 void 527 update_tid(struct adapter *sc, int tid, void *ctx) 528 { 529 struct tid_info *t = &sc->tids; 530 531 t->tid_tab[tid] = ctx; 532 } 533 534 void 535 remove_tid(struct adapter *sc, int tid, int ntids) 536 { 537 struct tid_info *t = &sc->tids; 538 539 t->tid_tab[tid] = NULL; 540 atomic_subtract_int(&t->tids_in_use, ntids); 541 } 542 543 /* 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. 549 */ 550 int 551 find_best_mtu_idx(struct adapter *sc, struct in_conninfo *inc, 552 struct offload_settings *s) 553 { 554 unsigned short *mtus = &sc->params.mtus[0]; 555 int i, mss, mtu; 556 557 MPASS(inc != NULL); 558 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); 562 else 563 mtu = mss + sizeof(struct ip) + sizeof(struct tcphdr); 564 565 for (i = 0; i < NMTUS - 1 && mtus[i + 1] <= mtu; i++) 566 continue; 567 568 return (i); 569 } 570 571 /* 572 * Determine the receive window size for a socket. 573 */ 574 u_long 575 select_rcv_wnd(struct socket *so) 576 { 577 unsigned long wnd; 578 579 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 580 581 wnd = sbspace(&so->so_rcv); 582 if (wnd < MIN_RCV_WND) 583 wnd = MIN_RCV_WND; 584 585 return min(wnd, MAX_RCV_WND); 586 } 587 588 int 589 select_rcv_wscale(void) 590 { 591 int wscale = 0; 592 unsigned long space = sb_max; 593 594 if (space > MAX_RCV_WND) 595 space = MAX_RCV_WND; 596 597 while (wscale < TCP_MAX_WINSHIFT && (TCP_MAXWIN << wscale) < space) 598 wscale++; 599 600 return (wscale); 601 } 602 603 /* 604 * socket so could be a listening socket too. 605 */ 606 uint64_t 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) 610 { 611 int keepalive; 612 uint64_t opt0; 613 614 MPASS(so != NULL); 615 MPASS(vi != NULL); 616 KASSERT(rx_credits <= M_RCV_BUFSIZ, 617 ("%s: rcv_bufsiz too high", __func__)); 618 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); 623 624 keepalive = tcp_always_keepalive || so_options_get(so) & SO_KEEPALIVE; 625 opt0 |= V_KEEP_ALIVE(keepalive != 0); 626 627 if (s->nagle < 0) { 628 struct inpcb *inp = sotoinpcb(so); 629 struct tcpcb *tp = intotcpcb(inp); 630 631 opt0 |= V_NAGLE((tp->t_flags & TF_NODELAY) == 0); 632 } else 633 opt0 |= V_NAGLE(s->nagle != 0); 634 635 return htobe64(opt0); 636 } 637 638 uint64_t 639 select_ntuple(struct vi_info *vi, struct l2t_entry *e) 640 { 641 struct adapter *sc = vi->pi->adapter; 642 struct tp_params *tp = &sc->params.tp; 643 uint16_t viid = vi->viid; 644 uint64_t ntuple = 0; 645 646 /* 647 * Initialize each of the fields which we care about which are present 648 * in the Compressed Filter Tuple. 649 */ 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; 652 653 if (tp->port_shift >= 0) 654 ntuple |= (uint64_t)e->lport << tp->port_shift; 655 656 if (tp->protocol_shift >= 0) 657 ntuple |= (uint64_t)IPPROTO_TCP << tp->protocol_shift; 658 659 if (tp->vnic_shift >= 0 && tp->ingress_config & F_VNIC) { 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); 663 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; 666 } 667 668 if (is_t4(sc)) 669 return (htobe32((uint32_t)ntuple)); 670 else 671 return (htobe64(V_FILTER_TUPLE(ntuple))); 672 } 673 674 static int 675 is_tls_sock(struct socket *so, struct adapter *sc) 676 { 677 struct inpcb *inp = sotoinpcb(so); 678 int i, rc; 679 680 /* XXX: Eventually add a SO_WANT_TLS socket option perhaps? */ 681 rc = 0; 682 ADAPTER_LOCK(sc); 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])) { 686 rc = 1; 687 break; 688 } 689 } 690 ADAPTER_UNLOCK(sc); 691 return (rc); 692 } 693 694 int 695 select_ulp_mode(struct socket *so, struct adapter *sc, 696 struct offload_settings *s) 697 { 698 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); 705 else 706 return (ULP_MODE_NONE); 707 } 708 709 void 710 set_ulp_mode(struct toepcb *toep, int ulp_mode) 711 { 712 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; 716 tls_init_toep(toep); 717 if (toep->ulp_mode == ULP_MODE_TCPDDP) 718 ddp_init_toep(toep); 719 } 720 721 int 722 negative_advice(int status) 723 { 724 725 return (status == CPL_ERR_RTX_NEG_ADVICE || 726 status == CPL_ERR_PERSIST_NEG_ADVICE || 727 status == CPL_ERR_KEEPALV_NEG_ADVICE); 728 } 729 730 static int 731 alloc_tid_tab(struct tid_info *t, int flags) 732 { 733 734 MPASS(t->ntids > 0); 735 MPASS(t->tid_tab == NULL); 736 737 t->tid_tab = malloc(t->ntids * sizeof(*t->tid_tab), M_CXGBE, 738 M_ZERO | flags); 739 if (t->tid_tab == NULL) 740 return (ENOMEM); 741 atomic_store_rel_int(&t->tids_in_use, 0); 742 743 return (0); 744 } 745 746 static void 747 free_tid_tab(struct tid_info *t) 748 { 749 750 KASSERT(t->tids_in_use == 0, 751 ("%s: %d tids still in use.", __func__, t->tids_in_use)); 752 753 free(t->tid_tab, M_CXGBE); 754 t->tid_tab = NULL; 755 } 756 757 static int 758 alloc_stid_tab(struct tid_info *t, int flags) 759 { 760 761 MPASS(t->nstids > 0); 762 MPASS(t->stid_tab == NULL); 763 764 t->stid_tab = malloc(t->nstids * sizeof(*t->stid_tab), M_CXGBE, 765 M_ZERO | flags); 766 if (t->stid_tab == NULL) 767 return (ENOMEM); 768 mtx_init(&t->stid_lock, "stid lock", NULL, MTX_DEF); 769 t->stids_in_use = 0; 770 TAILQ_INIT(&t->stids); 771 t->nstids_free_head = t->nstids; 772 773 return (0); 774 } 775 776 static void 777 free_stid_tab(struct tid_info *t) 778 { 779 780 KASSERT(t->stids_in_use == 0, 781 ("%s: %d tids still in use.", __func__, t->stids_in_use)); 782 783 if (mtx_initialized(&t->stid_lock)) 784 mtx_destroy(&t->stid_lock); 785 free(t->stid_tab, M_CXGBE); 786 t->stid_tab = NULL; 787 } 788 789 static void 790 free_tid_tabs(struct tid_info *t) 791 { 792 793 free_tid_tab(t); 794 free_atid_tab(t); 795 free_stid_tab(t); 796 } 797 798 static int 799 alloc_tid_tabs(struct tid_info *t) 800 { 801 int rc; 802 803 rc = alloc_tid_tab(t, M_NOWAIT); 804 if (rc != 0) 805 goto failed; 806 807 rc = alloc_atid_tab(t, M_NOWAIT); 808 if (rc != 0) 809 goto failed; 810 811 rc = alloc_stid_tab(t, M_NOWAIT); 812 if (rc != 0) 813 goto failed; 814 815 return (0); 816 failed: 817 free_tid_tabs(t); 818 return (rc); 819 } 820 821 static int 822 add_lip(struct adapter *sc, struct in6_addr *lip) 823 { 824 struct fw_clip_cmd c; 825 826 ASSERT_SYNCHRONIZED_OP(sc); 827 /* mtx_assert(&td->clip_table_lock, MA_OWNED); */ 828 829 memset(&c, 0, sizeof(c)); 830 c.op_to_write = htonl(V_FW_CMD_OP(FW_CLIP_CMD) | F_FW_CMD_REQUEST | 831 F_FW_CMD_WRITE); 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]; 835 836 return (-t4_wr_mbox_ns(sc, sc->mbox, &c, sizeof(c), &c)); 837 } 838 839 static int 840 delete_lip(struct adapter *sc, struct in6_addr *lip) 841 { 842 struct fw_clip_cmd c; 843 844 ASSERT_SYNCHRONIZED_OP(sc); 845 /* mtx_assert(&td->clip_table_lock, MA_OWNED); */ 846 847 memset(&c, 0, sizeof(c)); 848 c.op_to_write = htonl(V_FW_CMD_OP(FW_CLIP_CMD) | F_FW_CMD_REQUEST | 849 F_FW_CMD_READ); 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]; 853 854 return (-t4_wr_mbox_ns(sc, sc->mbox, &c, sizeof(c), &c)); 855 } 856 857 static struct clip_entry * 858 search_lip(struct tom_data *td, struct in6_addr *lip) 859 { 860 struct clip_entry *ce; 861 862 mtx_assert(&td->clip_table_lock, MA_OWNED); 863 864 TAILQ_FOREACH(ce, &td->clip_table, link) { 865 if (IN6_ARE_ADDR_EQUAL(&ce->lip, lip)) 866 return (ce); 867 } 868 869 return (NULL); 870 } 871 872 struct clip_entry * 873 hold_lip(struct tom_data *td, struct in6_addr *lip, struct clip_entry *ce) 874 { 875 876 mtx_lock(&td->clip_table_lock); 877 if (ce == NULL) 878 ce = search_lip(td, lip); 879 if (ce != NULL) 880 ce->refcount++; 881 mtx_unlock(&td->clip_table_lock); 882 883 return (ce); 884 } 885 886 void 887 release_lip(struct tom_data *td, struct clip_entry *ce) 888 { 889 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)); 895 --ce->refcount; 896 mtx_unlock(&td->clip_table_lock); 897 } 898 899 static void 900 init_clip_table(struct adapter *sc, struct tom_data *td) 901 { 902 903 ASSERT_SYNCHRONIZED_OP(sc); 904 905 mtx_init(&td->clip_table_lock, "CLIP table lock", NULL, MTX_DEF); 906 TAILQ_INIT(&td->clip_table); 907 td->clip_gen = -1; 908 909 update_clip_table(sc, td); 910 } 911 912 static void 913 update_clip(struct adapter *sc, void *arg __unused) 914 { 915 916 if (begin_synchronized_op(sc, NULL, HOLD_LOCK, "t4tomuc")) 917 return; 918 919 if (uld_active(sc, ULD_TOM)) 920 update_clip_table(sc, sc->tom_softc); 921 922 end_synchronized_op(sc, LOCK_HELD); 923 } 924 925 static void 926 t4_clip_task(void *arg, int count) 927 { 928 929 t4_iterate(update_clip, NULL); 930 } 931 932 static void 933 update_clip_table(struct adapter *sc, struct tom_data *td) 934 { 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; 940 struct vi_info *vi; 941 int rc, gen, i, j; 942 uintptr_t last_vnet; 943 944 ASSERT_SYNCHRONIZED_OP(sc); 945 946 IN6_IFADDR_RLOCK(&in6_ifa_tracker); 947 mtx_lock(&td->clip_table_lock); 948 949 gen = atomic_load_acq_int(&in6_ifaddr_gen); 950 if (gen == td->clip_gen) 951 goto done; 952 953 TAILQ_INIT(&stale); 954 TAILQ_CONCAT(&stale, &td->clip_table, link); 955 956 /* 957 * last_vnet optimizes the common cases where all if_vnet = NULL (no 958 * VIMAGE) or all if_vnet = vnet0. 959 */ 960 last_vnet = (uintptr_t)(-1); 961 for_each_port(sc, i) 962 for_each_vi(sc->port[i], j, vi) { 963 if (last_vnet == (uintptr_t)vi->ifp->if_vnet) 964 continue; 965 966 /* XXX: races with if_vmove */ 967 CURVNET_SET(vi->ifp->if_vnet); 968 CK_STAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) { 969 lip = &ia->ia_addr.sin6_addr; 970 971 KASSERT(!IN6_IS_ADDR_MULTICAST(lip), 972 ("%s: mcast address in in6_ifaddr list", __func__)); 973 974 if (IN6_IS_ADDR_LOOPBACK(lip)) 975 continue; 976 if (IN6_IS_SCOPE_EMBED(lip)) { 977 /* Remove the embedded scope */ 978 tlip = *lip; 979 lip = &tlip; 980 in6_clearscope(lip); 981 } 982 /* 983 * XXX: how to weed out the link local address for the 984 * loopback interface? It's fe80::1 usually (always?). 985 */ 986 987 /* 988 * If it's in the main list then we already know it's 989 * not stale. 990 */ 991 TAILQ_FOREACH(ce, &td->clip_table, link) { 992 if (IN6_ARE_ADDR_EQUAL(&ce->lip, lip)) 993 goto next; 994 } 995 996 /* 997 * If it's in the stale list we should move it to the 998 * main list. 999 */ 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, 1004 link); 1005 goto next; 1006 } 1007 } 1008 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)); 1012 ce->refcount = 0; 1013 rc = add_lip(sc, lip); 1014 if (rc == 0) 1015 TAILQ_INSERT_TAIL(&td->clip_table, ce, link); 1016 else { 1017 char ip[INET6_ADDRSTRLEN]; 1018 1019 inet_ntop(AF_INET6, &ce->lip, &ip[0], 1020 sizeof(ip)); 1021 log(LOG_ERR, "%s: could not add %s (%d)\n", 1022 __func__, ip, rc); 1023 free(ce, M_CXGBE); 1024 } 1025 next: 1026 continue; 1027 } 1028 CURVNET_RESTORE(); 1029 last_vnet = (uintptr_t)vi->ifp->if_vnet; 1030 } 1031 1032 /* 1033 * Remove stale addresses (those no longer in V_in6_ifaddrhead) that are 1034 * no longer referenced by the driver. 1035 */ 1036 TAILQ_FOREACH_SAFE(ce, &stale, link, ce_temp) { 1037 if (ce->refcount == 0) { 1038 rc = delete_lip(sc, &ce->lip); 1039 if (rc == 0) { 1040 TAILQ_REMOVE(&stale, ce, link); 1041 free(ce, M_CXGBE); 1042 } else { 1043 char ip[INET6_ADDRSTRLEN]; 1044 1045 inet_ntop(AF_INET6, &ce->lip, &ip[0], 1046 sizeof(ip)); 1047 log(LOG_ERR, "%s: could not delete %s (%d)\n", 1048 __func__, ip, rc); 1049 } 1050 } 1051 } 1052 /* The ones that are still referenced need to stay in the CLIP table */ 1053 TAILQ_CONCAT(&td->clip_table, &stale, link); 1054 1055 td->clip_gen = gen; 1056 done: 1057 mtx_unlock(&td->clip_table_lock); 1058 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker); 1059 } 1060 1061 static void 1062 destroy_clip_table(struct adapter *sc, struct tom_data *td) 1063 { 1064 struct clip_entry *ce, *ce_temp; 1065 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__, 1071 ce, ce->refcount)); 1072 TAILQ_REMOVE(&td->clip_table, ce, link); 1073 delete_lip(sc, &ce->lip); 1074 free(ce, M_CXGBE); 1075 } 1076 mtx_unlock(&td->clip_table_lock); 1077 mtx_destroy(&td->clip_table_lock); 1078 } 1079 } 1080 1081 static void 1082 free_tom_data(struct adapter *sc, struct tom_data *td) 1083 { 1084 1085 ASSERT_SYNCHRONIZED_OP(sc); 1086 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__)); 1091 1092 tls_free_kmap(td); 1093 t4_free_ppod_region(&td->pr); 1094 destroy_clip_table(sc, td); 1095 1096 if (td->listen_mask != 0) 1097 hashdestroy(td->listen_hash, M_CXGBE, td->listen_mask); 1098 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); 1105 1106 free_tid_tabs(&sc->tids); 1107 free(td, M_CXGBE); 1108 } 1109 1110 static char * 1111 prepare_pkt(int open_type, uint16_t vtag, struct inpcb *inp, int *pktlen, 1112 int *buflen) 1113 { 1114 char *pkt; 1115 struct tcphdr *th; 1116 int ipv6, len; 1117 const int maxlen = 1118 max(sizeof(struct ether_header), sizeof(struct ether_vlan_header)) + 1119 max(sizeof(struct ip), sizeof(struct ip6_hdr)) + 1120 sizeof(struct tcphdr); 1121 1122 MPASS(open_type == OPEN_TYPE_ACTIVE || open_type == OPEN_TYPE_LISTEN); 1123 1124 pkt = malloc(maxlen, M_CXGBE, M_ZERO | M_NOWAIT); 1125 if (pkt == NULL) 1126 return (NULL); 1127 1128 ipv6 = inp->inp_vflag & INP_IPV6; 1129 len = 0; 1130 1131 if (vtag == 0xffff) { 1132 struct ether_header *eh = (void *)pkt; 1133 1134 if (ipv6) 1135 eh->ether_type = htons(ETHERTYPE_IPV6); 1136 else 1137 eh->ether_type = htons(ETHERTYPE_IP); 1138 1139 len += sizeof(*eh); 1140 } else { 1141 struct ether_vlan_header *evh = (void *)pkt; 1142 1143 evh->evl_encap_proto = htons(ETHERTYPE_VLAN); 1144 evh->evl_tag = htons(vtag); 1145 if (ipv6) 1146 evh->evl_proto = htons(ETHERTYPE_IPV6); 1147 else 1148 evh->evl_proto = htons(ETHERTYPE_IP); 1149 1150 len += sizeof(*evh); 1151 } 1152 1153 if (ipv6) { 1154 struct ip6_hdr *ip6 = (void *)&pkt[len]; 1155 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; 1165 } 1166 1167 len += sizeof(*ip6); 1168 } else { 1169 struct ip *ip = (void *)&pkt[len]; 1170 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; 1183 } 1184 1185 len += sizeof(*ip); 1186 } 1187 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; 1195 } 1196 len += sizeof(th); 1197 1198 *pktlen = *buflen = len; 1199 return (pkt); 1200 } 1201 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) 1205 { 1206 const struct t4_offload_policy *op; 1207 char *pkt; 1208 struct offload_rule *r; 1209 int i, matched, pktlen, buflen; 1210 static const struct offload_settings allow_offloading_settings = { 1211 .offload = 1, 1212 .rx_coalesce = -1, 1213 .cong_algo = -1, 1214 .sched_class = -1, 1215 .tstamp = -1, 1216 .sack = -1, 1217 .nagle = -1, 1218 .ecn = -1, 1219 .ddp = -1, 1220 .tls = -1, 1221 .txq = -1, 1222 .rxq = -1, 1223 .mss = -1, 1224 }; 1225 static const struct offload_settings disallow_offloading_settings = { 1226 .offload = 0, 1227 /* rest is irrelevant when offload is off. */ 1228 }; 1229 1230 rw_assert(&sc->policy_lock, RA_LOCKED); 1231 1232 /* 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 ... 1238 */ 1239 op = sc->policy; 1240 if (op == NULL) { 1241 if (sc->tt.cop_managed_offloading) 1242 return (&disallow_offloading_settings); 1243 else 1244 return (&allow_offloading_settings); 1245 } 1246 1247 switch (open_type) { 1248 case OPEN_TYPE_ACTIVE: 1249 case OPEN_TYPE_LISTEN: 1250 pkt = prepare_pkt(open_type, vtag, inp, &pktlen, &buflen); 1251 break; 1252 case OPEN_TYPE_PASSIVE: 1253 MPASS(m != NULL); 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); 1259 break; 1260 default: 1261 MPASS(0); 1262 return (&disallow_offloading_settings); 1263 } 1264 1265 if (pkt == NULL || pktlen == 0 || buflen == 0) 1266 return (&disallow_offloading_settings); 1267 1268 r = &op->rule[0]; 1269 for (i = 0; i < op->nrules; i++, r++) { 1270 if (r->open_type != open_type && 1271 r->open_type != OPEN_TYPE_DONTCARE) { 1272 continue; 1273 } 1274 matched = bpf_filter(r->bpf_prog.bf_insns, pkt, pktlen, buflen); 1275 if (matched) 1276 break; 1277 } 1278 1279 if (open_type == OPEN_TYPE_ACTIVE || open_type == OPEN_TYPE_LISTEN) 1280 free(pkt, M_CXGBE); 1281 1282 return (matched ? &r->settings : &disallow_offloading_settings); 1283 } 1284 1285 static void 1286 reclaim_wr_resources(void *arg, int count) 1287 { 1288 struct tom_data *td = arg; 1289 STAILQ_HEAD(, wrqe) twr_list = STAILQ_HEAD_INITIALIZER(twr_list); 1290 struct cpl_act_open_req *cpl; 1291 u_int opcode, atid; 1292 struct wrqe *wr; 1293 struct adapter *sc; 1294 1295 mtx_lock(&td->unsent_wr_lock); 1296 STAILQ_SWAP(&td->unsent_wr_list, &twr_list, wrqe); 1297 mtx_unlock(&td->unsent_wr_lock); 1298 1299 while ((wr = STAILQ_FIRST(&twr_list)) != NULL) { 1300 STAILQ_REMOVE_HEAD(&twr_list, link); 1301 1302 cpl = wrtod(wr); 1303 opcode = GET_OPCODE(cpl); 1304 1305 switch (opcode) { 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); 1310 1311 CTR2(KTR_CXGBE, "%s: atid %u ", __func__, atid); 1312 act_open_failure_cleanup(sc, atid, EHOSTUNREACH); 1313 free(wr, M_CXGBE); 1314 break; 1315 default: 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. */ 1319 } 1320 } 1321 } 1322 1323 /* 1324 * Ground control to Major TOM 1325 * Commencing countdown, engines on 1326 */ 1327 static int 1328 t4_tom_activate(struct adapter *sc) 1329 { 1330 struct tom_data *td; 1331 struct toedev *tod; 1332 struct vi_info *vi; 1333 int i, rc, v; 1334 1335 ASSERT_SYNCHRONIZED_OP(sc); 1336 1337 /* per-adapter softc for TOM */ 1338 td = malloc(sizeof(*td), M_CXGBE, M_ZERO | M_NOWAIT); 1339 if (td == NULL) 1340 return (ENOMEM); 1341 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); 1345 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); 1350 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); 1355 1356 /* TID tables */ 1357 rc = alloc_tid_tabs(&sc->tids); 1358 if (rc != 0) 1359 goto done; 1360 1361 rc = t4_init_ppod_region(&td->pr, &sc->vres.ddp, 1362 t4_read_reg(sc, A_ULP_RX_TDDP_PSZ), "TDDP page pods"); 1363 if (rc != 0) 1364 goto done; 1365 t4_set_reg_field(sc, A_ULP_RX_TDDP_TAGMASK, 1366 V_TDDPTAGMASK(M_TDDPTAGMASK), td->pr.pr_tag_mask); 1367 1368 /* CLIP table for IPv6 offload */ 1369 init_clip_table(sc, td); 1370 1371 if (sc->vres.key.size != 0) { 1372 rc = tls_init_kmap(sc, td); 1373 if (rc != 0) 1374 goto done; 1375 } 1376 1377 /* toedev ops */ 1378 tod = &td->tod; 1379 init_toedev(tod); 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; 1396 1397 for_each_port(sc, i) { 1398 for_each_vi(sc->port[i], v, vi) { 1399 TOEDEV(vi->ifp) = &td->tod; 1400 } 1401 } 1402 1403 sc->tom_softc = td; 1404 register_toedev(sc->tom_softc); 1405 1406 done: 1407 if (rc != 0) 1408 free_tom_data(sc, td); 1409 return (rc); 1410 } 1411 1412 static int 1413 t4_tom_deactivate(struct adapter *sc) 1414 { 1415 int rc = 0; 1416 struct tom_data *td = sc->tom_softc; 1417 1418 ASSERT_SYNCHRONIZED_OP(sc); 1419 1420 if (td == NULL) 1421 return (0); /* XXX. KASSERT? */ 1422 1423 if (sc->offload_map != 0) 1424 return (EBUSY); /* at least one port has IFCAP_TOE enabled */ 1425 1426 if (uld_active(sc, ULD_IWARP) || uld_active(sc, ULD_ISCSI)) 1427 return (EBUSY); /* both iWARP and iSCSI rely on the TOE. */ 1428 1429 mtx_lock(&td->toep_list_lock); 1430 if (!TAILQ_EMPTY(&td->toep_list)) 1431 rc = EBUSY; 1432 mtx_unlock(&td->toep_list_lock); 1433 1434 mtx_lock(&td->lctx_hash_lock); 1435 if (td->lctx_count > 0) 1436 rc = EBUSY; 1437 mtx_unlock(&td->lctx_hash_lock); 1438 1439 taskqueue_drain(taskqueue_thread, &td->reclaim_wr_resources); 1440 mtx_lock(&td->unsent_wr_lock); 1441 if (!STAILQ_EMPTY(&td->unsent_wr_list)) 1442 rc = EBUSY; 1443 mtx_unlock(&td->unsent_wr_lock); 1444 1445 if (rc == 0) { 1446 unregister_toedev(sc->tom_softc); 1447 free_tom_data(sc, td); 1448 sc->tom_softc = NULL; 1449 } 1450 1451 return (rc); 1452 } 1453 1454 static void 1455 t4_tom_ifaddr_event(void *arg __unused, struct ifnet *ifp) 1456 { 1457 1458 atomic_add_rel_int(&in6_ifaddr_gen, 1); 1459 taskqueue_enqueue_timeout(taskqueue_thread, &clip_task, -hz / 4); 1460 } 1461 1462 static int 1463 t4_aio_queue_tom(struct socket *so, struct kaiocb *job) 1464 { 1465 struct tcpcb *tp = so_sototcpcb(so); 1466 struct toepcb *toep = tp->t_toe; 1467 int error; 1468 1469 if (toep->ulp_mode == ULP_MODE_TCPDDP) { 1470 error = t4_aio_queue_ddp(so, job); 1471 if (error != EOPNOTSUPP) 1472 return (error); 1473 } 1474 1475 return (t4_aio_queue_aiotx(so, job)); 1476 } 1477 1478 static int 1479 t4_ctloutput_tom(struct socket *so, struct sockopt *sopt) 1480 { 1481 1482 if (sopt->sopt_level != IPPROTO_TCP) 1483 return (tcp_ctloutput(so, sopt)); 1484 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)); 1491 default: 1492 return (tcp_ctloutput(so, sopt)); 1493 } 1494 } 1495 1496 static int 1497 t4_tom_mod_load(void) 1498 { 1499 struct protosw *tcp_protosw, *tcp6_protosw; 1500 1501 /* CPL handlers */ 1502 t4_register_shared_cpl_handler(CPL_L2T_WRITE_RPL, do_l2t_write_rpl2, 1503 CPL_COOKIE_TOM); 1504 t4_init_connect_cpl_handlers(); 1505 t4_init_listen_cpl_handlers(); 1506 t4_init_cpl_io_handlers(); 1507 1508 t4_ddp_mod_load(); 1509 t4_tls_mod_load(); 1510 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; 1519 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; 1528 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); 1532 1533 return (t4_register_uld(&tom_uld_info)); 1534 } 1535 1536 static void 1537 tom_uninit(struct adapter *sc, void *arg __unused) 1538 { 1539 if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4tomun")) 1540 return; 1541 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); 1545 1546 end_synchronized_op(sc, 0); 1547 } 1548 1549 static int 1550 t4_tom_mod_unload(void) 1551 { 1552 t4_iterate(tom_uninit, NULL); 1553 1554 if (t4_unregister_uld(&tom_uld_info) == EBUSY) 1555 return (EBUSY); 1556 1557 if (ifaddr_evhandler) { 1558 EVENTHANDLER_DEREGISTER(ifaddr_event, ifaddr_evhandler); 1559 taskqueue_cancel_timeout(taskqueue_thread, &clip_task, NULL); 1560 } 1561 1562 t4_tls_mod_unload(); 1563 t4_ddp_mod_unload(); 1564 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); 1569 1570 return (0); 1571 } 1572 #endif /* TCP_OFFLOAD */ 1573 1574 static int 1575 t4_tom_modevent(module_t mod, int cmd, void *arg) 1576 { 1577 int rc = 0; 1578 1579 #ifdef TCP_OFFLOAD 1580 switch (cmd) { 1581 case MOD_LOAD: 1582 rc = t4_tom_mod_load(); 1583 break; 1584 1585 case MOD_UNLOAD: 1586 rc = t4_tom_mod_unload(); 1587 break; 1588 1589 default: 1590 rc = EINVAL; 1591 } 1592 #else 1593 printf("t4_tom: compiled without TCP_OFFLOAD support.\n"); 1594 rc = EOPNOTSUPP; 1595 #endif 1596 return (rc); 1597 } 1598 1599 static moduledata_t t4_tom_moddata= { 1600 "t4_tom", 1601 t4_tom_modevent, 1602 0 1603 }; 1604 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); 1609