2 * Copyright (c) 2012 Chelsio Communications, Inc.
4 * Written by: Navdeep Parhar <np@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 #include "opt_inet6.h"
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/kernel.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>
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>
54 #include <netinet/tcp_fsm.h>
55 #include <netinet/toecore.h>
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"
65 static int alloc_atid(struct adapter *, void *);
66 static void *lookup_atid(struct adapter *, int);
67 static void free_atid(struct adapter *, int);
70 alloc_atid(struct adapter *sc, void *ctx)
72 struct tid_info *t = &sc->tids;
75 mtx_lock(&t->atid_lock);
77 union aopen_entry *p = t->afree;
79 atid = p - t->atid_tab;
84 mtx_unlock(&t->atid_lock);
89 lookup_atid(struct adapter *sc, int atid)
91 struct tid_info *t = &sc->tids;
93 return (t->atid_tab[atid].data);
97 free_atid(struct adapter *sc, int atid)
99 struct tid_info *t = &sc->tids;
100 union aopen_entry *p = &t->atid_tab[atid];
102 mtx_lock(&t->atid_lock);
106 mtx_unlock(&t->atid_lock);
110 * Active open failed.
113 do_act_establish(struct sge_iq *iq, const struct rss_header *rss,
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;
123 KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
124 KASSERT(toep->tid == atid, ("%s: toep tid/atid mismatch", __func__));
126 CTR3(KTR_CXGBE, "%s: atid %u, tid %u", __func__, atid, tid);
131 insert_tid(sc, tid, toep);
132 if (inp->inp_flags & INP_DROPPED) {
134 /* socket closed by the kernel before hw told us it connected */
136 send_flowc_wr(toep, NULL);
137 send_reset(sc, toep, be32toh(cpl->snd_isn));
141 make_established(toep, cpl->snd_isn, cpl->rcv_isn, cpl->tcp_opt);
148 act_open_has_tid(unsigned int status)
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);
158 * Convert an ACT_OPEN_RPL status to an errno.
161 act_open_rpl_status_to_errno(int 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:
171 case CPL_ERR_TCAM_FULL:
173 case CPL_ERR_CONN_EXIST:
174 log(LOG_ERR, "ACTIVE_OPEN_RPL: 4-tuple in use\n");
182 act_open_failure_cleanup(struct adapter *sc, u_int atid, u_int status)
184 struct toepcb *toep = lookup_atid(sc, atid);
185 struct inpcb *inp = toep->inp;
186 struct toedev *tod = &toep->td->tod;
191 if (status != EAGAIN)
192 INP_INFO_WLOCK(&V_tcbinfo);
194 toe_connect_failed(tod, inp, status);
195 final_cpl_received(toep); /* unlocks inp */
196 if (status != EAGAIN)
197 INP_INFO_WUNLOCK(&V_tcbinfo);
201 do_act_open_rpl(struct sge_iq *iq, const struct rss_header *rss,
204 struct adapter *sc = iq->adapter;
205 const struct cpl_act_open_rpl *cpl = (const void *)(rss + 1);
206 u_int atid = G_TID_TID(G_AOPEN_ATID(be32toh(cpl->atid_status)));
207 u_int status = G_AOPEN_STATUS(be32toh(cpl->atid_status));
208 struct toepcb *toep = lookup_atid(sc, atid);
211 KASSERT(m == NULL, ("%s: wasn't expecting payload", __func__));
212 KASSERT(toep->tid == atid, ("%s: toep tid/atid mismatch", __func__));
214 CTR3(KTR_CXGBE, "%s: atid %u, status %u ", __func__, atid, status);
216 /* Ignore negative advice */
217 if (negative_advice(status))
220 if (status && act_open_has_tid(status))
221 release_tid(sc, GET_TID(cpl), toep->ctrlq);
223 rc = act_open_rpl_status_to_errno(status);
224 act_open_failure_cleanup(sc, atid, rc);
230 * Options2 for active open.
233 calc_opt2a(struct socket *so, struct toepcb *toep)
235 struct tcpcb *tp = so_sototcpcb(so);
236 struct port_info *pi = toep->port;
237 struct adapter *sc = pi->adapter;
240 opt2 = V_TX_QUEUE(sc->params.tp.tx_modq[pi->tx_chan]) |
241 F_RSS_QUEUE_VALID | V_RSS_QUEUE(toep->ofld_rxq->iq.abs_id);
243 if (tp->t_flags & TF_SACK_PERMIT)
246 if (tp->t_flags & TF_REQ_TSTMP)
247 opt2 |= F_TSTAMPS_EN;
249 if (tp->t_flags & TF_REQ_SCALE)
250 opt2 |= F_WND_SCALE_EN;
255 /* RX_COALESCE is always a valid value (M_RX_COALESCE). */
257 opt2 |= F_RX_COALESCE_VALID;
259 opt2 |= F_T5_OPT_2_VALID;
260 opt2 |= F_CONG_CNTRL_VALID; /* OPT_2_ISS really, for T5 */
262 if (sc->tt.rx_coalesce)
263 opt2 |= V_RX_COALESCE(M_RX_COALESCE);
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;
270 return (htobe32(opt2));
274 t4_init_connect_cpl_handlers(struct adapter *sc)
277 t4_register_cpl_handler(sc, CPL_ACT_ESTABLISH, do_act_establish);
278 t4_register_cpl_handler(sc, CPL_ACT_OPEN_RPL, do_act_open_rpl);
281 #define DONT_OFFLOAD_ACTIVE_OPEN(x) do { \
288 act_open_cpl_size(struct adapter *sc, int isipv6)
290 static const int sz_t4[] = {
291 sizeof (struct cpl_act_open_req),
292 sizeof (struct cpl_act_open_req6)
294 static const int sz_t5[] = {
295 sizeof (struct cpl_t5_act_open_req),
296 sizeof (struct cpl_t5_act_open_req6)
300 return (sz_t4[!!isipv6]);
302 return (sz_t5[!!isipv6]);
306 * active open (soconnect).
308 * State of affairs on entry:
309 * soisconnecting (so_state |= SS_ISCONNECTING)
310 * tcbinfo not locked (This has changed - used to be WLOCKed)
312 * tp->t_state = TCPS_SYN_SENT
313 * rtalloc1, RT_UNLOCK on rt.
316 t4_connect(struct toedev *tod, struct socket *so, struct rtentry *rt,
317 struct sockaddr *nam)
319 struct adapter *sc = tod->tod_softc;
320 struct tom_data *td = tod_td(tod);
321 struct toepcb *toep = NULL;
322 struct wrqe *wr = NULL;
323 struct ifnet *rt_ifp = rt->rt_ifp;
324 struct port_info *pi;
325 int mtu_idx, rscale, qid_atid, rc, isipv6;
326 struct inpcb *inp = sotoinpcb(so);
327 struct tcpcb *tp = intotcpcb(inp);
330 INP_WLOCK_ASSERT(inp);
331 KASSERT(nam->sa_family == AF_INET || nam->sa_family == AF_INET6,
332 ("%s: dest addr %p has family %u", __func__, nam, nam->sa_family));
334 if (rt_ifp->if_type == IFT_ETHER)
335 pi = rt_ifp->if_softc;
336 else if (rt_ifp->if_type == IFT_L2VLAN) {
337 struct ifnet *ifp = VLAN_COOKIE(rt_ifp);
340 } else if (rt_ifp->if_type == IFT_IEEE8023ADLAG)
341 DONT_OFFLOAD_ACTIVE_OPEN(ENOSYS); /* XXX: implement lagg+TOE */
343 DONT_OFFLOAD_ACTIVE_OPEN(ENOTSUP);
345 toep = alloc_toepcb(pi, -1, -1, M_NOWAIT);
347 DONT_OFFLOAD_ACTIVE_OPEN(ENOMEM);
349 toep->tid = alloc_atid(sc, toep);
351 DONT_OFFLOAD_ACTIVE_OPEN(ENOMEM);
353 toep->l2te = t4_l2t_get(pi, rt_ifp,
354 rt->rt_flags & RTF_GATEWAY ? rt->rt_gateway : nam);
355 if (toep->l2te == NULL)
356 DONT_OFFLOAD_ACTIVE_OPEN(ENOMEM);
358 isipv6 = nam->sa_family == AF_INET6;
359 wr = alloc_wrqe(act_open_cpl_size(sc, isipv6), toep->ctrlq);
361 DONT_OFFLOAD_ACTIVE_OPEN(ENOMEM);
363 if (sc->tt.ddp && (so->so_options & SO_NO_DDP) == 0)
364 set_tcpddp_ulp_mode(toep);
366 toep->ulp_mode = ULP_MODE_NONE;
367 SOCKBUF_LOCK(&so->so_rcv);
368 /* opt0 rcv_bufsiz initially, assumes its normal meaning later */
369 toep->rx_credits = min(select_rcv_wnd(so) >> 10, M_RCV_BUFSIZ);
370 SOCKBUF_UNLOCK(&so->so_rcv);
373 * The kernel sets request_r_scale based on sb_max whereas we need to
374 * take hardware's MAX_RCV_WND into account too. This is normally a
375 * no-op as MAX_RCV_WND is much larger than the default sb_max.
377 if (tp->t_flags & TF_REQ_SCALE)
378 rscale = tp->request_r_scale = select_rcv_wscale();
381 mtu_idx = find_best_mtu_idx(sc, &inp->inp_inc, 0);
382 qid_atid = (toep->ofld_rxq->iq.abs_id << 14) | toep->tid;
385 struct cpl_act_open_req6 *cpl = wrtod(wr);
387 if ((inp->inp_vflag & INP_IPV6) == 0) {
388 /* XXX think about this a bit more */
390 "%s: time to think about AF_INET6 + vflag 0x%x.\n",
391 __func__, inp->inp_vflag);
392 DONT_OFFLOAD_ACTIVE_OPEN(ENOTSUP);
395 toep->ce = hold_lip(td, &inp->in6p_laddr);
396 if (toep->ce == NULL)
397 DONT_OFFLOAD_ACTIVE_OPEN(ENOENT);
401 cpl->params = select_ntuple(pi, toep->l2te);
403 struct cpl_t5_act_open_req6 *c5 = (void *)cpl;
406 c5->iss = htobe32(tp->iss);
407 c5->params = select_ntuple(pi, toep->l2te);
409 OPCODE_TID(cpl) = htobe32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ6,
411 cpl->local_port = inp->inp_lport;
412 cpl->local_ip_hi = *(uint64_t *)&inp->in6p_laddr.s6_addr[0];
413 cpl->local_ip_lo = *(uint64_t *)&inp->in6p_laddr.s6_addr[8];
414 cpl->peer_port = inp->inp_fport;
415 cpl->peer_ip_hi = *(uint64_t *)&inp->in6p_faddr.s6_addr[0];
416 cpl->peer_ip_lo = *(uint64_t *)&inp->in6p_faddr.s6_addr[8];
417 cpl->opt0 = calc_opt0(so, pi, toep->l2te, mtu_idx, rscale,
418 toep->rx_credits, toep->ulp_mode);
419 cpl->opt2 = calc_opt2a(so, toep);
421 struct cpl_act_open_req *cpl = wrtod(wr);
425 cpl->params = select_ntuple(pi, toep->l2te);
427 struct cpl_t5_act_open_req *c5 = (void *)cpl;
430 c5->iss = htobe32(tp->iss);
431 c5->params = select_ntuple(pi, toep->l2te);
433 OPCODE_TID(cpl) = htobe32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ,
435 inp_4tuple_get(inp, &cpl->local_ip, &cpl->local_port,
436 &cpl->peer_ip, &cpl->peer_port);
437 cpl->opt0 = calc_opt0(so, pi, toep->l2te, mtu_idx, rscale,
438 toep->rx_credits, toep->ulp_mode);
439 cpl->opt2 = calc_opt2a(so, toep);
442 CTR5(KTR_CXGBE, "%s: atid %u (%s), toep %p, inp %p", __func__,
443 toep->tid, tcpstates[tp->t_state], toep, inp);
445 offload_socket(so, toep);
446 rc = t4_l2t_send(sc, wr, toep->l2te);
448 toep->flags |= TPF_CPL_PENDING;
452 undo_offload_socket(so);
455 CTR3(KTR_CXGBE, "%s: not offloading (%d), rc %d", __func__, reason, rc);
462 free_atid(sc, toep->tid);
464 t4_l2t_release(toep->l2te);
466 release_lip(td, toep->ce);