]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/cxgbe/tom/t4_listen.c
cxgbe/t4_tom: Fix tid accounting. An offloaded IPv6 connection uses 2
[FreeBSD/FreeBSD.git] / sys / dev / cxgbe / tom / t4_listen.c
1 /*-
2  * Copyright (c) 2012 Chelsio Communications, Inc.
3  * All rights reserved.
4  * Written by: Navdeep Parhar <np@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include "opt_inet.h"
32 #include "opt_inet6.h"
33
34 #ifdef TCP_OFFLOAD
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/kernel.h>
38 #include <sys/ktr.h>
39 #include <sys/module.h>
40 #include <sys/protosw.h>
41 #include <sys/refcount.h>
42 #include <sys/domain.h>
43 #include <sys/fnv_hash.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <net/ethernet.h>
47 #include <net/if.h>
48 #include <net/if_types.h>
49 #include <net/if_vlan_var.h>
50 #include <net/route.h>
51 #include <netinet/in.h>
52 #include <netinet/in_fib.h>
53 #include <netinet/in_pcb.h>
54 #include <netinet/ip.h>
55 #include <netinet/ip6.h>
56 #include <netinet6/in6_fib.h>
57 #include <netinet6/scope6_var.h>
58 #include <netinet/tcp_timer.h>
59 #define TCPSTATES
60 #include <netinet/tcp_fsm.h>
61 #include <netinet/tcp_var.h>
62 #include <netinet/toecore.h>
63
64 #include "common/common.h"
65 #include "common/t4_msg.h"
66 #include "common/t4_regs.h"
67 #include "tom/t4_tom_l2t.h"
68 #include "tom/t4_tom.h"
69
70 /* stid services */
71 static int alloc_stid(struct adapter *, struct listen_ctx *, int);
72 static struct listen_ctx *lookup_stid(struct adapter *, int);
73 static void free_stid(struct adapter *, struct listen_ctx *);
74
75 /* lctx services */
76 static struct listen_ctx *alloc_lctx(struct adapter *, struct inpcb *,
77     struct vi_info *);
78 static int free_lctx(struct adapter *, struct listen_ctx *);
79 static void hold_lctx(struct listen_ctx *);
80 static void listen_hash_add(struct adapter *, struct listen_ctx *);
81 static struct listen_ctx *listen_hash_find(struct adapter *, struct inpcb *);
82 static struct listen_ctx *listen_hash_del(struct adapter *, struct inpcb *);
83 static struct inpcb *release_lctx(struct adapter *, struct listen_ctx *);
84
85 static inline void save_qids_in_mbuf(struct mbuf *, struct vi_info *);
86 static inline void get_qids_from_mbuf(struct mbuf *m, int *, int *);
87 static void send_reset_synqe(struct toedev *, struct synq_entry *);
88
89 static int
90 alloc_stid(struct adapter *sc, struct listen_ctx *lctx, int isipv6)
91 {
92         struct tid_info *t = &sc->tids;
93         u_int stid, n, f, mask;
94         struct stid_region *sr = &lctx->stid_region;
95
96         /*
97          * An IPv6 server needs 2 naturally aligned stids (1 stid = 4 cells) in
98          * the TCAM.  The start of the stid region is properly aligned (the chip
99          * requires each region to be 128-cell aligned).
100          */
101         n = isipv6 ? 2 : 1;
102         mask = n - 1;
103         KASSERT((t->stid_base & mask) == 0 && (t->nstids & mask) == 0,
104             ("%s: stid region (%u, %u) not properly aligned.  n = %u",
105             __func__, t->stid_base, t->nstids, n));
106
107         mtx_lock(&t->stid_lock);
108         if (n > t->nstids - t->stids_in_use) {
109                 mtx_unlock(&t->stid_lock);
110                 return (-1);
111         }
112
113         if (t->nstids_free_head >= n) {
114                 /*
115                  * This allocation will definitely succeed because the region
116                  * starts at a good alignment and we just checked we have enough
117                  * stids free.
118                  */
119                 f = t->nstids_free_head & mask;
120                 t->nstids_free_head -= n + f;
121                 stid = t->nstids_free_head;
122                 TAILQ_INSERT_HEAD(&t->stids, sr, link);
123         } else {
124                 struct stid_region *s;
125
126                 stid = t->nstids_free_head;
127                 TAILQ_FOREACH(s, &t->stids, link) {
128                         stid += s->used + s->free;
129                         f = stid & mask;
130                         if (s->free >= n + f) {
131                                 stid -= n + f;
132                                 s->free -= n + f;
133                                 TAILQ_INSERT_AFTER(&t->stids, s, sr, link);
134                                 goto allocated;
135                         }
136                 }
137
138                 if (__predict_false(stid != t->nstids)) {
139                         panic("%s: stids TAILQ (%p) corrupt."
140                             "  At %d instead of %d at the end of the queue.",
141                             __func__, &t->stids, stid, t->nstids);
142                 }
143
144                 mtx_unlock(&t->stid_lock);
145                 return (-1);
146         }
147
148 allocated:
149         sr->used = n;
150         sr->free = f;
151         t->stids_in_use += n;
152         t->stid_tab[stid] = lctx;
153         mtx_unlock(&t->stid_lock);
154
155         KASSERT(((stid + t->stid_base) & mask) == 0,
156             ("%s: EDOOFUS.", __func__));
157         return (stid + t->stid_base);
158 }
159
160 static struct listen_ctx *
161 lookup_stid(struct adapter *sc, int stid)
162 {
163         struct tid_info *t = &sc->tids;
164
165         return (t->stid_tab[stid - t->stid_base]);
166 }
167
168 static void
169 free_stid(struct adapter *sc, struct listen_ctx *lctx)
170 {
171         struct tid_info *t = &sc->tids;
172         struct stid_region *sr = &lctx->stid_region;
173         struct stid_region *s;
174
175         KASSERT(sr->used > 0, ("%s: nonsense free (%d)", __func__, sr->used));
176
177         mtx_lock(&t->stid_lock);
178         s = TAILQ_PREV(sr, stid_head, link);
179         if (s != NULL)
180                 s->free += sr->used + sr->free;
181         else
182                 t->nstids_free_head += sr->used + sr->free;
183         KASSERT(t->stids_in_use >= sr->used,
184             ("%s: stids_in_use (%u) < stids being freed (%u)", __func__,
185             t->stids_in_use, sr->used));
186         t->stids_in_use -= sr->used;
187         TAILQ_REMOVE(&t->stids, sr, link);
188         mtx_unlock(&t->stid_lock);
189 }
190
191 static struct listen_ctx *
192 alloc_lctx(struct adapter *sc, struct inpcb *inp, struct vi_info *vi)
193 {
194         struct listen_ctx *lctx;
195
196         INP_WLOCK_ASSERT(inp);
197
198         lctx = malloc(sizeof(struct listen_ctx), M_CXGBE, M_NOWAIT | M_ZERO);
199         if (lctx == NULL)
200                 return (NULL);
201
202         lctx->stid = alloc_stid(sc, lctx, inp->inp_vflag & INP_IPV6);
203         if (lctx->stid < 0) {
204                 free(lctx, M_CXGBE);
205                 return (NULL);
206         }
207
208         if (inp->inp_vflag & INP_IPV6 &&
209             !IN6_ARE_ADDR_EQUAL(&in6addr_any, &inp->in6p_laddr)) {
210                 struct tom_data *td = sc->tom_softc;
211
212                 lctx->ce = hold_lip(td, &inp->in6p_laddr);
213                 if (lctx->ce == NULL) {
214                         free(lctx, M_CXGBE);
215                         return (NULL);
216                 }
217         }
218
219         lctx->ctrlq = &sc->sge.ctrlq[vi->pi->port_id];
220         lctx->ofld_rxq = &sc->sge.ofld_rxq[vi->first_ofld_rxq];
221         refcount_init(&lctx->refcount, 1);
222         TAILQ_INIT(&lctx->synq);
223
224         lctx->inp = inp;
225         in_pcbref(inp);
226
227         return (lctx);
228 }
229
230 /* Don't call this directly, use release_lctx instead */
231 static int
232 free_lctx(struct adapter *sc, struct listen_ctx *lctx)
233 {
234         struct inpcb *inp = lctx->inp;
235         struct tom_data *td = sc->tom_softc;
236
237         INP_WLOCK_ASSERT(inp);
238         KASSERT(lctx->refcount == 0,
239             ("%s: refcount %d", __func__, lctx->refcount));
240         KASSERT(TAILQ_EMPTY(&lctx->synq),
241             ("%s: synq not empty.", __func__));
242         KASSERT(lctx->stid >= 0, ("%s: bad stid %d.", __func__, lctx->stid));
243
244         CTR4(KTR_CXGBE, "%s: stid %u, lctx %p, inp %p",
245             __func__, lctx->stid, lctx, lctx->inp);
246
247         if (lctx->ce)
248                 release_lip(td, lctx->ce);
249         free_stid(sc, lctx);
250         free(lctx, M_CXGBE);
251
252         return (in_pcbrele_wlocked(inp));
253 }
254
255 static void
256 hold_lctx(struct listen_ctx *lctx)
257 {
258
259         refcount_acquire(&lctx->refcount);
260 }
261
262 static inline uint32_t
263 listen_hashfn(void *key, u_long mask)
264 {
265
266         return (fnv_32_buf(&key, sizeof(key), FNV1_32_INIT) & mask);
267 }
268
269 /*
270  * Add a listen_ctx entry to the listen hash table.
271  */
272 static void
273 listen_hash_add(struct adapter *sc, struct listen_ctx *lctx)
274 {
275         struct tom_data *td = sc->tom_softc;
276         int bucket = listen_hashfn(lctx->inp, td->listen_mask);
277
278         mtx_lock(&td->lctx_hash_lock);
279         LIST_INSERT_HEAD(&td->listen_hash[bucket], lctx, link);
280         td->lctx_count++;
281         mtx_unlock(&td->lctx_hash_lock);
282 }
283
284 /*
285  * Look for the listening socket's context entry in the hash and return it.
286  */
287 static struct listen_ctx *
288 listen_hash_find(struct adapter *sc, struct inpcb *inp)
289 {
290         struct tom_data *td = sc->tom_softc;
291         int bucket = listen_hashfn(inp, td->listen_mask);
292         struct listen_ctx *lctx;
293
294         mtx_lock(&td->lctx_hash_lock);
295         LIST_FOREACH(lctx, &td->listen_hash[bucket], link) {
296                 if (lctx->inp == inp)
297                         break;
298         }
299         mtx_unlock(&td->lctx_hash_lock);
300
301         return (lctx);
302 }
303
304 /*
305  * Removes the listen_ctx structure for inp from the hash and returns it.
306  */
307 static struct listen_ctx *
308 listen_hash_del(struct adapter *sc, struct inpcb *inp)
309 {
310         struct tom_data *td = sc->tom_softc;
311         int bucket = listen_hashfn(inp, td->listen_mask);
312         struct listen_ctx *lctx, *l;
313
314         mtx_lock(&td->lctx_hash_lock);
315         LIST_FOREACH_SAFE(lctx, &td->listen_hash[bucket], link, l) {
316                 if (lctx->inp == inp) {
317                         LIST_REMOVE(lctx, link);
318                         td->lctx_count--;
319                         break;
320                 }
321         }
322         mtx_unlock(&td->lctx_hash_lock);
323
324         return (lctx);
325 }
326
327 /*
328  * Releases a hold on the lctx.  Must be called with the listening socket's inp
329  * locked.  The inp may be freed by this function and it returns NULL to
330  * indicate this.
331  */
332 static struct inpcb *
333 release_lctx(struct adapter *sc, struct listen_ctx *lctx)
334 {
335         struct inpcb *inp = lctx->inp;
336         int inp_freed = 0;
337
338         INP_WLOCK_ASSERT(inp);
339         if (refcount_release(&lctx->refcount))
340                 inp_freed = free_lctx(sc, lctx);
341
342         return (inp_freed ? NULL : inp);
343 }
344
345 static void
346 send_reset_synqe(struct toedev *tod, struct synq_entry *synqe)
347 {
348         struct adapter *sc = tod->tod_softc;
349         struct mbuf *m = synqe->syn;
350         struct ifnet *ifp = m->m_pkthdr.rcvif;
351         struct vi_info *vi = ifp->if_softc;
352         struct port_info *pi = vi->pi;
353         struct l2t_entry *e = &sc->l2t->l2tab[synqe->l2e_idx];
354         struct wrqe *wr;
355         struct fw_flowc_wr *flowc;
356         struct cpl_abort_req *req;
357         int txqid, rxqid, flowclen;
358         struct sge_wrq *ofld_txq;
359         struct sge_ofld_rxq *ofld_rxq;
360         const int nparams = 6;
361         unsigned int pfvf = G_FW_VIID_PFN(vi->viid) << S_FW_VIID_PFN;
362
363         INP_WLOCK_ASSERT(synqe->lctx->inp);
364
365         CTR5(KTR_CXGBE, "%s: synqe %p (0x%x), tid %d%s",
366             __func__, synqe, synqe->flags, synqe->tid,
367             synqe->flags & TPF_ABORT_SHUTDOWN ?
368             " (abort already in progress)" : "");
369         if (synqe->flags & TPF_ABORT_SHUTDOWN)
370                 return; /* abort already in progress */
371         synqe->flags |= TPF_ABORT_SHUTDOWN;
372
373         get_qids_from_mbuf(m, &txqid, &rxqid);
374         ofld_txq = &sc->sge.ofld_txq[txqid];
375         ofld_rxq = &sc->sge.ofld_rxq[rxqid];
376
377         /* The wrqe will have two WRs - a flowc followed by an abort_req */
378         flowclen = sizeof(*flowc) + nparams * sizeof(struct fw_flowc_mnemval);
379
380         wr = alloc_wrqe(roundup2(flowclen, EQ_ESIZE) + sizeof(*req), ofld_txq);
381         if (wr == NULL) {
382                 /* XXX */
383                 panic("%s: allocation failure.", __func__);
384         }
385         flowc = wrtod(wr);
386         req = (void *)((caddr_t)flowc + roundup2(flowclen, EQ_ESIZE));
387
388         /* First the flowc ... */
389         memset(flowc, 0, wr->wr_len);
390         flowc->op_to_nparams = htobe32(V_FW_WR_OP(FW_FLOWC_WR) |
391             V_FW_FLOWC_WR_NPARAMS(nparams));
392         flowc->flowid_len16 = htonl(V_FW_WR_LEN16(howmany(flowclen, 16)) |
393             V_FW_WR_FLOWID(synqe->tid));
394         flowc->mnemval[0].mnemonic = FW_FLOWC_MNEM_PFNVFN;
395         flowc->mnemval[0].val = htobe32(pfvf);
396         flowc->mnemval[1].mnemonic = FW_FLOWC_MNEM_CH;
397         flowc->mnemval[1].val = htobe32(pi->tx_chan);
398         flowc->mnemval[2].mnemonic = FW_FLOWC_MNEM_PORT;
399         flowc->mnemval[2].val = htobe32(pi->tx_chan);
400         flowc->mnemval[3].mnemonic = FW_FLOWC_MNEM_IQID;
401         flowc->mnemval[3].val = htobe32(ofld_rxq->iq.abs_id);
402         flowc->mnemval[4].mnemonic = FW_FLOWC_MNEM_SNDBUF;
403         flowc->mnemval[4].val = htobe32(512);
404         flowc->mnemval[5].mnemonic = FW_FLOWC_MNEM_MSS;
405         flowc->mnemval[5].val = htobe32(512);
406         synqe->flags |= TPF_FLOWC_WR_SENT;
407
408         /* ... then ABORT request */
409         INIT_TP_WR_MIT_CPL(req, CPL_ABORT_REQ, synqe->tid);
410         req->rsvd0 = 0; /* don't have a snd_nxt */
411         req->rsvd1 = 1; /* no data sent yet */
412         req->cmd = CPL_ABORT_SEND_RST;
413
414         t4_l2t_send(sc, wr, e);
415 }
416
417 static int
418 create_server(struct adapter *sc, struct listen_ctx *lctx)
419 {
420         struct wrqe *wr;
421         struct cpl_pass_open_req *req;
422         struct inpcb *inp = lctx->inp;
423
424         wr = alloc_wrqe(sizeof(*req), lctx->ctrlq);
425         if (wr == NULL) {
426                 log(LOG_ERR, "%s: allocation failure", __func__);
427                 return (ENOMEM);
428         }
429         req = wrtod(wr);
430
431         INIT_TP_WR(req, 0);
432         OPCODE_TID(req) = htobe32(MK_OPCODE_TID(CPL_PASS_OPEN_REQ, lctx->stid));
433         req->local_port = inp->inp_lport;
434         req->peer_port = 0;
435         req->local_ip = inp->inp_laddr.s_addr;
436         req->peer_ip = 0;
437         req->opt0 = htobe64(V_TX_CHAN(lctx->ctrlq->eq.tx_chan));
438         req->opt1 = htobe64(V_CONN_POLICY(CPL_CONN_POLICY_ASK) |
439             F_SYN_RSS_ENABLE | V_SYN_RSS_QUEUE(lctx->ofld_rxq->iq.abs_id));
440
441         t4_wrq_tx(sc, wr);
442         return (0);
443 }
444
445 static int
446 create_server6(struct adapter *sc, struct listen_ctx *lctx)
447 {
448         struct wrqe *wr;
449         struct cpl_pass_open_req6 *req;
450         struct inpcb *inp = lctx->inp;
451
452         wr = alloc_wrqe(sizeof(*req), lctx->ctrlq);
453         if (wr == NULL) {
454                 log(LOG_ERR, "%s: allocation failure", __func__);
455                 return (ENOMEM);
456         }
457         req = wrtod(wr);
458
459         INIT_TP_WR(req, 0);
460         OPCODE_TID(req) = htobe32(MK_OPCODE_TID(CPL_PASS_OPEN_REQ6, lctx->stid));
461         req->local_port = inp->inp_lport;
462         req->peer_port = 0;
463         req->local_ip_hi = *(uint64_t *)&inp->in6p_laddr.s6_addr[0];
464         req->local_ip_lo = *(uint64_t *)&inp->in6p_laddr.s6_addr[8];
465         req->peer_ip_hi = 0;
466         req->peer_ip_lo = 0;
467         req->opt0 = htobe64(V_TX_CHAN(lctx->ctrlq->eq.tx_chan));
468         req->opt1 = htobe64(V_CONN_POLICY(CPL_CONN_POLICY_ASK) |
469             F_SYN_RSS_ENABLE | V_SYN_RSS_QUEUE(lctx->ofld_rxq->iq.abs_id));
470
471         t4_wrq_tx(sc, wr);
472         return (0);
473 }
474
475 static int
476 destroy_server(struct adapter *sc, struct listen_ctx *lctx)
477 {
478         struct wrqe *wr;
479         struct cpl_close_listsvr_req *req;
480
481         wr = alloc_wrqe(sizeof(*req), lctx->ctrlq);
482         if (wr == NULL) {
483                 /* XXX */
484                 panic("%s: allocation failure.", __func__);
485         }
486         req = wrtod(wr);
487
488         INIT_TP_WR(req, 0);
489         OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_CLOSE_LISTSRV_REQ,
490             lctx->stid));
491         req->reply_ctrl = htobe16(lctx->ofld_rxq->iq.abs_id);
492         req->rsvd = htobe16(0);
493
494         t4_wrq_tx(sc, wr);
495         return (0);
496 }
497
498 /*
499  * Start a listening server by sending a passive open request to HW.
500  *
501  * Can't take adapter lock here and access to sc->flags,
502  * sc->offload_map, if_capenable are all race prone.
503  */
504 int
505 t4_listen_start(struct toedev *tod, struct tcpcb *tp)
506 {
507         struct adapter *sc = tod->tod_softc;
508         struct vi_info *vi;
509         struct port_info *pi;
510         struct inpcb *inp = tp->t_inpcb;
511         struct listen_ctx *lctx;
512         int i, rc, v;
513
514         INP_WLOCK_ASSERT(inp);
515
516         /* Don't start a hardware listener for any loopback address. */
517         if (inp->inp_vflag & INP_IPV6 && IN6_IS_ADDR_LOOPBACK(&inp->in6p_laddr))
518                 return (0);
519         if (!(inp->inp_vflag & INP_IPV6) &&
520             IN_LOOPBACK(ntohl(inp->inp_laddr.s_addr)))
521                 return (0);
522 #if 0
523         ADAPTER_LOCK(sc);
524         if (IS_BUSY(sc)) {
525                 log(LOG_ERR, "%s: listen request ignored, %s is busy",
526                     __func__, device_get_nameunit(sc->dev));
527                 goto done;
528         }
529
530         KASSERT(uld_active(sc, ULD_TOM),
531             ("%s: TOM not initialized", __func__));
532 #endif
533
534         /*
535          * Find an initialized VI with IFCAP_TOE (4 or 6).  We'll use the first
536          * such VI's queues to send the passive open and receive the reply to
537          * it.
538          *
539          * XXX: need a way to mark a port in use by offload.  if_cxgbe should
540          * then reject any attempt to bring down such a port (and maybe reject
541          * attempts to disable IFCAP_TOE on that port too?).
542          */
543         for_each_port(sc, i) {
544                 pi = sc->port[i];
545                 for_each_vi(pi, v, vi) {
546                         if (vi->flags & VI_INIT_DONE &&
547                             vi->ifp->if_capenable & IFCAP_TOE)
548                                 goto found;
549                 }
550         }
551         goto done;      /* no port that's UP with IFCAP_TOE enabled */
552 found:
553
554         if (listen_hash_find(sc, inp) != NULL)
555                 goto done;      /* already setup */
556
557         lctx = alloc_lctx(sc, inp, vi);
558         if (lctx == NULL) {
559                 log(LOG_ERR,
560                     "%s: listen request ignored, %s couldn't allocate lctx\n",
561                     __func__, device_get_nameunit(sc->dev));
562                 goto done;
563         }
564         listen_hash_add(sc, lctx);
565
566         CTR6(KTR_CXGBE, "%s: stid %u (%s), lctx %p, inp %p vflag 0x%x",
567             __func__, lctx->stid, tcpstates[tp->t_state], lctx, inp,
568             inp->inp_vflag);
569
570         if (inp->inp_vflag & INP_IPV6)
571                 rc = create_server6(sc, lctx);
572         else
573                 rc = create_server(sc, lctx);
574         if (rc != 0) {
575                 log(LOG_ERR, "%s: %s failed to create hw listener: %d.\n",
576                     __func__, device_get_nameunit(sc->dev), rc);
577                 (void) listen_hash_del(sc, inp);
578                 inp = release_lctx(sc, lctx);
579                 /* can't be freed, host stack has a reference */
580                 KASSERT(inp != NULL, ("%s: inp freed", __func__));
581                 goto done;
582         }
583         lctx->flags |= LCTX_RPL_PENDING;
584 done:
585 #if 0
586         ADAPTER_UNLOCK(sc);
587 #endif
588         return (0);
589 }
590
591 int
592 t4_listen_stop(struct toedev *tod, struct tcpcb *tp)
593 {
594         struct listen_ctx *lctx;
595         struct adapter *sc = tod->tod_softc;
596         struct inpcb *inp = tp->t_inpcb;
597         struct synq_entry *synqe;
598
599         INP_WLOCK_ASSERT(inp);
600
601         lctx = listen_hash_del(sc, inp);
602         if (lctx == NULL)
603                 return (ENOENT);        /* no hardware listener for this inp */
604
605         CTR4(KTR_CXGBE, "%s: stid %u, lctx %p, flags %x", __func__, lctx->stid,
606             lctx, lctx->flags);
607
608         /*
609          * If the reply to the PASS_OPEN is still pending we'll wait for it to
610          * arrive and clean up when it does.
611          */
612         if (lctx->flags & LCTX_RPL_PENDING) {
613                 KASSERT(TAILQ_EMPTY(&lctx->synq),
614                     ("%s: synq not empty.", __func__));
615                 return (EINPROGRESS);
616         }
617
618         /*
619          * The host stack will abort all the connections on the listening
620          * socket's so_comp.  It doesn't know about the connections on the synq
621          * so we need to take care of those.
622          */
623         TAILQ_FOREACH(synqe, &lctx->synq, link) {
624                 if (synqe->flags & TPF_SYNQE_HAS_L2TE)
625                         send_reset_synqe(tod, synqe);
626         }
627
628         destroy_server(sc, lctx);
629         return (0);
630 }
631
632 static inline void
633 hold_synqe(struct synq_entry *synqe)
634 {
635
636         refcount_acquire(&synqe->refcnt);
637 }
638
639 static inline void
640 release_synqe(struct synq_entry *synqe)
641 {
642
643         if (refcount_release(&synqe->refcnt)) {
644                 int needfree = synqe->flags & TPF_SYNQE_NEEDFREE;
645
646                 m_freem(synqe->syn);
647                 if (needfree)
648                         free(synqe, M_CXGBE);
649         }
650 }
651
652 void
653 t4_syncache_added(struct toedev *tod __unused, void *arg)
654 {
655         struct synq_entry *synqe = arg;
656
657         hold_synqe(synqe);
658 }
659
660 void
661 t4_syncache_removed(struct toedev *tod __unused, void *arg)
662 {
663         struct synq_entry *synqe = arg;
664
665         release_synqe(synqe);
666 }
667
668 int
669 t4_syncache_respond(struct toedev *tod, void *arg, struct mbuf *m)
670 {
671         struct adapter *sc = tod->tod_softc;
672         struct synq_entry *synqe = arg;
673         struct wrqe *wr;
674         struct l2t_entry *e;
675         struct tcpopt to;
676         struct ip *ip = mtod(m, struct ip *);
677         struct tcphdr *th;
678
679         wr = (struct wrqe *)atomic_readandclear_ptr(&synqe->wr);
680         if (wr == NULL) {
681                 m_freem(m);
682                 return (EALREADY);
683         }
684
685         if (ip->ip_v == IPVERSION)
686                 th = (void *)(ip + 1);
687         else
688                 th = (void *)((struct ip6_hdr *)ip + 1);
689         bzero(&to, sizeof(to));
690         tcp_dooptions(&to, (void *)(th + 1), (th->th_off << 2) - sizeof(*th),
691             TO_SYN);
692
693         /* save these for later */
694         synqe->iss = be32toh(th->th_seq);
695         synqe->ts = to.to_tsval;
696
697         if (chip_id(sc) >= CHELSIO_T5) {
698                 struct cpl_t5_pass_accept_rpl *rpl5 = wrtod(wr);
699
700                 rpl5->iss = th->th_seq;
701         }
702
703         e = &sc->l2t->l2tab[synqe->l2e_idx];
704         t4_l2t_send(sc, wr, e);
705
706         m_freem(m);     /* don't need this any more */
707         return (0);
708 }
709
710 static int
711 do_pass_open_rpl(struct sge_iq *iq, const struct rss_header *rss,
712     struct mbuf *m)
713 {
714         struct adapter *sc = iq->adapter;
715         const struct cpl_pass_open_rpl *cpl = (const void *)(rss + 1);
716         int stid = GET_TID(cpl);
717         unsigned int status = cpl->status;
718         struct listen_ctx *lctx = lookup_stid(sc, stid);
719         struct inpcb *inp = lctx->inp;
720 #ifdef INVARIANTS
721         unsigned int opcode = G_CPL_OPCODE(be32toh(OPCODE_TID(cpl)));
722 #endif
723
724         KASSERT(opcode == CPL_PASS_OPEN_RPL,
725             ("%s: unexpected opcode 0x%x", __func__, opcode));
726         KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
727         KASSERT(lctx->stid == stid, ("%s: lctx stid mismatch", __func__));
728
729         INP_WLOCK(inp);
730
731         CTR4(KTR_CXGBE, "%s: stid %d, status %u, flags 0x%x",
732             __func__, stid, status, lctx->flags);
733
734         lctx->flags &= ~LCTX_RPL_PENDING;
735
736         if (status != CPL_ERR_NONE)
737                 log(LOG_ERR, "listener (stid %u) failed: %d\n", stid, status);
738
739 #ifdef INVARIANTS
740         /*
741          * If the inp has been dropped (listening socket closed) then
742          * listen_stop must have run and taken the inp out of the hash.
743          */
744         if (inp->inp_flags & INP_DROPPED) {
745                 KASSERT(listen_hash_del(sc, inp) == NULL,
746                     ("%s: inp %p still in listen hash", __func__, inp));
747         }
748 #endif
749
750         if (inp->inp_flags & INP_DROPPED && status != CPL_ERR_NONE) {
751                 if (release_lctx(sc, lctx) != NULL)
752                         INP_WUNLOCK(inp);
753                 return (status);
754         }
755
756         /*
757          * Listening socket stopped listening earlier and now the chip tells us
758          * it has started the hardware listener.  Stop it; the lctx will be
759          * released in do_close_server_rpl.
760          */
761         if (inp->inp_flags & INP_DROPPED) {
762                 destroy_server(sc, lctx);
763                 INP_WUNLOCK(inp);
764                 return (status);
765         }
766
767         /*
768          * Failed to start hardware listener.  Take inp out of the hash and
769          * release our reference on it.  An error message has been logged
770          * already.
771          */
772         if (status != CPL_ERR_NONE) {
773                 listen_hash_del(sc, inp);
774                 if (release_lctx(sc, lctx) != NULL)
775                         INP_WUNLOCK(inp);
776                 return (status);
777         }
778
779         /* hardware listener open for business */
780
781         INP_WUNLOCK(inp);
782         return (status);
783 }
784
785 static int
786 do_close_server_rpl(struct sge_iq *iq, const struct rss_header *rss,
787     struct mbuf *m)
788 {
789         struct adapter *sc = iq->adapter;
790         const struct cpl_close_listsvr_rpl *cpl = (const void *)(rss + 1);
791         int stid = GET_TID(cpl);
792         unsigned int status = cpl->status;
793         struct listen_ctx *lctx = lookup_stid(sc, stid);
794         struct inpcb *inp = lctx->inp;
795 #ifdef INVARIANTS
796         unsigned int opcode = G_CPL_OPCODE(be32toh(OPCODE_TID(cpl)));
797 #endif
798
799         KASSERT(opcode == CPL_CLOSE_LISTSRV_RPL,
800             ("%s: unexpected opcode 0x%x", __func__, opcode));
801         KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
802         KASSERT(lctx->stid == stid, ("%s: lctx stid mismatch", __func__));
803
804         CTR3(KTR_CXGBE, "%s: stid %u, status %u", __func__, stid, status);
805
806         if (status != CPL_ERR_NONE) {
807                 log(LOG_ERR, "%s: failed (%u) to close listener for stid %u\n",
808                     __func__, status, stid);
809                 return (status);
810         }
811
812         INP_WLOCK(inp);
813         inp = release_lctx(sc, lctx);
814         if (inp != NULL)
815                 INP_WUNLOCK(inp);
816
817         return (status);
818 }
819
820 static void
821 done_with_synqe(struct adapter *sc, struct synq_entry *synqe)
822 {
823         struct listen_ctx *lctx = synqe->lctx;
824         struct inpcb *inp = lctx->inp;
825         struct vi_info *vi = synqe->syn->m_pkthdr.rcvif->if_softc;
826         struct l2t_entry *e = &sc->l2t->l2tab[synqe->l2e_idx];
827         int ntids;
828
829         INP_WLOCK_ASSERT(inp);
830         ntids = inp->inp_vflag & INP_IPV6 ? 2 : 1;
831
832         TAILQ_REMOVE(&lctx->synq, synqe, link);
833         inp = release_lctx(sc, lctx);
834         if (inp)
835                 INP_WUNLOCK(inp);
836         remove_tid(sc, synqe->tid, ntids);
837         release_tid(sc, synqe->tid, &sc->sge.ctrlq[vi->pi->port_id]);
838         t4_l2t_release(e);
839         release_synqe(synqe);   /* removed from synq list */
840 }
841
842 int
843 do_abort_req_synqe(struct sge_iq *iq, const struct rss_header *rss,
844     struct mbuf *m)
845 {
846         struct adapter *sc = iq->adapter;
847         const struct cpl_abort_req_rss *cpl = (const void *)(rss + 1);
848         unsigned int tid = GET_TID(cpl);
849         struct synq_entry *synqe = lookup_tid(sc, tid);
850         struct listen_ctx *lctx = synqe->lctx;
851         struct inpcb *inp = lctx->inp;
852         int txqid;
853         struct sge_wrq *ofld_txq;
854 #ifdef INVARIANTS
855         unsigned int opcode = G_CPL_OPCODE(be32toh(OPCODE_TID(cpl)));
856 #endif
857
858         KASSERT(opcode == CPL_ABORT_REQ_RSS,
859             ("%s: unexpected opcode 0x%x", __func__, opcode));
860         KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
861         KASSERT(synqe->tid == tid, ("%s: toep tid mismatch", __func__));
862
863         CTR6(KTR_CXGBE, "%s: tid %u, synqe %p (0x%x), lctx %p, status %d",
864             __func__, tid, synqe, synqe->flags, synqe->lctx, cpl->status);
865
866         if (negative_advice(cpl->status))
867                 return (0);     /* Ignore negative advice */
868
869         INP_WLOCK(inp);
870
871         get_qids_from_mbuf(synqe->syn, &txqid, NULL);
872         ofld_txq = &sc->sge.ofld_txq[txqid];
873
874         /*
875          * If we'd initiated an abort earlier the reply to it is responsible for
876          * cleaning up resources.  Otherwise we tear everything down right here
877          * right now.  We owe the T4 a CPL_ABORT_RPL no matter what.
878          */
879         if (synqe->flags & TPF_ABORT_SHUTDOWN) {
880                 INP_WUNLOCK(inp);
881                 goto done;
882         }
883
884         done_with_synqe(sc, synqe);
885         /* inp lock released by done_with_synqe */
886 done:
887         send_abort_rpl(sc, ofld_txq, tid, CPL_ABORT_NO_RST);
888         return (0);
889 }
890
891 int
892 do_abort_rpl_synqe(struct sge_iq *iq, const struct rss_header *rss,
893     struct mbuf *m)
894 {
895         struct adapter *sc = iq->adapter;
896         const struct cpl_abort_rpl_rss *cpl = (const void *)(rss + 1);
897         unsigned int tid = GET_TID(cpl);
898         struct synq_entry *synqe = lookup_tid(sc, tid);
899         struct listen_ctx *lctx = synqe->lctx;
900         struct inpcb *inp = lctx->inp;
901 #ifdef INVARIANTS
902         unsigned int opcode = G_CPL_OPCODE(be32toh(OPCODE_TID(cpl)));
903 #endif
904
905         KASSERT(opcode == CPL_ABORT_RPL_RSS,
906             ("%s: unexpected opcode 0x%x", __func__, opcode));
907         KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
908         KASSERT(synqe->tid == tid, ("%s: toep tid mismatch", __func__));
909
910         CTR6(KTR_CXGBE, "%s: tid %u, synqe %p (0x%x), lctx %p, status %d",
911             __func__, tid, synqe, synqe->flags, synqe->lctx, cpl->status);
912
913         INP_WLOCK(inp);
914         KASSERT(synqe->flags & TPF_ABORT_SHUTDOWN,
915             ("%s: wasn't expecting abort reply for synqe %p (0x%x)",
916             __func__, synqe, synqe->flags));
917
918         done_with_synqe(sc, synqe);
919         /* inp lock released by done_with_synqe */
920
921         return (0);
922 }
923
924 void
925 t4_offload_socket(struct toedev *tod, void *arg, struct socket *so)
926 {
927         struct adapter *sc = tod->tod_softc;
928         struct synq_entry *synqe = arg;
929 #ifdef INVARIANTS
930         struct inpcb *inp = sotoinpcb(so);
931 #endif
932         struct cpl_pass_establish *cpl = mtod(synqe->syn, void *);
933         struct toepcb *toep = *(struct toepcb **)(cpl + 1);
934
935         INP_INFO_RLOCK_ASSERT(&V_tcbinfo); /* prevents bad race with accept() */
936         INP_WLOCK_ASSERT(inp);
937         KASSERT(synqe->flags & TPF_SYNQE,
938             ("%s: %p not a synq_entry?", __func__, arg));
939
940         offload_socket(so, toep);
941         make_established(toep, cpl->snd_isn, cpl->rcv_isn, cpl->tcp_opt);
942         toep->flags |= TPF_CPL_PENDING;
943         update_tid(sc, synqe->tid, toep);
944         synqe->flags |= TPF_SYNQE_EXPANDED;
945 }
946
947 static inline void
948 save_qids_in_mbuf(struct mbuf *m, struct vi_info *vi)
949 {
950         uint32_t txqid, rxqid;
951
952         txqid = (arc4random() % vi->nofldtxq) + vi->first_ofld_txq;
953         rxqid = (arc4random() % vi->nofldrxq) + vi->first_ofld_rxq;
954
955         m->m_pkthdr.flowid = (txqid << 16) | (rxqid & 0xffff);
956 }
957
958 static inline void
959 get_qids_from_mbuf(struct mbuf *m, int *txqid, int *rxqid)
960 {
961
962         if (txqid)
963                 *txqid = m->m_pkthdr.flowid >> 16;
964         if (rxqid)
965                 *rxqid = m->m_pkthdr.flowid & 0xffff;
966 }
967
968 /*
969  * Use the trailing space in the mbuf in which the PASS_ACCEPT_REQ arrived to
970  * store some state temporarily.
971  */
972 static struct synq_entry *
973 mbuf_to_synqe(struct mbuf *m)
974 {
975         int len = roundup2(sizeof (struct synq_entry), 8);
976         int tspace = M_TRAILINGSPACE(m);
977         struct synq_entry *synqe = NULL;
978
979         if (tspace < len) {
980                 synqe = malloc(sizeof(*synqe), M_CXGBE, M_NOWAIT);
981                 if (synqe == NULL)
982                         return (NULL);
983                 synqe->flags = TPF_SYNQE | TPF_SYNQE_NEEDFREE;
984         } else {
985                 synqe = (void *)(m->m_data + m->m_len + tspace - len);
986                 synqe->flags = TPF_SYNQE;
987         }
988
989         return (synqe);
990 }
991
992 static void
993 t4opt_to_tcpopt(const struct tcp_options *t4opt, struct tcpopt *to)
994 {
995         bzero(to, sizeof(*to));
996
997         if (t4opt->mss) {
998                 to->to_flags |= TOF_MSS;
999                 to->to_mss = be16toh(t4opt->mss);
1000         }
1001
1002         if (t4opt->wsf) {
1003                 to->to_flags |= TOF_SCALE;
1004                 to->to_wscale = t4opt->wsf;
1005         }
1006
1007         if (t4opt->tstamp)
1008                 to->to_flags |= TOF_TS;
1009
1010         if (t4opt->sack)
1011                 to->to_flags |= TOF_SACKPERM;
1012 }
1013
1014 /*
1015  * Options2 for passive open.
1016  */
1017 static uint32_t
1018 calc_opt2p(struct adapter *sc, struct port_info *pi, int rxqid,
1019     const struct tcp_options *tcpopt, struct tcphdr *th, int ulp_mode)
1020 {
1021         struct sge_ofld_rxq *ofld_rxq = &sc->sge.ofld_rxq[rxqid];
1022         uint32_t opt2;
1023
1024         opt2 = V_TX_QUEUE(sc->params.tp.tx_modq[pi->tx_chan]) |
1025             F_RSS_QUEUE_VALID | V_RSS_QUEUE(ofld_rxq->iq.abs_id);
1026
1027         if (V_tcp_do_rfc1323) {
1028                 if (tcpopt->tstamp)
1029                         opt2 |= F_TSTAMPS_EN;
1030                 if (tcpopt->sack)
1031                         opt2 |= F_SACK_EN;
1032                 if (tcpopt->wsf <= 14)
1033                         opt2 |= F_WND_SCALE_EN;
1034         }
1035
1036         if (V_tcp_do_ecn && th->th_flags & (TH_ECE | TH_CWR))
1037                 opt2 |= F_CCTRL_ECN;
1038
1039         /* RX_COALESCE is always a valid value (0 or M_RX_COALESCE). */
1040         if (is_t4(sc))
1041                 opt2 |= F_RX_COALESCE_VALID;
1042         else {
1043                 opt2 |= F_T5_OPT_2_VALID;
1044                 opt2 |= F_CONG_CNTRL_VALID; /* OPT_2_ISS really, for T5 */
1045         }
1046         if (sc->tt.rx_coalesce)
1047                 opt2 |= V_RX_COALESCE(M_RX_COALESCE);
1048
1049 #ifdef USE_DDP_RX_FLOW_CONTROL
1050         if (ulp_mode == ULP_MODE_TCPDDP)
1051                 opt2 |= F_RX_FC_VALID | F_RX_FC_DDP;
1052 #endif
1053
1054         return htobe32(opt2);
1055 }
1056
1057 static void
1058 pass_accept_req_to_protohdrs(struct adapter *sc, const struct mbuf *m,
1059     struct in_conninfo *inc, struct tcphdr *th)
1060 {
1061         const struct cpl_pass_accept_req *cpl = mtod(m, const void *);
1062         const struct ether_header *eh;
1063         unsigned int hlen = be32toh(cpl->hdr_len);
1064         uintptr_t l3hdr;
1065         const struct tcphdr *tcp;
1066
1067         eh = (const void *)(cpl + 1);
1068         if (chip_id(sc) >= CHELSIO_T6) {
1069                 l3hdr = ((uintptr_t)eh + G_T6_ETH_HDR_LEN(hlen));
1070                 tcp = (const void *)(l3hdr + G_T6_IP_HDR_LEN(hlen));
1071         } else {
1072                 l3hdr = ((uintptr_t)eh + G_ETH_HDR_LEN(hlen));
1073                 tcp = (const void *)(l3hdr + G_IP_HDR_LEN(hlen));
1074         }
1075
1076         if (inc) {
1077                 bzero(inc, sizeof(*inc));
1078                 inc->inc_fport = tcp->th_sport;
1079                 inc->inc_lport = tcp->th_dport;
1080                 if (((struct ip *)l3hdr)->ip_v == IPVERSION) {
1081                         const struct ip *ip = (const void *)l3hdr;
1082
1083                         inc->inc_faddr = ip->ip_src;
1084                         inc->inc_laddr = ip->ip_dst;
1085                 } else {
1086                         const struct ip6_hdr *ip6 = (const void *)l3hdr;
1087
1088                         inc->inc_flags |= INC_ISIPV6;
1089                         inc->inc6_faddr = ip6->ip6_src;
1090                         inc->inc6_laddr = ip6->ip6_dst;
1091                 }
1092         }
1093
1094         if (th) {
1095                 bcopy(tcp, th, sizeof(*th));
1096                 tcp_fields_to_host(th);         /* just like tcp_input */
1097         }
1098 }
1099
1100 static struct l2t_entry *
1101 get_l2te_for_nexthop(struct port_info *pi, struct ifnet *ifp,
1102     struct in_conninfo *inc)
1103 {
1104         struct l2t_entry *e;
1105         struct sockaddr_in6 sin6;
1106         struct sockaddr *dst = (void *)&sin6;
1107  
1108         if (inc->inc_flags & INC_ISIPV6) {
1109                 struct nhop6_basic nh6;
1110
1111                 bzero(dst, sizeof(struct sockaddr_in6));
1112                 dst->sa_len = sizeof(struct sockaddr_in6);
1113                 dst->sa_family = AF_INET6;
1114
1115                 if (IN6_IS_ADDR_LINKLOCAL(&inc->inc6_laddr)) {
1116                         /* no need for route lookup */
1117                         e = t4_l2t_get(pi, ifp, dst);
1118                         return (e);
1119                 }
1120
1121                 if (fib6_lookup_nh_basic(RT_DEFAULT_FIB, &inc->inc6_faddr,
1122                     0, 0, 0, &nh6) != 0)
1123                         return (NULL);
1124                 if (nh6.nh_ifp != ifp)
1125                         return (NULL);
1126                 ((struct sockaddr_in6 *)dst)->sin6_addr = nh6.nh_addr;
1127         } else {
1128                 struct nhop4_basic nh4;
1129
1130                 dst->sa_len = sizeof(struct sockaddr_in);
1131                 dst->sa_family = AF_INET;
1132
1133                 if (fib4_lookup_nh_basic(RT_DEFAULT_FIB, inc->inc_faddr, 0, 0,
1134                     &nh4) != 0)
1135                         return (NULL);
1136                 if (nh4.nh_ifp != ifp)
1137                         return (NULL);
1138                 ((struct sockaddr_in *)dst)->sin_addr = nh4.nh_addr;
1139         }
1140
1141         e = t4_l2t_get(pi, ifp, dst);
1142         return (e);
1143 }
1144
1145 #define REJECT_PASS_ACCEPT()    do { \
1146         reject_reason = __LINE__; \
1147         goto reject; \
1148 } while (0)
1149
1150 /*
1151  * The context associated with a tid entry via insert_tid could be a synq_entry
1152  * or a toepcb.  The only way CPL handlers can tell is via a bit in these flags.
1153  */
1154 CTASSERT(offsetof(struct toepcb, flags) == offsetof(struct synq_entry, flags));
1155
1156 /*
1157  * Incoming SYN on a listening socket.
1158  *
1159  * XXX: Every use of ifp in this routine has a bad race with up/down, toe/-toe,
1160  * etc.
1161  */
1162 static int
1163 do_pass_accept_req(struct sge_iq *iq, const struct rss_header *rss,
1164     struct mbuf *m)
1165 {
1166         struct adapter *sc = iq->adapter;
1167         struct toedev *tod;
1168         const struct cpl_pass_accept_req *cpl = mtod(m, const void *);
1169         struct cpl_pass_accept_rpl *rpl;
1170         struct wrqe *wr;
1171         unsigned int stid = G_PASS_OPEN_TID(be32toh(cpl->tos_stid));
1172         unsigned int tid = GET_TID(cpl);
1173         struct listen_ctx *lctx = lookup_stid(sc, stid);
1174         struct inpcb *inp;
1175         struct socket *so;
1176         struct in_conninfo inc;
1177         struct tcphdr th;
1178         struct tcpopt to;
1179         struct port_info *pi;
1180         struct vi_info *vi;
1181         struct ifnet *hw_ifp, *ifp;
1182         struct l2t_entry *e = NULL;
1183         int rscale, mtu_idx, rx_credits, rxqid, ulp_mode;
1184         struct synq_entry *synqe = NULL;
1185         int reject_reason, v, ntids;
1186         uint16_t vid;
1187 #ifdef INVARIANTS
1188         unsigned int opcode = G_CPL_OPCODE(be32toh(OPCODE_TID(cpl)));
1189 #endif
1190
1191         KASSERT(opcode == CPL_PASS_ACCEPT_REQ,
1192             ("%s: unexpected opcode 0x%x", __func__, opcode));
1193         KASSERT(lctx->stid == stid, ("%s: lctx stid mismatch", __func__));
1194
1195         CTR4(KTR_CXGBE, "%s: stid %u, tid %u, lctx %p", __func__, stid, tid,
1196             lctx);
1197
1198         pass_accept_req_to_protohdrs(sc, m, &inc, &th);
1199         t4opt_to_tcpopt(&cpl->tcpopt, &to);
1200
1201         pi = sc->port[G_SYN_INTF(be16toh(cpl->l2info))];
1202
1203         /*
1204          * Use the MAC index to lookup the associated VI.  If this SYN
1205          * didn't match a perfect MAC filter, punt.
1206          */
1207         if (!(be16toh(cpl->l2info) & F_SYN_XACT_MATCH)) {
1208                 m_freem(m);
1209                 m = NULL;
1210                 REJECT_PASS_ACCEPT();
1211         }
1212         for_each_vi(pi, v, vi) {
1213                 if (vi->xact_addr_filt == G_SYN_MAC_IDX(be16toh(cpl->l2info)))
1214                         goto found;
1215         }
1216         m_freem(m);
1217         m = NULL;
1218         REJECT_PASS_ACCEPT();
1219
1220 found:
1221         hw_ifp = vi->ifp;       /* the (v)cxgbeX ifnet */
1222         m->m_pkthdr.rcvif = hw_ifp;
1223         tod = TOEDEV(hw_ifp);
1224
1225         /*
1226          * Figure out if there is a pseudo interface (vlan, lagg, etc.)
1227          * involved.  Don't offload if the SYN had a VLAN tag and the vid
1228          * doesn't match anything on this interface.
1229          *
1230          * XXX: lagg support, lagg + vlan support.
1231          */
1232         vid = EVL_VLANOFTAG(be16toh(cpl->vlan));
1233         if (vid != 0xfff) {
1234                 ifp = VLAN_DEVAT(hw_ifp, vid);
1235                 if (ifp == NULL)
1236                         REJECT_PASS_ACCEPT();
1237         } else
1238                 ifp = hw_ifp;
1239
1240         /*
1241          * Don't offload if the peer requested a TCP option that's not known to
1242          * the silicon.
1243          */
1244         if (cpl->tcpopt.unknown)
1245                 REJECT_PASS_ACCEPT();
1246
1247         if (inc.inc_flags & INC_ISIPV6) {
1248
1249                 /* Don't offload if the ifcap isn't enabled */
1250                 if ((ifp->if_capenable & IFCAP_TOE6) == 0)
1251                         REJECT_PASS_ACCEPT();
1252
1253                 /*
1254                  * SYN must be directed to an IP6 address on this ifnet.  This
1255                  * is more restrictive than in6_localip.
1256                  */
1257                 if (!in6_ifhasaddr(ifp, &inc.inc6_laddr))
1258                         REJECT_PASS_ACCEPT();
1259
1260                 ntids = 2;
1261         } else {
1262
1263                 /* Don't offload if the ifcap isn't enabled */
1264                 if ((ifp->if_capenable & IFCAP_TOE4) == 0)
1265                         REJECT_PASS_ACCEPT();
1266
1267                 /*
1268                  * SYN must be directed to an IP address on this ifnet.  This
1269                  * is more restrictive than in_localip.
1270                  */
1271                 if (!in_ifhasaddr(ifp, inc.inc_laddr))
1272                         REJECT_PASS_ACCEPT();
1273
1274                 ntids = 1;
1275         }
1276
1277         e = get_l2te_for_nexthop(pi, ifp, &inc);
1278         if (e == NULL)
1279                 REJECT_PASS_ACCEPT();
1280
1281         synqe = mbuf_to_synqe(m);
1282         if (synqe == NULL)
1283                 REJECT_PASS_ACCEPT();
1284
1285         wr = alloc_wrqe(is_t4(sc) ? sizeof(struct cpl_pass_accept_rpl) :
1286             sizeof(struct cpl_t5_pass_accept_rpl), &sc->sge.ctrlq[pi->port_id]);
1287         if (wr == NULL)
1288                 REJECT_PASS_ACCEPT();
1289         rpl = wrtod(wr);
1290
1291         INP_INFO_RLOCK(&V_tcbinfo);     /* for 4-tuple check */
1292
1293         /* Don't offload if the 4-tuple is already in use */
1294         if (toe_4tuple_check(&inc, &th, ifp) != 0) {
1295                 INP_INFO_RUNLOCK(&V_tcbinfo);
1296                 free(wr, M_CXGBE);
1297                 REJECT_PASS_ACCEPT();
1298         }
1299         INP_INFO_RUNLOCK(&V_tcbinfo);
1300
1301         inp = lctx->inp;                /* listening socket, not owned by TOE */
1302         INP_WLOCK(inp);
1303
1304         /* Don't offload if the listening socket has closed */
1305         if (__predict_false(inp->inp_flags & INP_DROPPED)) {
1306                 /*
1307                  * The listening socket has closed.  The reply from the TOE to
1308                  * our CPL_CLOSE_LISTSRV_REQ will ultimately release all
1309                  * resources tied to this listen context.
1310                  */
1311                 INP_WUNLOCK(inp);
1312                 free(wr, M_CXGBE);
1313                 REJECT_PASS_ACCEPT();
1314         }
1315         so = inp->inp_socket;
1316         CURVNET_SET(so->so_vnet);
1317
1318         mtu_idx = find_best_mtu_idx(sc, &inc, be16toh(cpl->tcpopt.mss));
1319         rscale = cpl->tcpopt.wsf && V_tcp_do_rfc1323 ? select_rcv_wscale() : 0;
1320         SOCKBUF_LOCK(&so->so_rcv);
1321         /* opt0 rcv_bufsiz initially, assumes its normal meaning later */
1322         rx_credits = min(select_rcv_wnd(so) >> 10, M_RCV_BUFSIZ);
1323         SOCKBUF_UNLOCK(&so->so_rcv);
1324
1325         save_qids_in_mbuf(m, vi);
1326         get_qids_from_mbuf(m, NULL, &rxqid);
1327
1328         if (is_t4(sc))
1329                 INIT_TP_WR_MIT_CPL(rpl, CPL_PASS_ACCEPT_RPL, tid);
1330         else {
1331                 struct cpl_t5_pass_accept_rpl *rpl5 = (void *)rpl;
1332
1333                 INIT_TP_WR_MIT_CPL(rpl5, CPL_PASS_ACCEPT_RPL, tid);
1334         }
1335         if (sc->tt.ddp && (so->so_options & SO_NO_DDP) == 0) {
1336                 ulp_mode = ULP_MODE_TCPDDP;
1337                 synqe->flags |= TPF_SYNQE_TCPDDP;
1338         } else
1339                 ulp_mode = ULP_MODE_NONE;
1340         rpl->opt0 = calc_opt0(so, vi, e, mtu_idx, rscale, rx_credits, ulp_mode);
1341         rpl->opt2 = calc_opt2p(sc, pi, rxqid, &cpl->tcpopt, &th, ulp_mode);
1342
1343         synqe->tid = tid;
1344         synqe->lctx = lctx;
1345         synqe->syn = m;
1346         m = NULL;
1347         refcount_init(&synqe->refcnt, 1);       /* 1 means extra hold */
1348         synqe->l2e_idx = e->idx;
1349         synqe->rcv_bufsize = rx_credits;
1350         atomic_store_rel_ptr(&synqe->wr, (uintptr_t)wr);
1351
1352         insert_tid(sc, tid, synqe, ntids);
1353         TAILQ_INSERT_TAIL(&lctx->synq, synqe, link);
1354         hold_synqe(synqe);      /* hold for the duration it's in the synq */
1355         hold_lctx(lctx);        /* A synqe on the list has a ref on its lctx */
1356
1357         /*
1358          * If all goes well t4_syncache_respond will get called during
1359          * syncache_add.  Note that syncache_add releases the pcb lock.
1360          */
1361         toe_syncache_add(&inc, &to, &th, inp, tod, synqe);
1362         INP_UNLOCK_ASSERT(inp); /* ok to assert, we have a ref on the inp */
1363         CURVNET_RESTORE();
1364
1365         /*
1366          * If we replied during syncache_add (synqe->wr has been consumed),
1367          * good.  Otherwise, set it to 0 so that further syncache_respond
1368          * attempts by the kernel will be ignored.
1369          */
1370         if (atomic_cmpset_ptr(&synqe->wr, (uintptr_t)wr, 0)) {
1371
1372                 /*
1373                  * syncache may or may not have a hold on the synqe, which may
1374                  * or may not be stashed in the original SYN mbuf passed to us.
1375                  * Just copy it over instead of dealing with all possibilities.
1376                  */
1377                 m = m_dup(synqe->syn, M_NOWAIT);
1378                 if (m)
1379                         m->m_pkthdr.rcvif = hw_ifp;
1380
1381                 remove_tid(sc, synqe->tid, ntids);
1382                 free(wr, M_CXGBE);
1383
1384                 /* Yank the synqe out of the lctx synq. */
1385                 INP_WLOCK(inp);
1386                 TAILQ_REMOVE(&lctx->synq, synqe, link);
1387                 release_synqe(synqe);   /* removed from synq list */
1388                 inp = release_lctx(sc, lctx);
1389                 if (inp)
1390                         INP_WUNLOCK(inp);
1391
1392                 release_synqe(synqe);   /* extra hold */
1393                 REJECT_PASS_ACCEPT();
1394         }
1395
1396         CTR5(KTR_CXGBE, "%s: stid %u, tid %u, lctx %p, synqe %p, SYNACK",
1397             __func__, stid, tid, lctx, synqe);
1398
1399         INP_WLOCK(inp);
1400         synqe->flags |= TPF_SYNQE_HAS_L2TE;
1401         if (__predict_false(inp->inp_flags & INP_DROPPED)) {
1402                 /*
1403                  * Listening socket closed but tod_listen_stop did not abort
1404                  * this tid because there was no L2T entry for the tid at that
1405                  * time.  Abort it now.  The reply to the abort will clean up.
1406                  */
1407                 CTR6(KTR_CXGBE,
1408                     "%s: stid %u, tid %u, lctx %p, synqe %p (0x%x), ABORT",
1409                     __func__, stid, tid, lctx, synqe, synqe->flags);
1410                 if (!(synqe->flags & TPF_SYNQE_EXPANDED))
1411                         send_reset_synqe(tod, synqe);
1412                 INP_WUNLOCK(inp);
1413
1414                 release_synqe(synqe);   /* extra hold */
1415                 return (__LINE__);
1416         }
1417         INP_WUNLOCK(inp);
1418
1419         release_synqe(synqe);   /* extra hold */
1420         return (0);
1421 reject:
1422         CTR4(KTR_CXGBE, "%s: stid %u, tid %u, REJECT (%d)", __func__, stid, tid,
1423             reject_reason);
1424
1425         if (e)
1426                 t4_l2t_release(e);
1427         release_tid(sc, tid, lctx->ctrlq);
1428
1429         if (__predict_true(m != NULL)) {
1430                 m_adj(m, sizeof(*cpl));
1431                 m->m_pkthdr.csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID |
1432                     CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1433                 m->m_pkthdr.csum_data = 0xffff;
1434                 hw_ifp->if_input(hw_ifp, m);
1435         }
1436
1437         return (reject_reason);
1438 }
1439
1440 static void
1441 synqe_to_protohdrs(struct adapter *sc, struct synq_entry *synqe,
1442     const struct cpl_pass_establish *cpl, struct in_conninfo *inc,
1443     struct tcphdr *th, struct tcpopt *to)
1444 {
1445         uint16_t tcp_opt = be16toh(cpl->tcp_opt);
1446
1447         /* start off with the original SYN */
1448         pass_accept_req_to_protohdrs(sc, synqe->syn, inc, th);
1449
1450         /* modify parts to make it look like the ACK to our SYN|ACK */
1451         th->th_flags = TH_ACK;
1452         th->th_ack = synqe->iss + 1;
1453         th->th_seq = be32toh(cpl->rcv_isn);
1454         bzero(to, sizeof(*to));
1455         if (G_TCPOPT_TSTAMP(tcp_opt)) {
1456                 to->to_flags |= TOF_TS;
1457                 to->to_tsecr = synqe->ts;
1458         }
1459 }
1460
1461 static int
1462 do_pass_establish(struct sge_iq *iq, const struct rss_header *rss,
1463     struct mbuf *m)
1464 {
1465         struct adapter *sc = iq->adapter;
1466         struct vi_info *vi;
1467         struct ifnet *ifp;
1468         const struct cpl_pass_establish *cpl = (const void *)(rss + 1);
1469 #if defined(KTR) || defined(INVARIANTS)
1470         unsigned int stid = G_PASS_OPEN_TID(be32toh(cpl->tos_stid));
1471 #endif
1472         unsigned int tid = GET_TID(cpl);
1473         struct synq_entry *synqe = lookup_tid(sc, tid);
1474         struct listen_ctx *lctx = synqe->lctx;
1475         struct inpcb *inp = lctx->inp, *new_inp;
1476         struct socket *so;
1477         struct tcphdr th;
1478         struct tcpopt to;
1479         struct in_conninfo inc;
1480         struct toepcb *toep;
1481         u_int txqid, rxqid;
1482 #ifdef INVARIANTS
1483         unsigned int opcode = G_CPL_OPCODE(be32toh(OPCODE_TID(cpl)));
1484 #endif
1485
1486         KASSERT(opcode == CPL_PASS_ESTABLISH,
1487             ("%s: unexpected opcode 0x%x", __func__, opcode));
1488         KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
1489         KASSERT(lctx->stid == stid, ("%s: lctx stid mismatch", __func__));
1490         KASSERT(synqe->flags & TPF_SYNQE,
1491             ("%s: tid %u (ctx %p) not a synqe", __func__, tid, synqe));
1492
1493         INP_INFO_RLOCK(&V_tcbinfo);     /* for syncache_expand */
1494         INP_WLOCK(inp);
1495
1496         CTR6(KTR_CXGBE,
1497             "%s: stid %u, tid %u, synqe %p (0x%x), inp_flags 0x%x",
1498             __func__, stid, tid, synqe, synqe->flags, inp->inp_flags);
1499
1500         if (__predict_false(inp->inp_flags & INP_DROPPED)) {
1501
1502                 if (synqe->flags & TPF_SYNQE_HAS_L2TE) {
1503                         KASSERT(synqe->flags & TPF_ABORT_SHUTDOWN,
1504                             ("%s: listen socket closed but tid %u not aborted.",
1505                             __func__, tid));
1506                 }
1507
1508                 INP_WUNLOCK(inp);
1509                 INP_INFO_RUNLOCK(&V_tcbinfo);
1510                 return (0);
1511         }
1512
1513         ifp = synqe->syn->m_pkthdr.rcvif;
1514         vi = ifp->if_softc;
1515         KASSERT(vi->pi->adapter == sc,
1516             ("%s: vi %p, sc %p mismatch", __func__, vi, sc));
1517
1518         get_qids_from_mbuf(synqe->syn, &txqid, &rxqid);
1519         KASSERT(rxqid == iq_to_ofld_rxq(iq) - &sc->sge.ofld_rxq[0],
1520             ("%s: CPL arrived on unexpected rxq.  %d %d", __func__, rxqid,
1521             (int)(iq_to_ofld_rxq(iq) - &sc->sge.ofld_rxq[0])));
1522
1523         toep = alloc_toepcb(vi, txqid, rxqid, M_NOWAIT);
1524         if (toep == NULL) {
1525 reset:
1526                 /*
1527                  * The reply to this abort will perform final cleanup.  There is
1528                  * no need to check for HAS_L2TE here.  We can be here only if
1529                  * we responded to the PASS_ACCEPT_REQ, and our response had the
1530                  * L2T idx.
1531                  */
1532                 send_reset_synqe(TOEDEV(ifp), synqe);
1533                 INP_WUNLOCK(inp);
1534                 INP_INFO_RUNLOCK(&V_tcbinfo);
1535                 return (0);
1536         }
1537         toep->tid = tid;
1538         toep->l2te = &sc->l2t->l2tab[synqe->l2e_idx];
1539         if (synqe->flags & TPF_SYNQE_TCPDDP)
1540                 set_tcpddp_ulp_mode(toep);
1541         else
1542                 toep->ulp_mode = ULP_MODE_NONE;
1543         /* opt0 rcv_bufsiz initially, assumes its normal meaning later */
1544         toep->rx_credits = synqe->rcv_bufsize;
1545
1546         so = inp->inp_socket;
1547         KASSERT(so != NULL, ("%s: socket is NULL", __func__));
1548
1549         /* Come up with something that syncache_expand should be ok with. */
1550         synqe_to_protohdrs(sc, synqe, cpl, &inc, &th, &to);
1551
1552         /*
1553          * No more need for anything in the mbuf that carried the
1554          * CPL_PASS_ACCEPT_REQ.  Drop the CPL_PASS_ESTABLISH and toep pointer
1555          * there.  XXX: bad form but I don't want to increase the size of synqe.
1556          */
1557         m = synqe->syn;
1558         KASSERT(sizeof(*cpl) + sizeof(toep) <= m->m_len,
1559             ("%s: no room in mbuf %p (m_len %d)", __func__, m, m->m_len));
1560         bcopy(cpl, mtod(m, void *), sizeof(*cpl));
1561         *(struct toepcb **)(mtod(m, struct cpl_pass_establish *) + 1) = toep;
1562
1563         if (!toe_syncache_expand(&inc, &to, &th, &so) || so == NULL) {
1564                 free_toepcb(toep);
1565                 goto reset;
1566         }
1567
1568         /* New connection inpcb is already locked by syncache_expand(). */
1569         new_inp = sotoinpcb(so);
1570         INP_WLOCK_ASSERT(new_inp);
1571
1572         /*
1573          * This is for the unlikely case where the syncache entry that we added
1574          * has been evicted from the syncache, but the syncache_expand above
1575          * works because of syncookies.
1576          *
1577          * XXX: we've held the tcbinfo lock throughout so there's no risk of
1578          * anyone accept'ing a connection before we've installed our hooks, but
1579          * this somewhat defeats the purpose of having a tod_offload_socket :-(
1580          */
1581         if (__predict_false(!(synqe->flags & TPF_SYNQE_EXPANDED))) {
1582                 tcp_timer_activate(intotcpcb(new_inp), TT_KEEP, 0);
1583                 t4_offload_socket(TOEDEV(ifp), synqe, so);
1584         }
1585
1586         INP_WUNLOCK(new_inp);
1587
1588         /* Done with the synqe */
1589         TAILQ_REMOVE(&lctx->synq, synqe, link);
1590         inp = release_lctx(sc, lctx);
1591         if (inp != NULL)
1592                 INP_WUNLOCK(inp);
1593         INP_INFO_RUNLOCK(&V_tcbinfo);
1594         release_synqe(synqe);
1595
1596         return (0);
1597 }
1598
1599 void
1600 t4_init_listen_cpl_handlers(void)
1601 {
1602
1603         t4_register_cpl_handler(CPL_PASS_OPEN_RPL, do_pass_open_rpl);
1604         t4_register_cpl_handler(CPL_CLOSE_LISTSRV_RPL, do_close_server_rpl);
1605         t4_register_cpl_handler(CPL_PASS_ACCEPT_REQ, do_pass_accept_req);
1606         t4_register_cpl_handler(CPL_PASS_ESTABLISH, do_pass_establish);
1607 }
1608 #endif