1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell RVU Ethernet driver
3  *
4  * Copyright (C) 2020 Marvell.
5  *
6  */
7 
8 #include <net/ipv6.h>
9 #include <linux/sort.h>
10 
11 #include "otx2_common.h"
12 
13 #define OTX2_DEFAULT_ACTION	0x1
14 
15 static int otx2_mcam_entry_init(struct otx2_nic *pfvf);
16 
17 struct otx2_flow {
18 	struct ethtool_rx_flow_spec flow_spec;
19 	struct list_head list;
20 	u32 location;
21 	u32 entry;
22 	bool is_vf;
23 	u8 rss_ctx_id;
24 #define DMAC_FILTER_RULE		BIT(0)
25 #define PFC_FLOWCTRL_RULE		BIT(1)
26 	u16 rule_type;
27 	int vf;
28 };
29 
30 enum dmac_req {
31 	DMAC_ADDR_UPDATE,
32 	DMAC_ADDR_DEL
33 };
34 
35 static void otx2_clear_ntuple_flow_info(struct otx2_nic *pfvf, struct otx2_flow_config *flow_cfg)
36 {
37 	devm_kfree(pfvf->dev, flow_cfg->flow_ent);
38 	flow_cfg->flow_ent = NULL;
39 	flow_cfg->max_flows = 0;
40 }
41 
42 static int otx2_free_ntuple_mcam_entries(struct otx2_nic *pfvf)
43 {
44 	struct otx2_flow_config *flow_cfg = pfvf->flow_cfg;
45 	struct npc_mcam_free_entry_req *req;
46 	int ent, err;
47 
48 	if (!flow_cfg->max_flows)
49 		return 0;
50 
51 	mutex_lock(&pfvf->mbox.lock);
52 	for (ent = 0; ent < flow_cfg->max_flows; ent++) {
53 		req = otx2_mbox_alloc_msg_npc_mcam_free_entry(&pfvf->mbox);
54 		if (!req)
55 			break;
56 
57 		req->entry = flow_cfg->flow_ent[ent];
58 
59 		/* Send message to AF to free MCAM entries */
60 		err = otx2_sync_mbox_msg(&pfvf->mbox);
61 		if (err)
62 			break;
63 	}
64 	mutex_unlock(&pfvf->mbox.lock);
65 	otx2_clear_ntuple_flow_info(pfvf, flow_cfg);
66 	return 0;
67 }
68 
69 static int mcam_entry_cmp(const void *a, const void *b)
70 {
71 	return *(u16 *)a - *(u16 *)b;
72 }
73 
74 int otx2_alloc_mcam_entries(struct otx2_nic *pfvf, u16 count)
75 {
76 	struct otx2_flow_config *flow_cfg = pfvf->flow_cfg;
77 	struct npc_mcam_alloc_entry_req *req;
78 	struct npc_mcam_alloc_entry_rsp *rsp;
79 	int ent, allocated = 0;
80 
81 	/* Free current ones and allocate new ones with requested count */
82 	otx2_free_ntuple_mcam_entries(pfvf);
83 
84 	if (!count)
85 		return 0;
86 
87 	flow_cfg->flow_ent = devm_kmalloc_array(pfvf->dev, count,
88 						sizeof(u16), GFP_KERNEL);
89 	if (!flow_cfg->flow_ent) {
90 		netdev_err(pfvf->netdev,
91 			   "%s: Unable to allocate memory for flow entries\n",
92 			    __func__);
93 		return -ENOMEM;
94 	}
95 
96 	mutex_lock(&pfvf->mbox.lock);
97 
98 	/* In a single request a max of NPC_MAX_NONCONTIG_ENTRIES MCAM entries
99 	 * can only be allocated.
100 	 */
101 	while (allocated < count) {
102 		req = otx2_mbox_alloc_msg_npc_mcam_alloc_entry(&pfvf->mbox);
103 		if (!req)
104 			goto exit;
105 
106 		req->contig = false;
107 		req->count = (count - allocated) > NPC_MAX_NONCONTIG_ENTRIES ?
108 				NPC_MAX_NONCONTIG_ENTRIES : count - allocated;
109 
110 		/* Allocate higher priority entries for PFs, so that VF's entries
111 		 * will be on top of PF.
112 		 */
113 		if (!is_otx2_vf(pfvf->pcifunc)) {
114 			req->priority = NPC_MCAM_HIGHER_PRIO;
115 			req->ref_entry = flow_cfg->def_ent[0];
116 		}
117 
118 		/* Send message to AF */
119 		if (otx2_sync_mbox_msg(&pfvf->mbox))
120 			goto exit;
121 
122 		rsp = (struct npc_mcam_alloc_entry_rsp *)otx2_mbox_get_rsp
123 			(&pfvf->mbox.mbox, 0, &req->hdr);
124 
125 		for (ent = 0; ent < rsp->count; ent++)
126 			flow_cfg->flow_ent[ent + allocated] = rsp->entry_list[ent];
127 
128 		allocated += rsp->count;
129 
130 		/* If this request is not fulfilled, no need to send
131 		 * further requests.
132 		 */
133 		if (rsp->count != req->count)
134 			break;
135 	}
136 
137 	/* Multiple MCAM entry alloc requests could result in non-sequential
138 	 * MCAM entries in the flow_ent[] array. Sort them in an ascending order,
139 	 * otherwise user installed ntuple filter index and MCAM entry index will
140 	 * not be in sync.
141 	 */
142 	if (allocated)
143 		sort(&flow_cfg->flow_ent[0], allocated,
144 		     sizeof(flow_cfg->flow_ent[0]), mcam_entry_cmp, NULL);
145 
146 exit:
147 	mutex_unlock(&pfvf->mbox.lock);
148 
149 	flow_cfg->max_flows = allocated;
150 
151 	if (allocated) {
152 		pfvf->flags |= OTX2_FLAG_MCAM_ENTRIES_ALLOC;
153 		pfvf->flags |= OTX2_FLAG_NTUPLE_SUPPORT;
154 	}
155 
156 	if (allocated != count)
157 		netdev_info(pfvf->netdev,
158 			    "Unable to allocate %d MCAM entries, got only %d\n",
159 			    count, allocated);
160 	return allocated;
161 }
162 EXPORT_SYMBOL(otx2_alloc_mcam_entries);
163 
164 static int otx2_mcam_entry_init(struct otx2_nic *pfvf)
165 {
166 	struct otx2_flow_config *flow_cfg = pfvf->flow_cfg;
167 	struct npc_get_field_status_req *freq;
168 	struct npc_get_field_status_rsp *frsp;
169 	struct npc_mcam_alloc_entry_req *req;
170 	struct npc_mcam_alloc_entry_rsp *rsp;
171 	int vf_vlan_max_flows;
172 	int ent, count;
173 
174 	vf_vlan_max_flows = pfvf->total_vfs * OTX2_PER_VF_VLAN_FLOWS;
175 	count = OTX2_MAX_UNICAST_FLOWS +
176 			OTX2_MAX_VLAN_FLOWS + vf_vlan_max_flows;
177 
178 	flow_cfg->def_ent = devm_kmalloc_array(pfvf->dev, count,
179 					       sizeof(u16), GFP_KERNEL);
180 	if (!flow_cfg->def_ent)
181 		return -ENOMEM;
182 
183 	mutex_lock(&pfvf->mbox.lock);
184 
185 	req = otx2_mbox_alloc_msg_npc_mcam_alloc_entry(&pfvf->mbox);
186 	if (!req) {
187 		mutex_unlock(&pfvf->mbox.lock);
188 		return -ENOMEM;
189 	}
190 
191 	req->contig = false;
192 	req->count = count;
193 
194 	/* Send message to AF */
195 	if (otx2_sync_mbox_msg(&pfvf->mbox)) {
196 		mutex_unlock(&pfvf->mbox.lock);
197 		return -EINVAL;
198 	}
199 
200 	rsp = (struct npc_mcam_alloc_entry_rsp *)otx2_mbox_get_rsp
201 	       (&pfvf->mbox.mbox, 0, &req->hdr);
202 
203 	if (rsp->count != req->count) {
204 		netdev_info(pfvf->netdev,
205 			    "Unable to allocate MCAM entries for ucast, vlan and vf_vlan\n");
206 		mutex_unlock(&pfvf->mbox.lock);
207 		devm_kfree(pfvf->dev, flow_cfg->def_ent);
208 		return 0;
209 	}
210 
211 	for (ent = 0; ent < rsp->count; ent++)
212 		flow_cfg->def_ent[ent] = rsp->entry_list[ent];
213 
214 	flow_cfg->vf_vlan_offset = 0;
215 	flow_cfg->unicast_offset = vf_vlan_max_flows;
216 	flow_cfg->rx_vlan_offset = flow_cfg->unicast_offset +
217 					OTX2_MAX_UNICAST_FLOWS;
218 	pfvf->flags |= OTX2_FLAG_UCAST_FLTR_SUPPORT;
219 
220 	/* Check if NPC_DMAC field is supported
221 	 * by the mkex profile before setting VLAN support flag.
222 	 */
223 	freq = otx2_mbox_alloc_msg_npc_get_field_status(&pfvf->mbox);
224 	if (!freq) {
225 		mutex_unlock(&pfvf->mbox.lock);
226 		return -ENOMEM;
227 	}
228 
229 	freq->field = NPC_DMAC;
230 	if (otx2_sync_mbox_msg(&pfvf->mbox)) {
231 		mutex_unlock(&pfvf->mbox.lock);
232 		return -EINVAL;
233 	}
234 
235 	frsp = (struct npc_get_field_status_rsp *)otx2_mbox_get_rsp
236 	       (&pfvf->mbox.mbox, 0, &freq->hdr);
237 
238 	if (frsp->enable) {
239 		pfvf->flags |= OTX2_FLAG_RX_VLAN_SUPPORT;
240 		pfvf->flags |= OTX2_FLAG_VF_VLAN_SUPPORT;
241 	}
242 
243 	pfvf->flags |= OTX2_FLAG_MCAM_ENTRIES_ALLOC;
244 	mutex_unlock(&pfvf->mbox.lock);
245 
246 	/* Allocate entries for Ntuple filters */
247 	count = otx2_alloc_mcam_entries(pfvf, OTX2_DEFAULT_FLOWCOUNT);
248 	if (count <= 0) {
249 		otx2_clear_ntuple_flow_info(pfvf, flow_cfg);
250 		return 0;
251 	}
252 
253 	pfvf->flags |= OTX2_FLAG_TC_FLOWER_SUPPORT;
254 
255 	return 0;
256 }
257 
258 /* TODO : revisit on size */
259 #define OTX2_DMAC_FLTR_BITMAP_SZ (4 * 2048 + 32)
260 
261 int otx2vf_mcam_flow_init(struct otx2_nic *pfvf)
262 {
263 	struct otx2_flow_config *flow_cfg;
264 
265 	pfvf->flow_cfg = devm_kzalloc(pfvf->dev,
266 				      sizeof(struct otx2_flow_config),
267 				      GFP_KERNEL);
268 	if (!pfvf->flow_cfg)
269 		return -ENOMEM;
270 
271 	pfvf->flow_cfg->dmacflt_bmap = devm_kcalloc(pfvf->dev,
272 						    BITS_TO_LONGS(OTX2_DMAC_FLTR_BITMAP_SZ),
273 						    sizeof(long), GFP_KERNEL);
274 	if (!pfvf->flow_cfg->dmacflt_bmap)
275 		return -ENOMEM;
276 
277 	flow_cfg = pfvf->flow_cfg;
278 	INIT_LIST_HEAD(&flow_cfg->flow_list);
279 	flow_cfg->max_flows = 0;
280 
281 	return 0;
282 }
283 EXPORT_SYMBOL(otx2vf_mcam_flow_init);
284 
285 int otx2_mcam_flow_init(struct otx2_nic *pf)
286 {
287 	int err;
288 
289 	pf->flow_cfg = devm_kzalloc(pf->dev, sizeof(struct otx2_flow_config),
290 				    GFP_KERNEL);
291 	if (!pf->flow_cfg)
292 		return -ENOMEM;
293 
294 	pf->flow_cfg->dmacflt_bmap = devm_kcalloc(pf->dev,
295 						  BITS_TO_LONGS(OTX2_DMAC_FLTR_BITMAP_SZ),
296 						  sizeof(long), GFP_KERNEL);
297 	if (!pf->flow_cfg->dmacflt_bmap)
298 		return -ENOMEM;
299 
300 	INIT_LIST_HEAD(&pf->flow_cfg->flow_list);
301 
302 	/* Allocate bare minimum number of MCAM entries needed for
303 	 * unicast and ntuple filters.
304 	 */
305 	err = otx2_mcam_entry_init(pf);
306 	if (err)
307 		return err;
308 
309 	/* Check if MCAM entries are allocate or not */
310 	if (!(pf->flags & OTX2_FLAG_UCAST_FLTR_SUPPORT))
311 		return 0;
312 
313 	pf->mac_table = devm_kzalloc(pf->dev, sizeof(struct otx2_mac_table)
314 					* OTX2_MAX_UNICAST_FLOWS, GFP_KERNEL);
315 	if (!pf->mac_table)
316 		return -ENOMEM;
317 
318 	otx2_dmacflt_get_max_cnt(pf);
319 
320 	/* DMAC filters are not allocated */
321 	if (!pf->flow_cfg->dmacflt_max_flows)
322 		return 0;
323 
324 	pf->flow_cfg->bmap_to_dmacindex =
325 			devm_kzalloc(pf->dev, sizeof(u32) *
326 				     pf->flow_cfg->dmacflt_max_flows,
327 				     GFP_KERNEL);
328 
329 	if (!pf->flow_cfg->bmap_to_dmacindex)
330 		return -ENOMEM;
331 
332 	pf->flags |= OTX2_FLAG_DMACFLTR_SUPPORT;
333 
334 	return 0;
335 }
336 
337 void otx2_mcam_flow_del(struct otx2_nic *pf)
338 {
339 	otx2_destroy_mcam_flows(pf);
340 }
341 EXPORT_SYMBOL(otx2_mcam_flow_del);
342 
343 /*  On success adds mcam entry
344  *  On failure enable promisous mode
345  */
346 static int otx2_do_add_macfilter(struct otx2_nic *pf, const u8 *mac)
347 {
348 	struct otx2_flow_config *flow_cfg = pf->flow_cfg;
349 	struct npc_install_flow_req *req;
350 	int err, i;
351 
352 	if (!(pf->flags & OTX2_FLAG_UCAST_FLTR_SUPPORT))
353 		return -ENOMEM;
354 
355 	/* dont have free mcam entries or uc list is greater than alloted */
356 	if (netdev_uc_count(pf->netdev) > OTX2_MAX_UNICAST_FLOWS)
357 		return -ENOMEM;
358 
359 	mutex_lock(&pf->mbox.lock);
360 	req = otx2_mbox_alloc_msg_npc_install_flow(&pf->mbox);
361 	if (!req) {
362 		mutex_unlock(&pf->mbox.lock);
363 		return -ENOMEM;
364 	}
365 
366 	/* unicast offset starts with 32 0..31 for ntuple */
367 	for (i = 0; i <  OTX2_MAX_UNICAST_FLOWS; i++) {
368 		if (pf->mac_table[i].inuse)
369 			continue;
370 		ether_addr_copy(pf->mac_table[i].addr, mac);
371 		pf->mac_table[i].inuse = true;
372 		pf->mac_table[i].mcam_entry =
373 			flow_cfg->def_ent[i + flow_cfg->unicast_offset];
374 		req->entry =  pf->mac_table[i].mcam_entry;
375 		break;
376 	}
377 
378 	ether_addr_copy(req->packet.dmac, mac);
379 	eth_broadcast_addr((u8 *)&req->mask.dmac);
380 	req->features = BIT_ULL(NPC_DMAC);
381 	req->channel = pf->hw.rx_chan_base;
382 	req->intf = NIX_INTF_RX;
383 	req->op = NIX_RX_ACTION_DEFAULT;
384 	req->set_cntr = 1;
385 
386 	err = otx2_sync_mbox_msg(&pf->mbox);
387 	mutex_unlock(&pf->mbox.lock);
388 
389 	return err;
390 }
391 
392 int otx2_add_macfilter(struct net_device *netdev, const u8 *mac)
393 {
394 	struct otx2_nic *pf = netdev_priv(netdev);
395 
396 	if (!bitmap_empty(pf->flow_cfg->dmacflt_bmap,
397 			  pf->flow_cfg->dmacflt_max_flows))
398 		netdev_warn(netdev,
399 			    "Add %pM to CGX/RPM DMAC filters list as well\n",
400 			    mac);
401 
402 	return otx2_do_add_macfilter(pf, mac);
403 }
404 
405 static bool otx2_get_mcamentry_for_mac(struct otx2_nic *pf, const u8 *mac,
406 				       int *mcam_entry)
407 {
408 	int i;
409 
410 	for (i = 0; i < OTX2_MAX_UNICAST_FLOWS; i++) {
411 		if (!pf->mac_table[i].inuse)
412 			continue;
413 
414 		if (ether_addr_equal(pf->mac_table[i].addr, mac)) {
415 			*mcam_entry = pf->mac_table[i].mcam_entry;
416 			pf->mac_table[i].inuse = false;
417 			return true;
418 		}
419 	}
420 	return false;
421 }
422 
423 int otx2_del_macfilter(struct net_device *netdev, const u8 *mac)
424 {
425 	struct otx2_nic *pf = netdev_priv(netdev);
426 	struct npc_delete_flow_req *req;
427 	int err, mcam_entry;
428 
429 	/* check does mcam entry exists for given mac */
430 	if (!otx2_get_mcamentry_for_mac(pf, mac, &mcam_entry))
431 		return 0;
432 
433 	mutex_lock(&pf->mbox.lock);
434 	req = otx2_mbox_alloc_msg_npc_delete_flow(&pf->mbox);
435 	if (!req) {
436 		mutex_unlock(&pf->mbox.lock);
437 		return -ENOMEM;
438 	}
439 	req->entry = mcam_entry;
440 	/* Send message to AF */
441 	err = otx2_sync_mbox_msg(&pf->mbox);
442 	mutex_unlock(&pf->mbox.lock);
443 
444 	return err;
445 }
446 
447 static struct otx2_flow *otx2_find_flow(struct otx2_nic *pfvf, u32 location)
448 {
449 	struct otx2_flow *iter;
450 
451 	list_for_each_entry(iter, &pfvf->flow_cfg->flow_list, list) {
452 		if (iter->location == location)
453 			return iter;
454 	}
455 
456 	return NULL;
457 }
458 
459 static void otx2_add_flow_to_list(struct otx2_nic *pfvf, struct otx2_flow *flow)
460 {
461 	struct list_head *head = &pfvf->flow_cfg->flow_list;
462 	struct otx2_flow *iter;
463 
464 	list_for_each_entry(iter, &pfvf->flow_cfg->flow_list, list) {
465 		if (iter->location > flow->location)
466 			break;
467 		head = &iter->list;
468 	}
469 
470 	list_add(&flow->list, head);
471 }
472 
473 int otx2_get_maxflows(struct otx2_flow_config *flow_cfg)
474 {
475 	if (!flow_cfg)
476 		return 0;
477 
478 	if (flow_cfg->nr_flows == flow_cfg->max_flows ||
479 	    !bitmap_empty(flow_cfg->dmacflt_bmap,
480 			  flow_cfg->dmacflt_max_flows))
481 		return flow_cfg->max_flows + flow_cfg->dmacflt_max_flows;
482 	else
483 		return flow_cfg->max_flows;
484 }
485 EXPORT_SYMBOL(otx2_get_maxflows);
486 
487 int otx2_get_flow(struct otx2_nic *pfvf, struct ethtool_rxnfc *nfc,
488 		  u32 location)
489 {
490 	struct otx2_flow *iter;
491 
492 	if (location >= otx2_get_maxflows(pfvf->flow_cfg))
493 		return -EINVAL;
494 
495 	list_for_each_entry(iter, &pfvf->flow_cfg->flow_list, list) {
496 		if (iter->location == location) {
497 			nfc->fs = iter->flow_spec;
498 			nfc->rss_context = iter->rss_ctx_id;
499 			return 0;
500 		}
501 	}
502 
503 	return -ENOENT;
504 }
505 
506 int otx2_get_all_flows(struct otx2_nic *pfvf, struct ethtool_rxnfc *nfc,
507 		       u32 *rule_locs)
508 {
509 	u32 rule_cnt = nfc->rule_cnt;
510 	u32 location = 0;
511 	int idx = 0;
512 	int err = 0;
513 
514 	nfc->data = otx2_get_maxflows(pfvf->flow_cfg);
515 	while ((!err || err == -ENOENT) && idx < rule_cnt) {
516 		err = otx2_get_flow(pfvf, nfc, location);
517 		if (!err)
518 			rule_locs[idx++] = location;
519 		location++;
520 	}
521 	nfc->rule_cnt = rule_cnt;
522 
523 	return err;
524 }
525 
526 static int otx2_prepare_ipv4_flow(struct ethtool_rx_flow_spec *fsp,
527 				  struct npc_install_flow_req *req,
528 				  u32 flow_type)
529 {
530 	struct ethtool_usrip4_spec *ipv4_usr_mask = &fsp->m_u.usr_ip4_spec;
531 	struct ethtool_usrip4_spec *ipv4_usr_hdr = &fsp->h_u.usr_ip4_spec;
532 	struct ethtool_tcpip4_spec *ipv4_l4_mask = &fsp->m_u.tcp_ip4_spec;
533 	struct ethtool_tcpip4_spec *ipv4_l4_hdr = &fsp->h_u.tcp_ip4_spec;
534 	struct ethtool_ah_espip4_spec *ah_esp_hdr = &fsp->h_u.ah_ip4_spec;
535 	struct ethtool_ah_espip4_spec *ah_esp_mask = &fsp->m_u.ah_ip4_spec;
536 	struct flow_msg *pmask = &req->mask;
537 	struct flow_msg *pkt = &req->packet;
538 
539 	switch (flow_type) {
540 	case IP_USER_FLOW:
541 		if (ipv4_usr_mask->ip4src) {
542 			memcpy(&pkt->ip4src, &ipv4_usr_hdr->ip4src,
543 			       sizeof(pkt->ip4src));
544 			memcpy(&pmask->ip4src, &ipv4_usr_mask->ip4src,
545 			       sizeof(pmask->ip4src));
546 			req->features |= BIT_ULL(NPC_SIP_IPV4);
547 		}
548 		if (ipv4_usr_mask->ip4dst) {
549 			memcpy(&pkt->ip4dst, &ipv4_usr_hdr->ip4dst,
550 			       sizeof(pkt->ip4dst));
551 			memcpy(&pmask->ip4dst, &ipv4_usr_mask->ip4dst,
552 			       sizeof(pmask->ip4dst));
553 			req->features |= BIT_ULL(NPC_DIP_IPV4);
554 		}
555 		if (ipv4_usr_mask->tos) {
556 			pkt->tos = ipv4_usr_hdr->tos;
557 			pmask->tos = ipv4_usr_mask->tos;
558 			req->features |= BIT_ULL(NPC_TOS);
559 		}
560 		if (ipv4_usr_mask->proto) {
561 			switch (ipv4_usr_hdr->proto) {
562 			case IPPROTO_ICMP:
563 				req->features |= BIT_ULL(NPC_IPPROTO_ICMP);
564 				break;
565 			case IPPROTO_TCP:
566 				req->features |= BIT_ULL(NPC_IPPROTO_TCP);
567 				break;
568 			case IPPROTO_UDP:
569 				req->features |= BIT_ULL(NPC_IPPROTO_UDP);
570 				break;
571 			case IPPROTO_SCTP:
572 				req->features |= BIT_ULL(NPC_IPPROTO_SCTP);
573 				break;
574 			case IPPROTO_AH:
575 				req->features |= BIT_ULL(NPC_IPPROTO_AH);
576 				break;
577 			case IPPROTO_ESP:
578 				req->features |= BIT_ULL(NPC_IPPROTO_ESP);
579 				break;
580 			default:
581 				return -EOPNOTSUPP;
582 			}
583 		}
584 		pkt->etype = cpu_to_be16(ETH_P_IP);
585 		pmask->etype = cpu_to_be16(0xFFFF);
586 		req->features |= BIT_ULL(NPC_ETYPE);
587 		break;
588 	case TCP_V4_FLOW:
589 	case UDP_V4_FLOW:
590 	case SCTP_V4_FLOW:
591 		pkt->etype = cpu_to_be16(ETH_P_IP);
592 		pmask->etype = cpu_to_be16(0xFFFF);
593 		req->features |= BIT_ULL(NPC_ETYPE);
594 		if (ipv4_l4_mask->ip4src) {
595 			memcpy(&pkt->ip4src, &ipv4_l4_hdr->ip4src,
596 			       sizeof(pkt->ip4src));
597 			memcpy(&pmask->ip4src, &ipv4_l4_mask->ip4src,
598 			       sizeof(pmask->ip4src));
599 			req->features |= BIT_ULL(NPC_SIP_IPV4);
600 		}
601 		if (ipv4_l4_mask->ip4dst) {
602 			memcpy(&pkt->ip4dst, &ipv4_l4_hdr->ip4dst,
603 			       sizeof(pkt->ip4dst));
604 			memcpy(&pmask->ip4dst, &ipv4_l4_mask->ip4dst,
605 			       sizeof(pmask->ip4dst));
606 			req->features |= BIT_ULL(NPC_DIP_IPV4);
607 		}
608 		if (ipv4_l4_mask->tos) {
609 			pkt->tos = ipv4_l4_hdr->tos;
610 			pmask->tos = ipv4_l4_mask->tos;
611 			req->features |= BIT_ULL(NPC_TOS);
612 		}
613 		if (ipv4_l4_mask->psrc) {
614 			memcpy(&pkt->sport, &ipv4_l4_hdr->psrc,
615 			       sizeof(pkt->sport));
616 			memcpy(&pmask->sport, &ipv4_l4_mask->psrc,
617 			       sizeof(pmask->sport));
618 			if (flow_type == UDP_V4_FLOW)
619 				req->features |= BIT_ULL(NPC_SPORT_UDP);
620 			else if (flow_type == TCP_V4_FLOW)
621 				req->features |= BIT_ULL(NPC_SPORT_TCP);
622 			else
623 				req->features |= BIT_ULL(NPC_SPORT_SCTP);
624 		}
625 		if (ipv4_l4_mask->pdst) {
626 			memcpy(&pkt->dport, &ipv4_l4_hdr->pdst,
627 			       sizeof(pkt->dport));
628 			memcpy(&pmask->dport, &ipv4_l4_mask->pdst,
629 			       sizeof(pmask->dport));
630 			if (flow_type == UDP_V4_FLOW)
631 				req->features |= BIT_ULL(NPC_DPORT_UDP);
632 			else if (flow_type == TCP_V4_FLOW)
633 				req->features |= BIT_ULL(NPC_DPORT_TCP);
634 			else
635 				req->features |= BIT_ULL(NPC_DPORT_SCTP);
636 		}
637 		if (flow_type == UDP_V4_FLOW)
638 			req->features |= BIT_ULL(NPC_IPPROTO_UDP);
639 		else if (flow_type == TCP_V4_FLOW)
640 			req->features |= BIT_ULL(NPC_IPPROTO_TCP);
641 		else
642 			req->features |= BIT_ULL(NPC_IPPROTO_SCTP);
643 		break;
644 	case AH_V4_FLOW:
645 	case ESP_V4_FLOW:
646 		pkt->etype = cpu_to_be16(ETH_P_IP);
647 		pmask->etype = cpu_to_be16(0xFFFF);
648 		req->features |= BIT_ULL(NPC_ETYPE);
649 		if (ah_esp_mask->ip4src) {
650 			memcpy(&pkt->ip4src, &ah_esp_hdr->ip4src,
651 			       sizeof(pkt->ip4src));
652 			memcpy(&pmask->ip4src, &ah_esp_mask->ip4src,
653 			       sizeof(pmask->ip4src));
654 			req->features |= BIT_ULL(NPC_SIP_IPV4);
655 		}
656 		if (ah_esp_mask->ip4dst) {
657 			memcpy(&pkt->ip4dst, &ah_esp_hdr->ip4dst,
658 			       sizeof(pkt->ip4dst));
659 			memcpy(&pmask->ip4dst, &ah_esp_mask->ip4dst,
660 			       sizeof(pmask->ip4dst));
661 			req->features |= BIT_ULL(NPC_DIP_IPV4);
662 		}
663 		if (ah_esp_mask->tos) {
664 			pkt->tos = ah_esp_hdr->tos;
665 			pmask->tos = ah_esp_mask->tos;
666 			req->features |= BIT_ULL(NPC_TOS);
667 		}
668 
669 		/* NPC profile doesn't extract AH/ESP header fields */
670 		if (ah_esp_mask->spi & ah_esp_hdr->spi)
671 			return -EOPNOTSUPP;
672 
673 		if (flow_type == AH_V4_FLOW)
674 			req->features |= BIT_ULL(NPC_IPPROTO_AH);
675 		else
676 			req->features |= BIT_ULL(NPC_IPPROTO_ESP);
677 		break;
678 	default:
679 		break;
680 	}
681 
682 	return 0;
683 }
684 
685 static int otx2_prepare_ipv6_flow(struct ethtool_rx_flow_spec *fsp,
686 				  struct npc_install_flow_req *req,
687 				  u32 flow_type)
688 {
689 	struct ethtool_usrip6_spec *ipv6_usr_mask = &fsp->m_u.usr_ip6_spec;
690 	struct ethtool_usrip6_spec *ipv6_usr_hdr = &fsp->h_u.usr_ip6_spec;
691 	struct ethtool_tcpip6_spec *ipv6_l4_mask = &fsp->m_u.tcp_ip6_spec;
692 	struct ethtool_tcpip6_spec *ipv6_l4_hdr = &fsp->h_u.tcp_ip6_spec;
693 	struct ethtool_ah_espip6_spec *ah_esp_hdr = &fsp->h_u.ah_ip6_spec;
694 	struct ethtool_ah_espip6_spec *ah_esp_mask = &fsp->m_u.ah_ip6_spec;
695 	struct flow_msg *pmask = &req->mask;
696 	struct flow_msg *pkt = &req->packet;
697 
698 	switch (flow_type) {
699 	case IPV6_USER_FLOW:
700 		if (!ipv6_addr_any((struct in6_addr *)ipv6_usr_mask->ip6src)) {
701 			memcpy(&pkt->ip6src, &ipv6_usr_hdr->ip6src,
702 			       sizeof(pkt->ip6src));
703 			memcpy(&pmask->ip6src, &ipv6_usr_mask->ip6src,
704 			       sizeof(pmask->ip6src));
705 			req->features |= BIT_ULL(NPC_SIP_IPV6);
706 		}
707 		if (!ipv6_addr_any((struct in6_addr *)ipv6_usr_mask->ip6dst)) {
708 			memcpy(&pkt->ip6dst, &ipv6_usr_hdr->ip6dst,
709 			       sizeof(pkt->ip6dst));
710 			memcpy(&pmask->ip6dst, &ipv6_usr_mask->ip6dst,
711 			       sizeof(pmask->ip6dst));
712 			req->features |= BIT_ULL(NPC_DIP_IPV6);
713 		}
714 		if (ipv6_usr_hdr->l4_proto == IPPROTO_FRAGMENT) {
715 			pkt->next_header = ipv6_usr_hdr->l4_proto;
716 			pmask->next_header = ipv6_usr_mask->l4_proto;
717 			req->features |= BIT_ULL(NPC_IPFRAG_IPV6);
718 		}
719 		pkt->etype = cpu_to_be16(ETH_P_IPV6);
720 		pmask->etype = cpu_to_be16(0xFFFF);
721 		req->features |= BIT_ULL(NPC_ETYPE);
722 		break;
723 	case TCP_V6_FLOW:
724 	case UDP_V6_FLOW:
725 	case SCTP_V6_FLOW:
726 		pkt->etype = cpu_to_be16(ETH_P_IPV6);
727 		pmask->etype = cpu_to_be16(0xFFFF);
728 		req->features |= BIT_ULL(NPC_ETYPE);
729 		if (!ipv6_addr_any((struct in6_addr *)ipv6_l4_mask->ip6src)) {
730 			memcpy(&pkt->ip6src, &ipv6_l4_hdr->ip6src,
731 			       sizeof(pkt->ip6src));
732 			memcpy(&pmask->ip6src, &ipv6_l4_mask->ip6src,
733 			       sizeof(pmask->ip6src));
734 			req->features |= BIT_ULL(NPC_SIP_IPV6);
735 		}
736 		if (!ipv6_addr_any((struct in6_addr *)ipv6_l4_mask->ip6dst)) {
737 			memcpy(&pkt->ip6dst, &ipv6_l4_hdr->ip6dst,
738 			       sizeof(pkt->ip6dst));
739 			memcpy(&pmask->ip6dst, &ipv6_l4_mask->ip6dst,
740 			       sizeof(pmask->ip6dst));
741 			req->features |= BIT_ULL(NPC_DIP_IPV6);
742 		}
743 		if (ipv6_l4_mask->psrc) {
744 			memcpy(&pkt->sport, &ipv6_l4_hdr->psrc,
745 			       sizeof(pkt->sport));
746 			memcpy(&pmask->sport, &ipv6_l4_mask->psrc,
747 			       sizeof(pmask->sport));
748 			if (flow_type == UDP_V6_FLOW)
749 				req->features |= BIT_ULL(NPC_SPORT_UDP);
750 			else if (flow_type == TCP_V6_FLOW)
751 				req->features |= BIT_ULL(NPC_SPORT_TCP);
752 			else
753 				req->features |= BIT_ULL(NPC_SPORT_SCTP);
754 		}
755 		if (ipv6_l4_mask->pdst) {
756 			memcpy(&pkt->dport, &ipv6_l4_hdr->pdst,
757 			       sizeof(pkt->dport));
758 			memcpy(&pmask->dport, &ipv6_l4_mask->pdst,
759 			       sizeof(pmask->dport));
760 			if (flow_type == UDP_V6_FLOW)
761 				req->features |= BIT_ULL(NPC_DPORT_UDP);
762 			else if (flow_type == TCP_V6_FLOW)
763 				req->features |= BIT_ULL(NPC_DPORT_TCP);
764 			else
765 				req->features |= BIT_ULL(NPC_DPORT_SCTP);
766 		}
767 		if (flow_type == UDP_V6_FLOW)
768 			req->features |= BIT_ULL(NPC_IPPROTO_UDP);
769 		else if (flow_type == TCP_V6_FLOW)
770 			req->features |= BIT_ULL(NPC_IPPROTO_TCP);
771 		else
772 			req->features |= BIT_ULL(NPC_IPPROTO_SCTP);
773 		break;
774 	case AH_V6_FLOW:
775 	case ESP_V6_FLOW:
776 		pkt->etype = cpu_to_be16(ETH_P_IPV6);
777 		pmask->etype = cpu_to_be16(0xFFFF);
778 		req->features |= BIT_ULL(NPC_ETYPE);
779 		if (!ipv6_addr_any((struct in6_addr *)ah_esp_hdr->ip6src)) {
780 			memcpy(&pkt->ip6src, &ah_esp_hdr->ip6src,
781 			       sizeof(pkt->ip6src));
782 			memcpy(&pmask->ip6src, &ah_esp_mask->ip6src,
783 			       sizeof(pmask->ip6src));
784 			req->features |= BIT_ULL(NPC_SIP_IPV6);
785 		}
786 		if (!ipv6_addr_any((struct in6_addr *)ah_esp_hdr->ip6dst)) {
787 			memcpy(&pkt->ip6dst, &ah_esp_hdr->ip6dst,
788 			       sizeof(pkt->ip6dst));
789 			memcpy(&pmask->ip6dst, &ah_esp_mask->ip6dst,
790 			       sizeof(pmask->ip6dst));
791 			req->features |= BIT_ULL(NPC_DIP_IPV6);
792 		}
793 
794 		/* NPC profile doesn't extract AH/ESP header fields */
795 		if ((ah_esp_mask->spi & ah_esp_hdr->spi) ||
796 		    (ah_esp_mask->tclass & ah_esp_hdr->tclass))
797 			return -EOPNOTSUPP;
798 
799 		if (flow_type == AH_V6_FLOW)
800 			req->features |= BIT_ULL(NPC_IPPROTO_AH);
801 		else
802 			req->features |= BIT_ULL(NPC_IPPROTO_ESP);
803 		break;
804 	default:
805 		break;
806 	}
807 
808 	return 0;
809 }
810 
811 static int otx2_prepare_flow_request(struct ethtool_rx_flow_spec *fsp,
812 			      struct npc_install_flow_req *req)
813 {
814 	struct ethhdr *eth_mask = &fsp->m_u.ether_spec;
815 	struct ethhdr *eth_hdr = &fsp->h_u.ether_spec;
816 	struct flow_msg *pmask = &req->mask;
817 	struct flow_msg *pkt = &req->packet;
818 	u32 flow_type;
819 	int ret;
820 
821 	flow_type = fsp->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS);
822 	switch (flow_type) {
823 	/* bits not set in mask are don't care */
824 	case ETHER_FLOW:
825 		if (!is_zero_ether_addr(eth_mask->h_source)) {
826 			ether_addr_copy(pkt->smac, eth_hdr->h_source);
827 			ether_addr_copy(pmask->smac, eth_mask->h_source);
828 			req->features |= BIT_ULL(NPC_SMAC);
829 		}
830 		if (!is_zero_ether_addr(eth_mask->h_dest)) {
831 			ether_addr_copy(pkt->dmac, eth_hdr->h_dest);
832 			ether_addr_copy(pmask->dmac, eth_mask->h_dest);
833 			req->features |= BIT_ULL(NPC_DMAC);
834 		}
835 		if (eth_hdr->h_proto) {
836 			memcpy(&pkt->etype, &eth_hdr->h_proto,
837 			       sizeof(pkt->etype));
838 			memcpy(&pmask->etype, &eth_mask->h_proto,
839 			       sizeof(pmask->etype));
840 			req->features |= BIT_ULL(NPC_ETYPE);
841 		}
842 		break;
843 	case IP_USER_FLOW:
844 	case TCP_V4_FLOW:
845 	case UDP_V4_FLOW:
846 	case SCTP_V4_FLOW:
847 	case AH_V4_FLOW:
848 	case ESP_V4_FLOW:
849 		ret = otx2_prepare_ipv4_flow(fsp, req, flow_type);
850 		if (ret)
851 			return ret;
852 		break;
853 	case IPV6_USER_FLOW:
854 	case TCP_V6_FLOW:
855 	case UDP_V6_FLOW:
856 	case SCTP_V6_FLOW:
857 	case AH_V6_FLOW:
858 	case ESP_V6_FLOW:
859 		ret = otx2_prepare_ipv6_flow(fsp, req, flow_type);
860 		if (ret)
861 			return ret;
862 		break;
863 	default:
864 		return -EOPNOTSUPP;
865 	}
866 	if (fsp->flow_type & FLOW_EXT) {
867 		u16 vlan_etype;
868 
869 		if (fsp->m_ext.vlan_etype) {
870 			/* Partial masks not supported */
871 			if (be16_to_cpu(fsp->m_ext.vlan_etype) != 0xFFFF)
872 				return -EINVAL;
873 
874 			vlan_etype = be16_to_cpu(fsp->h_ext.vlan_etype);
875 
876 			/* Drop rule with vlan_etype == 802.1Q
877 			 * and vlan_id == 0 is not supported
878 			 */
879 			if (vlan_etype == ETH_P_8021Q && !fsp->m_ext.vlan_tci &&
880 			    fsp->ring_cookie == RX_CLS_FLOW_DISC)
881 				return -EINVAL;
882 
883 			/* Only ETH_P_8021Q and ETH_P_802AD types supported */
884 			if (vlan_etype != ETH_P_8021Q &&
885 			    vlan_etype != ETH_P_8021AD)
886 				return -EINVAL;
887 
888 			memcpy(&pkt->vlan_etype, &fsp->h_ext.vlan_etype,
889 			       sizeof(pkt->vlan_etype));
890 			memcpy(&pmask->vlan_etype, &fsp->m_ext.vlan_etype,
891 			       sizeof(pmask->vlan_etype));
892 
893 			if (vlan_etype == ETH_P_8021Q)
894 				req->features |= BIT_ULL(NPC_VLAN_ETYPE_CTAG);
895 			else
896 				req->features |= BIT_ULL(NPC_VLAN_ETYPE_STAG);
897 		}
898 
899 		if (fsp->m_ext.vlan_tci) {
900 			memcpy(&pkt->vlan_tci, &fsp->h_ext.vlan_tci,
901 			       sizeof(pkt->vlan_tci));
902 			memcpy(&pmask->vlan_tci, &fsp->m_ext.vlan_tci,
903 			       sizeof(pmask->vlan_tci));
904 			req->features |= BIT_ULL(NPC_OUTER_VID);
905 		}
906 
907 		if (fsp->m_ext.data[1]) {
908 			if (flow_type == IP_USER_FLOW) {
909 				if (be32_to_cpu(fsp->h_ext.data[1]) != IPV4_FLAG_MORE)
910 					return -EINVAL;
911 
912 				pkt->ip_flag = be32_to_cpu(fsp->h_ext.data[1]);
913 				pmask->ip_flag = be32_to_cpu(fsp->m_ext.data[1]);
914 				req->features |= BIT_ULL(NPC_IPFRAG_IPV4);
915 			} else if (fsp->h_ext.data[1] ==
916 					cpu_to_be32(OTX2_DEFAULT_ACTION)) {
917 				/* Not Drop/Direct to queue but use action
918 				 * in default entry
919 				 */
920 				req->op = NIX_RX_ACTION_DEFAULT;
921 			}
922 		}
923 	}
924 
925 	if (fsp->flow_type & FLOW_MAC_EXT &&
926 	    !is_zero_ether_addr(fsp->m_ext.h_dest)) {
927 		ether_addr_copy(pkt->dmac, fsp->h_ext.h_dest);
928 		ether_addr_copy(pmask->dmac, fsp->m_ext.h_dest);
929 		req->features |= BIT_ULL(NPC_DMAC);
930 	}
931 
932 	if (!req->features)
933 		return -EOPNOTSUPP;
934 
935 	return 0;
936 }
937 
938 static int otx2_is_flow_rule_dmacfilter(struct otx2_nic *pfvf,
939 					struct ethtool_rx_flow_spec *fsp)
940 {
941 	struct ethhdr *eth_mask = &fsp->m_u.ether_spec;
942 	struct ethhdr *eth_hdr = &fsp->h_u.ether_spec;
943 	u64 ring_cookie = fsp->ring_cookie;
944 	u32 flow_type;
945 
946 	if (!(pfvf->flags & OTX2_FLAG_DMACFLTR_SUPPORT))
947 		return false;
948 
949 	flow_type = fsp->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS);
950 
951 	/* CGX/RPM block dmac filtering configured for white listing
952 	 * check for action other than DROP
953 	 */
954 	if (flow_type == ETHER_FLOW && ring_cookie != RX_CLS_FLOW_DISC &&
955 	    !ethtool_get_flow_spec_ring_vf(ring_cookie)) {
956 		if (is_zero_ether_addr(eth_mask->h_dest) &&
957 		    is_valid_ether_addr(eth_hdr->h_dest))
958 			return true;
959 	}
960 
961 	return false;
962 }
963 
964 static int otx2_add_flow_msg(struct otx2_nic *pfvf, struct otx2_flow *flow)
965 {
966 	u64 ring_cookie = flow->flow_spec.ring_cookie;
967 #ifdef CONFIG_DCB
968 	int vlan_prio, qidx, pfc_rule = 0;
969 #endif
970 	struct npc_install_flow_req *req;
971 	int err, vf = 0;
972 
973 	mutex_lock(&pfvf->mbox.lock);
974 	req = otx2_mbox_alloc_msg_npc_install_flow(&pfvf->mbox);
975 	if (!req) {
976 		mutex_unlock(&pfvf->mbox.lock);
977 		return -ENOMEM;
978 	}
979 
980 	err = otx2_prepare_flow_request(&flow->flow_spec, req);
981 	if (err) {
982 		/* free the allocated msg above */
983 		otx2_mbox_reset(&pfvf->mbox.mbox, 0);
984 		mutex_unlock(&pfvf->mbox.lock);
985 		return err;
986 	}
987 
988 	req->entry = flow->entry;
989 	req->intf = NIX_INTF_RX;
990 	req->set_cntr = 1;
991 	req->channel = pfvf->hw.rx_chan_base;
992 	if (ring_cookie == RX_CLS_FLOW_DISC) {
993 		req->op = NIX_RX_ACTIONOP_DROP;
994 	} else {
995 		/* change to unicast only if action of default entry is not
996 		 * requested by user
997 		 */
998 		if (flow->flow_spec.flow_type & FLOW_RSS) {
999 			req->op = NIX_RX_ACTIONOP_RSS;
1000 			req->index = flow->rss_ctx_id;
1001 			req->flow_key_alg = pfvf->hw.flowkey_alg_idx;
1002 		} else {
1003 			req->op = NIX_RX_ACTIONOP_UCAST;
1004 			req->index = ethtool_get_flow_spec_ring(ring_cookie);
1005 		}
1006 		vf = ethtool_get_flow_spec_ring_vf(ring_cookie);
1007 		if (vf > pci_num_vf(pfvf->pdev)) {
1008 			mutex_unlock(&pfvf->mbox.lock);
1009 			return -EINVAL;
1010 		}
1011 
1012 #ifdef CONFIG_DCB
1013 		/* Identify PFC rule if PFC enabled and ntuple rule is vlan */
1014 		if (!vf && (req->features & BIT_ULL(NPC_OUTER_VID)) &&
1015 		    pfvf->pfc_en && req->op != NIX_RX_ACTIONOP_RSS) {
1016 			vlan_prio = ntohs(req->packet.vlan_tci) &
1017 				    ntohs(req->mask.vlan_tci);
1018 
1019 			/* Get the priority */
1020 			vlan_prio >>= 13;
1021 			flow->rule_type |= PFC_FLOWCTRL_RULE;
1022 			/* Check if PFC enabled for this priority */
1023 			if (pfvf->pfc_en & BIT(vlan_prio)) {
1024 				pfc_rule = true;
1025 				qidx = req->index;
1026 			}
1027 		}
1028 #endif
1029 	}
1030 
1031 	/* ethtool ring_cookie has (VF + 1) for VF */
1032 	if (vf) {
1033 		req->vf = vf;
1034 		flow->is_vf = true;
1035 		flow->vf = vf;
1036 	}
1037 
1038 	/* Send message to AF */
1039 	err = otx2_sync_mbox_msg(&pfvf->mbox);
1040 
1041 #ifdef CONFIG_DCB
1042 	if (!err && pfc_rule)
1043 		otx2_update_bpid_in_rqctx(pfvf, vlan_prio, qidx, true);
1044 #endif
1045 
1046 	mutex_unlock(&pfvf->mbox.lock);
1047 	return err;
1048 }
1049 
1050 static int otx2_add_flow_with_pfmac(struct otx2_nic *pfvf,
1051 				    struct otx2_flow *flow)
1052 {
1053 	struct otx2_flow *pf_mac;
1054 	struct ethhdr *eth_hdr;
1055 
1056 	pf_mac = kzalloc(sizeof(*pf_mac), GFP_KERNEL);
1057 	if (!pf_mac)
1058 		return -ENOMEM;
1059 
1060 	pf_mac->entry = 0;
1061 	pf_mac->rule_type |= DMAC_FILTER_RULE;
1062 	pf_mac->location = pfvf->flow_cfg->max_flows;
1063 	memcpy(&pf_mac->flow_spec, &flow->flow_spec,
1064 	       sizeof(struct ethtool_rx_flow_spec));
1065 	pf_mac->flow_spec.location = pf_mac->location;
1066 
1067 	/* Copy PF mac address */
1068 	eth_hdr = &pf_mac->flow_spec.h_u.ether_spec;
1069 	ether_addr_copy(eth_hdr->h_dest, pfvf->netdev->dev_addr);
1070 
1071 	/* Install DMAC filter with PF mac address */
1072 	otx2_dmacflt_add(pfvf, eth_hdr->h_dest, 0);
1073 
1074 	otx2_add_flow_to_list(pfvf, pf_mac);
1075 	pfvf->flow_cfg->nr_flows++;
1076 	set_bit(0, pfvf->flow_cfg->dmacflt_bmap);
1077 
1078 	return 0;
1079 }
1080 
1081 int otx2_add_flow(struct otx2_nic *pfvf, struct ethtool_rxnfc *nfc)
1082 {
1083 	struct otx2_flow_config *flow_cfg = pfvf->flow_cfg;
1084 	struct ethtool_rx_flow_spec *fsp = &nfc->fs;
1085 	struct otx2_flow *flow;
1086 	struct ethhdr *eth_hdr;
1087 	bool new = false;
1088 	int err = 0;
1089 	u32 ring;
1090 
1091 	if (!flow_cfg->max_flows) {
1092 		netdev_err(pfvf->netdev,
1093 			   "Ntuple rule count is 0, allocate and retry\n");
1094 		return -EINVAL;
1095 	}
1096 
1097 	ring = ethtool_get_flow_spec_ring(fsp->ring_cookie);
1098 	if (!(pfvf->flags & OTX2_FLAG_NTUPLE_SUPPORT))
1099 		return -ENOMEM;
1100 
1101 	if (ring >= pfvf->hw.rx_queues && fsp->ring_cookie != RX_CLS_FLOW_DISC)
1102 		return -EINVAL;
1103 
1104 	if (fsp->location >= otx2_get_maxflows(flow_cfg))
1105 		return -EINVAL;
1106 
1107 	flow = otx2_find_flow(pfvf, fsp->location);
1108 	if (!flow) {
1109 		flow = kzalloc(sizeof(*flow), GFP_KERNEL);
1110 		if (!flow)
1111 			return -ENOMEM;
1112 		flow->location = fsp->location;
1113 		flow->entry = flow_cfg->flow_ent[flow->location];
1114 		new = true;
1115 	}
1116 	/* struct copy */
1117 	flow->flow_spec = *fsp;
1118 
1119 	if (fsp->flow_type & FLOW_RSS)
1120 		flow->rss_ctx_id = nfc->rss_context;
1121 
1122 	if (otx2_is_flow_rule_dmacfilter(pfvf, &flow->flow_spec)) {
1123 		eth_hdr = &flow->flow_spec.h_u.ether_spec;
1124 
1125 		/* Sync dmac filter table with updated fields */
1126 		if (flow->rule_type & DMAC_FILTER_RULE)
1127 			return otx2_dmacflt_update(pfvf, eth_hdr->h_dest,
1128 						   flow->entry);
1129 
1130 		if (bitmap_full(flow_cfg->dmacflt_bmap,
1131 				flow_cfg->dmacflt_max_flows)) {
1132 			netdev_warn(pfvf->netdev,
1133 				    "Can't insert the rule %d as max allowed dmac filters are %d\n",
1134 				    flow->location +
1135 				    flow_cfg->dmacflt_max_flows,
1136 				    flow_cfg->dmacflt_max_flows);
1137 			err = -EINVAL;
1138 			if (new)
1139 				kfree(flow);
1140 			return err;
1141 		}
1142 
1143 		/* Install PF mac address to DMAC filter list */
1144 		if (!test_bit(0, flow_cfg->dmacflt_bmap))
1145 			otx2_add_flow_with_pfmac(pfvf, flow);
1146 
1147 		flow->rule_type |= DMAC_FILTER_RULE;
1148 		flow->entry = find_first_zero_bit(flow_cfg->dmacflt_bmap,
1149 						  flow_cfg->dmacflt_max_flows);
1150 		fsp->location = flow_cfg->max_flows + flow->entry;
1151 		flow->flow_spec.location = fsp->location;
1152 		flow->location = fsp->location;
1153 
1154 		set_bit(flow->entry, flow_cfg->dmacflt_bmap);
1155 		otx2_dmacflt_add(pfvf, eth_hdr->h_dest, flow->entry);
1156 
1157 	} else {
1158 		if (flow->location >= pfvf->flow_cfg->max_flows) {
1159 			netdev_warn(pfvf->netdev,
1160 				    "Can't insert non dmac ntuple rule at %d, allowed range %d-0\n",
1161 				    flow->location,
1162 				    flow_cfg->max_flows - 1);
1163 			err = -EINVAL;
1164 		} else {
1165 			err = otx2_add_flow_msg(pfvf, flow);
1166 		}
1167 	}
1168 
1169 	if (err) {
1170 		if (err == MBOX_MSG_INVALID)
1171 			err = -EINVAL;
1172 		if (new)
1173 			kfree(flow);
1174 		return err;
1175 	}
1176 
1177 	/* add the new flow installed to list */
1178 	if (new) {
1179 		otx2_add_flow_to_list(pfvf, flow);
1180 		flow_cfg->nr_flows++;
1181 	}
1182 
1183 	return 0;
1184 }
1185 
1186 static int otx2_remove_flow_msg(struct otx2_nic *pfvf, u16 entry, bool all)
1187 {
1188 	struct npc_delete_flow_req *req;
1189 	int err;
1190 
1191 	mutex_lock(&pfvf->mbox.lock);
1192 	req = otx2_mbox_alloc_msg_npc_delete_flow(&pfvf->mbox);
1193 	if (!req) {
1194 		mutex_unlock(&pfvf->mbox.lock);
1195 		return -ENOMEM;
1196 	}
1197 
1198 	req->entry = entry;
1199 	if (all)
1200 		req->all = 1;
1201 
1202 	/* Send message to AF */
1203 	err = otx2_sync_mbox_msg(&pfvf->mbox);
1204 	mutex_unlock(&pfvf->mbox.lock);
1205 	return err;
1206 }
1207 
1208 static void otx2_update_rem_pfmac(struct otx2_nic *pfvf, int req)
1209 {
1210 	struct otx2_flow *iter;
1211 	struct ethhdr *eth_hdr;
1212 	bool found = false;
1213 
1214 	list_for_each_entry(iter, &pfvf->flow_cfg->flow_list, list) {
1215 		if ((iter->rule_type & DMAC_FILTER_RULE) && iter->entry == 0) {
1216 			eth_hdr = &iter->flow_spec.h_u.ether_spec;
1217 			if (req == DMAC_ADDR_DEL) {
1218 				otx2_dmacflt_remove(pfvf, eth_hdr->h_dest,
1219 						    0);
1220 				clear_bit(0, pfvf->flow_cfg->dmacflt_bmap);
1221 				found = true;
1222 			} else {
1223 				ether_addr_copy(eth_hdr->h_dest,
1224 						pfvf->netdev->dev_addr);
1225 
1226 				otx2_dmacflt_update(pfvf, eth_hdr->h_dest, 0);
1227 			}
1228 			break;
1229 		}
1230 	}
1231 
1232 	if (found) {
1233 		list_del(&iter->list);
1234 		kfree(iter);
1235 		pfvf->flow_cfg->nr_flows--;
1236 	}
1237 }
1238 
1239 int otx2_remove_flow(struct otx2_nic *pfvf, u32 location)
1240 {
1241 	struct otx2_flow_config *flow_cfg = pfvf->flow_cfg;
1242 	struct otx2_flow *flow;
1243 	int err;
1244 
1245 	if (location >= otx2_get_maxflows(flow_cfg))
1246 		return -EINVAL;
1247 
1248 	flow = otx2_find_flow(pfvf, location);
1249 	if (!flow)
1250 		return -ENOENT;
1251 
1252 	if (flow->rule_type & DMAC_FILTER_RULE) {
1253 		struct ethhdr *eth_hdr = &flow->flow_spec.h_u.ether_spec;
1254 
1255 		/* user not allowed to remove dmac filter with interface mac */
1256 		if (ether_addr_equal(pfvf->netdev->dev_addr, eth_hdr->h_dest))
1257 			return -EPERM;
1258 
1259 		err = otx2_dmacflt_remove(pfvf, eth_hdr->h_dest,
1260 					  flow->entry);
1261 		clear_bit(flow->entry, flow_cfg->dmacflt_bmap);
1262 		/* If all dmac filters are removed delete macfilter with
1263 		 * interface mac address and configure CGX/RPM block in
1264 		 * promiscuous mode
1265 		 */
1266 		if (bitmap_weight(flow_cfg->dmacflt_bmap,
1267 				  flow_cfg->dmacflt_max_flows) == 1)
1268 			otx2_update_rem_pfmac(pfvf, DMAC_ADDR_DEL);
1269 	} else {
1270 #ifdef CONFIG_DCB
1271 		if (flow->rule_type & PFC_FLOWCTRL_RULE)
1272 			otx2_update_bpid_in_rqctx(pfvf, 0,
1273 						  flow->flow_spec.ring_cookie,
1274 						  false);
1275 #endif
1276 
1277 		err = otx2_remove_flow_msg(pfvf, flow->entry, false);
1278 	}
1279 
1280 	if (err)
1281 		return err;
1282 
1283 	list_del(&flow->list);
1284 	kfree(flow);
1285 	flow_cfg->nr_flows--;
1286 
1287 	return 0;
1288 }
1289 
1290 void otx2_rss_ctx_flow_del(struct otx2_nic *pfvf, int ctx_id)
1291 {
1292 	struct otx2_flow *flow, *tmp;
1293 	int err;
1294 
1295 	list_for_each_entry_safe(flow, tmp, &pfvf->flow_cfg->flow_list, list) {
1296 		if (flow->rss_ctx_id != ctx_id)
1297 			continue;
1298 		err = otx2_remove_flow(pfvf, flow->location);
1299 		if (err)
1300 			netdev_warn(pfvf->netdev,
1301 				    "Can't delete the rule %d associated with this rss group err:%d",
1302 				    flow->location, err);
1303 	}
1304 }
1305 
1306 int otx2_destroy_ntuple_flows(struct otx2_nic *pfvf)
1307 {
1308 	struct otx2_flow_config *flow_cfg = pfvf->flow_cfg;
1309 	struct npc_delete_flow_req *req;
1310 	struct otx2_flow *iter, *tmp;
1311 	int err;
1312 
1313 	if (!(pfvf->flags & OTX2_FLAG_NTUPLE_SUPPORT))
1314 		return 0;
1315 
1316 	if (!flow_cfg->max_flows)
1317 		return 0;
1318 
1319 	mutex_lock(&pfvf->mbox.lock);
1320 	req = otx2_mbox_alloc_msg_npc_delete_flow(&pfvf->mbox);
1321 	if (!req) {
1322 		mutex_unlock(&pfvf->mbox.lock);
1323 		return -ENOMEM;
1324 	}
1325 
1326 	req->start = flow_cfg->flow_ent[0];
1327 	req->end   = flow_cfg->flow_ent[flow_cfg->max_flows - 1];
1328 	err = otx2_sync_mbox_msg(&pfvf->mbox);
1329 	mutex_unlock(&pfvf->mbox.lock);
1330 
1331 	list_for_each_entry_safe(iter, tmp, &flow_cfg->flow_list, list) {
1332 		list_del(&iter->list);
1333 		kfree(iter);
1334 		flow_cfg->nr_flows--;
1335 	}
1336 	return err;
1337 }
1338 
1339 int otx2_destroy_mcam_flows(struct otx2_nic *pfvf)
1340 {
1341 	struct otx2_flow_config *flow_cfg = pfvf->flow_cfg;
1342 	struct npc_mcam_free_entry_req *req;
1343 	struct otx2_flow *iter, *tmp;
1344 	int err;
1345 
1346 	if (!(pfvf->flags & OTX2_FLAG_MCAM_ENTRIES_ALLOC))
1347 		return 0;
1348 
1349 	/* remove all flows */
1350 	err = otx2_remove_flow_msg(pfvf, 0, true);
1351 	if (err)
1352 		return err;
1353 
1354 	list_for_each_entry_safe(iter, tmp, &flow_cfg->flow_list, list) {
1355 		list_del(&iter->list);
1356 		kfree(iter);
1357 		flow_cfg->nr_flows--;
1358 	}
1359 
1360 	mutex_lock(&pfvf->mbox.lock);
1361 	req = otx2_mbox_alloc_msg_npc_mcam_free_entry(&pfvf->mbox);
1362 	if (!req) {
1363 		mutex_unlock(&pfvf->mbox.lock);
1364 		return -ENOMEM;
1365 	}
1366 
1367 	req->all = 1;
1368 	/* Send message to AF to free MCAM entries */
1369 	err = otx2_sync_mbox_msg(&pfvf->mbox);
1370 	if (err) {
1371 		mutex_unlock(&pfvf->mbox.lock);
1372 		return err;
1373 	}
1374 
1375 	pfvf->flags &= ~OTX2_FLAG_MCAM_ENTRIES_ALLOC;
1376 	mutex_unlock(&pfvf->mbox.lock);
1377 
1378 	return 0;
1379 }
1380 
1381 int otx2_install_rxvlan_offload_flow(struct otx2_nic *pfvf)
1382 {
1383 	struct otx2_flow_config *flow_cfg = pfvf->flow_cfg;
1384 	struct npc_install_flow_req *req;
1385 	int err;
1386 
1387 	mutex_lock(&pfvf->mbox.lock);
1388 	req = otx2_mbox_alloc_msg_npc_install_flow(&pfvf->mbox);
1389 	if (!req) {
1390 		mutex_unlock(&pfvf->mbox.lock);
1391 		return -ENOMEM;
1392 	}
1393 
1394 	req->entry = flow_cfg->def_ent[flow_cfg->rx_vlan_offset];
1395 	req->intf = NIX_INTF_RX;
1396 	ether_addr_copy(req->packet.dmac, pfvf->netdev->dev_addr);
1397 	eth_broadcast_addr((u8 *)&req->mask.dmac);
1398 	req->channel = pfvf->hw.rx_chan_base;
1399 	req->op = NIX_RX_ACTION_DEFAULT;
1400 	req->features = BIT_ULL(NPC_OUTER_VID) | BIT_ULL(NPC_DMAC);
1401 	req->vtag0_valid = true;
1402 	req->vtag0_type = NIX_AF_LFX_RX_VTAG_TYPE0;
1403 
1404 	/* Send message to AF */
1405 	err = otx2_sync_mbox_msg(&pfvf->mbox);
1406 	mutex_unlock(&pfvf->mbox.lock);
1407 	return err;
1408 }
1409 
1410 static int otx2_delete_rxvlan_offload_flow(struct otx2_nic *pfvf)
1411 {
1412 	struct otx2_flow_config *flow_cfg = pfvf->flow_cfg;
1413 	struct npc_delete_flow_req *req;
1414 	int err;
1415 
1416 	mutex_lock(&pfvf->mbox.lock);
1417 	req = otx2_mbox_alloc_msg_npc_delete_flow(&pfvf->mbox);
1418 	if (!req) {
1419 		mutex_unlock(&pfvf->mbox.lock);
1420 		return -ENOMEM;
1421 	}
1422 
1423 	req->entry = flow_cfg->def_ent[flow_cfg->rx_vlan_offset];
1424 	/* Send message to AF */
1425 	err = otx2_sync_mbox_msg(&pfvf->mbox);
1426 	mutex_unlock(&pfvf->mbox.lock);
1427 	return err;
1428 }
1429 
1430 int otx2_enable_rxvlan(struct otx2_nic *pf, bool enable)
1431 {
1432 	struct nix_vtag_config *req;
1433 	struct mbox_msghdr *rsp_hdr;
1434 	int err;
1435 
1436 	/* Dont have enough mcam entries */
1437 	if (!(pf->flags & OTX2_FLAG_RX_VLAN_SUPPORT))
1438 		return -ENOMEM;
1439 
1440 	if (enable) {
1441 		err = otx2_install_rxvlan_offload_flow(pf);
1442 		if (err)
1443 			return err;
1444 	} else {
1445 		err = otx2_delete_rxvlan_offload_flow(pf);
1446 		if (err)
1447 			return err;
1448 	}
1449 
1450 	mutex_lock(&pf->mbox.lock);
1451 	req = otx2_mbox_alloc_msg_nix_vtag_cfg(&pf->mbox);
1452 	if (!req) {
1453 		mutex_unlock(&pf->mbox.lock);
1454 		return -ENOMEM;
1455 	}
1456 
1457 	/* config strip, capture and size */
1458 	req->vtag_size = VTAGSIZE_T4;
1459 	req->cfg_type = 1; /* rx vlan cfg */
1460 	req->rx.vtag_type = NIX_AF_LFX_RX_VTAG_TYPE0;
1461 	req->rx.strip_vtag = enable;
1462 	req->rx.capture_vtag = enable;
1463 
1464 	err = otx2_sync_mbox_msg(&pf->mbox);
1465 	if (err) {
1466 		mutex_unlock(&pf->mbox.lock);
1467 		return err;
1468 	}
1469 
1470 	rsp_hdr = otx2_mbox_get_rsp(&pf->mbox.mbox, 0, &req->hdr);
1471 	if (IS_ERR(rsp_hdr)) {
1472 		mutex_unlock(&pf->mbox.lock);
1473 		return PTR_ERR(rsp_hdr);
1474 	}
1475 
1476 	mutex_unlock(&pf->mbox.lock);
1477 	return rsp_hdr->rc;
1478 }
1479 
1480 void otx2_dmacflt_reinstall_flows(struct otx2_nic *pf)
1481 {
1482 	struct otx2_flow *iter;
1483 	struct ethhdr *eth_hdr;
1484 
1485 	list_for_each_entry(iter, &pf->flow_cfg->flow_list, list) {
1486 		if (iter->rule_type & DMAC_FILTER_RULE) {
1487 			eth_hdr = &iter->flow_spec.h_u.ether_spec;
1488 			otx2_dmacflt_add(pf, eth_hdr->h_dest,
1489 					 iter->entry);
1490 		}
1491 	}
1492 }
1493 
1494 void otx2_dmacflt_update_pfmac_flow(struct otx2_nic *pfvf)
1495 {
1496 	otx2_update_rem_pfmac(pfvf, DMAC_ADDR_UPDATE);
1497 }
1498