cxgb4: Use t4_mgmt_tx() API for sending write l2t request ctrl packets.
[cascardo/linux.git] / drivers / net / ethernet / chelsio / cxgb4 / l2t.c
1 /*
2  * This file is part of the Chelsio T4 Ethernet driver for Linux.
3  *
4  * Copyright (c) 2003-2014 Chelsio Communications, Inc. All rights reserved.
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenIB.org BSD license below:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
20  *      - Redistributions in binary form must reproduce the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer in the documentation and/or other materials
23  *        provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
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 #define VLAN_NONE 0xfff
52
53 /* identifies sync vs async L2T_WRITE_REQs */
54 #define SYNC_WR_S    12
55 #define SYNC_WR_V(x) ((x) << SYNC_WR_S)
56 #define SYNC_WR_F    SYNC_WR_V(1)
57
58 struct l2t_data {
59         unsigned int l2t_start;     /* start index of our piece of the L2T */
60         unsigned int l2t_size;      /* number of entries in l2tab */
61         rwlock_t lock;
62         atomic_t nfree;             /* number of free entries */
63         struct l2t_entry *rover;    /* starting point for next allocation */
64         struct l2t_entry l2tab[0];  /* MUST BE LAST */
65 };
66
67 static inline unsigned int vlan_prio(const struct l2t_entry *e)
68 {
69         return e->vlan >> VLAN_PRIO_SHIFT;
70 }
71
72 static inline void l2t_hold(struct l2t_data *d, struct l2t_entry *e)
73 {
74         if (atomic_add_return(1, &e->refcnt) == 1)  /* 0 -> 1 transition */
75                 atomic_dec(&d->nfree);
76 }
77
78 /*
79  * To avoid having to check address families we do not allow v4 and v6
80  * neighbors to be on the same hash chain.  We keep v4 entries in the first
81  * half of available hash buckets and v6 in the second.  We need at least two
82  * entries in our L2T for this scheme to work.
83  */
84 enum {
85         L2T_MIN_HASH_BUCKETS = 2,
86 };
87
88 static inline unsigned int arp_hash(struct l2t_data *d, const u32 *key,
89                                     int ifindex)
90 {
91         unsigned int l2t_size_half = d->l2t_size / 2;
92
93         return jhash_2words(*key, ifindex, 0) % l2t_size_half;
94 }
95
96 static inline unsigned int ipv6_hash(struct l2t_data *d, const u32 *key,
97                                      int ifindex)
98 {
99         unsigned int l2t_size_half = d->l2t_size / 2;
100         u32 xor = key[0] ^ key[1] ^ key[2] ^ key[3];
101
102         return (l2t_size_half +
103                 (jhash_2words(xor, ifindex, 0) % l2t_size_half));
104 }
105
106 static unsigned int addr_hash(struct l2t_data *d, const u32 *addr,
107                               int addr_len, int ifindex)
108 {
109         return addr_len == 4 ? arp_hash(d, addr, ifindex) :
110                                ipv6_hash(d, addr, ifindex);
111 }
112
113 /*
114  * Checks if an L2T entry is for the given IP/IPv6 address.  It does not check
115  * whether the L2T entry and the address are of the same address family.
116  * Callers ensure an address is only checked against L2T entries of the same
117  * family, something made trivial by the separation of IP and IPv6 hash chains
118  * mentioned above.  Returns 0 if there's a match,
119  */
120 static int addreq(const struct l2t_entry *e, const u32 *addr)
121 {
122         if (e->v6)
123                 return (e->addr[0] ^ addr[0]) | (e->addr[1] ^ addr[1]) |
124                        (e->addr[2] ^ addr[2]) | (e->addr[3] ^ addr[3]);
125         return e->addr[0] ^ addr[0];
126 }
127
128 static void neigh_replace(struct l2t_entry *e, struct neighbour *n)
129 {
130         neigh_hold(n);
131         if (e->neigh)
132                 neigh_release(e->neigh);
133         e->neigh = n;
134 }
135
136 /*
137  * Write an L2T entry.  Must be called with the entry locked.
138  * The write may be synchronous or asynchronous.
139  */
140 static int write_l2e(struct adapter *adap, struct l2t_entry *e, int sync)
141 {
142         struct l2t_data *d = adap->l2t;
143         unsigned int l2t_idx = e->idx + d->l2t_start;
144         struct sk_buff *skb;
145         struct cpl_l2t_write_req *req;
146
147         skb = alloc_skb(sizeof(*req), GFP_ATOMIC);
148         if (!skb)
149                 return -ENOMEM;
150
151         req = (struct cpl_l2t_write_req *)__skb_put(skb, sizeof(*req));
152         INIT_TP_WR(req, 0);
153
154         OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_L2T_WRITE_REQ,
155                                         l2t_idx | (sync ? SYNC_WR_F : 0) |
156                                         TID_QID_V(adap->sge.fw_evtq.abs_id)));
157         req->params = htons(L2T_W_PORT_V(e->lport) | L2T_W_NOREPLY_V(!sync));
158         req->l2t_idx = htons(l2t_idx);
159         req->vlan = htons(e->vlan);
160         if (e->neigh && !(e->neigh->dev->flags & IFF_LOOPBACK))
161                 memcpy(e->dmac, e->neigh->ha, sizeof(e->dmac));
162         memcpy(req->dst_mac, e->dmac, sizeof(req->dst_mac));
163
164         t4_mgmt_tx(adap, skb);
165
166         if (sync && e->state != L2T_STATE_SWITCHING)
167                 e->state = L2T_STATE_SYNC_WRITE;
168         return 0;
169 }
170
171 /*
172  * Send packets waiting in an L2T entry's ARP queue.  Must be called with the
173  * entry locked.
174  */
175 static void send_pending(struct adapter *adap, struct l2t_entry *e)
176 {
177         while (e->arpq_head) {
178                 struct sk_buff *skb = e->arpq_head;
179
180                 e->arpq_head = skb->next;
181                 skb->next = NULL;
182                 t4_ofld_send(adap, skb);
183         }
184         e->arpq_tail = NULL;
185 }
186
187 /*
188  * Process a CPL_L2T_WRITE_RPL.  Wake up the ARP queue if it completes a
189  * synchronous L2T_WRITE.  Note that the TID in the reply is really the L2T
190  * index it refers to.
191  */
192 void do_l2t_write_rpl(struct adapter *adap, const struct cpl_l2t_write_rpl *rpl)
193 {
194         struct l2t_data *d = adap->l2t;
195         unsigned int tid = GET_TID(rpl);
196         unsigned int l2t_idx = tid % L2T_SIZE;
197
198         if (unlikely(rpl->status != CPL_ERR_NONE)) {
199                 dev_err(adap->pdev_dev,
200                         "Unexpected L2T_WRITE_RPL status %u for entry %u\n",
201                         rpl->status, l2t_idx);
202                 return;
203         }
204
205         if (tid & SYNC_WR_F) {
206                 struct l2t_entry *e = &d->l2tab[l2t_idx - d->l2t_start];
207
208                 spin_lock(&e->lock);
209                 if (e->state != L2T_STATE_SWITCHING) {
210                         send_pending(adap, e);
211                         e->state = (e->neigh->nud_state & NUD_STALE) ?
212                                         L2T_STATE_STALE : L2T_STATE_VALID;
213                 }
214                 spin_unlock(&e->lock);
215         }
216 }
217
218 /*
219  * Add a packet to an L2T entry's queue of packets awaiting resolution.
220  * Must be called with the entry's lock held.
221  */
222 static inline void arpq_enqueue(struct l2t_entry *e, struct sk_buff *skb)
223 {
224         skb->next = NULL;
225         if (e->arpq_head)
226                 e->arpq_tail->next = skb;
227         else
228                 e->arpq_head = skb;
229         e->arpq_tail = skb;
230 }
231
232 int cxgb4_l2t_send(struct net_device *dev, struct sk_buff *skb,
233                    struct l2t_entry *e)
234 {
235         struct adapter *adap = netdev2adap(dev);
236
237 again:
238         switch (e->state) {
239         case L2T_STATE_STALE:     /* entry is stale, kick off revalidation */
240                 neigh_event_send(e->neigh, NULL);
241                 spin_lock_bh(&e->lock);
242                 if (e->state == L2T_STATE_STALE)
243                         e->state = L2T_STATE_VALID;
244                 spin_unlock_bh(&e->lock);
245         case L2T_STATE_VALID:     /* fast-path, send the packet on */
246                 return t4_ofld_send(adap, skb);
247         case L2T_STATE_RESOLVING:
248         case L2T_STATE_SYNC_WRITE:
249                 spin_lock_bh(&e->lock);
250                 if (e->state != L2T_STATE_SYNC_WRITE &&
251                     e->state != L2T_STATE_RESOLVING) {
252                         spin_unlock_bh(&e->lock);
253                         goto again;
254                 }
255                 arpq_enqueue(e, skb);
256                 spin_unlock_bh(&e->lock);
257
258                 if (e->state == L2T_STATE_RESOLVING &&
259                     !neigh_event_send(e->neigh, NULL)) {
260                         spin_lock_bh(&e->lock);
261                         if (e->state == L2T_STATE_RESOLVING && e->arpq_head)
262                                 write_l2e(adap, e, 1);
263                         spin_unlock_bh(&e->lock);
264                 }
265         }
266         return 0;
267 }
268 EXPORT_SYMBOL(cxgb4_l2t_send);
269
270 /*
271  * Allocate a free L2T entry.  Must be called with l2t_data.lock held.
272  */
273 static struct l2t_entry *alloc_l2e(struct l2t_data *d)
274 {
275         struct l2t_entry *end, *e, **p;
276
277         if (!atomic_read(&d->nfree))
278                 return NULL;
279
280         /* there's definitely a free entry */
281         for (e = d->rover, end = &d->l2tab[d->l2t_size]; e != end; ++e)
282                 if (atomic_read(&e->refcnt) == 0)
283                         goto found;
284
285         for (e = d->l2tab; atomic_read(&e->refcnt); ++e)
286                 ;
287 found:
288         d->rover = e + 1;
289         atomic_dec(&d->nfree);
290
291         /*
292          * The entry we found may be an inactive entry that is
293          * presently in the hash table.  We need to remove it.
294          */
295         if (e->state < L2T_STATE_SWITCHING)
296                 for (p = &d->l2tab[e->hash].first; *p; p = &(*p)->next)
297                         if (*p == e) {
298                                 *p = e->next;
299                                 e->next = NULL;
300                                 break;
301                         }
302
303         e->state = L2T_STATE_UNUSED;
304         return e;
305 }
306
307 static struct l2t_entry *find_or_alloc_l2e(struct l2t_data *d, u16 vlan,
308                                            u8 port, u8 *dmac)
309 {
310         struct l2t_entry *end, *e, **p;
311         struct l2t_entry *first_free = NULL;
312
313         for (e = &d->l2tab[0], end = &d->l2tab[d->l2t_size]; e != end; ++e) {
314                 if (atomic_read(&e->refcnt) == 0) {
315                         if (!first_free)
316                                 first_free = e;
317                 } else {
318                         if (e->state == L2T_STATE_SWITCHING) {
319                                 if (ether_addr_equal(e->dmac, dmac) &&
320                                     (e->vlan == vlan) && (e->lport == port))
321                                         goto exists;
322                         }
323                 }
324         }
325
326         if (first_free) {
327                 e = first_free;
328                 goto found;
329         }
330
331         return NULL;
332
333 found:
334         /* The entry we found may be an inactive entry that is
335          * presently in the hash table.  We need to remove it.
336          */
337         if (e->state < L2T_STATE_SWITCHING)
338                 for (p = &d->l2tab[e->hash].first; *p; p = &(*p)->next)
339                         if (*p == e) {
340                                 *p = e->next;
341                                 e->next = NULL;
342                                 break;
343                         }
344         e->state = L2T_STATE_UNUSED;
345
346 exists:
347         return e;
348 }
349
350 /* Called when an L2T entry has no more users.  The entry is left in the hash
351  * table since it is likely to be reused but we also bump nfree to indicate
352  * that the entry can be reallocated for a different neighbor.  We also drop
353  * the existing neighbor reference in case the neighbor is going away and is
354  * waiting on our reference.
355  *
356  * Because entries can be reallocated to other neighbors once their ref count
357  * drops to 0 we need to take the entry's lock to avoid races with a new
358  * incarnation.
359  */
360 static void _t4_l2e_free(struct l2t_entry *e)
361 {
362         struct l2t_data *d;
363
364         if (atomic_read(&e->refcnt) == 0) {  /* hasn't been recycled */
365                 if (e->neigh) {
366                         neigh_release(e->neigh);
367                         e->neigh = NULL;
368                 }
369                 while (e->arpq_head) {
370                         struct sk_buff *skb = e->arpq_head;
371
372                         e->arpq_head = skb->next;
373                         kfree_skb(skb);
374                 }
375                 e->arpq_tail = NULL;
376         }
377
378         d = container_of(e, struct l2t_data, l2tab[e->idx]);
379         atomic_inc(&d->nfree);
380 }
381
382 /* Locked version of _t4_l2e_free */
383 static void t4_l2e_free(struct l2t_entry *e)
384 {
385         struct l2t_data *d;
386
387         spin_lock_bh(&e->lock);
388         if (atomic_read(&e->refcnt) == 0) {  /* hasn't been recycled */
389                 if (e->neigh) {
390                         neigh_release(e->neigh);
391                         e->neigh = NULL;
392                 }
393                 while (e->arpq_head) {
394                         struct sk_buff *skb = e->arpq_head;
395
396                         e->arpq_head = skb->next;
397                         kfree_skb(skb);
398                 }
399                 e->arpq_tail = NULL;
400         }
401         spin_unlock_bh(&e->lock);
402
403         d = container_of(e, struct l2t_data, l2tab[e->idx]);
404         atomic_inc(&d->nfree);
405 }
406
407 void cxgb4_l2t_release(struct l2t_entry *e)
408 {
409         if (atomic_dec_and_test(&e->refcnt))
410                 t4_l2e_free(e);
411 }
412 EXPORT_SYMBOL(cxgb4_l2t_release);
413
414 /*
415  * Update an L2T entry that was previously used for the same next hop as neigh.
416  * Must be called with softirqs disabled.
417  */
418 static void reuse_entry(struct l2t_entry *e, struct neighbour *neigh)
419 {
420         unsigned int nud_state;
421
422         spin_lock(&e->lock);                /* avoid race with t4_l2t_free */
423         if (neigh != e->neigh)
424                 neigh_replace(e, neigh);
425         nud_state = neigh->nud_state;
426         if (memcmp(e->dmac, neigh->ha, sizeof(e->dmac)) ||
427             !(nud_state & NUD_VALID))
428                 e->state = L2T_STATE_RESOLVING;
429         else if (nud_state & NUD_CONNECTED)
430                 e->state = L2T_STATE_VALID;
431         else
432                 e->state = L2T_STATE_STALE;
433         spin_unlock(&e->lock);
434 }
435
436 struct l2t_entry *cxgb4_l2t_get(struct l2t_data *d, struct neighbour *neigh,
437                                 const struct net_device *physdev,
438                                 unsigned int priority)
439 {
440         u8 lport;
441         u16 vlan;
442         struct l2t_entry *e;
443         int addr_len = neigh->tbl->key_len;
444         u32 *addr = (u32 *)neigh->primary_key;
445         int ifidx = neigh->dev->ifindex;
446         int hash = addr_hash(d, addr, addr_len, ifidx);
447
448         if (neigh->dev->flags & IFF_LOOPBACK)
449                 lport = netdev2pinfo(physdev)->tx_chan + 4;
450         else
451                 lport = netdev2pinfo(physdev)->lport;
452
453         if (neigh->dev->priv_flags & IFF_802_1Q_VLAN)
454                 vlan = vlan_dev_vlan_id(neigh->dev);
455         else
456                 vlan = VLAN_NONE;
457
458         write_lock_bh(&d->lock);
459         for (e = d->l2tab[hash].first; e; e = e->next)
460                 if (!addreq(e, addr) && e->ifindex == ifidx &&
461                     e->vlan == vlan && e->lport == lport) {
462                         l2t_hold(d, e);
463                         if (atomic_read(&e->refcnt) == 1)
464                                 reuse_entry(e, neigh);
465                         goto done;
466                 }
467
468         /* Need to allocate a new entry */
469         e = alloc_l2e(d);
470         if (e) {
471                 spin_lock(&e->lock);          /* avoid race with t4_l2t_free */
472                 e->state = L2T_STATE_RESOLVING;
473                 if (neigh->dev->flags & IFF_LOOPBACK)
474                         memcpy(e->dmac, physdev->dev_addr, sizeof(e->dmac));
475                 memcpy(e->addr, addr, addr_len);
476                 e->ifindex = ifidx;
477                 e->hash = hash;
478                 e->lport = lport;
479                 e->v6 = addr_len == 16;
480                 atomic_set(&e->refcnt, 1);
481                 neigh_replace(e, neigh);
482                 e->vlan = vlan;
483                 e->next = d->l2tab[hash].first;
484                 d->l2tab[hash].first = e;
485                 spin_unlock(&e->lock);
486         }
487 done:
488         write_unlock_bh(&d->lock);
489         return e;
490 }
491 EXPORT_SYMBOL(cxgb4_l2t_get);
492
493 u64 cxgb4_select_ntuple(struct net_device *dev,
494                         const struct l2t_entry *l2t)
495 {
496         struct adapter *adap = netdev2adap(dev);
497         struct tp_params *tp = &adap->params.tp;
498         u64 ntuple = 0;
499
500         /* Initialize each of the fields which we care about which are present
501          * in the Compressed Filter Tuple.
502          */
503         if (tp->vlan_shift >= 0 && l2t->vlan != VLAN_NONE)
504                 ntuple |= (u64)(FT_VLAN_VLD_F | l2t->vlan) << tp->vlan_shift;
505
506         if (tp->port_shift >= 0)
507                 ntuple |= (u64)l2t->lport << tp->port_shift;
508
509         if (tp->protocol_shift >= 0)
510                 ntuple |= (u64)IPPROTO_TCP << tp->protocol_shift;
511
512         if (tp->vnic_shift >= 0) {
513                 u32 viid = cxgb4_port_viid(dev);
514                 u32 vf = FW_VIID_VIN_G(viid);
515                 u32 pf = FW_VIID_PFN_G(viid);
516                 u32 vld = FW_VIID_VIVLD_G(viid);
517
518                 ntuple |= (u64)(FT_VNID_ID_VF_V(vf) |
519                                 FT_VNID_ID_PF_V(pf) |
520                                 FT_VNID_ID_VLD_V(vld)) << tp->vnic_shift;
521         }
522
523         return ntuple;
524 }
525 EXPORT_SYMBOL(cxgb4_select_ntuple);
526
527 /*
528  * Called when address resolution fails for an L2T entry to handle packets
529  * on the arpq head.  If a packet specifies a failure handler it is invoked,
530  * otherwise the packet is sent to the device.
531  */
532 static void handle_failed_resolution(struct adapter *adap, struct sk_buff *arpq)
533 {
534         while (arpq) {
535                 struct sk_buff *skb = arpq;
536                 const struct l2t_skb_cb *cb = L2T_SKB_CB(skb);
537
538                 arpq = skb->next;
539                 skb->next = NULL;
540                 if (cb->arp_err_handler)
541                         cb->arp_err_handler(cb->handle, skb);
542                 else
543                         t4_ofld_send(adap, skb);
544         }
545 }
546
547 /*
548  * Called when the host's neighbor layer makes a change to some entry that is
549  * loaded into the HW L2 table.
550  */
551 void t4_l2t_update(struct adapter *adap, struct neighbour *neigh)
552 {
553         struct l2t_entry *e;
554         struct sk_buff *arpq = NULL;
555         struct l2t_data *d = adap->l2t;
556         int addr_len = neigh->tbl->key_len;
557         u32 *addr = (u32 *) neigh->primary_key;
558         int ifidx = neigh->dev->ifindex;
559         int hash = addr_hash(d, addr, addr_len, ifidx);
560
561         read_lock_bh(&d->lock);
562         for (e = d->l2tab[hash].first; e; e = e->next)
563                 if (!addreq(e, addr) && e->ifindex == ifidx) {
564                         spin_lock(&e->lock);
565                         if (atomic_read(&e->refcnt))
566                                 goto found;
567                         spin_unlock(&e->lock);
568                         break;
569                 }
570         read_unlock_bh(&d->lock);
571         return;
572
573  found:
574         read_unlock(&d->lock);
575
576         if (neigh != e->neigh)
577                 neigh_replace(e, neigh);
578
579         if (e->state == L2T_STATE_RESOLVING) {
580                 if (neigh->nud_state & NUD_FAILED) {
581                         arpq = e->arpq_head;
582                         e->arpq_head = e->arpq_tail = NULL;
583                 } else if ((neigh->nud_state & (NUD_CONNECTED | NUD_STALE)) &&
584                            e->arpq_head) {
585                         write_l2e(adap, e, 1);
586                 }
587         } else {
588                 e->state = neigh->nud_state & NUD_CONNECTED ?
589                         L2T_STATE_VALID : L2T_STATE_STALE;
590                 if (memcmp(e->dmac, neigh->ha, sizeof(e->dmac)))
591                         write_l2e(adap, e, 0);
592         }
593
594         spin_unlock_bh(&e->lock);
595
596         if (arpq)
597                 handle_failed_resolution(adap, arpq);
598 }
599
600 /* Allocate an L2T entry for use by a switching rule.  Such need to be
601  * explicitly freed and while busy they are not on any hash chain, so normal
602  * address resolution updates do not see them.
603  */
604 struct l2t_entry *t4_l2t_alloc_switching(struct adapter *adap, u16 vlan,
605                                          u8 port, u8 *eth_addr)
606 {
607         struct l2t_data *d = adap->l2t;
608         struct l2t_entry *e;
609         int ret;
610
611         write_lock_bh(&d->lock);
612         e = find_or_alloc_l2e(d, vlan, port, eth_addr);
613         if (e) {
614                 spin_lock(&e->lock);          /* avoid race with t4_l2t_free */
615                 if (!atomic_read(&e->refcnt)) {
616                         e->state = L2T_STATE_SWITCHING;
617                         e->vlan = vlan;
618                         e->lport = port;
619                         ether_addr_copy(e->dmac, eth_addr);
620                         atomic_set(&e->refcnt, 1);
621                         ret = write_l2e(adap, e, 0);
622                         if (ret < 0) {
623                                 _t4_l2e_free(e);
624                                 spin_unlock(&e->lock);
625                                 write_unlock_bh(&d->lock);
626                                 return NULL;
627                         }
628                 } else {
629                         atomic_inc(&e->refcnt);
630                 }
631
632                 spin_unlock(&e->lock);
633         }
634         write_unlock_bh(&d->lock);
635         return e;
636 }
637
638 /**
639  * @dev: net_device pointer
640  * @vlan: VLAN Id
641  * @port: Associated port
642  * @dmac: Destination MAC address to add to L2T
643  * Returns pointer to the allocated l2t entry
644  *
645  * Allocates an L2T entry for use by switching rule of a filter
646  */
647 struct l2t_entry *cxgb4_l2t_alloc_switching(struct net_device *dev, u16 vlan,
648                                             u8 port, u8 *dmac)
649 {
650         struct adapter *adap = netdev2adap(dev);
651
652         return t4_l2t_alloc_switching(adap, vlan, port, dmac);
653 }
654 EXPORT_SYMBOL(cxgb4_l2t_alloc_switching);
655
656 struct l2t_data *t4_init_l2t(unsigned int l2t_start, unsigned int l2t_end)
657 {
658         unsigned int l2t_size;
659         int i;
660         struct l2t_data *d;
661
662         if (l2t_start >= l2t_end || l2t_end >= L2T_SIZE)
663                 return NULL;
664         l2t_size = l2t_end - l2t_start + 1;
665         if (l2t_size < L2T_MIN_HASH_BUCKETS)
666                 return NULL;
667
668         d = t4_alloc_mem(sizeof(*d) + l2t_size * sizeof(struct l2t_entry));
669         if (!d)
670                 return NULL;
671
672         d->l2t_start = l2t_start;
673         d->l2t_size = l2t_size;
674
675         d->rover = d->l2tab;
676         atomic_set(&d->nfree, l2t_size);
677         rwlock_init(&d->lock);
678
679         for (i = 0; i < d->l2t_size; ++i) {
680                 d->l2tab[i].idx = i;
681                 d->l2tab[i].state = L2T_STATE_UNUSED;
682                 spin_lock_init(&d->l2tab[i].lock);
683                 atomic_set(&d->l2tab[i].refcnt, 0);
684         }
685         return d;
686 }
687
688 static inline void *l2t_get_idx(struct seq_file *seq, loff_t pos)
689 {
690         struct l2t_data *d = seq->private;
691
692         return pos >= d->l2t_size ? NULL : &d->l2tab[pos];
693 }
694
695 static void *l2t_seq_start(struct seq_file *seq, loff_t *pos)
696 {
697         return *pos ? l2t_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
698 }
699
700 static void *l2t_seq_next(struct seq_file *seq, void *v, loff_t *pos)
701 {
702         v = l2t_get_idx(seq, *pos);
703         if (v)
704                 ++*pos;
705         return v;
706 }
707
708 static void l2t_seq_stop(struct seq_file *seq, void *v)
709 {
710 }
711
712 static char l2e_state(const struct l2t_entry *e)
713 {
714         switch (e->state) {
715         case L2T_STATE_VALID: return 'V';
716         case L2T_STATE_STALE: return 'S';
717         case L2T_STATE_SYNC_WRITE: return 'W';
718         case L2T_STATE_RESOLVING: return e->arpq_head ? 'A' : 'R';
719         case L2T_STATE_SWITCHING: return 'X';
720         default:
721                 return 'U';
722         }
723 }
724
725 static int l2t_seq_show(struct seq_file *seq, void *v)
726 {
727         if (v == SEQ_START_TOKEN)
728                 seq_puts(seq, " Idx IP address                "
729                          "Ethernet address  VLAN/P LP State Users Port\n");
730         else {
731                 char ip[60];
732                 struct l2t_data *d = seq->private;
733                 struct l2t_entry *e = v;
734
735                 spin_lock_bh(&e->lock);
736                 if (e->state == L2T_STATE_SWITCHING)
737                         ip[0] = '\0';
738                 else
739                         sprintf(ip, e->v6 ? "%pI6c" : "%pI4", e->addr);
740                 seq_printf(seq, "%4u %-25s %17pM %4d %u %2u   %c   %5u %s\n",
741                            e->idx + d->l2t_start, ip, e->dmac,
742                            e->vlan & VLAN_VID_MASK, vlan_prio(e), e->lport,
743                            l2e_state(e), atomic_read(&e->refcnt),
744                            e->neigh ? e->neigh->dev->name : "");
745                 spin_unlock_bh(&e->lock);
746         }
747         return 0;
748 }
749
750 static const struct seq_operations l2t_seq_ops = {
751         .start = l2t_seq_start,
752         .next = l2t_seq_next,
753         .stop = l2t_seq_stop,
754         .show = l2t_seq_show
755 };
756
757 static int l2t_seq_open(struct inode *inode, struct file *file)
758 {
759         int rc = seq_open(file, &l2t_seq_ops);
760
761         if (!rc) {
762                 struct adapter *adap = inode->i_private;
763                 struct seq_file *seq = file->private_data;
764
765                 seq->private = adap->l2t;
766         }
767         return rc;
768 }
769
770 const struct file_operations t4_l2t_fops = {
771         .owner = THIS_MODULE,
772         .open = l2t_seq_open,
773         .read = seq_read,
774         .llseek = seq_lseek,
775         .release = seq_release,
776 };