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/tcp_var.h>
49 #include <netinet/tcp_fsm.h>
50 #include <netinet/tcp_seq.h>
51 #include <netinet/toecore.h>
53 #include "common/common.h"
54 #include "common/t4_msg.h"
55 #include "common/t4_regs.h"
56 #include "common/t4_tcb.h"
57 #include "tom/t4_tom_l2t.h"
58 #include "tom/t4_tom.h"
60 VNET_DECLARE(int, tcp_do_autosndbuf);
61 #define V_tcp_do_autosndbuf VNET(tcp_do_autosndbuf)
62 VNET_DECLARE(int, tcp_autosndbuf_inc);
63 #define V_tcp_autosndbuf_inc VNET(tcp_autosndbuf_inc)
64 VNET_DECLARE(int, tcp_autosndbuf_max);
65 #define V_tcp_autosndbuf_max VNET(tcp_autosndbuf_max)
66 VNET_DECLARE(int, tcp_do_autorcvbuf);
67 #define V_tcp_do_autorcvbuf VNET(tcp_do_autorcvbuf)
68 VNET_DECLARE(int, tcp_autorcvbuf_inc);
69 #define V_tcp_autorcvbuf_inc VNET(tcp_autorcvbuf_inc)
70 VNET_DECLARE(int, tcp_autorcvbuf_max);
71 #define V_tcp_autorcvbuf_max VNET(tcp_autorcvbuf_max)
74 * For ULP connections HW may add headers, e.g., for digests, that aren't part
75 * of the messages sent by the host but that are part of the TCP payload and
76 * therefore consume TCP sequence space. Tx connection parameters that
77 * operate in TCP sequence space are affected by the HW additions and need to
78 * compensate for them to accurately track TCP sequence numbers. This array
79 * contains the compensating extra lengths for ULP packets. It is indexed by
80 * a packet's ULP submode.
82 const unsigned int t4_ulp_extra_len[] = {0, 4, 4, 8};
85 * Return the length of any HW additions that will be made to a Tx packet.
86 * Such additions can happen for some types of ULP packets.
88 static inline unsigned int
89 ulp_extra_len(struct mbuf *m, int *ulp_mode)
93 if ((mtag = m_tag_find(m, CXGBE_ISCSI_MBUF_TAG, NULL)) == NULL)
95 *ulp_mode = *((int *)(mtag + 1));
97 return (t4_ulp_extra_len[*ulp_mode & 3]);
101 send_flowc_wr(struct toepcb *toep, struct flowc_tx_params *ftxp)
104 struct fw_flowc_wr *flowc;
105 unsigned int nparams = ftxp ? 8 : 6, flowclen;
106 struct port_info *pi = toep->port;
107 struct adapter *sc = pi->adapter;
108 unsigned int pfvf = G_FW_VIID_PFN(pi->viid) << S_FW_VIID_PFN;
109 struct ofld_tx_sdesc *txsd = &toep->txsd[toep->txsd_pidx];
111 KASSERT(!(toep->flags & TPF_FLOWC_WR_SENT),
112 ("%s: flowc for tid %u sent already", __func__, toep->tid));
114 CTR2(KTR_CXGBE, "%s: tid %u", __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);
151 flowc->mnemval[4].mnemonic = FW_FLOWC_MNEM_SNDBUF;
152 flowc->mnemval[4].val = htobe32(512);
153 flowc->mnemval[5].mnemonic = FW_FLOWC_MNEM_MSS;
154 flowc->mnemval[5].val = htobe32(512);
157 txsd->tx_credits = howmany(flowclen, 16);
159 KASSERT(toep->tx_credits >= txsd->tx_credits && toep->txsd_avail > 0,
160 ("%s: not enough credits (%d)", __func__, toep->tx_credits));
161 toep->tx_credits -= txsd->tx_credits;
162 if (__predict_false(++toep->txsd_pidx == toep->txsd_total))
166 toep->flags |= TPF_FLOWC_WR_SENT;
171 send_reset(struct adapter *sc, struct toepcb *toep, uint32_t snd_nxt)
174 struct cpl_abort_req *req;
176 struct inpcb *inp = toep->inp;
177 struct tcpcb *tp = intotcpcb(inp); /* don't use if INP_DROPPED */
179 INP_WLOCK_ASSERT(inp);
181 CTR6(KTR_CXGBE, "%s: tid %d (%s), toep_flags 0x%x, inp_flags 0x%x%s",
183 inp->inp_flags & INP_DROPPED ? "inp dropped" :
184 tcpstates[tp->t_state],
185 toep->flags, inp->inp_flags,
186 toep->flags & TPF_ABORT_SHUTDOWN ?
187 " (abort already in progress)" : "");
189 if (toep->flags & TPF_ABORT_SHUTDOWN)
190 return; /* abort already in progress */
192 toep->flags |= TPF_ABORT_SHUTDOWN;
194 KASSERT(toep->flags & TPF_FLOWC_WR_SENT,
195 ("%s: flowc_wr not sent for tid %d.", __func__, tid));
197 wr = alloc_wrqe(sizeof(*req), toep->ofld_txq);
200 panic("%s: allocation failure.", __func__);
204 INIT_TP_WR_MIT_CPL(req, CPL_ABORT_REQ, tid);
205 if (inp->inp_flags & INP_DROPPED)
206 req->rsvd0 = htobe32(snd_nxt);
208 req->rsvd0 = htobe32(tp->snd_nxt);
209 req->rsvd1 = !(toep->flags & TPF_TX_DATA_SENT);
210 req->cmd = CPL_ABORT_SEND_RST;
213 * XXX: What's the correct way to tell that the inp hasn't been detached
214 * from its socket? Should I even be flushing the snd buffer here?
216 if ((inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT)) == 0) {
217 struct socket *so = inp->inp_socket;
219 if (so != NULL) /* because I'm not sure. See comment above */
220 sbflush(&so->so_snd);
223 t4_l2t_send(sc, wr, toep->l2te);
227 * Called when a connection is established to translate the TCP options
228 * reported by HW to FreeBSD's native format.
231 assign_rxopt(struct tcpcb *tp, unsigned int opt)
233 struct toepcb *toep = tp->t_toe;
234 struct adapter *sc = td_adapter(toep->td);
236 INP_LOCK_ASSERT(tp->t_inpcb);
238 tp->t_maxseg = tp->t_maxopd = sc->params.mtus[G_TCPOPT_MSS(opt)] - 40;
240 if (G_TCPOPT_TSTAMP(opt)) {
241 tp->t_flags |= TF_RCVD_TSTMP; /* timestamps ok */
242 tp->ts_recent = 0; /* hmmm */
243 tp->ts_recent_age = tcp_ts_getticks();
244 tp->t_maxseg -= TCPOLEN_TSTAMP_APPA;
247 if (G_TCPOPT_SACK(opt))
248 tp->t_flags |= TF_SACK_PERMIT; /* should already be set */
250 tp->t_flags &= ~TF_SACK_PERMIT; /* sack disallowed by peer */
252 if (G_TCPOPT_WSCALE_OK(opt))
253 tp->t_flags |= TF_RCVD_SCALE;
255 /* Doing window scaling? */
256 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
257 (TF_RCVD_SCALE | TF_REQ_SCALE)) {
258 tp->rcv_scale = tp->request_r_scale;
259 tp->snd_scale = G_TCPOPT_SND_WSCALE(opt);
264 * Completes some final bits of initialization for just established connections
265 * and changes their state to TCPS_ESTABLISHED.
267 * The ISNs are from after the exchange of SYNs. i.e., the true ISN + 1.
270 make_established(struct toepcb *toep, uint32_t snd_isn, uint32_t rcv_isn,
273 struct inpcb *inp = toep->inp;
274 struct socket *so = inp->inp_socket;
275 struct tcpcb *tp = intotcpcb(inp);
277 uint32_t iss = be32toh(snd_isn) - 1; /* true ISS */
278 uint32_t irs = be32toh(rcv_isn) - 1; /* true IRS */
279 uint16_t tcpopt = be16toh(opt);
280 struct flowc_tx_params ftxp;
282 INP_WLOCK_ASSERT(inp);
283 KASSERT(tp->t_state == TCPS_SYN_SENT ||
284 tp->t_state == TCPS_SYN_RECEIVED,
285 ("%s: TCP state %s", __func__, tcpstates[tp->t_state]));
287 CTR4(KTR_CXGBE, "%s: tid %d, toep %p, inp %p",
288 __func__, toep->tid, toep, inp);
290 tp->t_state = TCPS_ESTABLISHED;
291 tp->t_starttime = ticks;
292 TCPSTAT_INC(tcps_connects);
296 tp->rcv_wnd = toep->rx_credits << 10;
297 tp->rcv_adv += tp->rcv_wnd;
298 tp->last_ack_sent = tp->rcv_nxt;
301 * If we were unable to send all rx credits via opt0, save the remainder
302 * in rx_credits so that they can be handed over with the next credit
305 SOCKBUF_LOCK(&so->so_rcv);
306 bufsize = select_rcv_wnd(so);
307 SOCKBUF_UNLOCK(&so->so_rcv);
308 toep->rx_credits = bufsize - tp->rcv_wnd;
312 tp->snd_una = iss + 1;
313 tp->snd_nxt = iss + 1;
314 tp->snd_max = iss + 1;
316 assign_rxopt(tp, tcpopt);
318 SOCKBUF_LOCK(&so->so_snd);
319 if (so->so_snd.sb_flags & SB_AUTOSIZE && V_tcp_do_autosndbuf)
320 bufsize = V_tcp_autosndbuf_max;
322 bufsize = sbspace(&so->so_snd);
323 SOCKBUF_UNLOCK(&so->so_snd);
325 ftxp.snd_nxt = tp->snd_nxt;
326 ftxp.rcv_nxt = tp->rcv_nxt;
327 ftxp.snd_space = bufsize;
328 ftxp.mss = tp->t_maxseg;
329 send_flowc_wr(toep, &ftxp);
335 send_rx_credits(struct adapter *sc, struct toepcb *toep, int credits)
338 struct cpl_rx_data_ack *req;
339 uint32_t dack = F_RX_DACK_CHANGE | V_RX_DACK_MODE(1);
341 KASSERT(credits >= 0, ("%s: %d credits", __func__, credits));
343 wr = alloc_wrqe(sizeof(*req), toep->ctrlq);
348 INIT_TP_WR_MIT_CPL(req, CPL_RX_DATA_ACK, toep->tid);
349 req->credit_dack = htobe32(dack | V_RX_CREDITS(credits));
356 t4_rcvd(struct toedev *tod, struct tcpcb *tp)
358 struct adapter *sc = tod->tod_softc;
359 struct inpcb *inp = tp->t_inpcb;
360 struct socket *so = inp->inp_socket;
361 struct sockbuf *sb = &so->so_rcv;
362 struct toepcb *toep = tp->t_toe;
365 INP_WLOCK_ASSERT(inp);
368 KASSERT(toep->sb_cc >= sbused(sb),
369 ("%s: sb %p has more data (%d) than last time (%d).",
370 __func__, sb, sbused(sb), toep->sb_cc));
371 if (toep->ulp_mode == ULP_MODE_ISCSI) {
372 toep->rx_credits += toep->sb_cc;
375 toep->rx_credits += toep->sb_cc - sbused(sb);
376 toep->sb_cc = sbused(sb);
378 credits = toep->rx_credits;
382 (credits + 16384 >= tp->rcv_wnd || credits >= 15 * 1024)) {
384 credits = send_rx_credits(sc, toep, credits);
386 toep->rx_credits -= credits;
388 tp->rcv_wnd += credits;
389 tp->rcv_adv += credits;
394 * Close a connection by sending a CPL_CLOSE_CON_REQ message.
397 close_conn(struct adapter *sc, struct toepcb *toep)
400 struct cpl_close_con_req *req;
401 unsigned int tid = toep->tid;
403 CTR3(KTR_CXGBE, "%s: tid %u%s", __func__, toep->tid,
404 toep->flags & TPF_FIN_SENT ? ", IGNORED" : "");
406 if (toep->flags & TPF_FIN_SENT)
409 KASSERT(toep->flags & TPF_FLOWC_WR_SENT,
410 ("%s: flowc_wr not sent for tid %u.", __func__, tid));
412 wr = alloc_wrqe(sizeof(*req), toep->ofld_txq);
415 panic("%s: allocation failure.", __func__);
419 req->wr.wr_hi = htonl(V_FW_WR_OP(FW_TP_WR) |
420 V_FW_WR_IMMDLEN(sizeof(*req) - sizeof(req->wr)));
421 req->wr.wr_mid = htonl(V_FW_WR_LEN16(howmany(sizeof(*req), 16)) |
422 V_FW_WR_FLOWID(tid));
423 req->wr.wr_lo = cpu_to_be64(0);
424 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_CON_REQ, tid));
427 toep->flags |= TPF_FIN_SENT;
428 toep->flags &= ~TPF_SEND_FIN;
429 t4_l2t_send(sc, wr, toep->l2te);
434 #define MAX_OFLD_TX_CREDITS (SGE_MAX_WR_LEN / 16)
435 #define MIN_OFLD_TX_CREDITS (howmany(sizeof(struct fw_ofld_tx_data_wr) + 1, 16))
437 /* Maximum amount of immediate data we could stuff in a WR */
439 max_imm_payload(int tx_credits)
441 const int n = 2; /* Use only up to 2 desc for imm. data WR */
443 KASSERT(tx_credits >= 0 &&
444 tx_credits <= MAX_OFLD_TX_CREDITS,
445 ("%s: %d credits", __func__, tx_credits));
447 if (tx_credits < MIN_OFLD_TX_CREDITS)
450 if (tx_credits >= (n * EQ_ESIZE) / 16)
451 return ((n * EQ_ESIZE) - sizeof(struct fw_ofld_tx_data_wr));
453 return (tx_credits * 16 - sizeof(struct fw_ofld_tx_data_wr));
456 /* Maximum number of SGL entries we could stuff in a WR */
458 max_dsgl_nsegs(int tx_credits)
460 int nseg = 1; /* ulptx_sgl has room for 1, rest ulp_tx_sge_pair */
461 int sge_pair_credits = tx_credits - MIN_OFLD_TX_CREDITS;
463 KASSERT(tx_credits >= 0 &&
464 tx_credits <= MAX_OFLD_TX_CREDITS,
465 ("%s: %d credits", __func__, tx_credits));
467 if (tx_credits < MIN_OFLD_TX_CREDITS)
470 nseg += 2 * (sge_pair_credits * 16 / 24);
471 if ((sge_pair_credits * 16) % 24 == 16)
478 write_tx_wr(void *dst, struct toepcb *toep, unsigned int immdlen,
479 unsigned int plen, uint8_t credits, int shove, int ulp_mode)
481 struct fw_ofld_tx_data_wr *txwr = dst;
482 unsigned int wr_ulp_mode;
484 txwr->op_to_immdlen = htobe32(V_WR_OP(FW_OFLD_TX_DATA_WR) |
485 V_FW_WR_IMMDLEN(immdlen));
486 txwr->flowid_len16 = htobe32(V_FW_WR_FLOWID(toep->tid) |
487 V_FW_WR_LEN16(credits));
489 /* for iscsi, the mode & submode setting is per-packet */
490 if (toep->ulp_mode == ULP_MODE_ISCSI)
491 wr_ulp_mode = V_FW_OFLD_TX_DATA_WR_ULPMODE(ulp_mode >> 4) |
492 V_FW_OFLD_TX_DATA_WR_ULPSUBMODE(ulp_mode & 3);
494 wr_ulp_mode = V_FW_OFLD_TX_DATA_WR_ULPMODE(toep->ulp_mode);
496 txwr->lsodisable_to_proxy =
497 htobe32(wr_ulp_mode |
498 V_FW_OFLD_TX_DATA_WR_URGENT(0) | /* XXX */
499 V_FW_OFLD_TX_DATA_WR_SHOVE(shove));
500 txwr->plen = htobe32(plen);
504 * Generate a DSGL from a starting mbuf. The total number of segments and the
505 * maximum segments in any one mbuf are provided.
508 write_tx_sgl(void *dst, struct mbuf *start, struct mbuf *stop, int nsegs, int n)
511 struct ulptx_sgl *usgl = dst;
514 struct sglist_seg segs[n];
516 KASSERT(nsegs > 0, ("%s: nsegs 0", __func__));
518 sglist_init(&sg, n, segs);
519 usgl->cmd_nsge = htobe32(V_ULPTX_CMD(ULP_TX_SC_DSGL) |
520 V_ULPTX_NSGE(nsegs));
523 for (m = start; m != stop; m = m->m_next) {
524 rc = sglist_append(&sg, mtod(m, void *), m->m_len);
525 if (__predict_false(rc != 0))
526 panic("%s: sglist_append %d", __func__, rc);
528 for (j = 0; j < sg.sg_nseg; i++, j++) {
530 usgl->len0 = htobe32(segs[j].ss_len);
531 usgl->addr0 = htobe64(segs[j].ss_paddr);
533 usgl->sge[i / 2].len[i & 1] =
534 htobe32(segs[j].ss_len);
535 usgl->sge[i / 2].addr[i & 1] =
536 htobe64(segs[j].ss_paddr);
545 usgl->sge[i / 2].len[1] = htobe32(0);
546 KASSERT(nsegs == 0, ("%s: nsegs %d, start %p, stop %p",
547 __func__, nsegs, start, stop));
551 * Max number of SGL entries an offload tx work request can have. This is 41
552 * (1 + 40) for a full 512B work request.
553 * fw_ofld_tx_data_wr(16B) + ulptx_sgl(16B, 1) + ulptx_sge_pair(480B, 40)
555 #define OFLD_SGL_LEN (41)
558 * Send data and/or a FIN to the peer.
560 * The socket's so_snd buffer consists of a stream of data starting with sb_mb
561 * and linked together with m_next. sb_sndptr, if set, is the last mbuf that
564 * drop indicates the number of bytes that should be dropped from the head of
565 * the send buffer. It is an optimization that lets do_fw4_ack avoid creating
566 * contention on the send buffer lock (before this change it used to do
567 * sowwakeup and then t4_push_frames right after that when recovering from tx
568 * stalls). When drop is set this function MUST drop the bytes and wake up any
572 t4_push_frames(struct adapter *sc, struct toepcb *toep, int drop)
574 struct mbuf *sndptr, *m, *sb_sndptr;
575 struct fw_ofld_tx_data_wr *txwr;
577 u_int plen, nsegs, credits, max_imm, max_nsegs, max_nsegs_1mbuf;
578 struct inpcb *inp = toep->inp;
579 struct tcpcb *tp = intotcpcb(inp);
580 struct socket *so = inp->inp_socket;
581 struct sockbuf *sb = &so->so_snd;
582 int tx_credits, shove, compl, space, sowwakeup;
583 struct ofld_tx_sdesc *txsd = &toep->txsd[toep->txsd_pidx];
585 INP_WLOCK_ASSERT(inp);
586 KASSERT(toep->flags & TPF_FLOWC_WR_SENT,
587 ("%s: flowc_wr not sent for tid %u.", __func__, toep->tid));
589 KASSERT(toep->ulp_mode == ULP_MODE_NONE ||
590 toep->ulp_mode == ULP_MODE_TCPDDP ||
591 toep->ulp_mode == ULP_MODE_RDMA,
592 ("%s: ulp_mode %u for toep %p", __func__, toep->ulp_mode, toep));
595 * This function doesn't resume by itself. Someone else must clear the
596 * flag and call this function.
598 if (__predict_false(toep->flags & TPF_TX_SUSPENDED)) {
600 ("%s: drop (%d) != 0 but tx is suspended", __func__, drop));
605 tx_credits = min(toep->tx_credits, MAX_OFLD_TX_CREDITS);
606 max_imm = max_imm_payload(tx_credits);
607 max_nsegs = max_dsgl_nsegs(tx_credits);
612 sbdrop_locked(sb, drop);
615 sb_sndptr = sb->sb_sndptr;
616 sndptr = sb_sndptr ? sb_sndptr->m_next : sb->sb_mb;
619 max_nsegs_1mbuf = 0; /* max # of SGL segments in any one mbuf */
620 for (m = sndptr; m != NULL; m = m->m_next) {
621 int n = sglist_count(mtod(m, void *), m->m_len);
626 /* This mbuf sent us _over_ the nsegs limit, back out */
627 if (plen > max_imm && nsegs > max_nsegs) {
631 /* Too few credits */
632 toep->flags |= TPF_TX_SUSPENDED;
634 sowwakeup_locked(so);
637 SOCKBUF_UNLOCK_ASSERT(sb);
643 if (max_nsegs_1mbuf < n)
645 sb_sndptr = m; /* new sb->sb_sndptr if all goes well */
647 /* This mbuf put us right at the max_nsegs limit */
648 if (plen > max_imm && nsegs == max_nsegs) {
654 shove = m == NULL && !(tp->t_flags & TF_MORETOCOME);
657 if (space <= sb->sb_hiwat * 3 / 8 &&
658 toep->plen_nocompl + plen >= sb->sb_hiwat / 4)
663 if (sb->sb_flags & SB_AUTOSIZE &&
664 V_tcp_do_autosndbuf &&
665 sb->sb_hiwat < V_tcp_autosndbuf_max &&
666 space < sb->sb_hiwat / 8) {
667 int newsize = min(sb->sb_hiwat + V_tcp_autosndbuf_inc,
668 V_tcp_autosndbuf_max);
670 if (!sbreserve_locked(sb, newsize, so, NULL))
671 sb->sb_flags &= ~SB_AUTOSIZE;
673 sowwakeup = 1; /* room available */
676 sowwakeup_locked(so);
679 SOCKBUF_UNLOCK_ASSERT(sb);
681 /* nothing to send */
684 ("%s: nothing to send, but m != NULL", __func__));
688 if (__predict_false(toep->flags & TPF_FIN_SENT))
689 panic("%s: excess tx.", __func__);
691 if (plen <= max_imm) {
693 /* Immediate data tx */
695 wr = alloc_wrqe(roundup2(sizeof(*txwr) + plen, 16),
698 /* XXX: how will we recover from this? */
699 toep->flags |= TPF_TX_SUSPENDED;
703 credits = howmany(wr->wr_len, 16);
704 write_tx_wr(txwr, toep, plen, plen, credits, shove, 0);
705 m_copydata(sndptr, 0, plen, (void *)(txwr + 1));
712 wr_len = sizeof(*txwr) + sizeof(struct ulptx_sgl) +
713 ((3 * (nsegs - 1)) / 2 + ((nsegs - 1) & 1)) * 8;
714 wr = alloc_wrqe(roundup2(wr_len, 16), toep->ofld_txq);
716 /* XXX: how will we recover from this? */
717 toep->flags |= TPF_TX_SUSPENDED;
721 credits = howmany(wr_len, 16);
722 write_tx_wr(txwr, toep, 0, plen, credits, shove, 0);
723 write_tx_sgl(txwr + 1, sndptr, m, nsegs,
726 uint64_t *pad = (uint64_t *)
727 ((uintptr_t)txwr + wr_len);
732 KASSERT(toep->tx_credits >= credits,
733 ("%s: not enough credits", __func__));
735 toep->tx_credits -= credits;
736 toep->tx_nocompl += credits;
737 toep->plen_nocompl += plen;
738 if (toep->tx_credits <= toep->tx_total * 3 / 8 &&
739 toep->tx_nocompl >= toep->tx_total / 4)
742 if (compl || toep->ulp_mode == ULP_MODE_RDMA) {
743 txwr->op_to_immdlen |= htobe32(F_FW_WR_COMPL);
744 toep->tx_nocompl = 0;
745 toep->plen_nocompl = 0;
752 KASSERT(sb_sndptr, ("%s: sb_sndptr is NULL", __func__));
753 sb->sb_sndptr = sb_sndptr;
756 toep->flags |= TPF_TX_DATA_SENT;
757 if (toep->tx_credits < MIN_OFLD_TX_CREDITS)
758 toep->flags |= TPF_TX_SUSPENDED;
760 KASSERT(toep->txsd_avail > 0, ("%s: no txsd", __func__));
762 txsd->tx_credits = credits;
764 if (__predict_false(++toep->txsd_pidx == toep->txsd_total)) {
766 txsd = &toep->txsd[0];
770 t4_l2t_send(sc, wr, toep->l2te);
773 /* Send a FIN if requested, but only if there's no more data to send */
774 if (m == NULL && toep->flags & TPF_SEND_FIN)
775 close_conn(sc, toep);
778 /* Send ULP data over TOE using TX_DATA_WR. We send whole mbuf at once */
780 t4_ulp_push_frames(struct adapter *sc, struct toepcb *toep, int drop)
782 struct mbuf *sndptr, *m = NULL;
783 struct fw_ofld_tx_data_wr *txwr;
785 unsigned int plen, nsegs, credits, max_imm, max_nsegs, max_nsegs_1mbuf;
786 struct inpcb *inp = toep->inp;
790 int tx_credits, ulp_len = 0, ulp_mode = 0, qlen = 0;
792 struct ofld_tx_sdesc *txsd;
794 INP_WLOCK_ASSERT(inp);
795 if (toep->flags & TPF_ABORT_SHUTDOWN)
799 so = inp->inp_socket;
801 txsd = &toep->txsd[toep->txsd_pidx];
803 KASSERT(toep->flags & TPF_FLOWC_WR_SENT,
804 ("%s: flowc_wr not sent for tid %u.", __func__, toep->tid));
807 * This function doesn't resume by itself. Someone else must clear the
808 * flag and call this function.
810 if (__predict_false(toep->flags & TPF_TX_SUSPENDED))
813 sndptr = t4_queue_iscsi_callback(so, toep, 1, &qlen);
818 tx_credits = min(toep->tx_credits, MAX_OFLD_TX_CREDITS);
819 max_imm = max_imm_payload(tx_credits);
820 max_nsegs = max_dsgl_nsegs(tx_credits);
823 t4_cpl_iscsi_callback(toep->td, toep, &drop,
830 max_nsegs_1mbuf = 0; /* max # of SGL segments in any one mbuf */
831 for (m = sndptr; m != NULL; m = m->m_next) {
832 int n = sglist_count(mtod(m, void *), m->m_len);
837 /* This mbuf sent us _over_ the nsegs limit, return */
838 if (plen > max_imm && nsegs > max_nsegs) {
839 toep->flags |= TPF_TX_SUSPENDED;
843 if (max_nsegs_1mbuf < n)
846 /* This mbuf put us right at the max_nsegs limit */
847 if (plen > max_imm && nsegs == max_nsegs) {
848 toep->flags |= TPF_TX_SUSPENDED;
853 shove = m == NULL && !(tp->t_flags & TF_MORETOCOME);
854 /* nothing to send */
857 ("%s: nothing to send, but m != NULL", __func__));
861 if (__predict_false(toep->flags & TPF_FIN_SENT))
862 panic("%s: excess tx.", __func__);
864 ulp_len = plen + ulp_extra_len(sndptr, &ulp_mode);
865 if (plen <= max_imm) {
867 /* Immediate data tx */
868 wr = alloc_wrqe(roundup(sizeof(*txwr) + plen, 16),
871 /* XXX: how will we recover from this? */
872 toep->flags |= TPF_TX_SUSPENDED;
876 credits = howmany(wr->wr_len, 16);
877 write_tx_wr(txwr, toep, plen, ulp_len, credits, shove,
879 m_copydata(sndptr, 0, plen, (void *)(txwr + 1));
884 wr_len = sizeof(*txwr) + sizeof(struct ulptx_sgl) +
885 ((3 * (nsegs - 1)) / 2 + ((nsegs - 1) & 1)) * 8;
886 wr = alloc_wrqe(roundup(wr_len, 16), toep->ofld_txq);
888 /* XXX: how will we recover from this? */
889 toep->flags |= TPF_TX_SUSPENDED;
893 credits = howmany(wr_len, 16);
894 write_tx_wr(txwr, toep, 0, ulp_len, credits, shove,
896 write_tx_sgl(txwr + 1, sndptr, m, nsegs,
899 uint64_t *pad = (uint64_t *)
900 ((uintptr_t)txwr + wr_len);
905 KASSERT(toep->tx_credits >= credits,
906 ("%s: not enough credits", __func__));
908 toep->tx_credits -= credits;
909 toep->tx_nocompl += credits;
910 toep->plen_nocompl += plen;
911 if (toep->tx_credits <= toep->tx_total * 3 / 8 &&
912 toep->tx_nocompl >= toep->tx_total / 4)
916 txwr->op_to_immdlen |= htobe32(F_FW_WR_COMPL);
917 toep->tx_nocompl = 0;
918 toep->plen_nocompl = 0;
920 tp->snd_nxt += ulp_len;
921 tp->snd_max += ulp_len;
924 sndptr = m = t4_queue_iscsi_callback(so, toep, 2, &qlen);
926 toep->flags |= TPF_TX_DATA_SENT;
927 if (toep->tx_credits < MIN_OFLD_TX_CREDITS) {
928 toep->flags |= TPF_TX_SUSPENDED;
931 KASSERT(toep->txsd_avail > 0, ("%s: no txsd", __func__));
933 txsd->tx_credits = credits;
935 if (__predict_false(++toep->txsd_pidx == toep->txsd_total)) {
937 txsd = &toep->txsd[0];
941 t4_l2t_send(sc, wr, toep->l2te);
944 /* Send a FIN if requested, but only if there's no more data to send */
945 if (m == NULL && toep->flags & TPF_SEND_FIN)
946 close_conn(sc, toep);
950 t4_tod_output(struct toedev *tod, struct tcpcb *tp)
952 struct adapter *sc = tod->tod_softc;
954 struct inpcb *inp = tp->t_inpcb;
956 struct toepcb *toep = tp->t_toe;
958 INP_WLOCK_ASSERT(inp);
959 KASSERT((inp->inp_flags & INP_DROPPED) == 0,
960 ("%s: inp %p dropped.", __func__, inp));
961 KASSERT(toep != NULL, ("%s: toep is NULL", __func__));
963 t4_push_frames(sc, toep, 0);
969 t4_send_fin(struct toedev *tod, struct tcpcb *tp)
971 struct adapter *sc = tod->tod_softc;
973 struct inpcb *inp = tp->t_inpcb;
975 struct toepcb *toep = tp->t_toe;
977 INP_WLOCK_ASSERT(inp);
978 KASSERT((inp->inp_flags & INP_DROPPED) == 0,
979 ("%s: inp %p dropped.", __func__, inp));
980 KASSERT(toep != NULL, ("%s: toep is NULL", __func__));
982 toep->flags |= TPF_SEND_FIN;
983 if (tp->t_state >= TCPS_ESTABLISHED) {
984 if (toep->ulp_mode == ULP_MODE_ISCSI)
985 t4_ulp_push_frames(sc, toep, 0);
987 t4_push_frames(sc, toep, 0);
994 t4_send_rst(struct toedev *tod, struct tcpcb *tp)
996 struct adapter *sc = tod->tod_softc;
997 #if defined(INVARIANTS)
998 struct inpcb *inp = tp->t_inpcb;
1000 struct toepcb *toep = tp->t_toe;
1002 INP_WLOCK_ASSERT(inp);
1003 KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1004 ("%s: inp %p dropped.", __func__, inp));
1005 KASSERT(toep != NULL, ("%s: toep is NULL", __func__));
1008 KASSERT(toep->flags & TPF_FLOWC_WR_SENT,
1009 ("%s: flowc for tid %u [%s] not sent already",
1010 __func__, toep->tid, tcpstates[tp->t_state]));
1012 send_reset(sc, toep, 0);
1017 * Peer has sent us a FIN.
1020 do_peer_close(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
1022 struct adapter *sc = iq->adapter;
1023 const struct cpl_peer_close *cpl = (const void *)(rss + 1);
1024 unsigned int tid = GET_TID(cpl);
1025 struct toepcb *toep = lookup_tid(sc, tid);
1026 struct inpcb *inp = toep->inp;
1027 struct tcpcb *tp = NULL;
1031 unsigned int opcode = G_CPL_OPCODE(be32toh(OPCODE_TID(cpl)));
1034 KASSERT(opcode == CPL_PEER_CLOSE,
1035 ("%s: unexpected opcode 0x%x", __func__, opcode));
1036 KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
1038 if (__predict_false(toep->flags & TPF_SYNQE)) {
1040 struct synq_entry *synqe = (void *)toep;
1042 INP_WLOCK(synqe->lctx->inp);
1043 if (synqe->flags & TPF_SYNQE_HAS_L2TE) {
1044 KASSERT(synqe->flags & TPF_ABORT_SHUTDOWN,
1045 ("%s: listen socket closed but tid %u not aborted.",
1049 * do_pass_accept_req is still running and will
1050 * eventually take care of this tid.
1053 INP_WUNLOCK(synqe->lctx->inp);
1055 CTR4(KTR_CXGBE, "%s: tid %u, synqe %p (0x%x)", __func__, tid,
1060 KASSERT(toep->tid == tid, ("%s: toep tid mismatch", __func__));
1062 INP_INFO_WLOCK(&V_tcbinfo);
1064 tp = intotcpcb(inp);
1066 CTR5(KTR_CXGBE, "%s: tid %u (%s), toep_flags 0x%x, inp %p", __func__,
1067 tid, tp ? tcpstates[tp->t_state] : "no tp", toep->flags, inp);
1069 if (toep->flags & TPF_ABORT_SHUTDOWN)
1072 tp->rcv_nxt++; /* FIN */
1074 so = inp->inp_socket;
1077 if (__predict_false(toep->ddp_flags & (DDP_BUF0_ACTIVE | DDP_BUF1_ACTIVE))) {
1078 m = get_ddp_mbuf(be32toh(cpl->rcv_nxt) - tp->rcv_nxt);
1079 tp->rcv_nxt = be32toh(cpl->rcv_nxt);
1080 toep->ddp_flags &= ~(DDP_BUF0_ACTIVE | DDP_BUF1_ACTIVE);
1082 KASSERT(toep->sb_cc >= sbused(sb),
1083 ("%s: sb %p has more data (%d) than last time (%d).",
1084 __func__, sb, sbused(sb), toep->sb_cc));
1085 toep->rx_credits += toep->sb_cc - sbused(sb);
1086 #ifdef USE_DDP_RX_FLOW_CONTROL
1087 toep->rx_credits -= m->m_len; /* adjust for F_RX_FC_DDP */
1089 sbappendstream_locked(sb, m, 0);
1090 toep->sb_cc = sbused(sb);
1092 socantrcvmore_locked(so); /* unlocks the sockbuf */
1094 if (toep->ulp_mode != ULP_MODE_RDMA) {
1095 KASSERT(tp->rcv_nxt == be32toh(cpl->rcv_nxt),
1096 ("%s: rcv_nxt mismatch: %u %u", __func__, tp->rcv_nxt,
1097 be32toh(cpl->rcv_nxt)));
1100 switch (tp->t_state) {
1101 case TCPS_SYN_RECEIVED:
1102 tp->t_starttime = ticks;
1105 case TCPS_ESTABLISHED:
1106 tp->t_state = TCPS_CLOSE_WAIT;
1109 case TCPS_FIN_WAIT_1:
1110 tp->t_state = TCPS_CLOSING;
1113 case TCPS_FIN_WAIT_2:
1115 INP_UNLOCK_ASSERT(inp); /* safe, we have a ref on the inp */
1116 INP_INFO_WUNLOCK(&V_tcbinfo);
1119 final_cpl_received(toep);
1123 log(LOG_ERR, "%s: TID %u received CPL_PEER_CLOSE in state %d\n",
1124 __func__, tid, tp->t_state);
1128 INP_INFO_WUNLOCK(&V_tcbinfo);
1133 * Peer has ACK'd our FIN.
1136 do_close_con_rpl(struct sge_iq *iq, const struct rss_header *rss,
1139 struct adapter *sc = iq->adapter;
1140 const struct cpl_close_con_rpl *cpl = (const void *)(rss + 1);
1141 unsigned int tid = GET_TID(cpl);
1142 struct toepcb *toep = lookup_tid(sc, tid);
1143 struct inpcb *inp = toep->inp;
1144 struct tcpcb *tp = NULL;
1145 struct socket *so = NULL;
1147 unsigned int opcode = G_CPL_OPCODE(be32toh(OPCODE_TID(cpl)));
1150 KASSERT(opcode == CPL_CLOSE_CON_RPL,
1151 ("%s: unexpected opcode 0x%x", __func__, opcode));
1152 KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
1153 KASSERT(toep->tid == tid, ("%s: toep tid mismatch", __func__));
1155 INP_INFO_WLOCK(&V_tcbinfo);
1157 tp = intotcpcb(inp);
1159 CTR4(KTR_CXGBE, "%s: tid %u (%s), toep_flags 0x%x",
1160 __func__, tid, tp ? tcpstates[tp->t_state] : "no tp", toep->flags);
1162 if (toep->flags & TPF_ABORT_SHUTDOWN)
1165 so = inp->inp_socket;
1166 tp->snd_una = be32toh(cpl->snd_nxt) - 1; /* exclude FIN */
1168 switch (tp->t_state) {
1169 case TCPS_CLOSING: /* see TCPS_FIN_WAIT_2 in do_peer_close too */
1172 INP_UNLOCK_ASSERT(inp); /* safe, we have a ref on the inp */
1173 INP_INFO_WUNLOCK(&V_tcbinfo);
1176 final_cpl_received(toep); /* no more CPLs expected */
1184 case TCPS_FIN_WAIT_1:
1185 if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
1186 soisdisconnected(so);
1187 tp->t_state = TCPS_FIN_WAIT_2;
1192 "%s: TID %u received CPL_CLOSE_CON_RPL in state %s\n",
1193 __func__, tid, tcpstates[tp->t_state]);
1197 INP_INFO_WUNLOCK(&V_tcbinfo);
1202 send_abort_rpl(struct adapter *sc, struct sge_wrq *ofld_txq, int tid,
1206 struct cpl_abort_rpl *cpl;
1208 wr = alloc_wrqe(sizeof(*cpl), ofld_txq);
1211 panic("%s: allocation failure.", __func__);
1215 INIT_TP_WR_MIT_CPL(cpl, CPL_ABORT_RPL, tid);
1216 cpl->cmd = rst_status;
1222 abort_status_to_errno(struct tcpcb *tp, unsigned int abort_reason)
1224 switch (abort_reason) {
1225 case CPL_ERR_BAD_SYN:
1226 case CPL_ERR_CONN_RESET:
1227 return (tp->t_state == TCPS_CLOSE_WAIT ? EPIPE : ECONNRESET);
1228 case CPL_ERR_XMIT_TIMEDOUT:
1229 case CPL_ERR_PERSIST_TIMEDOUT:
1230 case CPL_ERR_FINWAIT2_TIMEDOUT:
1231 case CPL_ERR_KEEPALIVE_TIMEDOUT:
1239 cpl_not_handled(struct sge_iq *, const struct rss_header *, struct mbuf *);
1241 * tom_cpl_iscsi_callback -
1242 * iscsi and tom would share the following cpl messages, so when any of these
1243 * message is received, after tom is done with processing it, the messages
1244 * needs to be forwarded to iscsi for further processing:
1248 void (*tom_cpl_iscsi_callback)(struct tom_data *, struct socket *, void *,
1251 struct mbuf *(*tom_queue_iscsi_callback)(struct socket *, unsigned int, int *);
1253 * Check if the handler function is set for a given CPL
1254 * return 0 if the function is NULL or cpl_not_handled, 1 otherwise.
1257 t4tom_cpl_handler_registered(struct adapter *sc, unsigned int opcode)
1260 MPASS(opcode < nitems(sc->cpl_handler));
1262 return (sc->cpl_handler[opcode] &&
1263 sc->cpl_handler[opcode] != cpl_not_handled);
1267 * set the tom_cpl_iscsi_callback function, this function should be used
1268 * whenever both toe and iscsi need to process the same cpl msg.
1271 t4tom_register_cpl_iscsi_callback(void (*fp)(struct tom_data *, struct socket *,
1272 void *, unsigned int))
1275 tom_cpl_iscsi_callback = fp;
1279 t4tom_register_queue_iscsi_callback(struct mbuf *(*fp)(struct socket *,
1280 unsigned int, int *qlen))
1283 tom_queue_iscsi_callback = fp;
1287 t4_cpl_iscsi_callback(struct tom_data *td, struct toepcb *toep, void *m,
1288 unsigned int opcode)
1292 if (opcode == CPL_FW4_ACK)
1293 so = toep->inp->inp_socket;
1295 INP_WLOCK(toep->inp);
1296 so = toep->inp->inp_socket;
1297 INP_WUNLOCK(toep->inp);
1300 if (tom_cpl_iscsi_callback && so) {
1301 if (toep->ulp_mode == ULP_MODE_ISCSI) {
1302 tom_cpl_iscsi_callback(td, so, m, opcode);
1311 t4_queue_iscsi_callback(struct socket *so, struct toepcb *toep,
1312 unsigned int cmd, int *qlen)
1315 if (tom_queue_iscsi_callback && so) {
1316 if (toep->ulp_mode == ULP_MODE_ISCSI)
1317 return (tom_queue_iscsi_callback(so, cmd, qlen));
1324 * TCP RST from the peer, timeout, or some other such critical error.
1327 do_abort_req(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
1329 struct adapter *sc = iq->adapter;
1330 const struct cpl_abort_req_rss *cpl = (const void *)(rss + 1);
1331 unsigned int tid = GET_TID(cpl);
1332 struct toepcb *toep = lookup_tid(sc, tid);
1333 struct sge_wrq *ofld_txq = toep->ofld_txq;
1337 unsigned int opcode = G_CPL_OPCODE(be32toh(OPCODE_TID(cpl)));
1340 KASSERT(opcode == CPL_ABORT_REQ_RSS,
1341 ("%s: unexpected opcode 0x%x", __func__, opcode));
1342 KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
1344 if (toep->flags & TPF_SYNQE)
1345 return (do_abort_req_synqe(iq, rss, m));
1347 KASSERT(toep->tid == tid, ("%s: toep tid mismatch", __func__));
1349 if (negative_advice(cpl->status)) {
1350 CTR4(KTR_CXGBE, "%s: negative advice %d for tid %d (0x%x)",
1351 __func__, cpl->status, tid, toep->flags);
1352 return (0); /* Ignore negative advice */
1356 INP_INFO_WLOCK(&V_tcbinfo); /* for tcp_close */
1359 tp = intotcpcb(inp);
1362 "%s: tid %d (%s), toep_flags 0x%x, inp_flags 0x%x, status %d",
1363 __func__, tid, tp ? tcpstates[tp->t_state] : "no tp", toep->flags,
1364 inp->inp_flags, cpl->status);
1367 * If we'd initiated an abort earlier the reply to it is responsible for
1368 * cleaning up resources. Otherwise we tear everything down right here
1369 * right now. We owe the T4 a CPL_ABORT_RPL no matter what.
1371 if (toep->flags & TPF_ABORT_SHUTDOWN) {
1375 toep->flags |= TPF_ABORT_SHUTDOWN;
1377 if ((inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT)) == 0) {
1378 struct socket *so = inp->inp_socket;
1381 so_error_set(so, abort_status_to_errno(tp,
1385 INP_WLOCK(inp); /* re-acquire */
1388 final_cpl_received(toep);
1390 INP_INFO_WUNLOCK(&V_tcbinfo);
1391 send_abort_rpl(sc, ofld_txq, tid, CPL_ABORT_NO_RST);
1396 * Reply to the CPL_ABORT_REQ (send_reset)
1399 do_abort_rpl(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
1401 struct adapter *sc = iq->adapter;
1402 const struct cpl_abort_rpl_rss *cpl = (const void *)(rss + 1);
1403 unsigned int tid = GET_TID(cpl);
1404 struct toepcb *toep = lookup_tid(sc, tid);
1405 struct inpcb *inp = toep->inp;
1407 unsigned int opcode = G_CPL_OPCODE(be32toh(OPCODE_TID(cpl)));
1410 KASSERT(opcode == CPL_ABORT_RPL_RSS,
1411 ("%s: unexpected opcode 0x%x", __func__, opcode));
1412 KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
1414 if (toep->flags & TPF_SYNQE)
1415 return (do_abort_rpl_synqe(iq, rss, m));
1417 KASSERT(toep->tid == tid, ("%s: toep tid mismatch", __func__));
1419 CTR5(KTR_CXGBE, "%s: tid %u, toep %p, inp %p, status %d",
1420 __func__, tid, toep, inp, cpl->status);
1422 KASSERT(toep->flags & TPF_ABORT_SHUTDOWN,
1423 ("%s: wasn't expecting abort reply", __func__));
1426 final_cpl_received(toep);
1432 do_rx_data(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
1434 struct adapter *sc = iq->adapter;
1435 const struct cpl_rx_data *cpl = mtod(m, const void *);
1436 unsigned int tid = GET_TID(cpl);
1437 struct toepcb *toep = lookup_tid(sc, tid);
1438 struct inpcb *inp = toep->inp;
1443 uint32_t ddp_placed = 0;
1445 if (__predict_false(toep->flags & TPF_SYNQE)) {
1447 struct synq_entry *synqe = (void *)toep;
1449 INP_WLOCK(synqe->lctx->inp);
1450 if (synqe->flags & TPF_SYNQE_HAS_L2TE) {
1451 KASSERT(synqe->flags & TPF_ABORT_SHUTDOWN,
1452 ("%s: listen socket closed but tid %u not aborted.",
1456 * do_pass_accept_req is still running and will
1457 * eventually take care of this tid.
1460 INP_WUNLOCK(synqe->lctx->inp);
1462 CTR4(KTR_CXGBE, "%s: tid %u, synqe %p (0x%x)", __func__, tid,
1468 KASSERT(toep->tid == tid, ("%s: toep tid mismatch", __func__));
1470 /* strip off CPL header */
1471 m_adj(m, sizeof(*cpl));
1472 len = m->m_pkthdr.len;
1475 if (inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT)) {
1476 CTR4(KTR_CXGBE, "%s: tid %u, rx (%d bytes), inp_flags 0x%x",
1477 __func__, tid, len, inp->inp_flags);
1483 tp = intotcpcb(inp);
1485 if (__predict_false(tp->rcv_nxt != be32toh(cpl->seq)))
1486 ddp_placed = be32toh(cpl->seq) - tp->rcv_nxt;
1489 KASSERT(tp->rcv_wnd >= len, ("%s: negative window size", __func__));
1491 tp->t_rcvtime = ticks;
1493 so = inp_inpcbtosocket(inp);
1497 if (__predict_false(sb->sb_state & SBS_CANTRCVMORE)) {
1498 CTR3(KTR_CXGBE, "%s: tid %u, excess rx (%d bytes)",
1499 __func__, tid, len);
1504 INP_INFO_WLOCK(&V_tcbinfo);
1506 tp = tcp_drop(tp, ECONNRESET);
1509 INP_INFO_WUNLOCK(&V_tcbinfo);
1514 /* receive buffer autosize */
1515 if (sb->sb_flags & SB_AUTOSIZE &&
1516 V_tcp_do_autorcvbuf &&
1517 sb->sb_hiwat < V_tcp_autorcvbuf_max &&
1518 len > (sbspace(sb) / 8 * 7)) {
1519 unsigned int hiwat = sb->sb_hiwat;
1520 unsigned int newsize = min(hiwat + V_tcp_autorcvbuf_inc,
1521 V_tcp_autorcvbuf_max);
1523 if (!sbreserve_locked(sb, newsize, so, NULL))
1524 sb->sb_flags &= ~SB_AUTOSIZE;
1526 toep->rx_credits += newsize - hiwat;
1529 if (toep->ulp_mode == ULP_MODE_TCPDDP) {
1530 int changed = !(toep->ddp_flags & DDP_ON) ^ cpl->ddp_off;
1533 if (toep->ddp_flags & DDP_SC_REQ)
1534 toep->ddp_flags ^= DDP_ON | DDP_SC_REQ;
1536 KASSERT(cpl->ddp_off == 1,
1537 ("%s: DDP switched on by itself.",
1540 /* Fell out of DDP mode */
1541 toep->ddp_flags &= ~(DDP_ON | DDP_BUF0_ACTIVE |
1545 insert_ddp_data(toep, ddp_placed);
1549 if ((toep->ddp_flags & DDP_OK) == 0 &&
1550 time_uptime >= toep->ddp_disabled + DDP_RETRY_WAIT) {
1551 toep->ddp_score = DDP_LOW_SCORE;
1552 toep->ddp_flags |= DDP_OK;
1553 CTR3(KTR_CXGBE, "%s: tid %u DDP_OK @ %u",
1554 __func__, tid, time_uptime);
1557 if (toep->ddp_flags & DDP_ON) {
1560 * CPL_RX_DATA with DDP on can only be an indicate. Ask
1561 * soreceive to post a buffer or disable DDP. The
1562 * payload that arrived in this indicate is appended to
1563 * the socket buffer as usual.
1568 "%s: tid %u (0x%x) DDP indicate (seq 0x%x, len %d)",
1569 __func__, tid, toep->flags, be32toh(cpl->seq), len);
1571 sb->sb_flags |= SB_DDP_INDICATE;
1572 } else if ((toep->ddp_flags & (DDP_OK|DDP_SC_REQ)) == DDP_OK &&
1573 tp->rcv_wnd > DDP_RSVD_WIN && len >= sc->tt.ddp_thres) {
1576 * DDP allowed but isn't on (and a request to switch it
1577 * on isn't pending either), and conditions are ripe for
1578 * it to work. Switch it on.
1581 enable_ddp(sc, toep);
1585 KASSERT(toep->sb_cc >= sbused(sb),
1586 ("%s: sb %p has more data (%d) than last time (%d).",
1587 __func__, sb, sbused(sb), toep->sb_cc));
1588 toep->rx_credits += toep->sb_cc - sbused(sb);
1589 sbappendstream_locked(sb, m, 0);
1590 toep->sb_cc = sbused(sb);
1591 sorwakeup_locked(so);
1592 SOCKBUF_UNLOCK_ASSERT(sb);
1598 #define S_CPL_FW4_ACK_OPCODE 24
1599 #define M_CPL_FW4_ACK_OPCODE 0xff
1600 #define V_CPL_FW4_ACK_OPCODE(x) ((x) << S_CPL_FW4_ACK_OPCODE)
1601 #define G_CPL_FW4_ACK_OPCODE(x) \
1602 (((x) >> S_CPL_FW4_ACK_OPCODE) & M_CPL_FW4_ACK_OPCODE)
1604 #define S_CPL_FW4_ACK_FLOWID 0
1605 #define M_CPL_FW4_ACK_FLOWID 0xffffff
1606 #define V_CPL_FW4_ACK_FLOWID(x) ((x) << S_CPL_FW4_ACK_FLOWID)
1607 #define G_CPL_FW4_ACK_FLOWID(x) \
1608 (((x) >> S_CPL_FW4_ACK_FLOWID) & M_CPL_FW4_ACK_FLOWID)
1610 #define S_CPL_FW4_ACK_CR 24
1611 #define M_CPL_FW4_ACK_CR 0xff
1612 #define V_CPL_FW4_ACK_CR(x) ((x) << S_CPL_FW4_ACK_CR)
1613 #define G_CPL_FW4_ACK_CR(x) (((x) >> S_CPL_FW4_ACK_CR) & M_CPL_FW4_ACK_CR)
1615 #define S_CPL_FW4_ACK_SEQVAL 0
1616 #define M_CPL_FW4_ACK_SEQVAL 0x1
1617 #define V_CPL_FW4_ACK_SEQVAL(x) ((x) << S_CPL_FW4_ACK_SEQVAL)
1618 #define G_CPL_FW4_ACK_SEQVAL(x) \
1619 (((x) >> S_CPL_FW4_ACK_SEQVAL) & M_CPL_FW4_ACK_SEQVAL)
1620 #define F_CPL_FW4_ACK_SEQVAL V_CPL_FW4_ACK_SEQVAL(1U)
1623 do_fw4_ack(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
1625 struct adapter *sc = iq->adapter;
1626 const struct cpl_fw4_ack *cpl = (const void *)(rss + 1);
1627 unsigned int tid = G_CPL_FW4_ACK_FLOWID(be32toh(OPCODE_TID(cpl)));
1628 struct toepcb *toep = lookup_tid(sc, tid);
1632 uint8_t credits = cpl->credits;
1633 struct ofld_tx_sdesc *txsd;
1636 unsigned int opcode = G_CPL_FW4_ACK_OPCODE(be32toh(OPCODE_TID(cpl)));
1640 * Very unusual case: we'd sent a flowc + abort_req for a synq entry and
1641 * now this comes back carrying the credits for the flowc.
1643 if (__predict_false(toep->flags & TPF_SYNQE)) {
1644 KASSERT(toep->flags & TPF_ABORT_SHUTDOWN,
1645 ("%s: credits for a synq entry %p", __func__, toep));
1651 KASSERT(opcode == CPL_FW4_ACK,
1652 ("%s: unexpected opcode 0x%x", __func__, opcode));
1653 KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
1654 KASSERT(toep->tid == tid, ("%s: toep tid mismatch", __func__));
1658 if (__predict_false(toep->flags & TPF_ABORT_SHUTDOWN)) {
1663 KASSERT((inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) == 0,
1664 ("%s: inp_flags 0x%x", __func__, inp->inp_flags));
1666 tp = intotcpcb(inp);
1668 if (cpl->flags & CPL_FW4_ACK_FLAGS_SEQVAL) {
1669 tcp_seq snd_una = be32toh(cpl->snd_una);
1672 if (__predict_false(SEQ_LT(snd_una, tp->snd_una))) {
1674 "%s: unexpected seq# %x for TID %u, snd_una %x\n",
1675 __func__, snd_una, toep->tid, tp->snd_una);
1679 if (tp->snd_una != snd_una) {
1680 tp->snd_una = snd_una;
1681 tp->ts_recent_age = tcp_ts_getticks();
1685 so = inp->inp_socket;
1686 txsd = &toep->txsd[toep->txsd_cidx];
1689 KASSERT(credits >= txsd->tx_credits,
1690 ("%s: too many (or partial) credits", __func__));
1691 credits -= txsd->tx_credits;
1692 toep->tx_credits += txsd->tx_credits;
1696 KASSERT(toep->txsd_avail <= toep->txsd_total,
1697 ("%s: txsd avail > total", __func__));
1698 if (__predict_false(++toep->txsd_cidx == toep->txsd_total)) {
1699 txsd = &toep->txsd[0];
1700 toep->txsd_cidx = 0;
1704 if (toep->tx_credits == toep->tx_total) {
1705 toep->tx_nocompl = 0;
1706 toep->plen_nocompl = 0;
1709 if (toep->flags & TPF_TX_SUSPENDED &&
1710 toep->tx_credits >= toep->tx_total / 4) {
1711 toep->flags &= ~TPF_TX_SUSPENDED;
1712 if (toep->ulp_mode == ULP_MODE_ISCSI)
1713 t4_ulp_push_frames(sc, toep, plen);
1715 t4_push_frames(sc, toep, plen);
1716 } else if (plen > 0) {
1717 struct sockbuf *sb = &so->so_snd;
1719 if (toep->ulp_mode == ULP_MODE_ISCSI)
1720 t4_cpl_iscsi_callback(toep->td, toep, &plen,
1724 sbdrop_locked(sb, plen);
1725 sowwakeup_locked(so);
1726 SOCKBUF_UNLOCK_ASSERT(sb);
1736 do_set_tcb_rpl(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
1738 struct adapter *sc = iq->adapter;
1739 const struct cpl_set_tcb_rpl *cpl = (const void *)(rss + 1);
1740 unsigned int tid = GET_TID(cpl);
1742 unsigned int opcode = G_CPL_OPCODE(be32toh(OPCODE_TID(cpl)));
1745 KASSERT(opcode == CPL_SET_TCB_RPL,
1746 ("%s: unexpected opcode 0x%x", __func__, opcode));
1747 KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
1749 if (is_ftid(sc, tid))
1750 return (t4_filter_rpl(iq, rss, m)); /* TCB is a filter */
1752 struct toepcb *toep = lookup_tid(sc, tid);
1754 t4_cpl_iscsi_callback(toep->td, toep, m, CPL_SET_TCB_RPL);
1758 CXGBE_UNIMPLEMENTED(__func__);
1762 t4_set_tcb_field(struct adapter *sc, struct toepcb *toep, int ctrl,
1763 uint16_t word, uint64_t mask, uint64_t val)
1766 struct cpl_set_tcb_field *req;
1768 wr = alloc_wrqe(sizeof(*req), ctrl ? toep->ctrlq : toep->ofld_txq);
1771 panic("%s: allocation failure.", __func__);
1775 INIT_TP_WR_MIT_CPL(req, CPL_SET_TCB_FIELD, toep->tid);
1776 req->reply_ctrl = htobe16(V_NO_REPLY(1) |
1777 V_QUEUENO(toep->ofld_rxq->iq.abs_id));
1778 req->word_cookie = htobe16(V_WORD(word) | V_COOKIE(0));
1779 req->mask = htobe64(mask);
1780 req->val = htobe64(val);
1786 t4_init_cpl_io_handlers(struct adapter *sc)
1789 t4_register_cpl_handler(sc, CPL_PEER_CLOSE, do_peer_close);
1790 t4_register_cpl_handler(sc, CPL_CLOSE_CON_RPL, do_close_con_rpl);
1791 t4_register_cpl_handler(sc, CPL_ABORT_REQ_RSS, do_abort_req);
1792 t4_register_cpl_handler(sc, CPL_ABORT_RPL_RSS, do_abort_rpl);
1793 t4_register_cpl_handler(sc, CPL_RX_DATA, do_rx_data);
1794 t4_register_cpl_handler(sc, CPL_FW4_ACK, do_fw4_ack);
1795 t4_register_cpl_handler(sc, CPL_SET_TCB_RPL, do_set_tcb_rpl);
1799 t4_uninit_cpl_io_handlers(struct adapter *sc)
1802 t4_register_cpl_handler(sc, CPL_SET_TCB_RPL, t4_filter_rpl);