xref: /freebsd/sys/dev/cxgbe/tom/t4_connect.c (revision ca987d4641cdcd7f27e153db17c5bf064934faf5)
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/systm.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 #define TCPSTATES
53 #include <netinet/tcp_fsm.h>
54 #include <netinet/tcp_var.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 succeeded.
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 	u_int tid = GET_TID(cpl);
119 	u_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 	CURVNET_SET(toep->vnet);
130 	INP_WLOCK(inp);
131 	toep->tid = tid;
132 	insert_tid(sc, tid, toep, inp->inp_vflag & INP_IPV6 ? 2 : 1);
133 	if (inp->inp_flags & INP_DROPPED) {
134 
135 		/* socket closed by the kernel before hw told us it connected */
136 
137 		send_flowc_wr(toep, NULL);
138 		send_reset(sc, toep, be32toh(cpl->snd_isn));
139 		goto done;
140 	}
141 
142 	make_established(toep, cpl->snd_isn, cpl->rcv_isn, cpl->tcp_opt);
143 done:
144 	INP_WUNLOCK(inp);
145 	CURVNET_RESTORE();
146 	return (0);
147 }
148 
149 /*
150  * Convert an ACT_OPEN_RPL status to an errno.
151  */
152 static inline int
153 act_open_rpl_status_to_errno(int status)
154 {
155 
156 	switch (status) {
157 	case CPL_ERR_CONN_RESET:
158 		return (ECONNREFUSED);
159 	case CPL_ERR_ARP_MISS:
160 		return (EHOSTUNREACH);
161 	case CPL_ERR_CONN_TIMEDOUT:
162 		return (ETIMEDOUT);
163 	case CPL_ERR_TCAM_FULL:
164 		return (EAGAIN);
165 	case CPL_ERR_CONN_EXIST:
166 		log(LOG_ERR, "ACTIVE_OPEN_RPL: 4-tuple in use\n");
167 		return (EAGAIN);
168 	default:
169 		return (EIO);
170 	}
171 }
172 
173 void
174 act_open_failure_cleanup(struct adapter *sc, u_int atid, u_int status)
175 {
176 	struct toepcb *toep = lookup_atid(sc, atid);
177 	struct inpcb *inp = toep->inp;
178 	struct toedev *tod = &toep->td->tod;
179 
180 	free_atid(sc, atid);
181 	toep->tid = -1;
182 
183 	CURVNET_SET(toep->vnet);
184 	if (status != EAGAIN)
185 		INP_INFO_RLOCK(&V_tcbinfo);
186 	INP_WLOCK(inp);
187 	toe_connect_failed(tod, inp, status);
188 	final_cpl_received(toep);	/* unlocks inp */
189 	if (status != EAGAIN)
190 		INP_INFO_RUNLOCK(&V_tcbinfo);
191 	CURVNET_RESTORE();
192 }
193 
194 /*
195  * Active open failed.
196  */
197 static int
198 do_act_open_rpl(struct sge_iq *iq, const struct rss_header *rss,
199     struct mbuf *m)
200 {
201 	struct adapter *sc = iq->adapter;
202 	const struct cpl_act_open_rpl *cpl = (const void *)(rss + 1);
203 	u_int atid = G_TID_TID(G_AOPEN_ATID(be32toh(cpl->atid_status)));
204 	u_int status = G_AOPEN_STATUS(be32toh(cpl->atid_status));
205 	struct toepcb *toep = lookup_atid(sc, atid);
206 	int rc;
207 
208 	KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
209 	KASSERT(toep->tid == atid, ("%s: toep tid/atid mismatch", __func__));
210 
211 	CTR3(KTR_CXGBE, "%s: atid %u, status %u ", __func__, atid, status);
212 
213 	/* Ignore negative advice */
214 	if (negative_advice(status))
215 		return (0);
216 
217 	if (status && act_open_has_tid(status))
218 		release_tid(sc, GET_TID(cpl), toep->ctrlq);
219 
220 	rc = act_open_rpl_status_to_errno(status);
221 	act_open_failure_cleanup(sc, atid, rc);
222 
223 	return (0);
224 }
225 
226 /*
227  * Options2 for active open.
228  */
229 static uint32_t
230 calc_opt2a(struct socket *so, struct toepcb *toep)
231 {
232 	struct tcpcb *tp = so_sototcpcb(so);
233 	struct port_info *pi = toep->vi->pi;
234 	struct adapter *sc = pi->adapter;
235 	uint32_t opt2;
236 
237 	opt2 = V_TX_QUEUE(sc->params.tp.tx_modq[pi->tx_chan]) |
238 	    F_RSS_QUEUE_VALID | V_RSS_QUEUE(toep->ofld_rxq->iq.abs_id);
239 
240 	if (tp->t_flags & TF_SACK_PERMIT)
241 		opt2 |= F_SACK_EN;
242 
243 	if (tp->t_flags & TF_REQ_TSTMP)
244 		opt2 |= F_TSTAMPS_EN;
245 
246 	if (tp->t_flags & TF_REQ_SCALE)
247 		opt2 |= F_WND_SCALE_EN;
248 
249 	if (V_tcp_do_ecn)
250 		opt2 |= F_CCTRL_ECN;
251 
252 	/* RX_COALESCE is always a valid value (M_RX_COALESCE). */
253 	if (is_t4(sc))
254 		opt2 |= F_RX_COALESCE_VALID;
255 	else {
256 		opt2 |= F_T5_OPT_2_VALID;
257 		opt2 |= F_T5_ISS;
258 	}
259 	if (sc->tt.rx_coalesce)
260 		opt2 |= V_RX_COALESCE(M_RX_COALESCE);
261 
262 	if (sc->tt.cong_algorithm != -1)
263 		opt2 |= V_CONG_CNTRL(sc->tt.cong_algorithm & M_CONG_CNTRL);
264 
265 #ifdef USE_DDP_RX_FLOW_CONTROL
266 	if (toep->ulp_mode == ULP_MODE_TCPDDP)
267 		opt2 |= F_RX_FC_VALID | F_RX_FC_DDP;
268 #endif
269 
270 	return (htobe32(opt2));
271 }
272 
273 void
274 t4_init_connect_cpl_handlers(void)
275 {
276 
277 	t4_register_cpl_handler(CPL_ACT_ESTABLISH, do_act_establish);
278 	t4_register_cpl_handler(CPL_ACT_OPEN_RPL, do_act_open_rpl);
279 }
280 
281 void
282 t4_uninit_connect_cpl_handlers(void)
283 {
284 
285 	t4_register_cpl_handler(CPL_ACT_ESTABLISH, NULL);
286 	t4_register_cpl_handler(CPL_ACT_OPEN_RPL, NULL);
287 }
288 
289 #define DONT_OFFLOAD_ACTIVE_OPEN(x)	do { \
290 	reason = __LINE__; \
291 	rc = (x); \
292 	goto failed; \
293 } while (0)
294 
295 static inline int
296 act_open_cpl_size(struct adapter *sc, int isipv6)
297 {
298 	int idx;
299 	static const int sz_table[3][2] = {
300 		{
301 			sizeof (struct cpl_act_open_req),
302 			sizeof (struct cpl_act_open_req6)
303 		},
304 		{
305 			sizeof (struct cpl_t5_act_open_req),
306 			sizeof (struct cpl_t5_act_open_req6)
307 		},
308 		{
309 			sizeof (struct cpl_t6_act_open_req),
310 			sizeof (struct cpl_t6_act_open_req6)
311 		},
312 	};
313 
314 	MPASS(chip_id(sc) >= CHELSIO_T4);
315 	idx = min(chip_id(sc) - CHELSIO_T4, 2);
316 
317 	return (sz_table[idx][!!isipv6]);
318 }
319 
320 /*
321  * active open (soconnect).
322  *
323  * State of affairs on entry:
324  * soisconnecting (so_state |= SS_ISCONNECTING)
325  * tcbinfo not locked (This has changed - used to be WLOCKed)
326  * inp WLOCKed
327  * tp->t_state = TCPS_SYN_SENT
328  * rtalloc1, RT_UNLOCK on rt.
329  */
330 int
331 t4_connect(struct toedev *tod, struct socket *so, struct rtentry *rt,
332     struct sockaddr *nam)
333 {
334 	struct adapter *sc = tod->tod_softc;
335 	struct tom_data *td = tod_td(tod);
336 	struct toepcb *toep = NULL;
337 	struct wrqe *wr = NULL;
338 	struct ifnet *rt_ifp = rt->rt_ifp;
339 	struct vi_info *vi;
340 	int mtu_idx, rscale, qid_atid, rc, isipv6;
341 	struct inpcb *inp = sotoinpcb(so);
342 	struct tcpcb *tp = intotcpcb(inp);
343 	int reason;
344 
345 	INP_WLOCK_ASSERT(inp);
346 	KASSERT(nam->sa_family == AF_INET || nam->sa_family == AF_INET6,
347 	    ("%s: dest addr %p has family %u", __func__, nam, nam->sa_family));
348 
349 	if (rt_ifp->if_type == IFT_ETHER)
350 		vi = rt_ifp->if_softc;
351 	else if (rt_ifp->if_type == IFT_L2VLAN) {
352 		struct ifnet *ifp = VLAN_COOKIE(rt_ifp);
353 
354 		vi = ifp->if_softc;
355 	} else if (rt_ifp->if_type == IFT_IEEE8023ADLAG)
356 		DONT_OFFLOAD_ACTIVE_OPEN(ENOSYS); /* XXX: implement lagg+TOE */
357 	else
358 		DONT_OFFLOAD_ACTIVE_OPEN(ENOTSUP);
359 
360 	toep = alloc_toepcb(vi, -1, -1, M_NOWAIT | M_ZERO);
361 	if (toep == NULL)
362 		DONT_OFFLOAD_ACTIVE_OPEN(ENOMEM);
363 
364 	toep->tid = alloc_atid(sc, toep);
365 	if (toep->tid < 0)
366 		DONT_OFFLOAD_ACTIVE_OPEN(ENOMEM);
367 
368 	toep->l2te = t4_l2t_get(vi->pi, rt_ifp,
369 	    rt->rt_flags & RTF_GATEWAY ? rt->rt_gateway : nam);
370 	if (toep->l2te == NULL)
371 		DONT_OFFLOAD_ACTIVE_OPEN(ENOMEM);
372 
373 	isipv6 = nam->sa_family == AF_INET6;
374 	wr = alloc_wrqe(act_open_cpl_size(sc, isipv6), toep->ctrlq);
375 	if (wr == NULL)
376 		DONT_OFFLOAD_ACTIVE_OPEN(ENOMEM);
377 
378 	toep->vnet = so->so_vnet;
379 	if (sc->tt.ddp && (so->so_options & SO_NO_DDP) == 0)
380 		set_tcpddp_ulp_mode(toep);
381 	else
382 		toep->ulp_mode = ULP_MODE_NONE;
383 	SOCKBUF_LOCK(&so->so_rcv);
384 	/* opt0 rcv_bufsiz initially, assumes its normal meaning later */
385 	toep->rx_credits = min(select_rcv_wnd(so) >> 10, M_RCV_BUFSIZ);
386 	SOCKBUF_UNLOCK(&so->so_rcv);
387 
388 	/*
389 	 * The kernel sets request_r_scale based on sb_max whereas we need to
390 	 * take hardware's MAX_RCV_WND into account too.  This is normally a
391 	 * no-op as MAX_RCV_WND is much larger than the default sb_max.
392 	 */
393 	if (tp->t_flags & TF_REQ_SCALE)
394 		rscale = tp->request_r_scale = select_rcv_wscale();
395 	else
396 		rscale = 0;
397 	mtu_idx = find_best_mtu_idx(sc, &inp->inp_inc, 0);
398 	qid_atid = (toep->ofld_rxq->iq.abs_id << 14) | toep->tid;
399 
400 	if (isipv6) {
401 		struct cpl_act_open_req6 *cpl = wrtod(wr);
402 		struct cpl_t5_act_open_req6 *cpl5 = (void *)cpl;
403 		struct cpl_t6_act_open_req6 *cpl6 = (void *)cpl;
404 
405 		if ((inp->inp_vflag & INP_IPV6) == 0)
406 			DONT_OFFLOAD_ACTIVE_OPEN(ENOTSUP);
407 
408 		toep->ce = hold_lip(td, &inp->in6p_laddr, NULL);
409 		if (toep->ce == NULL)
410 			DONT_OFFLOAD_ACTIVE_OPEN(ENOENT);
411 
412 		switch (chip_id(sc)) {
413 		case CHELSIO_T4:
414 			INIT_TP_WR(cpl, 0);
415 			cpl->params = select_ntuple(vi, toep->l2te);
416 			break;
417 		case CHELSIO_T5:
418 			INIT_TP_WR(cpl5, 0);
419 			cpl5->iss = htobe32(tp->iss);
420 			cpl5->params = select_ntuple(vi, toep->l2te);
421 			break;
422 		case CHELSIO_T6:
423 		default:
424 			INIT_TP_WR(cpl6, 0);
425 			cpl6->iss = htobe32(tp->iss);
426 			cpl6->params = select_ntuple(vi, toep->l2te);
427 			break;
428 		}
429 		OPCODE_TID(cpl) = htobe32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ6,
430 		    qid_atid));
431 		cpl->local_port = inp->inp_lport;
432 		cpl->local_ip_hi = *(uint64_t *)&inp->in6p_laddr.s6_addr[0];
433 		cpl->local_ip_lo = *(uint64_t *)&inp->in6p_laddr.s6_addr[8];
434 		cpl->peer_port = inp->inp_fport;
435 		cpl->peer_ip_hi = *(uint64_t *)&inp->in6p_faddr.s6_addr[0];
436 		cpl->peer_ip_lo = *(uint64_t *)&inp->in6p_faddr.s6_addr[8];
437 		cpl->opt0 = calc_opt0(so, vi, toep->l2te, mtu_idx, rscale,
438 		    toep->rx_credits, toep->ulp_mode);
439 		cpl->opt2 = calc_opt2a(so, toep);
440 	} else {
441 		struct cpl_act_open_req *cpl = wrtod(wr);
442 		struct cpl_t5_act_open_req *cpl5 = (void *)cpl;
443 		struct cpl_t6_act_open_req *cpl6 = (void *)cpl;
444 
445 		switch (chip_id(sc)) {
446 		case CHELSIO_T4:
447 			INIT_TP_WR(cpl, 0);
448 			cpl->params = select_ntuple(vi, toep->l2te);
449 			break;
450 		case CHELSIO_T5:
451 			INIT_TP_WR(cpl5, 0);
452 			cpl5->iss = htobe32(tp->iss);
453 			cpl5->params = select_ntuple(vi, toep->l2te);
454 			break;
455 		case CHELSIO_T6:
456 		default:
457 			INIT_TP_WR(cpl6, 0);
458 			cpl6->iss = htobe32(tp->iss);
459 			cpl6->params = select_ntuple(vi, toep->l2te);
460 			break;
461 		}
462 		OPCODE_TID(cpl) = htobe32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ,
463 		    qid_atid));
464 		inp_4tuple_get(inp, &cpl->local_ip, &cpl->local_port,
465 		    &cpl->peer_ip, &cpl->peer_port);
466 		cpl->opt0 = calc_opt0(so, vi, toep->l2te, mtu_idx, rscale,
467 		    toep->rx_credits, toep->ulp_mode);
468 		cpl->opt2 = calc_opt2a(so, toep);
469 	}
470 
471 	CTR5(KTR_CXGBE, "%s: atid %u (%s), toep %p, inp %p", __func__,
472 	    toep->tid, tcpstates[tp->t_state], toep, inp);
473 
474 	offload_socket(so, toep);
475 	rc = t4_l2t_send(sc, wr, toep->l2te);
476 	if (rc == 0) {
477 		toep->flags |= TPF_CPL_PENDING;
478 		return (0);
479 	}
480 
481 	undo_offload_socket(so);
482 	reason = __LINE__;
483 failed:
484 	CTR3(KTR_CXGBE, "%s: not offloading (%d), rc %d", __func__, reason, rc);
485 
486 	if (wr)
487 		free_wrqe(wr);
488 
489 	if (toep) {
490 		if (toep->tid >= 0)
491 			free_atid(sc, toep->tid);
492 		if (toep->l2te)
493 			t4_l2t_release(toep->l2te);
494 		if (toep->ce)
495 			release_lip(td, toep->ce);
496 		free_toepcb(toep);
497 	}
498 
499 	return (rc);
500 }
501 #endif
502