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