2 * Copyright (c) 2012 Chelsio Communications, Inc.
4 * Written by: Navdeep Parhar <np@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/protosw.h>
40 #include <sys/domain.h>
41 #include <sys/socket.h>
42 #include <sys/socketvar.h>
43 #include <sys/sglist.h>
44 #include <netinet/in.h>
45 #include <netinet/in_pcb.h>
46 #include <netinet/ip.h>
47 #include <netinet/ip6.h>
48 #include <netinet/tcp_var.h>
50 #include <netinet/tcp_fsm.h>
51 #include <netinet/tcp_seq.h>
52 #include <netinet/toecore.h>
54 #include "common/common.h"
55 #include "common/t4_msg.h"
56 #include "common/t4_regs.h"
57 #include "common/t4_tcb.h"
58 #include "tom/t4_tom_l2t.h"
59 #include "tom/t4_tom.h"
61 VNET_DECLARE(int, tcp_do_autosndbuf);
62 #define V_tcp_do_autosndbuf VNET(tcp_do_autosndbuf)
63 VNET_DECLARE(int, tcp_autosndbuf_inc);
64 #define V_tcp_autosndbuf_inc VNET(tcp_autosndbuf_inc)
65 VNET_DECLARE(int, tcp_autosndbuf_max);
66 #define V_tcp_autosndbuf_max VNET(tcp_autosndbuf_max)
67 VNET_DECLARE(int, tcp_do_autorcvbuf);
68 #define V_tcp_do_autorcvbuf VNET(tcp_do_autorcvbuf)
69 VNET_DECLARE(int, tcp_autorcvbuf_inc);
70 #define V_tcp_autorcvbuf_inc VNET(tcp_autorcvbuf_inc)
71 VNET_DECLARE(int, tcp_autorcvbuf_max);
72 #define V_tcp_autorcvbuf_max VNET(tcp_autorcvbuf_max)
75 * For ULP connections HW may add headers, e.g., for digests, that aren't part
76 * of the messages sent by the host but that are part of the TCP payload and
77 * therefore consume TCP sequence space. Tx connection parameters that
78 * operate in TCP sequence space are affected by the HW additions and need to
79 * compensate for them to accurately track TCP sequence numbers. This array
80 * contains the compensating extra lengths for ULP packets. It is indexed by
81 * a packet's ULP submode.
83 const unsigned int t4_ulp_extra_len[] = {0, 4, 4, 8};
86 * Return the length of any HW additions that will be made to a Tx packet.
87 * Such additions can happen for some types of ULP packets.
89 static inline unsigned int
90 ulp_extra_len(struct mbuf *m, int *ulp_mode)
94 if ((mtag = m_tag_find(m, CXGBE_ISCSI_MBUF_TAG, NULL)) == NULL)
96 *ulp_mode = *((int *)(mtag + 1));
98 return (t4_ulp_extra_len[*ulp_mode & 3]);
102 send_flowc_wr(struct toepcb *toep, struct flowc_tx_params *ftxp)
105 struct fw_flowc_wr *flowc;
106 unsigned int nparams = ftxp ? 8 : 6, flowclen;
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];
113 KASSERT(!(toep->flags & TPF_FLOWC_WR_SENT),
114 ("%s: flowc for tid %u sent already", __func__, toep->tid));
116 flowclen = sizeof(*flowc) + nparams * sizeof(struct fw_flowc_mnemval);
118 wr = alloc_wrqe(roundup2(flowclen, 16), toep->ofld_txq);
121 panic("%s: allocation failure.", __func__);
124 memset(flowc, 0, wr->wr_len);
126 flowc->op_to_nparams = htobe32(V_FW_WR_OP(FW_FLOWC_WR) |
127 V_FW_FLOWC_WR_NPARAMS(nparams));
128 flowc->flowid_len16 = htonl(V_FW_WR_LEN16(howmany(flowclen, 16)) |
129 V_FW_WR_FLOWID(toep->tid));
131 flowc->mnemval[0].mnemonic = FW_FLOWC_MNEM_PFNVFN;
132 flowc->mnemval[0].val = htobe32(pfvf);
133 flowc->mnemval[1].mnemonic = FW_FLOWC_MNEM_CH;
134 flowc->mnemval[1].val = htobe32(pi->tx_chan);
135 flowc->mnemval[2].mnemonic = FW_FLOWC_MNEM_PORT;
136 flowc->mnemval[2].val = htobe32(pi->tx_chan);
137 flowc->mnemval[3].mnemonic = FW_FLOWC_MNEM_IQID;
138 flowc->mnemval[3].val = htobe32(toep->ofld_rxq->iq.abs_id);
140 uint32_t sndbuf = min(ftxp->snd_space, sc->tt.sndbuf);
142 flowc->mnemval[4].mnemonic = FW_FLOWC_MNEM_SNDNXT;
143 flowc->mnemval[4].val = htobe32(ftxp->snd_nxt);
144 flowc->mnemval[5].mnemonic = FW_FLOWC_MNEM_RCVNXT;
145 flowc->mnemval[5].val = htobe32(ftxp->rcv_nxt);
146 flowc->mnemval[6].mnemonic = FW_FLOWC_MNEM_SNDBUF;
147 flowc->mnemval[6].val = htobe32(sndbuf);
148 flowc->mnemval[7].mnemonic = FW_FLOWC_MNEM_MSS;
149 flowc->mnemval[7].val = htobe32(ftxp->mss);
152 "%s: tid %u, mss %u, sndbuf %u, snd_nxt 0x%x, rcv_nxt 0x%x",
153 __func__, toep->tid, ftxp->mss, sndbuf, ftxp->snd_nxt,
156 flowc->mnemval[4].mnemonic = FW_FLOWC_MNEM_SNDBUF;
157 flowc->mnemval[4].val = htobe32(512);
158 flowc->mnemval[5].mnemonic = FW_FLOWC_MNEM_MSS;
159 flowc->mnemval[5].val = htobe32(512);
161 CTR2(KTR_CXGBE, "%s: tid %u", __func__, toep->tid);
164 txsd->tx_credits = howmany(flowclen, 16);
166 KASSERT(toep->tx_credits >= txsd->tx_credits && toep->txsd_avail > 0,
167 ("%s: not enough credits (%d)", __func__, toep->tx_credits));
168 toep->tx_credits -= txsd->tx_credits;
169 if (__predict_false(++toep->txsd_pidx == toep->txsd_total))
173 toep->flags |= TPF_FLOWC_WR_SENT;
178 send_reset(struct adapter *sc, struct toepcb *toep, uint32_t snd_nxt)
181 struct cpl_abort_req *req;
183 struct inpcb *inp = toep->inp;
184 struct tcpcb *tp = intotcpcb(inp); /* don't use if INP_DROPPED */
186 INP_WLOCK_ASSERT(inp);
188 CTR6(KTR_CXGBE, "%s: tid %d (%s), toep_flags 0x%x, inp_flags 0x%x%s",
190 inp->inp_flags & INP_DROPPED ? "inp dropped" :
191 tcpstates[tp->t_state],
192 toep->flags, inp->inp_flags,
193 toep->flags & TPF_ABORT_SHUTDOWN ?
194 " (abort already in progress)" : "");
196 if (toep->flags & TPF_ABORT_SHUTDOWN)
197 return; /* abort already in progress */
199 toep->flags |= TPF_ABORT_SHUTDOWN;
201 KASSERT(toep->flags & TPF_FLOWC_WR_SENT,
202 ("%s: flowc_wr not sent for tid %d.", __func__, tid));
204 wr = alloc_wrqe(sizeof(*req), toep->ofld_txq);
207 panic("%s: allocation failure.", __func__);
211 INIT_TP_WR_MIT_CPL(req, CPL_ABORT_REQ, tid);
212 if (inp->inp_flags & INP_DROPPED)
213 req->rsvd0 = htobe32(snd_nxt);
215 req->rsvd0 = htobe32(tp->snd_nxt);
216 req->rsvd1 = !(toep->flags & TPF_TX_DATA_SENT);
217 req->cmd = CPL_ABORT_SEND_RST;
220 * XXX: What's the correct way to tell that the inp hasn't been detached
221 * from its socket? Should I even be flushing the snd buffer here?
223 if ((inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT)) == 0) {
224 struct socket *so = inp->inp_socket;
226 if (so != NULL) /* because I'm not sure. See comment above */
227 sbflush(&so->so_snd);
230 t4_l2t_send(sc, wr, toep->l2te);
234 * Called when a connection is established to translate the TCP options
235 * reported by HW to FreeBSD's native format.
238 assign_rxopt(struct tcpcb *tp, unsigned int opt)
240 struct toepcb *toep = tp->t_toe;
241 struct inpcb *inp = tp->t_inpcb;
242 struct adapter *sc = td_adapter(toep->td);
245 INP_LOCK_ASSERT(inp);
247 if (inp->inp_inc.inc_flags & INC_ISIPV6)
248 n = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
250 n = sizeof(struct ip) + sizeof(struct tcphdr);
251 tp->t_maxseg = tp->t_maxopd = sc->params.mtus[G_TCPOPT_MSS(opt)] - n;
253 CTR4(KTR_CXGBE, "%s: tid %d, mtu_idx %u (%u)", __func__, toep->tid,
254 G_TCPOPT_MSS(opt), sc->params.mtus[G_TCPOPT_MSS(opt)]);
256 if (G_TCPOPT_TSTAMP(opt)) {
257 tp->t_flags |= TF_RCVD_TSTMP; /* timestamps ok */
258 tp->ts_recent = 0; /* hmmm */
259 tp->ts_recent_age = tcp_ts_getticks();
260 tp->t_maxseg -= TCPOLEN_TSTAMP_APPA;
263 if (G_TCPOPT_SACK(opt))
264 tp->t_flags |= TF_SACK_PERMIT; /* should already be set */
266 tp->t_flags &= ~TF_SACK_PERMIT; /* sack disallowed by peer */
268 if (G_TCPOPT_WSCALE_OK(opt))
269 tp->t_flags |= TF_RCVD_SCALE;
271 /* Doing window scaling? */
272 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
273 (TF_RCVD_SCALE | TF_REQ_SCALE)) {
274 tp->rcv_scale = tp->request_r_scale;
275 tp->snd_scale = G_TCPOPT_SND_WSCALE(opt);
280 * Completes some final bits of initialization for just established connections
281 * and changes their state to TCPS_ESTABLISHED.
283 * The ISNs are from after the exchange of SYNs. i.e., the true ISN + 1.
286 make_established(struct toepcb *toep, uint32_t snd_isn, uint32_t rcv_isn,
289 struct inpcb *inp = toep->inp;
290 struct socket *so = inp->inp_socket;
291 struct tcpcb *tp = intotcpcb(inp);
293 uint32_t iss = be32toh(snd_isn) - 1; /* true ISS */
294 uint32_t irs = be32toh(rcv_isn) - 1; /* true IRS */
295 uint16_t tcpopt = be16toh(opt);
296 struct flowc_tx_params ftxp;
298 CURVNET_SET(so->so_vnet);
299 INP_WLOCK_ASSERT(inp);
300 KASSERT(tp->t_state == TCPS_SYN_SENT ||
301 tp->t_state == TCPS_SYN_RECEIVED,
302 ("%s: TCP state %s", __func__, tcpstates[tp->t_state]));
304 CTR4(KTR_CXGBE, "%s: tid %d, toep %p, inp %p",
305 __func__, toep->tid, toep, inp);
307 tp->t_state = TCPS_ESTABLISHED;
308 tp->t_starttime = ticks;
309 TCPSTAT_INC(tcps_connects);
313 tp->rcv_wnd = toep->rx_credits << 10;
314 tp->rcv_adv += tp->rcv_wnd;
315 tp->last_ack_sent = tp->rcv_nxt;
318 * If we were unable to send all rx credits via opt0, save the remainder
319 * in rx_credits so that they can be handed over with the next credit
322 SOCKBUF_LOCK(&so->so_rcv);
323 bufsize = select_rcv_wnd(so);
324 SOCKBUF_UNLOCK(&so->so_rcv);
325 toep->rx_credits = bufsize - tp->rcv_wnd;
329 tp->snd_una = iss + 1;
330 tp->snd_nxt = iss + 1;
331 tp->snd_max = iss + 1;
333 assign_rxopt(tp, tcpopt);
335 SOCKBUF_LOCK(&so->so_snd);
336 if (so->so_snd.sb_flags & SB_AUTOSIZE && V_tcp_do_autosndbuf)
337 bufsize = V_tcp_autosndbuf_max;
339 bufsize = sbspace(&so->so_snd);
340 SOCKBUF_UNLOCK(&so->so_snd);
342 ftxp.snd_nxt = tp->snd_nxt;
343 ftxp.rcv_nxt = tp->rcv_nxt;
344 ftxp.snd_space = bufsize;
345 ftxp.mss = tp->t_maxseg;
346 send_flowc_wr(toep, &ftxp);
353 send_rx_credits(struct adapter *sc, struct toepcb *toep, int credits)
356 struct cpl_rx_data_ack *req;
357 uint32_t dack = F_RX_DACK_CHANGE | V_RX_DACK_MODE(1);
359 KASSERT(credits >= 0, ("%s: %d credits", __func__, credits));
361 wr = alloc_wrqe(sizeof(*req), toep->ctrlq);
366 INIT_TP_WR_MIT_CPL(req, CPL_RX_DATA_ACK, toep->tid);
367 req->credit_dack = htobe32(dack | V_RX_CREDITS(credits));
374 t4_rcvd(struct toedev *tod, struct tcpcb *tp)
376 struct adapter *sc = tod->tod_softc;
377 struct inpcb *inp = tp->t_inpcb;
378 struct socket *so = inp->inp_socket;
379 struct sockbuf *sb = &so->so_rcv;
380 struct toepcb *toep = tp->t_toe;
383 INP_WLOCK_ASSERT(inp);
386 KASSERT(toep->sb_cc >= sb->sb_cc,
387 ("%s: sb %p has more data (%d) than last time (%d).",
388 __func__, sb, sb->sb_cc, toep->sb_cc));
389 if (toep->ulp_mode == ULP_MODE_ISCSI) {
390 toep->rx_credits += toep->sb_cc;
393 toep->rx_credits += toep->sb_cc - sb->sb_cc;
394 toep->sb_cc = sb->sb_cc;
396 if (toep->rx_credits > 0 &&
397 (tp->rcv_wnd <= 32 * 1024 || toep->rx_credits >= 64 * 1024 ||
398 (toep->rx_credits >= 16 * 1024 && tp->rcv_wnd <= 128 * 1024) ||
399 toep->sb_cc + tp->rcv_wnd < sb->sb_lowat)) {
401 credits = send_rx_credits(sc, toep, toep->rx_credits);
402 toep->rx_credits -= credits;
403 tp->rcv_wnd += credits;
404 tp->rcv_adv += credits;
410 * Close a connection by sending a CPL_CLOSE_CON_REQ message.
413 close_conn(struct adapter *sc, struct toepcb *toep)
416 struct cpl_close_con_req *req;
417 unsigned int tid = toep->tid;
419 CTR3(KTR_CXGBE, "%s: tid %u%s", __func__, toep->tid,
420 toep->flags & TPF_FIN_SENT ? ", IGNORED" : "");
422 if (toep->flags & TPF_FIN_SENT)
425 KASSERT(toep->flags & TPF_FLOWC_WR_SENT,
426 ("%s: flowc_wr not sent for tid %u.", __func__, tid));
428 wr = alloc_wrqe(sizeof(*req), toep->ofld_txq);
431 panic("%s: allocation failure.", __func__);
435 req->wr.wr_hi = htonl(V_FW_WR_OP(FW_TP_WR) |
436 V_FW_WR_IMMDLEN(sizeof(*req) - sizeof(req->wr)));
437 req->wr.wr_mid = htonl(V_FW_WR_LEN16(howmany(sizeof(*req), 16)) |
438 V_FW_WR_FLOWID(tid));
439 req->wr.wr_lo = cpu_to_be64(0);
440 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_CON_REQ, tid));
443 toep->flags |= TPF_FIN_SENT;
444 toep->flags &= ~TPF_SEND_FIN;
445 t4_l2t_send(sc, wr, toep->l2te);
450 #define MAX_OFLD_TX_CREDITS (SGE_MAX_WR_LEN / 16)
451 #define MIN_OFLD_TX_CREDITS (howmany(sizeof(struct fw_ofld_tx_data_wr) + 1, 16))
453 /* Maximum amount of immediate data we could stuff in a WR */
455 max_imm_payload(int tx_credits)
457 const int n = 2; /* Use only up to 2 desc for imm. data WR */
459 KASSERT(tx_credits >= 0 &&
460 tx_credits <= MAX_OFLD_TX_CREDITS,
461 ("%s: %d credits", __func__, tx_credits));
463 if (tx_credits < MIN_OFLD_TX_CREDITS)
466 if (tx_credits >= (n * EQ_ESIZE) / 16)
467 return ((n * EQ_ESIZE) - sizeof(struct fw_ofld_tx_data_wr));
469 return (tx_credits * 16 - sizeof(struct fw_ofld_tx_data_wr));
472 /* Maximum number of SGL entries we could stuff in a WR */
474 max_dsgl_nsegs(int tx_credits)
476 int nseg = 1; /* ulptx_sgl has room for 1, rest ulp_tx_sge_pair */
477 int sge_pair_credits = tx_credits - MIN_OFLD_TX_CREDITS;
479 KASSERT(tx_credits >= 0 &&
480 tx_credits <= MAX_OFLD_TX_CREDITS,
481 ("%s: %d credits", __func__, tx_credits));
483 if (tx_credits < MIN_OFLD_TX_CREDITS)
486 nseg += 2 * (sge_pair_credits * 16 / 24);
487 if ((sge_pair_credits * 16) % 24 == 16)
494 write_tx_wr(void *dst, struct toepcb *toep, unsigned int immdlen,
495 unsigned int plen, uint8_t credits, int shove, int ulp_mode, int txalign)
497 struct fw_ofld_tx_data_wr *txwr = dst;
498 unsigned int wr_ulp_mode;
500 txwr->op_to_immdlen = htobe32(V_WR_OP(FW_OFLD_TX_DATA_WR) |
501 V_FW_WR_IMMDLEN(immdlen));
502 txwr->flowid_len16 = htobe32(V_FW_WR_FLOWID(toep->tid) |
503 V_FW_WR_LEN16(credits));
505 /* for iscsi, the mode & submode setting is per-packet */
506 if (toep->ulp_mode == ULP_MODE_ISCSI)
507 wr_ulp_mode = V_TX_ULP_MODE(ulp_mode >> 4) |
508 V_TX_ULP_SUBMODE(ulp_mode & 3);
510 wr_ulp_mode = V_TX_ULP_MODE(toep->ulp_mode);
512 txwr->lsodisable_to_flags = htobe32(wr_ulp_mode | V_TX_URG(0) | /*XXX*/
514 txwr->plen = htobe32(plen);
517 struct tcpcb *tp = intotcpcb(toep->inp);
519 if (plen < 2 * tp->t_maxseg || is_10G_port(toep->vi->pi))
520 txwr->lsodisable_to_flags |=
521 htobe32(F_FW_OFLD_TX_DATA_WR_LSODISABLE);
523 txwr->lsodisable_to_flags |=
524 htobe32(F_FW_OFLD_TX_DATA_WR_ALIGNPLD |
525 (tp->t_flags & TF_NODELAY ? 0 :
526 F_FW_OFLD_TX_DATA_WR_ALIGNPLDSHOVE));
531 * Generate a DSGL from a starting mbuf. The total number of segments and the
532 * maximum segments in any one mbuf are provided.
535 write_tx_sgl(void *dst, struct mbuf *start, struct mbuf *stop, int nsegs, int n)
538 struct ulptx_sgl *usgl = dst;
541 struct sglist_seg segs[n];
543 KASSERT(nsegs > 0, ("%s: nsegs 0", __func__));
545 sglist_init(&sg, n, segs);
546 usgl->cmd_nsge = htobe32(V_ULPTX_CMD(ULP_TX_SC_DSGL) |
547 V_ULPTX_NSGE(nsegs));
550 for (m = start; m != stop; m = m->m_next) {
551 rc = sglist_append(&sg, mtod(m, void *), m->m_len);
552 if (__predict_false(rc != 0))
553 panic("%s: sglist_append %d", __func__, rc);
555 for (j = 0; j < sg.sg_nseg; i++, j++) {
557 usgl->len0 = htobe32(segs[j].ss_len);
558 usgl->addr0 = htobe64(segs[j].ss_paddr);
560 usgl->sge[i / 2].len[i & 1] =
561 htobe32(segs[j].ss_len);
562 usgl->sge[i / 2].addr[i & 1] =
563 htobe64(segs[j].ss_paddr);
572 usgl->sge[i / 2].len[1] = htobe32(0);
573 KASSERT(nsegs == 0, ("%s: nsegs %d, start %p, stop %p",
574 __func__, nsegs, start, stop));
578 * Max number of SGL entries an offload tx work request can have. This is 41
579 * (1 + 40) for a full 512B work request.
580 * fw_ofld_tx_data_wr(16B) + ulptx_sgl(16B, 1) + ulptx_sge_pair(480B, 40)
582 #define OFLD_SGL_LEN (41)
585 * Send data and/or a FIN to the peer.
587 * The socket's so_snd buffer consists of a stream of data starting with sb_mb
588 * and linked together with m_next. sb_sndptr, if set, is the last mbuf that
591 * drop indicates the number of bytes that should be dropped from the head of
592 * the send buffer. It is an optimization that lets do_fw4_ack avoid creating
593 * contention on the send buffer lock (before this change it used to do
594 * sowwakeup and then t4_push_frames right after that when recovering from tx
595 * stalls). When drop is set this function MUST drop the bytes and wake up any
599 t4_push_frames(struct adapter *sc, struct toepcb *toep, int drop)
601 struct mbuf *sndptr, *m, *sb_sndptr;
602 struct fw_ofld_tx_data_wr *txwr;
604 u_int plen, nsegs, credits, max_imm, max_nsegs, max_nsegs_1mbuf;
605 struct inpcb *inp = toep->inp;
606 struct tcpcb *tp = intotcpcb(inp);
607 struct socket *so = inp->inp_socket;
608 struct sockbuf *sb = &so->so_snd;
609 int tx_credits, shove, compl, space, sowwakeup;
610 struct ofld_tx_sdesc *txsd = &toep->txsd[toep->txsd_pidx];
612 INP_WLOCK_ASSERT(inp);
613 KASSERT(toep->flags & TPF_FLOWC_WR_SENT,
614 ("%s: flowc_wr not sent for tid %u.", __func__, toep->tid));
616 KASSERT(toep->ulp_mode == ULP_MODE_NONE ||
617 toep->ulp_mode == ULP_MODE_TCPDDP ||
618 toep->ulp_mode == ULP_MODE_RDMA,
619 ("%s: ulp_mode %u for toep %p", __func__, toep->ulp_mode, toep));
622 * This function doesn't resume by itself. Someone else must clear the
623 * flag and call this function.
625 if (__predict_false(toep->flags & TPF_TX_SUSPENDED)) {
627 ("%s: drop (%d) != 0 but tx is suspended", __func__, drop));
632 tx_credits = min(toep->tx_credits, MAX_OFLD_TX_CREDITS);
633 max_imm = max_imm_payload(tx_credits);
634 max_nsegs = max_dsgl_nsegs(tx_credits);
639 sbdrop_locked(sb, drop);
642 sb_sndptr = sb->sb_sndptr;
643 sndptr = sb_sndptr ? sb_sndptr->m_next : sb->sb_mb;
646 max_nsegs_1mbuf = 0; /* max # of SGL segments in any one mbuf */
647 for (m = sndptr; m != NULL; m = m->m_next) {
648 int n = sglist_count(mtod(m, void *), m->m_len);
653 /* This mbuf sent us _over_ the nsegs limit, back out */
654 if (plen > max_imm && nsegs > max_nsegs) {
658 /* Too few credits */
659 toep->flags |= TPF_TX_SUSPENDED;
661 sowwakeup_locked(so);
664 SOCKBUF_UNLOCK_ASSERT(sb);
670 if (max_nsegs_1mbuf < n)
672 sb_sndptr = m; /* new sb->sb_sndptr if all goes well */
674 /* This mbuf put us right at the max_nsegs limit */
675 if (plen > max_imm && nsegs == max_nsegs) {
683 if (space <= sb->sb_hiwat * 3 / 8 &&
684 toep->plen_nocompl + plen >= sb->sb_hiwat / 4)
689 if (sb->sb_flags & SB_AUTOSIZE &&
690 V_tcp_do_autosndbuf &&
691 sb->sb_hiwat < V_tcp_autosndbuf_max &&
692 space < sb->sb_hiwat / 8) {
693 int newsize = min(sb->sb_hiwat + V_tcp_autosndbuf_inc,
694 V_tcp_autosndbuf_max);
696 if (!sbreserve_locked(sb, newsize, so, NULL))
697 sb->sb_flags &= ~SB_AUTOSIZE;
699 sowwakeup = 1; /* room available */
702 sowwakeup_locked(so);
705 SOCKBUF_UNLOCK_ASSERT(sb);
707 /* nothing to send */
710 ("%s: nothing to send, but m != NULL", __func__));
714 if (__predict_false(toep->flags & TPF_FIN_SENT))
715 panic("%s: excess tx.", __func__);
717 shove = m == NULL && !(tp->t_flags & TF_MORETOCOME);
718 if (plen <= max_imm) {
720 /* Immediate data tx */
722 wr = alloc_wrqe(roundup2(sizeof(*txwr) + plen, 16),
725 /* XXX: how will we recover from this? */
726 toep->flags |= TPF_TX_SUSPENDED;
730 credits = howmany(wr->wr_len, 16);
731 write_tx_wr(txwr, toep, plen, plen, credits, shove, 0,
733 m_copydata(sndptr, 0, plen, (void *)(txwr + 1));
740 wr_len = sizeof(*txwr) + sizeof(struct ulptx_sgl) +
741 ((3 * (nsegs - 1)) / 2 + ((nsegs - 1) & 1)) * 8;
742 wr = alloc_wrqe(roundup2(wr_len, 16), toep->ofld_txq);
744 /* XXX: how will we recover from this? */
745 toep->flags |= TPF_TX_SUSPENDED;
749 credits = howmany(wr_len, 16);
750 write_tx_wr(txwr, toep, 0, plen, credits, shove, 0,
752 write_tx_sgl(txwr + 1, sndptr, m, nsegs,
755 uint64_t *pad = (uint64_t *)
756 ((uintptr_t)txwr + wr_len);
761 KASSERT(toep->tx_credits >= credits,
762 ("%s: not enough credits", __func__));
764 toep->tx_credits -= credits;
765 toep->tx_nocompl += credits;
766 toep->plen_nocompl += plen;
767 if (toep->tx_credits <= toep->tx_total * 3 / 8 &&
768 toep->tx_nocompl >= toep->tx_total / 4)
771 if (compl || toep->ulp_mode == ULP_MODE_RDMA) {
772 txwr->op_to_immdlen |= htobe32(F_FW_WR_COMPL);
773 toep->tx_nocompl = 0;
774 toep->plen_nocompl = 0;
781 KASSERT(sb_sndptr, ("%s: sb_sndptr is NULL", __func__));
782 sb->sb_sndptr = sb_sndptr;
785 toep->flags |= TPF_TX_DATA_SENT;
786 if (toep->tx_credits < MIN_OFLD_TX_CREDITS)
787 toep->flags |= TPF_TX_SUSPENDED;
789 KASSERT(toep->txsd_avail > 0, ("%s: no txsd", __func__));
791 txsd->tx_credits = credits;
793 if (__predict_false(++toep->txsd_pidx == toep->txsd_total)) {
795 txsd = &toep->txsd[0];
799 t4_l2t_send(sc, wr, toep->l2te);
802 /* Send a FIN if requested, but only if there's no more data to send */
803 if (m == NULL && toep->flags & TPF_SEND_FIN)
804 close_conn(sc, toep);
807 /* Send ULP data over TOE using TX_DATA_WR. We send whole mbuf at once */
809 t4_ulp_push_frames(struct adapter *sc, struct toepcb *toep, int drop)
811 struct mbuf *sndptr, *m = NULL;
812 struct fw_ofld_tx_data_wr *txwr;
814 unsigned int plen, nsegs, credits, max_imm, max_nsegs, max_nsegs_1mbuf;
815 struct inpcb *inp = toep->inp;
819 int tx_credits, ulp_len = 0, ulp_mode = 0, qlen = 0;
821 struct ofld_tx_sdesc *txsd;
823 INP_WLOCK_ASSERT(inp);
824 if (toep->flags & TPF_ABORT_SHUTDOWN)
828 so = inp->inp_socket;
830 txsd = &toep->txsd[toep->txsd_pidx];
832 KASSERT(toep->flags & TPF_FLOWC_WR_SENT,
833 ("%s: flowc_wr not sent for tid %u.", __func__, toep->tid));
836 * This function doesn't resume by itself. Someone else must clear the
837 * flag and call this function.
839 if (__predict_false(toep->flags & TPF_TX_SUSPENDED))
842 sndptr = t4_queue_iscsi_callback(so, toep, 1, &qlen);
847 tx_credits = min(toep->tx_credits, MAX_OFLD_TX_CREDITS);
848 max_imm = max_imm_payload(tx_credits);
849 max_nsegs = max_dsgl_nsegs(tx_credits);
852 t4_cpl_iscsi_callback(toep->td, toep, &drop,
859 max_nsegs_1mbuf = 0; /* max # of SGL segments in any one mbuf */
860 for (m = sndptr; m != NULL; m = m->m_next) {
861 int n = sglist_count(mtod(m, void *), m->m_len);
866 /* This mbuf sent us _over_ the nsegs limit, return */
867 if (plen > max_imm && nsegs > max_nsegs) {
868 toep->flags |= TPF_TX_SUSPENDED;
872 if (max_nsegs_1mbuf < n)
875 /* This mbuf put us right at the max_nsegs limit */
876 if (plen > max_imm && nsegs == max_nsegs) {
877 toep->flags |= TPF_TX_SUSPENDED;
882 shove = m == NULL && !(tp->t_flags & TF_MORETOCOME);
883 /* nothing to send */
886 ("%s: nothing to send, but m != NULL", __func__));
890 if (__predict_false(toep->flags & TPF_FIN_SENT))
891 panic("%s: excess tx.", __func__);
893 ulp_len = plen + ulp_extra_len(sndptr, &ulp_mode);
894 if (plen <= max_imm) {
896 /* Immediate data tx */
897 wr = alloc_wrqe(roundup(sizeof(*txwr) + plen, 16),
900 /* XXX: how will we recover from this? */
901 toep->flags |= TPF_TX_SUSPENDED;
905 credits = howmany(wr->wr_len, 16);
906 write_tx_wr(txwr, toep, plen, ulp_len, credits, shove,
908 m_copydata(sndptr, 0, plen, (void *)(txwr + 1));
913 wr_len = sizeof(*txwr) + sizeof(struct ulptx_sgl) +
914 ((3 * (nsegs - 1)) / 2 + ((nsegs - 1) & 1)) * 8;
915 wr = alloc_wrqe(roundup(wr_len, 16), toep->ofld_txq);
917 /* XXX: how will we recover from this? */
918 toep->flags |= TPF_TX_SUSPENDED;
922 credits = howmany(wr_len, 16);
923 write_tx_wr(txwr, toep, 0, ulp_len, credits, shove,
925 write_tx_sgl(txwr + 1, sndptr, m, nsegs,
928 uint64_t *pad = (uint64_t *)
929 ((uintptr_t)txwr + wr_len);
934 KASSERT(toep->tx_credits >= credits,
935 ("%s: not enough credits", __func__));
937 toep->tx_credits -= credits;
938 toep->tx_nocompl += credits;
939 toep->plen_nocompl += plen;
940 if (toep->tx_credits <= toep->tx_total * 3 / 8 &&
941 toep->tx_nocompl >= toep->tx_total / 4)
945 txwr->op_to_immdlen |= htobe32(F_FW_WR_COMPL);
946 toep->tx_nocompl = 0;
947 toep->plen_nocompl = 0;
949 tp->snd_nxt += ulp_len;
950 tp->snd_max += ulp_len;
953 sndptr = m = t4_queue_iscsi_callback(so, toep, 2, &qlen);
955 toep->flags |= TPF_TX_DATA_SENT;
956 if (toep->tx_credits < MIN_OFLD_TX_CREDITS) {
957 toep->flags |= TPF_TX_SUSPENDED;
960 KASSERT(toep->txsd_avail > 0, ("%s: no txsd", __func__));
962 txsd->tx_credits = credits;
964 if (__predict_false(++toep->txsd_pidx == toep->txsd_total)) {
966 txsd = &toep->txsd[0];
970 t4_l2t_send(sc, wr, toep->l2te);
973 /* Send a FIN if requested, but only if there's no more data to send */
974 if (m == NULL && toep->flags & TPF_SEND_FIN)
975 close_conn(sc, toep);
979 t4_tod_output(struct toedev *tod, struct tcpcb *tp)
981 struct adapter *sc = tod->tod_softc;
983 struct inpcb *inp = tp->t_inpcb;
985 struct toepcb *toep = tp->t_toe;
987 INP_WLOCK_ASSERT(inp);
988 KASSERT((inp->inp_flags & INP_DROPPED) == 0,
989 ("%s: inp %p dropped.", __func__, inp));
990 KASSERT(toep != NULL, ("%s: toep is NULL", __func__));
992 t4_push_frames(sc, toep, 0);
998 t4_send_fin(struct toedev *tod, struct tcpcb *tp)
1000 struct adapter *sc = tod->tod_softc;
1002 struct inpcb *inp = tp->t_inpcb;
1004 struct toepcb *toep = tp->t_toe;
1006 INP_WLOCK_ASSERT(inp);
1007 KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1008 ("%s: inp %p dropped.", __func__, inp));
1009 KASSERT(toep != NULL, ("%s: toep is NULL", __func__));
1011 toep->flags |= TPF_SEND_FIN;
1012 if (tp->t_state >= TCPS_ESTABLISHED) {
1013 if (toep->ulp_mode == ULP_MODE_ISCSI)
1014 t4_ulp_push_frames(sc, toep, 0);
1016 t4_push_frames(sc, toep, 0);
1023 t4_send_rst(struct toedev *tod, struct tcpcb *tp)
1025 struct adapter *sc = tod->tod_softc;
1026 #if defined(INVARIANTS)
1027 struct inpcb *inp = tp->t_inpcb;
1029 struct toepcb *toep = tp->t_toe;
1031 INP_WLOCK_ASSERT(inp);
1032 KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1033 ("%s: inp %p dropped.", __func__, inp));
1034 KASSERT(toep != NULL, ("%s: toep is NULL", __func__));
1037 KASSERT(toep->flags & TPF_FLOWC_WR_SENT,
1038 ("%s: flowc for tid %u [%s] not sent already",
1039 __func__, toep->tid, tcpstates[tp->t_state]));
1041 send_reset(sc, toep, 0);
1046 * Peer has sent us a FIN.
1049 do_peer_close(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
1051 struct adapter *sc = iq->adapter;
1052 const struct cpl_peer_close *cpl = (const void *)(rss + 1);
1053 unsigned int tid = GET_TID(cpl);
1054 struct toepcb *toep = lookup_tid(sc, tid);
1055 struct inpcb *inp = toep->inp;
1056 struct tcpcb *tp = NULL;
1060 unsigned int opcode = G_CPL_OPCODE(be32toh(OPCODE_TID(cpl)));
1063 KASSERT(opcode == CPL_PEER_CLOSE,
1064 ("%s: unexpected opcode 0x%x", __func__, opcode));
1065 KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
1067 if (__predict_false(toep->flags & TPF_SYNQE)) {
1069 struct synq_entry *synqe = (void *)toep;
1071 INP_WLOCK(synqe->lctx->inp);
1072 if (synqe->flags & TPF_SYNQE_HAS_L2TE) {
1073 KASSERT(synqe->flags & TPF_ABORT_SHUTDOWN,
1074 ("%s: listen socket closed but tid %u not aborted.",
1078 * do_pass_accept_req is still running and will
1079 * eventually take care of this tid.
1082 INP_WUNLOCK(synqe->lctx->inp);
1084 CTR4(KTR_CXGBE, "%s: tid %u, synqe %p (0x%x)", __func__, tid,
1089 KASSERT(toep->tid == tid, ("%s: toep tid mismatch", __func__));
1091 INP_INFO_WLOCK(&V_tcbinfo);
1093 tp = intotcpcb(inp);
1095 CTR5(KTR_CXGBE, "%s: tid %u (%s), toep_flags 0x%x, inp %p", __func__,
1096 tid, tp ? tcpstates[tp->t_state] : "no tp", toep->flags, inp);
1098 if (toep->flags & TPF_ABORT_SHUTDOWN)
1101 tp->rcv_nxt++; /* FIN */
1103 so = inp->inp_socket;
1106 if (__predict_false(toep->ddp_flags & (DDP_BUF0_ACTIVE | DDP_BUF1_ACTIVE))) {
1107 handle_ddp_close(toep, tp, sb, cpl->rcv_nxt);
1109 socantrcvmore_locked(so); /* unlocks the sockbuf */
1111 if (toep->ulp_mode != ULP_MODE_RDMA) {
1112 KASSERT(tp->rcv_nxt == be32toh(cpl->rcv_nxt),
1113 ("%s: rcv_nxt mismatch: %u %u", __func__, tp->rcv_nxt,
1114 be32toh(cpl->rcv_nxt)));
1117 switch (tp->t_state) {
1118 case TCPS_SYN_RECEIVED:
1119 tp->t_starttime = ticks;
1122 case TCPS_ESTABLISHED:
1123 tp->t_state = TCPS_CLOSE_WAIT;
1126 case TCPS_FIN_WAIT_1:
1127 tp->t_state = TCPS_CLOSING;
1130 case TCPS_FIN_WAIT_2:
1132 INP_UNLOCK_ASSERT(inp); /* safe, we have a ref on the inp */
1133 INP_INFO_WUNLOCK(&V_tcbinfo);
1136 final_cpl_received(toep);
1140 log(LOG_ERR, "%s: TID %u received CPL_PEER_CLOSE in state %d\n",
1141 __func__, tid, tp->t_state);
1145 INP_INFO_WUNLOCK(&V_tcbinfo);
1150 * Peer has ACK'd our FIN.
1153 do_close_con_rpl(struct sge_iq *iq, const struct rss_header *rss,
1156 struct adapter *sc = iq->adapter;
1157 const struct cpl_close_con_rpl *cpl = (const void *)(rss + 1);
1158 unsigned int tid = GET_TID(cpl);
1159 struct toepcb *toep = lookup_tid(sc, tid);
1160 struct inpcb *inp = toep->inp;
1161 struct tcpcb *tp = NULL;
1162 struct socket *so = NULL;
1164 unsigned int opcode = G_CPL_OPCODE(be32toh(OPCODE_TID(cpl)));
1167 KASSERT(opcode == CPL_CLOSE_CON_RPL,
1168 ("%s: unexpected opcode 0x%x", __func__, opcode));
1169 KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
1170 KASSERT(toep->tid == tid, ("%s: toep tid mismatch", __func__));
1172 INP_INFO_WLOCK(&V_tcbinfo);
1174 tp = intotcpcb(inp);
1176 CTR4(KTR_CXGBE, "%s: tid %u (%s), toep_flags 0x%x",
1177 __func__, tid, tp ? tcpstates[tp->t_state] : "no tp", toep->flags);
1179 if (toep->flags & TPF_ABORT_SHUTDOWN)
1182 so = inp->inp_socket;
1183 tp->snd_una = be32toh(cpl->snd_nxt) - 1; /* exclude FIN */
1185 switch (tp->t_state) {
1186 case TCPS_CLOSING: /* see TCPS_FIN_WAIT_2 in do_peer_close too */
1189 INP_UNLOCK_ASSERT(inp); /* safe, we have a ref on the inp */
1190 INP_INFO_WUNLOCK(&V_tcbinfo);
1193 final_cpl_received(toep); /* no more CPLs expected */
1201 case TCPS_FIN_WAIT_1:
1202 if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
1203 soisdisconnected(so);
1204 tp->t_state = TCPS_FIN_WAIT_2;
1209 "%s: TID %u received CPL_CLOSE_CON_RPL in state %s\n",
1210 __func__, tid, tcpstates[tp->t_state]);
1214 INP_INFO_WUNLOCK(&V_tcbinfo);
1219 send_abort_rpl(struct adapter *sc, struct sge_wrq *ofld_txq, int tid,
1223 struct cpl_abort_rpl *cpl;
1225 wr = alloc_wrqe(sizeof(*cpl), ofld_txq);
1228 panic("%s: allocation failure.", __func__);
1232 INIT_TP_WR_MIT_CPL(cpl, CPL_ABORT_RPL, tid);
1233 cpl->cmd = rst_status;
1239 abort_status_to_errno(struct tcpcb *tp, unsigned int abort_reason)
1241 switch (abort_reason) {
1242 case CPL_ERR_BAD_SYN:
1243 case CPL_ERR_CONN_RESET:
1244 return (tp->t_state == TCPS_CLOSE_WAIT ? EPIPE : ECONNRESET);
1245 case CPL_ERR_XMIT_TIMEDOUT:
1246 case CPL_ERR_PERSIST_TIMEDOUT:
1247 case CPL_ERR_FINWAIT2_TIMEDOUT:
1248 case CPL_ERR_KEEPALIVE_TIMEDOUT:
1256 cpl_not_handled(struct sge_iq *, const struct rss_header *, struct mbuf *);
1258 * tom_cpl_iscsi_callback -
1259 * iscsi and tom would share the following cpl messages, so when any of these
1260 * message is received, after tom is done with processing it, the messages
1261 * needs to be forwarded to iscsi for further processing:
1265 void (*tom_cpl_iscsi_callback)(struct tom_data *, struct socket *, void *,
1268 struct mbuf *(*tom_queue_iscsi_callback)(struct socket *, unsigned int, int *);
1270 * Check if the handler function is set for a given CPL
1271 * return 0 if the function is NULL or cpl_not_handled, 1 otherwise.
1274 t4tom_cpl_handler_registered(struct adapter *sc, unsigned int opcode)
1277 MPASS(opcode < nitems(sc->cpl_handler));
1279 return (sc->cpl_handler[opcode] &&
1280 sc->cpl_handler[opcode] != cpl_not_handled);
1284 * set the tom_cpl_iscsi_callback function, this function should be used
1285 * whenever both toe and iscsi need to process the same cpl msg.
1288 t4tom_register_cpl_iscsi_callback(void (*fp)(struct tom_data *, struct socket *,
1289 void *, unsigned int))
1292 tom_cpl_iscsi_callback = fp;
1296 t4tom_register_queue_iscsi_callback(struct mbuf *(*fp)(struct socket *,
1297 unsigned int, int *qlen))
1300 tom_queue_iscsi_callback = fp;
1304 t4_cpl_iscsi_callback(struct tom_data *td, struct toepcb *toep, void *m,
1305 unsigned int opcode)
1309 if (opcode == CPL_FW4_ACK)
1310 so = toep->inp->inp_socket;
1312 INP_WLOCK(toep->inp);
1313 so = toep->inp->inp_socket;
1314 INP_WUNLOCK(toep->inp);
1317 if (tom_cpl_iscsi_callback && so) {
1318 if (toep->ulp_mode == ULP_MODE_ISCSI) {
1319 tom_cpl_iscsi_callback(td, so, m, opcode);
1328 t4_queue_iscsi_callback(struct socket *so, struct toepcb *toep,
1329 unsigned int cmd, int *qlen)
1332 if (tom_queue_iscsi_callback && so) {
1333 if (toep->ulp_mode == ULP_MODE_ISCSI)
1334 return (tom_queue_iscsi_callback(so, cmd, qlen));
1341 * TCP RST from the peer, timeout, or some other such critical error.
1344 do_abort_req(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
1346 struct adapter *sc = iq->adapter;
1347 const struct cpl_abort_req_rss *cpl = (const void *)(rss + 1);
1348 unsigned int tid = GET_TID(cpl);
1349 struct toepcb *toep = lookup_tid(sc, tid);
1350 struct sge_wrq *ofld_txq = toep->ofld_txq;
1354 unsigned int opcode = G_CPL_OPCODE(be32toh(OPCODE_TID(cpl)));
1357 KASSERT(opcode == CPL_ABORT_REQ_RSS,
1358 ("%s: unexpected opcode 0x%x", __func__, opcode));
1359 KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
1361 if (toep->flags & TPF_SYNQE)
1362 return (do_abort_req_synqe(iq, rss, m));
1364 KASSERT(toep->tid == tid, ("%s: toep tid mismatch", __func__));
1366 if (negative_advice(cpl->status)) {
1367 CTR4(KTR_CXGBE, "%s: negative advice %d for tid %d (0x%x)",
1368 __func__, cpl->status, tid, toep->flags);
1369 return (0); /* Ignore negative advice */
1373 INP_INFO_WLOCK(&V_tcbinfo); /* for tcp_close */
1376 tp = intotcpcb(inp);
1379 "%s: tid %d (%s), toep_flags 0x%x, inp_flags 0x%x, status %d",
1380 __func__, tid, tp ? tcpstates[tp->t_state] : "no tp", toep->flags,
1381 inp->inp_flags, cpl->status);
1384 * If we'd initiated an abort earlier the reply to it is responsible for
1385 * cleaning up resources. Otherwise we tear everything down right here
1386 * right now. We owe the T4 a CPL_ABORT_RPL no matter what.
1388 if (toep->flags & TPF_ABORT_SHUTDOWN) {
1392 toep->flags |= TPF_ABORT_SHUTDOWN;
1394 if ((inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT)) == 0) {
1395 struct socket *so = inp->inp_socket;
1398 so_error_set(so, abort_status_to_errno(tp,
1402 INP_WLOCK(inp); /* re-acquire */
1405 final_cpl_received(toep);
1407 INP_INFO_WUNLOCK(&V_tcbinfo);
1408 send_abort_rpl(sc, ofld_txq, tid, CPL_ABORT_NO_RST);
1413 * Reply to the CPL_ABORT_REQ (send_reset)
1416 do_abort_rpl(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
1418 struct adapter *sc = iq->adapter;
1419 const struct cpl_abort_rpl_rss *cpl = (const void *)(rss + 1);
1420 unsigned int tid = GET_TID(cpl);
1421 struct toepcb *toep = lookup_tid(sc, tid);
1422 struct inpcb *inp = toep->inp;
1424 unsigned int opcode = G_CPL_OPCODE(be32toh(OPCODE_TID(cpl)));
1427 KASSERT(opcode == CPL_ABORT_RPL_RSS,
1428 ("%s: unexpected opcode 0x%x", __func__, opcode));
1429 KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
1431 if (toep->flags & TPF_SYNQE)
1432 return (do_abort_rpl_synqe(iq, rss, m));
1434 KASSERT(toep->tid == tid, ("%s: toep tid mismatch", __func__));
1436 CTR5(KTR_CXGBE, "%s: tid %u, toep %p, inp %p, status %d",
1437 __func__, tid, toep, inp, cpl->status);
1439 KASSERT(toep->flags & TPF_ABORT_SHUTDOWN,
1440 ("%s: wasn't expecting abort reply", __func__));
1443 final_cpl_received(toep);
1449 do_rx_data(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
1451 struct adapter *sc = iq->adapter;
1452 const struct cpl_rx_data *cpl = mtod(m, const void *);
1453 unsigned int tid = GET_TID(cpl);
1454 struct toepcb *toep = lookup_tid(sc, tid);
1455 struct inpcb *inp = toep->inp;
1460 uint32_t ddp_placed = 0;
1462 if (__predict_false(toep->flags & TPF_SYNQE)) {
1464 struct synq_entry *synqe = (void *)toep;
1466 INP_WLOCK(synqe->lctx->inp);
1467 if (synqe->flags & TPF_SYNQE_HAS_L2TE) {
1468 KASSERT(synqe->flags & TPF_ABORT_SHUTDOWN,
1469 ("%s: listen socket closed but tid %u not aborted.",
1473 * do_pass_accept_req is still running and will
1474 * eventually take care of this tid.
1477 INP_WUNLOCK(synqe->lctx->inp);
1479 CTR4(KTR_CXGBE, "%s: tid %u, synqe %p (0x%x)", __func__, tid,
1485 KASSERT(toep->tid == tid, ("%s: toep tid mismatch", __func__));
1487 /* strip off CPL header */
1488 m_adj(m, sizeof(*cpl));
1489 len = m->m_pkthdr.len;
1492 if (inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT)) {
1493 CTR4(KTR_CXGBE, "%s: tid %u, rx (%d bytes), inp_flags 0x%x",
1494 __func__, tid, len, inp->inp_flags);
1500 tp = intotcpcb(inp);
1502 if (__predict_false(tp->rcv_nxt != be32toh(cpl->seq)))
1503 ddp_placed = be32toh(cpl->seq) - tp->rcv_nxt;
1506 if (tp->rcv_wnd < len) {
1507 KASSERT(toep->ulp_mode == ULP_MODE_RDMA,
1508 ("%s: negative window size", __func__));
1512 tp->t_rcvtime = ticks;
1514 so = inp_inpcbtosocket(inp);
1518 if (__predict_false(sb->sb_state & SBS_CANTRCVMORE)) {
1519 CTR3(KTR_CXGBE, "%s: tid %u, excess rx (%d bytes)",
1520 __func__, tid, len);
1525 INP_INFO_WLOCK(&V_tcbinfo);
1527 tp = tcp_drop(tp, ECONNRESET);
1530 INP_INFO_WUNLOCK(&V_tcbinfo);
1535 /* receive buffer autosize */
1536 CURVNET_SET(so->so_vnet);
1537 if (sb->sb_flags & SB_AUTOSIZE &&
1538 V_tcp_do_autorcvbuf &&
1539 sb->sb_hiwat < V_tcp_autorcvbuf_max &&
1540 len > (sbspace(sb) / 8 * 7)) {
1541 unsigned int hiwat = sb->sb_hiwat;
1542 unsigned int newsize = min(hiwat + V_tcp_autorcvbuf_inc,
1543 V_tcp_autorcvbuf_max);
1545 if (!sbreserve_locked(sb, newsize, so, NULL))
1546 sb->sb_flags &= ~SB_AUTOSIZE;
1548 toep->rx_credits += newsize - hiwat;
1551 if (toep->ulp_mode == ULP_MODE_TCPDDP) {
1552 int changed = !(toep->ddp_flags & DDP_ON) ^ cpl->ddp_off;
1555 if (toep->ddp_flags & DDP_SC_REQ)
1556 toep->ddp_flags ^= DDP_ON | DDP_SC_REQ;
1558 KASSERT(cpl->ddp_off == 1,
1559 ("%s: DDP switched on by itself.",
1562 /* Fell out of DDP mode */
1563 toep->ddp_flags &= ~(DDP_ON | DDP_BUF0_ACTIVE |
1567 insert_ddp_data(toep, ddp_placed);
1571 if ((toep->ddp_flags & DDP_OK) == 0 &&
1572 time_uptime >= toep->ddp_disabled + DDP_RETRY_WAIT) {
1573 toep->ddp_score = DDP_LOW_SCORE;
1574 toep->ddp_flags |= DDP_OK;
1575 CTR3(KTR_CXGBE, "%s: tid %u DDP_OK @ %u",
1576 __func__, tid, time_uptime);
1579 if (toep->ddp_flags & DDP_ON) {
1582 * CPL_RX_DATA with DDP on can only be an indicate. Ask
1583 * soreceive to post a buffer or disable DDP. The
1584 * payload that arrived in this indicate is appended to
1585 * the socket buffer as usual.
1590 "%s: tid %u (0x%x) DDP indicate (seq 0x%x, len %d)",
1591 __func__, tid, toep->flags, be32toh(cpl->seq), len);
1593 sb->sb_flags |= SB_DDP_INDICATE;
1594 } else if ((toep->ddp_flags & (DDP_OK|DDP_SC_REQ)) == DDP_OK &&
1595 tp->rcv_wnd > DDP_RSVD_WIN && len >= sc->tt.ddp_thres) {
1598 * DDP allowed but isn't on (and a request to switch it
1599 * on isn't pending either), and conditions are ripe for
1600 * it to work. Switch it on.
1603 enable_ddp(sc, toep);
1607 KASSERT(toep->sb_cc >= sb->sb_cc,
1608 ("%s: sb %p has more data (%d) than last time (%d).",
1609 __func__, sb, sb->sb_cc, toep->sb_cc));
1610 toep->rx_credits += toep->sb_cc - sb->sb_cc;
1611 sbappendstream_locked(sb, m);
1612 toep->sb_cc = sb->sb_cc;
1613 if (toep->rx_credits > 0 && toep->sb_cc + tp->rcv_wnd < sb->sb_lowat) {
1616 credits = send_rx_credits(sc, toep, toep->rx_credits);
1617 toep->rx_credits -= credits;
1618 tp->rcv_wnd += credits;
1619 tp->rcv_adv += credits;
1621 sorwakeup_locked(so);
1622 SOCKBUF_UNLOCK_ASSERT(sb);
1629 #define S_CPL_FW4_ACK_OPCODE 24
1630 #define M_CPL_FW4_ACK_OPCODE 0xff
1631 #define V_CPL_FW4_ACK_OPCODE(x) ((x) << S_CPL_FW4_ACK_OPCODE)
1632 #define G_CPL_FW4_ACK_OPCODE(x) \
1633 (((x) >> S_CPL_FW4_ACK_OPCODE) & M_CPL_FW4_ACK_OPCODE)
1635 #define S_CPL_FW4_ACK_FLOWID 0
1636 #define M_CPL_FW4_ACK_FLOWID 0xffffff
1637 #define V_CPL_FW4_ACK_FLOWID(x) ((x) << S_CPL_FW4_ACK_FLOWID)
1638 #define G_CPL_FW4_ACK_FLOWID(x) \
1639 (((x) >> S_CPL_FW4_ACK_FLOWID) & M_CPL_FW4_ACK_FLOWID)
1641 #define S_CPL_FW4_ACK_CR 24
1642 #define M_CPL_FW4_ACK_CR 0xff
1643 #define V_CPL_FW4_ACK_CR(x) ((x) << S_CPL_FW4_ACK_CR)
1644 #define G_CPL_FW4_ACK_CR(x) (((x) >> S_CPL_FW4_ACK_CR) & M_CPL_FW4_ACK_CR)
1646 #define S_CPL_FW4_ACK_SEQVAL 0
1647 #define M_CPL_FW4_ACK_SEQVAL 0x1
1648 #define V_CPL_FW4_ACK_SEQVAL(x) ((x) << S_CPL_FW4_ACK_SEQVAL)
1649 #define G_CPL_FW4_ACK_SEQVAL(x) \
1650 (((x) >> S_CPL_FW4_ACK_SEQVAL) & M_CPL_FW4_ACK_SEQVAL)
1651 #define F_CPL_FW4_ACK_SEQVAL V_CPL_FW4_ACK_SEQVAL(1U)
1654 do_fw4_ack(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
1656 struct adapter *sc = iq->adapter;
1657 const struct cpl_fw4_ack *cpl = (const void *)(rss + 1);
1658 unsigned int tid = G_CPL_FW4_ACK_FLOWID(be32toh(OPCODE_TID(cpl)));
1659 struct toepcb *toep = lookup_tid(sc, tid);
1663 uint8_t credits = cpl->credits;
1664 struct ofld_tx_sdesc *txsd;
1667 unsigned int opcode = G_CPL_FW4_ACK_OPCODE(be32toh(OPCODE_TID(cpl)));
1671 * Very unusual case: we'd sent a flowc + abort_req for a synq entry and
1672 * now this comes back carrying the credits for the flowc.
1674 if (__predict_false(toep->flags & TPF_SYNQE)) {
1675 KASSERT(toep->flags & TPF_ABORT_SHUTDOWN,
1676 ("%s: credits for a synq entry %p", __func__, toep));
1682 KASSERT(opcode == CPL_FW4_ACK,
1683 ("%s: unexpected opcode 0x%x", __func__, opcode));
1684 KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
1685 KASSERT(toep->tid == tid, ("%s: toep tid mismatch", __func__));
1689 if (__predict_false(toep->flags & TPF_ABORT_SHUTDOWN)) {
1694 KASSERT((inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) == 0,
1695 ("%s: inp_flags 0x%x", __func__, inp->inp_flags));
1697 tp = intotcpcb(inp);
1699 if (cpl->flags & CPL_FW4_ACK_FLAGS_SEQVAL) {
1700 tcp_seq snd_una = be32toh(cpl->snd_una);
1703 if (__predict_false(SEQ_LT(snd_una, tp->snd_una))) {
1705 "%s: unexpected seq# %x for TID %u, snd_una %x\n",
1706 __func__, snd_una, toep->tid, tp->snd_una);
1710 if (tp->snd_una != snd_una) {
1711 tp->snd_una = snd_una;
1712 tp->ts_recent_age = tcp_ts_getticks();
1716 so = inp->inp_socket;
1717 txsd = &toep->txsd[toep->txsd_cidx];
1720 KASSERT(credits >= txsd->tx_credits,
1721 ("%s: too many (or partial) credits", __func__));
1722 credits -= txsd->tx_credits;
1723 toep->tx_credits += txsd->tx_credits;
1727 KASSERT(toep->txsd_avail <= toep->txsd_total,
1728 ("%s: txsd avail > total", __func__));
1729 if (__predict_false(++toep->txsd_cidx == toep->txsd_total)) {
1730 txsd = &toep->txsd[0];
1731 toep->txsd_cidx = 0;
1735 if (toep->tx_credits == toep->tx_total) {
1736 toep->tx_nocompl = 0;
1737 toep->plen_nocompl = 0;
1740 if (toep->flags & TPF_TX_SUSPENDED &&
1741 toep->tx_credits >= toep->tx_total / 4) {
1742 toep->flags &= ~TPF_TX_SUSPENDED;
1743 if (toep->ulp_mode == ULP_MODE_ISCSI)
1744 t4_ulp_push_frames(sc, toep, plen);
1746 t4_push_frames(sc, toep, plen);
1747 } else if (plen > 0) {
1748 struct sockbuf *sb = &so->so_snd;
1750 if (toep->ulp_mode == ULP_MODE_ISCSI)
1751 t4_cpl_iscsi_callback(toep->td, toep, &plen,
1755 sbdrop_locked(sb, plen);
1756 sowwakeup_locked(so);
1757 SOCKBUF_UNLOCK_ASSERT(sb);
1767 do_set_tcb_rpl(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
1769 struct adapter *sc = iq->adapter;
1770 const struct cpl_set_tcb_rpl *cpl = (const void *)(rss + 1);
1771 unsigned int tid = GET_TID(cpl);
1773 unsigned int opcode = G_CPL_OPCODE(be32toh(OPCODE_TID(cpl)));
1776 KASSERT(opcode == CPL_SET_TCB_RPL,
1777 ("%s: unexpected opcode 0x%x", __func__, opcode));
1778 KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
1780 if (is_ftid(sc, tid))
1781 return (t4_filter_rpl(iq, rss, m)); /* TCB is a filter */
1783 struct toepcb *toep = lookup_tid(sc, tid);
1785 t4_cpl_iscsi_callback(toep->td, toep, m, CPL_SET_TCB_RPL);
1789 CXGBE_UNIMPLEMENTED(__func__);
1793 t4_set_tcb_field(struct adapter *sc, struct toepcb *toep, int ctrl,
1794 uint16_t word, uint64_t mask, uint64_t val)
1797 struct cpl_set_tcb_field *req;
1799 wr = alloc_wrqe(sizeof(*req), ctrl ? toep->ctrlq : toep->ofld_txq);
1802 panic("%s: allocation failure.", __func__);
1806 INIT_TP_WR_MIT_CPL(req, CPL_SET_TCB_FIELD, toep->tid);
1807 req->reply_ctrl = htobe16(V_NO_REPLY(1) |
1808 V_QUEUENO(toep->ofld_rxq->iq.abs_id));
1809 req->word_cookie = htobe16(V_WORD(word) | V_COOKIE(0));
1810 req->mask = htobe64(mask);
1811 req->val = htobe64(val);
1817 t4_init_cpl_io_handlers(struct adapter *sc)
1820 t4_register_cpl_handler(sc, CPL_PEER_CLOSE, do_peer_close);
1821 t4_register_cpl_handler(sc, CPL_CLOSE_CON_RPL, do_close_con_rpl);
1822 t4_register_cpl_handler(sc, CPL_ABORT_REQ_RSS, do_abort_req);
1823 t4_register_cpl_handler(sc, CPL_ABORT_RPL_RSS, do_abort_rpl);
1824 t4_register_cpl_handler(sc, CPL_RX_DATA, do_rx_data);
1825 t4_register_cpl_handler(sc, CPL_FW4_ACK, do_fw4_ack);
1826 t4_register_cpl_handler(sc, CPL_SET_TCB_RPL, do_set_tcb_rpl);
1830 t4_uninit_cpl_io_handlers(struct adapter *sc)
1833 t4_register_cpl_handler(sc, CPL_SET_TCB_RPL, t4_filter_rpl);