1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2012, 2015 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 #include "opt_ratelimit.h" 36 37 #ifdef TCP_OFFLOAD 38 #include <sys/param.h> 39 #include <sys/aio.h> 40 #include <sys/file.h> 41 #include <sys/kernel.h> 42 #include <sys/ktr.h> 43 #include <sys/module.h> 44 #include <sys/proc.h> 45 #include <sys/protosw.h> 46 #include <sys/domain.h> 47 #include <sys/socket.h> 48 #include <sys/socketvar.h> 49 #include <sys/sglist.h> 50 #include <sys/taskqueue.h> 51 #include <netinet/in.h> 52 #include <netinet/in_pcb.h> 53 #include <netinet/ip.h> 54 #include <netinet/ip6.h> 55 #define TCPSTATES 56 #include <netinet/tcp_fsm.h> 57 #include <netinet/tcp_seq.h> 58 #include <netinet/tcp_var.h> 59 #include <netinet/toecore.h> 60 61 #include <security/mac/mac_framework.h> 62 63 #include <vm/vm.h> 64 #include <vm/vm_extern.h> 65 #include <vm/pmap.h> 66 #include <vm/vm_map.h> 67 #include <vm/vm_page.h> 68 69 #include "common/common.h" 70 #include "common/t4_msg.h" 71 #include "common/t4_regs.h" 72 #include "common/t4_tcb.h" 73 #include "tom/t4_tom_l2t.h" 74 #include "tom/t4_tom.h" 75 76 static void t4_aiotx_cancel(struct kaiocb *job); 77 static void t4_aiotx_queue_toep(struct toepcb *toep); 78 79 static size_t 80 aiotx_mbuf_pgoff(struct mbuf *m) 81 { 82 struct aiotx_buffer *ab; 83 84 MPASS(IS_AIOTX_MBUF(m)); 85 ab = m->m_ext.ext_arg1; 86 return ((ab->ps.offset + (uintptr_t)m->m_ext.ext_arg2) % PAGE_SIZE); 87 } 88 89 static vm_page_t * 90 aiotx_mbuf_pages(struct mbuf *m) 91 { 92 struct aiotx_buffer *ab; 93 int npages; 94 95 MPASS(IS_AIOTX_MBUF(m)); 96 ab = m->m_ext.ext_arg1; 97 npages = (ab->ps.offset + (uintptr_t)m->m_ext.ext_arg2) / PAGE_SIZE; 98 return (ab->ps.pages + npages); 99 } 100 101 void 102 send_flowc_wr(struct toepcb *toep, struct flowc_tx_params *ftxp) 103 { 104 struct wrqe *wr; 105 struct fw_flowc_wr *flowc; 106 unsigned int nparams, flowclen, paramidx; 107 struct vi_info *vi = toep->vi; 108 struct port_info *pi = vi->pi; 109 struct adapter *sc = pi->adapter; 110 unsigned int pfvf = G_FW_VIID_PFN(vi->viid) << S_FW_VIID_PFN; 111 struct ofld_tx_sdesc *txsd = &toep->txsd[toep->txsd_pidx]; 112 113 KASSERT(!(toep->flags & TPF_FLOWC_WR_SENT), 114 ("%s: flowc for tid %u sent already", __func__, toep->tid)); 115 116 if (ftxp != NULL) 117 nparams = 8; 118 else 119 nparams = 6; 120 if (toep->ulp_mode == ULP_MODE_TLS) 121 nparams++; 122 if (toep->tls.fcplenmax != 0) 123 nparams++; 124 if (toep->tc_idx != -1) { 125 MPASS(toep->tc_idx >= 0 && 126 toep->tc_idx < sc->chip_params->nsched_cls); 127 nparams++; 128 } 129 130 flowclen = sizeof(*flowc) + nparams * sizeof(struct fw_flowc_mnemval); 131 132 wr = alloc_wrqe(roundup2(flowclen, 16), toep->ofld_txq); 133 if (wr == NULL) { 134 /* XXX */ 135 panic("%s: allocation failure.", __func__); 136 } 137 flowc = wrtod(wr); 138 memset(flowc, 0, wr->wr_len); 139 140 flowc->op_to_nparams = htobe32(V_FW_WR_OP(FW_FLOWC_WR) | 141 V_FW_FLOWC_WR_NPARAMS(nparams)); 142 flowc->flowid_len16 = htonl(V_FW_WR_LEN16(howmany(flowclen, 16)) | 143 V_FW_WR_FLOWID(toep->tid)); 144 145 #define FLOWC_PARAM(__m, __v) \ 146 do { \ 147 flowc->mnemval[paramidx].mnemonic = FW_FLOWC_MNEM_##__m; \ 148 flowc->mnemval[paramidx].val = htobe32(__v); \ 149 paramidx++; \ 150 } while (0) 151 152 paramidx = 0; 153 154 FLOWC_PARAM(PFNVFN, pfvf); 155 FLOWC_PARAM(CH, pi->tx_chan); 156 FLOWC_PARAM(PORT, pi->tx_chan); 157 FLOWC_PARAM(IQID, toep->ofld_rxq->iq.abs_id); 158 if (ftxp) { 159 uint32_t sndbuf = min(ftxp->snd_space, sc->tt.sndbuf); 160 161 FLOWC_PARAM(SNDNXT, ftxp->snd_nxt); 162 FLOWC_PARAM(RCVNXT, ftxp->rcv_nxt); 163 FLOWC_PARAM(SNDBUF, sndbuf); 164 FLOWC_PARAM(MSS, ftxp->mss); 165 166 CTR6(KTR_CXGBE, 167 "%s: tid %u, mss %u, sndbuf %u, snd_nxt 0x%x, rcv_nxt 0x%x", 168 __func__, toep->tid, ftxp->mss, sndbuf, ftxp->snd_nxt, 169 ftxp->rcv_nxt); 170 } else { 171 FLOWC_PARAM(SNDBUF, 512); 172 FLOWC_PARAM(MSS, 512); 173 174 CTR2(KTR_CXGBE, "%s: tid %u", __func__, toep->tid); 175 } 176 if (toep->ulp_mode == ULP_MODE_TLS) 177 FLOWC_PARAM(ULP_MODE, toep->ulp_mode); 178 if (toep->tls.fcplenmax != 0) 179 FLOWC_PARAM(TXDATAPLEN_MAX, toep->tls.fcplenmax); 180 if (toep->tc_idx != -1) 181 FLOWC_PARAM(SCHEDCLASS, toep->tc_idx); 182 #undef FLOWC_PARAM 183 184 KASSERT(paramidx == nparams, ("nparams mismatch")); 185 186 txsd->tx_credits = howmany(flowclen, 16); 187 txsd->plen = 0; 188 KASSERT(toep->tx_credits >= txsd->tx_credits && toep->txsd_avail > 0, 189 ("%s: not enough credits (%d)", __func__, toep->tx_credits)); 190 toep->tx_credits -= txsd->tx_credits; 191 if (__predict_false(++toep->txsd_pidx == toep->txsd_total)) 192 toep->txsd_pidx = 0; 193 toep->txsd_avail--; 194 195 toep->flags |= TPF_FLOWC_WR_SENT; 196 t4_wrq_tx(sc, wr); 197 } 198 199 #ifdef RATELIMIT 200 /* 201 * Input is Bytes/second (so_max_pacing_rate), chip counts in Kilobits/second. 202 */ 203 static int 204 update_tx_rate_limit(struct adapter *sc, struct toepcb *toep, u_int Bps) 205 { 206 int tc_idx, rc; 207 const u_int kbps = (u_int) (uint64_t)Bps * 8ULL / 1000; 208 const int port_id = toep->vi->pi->port_id; 209 210 CTR3(KTR_CXGBE, "%s: tid %u, rate %uKbps", __func__, toep->tid, kbps); 211 212 if (kbps == 0) { 213 /* unbind */ 214 tc_idx = -1; 215 } else { 216 rc = t4_reserve_cl_rl_kbps(sc, port_id, kbps, &tc_idx); 217 if (rc != 0) 218 return (rc); 219 MPASS(tc_idx >= 0 && tc_idx < sc->chip_params->nsched_cls); 220 } 221 222 if (toep->tc_idx != tc_idx) { 223 struct wrqe *wr; 224 struct fw_flowc_wr *flowc; 225 int nparams = 1, flowclen, flowclen16; 226 struct ofld_tx_sdesc *txsd = &toep->txsd[toep->txsd_pidx]; 227 228 flowclen = sizeof(*flowc) + nparams * sizeof(struct 229 fw_flowc_mnemval); 230 flowclen16 = howmany(flowclen, 16); 231 if (toep->tx_credits < flowclen16 || toep->txsd_avail == 0 || 232 (wr = alloc_wrqe(roundup2(flowclen, 16), toep->ofld_txq)) == NULL) { 233 if (tc_idx >= 0) 234 t4_release_cl_rl(sc, port_id, tc_idx); 235 return (ENOMEM); 236 } 237 238 flowc = wrtod(wr); 239 memset(flowc, 0, wr->wr_len); 240 241 flowc->op_to_nparams = htobe32(V_FW_WR_OP(FW_FLOWC_WR) | 242 V_FW_FLOWC_WR_NPARAMS(nparams)); 243 flowc->flowid_len16 = htonl(V_FW_WR_LEN16(flowclen16) | 244 V_FW_WR_FLOWID(toep->tid)); 245 246 flowc->mnemval[0].mnemonic = FW_FLOWC_MNEM_SCHEDCLASS; 247 if (tc_idx == -1) 248 flowc->mnemval[0].val = htobe32(0xff); 249 else 250 flowc->mnemval[0].val = htobe32(tc_idx); 251 252 txsd->tx_credits = flowclen16; 253 txsd->plen = 0; 254 toep->tx_credits -= txsd->tx_credits; 255 if (__predict_false(++toep->txsd_pidx == toep->txsd_total)) 256 toep->txsd_pidx = 0; 257 toep->txsd_avail--; 258 t4_wrq_tx(sc, wr); 259 } 260 261 if (toep->tc_idx >= 0) 262 t4_release_cl_rl(sc, port_id, toep->tc_idx); 263 toep->tc_idx = tc_idx; 264 265 return (0); 266 } 267 #endif 268 269 void 270 send_reset(struct adapter *sc, struct toepcb *toep, uint32_t snd_nxt) 271 { 272 struct wrqe *wr; 273 struct cpl_abort_req *req; 274 int tid = toep->tid; 275 struct inpcb *inp = toep->inp; 276 struct tcpcb *tp = intotcpcb(inp); /* don't use if INP_DROPPED */ 277 278 INP_WLOCK_ASSERT(inp); 279 280 CTR6(KTR_CXGBE, "%s: tid %d (%s), toep_flags 0x%x, inp_flags 0x%x%s", 281 __func__, toep->tid, 282 inp->inp_flags & INP_DROPPED ? "inp dropped" : 283 tcpstates[tp->t_state], 284 toep->flags, inp->inp_flags, 285 toep->flags & TPF_ABORT_SHUTDOWN ? 286 " (abort already in progress)" : ""); 287 288 if (toep->flags & TPF_ABORT_SHUTDOWN) 289 return; /* abort already in progress */ 290 291 toep->flags |= TPF_ABORT_SHUTDOWN; 292 293 KASSERT(toep->flags & TPF_FLOWC_WR_SENT, 294 ("%s: flowc_wr not sent for tid %d.", __func__, tid)); 295 296 wr = alloc_wrqe(sizeof(*req), toep->ofld_txq); 297 if (wr == NULL) { 298 /* XXX */ 299 panic("%s: allocation failure.", __func__); 300 } 301 req = wrtod(wr); 302 303 INIT_TP_WR_MIT_CPL(req, CPL_ABORT_REQ, tid); 304 if (inp->inp_flags & INP_DROPPED) 305 req->rsvd0 = htobe32(snd_nxt); 306 else 307 req->rsvd0 = htobe32(tp->snd_nxt); 308 req->rsvd1 = !(toep->flags & TPF_TX_DATA_SENT); 309 req->cmd = CPL_ABORT_SEND_RST; 310 311 /* 312 * XXX: What's the correct way to tell that the inp hasn't been detached 313 * from its socket? Should I even be flushing the snd buffer here? 314 */ 315 if ((inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT)) == 0) { 316 struct socket *so = inp->inp_socket; 317 318 if (so != NULL) /* because I'm not sure. See comment above */ 319 sbflush(&so->so_snd); 320 } 321 322 t4_l2t_send(sc, wr, toep->l2te); 323 } 324 325 /* 326 * Called when a connection is established to translate the TCP options 327 * reported by HW to FreeBSD's native format. 328 */ 329 static void 330 assign_rxopt(struct tcpcb *tp, unsigned int opt) 331 { 332 struct toepcb *toep = tp->t_toe; 333 struct inpcb *inp = tp->t_inpcb; 334 struct adapter *sc = td_adapter(toep->td); 335 int n; 336 337 INP_LOCK_ASSERT(inp); 338 339 if (inp->inp_inc.inc_flags & INC_ISIPV6) 340 n = sizeof(struct ip6_hdr) + sizeof(struct tcphdr); 341 else 342 n = sizeof(struct ip) + sizeof(struct tcphdr); 343 tp->t_maxseg = sc->params.mtus[G_TCPOPT_MSS(opt)] - n; 344 345 if (G_TCPOPT_TSTAMP(opt)) { 346 tp->t_flags |= TF_RCVD_TSTMP; /* timestamps ok */ 347 tp->ts_recent = 0; /* hmmm */ 348 tp->ts_recent_age = tcp_ts_getticks(); 349 tp->t_maxseg -= TCPOLEN_TSTAMP_APPA; 350 } 351 352 CTR5(KTR_CXGBE, "%s: tid %d, mtu_idx %u (%u), mss %u", __func__, 353 toep->tid, G_TCPOPT_MSS(opt), sc->params.mtus[G_TCPOPT_MSS(opt)], 354 tp->t_maxseg); 355 356 if (G_TCPOPT_SACK(opt)) 357 tp->t_flags |= TF_SACK_PERMIT; /* should already be set */ 358 else 359 tp->t_flags &= ~TF_SACK_PERMIT; /* sack disallowed by peer */ 360 361 if (G_TCPOPT_WSCALE_OK(opt)) 362 tp->t_flags |= TF_RCVD_SCALE; 363 364 /* Doing window scaling? */ 365 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) == 366 (TF_RCVD_SCALE | TF_REQ_SCALE)) { 367 tp->rcv_scale = tp->request_r_scale; 368 tp->snd_scale = G_TCPOPT_SND_WSCALE(opt); 369 } 370 } 371 372 /* 373 * Completes some final bits of initialization for just established connections 374 * and changes their state to TCPS_ESTABLISHED. 375 * 376 * The ISNs are from after the exchange of SYNs. i.e., the true ISN + 1. 377 */ 378 void 379 make_established(struct toepcb *toep, uint32_t snd_isn, uint32_t rcv_isn, 380 uint16_t opt) 381 { 382 struct inpcb *inp = toep->inp; 383 struct socket *so = inp->inp_socket; 384 struct tcpcb *tp = intotcpcb(inp); 385 long bufsize; 386 uint32_t iss = be32toh(snd_isn) - 1; /* true ISS */ 387 uint32_t irs = be32toh(rcv_isn) - 1; /* true IRS */ 388 uint16_t tcpopt = be16toh(opt); 389 struct flowc_tx_params ftxp; 390 391 INP_WLOCK_ASSERT(inp); 392 KASSERT(tp->t_state == TCPS_SYN_SENT || 393 tp->t_state == TCPS_SYN_RECEIVED, 394 ("%s: TCP state %s", __func__, tcpstates[tp->t_state])); 395 396 CTR6(KTR_CXGBE, "%s: tid %d, so %p, inp %p, tp %p, toep %p", 397 __func__, toep->tid, so, inp, tp, toep); 398 399 tp->t_state = TCPS_ESTABLISHED; 400 tp->t_starttime = ticks; 401 TCPSTAT_INC(tcps_connects); 402 403 tp->irs = irs; 404 tcp_rcvseqinit(tp); 405 tp->rcv_wnd = toep->rx_credits << 10; 406 tp->rcv_adv += tp->rcv_wnd; 407 tp->last_ack_sent = tp->rcv_nxt; 408 409 /* 410 * If we were unable to send all rx credits via opt0, save the remainder 411 * in rx_credits so that they can be handed over with the next credit 412 * update. 413 */ 414 SOCKBUF_LOCK(&so->so_rcv); 415 bufsize = select_rcv_wnd(so); 416 SOCKBUF_UNLOCK(&so->so_rcv); 417 toep->rx_credits = bufsize - tp->rcv_wnd; 418 419 tp->iss = iss; 420 tcp_sendseqinit(tp); 421 tp->snd_una = iss + 1; 422 tp->snd_nxt = iss + 1; 423 tp->snd_max = iss + 1; 424 425 assign_rxopt(tp, tcpopt); 426 427 SOCKBUF_LOCK(&so->so_snd); 428 if (so->so_snd.sb_flags & SB_AUTOSIZE && V_tcp_do_autosndbuf) 429 bufsize = V_tcp_autosndbuf_max; 430 else 431 bufsize = sbspace(&so->so_snd); 432 SOCKBUF_UNLOCK(&so->so_snd); 433 434 ftxp.snd_nxt = tp->snd_nxt; 435 ftxp.rcv_nxt = tp->rcv_nxt; 436 ftxp.snd_space = bufsize; 437 ftxp.mss = tp->t_maxseg; 438 send_flowc_wr(toep, &ftxp); 439 440 soisconnected(so); 441 } 442 443 int 444 send_rx_credits(struct adapter *sc, struct toepcb *toep, int credits) 445 { 446 struct wrqe *wr; 447 struct cpl_rx_data_ack *req; 448 uint32_t dack = F_RX_DACK_CHANGE | V_RX_DACK_MODE(1); 449 450 KASSERT(credits >= 0, ("%s: %d credits", __func__, credits)); 451 452 wr = alloc_wrqe(sizeof(*req), toep->ctrlq); 453 if (wr == NULL) 454 return (0); 455 req = wrtod(wr); 456 457 INIT_TP_WR_MIT_CPL(req, CPL_RX_DATA_ACK, toep->tid); 458 req->credit_dack = htobe32(dack | V_RX_CREDITS(credits)); 459 460 t4_wrq_tx(sc, wr); 461 return (credits); 462 } 463 464 void 465 send_rx_modulate(struct adapter *sc, struct toepcb *toep) 466 { 467 struct wrqe *wr; 468 struct cpl_rx_data_ack *req; 469 470 wr = alloc_wrqe(sizeof(*req), toep->ctrlq); 471 if (wr == NULL) 472 return; 473 req = wrtod(wr); 474 475 INIT_TP_WR_MIT_CPL(req, CPL_RX_DATA_ACK, toep->tid); 476 req->credit_dack = htobe32(F_RX_MODULATE_RX); 477 478 t4_wrq_tx(sc, wr); 479 } 480 481 void 482 t4_rcvd_locked(struct toedev *tod, struct tcpcb *tp) 483 { 484 struct adapter *sc = tod->tod_softc; 485 struct inpcb *inp = tp->t_inpcb; 486 struct socket *so = inp->inp_socket; 487 struct sockbuf *sb = &so->so_rcv; 488 struct toepcb *toep = tp->t_toe; 489 int credits; 490 491 INP_WLOCK_ASSERT(inp); 492 493 SOCKBUF_LOCK_ASSERT(sb); 494 KASSERT(toep->sb_cc >= sbused(sb), 495 ("%s: sb %p has more data (%d) than last time (%d).", 496 __func__, sb, sbused(sb), toep->sb_cc)); 497 498 credits = toep->sb_cc - sbused(sb); 499 toep->sb_cc = sbused(sb); 500 if (toep->ulp_mode == ULP_MODE_TLS) { 501 if (toep->tls.rcv_over >= credits) { 502 toep->tls.rcv_over -= credits; 503 credits = 0; 504 } else { 505 credits -= toep->tls.rcv_over; 506 toep->tls.rcv_over = 0; 507 } 508 } 509 toep->rx_credits += credits; 510 511 if (toep->rx_credits > 0 && 512 (tp->rcv_wnd <= 32 * 1024 || toep->rx_credits >= 64 * 1024 || 513 (toep->rx_credits >= 16 * 1024 && tp->rcv_wnd <= 128 * 1024) || 514 toep->sb_cc + tp->rcv_wnd < sb->sb_lowat)) { 515 516 credits = send_rx_credits(sc, toep, toep->rx_credits); 517 toep->rx_credits -= credits; 518 tp->rcv_wnd += credits; 519 tp->rcv_adv += credits; 520 } else if (toep->flags & TPF_FORCE_CREDITS) 521 send_rx_modulate(sc, toep); 522 } 523 524 void 525 t4_rcvd(struct toedev *tod, struct tcpcb *tp) 526 { 527 struct inpcb *inp = tp->t_inpcb; 528 struct socket *so = inp->inp_socket; 529 struct sockbuf *sb = &so->so_rcv; 530 531 SOCKBUF_LOCK(sb); 532 t4_rcvd_locked(tod, tp); 533 SOCKBUF_UNLOCK(sb); 534 } 535 536 /* 537 * Close a connection by sending a CPL_CLOSE_CON_REQ message. 538 */ 539 int 540 t4_close_conn(struct adapter *sc, struct toepcb *toep) 541 { 542 struct wrqe *wr; 543 struct cpl_close_con_req *req; 544 unsigned int tid = toep->tid; 545 546 CTR3(KTR_CXGBE, "%s: tid %u%s", __func__, toep->tid, 547 toep->flags & TPF_FIN_SENT ? ", IGNORED" : ""); 548 549 if (toep->flags & TPF_FIN_SENT) 550 return (0); 551 552 KASSERT(toep->flags & TPF_FLOWC_WR_SENT, 553 ("%s: flowc_wr not sent for tid %u.", __func__, tid)); 554 555 wr = alloc_wrqe(sizeof(*req), toep->ofld_txq); 556 if (wr == NULL) { 557 /* XXX */ 558 panic("%s: allocation failure.", __func__); 559 } 560 req = wrtod(wr); 561 562 req->wr.wr_hi = htonl(V_FW_WR_OP(FW_TP_WR) | 563 V_FW_WR_IMMDLEN(sizeof(*req) - sizeof(req->wr))); 564 req->wr.wr_mid = htonl(V_FW_WR_LEN16(howmany(sizeof(*req), 16)) | 565 V_FW_WR_FLOWID(tid)); 566 req->wr.wr_lo = cpu_to_be64(0); 567 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_CON_REQ, tid)); 568 req->rsvd = 0; 569 570 toep->flags |= TPF_FIN_SENT; 571 toep->flags &= ~TPF_SEND_FIN; 572 t4_l2t_send(sc, wr, toep->l2te); 573 574 return (0); 575 } 576 577 #define MAX_OFLD_TX_CREDITS (SGE_MAX_WR_LEN / 16) 578 #define MIN_OFLD_TX_CREDITS (howmany(sizeof(struct fw_ofld_tx_data_wr) + 1, 16)) 579 580 /* Maximum amount of immediate data we could stuff in a WR */ 581 static inline int 582 max_imm_payload(int tx_credits) 583 { 584 const int n = 2; /* Use only up to 2 desc for imm. data WR */ 585 586 KASSERT(tx_credits >= 0 && 587 tx_credits <= MAX_OFLD_TX_CREDITS, 588 ("%s: %d credits", __func__, tx_credits)); 589 590 if (tx_credits < MIN_OFLD_TX_CREDITS) 591 return (0); 592 593 if (tx_credits >= (n * EQ_ESIZE) / 16) 594 return ((n * EQ_ESIZE) - sizeof(struct fw_ofld_tx_data_wr)); 595 else 596 return (tx_credits * 16 - sizeof(struct fw_ofld_tx_data_wr)); 597 } 598 599 /* Maximum number of SGL entries we could stuff in a WR */ 600 static inline int 601 max_dsgl_nsegs(int tx_credits) 602 { 603 int nseg = 1; /* ulptx_sgl has room for 1, rest ulp_tx_sge_pair */ 604 int sge_pair_credits = tx_credits - MIN_OFLD_TX_CREDITS; 605 606 KASSERT(tx_credits >= 0 && 607 tx_credits <= MAX_OFLD_TX_CREDITS, 608 ("%s: %d credits", __func__, tx_credits)); 609 610 if (tx_credits < MIN_OFLD_TX_CREDITS) 611 return (0); 612 613 nseg += 2 * (sge_pair_credits * 16 / 24); 614 if ((sge_pair_credits * 16) % 24 == 16) 615 nseg++; 616 617 return (nseg); 618 } 619 620 static inline void 621 write_tx_wr(void *dst, struct toepcb *toep, unsigned int immdlen, 622 unsigned int plen, uint8_t credits, int shove, int ulp_submode, int txalign) 623 { 624 struct fw_ofld_tx_data_wr *txwr = dst; 625 626 txwr->op_to_immdlen = htobe32(V_WR_OP(FW_OFLD_TX_DATA_WR) | 627 V_FW_WR_IMMDLEN(immdlen)); 628 txwr->flowid_len16 = htobe32(V_FW_WR_FLOWID(toep->tid) | 629 V_FW_WR_LEN16(credits)); 630 txwr->lsodisable_to_flags = htobe32(V_TX_ULP_MODE(toep->ulp_mode) | 631 V_TX_ULP_SUBMODE(ulp_submode) | V_TX_URG(0) | V_TX_SHOVE(shove)); 632 txwr->plen = htobe32(plen); 633 634 if (txalign > 0) { 635 struct tcpcb *tp = intotcpcb(toep->inp); 636 637 if (plen < 2 * tp->t_maxseg || is_10G_port(toep->vi->pi)) 638 txwr->lsodisable_to_flags |= 639 htobe32(F_FW_OFLD_TX_DATA_WR_LSODISABLE); 640 else 641 txwr->lsodisable_to_flags |= 642 htobe32(F_FW_OFLD_TX_DATA_WR_ALIGNPLD | 643 (tp->t_flags & TF_NODELAY ? 0 : 644 F_FW_OFLD_TX_DATA_WR_ALIGNPLDSHOVE)); 645 } 646 } 647 648 /* 649 * Generate a DSGL from a starting mbuf. The total number of segments and the 650 * maximum segments in any one mbuf are provided. 651 */ 652 static void 653 write_tx_sgl(void *dst, struct mbuf *start, struct mbuf *stop, int nsegs, int n) 654 { 655 struct mbuf *m; 656 struct ulptx_sgl *usgl = dst; 657 int i, j, rc; 658 struct sglist sg; 659 struct sglist_seg segs[n]; 660 661 KASSERT(nsegs > 0, ("%s: nsegs 0", __func__)); 662 663 sglist_init(&sg, n, segs); 664 usgl->cmd_nsge = htobe32(V_ULPTX_CMD(ULP_TX_SC_DSGL) | 665 V_ULPTX_NSGE(nsegs)); 666 667 i = -1; 668 for (m = start; m != stop; m = m->m_next) { 669 if (IS_AIOTX_MBUF(m)) 670 rc = sglist_append_vmpages(&sg, aiotx_mbuf_pages(m), 671 aiotx_mbuf_pgoff(m), m->m_len); 672 else 673 rc = sglist_append(&sg, mtod(m, void *), m->m_len); 674 if (__predict_false(rc != 0)) 675 panic("%s: sglist_append %d", __func__, rc); 676 677 for (j = 0; j < sg.sg_nseg; i++, j++) { 678 if (i < 0) { 679 usgl->len0 = htobe32(segs[j].ss_len); 680 usgl->addr0 = htobe64(segs[j].ss_paddr); 681 } else { 682 usgl->sge[i / 2].len[i & 1] = 683 htobe32(segs[j].ss_len); 684 usgl->sge[i / 2].addr[i & 1] = 685 htobe64(segs[j].ss_paddr); 686 } 687 #ifdef INVARIANTS 688 nsegs--; 689 #endif 690 } 691 sglist_reset(&sg); 692 } 693 if (i & 1) 694 usgl->sge[i / 2].len[1] = htobe32(0); 695 KASSERT(nsegs == 0, ("%s: nsegs %d, start %p, stop %p", 696 __func__, nsegs, start, stop)); 697 } 698 699 /* 700 * Max number of SGL entries an offload tx work request can have. This is 41 701 * (1 + 40) for a full 512B work request. 702 * fw_ofld_tx_data_wr(16B) + ulptx_sgl(16B, 1) + ulptx_sge_pair(480B, 40) 703 */ 704 #define OFLD_SGL_LEN (41) 705 706 /* 707 * Send data and/or a FIN to the peer. 708 * 709 * The socket's so_snd buffer consists of a stream of data starting with sb_mb 710 * and linked together with m_next. sb_sndptr, if set, is the last mbuf that 711 * was transmitted. 712 * 713 * drop indicates the number of bytes that should be dropped from the head of 714 * the send buffer. It is an optimization that lets do_fw4_ack avoid creating 715 * contention on the send buffer lock (before this change it used to do 716 * sowwakeup and then t4_push_frames right after that when recovering from tx 717 * stalls). When drop is set this function MUST drop the bytes and wake up any 718 * writers. 719 */ 720 void 721 t4_push_frames(struct adapter *sc, struct toepcb *toep, int drop) 722 { 723 struct mbuf *sndptr, *m, *sb_sndptr; 724 struct fw_ofld_tx_data_wr *txwr; 725 struct wrqe *wr; 726 u_int plen, nsegs, credits, max_imm, max_nsegs, max_nsegs_1mbuf; 727 struct inpcb *inp = toep->inp; 728 struct tcpcb *tp = intotcpcb(inp); 729 struct socket *so = inp->inp_socket; 730 struct sockbuf *sb = &so->so_snd; 731 int tx_credits, shove, compl, sowwakeup; 732 struct ofld_tx_sdesc *txsd; 733 bool aiotx_mbuf_seen; 734 735 INP_WLOCK_ASSERT(inp); 736 KASSERT(toep->flags & TPF_FLOWC_WR_SENT, 737 ("%s: flowc_wr not sent for tid %u.", __func__, toep->tid)); 738 739 KASSERT(toep->ulp_mode == ULP_MODE_NONE || 740 toep->ulp_mode == ULP_MODE_TCPDDP || 741 toep->ulp_mode == ULP_MODE_TLS || 742 toep->ulp_mode == ULP_MODE_RDMA, 743 ("%s: ulp_mode %u for toep %p", __func__, toep->ulp_mode, toep)); 744 745 #ifdef VERBOSE_TRACES 746 CTR4(KTR_CXGBE, "%s: tid %d toep flags %#x tp flags %#x drop %d", 747 __func__, toep->tid, toep->flags, tp->t_flags); 748 #endif 749 if (__predict_false(toep->flags & TPF_ABORT_SHUTDOWN)) 750 return; 751 752 #ifdef RATELIMIT 753 if (__predict_false(inp->inp_flags2 & INP_RATE_LIMIT_CHANGED) && 754 (update_tx_rate_limit(sc, toep, so->so_max_pacing_rate) == 0)) { 755 inp->inp_flags2 &= ~INP_RATE_LIMIT_CHANGED; 756 } 757 #endif 758 759 /* 760 * This function doesn't resume by itself. Someone else must clear the 761 * flag and call this function. 762 */ 763 if (__predict_false(toep->flags & TPF_TX_SUSPENDED)) { 764 KASSERT(drop == 0, 765 ("%s: drop (%d) != 0 but tx is suspended", __func__, drop)); 766 return; 767 } 768 769 txsd = &toep->txsd[toep->txsd_pidx]; 770 do { 771 tx_credits = min(toep->tx_credits, MAX_OFLD_TX_CREDITS); 772 max_imm = max_imm_payload(tx_credits); 773 max_nsegs = max_dsgl_nsegs(tx_credits); 774 775 SOCKBUF_LOCK(sb); 776 sowwakeup = drop; 777 if (drop) { 778 sbdrop_locked(sb, drop); 779 drop = 0; 780 } 781 sb_sndptr = sb->sb_sndptr; 782 sndptr = sb_sndptr ? sb_sndptr->m_next : sb->sb_mb; 783 plen = 0; 784 nsegs = 0; 785 max_nsegs_1mbuf = 0; /* max # of SGL segments in any one mbuf */ 786 aiotx_mbuf_seen = false; 787 for (m = sndptr; m != NULL; m = m->m_next) { 788 int n; 789 790 if (IS_AIOTX_MBUF(m)) 791 n = sglist_count_vmpages(aiotx_mbuf_pages(m), 792 aiotx_mbuf_pgoff(m), m->m_len); 793 else 794 n = sglist_count(mtod(m, void *), m->m_len); 795 796 nsegs += n; 797 plen += m->m_len; 798 799 /* This mbuf sent us _over_ the nsegs limit, back out */ 800 if (plen > max_imm && nsegs > max_nsegs) { 801 nsegs -= n; 802 plen -= m->m_len; 803 if (plen == 0) { 804 /* Too few credits */ 805 toep->flags |= TPF_TX_SUSPENDED; 806 if (sowwakeup) { 807 if (!TAILQ_EMPTY( 808 &toep->aiotx_jobq)) 809 t4_aiotx_queue_toep( 810 toep); 811 sowwakeup_locked(so); 812 } else 813 SOCKBUF_UNLOCK(sb); 814 SOCKBUF_UNLOCK_ASSERT(sb); 815 return; 816 } 817 break; 818 } 819 820 if (IS_AIOTX_MBUF(m)) 821 aiotx_mbuf_seen = true; 822 if (max_nsegs_1mbuf < n) 823 max_nsegs_1mbuf = n; 824 sb_sndptr = m; /* new sb->sb_sndptr if all goes well */ 825 826 /* This mbuf put us right at the max_nsegs limit */ 827 if (plen > max_imm && nsegs == max_nsegs) { 828 m = m->m_next; 829 break; 830 } 831 } 832 833 if (sbused(sb) > sb->sb_hiwat * 5 / 8 && 834 toep->plen_nocompl + plen >= sb->sb_hiwat / 4) 835 compl = 1; 836 else 837 compl = 0; 838 839 if (sb->sb_flags & SB_AUTOSIZE && 840 V_tcp_do_autosndbuf && 841 sb->sb_hiwat < V_tcp_autosndbuf_max && 842 sbused(sb) >= sb->sb_hiwat * 7 / 8) { 843 int newsize = min(sb->sb_hiwat + V_tcp_autosndbuf_inc, 844 V_tcp_autosndbuf_max); 845 846 if (!sbreserve_locked(sb, newsize, so, NULL)) 847 sb->sb_flags &= ~SB_AUTOSIZE; 848 else 849 sowwakeup = 1; /* room available */ 850 } 851 if (sowwakeup) { 852 if (!TAILQ_EMPTY(&toep->aiotx_jobq)) 853 t4_aiotx_queue_toep(toep); 854 sowwakeup_locked(so); 855 } else 856 SOCKBUF_UNLOCK(sb); 857 SOCKBUF_UNLOCK_ASSERT(sb); 858 859 /* nothing to send */ 860 if (plen == 0) { 861 KASSERT(m == NULL, 862 ("%s: nothing to send, but m != NULL", __func__)); 863 break; 864 } 865 866 if (__predict_false(toep->flags & TPF_FIN_SENT)) 867 panic("%s: excess tx.", __func__); 868 869 shove = m == NULL && !(tp->t_flags & TF_MORETOCOME); 870 if (plen <= max_imm && !aiotx_mbuf_seen) { 871 872 /* Immediate data tx */ 873 874 wr = alloc_wrqe(roundup2(sizeof(*txwr) + plen, 16), 875 toep->ofld_txq); 876 if (wr == NULL) { 877 /* XXX: how will we recover from this? */ 878 toep->flags |= TPF_TX_SUSPENDED; 879 return; 880 } 881 txwr = wrtod(wr); 882 credits = howmany(wr->wr_len, 16); 883 write_tx_wr(txwr, toep, plen, plen, credits, shove, 0, 884 sc->tt.tx_align); 885 m_copydata(sndptr, 0, plen, (void *)(txwr + 1)); 886 nsegs = 0; 887 } else { 888 int wr_len; 889 890 /* DSGL tx */ 891 892 wr_len = sizeof(*txwr) + sizeof(struct ulptx_sgl) + 893 ((3 * (nsegs - 1)) / 2 + ((nsegs - 1) & 1)) * 8; 894 wr = alloc_wrqe(roundup2(wr_len, 16), toep->ofld_txq); 895 if (wr == NULL) { 896 /* XXX: how will we recover from this? */ 897 toep->flags |= TPF_TX_SUSPENDED; 898 return; 899 } 900 txwr = wrtod(wr); 901 credits = howmany(wr_len, 16); 902 write_tx_wr(txwr, toep, 0, plen, credits, shove, 0, 903 sc->tt.tx_align); 904 write_tx_sgl(txwr + 1, sndptr, m, nsegs, 905 max_nsegs_1mbuf); 906 if (wr_len & 0xf) { 907 uint64_t *pad = (uint64_t *) 908 ((uintptr_t)txwr + wr_len); 909 *pad = 0; 910 } 911 } 912 913 KASSERT(toep->tx_credits >= credits, 914 ("%s: not enough credits", __func__)); 915 916 toep->tx_credits -= credits; 917 toep->tx_nocompl += credits; 918 toep->plen_nocompl += plen; 919 if (toep->tx_credits <= toep->tx_total * 3 / 8 && 920 toep->tx_nocompl >= toep->tx_total / 4) 921 compl = 1; 922 923 if (compl || toep->ulp_mode == ULP_MODE_RDMA) { 924 txwr->op_to_immdlen |= htobe32(F_FW_WR_COMPL); 925 toep->tx_nocompl = 0; 926 toep->plen_nocompl = 0; 927 } 928 929 tp->snd_nxt += plen; 930 tp->snd_max += plen; 931 932 SOCKBUF_LOCK(sb); 933 KASSERT(sb_sndptr, ("%s: sb_sndptr is NULL", __func__)); 934 sb->sb_sndptr = sb_sndptr; 935 SOCKBUF_UNLOCK(sb); 936 937 toep->flags |= TPF_TX_DATA_SENT; 938 if (toep->tx_credits < MIN_OFLD_TX_CREDITS) 939 toep->flags |= TPF_TX_SUSPENDED; 940 941 KASSERT(toep->txsd_avail > 0, ("%s: no txsd", __func__)); 942 txsd->plen = plen; 943 txsd->tx_credits = credits; 944 txsd++; 945 if (__predict_false(++toep->txsd_pidx == toep->txsd_total)) { 946 toep->txsd_pidx = 0; 947 txsd = &toep->txsd[0]; 948 } 949 toep->txsd_avail--; 950 951 t4_l2t_send(sc, wr, toep->l2te); 952 } while (m != NULL); 953 954 /* Send a FIN if requested, but only if there's no more data to send */ 955 if (m == NULL && toep->flags & TPF_SEND_FIN) 956 t4_close_conn(sc, toep); 957 } 958 959 static inline void 960 rqdrop_locked(struct mbufq *q, int plen) 961 { 962 struct mbuf *m; 963 964 while (plen > 0) { 965 m = mbufq_dequeue(q); 966 967 /* Too many credits. */ 968 MPASS(m != NULL); 969 M_ASSERTPKTHDR(m); 970 971 /* Partial credits. */ 972 MPASS(plen >= m->m_pkthdr.len); 973 974 plen -= m->m_pkthdr.len; 975 m_freem(m); 976 } 977 } 978 979 void 980 t4_push_pdus(struct adapter *sc, struct toepcb *toep, int drop) 981 { 982 struct mbuf *sndptr, *m; 983 struct fw_ofld_tx_data_wr *txwr; 984 struct wrqe *wr; 985 u_int plen, nsegs, credits, max_imm, max_nsegs, max_nsegs_1mbuf; 986 u_int adjusted_plen, ulp_submode; 987 struct inpcb *inp = toep->inp; 988 struct tcpcb *tp = intotcpcb(inp); 989 int tx_credits, shove; 990 struct ofld_tx_sdesc *txsd = &toep->txsd[toep->txsd_pidx]; 991 struct mbufq *pduq = &toep->ulp_pduq; 992 static const u_int ulp_extra_len[] = {0, 4, 4, 8}; 993 994 INP_WLOCK_ASSERT(inp); 995 KASSERT(toep->flags & TPF_FLOWC_WR_SENT, 996 ("%s: flowc_wr not sent for tid %u.", __func__, toep->tid)); 997 KASSERT(toep->ulp_mode == ULP_MODE_ISCSI, 998 ("%s: ulp_mode %u for toep %p", __func__, toep->ulp_mode, toep)); 999 1000 if (__predict_false(toep->flags & TPF_ABORT_SHUTDOWN)) 1001 return; 1002 1003 /* 1004 * This function doesn't resume by itself. Someone else must clear the 1005 * flag and call this function. 1006 */ 1007 if (__predict_false(toep->flags & TPF_TX_SUSPENDED)) { 1008 KASSERT(drop == 0, 1009 ("%s: drop (%d) != 0 but tx is suspended", __func__, drop)); 1010 return; 1011 } 1012 1013 if (drop) 1014 rqdrop_locked(&toep->ulp_pdu_reclaimq, drop); 1015 1016 while ((sndptr = mbufq_first(pduq)) != NULL) { 1017 M_ASSERTPKTHDR(sndptr); 1018 1019 tx_credits = min(toep->tx_credits, MAX_OFLD_TX_CREDITS); 1020 max_imm = max_imm_payload(tx_credits); 1021 max_nsegs = max_dsgl_nsegs(tx_credits); 1022 1023 plen = 0; 1024 nsegs = 0; 1025 max_nsegs_1mbuf = 0; /* max # of SGL segments in any one mbuf */ 1026 for (m = sndptr; m != NULL; m = m->m_next) { 1027 int n = sglist_count(mtod(m, void *), m->m_len); 1028 1029 nsegs += n; 1030 plen += m->m_len; 1031 1032 /* 1033 * This mbuf would send us _over_ the nsegs limit. 1034 * Suspend tx because the PDU can't be sent out. 1035 */ 1036 if (plen > max_imm && nsegs > max_nsegs) { 1037 toep->flags |= TPF_TX_SUSPENDED; 1038 return; 1039 } 1040 1041 if (max_nsegs_1mbuf < n) 1042 max_nsegs_1mbuf = n; 1043 } 1044 1045 if (__predict_false(toep->flags & TPF_FIN_SENT)) 1046 panic("%s: excess tx.", __func__); 1047 1048 /* 1049 * We have a PDU to send. All of it goes out in one WR so 'm' 1050 * is NULL. A PDU's length is always a multiple of 4. 1051 */ 1052 MPASS(m == NULL); 1053 MPASS((plen & 3) == 0); 1054 MPASS(sndptr->m_pkthdr.len == plen); 1055 1056 shove = !(tp->t_flags & TF_MORETOCOME); 1057 ulp_submode = mbuf_ulp_submode(sndptr); 1058 MPASS(ulp_submode < nitems(ulp_extra_len)); 1059 1060 /* 1061 * plen doesn't include header and data digests, which are 1062 * generated and inserted in the right places by the TOE, but 1063 * they do occupy TCP sequence space and need to be accounted 1064 * for. 1065 */ 1066 adjusted_plen = plen + ulp_extra_len[ulp_submode]; 1067 if (plen <= max_imm) { 1068 1069 /* Immediate data tx */ 1070 1071 wr = alloc_wrqe(roundup2(sizeof(*txwr) + plen, 16), 1072 toep->ofld_txq); 1073 if (wr == NULL) { 1074 /* XXX: how will we recover from this? */ 1075 toep->flags |= TPF_TX_SUSPENDED; 1076 return; 1077 } 1078 txwr = wrtod(wr); 1079 credits = howmany(wr->wr_len, 16); 1080 write_tx_wr(txwr, toep, plen, adjusted_plen, credits, 1081 shove, ulp_submode, sc->tt.tx_align); 1082 m_copydata(sndptr, 0, plen, (void *)(txwr + 1)); 1083 nsegs = 0; 1084 } else { 1085 int wr_len; 1086 1087 /* DSGL tx */ 1088 wr_len = sizeof(*txwr) + sizeof(struct ulptx_sgl) + 1089 ((3 * (nsegs - 1)) / 2 + ((nsegs - 1) & 1)) * 8; 1090 wr = alloc_wrqe(roundup2(wr_len, 16), toep->ofld_txq); 1091 if (wr == NULL) { 1092 /* XXX: how will we recover from this? */ 1093 toep->flags |= TPF_TX_SUSPENDED; 1094 return; 1095 } 1096 txwr = wrtod(wr); 1097 credits = howmany(wr_len, 16); 1098 write_tx_wr(txwr, toep, 0, adjusted_plen, credits, 1099 shove, ulp_submode, sc->tt.tx_align); 1100 write_tx_sgl(txwr + 1, sndptr, m, nsegs, 1101 max_nsegs_1mbuf); 1102 if (wr_len & 0xf) { 1103 uint64_t *pad = (uint64_t *) 1104 ((uintptr_t)txwr + wr_len); 1105 *pad = 0; 1106 } 1107 } 1108 1109 KASSERT(toep->tx_credits >= credits, 1110 ("%s: not enough credits", __func__)); 1111 1112 m = mbufq_dequeue(pduq); 1113 MPASS(m == sndptr); 1114 mbufq_enqueue(&toep->ulp_pdu_reclaimq, m); 1115 1116 toep->tx_credits -= credits; 1117 toep->tx_nocompl += credits; 1118 toep->plen_nocompl += plen; 1119 if (toep->tx_credits <= toep->tx_total * 3 / 8 && 1120 toep->tx_nocompl >= toep->tx_total / 4) { 1121 txwr->op_to_immdlen |= htobe32(F_FW_WR_COMPL); 1122 toep->tx_nocompl = 0; 1123 toep->plen_nocompl = 0; 1124 } 1125 1126 tp->snd_nxt += adjusted_plen; 1127 tp->snd_max += adjusted_plen; 1128 1129 toep->flags |= TPF_TX_DATA_SENT; 1130 if (toep->tx_credits < MIN_OFLD_TX_CREDITS) 1131 toep->flags |= TPF_TX_SUSPENDED; 1132 1133 KASSERT(toep->txsd_avail > 0, ("%s: no txsd", __func__)); 1134 txsd->plen = plen; 1135 txsd->tx_credits = credits; 1136 txsd++; 1137 if (__predict_false(++toep->txsd_pidx == toep->txsd_total)) { 1138 toep->txsd_pidx = 0; 1139 txsd = &toep->txsd[0]; 1140 } 1141 toep->txsd_avail--; 1142 1143 t4_l2t_send(sc, wr, toep->l2te); 1144 } 1145 1146 /* Send a FIN if requested, but only if there are no more PDUs to send */ 1147 if (mbufq_first(pduq) == NULL && toep->flags & TPF_SEND_FIN) 1148 t4_close_conn(sc, toep); 1149 } 1150 1151 int 1152 t4_tod_output(struct toedev *tod, struct tcpcb *tp) 1153 { 1154 struct adapter *sc = tod->tod_softc; 1155 #ifdef INVARIANTS 1156 struct inpcb *inp = tp->t_inpcb; 1157 #endif 1158 struct toepcb *toep = tp->t_toe; 1159 1160 INP_WLOCK_ASSERT(inp); 1161 KASSERT((inp->inp_flags & INP_DROPPED) == 0, 1162 ("%s: inp %p dropped.", __func__, inp)); 1163 KASSERT(toep != NULL, ("%s: toep is NULL", __func__)); 1164 1165 if (toep->ulp_mode == ULP_MODE_ISCSI) 1166 t4_push_pdus(sc, toep, 0); 1167 else if (tls_tx_key(toep)) 1168 t4_push_tls_records(sc, toep, 0); 1169 else 1170 t4_push_frames(sc, toep, 0); 1171 1172 return (0); 1173 } 1174 1175 int 1176 t4_send_fin(struct toedev *tod, struct tcpcb *tp) 1177 { 1178 struct adapter *sc = tod->tod_softc; 1179 #ifdef INVARIANTS 1180 struct inpcb *inp = tp->t_inpcb; 1181 #endif 1182 struct toepcb *toep = tp->t_toe; 1183 1184 INP_WLOCK_ASSERT(inp); 1185 KASSERT((inp->inp_flags & INP_DROPPED) == 0, 1186 ("%s: inp %p dropped.", __func__, inp)); 1187 KASSERT(toep != NULL, ("%s: toep is NULL", __func__)); 1188 1189 toep->flags |= TPF_SEND_FIN; 1190 if (tp->t_state >= TCPS_ESTABLISHED) { 1191 if (toep->ulp_mode == ULP_MODE_ISCSI) 1192 t4_push_pdus(sc, toep, 0); 1193 else if (tls_tx_key(toep)) 1194 t4_push_tls_records(sc, toep, 0); 1195 else 1196 t4_push_frames(sc, toep, 0); 1197 } 1198 1199 return (0); 1200 } 1201 1202 int 1203 t4_send_rst(struct toedev *tod, struct tcpcb *tp) 1204 { 1205 struct adapter *sc = tod->tod_softc; 1206 #if defined(INVARIANTS) 1207 struct inpcb *inp = tp->t_inpcb; 1208 #endif 1209 struct toepcb *toep = tp->t_toe; 1210 1211 INP_WLOCK_ASSERT(inp); 1212 KASSERT((inp->inp_flags & INP_DROPPED) == 0, 1213 ("%s: inp %p dropped.", __func__, inp)); 1214 KASSERT(toep != NULL, ("%s: toep is NULL", __func__)); 1215 1216 /* hmmmm */ 1217 KASSERT(toep->flags & TPF_FLOWC_WR_SENT, 1218 ("%s: flowc for tid %u [%s] not sent already", 1219 __func__, toep->tid, tcpstates[tp->t_state])); 1220 1221 send_reset(sc, toep, 0); 1222 return (0); 1223 } 1224 1225 /* 1226 * Peer has sent us a FIN. 1227 */ 1228 static int 1229 do_peer_close(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m) 1230 { 1231 struct adapter *sc = iq->adapter; 1232 const struct cpl_peer_close *cpl = (const void *)(rss + 1); 1233 unsigned int tid = GET_TID(cpl); 1234 struct toepcb *toep = lookup_tid(sc, tid); 1235 struct inpcb *inp = toep->inp; 1236 struct tcpcb *tp = NULL; 1237 struct socket *so; 1238 struct epoch_tracker et; 1239 #ifdef INVARIANTS 1240 unsigned int opcode = G_CPL_OPCODE(be32toh(OPCODE_TID(cpl))); 1241 #endif 1242 1243 KASSERT(opcode == CPL_PEER_CLOSE, 1244 ("%s: unexpected opcode 0x%x", __func__, opcode)); 1245 KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__)); 1246 1247 if (__predict_false(toep->flags & TPF_SYNQE)) { 1248 #ifdef INVARIANTS 1249 struct synq_entry *synqe = (void *)toep; 1250 1251 INP_WLOCK(synqe->lctx->inp); 1252 if (synqe->flags & TPF_SYNQE_HAS_L2TE) { 1253 KASSERT(synqe->flags & TPF_ABORT_SHUTDOWN, 1254 ("%s: listen socket closed but tid %u not aborted.", 1255 __func__, tid)); 1256 } else { 1257 /* 1258 * do_pass_accept_req is still running and will 1259 * eventually take care of this tid. 1260 */ 1261 } 1262 INP_WUNLOCK(synqe->lctx->inp); 1263 #endif 1264 CTR4(KTR_CXGBE, "%s: tid %u, synqe %p (0x%x)", __func__, tid, 1265 toep, toep->flags); 1266 return (0); 1267 } 1268 1269 KASSERT(toep->tid == tid, ("%s: toep tid mismatch", __func__)); 1270 1271 CURVNET_SET(toep->vnet); 1272 INP_INFO_RLOCK_ET(&V_tcbinfo, et); 1273 INP_WLOCK(inp); 1274 tp = intotcpcb(inp); 1275 1276 CTR5(KTR_CXGBE, "%s: tid %u (%s), toep_flags 0x%x, inp %p", __func__, 1277 tid, tp ? tcpstates[tp->t_state] : "no tp", toep->flags, inp); 1278 1279 if (toep->flags & TPF_ABORT_SHUTDOWN) 1280 goto done; 1281 1282 tp->rcv_nxt++; /* FIN */ 1283 1284 so = inp->inp_socket; 1285 if (toep->ulp_mode == ULP_MODE_TCPDDP) { 1286 DDP_LOCK(toep); 1287 if (__predict_false(toep->ddp.flags & 1288 (DDP_BUF0_ACTIVE | DDP_BUF1_ACTIVE))) 1289 handle_ddp_close(toep, tp, cpl->rcv_nxt); 1290 DDP_UNLOCK(toep); 1291 } 1292 socantrcvmore(so); 1293 1294 if (toep->ulp_mode != ULP_MODE_RDMA) { 1295 KASSERT(tp->rcv_nxt == be32toh(cpl->rcv_nxt), 1296 ("%s: rcv_nxt mismatch: %u %u", __func__, tp->rcv_nxt, 1297 be32toh(cpl->rcv_nxt))); 1298 } 1299 1300 switch (tp->t_state) { 1301 case TCPS_SYN_RECEIVED: 1302 tp->t_starttime = ticks; 1303 /* FALLTHROUGH */ 1304 1305 case TCPS_ESTABLISHED: 1306 tp->t_state = TCPS_CLOSE_WAIT; 1307 break; 1308 1309 case TCPS_FIN_WAIT_1: 1310 tp->t_state = TCPS_CLOSING; 1311 break; 1312 1313 case TCPS_FIN_WAIT_2: 1314 tcp_twstart(tp); 1315 INP_UNLOCK_ASSERT(inp); /* safe, we have a ref on the inp */ 1316 INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 1317 CURVNET_RESTORE(); 1318 1319 INP_WLOCK(inp); 1320 final_cpl_received(toep); 1321 return (0); 1322 1323 default: 1324 log(LOG_ERR, "%s: TID %u received CPL_PEER_CLOSE in state %d\n", 1325 __func__, tid, tp->t_state); 1326 } 1327 done: 1328 INP_WUNLOCK(inp); 1329 INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 1330 CURVNET_RESTORE(); 1331 return (0); 1332 } 1333 1334 /* 1335 * Peer has ACK'd our FIN. 1336 */ 1337 static int 1338 do_close_con_rpl(struct sge_iq *iq, const struct rss_header *rss, 1339 struct mbuf *m) 1340 { 1341 struct adapter *sc = iq->adapter; 1342 const struct cpl_close_con_rpl *cpl = (const void *)(rss + 1); 1343 unsigned int tid = GET_TID(cpl); 1344 struct toepcb *toep = lookup_tid(sc, tid); 1345 struct inpcb *inp = toep->inp; 1346 struct tcpcb *tp = NULL; 1347 struct socket *so = NULL; 1348 struct epoch_tracker et; 1349 #ifdef INVARIANTS 1350 unsigned int opcode = G_CPL_OPCODE(be32toh(OPCODE_TID(cpl))); 1351 #endif 1352 1353 KASSERT(opcode == CPL_CLOSE_CON_RPL, 1354 ("%s: unexpected opcode 0x%x", __func__, opcode)); 1355 KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__)); 1356 KASSERT(toep->tid == tid, ("%s: toep tid mismatch", __func__)); 1357 1358 CURVNET_SET(toep->vnet); 1359 INP_INFO_RLOCK_ET(&V_tcbinfo, et); 1360 INP_WLOCK(inp); 1361 tp = intotcpcb(inp); 1362 1363 CTR4(KTR_CXGBE, "%s: tid %u (%s), toep_flags 0x%x", 1364 __func__, tid, tp ? tcpstates[tp->t_state] : "no tp", toep->flags); 1365 1366 if (toep->flags & TPF_ABORT_SHUTDOWN) 1367 goto done; 1368 1369 so = inp->inp_socket; 1370 tp->snd_una = be32toh(cpl->snd_nxt) - 1; /* exclude FIN */ 1371 1372 switch (tp->t_state) { 1373 case TCPS_CLOSING: /* see TCPS_FIN_WAIT_2 in do_peer_close too */ 1374 tcp_twstart(tp); 1375 release: 1376 INP_UNLOCK_ASSERT(inp); /* safe, we have a ref on the inp */ 1377 INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 1378 CURVNET_RESTORE(); 1379 1380 INP_WLOCK(inp); 1381 final_cpl_received(toep); /* no more CPLs expected */ 1382 1383 return (0); 1384 case TCPS_LAST_ACK: 1385 if (tcp_close(tp)) 1386 INP_WUNLOCK(inp); 1387 goto release; 1388 1389 case TCPS_FIN_WAIT_1: 1390 if (so->so_rcv.sb_state & SBS_CANTRCVMORE) 1391 soisdisconnected(so); 1392 tp->t_state = TCPS_FIN_WAIT_2; 1393 break; 1394 1395 default: 1396 log(LOG_ERR, 1397 "%s: TID %u received CPL_CLOSE_CON_RPL in state %s\n", 1398 __func__, tid, tcpstates[tp->t_state]); 1399 } 1400 done: 1401 INP_WUNLOCK(inp); 1402 INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 1403 CURVNET_RESTORE(); 1404 return (0); 1405 } 1406 1407 void 1408 send_abort_rpl(struct adapter *sc, struct sge_wrq *ofld_txq, int tid, 1409 int rst_status) 1410 { 1411 struct wrqe *wr; 1412 struct cpl_abort_rpl *cpl; 1413 1414 wr = alloc_wrqe(sizeof(*cpl), ofld_txq); 1415 if (wr == NULL) { 1416 /* XXX */ 1417 panic("%s: allocation failure.", __func__); 1418 } 1419 cpl = wrtod(wr); 1420 1421 INIT_TP_WR_MIT_CPL(cpl, CPL_ABORT_RPL, tid); 1422 cpl->cmd = rst_status; 1423 1424 t4_wrq_tx(sc, wr); 1425 } 1426 1427 static int 1428 abort_status_to_errno(struct tcpcb *tp, unsigned int abort_reason) 1429 { 1430 switch (abort_reason) { 1431 case CPL_ERR_BAD_SYN: 1432 case CPL_ERR_CONN_RESET: 1433 return (tp->t_state == TCPS_CLOSE_WAIT ? EPIPE : ECONNRESET); 1434 case CPL_ERR_XMIT_TIMEDOUT: 1435 case CPL_ERR_PERSIST_TIMEDOUT: 1436 case CPL_ERR_FINWAIT2_TIMEDOUT: 1437 case CPL_ERR_KEEPALIVE_TIMEDOUT: 1438 return (ETIMEDOUT); 1439 default: 1440 return (EIO); 1441 } 1442 } 1443 1444 /* 1445 * TCP RST from the peer, timeout, or some other such critical error. 1446 */ 1447 static int 1448 do_abort_req(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m) 1449 { 1450 struct adapter *sc = iq->adapter; 1451 const struct cpl_abort_req_rss *cpl = (const void *)(rss + 1); 1452 unsigned int tid = GET_TID(cpl); 1453 struct toepcb *toep = lookup_tid(sc, tid); 1454 struct sge_wrq *ofld_txq = toep->ofld_txq; 1455 struct inpcb *inp; 1456 struct tcpcb *tp; 1457 struct epoch_tracker et; 1458 #ifdef INVARIANTS 1459 unsigned int opcode = G_CPL_OPCODE(be32toh(OPCODE_TID(cpl))); 1460 #endif 1461 1462 KASSERT(opcode == CPL_ABORT_REQ_RSS, 1463 ("%s: unexpected opcode 0x%x", __func__, opcode)); 1464 KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__)); 1465 1466 if (toep->flags & TPF_SYNQE) 1467 return (do_abort_req_synqe(iq, rss, m)); 1468 1469 KASSERT(toep->tid == tid, ("%s: toep tid mismatch", __func__)); 1470 1471 if (negative_advice(cpl->status)) { 1472 CTR4(KTR_CXGBE, "%s: negative advice %d for tid %d (0x%x)", 1473 __func__, cpl->status, tid, toep->flags); 1474 return (0); /* Ignore negative advice */ 1475 } 1476 1477 inp = toep->inp; 1478 CURVNET_SET(toep->vnet); 1479 INP_INFO_RLOCK_ET(&V_tcbinfo, et); /* for tcp_close */ 1480 INP_WLOCK(inp); 1481 1482 tp = intotcpcb(inp); 1483 1484 CTR6(KTR_CXGBE, 1485 "%s: tid %d (%s), toep_flags 0x%x, inp_flags 0x%x, status %d", 1486 __func__, tid, tp ? tcpstates[tp->t_state] : "no tp", toep->flags, 1487 inp->inp_flags, cpl->status); 1488 1489 /* 1490 * If we'd initiated an abort earlier the reply to it is responsible for 1491 * cleaning up resources. Otherwise we tear everything down right here 1492 * right now. We owe the T4 a CPL_ABORT_RPL no matter what. 1493 */ 1494 if (toep->flags & TPF_ABORT_SHUTDOWN) { 1495 INP_WUNLOCK(inp); 1496 goto done; 1497 } 1498 toep->flags |= TPF_ABORT_SHUTDOWN; 1499 1500 if ((inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT)) == 0) { 1501 struct socket *so = inp->inp_socket; 1502 1503 if (so != NULL) 1504 so_error_set(so, abort_status_to_errno(tp, 1505 cpl->status)); 1506 tp = tcp_close(tp); 1507 if (tp == NULL) 1508 INP_WLOCK(inp); /* re-acquire */ 1509 } 1510 1511 final_cpl_received(toep); 1512 done: 1513 INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 1514 CURVNET_RESTORE(); 1515 send_abort_rpl(sc, ofld_txq, tid, CPL_ABORT_NO_RST); 1516 return (0); 1517 } 1518 1519 /* 1520 * Reply to the CPL_ABORT_REQ (send_reset) 1521 */ 1522 static int 1523 do_abort_rpl(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m) 1524 { 1525 struct adapter *sc = iq->adapter; 1526 const struct cpl_abort_rpl_rss *cpl = (const void *)(rss + 1); 1527 unsigned int tid = GET_TID(cpl); 1528 struct toepcb *toep = lookup_tid(sc, tid); 1529 struct inpcb *inp = toep->inp; 1530 #ifdef INVARIANTS 1531 unsigned int opcode = G_CPL_OPCODE(be32toh(OPCODE_TID(cpl))); 1532 #endif 1533 1534 KASSERT(opcode == CPL_ABORT_RPL_RSS, 1535 ("%s: unexpected opcode 0x%x", __func__, opcode)); 1536 KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__)); 1537 1538 if (toep->flags & TPF_SYNQE) 1539 return (do_abort_rpl_synqe(iq, rss, m)); 1540 1541 KASSERT(toep->tid == tid, ("%s: toep tid mismatch", __func__)); 1542 1543 CTR5(KTR_CXGBE, "%s: tid %u, toep %p, inp %p, status %d", 1544 __func__, tid, toep, inp, cpl->status); 1545 1546 KASSERT(toep->flags & TPF_ABORT_SHUTDOWN, 1547 ("%s: wasn't expecting abort reply", __func__)); 1548 1549 INP_WLOCK(inp); 1550 final_cpl_received(toep); 1551 1552 return (0); 1553 } 1554 1555 static int 1556 do_rx_data(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m) 1557 { 1558 struct adapter *sc = iq->adapter; 1559 const struct cpl_rx_data *cpl = mtod(m, const void *); 1560 unsigned int tid = GET_TID(cpl); 1561 struct toepcb *toep = lookup_tid(sc, tid); 1562 struct inpcb *inp = toep->inp; 1563 struct tcpcb *tp; 1564 struct socket *so; 1565 struct sockbuf *sb; 1566 struct epoch_tracker et; 1567 int len; 1568 uint32_t ddp_placed = 0; 1569 1570 if (__predict_false(toep->flags & TPF_SYNQE)) { 1571 #ifdef INVARIANTS 1572 struct synq_entry *synqe = (void *)toep; 1573 1574 INP_WLOCK(synqe->lctx->inp); 1575 if (synqe->flags & TPF_SYNQE_HAS_L2TE) { 1576 KASSERT(synqe->flags & TPF_ABORT_SHUTDOWN, 1577 ("%s: listen socket closed but tid %u not aborted.", 1578 __func__, tid)); 1579 } else { 1580 /* 1581 * do_pass_accept_req is still running and will 1582 * eventually take care of this tid. 1583 */ 1584 } 1585 INP_WUNLOCK(synqe->lctx->inp); 1586 #endif 1587 CTR4(KTR_CXGBE, "%s: tid %u, synqe %p (0x%x)", __func__, tid, 1588 toep, toep->flags); 1589 m_freem(m); 1590 return (0); 1591 } 1592 1593 KASSERT(toep->tid == tid, ("%s: toep tid mismatch", __func__)); 1594 1595 /* strip off CPL header */ 1596 m_adj(m, sizeof(*cpl)); 1597 len = m->m_pkthdr.len; 1598 1599 INP_WLOCK(inp); 1600 if (inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT)) { 1601 CTR4(KTR_CXGBE, "%s: tid %u, rx (%d bytes), inp_flags 0x%x", 1602 __func__, tid, len, inp->inp_flags); 1603 INP_WUNLOCK(inp); 1604 m_freem(m); 1605 return (0); 1606 } 1607 1608 tp = intotcpcb(inp); 1609 1610 if (__predict_false(tp->rcv_nxt != be32toh(cpl->seq))) 1611 ddp_placed = be32toh(cpl->seq) - tp->rcv_nxt; 1612 1613 tp->rcv_nxt += len; 1614 if (tp->rcv_wnd < len) { 1615 KASSERT(toep->ulp_mode == ULP_MODE_RDMA, 1616 ("%s: negative window size", __func__)); 1617 } 1618 1619 tp->rcv_wnd -= len; 1620 tp->t_rcvtime = ticks; 1621 1622 if (toep->ulp_mode == ULP_MODE_TCPDDP) 1623 DDP_LOCK(toep); 1624 so = inp_inpcbtosocket(inp); 1625 sb = &so->so_rcv; 1626 SOCKBUF_LOCK(sb); 1627 1628 if (__predict_false(sb->sb_state & SBS_CANTRCVMORE)) { 1629 CTR3(KTR_CXGBE, "%s: tid %u, excess rx (%d bytes)", 1630 __func__, tid, len); 1631 m_freem(m); 1632 SOCKBUF_UNLOCK(sb); 1633 if (toep->ulp_mode == ULP_MODE_TCPDDP) 1634 DDP_UNLOCK(toep); 1635 INP_WUNLOCK(inp); 1636 1637 CURVNET_SET(toep->vnet); 1638 INP_INFO_RLOCK_ET(&V_tcbinfo, et); 1639 INP_WLOCK(inp); 1640 tp = tcp_drop(tp, ECONNRESET); 1641 if (tp) 1642 INP_WUNLOCK(inp); 1643 INP_INFO_RUNLOCK_ET(&V_tcbinfo, et); 1644 CURVNET_RESTORE(); 1645 1646 return (0); 1647 } 1648 1649 /* receive buffer autosize */ 1650 MPASS(toep->vnet == so->so_vnet); 1651 CURVNET_SET(toep->vnet); 1652 if (sb->sb_flags & SB_AUTOSIZE && 1653 V_tcp_do_autorcvbuf && 1654 sb->sb_hiwat < V_tcp_autorcvbuf_max && 1655 len > (sbspace(sb) / 8 * 7)) { 1656 unsigned int hiwat = sb->sb_hiwat; 1657 unsigned int newsize = min(hiwat + V_tcp_autorcvbuf_inc, 1658 V_tcp_autorcvbuf_max); 1659 1660 if (!sbreserve_locked(sb, newsize, so, NULL)) 1661 sb->sb_flags &= ~SB_AUTOSIZE; 1662 else 1663 toep->rx_credits += newsize - hiwat; 1664 } 1665 1666 if (toep->ulp_mode == ULP_MODE_TCPDDP) { 1667 int changed = !(toep->ddp.flags & DDP_ON) ^ cpl->ddp_off; 1668 1669 if (toep->ddp.waiting_count != 0 || toep->ddp.active_count != 0) 1670 CTR3(KTR_CXGBE, "%s: tid %u, non-ddp rx (%d bytes)", 1671 __func__, tid, len); 1672 1673 if (changed) { 1674 if (toep->ddp.flags & DDP_SC_REQ) 1675 toep->ddp.flags ^= DDP_ON | DDP_SC_REQ; 1676 else { 1677 KASSERT(cpl->ddp_off == 1, 1678 ("%s: DDP switched on by itself.", 1679 __func__)); 1680 1681 /* Fell out of DDP mode */ 1682 toep->ddp.flags &= ~DDP_ON; 1683 CTR1(KTR_CXGBE, "%s: fell out of DDP mode", 1684 __func__); 1685 1686 insert_ddp_data(toep, ddp_placed); 1687 } 1688 } 1689 1690 if (toep->ddp.flags & DDP_ON) { 1691 /* 1692 * CPL_RX_DATA with DDP on can only be an indicate. 1693 * Start posting queued AIO requests via DDP. The 1694 * payload that arrived in this indicate is appended 1695 * to the socket buffer as usual. 1696 */ 1697 handle_ddp_indicate(toep); 1698 } 1699 } 1700 1701 KASSERT(toep->sb_cc >= sbused(sb), 1702 ("%s: sb %p has more data (%d) than last time (%d).", 1703 __func__, sb, sbused(sb), toep->sb_cc)); 1704 toep->rx_credits += toep->sb_cc - sbused(sb); 1705 sbappendstream_locked(sb, m, 0); 1706 toep->sb_cc = sbused(sb); 1707 if (toep->rx_credits > 0 && toep->sb_cc + tp->rcv_wnd < sb->sb_lowat) { 1708 int credits; 1709 1710 credits = send_rx_credits(sc, toep, toep->rx_credits); 1711 toep->rx_credits -= credits; 1712 tp->rcv_wnd += credits; 1713 tp->rcv_adv += credits; 1714 } 1715 1716 if (toep->ulp_mode == ULP_MODE_TCPDDP && toep->ddp.waiting_count > 0 && 1717 sbavail(sb) != 0) { 1718 CTR2(KTR_CXGBE, "%s: tid %u queueing AIO task", __func__, 1719 tid); 1720 ddp_queue_toep(toep); 1721 } 1722 sorwakeup_locked(so); 1723 SOCKBUF_UNLOCK_ASSERT(sb); 1724 if (toep->ulp_mode == ULP_MODE_TCPDDP) 1725 DDP_UNLOCK(toep); 1726 1727 INP_WUNLOCK(inp); 1728 CURVNET_RESTORE(); 1729 return (0); 1730 } 1731 1732 static int 1733 do_fw4_ack(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m) 1734 { 1735 struct adapter *sc = iq->adapter; 1736 const struct cpl_fw4_ack *cpl = (const void *)(rss + 1); 1737 unsigned int tid = G_CPL_FW4_ACK_FLOWID(be32toh(OPCODE_TID(cpl))); 1738 struct toepcb *toep = lookup_tid(sc, tid); 1739 struct inpcb *inp; 1740 struct tcpcb *tp; 1741 struct socket *so; 1742 uint8_t credits = cpl->credits; 1743 struct ofld_tx_sdesc *txsd; 1744 int plen; 1745 #ifdef INVARIANTS 1746 unsigned int opcode = G_CPL_FW4_ACK_OPCODE(be32toh(OPCODE_TID(cpl))); 1747 #endif 1748 1749 /* 1750 * Very unusual case: we'd sent a flowc + abort_req for a synq entry and 1751 * now this comes back carrying the credits for the flowc. 1752 */ 1753 if (__predict_false(toep->flags & TPF_SYNQE)) { 1754 KASSERT(toep->flags & TPF_ABORT_SHUTDOWN, 1755 ("%s: credits for a synq entry %p", __func__, toep)); 1756 return (0); 1757 } 1758 1759 inp = toep->inp; 1760 1761 KASSERT(opcode == CPL_FW4_ACK, 1762 ("%s: unexpected opcode 0x%x", __func__, opcode)); 1763 KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__)); 1764 KASSERT(toep->tid == tid, ("%s: toep tid mismatch", __func__)); 1765 1766 INP_WLOCK(inp); 1767 1768 if (__predict_false(toep->flags & TPF_ABORT_SHUTDOWN)) { 1769 INP_WUNLOCK(inp); 1770 return (0); 1771 } 1772 1773 KASSERT((inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) == 0, 1774 ("%s: inp_flags 0x%x", __func__, inp->inp_flags)); 1775 1776 tp = intotcpcb(inp); 1777 1778 if (cpl->flags & CPL_FW4_ACK_FLAGS_SEQVAL) { 1779 tcp_seq snd_una = be32toh(cpl->snd_una); 1780 1781 #ifdef INVARIANTS 1782 if (__predict_false(SEQ_LT(snd_una, tp->snd_una))) { 1783 log(LOG_ERR, 1784 "%s: unexpected seq# %x for TID %u, snd_una %x\n", 1785 __func__, snd_una, toep->tid, tp->snd_una); 1786 } 1787 #endif 1788 1789 if (tp->snd_una != snd_una) { 1790 tp->snd_una = snd_una; 1791 tp->ts_recent_age = tcp_ts_getticks(); 1792 } 1793 } 1794 1795 #ifdef VERBOSE_TRACES 1796 CTR3(KTR_CXGBE, "%s: tid %d credits %u", __func__, tid, credits); 1797 #endif 1798 so = inp->inp_socket; 1799 txsd = &toep->txsd[toep->txsd_cidx]; 1800 plen = 0; 1801 while (credits) { 1802 KASSERT(credits >= txsd->tx_credits, 1803 ("%s: too many (or partial) credits", __func__)); 1804 credits -= txsd->tx_credits; 1805 toep->tx_credits += txsd->tx_credits; 1806 plen += txsd->plen; 1807 if (txsd->iv_buffer) { 1808 free(txsd->iv_buffer, M_CXGBE); 1809 txsd->iv_buffer = NULL; 1810 } 1811 txsd++; 1812 toep->txsd_avail++; 1813 KASSERT(toep->txsd_avail <= toep->txsd_total, 1814 ("%s: txsd avail > total", __func__)); 1815 if (__predict_false(++toep->txsd_cidx == toep->txsd_total)) { 1816 txsd = &toep->txsd[0]; 1817 toep->txsd_cidx = 0; 1818 } 1819 } 1820 1821 if (toep->tx_credits == toep->tx_total) { 1822 toep->tx_nocompl = 0; 1823 toep->plen_nocompl = 0; 1824 } 1825 1826 if (toep->flags & TPF_TX_SUSPENDED && 1827 toep->tx_credits >= toep->tx_total / 4) { 1828 #ifdef VERBOSE_TRACES 1829 CTR2(KTR_CXGBE, "%s: tid %d calling t4_push_frames", __func__, 1830 tid); 1831 #endif 1832 toep->flags &= ~TPF_TX_SUSPENDED; 1833 CURVNET_SET(toep->vnet); 1834 if (toep->ulp_mode == ULP_MODE_ISCSI) 1835 t4_push_pdus(sc, toep, plen); 1836 else if (tls_tx_key(toep)) 1837 t4_push_tls_records(sc, toep, plen); 1838 else 1839 t4_push_frames(sc, toep, plen); 1840 CURVNET_RESTORE(); 1841 } else if (plen > 0) { 1842 struct sockbuf *sb = &so->so_snd; 1843 int sbu; 1844 1845 SOCKBUF_LOCK(sb); 1846 sbu = sbused(sb); 1847 if (toep->ulp_mode == ULP_MODE_ISCSI) { 1848 1849 if (__predict_false(sbu > 0)) { 1850 /* 1851 * The data trasmitted before the tid's ULP mode 1852 * changed to ISCSI is still in so_snd. 1853 * Incoming credits should account for so_snd 1854 * first. 1855 */ 1856 sbdrop_locked(sb, min(sbu, plen)); 1857 plen -= min(sbu, plen); 1858 } 1859 sowwakeup_locked(so); /* unlocks so_snd */ 1860 rqdrop_locked(&toep->ulp_pdu_reclaimq, plen); 1861 } else { 1862 #ifdef VERBOSE_TRACES 1863 CTR3(KTR_CXGBE, "%s: tid %d dropped %d bytes", __func__, 1864 tid, plen); 1865 #endif 1866 sbdrop_locked(sb, plen); 1867 if (tls_tx_key(toep)) { 1868 struct tls_ofld_info *tls_ofld = &toep->tls; 1869 1870 MPASS(tls_ofld->sb_off >= plen); 1871 tls_ofld->sb_off -= plen; 1872 } 1873 if (!TAILQ_EMPTY(&toep->aiotx_jobq)) 1874 t4_aiotx_queue_toep(toep); 1875 sowwakeup_locked(so); /* unlocks so_snd */ 1876 } 1877 SOCKBUF_UNLOCK_ASSERT(sb); 1878 } 1879 1880 INP_WUNLOCK(inp); 1881 1882 return (0); 1883 } 1884 1885 void 1886 t4_set_tcb_field(struct adapter *sc, struct sge_wrq *wrq, struct toepcb *toep, 1887 uint16_t word, uint64_t mask, uint64_t val, int reply, int cookie) 1888 { 1889 struct wrqe *wr; 1890 struct cpl_set_tcb_field *req; 1891 struct ofld_tx_sdesc *txsd; 1892 1893 MPASS((cookie & ~M_COOKIE) == 0); 1894 if (reply) { 1895 MPASS(cookie != CPL_COOKIE_RESERVED); 1896 } 1897 1898 wr = alloc_wrqe(sizeof(*req), wrq); 1899 if (wr == NULL) { 1900 /* XXX */ 1901 panic("%s: allocation failure.", __func__); 1902 } 1903 req = wrtod(wr); 1904 1905 INIT_TP_WR_MIT_CPL(req, CPL_SET_TCB_FIELD, toep->tid); 1906 req->reply_ctrl = htobe16(V_QUEUENO(toep->ofld_rxq->iq.abs_id)); 1907 if (reply == 0) 1908 req->reply_ctrl |= htobe16(F_NO_REPLY); 1909 req->word_cookie = htobe16(V_WORD(word) | V_COOKIE(cookie)); 1910 req->mask = htobe64(mask); 1911 req->val = htobe64(val); 1912 if ((wrq->eq.flags & EQ_TYPEMASK) == EQ_OFLD) { 1913 txsd = &toep->txsd[toep->txsd_pidx]; 1914 txsd->tx_credits = howmany(sizeof(*req), 16); 1915 txsd->plen = 0; 1916 KASSERT(toep->tx_credits >= txsd->tx_credits && 1917 toep->txsd_avail > 0, 1918 ("%s: not enough credits (%d)", __func__, 1919 toep->tx_credits)); 1920 toep->tx_credits -= txsd->tx_credits; 1921 if (__predict_false(++toep->txsd_pidx == toep->txsd_total)) 1922 toep->txsd_pidx = 0; 1923 toep->txsd_avail--; 1924 } 1925 1926 t4_wrq_tx(sc, wr); 1927 } 1928 1929 void 1930 t4_init_cpl_io_handlers(void) 1931 { 1932 1933 t4_register_cpl_handler(CPL_PEER_CLOSE, do_peer_close); 1934 t4_register_cpl_handler(CPL_CLOSE_CON_RPL, do_close_con_rpl); 1935 t4_register_cpl_handler(CPL_ABORT_REQ_RSS, do_abort_req); 1936 t4_register_shared_cpl_handler(CPL_ABORT_RPL_RSS, do_abort_rpl, 1937 CPL_COOKIE_TOM); 1938 t4_register_cpl_handler(CPL_RX_DATA, do_rx_data); 1939 t4_register_shared_cpl_handler(CPL_FW4_ACK, do_fw4_ack, CPL_COOKIE_TOM); 1940 } 1941 1942 void 1943 t4_uninit_cpl_io_handlers(void) 1944 { 1945 1946 t4_register_cpl_handler(CPL_PEER_CLOSE, NULL); 1947 t4_register_cpl_handler(CPL_CLOSE_CON_RPL, NULL); 1948 t4_register_cpl_handler(CPL_ABORT_REQ_RSS, NULL); 1949 t4_register_shared_cpl_handler(CPL_ABORT_RPL_RSS, NULL, CPL_COOKIE_TOM); 1950 t4_register_cpl_handler(CPL_RX_DATA, NULL); 1951 t4_register_shared_cpl_handler(CPL_FW4_ACK, NULL, CPL_COOKIE_TOM); 1952 } 1953 1954 /* 1955 * Use the 'backend3' field in AIO jobs to store the amount of data 1956 * sent by the AIO job so far and the 'backend4' field to hold an 1957 * error that should be reported when the job is completed. 1958 */ 1959 #define aio_sent backend3 1960 #define aio_error backend4 1961 1962 #define jobtotid(job) \ 1963 (((struct toepcb *)(so_sototcpcb((job)->fd_file->f_data)->t_toe))->tid) 1964 1965 static void 1966 free_aiotx_buffer(struct aiotx_buffer *ab) 1967 { 1968 struct kaiocb *job; 1969 long status; 1970 int error; 1971 1972 if (refcount_release(&ab->refcount) == 0) 1973 return; 1974 1975 job = ab->job; 1976 error = job->aio_error; 1977 status = job->aio_sent; 1978 vm_page_unhold_pages(ab->ps.pages, ab->ps.npages); 1979 free(ab, M_CXGBE); 1980 #ifdef VERBOSE_TRACES 1981 CTR5(KTR_CXGBE, "%s: tid %d completed %p len %ld, error %d", __func__, 1982 jobtotid(job), job, status, error); 1983 #endif 1984 if (error == ECANCELED && status != 0) 1985 error = 0; 1986 if (error == ECANCELED) 1987 aio_cancel(job); 1988 else if (error) 1989 aio_complete(job, -1, error); 1990 else 1991 aio_complete(job, status, 0); 1992 } 1993 1994 static void 1995 t4_aiotx_mbuf_free(struct mbuf *m) 1996 { 1997 struct aiotx_buffer *ab = m->m_ext.ext_arg1; 1998 1999 #ifdef VERBOSE_TRACES 2000 CTR3(KTR_CXGBE, "%s: completed %d bytes for tid %d", __func__, 2001 m->m_len, jobtotid(ab->job)); 2002 #endif 2003 free_aiotx_buffer(ab); 2004 } 2005 2006 /* 2007 * Hold the buffer backing an AIO request and return an AIO transmit 2008 * buffer. 2009 */ 2010 static int 2011 hold_aio(struct kaiocb *job) 2012 { 2013 struct aiotx_buffer *ab; 2014 struct vmspace *vm; 2015 vm_map_t map; 2016 vm_offset_t start, end, pgoff; 2017 int n; 2018 2019 MPASS(job->backend1 == NULL); 2020 2021 /* 2022 * The AIO subsystem will cancel and drain all requests before 2023 * permitting a process to exit or exec, so p_vmspace should 2024 * be stable here. 2025 */ 2026 vm = job->userproc->p_vmspace; 2027 map = &vm->vm_map; 2028 start = (uintptr_t)job->uaiocb.aio_buf; 2029 pgoff = start & PAGE_MASK; 2030 end = round_page(start + job->uaiocb.aio_nbytes); 2031 start = trunc_page(start); 2032 n = atop(end - start); 2033 2034 ab = malloc(sizeof(*ab) + n * sizeof(vm_page_t), M_CXGBE, M_WAITOK | 2035 M_ZERO); 2036 refcount_init(&ab->refcount, 1); 2037 ab->ps.pages = (vm_page_t *)(ab + 1); 2038 ab->ps.npages = vm_fault_quick_hold_pages(map, start, end - start, 2039 VM_PROT_WRITE, ab->ps.pages, n); 2040 if (ab->ps.npages < 0) { 2041 free(ab, M_CXGBE); 2042 return (EFAULT); 2043 } 2044 2045 KASSERT(ab->ps.npages == n, 2046 ("hold_aio: page count mismatch: %d vs %d", ab->ps.npages, n)); 2047 2048 ab->ps.offset = pgoff; 2049 ab->ps.len = job->uaiocb.aio_nbytes; 2050 ab->job = job; 2051 job->backend1 = ab; 2052 #ifdef VERBOSE_TRACES 2053 CTR5(KTR_CXGBE, "%s: tid %d, new pageset %p for job %p, npages %d", 2054 __func__, jobtotid(job), &ab->ps, job, ab->ps.npages); 2055 #endif 2056 return (0); 2057 } 2058 2059 static void 2060 t4_aiotx_process_job(struct toepcb *toep, struct socket *so, struct kaiocb *job) 2061 { 2062 struct adapter *sc; 2063 struct sockbuf *sb; 2064 struct file *fp; 2065 struct aiotx_buffer *ab; 2066 struct inpcb *inp; 2067 struct tcpcb *tp; 2068 struct mbuf *m; 2069 int error; 2070 bool moretocome, sendmore; 2071 2072 sc = td_adapter(toep->td); 2073 sb = &so->so_snd; 2074 SOCKBUF_UNLOCK(sb); 2075 fp = job->fd_file; 2076 ab = job->backend1; 2077 m = NULL; 2078 2079 #ifdef MAC 2080 error = mac_socket_check_send(fp->f_cred, so); 2081 if (error != 0) 2082 goto out; 2083 #endif 2084 2085 if (ab == NULL) { 2086 error = hold_aio(job); 2087 if (error != 0) 2088 goto out; 2089 ab = job->backend1; 2090 } 2091 2092 /* Inline sosend_generic(). */ 2093 2094 job->msgsnd = 1; 2095 2096 error = sblock(sb, SBL_WAIT); 2097 MPASS(error == 0); 2098 2099 sendanother: 2100 m = m_get(M_WAITOK, MT_DATA); 2101 2102 SOCKBUF_LOCK(sb); 2103 if (so->so_snd.sb_state & SBS_CANTSENDMORE) { 2104 SOCKBUF_UNLOCK(sb); 2105 sbunlock(sb); 2106 if ((so->so_options & SO_NOSIGPIPE) == 0) { 2107 PROC_LOCK(job->userproc); 2108 kern_psignal(job->userproc, SIGPIPE); 2109 PROC_UNLOCK(job->userproc); 2110 } 2111 error = EPIPE; 2112 goto out; 2113 } 2114 if (so->so_error) { 2115 error = so->so_error; 2116 so->so_error = 0; 2117 SOCKBUF_UNLOCK(sb); 2118 sbunlock(sb); 2119 goto out; 2120 } 2121 if ((so->so_state & SS_ISCONNECTED) == 0) { 2122 SOCKBUF_UNLOCK(sb); 2123 sbunlock(sb); 2124 error = ENOTCONN; 2125 goto out; 2126 } 2127 if (sbspace(sb) < sb->sb_lowat) { 2128 MPASS(job->aio_sent == 0 || !(so->so_state & SS_NBIO)); 2129 2130 /* 2131 * Don't block if there is too little room in the socket 2132 * buffer. Instead, requeue the request. 2133 */ 2134 if (!aio_set_cancel_function(job, t4_aiotx_cancel)) { 2135 SOCKBUF_UNLOCK(sb); 2136 sbunlock(sb); 2137 error = ECANCELED; 2138 goto out; 2139 } 2140 TAILQ_INSERT_HEAD(&toep->aiotx_jobq, job, list); 2141 SOCKBUF_UNLOCK(sb); 2142 sbunlock(sb); 2143 goto out; 2144 } 2145 2146 /* 2147 * Write as much data as the socket permits, but no more than a 2148 * a single sndbuf at a time. 2149 */ 2150 m->m_len = sbspace(sb); 2151 if (m->m_len > ab->ps.len - job->aio_sent) { 2152 m->m_len = ab->ps.len - job->aio_sent; 2153 moretocome = false; 2154 } else 2155 moretocome = true; 2156 if (m->m_len > sc->tt.sndbuf) { 2157 m->m_len = sc->tt.sndbuf; 2158 sendmore = true; 2159 } else 2160 sendmore = false; 2161 2162 if (!TAILQ_EMPTY(&toep->aiotx_jobq)) 2163 moretocome = true; 2164 SOCKBUF_UNLOCK(sb); 2165 MPASS(m->m_len != 0); 2166 2167 /* Inlined tcp_usr_send(). */ 2168 2169 inp = toep->inp; 2170 INP_WLOCK(inp); 2171 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { 2172 INP_WUNLOCK(inp); 2173 sbunlock(sb); 2174 error = ECONNRESET; 2175 goto out; 2176 } 2177 2178 refcount_acquire(&ab->refcount); 2179 m_extadd(m, NULL, ab->ps.len, t4_aiotx_mbuf_free, ab, 2180 (void *)(uintptr_t)job->aio_sent, 0, EXT_NET_DRV); 2181 m->m_ext.ext_flags |= EXT_FLAG_AIOTX; 2182 job->aio_sent += m->m_len; 2183 2184 sbappendstream(sb, m, 0); 2185 m = NULL; 2186 2187 if (!(inp->inp_flags & INP_DROPPED)) { 2188 tp = intotcpcb(inp); 2189 if (moretocome) 2190 tp->t_flags |= TF_MORETOCOME; 2191 error = tp->t_fb->tfb_tcp_output(tp); 2192 if (moretocome) 2193 tp->t_flags &= ~TF_MORETOCOME; 2194 } 2195 2196 INP_WUNLOCK(inp); 2197 if (sendmore) 2198 goto sendanother; 2199 sbunlock(sb); 2200 2201 if (error) 2202 goto out; 2203 2204 /* 2205 * If this is a non-blocking socket and the request has not 2206 * been fully completed, requeue it until the socket is ready 2207 * again. 2208 */ 2209 if (job->aio_sent < job->uaiocb.aio_nbytes && 2210 !(so->so_state & SS_NBIO)) { 2211 SOCKBUF_LOCK(sb); 2212 if (!aio_set_cancel_function(job, t4_aiotx_cancel)) { 2213 SOCKBUF_UNLOCK(sb); 2214 error = ECANCELED; 2215 goto out; 2216 } 2217 TAILQ_INSERT_HEAD(&toep->aiotx_jobq, job, list); 2218 return; 2219 } 2220 2221 /* 2222 * If the request will not be requeued, drop a reference on 2223 * the aiotx buffer. Any mbufs in flight should still 2224 * contain a reference, but this drops the reference that the 2225 * job owns while it is waiting to queue mbufs to the socket. 2226 */ 2227 free_aiotx_buffer(ab); 2228 2229 out: 2230 if (error) { 2231 if (ab != NULL) { 2232 job->aio_error = error; 2233 free_aiotx_buffer(ab); 2234 } else { 2235 MPASS(job->aio_sent == 0); 2236 aio_complete(job, -1, error); 2237 } 2238 } 2239 if (m != NULL) 2240 m_free(m); 2241 SOCKBUF_LOCK(sb); 2242 } 2243 2244 static void 2245 t4_aiotx_task(void *context, int pending) 2246 { 2247 struct toepcb *toep = context; 2248 struct inpcb *inp = toep->inp; 2249 struct socket *so = inp->inp_socket; 2250 struct kaiocb *job; 2251 2252 CURVNET_SET(toep->vnet); 2253 SOCKBUF_LOCK(&so->so_snd); 2254 while (!TAILQ_EMPTY(&toep->aiotx_jobq) && sowriteable(so)) { 2255 job = TAILQ_FIRST(&toep->aiotx_jobq); 2256 TAILQ_REMOVE(&toep->aiotx_jobq, job, list); 2257 if (!aio_clear_cancel_function(job)) 2258 continue; 2259 2260 t4_aiotx_process_job(toep, so, job); 2261 } 2262 toep->aiotx_task_active = false; 2263 SOCKBUF_UNLOCK(&so->so_snd); 2264 CURVNET_RESTORE(); 2265 2266 free_toepcb(toep); 2267 } 2268 2269 static void 2270 t4_aiotx_queue_toep(struct toepcb *toep) 2271 { 2272 2273 SOCKBUF_LOCK_ASSERT(&toep->inp->inp_socket->so_snd); 2274 #ifdef VERBOSE_TRACES 2275 CTR3(KTR_CXGBE, "%s: queueing aiotx task for tid %d, active = %s", 2276 __func__, toep->tid, toep->aiotx_task_active ? "true" : "false"); 2277 #endif 2278 if (toep->aiotx_task_active) 2279 return; 2280 toep->aiotx_task_active = true; 2281 hold_toepcb(toep); 2282 soaio_enqueue(&toep->aiotx_task); 2283 } 2284 2285 static void 2286 t4_aiotx_cancel(struct kaiocb *job) 2287 { 2288 struct aiotx_buffer *ab; 2289 struct socket *so; 2290 struct sockbuf *sb; 2291 struct tcpcb *tp; 2292 struct toepcb *toep; 2293 2294 so = job->fd_file->f_data; 2295 tp = so_sototcpcb(so); 2296 toep = tp->t_toe; 2297 MPASS(job->uaiocb.aio_lio_opcode == LIO_WRITE); 2298 sb = &so->so_snd; 2299 2300 SOCKBUF_LOCK(sb); 2301 if (!aio_cancel_cleared(job)) 2302 TAILQ_REMOVE(&toep->aiotx_jobq, job, list); 2303 SOCKBUF_UNLOCK(sb); 2304 2305 ab = job->backend1; 2306 if (ab != NULL) 2307 free_aiotx_buffer(ab); 2308 else 2309 aio_cancel(job); 2310 } 2311 2312 int 2313 t4_aio_queue_aiotx(struct socket *so, struct kaiocb *job) 2314 { 2315 struct tcpcb *tp = so_sototcpcb(so); 2316 struct toepcb *toep = tp->t_toe; 2317 struct adapter *sc = td_adapter(toep->td); 2318 2319 /* This only handles writes. */ 2320 if (job->uaiocb.aio_lio_opcode != LIO_WRITE) 2321 return (EOPNOTSUPP); 2322 2323 if (!sc->tt.tx_zcopy) 2324 return (EOPNOTSUPP); 2325 2326 if (tls_tx_key(toep)) 2327 return (EOPNOTSUPP); 2328 2329 SOCKBUF_LOCK(&so->so_snd); 2330 #ifdef VERBOSE_TRACES 2331 CTR2(KTR_CXGBE, "%s: queueing %p", __func__, job); 2332 #endif 2333 if (!aio_set_cancel_function(job, t4_aiotx_cancel)) 2334 panic("new job was cancelled"); 2335 TAILQ_INSERT_TAIL(&toep->aiotx_jobq, job, list); 2336 if (sowriteable(so)) 2337 t4_aiotx_queue_toep(toep); 2338 SOCKBUF_UNLOCK(&so->so_snd); 2339 return (0); 2340 } 2341 2342 void 2343 aiotx_init_toep(struct toepcb *toep) 2344 { 2345 2346 TAILQ_INIT(&toep->aiotx_jobq); 2347 TASK_INIT(&toep->aiotx_task, 0, t4_aiotx_task, toep); 2348 } 2349 #endif 2350