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