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 #ifndef _CHELSIO_L2T_H
33f7917c00SJeff Kirsher #define _CHELSIO_L2T_H
34f7917c00SJeff Kirsher
35f7917c00SJeff Kirsher #include <linux/spinlock.h>
36f7917c00SJeff Kirsher #include "t3cdev.h"
37f7917c00SJeff Kirsher #include <linux/atomic.h>
38f7917c00SJeff Kirsher
39f7917c00SJeff Kirsher enum {
40f7917c00SJeff Kirsher L2T_STATE_VALID, /* entry is up to date */
41f7917c00SJeff Kirsher L2T_STATE_STALE, /* entry may be used but needs revalidation */
42f7917c00SJeff Kirsher L2T_STATE_RESOLVING, /* entry needs address resolution */
43f7917c00SJeff Kirsher L2T_STATE_UNUSED /* entry not in use */
44f7917c00SJeff Kirsher };
45f7917c00SJeff Kirsher
46f7917c00SJeff Kirsher struct neighbour;
47f7917c00SJeff Kirsher struct sk_buff;
48f7917c00SJeff Kirsher
49f7917c00SJeff Kirsher /*
50f7917c00SJeff Kirsher * Each L2T entry plays multiple roles. First of all, it keeps state for the
51f7917c00SJeff Kirsher * corresponding entry of the HW L2 table and maintains a queue of offload
52f7917c00SJeff Kirsher * packets awaiting address resolution. Second, it is a node of a hash table
53f7917c00SJeff Kirsher * chain, where the nodes of the chain are linked together through their next
54f7917c00SJeff Kirsher * pointer. Finally, each node is a bucket of a hash table, pointing to the
55f7917c00SJeff Kirsher * first element in its chain through its first pointer.
56f7917c00SJeff Kirsher */
57f7917c00SJeff Kirsher struct l2t_entry {
58f7917c00SJeff Kirsher u16 state; /* entry state */
59f7917c00SJeff Kirsher u16 idx; /* entry index */
60f7917c00SJeff Kirsher u32 addr; /* dest IP address */
61f7917c00SJeff Kirsher int ifindex; /* neighbor's net_device's ifindex */
62f7917c00SJeff Kirsher u16 smt_idx; /* SMT index */
63f7917c00SJeff Kirsher u16 vlan; /* VLAN TCI (id: bits 0-11, prio: 13-15 */
64f7917c00SJeff Kirsher struct neighbour *neigh; /* associated neighbour */
65f7917c00SJeff Kirsher struct l2t_entry *first; /* start of hash chain */
66f7917c00SJeff Kirsher struct l2t_entry *next; /* next l2t_entry on chain */
67f7917c00SJeff Kirsher struct sk_buff_head arpq; /* queue of packets awaiting resolution */
68f7917c00SJeff Kirsher spinlock_t lock;
69f7917c00SJeff Kirsher atomic_t refcnt; /* entry reference count */
70f7917c00SJeff Kirsher u8 dmac[6]; /* neighbour's MAC address */
71f7917c00SJeff Kirsher };
72f7917c00SJeff Kirsher
73f7917c00SJeff Kirsher struct l2t_data {
74f7917c00SJeff Kirsher unsigned int nentries; /* number of entries */
75f7917c00SJeff Kirsher struct l2t_entry *rover; /* starting point for next allocation */
76f7917c00SJeff Kirsher atomic_t nfree; /* number of free entries */
77f7917c00SJeff Kirsher rwlock_t lock;
7888c5100cSDavid S. Miller struct rcu_head rcu_head; /* to handle rcu cleanup */
7976497732SGustavo A. R. Silva struct l2t_entry l2tab[];
80f7917c00SJeff Kirsher };
81f7917c00SJeff Kirsher
82f7917c00SJeff Kirsher typedef void (*arp_failure_handler_func)(struct t3cdev * dev,
83f7917c00SJeff Kirsher struct sk_buff * skb);
84f7917c00SJeff Kirsher
85f7917c00SJeff Kirsher /*
86f7917c00SJeff Kirsher * Callback stored in an skb to handle address resolution failure.
87f7917c00SJeff Kirsher */
88f7917c00SJeff Kirsher struct l2t_skb_cb {
89f7917c00SJeff Kirsher arp_failure_handler_func arp_failure_handler;
90f7917c00SJeff Kirsher };
91f7917c00SJeff Kirsher
92f7917c00SJeff Kirsher #define L2T_SKB_CB(skb) ((struct l2t_skb_cb *)(skb)->cb)
93f7917c00SJeff Kirsher
set_arp_failure_handler(struct sk_buff * skb,arp_failure_handler_func hnd)94f7917c00SJeff Kirsher static inline void set_arp_failure_handler(struct sk_buff *skb,
95f7917c00SJeff Kirsher arp_failure_handler_func hnd)
96f7917c00SJeff Kirsher {
97f7917c00SJeff Kirsher L2T_SKB_CB(skb)->arp_failure_handler = hnd;
98f7917c00SJeff Kirsher }
99f7917c00SJeff Kirsher
100f7917c00SJeff Kirsher /*
101f7917c00SJeff Kirsher * Getting to the L2 data from an offload device.
102f7917c00SJeff Kirsher */
10388c5100cSDavid S. Miller #define L2DATA(cdev) (rcu_dereference((cdev)->l2opt))
104f7917c00SJeff Kirsher
105f7917c00SJeff Kirsher #define W_TCB_L2T_IX 0
106f7917c00SJeff Kirsher #define S_TCB_L2T_IX 7
107f7917c00SJeff Kirsher #define M_TCB_L2T_IX 0x7ffULL
108f7917c00SJeff Kirsher #define V_TCB_L2T_IX(x) ((x) << S_TCB_L2T_IX)
109f7917c00SJeff Kirsher
110f7917c00SJeff Kirsher void t3_l2e_free(struct l2t_data *d, struct l2t_entry *e);
111f7917c00SJeff Kirsher void t3_l2t_update(struct t3cdev *dev, struct neighbour *neigh);
112a4757123SDavid Miller struct l2t_entry *t3_l2t_get(struct t3cdev *cdev, struct dst_entry *dst,
113534cb283SDavid S. Miller struct net_device *dev, const void *daddr);
114f7917c00SJeff Kirsher int t3_l2t_send_slow(struct t3cdev *dev, struct sk_buff *skb,
115f7917c00SJeff Kirsher struct l2t_entry *e);
116f7917c00SJeff Kirsher void t3_l2t_send_event(struct t3cdev *dev, struct l2t_entry *e);
117f7917c00SJeff Kirsher struct l2t_data *t3_init_l2t(unsigned int l2t_capacity);
118f7917c00SJeff Kirsher
119f7917c00SJeff Kirsher int cxgb3_ofld_send(struct t3cdev *dev, struct sk_buff *skb);
120f7917c00SJeff Kirsher
l2t_send(struct t3cdev * dev,struct sk_buff * skb,struct l2t_entry * e)121f7917c00SJeff Kirsher static inline int l2t_send(struct t3cdev *dev, struct sk_buff *skb,
122f7917c00SJeff Kirsher struct l2t_entry *e)
123f7917c00SJeff Kirsher {
124f7917c00SJeff Kirsher if (likely(e->state == L2T_STATE_VALID))
125f7917c00SJeff Kirsher return cxgb3_ofld_send(dev, skb);
126f7917c00SJeff Kirsher return t3_l2t_send_slow(dev, skb, e);
127f7917c00SJeff Kirsher }
128f7917c00SJeff Kirsher
l2t_release(struct t3cdev * t,struct l2t_entry * e)12988c5100cSDavid S. Miller static inline void l2t_release(struct t3cdev *t, struct l2t_entry *e)
130f7917c00SJeff Kirsher {
13188c5100cSDavid S. Miller struct l2t_data *d;
13288c5100cSDavid S. Miller
13388c5100cSDavid S. Miller rcu_read_lock();
13488c5100cSDavid S. Miller d = L2DATA(t);
13588c5100cSDavid S. Miller
13688c5100cSDavid S. Miller if (atomic_dec_and_test(&e->refcnt) && d)
137f7917c00SJeff Kirsher t3_l2e_free(d, e);
13888c5100cSDavid S. Miller
13988c5100cSDavid S. Miller rcu_read_unlock();
140f7917c00SJeff Kirsher }
141f7917c00SJeff Kirsher
l2t_hold(struct l2t_data * d,struct l2t_entry * e)142f7917c00SJeff Kirsher static inline void l2t_hold(struct l2t_data *d, struct l2t_entry *e)
143f7917c00SJeff Kirsher {
14488c5100cSDavid S. Miller if (d && atomic_add_return(1, &e->refcnt) == 1) /* 0 -> 1 transition */
145f7917c00SJeff Kirsher atomic_dec(&d->nfree);
146f7917c00SJeff Kirsher }
147f7917c00SJeff Kirsher
148f7917c00SJeff Kirsher #endif
149