xref: /freebsd-11-stable/sys/dev/cxgbe/t4_l2t.c (revision f15004e1763d999a6f5b934fd426254f79943b56)
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$");
28 
29 #include "opt_inet.h"
30 #include "opt_inet6.h"
31 
32 #include <sys/param.h>
33 #include <sys/eventhandler.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/bus.h>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 #include <sys/rwlock.h>
41 #include <sys/socket.h>
42 #include <sys/sbuf.h>
43 #include <netinet/in.h>
44 
45 #include "common/common.h"
46 #include "common/t4_msg.h"
47 #include "t4_l2t.h"
48 
49 /*
50  * Module locking notes:  There is a RW lock protecting the L2 table as a
51  * whole plus a spinlock per L2T entry.  Entry lookups and allocations happen
52  * under the protection of the table lock, individual entry changes happen
53  * while holding that entry's spinlock.  The table lock nests outside the
54  * entry locks.  Allocations of new entries take the table lock as writers so
55  * no other lookups can happen while allocating new entries.  Entry updates
56  * take the table lock as readers so multiple entries can be updated in
57  * parallel.  An L2T entry can be dropped by decrementing its reference count
58  * and therefore can happen in parallel with entry allocation but no entry
59  * can change state or increment its ref count during allocation as both of
60  * these perform lookups.
61  *
62  * Note: We do not take references to ifnets in this module because both
63  * the TOE and the sockets already hold references to the interfaces and the
64  * lifetime of an L2T entry is fully contained in the lifetime of the TOE.
65  */
66 
67 /*
68  * Allocate a free L2T entry.  Must be called with l2t_data.lock held.
69  */
70 struct l2t_entry *
t4_alloc_l2e(struct l2t_data * d)71 t4_alloc_l2e(struct l2t_data *d)
72 {
73 	struct l2t_entry *end, *e, **p;
74 
75 	rw_assert(&d->lock, RA_WLOCKED);
76 
77 	if (!atomic_load_acq_int(&d->nfree))
78 		return (NULL);
79 
80 	/* there's definitely a free entry */
81 	for (e = d->rover, end = &d->l2tab[d->l2t_size]; e != end; ++e)
82 		if (atomic_load_acq_int(&e->refcnt) == 0)
83 			goto found;
84 
85 	for (e = d->l2tab; atomic_load_acq_int(&e->refcnt); ++e)
86 		continue;
87 found:
88 	d->rover = e + 1;
89 	atomic_subtract_int(&d->nfree, 1);
90 
91 	/*
92 	 * The entry we found may be an inactive entry that is
93 	 * presently in the hash table.  We need to remove it.
94 	 */
95 	if (e->state < L2T_STATE_SWITCHING) {
96 		for (p = &d->l2tab[e->hash].first; *p; p = &(*p)->next) {
97 			if (*p == e) {
98 				*p = e->next;
99 				e->next = NULL;
100 				break;
101 			}
102 		}
103 	}
104 
105 	e->state = L2T_STATE_UNUSED;
106 	return (e);
107 }
108 
109 static struct l2t_entry *
find_or_alloc_l2e(struct l2t_data * d,uint16_t vlan,uint8_t port,uint8_t * dmac)110 find_or_alloc_l2e(struct l2t_data *d, uint16_t vlan, uint8_t port, uint8_t *dmac)
111 {
112 	struct l2t_entry *end, *e, **p;
113 	struct l2t_entry *first_free = NULL;
114 
115 	for (e = &d->l2tab[0], end = &d->l2tab[d->l2t_size]; e != end; ++e) {
116 		if (atomic_load_acq_int(&e->refcnt) == 0) {
117 			if (!first_free)
118 				first_free = e;
119 		} else if (e->state == L2T_STATE_SWITCHING &&
120 		    memcmp(e->dmac, dmac, ETHER_ADDR_LEN) == 0 &&
121 		    e->vlan == vlan && e->lport == port)
122 			return (e);	/* Found existing entry that matches. */
123 	}
124 
125 	if (first_free == NULL)
126 		return (NULL);	/* No match and no room for a new entry. */
127 
128 	/*
129 	 * The entry we found may be an inactive entry that is
130 	 * presently in the hash table.  We need to remove it.
131 	 */
132 	e = first_free;
133 	if (e->state < L2T_STATE_SWITCHING) {
134 		for (p = &d->l2tab[e->hash].first; *p; p = &(*p)->next) {
135 			if (*p == e) {
136 				*p = e->next;
137 				e->next = NULL;
138 				break;
139 			}
140 		}
141 	}
142 	e->state = L2T_STATE_UNUSED;
143 	return (e);
144 }
145 
146 
147 /*
148  * Write an L2T entry.  Must be called with the entry locked.
149  * The write may be synchronous or asynchronous.
150  */
151 int
t4_write_l2e(struct l2t_entry * e,int sync)152 t4_write_l2e(struct l2t_entry *e, int sync)
153 {
154 	struct sge_wrq *wrq;
155 	struct adapter *sc;
156 	struct wrq_cookie cookie;
157 	struct cpl_l2t_write_req *req;
158 	int idx;
159 
160 	mtx_assert(&e->lock, MA_OWNED);
161 	MPASS(e->wrq != NULL);
162 
163 	wrq = e->wrq;
164 	sc = wrq->adapter;
165 
166 	req = start_wrq_wr(wrq, howmany(sizeof(*req), 16), &cookie);
167 	if (req == NULL)
168 		return (ENOMEM);
169 
170 	idx = e->idx + sc->vres.l2t.start;
171 	INIT_TP_WR(req, 0);
172 	OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_L2T_WRITE_REQ, idx |
173 	    V_SYNC_WR(sync) | V_TID_QID(e->iqid)));
174 	req->params = htons(V_L2T_W_PORT(e->lport) | V_L2T_W_NOREPLY(!sync));
175 	req->l2t_idx = htons(idx);
176 	req->vlan = htons(e->vlan);
177 	memcpy(req->dst_mac, e->dmac, sizeof(req->dst_mac));
178 
179 	commit_wrq_wr(wrq, req, &cookie);
180 
181 	if (sync && e->state != L2T_STATE_SWITCHING)
182 		e->state = L2T_STATE_SYNC_WRITE;
183 
184 	return (0);
185 }
186 
187 /*
188  * Allocate an L2T entry for use by a switching rule.  Such need to be
189  * explicitly freed and while busy they are not on any hash chain, so normal
190  * address resolution updates do not see them.
191  */
192 struct l2t_entry *
t4_l2t_alloc_switching(struct adapter * sc,uint16_t vlan,uint8_t port,uint8_t * eth_addr)193 t4_l2t_alloc_switching(struct adapter *sc, uint16_t vlan, uint8_t port,
194     uint8_t *eth_addr)
195 {
196 	struct l2t_data *d = sc->l2t;
197 	struct l2t_entry *e;
198 	int rc;
199 
200 	rw_wlock(&d->lock);
201 	e = find_or_alloc_l2e(d, vlan, port, eth_addr);
202 	if (e) {
203 		if (atomic_load_acq_int(&e->refcnt) == 0) {
204 			mtx_lock(&e->lock);    /* avoid race with t4_l2t_free */
205 			e->wrq = &sc->sge.ctrlq[0];
206 			e->iqid = sc->sge.fwq.abs_id;
207 			e->state = L2T_STATE_SWITCHING;
208 			e->vlan = vlan;
209 			e->lport = port;
210 			memcpy(e->dmac, eth_addr, ETHER_ADDR_LEN);
211 			atomic_store_rel_int(&e->refcnt, 1);
212 			atomic_subtract_int(&d->nfree, 1);
213 			rc = t4_write_l2e(e, 0);
214 			mtx_unlock(&e->lock);
215 			if (rc != 0)
216 				e = NULL;
217 		} else {
218 			MPASS(e->vlan == vlan);
219 			MPASS(e->lport == port);
220 			atomic_add_int(&e->refcnt, 1);
221 		}
222 	}
223 	rw_wunlock(&d->lock);
224 	return (e);
225 }
226 
227 int
t4_init_l2t(struct adapter * sc,int flags)228 t4_init_l2t(struct adapter *sc, int flags)
229 {
230 	int i, l2t_size;
231 	struct l2t_data *d;
232 
233 	l2t_size = sc->vres.l2t.size;
234 	if (l2t_size < 2)	/* At least 1 bucket for IP and 1 for IPv6 */
235 		return (EINVAL);
236 
237 	d = malloc(sizeof(*d) + l2t_size * sizeof (struct l2t_entry), M_CXGBE,
238 	    M_ZERO | flags);
239 	if (!d)
240 		return (ENOMEM);
241 
242 	d->l2t_size = l2t_size;
243 	d->rover = d->l2tab;
244 	atomic_store_rel_int(&d->nfree, l2t_size);
245 	rw_init(&d->lock, "L2T");
246 
247 	for (i = 0; i < l2t_size; i++) {
248 		struct l2t_entry *e = &d->l2tab[i];
249 
250 		e->idx = i;
251 		e->state = L2T_STATE_UNUSED;
252 		mtx_init(&e->lock, "L2T_E", NULL, MTX_DEF);
253 		STAILQ_INIT(&e->wr_list);
254 		atomic_store_rel_int(&e->refcnt, 0);
255 	}
256 
257 	sc->l2t = d;
258 
259 	return (0);
260 }
261 
262 int
t4_free_l2t(struct l2t_data * d)263 t4_free_l2t(struct l2t_data *d)
264 {
265 	int i;
266 
267 	for (i = 0; i < d->l2t_size; i++)
268 		mtx_destroy(&d->l2tab[i].lock);
269 	rw_destroy(&d->lock);
270 	free(d, M_CXGBE);
271 
272 	return (0);
273 }
274 
275 int
do_l2t_write_rpl(struct sge_iq * iq,const struct rss_header * rss,struct mbuf * m)276 do_l2t_write_rpl(struct sge_iq *iq, const struct rss_header *rss,
277     struct mbuf *m)
278 {
279 	const struct cpl_l2t_write_rpl *rpl = (const void *)(rss + 1);
280 	unsigned int tid = GET_TID(rpl);
281 	unsigned int idx = tid % L2T_SIZE;
282 
283 	if (__predict_false(rpl->status != CPL_ERR_NONE)) {
284 		log(LOG_ERR,
285 		    "Unexpected L2T_WRITE_RPL (%u) for entry at hw_idx %u\n",
286 		    rpl->status, idx);
287 		return (EINVAL);
288 	}
289 
290 	return (0);
291 }
292 
293 static inline unsigned int
vlan_prio(const struct l2t_entry * e)294 vlan_prio(const struct l2t_entry *e)
295 {
296 	return e->vlan >> 13;
297 }
298 
299 static char
l2e_state(const struct l2t_entry * e)300 l2e_state(const struct l2t_entry *e)
301 {
302 	switch (e->state) {
303 	case L2T_STATE_VALID: return 'V';  /* valid, fast-path entry */
304 	case L2T_STATE_STALE: return 'S';  /* needs revalidation, but usable */
305 	case L2T_STATE_SYNC_WRITE: return 'W';
306 	case L2T_STATE_RESOLVING: return STAILQ_EMPTY(&e->wr_list) ? 'R' : 'A';
307 	case L2T_STATE_SWITCHING: return 'X';
308 	default: return 'U';
309 	}
310 }
311 
312 int
sysctl_l2t(SYSCTL_HANDLER_ARGS)313 sysctl_l2t(SYSCTL_HANDLER_ARGS)
314 {
315 	struct adapter *sc = arg1;
316 	struct l2t_data *l2t = sc->l2t;
317 	struct l2t_entry *e;
318 	struct sbuf *sb;
319 	int rc, i, header = 0;
320 	char ip[INET6_ADDRSTRLEN];
321 
322 	if (l2t == NULL)
323 		return (ENXIO);
324 
325 	rc = sysctl_wire_old_buffer(req, 0);
326 	if (rc != 0)
327 		return (rc);
328 
329 	sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
330 	if (sb == NULL)
331 		return (ENOMEM);
332 
333 	e = &l2t->l2tab[0];
334 	for (i = 0; i < l2t->l2t_size; i++, e++) {
335 		mtx_lock(&e->lock);
336 		if (e->state == L2T_STATE_UNUSED)
337 			goto skip;
338 
339 		if (header == 0) {
340 			sbuf_printf(sb, " Idx IP address      "
341 			    "Ethernet address  VLAN/P LP State Users Port");
342 			header = 1;
343 		}
344 		if (e->state == L2T_STATE_SWITCHING)
345 			ip[0] = 0;
346 		else {
347 			inet_ntop(e->ipv6 ? AF_INET6 : AF_INET, &e->addr[0],
348 			    &ip[0], sizeof(ip));
349 		}
350 
351 		/*
352 		 * XXX: IPv6 addresses may not align properly in the output.
353 		 */
354 		sbuf_printf(sb, "\n%4u %-15s %02x:%02x:%02x:%02x:%02x:%02x %4d"
355 			   " %u %2u   %c   %5u %s",
356 			   e->idx, ip, e->dmac[0], e->dmac[1], e->dmac[2],
357 			   e->dmac[3], e->dmac[4], e->dmac[5],
358 			   e->vlan & 0xfff, vlan_prio(e), e->lport,
359 			   l2e_state(e), atomic_load_acq_int(&e->refcnt),
360 			   e->ifp ? e->ifp->if_xname : "-");
361 skip:
362 		mtx_unlock(&e->lock);
363 	}
364 
365 	rc = sbuf_finish(sb);
366 	sbuf_delete(sb);
367 
368 	return (rc);
369 }
370