1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2020 Chelsio Communications.
6 #include "base/common.h"
9 void cxgbe_do_smt_write_rpl(struct adapter *adap,
10 const struct cpl_smt_write_rpl *rpl)
12 unsigned int smtidx = G_TID_TID(GET_TID(rpl));
13 struct smt_data *s = adap->smt;
15 if (unlikely(rpl->status != CPL_ERR_NONE)) {
16 struct smt_entry *e = &s->smtab[smtidx];
19 "Unexpected SMT_WRITE_RPL status %u for entry %u\n",
22 e->state = SMT_STATE_ERROR;
23 t4_os_unlock(&e->lock);
27 static int write_smt_entry(struct rte_eth_dev *dev, struct smt_entry *e)
29 unsigned int port_id = ethdev2pinfo(dev)->port_id;
30 struct adapter *adap = ethdev2adap(dev);
31 struct cpl_t6_smt_write_req *t6req;
32 struct smt_data *s = adap->smt;
33 struct cpl_smt_write_req *req;
34 struct sge_ctrl_txq *ctrlq;
35 struct rte_mbuf *mbuf;
38 ctrlq = &adap->sge.ctrlq[port_id];
39 mbuf = rte_pktmbuf_alloc(ctrlq->mb_pool);
43 if (CHELSIO_CHIP_VERSION(adap->params.chip) <= CHELSIO_T5) {
44 mbuf->data_len = sizeof(*req);
45 mbuf->pkt_len = mbuf->data_len;
47 /* Source MAC Table (SMT) contains 256 SMAC entries
48 * organized in 128 rows of 2 entries each.
50 req = rte_pktmbuf_mtod(mbuf, struct cpl_smt_write_req *);
53 /* Each row contains an SMAC pair.
54 * LSB selects the SMAC entry within a row
58 rte_memcpy(req->src_mac1, e->src_mac,
61 /* fill pfvf0/src_mac0 with entry
62 * at prev index from smt-tab.
65 rte_memcpy(req->src_mac0, s->smtab[e->idx - 1].src_mac,
69 rte_memcpy(req->src_mac0, e->src_mac,
72 /* fill pfvf1/src_mac1 with entry
73 * at next index from smt-tab
76 rte_memcpy(req->src_mac1, s->smtab[e->idx + 1].src_mac,
79 row = (e->hw_idx >> 1);
81 mbuf->data_len = sizeof(*t6req);
82 mbuf->pkt_len = mbuf->data_len;
84 /* Source MAC Table (SMT) contains 256 SMAC entries */
85 t6req = rte_pktmbuf_mtod(mbuf, struct cpl_t6_smt_write_req *);
88 /* fill pfvf0/src_mac0 from smt-tab */
90 rte_memcpy(t6req->src_mac0, s->smtab[e->idx].src_mac,
93 req = (struct cpl_smt_write_req *)t6req;
97 cpu_to_be32(MK_OPCODE_TID(CPL_SMT_WRITE_REQ,
99 V_TID_QID(adap->sge.fw_evtq.abs_id)));
101 req->params = cpu_to_be32(V_SMTW_NORPL(0) |
103 V_SMTW_OVLAN_IDX(0));
104 t4_mgmt_tx(ctrlq, mbuf);
110 * find_or_alloc_smte - Find/Allocate a free SMT entry
112 * @smac: Source MAC address to compare/add
113 * Returns pointer to the SMT entry found/created
115 * Finds/Allocates an SMT entry to be used by switching rule of a filter.
117 static struct smt_entry *find_or_alloc_smte(struct smt_data *s, u8 *smac)
119 struct smt_entry *e, *end, *first_free = NULL;
121 for (e = &s->smtab[0], end = &s->smtab[s->smt_size]; e != end; ++e) {
122 if (!rte_atomic32_read(&e->refcnt)) {
126 if (e->state == SMT_STATE_SWITCHING) {
127 /* This entry is actually in use. See if we can
130 if (!memcmp(e->src_mac, smac,
141 e->state = SMT_STATE_UNUSED;
147 static struct smt_entry *t4_smt_alloc_switching(struct rte_eth_dev *dev,
150 struct adapter *adap = ethdev2adap(dev);
151 struct smt_data *s = adap->smt;
155 t4_os_write_lock(&s->lock);
156 e = find_or_alloc_smte(s, smac);
158 t4_os_lock(&e->lock);
159 if (!rte_atomic32_read(&e->refcnt)) {
161 rte_memcpy(e->src_mac, smac, RTE_ETHER_ADDR_LEN);
162 ret = write_smt_entry(dev, e);
165 memset(e->src_mac, 0, RTE_ETHER_ADDR_LEN);
166 t4_os_unlock(&e->lock);
168 goto out_write_unlock;
170 e->state = SMT_STATE_SWITCHING;
171 rte_atomic32_set(&e->refcnt, 1);
173 rte_atomic32_inc(&e->refcnt);
175 t4_os_unlock(&e->lock);
179 t4_os_write_unlock(&s->lock);
184 * cxgbe_smt_alloc_switching - Allocate an SMT entry for switching rule
185 * @dev: rte_eth_dev pointer
186 * @smac: MAC address to add to SMT
187 * Returns pointer to the SMT entry created
189 * Allocates an SMT entry to be used by switching rule of a filter.
191 struct smt_entry *cxgbe_smt_alloc_switching(struct rte_eth_dev *dev, u8 *smac)
193 return t4_smt_alloc_switching(dev, 0x0, smac);
197 * Initialize Source MAC Table
199 struct smt_data *t4_init_smt(u32 smt_start_idx, u32 smt_size)
204 s = t4_alloc_mem(sizeof(*s) + smt_size * sizeof(struct smt_entry));
208 s->smt_start = smt_start_idx;
209 s->smt_size = smt_size;
210 t4_os_rwlock_init(&s->lock);
212 for (i = 0; i < s->smt_size; ++i) {
214 s->smtab[i].hw_idx = smt_start_idx + i;
215 s->smtab[i].state = SMT_STATE_UNUSED;
216 memset(&s->smtab[i].src_mac, 0, RTE_ETHER_ADDR_LEN);
217 t4_os_lock_init(&s->smtab[i].lock);
218 rte_atomic32_set(&s->smtab[i].refcnt, 0);
224 * Cleanup Source MAC Table
226 void t4_cleanup_smt(struct adapter *adap)
229 t4_os_free(adap->smt);