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_kern_tls.h" 36 #include "opt_ratelimit.h" 37 38 #include <sys/param.h> 39 #include <sys/types.h> 40 #include <sys/systm.h> 41 #include <sys/kernel.h> 42 #include <sys/ktr.h> 43 #include <sys/lock.h> 44 #include <sys/limits.h> 45 #include <sys/module.h> 46 #include <sys/protosw.h> 47 #include <sys/domain.h> 48 #include <sys/refcount.h> 49 #include <sys/rmlock.h> 50 #include <sys/socket.h> 51 #include <sys/socketvar.h> 52 #include <sys/sysctl.h> 53 #include <sys/taskqueue.h> 54 #include <net/if.h> 55 #include <net/if_var.h> 56 #include <net/if_types.h> 57 #include <net/if_vlan_var.h> 58 #include <netinet/in.h> 59 #include <netinet/in_pcb.h> 60 #include <netinet/in_var.h> 61 #include <netinet/ip.h> 62 #include <netinet/ip6.h> 63 #include <netinet6/scope6_var.h> 64 #define TCPSTATES 65 #include <netinet/tcp_fsm.h> 66 #include <netinet/tcp_seq.h> 67 #include <netinet/tcp_timer.h> 68 #include <netinet/tcp_var.h> 69 #include <netinet/toecore.h> 70 #include <netinet/cc/cc.h> 71 72 #ifdef TCP_OFFLOAD 73 #include "common/common.h" 74 #include "common/t4_msg.h" 75 #include "common/t4_regs.h" 76 #include "common/t4_regs_values.h" 77 #include "common/t4_tcb.h" 78 #include "t4_clip.h" 79 #include "tom/t4_tom_l2t.h" 80 #include "tom/t4_tom.h" 81 #include "tom/t4_tls.h" 82 83 static struct protosw toe_protosw; 84 static struct protosw toe6_protosw; 85 86 /* Module ops */ 87 static int t4_tom_mod_load(void); 88 static int t4_tom_mod_unload(void); 89 static int t4_tom_modevent(module_t, int, void *); 90 91 /* ULD ops and helpers */ 92 static int t4_tom_activate(struct adapter *); 93 static int t4_tom_deactivate(struct adapter *); 94 95 static struct uld_info tom_uld_info = { 96 .uld_id = ULD_TOM, 97 .activate = t4_tom_activate, 98 .deactivate = t4_tom_deactivate, 99 }; 100 101 static void release_offload_resources(struct toepcb *); 102 static int alloc_tid_tabs(struct tid_info *); 103 static void free_tid_tabs(struct tid_info *); 104 static void free_tom_data(struct adapter *, struct tom_data *); 105 static void reclaim_wr_resources(void *, int); 106 107 struct toepcb * 108 alloc_toepcb(struct vi_info *vi, int flags) 109 { 110 struct port_info *pi = vi->pi; 111 struct adapter *sc = pi->adapter; 112 struct toepcb *toep; 113 int tx_credits, txsd_total, len; 114 115 /* 116 * The firmware counts tx work request credits in units of 16 bytes 117 * each. Reserve room for an ABORT_REQ so the driver never has to worry 118 * about tx credits if it wants to abort a connection. 119 */ 120 tx_credits = sc->params.ofldq_wr_cred; 121 tx_credits -= howmany(sizeof(struct cpl_abort_req), 16); 122 123 /* 124 * Shortest possible tx work request is a fw_ofld_tx_data_wr + 1 byte 125 * immediate payload, and firmware counts tx work request credits in 126 * units of 16 byte. Calculate the maximum work requests possible. 127 */ 128 txsd_total = tx_credits / 129 howmany(sizeof(struct fw_ofld_tx_data_wr) + 1, 16); 130 131 len = offsetof(struct toepcb, txsd) + 132 txsd_total * sizeof(struct ofld_tx_sdesc); 133 134 toep = malloc(len, M_CXGBE, M_ZERO | flags); 135 if (toep == NULL) 136 return (NULL); 137 138 refcount_init(&toep->refcount, 1); 139 toep->td = sc->tom_softc; 140 toep->vi = vi; 141 toep->tid = -1; 142 toep->tx_total = tx_credits; 143 toep->tx_credits = tx_credits; 144 mbufq_init(&toep->ulp_pduq, INT_MAX); 145 mbufq_init(&toep->ulp_pdu_reclaimq, INT_MAX); 146 toep->txsd_total = txsd_total; 147 toep->txsd_avail = txsd_total; 148 toep->txsd_pidx = 0; 149 toep->txsd_cidx = 0; 150 aiotx_init_toep(toep); 151 152 return (toep); 153 } 154 155 /* 156 * Initialize a toepcb after its params have been filled out. 157 */ 158 int 159 init_toepcb(struct vi_info *vi, struct toepcb *toep) 160 { 161 struct conn_params *cp = &toep->params; 162 struct port_info *pi = vi->pi; 163 struct adapter *sc = pi->adapter; 164 struct tx_cl_rl_params *tc; 165 166 if (cp->tc_idx >= 0 && cp->tc_idx < sc->params.nsched_cls) { 167 tc = &pi->sched_params->cl_rl[cp->tc_idx]; 168 mtx_lock(&sc->tc_lock); 169 if (tc->state != CS_HW_CONFIGURED) { 170 CH_ERR(vi, "tid %d cannot be bound to traffic class %d " 171 "because it is not configured (its state is %d)\n", 172 toep->tid, cp->tc_idx, tc->state); 173 cp->tc_idx = -1; 174 } else { 175 tc->refcount++; 176 } 177 mtx_unlock(&sc->tc_lock); 178 } 179 toep->ofld_txq = &sc->sge.ofld_txq[cp->txq_idx]; 180 toep->ofld_rxq = &sc->sge.ofld_rxq[cp->rxq_idx]; 181 toep->ctrlq = &sc->sge.ctrlq[pi->port_id]; 182 183 tls_init_toep(toep); 184 if (ulp_mode(toep) == ULP_MODE_TCPDDP) 185 ddp_init_toep(toep); 186 187 toep->flags |= TPF_INITIALIZED; 188 189 return (0); 190 } 191 192 struct toepcb * 193 hold_toepcb(struct toepcb *toep) 194 { 195 196 refcount_acquire(&toep->refcount); 197 return (toep); 198 } 199 200 void 201 free_toepcb(struct toepcb *toep) 202 { 203 204 if (refcount_release(&toep->refcount) == 0) 205 return; 206 207 KASSERT(!(toep->flags & TPF_ATTACHED), 208 ("%s: attached to an inpcb", __func__)); 209 KASSERT(!(toep->flags & TPF_CPL_PENDING), 210 ("%s: CPL pending", __func__)); 211 212 if (toep->flags & TPF_INITIALIZED) { 213 if (ulp_mode(toep) == ULP_MODE_TCPDDP) 214 ddp_uninit_toep(toep); 215 tls_uninit_toep(toep); 216 } 217 free(toep, M_CXGBE); 218 } 219 220 /* 221 * Set up the socket for TCP offload. 222 */ 223 void 224 offload_socket(struct socket *so, struct toepcb *toep) 225 { 226 struct tom_data *td = toep->td; 227 struct inpcb *inp = sotoinpcb(so); 228 struct tcpcb *tp = intotcpcb(inp); 229 struct sockbuf *sb; 230 231 INP_WLOCK_ASSERT(inp); 232 233 /* Update socket */ 234 sb = &so->so_snd; 235 SOCKBUF_LOCK(sb); 236 sb->sb_flags |= SB_NOCOALESCE; 237 SOCKBUF_UNLOCK(sb); 238 sb = &so->so_rcv; 239 SOCKBUF_LOCK(sb); 240 sb->sb_flags |= SB_NOCOALESCE; 241 if (inp->inp_vflag & INP_IPV6) 242 so->so_proto = &toe6_protosw; 243 else 244 so->so_proto = &toe_protosw; 245 SOCKBUF_UNLOCK(sb); 246 247 /* Update TCP PCB */ 248 tp->tod = &td->tod; 249 tp->t_toe = toep; 250 tp->t_flags |= TF_TOE; 251 252 /* Install an extra hold on inp */ 253 toep->inp = inp; 254 toep->flags |= TPF_ATTACHED; 255 in_pcbref(inp); 256 257 /* Add the TOE PCB to the active list */ 258 mtx_lock(&td->toep_list_lock); 259 TAILQ_INSERT_HEAD(&td->toep_list, toep, link); 260 mtx_unlock(&td->toep_list_lock); 261 } 262 263 void 264 restore_so_proto(struct socket *so, bool v6) 265 { 266 if (v6) 267 so->so_proto = &tcp6_protosw; 268 else 269 so->so_proto = &tcp_protosw; 270 } 271 272 /* This is _not_ the normal way to "unoffload" a socket. */ 273 void 274 undo_offload_socket(struct socket *so) 275 { 276 struct inpcb *inp = sotoinpcb(so); 277 struct tcpcb *tp = intotcpcb(inp); 278 struct toepcb *toep = tp->t_toe; 279 struct tom_data *td = toep->td; 280 struct sockbuf *sb; 281 282 INP_WLOCK_ASSERT(inp); 283 284 sb = &so->so_snd; 285 SOCKBUF_LOCK(sb); 286 sb->sb_flags &= ~SB_NOCOALESCE; 287 SOCKBUF_UNLOCK(sb); 288 sb = &so->so_rcv; 289 SOCKBUF_LOCK(sb); 290 sb->sb_flags &= ~SB_NOCOALESCE; 291 restore_so_proto(so, inp->inp_vflag & INP_IPV6); 292 SOCKBUF_UNLOCK(sb); 293 294 tp->tod = NULL; 295 tp->t_toe = NULL; 296 tp->t_flags &= ~TF_TOE; 297 298 toep->inp = NULL; 299 toep->flags &= ~TPF_ATTACHED; 300 if (in_pcbrele_wlocked(inp)) 301 panic("%s: inp freed.", __func__); 302 303 mtx_lock(&td->toep_list_lock); 304 TAILQ_REMOVE(&td->toep_list, toep, link); 305 mtx_unlock(&td->toep_list_lock); 306 } 307 308 static void 309 release_offload_resources(struct toepcb *toep) 310 { 311 struct tom_data *td = toep->td; 312 struct adapter *sc = td_adapter(td); 313 int tid = toep->tid; 314 315 KASSERT(!(toep->flags & TPF_CPL_PENDING), 316 ("%s: %p has CPL pending.", __func__, toep)); 317 KASSERT(!(toep->flags & TPF_ATTACHED), 318 ("%s: %p is still attached.", __func__, toep)); 319 320 CTR5(KTR_CXGBE, "%s: toep %p (tid %d, l2te %p, ce %p)", 321 __func__, toep, tid, toep->l2te, toep->ce); 322 323 /* 324 * These queues should have been emptied at approximately the same time 325 * that a normal connection's socket's so_snd would have been purged or 326 * drained. Do _not_ clean up here. 327 */ 328 MPASS(mbufq_len(&toep->ulp_pduq) == 0); 329 MPASS(mbufq_len(&toep->ulp_pdu_reclaimq) == 0); 330 #ifdef INVARIANTS 331 if (ulp_mode(toep) == ULP_MODE_TCPDDP) 332 ddp_assert_empty(toep); 333 #endif 334 MPASS(TAILQ_EMPTY(&toep->aiotx_jobq)); 335 336 if (toep->l2te) 337 t4_l2t_release(toep->l2te); 338 339 if (tid >= 0) { 340 remove_tid(sc, tid, toep->ce ? 2 : 1); 341 release_tid(sc, tid, toep->ctrlq); 342 } 343 344 if (toep->ce) 345 t4_release_clip_entry(sc, toep->ce); 346 347 if (toep->params.tc_idx != -1) 348 t4_release_cl_rl(sc, toep->vi->pi->port_id, toep->params.tc_idx); 349 350 mtx_lock(&td->toep_list_lock); 351 TAILQ_REMOVE(&td->toep_list, toep, link); 352 mtx_unlock(&td->toep_list_lock); 353 354 free_toepcb(toep); 355 } 356 357 /* 358 * The kernel is done with the TCP PCB and this is our opportunity to unhook the 359 * toepcb hanging off of it. If the TOE driver is also done with the toepcb (no 360 * pending CPL) then it is time to release all resources tied to the toepcb. 361 * 362 * Also gets called when an offloaded active open fails and the TOM wants the 363 * kernel to take the TCP PCB back. 364 */ 365 static void 366 t4_pcb_detach(struct toedev *tod __unused, struct tcpcb *tp) 367 { 368 #if defined(KTR) || defined(INVARIANTS) 369 struct inpcb *inp = tp->t_inpcb; 370 #endif 371 struct toepcb *toep = tp->t_toe; 372 373 INP_WLOCK_ASSERT(inp); 374 375 KASSERT(toep != NULL, ("%s: toep is NULL", __func__)); 376 KASSERT(toep->flags & TPF_ATTACHED, 377 ("%s: not attached", __func__)); 378 379 #ifdef KTR 380 if (tp->t_state == TCPS_SYN_SENT) { 381 CTR6(KTR_CXGBE, "%s: atid %d, toep %p (0x%x), inp %p (0x%x)", 382 __func__, toep->tid, toep, toep->flags, inp, 383 inp->inp_flags); 384 } else { 385 CTR6(KTR_CXGBE, 386 "t4_pcb_detach: tid %d (%s), toep %p (0x%x), inp %p (0x%x)", 387 toep->tid, tcpstates[tp->t_state], toep, toep->flags, inp, 388 inp->inp_flags); 389 } 390 #endif 391 392 if (ulp_mode(toep) == ULP_MODE_TLS) 393 tls_detach(toep); 394 395 tp->tod = NULL; 396 tp->t_toe = NULL; 397 tp->t_flags &= ~TF_TOE; 398 toep->flags &= ~TPF_ATTACHED; 399 400 if (!(toep->flags & TPF_CPL_PENDING)) 401 release_offload_resources(toep); 402 } 403 404 /* 405 * setsockopt handler. 406 */ 407 static void 408 t4_ctloutput(struct toedev *tod, struct tcpcb *tp, int dir, int name) 409 { 410 struct adapter *sc = tod->tod_softc; 411 struct toepcb *toep = tp->t_toe; 412 413 if (dir == SOPT_GET) 414 return; 415 416 CTR4(KTR_CXGBE, "%s: tp %p, dir %u, name %u", __func__, tp, dir, name); 417 418 switch (name) { 419 case TCP_NODELAY: 420 if (tp->t_state != TCPS_ESTABLISHED) 421 break; 422 toep->params.nagle = tp->t_flags & TF_NODELAY ? 0 : 1; 423 t4_set_tcb_field(sc, toep->ctrlq, toep, W_TCB_T_FLAGS, 424 V_TF_NAGLE(1), V_TF_NAGLE(toep->params.nagle), 0, 0); 425 break; 426 default: 427 break; 428 } 429 } 430 431 static inline uint64_t 432 get_tcb_tflags(const uint64_t *tcb) 433 { 434 435 return ((be64toh(tcb[14]) << 32) | (be64toh(tcb[15]) >> 32)); 436 } 437 438 static inline uint32_t 439 get_tcb_field(const uint64_t *tcb, u_int word, uint32_t mask, u_int shift) 440 { 441 #define LAST_WORD ((TCB_SIZE / 4) - 1) 442 uint64_t t1, t2; 443 int flit_idx; 444 445 MPASS(mask != 0); 446 MPASS(word <= LAST_WORD); 447 MPASS(shift < 32); 448 449 flit_idx = (LAST_WORD - word) / 2; 450 if (word & 0x1) 451 shift += 32; 452 t1 = be64toh(tcb[flit_idx]) >> shift; 453 t2 = 0; 454 if (fls(mask) > 64 - shift) { 455 /* 456 * Will spill over into the next logical flit, which is the flit 457 * before this one. The flit_idx before this one must be valid. 458 */ 459 MPASS(flit_idx > 0); 460 t2 = be64toh(tcb[flit_idx - 1]) << (64 - shift); 461 } 462 return ((t2 | t1) & mask); 463 #undef LAST_WORD 464 } 465 #define GET_TCB_FIELD(tcb, F) \ 466 get_tcb_field(tcb, W_TCB_##F, M_TCB_##F, S_TCB_##F) 467 468 /* 469 * Issues a CPL_GET_TCB to read the entire TCB for the tid. 470 */ 471 static int 472 send_get_tcb(struct adapter *sc, u_int tid) 473 { 474 struct cpl_get_tcb *cpl; 475 struct wrq_cookie cookie; 476 477 MPASS(tid >= sc->tids.tid_base); 478 MPASS(tid - sc->tids.tid_base < sc->tids.ntids); 479 480 cpl = start_wrq_wr(&sc->sge.ctrlq[0], howmany(sizeof(*cpl), 16), 481 &cookie); 482 if (__predict_false(cpl == NULL)) 483 return (ENOMEM); 484 bzero(cpl, sizeof(*cpl)); 485 INIT_TP_WR(cpl, tid); 486 OPCODE_TID(cpl) = htobe32(MK_OPCODE_TID(CPL_GET_TCB, tid)); 487 cpl->reply_ctrl = htobe16(V_REPLY_CHAN(0) | 488 V_QUEUENO(sc->sge.ofld_rxq[0].iq.cntxt_id)); 489 cpl->cookie = 0xff; 490 commit_wrq_wr(&sc->sge.ctrlq[0], cpl, &cookie); 491 492 return (0); 493 } 494 495 static struct tcb_histent * 496 alloc_tcb_histent(struct adapter *sc, u_int tid, int flags) 497 { 498 struct tcb_histent *te; 499 500 MPASS(flags == M_NOWAIT || flags == M_WAITOK); 501 502 te = malloc(sizeof(*te), M_CXGBE, M_ZERO | flags); 503 if (te == NULL) 504 return (NULL); 505 mtx_init(&te->te_lock, "TCB entry", NULL, MTX_DEF); 506 callout_init_mtx(&te->te_callout, &te->te_lock, 0); 507 te->te_adapter = sc; 508 te->te_tid = tid; 509 510 return (te); 511 } 512 513 static void 514 free_tcb_histent(struct tcb_histent *te) 515 { 516 517 mtx_destroy(&te->te_lock); 518 free(te, M_CXGBE); 519 } 520 521 /* 522 * Start tracking the tid in the TCB history. 523 */ 524 int 525 add_tid_to_history(struct adapter *sc, u_int tid) 526 { 527 struct tcb_histent *te = NULL; 528 struct tom_data *td = sc->tom_softc; 529 int rc; 530 531 MPASS(tid >= sc->tids.tid_base); 532 MPASS(tid - sc->tids.tid_base < sc->tids.ntids); 533 534 if (td->tcb_history == NULL) 535 return (ENXIO); 536 537 rw_wlock(&td->tcb_history_lock); 538 if (td->tcb_history[tid] != NULL) { 539 rc = EEXIST; 540 goto done; 541 } 542 te = alloc_tcb_histent(sc, tid, M_NOWAIT); 543 if (te == NULL) { 544 rc = ENOMEM; 545 goto done; 546 } 547 mtx_lock(&te->te_lock); 548 rc = send_get_tcb(sc, tid); 549 if (rc == 0) { 550 te->te_flags |= TE_RPL_PENDING; 551 td->tcb_history[tid] = te; 552 } else { 553 free(te, M_CXGBE); 554 } 555 mtx_unlock(&te->te_lock); 556 done: 557 rw_wunlock(&td->tcb_history_lock); 558 return (rc); 559 } 560 561 static void 562 remove_tcb_histent(struct tcb_histent *te) 563 { 564 struct adapter *sc = te->te_adapter; 565 struct tom_data *td = sc->tom_softc; 566 567 rw_assert(&td->tcb_history_lock, RA_WLOCKED); 568 mtx_assert(&te->te_lock, MA_OWNED); 569 MPASS(td->tcb_history[te->te_tid] == te); 570 571 td->tcb_history[te->te_tid] = NULL; 572 free_tcb_histent(te); 573 rw_wunlock(&td->tcb_history_lock); 574 } 575 576 static inline struct tcb_histent * 577 lookup_tcb_histent(struct adapter *sc, u_int tid, bool addrem) 578 { 579 struct tcb_histent *te; 580 struct tom_data *td = sc->tom_softc; 581 582 MPASS(tid >= sc->tids.tid_base); 583 MPASS(tid - sc->tids.tid_base < sc->tids.ntids); 584 585 if (td->tcb_history == NULL) 586 return (NULL); 587 588 if (addrem) 589 rw_wlock(&td->tcb_history_lock); 590 else 591 rw_rlock(&td->tcb_history_lock); 592 te = td->tcb_history[tid]; 593 if (te != NULL) { 594 mtx_lock(&te->te_lock); 595 return (te); /* with both locks held */ 596 } 597 if (addrem) 598 rw_wunlock(&td->tcb_history_lock); 599 else 600 rw_runlock(&td->tcb_history_lock); 601 602 return (te); 603 } 604 605 static inline void 606 release_tcb_histent(struct tcb_histent *te) 607 { 608 struct adapter *sc = te->te_adapter; 609 struct tom_data *td = sc->tom_softc; 610 611 mtx_assert(&te->te_lock, MA_OWNED); 612 mtx_unlock(&te->te_lock); 613 rw_assert(&td->tcb_history_lock, RA_RLOCKED); 614 rw_runlock(&td->tcb_history_lock); 615 } 616 617 static void 618 request_tcb(void *arg) 619 { 620 struct tcb_histent *te = arg; 621 622 mtx_assert(&te->te_lock, MA_OWNED); 623 624 /* Noone else is supposed to update the histent. */ 625 MPASS(!(te->te_flags & TE_RPL_PENDING)); 626 if (send_get_tcb(te->te_adapter, te->te_tid) == 0) 627 te->te_flags |= TE_RPL_PENDING; 628 else 629 callout_schedule(&te->te_callout, hz / 100); 630 } 631 632 static void 633 update_tcb_histent(struct tcb_histent *te, const uint64_t *tcb) 634 { 635 struct tom_data *td = te->te_adapter->tom_softc; 636 uint64_t tflags = get_tcb_tflags(tcb); 637 uint8_t sample = 0; 638 639 if (GET_TCB_FIELD(tcb, SND_MAX_RAW) != GET_TCB_FIELD(tcb, SND_UNA_RAW)) { 640 if (GET_TCB_FIELD(tcb, T_RXTSHIFT) != 0) 641 sample |= TS_RTO; 642 if (GET_TCB_FIELD(tcb, T_DUPACKS) != 0) 643 sample |= TS_DUPACKS; 644 if (GET_TCB_FIELD(tcb, T_DUPACKS) >= td->dupack_threshold) 645 sample |= TS_FASTREXMT; 646 } 647 648 if (GET_TCB_FIELD(tcb, SND_MAX_RAW) != 0) { 649 uint32_t snd_wnd; 650 651 sample |= TS_SND_BACKLOGGED; /* for whatever reason. */ 652 653 snd_wnd = GET_TCB_FIELD(tcb, RCV_ADV); 654 if (tflags & V_TF_RECV_SCALE(1)) 655 snd_wnd <<= GET_TCB_FIELD(tcb, RCV_SCALE); 656 if (GET_TCB_FIELD(tcb, SND_CWND) < snd_wnd) 657 sample |= TS_CWND_LIMITED; /* maybe due to CWND */ 658 } 659 660 if (tflags & V_TF_CCTRL_ECN(1)) { 661 662 /* 663 * CE marker on incoming IP hdr, echoing ECE back in the TCP 664 * hdr. Indicates congestion somewhere on the way from the peer 665 * to this node. 666 */ 667 if (tflags & V_TF_CCTRL_ECE(1)) 668 sample |= TS_ECN_ECE; 669 670 /* 671 * ECE seen and CWR sent (or about to be sent). Might indicate 672 * congestion on the way to the peer. This node is reducing its 673 * congestion window in response. 674 */ 675 if (tflags & (V_TF_CCTRL_CWR(1) | V_TF_CCTRL_RFR(1))) 676 sample |= TS_ECN_CWR; 677 } 678 679 te->te_sample[te->te_pidx] = sample; 680 if (++te->te_pidx == nitems(te->te_sample)) 681 te->te_pidx = 0; 682 memcpy(te->te_tcb, tcb, TCB_SIZE); 683 te->te_flags |= TE_ACTIVE; 684 } 685 686 static int 687 do_get_tcb_rpl(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m) 688 { 689 struct adapter *sc = iq->adapter; 690 const struct cpl_get_tcb_rpl *cpl = mtod(m, const void *); 691 const uint64_t *tcb = (const uint64_t *)(const void *)(cpl + 1); 692 struct tcb_histent *te; 693 const u_int tid = GET_TID(cpl); 694 bool remove; 695 696 remove = GET_TCB_FIELD(tcb, T_STATE) == TCPS_CLOSED; 697 te = lookup_tcb_histent(sc, tid, remove); 698 if (te == NULL) { 699 /* Not in the history. Who issued the GET_TCB for this? */ 700 device_printf(sc->dev, "tcb %u: flags 0x%016jx, state %u, " 701 "srtt %u, sscale %u, rscale %u, cookie 0x%x\n", tid, 702 (uintmax_t)get_tcb_tflags(tcb), GET_TCB_FIELD(tcb, T_STATE), 703 GET_TCB_FIELD(tcb, T_SRTT), GET_TCB_FIELD(tcb, SND_SCALE), 704 GET_TCB_FIELD(tcb, RCV_SCALE), cpl->cookie); 705 goto done; 706 } 707 708 MPASS(te->te_flags & TE_RPL_PENDING); 709 te->te_flags &= ~TE_RPL_PENDING; 710 if (remove) { 711 remove_tcb_histent(te); 712 } else { 713 update_tcb_histent(te, tcb); 714 callout_reset(&te->te_callout, hz / 10, request_tcb, te); 715 release_tcb_histent(te); 716 } 717 done: 718 m_freem(m); 719 return (0); 720 } 721 722 static void 723 fill_tcp_info_from_tcb(struct adapter *sc, uint64_t *tcb, struct tcp_info *ti) 724 { 725 uint32_t v; 726 727 ti->tcpi_state = GET_TCB_FIELD(tcb, T_STATE); 728 729 v = GET_TCB_FIELD(tcb, T_SRTT); 730 ti->tcpi_rtt = tcp_ticks_to_us(sc, v); 731 732 v = GET_TCB_FIELD(tcb, T_RTTVAR); 733 ti->tcpi_rttvar = tcp_ticks_to_us(sc, v); 734 735 ti->tcpi_snd_ssthresh = GET_TCB_FIELD(tcb, SND_SSTHRESH); 736 ti->tcpi_snd_cwnd = GET_TCB_FIELD(tcb, SND_CWND); 737 ti->tcpi_rcv_nxt = GET_TCB_FIELD(tcb, RCV_NXT); 738 739 v = GET_TCB_FIELD(tcb, TX_MAX); 740 ti->tcpi_snd_nxt = v - GET_TCB_FIELD(tcb, SND_NXT_RAW); 741 742 /* Receive window being advertised by us. */ 743 ti->tcpi_rcv_wscale = GET_TCB_FIELD(tcb, SND_SCALE); /* Yes, SND. */ 744 ti->tcpi_rcv_space = GET_TCB_FIELD(tcb, RCV_WND); 745 746 /* Send window */ 747 ti->tcpi_snd_wscale = GET_TCB_FIELD(tcb, RCV_SCALE); /* Yes, RCV. */ 748 ti->tcpi_snd_wnd = GET_TCB_FIELD(tcb, RCV_ADV); 749 if (get_tcb_tflags(tcb) & V_TF_RECV_SCALE(1)) 750 ti->tcpi_snd_wnd <<= ti->tcpi_snd_wscale; 751 else 752 ti->tcpi_snd_wscale = 0; 753 754 } 755 756 static void 757 fill_tcp_info_from_history(struct adapter *sc, struct tcb_histent *te, 758 struct tcp_info *ti) 759 { 760 761 fill_tcp_info_from_tcb(sc, te->te_tcb, ti); 762 } 763 764 /* 765 * Reads the TCB for the given tid using a memory window and copies it to 'buf' 766 * in the same format as CPL_GET_TCB_RPL. 767 */ 768 static void 769 read_tcb_using_memwin(struct adapter *sc, u_int tid, uint64_t *buf) 770 { 771 int i, j, k, rc; 772 uint32_t addr; 773 u_char *tcb, tmp; 774 775 MPASS(tid >= sc->tids.tid_base); 776 MPASS(tid - sc->tids.tid_base < sc->tids.ntids); 777 778 addr = t4_read_reg(sc, A_TP_CMM_TCB_BASE) + tid * TCB_SIZE; 779 rc = read_via_memwin(sc, 2, addr, (uint32_t *)buf, TCB_SIZE); 780 if (rc != 0) 781 return; 782 783 tcb = (u_char *)buf; 784 for (i = 0, j = TCB_SIZE - 16; i < j; i += 16, j -= 16) { 785 for (k = 0; k < 16; k++) { 786 tmp = tcb[i + k]; 787 tcb[i + k] = tcb[j + k]; 788 tcb[j + k] = tmp; 789 } 790 } 791 } 792 793 static void 794 fill_tcp_info(struct adapter *sc, u_int tid, struct tcp_info *ti) 795 { 796 uint64_t tcb[TCB_SIZE / sizeof(uint64_t)]; 797 struct tcb_histent *te; 798 799 ti->tcpi_toe_tid = tid; 800 te = lookup_tcb_histent(sc, tid, false); 801 if (te != NULL) { 802 fill_tcp_info_from_history(sc, te, ti); 803 release_tcb_histent(te); 804 } else { 805 if (!(sc->debug_flags & DF_DISABLE_TCB_CACHE)) { 806 /* XXX: tell firmware to flush TCB cache. */ 807 } 808 read_tcb_using_memwin(sc, tid, tcb); 809 fill_tcp_info_from_tcb(sc, tcb, ti); 810 } 811 } 812 813 /* 814 * Called by the kernel to allow the TOE driver to "refine" values filled up in 815 * the tcp_info for an offloaded connection. 816 */ 817 static void 818 t4_tcp_info(struct toedev *tod, struct tcpcb *tp, struct tcp_info *ti) 819 { 820 struct adapter *sc = tod->tod_softc; 821 struct toepcb *toep = tp->t_toe; 822 823 INP_WLOCK_ASSERT(tp->t_inpcb); 824 MPASS(ti != NULL); 825 826 fill_tcp_info(sc, toep->tid, ti); 827 } 828 829 #ifdef KERN_TLS 830 static int 831 t4_alloc_tls_session(struct toedev *tod, struct tcpcb *tp, 832 struct ktls_session *tls, int direction) 833 { 834 struct toepcb *toep = tp->t_toe; 835 836 INP_WLOCK_ASSERT(tp->t_inpcb); 837 MPASS(tls != NULL); 838 839 return (tls_alloc_ktls(toep, tls, direction)); 840 } 841 #endif 842 843 /* SET_TCB_FIELD sent as a ULP command looks like this */ 844 #define LEN__SET_TCB_FIELD_ULP (sizeof(struct ulp_txpkt) + \ 845 sizeof(struct ulptx_idata) + sizeof(struct cpl_set_tcb_field_core)) 846 847 static void * 848 mk_set_tcb_field_ulp(struct ulp_txpkt *ulpmc, uint64_t word, uint64_t mask, 849 uint64_t val, uint32_t tid) 850 { 851 struct ulptx_idata *ulpsc; 852 struct cpl_set_tcb_field_core *req; 853 854 ulpmc->cmd_dest = htonl(V_ULPTX_CMD(ULP_TX_PKT) | V_ULP_TXPKT_DEST(0)); 855 ulpmc->len = htobe32(howmany(LEN__SET_TCB_FIELD_ULP, 16)); 856 857 ulpsc = (struct ulptx_idata *)(ulpmc + 1); 858 ulpsc->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_IMM)); 859 ulpsc->len = htobe32(sizeof(*req)); 860 861 req = (struct cpl_set_tcb_field_core *)(ulpsc + 1); 862 OPCODE_TID(req) = htobe32(MK_OPCODE_TID(CPL_SET_TCB_FIELD, tid)); 863 req->reply_ctrl = htobe16(V_NO_REPLY(1)); 864 req->word_cookie = htobe16(V_WORD(word) | V_COOKIE(0)); 865 req->mask = htobe64(mask); 866 req->val = htobe64(val); 867 868 ulpsc = (struct ulptx_idata *)(req + 1); 869 if (LEN__SET_TCB_FIELD_ULP % 16) { 870 ulpsc->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_NOOP)); 871 ulpsc->len = htobe32(0); 872 return (ulpsc + 1); 873 } 874 return (ulpsc); 875 } 876 877 static void 878 send_mss_flowc_wr(struct adapter *sc, struct toepcb *toep) 879 { 880 struct wrq_cookie cookie; 881 struct fw_flowc_wr *flowc; 882 struct ofld_tx_sdesc *txsd; 883 const int flowclen = sizeof(*flowc) + sizeof(struct fw_flowc_mnemval); 884 const int flowclen16 = howmany(flowclen, 16); 885 886 if (toep->tx_credits < flowclen16 || toep->txsd_avail == 0) { 887 CH_ERR(sc, "%s: tid %u out of tx credits (%d, %d).\n", __func__, 888 toep->tid, toep->tx_credits, toep->txsd_avail); 889 return; 890 } 891 892 flowc = start_wrq_wr(&toep->ofld_txq->wrq, flowclen16, &cookie); 893 if (__predict_false(flowc == NULL)) { 894 CH_ERR(sc, "ENOMEM in %s for tid %u.\n", __func__, toep->tid); 895 return; 896 } 897 flowc->op_to_nparams = htobe32(V_FW_WR_OP(FW_FLOWC_WR) | 898 V_FW_FLOWC_WR_NPARAMS(1)); 899 flowc->flowid_len16 = htonl(V_FW_WR_LEN16(flowclen16) | 900 V_FW_WR_FLOWID(toep->tid)); 901 flowc->mnemval[0].mnemonic = FW_FLOWC_MNEM_MSS; 902 flowc->mnemval[0].val = htobe32(toep->params.emss); 903 904 txsd = &toep->txsd[toep->txsd_pidx]; 905 txsd->tx_credits = flowclen16; 906 txsd->plen = 0; 907 toep->tx_credits -= txsd->tx_credits; 908 if (__predict_false(++toep->txsd_pidx == toep->txsd_total)) 909 toep->txsd_pidx = 0; 910 toep->txsd_avail--; 911 commit_wrq_wr(&toep->ofld_txq->wrq, flowc, &cookie); 912 } 913 914 static void 915 t4_pmtu_update(struct toedev *tod, struct tcpcb *tp, tcp_seq seq, int mtu) 916 { 917 struct work_request_hdr *wrh; 918 struct ulp_txpkt *ulpmc; 919 int idx, len; 920 struct wrq_cookie cookie; 921 struct inpcb *inp = tp->t_inpcb; 922 struct toepcb *toep = tp->t_toe; 923 struct adapter *sc = td_adapter(toep->td); 924 unsigned short *mtus = &sc->params.mtus[0]; 925 926 INP_WLOCK_ASSERT(inp); 927 MPASS(mtu > 0); /* kernel is supposed to provide something usable. */ 928 929 /* tp->snd_una and snd_max are in host byte order too. */ 930 seq = be32toh(seq); 931 932 CTR6(KTR_CXGBE, "%s: tid %d, seq 0x%08x, mtu %u, mtu_idx %u (%d)", 933 __func__, toep->tid, seq, mtu, toep->params.mtu_idx, 934 mtus[toep->params.mtu_idx]); 935 936 if (ulp_mode(toep) == ULP_MODE_NONE && /* XXX: Read TCB otherwise? */ 937 (SEQ_LT(seq, tp->snd_una) || SEQ_GEQ(seq, tp->snd_max))) { 938 CTR5(KTR_CXGBE, 939 "%s: tid %d, seq 0x%08x not in range [0x%08x, 0x%08x).", 940 __func__, toep->tid, seq, tp->snd_una, tp->snd_max); 941 return; 942 } 943 944 /* Find the best mtu_idx for the suggested MTU. */ 945 for (idx = 0; idx < NMTUS - 1 && mtus[idx + 1] <= mtu; idx++) 946 continue; 947 if (idx >= toep->params.mtu_idx) 948 return; /* Never increase the PMTU (just like the kernel). */ 949 950 /* 951 * We'll send a compound work request with 2 SET_TCB_FIELDs -- the first 952 * one updates the mtu_idx and the second one triggers a retransmit. 953 */ 954 len = sizeof(*wrh) + 2 * roundup2(LEN__SET_TCB_FIELD_ULP, 16); 955 wrh = start_wrq_wr(toep->ctrlq, howmany(len, 16), &cookie); 956 if (wrh == NULL) { 957 CH_ERR(sc, "failed to change mtu_idx of tid %d (%u -> %u).\n", 958 toep->tid, toep->params.mtu_idx, idx); 959 return; 960 } 961 INIT_ULPTX_WRH(wrh, len, 1, 0); /* atomic */ 962 ulpmc = (struct ulp_txpkt *)(wrh + 1); 963 ulpmc = mk_set_tcb_field_ulp(ulpmc, W_TCB_T_MAXSEG, 964 V_TCB_T_MAXSEG(M_TCB_T_MAXSEG), V_TCB_T_MAXSEG(idx), toep->tid); 965 ulpmc = mk_set_tcb_field_ulp(ulpmc, W_TCB_TIMESTAMP, 966 V_TCB_TIMESTAMP(0x7FFFFULL << 11), 0, toep->tid); 967 commit_wrq_wr(toep->ctrlq, wrh, &cookie); 968 969 /* Update the software toepcb and tcpcb. */ 970 toep->params.mtu_idx = idx; 971 tp->t_maxseg = mtus[toep->params.mtu_idx]; 972 if (inp->inp_inc.inc_flags & INC_ISIPV6) 973 tp->t_maxseg -= sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 974 else 975 tp->t_maxseg -= sizeof(struct ip) + sizeof(struct tcphdr); 976 toep->params.emss = tp->t_maxseg; 977 if (tp->t_flags & TF_RCVD_TSTMP) 978 toep->params.emss -= TCPOLEN_TSTAMP_APPA; 979 980 /* Update the firmware flowc. */ 981 send_mss_flowc_wr(sc, toep); 982 983 /* Update the MTU in the kernel's hostcache. */ 984 if (sc->tt.update_hc_on_pmtu_change != 0) { 985 struct in_conninfo inc = {0}; 986 987 inc.inc_fibnum = inp->inp_inc.inc_fibnum; 988 if (inp->inp_inc.inc_flags & INC_ISIPV6) { 989 inc.inc_flags |= INC_ISIPV6; 990 inc.inc6_faddr = inp->inp_inc.inc6_faddr; 991 } else { 992 inc.inc_faddr = inp->inp_inc.inc_faddr; 993 } 994 tcp_hc_updatemtu(&inc, mtu); 995 } 996 997 CTR6(KTR_CXGBE, "%s: tid %d, mtu_idx %u (%u), t_maxseg %u, emss %u", 998 __func__, toep->tid, toep->params.mtu_idx, 999 mtus[toep->params.mtu_idx], tp->t_maxseg, toep->params.emss); 1000 } 1001 1002 /* 1003 * The TOE driver will not receive any more CPLs for the tid associated with the 1004 * toepcb; release the hold on the inpcb. 1005 */ 1006 void 1007 final_cpl_received(struct toepcb *toep) 1008 { 1009 struct inpcb *inp = toep->inp; 1010 bool need_wakeup; 1011 1012 KASSERT(inp != NULL, ("%s: inp is NULL", __func__)); 1013 INP_WLOCK_ASSERT(inp); 1014 KASSERT(toep->flags & TPF_CPL_PENDING, 1015 ("%s: CPL not pending already?", __func__)); 1016 1017 CTR6(KTR_CXGBE, "%s: tid %d, toep %p (0x%x), inp %p (0x%x)", 1018 __func__, toep->tid, toep, toep->flags, inp, inp->inp_flags); 1019 1020 if (ulp_mode(toep) == ULP_MODE_TCPDDP) 1021 release_ddp_resources(toep); 1022 else if (ulp_mode(toep) == ULP_MODE_TLS) 1023 tls_detach(toep); 1024 toep->inp = NULL; 1025 need_wakeup = (toep->flags & TPF_WAITING_FOR_FINAL) != 0; 1026 toep->flags &= ~(TPF_CPL_PENDING | TPF_WAITING_FOR_FINAL); 1027 mbufq_drain(&toep->ulp_pduq); 1028 mbufq_drain(&toep->ulp_pdu_reclaimq); 1029 1030 if (!(toep->flags & TPF_ATTACHED)) 1031 release_offload_resources(toep); 1032 1033 if (!in_pcbrele_wlocked(inp)) 1034 INP_WUNLOCK(inp); 1035 1036 if (need_wakeup) { 1037 struct mtx *lock = mtx_pool_find(mtxpool_sleep, toep); 1038 1039 mtx_lock(lock); 1040 wakeup(toep); 1041 mtx_unlock(lock); 1042 } 1043 } 1044 1045 void 1046 insert_tid(struct adapter *sc, int tid, void *ctx, int ntids) 1047 { 1048 struct tid_info *t = &sc->tids; 1049 1050 MPASS(tid >= t->tid_base); 1051 MPASS(tid - t->tid_base < t->ntids); 1052 1053 t->tid_tab[tid - t->tid_base] = ctx; 1054 atomic_add_int(&t->tids_in_use, ntids); 1055 } 1056 1057 void * 1058 lookup_tid(struct adapter *sc, int tid) 1059 { 1060 struct tid_info *t = &sc->tids; 1061 1062 return (t->tid_tab[tid - t->tid_base]); 1063 } 1064 1065 void 1066 update_tid(struct adapter *sc, int tid, void *ctx) 1067 { 1068 struct tid_info *t = &sc->tids; 1069 1070 t->tid_tab[tid - t->tid_base] = ctx; 1071 } 1072 1073 void 1074 remove_tid(struct adapter *sc, int tid, int ntids) 1075 { 1076 struct tid_info *t = &sc->tids; 1077 1078 t->tid_tab[tid - t->tid_base] = NULL; 1079 atomic_subtract_int(&t->tids_in_use, ntids); 1080 } 1081 1082 /* 1083 * What mtu_idx to use, given a 4-tuple. Note that both s->mss and tcp_mssopt 1084 * have the MSS that we should advertise in our SYN. Advertised MSS doesn't 1085 * account for any TCP options so the effective MSS (only payload, no headers or 1086 * options) could be different. 1087 */ 1088 static int 1089 find_best_mtu_idx(struct adapter *sc, struct in_conninfo *inc, 1090 struct offload_settings *s) 1091 { 1092 unsigned short *mtus = &sc->params.mtus[0]; 1093 int i, mss, mtu; 1094 1095 MPASS(inc != NULL); 1096 1097 mss = s->mss > 0 ? s->mss : tcp_mssopt(inc); 1098 if (inc->inc_flags & INC_ISIPV6) 1099 mtu = mss + sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 1100 else 1101 mtu = mss + sizeof(struct ip) + sizeof(struct tcphdr); 1102 1103 for (i = 0; i < NMTUS - 1 && mtus[i + 1] <= mtu; i++) 1104 continue; 1105 1106 return (i); 1107 } 1108 1109 /* 1110 * Determine the receive window size for a socket. 1111 */ 1112 u_long 1113 select_rcv_wnd(struct socket *so) 1114 { 1115 unsigned long wnd; 1116 1117 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 1118 1119 wnd = sbspace(&so->so_rcv); 1120 if (wnd < MIN_RCV_WND) 1121 wnd = MIN_RCV_WND; 1122 1123 return min(wnd, MAX_RCV_WND); 1124 } 1125 1126 int 1127 select_rcv_wscale(void) 1128 { 1129 int wscale = 0; 1130 unsigned long space = sb_max; 1131 1132 if (space > MAX_RCV_WND) 1133 space = MAX_RCV_WND; 1134 1135 while (wscale < TCP_MAX_WINSHIFT && (TCP_MAXWIN << wscale) < space) 1136 wscale++; 1137 1138 return (wscale); 1139 } 1140 1141 __be64 1142 calc_options0(struct vi_info *vi, struct conn_params *cp) 1143 { 1144 uint64_t opt0 = 0; 1145 1146 opt0 |= F_TCAM_BYPASS; 1147 1148 MPASS(cp->wscale >= 0 && cp->wscale <= M_WND_SCALE); 1149 opt0 |= V_WND_SCALE(cp->wscale); 1150 1151 MPASS(cp->mtu_idx >= 0 && cp->mtu_idx < NMTUS); 1152 opt0 |= V_MSS_IDX(cp->mtu_idx); 1153 1154 MPASS(cp->ulp_mode >= 0 && cp->ulp_mode <= M_ULP_MODE); 1155 opt0 |= V_ULP_MODE(cp->ulp_mode); 1156 1157 MPASS(cp->opt0_bufsize >= 0 && cp->opt0_bufsize <= M_RCV_BUFSIZ); 1158 opt0 |= V_RCV_BUFSIZ(cp->opt0_bufsize); 1159 1160 MPASS(cp->l2t_idx >= 0 && cp->l2t_idx < vi->adapter->vres.l2t.size); 1161 opt0 |= V_L2T_IDX(cp->l2t_idx); 1162 1163 opt0 |= V_SMAC_SEL(vi->smt_idx); 1164 opt0 |= V_TX_CHAN(vi->pi->tx_chan); 1165 1166 MPASS(cp->keepalive == 0 || cp->keepalive == 1); 1167 opt0 |= V_KEEP_ALIVE(cp->keepalive); 1168 1169 MPASS(cp->nagle == 0 || cp->nagle == 1); 1170 opt0 |= V_NAGLE(cp->nagle); 1171 1172 return (htobe64(opt0)); 1173 } 1174 1175 __be32 1176 calc_options2(struct vi_info *vi, struct conn_params *cp) 1177 { 1178 uint32_t opt2 = 0; 1179 struct port_info *pi = vi->pi; 1180 struct adapter *sc = pi->adapter; 1181 1182 /* 1183 * rx flow control, rx coalesce, congestion control, and tx pace are all 1184 * explicitly set by the driver. On T5+ the ISS is also set by the 1185 * driver to the value picked by the kernel. 1186 */ 1187 if (is_t4(sc)) { 1188 opt2 |= F_RX_FC_VALID | F_RX_COALESCE_VALID; 1189 opt2 |= F_CONG_CNTRL_VALID | F_PACE_VALID; 1190 } else { 1191 opt2 |= F_T5_OPT_2_VALID; /* all 4 valid */ 1192 opt2 |= F_T5_ISS; /* ISS provided in CPL */ 1193 } 1194 1195 MPASS(cp->sack == 0 || cp->sack == 1); 1196 opt2 |= V_SACK_EN(cp->sack); 1197 1198 MPASS(cp->tstamp == 0 || cp->tstamp == 1); 1199 opt2 |= V_TSTAMPS_EN(cp->tstamp); 1200 1201 if (cp->wscale > 0) 1202 opt2 |= F_WND_SCALE_EN; 1203 1204 MPASS(cp->ecn == 0 || cp->ecn == 1); 1205 opt2 |= V_CCTRL_ECN(cp->ecn); 1206 1207 /* XXX: F_RX_CHANNEL for multiple rx c-chan support goes here. */ 1208 1209 opt2 |= V_TX_QUEUE(sc->params.tp.tx_modq[pi->tx_chan]); 1210 opt2 |= V_PACE(0); 1211 opt2 |= F_RSS_QUEUE_VALID; 1212 opt2 |= V_RSS_QUEUE(sc->sge.ofld_rxq[cp->rxq_idx].iq.abs_id); 1213 1214 MPASS(cp->cong_algo >= 0 && cp->cong_algo <= M_CONG_CNTRL); 1215 opt2 |= V_CONG_CNTRL(cp->cong_algo); 1216 1217 MPASS(cp->rx_coalesce == 0 || cp->rx_coalesce == 1); 1218 if (cp->rx_coalesce == 1) 1219 opt2 |= V_RX_COALESCE(M_RX_COALESCE); 1220 1221 opt2 |= V_RX_FC_DDP(0) | V_RX_FC_DISABLE(0); 1222 #ifdef USE_DDP_RX_FLOW_CONTROL 1223 if (cp->ulp_mode == ULP_MODE_TCPDDP) 1224 opt2 |= F_RX_FC_DDP; 1225 #endif 1226 1227 return (htobe32(opt2)); 1228 } 1229 1230 uint64_t 1231 select_ntuple(struct vi_info *vi, struct l2t_entry *e) 1232 { 1233 struct adapter *sc = vi->adapter; 1234 struct tp_params *tp = &sc->params.tp; 1235 uint64_t ntuple = 0; 1236 1237 /* 1238 * Initialize each of the fields which we care about which are present 1239 * in the Compressed Filter Tuple. 1240 */ 1241 if (tp->vlan_shift >= 0 && EVL_VLANOFTAG(e->vlan) != CPL_L2T_VLAN_NONE) 1242 ntuple |= (uint64_t)(F_FT_VLAN_VLD | e->vlan) << tp->vlan_shift; 1243 1244 if (tp->port_shift >= 0) 1245 ntuple |= (uint64_t)e->lport << tp->port_shift; 1246 1247 if (tp->protocol_shift >= 0) 1248 ntuple |= (uint64_t)IPPROTO_TCP << tp->protocol_shift; 1249 1250 if (tp->vnic_shift >= 0 && tp->vnic_mode == FW_VNIC_MODE_PF_VF) { 1251 ntuple |= (uint64_t)(V_FT_VNID_ID_VF(vi->vin) | 1252 V_FT_VNID_ID_PF(sc->pf) | V_FT_VNID_ID_VLD(vi->vfvld)) << 1253 tp->vnic_shift; 1254 } 1255 1256 if (is_t4(sc)) 1257 return (htobe32((uint32_t)ntuple)); 1258 else 1259 return (htobe64(V_FILTER_TUPLE(ntuple))); 1260 } 1261 1262 static int 1263 is_tls_sock(struct socket *so, struct adapter *sc) 1264 { 1265 struct inpcb *inp = sotoinpcb(so); 1266 int i, rc; 1267 1268 /* XXX: Eventually add a SO_WANT_TLS socket option perhaps? */ 1269 rc = 0; 1270 ADAPTER_LOCK(sc); 1271 for (i = 0; i < sc->tt.num_tls_rx_ports; i++) { 1272 if (inp->inp_lport == htons(sc->tt.tls_rx_ports[i]) || 1273 inp->inp_fport == htons(sc->tt.tls_rx_ports[i])) { 1274 rc = 1; 1275 break; 1276 } 1277 } 1278 ADAPTER_UNLOCK(sc); 1279 return (rc); 1280 } 1281 1282 /* 1283 * Initialize various connection parameters. 1284 */ 1285 void 1286 init_conn_params(struct vi_info *vi , struct offload_settings *s, 1287 struct in_conninfo *inc, struct socket *so, 1288 const struct tcp_options *tcpopt, int16_t l2t_idx, struct conn_params *cp) 1289 { 1290 struct port_info *pi = vi->pi; 1291 struct adapter *sc = pi->adapter; 1292 struct tom_tunables *tt = &sc->tt; 1293 struct inpcb *inp = sotoinpcb(so); 1294 struct tcpcb *tp = intotcpcb(inp); 1295 u_long wnd; 1296 u_int q_idx; 1297 1298 MPASS(s->offload != 0); 1299 1300 /* Congestion control algorithm */ 1301 if (s->cong_algo >= 0) 1302 cp->cong_algo = s->cong_algo & M_CONG_CNTRL; 1303 else if (sc->tt.cong_algorithm >= 0) 1304 cp->cong_algo = tt->cong_algorithm & M_CONG_CNTRL; 1305 else { 1306 struct cc_algo *cc = CC_ALGO(tp); 1307 1308 if (strcasecmp(cc->name, "reno") == 0) 1309 cp->cong_algo = CONG_ALG_RENO; 1310 else if (strcasecmp(cc->name, "tahoe") == 0) 1311 cp->cong_algo = CONG_ALG_TAHOE; 1312 if (strcasecmp(cc->name, "newreno") == 0) 1313 cp->cong_algo = CONG_ALG_NEWRENO; 1314 if (strcasecmp(cc->name, "highspeed") == 0) 1315 cp->cong_algo = CONG_ALG_HIGHSPEED; 1316 else { 1317 /* 1318 * Use newreno in case the algorithm selected by the 1319 * host stack is not supported by the hardware. 1320 */ 1321 cp->cong_algo = CONG_ALG_NEWRENO; 1322 } 1323 } 1324 1325 /* Tx traffic scheduling class. */ 1326 if (s->sched_class >= 0 && s->sched_class < sc->params.nsched_cls) 1327 cp->tc_idx = s->sched_class; 1328 else 1329 cp->tc_idx = -1; 1330 1331 /* Nagle's algorithm. */ 1332 if (s->nagle >= 0) 1333 cp->nagle = s->nagle > 0 ? 1 : 0; 1334 else 1335 cp->nagle = tp->t_flags & TF_NODELAY ? 0 : 1; 1336 1337 /* TCP Keepalive. */ 1338 if (V_tcp_always_keepalive || so_options_get(so) & SO_KEEPALIVE) 1339 cp->keepalive = 1; 1340 else 1341 cp->keepalive = 0; 1342 1343 /* Optimization that's specific to T5 @ 40G. */ 1344 if (tt->tx_align >= 0) 1345 cp->tx_align = tt->tx_align > 0 ? 1 : 0; 1346 else if (chip_id(sc) == CHELSIO_T5 && 1347 (port_top_speed(pi) > 10 || sc->params.nports > 2)) 1348 cp->tx_align = 1; 1349 else 1350 cp->tx_align = 0; 1351 1352 /* ULP mode. */ 1353 if (can_tls_offload(sc) && 1354 (s->tls > 0 || (s->tls < 0 && is_tls_sock(so, sc)))) 1355 cp->ulp_mode = ULP_MODE_TLS; 1356 else if (s->ddp > 0 || 1357 (s->ddp < 0 && sc->tt.ddp && (so_options_get(so) & SO_NO_DDP) == 0)) 1358 cp->ulp_mode = ULP_MODE_TCPDDP; 1359 else 1360 cp->ulp_mode = ULP_MODE_NONE; 1361 1362 /* Rx coalescing. */ 1363 if (s->rx_coalesce >= 0) 1364 cp->rx_coalesce = s->rx_coalesce > 0 ? 1 : 0; 1365 else if (cp->ulp_mode == ULP_MODE_TLS) 1366 cp->rx_coalesce = 0; 1367 else if (tt->rx_coalesce >= 0) 1368 cp->rx_coalesce = tt->rx_coalesce > 0 ? 1 : 0; 1369 else 1370 cp->rx_coalesce = 1; /* default */ 1371 1372 /* 1373 * Index in the PMTU table. This controls the MSS that we announce in 1374 * our SYN initially, but after ESTABLISHED it controls the MSS that we 1375 * use to send data. 1376 */ 1377 cp->mtu_idx = find_best_mtu_idx(sc, inc, s); 1378 1379 /* Tx queue for this connection. */ 1380 if (s->txq == QUEUE_RANDOM) 1381 q_idx = arc4random(); 1382 else if (s->txq == QUEUE_ROUNDROBIN) 1383 q_idx = atomic_fetchadd_int(&vi->txq_rr, 1); 1384 else 1385 q_idx = s->txq; 1386 cp->txq_idx = vi->first_ofld_txq + q_idx % vi->nofldtxq; 1387 1388 /* Rx queue for this connection. */ 1389 if (s->rxq == QUEUE_RANDOM) 1390 q_idx = arc4random(); 1391 else if (s->rxq == QUEUE_ROUNDROBIN) 1392 q_idx = atomic_fetchadd_int(&vi->rxq_rr, 1); 1393 else 1394 q_idx = s->rxq; 1395 cp->rxq_idx = vi->first_ofld_rxq + q_idx % vi->nofldrxq; 1396 1397 if (SOLISTENING(so)) { 1398 /* Passive open */ 1399 MPASS(tcpopt != NULL); 1400 1401 /* TCP timestamp option */ 1402 if (tcpopt->tstamp && 1403 (s->tstamp > 0 || (s->tstamp < 0 && V_tcp_do_rfc1323))) 1404 cp->tstamp = 1; 1405 else 1406 cp->tstamp = 0; 1407 1408 /* SACK */ 1409 if (tcpopt->sack && 1410 (s->sack > 0 || (s->sack < 0 && V_tcp_do_sack))) 1411 cp->sack = 1; 1412 else 1413 cp->sack = 0; 1414 1415 /* Receive window scaling. */ 1416 if (tcpopt->wsf > 0 && tcpopt->wsf < 15 && V_tcp_do_rfc1323) 1417 cp->wscale = select_rcv_wscale(); 1418 else 1419 cp->wscale = 0; 1420 1421 /* ECN */ 1422 if (tcpopt->ecn && /* XXX: review. */ 1423 (s->ecn > 0 || (s->ecn < 0 && V_tcp_do_ecn))) 1424 cp->ecn = 1; 1425 else 1426 cp->ecn = 0; 1427 1428 wnd = max(so->sol_sbrcv_hiwat, MIN_RCV_WND); 1429 cp->opt0_bufsize = min(wnd >> 10, M_RCV_BUFSIZ); 1430 1431 if (tt->sndbuf > 0) 1432 cp->sndbuf = tt->sndbuf; 1433 else if (so->sol_sbsnd_flags & SB_AUTOSIZE && 1434 V_tcp_do_autosndbuf) 1435 cp->sndbuf = 256 * 1024; 1436 else 1437 cp->sndbuf = so->sol_sbsnd_hiwat; 1438 } else { 1439 /* Active open */ 1440 1441 /* TCP timestamp option */ 1442 if (s->tstamp > 0 || 1443 (s->tstamp < 0 && (tp->t_flags & TF_REQ_TSTMP))) 1444 cp->tstamp = 1; 1445 else 1446 cp->tstamp = 0; 1447 1448 /* SACK */ 1449 if (s->sack > 0 || 1450 (s->sack < 0 && (tp->t_flags & TF_SACK_PERMIT))) 1451 cp->sack = 1; 1452 else 1453 cp->sack = 0; 1454 1455 /* Receive window scaling */ 1456 if (tp->t_flags & TF_REQ_SCALE) 1457 cp->wscale = select_rcv_wscale(); 1458 else 1459 cp->wscale = 0; 1460 1461 /* ECN */ 1462 if (s->ecn > 0 || (s->ecn < 0 && V_tcp_do_ecn == 1)) 1463 cp->ecn = 1; 1464 else 1465 cp->ecn = 0; 1466 1467 SOCKBUF_LOCK(&so->so_rcv); 1468 wnd = max(select_rcv_wnd(so), MIN_RCV_WND); 1469 SOCKBUF_UNLOCK(&so->so_rcv); 1470 cp->opt0_bufsize = min(wnd >> 10, M_RCV_BUFSIZ); 1471 1472 if (tt->sndbuf > 0) 1473 cp->sndbuf = tt->sndbuf; 1474 else { 1475 SOCKBUF_LOCK(&so->so_snd); 1476 if (so->so_snd.sb_flags & SB_AUTOSIZE && 1477 V_tcp_do_autosndbuf) 1478 cp->sndbuf = 256 * 1024; 1479 else 1480 cp->sndbuf = so->so_snd.sb_hiwat; 1481 SOCKBUF_UNLOCK(&so->so_snd); 1482 } 1483 } 1484 1485 cp->l2t_idx = l2t_idx; 1486 1487 /* This will be initialized on ESTABLISHED. */ 1488 cp->emss = 0; 1489 } 1490 1491 int 1492 negative_advice(int status) 1493 { 1494 1495 return (status == CPL_ERR_RTX_NEG_ADVICE || 1496 status == CPL_ERR_PERSIST_NEG_ADVICE || 1497 status == CPL_ERR_KEEPALV_NEG_ADVICE); 1498 } 1499 1500 static int 1501 alloc_tid_tab(struct tid_info *t, int flags) 1502 { 1503 1504 MPASS(t->ntids > 0); 1505 MPASS(t->tid_tab == NULL); 1506 1507 t->tid_tab = malloc(t->ntids * sizeof(*t->tid_tab), M_CXGBE, 1508 M_ZERO | flags); 1509 if (t->tid_tab == NULL) 1510 return (ENOMEM); 1511 atomic_store_rel_int(&t->tids_in_use, 0); 1512 1513 return (0); 1514 } 1515 1516 static void 1517 free_tid_tab(struct tid_info *t) 1518 { 1519 1520 KASSERT(t->tids_in_use == 0, 1521 ("%s: %d tids still in use.", __func__, t->tids_in_use)); 1522 1523 free(t->tid_tab, M_CXGBE); 1524 t->tid_tab = NULL; 1525 } 1526 1527 static int 1528 alloc_stid_tab(struct tid_info *t, int flags) 1529 { 1530 1531 MPASS(t->nstids > 0); 1532 MPASS(t->stid_tab == NULL); 1533 1534 t->stid_tab = malloc(t->nstids * sizeof(*t->stid_tab), M_CXGBE, 1535 M_ZERO | flags); 1536 if (t->stid_tab == NULL) 1537 return (ENOMEM); 1538 mtx_init(&t->stid_lock, "stid lock", NULL, MTX_DEF); 1539 t->stids_in_use = 0; 1540 TAILQ_INIT(&t->stids); 1541 t->nstids_free_head = t->nstids; 1542 1543 return (0); 1544 } 1545 1546 static void 1547 free_stid_tab(struct tid_info *t) 1548 { 1549 1550 KASSERT(t->stids_in_use == 0, 1551 ("%s: %d tids still in use.", __func__, t->stids_in_use)); 1552 1553 if (mtx_initialized(&t->stid_lock)) 1554 mtx_destroy(&t->stid_lock); 1555 free(t->stid_tab, M_CXGBE); 1556 t->stid_tab = NULL; 1557 } 1558 1559 static void 1560 free_tid_tabs(struct tid_info *t) 1561 { 1562 1563 free_tid_tab(t); 1564 free_stid_tab(t); 1565 } 1566 1567 static int 1568 alloc_tid_tabs(struct tid_info *t) 1569 { 1570 int rc; 1571 1572 rc = alloc_tid_tab(t, M_NOWAIT); 1573 if (rc != 0) 1574 goto failed; 1575 1576 rc = alloc_stid_tab(t, M_NOWAIT); 1577 if (rc != 0) 1578 goto failed; 1579 1580 return (0); 1581 failed: 1582 free_tid_tabs(t); 1583 return (rc); 1584 } 1585 1586 static inline void 1587 alloc_tcb_history(struct adapter *sc, struct tom_data *td) 1588 { 1589 1590 if (sc->tids.ntids == 0 || sc->tids.ntids > 1024) 1591 return; 1592 rw_init(&td->tcb_history_lock, "TCB history"); 1593 td->tcb_history = malloc(sc->tids.ntids * sizeof(*td->tcb_history), 1594 M_CXGBE, M_ZERO | M_NOWAIT); 1595 td->dupack_threshold = G_DUPACKTHRESH(t4_read_reg(sc, A_TP_PARA_REG0)); 1596 } 1597 1598 static inline void 1599 free_tcb_history(struct adapter *sc, struct tom_data *td) 1600 { 1601 #ifdef INVARIANTS 1602 int i; 1603 1604 if (td->tcb_history != NULL) { 1605 for (i = 0; i < sc->tids.ntids; i++) { 1606 MPASS(td->tcb_history[i] == NULL); 1607 } 1608 } 1609 #endif 1610 free(td->tcb_history, M_CXGBE); 1611 if (rw_initialized(&td->tcb_history_lock)) 1612 rw_destroy(&td->tcb_history_lock); 1613 } 1614 1615 static void 1616 free_tom_data(struct adapter *sc, struct tom_data *td) 1617 { 1618 1619 ASSERT_SYNCHRONIZED_OP(sc); 1620 1621 KASSERT(TAILQ_EMPTY(&td->toep_list), 1622 ("%s: TOE PCB list is not empty.", __func__)); 1623 KASSERT(td->lctx_count == 0, 1624 ("%s: lctx hash table is not empty.", __func__)); 1625 1626 t4_free_ppod_region(&td->pr); 1627 1628 if (td->listen_mask != 0) 1629 hashdestroy(td->listen_hash, M_CXGBE, td->listen_mask); 1630 1631 if (mtx_initialized(&td->unsent_wr_lock)) 1632 mtx_destroy(&td->unsent_wr_lock); 1633 if (mtx_initialized(&td->lctx_hash_lock)) 1634 mtx_destroy(&td->lctx_hash_lock); 1635 if (mtx_initialized(&td->toep_list_lock)) 1636 mtx_destroy(&td->toep_list_lock); 1637 1638 free_tcb_history(sc, td); 1639 free_tid_tabs(&sc->tids); 1640 free(td, M_CXGBE); 1641 } 1642 1643 static char * 1644 prepare_pkt(int open_type, uint16_t vtag, struct inpcb *inp, int *pktlen, 1645 int *buflen) 1646 { 1647 char *pkt; 1648 struct tcphdr *th; 1649 int ipv6, len; 1650 const int maxlen = 1651 max(sizeof(struct ether_header), sizeof(struct ether_vlan_header)) + 1652 max(sizeof(struct ip), sizeof(struct ip6_hdr)) + 1653 sizeof(struct tcphdr); 1654 1655 MPASS(open_type == OPEN_TYPE_ACTIVE || open_type == OPEN_TYPE_LISTEN); 1656 1657 pkt = malloc(maxlen, M_CXGBE, M_ZERO | M_NOWAIT); 1658 if (pkt == NULL) 1659 return (NULL); 1660 1661 ipv6 = inp->inp_vflag & INP_IPV6; 1662 len = 0; 1663 1664 if (EVL_VLANOFTAG(vtag) == 0xfff) { 1665 struct ether_header *eh = (void *)pkt; 1666 1667 if (ipv6) 1668 eh->ether_type = htons(ETHERTYPE_IPV6); 1669 else 1670 eh->ether_type = htons(ETHERTYPE_IP); 1671 1672 len += sizeof(*eh); 1673 } else { 1674 struct ether_vlan_header *evh = (void *)pkt; 1675 1676 evh->evl_encap_proto = htons(ETHERTYPE_VLAN); 1677 evh->evl_tag = htons(vtag); 1678 if (ipv6) 1679 evh->evl_proto = htons(ETHERTYPE_IPV6); 1680 else 1681 evh->evl_proto = htons(ETHERTYPE_IP); 1682 1683 len += sizeof(*evh); 1684 } 1685 1686 if (ipv6) { 1687 struct ip6_hdr *ip6 = (void *)&pkt[len]; 1688 1689 ip6->ip6_vfc = IPV6_VERSION; 1690 ip6->ip6_plen = htons(sizeof(struct tcphdr)); 1691 ip6->ip6_nxt = IPPROTO_TCP; 1692 if (open_type == OPEN_TYPE_ACTIVE) { 1693 ip6->ip6_src = inp->in6p_laddr; 1694 ip6->ip6_dst = inp->in6p_faddr; 1695 } else if (open_type == OPEN_TYPE_LISTEN) { 1696 ip6->ip6_src = inp->in6p_laddr; 1697 ip6->ip6_dst = ip6->ip6_src; 1698 } 1699 1700 len += sizeof(*ip6); 1701 } else { 1702 struct ip *ip = (void *)&pkt[len]; 1703 1704 ip->ip_v = IPVERSION; 1705 ip->ip_hl = sizeof(*ip) >> 2; 1706 ip->ip_tos = inp->inp_ip_tos; 1707 ip->ip_len = htons(sizeof(struct ip) + sizeof(struct tcphdr)); 1708 ip->ip_ttl = inp->inp_ip_ttl; 1709 ip->ip_p = IPPROTO_TCP; 1710 if (open_type == OPEN_TYPE_ACTIVE) { 1711 ip->ip_src = inp->inp_laddr; 1712 ip->ip_dst = inp->inp_faddr; 1713 } else if (open_type == OPEN_TYPE_LISTEN) { 1714 ip->ip_src = inp->inp_laddr; 1715 ip->ip_dst = ip->ip_src; 1716 } 1717 1718 len += sizeof(*ip); 1719 } 1720 1721 th = (void *)&pkt[len]; 1722 if (open_type == OPEN_TYPE_ACTIVE) { 1723 th->th_sport = inp->inp_lport; /* network byte order already */ 1724 th->th_dport = inp->inp_fport; /* ditto */ 1725 } else if (open_type == OPEN_TYPE_LISTEN) { 1726 th->th_sport = inp->inp_lport; /* network byte order already */ 1727 th->th_dport = th->th_sport; 1728 } 1729 len += sizeof(th); 1730 1731 *pktlen = *buflen = len; 1732 return (pkt); 1733 } 1734 1735 const struct offload_settings * 1736 lookup_offload_policy(struct adapter *sc, int open_type, struct mbuf *m, 1737 uint16_t vtag, struct inpcb *inp) 1738 { 1739 const struct t4_offload_policy *op; 1740 char *pkt; 1741 struct offload_rule *r; 1742 int i, matched, pktlen, buflen; 1743 static const struct offload_settings allow_offloading_settings = { 1744 .offload = 1, 1745 .rx_coalesce = -1, 1746 .cong_algo = -1, 1747 .sched_class = -1, 1748 .tstamp = -1, 1749 .sack = -1, 1750 .nagle = -1, 1751 .ecn = -1, 1752 .ddp = -1, 1753 .tls = -1, 1754 .txq = QUEUE_RANDOM, 1755 .rxq = QUEUE_RANDOM, 1756 .mss = -1, 1757 }; 1758 static const struct offload_settings disallow_offloading_settings = { 1759 .offload = 0, 1760 /* rest is irrelevant when offload is off. */ 1761 }; 1762 1763 rw_assert(&sc->policy_lock, RA_LOCKED); 1764 1765 /* 1766 * If there's no Connection Offloading Policy attached to the device 1767 * then we need to return a default static policy. If 1768 * "cop_managed_offloading" is true, then we need to disallow 1769 * offloading until a COP is attached to the device. Otherwise we 1770 * allow offloading ... 1771 */ 1772 op = sc->policy; 1773 if (op == NULL) { 1774 if (sc->tt.cop_managed_offloading) 1775 return (&disallow_offloading_settings); 1776 else 1777 return (&allow_offloading_settings); 1778 } 1779 1780 switch (open_type) { 1781 case OPEN_TYPE_ACTIVE: 1782 case OPEN_TYPE_LISTEN: 1783 pkt = prepare_pkt(open_type, vtag, inp, &pktlen, &buflen); 1784 break; 1785 case OPEN_TYPE_PASSIVE: 1786 MPASS(m != NULL); 1787 pkt = mtod(m, char *); 1788 MPASS(*pkt == CPL_PASS_ACCEPT_REQ); 1789 pkt += sizeof(struct cpl_pass_accept_req); 1790 pktlen = m->m_pkthdr.len - sizeof(struct cpl_pass_accept_req); 1791 buflen = m->m_len - sizeof(struct cpl_pass_accept_req); 1792 break; 1793 default: 1794 MPASS(0); 1795 return (&disallow_offloading_settings); 1796 } 1797 1798 if (pkt == NULL || pktlen == 0 || buflen == 0) 1799 return (&disallow_offloading_settings); 1800 1801 matched = 0; 1802 r = &op->rule[0]; 1803 for (i = 0; i < op->nrules; i++, r++) { 1804 if (r->open_type != open_type && 1805 r->open_type != OPEN_TYPE_DONTCARE) { 1806 continue; 1807 } 1808 matched = bpf_filter(r->bpf_prog.bf_insns, pkt, pktlen, buflen); 1809 if (matched) 1810 break; 1811 } 1812 1813 if (open_type == OPEN_TYPE_ACTIVE || open_type == OPEN_TYPE_LISTEN) 1814 free(pkt, M_CXGBE); 1815 1816 return (matched ? &r->settings : &disallow_offloading_settings); 1817 } 1818 1819 static void 1820 reclaim_wr_resources(void *arg, int count) 1821 { 1822 struct tom_data *td = arg; 1823 STAILQ_HEAD(, wrqe) twr_list = STAILQ_HEAD_INITIALIZER(twr_list); 1824 struct cpl_act_open_req *cpl; 1825 u_int opcode, atid, tid; 1826 struct wrqe *wr; 1827 struct adapter *sc = td_adapter(td); 1828 1829 mtx_lock(&td->unsent_wr_lock); 1830 STAILQ_SWAP(&td->unsent_wr_list, &twr_list, wrqe); 1831 mtx_unlock(&td->unsent_wr_lock); 1832 1833 while ((wr = STAILQ_FIRST(&twr_list)) != NULL) { 1834 STAILQ_REMOVE_HEAD(&twr_list, link); 1835 1836 cpl = wrtod(wr); 1837 opcode = GET_OPCODE(cpl); 1838 1839 switch (opcode) { 1840 case CPL_ACT_OPEN_REQ: 1841 case CPL_ACT_OPEN_REQ6: 1842 atid = G_TID_TID(be32toh(OPCODE_TID(cpl))); 1843 CTR2(KTR_CXGBE, "%s: atid %u ", __func__, atid); 1844 act_open_failure_cleanup(sc, atid, EHOSTUNREACH); 1845 free(wr, M_CXGBE); 1846 break; 1847 case CPL_PASS_ACCEPT_RPL: 1848 tid = GET_TID(cpl); 1849 CTR2(KTR_CXGBE, "%s: tid %u ", __func__, tid); 1850 synack_failure_cleanup(sc, tid); 1851 free(wr, M_CXGBE); 1852 break; 1853 default: 1854 log(LOG_ERR, "%s: leaked work request %p, wr_len %d, " 1855 "opcode %x\n", __func__, wr, wr->wr_len, opcode); 1856 /* WR not freed here; go look at it with a debugger. */ 1857 } 1858 } 1859 } 1860 1861 /* 1862 * Ground control to Major TOM 1863 * Commencing countdown, engines on 1864 */ 1865 static int 1866 t4_tom_activate(struct adapter *sc) 1867 { 1868 struct tom_data *td; 1869 struct toedev *tod; 1870 struct vi_info *vi; 1871 int i, rc, v; 1872 1873 ASSERT_SYNCHRONIZED_OP(sc); 1874 1875 /* per-adapter softc for TOM */ 1876 td = malloc(sizeof(*td), M_CXGBE, M_ZERO | M_NOWAIT); 1877 if (td == NULL) 1878 return (ENOMEM); 1879 1880 /* List of TOE PCBs and associated lock */ 1881 mtx_init(&td->toep_list_lock, "PCB list lock", NULL, MTX_DEF); 1882 TAILQ_INIT(&td->toep_list); 1883 1884 /* Listen context */ 1885 mtx_init(&td->lctx_hash_lock, "lctx hash lock", NULL, MTX_DEF); 1886 td->listen_hash = hashinit_flags(LISTEN_HASH_SIZE, M_CXGBE, 1887 &td->listen_mask, HASH_NOWAIT); 1888 1889 /* List of WRs for which L2 resolution failed */ 1890 mtx_init(&td->unsent_wr_lock, "Unsent WR list lock", NULL, MTX_DEF); 1891 STAILQ_INIT(&td->unsent_wr_list); 1892 TASK_INIT(&td->reclaim_wr_resources, 0, reclaim_wr_resources, td); 1893 1894 /* TID tables */ 1895 rc = alloc_tid_tabs(&sc->tids); 1896 if (rc != 0) 1897 goto done; 1898 1899 rc = t4_init_ppod_region(&td->pr, &sc->vres.ddp, 1900 t4_read_reg(sc, A_ULP_RX_TDDP_PSZ), "TDDP page pods"); 1901 if (rc != 0) 1902 goto done; 1903 t4_set_reg_field(sc, A_ULP_RX_TDDP_TAGMASK, 1904 V_TDDPTAGMASK(M_TDDPTAGMASK), td->pr.pr_tag_mask); 1905 1906 alloc_tcb_history(sc, td); 1907 1908 /* toedev ops */ 1909 tod = &td->tod; 1910 init_toedev(tod); 1911 tod->tod_softc = sc; 1912 tod->tod_connect = t4_connect; 1913 tod->tod_listen_start = t4_listen_start; 1914 tod->tod_listen_stop = t4_listen_stop; 1915 tod->tod_rcvd = t4_rcvd; 1916 tod->tod_output = t4_tod_output; 1917 tod->tod_send_rst = t4_send_rst; 1918 tod->tod_send_fin = t4_send_fin; 1919 tod->tod_pcb_detach = t4_pcb_detach; 1920 tod->tod_l2_update = t4_l2_update; 1921 tod->tod_syncache_added = t4_syncache_added; 1922 tod->tod_syncache_removed = t4_syncache_removed; 1923 tod->tod_syncache_respond = t4_syncache_respond; 1924 tod->tod_offload_socket = t4_offload_socket; 1925 tod->tod_ctloutput = t4_ctloutput; 1926 tod->tod_tcp_info = t4_tcp_info; 1927 #ifdef KERN_TLS 1928 tod->tod_alloc_tls_session = t4_alloc_tls_session; 1929 #endif 1930 tod->tod_pmtu_update = t4_pmtu_update; 1931 1932 for_each_port(sc, i) { 1933 for_each_vi(sc->port[i], v, vi) { 1934 TOEDEV(vi->ifp) = &td->tod; 1935 } 1936 } 1937 1938 sc->tom_softc = td; 1939 register_toedev(sc->tom_softc); 1940 1941 done: 1942 if (rc != 0) 1943 free_tom_data(sc, td); 1944 return (rc); 1945 } 1946 1947 static int 1948 t4_tom_deactivate(struct adapter *sc) 1949 { 1950 int rc = 0; 1951 struct tom_data *td = sc->tom_softc; 1952 1953 ASSERT_SYNCHRONIZED_OP(sc); 1954 1955 if (td == NULL) 1956 return (0); /* XXX. KASSERT? */ 1957 1958 if (sc->offload_map != 0) 1959 return (EBUSY); /* at least one port has IFCAP_TOE enabled */ 1960 1961 if (uld_active(sc, ULD_IWARP) || uld_active(sc, ULD_ISCSI)) 1962 return (EBUSY); /* both iWARP and iSCSI rely on the TOE. */ 1963 1964 mtx_lock(&td->toep_list_lock); 1965 if (!TAILQ_EMPTY(&td->toep_list)) 1966 rc = EBUSY; 1967 mtx_unlock(&td->toep_list_lock); 1968 1969 mtx_lock(&td->lctx_hash_lock); 1970 if (td->lctx_count > 0) 1971 rc = EBUSY; 1972 mtx_unlock(&td->lctx_hash_lock); 1973 1974 taskqueue_drain(taskqueue_thread, &td->reclaim_wr_resources); 1975 mtx_lock(&td->unsent_wr_lock); 1976 if (!STAILQ_EMPTY(&td->unsent_wr_list)) 1977 rc = EBUSY; 1978 mtx_unlock(&td->unsent_wr_lock); 1979 1980 if (rc == 0) { 1981 unregister_toedev(sc->tom_softc); 1982 free_tom_data(sc, td); 1983 sc->tom_softc = NULL; 1984 } 1985 1986 return (rc); 1987 } 1988 1989 static int 1990 t4_aio_queue_tom(struct socket *so, struct kaiocb *job) 1991 { 1992 struct tcpcb *tp = so_sototcpcb(so); 1993 struct toepcb *toep = tp->t_toe; 1994 int error; 1995 1996 /* 1997 * No lock is needed as TOE sockets never change between 1998 * active and passive. 1999 */ 2000 if (SOLISTENING(so)) 2001 return (EINVAL); 2002 2003 if (ulp_mode(toep) == ULP_MODE_TCPDDP) { 2004 error = t4_aio_queue_ddp(so, job); 2005 if (error != EOPNOTSUPP) 2006 return (error); 2007 } 2008 2009 return (t4_aio_queue_aiotx(so, job)); 2010 } 2011 2012 static int 2013 t4_tom_mod_load(void) 2014 { 2015 /* CPL handlers */ 2016 t4_register_cpl_handler(CPL_GET_TCB_RPL, do_get_tcb_rpl); 2017 t4_register_shared_cpl_handler(CPL_L2T_WRITE_RPL, do_l2t_write_rpl2, 2018 CPL_COOKIE_TOM); 2019 t4_init_connect_cpl_handlers(); 2020 t4_init_listen_cpl_handlers(); 2021 t4_init_cpl_io_handlers(); 2022 2023 t4_ddp_mod_load(); 2024 t4_tls_mod_load(); 2025 2026 bcopy(&tcp_protosw, &toe_protosw, sizeof(toe_protosw)); 2027 toe_protosw.pr_aio_queue = t4_aio_queue_tom; 2028 2029 bcopy(&tcp6_protosw, &toe6_protosw, sizeof(toe6_protosw)); 2030 toe6_protosw.pr_aio_queue = t4_aio_queue_tom; 2031 2032 return (t4_register_uld(&tom_uld_info)); 2033 } 2034 2035 static void 2036 tom_uninit(struct adapter *sc, void *arg __unused) 2037 { 2038 if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4tomun")) 2039 return; 2040 2041 /* Try to free resources (works only if no port has IFCAP_TOE) */ 2042 if (uld_active(sc, ULD_TOM)) 2043 t4_deactivate_uld(sc, ULD_TOM); 2044 2045 end_synchronized_op(sc, 0); 2046 } 2047 2048 static int 2049 t4_tom_mod_unload(void) 2050 { 2051 t4_iterate(tom_uninit, NULL); 2052 2053 if (t4_unregister_uld(&tom_uld_info) == EBUSY) 2054 return (EBUSY); 2055 2056 t4_tls_mod_unload(); 2057 t4_ddp_mod_unload(); 2058 2059 t4_uninit_connect_cpl_handlers(); 2060 t4_uninit_listen_cpl_handlers(); 2061 t4_uninit_cpl_io_handlers(); 2062 t4_register_shared_cpl_handler(CPL_L2T_WRITE_RPL, NULL, CPL_COOKIE_TOM); 2063 t4_register_cpl_handler(CPL_GET_TCB_RPL, NULL); 2064 2065 return (0); 2066 } 2067 #endif /* TCP_OFFLOAD */ 2068 2069 static int 2070 t4_tom_modevent(module_t mod, int cmd, void *arg) 2071 { 2072 int rc = 0; 2073 2074 #ifdef TCP_OFFLOAD 2075 switch (cmd) { 2076 case MOD_LOAD: 2077 rc = t4_tom_mod_load(); 2078 break; 2079 2080 case MOD_UNLOAD: 2081 rc = t4_tom_mod_unload(); 2082 break; 2083 2084 default: 2085 rc = EINVAL; 2086 } 2087 #else 2088 printf("t4_tom: compiled without TCP_OFFLOAD support.\n"); 2089 rc = EOPNOTSUPP; 2090 #endif 2091 return (rc); 2092 } 2093 2094 static moduledata_t t4_tom_moddata= { 2095 "t4_tom", 2096 t4_tom_modevent, 2097 0 2098 }; 2099 2100 MODULE_VERSION(t4_tom, 1); 2101 MODULE_DEPEND(t4_tom, toecore, 1, 1, 1); 2102 MODULE_DEPEND(t4_tom, t4nex, 1, 1, 1); 2103 DECLARE_MODULE(t4_tom, t4_tom_moddata, SI_SUB_EXEC, SI_ORDER_ANY); 2104