]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/cxgbe/tom/t4_tom.c
MFC r317849 (partial), r332506, and r332787.
[FreeBSD/FreeBSD.git] / sys / dev / cxgbe / tom / t4_tom.c
1 /*-
2  * Copyright (c) 2012 Chelsio Communications, Inc.
3  * All rights reserved.
4  * Written by: Navdeep Parhar <np@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include "opt_inet.h"
32 #include "opt_inet6.h"
33
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/ktr.h>
39 #include <sys/lock.h>
40 #include <sys/limits.h>
41 #include <sys/module.h>
42 #include <sys/protosw.h>
43 #include <sys/domain.h>
44 #include <sys/refcount.h>
45 #include <sys/rmlock.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/taskqueue.h>
49 #include <net/if.h>
50 #include <net/if_var.h>
51 #include <net/if_types.h>
52 #include <net/if_vlan_var.h>
53 #include <netinet/in.h>
54 #include <netinet/in_pcb.h>
55 #include <netinet/in_var.h>
56 #include <netinet/ip.h>
57 #include <netinet/ip6.h>
58 #include <netinet6/scope6_var.h>
59 #define TCPSTATES
60 #include <netinet/tcp_fsm.h>
61 #include <netinet/tcp_timer.h>
62 #include <netinet/tcp_var.h>
63 #include <netinet/toecore.h>
64
65 #ifdef TCP_OFFLOAD
66 #include "common/common.h"
67 #include "common/t4_msg.h"
68 #include "common/t4_regs.h"
69 #include "common/t4_regs_values.h"
70 #include "common/t4_tcb.h"
71 #include "tom/t4_tom_l2t.h"
72 #include "tom/t4_tom.h"
73 #include "tom/t4_tls.h"
74
75 static struct protosw toe_protosw;
76 static struct pr_usrreqs toe_usrreqs;
77
78 static struct protosw toe6_protosw;
79 static struct pr_usrreqs toe6_usrreqs;
80
81 /* Module ops */
82 static int t4_tom_mod_load(void);
83 static int t4_tom_mod_unload(void);
84 static int t4_tom_modevent(module_t, int, void *);
85
86 /* ULD ops and helpers */
87 static int t4_tom_activate(struct adapter *);
88 static int t4_tom_deactivate(struct adapter *);
89
90 static struct uld_info tom_uld_info = {
91         .uld_id = ULD_TOM,
92         .activate = t4_tom_activate,
93         .deactivate = t4_tom_deactivate,
94 };
95
96 static void queue_tid_release(struct adapter *, int);
97 static void release_offload_resources(struct toepcb *);
98 static int alloc_tid_tabs(struct tid_info *);
99 static void free_tid_tabs(struct tid_info *);
100 static int add_lip(struct adapter *, struct in6_addr *);
101 static int delete_lip(struct adapter *, struct in6_addr *);
102 static struct clip_entry *search_lip(struct tom_data *, struct in6_addr *);
103 static void init_clip_table(struct adapter *, struct tom_data *);
104 static void update_clip(struct adapter *, void *);
105 static void t4_clip_task(void *, int);
106 static void update_clip_table(struct adapter *, struct tom_data *);
107 static void destroy_clip_table(struct adapter *, struct tom_data *);
108 static void free_tom_data(struct adapter *, struct tom_data *);
109 static void reclaim_wr_resources(void *, int);
110
111 static int in6_ifaddr_gen;
112 static eventhandler_tag ifaddr_evhandler;
113 static struct timeout_task clip_task;
114
115 struct toepcb *
116 alloc_toepcb(struct vi_info *vi, int txqid, int rxqid, int flags)
117 {
118         struct port_info *pi = vi->pi;
119         struct adapter *sc = pi->adapter;
120         struct toepcb *toep;
121         int tx_credits, txsd_total, len;
122
123         /*
124          * The firmware counts tx work request credits in units of 16 bytes
125          * each.  Reserve room for an ABORT_REQ so the driver never has to worry
126          * about tx credits if it wants to abort a connection.
127          */
128         tx_credits = sc->params.ofldq_wr_cred;
129         tx_credits -= howmany(sizeof(struct cpl_abort_req), 16);
130
131         /*
132          * Shortest possible tx work request is a fw_ofld_tx_data_wr + 1 byte
133          * immediate payload, and firmware counts tx work request credits in
134          * units of 16 byte.  Calculate the maximum work requests possible.
135          */
136         txsd_total = tx_credits /
137             howmany(sizeof(struct fw_ofld_tx_data_wr) + 1, 16);
138
139         KASSERT(txqid >= vi->first_ofld_txq &&
140             txqid < vi->first_ofld_txq + vi->nofldtxq,
141             ("%s: txqid %d for vi %p (first %d, n %d)", __func__, txqid, vi,
142                 vi->first_ofld_txq, vi->nofldtxq));
143
144         KASSERT(rxqid >= vi->first_ofld_rxq &&
145             rxqid < vi->first_ofld_rxq + vi->nofldrxq,
146             ("%s: rxqid %d for vi %p (first %d, n %d)", __func__, rxqid, vi,
147                 vi->first_ofld_rxq, vi->nofldrxq));
148
149         len = offsetof(struct toepcb, txsd) +
150             txsd_total * sizeof(struct ofld_tx_sdesc);
151
152         toep = malloc(len, M_CXGBE, M_ZERO | flags);
153         if (toep == NULL)
154                 return (NULL);
155
156         refcount_init(&toep->refcount, 1);
157         toep->td = sc->tom_softc;
158         toep->vi = vi;
159         toep->tc_idx = -1;
160         toep->tx_total = tx_credits;
161         toep->tx_credits = tx_credits;
162         toep->ofld_txq = &sc->sge.ofld_txq[txqid];
163         toep->ofld_rxq = &sc->sge.ofld_rxq[rxqid];
164         toep->ctrlq = &sc->sge.ctrlq[pi->port_id];
165         mbufq_init(&toep->ulp_pduq, INT_MAX);
166         mbufq_init(&toep->ulp_pdu_reclaimq, INT_MAX);
167         toep->txsd_total = txsd_total;
168         toep->txsd_avail = txsd_total;
169         toep->txsd_pidx = 0;
170         toep->txsd_cidx = 0;
171         aiotx_init_toep(toep);
172
173         return (toep);
174 }
175
176 struct toepcb *
177 hold_toepcb(struct toepcb *toep)
178 {
179
180         refcount_acquire(&toep->refcount);
181         return (toep);
182 }
183
184 void
185 free_toepcb(struct toepcb *toep)
186 {
187
188         if (refcount_release(&toep->refcount) == 0)
189                 return;
190
191         KASSERT(!(toep->flags & TPF_ATTACHED),
192             ("%s: attached to an inpcb", __func__));
193         KASSERT(!(toep->flags & TPF_CPL_PENDING),
194             ("%s: CPL pending", __func__));
195
196         if (toep->ulp_mode == ULP_MODE_TCPDDP)
197                 ddp_uninit_toep(toep);
198         tls_uninit_toep(toep);
199         free(toep, M_CXGBE);
200 }
201
202 /*
203  * Set up the socket for TCP offload.
204  */
205 void
206 offload_socket(struct socket *so, struct toepcb *toep)
207 {
208         struct tom_data *td = toep->td;
209         struct inpcb *inp = sotoinpcb(so);
210         struct tcpcb *tp = intotcpcb(inp);
211         struct sockbuf *sb;
212
213         INP_WLOCK_ASSERT(inp);
214
215         /* Update socket */
216         sb = &so->so_snd;
217         SOCKBUF_LOCK(sb);
218         sb->sb_flags |= SB_NOCOALESCE;
219         SOCKBUF_UNLOCK(sb);
220         sb = &so->so_rcv;
221         SOCKBUF_LOCK(sb);
222         sb->sb_flags |= SB_NOCOALESCE;
223         if (inp->inp_vflag & INP_IPV6)
224                 so->so_proto = &toe6_protosw;
225         else
226                 so->so_proto = &toe_protosw;
227         SOCKBUF_UNLOCK(sb);
228
229         /* Update TCP PCB */
230         tp->tod = &td->tod;
231         tp->t_toe = toep;
232         tp->t_flags |= TF_TOE;
233
234         /* Install an extra hold on inp */
235         toep->inp = inp;
236         toep->flags |= TPF_ATTACHED;
237         in_pcbref(inp);
238
239         /* Add the TOE PCB to the active list */
240         mtx_lock(&td->toep_list_lock);
241         TAILQ_INSERT_HEAD(&td->toep_list, toep, link);
242         mtx_unlock(&td->toep_list_lock);
243 }
244
245 /* This is _not_ the normal way to "unoffload" a socket. */
246 void
247 undo_offload_socket(struct socket *so)
248 {
249         struct inpcb *inp = sotoinpcb(so);
250         struct tcpcb *tp = intotcpcb(inp);
251         struct toepcb *toep = tp->t_toe;
252         struct tom_data *td = toep->td;
253         struct sockbuf *sb;
254
255         INP_WLOCK_ASSERT(inp);
256
257         sb = &so->so_snd;
258         SOCKBUF_LOCK(sb);
259         sb->sb_flags &= ~SB_NOCOALESCE;
260         SOCKBUF_UNLOCK(sb);
261         sb = &so->so_rcv;
262         SOCKBUF_LOCK(sb);
263         sb->sb_flags &= ~SB_NOCOALESCE;
264         SOCKBUF_UNLOCK(sb);
265
266         tp->tod = NULL;
267         tp->t_toe = NULL;
268         tp->t_flags &= ~TF_TOE;
269
270         toep->inp = NULL;
271         toep->flags &= ~TPF_ATTACHED;
272         if (in_pcbrele_wlocked(inp))
273                 panic("%s: inp freed.", __func__);
274
275         mtx_lock(&td->toep_list_lock);
276         TAILQ_REMOVE(&td->toep_list, toep, link);
277         mtx_unlock(&td->toep_list_lock);
278 }
279
280 static void
281 release_offload_resources(struct toepcb *toep)
282 {
283         struct tom_data *td = toep->td;
284         struct adapter *sc = td_adapter(td);
285         int tid = toep->tid;
286
287         KASSERT(!(toep->flags & TPF_CPL_PENDING),
288             ("%s: %p has CPL pending.", __func__, toep));
289         KASSERT(!(toep->flags & TPF_ATTACHED),
290             ("%s: %p is still attached.", __func__, toep));
291
292         CTR5(KTR_CXGBE, "%s: toep %p (tid %d, l2te %p, ce %p)",
293             __func__, toep, tid, toep->l2te, toep->ce);
294
295         /*
296          * These queues should have been emptied at approximately the same time
297          * that a normal connection's socket's so_snd would have been purged or
298          * drained.  Do _not_ clean up here.
299          */
300         MPASS(mbufq_len(&toep->ulp_pduq) == 0);
301         MPASS(mbufq_len(&toep->ulp_pdu_reclaimq) == 0);
302 #ifdef INVARIANTS
303         if (toep->ulp_mode == ULP_MODE_TCPDDP)
304                 ddp_assert_empty(toep);
305 #endif
306
307         if (toep->l2te)
308                 t4_l2t_release(toep->l2te);
309
310         if (tid >= 0) {
311                 remove_tid(sc, tid, toep->ce ? 2 : 1);
312                 release_tid(sc, tid, toep->ctrlq);
313         }
314
315         if (toep->ce)
316                 release_lip(td, toep->ce);
317
318 #ifdef RATELIMIT
319         if (toep->tc_idx != -1)
320                 t4_release_cl_rl_kbps(sc, toep->vi->pi->port_id, toep->tc_idx);
321 #endif
322         mtx_lock(&td->toep_list_lock);
323         TAILQ_REMOVE(&td->toep_list, toep, link);
324         mtx_unlock(&td->toep_list_lock);
325
326         free_toepcb(toep);
327 }
328
329 /*
330  * The kernel is done with the TCP PCB and this is our opportunity to unhook the
331  * toepcb hanging off of it.  If the TOE driver is also done with the toepcb (no
332  * pending CPL) then it is time to release all resources tied to the toepcb.
333  *
334  * Also gets called when an offloaded active open fails and the TOM wants the
335  * kernel to take the TCP PCB back.
336  */
337 static void
338 t4_pcb_detach(struct toedev *tod __unused, struct tcpcb *tp)
339 {
340 #if defined(KTR) || defined(INVARIANTS)
341         struct inpcb *inp = tp->t_inpcb;
342 #endif
343         struct toepcb *toep = tp->t_toe;
344
345         INP_WLOCK_ASSERT(inp);
346
347         KASSERT(toep != NULL, ("%s: toep is NULL", __func__));
348         KASSERT(toep->flags & TPF_ATTACHED,
349             ("%s: not attached", __func__));
350
351 #ifdef KTR
352         if (tp->t_state == TCPS_SYN_SENT) {
353                 CTR6(KTR_CXGBE, "%s: atid %d, toep %p (0x%x), inp %p (0x%x)",
354                     __func__, toep->tid, toep, toep->flags, inp,
355                     inp->inp_flags);
356         } else {
357                 CTR6(KTR_CXGBE,
358                     "t4_pcb_detach: tid %d (%s), toep %p (0x%x), inp %p (0x%x)",
359                     toep->tid, tcpstates[tp->t_state], toep, toep->flags, inp,
360                     inp->inp_flags);
361         }
362 #endif
363
364         tp->t_toe = NULL;
365         tp->t_flags &= ~TF_TOE;
366         toep->flags &= ~TPF_ATTACHED;
367
368         if (!(toep->flags & TPF_CPL_PENDING))
369                 release_offload_resources(toep);
370 }
371
372 /*
373  * setsockopt handler.
374  */
375 static void
376 t4_ctloutput(struct toedev *tod, struct tcpcb *tp, int dir, int name)
377 {
378         struct adapter *sc = tod->tod_softc;
379         struct toepcb *toep = tp->t_toe;
380
381         if (dir == SOPT_GET)
382                 return;
383
384         CTR4(KTR_CXGBE, "%s: tp %p, dir %u, name %u", __func__, tp, dir, name);
385
386         switch (name) {
387         case TCP_NODELAY:
388                 if (tp->t_state != TCPS_ESTABLISHED)
389                         break;
390                 t4_set_tcb_field(sc, toep->ctrlq, toep, W_TCB_T_FLAGS,
391                     V_TF_NAGLE(1), V_TF_NAGLE(tp->t_flags & TF_NODELAY ? 0 : 1),
392                     0, 0);
393                 break;
394         default:
395                 break;
396         }
397 }
398
399 /*
400  * The TOE driver will not receive any more CPLs for the tid associated with the
401  * toepcb; release the hold on the inpcb.
402  */
403 void
404 final_cpl_received(struct toepcb *toep)
405 {
406         struct inpcb *inp = toep->inp;
407
408         KASSERT(inp != NULL, ("%s: inp is NULL", __func__));
409         INP_WLOCK_ASSERT(inp);
410         KASSERT(toep->flags & TPF_CPL_PENDING,
411             ("%s: CPL not pending already?", __func__));
412
413         CTR6(KTR_CXGBE, "%s: tid %d, toep %p (0x%x), inp %p (0x%x)",
414             __func__, toep->tid, toep, toep->flags, inp, inp->inp_flags);
415
416         if (toep->ulp_mode == ULP_MODE_TCPDDP)
417                 release_ddp_resources(toep);
418         toep->inp = NULL;
419         toep->flags &= ~TPF_CPL_PENDING;
420         mbufq_drain(&toep->ulp_pdu_reclaimq);
421
422         if (!(toep->flags & TPF_ATTACHED))
423                 release_offload_resources(toep);
424
425         if (!in_pcbrele_wlocked(inp))
426                 INP_WUNLOCK(inp);
427 }
428
429 void
430 insert_tid(struct adapter *sc, int tid, void *ctx, int ntids)
431 {
432         struct tid_info *t = &sc->tids;
433
434         t->tid_tab[tid] = ctx;
435         atomic_add_int(&t->tids_in_use, ntids);
436 }
437
438 void *
439 lookup_tid(struct adapter *sc, int tid)
440 {
441         struct tid_info *t = &sc->tids;
442
443         return (t->tid_tab[tid]);
444 }
445
446 void
447 update_tid(struct adapter *sc, int tid, void *ctx)
448 {
449         struct tid_info *t = &sc->tids;
450
451         t->tid_tab[tid] = ctx;
452 }
453
454 void
455 remove_tid(struct adapter *sc, int tid, int ntids)
456 {
457         struct tid_info *t = &sc->tids;
458
459         t->tid_tab[tid] = NULL;
460         atomic_subtract_int(&t->tids_in_use, ntids);
461 }
462
463 void
464 release_tid(struct adapter *sc, int tid, struct sge_wrq *ctrlq)
465 {
466         struct wrqe *wr;
467         struct cpl_tid_release *req;
468
469         wr = alloc_wrqe(sizeof(*req), ctrlq);
470         if (wr == NULL) {
471                 queue_tid_release(sc, tid);     /* defer */
472                 return;
473         }
474         req = wrtod(wr);
475
476         INIT_TP_WR_MIT_CPL(req, CPL_TID_RELEASE, tid);
477
478         t4_wrq_tx(sc, wr);
479 }
480
481 static void
482 queue_tid_release(struct adapter *sc, int tid)
483 {
484
485         CXGBE_UNIMPLEMENTED("deferred tid release");
486 }
487
488 /*
489  * What mtu_idx to use, given a 4-tuple.  Note that both s->mss and tcp_mssopt
490  * have the MSS that we should advertise in our SYN.  Advertised MSS doesn't
491  * account for any TCP options so the effective MSS (only payload, no headers or
492  * options) could be different.  We fill up tp->t_maxseg with the effective MSS
493  * at the end of the 3-way handshake.
494  */
495 int
496 find_best_mtu_idx(struct adapter *sc, struct in_conninfo *inc,
497     struct offload_settings *s)
498 {
499         unsigned short *mtus = &sc->params.mtus[0];
500         int i, mss, mtu;
501
502         MPASS(inc != NULL);
503
504         mss = s->mss > 0 ? s->mss : tcp_mssopt(inc);
505         if (inc->inc_flags & INC_ISIPV6)
506                 mtu = mss + sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
507         else
508                 mtu = mss + sizeof(struct ip) + sizeof(struct tcphdr);
509
510         for (i = 0; i < NMTUS - 1 && mtus[i + 1] <= mtu; i++)
511                 continue;
512
513         return (i);
514 }
515
516 /*
517  * Determine the receive window size for a socket.
518  */
519 u_long
520 select_rcv_wnd(struct socket *so)
521 {
522         unsigned long wnd;
523
524         SOCKBUF_LOCK_ASSERT(&so->so_rcv);
525
526         wnd = sbspace(&so->so_rcv);
527         if (wnd < MIN_RCV_WND)
528                 wnd = MIN_RCV_WND;
529
530         return min(wnd, MAX_RCV_WND);
531 }
532
533 int
534 select_rcv_wscale(void)
535 {
536         int wscale = 0;
537         unsigned long space = sb_max;
538
539         if (space > MAX_RCV_WND)
540                 space = MAX_RCV_WND;
541
542         while (wscale < TCP_MAX_WINSHIFT && (TCP_MAXWIN << wscale) < space)
543                 wscale++;
544
545         return (wscale);
546 }
547
548 /*
549  * socket so could be a listening socket too.
550  */
551 uint64_t
552 calc_opt0(struct socket *so, struct vi_info *vi, struct l2t_entry *e,
553     int mtu_idx, int rscale, int rx_credits, int ulp_mode,
554     struct offload_settings *s)
555 {
556         int keepalive;
557         uint64_t opt0;
558
559         MPASS(so != NULL);
560         MPASS(vi != NULL);
561         KASSERT(rx_credits <= M_RCV_BUFSIZ,
562             ("%s: rcv_bufsiz too high", __func__));
563
564         opt0 = F_TCAM_BYPASS | V_WND_SCALE(rscale) | V_MSS_IDX(mtu_idx) |
565             V_ULP_MODE(ulp_mode) | V_RCV_BUFSIZ(rx_credits) |
566             V_L2T_IDX(e->idx) | V_SMAC_SEL(vi->smt_idx) |
567             V_TX_CHAN(vi->pi->tx_chan);
568
569         keepalive = tcp_always_keepalive || so_options_get(so) & SO_KEEPALIVE;
570         opt0 |= V_KEEP_ALIVE(keepalive != 0);
571
572         if (s->nagle < 0) {
573                 struct inpcb *inp = sotoinpcb(so);
574                 struct tcpcb *tp = intotcpcb(inp);
575
576                 opt0 |= V_NAGLE((tp->t_flags & TF_NODELAY) == 0);
577         } else
578                 opt0 |= V_NAGLE(s->nagle != 0);
579
580         return htobe64(opt0);
581 }
582
583 uint64_t
584 select_ntuple(struct vi_info *vi, struct l2t_entry *e)
585 {
586         struct adapter *sc = vi->pi->adapter;
587         struct tp_params *tp = &sc->params.tp;
588         uint16_t viid = vi->viid;
589         uint64_t ntuple = 0;
590
591         /*
592          * Initialize each of the fields which we care about which are present
593          * in the Compressed Filter Tuple.
594          */
595         if (tp->vlan_shift >= 0 && e->vlan != CPL_L2T_VLAN_NONE)
596                 ntuple |= (uint64_t)(F_FT_VLAN_VLD | e->vlan) << tp->vlan_shift;
597
598         if (tp->port_shift >= 0)
599                 ntuple |= (uint64_t)e->lport << tp->port_shift;
600
601         if (tp->protocol_shift >= 0)
602                 ntuple |= (uint64_t)IPPROTO_TCP << tp->protocol_shift;
603
604         if (tp->vnic_shift >= 0) {
605                 uint32_t vf = G_FW_VIID_VIN(viid);
606                 uint32_t pf = G_FW_VIID_PFN(viid);
607                 uint32_t vld = G_FW_VIID_VIVLD(viid);
608
609                 ntuple |= (uint64_t)(V_FT_VNID_ID_VF(vf) | V_FT_VNID_ID_PF(pf) |
610                     V_FT_VNID_ID_VLD(vld)) << tp->vnic_shift;
611         }
612
613         if (is_t4(sc))
614                 return (htobe32((uint32_t)ntuple));
615         else
616                 return (htobe64(V_FILTER_TUPLE(ntuple)));
617 }
618
619 static int
620 is_tls_sock(struct socket *so, struct adapter *sc)
621 {
622         struct inpcb *inp = sotoinpcb(so);
623         int i, rc;
624
625         /* XXX: Eventually add a SO_WANT_TLS socket option perhaps? */
626         rc = 0;
627         ADAPTER_LOCK(sc);
628         for (i = 0; i < sc->tt.num_tls_rx_ports; i++) {
629                 if (inp->inp_lport == htons(sc->tt.tls_rx_ports[i]) ||
630                     inp->inp_fport == htons(sc->tt.tls_rx_ports[i])) {
631                         rc = 1;
632                         break;
633                 }
634         }
635         ADAPTER_UNLOCK(sc);
636         return (rc);
637 }
638
639 int
640 select_ulp_mode(struct socket *so, struct adapter *sc,
641     struct offload_settings *s)
642 {
643
644         if (can_tls_offload(sc) &&
645             (s->tls > 0 || (s->tls < 0 && is_tls_sock(so, sc))))
646                 return (ULP_MODE_TLS);
647         else if (s->ddp > 0 ||
648             (s->ddp < 0 && sc->tt.ddp && (so->so_options & SO_NO_DDP) == 0))
649                 return (ULP_MODE_TCPDDP);
650         else
651                 return (ULP_MODE_NONE);
652 }
653
654 void
655 set_ulp_mode(struct toepcb *toep, int ulp_mode)
656 {
657
658         CTR4(KTR_CXGBE, "%s: toep %p (tid %d) ulp_mode %d",
659             __func__, toep, toep->tid, ulp_mode);
660         toep->ulp_mode = ulp_mode;
661         tls_init_toep(toep);
662         if (toep->ulp_mode == ULP_MODE_TCPDDP)
663                 ddp_init_toep(toep);
664 }
665
666 int
667 negative_advice(int status)
668 {
669
670         return (status == CPL_ERR_RTX_NEG_ADVICE ||
671             status == CPL_ERR_PERSIST_NEG_ADVICE ||
672             status == CPL_ERR_KEEPALV_NEG_ADVICE);
673 }
674
675 static int
676 alloc_tid_tabs(struct tid_info *t)
677 {
678         size_t size;
679         unsigned int i;
680
681         size = t->ntids * sizeof(*t->tid_tab) +
682             t->natids * sizeof(*t->atid_tab) +
683             t->nstids * sizeof(*t->stid_tab);
684
685         t->tid_tab = malloc(size, M_CXGBE, M_ZERO | M_NOWAIT);
686         if (t->tid_tab == NULL)
687                 return (ENOMEM);
688
689         mtx_init(&t->atid_lock, "atid lock", NULL, MTX_DEF);
690         t->atid_tab = (union aopen_entry *)&t->tid_tab[t->ntids];
691         t->afree = t->atid_tab;
692         t->atids_in_use = 0;
693         for (i = 1; i < t->natids; i++)
694                 t->atid_tab[i - 1].next = &t->atid_tab[i];
695         t->atid_tab[t->natids - 1].next = NULL;
696
697         mtx_init(&t->stid_lock, "stid lock", NULL, MTX_DEF);
698         t->stid_tab = (struct listen_ctx **)&t->atid_tab[t->natids];
699         t->stids_in_use = 0;
700         TAILQ_INIT(&t->stids);
701         t->nstids_free_head = t->nstids;
702
703         atomic_store_rel_int(&t->tids_in_use, 0);
704
705         return (0);
706 }
707
708 static void
709 free_tid_tabs(struct tid_info *t)
710 {
711         KASSERT(t->tids_in_use == 0,
712             ("%s: %d tids still in use.", __func__, t->tids_in_use));
713         KASSERT(t->atids_in_use == 0,
714             ("%s: %d atids still in use.", __func__, t->atids_in_use));
715         KASSERT(t->stids_in_use == 0,
716             ("%s: %d tids still in use.", __func__, t->stids_in_use));
717
718         free(t->tid_tab, M_CXGBE);
719         t->tid_tab = NULL;
720
721         if (mtx_initialized(&t->atid_lock))
722                 mtx_destroy(&t->atid_lock);
723         if (mtx_initialized(&t->stid_lock))
724                 mtx_destroy(&t->stid_lock);
725 }
726
727 static int
728 add_lip(struct adapter *sc, struct in6_addr *lip)
729 {
730         struct fw_clip_cmd c;
731
732         ASSERT_SYNCHRONIZED_OP(sc);
733         /* mtx_assert(&td->clip_table_lock, MA_OWNED); */
734
735         memset(&c, 0, sizeof(c));
736         c.op_to_write = htonl(V_FW_CMD_OP(FW_CLIP_CMD) | F_FW_CMD_REQUEST |
737             F_FW_CMD_WRITE);
738         c.alloc_to_len16 = htonl(F_FW_CLIP_CMD_ALLOC | FW_LEN16(c));
739         c.ip_hi = *(uint64_t *)&lip->s6_addr[0];
740         c.ip_lo = *(uint64_t *)&lip->s6_addr[8];
741
742         return (-t4_wr_mbox_ns(sc, sc->mbox, &c, sizeof(c), &c));
743 }
744
745 static int
746 delete_lip(struct adapter *sc, struct in6_addr *lip)
747 {
748         struct fw_clip_cmd c;
749
750         ASSERT_SYNCHRONIZED_OP(sc);
751         /* mtx_assert(&td->clip_table_lock, MA_OWNED); */
752
753         memset(&c, 0, sizeof(c));
754         c.op_to_write = htonl(V_FW_CMD_OP(FW_CLIP_CMD) | F_FW_CMD_REQUEST |
755             F_FW_CMD_READ);
756         c.alloc_to_len16 = htonl(F_FW_CLIP_CMD_FREE | FW_LEN16(c));
757         c.ip_hi = *(uint64_t *)&lip->s6_addr[0];
758         c.ip_lo = *(uint64_t *)&lip->s6_addr[8];
759
760         return (-t4_wr_mbox_ns(sc, sc->mbox, &c, sizeof(c), &c));
761 }
762
763 static struct clip_entry *
764 search_lip(struct tom_data *td, struct in6_addr *lip)
765 {
766         struct clip_entry *ce;
767
768         mtx_assert(&td->clip_table_lock, MA_OWNED);
769
770         TAILQ_FOREACH(ce, &td->clip_table, link) {
771                 if (IN6_ARE_ADDR_EQUAL(&ce->lip, lip))
772                         return (ce);
773         }
774
775         return (NULL);
776 }
777
778 struct clip_entry *
779 hold_lip(struct tom_data *td, struct in6_addr *lip, struct clip_entry *ce)
780 {
781
782         mtx_lock(&td->clip_table_lock);
783         if (ce == NULL)
784                 ce = search_lip(td, lip);
785         if (ce != NULL)
786                 ce->refcount++;
787         mtx_unlock(&td->clip_table_lock);
788
789         return (ce);
790 }
791
792 void
793 release_lip(struct tom_data *td, struct clip_entry *ce)
794 {
795
796         mtx_lock(&td->clip_table_lock);
797         KASSERT(search_lip(td, &ce->lip) == ce,
798             ("%s: CLIP entry %p p not in CLIP table.", __func__, ce));
799         KASSERT(ce->refcount > 0,
800             ("%s: CLIP entry %p has refcount 0", __func__, ce));
801         --ce->refcount;
802         mtx_unlock(&td->clip_table_lock);
803 }
804
805 static void
806 init_clip_table(struct adapter *sc, struct tom_data *td)
807 {
808
809         ASSERT_SYNCHRONIZED_OP(sc);
810
811         mtx_init(&td->clip_table_lock, "CLIP table lock", NULL, MTX_DEF);
812         TAILQ_INIT(&td->clip_table);
813         td->clip_gen = -1;
814
815         update_clip_table(sc, td);
816 }
817
818 static void
819 update_clip(struct adapter *sc, void *arg __unused)
820 {
821
822         if (begin_synchronized_op(sc, NULL, HOLD_LOCK, "t4tomuc"))
823                 return;
824
825         if (uld_active(sc, ULD_TOM))
826                 update_clip_table(sc, sc->tom_softc);
827
828         end_synchronized_op(sc, LOCK_HELD);
829 }
830
831 static void
832 t4_clip_task(void *arg, int count)
833 {
834
835         t4_iterate(update_clip, NULL);
836 }
837
838 static void
839 update_clip_table(struct adapter *sc, struct tom_data *td)
840 {
841         struct rm_priotracker in6_ifa_tracker;
842         struct in6_ifaddr *ia;
843         struct in6_addr *lip, tlip;
844         struct clip_head stale;
845         struct clip_entry *ce, *ce_temp;
846         struct vi_info *vi;
847         int rc, gen, i, j;
848         uintptr_t last_vnet;
849
850         ASSERT_SYNCHRONIZED_OP(sc);
851
852         IN6_IFADDR_RLOCK(&in6_ifa_tracker);
853         mtx_lock(&td->clip_table_lock);
854
855         gen = atomic_load_acq_int(&in6_ifaddr_gen);
856         if (gen == td->clip_gen)
857                 goto done;
858
859         TAILQ_INIT(&stale);
860         TAILQ_CONCAT(&stale, &td->clip_table, link);
861
862         /*
863          * last_vnet optimizes the common cases where all if_vnet = NULL (no
864          * VIMAGE) or all if_vnet = vnet0.
865          */
866         last_vnet = (uintptr_t)(-1);
867         for_each_port(sc, i)
868         for_each_vi(sc->port[i], j, vi) {
869                 if (last_vnet == (uintptr_t)vi->ifp->if_vnet)
870                         continue;
871
872                 /* XXX: races with if_vmove */
873                 CURVNET_SET(vi->ifp->if_vnet);
874                 TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
875                         lip = &ia->ia_addr.sin6_addr;
876
877                         KASSERT(!IN6_IS_ADDR_MULTICAST(lip),
878                             ("%s: mcast address in in6_ifaddr list", __func__));
879
880                         if (IN6_IS_ADDR_LOOPBACK(lip))
881                                 continue;
882                         if (IN6_IS_SCOPE_EMBED(lip)) {
883                                 /* Remove the embedded scope */
884                                 tlip = *lip;
885                                 lip = &tlip;
886                                 in6_clearscope(lip);
887                         }
888                         /*
889                          * XXX: how to weed out the link local address for the
890                          * loopback interface?  It's fe80::1 usually (always?).
891                          */
892
893                         /*
894                          * If it's in the main list then we already know it's
895                          * not stale.
896                          */
897                         TAILQ_FOREACH(ce, &td->clip_table, link) {
898                                 if (IN6_ARE_ADDR_EQUAL(&ce->lip, lip))
899                                         goto next;
900                         }
901
902                         /*
903                          * If it's in the stale list we should move it to the
904                          * main list.
905                          */
906                         TAILQ_FOREACH(ce, &stale, link) {
907                                 if (IN6_ARE_ADDR_EQUAL(&ce->lip, lip)) {
908                                         TAILQ_REMOVE(&stale, ce, link);
909                                         TAILQ_INSERT_TAIL(&td->clip_table, ce,
910                                             link);
911                                         goto next;
912                                 }
913                         }
914
915                         /* A new IP6 address; add it to the CLIP table */
916                         ce = malloc(sizeof(*ce), M_CXGBE, M_NOWAIT);
917                         memcpy(&ce->lip, lip, sizeof(ce->lip));
918                         ce->refcount = 0;
919                         rc = add_lip(sc, lip);
920                         if (rc == 0)
921                                 TAILQ_INSERT_TAIL(&td->clip_table, ce, link);
922                         else {
923                                 char ip[INET6_ADDRSTRLEN];
924
925                                 inet_ntop(AF_INET6, &ce->lip, &ip[0],
926                                     sizeof(ip));
927                                 log(LOG_ERR, "%s: could not add %s (%d)\n",
928                                     __func__, ip, rc);
929                                 free(ce, M_CXGBE);
930                         }
931 next:
932                         continue;
933                 }
934                 CURVNET_RESTORE();
935                 last_vnet = (uintptr_t)vi->ifp->if_vnet;
936         }
937
938         /*
939          * Remove stale addresses (those no longer in V_in6_ifaddrhead) that are
940          * no longer referenced by the driver.
941          */
942         TAILQ_FOREACH_SAFE(ce, &stale, link, ce_temp) {
943                 if (ce->refcount == 0) {
944                         rc = delete_lip(sc, &ce->lip);
945                         if (rc == 0) {
946                                 TAILQ_REMOVE(&stale, ce, link);
947                                 free(ce, M_CXGBE);
948                         } else {
949                                 char ip[INET6_ADDRSTRLEN];
950
951                                 inet_ntop(AF_INET6, &ce->lip, &ip[0],
952                                     sizeof(ip));
953                                 log(LOG_ERR, "%s: could not delete %s (%d)\n",
954                                     __func__, ip, rc);
955                         }
956                 }
957         }
958         /* The ones that are still referenced need to stay in the CLIP table */
959         TAILQ_CONCAT(&td->clip_table, &stale, link);
960
961         td->clip_gen = gen;
962 done:
963         mtx_unlock(&td->clip_table_lock);
964         IN6_IFADDR_RUNLOCK(&in6_ifa_tracker);
965 }
966
967 static void
968 destroy_clip_table(struct adapter *sc, struct tom_data *td)
969 {
970         struct clip_entry *ce, *ce_temp;
971
972         if (mtx_initialized(&td->clip_table_lock)) {
973                 mtx_lock(&td->clip_table_lock);
974                 TAILQ_FOREACH_SAFE(ce, &td->clip_table, link, ce_temp) {
975                         KASSERT(ce->refcount == 0,
976                             ("%s: CLIP entry %p still in use (%d)", __func__,
977                             ce, ce->refcount));
978                         TAILQ_REMOVE(&td->clip_table, ce, link);
979                         delete_lip(sc, &ce->lip);
980                         free(ce, M_CXGBE);
981                 }
982                 mtx_unlock(&td->clip_table_lock);
983                 mtx_destroy(&td->clip_table_lock);
984         }
985 }
986
987 static void
988 free_tom_data(struct adapter *sc, struct tom_data *td)
989 {
990
991         ASSERT_SYNCHRONIZED_OP(sc);
992
993         KASSERT(TAILQ_EMPTY(&td->toep_list),
994             ("%s: TOE PCB list is not empty.", __func__));
995         KASSERT(td->lctx_count == 0,
996             ("%s: lctx hash table is not empty.", __func__));
997
998         t4_free_ppod_region(&td->pr);
999         destroy_clip_table(sc, td);
1000
1001         if (td->listen_mask != 0)
1002                 hashdestroy(td->listen_hash, M_CXGBE, td->listen_mask);
1003
1004         if (mtx_initialized(&td->unsent_wr_lock))
1005                 mtx_destroy(&td->unsent_wr_lock);
1006         if (mtx_initialized(&td->lctx_hash_lock))
1007                 mtx_destroy(&td->lctx_hash_lock);
1008         if (mtx_initialized(&td->toep_list_lock))
1009                 mtx_destroy(&td->toep_list_lock);
1010
1011         free_tid_tabs(&sc->tids);
1012         free(td, M_CXGBE);
1013 }
1014
1015 static char *
1016 prepare_pkt(int open_type, uint16_t vtag, struct inpcb *inp, int *pktlen,
1017     int *buflen)
1018 {
1019         char *pkt;
1020         struct tcphdr *th;
1021         int ipv6, len;
1022         const int maxlen =
1023             max(sizeof(struct ether_header), sizeof(struct ether_vlan_header)) +
1024             max(sizeof(struct ip), sizeof(struct ip6_hdr)) +
1025             sizeof(struct tcphdr);
1026
1027         MPASS(open_type == OPEN_TYPE_ACTIVE || open_type == OPEN_TYPE_LISTEN);
1028
1029         pkt = malloc(maxlen, M_CXGBE, M_ZERO | M_NOWAIT);
1030         if (pkt == NULL)
1031                 return (NULL);
1032
1033         ipv6 = inp->inp_vflag & INP_IPV6;
1034         len = 0;
1035
1036         if (vtag == 0xffff) {
1037                 struct ether_header *eh = (void *)pkt;
1038
1039                 if (ipv6)
1040                         eh->ether_type = htons(ETHERTYPE_IPV6);
1041                 else
1042                         eh->ether_type = htons(ETHERTYPE_IP);
1043
1044                 len += sizeof(*eh);
1045         } else {
1046                 struct ether_vlan_header *evh = (void *)pkt;
1047
1048                 evh->evl_encap_proto = htons(ETHERTYPE_VLAN);
1049                 evh->evl_tag = htons(vtag);
1050                 if (ipv6)
1051                         evh->evl_proto = htons(ETHERTYPE_IPV6);
1052                 else
1053                         evh->evl_proto = htons(ETHERTYPE_IP);
1054
1055                 len += sizeof(*evh);
1056         }
1057
1058         if (ipv6) {
1059                 struct ip6_hdr *ip6 = (void *)&pkt[len];
1060
1061                 ip6->ip6_vfc = IPV6_VERSION;
1062                 ip6->ip6_plen = htons(sizeof(struct tcphdr));
1063                 ip6->ip6_nxt = IPPROTO_TCP;
1064                 if (open_type == OPEN_TYPE_ACTIVE) {
1065                         ip6->ip6_src = inp->in6p_laddr;
1066                         ip6->ip6_dst = inp->in6p_faddr;
1067                 } else if (open_type == OPEN_TYPE_LISTEN) {
1068                         ip6->ip6_src = inp->in6p_laddr;
1069                         ip6->ip6_dst = ip6->ip6_src;
1070                 }
1071
1072                 len += sizeof(*ip6);
1073         } else {
1074                 struct ip *ip = (void *)&pkt[len];
1075
1076                 ip->ip_v = IPVERSION;
1077                 ip->ip_hl = sizeof(*ip) >> 2;
1078                 ip->ip_tos = inp->inp_ip_tos;
1079                 ip->ip_len = htons(sizeof(struct ip) + sizeof(struct tcphdr));
1080                 ip->ip_ttl = inp->inp_ip_ttl;
1081                 ip->ip_p = IPPROTO_TCP;
1082                 if (open_type == OPEN_TYPE_ACTIVE) {
1083                         ip->ip_src = inp->inp_laddr;
1084                         ip->ip_dst = inp->inp_faddr;
1085                 } else if (open_type == OPEN_TYPE_LISTEN) {
1086                         ip->ip_src = inp->inp_laddr;
1087                         ip->ip_dst = ip->ip_src;
1088                 }
1089
1090                 len += sizeof(*ip);
1091         }
1092
1093         th = (void *)&pkt[len];
1094         if (open_type == OPEN_TYPE_ACTIVE) {
1095                 th->th_sport = inp->inp_lport;  /* network byte order already */
1096                 th->th_dport = inp->inp_fport;  /* ditto */
1097         } else if (open_type == OPEN_TYPE_LISTEN) {
1098                 th->th_sport = inp->inp_lport;  /* network byte order already */
1099                 th->th_dport = th->th_sport;
1100         }
1101         len += sizeof(th);
1102
1103         *pktlen = *buflen = len;
1104         return (pkt);
1105 }
1106
1107 const struct offload_settings *
1108 lookup_offload_policy(struct adapter *sc, int open_type, struct mbuf *m,
1109     uint16_t vtag, struct inpcb *inp)
1110 {
1111         const struct t4_offload_policy *op;
1112         char *pkt;
1113         struct offload_rule *r;
1114         int i, matched, pktlen, buflen;
1115         static const struct offload_settings allow_offloading_settings = {
1116                 .offload = 1,
1117                 .rx_coalesce = -1,
1118                 .cong_algo = -1,
1119                 .sched_class = -1,
1120                 .tstamp = -1,
1121                 .sack = -1,
1122                 .nagle = -1,
1123                 .ecn = -1,
1124                 .ddp = -1,
1125                 .tls = -1,
1126                 .txq = -1,
1127                 .rxq = -1,
1128                 .mss = -1,
1129         };
1130         static const struct offload_settings disallow_offloading_settings = {
1131                 .offload = 0,
1132                 /* rest is irrelevant when offload is off. */
1133         };
1134
1135         rw_assert(&sc->policy_lock, RA_LOCKED);
1136
1137         /*
1138          * If there's no Connection Offloading Policy attached to the device
1139          * then we need to return a default static policy.  If
1140          * "cop_managed_offloading" is true, then we need to disallow
1141          * offloading until a COP is attached to the device.  Otherwise we
1142          * allow offloading ...
1143          */
1144         op = sc->policy;
1145         if (op == NULL) {
1146                 if (sc->tt.cop_managed_offloading)
1147                         return (&disallow_offloading_settings);
1148                 else
1149                         return (&allow_offloading_settings);
1150         }
1151
1152         switch (open_type) {
1153         case OPEN_TYPE_ACTIVE:
1154         case OPEN_TYPE_LISTEN:
1155                 pkt = prepare_pkt(open_type, vtag, inp, &pktlen, &buflen);
1156                 break;
1157         case OPEN_TYPE_PASSIVE:
1158                 MPASS(m != NULL);
1159                 pkt = mtod(m, char *);
1160                 MPASS(*pkt == CPL_PASS_ACCEPT_REQ);
1161                 pkt += sizeof(struct cpl_pass_accept_req);
1162                 pktlen = m->m_pkthdr.len - sizeof(struct cpl_pass_accept_req);
1163                 buflen = m->m_len - sizeof(struct cpl_pass_accept_req);
1164                 break;
1165         default:
1166                 MPASS(0);
1167                 return (&disallow_offloading_settings);
1168         }
1169
1170         if (pkt == NULL || pktlen == 0 || buflen == 0)
1171                 return (&disallow_offloading_settings);
1172
1173         r = &op->rule[0];
1174         for (i = 0; i < op->nrules; i++, r++) {
1175                 if (r->open_type != open_type &&
1176                     r->open_type != OPEN_TYPE_DONTCARE) {
1177                         continue;
1178                 }
1179                 matched = bpf_filter(r->bpf_prog.bf_insns, pkt, pktlen, buflen);
1180                 if (matched)
1181                         break;
1182         }
1183
1184         if (open_type == OPEN_TYPE_ACTIVE || open_type == OPEN_TYPE_LISTEN)
1185                 free(pkt, M_CXGBE);
1186
1187         return (matched ? &r->settings : &disallow_offloading_settings);
1188 }
1189
1190 static void
1191 reclaim_wr_resources(void *arg, int count)
1192 {
1193         struct tom_data *td = arg;
1194         STAILQ_HEAD(, wrqe) twr_list = STAILQ_HEAD_INITIALIZER(twr_list);
1195         struct cpl_act_open_req *cpl;
1196         u_int opcode, atid;
1197         struct wrqe *wr;
1198         struct adapter *sc;
1199
1200         mtx_lock(&td->unsent_wr_lock);
1201         STAILQ_SWAP(&td->unsent_wr_list, &twr_list, wrqe);
1202         mtx_unlock(&td->unsent_wr_lock);
1203
1204         while ((wr = STAILQ_FIRST(&twr_list)) != NULL) {
1205                 STAILQ_REMOVE_HEAD(&twr_list, link);
1206
1207                 cpl = wrtod(wr);
1208                 opcode = GET_OPCODE(cpl);
1209
1210                 switch (opcode) {
1211                 case CPL_ACT_OPEN_REQ:
1212                 case CPL_ACT_OPEN_REQ6:
1213                         atid = G_TID_TID(be32toh(OPCODE_TID(cpl)));
1214                         sc = td_adapter(td);
1215
1216                         CTR2(KTR_CXGBE, "%s: atid %u ", __func__, atid);
1217                         act_open_failure_cleanup(sc, atid, EHOSTUNREACH);
1218                         free(wr, M_CXGBE);
1219                         break;
1220                 default:
1221                         log(LOG_ERR, "%s: leaked work request %p, wr_len %d, "
1222                             "opcode %x\n", __func__, wr, wr->wr_len, opcode);
1223                         /* WR not freed here; go look at it with a debugger.  */
1224                 }
1225         }
1226 }
1227
1228 /*
1229  * Ground control to Major TOM
1230  * Commencing countdown, engines on
1231  */
1232 static int
1233 t4_tom_activate(struct adapter *sc)
1234 {
1235         struct tom_data *td;
1236         struct toedev *tod;
1237         struct vi_info *vi;
1238         struct sge_ofld_rxq *ofld_rxq;
1239         int i, j, rc, v;
1240
1241         ASSERT_SYNCHRONIZED_OP(sc);
1242
1243         /* per-adapter softc for TOM */
1244         td = malloc(sizeof(*td), M_CXGBE, M_ZERO | M_NOWAIT);
1245         if (td == NULL)
1246                 return (ENOMEM);
1247
1248         /* List of TOE PCBs and associated lock */
1249         mtx_init(&td->toep_list_lock, "PCB list lock", NULL, MTX_DEF);
1250         TAILQ_INIT(&td->toep_list);
1251
1252         /* Listen context */
1253         mtx_init(&td->lctx_hash_lock, "lctx hash lock", NULL, MTX_DEF);
1254         td->listen_hash = hashinit_flags(LISTEN_HASH_SIZE, M_CXGBE,
1255             &td->listen_mask, HASH_NOWAIT);
1256
1257         /* List of WRs for which L2 resolution failed */
1258         mtx_init(&td->unsent_wr_lock, "Unsent WR list lock", NULL, MTX_DEF);
1259         STAILQ_INIT(&td->unsent_wr_list);
1260         TASK_INIT(&td->reclaim_wr_resources, 0, reclaim_wr_resources, td);
1261
1262         /* TID tables */
1263         rc = alloc_tid_tabs(&sc->tids);
1264         if (rc != 0)
1265                 goto done;
1266
1267         rc = t4_init_ppod_region(&td->pr, &sc->vres.ddp,
1268             t4_read_reg(sc, A_ULP_RX_TDDP_PSZ), "TDDP page pods");
1269         if (rc != 0)
1270                 goto done;
1271         t4_set_reg_field(sc, A_ULP_RX_TDDP_TAGMASK,
1272             V_TDDPTAGMASK(M_TDDPTAGMASK), td->pr.pr_tag_mask);
1273
1274         /* CLIP table for IPv6 offload */
1275         init_clip_table(sc, td);
1276
1277         /* toedev ops */
1278         tod = &td->tod;
1279         init_toedev(tod);
1280         tod->tod_softc = sc;
1281         tod->tod_connect = t4_connect;
1282         tod->tod_listen_start = t4_listen_start;
1283         tod->tod_listen_stop = t4_listen_stop;
1284         tod->tod_rcvd = t4_rcvd;
1285         tod->tod_output = t4_tod_output;
1286         tod->tod_send_rst = t4_send_rst;
1287         tod->tod_send_fin = t4_send_fin;
1288         tod->tod_pcb_detach = t4_pcb_detach;
1289         tod->tod_l2_update = t4_l2_update;
1290         tod->tod_syncache_added = t4_syncache_added;
1291         tod->tod_syncache_removed = t4_syncache_removed;
1292         tod->tod_syncache_respond = t4_syncache_respond;
1293         tod->tod_offload_socket = t4_offload_socket;
1294         tod->tod_ctloutput = t4_ctloutput;
1295
1296         for_each_port(sc, i) {
1297                 for_each_vi(sc->port[i], v, vi) {
1298                         TOEDEV(vi->ifp) = &td->tod;
1299                         for_each_ofld_rxq(vi, j, ofld_rxq) {
1300                                 ofld_rxq->iq.set_tcb_rpl = do_set_tcb_rpl;
1301                                 ofld_rxq->iq.l2t_write_rpl = do_l2t_write_rpl2;
1302                         }
1303                 }
1304         }
1305
1306         sc->tom_softc = td;
1307         register_toedev(sc->tom_softc);
1308
1309 done:
1310         if (rc != 0)
1311                 free_tom_data(sc, td);
1312         return (rc);
1313 }
1314
1315 static int
1316 t4_tom_deactivate(struct adapter *sc)
1317 {
1318         int rc = 0;
1319         struct tom_data *td = sc->tom_softc;
1320
1321         ASSERT_SYNCHRONIZED_OP(sc);
1322
1323         if (td == NULL)
1324                 return (0);     /* XXX. KASSERT? */
1325
1326         if (sc->offload_map != 0)
1327                 return (EBUSY); /* at least one port has IFCAP_TOE enabled */
1328
1329         if (uld_active(sc, ULD_IWARP) || uld_active(sc, ULD_ISCSI))
1330                 return (EBUSY); /* both iWARP and iSCSI rely on the TOE. */
1331
1332         mtx_lock(&td->toep_list_lock);
1333         if (!TAILQ_EMPTY(&td->toep_list))
1334                 rc = EBUSY;
1335         mtx_unlock(&td->toep_list_lock);
1336
1337         mtx_lock(&td->lctx_hash_lock);
1338         if (td->lctx_count > 0)
1339                 rc = EBUSY;
1340         mtx_unlock(&td->lctx_hash_lock);
1341
1342         taskqueue_drain(taskqueue_thread, &td->reclaim_wr_resources);
1343         mtx_lock(&td->unsent_wr_lock);
1344         if (!STAILQ_EMPTY(&td->unsent_wr_list))
1345                 rc = EBUSY;
1346         mtx_unlock(&td->unsent_wr_lock);
1347
1348         if (rc == 0) {
1349                 unregister_toedev(sc->tom_softc);
1350                 free_tom_data(sc, td);
1351                 sc->tom_softc = NULL;
1352         }
1353
1354         return (rc);
1355 }
1356
1357 static void
1358 t4_tom_ifaddr_event(void *arg __unused, struct ifnet *ifp)
1359 {
1360
1361         atomic_add_rel_int(&in6_ifaddr_gen, 1);
1362         taskqueue_enqueue_timeout(taskqueue_thread, &clip_task, -hz / 4);
1363 }
1364
1365 static int
1366 t4_aio_queue_tom(struct socket *so, struct kaiocb *job)
1367 {
1368         struct tcpcb *tp = so_sototcpcb(so);
1369         struct toepcb *toep = tp->t_toe;
1370         int error;
1371
1372         if (toep->ulp_mode == ULP_MODE_TCPDDP) {
1373                 error = t4_aio_queue_ddp(so, job);
1374                 if (error != EOPNOTSUPP)
1375                         return (error);
1376         }
1377
1378         return (t4_aio_queue_aiotx(so, job));
1379 }
1380
1381 static int
1382 t4_ctloutput_tom(struct socket *so, struct sockopt *sopt)
1383 {
1384
1385         if (sopt->sopt_level != IPPROTO_TCP)
1386                 return (tcp_ctloutput(so, sopt));
1387
1388         switch (sopt->sopt_name) {
1389         case TCP_TLSOM_SET_TLS_CONTEXT:
1390         case TCP_TLSOM_GET_TLS_TOM:
1391         case TCP_TLSOM_CLR_TLS_TOM:
1392         case TCP_TLSOM_CLR_QUIES:
1393                 return (t4_ctloutput_tls(so, sopt));
1394         default:
1395                 return (tcp_ctloutput(so, sopt));
1396         }
1397 }
1398
1399 static int
1400 t4_tom_mod_load(void)
1401 {
1402         struct protosw *tcp_protosw, *tcp6_protosw;
1403
1404         /* CPL handlers */
1405         t4_init_connect_cpl_handlers();
1406         t4_init_listen_cpl_handlers();
1407         t4_init_cpl_io_handlers();
1408
1409         t4_ddp_mod_load();
1410         t4_tls_mod_load();
1411
1412         tcp_protosw = pffindproto(PF_INET, IPPROTO_TCP, SOCK_STREAM);
1413         if (tcp_protosw == NULL)
1414                 return (ENOPROTOOPT);
1415         bcopy(tcp_protosw, &toe_protosw, sizeof(toe_protosw));
1416         bcopy(tcp_protosw->pr_usrreqs, &toe_usrreqs, sizeof(toe_usrreqs));
1417         toe_usrreqs.pru_aio_queue = t4_aio_queue_tom;
1418         toe_protosw.pr_ctloutput = t4_ctloutput_tom;
1419         toe_protosw.pr_usrreqs = &toe_usrreqs;
1420
1421         tcp6_protosw = pffindproto(PF_INET6, IPPROTO_TCP, SOCK_STREAM);
1422         if (tcp6_protosw == NULL)
1423                 return (ENOPROTOOPT);
1424         bcopy(tcp6_protosw, &toe6_protosw, sizeof(toe6_protosw));
1425         bcopy(tcp6_protosw->pr_usrreqs, &toe6_usrreqs, sizeof(toe6_usrreqs));
1426         toe6_usrreqs.pru_aio_queue = t4_aio_queue_tom;
1427         toe6_protosw.pr_ctloutput = t4_ctloutput_tom;
1428         toe6_protosw.pr_usrreqs = &toe6_usrreqs;
1429
1430         TIMEOUT_TASK_INIT(taskqueue_thread, &clip_task, 0, t4_clip_task, NULL);
1431         ifaddr_evhandler = EVENTHANDLER_REGISTER(ifaddr_event,
1432             t4_tom_ifaddr_event, NULL, EVENTHANDLER_PRI_ANY);
1433
1434         return (t4_register_uld(&tom_uld_info));
1435 }
1436
1437 static void
1438 tom_uninit(struct adapter *sc, void *arg __unused)
1439 {
1440         if (begin_synchronized_op(sc, NULL, SLEEP_OK | INTR_OK, "t4tomun"))
1441                 return;
1442
1443         /* Try to free resources (works only if no port has IFCAP_TOE) */
1444         if (uld_active(sc, ULD_TOM))
1445                 t4_deactivate_uld(sc, ULD_TOM);
1446
1447         end_synchronized_op(sc, 0);
1448 }
1449
1450 static int
1451 t4_tom_mod_unload(void)
1452 {
1453         t4_iterate(tom_uninit, NULL);
1454
1455         if (t4_unregister_uld(&tom_uld_info) == EBUSY)
1456                 return (EBUSY);
1457
1458         if (ifaddr_evhandler) {
1459                 EVENTHANDLER_DEREGISTER(ifaddr_event, ifaddr_evhandler);
1460                 taskqueue_cancel_timeout(taskqueue_thread, &clip_task, NULL);
1461         }
1462
1463         t4_tls_mod_unload();
1464         t4_ddp_mod_unload();
1465
1466         t4_uninit_connect_cpl_handlers();
1467         t4_uninit_listen_cpl_handlers();
1468         t4_uninit_cpl_io_handlers();
1469
1470         return (0);
1471 }
1472 #endif  /* TCP_OFFLOAD */
1473
1474 static int
1475 t4_tom_modevent(module_t mod, int cmd, void *arg)
1476 {
1477         int rc = 0;
1478
1479 #ifdef TCP_OFFLOAD
1480         switch (cmd) {
1481         case MOD_LOAD:
1482                 rc = t4_tom_mod_load();
1483                 break;
1484
1485         case MOD_UNLOAD:
1486                 rc = t4_tom_mod_unload();
1487                 break;
1488
1489         default:
1490                 rc = EINVAL;
1491         }
1492 #else
1493         printf("t4_tom: compiled without TCP_OFFLOAD support.\n");
1494         rc = EOPNOTSUPP;
1495 #endif
1496         return (rc);
1497 }
1498
1499 static moduledata_t t4_tom_moddata= {
1500         "t4_tom",
1501         t4_tom_modevent,
1502         0
1503 };
1504
1505 MODULE_VERSION(t4_tom, 1);
1506 MODULE_DEPEND(t4_tom, toecore, 1, 1, 1);
1507 MODULE_DEPEND(t4_tom, t4nex, 1, 1, 1);
1508 DECLARE_MODULE(t4_tom, t4_tom_moddata, SI_SUB_EXEC, SI_ORDER_ANY);