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 <net/if.h> 45 #include <netinet/in.h> 46 #include <netinet/in_pcb.h> 47 #include <netinet/in_var.h> 48 #include <netinet/ip.h> 49 #include <netinet/ip6.h> 50 #include <netinet/tcp_var.h> 51 #include <netinet6/scope6_var.h> 52 #define TCPSTATES 53 #include <netinet/tcp_fsm.h> 54 #include <netinet/toecore.h> 55 56 #ifdef TCP_OFFLOAD 57 #include "common/common.h" 58 #include "common/t4_msg.h" 59 #include "common/t4_regs.h" 60 #include "tom/t4_tom_l2t.h" 61 #include "tom/t4_tom.h" 62 63 static struct protosw ddp_protosw; 64 static struct pr_usrreqs ddp_usrreqs; 65 66 static struct protosw ddp6_protosw; 67 static struct pr_usrreqs ddp6_usrreqs; 68 69 /* Module ops */ 70 static int t4_tom_mod_load(void); 71 static int t4_tom_mod_unload(void); 72 static int t4_tom_modevent(module_t, int, void *); 73 74 /* ULD ops and helpers */ 75 static int t4_tom_activate(struct adapter *); 76 static int t4_tom_deactivate(struct adapter *); 77 78 static struct uld_info tom_uld_info = { 79 .uld_id = ULD_TOM, 80 .activate = t4_tom_activate, 81 .deactivate = t4_tom_deactivate, 82 }; 83 84 static void queue_tid_release(struct adapter *, int); 85 static void release_offload_resources(struct toepcb *); 86 static int alloc_tid_tabs(struct tid_info *); 87 static void free_tid_tabs(struct tid_info *); 88 static int add_lip(struct adapter *, struct in6_addr *); 89 static int delete_lip(struct adapter *, struct in6_addr *); 90 static struct clip_entry *search_lip(struct tom_data *, struct in6_addr *); 91 static void init_clip_table(struct adapter *, struct tom_data *); 92 static void destroy_clip_table(struct adapter *, struct tom_data *); 93 static void free_tom_data(struct adapter *, struct tom_data *); 94 95 struct toepcb * 96 alloc_toepcb(struct port_info *pi, int txqid, int rxqid, int flags) 97 { 98 struct adapter *sc = pi->adapter; 99 struct toepcb *toep; 100 int tx_credits, txsd_total, len; 101 102 /* 103 * The firmware counts tx work request credits in units of 16 bytes 104 * each. Reserve room for an ABORT_REQ so the driver never has to worry 105 * about tx credits if it wants to abort a connection. 106 */ 107 tx_credits = sc->params.ofldq_wr_cred; 108 tx_credits -= howmany(sizeof(struct cpl_abort_req), 16); 109 110 /* 111 * Shortest possible tx work request is a fw_ofld_tx_data_wr + 1 byte 112 * immediate payload, and firmware counts tx work request credits in 113 * units of 16 byte. Calculate the maximum work requests possible. 114 */ 115 txsd_total = tx_credits / 116 howmany((sizeof(struct fw_ofld_tx_data_wr) + 1), 16); 117 118 if (txqid < 0) 119 txqid = (arc4random() % pi->nofldtxq) + pi->first_ofld_txq; 120 KASSERT(txqid >= pi->first_ofld_txq && 121 txqid < pi->first_ofld_txq + pi->nofldtxq, 122 ("%s: txqid %d for port %p (first %d, n %d)", __func__, txqid, pi, 123 pi->first_ofld_txq, pi->nofldtxq)); 124 125 if (rxqid < 0) 126 rxqid = (arc4random() % pi->nofldrxq) + pi->first_ofld_rxq; 127 KASSERT(rxqid >= pi->first_ofld_rxq && 128 rxqid < pi->first_ofld_rxq + pi->nofldrxq, 129 ("%s: rxqid %d for port %p (first %d, n %d)", __func__, rxqid, pi, 130 pi->first_ofld_rxq, pi->nofldrxq)); 131 132 len = offsetof(struct toepcb, txsd) + 133 txsd_total * sizeof(struct ofld_tx_sdesc); 134 135 toep = malloc(len, M_CXGBE, M_ZERO | flags); 136 if (toep == NULL) 137 return (NULL); 138 139 toep->td = sc->tom_softc; 140 toep->port = pi; 141 toep->tx_credits = tx_credits; 142 toep->ofld_txq = &sc->sge.ofld_txq[txqid]; 143 toep->ofld_rxq = &sc->sge.ofld_rxq[rxqid]; 144 toep->ctrlq = &sc->sge.ctrlq[pi->port_id]; 145 toep->txsd_total = txsd_total; 146 toep->txsd_avail = txsd_total; 147 toep->txsd_pidx = 0; 148 toep->txsd_cidx = 0; 149 150 return (toep); 151 } 152 153 void 154 free_toepcb(struct toepcb *toep) 155 { 156 157 KASSERT(!(toep->flags & TPF_ATTACHED), 158 ("%s: attached to an inpcb", __func__)); 159 KASSERT(!(toep->flags & TPF_CPL_PENDING), 160 ("%s: CPL pending", __func__)); 161 162 free(toep, M_CXGBE); 163 } 164 165 /* 166 * Set up the socket for TCP offload. 167 */ 168 void 169 offload_socket(struct socket *so, struct toepcb *toep) 170 { 171 struct tom_data *td = toep->td; 172 struct inpcb *inp = sotoinpcb(so); 173 struct tcpcb *tp = intotcpcb(inp); 174 struct sockbuf *sb; 175 176 INP_WLOCK_ASSERT(inp); 177 178 /* Update socket */ 179 sb = &so->so_snd; 180 SOCKBUF_LOCK(sb); 181 sb->sb_flags |= SB_NOCOALESCE; 182 SOCKBUF_UNLOCK(sb); 183 sb = &so->so_rcv; 184 SOCKBUF_LOCK(sb); 185 sb->sb_flags |= SB_NOCOALESCE; 186 if (toep->ulp_mode == ULP_MODE_TCPDDP) { 187 if (inp->inp_vflag & INP_IPV6) 188 so->so_proto = &ddp6_protosw; 189 else 190 so->so_proto = &ddp_protosw; 191 } 192 SOCKBUF_UNLOCK(sb); 193 194 /* Update TCP PCB */ 195 tp->tod = &td->tod; 196 tp->t_toe = toep; 197 tp->t_flags |= TF_TOE; 198 199 /* Install an extra hold on inp */ 200 toep->inp = inp; 201 toep->flags |= TPF_ATTACHED; 202 in_pcbref(inp); 203 204 /* Add the TOE PCB to the active list */ 205 mtx_lock(&td->toep_list_lock); 206 TAILQ_INSERT_HEAD(&td->toep_list, toep, link); 207 mtx_unlock(&td->toep_list_lock); 208 } 209 210 /* This is _not_ the normal way to "unoffload" a socket. */ 211 void 212 undo_offload_socket(struct socket *so) 213 { 214 struct inpcb *inp = sotoinpcb(so); 215 struct tcpcb *tp = intotcpcb(inp); 216 struct toepcb *toep = tp->t_toe; 217 struct tom_data *td = toep->td; 218 struct sockbuf *sb; 219 220 INP_WLOCK_ASSERT(inp); 221 222 sb = &so->so_snd; 223 SOCKBUF_LOCK(sb); 224 sb->sb_flags &= ~SB_NOCOALESCE; 225 SOCKBUF_UNLOCK(sb); 226 sb = &so->so_rcv; 227 SOCKBUF_LOCK(sb); 228 sb->sb_flags &= ~SB_NOCOALESCE; 229 SOCKBUF_UNLOCK(sb); 230 231 tp->tod = NULL; 232 tp->t_toe = NULL; 233 tp->t_flags &= ~TF_TOE; 234 235 toep->inp = NULL; 236 toep->flags &= ~TPF_ATTACHED; 237 if (in_pcbrele_wlocked(inp)) 238 panic("%s: inp freed.", __func__); 239 240 mtx_lock(&td->toep_list_lock); 241 TAILQ_REMOVE(&td->toep_list, toep, link); 242 mtx_unlock(&td->toep_list_lock); 243 } 244 245 static void 246 release_offload_resources(struct toepcb *toep) 247 { 248 struct tom_data *td = toep->td; 249 struct adapter *sc = td_adapter(td); 250 int tid = toep->tid; 251 252 KASSERT(!(toep->flags & TPF_CPL_PENDING), 253 ("%s: %p has CPL pending.", __func__, toep)); 254 KASSERT(!(toep->flags & TPF_ATTACHED), 255 ("%s: %p is still attached.", __func__, toep)); 256 257 CTR5(KTR_CXGBE, "%s: toep %p (tid %d, l2te %p, ce %p)", 258 __func__, toep, tid, toep->l2te, toep->ce); 259 260 if (toep->ulp_mode == ULP_MODE_TCPDDP) 261 release_ddp_resources(toep); 262 263 if (toep->l2te) 264 t4_l2t_release(toep->l2te); 265 266 if (tid >= 0) { 267 remove_tid(sc, tid); 268 release_tid(sc, tid, toep->ctrlq); 269 } 270 271 if (toep->ce) 272 release_lip(td, toep->ce); 273 274 mtx_lock(&td->toep_list_lock); 275 TAILQ_REMOVE(&td->toep_list, toep, link); 276 mtx_unlock(&td->toep_list_lock); 277 278 free_toepcb(toep); 279 } 280 281 /* 282 * The kernel is done with the TCP PCB and this is our opportunity to unhook the 283 * toepcb hanging off of it. If the TOE driver is also done with the toepcb (no 284 * pending CPL) then it is time to release all resources tied to the toepcb. 285 * 286 * Also gets called when an offloaded active open fails and the TOM wants the 287 * kernel to take the TCP PCB back. 288 */ 289 static void 290 t4_pcb_detach(struct toedev *tod __unused, struct tcpcb *tp) 291 { 292 #if defined(KTR) || defined(INVARIANTS) 293 struct inpcb *inp = tp->t_inpcb; 294 #endif 295 struct toepcb *toep = tp->t_toe; 296 297 INP_WLOCK_ASSERT(inp); 298 299 KASSERT(toep != NULL, ("%s: toep is NULL", __func__)); 300 KASSERT(toep->flags & TPF_ATTACHED, 301 ("%s: not attached", __func__)); 302 303 #ifdef KTR 304 if (tp->t_state == TCPS_SYN_SENT) { 305 CTR6(KTR_CXGBE, "%s: atid %d, toep %p (0x%x), inp %p (0x%x)", 306 __func__, toep->tid, toep, toep->flags, inp, 307 inp->inp_flags); 308 } else { 309 CTR6(KTR_CXGBE, 310 "t4_pcb_detach: tid %d (%s), toep %p (0x%x), inp %p (0x%x)", 311 toep->tid, tcpstates[tp->t_state], toep, toep->flags, inp, 312 inp->inp_flags); 313 } 314 #endif 315 316 tp->t_toe = NULL; 317 tp->t_flags &= ~TF_TOE; 318 toep->flags &= ~TPF_ATTACHED; 319 320 if (!(toep->flags & TPF_CPL_PENDING)) 321 release_offload_resources(toep); 322 } 323 324 /* 325 * The TOE driver will not receive any more CPLs for the tid associated with the 326 * toepcb; release the hold on the inpcb. 327 */ 328 void 329 final_cpl_received(struct toepcb *toep) 330 { 331 struct inpcb *inp = toep->inp; 332 333 KASSERT(inp != NULL, ("%s: inp is NULL", __func__)); 334 INP_WLOCK_ASSERT(inp); 335 KASSERT(toep->flags & TPF_CPL_PENDING, 336 ("%s: CPL not pending already?", __func__)); 337 338 CTR6(KTR_CXGBE, "%s: tid %d, toep %p (0x%x), inp %p (0x%x)", 339 __func__, toep->tid, toep, toep->flags, inp, inp->inp_flags); 340 341 toep->inp = NULL; 342 toep->flags &= ~TPF_CPL_PENDING; 343 344 if (!(toep->flags & TPF_ATTACHED)) 345 release_offload_resources(toep); 346 347 if (!in_pcbrele_wlocked(inp)) 348 INP_WUNLOCK(inp); 349 } 350 351 void 352 insert_tid(struct adapter *sc, int tid, void *ctx) 353 { 354 struct tid_info *t = &sc->tids; 355 356 t->tid_tab[tid] = ctx; 357 atomic_add_int(&t->tids_in_use, 1); 358 } 359 360 void * 361 lookup_tid(struct adapter *sc, int tid) 362 { 363 struct tid_info *t = &sc->tids; 364 365 return (t->tid_tab[tid]); 366 } 367 368 void 369 update_tid(struct adapter *sc, int tid, void *ctx) 370 { 371 struct tid_info *t = &sc->tids; 372 373 t->tid_tab[tid] = ctx; 374 } 375 376 void 377 remove_tid(struct adapter *sc, int tid) 378 { 379 struct tid_info *t = &sc->tids; 380 381 t->tid_tab[tid] = NULL; 382 atomic_subtract_int(&t->tids_in_use, 1); 383 } 384 385 void 386 release_tid(struct adapter *sc, int tid, struct sge_wrq *ctrlq) 387 { 388 struct wrqe *wr; 389 struct cpl_tid_release *req; 390 391 wr = alloc_wrqe(sizeof(*req), ctrlq); 392 if (wr == NULL) { 393 queue_tid_release(sc, tid); /* defer */ 394 return; 395 } 396 req = wrtod(wr); 397 398 INIT_TP_WR_MIT_CPL(req, CPL_TID_RELEASE, tid); 399 400 t4_wrq_tx(sc, wr); 401 } 402 403 static void 404 queue_tid_release(struct adapter *sc, int tid) 405 { 406 407 CXGBE_UNIMPLEMENTED("deferred tid release"); 408 } 409 410 /* 411 * What mtu_idx to use, given a 4-tuple and/or an MSS cap 412 */ 413 int 414 find_best_mtu_idx(struct adapter *sc, struct in_conninfo *inc, int pmss) 415 { 416 unsigned short *mtus = &sc->params.mtus[0]; 417 int i, mss, n; 418 419 KASSERT(inc != NULL || pmss > 0, 420 ("%s: at least one of inc/pmss must be specified", __func__)); 421 422 mss = inc ? tcp_mssopt(inc) : pmss; 423 if (pmss > 0 && mss > pmss) 424 mss = pmss; 425 426 if (inc->inc_flags & INC_ISIPV6) 427 n = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 428 else 429 n = sizeof(struct ip) + sizeof(struct tcphdr); 430 431 for (i = 0; i < NMTUS - 1 && mtus[i + 1] <= mss + n; i++) 432 continue; 433 434 return (i); 435 } 436 437 /* 438 * Determine the receive window size for a socket. 439 */ 440 u_long 441 select_rcv_wnd(struct socket *so) 442 { 443 unsigned long wnd; 444 445 SOCKBUF_LOCK_ASSERT(&so->so_rcv); 446 447 wnd = sbspace(&so->so_rcv); 448 if (wnd < MIN_RCV_WND) 449 wnd = MIN_RCV_WND; 450 451 return min(wnd, MAX_RCV_WND); 452 } 453 454 int 455 select_rcv_wscale(void) 456 { 457 int wscale = 0; 458 unsigned long space = sb_max; 459 460 if (space > MAX_RCV_WND) 461 space = MAX_RCV_WND; 462 463 while (wscale < TCP_MAX_WINSHIFT && (TCP_MAXWIN << wscale) < space) 464 wscale++; 465 466 return (wscale); 467 } 468 469 extern int always_keepalive; 470 #define VIID_SMACIDX(v) (((unsigned int)(v) & 0x7f) << 1) 471 472 /* 473 * socket so could be a listening socket too. 474 */ 475 uint64_t 476 calc_opt0(struct socket *so, struct port_info *pi, struct l2t_entry *e, 477 int mtu_idx, int rscale, int rx_credits, int ulp_mode) 478 { 479 uint64_t opt0; 480 481 KASSERT(rx_credits <= M_RCV_BUFSIZ, 482 ("%s: rcv_bufsiz too high", __func__)); 483 484 opt0 = F_TCAM_BYPASS | V_WND_SCALE(rscale) | V_MSS_IDX(mtu_idx) | 485 V_ULP_MODE(ulp_mode) | V_RCV_BUFSIZ(rx_credits); 486 487 if (so != NULL) { 488 struct inpcb *inp = sotoinpcb(so); 489 struct tcpcb *tp = intotcpcb(inp); 490 int keepalive = always_keepalive || 491 so_options_get(so) & SO_KEEPALIVE; 492 493 opt0 |= V_NAGLE((tp->t_flags & TF_NODELAY) == 0); 494 opt0 |= V_KEEP_ALIVE(keepalive != 0); 495 } 496 497 if (e != NULL) 498 opt0 |= V_L2T_IDX(e->idx); 499 500 if (pi != NULL) { 501 opt0 |= V_SMAC_SEL(VIID_SMACIDX(pi->viid)); 502 opt0 |= V_TX_CHAN(pi->tx_chan); 503 } 504 505 return htobe64(opt0); 506 } 507 508 #define FILTER_SEL_WIDTH_P_FC (3 + 1) 509 #define FILTER_SEL_WIDTH_VIN_P_FC (6 + 7 + FILTER_SEL_WIDTH_P_FC) 510 #define FILTER_SEL_WIDTH_TAG_P_FC (3 + FILTER_SEL_WIDTH_VIN_P_FC) 511 #define FILTER_SEL_WIDTH_VLD_TAG_P_FC (1 + FILTER_SEL_WIDTH_TAG_P_FC) 512 #define VLAN_NONE 0xfff 513 #define FILTER_SEL_VLAN_NONE 0xffff 514 515 uint32_t 516 select_ntuple(struct port_info *pi, struct l2t_entry *e, uint32_t filter_mode) 517 { 518 uint16_t viid = pi->viid; 519 uint32_t ntuple = 0; 520 521 if (filter_mode == HW_TPL_FR_MT_PR_IV_P_FC) { 522 if (e->vlan == VLAN_NONE) 523 ntuple |= FILTER_SEL_VLAN_NONE << FILTER_SEL_WIDTH_P_FC; 524 else { 525 ntuple |= e->vlan << FILTER_SEL_WIDTH_P_FC; 526 ntuple |= 1 << FILTER_SEL_WIDTH_VLD_TAG_P_FC; 527 } 528 ntuple |= e->lport << S_PORT; 529 ntuple |= IPPROTO_TCP << FILTER_SEL_WIDTH_VLD_TAG_P_FC; 530 } else if (filter_mode == HW_TPL_FR_MT_PR_OV_P_FC) { 531 ntuple |= G_FW_VIID_VIN(viid) << FILTER_SEL_WIDTH_P_FC; 532 ntuple |= G_FW_VIID_PFN(viid) << FILTER_SEL_WIDTH_VIN_P_FC; 533 ntuple |= G_FW_VIID_VIVLD(viid) << FILTER_SEL_WIDTH_TAG_P_FC; 534 ntuple |= e->lport << S_PORT; 535 ntuple |= IPPROTO_TCP << FILTER_SEL_WIDTH_VLD_TAG_P_FC; 536 } 537 538 return (htobe32(ntuple)); 539 } 540 541 void 542 set_tcpddp_ulp_mode(struct toepcb *toep) 543 { 544 545 toep->ulp_mode = ULP_MODE_TCPDDP; 546 toep->ddp_flags = DDP_OK; 547 toep->ddp_score = DDP_LOW_SCORE; 548 } 549 550 int 551 negative_advice(int status) 552 { 553 554 return (status == CPL_ERR_RTX_NEG_ADVICE || 555 status == CPL_ERR_PERSIST_NEG_ADVICE || 556 status == CPL_ERR_KEEPALV_NEG_ADVICE); 557 } 558 559 static int 560 alloc_tid_tabs(struct tid_info *t) 561 { 562 size_t size; 563 unsigned int i; 564 565 size = t->ntids * sizeof(*t->tid_tab) + 566 t->natids * sizeof(*t->atid_tab) + 567 t->nstids * sizeof(*t->stid_tab); 568 569 t->tid_tab = malloc(size, M_CXGBE, M_ZERO | M_NOWAIT); 570 if (t->tid_tab == NULL) 571 return (ENOMEM); 572 573 mtx_init(&t->atid_lock, "atid lock", NULL, MTX_DEF); 574 t->atid_tab = (union aopen_entry *)&t->tid_tab[t->ntids]; 575 t->afree = t->atid_tab; 576 t->atids_in_use = 0; 577 for (i = 1; i < t->natids; i++) 578 t->atid_tab[i - 1].next = &t->atid_tab[i]; 579 t->atid_tab[t->natids - 1].next = NULL; 580 581 mtx_init(&t->stid_lock, "stid lock", NULL, MTX_DEF); 582 t->stid_tab = (struct listen_ctx **)&t->atid_tab[t->natids]; 583 t->stids_in_use = 0; 584 TAILQ_INIT(&t->stids); 585 t->nstids_free_head = t->nstids; 586 587 atomic_store_rel_int(&t->tids_in_use, 0); 588 589 return (0); 590 } 591 592 static void 593 free_tid_tabs(struct tid_info *t) 594 { 595 KASSERT(t->tids_in_use == 0, 596 ("%s: %d tids still in use.", __func__, t->tids_in_use)); 597 KASSERT(t->atids_in_use == 0, 598 ("%s: %d atids still in use.", __func__, t->atids_in_use)); 599 KASSERT(t->stids_in_use == 0, 600 ("%s: %d tids still in use.", __func__, t->stids_in_use)); 601 602 free(t->tid_tab, M_CXGBE); 603 t->tid_tab = NULL; 604 605 if (mtx_initialized(&t->atid_lock)) 606 mtx_destroy(&t->atid_lock); 607 if (mtx_initialized(&t->stid_lock)) 608 mtx_destroy(&t->stid_lock); 609 } 610 611 static int 612 add_lip(struct adapter *sc, struct in6_addr *lip) 613 { 614 struct fw_clip_cmd c; 615 616 ASSERT_SYNCHRONIZED_OP(sc); 617 /* mtx_assert(&td->clip_table_lock, MA_OWNED); */ 618 619 memset(&c, 0, sizeof(c)); 620 c.op_to_write = htonl(V_FW_CMD_OP(FW_CLIP_CMD) | F_FW_CMD_REQUEST | 621 F_FW_CMD_WRITE); 622 c.alloc_to_len16 = htonl(F_FW_CLIP_CMD_ALLOC | FW_LEN16(c)); 623 c.ip_hi = *(uint64_t *)&lip->s6_addr[0]; 624 c.ip_lo = *(uint64_t *)&lip->s6_addr[8]; 625 626 return (t4_wr_mbox_ns(sc, sc->mbox, &c, sizeof(c), &c)); 627 } 628 629 static int 630 delete_lip(struct adapter *sc, struct in6_addr *lip) 631 { 632 struct fw_clip_cmd c; 633 634 ASSERT_SYNCHRONIZED_OP(sc); 635 /* mtx_assert(&td->clip_table_lock, MA_OWNED); */ 636 637 memset(&c, 0, sizeof(c)); 638 c.op_to_write = htonl(V_FW_CMD_OP(FW_CLIP_CMD) | F_FW_CMD_REQUEST | 639 F_FW_CMD_READ); 640 c.alloc_to_len16 = htonl(F_FW_CLIP_CMD_FREE | FW_LEN16(c)); 641 c.ip_hi = *(uint64_t *)&lip->s6_addr[0]; 642 c.ip_lo = *(uint64_t *)&lip->s6_addr[8]; 643 644 return (t4_wr_mbox_ns(sc, sc->mbox, &c, sizeof(c), &c)); 645 } 646 647 static struct clip_entry * 648 search_lip(struct tom_data *td, struct in6_addr *lip) 649 { 650 struct clip_entry *ce; 651 652 mtx_assert(&td->clip_table_lock, MA_OWNED); 653 654 TAILQ_FOREACH(ce, &td->clip_table, link) { 655 if (IN6_ARE_ADDR_EQUAL(&ce->lip, lip)) 656 return (ce); 657 } 658 659 return (NULL); 660 } 661 662 struct clip_entry * 663 hold_lip(struct tom_data *td, struct in6_addr *lip) 664 { 665 struct clip_entry *ce; 666 667 mtx_lock(&td->clip_table_lock); 668 ce = search_lip(td, lip); 669 if (ce != NULL) 670 ce->refcount++; 671 mtx_unlock(&td->clip_table_lock); 672 673 return (ce); 674 } 675 676 void 677 release_lip(struct tom_data *td, struct clip_entry *ce) 678 { 679 680 mtx_lock(&td->clip_table_lock); 681 KASSERT(search_lip(td, &ce->lip) == ce, 682 ("%s: CLIP entry %p p not in CLIP table.", __func__, ce)); 683 KASSERT(ce->refcount > 0, 684 ("%s: CLIP entry %p has refcount 0", __func__, ce)); 685 --ce->refcount; 686 mtx_unlock(&td->clip_table_lock); 687 } 688 689 static void 690 init_clip_table(struct adapter *sc, struct tom_data *td) 691 { 692 struct in6_ifaddr *ia; 693 struct in6_addr *lip, tlip; 694 struct clip_entry *ce; 695 696 ASSERT_SYNCHRONIZED_OP(sc); 697 698 mtx_init(&td->clip_table_lock, "CLIP table lock", NULL, MTX_DEF); 699 TAILQ_INIT(&td->clip_table); 700 701 IN6_IFADDR_RLOCK(); 702 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) { 703 lip = &ia->ia_addr.sin6_addr; 704 705 KASSERT(!IN6_IS_ADDR_MULTICAST(lip), 706 ("%s: mcast address in in6_ifaddr list", __func__)); 707 708 if (IN6_IS_ADDR_LOOPBACK(lip)) 709 continue; 710 if (IN6_IS_SCOPE_EMBED(lip)) { 711 /* Remove the embedded scope */ 712 tlip = *lip; 713 lip = &tlip; 714 in6_clearscope(lip); 715 } 716 /* 717 * XXX: how to weed out the link local address for the loopback 718 * interface? It's fe80::1 usually (always?). 719 */ 720 721 mtx_lock(&td->clip_table_lock); 722 if (search_lip(td, lip) == NULL) { 723 ce = malloc(sizeof(*ce), M_CXGBE, M_NOWAIT); 724 memcpy(&ce->lip, lip, sizeof(ce->lip)); 725 ce->refcount = 0; 726 if (add_lip(sc, lip) == 0) 727 TAILQ_INSERT_TAIL(&td->clip_table, ce, link); 728 else 729 free(ce, M_CXGBE); 730 } 731 mtx_unlock(&td->clip_table_lock); 732 } 733 IN6_IFADDR_RUNLOCK(); 734 } 735 736 static void 737 destroy_clip_table(struct adapter *sc, struct tom_data *td) 738 { 739 struct clip_entry *ce, *ce_temp; 740 741 if (mtx_initialized(&td->clip_table_lock)) { 742 mtx_lock(&td->clip_table_lock); 743 TAILQ_FOREACH_SAFE(ce, &td->clip_table, link, ce_temp) { 744 KASSERT(ce->refcount == 0, 745 ("%s: CLIP entry %p still in use (%d)", __func__, 746 ce, ce->refcount)); 747 TAILQ_REMOVE(&td->clip_table, ce, link); 748 delete_lip(sc, &ce->lip); 749 free(ce, M_CXGBE); 750 } 751 mtx_unlock(&td->clip_table_lock); 752 mtx_destroy(&td->clip_table_lock); 753 } 754 } 755 756 static void 757 free_tom_data(struct adapter *sc, struct tom_data *td) 758 { 759 760 ASSERT_SYNCHRONIZED_OP(sc); 761 762 KASSERT(TAILQ_EMPTY(&td->toep_list), 763 ("%s: TOE PCB list is not empty.", __func__)); 764 KASSERT(td->lctx_count == 0, 765 ("%s: lctx hash table is not empty.", __func__)); 766 767 t4_uninit_l2t_cpl_handlers(sc); 768 t4_uninit_cpl_io_handlers(sc); 769 t4_uninit_ddp(sc, td); 770 destroy_clip_table(sc, td); 771 772 if (td->listen_mask != 0) 773 hashdestroy(td->listen_hash, M_CXGBE, td->listen_mask); 774 775 if (mtx_initialized(&td->lctx_hash_lock)) 776 mtx_destroy(&td->lctx_hash_lock); 777 if (mtx_initialized(&td->toep_list_lock)) 778 mtx_destroy(&td->toep_list_lock); 779 780 free_tid_tabs(&sc->tids); 781 free(td, M_CXGBE); 782 } 783 784 /* 785 * Ground control to Major TOM 786 * Commencing countdown, engines on 787 */ 788 static int 789 t4_tom_activate(struct adapter *sc) 790 { 791 struct tom_data *td; 792 struct toedev *tod; 793 int i, rc; 794 795 ASSERT_SYNCHRONIZED_OP(sc); 796 797 /* per-adapter softc for TOM */ 798 td = malloc(sizeof(*td), M_CXGBE, M_ZERO | M_NOWAIT); 799 if (td == NULL) 800 return (ENOMEM); 801 802 /* List of TOE PCBs and associated lock */ 803 mtx_init(&td->toep_list_lock, "PCB list lock", NULL, MTX_DEF); 804 TAILQ_INIT(&td->toep_list); 805 806 /* Listen context */ 807 mtx_init(&td->lctx_hash_lock, "lctx hash lock", NULL, MTX_DEF); 808 td->listen_hash = hashinit_flags(LISTEN_HASH_SIZE, M_CXGBE, 809 &td->listen_mask, HASH_NOWAIT); 810 811 /* TID tables */ 812 rc = alloc_tid_tabs(&sc->tids); 813 if (rc != 0) 814 goto done; 815 816 /* DDP page pods and CPL handlers */ 817 t4_init_ddp(sc, td); 818 819 /* CLIP table for IPv6 offload */ 820 init_clip_table(sc, td); 821 822 /* CPL handlers */ 823 t4_init_connect_cpl_handlers(sc); 824 t4_init_l2t_cpl_handlers(sc); 825 t4_init_listen_cpl_handlers(sc); 826 t4_init_cpl_io_handlers(sc); 827 828 /* toedev ops */ 829 tod = &td->tod; 830 init_toedev(tod); 831 tod->tod_softc = sc; 832 tod->tod_connect = t4_connect; 833 tod->tod_listen_start = t4_listen_start; 834 tod->tod_listen_stop = t4_listen_stop; 835 tod->tod_rcvd = t4_rcvd; 836 tod->tod_output = t4_tod_output; 837 tod->tod_send_rst = t4_send_rst; 838 tod->tod_send_fin = t4_send_fin; 839 tod->tod_pcb_detach = t4_pcb_detach; 840 tod->tod_l2_update = t4_l2_update; 841 tod->tod_syncache_added = t4_syncache_added; 842 tod->tod_syncache_removed = t4_syncache_removed; 843 tod->tod_syncache_respond = t4_syncache_respond; 844 tod->tod_offload_socket = t4_offload_socket; 845 846 for_each_port(sc, i) 847 TOEDEV(sc->port[i]->ifp) = &td->tod; 848 849 sc->tom_softc = td; 850 sc->flags |= TOM_INIT_DONE; 851 register_toedev(sc->tom_softc); 852 853 done: 854 if (rc != 0) 855 free_tom_data(sc, td); 856 return (rc); 857 } 858 859 static int 860 t4_tom_deactivate(struct adapter *sc) 861 { 862 int rc = 0; 863 struct tom_data *td = sc->tom_softc; 864 865 ASSERT_SYNCHRONIZED_OP(sc); 866 867 if (td == NULL) 868 return (0); /* XXX. KASSERT? */ 869 870 if (sc->offload_map != 0) 871 return (EBUSY); /* at least one port has IFCAP_TOE enabled */ 872 873 mtx_lock(&td->toep_list_lock); 874 if (!TAILQ_EMPTY(&td->toep_list)) 875 rc = EBUSY; 876 mtx_unlock(&td->toep_list_lock); 877 878 mtx_lock(&td->lctx_hash_lock); 879 if (td->lctx_count > 0) 880 rc = EBUSY; 881 mtx_unlock(&td->lctx_hash_lock); 882 883 if (rc == 0) { 884 unregister_toedev(sc->tom_softc); 885 free_tom_data(sc, td); 886 sc->tom_softc = NULL; 887 sc->flags &= ~TOM_INIT_DONE; 888 } 889 890 return (rc); 891 } 892 893 static int 894 t4_tom_mod_load(void) 895 { 896 int rc; 897 struct protosw *tcp_protosw, *tcp6_protosw; 898 899 tcp_protosw = pffindproto(PF_INET, IPPROTO_TCP, SOCK_STREAM); 900 if (tcp_protosw == NULL) 901 return (ENOPROTOOPT); 902 bcopy(tcp_protosw, &ddp_protosw, sizeof(ddp_protosw)); 903 bcopy(tcp_protosw->pr_usrreqs, &ddp_usrreqs, sizeof(ddp_usrreqs)); 904 ddp_usrreqs.pru_soreceive = t4_soreceive_ddp; 905 ddp_protosw.pr_usrreqs = &ddp_usrreqs; 906 907 tcp6_protosw = pffindproto(PF_INET6, IPPROTO_TCP, SOCK_STREAM); 908 if (tcp6_protosw == NULL) 909 return (ENOPROTOOPT); 910 bcopy(tcp6_protosw, &ddp6_protosw, sizeof(ddp6_protosw)); 911 bcopy(tcp6_protosw->pr_usrreqs, &ddp6_usrreqs, sizeof(ddp6_usrreqs)); 912 ddp6_usrreqs.pru_soreceive = t4_soreceive_ddp; 913 ddp6_protosw.pr_usrreqs = &ddp6_usrreqs; 914 915 rc = t4_register_uld(&tom_uld_info); 916 if (rc != 0) 917 t4_tom_mod_unload(); 918 919 return (rc); 920 } 921 922 static void 923 tom_uninit(struct adapter *sc, void *arg __unused) 924 { 925 if (begin_synchronized_op(sc, NULL, HOLD_LOCK, "t4tomun")) 926 return; 927 928 /* Try to free resources (works only if no port has IFCAP_TOE) */ 929 if (sc->flags & TOM_INIT_DONE) 930 t4_deactivate_uld(sc, ULD_TOM); 931 932 end_synchronized_op(sc, LOCK_HELD); 933 } 934 935 static int 936 t4_tom_mod_unload(void) 937 { 938 t4_iterate(tom_uninit, NULL); 939 940 if (t4_unregister_uld(&tom_uld_info) == EBUSY) 941 return (EBUSY); 942 943 return (0); 944 } 945 #endif /* TCP_OFFLOAD */ 946 947 static int 948 t4_tom_modevent(module_t mod, int cmd, void *arg) 949 { 950 int rc = 0; 951 952 #ifdef TCP_OFFLOAD 953 switch (cmd) { 954 case MOD_LOAD: 955 rc = t4_tom_mod_load(); 956 break; 957 958 case MOD_UNLOAD: 959 rc = t4_tom_mod_unload(); 960 break; 961 962 default: 963 rc = EINVAL; 964 } 965 #else 966 printf("t4_tom: compiled without TCP_OFFLOAD support.\n"); 967 rc = EOPNOTSUPP; 968 #endif 969 return (rc); 970 } 971 972 static moduledata_t t4_tom_moddata= { 973 "t4_tom", 974 t4_tom_modevent, 975 0 976 }; 977 978 MODULE_VERSION(t4_tom, 1); 979 MODULE_DEPEND(t4_tom, toecore, 1, 1, 1); 980 MODULE_DEPEND(t4_tom, t4nex, 1, 1, 1); 981 DECLARE_MODULE(t4_tom, t4_tom_moddata, SI_SUB_EXEC, SI_ORDER_ANY); 982