1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
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 #include "opt_inet.h"
32 #include "opt_inet6.h"
33 #include "opt_kern_tls.h"
34 #include "opt_ratelimit.h"
35
36 #ifdef TCP_OFFLOAD
37 #include <sys/param.h>
38 #include <sys/aio.h>
39 #include <sys/file.h>
40 #include <sys/kernel.h>
41 #include <sys/ktr.h>
42 #include <sys/module.h>
43 #include <sys/proc.h>
44 #include <sys/protosw.h>
45 #include <sys/domain.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/sglist.h>
49 #include <sys/taskqueue.h>
50 #include <netinet/in.h>
51 #include <netinet/in_pcb.h>
52 #include <netinet/ip.h>
53 #include <netinet/ip6.h>
54 #define TCPSTATES
55 #include <netinet/tcp_fsm.h>
56 #include <netinet/tcp_seq.h>
57 #include <netinet/tcp_var.h>
58 #include <netinet/toecore.h>
59
60 #include <security/mac/mac_framework.h>
61
62 #include <vm/vm.h>
63 #include <vm/vm_extern.h>
64 #include <vm/pmap.h>
65 #include <vm/vm_map.h>
66 #include <vm/vm_page.h>
67
68 #include <dev/iscsi/iscsi_proto.h>
69 #include <dev/nvmf/nvmf_proto.h>
70
71 #include "common/common.h"
72 #include "common/t4_msg.h"
73 #include "common/t4_regs.h"
74 #include "common/t4_tcb.h"
75 #include "tom/t4_tom_l2t.h"
76 #include "tom/t4_tom.h"
77
78 static void t4_aiotx_cancel(struct kaiocb *job);
79 static void t4_aiotx_queue_toep(struct socket *so, struct toepcb *toep);
80
81 void
send_flowc_wr(struct toepcb * toep,struct tcpcb * tp)82 send_flowc_wr(struct toepcb *toep, struct tcpcb *tp)
83 {
84 struct wrqe *wr;
85 struct fw_flowc_wr *flowc;
86 unsigned int nparams, flowclen, paramidx;
87 struct vi_info *vi = toep->vi;
88 struct port_info *pi = vi->pi;
89 struct adapter *sc = pi->adapter;
90 unsigned int pfvf = sc->pf << S_FW_VIID_PFN;
91 struct ofld_tx_sdesc *txsd = &toep->txsd[toep->txsd_pidx];
92
93 KASSERT(!(toep->flags & TPF_FLOWC_WR_SENT),
94 ("%s: flowc for tid %u sent already", __func__, toep->tid));
95
96 if (tp != NULL)
97 nparams = 8;
98 else
99 nparams = 6;
100 if (toep->params.tc_idx != -1) {
101 MPASS(toep->params.tc_idx >= 0 &&
102 toep->params.tc_idx < sc->params.nsched_cls);
103 nparams++;
104 }
105
106 flowclen = sizeof(*flowc) + nparams * sizeof(struct fw_flowc_mnemval);
107
108 wr = alloc_wrqe(roundup2(flowclen, 16), &toep->ofld_txq->wrq);
109 if (wr == NULL) {
110 /* XXX */
111 panic("%s: allocation failure.", __func__);
112 }
113 flowc = wrtod(wr);
114 memset(flowc, 0, wr->wr_len);
115
116 flowc->op_to_nparams = htobe32(V_FW_WR_OP(FW_FLOWC_WR) |
117 V_FW_FLOWC_WR_NPARAMS(nparams));
118 flowc->flowid_len16 = htonl(V_FW_WR_LEN16(howmany(flowclen, 16)) |
119 V_FW_WR_FLOWID(toep->tid));
120
121 #define FLOWC_PARAM(__m, __v) \
122 do { \
123 flowc->mnemval[paramidx].mnemonic = FW_FLOWC_MNEM_##__m; \
124 flowc->mnemval[paramidx].val = htobe32(__v); \
125 paramidx++; \
126 } while (0)
127
128 paramidx = 0;
129
130 FLOWC_PARAM(PFNVFN, pfvf);
131 /* Firmware expects hw port and will translate to channel itself. */
132 FLOWC_PARAM(CH, pi->hw_port);
133 FLOWC_PARAM(PORT, pi->hw_port);
134 FLOWC_PARAM(IQID, toep->ofld_rxq->iq.abs_id);
135 FLOWC_PARAM(SNDBUF, toep->params.sndbuf);
136 if (tp) {
137 FLOWC_PARAM(MSS, toep->params.emss);
138 FLOWC_PARAM(SNDNXT, tp->snd_nxt);
139 FLOWC_PARAM(RCVNXT, tp->rcv_nxt);
140 } else
141 FLOWC_PARAM(MSS, 512);
142 CTR6(KTR_CXGBE,
143 "%s: tid %u, mss %u, sndbuf %u, snd_nxt 0x%x, rcv_nxt 0x%x",
144 __func__, toep->tid, toep->params.emss, toep->params.sndbuf,
145 tp ? tp->snd_nxt : 0, tp ? tp->rcv_nxt : 0);
146
147 if (toep->params.tc_idx != -1)
148 FLOWC_PARAM(SCHEDCLASS, toep->params.tc_idx);
149 #undef FLOWC_PARAM
150
151 KASSERT(paramidx == nparams, ("nparams mismatch"));
152
153 KASSERT(howmany(flowclen, 16) <= MAX_OFLD_TX_SDESC_CREDITS,
154 ("%s: tx_credits %u too large", __func__, howmany(flowclen, 16)));
155 txsd->tx_credits = howmany(flowclen, 16);
156 txsd->plen = 0;
157 KASSERT(toep->tx_credits >= txsd->tx_credits && toep->txsd_avail > 0,
158 ("%s: not enough credits (%d)", __func__, toep->tx_credits));
159 toep->tx_credits -= txsd->tx_credits;
160 if (__predict_false(++toep->txsd_pidx == toep->txsd_total))
161 toep->txsd_pidx = 0;
162 toep->txsd_avail--;
163
164 toep->flags |= TPF_FLOWC_WR_SENT;
165 t4_wrq_tx(sc, wr);
166 }
167
168 #ifdef RATELIMIT
169 /*
170 * Input is Bytes/second (so_max_pacing_rate), chip counts in Kilobits/second.
171 */
172 static int
update_tx_rate_limit(struct adapter * sc,struct toepcb * toep,u_int Bps)173 update_tx_rate_limit(struct adapter *sc, struct toepcb *toep, u_int Bps)
174 {
175 int tc_idx, rc;
176 const u_int kbps = (u_int) (uint64_t)Bps * 8ULL / 1000;
177 const int port_id = toep->vi->pi->port_id;
178
179 CTR3(KTR_CXGBE, "%s: tid %u, rate %uKbps", __func__, toep->tid, kbps);
180
181 if (kbps == 0) {
182 /* unbind */
183 tc_idx = -1;
184 } else {
185 rc = t4_reserve_cl_rl_kbps(sc, port_id, kbps, &tc_idx);
186 if (rc != 0)
187 return (rc);
188 MPASS(tc_idx >= 0 && tc_idx < sc->params.nsched_cls);
189 }
190
191 if (toep->params.tc_idx != tc_idx) {
192 struct wrqe *wr;
193 struct fw_flowc_wr *flowc;
194 int nparams = 1, flowclen, flowclen16;
195 struct ofld_tx_sdesc *txsd = &toep->txsd[toep->txsd_pidx];
196
197 flowclen = sizeof(*flowc) + nparams * sizeof(struct
198 fw_flowc_mnemval);
199 flowclen16 = howmany(flowclen, 16);
200 if (toep->tx_credits < flowclen16 || toep->txsd_avail == 0 ||
201 (wr = alloc_wrqe(roundup2(flowclen, 16),
202 &toep->ofld_txq->wrq)) == NULL) {
203 if (tc_idx >= 0)
204 t4_release_cl_rl(sc, port_id, tc_idx);
205 return (ENOMEM);
206 }
207
208 flowc = wrtod(wr);
209 memset(flowc, 0, wr->wr_len);
210
211 flowc->op_to_nparams = htobe32(V_FW_WR_OP(FW_FLOWC_WR) |
212 V_FW_FLOWC_WR_NPARAMS(nparams));
213 flowc->flowid_len16 = htonl(V_FW_WR_LEN16(flowclen16) |
214 V_FW_WR_FLOWID(toep->tid));
215
216 flowc->mnemval[0].mnemonic = FW_FLOWC_MNEM_SCHEDCLASS;
217 if (tc_idx == -1)
218 flowc->mnemval[0].val = htobe32(0xff);
219 else
220 flowc->mnemval[0].val = htobe32(tc_idx);
221
222 KASSERT(flowclen16 <= MAX_OFLD_TX_SDESC_CREDITS,
223 ("%s: tx_credits %u too large", __func__, flowclen16));
224 txsd->tx_credits = flowclen16;
225 txsd->plen = 0;
226 toep->tx_credits -= txsd->tx_credits;
227 if (__predict_false(++toep->txsd_pidx == toep->txsd_total))
228 toep->txsd_pidx = 0;
229 toep->txsd_avail--;
230 t4_wrq_tx(sc, wr);
231 }
232
233 if (toep->params.tc_idx >= 0)
234 t4_release_cl_rl(sc, port_id, toep->params.tc_idx);
235 toep->params.tc_idx = tc_idx;
236
237 return (0);
238 }
239 #endif
240
241 void
send_reset(struct adapter * sc,struct toepcb * toep,uint32_t snd_nxt)242 send_reset(struct adapter *sc, struct toepcb *toep, uint32_t snd_nxt)
243 {
244 struct wrqe *wr;
245 struct cpl_abort_req *req;
246 int tid = toep->tid;
247 struct inpcb *inp = toep->inp;
248 struct tcpcb *tp = intotcpcb(inp); /* don't use if INP_DROPPED */
249
250 INP_WLOCK_ASSERT(inp);
251
252 CTR6(KTR_CXGBE, "%s: tid %d (%s), toep_flags 0x%x, inp_flags 0x%x%s",
253 __func__, toep->tid,
254 inp->inp_flags & INP_DROPPED ? "inp dropped" :
255 tcpstates[tp->t_state],
256 toep->flags, inp->inp_flags,
257 toep->flags & TPF_ABORT_SHUTDOWN ?
258 " (abort already in progress)" : "");
259
260 if (toep->flags & TPF_ABORT_SHUTDOWN)
261 return; /* abort already in progress */
262
263 toep->flags |= TPF_ABORT_SHUTDOWN;
264
265 KASSERT(toep->flags & TPF_FLOWC_WR_SENT,
266 ("%s: flowc_wr not sent for tid %d.", __func__, tid));
267
268 wr = alloc_wrqe(sizeof(*req), &toep->ofld_txq->wrq);
269 if (wr == NULL) {
270 /* XXX */
271 panic("%s: allocation failure.", __func__);
272 }
273 req = wrtod(wr);
274
275 INIT_TP_WR_MIT_CPL(req, CPL_ABORT_REQ, tid);
276 if (inp->inp_flags & INP_DROPPED)
277 req->rsvd0 = htobe32(snd_nxt);
278 else
279 req->rsvd0 = htobe32(tp->snd_nxt);
280 req->rsvd1 = !(toep->flags & TPF_TX_DATA_SENT);
281 req->cmd = CPL_ABORT_SEND_RST;
282
283 /*
284 * XXX: What's the correct way to tell that the inp hasn't been detached
285 * from its socket? Should I even be flushing the snd buffer here?
286 */
287 if ((inp->inp_flags & INP_DROPPED) == 0) {
288 struct socket *so = inp->inp_socket;
289
290 if (so != NULL) /* because I'm not sure. See comment above */
291 sbflush(&so->so_snd);
292 }
293
294 t4_l2t_send(sc, wr, toep->l2te);
295 }
296
297 /*
298 * Called when a connection is established to translate the TCP options
299 * reported by HW to FreeBSD's native format.
300 */
301 static void
assign_rxopt(struct tcpcb * tp,uint16_t opt)302 assign_rxopt(struct tcpcb *tp, uint16_t opt)
303 {
304 struct toepcb *toep = tp->t_toe;
305 struct inpcb *inp = tptoinpcb(tp);
306 struct adapter *sc = td_adapter(toep->td);
307
308 INP_LOCK_ASSERT(inp);
309
310 toep->params.mtu_idx = G_TCPOPT_MSS(opt);
311 tp->t_maxseg = sc->params.mtus[toep->params.mtu_idx];
312 if (inp->inp_inc.inc_flags & INC_ISIPV6)
313 tp->t_maxseg -= sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
314 else
315 tp->t_maxseg -= sizeof(struct ip) + sizeof(struct tcphdr);
316
317 toep->params.emss = tp->t_maxseg;
318 if (G_TCPOPT_TSTAMP(opt)) {
319 toep->params.tstamp = 1;
320 toep->params.emss -= TCPOLEN_TSTAMP_APPA;
321 tp->t_flags |= TF_RCVD_TSTMP; /* timestamps ok */
322 tp->ts_recent = 0; /* hmmm */
323 tp->ts_recent_age = tcp_ts_getticks();
324 } else
325 toep->params.tstamp = 0;
326
327 if (G_TCPOPT_SACK(opt)) {
328 toep->params.sack = 1;
329 tp->t_flags |= TF_SACK_PERMIT; /* should already be set */
330 } else {
331 toep->params.sack = 0;
332 tp->t_flags &= ~TF_SACK_PERMIT; /* sack disallowed by peer */
333 }
334
335 if (G_TCPOPT_WSCALE_OK(opt))
336 tp->t_flags |= TF_RCVD_SCALE;
337
338 /* Doing window scaling? */
339 if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
340 (TF_RCVD_SCALE | TF_REQ_SCALE)) {
341 tp->rcv_scale = tp->request_r_scale;
342 tp->snd_scale = G_TCPOPT_SND_WSCALE(opt);
343 } else
344 toep->params.wscale = 0;
345
346 CTR6(KTR_CXGBE,
347 "assign_rxopt: tid %d, mtu_idx %u, emss %u, ts %u, sack %u, wscale %u",
348 toep->tid, toep->params.mtu_idx, toep->params.emss,
349 toep->params.tstamp, toep->params.sack, toep->params.wscale);
350 }
351
352 /*
353 * Completes some final bits of initialization for just established connections
354 * and changes their state to TCPS_ESTABLISHED.
355 *
356 * The ISNs are from the exchange of SYNs.
357 */
358 void
make_established(struct toepcb * toep,uint32_t iss,uint32_t irs,uint16_t opt)359 make_established(struct toepcb *toep, uint32_t iss, uint32_t irs, uint16_t opt)
360 {
361 struct inpcb *inp = toep->inp;
362 struct socket *so = inp->inp_socket;
363 struct tcpcb *tp = intotcpcb(inp);
364 uint16_t tcpopt = be16toh(opt);
365
366 INP_WLOCK_ASSERT(inp);
367 KASSERT(tp->t_state == TCPS_SYN_SENT ||
368 tp->t_state == TCPS_SYN_RECEIVED,
369 ("%s: TCP state %s", __func__, tcpstates[tp->t_state]));
370
371 CTR6(KTR_CXGBE, "%s: tid %d, so %p, inp %p, tp %p, toep %p",
372 __func__, toep->tid, so, inp, tp, toep);
373
374 tcp_state_change(tp, TCPS_ESTABLISHED);
375 tp->t_starttime = ticks;
376 TCPSTAT_INC(tcps_connects);
377
378 tp->irs = irs;
379 tcp_rcvseqinit(tp);
380 tp->rcv_wnd = (u_int)toep->params.opt0_bufsize << 10;
381 tp->rcv_adv += tp->rcv_wnd;
382 tp->last_ack_sent = tp->rcv_nxt;
383
384 tp->iss = iss;
385 tcp_sendseqinit(tp);
386 tp->snd_una = iss + 1;
387 tp->snd_nxt = iss + 1;
388 tp->snd_max = iss + 1;
389
390 assign_rxopt(tp, tcpopt);
391 send_flowc_wr(toep, tp);
392
393 soisconnected(so);
394 }
395
396 int
send_rx_credits(struct adapter * sc,struct toepcb * toep,int credits)397 send_rx_credits(struct adapter *sc, struct toepcb *toep, int credits)
398 {
399 struct wrqe *wr;
400 struct cpl_rx_data_ack *req;
401 uint32_t dack = F_RX_DACK_CHANGE | V_RX_DACK_MODE(1);
402
403 KASSERT(credits >= 0, ("%s: %d credits", __func__, credits));
404
405 wr = alloc_wrqe(sizeof(*req), toep->ctrlq);
406 if (wr == NULL)
407 return (0);
408 req = wrtod(wr);
409
410 INIT_TP_WR_MIT_CPL(req, CPL_RX_DATA_ACK, toep->tid);
411 req->credit_dack = htobe32(dack | V_RX_CREDITS(credits));
412
413 t4_wrq_tx(sc, wr);
414 return (credits);
415 }
416
417 void
t4_rcvd_locked(struct toedev * tod,struct tcpcb * tp)418 t4_rcvd_locked(struct toedev *tod, struct tcpcb *tp)
419 {
420 struct adapter *sc = tod->tod_softc;
421 struct inpcb *inp = tptoinpcb(tp);
422 struct socket *so = inp->inp_socket;
423 struct sockbuf *sb = &so->so_rcv;
424 struct toepcb *toep = tp->t_toe;
425 int rx_credits;
426
427 INP_WLOCK_ASSERT(inp);
428 SOCKBUF_LOCK_ASSERT(sb);
429
430 rx_credits = sbspace(sb) > tp->rcv_wnd ? sbspace(sb) - tp->rcv_wnd : 0;
431 if (rx_credits > 0 &&
432 (tp->rcv_wnd <= 32 * 1024 || rx_credits >= 64 * 1024 ||
433 (rx_credits >= 16 * 1024 && tp->rcv_wnd <= 128 * 1024) ||
434 sbused(sb) + tp->rcv_wnd < sb->sb_lowat)) {
435 rx_credits = send_rx_credits(sc, toep, rx_credits);
436 tp->rcv_wnd += rx_credits;
437 tp->rcv_adv += rx_credits;
438 }
439 }
440
441 void
t4_rcvd(struct toedev * tod,struct tcpcb * tp)442 t4_rcvd(struct toedev *tod, struct tcpcb *tp)
443 {
444 struct inpcb *inp = tptoinpcb(tp);
445 struct socket *so = inp->inp_socket;
446 struct sockbuf *sb = &so->so_rcv;
447
448 SOCKBUF_LOCK(sb);
449 t4_rcvd_locked(tod, tp);
450 SOCKBUF_UNLOCK(sb);
451 }
452
453 /*
454 * Close a connection by sending a CPL_CLOSE_CON_REQ message.
455 */
456 int
t4_close_conn(struct adapter * sc,struct toepcb * toep)457 t4_close_conn(struct adapter *sc, struct toepcb *toep)
458 {
459 struct wrqe *wr;
460 struct cpl_close_con_req *req;
461 unsigned int tid = toep->tid;
462
463 CTR3(KTR_CXGBE, "%s: tid %u%s", __func__, toep->tid,
464 toep->flags & TPF_FIN_SENT ? ", IGNORED" : "");
465
466 if (toep->flags & TPF_FIN_SENT)
467 return (0);
468
469 KASSERT(toep->flags & TPF_FLOWC_WR_SENT,
470 ("%s: flowc_wr not sent for tid %u.", __func__, tid));
471
472 wr = alloc_wrqe(sizeof(*req), &toep->ofld_txq->wrq);
473 if (wr == NULL) {
474 /* XXX */
475 panic("%s: allocation failure.", __func__);
476 }
477 req = wrtod(wr);
478
479 req->wr.wr_hi = htonl(V_FW_WR_OP(FW_TP_WR) |
480 V_FW_WR_IMMDLEN(sizeof(*req) - sizeof(req->wr)));
481 req->wr.wr_mid = htonl(V_FW_WR_LEN16(howmany(sizeof(*req), 16)) |
482 V_FW_WR_FLOWID(tid));
483 req->wr.wr_lo = cpu_to_be64(0);
484 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_CON_REQ, tid));
485 req->rsvd = 0;
486
487 toep->flags |= TPF_FIN_SENT;
488 toep->flags &= ~TPF_SEND_FIN;
489 t4_l2t_send(sc, wr, toep->l2te);
490
491 return (0);
492 }
493
494 #define MAX_OFLD_TX_CREDITS (SGE_MAX_WR_LEN / 16)
495 #define MIN_OFLD_TX_CREDITS (howmany(sizeof(struct fw_ofld_tx_data_wr) + 1, 16))
496 #define MIN_ISO_TX_CREDITS (howmany(sizeof(struct cpl_tx_data_iso), 16))
497 #define MIN_TX_CREDITS(iso) \
498 (MIN_OFLD_TX_CREDITS + ((iso) ? MIN_ISO_TX_CREDITS : 0))
499
500 _Static_assert(MAX_OFLD_TX_CREDITS <= MAX_OFLD_TX_SDESC_CREDITS,
501 "MAX_OFLD_TX_SDESC_CREDITS too small");
502
503 /* Maximum amount of immediate data we could stuff in a WR */
504 static inline int
max_imm_payload(int tx_credits,int iso)505 max_imm_payload(int tx_credits, int iso)
506 {
507 const int iso_cpl_size = iso ? sizeof(struct cpl_tx_data_iso) : 0;
508 const int n = 1; /* Use no more than one desc for imm. data WR */
509
510 KASSERT(tx_credits >= 0 &&
511 tx_credits <= MAX_OFLD_TX_CREDITS,
512 ("%s: %d credits", __func__, tx_credits));
513
514 if (tx_credits < MIN_TX_CREDITS(iso))
515 return (0);
516
517 if (tx_credits >= (n * EQ_ESIZE) / 16)
518 return ((n * EQ_ESIZE) - sizeof(struct fw_ofld_tx_data_wr) -
519 iso_cpl_size);
520 else
521 return (tx_credits * 16 - sizeof(struct fw_ofld_tx_data_wr) -
522 iso_cpl_size);
523 }
524
525 /* Maximum number of SGL entries we could stuff in a WR */
526 static inline int
max_dsgl_nsegs(int tx_credits,int iso)527 max_dsgl_nsegs(int tx_credits, int iso)
528 {
529 int nseg = 1; /* ulptx_sgl has room for 1, rest ulp_tx_sge_pair */
530 int sge_pair_credits = tx_credits - MIN_TX_CREDITS(iso);
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_TX_CREDITS(iso))
537 return (0);
538
539 nseg += 2 * (sge_pair_credits * 16 / 24);
540 if ((sge_pair_credits * 16) % 24 == 16)
541 nseg++;
542
543 return (nseg);
544 }
545
546 static inline void
write_tx_wr(void * dst,struct toepcb * toep,int fw_wr_opcode,unsigned int immdlen,unsigned int plen,uint8_t credits,int shove,int ulp_submode)547 write_tx_wr(void *dst, struct toepcb *toep, int fw_wr_opcode,
548 unsigned int immdlen, unsigned int plen, uint8_t credits, int shove,
549 int ulp_submode)
550 {
551 struct fw_ofld_tx_data_wr *txwr = dst;
552
553 txwr->op_to_immdlen = htobe32(V_WR_OP(fw_wr_opcode) |
554 V_FW_WR_IMMDLEN(immdlen));
555 txwr->flowid_len16 = htobe32(V_FW_WR_FLOWID(toep->tid) |
556 V_FW_WR_LEN16(credits));
557 txwr->lsodisable_to_flags = htobe32(V_TX_ULP_MODE(ulp_mode(toep)) |
558 V_TX_ULP_SUBMODE(ulp_submode) | V_TX_URG(0) | V_TX_SHOVE(shove));
559 txwr->plen = htobe32(plen);
560
561 if (toep->params.tx_align > 0) {
562 if (plen < 2 * toep->params.emss)
563 txwr->lsodisable_to_flags |=
564 htobe32(F_FW_OFLD_TX_DATA_WR_LSODISABLE);
565 else
566 txwr->lsodisable_to_flags |=
567 htobe32(F_FW_OFLD_TX_DATA_WR_ALIGNPLD |
568 (toep->params.nagle == 0 ? 0 :
569 F_FW_OFLD_TX_DATA_WR_ALIGNPLDSHOVE));
570 }
571 }
572
573 /*
574 * Generate a DSGL from a starting mbuf. The total number of segments and the
575 * maximum segments in any one mbuf are provided.
576 */
577 static void
write_tx_sgl(void * dst,struct mbuf * start,struct mbuf * stop,int nsegs,int n)578 write_tx_sgl(void *dst, struct mbuf *start, struct mbuf *stop, int nsegs, int n)
579 {
580 struct mbuf *m;
581 struct ulptx_sgl *usgl = dst;
582 int i, j, rc;
583 struct sglist sg;
584 struct sglist_seg segs[n];
585
586 KASSERT(nsegs > 0, ("%s: nsegs 0", __func__));
587
588 sglist_init(&sg, n, segs);
589 usgl->cmd_nsge = htobe32(V_ULPTX_CMD(ULP_TX_SC_DSGL) |
590 V_ULPTX_NSGE(nsegs));
591
592 i = -1;
593 for (m = start; m != stop; m = m->m_next) {
594 if (m->m_flags & M_EXTPG)
595 rc = sglist_append_mbuf_epg(&sg, m,
596 mtod(m, vm_offset_t), m->m_len);
597 else
598 rc = sglist_append(&sg, mtod(m, void *), m->m_len);
599 if (__predict_false(rc != 0))
600 panic("%s: sglist_append %d", __func__, rc);
601
602 for (j = 0; j < sg.sg_nseg; i++, j++) {
603 if (i < 0) {
604 usgl->len0 = htobe32(segs[j].ss_len);
605 usgl->addr0 = htobe64(segs[j].ss_paddr);
606 } else {
607 usgl->sge[i / 2].len[i & 1] =
608 htobe32(segs[j].ss_len);
609 usgl->sge[i / 2].addr[i & 1] =
610 htobe64(segs[j].ss_paddr);
611 }
612 #ifdef INVARIANTS
613 nsegs--;
614 #endif
615 }
616 sglist_reset(&sg);
617 }
618 if (i & 1)
619 usgl->sge[i / 2].len[1] = htobe32(0);
620 KASSERT(nsegs == 0, ("%s: nsegs %d, start %p, stop %p",
621 __func__, nsegs, start, stop));
622 }
623
624 bool
t4_push_raw_wr(struct adapter * sc,struct toepcb * toep,struct mbuf * m)625 t4_push_raw_wr(struct adapter *sc, struct toepcb *toep, struct mbuf *m)
626 {
627 #ifdef INVARIANTS
628 struct inpcb *inp = toep->inp;
629 #endif
630 struct wrqe *wr;
631 struct ofld_tx_sdesc *txsd;
632 u_int credits, plen;
633
634 INP_WLOCK_ASSERT(inp);
635 MPASS(mbuf_raw_wr(m));
636 plen = m->m_pkthdr.len;
637 credits = howmany(plen, 16);
638 if (credits > toep->tx_credits)
639 return (false);
640
641 wr = alloc_wrqe(roundup2(plen, 16), &toep->ofld_txq->wrq);
642 if (wr == NULL)
643 return (false);
644
645 m_copydata(m, 0, plen, wrtod(wr));
646 m_freem(m);
647
648 toep->tx_credits -= credits;
649 if (toep->tx_credits < MIN_OFLD_TX_CREDITS)
650 toep->flags |= TPF_TX_SUSPENDED;
651
652 KASSERT(toep->txsd_avail > 0, ("%s: no txsd", __func__));
653 KASSERT(credits <= MAX_OFLD_TX_SDESC_CREDITS,
654 ("%s: tx_credits %u too large", __func__, credits));
655 txsd = &toep->txsd[toep->txsd_pidx];
656 txsd->plen = 0;
657 txsd->tx_credits = credits;
658 if (__predict_false(++toep->txsd_pidx == toep->txsd_total))
659 toep->txsd_pidx = 0;
660 toep->txsd_avail--;
661
662 t4_wrq_tx(sc, wr);
663 return (true);
664 }
665
666 /*
667 * Max number of SGL entries an offload tx work request can have. This is 41
668 * (1 + 40) for a full 512B work request.
669 * fw_ofld_tx_data_wr(16B) + ulptx_sgl(16B, 1) + ulptx_sge_pair(480B, 40)
670 */
671 #define OFLD_SGL_LEN (41)
672
673 /*
674 * Send data and/or a FIN to the peer.
675 *
676 * The socket's so_snd buffer consists of a stream of data starting with sb_mb
677 * and linked together with m_next. sb_sndptr, if set, is the last mbuf that
678 * was transmitted.
679 *
680 * drop indicates the number of bytes that should be dropped from the head of
681 * the send buffer. It is an optimization that lets do_fw4_ack avoid creating
682 * contention on the send buffer lock (before this change it used to do
683 * sowwakeup and then t4_push_frames right after that when recovering from tx
684 * stalls). When drop is set this function MUST drop the bytes and wake up any
685 * writers.
686 */
687 static void
t4_push_frames(struct adapter * sc,struct toepcb * toep,int drop)688 t4_push_frames(struct adapter *sc, struct toepcb *toep, int drop)
689 {
690 struct mbuf *sndptr, *m, *sb_sndptr;
691 struct fw_ofld_tx_data_wr *txwr;
692 struct wrqe *wr;
693 u_int plen, nsegs, credits, max_imm, max_nsegs, max_nsegs_1mbuf;
694 struct inpcb *inp = toep->inp;
695 struct tcpcb *tp = intotcpcb(inp);
696 struct socket *so = inp->inp_socket;
697 struct sockbuf *sb = &so->so_snd;
698 struct mbufq *pduq = &toep->ulp_pduq;
699 int tx_credits, shove, compl, sowwakeup;
700 struct ofld_tx_sdesc *txsd;
701 bool nomap_mbuf_seen;
702
703 INP_WLOCK_ASSERT(inp);
704 KASSERT(toep->flags & TPF_FLOWC_WR_SENT,
705 ("%s: flowc_wr not sent for tid %u.", __func__, toep->tid));
706
707 KASSERT(ulp_mode(toep) == ULP_MODE_NONE ||
708 ulp_mode(toep) == ULP_MODE_TCPDDP ||
709 ulp_mode(toep) == ULP_MODE_TLS ||
710 ulp_mode(toep) == ULP_MODE_RDMA,
711 ("%s: ulp_mode %u for toep %p", __func__, ulp_mode(toep), toep));
712
713 #ifdef VERBOSE_TRACES
714 CTR5(KTR_CXGBE, "%s: tid %d toep flags %#x tp flags %#x drop %d",
715 __func__, toep->tid, toep->flags, tp->t_flags, drop);
716 #endif
717 if (__predict_false(toep->flags & TPF_ABORT_SHUTDOWN))
718 return;
719
720 #ifdef RATELIMIT
721 if (__predict_false(inp->inp_flags2 & INP_RATE_LIMIT_CHANGED) &&
722 (update_tx_rate_limit(sc, toep, so->so_max_pacing_rate) == 0)) {
723 inp->inp_flags2 &= ~INP_RATE_LIMIT_CHANGED;
724 }
725 #endif
726
727 /*
728 * This function doesn't resume by itself. Someone else must clear the
729 * flag and call this function.
730 */
731 if (__predict_false(toep->flags & TPF_TX_SUSPENDED)) {
732 KASSERT(drop == 0,
733 ("%s: drop (%d) != 0 but tx is suspended", __func__, drop));
734 return;
735 }
736
737 txsd = &toep->txsd[toep->txsd_pidx];
738 do {
739 tx_credits = min(toep->tx_credits, MAX_OFLD_TX_CREDITS);
740 max_imm = max_imm_payload(tx_credits, 0);
741 max_nsegs = max_dsgl_nsegs(tx_credits, 0);
742
743 if (__predict_false((sndptr = mbufq_first(pduq)) != NULL)) {
744 if (!t4_push_raw_wr(sc, toep, sndptr)) {
745 toep->flags |= TPF_TX_SUSPENDED;
746 return;
747 }
748
749 m = mbufq_dequeue(pduq);
750 MPASS(m == sndptr);
751
752 txsd = &toep->txsd[toep->txsd_pidx];
753 continue;
754 }
755
756 SOCKBUF_LOCK(sb);
757 sowwakeup = drop;
758 if (drop) {
759 sbdrop_locked(sb, drop);
760 drop = 0;
761 }
762 sb_sndptr = sb->sb_sndptr;
763 sndptr = sb_sndptr ? sb_sndptr->m_next : sb->sb_mb;
764 plen = 0;
765 nsegs = 0;
766 max_nsegs_1mbuf = 0; /* max # of SGL segments in any one mbuf */
767 nomap_mbuf_seen = false;
768 for (m = sndptr; m != NULL; m = m->m_next) {
769 int n;
770
771 if ((m->m_flags & M_NOTREADY) != 0)
772 break;
773 if (plen + m->m_len > MAX_OFLD_TX_SDESC_PLEN)
774 break;
775 if (m->m_flags & M_EXTPG) {
776 #ifdef KERN_TLS
777 if (m->m_epg_tls != NULL) {
778 toep->flags |= TPF_KTLS;
779 if (plen == 0) {
780 SOCKBUF_UNLOCK(sb);
781 t4_push_ktls(sc, toep, 0);
782 return;
783 }
784 break;
785 }
786 #endif
787 n = sglist_count_mbuf_epg(m,
788 mtod(m, vm_offset_t), m->m_len);
789 } else
790 n = sglist_count(mtod(m, void *), m->m_len);
791
792 nsegs += n;
793 plen += m->m_len;
794
795 /* This mbuf sent us _over_ the nsegs limit, back out */
796 if (plen > max_imm && nsegs > max_nsegs) {
797 nsegs -= n;
798 plen -= m->m_len;
799 if (plen == 0) {
800 /* Too few credits */
801 toep->flags |= TPF_TX_SUSPENDED;
802 if (sowwakeup) {
803 if (!TAILQ_EMPTY(
804 &toep->aiotx_jobq))
805 t4_aiotx_queue_toep(so,
806 toep);
807 sowwakeup_locked(so);
808 } else
809 SOCKBUF_UNLOCK(sb);
810 SOCKBUF_UNLOCK_ASSERT(sb);
811 return;
812 }
813 break;
814 }
815
816 if (m->m_flags & M_EXTPG)
817 nomap_mbuf_seen = true;
818 if (max_nsegs_1mbuf < n)
819 max_nsegs_1mbuf = n;
820 sb_sndptr = m; /* new sb->sb_sndptr if all goes well */
821
822 /* This mbuf put us right at the max_nsegs limit */
823 if (plen > max_imm && nsegs == max_nsegs) {
824 m = m->m_next;
825 break;
826 }
827 }
828
829 if (sbused(sb) > sb->sb_hiwat * 5 / 8 &&
830 toep->plen_nocompl + plen >= sb->sb_hiwat / 4)
831 compl = 1;
832 else
833 compl = 0;
834
835 if (sb->sb_flags & SB_AUTOSIZE &&
836 V_tcp_do_autosndbuf &&
837 sb->sb_hiwat < V_tcp_autosndbuf_max &&
838 sbused(sb) >= sb->sb_hiwat * 7 / 8) {
839 int newsize = min(sb->sb_hiwat + V_tcp_autosndbuf_inc,
840 V_tcp_autosndbuf_max);
841
842 if (!sbreserve_locked(so, SO_SND, newsize, NULL))
843 sb->sb_flags &= ~SB_AUTOSIZE;
844 else
845 sowwakeup = 1; /* room available */
846 }
847 if (sowwakeup) {
848 if (!TAILQ_EMPTY(&toep->aiotx_jobq))
849 t4_aiotx_queue_toep(so, toep);
850 sowwakeup_locked(so);
851 } else
852 SOCKBUF_UNLOCK(sb);
853 SOCKBUF_UNLOCK_ASSERT(sb);
854
855 /* nothing to send */
856 if (plen == 0) {
857 KASSERT(m == NULL || (m->m_flags & M_NOTREADY) != 0,
858 ("%s: nothing to send, but m != NULL is ready",
859 __func__));
860 break;
861 }
862
863 if (__predict_false(toep->flags & TPF_FIN_SENT))
864 panic("%s: excess tx.", __func__);
865
866 shove = m == NULL && !(tp->t_flags & TF_MORETOCOME);
867 if (plen <= max_imm && !nomap_mbuf_seen) {
868
869 /* Immediate data tx */
870
871 wr = alloc_wrqe(roundup2(sizeof(*txwr) + plen, 16),
872 &toep->ofld_txq->wrq);
873 if (wr == NULL) {
874 /* XXX: how will we recover from this? */
875 toep->flags |= TPF_TX_SUSPENDED;
876 return;
877 }
878 txwr = wrtod(wr);
879 credits = howmany(wr->wr_len, 16);
880 write_tx_wr(txwr, toep, FW_OFLD_TX_DATA_WR, plen, plen,
881 credits, shove, 0);
882 m_copydata(sndptr, 0, plen, (void *)(txwr + 1));
883 nsegs = 0;
884 } else {
885 int wr_len;
886
887 /* DSGL tx */
888
889 wr_len = sizeof(*txwr) + sizeof(struct ulptx_sgl) +
890 ((3 * (nsegs - 1)) / 2 + ((nsegs - 1) & 1)) * 8;
891 wr = alloc_wrqe(roundup2(wr_len, 16),
892 &toep->ofld_txq->wrq);
893 if (wr == NULL) {
894 /* XXX: how will we recover from this? */
895 toep->flags |= TPF_TX_SUSPENDED;
896 return;
897 }
898 txwr = wrtod(wr);
899 credits = howmany(wr_len, 16);
900 write_tx_wr(txwr, toep, FW_OFLD_TX_DATA_WR, 0, plen,
901 credits, shove, 0);
902 write_tx_sgl(txwr + 1, sndptr, m, nsegs,
903 max_nsegs_1mbuf);
904 if (wr_len & 0xf) {
905 uint64_t *pad = (uint64_t *)
906 ((uintptr_t)txwr + wr_len);
907 *pad = 0;
908 }
909 }
910
911 KASSERT(toep->tx_credits >= credits,
912 ("%s: not enough credits", __func__));
913
914 toep->tx_credits -= credits;
915 toep->tx_nocompl += credits;
916 toep->plen_nocompl += plen;
917 if (toep->tx_credits <= toep->tx_total * 3 / 8 &&
918 toep->tx_nocompl >= toep->tx_total / 4)
919 compl = 1;
920
921 if (compl || ulp_mode(toep) == ULP_MODE_RDMA) {
922 txwr->op_to_immdlen |= htobe32(F_FW_WR_COMPL);
923 toep->tx_nocompl = 0;
924 toep->plen_nocompl = 0;
925 }
926
927 tp->snd_nxt += plen;
928 tp->snd_max += plen;
929
930 SOCKBUF_LOCK(sb);
931 KASSERT(sb_sndptr, ("%s: sb_sndptr is NULL", __func__));
932 sb->sb_sndptr = sb_sndptr;
933 SOCKBUF_UNLOCK(sb);
934
935 toep->flags |= TPF_TX_DATA_SENT;
936 if (toep->tx_credits < MIN_OFLD_TX_CREDITS)
937 toep->flags |= TPF_TX_SUSPENDED;
938
939 KASSERT(toep->txsd_avail > 0, ("%s: no txsd", __func__));
940 KASSERT(plen <= MAX_OFLD_TX_SDESC_PLEN,
941 ("%s: plen %u too large", __func__, plen));
942 txsd->plen = plen;
943 txsd->tx_credits = credits;
944 txsd++;
945 if (__predict_false(++toep->txsd_pidx == toep->txsd_total)) {
946 toep->txsd_pidx = 0;
947 txsd = &toep->txsd[0];
948 }
949 toep->txsd_avail--;
950
951 t4_l2t_send(sc, wr, toep->l2te);
952 } while (m != NULL && (m->m_flags & M_NOTREADY) == 0);
953
954 /* Send a FIN if requested, but only if there's no more data to send */
955 if (m == NULL && toep->flags & TPF_SEND_FIN)
956 t4_close_conn(sc, toep);
957 }
958
959 static inline void
rqdrop_locked(struct mbufq * q,int plen)960 rqdrop_locked(struct mbufq *q, int plen)
961 {
962 struct mbuf *m;
963
964 while (plen > 0) {
965 m = mbufq_dequeue(q);
966
967 /* Too many credits. */
968 MPASS(m != NULL);
969 M_ASSERTPKTHDR(m);
970
971 /* Partial credits. */
972 MPASS(plen >= m->m_pkthdr.len);
973
974 plen -= m->m_pkthdr.len;
975 m_freem(m);
976 }
977 }
978
979 /*
980 * Not a bit in the TCB, but is a bit in the ulp_submode field of the
981 * CPL_TX_DATA flags field in FW_ISCSI_TX_DATA_WR.
982 */
983 #define ULP_ISO G_TX_ULP_SUBMODE(F_FW_ISCSI_TX_DATA_WR_ULPSUBMODE_ISO)
984
985 static void
write_iscsi_tx_data_iso(void * dst,u_int ulp_submode,uint8_t flags,uint16_t mss,int len,int npdu)986 write_iscsi_tx_data_iso(void *dst, u_int ulp_submode, uint8_t flags,
987 uint16_t mss, int len, int npdu)
988 {
989 struct cpl_tx_data_iso *cpl;
990 unsigned int burst_size;
991 unsigned int last;
992
993 /*
994 * The firmware will set the 'F' bit on the last PDU when
995 * either condition is true:
996 *
997 * - this large PDU is marked as the "last" slice
998 *
999 * - the amount of data payload bytes equals the burst_size
1000 *
1001 * The strategy used here is to always set the burst_size
1002 * artificially high (len includes the size of the template
1003 * BHS) and only set the "last" flag if the original PDU had
1004 * 'F' set.
1005 */
1006 burst_size = len;
1007 last = !!(flags & CXGBE_ISO_F);
1008
1009 cpl = (struct cpl_tx_data_iso *)dst;
1010 cpl->op_to_scsi = htonl(V_CPL_TX_DATA_ISO_OP(CPL_TX_DATA_ISO) |
1011 V_CPL_TX_DATA_ISO_FIRST(1) | V_CPL_TX_DATA_ISO_LAST(last) |
1012 V_CPL_TX_DATA_ISO_CPLHDRLEN(0) |
1013 V_CPL_TX_DATA_ISO_HDRCRC(!!(ulp_submode & ULP_CRC_HEADER)) |
1014 V_CPL_TX_DATA_ISO_PLDCRC(!!(ulp_submode & ULP_CRC_DATA)) |
1015 V_CPL_TX_DATA_ISO_IMMEDIATE(0) |
1016 V_CPL_TX_DATA_ISO_SCSI(CXGBE_ISO_TYPE(flags)));
1017
1018 cpl->ahs_len = 0;
1019 cpl->mpdu = htons(DIV_ROUND_UP(mss, 4));
1020 cpl->burst_size = htonl(DIV_ROUND_UP(burst_size, 4));
1021 cpl->len = htonl(len);
1022 cpl->reserved2_seglen_offset = htonl(0);
1023 cpl->datasn_offset = htonl(0);
1024 cpl->buffer_offset = htonl(0);
1025 cpl->reserved3 = 0;
1026 }
1027
1028 static struct wrqe *
write_iscsi_mbuf_wr(struct toepcb * toep,struct mbuf * sndptr)1029 write_iscsi_mbuf_wr(struct toepcb *toep, struct mbuf *sndptr)
1030 {
1031 struct mbuf *m;
1032 struct fw_ofld_tx_data_wr *txwr;
1033 struct cpl_tx_data_iso *cpl_iso;
1034 void *p;
1035 struct wrqe *wr;
1036 u_int plen, nsegs, credits, max_imm, max_nsegs, max_nsegs_1mbuf;
1037 u_int adjusted_plen, imm_data, ulp_submode;
1038 struct inpcb *inp = toep->inp;
1039 struct tcpcb *tp = intotcpcb(inp);
1040 int tx_credits, shove, npdu, wr_len;
1041 uint16_t iso_mss;
1042 static const u_int ulp_extra_len[] = {0, 4, 4, 8};
1043 bool iso, nomap_mbuf_seen;
1044
1045 M_ASSERTPKTHDR(sndptr);
1046
1047 tx_credits = min(toep->tx_credits, MAX_OFLD_TX_CREDITS);
1048 if (mbuf_raw_wr(sndptr)) {
1049 plen = sndptr->m_pkthdr.len;
1050 KASSERT(plen <= SGE_MAX_WR_LEN,
1051 ("raw WR len %u is greater than max WR len", plen));
1052 if (plen > tx_credits * 16)
1053 return (NULL);
1054
1055 wr = alloc_wrqe(roundup2(plen, 16), &toep->ofld_txq->wrq);
1056 if (__predict_false(wr == NULL))
1057 return (NULL);
1058
1059 m_copydata(sndptr, 0, plen, wrtod(wr));
1060 return (wr);
1061 }
1062
1063 iso = mbuf_iscsi_iso(sndptr);
1064 max_imm = max_imm_payload(tx_credits, iso);
1065 max_nsegs = max_dsgl_nsegs(tx_credits, iso);
1066 iso_mss = mbuf_iscsi_iso_mss(sndptr);
1067
1068 plen = 0;
1069 nsegs = 0;
1070 max_nsegs_1mbuf = 0; /* max # of SGL segments in any one mbuf */
1071 nomap_mbuf_seen = false;
1072 for (m = sndptr; m != NULL; m = m->m_next) {
1073 int n;
1074
1075 if (m->m_flags & M_EXTPG)
1076 n = sglist_count_mbuf_epg(m, mtod(m, vm_offset_t),
1077 m->m_len);
1078 else
1079 n = sglist_count(mtod(m, void *), m->m_len);
1080
1081 nsegs += n;
1082 plen += m->m_len;
1083
1084 /*
1085 * This mbuf would send us _over_ the nsegs limit.
1086 * Suspend tx because the PDU can't be sent out.
1087 */
1088 if ((nomap_mbuf_seen || plen > max_imm) && nsegs > max_nsegs)
1089 return (NULL);
1090
1091 if (m->m_flags & M_EXTPG)
1092 nomap_mbuf_seen = true;
1093 if (max_nsegs_1mbuf < n)
1094 max_nsegs_1mbuf = n;
1095 }
1096
1097 if (__predict_false(toep->flags & TPF_FIN_SENT))
1098 panic("%s: excess tx.", __func__);
1099
1100 /*
1101 * We have a PDU to send. All of it goes out in one WR so 'm'
1102 * is NULL. A PDU's length is always a multiple of 4.
1103 */
1104 MPASS(m == NULL);
1105 MPASS((plen & 3) == 0);
1106 MPASS(sndptr->m_pkthdr.len == plen);
1107
1108 shove = !(tp->t_flags & TF_MORETOCOME);
1109
1110 /*
1111 * plen doesn't include header and data digests, which are
1112 * generated and inserted in the right places by the TOE, but
1113 * they do occupy TCP sequence space and need to be accounted
1114 * for.
1115 */
1116 ulp_submode = mbuf_ulp_submode(sndptr);
1117 MPASS(ulp_submode < nitems(ulp_extra_len));
1118 npdu = iso ? howmany(plen - ISCSI_BHS_SIZE, iso_mss) : 1;
1119 adjusted_plen = plen + ulp_extra_len[ulp_submode] * npdu;
1120 if (iso)
1121 adjusted_plen += ISCSI_BHS_SIZE * (npdu - 1);
1122 wr_len = sizeof(*txwr);
1123 if (iso)
1124 wr_len += sizeof(struct cpl_tx_data_iso);
1125 if (plen <= max_imm && !nomap_mbuf_seen) {
1126 /* Immediate data tx */
1127 imm_data = plen;
1128 wr_len += plen;
1129 nsegs = 0;
1130 } else {
1131 /* DSGL tx */
1132 imm_data = 0;
1133 wr_len += sizeof(struct ulptx_sgl) +
1134 ((3 * (nsegs - 1)) / 2 + ((nsegs - 1) & 1)) * 8;
1135 }
1136
1137 wr = alloc_wrqe(roundup2(wr_len, 16), &toep->ofld_txq->wrq);
1138 if (wr == NULL) {
1139 /* XXX: how will we recover from this? */
1140 return (NULL);
1141 }
1142 txwr = wrtod(wr);
1143 credits = howmany(wr->wr_len, 16);
1144
1145 if (iso) {
1146 write_tx_wr(txwr, toep, FW_ISCSI_TX_DATA_WR,
1147 imm_data + sizeof(struct cpl_tx_data_iso),
1148 adjusted_plen, credits, shove, ulp_submode | ULP_ISO);
1149 cpl_iso = (struct cpl_tx_data_iso *)(txwr + 1);
1150 MPASS(plen == sndptr->m_pkthdr.len);
1151 write_iscsi_tx_data_iso(cpl_iso, ulp_submode,
1152 mbuf_iscsi_iso_flags(sndptr), iso_mss, plen, npdu);
1153 p = cpl_iso + 1;
1154 } else {
1155 write_tx_wr(txwr, toep, FW_OFLD_TX_DATA_WR, imm_data,
1156 adjusted_plen, credits, shove, ulp_submode);
1157 p = txwr + 1;
1158 }
1159
1160 if (imm_data != 0) {
1161 m_copydata(sndptr, 0, plen, p);
1162 } else {
1163 write_tx_sgl(p, sndptr, m, nsegs, max_nsegs_1mbuf);
1164 if (wr_len & 0xf) {
1165 uint64_t *pad = (uint64_t *)((uintptr_t)txwr + wr_len);
1166 *pad = 0;
1167 }
1168 }
1169
1170 KASSERT(toep->tx_credits >= credits,
1171 ("%s: not enough credits: credits %u "
1172 "toep->tx_credits %u tx_credits %u nsegs %u "
1173 "max_nsegs %u iso %d", __func__, credits,
1174 toep->tx_credits, tx_credits, nsegs, max_nsegs, iso));
1175
1176 tp->snd_nxt += adjusted_plen;
1177 tp->snd_max += adjusted_plen;
1178
1179 counter_u64_add(toep->ofld_txq->tx_iscsi_pdus, npdu);
1180 counter_u64_add(toep->ofld_txq->tx_iscsi_octets, plen);
1181 if (iso)
1182 counter_u64_add(toep->ofld_txq->tx_iscsi_iso_wrs, 1);
1183
1184 return (wr);
1185 }
1186
1187 static void
write_nvme_tx_data_iso(void * dst,u_int ulp_submode,u_int iso_type,uint16_t mss,int len,int npdu,int pdo)1188 write_nvme_tx_data_iso(void *dst, u_int ulp_submode, u_int iso_type,
1189 uint16_t mss, int len, int npdu, int pdo)
1190 {
1191 struct cpl_t7_tx_data_iso *cpl;
1192 unsigned int burst_size;
1193
1194 /*
1195 * TODO: Need to figure out how the LAST_PDU and SUCCESS flags
1196 * are handled.
1197 *
1198 * - Does len need padding bytes? (If so, does padding need
1199 * to be in DSGL input?)
1200 *
1201 * - burst always 0?
1202 */
1203 burst_size = 0;
1204
1205 cpl = (struct cpl_t7_tx_data_iso *)dst;
1206 cpl->op_to_scsi = htonl(V_CPL_T7_TX_DATA_ISO_OPCODE(CPL_TX_DATA_ISO) |
1207 V_CPL_T7_TX_DATA_ISO_FIRST(1) |
1208 V_CPL_T7_TX_DATA_ISO_LAST(1) |
1209 V_CPL_T7_TX_DATA_ISO_CPLHDRLEN(0) |
1210 V_CPL_T7_TX_DATA_ISO_HDRCRC(!!(ulp_submode & ULP_CRC_HEADER)) |
1211 V_CPL_T7_TX_DATA_ISO_PLDCRC(!!(ulp_submode & ULP_CRC_DATA)) |
1212 V_CPL_T7_TX_DATA_ISO_IMMEDIATE(0) |
1213 V_CPL_T7_TX_DATA_ISO_SCSI(iso_type));
1214
1215 cpl->nvme_tcp_pkd = F_CPL_T7_TX_DATA_ISO_NVME_TCP;
1216 cpl->ahs = 0;
1217 cpl->mpdu = htons(DIV_ROUND_UP(mss, 4));
1218 cpl->burst = htonl(DIV_ROUND_UP(burst_size, 4));
1219 cpl->size = htonl(len);
1220 cpl->num_pi_bytes_seglen_offset = htonl(0);
1221 cpl->datasn_offset = htonl(0);
1222 cpl->buffer_offset = htonl(0);
1223 cpl->pdo_pkd = pdo;
1224 }
1225
1226 static struct wrqe *
write_nvme_mbuf_wr(struct toepcb * toep,struct mbuf * sndptr)1227 write_nvme_mbuf_wr(struct toepcb *toep, struct mbuf *sndptr)
1228 {
1229 struct mbuf *m;
1230 const struct nvme_tcp_common_pdu_hdr *hdr;
1231 struct fw_ofld_tx_data_wr *txwr;
1232 struct cpl_tx_data_iso *cpl_iso;
1233 void *p;
1234 struct wrqe *wr;
1235 u_int plen, nsegs, credits, max_imm, max_nsegs, max_nsegs_1mbuf;
1236 u_int adjusted_plen, imm_data, ulp_submode;
1237 struct inpcb *inp = toep->inp;
1238 struct tcpcb *tp = intotcpcb(inp);
1239 int tx_credits, shove, npdu, wr_len;
1240 uint16_t iso_mss;
1241 bool iso, nomap_mbuf_seen;
1242
1243 M_ASSERTPKTHDR(sndptr);
1244
1245 tx_credits = min(toep->tx_credits, MAX_OFLD_TX_CREDITS);
1246 if (mbuf_raw_wr(sndptr)) {
1247 plen = sndptr->m_pkthdr.len;
1248 KASSERT(plen <= SGE_MAX_WR_LEN,
1249 ("raw WR len %u is greater than max WR len", plen));
1250 if (plen > tx_credits * 16)
1251 return (NULL);
1252
1253 wr = alloc_wrqe(roundup2(plen, 16), &toep->ofld_txq->wrq);
1254 if (__predict_false(wr == NULL))
1255 return (NULL);
1256
1257 m_copydata(sndptr, 0, plen, wrtod(wr));
1258 return (wr);
1259 }
1260
1261 iso = mbuf_iscsi_iso(sndptr);
1262 max_imm = max_imm_payload(tx_credits, iso);
1263 max_nsegs = max_dsgl_nsegs(tx_credits, iso);
1264 iso_mss = mbuf_iscsi_iso_mss(sndptr);
1265
1266 plen = 0;
1267 nsegs = 0;
1268 max_nsegs_1mbuf = 0; /* max # of SGL segments in any one mbuf */
1269 nomap_mbuf_seen = false;
1270 for (m = sndptr; m != NULL; m = m->m_next) {
1271 int n;
1272
1273 if (m->m_flags & M_EXTPG)
1274 n = sglist_count_mbuf_epg(m, mtod(m, vm_offset_t),
1275 m->m_len);
1276 else
1277 n = sglist_count(mtod(m, void *), m->m_len);
1278
1279 nsegs += n;
1280 plen += m->m_len;
1281
1282 /*
1283 * This mbuf would send us _over_ the nsegs limit.
1284 * Suspend tx because the PDU can't be sent out.
1285 */
1286 if ((nomap_mbuf_seen || plen > max_imm) && nsegs > max_nsegs)
1287 return (NULL);
1288
1289 if (m->m_flags & M_EXTPG)
1290 nomap_mbuf_seen = true;
1291 if (max_nsegs_1mbuf < n)
1292 max_nsegs_1mbuf = n;
1293 }
1294
1295 if (__predict_false(toep->flags & TPF_FIN_SENT))
1296 panic("%s: excess tx.", __func__);
1297
1298 /*
1299 * We have a PDU to send. All of it goes out in one WR so 'm'
1300 * is NULL. A PDU's length is always a multiple of 4.
1301 */
1302 MPASS(m == NULL);
1303 MPASS((plen & 3) == 0);
1304 MPASS(sndptr->m_pkthdr.len == plen);
1305
1306 shove = !(tp->t_flags & TF_MORETOCOME);
1307
1308 /*
1309 * plen doesn't include header digests, padding, and data
1310 * digests which are generated and inserted in the right
1311 * places by the TOE, but they do occupy TCP sequence space
1312 * and need to be accounted for.
1313 *
1314 * To determine the overhead, check the PDU header in sndptr.
1315 * Note that only certain PDU types can use digests and
1316 * padding, and PDO accounts for all but the data digests for
1317 * those PDUs.
1318 */
1319 MPASS((sndptr->m_flags & M_EXTPG) == 0);
1320 ulp_submode = mbuf_ulp_submode(sndptr);
1321 hdr = mtod(sndptr, const void *);
1322 switch (hdr->pdu_type) {
1323 case NVME_TCP_PDU_TYPE_H2C_TERM_REQ:
1324 case NVME_TCP_PDU_TYPE_C2H_TERM_REQ:
1325 MPASS(ulp_submode == 0);
1326 MPASS(!iso);
1327 break;
1328 case NVME_TCP_PDU_TYPE_CAPSULE_RESP:
1329 case NVME_TCP_PDU_TYPE_R2T:
1330 MPASS((ulp_submode & ULP_CRC_DATA) == 0);
1331 /* FALLTHROUGH */
1332 case NVME_TCP_PDU_TYPE_CAPSULE_CMD:
1333 MPASS(!iso);
1334 break;
1335 case NVME_TCP_PDU_TYPE_H2C_DATA:
1336 case NVME_TCP_PDU_TYPE_C2H_DATA:
1337 if (le32toh(hdr->plen) + ((ulp_submode & ULP_CRC_DATA) != 0 ?
1338 sizeof(uint32_t) : 0) == plen)
1339 MPASS(!iso);
1340 break;
1341 default:
1342 __assert_unreachable();
1343 }
1344
1345 if (iso) {
1346 npdu = howmany(plen - hdr->hlen, iso_mss);
1347 adjusted_plen = hdr->pdo * npdu + (plen - hdr->hlen);
1348 if ((ulp_submode & ULP_CRC_DATA) != 0)
1349 adjusted_plen += npdu * sizeof(uint32_t);
1350 } else {
1351 npdu = 1;
1352 adjusted_plen = le32toh(hdr->plen);
1353 }
1354 wr_len = sizeof(*txwr);
1355 if (iso)
1356 wr_len += sizeof(struct cpl_tx_data_iso);
1357 if (plen <= max_imm && !nomap_mbuf_seen) {
1358 /* Immediate data tx */
1359 imm_data = plen;
1360 wr_len += plen;
1361 nsegs = 0;
1362 } else {
1363 /* DSGL tx for PDU data */
1364 imm_data = 0;
1365 wr_len += sizeof(struct ulptx_sgl) +
1366 ((3 * (nsegs - 1)) / 2 + ((nsegs - 1) & 1)) * 8;
1367 }
1368
1369 wr = alloc_wrqe(roundup2(wr_len, 16), &toep->ofld_txq->wrq);
1370 if (wr == NULL) {
1371 /* XXX: how will we recover from this? */
1372 return (NULL);
1373 }
1374 txwr = wrtod(wr);
1375 credits = howmany(wr->wr_len, 16);
1376
1377 if (iso) {
1378 write_tx_wr(txwr, toep, FW_ISCSI_TX_DATA_WR,
1379 imm_data + sizeof(struct cpl_tx_data_iso),
1380 adjusted_plen, credits, shove, ulp_submode | ULP_ISO);
1381 cpl_iso = (struct cpl_tx_data_iso *)(txwr + 1);
1382 MPASS(plen == sndptr->m_pkthdr.len);
1383 write_nvme_tx_data_iso(cpl_iso, ulp_submode,
1384 (hdr->pdu_type & 0x1) == 0 ? 1 : 2, iso_mss, plen, npdu,
1385 hdr->pdo);
1386 p = cpl_iso + 1;
1387 } else {
1388 write_tx_wr(txwr, toep, FW_OFLD_TX_DATA_WR, imm_data,
1389 adjusted_plen, credits, shove, ulp_submode);
1390 p = txwr + 1;
1391 }
1392
1393 if (imm_data != 0) {
1394 m_copydata(sndptr, 0, plen, p);
1395 } else {
1396 write_tx_sgl(p, sndptr, NULL, nsegs, max_nsegs_1mbuf);
1397 if (wr_len & 0xf) {
1398 uint64_t *pad = (uint64_t *)((uintptr_t)txwr + wr_len);
1399 *pad = 0;
1400 }
1401 }
1402
1403 KASSERT(toep->tx_credits >= credits,
1404 ("%s: not enough credits: credits %u "
1405 "toep->tx_credits %u tx_credits %u nsegs %u "
1406 "max_nsegs %u iso %d", __func__, credits,
1407 toep->tx_credits, tx_credits, nsegs, max_nsegs, iso));
1408
1409 tp->snd_nxt += adjusted_plen;
1410 tp->snd_max += adjusted_plen;
1411
1412 counter_u64_add(toep->ofld_txq->tx_nvme_pdus, npdu);
1413 counter_u64_add(toep->ofld_txq->tx_nvme_octets, plen);
1414 if (iso)
1415 counter_u64_add(toep->ofld_txq->tx_nvme_iso_wrs, 1);
1416
1417 return (wr);
1418 }
1419
1420 void
t4_push_pdus(struct adapter * sc,struct toepcb * toep,int drop)1421 t4_push_pdus(struct adapter *sc, struct toepcb *toep, int drop)
1422 {
1423 struct mbuf *sndptr, *m;
1424 struct fw_wr_hdr *wrhdr;
1425 struct wrqe *wr;
1426 u_int plen, credits, mode;
1427 struct inpcb *inp = toep->inp;
1428 struct ofld_tx_sdesc *txsd = &toep->txsd[toep->txsd_pidx];
1429 struct mbufq *pduq = &toep->ulp_pduq;
1430
1431 INP_WLOCK_ASSERT(inp);
1432 mode = ulp_mode(toep);
1433 KASSERT(toep->flags & TPF_FLOWC_WR_SENT,
1434 ("%s: flowc_wr not sent for tid %u.", __func__, toep->tid));
1435 KASSERT(mode == ULP_MODE_ISCSI || mode == ULP_MODE_NVMET,
1436 ("%s: ulp_mode %u for toep %p", __func__, ulp_mode(toep), toep));
1437
1438 if (__predict_false(toep->flags & TPF_ABORT_SHUTDOWN))
1439 return;
1440
1441 /*
1442 * This function doesn't resume by itself. Someone else must clear the
1443 * flag and call this function.
1444 */
1445 if (__predict_false(toep->flags & TPF_TX_SUSPENDED)) {
1446 KASSERT(drop == 0,
1447 ("%s: drop (%d) != 0 but tx is suspended", __func__, drop));
1448 return;
1449 }
1450
1451 if (drop) {
1452 struct socket *so = inp->inp_socket;
1453 struct sockbuf *sb = &so->so_snd;
1454 int sbu;
1455
1456 /*
1457 * An unlocked read is ok here as the data should only
1458 * transition from a non-zero value to either another
1459 * non-zero value or zero. Once it is zero it should
1460 * stay zero.
1461 */
1462 if (__predict_false(sbused(sb)) > 0) {
1463 SOCKBUF_LOCK(sb);
1464 sbu = sbused(sb);
1465 if (sbu > 0) {
1466 /*
1467 * The data transmitted before the
1468 * tid's ULP mode changed to ISCSI/NVMET is
1469 * still in so_snd. Incoming credits
1470 * should account for so_snd first.
1471 */
1472 sbdrop_locked(sb, min(sbu, drop));
1473 drop -= min(sbu, drop);
1474 }
1475 sowwakeup_locked(so); /* unlocks so_snd */
1476 }
1477 rqdrop_locked(&toep->ulp_pdu_reclaimq, drop);
1478 }
1479
1480 while ((sndptr = mbufq_first(pduq)) != NULL) {
1481 if (mode == ULP_MODE_ISCSI)
1482 wr = write_iscsi_mbuf_wr(toep, sndptr);
1483 else
1484 wr = write_nvme_mbuf_wr(toep, sndptr);
1485 if (wr == NULL) {
1486 toep->flags |= TPF_TX_SUSPENDED;
1487 return;
1488 }
1489
1490 plen = sndptr->m_pkthdr.len;
1491 credits = howmany(wr->wr_len, 16);
1492 KASSERT(toep->tx_credits >= credits,
1493 ("%s: not enough credits", __func__));
1494
1495 m = mbufq_dequeue(pduq);
1496 MPASS(m == sndptr);
1497 mbufq_enqueue(&toep->ulp_pdu_reclaimq, m);
1498
1499 toep->tx_credits -= credits;
1500 toep->tx_nocompl += credits;
1501 toep->plen_nocompl += plen;
1502
1503 /*
1504 * Ensure there are enough credits for a full-sized WR
1505 * as page pod WRs can be full-sized.
1506 */
1507 if (toep->tx_credits <= SGE_MAX_WR_LEN * 5 / 4 &&
1508 toep->tx_nocompl >= toep->tx_total / 4) {
1509 wrhdr = wrtod(wr);
1510 wrhdr->hi |= htobe32(F_FW_WR_COMPL);
1511 toep->tx_nocompl = 0;
1512 toep->plen_nocompl = 0;
1513 }
1514
1515 toep->flags |= TPF_TX_DATA_SENT;
1516 if (toep->tx_credits < MIN_OFLD_TX_CREDITS)
1517 toep->flags |= TPF_TX_SUSPENDED;
1518
1519 KASSERT(toep->txsd_avail > 0, ("%s: no txsd", __func__));
1520 KASSERT(plen <= MAX_OFLD_TX_SDESC_PLEN,
1521 ("%s: plen %u too large", __func__, plen));
1522 txsd->plen = plen;
1523 txsd->tx_credits = credits;
1524 txsd++;
1525 if (__predict_false(++toep->txsd_pidx == toep->txsd_total)) {
1526 toep->txsd_pidx = 0;
1527 txsd = &toep->txsd[0];
1528 }
1529 toep->txsd_avail--;
1530
1531 t4_l2t_send(sc, wr, toep->l2te);
1532 }
1533
1534 /* Send a FIN if requested, but only if there are no more PDUs to send */
1535 if (mbufq_first(pduq) == NULL && toep->flags & TPF_SEND_FIN)
1536 t4_close_conn(sc, toep);
1537 }
1538
1539 static inline void
t4_push_data(struct adapter * sc,struct toepcb * toep,int drop)1540 t4_push_data(struct adapter *sc, struct toepcb *toep, int drop)
1541 {
1542
1543 if (ulp_mode(toep) == ULP_MODE_ISCSI ||
1544 ulp_mode(toep) == ULP_MODE_NVMET)
1545 t4_push_pdus(sc, toep, drop);
1546 else if (toep->flags & TPF_KTLS)
1547 t4_push_ktls(sc, toep, drop);
1548 else
1549 t4_push_frames(sc, toep, drop);
1550 }
1551
1552 void
t4_raw_wr_tx(struct adapter * sc,struct toepcb * toep,struct mbuf * m)1553 t4_raw_wr_tx(struct adapter *sc, struct toepcb *toep, struct mbuf *m)
1554 {
1555 #ifdef INVARIANTS
1556 struct inpcb *inp = toep->inp;
1557 #endif
1558
1559 INP_WLOCK_ASSERT(inp);
1560
1561 /*
1562 * If there are other raw WRs enqueued, enqueue to preserve
1563 * FIFO ordering.
1564 */
1565 if (!mbufq_empty(&toep->ulp_pduq)) {
1566 mbufq_enqueue(&toep->ulp_pduq, m);
1567 return;
1568 }
1569
1570 /*
1571 * Cannot call t4_push_data here as that will lock so_snd and
1572 * some callers of this run in rx handlers with so_rcv locked.
1573 * Instead, just try to transmit this WR.
1574 */
1575 if (!t4_push_raw_wr(sc, toep, m)) {
1576 mbufq_enqueue(&toep->ulp_pduq, m);
1577 toep->flags |= TPF_TX_SUSPENDED;
1578 }
1579 }
1580
1581 int
t4_tod_output(struct toedev * tod,struct tcpcb * tp)1582 t4_tod_output(struct toedev *tod, struct tcpcb *tp)
1583 {
1584 struct adapter *sc = tod->tod_softc;
1585 #ifdef INVARIANTS
1586 struct inpcb *inp = tptoinpcb(tp);
1587 #endif
1588 struct toepcb *toep = tp->t_toe;
1589
1590 INP_WLOCK_ASSERT(inp);
1591 KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1592 ("%s: inp %p dropped.", __func__, inp));
1593 KASSERT(toep != NULL, ("%s: toep is NULL", __func__));
1594
1595 t4_push_data(sc, toep, 0);
1596
1597 return (0);
1598 }
1599
1600 int
t4_send_fin(struct toedev * tod,struct tcpcb * tp)1601 t4_send_fin(struct toedev *tod, struct tcpcb *tp)
1602 {
1603 struct adapter *sc = tod->tod_softc;
1604 #ifdef INVARIANTS
1605 struct inpcb *inp = tptoinpcb(tp);
1606 #endif
1607 struct toepcb *toep = tp->t_toe;
1608
1609 INP_WLOCK_ASSERT(inp);
1610 KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1611 ("%s: inp %p dropped.", __func__, inp));
1612 KASSERT(toep != NULL, ("%s: toep is NULL", __func__));
1613
1614 toep->flags |= TPF_SEND_FIN;
1615 if (tp->t_state >= TCPS_ESTABLISHED)
1616 t4_push_data(sc, toep, 0);
1617
1618 return (0);
1619 }
1620
1621 int
t4_send_rst(struct toedev * tod,struct tcpcb * tp)1622 t4_send_rst(struct toedev *tod, struct tcpcb *tp)
1623 {
1624 struct adapter *sc = tod->tod_softc;
1625 #if defined(INVARIANTS)
1626 struct inpcb *inp = tptoinpcb(tp);
1627 #endif
1628 struct toepcb *toep = tp->t_toe;
1629
1630 INP_WLOCK_ASSERT(inp);
1631 KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1632 ("%s: inp %p dropped.", __func__, inp));
1633 KASSERT(toep != NULL, ("%s: toep is NULL", __func__));
1634
1635 /* hmmmm */
1636 KASSERT(toep->flags & TPF_FLOWC_WR_SENT,
1637 ("%s: flowc for tid %u [%s] not sent already",
1638 __func__, toep->tid, tcpstates[tp->t_state]));
1639
1640 send_reset(sc, toep, 0);
1641 return (0);
1642 }
1643
1644 /*
1645 * Peer has sent us a FIN.
1646 */
1647 static int
do_peer_close(struct sge_iq * iq,const struct rss_header * rss,struct mbuf * m)1648 do_peer_close(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
1649 {
1650 struct adapter *sc = iq->adapter;
1651 const struct cpl_peer_close *cpl = (const void *)(rss + 1);
1652 unsigned int tid = GET_TID(cpl);
1653 struct toepcb *toep = lookup_tid(sc, tid);
1654 struct inpcb *inp = toep->inp;
1655 struct tcpcb *tp = NULL;
1656 struct socket *so;
1657 struct epoch_tracker et;
1658 #ifdef INVARIANTS
1659 unsigned int opcode = G_CPL_OPCODE(be32toh(OPCODE_TID(cpl)));
1660 #endif
1661
1662 KASSERT(opcode == CPL_PEER_CLOSE,
1663 ("%s: unexpected opcode 0x%x", __func__, opcode));
1664 KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
1665
1666 if (__predict_false(toep->flags & TPF_SYNQE)) {
1667 /*
1668 * do_pass_establish must have run before do_peer_close and if
1669 * this is still a synqe instead of a toepcb then the connection
1670 * must be getting aborted.
1671 */
1672 MPASS(toep->flags & TPF_ABORT_SHUTDOWN);
1673 CTR4(KTR_CXGBE, "%s: tid %u, synqe %p (0x%x)", __func__, tid,
1674 toep, toep->flags);
1675 return (0);
1676 }
1677
1678 KASSERT(toep->tid == tid, ("%s: toep tid mismatch", __func__));
1679
1680 CURVNET_SET(toep->vnet);
1681 NET_EPOCH_ENTER(et);
1682 INP_WLOCK(inp);
1683 tp = intotcpcb(inp);
1684
1685 CTR6(KTR_CXGBE,
1686 "%s: tid %u (%s), toep_flags 0x%x, ddp_flags 0x%x, inp %p",
1687 __func__, tid, tp ? tcpstates[tp->t_state] : "no tp", toep->flags,
1688 toep->ddp.flags, inp);
1689
1690 if (toep->flags & TPF_ABORT_SHUTDOWN)
1691 goto done;
1692
1693 if (ulp_mode(toep) == ULP_MODE_TCPDDP) {
1694 DDP_LOCK(toep);
1695 if (__predict_false(toep->ddp.flags &
1696 (DDP_BUF0_ACTIVE | DDP_BUF1_ACTIVE)))
1697 handle_ddp_close(toep, tp, cpl->rcv_nxt);
1698 DDP_UNLOCK(toep);
1699 }
1700 so = inp->inp_socket;
1701 socantrcvmore(so);
1702
1703 if (ulp_mode(toep) == ULP_MODE_RDMA ||
1704 (ulp_mode(toep) == ULP_MODE_ISCSI && chip_id(sc) >= CHELSIO_T6) ||
1705 ulp_mode(toep) == ULP_MODE_NVMET) {
1706 /*
1707 * There might be data received via DDP before the FIN
1708 * not reported to the driver. Just assume the
1709 * sequence number in the CPL is correct as the
1710 * sequence number of the FIN.
1711 */
1712 } else {
1713 KASSERT(tp->rcv_nxt + 1 == be32toh(cpl->rcv_nxt),
1714 ("%s: rcv_nxt mismatch: %u %u", __func__, tp->rcv_nxt,
1715 be32toh(cpl->rcv_nxt)));
1716 }
1717
1718 tp->rcv_nxt = be32toh(cpl->rcv_nxt);
1719
1720 switch (tp->t_state) {
1721 case TCPS_SYN_RECEIVED:
1722 tp->t_starttime = ticks;
1723 /* FALLTHROUGH */
1724
1725 case TCPS_ESTABLISHED:
1726 tcp_state_change(tp, TCPS_CLOSE_WAIT);
1727 break;
1728
1729 case TCPS_FIN_WAIT_1:
1730 tcp_state_change(tp, TCPS_CLOSING);
1731 break;
1732
1733 case TCPS_FIN_WAIT_2:
1734 restore_so_proto(so, inp->inp_vflag & INP_IPV6);
1735 t4_pcb_detach(NULL, tp);
1736 tcp_twstart(tp);
1737 INP_UNLOCK_ASSERT(inp); /* safe, we have a ref on the inp */
1738 NET_EPOCH_EXIT(et);
1739 CURVNET_RESTORE();
1740
1741 INP_WLOCK(inp);
1742 final_cpl_received(toep);
1743 return (0);
1744
1745 default:
1746 log(LOG_ERR, "%s: TID %u received CPL_PEER_CLOSE in state %d\n",
1747 __func__, tid, tp->t_state);
1748 }
1749 done:
1750 INP_WUNLOCK(inp);
1751 NET_EPOCH_EXIT(et);
1752 CURVNET_RESTORE();
1753 return (0);
1754 }
1755
1756 /*
1757 * Peer has ACK'd our FIN.
1758 */
1759 static int
do_close_con_rpl(struct sge_iq * iq,const struct rss_header * rss,struct mbuf * m)1760 do_close_con_rpl(struct sge_iq *iq, const struct rss_header *rss,
1761 struct mbuf *m)
1762 {
1763 struct adapter *sc = iq->adapter;
1764 const struct cpl_close_con_rpl *cpl = (const void *)(rss + 1);
1765 unsigned int tid = GET_TID(cpl);
1766 struct toepcb *toep = lookup_tid(sc, tid);
1767 struct inpcb *inp = toep->inp;
1768 struct tcpcb *tp = NULL;
1769 struct socket *so = NULL;
1770 struct epoch_tracker et;
1771 #ifdef INVARIANTS
1772 unsigned int opcode = G_CPL_OPCODE(be32toh(OPCODE_TID(cpl)));
1773 #endif
1774
1775 KASSERT(opcode == CPL_CLOSE_CON_RPL,
1776 ("%s: unexpected opcode 0x%x", __func__, opcode));
1777 KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
1778 KASSERT(toep->tid == tid, ("%s: toep tid mismatch", __func__));
1779
1780 CURVNET_SET(toep->vnet);
1781 NET_EPOCH_ENTER(et);
1782 INP_WLOCK(inp);
1783 tp = intotcpcb(inp);
1784
1785 CTR4(KTR_CXGBE, "%s: tid %u (%s), toep_flags 0x%x",
1786 __func__, tid, tp ? tcpstates[tp->t_state] : "no tp", toep->flags);
1787
1788 if (toep->flags & TPF_ABORT_SHUTDOWN)
1789 goto done;
1790
1791 so = inp->inp_socket;
1792 tp->snd_una = be32toh(cpl->snd_nxt) - 1; /* exclude FIN */
1793
1794 switch (tp->t_state) {
1795 case TCPS_CLOSING: /* see TCPS_FIN_WAIT_2 in do_peer_close too */
1796 restore_so_proto(so, inp->inp_vflag & INP_IPV6);
1797 t4_pcb_detach(NULL, tp);
1798 tcp_twstart(tp);
1799 release:
1800 INP_UNLOCK_ASSERT(inp); /* safe, we have a ref on the inp */
1801 NET_EPOCH_EXIT(et);
1802 CURVNET_RESTORE();
1803
1804 INP_WLOCK(inp);
1805 final_cpl_received(toep); /* no more CPLs expected */
1806
1807 return (0);
1808 case TCPS_LAST_ACK:
1809 if (tcp_close(tp))
1810 INP_WUNLOCK(inp);
1811 goto release;
1812
1813 case TCPS_FIN_WAIT_1:
1814 if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
1815 soisdisconnected(so);
1816 tcp_state_change(tp, TCPS_FIN_WAIT_2);
1817 break;
1818
1819 default:
1820 log(LOG_ERR,
1821 "%s: TID %u received CPL_CLOSE_CON_RPL in state %s\n",
1822 __func__, tid, tcpstates[tp->t_state]);
1823 }
1824 done:
1825 INP_WUNLOCK(inp);
1826 NET_EPOCH_EXIT(et);
1827 CURVNET_RESTORE();
1828 return (0);
1829 }
1830
1831 void
send_abort_rpl(struct adapter * sc,struct sge_ofld_txq * ofld_txq,int tid,int rst_status)1832 send_abort_rpl(struct adapter *sc, struct sge_ofld_txq *ofld_txq, int tid,
1833 int rst_status)
1834 {
1835 struct wrqe *wr;
1836 struct cpl_abort_rpl *cpl;
1837
1838 wr = alloc_wrqe(sizeof(*cpl), &ofld_txq->wrq);
1839 if (wr == NULL) {
1840 /* XXX */
1841 panic("%s: allocation failure.", __func__);
1842 }
1843 cpl = wrtod(wr);
1844
1845 INIT_TP_WR_MIT_CPL(cpl, CPL_ABORT_RPL, tid);
1846 cpl->cmd = rst_status;
1847
1848 t4_wrq_tx(sc, wr);
1849 }
1850
1851 static int
abort_status_to_errno(struct tcpcb * tp,unsigned int abort_reason)1852 abort_status_to_errno(struct tcpcb *tp, unsigned int abort_reason)
1853 {
1854 switch (abort_reason) {
1855 case CPL_ERR_BAD_SYN:
1856 case CPL_ERR_CONN_RESET:
1857 return (tp->t_state == TCPS_CLOSE_WAIT ? EPIPE : ECONNRESET);
1858 case CPL_ERR_XMIT_TIMEDOUT:
1859 case CPL_ERR_PERSIST_TIMEDOUT:
1860 case CPL_ERR_FINWAIT2_TIMEDOUT:
1861 case CPL_ERR_KEEPALIVE_TIMEDOUT:
1862 return (ETIMEDOUT);
1863 default:
1864 return (EIO);
1865 }
1866 }
1867
1868 /*
1869 * TCP RST from the peer, timeout, or some other such critical error.
1870 */
1871 static int
do_abort_req(struct sge_iq * iq,const struct rss_header * rss,struct mbuf * m)1872 do_abort_req(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
1873 {
1874 struct adapter *sc = iq->adapter;
1875 const struct cpl_abort_req_rss *cpl = (const void *)(rss + 1);
1876 unsigned int tid = GET_TID(cpl);
1877 struct toepcb *toep = lookup_tid(sc, tid);
1878 struct sge_ofld_txq *ofld_txq = toep->ofld_txq;
1879 struct inpcb *inp;
1880 struct tcpcb *tp;
1881 struct epoch_tracker et;
1882 #ifdef INVARIANTS
1883 unsigned int opcode = G_CPL_OPCODE(be32toh(OPCODE_TID(cpl)));
1884 #endif
1885
1886 KASSERT(opcode == CPL_ABORT_REQ_RSS,
1887 ("%s: unexpected opcode 0x%x", __func__, opcode));
1888 KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
1889
1890 if (toep->flags & TPF_SYNQE)
1891 return (do_abort_req_synqe(iq, rss, m));
1892
1893 KASSERT(toep->tid == tid, ("%s: toep tid mismatch", __func__));
1894
1895 if (negative_advice(cpl->status)) {
1896 CTR4(KTR_CXGBE, "%s: negative advice %d for tid %d (0x%x)",
1897 __func__, cpl->status, tid, toep->flags);
1898 return (0); /* Ignore negative advice */
1899 }
1900
1901 inp = toep->inp;
1902 CURVNET_SET(toep->vnet);
1903 NET_EPOCH_ENTER(et); /* for tcp_close */
1904 INP_WLOCK(inp);
1905
1906 tp = intotcpcb(inp);
1907
1908 CTR6(KTR_CXGBE,
1909 "%s: tid %d (%s), toep_flags 0x%x, inp_flags 0x%x, status %d",
1910 __func__, tid, tp ? tcpstates[tp->t_state] : "no tp", toep->flags,
1911 inp->inp_flags, cpl->status);
1912
1913 /*
1914 * If we'd initiated an abort earlier the reply to it is responsible for
1915 * cleaning up resources. Otherwise we tear everything down right here
1916 * right now. We owe the T4 a CPL_ABORT_RPL no matter what.
1917 */
1918 if (toep->flags & TPF_ABORT_SHUTDOWN) {
1919 INP_WUNLOCK(inp);
1920 goto done;
1921 }
1922 toep->flags |= TPF_ABORT_SHUTDOWN;
1923
1924 if ((inp->inp_flags & INP_DROPPED) == 0) {
1925 struct socket *so = inp->inp_socket;
1926
1927 if (so != NULL)
1928 so_error_set(so, abort_status_to_errno(tp,
1929 cpl->status));
1930 tp = tcp_close(tp);
1931 if (tp == NULL)
1932 INP_WLOCK(inp); /* re-acquire */
1933 }
1934
1935 final_cpl_received(toep);
1936 done:
1937 NET_EPOCH_EXIT(et);
1938 CURVNET_RESTORE();
1939 send_abort_rpl(sc, ofld_txq, tid, CPL_ABORT_NO_RST);
1940 return (0);
1941 }
1942
1943 /*
1944 * Reply to the CPL_ABORT_REQ (send_reset)
1945 */
1946 static int
do_abort_rpl(struct sge_iq * iq,const struct rss_header * rss,struct mbuf * m)1947 do_abort_rpl(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
1948 {
1949 struct adapter *sc = iq->adapter;
1950 const struct cpl_abort_rpl_rss *cpl = (const void *)(rss + 1);
1951 unsigned int tid = GET_TID(cpl);
1952 struct toepcb *toep = lookup_tid(sc, tid);
1953 struct inpcb *inp = toep->inp;
1954 #ifdef INVARIANTS
1955 unsigned int opcode = G_CPL_OPCODE(be32toh(OPCODE_TID(cpl)));
1956 #endif
1957
1958 KASSERT(opcode == CPL_ABORT_RPL_RSS,
1959 ("%s: unexpected opcode 0x%x", __func__, opcode));
1960 KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
1961
1962 if (toep->flags & TPF_SYNQE)
1963 return (do_abort_rpl_synqe(iq, rss, m));
1964
1965 KASSERT(toep->tid == tid, ("%s: toep tid mismatch", __func__));
1966
1967 CTR5(KTR_CXGBE, "%s: tid %u, toep %p, inp %p, status %d",
1968 __func__, tid, toep, inp, cpl->status);
1969
1970 KASSERT(toep->flags & TPF_ABORT_SHUTDOWN,
1971 ("%s: wasn't expecting abort reply", __func__));
1972
1973 INP_WLOCK(inp);
1974 final_cpl_received(toep);
1975
1976 return (0);
1977 }
1978
1979 static int
do_rx_data(struct sge_iq * iq,const struct rss_header * rss,struct mbuf * m)1980 do_rx_data(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
1981 {
1982 struct adapter *sc = iq->adapter;
1983 const struct cpl_rx_data *cpl = mtod(m, const void *);
1984 unsigned int tid = GET_TID(cpl);
1985 struct toepcb *toep = lookup_tid(sc, tid);
1986 struct inpcb *inp = toep->inp;
1987 struct tcpcb *tp;
1988 struct socket *so;
1989 struct sockbuf *sb;
1990 struct epoch_tracker et;
1991 int len;
1992 uint32_t ddp_placed = 0;
1993
1994 if (__predict_false(toep->flags & TPF_SYNQE)) {
1995 /*
1996 * do_pass_establish must have run before do_rx_data and if this
1997 * is still a synqe instead of a toepcb then the connection must
1998 * be getting aborted.
1999 */
2000 MPASS(toep->flags & TPF_ABORT_SHUTDOWN);
2001 CTR4(KTR_CXGBE, "%s: tid %u, synqe %p (0x%x)", __func__, tid,
2002 toep, toep->flags);
2003 m_freem(m);
2004 return (0);
2005 }
2006
2007 KASSERT(toep->tid == tid, ("%s: toep tid mismatch", __func__));
2008
2009 /* strip off CPL header */
2010 m_adj(m, sizeof(*cpl));
2011 len = m->m_pkthdr.len;
2012
2013 INP_WLOCK(inp);
2014 if (inp->inp_flags & INP_DROPPED) {
2015 CTR4(KTR_CXGBE, "%s: tid %u, rx (%d bytes), inp_flags 0x%x",
2016 __func__, tid, len, inp->inp_flags);
2017 INP_WUNLOCK(inp);
2018 m_freem(m);
2019 return (0);
2020 }
2021
2022 tp = intotcpcb(inp);
2023
2024 if (__predict_false(ulp_mode(toep) == ULP_MODE_TLS &&
2025 toep->flags & TPF_TLS_RECEIVE)) {
2026 /* Received "raw" data on a TLS socket. */
2027 CTR3(KTR_CXGBE, "%s: tid %u, raw TLS data (%d bytes)",
2028 __func__, tid, len);
2029 do_rx_data_tls(cpl, toep, m);
2030 return (0);
2031 }
2032
2033 if (__predict_false(tp->rcv_nxt != be32toh(cpl->seq)))
2034 ddp_placed = be32toh(cpl->seq) - tp->rcv_nxt;
2035
2036 tp->rcv_nxt += len;
2037 if (tp->rcv_wnd < len) {
2038 KASSERT(ulp_mode(toep) == ULP_MODE_RDMA,
2039 ("%s: negative window size", __func__));
2040 }
2041
2042 tp->rcv_wnd -= len;
2043 tp->t_rcvtime = ticks;
2044
2045 if (ulp_mode(toep) == ULP_MODE_TCPDDP)
2046 DDP_LOCK(toep);
2047 so = inp_inpcbtosocket(inp);
2048 sb = &so->so_rcv;
2049 SOCKBUF_LOCK(sb);
2050
2051 if (__predict_false(sb->sb_state & SBS_CANTRCVMORE)) {
2052 CTR3(KTR_CXGBE, "%s: tid %u, excess rx (%d bytes)",
2053 __func__, tid, len);
2054 m_freem(m);
2055 SOCKBUF_UNLOCK(sb);
2056 if (ulp_mode(toep) == ULP_MODE_TCPDDP)
2057 DDP_UNLOCK(toep);
2058 INP_WUNLOCK(inp);
2059
2060 CURVNET_SET(toep->vnet);
2061 NET_EPOCH_ENTER(et);
2062 INP_WLOCK(inp);
2063 tp = tcp_drop(tp, ECONNRESET);
2064 if (tp)
2065 INP_WUNLOCK(inp);
2066 NET_EPOCH_EXIT(et);
2067 CURVNET_RESTORE();
2068
2069 return (0);
2070 }
2071
2072 /* receive buffer autosize */
2073 MPASS(toep->vnet == so->so_vnet);
2074 CURVNET_SET(toep->vnet);
2075 if (sb->sb_flags & SB_AUTOSIZE &&
2076 V_tcp_do_autorcvbuf &&
2077 sb->sb_hiwat < V_tcp_autorcvbuf_max &&
2078 len > (sbspace(sb) / 8 * 7)) {
2079 unsigned int hiwat = sb->sb_hiwat;
2080 unsigned int newsize = min(hiwat + sc->tt.autorcvbuf_inc,
2081 V_tcp_autorcvbuf_max);
2082
2083 if (!sbreserve_locked(so, SO_RCV, newsize, NULL))
2084 sb->sb_flags &= ~SB_AUTOSIZE;
2085 }
2086
2087 if (ulp_mode(toep) == ULP_MODE_TCPDDP) {
2088 int changed = !(toep->ddp.flags & DDP_ON) ^ cpl->ddp_off;
2089
2090 if (toep->ddp.waiting_count != 0 || toep->ddp.active_count != 0)
2091 CTR3(KTR_CXGBE, "%s: tid %u, non-ddp rx (%d bytes)",
2092 __func__, tid, len);
2093
2094 if (changed) {
2095 if (toep->ddp.flags & DDP_SC_REQ)
2096 toep->ddp.flags ^= DDP_ON | DDP_SC_REQ;
2097 else if (cpl->ddp_off == 1) {
2098 /* Fell out of DDP mode */
2099 toep->ddp.flags &= ~DDP_ON;
2100 CTR1(KTR_CXGBE, "%s: fell out of DDP mode",
2101 __func__);
2102
2103 insert_ddp_data(toep, ddp_placed);
2104 } else {
2105 /*
2106 * Data was received while still
2107 * ULP_MODE_NONE, just fall through.
2108 */
2109 }
2110 }
2111
2112 if (toep->ddp.flags & DDP_ON) {
2113 /*
2114 * CPL_RX_DATA with DDP on can only be an indicate.
2115 * Start posting queued AIO requests via DDP. The
2116 * payload that arrived in this indicate is appended
2117 * to the socket buffer as usual.
2118 */
2119 handle_ddp_indicate(toep);
2120 }
2121 }
2122
2123 sbappendstream_locked(sb, m, 0);
2124 t4_rcvd_locked(&toep->td->tod, tp);
2125
2126 if (ulp_mode(toep) == ULP_MODE_TCPDDP &&
2127 (toep->ddp.flags & DDP_AIO) != 0 && toep->ddp.waiting_count > 0 &&
2128 sbavail(sb) != 0) {
2129 CTR2(KTR_CXGBE, "%s: tid %u queueing AIO task", __func__,
2130 tid);
2131 ddp_queue_toep(toep);
2132 }
2133 if (toep->flags & TPF_TLS_STARTING)
2134 tls_received_starting_data(sc, toep, sb, len);
2135 sorwakeup_locked(so);
2136 SOCKBUF_UNLOCK_ASSERT(sb);
2137 if (ulp_mode(toep) == ULP_MODE_TCPDDP)
2138 DDP_UNLOCK(toep);
2139
2140 INP_WUNLOCK(inp);
2141 CURVNET_RESTORE();
2142 return (0);
2143 }
2144
2145 static int
do_fw4_ack(struct sge_iq * iq,const struct rss_header * rss,struct mbuf * m)2146 do_fw4_ack(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
2147 {
2148 struct adapter *sc = iq->adapter;
2149 const struct cpl_fw4_ack *cpl = (const void *)(rss + 1);
2150 unsigned int tid = G_CPL_FW4_ACK_FLOWID(be32toh(OPCODE_TID(cpl)));
2151 struct toepcb *toep = lookup_tid(sc, tid);
2152 struct inpcb *inp;
2153 struct tcpcb *tp;
2154 struct socket *so;
2155 uint8_t credits = cpl->credits;
2156 struct ofld_tx_sdesc *txsd;
2157 int plen;
2158 #ifdef INVARIANTS
2159 unsigned int opcode = G_CPL_FW4_ACK_OPCODE(be32toh(OPCODE_TID(cpl)));
2160 #endif
2161
2162 /*
2163 * Very unusual case: we'd sent a flowc + abort_req for a synq entry and
2164 * now this comes back carrying the credits for the flowc.
2165 */
2166 if (__predict_false(toep->flags & TPF_SYNQE)) {
2167 KASSERT(toep->flags & TPF_ABORT_SHUTDOWN,
2168 ("%s: credits for a synq entry %p", __func__, toep));
2169 return (0);
2170 }
2171
2172 inp = toep->inp;
2173
2174 KASSERT(opcode == CPL_FW4_ACK,
2175 ("%s: unexpected opcode 0x%x", __func__, opcode));
2176 KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
2177 KASSERT(toep->tid == tid, ("%s: toep tid mismatch", __func__));
2178
2179 INP_WLOCK(inp);
2180
2181 if (__predict_false(toep->flags & TPF_ABORT_SHUTDOWN)) {
2182 INP_WUNLOCK(inp);
2183 return (0);
2184 }
2185
2186 KASSERT((inp->inp_flags & INP_DROPPED) == 0,
2187 ("%s: inp_flags 0x%x", __func__, inp->inp_flags));
2188
2189 tp = intotcpcb(inp);
2190
2191 if (cpl->flags & CPL_FW4_ACK_FLAGS_SEQVAL) {
2192 tcp_seq snd_una = be32toh(cpl->snd_una);
2193
2194 #ifdef INVARIANTS
2195 if (__predict_false(SEQ_LT(snd_una, tp->snd_una))) {
2196 log(LOG_ERR,
2197 "%s: unexpected seq# %x for TID %u, snd_una %x\n",
2198 __func__, snd_una, toep->tid, tp->snd_una);
2199 }
2200 #endif
2201
2202 if (tp->snd_una != snd_una) {
2203 tp->snd_una = snd_una;
2204 tp->ts_recent_age = tcp_ts_getticks();
2205 }
2206 }
2207
2208 #ifdef VERBOSE_TRACES
2209 CTR3(KTR_CXGBE, "%s: tid %d credits %u", __func__, tid, credits);
2210 #endif
2211 so = inp->inp_socket;
2212 txsd = &toep->txsd[toep->txsd_cidx];
2213 plen = 0;
2214 while (credits) {
2215 KASSERT(credits >= txsd->tx_credits,
2216 ("%s: too many (or partial) credits", __func__));
2217 credits -= txsd->tx_credits;
2218 toep->tx_credits += txsd->tx_credits;
2219 plen += txsd->plen;
2220 txsd++;
2221 toep->txsd_avail++;
2222 KASSERT(toep->txsd_avail <= toep->txsd_total,
2223 ("%s: txsd avail > total", __func__));
2224 if (__predict_false(++toep->txsd_cidx == toep->txsd_total)) {
2225 txsd = &toep->txsd[0];
2226 toep->txsd_cidx = 0;
2227 }
2228 }
2229
2230 if (toep->tx_credits == toep->tx_total) {
2231 toep->tx_nocompl = 0;
2232 toep->plen_nocompl = 0;
2233 }
2234
2235 if (toep->flags & TPF_TX_SUSPENDED &&
2236 toep->tx_credits >= toep->tx_total / 4) {
2237 #ifdef VERBOSE_TRACES
2238 CTR2(KTR_CXGBE, "%s: tid %d calling t4_push_frames", __func__,
2239 tid);
2240 #endif
2241 toep->flags &= ~TPF_TX_SUSPENDED;
2242 CURVNET_SET(toep->vnet);
2243 t4_push_data(sc, toep, plen);
2244 CURVNET_RESTORE();
2245 } else if (plen > 0) {
2246 struct sockbuf *sb = &so->so_snd;
2247 int sbu;
2248
2249 SOCKBUF_LOCK(sb);
2250 sbu = sbused(sb);
2251 if (ulp_mode(toep) == ULP_MODE_ISCSI ||
2252 ulp_mode(toep) == ULP_MODE_NVMET) {
2253 if (__predict_false(sbu > 0)) {
2254 /*
2255 * The data transmitted before the
2256 * tid's ULP mode changed to ISCSI is
2257 * still in so_snd. Incoming credits
2258 * should account for so_snd first.
2259 */
2260 sbdrop_locked(sb, min(sbu, plen));
2261 plen -= min(sbu, plen);
2262 }
2263 sowwakeup_locked(so); /* unlocks so_snd */
2264 rqdrop_locked(&toep->ulp_pdu_reclaimq, plen);
2265 } else {
2266 #ifdef VERBOSE_TRACES
2267 CTR3(KTR_CXGBE, "%s: tid %d dropped %d bytes", __func__,
2268 tid, plen);
2269 #endif
2270 sbdrop_locked(sb, plen);
2271 if (!TAILQ_EMPTY(&toep->aiotx_jobq))
2272 t4_aiotx_queue_toep(so, toep);
2273 sowwakeup_locked(so); /* unlocks so_snd */
2274 }
2275 SOCKBUF_UNLOCK_ASSERT(sb);
2276 }
2277
2278 INP_WUNLOCK(inp);
2279
2280 return (0);
2281 }
2282
2283 void
write_set_tcb_field(struct adapter * sc,void * dst,struct toepcb * toep,uint16_t word,uint64_t mask,uint64_t val,int reply,int cookie)2284 write_set_tcb_field(struct adapter *sc, void *dst, struct toepcb *toep,
2285 uint16_t word, uint64_t mask, uint64_t val, int reply, int cookie)
2286 {
2287 struct cpl_set_tcb_field *req = dst;
2288
2289 MPASS((cookie & ~M_COOKIE) == 0);
2290 if (reply) {
2291 MPASS(cookie != CPL_COOKIE_RESERVED);
2292 }
2293
2294 INIT_TP_WR_MIT_CPL(req, CPL_SET_TCB_FIELD, toep->tid);
2295 if (reply == 0) {
2296 req->reply_ctrl = htobe16(F_NO_REPLY);
2297 } else {
2298 const int qid = toep->ofld_rxq->iq.abs_id;
2299 if (chip_id(sc) >= CHELSIO_T7) {
2300 req->reply_ctrl = htobe16(V_T7_QUEUENO(qid) |
2301 V_T7_REPLY_CHAN(0) | V_NO_REPLY(0));
2302 } else {
2303 req->reply_ctrl = htobe16(V_QUEUENO(qid) |
2304 V_REPLY_CHAN(0) | V_NO_REPLY(0));
2305 }
2306 }
2307 req->word_cookie = htobe16(V_WORD(word) | V_COOKIE(cookie));
2308 req->mask = htobe64(mask);
2309 req->val = htobe64(val);
2310 }
2311
2312 void
t4_set_tcb_field(struct adapter * sc,struct sge_wrq * wrq,struct toepcb * toep,uint16_t word,uint64_t mask,uint64_t val,int reply,int cookie)2313 t4_set_tcb_field(struct adapter *sc, struct sge_wrq *wrq, struct toepcb *toep,
2314 uint16_t word, uint64_t mask, uint64_t val, int reply, int cookie)
2315 {
2316 struct wrqe *wr;
2317 struct ofld_tx_sdesc *txsd;
2318 const u_int len = sizeof(struct cpl_set_tcb_field);
2319
2320 wr = alloc_wrqe(len, wrq);
2321 if (wr == NULL) {
2322 /* XXX */
2323 panic("%s: allocation failure.", __func__);
2324 }
2325 write_set_tcb_field(sc, wrtod(wr), toep, word, mask, val, reply,
2326 cookie);
2327
2328 if (wrq->eq.type == EQ_OFLD) {
2329 txsd = &toep->txsd[toep->txsd_pidx];
2330 _Static_assert(howmany(len, 16) <= MAX_OFLD_TX_SDESC_CREDITS,
2331 "MAX_OFLD_TX_SDESC_CREDITS too small");
2332 txsd->tx_credits = howmany(len, 16);
2333 txsd->plen = 0;
2334 KASSERT(toep->tx_credits >= txsd->tx_credits &&
2335 toep->txsd_avail > 0,
2336 ("%s: not enough credits (%d)", __func__,
2337 toep->tx_credits));
2338 toep->tx_credits -= txsd->tx_credits;
2339 if (__predict_false(++toep->txsd_pidx == toep->txsd_total))
2340 toep->txsd_pidx = 0;
2341 toep->txsd_avail--;
2342 }
2343
2344 t4_wrq_tx(sc, wr);
2345 }
2346
2347 void
t4_init_cpl_io_handlers(void)2348 t4_init_cpl_io_handlers(void)
2349 {
2350
2351 t4_register_cpl_handler(CPL_PEER_CLOSE, do_peer_close);
2352 t4_register_cpl_handler(CPL_CLOSE_CON_RPL, do_close_con_rpl);
2353 t4_register_cpl_handler(CPL_ABORT_REQ_RSS, do_abort_req);
2354 t4_register_shared_cpl_handler(CPL_ABORT_RPL_RSS, do_abort_rpl,
2355 CPL_COOKIE_TOM);
2356 t4_register_cpl_handler(CPL_RX_DATA, do_rx_data);
2357 t4_register_shared_cpl_handler(CPL_FW4_ACK, do_fw4_ack, CPL_COOKIE_TOM);
2358 }
2359
2360 void
t4_uninit_cpl_io_handlers(void)2361 t4_uninit_cpl_io_handlers(void)
2362 {
2363
2364 t4_register_cpl_handler(CPL_PEER_CLOSE, NULL);
2365 t4_register_cpl_handler(CPL_CLOSE_CON_RPL, NULL);
2366 t4_register_cpl_handler(CPL_ABORT_REQ_RSS, NULL);
2367 t4_register_shared_cpl_handler(CPL_ABORT_RPL_RSS, NULL, CPL_COOKIE_TOM);
2368 t4_register_cpl_handler(CPL_RX_DATA, NULL);
2369 t4_register_shared_cpl_handler(CPL_FW4_ACK, NULL, CPL_COOKIE_TOM);
2370 }
2371
2372 /*
2373 * Use the 'backend1' field in AIO jobs to hold an error that should
2374 * be reported when the job is completed, the 'backend3' field to
2375 * store the amount of data sent by the AIO job so far, and the
2376 * 'backend4' field to hold a reference count on the job.
2377 *
2378 * Each unmapped mbuf holds a reference on the job as does the queue
2379 * so long as the job is queued.
2380 */
2381 #define aio_error backend1
2382 #define aio_sent backend3
2383 #define aio_refs backend4
2384
2385 #ifdef VERBOSE_TRACES
2386 static int
jobtotid(struct kaiocb * job)2387 jobtotid(struct kaiocb *job)
2388 {
2389 struct socket *so;
2390 struct tcpcb *tp;
2391 struct toepcb *toep;
2392
2393 so = job->fd_file->f_data;
2394 tp = sototcpcb(so);
2395 toep = tp->t_toe;
2396 return (toep->tid);
2397 }
2398 #endif
2399
2400 static void
aiotx_free_job(struct kaiocb * job)2401 aiotx_free_job(struct kaiocb *job)
2402 {
2403 long status;
2404 int error;
2405
2406 if (refcount_release(&job->aio_refs) == 0)
2407 return;
2408
2409 error = (intptr_t)job->aio_error;
2410 status = job->aio_sent;
2411 #ifdef VERBOSE_TRACES
2412 CTR5(KTR_CXGBE, "%s: tid %d completed %p len %ld, error %d", __func__,
2413 jobtotid(job), job, status, error);
2414 #endif
2415 if (error != 0 && status != 0)
2416 error = 0;
2417 if (error == ECANCELED)
2418 aio_cancel(job);
2419 else if (error)
2420 aio_complete(job, -1, error);
2421 else {
2422 job->msgsnd = 1;
2423 aio_complete(job, status, 0);
2424 }
2425 }
2426
2427 static void
aiotx_free_pgs(struct mbuf * m)2428 aiotx_free_pgs(struct mbuf *m)
2429 {
2430 struct kaiocb *job;
2431 vm_page_t pg;
2432
2433 M_ASSERTEXTPG(m);
2434 job = m->m_ext.ext_arg1;
2435 #ifdef VERBOSE_TRACES
2436 CTR3(KTR_CXGBE, "%s: completed %d bytes for tid %d", __func__,
2437 m->m_len, jobtotid(job));
2438 #endif
2439
2440 for (int i = 0; i < m->m_epg_npgs; i++) {
2441 pg = PHYS_TO_VM_PAGE(m->m_epg_pa[i]);
2442 vm_page_unwire(pg, PQ_ACTIVE);
2443 }
2444
2445 aiotx_free_job(job);
2446 }
2447
2448 /*
2449 * Allocate a chain of unmapped mbufs describing the next 'len' bytes
2450 * of an AIO job.
2451 */
2452 static struct mbuf *
alloc_aiotx_mbuf(struct kaiocb * job,int len)2453 alloc_aiotx_mbuf(struct kaiocb *job, int len)
2454 {
2455 struct vmspace *vm;
2456 vm_page_t pgs[MBUF_PEXT_MAX_PGS];
2457 struct mbuf *m, *top, *last;
2458 vm_map_t map;
2459 vm_offset_t start;
2460 int i, mlen, npages, pgoff;
2461
2462 KASSERT(job->aio_sent + len <= job->uaiocb.aio_nbytes,
2463 ("%s(%p, %d): request to send beyond end of buffer", __func__,
2464 job, len));
2465
2466 /*
2467 * The AIO subsystem will cancel and drain all requests before
2468 * permitting a process to exit or exec, so p_vmspace should
2469 * be stable here.
2470 */
2471 vm = job->userproc->p_vmspace;
2472 map = &vm->vm_map;
2473 start = (uintptr_t)job->uaiocb.aio_buf + job->aio_sent;
2474 pgoff = start & PAGE_MASK;
2475
2476 top = NULL;
2477 last = NULL;
2478 while (len > 0) {
2479 mlen = imin(len, MBUF_PEXT_MAX_PGS * PAGE_SIZE - pgoff);
2480 KASSERT(mlen == len || ((start + mlen) & PAGE_MASK) == 0,
2481 ("%s: next start (%#jx + %#x) is not page aligned",
2482 __func__, (uintmax_t)start, mlen));
2483
2484 npages = vm_fault_quick_hold_pages(map, start, mlen,
2485 VM_PROT_WRITE, pgs, nitems(pgs));
2486 if (npages < 0)
2487 break;
2488
2489 m = mb_alloc_ext_pgs(M_WAITOK, aiotx_free_pgs, M_RDONLY);
2490 m->m_epg_1st_off = pgoff;
2491 m->m_epg_npgs = npages;
2492 if (npages == 1) {
2493 KASSERT(mlen + pgoff <= PAGE_SIZE,
2494 ("%s: single page is too large (off %d len %d)",
2495 __func__, pgoff, mlen));
2496 m->m_epg_last_len = mlen;
2497 } else {
2498 m->m_epg_last_len = mlen - (PAGE_SIZE - pgoff) -
2499 (npages - 2) * PAGE_SIZE;
2500 }
2501 for (i = 0; i < npages; i++)
2502 m->m_epg_pa[i] = VM_PAGE_TO_PHYS(pgs[i]);
2503
2504 m->m_len = mlen;
2505 m->m_ext.ext_size = npages * PAGE_SIZE;
2506 m->m_ext.ext_arg1 = job;
2507 refcount_acquire(&job->aio_refs);
2508
2509 #ifdef VERBOSE_TRACES
2510 CTR5(KTR_CXGBE, "%s: tid %d, new mbuf %p for job %p, npages %d",
2511 __func__, jobtotid(job), m, job, npages);
2512 #endif
2513
2514 if (top == NULL)
2515 top = m;
2516 else
2517 last->m_next = m;
2518 last = m;
2519
2520 len -= mlen;
2521 start += mlen;
2522 pgoff = 0;
2523 }
2524
2525 return (top);
2526 }
2527
2528 static void
t4_aiotx_process_job(struct toepcb * toep,struct socket * so,struct kaiocb * job)2529 t4_aiotx_process_job(struct toepcb *toep, struct socket *so, struct kaiocb *job)
2530 {
2531 struct sockbuf *sb;
2532 struct inpcb *inp;
2533 struct tcpcb *tp;
2534 struct mbuf *m;
2535 u_int sent;
2536 int error, len;
2537 bool moretocome, sendmore;
2538
2539 sb = &so->so_snd;
2540 SOCKBUF_UNLOCK(sb);
2541 m = NULL;
2542
2543 #ifdef MAC
2544 error = mac_socket_check_send(job->fd_file->f_cred, so);
2545 if (error != 0)
2546 goto out;
2547 #endif
2548
2549 /* Inline sosend_generic(). */
2550
2551 error = SOCK_IO_SEND_LOCK(so, SBL_WAIT);
2552 MPASS(error == 0);
2553
2554 sendanother:
2555 SOCKBUF_LOCK(sb);
2556 if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2557 SOCKBUF_UNLOCK(sb);
2558 SOCK_IO_SEND_UNLOCK(so);
2559 if ((so->so_options & SO_NOSIGPIPE) == 0) {
2560 PROC_LOCK(job->userproc);
2561 kern_psignal(job->userproc, SIGPIPE);
2562 PROC_UNLOCK(job->userproc);
2563 }
2564 error = EPIPE;
2565 goto out;
2566 }
2567 if (so->so_error) {
2568 error = so->so_error;
2569 so->so_error = 0;
2570 SOCKBUF_UNLOCK(sb);
2571 SOCK_IO_SEND_UNLOCK(so);
2572 goto out;
2573 }
2574 if ((so->so_state & SS_ISCONNECTED) == 0) {
2575 SOCKBUF_UNLOCK(sb);
2576 SOCK_IO_SEND_UNLOCK(so);
2577 error = ENOTCONN;
2578 goto out;
2579 }
2580 if (sbspace(sb) < sb->sb_lowat) {
2581 MPASS(job->aio_sent == 0 || !(so->so_state & SS_NBIO));
2582
2583 /*
2584 * Don't block if there is too little room in the socket
2585 * buffer. Instead, requeue the request.
2586 */
2587 if (!aio_set_cancel_function(job, t4_aiotx_cancel)) {
2588 SOCKBUF_UNLOCK(sb);
2589 SOCK_IO_SEND_UNLOCK(so);
2590 error = ECANCELED;
2591 goto out;
2592 }
2593 TAILQ_INSERT_HEAD(&toep->aiotx_jobq, job, list);
2594 SOCKBUF_UNLOCK(sb);
2595 SOCK_IO_SEND_UNLOCK(so);
2596 goto out;
2597 }
2598
2599 /*
2600 * Write as much data as the socket permits, but no more than a
2601 * a single sndbuf at a time.
2602 */
2603 len = sbspace(sb);
2604 if (len > job->uaiocb.aio_nbytes - job->aio_sent) {
2605 len = job->uaiocb.aio_nbytes - job->aio_sent;
2606 moretocome = false;
2607 } else
2608 moretocome = true;
2609 if (len > toep->params.sndbuf) {
2610 len = toep->params.sndbuf;
2611 sendmore = true;
2612 } else
2613 sendmore = false;
2614
2615 if (!TAILQ_EMPTY(&toep->aiotx_jobq))
2616 moretocome = true;
2617 SOCKBUF_UNLOCK(sb);
2618 MPASS(len != 0);
2619
2620 m = alloc_aiotx_mbuf(job, len);
2621 if (m == NULL) {
2622 SOCK_IO_SEND_UNLOCK(so);
2623 error = EFAULT;
2624 goto out;
2625 }
2626
2627 /* Inlined tcp_usr_send(). */
2628
2629 inp = toep->inp;
2630 INP_WLOCK(inp);
2631 if (inp->inp_flags & INP_DROPPED) {
2632 INP_WUNLOCK(inp);
2633 SOCK_IO_SEND_UNLOCK(so);
2634 error = ECONNRESET;
2635 goto out;
2636 }
2637
2638 sent = m_length(m, NULL);
2639 job->aio_sent += sent;
2640 counter_u64_add(toep->ofld_txq->tx_aio_octets, sent);
2641
2642 sbappendstream(sb, m, 0);
2643 m = NULL;
2644
2645 if (!(inp->inp_flags & INP_DROPPED)) {
2646 tp = intotcpcb(inp);
2647 if (moretocome)
2648 tp->t_flags |= TF_MORETOCOME;
2649 error = tcp_output(tp);
2650 if (error < 0) {
2651 INP_UNLOCK_ASSERT(inp);
2652 SOCK_IO_SEND_UNLOCK(so);
2653 error = -error;
2654 goto out;
2655 }
2656 if (moretocome)
2657 tp->t_flags &= ~TF_MORETOCOME;
2658 }
2659
2660 INP_WUNLOCK(inp);
2661 if (sendmore)
2662 goto sendanother;
2663 SOCK_IO_SEND_UNLOCK(so);
2664
2665 if (error)
2666 goto out;
2667
2668 /*
2669 * If this is a blocking socket and the request has not been
2670 * fully completed, requeue it until the socket is ready
2671 * again.
2672 */
2673 if (job->aio_sent < job->uaiocb.aio_nbytes &&
2674 !(so->so_state & SS_NBIO)) {
2675 SOCKBUF_LOCK(sb);
2676 if (!aio_set_cancel_function(job, t4_aiotx_cancel)) {
2677 SOCKBUF_UNLOCK(sb);
2678 error = ECANCELED;
2679 goto out;
2680 }
2681 TAILQ_INSERT_HEAD(&toep->aiotx_jobq, job, list);
2682 return;
2683 }
2684
2685 /*
2686 * If the request will not be requeued, drop the queue's
2687 * reference to the job. Any mbufs in flight should still
2688 * hold a reference, but this drops the reference that the
2689 * queue owns while it is waiting to queue mbufs to the
2690 * socket.
2691 */
2692 aiotx_free_job(job);
2693 counter_u64_add(toep->ofld_txq->tx_aio_jobs, 1);
2694
2695 out:
2696 if (error) {
2697 job->aio_error = (void *)(intptr_t)error;
2698 aiotx_free_job(job);
2699 }
2700 m_freem(m);
2701 SOCKBUF_LOCK(sb);
2702 }
2703
2704 static void
t4_aiotx_task(void * context,int pending)2705 t4_aiotx_task(void *context, int pending)
2706 {
2707 struct toepcb *toep = context;
2708 struct socket *so;
2709 struct kaiocb *job;
2710 struct epoch_tracker et;
2711
2712 so = toep->aiotx_so;
2713 CURVNET_SET(toep->vnet);
2714 NET_EPOCH_ENTER(et);
2715 SOCKBUF_LOCK(&so->so_snd);
2716 while (!TAILQ_EMPTY(&toep->aiotx_jobq) && sowriteable(so)) {
2717 job = TAILQ_FIRST(&toep->aiotx_jobq);
2718 TAILQ_REMOVE(&toep->aiotx_jobq, job, list);
2719 if (!aio_clear_cancel_function(job))
2720 continue;
2721
2722 t4_aiotx_process_job(toep, so, job);
2723 }
2724 toep->aiotx_so = NULL;
2725 SOCKBUF_UNLOCK(&so->so_snd);
2726 NET_EPOCH_EXIT(et);
2727
2728 free_toepcb(toep);
2729 sorele(so);
2730 CURVNET_RESTORE();
2731 }
2732
2733 static void
t4_aiotx_queue_toep(struct socket * so,struct toepcb * toep)2734 t4_aiotx_queue_toep(struct socket *so, struct toepcb *toep)
2735 {
2736
2737 SOCKBUF_LOCK_ASSERT(&toep->inp->inp_socket->so_snd);
2738 #ifdef VERBOSE_TRACES
2739 CTR3(KTR_CXGBE, "%s: queueing aiotx task for tid %d, active = %s",
2740 __func__, toep->tid, toep->aiotx_so != NULL ? "true" : "false");
2741 #endif
2742 if (toep->aiotx_so != NULL)
2743 return;
2744 soref(so);
2745 toep->aiotx_so = so;
2746 hold_toepcb(toep);
2747 soaio_enqueue(&toep->aiotx_task);
2748 }
2749
2750 static void
t4_aiotx_cancel(struct kaiocb * job)2751 t4_aiotx_cancel(struct kaiocb *job)
2752 {
2753 struct socket *so;
2754 struct sockbuf *sb;
2755 struct tcpcb *tp;
2756 struct toepcb *toep;
2757
2758 so = job->fd_file->f_data;
2759 tp = sototcpcb(so);
2760 toep = tp->t_toe;
2761 MPASS(job->uaiocb.aio_lio_opcode == LIO_WRITE);
2762 sb = &so->so_snd;
2763
2764 SOCKBUF_LOCK(sb);
2765 if (!aio_cancel_cleared(job))
2766 TAILQ_REMOVE(&toep->aiotx_jobq, job, list);
2767 SOCKBUF_UNLOCK(sb);
2768
2769 job->aio_error = (void *)(intptr_t)ECANCELED;
2770 aiotx_free_job(job);
2771 }
2772
2773 int
t4_aio_queue_aiotx(struct socket * so,struct kaiocb * job)2774 t4_aio_queue_aiotx(struct socket *so, struct kaiocb *job)
2775 {
2776 struct tcpcb *tp = sototcpcb(so);
2777 struct toepcb *toep = tp->t_toe;
2778 struct adapter *sc = td_adapter(toep->td);
2779
2780 /* This only handles writes. */
2781 if (job->uaiocb.aio_lio_opcode != LIO_WRITE)
2782 return (EOPNOTSUPP);
2783
2784 if (!sc->tt.tx_zcopy)
2785 return (EOPNOTSUPP);
2786
2787 if (tls_tx_key(toep))
2788 return (EOPNOTSUPP);
2789
2790 SOCKBUF_LOCK(&so->so_snd);
2791 #ifdef VERBOSE_TRACES
2792 CTR3(KTR_CXGBE, "%s: queueing %p for tid %u", __func__, job, toep->tid);
2793 #endif
2794 if (!aio_set_cancel_function(job, t4_aiotx_cancel))
2795 panic("new job was cancelled");
2796 refcount_init(&job->aio_refs, 1);
2797 TAILQ_INSERT_TAIL(&toep->aiotx_jobq, job, list);
2798 if (sowriteable(so))
2799 t4_aiotx_queue_toep(so, toep);
2800 SOCKBUF_UNLOCK(&so->so_snd);
2801 return (0);
2802 }
2803
2804 void
aiotx_init_toep(struct toepcb * toep)2805 aiotx_init_toep(struct toepcb *toep)
2806 {
2807
2808 TAILQ_INIT(&toep->aiotx_jobq);
2809 TASK_INIT(&toep->aiotx_task, 0, t4_aiotx_task, toep);
2810 }
2811 #endif
2812