1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32#include <linux/skbuff.h>
33#include <linux/netdevice.h>
34#include <linux/if.h>
35#include <linux/if_vlan.h>
36#include <linux/jhash.h>
37#include <net/neighbour.h>
38#include "common.h"
39#include "t3cdev.h"
40#include "cxgb3_defs.h"
41#include "l2t.h"
42#include "t3_cpl.h"
43#include "firmware_exports.h"
44
45#define VLAN_NONE 0xfff
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61static inline unsigned int vlan_prio(const struct l2t_entry *e)
62{
63 return e->vlan >> 13;
64}
65
66static inline unsigned int arp_hash(u32 key, int ifindex,
67 const struct l2t_data *d)
68{
69 return jhash_2words(key, ifindex, 0) & (d->nentries - 1);
70}
71
72static inline void neigh_replace(struct l2t_entry *e, struct neighbour *n)
73{
74 neigh_hold(n);
75 if (e->neigh)
76 neigh_release(e->neigh);
77 e->neigh = n;
78}
79
80
81
82
83
84
85static int setup_l2e_send_pending(struct t3cdev *dev, struct sk_buff *skb,
86 struct l2t_entry *e)
87{
88 struct cpl_l2t_write_req *req;
89 struct sk_buff *tmp;
90
91 if (!skb) {
92 skb = alloc_skb(sizeof(*req), GFP_ATOMIC);
93 if (!skb)
94 return -ENOMEM;
95 }
96
97 req = (struct cpl_l2t_write_req *)__skb_put(skb, sizeof(*req));
98 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
99 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_L2T_WRITE_REQ, e->idx));
100 req->params = htonl(V_L2T_W_IDX(e->idx) | V_L2T_W_IFF(e->smt_idx) |
101 V_L2T_W_VLAN(e->vlan & VLAN_VID_MASK) |
102 V_L2T_W_PRIO(vlan_prio(e)));
103 memcpy(e->dmac, e->neigh->ha, sizeof(e->dmac));
104 memcpy(req->dst_mac, e->dmac, sizeof(req->dst_mac));
105 skb->priority = CPL_PRIORITY_CONTROL;
106 cxgb3_ofld_send(dev, skb);
107
108 skb_queue_walk_safe(&e->arpq, skb, tmp) {
109 __skb_unlink(skb, &e->arpq);
110 cxgb3_ofld_send(dev, skb);
111 }
112 e->state = L2T_STATE_VALID;
113
114 return 0;
115}
116
117
118
119
120
121static inline void arpq_enqueue(struct l2t_entry *e, struct sk_buff *skb)
122{
123 __skb_queue_tail(&e->arpq, skb);
124}
125
126int t3_l2t_send_slow(struct t3cdev *dev, struct sk_buff *skb,
127 struct l2t_entry *e)
128{
129again:
130 switch (e->state) {
131 case L2T_STATE_STALE:
132 neigh_event_send(e->neigh, NULL);
133 spin_lock_bh(&e->lock);
134 if (e->state == L2T_STATE_STALE)
135 e->state = L2T_STATE_VALID;
136 spin_unlock_bh(&e->lock);
137 case L2T_STATE_VALID:
138 return cxgb3_ofld_send(dev, skb);
139 case L2T_STATE_RESOLVING:
140 spin_lock_bh(&e->lock);
141 if (e->state != L2T_STATE_RESOLVING) {
142
143 spin_unlock_bh(&e->lock);
144 goto again;
145 }
146 arpq_enqueue(e, skb);
147 spin_unlock_bh(&e->lock);
148
149
150
151
152
153
154
155
156
157 if (!neigh_event_send(e->neigh, NULL)) {
158 skb = alloc_skb(sizeof(struct cpl_l2t_write_req),
159 GFP_ATOMIC);
160 if (!skb)
161 break;
162
163 spin_lock_bh(&e->lock);
164 if (!skb_queue_empty(&e->arpq))
165 setup_l2e_send_pending(dev, skb, e);
166 else
167 __kfree_skb(skb);
168 spin_unlock_bh(&e->lock);
169 }
170 }
171 return 0;
172}
173
174EXPORT_SYMBOL(t3_l2t_send_slow);
175
176void t3_l2t_send_event(struct t3cdev *dev, struct l2t_entry *e)
177{
178again:
179 switch (e->state) {
180 case L2T_STATE_STALE:
181 neigh_event_send(e->neigh, NULL);
182 spin_lock_bh(&e->lock);
183 if (e->state == L2T_STATE_STALE) {
184 e->state = L2T_STATE_VALID;
185 }
186 spin_unlock_bh(&e->lock);
187 return;
188 case L2T_STATE_VALID:
189 return;
190 case L2T_STATE_RESOLVING:
191 spin_lock_bh(&e->lock);
192 if (e->state != L2T_STATE_RESOLVING) {
193
194 spin_unlock_bh(&e->lock);
195 goto again;
196 }
197 spin_unlock_bh(&e->lock);
198
199
200
201
202
203
204
205
206
207 neigh_event_send(e->neigh, NULL);
208 }
209 return;
210}
211
212EXPORT_SYMBOL(t3_l2t_send_event);
213
214
215
216
217static struct l2t_entry *alloc_l2e(struct l2t_data *d)
218{
219 struct l2t_entry *end, *e, **p;
220
221 if (!atomic_read(&d->nfree))
222 return NULL;
223
224
225 for (e = d->rover, end = &d->l2tab[d->nentries]; e != end; ++e)
226 if (atomic_read(&e->refcnt) == 0)
227 goto found;
228
229 for (e = &d->l2tab[1]; atomic_read(&e->refcnt); ++e) ;
230found:
231 d->rover = e + 1;
232 atomic_dec(&d->nfree);
233
234
235
236
237
238 if (e->state != L2T_STATE_UNUSED) {
239 int hash = arp_hash(e->addr, e->ifindex, d);
240
241 for (p = &d->l2tab[hash].first; *p; p = &(*p)->next)
242 if (*p == e) {
243 *p = e->next;
244 break;
245 }
246 e->state = L2T_STATE_UNUSED;
247 }
248 return e;
249}
250
251
252
253
254
255
256
257
258
259
260
261
262void t3_l2e_free(struct l2t_data *d, struct l2t_entry *e)
263{
264 spin_lock_bh(&e->lock);
265 if (atomic_read(&e->refcnt) == 0) {
266 if (e->neigh) {
267 neigh_release(e->neigh);
268 e->neigh = NULL;
269 }
270 }
271 spin_unlock_bh(&e->lock);
272 atomic_inc(&d->nfree);
273}
274
275EXPORT_SYMBOL(t3_l2e_free);
276
277
278
279
280
281static inline void reuse_entry(struct l2t_entry *e, struct neighbour *neigh)
282{
283 unsigned int nud_state;
284
285 spin_lock(&e->lock);
286
287 if (neigh != e->neigh)
288 neigh_replace(e, neigh);
289 nud_state = neigh->nud_state;
290 if (memcmp(e->dmac, neigh->ha, sizeof(e->dmac)) ||
291 !(nud_state & NUD_VALID))
292 e->state = L2T_STATE_RESOLVING;
293 else if (nud_state & NUD_CONNECTED)
294 e->state = L2T_STATE_VALID;
295 else
296 e->state = L2T_STATE_STALE;
297 spin_unlock(&e->lock);
298}
299
300struct l2t_entry *t3_l2t_get(struct t3cdev *cdev, struct neighbour *neigh,
301 struct net_device *dev)
302{
303 struct l2t_entry *e;
304 struct l2t_data *d = L2DATA(cdev);
305 u32 addr = *(u32 *) neigh->primary_key;
306 int ifidx = neigh->dev->ifindex;
307 int hash = arp_hash(addr, ifidx, d);
308 struct port_info *p = netdev_priv(dev);
309 int smt_idx = p->port_id;
310
311 write_lock_bh(&d->lock);
312 for (e = d->l2tab[hash].first; e; e = e->next)
313 if (e->addr == addr && e->ifindex == ifidx &&
314 e->smt_idx == smt_idx) {
315 l2t_hold(d, e);
316 if (atomic_read(&e->refcnt) == 1)
317 reuse_entry(e, neigh);
318 goto done;
319 }
320
321
322 e = alloc_l2e(d);
323 if (e) {
324 spin_lock(&e->lock);
325 e->next = d->l2tab[hash].first;
326 d->l2tab[hash].first = e;
327 e->state = L2T_STATE_RESOLVING;
328 e->addr = addr;
329 e->ifindex = ifidx;
330 e->smt_idx = smt_idx;
331 atomic_set(&e->refcnt, 1);
332 neigh_replace(e, neigh);
333 if (neigh->dev->priv_flags & IFF_802_1Q_VLAN)
334 e->vlan = vlan_dev_vlan_id(neigh->dev);
335 else
336 e->vlan = VLAN_NONE;
337 spin_unlock(&e->lock);
338 }
339done:
340 write_unlock_bh(&d->lock);
341 return e;
342}
343
344EXPORT_SYMBOL(t3_l2t_get);
345
346
347
348
349
350
351
352
353
354static void handle_failed_resolution(struct t3cdev *dev, struct sk_buff_head *arpq)
355{
356 struct sk_buff *skb, *tmp;
357
358 skb_queue_walk_safe(arpq, skb, tmp) {
359 struct l2t_skb_cb *cb = L2T_SKB_CB(skb);
360
361 __skb_unlink(skb, arpq);
362 if (cb->arp_failure_handler)
363 cb->arp_failure_handler(dev, skb);
364 else
365 cxgb3_ofld_send(dev, skb);
366 }
367}
368
369
370
371
372
373void t3_l2t_update(struct t3cdev *dev, struct neighbour *neigh)
374{
375 struct sk_buff_head arpq;
376 struct l2t_entry *e;
377 struct l2t_data *d = L2DATA(dev);
378 u32 addr = *(u32 *) neigh->primary_key;
379 int ifidx = neigh->dev->ifindex;
380 int hash = arp_hash(addr, ifidx, d);
381
382 read_lock_bh(&d->lock);
383 for (e = d->l2tab[hash].first; e; e = e->next)
384 if (e->addr == addr && e->ifindex == ifidx) {
385 spin_lock(&e->lock);
386 goto found;
387 }
388 read_unlock_bh(&d->lock);
389 return;
390
391found:
392 __skb_queue_head_init(&arpq);
393
394 read_unlock(&d->lock);
395 if (atomic_read(&e->refcnt)) {
396 if (neigh != e->neigh)
397 neigh_replace(e, neigh);
398
399 if (e->state == L2T_STATE_RESOLVING) {
400 if (neigh->nud_state & NUD_FAILED) {
401 skb_queue_splice_init(&e->arpq, &arpq);
402 } else if (neigh->nud_state & (NUD_CONNECTED|NUD_STALE))
403 setup_l2e_send_pending(dev, NULL, e);
404 } else {
405 e->state = neigh->nud_state & NUD_CONNECTED ?
406 L2T_STATE_VALID : L2T_STATE_STALE;
407 if (memcmp(e->dmac, neigh->ha, 6))
408 setup_l2e_send_pending(dev, NULL, e);
409 }
410 }
411 spin_unlock_bh(&e->lock);
412
413 if (!skb_queue_empty(&arpq))
414 handle_failed_resolution(dev, &arpq);
415}
416
417struct l2t_data *t3_init_l2t(unsigned int l2t_capacity)
418{
419 struct l2t_data *d;
420 int i, size = sizeof(*d) + l2t_capacity * sizeof(struct l2t_entry);
421
422 d = cxgb_alloc_mem(size);
423 if (!d)
424 return NULL;
425
426 d->nentries = l2t_capacity;
427 d->rover = &d->l2tab[1];
428 atomic_set(&d->nfree, l2t_capacity - 1);
429 rwlock_init(&d->lock);
430
431 for (i = 0; i < l2t_capacity; ++i) {
432 d->l2tab[i].idx = i;
433 d->l2tab[i].state = L2T_STATE_UNUSED;
434 __skb_queue_head_init(&d->l2tab[i].arpq);
435 spin_lock_init(&d->l2tab[i].lock);
436 atomic_set(&d->l2tab[i].refcnt, 0);
437 }
438 return d;
439}
440
441void t3_free_l2t(struct l2t_data *d)
442{
443 cxgb_free_mem(d);
444}
445
446