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