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 if (toep->tc_idx != -1) 321 t4_release_cl_rl(sc, toep->vi->pi->port_id, toep->tc_idx); 322 323 mtx_lock(&td->toep_list_lock); 324 TAILQ_REMOVE(&td->toep_list, toep, link); 325 mtx_unlock(&td->toep_list_lock); 326 327 free_toepcb(toep); 328 } 329 330 /* 331 * The kernel is done with the TCP PCB and this is our opportunity to unhook the 332 * toepcb hanging off of it. If the TOE driver is also done with the toepcb (no 333 * pending CPL) then it is time to release all resources tied to the toepcb. 334 * 335 * Also gets called when an offloaded active open fails and the TOM wants the 336 * kernel to take the TCP PCB back. 337 */ 338 static void 339 t4_pcb_detach(struct toedev *tod __unused, struct tcpcb *tp) 340 { 341 #if defined(KTR) || defined(INVARIANTS) 342 struct inpcb *inp = tp->t_inpcb; 343 #endif 344 struct toepcb *toep = tp->t_toe; 345 346 INP_WLOCK_ASSERT(inp); 347 348 KASSERT(toep != NULL, ("%s: toep is NULL", __func__)); 349 KASSERT(toep->flags & TPF_ATTACHED, 350 ("%s: not attached", __func__)); 351 352 #ifdef KTR 353 if (tp->t_state == TCPS_SYN_SENT) { 354 CTR6(KTR_CXGBE, "%s: atid %d, toep %p (0x%x), inp %p (0x%x)", 355 __func__, toep->tid, toep, toep->flags, inp, 356 inp->inp_flags); 357 } else { 358 CTR6(KTR_CXGBE, 359 "t4_pcb_detach: tid %d (%s), toep %p (0x%x), inp %p (0x%x)", 360 toep->tid, tcpstates[tp->t_state], toep, toep->flags, inp, 361 inp->inp_flags); 362 } 363 #endif 364 365 tp->t_toe = NULL; 366 tp->t_flags &= ~TF_TOE; 367 toep->flags &= ~TPF_ATTACHED; 368 369 if (!(toep->flags & TPF_CPL_PENDING)) 370 release_offload_resources(toep); 371 } 372 373 /* 374 * setsockopt handler. 375 */ 376 static void 377 t4_ctloutput(struct toedev *tod, struct tcpcb *tp, int dir, int name) 378 { 379 struct adapter *sc = tod->tod_softc; 380 struct toepcb *toep = tp->t_toe; 381 382 if (dir == SOPT_GET) 383 return; 384 385 CTR4(KTR_CXGBE, "%s: tp %p, dir %u, name %u", __func__, tp, dir, name); 386 387 switch (name) { 388 case TCP_NODELAY: 389 if (tp->t_state != TCPS_ESTABLISHED) 390 break; 391 t4_set_tcb_field(sc, toep->ctrlq, toep, W_TCB_T_FLAGS, 392 V_TF_NAGLE(1), V_TF_NAGLE(tp->t_flags & TF_NODELAY ? 0 : 1), 393 0, 0); 394 break; 395 default: 396 break; 397 } 398 } 399 400 static inline int 401 get_tcb_bit(u_char *tcb, int bit) 402 { 403 int ix, shift; 404 405 ix = 127 - (bit >> 3); 406 shift = bit & 0x7; 407 408 return ((tcb[ix] >> shift) & 1); 409 } 410 411 static inline uint64_t 412 get_tcb_bits(u_char *tcb, int hi, int lo) 413 { 414 uint64_t rc = 0; 415 416 while (hi >= lo) { 417 rc = (rc << 1) | get_tcb_bit(tcb, hi); 418 --hi; 419 } 420 421 return (rc); 422 } 423 424 /* 425 * Called by the kernel to allow the TOE driver to "refine" values filled up in 426 * the tcp_info for an offloaded connection. 427 */ 428 static void 429 t4_tcp_info(struct toedev *tod, struct tcpcb *tp, struct tcp_info *ti) 430 { 431 int i, j, k, rc; 432 struct adapter *sc = tod->tod_softc; 433 struct toepcb *toep = tp->t_toe; 434 uint32_t addr, v; 435 uint32_t buf[TCB_SIZE / sizeof(uint32_t)]; 436 u_char *tcb, tmp; 437 438 INP_WLOCK_ASSERT(tp->t_inpcb); 439 MPASS(ti != NULL); 440 441 addr = t4_read_reg(sc, A_TP_CMM_TCB_BASE) + toep->tid * TCB_SIZE; 442 rc = read_via_memwin(sc, 2, addr, &buf[0], TCB_SIZE); 443 if (rc != 0) 444 return; 445 446 tcb = (u_char *)&buf[0]; 447 for (i = 0, j = TCB_SIZE - 16; i < j; i += 16, j -= 16) { 448 for (k = 0; k < 16; k++) { 449 tmp = tcb[i + k]; 450 tcb[i + k] = tcb[j + k]; 451 tcb[j + k] = tmp; 452 } 453 } 454 455 ti->tcpi_state = get_tcb_bits(tcb, 115, 112); 456 457 v = get_tcb_bits(tcb, 271, 256); 458 ti->tcpi_rtt = tcp_ticks_to_us(sc, v); 459 460 v = get_tcb_bits(tcb, 287, 272); 461 ti->tcpi_rttvar = tcp_ticks_to_us(sc, v); 462 463 ti->tcpi_snd_ssthresh = get_tcb_bits(tcb, 487, 460); 464 ti->tcpi_snd_cwnd = get_tcb_bits(tcb, 459, 432); 465 ti->tcpi_rcv_nxt = get_tcb_bits(tcb, 553, 522); 466 467 ti->tcpi_snd_nxt = get_tcb_bits(tcb, 319, 288) - 468 get_tcb_bits(tcb, 375, 348); 469 470 /* Receive window being advertised by us. */ 471 ti->tcpi_rcv_space = get_tcb_bits(tcb, 581, 554); 472 473 /* Send window ceiling. */ 474 v = get_tcb_bits(tcb, 159, 144) << get_tcb_bits(tcb, 131, 128); 475 ti->tcpi_snd_wnd = min(v, ti->tcpi_snd_cwnd); 476 } 477 478 /* 479 * The TOE driver will not receive any more CPLs for the tid associated with the 480 * toepcb; release the hold on the inpcb. 481 */ 482 void 483 final_cpl_received(struct toepcb *toep) 484 { 485 struct inpcb *inp = toep->inp; 486 487 KASSERT(inp != NULL, ("%s: inp is NULL", __func__)); 488 INP_WLOCK_ASSERT(inp); 489 KASSERT(toep->flags & TPF_CPL_PENDING, 490 ("%s: CPL not pending already?", __func__)); 491 492 CTR6(KTR_CXGBE, "%s: tid %d, toep %p (0x%x), inp %p (0x%x)", 493 __func__, toep->tid, toep, toep->flags, inp, inp->inp_flags); 494 495 if (toep->ulp_mode == ULP_MODE_TCPDDP) 496 release_ddp_resources(toep); 497 toep->inp = NULL; 498 toep->flags &= ~TPF_CPL_PENDING; 499 mbufq_drain(&toep->ulp_pdu_reclaimq); 500 501 if (!(toep->flags & TPF_ATTACHED)) 502 release_offload_resources(toep); 503 504 if (!in_pcbrele_wlocked(inp)) 505 INP_WUNLOCK(inp); 506 } 507 508 void 509 insert_tid(struct adapter *sc, int tid, void *ctx, int ntids) 510 { 511 struct tid_info *t = &sc->tids; 512 513 MPASS(tid >= t->tid_base); 514 MPASS(tid - t->tid_base < t->ntids); 515 516 t->tid_tab[tid - t->tid_base] = ctx; 517 atomic_add_int(&t->tids_in_use, ntids); 518 } 519 520 void * 521 lookup_tid(struct adapter *sc, int tid) 522 { 523 struct tid_info *t = &sc->tids; 524 525 return (t->tid_tab[tid - t->tid_base]); 526 } 527 528 void 529 update_tid(struct adapter *sc, int tid, void *ctx) 530 { 531 struct tid_info *t = &sc->tids; 532 533 t->tid_tab[tid - t->tid_base] = ctx; 534 } 535 536 void 537 remove_tid(struct adapter *sc, int tid, int ntids) 538 { 539 struct tid_info *t = &sc->tids; 540 541 t->tid_tab[tid - t->tid_base] = NULL; 542 atomic_subtract_int(&t->tids_in_use, ntids); 543 } 544 545 /* 546 * What mtu_idx to use, given a 4-tuple. Note that both s->mss and tcp_mssopt 547 * have the MSS that we should advertise in our SYN. Advertised MSS doesn't 548 * account for any TCP options so the effective MSS (only payload, no headers or 549 * options) could be different. We fill up tp->t_maxseg with the effective MSS 550 * at the end of the 3-way handshake. 551 */ 552 int 553 find_best_mtu_idx(struct adapter *sc, struct in_conninfo *inc, 554 struct offload_settings *s) 555 { 556 unsigned short *mtus = &sc->params.mtus[0]; 557 int i, mss, mtu; 558 559 MPASS(inc != NULL); 560 561 mss = s->mss > 0 ? s->mss : tcp_mssopt(inc); 562 if (inc->inc_flags & INC_ISIPV6) 563 mtu = mss + sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 564 else 565 mtu = mss + sizeof(struct ip) + sizeof(struct tcphdr); 566 567 for (i = 0; i < NMTUS - 1 && mtus[i + 1] <= mtu; i++) 568 continue; 569 570 return (i); 571 } 572 573 /* 574 * Determine the receive window size for a socket. 575 */ 576 u_long 577 select_rcv_wnd(struct socket *so) 578 { 579 unsigned long wnd; 580 581 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 582 583 wnd = sbspace(&so->so_rcv); 584 if (wnd < MIN_RCV_WND) 585 wnd = MIN_RCV_WND; 586 587 return min(wnd, MAX_RCV_WND); 588 } 589 590 int 591 select_rcv_wscale(void) 592 { 593 int wscale = 0; 594 unsigned long space = sb_max; 595 596 if (space > MAX_RCV_WND) 597 space = MAX_RCV_WND; 598 599 while (wscale < TCP_MAX_WINSHIFT && (TCP_MAXWIN << wscale) < space) 600 wscale++; 601 602 return (wscale); 603 } 604 605 /* 606 * socket so could be a listening socket too. 607 */ 608 uint64_t 609 calc_opt0(struct socket *so, struct vi_info *vi, struct l2t_entry *e, 610 int mtu_idx, int rscale, int rx_credits, int ulp_mode, 611 struct offload_settings *s) 612 { 613 int keepalive; 614 uint64_t opt0; 615 616 MPASS(so != NULL); 617 MPASS(vi != NULL); 618 KASSERT(rx_credits <= M_RCV_BUFSIZ, 619 ("%s: rcv_bufsiz too high", __func__)); 620 621 opt0 = F_TCAM_BYPASS | V_WND_SCALE(rscale) | V_MSS_IDX(mtu_idx) | 622 V_ULP_MODE(ulp_mode) | V_RCV_BUFSIZ(rx_credits) | 623 V_L2T_IDX(e->idx) | V_SMAC_SEL(vi->smt_idx) | 624 V_TX_CHAN(vi->pi->tx_chan); 625 626 keepalive = tcp_always_keepalive || so_options_get(so) & SO_KEEPALIVE; 627 opt0 |= V_KEEP_ALIVE(keepalive != 0); 628 629 if (s->nagle < 0) { 630 struct inpcb *inp = sotoinpcb(so); 631 struct tcpcb *tp = intotcpcb(inp); 632 633 opt0 |= V_NAGLE((tp->t_flags & TF_NODELAY) == 0); 634 } else 635 opt0 |= V_NAGLE(s->nagle != 0); 636 637 return htobe64(opt0); 638 } 639 640 uint64_t 641 select_ntuple(struct vi_info *vi, struct l2t_entry *e) 642 { 643 struct adapter *sc = vi->pi->adapter; 644 struct tp_params *tp = &sc->params.tp; 645 uint16_t viid = vi->viid; 646 uint64_t ntuple = 0; 647 648 /* 649 * Initialize each of the fields which we care about which are present 650 * in the Compressed Filter Tuple. 651 */ 652 if (tp->vlan_shift >= 0 && e->vlan != CPL_L2T_VLAN_NONE) 653 ntuple |= (uint64_t)(F_FT_VLAN_VLD | e->vlan) << tp->vlan_shift; 654 655 if (tp->port_shift >= 0) 656 ntuple |= (uint64_t)e->lport << tp->port_shift; 657 658 if (tp->protocol_shift >= 0) 659 ntuple |= (uint64_t)IPPROTO_TCP << tp->protocol_shift; 660 661 if (tp->vnic_shift >= 0 && tp->ingress_config & F_VNIC) { 662 uint32_t vf = G_FW_VIID_VIN(viid); 663 uint32_t pf = G_FW_VIID_PFN(viid); 664 uint32_t vld = G_FW_VIID_VIVLD(viid); 665 666 ntuple |= (uint64_t)(V_FT_VNID_ID_VF(vf) | V_FT_VNID_ID_PF(pf) | 667 V_FT_VNID_ID_VLD(vld)) << tp->vnic_shift; 668 } 669 670 if (is_t4(sc)) 671 return (htobe32((uint32_t)ntuple)); 672 else 673 return (htobe64(V_FILTER_TUPLE(ntuple))); 674 } 675 676 static int 677 is_tls_sock(struct socket *so, struct adapter *sc) 678 { 679 struct inpcb *inp = sotoinpcb(so); 680 int i, rc; 681 682 /* XXX: Eventually add a SO_WANT_TLS socket option perhaps? */ 683 rc = 0; 684 ADAPTER_LOCK(sc); 685 for (i = 0; i < sc->tt.num_tls_rx_ports; i++) { 686 if (inp->inp_lport == htons(sc->tt.tls_rx_ports[i]) || 687 inp->inp_fport == htons(sc->tt.tls_rx_ports[i])) { 688 rc = 1; 689 break; 690 } 691 } 692 ADAPTER_UNLOCK(sc); 693 return (rc); 694 } 695 696 int 697 select_ulp_mode(struct socket *so, struct adapter *sc, 698 struct offload_settings *s) 699 { 700 701 if (can_tls_offload(sc) && 702 (s->tls > 0 || (s->tls < 0 && is_tls_sock(so, sc)))) 703 return (ULP_MODE_TLS); 704 else if (s->ddp > 0 || 705 (s->ddp < 0 && sc->tt.ddp && (so->so_options & SO_NO_DDP) == 0)) 706 return (ULP_MODE_TCPDDP); 707 else 708 return (ULP_MODE_NONE); 709 } 710 711 void 712 set_ulp_mode(struct toepcb *toep, int ulp_mode) 713 { 714 715 CTR4(KTR_CXGBE, "%s: toep %p (tid %d) ulp_mode %d", 716 __func__, toep, toep->tid, ulp_mode); 717 toep->ulp_mode = ulp_mode; 718 tls_init_toep(toep); 719 if (toep->ulp_mode == ULP_MODE_TCPDDP) 720 ddp_init_toep(toep); 721 } 722 723 int 724 negative_advice(int status) 725 { 726 727 return (status == CPL_ERR_RTX_NEG_ADVICE || 728 status == CPL_ERR_PERSIST_NEG_ADVICE || 729 status == CPL_ERR_KEEPALV_NEG_ADVICE); 730 } 731 732 static int 733 alloc_tid_tab(struct tid_info *t, int flags) 734 { 735 736 MPASS(t->ntids > 0); 737 MPASS(t->tid_tab == NULL); 738 739 t->tid_tab = malloc(t->ntids * sizeof(*t->tid_tab), M_CXGBE, 740 M_ZERO | flags); 741 if (t->tid_tab == NULL) 742 return (ENOMEM); 743 atomic_store_rel_int(&t->tids_in_use, 0); 744 745 return (0); 746 } 747 748 static void 749 free_tid_tab(struct tid_info *t) 750 { 751 752 KASSERT(t->tids_in_use == 0, 753 ("%s: %d tids still in use.", __func__, t->tids_in_use)); 754 755 free(t->tid_tab, M_CXGBE); 756 t->tid_tab = NULL; 757 } 758 759 static int 760 alloc_stid_tab(struct tid_info *t, int flags) 761 { 762 763 MPASS(t->nstids > 0); 764 MPASS(t->stid_tab == NULL); 765 766 t->stid_tab = malloc(t->nstids * sizeof(*t->stid_tab), M_CXGBE, 767 M_ZERO | flags); 768 if (t->stid_tab == NULL) 769 return (ENOMEM); 770 mtx_init(&t->stid_lock, "stid lock", NULL, MTX_DEF); 771 t->stids_in_use = 0; 772 TAILQ_INIT(&t->stids); 773 t->nstids_free_head = t->nstids; 774 775 return (0); 776 } 777 778 static void 779 free_stid_tab(struct tid_info *t) 780 { 781 782 KASSERT(t->stids_in_use == 0, 783 ("%s: %d tids still in use.", __func__, t->stids_in_use)); 784 785 if (mtx_initialized(&t->stid_lock)) 786 mtx_destroy(&t->stid_lock); 787 free(t->stid_tab, M_CXGBE); 788 t->stid_tab = NULL; 789 } 790 791 static void 792 free_tid_tabs(struct tid_info *t) 793 { 794 795 free_tid_tab(t); 796 free_atid_tab(t); 797 free_stid_tab(t); 798 } 799 800 static int 801 alloc_tid_tabs(struct tid_info *t) 802 { 803 int rc; 804 805 rc = alloc_tid_tab(t, M_NOWAIT); 806 if (rc != 0) 807 goto failed; 808 809 rc = alloc_atid_tab(t, M_NOWAIT); 810 if (rc != 0) 811 goto failed; 812 813 rc = alloc_stid_tab(t, M_NOWAIT); 814 if (rc != 0) 815 goto failed; 816 817 return (0); 818 failed: 819 free_tid_tabs(t); 820 return (rc); 821 } 822 823 static int 824 add_lip(struct adapter *sc, struct in6_addr *lip) 825 { 826 struct fw_clip_cmd c; 827 828 ASSERT_SYNCHRONIZED_OP(sc); 829 /* mtx_assert(&td->clip_table_lock, MA_OWNED); */ 830 831 memset(&c, 0, sizeof(c)); 832 c.op_to_write = htonl(V_FW_CMD_OP(FW_CLIP_CMD) | F_FW_CMD_REQUEST | 833 F_FW_CMD_WRITE); 834 c.alloc_to_len16 = htonl(F_FW_CLIP_CMD_ALLOC | FW_LEN16(c)); 835 c.ip_hi = *(uint64_t *)&lip->s6_addr[0]; 836 c.ip_lo = *(uint64_t *)&lip->s6_addr[8]; 837 838 return (-t4_wr_mbox_ns(sc, sc->mbox, &c, sizeof(c), &c)); 839 } 840 841 static int 842 delete_lip(struct adapter *sc, struct in6_addr *lip) 843 { 844 struct fw_clip_cmd c; 845 846 ASSERT_SYNCHRONIZED_OP(sc); 847 /* mtx_assert(&td->clip_table_lock, MA_OWNED); */ 848 849 memset(&c, 0, sizeof(c)); 850 c.op_to_write = htonl(V_FW_CMD_OP(FW_CLIP_CMD) | F_FW_CMD_REQUEST | 851 F_FW_CMD_READ); 852 c.alloc_to_len16 = htonl(F_FW_CLIP_CMD_FREE | FW_LEN16(c)); 853 c.ip_hi = *(uint64_t *)&lip->s6_addr[0]; 854 c.ip_lo = *(uint64_t *)&lip->s6_addr[8]; 855 856 return (-t4_wr_mbox_ns(sc, sc->mbox, &c, sizeof(c), &c)); 857 } 858 859 static struct clip_entry * 860 search_lip(struct tom_data *td, struct in6_addr *lip) 861 { 862 struct clip_entry *ce; 863 864 mtx_assert(&td->clip_table_lock, MA_OWNED); 865 866 TAILQ_FOREACH(ce, &td->clip_table, link) { 867 if (IN6_ARE_ADDR_EQUAL(&ce->lip, lip)) 868 return (ce); 869 } 870 871 return (NULL); 872 } 873 874 struct clip_entry * 875 hold_lip(struct tom_data *td, struct in6_addr *lip, struct clip_entry *ce) 876 { 877 878 mtx_lock(&td->clip_table_lock); 879 if (ce == NULL) 880 ce = search_lip(td, lip); 881 if (ce != NULL) 882 ce->refcount++; 883 mtx_unlock(&td->clip_table_lock); 884 885 return (ce); 886 } 887 888 void 889 release_lip(struct tom_data *td, struct clip_entry *ce) 890 { 891 892 mtx_lock(&td->clip_table_lock); 893 KASSERT(search_lip(td, &ce->lip) == ce, 894 ("%s: CLIP entry %p p not in CLIP table.", __func__, ce)); 895 KASSERT(ce->refcount > 0, 896 ("%s: CLIP entry %p has refcount 0", __func__, ce)); 897 --ce->refcount; 898 mtx_unlock(&td->clip_table_lock); 899 } 900 901 static void 902 init_clip_table(struct adapter *sc, struct tom_data *td) 903 { 904 905 ASSERT_SYNCHRONIZED_OP(sc); 906 907 mtx_init(&td->clip_table_lock, "CLIP table lock", NULL, MTX_DEF); 908 TAILQ_INIT(&td->clip_table); 909 td->clip_gen = -1; 910 911 update_clip_table(sc, td); 912 } 913 914 static void 915 update_clip(struct adapter *sc, void *arg __unused) 916 { 917 918 if (begin_synchronized_op(sc, NULL, HOLD_LOCK, "t4tomuc")) 919 return; 920 921 if (uld_active(sc, ULD_TOM)) 922 update_clip_table(sc, sc->tom_softc); 923 924 end_synchronized_op(sc, LOCK_HELD); 925 } 926 927 static void 928 t4_clip_task(void *arg, int count) 929 { 930 931 t4_iterate(update_clip, NULL); 932 } 933 934 static void 935 update_clip_table(struct adapter *sc, struct tom_data *td) 936 { 937 struct rm_priotracker in6_ifa_tracker; 938 struct in6_ifaddr *ia; 939 struct in6_addr *lip, tlip; 940 struct clip_head stale; 941 struct clip_entry *ce, *ce_temp; 942 struct vi_info *vi; 943 int rc, gen, i, j; 944 uintptr_t last_vnet; 945 946 ASSERT_SYNCHRONIZED_OP(sc); 947 948 IN6_IFADDR_RLOCK(&in6_ifa_tracker); 949 mtx_lock(&td->clip_table_lock); 950 951 gen = atomic_load_acq_int(&in6_ifaddr_gen); 952 if (gen == td->clip_gen) 953 goto done; 954 955 TAILQ_INIT(&stale); 956 TAILQ_CONCAT(&stale, &td->clip_table, link); 957 958 /* 959 * last_vnet optimizes the common cases where all if_vnet = NULL (no 960 * VIMAGE) or all if_vnet = vnet0. 961 */ 962 last_vnet = (uintptr_t)(-1); 963 for_each_port(sc, i) 964 for_each_vi(sc->port[i], j, vi) { 965 if (last_vnet == (uintptr_t)vi->ifp->if_vnet) 966 continue; 967 968 /* XXX: races with if_vmove */ 969 CURVNET_SET(vi->ifp->if_vnet); 970 CK_STAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) { 971 lip = &ia->ia_addr.sin6_addr; 972 973 KASSERT(!IN6_IS_ADDR_MULTICAST(lip), 974 ("%s: mcast address in in6_ifaddr list", __func__)); 975 976 if (IN6_IS_ADDR_LOOPBACK(lip)) 977 continue; 978 if (IN6_IS_SCOPE_EMBED(lip)) { 979 /* Remove the embedded scope */ 980 tlip = *lip; 981 lip = &tlip; 982 in6_clearscope(lip); 983 } 984 /* 985 * XXX: how to weed out the link local address for the 986 * loopback interface? It's fe80::1 usually (always?). 987 */ 988 989 /* 990 * If it's in the main list then we already know it's 991 * not stale. 992 */ 993 TAILQ_FOREACH(ce, &td->clip_table, link) { 994 if (IN6_ARE_ADDR_EQUAL(&ce->lip, lip)) 995 goto next; 996 } 997 998 /* 999 * If it's in the stale list we should move it to the 1000 * main list. 1001 */ 1002 TAILQ_FOREACH(ce, &stale, link) { 1003 if (IN6_ARE_ADDR_EQUAL(&ce->lip, lip)) { 1004 TAILQ_REMOVE(&stale, ce, link); 1005 TAILQ_INSERT_TAIL(&td->clip_table, ce, 1006 link); 1007 goto next; 1008 } 1009 } 1010 1011 /* A new IP6 address; add it to the CLIP table */ 1012 ce = malloc(sizeof(*ce), M_CXGBE, M_NOWAIT); 1013 memcpy(&ce->lip, lip, sizeof(ce->lip)); 1014 ce->refcount = 0; 1015 rc = add_lip(sc, lip); 1016 if (rc == 0) 1017 TAILQ_INSERT_TAIL(&td->clip_table, ce, link); 1018 else { 1019 char ip[INET6_ADDRSTRLEN]; 1020 1021 inet_ntop(AF_INET6, &ce->lip, &ip[0], 1022 sizeof(ip)); 1023 log(LOG_ERR, "%s: could not add %s (%d)\n", 1024 __func__, ip, rc); 1025 free(ce, M_CXGBE); 1026 } 1027 next: 1028 continue; 1029 } 1030 CURVNET_RESTORE(); 1031 last_vnet = (uintptr_t)vi->ifp->if_vnet; 1032 } 1033 1034 /* 1035 * Remove stale addresses (those no longer in V_in6_ifaddrhead) that are 1036 * no longer referenced by the driver. 1037 */ 1038 TAILQ_FOREACH_SAFE(ce, &stale, link, ce_temp) { 1039 if (ce->refcount == 0) { 1040 rc = delete_lip(sc, &ce->lip); 1041 if (rc == 0) { 1042 TAILQ_REMOVE(&stale, ce, link); 1043 free(ce, M_CXGBE); 1044 } else { 1045 char ip[INET6_ADDRSTRLEN]; 1046 1047 inet_ntop(AF_INET6, &ce->lip, &ip[0], 1048 sizeof(ip)); 1049 log(LOG_ERR, "%s: could not delete %s (%d)\n", 1050 __func__, ip, rc); 1051 } 1052 } 1053 } 1054 /* The ones that are still referenced need to stay in the CLIP table */ 1055 TAILQ_CONCAT(&td->clip_table, &stale, link); 1056 1057 td->clip_gen = gen; 1058 done: 1059 mtx_unlock(&td->clip_table_lock); 1060 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker); 1061 } 1062 1063 static void 1064 destroy_clip_table(struct adapter *sc, struct tom_data *td) 1065 { 1066 struct clip_entry *ce, *ce_temp; 1067 1068 if (mtx_initialized(&td->clip_table_lock)) { 1069 mtx_lock(&td->clip_table_lock); 1070 TAILQ_FOREACH_SAFE(ce, &td->clip_table, link, ce_temp) { 1071 KASSERT(ce->refcount == 0, 1072 ("%s: CLIP entry %p still in use (%d)", __func__, 1073 ce, ce->refcount)); 1074 TAILQ_REMOVE(&td->clip_table, ce, link); 1075 delete_lip(sc, &ce->lip); 1076 free(ce, M_CXGBE); 1077 } 1078 mtx_unlock(&td->clip_table_lock); 1079 mtx_destroy(&td->clip_table_lock); 1080 } 1081 } 1082 1083 static void 1084 free_tom_data(struct adapter *sc, struct tom_data *td) 1085 { 1086 1087 ASSERT_SYNCHRONIZED_OP(sc); 1088 1089 KASSERT(TAILQ_EMPTY(&td->toep_list), 1090 ("%s: TOE PCB list is not empty.", __func__)); 1091 KASSERT(td->lctx_count == 0, 1092 ("%s: lctx hash table is not empty.", __func__)); 1093 1094 tls_free_kmap(td); 1095 t4_free_ppod_region(&td->pr); 1096 destroy_clip_table(sc, td); 1097 1098 if (td->listen_mask != 0) 1099 hashdestroy(td->listen_hash, M_CXGBE, td->listen_mask); 1100 1101 if (mtx_initialized(&td->unsent_wr_lock)) 1102 mtx_destroy(&td->unsent_wr_lock); 1103 if (mtx_initialized(&td->lctx_hash_lock)) 1104 mtx_destroy(&td->lctx_hash_lock); 1105 if (mtx_initialized(&td->toep_list_lock)) 1106 mtx_destroy(&td->toep_list_lock); 1107 1108 free_tid_tabs(&sc->tids); 1109 free(td, M_CXGBE); 1110 } 1111 1112 static char * 1113 prepare_pkt(int open_type, uint16_t vtag, struct inpcb *inp, int *pktlen, 1114 int *buflen) 1115 { 1116 char *pkt; 1117 struct tcphdr *th; 1118 int ipv6, len; 1119 const int maxlen = 1120 max(sizeof(struct ether_header), sizeof(struct ether_vlan_header)) + 1121 max(sizeof(struct ip), sizeof(struct ip6_hdr)) + 1122 sizeof(struct tcphdr); 1123 1124 MPASS(open_type == OPEN_TYPE_ACTIVE || open_type == OPEN_TYPE_LISTEN); 1125 1126 pkt = malloc(maxlen, M_CXGBE, M_ZERO | M_NOWAIT); 1127 if (pkt == NULL) 1128 return (NULL); 1129 1130 ipv6 = inp->inp_vflag & INP_IPV6; 1131 len = 0; 1132 1133 if (vtag == 0xffff) { 1134 struct ether_header *eh = (void *)pkt; 1135 1136 if (ipv6) 1137 eh->ether_type = htons(ETHERTYPE_IPV6); 1138 else 1139 eh->ether_type = htons(ETHERTYPE_IP); 1140 1141 len += sizeof(*eh); 1142 } else { 1143 struct ether_vlan_header *evh = (void *)pkt; 1144 1145 evh->evl_encap_proto = htons(ETHERTYPE_VLAN); 1146 evh->evl_tag = htons(vtag); 1147 if (ipv6) 1148 evh->evl_proto = htons(ETHERTYPE_IPV6); 1149 else 1150 evh->evl_proto = htons(ETHERTYPE_IP); 1151 1152 len += sizeof(*evh); 1153 } 1154 1155 if (ipv6) { 1156 struct ip6_hdr *ip6 = (void *)&pkt[len]; 1157 1158 ip6->ip6_vfc = IPV6_VERSION; 1159 ip6->ip6_plen = htons(sizeof(struct tcphdr)); 1160 ip6->ip6_nxt = IPPROTO_TCP; 1161 if (open_type == OPEN_TYPE_ACTIVE) { 1162 ip6->ip6_src = inp->in6p_laddr; 1163 ip6->ip6_dst = inp->in6p_faddr; 1164 } else if (open_type == OPEN_TYPE_LISTEN) { 1165 ip6->ip6_src = inp->in6p_laddr; 1166 ip6->ip6_dst = ip6->ip6_src; 1167 } 1168 1169 len += sizeof(*ip6); 1170 } else { 1171 struct ip *ip = (void *)&pkt[len]; 1172 1173 ip->ip_v = IPVERSION; 1174 ip->ip_hl = sizeof(*ip) >> 2; 1175 ip->ip_tos = inp->inp_ip_tos; 1176 ip->ip_len = htons(sizeof(struct ip) + sizeof(struct tcphdr)); 1177 ip->ip_ttl = inp->inp_ip_ttl; 1178 ip->ip_p = IPPROTO_TCP; 1179 if (open_type == OPEN_TYPE_ACTIVE) { 1180 ip->ip_src = inp->inp_laddr; 1181 ip->ip_dst = inp->inp_faddr; 1182 } else if (open_type == OPEN_TYPE_LISTEN) { 1183 ip->ip_src = inp->inp_laddr; 1184 ip->ip_dst = ip->ip_src; 1185 } 1186 1187 len += sizeof(*ip); 1188 } 1189 1190 th = (void *)&pkt[len]; 1191 if (open_type == OPEN_TYPE_ACTIVE) { 1192 th->th_sport = inp->inp_lport; /* network byte order already */ 1193 th->th_dport = inp->inp_fport; /* ditto */ 1194 } else if (open_type == OPEN_TYPE_LISTEN) { 1195 th->th_sport = inp->inp_lport; /* network byte order already */ 1196 th->th_dport = th->th_sport; 1197 } 1198 len += sizeof(th); 1199 1200 *pktlen = *buflen = len; 1201 return (pkt); 1202 } 1203 1204 const struct offload_settings * 1205 lookup_offload_policy(struct adapter *sc, int open_type, struct mbuf *m, 1206 uint16_t vtag, struct inpcb *inp) 1207 { 1208 const struct t4_offload_policy *op; 1209 char *pkt; 1210 struct offload_rule *r; 1211 int i, matched, pktlen, buflen; 1212 static const struct offload_settings allow_offloading_settings = { 1213 .offload = 1, 1214 .rx_coalesce = -1, 1215 .cong_algo = -1, 1216 .sched_class = -1, 1217 .tstamp = -1, 1218 .sack = -1, 1219 .nagle = -1, 1220 .ecn = -1, 1221 .ddp = -1, 1222 .tls = -1, 1223 .txq = -1, 1224 .rxq = -1, 1225 .mss = -1, 1226 }; 1227 static const struct offload_settings disallow_offloading_settings = { 1228 .offload = 0, 1229 /* rest is irrelevant when offload is off. */ 1230 }; 1231 1232 rw_assert(&sc->policy_lock, RA_LOCKED); 1233 1234 /* 1235 * If there's no Connection Offloading Policy attached to the device 1236 * then we need to return a default static policy. If 1237 * "cop_managed_offloading" is true, then we need to disallow 1238 * offloading until a COP is attached to the device. Otherwise we 1239 * allow offloading ... 1240 */ 1241 op = sc->policy; 1242 if (op == NULL) { 1243 if (sc->tt.cop_managed_offloading) 1244 return (&disallow_offloading_settings); 1245 else 1246 return (&allow_offloading_settings); 1247 } 1248 1249 switch (open_type) { 1250 case OPEN_TYPE_ACTIVE: 1251 case OPEN_TYPE_LISTEN: 1252 pkt = prepare_pkt(open_type, vtag, inp, &pktlen, &buflen); 1253 break; 1254 case OPEN_TYPE_PASSIVE: 1255 MPASS(m != NULL); 1256 pkt = mtod(m, char *); 1257 MPASS(*pkt == CPL_PASS_ACCEPT_REQ); 1258 pkt += sizeof(struct cpl_pass_accept_req); 1259 pktlen = m->m_pkthdr.len - sizeof(struct cpl_pass_accept_req); 1260 buflen = m->m_len - sizeof(struct cpl_pass_accept_req); 1261 break; 1262 default: 1263 MPASS(0); 1264 return (&disallow_offloading_settings); 1265 } 1266 1267 if (pkt == NULL || pktlen == 0 || buflen == 0) 1268 return (&disallow_offloading_settings); 1269 1270 r = &op->rule[0]; 1271 for (i = 0; i < op->nrules; i++, r++) { 1272 if (r->open_type != open_type && 1273 r->open_type != OPEN_TYPE_DONTCARE) { 1274 continue; 1275 } 1276 matched = bpf_filter(r->bpf_prog.bf_insns, pkt, pktlen, buflen); 1277 if (matched) 1278 break; 1279 } 1280 1281 if (open_type == OPEN_TYPE_ACTIVE || open_type == OPEN_TYPE_LISTEN) 1282 free(pkt, M_CXGBE); 1283 1284 return (matched ? &r->settings : &disallow_offloading_settings); 1285 } 1286 1287 static void 1288 reclaim_wr_resources(void *arg, int count) 1289 { 1290 struct tom_data *td = arg; 1291 STAILQ_HEAD(, wrqe) twr_list = STAILQ_HEAD_INITIALIZER(twr_list); 1292 struct cpl_act_open_req *cpl; 1293 u_int opcode, atid; 1294 struct wrqe *wr; 1295 struct adapter *sc; 1296 1297 mtx_lock(&td->unsent_wr_lock); 1298 STAILQ_SWAP(&td->unsent_wr_list, &twr_list, wrqe); 1299 mtx_unlock(&td->unsent_wr_lock); 1300 1301 while ((wr = STAILQ_FIRST(&twr_list)) != NULL) { 1302 STAILQ_REMOVE_HEAD(&twr_list, link); 1303 1304 cpl = wrtod(wr); 1305 opcode = GET_OPCODE(cpl); 1306 1307 switch (opcode) { 1308 case CPL_ACT_OPEN_REQ: 1309 case CPL_ACT_OPEN_REQ6: 1310 atid = G_TID_TID(be32toh(OPCODE_TID(cpl))); 1311 sc = td_adapter(td); 1312 1313 CTR2(KTR_CXGBE, "%s: atid %u ", __func__, atid); 1314 act_open_failure_cleanup(sc, atid, EHOSTUNREACH); 1315 free(wr, M_CXGBE); 1316 break; 1317 default: 1318 log(LOG_ERR, "%s: leaked work request %p, wr_len %d, " 1319 "opcode %x\n", __func__, wr, wr->wr_len, opcode); 1320 /* WR not freed here; go look at it with a debugger. */ 1321 } 1322 } 1323 } 1324 1325 /* 1326 * Ground control to Major TOM 1327 * Commencing countdown, engines on 1328 */ 1329 static int 1330 t4_tom_activate(struct adapter *sc) 1331 { 1332 struct tom_data *td; 1333 struct toedev *tod; 1334 struct vi_info *vi; 1335 int i, rc, v; 1336 1337 ASSERT_SYNCHRONIZED_OP(sc); 1338 1339 /* per-adapter softc for TOM */ 1340 td = malloc(sizeof(*td), M_CXGBE, M_ZERO | M_NOWAIT); 1341 if (td == NULL) 1342 return (ENOMEM); 1343 1344 /* List of TOE PCBs and associated lock */ 1345 mtx_init(&td->toep_list_lock, "PCB list lock", NULL, MTX_DEF); 1346 TAILQ_INIT(&td->toep_list); 1347 1348 /* Listen context */ 1349 mtx_init(&td->lctx_hash_lock, "lctx hash lock", NULL, MTX_DEF); 1350 td->listen_hash = hashinit_flags(LISTEN_HASH_SIZE, M_CXGBE, 1351 &td->listen_mask, HASH_NOWAIT); 1352 1353 /* List of WRs for which L2 resolution failed */ 1354 mtx_init(&td->unsent_wr_lock, "Unsent WR list lock", NULL, MTX_DEF); 1355 STAILQ_INIT(&td->unsent_wr_list); 1356 TASK_INIT(&td->reclaim_wr_resources, 0, reclaim_wr_resources, td); 1357 1358 /* TID tables */ 1359 rc = alloc_tid_tabs(&sc->tids); 1360 if (rc != 0) 1361 goto done; 1362 1363 rc = t4_init_ppod_region(&td->pr, &sc->vres.ddp, 1364 t4_read_reg(sc, A_ULP_RX_TDDP_PSZ), "TDDP page pods"); 1365 if (rc != 0) 1366 goto done; 1367 t4_set_reg_field(sc, A_ULP_RX_TDDP_TAGMASK, 1368 V_TDDPTAGMASK(M_TDDPTAGMASK), td->pr.pr_tag_mask); 1369 1370 /* CLIP table for IPv6 offload */ 1371 init_clip_table(sc, td); 1372 1373 if (sc->vres.key.size != 0) { 1374 rc = tls_init_kmap(sc, td); 1375 if (rc != 0) 1376 goto done; 1377 } 1378 1379 /* toedev ops */ 1380 tod = &td->tod; 1381 init_toedev(tod); 1382 tod->tod_softc = sc; 1383 tod->tod_connect = t4_connect; 1384 tod->tod_listen_start = t4_listen_start; 1385 tod->tod_listen_stop = t4_listen_stop; 1386 tod->tod_rcvd = t4_rcvd; 1387 tod->tod_output = t4_tod_output; 1388 tod->tod_send_rst = t4_send_rst; 1389 tod->tod_send_fin = t4_send_fin; 1390 tod->tod_pcb_detach = t4_pcb_detach; 1391 tod->tod_l2_update = t4_l2_update; 1392 tod->tod_syncache_added = t4_syncache_added; 1393 tod->tod_syncache_removed = t4_syncache_removed; 1394 tod->tod_syncache_respond = t4_syncache_respond; 1395 tod->tod_offload_socket = t4_offload_socket; 1396 tod->tod_ctloutput = t4_ctloutput; 1397 tod->tod_tcp_info = t4_tcp_info; 1398 1399 for_each_port(sc, i) { 1400 for_each_vi(sc->port[i], v, vi) { 1401 TOEDEV(vi->ifp) = &td->tod; 1402 } 1403 } 1404 1405 sc->tom_softc = td; 1406 register_toedev(sc->tom_softc); 1407 1408 done: 1409 if (rc != 0) 1410 free_tom_data(sc, td); 1411 return (rc); 1412 } 1413 1414 static int 1415 t4_tom_deactivate(struct adapter *sc) 1416 { 1417 int rc = 0; 1418 struct tom_data *td = sc->tom_softc; 1419 1420 ASSERT_SYNCHRONIZED_OP(sc); 1421 1422 if (td == NULL) 1423 return (0); /* XXX. KASSERT? */ 1424 1425 if (sc->offload_map != 0) 1426 return (EBUSY); /* at least one port has IFCAP_TOE enabled */ 1427 1428 if (uld_active(sc, ULD_IWARP) || uld_active(sc, ULD_ISCSI)) 1429 return (EBUSY); /* both iWARP and iSCSI rely on the TOE. */ 1430 1431 mtx_lock(&td->toep_list_lock); 1432 if (!TAILQ_EMPTY(&td->toep_list)) 1433 rc = EBUSY; 1434 mtx_unlock(&td->toep_list_lock); 1435 1436 mtx_lock(&td->lctx_hash_lock); 1437 if (td->lctx_count > 0) 1438 rc = EBUSY; 1439 mtx_unlock(&td->lctx_hash_lock); 1440 1441 taskqueue_drain(taskqueue_thread, &td->reclaim_wr_resources); 1442 mtx_lock(&td->unsent_wr_lock); 1443 if (!STAILQ_EMPTY(&td->unsent_wr_list)) 1444 rc = EBUSY; 1445 mtx_unlock(&td->unsent_wr_lock); 1446 1447 if (rc == 0) { 1448 unregister_toedev(sc->tom_softc); 1449 free_tom_data(sc, td); 1450 sc->tom_softc = NULL; 1451 } 1452 1453 return (rc); 1454 } 1455 1456 static void 1457 t4_tom_ifaddr_event(void *arg __unused, struct ifnet *ifp) 1458 { 1459 1460 atomic_add_rel_int(&in6_ifaddr_gen, 1); 1461 taskqueue_enqueue_timeout(taskqueue_thread, &clip_task, -hz / 4); 1462 } 1463 1464 static int 1465 t4_aio_queue_tom(struct socket *so, struct kaiocb *job) 1466 { 1467 struct tcpcb *tp = so_sototcpcb(so); 1468 struct toepcb *toep = tp->t_toe; 1469 int error; 1470 1471 if (toep->ulp_mode == ULP_MODE_TCPDDP) { 1472 error = t4_aio_queue_ddp(so, job); 1473 if (error != EOPNOTSUPP) 1474 return (error); 1475 } 1476 1477 return (t4_aio_queue_aiotx(so, job)); 1478 } 1479 1480 static int 1481 t4_ctloutput_tom(struct socket *so, struct sockopt *sopt) 1482 { 1483 1484 if (sopt->sopt_level != IPPROTO_TCP) 1485 return (tcp_ctloutput(so, sopt)); 1486 1487 switch (sopt->sopt_name) { 1488 case TCP_TLSOM_SET_TLS_CONTEXT: 1489 case TCP_TLSOM_GET_TLS_TOM: 1490 case TCP_TLSOM_CLR_TLS_TOM: 1491 case TCP_TLSOM_CLR_QUIES: 1492 return (t4_ctloutput_tls(so, sopt)); 1493 default: 1494 return (tcp_ctloutput(so, sopt)); 1495 } 1496 } 1497 1498 static int 1499 t4_tom_mod_load(void) 1500 { 1501 struct protosw *tcp_protosw, *tcp6_protosw; 1502 1503 /* CPL handlers */ 1504 t4_register_shared_cpl_handler(CPL_L2T_WRITE_RPL, do_l2t_write_rpl2, 1505 CPL_COOKIE_TOM); 1506 t4_init_connect_cpl_handlers(); 1507 t4_init_listen_cpl_handlers(); 1508 t4_init_cpl_io_handlers(); 1509 1510 t4_ddp_mod_load(); 1511 t4_tls_mod_load(); 1512 1513 tcp_protosw = pffindproto(PF_INET, IPPROTO_TCP, SOCK_STREAM); 1514 if (tcp_protosw == NULL) 1515 return (ENOPROTOOPT); 1516 bcopy(tcp_protosw, &toe_protosw, sizeof(toe_protosw)); 1517 bcopy(tcp_protosw->pr_usrreqs, &toe_usrreqs, sizeof(toe_usrreqs)); 1518 toe_usrreqs.pru_aio_queue = t4_aio_queue_tom; 1519 toe_protosw.pr_ctloutput = t4_ctloutput_tom; 1520 toe_protosw.pr_usrreqs = &toe_usrreqs; 1521 1522 tcp6_protosw = pffindproto(PF_INET6, IPPROTO_TCP, SOCK_STREAM); 1523 if (tcp6_protosw == NULL) 1524 return (ENOPROTOOPT); 1525 bcopy(tcp6_protosw, &toe6_protosw, sizeof(toe6_protosw)); 1526 bcopy(tcp6_protosw->pr_usrreqs, &toe6_usrreqs, sizeof(toe6_usrreqs)); 1527 toe6_usrreqs.pru_aio_queue = t4_aio_queue_tom; 1528 toe6_protosw.pr_ctloutput = t4_ctloutput_tom; 1529 toe6_protosw.pr_usrreqs = &toe6_usrreqs; 1530 1531 TIMEOUT_TASK_INIT(taskqueue_thread, &clip_task, 0, t4_clip_task, NULL); 1532 ifaddr_evhandler = EVENTHANDLER_REGISTER(ifaddr_event, 1533 t4_tom_ifaddr_event, NULL, EVENTHANDLER_PRI_ANY); 1534 1535 return (t4_register_uld(&tom_uld_info)); 1536 } 1537 1538 static void 1539 tom_uninit(struct adapter *sc, void *arg __unused) 1540 { 1541 if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4tomun")) 1542 return; 1543 1544 /* Try to free resources (works only if no port has IFCAP_TOE) */ 1545 if (uld_active(sc, ULD_TOM)) 1546 t4_deactivate_uld(sc, ULD_TOM); 1547 1548 end_synchronized_op(sc, 0); 1549 } 1550 1551 static int 1552 t4_tom_mod_unload(void) 1553 { 1554 t4_iterate(tom_uninit, NULL); 1555 1556 if (t4_unregister_uld(&tom_uld_info) == EBUSY) 1557 return (EBUSY); 1558 1559 if (ifaddr_evhandler) { 1560 EVENTHANDLER_DEREGISTER(ifaddr_event, ifaddr_evhandler); 1561 taskqueue_cancel_timeout(taskqueue_thread, &clip_task, NULL); 1562 } 1563 1564 t4_tls_mod_unload(); 1565 t4_ddp_mod_unload(); 1566 1567 t4_uninit_connect_cpl_handlers(); 1568 t4_uninit_listen_cpl_handlers(); 1569 t4_uninit_cpl_io_handlers(); 1570 t4_register_shared_cpl_handler(CPL_L2T_WRITE_RPL, NULL, CPL_COOKIE_TOM); 1571 1572 return (0); 1573 } 1574 #endif /* TCP_OFFLOAD */ 1575 1576 static int 1577 t4_tom_modevent(module_t mod, int cmd, void *arg) 1578 { 1579 int rc = 0; 1580 1581 #ifdef TCP_OFFLOAD 1582 switch (cmd) { 1583 case MOD_LOAD: 1584 rc = t4_tom_mod_load(); 1585 break; 1586 1587 case MOD_UNLOAD: 1588 rc = t4_tom_mod_unload(); 1589 break; 1590 1591 default: 1592 rc = EINVAL; 1593 } 1594 #else 1595 printf("t4_tom: compiled without TCP_OFFLOAD support.\n"); 1596 rc = EOPNOTSUPP; 1597 #endif 1598 return (rc); 1599 } 1600 1601 static moduledata_t t4_tom_moddata= { 1602 "t4_tom", 1603 t4_tom_modevent, 1604 0 1605 }; 1606 1607 MODULE_VERSION(t4_tom, 1); 1608 MODULE_DEPEND(t4_tom, toecore, 1, 1, 1); 1609 MODULE_DEPEND(t4_tom, t4nex, 1, 1, 1); 1610 DECLARE_MODULE(t4_tom, t4_tom_moddata, SI_SUB_EXEC, SI_ORDER_ANY); 1611