xref: /linux/drivers/net/ethernet/meta/fbnic/fbnic_rpc.c (revision 0ad9617c78acbc71373fb341a6f75d4012b01d69)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) Meta Platforms, Inc. and affiliates. */
3 
4 #include <linux/etherdevice.h>
5 #include <linux/ethtool.h>
6 
7 #include "fbnic.h"
8 #include "fbnic_netdev.h"
9 #include "fbnic_rpc.h"
10 
11 void fbnic_reset_indir_tbl(struct fbnic_net *fbn)
12 {
13 	unsigned int num_rx = fbn->num_rx_queues;
14 	unsigned int i;
15 
16 	if (netif_is_rxfh_configured(fbn->netdev))
17 		return;
18 
19 	for (i = 0; i < FBNIC_RPC_RSS_TBL_SIZE; i++)
20 		fbn->indir_tbl[0][i] = ethtool_rxfh_indir_default(i, num_rx);
21 }
22 
23 void fbnic_rss_key_fill(u32 *buffer)
24 {
25 	static u32 rss_key[FBNIC_RPC_RSS_KEY_DWORD_LEN];
26 
27 	net_get_random_once(rss_key, sizeof(rss_key));
28 	rss_key[FBNIC_RPC_RSS_KEY_LAST_IDX] &= FBNIC_RPC_RSS_KEY_LAST_MASK;
29 
30 	memcpy(buffer, rss_key, sizeof(rss_key));
31 }
32 
33 #define RX_HASH_OPT_L4 \
34 	(RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3)
35 #define RX_HASH_OPT_L3 \
36 	(RXH_IP_SRC | RXH_IP_DST)
37 #define RX_HASH_OPT_L2 RXH_L2DA
38 
39 void fbnic_rss_init_en_mask(struct fbnic_net *fbn)
40 {
41 	fbn->rss_flow_hash[FBNIC_TCP4_HASH_OPT] = RX_HASH_OPT_L4;
42 	fbn->rss_flow_hash[FBNIC_TCP6_HASH_OPT] = RX_HASH_OPT_L4;
43 
44 	fbn->rss_flow_hash[FBNIC_UDP4_HASH_OPT] = RX_HASH_OPT_L3;
45 	fbn->rss_flow_hash[FBNIC_UDP6_HASH_OPT] = RX_HASH_OPT_L3;
46 	fbn->rss_flow_hash[FBNIC_IPV4_HASH_OPT] = RX_HASH_OPT_L3;
47 	fbn->rss_flow_hash[FBNIC_IPV6_HASH_OPT] = RX_HASH_OPT_L3;
48 
49 	fbn->rss_flow_hash[FBNIC_ETHER_HASH_OPT] = RX_HASH_OPT_L2;
50 }
51 
52 void fbnic_rss_disable_hw(struct fbnic_dev *fbd)
53 {
54 	/* Disable RPC by clearing enable bit and configuration */
55 	if (!fbnic_bmc_present(fbd))
56 		wr32(fbd, FBNIC_RPC_RMI_CONFIG,
57 		     FIELD_PREP(FBNIC_RPC_RMI_CONFIG_OH_BYTES, 20));
58 }
59 
60 #define FBNIC_FH_2_RSSEM_BIT(_fh, _rssem, _val)		\
61 	FIELD_PREP(FBNIC_RPC_ACT_TBL1_RSS_ENA_##_rssem,	\
62 		   FIELD_GET(RXH_##_fh, _val))
63 static u16 fbnic_flow_hash_2_rss_en_mask(struct fbnic_net *fbn, int flow_type)
64 {
65 	u32 flow_hash = fbn->rss_flow_hash[flow_type];
66 	u32 rss_en_mask = 0;
67 
68 	rss_en_mask |= FBNIC_FH_2_RSSEM_BIT(L2DA, L2_DA, flow_hash);
69 	rss_en_mask |= FBNIC_FH_2_RSSEM_BIT(IP_SRC, IP_SRC, flow_hash);
70 	rss_en_mask |= FBNIC_FH_2_RSSEM_BIT(IP_DST, IP_DST, flow_hash);
71 	rss_en_mask |= FBNIC_FH_2_RSSEM_BIT(L4_B_0_1, L4_SRC, flow_hash);
72 	rss_en_mask |= FBNIC_FH_2_RSSEM_BIT(L4_B_2_3, L4_DST, flow_hash);
73 
74 	return rss_en_mask;
75 }
76 
77 void fbnic_rss_reinit_hw(struct fbnic_dev *fbd, struct fbnic_net *fbn)
78 {
79 	unsigned int i;
80 
81 	for (i = 0; i < FBNIC_RPC_RSS_TBL_SIZE; i++) {
82 		wr32(fbd, FBNIC_RPC_RSS_TBL(0, i), fbn->indir_tbl[0][i]);
83 		wr32(fbd, FBNIC_RPC_RSS_TBL(1, i), fbn->indir_tbl[1][i]);
84 	}
85 
86 	for (i = 0; i < FBNIC_RPC_RSS_KEY_DWORD_LEN; i++)
87 		wr32(fbd, FBNIC_RPC_RSS_KEY(i), fbn->rss_key[i]);
88 
89 	/* Default action for this to drop w/ no destination */
90 	wr32(fbd, FBNIC_RPC_ACT_TBL0_DEFAULT, FBNIC_RPC_ACT_TBL0_DROP);
91 	wrfl(fbd);
92 
93 	wr32(fbd, FBNIC_RPC_ACT_TBL1_DEFAULT, 0);
94 
95 	/* If it isn't already enabled set the RMI Config value to enable RPC */
96 	wr32(fbd, FBNIC_RPC_RMI_CONFIG,
97 	     FIELD_PREP(FBNIC_RPC_RMI_CONFIG_MTU, FBNIC_MAX_JUMBO_FRAME_SIZE) |
98 	     FIELD_PREP(FBNIC_RPC_RMI_CONFIG_OH_BYTES, 20) |
99 	     FBNIC_RPC_RMI_CONFIG_ENABLE);
100 }
101 
102 void fbnic_bmc_rpc_all_multi_config(struct fbnic_dev *fbd,
103 				    bool enable_host)
104 {
105 	struct fbnic_act_tcam *act_tcam;
106 	struct fbnic_mac_addr *mac_addr;
107 	int j;
108 
109 	/* We need to add the all multicast filter at the end of the
110 	 * multicast address list. This way if there are any that are
111 	 * shared between the host and the BMC they can be directed to
112 	 * both. Otherwise the remainder just get sent directly to the
113 	 * BMC.
114 	 */
115 	mac_addr = &fbd->mac_addr[fbd->mac_addr_boundary - 1];
116 	if (fbnic_bmc_present(fbd) && fbd->fw_cap.all_multi) {
117 		if (mac_addr->state != FBNIC_TCAM_S_VALID) {
118 			eth_zero_addr(mac_addr->value.addr8);
119 			eth_broadcast_addr(mac_addr->mask.addr8);
120 			mac_addr->value.addr8[0] ^= 1;
121 			mac_addr->mask.addr8[0] ^= 1;
122 			set_bit(FBNIC_MAC_ADDR_T_BMC, mac_addr->act_tcam);
123 			mac_addr->state = FBNIC_TCAM_S_ADD;
124 		}
125 		if (enable_host)
126 			set_bit(FBNIC_MAC_ADDR_T_ALLMULTI,
127 				mac_addr->act_tcam);
128 		else
129 			clear_bit(FBNIC_MAC_ADDR_T_ALLMULTI,
130 				  mac_addr->act_tcam);
131 	} else if (!test_bit(FBNIC_MAC_ADDR_T_BMC, mac_addr->act_tcam) &&
132 		   !is_zero_ether_addr(mac_addr->mask.addr8) &&
133 		   mac_addr->state == FBNIC_TCAM_S_VALID) {
134 		clear_bit(FBNIC_MAC_ADDR_T_ALLMULTI, mac_addr->act_tcam);
135 		clear_bit(FBNIC_MAC_ADDR_T_BMC, mac_addr->act_tcam);
136 		mac_addr->state = FBNIC_TCAM_S_DELETE;
137 	}
138 
139 	/* We have to add a special handler for multicast as the
140 	 * BMC may have an all-multi rule already in place. As such
141 	 * adding a rule ourselves won't do any good so we will have
142 	 * to modify the rules for the ALL MULTI below if the BMC
143 	 * already has the rule in place.
144 	 */
145 	act_tcam = &fbd->act_tcam[FBNIC_RPC_ACT_TBL_BMC_ALL_MULTI_OFFSET];
146 
147 	/* If we are not enabling the rule just delete it. We will fall
148 	 * back to the RSS rules that support the multicast addresses.
149 	 */
150 	if (!fbnic_bmc_present(fbd) || !fbd->fw_cap.all_multi || enable_host) {
151 		if (act_tcam->state == FBNIC_TCAM_S_VALID)
152 			act_tcam->state = FBNIC_TCAM_S_DELETE;
153 		return;
154 	}
155 
156 	/* Rewrite TCAM rule 23 to handle BMC all-multi traffic */
157 	act_tcam->dest = FIELD_PREP(FBNIC_RPC_ACT_TBL0_DEST_MASK,
158 				    FBNIC_RPC_ACT_TBL0_DEST_BMC);
159 	act_tcam->mask.tcam[0] = 0xffff;
160 
161 	/* MACDA 0 - 3 is reserved for the BMC MAC address */
162 	act_tcam->value.tcam[1] =
163 			FIELD_PREP(FBNIC_RPC_TCAM_ACT1_L2_MACDA_IDX,
164 				   fbd->mac_addr_boundary - 1) |
165 			FBNIC_RPC_TCAM_ACT1_L2_MACDA_VALID;
166 	act_tcam->mask.tcam[1] = 0xffff &
167 			 ~FBNIC_RPC_TCAM_ACT1_L2_MACDA_IDX &
168 			 ~FBNIC_RPC_TCAM_ACT1_L2_MACDA_VALID;
169 
170 	for (j = 2; j < FBNIC_RPC_TCAM_ACT_WORD_LEN; j++)
171 		act_tcam->mask.tcam[j] = 0xffff;
172 
173 	act_tcam->state = FBNIC_TCAM_S_UPDATE;
174 }
175 
176 void fbnic_bmc_rpc_init(struct fbnic_dev *fbd)
177 {
178 	int i = FBNIC_RPC_TCAM_MACDA_BMC_ADDR_IDX;
179 	struct fbnic_act_tcam *act_tcam;
180 	struct fbnic_mac_addr *mac_addr;
181 	int j;
182 
183 	/* Check if BMC is present */
184 	if (!fbnic_bmc_present(fbd))
185 		return;
186 
187 	/* Fetch BMC MAC addresses from firmware capabilities */
188 	for (j = 0; j < 4; j++) {
189 		u8 *bmc_mac = fbd->fw_cap.bmc_mac_addr[j];
190 
191 		/* Validate BMC MAC addresses */
192 		if (is_zero_ether_addr(bmc_mac))
193 			continue;
194 
195 		if (is_multicast_ether_addr(bmc_mac))
196 			mac_addr = __fbnic_mc_sync(fbd, bmc_mac);
197 		else
198 			mac_addr = &fbd->mac_addr[i++];
199 
200 		if (!mac_addr) {
201 			netdev_err(fbd->netdev,
202 				   "No slot for BMC MAC address[%d]\n", j);
203 			continue;
204 		}
205 
206 		ether_addr_copy(mac_addr->value.addr8, bmc_mac);
207 		eth_zero_addr(mac_addr->mask.addr8);
208 
209 		set_bit(FBNIC_MAC_ADDR_T_BMC, mac_addr->act_tcam);
210 		mac_addr->state = FBNIC_TCAM_S_ADD;
211 	}
212 
213 	/* Validate Broadcast is also present, record it and tag it */
214 	mac_addr = &fbd->mac_addr[FBNIC_RPC_TCAM_MACDA_BROADCAST_IDX];
215 	eth_broadcast_addr(mac_addr->value.addr8);
216 	set_bit(FBNIC_MAC_ADDR_T_BMC, mac_addr->act_tcam);
217 	mac_addr->state = FBNIC_TCAM_S_ADD;
218 
219 	/* Rewrite TCAM rule 0 if it isn't present to relocate BMC rules */
220 	act_tcam = &fbd->act_tcam[FBNIC_RPC_ACT_TBL_BMC_OFFSET];
221 	act_tcam->dest = FIELD_PREP(FBNIC_RPC_ACT_TBL0_DEST_MASK,
222 				    FBNIC_RPC_ACT_TBL0_DEST_BMC);
223 	act_tcam->mask.tcam[0] = 0xffff;
224 
225 	/* MACDA 0 - 3 is reserved for the BMC MAC address
226 	 * to account for that we have to mask out the lower 2 bits
227 	 * of the macda by performing an &= with 0x1c.
228 	 */
229 	act_tcam->value.tcam[1] = FBNIC_RPC_TCAM_ACT1_L2_MACDA_VALID;
230 	act_tcam->mask.tcam[1] = 0xffff &
231 			~FIELD_PREP(FBNIC_RPC_TCAM_ACT1_L2_MACDA_IDX, 0x1c) &
232 			~FBNIC_RPC_TCAM_ACT1_L2_MACDA_VALID;
233 
234 	for (j = 2; j < FBNIC_RPC_TCAM_ACT_WORD_LEN; j++)
235 		act_tcam->mask.tcam[j] = 0xffff;
236 
237 	act_tcam->state = FBNIC_TCAM_S_UPDATE;
238 
239 	fbnic_bmc_rpc_all_multi_config(fbd, false);
240 }
241 
242 #define FBNIC_ACT1_INIT(_l4, _udp, _ip, _v6)		\
243 	(((_l4) ? FBNIC_RPC_TCAM_ACT1_L4_VALID : 0) |	\
244 	 ((_udp) ? FBNIC_RPC_TCAM_ACT1_L4_IS_UDP : 0) |	\
245 	 ((_ip) ? FBNIC_RPC_TCAM_ACT1_IP_VALID : 0) |	\
246 	 ((_v6) ? FBNIC_RPC_TCAM_ACT1_IP_IS_V6 : 0))
247 
248 #define FBNIC_TSTAMP_MASK(_all, _udp, _ether)			\
249 	(((_all) ? ((1u << FBNIC_NUM_HASH_OPT) - 1) : 0) |	\
250 	 ((_udp) ? (1u << FBNIC_UDP6_HASH_OPT) |		\
251 		   (1u << FBNIC_UDP4_HASH_OPT) : 0) |		\
252 	 ((_ether) ? (1u << FBNIC_ETHER_HASH_OPT) : 0))
253 
254 void fbnic_rss_reinit(struct fbnic_dev *fbd, struct fbnic_net *fbn)
255 {
256 	static const u32 act1_value[FBNIC_NUM_HASH_OPT] = {
257 		FBNIC_ACT1_INIT(1, 1, 1, 1),	/* UDP6 */
258 		FBNIC_ACT1_INIT(1, 1, 1, 0),	/* UDP4 */
259 		FBNIC_ACT1_INIT(1, 0, 1, 1),	/* TCP6 */
260 		FBNIC_ACT1_INIT(1, 0, 1, 0),	/* TCP4 */
261 		FBNIC_ACT1_INIT(0, 0, 1, 1),	/* IP6 */
262 		FBNIC_ACT1_INIT(0, 0, 1, 0),	/* IP4 */
263 		0				/* Ether */
264 	};
265 	u32 tstamp_mask = 0;
266 	unsigned int i;
267 
268 	/* To support scenarios where a BMC is present we must write the
269 	 * rules twice, once for the unicast cases, and once again for
270 	 * the broadcast/multicast cases as we have to support 2 destinations.
271 	 */
272 	BUILD_BUG_ON(FBNIC_RSS_EN_NUM_UNICAST * 2 != FBNIC_RSS_EN_NUM_ENTRIES);
273 	BUILD_BUG_ON(ARRAY_SIZE(act1_value) != FBNIC_NUM_HASH_OPT);
274 
275 	/* Set timestamp mask with 1b per flow type */
276 	if (fbn->hwtstamp_config.rx_filter != HWTSTAMP_FILTER_NONE) {
277 		switch (fbn->hwtstamp_config.rx_filter) {
278 		case HWTSTAMP_FILTER_ALL:
279 			tstamp_mask = FBNIC_TSTAMP_MASK(1, 1, 1);
280 			break;
281 		case HWTSTAMP_FILTER_PTP_V2_EVENT:
282 			tstamp_mask = FBNIC_TSTAMP_MASK(0, 1, 1);
283 			break;
284 		case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
285 		case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
286 			tstamp_mask = FBNIC_TSTAMP_MASK(0, 1, 0);
287 			break;
288 		case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
289 			tstamp_mask = FBNIC_TSTAMP_MASK(0, 0, 1);
290 			break;
291 		default:
292 			netdev_warn(fbn->netdev, "Unsupported hwtstamp_rx_filter\n");
293 			break;
294 		}
295 	}
296 
297 	/* Program RSS hash enable mask for host in action TCAM/table. */
298 	for (i = fbnic_bmc_present(fbd) ? 0 : FBNIC_RSS_EN_NUM_UNICAST;
299 	     i < FBNIC_RSS_EN_NUM_ENTRIES; i++) {
300 		unsigned int idx = i + FBNIC_RPC_ACT_TBL_RSS_OFFSET;
301 		struct fbnic_act_tcam *act_tcam = &fbd->act_tcam[idx];
302 		u32 flow_hash, dest, rss_en_mask;
303 		int flow_type, j;
304 		u16 value = 0;
305 
306 		flow_type = i % FBNIC_RSS_EN_NUM_UNICAST;
307 		flow_hash = fbn->rss_flow_hash[flow_type];
308 
309 		/* Set DEST_HOST based on absence of RXH_DISCARD */
310 		dest = FIELD_PREP(FBNIC_RPC_ACT_TBL0_DEST_MASK,
311 				  !(RXH_DISCARD & flow_hash) ?
312 				  FBNIC_RPC_ACT_TBL0_DEST_HOST : 0);
313 
314 		if (i >= FBNIC_RSS_EN_NUM_UNICAST && fbnic_bmc_present(fbd))
315 			dest |= FIELD_PREP(FBNIC_RPC_ACT_TBL0_DEST_MASK,
316 					   FBNIC_RPC_ACT_TBL0_DEST_BMC);
317 
318 		if (!dest)
319 			dest = FBNIC_RPC_ACT_TBL0_DROP;
320 		else if (tstamp_mask & (1u << flow_type))
321 			dest |= FBNIC_RPC_ACT_TBL0_TS_ENA;
322 
323 		if (act1_value[flow_type] & FBNIC_RPC_TCAM_ACT1_L4_VALID)
324 			dest |= FIELD_PREP(FBNIC_RPC_ACT_TBL0_DMA_HINT,
325 					   FBNIC_RCD_HDR_AL_DMA_HINT_L4);
326 
327 		rss_en_mask = fbnic_flow_hash_2_rss_en_mask(fbn, flow_type);
328 
329 		act_tcam->dest = dest;
330 		act_tcam->rss_en_mask = rss_en_mask;
331 		act_tcam->state = FBNIC_TCAM_S_UPDATE;
332 
333 		act_tcam->mask.tcam[0] = 0xffff;
334 
335 		/* We reserve the upper 8 MACDA TCAM entries for host
336 		 * unicast. So we set the value to 24, and the mask the
337 		 * lower bits so that the lower entries can be used as
338 		 * multicast or BMC addresses.
339 		 */
340 		if (i < FBNIC_RSS_EN_NUM_UNICAST)
341 			value = FIELD_PREP(FBNIC_RPC_TCAM_ACT1_L2_MACDA_IDX,
342 					   fbd->mac_addr_boundary);
343 		value |= FBNIC_RPC_TCAM_ACT1_L2_MACDA_VALID;
344 
345 		flow_type = i % FBNIC_RSS_EN_NUM_UNICAST;
346 		value |= act1_value[flow_type];
347 
348 		act_tcam->value.tcam[1] = value;
349 		act_tcam->mask.tcam[1] = ~value;
350 
351 		for (j = 2; j < FBNIC_RPC_TCAM_ACT_WORD_LEN; j++)
352 			act_tcam->mask.tcam[j] = 0xffff;
353 
354 		act_tcam->state = FBNIC_TCAM_S_UPDATE;
355 	}
356 }
357 
358 struct fbnic_mac_addr *__fbnic_uc_sync(struct fbnic_dev *fbd,
359 				       const unsigned char *addr)
360 {
361 	struct fbnic_mac_addr *avail_addr = NULL;
362 	unsigned int i;
363 
364 	/* Scan from middle of list to bottom, filling bottom up.
365 	 * Skip the first entry which is reserved for dev_addr and
366 	 * leave the last entry to use for promiscuous filtering.
367 	 */
368 	for (i = fbd->mac_addr_boundary - 1;
369 	     i < FBNIC_RPC_TCAM_MACDA_HOST_ADDR_IDX; i++) {
370 		struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[i];
371 
372 		if (mac_addr->state == FBNIC_TCAM_S_DISABLED) {
373 			avail_addr = mac_addr;
374 		} else if (ether_addr_equal(mac_addr->value.addr8, addr)) {
375 			avail_addr = mac_addr;
376 			break;
377 		}
378 	}
379 
380 	if (avail_addr && avail_addr->state == FBNIC_TCAM_S_DISABLED) {
381 		ether_addr_copy(avail_addr->value.addr8, addr);
382 		eth_zero_addr(avail_addr->mask.addr8);
383 		avail_addr->state = FBNIC_TCAM_S_ADD;
384 	}
385 
386 	return avail_addr;
387 }
388 
389 struct fbnic_mac_addr *__fbnic_mc_sync(struct fbnic_dev *fbd,
390 				       const unsigned char *addr)
391 {
392 	struct fbnic_mac_addr *avail_addr = NULL;
393 	unsigned int i;
394 
395 	/* Scan from middle of list to top, filling top down.
396 	 * Skip over the address reserved for the BMC MAC and
397 	 * exclude index 0 as that belongs to the broadcast address
398 	 */
399 	for (i = fbd->mac_addr_boundary;
400 	     --i > FBNIC_RPC_TCAM_MACDA_BROADCAST_IDX;) {
401 		struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[i];
402 
403 		if (mac_addr->state == FBNIC_TCAM_S_DISABLED) {
404 			avail_addr = mac_addr;
405 		} else if (ether_addr_equal(mac_addr->value.addr8, addr)) {
406 			avail_addr = mac_addr;
407 			break;
408 		}
409 	}
410 
411 	/* Scan the BMC addresses to see if it may have already
412 	 * reserved the address.
413 	 */
414 	while (--i) {
415 		struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[i];
416 
417 		if (!is_zero_ether_addr(mac_addr->mask.addr8))
418 			continue;
419 
420 		/* Only move on if we find a match */
421 		if (!ether_addr_equal(mac_addr->value.addr8, addr))
422 			continue;
423 
424 		/* We need to pull this address to the shared area */
425 		if (avail_addr) {
426 			memcpy(avail_addr, mac_addr, sizeof(*mac_addr));
427 			mac_addr->state = FBNIC_TCAM_S_DELETE;
428 			avail_addr->state = FBNIC_TCAM_S_ADD;
429 		}
430 
431 		break;
432 	}
433 
434 	if (avail_addr && avail_addr->state == FBNIC_TCAM_S_DISABLED) {
435 		ether_addr_copy(avail_addr->value.addr8, addr);
436 		eth_zero_addr(avail_addr->mask.addr8);
437 		avail_addr->state = FBNIC_TCAM_S_ADD;
438 	}
439 
440 	return avail_addr;
441 }
442 
443 int __fbnic_xc_unsync(struct fbnic_mac_addr *mac_addr, unsigned int tcam_idx)
444 {
445 	if (!test_and_clear_bit(tcam_idx, mac_addr->act_tcam))
446 		return -ENOENT;
447 
448 	if (bitmap_empty(mac_addr->act_tcam, FBNIC_RPC_TCAM_ACT_NUM_ENTRIES))
449 		mac_addr->state = FBNIC_TCAM_S_DELETE;
450 
451 	return 0;
452 }
453 
454 void fbnic_sift_macda(struct fbnic_dev *fbd)
455 {
456 	int dest, src;
457 
458 	/* Move BMC only addresses back into BMC region */
459 	for (dest = FBNIC_RPC_TCAM_MACDA_BMC_ADDR_IDX,
460 	     src = FBNIC_RPC_TCAM_MACDA_MULTICAST_IDX;
461 	     ++dest < FBNIC_RPC_TCAM_MACDA_BROADCAST_IDX &&
462 	     src < fbd->mac_addr_boundary;) {
463 		struct fbnic_mac_addr *dest_addr = &fbd->mac_addr[dest];
464 
465 		if (dest_addr->state != FBNIC_TCAM_S_DISABLED)
466 			continue;
467 
468 		while (src < fbd->mac_addr_boundary) {
469 			struct fbnic_mac_addr *src_addr = &fbd->mac_addr[src++];
470 
471 			/* Verify BMC bit is set */
472 			if (!test_bit(FBNIC_MAC_ADDR_T_BMC, src_addr->act_tcam))
473 				continue;
474 
475 			/* Verify filter isn't already disabled */
476 			if (src_addr->state == FBNIC_TCAM_S_DISABLED ||
477 			    src_addr->state == FBNIC_TCAM_S_DELETE)
478 				continue;
479 
480 			/* Verify only BMC bit is set */
481 			if (bitmap_weight(src_addr->act_tcam,
482 					  FBNIC_RPC_TCAM_ACT_NUM_ENTRIES) != 1)
483 				continue;
484 
485 			/* Verify we are not moving wildcard address */
486 			if (!is_zero_ether_addr(src_addr->mask.addr8))
487 				continue;
488 
489 			memcpy(dest_addr, src_addr, sizeof(*src_addr));
490 			src_addr->state = FBNIC_TCAM_S_DELETE;
491 			dest_addr->state = FBNIC_TCAM_S_ADD;
492 		}
493 	}
494 }
495 
496 static void fbnic_clear_macda_entry(struct fbnic_dev *fbd, unsigned int idx)
497 {
498 	int i;
499 
500 	/* Invalidate entry and clear addr state info */
501 	for (i = 0; i <= FBNIC_RPC_TCAM_MACDA_WORD_LEN; i++)
502 		wr32(fbd, FBNIC_RPC_TCAM_MACDA(idx, i), 0);
503 }
504 
505 static void fbnic_clear_macda(struct fbnic_dev *fbd)
506 {
507 	int idx;
508 
509 	for (idx = ARRAY_SIZE(fbd->mac_addr); idx--;) {
510 		struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[idx];
511 
512 		if (mac_addr->state == FBNIC_TCAM_S_DISABLED)
513 			continue;
514 
515 		if (test_bit(FBNIC_MAC_ADDR_T_BMC, mac_addr->act_tcam)) {
516 			if (fbnic_bmc_present(fbd))
517 				continue;
518 			dev_warn_once(fbd->dev,
519 				      "Found BMC MAC address w/ BMC not present\n");
520 		}
521 
522 		fbnic_clear_macda_entry(fbd, idx);
523 
524 		/* If rule was already destined for deletion just wipe it now */
525 		if (mac_addr->state == FBNIC_TCAM_S_DELETE) {
526 			memset(mac_addr, 0, sizeof(*mac_addr));
527 			continue;
528 		}
529 
530 		/* Change state to update so that we will rewrite
531 		 * this tcam the next time fbnic_write_macda is called.
532 		 */
533 		mac_addr->state = FBNIC_TCAM_S_UPDATE;
534 	}
535 }
536 
537 static void fbnic_write_macda_entry(struct fbnic_dev *fbd, unsigned int idx,
538 				    struct fbnic_mac_addr *mac_addr)
539 {
540 	__be16 *mask, *value;
541 	int i;
542 
543 	mask = &mac_addr->mask.addr16[FBNIC_RPC_TCAM_MACDA_WORD_LEN - 1];
544 	value = &mac_addr->value.addr16[FBNIC_RPC_TCAM_MACDA_WORD_LEN - 1];
545 
546 	for (i = 0; i < FBNIC_RPC_TCAM_MACDA_WORD_LEN; i++)
547 		wr32(fbd, FBNIC_RPC_TCAM_MACDA(idx, i),
548 		     FIELD_PREP(FBNIC_RPC_TCAM_MACDA_MASK, ntohs(*mask--)) |
549 		     FIELD_PREP(FBNIC_RPC_TCAM_MACDA_VALUE, ntohs(*value--)));
550 
551 	wrfl(fbd);
552 
553 	wr32(fbd, FBNIC_RPC_TCAM_MACDA(idx, i), FBNIC_RPC_TCAM_VALIDATE);
554 }
555 
556 void fbnic_write_macda(struct fbnic_dev *fbd)
557 {
558 	int idx;
559 
560 	for (idx = ARRAY_SIZE(fbd->mac_addr); idx--;) {
561 		struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[idx];
562 
563 		/* Check if update flag is set else exit. */
564 		if (!(mac_addr->state & FBNIC_TCAM_S_UPDATE))
565 			continue;
566 
567 		/* Clear by writing 0s. */
568 		if (mac_addr->state == FBNIC_TCAM_S_DELETE) {
569 			/* Invalidate entry and clear addr state info */
570 			fbnic_clear_macda_entry(fbd, idx);
571 			memset(mac_addr, 0, sizeof(*mac_addr));
572 
573 			continue;
574 		}
575 
576 		fbnic_write_macda_entry(fbd, idx, mac_addr);
577 
578 		mac_addr->state = FBNIC_TCAM_S_VALID;
579 	}
580 }
581 
582 static void fbnic_clear_act_tcam(struct fbnic_dev *fbd, unsigned int idx)
583 {
584 	int i;
585 
586 	/* Invalidate entry and clear addr state info */
587 	for (i = 0; i <= FBNIC_RPC_TCAM_ACT_WORD_LEN; i++)
588 		wr32(fbd, FBNIC_RPC_TCAM_ACT(idx, i), 0);
589 }
590 
591 static void fbnic_clear_tce_tcam_entry(struct fbnic_dev *fbd, unsigned int idx)
592 {
593 	int i;
594 
595 	/* Invalidate entry and clear addr state info */
596 	for (i = 0; i <= FBNIC_TCE_TCAM_WORD_LEN; i++)
597 		wr32(fbd, FBNIC_TCE_RAM_TCAM(idx, i), 0);
598 }
599 
600 static void fbnic_write_tce_tcam_dest(struct fbnic_dev *fbd, unsigned int idx,
601 				      struct fbnic_mac_addr *mac_addr)
602 {
603 	u32 dest = FBNIC_TCE_TCAM_DEST_BMC;
604 	u32 idx2dest_map;
605 
606 	if (is_multicast_ether_addr(mac_addr->value.addr8))
607 		dest |= FBNIC_TCE_TCAM_DEST_MAC;
608 
609 	idx2dest_map = rd32(fbd, FBNIC_TCE_TCAM_IDX2DEST_MAP);
610 	idx2dest_map &= ~(FBNIC_TCE_TCAM_IDX2DEST_MAP_DEST_ID_0 << (4 * idx));
611 	idx2dest_map |= dest << (4 * idx);
612 
613 	wr32(fbd, FBNIC_TCE_TCAM_IDX2DEST_MAP, idx2dest_map);
614 }
615 
616 static void fbnic_write_tce_tcam_entry(struct fbnic_dev *fbd, unsigned int idx,
617 				       struct fbnic_mac_addr *mac_addr)
618 {
619 	__be16 *mask, *value;
620 	int i;
621 
622 	mask = &mac_addr->mask.addr16[FBNIC_TCE_TCAM_WORD_LEN - 1];
623 	value = &mac_addr->value.addr16[FBNIC_TCE_TCAM_WORD_LEN - 1];
624 
625 	for (i = 0; i < FBNIC_TCE_TCAM_WORD_LEN; i++)
626 		wr32(fbd, FBNIC_TCE_RAM_TCAM(idx, i),
627 		     FIELD_PREP(FBNIC_TCE_RAM_TCAM_MASK, ntohs(*mask--)) |
628 		     FIELD_PREP(FBNIC_TCE_RAM_TCAM_VALUE, ntohs(*value--)));
629 
630 	wrfl(fbd);
631 
632 	wr32(fbd, FBNIC_TCE_RAM_TCAM3(idx), FBNIC_TCE_RAM_TCAM3_MCQ_MASK |
633 				       FBNIC_TCE_RAM_TCAM3_DEST_MASK |
634 				       FBNIC_TCE_RAM_TCAM3_VALIDATE);
635 }
636 
637 static void __fbnic_write_tce_tcam_rev(struct fbnic_dev *fbd)
638 {
639 	int tcam_idx = FBNIC_TCE_TCAM_NUM_ENTRIES;
640 	int mac_idx;
641 
642 	for (mac_idx = ARRAY_SIZE(fbd->mac_addr); mac_idx--;) {
643 		struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[mac_idx];
644 
645 		/* Verify BMC bit is set */
646 		if (!test_bit(FBNIC_MAC_ADDR_T_BMC, mac_addr->act_tcam))
647 			continue;
648 
649 		if (!tcam_idx) {
650 			dev_err(fbd->dev, "TCE TCAM overflow\n");
651 			return;
652 		}
653 
654 		tcam_idx--;
655 		fbnic_write_tce_tcam_dest(fbd, tcam_idx, mac_addr);
656 		fbnic_write_tce_tcam_entry(fbd, tcam_idx, mac_addr);
657 	}
658 
659 	while (tcam_idx)
660 		fbnic_clear_tce_tcam_entry(fbd, --tcam_idx);
661 
662 	fbd->tce_tcam_last = tcam_idx;
663 }
664 
665 static void __fbnic_write_tce_tcam(struct fbnic_dev *fbd)
666 {
667 	int tcam_idx = 0;
668 	int mac_idx;
669 
670 	for (mac_idx = 0; mac_idx < ARRAY_SIZE(fbd->mac_addr); mac_idx++) {
671 		struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[mac_idx];
672 
673 		/* Verify BMC bit is set */
674 		if (!test_bit(FBNIC_MAC_ADDR_T_BMC, mac_addr->act_tcam))
675 			continue;
676 
677 		if (tcam_idx == FBNIC_TCE_TCAM_NUM_ENTRIES) {
678 			dev_err(fbd->dev, "TCE TCAM overflow\n");
679 			return;
680 		}
681 
682 		fbnic_write_tce_tcam_dest(fbd, tcam_idx, mac_addr);
683 		fbnic_write_tce_tcam_entry(fbd, tcam_idx, mac_addr);
684 		tcam_idx++;
685 	}
686 
687 	while (tcam_idx < FBNIC_TCE_TCAM_NUM_ENTRIES)
688 		fbnic_clear_tce_tcam_entry(fbd, tcam_idx++);
689 
690 	fbd->tce_tcam_last = tcam_idx;
691 }
692 
693 void fbnic_write_tce_tcam(struct fbnic_dev *fbd)
694 {
695 	if (fbd->tce_tcam_last)
696 		__fbnic_write_tce_tcam_rev(fbd);
697 	else
698 		__fbnic_write_tce_tcam(fbd);
699 }
700 
701 void fbnic_clear_rules(struct fbnic_dev *fbd)
702 {
703 	u32 dest = FIELD_PREP(FBNIC_RPC_ACT_TBL0_DEST_MASK,
704 			      FBNIC_RPC_ACT_TBL0_DEST_BMC);
705 	int i = FBNIC_RPC_TCAM_ACT_NUM_ENTRIES - 1;
706 	struct fbnic_act_tcam *act_tcam;
707 
708 	/* Clear MAC rules */
709 	fbnic_clear_macda(fbd);
710 
711 	/* If BMC is present we need to preserve the last rule which
712 	 * will be used to route traffic to the BMC if it is received.
713 	 *
714 	 * At this point it should be the only MAC address in the MACDA
715 	 * so any unicast or multicast traffic received should be routed
716 	 * to it. So leave the last rule in place.
717 	 *
718 	 * It will be rewritten to add the host again when we bring
719 	 * the interface back up.
720 	 */
721 	if (fbnic_bmc_present(fbd)) {
722 		act_tcam = &fbd->act_tcam[i];
723 
724 		if (act_tcam->state == FBNIC_TCAM_S_VALID &&
725 		    (act_tcam->dest & dest)) {
726 			wr32(fbd, FBNIC_RPC_ACT_TBL0(i), dest);
727 			wr32(fbd, FBNIC_RPC_ACT_TBL1(i), 0);
728 
729 			act_tcam->state = FBNIC_TCAM_S_UPDATE;
730 
731 			i--;
732 		}
733 	}
734 
735 	/* Work from the bottom up deleting all other rules from hardware */
736 	do {
737 		act_tcam = &fbd->act_tcam[i];
738 
739 		if (act_tcam->state != FBNIC_TCAM_S_VALID)
740 			continue;
741 
742 		fbnic_clear_act_tcam(fbd, i);
743 		act_tcam->state = FBNIC_TCAM_S_UPDATE;
744 	} while (i--);
745 }
746 
747 static void fbnic_delete_act_tcam(struct fbnic_dev *fbd, unsigned int idx)
748 {
749 	fbnic_clear_act_tcam(fbd, idx);
750 	memset(&fbd->act_tcam[idx], 0, sizeof(struct fbnic_act_tcam));
751 }
752 
753 static void fbnic_update_act_tcam(struct fbnic_dev *fbd, unsigned int idx)
754 {
755 	struct fbnic_act_tcam *act_tcam = &fbd->act_tcam[idx];
756 	int i;
757 
758 	/* Update entry by writing the destination and RSS mask */
759 	wr32(fbd, FBNIC_RPC_ACT_TBL0(idx), act_tcam->dest);
760 	wr32(fbd, FBNIC_RPC_ACT_TBL1(idx), act_tcam->rss_en_mask);
761 
762 	/* Write new TCAM rule to hardware */
763 	for (i = 0; i < FBNIC_RPC_TCAM_ACT_WORD_LEN; i++)
764 		wr32(fbd, FBNIC_RPC_TCAM_ACT(idx, i),
765 		     FIELD_PREP(FBNIC_RPC_TCAM_ACT_MASK,
766 				act_tcam->mask.tcam[i]) |
767 		     FIELD_PREP(FBNIC_RPC_TCAM_ACT_VALUE,
768 				act_tcam->value.tcam[i]));
769 
770 	wrfl(fbd);
771 
772 	wr32(fbd, FBNIC_RPC_TCAM_ACT(idx, i), FBNIC_RPC_TCAM_VALIDATE);
773 	act_tcam->state = FBNIC_TCAM_S_VALID;
774 }
775 
776 void fbnic_write_rules(struct fbnic_dev *fbd)
777 {
778 	int i;
779 
780 	/* Flush any pending action table rules */
781 	for (i = 0; i < FBNIC_RPC_ACT_TBL_NUM_ENTRIES; i++) {
782 		struct fbnic_act_tcam *act_tcam = &fbd->act_tcam[i];
783 
784 		/* Check if update flag is set else exit. */
785 		if (!(act_tcam->state & FBNIC_TCAM_S_UPDATE))
786 			continue;
787 
788 		if (act_tcam->state == FBNIC_TCAM_S_DELETE)
789 			fbnic_delete_act_tcam(fbd, i);
790 		else
791 			fbnic_update_act_tcam(fbd, i);
792 	}
793 }
794