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