1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2012 Chelsio Communications, Inc. 5 * All rights reserved. 6 * Written by: Navdeep Parhar <np@FreeBSD.org> 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include "opt_inet.h" 34 #include "opt_inet6.h" 35 36 #ifdef TCP_OFFLOAD 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/kernel.h> 40 #include <sys/ktr.h> 41 #include <sys/module.h> 42 #include <sys/protosw.h> 43 #include <sys/domain.h> 44 #include <sys/socket.h> 45 #include <sys/socketvar.h> 46 #include <net/ethernet.h> 47 #include <net/if.h> 48 #include <net/if_types.h> 49 #include <net/if_vlan_var.h> 50 #include <net/route.h> 51 #include <netinet/in.h> 52 #include <netinet/in_pcb.h> 53 #include <netinet/ip.h> 54 #define TCPSTATES 55 #include <netinet/tcp_fsm.h> 56 #include <netinet/tcp_var.h> 57 #include <netinet/toecore.h> 58 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 "tom/t4_tom_l2t.h" 64 #include "tom/t4_tom.h" 65 66 /* atid services */ 67 static int alloc_atid(struct adapter *, void *); 68 static void *lookup_atid(struct adapter *, int); 69 static void free_atid(struct adapter *, int); 70 71 static int 72 alloc_atid(struct adapter *sc, void *ctx) 73 { 74 struct tid_info *t = &sc->tids; 75 int atid = -1; 76 77 mtx_lock(&t->atid_lock); 78 if (t->afree) { 79 union aopen_entry *p = t->afree; 80 81 atid = p - t->atid_tab; 82 t->afree = p->next; 83 p->data = ctx; 84 t->atids_in_use++; 85 } 86 mtx_unlock(&t->atid_lock); 87 return (atid); 88 } 89 90 static void * 91 lookup_atid(struct adapter *sc, int atid) 92 { 93 struct tid_info *t = &sc->tids; 94 95 return (t->atid_tab[atid].data); 96 } 97 98 static void 99 free_atid(struct adapter *sc, int atid) 100 { 101 struct tid_info *t = &sc->tids; 102 union aopen_entry *p = &t->atid_tab[atid]; 103 104 mtx_lock(&t->atid_lock); 105 p->next = t->afree; 106 t->afree = p; 107 t->atids_in_use--; 108 mtx_unlock(&t->atid_lock); 109 } 110 111 /* 112 * Active open succeeded. 113 */ 114 static int 115 do_act_establish(struct sge_iq *iq, const struct rss_header *rss, 116 struct mbuf *m) 117 { 118 struct adapter *sc = iq->adapter; 119 const struct cpl_act_establish *cpl = (const void *)(rss + 1); 120 u_int tid = GET_TID(cpl); 121 u_int atid = G_TID_TID(ntohl(cpl->tos_atid)); 122 struct toepcb *toep = lookup_atid(sc, atid); 123 struct inpcb *inp = toep->inp; 124 125 KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__)); 126 KASSERT(toep->tid == atid, ("%s: toep tid/atid mismatch", __func__)); 127 128 CTR3(KTR_CXGBE, "%s: atid %u, tid %u", __func__, atid, tid); 129 free_atid(sc, atid); 130 131 CURVNET_SET(toep->vnet); 132 INP_WLOCK(inp); 133 toep->tid = tid; 134 insert_tid(sc, tid, toep, inp->inp_vflag & INP_IPV6 ? 2 : 1); 135 if (inp->inp_flags & INP_DROPPED) { 136 137 /* socket closed by the kernel before hw told us it connected */ 138 139 send_flowc_wr(toep, NULL); 140 send_reset(sc, toep, be32toh(cpl->snd_isn)); 141 goto done; 142 } 143 144 make_established(toep, cpl->snd_isn, cpl->rcv_isn, cpl->tcp_opt); 145 done: 146 INP_WUNLOCK(inp); 147 CURVNET_RESTORE(); 148 return (0); 149 } 150 151 /* 152 * Convert an ACT_OPEN_RPL status to an errno. 153 */ 154 static inline int 155 act_open_rpl_status_to_errno(int status) 156 { 157 158 switch (status) { 159 case CPL_ERR_CONN_RESET: 160 return (ECONNREFUSED); 161 case CPL_ERR_ARP_MISS: 162 return (EHOSTUNREACH); 163 case CPL_ERR_CONN_TIMEDOUT: 164 return (ETIMEDOUT); 165 case CPL_ERR_TCAM_FULL: 166 return (EAGAIN); 167 case CPL_ERR_CONN_EXIST: 168 log(LOG_ERR, "ACTIVE_OPEN_RPL: 4-tuple in use\n"); 169 return (EAGAIN); 170 default: 171 return (EIO); 172 } 173 } 174 175 void 176 act_open_failure_cleanup(struct adapter *sc, u_int atid, u_int status) 177 { 178 struct toepcb *toep = lookup_atid(sc, atid); 179 struct inpcb *inp = toep->inp; 180 struct toedev *tod = &toep->td->tod; 181 182 free_atid(sc, atid); 183 toep->tid = -1; 184 185 CURVNET_SET(toep->vnet); 186 if (status != EAGAIN) 187 INP_INFO_RLOCK(&V_tcbinfo); 188 INP_WLOCK(inp); 189 toe_connect_failed(tod, inp, status); 190 final_cpl_received(toep); /* unlocks inp */ 191 if (status != EAGAIN) 192 INP_INFO_RUNLOCK(&V_tcbinfo); 193 CURVNET_RESTORE(); 194 } 195 196 /* 197 * Active open failed. 198 */ 199 static int 200 do_act_open_rpl(struct sge_iq *iq, const struct rss_header *rss, 201 struct mbuf *m) 202 { 203 struct adapter *sc = iq->adapter; 204 const struct cpl_act_open_rpl *cpl = (const void *)(rss + 1); 205 u_int atid = G_TID_TID(G_AOPEN_ATID(be32toh(cpl->atid_status))); 206 u_int status = G_AOPEN_STATUS(be32toh(cpl->atid_status)); 207 struct toepcb *toep = lookup_atid(sc, atid); 208 int rc; 209 210 KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__)); 211 KASSERT(toep->tid == atid, ("%s: toep tid/atid mismatch", __func__)); 212 213 CTR3(KTR_CXGBE, "%s: atid %u, status %u ", __func__, atid, status); 214 215 /* Ignore negative advice */ 216 if (negative_advice(status)) 217 return (0); 218 219 if (status && act_open_has_tid(status)) 220 release_tid(sc, GET_TID(cpl), toep->ctrlq); 221 222 rc = act_open_rpl_status_to_errno(status); 223 act_open_failure_cleanup(sc, atid, rc); 224 225 return (0); 226 } 227 228 /* 229 * Options2 for active open. 230 */ 231 static uint32_t 232 calc_opt2a(struct socket *so, struct toepcb *toep) 233 { 234 struct tcpcb *tp = so_sototcpcb(so); 235 struct port_info *pi = toep->vi->pi; 236 struct adapter *sc = pi->adapter; 237 uint32_t opt2; 238 239 opt2 = V_TX_QUEUE(sc->params.tp.tx_modq[pi->tx_chan]) | 240 F_RSS_QUEUE_VALID | V_RSS_QUEUE(toep->ofld_rxq->iq.abs_id); 241 242 if (tp->t_flags & TF_SACK_PERMIT) 243 opt2 |= F_SACK_EN; 244 245 if (tp->t_flags & TF_REQ_TSTMP) 246 opt2 |= F_TSTAMPS_EN; 247 248 if (tp->t_flags & TF_REQ_SCALE) 249 opt2 |= F_WND_SCALE_EN; 250 251 if (V_tcp_do_ecn) 252 opt2 |= F_CCTRL_ECN; 253 254 /* RX_COALESCE is always a valid value (M_RX_COALESCE). */ 255 if (is_t4(sc)) 256 opt2 |= F_RX_COALESCE_VALID; 257 else { 258 opt2 |= F_T5_OPT_2_VALID; 259 opt2 |= F_T5_ISS; 260 } 261 if (sc->tt.rx_coalesce) 262 opt2 |= V_RX_COALESCE(M_RX_COALESCE); 263 264 if (sc->tt.cong_algorithm != -1) 265 opt2 |= V_CONG_CNTRL(sc->tt.cong_algorithm & M_CONG_CNTRL); 266 267 #ifdef USE_DDP_RX_FLOW_CONTROL 268 if (toep->ulp_mode == ULP_MODE_TCPDDP) 269 opt2 |= F_RX_FC_VALID | F_RX_FC_DDP; 270 #endif 271 272 return (htobe32(opt2)); 273 } 274 275 void 276 t4_init_connect_cpl_handlers(void) 277 { 278 279 t4_register_cpl_handler(CPL_ACT_ESTABLISH, do_act_establish); 280 t4_register_cpl_handler(CPL_ACT_OPEN_RPL, do_act_open_rpl); 281 } 282 283 void 284 t4_uninit_connect_cpl_handlers(void) 285 { 286 287 t4_register_cpl_handler(CPL_ACT_ESTABLISH, NULL); 288 t4_register_cpl_handler(CPL_ACT_OPEN_RPL, NULL); 289 } 290 291 #define DONT_OFFLOAD_ACTIVE_OPEN(x) do { \ 292 reason = __LINE__; \ 293 rc = (x); \ 294 goto failed; \ 295 } while (0) 296 297 static inline int 298 act_open_cpl_size(struct adapter *sc, int isipv6) 299 { 300 int idx; 301 static const int sz_table[3][2] = { 302 { 303 sizeof (struct cpl_act_open_req), 304 sizeof (struct cpl_act_open_req6) 305 }, 306 { 307 sizeof (struct cpl_t5_act_open_req), 308 sizeof (struct cpl_t5_act_open_req6) 309 }, 310 { 311 sizeof (struct cpl_t6_act_open_req), 312 sizeof (struct cpl_t6_act_open_req6) 313 }, 314 }; 315 316 MPASS(chip_id(sc) >= CHELSIO_T4); 317 idx = min(chip_id(sc) - CHELSIO_T4, 2); 318 319 return (sz_table[idx][!!isipv6]); 320 } 321 322 /* 323 * active open (soconnect). 324 * 325 * State of affairs on entry: 326 * soisconnecting (so_state |= SS_ISCONNECTING) 327 * tcbinfo not locked (This has changed - used to be WLOCKed) 328 * inp WLOCKed 329 * tp->t_state = TCPS_SYN_SENT 330 * rtalloc1, RT_UNLOCK on rt. 331 */ 332 int 333 t4_connect(struct toedev *tod, struct socket *so, struct rtentry *rt, 334 struct sockaddr *nam) 335 { 336 struct adapter *sc = tod->tod_softc; 337 struct tom_data *td = tod_td(tod); 338 struct toepcb *toep = NULL; 339 struct wrqe *wr = NULL; 340 struct ifnet *rt_ifp = rt->rt_ifp; 341 struct vi_info *vi; 342 int mtu_idx, rscale, qid_atid, rc, isipv6; 343 struct inpcb *inp = sotoinpcb(so); 344 struct tcpcb *tp = intotcpcb(inp); 345 int reason; 346 347 INP_WLOCK_ASSERT(inp); 348 KASSERT(nam->sa_family == AF_INET || nam->sa_family == AF_INET6, 349 ("%s: dest addr %p has family %u", __func__, nam, nam->sa_family)); 350 351 if (rt_ifp->if_type == IFT_ETHER) 352 vi = rt_ifp->if_softc; 353 else if (rt_ifp->if_type == IFT_L2VLAN) { 354 struct ifnet *ifp = VLAN_COOKIE(rt_ifp); 355 356 vi = ifp->if_softc; 357 } else if (rt_ifp->if_type == IFT_IEEE8023ADLAG) 358 DONT_OFFLOAD_ACTIVE_OPEN(ENOSYS); /* XXX: implement lagg+TOE */ 359 else 360 DONT_OFFLOAD_ACTIVE_OPEN(ENOTSUP); 361 362 toep = alloc_toepcb(vi, -1, -1, M_NOWAIT | M_ZERO); 363 if (toep == NULL) 364 DONT_OFFLOAD_ACTIVE_OPEN(ENOMEM); 365 366 toep->tid = alloc_atid(sc, toep); 367 if (toep->tid < 0) 368 DONT_OFFLOAD_ACTIVE_OPEN(ENOMEM); 369 370 toep->l2te = t4_l2t_get(vi->pi, rt_ifp, 371 rt->rt_flags & RTF_GATEWAY ? rt->rt_gateway : nam); 372 if (toep->l2te == NULL) 373 DONT_OFFLOAD_ACTIVE_OPEN(ENOMEM); 374 375 isipv6 = nam->sa_family == AF_INET6; 376 wr = alloc_wrqe(act_open_cpl_size(sc, isipv6), toep->ctrlq); 377 if (wr == NULL) 378 DONT_OFFLOAD_ACTIVE_OPEN(ENOMEM); 379 380 toep->vnet = so->so_vnet; 381 if (sc->tt.ddp && (so->so_options & SO_NO_DDP) == 0) 382 set_tcpddp_ulp_mode(toep); 383 else 384 toep->ulp_mode = ULP_MODE_NONE; 385 SOCKBUF_LOCK(&so->so_rcv); 386 /* opt0 rcv_bufsiz initially, assumes its normal meaning later */ 387 toep->rx_credits = min(select_rcv_wnd(so) >> 10, M_RCV_BUFSIZ); 388 SOCKBUF_UNLOCK(&so->so_rcv); 389 390 /* 391 * The kernel sets request_r_scale based on sb_max whereas we need to 392 * take hardware's MAX_RCV_WND into account too. This is normally a 393 * no-op as MAX_RCV_WND is much larger than the default sb_max. 394 */ 395 if (tp->t_flags & TF_REQ_SCALE) 396 rscale = tp->request_r_scale = select_rcv_wscale(); 397 else 398 rscale = 0; 399 mtu_idx = find_best_mtu_idx(sc, &inp->inp_inc, 0); 400 qid_atid = (toep->ofld_rxq->iq.abs_id << 14) | toep->tid; 401 402 if (isipv6) { 403 struct cpl_act_open_req6 *cpl = wrtod(wr); 404 struct cpl_t5_act_open_req6 *cpl5 = (void *)cpl; 405 struct cpl_t6_act_open_req6 *cpl6 = (void *)cpl; 406 407 if ((inp->inp_vflag & INP_IPV6) == 0) 408 DONT_OFFLOAD_ACTIVE_OPEN(ENOTSUP); 409 410 toep->ce = hold_lip(td, &inp->in6p_laddr, NULL); 411 if (toep->ce == NULL) 412 DONT_OFFLOAD_ACTIVE_OPEN(ENOENT); 413 414 switch (chip_id(sc)) { 415 case CHELSIO_T4: 416 INIT_TP_WR(cpl, 0); 417 cpl->params = select_ntuple(vi, toep->l2te); 418 break; 419 case CHELSIO_T5: 420 INIT_TP_WR(cpl5, 0); 421 cpl5->iss = htobe32(tp->iss); 422 cpl5->params = select_ntuple(vi, toep->l2te); 423 break; 424 case CHELSIO_T6: 425 default: 426 INIT_TP_WR(cpl6, 0); 427 cpl6->iss = htobe32(tp->iss); 428 cpl6->params = select_ntuple(vi, toep->l2te); 429 break; 430 } 431 OPCODE_TID(cpl) = htobe32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ6, 432 qid_atid)); 433 cpl->local_port = inp->inp_lport; 434 cpl->local_ip_hi = *(uint64_t *)&inp->in6p_laddr.s6_addr[0]; 435 cpl->local_ip_lo = *(uint64_t *)&inp->in6p_laddr.s6_addr[8]; 436 cpl->peer_port = inp->inp_fport; 437 cpl->peer_ip_hi = *(uint64_t *)&inp->in6p_faddr.s6_addr[0]; 438 cpl->peer_ip_lo = *(uint64_t *)&inp->in6p_faddr.s6_addr[8]; 439 cpl->opt0 = calc_opt0(so, vi, toep->l2te, mtu_idx, rscale, 440 toep->rx_credits, toep->ulp_mode); 441 cpl->opt2 = calc_opt2a(so, toep); 442 } else { 443 struct cpl_act_open_req *cpl = wrtod(wr); 444 struct cpl_t5_act_open_req *cpl5 = (void *)cpl; 445 struct cpl_t6_act_open_req *cpl6 = (void *)cpl; 446 447 switch (chip_id(sc)) { 448 case CHELSIO_T4: 449 INIT_TP_WR(cpl, 0); 450 cpl->params = select_ntuple(vi, toep->l2te); 451 break; 452 case CHELSIO_T5: 453 INIT_TP_WR(cpl5, 0); 454 cpl5->iss = htobe32(tp->iss); 455 cpl5->params = select_ntuple(vi, toep->l2te); 456 break; 457 case CHELSIO_T6: 458 default: 459 INIT_TP_WR(cpl6, 0); 460 cpl6->iss = htobe32(tp->iss); 461 cpl6->params = select_ntuple(vi, toep->l2te); 462 break; 463 } 464 OPCODE_TID(cpl) = htobe32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ, 465 qid_atid)); 466 inp_4tuple_get(inp, &cpl->local_ip, &cpl->local_port, 467 &cpl->peer_ip, &cpl->peer_port); 468 cpl->opt0 = calc_opt0(so, vi, toep->l2te, mtu_idx, rscale, 469 toep->rx_credits, toep->ulp_mode); 470 cpl->opt2 = calc_opt2a(so, toep); 471 } 472 473 CTR5(KTR_CXGBE, "%s: atid %u (%s), toep %p, inp %p", __func__, 474 toep->tid, tcpstates[tp->t_state], toep, inp); 475 476 offload_socket(so, toep); 477 rc = t4_l2t_send(sc, wr, toep->l2te); 478 if (rc == 0) { 479 toep->flags |= TPF_CPL_PENDING; 480 return (0); 481 } 482 483 undo_offload_socket(so); 484 reason = __LINE__; 485 failed: 486 CTR3(KTR_CXGBE, "%s: not offloading (%d), rc %d", __func__, reason, rc); 487 488 if (wr) 489 free_wrqe(wr); 490 491 if (toep) { 492 if (toep->tid >= 0) 493 free_atid(sc, toep->tid); 494 if (toep->l2te) 495 t4_l2t_release(toep->l2te); 496 if (toep->ce) 497 release_lip(td, toep->ce); 498 free_toepcb(toep); 499 } 500 501 return (rc); 502 } 503 #endif 504