xref: /freebsd/sys/dev/cxgbe/tom/t4_connect.c (revision 63a938566d524836885917d95bd491aa4400b181)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2012 Chelsio Communications, Inc.
5  * All rights reserved.
6  * Written by: Navdeep Parhar <np@FreeBSD.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include "opt_inet.h"
34 #include "opt_inet6.h"
35 
36 #ifdef TCP_OFFLOAD
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/ktr.h>
41 #include <sys/module.h>
42 #include <sys/protosw.h>
43 #include <sys/domain.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <net/ethernet.h>
47 #include <net/if.h>
48 #include <net/if_types.h>
49 #include <net/if_vlan_var.h>
50 #include <net/route.h>
51 #include <netinet/in.h>
52 #include <netinet/in_pcb.h>
53 #include <netinet/ip.h>
54 #define TCPSTATES
55 #include <netinet/tcp_fsm.h>
56 #include <netinet/tcp_var.h>
57 #include <netinet/toecore.h>
58 
59 #include "common/common.h"
60 #include "common/t4_msg.h"
61 #include "common/t4_regs.h"
62 #include "common/t4_regs_values.h"
63 #include "tom/t4_tom_l2t.h"
64 #include "tom/t4_tom.h"
65 
66 /* atid services */
67 static int alloc_atid(struct adapter *, void *);
68 static void *lookup_atid(struct adapter *, int);
69 static void free_atid(struct adapter *, int);
70 
71 static int
72 alloc_atid(struct adapter *sc, void *ctx)
73 {
74 	struct tid_info *t = &sc->tids;
75 	int atid = -1;
76 
77 	mtx_lock(&t->atid_lock);
78 	if (t->afree) {
79 		union aopen_entry *p = t->afree;
80 
81 		atid = p - t->atid_tab;
82 		t->afree = p->next;
83 		p->data = ctx;
84 		t->atids_in_use++;
85 	}
86 	mtx_unlock(&t->atid_lock);
87 	return (atid);
88 }
89 
90 static void *
91 lookup_atid(struct adapter *sc, int atid)
92 {
93 	struct tid_info *t = &sc->tids;
94 
95 	return (t->atid_tab[atid].data);
96 }
97 
98 static void
99 free_atid(struct adapter *sc, int atid)
100 {
101 	struct tid_info *t = &sc->tids;
102 	union aopen_entry *p = &t->atid_tab[atid];
103 
104 	mtx_lock(&t->atid_lock);
105 	p->next = t->afree;
106 	t->afree = p;
107 	t->atids_in_use--;
108 	mtx_unlock(&t->atid_lock);
109 }
110 
111 /*
112  * Active open succeeded.
113  */
114 static int
115 do_act_establish(struct sge_iq *iq, const struct rss_header *rss,
116     struct mbuf *m)
117 {
118 	struct adapter *sc = iq->adapter;
119 	const struct cpl_act_establish *cpl = (const void *)(rss + 1);
120 	u_int tid = GET_TID(cpl);
121 	u_int atid = G_TID_TID(ntohl(cpl->tos_atid));
122 	struct toepcb *toep = lookup_atid(sc, atid);
123 	struct inpcb *inp = toep->inp;
124 
125 	KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
126 	KASSERT(toep->tid == atid, ("%s: toep tid/atid mismatch", __func__));
127 
128 	CTR3(KTR_CXGBE, "%s: atid %u, tid %u", __func__, atid, tid);
129 	free_atid(sc, atid);
130 
131 	CURVNET_SET(toep->vnet);
132 	INP_WLOCK(inp);
133 	toep->tid = tid;
134 	insert_tid(sc, tid, toep, inp->inp_vflag & INP_IPV6 ? 2 : 1);
135 	if (inp->inp_flags & INP_DROPPED) {
136 
137 		/* socket closed by the kernel before hw told us it connected */
138 
139 		send_flowc_wr(toep, NULL);
140 		send_reset(sc, toep, be32toh(cpl->snd_isn));
141 		goto done;
142 	}
143 
144 	make_established(toep, cpl->snd_isn, cpl->rcv_isn, cpl->tcp_opt);
145 
146 	if (toep->ulp_mode == ULP_MODE_TLS)
147 		tls_establish(toep);
148 
149 done:
150 	INP_WUNLOCK(inp);
151 	CURVNET_RESTORE();
152 	return (0);
153 }
154 
155 /*
156  * Convert an ACT_OPEN_RPL status to an errno.
157  */
158 static inline int
159 act_open_rpl_status_to_errno(int status)
160 {
161 
162 	switch (status) {
163 	case CPL_ERR_CONN_RESET:
164 		return (ECONNREFUSED);
165 	case CPL_ERR_ARP_MISS:
166 		return (EHOSTUNREACH);
167 	case CPL_ERR_CONN_TIMEDOUT:
168 		return (ETIMEDOUT);
169 	case CPL_ERR_TCAM_FULL:
170 		return (EAGAIN);
171 	case CPL_ERR_CONN_EXIST:
172 		log(LOG_ERR, "ACTIVE_OPEN_RPL: 4-tuple in use\n");
173 		return (EAGAIN);
174 	default:
175 		return (EIO);
176 	}
177 }
178 
179 void
180 act_open_failure_cleanup(struct adapter *sc, u_int atid, u_int status)
181 {
182 	struct toepcb *toep = lookup_atid(sc, atid);
183 	struct inpcb *inp = toep->inp;
184 	struct toedev *tod = &toep->td->tod;
185 
186 	free_atid(sc, atid);
187 	toep->tid = -1;
188 
189 	CURVNET_SET(toep->vnet);
190 	if (status != EAGAIN)
191 		INP_INFO_RLOCK(&V_tcbinfo);
192 	INP_WLOCK(inp);
193 	toe_connect_failed(tod, inp, status);
194 	final_cpl_received(toep);	/* unlocks inp */
195 	if (status != EAGAIN)
196 		INP_INFO_RUNLOCK(&V_tcbinfo);
197 	CURVNET_RESTORE();
198 }
199 
200 /*
201  * Active open failed.
202  */
203 static int
204 do_act_open_rpl(struct sge_iq *iq, const struct rss_header *rss,
205     struct mbuf *m)
206 {
207 	struct adapter *sc = iq->adapter;
208 	const struct cpl_act_open_rpl *cpl = (const void *)(rss + 1);
209 	u_int atid = G_TID_TID(G_AOPEN_ATID(be32toh(cpl->atid_status)));
210 	u_int status = G_AOPEN_STATUS(be32toh(cpl->atid_status));
211 	struct toepcb *toep = lookup_atid(sc, atid);
212 	int rc;
213 
214 	KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
215 	KASSERT(toep->tid == atid, ("%s: toep tid/atid mismatch", __func__));
216 
217 	CTR3(KTR_CXGBE, "%s: atid %u, status %u ", __func__, atid, status);
218 
219 	/* Ignore negative advice */
220 	if (negative_advice(status))
221 		return (0);
222 
223 	if (status && act_open_has_tid(status))
224 		release_tid(sc, GET_TID(cpl), toep->ctrlq);
225 
226 	rc = act_open_rpl_status_to_errno(status);
227 	act_open_failure_cleanup(sc, atid, rc);
228 
229 	return (0);
230 }
231 
232 /*
233  * Options2 for active open.
234  */
235 static uint32_t
236 calc_opt2a(struct socket *so, struct toepcb *toep)
237 {
238 	struct tcpcb *tp = so_sototcpcb(so);
239 	struct port_info *pi = toep->vi->pi;
240 	struct adapter *sc = pi->adapter;
241 	uint32_t opt2;
242 
243 	opt2 = V_TX_QUEUE(sc->params.tp.tx_modq[pi->tx_chan]) |
244 	    F_RSS_QUEUE_VALID | V_RSS_QUEUE(toep->ofld_rxq->iq.abs_id);
245 
246 	if (tp->t_flags & TF_SACK_PERMIT)
247 		opt2 |= F_SACK_EN;
248 
249 	if (tp->t_flags & TF_REQ_TSTMP)
250 		opt2 |= F_TSTAMPS_EN;
251 
252 	if (tp->t_flags & TF_REQ_SCALE)
253 		opt2 |= F_WND_SCALE_EN;
254 
255 	if (V_tcp_do_ecn)
256 		opt2 |= F_CCTRL_ECN;
257 
258 	/* RX_COALESCE is always a valid value (M_RX_COALESCE). */
259 	if (is_t4(sc))
260 		opt2 |= F_RX_COALESCE_VALID;
261 	else {
262 		opt2 |= F_T5_OPT_2_VALID;
263 		opt2 |= F_T5_ISS;
264 	}
265 	if (sc->tt.rx_coalesce)
266 		opt2 |= V_RX_COALESCE(M_RX_COALESCE);
267 
268 	if (sc->tt.cong_algorithm != -1)
269 		opt2 |= V_CONG_CNTRL(sc->tt.cong_algorithm & M_CONG_CNTRL);
270 
271 #ifdef USE_DDP_RX_FLOW_CONTROL
272 	if (toep->ulp_mode == ULP_MODE_TCPDDP)
273 		opt2 |= F_RX_FC_VALID | F_RX_FC_DDP;
274 #endif
275 	if (toep->ulp_mode == ULP_MODE_TLS) {
276 		opt2 |= F_RX_FC_VALID;
277 		opt2 &= ~V_RX_COALESCE(M_RX_COALESCE);
278 		opt2 |= F_RX_FC_DISABLE;
279 	}
280 
281 	return (htobe32(opt2));
282 }
283 
284 void
285 t4_init_connect_cpl_handlers(void)
286 {
287 
288 	t4_register_cpl_handler(CPL_ACT_ESTABLISH, do_act_establish);
289 	t4_register_cpl_handler(CPL_ACT_OPEN_RPL, do_act_open_rpl);
290 }
291 
292 void
293 t4_uninit_connect_cpl_handlers(void)
294 {
295 
296 	t4_register_cpl_handler(CPL_ACT_ESTABLISH, NULL);
297 	t4_register_cpl_handler(CPL_ACT_OPEN_RPL, NULL);
298 }
299 
300 #define DONT_OFFLOAD_ACTIVE_OPEN(x)	do { \
301 	reason = __LINE__; \
302 	rc = (x); \
303 	goto failed; \
304 } while (0)
305 
306 static inline int
307 act_open_cpl_size(struct adapter *sc, int isipv6)
308 {
309 	int idx;
310 	static const int sz_table[3][2] = {
311 		{
312 			sizeof (struct cpl_act_open_req),
313 			sizeof (struct cpl_act_open_req6)
314 		},
315 		{
316 			sizeof (struct cpl_t5_act_open_req),
317 			sizeof (struct cpl_t5_act_open_req6)
318 		},
319 		{
320 			sizeof (struct cpl_t6_act_open_req),
321 			sizeof (struct cpl_t6_act_open_req6)
322 		},
323 	};
324 
325 	MPASS(chip_id(sc) >= CHELSIO_T4);
326 	idx = min(chip_id(sc) - CHELSIO_T4, 2);
327 
328 	return (sz_table[idx][!!isipv6]);
329 }
330 
331 /*
332  * active open (soconnect).
333  *
334  * State of affairs on entry:
335  * soisconnecting (so_state |= SS_ISCONNECTING)
336  * tcbinfo not locked (This has changed - used to be WLOCKed)
337  * inp WLOCKed
338  * tp->t_state = TCPS_SYN_SENT
339  * rtalloc1, RT_UNLOCK on rt.
340  */
341 int
342 t4_connect(struct toedev *tod, struct socket *so, struct rtentry *rt,
343     struct sockaddr *nam)
344 {
345 	struct adapter *sc = tod->tod_softc;
346 	struct tom_data *td = tod_td(tod);
347 	struct toepcb *toep = NULL;
348 	struct wrqe *wr = NULL;
349 	struct ifnet *rt_ifp = rt->rt_ifp;
350 	struct vi_info *vi;
351 	int mtu_idx, rscale, qid_atid, rc, isipv6;
352 	struct inpcb *inp = sotoinpcb(so);
353 	struct tcpcb *tp = intotcpcb(inp);
354 	int reason;
355 
356 	INP_WLOCK_ASSERT(inp);
357 	KASSERT(nam->sa_family == AF_INET || nam->sa_family == AF_INET6,
358 	    ("%s: dest addr %p has family %u", __func__, nam, nam->sa_family));
359 
360 	if (rt_ifp->if_type == IFT_ETHER)
361 		vi = rt_ifp->if_softc;
362 	else if (rt_ifp->if_type == IFT_L2VLAN) {
363 		struct ifnet *ifp = VLAN_COOKIE(rt_ifp);
364 
365 		vi = ifp->if_softc;
366 	} else if (rt_ifp->if_type == IFT_IEEE8023ADLAG)
367 		DONT_OFFLOAD_ACTIVE_OPEN(ENOSYS); /* XXX: implement lagg+TOE */
368 	else
369 		DONT_OFFLOAD_ACTIVE_OPEN(ENOTSUP);
370 
371 	toep = alloc_toepcb(vi, -1, -1, M_NOWAIT | M_ZERO);
372 	if (toep == NULL)
373 		DONT_OFFLOAD_ACTIVE_OPEN(ENOMEM);
374 
375 	toep->tid = alloc_atid(sc, toep);
376 	if (toep->tid < 0)
377 		DONT_OFFLOAD_ACTIVE_OPEN(ENOMEM);
378 
379 	toep->l2te = t4_l2t_get(vi->pi, rt_ifp,
380 	    rt->rt_flags & RTF_GATEWAY ? rt->rt_gateway : nam);
381 	if (toep->l2te == NULL)
382 		DONT_OFFLOAD_ACTIVE_OPEN(ENOMEM);
383 
384 	isipv6 = nam->sa_family == AF_INET6;
385 	wr = alloc_wrqe(act_open_cpl_size(sc, isipv6), toep->ctrlq);
386 	if (wr == NULL)
387 		DONT_OFFLOAD_ACTIVE_OPEN(ENOMEM);
388 
389 	toep->vnet = so->so_vnet;
390 	set_ulp_mode(toep, select_ulp_mode(so, sc));
391 	SOCKBUF_LOCK(&so->so_rcv);
392 	/* opt0 rcv_bufsiz initially, assumes its normal meaning later */
393 	toep->rx_credits = min(select_rcv_wnd(so) >> 10, M_RCV_BUFSIZ);
394 	SOCKBUF_UNLOCK(&so->so_rcv);
395 
396 	/*
397 	 * The kernel sets request_r_scale based on sb_max whereas we need to
398 	 * take hardware's MAX_RCV_WND into account too.  This is normally a
399 	 * no-op as MAX_RCV_WND is much larger than the default sb_max.
400 	 */
401 	if (tp->t_flags & TF_REQ_SCALE)
402 		rscale = tp->request_r_scale = select_rcv_wscale();
403 	else
404 		rscale = 0;
405 	mtu_idx = find_best_mtu_idx(sc, &inp->inp_inc, 0);
406 	qid_atid = (toep->ofld_rxq->iq.abs_id << 14) | toep->tid;
407 
408 	if (isipv6) {
409 		struct cpl_act_open_req6 *cpl = wrtod(wr);
410 		struct cpl_t5_act_open_req6 *cpl5 = (void *)cpl;
411 		struct cpl_t6_act_open_req6 *cpl6 = (void *)cpl;
412 
413 		if ((inp->inp_vflag & INP_IPV6) == 0)
414 			DONT_OFFLOAD_ACTIVE_OPEN(ENOTSUP);
415 
416 		toep->ce = hold_lip(td, &inp->in6p_laddr, NULL);
417 		if (toep->ce == NULL)
418 			DONT_OFFLOAD_ACTIVE_OPEN(ENOENT);
419 
420 		switch (chip_id(sc)) {
421 		case CHELSIO_T4:
422 			INIT_TP_WR(cpl, 0);
423 			cpl->params = select_ntuple(vi, toep->l2te);
424 			break;
425 		case CHELSIO_T5:
426 			INIT_TP_WR(cpl5, 0);
427 			cpl5->iss = htobe32(tp->iss);
428 			cpl5->params = select_ntuple(vi, toep->l2te);
429 			break;
430 		case CHELSIO_T6:
431 		default:
432 			INIT_TP_WR(cpl6, 0);
433 			cpl6->iss = htobe32(tp->iss);
434 			cpl6->params = select_ntuple(vi, toep->l2te);
435 			break;
436 		}
437 		OPCODE_TID(cpl) = htobe32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ6,
438 		    qid_atid));
439 		cpl->local_port = inp->inp_lport;
440 		cpl->local_ip_hi = *(uint64_t *)&inp->in6p_laddr.s6_addr[0];
441 		cpl->local_ip_lo = *(uint64_t *)&inp->in6p_laddr.s6_addr[8];
442 		cpl->peer_port = inp->inp_fport;
443 		cpl->peer_ip_hi = *(uint64_t *)&inp->in6p_faddr.s6_addr[0];
444 		cpl->peer_ip_lo = *(uint64_t *)&inp->in6p_faddr.s6_addr[8];
445 		cpl->opt0 = calc_opt0(so, vi, toep->l2te, mtu_idx, rscale,
446 		    toep->rx_credits, toep->ulp_mode);
447 		cpl->opt2 = calc_opt2a(so, toep);
448 	} else {
449 		struct cpl_act_open_req *cpl = wrtod(wr);
450 		struct cpl_t5_act_open_req *cpl5 = (void *)cpl;
451 		struct cpl_t6_act_open_req *cpl6 = (void *)cpl;
452 
453 		switch (chip_id(sc)) {
454 		case CHELSIO_T4:
455 			INIT_TP_WR(cpl, 0);
456 			cpl->params = select_ntuple(vi, toep->l2te);
457 			break;
458 		case CHELSIO_T5:
459 			INIT_TP_WR(cpl5, 0);
460 			cpl5->iss = htobe32(tp->iss);
461 			cpl5->params = select_ntuple(vi, toep->l2te);
462 			break;
463 		case CHELSIO_T6:
464 		default:
465 			INIT_TP_WR(cpl6, 0);
466 			cpl6->iss = htobe32(tp->iss);
467 			cpl6->params = select_ntuple(vi, toep->l2te);
468 			break;
469 		}
470 		OPCODE_TID(cpl) = htobe32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ,
471 		    qid_atid));
472 		inp_4tuple_get(inp, &cpl->local_ip, &cpl->local_port,
473 		    &cpl->peer_ip, &cpl->peer_port);
474 		cpl->opt0 = calc_opt0(so, vi, toep->l2te, mtu_idx, rscale,
475 		    toep->rx_credits, toep->ulp_mode);
476 		cpl->opt2 = calc_opt2a(so, toep);
477 	}
478 
479 	CTR5(KTR_CXGBE, "%s: atid %u (%s), toep %p, inp %p", __func__,
480 	    toep->tid, tcpstates[tp->t_state], toep, inp);
481 
482 	offload_socket(so, toep);
483 	rc = t4_l2t_send(sc, wr, toep->l2te);
484 	if (rc == 0) {
485 		toep->flags |= TPF_CPL_PENDING;
486 		return (0);
487 	}
488 
489 	undo_offload_socket(so);
490 	reason = __LINE__;
491 failed:
492 	CTR3(KTR_CXGBE, "%s: not offloading (%d), rc %d", __func__, reason, rc);
493 
494 	if (wr)
495 		free_wrqe(wr);
496 
497 	if (toep) {
498 		if (toep->tid >= 0)
499 			free_atid(sc, toep->tid);
500 		if (toep->l2te)
501 			t4_l2t_release(toep->l2te);
502 		if (toep->ce)
503 			release_lip(td, toep->ce);
504 		free_toepcb(toep);
505 	}
506 
507 	return (rc);
508 }
509 #endif
510