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