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