18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (c) 2003-2008 Chelsio, Inc. All rights reserved.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * This software is available to you under a choice of one of two
58c2ecf20Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
68c2ecf20Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
78c2ecf20Sopenharmony_ci * COPYING in the main directory of this source tree, or the
88c2ecf20Sopenharmony_ci * OpenIB.org BSD license below:
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
118c2ecf20Sopenharmony_ci *     without modification, are permitted provided that the following
128c2ecf20Sopenharmony_ci *     conditions are met:
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci *      - Redistributions of source code must retain the above
158c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
168c2ecf20Sopenharmony_ci *        disclaimer.
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
198c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
208c2ecf20Sopenharmony_ci *        disclaimer in the documentation and/or other materials
218c2ecf20Sopenharmony_ci *        provided with the distribution.
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
248c2ecf20Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
258c2ecf20Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
268c2ecf20Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
278c2ecf20Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
288c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
298c2ecf20Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
308c2ecf20Sopenharmony_ci * SOFTWARE.
318c2ecf20Sopenharmony_ci */
328c2ecf20Sopenharmony_ci#ifndef _CHELSIO_L2T_H
338c2ecf20Sopenharmony_ci#define _CHELSIO_L2T_H
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
368c2ecf20Sopenharmony_ci#include "t3cdev.h"
378c2ecf20Sopenharmony_ci#include <linux/atomic.h>
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cienum {
408c2ecf20Sopenharmony_ci	L2T_STATE_VALID,	/* entry is up to date */
418c2ecf20Sopenharmony_ci	L2T_STATE_STALE,	/* entry may be used but needs revalidation */
428c2ecf20Sopenharmony_ci	L2T_STATE_RESOLVING,	/* entry needs address resolution */
438c2ecf20Sopenharmony_ci	L2T_STATE_UNUSED	/* entry not in use */
448c2ecf20Sopenharmony_ci};
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistruct neighbour;
478c2ecf20Sopenharmony_cistruct sk_buff;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci/*
508c2ecf20Sopenharmony_ci * Each L2T entry plays multiple roles.  First of all, it keeps state for the
518c2ecf20Sopenharmony_ci * corresponding entry of the HW L2 table and maintains a queue of offload
528c2ecf20Sopenharmony_ci * packets awaiting address resolution.  Second, it is a node of a hash table
538c2ecf20Sopenharmony_ci * chain, where the nodes of the chain are linked together through their next
548c2ecf20Sopenharmony_ci * pointer.  Finally, each node is a bucket of a hash table, pointing to the
558c2ecf20Sopenharmony_ci * first element in its chain through its first pointer.
568c2ecf20Sopenharmony_ci */
578c2ecf20Sopenharmony_cistruct l2t_entry {
588c2ecf20Sopenharmony_ci	u16 state;		/* entry state */
598c2ecf20Sopenharmony_ci	u16 idx;		/* entry index */
608c2ecf20Sopenharmony_ci	u32 addr;		/* dest IP address */
618c2ecf20Sopenharmony_ci	int ifindex;		/* neighbor's net_device's ifindex */
628c2ecf20Sopenharmony_ci	u16 smt_idx;		/* SMT index */
638c2ecf20Sopenharmony_ci	u16 vlan;		/* VLAN TCI (id: bits 0-11, prio: 13-15 */
648c2ecf20Sopenharmony_ci	struct neighbour *neigh;	/* associated neighbour */
658c2ecf20Sopenharmony_ci	struct l2t_entry *first;	/* start of hash chain */
668c2ecf20Sopenharmony_ci	struct l2t_entry *next;	/* next l2t_entry on chain */
678c2ecf20Sopenharmony_ci	struct sk_buff_head arpq;	/* queue of packets awaiting resolution */
688c2ecf20Sopenharmony_ci	spinlock_t lock;
698c2ecf20Sopenharmony_ci	atomic_t refcnt;	/* entry reference count */
708c2ecf20Sopenharmony_ci	u8 dmac[6];		/* neighbour's MAC address */
718c2ecf20Sopenharmony_ci};
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_cistruct l2t_data {
748c2ecf20Sopenharmony_ci	unsigned int nentries;	/* number of entries */
758c2ecf20Sopenharmony_ci	struct l2t_entry *rover;	/* starting point for next allocation */
768c2ecf20Sopenharmony_ci	atomic_t nfree;		/* number of free entries */
778c2ecf20Sopenharmony_ci	rwlock_t lock;
788c2ecf20Sopenharmony_ci	struct rcu_head rcu_head;	/* to handle rcu cleanup */
798c2ecf20Sopenharmony_ci	struct l2t_entry l2tab[];
808c2ecf20Sopenharmony_ci};
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_citypedef void (*arp_failure_handler_func)(struct t3cdev * dev,
838c2ecf20Sopenharmony_ci					 struct sk_buff * skb);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci/*
868c2ecf20Sopenharmony_ci * Callback stored in an skb to handle address resolution failure.
878c2ecf20Sopenharmony_ci */
888c2ecf20Sopenharmony_cistruct l2t_skb_cb {
898c2ecf20Sopenharmony_ci	arp_failure_handler_func arp_failure_handler;
908c2ecf20Sopenharmony_ci};
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci#define L2T_SKB_CB(skb) ((struct l2t_skb_cb *)(skb)->cb)
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_cistatic inline void set_arp_failure_handler(struct sk_buff *skb,
958c2ecf20Sopenharmony_ci					   arp_failure_handler_func hnd)
968c2ecf20Sopenharmony_ci{
978c2ecf20Sopenharmony_ci	L2T_SKB_CB(skb)->arp_failure_handler = hnd;
988c2ecf20Sopenharmony_ci}
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci/*
1018c2ecf20Sopenharmony_ci * Getting to the L2 data from an offload device.
1028c2ecf20Sopenharmony_ci */
1038c2ecf20Sopenharmony_ci#define L2DATA(cdev) (rcu_dereference((cdev)->l2opt))
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci#define W_TCB_L2T_IX    0
1068c2ecf20Sopenharmony_ci#define S_TCB_L2T_IX    7
1078c2ecf20Sopenharmony_ci#define M_TCB_L2T_IX    0x7ffULL
1088c2ecf20Sopenharmony_ci#define V_TCB_L2T_IX(x) ((x) << S_TCB_L2T_IX)
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_civoid t3_l2e_free(struct l2t_data *d, struct l2t_entry *e);
1118c2ecf20Sopenharmony_civoid t3_l2t_update(struct t3cdev *dev, struct neighbour *neigh);
1128c2ecf20Sopenharmony_cistruct l2t_entry *t3_l2t_get(struct t3cdev *cdev, struct dst_entry *dst,
1138c2ecf20Sopenharmony_ci			     struct net_device *dev, const void *daddr);
1148c2ecf20Sopenharmony_ciint t3_l2t_send_slow(struct t3cdev *dev, struct sk_buff *skb,
1158c2ecf20Sopenharmony_ci		     struct l2t_entry *e);
1168c2ecf20Sopenharmony_civoid t3_l2t_send_event(struct t3cdev *dev, struct l2t_entry *e);
1178c2ecf20Sopenharmony_cistruct l2t_data *t3_init_l2t(unsigned int l2t_capacity);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ciint cxgb3_ofld_send(struct t3cdev *dev, struct sk_buff *skb);
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_cistatic inline int l2t_send(struct t3cdev *dev, struct sk_buff *skb,
1228c2ecf20Sopenharmony_ci			   struct l2t_entry *e)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	if (likely(e->state == L2T_STATE_VALID))
1258c2ecf20Sopenharmony_ci		return cxgb3_ofld_send(dev, skb);
1268c2ecf20Sopenharmony_ci	return t3_l2t_send_slow(dev, skb, e);
1278c2ecf20Sopenharmony_ci}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_cistatic inline void l2t_release(struct t3cdev *t, struct l2t_entry *e)
1308c2ecf20Sopenharmony_ci{
1318c2ecf20Sopenharmony_ci	struct l2t_data *d;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	rcu_read_lock();
1348c2ecf20Sopenharmony_ci	d = L2DATA(t);
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	if (atomic_dec_and_test(&e->refcnt) && d)
1378c2ecf20Sopenharmony_ci		t3_l2e_free(d, e);
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	rcu_read_unlock();
1408c2ecf20Sopenharmony_ci}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_cistatic inline void l2t_hold(struct l2t_data *d, struct l2t_entry *e)
1438c2ecf20Sopenharmony_ci{
1448c2ecf20Sopenharmony_ci	if (d && atomic_add_return(1, &e->refcnt) == 1)	/* 0 -> 1 transition */
1458c2ecf20Sopenharmony_ci		atomic_dec(&d->nfree);
1468c2ecf20Sopenharmony_ci}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci#endif
149