1 /*- 2 * Copyright (c) 2012 Chelsio Communications, Inc. 3 * All rights reserved. 4 * Written by: Navdeep Parhar <np@FreeBSD.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include "opt_inet.h" 32 #include "opt_inet6.h" 33 34 #include <sys/param.h> 35 #include <sys/types.h> 36 #include <sys/systm.h> 37 #include <sys/kernel.h> 38 #include <sys/ktr.h> 39 #include <sys/module.h> 40 #include <sys/protosw.h> 41 #include <sys/domain.h> 42 #include <sys/socket.h> 43 #include <sys/socketvar.h> 44 #include <sys/taskqueue.h> 45 #include <net/if.h> 46 #include <net/if_var.h> 47 #include <netinet/in.h> 48 #include <netinet/in_pcb.h> 49 #include <netinet/in_var.h> 50 #include <netinet/ip.h> 51 #include <netinet/ip6.h> 52 #include <netinet/tcp_var.h> 53 #include <netinet6/scope6_var.h> 54 #define TCPSTATES 55 #include <netinet/tcp_fsm.h> 56 #include <netinet/toecore.h> 57 58 #ifdef TCP_OFFLOAD 59 #include "common/common.h" 60 #include "common/t4_msg.h" 61 #include "common/t4_regs.h" 62 #include "common/t4_regs_values.h" 63 #include "common/t4_tcb.h" 64 #include "tom/t4_tom_l2t.h" 65 #include "tom/t4_tom.h" 66 67 static struct protosw ddp_protosw; 68 static struct pr_usrreqs ddp_usrreqs; 69 70 static struct protosw ddp6_protosw; 71 static struct pr_usrreqs ddp6_usrreqs; 72 73 /* Module ops */ 74 static int t4_tom_mod_load(void); 75 static int t4_tom_mod_unload(void); 76 static int t4_tom_modevent(module_t, int, void *); 77 78 /* ULD ops and helpers */ 79 static int t4_tom_activate(struct adapter *); 80 static int t4_tom_deactivate(struct adapter *); 81 82 static struct uld_info tom_uld_info = { 83 .uld_id = ULD_TOM, 84 .activate = t4_tom_activate, 85 .deactivate = t4_tom_deactivate, 86 }; 87 88 static void queue_tid_release(struct adapter *, int); 89 static void release_offload_resources(struct toepcb *); 90 static int alloc_tid_tabs(struct tid_info *); 91 static void free_tid_tabs(struct tid_info *); 92 static int add_lip(struct adapter *, struct in6_addr *); 93 static int delete_lip(struct adapter *, struct in6_addr *); 94 static struct clip_entry *search_lip(struct tom_data *, struct in6_addr *); 95 static void init_clip_table(struct adapter *, struct tom_data *); 96 static void update_clip(struct adapter *, void *); 97 static void t4_clip_task(void *, int); 98 static void update_clip_table(struct adapter *, struct tom_data *); 99 static void destroy_clip_table(struct adapter *, struct tom_data *); 100 static void free_tom_data(struct adapter *, struct tom_data *); 101 static void reclaim_wr_resources(void *, int); 102 103 static int in6_ifaddr_gen; 104 static eventhandler_tag ifaddr_evhandler; 105 static struct timeout_task clip_task; 106 107 struct toepcb * 108 alloc_toepcb(struct port_info *pi, int txqid, int rxqid, int flags) 109 { 110 struct adapter *sc = pi->adapter; 111 struct toepcb *toep; 112 int tx_credits, txsd_total, len; 113 114 /* 115 * The firmware counts tx work request credits in units of 16 bytes 116 * each. Reserve room for an ABORT_REQ so the driver never has to worry 117 * about tx credits if it wants to abort a connection. 118 */ 119 tx_credits = sc->params.ofldq_wr_cred; 120 tx_credits -= howmany(sizeof(struct cpl_abort_req), 16); 121 122 /* 123 * Shortest possible tx work request is a fw_ofld_tx_data_wr + 1 byte 124 * immediate payload, and firmware counts tx work request credits in 125 * units of 16 byte. Calculate the maximum work requests possible. 126 */ 127 txsd_total = tx_credits / 128 howmany((sizeof(struct fw_ofld_tx_data_wr) + 1), 16); 129 130 if (txqid < 0) 131 txqid = (arc4random() % pi->nofldtxq) + pi->first_ofld_txq; 132 KASSERT(txqid >= pi->first_ofld_txq && 133 txqid < pi->first_ofld_txq + pi->nofldtxq, 134 ("%s: txqid %d for port %p (first %d, n %d)", __func__, txqid, pi, 135 pi->first_ofld_txq, pi->nofldtxq)); 136 137 if (rxqid < 0) 138 rxqid = (arc4random() % pi->nofldrxq) + pi->first_ofld_rxq; 139 KASSERT(rxqid >= pi->first_ofld_rxq && 140 rxqid < pi->first_ofld_rxq + pi->nofldrxq, 141 ("%s: rxqid %d for port %p (first %d, n %d)", __func__, rxqid, pi, 142 pi->first_ofld_rxq, pi->nofldrxq)); 143 144 len = offsetof(struct toepcb, txsd) + 145 txsd_total * sizeof(struct ofld_tx_sdesc); 146 147 toep = malloc(len, M_CXGBE, M_ZERO | flags); 148 if (toep == NULL) 149 return (NULL); 150 151 toep->td = sc->tom_softc; 152 toep->port = pi; 153 toep->tx_total = tx_credits; 154 toep->tx_credits = tx_credits; 155 toep->ofld_txq = &sc->sge.ofld_txq[txqid]; 156 toep->ofld_rxq = &sc->sge.ofld_rxq[rxqid]; 157 toep->ctrlq = &sc->sge.ctrlq[pi->port_id]; 158 toep->txsd_total = txsd_total; 159 toep->txsd_avail = txsd_total; 160 toep->txsd_pidx = 0; 161 toep->txsd_cidx = 0; 162 163 return (toep); 164 } 165 166 void 167 free_toepcb(struct toepcb *toep) 168 { 169 170 KASSERT(!(toep->flags & TPF_ATTACHED), 171 ("%s: attached to an inpcb", __func__)); 172 KASSERT(!(toep->flags & TPF_CPL_PENDING), 173 ("%s: CPL pending", __func__)); 174 175 free(toep, M_CXGBE); 176 } 177 178 /* 179 * Set up the socket for TCP offload. 180 */ 181 void 182 offload_socket(struct socket *so, struct toepcb *toep) 183 { 184 struct tom_data *td = toep->td; 185 struct inpcb *inp = sotoinpcb(so); 186 struct tcpcb *tp = intotcpcb(inp); 187 struct sockbuf *sb; 188 189 INP_WLOCK_ASSERT(inp); 190 191 /* Update socket */ 192 sb = &so->so_snd; 193 SOCKBUF_LOCK(sb); 194 sb->sb_flags |= SB_NOCOALESCE; 195 SOCKBUF_UNLOCK(sb); 196 sb = &so->so_rcv; 197 SOCKBUF_LOCK(sb); 198 sb->sb_flags |= SB_NOCOALESCE; 199 if (toep->ulp_mode == ULP_MODE_TCPDDP) { 200 if (inp->inp_vflag & INP_IPV6) 201 so->so_proto = &ddp6_protosw; 202 else 203 so->so_proto = &ddp_protosw; 204 } 205 SOCKBUF_UNLOCK(sb); 206 207 /* Update TCP PCB */ 208 tp->tod = &td->tod; 209 tp->t_toe = toep; 210 tp->t_flags |= TF_TOE; 211 212 /* Install an extra hold on inp */ 213 toep->inp = inp; 214 toep->flags |= TPF_ATTACHED; 215 in_pcbref(inp); 216 217 /* Add the TOE PCB to the active list */ 218 mtx_lock(&td->toep_list_lock); 219 TAILQ_INSERT_HEAD(&td->toep_list, toep, link); 220 mtx_unlock(&td->toep_list_lock); 221 } 222 223 /* This is _not_ the normal way to "unoffload" a socket. */ 224 void 225 undo_offload_socket(struct socket *so) 226 { 227 struct inpcb *inp = sotoinpcb(so); 228 struct tcpcb *tp = intotcpcb(inp); 229 struct toepcb *toep = tp->t_toe; 230 struct tom_data *td = toep->td; 231 struct sockbuf *sb; 232 233 INP_WLOCK_ASSERT(inp); 234 235 sb = &so->so_snd; 236 SOCKBUF_LOCK(sb); 237 sb->sb_flags &= ~SB_NOCOALESCE; 238 SOCKBUF_UNLOCK(sb); 239 sb = &so->so_rcv; 240 SOCKBUF_LOCK(sb); 241 sb->sb_flags &= ~SB_NOCOALESCE; 242 SOCKBUF_UNLOCK(sb); 243 244 tp->tod = NULL; 245 tp->t_toe = NULL; 246 tp->t_flags &= ~TF_TOE; 247 248 toep->inp = NULL; 249 toep->flags &= ~TPF_ATTACHED; 250 if (in_pcbrele_wlocked(inp)) 251 panic("%s: inp freed.", __func__); 252 253 mtx_lock(&td->toep_list_lock); 254 TAILQ_REMOVE(&td->toep_list, toep, link); 255 mtx_unlock(&td->toep_list_lock); 256 } 257 258 static void 259 release_offload_resources(struct toepcb *toep) 260 { 261 struct tom_data *td = toep->td; 262 struct adapter *sc = td_adapter(td); 263 int tid = toep->tid; 264 265 KASSERT(!(toep->flags & TPF_CPL_PENDING), 266 ("%s: %p has CPL pending.", __func__, toep)); 267 KASSERT(!(toep->flags & TPF_ATTACHED), 268 ("%s: %p is still attached.", __func__, toep)); 269 270 CTR5(KTR_CXGBE, "%s: toep %p (tid %d, l2te %p, ce %p)", 271 __func__, toep, tid, toep->l2te, toep->ce); 272 273 if (toep->ulp_mode == ULP_MODE_TCPDDP) 274 release_ddp_resources(toep); 275 276 if (toep->l2te) 277 t4_l2t_release(toep->l2te); 278 279 if (tid >= 0) { 280 remove_tid(sc, tid); 281 release_tid(sc, tid, toep->ctrlq); 282 } 283 284 if (toep->ce) 285 release_lip(td, toep->ce); 286 287 mtx_lock(&td->toep_list_lock); 288 TAILQ_REMOVE(&td->toep_list, toep, link); 289 mtx_unlock(&td->toep_list_lock); 290 291 free_toepcb(toep); 292 } 293 294 /* 295 * The kernel is done with the TCP PCB and this is our opportunity to unhook the 296 * toepcb hanging off of it. If the TOE driver is also done with the toepcb (no 297 * pending CPL) then it is time to release all resources tied to the toepcb. 298 * 299 * Also gets called when an offloaded active open fails and the TOM wants the 300 * kernel to take the TCP PCB back. 301 */ 302 static void 303 t4_pcb_detach(struct toedev *tod __unused, struct tcpcb *tp) 304 { 305 #if defined(KTR) || defined(INVARIANTS) 306 struct inpcb *inp = tp->t_inpcb; 307 #endif 308 struct toepcb *toep = tp->t_toe; 309 310 INP_WLOCK_ASSERT(inp); 311 312 KASSERT(toep != NULL, ("%s: toep is NULL", __func__)); 313 KASSERT(toep->flags & TPF_ATTACHED, 314 ("%s: not attached", __func__)); 315 316 #ifdef KTR 317 if (tp->t_state == TCPS_SYN_SENT) { 318 CTR6(KTR_CXGBE, "%s: atid %d, toep %p (0x%x), inp %p (0x%x)", 319 __func__, toep->tid, toep, toep->flags, inp, 320 inp->inp_flags); 321 } else { 322 CTR6(KTR_CXGBE, 323 "t4_pcb_detach: tid %d (%s), toep %p (0x%x), inp %p (0x%x)", 324 toep->tid, tcpstates[tp->t_state], toep, toep->flags, inp, 325 inp->inp_flags); 326 } 327 #endif 328 329 tp->t_toe = NULL; 330 tp->t_flags &= ~TF_TOE; 331 toep->flags &= ~TPF_ATTACHED; 332 333 if (!(toep->flags & TPF_CPL_PENDING)) 334 release_offload_resources(toep); 335 } 336 337 /* 338 * setsockopt handler. 339 */ 340 static void 341 t4_ctloutput(struct toedev *tod, struct tcpcb *tp, int dir, int name) 342 { 343 struct adapter *sc = tod->tod_softc; 344 struct toepcb *toep = tp->t_toe; 345 346 if (dir == SOPT_GET) 347 return; 348 349 CTR4(KTR_CXGBE, "%s: tp %p, dir %u, name %u", __func__, tp, dir, name); 350 351 switch (name) { 352 case TCP_NODELAY: 353 t4_set_tcb_field(sc, toep, 1, W_TCB_T_FLAGS, V_TF_NAGLE(1), 354 V_TF_NAGLE(tp->t_flags & TF_NODELAY ? 0 : 1)); 355 break; 356 default: 357 break; 358 } 359 } 360 361 /* 362 * The TOE driver will not receive any more CPLs for the tid associated with the 363 * toepcb; release the hold on the inpcb. 364 */ 365 void 366 final_cpl_received(struct toepcb *toep) 367 { 368 struct inpcb *inp = toep->inp; 369 370 KASSERT(inp != NULL, ("%s: inp is NULL", __func__)); 371 INP_WLOCK_ASSERT(inp); 372 KASSERT(toep->flags & TPF_CPL_PENDING, 373 ("%s: CPL not pending already?", __func__)); 374 375 CTR6(KTR_CXGBE, "%s: tid %d, toep %p (0x%x), inp %p (0x%x)", 376 __func__, toep->tid, toep, toep->flags, inp, inp->inp_flags); 377 378 toep->inp = NULL; 379 toep->flags &= ~TPF_CPL_PENDING; 380 381 if (!(toep->flags & TPF_ATTACHED)) 382 release_offload_resources(toep); 383 384 if (!in_pcbrele_wlocked(inp)) 385 INP_WUNLOCK(inp); 386 } 387 388 void 389 insert_tid(struct adapter *sc, int tid, void *ctx) 390 { 391 struct tid_info *t = &sc->tids; 392 393 t->tid_tab[tid] = ctx; 394 atomic_add_int(&t->tids_in_use, 1); 395 } 396 397 void * 398 lookup_tid(struct adapter *sc, int tid) 399 { 400 struct tid_info *t = &sc->tids; 401 402 return (t->tid_tab[tid]); 403 } 404 405 void 406 update_tid(struct adapter *sc, int tid, void *ctx) 407 { 408 struct tid_info *t = &sc->tids; 409 410 t->tid_tab[tid] = ctx; 411 } 412 413 void 414 remove_tid(struct adapter *sc, int tid) 415 { 416 struct tid_info *t = &sc->tids; 417 418 t->tid_tab[tid] = NULL; 419 atomic_subtract_int(&t->tids_in_use, 1); 420 } 421 422 void 423 release_tid(struct adapter *sc, int tid, struct sge_wrq *ctrlq) 424 { 425 struct wrqe *wr; 426 struct cpl_tid_release *req; 427 428 wr = alloc_wrqe(sizeof(*req), ctrlq); 429 if (wr == NULL) { 430 queue_tid_release(sc, tid); /* defer */ 431 return; 432 } 433 req = wrtod(wr); 434 435 INIT_TP_WR_MIT_CPL(req, CPL_TID_RELEASE, tid); 436 437 t4_wrq_tx(sc, wr); 438 } 439 440 static void 441 queue_tid_release(struct adapter *sc, int tid) 442 { 443 444 CXGBE_UNIMPLEMENTED("deferred tid release"); 445 } 446 447 /* 448 * What mtu_idx to use, given a 4-tuple and/or an MSS cap 449 */ 450 int 451 find_best_mtu_idx(struct adapter *sc, struct in_conninfo *inc, int pmss) 452 { 453 unsigned short *mtus = &sc->params.mtus[0]; 454 int i, mss, n; 455 456 KASSERT(inc != NULL || pmss > 0, 457 ("%s: at least one of inc/pmss must be specified", __func__)); 458 459 mss = inc ? tcp_mssopt(inc) : pmss; 460 if (pmss > 0 && mss > pmss) 461 mss = pmss; 462 463 if (inc->inc_flags & INC_ISIPV6) 464 n = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 465 else 466 n = sizeof(struct ip) + sizeof(struct tcphdr); 467 468 for (i = 0; i < NMTUS - 1 && mtus[i + 1] <= mss + n; i++) 469 continue; 470 471 return (i); 472 } 473 474 /* 475 * Determine the receive window size for a socket. 476 */ 477 u_long 478 select_rcv_wnd(struct socket *so) 479 { 480 unsigned long wnd; 481 482 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 483 484 wnd = sbspace(&so->so_rcv); 485 if (wnd < MIN_RCV_WND) 486 wnd = MIN_RCV_WND; 487 488 return min(wnd, MAX_RCV_WND); 489 } 490 491 int 492 select_rcv_wscale(void) 493 { 494 int wscale = 0; 495 unsigned long space = sb_max; 496 497 if (space > MAX_RCV_WND) 498 space = MAX_RCV_WND; 499 500 while (wscale < TCP_MAX_WINSHIFT && (TCP_MAXWIN << wscale) < space) 501 wscale++; 502 503 return (wscale); 504 } 505 506 extern int always_keepalive; 507 #define VIID_SMACIDX(v) (((unsigned int)(v) & 0x7f) << 1) 508 509 /* 510 * socket so could be a listening socket too. 511 */ 512 uint64_t 513 calc_opt0(struct socket *so, struct port_info *pi, struct l2t_entry *e, 514 int mtu_idx, int rscale, int rx_credits, int ulp_mode) 515 { 516 uint64_t opt0; 517 518 KASSERT(rx_credits <= M_RCV_BUFSIZ, 519 ("%s: rcv_bufsiz too high", __func__)); 520 521 opt0 = F_TCAM_BYPASS | V_WND_SCALE(rscale) | V_MSS_IDX(mtu_idx) | 522 V_ULP_MODE(ulp_mode) | V_RCV_BUFSIZ(rx_credits); 523 524 if (so != NULL) { 525 struct inpcb *inp = sotoinpcb(so); 526 struct tcpcb *tp = intotcpcb(inp); 527 int keepalive = always_keepalive || 528 so_options_get(so) & SO_KEEPALIVE; 529 530 opt0 |= V_NAGLE((tp->t_flags & TF_NODELAY) == 0); 531 opt0 |= V_KEEP_ALIVE(keepalive != 0); 532 } 533 534 if (e != NULL) 535 opt0 |= V_L2T_IDX(e->idx); 536 537 if (pi != NULL) { 538 opt0 |= V_SMAC_SEL(VIID_SMACIDX(pi->viid)); 539 opt0 |= V_TX_CHAN(pi->tx_chan); 540 } 541 542 return htobe64(opt0); 543 } 544 545 uint64_t 546 select_ntuple(struct port_info *pi, struct l2t_entry *e) 547 { 548 struct adapter *sc = pi->adapter; 549 struct tp_params *tp = &sc->params.tp; 550 uint16_t viid = pi->viid; 551 uint64_t ntuple = 0; 552 553 /* 554 * Initialize each of the fields which we care about which are present 555 * in the Compressed Filter Tuple. 556 */ 557 if (tp->vlan_shift >= 0 && e->vlan != CPL_L2T_VLAN_NONE) 558 ntuple |= (uint64_t)(F_FT_VLAN_VLD | e->vlan) << tp->vlan_shift; 559 560 if (tp->port_shift >= 0) 561 ntuple |= (uint64_t)e->lport << tp->port_shift; 562 563 if (tp->protocol_shift >= 0) 564 ntuple |= (uint64_t)IPPROTO_TCP << tp->protocol_shift; 565 566 if (tp->vnic_shift >= 0) { 567 uint32_t vf = G_FW_VIID_VIN(viid); 568 uint32_t pf = G_FW_VIID_PFN(viid); 569 uint32_t vld = G_FW_VIID_VIVLD(viid); 570 571 ntuple |= (uint64_t)(V_FT_VNID_ID_VF(vf) | V_FT_VNID_ID_PF(pf) | 572 V_FT_VNID_ID_VLD(vld)) << tp->vnic_shift; 573 } 574 575 if (is_t4(sc)) 576 return (htobe32((uint32_t)ntuple)); 577 else 578 return (htobe64(V_FILTER_TUPLE(ntuple))); 579 } 580 581 void 582 set_tcpddp_ulp_mode(struct toepcb *toep) 583 { 584 585 toep->ulp_mode = ULP_MODE_TCPDDP; 586 toep->ddp_flags = DDP_OK; 587 toep->ddp_score = DDP_LOW_SCORE; 588 } 589 590 int 591 negative_advice(int status) 592 { 593 594 return (status == CPL_ERR_RTX_NEG_ADVICE || 595 status == CPL_ERR_PERSIST_NEG_ADVICE || 596 status == CPL_ERR_KEEPALV_NEG_ADVICE); 597 } 598 599 static int 600 alloc_tid_tabs(struct tid_info *t) 601 { 602 size_t size; 603 unsigned int i; 604 605 size = t->ntids * sizeof(*t->tid_tab) + 606 t->natids * sizeof(*t->atid_tab) + 607 t->nstids * sizeof(*t->stid_tab); 608 609 t->tid_tab = malloc(size, M_CXGBE, M_ZERO | M_NOWAIT); 610 if (t->tid_tab == NULL) 611 return (ENOMEM); 612 613 mtx_init(&t->atid_lock, "atid lock", NULL, MTX_DEF); 614 t->atid_tab = (union aopen_entry *)&t->tid_tab[t->ntids]; 615 t->afree = t->atid_tab; 616 t->atids_in_use = 0; 617 for (i = 1; i < t->natids; i++) 618 t->atid_tab[i - 1].next = &t->atid_tab[i]; 619 t->atid_tab[t->natids - 1].next = NULL; 620 621 mtx_init(&t->stid_lock, "stid lock", NULL, MTX_DEF); 622 t->stid_tab = (struct listen_ctx **)&t->atid_tab[t->natids]; 623 t->stids_in_use = 0; 624 TAILQ_INIT(&t->stids); 625 t->nstids_free_head = t->nstids; 626 627 atomic_store_rel_int(&t->tids_in_use, 0); 628 629 return (0); 630 } 631 632 static void 633 free_tid_tabs(struct tid_info *t) 634 { 635 KASSERT(t->tids_in_use == 0, 636 ("%s: %d tids still in use.", __func__, t->tids_in_use)); 637 KASSERT(t->atids_in_use == 0, 638 ("%s: %d atids still in use.", __func__, t->atids_in_use)); 639 KASSERT(t->stids_in_use == 0, 640 ("%s: %d tids still in use.", __func__, t->stids_in_use)); 641 642 free(t->tid_tab, M_CXGBE); 643 t->tid_tab = NULL; 644 645 if (mtx_initialized(&t->atid_lock)) 646 mtx_destroy(&t->atid_lock); 647 if (mtx_initialized(&t->stid_lock)) 648 mtx_destroy(&t->stid_lock); 649 } 650 651 static int 652 add_lip(struct adapter *sc, struct in6_addr *lip) 653 { 654 struct fw_clip_cmd c; 655 656 ASSERT_SYNCHRONIZED_OP(sc); 657 /* mtx_assert(&td->clip_table_lock, MA_OWNED); */ 658 659 memset(&c, 0, sizeof(c)); 660 c.op_to_write = htonl(V_FW_CMD_OP(FW_CLIP_CMD) | F_FW_CMD_REQUEST | 661 F_FW_CMD_WRITE); 662 c.alloc_to_len16 = htonl(F_FW_CLIP_CMD_ALLOC | FW_LEN16(c)); 663 c.ip_hi = *(uint64_t *)&lip->s6_addr[0]; 664 c.ip_lo = *(uint64_t *)&lip->s6_addr[8]; 665 666 return (-t4_wr_mbox_ns(sc, sc->mbox, &c, sizeof(c), &c)); 667 } 668 669 static int 670 delete_lip(struct adapter *sc, struct in6_addr *lip) 671 { 672 struct fw_clip_cmd c; 673 674 ASSERT_SYNCHRONIZED_OP(sc); 675 /* mtx_assert(&td->clip_table_lock, MA_OWNED); */ 676 677 memset(&c, 0, sizeof(c)); 678 c.op_to_write = htonl(V_FW_CMD_OP(FW_CLIP_CMD) | F_FW_CMD_REQUEST | 679 F_FW_CMD_READ); 680 c.alloc_to_len16 = htonl(F_FW_CLIP_CMD_FREE | FW_LEN16(c)); 681 c.ip_hi = *(uint64_t *)&lip->s6_addr[0]; 682 c.ip_lo = *(uint64_t *)&lip->s6_addr[8]; 683 684 return (-t4_wr_mbox_ns(sc, sc->mbox, &c, sizeof(c), &c)); 685 } 686 687 static struct clip_entry * 688 search_lip(struct tom_data *td, struct in6_addr *lip) 689 { 690 struct clip_entry *ce; 691 692 mtx_assert(&td->clip_table_lock, MA_OWNED); 693 694 TAILQ_FOREACH(ce, &td->clip_table, link) { 695 if (IN6_ARE_ADDR_EQUAL(&ce->lip, lip)) 696 return (ce); 697 } 698 699 return (NULL); 700 } 701 702 struct clip_entry * 703 hold_lip(struct tom_data *td, struct in6_addr *lip) 704 { 705 struct clip_entry *ce; 706 707 mtx_lock(&td->clip_table_lock); 708 ce = search_lip(td, lip); 709 if (ce != NULL) 710 ce->refcount++; 711 mtx_unlock(&td->clip_table_lock); 712 713 return (ce); 714 } 715 716 void 717 release_lip(struct tom_data *td, struct clip_entry *ce) 718 { 719 720 mtx_lock(&td->clip_table_lock); 721 KASSERT(search_lip(td, &ce->lip) == ce, 722 ("%s: CLIP entry %p p not in CLIP table.", __func__, ce)); 723 KASSERT(ce->refcount > 0, 724 ("%s: CLIP entry %p has refcount 0", __func__, ce)); 725 --ce->refcount; 726 mtx_unlock(&td->clip_table_lock); 727 } 728 729 static void 730 init_clip_table(struct adapter *sc, struct tom_data *td) 731 { 732 733 ASSERT_SYNCHRONIZED_OP(sc); 734 735 mtx_init(&td->clip_table_lock, "CLIP table lock", NULL, MTX_DEF); 736 TAILQ_INIT(&td->clip_table); 737 td->clip_gen = -1; 738 739 update_clip_table(sc, td); 740 } 741 742 static void 743 update_clip(struct adapter *sc, void *arg __unused) 744 { 745 746 if (begin_synchronized_op(sc, NULL, HOLD_LOCK, "t4tomuc")) 747 return; 748 749 if (sc->flags & TOM_INIT_DONE) 750 update_clip_table(sc, sc->tom_softc); 751 752 end_synchronized_op(sc, LOCK_HELD); 753 } 754 755 static void 756 t4_clip_task(void *arg, int count) 757 { 758 759 t4_iterate(update_clip, NULL); 760 } 761 762 static void 763 update_clip_table(struct adapter *sc, struct tom_data *td) 764 { 765 struct in6_ifaddr *ia; 766 struct in6_addr *lip, tlip; 767 struct clip_head stale; 768 struct clip_entry *ce, *ce_temp; 769 int rc, gen = atomic_load_acq_int(&in6_ifaddr_gen); 770 771 ASSERT_SYNCHRONIZED_OP(sc); 772 773 IN6_IFADDR_RLOCK(); 774 mtx_lock(&td->clip_table_lock); 775 776 if (gen == td->clip_gen) 777 goto done; 778 779 TAILQ_INIT(&stale); 780 TAILQ_CONCAT(&stale, &td->clip_table, link); 781 782 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) { 783 lip = &ia->ia_addr.sin6_addr; 784 785 KASSERT(!IN6_IS_ADDR_MULTICAST(lip), 786 ("%s: mcast address in in6_ifaddr list", __func__)); 787 788 if (IN6_IS_ADDR_LOOPBACK(lip)) 789 continue; 790 if (IN6_IS_SCOPE_EMBED(lip)) { 791 /* Remove the embedded scope */ 792 tlip = *lip; 793 lip = &tlip; 794 in6_clearscope(lip); 795 } 796 /* 797 * XXX: how to weed out the link local address for the loopback 798 * interface? It's fe80::1 usually (always?). 799 */ 800 801 /* 802 * If it's in the main list then we already know it's not stale. 803 */ 804 TAILQ_FOREACH(ce, &td->clip_table, link) { 805 if (IN6_ARE_ADDR_EQUAL(&ce->lip, lip)) 806 goto next; 807 } 808 809 /* 810 * If it's in the stale list we should move it to the main list. 811 */ 812 TAILQ_FOREACH(ce, &stale, link) { 813 if (IN6_ARE_ADDR_EQUAL(&ce->lip, lip)) { 814 TAILQ_REMOVE(&stale, ce, link); 815 TAILQ_INSERT_TAIL(&td->clip_table, ce, link); 816 goto next; 817 } 818 } 819 820 /* A new IP6 address; add it to the CLIP table */ 821 ce = malloc(sizeof(*ce), M_CXGBE, M_NOWAIT); 822 memcpy(&ce->lip, lip, sizeof(ce->lip)); 823 ce->refcount = 0; 824 rc = add_lip(sc, lip); 825 if (rc == 0) 826 TAILQ_INSERT_TAIL(&td->clip_table, ce, link); 827 else { 828 char ip[INET6_ADDRSTRLEN]; 829 830 inet_ntop(AF_INET6, &ce->lip, &ip[0], sizeof(ip)); 831 log(LOG_ERR, "%s: could not add %s (%d)\n", 832 __func__, ip, rc); 833 free(ce, M_CXGBE); 834 } 835 next: 836 continue; 837 } 838 839 /* 840 * Remove stale addresses (those no longer in V_in6_ifaddrhead) that are 841 * no longer referenced by the driver. 842 */ 843 TAILQ_FOREACH_SAFE(ce, &stale, link, ce_temp) { 844 if (ce->refcount == 0) { 845 rc = delete_lip(sc, &ce->lip); 846 if (rc == 0) { 847 TAILQ_REMOVE(&stale, ce, link); 848 free(ce, M_CXGBE); 849 } else { 850 char ip[INET6_ADDRSTRLEN]; 851 852 inet_ntop(AF_INET6, &ce->lip, &ip[0], 853 sizeof(ip)); 854 log(LOG_ERR, "%s: could not delete %s (%d)\n", 855 __func__, ip, rc); 856 } 857 } 858 } 859 /* The ones that are still referenced need to stay in the CLIP table */ 860 TAILQ_CONCAT(&td->clip_table, &stale, link); 861 862 td->clip_gen = gen; 863 done: 864 mtx_unlock(&td->clip_table_lock); 865 IN6_IFADDR_RUNLOCK(); 866 } 867 868 static void 869 destroy_clip_table(struct adapter *sc, struct tom_data *td) 870 { 871 struct clip_entry *ce, *ce_temp; 872 873 if (mtx_initialized(&td->clip_table_lock)) { 874 mtx_lock(&td->clip_table_lock); 875 TAILQ_FOREACH_SAFE(ce, &td->clip_table, link, ce_temp) { 876 KASSERT(ce->refcount == 0, 877 ("%s: CLIP entry %p still in use (%d)", __func__, 878 ce, ce->refcount)); 879 TAILQ_REMOVE(&td->clip_table, ce, link); 880 delete_lip(sc, &ce->lip); 881 free(ce, M_CXGBE); 882 } 883 mtx_unlock(&td->clip_table_lock); 884 mtx_destroy(&td->clip_table_lock); 885 } 886 } 887 888 static void 889 free_tom_data(struct adapter *sc, struct tom_data *td) 890 { 891 892 ASSERT_SYNCHRONIZED_OP(sc); 893 894 KASSERT(TAILQ_EMPTY(&td->toep_list), 895 ("%s: TOE PCB list is not empty.", __func__)); 896 KASSERT(td->lctx_count == 0, 897 ("%s: lctx hash table is not empty.", __func__)); 898 899 t4_uninit_l2t_cpl_handlers(sc); 900 t4_uninit_cpl_io_handlers(sc); 901 t4_uninit_ddp(sc, td); 902 destroy_clip_table(sc, td); 903 904 if (td->listen_mask != 0) 905 hashdestroy(td->listen_hash, M_CXGBE, td->listen_mask); 906 907 if (mtx_initialized(&td->unsent_wr_lock)) 908 mtx_destroy(&td->unsent_wr_lock); 909 if (mtx_initialized(&td->lctx_hash_lock)) 910 mtx_destroy(&td->lctx_hash_lock); 911 if (mtx_initialized(&td->toep_list_lock)) 912 mtx_destroy(&td->toep_list_lock); 913 914 free_tid_tabs(&sc->tids); 915 free(td, M_CXGBE); 916 } 917 918 static void 919 reclaim_wr_resources(void *arg, int count) 920 { 921 struct tom_data *td = arg; 922 STAILQ_HEAD(, wrqe) twr_list = STAILQ_HEAD_INITIALIZER(twr_list); 923 struct cpl_act_open_req *cpl; 924 u_int opcode, atid; 925 struct wrqe *wr; 926 struct adapter *sc; 927 928 mtx_lock(&td->unsent_wr_lock); 929 STAILQ_SWAP(&td->unsent_wr_list, &twr_list, wrqe); 930 mtx_unlock(&td->unsent_wr_lock); 931 932 while ((wr = STAILQ_FIRST(&twr_list)) != NULL) { 933 STAILQ_REMOVE_HEAD(&twr_list, link); 934 935 cpl = wrtod(wr); 936 opcode = GET_OPCODE(cpl); 937 938 switch (opcode) { 939 case CPL_ACT_OPEN_REQ: 940 case CPL_ACT_OPEN_REQ6: 941 atid = G_TID_TID(be32toh(OPCODE_TID(cpl))); 942 sc = td_adapter(td); 943 944 CTR2(KTR_CXGBE, "%s: atid %u ", __func__, atid); 945 act_open_failure_cleanup(sc, atid, EHOSTUNREACH); 946 free(wr, M_CXGBE); 947 break; 948 default: 949 log(LOG_ERR, "%s: leaked work request %p, wr_len %d, " 950 "opcode %x\n", __func__, wr, wr->wr_len, opcode); 951 /* WR not freed here; go look at it with a debugger. */ 952 } 953 } 954 } 955 956 /* 957 * Ground control to Major TOM 958 * Commencing countdown, engines on 959 */ 960 static int 961 t4_tom_activate(struct adapter *sc) 962 { 963 struct tom_data *td; 964 struct toedev *tod; 965 int i, rc; 966 967 ASSERT_SYNCHRONIZED_OP(sc); 968 969 /* per-adapter softc for TOM */ 970 td = malloc(sizeof(*td), M_CXGBE, M_ZERO | M_NOWAIT); 971 if (td == NULL) 972 return (ENOMEM); 973 974 /* List of TOE PCBs and associated lock */ 975 mtx_init(&td->toep_list_lock, "PCB list lock", NULL, MTX_DEF); 976 TAILQ_INIT(&td->toep_list); 977 978 /* Listen context */ 979 mtx_init(&td->lctx_hash_lock, "lctx hash lock", NULL, MTX_DEF); 980 td->listen_hash = hashinit_flags(LISTEN_HASH_SIZE, M_CXGBE, 981 &td->listen_mask, HASH_NOWAIT); 982 983 /* List of WRs for which L2 resolution failed */ 984 mtx_init(&td->unsent_wr_lock, "Unsent WR list lock", NULL, MTX_DEF); 985 STAILQ_INIT(&td->unsent_wr_list); 986 TASK_INIT(&td->reclaim_wr_resources, 0, reclaim_wr_resources, td); 987 988 /* TID tables */ 989 rc = alloc_tid_tabs(&sc->tids); 990 if (rc != 0) 991 goto done; 992 993 /* DDP page pods and CPL handlers */ 994 t4_init_ddp(sc, td); 995 996 /* CLIP table for IPv6 offload */ 997 init_clip_table(sc, td); 998 999 /* CPL handlers */ 1000 t4_init_connect_cpl_handlers(sc); 1001 t4_init_l2t_cpl_handlers(sc); 1002 t4_init_listen_cpl_handlers(sc); 1003 t4_init_cpl_io_handlers(sc); 1004 1005 /* toedev ops */ 1006 tod = &td->tod; 1007 init_toedev(tod); 1008 tod->tod_softc = sc; 1009 tod->tod_connect = t4_connect; 1010 tod->tod_listen_start = t4_listen_start; 1011 tod->tod_listen_stop = t4_listen_stop; 1012 tod->tod_rcvd = t4_rcvd; 1013 tod->tod_output = t4_tod_output; 1014 tod->tod_send_rst = t4_send_rst; 1015 tod->tod_send_fin = t4_send_fin; 1016 tod->tod_pcb_detach = t4_pcb_detach; 1017 tod->tod_l2_update = t4_l2_update; 1018 tod->tod_syncache_added = t4_syncache_added; 1019 tod->tod_syncache_removed = t4_syncache_removed; 1020 tod->tod_syncache_respond = t4_syncache_respond; 1021 tod->tod_offload_socket = t4_offload_socket; 1022 tod->tod_ctloutput = t4_ctloutput; 1023 1024 for_each_port(sc, i) 1025 TOEDEV(sc->port[i]->ifp) = &td->tod; 1026 1027 sc->tom_softc = td; 1028 sc->flags |= TOM_INIT_DONE; 1029 register_toedev(sc->tom_softc); 1030 1031 done: 1032 if (rc != 0) 1033 free_tom_data(sc, td); 1034 return (rc); 1035 } 1036 1037 static int 1038 t4_tom_deactivate(struct adapter *sc) 1039 { 1040 int rc = 0; 1041 struct tom_data *td = sc->tom_softc; 1042 1043 ASSERT_SYNCHRONIZED_OP(sc); 1044 1045 if (td == NULL) 1046 return (0); /* XXX. KASSERT? */ 1047 1048 if (sc->offload_map != 0) 1049 return (EBUSY); /* at least one port has IFCAP_TOE enabled */ 1050 1051 mtx_lock(&td->toep_list_lock); 1052 if (!TAILQ_EMPTY(&td->toep_list)) 1053 rc = EBUSY; 1054 mtx_unlock(&td->toep_list_lock); 1055 1056 mtx_lock(&td->lctx_hash_lock); 1057 if (td->lctx_count > 0) 1058 rc = EBUSY; 1059 mtx_unlock(&td->lctx_hash_lock); 1060 1061 taskqueue_drain(taskqueue_thread, &td->reclaim_wr_resources); 1062 mtx_lock(&td->unsent_wr_lock); 1063 if (!STAILQ_EMPTY(&td->unsent_wr_list)) 1064 rc = EBUSY; 1065 mtx_unlock(&td->unsent_wr_lock); 1066 1067 if (rc == 0) { 1068 unregister_toedev(sc->tom_softc); 1069 free_tom_data(sc, td); 1070 sc->tom_softc = NULL; 1071 sc->flags &= ~TOM_INIT_DONE; 1072 } 1073 1074 return (rc); 1075 } 1076 1077 static void 1078 t4_tom_ifaddr_event(void *arg __unused, struct ifnet *ifp) 1079 { 1080 1081 atomic_add_rel_int(&in6_ifaddr_gen, 1); 1082 taskqueue_enqueue_timeout(taskqueue_thread, &clip_task, -hz / 4); 1083 } 1084 1085 static int 1086 t4_tom_mod_load(void) 1087 { 1088 int rc; 1089 struct protosw *tcp_protosw, *tcp6_protosw; 1090 1091 tcp_protosw = pffindproto(PF_INET, IPPROTO_TCP, SOCK_STREAM); 1092 if (tcp_protosw == NULL) 1093 return (ENOPROTOOPT); 1094 bcopy(tcp_protosw, &ddp_protosw, sizeof(ddp_protosw)); 1095 bcopy(tcp_protosw->pr_usrreqs, &ddp_usrreqs, sizeof(ddp_usrreqs)); 1096 ddp_usrreqs.pru_soreceive = t4_soreceive_ddp; 1097 ddp_protosw.pr_usrreqs = &ddp_usrreqs; 1098 1099 tcp6_protosw = pffindproto(PF_INET6, IPPROTO_TCP, SOCK_STREAM); 1100 if (tcp6_protosw == NULL) 1101 return (ENOPROTOOPT); 1102 bcopy(tcp6_protosw, &ddp6_protosw, sizeof(ddp6_protosw)); 1103 bcopy(tcp6_protosw->pr_usrreqs, &ddp6_usrreqs, sizeof(ddp6_usrreqs)); 1104 ddp6_usrreqs.pru_soreceive = t4_soreceive_ddp; 1105 ddp6_protosw.pr_usrreqs = &ddp6_usrreqs; 1106 1107 TIMEOUT_TASK_INIT(taskqueue_thread, &clip_task, 0, t4_clip_task, NULL); 1108 ifaddr_evhandler = EVENTHANDLER_REGISTER(ifaddr_event, 1109 t4_tom_ifaddr_event, NULL, EVENTHANDLER_PRI_ANY); 1110 1111 rc = t4_register_uld(&tom_uld_info); 1112 if (rc != 0) 1113 t4_tom_mod_unload(); 1114 1115 return (rc); 1116 } 1117 1118 static void 1119 tom_uninit(struct adapter *sc, void *arg __unused) 1120 { 1121 if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4tomun")) 1122 return; 1123 1124 /* Try to free resources (works only if no port has IFCAP_TOE) */ 1125 if (sc->flags & TOM_INIT_DONE) 1126 t4_deactivate_uld(sc, ULD_TOM); 1127 1128 end_synchronized_op(sc, 0); 1129 } 1130 1131 static int 1132 t4_tom_mod_unload(void) 1133 { 1134 t4_iterate(tom_uninit, NULL); 1135 1136 if (t4_unregister_uld(&tom_uld_info) == EBUSY) 1137 return (EBUSY); 1138 1139 if (ifaddr_evhandler) { 1140 EVENTHANDLER_DEREGISTER(ifaddr_event, ifaddr_evhandler); 1141 taskqueue_cancel_timeout(taskqueue_thread, &clip_task, NULL); 1142 } 1143 1144 return (0); 1145 } 1146 #endif /* TCP_OFFLOAD */ 1147 1148 static int 1149 t4_tom_modevent(module_t mod, int cmd, void *arg) 1150 { 1151 int rc = 0; 1152 1153 #ifdef TCP_OFFLOAD 1154 switch (cmd) { 1155 case MOD_LOAD: 1156 rc = t4_tom_mod_load(); 1157 break; 1158 1159 case MOD_UNLOAD: 1160 rc = t4_tom_mod_unload(); 1161 break; 1162 1163 default: 1164 rc = EINVAL; 1165 } 1166 #else 1167 printf("t4_tom: compiled without TCP_OFFLOAD support.\n"); 1168 rc = EOPNOTSUPP; 1169 #endif 1170 return (rc); 1171 } 1172 1173 static moduledata_t t4_tom_moddata= { 1174 "t4_tom", 1175 t4_tom_modevent, 1176 0 1177 }; 1178 1179 MODULE_VERSION(t4_tom, 1); 1180 MODULE_DEPEND(t4_tom, toecore, 1, 1, 1); 1181 MODULE_DEPEND(t4_tom, t4nex, 1, 1, 1); 1182 DECLARE_MODULE(t4_tom, t4_tom_moddata, SI_SUB_EXEC, SI_ORDER_ANY); 1183