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