]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/cxgbe/tom/t4_cpl_io.c
MFC 292736:
[FreeBSD/stable/10.git] / sys / dev / cxgbe / tom / t4_cpl_io.c
1 /*-
2  * Copyright (c) 2012, 2015 Chelsio Communications, Inc.
3  * All rights reserved.
4  * Written by: Navdeep Parhar <np@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include "opt_inet.h"
32
33 #ifdef TCP_OFFLOAD
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/kernel.h>
37 #include <sys/ktr.h>
38 #include <sys/module.h>
39 #include <sys/protosw.h>
40 #include <sys/domain.h>
41 #include <sys/socket.h>
42 #include <sys/socketvar.h>
43 #include <sys/sglist.h>
44 #include <netinet/in.h>
45 #include <netinet/in_pcb.h>
46 #include <netinet/ip.h>
47 #include <netinet/ip6.h>
48 #include <netinet/tcp_var.h>
49 #define TCPSTATES
50 #include <netinet/tcp_fsm.h>
51 #include <netinet/tcp_seq.h>
52 #include <netinet/toecore.h>
53
54 #include "common/common.h"
55 #include "common/t4_msg.h"
56 #include "common/t4_regs.h"
57 #include "common/t4_tcb.h"
58 #include "tom/t4_tom_l2t.h"
59 #include "tom/t4_tom.h"
60
61 VNET_DECLARE(int, tcp_do_autosndbuf);
62 #define V_tcp_do_autosndbuf VNET(tcp_do_autosndbuf)
63 VNET_DECLARE(int, tcp_autosndbuf_inc);
64 #define V_tcp_autosndbuf_inc VNET(tcp_autosndbuf_inc)
65 VNET_DECLARE(int, tcp_autosndbuf_max);
66 #define V_tcp_autosndbuf_max VNET(tcp_autosndbuf_max)
67 VNET_DECLARE(int, tcp_do_autorcvbuf);
68 #define V_tcp_do_autorcvbuf VNET(tcp_do_autorcvbuf)
69 VNET_DECLARE(int, tcp_autorcvbuf_inc);
70 #define V_tcp_autorcvbuf_inc VNET(tcp_autorcvbuf_inc)
71 VNET_DECLARE(int, tcp_autorcvbuf_max);
72 #define V_tcp_autorcvbuf_max VNET(tcp_autorcvbuf_max)
73
74 static inline struct mbuf *
75 mbufq_dequeue(struct mbufq *q)
76 {
77         struct mbuf *m;
78
79         m = q->head;
80         if (m) {
81                 if (q->tail == m)
82                         q->tail = NULL;
83                 q->head = m->m_nextpkt;
84                 m->m_nextpkt = NULL;
85         }
86         return (m);
87 }
88
89 static inline void
90 mbufq_enqueue(struct mbufq *q, struct mbuf *m)
91 {
92
93         m->m_nextpkt = NULL;
94         if (q->tail)
95                 q->tail->m_nextpkt = m;
96         else
97                 q->head = m;
98         q->tail = m;
99 }
100
101 static inline struct mbuf *
102 mbufq_first(const struct mbufq *q)
103 {
104
105         return (q->head);
106 }
107
108 void
109 send_flowc_wr(struct toepcb *toep, struct flowc_tx_params *ftxp)
110 {
111         struct wrqe *wr;
112         struct fw_flowc_wr *flowc;
113         unsigned int nparams = ftxp ? 8 : 6, flowclen;
114         struct vi_info *vi = toep->vi;
115         struct port_info *pi = vi->pi;
116         struct adapter *sc = pi->adapter;
117         unsigned int pfvf = G_FW_VIID_PFN(vi->viid) << S_FW_VIID_PFN;
118         struct ofld_tx_sdesc *txsd = &toep->txsd[toep->txsd_pidx];
119
120         KASSERT(!(toep->flags & TPF_FLOWC_WR_SENT),
121             ("%s: flowc for tid %u sent already", __func__, toep->tid));
122
123         flowclen = sizeof(*flowc) + nparams * sizeof(struct fw_flowc_mnemval);
124
125         wr = alloc_wrqe(roundup2(flowclen, 16), toep->ofld_txq);
126         if (wr == NULL) {
127                 /* XXX */
128                 panic("%s: allocation failure.", __func__);
129         }
130         flowc = wrtod(wr);
131         memset(flowc, 0, wr->wr_len);
132
133         flowc->op_to_nparams = htobe32(V_FW_WR_OP(FW_FLOWC_WR) |
134             V_FW_FLOWC_WR_NPARAMS(nparams));
135         flowc->flowid_len16 = htonl(V_FW_WR_LEN16(howmany(flowclen, 16)) |
136             V_FW_WR_FLOWID(toep->tid));
137
138         flowc->mnemval[0].mnemonic = FW_FLOWC_MNEM_PFNVFN;
139         flowc->mnemval[0].val = htobe32(pfvf);
140         flowc->mnemval[1].mnemonic = FW_FLOWC_MNEM_CH;
141         flowc->mnemval[1].val = htobe32(pi->tx_chan);
142         flowc->mnemval[2].mnemonic = FW_FLOWC_MNEM_PORT;
143         flowc->mnemval[2].val = htobe32(pi->tx_chan);
144         flowc->mnemval[3].mnemonic = FW_FLOWC_MNEM_IQID;
145         flowc->mnemval[3].val = htobe32(toep->ofld_rxq->iq.abs_id);
146         if (ftxp) {
147                 uint32_t sndbuf = min(ftxp->snd_space, sc->tt.sndbuf);
148
149                 flowc->mnemval[4].mnemonic = FW_FLOWC_MNEM_SNDNXT;
150                 flowc->mnemval[4].val = htobe32(ftxp->snd_nxt);
151                 flowc->mnemval[5].mnemonic = FW_FLOWC_MNEM_RCVNXT;
152                 flowc->mnemval[5].val = htobe32(ftxp->rcv_nxt);
153                 flowc->mnemval[6].mnemonic = FW_FLOWC_MNEM_SNDBUF;
154                 flowc->mnemval[6].val = htobe32(sndbuf);
155                 flowc->mnemval[7].mnemonic = FW_FLOWC_MNEM_MSS;
156                 flowc->mnemval[7].val = htobe32(ftxp->mss);
157
158                 CTR6(KTR_CXGBE,
159                     "%s: tid %u, mss %u, sndbuf %u, snd_nxt 0x%x, rcv_nxt 0x%x",
160                     __func__, toep->tid, ftxp->mss, sndbuf, ftxp->snd_nxt,
161                     ftxp->rcv_nxt);
162         } else {
163                 flowc->mnemval[4].mnemonic = FW_FLOWC_MNEM_SNDBUF;
164                 flowc->mnemval[4].val = htobe32(512);
165                 flowc->mnemval[5].mnemonic = FW_FLOWC_MNEM_MSS;
166                 flowc->mnemval[5].val = htobe32(512);
167
168                 CTR2(KTR_CXGBE, "%s: tid %u", __func__, toep->tid);
169         }
170
171         txsd->tx_credits = howmany(flowclen, 16);
172         txsd->plen = 0;
173         KASSERT(toep->tx_credits >= txsd->tx_credits && toep->txsd_avail > 0,
174             ("%s: not enough credits (%d)", __func__, toep->tx_credits));
175         toep->tx_credits -= txsd->tx_credits;
176         if (__predict_false(++toep->txsd_pidx == toep->txsd_total))
177                 toep->txsd_pidx = 0;
178         toep->txsd_avail--;
179
180         toep->flags |= TPF_FLOWC_WR_SENT;
181         t4_wrq_tx(sc, wr);
182 }
183
184 void
185 send_reset(struct adapter *sc, struct toepcb *toep, uint32_t snd_nxt)
186 {
187         struct wrqe *wr;
188         struct cpl_abort_req *req;
189         int tid = toep->tid;
190         struct inpcb *inp = toep->inp;
191         struct tcpcb *tp = intotcpcb(inp);      /* don't use if INP_DROPPED */
192
193         INP_WLOCK_ASSERT(inp);
194
195         CTR6(KTR_CXGBE, "%s: tid %d (%s), toep_flags 0x%x, inp_flags 0x%x%s",
196             __func__, toep->tid,
197             inp->inp_flags & INP_DROPPED ? "inp dropped" :
198             tcpstates[tp->t_state],
199             toep->flags, inp->inp_flags,
200             toep->flags & TPF_ABORT_SHUTDOWN ?
201             " (abort already in progress)" : "");
202
203         if (toep->flags & TPF_ABORT_SHUTDOWN)
204                 return; /* abort already in progress */
205
206         toep->flags |= TPF_ABORT_SHUTDOWN;
207
208         KASSERT(toep->flags & TPF_FLOWC_WR_SENT,
209             ("%s: flowc_wr not sent for tid %d.", __func__, tid));
210
211         wr = alloc_wrqe(sizeof(*req), toep->ofld_txq);
212         if (wr == NULL) {
213                 /* XXX */
214                 panic("%s: allocation failure.", __func__);
215         }
216         req = wrtod(wr);
217
218         INIT_TP_WR_MIT_CPL(req, CPL_ABORT_REQ, tid);
219         if (inp->inp_flags & INP_DROPPED)
220                 req->rsvd0 = htobe32(snd_nxt);
221         else
222                 req->rsvd0 = htobe32(tp->snd_nxt);
223         req->rsvd1 = !(toep->flags & TPF_TX_DATA_SENT);
224         req->cmd = CPL_ABORT_SEND_RST;
225
226         /*
227          * XXX: What's the correct way to tell that the inp hasn't been detached
228          * from its socket?  Should I even be flushing the snd buffer here?
229          */
230         if ((inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT)) == 0) {
231                 struct socket *so = inp->inp_socket;
232
233                 if (so != NULL) /* because I'm not sure.  See comment above */
234                         sbflush(&so->so_snd);
235         }
236
237         t4_l2t_send(sc, wr, toep->l2te);
238 }
239
240 /*
241  * Called when a connection is established to translate the TCP options
242  * reported by HW to FreeBSD's native format.
243  */
244 static void
245 assign_rxopt(struct tcpcb *tp, unsigned int opt)
246 {
247         struct toepcb *toep = tp->t_toe;
248         struct inpcb *inp = tp->t_inpcb;
249         struct adapter *sc = td_adapter(toep->td);
250         int n;
251
252         INP_LOCK_ASSERT(inp);
253
254         if (inp->inp_inc.inc_flags & INC_ISIPV6)
255                 n = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
256         else
257                 n = sizeof(struct ip) + sizeof(struct tcphdr);
258         tp->t_maxseg = tp->t_maxopd = sc->params.mtus[G_TCPOPT_MSS(opt)] - n;
259
260         CTR4(KTR_CXGBE, "%s: tid %d, mtu_idx %u (%u)", __func__, toep->tid,
261             G_TCPOPT_MSS(opt), sc->params.mtus[G_TCPOPT_MSS(opt)]);
262
263         if (G_TCPOPT_TSTAMP(opt)) {
264                 tp->t_flags |= TF_RCVD_TSTMP;   /* timestamps ok */
265                 tp->ts_recent = 0;              /* hmmm */
266                 tp->ts_recent_age = tcp_ts_getticks();
267                 tp->t_maxseg -= TCPOLEN_TSTAMP_APPA;
268         }
269
270         if (G_TCPOPT_SACK(opt))
271                 tp->t_flags |= TF_SACK_PERMIT;  /* should already be set */
272         else
273                 tp->t_flags &= ~TF_SACK_PERMIT; /* sack disallowed by peer */
274
275         if (G_TCPOPT_WSCALE_OK(opt))
276                 tp->t_flags |= TF_RCVD_SCALE;
277
278         /* Doing window scaling? */
279         if ((tp->t_flags & (TF_RCVD_SCALE | TF_REQ_SCALE)) ==
280             (TF_RCVD_SCALE | TF_REQ_SCALE)) {
281                 tp->rcv_scale = tp->request_r_scale;
282                 tp->snd_scale = G_TCPOPT_SND_WSCALE(opt);
283         }
284 }
285
286 /*
287  * Completes some final bits of initialization for just established connections
288  * and changes their state to TCPS_ESTABLISHED.
289  *
290  * The ISNs are from after the exchange of SYNs.  i.e., the true ISN + 1.
291  */
292 void
293 make_established(struct toepcb *toep, uint32_t snd_isn, uint32_t rcv_isn,
294     uint16_t opt)
295 {
296         struct inpcb *inp = toep->inp;
297         struct socket *so = inp->inp_socket;
298         struct tcpcb *tp = intotcpcb(inp);
299         long bufsize;
300         uint32_t iss = be32toh(snd_isn) - 1;    /* true ISS */
301         uint32_t irs = be32toh(rcv_isn) - 1;    /* true IRS */
302         uint16_t tcpopt = be16toh(opt);
303         struct flowc_tx_params ftxp;
304
305         CURVNET_SET(so->so_vnet);
306         INP_WLOCK_ASSERT(inp);
307         KASSERT(tp->t_state == TCPS_SYN_SENT ||
308             tp->t_state == TCPS_SYN_RECEIVED,
309             ("%s: TCP state %s", __func__, tcpstates[tp->t_state]));
310
311         CTR4(KTR_CXGBE, "%s: tid %d, toep %p, inp %p",
312             __func__, toep->tid, toep, inp);
313
314         tp->t_state = TCPS_ESTABLISHED;
315         tp->t_starttime = ticks;
316         TCPSTAT_INC(tcps_connects);
317
318         tp->irs = irs;
319         tcp_rcvseqinit(tp);
320         tp->rcv_wnd = toep->rx_credits << 10;
321         tp->rcv_adv += tp->rcv_wnd;
322         tp->last_ack_sent = tp->rcv_nxt;
323
324         /*
325          * If we were unable to send all rx credits via opt0, save the remainder
326          * in rx_credits so that they can be handed over with the next credit
327          * update.
328          */
329         SOCKBUF_LOCK(&so->so_rcv);
330         bufsize = select_rcv_wnd(so);
331         SOCKBUF_UNLOCK(&so->so_rcv);
332         toep->rx_credits = bufsize - tp->rcv_wnd;
333
334         tp->iss = iss;
335         tcp_sendseqinit(tp);
336         tp->snd_una = iss + 1;
337         tp->snd_nxt = iss + 1;
338         tp->snd_max = iss + 1;
339
340         assign_rxopt(tp, tcpopt);
341
342         SOCKBUF_LOCK(&so->so_snd);
343         if (so->so_snd.sb_flags & SB_AUTOSIZE && V_tcp_do_autosndbuf)
344                 bufsize = V_tcp_autosndbuf_max;
345         else
346                 bufsize = sbspace(&so->so_snd);
347         SOCKBUF_UNLOCK(&so->so_snd);
348
349         ftxp.snd_nxt = tp->snd_nxt;
350         ftxp.rcv_nxt = tp->rcv_nxt;
351         ftxp.snd_space = bufsize;
352         ftxp.mss = tp->t_maxseg;
353         send_flowc_wr(toep, &ftxp);
354
355         soisconnected(so);
356         CURVNET_RESTORE();
357 }
358
359 static int
360 send_rx_credits(struct adapter *sc, struct toepcb *toep, int credits)
361 {
362         struct wrqe *wr;
363         struct cpl_rx_data_ack *req;
364         uint32_t dack = F_RX_DACK_CHANGE | V_RX_DACK_MODE(1);
365
366         KASSERT(credits >= 0, ("%s: %d credits", __func__, credits));
367
368         wr = alloc_wrqe(sizeof(*req), toep->ctrlq);
369         if (wr == NULL)
370                 return (0);
371         req = wrtod(wr);
372
373         INIT_TP_WR_MIT_CPL(req, CPL_RX_DATA_ACK, toep->tid);
374         req->credit_dack = htobe32(dack | V_RX_CREDITS(credits));
375
376         t4_wrq_tx(sc, wr);
377         return (credits);
378 }
379
380 void
381 t4_rcvd(struct toedev *tod, struct tcpcb *tp)
382 {
383         struct adapter *sc = tod->tod_softc;
384         struct inpcb *inp = tp->t_inpcb;
385         struct socket *so = inp->inp_socket;
386         struct sockbuf *sb = &so->so_rcv;
387         struct toepcb *toep = tp->t_toe;
388         int credits;
389
390         INP_WLOCK_ASSERT(inp);
391
392         SOCKBUF_LOCK(sb);
393         KASSERT(toep->sb_cc >= sb->sb_cc,
394             ("%s: sb %p has more data (%d) than last time (%d).",
395             __func__, sb, sb->sb_cc, toep->sb_cc));
396         toep->rx_credits += toep->sb_cc - sb->sb_cc;
397         toep->sb_cc = sb->sb_cc;
398
399         if (toep->rx_credits > 0 &&
400             (tp->rcv_wnd <= 32 * 1024 || toep->rx_credits >= 64 * 1024 ||
401             (toep->rx_credits >= 16 * 1024 && tp->rcv_wnd <= 128 * 1024) ||
402             toep->sb_cc + tp->rcv_wnd < sb->sb_lowat)) {
403
404                 credits = send_rx_credits(sc, toep, toep->rx_credits);
405                 toep->rx_credits -= credits;
406                 tp->rcv_wnd += credits;
407                 tp->rcv_adv += credits;
408         }
409         SOCKBUF_UNLOCK(sb);
410 }
411
412 /*
413  * Close a connection by sending a CPL_CLOSE_CON_REQ message.
414  */
415 static int
416 close_conn(struct adapter *sc, struct toepcb *toep)
417 {
418         struct wrqe *wr;
419         struct cpl_close_con_req *req;
420         unsigned int tid = toep->tid;
421
422         CTR3(KTR_CXGBE, "%s: tid %u%s", __func__, toep->tid,
423             toep->flags & TPF_FIN_SENT ? ", IGNORED" : "");
424
425         if (toep->flags & TPF_FIN_SENT)
426                 return (0);
427
428         KASSERT(toep->flags & TPF_FLOWC_WR_SENT,
429             ("%s: flowc_wr not sent for tid %u.", __func__, tid));
430
431         wr = alloc_wrqe(sizeof(*req), toep->ofld_txq);
432         if (wr == NULL) {
433                 /* XXX */
434                 panic("%s: allocation failure.", __func__);
435         }
436         req = wrtod(wr);
437
438         req->wr.wr_hi = htonl(V_FW_WR_OP(FW_TP_WR) |
439             V_FW_WR_IMMDLEN(sizeof(*req) - sizeof(req->wr)));
440         req->wr.wr_mid = htonl(V_FW_WR_LEN16(howmany(sizeof(*req), 16)) |
441             V_FW_WR_FLOWID(tid));
442         req->wr.wr_lo = cpu_to_be64(0);
443         OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_CON_REQ, tid));
444         req->rsvd = 0;
445
446         toep->flags |= TPF_FIN_SENT;
447         toep->flags &= ~TPF_SEND_FIN;
448         t4_l2t_send(sc, wr, toep->l2te);
449
450         return (0);
451 }
452
453 #define MAX_OFLD_TX_CREDITS (SGE_MAX_WR_LEN / 16)
454 #define MIN_OFLD_TX_CREDITS (howmany(sizeof(struct fw_ofld_tx_data_wr) + 1, 16))
455
456 /* Maximum amount of immediate data we could stuff in a WR */
457 static inline int
458 max_imm_payload(int tx_credits)
459 {
460         const int n = 2;        /* Use only up to 2 desc for imm. data WR */
461
462         KASSERT(tx_credits >= 0 &&
463                 tx_credits <= MAX_OFLD_TX_CREDITS,
464                 ("%s: %d credits", __func__, tx_credits));
465
466         if (tx_credits < MIN_OFLD_TX_CREDITS)
467                 return (0);
468
469         if (tx_credits >= (n * EQ_ESIZE) / 16)
470                 return ((n * EQ_ESIZE) - sizeof(struct fw_ofld_tx_data_wr));
471         else
472                 return (tx_credits * 16 - sizeof(struct fw_ofld_tx_data_wr));
473 }
474
475 /* Maximum number of SGL entries we could stuff in a WR */
476 static inline int
477 max_dsgl_nsegs(int tx_credits)
478 {
479         int nseg = 1;   /* ulptx_sgl has room for 1, rest ulp_tx_sge_pair */
480         int sge_pair_credits = tx_credits - MIN_OFLD_TX_CREDITS;
481
482         KASSERT(tx_credits >= 0 &&
483                 tx_credits <= MAX_OFLD_TX_CREDITS,
484                 ("%s: %d credits", __func__, tx_credits));
485
486         if (tx_credits < MIN_OFLD_TX_CREDITS)
487                 return (0);
488
489         nseg += 2 * (sge_pair_credits * 16 / 24);
490         if ((sge_pair_credits * 16) % 24 == 16)
491                 nseg++;
492
493         return (nseg);
494 }
495
496 static inline void
497 write_tx_wr(void *dst, struct toepcb *toep, unsigned int immdlen,
498     unsigned int plen, uint8_t credits, int shove, int ulp_submode, int txalign)
499 {
500         struct fw_ofld_tx_data_wr *txwr = dst;
501
502         txwr->op_to_immdlen = htobe32(V_WR_OP(FW_OFLD_TX_DATA_WR) |
503             V_FW_WR_IMMDLEN(immdlen));
504         txwr->flowid_len16 = htobe32(V_FW_WR_FLOWID(toep->tid) |
505             V_FW_WR_LEN16(credits));
506         txwr->lsodisable_to_flags = htobe32(V_TX_ULP_MODE(toep->ulp_mode) |
507             V_TX_ULP_SUBMODE(ulp_submode) | V_TX_URG(0) | V_TX_SHOVE(shove));
508         txwr->plen = htobe32(plen);
509
510         if (txalign > 0) {
511                 struct tcpcb *tp = intotcpcb(toep->inp);
512
513                 if (plen < 2 * tp->t_maxseg || is_10G_port(toep->vi->pi))
514                         txwr->lsodisable_to_flags |=
515                             htobe32(F_FW_OFLD_TX_DATA_WR_LSODISABLE);
516                 else
517                         txwr->lsodisable_to_flags |=
518                             htobe32(F_FW_OFLD_TX_DATA_WR_ALIGNPLD |
519                                 (tp->t_flags & TF_NODELAY ? 0 :
520                                 F_FW_OFLD_TX_DATA_WR_ALIGNPLDSHOVE));
521         }
522 }
523
524 /*
525  * Generate a DSGL from a starting mbuf.  The total number of segments and the
526  * maximum segments in any one mbuf are provided.
527  */
528 static void
529 write_tx_sgl(void *dst, struct mbuf *start, struct mbuf *stop, int nsegs, int n)
530 {
531         struct mbuf *m;
532         struct ulptx_sgl *usgl = dst;
533         int i, j, rc;
534         struct sglist sg;
535         struct sglist_seg segs[n];
536
537         KASSERT(nsegs > 0, ("%s: nsegs 0", __func__));
538
539         sglist_init(&sg, n, segs);
540         usgl->cmd_nsge = htobe32(V_ULPTX_CMD(ULP_TX_SC_DSGL) |
541             V_ULPTX_NSGE(nsegs));
542
543         i = -1;
544         for (m = start; m != stop; m = m->m_next) {
545                 rc = sglist_append(&sg, mtod(m, void *), m->m_len);
546                 if (__predict_false(rc != 0))
547                         panic("%s: sglist_append %d", __func__, rc);
548
549                 for (j = 0; j < sg.sg_nseg; i++, j++) {
550                         if (i < 0) {
551                                 usgl->len0 = htobe32(segs[j].ss_len);
552                                 usgl->addr0 = htobe64(segs[j].ss_paddr);
553                         } else {
554                                 usgl->sge[i / 2].len[i & 1] =
555                                     htobe32(segs[j].ss_len);
556                                 usgl->sge[i / 2].addr[i & 1] =
557                                     htobe64(segs[j].ss_paddr);
558                         }
559 #ifdef INVARIANTS
560                         nsegs--;
561 #endif
562                 }
563                 sglist_reset(&sg);
564         }
565         if (i & 1)
566                 usgl->sge[i / 2].len[1] = htobe32(0);
567         KASSERT(nsegs == 0, ("%s: nsegs %d, start %p, stop %p",
568             __func__, nsegs, start, stop));
569 }
570
571 /*
572  * Max number of SGL entries an offload tx work request can have.  This is 41
573  * (1 + 40) for a full 512B work request.
574  * fw_ofld_tx_data_wr(16B) + ulptx_sgl(16B, 1) + ulptx_sge_pair(480B, 40)
575  */
576 #define OFLD_SGL_LEN (41)
577
578 /*
579  * Send data and/or a FIN to the peer.
580  *
581  * The socket's so_snd buffer consists of a stream of data starting with sb_mb
582  * and linked together with m_next.  sb_sndptr, if set, is the last mbuf that
583  * was transmitted.
584  *
585  * drop indicates the number of bytes that should be dropped from the head of
586  * the send buffer.  It is an optimization that lets do_fw4_ack avoid creating
587  * contention on the send buffer lock (before this change it used to do
588  * sowwakeup and then t4_push_frames right after that when recovering from tx
589  * stalls).  When drop is set this function MUST drop the bytes and wake up any
590  * writers.
591  */
592 void
593 t4_push_frames(struct adapter *sc, struct toepcb *toep, int drop)
594 {
595         struct mbuf *sndptr, *m, *sb_sndptr;
596         struct fw_ofld_tx_data_wr *txwr;
597         struct wrqe *wr;
598         u_int plen, nsegs, credits, max_imm, max_nsegs, max_nsegs_1mbuf;
599         struct inpcb *inp = toep->inp;
600         struct tcpcb *tp = intotcpcb(inp);
601         struct socket *so = inp->inp_socket;
602         struct sockbuf *sb = &so->so_snd;
603         int tx_credits, shove, compl, sowwakeup;
604         struct ofld_tx_sdesc *txsd = &toep->txsd[toep->txsd_pidx];
605
606         INP_WLOCK_ASSERT(inp);
607         KASSERT(toep->flags & TPF_FLOWC_WR_SENT,
608             ("%s: flowc_wr not sent for tid %u.", __func__, toep->tid));
609
610         KASSERT(toep->ulp_mode == ULP_MODE_NONE ||
611             toep->ulp_mode == ULP_MODE_TCPDDP ||
612             toep->ulp_mode == ULP_MODE_RDMA,
613             ("%s: ulp_mode %u for toep %p", __func__, toep->ulp_mode, toep));
614
615         if (__predict_false(toep->flags & TPF_ABORT_SHUTDOWN))
616                 return;
617
618         /*
619          * This function doesn't resume by itself.  Someone else must clear the
620          * flag and call this function.
621          */
622         if (__predict_false(toep->flags & TPF_TX_SUSPENDED)) {
623                 KASSERT(drop == 0,
624                     ("%s: drop (%d) != 0 but tx is suspended", __func__, drop));
625                 return;
626         }
627
628         do {
629                 tx_credits = min(toep->tx_credits, MAX_OFLD_TX_CREDITS);
630                 max_imm = max_imm_payload(tx_credits);
631                 max_nsegs = max_dsgl_nsegs(tx_credits);
632
633                 SOCKBUF_LOCK(sb);
634                 sowwakeup = drop;
635                 if (drop) {
636                         sbdrop_locked(sb, drop);
637                         drop = 0;
638                 }
639                 sb_sndptr = sb->sb_sndptr;
640                 sndptr = sb_sndptr ? sb_sndptr->m_next : sb->sb_mb;
641                 plen = 0;
642                 nsegs = 0;
643                 max_nsegs_1mbuf = 0; /* max # of SGL segments in any one mbuf */
644                 for (m = sndptr; m != NULL; m = m->m_next) {
645                         int n = sglist_count(mtod(m, void *), m->m_len);
646
647                         nsegs += n;
648                         plen += m->m_len;
649
650                         /* This mbuf sent us _over_ the nsegs limit, back out */
651                         if (plen > max_imm && nsegs > max_nsegs) {
652                                 nsegs -= n;
653                                 plen -= m->m_len;
654                                 if (plen == 0) {
655                                         /* Too few credits */
656                                         toep->flags |= TPF_TX_SUSPENDED;
657                                         if (sowwakeup)
658                                                 sowwakeup_locked(so);
659                                         else
660                                                 SOCKBUF_UNLOCK(sb);
661                                         SOCKBUF_UNLOCK_ASSERT(sb);
662                                         return;
663                                 }
664                                 break;
665                         }
666
667                         if (max_nsegs_1mbuf < n)
668                                 max_nsegs_1mbuf = n;
669                         sb_sndptr = m;  /* new sb->sb_sndptr if all goes well */
670
671                         /* This mbuf put us right at the max_nsegs limit */
672                         if (plen > max_imm && nsegs == max_nsegs) {
673                                 m = m->m_next;
674                                 break;
675                         }
676                 }
677
678                 if (sb->sb_cc > sb->sb_hiwat * 5 / 8 &&
679                     toep->plen_nocompl + plen >= sb->sb_hiwat / 4)
680                         compl = 1;
681                 else
682                         compl = 0;
683
684                 if (sb->sb_flags & SB_AUTOSIZE &&
685                     V_tcp_do_autosndbuf &&
686                     sb->sb_hiwat < V_tcp_autosndbuf_max &&
687                     sb->sb_cc >= sb->sb_hiwat * 7 / 8) {
688                         int newsize = min(sb->sb_hiwat + V_tcp_autosndbuf_inc,
689                             V_tcp_autosndbuf_max);
690
691                         if (!sbreserve_locked(sb, newsize, so, NULL))
692                                 sb->sb_flags &= ~SB_AUTOSIZE;
693                         else
694                                 sowwakeup = 1;  /* room available */
695                 }
696                 if (sowwakeup)
697                         sowwakeup_locked(so);
698                 else
699                         SOCKBUF_UNLOCK(sb);
700                 SOCKBUF_UNLOCK_ASSERT(sb);
701
702                 /* nothing to send */
703                 if (plen == 0) {
704                         KASSERT(m == NULL,
705                             ("%s: nothing to send, but m != NULL", __func__));
706                         break;
707                 }
708
709                 if (__predict_false(toep->flags & TPF_FIN_SENT))
710                         panic("%s: excess tx.", __func__);
711
712                 shove = m == NULL && !(tp->t_flags & TF_MORETOCOME);
713                 if (plen <= max_imm) {
714
715                         /* Immediate data tx */
716
717                         wr = alloc_wrqe(roundup2(sizeof(*txwr) + plen, 16),
718                                         toep->ofld_txq);
719                         if (wr == NULL) {
720                                 /* XXX: how will we recover from this? */
721                                 toep->flags |= TPF_TX_SUSPENDED;
722                                 return;
723                         }
724                         txwr = wrtod(wr);
725                         credits = howmany(wr->wr_len, 16);
726                         write_tx_wr(txwr, toep, plen, plen, credits, shove, 0,
727                             sc->tt.tx_align);
728                         m_copydata(sndptr, 0, plen, (void *)(txwr + 1));
729                         nsegs = 0;
730                 } else {
731                         int wr_len;
732
733                         /* DSGL tx */
734
735                         wr_len = sizeof(*txwr) + sizeof(struct ulptx_sgl) +
736                             ((3 * (nsegs - 1)) / 2 + ((nsegs - 1) & 1)) * 8;
737                         wr = alloc_wrqe(roundup2(wr_len, 16), toep->ofld_txq);
738                         if (wr == NULL) {
739                                 /* XXX: how will we recover from this? */
740                                 toep->flags |= TPF_TX_SUSPENDED;
741                                 return;
742                         }
743                         txwr = wrtod(wr);
744                         credits = howmany(wr_len, 16);
745                         write_tx_wr(txwr, toep, 0, plen, credits, shove, 0,
746                             sc->tt.tx_align);
747                         write_tx_sgl(txwr + 1, sndptr, m, nsegs,
748                             max_nsegs_1mbuf);
749                         if (wr_len & 0xf) {
750                                 uint64_t *pad = (uint64_t *)
751                                     ((uintptr_t)txwr + wr_len);
752                                 *pad = 0;
753                         }
754                 }
755
756                 KASSERT(toep->tx_credits >= credits,
757                         ("%s: not enough credits", __func__));
758
759                 toep->tx_credits -= credits;
760                 toep->tx_nocompl += credits;
761                 toep->plen_nocompl += plen;
762                 if (toep->tx_credits <= toep->tx_total * 3 / 8 &&
763                     toep->tx_nocompl >= toep->tx_total / 4)
764                         compl = 1;
765
766                 if (compl || toep->ulp_mode == ULP_MODE_RDMA) {
767                         txwr->op_to_immdlen |= htobe32(F_FW_WR_COMPL);
768                         toep->tx_nocompl = 0;
769                         toep->plen_nocompl = 0;
770                 }
771
772                 tp->snd_nxt += plen;
773                 tp->snd_max += plen;
774
775                 SOCKBUF_LOCK(sb);
776                 KASSERT(sb_sndptr, ("%s: sb_sndptr is NULL", __func__));
777                 sb->sb_sndptr = sb_sndptr;
778                 SOCKBUF_UNLOCK(sb);
779
780                 toep->flags |= TPF_TX_DATA_SENT;
781                 if (toep->tx_credits < MIN_OFLD_TX_CREDITS)
782                         toep->flags |= TPF_TX_SUSPENDED;
783
784                 KASSERT(toep->txsd_avail > 0, ("%s: no txsd", __func__));
785                 txsd->plen = plen;
786                 txsd->tx_credits = credits;
787                 txsd++;
788                 if (__predict_false(++toep->txsd_pidx == toep->txsd_total)) {
789                         toep->txsd_pidx = 0;
790                         txsd = &toep->txsd[0];
791                 }
792                 toep->txsd_avail--;
793
794                 t4_l2t_send(sc, wr, toep->l2te);
795         } while (m != NULL);
796
797         /* Send a FIN if requested, but only if there's no more data to send */
798         if (m == NULL && toep->flags & TPF_SEND_FIN)
799                 close_conn(sc, toep);
800 }
801
802 static inline void
803 rqdrop_locked(struct mbufq *q, int plen)
804 {
805         struct mbuf *m;
806
807         while (plen > 0) {
808                 m = mbufq_dequeue(q);
809
810                 /* Too many credits. */
811                 MPASS(m != NULL);
812                 M_ASSERTPKTHDR(m);
813
814                 /* Partial credits. */
815                 MPASS(plen >= m->m_pkthdr.len);
816
817                 plen -= m->m_pkthdr.len;
818                 m_freem(m);
819         }
820 }
821
822 void
823 t4_push_pdus(struct adapter *sc, struct toepcb *toep, int drop)
824 {
825         struct mbuf *sndptr, *m;
826         struct fw_ofld_tx_data_wr *txwr;
827         struct wrqe *wr;
828         u_int plen, nsegs, credits, max_imm, max_nsegs, max_nsegs_1mbuf;
829         u_int adjusted_plen, ulp_submode;
830         struct inpcb *inp = toep->inp;
831         struct tcpcb *tp = intotcpcb(inp);
832         int tx_credits, shove;
833         struct ofld_tx_sdesc *txsd = &toep->txsd[toep->txsd_pidx];
834         struct mbufq *pduq = &toep->ulp_pduq;
835         static const u_int ulp_extra_len[] = {0, 4, 4, 8};
836
837         INP_WLOCK_ASSERT(inp);
838         KASSERT(toep->flags & TPF_FLOWC_WR_SENT,
839             ("%s: flowc_wr not sent for tid %u.", __func__, toep->tid));
840         KASSERT(toep->ulp_mode == ULP_MODE_ISCSI,
841             ("%s: ulp_mode %u for toep %p", __func__, toep->ulp_mode, toep));
842
843         if (__predict_false(toep->flags & TPF_ABORT_SHUTDOWN))
844                 return;
845
846         /*
847          * This function doesn't resume by itself.  Someone else must clear the
848          * flag and call this function.
849          */
850         if (__predict_false(toep->flags & TPF_TX_SUSPENDED)) {
851                 KASSERT(drop == 0,
852                     ("%s: drop (%d) != 0 but tx is suspended", __func__, drop));
853                 return;
854         }
855
856         if (drop)
857                 rqdrop_locked(&toep->ulp_pdu_reclaimq, drop);
858
859         while ((sndptr = mbufq_first(pduq)) != NULL) {
860                 M_ASSERTPKTHDR(sndptr);
861
862                 tx_credits = min(toep->tx_credits, MAX_OFLD_TX_CREDITS);
863                 max_imm = max_imm_payload(tx_credits);
864                 max_nsegs = max_dsgl_nsegs(tx_credits);
865
866                 plen = 0;
867                 nsegs = 0;
868                 max_nsegs_1mbuf = 0; /* max # of SGL segments in any one mbuf */
869                 for (m = sndptr; m != NULL; m = m->m_next) {
870                         int n = sglist_count(mtod(m, void *), m->m_len);
871
872                         nsegs += n;
873                         plen += m->m_len;
874
875                         /*
876                          * This mbuf would send us _over_ the nsegs limit.
877                          * Suspend tx because the PDU can't be sent out.
878                          */
879                         if (plen > max_imm && nsegs > max_nsegs) {
880                                 toep->flags |= TPF_TX_SUSPENDED;
881                                 return;
882                         }
883
884                         if (max_nsegs_1mbuf < n)
885                                 max_nsegs_1mbuf = n;
886                 }
887
888                 if (__predict_false(toep->flags & TPF_FIN_SENT))
889                         panic("%s: excess tx.", __func__);
890
891                 /*
892                  * We have a PDU to send.  All of it goes out in one WR so 'm'
893                  * is NULL.  A PDU's length is always a multiple of 4.
894                  */
895                 MPASS(m == NULL);
896                 MPASS((plen & 3) == 0);
897                 MPASS(sndptr->m_pkthdr.len == plen);
898
899                 shove = !(tp->t_flags & TF_MORETOCOME);
900                 ulp_submode = mbuf_ulp_submode(sndptr);
901                 MPASS(ulp_submode < nitems(ulp_extra_len));
902
903                 /*
904                  * plen doesn't include header and data digests, which are
905                  * generated and inserted in the right places by the TOE, but
906                  * they do occupy TCP sequence space and need to be accounted
907                  * for.
908                  */
909                 adjusted_plen = plen + ulp_extra_len[ulp_submode];
910                 if (plen <= max_imm) {
911
912                         /* Immediate data tx */
913
914                         wr = alloc_wrqe(roundup2(sizeof(*txwr) + plen, 16),
915                                         toep->ofld_txq);
916                         if (wr == NULL) {
917                                 /* XXX: how will we recover from this? */
918                                 toep->flags |= TPF_TX_SUSPENDED;
919                                 return;
920                         }
921                         txwr = wrtod(wr);
922                         credits = howmany(wr->wr_len, 16);
923                         write_tx_wr(txwr, toep, plen, adjusted_plen, credits,
924                             shove, ulp_submode, sc->tt.tx_align);
925                         m_copydata(sndptr, 0, plen, (void *)(txwr + 1));
926                         nsegs = 0;
927                 } else {
928                         int wr_len;
929
930                         /* DSGL tx */
931                         wr_len = sizeof(*txwr) + sizeof(struct ulptx_sgl) +
932                             ((3 * (nsegs - 1)) / 2 + ((nsegs - 1) & 1)) * 8;
933                         wr = alloc_wrqe(roundup2(wr_len, 16), toep->ofld_txq);
934                         if (wr == NULL) {
935                                 /* XXX: how will we recover from this? */
936                                 toep->flags |= TPF_TX_SUSPENDED;
937                                 return;
938                         }
939                         txwr = wrtod(wr);
940                         credits = howmany(wr_len, 16);
941                         write_tx_wr(txwr, toep, 0, adjusted_plen, credits,
942                             shove, ulp_submode, sc->tt.tx_align);
943                         write_tx_sgl(txwr + 1, sndptr, m, nsegs,
944                             max_nsegs_1mbuf);
945                         if (wr_len & 0xf) {
946                                 uint64_t *pad = (uint64_t *)
947                                     ((uintptr_t)txwr + wr_len);
948                                 *pad = 0;
949                         }
950                 }
951
952                 KASSERT(toep->tx_credits >= credits,
953                         ("%s: not enough credits", __func__));
954
955                 m = mbufq_dequeue(pduq);
956                 MPASS(m == sndptr);
957                 mbufq_enqueue(&toep->ulp_pdu_reclaimq, m);
958
959                 toep->tx_credits -= credits;
960                 toep->tx_nocompl += credits;
961                 toep->plen_nocompl += plen;
962                 if (toep->tx_credits <= toep->tx_total * 3 / 8 &&
963                     toep->tx_nocompl >= toep->tx_total / 4) {
964                         txwr->op_to_immdlen |= htobe32(F_FW_WR_COMPL);
965                         toep->tx_nocompl = 0;
966                         toep->plen_nocompl = 0;
967                 }
968
969                 tp->snd_nxt += adjusted_plen;
970                 tp->snd_max += adjusted_plen;
971
972                 toep->flags |= TPF_TX_DATA_SENT;
973                 if (toep->tx_credits < MIN_OFLD_TX_CREDITS)
974                         toep->flags |= TPF_TX_SUSPENDED;
975
976                 KASSERT(toep->txsd_avail > 0, ("%s: no txsd", __func__));
977                 txsd->plen = plen;
978                 txsd->tx_credits = credits;
979                 txsd++;
980                 if (__predict_false(++toep->txsd_pidx == toep->txsd_total)) {
981                         toep->txsd_pidx = 0;
982                         txsd = &toep->txsd[0];
983                 }
984                 toep->txsd_avail--;
985
986                 t4_l2t_send(sc, wr, toep->l2te);
987         }
988
989         /* Send a FIN if requested, but only if there are no more PDUs to send */
990         if (mbufq_first(pduq) == NULL && toep->flags & TPF_SEND_FIN)
991                 close_conn(sc, toep);
992 }
993
994 int
995 t4_tod_output(struct toedev *tod, struct tcpcb *tp)
996 {
997         struct adapter *sc = tod->tod_softc;
998 #ifdef INVARIANTS
999         struct inpcb *inp = tp->t_inpcb;
1000 #endif
1001         struct toepcb *toep = tp->t_toe;
1002
1003         INP_WLOCK_ASSERT(inp);
1004         KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1005             ("%s: inp %p dropped.", __func__, inp));
1006         KASSERT(toep != NULL, ("%s: toep is NULL", __func__));
1007
1008         if (toep->ulp_mode == ULP_MODE_ISCSI)
1009                 t4_push_pdus(sc, toep, 0);
1010         else
1011                 t4_push_frames(sc, toep, 0);
1012
1013         return (0);
1014 }
1015
1016 int
1017 t4_send_fin(struct toedev *tod, struct tcpcb *tp)
1018 {
1019         struct adapter *sc = tod->tod_softc;
1020 #ifdef INVARIANTS
1021         struct inpcb *inp = tp->t_inpcb;
1022 #endif
1023         struct toepcb *toep = tp->t_toe;
1024
1025         INP_WLOCK_ASSERT(inp);
1026         KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1027             ("%s: inp %p dropped.", __func__, inp));
1028         KASSERT(toep != NULL, ("%s: toep is NULL", __func__));
1029
1030         toep->flags |= TPF_SEND_FIN;
1031         if (tp->t_state >= TCPS_ESTABLISHED) {
1032                 if (toep->ulp_mode == ULP_MODE_ISCSI)
1033                         t4_push_pdus(sc, toep, 0);
1034                 else
1035                         t4_push_frames(sc, toep, 0);
1036         }
1037
1038         return (0);
1039 }
1040
1041 int
1042 t4_send_rst(struct toedev *tod, struct tcpcb *tp)
1043 {
1044         struct adapter *sc = tod->tod_softc;
1045 #if defined(INVARIANTS)
1046         struct inpcb *inp = tp->t_inpcb;
1047 #endif
1048         struct toepcb *toep = tp->t_toe;
1049
1050         INP_WLOCK_ASSERT(inp);
1051         KASSERT((inp->inp_flags & INP_DROPPED) == 0,
1052             ("%s: inp %p dropped.", __func__, inp));
1053         KASSERT(toep != NULL, ("%s: toep is NULL", __func__));
1054
1055         /* hmmmm */
1056         KASSERT(toep->flags & TPF_FLOWC_WR_SENT,
1057             ("%s: flowc for tid %u [%s] not sent already",
1058             __func__, toep->tid, tcpstates[tp->t_state]));
1059
1060         send_reset(sc, toep, 0);
1061         return (0);
1062 }
1063
1064 /*
1065  * Peer has sent us a FIN.
1066  */
1067 static int
1068 do_peer_close(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
1069 {
1070         struct adapter *sc = iq->adapter;
1071         const struct cpl_peer_close *cpl = (const void *)(rss + 1);
1072         unsigned int tid = GET_TID(cpl);
1073         struct toepcb *toep = lookup_tid(sc, tid);
1074         struct inpcb *inp = toep->inp;
1075         struct tcpcb *tp = NULL;
1076         struct socket *so;
1077         struct sockbuf *sb;
1078 #ifdef INVARIANTS
1079         unsigned int opcode = G_CPL_OPCODE(be32toh(OPCODE_TID(cpl)));
1080 #endif
1081
1082         KASSERT(opcode == CPL_PEER_CLOSE,
1083             ("%s: unexpected opcode 0x%x", __func__, opcode));
1084         KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
1085
1086         if (__predict_false(toep->flags & TPF_SYNQE)) {
1087 #ifdef INVARIANTS
1088                 struct synq_entry *synqe = (void *)toep;
1089
1090                 INP_WLOCK(synqe->lctx->inp);
1091                 if (synqe->flags & TPF_SYNQE_HAS_L2TE) {
1092                         KASSERT(synqe->flags & TPF_ABORT_SHUTDOWN,
1093                             ("%s: listen socket closed but tid %u not aborted.",
1094                             __func__, tid));
1095                 } else {
1096                         /*
1097                          * do_pass_accept_req is still running and will
1098                          * eventually take care of this tid.
1099                          */
1100                 }
1101                 INP_WUNLOCK(synqe->lctx->inp);
1102 #endif
1103                 CTR4(KTR_CXGBE, "%s: tid %u, synqe %p (0x%x)", __func__, tid,
1104                     toep, toep->flags);
1105                 return (0);
1106         }
1107
1108         KASSERT(toep->tid == tid, ("%s: toep tid mismatch", __func__));
1109
1110         INP_INFO_RLOCK(&V_tcbinfo);
1111         INP_WLOCK(inp);
1112         tp = intotcpcb(inp);
1113
1114         CTR5(KTR_CXGBE, "%s: tid %u (%s), toep_flags 0x%x, inp %p", __func__,
1115             tid, tp ? tcpstates[tp->t_state] : "no tp", toep->flags, inp);
1116
1117         if (toep->flags & TPF_ABORT_SHUTDOWN)
1118                 goto done;
1119
1120         tp->rcv_nxt++;  /* FIN */
1121
1122         so = inp->inp_socket;
1123         sb = &so->so_rcv;
1124         SOCKBUF_LOCK(sb);
1125         if (__predict_false(toep->ddp_flags & (DDP_BUF0_ACTIVE | DDP_BUF1_ACTIVE))) {
1126                 handle_ddp_close(toep, tp, sb, cpl->rcv_nxt);
1127         }
1128         socantrcvmore_locked(so);       /* unlocks the sockbuf */
1129
1130         if (toep->ulp_mode != ULP_MODE_RDMA) {
1131                 KASSERT(tp->rcv_nxt == be32toh(cpl->rcv_nxt),
1132                         ("%s: rcv_nxt mismatch: %u %u", __func__, tp->rcv_nxt,
1133                         be32toh(cpl->rcv_nxt)));
1134         }
1135
1136         switch (tp->t_state) {
1137         case TCPS_SYN_RECEIVED:
1138                 tp->t_starttime = ticks;
1139                 /* FALLTHROUGH */ 
1140
1141         case TCPS_ESTABLISHED:
1142                 tp->t_state = TCPS_CLOSE_WAIT;
1143                 break;
1144
1145         case TCPS_FIN_WAIT_1:
1146                 tp->t_state = TCPS_CLOSING;
1147                 break;
1148
1149         case TCPS_FIN_WAIT_2:
1150                 tcp_twstart(tp);
1151                 INP_UNLOCK_ASSERT(inp);  /* safe, we have a ref on the inp */
1152                 INP_INFO_RUNLOCK(&V_tcbinfo);
1153
1154                 INP_WLOCK(inp);
1155                 final_cpl_received(toep);
1156                 return (0);
1157
1158         default:
1159                 log(LOG_ERR, "%s: TID %u received CPL_PEER_CLOSE in state %d\n",
1160                     __func__, tid, tp->t_state);
1161         }
1162 done:
1163         INP_WUNLOCK(inp);
1164         INP_INFO_RUNLOCK(&V_tcbinfo);
1165         return (0);
1166 }
1167
1168 /*
1169  * Peer has ACK'd our FIN.
1170  */
1171 static int
1172 do_close_con_rpl(struct sge_iq *iq, const struct rss_header *rss,
1173     struct mbuf *m)
1174 {
1175         struct adapter *sc = iq->adapter;
1176         const struct cpl_close_con_rpl *cpl = (const void *)(rss + 1);
1177         unsigned int tid = GET_TID(cpl);
1178         struct toepcb *toep = lookup_tid(sc, tid);
1179         struct inpcb *inp = toep->inp;
1180         struct tcpcb *tp = NULL;
1181         struct socket *so = NULL;
1182 #ifdef INVARIANTS
1183         unsigned int opcode = G_CPL_OPCODE(be32toh(OPCODE_TID(cpl)));
1184 #endif
1185
1186         KASSERT(opcode == CPL_CLOSE_CON_RPL,
1187             ("%s: unexpected opcode 0x%x", __func__, opcode));
1188         KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
1189         KASSERT(toep->tid == tid, ("%s: toep tid mismatch", __func__));
1190
1191         INP_INFO_RLOCK(&V_tcbinfo);
1192         INP_WLOCK(inp);
1193         tp = intotcpcb(inp);
1194
1195         CTR4(KTR_CXGBE, "%s: tid %u (%s), toep_flags 0x%x",
1196             __func__, tid, tp ? tcpstates[tp->t_state] : "no tp", toep->flags);
1197
1198         if (toep->flags & TPF_ABORT_SHUTDOWN)
1199                 goto done;
1200
1201         so = inp->inp_socket;
1202         tp->snd_una = be32toh(cpl->snd_nxt) - 1;        /* exclude FIN */
1203
1204         switch (tp->t_state) {
1205         case TCPS_CLOSING:      /* see TCPS_FIN_WAIT_2 in do_peer_close too */
1206                 tcp_twstart(tp);
1207 release:
1208                 INP_UNLOCK_ASSERT(inp); /* safe, we have a ref on the  inp */
1209                 INP_INFO_RUNLOCK(&V_tcbinfo);
1210
1211                 INP_WLOCK(inp);
1212                 final_cpl_received(toep);       /* no more CPLs expected */
1213
1214                 return (0);
1215         case TCPS_LAST_ACK:
1216                 if (tcp_close(tp))
1217                         INP_WUNLOCK(inp);
1218                 goto release;
1219
1220         case TCPS_FIN_WAIT_1:
1221                 if (so->so_rcv.sb_state & SBS_CANTRCVMORE)
1222                         soisdisconnected(so);
1223                 tp->t_state = TCPS_FIN_WAIT_2;
1224                 break;
1225
1226         default:
1227                 log(LOG_ERR,
1228                     "%s: TID %u received CPL_CLOSE_CON_RPL in state %s\n",
1229                     __func__, tid, tcpstates[tp->t_state]);
1230         }
1231 done:
1232         INP_WUNLOCK(inp);
1233         INP_INFO_RUNLOCK(&V_tcbinfo);
1234         return (0);
1235 }
1236
1237 void
1238 send_abort_rpl(struct adapter *sc, struct sge_wrq *ofld_txq, int tid,
1239     int rst_status)
1240 {
1241         struct wrqe *wr;
1242         struct cpl_abort_rpl *cpl;
1243
1244         wr = alloc_wrqe(sizeof(*cpl), ofld_txq);
1245         if (wr == NULL) {
1246                 /* XXX */
1247                 panic("%s: allocation failure.", __func__);
1248         }
1249         cpl = wrtod(wr);
1250
1251         INIT_TP_WR_MIT_CPL(cpl, CPL_ABORT_RPL, tid);
1252         cpl->cmd = rst_status;
1253
1254         t4_wrq_tx(sc, wr);
1255 }
1256
1257 static int
1258 abort_status_to_errno(struct tcpcb *tp, unsigned int abort_reason)
1259 {
1260         switch (abort_reason) {
1261         case CPL_ERR_BAD_SYN:
1262         case CPL_ERR_CONN_RESET:
1263                 return (tp->t_state == TCPS_CLOSE_WAIT ? EPIPE : ECONNRESET);
1264         case CPL_ERR_XMIT_TIMEDOUT:
1265         case CPL_ERR_PERSIST_TIMEDOUT:
1266         case CPL_ERR_FINWAIT2_TIMEDOUT:
1267         case CPL_ERR_KEEPALIVE_TIMEDOUT:
1268                 return (ETIMEDOUT);
1269         default:
1270                 return (EIO);
1271         }
1272 }
1273
1274 /*
1275  * TCP RST from the peer, timeout, or some other such critical error.
1276  */
1277 static int
1278 do_abort_req(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
1279 {
1280         struct adapter *sc = iq->adapter;
1281         const struct cpl_abort_req_rss *cpl = (const void *)(rss + 1);
1282         unsigned int tid = GET_TID(cpl);
1283         struct toepcb *toep = lookup_tid(sc, tid);
1284         struct sge_wrq *ofld_txq = toep->ofld_txq;
1285         struct inpcb *inp;
1286         struct tcpcb *tp;
1287 #ifdef INVARIANTS
1288         unsigned int opcode = G_CPL_OPCODE(be32toh(OPCODE_TID(cpl)));
1289 #endif
1290
1291         KASSERT(opcode == CPL_ABORT_REQ_RSS,
1292             ("%s: unexpected opcode 0x%x", __func__, opcode));
1293         KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
1294
1295         if (toep->flags & TPF_SYNQE)
1296                 return (do_abort_req_synqe(iq, rss, m));
1297
1298         KASSERT(toep->tid == tid, ("%s: toep tid mismatch", __func__));
1299
1300         if (negative_advice(cpl->status)) {
1301                 CTR4(KTR_CXGBE, "%s: negative advice %d for tid %d (0x%x)",
1302                     __func__, cpl->status, tid, toep->flags);
1303                 return (0);     /* Ignore negative advice */
1304         }
1305
1306         inp = toep->inp;
1307         INP_INFO_RLOCK(&V_tcbinfo);     /* for tcp_close */
1308         INP_WLOCK(inp);
1309
1310         tp = intotcpcb(inp);
1311
1312         CTR6(KTR_CXGBE,
1313             "%s: tid %d (%s), toep_flags 0x%x, inp_flags 0x%x, status %d",
1314             __func__, tid, tp ? tcpstates[tp->t_state] : "no tp", toep->flags,
1315             inp->inp_flags, cpl->status);
1316
1317         /*
1318          * If we'd initiated an abort earlier the reply to it is responsible for
1319          * cleaning up resources.  Otherwise we tear everything down right here
1320          * right now.  We owe the T4 a CPL_ABORT_RPL no matter what.
1321          */
1322         if (toep->flags & TPF_ABORT_SHUTDOWN) {
1323                 INP_WUNLOCK(inp);
1324                 goto done;
1325         }
1326         toep->flags |= TPF_ABORT_SHUTDOWN;
1327
1328         if ((inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT)) == 0) {
1329                 struct socket *so = inp->inp_socket;
1330
1331                 if (so != NULL)
1332                         so_error_set(so, abort_status_to_errno(tp,
1333                             cpl->status));
1334                 tp = tcp_close(tp);
1335                 if (tp == NULL)
1336                         INP_WLOCK(inp); /* re-acquire */
1337         }
1338
1339         final_cpl_received(toep);
1340 done:
1341         INP_INFO_RUNLOCK(&V_tcbinfo);
1342         send_abort_rpl(sc, ofld_txq, tid, CPL_ABORT_NO_RST);
1343         return (0);
1344 }
1345
1346 /*
1347  * Reply to the CPL_ABORT_REQ (send_reset)
1348  */
1349 static int
1350 do_abort_rpl(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
1351 {
1352         struct adapter *sc = iq->adapter;
1353         const struct cpl_abort_rpl_rss *cpl = (const void *)(rss + 1);
1354         unsigned int tid = GET_TID(cpl);
1355         struct toepcb *toep = lookup_tid(sc, tid);
1356         struct inpcb *inp = toep->inp;
1357 #ifdef INVARIANTS
1358         unsigned int opcode = G_CPL_OPCODE(be32toh(OPCODE_TID(cpl)));
1359 #endif
1360
1361         KASSERT(opcode == CPL_ABORT_RPL_RSS,
1362             ("%s: unexpected opcode 0x%x", __func__, opcode));
1363         KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
1364
1365         if (toep->flags & TPF_SYNQE)
1366                 return (do_abort_rpl_synqe(iq, rss, m));
1367
1368         KASSERT(toep->tid == tid, ("%s: toep tid mismatch", __func__));
1369
1370         CTR5(KTR_CXGBE, "%s: tid %u, toep %p, inp %p, status %d",
1371             __func__, tid, toep, inp, cpl->status);
1372
1373         KASSERT(toep->flags & TPF_ABORT_SHUTDOWN,
1374             ("%s: wasn't expecting abort reply", __func__));
1375
1376         INP_WLOCK(inp);
1377         final_cpl_received(toep);
1378
1379         return (0);
1380 }
1381
1382 static int
1383 do_rx_data(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
1384 {
1385         struct adapter *sc = iq->adapter;
1386         const struct cpl_rx_data *cpl = mtod(m, const void *);
1387         unsigned int tid = GET_TID(cpl);
1388         struct toepcb *toep = lookup_tid(sc, tid);
1389         struct inpcb *inp = toep->inp;
1390         struct tcpcb *tp;
1391         struct socket *so;
1392         struct sockbuf *sb;
1393         int len;
1394         uint32_t ddp_placed = 0;
1395
1396         if (__predict_false(toep->flags & TPF_SYNQE)) {
1397 #ifdef INVARIANTS
1398                 struct synq_entry *synqe = (void *)toep;
1399
1400                 INP_WLOCK(synqe->lctx->inp);
1401                 if (synqe->flags & TPF_SYNQE_HAS_L2TE) {
1402                         KASSERT(synqe->flags & TPF_ABORT_SHUTDOWN,
1403                             ("%s: listen socket closed but tid %u not aborted.",
1404                             __func__, tid));
1405                 } else {
1406                         /*
1407                          * do_pass_accept_req is still running and will
1408                          * eventually take care of this tid.
1409                          */
1410                 }
1411                 INP_WUNLOCK(synqe->lctx->inp);
1412 #endif
1413                 CTR4(KTR_CXGBE, "%s: tid %u, synqe %p (0x%x)", __func__, tid,
1414                     toep, toep->flags);
1415                 m_freem(m);
1416                 return (0);
1417         }
1418
1419         KASSERT(toep->tid == tid, ("%s: toep tid mismatch", __func__));
1420
1421         /* strip off CPL header */
1422         m_adj(m, sizeof(*cpl));
1423         len = m->m_pkthdr.len;
1424
1425         INP_WLOCK(inp);
1426         if (inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT)) {
1427                 CTR4(KTR_CXGBE, "%s: tid %u, rx (%d bytes), inp_flags 0x%x",
1428                     __func__, tid, len, inp->inp_flags);
1429                 INP_WUNLOCK(inp);
1430                 m_freem(m);
1431                 return (0);
1432         }
1433
1434         tp = intotcpcb(inp);
1435
1436         if (__predict_false(tp->rcv_nxt != be32toh(cpl->seq)))
1437                 ddp_placed = be32toh(cpl->seq) - tp->rcv_nxt;
1438
1439         tp->rcv_nxt += len;
1440         if (tp->rcv_wnd < len) {
1441                 KASSERT(toep->ulp_mode == ULP_MODE_RDMA,
1442                                 ("%s: negative window size", __func__));
1443         }
1444
1445         tp->rcv_wnd -= len;
1446         tp->t_rcvtime = ticks;
1447
1448         so = inp_inpcbtosocket(inp);
1449         sb = &so->so_rcv;
1450         SOCKBUF_LOCK(sb);
1451
1452         if (__predict_false(sb->sb_state & SBS_CANTRCVMORE)) {
1453                 CTR3(KTR_CXGBE, "%s: tid %u, excess rx (%d bytes)",
1454                     __func__, tid, len);
1455                 m_freem(m);
1456                 SOCKBUF_UNLOCK(sb);
1457                 INP_WUNLOCK(inp);
1458
1459                 INP_INFO_RLOCK(&V_tcbinfo);
1460                 INP_WLOCK(inp);
1461                 tp = tcp_drop(tp, ECONNRESET);
1462                 if (tp)
1463                         INP_WUNLOCK(inp);
1464                 INP_INFO_RUNLOCK(&V_tcbinfo);
1465
1466                 return (0);
1467         }
1468
1469         /* receive buffer autosize */
1470         CURVNET_SET(so->so_vnet);
1471         if (sb->sb_flags & SB_AUTOSIZE &&
1472             V_tcp_do_autorcvbuf &&
1473             sb->sb_hiwat < V_tcp_autorcvbuf_max &&
1474             len > (sbspace(sb) / 8 * 7)) {
1475                 unsigned int hiwat = sb->sb_hiwat;
1476                 unsigned int newsize = min(hiwat + V_tcp_autorcvbuf_inc,
1477                     V_tcp_autorcvbuf_max);
1478
1479                 if (!sbreserve_locked(sb, newsize, so, NULL))
1480                         sb->sb_flags &= ~SB_AUTOSIZE;
1481                 else
1482                         toep->rx_credits += newsize - hiwat;
1483         }
1484
1485         if (toep->ulp_mode == ULP_MODE_TCPDDP) {
1486                 int changed = !(toep->ddp_flags & DDP_ON) ^ cpl->ddp_off;
1487
1488                 if (changed) {
1489                         if (toep->ddp_flags & DDP_SC_REQ)
1490                                 toep->ddp_flags ^= DDP_ON | DDP_SC_REQ;
1491                         else {
1492                                 KASSERT(cpl->ddp_off == 1,
1493                                     ("%s: DDP switched on by itself.",
1494                                     __func__));
1495
1496                                 /* Fell out of DDP mode */
1497                                 toep->ddp_flags &= ~(DDP_ON | DDP_BUF0_ACTIVE |
1498                                     DDP_BUF1_ACTIVE);
1499
1500                                 if (ddp_placed)
1501                                         insert_ddp_data(toep, ddp_placed);
1502                         }
1503                 }
1504
1505                 if ((toep->ddp_flags & DDP_OK) == 0 &&
1506                     time_uptime >= toep->ddp_disabled + DDP_RETRY_WAIT) {
1507                         toep->ddp_score = DDP_LOW_SCORE;
1508                         toep->ddp_flags |= DDP_OK;
1509                         CTR3(KTR_CXGBE, "%s: tid %u DDP_OK @ %u",
1510                             __func__, tid, time_uptime);
1511                 }
1512
1513                 if (toep->ddp_flags & DDP_ON) {
1514
1515                         /*
1516                          * CPL_RX_DATA with DDP on can only be an indicate.  Ask
1517                          * soreceive to post a buffer or disable DDP.  The
1518                          * payload that arrived in this indicate is appended to
1519                          * the socket buffer as usual.
1520                          */
1521
1522 #if 0
1523                         CTR5(KTR_CXGBE,
1524                             "%s: tid %u (0x%x) DDP indicate (seq 0x%x, len %d)",
1525                             __func__, tid, toep->flags, be32toh(cpl->seq), len);
1526 #endif
1527                         sb->sb_flags |= SB_DDP_INDICATE;
1528                 } else if ((toep->ddp_flags & (DDP_OK|DDP_SC_REQ)) == DDP_OK &&
1529                     tp->rcv_wnd > DDP_RSVD_WIN && len >= sc->tt.ddp_thres) {
1530
1531                         /*
1532                          * DDP allowed but isn't on (and a request to switch it
1533                          * on isn't pending either), and conditions are ripe for
1534                          * it to work.  Switch it on.
1535                          */
1536
1537                         enable_ddp(sc, toep);
1538                 }
1539         }
1540
1541         KASSERT(toep->sb_cc >= sb->sb_cc,
1542             ("%s: sb %p has more data (%d) than last time (%d).",
1543             __func__, sb, sb->sb_cc, toep->sb_cc));
1544         toep->rx_credits += toep->sb_cc - sb->sb_cc;
1545         sbappendstream_locked(sb, m);
1546         toep->sb_cc = sb->sb_cc;
1547         if (toep->rx_credits > 0 && toep->sb_cc + tp->rcv_wnd < sb->sb_lowat) {
1548                 int credits;
1549
1550                 credits = send_rx_credits(sc, toep, toep->rx_credits);
1551                 toep->rx_credits -= credits;
1552                 tp->rcv_wnd += credits;
1553                 tp->rcv_adv += credits;
1554         }
1555         sorwakeup_locked(so);
1556         SOCKBUF_UNLOCK_ASSERT(sb);
1557
1558         INP_WUNLOCK(inp);
1559         CURVNET_RESTORE();
1560         return (0);
1561 }
1562
1563 #define S_CPL_FW4_ACK_OPCODE    24
1564 #define M_CPL_FW4_ACK_OPCODE    0xff
1565 #define V_CPL_FW4_ACK_OPCODE(x) ((x) << S_CPL_FW4_ACK_OPCODE)
1566 #define G_CPL_FW4_ACK_OPCODE(x) \
1567     (((x) >> S_CPL_FW4_ACK_OPCODE) & M_CPL_FW4_ACK_OPCODE)
1568
1569 #define S_CPL_FW4_ACK_FLOWID    0
1570 #define M_CPL_FW4_ACK_FLOWID    0xffffff
1571 #define V_CPL_FW4_ACK_FLOWID(x) ((x) << S_CPL_FW4_ACK_FLOWID)
1572 #define G_CPL_FW4_ACK_FLOWID(x) \
1573     (((x) >> S_CPL_FW4_ACK_FLOWID) & M_CPL_FW4_ACK_FLOWID)
1574
1575 #define S_CPL_FW4_ACK_CR        24
1576 #define M_CPL_FW4_ACK_CR        0xff
1577 #define V_CPL_FW4_ACK_CR(x)     ((x) << S_CPL_FW4_ACK_CR)
1578 #define G_CPL_FW4_ACK_CR(x)     (((x) >> S_CPL_FW4_ACK_CR) & M_CPL_FW4_ACK_CR)
1579
1580 #define S_CPL_FW4_ACK_SEQVAL    0
1581 #define M_CPL_FW4_ACK_SEQVAL    0x1
1582 #define V_CPL_FW4_ACK_SEQVAL(x) ((x) << S_CPL_FW4_ACK_SEQVAL)
1583 #define G_CPL_FW4_ACK_SEQVAL(x) \
1584     (((x) >> S_CPL_FW4_ACK_SEQVAL) & M_CPL_FW4_ACK_SEQVAL)
1585 #define F_CPL_FW4_ACK_SEQVAL    V_CPL_FW4_ACK_SEQVAL(1U)
1586
1587 static int
1588 do_fw4_ack(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
1589 {
1590         struct adapter *sc = iq->adapter;
1591         const struct cpl_fw4_ack *cpl = (const void *)(rss + 1);
1592         unsigned int tid = G_CPL_FW4_ACK_FLOWID(be32toh(OPCODE_TID(cpl)));
1593         struct toepcb *toep = lookup_tid(sc, tid);
1594         struct inpcb *inp;
1595         struct tcpcb *tp;
1596         struct socket *so;
1597         uint8_t credits = cpl->credits;
1598         struct ofld_tx_sdesc *txsd;
1599         int plen;
1600 #ifdef INVARIANTS
1601         unsigned int opcode = G_CPL_FW4_ACK_OPCODE(be32toh(OPCODE_TID(cpl)));
1602 #endif
1603
1604         /*
1605          * Very unusual case: we'd sent a flowc + abort_req for a synq entry and
1606          * now this comes back carrying the credits for the flowc.
1607          */
1608         if (__predict_false(toep->flags & TPF_SYNQE)) {
1609                 KASSERT(toep->flags & TPF_ABORT_SHUTDOWN,
1610                     ("%s: credits for a synq entry %p", __func__, toep));
1611                 return (0);
1612         }
1613
1614         inp = toep->inp;
1615
1616         KASSERT(opcode == CPL_FW4_ACK,
1617             ("%s: unexpected opcode 0x%x", __func__, opcode));
1618         KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
1619         KASSERT(toep->tid == tid, ("%s: toep tid mismatch", __func__));
1620
1621         INP_WLOCK(inp);
1622
1623         if (__predict_false(toep->flags & TPF_ABORT_SHUTDOWN)) {
1624                 INP_WUNLOCK(inp);
1625                 return (0);
1626         }
1627
1628         KASSERT((inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) == 0,
1629             ("%s: inp_flags 0x%x", __func__, inp->inp_flags));
1630
1631         tp = intotcpcb(inp);
1632
1633         if (cpl->flags & CPL_FW4_ACK_FLAGS_SEQVAL) {
1634                 tcp_seq snd_una = be32toh(cpl->snd_una);
1635
1636 #ifdef INVARIANTS
1637                 if (__predict_false(SEQ_LT(snd_una, tp->snd_una))) {
1638                         log(LOG_ERR,
1639                             "%s: unexpected seq# %x for TID %u, snd_una %x\n",
1640                             __func__, snd_una, toep->tid, tp->snd_una);
1641                 }
1642 #endif
1643
1644                 if (tp->snd_una != snd_una) {
1645                         tp->snd_una = snd_una;
1646                         tp->ts_recent_age = tcp_ts_getticks();
1647                 }
1648         }
1649
1650         so = inp->inp_socket;
1651         txsd = &toep->txsd[toep->txsd_cidx];
1652         plen = 0;
1653         while (credits) {
1654                 KASSERT(credits >= txsd->tx_credits,
1655                     ("%s: too many (or partial) credits", __func__));
1656                 credits -= txsd->tx_credits;
1657                 toep->tx_credits += txsd->tx_credits;
1658                 plen += txsd->plen;
1659                 txsd++;
1660                 toep->txsd_avail++;
1661                 KASSERT(toep->txsd_avail <= toep->txsd_total,
1662                     ("%s: txsd avail > total", __func__));
1663                 if (__predict_false(++toep->txsd_cidx == toep->txsd_total)) {
1664                         txsd = &toep->txsd[0];
1665                         toep->txsd_cidx = 0;
1666                 }
1667         }
1668
1669         if (toep->tx_credits == toep->tx_total) {
1670                 toep->tx_nocompl = 0;
1671                 toep->plen_nocompl = 0;
1672         }
1673
1674         if (toep->flags & TPF_TX_SUSPENDED &&
1675             toep->tx_credits >= toep->tx_total / 4) {
1676                 toep->flags &= ~TPF_TX_SUSPENDED;
1677                 if (toep->ulp_mode == ULP_MODE_ISCSI)
1678                         t4_push_pdus(sc, toep, plen);
1679                 else
1680                         t4_push_frames(sc, toep, plen);
1681         } else if (plen > 0) {
1682                 struct sockbuf *sb = &so->so_snd;
1683                 int sbu;
1684
1685                 SOCKBUF_LOCK(sb);
1686                 sbu = sb->sb_cc;
1687                 if (toep->ulp_mode == ULP_MODE_ISCSI) {
1688
1689                         if (__predict_false(sbu > 0)) {
1690                                 /*
1691                                  * The data trasmitted before the tid's ULP mode
1692                                  * changed to ISCSI is still in so_snd.
1693                                  * Incoming credits should account for so_snd
1694                                  * first.
1695                                  */
1696                                 sbdrop_locked(sb, min(sbu, plen));
1697                                 plen -= min(sbu, plen);
1698                         }
1699                         sowwakeup_locked(so);   /* unlocks so_snd */
1700                         rqdrop_locked(&toep->ulp_pdu_reclaimq, plen);
1701                 } else {
1702                         sbdrop_locked(sb, plen);
1703                         sowwakeup_locked(so);   /* unlocks so_snd */
1704                 }
1705                 SOCKBUF_UNLOCK_ASSERT(sb);
1706         }
1707
1708         INP_WUNLOCK(inp);
1709
1710         return (0);
1711 }
1712
1713 static int
1714 do_set_tcb_rpl(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
1715 {
1716         struct adapter *sc = iq->adapter;
1717         const struct cpl_set_tcb_rpl *cpl = (const void *)(rss + 1);
1718         unsigned int tid = GET_TID(cpl);
1719 #ifdef INVARIANTS
1720         unsigned int opcode = G_CPL_OPCODE(be32toh(OPCODE_TID(cpl)));
1721 #endif
1722
1723         KASSERT(opcode == CPL_SET_TCB_RPL,
1724             ("%s: unexpected opcode 0x%x", __func__, opcode));
1725         KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
1726
1727         if (is_ftid(sc, tid))
1728                 return (t4_filter_rpl(iq, rss, m)); /* TCB is a filter */
1729
1730         /*
1731          * TOM and/or other ULPs don't request replies for CPL_SET_TCB or
1732          * CPL_SET_TCB_FIELD requests.  This can easily change and when it does
1733          * the dispatch code will go here.
1734          */
1735 #ifdef INVARIANTS
1736         panic("%s: Unexpected CPL_SET_TCB_RPL for tid %u on iq %p", __func__,
1737             tid, iq);
1738 #else
1739         log(LOG_ERR, "%s: Unexpected CPL_SET_TCB_RPL for tid %u on iq %p\n",
1740             __func__, tid, iq);
1741 #endif
1742
1743         return (0);
1744 }
1745
1746 void
1747 t4_set_tcb_field(struct adapter *sc, struct toepcb *toep, int ctrl,
1748     uint16_t word, uint64_t mask, uint64_t val)
1749 {
1750         struct wrqe *wr;
1751         struct cpl_set_tcb_field *req;
1752
1753         wr = alloc_wrqe(sizeof(*req), ctrl ? toep->ctrlq : toep->ofld_txq);
1754         if (wr == NULL) {
1755                 /* XXX */
1756                 panic("%s: allocation failure.", __func__);
1757         }
1758         req = wrtod(wr);
1759
1760         INIT_TP_WR_MIT_CPL(req, CPL_SET_TCB_FIELD, toep->tid);
1761         req->reply_ctrl = htobe16(V_NO_REPLY(1) |
1762             V_QUEUENO(toep->ofld_rxq->iq.abs_id));
1763         req->word_cookie = htobe16(V_WORD(word) | V_COOKIE(0));
1764         req->mask = htobe64(mask);
1765         req->val = htobe64(val);
1766
1767         t4_wrq_tx(sc, wr);
1768 }
1769
1770 void
1771 t4_init_cpl_io_handlers(struct adapter *sc)
1772 {
1773
1774         t4_register_cpl_handler(sc, CPL_PEER_CLOSE, do_peer_close);
1775         t4_register_cpl_handler(sc, CPL_CLOSE_CON_RPL, do_close_con_rpl);
1776         t4_register_cpl_handler(sc, CPL_ABORT_REQ_RSS, do_abort_req);
1777         t4_register_cpl_handler(sc, CPL_ABORT_RPL_RSS, do_abort_rpl);
1778         t4_register_cpl_handler(sc, CPL_RX_DATA, do_rx_data);
1779         t4_register_cpl_handler(sc, CPL_FW4_ACK, do_fw4_ack);
1780         t4_register_cpl_handler(sc, CPL_SET_TCB_RPL, do_set_tcb_rpl);
1781 }
1782
1783 void
1784 t4_uninit_cpl_io_handlers(struct adapter *sc)
1785 {
1786
1787         t4_register_cpl_handler(sc, CPL_SET_TCB_RPL, t4_filter_rpl);
1788 }
1789 #endif