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