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
33
34
35#include <linux/skbuff.h>
36#include <linux/netdevice.h>
37#include <linux/if.h>
38#include <linux/if_vlan.h>
39#include <linux/jhash.h>
40#include <linux/module.h>
41#include <linux/debugfs.h>
42#include <linux/seq_file.h>
43#include <net/neighbour.h>
44#include "cxgb4.h"
45#include "l2t.h"
46#include "t4_msg.h"
47#include "t4fw_api.h"
48#include "t4_regs.h"
49#include "t4_values.h"
50
51
52#define SYNC_WR_S 12
53#define SYNC_WR_V(x) ((x) << SYNC_WR_S)
54#define SYNC_WR_F SYNC_WR_V(1)
55
56struct l2t_data {
57 unsigned int l2t_start;
58 unsigned int l2t_size;
59 rwlock_t lock;
60 atomic_t nfree;
61 struct l2t_entry *rover;
62 struct l2t_entry l2tab[0];
63};
64
65static inline unsigned int vlan_prio(const struct l2t_entry *e)
66{
67 return e->vlan >> VLAN_PRIO_SHIFT;
68}
69
70static inline void l2t_hold(struct l2t_data *d, struct l2t_entry *e)
71{
72 if (atomic_add_return(1, &e->refcnt) == 1)
73 atomic_dec(&d->nfree);
74}
75
76
77
78
79
80
81
82enum {
83 L2T_MIN_HASH_BUCKETS = 2,
84};
85
86static inline unsigned int arp_hash(struct l2t_data *d, const u32 *key,
87 int ifindex)
88{
89 unsigned int l2t_size_half = d->l2t_size / 2;
90
91 return jhash_2words(*key, ifindex, 0) % l2t_size_half;
92}
93
94static inline unsigned int ipv6_hash(struct l2t_data *d, const u32 *key,
95 int ifindex)
96{
97 unsigned int l2t_size_half = d->l2t_size / 2;
98 u32 xor = key[0] ^ key[1] ^ key[2] ^ key[3];
99
100 return (l2t_size_half +
101 (jhash_2words(xor, ifindex, 0) % l2t_size_half));
102}
103
104static unsigned int addr_hash(struct l2t_data *d, const u32 *addr,
105 int addr_len, int ifindex)
106{
107 return addr_len == 4 ? arp_hash(d, addr, ifindex) :
108 ipv6_hash(d, addr, ifindex);
109}
110
111
112
113
114
115
116
117
118static int addreq(const struct l2t_entry *e, const u32 *addr)
119{
120 if (e->v6)
121 return (e->addr[0] ^ addr[0]) | (e->addr[1] ^ addr[1]) |
122 (e->addr[2] ^ addr[2]) | (e->addr[3] ^ addr[3]);
123 return e->addr[0] ^ addr[0];
124}
125
126static void neigh_replace(struct l2t_entry *e, struct neighbour *n)
127{
128 neigh_hold(n);
129 if (e->neigh)
130 neigh_release(e->neigh);
131 e->neigh = n;
132}
133
134
135
136
137
138static int write_l2e(struct adapter *adap, struct l2t_entry *e, int sync)
139{
140 struct l2t_data *d = adap->l2t;
141 unsigned int l2t_idx = e->idx + d->l2t_start;
142 struct sk_buff *skb;
143 struct cpl_l2t_write_req *req;
144
145 skb = alloc_skb(sizeof(*req), GFP_ATOMIC);
146 if (!skb)
147 return -ENOMEM;
148
149 req = __skb_put(skb, sizeof(*req));
150 INIT_TP_WR(req, 0);
151
152 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_L2T_WRITE_REQ,
153 l2t_idx | (sync ? SYNC_WR_F : 0) |
154 TID_QID_V(adap->sge.fw_evtq.abs_id)));
155 req->params = htons(L2T_W_PORT_V(e->lport) | L2T_W_NOREPLY_V(!sync));
156 req->l2t_idx = htons(l2t_idx);
157 req->vlan = htons(e->vlan);
158 if (e->neigh && !(e->neigh->dev->flags & IFF_LOOPBACK))
159 memcpy(e->dmac, e->neigh->ha, sizeof(e->dmac));
160 memcpy(req->dst_mac, e->dmac, sizeof(req->dst_mac));
161
162 t4_mgmt_tx(adap, skb);
163
164 if (sync && e->state != L2T_STATE_SWITCHING)
165 e->state = L2T_STATE_SYNC_WRITE;
166 return 0;
167}
168
169
170
171
172
173static void send_pending(struct adapter *adap, struct l2t_entry *e)
174{
175 struct sk_buff *skb;
176
177 while ((skb = __skb_dequeue(&e->arpq)) != NULL)
178 t4_ofld_send(adap, skb);
179}
180
181
182
183
184
185
186void do_l2t_write_rpl(struct adapter *adap, const struct cpl_l2t_write_rpl *rpl)
187{
188 struct l2t_data *d = adap->l2t;
189 unsigned int tid = GET_TID(rpl);
190 unsigned int l2t_idx = tid % L2T_SIZE;
191
192 if (unlikely(rpl->status != CPL_ERR_NONE)) {
193 dev_err(adap->pdev_dev,
194 "Unexpected L2T_WRITE_RPL status %u for entry %u\n",
195 rpl->status, l2t_idx);
196 return;
197 }
198
199 if (tid & SYNC_WR_F) {
200 struct l2t_entry *e = &d->l2tab[l2t_idx - d->l2t_start];
201
202 spin_lock(&e->lock);
203 if (e->state != L2T_STATE_SWITCHING) {
204 send_pending(adap, e);
205 e->state = (e->neigh->nud_state & NUD_STALE) ?
206 L2T_STATE_STALE : L2T_STATE_VALID;
207 }
208 spin_unlock(&e->lock);
209 }
210}
211
212
213
214
215
216static inline void arpq_enqueue(struct l2t_entry *e, struct sk_buff *skb)
217{
218 __skb_queue_tail(&e->arpq, skb);
219}
220
221int cxgb4_l2t_send(struct net_device *dev, struct sk_buff *skb,
222 struct l2t_entry *e)
223{
224 struct adapter *adap = netdev2adap(dev);
225
226again:
227 switch (e->state) {
228 case L2T_STATE_STALE:
229 neigh_event_send(e->neigh, NULL);
230 spin_lock_bh(&e->lock);
231 if (e->state == L2T_STATE_STALE)
232 e->state = L2T_STATE_VALID;
233 spin_unlock_bh(&e->lock);
234 case L2T_STATE_VALID:
235 return t4_ofld_send(adap, skb);
236 case L2T_STATE_RESOLVING:
237 case L2T_STATE_SYNC_WRITE:
238 spin_lock_bh(&e->lock);
239 if (e->state != L2T_STATE_SYNC_WRITE &&
240 e->state != L2T_STATE_RESOLVING) {
241 spin_unlock_bh(&e->lock);
242 goto again;
243 }
244 arpq_enqueue(e, skb);
245 spin_unlock_bh(&e->lock);
246
247 if (e->state == L2T_STATE_RESOLVING &&
248 !neigh_event_send(e->neigh, NULL)) {
249 spin_lock_bh(&e->lock);
250 if (e->state == L2T_STATE_RESOLVING &&
251 !skb_queue_empty(&e->arpq))
252 write_l2e(adap, e, 1);
253 spin_unlock_bh(&e->lock);
254 }
255 }
256 return 0;
257}
258EXPORT_SYMBOL(cxgb4_l2t_send);
259
260
261
262
263static struct l2t_entry *alloc_l2e(struct l2t_data *d)
264{
265 struct l2t_entry *end, *e, **p;
266
267 if (!atomic_read(&d->nfree))
268 return NULL;
269
270
271 for (e = d->rover, end = &d->l2tab[d->l2t_size]; e != end; ++e)
272 if (atomic_read(&e->refcnt) == 0)
273 goto found;
274
275 for (e = d->l2tab; atomic_read(&e->refcnt); ++e)
276 ;
277found:
278 d->rover = e + 1;
279 atomic_dec(&d->nfree);
280
281
282
283
284
285 if (e->state < L2T_STATE_SWITCHING)
286 for (p = &d->l2tab[e->hash].first; *p; p = &(*p)->next)
287 if (*p == e) {
288 *p = e->next;
289 e->next = NULL;
290 break;
291 }
292
293 e->state = L2T_STATE_UNUSED;
294 return e;
295}
296
297static struct l2t_entry *find_or_alloc_l2e(struct l2t_data *d, u16 vlan,
298 u8 port, u8 *dmac)
299{
300 struct l2t_entry *end, *e, **p;
301 struct l2t_entry *first_free = NULL;
302
303 for (e = &d->l2tab[0], end = &d->l2tab[d->l2t_size]; e != end; ++e) {
304 if (atomic_read(&e->refcnt) == 0) {
305 if (!first_free)
306 first_free = e;
307 } else {
308 if (e->state == L2T_STATE_SWITCHING) {
309 if (ether_addr_equal(e->dmac, dmac) &&
310 (e->vlan == vlan) && (e->lport == port))
311 goto exists;
312 }
313 }
314 }
315
316 if (first_free) {
317 e = first_free;
318 goto found;
319 }
320
321 return NULL;
322
323found:
324
325
326
327 if (e->state < L2T_STATE_SWITCHING)
328 for (p = &d->l2tab[e->hash].first; *p; p = &(*p)->next)
329 if (*p == e) {
330 *p = e->next;
331 e->next = NULL;
332 break;
333 }
334 e->state = L2T_STATE_UNUSED;
335
336exists:
337 return e;
338}
339
340
341
342
343
344
345
346
347
348
349
350static void _t4_l2e_free(struct l2t_entry *e)
351{
352 struct l2t_data *d;
353 struct sk_buff *skb;
354
355 if (atomic_read(&e->refcnt) == 0) {
356 if (e->neigh) {
357 neigh_release(e->neigh);
358 e->neigh = NULL;
359 }
360 while ((skb = __skb_dequeue(&e->arpq)) != NULL)
361 kfree_skb(skb);
362 }
363
364 d = container_of(e, struct l2t_data, l2tab[e->idx]);
365 atomic_inc(&d->nfree);
366}
367
368
369static void t4_l2e_free(struct l2t_entry *e)
370{
371 struct l2t_data *d;
372 struct sk_buff *skb;
373
374 spin_lock_bh(&e->lock);
375 if (atomic_read(&e->refcnt) == 0) {
376 if (e->neigh) {
377 neigh_release(e->neigh);
378 e->neigh = NULL;
379 }
380 while ((skb = __skb_dequeue(&e->arpq)) != NULL)
381 kfree_skb(skb);
382 }
383 spin_unlock_bh(&e->lock);
384
385 d = container_of(e, struct l2t_data, l2tab[e->idx]);
386 atomic_inc(&d->nfree);
387}
388
389void cxgb4_l2t_release(struct l2t_entry *e)
390{
391 if (atomic_dec_and_test(&e->refcnt))
392 t4_l2e_free(e);
393}
394EXPORT_SYMBOL(cxgb4_l2t_release);
395
396
397
398
399
400static void reuse_entry(struct l2t_entry *e, struct neighbour *neigh)
401{
402 unsigned int nud_state;
403
404 spin_lock(&e->lock);
405 if (neigh != e->neigh)
406 neigh_replace(e, neigh);
407 nud_state = neigh->nud_state;
408 if (memcmp(e->dmac, neigh->ha, sizeof(e->dmac)) ||
409 !(nud_state & NUD_VALID))
410 e->state = L2T_STATE_RESOLVING;
411 else if (nud_state & NUD_CONNECTED)
412 e->state = L2T_STATE_VALID;
413 else
414 e->state = L2T_STATE_STALE;
415 spin_unlock(&e->lock);
416}
417
418struct l2t_entry *cxgb4_l2t_get(struct l2t_data *d, struct neighbour *neigh,
419 const struct net_device *physdev,
420 unsigned int priority)
421{
422 u8 lport;
423 u16 vlan;
424 struct l2t_entry *e;
425 unsigned int addr_len = neigh->tbl->key_len;
426 u32 *addr = (u32 *)neigh->primary_key;
427 int ifidx = neigh->dev->ifindex;
428 int hash = addr_hash(d, addr, addr_len, ifidx);
429
430 if (neigh->dev->flags & IFF_LOOPBACK)
431 lport = netdev2pinfo(physdev)->tx_chan + 4;
432 else
433 lport = netdev2pinfo(physdev)->lport;
434
435 if (is_vlan_dev(neigh->dev)) {
436 vlan = vlan_dev_vlan_id(neigh->dev);
437 vlan |= vlan_dev_get_egress_qos_mask(neigh->dev, priority);
438 } else {
439 vlan = VLAN_NONE;
440 }
441
442 write_lock_bh(&d->lock);
443 for (e = d->l2tab[hash].first; e; e = e->next)
444 if (!addreq(e, addr) && e->ifindex == ifidx &&
445 e->vlan == vlan && e->lport == lport) {
446 l2t_hold(d, e);
447 if (atomic_read(&e->refcnt) == 1)
448 reuse_entry(e, neigh);
449 goto done;
450 }
451
452
453 e = alloc_l2e(d);
454 if (e) {
455 spin_lock(&e->lock);
456 e->state = L2T_STATE_RESOLVING;
457 if (neigh->dev->flags & IFF_LOOPBACK)
458 memcpy(e->dmac, physdev->dev_addr, sizeof(e->dmac));
459 memcpy(e->addr, addr, addr_len);
460 e->ifindex = ifidx;
461 e->hash = hash;
462 e->lport = lport;
463 e->v6 = addr_len == 16;
464 atomic_set(&e->refcnt, 1);
465 neigh_replace(e, neigh);
466 e->vlan = vlan;
467 e->next = d->l2tab[hash].first;
468 d->l2tab[hash].first = e;
469 spin_unlock(&e->lock);
470 }
471done:
472 write_unlock_bh(&d->lock);
473 return e;
474}
475EXPORT_SYMBOL(cxgb4_l2t_get);
476
477u64 cxgb4_select_ntuple(struct net_device *dev,
478 const struct l2t_entry *l2t)
479{
480 struct adapter *adap = netdev2adap(dev);
481 struct tp_params *tp = &adap->params.tp;
482 u64 ntuple = 0;
483
484
485
486
487 if (tp->vlan_shift >= 0 && l2t->vlan != VLAN_NONE)
488 ntuple |= (u64)(FT_VLAN_VLD_F | l2t->vlan) << tp->vlan_shift;
489
490 if (tp->port_shift >= 0)
491 ntuple |= (u64)l2t->lport << tp->port_shift;
492
493 if (tp->protocol_shift >= 0)
494 ntuple |= (u64)IPPROTO_TCP << tp->protocol_shift;
495
496 if (tp->vnic_shift >= 0 && (tp->ingress_config & VNIC_F)) {
497 u32 viid = cxgb4_port_viid(dev);
498 u32 vf = FW_VIID_VIN_G(viid);
499 u32 pf = FW_VIID_PFN_G(viid);
500 u32 vld = FW_VIID_VIVLD_G(viid);
501
502 ntuple |= (u64)(FT_VNID_ID_VF_V(vf) |
503 FT_VNID_ID_PF_V(pf) |
504 FT_VNID_ID_VLD_V(vld)) << tp->vnic_shift;
505 }
506
507 return ntuple;
508}
509EXPORT_SYMBOL(cxgb4_select_ntuple);
510
511
512
513
514
515
516static void handle_failed_resolution(struct adapter *adap, struct l2t_entry *e)
517{
518 struct sk_buff *skb;
519
520 while ((skb = __skb_dequeue(&e->arpq)) != NULL) {
521 const struct l2t_skb_cb *cb = L2T_SKB_CB(skb);
522
523 spin_unlock(&e->lock);
524 if (cb->arp_err_handler)
525 cb->arp_err_handler(cb->handle, skb);
526 else
527 t4_ofld_send(adap, skb);
528 spin_lock(&e->lock);
529 }
530}
531
532
533
534
535
536void t4_l2t_update(struct adapter *adap, struct neighbour *neigh)
537{
538 struct l2t_entry *e;
539 struct sk_buff_head *arpq = NULL;
540 struct l2t_data *d = adap->l2t;
541 unsigned int addr_len = neigh->tbl->key_len;
542 u32 *addr = (u32 *) neigh->primary_key;
543 int ifidx = neigh->dev->ifindex;
544 int hash = addr_hash(d, addr, addr_len, ifidx);
545
546 read_lock_bh(&d->lock);
547 for (e = d->l2tab[hash].first; e; e = e->next)
548 if (!addreq(e, addr) && e->ifindex == ifidx) {
549 spin_lock(&e->lock);
550 if (atomic_read(&e->refcnt))
551 goto found;
552 spin_unlock(&e->lock);
553 break;
554 }
555 read_unlock_bh(&d->lock);
556 return;
557
558 found:
559 read_unlock(&d->lock);
560
561 if (neigh != e->neigh)
562 neigh_replace(e, neigh);
563
564 if (e->state == L2T_STATE_RESOLVING) {
565 if (neigh->nud_state & NUD_FAILED) {
566 arpq = &e->arpq;
567 } else if ((neigh->nud_state & (NUD_CONNECTED | NUD_STALE)) &&
568 !skb_queue_empty(&e->arpq)) {
569 write_l2e(adap, e, 1);
570 }
571 } else {
572 e->state = neigh->nud_state & NUD_CONNECTED ?
573 L2T_STATE_VALID : L2T_STATE_STALE;
574 if (memcmp(e->dmac, neigh->ha, sizeof(e->dmac)))
575 write_l2e(adap, e, 0);
576 }
577
578 if (arpq)
579 handle_failed_resolution(adap, e);
580 spin_unlock_bh(&e->lock);
581}
582
583
584
585
586
587struct l2t_entry *t4_l2t_alloc_switching(struct adapter *adap, u16 vlan,
588 u8 port, u8 *eth_addr)
589{
590 struct l2t_data *d = adap->l2t;
591 struct l2t_entry *e;
592 int ret;
593
594 write_lock_bh(&d->lock);
595 e = find_or_alloc_l2e(d, vlan, port, eth_addr);
596 if (e) {
597 spin_lock(&e->lock);
598 if (!atomic_read(&e->refcnt)) {
599 e->state = L2T_STATE_SWITCHING;
600 e->vlan = vlan;
601 e->lport = port;
602 ether_addr_copy(e->dmac, eth_addr);
603 atomic_set(&e->refcnt, 1);
604 ret = write_l2e(adap, e, 0);
605 if (ret < 0) {
606 _t4_l2e_free(e);
607 spin_unlock(&e->lock);
608 write_unlock_bh(&d->lock);
609 return NULL;
610 }
611 } else {
612 atomic_inc(&e->refcnt);
613 }
614
615 spin_unlock(&e->lock);
616 }
617 write_unlock_bh(&d->lock);
618 return e;
619}
620
621
622
623
624
625
626
627
628
629
630struct l2t_entry *cxgb4_l2t_alloc_switching(struct net_device *dev, u16 vlan,
631 u8 port, u8 *dmac)
632{
633 struct adapter *adap = netdev2adap(dev);
634
635 return t4_l2t_alloc_switching(adap, vlan, port, dmac);
636}
637EXPORT_SYMBOL(cxgb4_l2t_alloc_switching);
638
639struct l2t_data *t4_init_l2t(unsigned int l2t_start, unsigned int l2t_end)
640{
641 unsigned int l2t_size;
642 int i;
643 struct l2t_data *d;
644
645 if (l2t_start >= l2t_end || l2t_end >= L2T_SIZE)
646 return NULL;
647 l2t_size = l2t_end - l2t_start + 1;
648 if (l2t_size < L2T_MIN_HASH_BUCKETS)
649 return NULL;
650
651 d = kvzalloc(sizeof(*d) + l2t_size * sizeof(struct l2t_entry), GFP_KERNEL);
652 if (!d)
653 return NULL;
654
655 d->l2t_start = l2t_start;
656 d->l2t_size = l2t_size;
657
658 d->rover = d->l2tab;
659 atomic_set(&d->nfree, l2t_size);
660 rwlock_init(&d->lock);
661
662 for (i = 0; i < d->l2t_size; ++i) {
663 d->l2tab[i].idx = i;
664 d->l2tab[i].state = L2T_STATE_UNUSED;
665 spin_lock_init(&d->l2tab[i].lock);
666 atomic_set(&d->l2tab[i].refcnt, 0);
667 skb_queue_head_init(&d->l2tab[i].arpq);
668 }
669 return d;
670}
671
672static inline void *l2t_get_idx(struct seq_file *seq, loff_t pos)
673{
674 struct l2t_data *d = seq->private;
675
676 return pos >= d->l2t_size ? NULL : &d->l2tab[pos];
677}
678
679static void *l2t_seq_start(struct seq_file *seq, loff_t *pos)
680{
681 return *pos ? l2t_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
682}
683
684static void *l2t_seq_next(struct seq_file *seq, void *v, loff_t *pos)
685{
686 v = l2t_get_idx(seq, *pos);
687 if (v)
688 ++*pos;
689 return v;
690}
691
692static void l2t_seq_stop(struct seq_file *seq, void *v)
693{
694}
695
696static char l2e_state(const struct l2t_entry *e)
697{
698 switch (e->state) {
699 case L2T_STATE_VALID: return 'V';
700 case L2T_STATE_STALE: return 'S';
701 case L2T_STATE_SYNC_WRITE: return 'W';
702 case L2T_STATE_RESOLVING:
703 return skb_queue_empty(&e->arpq) ? 'R' : 'A';
704 case L2T_STATE_SWITCHING: return 'X';
705 default:
706 return 'U';
707 }
708}
709
710static int l2t_seq_show(struct seq_file *seq, void *v)
711{
712 if (v == SEQ_START_TOKEN)
713 seq_puts(seq, " Idx IP address "
714 "Ethernet address VLAN/P LP State Users Port\n");
715 else {
716 char ip[60];
717 struct l2t_data *d = seq->private;
718 struct l2t_entry *e = v;
719
720 spin_lock_bh(&e->lock);
721 if (e->state == L2T_STATE_SWITCHING)
722 ip[0] = '\0';
723 else
724 sprintf(ip, e->v6 ? "%pI6c" : "%pI4", e->addr);
725 seq_printf(seq, "%4u %-25s %17pM %4d %u %2u %c %5u %s\n",
726 e->idx + d->l2t_start, ip, e->dmac,
727 e->vlan & VLAN_VID_MASK, vlan_prio(e), e->lport,
728 l2e_state(e), atomic_read(&e->refcnt),
729 e->neigh ? e->neigh->dev->name : "");
730 spin_unlock_bh(&e->lock);
731 }
732 return 0;
733}
734
735static const struct seq_operations l2t_seq_ops = {
736 .start = l2t_seq_start,
737 .next = l2t_seq_next,
738 .stop = l2t_seq_stop,
739 .show = l2t_seq_show
740};
741
742static int l2t_seq_open(struct inode *inode, struct file *file)
743{
744 int rc = seq_open(file, &l2t_seq_ops);
745
746 if (!rc) {
747 struct adapter *adap = inode->i_private;
748 struct seq_file *seq = file->private_data;
749
750 seq->private = adap->l2t;
751 }
752 return rc;
753}
754
755const struct file_operations t4_l2t_fops = {
756 .owner = THIS_MODULE,
757 .open = l2t_seq_open,
758 .read = seq_read,
759 .llseek = seq_lseek,
760 .release = seq_release,
761};
762