t4_l2t.c revision 302339
1222509Snp/*- 2237263Snp * Copyright (c) 2012 Chelsio Communications, Inc. 3222509Snp * All rights reserved. 4222509Snp * 5222509Snp * Redistribution and use in source and binary forms, with or without 6222509Snp * modification, are permitted provided that the following conditions 7222509Snp * are met: 8222509Snp * 1. Redistributions of source code must retain the above copyright 9222509Snp * notice, this list of conditions and the following disclaimer. 10222509Snp * 2. Redistributions in binary form must reproduce the above copyright 11222509Snp * notice, this list of conditions and the following disclaimer in the 12222509Snp * documentation and/or other materials provided with the distribution. 13222509Snp * 14222509Snp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15222509Snp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16222509Snp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17222509Snp * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18222509Snp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19222509Snp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20222509Snp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21222509Snp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22222509Snp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23222509Snp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24222509Snp * SUCH DAMAGE. 25222509Snp */ 26222509Snp#include <sys/cdefs.h> 27222509Snp__FBSDID("$FreeBSD: head/sys/dev/cxgbe/t4_l2t.c 302339 2016-07-05 01:29:24Z np $"); 28222509Snp 29222509Snp#include "opt_inet.h" 30237819Snp#include "opt_inet6.h" 31222509Snp 32222509Snp#include <sys/param.h> 33257241Sglebius#include <sys/eventhandler.h> 34222509Snp#include <sys/systm.h> 35222509Snp#include <sys/kernel.h> 36222509Snp#include <sys/module.h> 37222509Snp#include <sys/bus.h> 38222509Snp#include <sys/lock.h> 39222509Snp#include <sys/mutex.h> 40222509Snp#include <sys/rwlock.h> 41222509Snp#include <sys/socket.h> 42228561Snp#include <sys/sbuf.h> 43222509Snp#include <netinet/in.h> 44222509Snp 45222509Snp#include "common/common.h" 46222509Snp#include "common/t4_msg.h" 47222509Snp#include "t4_l2t.h" 48222509Snp 49228561Snp/* 50228561Snp * Module locking notes: There is a RW lock protecting the L2 table as a 51228561Snp * whole plus a spinlock per L2T entry. Entry lookups and allocations happen 52228561Snp * under the protection of the table lock, individual entry changes happen 53228561Snp * while holding that entry's spinlock. The table lock nests outside the 54228561Snp * entry locks. Allocations of new entries take the table lock as writers so 55228561Snp * no other lookups can happen while allocating new entries. Entry updates 56228561Snp * take the table lock as readers so multiple entries can be updated in 57228561Snp * parallel. An L2T entry can be dropped by decrementing its reference count 58228561Snp * and therefore can happen in parallel with entry allocation but no entry 59228561Snp * can change state or increment its ref count during allocation as both of 60228561Snp * these perform lookups. 61228561Snp * 62298955Spfg * Note: We do not take references to ifnets in this module because both 63228561Snp * the TOE and the sockets already hold references to the interfaces and the 64228561Snp * lifetime of an L2T entry is fully contained in the lifetime of the TOE. 65228561Snp */ 66228561Snp 67222509Snp/* 68228561Snp * Allocate a free L2T entry. Must be called with l2t_data.lock held. 69222509Snp */ 70237263Snpstruct l2t_entry * 71237263Snpt4_alloc_l2e(struct l2t_data *d) 72228561Snp{ 73228561Snp struct l2t_entry *end, *e, **p; 74228561Snp 75228561Snp rw_assert(&d->lock, RA_WLOCKED); 76228561Snp 77228561Snp if (!atomic_load_acq_int(&d->nfree)) 78228561Snp return (NULL); 79228561Snp 80228561Snp /* there's definitely a free entry */ 81245434Snp for (e = d->rover, end = &d->l2tab[d->l2t_size]; e != end; ++e) 82228561Snp if (atomic_load_acq_int(&e->refcnt) == 0) 83228561Snp goto found; 84228561Snp 85237263Snp for (e = d->l2tab; atomic_load_acq_int(&e->refcnt); ++e) 86237263Snp continue; 87228561Snpfound: 88228561Snp d->rover = e + 1; 89228561Snp atomic_subtract_int(&d->nfree, 1); 90228561Snp 91228561Snp /* 92228561Snp * The entry we found may be an inactive entry that is 93228561Snp * presently in the hash table. We need to remove it. 94228561Snp */ 95228561Snp if (e->state < L2T_STATE_SWITCHING) { 96228561Snp for (p = &d->l2tab[e->hash].first; *p; p = &(*p)->next) { 97228561Snp if (*p == e) { 98228561Snp *p = e->next; 99228561Snp e->next = NULL; 100228561Snp break; 101228561Snp } 102228561Snp } 103228561Snp } 104228561Snp 105228561Snp e->state = L2T_STATE_UNUSED; 106228561Snp return (e); 107228561Snp} 108228561Snp 109228561Snp/* 110228561Snp * Write an L2T entry. Must be called with the entry locked. 111228561Snp * The write may be synchronous or asynchronous. 112228561Snp */ 113237263Snpint 114302339Snpt4_write_l2e(struct l2t_entry *e, int sync) 115228561Snp{ 116302339Snp struct sge_wrq *wrq; 117302339Snp struct adapter *sc; 118276485Snp struct wrq_cookie cookie; 119228561Snp struct cpl_l2t_write_req *req; 120302339Snp int idx; 121228561Snp 122228561Snp mtx_assert(&e->lock, MA_OWNED); 123302339Snp MPASS(e->wrq != NULL); 124228561Snp 125302339Snp wrq = e->wrq; 126302339Snp sc = wrq->adapter; 127302339Snp 128302339Snp req = start_wrq_wr(wrq, howmany(sizeof(*req), 16), &cookie); 129276485Snp if (req == NULL) 130228561Snp return (ENOMEM); 131228561Snp 132302339Snp idx = e->idx + sc->vres.l2t.start; 133228561Snp INIT_TP_WR(req, 0); 134245434Snp OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_L2T_WRITE_REQ, idx | 135302339Snp V_SYNC_WR(sync) | V_TID_QID(e->iqid))); 136228561Snp req->params = htons(V_L2T_W_PORT(e->lport) | V_L2T_W_NOREPLY(!sync)); 137245434Snp req->l2t_idx = htons(idx); 138228561Snp req->vlan = htons(e->vlan); 139228561Snp memcpy(req->dst_mac, e->dmac, sizeof(req->dst_mac)); 140228561Snp 141302339Snp commit_wrq_wr(wrq, req, &cookie); 142228561Snp 143228561Snp if (sync && e->state != L2T_STATE_SWITCHING) 144228561Snp e->state = L2T_STATE_SYNC_WRITE; 145228561Snp 146228561Snp return (0); 147228561Snp} 148228561Snp 149228561Snp/* 150228561Snp * Allocate an L2T entry for use by a switching rule. Such need to be 151228561Snp * explicitly freed and while busy they are not on any hash chain, so normal 152228561Snp * address resolution updates do not see them. 153228561Snp */ 154228561Snpstruct l2t_entry * 155228561Snpt4_l2t_alloc_switching(struct l2t_data *d) 156228561Snp{ 157228561Snp struct l2t_entry *e; 158228561Snp 159244551Snp rw_wlock(&d->lock); 160237263Snp e = t4_alloc_l2e(d); 161228561Snp if (e) { 162228561Snp mtx_lock(&e->lock); /* avoid race with t4_l2t_free */ 163228561Snp e->state = L2T_STATE_SWITCHING; 164228561Snp atomic_store_rel_int(&e->refcnt, 1); 165228561Snp mtx_unlock(&e->lock); 166228561Snp } 167244551Snp rw_wunlock(&d->lock); 168228561Snp return e; 169228561Snp} 170228561Snp 171228561Snp/* 172228561Snp * Sets/updates the contents of a switching L2T entry that has been allocated 173228561Snp * with an earlier call to @t4_l2t_alloc_switching. 174228561Snp */ 175228561Snpint 176228561Snpt4_l2t_set_switching(struct adapter *sc, struct l2t_entry *e, uint16_t vlan, 177228561Snp uint8_t port, uint8_t *eth_addr) 178228561Snp{ 179228561Snp int rc; 180228561Snp 181228561Snp e->vlan = vlan; 182228561Snp e->lport = port; 183302339Snp e->wrq = &sc->sge.mgmtq; 184302339Snp e->iqid = sc->sge.fwq.abs_id; 185228561Snp memcpy(e->dmac, eth_addr, ETHER_ADDR_LEN); 186228561Snp mtx_lock(&e->lock); 187302339Snp rc = t4_write_l2e(e, 0); 188228561Snp mtx_unlock(&e->lock); 189228561Snp return (rc); 190228561Snp} 191228561Snp 192228561Snpint 193228561Snpt4_init_l2t(struct adapter *sc, int flags) 194228561Snp{ 195245434Snp int i, l2t_size; 196228561Snp struct l2t_data *d; 197228561Snp 198245434Snp l2t_size = sc->vres.l2t.size; 199245434Snp if (l2t_size < 2) /* At least 1 bucket for IP and 1 for IPv6 */ 200245434Snp return (EINVAL); 201245434Snp 202245434Snp d = malloc(sizeof(*d) + l2t_size * sizeof (struct l2t_entry), M_CXGBE, 203245434Snp M_ZERO | flags); 204228561Snp if (!d) 205228561Snp return (ENOMEM); 206228561Snp 207245434Snp d->l2t_size = l2t_size; 208228561Snp d->rover = d->l2tab; 209245434Snp atomic_store_rel_int(&d->nfree, l2t_size); 210228561Snp rw_init(&d->lock, "L2T"); 211228561Snp 212245434Snp for (i = 0; i < l2t_size; i++) { 213237263Snp struct l2t_entry *e = &d->l2tab[i]; 214237263Snp 215237263Snp e->idx = i; 216237263Snp e->state = L2T_STATE_UNUSED; 217237263Snp mtx_init(&e->lock, "L2T_E", NULL, MTX_DEF); 218237263Snp STAILQ_INIT(&e->wr_list); 219237263Snp atomic_store_rel_int(&e->refcnt, 0); 220228561Snp } 221228561Snp 222228561Snp sc->l2t = d; 223228561Snp 224228561Snp return (0); 225228561Snp} 226228561Snp 227228561Snpint 228228561Snpt4_free_l2t(struct l2t_data *d) 229228561Snp{ 230228561Snp int i; 231228561Snp 232245434Snp for (i = 0; i < d->l2t_size; i++) 233228561Snp mtx_destroy(&d->l2tab[i].lock); 234228561Snp rw_destroy(&d->lock); 235228561Snp free(d, M_CXGBE); 236228561Snp 237228561Snp return (0); 238228561Snp} 239228561Snp 240237263Snpint 241237263Snpdo_l2t_write_rpl(struct sge_iq *iq, const struct rss_header *rss, 242237263Snp struct mbuf *m) 243237263Snp{ 244237263Snp const struct cpl_l2t_write_rpl *rpl = (const void *)(rss + 1); 245237263Snp unsigned int tid = GET_TID(rpl); 246245434Snp unsigned int idx = tid % L2T_SIZE; 247237263Snp 248237263Snp if (__predict_false(rpl->status != CPL_ERR_NONE)) { 249237263Snp log(LOG_ERR, 250245434Snp "Unexpected L2T_WRITE_RPL (%u) for entry at hw_idx %u\n", 251237263Snp rpl->status, idx); 252237263Snp return (EINVAL); 253237263Snp } 254237263Snp 255237263Snp return (0); 256237263Snp} 257237263Snp 258231115Snp#ifdef SBUF_DRAIN 259222509Snpstatic inline unsigned int 260222509Snpvlan_prio(const struct l2t_entry *e) 261222509Snp{ 262222509Snp return e->vlan >> 13; 263222509Snp} 264222509Snp 265228561Snpstatic char 266228561Snpl2e_state(const struct l2t_entry *e) 267228561Snp{ 268228561Snp switch (e->state) { 269228561Snp case L2T_STATE_VALID: return 'V'; /* valid, fast-path entry */ 270228561Snp case L2T_STATE_STALE: return 'S'; /* needs revalidation, but usable */ 271228561Snp case L2T_STATE_SYNC_WRITE: return 'W'; 272237263Snp case L2T_STATE_RESOLVING: return STAILQ_EMPTY(&e->wr_list) ? 'R' : 'A'; 273228561Snp case L2T_STATE_SWITCHING: return 'X'; 274228561Snp default: return 'U'; 275228561Snp } 276228561Snp} 277228561Snp 278228561Snpint 279228561Snpsysctl_l2t(SYSCTL_HANDLER_ARGS) 280228561Snp{ 281228561Snp struct adapter *sc = arg1; 282228561Snp struct l2t_data *l2t = sc->l2t; 283228561Snp struct l2t_entry *e; 284228561Snp struct sbuf *sb; 285228561Snp int rc, i, header = 0; 286245434Snp char ip[INET6_ADDRSTRLEN]; 287228561Snp 288228561Snp if (l2t == NULL) 289228561Snp return (ENXIO); 290228561Snp 291228561Snp rc = sysctl_wire_old_buffer(req, 0); 292228561Snp if (rc != 0) 293228561Snp return (rc); 294228561Snp 295228561Snp sb = sbuf_new_for_sysctl(NULL, NULL, 4096, req); 296228561Snp if (sb == NULL) 297228561Snp return (ENOMEM); 298228561Snp 299228561Snp e = &l2t->l2tab[0]; 300245434Snp for (i = 0; i < l2t->l2t_size; i++, e++) { 301228561Snp mtx_lock(&e->lock); 302228561Snp if (e->state == L2T_STATE_UNUSED) 303228561Snp goto skip; 304228561Snp 305228561Snp if (header == 0) { 306228561Snp sbuf_printf(sb, " Idx IP address " 307228561Snp "Ethernet address VLAN/P LP State Users Port"); 308228561Snp header = 1; 309228561Snp } 310237263Snp if (e->state == L2T_STATE_SWITCHING) 311228561Snp ip[0] = 0; 312245434Snp else { 313245434Snp inet_ntop(e->ipv6 ? AF_INET6 : AF_INET, &e->addr[0], 314245434Snp &ip[0], sizeof(ip)); 315245434Snp } 316228561Snp 317245434Snp /* 318245434Snp * XXX: IPv6 addresses may not align properly in the output. 319245434Snp */ 320228561Snp sbuf_printf(sb, "\n%4u %-15s %02x:%02x:%02x:%02x:%02x:%02x %4d" 321228561Snp " %u %2u %c %5u %s", 322228561Snp e->idx, ip, e->dmac[0], e->dmac[1], e->dmac[2], 323228561Snp e->dmac[3], e->dmac[4], e->dmac[5], 324228561Snp e->vlan & 0xfff, vlan_prio(e), e->lport, 325228561Snp l2e_state(e), atomic_load_acq_int(&e->refcnt), 326302313Snp e->ifp ? e->ifp->if_xname : "-"); 327228561Snpskip: 328228561Snp mtx_unlock(&e->lock); 329228561Snp } 330228561Snp 331228561Snp rc = sbuf_finish(sb); 332228561Snp sbuf_delete(sb); 333228561Snp 334228561Snp return (rc); 335228561Snp} 336231115Snp#endif 337