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