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