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