2 * Copyright (c) 2012 Chelsio Communications, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
30 #include "opt_inet6.h"
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
38 #include <sys/mutex.h>
39 #include <sys/rwlock.h>
40 #include <sys/socket.h>
42 #include <netinet/in.h>
44 #include "common/common.h"
45 #include "common/t4_msg.h"
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.
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.
67 * Allocate a free L2T entry. Must be called with l2t_data.lock held.
70 t4_alloc_l2e(struct l2t_data *d)
72 struct l2t_entry *end, *e, **p;
74 rw_assert(&d->lock, RA_WLOCKED);
76 if (!atomic_load_acq_int(&d->nfree))
79 /* there's definitely a free entry */
80 for (e = d->rover, end = &d->l2tab[d->l2t_size]; e != end; ++e)
81 if (atomic_load_acq_int(&e->refcnt) == 0)
84 for (e = d->l2tab; atomic_load_acq_int(&e->refcnt); ++e)
88 atomic_subtract_int(&d->nfree, 1);
91 * The entry we found may be an inactive entry that is
92 * presently in the hash table. We need to remove it.
94 if (e->state < L2T_STATE_SWITCHING) {
95 for (p = &d->l2tab[e->hash].first; *p; p = &(*p)->next) {
104 e->state = L2T_STATE_UNUSED;
109 * Write an L2T entry. Must be called with the entry locked.
110 * The write may be synchronous or asynchronous.
113 t4_write_l2e(struct l2t_entry *e, int sync)
117 struct wrq_cookie cookie;
118 struct cpl_l2t_write_req *req;
121 mtx_assert(&e->lock, MA_OWNED);
122 MPASS(e->wrq != NULL);
127 req = start_wrq_wr(wrq, howmany(sizeof(*req), 16), &cookie);
131 idx = e->idx + sc->vres.l2t.start;
133 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_L2T_WRITE_REQ, idx |
134 V_SYNC_WR(sync) | V_TID_QID(e->iqid)));
135 req->params = htons(V_L2T_W_PORT(e->lport) | V_L2T_W_NOREPLY(!sync));
136 req->l2t_idx = htons(idx);
137 req->vlan = htons(e->vlan);
138 memcpy(req->dst_mac, e->dmac, sizeof(req->dst_mac));
140 commit_wrq_wr(wrq, req, &cookie);
142 if (sync && e->state != L2T_STATE_SWITCHING)
143 e->state = L2T_STATE_SYNC_WRITE;
149 * Allocate an L2T entry for use by a switching rule. Such need to be
150 * explicitly freed and while busy they are not on any hash chain, so normal
151 * address resolution updates do not see them.
154 t4_l2t_alloc_switching(struct l2t_data *d)
161 mtx_lock(&e->lock); /* avoid race with t4_l2t_free */
162 e->state = L2T_STATE_SWITCHING;
163 atomic_store_rel_int(&e->refcnt, 1);
164 mtx_unlock(&e->lock);
166 rw_wunlock(&d->lock);
171 * Sets/updates the contents of a switching L2T entry that has been allocated
172 * with an earlier call to @t4_l2t_alloc_switching.
175 t4_l2t_set_switching(struct adapter *sc, struct l2t_entry *e, uint16_t vlan,
176 uint8_t port, uint8_t *eth_addr)
182 e->wrq = &sc->sge.mgmtq;
183 e->iqid = sc->sge.fwq.abs_id;
184 memcpy(e->dmac, eth_addr, ETHER_ADDR_LEN);
186 rc = t4_write_l2e(e, 0);
187 mtx_unlock(&e->lock);
192 t4_init_l2t(struct adapter *sc, int flags)
197 l2t_size = sc->vres.l2t.size;
198 if (l2t_size < 2) /* At least 1 bucket for IP and 1 for IPv6 */
201 d = malloc(sizeof(*d) + l2t_size * sizeof (struct l2t_entry), M_CXGBE,
206 d->l2t_size = l2t_size;
208 atomic_store_rel_int(&d->nfree, l2t_size);
209 rw_init(&d->lock, "L2T");
211 for (i = 0; i < l2t_size; i++) {
212 struct l2t_entry *e = &d->l2tab[i];
215 e->state = L2T_STATE_UNUSED;
216 mtx_init(&e->lock, "L2T_E", NULL, MTX_DEF);
217 STAILQ_INIT(&e->wr_list);
218 atomic_store_rel_int(&e->refcnt, 0);
227 t4_free_l2t(struct l2t_data *d)
231 for (i = 0; i < d->l2t_size; i++)
232 mtx_destroy(&d->l2tab[i].lock);
233 rw_destroy(&d->lock);
240 do_l2t_write_rpl(struct sge_iq *iq, const struct rss_header *rss,
243 const struct cpl_l2t_write_rpl *rpl = (const void *)(rss + 1);
244 unsigned int tid = GET_TID(rpl);
245 unsigned int idx = tid % L2T_SIZE;
247 if (__predict_false(rpl->status != CPL_ERR_NONE)) {
249 "Unexpected L2T_WRITE_RPL (%u) for entry at hw_idx %u\n",
258 static inline unsigned int
259 vlan_prio(const struct l2t_entry *e)
261 return e->vlan >> 13;
265 l2e_state(const struct l2t_entry *e)
268 case L2T_STATE_VALID: return 'V'; /* valid, fast-path entry */
269 case L2T_STATE_STALE: return 'S'; /* needs revalidation, but usable */
270 case L2T_STATE_SYNC_WRITE: return 'W';
271 case L2T_STATE_RESOLVING: return STAILQ_EMPTY(&e->wr_list) ? 'R' : 'A';
272 case L2T_STATE_SWITCHING: return 'X';
278 sysctl_l2t(SYSCTL_HANDLER_ARGS)
280 struct adapter *sc = arg1;
281 struct l2t_data *l2t = sc->l2t;
284 int rc, i, header = 0;
285 char ip[INET6_ADDRSTRLEN];
290 rc = sysctl_wire_old_buffer(req, 0);
294 sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req);
299 for (i = 0; i < l2t->l2t_size; i++, e++) {
301 if (e->state == L2T_STATE_UNUSED)
305 sbuf_printf(sb, " Idx IP address "
306 "Ethernet address VLAN/P LP State Users Port");
309 if (e->state == L2T_STATE_SWITCHING)
312 inet_ntop(e->ipv6 ? AF_INET6 : AF_INET, &e->addr[0],
317 * XXX: IPv6 addresses may not align properly in the output.
319 sbuf_printf(sb, "\n%4u %-15s %02x:%02x:%02x:%02x:%02x:%02x %4d"
321 e->idx, ip, e->dmac[0], e->dmac[1], e->dmac[2],
322 e->dmac[3], e->dmac[4], e->dmac[5],
323 e->vlan & 0xfff, vlan_prio(e), e->lport,
324 l2e_state(e), atomic_load_acq_int(&e->refcnt),
325 e->ifp ? e->ifp->if_xname : "-");
327 mtx_unlock(&e->lock);
330 rc = sbuf_finish(sb);