xref: /linux/drivers/net/ethernet/meta/fbnic/fbnic_rpc.c (revision f09fc24dd9a5ec989dfdde7090624924ede6ddc7)
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 #include <net/ipv6.h>
7 
8 #include "fbnic.h"
9 #include "fbnic_netdev.h"
10 #include "fbnic_rpc.h"
11 
12 void fbnic_reset_indir_tbl(struct fbnic_net *fbn)
13 {
14 	unsigned int num_rx = fbn->num_rx_queues;
15 	unsigned int i;
16 
17 	if (netif_is_rxfh_configured(fbn->netdev))
18 		return;
19 
20 	for (i = 0; i < FBNIC_RPC_RSS_TBL_SIZE; i++)
21 		fbn->indir_tbl[0][i] = ethtool_rxfh_indir_default(i, num_rx);
22 }
23 
24 void fbnic_rss_key_fill(u32 *buffer)
25 {
26 	static u32 rss_key[FBNIC_RPC_RSS_KEY_DWORD_LEN];
27 
28 	net_get_random_once(rss_key, sizeof(rss_key));
29 	rss_key[FBNIC_RPC_RSS_KEY_LAST_IDX] &= FBNIC_RPC_RSS_KEY_LAST_MASK;
30 
31 	memcpy(buffer, rss_key, sizeof(rss_key));
32 }
33 
34 #define RX_HASH_OPT_L4 \
35 	(RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3)
36 #define RX_HASH_OPT_L3 \
37 	(RXH_IP_SRC | RXH_IP_DST)
38 #define RX_HASH_OPT_L2 RXH_L2DA
39 
40 void fbnic_rss_init_en_mask(struct fbnic_net *fbn)
41 {
42 	fbn->rss_flow_hash[FBNIC_TCP4_HASH_OPT] = RX_HASH_OPT_L4;
43 	fbn->rss_flow_hash[FBNIC_TCP6_HASH_OPT] = RX_HASH_OPT_L4;
44 
45 	fbn->rss_flow_hash[FBNIC_UDP4_HASH_OPT] = RX_HASH_OPT_L3;
46 	fbn->rss_flow_hash[FBNIC_UDP6_HASH_OPT] = RX_HASH_OPT_L3;
47 	fbn->rss_flow_hash[FBNIC_IPV4_HASH_OPT] = RX_HASH_OPT_L3;
48 	fbn->rss_flow_hash[FBNIC_IPV6_HASH_OPT] = RX_HASH_OPT_L3;
49 
50 	fbn->rss_flow_hash[FBNIC_ETHER_HASH_OPT] = RX_HASH_OPT_L2;
51 }
52 
53 void fbnic_rss_disable_hw(struct fbnic_dev *fbd)
54 {
55 	/* Disable RPC by clearing enable bit and configuration */
56 	if (!fbnic_bmc_present(fbd))
57 		wr32(fbd, FBNIC_RPC_RMI_CONFIG,
58 		     FIELD_PREP(FBNIC_RPC_RMI_CONFIG_OH_BYTES, 20));
59 }
60 
61 #define FBNIC_FH_2_RSSEM_BIT(_fh, _rssem, _val)		\
62 	FIELD_PREP(FBNIC_RPC_ACT_TBL1_RSS_ENA_##_rssem,	\
63 		   FIELD_GET(RXH_##_fh, _val))
64 u16 fbnic_flow_hash_2_rss_en_mask(struct fbnic_net *fbn, int flow_type)
65 {
66 	u32 flow_hash = fbn->rss_flow_hash[flow_type];
67 	u32 rss_en_mask = 0;
68 
69 	rss_en_mask |= FBNIC_FH_2_RSSEM_BIT(L2DA, L2_DA, flow_hash);
70 	rss_en_mask |= FBNIC_FH_2_RSSEM_BIT(IP_SRC, IP_SRC, flow_hash);
71 	rss_en_mask |= FBNIC_FH_2_RSSEM_BIT(IP_DST, IP_DST, flow_hash);
72 	rss_en_mask |= FBNIC_FH_2_RSSEM_BIT(L4_B_0_1, L4_SRC, flow_hash);
73 	rss_en_mask |= FBNIC_FH_2_RSSEM_BIT(L4_B_2_3, L4_DST, flow_hash);
74 	rss_en_mask |= FBNIC_FH_2_RSSEM_BIT(IP6_FL, OV6_FL_LBL, flow_hash);
75 	rss_en_mask |= FBNIC_FH_2_RSSEM_BIT(IP6_FL, IV6_FL_LBL, flow_hash);
76 
77 	return rss_en_mask;
78 }
79 
80 void fbnic_rss_reinit_hw(struct fbnic_dev *fbd, struct fbnic_net *fbn)
81 {
82 	unsigned int i;
83 
84 	for (i = 0; i < FBNIC_RPC_RSS_TBL_SIZE; i++) {
85 		wr32(fbd, FBNIC_RPC_RSS_TBL(0, i), fbn->indir_tbl[0][i]);
86 		wr32(fbd, FBNIC_RPC_RSS_TBL(1, i), fbn->indir_tbl[1][i]);
87 	}
88 
89 	for (i = 0; i < FBNIC_RPC_RSS_KEY_DWORD_LEN; i++)
90 		wr32(fbd, FBNIC_RPC_RSS_KEY(i), fbn->rss_key[i]);
91 
92 	/* Default action for this to drop w/ no destination */
93 	wr32(fbd, FBNIC_RPC_ACT_TBL0_DEFAULT, FBNIC_RPC_ACT_TBL0_DROP);
94 	wrfl(fbd);
95 
96 	wr32(fbd, FBNIC_RPC_ACT_TBL1_DEFAULT, 0);
97 
98 	/* If it isn't already enabled set the RMI Config value to enable RPC */
99 	wr32(fbd, FBNIC_RPC_RMI_CONFIG,
100 	     FIELD_PREP(FBNIC_RPC_RMI_CONFIG_MTU, FBNIC_MAX_JUMBO_FRAME_SIZE) |
101 	     FIELD_PREP(FBNIC_RPC_RMI_CONFIG_OH_BYTES, 20) |
102 	     FBNIC_RPC_RMI_CONFIG_ENABLE);
103 }
104 
105 void fbnic_bmc_rpc_all_multi_config(struct fbnic_dev *fbd,
106 				    bool enable_host)
107 {
108 	struct fbnic_act_tcam *act_tcam;
109 	struct fbnic_mac_addr *mac_addr;
110 	int j;
111 
112 	/* We need to add the all multicast filter at the end of the
113 	 * multicast address list. This way if there are any that are
114 	 * shared between the host and the BMC they can be directed to
115 	 * both. Otherwise the remainder just get sent directly to the
116 	 * BMC.
117 	 */
118 	mac_addr = &fbd->mac_addr[fbd->mac_addr_boundary - 1];
119 	if (fbnic_bmc_present(fbd) && fbd->fw_cap.all_multi) {
120 		if (mac_addr->state != FBNIC_TCAM_S_VALID) {
121 			eth_zero_addr(mac_addr->value.addr8);
122 			eth_broadcast_addr(mac_addr->mask.addr8);
123 			mac_addr->value.addr8[0] ^= 1;
124 			mac_addr->mask.addr8[0] ^= 1;
125 			set_bit(FBNIC_MAC_ADDR_T_BMC, mac_addr->act_tcam);
126 			mac_addr->state = FBNIC_TCAM_S_ADD;
127 		}
128 		if (enable_host)
129 			set_bit(FBNIC_MAC_ADDR_T_ALLMULTI,
130 				mac_addr->act_tcam);
131 		else
132 			clear_bit(FBNIC_MAC_ADDR_T_ALLMULTI,
133 				  mac_addr->act_tcam);
134 	} else if (!test_bit(FBNIC_MAC_ADDR_T_BMC, mac_addr->act_tcam) &&
135 		   !is_zero_ether_addr(mac_addr->mask.addr8) &&
136 		   mac_addr->state == FBNIC_TCAM_S_VALID) {
137 		clear_bit(FBNIC_MAC_ADDR_T_ALLMULTI, mac_addr->act_tcam);
138 		clear_bit(FBNIC_MAC_ADDR_T_BMC, mac_addr->act_tcam);
139 		mac_addr->state = FBNIC_TCAM_S_DELETE;
140 	}
141 
142 	/* We have to add a special handler for multicast as the
143 	 * BMC may have an all-multi rule already in place. As such
144 	 * adding a rule ourselves won't do any good so we will have
145 	 * to modify the rules for the ALL MULTI below if the BMC
146 	 * already has the rule in place.
147 	 */
148 	act_tcam = &fbd->act_tcam[FBNIC_RPC_ACT_TBL_BMC_ALL_MULTI_OFFSET];
149 
150 	/* If we are not enabling the rule just delete it. We will fall
151 	 * back to the RSS rules that support the multicast addresses.
152 	 */
153 	if (!fbnic_bmc_present(fbd) || !fbd->fw_cap.all_multi || enable_host) {
154 		if (act_tcam->state == FBNIC_TCAM_S_VALID)
155 			act_tcam->state = FBNIC_TCAM_S_DELETE;
156 		return;
157 	}
158 
159 	/* Rewrite TCAM rule 23 to handle BMC all-multi traffic */
160 	act_tcam->dest = FIELD_PREP(FBNIC_RPC_ACT_TBL0_DEST_MASK,
161 				    FBNIC_RPC_ACT_TBL0_DEST_BMC);
162 	act_tcam->mask.tcam[0] = 0xffff;
163 
164 	/* MACDA 0 - 3 is reserved for the BMC MAC address */
165 	act_tcam->value.tcam[1] =
166 			FIELD_PREP(FBNIC_RPC_TCAM_ACT1_L2_MACDA_IDX,
167 				   fbd->mac_addr_boundary - 1) |
168 			FBNIC_RPC_TCAM_ACT1_L2_MACDA_VALID;
169 	act_tcam->mask.tcam[1] = 0xffff &
170 			 ~FBNIC_RPC_TCAM_ACT1_L2_MACDA_IDX &
171 			 ~FBNIC_RPC_TCAM_ACT1_L2_MACDA_VALID;
172 
173 	for (j = 2; j < FBNIC_RPC_TCAM_ACT_WORD_LEN; j++)
174 		act_tcam->mask.tcam[j] = 0xffff;
175 
176 	act_tcam->state = FBNIC_TCAM_S_UPDATE;
177 }
178 
179 void fbnic_bmc_rpc_init(struct fbnic_dev *fbd)
180 {
181 	int i = FBNIC_RPC_TCAM_MACDA_BMC_ADDR_IDX;
182 	struct fbnic_act_tcam *act_tcam;
183 	struct fbnic_mac_addr *mac_addr;
184 	int j;
185 
186 	/* Check if BMC is present */
187 	if (!fbnic_bmc_present(fbd))
188 		return;
189 
190 	/* Fetch BMC MAC addresses from firmware capabilities */
191 	for (j = 0; j < 4; j++) {
192 		u8 *bmc_mac = fbd->fw_cap.bmc_mac_addr[j];
193 
194 		/* Validate BMC MAC addresses */
195 		if (is_zero_ether_addr(bmc_mac))
196 			continue;
197 
198 		if (is_multicast_ether_addr(bmc_mac))
199 			mac_addr = __fbnic_mc_sync(fbd, bmc_mac);
200 		else
201 			mac_addr = &fbd->mac_addr[i++];
202 
203 		if (!mac_addr) {
204 			netdev_err(fbd->netdev,
205 				   "No slot for BMC MAC address[%d]\n", j);
206 			continue;
207 		}
208 
209 		ether_addr_copy(mac_addr->value.addr8, bmc_mac);
210 		eth_zero_addr(mac_addr->mask.addr8);
211 
212 		set_bit(FBNIC_MAC_ADDR_T_BMC, mac_addr->act_tcam);
213 		mac_addr->state = FBNIC_TCAM_S_ADD;
214 	}
215 
216 	/* Validate Broadcast is also present, record it and tag it */
217 	mac_addr = &fbd->mac_addr[FBNIC_RPC_TCAM_MACDA_BROADCAST_IDX];
218 	eth_broadcast_addr(mac_addr->value.addr8);
219 	set_bit(FBNIC_MAC_ADDR_T_BMC, mac_addr->act_tcam);
220 	mac_addr->state = FBNIC_TCAM_S_ADD;
221 
222 	/* Rewrite TCAM rule 0 if it isn't present to relocate BMC rules */
223 	act_tcam = &fbd->act_tcam[FBNIC_RPC_ACT_TBL_BMC_OFFSET];
224 	act_tcam->dest = FIELD_PREP(FBNIC_RPC_ACT_TBL0_DEST_MASK,
225 				    FBNIC_RPC_ACT_TBL0_DEST_BMC);
226 	act_tcam->mask.tcam[0] = 0xffff;
227 
228 	/* MACDA 0 - 3 is reserved for the BMC MAC address
229 	 * to account for that we have to mask out the lower 2 bits
230 	 * of the macda by performing an &= with 0x1c.
231 	 */
232 	act_tcam->value.tcam[1] = FBNIC_RPC_TCAM_ACT1_L2_MACDA_VALID;
233 	act_tcam->mask.tcam[1] = 0xffff &
234 			~FIELD_PREP(FBNIC_RPC_TCAM_ACT1_L2_MACDA_IDX, 0x1c) &
235 			~FBNIC_RPC_TCAM_ACT1_L2_MACDA_VALID;
236 
237 	for (j = 2; j < FBNIC_RPC_TCAM_ACT_WORD_LEN; j++)
238 		act_tcam->mask.tcam[j] = 0xffff;
239 
240 	act_tcam->state = FBNIC_TCAM_S_UPDATE;
241 
242 	fbnic_bmc_rpc_all_multi_config(fbd, false);
243 }
244 
245 #define FBNIC_ACT1_INIT(_l4, _udp, _ip, _v6)		\
246 	(((_l4) ? FBNIC_RPC_TCAM_ACT1_L4_VALID : 0) |	\
247 	 ((_udp) ? FBNIC_RPC_TCAM_ACT1_L4_IS_UDP : 0) |	\
248 	 ((_ip) ? FBNIC_RPC_TCAM_ACT1_IP_VALID : 0) |	\
249 	 ((_v6) ? FBNIC_RPC_TCAM_ACT1_IP_IS_V6 : 0))
250 
251 #define FBNIC_TSTAMP_MASK(_all, _udp, _ether)			\
252 	(((_all) ? ((1u << FBNIC_NUM_HASH_OPT) - 1) : 0) |	\
253 	 ((_udp) ? (1u << FBNIC_UDP6_HASH_OPT) |		\
254 		   (1u << FBNIC_UDP4_HASH_OPT) : 0) |		\
255 	 ((_ether) ? (1u << FBNIC_ETHER_HASH_OPT) : 0))
256 
257 void fbnic_rss_reinit(struct fbnic_dev *fbd, struct fbnic_net *fbn)
258 {
259 	static const u32 act1_value[FBNIC_NUM_HASH_OPT] = {
260 		FBNIC_ACT1_INIT(1, 1, 1, 1),	/* UDP6 */
261 		FBNIC_ACT1_INIT(1, 1, 1, 0),	/* UDP4 */
262 		FBNIC_ACT1_INIT(1, 0, 1, 1),	/* TCP6 */
263 		FBNIC_ACT1_INIT(1, 0, 1, 0),	/* TCP4 */
264 		FBNIC_ACT1_INIT(0, 0, 1, 1),	/* IP6 */
265 		FBNIC_ACT1_INIT(0, 0, 1, 0),	/* IP4 */
266 		0				/* Ether */
267 	};
268 	u32 tstamp_mask = 0;
269 	unsigned int i;
270 
271 	/* To support scenarios where a BMC is present we must write the
272 	 * rules twice, once for the unicast cases, and once again for
273 	 * the broadcast/multicast cases as we have to support 2 destinations.
274 	 */
275 	BUILD_BUG_ON(FBNIC_RSS_EN_NUM_UNICAST * 2 != FBNIC_RSS_EN_NUM_ENTRIES);
276 	BUILD_BUG_ON(ARRAY_SIZE(act1_value) != FBNIC_NUM_HASH_OPT);
277 
278 	/* Set timestamp mask with 1b per flow type */
279 	if (fbn->hwtstamp_config.rx_filter != HWTSTAMP_FILTER_NONE) {
280 		switch (fbn->hwtstamp_config.rx_filter) {
281 		case HWTSTAMP_FILTER_ALL:
282 			tstamp_mask = FBNIC_TSTAMP_MASK(1, 1, 1);
283 			break;
284 		case HWTSTAMP_FILTER_PTP_V2_EVENT:
285 			tstamp_mask = FBNIC_TSTAMP_MASK(0, 1, 1);
286 			break;
287 		case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
288 		case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
289 			tstamp_mask = FBNIC_TSTAMP_MASK(0, 1, 0);
290 			break;
291 		case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
292 			tstamp_mask = FBNIC_TSTAMP_MASK(0, 0, 1);
293 			break;
294 		default:
295 			netdev_warn(fbn->netdev, "Unsupported hwtstamp_rx_filter\n");
296 			break;
297 		}
298 	}
299 
300 	/* Program RSS hash enable mask for host in action TCAM/table. */
301 	for (i = fbnic_bmc_present(fbd) ? 0 : FBNIC_RSS_EN_NUM_UNICAST;
302 	     i < FBNIC_RSS_EN_NUM_ENTRIES; i++) {
303 		unsigned int idx = i + FBNIC_RPC_ACT_TBL_RSS_OFFSET;
304 		struct fbnic_act_tcam *act_tcam = &fbd->act_tcam[idx];
305 		u32 flow_hash, dest, rss_en_mask;
306 		int flow_type, j;
307 		u16 value = 0;
308 
309 		flow_type = i % FBNIC_RSS_EN_NUM_UNICAST;
310 		flow_hash = fbn->rss_flow_hash[flow_type];
311 
312 		/* Set DEST_HOST based on absence of RXH_DISCARD */
313 		dest = FIELD_PREP(FBNIC_RPC_ACT_TBL0_DEST_MASK,
314 				  !(RXH_DISCARD & flow_hash) ?
315 				  FBNIC_RPC_ACT_TBL0_DEST_HOST : 0);
316 
317 		if (i >= FBNIC_RSS_EN_NUM_UNICAST && fbnic_bmc_present(fbd))
318 			dest |= FIELD_PREP(FBNIC_RPC_ACT_TBL0_DEST_MASK,
319 					   FBNIC_RPC_ACT_TBL0_DEST_BMC);
320 
321 		if (!dest)
322 			dest = FBNIC_RPC_ACT_TBL0_DROP;
323 		else if (tstamp_mask & (1u << flow_type))
324 			dest |= FBNIC_RPC_ACT_TBL0_TS_ENA;
325 
326 		if (act1_value[flow_type] & FBNIC_RPC_TCAM_ACT1_L4_VALID)
327 			dest |= FIELD_PREP(FBNIC_RPC_ACT_TBL0_DMA_HINT,
328 					   FBNIC_RCD_HDR_AL_DMA_HINT_L4);
329 
330 		rss_en_mask = fbnic_flow_hash_2_rss_en_mask(fbn, flow_type);
331 
332 		act_tcam->dest = dest;
333 		act_tcam->rss_en_mask = rss_en_mask;
334 		act_tcam->state = FBNIC_TCAM_S_UPDATE;
335 
336 		act_tcam->mask.tcam[0] = 0xffff;
337 
338 		/* We reserve the upper 8 MACDA TCAM entries for host
339 		 * unicast. So we set the value to 24, and the mask the
340 		 * lower bits so that the lower entries can be used as
341 		 * multicast or BMC addresses.
342 		 */
343 		if (i < FBNIC_RSS_EN_NUM_UNICAST)
344 			value = FIELD_PREP(FBNIC_RPC_TCAM_ACT1_L2_MACDA_IDX,
345 					   fbd->mac_addr_boundary);
346 		value |= FBNIC_RPC_TCAM_ACT1_L2_MACDA_VALID;
347 
348 		flow_type = i % FBNIC_RSS_EN_NUM_UNICAST;
349 		value |= act1_value[flow_type];
350 
351 		act_tcam->value.tcam[1] = value;
352 		act_tcam->mask.tcam[1] = ~value;
353 
354 		for (j = 2; j < FBNIC_RPC_TCAM_ACT_WORD_LEN; j++)
355 			act_tcam->mask.tcam[j] = 0xffff;
356 
357 		act_tcam->state = FBNIC_TCAM_S_UPDATE;
358 	}
359 }
360 
361 struct fbnic_mac_addr *__fbnic_uc_sync(struct fbnic_dev *fbd,
362 				       const unsigned char *addr)
363 {
364 	struct fbnic_mac_addr *avail_addr = NULL;
365 	unsigned int i;
366 
367 	/* Scan from middle of list to bottom, filling bottom up.
368 	 * Skip the first entry which is reserved for dev_addr and
369 	 * leave the last entry to use for promiscuous filtering.
370 	 */
371 	for (i = fbd->mac_addr_boundary - 1;
372 	     i < FBNIC_RPC_TCAM_MACDA_HOST_ADDR_IDX; i++) {
373 		struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[i];
374 
375 		if (mac_addr->state == FBNIC_TCAM_S_DISABLED) {
376 			avail_addr = mac_addr;
377 		} else if (ether_addr_equal(mac_addr->value.addr8, addr)) {
378 			avail_addr = mac_addr;
379 			break;
380 		}
381 	}
382 
383 	if (avail_addr && avail_addr->state == FBNIC_TCAM_S_DISABLED) {
384 		ether_addr_copy(avail_addr->value.addr8, addr);
385 		eth_zero_addr(avail_addr->mask.addr8);
386 		avail_addr->state = FBNIC_TCAM_S_ADD;
387 	}
388 
389 	return avail_addr;
390 }
391 
392 struct fbnic_mac_addr *__fbnic_mc_sync(struct fbnic_dev *fbd,
393 				       const unsigned char *addr)
394 {
395 	struct fbnic_mac_addr *avail_addr = NULL;
396 	unsigned int i;
397 
398 	/* Scan from middle of list to top, filling top down.
399 	 * Skip over the address reserved for the BMC MAC and
400 	 * exclude index 0 as that belongs to the broadcast address
401 	 */
402 	for (i = fbd->mac_addr_boundary;
403 	     --i > FBNIC_RPC_TCAM_MACDA_BROADCAST_IDX;) {
404 		struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[i];
405 
406 		if (mac_addr->state == FBNIC_TCAM_S_DISABLED) {
407 			avail_addr = mac_addr;
408 		} else if (ether_addr_equal(mac_addr->value.addr8, addr)) {
409 			avail_addr = mac_addr;
410 			break;
411 		}
412 	}
413 
414 	/* Scan the BMC addresses to see if it may have already
415 	 * reserved the address.
416 	 */
417 	while (--i) {
418 		struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[i];
419 
420 		if (!is_zero_ether_addr(mac_addr->mask.addr8))
421 			continue;
422 
423 		/* Only move on if we find a match */
424 		if (!ether_addr_equal(mac_addr->value.addr8, addr))
425 			continue;
426 
427 		/* We need to pull this address to the shared area */
428 		if (avail_addr) {
429 			memcpy(avail_addr, mac_addr, sizeof(*mac_addr));
430 			mac_addr->state = FBNIC_TCAM_S_DELETE;
431 			avail_addr->state = FBNIC_TCAM_S_ADD;
432 		}
433 
434 		break;
435 	}
436 
437 	if (avail_addr && avail_addr->state == FBNIC_TCAM_S_DISABLED) {
438 		ether_addr_copy(avail_addr->value.addr8, addr);
439 		eth_zero_addr(avail_addr->mask.addr8);
440 		avail_addr->state = FBNIC_TCAM_S_ADD;
441 	}
442 
443 	return avail_addr;
444 }
445 
446 int __fbnic_xc_unsync(struct fbnic_mac_addr *mac_addr, unsigned int tcam_idx)
447 {
448 	if (!test_and_clear_bit(tcam_idx, mac_addr->act_tcam))
449 		return -ENOENT;
450 
451 	if (bitmap_empty(mac_addr->act_tcam, FBNIC_RPC_TCAM_ACT_NUM_ENTRIES))
452 		mac_addr->state = FBNIC_TCAM_S_DELETE;
453 
454 	return 0;
455 }
456 
457 void fbnic_sift_macda(struct fbnic_dev *fbd)
458 {
459 	int dest, src;
460 
461 	/* Move BMC only addresses back into BMC region */
462 	for (dest = FBNIC_RPC_TCAM_MACDA_BMC_ADDR_IDX,
463 	     src = FBNIC_RPC_TCAM_MACDA_MULTICAST_IDX;
464 	     ++dest < FBNIC_RPC_TCAM_MACDA_BROADCAST_IDX &&
465 	     src < fbd->mac_addr_boundary;) {
466 		struct fbnic_mac_addr *dest_addr = &fbd->mac_addr[dest];
467 
468 		if (dest_addr->state != FBNIC_TCAM_S_DISABLED)
469 			continue;
470 
471 		while (src < fbd->mac_addr_boundary) {
472 			struct fbnic_mac_addr *src_addr = &fbd->mac_addr[src++];
473 
474 			/* Verify BMC bit is set */
475 			if (!test_bit(FBNIC_MAC_ADDR_T_BMC, src_addr->act_tcam))
476 				continue;
477 
478 			/* Verify filter isn't already disabled */
479 			if (src_addr->state == FBNIC_TCAM_S_DISABLED ||
480 			    src_addr->state == FBNIC_TCAM_S_DELETE)
481 				continue;
482 
483 			/* Verify only BMC bit is set */
484 			if (bitmap_weight(src_addr->act_tcam,
485 					  FBNIC_RPC_TCAM_ACT_NUM_ENTRIES) != 1)
486 				continue;
487 
488 			/* Verify we are not moving wildcard address */
489 			if (!is_zero_ether_addr(src_addr->mask.addr8))
490 				continue;
491 
492 			memcpy(dest_addr, src_addr, sizeof(*src_addr));
493 			src_addr->state = FBNIC_TCAM_S_DELETE;
494 			dest_addr->state = FBNIC_TCAM_S_ADD;
495 		}
496 	}
497 }
498 
499 static void fbnic_clear_macda_entry(struct fbnic_dev *fbd, unsigned int idx)
500 {
501 	int i;
502 
503 	/* Invalidate entry and clear addr state info */
504 	for (i = 0; i <= FBNIC_RPC_TCAM_MACDA_WORD_LEN; i++)
505 		wr32(fbd, FBNIC_RPC_TCAM_MACDA(idx, i), 0);
506 }
507 
508 static void fbnic_clear_macda(struct fbnic_dev *fbd)
509 {
510 	int idx;
511 
512 	for (idx = ARRAY_SIZE(fbd->mac_addr); idx--;) {
513 		struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[idx];
514 
515 		if (mac_addr->state == FBNIC_TCAM_S_DISABLED)
516 			continue;
517 
518 		if (test_bit(FBNIC_MAC_ADDR_T_BMC, mac_addr->act_tcam)) {
519 			if (fbnic_bmc_present(fbd))
520 				continue;
521 			dev_warn_once(fbd->dev,
522 				      "Found BMC MAC address w/ BMC not present\n");
523 		}
524 
525 		fbnic_clear_macda_entry(fbd, idx);
526 
527 		/* If rule was already destined for deletion just wipe it now */
528 		if (mac_addr->state == FBNIC_TCAM_S_DELETE) {
529 			memset(mac_addr, 0, sizeof(*mac_addr));
530 			continue;
531 		}
532 
533 		/* Change state to update so that we will rewrite
534 		 * this tcam the next time fbnic_write_macda is called.
535 		 */
536 		mac_addr->state = FBNIC_TCAM_S_UPDATE;
537 	}
538 }
539 
540 static void fbnic_write_macda_entry(struct fbnic_dev *fbd, unsigned int idx,
541 				    struct fbnic_mac_addr *mac_addr)
542 {
543 	__be16 *mask, *value;
544 	int i;
545 
546 	mask = &mac_addr->mask.addr16[FBNIC_RPC_TCAM_MACDA_WORD_LEN - 1];
547 	value = &mac_addr->value.addr16[FBNIC_RPC_TCAM_MACDA_WORD_LEN - 1];
548 
549 	for (i = 0; i < FBNIC_RPC_TCAM_MACDA_WORD_LEN; i++)
550 		wr32(fbd, FBNIC_RPC_TCAM_MACDA(idx, i),
551 		     FIELD_PREP(FBNIC_RPC_TCAM_MACDA_MASK, ntohs(*mask--)) |
552 		     FIELD_PREP(FBNIC_RPC_TCAM_MACDA_VALUE, ntohs(*value--)));
553 
554 	wrfl(fbd);
555 
556 	wr32(fbd, FBNIC_RPC_TCAM_MACDA(idx, i), FBNIC_RPC_TCAM_VALIDATE);
557 }
558 
559 void fbnic_write_macda(struct fbnic_dev *fbd)
560 {
561 	int idx;
562 
563 	for (idx = ARRAY_SIZE(fbd->mac_addr); idx--;) {
564 		struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[idx];
565 
566 		/* Check if update flag is set else exit. */
567 		if (!(mac_addr->state & FBNIC_TCAM_S_UPDATE))
568 			continue;
569 
570 		/* Clear by writing 0s. */
571 		if (mac_addr->state == FBNIC_TCAM_S_DELETE) {
572 			/* Invalidate entry and clear addr state info */
573 			fbnic_clear_macda_entry(fbd, idx);
574 			memset(mac_addr, 0, sizeof(*mac_addr));
575 
576 			continue;
577 		}
578 
579 		fbnic_write_macda_entry(fbd, idx, mac_addr);
580 
581 		mac_addr->state = FBNIC_TCAM_S_VALID;
582 	}
583 }
584 
585 static void fbnic_clear_act_tcam(struct fbnic_dev *fbd, unsigned int idx)
586 {
587 	int i;
588 
589 	/* Invalidate entry and clear addr state info */
590 	for (i = 0; i <= FBNIC_RPC_TCAM_ACT_WORD_LEN; i++)
591 		wr32(fbd, FBNIC_RPC_TCAM_ACT(idx, i), 0);
592 }
593 
594 static void fbnic_clear_tce_tcam_entry(struct fbnic_dev *fbd, unsigned int idx)
595 {
596 	int i;
597 
598 	/* Invalidate entry and clear addr state info */
599 	for (i = 0; i <= FBNIC_TCE_TCAM_WORD_LEN; i++)
600 		wr32(fbd, FBNIC_TCE_RAM_TCAM(idx, i), 0);
601 }
602 
603 static void fbnic_write_tce_tcam_dest(struct fbnic_dev *fbd, unsigned int idx,
604 				      struct fbnic_mac_addr *mac_addr)
605 {
606 	u32 dest = FBNIC_TCE_TCAM_DEST_BMC;
607 	u32 idx2dest_map;
608 
609 	if (is_multicast_ether_addr(mac_addr->value.addr8))
610 		dest |= FBNIC_TCE_TCAM_DEST_MAC;
611 
612 	idx2dest_map = rd32(fbd, FBNIC_TCE_TCAM_IDX2DEST_MAP);
613 	idx2dest_map &= ~(FBNIC_TCE_TCAM_IDX2DEST_MAP_DEST_ID_0 << (4 * idx));
614 	idx2dest_map |= dest << (4 * idx);
615 
616 	wr32(fbd, FBNIC_TCE_TCAM_IDX2DEST_MAP, idx2dest_map);
617 }
618 
619 static void fbnic_write_tce_tcam_entry(struct fbnic_dev *fbd, unsigned int idx,
620 				       struct fbnic_mac_addr *mac_addr)
621 {
622 	__be16 *mask, *value;
623 	int i;
624 
625 	mask = &mac_addr->mask.addr16[FBNIC_TCE_TCAM_WORD_LEN - 1];
626 	value = &mac_addr->value.addr16[FBNIC_TCE_TCAM_WORD_LEN - 1];
627 
628 	for (i = 0; i < FBNIC_TCE_TCAM_WORD_LEN; i++)
629 		wr32(fbd, FBNIC_TCE_RAM_TCAM(idx, i),
630 		     FIELD_PREP(FBNIC_TCE_RAM_TCAM_MASK, ntohs(*mask--)) |
631 		     FIELD_PREP(FBNIC_TCE_RAM_TCAM_VALUE, ntohs(*value--)));
632 
633 	wrfl(fbd);
634 
635 	wr32(fbd, FBNIC_TCE_RAM_TCAM3(idx), FBNIC_TCE_RAM_TCAM3_MCQ_MASK |
636 				       FBNIC_TCE_RAM_TCAM3_DEST_MASK |
637 				       FBNIC_TCE_RAM_TCAM3_VALIDATE);
638 }
639 
640 static void __fbnic_write_tce_tcam_rev(struct fbnic_dev *fbd)
641 {
642 	int tcam_idx = FBNIC_TCE_TCAM_NUM_ENTRIES;
643 	int mac_idx;
644 
645 	for (mac_idx = ARRAY_SIZE(fbd->mac_addr); mac_idx--;) {
646 		struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[mac_idx];
647 
648 		/* Verify BMC bit is set */
649 		if (!test_bit(FBNIC_MAC_ADDR_T_BMC, mac_addr->act_tcam))
650 			continue;
651 
652 		if (!tcam_idx) {
653 			dev_err(fbd->dev, "TCE TCAM overflow\n");
654 			return;
655 		}
656 
657 		tcam_idx--;
658 		fbnic_write_tce_tcam_dest(fbd, tcam_idx, mac_addr);
659 		fbnic_write_tce_tcam_entry(fbd, tcam_idx, mac_addr);
660 	}
661 
662 	while (tcam_idx)
663 		fbnic_clear_tce_tcam_entry(fbd, --tcam_idx);
664 
665 	fbd->tce_tcam_last = tcam_idx;
666 }
667 
668 static void __fbnic_write_tce_tcam(struct fbnic_dev *fbd)
669 {
670 	int tcam_idx = 0;
671 	int mac_idx;
672 
673 	for (mac_idx = 0; mac_idx < ARRAY_SIZE(fbd->mac_addr); mac_idx++) {
674 		struct fbnic_mac_addr *mac_addr = &fbd->mac_addr[mac_idx];
675 
676 		/* Verify BMC bit is set */
677 		if (!test_bit(FBNIC_MAC_ADDR_T_BMC, mac_addr->act_tcam))
678 			continue;
679 
680 		if (tcam_idx == FBNIC_TCE_TCAM_NUM_ENTRIES) {
681 			dev_err(fbd->dev, "TCE TCAM overflow\n");
682 			return;
683 		}
684 
685 		fbnic_write_tce_tcam_dest(fbd, tcam_idx, mac_addr);
686 		fbnic_write_tce_tcam_entry(fbd, tcam_idx, mac_addr);
687 		tcam_idx++;
688 	}
689 
690 	while (tcam_idx < FBNIC_TCE_TCAM_NUM_ENTRIES)
691 		fbnic_clear_tce_tcam_entry(fbd, tcam_idx++);
692 
693 	fbd->tce_tcam_last = tcam_idx;
694 }
695 
696 void fbnic_write_tce_tcam(struct fbnic_dev *fbd)
697 {
698 	if (fbd->tce_tcam_last)
699 		__fbnic_write_tce_tcam_rev(fbd);
700 	else
701 		__fbnic_write_tce_tcam(fbd);
702 }
703 
704 struct fbnic_ip_addr *__fbnic_ip4_sync(struct fbnic_dev *fbd,
705 				       struct fbnic_ip_addr *ip_addr,
706 				       const struct in_addr *addr,
707 				       const struct in_addr *mask)
708 {
709 	struct fbnic_ip_addr *avail_addr = NULL;
710 	unsigned int i;
711 
712 	/* Scan from top of list to bottom, filling bottom up. */
713 	for (i = 0; i < FBNIC_RPC_TCAM_IP_ADDR_NUM_ENTRIES; i++, ip_addr++) {
714 		struct in6_addr *m = &ip_addr->mask;
715 
716 		if (ip_addr->state == FBNIC_TCAM_S_DISABLED) {
717 			avail_addr = ip_addr;
718 			continue;
719 		}
720 
721 		if (ip_addr->version != 4)
722 			continue;
723 
724 		/* Drop avail_addr if mask is a subset of our current mask,
725 		 * This prevents us from inserting a longer prefix behind a
726 		 * shorter one.
727 		 *
728 		 * The mask is stored inverted value so as an example:
729 		 * m	ffff ffff ffff ffff ffff ffff ffff 0000 0000
730 		 * mask 0000 0000 0000 0000 0000 0000 0000 ffff ffff
731 		 *
732 		 * "m" and "mask" represent typical IPv4 mask stored in
733 		 * the TCAM and those provided by the stack. The code below
734 		 * should return a non-zero result if there is a 0 stored
735 		 * anywhere in "m" where "mask" has a 0.
736 		 */
737 		if (~m->s6_addr32[3] & ~mask->s_addr) {
738 			avail_addr = NULL;
739 			continue;
740 		}
741 
742 		/* Check to see if the mask actually contains fewer bits than
743 		 * our new mask "m". The XOR below should only result in 0 if
744 		 * "m" is masking a bit that we are looking for in our new
745 		 * "mask", we eliminated the 0^0 case with the check above.
746 		 *
747 		 * If it contains fewer bits we need to stop here, otherwise
748 		 * we might be adding an unreachable rule.
749 		 */
750 		if (~(m->s6_addr32[3] ^ mask->s_addr))
751 			break;
752 
753 		if (ip_addr->value.s6_addr32[3] == addr->s_addr) {
754 			avail_addr = ip_addr;
755 			break;
756 		}
757 	}
758 
759 	if (avail_addr && avail_addr->state == FBNIC_TCAM_S_DISABLED) {
760 		ipv6_addr_set(&avail_addr->value, 0, 0, 0, addr->s_addr);
761 		ipv6_addr_set(&avail_addr->mask, htonl(~0), htonl(~0),
762 			      htonl(~0), ~mask->s_addr);
763 		avail_addr->version = 4;
764 
765 		avail_addr->state = FBNIC_TCAM_S_ADD;
766 	}
767 
768 	return avail_addr;
769 }
770 
771 struct fbnic_ip_addr *__fbnic_ip6_sync(struct fbnic_dev *fbd,
772 				       struct fbnic_ip_addr *ip_addr,
773 				       const struct in6_addr *addr,
774 				       const struct in6_addr *mask)
775 {
776 	struct fbnic_ip_addr *avail_addr = NULL;
777 	unsigned int i;
778 
779 	ip_addr = &ip_addr[FBNIC_RPC_TCAM_IP_ADDR_NUM_ENTRIES - 1];
780 
781 	/* Scan from bottom of list to top, filling top down. */
782 	for (i = FBNIC_RPC_TCAM_IP_ADDR_NUM_ENTRIES; i--; ip_addr--) {
783 		struct in6_addr *m = &ip_addr->mask;
784 
785 		if (ip_addr->state == FBNIC_TCAM_S_DISABLED) {
786 			avail_addr = ip_addr;
787 			continue;
788 		}
789 
790 		if (ip_addr->version != 6)
791 			continue;
792 
793 		/* Drop avail_addr if mask is a superset of our current mask.
794 		 * This prevents us from inserting a longer prefix behind a
795 		 * shorter one.
796 		 *
797 		 * The mask is stored inverted value so as an example:
798 		 * m	0000 0000 0000 0000 0000 0000 0000 0000 0000
799 		 * mask ffff ffff ffff ffff ffff ffff ffff ffff ffff
800 		 *
801 		 * "m" and "mask" represent typical IPv6 mask stored in
802 		 * the TCAM and those provided by the stack. The code below
803 		 * should return a non-zero result which will cause us
804 		 * to drop the avail_addr value that might be cached
805 		 * to prevent us from dropping a v6 address behind it.
806 		 */
807 		if ((m->s6_addr32[0] & mask->s6_addr32[0]) |
808 		    (m->s6_addr32[1] & mask->s6_addr32[1]) |
809 		    (m->s6_addr32[2] & mask->s6_addr32[2]) |
810 		    (m->s6_addr32[3] & mask->s6_addr32[3])) {
811 			avail_addr = NULL;
812 			continue;
813 		}
814 
815 		/* The previous test eliminated any overlap between the
816 		 * two values so now we need to check for gaps.
817 		 *
818 		 * If the mask is equal to our current mask then it should
819 		 * result with m ^ mask = ffff ffff, if however the value
820 		 * stored in m is bigger then we should see a 0 appear
821 		 * somewhere in the mask.
822 		 */
823 		if (~(m->s6_addr32[0] ^ mask->s6_addr32[0]) |
824 		    ~(m->s6_addr32[1] ^ mask->s6_addr32[1]) |
825 		    ~(m->s6_addr32[2] ^ mask->s6_addr32[2]) |
826 		    ~(m->s6_addr32[3] ^ mask->s6_addr32[3]))
827 			break;
828 
829 		if (ipv6_addr_cmp(&ip_addr->value, addr))
830 			continue;
831 
832 		avail_addr = ip_addr;
833 		break;
834 	}
835 
836 	if (avail_addr && avail_addr->state == FBNIC_TCAM_S_DISABLED) {
837 		memcpy(&avail_addr->value, addr, sizeof(*addr));
838 		ipv6_addr_set(&avail_addr->mask,
839 			      ~mask->s6_addr32[0], ~mask->s6_addr32[1],
840 			      ~mask->s6_addr32[2], ~mask->s6_addr32[3]);
841 		avail_addr->version = 6;
842 
843 		avail_addr->state = FBNIC_TCAM_S_ADD;
844 	}
845 
846 	return avail_addr;
847 }
848 
849 int __fbnic_ip_unsync(struct fbnic_ip_addr *ip_addr, unsigned int tcam_idx)
850 {
851 	if (!test_and_clear_bit(tcam_idx, ip_addr->act_tcam))
852 		return -ENOENT;
853 
854 	if (bitmap_empty(ip_addr->act_tcam, FBNIC_RPC_TCAM_ACT_NUM_ENTRIES))
855 		ip_addr->state = FBNIC_TCAM_S_DELETE;
856 
857 	return 0;
858 }
859 
860 static void fbnic_clear_ip_src_entry(struct fbnic_dev *fbd, unsigned int idx)
861 {
862 	int i;
863 
864 	/* Invalidate entry and clear addr state info */
865 	for (i = 0; i <= FBNIC_RPC_TCAM_IP_ADDR_WORD_LEN; i++)
866 		wr32(fbd, FBNIC_RPC_TCAM_IPSRC(idx, i), 0);
867 }
868 
869 static void fbnic_clear_ip_dst_entry(struct fbnic_dev *fbd, unsigned int idx)
870 {
871 	int i;
872 
873 	/* Invalidate entry and clear addr state info */
874 	for (i = 0; i <= FBNIC_RPC_TCAM_IP_ADDR_WORD_LEN; i++)
875 		wr32(fbd, FBNIC_RPC_TCAM_IPDST(idx, i), 0);
876 }
877 
878 static void fbnic_clear_ip_outer_src_entry(struct fbnic_dev *fbd,
879 					   unsigned int idx)
880 {
881 	int i;
882 
883 	/* Invalidate entry and clear addr state info */
884 	for (i = 0; i <= FBNIC_RPC_TCAM_IP_ADDR_WORD_LEN; i++)
885 		wr32(fbd, FBNIC_RPC_TCAM_OUTER_IPSRC(idx, i), 0);
886 }
887 
888 static void fbnic_clear_ip_outer_dst_entry(struct fbnic_dev *fbd,
889 					   unsigned int idx)
890 {
891 	int i;
892 
893 	/* Invalidate entry and clear addr state info */
894 	for (i = 0; i <= FBNIC_RPC_TCAM_IP_ADDR_WORD_LEN; i++)
895 		wr32(fbd, FBNIC_RPC_TCAM_OUTER_IPDST(idx, i), 0);
896 }
897 
898 static void fbnic_write_ip_src_entry(struct fbnic_dev *fbd, unsigned int idx,
899 				     struct fbnic_ip_addr *ip_addr)
900 {
901 	__be16 *mask, *value;
902 	int i;
903 
904 	mask = &ip_addr->mask.s6_addr16[FBNIC_RPC_TCAM_IP_ADDR_WORD_LEN - 1];
905 	value = &ip_addr->value.s6_addr16[FBNIC_RPC_TCAM_IP_ADDR_WORD_LEN - 1];
906 
907 	for (i = 0; i < FBNIC_RPC_TCAM_IP_ADDR_WORD_LEN; i++)
908 		wr32(fbd, FBNIC_RPC_TCAM_IPSRC(idx, i),
909 		     FIELD_PREP(FBNIC_RPC_TCAM_IP_ADDR_MASK, ntohs(*mask--)) |
910 		     FIELD_PREP(FBNIC_RPC_TCAM_IP_ADDR_VALUE, ntohs(*value--)));
911 	wrfl(fbd);
912 
913 	/* Bit 129 is used to flag for v4/v6 */
914 	wr32(fbd, FBNIC_RPC_TCAM_IPSRC(idx, i),
915 	     (ip_addr->version == 6) | FBNIC_RPC_TCAM_VALIDATE);
916 }
917 
918 static void fbnic_write_ip_dst_entry(struct fbnic_dev *fbd, unsigned int idx,
919 				     struct fbnic_ip_addr *ip_addr)
920 {
921 	__be16 *mask, *value;
922 	int i;
923 
924 	mask = &ip_addr->mask.s6_addr16[FBNIC_RPC_TCAM_IP_ADDR_WORD_LEN - 1];
925 	value = &ip_addr->value.s6_addr16[FBNIC_RPC_TCAM_IP_ADDR_WORD_LEN - 1];
926 
927 	for (i = 0; i < FBNIC_RPC_TCAM_IP_ADDR_WORD_LEN; i++)
928 		wr32(fbd, FBNIC_RPC_TCAM_IPDST(idx, i),
929 		     FIELD_PREP(FBNIC_RPC_TCAM_IP_ADDR_MASK, ntohs(*mask--)) |
930 		     FIELD_PREP(FBNIC_RPC_TCAM_IP_ADDR_VALUE, ntohs(*value--)));
931 	wrfl(fbd);
932 
933 	/* Bit 129 is used to flag for v4/v6 */
934 	wr32(fbd, FBNIC_RPC_TCAM_IPDST(idx, i),
935 	     (ip_addr->version == 6) | FBNIC_RPC_TCAM_VALIDATE);
936 }
937 
938 static void fbnic_write_ip_outer_src_entry(struct fbnic_dev *fbd,
939 					   unsigned int idx,
940 					   struct fbnic_ip_addr *ip_addr)
941 {
942 	__be16 *mask, *value;
943 	int i;
944 
945 	mask = &ip_addr->mask.s6_addr16[FBNIC_RPC_TCAM_IP_ADDR_WORD_LEN - 1];
946 	value = &ip_addr->value.s6_addr16[FBNIC_RPC_TCAM_IP_ADDR_WORD_LEN - 1];
947 
948 	for (i = 0; i < FBNIC_RPC_TCAM_IP_ADDR_WORD_LEN; i++)
949 		wr32(fbd, FBNIC_RPC_TCAM_OUTER_IPSRC(idx, i),
950 		     FIELD_PREP(FBNIC_RPC_TCAM_IP_ADDR_MASK, ntohs(*mask--)) |
951 		     FIELD_PREP(FBNIC_RPC_TCAM_IP_ADDR_VALUE, ntohs(*value--)));
952 	wrfl(fbd);
953 
954 	wr32(fbd, FBNIC_RPC_TCAM_OUTER_IPSRC(idx, i), FBNIC_RPC_TCAM_VALIDATE);
955 }
956 
957 static void fbnic_write_ip_outer_dst_entry(struct fbnic_dev *fbd,
958 					   unsigned int idx,
959 					   struct fbnic_ip_addr *ip_addr)
960 {
961 	__be16 *mask, *value;
962 	int i;
963 
964 	mask = &ip_addr->mask.s6_addr16[FBNIC_RPC_TCAM_IP_ADDR_WORD_LEN - 1];
965 	value = &ip_addr->value.s6_addr16[FBNIC_RPC_TCAM_IP_ADDR_WORD_LEN - 1];
966 
967 	for (i = 0; i < FBNIC_RPC_TCAM_IP_ADDR_WORD_LEN; i++)
968 		wr32(fbd, FBNIC_RPC_TCAM_OUTER_IPDST(idx, i),
969 		     FIELD_PREP(FBNIC_RPC_TCAM_IP_ADDR_MASK, ntohs(*mask--)) |
970 		     FIELD_PREP(FBNIC_RPC_TCAM_IP_ADDR_VALUE, ntohs(*value--)));
971 	wrfl(fbd);
972 
973 	wr32(fbd, FBNIC_RPC_TCAM_OUTER_IPDST(idx, i), FBNIC_RPC_TCAM_VALIDATE);
974 }
975 
976 void fbnic_write_ip_addr(struct fbnic_dev *fbd)
977 {
978 	int idx;
979 
980 	for (idx = ARRAY_SIZE(fbd->ip_src); idx--;) {
981 		struct fbnic_ip_addr *ip_addr = &fbd->ip_src[idx];
982 
983 		/* Check if update flag is set else skip. */
984 		if (!(ip_addr->state & FBNIC_TCAM_S_UPDATE))
985 			continue;
986 
987 		/* Clear by writing 0s. */
988 		if (ip_addr->state == FBNIC_TCAM_S_DELETE) {
989 			/* Invalidate entry and clear addr state info */
990 			fbnic_clear_ip_src_entry(fbd, idx);
991 			memset(ip_addr, 0, sizeof(*ip_addr));
992 
993 			continue;
994 		}
995 
996 		fbnic_write_ip_src_entry(fbd, idx, ip_addr);
997 
998 		ip_addr->state = FBNIC_TCAM_S_VALID;
999 	}
1000 
1001 	/* Repeat process for other IP TCAMs */
1002 	for (idx = ARRAY_SIZE(fbd->ip_dst); idx--;) {
1003 		struct fbnic_ip_addr *ip_addr = &fbd->ip_dst[idx];
1004 
1005 		if (!(ip_addr->state & FBNIC_TCAM_S_UPDATE))
1006 			continue;
1007 
1008 		if (ip_addr->state == FBNIC_TCAM_S_DELETE) {
1009 			fbnic_clear_ip_dst_entry(fbd, idx);
1010 			memset(ip_addr, 0, sizeof(*ip_addr));
1011 
1012 			continue;
1013 		}
1014 
1015 		fbnic_write_ip_dst_entry(fbd, idx, ip_addr);
1016 
1017 		ip_addr->state = FBNIC_TCAM_S_VALID;
1018 	}
1019 
1020 	for (idx = ARRAY_SIZE(fbd->ipo_src); idx--;) {
1021 		struct fbnic_ip_addr *ip_addr = &fbd->ipo_src[idx];
1022 
1023 		if (!(ip_addr->state & FBNIC_TCAM_S_UPDATE))
1024 			continue;
1025 
1026 		if (ip_addr->state == FBNIC_TCAM_S_DELETE) {
1027 			fbnic_clear_ip_outer_src_entry(fbd, idx);
1028 			memset(ip_addr, 0, sizeof(*ip_addr));
1029 
1030 			continue;
1031 		}
1032 
1033 		fbnic_write_ip_outer_src_entry(fbd, idx, ip_addr);
1034 
1035 		ip_addr->state = FBNIC_TCAM_S_VALID;
1036 	}
1037 
1038 	for (idx = ARRAY_SIZE(fbd->ipo_dst); idx--;) {
1039 		struct fbnic_ip_addr *ip_addr = &fbd->ipo_dst[idx];
1040 
1041 		if (!(ip_addr->state & FBNIC_TCAM_S_UPDATE))
1042 			continue;
1043 
1044 		if (ip_addr->state == FBNIC_TCAM_S_DELETE) {
1045 			fbnic_clear_ip_outer_dst_entry(fbd, idx);
1046 			memset(ip_addr, 0, sizeof(*ip_addr));
1047 
1048 			continue;
1049 		}
1050 
1051 		fbnic_write_ip_outer_dst_entry(fbd, idx, ip_addr);
1052 
1053 		ip_addr->state = FBNIC_TCAM_S_VALID;
1054 	}
1055 }
1056 
1057 void fbnic_clear_rules(struct fbnic_dev *fbd)
1058 {
1059 	u32 dest = FIELD_PREP(FBNIC_RPC_ACT_TBL0_DEST_MASK,
1060 			      FBNIC_RPC_ACT_TBL0_DEST_BMC);
1061 	int i = FBNIC_RPC_TCAM_ACT_NUM_ENTRIES - 1;
1062 	struct fbnic_act_tcam *act_tcam;
1063 
1064 	/* Clear MAC rules */
1065 	fbnic_clear_macda(fbd);
1066 
1067 	/* If BMC is present we need to preserve the last rule which
1068 	 * will be used to route traffic to the BMC if it is received.
1069 	 *
1070 	 * At this point it should be the only MAC address in the MACDA
1071 	 * so any unicast or multicast traffic received should be routed
1072 	 * to it. So leave the last rule in place.
1073 	 *
1074 	 * It will be rewritten to add the host again when we bring
1075 	 * the interface back up.
1076 	 */
1077 	if (fbnic_bmc_present(fbd)) {
1078 		act_tcam = &fbd->act_tcam[i];
1079 
1080 		if (act_tcam->state == FBNIC_TCAM_S_VALID &&
1081 		    (act_tcam->dest & dest)) {
1082 			wr32(fbd, FBNIC_RPC_ACT_TBL0(i), dest);
1083 			wr32(fbd, FBNIC_RPC_ACT_TBL1(i), 0);
1084 
1085 			act_tcam->state = FBNIC_TCAM_S_UPDATE;
1086 
1087 			i--;
1088 		}
1089 	}
1090 
1091 	/* Work from the bottom up deleting all other rules from hardware */
1092 	do {
1093 		act_tcam = &fbd->act_tcam[i];
1094 
1095 		if (act_tcam->state != FBNIC_TCAM_S_VALID)
1096 			continue;
1097 
1098 		fbnic_clear_act_tcam(fbd, i);
1099 		act_tcam->state = FBNIC_TCAM_S_UPDATE;
1100 	} while (i--);
1101 }
1102 
1103 static void fbnic_delete_act_tcam(struct fbnic_dev *fbd, unsigned int idx)
1104 {
1105 	fbnic_clear_act_tcam(fbd, idx);
1106 	memset(&fbd->act_tcam[idx], 0, sizeof(struct fbnic_act_tcam));
1107 }
1108 
1109 static void fbnic_update_act_tcam(struct fbnic_dev *fbd, unsigned int idx)
1110 {
1111 	struct fbnic_act_tcam *act_tcam = &fbd->act_tcam[idx];
1112 	int i;
1113 
1114 	/* Update entry by writing the destination and RSS mask */
1115 	wr32(fbd, FBNIC_RPC_ACT_TBL0(idx), act_tcam->dest);
1116 	wr32(fbd, FBNIC_RPC_ACT_TBL1(idx), act_tcam->rss_en_mask);
1117 
1118 	/* Write new TCAM rule to hardware */
1119 	for (i = 0; i < FBNIC_RPC_TCAM_ACT_WORD_LEN; i++)
1120 		wr32(fbd, FBNIC_RPC_TCAM_ACT(idx, i),
1121 		     FIELD_PREP(FBNIC_RPC_TCAM_ACT_MASK,
1122 				act_tcam->mask.tcam[i]) |
1123 		     FIELD_PREP(FBNIC_RPC_TCAM_ACT_VALUE,
1124 				act_tcam->value.tcam[i]));
1125 
1126 	wrfl(fbd);
1127 
1128 	wr32(fbd, FBNIC_RPC_TCAM_ACT(idx, i), FBNIC_RPC_TCAM_VALIDATE);
1129 	act_tcam->state = FBNIC_TCAM_S_VALID;
1130 }
1131 
1132 void fbnic_write_rules(struct fbnic_dev *fbd)
1133 {
1134 	int i;
1135 
1136 	/* Flush any pending action table rules */
1137 	for (i = 0; i < FBNIC_RPC_ACT_TBL_NUM_ENTRIES; i++) {
1138 		struct fbnic_act_tcam *act_tcam = &fbd->act_tcam[i];
1139 
1140 		/* Check if update flag is set else exit. */
1141 		if (!(act_tcam->state & FBNIC_TCAM_S_UPDATE))
1142 			continue;
1143 
1144 		if (act_tcam->state == FBNIC_TCAM_S_DELETE)
1145 			fbnic_delete_act_tcam(fbd, i);
1146 		else
1147 			fbnic_update_act_tcam(fbd, i);
1148 	}
1149 }
1150