1f7917c00SJeff Kirsher /*
2f7917c00SJeff Kirsher  * Copyright (c) 2003-2008 Chelsio, Inc. All rights reserved.
3f7917c00SJeff Kirsher  *
4f7917c00SJeff Kirsher  * This software is available to you under a choice of one of two
5f7917c00SJeff Kirsher  * licenses.  You may choose to be licensed under the terms of the GNU
6f7917c00SJeff Kirsher  * General Public License (GPL) Version 2, available from the file
7f7917c00SJeff Kirsher  * COPYING in the main directory of this source tree, or the
8f7917c00SJeff Kirsher  * OpenIB.org BSD license below:
9f7917c00SJeff Kirsher  *
10f7917c00SJeff Kirsher  *     Redistribution and use in source and binary forms, with or
11f7917c00SJeff Kirsher  *     without modification, are permitted provided that the following
12f7917c00SJeff Kirsher  *     conditions are met:
13f7917c00SJeff Kirsher  *
14f7917c00SJeff Kirsher  *      - Redistributions of source code must retain the above
15f7917c00SJeff Kirsher  *        copyright notice, this list of conditions and the following
16f7917c00SJeff Kirsher  *        disclaimer.
17f7917c00SJeff Kirsher  *
18f7917c00SJeff Kirsher  *      - Redistributions in binary form must reproduce the above
19f7917c00SJeff Kirsher  *        copyright notice, this list of conditions and the following
20f7917c00SJeff Kirsher  *        disclaimer in the documentation and/or other materials
21f7917c00SJeff Kirsher  *        provided with the distribution.
22f7917c00SJeff Kirsher  *
23f7917c00SJeff Kirsher  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24f7917c00SJeff Kirsher  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25f7917c00SJeff Kirsher  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26f7917c00SJeff Kirsher  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27f7917c00SJeff Kirsher  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28f7917c00SJeff Kirsher  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29f7917c00SJeff Kirsher  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30f7917c00SJeff Kirsher  * SOFTWARE.
31f7917c00SJeff Kirsher  */
32f7917c00SJeff Kirsher #include <linux/skbuff.h>
33f7917c00SJeff Kirsher #include <linux/netdevice.h>
34f7917c00SJeff Kirsher #include <linux/if.h>
35f7917c00SJeff Kirsher #include <linux/if_vlan.h>
36f7917c00SJeff Kirsher #include <linux/jhash.h>
37f7917c00SJeff Kirsher #include <linux/slab.h>
38f7917c00SJeff Kirsher #include <net/neighbour.h>
39f7917c00SJeff Kirsher #include "common.h"
40f7917c00SJeff Kirsher #include "t3cdev.h"
41f7917c00SJeff Kirsher #include "cxgb3_defs.h"
42f7917c00SJeff Kirsher #include "l2t.h"
43f7917c00SJeff Kirsher #include "t3_cpl.h"
44f7917c00SJeff Kirsher #include "firmware_exports.h"
45f7917c00SJeff Kirsher 
46f7917c00SJeff Kirsher #define VLAN_NONE 0xfff
47f7917c00SJeff Kirsher 
48f7917c00SJeff Kirsher /*
49f7917c00SJeff Kirsher  * Module locking notes:  There is a RW lock protecting the L2 table as a
50f7917c00SJeff Kirsher  * whole plus a spinlock per L2T entry.  Entry lookups and allocations happen
51f7917c00SJeff Kirsher  * under the protection of the table lock, individual entry changes happen
52f7917c00SJeff Kirsher  * while holding that entry's spinlock.  The table lock nests outside the
53f7917c00SJeff Kirsher  * entry locks.  Allocations of new entries take the table lock as writers so
54f7917c00SJeff Kirsher  * no other lookups can happen while allocating new entries.  Entry updates
55f7917c00SJeff Kirsher  * take the table lock as readers so multiple entries can be updated in
56f7917c00SJeff Kirsher  * parallel.  An L2T entry can be dropped by decrementing its reference count
57f7917c00SJeff Kirsher  * and therefore can happen in parallel with entry allocation but no entry
58f7917c00SJeff Kirsher  * can change state or increment its ref count during allocation as both of
59f7917c00SJeff Kirsher  * these perform lookups.
60f7917c00SJeff Kirsher  */
61f7917c00SJeff Kirsher 
62f7917c00SJeff Kirsher static inline unsigned int vlan_prio(const struct l2t_entry *e)
63f7917c00SJeff Kirsher {
64f7917c00SJeff Kirsher 	return e->vlan >> 13;
65f7917c00SJeff Kirsher }
66f7917c00SJeff Kirsher 
67f7917c00SJeff Kirsher static inline unsigned int arp_hash(u32 key, int ifindex,
68f7917c00SJeff Kirsher 				    const struct l2t_data *d)
69f7917c00SJeff Kirsher {
70f7917c00SJeff Kirsher 	return jhash_2words(key, ifindex, 0) & (d->nentries - 1);
71f7917c00SJeff Kirsher }
72f7917c00SJeff Kirsher 
73f7917c00SJeff Kirsher static inline void neigh_replace(struct l2t_entry *e, struct neighbour *n)
74f7917c00SJeff Kirsher {
75f7917c00SJeff Kirsher 	neigh_hold(n);
76f7917c00SJeff Kirsher 	if (e->neigh)
77f7917c00SJeff Kirsher 		neigh_release(e->neigh);
78f7917c00SJeff Kirsher 	e->neigh = n;
79f7917c00SJeff Kirsher }
80f7917c00SJeff Kirsher 
81f7917c00SJeff Kirsher /*
82f7917c00SJeff Kirsher  * Set up an L2T entry and send any packets waiting in the arp queue.  The
83f7917c00SJeff Kirsher  * supplied skb is used for the CPL_L2T_WRITE_REQ.  Must be called with the
84f7917c00SJeff Kirsher  * entry locked.
85f7917c00SJeff Kirsher  */
86f7917c00SJeff Kirsher static int setup_l2e_send_pending(struct t3cdev *dev, struct sk_buff *skb,
87f7917c00SJeff Kirsher 				  struct l2t_entry *e)
88f7917c00SJeff Kirsher {
89f7917c00SJeff Kirsher 	struct cpl_l2t_write_req *req;
90f7917c00SJeff Kirsher 	struct sk_buff *tmp;
91f7917c00SJeff Kirsher 
92f7917c00SJeff Kirsher 	if (!skb) {
93f7917c00SJeff Kirsher 		skb = alloc_skb(sizeof(*req), GFP_ATOMIC);
94f7917c00SJeff Kirsher 		if (!skb)
95f7917c00SJeff Kirsher 			return -ENOMEM;
96f7917c00SJeff Kirsher 	}
97f7917c00SJeff Kirsher 
98f7917c00SJeff Kirsher 	req = (struct cpl_l2t_write_req *)__skb_put(skb, sizeof(*req));
99f7917c00SJeff Kirsher 	req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
100f7917c00SJeff Kirsher 	OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_L2T_WRITE_REQ, e->idx));
101f7917c00SJeff Kirsher 	req->params = htonl(V_L2T_W_IDX(e->idx) | V_L2T_W_IFF(e->smt_idx) |
102f7917c00SJeff Kirsher 			    V_L2T_W_VLAN(e->vlan & VLAN_VID_MASK) |
103f7917c00SJeff Kirsher 			    V_L2T_W_PRIO(vlan_prio(e)));
104f7917c00SJeff Kirsher 	memcpy(e->dmac, e->neigh->ha, sizeof(e->dmac));
105f7917c00SJeff Kirsher 	memcpy(req->dst_mac, e->dmac, sizeof(req->dst_mac));
106f7917c00SJeff Kirsher 	skb->priority = CPL_PRIORITY_CONTROL;
107f7917c00SJeff Kirsher 	cxgb3_ofld_send(dev, skb);
108f7917c00SJeff Kirsher 
109f7917c00SJeff Kirsher 	skb_queue_walk_safe(&e->arpq, skb, tmp) {
110f7917c00SJeff Kirsher 		__skb_unlink(skb, &e->arpq);
111f7917c00SJeff Kirsher 		cxgb3_ofld_send(dev, skb);
112f7917c00SJeff Kirsher 	}
113f7917c00SJeff Kirsher 	e->state = L2T_STATE_VALID;
114f7917c00SJeff Kirsher 
115f7917c00SJeff Kirsher 	return 0;
116f7917c00SJeff Kirsher }
117f7917c00SJeff Kirsher 
118f7917c00SJeff Kirsher /*
119f7917c00SJeff Kirsher  * Add a packet to the an L2T entry's queue of packets awaiting resolution.
120f7917c00SJeff Kirsher  * Must be called with the entry's lock held.
121f7917c00SJeff Kirsher  */
122f7917c00SJeff Kirsher static inline void arpq_enqueue(struct l2t_entry *e, struct sk_buff *skb)
123f7917c00SJeff Kirsher {
124f7917c00SJeff Kirsher 	__skb_queue_tail(&e->arpq, skb);
125f7917c00SJeff Kirsher }
126f7917c00SJeff Kirsher 
127f7917c00SJeff Kirsher int t3_l2t_send_slow(struct t3cdev *dev, struct sk_buff *skb,
128f7917c00SJeff Kirsher 		     struct l2t_entry *e)
129f7917c00SJeff Kirsher {
130f7917c00SJeff Kirsher again:
131f7917c00SJeff Kirsher 	switch (e->state) {
132f7917c00SJeff Kirsher 	case L2T_STATE_STALE:	/* entry is stale, kick off revalidation */
133f7917c00SJeff Kirsher 		neigh_event_send(e->neigh, NULL);
134f7917c00SJeff Kirsher 		spin_lock_bh(&e->lock);
135f7917c00SJeff Kirsher 		if (e->state == L2T_STATE_STALE)
136f7917c00SJeff Kirsher 			e->state = L2T_STATE_VALID;
137f7917c00SJeff Kirsher 		spin_unlock_bh(&e->lock);
138f7917c00SJeff Kirsher 	case L2T_STATE_VALID:	/* fast-path, send the packet on */
139f7917c00SJeff Kirsher 		return cxgb3_ofld_send(dev, skb);
140f7917c00SJeff Kirsher 	case L2T_STATE_RESOLVING:
141f7917c00SJeff Kirsher 		spin_lock_bh(&e->lock);
142f7917c00SJeff Kirsher 		if (e->state != L2T_STATE_RESOLVING) {
143f7917c00SJeff Kirsher 			/* ARP already completed */
144f7917c00SJeff Kirsher 			spin_unlock_bh(&e->lock);
145f7917c00SJeff Kirsher 			goto again;
146f7917c00SJeff Kirsher 		}
147f7917c00SJeff Kirsher 		arpq_enqueue(e, skb);
148f7917c00SJeff Kirsher 		spin_unlock_bh(&e->lock);
149f7917c00SJeff Kirsher 
150f7917c00SJeff Kirsher 		/*
151f7917c00SJeff Kirsher 		 * Only the first packet added to the arpq should kick off
152f7917c00SJeff Kirsher 		 * resolution.  However, because the alloc_skb below can fail,
153f7917c00SJeff Kirsher 		 * we allow each packet added to the arpq to retry resolution
154f7917c00SJeff Kirsher 		 * as a way of recovering from transient memory exhaustion.
155f7917c00SJeff Kirsher 		 * A better way would be to use a work request to retry L2T
156f7917c00SJeff Kirsher 		 * entries when there's no memory.
157f7917c00SJeff Kirsher 		 */
158f7917c00SJeff Kirsher 		if (!neigh_event_send(e->neigh, NULL)) {
159f7917c00SJeff Kirsher 			skb = alloc_skb(sizeof(struct cpl_l2t_write_req),
160f7917c00SJeff Kirsher 					GFP_ATOMIC);
161f7917c00SJeff Kirsher 			if (!skb)
162f7917c00SJeff Kirsher 				break;
163f7917c00SJeff Kirsher 
164f7917c00SJeff Kirsher 			spin_lock_bh(&e->lock);
165f7917c00SJeff Kirsher 			if (!skb_queue_empty(&e->arpq))
166f7917c00SJeff Kirsher 				setup_l2e_send_pending(dev, skb, e);
167f7917c00SJeff Kirsher 			else	/* we lost the race */
168f7917c00SJeff Kirsher 				__kfree_skb(skb);
169f7917c00SJeff Kirsher 			spin_unlock_bh(&e->lock);
170f7917c00SJeff Kirsher 		}
171f7917c00SJeff Kirsher 	}
172f7917c00SJeff Kirsher 	return 0;
173f7917c00SJeff Kirsher }
174f7917c00SJeff Kirsher 
175f7917c00SJeff Kirsher EXPORT_SYMBOL(t3_l2t_send_slow);
176f7917c00SJeff Kirsher 
177f7917c00SJeff Kirsher void t3_l2t_send_event(struct t3cdev *dev, struct l2t_entry *e)
178f7917c00SJeff Kirsher {
179f7917c00SJeff Kirsher again:
180f7917c00SJeff Kirsher 	switch (e->state) {
181f7917c00SJeff Kirsher 	case L2T_STATE_STALE:	/* entry is stale, kick off revalidation */
182f7917c00SJeff Kirsher 		neigh_event_send(e->neigh, NULL);
183f7917c00SJeff Kirsher 		spin_lock_bh(&e->lock);
184f7917c00SJeff Kirsher 		if (e->state == L2T_STATE_STALE) {
185f7917c00SJeff Kirsher 			e->state = L2T_STATE_VALID;
186f7917c00SJeff Kirsher 		}
187f7917c00SJeff Kirsher 		spin_unlock_bh(&e->lock);
188f7917c00SJeff Kirsher 		return;
189f7917c00SJeff Kirsher 	case L2T_STATE_VALID:	/* fast-path, send the packet on */
190f7917c00SJeff Kirsher 		return;
191f7917c00SJeff Kirsher 	case L2T_STATE_RESOLVING:
192f7917c00SJeff Kirsher 		spin_lock_bh(&e->lock);
193f7917c00SJeff Kirsher 		if (e->state != L2T_STATE_RESOLVING) {
194f7917c00SJeff Kirsher 			/* ARP already completed */
195f7917c00SJeff Kirsher 			spin_unlock_bh(&e->lock);
196f7917c00SJeff Kirsher 			goto again;
197f7917c00SJeff Kirsher 		}
198f7917c00SJeff Kirsher 		spin_unlock_bh(&e->lock);
199f7917c00SJeff Kirsher 
200f7917c00SJeff Kirsher 		/*
201f7917c00SJeff Kirsher 		 * Only the first packet added to the arpq should kick off
202f7917c00SJeff Kirsher 		 * resolution.  However, because the alloc_skb below can fail,
203f7917c00SJeff Kirsher 		 * we allow each packet added to the arpq to retry resolution
204f7917c00SJeff Kirsher 		 * as a way of recovering from transient memory exhaustion.
205f7917c00SJeff Kirsher 		 * A better way would be to use a work request to retry L2T
206f7917c00SJeff Kirsher 		 * entries when there's no memory.
207f7917c00SJeff Kirsher 		 */
208f7917c00SJeff Kirsher 		neigh_event_send(e->neigh, NULL);
209f7917c00SJeff Kirsher 	}
210f7917c00SJeff Kirsher }
211f7917c00SJeff Kirsher 
212f7917c00SJeff Kirsher EXPORT_SYMBOL(t3_l2t_send_event);
213f7917c00SJeff Kirsher 
214f7917c00SJeff Kirsher /*
215f7917c00SJeff Kirsher  * Allocate a free L2T entry.  Must be called with l2t_data.lock held.
216f7917c00SJeff Kirsher  */
217f7917c00SJeff Kirsher static struct l2t_entry *alloc_l2e(struct l2t_data *d)
218f7917c00SJeff Kirsher {
219f7917c00SJeff Kirsher 	struct l2t_entry *end, *e, **p;
220f7917c00SJeff Kirsher 
221f7917c00SJeff Kirsher 	if (!atomic_read(&d->nfree))
222f7917c00SJeff Kirsher 		return NULL;
223f7917c00SJeff Kirsher 
224f7917c00SJeff Kirsher 	/* there's definitely a free entry */
225f7917c00SJeff Kirsher 	for (e = d->rover, end = &d->l2tab[d->nentries]; e != end; ++e)
226f7917c00SJeff Kirsher 		if (atomic_read(&e->refcnt) == 0)
227f7917c00SJeff Kirsher 			goto found;
228f7917c00SJeff Kirsher 
229f7917c00SJeff Kirsher 	for (e = &d->l2tab[1]; atomic_read(&e->refcnt); ++e) ;
230f7917c00SJeff Kirsher found:
231f7917c00SJeff Kirsher 	d->rover = e + 1;
232f7917c00SJeff Kirsher 	atomic_dec(&d->nfree);
233f7917c00SJeff Kirsher 
234f7917c00SJeff Kirsher 	/*
235f7917c00SJeff Kirsher 	 * The entry we found may be an inactive entry that is
236f7917c00SJeff Kirsher 	 * presently in the hash table.  We need to remove it.
237f7917c00SJeff Kirsher 	 */
238f7917c00SJeff Kirsher 	if (e->state != L2T_STATE_UNUSED) {
239f7917c00SJeff Kirsher 		int hash = arp_hash(e->addr, e->ifindex, d);
240f7917c00SJeff Kirsher 
241f7917c00SJeff Kirsher 		for (p = &d->l2tab[hash].first; *p; p = &(*p)->next)
242f7917c00SJeff Kirsher 			if (*p == e) {
243f7917c00SJeff Kirsher 				*p = e->next;
244f7917c00SJeff Kirsher 				break;
245f7917c00SJeff Kirsher 			}
246f7917c00SJeff Kirsher 		e->state = L2T_STATE_UNUSED;
247f7917c00SJeff Kirsher 	}
248f7917c00SJeff Kirsher 	return e;
249f7917c00SJeff Kirsher }
250f7917c00SJeff Kirsher 
251f7917c00SJeff Kirsher /*
252f7917c00SJeff Kirsher  * Called when an L2T entry has no more users.  The entry is left in the hash
253f7917c00SJeff Kirsher  * table since it is likely to be reused but we also bump nfree to indicate
254f7917c00SJeff Kirsher  * that the entry can be reallocated for a different neighbor.  We also drop
255f7917c00SJeff Kirsher  * the existing neighbor reference in case the neighbor is going away and is
256f7917c00SJeff Kirsher  * waiting on our reference.
257f7917c00SJeff Kirsher  *
258f7917c00SJeff Kirsher  * Because entries can be reallocated to other neighbors once their ref count
259f7917c00SJeff Kirsher  * drops to 0 we need to take the entry's lock to avoid races with a new
260f7917c00SJeff Kirsher  * incarnation.
261f7917c00SJeff Kirsher  */
262f7917c00SJeff Kirsher void t3_l2e_free(struct l2t_data *d, struct l2t_entry *e)
263f7917c00SJeff Kirsher {
264f7917c00SJeff Kirsher 	spin_lock_bh(&e->lock);
265f7917c00SJeff Kirsher 	if (atomic_read(&e->refcnt) == 0) {	/* hasn't been recycled */
266f7917c00SJeff Kirsher 		if (e->neigh) {
267f7917c00SJeff Kirsher 			neigh_release(e->neigh);
268f7917c00SJeff Kirsher 			e->neigh = NULL;
269f7917c00SJeff Kirsher 		}
270f7917c00SJeff Kirsher 	}
271f7917c00SJeff Kirsher 	spin_unlock_bh(&e->lock);
272f7917c00SJeff Kirsher 	atomic_inc(&d->nfree);
273f7917c00SJeff Kirsher }
274f7917c00SJeff Kirsher 
275f7917c00SJeff Kirsher EXPORT_SYMBOL(t3_l2e_free);
276f7917c00SJeff Kirsher 
277f7917c00SJeff Kirsher /*
278f7917c00SJeff Kirsher  * Update an L2T entry that was previously used for the same next hop as neigh.
279f7917c00SJeff Kirsher  * Must be called with softirqs disabled.
280f7917c00SJeff Kirsher  */
281f7917c00SJeff Kirsher static inline void reuse_entry(struct l2t_entry *e, struct neighbour *neigh)
282f7917c00SJeff Kirsher {
283f7917c00SJeff Kirsher 	unsigned int nud_state;
284f7917c00SJeff Kirsher 
285f7917c00SJeff Kirsher 	spin_lock(&e->lock);	/* avoid race with t3_l2t_free */
286f7917c00SJeff Kirsher 
287f7917c00SJeff Kirsher 	if (neigh != e->neigh)
288f7917c00SJeff Kirsher 		neigh_replace(e, neigh);
289f7917c00SJeff Kirsher 	nud_state = neigh->nud_state;
290f7917c00SJeff Kirsher 	if (memcmp(e->dmac, neigh->ha, sizeof(e->dmac)) ||
291f7917c00SJeff Kirsher 	    !(nud_state & NUD_VALID))
292f7917c00SJeff Kirsher 		e->state = L2T_STATE_RESOLVING;
293f7917c00SJeff Kirsher 	else if (nud_state & NUD_CONNECTED)
294f7917c00SJeff Kirsher 		e->state = L2T_STATE_VALID;
295f7917c00SJeff Kirsher 	else
296f7917c00SJeff Kirsher 		e->state = L2T_STATE_STALE;
297f7917c00SJeff Kirsher 	spin_unlock(&e->lock);
298f7917c00SJeff Kirsher }
299f7917c00SJeff Kirsher 
300f7917c00SJeff Kirsher struct l2t_entry *t3_l2t_get(struct t3cdev *cdev, struct neighbour *neigh,
301f7917c00SJeff Kirsher 			     struct net_device *dev)
302f7917c00SJeff Kirsher {
303f7917c00SJeff Kirsher 	struct l2t_entry *e;
304f7917c00SJeff Kirsher 	struct l2t_data *d = L2DATA(cdev);
305f7917c00SJeff Kirsher 	u32 addr = *(u32 *) neigh->primary_key;
306f7917c00SJeff Kirsher 	int ifidx = neigh->dev->ifindex;
307f7917c00SJeff Kirsher 	int hash = arp_hash(addr, ifidx, d);
308f7917c00SJeff Kirsher 	struct port_info *p = netdev_priv(dev);
309f7917c00SJeff Kirsher 	int smt_idx = p->port_id;
310f7917c00SJeff Kirsher 
311f7917c00SJeff Kirsher 	write_lock_bh(&d->lock);
312f7917c00SJeff Kirsher 	for (e = d->l2tab[hash].first; e; e = e->next)
313f7917c00SJeff Kirsher 		if (e->addr == addr && e->ifindex == ifidx &&
314f7917c00SJeff Kirsher 		    e->smt_idx == smt_idx) {
315f7917c00SJeff Kirsher 			l2t_hold(d, e);
316f7917c00SJeff Kirsher 			if (atomic_read(&e->refcnt) == 1)
317f7917c00SJeff Kirsher 				reuse_entry(e, neigh);
318f7917c00SJeff Kirsher 			goto done;
319f7917c00SJeff Kirsher 		}
320f7917c00SJeff Kirsher 
321f7917c00SJeff Kirsher 	/* Need to allocate a new entry */
322f7917c00SJeff Kirsher 	e = alloc_l2e(d);
323f7917c00SJeff Kirsher 	if (e) {
324f7917c00SJeff Kirsher 		spin_lock(&e->lock);	/* avoid race with t3_l2t_free */
325f7917c00SJeff Kirsher 		e->next = d->l2tab[hash].first;
326f7917c00SJeff Kirsher 		d->l2tab[hash].first = e;
327f7917c00SJeff Kirsher 		e->state = L2T_STATE_RESOLVING;
328f7917c00SJeff Kirsher 		e->addr = addr;
329f7917c00SJeff Kirsher 		e->ifindex = ifidx;
330f7917c00SJeff Kirsher 		e->smt_idx = smt_idx;
331f7917c00SJeff Kirsher 		atomic_set(&e->refcnt, 1);
332f7917c00SJeff Kirsher 		neigh_replace(e, neigh);
333f7917c00SJeff Kirsher 		if (neigh->dev->priv_flags & IFF_802_1Q_VLAN)
334f7917c00SJeff Kirsher 			e->vlan = vlan_dev_vlan_id(neigh->dev);
335f7917c00SJeff Kirsher 		else
336f7917c00SJeff Kirsher 			e->vlan = VLAN_NONE;
337f7917c00SJeff Kirsher 		spin_unlock(&e->lock);
338f7917c00SJeff Kirsher 	}
339f7917c00SJeff Kirsher done:
340f7917c00SJeff Kirsher 	write_unlock_bh(&d->lock);
341f7917c00SJeff Kirsher 	return e;
342f7917c00SJeff Kirsher }
343f7917c00SJeff Kirsher 
344f7917c00SJeff Kirsher EXPORT_SYMBOL(t3_l2t_get);
345f7917c00SJeff Kirsher 
346f7917c00SJeff Kirsher /*
347f7917c00SJeff Kirsher  * Called when address resolution fails for an L2T entry to handle packets
348f7917c00SJeff Kirsher  * on the arpq head.  If a packet specifies a failure handler it is invoked,
349f7917c00SJeff Kirsher  * otherwise the packets is sent to the offload device.
350f7917c00SJeff Kirsher  *
351f7917c00SJeff Kirsher  * XXX: maybe we should abandon the latter behavior and just require a failure
352f7917c00SJeff Kirsher  * handler.
353f7917c00SJeff Kirsher  */
354f7917c00SJeff Kirsher static void handle_failed_resolution(struct t3cdev *dev, struct sk_buff_head *arpq)
355f7917c00SJeff Kirsher {
356f7917c00SJeff Kirsher 	struct sk_buff *skb, *tmp;
357f7917c00SJeff Kirsher 
358f7917c00SJeff Kirsher 	skb_queue_walk_safe(arpq, skb, tmp) {
359f7917c00SJeff Kirsher 		struct l2t_skb_cb *cb = L2T_SKB_CB(skb);
360f7917c00SJeff Kirsher 
361f7917c00SJeff Kirsher 		__skb_unlink(skb, arpq);
362f7917c00SJeff Kirsher 		if (cb->arp_failure_handler)
363f7917c00SJeff Kirsher 			cb->arp_failure_handler(dev, skb);
364f7917c00SJeff Kirsher 		else
365f7917c00SJeff Kirsher 			cxgb3_ofld_send(dev, skb);
366f7917c00SJeff Kirsher 	}
367f7917c00SJeff Kirsher }
368f7917c00SJeff Kirsher 
369f7917c00SJeff Kirsher /*
370f7917c00SJeff Kirsher  * Called when the host's ARP layer makes a change to some entry that is
371f7917c00SJeff Kirsher  * loaded into the HW L2 table.
372f7917c00SJeff Kirsher  */
373f7917c00SJeff Kirsher void t3_l2t_update(struct t3cdev *dev, struct neighbour *neigh)
374f7917c00SJeff Kirsher {
375f7917c00SJeff Kirsher 	struct sk_buff_head arpq;
376f7917c00SJeff Kirsher 	struct l2t_entry *e;
377f7917c00SJeff Kirsher 	struct l2t_data *d = L2DATA(dev);
378f7917c00SJeff Kirsher 	u32 addr = *(u32 *) neigh->primary_key;
379f7917c00SJeff Kirsher 	int ifidx = neigh->dev->ifindex;
380f7917c00SJeff Kirsher 	int hash = arp_hash(addr, ifidx, d);
381f7917c00SJeff Kirsher 
382f7917c00SJeff Kirsher 	read_lock_bh(&d->lock);
383f7917c00SJeff Kirsher 	for (e = d->l2tab[hash].first; e; e = e->next)
384f7917c00SJeff Kirsher 		if (e->addr == addr && e->ifindex == ifidx) {
385f7917c00SJeff Kirsher 			spin_lock(&e->lock);
386f7917c00SJeff Kirsher 			goto found;
387f7917c00SJeff Kirsher 		}
388f7917c00SJeff Kirsher 	read_unlock_bh(&d->lock);
389f7917c00SJeff Kirsher 	return;
390f7917c00SJeff Kirsher 
391f7917c00SJeff Kirsher found:
392f7917c00SJeff Kirsher 	__skb_queue_head_init(&arpq);
393f7917c00SJeff Kirsher 
394f7917c00SJeff Kirsher 	read_unlock(&d->lock);
395f7917c00SJeff Kirsher 	if (atomic_read(&e->refcnt)) {
396f7917c00SJeff Kirsher 		if (neigh != e->neigh)
397f7917c00SJeff Kirsher 			neigh_replace(e, neigh);
398f7917c00SJeff Kirsher 
399f7917c00SJeff Kirsher 		if (e->state == L2T_STATE_RESOLVING) {
400f7917c00SJeff Kirsher 			if (neigh->nud_state & NUD_FAILED) {
401f7917c00SJeff Kirsher 				skb_queue_splice_init(&e->arpq, &arpq);
402f7917c00SJeff Kirsher 			} else if (neigh->nud_state & (NUD_CONNECTED|NUD_STALE))
403f7917c00SJeff Kirsher 				setup_l2e_send_pending(dev, NULL, e);
404f7917c00SJeff Kirsher 		} else {
405f7917c00SJeff Kirsher 			e->state = neigh->nud_state & NUD_CONNECTED ?
406f7917c00SJeff Kirsher 			    L2T_STATE_VALID : L2T_STATE_STALE;
407f7917c00SJeff Kirsher 			if (memcmp(e->dmac, neigh->ha, 6))
408f7917c00SJeff Kirsher 				setup_l2e_send_pending(dev, NULL, e);
409f7917c00SJeff Kirsher 		}
410f7917c00SJeff Kirsher 	}
411f7917c00SJeff Kirsher 	spin_unlock_bh(&e->lock);
412f7917c00SJeff Kirsher 
413f7917c00SJeff Kirsher 	if (!skb_queue_empty(&arpq))
414f7917c00SJeff Kirsher 		handle_failed_resolution(dev, &arpq);
415f7917c00SJeff Kirsher }
416f7917c00SJeff Kirsher 
417f7917c00SJeff Kirsher struct l2t_data *t3_init_l2t(unsigned int l2t_capacity)
418f7917c00SJeff Kirsher {
419f7917c00SJeff Kirsher 	struct l2t_data *d;
420f7917c00SJeff Kirsher 	int i, size = sizeof(*d) + l2t_capacity * sizeof(struct l2t_entry);
421f7917c00SJeff Kirsher 
422f7917c00SJeff Kirsher 	d = cxgb_alloc_mem(size);
423f7917c00SJeff Kirsher 	if (!d)
424f7917c00SJeff Kirsher 		return NULL;
425f7917c00SJeff Kirsher 
426f7917c00SJeff Kirsher 	d->nentries = l2t_capacity;
427f7917c00SJeff Kirsher 	d->rover = &d->l2tab[1];	/* entry 0 is not used */
428f7917c00SJeff Kirsher 	atomic_set(&d->nfree, l2t_capacity - 1);
429f7917c00SJeff Kirsher 	rwlock_init(&d->lock);
430f7917c00SJeff Kirsher 
431f7917c00SJeff Kirsher 	for (i = 0; i < l2t_capacity; ++i) {
432f7917c00SJeff Kirsher 		d->l2tab[i].idx = i;
433f7917c00SJeff Kirsher 		d->l2tab[i].state = L2T_STATE_UNUSED;
434f7917c00SJeff Kirsher 		__skb_queue_head_init(&d->l2tab[i].arpq);
435f7917c00SJeff Kirsher 		spin_lock_init(&d->l2tab[i].lock);
436f7917c00SJeff Kirsher 		atomic_set(&d->l2tab[i].refcnt, 0);
437f7917c00SJeff Kirsher 	}
438f7917c00SJeff Kirsher 	return d;
439f7917c00SJeff Kirsher }
440f7917c00SJeff Kirsher 
441f7917c00SJeff Kirsher void t3_free_l2t(struct l2t_data *d)
442f7917c00SJeff Kirsher {
443f7917c00SJeff Kirsher 	cxgb_free_mem(d);
444f7917c00SJeff Kirsher }
445f7917c00SJeff Kirsher 
446