1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Chelsio Communications.
9 match_entry(struct mps_tcam_entry *entry, const u8 *eth_addr, const u8 *mask)
11 if (!memcmp(eth_addr, entry->eth_addr, RTE_ETHER_ADDR_LEN) &&
12 !memcmp(mask, entry->mask, RTE_ETHER_ADDR_LEN))
17 static int cxgbe_update_free_idx(struct mpstcam_table *t)
19 struct mps_tcam_entry *entry = t->entry;
20 u16 i, next = t->free_idx + 1;
22 if (entry[t->free_idx].state == MPS_ENTRY_UNUSED)
23 /* You are already pointing to a free entry !! */
26 /* loop, till we don't rollback to same index where we started */
27 for (i = next; i != t->free_idx; i++) {
29 /* rollback and search free entry from start */
32 if (entry[i].state == MPS_ENTRY_UNUSED) {
38 return -1; /* table is full */
41 static struct mps_tcam_entry *
42 cxgbe_mpstcam_lookup(struct mpstcam_table *t, const u8 *eth_addr,
45 struct mps_tcam_entry *entry = t->entry;
51 for (i = 0; i < t->size; i++) {
52 if (entry[i].state == MPS_ENTRY_UNUSED ||
53 entry[i].state == MPS_ENTRY_RAWF)
54 continue; /* entry is not being used */
55 if (match_entry(&entry[i], eth_addr, mask))
62 int cxgbe_mpstcam_alloc(struct port_info *pi, const u8 *eth_addr,
65 struct adapter *adap = pi->adapter;
66 struct mpstcam_table *mpstcam = adap->mpstcam;
67 struct mps_tcam_entry *entry;
71 dev_err(adap, "mpstcam table is not available\n");
75 /* If entry already present, return it. */
76 t4_os_write_lock(&mpstcam->lock);
77 entry = cxgbe_mpstcam_lookup(adap->mpstcam, eth_addr, mask);
79 __atomic_add_fetch(&entry->refcnt, 1, __ATOMIC_RELAXED);
80 t4_os_write_unlock(&mpstcam->lock);
85 t4_os_write_unlock(&mpstcam->lock);
86 dev_err(adap, "mps-tcam table is full\n");
90 ret = t4_alloc_raw_mac_filt(adap, pi->viid, eth_addr, mask,
91 mpstcam->free_idx, 0, pi->port_id, false);
93 t4_os_write_unlock(&mpstcam->lock);
97 /* Fill in the new values */
98 entry = &mpstcam->entry[ret];
99 memcpy(entry->eth_addr, eth_addr, RTE_ETHER_ADDR_LEN);
100 memcpy(entry->mask, mask, RTE_ETHER_ADDR_LEN);
101 __atomic_store_n(&entry->refcnt, 1, __ATOMIC_RELAXED);
102 entry->state = MPS_ENTRY_USED;
104 if (cxgbe_update_free_idx(mpstcam))
105 mpstcam->full = true;
107 t4_os_write_unlock(&mpstcam->lock);
111 int cxgbe_mpstcam_modify(struct port_info *pi, int idx, const u8 *addr)
113 struct adapter *adap = pi->adapter;
114 struct mpstcam_table *mpstcam = adap->mpstcam;
115 struct mps_tcam_entry *entry;
119 t4_os_write_lock(&mpstcam->lock);
120 if (idx != -1 && idx >= mpstcam->size) {
121 t4_os_write_unlock(&mpstcam->lock);
125 entry = &mpstcam->entry[idx];
126 /* user wants to modify an existing entry.
127 * verify if entry exists
129 if (entry->state != MPS_ENTRY_USED) {
130 t4_os_write_unlock(&mpstcam->lock);
135 idx = t4_change_mac(adap, adap->mbox, pi->viid, idx, addr, true, true);
137 t4_os_write_unlock(&mpstcam->lock);
141 /* idx can now be different from what user provided */
142 entry = &mpstcam->entry[idx];
143 memcpy(entry->eth_addr, addr, RTE_ETHER_ADDR_LEN);
144 memset(entry->mask, ~0, RTE_ETHER_ADDR_LEN);
145 /* NOTE: we have considered the case that idx returned by t4_change_mac
146 * will be different from the user provided value only if user
147 * provided value is -1
149 if (entry->state == MPS_ENTRY_UNUSED) {
150 __atomic_store_n(&entry->refcnt, 1, __ATOMIC_RELAXED);
151 entry->state = MPS_ENTRY_USED;
154 if (cxgbe_update_free_idx(mpstcam))
155 mpstcam->full = true;
157 t4_os_write_unlock(&mpstcam->lock);
162 * hold appropriate locks while calling this.
164 static inline void reset_mpstcam_entry(struct mps_tcam_entry *entry)
166 memset(entry->eth_addr, 0, RTE_ETHER_ADDR_LEN);
167 memset(entry->mask, 0, RTE_ETHER_ADDR_LEN);
168 __atomic_store_n(&entry->refcnt, 0, __ATOMIC_RELAXED);
169 entry->state = MPS_ENTRY_UNUSED;
173 * ret < 0: fatal error
174 * ret = 0: entry removed in h/w
175 * ret > 0: updated refcount.
177 int cxgbe_mpstcam_remove(struct port_info *pi, u16 idx)
179 struct adapter *adap = pi->adapter;
180 struct mpstcam_table *t = adap->mpstcam;
181 struct mps_tcam_entry *entry;
186 t4_os_write_lock(&t->lock);
187 entry = &t->entry[idx];
188 if (entry->state != MPS_ENTRY_USED) {
189 t4_os_write_unlock(&t->lock);
193 if (__atomic_load_n(&entry->refcnt, __ATOMIC_RELAXED) == 1)
194 ret = t4_free_raw_mac_filt(adap, pi->viid, entry->eth_addr,
195 entry->mask, idx, 1, pi->port_id,
198 ret = __atomic_sub_fetch(&entry->refcnt, 1, __ATOMIC_RELAXED);
201 reset_mpstcam_entry(entry);
202 t->full = false; /* We have atleast 1 free entry */
203 cxgbe_update_free_idx(t);
206 t4_os_write_unlock(&t->lock);
210 int cxgbe_mpstcam_rawf_enable(struct port_info *pi)
212 struct adapter *adap = pi->adapter;
213 struct mps_tcam_entry *entry;
214 struct mpstcam_table *t;
219 if (adap->params.rawf_size == 0 || t == NULL)
222 t4_os_write_lock(&t->lock);
223 rawf_idx = adap->params.rawf_start + pi->port_id;
224 entry = &t->entry[rawf_idx];
225 if (__atomic_load_n(&entry->refcnt, __ATOMIC_RELAXED) == 1)
228 ret = t4_alloc_raw_mac_filt(adap, pi->viid, entry->eth_addr,
229 entry->mask, rawf_idx, 0, pi->port_id,
234 __atomic_store_n(&entry->refcnt, 1, __ATOMIC_RELAXED);
237 t4_os_write_unlock(&t->lock);
241 int cxgbe_mpstcam_rawf_disable(struct port_info *pi)
243 struct adapter *adap = pi->adapter;
244 struct mps_tcam_entry *entry;
245 struct mpstcam_table *t;
250 if (adap->params.rawf_size == 0 || t == NULL)
253 t4_os_write_lock(&t->lock);
254 rawf_idx = adap->params.rawf_start + pi->port_id;
255 entry = &t->entry[rawf_idx];
256 if (__atomic_load_n(&entry->refcnt, __ATOMIC_RELAXED) != 1)
259 ret = t4_free_raw_mac_filt(adap, pi->viid, entry->eth_addr,
260 entry->mask, rawf_idx, 0, pi->port_id,
265 __atomic_store_n(&entry->refcnt, 0, __ATOMIC_RELAXED);
268 t4_os_write_unlock(&t->lock);
272 struct mpstcam_table *t4_init_mpstcam(struct adapter *adap)
274 u16 size = adap->params.arch.mps_tcam_size;
275 struct mpstcam_table *t;
278 t = t4_os_alloc(sizeof(*t) + size * sizeof(struct mps_tcam_entry));
282 t4_os_rwlock_init(&t->lock);
286 for (i = 0; i < size; i++) {
287 reset_mpstcam_entry(&t->entry[i]);
288 t->entry[i].mpstcam = t;
292 /* RAW MAC entries are reserved for match-all wildcard to
293 * match all promiscuous traffic. So, mark them special.
295 for (i = 0; i < adap->params.rawf_size; i++)
296 t->entry[adap->params.rawf_start + i].state = MPS_ENTRY_RAWF;
298 /* first entry is used by chip. this is overwritten only
299 * in t4_cleanup_mpstcam()
301 t->entry[0].state = MPS_ENTRY_USED;
307 void t4_cleanup_mpstcam(struct adapter *adap)
310 t4_os_free(adap->mpstcam);