1/**************************************************************************
2
3Copyright (c) 2007-2009, Chelsio Inc.
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are met:
8
9 1. Redistributions of source code must retain the above copyright notice,
10    this list of conditions and the following disclaimer.
11
12 2. Neither the name of the Chelsio Corporation nor the names of its
13    contributors may be used to endorse or promote products derived from
14    this software without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26POSSIBILITY OF SUCH DAMAGE.
27
28$FreeBSD$
29
30***************************************************************************/
31#ifndef _CHELSIO_L2T_H
32#define _CHELSIO_L2T_H
33
34#include <sys/lock.h>
35#include <sys/rwlock.h>
36
37enum {
38	L2T_SIZE = 2048
39};
40
41enum {
42	L2T_STATE_VALID,	/* entry is up to date */
43	L2T_STATE_STALE,	/* entry may be used but needs revalidation */
44	L2T_STATE_RESOLVING,	/* entry needs address resolution */
45	L2T_STATE_FAILED,	/* failed to resolve */
46	L2T_STATE_UNUSED	/* entry not in use */
47};
48
49/*
50 * Each L2T entry plays multiple roles.  First of all, it keeps state for the
51 * corresponding entry of the HW L2 table and maintains a queue of offload
52 * packets awaiting address resolution.  Second, it is a node of a hash table
53 * chain, where the nodes of the chain are linked together through their next
54 * pointer.  Finally, each node is a bucket of a hash table, pointing to the
55 * first element in its chain through its first pointer.
56 */
57struct l2t_entry {
58	uint16_t state;               /* entry state */
59	uint16_t idx;                 /* entry index */
60	uint32_t addr;                /* nexthop IP address */
61	struct ifnet *ifp;            /* outgoing interface */
62	uint16_t smt_idx;             /* SMT index */
63	uint16_t vlan;                /* VLAN TCI (id: bits 0-11, prio: 13-15 */
64	struct l2t_entry *first;      /* start of hash chain */
65	struct l2t_entry *next;       /* next l2t_entry on chain */
66	struct mbuf *arpq_head;       /* queue of packets awaiting resolution */
67	struct mbuf *arpq_tail;
68	struct mtx lock;
69	volatile uint32_t refcnt;     /* entry reference count */
70	uint8_t dmac[ETHER_ADDR_LEN]; /* nexthop's MAC address */
71};
72
73struct l2t_data {
74	unsigned int nentries;      /* number of entries */
75	struct l2t_entry *rover;    /* starting point for next allocation */
76	volatile uint32_t nfree;    /* number of free entries */
77	struct rwlock lock;
78	struct l2t_entry l2tab[0];
79};
80
81void t3_l2e_free(struct l2t_data *, struct l2t_entry *e);
82void t3_l2_update(struct toedev *tod, struct ifnet *ifp, struct sockaddr *sa,
83    uint8_t *lladdr, uint16_t vtag);
84struct l2t_entry *t3_l2t_get(struct port_info *, struct ifnet *,
85    struct sockaddr *);
86int t3_l2t_send_slow(struct adapter *, struct mbuf *, struct l2t_entry *);
87struct l2t_data *t3_init_l2t(unsigned int);
88void t3_free_l2t(struct l2t_data *);
89void t3_init_l2t_cpl_handlers(struct adapter *);
90
91static inline int
92l2t_send(struct adapter *sc, struct mbuf *m, struct l2t_entry *e)
93{
94	if (__predict_true(e->state == L2T_STATE_VALID))
95		return t3_offload_tx(sc, m);
96	else
97		return t3_l2t_send_slow(sc, m, e);
98}
99
100static inline void
101l2t_release(struct l2t_data *d, struct l2t_entry *e)
102{
103	if (atomic_fetchadd_int(&e->refcnt, -1) == 1) /* 1 -> 0 transition */
104		atomic_add_int(&d->nfree, 1);
105}
106
107static inline void
108l2t_hold(struct l2t_data *d, struct l2t_entry *e)
109{
110	if (atomic_fetchadd_int(&e->refcnt, 1) == 0)  /* 0 -> 1 transition */
111		atomic_add_int(&d->nfree, -1);
112}
113
114#endif
115