1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell RVU Admin Function driver
3  *
4  * Copyright (C) 2020 Marvell.
5  */
6 
7 #include <linux/bitfield.h>
8 
9 #include "rvu_struct.h"
10 #include "rvu_reg.h"
11 #include "rvu.h"
12 #include "npc.h"
13 #include "rvu_npc_fs.h"
14 #include "rvu_npc_hash.h"
15 
16 static const char * const npc_flow_names[] = {
17 	[NPC_DMAC]	= "dmac",
18 	[NPC_SMAC]	= "smac",
19 	[NPC_ETYPE]	= "ether type",
20 	[NPC_VLAN_ETYPE_CTAG] = "vlan ether type ctag",
21 	[NPC_VLAN_ETYPE_STAG] = "vlan ether type stag",
22 	[NPC_OUTER_VID]	= "outer vlan id",
23 	[NPC_INNER_VID]	= "inner vlan id",
24 	[NPC_TOS]	= "tos",
25 	[NPC_IPFRAG_IPV4] = "fragmented IPv4 header ",
26 	[NPC_SIP_IPV4]	= "ipv4 source ip",
27 	[NPC_DIP_IPV4]	= "ipv4 destination ip",
28 	[NPC_IPFRAG_IPV6] = "fragmented IPv6 header ",
29 	[NPC_SIP_IPV6]	= "ipv6 source ip",
30 	[NPC_DIP_IPV6]	= "ipv6 destination ip",
31 	[NPC_IPPROTO_TCP] = "ip proto tcp",
32 	[NPC_IPPROTO_UDP] = "ip proto udp",
33 	[NPC_IPPROTO_SCTP] = "ip proto sctp",
34 	[NPC_IPPROTO_ICMP] = "ip proto icmp",
35 	[NPC_IPPROTO_ICMP6] = "ip proto icmp6",
36 	[NPC_IPPROTO_AH] = "ip proto AH",
37 	[NPC_IPPROTO_ESP] = "ip proto ESP",
38 	[NPC_SPORT_TCP]	= "tcp source port",
39 	[NPC_DPORT_TCP]	= "tcp destination port",
40 	[NPC_SPORT_UDP]	= "udp source port",
41 	[NPC_DPORT_UDP]	= "udp destination port",
42 	[NPC_SPORT_SCTP] = "sctp source port",
43 	[NPC_DPORT_SCTP] = "sctp destination port",
44 	[NPC_LXMB]	= "Mcast/Bcast header ",
45 	[NPC_IPSEC_SPI] = "SPI ",
46 	[NPC_UNKNOWN]	= "unknown",
47 };
48 
49 bool npc_is_feature_supported(struct rvu *rvu, u64 features, u8 intf)
50 {
51 	struct npc_mcam *mcam = &rvu->hw->mcam;
52 	u64 mcam_features;
53 	u64 unsupported;
54 
55 	mcam_features = is_npc_intf_tx(intf) ? mcam->tx_features : mcam->rx_features;
56 	unsupported = (mcam_features ^ features) & ~mcam_features;
57 
58 	/* Return false if at least one of the input flows is not extracted */
59 	return !unsupported;
60 }
61 
62 const char *npc_get_field_name(u8 hdr)
63 {
64 	if (hdr >= ARRAY_SIZE(npc_flow_names))
65 		return npc_flow_names[NPC_UNKNOWN];
66 
67 	return npc_flow_names[hdr];
68 }
69 
70 /* Compute keyword masks and figure out the number of keywords a field
71  * spans in the key.
72  */
73 static void npc_set_kw_masks(struct npc_mcam *mcam, u8 type,
74 			     u8 nr_bits, int start_kwi, int offset, u8 intf)
75 {
76 	struct npc_key_field *field = &mcam->rx_key_fields[type];
77 	u8 bits_in_kw;
78 	int max_kwi;
79 
80 	if (mcam->banks_per_entry == 1)
81 		max_kwi = 1; /* NPC_MCAM_KEY_X1 */
82 	else if (mcam->banks_per_entry == 2)
83 		max_kwi = 3; /* NPC_MCAM_KEY_X2 */
84 	else
85 		max_kwi = 6; /* NPC_MCAM_KEY_X4 */
86 
87 	if (is_npc_intf_tx(intf))
88 		field = &mcam->tx_key_fields[type];
89 
90 	if (offset + nr_bits <= 64) {
91 		/* one KW only */
92 		if (start_kwi > max_kwi)
93 			return;
94 		field->kw_mask[start_kwi] |= GENMASK_ULL(nr_bits - 1, 0)
95 					     << offset;
96 		field->nr_kws = 1;
97 	} else if (offset + nr_bits > 64 &&
98 		   offset + nr_bits <= 128) {
99 		/* two KWs */
100 		if (start_kwi + 1 > max_kwi)
101 			return;
102 		/* first KW mask */
103 		bits_in_kw = 64 - offset;
104 		field->kw_mask[start_kwi] |= GENMASK_ULL(bits_in_kw - 1, 0)
105 					     << offset;
106 		/* second KW mask i.e. mask for rest of bits */
107 		bits_in_kw = nr_bits + offset - 64;
108 		field->kw_mask[start_kwi + 1] |= GENMASK_ULL(bits_in_kw - 1, 0);
109 		field->nr_kws = 2;
110 	} else {
111 		/* three KWs */
112 		if (start_kwi + 2 > max_kwi)
113 			return;
114 		/* first KW mask */
115 		bits_in_kw = 64 - offset;
116 		field->kw_mask[start_kwi] |= GENMASK_ULL(bits_in_kw - 1, 0)
117 					     << offset;
118 		/* second KW mask */
119 		field->kw_mask[start_kwi + 1] = ~0ULL;
120 		/* third KW mask i.e. mask for rest of bits */
121 		bits_in_kw = nr_bits + offset - 128;
122 		field->kw_mask[start_kwi + 2] |= GENMASK_ULL(bits_in_kw - 1, 0);
123 		field->nr_kws = 3;
124 	}
125 }
126 
127 /* Helper function to figure out whether field exists in the key */
128 static bool npc_is_field_present(struct rvu *rvu, enum key_fields type, u8 intf)
129 {
130 	struct npc_mcam *mcam = &rvu->hw->mcam;
131 	struct npc_key_field *input;
132 
133 	input  = &mcam->rx_key_fields[type];
134 	if (is_npc_intf_tx(intf))
135 		input  = &mcam->tx_key_fields[type];
136 
137 	return input->nr_kws > 0;
138 }
139 
140 static bool npc_is_same(struct npc_key_field *input,
141 			struct npc_key_field *field)
142 {
143 	return memcmp(&input->layer_mdata, &field->layer_mdata,
144 		     sizeof(struct npc_layer_mdata)) == 0;
145 }
146 
147 static void npc_set_layer_mdata(struct npc_mcam *mcam, enum key_fields type,
148 				u64 cfg, u8 lid, u8 lt, u8 intf)
149 {
150 	struct npc_key_field *input = &mcam->rx_key_fields[type];
151 
152 	if (is_npc_intf_tx(intf))
153 		input = &mcam->tx_key_fields[type];
154 
155 	input->layer_mdata.hdr = FIELD_GET(NPC_HDR_OFFSET, cfg);
156 	input->layer_mdata.key = FIELD_GET(NPC_KEY_OFFSET, cfg);
157 	input->layer_mdata.len = FIELD_GET(NPC_BYTESM, cfg) + 1;
158 	input->layer_mdata.ltype = lt;
159 	input->layer_mdata.lid = lid;
160 }
161 
162 static bool npc_check_overlap_fields(struct npc_key_field *input1,
163 				     struct npc_key_field *input2)
164 {
165 	int kwi;
166 
167 	/* Fields with same layer id and different ltypes are mutually
168 	 * exclusive hence they can be overlapped
169 	 */
170 	if (input1->layer_mdata.lid == input2->layer_mdata.lid &&
171 	    input1->layer_mdata.ltype != input2->layer_mdata.ltype)
172 		return false;
173 
174 	for (kwi = 0; kwi < NPC_MAX_KWS_IN_KEY; kwi++) {
175 		if (input1->kw_mask[kwi] & input2->kw_mask[kwi])
176 			return true;
177 	}
178 
179 	return false;
180 }
181 
182 /* Helper function to check whether given field overlaps with any other fields
183  * in the key. Due to limitations on key size and the key extraction profile in
184  * use higher layers can overwrite lower layer's header fields. Hence overlap
185  * needs to be checked.
186  */
187 static bool npc_check_overlap(struct rvu *rvu, int blkaddr,
188 			      enum key_fields type, u8 start_lid, u8 intf)
189 {
190 	struct npc_mcam *mcam = &rvu->hw->mcam;
191 	struct npc_key_field *dummy, *input;
192 	int start_kwi, offset;
193 	u8 nr_bits, lid, lt, ld;
194 	u64 cfg;
195 
196 	dummy = &mcam->rx_key_fields[NPC_UNKNOWN];
197 	input = &mcam->rx_key_fields[type];
198 
199 	if (is_npc_intf_tx(intf)) {
200 		dummy = &mcam->tx_key_fields[NPC_UNKNOWN];
201 		input = &mcam->tx_key_fields[type];
202 	}
203 
204 	for (lid = start_lid; lid < NPC_MAX_LID; lid++) {
205 		for (lt = 0; lt < NPC_MAX_LT; lt++) {
206 			for (ld = 0; ld < NPC_MAX_LD; ld++) {
207 				cfg = rvu_read64(rvu, blkaddr,
208 						 NPC_AF_INTFX_LIDX_LTX_LDX_CFG
209 						 (intf, lid, lt, ld));
210 				if (!FIELD_GET(NPC_LDATA_EN, cfg))
211 					continue;
212 				memset(dummy, 0, sizeof(struct npc_key_field));
213 				npc_set_layer_mdata(mcam, NPC_UNKNOWN, cfg,
214 						    lid, lt, intf);
215 				/* exclude input */
216 				if (npc_is_same(input, dummy))
217 					continue;
218 				start_kwi = dummy->layer_mdata.key / 8;
219 				offset = (dummy->layer_mdata.key * 8) % 64;
220 				nr_bits = dummy->layer_mdata.len * 8;
221 				/* form KW masks */
222 				npc_set_kw_masks(mcam, NPC_UNKNOWN, nr_bits,
223 						 start_kwi, offset, intf);
224 				/* check any input field bits falls in any
225 				 * other field bits.
226 				 */
227 				if (npc_check_overlap_fields(dummy, input))
228 					return true;
229 			}
230 		}
231 	}
232 
233 	return false;
234 }
235 
236 static bool npc_check_field(struct rvu *rvu, int blkaddr, enum key_fields type,
237 			    u8 intf)
238 {
239 	if (!npc_is_field_present(rvu, type, intf) ||
240 	    npc_check_overlap(rvu, blkaddr, type, 0, intf))
241 		return false;
242 	return true;
243 }
244 
245 static void npc_scan_exact_result(struct npc_mcam *mcam, u8 bit_number,
246 				  u8 key_nibble, u8 intf)
247 {
248 	u8 offset = (key_nibble * 4) % 64; /* offset within key word */
249 	u8 kwi = (key_nibble * 4) / 64; /* which word in key */
250 	u8 nr_bits = 4; /* bits in a nibble */
251 	u8 type;
252 
253 	switch (bit_number) {
254 	case 40 ... 43:
255 		type = NPC_EXACT_RESULT;
256 		break;
257 
258 	default:
259 		return;
260 	}
261 	npc_set_kw_masks(mcam, type, nr_bits, kwi, offset, intf);
262 }
263 
264 static void npc_scan_parse_result(struct npc_mcam *mcam, u8 bit_number,
265 				  u8 key_nibble, u8 intf)
266 {
267 	u8 offset = (key_nibble * 4) % 64; /* offset within key word */
268 	u8 kwi = (key_nibble * 4) / 64; /* which word in key */
269 	u8 nr_bits = 4; /* bits in a nibble */
270 	u8 type;
271 
272 	switch (bit_number) {
273 	case 0 ... 2:
274 		type = NPC_CHAN;
275 		break;
276 	case 3:
277 		type = NPC_ERRLEV;
278 		break;
279 	case 4 ... 5:
280 		type = NPC_ERRCODE;
281 		break;
282 	case 6:
283 		type = NPC_LXMB;
284 		break;
285 	/* check for LTYPE only as of now */
286 	case 9:
287 		type = NPC_LA;
288 		break;
289 	case 12:
290 		type = NPC_LB;
291 		break;
292 	case 15:
293 		type = NPC_LC;
294 		break;
295 	case 18:
296 		type = NPC_LD;
297 		break;
298 	case 21:
299 		type = NPC_LE;
300 		break;
301 	case 24:
302 		type = NPC_LF;
303 		break;
304 	case 27:
305 		type = NPC_LG;
306 		break;
307 	case 30:
308 		type = NPC_LH;
309 		break;
310 	default:
311 		return;
312 	}
313 
314 	npc_set_kw_masks(mcam, type, nr_bits, kwi, offset, intf);
315 }
316 
317 static void npc_handle_multi_layer_fields(struct rvu *rvu, int blkaddr, u8 intf)
318 {
319 	struct npc_mcam *mcam = &rvu->hw->mcam;
320 	struct npc_key_field *key_fields;
321 	/* Ether type can come from three layers
322 	 * (ethernet, single tagged, double tagged)
323 	 */
324 	struct npc_key_field *etype_ether;
325 	struct npc_key_field *etype_tag1;
326 	struct npc_key_field *etype_tag2;
327 	/* Outer VLAN TCI can come from two layers
328 	 * (single tagged, double tagged)
329 	 */
330 	struct npc_key_field *vlan_tag1;
331 	struct npc_key_field *vlan_tag2;
332 	/* Inner VLAN TCI for double tagged frames */
333 	struct npc_key_field *vlan_tag3;
334 	u64 *features;
335 	u8 start_lid;
336 	int i;
337 
338 	key_fields = mcam->rx_key_fields;
339 	features = &mcam->rx_features;
340 
341 	if (is_npc_intf_tx(intf)) {
342 		key_fields = mcam->tx_key_fields;
343 		features = &mcam->tx_features;
344 	}
345 
346 	/* Handle header fields which can come from multiple layers like
347 	 * etype, outer vlan tci. These fields should have same position in
348 	 * the key otherwise to install a mcam rule more than one entry is
349 	 * needed which complicates mcam space management.
350 	 */
351 	etype_ether = &key_fields[NPC_ETYPE_ETHER];
352 	etype_tag1 = &key_fields[NPC_ETYPE_TAG1];
353 	etype_tag2 = &key_fields[NPC_ETYPE_TAG2];
354 	vlan_tag1 = &key_fields[NPC_VLAN_TAG1];
355 	vlan_tag2 = &key_fields[NPC_VLAN_TAG2];
356 	vlan_tag3 = &key_fields[NPC_VLAN_TAG3];
357 
358 	/* if key profile programmed does not extract Ethertype at all */
359 	if (!etype_ether->nr_kws && !etype_tag1->nr_kws && !etype_tag2->nr_kws) {
360 		dev_err(rvu->dev, "mkex: Ethertype is not extracted.\n");
361 		goto vlan_tci;
362 	}
363 
364 	/* if key profile programmed extracts Ethertype from one layer */
365 	if (etype_ether->nr_kws && !etype_tag1->nr_kws && !etype_tag2->nr_kws)
366 		key_fields[NPC_ETYPE] = *etype_ether;
367 	if (!etype_ether->nr_kws && etype_tag1->nr_kws && !etype_tag2->nr_kws)
368 		key_fields[NPC_ETYPE] = *etype_tag1;
369 	if (!etype_ether->nr_kws && !etype_tag1->nr_kws && etype_tag2->nr_kws)
370 		key_fields[NPC_ETYPE] = *etype_tag2;
371 
372 	/* if key profile programmed extracts Ethertype from multiple layers */
373 	if (etype_ether->nr_kws && etype_tag1->nr_kws) {
374 		for (i = 0; i < NPC_MAX_KWS_IN_KEY; i++) {
375 			if (etype_ether->kw_mask[i] != etype_tag1->kw_mask[i]) {
376 				dev_err(rvu->dev, "mkex: Etype pos is different for untagged and tagged pkts.\n");
377 				goto vlan_tci;
378 			}
379 		}
380 		key_fields[NPC_ETYPE] = *etype_tag1;
381 	}
382 	if (etype_ether->nr_kws && etype_tag2->nr_kws) {
383 		for (i = 0; i < NPC_MAX_KWS_IN_KEY; i++) {
384 			if (etype_ether->kw_mask[i] != etype_tag2->kw_mask[i]) {
385 				dev_err(rvu->dev, "mkex: Etype pos is different for untagged and double tagged pkts.\n");
386 				goto vlan_tci;
387 			}
388 		}
389 		key_fields[NPC_ETYPE] = *etype_tag2;
390 	}
391 	if (etype_tag1->nr_kws && etype_tag2->nr_kws) {
392 		for (i = 0; i < NPC_MAX_KWS_IN_KEY; i++) {
393 			if (etype_tag1->kw_mask[i] != etype_tag2->kw_mask[i]) {
394 				dev_err(rvu->dev, "mkex: Etype pos is different for tagged and double tagged pkts.\n");
395 				goto vlan_tci;
396 			}
397 		}
398 		key_fields[NPC_ETYPE] = *etype_tag2;
399 	}
400 
401 	/* check none of higher layers overwrite Ethertype */
402 	start_lid = key_fields[NPC_ETYPE].layer_mdata.lid + 1;
403 	if (npc_check_overlap(rvu, blkaddr, NPC_ETYPE, start_lid, intf)) {
404 		dev_err(rvu->dev, "mkex: Ethertype is overwritten by higher layers.\n");
405 		goto vlan_tci;
406 	}
407 	*features |= BIT_ULL(NPC_ETYPE);
408 vlan_tci:
409 	/* if key profile does not extract outer vlan tci at all */
410 	if (!vlan_tag1->nr_kws && !vlan_tag2->nr_kws) {
411 		dev_err(rvu->dev, "mkex: Outer vlan tci is not extracted.\n");
412 		goto done;
413 	}
414 
415 	/* if key profile extracts outer vlan tci from one layer */
416 	if (vlan_tag1->nr_kws && !vlan_tag2->nr_kws)
417 		key_fields[NPC_OUTER_VID] = *vlan_tag1;
418 	if (!vlan_tag1->nr_kws && vlan_tag2->nr_kws)
419 		key_fields[NPC_OUTER_VID] = *vlan_tag2;
420 
421 	/* if key profile extracts outer vlan tci from multiple layers */
422 	if (vlan_tag1->nr_kws && vlan_tag2->nr_kws) {
423 		for (i = 0; i < NPC_MAX_KWS_IN_KEY; i++) {
424 			if (vlan_tag1->kw_mask[i] != vlan_tag2->kw_mask[i]) {
425 				dev_err(rvu->dev, "mkex: Out vlan tci pos is different for tagged and double tagged pkts.\n");
426 				goto done;
427 			}
428 		}
429 		key_fields[NPC_OUTER_VID] = *vlan_tag2;
430 	}
431 	/* check none of higher layers overwrite outer vlan tci */
432 	start_lid = key_fields[NPC_OUTER_VID].layer_mdata.lid + 1;
433 	if (npc_check_overlap(rvu, blkaddr, NPC_OUTER_VID, start_lid, intf)) {
434 		dev_err(rvu->dev, "mkex: Outer vlan tci is overwritten by higher layers.\n");
435 		goto done;
436 	}
437 	*features |= BIT_ULL(NPC_OUTER_VID);
438 
439 	/* If key profile extracts inner vlan tci */
440 	if (vlan_tag3->nr_kws) {
441 		key_fields[NPC_INNER_VID] = *vlan_tag3;
442 		*features |= BIT_ULL(NPC_INNER_VID);
443 	}
444 done:
445 	return;
446 }
447 
448 static void npc_scan_ldata(struct rvu *rvu, int blkaddr, u8 lid,
449 			   u8 lt, u64 cfg, u8 intf)
450 {
451 	struct npc_mcam_kex_hash *mkex_hash = rvu->kpu.mkex_hash;
452 	struct npc_mcam *mcam = &rvu->hw->mcam;
453 	u8 hdr, key, nr_bytes, bit_offset;
454 	u8 la_ltype, la_start;
455 	/* starting KW index and starting bit position */
456 	int start_kwi, offset;
457 
458 	nr_bytes = FIELD_GET(NPC_BYTESM, cfg) + 1;
459 	hdr = FIELD_GET(NPC_HDR_OFFSET, cfg);
460 	key = FIELD_GET(NPC_KEY_OFFSET, cfg);
461 
462 	/* For Tx, Layer A has NIX_INST_HDR_S(64 bytes) preceding
463 	 * ethernet header.
464 	 */
465 	if (is_npc_intf_tx(intf)) {
466 		la_ltype = NPC_LT_LA_IH_NIX_ETHER;
467 		la_start = 8;
468 	} else {
469 		la_ltype = NPC_LT_LA_ETHER;
470 		la_start = 0;
471 	}
472 
473 #define NPC_SCAN_HDR(name, hlid, hlt, hstart, hlen)			       \
474 do {									       \
475 	start_kwi = key / 8;						       \
476 	offset = (key * 8) % 64;					       \
477 	if (lid == (hlid) && lt == (hlt)) {				       \
478 		if ((hstart) >= hdr &&					       \
479 		    ((hstart) + (hlen)) <= (hdr + nr_bytes)) {	               \
480 			bit_offset = (hdr + nr_bytes - (hstart) - (hlen)) * 8; \
481 			npc_set_layer_mdata(mcam, (name), cfg, lid, lt, intf); \
482 			offset += bit_offset;				       \
483 			start_kwi += offset / 64;			       \
484 			offset %= 64;					       \
485 			npc_set_kw_masks(mcam, (name), (hlen) * 8,	       \
486 					 start_kwi, offset, intf);	       \
487 		}							       \
488 	}								       \
489 } while (0)
490 
491 	/* List LID, LTYPE, start offset from layer and length(in bytes) of
492 	 * packet header fields below.
493 	 * Example: Source IP is 4 bytes and starts at 12th byte of IP header
494 	 */
495 	NPC_SCAN_HDR(NPC_TOS, NPC_LID_LC, NPC_LT_LC_IP, 1, 1);
496 	NPC_SCAN_HDR(NPC_IPFRAG_IPV4, NPC_LID_LC, NPC_LT_LC_IP, 6, 1);
497 	NPC_SCAN_HDR(NPC_SIP_IPV4, NPC_LID_LC, NPC_LT_LC_IP, 12, 4);
498 	NPC_SCAN_HDR(NPC_DIP_IPV4, NPC_LID_LC, NPC_LT_LC_IP, 16, 4);
499 	NPC_SCAN_HDR(NPC_IPFRAG_IPV6, NPC_LID_LC, NPC_LT_LC_IP6_EXT, 6, 1);
500 	if (rvu->hw->cap.npc_hash_extract) {
501 		if (mkex_hash->lid_lt_ld_hash_en[intf][lid][lt][0])
502 			NPC_SCAN_HDR(NPC_SIP_IPV6, NPC_LID_LC, NPC_LT_LC_IP6, 8, 4);
503 		else
504 			NPC_SCAN_HDR(NPC_SIP_IPV6, NPC_LID_LC, NPC_LT_LC_IP6, 8, 16);
505 
506 		if (mkex_hash->lid_lt_ld_hash_en[intf][lid][lt][1])
507 			NPC_SCAN_HDR(NPC_DIP_IPV6, NPC_LID_LC, NPC_LT_LC_IP6, 24, 4);
508 		else
509 			NPC_SCAN_HDR(NPC_DIP_IPV6, NPC_LID_LC, NPC_LT_LC_IP6, 24, 16);
510 	} else {
511 		NPC_SCAN_HDR(NPC_SIP_IPV6, NPC_LID_LC, NPC_LT_LC_IP6, 8, 16);
512 		NPC_SCAN_HDR(NPC_DIP_IPV6, NPC_LID_LC, NPC_LT_LC_IP6, 24, 16);
513 	}
514 
515 	NPC_SCAN_HDR(NPC_SPORT_UDP, NPC_LID_LD, NPC_LT_LD_UDP, 0, 2);
516 	NPC_SCAN_HDR(NPC_DPORT_UDP, NPC_LID_LD, NPC_LT_LD_UDP, 2, 2);
517 	NPC_SCAN_HDR(NPC_SPORT_TCP, NPC_LID_LD, NPC_LT_LD_TCP, 0, 2);
518 	NPC_SCAN_HDR(NPC_DPORT_TCP, NPC_LID_LD, NPC_LT_LD_TCP, 2, 2);
519 	NPC_SCAN_HDR(NPC_SPORT_SCTP, NPC_LID_LD, NPC_LT_LD_SCTP, 0, 2);
520 	NPC_SCAN_HDR(NPC_DPORT_SCTP, NPC_LID_LD, NPC_LT_LD_SCTP, 2, 2);
521 	NPC_SCAN_HDR(NPC_ETYPE_ETHER, NPC_LID_LA, NPC_LT_LA_ETHER, 12, 2);
522 	NPC_SCAN_HDR(NPC_ETYPE_TAG1, NPC_LID_LB, NPC_LT_LB_CTAG, 4, 2);
523 	NPC_SCAN_HDR(NPC_ETYPE_TAG2, NPC_LID_LB, NPC_LT_LB_STAG_QINQ, 8, 2);
524 	NPC_SCAN_HDR(NPC_VLAN_TAG1, NPC_LID_LB, NPC_LT_LB_CTAG, 2, 2);
525 	NPC_SCAN_HDR(NPC_VLAN_TAG2, NPC_LID_LB, NPC_LT_LB_STAG_QINQ, 2, 2);
526 	NPC_SCAN_HDR(NPC_VLAN_TAG3, NPC_LID_LB, NPC_LT_LB_STAG_QINQ, 6, 2);
527 	NPC_SCAN_HDR(NPC_DMAC, NPC_LID_LA, la_ltype, la_start, 6);
528 
529 	NPC_SCAN_HDR(NPC_IPSEC_SPI, NPC_LID_LD, NPC_LT_LD_AH, 4, 4);
530 	NPC_SCAN_HDR(NPC_IPSEC_SPI, NPC_LID_LE, NPC_LT_LE_ESP, 0, 4);
531 
532 	/* SMAC follows the DMAC(which is 6 bytes) */
533 	NPC_SCAN_HDR(NPC_SMAC, NPC_LID_LA, la_ltype, la_start + 6, 6);
534 	/* PF_FUNC is 2 bytes at 0th byte of NPC_LT_LA_IH_NIX_ETHER */
535 	NPC_SCAN_HDR(NPC_PF_FUNC, NPC_LID_LA, NPC_LT_LA_IH_NIX_ETHER, 0, 2);
536 }
537 
538 static void npc_set_features(struct rvu *rvu, int blkaddr, u8 intf)
539 {
540 	struct npc_mcam *mcam = &rvu->hw->mcam;
541 	u64 *features = &mcam->rx_features;
542 	u64 tcp_udp_sctp;
543 	int hdr;
544 
545 	if (is_npc_intf_tx(intf))
546 		features = &mcam->tx_features;
547 
548 	for (hdr = NPC_DMAC; hdr < NPC_HEADER_FIELDS_MAX; hdr++) {
549 		if (npc_check_field(rvu, blkaddr, hdr, intf))
550 			*features |= BIT_ULL(hdr);
551 	}
552 
553 	tcp_udp_sctp = BIT_ULL(NPC_SPORT_TCP) | BIT_ULL(NPC_SPORT_UDP) |
554 		       BIT_ULL(NPC_DPORT_TCP) | BIT_ULL(NPC_DPORT_UDP) |
555 		       BIT_ULL(NPC_SPORT_SCTP) | BIT_ULL(NPC_DPORT_SCTP);
556 
557 	/* for tcp/udp/sctp corresponding layer type should be in the key */
558 	if (*features & tcp_udp_sctp) {
559 		if (!npc_check_field(rvu, blkaddr, NPC_LD, intf))
560 			*features &= ~tcp_udp_sctp;
561 		else
562 			*features |= BIT_ULL(NPC_IPPROTO_TCP) |
563 				     BIT_ULL(NPC_IPPROTO_UDP) |
564 				     BIT_ULL(NPC_IPPROTO_SCTP);
565 	}
566 
567 	/* for AH/ICMP/ICMPv6/, check if corresponding layer type is present in the key */
568 	if (npc_check_field(rvu, blkaddr, NPC_LD, intf)) {
569 		*features |= BIT_ULL(NPC_IPPROTO_AH);
570 		*features |= BIT_ULL(NPC_IPPROTO_ICMP);
571 		*features |= BIT_ULL(NPC_IPPROTO_ICMP6);
572 	}
573 
574 	/* for ESP, check if corresponding layer type is present in the key */
575 	if (npc_check_field(rvu, blkaddr, NPC_LE, intf))
576 		*features |= BIT_ULL(NPC_IPPROTO_ESP);
577 
578 	/* for vlan corresponding layer type should be in the key */
579 	if (*features & BIT_ULL(NPC_OUTER_VID))
580 		if (!npc_check_field(rvu, blkaddr, NPC_LB, intf))
581 			*features &= ~BIT_ULL(NPC_OUTER_VID);
582 
583 	if (*features & (BIT_ULL(NPC_IPPROTO_AH) | BIT_ULL(NPC_IPPROTO_ESP)))
584 		*features |= BIT_ULL(NPC_IPSEC_SPI);
585 
586 	/* for vlan ethertypes corresponding layer type should be in the key */
587 	if (npc_check_field(rvu, blkaddr, NPC_LB, intf))
588 		*features |= BIT_ULL(NPC_VLAN_ETYPE_CTAG) |
589 			     BIT_ULL(NPC_VLAN_ETYPE_STAG);
590 
591 	/* for L2M/L2B/L3M/L3B, check if the type is present in the key */
592 	if (npc_check_field(rvu, blkaddr, NPC_LXMB, intf))
593 		*features |= BIT_ULL(NPC_LXMB);
594 }
595 
596 /* Scan key extraction profile and record how fields of our interest
597  * fill the key structure. Also verify Channel and DMAC exists in
598  * key and not overwritten by other header fields.
599  */
600 static int npc_scan_kex(struct rvu *rvu, int blkaddr, u8 intf)
601 {
602 	struct npc_mcam *mcam = &rvu->hw->mcam;
603 	u8 lid, lt, ld, bitnr;
604 	u64 cfg, masked_cfg;
605 	u8 key_nibble = 0;
606 
607 	/* Scan and note how parse result is going to be in key.
608 	 * A bit set in PARSE_NIBBLE_ENA corresponds to a nibble from
609 	 * parse result in the key. The enabled nibbles from parse result
610 	 * will be concatenated in key.
611 	 */
612 	cfg = rvu_read64(rvu, blkaddr, NPC_AF_INTFX_KEX_CFG(intf));
613 	masked_cfg = cfg & NPC_PARSE_NIBBLE;
614 	for_each_set_bit(bitnr, (unsigned long *)&masked_cfg, 31) {
615 		npc_scan_parse_result(mcam, bitnr, key_nibble, intf);
616 		key_nibble++;
617 	}
618 
619 	/* Ignore exact match bits for mcam entries except the first rule
620 	 * which is drop on hit. This first rule is configured explitcitly by
621 	 * exact match code.
622 	 */
623 	masked_cfg = cfg & NPC_EXACT_NIBBLE;
624 	bitnr = NPC_EXACT_NIBBLE_START;
625 	for_each_set_bit_from(bitnr, (unsigned long *)&masked_cfg, NPC_EXACT_NIBBLE_END + 1) {
626 		npc_scan_exact_result(mcam, bitnr, key_nibble, intf);
627 		key_nibble++;
628 	}
629 
630 	/* Scan and note how layer data is going to be in key */
631 	for (lid = 0; lid < NPC_MAX_LID; lid++) {
632 		for (lt = 0; lt < NPC_MAX_LT; lt++) {
633 			for (ld = 0; ld < NPC_MAX_LD; ld++) {
634 				cfg = rvu_read64(rvu, blkaddr,
635 						 NPC_AF_INTFX_LIDX_LTX_LDX_CFG
636 						 (intf, lid, lt, ld));
637 				if (!FIELD_GET(NPC_LDATA_EN, cfg))
638 					continue;
639 				npc_scan_ldata(rvu, blkaddr, lid, lt, cfg,
640 					       intf);
641 			}
642 		}
643 	}
644 
645 	return 0;
646 }
647 
648 static int npc_scan_verify_kex(struct rvu *rvu, int blkaddr)
649 {
650 	int err;
651 
652 	err = npc_scan_kex(rvu, blkaddr, NIX_INTF_RX);
653 	if (err)
654 		return err;
655 
656 	err = npc_scan_kex(rvu, blkaddr, NIX_INTF_TX);
657 	if (err)
658 		return err;
659 
660 	/* Channel is mandatory */
661 	if (!npc_is_field_present(rvu, NPC_CHAN, NIX_INTF_RX)) {
662 		dev_err(rvu->dev, "Channel not present in Key\n");
663 		return -EINVAL;
664 	}
665 	/* check that none of the fields overwrite channel */
666 	if (npc_check_overlap(rvu, blkaddr, NPC_CHAN, 0, NIX_INTF_RX)) {
667 		dev_err(rvu->dev, "Channel cannot be overwritten\n");
668 		return -EINVAL;
669 	}
670 
671 	npc_set_features(rvu, blkaddr, NIX_INTF_TX);
672 	npc_set_features(rvu, blkaddr, NIX_INTF_RX);
673 	npc_handle_multi_layer_fields(rvu, blkaddr, NIX_INTF_TX);
674 	npc_handle_multi_layer_fields(rvu, blkaddr, NIX_INTF_RX);
675 
676 	return 0;
677 }
678 
679 int npc_flow_steering_init(struct rvu *rvu, int blkaddr)
680 {
681 	struct npc_mcam *mcam = &rvu->hw->mcam;
682 
683 	INIT_LIST_HEAD(&mcam->mcam_rules);
684 
685 	return npc_scan_verify_kex(rvu, blkaddr);
686 }
687 
688 static int npc_check_unsupported_flows(struct rvu *rvu, u64 features, u8 intf)
689 {
690 	struct npc_mcam *mcam = &rvu->hw->mcam;
691 	u64 *mcam_features = &mcam->rx_features;
692 	u64 unsupported;
693 	u8 bit;
694 
695 	if (is_npc_intf_tx(intf))
696 		mcam_features = &mcam->tx_features;
697 
698 	unsupported = (*mcam_features ^ features) & ~(*mcam_features);
699 	if (unsupported) {
700 		dev_warn(rvu->dev, "Unsupported flow(s):\n");
701 		for_each_set_bit(bit, (unsigned long *)&unsupported, 64)
702 			dev_warn(rvu->dev, "%s ", npc_get_field_name(bit));
703 		return -EOPNOTSUPP;
704 	}
705 
706 	return 0;
707 }
708 
709 /* npc_update_entry - Based on the masks generated during
710  * the key scanning, updates the given entry with value and
711  * masks for the field of interest. Maximum 16 bytes of a packet
712  * header can be extracted by HW hence lo and hi are sufficient.
713  * When field bytes are less than or equal to 8 then hi should be
714  * 0 for value and mask.
715  *
716  * If exact match of value is required then mask should be all 1's.
717  * If any bits in mask are 0 then corresponding bits in value are
718  * dont care.
719  */
720 void npc_update_entry(struct rvu *rvu, enum key_fields type,
721 		      struct mcam_entry *entry, u64 val_lo,
722 		      u64 val_hi, u64 mask_lo, u64 mask_hi, u8 intf)
723 {
724 	struct npc_mcam *mcam = &rvu->hw->mcam;
725 	struct mcam_entry dummy = { {0} };
726 	struct npc_key_field *field;
727 	u64 kw1, kw2, kw3;
728 	u8 shift;
729 	int i;
730 
731 	field = &mcam->rx_key_fields[type];
732 	if (is_npc_intf_tx(intf))
733 		field = &mcam->tx_key_fields[type];
734 
735 	if (!field->nr_kws)
736 		return;
737 
738 	for (i = 0; i < NPC_MAX_KWS_IN_KEY; i++) {
739 		if (!field->kw_mask[i])
740 			continue;
741 		/* place key value in kw[x] */
742 		shift = __ffs64(field->kw_mask[i]);
743 		/* update entry value */
744 		kw1 = (val_lo << shift) & field->kw_mask[i];
745 		dummy.kw[i] = kw1;
746 		/* update entry mask */
747 		kw1 = (mask_lo << shift) & field->kw_mask[i];
748 		dummy.kw_mask[i] = kw1;
749 
750 		if (field->nr_kws == 1)
751 			break;
752 		/* place remaining bits of key value in kw[x + 1] */
753 		if (field->nr_kws == 2) {
754 			/* update entry value */
755 			kw2 = shift ? val_lo >> (64 - shift) : 0;
756 			kw2 |= (val_hi << shift);
757 			kw2 &= field->kw_mask[i + 1];
758 			dummy.kw[i + 1] = kw2;
759 			/* update entry mask */
760 			kw2 = shift ? mask_lo >> (64 - shift) : 0;
761 			kw2 |= (mask_hi << shift);
762 			kw2 &= field->kw_mask[i + 1];
763 			dummy.kw_mask[i + 1] = kw2;
764 			break;
765 		}
766 		/* place remaining bits of key value in kw[x + 1], kw[x + 2] */
767 		if (field->nr_kws == 3) {
768 			/* update entry value */
769 			kw2 = shift ? val_lo >> (64 - shift) : 0;
770 			kw2 |= (val_hi << shift);
771 			kw2 &= field->kw_mask[i + 1];
772 			kw3 = shift ? val_hi >> (64 - shift) : 0;
773 			kw3 &= field->kw_mask[i + 2];
774 			dummy.kw[i + 1] = kw2;
775 			dummy.kw[i + 2] = kw3;
776 			/* update entry mask */
777 			kw2 = shift ? mask_lo >> (64 - shift) : 0;
778 			kw2 |= (mask_hi << shift);
779 			kw2 &= field->kw_mask[i + 1];
780 			kw3 = shift ? mask_hi >> (64 - shift) : 0;
781 			kw3 &= field->kw_mask[i + 2];
782 			dummy.kw_mask[i + 1] = kw2;
783 			dummy.kw_mask[i + 2] = kw3;
784 			break;
785 		}
786 	}
787 	/* dummy is ready with values and masks for given key
788 	 * field now clear and update input entry with those
789 	 */
790 	for (i = 0; i < NPC_MAX_KWS_IN_KEY; i++) {
791 		if (!field->kw_mask[i])
792 			continue;
793 		entry->kw[i] &= ~field->kw_mask[i];
794 		entry->kw_mask[i] &= ~field->kw_mask[i];
795 
796 		entry->kw[i] |= dummy.kw[i];
797 		entry->kw_mask[i] |= dummy.kw_mask[i];
798 	}
799 }
800 
801 static void npc_update_ipv6_flow(struct rvu *rvu, struct mcam_entry *entry,
802 				 u64 features, struct flow_msg *pkt,
803 				 struct flow_msg *mask,
804 				 struct rvu_npc_mcam_rule *output, u8 intf)
805 {
806 	u32 src_ip[IPV6_WORDS], src_ip_mask[IPV6_WORDS];
807 	u32 dst_ip[IPV6_WORDS], dst_ip_mask[IPV6_WORDS];
808 	struct flow_msg *opkt = &output->packet;
809 	struct flow_msg *omask = &output->mask;
810 	u64 mask_lo, mask_hi;
811 	u64 val_lo, val_hi;
812 
813 	/* For an ipv6 address fe80::2c68:63ff:fe5e:2d0a the packet
814 	 * values to be programmed in MCAM should as below:
815 	 * val_high: 0xfe80000000000000
816 	 * val_low: 0x2c6863fffe5e2d0a
817 	 */
818 	if (features & BIT_ULL(NPC_SIP_IPV6)) {
819 		be32_to_cpu_array(src_ip_mask, mask->ip6src, IPV6_WORDS);
820 		be32_to_cpu_array(src_ip, pkt->ip6src, IPV6_WORDS);
821 
822 		mask_hi = (u64)src_ip_mask[0] << 32 | src_ip_mask[1];
823 		mask_lo = (u64)src_ip_mask[2] << 32 | src_ip_mask[3];
824 		val_hi = (u64)src_ip[0] << 32 | src_ip[1];
825 		val_lo = (u64)src_ip[2] << 32 | src_ip[3];
826 
827 		npc_update_entry(rvu, NPC_SIP_IPV6, entry, val_lo, val_hi,
828 				 mask_lo, mask_hi, intf);
829 		memcpy(opkt->ip6src, pkt->ip6src, sizeof(opkt->ip6src));
830 		memcpy(omask->ip6src, mask->ip6src, sizeof(omask->ip6src));
831 	}
832 	if (features & BIT_ULL(NPC_DIP_IPV6)) {
833 		be32_to_cpu_array(dst_ip_mask, mask->ip6dst, IPV6_WORDS);
834 		be32_to_cpu_array(dst_ip, pkt->ip6dst, IPV6_WORDS);
835 
836 		mask_hi = (u64)dst_ip_mask[0] << 32 | dst_ip_mask[1];
837 		mask_lo = (u64)dst_ip_mask[2] << 32 | dst_ip_mask[3];
838 		val_hi = (u64)dst_ip[0] << 32 | dst_ip[1];
839 		val_lo = (u64)dst_ip[2] << 32 | dst_ip[3];
840 
841 		npc_update_entry(rvu, NPC_DIP_IPV6, entry, val_lo, val_hi,
842 				 mask_lo, mask_hi, intf);
843 		memcpy(opkt->ip6dst, pkt->ip6dst, sizeof(opkt->ip6dst));
844 		memcpy(omask->ip6dst, mask->ip6dst, sizeof(omask->ip6dst));
845 	}
846 }
847 
848 static void npc_update_vlan_features(struct rvu *rvu, struct mcam_entry *entry,
849 				     u64 features, u8 intf)
850 {
851 	bool ctag = !!(features & BIT_ULL(NPC_VLAN_ETYPE_CTAG));
852 	bool stag = !!(features & BIT_ULL(NPC_VLAN_ETYPE_STAG));
853 	bool vid = !!(features & BIT_ULL(NPC_OUTER_VID));
854 
855 	/* If only VLAN id is given then always match outer VLAN id */
856 	if (vid && !ctag && !stag) {
857 		npc_update_entry(rvu, NPC_LB, entry,
858 				 NPC_LT_LB_STAG_QINQ | NPC_LT_LB_CTAG, 0,
859 				 NPC_LT_LB_STAG_QINQ & NPC_LT_LB_CTAG, 0, intf);
860 		return;
861 	}
862 	if (ctag)
863 		npc_update_entry(rvu, NPC_LB, entry, NPC_LT_LB_CTAG, 0,
864 				 ~0ULL, 0, intf);
865 	if (stag)
866 		npc_update_entry(rvu, NPC_LB, entry, NPC_LT_LB_STAG_QINQ, 0,
867 				 ~0ULL, 0, intf);
868 }
869 
870 static void npc_update_flow(struct rvu *rvu, struct mcam_entry *entry,
871 			    u64 features, struct flow_msg *pkt,
872 			    struct flow_msg *mask,
873 			    struct rvu_npc_mcam_rule *output, u8 intf,
874 			    int blkaddr)
875 {
876 	u64 dmac_mask = ether_addr_to_u64(mask->dmac);
877 	u64 smac_mask = ether_addr_to_u64(mask->smac);
878 	u64 dmac_val = ether_addr_to_u64(pkt->dmac);
879 	u64 smac_val = ether_addr_to_u64(pkt->smac);
880 	struct flow_msg *opkt = &output->packet;
881 	struct flow_msg *omask = &output->mask;
882 
883 	if (!features)
884 		return;
885 
886 	/* For tcp/udp/sctp LTYPE should be present in entry */
887 	if (features & BIT_ULL(NPC_IPPROTO_TCP))
888 		npc_update_entry(rvu, NPC_LD, entry, NPC_LT_LD_TCP,
889 				 0, ~0ULL, 0, intf);
890 	if (features & BIT_ULL(NPC_IPPROTO_UDP))
891 		npc_update_entry(rvu, NPC_LD, entry, NPC_LT_LD_UDP,
892 				 0, ~0ULL, 0, intf);
893 	if (features & BIT_ULL(NPC_IPPROTO_SCTP))
894 		npc_update_entry(rvu, NPC_LD, entry, NPC_LT_LD_SCTP,
895 				 0, ~0ULL, 0, intf);
896 	if (features & BIT_ULL(NPC_IPPROTO_ICMP))
897 		npc_update_entry(rvu, NPC_LD, entry, NPC_LT_LD_ICMP,
898 				 0, ~0ULL, 0, intf);
899 	if (features & BIT_ULL(NPC_IPPROTO_ICMP6))
900 		npc_update_entry(rvu, NPC_LD, entry, NPC_LT_LD_ICMP6,
901 				 0, ~0ULL, 0, intf);
902 
903 	/* For AH, LTYPE should be present in entry */
904 	if (features & BIT_ULL(NPC_IPPROTO_AH))
905 		npc_update_entry(rvu, NPC_LD, entry, NPC_LT_LD_AH,
906 				 0, ~0ULL, 0, intf);
907 	/* For ESP, LTYPE should be present in entry */
908 	if (features & BIT_ULL(NPC_IPPROTO_ESP))
909 		npc_update_entry(rvu, NPC_LE, entry, NPC_LT_LE_ESP,
910 				 0, ~0ULL, 0, intf);
911 
912 	if (features & BIT_ULL(NPC_LXMB)) {
913 		output->lxmb = is_broadcast_ether_addr(pkt->dmac) ? 2 : 1;
914 		npc_update_entry(rvu, NPC_LXMB, entry, output->lxmb, 0,
915 				 output->lxmb, 0, intf);
916 	}
917 #define NPC_WRITE_FLOW(field, member, val_lo, val_hi, mask_lo, mask_hi)	      \
918 do {									      \
919 	if (features & BIT_ULL((field))) {				      \
920 		npc_update_entry(rvu, (field), entry, (val_lo), (val_hi),     \
921 				 (mask_lo), (mask_hi), intf);		      \
922 		memcpy(&opkt->member, &pkt->member, sizeof(pkt->member));     \
923 		memcpy(&omask->member, &mask->member, sizeof(mask->member));  \
924 	}								      \
925 } while (0)
926 
927 	NPC_WRITE_FLOW(NPC_DMAC, dmac, dmac_val, 0, dmac_mask, 0);
928 
929 	NPC_WRITE_FLOW(NPC_SMAC, smac, smac_val, 0, smac_mask, 0);
930 	NPC_WRITE_FLOW(NPC_ETYPE, etype, ntohs(pkt->etype), 0,
931 		       ntohs(mask->etype), 0);
932 	NPC_WRITE_FLOW(NPC_TOS, tos, pkt->tos, 0, mask->tos, 0);
933 	NPC_WRITE_FLOW(NPC_IPFRAG_IPV4, ip_flag, pkt->ip_flag, 0,
934 		       mask->ip_flag, 0);
935 	NPC_WRITE_FLOW(NPC_SIP_IPV4, ip4src, ntohl(pkt->ip4src), 0,
936 		       ntohl(mask->ip4src), 0);
937 	NPC_WRITE_FLOW(NPC_DIP_IPV4, ip4dst, ntohl(pkt->ip4dst), 0,
938 		       ntohl(mask->ip4dst), 0);
939 	NPC_WRITE_FLOW(NPC_SPORT_TCP, sport, ntohs(pkt->sport), 0,
940 		       ntohs(mask->sport), 0);
941 	NPC_WRITE_FLOW(NPC_SPORT_UDP, sport, ntohs(pkt->sport), 0,
942 		       ntohs(mask->sport), 0);
943 	NPC_WRITE_FLOW(NPC_DPORT_TCP, dport, ntohs(pkt->dport), 0,
944 		       ntohs(mask->dport), 0);
945 	NPC_WRITE_FLOW(NPC_DPORT_UDP, dport, ntohs(pkt->dport), 0,
946 		       ntohs(mask->dport), 0);
947 	NPC_WRITE_FLOW(NPC_SPORT_SCTP, sport, ntohs(pkt->sport), 0,
948 		       ntohs(mask->sport), 0);
949 	NPC_WRITE_FLOW(NPC_DPORT_SCTP, dport, ntohs(pkt->dport), 0,
950 		       ntohs(mask->dport), 0);
951 
952 	NPC_WRITE_FLOW(NPC_IPSEC_SPI, spi, ntohl(pkt->spi), 0,
953 		       ntohl(mask->spi), 0);
954 
955 	NPC_WRITE_FLOW(NPC_OUTER_VID, vlan_tci, ntohs(pkt->vlan_tci), 0,
956 		       ntohs(mask->vlan_tci), 0);
957 	NPC_WRITE_FLOW(NPC_INNER_VID, vlan_itci, ntohs(pkt->vlan_itci), 0,
958 		       ntohs(mask->vlan_itci), 0);
959 
960 	NPC_WRITE_FLOW(NPC_IPFRAG_IPV6, next_header, pkt->next_header, 0,
961 		       mask->next_header, 0);
962 	npc_update_ipv6_flow(rvu, entry, features, pkt, mask, output, intf);
963 	npc_update_vlan_features(rvu, entry, features, intf);
964 
965 	npc_update_field_hash(rvu, intf, entry, blkaddr, features,
966 			      pkt, mask, opkt, omask);
967 }
968 
969 static struct rvu_npc_mcam_rule *rvu_mcam_find_rule(struct npc_mcam *mcam, u16 entry)
970 {
971 	struct rvu_npc_mcam_rule *iter;
972 
973 	mutex_lock(&mcam->lock);
974 	list_for_each_entry(iter, &mcam->mcam_rules, list) {
975 		if (iter->entry == entry) {
976 			mutex_unlock(&mcam->lock);
977 			return iter;
978 		}
979 	}
980 	mutex_unlock(&mcam->lock);
981 
982 	return NULL;
983 }
984 
985 static void rvu_mcam_add_rule(struct npc_mcam *mcam,
986 			      struct rvu_npc_mcam_rule *rule)
987 {
988 	struct list_head *head = &mcam->mcam_rules;
989 	struct rvu_npc_mcam_rule *iter;
990 
991 	mutex_lock(&mcam->lock);
992 	list_for_each_entry(iter, &mcam->mcam_rules, list) {
993 		if (iter->entry > rule->entry)
994 			break;
995 		head = &iter->list;
996 	}
997 
998 	list_add(&rule->list, head);
999 	mutex_unlock(&mcam->lock);
1000 }
1001 
1002 static void rvu_mcam_remove_counter_from_rule(struct rvu *rvu, u16 pcifunc,
1003 					      struct rvu_npc_mcam_rule *rule)
1004 {
1005 	struct npc_mcam_oper_counter_req free_req = { 0 };
1006 	struct msg_rsp free_rsp;
1007 
1008 	if (!rule->has_cntr)
1009 		return;
1010 
1011 	free_req.hdr.pcifunc = pcifunc;
1012 	free_req.cntr = rule->cntr;
1013 
1014 	rvu_mbox_handler_npc_mcam_free_counter(rvu, &free_req, &free_rsp);
1015 	rule->has_cntr = false;
1016 }
1017 
1018 static void rvu_mcam_add_counter_to_rule(struct rvu *rvu, u16 pcifunc,
1019 					 struct rvu_npc_mcam_rule *rule,
1020 					 struct npc_install_flow_rsp *rsp)
1021 {
1022 	struct npc_mcam_alloc_counter_req cntr_req = { 0 };
1023 	struct npc_mcam_alloc_counter_rsp cntr_rsp = { 0 };
1024 	int err;
1025 
1026 	cntr_req.hdr.pcifunc = pcifunc;
1027 	cntr_req.contig = true;
1028 	cntr_req.count = 1;
1029 
1030 	/* we try to allocate a counter to track the stats of this
1031 	 * rule. If counter could not be allocated then proceed
1032 	 * without counter because counters are limited than entries.
1033 	 */
1034 	err = rvu_mbox_handler_npc_mcam_alloc_counter(rvu, &cntr_req,
1035 						      &cntr_rsp);
1036 	if (!err && cntr_rsp.count) {
1037 		rule->cntr = cntr_rsp.cntr;
1038 		rule->has_cntr = true;
1039 		rsp->counter = rule->cntr;
1040 	} else {
1041 		rsp->counter = err;
1042 	}
1043 }
1044 
1045 static void npc_update_rx_entry(struct rvu *rvu, struct rvu_pfvf *pfvf,
1046 				struct mcam_entry *entry,
1047 				struct npc_install_flow_req *req,
1048 				u16 target, bool pf_set_vfs_mac)
1049 {
1050 	struct rvu_switch *rswitch = &rvu->rswitch;
1051 	struct nix_rx_action action;
1052 
1053 	if (rswitch->mode == DEVLINK_ESWITCH_MODE_SWITCHDEV && pf_set_vfs_mac)
1054 		req->chan_mask = 0x0; /* Do not care channel */
1055 
1056 	npc_update_entry(rvu, NPC_CHAN, entry, req->channel, 0, req->chan_mask,
1057 			 0, NIX_INTF_RX);
1058 
1059 	*(u64 *)&action = 0x00;
1060 	action.pf_func = target;
1061 	action.op = req->op;
1062 	action.index = req->index;
1063 	action.match_id = req->match_id;
1064 	action.flow_key_alg = req->flow_key_alg;
1065 
1066 	if (req->op == NIX_RX_ACTION_DEFAULT) {
1067 		if (pfvf->def_ucast_rule) {
1068 			action = pfvf->def_ucast_rule->rx_action;
1069 		} else {
1070 			/* For profiles which do not extract DMAC, the default
1071 			 * unicast entry is unused. Hence modify action for the
1072 			 * requests which use same action as default unicast
1073 			 * entry
1074 			 */
1075 			*(u64 *)&action = 0;
1076 			action.pf_func = target;
1077 			action.op = NIX_RX_ACTIONOP_UCAST;
1078 		}
1079 	}
1080 
1081 	entry->action = *(u64 *)&action;
1082 
1083 	/* VTAG0 starts at 0th byte of LID_B.
1084 	 * VTAG1 starts at 4th byte of LID_B.
1085 	 */
1086 	entry->vtag_action = FIELD_PREP(RX_VTAG0_VALID_BIT, req->vtag0_valid) |
1087 			     FIELD_PREP(RX_VTAG0_TYPE_MASK, req->vtag0_type) |
1088 			     FIELD_PREP(RX_VTAG0_LID_MASK, NPC_LID_LB) |
1089 			     FIELD_PREP(RX_VTAG0_RELPTR_MASK, 0) |
1090 			     FIELD_PREP(RX_VTAG1_VALID_BIT, req->vtag1_valid) |
1091 			     FIELD_PREP(RX_VTAG1_TYPE_MASK, req->vtag1_type) |
1092 			     FIELD_PREP(RX_VTAG1_LID_MASK, NPC_LID_LB) |
1093 			     FIELD_PREP(RX_VTAG1_RELPTR_MASK, 4);
1094 }
1095 
1096 static void npc_update_tx_entry(struct rvu *rvu, struct rvu_pfvf *pfvf,
1097 				struct mcam_entry *entry,
1098 				struct npc_install_flow_req *req, u16 target)
1099 {
1100 	struct nix_tx_action action;
1101 	u64 mask = ~0ULL;
1102 
1103 	/* If AF is installing then do not care about
1104 	 * PF_FUNC in Send Descriptor
1105 	 */
1106 	if (is_pffunc_af(req->hdr.pcifunc))
1107 		mask = 0;
1108 
1109 	npc_update_entry(rvu, NPC_PF_FUNC, entry, (__force u16)htons(target),
1110 			 0, mask, 0, NIX_INTF_TX);
1111 
1112 	*(u64 *)&action = 0x00;
1113 	action.op = req->op;
1114 	action.index = req->index;
1115 	action.match_id = req->match_id;
1116 
1117 	entry->action = *(u64 *)&action;
1118 
1119 	/* VTAG0 starts at 0th byte of LID_B.
1120 	 * VTAG1 starts at 4th byte of LID_B.
1121 	 */
1122 	entry->vtag_action = FIELD_PREP(TX_VTAG0_DEF_MASK, req->vtag0_def) |
1123 			     FIELD_PREP(TX_VTAG0_OP_MASK, req->vtag0_op) |
1124 			     FIELD_PREP(TX_VTAG0_LID_MASK, NPC_LID_LA) |
1125 			     FIELD_PREP(TX_VTAG0_RELPTR_MASK, 20) |
1126 			     FIELD_PREP(TX_VTAG1_DEF_MASK, req->vtag1_def) |
1127 			     FIELD_PREP(TX_VTAG1_OP_MASK, req->vtag1_op) |
1128 			     FIELD_PREP(TX_VTAG1_LID_MASK, NPC_LID_LA) |
1129 			     FIELD_PREP(TX_VTAG1_RELPTR_MASK, 24);
1130 }
1131 
1132 static int npc_install_flow(struct rvu *rvu, int blkaddr, u16 target,
1133 			    int nixlf, struct rvu_pfvf *pfvf,
1134 			    struct npc_install_flow_req *req,
1135 			    struct npc_install_flow_rsp *rsp, bool enable,
1136 			    bool pf_set_vfs_mac)
1137 {
1138 	struct rvu_npc_mcam_rule *def_ucast_rule = pfvf->def_ucast_rule;
1139 	u64 features, installed_features, missing_features = 0;
1140 	struct npc_mcam_write_entry_req write_req = { 0 };
1141 	struct npc_mcam *mcam = &rvu->hw->mcam;
1142 	struct rvu_npc_mcam_rule dummy = { 0 };
1143 	struct rvu_npc_mcam_rule *rule;
1144 	u16 owner = req->hdr.pcifunc;
1145 	struct msg_rsp write_rsp;
1146 	struct mcam_entry *entry;
1147 	bool new = false;
1148 	u16 entry_index;
1149 	int err;
1150 
1151 	installed_features = req->features;
1152 	features = req->features;
1153 	entry = &write_req.entry_data;
1154 	entry_index = req->entry;
1155 
1156 	npc_update_flow(rvu, entry, features, &req->packet, &req->mask, &dummy,
1157 			req->intf, blkaddr);
1158 
1159 	if (is_npc_intf_rx(req->intf))
1160 		npc_update_rx_entry(rvu, pfvf, entry, req, target, pf_set_vfs_mac);
1161 	else
1162 		npc_update_tx_entry(rvu, pfvf, entry, req, target);
1163 
1164 	/* Default unicast rules do not exist for TX */
1165 	if (is_npc_intf_tx(req->intf))
1166 		goto find_rule;
1167 
1168 	if (req->default_rule) {
1169 		entry_index = npc_get_nixlf_mcam_index(mcam, target, nixlf,
1170 						       NIXLF_UCAST_ENTRY);
1171 		enable = is_mcam_entry_enabled(rvu, mcam, blkaddr, entry_index);
1172 	}
1173 
1174 	/* update mcam entry with default unicast rule attributes */
1175 	if (def_ucast_rule && (req->default_rule && req->append)) {
1176 		missing_features = (def_ucast_rule->features ^ features) &
1177 					def_ucast_rule->features;
1178 		if (missing_features)
1179 			npc_update_flow(rvu, entry, missing_features,
1180 					&def_ucast_rule->packet,
1181 					&def_ucast_rule->mask,
1182 					&dummy, req->intf,
1183 					blkaddr);
1184 		installed_features = req->features | missing_features;
1185 	}
1186 
1187 find_rule:
1188 	rule = rvu_mcam_find_rule(mcam, entry_index);
1189 	if (!rule) {
1190 		rule = kzalloc(sizeof(*rule), GFP_KERNEL);
1191 		if (!rule)
1192 			return -ENOMEM;
1193 		new = true;
1194 	}
1195 
1196 	/* allocate new counter if rule has no counter */
1197 	if (!req->default_rule && req->set_cntr && !rule->has_cntr)
1198 		rvu_mcam_add_counter_to_rule(rvu, owner, rule, rsp);
1199 
1200 	/* if user wants to delete an existing counter for a rule then
1201 	 * free the counter
1202 	 */
1203 	if (!req->set_cntr && rule->has_cntr)
1204 		rvu_mcam_remove_counter_from_rule(rvu, owner, rule);
1205 
1206 	write_req.hdr.pcifunc = owner;
1207 
1208 	/* AF owns the default rules so change the owner just to relax
1209 	 * the checks in rvu_mbox_handler_npc_mcam_write_entry
1210 	 */
1211 	if (req->default_rule)
1212 		write_req.hdr.pcifunc = 0;
1213 
1214 	write_req.entry = entry_index;
1215 	write_req.intf = req->intf;
1216 	write_req.enable_entry = (u8)enable;
1217 	/* if counter is available then clear and use it */
1218 	if (req->set_cntr && rule->has_cntr) {
1219 		rvu_write64(rvu, blkaddr, NPC_AF_MATCH_STATX(rule->cntr), req->cntr_val);
1220 		write_req.set_cntr = 1;
1221 		write_req.cntr = rule->cntr;
1222 	}
1223 
1224 	/* update rule */
1225 	memcpy(&rule->packet, &dummy.packet, sizeof(rule->packet));
1226 	memcpy(&rule->mask, &dummy.mask, sizeof(rule->mask));
1227 	rule->entry = entry_index;
1228 	memcpy(&rule->rx_action, &entry->action, sizeof(struct nix_rx_action));
1229 	if (is_npc_intf_tx(req->intf))
1230 		memcpy(&rule->tx_action, &entry->action,
1231 		       sizeof(struct nix_tx_action));
1232 	rule->vtag_action = entry->vtag_action;
1233 	rule->features = installed_features;
1234 	rule->default_rule = req->default_rule;
1235 	rule->owner = owner;
1236 	rule->enable = enable;
1237 	rule->chan_mask = write_req.entry_data.kw_mask[0] & NPC_KEX_CHAN_MASK;
1238 	rule->chan = write_req.entry_data.kw[0] & NPC_KEX_CHAN_MASK;
1239 	rule->chan &= rule->chan_mask;
1240 	rule->lxmb = dummy.lxmb;
1241 	if (is_npc_intf_tx(req->intf))
1242 		rule->intf = pfvf->nix_tx_intf;
1243 	else
1244 		rule->intf = pfvf->nix_rx_intf;
1245 
1246 	if (new)
1247 		rvu_mcam_add_rule(mcam, rule);
1248 	if (req->default_rule)
1249 		pfvf->def_ucast_rule = rule;
1250 
1251 	/* write to mcam entry registers */
1252 	err = rvu_mbox_handler_npc_mcam_write_entry(rvu, &write_req,
1253 						    &write_rsp);
1254 	if (err) {
1255 		rvu_mcam_remove_counter_from_rule(rvu, owner, rule);
1256 		if (new) {
1257 			list_del(&rule->list);
1258 			kfree(rule);
1259 		}
1260 		return err;
1261 	}
1262 
1263 	/* VF's MAC address is being changed via PF  */
1264 	if (pf_set_vfs_mac) {
1265 		ether_addr_copy(pfvf->default_mac, req->packet.dmac);
1266 		ether_addr_copy(pfvf->mac_addr, req->packet.dmac);
1267 		set_bit(PF_SET_VF_MAC, &pfvf->flags);
1268 	}
1269 
1270 	if (test_bit(PF_SET_VF_CFG, &pfvf->flags) &&
1271 	    req->vtag0_type == NIX_AF_LFX_RX_VTAG_TYPE7)
1272 		rule->vfvlan_cfg = true;
1273 
1274 	if (is_npc_intf_rx(req->intf) && req->match_id &&
1275 	    (req->op == NIX_RX_ACTIONOP_UCAST || req->op == NIX_RX_ACTIONOP_RSS))
1276 		return rvu_nix_setup_ratelimit_aggr(rvu, req->hdr.pcifunc,
1277 					     req->index, req->match_id);
1278 
1279 	return 0;
1280 }
1281 
1282 int rvu_mbox_handler_npc_install_flow(struct rvu *rvu,
1283 				      struct npc_install_flow_req *req,
1284 				      struct npc_install_flow_rsp *rsp)
1285 {
1286 	bool from_vf = !!(req->hdr.pcifunc & RVU_PFVF_FUNC_MASK);
1287 	struct rvu_switch *rswitch = &rvu->rswitch;
1288 	int blkaddr, nixlf, err;
1289 	struct rvu_pfvf *pfvf;
1290 	bool pf_set_vfs_mac = false;
1291 	bool enable = true;
1292 	u16 target;
1293 
1294 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1295 	if (blkaddr < 0) {
1296 		dev_err(rvu->dev, "%s: NPC block not implemented\n", __func__);
1297 		return NPC_MCAM_INVALID_REQ;
1298 	}
1299 
1300 	if (!is_npc_interface_valid(rvu, req->intf))
1301 		return NPC_FLOW_INTF_INVALID;
1302 
1303 	/* If DMAC is not extracted in MKEX, rules installed by AF
1304 	 * can rely on L2MB bit set by hardware protocol checker for
1305 	 * broadcast and multicast addresses.
1306 	 */
1307 	if (npc_check_field(rvu, blkaddr, NPC_DMAC, req->intf))
1308 		goto process_flow;
1309 
1310 	if (is_pffunc_af(req->hdr.pcifunc) &&
1311 	    req->features & BIT_ULL(NPC_DMAC)) {
1312 		if (is_unicast_ether_addr(req->packet.dmac)) {
1313 			dev_warn(rvu->dev,
1314 				 "%s: mkex profile does not support ucast flow\n",
1315 				 __func__);
1316 			return NPC_FLOW_NOT_SUPPORTED;
1317 		}
1318 
1319 		if (!npc_is_field_present(rvu, NPC_LXMB, req->intf)) {
1320 			dev_warn(rvu->dev,
1321 				 "%s: mkex profile does not support bcast/mcast flow",
1322 				 __func__);
1323 			return NPC_FLOW_NOT_SUPPORTED;
1324 		}
1325 
1326 		/* Modify feature to use LXMB instead of DMAC */
1327 		req->features &= ~BIT_ULL(NPC_DMAC);
1328 		req->features |= BIT_ULL(NPC_LXMB);
1329 	}
1330 
1331 process_flow:
1332 	if (from_vf && req->default_rule)
1333 		return NPC_FLOW_VF_PERM_DENIED;
1334 
1335 	/* Each PF/VF info is maintained in struct rvu_pfvf.
1336 	 * rvu_pfvf for the target PF/VF needs to be retrieved
1337 	 * hence modify pcifunc accordingly.
1338 	 */
1339 
1340 	/* AF installing for a PF/VF */
1341 	if (!req->hdr.pcifunc)
1342 		target = req->vf;
1343 	/* PF installing for its VF */
1344 	else if (!from_vf && req->vf) {
1345 		target = (req->hdr.pcifunc & ~RVU_PFVF_FUNC_MASK) | req->vf;
1346 		pf_set_vfs_mac = req->default_rule &&
1347 				(req->features & BIT_ULL(NPC_DMAC));
1348 	}
1349 	/* msg received from PF/VF */
1350 	else
1351 		target = req->hdr.pcifunc;
1352 
1353 	/* ignore chan_mask in case pf func is not AF, revisit later */
1354 	if (!is_pffunc_af(req->hdr.pcifunc))
1355 		req->chan_mask = 0xFFF;
1356 
1357 	err = npc_check_unsupported_flows(rvu, req->features, req->intf);
1358 	if (err)
1359 		return NPC_FLOW_NOT_SUPPORTED;
1360 
1361 	pfvf = rvu_get_pfvf(rvu, target);
1362 
1363 	/* PF installing for its VF */
1364 	if (req->hdr.pcifunc && !from_vf && req->vf)
1365 		set_bit(PF_SET_VF_CFG, &pfvf->flags);
1366 
1367 	/* update req destination mac addr */
1368 	if ((req->features & BIT_ULL(NPC_DMAC)) && is_npc_intf_rx(req->intf) &&
1369 	    is_zero_ether_addr(req->packet.dmac)) {
1370 		ether_addr_copy(req->packet.dmac, pfvf->mac_addr);
1371 		eth_broadcast_addr((u8 *)&req->mask.dmac);
1372 	}
1373 
1374 	/* Proceed if NIXLF is attached or not for TX rules */
1375 	err = nix_get_nixlf(rvu, target, &nixlf, NULL);
1376 	if (err && is_npc_intf_rx(req->intf) && !pf_set_vfs_mac)
1377 		return NPC_FLOW_NO_NIXLF;
1378 
1379 	/* don't enable rule when nixlf not attached or initialized */
1380 	if (!(is_nixlf_attached(rvu, target) &&
1381 	      test_bit(NIXLF_INITIALIZED, &pfvf->flags)))
1382 		enable = false;
1383 
1384 	/* Packets reaching NPC in Tx path implies that a
1385 	 * NIXLF is properly setup and transmitting.
1386 	 * Hence rules can be enabled for Tx.
1387 	 */
1388 	if (is_npc_intf_tx(req->intf))
1389 		enable = true;
1390 
1391 	/* Do not allow requests from uninitialized VFs */
1392 	if (from_vf && !enable)
1393 		return NPC_FLOW_VF_NOT_INIT;
1394 
1395 	/* PF sets VF mac & VF NIXLF is not attached, update the mac addr */
1396 	if (pf_set_vfs_mac && !enable) {
1397 		ether_addr_copy(pfvf->default_mac, req->packet.dmac);
1398 		ether_addr_copy(pfvf->mac_addr, req->packet.dmac);
1399 		set_bit(PF_SET_VF_MAC, &pfvf->flags);
1400 		return 0;
1401 	}
1402 
1403 	mutex_lock(&rswitch->switch_lock);
1404 	err = npc_install_flow(rvu, blkaddr, target, nixlf, pfvf,
1405 			       req, rsp, enable, pf_set_vfs_mac);
1406 	mutex_unlock(&rswitch->switch_lock);
1407 
1408 	return err;
1409 }
1410 
1411 static int npc_delete_flow(struct rvu *rvu, struct rvu_npc_mcam_rule *rule,
1412 			   u16 pcifunc)
1413 {
1414 	struct npc_mcam_ena_dis_entry_req dis_req = { 0 };
1415 	struct msg_rsp dis_rsp;
1416 
1417 	if (rule->default_rule)
1418 		return 0;
1419 
1420 	if (rule->has_cntr)
1421 		rvu_mcam_remove_counter_from_rule(rvu, pcifunc, rule);
1422 
1423 	dis_req.hdr.pcifunc = pcifunc;
1424 	dis_req.entry = rule->entry;
1425 
1426 	list_del(&rule->list);
1427 	kfree(rule);
1428 
1429 	return rvu_mbox_handler_npc_mcam_dis_entry(rvu, &dis_req, &dis_rsp);
1430 }
1431 
1432 int rvu_mbox_handler_npc_delete_flow(struct rvu *rvu,
1433 				     struct npc_delete_flow_req *req,
1434 				     struct npc_delete_flow_rsp *rsp)
1435 {
1436 	struct npc_mcam *mcam = &rvu->hw->mcam;
1437 	struct rvu_npc_mcam_rule *iter, *tmp;
1438 	u16 pcifunc = req->hdr.pcifunc;
1439 	struct list_head del_list;
1440 	int blkaddr;
1441 
1442 	INIT_LIST_HEAD(&del_list);
1443 
1444 	mutex_lock(&mcam->lock);
1445 	list_for_each_entry_safe(iter, tmp, &mcam->mcam_rules, list) {
1446 		if (iter->owner == pcifunc) {
1447 			/* All rules */
1448 			if (req->all) {
1449 				list_move_tail(&iter->list, &del_list);
1450 			/* Range of rules */
1451 			} else if (req->end && iter->entry >= req->start &&
1452 				   iter->entry <= req->end) {
1453 				list_move_tail(&iter->list, &del_list);
1454 			/* single rule */
1455 			} else if (req->entry == iter->entry) {
1456 				blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1457 				if (blkaddr)
1458 					rsp->cntr_val = rvu_read64(rvu, blkaddr,
1459 								   NPC_AF_MATCH_STATX(iter->cntr));
1460 				list_move_tail(&iter->list, &del_list);
1461 				break;
1462 			}
1463 		}
1464 	}
1465 	mutex_unlock(&mcam->lock);
1466 
1467 	list_for_each_entry_safe(iter, tmp, &del_list, list) {
1468 		u16 entry = iter->entry;
1469 
1470 		/* clear the mcam entry target pcifunc */
1471 		mcam->entry2target_pffunc[entry] = 0x0;
1472 		if (npc_delete_flow(rvu, iter, pcifunc))
1473 			dev_err(rvu->dev, "rule deletion failed for entry:%u",
1474 				entry);
1475 	}
1476 
1477 	return 0;
1478 }
1479 
1480 static int npc_update_dmac_value(struct rvu *rvu, int npcblkaddr,
1481 				 struct rvu_npc_mcam_rule *rule,
1482 				 struct rvu_pfvf *pfvf)
1483 {
1484 	struct npc_mcam_write_entry_req write_req = { 0 };
1485 	struct mcam_entry *entry = &write_req.entry_data;
1486 	struct npc_mcam *mcam = &rvu->hw->mcam;
1487 	struct msg_rsp rsp;
1488 	u8 intf, enable;
1489 	int err;
1490 
1491 	ether_addr_copy(rule->packet.dmac, pfvf->mac_addr);
1492 
1493 	npc_read_mcam_entry(rvu, mcam, npcblkaddr, rule->entry,
1494 			    entry, &intf,  &enable);
1495 
1496 	npc_update_entry(rvu, NPC_DMAC, entry,
1497 			 ether_addr_to_u64(pfvf->mac_addr), 0,
1498 			 0xffffffffffffull, 0, intf);
1499 
1500 	write_req.hdr.pcifunc = rule->owner;
1501 	write_req.entry = rule->entry;
1502 	write_req.intf = pfvf->nix_rx_intf;
1503 
1504 	mutex_unlock(&mcam->lock);
1505 	err = rvu_mbox_handler_npc_mcam_write_entry(rvu, &write_req, &rsp);
1506 	mutex_lock(&mcam->lock);
1507 
1508 	return err;
1509 }
1510 
1511 void npc_mcam_enable_flows(struct rvu *rvu, u16 target)
1512 {
1513 	struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, target);
1514 	struct rvu_npc_mcam_rule *def_ucast_rule;
1515 	struct npc_mcam *mcam = &rvu->hw->mcam;
1516 	struct rvu_npc_mcam_rule *rule;
1517 	int blkaddr, bank, index;
1518 	u64 def_action;
1519 
1520 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1521 	if (blkaddr < 0)
1522 		return;
1523 
1524 	def_ucast_rule = pfvf->def_ucast_rule;
1525 
1526 	mutex_lock(&mcam->lock);
1527 	list_for_each_entry(rule, &mcam->mcam_rules, list) {
1528 		if (is_npc_intf_rx(rule->intf) &&
1529 		    rule->rx_action.pf_func == target && !rule->enable) {
1530 			if (rule->default_rule) {
1531 				npc_enable_mcam_entry(rvu, mcam, blkaddr,
1532 						      rule->entry, true);
1533 				rule->enable = true;
1534 				continue;
1535 			}
1536 
1537 			if (rule->vfvlan_cfg)
1538 				npc_update_dmac_value(rvu, blkaddr, rule, pfvf);
1539 
1540 			if (rule->rx_action.op == NIX_RX_ACTION_DEFAULT) {
1541 				if (!def_ucast_rule)
1542 					continue;
1543 				/* Use default unicast entry action */
1544 				rule->rx_action = def_ucast_rule->rx_action;
1545 				def_action = *(u64 *)&def_ucast_rule->rx_action;
1546 				bank = npc_get_bank(mcam, rule->entry);
1547 				rvu_write64(rvu, blkaddr,
1548 					    NPC_AF_MCAMEX_BANKX_ACTION
1549 					    (rule->entry, bank), def_action);
1550 			}
1551 
1552 			npc_enable_mcam_entry(rvu, mcam, blkaddr,
1553 					      rule->entry, true);
1554 			rule->enable = true;
1555 		}
1556 	}
1557 
1558 	/* Enable MCAM entries installed by PF with target as VF pcifunc */
1559 	for (index = 0; index < mcam->bmap_entries; index++) {
1560 		if (mcam->entry2target_pffunc[index] == target)
1561 			npc_enable_mcam_entry(rvu, mcam, blkaddr,
1562 					      index, true);
1563 	}
1564 	mutex_unlock(&mcam->lock);
1565 }
1566 
1567 void npc_mcam_disable_flows(struct rvu *rvu, u16 target)
1568 {
1569 	struct npc_mcam *mcam = &rvu->hw->mcam;
1570 	int blkaddr, index;
1571 
1572 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1573 	if (blkaddr < 0)
1574 		return;
1575 
1576 	mutex_lock(&mcam->lock);
1577 	/* Disable MCAM entries installed by PF with target as VF pcifunc */
1578 	for (index = 0; index < mcam->bmap_entries; index++) {
1579 		if (mcam->entry2target_pffunc[index] == target)
1580 			npc_enable_mcam_entry(rvu, mcam, blkaddr,
1581 					      index, false);
1582 	}
1583 	mutex_unlock(&mcam->lock);
1584 }
1585 
1586 /* single drop on non hit rule starting from 0th index. This an extension
1587  * to RPM mac filter to support more rules.
1588  */
1589 int npc_install_mcam_drop_rule(struct rvu *rvu, int mcam_idx, u16 *counter_idx,
1590 			       u64 chan_val, u64 chan_mask, u64 exact_val, u64 exact_mask,
1591 			       u64 bcast_mcast_val, u64 bcast_mcast_mask)
1592 {
1593 	struct npc_mcam_alloc_counter_req cntr_req = { 0 };
1594 	struct npc_mcam_alloc_counter_rsp cntr_rsp = { 0 };
1595 	struct npc_mcam_write_entry_req req = { 0 };
1596 	struct npc_mcam *mcam = &rvu->hw->mcam;
1597 	struct rvu_npc_mcam_rule *rule;
1598 	struct msg_rsp rsp;
1599 	bool enabled;
1600 	int blkaddr;
1601 	int err;
1602 
1603 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1604 	if (blkaddr < 0) {
1605 		dev_err(rvu->dev, "%s: NPC block not implemented\n", __func__);
1606 		return -ENODEV;
1607 	}
1608 
1609 	/* Bail out if no exact match support */
1610 	if (!rvu_npc_exact_has_match_table(rvu)) {
1611 		dev_info(rvu->dev, "%s: No support for exact match feature\n", __func__);
1612 		return -EINVAL;
1613 	}
1614 
1615 	/* If 0th entry is already used, return err */
1616 	enabled = is_mcam_entry_enabled(rvu, mcam, blkaddr, mcam_idx);
1617 	if (enabled) {
1618 		dev_err(rvu->dev, "%s: failed to add single drop on non hit rule at %d th index\n",
1619 			__func__, mcam_idx);
1620 		return	-EINVAL;
1621 	}
1622 
1623 	/* Add this entry to mcam rules list */
1624 	rule = kzalloc(sizeof(*rule), GFP_KERNEL);
1625 	if (!rule)
1626 		return -ENOMEM;
1627 
1628 	/* Disable rule by default. Enable rule when first dmac filter is
1629 	 * installed
1630 	 */
1631 	rule->enable = false;
1632 	rule->chan = chan_val;
1633 	rule->chan_mask = chan_mask;
1634 	rule->entry = mcam_idx;
1635 	rvu_mcam_add_rule(mcam, rule);
1636 
1637 	/* Reserve slot 0 */
1638 	npc_mcam_rsrcs_reserve(rvu, blkaddr, mcam_idx);
1639 
1640 	/* Allocate counter for this single drop on non hit rule */
1641 	cntr_req.hdr.pcifunc = 0; /* AF request */
1642 	cntr_req.contig = true;
1643 	cntr_req.count = 1;
1644 	err = rvu_mbox_handler_npc_mcam_alloc_counter(rvu, &cntr_req, &cntr_rsp);
1645 	if (err) {
1646 		dev_err(rvu->dev, "%s: Err to allocate cntr for drop rule (err=%d)\n",
1647 			__func__, err);
1648 		return	-EFAULT;
1649 	}
1650 	*counter_idx = cntr_rsp.cntr;
1651 
1652 	/* Fill in fields for this mcam entry */
1653 	npc_update_entry(rvu, NPC_EXACT_RESULT, &req.entry_data, exact_val, 0,
1654 			 exact_mask, 0, NIX_INTF_RX);
1655 	npc_update_entry(rvu, NPC_CHAN, &req.entry_data, chan_val, 0,
1656 			 chan_mask, 0, NIX_INTF_RX);
1657 	npc_update_entry(rvu, NPC_LXMB, &req.entry_data, bcast_mcast_val, 0,
1658 			 bcast_mcast_mask, 0, NIX_INTF_RX);
1659 
1660 	req.intf = NIX_INTF_RX;
1661 	req.set_cntr = true;
1662 	req.cntr = cntr_rsp.cntr;
1663 	req.entry = mcam_idx;
1664 
1665 	err = rvu_mbox_handler_npc_mcam_write_entry(rvu, &req, &rsp);
1666 	if (err) {
1667 		dev_err(rvu->dev, "%s: Installation of single drop on non hit rule at %d failed\n",
1668 			__func__, mcam_idx);
1669 		return err;
1670 	}
1671 
1672 	dev_err(rvu->dev, "%s: Installed single drop on non hit rule at %d, cntr=%d\n",
1673 		__func__, mcam_idx, req.cntr);
1674 
1675 	/* disable entry at Bank 0, index 0 */
1676 	npc_enable_mcam_entry(rvu, mcam, blkaddr, mcam_idx, false);
1677 
1678 	return 0;
1679 }
1680 
1681 int rvu_mbox_handler_npc_get_field_status(struct rvu *rvu,
1682 					  struct npc_get_field_status_req *req,
1683 					  struct npc_get_field_status_rsp *rsp)
1684 {
1685 	int blkaddr;
1686 
1687 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1688 	if (blkaddr < 0)
1689 		return NPC_MCAM_INVALID_REQ;
1690 
1691 	if (!is_npc_interface_valid(rvu, req->intf))
1692 		return NPC_FLOW_INTF_INVALID;
1693 
1694 	if (npc_check_field(rvu, blkaddr, req->field, req->intf))
1695 		rsp->enable = 1;
1696 
1697 	return 0;
1698 }
1699