]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/cxgbe/tom/t4_connect.c
cxgbe(4): Add support for Chelsio's Terminator 5 (aka T5) ASIC. This
[FreeBSD/FreeBSD.git] / sys / dev / cxgbe / tom / t4_connect.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/domain.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <net/ethernet.h>
45 #include <net/if.h>
46 #include <net/if_types.h>
47 #include <net/if_vlan_var.h>
48 #include <net/route.h>
49 #include <netinet/in.h>
50 #include <netinet/in_pcb.h>
51 #include <netinet/ip.h>
52 #include <netinet/tcp_var.h>
53 #define TCPSTATES
54 #include <netinet/tcp_fsm.h>
55 #include <netinet/toecore.h>
56
57 #include "common/common.h"
58 #include "common/t4_msg.h"
59 #include "common/t4_regs.h"
60 #include "tom/t4_tom_l2t.h"
61 #include "tom/t4_tom.h"
62
63 /* atid services */
64 static int alloc_atid(struct adapter *, void *);
65 static void *lookup_atid(struct adapter *, int);
66 static void free_atid(struct adapter *, int);
67
68 static int
69 alloc_atid(struct adapter *sc, void *ctx)
70 {
71         struct tid_info *t = &sc->tids;
72         int atid = -1;
73
74         mtx_lock(&t->atid_lock);
75         if (t->afree) {
76                 union aopen_entry *p = t->afree;
77
78                 atid = p - t->atid_tab;
79                 t->afree = p->next;
80                 p->data = ctx;
81                 t->atids_in_use++;
82         }
83         mtx_unlock(&t->atid_lock);
84         return (atid);
85 }
86
87 static void *
88 lookup_atid(struct adapter *sc, int atid)
89 {
90         struct tid_info *t = &sc->tids;
91
92         return (t->atid_tab[atid].data);
93 }
94
95 static void
96 free_atid(struct adapter *sc, int atid)
97 {
98         struct tid_info *t = &sc->tids;
99         union aopen_entry *p = &t->atid_tab[atid];
100
101         mtx_lock(&t->atid_lock);
102         p->next = t->afree;
103         t->afree = p;
104         t->atids_in_use--;
105         mtx_unlock(&t->atid_lock);
106 }
107
108 /*
109  * Active open failed.
110  */
111 static int
112 do_act_establish(struct sge_iq *iq, const struct rss_header *rss,
113     struct mbuf *m)
114 {
115         struct adapter *sc = iq->adapter;
116         const struct cpl_act_establish *cpl = (const void *)(rss + 1);
117         unsigned int tid = GET_TID(cpl);
118         unsigned int atid = G_TID_TID(ntohl(cpl->tos_atid));
119         struct toepcb *toep = lookup_atid(sc, atid);
120         struct inpcb *inp = toep->inp;
121
122         KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
123         KASSERT(toep->tid == atid, ("%s: toep tid/atid mismatch", __func__));
124
125         CTR3(KTR_CXGBE, "%s: atid %u, tid %u", __func__, atid, tid);
126         free_atid(sc, atid);
127
128         INP_WLOCK(inp);
129         toep->tid = tid;
130         insert_tid(sc, tid, toep);
131         if (inp->inp_flags & INP_DROPPED) {
132
133                 /* socket closed by the kernel before hw told us it connected */
134
135                 send_flowc_wr(toep, NULL);
136                 send_reset(sc, toep, be32toh(cpl->snd_isn));
137                 goto done;
138         }
139
140         make_established(toep, cpl->snd_isn, cpl->rcv_isn, cpl->tcp_opt);
141 done:
142         INP_WUNLOCK(inp);
143         return (0);
144 }
145
146 static inline int
147 act_open_has_tid(unsigned int status)
148 {
149
150         return (status != CPL_ERR_TCAM_FULL &&
151             status != CPL_ERR_TCAM_PARITY &&
152             status != CPL_ERR_CONN_EXIST &&
153             status != CPL_ERR_ARP_MISS);
154 }
155
156 /*
157  * Convert an ACT_OPEN_RPL status to an errno.
158  */
159 static inline int
160 act_open_rpl_status_to_errno(int status)
161 {
162
163         switch (status) {
164         case CPL_ERR_CONN_RESET:
165                 return (ECONNREFUSED);
166         case CPL_ERR_ARP_MISS:
167                 return (EHOSTUNREACH);
168         case CPL_ERR_CONN_TIMEDOUT:
169                 return (ETIMEDOUT);
170         case CPL_ERR_TCAM_FULL:
171                 return (EAGAIN);
172         case CPL_ERR_CONN_EXIST:
173                 log(LOG_ERR, "ACTIVE_OPEN_RPL: 4-tuple in use\n");
174                 return (EAGAIN);
175         default:
176                 return (EIO);
177         }
178 }
179
180 static int
181 do_act_open_rpl(struct sge_iq *iq, const struct rss_header *rss,
182     struct mbuf *m)
183 {
184         struct adapter *sc = iq->adapter;
185         const struct cpl_act_open_rpl *cpl = (const void *)(rss + 1);
186         unsigned int atid = G_TID_TID(G_AOPEN_ATID(be32toh(cpl->atid_status)));
187         unsigned int status = G_AOPEN_STATUS(be32toh(cpl->atid_status));
188         struct toepcb *toep = lookup_atid(sc, atid);
189         struct inpcb *inp = toep->inp;
190         struct toedev *tod = &toep->td->tod;
191         int rc;
192
193         KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
194         KASSERT(toep->tid == atid, ("%s: toep tid/atid mismatch", __func__));
195
196         CTR3(KTR_CXGBE, "%s: atid %u, status %u ", __func__, atid, status);
197
198         /* Ignore negative advice */
199         if (negative_advice(status))
200                 return (0);
201
202         free_atid(sc, atid);
203         toep->tid = -1;
204
205         if (status && act_open_has_tid(status))
206                 release_tid(sc, GET_TID(cpl), toep->ctrlq);
207
208         rc = act_open_rpl_status_to_errno(status);
209         if (rc != EAGAIN)
210                 INP_INFO_WLOCK(&V_tcbinfo);
211         INP_WLOCK(inp);
212         toe_connect_failed(tod, inp, rc);
213         final_cpl_received(toep);       /* unlocks inp */
214         if (rc != EAGAIN)
215                 INP_INFO_WUNLOCK(&V_tcbinfo);
216
217         return (0);
218 }
219
220 /*
221  * Options2 for active open.
222  */
223 static uint32_t
224 calc_opt2a(struct socket *so, struct toepcb *toep)
225 {
226         struct tcpcb *tp = so_sototcpcb(so);
227         struct port_info *pi = toep->port;
228         struct adapter *sc = pi->adapter;
229         uint32_t opt2 = 0;
230
231         if (tp->t_flags & TF_SACK_PERMIT)
232                 opt2 |= F_SACK_EN;
233
234         if (tp->t_flags & TF_REQ_TSTMP)
235                 opt2 |= F_TSTAMPS_EN;
236
237         if (tp->t_flags & TF_REQ_SCALE)
238                 opt2 |= F_WND_SCALE_EN;
239
240         if (V_tcp_do_ecn)
241                 opt2 |= F_CCTRL_ECN;
242
243         opt2 |= V_TX_QUEUE(sc->params.tp.tx_modq[pi->tx_chan]);
244         opt2 |= F_RSS_QUEUE_VALID | V_RSS_QUEUE(toep->ofld_rxq->iq.abs_id);
245         if (is_t4(sc))
246                 opt2 |= F_RX_COALESCE_VALID | V_RX_COALESCE(M_RX_COALESCE);
247         else
248                 opt2 |= F_T5_OPT_2_VALID | V_RX_COALESCE(M_RX_COALESCE);
249
250 #ifdef USE_DDP_RX_FLOW_CONTROL
251         if (toep->ulp_mode == ULP_MODE_TCPDDP)
252                 opt2 |= F_RX_FC_VALID | F_RX_FC_DDP;
253 #endif
254
255         return (htobe32(opt2));
256 }
257
258 void
259 t4_init_connect_cpl_handlers(struct adapter *sc)
260 {
261
262         t4_register_cpl_handler(sc, CPL_ACT_ESTABLISH, do_act_establish);
263         t4_register_cpl_handler(sc, CPL_ACT_OPEN_RPL, do_act_open_rpl);
264 }
265
266 #define DONT_OFFLOAD_ACTIVE_OPEN(x)     do { \
267         reason = __LINE__; \
268         rc = (x); \
269         goto failed; \
270 } while (0)
271
272 static inline int
273 act_open_cpl_size(struct adapter *sc, int isipv6)
274 {
275         static const int sz_t4[] = {
276                 sizeof (struct cpl_act_open_req),
277                 sizeof (struct cpl_act_open_req6)
278         };
279         static const int sz_t5[] = {
280                 sizeof (struct cpl_t5_act_open_req),
281                 sizeof (struct cpl_t5_act_open_req6)
282         };
283
284         if (is_t4(sc))
285                 return (sz_t4[!!isipv6]);
286         else
287                 return (sz_t5[!!isipv6]);
288 }
289
290 /*
291  * active open (soconnect).
292  *
293  * State of affairs on entry:
294  * soisconnecting (so_state |= SS_ISCONNECTING)
295  * tcbinfo not locked (This has changed - used to be WLOCKed)
296  * inp WLOCKed
297  * tp->t_state = TCPS_SYN_SENT
298  * rtalloc1, RT_UNLOCK on rt.
299  */
300 int
301 t4_connect(struct toedev *tod, struct socket *so, struct rtentry *rt,
302     struct sockaddr *nam)
303 {
304         struct adapter *sc = tod->tod_softc;
305         struct tom_data *td = tod_td(tod);
306         struct toepcb *toep = NULL;
307         struct wrqe *wr = NULL;
308         struct ifnet *rt_ifp = rt->rt_ifp;
309         struct port_info *pi;
310         int mtu_idx, rscale, qid_atid, rc, isipv6;
311         struct inpcb *inp = sotoinpcb(so);
312         struct tcpcb *tp = intotcpcb(inp);
313         int reason;
314
315         INP_WLOCK_ASSERT(inp);
316         KASSERT(nam->sa_family == AF_INET || nam->sa_family == AF_INET6,
317             ("%s: dest addr %p has family %u", __func__, nam, nam->sa_family));
318
319         if (rt_ifp->if_type == IFT_ETHER)
320                 pi = rt_ifp->if_softc;
321         else if (rt_ifp->if_type == IFT_L2VLAN) {
322                 struct ifnet *ifp = VLAN_COOKIE(rt_ifp);
323
324                 pi = ifp->if_softc;
325         } else if (rt_ifp->if_type == IFT_IEEE8023ADLAG)
326                 DONT_OFFLOAD_ACTIVE_OPEN(ENOSYS); /* XXX: implement lagg+TOE */
327         else
328                 DONT_OFFLOAD_ACTIVE_OPEN(ENOTSUP);
329
330         toep = alloc_toepcb(pi, -1, -1, M_NOWAIT);
331         if (toep == NULL)
332                 DONT_OFFLOAD_ACTIVE_OPEN(ENOMEM);
333
334         toep->tid = alloc_atid(sc, toep);
335         if (toep->tid < 0)
336                 DONT_OFFLOAD_ACTIVE_OPEN(ENOMEM);
337
338         toep->l2te = t4_l2t_get(pi, rt_ifp,
339             rt->rt_flags & RTF_GATEWAY ? rt->rt_gateway : nam);
340         if (toep->l2te == NULL)
341                 DONT_OFFLOAD_ACTIVE_OPEN(ENOMEM);
342
343         isipv6 = nam->sa_family == AF_INET6;
344         wr = alloc_wrqe(act_open_cpl_size(sc, isipv6), toep->ctrlq);
345         if (wr == NULL)
346                 DONT_OFFLOAD_ACTIVE_OPEN(ENOMEM);
347
348         if (sc->tt.ddp && (so->so_options & SO_NO_DDP) == 0)
349                 set_tcpddp_ulp_mode(toep);
350         else
351                 toep->ulp_mode = ULP_MODE_NONE;
352         SOCKBUF_LOCK(&so->so_rcv);
353         /* opt0 rcv_bufsiz initially, assumes its normal meaning later */
354         toep->rx_credits = min(select_rcv_wnd(so) >> 10, M_RCV_BUFSIZ);
355         SOCKBUF_UNLOCK(&so->so_rcv);
356
357         /*
358          * The kernel sets request_r_scale based on sb_max whereas we need to
359          * take hardware's MAX_RCV_WND into account too.  This is normally a
360          * no-op as MAX_RCV_WND is much larger than the default sb_max.
361          */
362         if (tp->t_flags & TF_REQ_SCALE)
363                 rscale = tp->request_r_scale = select_rcv_wscale();
364         else
365                 rscale = 0;
366         mtu_idx = find_best_mtu_idx(sc, &inp->inp_inc, 0);
367         qid_atid = (toep->ofld_rxq->iq.abs_id << 14) | toep->tid;
368
369         if (isipv6) {
370                 struct cpl_act_open_req6 *cpl = wrtod(wr);
371
372                 if ((inp->inp_vflag & INP_IPV6) == 0) {
373                         /* XXX think about this a bit more */
374                         log(LOG_ERR,
375                             "%s: time to think about AF_INET6 + vflag 0x%x.\n",
376                             __func__, inp->inp_vflag);
377                         DONT_OFFLOAD_ACTIVE_OPEN(ENOTSUP);
378                 }
379
380                 toep->ce = hold_lip(td, &inp->in6p_laddr);
381                 if (toep->ce == NULL)
382                         DONT_OFFLOAD_ACTIVE_OPEN(ENOENT);
383
384                 INIT_TP_WR(cpl, 0);
385                 OPCODE_TID(cpl) = htobe32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ6,
386                     qid_atid));
387
388                 cpl->local_port = inp->inp_lport;
389                 cpl->local_ip_hi = *(uint64_t *)&inp->in6p_laddr.s6_addr[0];
390                 cpl->local_ip_lo = *(uint64_t *)&inp->in6p_laddr.s6_addr[8];
391                 cpl->peer_port = inp->inp_fport;
392                 cpl->peer_ip_hi = *(uint64_t *)&inp->in6p_faddr.s6_addr[0];
393                 cpl->peer_ip_lo = *(uint64_t *)&inp->in6p_faddr.s6_addr[8];
394                 cpl->opt0 = calc_opt0(so, pi, toep->l2te, mtu_idx, rscale,
395                     toep->rx_credits, toep->ulp_mode);
396                 cpl->opt2 = calc_opt2a(so, toep);
397                 if (is_t4(sc)) {
398                         cpl->params = select_ntuple(pi, toep->l2te,
399                             sc->filter_mode);
400                 } else {
401                         struct cpl_t5_act_open_req6 *c5 = (void *)cpl;
402
403                         c5->rsvd = 0;
404                         c5->params = select_ntuple(pi, toep->l2te,
405                             sc->filter_mode);
406                 }
407         } else {
408                 struct cpl_act_open_req *cpl = wrtod(wr);
409
410                 INIT_TP_WR(cpl, 0);
411                 OPCODE_TID(cpl) = htobe32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ,
412                     qid_atid));
413                 inp_4tuple_get(inp, &cpl->local_ip, &cpl->local_port,
414                     &cpl->peer_ip, &cpl->peer_port);
415                 cpl->opt0 = calc_opt0(so, pi, toep->l2te, mtu_idx, rscale,
416                     toep->rx_credits, toep->ulp_mode);
417                 cpl->opt2 = calc_opt2a(so, toep);
418                 if (is_t4(sc)) {
419                         cpl->params = select_ntuple(pi, toep->l2te,
420                             sc->filter_mode);
421                 } else {
422                         struct cpl_t5_act_open_req6 *c5 = (void *)cpl;
423
424                         c5->rsvd = 0;
425                         c5->params = select_ntuple(pi, toep->l2te,
426                             sc->filter_mode);
427                 }
428         }
429
430         CTR5(KTR_CXGBE, "%s: atid %u (%s), toep %p, inp %p", __func__,
431             toep->tid, tcpstates[tp->t_state], toep, inp);
432
433         offload_socket(so, toep);
434         rc = t4_l2t_send(sc, wr, toep->l2te);
435         if (rc == 0) {
436                 toep->flags |= TPF_CPL_PENDING;
437                 return (0);
438         }
439
440         undo_offload_socket(so);
441         reason = __LINE__;
442 failed:
443         CTR3(KTR_CXGBE, "%s: not offloading (%d), rc %d", __func__, reason, rc);
444
445         if (wr)
446                 free_wrqe(wr);
447
448         if (toep) {
449                 if (toep->tid >= 0)
450                         free_atid(sc, toep->tid);
451                 if (toep->l2te)
452                         t4_l2t_release(toep->l2te);
453                 if (toep->ce)
454                         release_lip(td, toep->ce);
455                 free_toepcb(toep);
456         }
457
458         return (rc);
459 }
460 #endif