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