t4_l2t.c revision 237263
1/*-
2 * Copyright (c) 2012 Chelsio Communications, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26#include <sys/cdefs.h>
27__FBSDID("$FreeBSD: head/sys/dev/cxgbe/t4_l2t.c 237263 2012-06-19 07:34:13Z np $");
28
29#include "opt_inet.h"
30
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/kernel.h>
34#include <sys/module.h>
35#include <sys/bus.h>
36#include <sys/lock.h>
37#include <sys/mutex.h>
38#include <sys/rwlock.h>
39#include <sys/socket.h>
40#include <sys/sbuf.h>
41#include <netinet/in.h>
42
43#include "common/common.h"
44#include "common/jhash.h"
45#include "common/t4_msg.h"
46#include "t4_l2t.h"
47
48/*
49 * Module locking notes:  There is a RW lock protecting the L2 table as a
50 * whole plus a spinlock per L2T entry.  Entry lookups and allocations happen
51 * under the protection of the table lock, individual entry changes happen
52 * while holding that entry's spinlock.  The table lock nests outside the
53 * entry locks.  Allocations of new entries take the table lock as writers so
54 * no other lookups can happen while allocating new entries.  Entry updates
55 * take the table lock as readers so multiple entries can be updated in
56 * parallel.  An L2T entry can be dropped by decrementing its reference count
57 * and therefore can happen in parallel with entry allocation but no entry
58 * can change state or increment its ref count during allocation as both of
59 * these perform lookups.
60 *
61 * Note: We do not take refereces to ifnets in this module because both
62 * the TOE and the sockets already hold references to the interfaces and the
63 * lifetime of an L2T entry is fully contained in the lifetime of the TOE.
64 */
65
66/*
67 * Allocate a free L2T entry.  Must be called with l2t_data.lock held.
68 */
69struct l2t_entry *
70t4_alloc_l2e(struct l2t_data *d)
71{
72	struct l2t_entry *end, *e, **p;
73
74	rw_assert(&d->lock, RA_WLOCKED);
75
76	if (!atomic_load_acq_int(&d->nfree))
77		return (NULL);
78
79	/* there's definitely a free entry */
80	for (e = d->rover, end = &d->l2tab[L2T_SIZE]; e != end; ++e)
81		if (atomic_load_acq_int(&e->refcnt) == 0)
82			goto found;
83
84	for (e = d->l2tab; atomic_load_acq_int(&e->refcnt); ++e)
85		continue;
86found:
87	d->rover = e + 1;
88	atomic_subtract_int(&d->nfree, 1);
89
90	/*
91	 * The entry we found may be an inactive entry that is
92	 * presently in the hash table.  We need to remove it.
93	 */
94	if (e->state < L2T_STATE_SWITCHING) {
95		for (p = &d->l2tab[e->hash].first; *p; p = &(*p)->next) {
96			if (*p == e) {
97				*p = e->next;
98				e->next = NULL;
99				break;
100			}
101		}
102	}
103
104	e->state = L2T_STATE_UNUSED;
105	return (e);
106}
107
108/*
109 * Write an L2T entry.  Must be called with the entry locked.
110 * The write may be synchronous or asynchronous.
111 */
112int
113t4_write_l2e(struct adapter *sc, struct l2t_entry *e, int sync)
114{
115	struct wrqe *wr;
116	struct cpl_l2t_write_req *req;
117
118	mtx_assert(&e->lock, MA_OWNED);
119
120	wr = alloc_wrqe(sizeof(*req), &sc->sge.mgmtq);
121	if (wr == NULL)
122		return (ENOMEM);
123	req = wrtod(wr);
124
125	INIT_TP_WR(req, 0);
126	OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_L2T_WRITE_REQ, e->idx |
127	    V_SYNC_WR(sync) | V_TID_QID(sc->sge.fwq.abs_id)));
128	req->params = htons(V_L2T_W_PORT(e->lport) | V_L2T_W_NOREPLY(!sync));
129	req->l2t_idx = htons(e->idx);
130	req->vlan = htons(e->vlan);
131	memcpy(req->dst_mac, e->dmac, sizeof(req->dst_mac));
132
133	t4_wrq_tx(sc, wr);
134
135	if (sync && e->state != L2T_STATE_SWITCHING)
136		e->state = L2T_STATE_SYNC_WRITE;
137
138	return (0);
139}
140
141/*
142 * Allocate an L2T entry for use by a switching rule.  Such need to be
143 * explicitly freed and while busy they are not on any hash chain, so normal
144 * address resolution updates do not see them.
145 */
146struct l2t_entry *
147t4_l2t_alloc_switching(struct l2t_data *d)
148{
149	struct l2t_entry *e;
150
151	rw_rlock(&d->lock);
152	e = t4_alloc_l2e(d);
153	if (e) {
154		mtx_lock(&e->lock);          /* avoid race with t4_l2t_free */
155		e->state = L2T_STATE_SWITCHING;
156		atomic_store_rel_int(&e->refcnt, 1);
157		mtx_unlock(&e->lock);
158	}
159	rw_runlock(&d->lock);
160	return e;
161}
162
163/*
164 * Sets/updates the contents of a switching L2T entry that has been allocated
165 * with an earlier call to @t4_l2t_alloc_switching.
166 */
167int
168t4_l2t_set_switching(struct adapter *sc, struct l2t_entry *e, uint16_t vlan,
169    uint8_t port, uint8_t *eth_addr)
170{
171	int rc;
172
173	e->vlan = vlan;
174	e->lport = port;
175	memcpy(e->dmac, eth_addr, ETHER_ADDR_LEN);
176	mtx_lock(&e->lock);
177	rc = t4_write_l2e(sc, e, 0);
178	mtx_unlock(&e->lock);
179	return (rc);
180}
181
182int
183t4_init_l2t(struct adapter *sc, int flags)
184{
185	int i;
186	struct l2t_data *d;
187
188	d = malloc(sizeof(*d), M_CXGBE, M_ZERO | flags);
189	if (!d)
190		return (ENOMEM);
191
192	d->rover = d->l2tab;
193	atomic_store_rel_int(&d->nfree, L2T_SIZE);
194	rw_init(&d->lock, "L2T");
195
196	for (i = 0; i < L2T_SIZE; i++) {
197		struct l2t_entry *e = &d->l2tab[i];
198
199		e->idx = i;
200		e->state = L2T_STATE_UNUSED;
201		mtx_init(&e->lock, "L2T_E", NULL, MTX_DEF);
202		STAILQ_INIT(&e->wr_list);
203		atomic_store_rel_int(&e->refcnt, 0);
204	}
205
206	sc->l2t = d;
207	t4_register_cpl_handler(sc, CPL_L2T_WRITE_RPL, do_l2t_write_rpl);
208
209	return (0);
210}
211
212int
213t4_free_l2t(struct l2t_data *d)
214{
215	int i;
216
217	for (i = 0; i < L2T_SIZE; i++)
218		mtx_destroy(&d->l2tab[i].lock);
219	rw_destroy(&d->lock);
220	free(d, M_CXGBE);
221
222	return (0);
223}
224
225int
226do_l2t_write_rpl(struct sge_iq *iq, const struct rss_header *rss,
227    struct mbuf *m)
228{
229	const struct cpl_l2t_write_rpl *rpl = (const void *)(rss + 1);
230	unsigned int tid = GET_TID(rpl);
231	unsigned int idx = tid & (L2T_SIZE - 1);
232
233	if (__predict_false(rpl->status != CPL_ERR_NONE)) {
234		log(LOG_ERR,
235		    "Unexpected L2T_WRITE_RPL status %u for entry %u\n",
236		    rpl->status, idx);
237		return (EINVAL);
238	}
239
240	return (0);
241}
242
243#ifdef SBUF_DRAIN
244static inline unsigned int
245vlan_prio(const struct l2t_entry *e)
246{
247	return e->vlan >> 13;
248}
249
250static char
251l2e_state(const struct l2t_entry *e)
252{
253	switch (e->state) {
254	case L2T_STATE_VALID: return 'V';  /* valid, fast-path entry */
255	case L2T_STATE_STALE: return 'S';  /* needs revalidation, but usable */
256	case L2T_STATE_SYNC_WRITE: return 'W';
257	case L2T_STATE_RESOLVING: return STAILQ_EMPTY(&e->wr_list) ? 'R' : 'A';
258	case L2T_STATE_SWITCHING: return 'X';
259	default: return 'U';
260	}
261}
262
263int
264sysctl_l2t(SYSCTL_HANDLER_ARGS)
265{
266	struct adapter *sc = arg1;
267	struct l2t_data *l2t = sc->l2t;
268	struct l2t_entry *e;
269	struct sbuf *sb;
270	int rc, i, header = 0;
271	char ip[60];
272
273	if (l2t == NULL)
274		return (ENXIO);
275
276	rc = sysctl_wire_old_buffer(req, 0);
277	if (rc != 0)
278		return (rc);
279
280	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
281	if (sb == NULL)
282		return (ENOMEM);
283
284	e = &l2t->l2tab[0];
285	for (i = 0; i < L2T_SIZE; i++, e++) {
286		mtx_lock(&e->lock);
287		if (e->state == L2T_STATE_UNUSED)
288			goto skip;
289
290		if (header == 0) {
291			sbuf_printf(sb, " Idx IP address      "
292			    "Ethernet address  VLAN/P LP State Users Port");
293			header = 1;
294		}
295		if (e->state == L2T_STATE_SWITCHING)
296			ip[0] = 0;
297		else
298			snprintf(ip, sizeof(ip), "%s",
299			    inet_ntoa(*(struct in_addr *)&e->addr));
300
301		/* XXX: e->ifp may not be around */
302		sbuf_printf(sb, "\n%4u %-15s %02x:%02x:%02x:%02x:%02x:%02x %4d"
303			   " %u %2u   %c   %5u %s",
304			   e->idx, ip, e->dmac[0], e->dmac[1], e->dmac[2],
305			   e->dmac[3], e->dmac[4], e->dmac[5],
306			   e->vlan & 0xfff, vlan_prio(e), e->lport,
307			   l2e_state(e), atomic_load_acq_int(&e->refcnt),
308			   e->ifp->if_xname);
309skip:
310		mtx_unlock(&e->lock);
311	}
312
313	rc = sbuf_finish(sb);
314	sbuf_delete(sb);
315
316	return (rc);
317}
318#endif
319