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