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 <[email protected]>
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 #include "opt_kern_tls.h"
36 #include "opt_ratelimit.h"
37
38 #include <sys/param.h>
39 #include <sys/types.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/ktr.h>
43 #include <sys/lock.h>
44 #include <sys/limits.h>
45 #include <sys/module.h>
46 #include <sys/protosw.h>
47 #include <sys/domain.h>
48 #include <sys/refcount.h>
49 #include <sys/rmlock.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/sysctl.h>
53 #include <sys/taskqueue.h>
54 #include <net/if.h>
55 #include <net/if_var.h>
56 #include <net/if_types.h>
57 #include <net/if_vlan_var.h>
58 #include <netinet/in.h>
59 #include <netinet/in_pcb.h>
60 #include <netinet/in_var.h>
61 #include <netinet/ip.h>
62 #include <netinet/ip6.h>
63 #include <netinet6/scope6_var.h>
64 #define TCPSTATES
65 #include <netinet/tcp_fsm.h>
66 #include <netinet/tcp_timer.h>
67 #include <netinet/tcp_var.h>
68 #include <netinet/toecore.h>
69 #include <netinet/cc/cc.h>
70
71 #ifdef TCP_OFFLOAD
72 #include "common/common.h"
73 #include "common/t4_msg.h"
74 #include "common/t4_regs.h"
75 #include "common/t4_regs_values.h"
76 #include "common/t4_tcb.h"
77 #include "t4_clip.h"
78 #include "tom/t4_tom_l2t.h"
79 #include "tom/t4_tom.h"
80 #include "tom/t4_tls.h"
81
82 static struct protosw *tcp_protosw;
83 static struct protosw toe_protosw;
84 static struct pr_usrreqs toe_usrreqs;
85
86 static struct protosw *tcp6_protosw;
87 static struct protosw toe6_protosw;
88 static struct pr_usrreqs toe6_usrreqs;
89
90 /* Module ops */
91 static int t4_tom_mod_load(void);
92 static int t4_tom_mod_unload(void);
93 static int t4_tom_modevent(module_t, int, void *);
94
95 /* ULD ops and helpers */
96 static int t4_tom_activate(struct adapter *);
97 static int t4_tom_deactivate(struct adapter *);
98
99 static struct uld_info tom_uld_info = {
100 .uld_id = ULD_TOM,
101 .activate = t4_tom_activate,
102 .deactivate = t4_tom_deactivate,
103 };
104
105 static void release_offload_resources(struct toepcb *);
106 static int alloc_tid_tabs(struct tid_info *);
107 static void free_tid_tabs(struct tid_info *);
108 static void free_tom_data(struct adapter *, struct tom_data *);
109 static void reclaim_wr_resources(void *, int);
110
111 struct toepcb *
alloc_toepcb(struct vi_info * vi,int flags)112 alloc_toepcb(struct vi_info *vi, int flags)
113 {
114 struct port_info *pi = vi->pi;
115 struct adapter *sc = pi->adapter;
116 struct toepcb *toep;
117 int tx_credits, txsd_total, len;
118
119 /*
120 * The firmware counts tx work request credits in units of 16 bytes
121 * each. Reserve room for an ABORT_REQ so the driver never has to worry
122 * about tx credits if it wants to abort a connection.
123 */
124 tx_credits = sc->params.ofldq_wr_cred;
125 tx_credits -= howmany(sizeof(struct cpl_abort_req), 16);
126
127 /*
128 * Shortest possible tx work request is a fw_ofld_tx_data_wr + 1 byte
129 * immediate payload, and firmware counts tx work request credits in
130 * units of 16 byte. Calculate the maximum work requests possible.
131 */
132 txsd_total = tx_credits /
133 howmany(sizeof(struct fw_ofld_tx_data_wr) + 1, 16);
134
135 len = offsetof(struct toepcb, txsd) +
136 txsd_total * sizeof(struct ofld_tx_sdesc);
137
138 toep = malloc(len, M_CXGBE, M_ZERO | flags);
139 if (toep == NULL)
140 return (NULL);
141
142 refcount_init(&toep->refcount, 1);
143 toep->td = sc->tom_softc;
144 toep->vi = vi;
145 toep->tid = -1;
146 toep->tx_total = tx_credits;
147 toep->tx_credits = tx_credits;
148 mbufq_init(&toep->ulp_pduq, INT_MAX);
149 mbufq_init(&toep->ulp_pdu_reclaimq, INT_MAX);
150 toep->txsd_total = txsd_total;
151 toep->txsd_avail = txsd_total;
152 toep->txsd_pidx = 0;
153 toep->txsd_cidx = 0;
154 aiotx_init_toep(toep);
155
156 return (toep);
157 }
158
159 /*
160 * Initialize a toepcb after its params have been filled out.
161 */
162 int
init_toepcb(struct vi_info * vi,struct toepcb * toep)163 init_toepcb(struct vi_info *vi, struct toepcb *toep)
164 {
165 struct conn_params *cp = &toep->params;
166 struct port_info *pi = vi->pi;
167 struct adapter *sc = pi->adapter;
168 struct tx_cl_rl_params *tc;
169
170 if (cp->tc_idx >= 0 && cp->tc_idx < sc->params.nsched_cls) {
171 tc = &pi->sched_params->cl_rl[cp->tc_idx];
172 mtx_lock(&sc->tc_lock);
173 if (tc->state != CS_HW_CONFIGURED) {
174 CH_ERR(vi, "tid %d cannot be bound to traffic class %d "
175 "because it is not configured (its state is %d)\n",
176 toep->tid, cp->tc_idx, tc->state);
177 cp->tc_idx = -1;
178 } else {
179 tc->refcount++;
180 }
181 mtx_unlock(&sc->tc_lock);
182 }
183 toep->ofld_txq = &sc->sge.ofld_txq[cp->txq_idx];
184 toep->ofld_rxq = &sc->sge.ofld_rxq[cp->rxq_idx];
185 toep->ctrlq = &sc->sge.ctrlq[pi->port_id];
186
187 tls_init_toep(toep);
188 if (ulp_mode(toep) == ULP_MODE_TCPDDP)
189 ddp_init_toep(toep);
190
191 toep->flags |= TPF_INITIALIZED;
192
193 return (0);
194 }
195
196 struct toepcb *
hold_toepcb(struct toepcb * toep)197 hold_toepcb(struct toepcb *toep)
198 {
199
200 refcount_acquire(&toep->refcount);
201 return (toep);
202 }
203
204 void
free_toepcb(struct toepcb * toep)205 free_toepcb(struct toepcb *toep)
206 {
207
208 if (refcount_release(&toep->refcount) == 0)
209 return;
210
211 KASSERT(!(toep->flags & TPF_ATTACHED),
212 ("%s: attached to an inpcb", __func__));
213 KASSERT(!(toep->flags & TPF_CPL_PENDING),
214 ("%s: CPL pending", __func__));
215
216 if (toep->flags & TPF_INITIALIZED) {
217 if (ulp_mode(toep) == ULP_MODE_TCPDDP)
218 ddp_uninit_toep(toep);
219 tls_uninit_toep(toep);
220 }
221 free(toep, M_CXGBE);
222 }
223
224 /*
225 * Set up the socket for TCP offload.
226 */
227 void
offload_socket(struct socket * so,struct toepcb * toep)228 offload_socket(struct socket *so, struct toepcb *toep)
229 {
230 struct tom_data *td = toep->td;
231 struct inpcb *inp = sotoinpcb(so);
232 struct tcpcb *tp = intotcpcb(inp);
233 struct sockbuf *sb;
234
235 INP_WLOCK_ASSERT(inp);
236
237 /* Update socket */
238 sb = &so->so_snd;
239 SOCKBUF_LOCK(sb);
240 sb->sb_flags |= SB_NOCOALESCE;
241 SOCKBUF_UNLOCK(sb);
242 sb = &so->so_rcv;
243 SOCKBUF_LOCK(sb);
244 sb->sb_flags |= SB_NOCOALESCE;
245 if (inp->inp_vflag & INP_IPV6)
246 so->so_proto = &toe6_protosw;
247 else
248 so->so_proto = &toe_protosw;
249 SOCKBUF_UNLOCK(sb);
250
251 /* Update TCP PCB */
252 tp->tod = &td->tod;
253 tp->t_toe = toep;
254 tp->t_flags |= TF_TOE;
255
256 /* Install an extra hold on inp */
257 toep->inp = inp;
258 toep->flags |= TPF_ATTACHED;
259 in_pcbref(inp);
260
261 /* Add the TOE PCB to the active list */
262 mtx_lock(&td->toep_list_lock);
263 TAILQ_INSERT_HEAD(&td->toep_list, toep, link);
264 mtx_unlock(&td->toep_list_lock);
265 }
266
267 void
restore_so_proto(struct socket * so,bool v6)268 restore_so_proto(struct socket *so, bool v6)
269 {
270 if (v6)
271 so->so_proto = tcp6_protosw;
272 else
273 so->so_proto = tcp_protosw;
274 }
275
276 /* This is _not_ the normal way to "unoffload" a socket. */
277 void
undo_offload_socket(struct socket * so)278 undo_offload_socket(struct socket *so)
279 {
280 struct inpcb *inp = sotoinpcb(so);
281 struct tcpcb *tp = intotcpcb(inp);
282 struct toepcb *toep = tp->t_toe;
283 struct tom_data *td = toep->td;
284 struct sockbuf *sb;
285
286 INP_WLOCK_ASSERT(inp);
287
288 sb = &so->so_snd;
289 SOCKBUF_LOCK(sb);
290 sb->sb_flags &= ~SB_NOCOALESCE;
291 SOCKBUF_UNLOCK(sb);
292 sb = &so->so_rcv;
293 SOCKBUF_LOCK(sb);
294 sb->sb_flags &= ~SB_NOCOALESCE;
295 restore_so_proto(so, inp->inp_vflag & INP_IPV6);
296 SOCKBUF_UNLOCK(sb);
297
298 tp->tod = NULL;
299 tp->t_toe = NULL;
300 tp->t_flags &= ~TF_TOE;
301
302 toep->inp = NULL;
303 toep->flags &= ~TPF_ATTACHED;
304 if (in_pcbrele_wlocked(inp))
305 panic("%s: inp freed.", __func__);
306
307 mtx_lock(&td->toep_list_lock);
308 TAILQ_REMOVE(&td->toep_list, toep, link);
309 mtx_unlock(&td->toep_list_lock);
310 }
311
312 static void
release_offload_resources(struct toepcb * toep)313 release_offload_resources(struct toepcb *toep)
314 {
315 struct tom_data *td = toep->td;
316 struct adapter *sc = td_adapter(td);
317 int tid = toep->tid;
318
319 KASSERT(!(toep->flags & TPF_CPL_PENDING),
320 ("%s: %p has CPL pending.", __func__, toep));
321 KASSERT(!(toep->flags & TPF_ATTACHED),
322 ("%s: %p is still attached.", __func__, toep));
323
324 CTR5(KTR_CXGBE, "%s: toep %p (tid %d, l2te %p, ce %p)",
325 __func__, toep, tid, toep->l2te, toep->ce);
326
327 /*
328 * These queues should have been emptied at approximately the same time
329 * that a normal connection's socket's so_snd would have been purged or
330 * drained. Do _not_ clean up here.
331 */
332 MPASS(mbufq_len(&toep->ulp_pduq) == 0);
333 MPASS(mbufq_len(&toep->ulp_pdu_reclaimq) == 0);
334 #ifdef INVARIANTS
335 if (ulp_mode(toep) == ULP_MODE_TCPDDP)
336 ddp_assert_empty(toep);
337 #endif
338 MPASS(TAILQ_EMPTY(&toep->aiotx_jobq));
339
340 if (toep->l2te)
341 t4_l2t_release(toep->l2te);
342
343 if (tid >= 0) {
344 remove_tid(sc, tid, toep->ce ? 2 : 1);
345 release_tid(sc, tid, toep->ctrlq);
346 }
347
348 if (toep->ce)
349 t4_release_clip_entry(sc, toep->ce);
350
351 if (toep->params.tc_idx != -1)
352 t4_release_cl_rl(sc, toep->vi->pi->port_id, toep->params.tc_idx);
353
354 mtx_lock(&td->toep_list_lock);
355 TAILQ_REMOVE(&td->toep_list, toep, link);
356 mtx_unlock(&td->toep_list_lock);
357
358 free_toepcb(toep);
359 }
360
361 /*
362 * The kernel is done with the TCP PCB and this is our opportunity to unhook the
363 * toepcb hanging off of it. If the TOE driver is also done with the toepcb (no
364 * pending CPL) then it is time to release all resources tied to the toepcb.
365 *
366 * Also gets called when an offloaded active open fails and the TOM wants the
367 * kernel to take the TCP PCB back.
368 */
369 static void
t4_pcb_detach(struct toedev * tod __unused,struct tcpcb * tp)370 t4_pcb_detach(struct toedev *tod __unused, struct tcpcb *tp)
371 {
372 #if defined(KTR) || defined(INVARIANTS)
373 struct inpcb *inp = tp->t_inpcb;
374 #endif
375 struct toepcb *toep = tp->t_toe;
376
377 INP_WLOCK_ASSERT(inp);
378
379 KASSERT(toep != NULL, ("%s: toep is NULL", __func__));
380 KASSERT(toep->flags & TPF_ATTACHED,
381 ("%s: not attached", __func__));
382
383 #ifdef KTR
384 if (tp->t_state == TCPS_SYN_SENT) {
385 CTR6(KTR_CXGBE, "%s: atid %d, toep %p (0x%x), inp %p (0x%x)",
386 __func__, toep->tid, toep, toep->flags, inp,
387 inp->inp_flags);
388 } else {
389 CTR6(KTR_CXGBE,
390 "t4_pcb_detach: tid %d (%s), toep %p (0x%x), inp %p (0x%x)",
391 toep->tid, tcpstates[tp->t_state], toep, toep->flags, inp,
392 inp->inp_flags);
393 }
394 #endif
395
396 if (ulp_mode(toep) == ULP_MODE_TLS)
397 tls_detach(toep);
398
399 tp->tod = NULL;
400 tp->t_toe = NULL;
401 tp->t_flags &= ~TF_TOE;
402 toep->flags &= ~TPF_ATTACHED;
403
404 if (!(toep->flags & TPF_CPL_PENDING))
405 release_offload_resources(toep);
406 }
407
408 /*
409 * setsockopt handler.
410 */
411 static void
t4_ctloutput(struct toedev * tod,struct tcpcb * tp,int dir,int name)412 t4_ctloutput(struct toedev *tod, struct tcpcb *tp, int dir, int name)
413 {
414 struct adapter *sc = tod->tod_softc;
415 struct toepcb *toep = tp->t_toe;
416
417 if (dir == SOPT_GET)
418 return;
419
420 CTR4(KTR_CXGBE, "%s: tp %p, dir %u, name %u", __func__, tp, dir, name);
421
422 switch (name) {
423 case TCP_NODELAY:
424 if (tp->t_state != TCPS_ESTABLISHED)
425 break;
426 toep->params.nagle = tp->t_flags & TF_NODELAY ? 0 : 1;
427 t4_set_tcb_field(sc, toep->ctrlq, toep, W_TCB_T_FLAGS,
428 V_TF_NAGLE(1), V_TF_NAGLE(toep->params.nagle), 0, 0);
429 break;
430 default:
431 break;
432 }
433 }
434
435 static inline uint64_t
get_tcb_tflags(const uint64_t * tcb)436 get_tcb_tflags(const uint64_t *tcb)
437 {
438
439 return ((be64toh(tcb[14]) << 32) | (be64toh(tcb[15]) >> 32));
440 }
441
442 static inline uint32_t
get_tcb_field(const uint64_t * tcb,u_int word,uint32_t mask,u_int shift)443 get_tcb_field(const uint64_t *tcb, u_int word, uint32_t mask, u_int shift)
444 {
445 #define LAST_WORD ((TCB_SIZE / 4) - 1)
446 uint64_t t1, t2;
447 int flit_idx;
448
449 MPASS(mask != 0);
450 MPASS(word <= LAST_WORD);
451 MPASS(shift < 32);
452
453 flit_idx = (LAST_WORD - word) / 2;
454 if (word & 0x1)
455 shift += 32;
456 t1 = be64toh(tcb[flit_idx]) >> shift;
457 t2 = 0;
458 if (fls(mask) > 64 - shift) {
459 /*
460 * Will spill over into the next logical flit, which is the flit
461 * before this one. The flit_idx before this one must be valid.
462 */
463 MPASS(flit_idx > 0);
464 t2 = be64toh(tcb[flit_idx - 1]) << (64 - shift);
465 }
466 return ((t2 | t1) & mask);
467 #undef LAST_WORD
468 }
469 #define GET_TCB_FIELD(tcb, F) \
470 get_tcb_field(tcb, W_TCB_##F, M_TCB_##F, S_TCB_##F)
471
472 /*
473 * Issues a CPL_GET_TCB to read the entire TCB for the tid.
474 */
475 static int
send_get_tcb(struct adapter * sc,u_int tid)476 send_get_tcb(struct adapter *sc, u_int tid)
477 {
478 struct cpl_get_tcb *cpl;
479 struct wrq_cookie cookie;
480
481 MPASS(tid < sc->tids.ntids);
482
483 cpl = start_wrq_wr(&sc->sge.ctrlq[0], howmany(sizeof(*cpl), 16),
484 &cookie);
485 if (__predict_false(cpl == NULL))
486 return (ENOMEM);
487 bzero(cpl, sizeof(*cpl));
488 INIT_TP_WR(cpl, tid);
489 OPCODE_TID(cpl) = htobe32(MK_OPCODE_TID(CPL_GET_TCB, tid));
490 cpl->reply_ctrl = htobe16(V_REPLY_CHAN(0) |
491 V_QUEUENO(sc->sge.ofld_rxq[0].iq.cntxt_id));
492 cpl->cookie = 0xff;
493 commit_wrq_wr(&sc->sge.ctrlq[0], cpl, &cookie);
494
495 return (0);
496 }
497
498 static struct tcb_histent *
alloc_tcb_histent(struct adapter * sc,u_int tid,int flags)499 alloc_tcb_histent(struct adapter *sc, u_int tid, int flags)
500 {
501 struct tcb_histent *te;
502
503 MPASS(flags == M_NOWAIT || flags == M_WAITOK);
504
505 te = malloc(sizeof(*te), M_CXGBE, M_ZERO | flags);
506 if (te == NULL)
507 return (NULL);
508 mtx_init(&te->te_lock, "TCB entry", NULL, MTX_DEF);
509 callout_init_mtx(&te->te_callout, &te->te_lock, 0);
510 te->te_adapter = sc;
511 te->te_tid = tid;
512
513 return (te);
514 }
515
516 static void
free_tcb_histent(struct tcb_histent * te)517 free_tcb_histent(struct tcb_histent *te)
518 {
519
520 mtx_destroy(&te->te_lock);
521 free(te, M_CXGBE);
522 }
523
524 /*
525 * Start tracking the tid in the TCB history.
526 */
527 int
add_tid_to_history(struct adapter * sc,u_int tid)528 add_tid_to_history(struct adapter *sc, u_int tid)
529 {
530 struct tcb_histent *te = NULL;
531 struct tom_data *td = sc->tom_softc;
532 int rc;
533
534 MPASS(tid < sc->tids.ntids);
535
536 if (td->tcb_history == NULL)
537 return (ENXIO);
538
539 rw_wlock(&td->tcb_history_lock);
540 if (td->tcb_history[tid] != NULL) {
541 rc = EEXIST;
542 goto done;
543 }
544 te = alloc_tcb_histent(sc, tid, M_NOWAIT);
545 if (te == NULL) {
546 rc = ENOMEM;
547 goto done;
548 }
549 mtx_lock(&te->te_lock);
550 rc = send_get_tcb(sc, tid);
551 if (rc == 0) {
552 te->te_flags |= TE_RPL_PENDING;
553 td->tcb_history[tid] = te;
554 } else {
555 free(te, M_CXGBE);
556 }
557 mtx_unlock(&te->te_lock);
558 done:
559 rw_wunlock(&td->tcb_history_lock);
560 return (rc);
561 }
562
563 static void
remove_tcb_histent(struct tcb_histent * te)564 remove_tcb_histent(struct tcb_histent *te)
565 {
566 struct adapter *sc = te->te_adapter;
567 struct tom_data *td = sc->tom_softc;
568
569 rw_assert(&td->tcb_history_lock, RA_WLOCKED);
570 mtx_assert(&te->te_lock, MA_OWNED);
571 MPASS(td->tcb_history[te->te_tid] == te);
572
573 td->tcb_history[te->te_tid] = NULL;
574 free_tcb_histent(te);
575 rw_wunlock(&td->tcb_history_lock);
576 }
577
578 static inline struct tcb_histent *
lookup_tcb_histent(struct adapter * sc,u_int tid,bool addrem)579 lookup_tcb_histent(struct adapter *sc, u_int tid, bool addrem)
580 {
581 struct tcb_histent *te;
582 struct tom_data *td = sc->tom_softc;
583
584 MPASS(tid < sc->tids.ntids);
585
586 if (td->tcb_history == NULL)
587 return (NULL);
588
589 if (addrem)
590 rw_wlock(&td->tcb_history_lock);
591 else
592 rw_rlock(&td->tcb_history_lock);
593 te = td->tcb_history[tid];
594 if (te != NULL) {
595 mtx_lock(&te->te_lock);
596 return (te); /* with both locks held */
597 }
598 if (addrem)
599 rw_wunlock(&td->tcb_history_lock);
600 else
601 rw_runlock(&td->tcb_history_lock);
602
603 return (te);
604 }
605
606 static inline void
release_tcb_histent(struct tcb_histent * te)607 release_tcb_histent(struct tcb_histent *te)
608 {
609 struct adapter *sc = te->te_adapter;
610 struct tom_data *td = sc->tom_softc;
611
612 mtx_assert(&te->te_lock, MA_OWNED);
613 mtx_unlock(&te->te_lock);
614 rw_assert(&td->tcb_history_lock, RA_RLOCKED);
615 rw_runlock(&td->tcb_history_lock);
616 }
617
618 static void
request_tcb(void * arg)619 request_tcb(void *arg)
620 {
621 struct tcb_histent *te = arg;
622
623 mtx_assert(&te->te_lock, MA_OWNED);
624
625 /* Noone else is supposed to update the histent. */
626 MPASS(!(te->te_flags & TE_RPL_PENDING));
627 if (send_get_tcb(te->te_adapter, te->te_tid) == 0)
628 te->te_flags |= TE_RPL_PENDING;
629 else
630 callout_schedule(&te->te_callout, hz / 100);
631 }
632
633 static void
update_tcb_histent(struct tcb_histent * te,const uint64_t * tcb)634 update_tcb_histent(struct tcb_histent *te, const uint64_t *tcb)
635 {
636 struct tom_data *td = te->te_adapter->tom_softc;
637 uint64_t tflags = get_tcb_tflags(tcb);
638 uint8_t sample = 0;
639
640 if (GET_TCB_FIELD(tcb, SND_MAX_RAW) != GET_TCB_FIELD(tcb, SND_UNA_RAW)) {
641 if (GET_TCB_FIELD(tcb, T_RXTSHIFT) != 0)
642 sample |= TS_RTO;
643 if (GET_TCB_FIELD(tcb, T_DUPACKS) != 0)
644 sample |= TS_DUPACKS;
645 if (GET_TCB_FIELD(tcb, T_DUPACKS) >= td->dupack_threshold)
646 sample |= TS_FASTREXMT;
647 }
648
649 if (GET_TCB_FIELD(tcb, SND_MAX_RAW) != 0) {
650 uint32_t snd_wnd;
651
652 sample |= TS_SND_BACKLOGGED; /* for whatever reason. */
653
654 snd_wnd = GET_TCB_FIELD(tcb, RCV_ADV);
655 if (tflags & V_TF_RECV_SCALE(1))
656 snd_wnd <<= GET_TCB_FIELD(tcb, RCV_SCALE);
657 if (GET_TCB_FIELD(tcb, SND_CWND) < snd_wnd)
658 sample |= TS_CWND_LIMITED; /* maybe due to CWND */
659 }
660
661 if (tflags & V_TF_CCTRL_ECN(1)) {
662
663 /*
664 * CE marker on incoming IP hdr, echoing ECE back in the TCP
665 * hdr. Indicates congestion somewhere on the way from the peer
666 * to this node.
667 */
668 if (tflags & V_TF_CCTRL_ECE(1))
669 sample |= TS_ECN_ECE;
670
671 /*
672 * ECE seen and CWR sent (or about to be sent). Might indicate
673 * congestion on the way to the peer. This node is reducing its
674 * congestion window in response.
675 */
676 if (tflags & (V_TF_CCTRL_CWR(1) | V_TF_CCTRL_RFR(1)))
677 sample |= TS_ECN_CWR;
678 }
679
680 te->te_sample[te->te_pidx] = sample;
681 if (++te->te_pidx == nitems(te->te_sample))
682 te->te_pidx = 0;
683 memcpy(te->te_tcb, tcb, TCB_SIZE);
684 te->te_flags |= TE_ACTIVE;
685 }
686
687 static int
do_get_tcb_rpl(struct sge_iq * iq,const struct rss_header * rss,struct mbuf * m)688 do_get_tcb_rpl(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
689 {
690 struct adapter *sc = iq->adapter;
691 const struct cpl_get_tcb_rpl *cpl = mtod(m, const void *);
692 const uint64_t *tcb = (const uint64_t *)(const void *)(cpl + 1);
693 struct tcb_histent *te;
694 const u_int tid = GET_TID(cpl);
695 bool remove;
696
697 remove = GET_TCB_FIELD(tcb, T_STATE) == TCPS_CLOSED;
698 te = lookup_tcb_histent(sc, tid, remove);
699 if (te == NULL) {
700 /* Not in the history. Who issued the GET_TCB for this? */
701 device_printf(sc->dev, "tcb %u: flags 0x%016jx, state %u, "
702 "srtt %u, sscale %u, rscale %u, cookie 0x%x\n", tid,
703 (uintmax_t)get_tcb_tflags(tcb), GET_TCB_FIELD(tcb, T_STATE),
704 GET_TCB_FIELD(tcb, T_SRTT), GET_TCB_FIELD(tcb, SND_SCALE),
705 GET_TCB_FIELD(tcb, RCV_SCALE), cpl->cookie);
706 goto done;
707 }
708
709 MPASS(te->te_flags & TE_RPL_PENDING);
710 te->te_flags &= ~TE_RPL_PENDING;
711 if (remove) {
712 remove_tcb_histent(te);
713 } else {
714 update_tcb_histent(te, tcb);
715 callout_reset(&te->te_callout, hz / 10, request_tcb, te);
716 release_tcb_histent(te);
717 }
718 done:
719 m_freem(m);
720 return (0);
721 }
722
723 static void
fill_tcp_info_from_tcb(struct adapter * sc,uint64_t * tcb,struct tcp_info * ti)724 fill_tcp_info_from_tcb(struct adapter *sc, uint64_t *tcb, struct tcp_info *ti)
725 {
726 uint32_t v;
727
728 ti->tcpi_state = GET_TCB_FIELD(tcb, T_STATE);
729
730 v = GET_TCB_FIELD(tcb, T_SRTT);
731 ti->tcpi_rtt = tcp_ticks_to_us(sc, v);
732
733 v = GET_TCB_FIELD(tcb, T_RTTVAR);
734 ti->tcpi_rttvar = tcp_ticks_to_us(sc, v);
735
736 ti->tcpi_snd_ssthresh = GET_TCB_FIELD(tcb, SND_SSTHRESH);
737 ti->tcpi_snd_cwnd = GET_TCB_FIELD(tcb, SND_CWND);
738 ti->tcpi_rcv_nxt = GET_TCB_FIELD(tcb, RCV_NXT);
739
740 v = GET_TCB_FIELD(tcb, TX_MAX);
741 ti->tcpi_snd_nxt = v - GET_TCB_FIELD(tcb, SND_NXT_RAW);
742
743 /* Receive window being advertised by us. */
744 ti->tcpi_rcv_wscale = GET_TCB_FIELD(tcb, SND_SCALE); /* Yes, SND. */
745 ti->tcpi_rcv_space = GET_TCB_FIELD(tcb, RCV_WND);
746
747 /* Send window */
748 ti->tcpi_snd_wscale = GET_TCB_FIELD(tcb, RCV_SCALE); /* Yes, RCV. */
749 ti->tcpi_snd_wnd = GET_TCB_FIELD(tcb, RCV_ADV);
750 if (get_tcb_tflags(tcb) & V_TF_RECV_SCALE(1))
751 ti->tcpi_snd_wnd <<= ti->tcpi_snd_wscale;
752 else
753 ti->tcpi_snd_wscale = 0;
754
755 }
756
757 static void
fill_tcp_info_from_history(struct adapter * sc,struct tcb_histent * te,struct tcp_info * ti)758 fill_tcp_info_from_history(struct adapter *sc, struct tcb_histent *te,
759 struct tcp_info *ti)
760 {
761
762 fill_tcp_info_from_tcb(sc, te->te_tcb, ti);
763 }
764
765 /*
766 * Reads the TCB for the given tid using a memory window and copies it to 'buf'
767 * in the same format as CPL_GET_TCB_RPL.
768 */
769 static void
read_tcb_using_memwin(struct adapter * sc,u_int tid,uint64_t * buf)770 read_tcb_using_memwin(struct adapter *sc, u_int tid, uint64_t *buf)
771 {
772 int i, j, k, rc;
773 uint32_t addr;
774 u_char *tcb, tmp;
775
776 MPASS(tid < sc->tids.ntids);
777
778 addr = t4_read_reg(sc, A_TP_CMM_TCB_BASE) + tid * TCB_SIZE;
779 rc = read_via_memwin(sc, 2, addr, (uint32_t *)buf, TCB_SIZE);
780 if (rc != 0)
781 return;
782
783 tcb = (u_char *)buf;
784 for (i = 0, j = TCB_SIZE - 16; i < j; i += 16, j -= 16) {
785 for (k = 0; k < 16; k++) {
786 tmp = tcb[i + k];
787 tcb[i + k] = tcb[j + k];
788 tcb[j + k] = tmp;
789 }
790 }
791 }
792
793 static void
fill_tcp_info(struct adapter * sc,u_int tid,struct tcp_info * ti)794 fill_tcp_info(struct adapter *sc, u_int tid, struct tcp_info *ti)
795 {
796 uint64_t tcb[TCB_SIZE / sizeof(uint64_t)];
797 struct tcb_histent *te;
798
799 ti->tcpi_toe_tid = tid;
800 te = lookup_tcb_histent(sc, tid, false);
801 if (te != NULL) {
802 fill_tcp_info_from_history(sc, te, ti);
803 release_tcb_histent(te);
804 } else {
805 if (!(sc->debug_flags & DF_DISABLE_TCB_CACHE)) {
806 /* XXX: tell firmware to flush TCB cache. */
807 }
808 read_tcb_using_memwin(sc, tid, tcb);
809 fill_tcp_info_from_tcb(sc, tcb, ti);
810 }
811 }
812
813 /*
814 * Called by the kernel to allow the TOE driver to "refine" values filled up in
815 * the tcp_info for an offloaded connection.
816 */
817 static void
t4_tcp_info(struct toedev * tod,struct tcpcb * tp,struct tcp_info * ti)818 t4_tcp_info(struct toedev *tod, struct tcpcb *tp, struct tcp_info *ti)
819 {
820 struct adapter *sc = tod->tod_softc;
821 struct toepcb *toep = tp->t_toe;
822
823 INP_WLOCK_ASSERT(tp->t_inpcb);
824 MPASS(ti != NULL);
825
826 fill_tcp_info(sc, toep->tid, ti);
827 }
828
829 #ifdef KERN_TLS
830 static int
t4_alloc_tls_session(struct toedev * tod,struct tcpcb * tp,struct ktls_session * tls,int direction)831 t4_alloc_tls_session(struct toedev *tod, struct tcpcb *tp,
832 struct ktls_session *tls, int direction)
833 {
834 struct toepcb *toep = tp->t_toe;
835
836 INP_WLOCK_ASSERT(tp->t_inpcb);
837 MPASS(tls != NULL);
838
839 return (tls_alloc_ktls(toep, tls, direction));
840 }
841 #endif
842
843 /*
844 * The TOE driver will not receive any more CPLs for the tid associated with the
845 * toepcb; release the hold on the inpcb.
846 */
847 void
final_cpl_received(struct toepcb * toep)848 final_cpl_received(struct toepcb *toep)
849 {
850 struct inpcb *inp = toep->inp;
851 bool need_wakeup;
852
853 KASSERT(inp != NULL, ("%s: inp is NULL", __func__));
854 INP_WLOCK_ASSERT(inp);
855 KASSERT(toep->flags & TPF_CPL_PENDING,
856 ("%s: CPL not pending already?", __func__));
857
858 CTR6(KTR_CXGBE, "%s: tid %d, toep %p (0x%x), inp %p (0x%x)",
859 __func__, toep->tid, toep, toep->flags, inp, inp->inp_flags);
860
861 if (ulp_mode(toep) == ULP_MODE_TCPDDP)
862 release_ddp_resources(toep);
863 else if (ulp_mode(toep) == ULP_MODE_TLS)
864 tls_detach(toep);
865 toep->inp = NULL;
866 need_wakeup = (toep->flags & TPF_WAITING_FOR_FINAL) != 0;
867 toep->flags &= ~(TPF_CPL_PENDING | TPF_WAITING_FOR_FINAL);
868 mbufq_drain(&toep->ulp_pduq);
869 mbufq_drain(&toep->ulp_pdu_reclaimq);
870
871 if (!(toep->flags & TPF_ATTACHED))
872 release_offload_resources(toep);
873
874 if (!in_pcbrele_wlocked(inp))
875 INP_WUNLOCK(inp);
876
877 if (need_wakeup) {
878 struct mtx *lock = mtx_pool_find(mtxpool_sleep, toep);
879
880 mtx_lock(lock);
881 wakeup(toep);
882 mtx_unlock(lock);
883 }
884 }
885
886 void
insert_tid(struct adapter * sc,int tid,void * ctx,int ntids)887 insert_tid(struct adapter *sc, int tid, void *ctx, int ntids)
888 {
889 struct tid_info *t = &sc->tids;
890
891 MPASS(tid >= t->tid_base);
892 MPASS(tid - t->tid_base < t->ntids);
893
894 t->tid_tab[tid - t->tid_base] = ctx;
895 atomic_add_int(&t->tids_in_use, ntids);
896 }
897
898 void *
lookup_tid(struct adapter * sc,int tid)899 lookup_tid(struct adapter *sc, int tid)
900 {
901 struct tid_info *t = &sc->tids;
902
903 return (t->tid_tab[tid - t->tid_base]);
904 }
905
906 void
update_tid(struct adapter * sc,int tid,void * ctx)907 update_tid(struct adapter *sc, int tid, void *ctx)
908 {
909 struct tid_info *t = &sc->tids;
910
911 t->tid_tab[tid - t->tid_base] = ctx;
912 }
913
914 void
remove_tid(struct adapter * sc,int tid,int ntids)915 remove_tid(struct adapter *sc, int tid, int ntids)
916 {
917 struct tid_info *t = &sc->tids;
918
919 t->tid_tab[tid - t->tid_base] = NULL;
920 atomic_subtract_int(&t->tids_in_use, ntids);
921 }
922
923 /*
924 * What mtu_idx to use, given a 4-tuple. Note that both s->mss and tcp_mssopt
925 * have the MSS that we should advertise in our SYN. Advertised MSS doesn't
926 * account for any TCP options so the effective MSS (only payload, no headers or
927 * options) could be different.
928 */
929 static int
find_best_mtu_idx(struct adapter * sc,struct in_conninfo * inc,struct offload_settings * s)930 find_best_mtu_idx(struct adapter *sc, struct in_conninfo *inc,
931 struct offload_settings *s)
932 {
933 unsigned short *mtus = &sc->params.mtus[0];
934 int i, mss, mtu;
935
936 MPASS(inc != NULL);
937
938 mss = s->mss > 0 ? s->mss : tcp_mssopt(inc);
939 if (inc->inc_flags & INC_ISIPV6)
940 mtu = mss + sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
941 else
942 mtu = mss + sizeof(struct ip) + sizeof(struct tcphdr);
943
944 for (i = 0; i < NMTUS - 1 && mtus[i + 1] <= mtu; i++)
945 continue;
946
947 return (i);
948 }
949
950 /*
951 * Determine the receive window size for a socket.
952 */
953 u_long
select_rcv_wnd(struct socket * so)954 select_rcv_wnd(struct socket *so)
955 {
956 unsigned long wnd;
957
958 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
959
960 wnd = sbspace(&so->so_rcv);
961 if (wnd < MIN_RCV_WND)
962 wnd = MIN_RCV_WND;
963
964 return min(wnd, MAX_RCV_WND);
965 }
966
967 int
select_rcv_wscale(void)968 select_rcv_wscale(void)
969 {
970 int wscale = 0;
971 unsigned long space = sb_max;
972
973 if (space > MAX_RCV_WND)
974 space = MAX_RCV_WND;
975
976 while (wscale < TCP_MAX_WINSHIFT && (TCP_MAXWIN << wscale) < space)
977 wscale++;
978
979 return (wscale);
980 }
981
982 __be64
calc_options0(struct vi_info * vi,struct conn_params * cp)983 calc_options0(struct vi_info *vi, struct conn_params *cp)
984 {
985 uint64_t opt0 = 0;
986
987 opt0 |= F_TCAM_BYPASS;
988
989 MPASS(cp->wscale >= 0 && cp->wscale <= M_WND_SCALE);
990 opt0 |= V_WND_SCALE(cp->wscale);
991
992 MPASS(cp->mtu_idx >= 0 && cp->mtu_idx < NMTUS);
993 opt0 |= V_MSS_IDX(cp->mtu_idx);
994
995 MPASS(cp->ulp_mode >= 0 && cp->ulp_mode <= M_ULP_MODE);
996 opt0 |= V_ULP_MODE(cp->ulp_mode);
997
998 MPASS(cp->opt0_bufsize >= 0 && cp->opt0_bufsize <= M_RCV_BUFSIZ);
999 opt0 |= V_RCV_BUFSIZ(cp->opt0_bufsize);
1000
1001 MPASS(cp->l2t_idx >= 0 && cp->l2t_idx < vi->adapter->vres.l2t.size);
1002 opt0 |= V_L2T_IDX(cp->l2t_idx);
1003
1004 opt0 |= V_SMAC_SEL(vi->smt_idx);
1005 opt0 |= V_TX_CHAN(vi->pi->tx_chan);
1006
1007 MPASS(cp->keepalive == 0 || cp->keepalive == 1);
1008 opt0 |= V_KEEP_ALIVE(cp->keepalive);
1009
1010 MPASS(cp->nagle == 0 || cp->nagle == 1);
1011 opt0 |= V_NAGLE(cp->nagle);
1012
1013 return (htobe64(opt0));
1014 }
1015
1016 __be32
calc_options2(struct vi_info * vi,struct conn_params * cp)1017 calc_options2(struct vi_info *vi, struct conn_params *cp)
1018 {
1019 uint32_t opt2 = 0;
1020 struct port_info *pi = vi->pi;
1021 struct adapter *sc = pi->adapter;
1022
1023 /*
1024 * rx flow control, rx coalesce, congestion control, and tx pace are all
1025 * explicitly set by the driver. On T5+ the ISS is also set by the
1026 * driver to the value picked by the kernel.
1027 */
1028 if (is_t4(sc)) {
1029 opt2 |= F_RX_FC_VALID | F_RX_COALESCE_VALID;
1030 opt2 |= F_CONG_CNTRL_VALID | F_PACE_VALID;
1031 } else {
1032 opt2 |= F_T5_OPT_2_VALID; /* all 4 valid */
1033 opt2 |= F_T5_ISS; /* ISS provided in CPL */
1034 }
1035
1036 MPASS(cp->sack == 0 || cp->sack == 1);
1037 opt2 |= V_SACK_EN(cp->sack);
1038
1039 MPASS(cp->tstamp == 0 || cp->tstamp == 1);
1040 opt2 |= V_TSTAMPS_EN(cp->tstamp);
1041
1042 if (cp->wscale > 0)
1043 opt2 |= F_WND_SCALE_EN;
1044
1045 MPASS(cp->ecn == 0 || cp->ecn == 1);
1046 opt2 |= V_CCTRL_ECN(cp->ecn);
1047
1048 /* XXX: F_RX_CHANNEL for multiple rx c-chan support goes here. */
1049
1050 opt2 |= V_TX_QUEUE(sc->params.tp.tx_modq[pi->tx_chan]);
1051 opt2 |= V_PACE(0);
1052 opt2 |= F_RSS_QUEUE_VALID;
1053 opt2 |= V_RSS_QUEUE(sc->sge.ofld_rxq[cp->rxq_idx].iq.abs_id);
1054
1055 MPASS(cp->cong_algo >= 0 && cp->cong_algo <= M_CONG_CNTRL);
1056 opt2 |= V_CONG_CNTRL(cp->cong_algo);
1057
1058 MPASS(cp->rx_coalesce == 0 || cp->rx_coalesce == 1);
1059 if (cp->rx_coalesce == 1)
1060 opt2 |= V_RX_COALESCE(M_RX_COALESCE);
1061
1062 opt2 |= V_RX_FC_DDP(0) | V_RX_FC_DISABLE(0);
1063 #ifdef USE_DDP_RX_FLOW_CONTROL
1064 if (cp->ulp_mode == ULP_MODE_TCPDDP)
1065 opt2 |= F_RX_FC_DDP;
1066 #endif
1067
1068 return (htobe32(opt2));
1069 }
1070
1071 uint64_t
select_ntuple(struct vi_info * vi,struct l2t_entry * e)1072 select_ntuple(struct vi_info *vi, struct l2t_entry *e)
1073 {
1074 struct adapter *sc = vi->adapter;
1075 struct tp_params *tp = &sc->params.tp;
1076 uint64_t ntuple = 0;
1077
1078 /*
1079 * Initialize each of the fields which we care about which are present
1080 * in the Compressed Filter Tuple.
1081 */
1082 if (tp->vlan_shift >= 0 && EVL_VLANOFTAG(e->vlan) != CPL_L2T_VLAN_NONE)
1083 ntuple |= (uint64_t)(F_FT_VLAN_VLD | e->vlan) << tp->vlan_shift;
1084
1085 if (tp->port_shift >= 0)
1086 ntuple |= (uint64_t)e->lport << tp->port_shift;
1087
1088 if (tp->protocol_shift >= 0)
1089 ntuple |= (uint64_t)IPPROTO_TCP << tp->protocol_shift;
1090
1091 if (tp->vnic_shift >= 0 && tp->vnic_mode == FW_VNIC_MODE_PF_VF) {
1092 ntuple |= (uint64_t)(V_FT_VNID_ID_VF(vi->vin) |
1093 V_FT_VNID_ID_PF(sc->pf) | V_FT_VNID_ID_VLD(vi->vfvld)) <<
1094 tp->vnic_shift;
1095 }
1096
1097 if (is_t4(sc))
1098 return (htobe32((uint32_t)ntuple));
1099 else
1100 return (htobe64(V_FILTER_TUPLE(ntuple)));
1101 }
1102
1103 static int
is_tls_sock(struct socket * so,struct adapter * sc)1104 is_tls_sock(struct socket *so, struct adapter *sc)
1105 {
1106 struct inpcb *inp = sotoinpcb(so);
1107 int i, rc;
1108
1109 /* XXX: Eventually add a SO_WANT_TLS socket option perhaps? */
1110 rc = 0;
1111 ADAPTER_LOCK(sc);
1112 for (i = 0; i < sc->tt.num_tls_rx_ports; i++) {
1113 if (inp->inp_lport == htons(sc->tt.tls_rx_ports[i]) ||
1114 inp->inp_fport == htons(sc->tt.tls_rx_ports[i])) {
1115 rc = 1;
1116 break;
1117 }
1118 }
1119 ADAPTER_UNLOCK(sc);
1120 return (rc);
1121 }
1122
1123 /*
1124 * Initialize various connection parameters.
1125 */
1126 void
init_conn_params(struct vi_info * vi,struct offload_settings * s,struct in_conninfo * inc,struct socket * so,const struct tcp_options * tcpopt,int16_t l2t_idx,struct conn_params * cp)1127 init_conn_params(struct vi_info *vi , struct offload_settings *s,
1128 struct in_conninfo *inc, struct socket *so,
1129 const struct tcp_options *tcpopt, int16_t l2t_idx, struct conn_params *cp)
1130 {
1131 struct port_info *pi = vi->pi;
1132 struct adapter *sc = pi->adapter;
1133 struct tom_tunables *tt = &sc->tt;
1134 struct inpcb *inp = sotoinpcb(so);
1135 struct tcpcb *tp = intotcpcb(inp);
1136 u_long wnd;
1137
1138 MPASS(s->offload != 0);
1139
1140 /* Congestion control algorithm */
1141 if (s->cong_algo >= 0)
1142 cp->cong_algo = s->cong_algo & M_CONG_CNTRL;
1143 else if (sc->tt.cong_algorithm >= 0)
1144 cp->cong_algo = tt->cong_algorithm & M_CONG_CNTRL;
1145 else {
1146 struct cc_algo *cc = CC_ALGO(tp);
1147
1148 if (strcasecmp(cc->name, "reno") == 0)
1149 cp->cong_algo = CONG_ALG_RENO;
1150 else if (strcasecmp(cc->name, "tahoe") == 0)
1151 cp->cong_algo = CONG_ALG_TAHOE;
1152 if (strcasecmp(cc->name, "newreno") == 0)
1153 cp->cong_algo = CONG_ALG_NEWRENO;
1154 if (strcasecmp(cc->name, "highspeed") == 0)
1155 cp->cong_algo = CONG_ALG_HIGHSPEED;
1156 else {
1157 /*
1158 * Use newreno in case the algorithm selected by the
1159 * host stack is not supported by the hardware.
1160 */
1161 cp->cong_algo = CONG_ALG_NEWRENO;
1162 }
1163 }
1164
1165 /* Tx traffic scheduling class. */
1166 if (s->sched_class >= 0 && s->sched_class < sc->params.nsched_cls)
1167 cp->tc_idx = s->sched_class;
1168 else
1169 cp->tc_idx = -1;
1170
1171 /* Nagle's algorithm. */
1172 if (s->nagle >= 0)
1173 cp->nagle = s->nagle > 0 ? 1 : 0;
1174 else
1175 cp->nagle = tp->t_flags & TF_NODELAY ? 0 : 1;
1176
1177 /* TCP Keepalive. */
1178 if (V_tcp_always_keepalive || so_options_get(so) & SO_KEEPALIVE)
1179 cp->keepalive = 1;
1180 else
1181 cp->keepalive = 0;
1182
1183 /* Optimization that's specific to T5 @ 40G. */
1184 if (tt->tx_align >= 0)
1185 cp->tx_align = tt->tx_align > 0 ? 1 : 0;
1186 else if (chip_id(sc) == CHELSIO_T5 &&
1187 (port_top_speed(pi) > 10 || sc->params.nports > 2))
1188 cp->tx_align = 1;
1189 else
1190 cp->tx_align = 0;
1191
1192 /* ULP mode. */
1193 if (can_tls_offload(sc) &&
1194 (s->tls > 0 || (s->tls < 0 && is_tls_sock(so, sc))))
1195 cp->ulp_mode = ULP_MODE_TLS;
1196 else if (s->ddp > 0 ||
1197 (s->ddp < 0 && sc->tt.ddp && (so_options_get(so) & SO_NO_DDP) == 0))
1198 cp->ulp_mode = ULP_MODE_TCPDDP;
1199 else
1200 cp->ulp_mode = ULP_MODE_NONE;
1201
1202 /* Rx coalescing. */
1203 if (s->rx_coalesce >= 0)
1204 cp->rx_coalesce = s->rx_coalesce > 0 ? 1 : 0;
1205 else if (cp->ulp_mode == ULP_MODE_TLS)
1206 cp->rx_coalesce = 0;
1207 else if (tt->rx_coalesce >= 0)
1208 cp->rx_coalesce = tt->rx_coalesce > 0 ? 1 : 0;
1209 else
1210 cp->rx_coalesce = 1; /* default */
1211
1212 /*
1213 * Index in the PMTU table. This controls the MSS that we announce in
1214 * our SYN initially, but after ESTABLISHED it controls the MSS that we
1215 * use to send data.
1216 */
1217 cp->mtu_idx = find_best_mtu_idx(sc, inc, s);
1218
1219 /* Tx queue for this connection. */
1220 if (s->txq >= 0 && s->txq < vi->nofldtxq)
1221 cp->txq_idx = s->txq;
1222 else
1223 cp->txq_idx = arc4random() % vi->nofldtxq;
1224 cp->txq_idx += vi->first_ofld_txq;
1225
1226 /* Rx queue for this connection. */
1227 if (s->rxq >= 0 && s->rxq < vi->nofldrxq)
1228 cp->rxq_idx = s->rxq;
1229 else
1230 cp->rxq_idx = arc4random() % vi->nofldrxq;
1231 cp->rxq_idx += vi->first_ofld_rxq;
1232
1233 if (SOLISTENING(so)) {
1234 /* Passive open */
1235 MPASS(tcpopt != NULL);
1236
1237 /* TCP timestamp option */
1238 if (tcpopt->tstamp &&
1239 (s->tstamp > 0 || (s->tstamp < 0 && V_tcp_do_rfc1323)))
1240 cp->tstamp = 1;
1241 else
1242 cp->tstamp = 0;
1243
1244 /* SACK */
1245 if (tcpopt->sack &&
1246 (s->sack > 0 || (s->sack < 0 && V_tcp_do_sack)))
1247 cp->sack = 1;
1248 else
1249 cp->sack = 0;
1250
1251 /* Receive window scaling. */
1252 if (tcpopt->wsf > 0 && tcpopt->wsf < 15 && V_tcp_do_rfc1323)
1253 cp->wscale = select_rcv_wscale();
1254 else
1255 cp->wscale = 0;
1256
1257 /* ECN */
1258 if (tcpopt->ecn && /* XXX: review. */
1259 (s->ecn > 0 || (s->ecn < 0 && V_tcp_do_ecn)))
1260 cp->ecn = 1;
1261 else
1262 cp->ecn = 0;
1263
1264 wnd = max(so->sol_sbrcv_hiwat, MIN_RCV_WND);
1265 cp->opt0_bufsize = min(wnd >> 10, M_RCV_BUFSIZ);
1266
1267 if (tt->sndbuf > 0)
1268 cp->sndbuf = tt->sndbuf;
1269 else if (so->sol_sbsnd_flags & SB_AUTOSIZE &&
1270 V_tcp_do_autosndbuf)
1271 cp->sndbuf = 256 * 1024;
1272 else
1273 cp->sndbuf = so->sol_sbsnd_hiwat;
1274 } else {
1275 /* Active open */
1276
1277 /* TCP timestamp option */
1278 if (s->tstamp > 0 ||
1279 (s->tstamp < 0 && (tp->t_flags & TF_REQ_TSTMP)))
1280 cp->tstamp = 1;
1281 else
1282 cp->tstamp = 0;
1283
1284 /* SACK */
1285 if (s->sack > 0 ||
1286 (s->sack < 0 && (tp->t_flags & TF_SACK_PERMIT)))
1287 cp->sack = 1;
1288 else
1289 cp->sack = 0;
1290
1291 /* Receive window scaling */
1292 if (tp->t_flags & TF_REQ_SCALE)
1293 cp->wscale = select_rcv_wscale();
1294 else
1295 cp->wscale = 0;
1296
1297 /* ECN */
1298 if (s->ecn > 0 || (s->ecn < 0 && V_tcp_do_ecn == 1))
1299 cp->ecn = 1;
1300 else
1301 cp->ecn = 0;
1302
1303 SOCKBUF_LOCK(&so->so_rcv);
1304 wnd = max(select_rcv_wnd(so), MIN_RCV_WND);
1305 SOCKBUF_UNLOCK(&so->so_rcv);
1306 cp->opt0_bufsize = min(wnd >> 10, M_RCV_BUFSIZ);
1307
1308 if (tt->sndbuf > 0)
1309 cp->sndbuf = tt->sndbuf;
1310 else {
1311 SOCKBUF_LOCK(&so->so_snd);
1312 if (so->so_snd.sb_flags & SB_AUTOSIZE &&
1313 V_tcp_do_autosndbuf)
1314 cp->sndbuf = 256 * 1024;
1315 else
1316 cp->sndbuf = so->so_snd.sb_hiwat;
1317 SOCKBUF_UNLOCK(&so->so_snd);
1318 }
1319 }
1320
1321 cp->l2t_idx = l2t_idx;
1322
1323 /* This will be initialized on ESTABLISHED. */
1324 cp->emss = 0;
1325 }
1326
1327 int
negative_advice(int status)1328 negative_advice(int status)
1329 {
1330
1331 return (status == CPL_ERR_RTX_NEG_ADVICE ||
1332 status == CPL_ERR_PERSIST_NEG_ADVICE ||
1333 status == CPL_ERR_KEEPALV_NEG_ADVICE);
1334 }
1335
1336 static int
alloc_tid_tab(struct tid_info * t,int flags)1337 alloc_tid_tab(struct tid_info *t, int flags)
1338 {
1339
1340 MPASS(t->ntids > 0);
1341 MPASS(t->tid_tab == NULL);
1342
1343 t->tid_tab = malloc(t->ntids * sizeof(*t->tid_tab), M_CXGBE,
1344 M_ZERO | flags);
1345 if (t->tid_tab == NULL)
1346 return (ENOMEM);
1347 atomic_store_rel_int(&t->tids_in_use, 0);
1348
1349 return (0);
1350 }
1351
1352 static void
free_tid_tab(struct tid_info * t)1353 free_tid_tab(struct tid_info *t)
1354 {
1355
1356 KASSERT(t->tids_in_use == 0,
1357 ("%s: %d tids still in use.", __func__, t->tids_in_use));
1358
1359 free(t->tid_tab, M_CXGBE);
1360 t->tid_tab = NULL;
1361 }
1362
1363 static int
alloc_stid_tab(struct tid_info * t,int flags)1364 alloc_stid_tab(struct tid_info *t, int flags)
1365 {
1366
1367 MPASS(t->nstids > 0);
1368 MPASS(t->stid_tab == NULL);
1369
1370 t->stid_tab = malloc(t->nstids * sizeof(*t->stid_tab), M_CXGBE,
1371 M_ZERO | flags);
1372 if (t->stid_tab == NULL)
1373 return (ENOMEM);
1374 mtx_init(&t->stid_lock, "stid lock", NULL, MTX_DEF);
1375 t->stids_in_use = 0;
1376 TAILQ_INIT(&t->stids);
1377 t->nstids_free_head = t->nstids;
1378
1379 return (0);
1380 }
1381
1382 static void
free_stid_tab(struct tid_info * t)1383 free_stid_tab(struct tid_info *t)
1384 {
1385
1386 KASSERT(t->stids_in_use == 0,
1387 ("%s: %d tids still in use.", __func__, t->stids_in_use));
1388
1389 if (mtx_initialized(&t->stid_lock))
1390 mtx_destroy(&t->stid_lock);
1391 free(t->stid_tab, M_CXGBE);
1392 t->stid_tab = NULL;
1393 }
1394
1395 static void
free_tid_tabs(struct tid_info * t)1396 free_tid_tabs(struct tid_info *t)
1397 {
1398
1399 free_tid_tab(t);
1400 free_stid_tab(t);
1401 }
1402
1403 static int
alloc_tid_tabs(struct tid_info * t)1404 alloc_tid_tabs(struct tid_info *t)
1405 {
1406 int rc;
1407
1408 rc = alloc_tid_tab(t, M_NOWAIT);
1409 if (rc != 0)
1410 goto failed;
1411
1412 rc = alloc_stid_tab(t, M_NOWAIT);
1413 if (rc != 0)
1414 goto failed;
1415
1416 return (0);
1417 failed:
1418 free_tid_tabs(t);
1419 return (rc);
1420 }
1421
1422 static inline void
alloc_tcb_history(struct adapter * sc,struct tom_data * td)1423 alloc_tcb_history(struct adapter *sc, struct tom_data *td)
1424 {
1425
1426 if (sc->tids.ntids == 0 || sc->tids.ntids > 1024)
1427 return;
1428 rw_init(&td->tcb_history_lock, "TCB history");
1429 td->tcb_history = malloc(sc->tids.ntids * sizeof(*td->tcb_history),
1430 M_CXGBE, M_ZERO | M_NOWAIT);
1431 td->dupack_threshold = G_DUPACKTHRESH(t4_read_reg(sc, A_TP_PARA_REG0));
1432 }
1433
1434 static inline void
free_tcb_history(struct adapter * sc,struct tom_data * td)1435 free_tcb_history(struct adapter *sc, struct tom_data *td)
1436 {
1437 #ifdef INVARIANTS
1438 int i;
1439
1440 if (td->tcb_history != NULL) {
1441 for (i = 0; i < sc->tids.ntids; i++) {
1442 MPASS(td->tcb_history[i] == NULL);
1443 }
1444 }
1445 #endif
1446 free(td->tcb_history, M_CXGBE);
1447 if (rw_initialized(&td->tcb_history_lock))
1448 rw_destroy(&td->tcb_history_lock);
1449 }
1450
1451 static void
free_tom_data(struct adapter * sc,struct tom_data * td)1452 free_tom_data(struct adapter *sc, struct tom_data *td)
1453 {
1454
1455 ASSERT_SYNCHRONIZED_OP(sc);
1456
1457 KASSERT(TAILQ_EMPTY(&td->toep_list),
1458 ("%s: TOE PCB list is not empty.", __func__));
1459 KASSERT(td->lctx_count == 0,
1460 ("%s: lctx hash table is not empty.", __func__));
1461
1462 t4_free_ppod_region(&td->pr);
1463
1464 if (td->listen_mask != 0)
1465 hashdestroy(td->listen_hash, M_CXGBE, td->listen_mask);
1466
1467 if (mtx_initialized(&td->unsent_wr_lock))
1468 mtx_destroy(&td->unsent_wr_lock);
1469 if (mtx_initialized(&td->lctx_hash_lock))
1470 mtx_destroy(&td->lctx_hash_lock);
1471 if (mtx_initialized(&td->toep_list_lock))
1472 mtx_destroy(&td->toep_list_lock);
1473
1474 free_tcb_history(sc, td);
1475 free_tid_tabs(&sc->tids);
1476 free(td, M_CXGBE);
1477 }
1478
1479 static char *
prepare_pkt(int open_type,uint16_t vtag,struct inpcb * inp,int * pktlen,int * buflen)1480 prepare_pkt(int open_type, uint16_t vtag, struct inpcb *inp, int *pktlen,
1481 int *buflen)
1482 {
1483 char *pkt;
1484 struct tcphdr *th;
1485 int ipv6, len;
1486 const int maxlen =
1487 max(sizeof(struct ether_header), sizeof(struct ether_vlan_header)) +
1488 max(sizeof(struct ip), sizeof(struct ip6_hdr)) +
1489 sizeof(struct tcphdr);
1490
1491 MPASS(open_type == OPEN_TYPE_ACTIVE || open_type == OPEN_TYPE_LISTEN);
1492
1493 pkt = malloc(maxlen, M_CXGBE, M_ZERO | M_NOWAIT);
1494 if (pkt == NULL)
1495 return (NULL);
1496
1497 ipv6 = inp->inp_vflag & INP_IPV6;
1498 len = 0;
1499
1500 if (EVL_VLANOFTAG(vtag) == 0xfff) {
1501 struct ether_header *eh = (void *)pkt;
1502
1503 if (ipv6)
1504 eh->ether_type = htons(ETHERTYPE_IPV6);
1505 else
1506 eh->ether_type = htons(ETHERTYPE_IP);
1507
1508 len += sizeof(*eh);
1509 } else {
1510 struct ether_vlan_header *evh = (void *)pkt;
1511
1512 evh->evl_encap_proto = htons(ETHERTYPE_VLAN);
1513 evh->evl_tag = htons(vtag);
1514 if (ipv6)
1515 evh->evl_proto = htons(ETHERTYPE_IPV6);
1516 else
1517 evh->evl_proto = htons(ETHERTYPE_IP);
1518
1519 len += sizeof(*evh);
1520 }
1521
1522 if (ipv6) {
1523 struct ip6_hdr *ip6 = (void *)&pkt[len];
1524
1525 ip6->ip6_vfc = IPV6_VERSION;
1526 ip6->ip6_plen = htons(sizeof(struct tcphdr));
1527 ip6->ip6_nxt = IPPROTO_TCP;
1528 if (open_type == OPEN_TYPE_ACTIVE) {
1529 ip6->ip6_src = inp->in6p_laddr;
1530 ip6->ip6_dst = inp->in6p_faddr;
1531 } else if (open_type == OPEN_TYPE_LISTEN) {
1532 ip6->ip6_src = inp->in6p_laddr;
1533 ip6->ip6_dst = ip6->ip6_src;
1534 }
1535
1536 len += sizeof(*ip6);
1537 } else {
1538 struct ip *ip = (void *)&pkt[len];
1539
1540 ip->ip_v = IPVERSION;
1541 ip->ip_hl = sizeof(*ip) >> 2;
1542 ip->ip_tos = inp->inp_ip_tos;
1543 ip->ip_len = htons(sizeof(struct ip) + sizeof(struct tcphdr));
1544 ip->ip_ttl = inp->inp_ip_ttl;
1545 ip->ip_p = IPPROTO_TCP;
1546 if (open_type == OPEN_TYPE_ACTIVE) {
1547 ip->ip_src = inp->inp_laddr;
1548 ip->ip_dst = inp->inp_faddr;
1549 } else if (open_type == OPEN_TYPE_LISTEN) {
1550 ip->ip_src = inp->inp_laddr;
1551 ip->ip_dst = ip->ip_src;
1552 }
1553
1554 len += sizeof(*ip);
1555 }
1556
1557 th = (void *)&pkt[len];
1558 if (open_type == OPEN_TYPE_ACTIVE) {
1559 th->th_sport = inp->inp_lport; /* network byte order already */
1560 th->th_dport = inp->inp_fport; /* ditto */
1561 } else if (open_type == OPEN_TYPE_LISTEN) {
1562 th->th_sport = inp->inp_lport; /* network byte order already */
1563 th->th_dport = th->th_sport;
1564 }
1565 len += sizeof(th);
1566
1567 *pktlen = *buflen = len;
1568 return (pkt);
1569 }
1570
1571 const struct offload_settings *
lookup_offload_policy(struct adapter * sc,int open_type,struct mbuf * m,uint16_t vtag,struct inpcb * inp)1572 lookup_offload_policy(struct adapter *sc, int open_type, struct mbuf *m,
1573 uint16_t vtag, struct inpcb *inp)
1574 {
1575 const struct t4_offload_policy *op;
1576 char *pkt;
1577 struct offload_rule *r;
1578 int i, matched, pktlen, buflen;
1579 static const struct offload_settings allow_offloading_settings = {
1580 .offload = 1,
1581 .rx_coalesce = -1,
1582 .cong_algo = -1,
1583 .sched_class = -1,
1584 .tstamp = -1,
1585 .sack = -1,
1586 .nagle = -1,
1587 .ecn = -1,
1588 .ddp = -1,
1589 .tls = -1,
1590 .txq = -1,
1591 .rxq = -1,
1592 .mss = -1,
1593 };
1594 static const struct offload_settings disallow_offloading_settings = {
1595 .offload = 0,
1596 /* rest is irrelevant when offload is off. */
1597 };
1598
1599 rw_assert(&sc->policy_lock, RA_LOCKED);
1600
1601 /*
1602 * If there's no Connection Offloading Policy attached to the device
1603 * then we need to return a default static policy. If
1604 * "cop_managed_offloading" is true, then we need to disallow
1605 * offloading until a COP is attached to the device. Otherwise we
1606 * allow offloading ...
1607 */
1608 op = sc->policy;
1609 if (op == NULL) {
1610 if (sc->tt.cop_managed_offloading)
1611 return (&disallow_offloading_settings);
1612 else
1613 return (&allow_offloading_settings);
1614 }
1615
1616 switch (open_type) {
1617 case OPEN_TYPE_ACTIVE:
1618 case OPEN_TYPE_LISTEN:
1619 pkt = prepare_pkt(open_type, vtag, inp, &pktlen, &buflen);
1620 break;
1621 case OPEN_TYPE_PASSIVE:
1622 MPASS(m != NULL);
1623 pkt = mtod(m, char *);
1624 MPASS(*pkt == CPL_PASS_ACCEPT_REQ);
1625 pkt += sizeof(struct cpl_pass_accept_req);
1626 pktlen = m->m_pkthdr.len - sizeof(struct cpl_pass_accept_req);
1627 buflen = m->m_len - sizeof(struct cpl_pass_accept_req);
1628 break;
1629 default:
1630 MPASS(0);
1631 return (&disallow_offloading_settings);
1632 }
1633
1634 if (pkt == NULL || pktlen == 0 || buflen == 0)
1635 return (&disallow_offloading_settings);
1636
1637 matched = 0;
1638 r = &op->rule[0];
1639 for (i = 0; i < op->nrules; i++, r++) {
1640 if (r->open_type != open_type &&
1641 r->open_type != OPEN_TYPE_DONTCARE) {
1642 continue;
1643 }
1644 matched = bpf_filter(r->bpf_prog.bf_insns, pkt, pktlen, buflen);
1645 if (matched)
1646 break;
1647 }
1648
1649 if (open_type == OPEN_TYPE_ACTIVE || open_type == OPEN_TYPE_LISTEN)
1650 free(pkt, M_CXGBE);
1651
1652 return (matched ? &r->settings : &disallow_offloading_settings);
1653 }
1654
1655 static void
reclaim_wr_resources(void * arg,int count)1656 reclaim_wr_resources(void *arg, int count)
1657 {
1658 struct tom_data *td = arg;
1659 STAILQ_HEAD(, wrqe) twr_list = STAILQ_HEAD_INITIALIZER(twr_list);
1660 struct cpl_act_open_req *cpl;
1661 u_int opcode, atid, tid;
1662 struct wrqe *wr;
1663 struct adapter *sc = td_adapter(td);
1664
1665 mtx_lock(&td->unsent_wr_lock);
1666 STAILQ_SWAP(&td->unsent_wr_list, &twr_list, wrqe);
1667 mtx_unlock(&td->unsent_wr_lock);
1668
1669 while ((wr = STAILQ_FIRST(&twr_list)) != NULL) {
1670 STAILQ_REMOVE_HEAD(&twr_list, link);
1671
1672 cpl = wrtod(wr);
1673 opcode = GET_OPCODE(cpl);
1674
1675 switch (opcode) {
1676 case CPL_ACT_OPEN_REQ:
1677 case CPL_ACT_OPEN_REQ6:
1678 atid = G_TID_TID(be32toh(OPCODE_TID(cpl)));
1679 CTR2(KTR_CXGBE, "%s: atid %u ", __func__, atid);
1680 act_open_failure_cleanup(sc, atid, EHOSTUNREACH);
1681 free(wr, M_CXGBE);
1682 break;
1683 case CPL_PASS_ACCEPT_RPL:
1684 tid = GET_TID(cpl);
1685 CTR2(KTR_CXGBE, "%s: tid %u ", __func__, tid);
1686 synack_failure_cleanup(sc, tid);
1687 free(wr, M_CXGBE);
1688 break;
1689 default:
1690 log(LOG_ERR, "%s: leaked work request %p, wr_len %d, "
1691 "opcode %x\n", __func__, wr, wr->wr_len, opcode);
1692 /* WR not freed here; go look at it with a debugger. */
1693 }
1694 }
1695 }
1696
1697 /*
1698 * Ground control to Major TOM
1699 * Commencing countdown, engines on
1700 */
1701 static int
t4_tom_activate(struct adapter * sc)1702 t4_tom_activate(struct adapter *sc)
1703 {
1704 struct tom_data *td;
1705 struct toedev *tod;
1706 struct vi_info *vi;
1707 int i, rc, v;
1708
1709 ASSERT_SYNCHRONIZED_OP(sc);
1710
1711 /* per-adapter softc for TOM */
1712 td = malloc(sizeof(*td), M_CXGBE, M_ZERO | M_NOWAIT);
1713 if (td == NULL)
1714 return (ENOMEM);
1715
1716 /* List of TOE PCBs and associated lock */
1717 mtx_init(&td->toep_list_lock, "PCB list lock", NULL, MTX_DEF);
1718 TAILQ_INIT(&td->toep_list);
1719
1720 /* Listen context */
1721 mtx_init(&td->lctx_hash_lock, "lctx hash lock", NULL, MTX_DEF);
1722 td->listen_hash = hashinit_flags(LISTEN_HASH_SIZE, M_CXGBE,
1723 &td->listen_mask, HASH_NOWAIT);
1724
1725 /* List of WRs for which L2 resolution failed */
1726 mtx_init(&td->unsent_wr_lock, "Unsent WR list lock", NULL, MTX_DEF);
1727 STAILQ_INIT(&td->unsent_wr_list);
1728 TASK_INIT(&td->reclaim_wr_resources, 0, reclaim_wr_resources, td);
1729
1730 /* TID tables */
1731 rc = alloc_tid_tabs(&sc->tids);
1732 if (rc != 0)
1733 goto done;
1734
1735 rc = t4_init_ppod_region(&td->pr, &sc->vres.ddp,
1736 t4_read_reg(sc, A_ULP_RX_TDDP_PSZ), "TDDP page pods");
1737 if (rc != 0)
1738 goto done;
1739 t4_set_reg_field(sc, A_ULP_RX_TDDP_TAGMASK,
1740 V_TDDPTAGMASK(M_TDDPTAGMASK), td->pr.pr_tag_mask);
1741
1742 alloc_tcb_history(sc, td);
1743
1744 /* toedev ops */
1745 tod = &td->tod;
1746 init_toedev(tod);
1747 tod->tod_softc = sc;
1748 tod->tod_connect = t4_connect;
1749 tod->tod_listen_start = t4_listen_start;
1750 tod->tod_listen_stop = t4_listen_stop;
1751 tod->tod_rcvd = t4_rcvd;
1752 tod->tod_output = t4_tod_output;
1753 tod->tod_send_rst = t4_send_rst;
1754 tod->tod_send_fin = t4_send_fin;
1755 tod->tod_pcb_detach = t4_pcb_detach;
1756 tod->tod_l2_update = t4_l2_update;
1757 tod->tod_syncache_added = t4_syncache_added;
1758 tod->tod_syncache_removed = t4_syncache_removed;
1759 tod->tod_syncache_respond = t4_syncache_respond;
1760 tod->tod_offload_socket = t4_offload_socket;
1761 tod->tod_ctloutput = t4_ctloutput;
1762 tod->tod_tcp_info = t4_tcp_info;
1763 #ifdef KERN_TLS
1764 tod->tod_alloc_tls_session = t4_alloc_tls_session;
1765 #endif
1766
1767 for_each_port(sc, i) {
1768 for_each_vi(sc->port[i], v, vi) {
1769 TOEDEV(vi->ifp) = &td->tod;
1770 }
1771 }
1772
1773 sc->tom_softc = td;
1774 register_toedev(sc->tom_softc);
1775
1776 done:
1777 if (rc != 0)
1778 free_tom_data(sc, td);
1779 return (rc);
1780 }
1781
1782 static int
t4_tom_deactivate(struct adapter * sc)1783 t4_tom_deactivate(struct adapter *sc)
1784 {
1785 int rc = 0;
1786 struct tom_data *td = sc->tom_softc;
1787
1788 ASSERT_SYNCHRONIZED_OP(sc);
1789
1790 if (td == NULL)
1791 return (0); /* XXX. KASSERT? */
1792
1793 if (sc->offload_map != 0)
1794 return (EBUSY); /* at least one port has IFCAP_TOE enabled */
1795
1796 if (uld_active(sc, ULD_IWARP) || uld_active(sc, ULD_ISCSI))
1797 return (EBUSY); /* both iWARP and iSCSI rely on the TOE. */
1798
1799 mtx_lock(&td->toep_list_lock);
1800 if (!TAILQ_EMPTY(&td->toep_list))
1801 rc = EBUSY;
1802 mtx_unlock(&td->toep_list_lock);
1803
1804 mtx_lock(&td->lctx_hash_lock);
1805 if (td->lctx_count > 0)
1806 rc = EBUSY;
1807 mtx_unlock(&td->lctx_hash_lock);
1808
1809 taskqueue_drain(taskqueue_thread, &td->reclaim_wr_resources);
1810 mtx_lock(&td->unsent_wr_lock);
1811 if (!STAILQ_EMPTY(&td->unsent_wr_list))
1812 rc = EBUSY;
1813 mtx_unlock(&td->unsent_wr_lock);
1814
1815 if (rc == 0) {
1816 unregister_toedev(sc->tom_softc);
1817 free_tom_data(sc, td);
1818 sc->tom_softc = NULL;
1819 }
1820
1821 return (rc);
1822 }
1823
1824 static int
t4_aio_queue_tom(struct socket * so,struct kaiocb * job)1825 t4_aio_queue_tom(struct socket *so, struct kaiocb *job)
1826 {
1827 struct tcpcb *tp = so_sototcpcb(so);
1828 struct toepcb *toep = tp->t_toe;
1829 int error;
1830
1831 if (ulp_mode(toep) == ULP_MODE_TCPDDP) {
1832 error = t4_aio_queue_ddp(so, job);
1833 if (error != EOPNOTSUPP)
1834 return (error);
1835 }
1836
1837 return (t4_aio_queue_aiotx(so, job));
1838 }
1839
1840 static int
t4_ctloutput_tom(struct socket * so,struct sockopt * sopt)1841 t4_ctloutput_tom(struct socket *so, struct sockopt *sopt)
1842 {
1843
1844 if (sopt->sopt_level != IPPROTO_TCP)
1845 return (tcp_ctloutput(so, sopt));
1846
1847 switch (sopt->sopt_name) {
1848 case TCP_TLSOM_SET_TLS_CONTEXT:
1849 case TCP_TLSOM_GET_TLS_TOM:
1850 case TCP_TLSOM_CLR_TLS_TOM:
1851 case TCP_TLSOM_CLR_QUIES:
1852 return (t4_ctloutput_tls(so, sopt));
1853 default:
1854 return (tcp_ctloutput(so, sopt));
1855 }
1856 }
1857
1858 static int
t4_tom_mod_load(void)1859 t4_tom_mod_load(void)
1860 {
1861 /* CPL handlers */
1862 t4_register_cpl_handler(CPL_GET_TCB_RPL, do_get_tcb_rpl);
1863 t4_register_shared_cpl_handler(CPL_L2T_WRITE_RPL, do_l2t_write_rpl2,
1864 CPL_COOKIE_TOM);
1865 t4_init_connect_cpl_handlers();
1866 t4_init_listen_cpl_handlers();
1867 t4_init_cpl_io_handlers();
1868
1869 t4_ddp_mod_load();
1870 t4_tls_mod_load();
1871
1872 tcp_protosw = pffindproto(PF_INET, IPPROTO_TCP, SOCK_STREAM);
1873 if (tcp_protosw == NULL)
1874 return (ENOPROTOOPT);
1875 bcopy(tcp_protosw, &toe_protosw, sizeof(toe_protosw));
1876 bcopy(tcp_protosw->pr_usrreqs, &toe_usrreqs, sizeof(toe_usrreqs));
1877 toe_usrreqs.pru_aio_queue = t4_aio_queue_tom;
1878 toe_protosw.pr_ctloutput = t4_ctloutput_tom;
1879 toe_protosw.pr_usrreqs = &toe_usrreqs;
1880
1881 tcp6_protosw = pffindproto(PF_INET6, IPPROTO_TCP, SOCK_STREAM);
1882 if (tcp6_protosw == NULL)
1883 return (ENOPROTOOPT);
1884 bcopy(tcp6_protosw, &toe6_protosw, sizeof(toe6_protosw));
1885 bcopy(tcp6_protosw->pr_usrreqs, &toe6_usrreqs, sizeof(toe6_usrreqs));
1886 toe6_usrreqs.pru_aio_queue = t4_aio_queue_tom;
1887 toe6_protosw.pr_ctloutput = t4_ctloutput_tom;
1888 toe6_protosw.pr_usrreqs = &toe6_usrreqs;
1889
1890 return (t4_register_uld(&tom_uld_info));
1891 }
1892
1893 static void
tom_uninit(struct adapter * sc,void * arg __unused)1894 tom_uninit(struct adapter *sc, void *arg __unused)
1895 {
1896 if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4tomun"))
1897 return;
1898
1899 /* Try to free resources (works only if no port has IFCAP_TOE) */
1900 if (uld_active(sc, ULD_TOM))
1901 t4_deactivate_uld(sc, ULD_TOM);
1902
1903 end_synchronized_op(sc, 0);
1904 }
1905
1906 static int
t4_tom_mod_unload(void)1907 t4_tom_mod_unload(void)
1908 {
1909 t4_iterate(tom_uninit, NULL);
1910
1911 if (t4_unregister_uld(&tom_uld_info) == EBUSY)
1912 return (EBUSY);
1913
1914 t4_tls_mod_unload();
1915 t4_ddp_mod_unload();
1916
1917 t4_uninit_connect_cpl_handlers();
1918 t4_uninit_listen_cpl_handlers();
1919 t4_uninit_cpl_io_handlers();
1920 t4_register_shared_cpl_handler(CPL_L2T_WRITE_RPL, NULL, CPL_COOKIE_TOM);
1921 t4_register_cpl_handler(CPL_GET_TCB_RPL, NULL);
1922
1923 return (0);
1924 }
1925 #endif /* TCP_OFFLOAD */
1926
1927 static int
t4_tom_modevent(module_t mod,int cmd,void * arg)1928 t4_tom_modevent(module_t mod, int cmd, void *arg)
1929 {
1930 int rc = 0;
1931
1932 #ifdef TCP_OFFLOAD
1933 switch (cmd) {
1934 case MOD_LOAD:
1935 rc = t4_tom_mod_load();
1936 break;
1937
1938 case MOD_UNLOAD:
1939 rc = t4_tom_mod_unload();
1940 break;
1941
1942 default:
1943 rc = EINVAL;
1944 }
1945 #else
1946 printf("t4_tom: compiled without TCP_OFFLOAD support.\n");
1947 rc = EOPNOTSUPP;
1948 #endif
1949 return (rc);
1950 }
1951
1952 static moduledata_t t4_tom_moddata= {
1953 "t4_tom",
1954 t4_tom_modevent,
1955 0
1956 };
1957
1958 MODULE_VERSION(t4_tom, 1);
1959 MODULE_DEPEND(t4_tom, toecore, 1, 1, 1);
1960 MODULE_DEPEND(t4_tom, t4nex, 1, 1, 1);
1961 DECLARE_MODULE(t4_tom, t4_tom_moddata, SI_SUB_EXEC, SI_ORDER_ANY);
1962