#include <sys/cdefs.h>
#include "opt_inet.h"
#include "opt_inet6.h"
#include <sys/param.h>
#include <sys/eventhandler.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/rwlock.h>
#include <sys/socket.h>
#include <sys/sbuf.h>
#include <netinet/in.h>
#include "common/common.h"
#include "common/t4_msg.h"
#include "t4_smt.h"
struct smt_entry *
t4_find_or_alloc_sme(struct smt_data *s, uint8_t *smac)
{
struct smt_entry *end, *e;
struct smt_entry *first_free = NULL;
rw_assert(&s->lock, RA_WLOCKED);
for (e = &s->smtab[0], end = &s->smtab[s->smt_size]; e != end; ++e) {
if (atomic_load_acq_int(&e->refcnt) == 0) {
if (!first_free)
first_free = e;
} else {
if (e->state == SMT_STATE_SWITCHING) {
if (memcmp(e->smac, smac, ETHER_ADDR_LEN) == 0)
goto found_reuse;
}
}
}
if (first_free) {
e = first_free;
goto found;
}
return NULL;
found:
e->state = SMT_STATE_UNUSED;
found_reuse:
atomic_add_int(&e->refcnt, 1);
return e;
}
int
t4_write_sme(struct smt_entry *e)
{
struct smt_data *s;
struct sge_wrq *wrq;
struct adapter *sc;
struct wrq_cookie cookie;
struct cpl_smt_write_req *req;
struct cpl_t6_smt_write_req *t6req;
u8 row;
mtx_assert(&e->lock, MA_OWNED);
MPASS(e->wrq != NULL);
wrq = e->wrq;
sc = wrq->adapter;
MPASS(wrq->adapter != NULL);
s = sc->smt;
if (chip_id(sc) <= CHELSIO_T5) {
req = start_wrq_wr(wrq, howmany(sizeof(*req), 16), &cookie);
if (req == NULL)
return (ENOMEM);
INIT_TP_WR(req, 0);
row = (e->idx >> 1);
if (e->idx & 1) {
req->pfvf1 = 0x0;
memcpy(req->src_mac1, e->smac, ETHER_ADDR_LEN);
req->pfvf0 = 0x0;
memcpy(req->src_mac0, s->smtab[e->idx - 1].smac,
ETHER_ADDR_LEN);
} else {
req->pfvf0 = 0x0;
memcpy(req->src_mac0, e->smac, ETHER_ADDR_LEN);
req->pfvf1 = 0x0;
memcpy(req->src_mac1, s->smtab[e->idx + 1].smac,
ETHER_ADDR_LEN);
}
} else {
t6req = start_wrq_wr(wrq, howmany(sizeof(*t6req), 16), &cookie);
if (t6req == NULL)
return (ENOMEM);
INIT_TP_WR(t6req, 0);
req = (struct cpl_smt_write_req *)t6req;
req->pfvf0 = 0x0;
memcpy(req->src_mac0, s->smtab[e->idx].smac, ETHER_ADDR_LEN);
row = e->idx;
}
OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SMT_WRITE_REQ, e->idx |
V_TID_QID(e->iqid)));
req->params = htonl(V_SMTW_NORPL(0) |
V_SMTW_IDX(row) |
V_SMTW_OVLAN_IDX(0));
commit_wrq_wr(wrq, req, &cookie);
return (0);
}
struct smt_entry *
t4_smt_alloc_switching(struct smt_data *s, uint8_t *smac)
{
struct smt_entry *e;
MPASS(s != NULL);
rw_wlock(&s->lock);
e = t4_find_or_alloc_sme(s, smac);
rw_wunlock(&s->lock);
return e;
}
int
t4_smt_set_switching(struct adapter *sc, struct smt_entry *e, uint16_t pfvf,
uint8_t *smac)
{
int rc = 0;
if (atomic_load_acq_int(&e->refcnt) == 1) {
mtx_lock(&e->lock);
e->wrq = &sc->sge.ctrlq[0];
e->iqid = sc->sge.fwq.abs_id;
e->pfvf = pfvf;
e->state = SMT_STATE_SWITCHING;
memcpy(e->smac, smac, ETHER_ADDR_LEN);
rc = t4_write_sme(e);
mtx_unlock(&e->lock);
}
return (rc);
}
int
t4_init_smt(struct adapter *sc, int flags)
{
int i, smt_size;
struct smt_data *s;
smt_size = SMT_SIZE;
s = malloc(sizeof(*s) + smt_size * sizeof (struct smt_entry), M_CXGBE,
M_ZERO | flags);
if (!s)
return (ENOMEM);
s->smt_size = smt_size;
rw_init(&s->lock, "SMT");
for (i = 0; i < smt_size; i++) {
struct smt_entry *e = &s->smtab[i];
e->idx = i;
e->state = SMT_STATE_UNUSED;
mtx_init(&e->lock, "SMT_E", NULL, MTX_DEF);
atomic_store_rel_int(&e->refcnt, 0);
}
sc->smt = s;
return (0);
}
int
t4_free_smt(struct smt_data *s)
{
int i;
for (i = 0; i < s->smt_size; i++)
mtx_destroy(&s->smtab[i].lock);
rw_destroy(&s->lock);
free(s, M_CXGBE);
return (0);
}
int
do_smt_write_rpl(struct sge_iq *iq, const struct rss_header *rss,
struct mbuf *m)
{
struct adapter *sc = iq->adapter;
const struct cpl_smt_write_rpl *rpl = (const void *)(rss + 1);
unsigned int tid = GET_TID(rpl);
unsigned int smtidx = G_TID_TID(tid);
if (__predict_false(rpl->status != CPL_ERR_NONE)) {
struct smt_entry *e = &sc->smt->smtab[smtidx];
log(LOG_ERR,
"Unexpected SMT_WRITE_RPL (%u) for entry at hw_idx %u\n",
rpl->status, smtidx);
mtx_lock(&e->lock);
e->state = SMT_STATE_ERROR;
mtx_unlock(&e->lock);
return (EINVAL);
}
return (0);
}
static char
smt_state(const struct smt_entry *e)
{
switch (e->state) {
case SMT_STATE_SWITCHING: return 'X';
case SMT_STATE_ERROR: return 'E';
default: return 'U';
}
}
int
sysctl_smt(SYSCTL_HANDLER_ARGS)
{
struct adapter *sc = arg1;
struct smt_data *smt = sc->smt;
struct smt_entry *e;
struct sbuf *sb;
int rc, i, header = 0;
if (smt == NULL)
return (ENXIO);
rc = sysctl_wire_old_buffer(req, 0);
if (rc != 0)
return (rc);
sb = sbuf_new_for_sysctl(NULL, NULL, SMT_SIZE, req);
if (sb == NULL)
return (ENOMEM);
e = &smt->smtab[0];
for (i = 0; i < smt->smt_size; i++, e++) {
mtx_lock(&e->lock);
if (e->state == SMT_STATE_UNUSED)
goto skip;
if (header == 0) {
sbuf_printf(sb, " Idx "
"Ethernet address State Users");
header = 1;
}
sbuf_printf(sb, "\n%4u %02x:%02x:%02x:%02x:%02x:%02x "
"%c %5u",
e->idx, e->smac[0], e->smac[1], e->smac[2],
e->smac[3], e->smac[4], e->smac[5],
smt_state(e), atomic_load_acq_int(&e->refcnt));
skip:
mtx_unlock(&e->lock);
}
rc = sbuf_finish(sb);
sbuf_delete(sb);
return (rc);
}