162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright (c) 2003-2008 Chelsio, Inc. All rights reserved.
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * This software is available to you under a choice of one of two
562306a36Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
662306a36Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
762306a36Sopenharmony_ci * COPYING in the main directory of this source tree, or the
862306a36Sopenharmony_ci * OpenIB.org BSD license below:
962306a36Sopenharmony_ci *
1062306a36Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
1162306a36Sopenharmony_ci *     without modification, are permitted provided that the following
1262306a36Sopenharmony_ci *     conditions are met:
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci *      - Redistributions of source code must retain the above
1562306a36Sopenharmony_ci *        copyright notice, this list of conditions and the following
1662306a36Sopenharmony_ci *        disclaimer.
1762306a36Sopenharmony_ci *
1862306a36Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
1962306a36Sopenharmony_ci *        copyright notice, this list of conditions and the following
2062306a36Sopenharmony_ci *        disclaimer in the documentation and/or other materials
2162306a36Sopenharmony_ci *        provided with the distribution.
2262306a36Sopenharmony_ci *
2362306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2462306a36Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2562306a36Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2662306a36Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
2762306a36Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
2862306a36Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
2962306a36Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3062306a36Sopenharmony_ci * SOFTWARE.
3162306a36Sopenharmony_ci */
3262306a36Sopenharmony_ci#ifndef _CHELSIO_L2T_H
3362306a36Sopenharmony_ci#define _CHELSIO_L2T_H
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci#include <linux/spinlock.h>
3662306a36Sopenharmony_ci#include "t3cdev.h"
3762306a36Sopenharmony_ci#include <linux/atomic.h>
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_cienum {
4062306a36Sopenharmony_ci	L2T_STATE_VALID,	/* entry is up to date */
4162306a36Sopenharmony_ci	L2T_STATE_STALE,	/* entry may be used but needs revalidation */
4262306a36Sopenharmony_ci	L2T_STATE_RESOLVING,	/* entry needs address resolution */
4362306a36Sopenharmony_ci	L2T_STATE_UNUSED	/* entry not in use */
4462306a36Sopenharmony_ci};
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_cistruct neighbour;
4762306a36Sopenharmony_cistruct sk_buff;
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci/*
5062306a36Sopenharmony_ci * Each L2T entry plays multiple roles.  First of all, it keeps state for the
5162306a36Sopenharmony_ci * corresponding entry of the HW L2 table and maintains a queue of offload
5262306a36Sopenharmony_ci * packets awaiting address resolution.  Second, it is a node of a hash table
5362306a36Sopenharmony_ci * chain, where the nodes of the chain are linked together through their next
5462306a36Sopenharmony_ci * pointer.  Finally, each node is a bucket of a hash table, pointing to the
5562306a36Sopenharmony_ci * first element in its chain through its first pointer.
5662306a36Sopenharmony_ci */
5762306a36Sopenharmony_cistruct l2t_entry {
5862306a36Sopenharmony_ci	u16 state;		/* entry state */
5962306a36Sopenharmony_ci	u16 idx;		/* entry index */
6062306a36Sopenharmony_ci	u32 addr;		/* dest IP address */
6162306a36Sopenharmony_ci	int ifindex;		/* neighbor's net_device's ifindex */
6262306a36Sopenharmony_ci	u16 smt_idx;		/* SMT index */
6362306a36Sopenharmony_ci	u16 vlan;		/* VLAN TCI (id: bits 0-11, prio: 13-15 */
6462306a36Sopenharmony_ci	struct neighbour *neigh;	/* associated neighbour */
6562306a36Sopenharmony_ci	struct l2t_entry *first;	/* start of hash chain */
6662306a36Sopenharmony_ci	struct l2t_entry *next;	/* next l2t_entry on chain */
6762306a36Sopenharmony_ci	struct sk_buff_head arpq;	/* queue of packets awaiting resolution */
6862306a36Sopenharmony_ci	spinlock_t lock;
6962306a36Sopenharmony_ci	atomic_t refcnt;	/* entry reference count */
7062306a36Sopenharmony_ci	u8 dmac[6];		/* neighbour's MAC address */
7162306a36Sopenharmony_ci};
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_cistruct l2t_data {
7462306a36Sopenharmony_ci	unsigned int nentries;	/* number of entries */
7562306a36Sopenharmony_ci	struct l2t_entry *rover;	/* starting point for next allocation */
7662306a36Sopenharmony_ci	atomic_t nfree;		/* number of free entries */
7762306a36Sopenharmony_ci	rwlock_t lock;
7862306a36Sopenharmony_ci	struct rcu_head rcu_head;	/* to handle rcu cleanup */
7962306a36Sopenharmony_ci	struct l2t_entry l2tab[];
8062306a36Sopenharmony_ci};
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_citypedef void (*arp_failure_handler_func)(struct t3cdev * dev,
8362306a36Sopenharmony_ci					 struct sk_buff * skb);
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci/*
8662306a36Sopenharmony_ci * Callback stored in an skb to handle address resolution failure.
8762306a36Sopenharmony_ci */
8862306a36Sopenharmony_cistruct l2t_skb_cb {
8962306a36Sopenharmony_ci	arp_failure_handler_func arp_failure_handler;
9062306a36Sopenharmony_ci};
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci#define L2T_SKB_CB(skb) ((struct l2t_skb_cb *)(skb)->cb)
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_cistatic inline void set_arp_failure_handler(struct sk_buff *skb,
9562306a36Sopenharmony_ci					   arp_failure_handler_func hnd)
9662306a36Sopenharmony_ci{
9762306a36Sopenharmony_ci	L2T_SKB_CB(skb)->arp_failure_handler = hnd;
9862306a36Sopenharmony_ci}
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci/*
10162306a36Sopenharmony_ci * Getting to the L2 data from an offload device.
10262306a36Sopenharmony_ci */
10362306a36Sopenharmony_ci#define L2DATA(cdev) (rcu_dereference((cdev)->l2opt))
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci#define W_TCB_L2T_IX    0
10662306a36Sopenharmony_ci#define S_TCB_L2T_IX    7
10762306a36Sopenharmony_ci#define M_TCB_L2T_IX    0x7ffULL
10862306a36Sopenharmony_ci#define V_TCB_L2T_IX(x) ((x) << S_TCB_L2T_IX)
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_civoid t3_l2e_free(struct l2t_data *d, struct l2t_entry *e);
11162306a36Sopenharmony_civoid t3_l2t_update(struct t3cdev *dev, struct neighbour *neigh);
11262306a36Sopenharmony_cistruct l2t_entry *t3_l2t_get(struct t3cdev *cdev, struct dst_entry *dst,
11362306a36Sopenharmony_ci			     struct net_device *dev, const void *daddr);
11462306a36Sopenharmony_ciint t3_l2t_send_slow(struct t3cdev *dev, struct sk_buff *skb,
11562306a36Sopenharmony_ci		     struct l2t_entry *e);
11662306a36Sopenharmony_civoid t3_l2t_send_event(struct t3cdev *dev, struct l2t_entry *e);
11762306a36Sopenharmony_cistruct l2t_data *t3_init_l2t(unsigned int l2t_capacity);
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ciint cxgb3_ofld_send(struct t3cdev *dev, struct sk_buff *skb);
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_cistatic inline int l2t_send(struct t3cdev *dev, struct sk_buff *skb,
12262306a36Sopenharmony_ci			   struct l2t_entry *e)
12362306a36Sopenharmony_ci{
12462306a36Sopenharmony_ci	if (likely(e->state == L2T_STATE_VALID))
12562306a36Sopenharmony_ci		return cxgb3_ofld_send(dev, skb);
12662306a36Sopenharmony_ci	return t3_l2t_send_slow(dev, skb, e);
12762306a36Sopenharmony_ci}
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_cistatic inline void l2t_release(struct t3cdev *t, struct l2t_entry *e)
13062306a36Sopenharmony_ci{
13162306a36Sopenharmony_ci	struct l2t_data *d;
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci	rcu_read_lock();
13462306a36Sopenharmony_ci	d = L2DATA(t);
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci	if (atomic_dec_and_test(&e->refcnt) && d)
13762306a36Sopenharmony_ci		t3_l2e_free(d, e);
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci	rcu_read_unlock();
14062306a36Sopenharmony_ci}
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_cistatic inline void l2t_hold(struct l2t_data *d, struct l2t_entry *e)
14362306a36Sopenharmony_ci{
14462306a36Sopenharmony_ci	if (d && atomic_add_return(1, &e->refcnt) == 1)	/* 0 -> 1 transition */
14562306a36Sopenharmony_ci		atomic_dec(&d->nfree);
14662306a36Sopenharmony_ci}
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ci#endif
149