xref: /linux/drivers/net/ethernet/marvell/octeontx2/af/rvu_npc_fs.c (revision 91ec2035134982b98fab0609a9fd8480e8217dc1)
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 #include "cn20k/reg.h"
16 #include "cn20k/npc.h"
17 
18 static const char * const npc_flow_names[] = {
19 	[NPC_DMAC]	= "dmac",
20 	[NPC_SMAC]	= "smac",
21 	[NPC_ETYPE]	= "ether type",
22 	[NPC_VLAN_ETYPE_CTAG] = "vlan ether type ctag",
23 	[NPC_VLAN_ETYPE_STAG] = "vlan ether type stag",
24 	[NPC_OUTER_VID]	= "outer vlan id",
25 	[NPC_INNER_VID]	= "inner vlan id",
26 	[NPC_TOS]	= "tos",
27 	[NPC_IPFRAG_IPV4] = "fragmented IPv4 header ",
28 	[NPC_SIP_IPV4]	= "ipv4 source ip",
29 	[NPC_DIP_IPV4]	= "ipv4 destination ip",
30 	[NPC_IPFRAG_IPV6] = "fragmented IPv6 header ",
31 	[NPC_SIP_IPV6]	= "ipv6 source ip",
32 	[NPC_DIP_IPV6]	= "ipv6 destination ip",
33 	[NPC_IPPROTO_TCP] = "ip proto tcp",
34 	[NPC_IPPROTO_UDP] = "ip proto udp",
35 	[NPC_IPPROTO_SCTP] = "ip proto sctp",
36 	[NPC_IPPROTO_ICMP] = "ip proto icmp",
37 	[NPC_IPPROTO_ICMP6] = "ip proto icmp6",
38 	[NPC_IPPROTO_AH] = "ip proto AH",
39 	[NPC_IPPROTO_ESP] = "ip proto ESP",
40 	[NPC_SPORT_TCP]	= "tcp source port",
41 	[NPC_DPORT_TCP]	= "tcp destination port",
42 	[NPC_SPORT_UDP]	= "udp source port",
43 	[NPC_DPORT_UDP]	= "udp destination port",
44 	[NPC_SPORT_SCTP] = "sctp source port",
45 	[NPC_DPORT_SCTP] = "sctp destination port",
46 	[NPC_LXMB]	= "Mcast/Bcast header ",
47 	[NPC_IPSEC_SPI] = "SPI ",
48 	[NPC_MPLS1_LBTCBOS] = "lse depth 1 label tc bos",
49 	[NPC_MPLS1_TTL]     = "lse depth 1 ttl",
50 	[NPC_MPLS2_LBTCBOS] = "lse depth 2 label tc bos",
51 	[NPC_MPLS2_TTL]     = "lse depth 2 ttl",
52 	[NPC_MPLS3_LBTCBOS] = "lse depth 3 label tc bos",
53 	[NPC_MPLS3_TTL]     = "lse depth 3 ttl",
54 	[NPC_MPLS4_LBTCBOS] = "lse depth 4 label tc bos",
55 	[NPC_MPLS4_TTL]     = "lse depth 4",
56 	[NPC_TYPE_ICMP] = "icmp type",
57 	[NPC_CODE_ICMP] = "icmp code",
58 	[NPC_TCP_FLAGS] = "tcp flags",
59 	[NPC_UNKNOWN]	= "unknown",
60 };
61 
npc_is_feature_supported(struct rvu * rvu,u64 features,u8 intf)62 bool npc_is_feature_supported(struct rvu *rvu, u64 features, u8 intf)
63 {
64 	struct npc_mcam *mcam = &rvu->hw->mcam;
65 	u64 mcam_features;
66 	u64 unsupported;
67 
68 	mcam_features = is_npc_intf_tx(intf) ? mcam->tx_features : mcam->rx_features;
69 	unsupported = (mcam_features ^ features) & ~mcam_features;
70 
71 	/* Return false if at least one of the input flows is not extracted */
72 	return !unsupported;
73 }
74 
npc_get_field_name(u8 hdr)75 const char *npc_get_field_name(u8 hdr)
76 {
77 	if (hdr >= ARRAY_SIZE(npc_flow_names))
78 		return npc_flow_names[NPC_UNKNOWN];
79 
80 	return npc_flow_names[hdr];
81 }
82 
83 /* Compute keyword masks and figure out the number of keywords a field
84  * spans in the key.
85  */
npc_set_kw_masks(struct rvu * rvu,struct npc_mcam * mcam,u8 type,u8 nr_bits,int start_kwi,int offset,u8 intf)86 static void npc_set_kw_masks(struct rvu *rvu, struct npc_mcam *mcam, u8 type,
87 			     u8 nr_bits, int start_kwi, int offset, u8 intf)
88 {
89 	struct npc_key_field *field = &mcam->rx_key_fields[type];
90 	u8 bits_in_kw;
91 	int max_kwi;
92 
93 	if (is_cn20k(rvu->pdev)) {
94 		if (mcam->banks_per_entry == 1)
95 			max_kwi = 3; /* NPC_MCAM_KEY_X2 */
96 		else
97 			max_kwi = 7; /* NPC_MCAM_KEY_X4 */
98 	} else {
99 		if (mcam->banks_per_entry == 1)
100 			max_kwi = 1; /* NPC_MCAM_KEY_X1 */
101 		else if (mcam->banks_per_entry == 2)
102 			max_kwi = 3; /* NPC_MCAM_KEY_X2 */
103 		else
104 			max_kwi = 6; /* NPC_MCAM_KEY_X4 */
105 	}
106 
107 	if (is_npc_intf_tx(intf))
108 		field = &mcam->tx_key_fields[type];
109 
110 	if (offset + nr_bits <= 64) {
111 		/* one KW only */
112 		if (start_kwi > max_kwi)
113 			return;
114 		field->kw_mask[start_kwi] |= GENMASK_ULL(nr_bits - 1, 0)
115 					     << offset;
116 		field->nr_kws = 1;
117 	} else if (offset + nr_bits > 64 &&
118 		   offset + nr_bits <= 128) {
119 		/* two KWs */
120 		if (start_kwi + 1 > max_kwi)
121 			return;
122 		/* first KW mask */
123 		bits_in_kw = 64 - offset;
124 		field->kw_mask[start_kwi] |= GENMASK_ULL(bits_in_kw - 1, 0)
125 					     << offset;
126 		/* second KW mask i.e. mask for rest of bits */
127 		bits_in_kw = nr_bits + offset - 64;
128 		field->kw_mask[start_kwi + 1] |= GENMASK_ULL(bits_in_kw - 1, 0);
129 		field->nr_kws = 2;
130 	} else {
131 		/* three KWs */
132 		if (start_kwi + 2 > max_kwi)
133 			return;
134 		/* first KW mask */
135 		bits_in_kw = 64 - offset;
136 		field->kw_mask[start_kwi] |= GENMASK_ULL(bits_in_kw - 1, 0)
137 					     << offset;
138 		/* second KW mask */
139 		field->kw_mask[start_kwi + 1] = ~0ULL;
140 		/* third KW mask i.e. mask for rest of bits */
141 		bits_in_kw = nr_bits + offset - 128;
142 		field->kw_mask[start_kwi + 2] |= GENMASK_ULL(bits_in_kw - 1, 0);
143 		field->nr_kws = 3;
144 	}
145 }
146 
147 /* Helper function to figure out whether field exists in the key */
npc_is_field_present(struct rvu * rvu,enum key_fields type,u8 intf)148 static bool npc_is_field_present(struct rvu *rvu, enum key_fields type, u8 intf)
149 {
150 	struct npc_mcam *mcam = &rvu->hw->mcam;
151 	struct npc_key_field *input;
152 
153 	input  = &mcam->rx_key_fields[type];
154 	if (is_npc_intf_tx(intf))
155 		input  = &mcam->tx_key_fields[type];
156 
157 	return input->nr_kws > 0;
158 }
159 
npc_is_same(struct npc_key_field * input,struct npc_key_field * field)160 static bool npc_is_same(struct npc_key_field *input,
161 			struct npc_key_field *field)
162 {
163 	return memcmp(&input->layer_mdata, &field->layer_mdata,
164 		     sizeof(struct npc_layer_mdata)) == 0;
165 }
166 
npc_set_layer_mdata(struct rvu * rvu,struct npc_mcam * mcam,enum key_fields type,u64 cfg,u8 lid,u8 lt,u8 intf)167 static void npc_set_layer_mdata(struct rvu *rvu,
168 				struct npc_mcam *mcam, enum key_fields type,
169 				u64 cfg, u8 lid, u8 lt, u8 intf)
170 {
171 	struct npc_key_field *input = &mcam->rx_key_fields[type];
172 
173 	if (is_npc_intf_tx(intf))
174 		input = &mcam->tx_key_fields[type];
175 
176 	input->layer_mdata.hdr = FIELD_GET(NPC_HDR_OFFSET, cfg);
177 	input->layer_mdata.key = FIELD_GET(NPC_KEY_OFFSET, cfg);
178 	if (is_cn20k(rvu->pdev))
179 		input->layer_mdata.len = FIELD_GET(NPC_CN20K_BYTESM, cfg) + 1;
180 	else
181 		input->layer_mdata.len = FIELD_GET(NPC_BYTESM, cfg) + 1;
182 	input->layer_mdata.ltype = lt;
183 	input->layer_mdata.lid = lid;
184 }
185 
npc_check_overlap_fields(struct npc_key_field * input1,struct npc_key_field * input2,int max_kw)186 static bool npc_check_overlap_fields(struct npc_key_field *input1,
187 				     struct npc_key_field *input2,
188 				     int max_kw)
189 {
190 	int kwi;
191 
192 	/* Fields with same layer id and different ltypes are mutually
193 	 * exclusive hence they can be overlapped
194 	 */
195 	if (input1->layer_mdata.lid == input2->layer_mdata.lid &&
196 	    input1->layer_mdata.ltype != input2->layer_mdata.ltype)
197 		return false;
198 
199 	for (kwi = 0; kwi < max_kw; kwi++) {
200 		if (input1->kw_mask[kwi] & input2->kw_mask[kwi])
201 			return true;
202 	}
203 
204 	return false;
205 }
206 
207 /* Helper function to check whether given field overlaps with any other fields
208  * in the key. Due to limitations on key size and the key extraction profile in
209  * use higher layers can overwrite lower layer's header fields. Hence overlap
210  * needs to be checked.
211  */
npc_check_overlap(struct rvu * rvu,int blkaddr,enum key_fields type,u8 start_lid,u8 intf)212 static bool npc_check_overlap(struct rvu *rvu, int blkaddr,
213 			      enum key_fields type, u8 start_lid, u8 intf)
214 {
215 	struct npc_mcam *mcam = &rvu->hw->mcam;
216 	struct npc_key_field *dummy, *input;
217 	int start_kwi, offset;
218 	u8 nr_bits, lid, lt, ld;
219 	int extr, kws;
220 	u64 cfg;
221 
222 	dummy = &mcam->rx_key_fields[NPC_UNKNOWN];
223 	input = &mcam->rx_key_fields[type];
224 
225 	if (is_npc_intf_tx(intf)) {
226 		dummy = &mcam->tx_key_fields[NPC_UNKNOWN];
227 		input = &mcam->tx_key_fields[type];
228 	}
229 
230 	kws = NPC_KWS_IN_KEY_SZ_7;
231 
232 	if (is_cn20k(rvu->pdev))
233 		goto skip_cn10k_config;
234 
235 	for (lid = start_lid; lid < NPC_MAX_LID; lid++) {
236 		for (lt = 0; lt < NPC_MAX_LT; lt++) {
237 			for (ld = 0; ld < NPC_MAX_LD; ld++) {
238 				cfg = rvu_read64(rvu, blkaddr,
239 						 NPC_AF_INTFX_LIDX_LTX_LDX_CFG
240 						 (intf, lid, lt, ld));
241 				if (!FIELD_GET(NPC_LDATA_EN, cfg))
242 					continue;
243 				memset(dummy, 0, sizeof(struct npc_key_field));
244 				npc_set_layer_mdata(rvu, mcam, NPC_UNKNOWN,
245 						    cfg, lid, lt, intf);
246 				/* exclude input */
247 				if (npc_is_same(input, dummy))
248 					continue;
249 				start_kwi = dummy->layer_mdata.key / 8;
250 				offset = (dummy->layer_mdata.key * 8) % 64;
251 				nr_bits = dummy->layer_mdata.len * 8;
252 				/* form KW masks */
253 				npc_set_kw_masks(rvu, mcam, NPC_UNKNOWN,
254 						 nr_bits, start_kwi,
255 						 offset, intf);
256 				/* check any input field bits falls in any
257 				 * other field bits.
258 				 */
259 				if (npc_check_overlap_fields(dummy, input, kws))
260 					return true;
261 			}
262 		}
263 	}
264 	return false;
265 
266 skip_cn10k_config:
267 	for (extr = 0 ; extr < rvu->hw->npc_kex_extr; extr++) {
268 		lid = CN20K_GET_EXTR_LID(intf, extr);
269 		if (lid < start_lid)
270 			continue;
271 		for (lt = 0; lt < NPC_MAX_LT; lt++) {
272 			cfg = CN20K_GET_EXTR_LT(intf, extr, lt);
273 			if (!FIELD_GET(NPC_LDATA_EN, cfg))
274 				continue;
275 
276 			memset(dummy, 0, sizeof(struct npc_key_field));
277 			npc_set_layer_mdata(rvu, mcam, NPC_UNKNOWN, cfg,
278 					    lid, lt, intf);
279 			/* exclude input */
280 			if (npc_is_same(input, dummy))
281 				continue;
282 			start_kwi = dummy->layer_mdata.key / 8;
283 			offset = (dummy->layer_mdata.key * 8) % 64;
284 			nr_bits = dummy->layer_mdata.len * 8;
285 			/* form KW masks */
286 			npc_set_kw_masks(rvu, mcam, NPC_UNKNOWN, nr_bits,
287 					 start_kwi, offset, intf);
288 			/* check any input field bits falls in any other
289 			 * field bits
290 			 */
291 			if (npc_check_overlap_fields(dummy, input,
292 						     NPC_KWS_IN_KEY_SZ_8))
293 				return true;
294 		}
295 	}
296 
297 	return false;
298 }
299 
npc_check_field(struct rvu * rvu,int blkaddr,enum key_fields type,u8 intf)300 static bool npc_check_field(struct rvu *rvu, int blkaddr, enum key_fields type,
301 			    u8 intf)
302 {
303 	if (!npc_is_field_present(rvu, type, intf) ||
304 	    npc_check_overlap(rvu, blkaddr, type, 0, intf))
305 		return false;
306 	return true;
307 }
308 
npc_scan_exact_result(struct rvu * rvu,struct npc_mcam * mcam,u8 bit_number,u8 key_nibble,u8 intf)309 static void npc_scan_exact_result(struct rvu *rvu,
310 				  struct npc_mcam *mcam, u8 bit_number,
311 				  u8 key_nibble, u8 intf)
312 {
313 	u8 offset = (key_nibble * 4) % 64; /* offset within key word */
314 	u8 kwi = (key_nibble * 4) / 64; /* which word in key */
315 	u8 nr_bits = 4; /* bits in a nibble */
316 	u8 type;
317 
318 	switch (bit_number) {
319 	case 40 ... 43:
320 		type = NPC_EXACT_RESULT;
321 		break;
322 
323 	default:
324 		return;
325 	}
326 	npc_set_kw_masks(rvu, mcam, type, nr_bits, kwi, offset, intf);
327 }
328 
npc_cn20k_scan_parse_result(struct rvu * rvu,struct npc_mcam * mcam,u8 bit_number,u8 key_nibble,u8 intf)329 static void npc_cn20k_scan_parse_result(struct rvu *rvu, struct npc_mcam *mcam,
330 					u8 bit_number, u8 key_nibble, u8 intf)
331 {
332 	u8 offset = (key_nibble * 4) % 64; /* offset within key word */
333 	u8 kwi = (key_nibble * 4) / 64; /* which word in key */
334 	u8 nr_bits = 4; /* bits in a nibble */
335 	u8 type;
336 
337 	switch (bit_number) {
338 	case 0 ... 2:
339 		type = NPC_CHAN;
340 		break;
341 	case 3:
342 		type = NPC_ERRLEV;
343 		break;
344 	case 4 ... 5:
345 		type = NPC_ERRCODE;
346 		break;
347 	case 6:
348 		type = NPC_LXMB;
349 		break;
350 	case 8:
351 		type = NPC_LA;
352 		break;
353 	case 10:
354 		type = NPC_LB;
355 		break;
356 	case 12:
357 		type = NPC_LC;
358 		break;
359 	case 14:
360 		type = NPC_LD;
361 		break;
362 	case 16:
363 		type = NPC_LE;
364 		break;
365 	case 18:
366 		type = NPC_LF;
367 		break;
368 	case 20:
369 		type = NPC_LG;
370 		break;
371 	case 22:
372 		type = NPC_LH;
373 		break;
374 	default:
375 		return;
376 	}
377 
378 	npc_set_kw_masks(rvu, mcam, type, nr_bits, kwi, offset, intf);
379 }
380 
npc_scan_parse_result(struct rvu * rvu,struct npc_mcam * mcam,u8 bit_number,u8 key_nibble,u8 intf)381 static void npc_scan_parse_result(struct rvu *rvu,
382 				  struct npc_mcam *mcam, u8 bit_number,
383 				  u8 key_nibble, u8 intf)
384 {
385 	u8 offset = (key_nibble * 4) % 64; /* offset within key word */
386 	u8 kwi = (key_nibble * 4) / 64; /* which word in key */
387 	u8 nr_bits = 4; /* bits in a nibble */
388 	u8 type;
389 
390 	if (is_cn20k(rvu->pdev)) {
391 		npc_cn20k_scan_parse_result(rvu, mcam, bit_number,
392 					    key_nibble, intf);
393 		return;
394 	}
395 
396 	switch (bit_number) {
397 	case 0 ... 2:
398 		type = NPC_CHAN;
399 		break;
400 	case 3:
401 		type = NPC_ERRLEV;
402 		break;
403 	case 4 ... 5:
404 		type = NPC_ERRCODE;
405 		break;
406 	case 6:
407 		type = NPC_LXMB;
408 		break;
409 	/* check for LTYPE only as of now */
410 	case 9:
411 		type = NPC_LA;
412 		break;
413 	case 12:
414 		type = NPC_LB;
415 		break;
416 	case 15:
417 		type = NPC_LC;
418 		break;
419 	case 18:
420 		type = NPC_LD;
421 		break;
422 	case 21:
423 		type = NPC_LE;
424 		break;
425 	case 24:
426 		type = NPC_LF;
427 		break;
428 	case 27:
429 		type = NPC_LG;
430 		break;
431 	case 30:
432 		type = NPC_LH;
433 		break;
434 	default:
435 		return;
436 	}
437 
438 	npc_set_kw_masks(rvu, mcam, type, nr_bits, kwi, offset, intf);
439 }
440 
npc_handle_multi_layer_fields(struct rvu * rvu,int blkaddr,u8 intf)441 static void npc_handle_multi_layer_fields(struct rvu *rvu, int blkaddr, u8 intf)
442 {
443 	struct npc_mcam *mcam = &rvu->hw->mcam;
444 	struct npc_key_field *key_fields;
445 	/* Ether type can come from three layers
446 	 * (ethernet, single tagged, double tagged)
447 	 */
448 	struct npc_key_field *etype_ether;
449 	struct npc_key_field *etype_tag1;
450 	struct npc_key_field *etype_tag2;
451 	/* Outer VLAN TCI can come from two layers
452 	 * (single tagged, double tagged)
453 	 */
454 	struct npc_key_field *vlan_tag1;
455 	struct npc_key_field *vlan_tag2;
456 	/* Inner VLAN TCI for double tagged frames */
457 	struct npc_key_field *vlan_tag3;
458 	u64 *features;
459 	int i, max_kw;
460 	u8 start_lid;
461 
462 	if (is_cn20k(rvu->pdev))
463 		max_kw = NPC_KWS_IN_KEY_SZ_8;
464 	else
465 		max_kw = NPC_KWS_IN_KEY_SZ_7;
466 
467 	key_fields = mcam->rx_key_fields;
468 	features = &mcam->rx_features;
469 
470 	if (is_npc_intf_tx(intf)) {
471 		key_fields = mcam->tx_key_fields;
472 		features = &mcam->tx_features;
473 	}
474 
475 	/* Handle header fields which can come from multiple layers like
476 	 * etype, outer vlan tci. These fields should have same position in
477 	 * the key otherwise to install a mcam rule more than one entry is
478 	 * needed which complicates mcam space management.
479 	 */
480 	etype_ether = &key_fields[NPC_ETYPE_ETHER];
481 	etype_tag1 = &key_fields[NPC_ETYPE_TAG1];
482 	etype_tag2 = &key_fields[NPC_ETYPE_TAG2];
483 	vlan_tag1 = &key_fields[NPC_VLAN_TAG1];
484 	vlan_tag2 = &key_fields[NPC_VLAN_TAG2];
485 	vlan_tag3 = &key_fields[NPC_VLAN_TAG3];
486 
487 	/* if key profile programmed does not extract Ethertype at all */
488 	if (!etype_ether->nr_kws && !etype_tag1->nr_kws && !etype_tag2->nr_kws) {
489 		dev_err(rvu->dev, "mkex: Ethertype is not extracted.\n");
490 		goto vlan_tci;
491 	}
492 
493 	/* if key profile programmed extracts Ethertype from one layer */
494 	if (etype_ether->nr_kws && !etype_tag1->nr_kws && !etype_tag2->nr_kws)
495 		key_fields[NPC_ETYPE] = *etype_ether;
496 	if (!etype_ether->nr_kws && etype_tag1->nr_kws && !etype_tag2->nr_kws)
497 		key_fields[NPC_ETYPE] = *etype_tag1;
498 	if (!etype_ether->nr_kws && !etype_tag1->nr_kws && etype_tag2->nr_kws)
499 		key_fields[NPC_ETYPE] = *etype_tag2;
500 
501 	/* if key profile programmed extracts Ethertype from multiple layers */
502 	if (etype_ether->nr_kws && etype_tag1->nr_kws) {
503 		for (i = 0; i < max_kw; i++) {
504 			if (etype_ether->kw_mask[i] != etype_tag1->kw_mask[i]) {
505 				dev_err(rvu->dev, "mkex: Etype pos is different for untagged and tagged pkts.\n");
506 				goto vlan_tci;
507 			}
508 		}
509 		key_fields[NPC_ETYPE] = *etype_tag1;
510 	}
511 	if (etype_ether->nr_kws && etype_tag2->nr_kws) {
512 		for (i = 0; i < max_kw; i++) {
513 			if (etype_ether->kw_mask[i] != etype_tag2->kw_mask[i]) {
514 				dev_err(rvu->dev, "mkex: Etype pos is different for untagged and double tagged pkts.\n");
515 				goto vlan_tci;
516 			}
517 		}
518 		key_fields[NPC_ETYPE] = *etype_tag2;
519 	}
520 	if (etype_tag1->nr_kws && etype_tag2->nr_kws) {
521 		for (i = 0; i < max_kw; i++) {
522 			if (etype_tag1->kw_mask[i] != etype_tag2->kw_mask[i]) {
523 				dev_err(rvu->dev, "mkex: Etype pos is different for tagged and double tagged pkts.\n");
524 				goto vlan_tci;
525 			}
526 		}
527 		key_fields[NPC_ETYPE] = *etype_tag2;
528 	}
529 
530 	/* check none of higher layers overwrite Ethertype */
531 	start_lid = key_fields[NPC_ETYPE].layer_mdata.lid + 1;
532 	if (npc_check_overlap(rvu, blkaddr, NPC_ETYPE, start_lid, intf)) {
533 		dev_err(rvu->dev, "mkex: Ethertype is overwritten by higher layers.\n");
534 		goto vlan_tci;
535 	}
536 	*features |= BIT_ULL(NPC_ETYPE);
537 vlan_tci:
538 	/* if key profile does not extract outer vlan tci at all */
539 	if (!vlan_tag1->nr_kws && !vlan_tag2->nr_kws) {
540 		dev_err(rvu->dev, "mkex: Outer vlan tci is not extracted.\n");
541 		goto done;
542 	}
543 
544 	/* if key profile extracts outer vlan tci from one layer */
545 	if (vlan_tag1->nr_kws && !vlan_tag2->nr_kws)
546 		key_fields[NPC_OUTER_VID] = *vlan_tag1;
547 	if (!vlan_tag1->nr_kws && vlan_tag2->nr_kws)
548 		key_fields[NPC_OUTER_VID] = *vlan_tag2;
549 
550 	/* if key profile extracts outer vlan tci from multiple layers */
551 	if (vlan_tag1->nr_kws && vlan_tag2->nr_kws) {
552 		for (i = 0; i < max_kw; i++) {
553 			if (vlan_tag1->kw_mask[i] != vlan_tag2->kw_mask[i]) {
554 				dev_err(rvu->dev, "mkex: Out vlan tci pos is different for tagged and double tagged pkts.\n");
555 				goto done;
556 			}
557 		}
558 		key_fields[NPC_OUTER_VID] = *vlan_tag2;
559 	}
560 	/* check none of higher layers overwrite outer vlan tci */
561 	start_lid = key_fields[NPC_OUTER_VID].layer_mdata.lid + 1;
562 	if (npc_check_overlap(rvu, blkaddr, NPC_OUTER_VID, start_lid, intf)) {
563 		dev_err(rvu->dev, "mkex: Outer vlan tci is overwritten by higher layers.\n");
564 		goto done;
565 	}
566 	*features |= BIT_ULL(NPC_OUTER_VID);
567 
568 	/* If key profile extracts inner vlan tci */
569 	if (vlan_tag3->nr_kws) {
570 		key_fields[NPC_INNER_VID] = *vlan_tag3;
571 		*features |= BIT_ULL(NPC_INNER_VID);
572 	}
573 done:
574 	return;
575 }
576 
npc_scan_ldata(struct rvu * rvu,int blkaddr,u8 lid,u8 lt,u64 cfg,u8 intf)577 static void npc_scan_ldata(struct rvu *rvu, int blkaddr, u8 lid,
578 			   u8 lt, u64 cfg, u8 intf)
579 {
580 	struct npc_mcam_kex_hash *mkex_hash = rvu->kpu.mkex_hash;
581 	struct npc_mcam *mcam = &rvu->hw->mcam;
582 	u8 hdr, key, nr_bytes, bit_offset;
583 	u8 la_ltype, la_start;
584 	/* starting KW index and starting bit position */
585 	int start_kwi, offset;
586 
587 	if (is_cn20k(rvu->pdev))
588 		nr_bytes = FIELD_GET(NPC_CN20K_BYTESM, cfg) + 1;
589 	else
590 		nr_bytes = FIELD_GET(NPC_BYTESM, cfg) + 1;
591 
592 	hdr = FIELD_GET(NPC_HDR_OFFSET, cfg);
593 	key = FIELD_GET(NPC_KEY_OFFSET, cfg);
594 
595 	/* For Tx, Layer A has NIX_INST_HDR_S(64 bytes) preceding
596 	 * ethernet header.
597 	 */
598 	if (is_npc_intf_tx(intf)) {
599 		la_ltype = NPC_LT_LA_IH_NIX_ETHER;
600 		la_start = 8;
601 	} else {
602 		la_ltype = NPC_LT_LA_ETHER;
603 		la_start = 0;
604 	}
605 
606 #define NPC_SCAN_HDR(name, hlid, hlt, hstart, hlen)			       \
607 do {									       \
608 	start_kwi = key / 8;						       \
609 	offset = (key * 8) % 64;					       \
610 	if (lid == (hlid) && lt == (hlt)) {				       \
611 		if ((hstart) >= hdr &&					       \
612 		    ((hstart) + (hlen)) <= (hdr + nr_bytes)) {	               \
613 			bit_offset = (hdr + nr_bytes - (hstart) - (hlen)) * 8; \
614 			npc_set_layer_mdata(rvu, mcam, (name), cfg, lid, lt,   \
615 									intf); \
616 			offset += bit_offset;				       \
617 			start_kwi += offset / 64;			       \
618 			offset %= 64;					       \
619 			npc_set_kw_masks(rvu, mcam, (name), (hlen) * 8,	       \
620 					 start_kwi, offset, intf);	       \
621 		}							       \
622 	}								       \
623 } while (0)
624 
625 	/* List LID, LTYPE, start offset from layer and length(in bytes) of
626 	 * packet header fields below.
627 	 * Example: Source IP is 4 bytes and starts at 12th byte of IP header
628 	 */
629 	NPC_SCAN_HDR(NPC_TOS, NPC_LID_LC, NPC_LT_LC_IP, 1, 1);
630 	NPC_SCAN_HDR(NPC_IPFRAG_IPV4, NPC_LID_LC, NPC_LT_LC_IP, 6, 1);
631 	NPC_SCAN_HDR(NPC_SIP_IPV4, NPC_LID_LC, NPC_LT_LC_IP, 12, 4);
632 	NPC_SCAN_HDR(NPC_DIP_IPV4, NPC_LID_LC, NPC_LT_LC_IP, 16, 4);
633 	NPC_SCAN_HDR(NPC_IPFRAG_IPV6, NPC_LID_LC, NPC_LT_LC_IP6_EXT, 6, 1);
634 	if (rvu->hw->cap.npc_hash_extract) {
635 		if (mkex_hash->lid_lt_ld_hash_en[intf][lid][lt][0])
636 			NPC_SCAN_HDR(NPC_SIP_IPV6, NPC_LID_LC, NPC_LT_LC_IP6, 8, 4);
637 		else
638 			NPC_SCAN_HDR(NPC_SIP_IPV6, NPC_LID_LC, NPC_LT_LC_IP6, 8, 16);
639 
640 		if (mkex_hash->lid_lt_ld_hash_en[intf][lid][lt][1])
641 			NPC_SCAN_HDR(NPC_DIP_IPV6, NPC_LID_LC, NPC_LT_LC_IP6, 24, 4);
642 		else
643 			NPC_SCAN_HDR(NPC_DIP_IPV6, NPC_LID_LC, NPC_LT_LC_IP6, 24, 16);
644 	} else {
645 		NPC_SCAN_HDR(NPC_SIP_IPV6, NPC_LID_LC, NPC_LT_LC_IP6, 8, 16);
646 		NPC_SCAN_HDR(NPC_DIP_IPV6, NPC_LID_LC, NPC_LT_LC_IP6, 24, 16);
647 	}
648 
649 	NPC_SCAN_HDR(NPC_SPORT_UDP, NPC_LID_LD, NPC_LT_LD_UDP, 0, 2);
650 	NPC_SCAN_HDR(NPC_DPORT_UDP, NPC_LID_LD, NPC_LT_LD_UDP, 2, 2);
651 	NPC_SCAN_HDR(NPC_SPORT_TCP, NPC_LID_LD, NPC_LT_LD_TCP, 0, 2);
652 	NPC_SCAN_HDR(NPC_DPORT_TCP, NPC_LID_LD, NPC_LT_LD_TCP, 2, 2);
653 	NPC_SCAN_HDR(NPC_SPORT_SCTP, NPC_LID_LD, NPC_LT_LD_SCTP, 0, 2);
654 	NPC_SCAN_HDR(NPC_DPORT_SCTP, NPC_LID_LD, NPC_LT_LD_SCTP, 2, 2);
655 	NPC_SCAN_HDR(NPC_TYPE_ICMP, NPC_LID_LD, NPC_LT_LD_ICMP, 0, 1);
656 	NPC_SCAN_HDR(NPC_CODE_ICMP, NPC_LID_LD, NPC_LT_LD_ICMP, 1, 1);
657 	NPC_SCAN_HDR(NPC_TCP_FLAGS, NPC_LID_LD, NPC_LT_LD_TCP, 12, 2);
658 	NPC_SCAN_HDR(NPC_ETYPE_ETHER, NPC_LID_LA, NPC_LT_LA_ETHER, 12, 2);
659 	NPC_SCAN_HDR(NPC_ETYPE_TAG1, NPC_LID_LB, NPC_LT_LB_CTAG, 4, 2);
660 	NPC_SCAN_HDR(NPC_ETYPE_TAG2, NPC_LID_LB, NPC_LT_LB_STAG_QINQ, 8, 2);
661 	NPC_SCAN_HDR(NPC_VLAN_TAG1, NPC_LID_LB, NPC_LT_LB_CTAG, 2, 2);
662 	NPC_SCAN_HDR(NPC_VLAN_TAG2, NPC_LID_LB, NPC_LT_LB_STAG_QINQ, 2, 2);
663 	NPC_SCAN_HDR(NPC_VLAN_TAG3, NPC_LID_LB, NPC_LT_LB_STAG_QINQ, 6, 2);
664 	NPC_SCAN_HDR(NPC_DMAC, NPC_LID_LA, la_ltype, la_start, 6);
665 
666 	NPC_SCAN_HDR(NPC_IPSEC_SPI, NPC_LID_LD, NPC_LT_LD_AH, 4, 4);
667 	NPC_SCAN_HDR(NPC_IPSEC_SPI, NPC_LID_LE, NPC_LT_LE_ESP, 0, 4);
668 	NPC_SCAN_HDR(NPC_MPLS1_LBTCBOS, NPC_LID_LC, NPC_LT_LC_MPLS, 0, 3);
669 	NPC_SCAN_HDR(NPC_MPLS1_TTL, NPC_LID_LC, NPC_LT_LC_MPLS, 3, 1);
670 	NPC_SCAN_HDR(NPC_MPLS2_LBTCBOS, NPC_LID_LC, NPC_LT_LC_MPLS, 4, 3);
671 	NPC_SCAN_HDR(NPC_MPLS2_TTL, NPC_LID_LC, NPC_LT_LC_MPLS, 7, 1);
672 	NPC_SCAN_HDR(NPC_MPLS3_LBTCBOS, NPC_LID_LC, NPC_LT_LC_MPLS, 8, 3);
673 	NPC_SCAN_HDR(NPC_MPLS3_TTL, NPC_LID_LC, NPC_LT_LC_MPLS, 11, 1);
674 	NPC_SCAN_HDR(NPC_MPLS4_LBTCBOS, NPC_LID_LC, NPC_LT_LC_MPLS, 12, 3);
675 	NPC_SCAN_HDR(NPC_MPLS4_TTL, NPC_LID_LC, NPC_LT_LC_MPLS, 15, 1);
676 
677 	/* SMAC follows the DMAC(which is 6 bytes) */
678 	NPC_SCAN_HDR(NPC_SMAC, NPC_LID_LA, la_ltype, la_start + 6, 6);
679 	/* PF_FUNC is 2 bytes at 0th byte of NPC_LT_LA_IH_NIX_ETHER */
680 	NPC_SCAN_HDR(NPC_PF_FUNC, NPC_LID_LA, NPC_LT_LA_IH_NIX_ETHER, 0, 2);
681 }
682 
npc_set_features(struct rvu * rvu,int blkaddr,u8 intf)683 static void npc_set_features(struct rvu *rvu, int blkaddr, u8 intf)
684 {
685 	struct npc_mcam *mcam = &rvu->hw->mcam;
686 	u64 *features = &mcam->rx_features;
687 	u64 proto_flags;
688 	int hdr;
689 
690 	if (is_npc_intf_tx(intf))
691 		features = &mcam->tx_features;
692 
693 	for (hdr = NPC_DMAC; hdr < NPC_HEADER_FIELDS_MAX; hdr++) {
694 		if (npc_check_field(rvu, blkaddr, hdr, intf))
695 			*features |= BIT_ULL(hdr);
696 	}
697 
698 	proto_flags = BIT_ULL(NPC_SPORT_TCP) | BIT_ULL(NPC_SPORT_UDP) |
699 		       BIT_ULL(NPC_DPORT_TCP) | BIT_ULL(NPC_DPORT_UDP) |
700 		       BIT_ULL(NPC_SPORT_SCTP) | BIT_ULL(NPC_DPORT_SCTP) |
701 		       BIT_ULL(NPC_SPORT_SCTP) | BIT_ULL(NPC_DPORT_SCTP) |
702 		       BIT_ULL(NPC_TYPE_ICMP) | BIT_ULL(NPC_CODE_ICMP) |
703 		       BIT_ULL(NPC_TCP_FLAGS);
704 
705 	/* for tcp/udp/sctp corresponding layer type should be in the key */
706 	if (*features & proto_flags) {
707 		if (!npc_check_field(rvu, blkaddr, NPC_LD, intf))
708 			*features &= ~proto_flags;
709 		else
710 			*features |= BIT_ULL(NPC_IPPROTO_TCP) |
711 				     BIT_ULL(NPC_IPPROTO_UDP) |
712 				     BIT_ULL(NPC_IPPROTO_SCTP) |
713 				     BIT_ULL(NPC_IPPROTO_ICMP);
714 	}
715 
716 	/* for AH/ICMP/ICMPv6/, check if corresponding layer type is present in the key */
717 	if (npc_check_field(rvu, blkaddr, NPC_LD, intf)) {
718 		*features |= BIT_ULL(NPC_IPPROTO_AH);
719 		*features |= BIT_ULL(NPC_IPPROTO_ICMP);
720 		*features |= BIT_ULL(NPC_IPPROTO_ICMP6);
721 	}
722 
723 	/* for ESP, check if corresponding layer type is present in the key */
724 	if (npc_check_field(rvu, blkaddr, NPC_LE, intf))
725 		*features |= BIT_ULL(NPC_IPPROTO_ESP);
726 
727 	/* for vlan corresponding layer type should be in the key */
728 	if (*features & BIT_ULL(NPC_OUTER_VID))
729 		if (!npc_check_field(rvu, blkaddr, NPC_LB, intf))
730 			*features &= ~BIT_ULL(NPC_OUTER_VID);
731 
732 	/* Warn on unrelated MKEX fields colliding with SPI key bits.  AH/ESP
733 	 * sharing the same SPI key offset is valid; use npc_is_field_present(),
734 	 * not npc_check_field(), to advertise the feature.
735 	 */
736 	if (npc_check_overlap(rvu, blkaddr, NPC_IPSEC_SPI, 0, intf))
737 		dev_warn(rvu->dev, "Overlap detected the field NPC_IPSEC_SPI\n");
738 	if (npc_is_field_present(rvu, NPC_IPSEC_SPI, intf) &&
739 	    (*features & (BIT_ULL(NPC_IPPROTO_ESP) | BIT_ULL(NPC_IPPROTO_AH))))
740 		*features |= BIT_ULL(NPC_IPSEC_SPI);
741 
742 	/* for vlan ethertypes corresponding layer type should be in the key */
743 	if (npc_check_field(rvu, blkaddr, NPC_LB, intf))
744 		*features |= BIT_ULL(NPC_VLAN_ETYPE_CTAG) |
745 			     BIT_ULL(NPC_VLAN_ETYPE_STAG);
746 
747 	/* for L2M/L2B/L3M/L3B, check if the type is present in the key */
748 	if (npc_check_field(rvu, blkaddr, NPC_LXMB, intf))
749 		*features |= BIT_ULL(NPC_LXMB);
750 
751 	for (hdr = NPC_MPLS1_LBTCBOS; hdr <= NPC_MPLS4_TTL; hdr++) {
752 		if (npc_check_field(rvu, blkaddr, hdr, intf))
753 			*features |= BIT_ULL(hdr);
754 	}
755 }
756 
757 /* Scan key extraction profile and record how fields of our interest
758  * fill the key structure. Also verify Channel and DMAC exists in
759  * key and not overwritten by other header fields.
760  */
npc_scan_kex(struct rvu * rvu,int blkaddr,u8 intf)761 static int npc_scan_kex(struct rvu *rvu, int blkaddr, u8 intf)
762 {
763 	struct npc_mcam *mcam = &rvu->hw->mcam;
764 	u8 lid, lt, ld, bitnr;
765 	u64 cfg, masked_cfg;
766 	u8 key_nibble = 0;
767 	int extr;
768 
769 	/* Scan and note how parse result is going to be in key.
770 	 * A bit set in PARSE_NIBBLE_ENA corresponds to a nibble from
771 	 * parse result in the key. The enabled nibbles from parse result
772 	 * will be concatenated in key.
773 	 */
774 	cfg = rvu_read64(rvu, blkaddr, NPC_AF_INTFX_KEX_CFG(intf));
775 	if (is_cn20k(rvu->pdev)) {
776 		masked_cfg = cfg & NPC_CN20K_PARSE_NIBBLE;
777 		for_each_set_bit(bitnr, (unsigned long *)&masked_cfg,
778 				 NPC_CN20K_TOTAL_NIBBLE) {
779 			npc_scan_parse_result(rvu, mcam, bitnr,
780 					      key_nibble, intf);
781 			key_nibble++;
782 		}
783 	} else {
784 		masked_cfg = cfg & NPC_PARSE_NIBBLE;
785 		for_each_set_bit(bitnr, (unsigned long *)&masked_cfg,
786 				 NPC_TOTAL_NIBBLE) {
787 			npc_scan_parse_result(rvu, mcam, bitnr,
788 					      key_nibble, intf);
789 			key_nibble++;
790 		}
791 	}
792 
793 	/* Ignore exact match bits for mcam entries except the first rule
794 	 * which is drop on hit. This first rule is configured explitcitly by
795 	 * exact match code.
796 	 */
797 	masked_cfg = cfg & NPC_EXACT_NIBBLE;
798 	bitnr = NPC_EXACT_NIBBLE_START;
799 	for_each_set_bit_from(bitnr, (unsigned long *)&masked_cfg, NPC_EXACT_NIBBLE_END + 1) {
800 		npc_scan_exact_result(rvu, mcam, bitnr, key_nibble, intf);
801 		key_nibble++;
802 	}
803 
804 	if (is_cn20k(rvu->pdev))
805 		goto skip_cn10k_config;
806 
807 	/* Scan and note how layer data is going to be in key */
808 	for (lid = 0; lid < NPC_MAX_LID; lid++) {
809 		for (lt = 0; lt < NPC_MAX_LT; lt++) {
810 			for (ld = 0; ld < NPC_MAX_LD; ld++) {
811 				cfg = rvu_read64(rvu, blkaddr,
812 						 NPC_AF_INTFX_LIDX_LTX_LDX_CFG
813 						 (intf, lid, lt, ld));
814 				if (!FIELD_GET(NPC_LDATA_EN, cfg))
815 					continue;
816 				npc_scan_ldata(rvu, blkaddr, lid, lt, cfg,
817 					       intf);
818 			}
819 		}
820 	}
821 
822 	return 0;
823 
824 skip_cn10k_config:
825 	for (extr = 0 ; extr < rvu->hw->npc_kex_extr; extr++) {
826 		lid = CN20K_GET_EXTR_LID(intf, extr);
827 		for (lt = 0; lt < NPC_MAX_LT; lt++) {
828 			cfg = CN20K_GET_EXTR_LT(intf, extr, lt);
829 			if (!FIELD_GET(NPC_LDATA_EN, cfg))
830 				continue;
831 			npc_scan_ldata(rvu, blkaddr, lid, lt, cfg,
832 				       intf);
833 		}
834 	}
835 	return 0;
836 }
837 
npc_scan_verify_kex(struct rvu * rvu,int blkaddr)838 static int npc_scan_verify_kex(struct rvu *rvu, int blkaddr)
839 {
840 	int err;
841 
842 	err = npc_scan_kex(rvu, blkaddr, NIX_INTF_RX);
843 	if (err)
844 		return err;
845 
846 	err = npc_scan_kex(rvu, blkaddr, NIX_INTF_TX);
847 	if (err)
848 		return err;
849 
850 	/* Channel is mandatory */
851 	if (!npc_is_field_present(rvu, NPC_CHAN, NIX_INTF_RX)) {
852 		dev_err(rvu->dev, "Channel not present in Key\n");
853 		return -EINVAL;
854 	}
855 	/* check that none of the fields overwrite channel */
856 	if (npc_check_overlap(rvu, blkaddr, NPC_CHAN, 0, NIX_INTF_RX)) {
857 		dev_err(rvu->dev, "Channel cannot be overwritten\n");
858 		return -EINVAL;
859 	}
860 
861 	npc_set_features(rvu, blkaddr, NIX_INTF_TX);
862 	npc_set_features(rvu, blkaddr, NIX_INTF_RX);
863 	npc_handle_multi_layer_fields(rvu, blkaddr, NIX_INTF_TX);
864 	npc_handle_multi_layer_fields(rvu, blkaddr, NIX_INTF_RX);
865 
866 	return 0;
867 }
868 
npc_flow_steering_init(struct rvu * rvu,int blkaddr)869 int npc_flow_steering_init(struct rvu *rvu, int blkaddr)
870 {
871 	struct npc_mcam *mcam = &rvu->hw->mcam;
872 
873 	INIT_LIST_HEAD(&mcam->mcam_rules);
874 
875 	return npc_scan_verify_kex(rvu, blkaddr);
876 }
877 
npc_check_unsupported_flows(struct rvu * rvu,u64 features,u8 intf)878 static int npc_check_unsupported_flows(struct rvu *rvu, u64 features, u8 intf)
879 {
880 	struct npc_mcam *mcam = &rvu->hw->mcam;
881 	u64 *mcam_features = &mcam->rx_features;
882 	u64 unsupported;
883 	u8 bit;
884 
885 	if (is_npc_intf_tx(intf))
886 		mcam_features = &mcam->tx_features;
887 
888 	unsupported = (*mcam_features ^ features) & ~(*mcam_features);
889 	if (unsupported) {
890 		dev_warn(rvu->dev, "Unsupported flow(s):\n");
891 		for_each_set_bit(bit, (unsigned long *)&unsupported, 64)
892 			dev_warn(rvu->dev, "%s ", npc_get_field_name(bit));
893 		return -EOPNOTSUPP;
894 	}
895 
896 	return 0;
897 }
898 
899 /* npc_update_entry - Based on the masks generated during
900  * the key scanning, updates the given entry with value and
901  * masks for the field of interest. Maximum 16 bytes of a packet
902  * header can be extracted by HW hence lo and hi are sufficient.
903  * When field bytes are less than or equal to 8 then hi should be
904  * 0 for value and mask.
905  *
906  * If exact match of value is required then mask should be all 1's.
907  * If any bits in mask are 0 then corresponding bits in value are
908  * dont care.
909  */
npc_update_entry(struct rvu * rvu,enum key_fields type,struct mcam_entry_mdata * mdata,u64 val_lo,u64 val_hi,u64 mask_lo,u64 mask_hi,u8 intf)910 void npc_update_entry(struct rvu *rvu, enum key_fields type,
911 		      struct mcam_entry_mdata *mdata, u64 val_lo,
912 		      u64 val_hi, u64 mask_lo, u64 mask_hi, u8 intf)
913 {
914 	u64 kw_mask[NPC_KWS_IN_KEY_SZ_MAX] = { 0 };
915 	u64 kw[NPC_KWS_IN_KEY_SZ_MAX] = { 0 };
916 	struct npc_mcam *mcam = &rvu->hw->mcam;
917 	struct npc_key_field *field;
918 	u64 kw1, kw2, kw3;
919 	u64 *val, *mask;
920 	int i, max_kw;
921 	u8 shift;
922 
923 	field = &mcam->rx_key_fields[type];
924 	if (is_npc_intf_tx(intf))
925 		field = &mcam->tx_key_fields[type];
926 
927 	if (!field->nr_kws)
928 		return;
929 
930 	if (is_cn20k(rvu->pdev))
931 		max_kw = NPC_KWS_IN_KEY_SZ_8;
932 	else
933 		max_kw = NPC_KWS_IN_KEY_SZ_7;
934 
935 	for (i = 0; i < max_kw; i++) {
936 		if (!field->kw_mask[i])
937 			continue;
938 		/* place key value in kw[x] */
939 		shift = __ffs64(field->kw_mask[i]);
940 		/* update entry value */
941 		kw1 = (val_lo << shift) & field->kw_mask[i];
942 		kw[i] = kw1;
943 		/* update entry mask */
944 		kw1 = (mask_lo << shift) & field->kw_mask[i];
945 		kw_mask[i] = kw1;
946 
947 		if (field->nr_kws == 1)
948 			break;
949 		/* place remaining bits of key value in kw[x + 1] */
950 		if (field->nr_kws == 2) {
951 			/* update entry value */
952 			kw2 = shift ? val_lo >> (64 - shift) : 0;
953 			kw2 |= (val_hi << shift);
954 			kw2 &= field->kw_mask[i + 1];
955 			kw[i + 1] = kw2;
956 			/* update entry mask */
957 			kw2 = shift ? mask_lo >> (64 - shift) : 0;
958 			kw2 |= (mask_hi << shift);
959 			kw2 &= field->kw_mask[i + 1];
960 			kw_mask[i + 1] = kw2;
961 			break;
962 		}
963 		/* place remaining bits of key value in kw[x + 1], kw[x + 2] */
964 		if (field->nr_kws == 3) {
965 			/* update entry value */
966 			kw2 = shift ? val_lo >> (64 - shift) : 0;
967 			kw2 |= (val_hi << shift);
968 			kw2 &= field->kw_mask[i + 1];
969 			kw3 = shift ? val_hi >> (64 - shift) : 0;
970 			kw3 &= field->kw_mask[i + 2];
971 			kw[i + 1] = kw2;
972 			kw[i + 2] = kw3;
973 			/* update entry mask */
974 			kw2 = shift ? mask_lo >> (64 - shift) : 0;
975 			kw2 |= (mask_hi << shift);
976 			kw2 &= field->kw_mask[i + 1];
977 			kw3 = shift ? mask_hi >> (64 - shift) : 0;
978 			kw3 &= field->kw_mask[i + 2];
979 			kw_mask[i + 1] = kw2;
980 			kw_mask[i + 2] = kw3;
981 			break;
982 		}
983 	}
984 	/* dummy is ready with values and masks for given key
985 	 * field now clear and update input entry with those
986 	 */
987 
988 	val = mdata->kw;
989 	mask = mdata->kw_mask;
990 
991 	for (i = 0; i < max_kw; i++, val++, mask++) {
992 		if (!field->kw_mask[i])
993 			continue;
994 
995 		*val &= ~field->kw_mask[i];
996 		*mask &= ~field->kw_mask[i];
997 
998 		*val |= kw[i];
999 		*mask |= kw_mask[i];
1000 	}
1001 }
1002 
npc_update_ipv6_flow(struct rvu * rvu,struct mcam_entry_mdata * mdata,u64 features,struct flow_msg * pkt,struct flow_msg * mask,struct rvu_npc_mcam_rule * output,u8 intf)1003 static void npc_update_ipv6_flow(struct rvu *rvu,
1004 				 struct mcam_entry_mdata *mdata,
1005 				 u64 features, struct flow_msg *pkt,
1006 				 struct flow_msg *mask,
1007 				 struct rvu_npc_mcam_rule *output, u8 intf)
1008 {
1009 	u32 src_ip[IPV6_WORDS], src_ip_mask[IPV6_WORDS];
1010 	u32 dst_ip[IPV6_WORDS], dst_ip_mask[IPV6_WORDS];
1011 	struct flow_msg *opkt = &output->packet;
1012 	struct flow_msg *omask = &output->mask;
1013 	u64 mask_lo, mask_hi;
1014 	u64 val_lo, val_hi;
1015 
1016 	/* For an ipv6 address fe80::2c68:63ff:fe5e:2d0a the packet
1017 	 * values to be programmed in MCAM should as below:
1018 	 * val_high: 0xfe80000000000000
1019 	 * val_low: 0x2c6863fffe5e2d0a
1020 	 */
1021 	if (features & BIT_ULL(NPC_SIP_IPV6)) {
1022 		be32_to_cpu_array(src_ip_mask, mask->ip6src, IPV6_WORDS);
1023 		be32_to_cpu_array(src_ip, pkt->ip6src, IPV6_WORDS);
1024 
1025 		mask_hi = (u64)src_ip_mask[0] << 32 | src_ip_mask[1];
1026 		mask_lo = (u64)src_ip_mask[2] << 32 | src_ip_mask[3];
1027 		val_hi = (u64)src_ip[0] << 32 | src_ip[1];
1028 		val_lo = (u64)src_ip[2] << 32 | src_ip[3];
1029 
1030 		npc_update_entry(rvu, NPC_SIP_IPV6, mdata, val_lo, val_hi,
1031 				 mask_lo, mask_hi, intf);
1032 		memcpy(opkt->ip6src, pkt->ip6src, sizeof(opkt->ip6src));
1033 		memcpy(omask->ip6src, mask->ip6src, sizeof(omask->ip6src));
1034 	}
1035 	if (features & BIT_ULL(NPC_DIP_IPV6)) {
1036 		be32_to_cpu_array(dst_ip_mask, mask->ip6dst, IPV6_WORDS);
1037 		be32_to_cpu_array(dst_ip, pkt->ip6dst, IPV6_WORDS);
1038 
1039 		mask_hi = (u64)dst_ip_mask[0] << 32 | dst_ip_mask[1];
1040 		mask_lo = (u64)dst_ip_mask[2] << 32 | dst_ip_mask[3];
1041 		val_hi = (u64)dst_ip[0] << 32 | dst_ip[1];
1042 		val_lo = (u64)dst_ip[2] << 32 | dst_ip[3];
1043 
1044 		npc_update_entry(rvu, NPC_DIP_IPV6, mdata, val_lo, val_hi,
1045 				 mask_lo, mask_hi, intf);
1046 		memcpy(opkt->ip6dst, pkt->ip6dst, sizeof(opkt->ip6dst));
1047 		memcpy(omask->ip6dst, mask->ip6dst, sizeof(omask->ip6dst));
1048 	}
1049 }
1050 
npc_update_vlan_features(struct rvu * rvu,struct mcam_entry_mdata * mdata,u64 features,u8 intf)1051 static void npc_update_vlan_features(struct rvu *rvu,
1052 				     struct mcam_entry_mdata *mdata,
1053 				     u64 features, u8 intf)
1054 {
1055 	bool ctag = !!(features & BIT_ULL(NPC_VLAN_ETYPE_CTAG));
1056 	bool stag = !!(features & BIT_ULL(NPC_VLAN_ETYPE_STAG));
1057 	bool vid = !!(features & BIT_ULL(NPC_OUTER_VID));
1058 
1059 	/* If only VLAN id is given then always match outer VLAN id */
1060 	if (vid && !ctag && !stag) {
1061 		npc_update_entry(rvu, NPC_LB, mdata,
1062 				 NPC_LT_LB_STAG_QINQ | NPC_LT_LB_CTAG, 0,
1063 				 NPC_LT_LB_STAG_QINQ & NPC_LT_LB_CTAG, 0, intf);
1064 		return;
1065 	}
1066 	if (ctag)
1067 		npc_update_entry(rvu, NPC_LB, mdata, NPC_LT_LB_CTAG, 0,
1068 				 ~0ULL, 0, intf);
1069 	if (stag)
1070 		npc_update_entry(rvu, NPC_LB, mdata, NPC_LT_LB_STAG_QINQ, 0,
1071 				 ~0ULL, 0, intf);
1072 }
1073 
npc_update_flow(struct rvu * rvu,struct mcam_entry_mdata * mdata,u64 features,struct flow_msg * pkt,struct flow_msg * mask,struct rvu_npc_mcam_rule * output,u8 intf,int blkaddr)1074 void npc_update_flow(struct rvu *rvu, struct mcam_entry_mdata *mdata,
1075 		     u64 features, struct flow_msg *pkt,
1076 		     struct flow_msg *mask,
1077 		     struct rvu_npc_mcam_rule *output, u8 intf,
1078 		     int blkaddr)
1079 {
1080 	u64 dmac_mask = ether_addr_to_u64(mask->dmac);
1081 	u64 smac_mask = ether_addr_to_u64(mask->smac);
1082 	u64 dmac_val = ether_addr_to_u64(pkt->dmac);
1083 	u64 smac_val = ether_addr_to_u64(pkt->smac);
1084 	struct flow_msg *opkt = &output->packet;
1085 	struct flow_msg *omask = &output->mask;
1086 
1087 	if (!features)
1088 		return;
1089 
1090 	/* For tcp/udp/sctp LTYPE should be present in entry */
1091 	if (features & BIT_ULL(NPC_IPPROTO_TCP))
1092 		npc_update_entry(rvu, NPC_LD, mdata, NPC_LT_LD_TCP,
1093 				 0, ~0ULL, 0, intf);
1094 	if (features & BIT_ULL(NPC_IPPROTO_UDP))
1095 		npc_update_entry(rvu, NPC_LD, mdata, NPC_LT_LD_UDP,
1096 				 0, ~0ULL, 0, intf);
1097 	if (features & BIT_ULL(NPC_IPPROTO_SCTP))
1098 		npc_update_entry(rvu, NPC_LD, mdata, NPC_LT_LD_SCTP,
1099 				 0, ~0ULL, 0, intf);
1100 	if (features & BIT_ULL(NPC_IPPROTO_ICMP))
1101 		npc_update_entry(rvu, NPC_LD, mdata, NPC_LT_LD_ICMP,
1102 				 0, ~0ULL, 0, intf);
1103 	if (features & BIT_ULL(NPC_IPPROTO_ICMP6))
1104 		npc_update_entry(rvu, NPC_LD, mdata, NPC_LT_LD_ICMP6,
1105 				 0, ~0ULL, 0, intf);
1106 
1107 	/* For AH, LTYPE should be present in entry */
1108 	if (features & BIT_ULL(NPC_IPPROTO_AH))
1109 		npc_update_entry(rvu, NPC_LD, mdata, NPC_LT_LD_AH,
1110 				 0, ~0ULL, 0, intf);
1111 	/* For ESP, LTYPE should be present in entry */
1112 	if (features & BIT_ULL(NPC_IPPROTO_ESP))
1113 		npc_update_entry(rvu, NPC_LE, mdata, NPC_LT_LE_ESP,
1114 				 0, ~0ULL, 0, intf);
1115 
1116 	if (features & BIT_ULL(NPC_LXMB)) {
1117 		output->lxmb = is_broadcast_ether_addr(pkt->dmac) ? 2 : 1;
1118 		npc_update_entry(rvu, NPC_LXMB, mdata, output->lxmb, 0,
1119 				 output->lxmb, 0, intf);
1120 	}
1121 #define NPC_WRITE_FLOW(field, member, val_lo, val_hi, mask_lo, mask_hi)	      \
1122 do {									      \
1123 	if (features & BIT_ULL((field))) {				      \
1124 		npc_update_entry(rvu, (field), mdata, (val_lo), (val_hi),     \
1125 				 (mask_lo), (mask_hi), intf);		      \
1126 		memcpy(&opkt->member, &pkt->member, sizeof(pkt->member));     \
1127 		memcpy(&omask->member, &mask->member, sizeof(mask->member));  \
1128 	}								      \
1129 } while (0)
1130 
1131 	NPC_WRITE_FLOW(NPC_DMAC, dmac, dmac_val, 0, dmac_mask, 0);
1132 
1133 	NPC_WRITE_FLOW(NPC_SMAC, smac, smac_val, 0, smac_mask, 0);
1134 	NPC_WRITE_FLOW(NPC_ETYPE, etype, ntohs(pkt->etype), 0,
1135 		       ntohs(mask->etype), 0);
1136 	NPC_WRITE_FLOW(NPC_TOS, tos, pkt->tos, 0, mask->tos, 0);
1137 	NPC_WRITE_FLOW(NPC_IPFRAG_IPV4, ip_flag, pkt->ip_flag, 0,
1138 		       mask->ip_flag, 0);
1139 	NPC_WRITE_FLOW(NPC_SIP_IPV4, ip4src, ntohl(pkt->ip4src), 0,
1140 		       ntohl(mask->ip4src), 0);
1141 	NPC_WRITE_FLOW(NPC_DIP_IPV4, ip4dst, ntohl(pkt->ip4dst), 0,
1142 		       ntohl(mask->ip4dst), 0);
1143 	NPC_WRITE_FLOW(NPC_SPORT_TCP, sport, ntohs(pkt->sport), 0,
1144 		       ntohs(mask->sport), 0);
1145 	NPC_WRITE_FLOW(NPC_SPORT_UDP, sport, ntohs(pkt->sport), 0,
1146 		       ntohs(mask->sport), 0);
1147 	NPC_WRITE_FLOW(NPC_DPORT_TCP, dport, ntohs(pkt->dport), 0,
1148 		       ntohs(mask->dport), 0);
1149 	NPC_WRITE_FLOW(NPC_DPORT_UDP, dport, ntohs(pkt->dport), 0,
1150 		       ntohs(mask->dport), 0);
1151 	NPC_WRITE_FLOW(NPC_SPORT_SCTP, sport, ntohs(pkt->sport), 0,
1152 		       ntohs(mask->sport), 0);
1153 	NPC_WRITE_FLOW(NPC_DPORT_SCTP, dport, ntohs(pkt->dport), 0,
1154 		       ntohs(mask->dport), 0);
1155 	NPC_WRITE_FLOW(NPC_TYPE_ICMP, icmp_type, pkt->icmp_type, 0,
1156 		       mask->icmp_type, 0);
1157 	NPC_WRITE_FLOW(NPC_CODE_ICMP, icmp_code, pkt->icmp_code, 0,
1158 		       mask->icmp_code, 0);
1159 	NPC_WRITE_FLOW(NPC_TCP_FLAGS, tcp_flags, ntohs(pkt->tcp_flags), 0,
1160 		       ntohs(mask->tcp_flags), 0);
1161 	NPC_WRITE_FLOW(NPC_IPSEC_SPI, spi, ntohl(pkt->spi), 0,
1162 		       ntohl(mask->spi), 0);
1163 
1164 	NPC_WRITE_FLOW(NPC_OUTER_VID, vlan_tci, ntohs(pkt->vlan_tci), 0,
1165 		       ntohs(mask->vlan_tci), 0);
1166 	NPC_WRITE_FLOW(NPC_INNER_VID, vlan_itci, ntohs(pkt->vlan_itci), 0,
1167 		       ntohs(mask->vlan_itci), 0);
1168 
1169 	NPC_WRITE_FLOW(NPC_MPLS1_LBTCBOS, mpls_lse,
1170 		       FIELD_GET(OTX2_FLOWER_MASK_MPLS_NON_TTL,
1171 				 pkt->mpls_lse[0]), 0,
1172 		       FIELD_GET(OTX2_FLOWER_MASK_MPLS_NON_TTL,
1173 				 mask->mpls_lse[0]), 0);
1174 	NPC_WRITE_FLOW(NPC_MPLS1_TTL, mpls_lse,
1175 		       FIELD_GET(OTX2_FLOWER_MASK_MPLS_TTL,
1176 				 pkt->mpls_lse[0]), 0,
1177 		       FIELD_GET(OTX2_FLOWER_MASK_MPLS_TTL,
1178 				 mask->mpls_lse[0]), 0);
1179 	NPC_WRITE_FLOW(NPC_MPLS2_LBTCBOS, mpls_lse,
1180 		       FIELD_GET(OTX2_FLOWER_MASK_MPLS_NON_TTL,
1181 				 pkt->mpls_lse[1]), 0,
1182 		       FIELD_GET(OTX2_FLOWER_MASK_MPLS_NON_TTL,
1183 				 mask->mpls_lse[1]), 0);
1184 	NPC_WRITE_FLOW(NPC_MPLS2_TTL, mpls_lse,
1185 		       FIELD_GET(OTX2_FLOWER_MASK_MPLS_TTL,
1186 				 pkt->mpls_lse[1]), 0,
1187 		       FIELD_GET(OTX2_FLOWER_MASK_MPLS_TTL,
1188 				 mask->mpls_lse[1]), 0);
1189 	NPC_WRITE_FLOW(NPC_MPLS3_LBTCBOS, mpls_lse,
1190 		       FIELD_GET(OTX2_FLOWER_MASK_MPLS_NON_TTL,
1191 				 pkt->mpls_lse[2]), 0,
1192 		       FIELD_GET(OTX2_FLOWER_MASK_MPLS_NON_TTL,
1193 				 mask->mpls_lse[2]), 0);
1194 	NPC_WRITE_FLOW(NPC_MPLS3_TTL, mpls_lse,
1195 		       FIELD_GET(OTX2_FLOWER_MASK_MPLS_TTL,
1196 				 pkt->mpls_lse[2]), 0,
1197 		       FIELD_GET(OTX2_FLOWER_MASK_MPLS_TTL,
1198 				 mask->mpls_lse[2]), 0);
1199 	NPC_WRITE_FLOW(NPC_MPLS4_LBTCBOS, mpls_lse,
1200 		       FIELD_GET(OTX2_FLOWER_MASK_MPLS_NON_TTL,
1201 				 pkt->mpls_lse[3]), 0,
1202 		       FIELD_GET(OTX2_FLOWER_MASK_MPLS_NON_TTL,
1203 				 mask->mpls_lse[3]), 0);
1204 	NPC_WRITE_FLOW(NPC_MPLS4_TTL, mpls_lse,
1205 		       FIELD_GET(OTX2_FLOWER_MASK_MPLS_TTL,
1206 				 pkt->mpls_lse[3]), 0,
1207 		       FIELD_GET(OTX2_FLOWER_MASK_MPLS_TTL,
1208 				 mask->mpls_lse[3]), 0);
1209 
1210 	NPC_WRITE_FLOW(NPC_IPFRAG_IPV6, next_header, pkt->next_header, 0,
1211 		       mask->next_header, 0);
1212 	npc_update_ipv6_flow(rvu, mdata, features, pkt, mask, output, intf);
1213 	npc_update_vlan_features(rvu, mdata, features, intf);
1214 
1215 	npc_update_field_hash(rvu, intf, mdata, blkaddr, features,
1216 			      pkt, mask, opkt, omask);
1217 }
1218 
rvu_mcam_find_rule(struct npc_mcam * mcam,u16 entry)1219 static struct rvu_npc_mcam_rule *rvu_mcam_find_rule(struct npc_mcam *mcam, u16 entry)
1220 {
1221 	struct rvu_npc_mcam_rule *iter;
1222 
1223 	mutex_lock(&mcam->lock);
1224 	list_for_each_entry(iter, &mcam->mcam_rules, list) {
1225 		if (iter->entry == entry) {
1226 			mutex_unlock(&mcam->lock);
1227 			return iter;
1228 		}
1229 	}
1230 	mutex_unlock(&mcam->lock);
1231 
1232 	return NULL;
1233 }
1234 
rvu_mcam_add_rule(struct npc_mcam * mcam,struct rvu_npc_mcam_rule * rule)1235 static void rvu_mcam_add_rule(struct npc_mcam *mcam,
1236 			      struct rvu_npc_mcam_rule *rule)
1237 {
1238 	struct list_head *head = &mcam->mcam_rules;
1239 	struct rvu_npc_mcam_rule *iter;
1240 
1241 	mutex_lock(&mcam->lock);
1242 	list_for_each_entry(iter, &mcam->mcam_rules, list) {
1243 		if (iter->entry > rule->entry)
1244 			break;
1245 		head = &iter->list;
1246 	}
1247 
1248 	list_add(&rule->list, head);
1249 	mutex_unlock(&mcam->lock);
1250 }
1251 
rvu_mcam_remove_counter_from_rule(struct rvu * rvu,u16 pcifunc,struct rvu_npc_mcam_rule * rule)1252 static void rvu_mcam_remove_counter_from_rule(struct rvu *rvu, u16 pcifunc,
1253 					      struct rvu_npc_mcam_rule *rule)
1254 {
1255 	struct npc_mcam *mcam = &rvu->hw->mcam;
1256 
1257 	/* There is no counter allotted for cn20k */
1258 	if (is_cn20k(rvu->pdev))
1259 		return;
1260 
1261 	mutex_lock(&mcam->lock);
1262 
1263 	__rvu_mcam_remove_counter_from_rule(rvu, pcifunc, rule);
1264 
1265 	mutex_unlock(&mcam->lock);
1266 }
1267 
rvu_mcam_add_counter_to_rule(struct rvu * rvu,u16 pcifunc,struct rvu_npc_mcam_rule * rule,struct npc_install_flow_rsp * rsp)1268 static void rvu_mcam_add_counter_to_rule(struct rvu *rvu, u16 pcifunc,
1269 					 struct rvu_npc_mcam_rule *rule,
1270 					 struct npc_install_flow_rsp *rsp)
1271 {
1272 	struct npc_mcam *mcam = &rvu->hw->mcam;
1273 
1274 	mutex_lock(&mcam->lock);
1275 
1276 	__rvu_mcam_add_counter_to_rule(rvu, pcifunc, rule, rsp);
1277 
1278 	mutex_unlock(&mcam->lock);
1279 }
1280 
npc_mcast_update_action_index(struct rvu * rvu,struct npc_install_flow_req * req,u64 op,void * action)1281 static int npc_mcast_update_action_index(struct rvu *rvu, struct npc_install_flow_req *req,
1282 					 u64 op, void *action)
1283 {
1284 	int mce_index;
1285 
1286 	/* If a PF/VF is installing a multicast rule then it is expected
1287 	 * that the PF/VF should have created a group for the multicast/mirror
1288 	 * list. Otherwise reject the configuration.
1289 	 * During this scenario, req->index is set as multicast/mirror
1290 	 * group index.
1291 	 */
1292 	if (req->hdr.pcifunc &&
1293 	    (op == NIX_RX_ACTIONOP_MCAST || op == NIX_TX_ACTIONOP_MCAST)) {
1294 		mce_index = rvu_nix_mcast_get_mce_index(rvu, req->hdr.pcifunc, req->index);
1295 		if (mce_index < 0)
1296 			return mce_index;
1297 
1298 		if (op == NIX_RX_ACTIONOP_MCAST)
1299 			((struct nix_rx_action *)action)->index = mce_index;
1300 		else
1301 			((struct nix_tx_action *)action)->index = mce_index;
1302 	}
1303 
1304 	return 0;
1305 }
1306 
1307 void
npc_populate_mcam_mdata(struct rvu * rvu,struct mcam_entry_mdata * mdata,struct cn20k_mcam_entry * cn20k_entry,struct mcam_entry * entry)1308 npc_populate_mcam_mdata(struct rvu *rvu,
1309 			struct mcam_entry_mdata *mdata,
1310 			struct cn20k_mcam_entry *cn20k_entry,
1311 			struct mcam_entry *entry)
1312 {
1313 	if (is_cn20k(rvu->pdev)) {
1314 		mdata->kw = cn20k_entry->kw;
1315 		mdata->kw_mask = cn20k_entry->kw_mask;
1316 		mdata->action = &cn20k_entry->action;
1317 		mdata->vtag_action = &cn20k_entry->vtag_action;
1318 		mdata->max_kw = NPC_KWS_IN_KEY_SZ_8;
1319 		return;
1320 	}
1321 	mdata->kw = entry->kw;
1322 	mdata->kw_mask = entry->kw_mask;
1323 	mdata->action = &entry->action;
1324 	mdata->vtag_action = &entry->vtag_action;
1325 	mdata->max_kw = NPC_KWS_IN_KEY_SZ_7;
1326 }
1327 
npc_update_rx_entry(struct rvu * rvu,struct rvu_pfvf * pfvf,struct mcam_entry_mdata * mdata,struct npc_install_flow_req * req,u16 target,bool pf_set_vfs_mac)1328 static int npc_update_rx_entry(struct rvu *rvu, struct rvu_pfvf *pfvf,
1329 			       struct mcam_entry_mdata *mdata,
1330 			       struct npc_install_flow_req *req,
1331 			       u16 target, bool pf_set_vfs_mac)
1332 {
1333 	struct rvu_switch *rswitch = &rvu->rswitch;
1334 	struct nix_rx_action action;
1335 	int ret;
1336 
1337 	if (rswitch->mode == DEVLINK_ESWITCH_MODE_SWITCHDEV && pf_set_vfs_mac)
1338 		req->chan_mask = 0x0; /* Do not care channel */
1339 
1340 	npc_update_entry(rvu, NPC_CHAN, mdata, req->channel, 0, req->chan_mask,
1341 			 0, NIX_INTF_RX);
1342 
1343 	*(u64 *)&action = 0x00;
1344 	action.pf_func = target;
1345 	action.op = req->op;
1346 	action.index = req->index;
1347 
1348 	ret = npc_mcast_update_action_index(rvu, req, action.op, (void *)&action);
1349 	if (ret)
1350 		return ret;
1351 
1352 	action.match_id = req->match_id;
1353 	action.flow_key_alg = req->flow_key_alg;
1354 
1355 	if (req->op == NIX_RX_ACTION_DEFAULT) {
1356 		if (pfvf->def_ucast_rule) {
1357 			action = pfvf->def_ucast_rule->rx_action;
1358 		} else {
1359 			/* For profiles which do not extract DMAC, the default
1360 			 * unicast entry is unused. Hence modify action for the
1361 			 * requests which use same action as default unicast
1362 			 * entry
1363 			 */
1364 			*(u64 *)&action = 0;
1365 			action.pf_func = target;
1366 			action.op = NIX_RX_ACTIONOP_UCAST;
1367 		}
1368 		if (req->match_id)
1369 			action.match_id = req->match_id;
1370 	}
1371 
1372 	*mdata->action = *(u64 *)&action;
1373 
1374 	/* VTAG0 starts at 0th byte of LID_B.
1375 	 * VTAG1 starts at 4th byte of LID_B.
1376 	 */
1377 	*mdata->vtag_action = FIELD_PREP(RX_VTAG0_VALID_BIT, req->vtag0_valid) |
1378 			     FIELD_PREP(RX_VTAG0_TYPE_MASK, req->vtag0_type) |
1379 			     FIELD_PREP(RX_VTAG0_LID_MASK, NPC_LID_LB) |
1380 			     FIELD_PREP(RX_VTAG0_RELPTR_MASK, 0) |
1381 			     FIELD_PREP(RX_VTAG1_VALID_BIT, req->vtag1_valid) |
1382 			     FIELD_PREP(RX_VTAG1_TYPE_MASK, req->vtag1_type) |
1383 			     FIELD_PREP(RX_VTAG1_LID_MASK, NPC_LID_LB) |
1384 			     FIELD_PREP(RX_VTAG1_RELPTR_MASK, 4);
1385 
1386 	return 0;
1387 }
1388 
npc_update_tx_entry(struct rvu * rvu,struct rvu_pfvf * pfvf,struct mcam_entry_mdata * mdata,struct npc_install_flow_req * req,u16 target)1389 static int npc_update_tx_entry(struct rvu *rvu, struct rvu_pfvf *pfvf,
1390 			       struct mcam_entry_mdata *mdata,
1391 			       struct npc_install_flow_req *req, u16 target)
1392 {
1393 	struct nix_tx_action action;
1394 	u64 mask = ~0ULL;
1395 	int ret;
1396 
1397 	/* If AF is installing then do not care about
1398 	 * PF_FUNC in Send Descriptor
1399 	 */
1400 	if (is_pffunc_af(req->hdr.pcifunc))
1401 		mask = 0;
1402 
1403 	npc_update_entry(rvu, NPC_PF_FUNC, mdata, (__force u16)htons(target),
1404 			 0, mask, 0, NIX_INTF_TX);
1405 
1406 	*(u64 *)&action = 0x00;
1407 	action.op = req->op;
1408 	action.index = req->index;
1409 
1410 	ret = npc_mcast_update_action_index(rvu, req, action.op, (void *)&action);
1411 	if (ret)
1412 		return ret;
1413 
1414 	action.match_id = req->match_id;
1415 
1416 	*mdata->action = *(u64 *)&action;
1417 
1418 	/* VTAG0 starts at 0th byte of LID_B.
1419 	 * VTAG1 starts at 4th byte of LID_B.
1420 	 */
1421 	*mdata->vtag_action = FIELD_PREP(TX_VTAG0_DEF_MASK, req->vtag0_def) |
1422 			     FIELD_PREP(TX_VTAG0_OP_MASK, req->vtag0_op) |
1423 			     FIELD_PREP(TX_VTAG0_LID_MASK, NPC_LID_LA) |
1424 			     FIELD_PREP(TX_VTAG0_RELPTR_MASK, 20) |
1425 			     FIELD_PREP(TX_VTAG1_DEF_MASK, req->vtag1_def) |
1426 			     FIELD_PREP(TX_VTAG1_OP_MASK, req->vtag1_op) |
1427 			     FIELD_PREP(TX_VTAG1_LID_MASK, NPC_LID_LA) |
1428 			     FIELD_PREP(TX_VTAG1_RELPTR_MASK, 24);
1429 
1430 	return 0;
1431 }
1432 
npc_install_flow(struct rvu * rvu,int blkaddr,u16 target,int nixlf,struct rvu_pfvf * pfvf,struct npc_install_flow_req * req,struct npc_install_flow_rsp * rsp,bool enable,bool pf_set_vfs_mac)1433 static int npc_install_flow(struct rvu *rvu, int blkaddr, u16 target,
1434 			    int nixlf, struct rvu_pfvf *pfvf,
1435 			    struct npc_install_flow_req *req,
1436 			    struct npc_install_flow_rsp *rsp, bool enable,
1437 			    bool pf_set_vfs_mac)
1438 {
1439 	struct rvu_npc_mcam_rule *def_ucast_rule = pfvf->def_ucast_rule;
1440 	struct npc_cn20k_mcam_write_entry_req cn20k_wreq = { 0 };
1441 	u64 features, installed_features, missing_features = 0;
1442 	struct npc_mcam_write_entry_req write_req = { 0 };
1443 	struct npc_mcam *mcam = &rvu->hw->mcam;
1444 	struct cn20k_mcam_entry *cn20k_entry;
1445 	struct mcam_entry_mdata mdata = { };
1446 	struct rvu_npc_mcam_rule dummy = { 0 };
1447 	struct rvu_npc_mcam_rule *rule;
1448 	u16 owner = req->hdr.pcifunc;
1449 	struct msg_rsp write_rsp;
1450 	struct mcam_entry *entry;
1451 	bool new = false;
1452 	int entry_index;
1453 	int err;
1454 
1455 	installed_features = req->features;
1456 	features = req->features;
1457 	entry_index = req->entry;
1458 
1459 	cn20k_entry = &cn20k_wreq.entry_data;
1460 	entry = &write_req.entry_data;
1461 
1462 	npc_populate_mcam_mdata(rvu, &mdata, cn20k_entry, entry);
1463 
1464 	npc_update_flow(rvu, &mdata, features, &req->packet, &req->mask, &dummy,
1465 			req->intf, blkaddr);
1466 
1467 	if (is_npc_intf_rx(req->intf)) {
1468 		err = npc_update_rx_entry(rvu, pfvf, &mdata, req, target,
1469 					  pf_set_vfs_mac);
1470 		if (err)
1471 			return err;
1472 	} else {
1473 		err = npc_update_tx_entry(rvu, pfvf, &mdata, req, target);
1474 		if (err)
1475 			return err;
1476 	}
1477 
1478 	/* Default unicast rules do not exist for TX */
1479 	if (is_npc_intf_tx(req->intf))
1480 		goto find_rule;
1481 
1482 	if (req->default_rule) {
1483 		entry_index = npc_get_nixlf_mcam_index(mcam, target, nixlf,
1484 						       NIXLF_UCAST_ENTRY);
1485 
1486 		if (entry_index < 0) {
1487 			dev_err(rvu->dev,
1488 				"%s: Error to get ucast entry for target=%#x\n",
1489 				__func__, target);
1490 			return -EINVAL;
1491 		}
1492 
1493 		enable = is_mcam_entry_enabled(rvu, mcam, blkaddr, entry_index);
1494 	}
1495 
1496 	/* update mcam entry with default unicast rule attributes */
1497 	if (def_ucast_rule && (req->default_rule && req->append)) {
1498 		missing_features = (def_ucast_rule->features ^ features) &
1499 					def_ucast_rule->features;
1500 		if (missing_features)
1501 			npc_update_flow(rvu, &mdata, missing_features,
1502 					&def_ucast_rule->packet,
1503 					&def_ucast_rule->mask,
1504 					&dummy, req->intf,
1505 					blkaddr);
1506 		installed_features = req->features | missing_features;
1507 	}
1508 
1509 find_rule:
1510 	rule = rvu_mcam_find_rule(mcam, entry_index);
1511 	if (!rule) {
1512 		rule = kzalloc_obj(*rule);
1513 		if (!rule)
1514 			return -ENOMEM;
1515 		new = true;
1516 	}
1517 
1518 	if (!is_cn20k(rvu->pdev)) {
1519 		write_req.hdr.pcifunc = owner;
1520 
1521 		/* allocate new counter if rule has no counter */
1522 		if (!req->default_rule && req->set_cntr && !rule->has_cntr)
1523 			rvu_mcam_add_counter_to_rule(rvu, owner, rule, rsp);
1524 
1525 		/* if user wants to delete an existing counter for a rule then
1526 		 * free the counter
1527 		 */
1528 		if (!req->set_cntr && rule->has_cntr)
1529 			rvu_mcam_remove_counter_from_rule(rvu, owner, rule);
1530 
1531 		/* AF owns the default rules so change the owner just to relax
1532 		 * the checks in rvu_mbox_handler_npc_mcam_write_entry
1533 		 */
1534 		if (req->default_rule)
1535 			write_req.hdr.pcifunc = 0;
1536 
1537 		write_req.entry = entry_index;
1538 		write_req.intf = req->intf;
1539 		write_req.enable_entry = (u8)enable;
1540 		/* if counter is available then clear and use it */
1541 		if (req->set_cntr && rule->has_cntr) {
1542 			rvu_write64(rvu, blkaddr,
1543 				    NPC_AF_MATCH_STATX(rule->cntr),
1544 				    req->cntr_val);
1545 			write_req.set_cntr = 1;
1546 			write_req.cntr = rule->cntr;
1547 		}
1548 		goto update_rule;
1549 	}
1550 
1551 	cn20k_wreq.hdr.pcifunc = owner;
1552 
1553 	if (req->default_rule)
1554 		cn20k_wreq.hdr.pcifunc = 0;
1555 
1556 	cn20k_wreq.entry = entry_index;
1557 	cn20k_wreq.intf = req->intf;
1558 	cn20k_wreq.enable_entry = (u8)enable;
1559 	cn20k_wreq.hw_prio = req->hw_prio;
1560 	cn20k_wreq.req_kw_type = req->req_kw_type;
1561 
1562 update_rule:
1563 
1564 	/* update rule */
1565 	memcpy(&rule->packet, &dummy.packet, sizeof(rule->packet));
1566 	memcpy(&rule->mask, &dummy.mask, sizeof(rule->mask));
1567 	rule->entry = entry_index;
1568 	if (is_cn20k(rvu->pdev)) {
1569 		memcpy(&rule->rx_action, &cn20k_entry->action,
1570 		       sizeof(struct nix_rx_action));
1571 		if (is_npc_intf_tx(req->intf))
1572 			memcpy(&rule->tx_action, &cn20k_entry->action,
1573 			       sizeof(struct nix_tx_action));
1574 		rule->vtag_action = cn20k_entry->vtag_action;
1575 	} else {
1576 		memcpy(&rule->rx_action, &entry->action,
1577 		       sizeof(struct nix_rx_action));
1578 		if (is_npc_intf_tx(req->intf))
1579 			memcpy(&rule->tx_action, &entry->action,
1580 			       sizeof(struct nix_tx_action));
1581 		rule->vtag_action = entry->vtag_action;
1582 	}
1583 
1584 	rule->features = installed_features;
1585 	rule->default_rule = req->default_rule;
1586 	rule->owner = owner;
1587 	rule->enable = enable;
1588 
1589 	if (is_cn20k(rvu->pdev)) {
1590 		rule->chan_mask = cn20k_wreq.entry_data.kw_mask[0] &
1591 						NPC_KEX_CHAN_MASK;
1592 		rule->chan = cn20k_wreq.entry_data.kw[0] &
1593 					NPC_KEX_CHAN_MASK;
1594 	} else {
1595 		rule->chan_mask = write_req.entry_data.kw_mask[0] &
1596 						NPC_KEX_CHAN_MASK;
1597 		rule->chan = write_req.entry_data.kw[0] & NPC_KEX_CHAN_MASK;
1598 	}
1599 
1600 	rule->chan &= rule->chan_mask;
1601 	rule->lxmb = dummy.lxmb;
1602 	rule->hw_prio = req->hw_prio;
1603 	if (is_npc_intf_tx(req->intf))
1604 		rule->intf = pfvf->nix_tx_intf;
1605 	else
1606 		rule->intf = pfvf->nix_rx_intf;
1607 
1608 	if (new)
1609 		rvu_mcam_add_rule(mcam, rule);
1610 	if (req->default_rule)
1611 		pfvf->def_ucast_rule = rule;
1612 
1613 	/* write to mcam entry registers */
1614 	if (is_cn20k(rvu->pdev))
1615 		err = rvu_mbox_handler_npc_cn20k_mcam_write_entry(rvu,
1616 								  &cn20k_wreq,
1617 								  &write_rsp);
1618 	else
1619 		err = rvu_mbox_handler_npc_mcam_write_entry(rvu, &write_req,
1620 							    &write_rsp);
1621 
1622 	if (err) {
1623 		rvu_mcam_remove_counter_from_rule(rvu, owner, rule);
1624 		if (new) {
1625 			list_del(&rule->list);
1626 			kfree(rule);
1627 		}
1628 		return err;
1629 	}
1630 
1631 	/* VF's MAC address is being changed via PF  */
1632 	if (pf_set_vfs_mac) {
1633 		ether_addr_copy(pfvf->default_mac, req->packet.dmac);
1634 		ether_addr_copy(pfvf->mac_addr, req->packet.dmac);
1635 		set_bit(PF_SET_VF_MAC, &pfvf->flags);
1636 	}
1637 
1638 	if (test_bit(PF_SET_VF_CFG, &pfvf->flags) &&
1639 	    req->vtag0_type == NIX_AF_LFX_RX_VTAG_TYPE7)
1640 		rule->vfvlan_cfg = true;
1641 
1642 	if (is_npc_intf_rx(req->intf) && req->match_id &&
1643 	    (req->op == NIX_RX_ACTIONOP_UCAST || req->op == NIX_RX_ACTIONOP_RSS))
1644 		return rvu_nix_setup_ratelimit_aggr(rvu, req->hdr.pcifunc,
1645 					     req->index, req->match_id);
1646 
1647 	if (owner && req->op == NIX_RX_ACTIONOP_MCAST)
1648 		return rvu_nix_mcast_update_mcam_entry(rvu, req->hdr.pcifunc,
1649 						       req->index, entry_index);
1650 
1651 	return 0;
1652 }
1653 
1654 static int
rvu_npc_free_entry_for_flow_install(struct rvu * rvu,u16 pcifunc,bool free_entry,int mcam_idx)1655 rvu_npc_free_entry_for_flow_install(struct rvu *rvu, u16 pcifunc,
1656 				    bool free_entry, int mcam_idx)
1657 {
1658 	struct npc_mcam_free_entry_req free_req = { 0 };
1659 	struct msg_rsp rsp;
1660 	int rc;
1661 
1662 	if (!free_entry)
1663 		return 0;
1664 
1665 	free_req.hdr.pcifunc = pcifunc;
1666 	free_req.entry = mcam_idx;
1667 	rc = rvu_mbox_handler_npc_mcam_free_entry(rvu, &free_req, &rsp);
1668 	return rc;
1669 }
1670 
1671 static int
rvu_npc_alloc_entry_for_flow_install(struct rvu * rvu,struct npc_install_flow_req * fl_req,u16 * mcam_idx,u8 * kw_type,bool * allocated)1672 rvu_npc_alloc_entry_for_flow_install(struct rvu *rvu,
1673 				     struct npc_install_flow_req *fl_req,
1674 				     u16 *mcam_idx, u8 *kw_type,
1675 				     bool *allocated)
1676 {
1677 	struct npc_mcam_alloc_entry_req entry_req;
1678 	struct npc_mcam_alloc_entry_rsp entry_rsp;
1679 	struct npc_get_pfl_info_rsp rsp = { 0 };
1680 	struct npc_get_num_kws_req kws_req;
1681 	struct npc_get_num_kws_rsp kws_rsp;
1682 	int off, kw_bits, rc;
1683 	struct msg_req req;
1684 	u8 *src, *dst;
1685 
1686 	if (!is_cn20k(rvu->pdev)) {
1687 		*kw_type = -1;
1688 		return 0;
1689 	}
1690 
1691 	if (!fl_req->alloc_entry) {
1692 		*kw_type = -1;
1693 		return 0;
1694 	}
1695 
1696 	off = offsetof(struct npc_install_flow_req, packet);
1697 	dst = (u8 *)&kws_req.fl + off;
1698 	src = (u8 *)fl_req + off;
1699 	memcpy(dst, src, sizeof(struct npc_install_flow_req) - off);
1700 	rc = rvu_mbox_handler_npc_get_num_kws(rvu, &kws_req, &kws_rsp);
1701 	if (rc)
1702 		return rc;
1703 
1704 	kw_bits = kws_rsp.kws * 64;
1705 
1706 	*kw_type = NPC_MCAM_KEY_X2;
1707 	if (kw_bits > 256) {
1708 		rvu_mbox_handler_npc_get_pfl_info(rvu, &req, &rsp);
1709 		if (rsp.kw_type == NPC_MCAM_KEY_X2) {
1710 			dev_err(rvu->dev,
1711 				"Only X2 entries are supported in X2 profile\n");
1712 			return -EOPNOTSUPP;
1713 		}
1714 
1715 		*kw_type = NPC_MCAM_KEY_X4;
1716 	}
1717 
1718 	memset(&entry_req, 0, sizeof(entry_req));
1719 	memset(&entry_rsp, 0, sizeof(entry_rsp));
1720 
1721 	entry_req.hdr.pcifunc = fl_req->hdr.pcifunc;
1722 	entry_req.ref_prio = fl_req->ref_prio;
1723 	entry_req.ref_entry = fl_req->ref_entry;
1724 	entry_req.kw_type = *kw_type;
1725 	entry_req.count = 1;
1726 	rc = rvu_mbox_handler_npc_mcam_alloc_entry(rvu,
1727 						   &entry_req,
1728 						   &entry_rsp);
1729 	if (rc)
1730 		return rc;
1731 
1732 	*mcam_idx = entry_rsp.entry_list[0];
1733 	*allocated = true;
1734 	return 0;
1735 }
1736 
rvu_mbox_handler_npc_install_flow(struct rvu * rvu,struct npc_install_flow_req * req,struct npc_install_flow_rsp * rsp)1737 int rvu_mbox_handler_npc_install_flow(struct rvu *rvu,
1738 				      struct npc_install_flow_req *req,
1739 				      struct npc_install_flow_rsp *rsp)
1740 {
1741 	bool from_vf = !!(req->hdr.pcifunc & RVU_PFVF_FUNC_MASK);
1742 	bool from_rep_dev = !!is_rep_dev(rvu, req->hdr.pcifunc);
1743 	struct rvu_switch *rswitch = &rvu->rswitch;
1744 	int blkaddr, nixlf, err;
1745 	struct rvu_pfvf *pfvf;
1746 	bool pf_set_vfs_mac = false;
1747 	bool allocated = false;
1748 	bool enable = true;
1749 	u8 kw_type;
1750 	u16 target;
1751 
1752 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1753 	if (blkaddr < 0) {
1754 		dev_err(rvu->dev, "%s: NPC block not implemented\n", __func__);
1755 		return NPC_MCAM_INVALID_REQ;
1756 	}
1757 
1758 	if (!is_npc_interface_valid(rvu, req->intf))
1759 		return NPC_FLOW_INTF_INVALID;
1760 
1761 	err = rvu_npc_alloc_entry_for_flow_install(rvu, req, &req->entry,
1762 						   &kw_type, &allocated);
1763 	if (err) {
1764 		dev_err(rvu->dev,
1765 			"%s: Error to alloc mcam entry for pcifunc=%#x\n",
1766 			__func__, req->hdr.pcifunc);
1767 		return err;
1768 	}
1769 
1770 	req->entry = npc_cn20k_vidx2idx(req->entry);
1771 
1772 	/* If DMAC is not extracted in MKEX, rules installed by AF
1773 	 * can rely on L2MB bit set by hardware protocol checker for
1774 	 * broadcast and multicast addresses.
1775 	 */
1776 	if (npc_check_field(rvu, blkaddr, NPC_DMAC, req->intf))
1777 		goto process_flow;
1778 
1779 	if (is_pffunc_af(req->hdr.pcifunc) &&
1780 	    req->features & BIT_ULL(NPC_DMAC)) {
1781 		if (is_unicast_ether_addr(req->packet.dmac)) {
1782 			dev_warn(rvu->dev,
1783 				 "%s: mkex profile does not support ucast flow\n",
1784 				 __func__);
1785 			rvu_npc_free_entry_for_flow_install(rvu,
1786 							    req->hdr.pcifunc,
1787 							    allocated,
1788 							    req->entry);
1789 			return NPC_FLOW_NOT_SUPPORTED;
1790 		}
1791 
1792 		if (!npc_is_field_present(rvu, NPC_LXMB, req->intf)) {
1793 			dev_warn(rvu->dev,
1794 				 "%s: mkex profile does not support bcast/mcast flow",
1795 				 __func__);
1796 			rvu_npc_free_entry_for_flow_install(rvu,
1797 							    req->hdr.pcifunc,
1798 							    allocated,
1799 							    req->entry);
1800 			return NPC_FLOW_NOT_SUPPORTED;
1801 		}
1802 
1803 		/* Modify feature to use LXMB instead of DMAC */
1804 		req->features &= ~BIT_ULL(NPC_DMAC);
1805 		req->features |= BIT_ULL(NPC_LXMB);
1806 	}
1807 
1808 process_flow:
1809 	if (from_vf && req->default_rule) {
1810 		rvu_npc_free_entry_for_flow_install(rvu, req->hdr.pcifunc,
1811 						    allocated, req->entry);
1812 		return NPC_FLOW_VF_PERM_DENIED;
1813 	}
1814 
1815 	/* Each PF/VF info is maintained in struct rvu_pfvf.
1816 	 * rvu_pfvf for the target PF/VF needs to be retrieved
1817 	 * hence modify pcifunc accordingly.
1818 	 */
1819 
1820 	if (!req->hdr.pcifunc) {
1821 		/* AF installing for a PF/VF */
1822 		target = req->vf;
1823 	} else if (!from_vf && req->vf && !from_rep_dev) {
1824 		/* PF installing for its VF */
1825 		target = (req->hdr.pcifunc & ~RVU_PFVF_FUNC_MASK) | req->vf;
1826 		pf_set_vfs_mac = req->default_rule &&
1827 				(req->features & BIT_ULL(NPC_DMAC));
1828 	} else if (from_rep_dev && req->vf) {
1829 		/* Representor device installing for a representee */
1830 		target = req->vf;
1831 	} else {
1832 		/* msg received from PF/VF */
1833 		target = req->hdr.pcifunc;
1834 	}
1835 
1836 	/* ignore chan_mask in case pf func is not AF, revisit later */
1837 	if (!is_pffunc_af(req->hdr.pcifunc))
1838 		req->chan_mask = rvu_get_cpt_chan_mask(rvu);
1839 
1840 	err = npc_check_unsupported_flows(rvu, req->features, req->intf);
1841 	if (err) {
1842 		rvu_npc_free_entry_for_flow_install(rvu, req->hdr.pcifunc,
1843 						    allocated, req->entry);
1844 		return NPC_FLOW_NOT_SUPPORTED;
1845 	}
1846 
1847 	pfvf = rvu_get_pfvf(rvu, target);
1848 
1849 	if (from_rep_dev)
1850 		req->channel = pfvf->rx_chan_base;
1851 	/* PF installing for its VF */
1852 	if (req->hdr.pcifunc && !from_vf && req->vf && !from_rep_dev)
1853 		set_bit(PF_SET_VF_CFG, &pfvf->flags);
1854 
1855 	/* update req destination mac addr */
1856 	if ((req->features & BIT_ULL(NPC_DMAC)) && is_npc_intf_rx(req->intf) &&
1857 	    is_zero_ether_addr(req->packet.dmac)) {
1858 		ether_addr_copy(req->packet.dmac, pfvf->mac_addr);
1859 		eth_broadcast_addr((u8 *)&req->mask.dmac);
1860 	}
1861 
1862 	/* Proceed if NIXLF is attached or not for TX rules */
1863 	err = nix_get_nixlf(rvu, target, &nixlf, NULL);
1864 	if (err && is_npc_intf_rx(req->intf) && !pf_set_vfs_mac) {
1865 		rvu_npc_free_entry_for_flow_install(rvu, req->hdr.pcifunc,
1866 						    allocated, req->entry);
1867 		return NPC_FLOW_NO_NIXLF;
1868 	}
1869 
1870 	/* don't enable rule when nixlf not attached or initialized */
1871 	if (!(is_nixlf_attached(rvu, target) &&
1872 	      test_bit(NIXLF_INITIALIZED, &pfvf->flags)))
1873 		enable = false;
1874 
1875 	/* Packets reaching NPC in Tx path implies that a
1876 	 * NIXLF is properly setup and transmitting.
1877 	 * Hence rules can be enabled for Tx.
1878 	 */
1879 	if (is_npc_intf_tx(req->intf))
1880 		enable = true;
1881 
1882 	/* Do not allow requests from uninitialized VFs */
1883 	if (from_vf && !enable) {
1884 		rvu_npc_free_entry_for_flow_install(rvu, req->hdr.pcifunc,
1885 						    allocated, req->entry);
1886 		return NPC_FLOW_VF_NOT_INIT;
1887 	}
1888 
1889 	/* PF sets VF mac & VF NIXLF is not attached, update the mac addr */
1890 	if (pf_set_vfs_mac && !enable) {
1891 		ether_addr_copy(pfvf->default_mac, req->packet.dmac);
1892 		ether_addr_copy(pfvf->mac_addr, req->packet.dmac);
1893 		set_bit(PF_SET_VF_MAC, &pfvf->flags);
1894 		rvu_npc_free_entry_for_flow_install(rvu, req->hdr.pcifunc,
1895 						    allocated, req->entry);
1896 		return 0;
1897 	}
1898 
1899 	mutex_lock(&rswitch->switch_lock);
1900 	err = npc_install_flow(rvu, blkaddr, target, nixlf, pfvf,
1901 			       req, rsp, enable, pf_set_vfs_mac);
1902 	if (err)
1903 		rvu_npc_free_entry_for_flow_install(rvu, req->hdr.pcifunc,
1904 						    allocated, req->entry);
1905 
1906 	rsp->kw_type = kw_type;
1907 	rsp->entry = req->entry;
1908 	mutex_unlock(&rswitch->switch_lock);
1909 
1910 	return err;
1911 }
1912 
npc_delete_flow(struct rvu * rvu,struct rvu_npc_mcam_rule * rule,u16 pcifunc)1913 static int npc_delete_flow(struct rvu *rvu, struct rvu_npc_mcam_rule *rule,
1914 			   u16 pcifunc)
1915 {
1916 	struct npc_mcam_ena_dis_entry_req dis_req = { 0 };
1917 	struct msg_rsp dis_rsp;
1918 
1919 	if (rule->default_rule)
1920 		return 0;
1921 
1922 	if (rule->has_cntr)
1923 		rvu_mcam_remove_counter_from_rule(rvu, pcifunc, rule);
1924 
1925 	dis_req.hdr.pcifunc = pcifunc;
1926 	dis_req.entry = rule->entry;
1927 
1928 	list_del(&rule->list);
1929 	kfree(rule);
1930 
1931 	return rvu_mbox_handler_npc_mcam_dis_entry(rvu, &dis_req, &dis_rsp);
1932 }
1933 
rvu_mbox_handler_npc_delete_flow(struct rvu * rvu,struct npc_delete_flow_req * req,struct npc_delete_flow_rsp * rsp)1934 int rvu_mbox_handler_npc_delete_flow(struct rvu *rvu,
1935 				     struct npc_delete_flow_req *req,
1936 				     struct npc_delete_flow_rsp *rsp)
1937 {
1938 	struct npc_mcam *mcam = &rvu->hw->mcam;
1939 	struct rvu_npc_mcam_rule *iter, *tmp;
1940 	u16 pcifunc = req->hdr.pcifunc;
1941 	struct list_head del_list;
1942 	int blkaddr;
1943 
1944 	req->entry = npc_cn20k_vidx2idx(req->entry);
1945 	req->start = npc_cn20k_vidx2idx(req->start);
1946 	req->end = npc_cn20k_vidx2idx(req->end);
1947 
1948 	INIT_LIST_HEAD(&del_list);
1949 
1950 	mutex_lock(&mcam->lock);
1951 	list_for_each_entry_safe(iter, tmp, &mcam->mcam_rules, list) {
1952 		if (iter->owner == pcifunc) {
1953 			/* All rules */
1954 			if (req->all) {
1955 				list_move_tail(&iter->list, &del_list);
1956 			/* Range of rules */
1957 			} else if (req->end && iter->entry >= req->start &&
1958 				   iter->entry <= req->end) {
1959 				list_move_tail(&iter->list, &del_list);
1960 			/* single rule */
1961 			} else if (req->entry == iter->entry) {
1962 				blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
1963 				if (blkaddr)
1964 					rsp->cntr_val = rvu_read64(rvu, blkaddr,
1965 								   NPC_AF_MATCH_STATX(iter->cntr));
1966 				list_move_tail(&iter->list, &del_list);
1967 				break;
1968 			}
1969 		}
1970 	}
1971 	mutex_unlock(&mcam->lock);
1972 
1973 	list_for_each_entry_safe(iter, tmp, &del_list, list) {
1974 		u16 entry = iter->entry;
1975 
1976 		/* clear the mcam entry target pcifunc */
1977 		mcam->entry2target_pffunc[entry] = 0x0;
1978 		if (npc_delete_flow(rvu, iter, pcifunc))
1979 			dev_err(rvu->dev, "rule deletion failed for entry:%u",
1980 				entry);
1981 	}
1982 
1983 	return 0;
1984 }
1985 
npc_update_dmac_value(struct rvu * rvu,int npcblkaddr,struct rvu_npc_mcam_rule * rule,struct rvu_pfvf * pfvf)1986 static int npc_update_dmac_value(struct rvu *rvu, int npcblkaddr,
1987 				 struct rvu_npc_mcam_rule *rule,
1988 				 struct rvu_pfvf *pfvf)
1989 {
1990 	struct npc_cn20k_mcam_write_entry_req cn20k_wreq = { 0 };
1991 	struct npc_mcam_write_entry_req write_req = { 0 };
1992 	struct mcam_entry_mdata mdata = { };
1993 	struct npc_mcam *mcam = &rvu->hw->mcam;
1994 	struct cn20k_mcam_entry *cn20k_entry;
1995 	struct mcam_entry *entry;
1996 	u8 intf, enable, hw_prio;
1997 	struct msg_rsp rsp;
1998 	int err;
1999 
2000 	cn20k_entry = &cn20k_wreq.entry_data;
2001 	entry = &write_req.entry_data;
2002 	npc_populate_mcam_mdata(rvu, &mdata, cn20k_entry, entry);
2003 
2004 	ether_addr_copy(rule->packet.dmac, pfvf->mac_addr);
2005 
2006 	if (is_cn20k(rvu->pdev)) {
2007 		if (npc_cn20k_read_mcam_entry(rvu, npcblkaddr, rule->entry,
2008 					      cn20k_entry, &intf,
2009 					      &enable, &hw_prio))
2010 			return -EINVAL;
2011 	} else {
2012 		npc_read_mcam_entry(rvu, mcam, npcblkaddr, rule->entry,
2013 				    entry, &intf, &enable);
2014 	}
2015 
2016 	npc_update_entry(rvu, NPC_DMAC, &mdata,
2017 			 ether_addr_to_u64(pfvf->mac_addr), 0,
2018 			 0xffffffffffffull, 0, intf);
2019 
2020 	mutex_unlock(&mcam->lock);
2021 	if (is_cn20k(rvu->pdev)) {
2022 		cn20k_wreq.hdr.pcifunc = rule->owner;
2023 		cn20k_wreq.entry = rule->entry;
2024 		cn20k_wreq.intf = pfvf->nix_rx_intf;
2025 		err = rvu_mbox_handler_npc_cn20k_mcam_write_entry(rvu,
2026 								  &cn20k_wreq,
2027 								  &rsp);
2028 	} else {
2029 		write_req.hdr.pcifunc = rule->owner;
2030 		write_req.entry = rule->entry;
2031 		write_req.intf = pfvf->nix_rx_intf;
2032 		err = rvu_mbox_handler_npc_mcam_write_entry(rvu, &write_req,
2033 							    &rsp);
2034 	}
2035 	mutex_lock(&mcam->lock);
2036 
2037 	return err;
2038 }
2039 
npc_mcam_enable_flows(struct rvu * rvu,u16 target)2040 void npc_mcam_enable_flows(struct rvu *rvu, u16 target)
2041 {
2042 	struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, target);
2043 	struct rvu_npc_mcam_rule *def_ucast_rule;
2044 	struct npc_mcam *mcam = &rvu->hw->mcam;
2045 	struct rvu_npc_mcam_rule *rule;
2046 	int blkaddr, bank, index;
2047 	u64 def_action;
2048 
2049 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2050 	if (blkaddr < 0)
2051 		return;
2052 
2053 	def_ucast_rule = pfvf->def_ucast_rule;
2054 
2055 	mutex_lock(&mcam->lock);
2056 	list_for_each_entry(rule, &mcam->mcam_rules, list) {
2057 		if (is_npc_intf_rx(rule->intf) &&
2058 		    rule->rx_action.pf_func == target && !rule->enable) {
2059 			if (rule->default_rule) {
2060 				npc_enable_mcam_entry(rvu, mcam, blkaddr,
2061 						      rule->entry, true);
2062 				rule->enable = true;
2063 				continue;
2064 			}
2065 
2066 			if (rule->vfvlan_cfg) {
2067 				if (npc_update_dmac_value(rvu, blkaddr, rule, pfvf))
2068 					dev_err(rvu->dev,
2069 						"Update dmac failed for %u, target=%#x\n",
2070 						rule->entry, target);
2071 			}
2072 
2073 			if (rule->rx_action.op == NIX_RX_ACTION_DEFAULT) {
2074 				if (!def_ucast_rule)
2075 					continue;
2076 				/* Use default unicast entry action */
2077 				rule->rx_action = def_ucast_rule->rx_action;
2078 				def_action = *(u64 *)&def_ucast_rule->rx_action;
2079 				bank = npc_get_bank(mcam, rule->entry);
2080 				rvu_write64(rvu, blkaddr,
2081 					    NPC_AF_MCAMEX_BANKX_ACTION
2082 					    (rule->entry, bank), def_action);
2083 			}
2084 
2085 			npc_enable_mcam_entry(rvu, mcam, blkaddr,
2086 					      rule->entry, true);
2087 			rule->enable = true;
2088 		}
2089 	}
2090 
2091 	/* Enable MCAM entries installed by PF with target as VF pcifunc */
2092 	for (index = 0; index < mcam->bmap_entries; index++) {
2093 		if (mcam->entry2target_pffunc[index] == target)
2094 			npc_enable_mcam_entry(rvu, mcam, blkaddr,
2095 					      index, true);
2096 	}
2097 	mutex_unlock(&mcam->lock);
2098 }
2099 
npc_mcam_disable_flows(struct rvu * rvu,u16 target)2100 void npc_mcam_disable_flows(struct rvu *rvu, u16 target)
2101 {
2102 	struct npc_mcam *mcam = &rvu->hw->mcam;
2103 	int blkaddr, index;
2104 
2105 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2106 	if (blkaddr < 0)
2107 		return;
2108 
2109 	mutex_lock(&mcam->lock);
2110 	/* Disable MCAM entries installed by PF with target as VF pcifunc */
2111 	for (index = 0; index < mcam->bmap_entries; index++) {
2112 		if (mcam->entry2target_pffunc[index] == target)
2113 			npc_enable_mcam_entry(rvu, mcam, blkaddr,
2114 					      index, false);
2115 	}
2116 	mutex_unlock(&mcam->lock);
2117 }
2118 
2119 /* single drop on non hit rule starting from 0th index. This an extension
2120  * to RPM mac filter to support more rules.
2121  */
npc_install_mcam_drop_rule(struct rvu * rvu,int mcam_idx,u16 * counter_idx,u64 chan_val,u64 chan_mask,u64 exact_val,u64 exact_mask,u64 bcast_mcast_val,u64 bcast_mcast_mask)2122 int npc_install_mcam_drop_rule(struct rvu *rvu, int mcam_idx, u16 *counter_idx,
2123 			       u64 chan_val, u64 chan_mask, u64 exact_val, u64 exact_mask,
2124 			       u64 bcast_mcast_val, u64 bcast_mcast_mask)
2125 {
2126 	struct npc_cn20k_mcam_write_entry_req cn20k_req = { 0 };
2127 	struct npc_mcam_alloc_counter_req cntr_req = { 0 };
2128 	struct npc_mcam_alloc_counter_rsp cntr_rsp = { 0 };
2129 	struct npc_mcam_write_entry_req req = { 0 };
2130 	struct npc_mcam *mcam = &rvu->hw->mcam;
2131 	struct mcam_entry_mdata mdata = { };
2132 	struct rvu_npc_mcam_rule *rule;
2133 	struct msg_rsp rsp;
2134 	bool enabled;
2135 	int blkaddr;
2136 	int err;
2137 
2138 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2139 	if (blkaddr < 0) {
2140 		dev_err(rvu->dev, "%s: NPC block not implemented\n", __func__);
2141 		return -ENODEV;
2142 	}
2143 
2144 	/* Bail out if no exact match support */
2145 	if (!rvu_npc_exact_has_match_table(rvu)) {
2146 		dev_info(rvu->dev, "%s: No support for exact match feature\n", __func__);
2147 		return -EINVAL;
2148 	}
2149 
2150 	/* If 0th entry is already used, return err */
2151 	enabled = is_mcam_entry_enabled(rvu, mcam, blkaddr, mcam_idx);
2152 	if (enabled) {
2153 		dev_err(rvu->dev, "%s: failed to add single drop on non hit rule at %d th index\n",
2154 			__func__, mcam_idx);
2155 		return	-EINVAL;
2156 	}
2157 
2158 	/* Add this entry to mcam rules list */
2159 	rule = kzalloc_obj(*rule);
2160 	if (!rule)
2161 		return -ENOMEM;
2162 
2163 	/* Disable rule by default. Enable rule when first dmac filter is
2164 	 * installed
2165 	 */
2166 	rule->enable = false;
2167 	rule->chan = chan_val;
2168 	rule->chan_mask = chan_mask;
2169 	rule->entry = mcam_idx;
2170 	rvu_mcam_add_rule(mcam, rule);
2171 
2172 	/* Reserve slot 0 */
2173 	npc_mcam_rsrcs_reserve(rvu, blkaddr, mcam_idx);
2174 
2175 	if (!is_cn20k(rvu->pdev)) {
2176 		/* Allocate counter for this single drop on non hit rule */
2177 		cntr_req.hdr.pcifunc = 0; /* AF request */
2178 		cntr_req.contig = true;
2179 		cntr_req.count = 1;
2180 		err = rvu_mbox_handler_npc_mcam_alloc_counter(rvu, &cntr_req,
2181 							      &cntr_rsp);
2182 		if (err) {
2183 			dev_err(rvu->dev,
2184 				"%s: Err to allocate cntr for drop rule (err=%d)\n",
2185 				__func__, err);
2186 			return	-EFAULT;
2187 		}
2188 		*counter_idx = cntr_rsp.cntr;
2189 	}
2190 
2191 	npc_populate_mcam_mdata(rvu, &mdata,
2192 				&cn20k_req.entry_data,
2193 				&req.entry_data);
2194 
2195 	/* Fill in fields for this mcam entry */
2196 	npc_update_entry(rvu, NPC_EXACT_RESULT, &mdata, exact_val, 0,
2197 			 exact_mask, 0, NIX_INTF_RX);
2198 	npc_update_entry(rvu, NPC_CHAN, &mdata, chan_val, 0,
2199 			 chan_mask, 0, NIX_INTF_RX);
2200 	npc_update_entry(rvu, NPC_LXMB, &mdata, bcast_mcast_val, 0,
2201 			 bcast_mcast_mask, 0, NIX_INTF_RX);
2202 
2203 	if (is_cn20k(rvu->pdev)) {
2204 		cn20k_req.intf = NIX_INTF_RX;
2205 		cn20k_req.entry = mcam_idx;
2206 
2207 		err = rvu_mbox_handler_npc_cn20k_mcam_write_entry(rvu,
2208 								  &cn20k_req,
2209 								  &rsp);
2210 		if (err) {
2211 			dev_err(rvu->dev,
2212 				"%s: Installation of single drop on non hit rule at %d failed\n",
2213 				__func__, mcam_idx);
2214 			return err;
2215 		}
2216 
2217 		goto enable_entry;
2218 	}
2219 
2220 	req.intf = NIX_INTF_RX;
2221 	req.set_cntr = true;
2222 	req.cntr = cntr_rsp.cntr;
2223 	req.entry = mcam_idx;
2224 
2225 	err = rvu_mbox_handler_npc_mcam_write_entry(rvu, &req, &rsp);
2226 	if (err) {
2227 		dev_err(rvu->dev,
2228 			"%s: Installation of single drop on non hit rule at %d failed\n",
2229 			__func__, mcam_idx);
2230 		return err;
2231 	}
2232 
2233 	dev_dbg(rvu->dev,
2234 		"%s: Installed single drop on non hit rule at %d, cntr=%d\n",
2235 		__func__, mcam_idx, req.cntr);
2236 
2237 enable_entry:
2238 	/* disable entry at Bank 0, index 0 */
2239 	npc_enable_mcam_entry(rvu, mcam, blkaddr, mcam_idx, false);
2240 
2241 	return 0;
2242 }
2243 
rvu_mbox_handler_npc_get_field_status(struct rvu * rvu,struct npc_get_field_status_req * req,struct npc_get_field_status_rsp * rsp)2244 int rvu_mbox_handler_npc_get_field_status(struct rvu *rvu,
2245 					  struct npc_get_field_status_req *req,
2246 					  struct npc_get_field_status_rsp *rsp)
2247 {
2248 	int blkaddr;
2249 
2250 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2251 	if (blkaddr < 0)
2252 		return NPC_MCAM_INVALID_REQ;
2253 
2254 	if (!is_npc_interface_valid(rvu, req->intf))
2255 		return NPC_FLOW_INTF_INVALID;
2256 
2257 	if (npc_check_field(rvu, blkaddr, req->field, req->intf))
2258 		rsp->enable = 1;
2259 
2260 	return 0;
2261 }
2262