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