t4_cpl_io.c revision 274421
1123992Ssobomax/*-
2103026Ssobomax * Copyright (c) 2012 Chelsio Communications, Inc.
3103026Ssobomax * All rights reserved.
4103026Ssobomax * Written by: Navdeep Parhar <np@FreeBSD.org>
5103026Ssobomax *
6103026Ssobomax * Redistribution and use in source and binary forms, with or without
7103026Ssobomax * modification, are permitted provided that the following conditions
8103026Ssobomax * are met:
9103026Ssobomax * 1. Redistributions of source code must retain the above copyright
10103026Ssobomax *    notice, this list of conditions and the following disclaimer.
11103026Ssobomax * 2. Redistributions in binary form must reproduce the above copyright
12103026Ssobomax *    notice, this list of conditions and the following disclaimer in the
13103026Ssobomax *    documentation and/or other materials provided with the distribution.
14103026Ssobomax *
15103026Ssobomax * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16103026Ssobomax * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17103026Ssobomax * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18103026Ssobomax * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19103026Ssobomax * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20103026Ssobomax * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21103026Ssobomax * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22103026Ssobomax * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23103026Ssobomax * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24103026Ssobomax * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25103026Ssobomax * SUCH DAMAGE.
26103026Ssobomax */
27103026Ssobomax
28103026Ssobomax#include <sys/cdefs.h>
29103026Ssobomax__FBSDID("$FreeBSD: head/sys/dev/cxgbe/tom/t4_cpl_io.c 274421 2014-11-12 09:57:15Z glebius $");
30103026Ssobomax
31103026Ssobomax#include "opt_inet.h"
32103026Ssobomax
33103026Ssobomax#ifdef TCP_OFFLOAD
34103026Ssobomax#include <sys/param.h>
35103026Ssobomax#include <sys/types.h>
36103026Ssobomax#include <sys/kernel.h>
37103026Ssobomax#include <sys/ktr.h>
38103026Ssobomax#include <sys/module.h>
39103026Ssobomax#include <sys/protosw.h>
40103026Ssobomax#include <sys/domain.h>
41103026Ssobomax#include <sys/socket.h>
42103026Ssobomax#include <sys/socketvar.h>
43103026Ssobomax#include <sys/sglist.h>
44103026Ssobomax#include <netinet/in.h>
45103026Ssobomax#include <netinet/in_pcb.h>
46103026Ssobomax#include <netinet/ip.h>
47103026Ssobomax#include <netinet/tcp_var.h>
48103026Ssobomax#define TCPSTATES
49103394Sbde#include <netinet/tcp_fsm.h>
50103026Ssobomax#include <netinet/tcp_seq.h>
51122699Sbms#include <netinet/toecore.h>
52103026Ssobomax
53103026Ssobomax#include "common/common.h"
54103026Ssobomax#include "common/t4_msg.h"
55103026Ssobomax#include "common/t4_regs.h"
56103026Ssobomax#include "common/t4_tcb.h"
57103026Ssobomax#include "tom/t4_tom_l2t.h"
58103026Ssobomax#include "tom/t4_tom.h"
59103026Ssobomax
60103026SsobomaxVNET_DECLARE(int, tcp_do_autosndbuf);
61103344Sbde#define V_tcp_do_autosndbuf VNET(tcp_do_autosndbuf)
62103026SsobomaxVNET_DECLARE(int, tcp_autosndbuf_inc);
63103026Ssobomax#define V_tcp_autosndbuf_inc VNET(tcp_autosndbuf_inc)
64103026SsobomaxVNET_DECLARE(int, tcp_autosndbuf_max);
65103026Ssobomax#define V_tcp_autosndbuf_max VNET(tcp_autosndbuf_max)
66103026SsobomaxVNET_DECLARE(int, tcp_do_autorcvbuf);
67103026Ssobomax#define V_tcp_do_autorcvbuf VNET(tcp_do_autorcvbuf)
68103026SsobomaxVNET_DECLARE(int, tcp_autorcvbuf_inc);
69103026Ssobomax#define V_tcp_autorcvbuf_inc VNET(tcp_autorcvbuf_inc)
70103026SsobomaxVNET_DECLARE(int, tcp_autorcvbuf_max);
71103026Ssobomax#define V_tcp_autorcvbuf_max VNET(tcp_autorcvbuf_max)
72103026Ssobomax
73103026Ssobomax/*
74103026Ssobomax * For ULP connections HW may add headers, e.g., for digests, that aren't part
75103026Ssobomax * of the messages sent by the host but that are part of the TCP payload and
76103026Ssobomax * therefore consume TCP sequence space.  Tx connection parameters that
77103026Ssobomax * operate in TCP sequence space are affected by the HW additions and need to
78103026Ssobomax * compensate for them to accurately track TCP sequence numbers. This array
79103026Ssobomax * contains the compensating extra lengths for ULP packets.  It is indexed by
80103026Ssobomax * a packet's ULP submode.
81103026Ssobomax */
82103026Ssobomaxconst unsigned int t4_ulp_extra_len[] = {0, 4, 4, 8};
83103026Ssobomax
84103026Ssobomax/*
85103026Ssobomax * Return the length of any HW additions that will be made to a Tx packet.
86103026Ssobomax * Such additions can happen for some types of ULP packets.
87103026Ssobomax */
88103026Ssobomaxstatic inline unsigned int
89103026Ssobomaxulp_extra_len(struct mbuf *m, int *ulp_mode)
90103026Ssobomax{
91103026Ssobomax	struct m_tag    *mtag;
92103026Ssobomax
93103026Ssobomax	if ((mtag = m_tag_find(m, CXGBE_ISCSI_MBUF_TAG, NULL)) == NULL)
94127307Srwatson		return (0);
95127307Srwatson	*ulp_mode = *((int *)(mtag + 1));
96127307Srwatson
97127307Srwatson	return (t4_ulp_extra_len[*ulp_mode & 3]);
98127307Srwatson}
99103026Ssobomax
100103026Ssobomaxvoid
101103026Ssobomaxsend_flowc_wr(struct toepcb *toep, struct flowc_tx_params *ftxp)
102103026Ssobomax{
103105300Salfred	struct wrqe *wr;
104105300Salfred	struct fw_flowc_wr *flowc;
105103032Ssobomax	unsigned int nparams = ftxp ? 8 : 6, flowclen;
106103032Ssobomax	struct port_info *pi = toep->port;
107103032Ssobomax	struct adapter *sc = pi->adapter;
108103026Ssobomax	unsigned int pfvf = G_FW_VIID_PFN(pi->viid) << S_FW_VIID_PFN;
109103032Ssobomax	struct ofld_tx_sdesc *txsd = &toep->txsd[toep->txsd_pidx];
110103026Ssobomax
111103026Ssobomax	KASSERT(!(toep->flags & TPF_FLOWC_WR_SENT),
112103032Ssobomax	    ("%s: flowc for tid %u sent already", __func__, toep->tid));
113103026Ssobomax
114105300Salfred	CTR2(KTR_CXGBE, "%s: tid %u", __func__, toep->tid);
115103026Ssobomax
116103026Ssobomax	flowclen = sizeof(*flowc) + nparams * sizeof(struct fw_flowc_mnemval);
117103026Ssobomax
118103026Ssobomax	wr = alloc_wrqe(roundup2(flowclen, 16), toep->ofld_txq);
119103026Ssobomax	if (wr == NULL) {
120103026Ssobomax		/* XXX */
121103026Ssobomax		panic("%s: allocation failure.", __func__);
122103026Ssobomax	}
123103026Ssobomax	flowc = wrtod(wr);
124103026Ssobomax	memset(flowc, 0, wr->wr_len);
125103026Ssobomax
126103026Ssobomax	flowc->op_to_nparams = htobe32(V_FW_WR_OP(FW_FLOWC_WR) |
127103026Ssobomax	    V_FW_FLOWC_WR_NPARAMS(nparams));
128103026Ssobomax	flowc->flowid_len16 = htonl(V_FW_WR_LEN16(howmany(flowclen, 16)) |
129103026Ssobomax	    V_FW_WR_FLOWID(toep->tid));
130103026Ssobomax
131103026Ssobomax	flowc->mnemval[0].mnemonic = FW_FLOWC_MNEM_PFNVFN;
132103026Ssobomax	flowc->mnemval[0].val = htobe32(pfvf);
133103026Ssobomax	flowc->mnemval[1].mnemonic = FW_FLOWC_MNEM_CH;
134103026Ssobomax	flowc->mnemval[1].val = htobe32(pi->tx_chan);
135123338Sbms	flowc->mnemval[2].mnemonic = FW_FLOWC_MNEM_PORT;
136103026Ssobomax	flowc->mnemval[2].val = htobe32(pi->tx_chan);
137103026Ssobomax	flowc->mnemval[3].mnemonic = FW_FLOWC_MNEM_IQID;
138103026Ssobomax	flowc->mnemval[3].val = htobe32(toep->ofld_rxq->iq.abs_id);
139103026Ssobomax	if (ftxp) {
140103026Ssobomax		uint32_t sndbuf = min(ftxp->snd_space, sc->tt.sndbuf);
141103026Ssobomax
142103026Ssobomax		flowc->mnemval[4].mnemonic = FW_FLOWC_MNEM_SNDNXT;
143103026Ssobomax		flowc->mnemval[4].val = htobe32(ftxp->snd_nxt);
144103026Ssobomax		flowc->mnemval[5].mnemonic = FW_FLOWC_MNEM_RCVNXT;
145103026Ssobomax		flowc->mnemval[5].val = htobe32(ftxp->rcv_nxt);
146103026Ssobomax		flowc->mnemval[6].mnemonic = FW_FLOWC_MNEM_SNDBUF;
147103026Ssobomax		flowc->mnemval[6].val = htobe32(sndbuf);
148103026Ssobomax		flowc->mnemval[7].mnemonic = FW_FLOWC_MNEM_MSS;
149103026Ssobomax		flowc->mnemval[7].val = htobe32(ftxp->mss);
150103026Ssobomax	} else {
151103026Ssobomax		flowc->mnemval[4].mnemonic = FW_FLOWC_MNEM_SNDBUF;
152103026Ssobomax		flowc->mnemval[4].val = htobe32(512);
153103032Ssobomax		flowc->mnemval[5].mnemonic = FW_FLOWC_MNEM_MSS;
154103026Ssobomax		flowc->mnemval[5].val = htobe32(512);
155103026Ssobomax	}
156103026Ssobomax
157127307Srwatson	txsd->tx_credits = howmany(flowclen, 16);
158103026Ssobomax	txsd->plen = 0;
159103026Ssobomax	KASSERT(toep->tx_credits >= txsd->tx_credits && toep->txsd_avail > 0,
160103026Ssobomax	    ("%s: not enough credits (%d)", __func__, toep->tx_credits));
161103026Ssobomax	toep->tx_credits -= txsd->tx_credits;
162103032Ssobomax	if (__predict_false(++toep->txsd_pidx == toep->txsd_total))
163103026Ssobomax		toep->txsd_pidx = 0;
164103026Ssobomax	toep->txsd_avail--;
165103026Ssobomax
166103026Ssobomax	toep->flags |= TPF_FLOWC_WR_SENT;
167103026Ssobomax        t4_wrq_tx(sc, wr);
168103026Ssobomax}
169111119Simp
170103026Ssobomaxvoid
171103026Ssobomaxsend_reset(struct adapter *sc, struct toepcb *toep, uint32_t snd_nxt)
172121816Sbrooks{
173103026Ssobomax	struct wrqe *wr;
174103026Ssobomax	struct cpl_abort_req *req;
175123338Sbms	int tid = toep->tid;
176103026Ssobomax	struct inpcb *inp = toep->inp;
177103026Ssobomax	struct tcpcb *tp = intotcpcb(inp);	/* don't use if INP_DROPPED */
178103026Ssobomax
179103026Ssobomax	INP_WLOCK_ASSERT(inp);
180103026Ssobomax
181103026Ssobomax	CTR6(KTR_CXGBE, "%s: tid %d (%s), toep_flags 0x%x, inp_flags 0x%x%s",
182103026Ssobomax	    __func__, toep->tid,
183103026Ssobomax	    inp->inp_flags & INP_DROPPED ? "inp dropped" :
184103026Ssobomax	    tcpstates[tp->t_state],
185103026Ssobomax	    toep->flags, inp->inp_flags,
186103026Ssobomax	    toep->flags & TPF_ABORT_SHUTDOWN ?
187125024Ssobomax	    " (abort already in progress)" : "");
188103026Ssobomax
189103026Ssobomax	if (toep->flags & TPF_ABORT_SHUTDOWN)
190127307Srwatson		return;	/* abort already in progress */
191103026Ssobomax
192127307Srwatson	toep->flags |= TPF_ABORT_SHUTDOWN;
193103026Ssobomax
194103026Ssobomax	KASSERT(toep->flags & TPF_FLOWC_WR_SENT,
195103026Ssobomax	    ("%s: flowc_wr not sent for tid %d.", __func__, tid));
196103032Ssobomax
197127307Srwatson	wr = alloc_wrqe(sizeof(*req), toep->ofld_txq);
198103026Ssobomax	if (wr == NULL) {
199103026Ssobomax		/* XXX */
200103026Ssobomax		panic("%s: allocation failure.", __func__);
201103026Ssobomax	}
202103026Ssobomax	req = wrtod(wr);
203103026Ssobomax
204127307Srwatson	INIT_TP_WR_MIT_CPL(req, CPL_ABORT_REQ, tid);
205127307Srwatson	if (inp->inp_flags & INP_DROPPED)
206103026Ssobomax		req->rsvd0 = htobe32(snd_nxt);
207103026Ssobomax	else
208103026Ssobomax		req->rsvd0 = htobe32(tp->snd_nxt);
209127307Srwatson	req->rsvd1 = !(toep->flags & TPF_TX_DATA_SENT);
210127307Srwatson	req->cmd = CPL_ABORT_SEND_RST;
211127307Srwatson
212127307Srwatson	/*
213127307Srwatson	 * XXX: What's the correct way to tell that the inp hasn't been detached
214127307Srwatson	 * from its socket?  Should I even be flushing the snd buffer here?
215127307Srwatson	 */
216127307Srwatson	if ((inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT)) == 0) {
217127307Srwatson		struct socket *so = inp->inp_socket;
218127307Srwatson
219127307Srwatson		if (so != NULL)	/* because I'm not sure.  See comment above */
220127307Srwatson			sbflush(&so->so_snd);
221103026Ssobomax	}
222103026Ssobomax
223103026Ssobomax	t4_l2t_send(sc, wr, toep->l2te);
224103026Ssobomax}
225103032Ssobomax
226103026Ssobomax/*
227103026Ssobomax * Called when a connection is established to translate the TCP options
228103026Ssobomax * reported by HW to FreeBSD's native format.
229103026Ssobomax */
230103026Ssobomaxstatic void
231103026Ssobomaxassign_rxopt(struct tcpcb *tp, unsigned int opt)
232103026Ssobomax{
233123992Ssobomax	struct toepcb *toep = tp->t_toe;
234103026Ssobomax	struct adapter *sc = td_adapter(toep->td);
235103026Ssobomax
236103026Ssobomax	INP_LOCK_ASSERT(tp->t_inpcb);
237103026Ssobomax
238103026Ssobomax	tp->t_maxseg = tp->t_maxopd = sc->params.mtus[G_TCPOPT_MSS(opt)] - 40;
239103026Ssobomax
240103026Ssobomax	if (G_TCPOPT_TSTAMP(opt)) {
241103026Ssobomax		tp->t_flags |= TF_RCVD_TSTMP;	/* timestamps ok */
242103026Ssobomax		tp->ts_recent = 0;		/* hmmm */
243103026Ssobomax		tp->ts_recent_age = tcp_ts_getticks();
244103026Ssobomax		tp->t_maxseg -= TCPOLEN_TSTAMP_APPA;
245103026Ssobomax	}
246103026Ssobomax
247103026Ssobomax	if (G_TCPOPT_SACK(opt))
248103026Ssobomax		tp->t_flags |= TF_SACK_PERMIT;	/* should already be set */
249103026Ssobomax	else
250103026Ssobomax		tp->t_flags &= ~TF_SACK_PERMIT;	/* sack disallowed by peer */
251103026Ssobomax
252103026Ssobomax	if (G_TCPOPT_WSCALE_OK(opt))
253103026Ssobomax		tp->t_flags |= TF_RCVD_SCALE;
254103026Ssobomax
255103026Ssobomax	/* Doing window scaling? */
256103026Ssobomax	if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
257103026Ssobomax	    (TF_RCVD_SCALE | TF_REQ_SCALE)) {
258103026Ssobomax		tp->rcv_scale = tp->request_r_scale;
259103026Ssobomax		tp->snd_scale = G_TCPOPT_SND_WSCALE(opt);
260123922Ssam	}
261103026Ssobomax}
262103026Ssobomax
263103026Ssobomax/*
264103026Ssobomax * Completes some final bits of initialization for just established connections
265103026Ssobomax * and changes their state to TCPS_ESTABLISHED.
266103026Ssobomax *
267103026Ssobomax * The ISNs are from after the exchange of SYNs.  i.e., the true ISN + 1.
268103026Ssobomax */
269103026Ssobomaxvoid
270103026Ssobomaxmake_established(struct toepcb *toep, uint32_t snd_isn, uint32_t rcv_isn,
271103026Ssobomax    uint16_t opt)
272103026Ssobomax{
273103026Ssobomax	struct inpcb *inp = toep->inp;
274103026Ssobomax	struct socket *so = inp->inp_socket;
275103026Ssobomax	struct tcpcb *tp = intotcpcb(inp);
276103026Ssobomax	long bufsize;
277103026Ssobomax	uint32_t iss = be32toh(snd_isn) - 1;	/* true ISS */
278103026Ssobomax	uint32_t irs = be32toh(rcv_isn) - 1;	/* true IRS */
279103026Ssobomax	uint16_t tcpopt = be16toh(opt);
280103026Ssobomax	struct flowc_tx_params ftxp;
281103026Ssobomax
282103026Ssobomax	INP_WLOCK_ASSERT(inp);
283103026Ssobomax	KASSERT(tp->t_state == TCPS_SYN_SENT ||
284103026Ssobomax	    tp->t_state == TCPS_SYN_RECEIVED,
285103026Ssobomax	    ("%s: TCP state %s", __func__, tcpstates[tp->t_state]));
286103026Ssobomax
287103026Ssobomax	CTR4(KTR_CXGBE, "%s: tid %d, toep %p, inp %p",
288103026Ssobomax	    __func__, toep->tid, toep, inp);
289103026Ssobomax
290103026Ssobomax	tp->t_state = TCPS_ESTABLISHED;
291103026Ssobomax	tp->t_starttime = ticks;
292103026Ssobomax	TCPSTAT_INC(tcps_connects);
293103026Ssobomax
294103026Ssobomax	tp->irs = irs;
295103026Ssobomax	tcp_rcvseqinit(tp);
296103026Ssobomax	tp->rcv_wnd = toep->rx_credits << 10;
297103026Ssobomax	tp->rcv_adv += tp->rcv_wnd;
298103026Ssobomax	tp->last_ack_sent = tp->rcv_nxt;
299103026Ssobomax
300103026Ssobomax	/*
301123992Ssobomax	 * If we were unable to send all rx credits via opt0, save the remainder
302103026Ssobomax	 * in rx_credits so that they can be handed over with the next credit
303103026Ssobomax	 * update.
304103026Ssobomax	 */
305111119Simp	SOCKBUF_LOCK(&so->so_rcv);
306103026Ssobomax	bufsize = select_rcv_wnd(so);
307103026Ssobomax	SOCKBUF_UNLOCK(&so->so_rcv);
308103026Ssobomax	toep->rx_credits = bufsize - tp->rcv_wnd;
309103026Ssobomax
310103026Ssobomax	tp->iss = iss;
311103026Ssobomax	tcp_sendseqinit(tp);
312103026Ssobomax	tp->snd_una = iss + 1;
313103026Ssobomax	tp->snd_nxt = iss + 1;
314103026Ssobomax	tp->snd_max = iss + 1;
315103026Ssobomax
316103026Ssobomax	assign_rxopt(tp, tcpopt);
317103026Ssobomax
318103026Ssobomax	SOCKBUF_LOCK(&so->so_snd);
319103026Ssobomax	if (so->so_snd.sb_flags & SB_AUTOSIZE && V_tcp_do_autosndbuf)
320103026Ssobomax		bufsize = V_tcp_autosndbuf_max;
321103026Ssobomax	else
322103026Ssobomax		bufsize = sbspace(&so->so_snd);
323103026Ssobomax	SOCKBUF_UNLOCK(&so->so_snd);
324103026Ssobomax
325103026Ssobomax	ftxp.snd_nxt = tp->snd_nxt;
326103026Ssobomax	ftxp.rcv_nxt = tp->rcv_nxt;
327103026Ssobomax	ftxp.snd_space = bufsize;
328103026Ssobomax	ftxp.mss = tp->t_maxseg;
329103026Ssobomax	send_flowc_wr(toep, &ftxp);
330103026Ssobomax
331103026Ssobomax	soisconnected(so);
332103026Ssobomax}
333103026Ssobomax
334103026Ssobomaxstatic int
335103026Ssobomaxsend_rx_credits(struct adapter *sc, struct toepcb *toep, int credits)
336103026Ssobomax{
337103026Ssobomax	struct wrqe *wr;
338103026Ssobomax	struct cpl_rx_data_ack *req;
339103026Ssobomax	uint32_t dack = F_RX_DACK_CHANGE | V_RX_DACK_MODE(1);
340103026Ssobomax
341103026Ssobomax	KASSERT(credits >= 0, ("%s: %d credits", __func__, credits));
342103026Ssobomax
343103026Ssobomax	wr = alloc_wrqe(sizeof(*req), toep->ctrlq);
344103026Ssobomax	if (wr == NULL)
345103026Ssobomax		return (0);
346103026Ssobomax	req = wrtod(wr);
347103026Ssobomax
348103026Ssobomax	INIT_TP_WR_MIT_CPL(req, CPL_RX_DATA_ACK, toep->tid);
349103026Ssobomax	req->credit_dack = htobe32(dack | V_RX_CREDITS(credits));
350103026Ssobomax
351103026Ssobomax	t4_wrq_tx(sc, wr);
352103026Ssobomax	return (credits);
353103026Ssobomax}
354111119Simp
355103026Ssobomaxvoid
356103026Ssobomaxt4_rcvd(struct toedev *tod, struct tcpcb *tp)
357103026Ssobomax{
358103026Ssobomax	struct adapter *sc = tod->tod_softc;
359103026Ssobomax	struct inpcb *inp = tp->t_inpcb;
360103026Ssobomax	struct socket *so = inp->inp_socket;
361103026Ssobomax	struct sockbuf *sb = &so->so_rcv;
362128580Sandre	struct toepcb *toep = tp->t_toe;
363103026Ssobomax	int credits;
364103026Ssobomax
365103026Ssobomax	INP_WLOCK_ASSERT(inp);
366103026Ssobomax
367103026Ssobomax	SOCKBUF_LOCK(sb);
368103026Ssobomax	KASSERT(toep->sb_cc >= sbused(sb),
369103026Ssobomax	    ("%s: sb %p has more data (%d) than last time (%d).",
370103026Ssobomax	    __func__, sb, sbused(sb), toep->sb_cc));
371125226Ssobomax	if (toep->ulp_mode == ULP_MODE_ISCSI) {
372103026Ssobomax		toep->rx_credits += toep->sb_cc;
373103026Ssobomax		toep->sb_cc = 0;
374103026Ssobomax	} else {
375103026Ssobomax		toep->rx_credits += toep->sb_cc - sbused(sb);
376103026Ssobomax		toep->sb_cc = sbused(sb);
377103026Ssobomax	}
378103026Ssobomax	credits = toep->rx_credits;
379103026Ssobomax	SOCKBUF_UNLOCK(sb);
380103032Ssobomax
381103026Ssobomax	if (credits > 0 &&
382103026Ssobomax	    (credits + 16384 >= tp->rcv_wnd || credits >= 15 * 1024)) {
383125226Ssobomax
384103026Ssobomax		credits = send_rx_credits(sc, toep, credits);
385103026Ssobomax		SOCKBUF_LOCK(sb);
386103026Ssobomax		toep->rx_credits -= credits;
387103026Ssobomax		SOCKBUF_UNLOCK(sb);
388128583Sandre		tp->rcv_wnd += credits;
389128583Sandre		tp->rcv_adv += credits;
390128583Sandre	}
391128583Sandre}
392128583Sandre
393128580Sandre/*
394123992Ssobomax * Close a connection by sending a CPL_CLOSE_CON_REQ message.
395103026Ssobomax */
396103026Ssobomaxstatic int
397103026Ssobomaxclose_conn(struct adapter *sc, struct toepcb *toep)
398103026Ssobomax{
399103026Ssobomax	struct wrqe *wr;
400103026Ssobomax	struct cpl_close_con_req *req;
401103026Ssobomax	unsigned int tid = toep->tid;
402103032Ssobomax
403103026Ssobomax	CTR3(KTR_CXGBE, "%s: tid %u%s", __func__, toep->tid,
404103026Ssobomax	    toep->flags & TPF_FIN_SENT ? ", IGNORED" : "");
405103026Ssobomax
406103026Ssobomax	if (toep->flags & TPF_FIN_SENT)
407103026Ssobomax		return (0);
408103026Ssobomax
409103026Ssobomax	KASSERT(toep->flags & TPF_FLOWC_WR_SENT,
410103026Ssobomax	    ("%s: flowc_wr not sent for tid %u.", __func__, tid));
411103026Ssobomax
412103026Ssobomax	wr = alloc_wrqe(sizeof(*req), toep->ofld_txq);
413103026Ssobomax	if (wr == NULL) {
414103026Ssobomax		/* XXX */
415103026Ssobomax		panic("%s: allocation failure.", __func__);
416103026Ssobomax	}
417103026Ssobomax	req = wrtod(wr);
418103026Ssobomax
419103026Ssobomax        req->wr.wr_hi = htonl(V_FW_WR_OP(FW_TP_WR) |
420103026Ssobomax	    V_FW_WR_IMMDLEN(sizeof(*req) - sizeof(req->wr)));
421103026Ssobomax	req->wr.wr_mid = htonl(V_FW_WR_LEN16(howmany(sizeof(*req), 16)) |
422125020Ssobomax	    V_FW_WR_FLOWID(tid));
423103026Ssobomax        req->wr.wr_lo = cpu_to_be64(0);
424103026Ssobomax        OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_CON_REQ, tid));
425103026Ssobomax	req->rsvd = 0;
426103026Ssobomax
427103026Ssobomax	toep->flags |= TPF_FIN_SENT;
428103026Ssobomax	toep->flags &= ~TPF_SEND_FIN;
429103026Ssobomax	t4_l2t_send(sc, wr, toep->l2te);
430103026Ssobomax
431125024Ssobomax	return (0);
432125024Ssobomax}
433125024Ssobomax
434125024Ssobomax#define MAX_OFLD_TX_CREDITS (SGE_MAX_WR_LEN / 16)
435103026Ssobomax#define MIN_OFLD_TX_CREDITS (howmany(sizeof(struct fw_ofld_tx_data_wr) + 1, 16))
436103026Ssobomax
437103026Ssobomax/* Maximum amount of immediate data we could stuff in a WR */
438103026Ssobomaxstatic inline int
439103026Ssobomaxmax_imm_payload(int tx_credits)
440103026Ssobomax{
441103026Ssobomax	const int n = 2;	/* Use only up to 2 desc for imm. data WR */
442103026Ssobomax
443103026Ssobomax	KASSERT(tx_credits >= 0 &&
444103026Ssobomax		tx_credits <= MAX_OFLD_TX_CREDITS,
445103026Ssobomax		("%s: %d credits", __func__, tx_credits));
446103026Ssobomax
447103026Ssobomax	if (tx_credits < MIN_OFLD_TX_CREDITS)
448103026Ssobomax		return (0);
449103026Ssobomax
450103026Ssobomax	if (tx_credits >= (n * EQ_ESIZE) / 16)
451103026Ssobomax		return ((n * EQ_ESIZE) - sizeof(struct fw_ofld_tx_data_wr));
452103026Ssobomax	else
453103026Ssobomax		return (tx_credits * 16 - sizeof(struct fw_ofld_tx_data_wr));
454103026Ssobomax}
455103026Ssobomax
456103026Ssobomax/* Maximum number of SGL entries we could stuff in a WR */
457103026Ssobomaxstatic inline int
458103026Ssobomaxmax_dsgl_nsegs(int tx_credits)
459103026Ssobomax{
460103026Ssobomax	int nseg = 1;	/* ulptx_sgl has room for 1, rest ulp_tx_sge_pair */
461103026Ssobomax	int sge_pair_credits = tx_credits - MIN_OFLD_TX_CREDITS;
462103026Ssobomax
463103026Ssobomax	KASSERT(tx_credits >= 0 &&
464103026Ssobomax		tx_credits <= MAX_OFLD_TX_CREDITS,
465103026Ssobomax		("%s: %d credits", __func__, tx_credits));
466103026Ssobomax
467103026Ssobomax	if (tx_credits < MIN_OFLD_TX_CREDITS)
468103026Ssobomax		return (0);
469103026Ssobomax
470103026Ssobomax	nseg += 2 * (sge_pair_credits * 16 / 24);
471103026Ssobomax	if ((sge_pair_credits * 16) % 24 == 16)
472103026Ssobomax		nseg++;
473103026Ssobomax
474103026Ssobomax	return (nseg);
475103026Ssobomax}
476103026Ssobomax
477103026Ssobomaxstatic inline void
478103026Ssobomaxwrite_tx_wr(void *dst, struct toepcb *toep, unsigned int immdlen,
479103026Ssobomax    unsigned int plen, uint8_t credits, int shove, int ulp_mode)
480103026Ssobomax{
481103026Ssobomax	struct fw_ofld_tx_data_wr *txwr = dst;
482103026Ssobomax	unsigned int wr_ulp_mode;
483103026Ssobomax
484103026Ssobomax	txwr->op_to_immdlen = htobe32(V_WR_OP(FW_OFLD_TX_DATA_WR) |
485103026Ssobomax	    V_FW_WR_IMMDLEN(immdlen));
486103026Ssobomax	txwr->flowid_len16 = htobe32(V_FW_WR_FLOWID(toep->tid) |
487103026Ssobomax	    V_FW_WR_LEN16(credits));
488103026Ssobomax
489103026Ssobomax	/* for iscsi, the mode & submode setting is per-packet */
490103026Ssobomax	if (toep->ulp_mode == ULP_MODE_ISCSI)
491103026Ssobomax		wr_ulp_mode = V_FW_OFLD_TX_DATA_WR_ULPMODE(ulp_mode >> 4) |
492103026Ssobomax			V_FW_OFLD_TX_DATA_WR_ULPSUBMODE(ulp_mode & 3);
493103026Ssobomax	else
494103026Ssobomax		wr_ulp_mode = V_FW_OFLD_TX_DATA_WR_ULPMODE(toep->ulp_mode);
495103026Ssobomax
496103026Ssobomax	txwr->lsodisable_to_proxy =
497103026Ssobomax	    htobe32(wr_ulp_mode |
498103026Ssobomax		V_FW_OFLD_TX_DATA_WR_URGENT(0) |	/* XXX */
499103026Ssobomax		V_FW_OFLD_TX_DATA_WR_SHOVE(shove));
500103026Ssobomax	txwr->plen = htobe32(plen);
501103026Ssobomax}
502103026Ssobomax
503103026Ssobomax/*
504103026Ssobomax * Generate a DSGL from a starting mbuf.  The total number of segments and the
505103026Ssobomax * maximum segments in any one mbuf are provided.
506103026Ssobomax */
507103026Ssobomaxstatic void
508103026Ssobomaxwrite_tx_sgl(void *dst, struct mbuf *start, struct mbuf *stop, int nsegs, int n)
509103026Ssobomax{
510103026Ssobomax	struct mbuf *m;
511103026Ssobomax	struct ulptx_sgl *usgl = dst;
512103026Ssobomax	int i, j, rc;
513103026Ssobomax	struct sglist sg;
514103026Ssobomax	struct sglist_seg segs[n];
515103026Ssobomax
516103026Ssobomax	KASSERT(nsegs > 0, ("%s: nsegs 0", __func__));
517125020Ssobomax
518103026Ssobomax	sglist_init(&sg, n, segs);
519103026Ssobomax	usgl->cmd_nsge = htobe32(V_ULPTX_CMD(ULP_TX_SC_DSGL) |
520103026Ssobomax	    V_ULPTX_NSGE(nsegs));
521103026Ssobomax
522103026Ssobomax	i = -1;
523103026Ssobomax	for (m = start; m != stop; m = m->m_next) {
524103026Ssobomax		rc = sglist_append(&sg, mtod(m, void *), m->m_len);
525103026Ssobomax		if (__predict_false(rc != 0))
526103026Ssobomax			panic("%s: sglist_append %d", __func__, rc);
527103026Ssobomax
528103026Ssobomax		for (j = 0; j < sg.sg_nseg; i++, j++) {
529103026Ssobomax			if (i < 0) {
530103026Ssobomax				usgl->len0 = htobe32(segs[j].ss_len);
531103026Ssobomax				usgl->addr0 = htobe64(segs[j].ss_paddr);
532103026Ssobomax			} else {
533103026Ssobomax				usgl->sge[i / 2].len[i & 1] =
534103026Ssobomax				    htobe32(segs[j].ss_len);
535103026Ssobomax				usgl->sge[i / 2].addr[i & 1] =
536103026Ssobomax				    htobe64(segs[j].ss_paddr);
537103026Ssobomax			}
538103026Ssobomax#ifdef INVARIANTS
539103026Ssobomax			nsegs--;
540103026Ssobomax#endif
541103026Ssobomax		}
542103026Ssobomax		sglist_reset(&sg);
543103026Ssobomax	}
544103026Ssobomax	if (i & 1)
545103026Ssobomax		usgl->sge[i / 2].len[1] = htobe32(0);
546103026Ssobomax	KASSERT(nsegs == 0, ("%s: nsegs %d, start %p, stop %p",
547103026Ssobomax	    __func__, nsegs, start, stop));
548103026Ssobomax}
549103026Ssobomax
550103026Ssobomax/*
551103026Ssobomax * Max number of SGL entries an offload tx work request can have.  This is 41
552103026Ssobomax * (1 + 40) for a full 512B work request.
553103026Ssobomax * fw_ofld_tx_data_wr(16B) + ulptx_sgl(16B, 1) + ulptx_sge_pair(480B, 40)
554103026Ssobomax */
555103026Ssobomax#define OFLD_SGL_LEN (41)
556103026Ssobomax
557103026Ssobomax/*
558103026Ssobomax * Send data and/or a FIN to the peer.
559103026Ssobomax *
560103026Ssobomax * The socket's so_snd buffer consists of a stream of data starting with sb_mb
561103026Ssobomax * and linked together with m_next.  sb_sndptr, if set, is the last mbuf that
562103026Ssobomax * was transmitted.
563103026Ssobomax *
564103026Ssobomax * drop indicates the number of bytes that should be dropped from the head of
565103026Ssobomax * the send buffer.  It is an optimization that lets do_fw4_ack avoid creating
566103026Ssobomax * contention on the send buffer lock (before this change it used to do
567103026Ssobomax * sowwakeup and then t4_push_frames right after that when recovering from tx
568103026Ssobomax * stalls).  When drop is set this function MUST drop the bytes and wake up any
569103026Ssobomax * writers.
570103026Ssobomax */
571103026Ssobomaxvoid
572103026Ssobomaxt4_push_frames(struct adapter *sc, struct toepcb *toep, int drop)
573103026Ssobomax{
574103026Ssobomax	struct mbuf *sndptr, *m, *sb_sndptr;
575103026Ssobomax	struct fw_ofld_tx_data_wr *txwr;
576103026Ssobomax	struct wrqe *wr;
577103026Ssobomax	u_int plen, nsegs, credits, max_imm, max_nsegs, max_nsegs_1mbuf;
578103026Ssobomax	struct inpcb *inp = toep->inp;
579103026Ssobomax	struct tcpcb *tp = intotcpcb(inp);
580103026Ssobomax	struct socket *so = inp->inp_socket;
581103026Ssobomax	struct sockbuf *sb = &so->so_snd;
582103026Ssobomax	int tx_credits, shove, compl, space, sowwakeup;
583103026Ssobomax	struct ofld_tx_sdesc *txsd = &toep->txsd[toep->txsd_pidx];
584103026Ssobomax
585103026Ssobomax	INP_WLOCK_ASSERT(inp);
586103026Ssobomax	KASSERT(toep->flags & TPF_FLOWC_WR_SENT,
587103026Ssobomax	    ("%s: flowc_wr not sent for tid %u.", __func__, toep->tid));
588103026Ssobomax
589103026Ssobomax	KASSERT(toep->ulp_mode == ULP_MODE_NONE ||
590103026Ssobomax	    toep->ulp_mode == ULP_MODE_TCPDDP ||
591103026Ssobomax	    toep->ulp_mode == ULP_MODE_RDMA,
592103026Ssobomax	    ("%s: ulp_mode %u for toep %p", __func__, toep->ulp_mode, toep));
593103026Ssobomax
594103026Ssobomax	/*
595103026Ssobomax	 * This function doesn't resume by itself.  Someone else must clear the
596103026Ssobomax	 * flag and call this function.
597103026Ssobomax	 */
598103026Ssobomax	if (__predict_false(toep->flags & TPF_TX_SUSPENDED)) {
599103026Ssobomax		KASSERT(drop == 0,
600103026Ssobomax		    ("%s: drop (%d) != 0 but tx is suspended", __func__, drop));
601103026Ssobomax		return;
602103026Ssobomax	}
603103026Ssobomax
604103026Ssobomax	do {
605103026Ssobomax		tx_credits = min(toep->tx_credits, MAX_OFLD_TX_CREDITS);
606122699Sbms		max_imm = max_imm_payload(tx_credits);
607122699Sbms		max_nsegs = max_dsgl_nsegs(tx_credits);
608122699Sbms
609103026Ssobomax		SOCKBUF_LOCK(sb);
610103026Ssobomax		sowwakeup = drop;
611103026Ssobomax		if (drop) {
612103026Ssobomax			sbdrop_locked(sb, drop);
613103026Ssobomax			drop = 0;
614103026Ssobomax		}
615103026Ssobomax		sb_sndptr = sb->sb_sndptr;
616103026Ssobomax		sndptr = sb_sndptr ? sb_sndptr->m_next : sb->sb_mb;
617103026Ssobomax		plen = 0;
618103026Ssobomax		nsegs = 0;
619103026Ssobomax		max_nsegs_1mbuf = 0; /* max # of SGL segments in any one mbuf */
620122699Sbms		for (m = sndptr; m != NULL; m = m->m_next) {
621122699Sbms			int n = sglist_count(mtod(m, void *), m->m_len);
622122699Sbms
623103026Ssobomax			nsegs += n;
624103026Ssobomax			plen += m->m_len;
625103026Ssobomax
626103026Ssobomax			/* This mbuf sent us _over_ the nsegs limit, back out */
627103026Ssobomax			if (plen > max_imm && nsegs > max_nsegs) {
628103026Ssobomax				nsegs -= n;
629103026Ssobomax				plen -= m->m_len;
630103026Ssobomax				if (plen == 0) {
631103026Ssobomax					/* Too few credits */
632103026Ssobomax					toep->flags |= TPF_TX_SUSPENDED;
633103026Ssobomax					if (sowwakeup)
634103026Ssobomax						sowwakeup_locked(so);
635103026Ssobomax					else
636103026Ssobomax						SOCKBUF_UNLOCK(sb);
637103026Ssobomax					SOCKBUF_UNLOCK_ASSERT(sb);
638103026Ssobomax					return;
639103026Ssobomax				}
640103026Ssobomax				break;
641103026Ssobomax			}
642103026Ssobomax
643103026Ssobomax			if (max_nsegs_1mbuf < n)
644103026Ssobomax				max_nsegs_1mbuf = n;
645103026Ssobomax			sb_sndptr = m;	/* new sb->sb_sndptr if all goes well */
646103026Ssobomax
647123992Ssobomax			/* This mbuf put us right at the max_nsegs limit */
648103026Ssobomax			if (plen > max_imm && nsegs == max_nsegs) {
649103026Ssobomax				m = m->m_next;
650103026Ssobomax				break;
651103026Ssobomax			}
652103026Ssobomax		}
653103032Ssobomax
654103026Ssobomax		shove = m == NULL && !(tp->t_flags & TF_MORETOCOME);
655103026Ssobomax		space = sbspace(sb);
656103026Ssobomax
657103026Ssobomax		if (space <= sb->sb_hiwat * 3 / 8 &&
658103026Ssobomax		    toep->plen_nocompl + plen >= sb->sb_hiwat / 4)
659103026Ssobomax			compl = 1;
660103026Ssobomax		else
661103026Ssobomax			compl = 0;
662103026Ssobomax
663103026Ssobomax		if (sb->sb_flags & SB_AUTOSIZE &&
664103026Ssobomax		    V_tcp_do_autosndbuf &&
665103026Ssobomax		    sb->sb_hiwat < V_tcp_autosndbuf_max &&
666103026Ssobomax		    space < sb->sb_hiwat / 8) {
667103026Ssobomax			int newsize = min(sb->sb_hiwat + V_tcp_autosndbuf_inc,
668103026Ssobomax			    V_tcp_autosndbuf_max);
669103026Ssobomax
670103026Ssobomax			if (!sbreserve_locked(sb, newsize, so, NULL))
671103026Ssobomax				sb->sb_flags &= ~SB_AUTOSIZE;
672103026Ssobomax			else
673103026Ssobomax				sowwakeup = 1;	/* room available */
674103026Ssobomax		}
675103026Ssobomax		if (sowwakeup)
676103026Ssobomax			sowwakeup_locked(so);
677103026Ssobomax		else
678103026Ssobomax			SOCKBUF_UNLOCK(sb);
679103026Ssobomax		SOCKBUF_UNLOCK_ASSERT(sb);
680103026Ssobomax
681103026Ssobomax		/* nothing to send */
682103026Ssobomax		if (plen == 0) {
683123992Ssobomax			KASSERT(m == NULL,
684103026Ssobomax			    ("%s: nothing to send, but m != NULL", __func__));
685103026Ssobomax			break;
686103026Ssobomax		}
687103026Ssobomax
688103026Ssobomax		if (__predict_false(toep->flags & TPF_FIN_SENT))
689103026Ssobomax			panic("%s: excess tx.", __func__);
690103026Ssobomax
691103026Ssobomax		if (plen <= max_imm) {
692103026Ssobomax
693103026Ssobomax			/* Immediate data tx */
694103026Ssobomax
695103026Ssobomax			wr = alloc_wrqe(roundup2(sizeof(*txwr) + plen, 16),
696103026Ssobomax					toep->ofld_txq);
697103026Ssobomax			if (wr == NULL) {
698103026Ssobomax				/* XXX: how will we recover from this? */
699103026Ssobomax				toep->flags |= TPF_TX_SUSPENDED;
700103026Ssobomax				return;
701103026Ssobomax			}
702103026Ssobomax			txwr = wrtod(wr);
703103026Ssobomax			credits = howmany(wr->wr_len, 16);
704103026Ssobomax			write_tx_wr(txwr, toep, plen, plen, credits, shove, 0);
705103026Ssobomax			m_copydata(sndptr, 0, plen, (void *)(txwr + 1));
706103026Ssobomax			nsegs = 0;
707103026Ssobomax		} else {
708103026Ssobomax			int wr_len;
709103026Ssobomax
710103026Ssobomax			/* DSGL tx */
711103026Ssobomax
712103026Ssobomax			wr_len = sizeof(*txwr) + sizeof(struct ulptx_sgl) +
713103026Ssobomax			    ((3 * (nsegs - 1)) / 2 + ((nsegs - 1) & 1)) * 8;
714103026Ssobomax			wr = alloc_wrqe(roundup2(wr_len, 16), toep->ofld_txq);
715103026Ssobomax			if (wr == NULL) {
716103026Ssobomax				/* XXX: how will we recover from this? */
717103026Ssobomax				toep->flags |= TPF_TX_SUSPENDED;
718103026Ssobomax				return;
719103026Ssobomax			}
720103026Ssobomax			txwr = wrtod(wr);
721103026Ssobomax			credits = howmany(wr_len, 16);
722103026Ssobomax			write_tx_wr(txwr, toep, 0, plen, credits, shove, 0);
723123992Ssobomax			write_tx_sgl(txwr + 1, sndptr, m, nsegs,
724123992Ssobomax			    max_nsegs_1mbuf);
725103026Ssobomax			if (wr_len & 0xf) {
726123992Ssobomax				uint64_t *pad = (uint64_t *)
727103026Ssobomax				    ((uintptr_t)txwr + wr_len);
728103026Ssobomax				*pad = 0;
729103026Ssobomax			}
730103026Ssobomax		}
731103026Ssobomax
732103026Ssobomax		KASSERT(toep->tx_credits >= credits,
733103026Ssobomax			("%s: not enough credits", __func__));
734103026Ssobomax
735103026Ssobomax		toep->tx_credits -= credits;
736103026Ssobomax		toep->tx_nocompl += credits;
737103026Ssobomax		toep->plen_nocompl += plen;
738103026Ssobomax		if (toep->tx_credits <= toep->tx_total * 3 / 8 &&
739103026Ssobomax		    toep->tx_nocompl >= toep->tx_total / 4)
740103026Ssobomax			compl = 1;
741103026Ssobomax
742103026Ssobomax		if (compl || toep->ulp_mode == ULP_MODE_RDMA) {
743103026Ssobomax			txwr->op_to_immdlen |= htobe32(F_FW_WR_COMPL);
744103026Ssobomax			toep->tx_nocompl = 0;
745103026Ssobomax			toep->plen_nocompl = 0;
746103026Ssobomax		}
747103026Ssobomax
748103026Ssobomax		tp->snd_nxt += plen;
749103026Ssobomax		tp->snd_max += plen;
750103026Ssobomax
751127307Srwatson		SOCKBUF_LOCK(sb);
752103026Ssobomax		KASSERT(sb_sndptr, ("%s: sb_sndptr is NULL", __func__));
753103026Ssobomax		sb->sb_sndptr = sb_sndptr;
754103026Ssobomax		SOCKBUF_UNLOCK(sb);
755103026Ssobomax
756103026Ssobomax		toep->flags |= TPF_TX_DATA_SENT;
757103026Ssobomax		if (toep->tx_credits < MIN_OFLD_TX_CREDITS)
758103026Ssobomax			toep->flags |= TPF_TX_SUSPENDED;
759103026Ssobomax
760127307Srwatson		KASSERT(toep->txsd_avail > 0, ("%s: no txsd", __func__));
761127307Srwatson		txsd->plen = plen;
762127307Srwatson		txsd->tx_credits = credits;
763127307Srwatson		txsd++;
764127307Srwatson		if (__predict_false(++toep->txsd_pidx == toep->txsd_total)) {
765127307Srwatson			toep->txsd_pidx = 0;
766127307Srwatson			txsd = &toep->txsd[0];
767127307Srwatson		}
768127307Srwatson		toep->txsd_avail--;
769103026Ssobomax
770103026Ssobomax		t4_l2t_send(sc, wr, toep->l2te);
771103026Ssobomax	} while (m != NULL);
772103026Ssobomax
773103026Ssobomax	/* Send a FIN if requested, but only if there's no more data to send */
774103026Ssobomax	if (m == NULL && toep->flags & TPF_SEND_FIN)
775103026Ssobomax		close_conn(sc, toep);
776103026Ssobomax}
777103026Ssobomax
778103026Ssobomax/* Send ULP data over TOE using TX_DATA_WR. We send whole mbuf at once */
779103026Ssobomaxvoid
780103026Ssobomaxt4_ulp_push_frames(struct adapter *sc, struct toepcb *toep, int drop)
781103026Ssobomax{
782	struct mbuf *sndptr, *m = NULL;
783	struct fw_ofld_tx_data_wr *txwr;
784	struct wrqe *wr;
785	unsigned int plen, nsegs, credits, max_imm, max_nsegs, max_nsegs_1mbuf;
786	struct inpcb *inp = toep->inp;
787	struct tcpcb *tp;
788	struct socket *so;
789	struct sockbuf *sb;
790	int tx_credits, ulp_len = 0, ulp_mode = 0, qlen = 0;
791	int shove, compl;
792	struct ofld_tx_sdesc *txsd;
793
794	INP_WLOCK_ASSERT(inp);
795	if (toep->flags & TPF_ABORT_SHUTDOWN)
796		return;
797
798	tp = intotcpcb(inp);
799	so = inp->inp_socket;
800	sb = &so->so_snd;
801	txsd = &toep->txsd[toep->txsd_pidx];
802
803	KASSERT(toep->flags & TPF_FLOWC_WR_SENT,
804	    ("%s: flowc_wr not sent for tid %u.", __func__, toep->tid));
805
806	/*
807	 * This function doesn't resume by itself.  Someone else must clear the
808	 * flag and call this function.
809	 */
810	if (__predict_false(toep->flags & TPF_TX_SUSPENDED))
811		return;
812
813	sndptr = t4_queue_iscsi_callback(so, toep, 1, &qlen);
814	if (!qlen)
815		return;
816
817	do {
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);
821
822		if (drop) {
823			t4_cpl_iscsi_callback(toep->td, toep, &drop,
824			    CPL_FW4_ACK);
825			drop = 0;
826		}
827
828		plen = 0;
829		nsegs = 0;
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);
833
834			nsegs += n;
835			plen += m->m_len;
836
837			/* This mbuf sent us _over_ the nsegs limit, return */
838			if (plen > max_imm && nsegs > max_nsegs) {
839				toep->flags |= TPF_TX_SUSPENDED;
840				return;
841			}
842
843			if (max_nsegs_1mbuf < n)
844				max_nsegs_1mbuf = n;
845
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;
849				return;
850			}
851		}
852
853		shove = m == NULL && !(tp->t_flags & TF_MORETOCOME);
854		/* nothing to send */
855		if (plen == 0) {
856			KASSERT(m == NULL,
857			    ("%s: nothing to send, but m != NULL", __func__));
858			break;
859		}
860
861		if (__predict_false(toep->flags & TPF_FIN_SENT))
862			panic("%s: excess tx.", __func__);
863
864		ulp_len = plen + ulp_extra_len(sndptr, &ulp_mode);
865		if (plen <= max_imm) {
866
867			/* Immediate data tx */
868			wr = alloc_wrqe(roundup(sizeof(*txwr) + plen, 16),
869					toep->ofld_txq);
870			if (wr == NULL) {
871				/* XXX: how will we recover from this? */
872				toep->flags |= TPF_TX_SUSPENDED;
873				return;
874			}
875			txwr = wrtod(wr);
876			credits = howmany(wr->wr_len, 16);
877			write_tx_wr(txwr, toep, plen, ulp_len, credits, shove,
878								ulp_mode);
879			m_copydata(sndptr, 0, plen, (void *)(txwr + 1));
880		} else {
881			int wr_len;
882
883			/* DSGL tx */
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);
887			if (wr == NULL) {
888				/* XXX: how will we recover from this? */
889				toep->flags |= TPF_TX_SUSPENDED;
890				return;
891			}
892			txwr = wrtod(wr);
893			credits = howmany(wr_len, 16);
894			write_tx_wr(txwr, toep, 0, ulp_len, credits, shove,
895								ulp_mode);
896			write_tx_sgl(txwr + 1, sndptr, m, nsegs,
897			    max_nsegs_1mbuf);
898			if (wr_len & 0xf) {
899				uint64_t *pad = (uint64_t *)
900				    ((uintptr_t)txwr + wr_len);
901				*pad = 0;
902			}
903		}
904
905		KASSERT(toep->tx_credits >= credits,
906			("%s: not enough credits", __func__));
907
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)
913			compl = 1;
914
915		if (compl) {
916			txwr->op_to_immdlen |= htobe32(F_FW_WR_COMPL);
917			toep->tx_nocompl = 0;
918			toep->plen_nocompl = 0;
919		}
920		tp->snd_nxt += ulp_len;
921		tp->snd_max += ulp_len;
922
923                /* goto next mbuf */
924		sndptr = m = t4_queue_iscsi_callback(so, toep, 2, &qlen);
925
926		toep->flags |= TPF_TX_DATA_SENT;
927		if (toep->tx_credits < MIN_OFLD_TX_CREDITS) {
928			toep->flags |= TPF_TX_SUSPENDED;
929		}
930
931		KASSERT(toep->txsd_avail > 0, ("%s: no txsd", __func__));
932		txsd->plen = plen;
933		txsd->tx_credits = credits;
934		txsd++;
935		if (__predict_false(++toep->txsd_pidx == toep->txsd_total)) {
936			toep->txsd_pidx = 0;
937			txsd = &toep->txsd[0];
938		}
939		toep->txsd_avail--;
940
941		t4_l2t_send(sc, wr, toep->l2te);
942	} while (m != NULL);
943
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);
947}
948
949int
950t4_tod_output(struct toedev *tod, struct tcpcb *tp)
951{
952	struct adapter *sc = tod->tod_softc;
953#ifdef INVARIANTS
954	struct inpcb *inp = tp->t_inpcb;
955#endif
956	struct toepcb *toep = tp->t_toe;
957
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__));
962
963	t4_push_frames(sc, toep, 0);
964
965	return (0);
966}
967
968int
969t4_send_fin(struct toedev *tod, struct tcpcb *tp)
970{
971	struct adapter *sc = tod->tod_softc;
972#ifdef INVARIANTS
973	struct inpcb *inp = tp->t_inpcb;
974#endif
975	struct toepcb *toep = tp->t_toe;
976
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__));
981
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);
986		else
987			t4_push_frames(sc, toep, 0);
988	}
989
990	return (0);
991}
992
993int
994t4_send_rst(struct toedev *tod, struct tcpcb *tp)
995{
996	struct adapter *sc = tod->tod_softc;
997#if defined(INVARIANTS)
998	struct inpcb *inp = tp->t_inpcb;
999#endif
1000	struct toepcb *toep = tp->t_toe;
1001
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__));
1006
1007	/* hmmmm */
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]));
1011
1012	send_reset(sc, toep, 0);
1013	return (0);
1014}
1015
1016/*
1017 * Peer has sent us a FIN.
1018 */
1019static int
1020do_peer_close(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
1021{
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;
1028	struct socket *so;
1029	struct sockbuf *sb;
1030#ifdef INVARIANTS
1031	unsigned int opcode = G_CPL_OPCODE(be32toh(OPCODE_TID(cpl)));
1032#endif
1033
1034	KASSERT(opcode == CPL_PEER_CLOSE,
1035	    ("%s: unexpected opcode 0x%x", __func__, opcode));
1036	KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
1037
1038	if (__predict_false(toep->flags & TPF_SYNQE)) {
1039#ifdef INVARIANTS
1040		struct synq_entry *synqe = (void *)toep;
1041
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.",
1046			    __func__, tid));
1047		} else {
1048			/*
1049			 * do_pass_accept_req is still running and will
1050			 * eventually take care of this tid.
1051			 */
1052		}
1053		INP_WUNLOCK(synqe->lctx->inp);
1054#endif
1055		CTR4(KTR_CXGBE, "%s: tid %u, synqe %p (0x%x)", __func__, tid,
1056		    toep, toep->flags);
1057		return (0);
1058	}
1059
1060	KASSERT(toep->tid == tid, ("%s: toep tid mismatch", __func__));
1061
1062	INP_INFO_WLOCK(&V_tcbinfo);
1063	INP_WLOCK(inp);
1064	tp = intotcpcb(inp);
1065
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);
1068
1069	if (toep->flags & TPF_ABORT_SHUTDOWN)
1070		goto done;
1071
1072	tp->rcv_nxt++;	/* FIN */
1073
1074	so = inp->inp_socket;
1075	sb = &so->so_rcv;
1076	SOCKBUF_LOCK(sb);
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);
1081
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 */
1088#endif
1089		sbappendstream_locked(sb, m);
1090		toep->sb_cc = sbused(sb);
1091	}
1092	socantrcvmore_locked(so);	/* unlocks the sockbuf */
1093
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)));
1098	}
1099
1100	switch (tp->t_state) {
1101	case TCPS_SYN_RECEIVED:
1102		tp->t_starttime = ticks;
1103		/* FALLTHROUGH */
1104
1105	case TCPS_ESTABLISHED:
1106		tp->t_state = TCPS_CLOSE_WAIT;
1107		break;
1108
1109	case TCPS_FIN_WAIT_1:
1110		tp->t_state = TCPS_CLOSING;
1111		break;
1112
1113	case TCPS_FIN_WAIT_2:
1114		tcp_twstart(tp);
1115		INP_UNLOCK_ASSERT(inp);	 /* safe, we have a ref on the inp */
1116		INP_INFO_WUNLOCK(&V_tcbinfo);
1117
1118		INP_WLOCK(inp);
1119		final_cpl_received(toep);
1120		return (0);
1121
1122	default:
1123		log(LOG_ERR, "%s: TID %u received CPL_PEER_CLOSE in state %d\n",
1124		    __func__, tid, tp->t_state);
1125	}
1126done:
1127	INP_WUNLOCK(inp);
1128	INP_INFO_WUNLOCK(&V_tcbinfo);
1129	return (0);
1130}
1131
1132/*
1133 * Peer has ACK'd our FIN.
1134 */
1135static int
1136do_close_con_rpl(struct sge_iq *iq, const struct rss_header *rss,
1137    struct mbuf *m)
1138{
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;
1146#ifdef INVARIANTS
1147	unsigned int opcode = G_CPL_OPCODE(be32toh(OPCODE_TID(cpl)));
1148#endif
1149
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__));
1154
1155	INP_INFO_WLOCK(&V_tcbinfo);
1156	INP_WLOCK(inp);
1157	tp = intotcpcb(inp);
1158
1159	CTR4(KTR_CXGBE, "%s: tid %u (%s), toep_flags 0x%x",
1160	    __func__, tid, tp ? tcpstates[tp->t_state] : "no tp", toep->flags);
1161
1162	if (toep->flags & TPF_ABORT_SHUTDOWN)
1163		goto done;
1164
1165	so = inp->inp_socket;
1166	tp->snd_una = be32toh(cpl->snd_nxt) - 1;	/* exclude FIN */
1167
1168	switch (tp->t_state) {
1169	case TCPS_CLOSING:	/* see TCPS_FIN_WAIT_2 in do_peer_close too */
1170		tcp_twstart(tp);
1171release:
1172		INP_UNLOCK_ASSERT(inp);	/* safe, we have a ref on the  inp */
1173		INP_INFO_WUNLOCK(&V_tcbinfo);
1174
1175		INP_WLOCK(inp);
1176		final_cpl_received(toep);	/* no more CPLs expected */
1177
1178		return (0);
1179	case TCPS_LAST_ACK:
1180		if (tcp_close(tp))
1181			INP_WUNLOCK(inp);
1182		goto release;
1183
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;
1188		break;
1189
1190	default:
1191		log(LOG_ERR,
1192		    "%s: TID %u received CPL_CLOSE_CON_RPL in state %s\n",
1193		    __func__, tid, tcpstates[tp->t_state]);
1194	}
1195done:
1196	INP_WUNLOCK(inp);
1197	INP_INFO_WUNLOCK(&V_tcbinfo);
1198	return (0);
1199}
1200
1201void
1202send_abort_rpl(struct adapter *sc, struct sge_wrq *ofld_txq, int tid,
1203    int rst_status)
1204{
1205	struct wrqe *wr;
1206	struct cpl_abort_rpl *cpl;
1207
1208	wr = alloc_wrqe(sizeof(*cpl), ofld_txq);
1209	if (wr == NULL) {
1210		/* XXX */
1211		panic("%s: allocation failure.", __func__);
1212	}
1213	cpl = wrtod(wr);
1214
1215	INIT_TP_WR_MIT_CPL(cpl, CPL_ABORT_RPL, tid);
1216	cpl->cmd = rst_status;
1217
1218	t4_wrq_tx(sc, wr);
1219}
1220
1221static int
1222abort_status_to_errno(struct tcpcb *tp, unsigned int abort_reason)
1223{
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:
1232		return (ETIMEDOUT);
1233	default:
1234		return (EIO);
1235	}
1236}
1237
1238int
1239cpl_not_handled(struct sge_iq *, const struct rss_header *, struct mbuf *);
1240/*
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:
1245 * - CPL_SET_TCB_RPL
1246 * - CPL_RX_DATA_DDP
1247 */
1248void (*tom_cpl_iscsi_callback)(struct tom_data *, struct socket *, void *,
1249    unsigned int);
1250
1251struct mbuf *(*tom_queue_iscsi_callback)(struct socket *, unsigned int, int *);
1252/*
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.
1255 */
1256int
1257t4tom_cpl_handler_registered(struct adapter *sc, unsigned int opcode)
1258{
1259
1260	MPASS(opcode < nitems(sc->cpl_handler));
1261
1262	return (sc->cpl_handler[opcode] &&
1263	    sc->cpl_handler[opcode] != cpl_not_handled);
1264}
1265
1266/*
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.
1269 */
1270void
1271t4tom_register_cpl_iscsi_callback(void (*fp)(struct tom_data *, struct socket *,
1272    void *, unsigned int))
1273{
1274
1275	tom_cpl_iscsi_callback = fp;
1276}
1277
1278void
1279t4tom_register_queue_iscsi_callback(struct mbuf *(*fp)(struct socket *,
1280    unsigned int, int *qlen))
1281{
1282
1283	tom_queue_iscsi_callback = fp;
1284}
1285
1286int
1287t4_cpl_iscsi_callback(struct tom_data *td, struct toepcb *toep, void *m,
1288    unsigned int opcode)
1289{
1290	struct socket *so;
1291
1292	if (opcode == CPL_FW4_ACK)
1293		so = toep->inp->inp_socket;
1294	else {
1295		INP_WLOCK(toep->inp);
1296		so = toep->inp->inp_socket;
1297		INP_WUNLOCK(toep->inp);
1298	}
1299
1300	if (tom_cpl_iscsi_callback && so) {
1301		if (toep->ulp_mode == ULP_MODE_ISCSI) {
1302			tom_cpl_iscsi_callback(td, so, m, opcode);
1303			return (0);
1304		}
1305	}
1306
1307	return (1);
1308}
1309
1310struct mbuf *
1311t4_queue_iscsi_callback(struct socket *so, struct toepcb *toep,
1312    unsigned int cmd, int *qlen)
1313{
1314
1315	if (tom_queue_iscsi_callback && so) {
1316		if (toep->ulp_mode == ULP_MODE_ISCSI)
1317			return (tom_queue_iscsi_callback(so, cmd, qlen));
1318	}
1319
1320	return (NULL);
1321}
1322
1323/*
1324 * TCP RST from the peer, timeout, or some other such critical error.
1325 */
1326static int
1327do_abort_req(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
1328{
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;
1334	struct inpcb *inp;
1335	struct tcpcb *tp;
1336#ifdef INVARIANTS
1337	unsigned int opcode = G_CPL_OPCODE(be32toh(OPCODE_TID(cpl)));
1338#endif
1339
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__));
1343
1344	if (toep->flags & TPF_SYNQE)
1345		return (do_abort_req_synqe(iq, rss, m));
1346
1347	KASSERT(toep->tid == tid, ("%s: toep tid mismatch", __func__));
1348
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 */
1353	}
1354
1355	inp = toep->inp;
1356	INP_INFO_WLOCK(&V_tcbinfo);	/* for tcp_close */
1357	INP_WLOCK(inp);
1358
1359	tp = intotcpcb(inp);
1360
1361	CTR6(KTR_CXGBE,
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);
1365
1366	/*
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.
1370	 */
1371	if (toep->flags & TPF_ABORT_SHUTDOWN) {
1372		INP_WUNLOCK(inp);
1373		goto done;
1374	}
1375	toep->flags |= TPF_ABORT_SHUTDOWN;
1376
1377	if ((inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT)) == 0) {
1378		struct socket *so = inp->inp_socket;
1379
1380		if (so != NULL)
1381			so_error_set(so, abort_status_to_errno(tp,
1382			    cpl->status));
1383		tp = tcp_close(tp);
1384		if (tp == NULL)
1385			INP_WLOCK(inp);	/* re-acquire */
1386	}
1387
1388	final_cpl_received(toep);
1389done:
1390	INP_INFO_WUNLOCK(&V_tcbinfo);
1391	send_abort_rpl(sc, ofld_txq, tid, CPL_ABORT_NO_RST);
1392	return (0);
1393}
1394
1395/*
1396 * Reply to the CPL_ABORT_REQ (send_reset)
1397 */
1398static int
1399do_abort_rpl(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
1400{
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;
1406#ifdef INVARIANTS
1407	unsigned int opcode = G_CPL_OPCODE(be32toh(OPCODE_TID(cpl)));
1408#endif
1409
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__));
1413
1414	if (toep->flags & TPF_SYNQE)
1415		return (do_abort_rpl_synqe(iq, rss, m));
1416
1417	KASSERT(toep->tid == tid, ("%s: toep tid mismatch", __func__));
1418
1419	CTR5(KTR_CXGBE, "%s: tid %u, toep %p, inp %p, status %d",
1420	    __func__, tid, toep, inp, cpl->status);
1421
1422	KASSERT(toep->flags & TPF_ABORT_SHUTDOWN,
1423	    ("%s: wasn't expecting abort reply", __func__));
1424
1425	INP_WLOCK(inp);
1426	final_cpl_received(toep);
1427
1428	return (0);
1429}
1430
1431static int
1432do_rx_data(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
1433{
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;
1439	struct tcpcb *tp;
1440	struct socket *so;
1441	struct sockbuf *sb;
1442	int len;
1443	uint32_t ddp_placed = 0;
1444
1445	if (__predict_false(toep->flags & TPF_SYNQE)) {
1446#ifdef INVARIANTS
1447		struct synq_entry *synqe = (void *)toep;
1448
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.",
1453			    __func__, tid));
1454		} else {
1455			/*
1456			 * do_pass_accept_req is still running and will
1457			 * eventually take care of this tid.
1458			 */
1459		}
1460		INP_WUNLOCK(synqe->lctx->inp);
1461#endif
1462		CTR4(KTR_CXGBE, "%s: tid %u, synqe %p (0x%x)", __func__, tid,
1463		    toep, toep->flags);
1464		m_freem(m);
1465		return (0);
1466	}
1467
1468	KASSERT(toep->tid == tid, ("%s: toep tid mismatch", __func__));
1469
1470	/* strip off CPL header */
1471	m_adj(m, sizeof(*cpl));
1472	len = m->m_pkthdr.len;
1473
1474	INP_WLOCK(inp);
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);
1478		INP_WUNLOCK(inp);
1479		m_freem(m);
1480		return (0);
1481	}
1482
1483	tp = intotcpcb(inp);
1484
1485	if (__predict_false(tp->rcv_nxt != be32toh(cpl->seq)))
1486		ddp_placed = be32toh(cpl->seq) - tp->rcv_nxt;
1487
1488	tp->rcv_nxt += len;
1489	KASSERT(tp->rcv_wnd >= len, ("%s: negative window size", __func__));
1490	tp->rcv_wnd -= len;
1491	tp->t_rcvtime = ticks;
1492
1493	so = inp_inpcbtosocket(inp);
1494	sb = &so->so_rcv;
1495	SOCKBUF_LOCK(sb);
1496
1497	if (__predict_false(sb->sb_state & SBS_CANTRCVMORE)) {
1498		CTR3(KTR_CXGBE, "%s: tid %u, excess rx (%d bytes)",
1499		    __func__, tid, len);
1500		m_freem(m);
1501		SOCKBUF_UNLOCK(sb);
1502		INP_WUNLOCK(inp);
1503
1504		INP_INFO_WLOCK(&V_tcbinfo);
1505		INP_WLOCK(inp);
1506		tp = tcp_drop(tp, ECONNRESET);
1507		if (tp)
1508			INP_WUNLOCK(inp);
1509		INP_INFO_WUNLOCK(&V_tcbinfo);
1510
1511		return (0);
1512	}
1513
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);
1522
1523		if (!sbreserve_locked(sb, newsize, so, NULL))
1524			sb->sb_flags &= ~SB_AUTOSIZE;
1525		else
1526			toep->rx_credits += newsize - hiwat;
1527	}
1528
1529	if (toep->ulp_mode == ULP_MODE_TCPDDP) {
1530		int changed = !(toep->ddp_flags & DDP_ON) ^ cpl->ddp_off;
1531
1532		if (changed) {
1533			if (toep->ddp_flags & DDP_SC_REQ)
1534				toep->ddp_flags ^= DDP_ON | DDP_SC_REQ;
1535			else {
1536				KASSERT(cpl->ddp_off == 1,
1537				    ("%s: DDP switched on by itself.",
1538				    __func__));
1539
1540				/* Fell out of DDP mode */
1541				toep->ddp_flags &= ~(DDP_ON | DDP_BUF0_ACTIVE |
1542				    DDP_BUF1_ACTIVE);
1543
1544				if (ddp_placed)
1545					insert_ddp_data(toep, ddp_placed);
1546			}
1547		}
1548
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);
1555		}
1556
1557		if (toep->ddp_flags & DDP_ON) {
1558
1559			/*
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.
1564			 */
1565
1566#if 0
1567			CTR5(KTR_CXGBE,
1568			    "%s: tid %u (0x%x) DDP indicate (seq 0x%x, len %d)",
1569			    __func__, tid, toep->flags, be32toh(cpl->seq), len);
1570#endif
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) {
1574
1575			/*
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.
1579			 */
1580
1581			enable_ddp(sc, toep);
1582		}
1583	}
1584
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);
1590	toep->sb_cc = sbused(sb);
1591	sorwakeup_locked(so);
1592	SOCKBUF_UNLOCK_ASSERT(sb);
1593
1594	INP_WUNLOCK(inp);
1595	return (0);
1596}
1597
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)
1603
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)
1609
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)
1614
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)
1621
1622static int
1623do_fw4_ack(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
1624{
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);
1629	struct inpcb *inp;
1630	struct tcpcb *tp;
1631	struct socket *so;
1632	uint8_t credits = cpl->credits;
1633	struct ofld_tx_sdesc *txsd;
1634	int plen;
1635#ifdef INVARIANTS
1636	unsigned int opcode = G_CPL_FW4_ACK_OPCODE(be32toh(OPCODE_TID(cpl)));
1637#endif
1638
1639	/*
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.
1642	 */
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));
1646		return (0);
1647	}
1648
1649	inp = toep->inp;
1650
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__));
1655
1656	INP_WLOCK(inp);
1657
1658	if (__predict_false(toep->flags & TPF_ABORT_SHUTDOWN)) {
1659		INP_WUNLOCK(inp);
1660		return (0);
1661	}
1662
1663	KASSERT((inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) == 0,
1664	    ("%s: inp_flags 0x%x", __func__, inp->inp_flags));
1665
1666	tp = intotcpcb(inp);
1667
1668	if (cpl->flags & CPL_FW4_ACK_FLAGS_SEQVAL) {
1669		tcp_seq snd_una = be32toh(cpl->snd_una);
1670
1671#ifdef INVARIANTS
1672		if (__predict_false(SEQ_LT(snd_una, tp->snd_una))) {
1673			log(LOG_ERR,
1674			    "%s: unexpected seq# %x for TID %u, snd_una %x\n",
1675			    __func__, snd_una, toep->tid, tp->snd_una);
1676		}
1677#endif
1678
1679		if (tp->snd_una != snd_una) {
1680			tp->snd_una = snd_una;
1681			tp->ts_recent_age = tcp_ts_getticks();
1682		}
1683	}
1684
1685	so = inp->inp_socket;
1686	txsd = &toep->txsd[toep->txsd_cidx];
1687	plen = 0;
1688	while (credits) {
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;
1693		plen += txsd->plen;
1694		txsd++;
1695		toep->txsd_avail++;
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;
1701		}
1702	}
1703
1704	if (toep->tx_credits == toep->tx_total) {
1705		toep->tx_nocompl = 0;
1706		toep->plen_nocompl = 0;
1707	}
1708
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);
1714		else
1715			t4_push_frames(sc, toep, plen);
1716	} else if (plen > 0) {
1717		struct sockbuf *sb = &so->so_snd;
1718
1719		if (toep->ulp_mode == ULP_MODE_ISCSI)
1720			t4_cpl_iscsi_callback(toep->td, toep, &plen,
1721			    CPL_FW4_ACK);
1722		else {
1723			SOCKBUF_LOCK(sb);
1724			sbdrop_locked(sb, plen);
1725			sowwakeup_locked(so);
1726			SOCKBUF_UNLOCK_ASSERT(sb);
1727		}
1728	}
1729
1730	INP_WUNLOCK(inp);
1731
1732	return (0);
1733}
1734
1735static int
1736do_set_tcb_rpl(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
1737{
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);
1741#ifdef INVARIANTS
1742	unsigned int opcode = G_CPL_OPCODE(be32toh(OPCODE_TID(cpl)));
1743#endif
1744
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__));
1748
1749	if (is_ftid(sc, tid))
1750		return (t4_filter_rpl(iq, rss, m)); /* TCB is a filter */
1751	else {
1752		struct toepcb *toep = lookup_tid(sc, tid);
1753
1754		t4_cpl_iscsi_callback(toep->td, toep, m, CPL_SET_TCB_RPL);
1755		return (0);
1756	}
1757
1758	CXGBE_UNIMPLEMENTED(__func__);
1759}
1760
1761void
1762t4_set_tcb_field(struct adapter *sc, struct toepcb *toep, int ctrl,
1763    uint16_t word, uint64_t mask, uint64_t val)
1764{
1765	struct wrqe *wr;
1766	struct cpl_set_tcb_field *req;
1767
1768	wr = alloc_wrqe(sizeof(*req), ctrl ? toep->ctrlq : toep->ofld_txq);
1769	if (wr == NULL) {
1770		/* XXX */
1771		panic("%s: allocation failure.", __func__);
1772	}
1773	req = wrtod(wr);
1774
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);
1781
1782	t4_wrq_tx(sc, wr);
1783}
1784
1785void
1786t4_init_cpl_io_handlers(struct adapter *sc)
1787{
1788
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);
1796}
1797
1798void
1799t4_uninit_cpl_io_handlers(struct adapter *sc)
1800{
1801
1802	t4_register_cpl_handler(sc, CPL_SET_TCB_RPL, t4_filter_rpl);
1803}
1804#endif
1805