]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/dev/cxgb/ulp/tom/cxgb_l2t.h
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / dev / cxgb / ulp / tom / cxgb_l2t.h
1 /**************************************************************************
2
3 Copyright (c) 2007-2008, Chelsio Inc.
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8
9  1. Redistributions of source code must retain the above copyright notice,
10     this list of conditions and the following disclaimer.
11
12  2. Neither the name of the Chelsio Corporation nor the names of its
13     contributors may be used to endorse or promote products derived from
14     this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 POSSIBILITY OF SUCH DAMAGE.
27
28 $FreeBSD$
29
30 ***************************************************************************/
31 #ifndef _CHELSIO_L2T_H
32 #define _CHELSIO_L2T_H
33
34 #include <ulp/toecore/cxgb_toedev.h>
35 #include <sys/lock.h>
36
37 #if __FreeBSD_version > 700000
38 #include <sys/rwlock.h>
39 #else
40 #define rwlock mtx
41 #define rw_wlock(x) mtx_lock((x))
42 #define rw_wunlock(x) mtx_unlock((x))
43 #define rw_rlock(x) mtx_lock((x))
44 #define rw_runlock(x) mtx_unlock((x))
45 #define rw_init(x, str) mtx_init((x), (str), NULL, MTX_DEF)
46 #define rw_destroy(x) mtx_destroy((x))
47 #endif
48
49 enum {
50         L2T_STATE_VALID,      /* entry is up to date */
51         L2T_STATE_STALE,      /* entry may be used but needs revalidation */
52         L2T_STATE_RESOLVING,  /* entry needs address resolution */
53         L2T_STATE_UNUSED      /* entry not in use */
54 };
55
56 /*
57  * Each L2T entry plays multiple roles.  First of all, it keeps state for the
58  * corresponding entry of the HW L2 table and maintains a queue of offload
59  * packets awaiting address resolution.  Second, it is a node of a hash table
60  * chain, where the nodes of the chain are linked together through their next
61  * pointer.  Finally, each node is a bucket of a hash table, pointing to the
62  * first element in its chain through its first pointer.
63  */
64 struct l2t_entry {
65         uint16_t state;               /* entry state */
66         uint16_t idx;                 /* entry index */
67         uint32_t addr;                /* dest IP address */
68         int ifindex;                  /* neighbor's net_device's ifindex */
69         uint16_t smt_idx;             /* SMT index */
70         uint16_t vlan;                /* VLAN TCI (id: bits 0-11, prio: 13-15 */
71         struct llentry *neigh;        /* associated neighbour */
72         struct l2t_entry *first;      /* start of hash chain */
73         struct l2t_entry *next;       /* next l2t_entry on chain */
74         struct mbuf *arpq_head;       /* queue of packets awaiting resolution */
75         struct mbuf *arpq_tail;
76         struct mtx lock;
77         volatile uint32_t refcnt;     /* entry reference count */
78         uint8_t dmac[6];              /* neighbour's MAC address */
79 };
80
81 struct l2t_data {
82         unsigned int nentries;      /* number of entries */
83         struct l2t_entry *rover;    /* starting point for next allocation */
84         volatile uint32_t nfree;    /* number of free entries */
85         struct rwlock lock;
86         struct l2t_entry l2tab[0];
87 };
88
89 typedef void (*arp_failure_handler_func)(struct t3cdev *dev,
90                                          struct mbuf *m);
91
92 typedef void (*opaque_arp_failure_handler_func)(void *dev,
93                                          struct mbuf *m);
94
95 /*
96  * Callback stored in an skb to handle address resolution failure.
97  */
98 struct l2t_mbuf_cb {
99         arp_failure_handler_func arp_failure_handler;
100 };
101
102 /*
103  * XXX 
104  */
105 #define L2T_MBUF_CB(skb) ((struct l2t_mbuf_cb *)(skb)->cb)
106
107
108 static __inline void set_arp_failure_handler(struct mbuf *m,
109                                            arp_failure_handler_func hnd)
110 {
111         m->m_pkthdr.header = (opaque_arp_failure_handler_func)hnd;
112
113 }
114
115 /*
116  * Getting to the L2 data from an offload device.
117  */
118 #define L2DATA(dev) ((dev)->l2opt)
119
120 void t3_l2e_free(struct l2t_data *d, struct l2t_entry *e);
121 void t3_l2t_update(struct t3cdev *dev, struct rtentry *rt, uint8_t *enaddr, struct sockaddr *sa);
122 struct l2t_entry *t3_l2t_get(struct t3cdev *dev, struct rtentry *neigh,
123     struct ifnet *ifp, struct sockaddr *sa);
124 int t3_l2t_send_slow(struct t3cdev *dev, struct mbuf *m,
125                      struct l2t_entry *e);
126 void t3_l2t_send_event(struct t3cdev *dev, struct l2t_entry *e);
127 struct l2t_data *t3_init_l2t(unsigned int l2t_capacity);
128 void t3_free_l2t(struct l2t_data *d);
129
130 #ifdef CONFIG_PROC_FS
131 int t3_l2t_proc_setup(struct proc_dir_entry *dir, struct l2t_data *d);
132 void t3_l2t_proc_free(struct proc_dir_entry *dir);
133 #else
134 #define l2t_proc_setup(dir, d) 0
135 #define l2t_proc_free(dir)
136 #endif
137
138 int cxgb_ofld_send(struct t3cdev *dev, struct mbuf *m);
139
140 static inline int l2t_send(struct t3cdev *dev, struct mbuf *m,
141                            struct l2t_entry *e)
142 {
143         if (__predict_true(e->state == L2T_STATE_VALID)) {
144                 return cxgb_ofld_send(dev, (struct mbuf *)m);
145         }
146         return t3_l2t_send_slow(dev, (struct mbuf *)m, e);
147 }
148
149 static inline void l2t_release(struct l2t_data *d, struct l2t_entry *e)
150 {
151         if (atomic_fetchadd_int(&e->refcnt, -1) == 1)
152                 t3_l2e_free(d, e);
153 }
154
155 static inline void l2t_hold(struct l2t_data *d, struct l2t_entry *e)
156 {
157         if (atomic_fetchadd_int(&e->refcnt, 1) == 1)  /* 0 -> 1 transition */
158                 atomic_add_int(&d->nfree, 1);
159 }
160
161 #endif