]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/dev/cxgbe/tom/t4_tom.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.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/module.h>
40 #include <sys/protosw.h>
41 #include <sys/domain.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/taskqueue.h>
45 #include <net/if.h>
46 #include <netinet/in.h>
47 #include <netinet/in_pcb.h>
48 #include <netinet/in_var.h>
49 #include <netinet/ip.h>
50 #include <netinet/ip6.h>
51 #include <netinet/tcp_var.h>
52 #include <netinet6/scope6_var.h>
53 #define TCPSTATES
54 #include <netinet/tcp_fsm.h>
55 #include <netinet/toecore.h>
56
57 #ifdef TCP_OFFLOAD
58 #include "common/common.h"
59 #include "common/t4_msg.h"
60 #include "common/t4_regs.h"
61 #include "common/t4_regs_values.h"
62 #include "common/t4_tcb.h"
63 #include "tom/t4_tom_l2t.h"
64 #include "tom/t4_tom.h"
65
66 static struct protosw ddp_protosw;
67 static struct pr_usrreqs ddp_usrreqs;
68
69 static struct protosw ddp6_protosw;
70 static struct pr_usrreqs ddp6_usrreqs;
71
72 /* Module ops */
73 static int t4_tom_mod_load(void);
74 static int t4_tom_mod_unload(void);
75 static int t4_tom_modevent(module_t, int, void *);
76
77 /* ULD ops and helpers */
78 static int t4_tom_activate(struct adapter *);
79 static int t4_tom_deactivate(struct adapter *);
80
81 static struct uld_info tom_uld_info = {
82         .uld_id = ULD_TOM,
83         .activate = t4_tom_activate,
84         .deactivate = t4_tom_deactivate,
85 };
86
87 static void queue_tid_release(struct adapter *, int);
88 static void release_offload_resources(struct toepcb *);
89 static int alloc_tid_tabs(struct tid_info *);
90 static void free_tid_tabs(struct tid_info *);
91 static int add_lip(struct adapter *, struct in6_addr *);
92 static int delete_lip(struct adapter *, struct in6_addr *);
93 static struct clip_entry *search_lip(struct tom_data *, struct in6_addr *);
94 static void init_clip_table(struct adapter *, struct tom_data *);
95 static void update_clip(struct adapter *, void *);
96 static void t4_clip_task(void *, int);
97 static void update_clip_table(struct adapter *, struct tom_data *);
98 static void destroy_clip_table(struct adapter *, struct tom_data *);
99 static void free_tom_data(struct adapter *, struct tom_data *);
100
101 static int in6_ifaddr_gen;
102 static eventhandler_tag ifaddr_evhandler;
103 static struct timeout_task clip_task;
104
105 struct toepcb *
106 alloc_toepcb(struct port_info *pi, int txqid, int rxqid, int flags)
107 {
108         struct adapter *sc = pi->adapter;
109         struct toepcb *toep;
110         int tx_credits, txsd_total, len;
111
112         /*
113          * The firmware counts tx work request credits in units of 16 bytes
114          * each.  Reserve room for an ABORT_REQ so the driver never has to worry
115          * about tx credits if it wants to abort a connection.
116          */
117         tx_credits = sc->params.ofldq_wr_cred;
118         tx_credits -= howmany(sizeof(struct cpl_abort_req), 16);
119
120         /*
121          * Shortest possible tx work request is a fw_ofld_tx_data_wr + 1 byte
122          * immediate payload, and firmware counts tx work request credits in
123          * units of 16 byte.  Calculate the maximum work requests possible.
124          */
125         txsd_total = tx_credits /
126             howmany((sizeof(struct fw_ofld_tx_data_wr) + 1), 16);
127
128         if (txqid < 0)
129                 txqid = (arc4random() % pi->nofldtxq) + pi->first_ofld_txq;
130         KASSERT(txqid >= pi->first_ofld_txq &&
131             txqid < pi->first_ofld_txq + pi->nofldtxq,
132             ("%s: txqid %d for port %p (first %d, n %d)", __func__, txqid, pi,
133                 pi->first_ofld_txq, pi->nofldtxq));
134
135         if (rxqid < 0)
136                 rxqid = (arc4random() % pi->nofldrxq) + pi->first_ofld_rxq;
137         KASSERT(rxqid >= pi->first_ofld_rxq &&
138             rxqid < pi->first_ofld_rxq + pi->nofldrxq,
139             ("%s: rxqid %d for port %p (first %d, n %d)", __func__, rxqid, pi,
140                 pi->first_ofld_rxq, pi->nofldrxq));
141
142         len = offsetof(struct toepcb, txsd) +
143             txsd_total * sizeof(struct ofld_tx_sdesc);
144
145         toep = malloc(len, M_CXGBE, M_ZERO | flags);
146         if (toep == NULL)
147                 return (NULL);
148
149         toep->td = sc->tom_softc;
150         toep->port = pi;
151         toep->tx_credits = tx_credits;
152         toep->ofld_txq = &sc->sge.ofld_txq[txqid];
153         toep->ofld_rxq = &sc->sge.ofld_rxq[rxqid];
154         toep->ctrlq = &sc->sge.ctrlq[pi->port_id];
155         toep->txsd_total = txsd_total;
156         toep->txsd_avail = txsd_total;
157         toep->txsd_pidx = 0;
158         toep->txsd_cidx = 0;
159
160         return (toep);
161 }
162
163 void
164 free_toepcb(struct toepcb *toep)
165 {
166
167         KASSERT(!(toep->flags & TPF_ATTACHED),
168             ("%s: attached to an inpcb", __func__));
169         KASSERT(!(toep->flags & TPF_CPL_PENDING),
170             ("%s: CPL pending", __func__));
171
172         free(toep, M_CXGBE);
173 }
174
175 /*
176  * Set up the socket for TCP offload.
177  */
178 void
179 offload_socket(struct socket *so, struct toepcb *toep)
180 {
181         struct tom_data *td = toep->td;
182         struct inpcb *inp = sotoinpcb(so);
183         struct tcpcb *tp = intotcpcb(inp);
184         struct sockbuf *sb;
185
186         INP_WLOCK_ASSERT(inp);
187
188         /* Update socket */
189         sb = &so->so_snd;
190         SOCKBUF_LOCK(sb);
191         sb->sb_flags |= SB_NOCOALESCE;
192         SOCKBUF_UNLOCK(sb);
193         sb = &so->so_rcv;
194         SOCKBUF_LOCK(sb);
195         sb->sb_flags |= SB_NOCOALESCE;
196         if (toep->ulp_mode == ULP_MODE_TCPDDP) {
197                 if (inp->inp_vflag & INP_IPV6)
198                         so->so_proto = &ddp6_protosw;
199                 else
200                         so->so_proto = &ddp_protosw;
201         }
202         SOCKBUF_UNLOCK(sb);
203
204         /* Update TCP PCB */
205         tp->tod = &td->tod;
206         tp->t_toe = toep;
207         tp->t_flags |= TF_TOE;
208
209         /* Install an extra hold on inp */
210         toep->inp = inp;
211         toep->flags |= TPF_ATTACHED;
212         in_pcbref(inp);
213
214         /* Add the TOE PCB to the active list */
215         mtx_lock(&td->toep_list_lock);
216         TAILQ_INSERT_HEAD(&td->toep_list, toep, link);
217         mtx_unlock(&td->toep_list_lock);
218 }
219
220 /* This is _not_ the normal way to "unoffload" a socket. */
221 void
222 undo_offload_socket(struct socket *so)
223 {
224         struct inpcb *inp = sotoinpcb(so);
225         struct tcpcb *tp = intotcpcb(inp);
226         struct toepcb *toep = tp->t_toe;
227         struct tom_data *td = toep->td;
228         struct sockbuf *sb;
229
230         INP_WLOCK_ASSERT(inp);
231
232         sb = &so->so_snd;
233         SOCKBUF_LOCK(sb);
234         sb->sb_flags &= ~SB_NOCOALESCE;
235         SOCKBUF_UNLOCK(sb);
236         sb = &so->so_rcv;
237         SOCKBUF_LOCK(sb);
238         sb->sb_flags &= ~SB_NOCOALESCE;
239         SOCKBUF_UNLOCK(sb);
240
241         tp->tod = NULL;
242         tp->t_toe = NULL;
243         tp->t_flags &= ~TF_TOE;
244
245         toep->inp = NULL;
246         toep->flags &= ~TPF_ATTACHED;
247         if (in_pcbrele_wlocked(inp))
248                 panic("%s: inp freed.", __func__);
249
250         mtx_lock(&td->toep_list_lock);
251         TAILQ_REMOVE(&td->toep_list, toep, link);
252         mtx_unlock(&td->toep_list_lock);
253 }
254
255 static void
256 release_offload_resources(struct toepcb *toep)
257 {
258         struct tom_data *td = toep->td;
259         struct adapter *sc = td_adapter(td);
260         int tid = toep->tid;
261
262         KASSERT(!(toep->flags & TPF_CPL_PENDING),
263             ("%s: %p has CPL pending.", __func__, toep));
264         KASSERT(!(toep->flags & TPF_ATTACHED),
265             ("%s: %p is still attached.", __func__, toep));
266
267         CTR5(KTR_CXGBE, "%s: toep %p (tid %d, l2te %p, ce %p)",
268             __func__, toep, tid, toep->l2te, toep->ce);
269
270         if (toep->ulp_mode == ULP_MODE_TCPDDP)
271                 release_ddp_resources(toep);
272
273         if (toep->l2te)
274                 t4_l2t_release(toep->l2te);
275
276         if (tid >= 0) {
277                 remove_tid(sc, tid);
278                 release_tid(sc, tid, toep->ctrlq);
279         }
280
281         if (toep->ce)
282                 release_lip(td, toep->ce);
283
284         mtx_lock(&td->toep_list_lock);
285         TAILQ_REMOVE(&td->toep_list, toep, link);
286         mtx_unlock(&td->toep_list_lock);
287
288         free_toepcb(toep);
289 }
290
291 /*
292  * The kernel is done with the TCP PCB and this is our opportunity to unhook the
293  * toepcb hanging off of it.  If the TOE driver is also done with the toepcb (no
294  * pending CPL) then it is time to release all resources tied to the toepcb.
295  *
296  * Also gets called when an offloaded active open fails and the TOM wants the
297  * kernel to take the TCP PCB back.
298  */
299 static void
300 t4_pcb_detach(struct toedev *tod __unused, struct tcpcb *tp)
301 {
302 #if defined(KTR) || defined(INVARIANTS)
303         struct inpcb *inp = tp->t_inpcb;
304 #endif
305         struct toepcb *toep = tp->t_toe;
306
307         INP_WLOCK_ASSERT(inp);
308
309         KASSERT(toep != NULL, ("%s: toep is NULL", __func__));
310         KASSERT(toep->flags & TPF_ATTACHED,
311             ("%s: not attached", __func__));
312
313 #ifdef KTR
314         if (tp->t_state == TCPS_SYN_SENT) {
315                 CTR6(KTR_CXGBE, "%s: atid %d, toep %p (0x%x), inp %p (0x%x)",
316                     __func__, toep->tid, toep, toep->flags, inp,
317                     inp->inp_flags);
318         } else {
319                 CTR6(KTR_CXGBE,
320                     "t4_pcb_detach: tid %d (%s), toep %p (0x%x), inp %p (0x%x)",
321                     toep->tid, tcpstates[tp->t_state], toep, toep->flags, inp,
322                     inp->inp_flags);
323         }
324 #endif
325
326         tp->t_toe = NULL;
327         tp->t_flags &= ~TF_TOE;
328         toep->flags &= ~TPF_ATTACHED;
329
330         if (!(toep->flags & TPF_CPL_PENDING))
331                 release_offload_resources(toep);
332 }
333
334 /*
335  * setsockopt handler.
336  */
337 static void
338 t4_ctloutput(struct toedev *tod, struct tcpcb *tp, int dir, int name)
339 {
340         struct adapter *sc = tod->tod_softc;
341         struct toepcb *toep = tp->t_toe;
342
343         if (dir == SOPT_GET)
344                 return;
345
346         CTR4(KTR_CXGBE, "%s: tp %p, dir %u, name %u", __func__, tp, dir, name);
347
348         switch (name) {
349         case TCP_NODELAY:
350                 t4_set_tcb_field(sc, toep, 1, W_TCB_T_FLAGS, V_TF_NAGLE(1),
351                     V_TF_NAGLE(tp->t_flags & TF_NODELAY ? 0 : 1));
352                 break;
353         default:
354                 break;
355         }
356 }
357
358 /*
359  * The TOE driver will not receive any more CPLs for the tid associated with the
360  * toepcb; release the hold on the inpcb.
361  */
362 void
363 final_cpl_received(struct toepcb *toep)
364 {
365         struct inpcb *inp = toep->inp;
366
367         KASSERT(inp != NULL, ("%s: inp is NULL", __func__));
368         INP_WLOCK_ASSERT(inp);
369         KASSERT(toep->flags & TPF_CPL_PENDING,
370             ("%s: CPL not pending already?", __func__));
371
372         CTR6(KTR_CXGBE, "%s: tid %d, toep %p (0x%x), inp %p (0x%x)",
373             __func__, toep->tid, toep, toep->flags, inp, inp->inp_flags);
374
375         toep->inp = NULL;
376         toep->flags &= ~TPF_CPL_PENDING;
377
378         if (!(toep->flags & TPF_ATTACHED))
379                 release_offload_resources(toep);
380
381         if (!in_pcbrele_wlocked(inp))
382                 INP_WUNLOCK(inp);
383 }
384
385 void
386 insert_tid(struct adapter *sc, int tid, void *ctx)
387 {
388         struct tid_info *t = &sc->tids;
389
390         t->tid_tab[tid] = ctx;
391         atomic_add_int(&t->tids_in_use, 1);
392 }
393
394 void *
395 lookup_tid(struct adapter *sc, int tid)
396 {
397         struct tid_info *t = &sc->tids;
398
399         return (t->tid_tab[tid]);
400 }
401
402 void
403 update_tid(struct adapter *sc, int tid, void *ctx)
404 {
405         struct tid_info *t = &sc->tids;
406
407         t->tid_tab[tid] = ctx;
408 }
409
410 void
411 remove_tid(struct adapter *sc, int tid)
412 {
413         struct tid_info *t = &sc->tids;
414
415         t->tid_tab[tid] = NULL;
416         atomic_subtract_int(&t->tids_in_use, 1);
417 }
418
419 void
420 release_tid(struct adapter *sc, int tid, struct sge_wrq *ctrlq)
421 {
422         struct wrqe *wr;
423         struct cpl_tid_release *req;
424
425         wr = alloc_wrqe(sizeof(*req), ctrlq);
426         if (wr == NULL) {
427                 queue_tid_release(sc, tid);     /* defer */
428                 return;
429         }
430         req = wrtod(wr);
431
432         INIT_TP_WR_MIT_CPL(req, CPL_TID_RELEASE, tid);
433
434         t4_wrq_tx(sc, wr);
435 }
436
437 static void
438 queue_tid_release(struct adapter *sc, int tid)
439 {
440
441         CXGBE_UNIMPLEMENTED("deferred tid release");
442 }
443
444 /*
445  * What mtu_idx to use, given a 4-tuple and/or an MSS cap
446  */
447 int
448 find_best_mtu_idx(struct adapter *sc, struct in_conninfo *inc, int pmss)
449 {
450         unsigned short *mtus = &sc->params.mtus[0];
451         int i, mss, n;
452
453         KASSERT(inc != NULL || pmss > 0,
454             ("%s: at least one of inc/pmss must be specified", __func__));
455
456         mss = inc ? tcp_mssopt(inc) : pmss;
457         if (pmss > 0 && mss > pmss)
458                 mss = pmss;
459
460         if (inc->inc_flags & INC_ISIPV6)
461                 n = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
462         else
463                 n = sizeof(struct ip) + sizeof(struct tcphdr);
464
465         for (i = 0; i < NMTUS - 1 && mtus[i + 1] <= mss + n; i++)
466                 continue;
467
468         return (i);
469 }
470
471 /*
472  * Determine the receive window size for a socket.
473  */
474 u_long
475 select_rcv_wnd(struct socket *so)
476 {
477         unsigned long wnd;
478
479         SOCKBUF_LOCK_ASSERT(&so->so_rcv);
480
481         wnd = sbspace(&so->so_rcv);
482         if (wnd < MIN_RCV_WND)
483                 wnd = MIN_RCV_WND;
484
485         return min(wnd, MAX_RCV_WND);
486 }
487
488 int
489 select_rcv_wscale(void)
490 {
491         int wscale = 0;
492         unsigned long space = sb_max;
493
494         if (space > MAX_RCV_WND)
495                 space = MAX_RCV_WND;
496
497         while (wscale < TCP_MAX_WINSHIFT && (TCP_MAXWIN << wscale) < space)
498                 wscale++;
499
500         return (wscale);
501 }
502
503 extern int always_keepalive;
504 #define VIID_SMACIDX(v) (((unsigned int)(v) & 0x7f) << 1)
505
506 /*
507  * socket so could be a listening socket too.
508  */
509 uint64_t
510 calc_opt0(struct socket *so, struct port_info *pi, struct l2t_entry *e,
511     int mtu_idx, int rscale, int rx_credits, int ulp_mode)
512 {
513         uint64_t opt0;
514
515         KASSERT(rx_credits <= M_RCV_BUFSIZ,
516             ("%s: rcv_bufsiz too high", __func__));
517
518         opt0 = F_TCAM_BYPASS | V_WND_SCALE(rscale) | V_MSS_IDX(mtu_idx) |
519             V_ULP_MODE(ulp_mode) | V_RCV_BUFSIZ(rx_credits);
520
521         if (so != NULL) {
522                 struct inpcb *inp = sotoinpcb(so);
523                 struct tcpcb *tp = intotcpcb(inp);
524                 int keepalive = always_keepalive ||
525                     so_options_get(so) & SO_KEEPALIVE;
526
527                 opt0 |= V_NAGLE((tp->t_flags & TF_NODELAY) == 0);
528                 opt0 |= V_KEEP_ALIVE(keepalive != 0);
529         }
530
531         if (e != NULL)
532                 opt0 |= V_L2T_IDX(e->idx);
533
534         if (pi != NULL) {
535                 opt0 |= V_SMAC_SEL(VIID_SMACIDX(pi->viid));
536                 opt0 |= V_TX_CHAN(pi->tx_chan);
537         }
538
539         return htobe64(opt0);
540 }
541
542 uint64_t
543 select_ntuple(struct port_info *pi, struct l2t_entry *e)
544 {
545         struct adapter *sc = pi->adapter;
546         struct tp_params *tp = &sc->params.tp;
547         uint16_t viid = pi->viid;
548         uint64_t ntuple = 0;
549
550         /*
551          * Initialize each of the fields which we care about which are present
552          * in the Compressed Filter Tuple.
553          */
554         if (tp->vlan_shift >= 0 && e->vlan != CPL_L2T_VLAN_NONE)
555                 ntuple |= (uint64_t)(F_FT_VLAN_VLD | e->vlan) << tp->vlan_shift;
556
557         if (tp->port_shift >= 0)
558                 ntuple |= (uint64_t)e->lport << tp->port_shift;
559
560         if (tp->protocol_shift >= 0)
561                 ntuple |= (uint64_t)IPPROTO_TCP << tp->protocol_shift;
562
563         if (tp->vnic_shift >= 0) {
564                 uint32_t vf = G_FW_VIID_VIN(viid);
565                 uint32_t pf = G_FW_VIID_PFN(viid);
566                 uint32_t vld = G_FW_VIID_VIVLD(viid);
567
568                 ntuple |= (uint64_t)(V_FT_VNID_ID_VF(vf) | V_FT_VNID_ID_PF(pf) |
569                     V_FT_VNID_ID_VLD(vld)) << tp->vnic_shift;
570         }
571
572         if (is_t4(sc))
573                 return (htobe32((uint32_t)ntuple));
574         else
575                 return (htobe64(V_FILTER_TUPLE(ntuple)));
576 }
577
578 void
579 set_tcpddp_ulp_mode(struct toepcb *toep)
580 {
581
582         toep->ulp_mode = ULP_MODE_TCPDDP;
583         toep->ddp_flags = DDP_OK;
584         toep->ddp_score = DDP_LOW_SCORE;
585 }
586
587 int
588 negative_advice(int status)
589 {
590
591         return (status == CPL_ERR_RTX_NEG_ADVICE ||
592             status == CPL_ERR_PERSIST_NEG_ADVICE ||
593             status == CPL_ERR_KEEPALV_NEG_ADVICE);
594 }
595
596 static int
597 alloc_tid_tabs(struct tid_info *t)
598 {
599         size_t size;
600         unsigned int i;
601
602         size = t->ntids * sizeof(*t->tid_tab) +
603             t->natids * sizeof(*t->atid_tab) +
604             t->nstids * sizeof(*t->stid_tab);
605
606         t->tid_tab = malloc(size, M_CXGBE, M_ZERO | M_NOWAIT);
607         if (t->tid_tab == NULL)
608                 return (ENOMEM);
609
610         mtx_init(&t->atid_lock, "atid lock", NULL, MTX_DEF);
611         t->atid_tab = (union aopen_entry *)&t->tid_tab[t->ntids];
612         t->afree = t->atid_tab;
613         t->atids_in_use = 0;
614         for (i = 1; i < t->natids; i++)
615                 t->atid_tab[i - 1].next = &t->atid_tab[i];
616         t->atid_tab[t->natids - 1].next = NULL;
617
618         mtx_init(&t->stid_lock, "stid lock", NULL, MTX_DEF);
619         t->stid_tab = (struct listen_ctx **)&t->atid_tab[t->natids];
620         t->stids_in_use = 0;
621         TAILQ_INIT(&t->stids);
622         t->nstids_free_head = t->nstids;
623
624         atomic_store_rel_int(&t->tids_in_use, 0);
625
626         return (0);
627 }
628
629 static void
630 free_tid_tabs(struct tid_info *t)
631 {
632         KASSERT(t->tids_in_use == 0,
633             ("%s: %d tids still in use.", __func__, t->tids_in_use));
634         KASSERT(t->atids_in_use == 0,
635             ("%s: %d atids still in use.", __func__, t->atids_in_use));
636         KASSERT(t->stids_in_use == 0,
637             ("%s: %d tids still in use.", __func__, t->stids_in_use));
638
639         free(t->tid_tab, M_CXGBE);
640         t->tid_tab = NULL;
641
642         if (mtx_initialized(&t->atid_lock))
643                 mtx_destroy(&t->atid_lock);
644         if (mtx_initialized(&t->stid_lock))
645                 mtx_destroy(&t->stid_lock);
646 }
647
648 static int
649 add_lip(struct adapter *sc, struct in6_addr *lip)
650 {
651         struct fw_clip_cmd c;
652
653         ASSERT_SYNCHRONIZED_OP(sc);
654         /* mtx_assert(&td->clip_table_lock, MA_OWNED); */
655
656         memset(&c, 0, sizeof(c));
657         c.op_to_write = htonl(V_FW_CMD_OP(FW_CLIP_CMD) | F_FW_CMD_REQUEST |
658             F_FW_CMD_WRITE);
659         c.alloc_to_len16 = htonl(F_FW_CLIP_CMD_ALLOC | FW_LEN16(c));
660         c.ip_hi = *(uint64_t *)&lip->s6_addr[0];
661         c.ip_lo = *(uint64_t *)&lip->s6_addr[8];
662
663         return (-t4_wr_mbox_ns(sc, sc->mbox, &c, sizeof(c), &c));
664 }
665
666 static int
667 delete_lip(struct adapter *sc, struct in6_addr *lip)
668 {
669         struct fw_clip_cmd c;
670
671         ASSERT_SYNCHRONIZED_OP(sc);
672         /* mtx_assert(&td->clip_table_lock, MA_OWNED); */
673
674         memset(&c, 0, sizeof(c));
675         c.op_to_write = htonl(V_FW_CMD_OP(FW_CLIP_CMD) | F_FW_CMD_REQUEST |
676             F_FW_CMD_READ);
677         c.alloc_to_len16 = htonl(F_FW_CLIP_CMD_FREE | FW_LEN16(c));
678         c.ip_hi = *(uint64_t *)&lip->s6_addr[0];
679         c.ip_lo = *(uint64_t *)&lip->s6_addr[8];
680
681         return (-t4_wr_mbox_ns(sc, sc->mbox, &c, sizeof(c), &c));
682 }
683
684 static struct clip_entry *
685 search_lip(struct tom_data *td, struct in6_addr *lip)
686 {
687         struct clip_entry *ce;
688
689         mtx_assert(&td->clip_table_lock, MA_OWNED);
690
691         TAILQ_FOREACH(ce, &td->clip_table, link) {
692                 if (IN6_ARE_ADDR_EQUAL(&ce->lip, lip))
693                         return (ce);
694         }
695
696         return (NULL);
697 }
698
699 struct clip_entry *
700 hold_lip(struct tom_data *td, struct in6_addr *lip)
701 {
702         struct clip_entry *ce;
703
704         mtx_lock(&td->clip_table_lock);
705         ce = search_lip(td, lip);
706         if (ce != NULL)
707                 ce->refcount++;
708         mtx_unlock(&td->clip_table_lock);
709
710         return (ce);
711 }
712
713 void
714 release_lip(struct tom_data *td, struct clip_entry *ce)
715 {
716
717         mtx_lock(&td->clip_table_lock);
718         KASSERT(search_lip(td, &ce->lip) == ce,
719             ("%s: CLIP entry %p p not in CLIP table.", __func__, ce));
720         KASSERT(ce->refcount > 0,
721             ("%s: CLIP entry %p has refcount 0", __func__, ce));
722         --ce->refcount;
723         mtx_unlock(&td->clip_table_lock);
724 }
725
726 static void
727 init_clip_table(struct adapter *sc, struct tom_data *td)
728 {
729
730         ASSERT_SYNCHRONIZED_OP(sc);
731
732         mtx_init(&td->clip_table_lock, "CLIP table lock", NULL, MTX_DEF);
733         TAILQ_INIT(&td->clip_table);
734         td->clip_gen = -1;
735
736         update_clip_table(sc, td);
737 }
738
739 static void
740 update_clip(struct adapter *sc, void *arg __unused)
741 {
742
743         if (begin_synchronized_op(sc, NULL, HOLD_LOCK, "t4tomuc"))
744                 return;
745
746         if (sc->flags & TOM_INIT_DONE)
747                 update_clip_table(sc, sc->tom_softc);
748
749         end_synchronized_op(sc, LOCK_HELD);
750 }
751
752 static void
753 t4_clip_task(void *arg, int count)
754 {
755
756         t4_iterate(update_clip, NULL);
757 }
758
759 static void
760 update_clip_table(struct adapter *sc, struct tom_data *td)
761 {
762         struct in6_ifaddr *ia;
763         struct in6_addr *lip, tlip;
764         struct clip_head stale;
765         struct clip_entry *ce, *ce_temp;
766         int rc, gen = atomic_load_acq_int(&in6_ifaddr_gen);
767
768         ASSERT_SYNCHRONIZED_OP(sc);
769
770         IN6_IFADDR_RLOCK();
771         mtx_lock(&td->clip_table_lock);
772
773         if (gen == td->clip_gen)
774                 goto done;
775
776         TAILQ_INIT(&stale);
777         TAILQ_CONCAT(&stale, &td->clip_table, link);
778
779         TAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) {
780                 lip = &ia->ia_addr.sin6_addr;
781
782                 KASSERT(!IN6_IS_ADDR_MULTICAST(lip),
783                     ("%s: mcast address in in6_ifaddr list", __func__));
784
785                 if (IN6_IS_ADDR_LOOPBACK(lip))
786                         continue;
787                 if (IN6_IS_SCOPE_EMBED(lip)) {
788                         /* Remove the embedded scope */
789                         tlip = *lip;
790                         lip = &tlip;
791                         in6_clearscope(lip);
792                 }
793                 /*
794                  * XXX: how to weed out the link local address for the loopback
795                  * interface?  It's fe80::1 usually (always?).
796                  */
797
798                 /*
799                  * If it's in the main list then we already know it's not stale.
800                  */
801                 TAILQ_FOREACH(ce, &td->clip_table, link) {
802                         if (IN6_ARE_ADDR_EQUAL(&ce->lip, lip))
803                                 goto next;
804                 }
805
806                 /*
807                  * If it's in the stale list we should move it to the main list.
808                  */
809                 TAILQ_FOREACH(ce, &stale, link) {
810                         if (IN6_ARE_ADDR_EQUAL(&ce->lip, lip)) {
811                                 TAILQ_REMOVE(&stale, ce, link);
812                                 TAILQ_INSERT_TAIL(&td->clip_table, ce, link);
813                                 goto next;
814                         }
815                 }
816
817                 /* A new IP6 address; add it to the CLIP table */
818                 ce = malloc(sizeof(*ce), M_CXGBE, M_NOWAIT);
819                 memcpy(&ce->lip, lip, sizeof(ce->lip));
820                 ce->refcount = 0;
821                 rc = add_lip(sc, lip);
822                 if (rc == 0)
823                         TAILQ_INSERT_TAIL(&td->clip_table, ce, link);
824                 else {
825                         char ip[INET6_ADDRSTRLEN];
826
827                         inet_ntop(AF_INET6, &ce->lip, &ip[0], sizeof(ip));
828                         log(LOG_ERR, "%s: could not add %s (%d)\n",
829                             __func__, ip, rc);
830                         free(ce, M_CXGBE);
831                 }
832 next:
833                 continue;
834         }
835
836         /*
837          * Remove stale addresses (those no longer in V_in6_ifaddrhead) that are
838          * no longer referenced by the driver.
839          */
840         TAILQ_FOREACH_SAFE(ce, &stale, link, ce_temp) {
841                 if (ce->refcount == 0) {
842                         rc = delete_lip(sc, &ce->lip);
843                         if (rc == 0) {
844                                 TAILQ_REMOVE(&stale, ce, link);
845                                 free(ce, M_CXGBE);
846                         } else {
847                                 char ip[INET6_ADDRSTRLEN];
848
849                                 inet_ntop(AF_INET6, &ce->lip, &ip[0],
850                                     sizeof(ip));
851                                 log(LOG_ERR, "%s: could not delete %s (%d)\n",
852                                     __func__, ip, rc);
853                         }
854                 }
855         }
856         /* The ones that are still referenced need to stay in the CLIP table */
857         TAILQ_CONCAT(&td->clip_table, &stale, link);
858
859         td->clip_gen = gen;
860 done:
861         mtx_unlock(&td->clip_table_lock);
862         IN6_IFADDR_RUNLOCK();
863 }
864
865 static void
866 destroy_clip_table(struct adapter *sc, struct tom_data *td)
867 {
868         struct clip_entry *ce, *ce_temp;
869
870         if (mtx_initialized(&td->clip_table_lock)) {
871                 mtx_lock(&td->clip_table_lock);
872                 TAILQ_FOREACH_SAFE(ce, &td->clip_table, link, ce_temp) {
873                         KASSERT(ce->refcount == 0,
874                             ("%s: CLIP entry %p still in use (%d)", __func__,
875                             ce, ce->refcount));
876                         TAILQ_REMOVE(&td->clip_table, ce, link);
877                         delete_lip(sc, &ce->lip);
878                         free(ce, M_CXGBE);
879                 }
880                 mtx_unlock(&td->clip_table_lock);
881                 mtx_destroy(&td->clip_table_lock);
882         }
883 }
884
885 static void
886 free_tom_data(struct adapter *sc, struct tom_data *td)
887 {
888
889         ASSERT_SYNCHRONIZED_OP(sc);
890
891         KASSERT(TAILQ_EMPTY(&td->toep_list),
892             ("%s: TOE PCB list is not empty.", __func__));
893         KASSERT(td->lctx_count == 0,
894             ("%s: lctx hash table is not empty.", __func__));
895
896         t4_uninit_l2t_cpl_handlers(sc);
897         t4_uninit_cpl_io_handlers(sc);
898         t4_uninit_ddp(sc, td);
899         destroy_clip_table(sc, td);
900
901         if (td->listen_mask != 0)
902                 hashdestroy(td->listen_hash, M_CXGBE, td->listen_mask);
903
904         if (mtx_initialized(&td->lctx_hash_lock))
905                 mtx_destroy(&td->lctx_hash_lock);
906         if (mtx_initialized(&td->toep_list_lock))
907                 mtx_destroy(&td->toep_list_lock);
908
909         free_tid_tabs(&sc->tids);
910         free(td, M_CXGBE);
911 }
912
913 /*
914  * Ground control to Major TOM
915  * Commencing countdown, engines on
916  */
917 static int
918 t4_tom_activate(struct adapter *sc)
919 {
920         struct tom_data *td;
921         struct toedev *tod;
922         int i, rc;
923
924         ASSERT_SYNCHRONIZED_OP(sc);
925
926         /* per-adapter softc for TOM */
927         td = malloc(sizeof(*td), M_CXGBE, M_ZERO | M_NOWAIT);
928         if (td == NULL)
929                 return (ENOMEM);
930
931         /* List of TOE PCBs and associated lock */
932         mtx_init(&td->toep_list_lock, "PCB list lock", NULL, MTX_DEF);
933         TAILQ_INIT(&td->toep_list);
934
935         /* Listen context */
936         mtx_init(&td->lctx_hash_lock, "lctx hash lock", NULL, MTX_DEF);
937         td->listen_hash = hashinit_flags(LISTEN_HASH_SIZE, M_CXGBE,
938             &td->listen_mask, HASH_NOWAIT);
939
940         /* TID tables */
941         rc = alloc_tid_tabs(&sc->tids);
942         if (rc != 0)
943                 goto done;
944
945         /* DDP page pods and CPL handlers */
946         t4_init_ddp(sc, td);
947
948         /* CLIP table for IPv6 offload */
949         init_clip_table(sc, td);
950
951         /* CPL handlers */
952         t4_init_connect_cpl_handlers(sc);
953         t4_init_l2t_cpl_handlers(sc);
954         t4_init_listen_cpl_handlers(sc);
955         t4_init_cpl_io_handlers(sc);
956
957         /* toedev ops */
958         tod = &td->tod;
959         init_toedev(tod);
960         tod->tod_softc = sc;
961         tod->tod_connect = t4_connect;
962         tod->tod_listen_start = t4_listen_start;
963         tod->tod_listen_stop = t4_listen_stop;
964         tod->tod_rcvd = t4_rcvd;
965         tod->tod_output = t4_tod_output;
966         tod->tod_send_rst = t4_send_rst;
967         tod->tod_send_fin = t4_send_fin;
968         tod->tod_pcb_detach = t4_pcb_detach;
969         tod->tod_l2_update = t4_l2_update;
970         tod->tod_syncache_added = t4_syncache_added;
971         tod->tod_syncache_removed = t4_syncache_removed;
972         tod->tod_syncache_respond = t4_syncache_respond;
973         tod->tod_offload_socket = t4_offload_socket;
974         tod->tod_ctloutput = t4_ctloutput;
975
976         for_each_port(sc, i)
977                 TOEDEV(sc->port[i]->ifp) = &td->tod;
978
979         sc->tom_softc = td;
980         sc->flags |= TOM_INIT_DONE;
981         register_toedev(sc->tom_softc);
982
983 done:
984         if (rc != 0)
985                 free_tom_data(sc, td);
986         return (rc);
987 }
988
989 static int
990 t4_tom_deactivate(struct adapter *sc)
991 {
992         int rc = 0;
993         struct tom_data *td = sc->tom_softc;
994
995         ASSERT_SYNCHRONIZED_OP(sc);
996
997         if (td == NULL)
998                 return (0);     /* XXX. KASSERT? */
999
1000         if (sc->offload_map != 0)
1001                 return (EBUSY); /* at least one port has IFCAP_TOE enabled */
1002
1003         mtx_lock(&td->toep_list_lock);
1004         if (!TAILQ_EMPTY(&td->toep_list))
1005                 rc = EBUSY;
1006         mtx_unlock(&td->toep_list_lock);
1007
1008         mtx_lock(&td->lctx_hash_lock);
1009         if (td->lctx_count > 0)
1010                 rc = EBUSY;
1011         mtx_unlock(&td->lctx_hash_lock);
1012
1013         if (rc == 0) {
1014                 unregister_toedev(sc->tom_softc);
1015                 free_tom_data(sc, td);
1016                 sc->tom_softc = NULL;
1017                 sc->flags &= ~TOM_INIT_DONE;
1018         }
1019
1020         return (rc);
1021 }
1022
1023 static void
1024 t4_tom_ifaddr_event(void *arg __unused, struct ifnet *ifp)
1025 {
1026
1027         atomic_add_rel_int(&in6_ifaddr_gen, 1);
1028         taskqueue_enqueue_timeout(taskqueue_thread, &clip_task, -hz / 4);
1029 }
1030
1031 static int
1032 t4_tom_mod_load(void)
1033 {
1034         int rc;
1035         struct protosw *tcp_protosw, *tcp6_protosw;
1036
1037         tcp_protosw = pffindproto(PF_INET, IPPROTO_TCP, SOCK_STREAM);
1038         if (tcp_protosw == NULL)
1039                 return (ENOPROTOOPT);
1040         bcopy(tcp_protosw, &ddp_protosw, sizeof(ddp_protosw));
1041         bcopy(tcp_protosw->pr_usrreqs, &ddp_usrreqs, sizeof(ddp_usrreqs));
1042         ddp_usrreqs.pru_soreceive = t4_soreceive_ddp;
1043         ddp_protosw.pr_usrreqs = &ddp_usrreqs;
1044
1045         tcp6_protosw = pffindproto(PF_INET6, IPPROTO_TCP, SOCK_STREAM);
1046         if (tcp6_protosw == NULL)
1047                 return (ENOPROTOOPT);
1048         bcopy(tcp6_protosw, &ddp6_protosw, sizeof(ddp6_protosw));
1049         bcopy(tcp6_protosw->pr_usrreqs, &ddp6_usrreqs, sizeof(ddp6_usrreqs));
1050         ddp6_usrreqs.pru_soreceive = t4_soreceive_ddp;
1051         ddp6_protosw.pr_usrreqs = &ddp6_usrreqs;
1052
1053         TIMEOUT_TASK_INIT(taskqueue_thread, &clip_task, 0, t4_clip_task, NULL);
1054         ifaddr_evhandler = EVENTHANDLER_REGISTER(ifaddr_event,
1055             t4_tom_ifaddr_event, NULL, EVENTHANDLER_PRI_ANY);
1056
1057         rc = t4_register_uld(&tom_uld_info);
1058         if (rc != 0)
1059                 t4_tom_mod_unload();
1060
1061         return (rc);
1062 }
1063
1064 static void
1065 tom_uninit(struct adapter *sc, void *arg __unused)
1066 {
1067         if (begin_synchronized_op(sc, NULL, HOLD_LOCK, "t4tomun"))
1068                 return;
1069
1070         /* Try to free resources (works only if no port has IFCAP_TOE) */
1071         if (sc->flags & TOM_INIT_DONE)
1072                 t4_deactivate_uld(sc, ULD_TOM);
1073
1074         end_synchronized_op(sc, LOCK_HELD);
1075 }
1076
1077 static int
1078 t4_tom_mod_unload(void)
1079 {
1080         t4_iterate(tom_uninit, NULL);
1081
1082         if (t4_unregister_uld(&tom_uld_info) == EBUSY)
1083                 return (EBUSY);
1084
1085         if (ifaddr_evhandler) {
1086                 EVENTHANDLER_DEREGISTER(ifaddr_event, ifaddr_evhandler);
1087                 taskqueue_cancel_timeout(taskqueue_thread, &clip_task, NULL);
1088         }
1089
1090         return (0);
1091 }
1092 #endif  /* TCP_OFFLOAD */
1093
1094 static int
1095 t4_tom_modevent(module_t mod, int cmd, void *arg)
1096 {
1097         int rc = 0;
1098
1099 #ifdef TCP_OFFLOAD
1100         switch (cmd) {
1101         case MOD_LOAD:
1102                 rc = t4_tom_mod_load();
1103                 break;
1104
1105         case MOD_UNLOAD:
1106                 rc = t4_tom_mod_unload();
1107                 break;
1108
1109         default:
1110                 rc = EINVAL;
1111         }
1112 #else
1113         printf("t4_tom: compiled without TCP_OFFLOAD support.\n");
1114         rc = EOPNOTSUPP;
1115 #endif
1116         return (rc);
1117 }
1118
1119 static moduledata_t t4_tom_moddata= {
1120         "t4_tom",
1121         t4_tom_modevent,
1122         0
1123 };
1124
1125 MODULE_VERSION(t4_tom, 1);
1126 MODULE_DEPEND(t4_tom, toecore, 1, 1, 1);
1127 MODULE_DEPEND(t4_tom, t4nex, 1, 1, 1);
1128 DECLARE_MODULE(t4_tom, t4_tom_moddata, SI_SUB_EXEC, SI_ORDER_ANY);