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 continue; /* entry is not being used */
54 if (match_entry(&entry[i], eth_addr, mask))
61 int cxgbe_mpstcam_alloc(struct port_info *pi, const u8 *eth_addr,
64 struct adapter *adap = pi->adapter;
65 struct mpstcam_table *mpstcam = adap->mpstcam;
66 struct mps_tcam_entry *entry;
70 dev_err(adap, "mpstcam table is not available\n");
74 /* If entry already present, return it. */
75 t4_os_write_lock(&mpstcam->lock);
76 entry = cxgbe_mpstcam_lookup(adap->mpstcam, eth_addr, mask);
78 rte_atomic32_add(&entry->refcnt, 1);
79 t4_os_write_unlock(&mpstcam->lock);
84 t4_os_write_unlock(&mpstcam->lock);
85 dev_err(adap, "mps-tcam table is full\n");
89 ret = t4_alloc_raw_mac_filt(adap, pi->viid, eth_addr, mask,
90 mpstcam->free_idx, 0, pi->port_id, false);
92 t4_os_write_unlock(&mpstcam->lock);
96 /* Fill in the new values */
97 entry = &mpstcam->entry[ret];
98 memcpy(entry->eth_addr, eth_addr, RTE_ETHER_ADDR_LEN);
99 memcpy(entry->mask, mask, RTE_ETHER_ADDR_LEN);
100 rte_atomic32_set(&entry->refcnt, 1);
101 entry->state = MPS_ENTRY_USED;
103 if (cxgbe_update_free_idx(mpstcam))
104 mpstcam->full = true;
106 t4_os_write_unlock(&mpstcam->lock);
110 int cxgbe_mpstcam_modify(struct port_info *pi, int idx, const u8 *addr)
112 struct adapter *adap = pi->adapter;
113 struct mpstcam_table *mpstcam = adap->mpstcam;
114 struct mps_tcam_entry *entry;
118 t4_os_write_lock(&mpstcam->lock);
119 if (idx != -1 && idx >= mpstcam->size) {
120 t4_os_write_unlock(&mpstcam->lock);
124 entry = &mpstcam->entry[idx];
125 /* user wants to modify an existing entry.
126 * verify if entry exists
128 if (entry->state != MPS_ENTRY_USED) {
129 t4_os_write_unlock(&mpstcam->lock);
134 idx = t4_change_mac(adap, adap->mbox, pi->viid, idx, addr, true, true);
136 t4_os_write_unlock(&mpstcam->lock);
140 /* idx can now be different from what user provided */
141 entry = &mpstcam->entry[idx];
142 memcpy(entry->eth_addr, addr, RTE_ETHER_ADDR_LEN);
143 /* NOTE: we have considered the case that idx returned by t4_change_mac
144 * will be different from the user provided value only if user
145 * provided value is -1
147 if (entry->state == MPS_ENTRY_UNUSED) {
148 rte_atomic32_set(&entry->refcnt, 1);
149 entry->state = MPS_ENTRY_USED;
152 if (cxgbe_update_free_idx(mpstcam))
153 mpstcam->full = true;
155 t4_os_write_unlock(&mpstcam->lock);
160 * hold appropriate locks while calling this.
162 static inline void reset_mpstcam_entry(struct mps_tcam_entry *entry)
164 memset(entry->eth_addr, 0, RTE_ETHER_ADDR_LEN);
165 memset(entry->mask, 0, RTE_ETHER_ADDR_LEN);
166 rte_atomic32_clear(&entry->refcnt);
167 entry->state = MPS_ENTRY_UNUSED;
171 * ret < 0: fatal error
172 * ret = 0: entry removed in h/w
173 * ret > 0: updated refcount.
175 int cxgbe_mpstcam_remove(struct port_info *pi, u16 idx)
177 struct adapter *adap = pi->adapter;
178 struct mpstcam_table *t = adap->mpstcam;
179 struct mps_tcam_entry *entry;
184 t4_os_write_lock(&t->lock);
185 entry = &t->entry[idx];
186 if (entry->state == MPS_ENTRY_UNUSED) {
187 t4_os_write_unlock(&t->lock);
191 if (rte_atomic32_read(&entry->refcnt) == 1)
192 ret = t4_free_raw_mac_filt(adap, pi->viid, entry->eth_addr,
193 entry->mask, idx, 1, pi->port_id,
196 ret = rte_atomic32_sub_return(&entry->refcnt, 1);
199 reset_mpstcam_entry(entry);
200 t->full = false; /* We have atleast 1 free entry */
201 cxgbe_update_free_idx(t);
204 t4_os_write_unlock(&t->lock);
208 struct mpstcam_table *t4_init_mpstcam(struct adapter *adap)
210 struct mpstcam_table *t;
212 u16 size = adap->params.arch.mps_tcam_size;
214 t = t4_os_alloc(sizeof(*t) + size * sizeof(struct mps_tcam_entry));
218 t4_os_rwlock_init(&t->lock);
222 for (i = 0; i < size; i++) {
223 reset_mpstcam_entry(&t->entry[i]);
224 t->entry[i].mpstcam = t;
228 /* first entry is used by chip. this is overwritten only
229 * in t4_cleanup_mpstcam()
231 t->entry[0].state = MPS_ENTRY_USED;
237 void t4_cleanup_mpstcam(struct adapter *adap)
240 t4_os_free(adap->mpstcam);