1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell RVU Admin Function driver
3  *
4  * Copyright (C) 2018 Marvell.
5  *
6  */
7 
8 #include <linux/module.h>
9 #include <linux/pci.h>
10 
11 #include "rvu_struct.h"
12 #include "rvu_reg.h"
13 #include "rvu.h"
14 #include "npc.h"
15 #include "cgx.h"
16 #include "lmac_common.h"
17 
18 static void nix_free_tx_vtag_entries(struct rvu *rvu, u16 pcifunc);
19 static int rvu_nix_get_bpid(struct rvu *rvu, struct nix_bp_cfg_req *req,
20 			    int type, int chan_id);
21 static int nix_update_mce_rule(struct rvu *rvu, u16 pcifunc,
22 			       int type, bool add);
23 static int nix_setup_ipolicers(struct rvu *rvu,
24 			       struct nix_hw *nix_hw, int blkaddr);
25 static void nix_ipolicer_freemem(struct rvu *rvu, struct nix_hw *nix_hw);
26 static int nix_verify_bandprof(struct nix_cn10k_aq_enq_req *req,
27 			       struct nix_hw *nix_hw, u16 pcifunc);
28 static int nix_free_all_bandprof(struct rvu *rvu, u16 pcifunc);
29 static void nix_clear_ratelimit_aggr(struct rvu *rvu, struct nix_hw *nix_hw,
30 				     u32 leaf_prof);
31 static const char *nix_get_ctx_name(int ctype);
32 
33 enum mc_tbl_sz {
34 	MC_TBL_SZ_256,
35 	MC_TBL_SZ_512,
36 	MC_TBL_SZ_1K,
37 	MC_TBL_SZ_2K,
38 	MC_TBL_SZ_4K,
39 	MC_TBL_SZ_8K,
40 	MC_TBL_SZ_16K,
41 	MC_TBL_SZ_32K,
42 	MC_TBL_SZ_64K,
43 };
44 
45 enum mc_buf_cnt {
46 	MC_BUF_CNT_8,
47 	MC_BUF_CNT_16,
48 	MC_BUF_CNT_32,
49 	MC_BUF_CNT_64,
50 	MC_BUF_CNT_128,
51 	MC_BUF_CNT_256,
52 	MC_BUF_CNT_512,
53 	MC_BUF_CNT_1024,
54 	MC_BUF_CNT_2048,
55 };
56 
57 enum nix_makr_fmt_indexes {
58 	NIX_MARK_CFG_IP_DSCP_RED,
59 	NIX_MARK_CFG_IP_DSCP_YELLOW,
60 	NIX_MARK_CFG_IP_DSCP_YELLOW_RED,
61 	NIX_MARK_CFG_IP_ECN_RED,
62 	NIX_MARK_CFG_IP_ECN_YELLOW,
63 	NIX_MARK_CFG_IP_ECN_YELLOW_RED,
64 	NIX_MARK_CFG_VLAN_DEI_RED,
65 	NIX_MARK_CFG_VLAN_DEI_YELLOW,
66 	NIX_MARK_CFG_VLAN_DEI_YELLOW_RED,
67 	NIX_MARK_CFG_MAX,
68 };
69 
70 /* For now considering MC resources needed for broadcast
71  * pkt replication only. i.e 256 HWVFs + 12 PFs.
72  */
73 #define MC_TBL_SIZE	MC_TBL_SZ_512
74 #define MC_BUF_CNT	MC_BUF_CNT_128
75 
76 struct mce {
77 	struct hlist_node	node;
78 	u16			pcifunc;
79 };
80 
81 int rvu_get_next_nix_blkaddr(struct rvu *rvu, int blkaddr)
82 {
83 	int i = 0;
84 
85 	/*If blkaddr is 0, return the first nix block address*/
86 	if (blkaddr == 0)
87 		return rvu->nix_blkaddr[blkaddr];
88 
89 	while (i + 1 < MAX_NIX_BLKS) {
90 		if (rvu->nix_blkaddr[i] == blkaddr)
91 			return rvu->nix_blkaddr[i + 1];
92 		i++;
93 	}
94 
95 	return 0;
96 }
97 
98 bool is_nixlf_attached(struct rvu *rvu, u16 pcifunc)
99 {
100 	struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
101 	int blkaddr;
102 
103 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
104 	if (!pfvf->nixlf || blkaddr < 0)
105 		return false;
106 	return true;
107 }
108 
109 int rvu_get_nixlf_count(struct rvu *rvu)
110 {
111 	int blkaddr = 0, max = 0;
112 	struct rvu_block *block;
113 
114 	blkaddr = rvu_get_next_nix_blkaddr(rvu, blkaddr);
115 	while (blkaddr) {
116 		block = &rvu->hw->block[blkaddr];
117 		max += block->lf.max;
118 		blkaddr = rvu_get_next_nix_blkaddr(rvu, blkaddr);
119 	}
120 	return max;
121 }
122 
123 int nix_get_nixlf(struct rvu *rvu, u16 pcifunc, int *nixlf, int *nix_blkaddr)
124 {
125 	struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
126 	struct rvu_hwinfo *hw = rvu->hw;
127 	int blkaddr;
128 
129 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
130 	if (!pfvf->nixlf || blkaddr < 0)
131 		return NIX_AF_ERR_AF_LF_INVALID;
132 
133 	*nixlf = rvu_get_lf(rvu, &hw->block[blkaddr], pcifunc, 0);
134 	if (*nixlf < 0)
135 		return NIX_AF_ERR_AF_LF_INVALID;
136 
137 	if (nix_blkaddr)
138 		*nix_blkaddr = blkaddr;
139 
140 	return 0;
141 }
142 
143 int nix_get_struct_ptrs(struct rvu *rvu, u16 pcifunc,
144 			struct nix_hw **nix_hw, int *blkaddr)
145 {
146 	struct rvu_pfvf *pfvf;
147 
148 	pfvf = rvu_get_pfvf(rvu, pcifunc);
149 	*blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
150 	if (!pfvf->nixlf || *blkaddr < 0)
151 		return NIX_AF_ERR_AF_LF_INVALID;
152 
153 	*nix_hw = get_nix_hw(rvu->hw, *blkaddr);
154 	if (!*nix_hw)
155 		return NIX_AF_ERR_INVALID_NIXBLK;
156 	return 0;
157 }
158 
159 static void nix_mce_list_init(struct nix_mce_list *list, int max)
160 {
161 	INIT_HLIST_HEAD(&list->head);
162 	list->count = 0;
163 	list->max = max;
164 }
165 
166 static u16 nix_alloc_mce_list(struct nix_mcast *mcast, int count)
167 {
168 	int idx;
169 
170 	if (!mcast)
171 		return 0;
172 
173 	idx = mcast->next_free_mce;
174 	mcast->next_free_mce += count;
175 	return idx;
176 }
177 
178 struct nix_hw *get_nix_hw(struct rvu_hwinfo *hw, int blkaddr)
179 {
180 	int nix_blkaddr = 0, i = 0;
181 	struct rvu *rvu = hw->rvu;
182 
183 	nix_blkaddr = rvu_get_next_nix_blkaddr(rvu, nix_blkaddr);
184 	while (nix_blkaddr) {
185 		if (blkaddr == nix_blkaddr && hw->nix)
186 			return &hw->nix[i];
187 		nix_blkaddr = rvu_get_next_nix_blkaddr(rvu, nix_blkaddr);
188 		i++;
189 	}
190 	return NULL;
191 }
192 
193 u32 convert_dwrr_mtu_to_bytes(u8 dwrr_mtu)
194 {
195 	dwrr_mtu &= 0x1FULL;
196 
197 	/* MTU used for DWRR calculation is in power of 2 up until 64K bytes.
198 	 * Value of 4 is reserved for MTU value of 9728 bytes.
199 	 * Value of 5 is reserved for MTU value of 10240 bytes.
200 	 */
201 	switch (dwrr_mtu) {
202 	case 4:
203 		return 9728;
204 	case 5:
205 		return 10240;
206 	default:
207 		return BIT_ULL(dwrr_mtu);
208 	}
209 
210 	return 0;
211 }
212 
213 u32 convert_bytes_to_dwrr_mtu(u32 bytes)
214 {
215 	/* MTU used for DWRR calculation is in power of 2 up until 64K bytes.
216 	 * Value of 4 is reserved for MTU value of 9728 bytes.
217 	 * Value of 5 is reserved for MTU value of 10240 bytes.
218 	 */
219 	if (bytes > BIT_ULL(16))
220 		return 0;
221 
222 	switch (bytes) {
223 	case 9728:
224 		return 4;
225 	case 10240:
226 		return 5;
227 	default:
228 		return ilog2(bytes);
229 	}
230 
231 	return 0;
232 }
233 
234 static void nix_rx_sync(struct rvu *rvu, int blkaddr)
235 {
236 	int err;
237 
238 	/* Sync all in flight RX packets to LLC/DRAM */
239 	rvu_write64(rvu, blkaddr, NIX_AF_RX_SW_SYNC, BIT_ULL(0));
240 	err = rvu_poll_reg(rvu, blkaddr, NIX_AF_RX_SW_SYNC, BIT_ULL(0), true);
241 	if (err)
242 		dev_err(rvu->dev, "SYNC1: NIX RX software sync failed\n");
243 
244 	/* SW_SYNC ensures all existing transactions are finished and pkts
245 	 * are written to LLC/DRAM, queues should be teared down after
246 	 * successful SW_SYNC. Due to a HW errata, in some rare scenarios
247 	 * an existing transaction might end after SW_SYNC operation. To
248 	 * ensure operation is fully done, do the SW_SYNC twice.
249 	 */
250 	rvu_write64(rvu, blkaddr, NIX_AF_RX_SW_SYNC, BIT_ULL(0));
251 	err = rvu_poll_reg(rvu, blkaddr, NIX_AF_RX_SW_SYNC, BIT_ULL(0), true);
252 	if (err)
253 		dev_err(rvu->dev, "SYNC2: NIX RX software sync failed\n");
254 }
255 
256 static bool is_valid_txschq(struct rvu *rvu, int blkaddr,
257 			    int lvl, u16 pcifunc, u16 schq)
258 {
259 	struct rvu_hwinfo *hw = rvu->hw;
260 	struct nix_txsch *txsch;
261 	struct nix_hw *nix_hw;
262 	u16 map_func;
263 
264 	nix_hw = get_nix_hw(rvu->hw, blkaddr);
265 	if (!nix_hw)
266 		return false;
267 
268 	txsch = &nix_hw->txsch[lvl];
269 	/* Check out of bounds */
270 	if (schq >= txsch->schq.max)
271 		return false;
272 
273 	mutex_lock(&rvu->rsrc_lock);
274 	map_func = TXSCH_MAP_FUNC(txsch->pfvf_map[schq]);
275 	mutex_unlock(&rvu->rsrc_lock);
276 
277 	/* TLs aggegating traffic are shared across PF and VFs */
278 	if (lvl >= hw->cap.nix_tx_aggr_lvl) {
279 		if (rvu_get_pf(map_func) != rvu_get_pf(pcifunc))
280 			return false;
281 		else
282 			return true;
283 	}
284 
285 	if (map_func != pcifunc)
286 		return false;
287 
288 	return true;
289 }
290 
291 static int nix_interface_init(struct rvu *rvu, u16 pcifunc, int type, int nixlf,
292 			      struct nix_lf_alloc_rsp *rsp, bool loop)
293 {
294 	struct rvu_pfvf *parent_pf, *pfvf = rvu_get_pfvf(rvu, pcifunc);
295 	u16 req_chan_base, req_chan_end, req_chan_cnt;
296 	struct rvu_hwinfo *hw = rvu->hw;
297 	struct sdp_node_info *sdp_info;
298 	int pkind, pf, vf, lbkid, vfid;
299 	struct mac_ops *mac_ops;
300 	u8 cgx_id, lmac_id;
301 	bool from_vf;
302 	int err;
303 
304 	pf = rvu_get_pf(pcifunc);
305 	if (!is_pf_cgxmapped(rvu, pf) && type != NIX_INTF_TYPE_LBK &&
306 	    type != NIX_INTF_TYPE_SDP)
307 		return 0;
308 
309 	switch (type) {
310 	case NIX_INTF_TYPE_CGX:
311 		pfvf->cgx_lmac = rvu->pf2cgxlmac_map[pf];
312 		rvu_get_cgx_lmac_id(pfvf->cgx_lmac, &cgx_id, &lmac_id);
313 
314 		pkind = rvu_npc_get_pkind(rvu, pf);
315 		if (pkind < 0) {
316 			dev_err(rvu->dev,
317 				"PF_Func 0x%x: Invalid pkind\n", pcifunc);
318 			return -EINVAL;
319 		}
320 		pfvf->rx_chan_base = rvu_nix_chan_cgx(rvu, cgx_id, lmac_id, 0);
321 		pfvf->tx_chan_base = pfvf->rx_chan_base;
322 		pfvf->rx_chan_cnt = 1;
323 		pfvf->tx_chan_cnt = 1;
324 		rsp->tx_link = cgx_id * hw->lmac_per_cgx + lmac_id;
325 
326 		cgx_set_pkind(rvu_cgx_pdata(cgx_id, rvu), lmac_id, pkind);
327 		rvu_npc_set_pkind(rvu, pkind, pfvf);
328 
329 		mac_ops = get_mac_ops(rvu_cgx_pdata(cgx_id, rvu));
330 
331 		/* By default we enable pause frames */
332 		if ((pcifunc & RVU_PFVF_FUNC_MASK) == 0)
333 			mac_ops->mac_enadis_pause_frm(rvu_cgx_pdata(cgx_id,
334 								    rvu),
335 						      lmac_id, true, true);
336 		break;
337 	case NIX_INTF_TYPE_LBK:
338 		vf = (pcifunc & RVU_PFVF_FUNC_MASK) - 1;
339 
340 		/* If NIX1 block is present on the silicon then NIXes are
341 		 * assigned alternatively for lbk interfaces. NIX0 should
342 		 * send packets on lbk link 1 channels and NIX1 should send
343 		 * on lbk link 0 channels for the communication between
344 		 * NIX0 and NIX1.
345 		 */
346 		lbkid = 0;
347 		if (rvu->hw->lbk_links > 1)
348 			lbkid = vf & 0x1 ? 0 : 1;
349 
350 		/* By default NIX0 is configured to send packet on lbk link 1
351 		 * (which corresponds to LBK1), same packet will receive on
352 		 * NIX1 over lbk link 0. If NIX1 sends packet on lbk link 0
353 		 * (which corresponds to LBK2) packet will receive on NIX0 lbk
354 		 * link 1.
355 		 * But if lbk links for NIX0 and NIX1 are negated, i.e NIX0
356 		 * transmits and receives on lbk link 0, whick corresponds
357 		 * to LBK1 block, back to back connectivity between NIX and
358 		 * LBK can be achieved (which is similar to 96xx)
359 		 *
360 		 *			RX		TX
361 		 * NIX0 lbk link	1 (LBK2)	1 (LBK1)
362 		 * NIX0 lbk link	0 (LBK0)	0 (LBK0)
363 		 * NIX1 lbk link	0 (LBK1)	0 (LBK2)
364 		 * NIX1 lbk link	1 (LBK3)	1 (LBK3)
365 		 */
366 		if (loop)
367 			lbkid = !lbkid;
368 
369 		/* Note that AF's VFs work in pairs and talk over consecutive
370 		 * loopback channels.Therefore if odd number of AF VFs are
371 		 * enabled then the last VF remains with no pair.
372 		 */
373 		pfvf->rx_chan_base = rvu_nix_chan_lbk(rvu, lbkid, vf);
374 		pfvf->tx_chan_base = vf & 0x1 ?
375 					rvu_nix_chan_lbk(rvu, lbkid, vf - 1) :
376 					rvu_nix_chan_lbk(rvu, lbkid, vf + 1);
377 		pfvf->rx_chan_cnt = 1;
378 		pfvf->tx_chan_cnt = 1;
379 		rsp->tx_link = hw->cgx_links + lbkid;
380 		pfvf->lbkid = lbkid;
381 		rvu_npc_set_pkind(rvu, NPC_RX_LBK_PKIND, pfvf);
382 		rvu_npc_install_promisc_entry(rvu, pcifunc, nixlf,
383 					      pfvf->rx_chan_base,
384 					      pfvf->rx_chan_cnt);
385 
386 		break;
387 	case NIX_INTF_TYPE_SDP:
388 		from_vf = !!(pcifunc & RVU_PFVF_FUNC_MASK);
389 		parent_pf = &rvu->pf[rvu_get_pf(pcifunc)];
390 		sdp_info = parent_pf->sdp_info;
391 		if (!sdp_info) {
392 			dev_err(rvu->dev, "Invalid sdp_info pointer\n");
393 			return -EINVAL;
394 		}
395 		if (from_vf) {
396 			req_chan_base = rvu_nix_chan_sdp(rvu, 0) + sdp_info->pf_srn +
397 				sdp_info->num_pf_rings;
398 			vf = (pcifunc & RVU_PFVF_FUNC_MASK) - 1;
399 			for (vfid = 0; vfid < vf; vfid++)
400 				req_chan_base += sdp_info->vf_rings[vfid];
401 			req_chan_cnt = sdp_info->vf_rings[vf];
402 			req_chan_end = req_chan_base + req_chan_cnt - 1;
403 			if (req_chan_base < rvu_nix_chan_sdp(rvu, 0) ||
404 			    req_chan_end > rvu_nix_chan_sdp(rvu, 255)) {
405 				dev_err(rvu->dev,
406 					"PF_Func 0x%x: Invalid channel base and count\n",
407 					pcifunc);
408 				return -EINVAL;
409 			}
410 		} else {
411 			req_chan_base = rvu_nix_chan_sdp(rvu, 0) + sdp_info->pf_srn;
412 			req_chan_cnt = sdp_info->num_pf_rings;
413 		}
414 
415 		pfvf->rx_chan_base = req_chan_base;
416 		pfvf->rx_chan_cnt = req_chan_cnt;
417 		pfvf->tx_chan_base = pfvf->rx_chan_base;
418 		pfvf->tx_chan_cnt = pfvf->rx_chan_cnt;
419 
420 		rsp->tx_link = hw->cgx_links + hw->lbk_links;
421 		rvu_npc_install_promisc_entry(rvu, pcifunc, nixlf,
422 					      pfvf->rx_chan_base,
423 					      pfvf->rx_chan_cnt);
424 		break;
425 	}
426 
427 	/* Add a UCAST forwarding rule in MCAM with this NIXLF attached
428 	 * RVU PF/VF's MAC address.
429 	 */
430 	rvu_npc_install_ucast_entry(rvu, pcifunc, nixlf,
431 				    pfvf->rx_chan_base, pfvf->mac_addr);
432 
433 	/* Add this PF_FUNC to bcast pkt replication list */
434 	err = nix_update_mce_rule(rvu, pcifunc, NIXLF_BCAST_ENTRY, true);
435 	if (err) {
436 		dev_err(rvu->dev,
437 			"Bcast list, failed to enable PF_FUNC 0x%x\n",
438 			pcifunc);
439 		return err;
440 	}
441 	/* Install MCAM rule matching Ethernet broadcast mac address */
442 	rvu_npc_install_bcast_match_entry(rvu, pcifunc,
443 					  nixlf, pfvf->rx_chan_base);
444 
445 	pfvf->maxlen = NIC_HW_MIN_FRS;
446 	pfvf->minlen = NIC_HW_MIN_FRS;
447 
448 	return 0;
449 }
450 
451 static void nix_interface_deinit(struct rvu *rvu, u16 pcifunc, u8 nixlf)
452 {
453 	struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
454 	int err;
455 
456 	pfvf->maxlen = 0;
457 	pfvf->minlen = 0;
458 
459 	/* Remove this PF_FUNC from bcast pkt replication list */
460 	err = nix_update_mce_rule(rvu, pcifunc, NIXLF_BCAST_ENTRY, false);
461 	if (err) {
462 		dev_err(rvu->dev,
463 			"Bcast list, failed to disable PF_FUNC 0x%x\n",
464 			pcifunc);
465 	}
466 
467 	/* Free and disable any MCAM entries used by this NIX LF */
468 	rvu_npc_disable_mcam_entries(rvu, pcifunc, nixlf);
469 
470 	/* Disable DMAC filters used */
471 	rvu_cgx_disable_dmac_entries(rvu, pcifunc);
472 }
473 
474 int rvu_mbox_handler_nix_bp_disable(struct rvu *rvu,
475 				    struct nix_bp_cfg_req *req,
476 				    struct msg_rsp *rsp)
477 {
478 	u16 pcifunc = req->hdr.pcifunc;
479 	struct rvu_pfvf *pfvf;
480 	int blkaddr, pf, type;
481 	u16 chan_base, chan;
482 	u64 cfg;
483 
484 	pf = rvu_get_pf(pcifunc);
485 	type = is_afvf(pcifunc) ? NIX_INTF_TYPE_LBK : NIX_INTF_TYPE_CGX;
486 	if (!is_pf_cgxmapped(rvu, pf) && type != NIX_INTF_TYPE_LBK)
487 		return 0;
488 
489 	pfvf = rvu_get_pfvf(rvu, pcifunc);
490 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
491 
492 	chan_base = pfvf->rx_chan_base + req->chan_base;
493 	for (chan = chan_base; chan < (chan_base + req->chan_cnt); chan++) {
494 		cfg = rvu_read64(rvu, blkaddr, NIX_AF_RX_CHANX_CFG(chan));
495 		rvu_write64(rvu, blkaddr, NIX_AF_RX_CHANX_CFG(chan),
496 			    cfg & ~BIT_ULL(16));
497 	}
498 	return 0;
499 }
500 
501 static int rvu_nix_get_bpid(struct rvu *rvu, struct nix_bp_cfg_req *req,
502 			    int type, int chan_id)
503 {
504 	int bpid, blkaddr, lmac_chan_cnt, sdp_chan_cnt;
505 	u16 cgx_bpid_cnt, lbk_bpid_cnt, sdp_bpid_cnt;
506 	struct rvu_hwinfo *hw = rvu->hw;
507 	struct rvu_pfvf *pfvf;
508 	u8 cgx_id, lmac_id;
509 	u64 cfg;
510 
511 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, req->hdr.pcifunc);
512 	cfg = rvu_read64(rvu, blkaddr, NIX_AF_CONST);
513 	lmac_chan_cnt = cfg & 0xFF;
514 
515 	cfg = rvu_read64(rvu, blkaddr, NIX_AF_CONST1);
516 	sdp_chan_cnt = cfg & 0xFFF;
517 
518 	cgx_bpid_cnt = hw->cgx_links * lmac_chan_cnt;
519 	lbk_bpid_cnt = hw->lbk_links * ((cfg >> 16) & 0xFF);
520 	sdp_bpid_cnt = hw->sdp_links * sdp_chan_cnt;
521 
522 	pfvf = rvu_get_pfvf(rvu, req->hdr.pcifunc);
523 
524 	/* Backpressure IDs range division
525 	 * CGX channles are mapped to (0 - 191) BPIDs
526 	 * LBK channles are mapped to (192 - 255) BPIDs
527 	 * SDP channles are mapped to (256 - 511) BPIDs
528 	 *
529 	 * Lmac channles and bpids mapped as follows
530 	 * cgx(0)_lmac(0)_chan(0 - 15) = bpid(0 - 15)
531 	 * cgx(0)_lmac(1)_chan(0 - 15) = bpid(16 - 31) ....
532 	 * cgx(1)_lmac(0)_chan(0 - 15) = bpid(64 - 79) ....
533 	 */
534 	switch (type) {
535 	case NIX_INTF_TYPE_CGX:
536 		if ((req->chan_base + req->chan_cnt) > 15)
537 			return -EINVAL;
538 		rvu_get_cgx_lmac_id(pfvf->cgx_lmac, &cgx_id, &lmac_id);
539 		/* Assign bpid based on cgx, lmac and chan id */
540 		bpid = (cgx_id * hw->lmac_per_cgx * lmac_chan_cnt) +
541 			(lmac_id * lmac_chan_cnt) + req->chan_base;
542 
543 		if (req->bpid_per_chan)
544 			bpid += chan_id;
545 		if (bpid > cgx_bpid_cnt)
546 			return -EINVAL;
547 		break;
548 
549 	case NIX_INTF_TYPE_LBK:
550 		if ((req->chan_base + req->chan_cnt) > 63)
551 			return -EINVAL;
552 		bpid = cgx_bpid_cnt + req->chan_base;
553 		if (req->bpid_per_chan)
554 			bpid += chan_id;
555 		if (bpid > (cgx_bpid_cnt + lbk_bpid_cnt))
556 			return -EINVAL;
557 		break;
558 	case NIX_INTF_TYPE_SDP:
559 		if ((req->chan_base + req->chan_cnt) > 255)
560 			return -EINVAL;
561 
562 		bpid = sdp_bpid_cnt + req->chan_base;
563 		if (req->bpid_per_chan)
564 			bpid += chan_id;
565 
566 		if (bpid > (cgx_bpid_cnt + lbk_bpid_cnt + sdp_bpid_cnt))
567 			return -EINVAL;
568 		break;
569 	default:
570 		return -EINVAL;
571 	}
572 	return bpid;
573 }
574 
575 int rvu_mbox_handler_nix_bp_enable(struct rvu *rvu,
576 				   struct nix_bp_cfg_req *req,
577 				   struct nix_bp_cfg_rsp *rsp)
578 {
579 	int blkaddr, pf, type, chan_id = 0;
580 	u16 pcifunc = req->hdr.pcifunc;
581 	struct rvu_pfvf *pfvf;
582 	u16 chan_base, chan;
583 	s16 bpid, bpid_base;
584 	u64 cfg;
585 
586 	pf = rvu_get_pf(pcifunc);
587 	type = is_afvf(pcifunc) ? NIX_INTF_TYPE_LBK : NIX_INTF_TYPE_CGX;
588 	if (is_sdp_pfvf(pcifunc))
589 		type = NIX_INTF_TYPE_SDP;
590 
591 	/* Enable backpressure only for CGX mapped PFs and LBK/SDP interface */
592 	if (!is_pf_cgxmapped(rvu, pf) && type != NIX_INTF_TYPE_LBK &&
593 	    type != NIX_INTF_TYPE_SDP)
594 		return 0;
595 
596 	pfvf = rvu_get_pfvf(rvu, pcifunc);
597 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
598 
599 	bpid_base = rvu_nix_get_bpid(rvu, req, type, chan_id);
600 	chan_base = pfvf->rx_chan_base + req->chan_base;
601 	bpid = bpid_base;
602 
603 	for (chan = chan_base; chan < (chan_base + req->chan_cnt); chan++) {
604 		if (bpid < 0) {
605 			dev_warn(rvu->dev, "Fail to enable backpressure\n");
606 			return -EINVAL;
607 		}
608 
609 		cfg = rvu_read64(rvu, blkaddr, NIX_AF_RX_CHANX_CFG(chan));
610 		cfg &= ~GENMASK_ULL(8, 0);
611 		rvu_write64(rvu, blkaddr, NIX_AF_RX_CHANX_CFG(chan),
612 			    cfg | (bpid & GENMASK_ULL(8, 0)) | BIT_ULL(16));
613 		chan_id++;
614 		bpid = rvu_nix_get_bpid(rvu, req, type, chan_id);
615 	}
616 
617 	for (chan = 0; chan < req->chan_cnt; chan++) {
618 		/* Map channel and bpid assign to it */
619 		rsp->chan_bpid[chan] = ((req->chan_base + chan) & 0x7F) << 10 |
620 					(bpid_base & 0x3FF);
621 		if (req->bpid_per_chan)
622 			bpid_base++;
623 	}
624 	rsp->chan_cnt = req->chan_cnt;
625 
626 	return 0;
627 }
628 
629 static void nix_setup_lso_tso_l3(struct rvu *rvu, int blkaddr,
630 				 u64 format, bool v4, u64 *fidx)
631 {
632 	struct nix_lso_format field = {0};
633 
634 	/* IP's Length field */
635 	field.layer = NIX_TXLAYER_OL3;
636 	/* In ipv4, length field is at offset 2 bytes, for ipv6 it's 4 */
637 	field.offset = v4 ? 2 : 4;
638 	field.sizem1 = 1; /* i.e 2 bytes */
639 	field.alg = NIX_LSOALG_ADD_PAYLEN;
640 	rvu_write64(rvu, blkaddr,
641 		    NIX_AF_LSO_FORMATX_FIELDX(format, (*fidx)++),
642 		    *(u64 *)&field);
643 
644 	/* No ID field in IPv6 header */
645 	if (!v4)
646 		return;
647 
648 	/* IP's ID field */
649 	field.layer = NIX_TXLAYER_OL3;
650 	field.offset = 4;
651 	field.sizem1 = 1; /* i.e 2 bytes */
652 	field.alg = NIX_LSOALG_ADD_SEGNUM;
653 	rvu_write64(rvu, blkaddr,
654 		    NIX_AF_LSO_FORMATX_FIELDX(format, (*fidx)++),
655 		    *(u64 *)&field);
656 }
657 
658 static void nix_setup_lso_tso_l4(struct rvu *rvu, int blkaddr,
659 				 u64 format, u64 *fidx)
660 {
661 	struct nix_lso_format field = {0};
662 
663 	/* TCP's sequence number field */
664 	field.layer = NIX_TXLAYER_OL4;
665 	field.offset = 4;
666 	field.sizem1 = 3; /* i.e 4 bytes */
667 	field.alg = NIX_LSOALG_ADD_OFFSET;
668 	rvu_write64(rvu, blkaddr,
669 		    NIX_AF_LSO_FORMATX_FIELDX(format, (*fidx)++),
670 		    *(u64 *)&field);
671 
672 	/* TCP's flags field */
673 	field.layer = NIX_TXLAYER_OL4;
674 	field.offset = 12;
675 	field.sizem1 = 1; /* 2 bytes */
676 	field.alg = NIX_LSOALG_TCP_FLAGS;
677 	rvu_write64(rvu, blkaddr,
678 		    NIX_AF_LSO_FORMATX_FIELDX(format, (*fidx)++),
679 		    *(u64 *)&field);
680 }
681 
682 static void nix_setup_lso(struct rvu *rvu, struct nix_hw *nix_hw, int blkaddr)
683 {
684 	u64 cfg, idx, fidx = 0;
685 
686 	/* Get max HW supported format indices */
687 	cfg = (rvu_read64(rvu, blkaddr, NIX_AF_CONST1) >> 48) & 0xFF;
688 	nix_hw->lso.total = cfg;
689 
690 	/* Enable LSO */
691 	cfg = rvu_read64(rvu, blkaddr, NIX_AF_LSO_CFG);
692 	/* For TSO, set first and middle segment flags to
693 	 * mask out PSH, RST & FIN flags in TCP packet
694 	 */
695 	cfg &= ~((0xFFFFULL << 32) | (0xFFFFULL << 16));
696 	cfg |= (0xFFF2ULL << 32) | (0xFFF2ULL << 16);
697 	rvu_write64(rvu, blkaddr, NIX_AF_LSO_CFG, cfg | BIT_ULL(63));
698 
699 	/* Setup default static LSO formats
700 	 *
701 	 * Configure format fields for TCPv4 segmentation offload
702 	 */
703 	idx = NIX_LSO_FORMAT_IDX_TSOV4;
704 	nix_setup_lso_tso_l3(rvu, blkaddr, idx, true, &fidx);
705 	nix_setup_lso_tso_l4(rvu, blkaddr, idx, &fidx);
706 
707 	/* Set rest of the fields to NOP */
708 	for (; fidx < 8; fidx++) {
709 		rvu_write64(rvu, blkaddr,
710 			    NIX_AF_LSO_FORMATX_FIELDX(idx, fidx), 0x0ULL);
711 	}
712 	nix_hw->lso.in_use++;
713 
714 	/* Configure format fields for TCPv6 segmentation offload */
715 	idx = NIX_LSO_FORMAT_IDX_TSOV6;
716 	fidx = 0;
717 	nix_setup_lso_tso_l3(rvu, blkaddr, idx, false, &fidx);
718 	nix_setup_lso_tso_l4(rvu, blkaddr, idx, &fidx);
719 
720 	/* Set rest of the fields to NOP */
721 	for (; fidx < 8; fidx++) {
722 		rvu_write64(rvu, blkaddr,
723 			    NIX_AF_LSO_FORMATX_FIELDX(idx, fidx), 0x0ULL);
724 	}
725 	nix_hw->lso.in_use++;
726 }
727 
728 static void nix_ctx_free(struct rvu *rvu, struct rvu_pfvf *pfvf)
729 {
730 	kfree(pfvf->rq_bmap);
731 	kfree(pfvf->sq_bmap);
732 	kfree(pfvf->cq_bmap);
733 	if (pfvf->rq_ctx)
734 		qmem_free(rvu->dev, pfvf->rq_ctx);
735 	if (pfvf->sq_ctx)
736 		qmem_free(rvu->dev, pfvf->sq_ctx);
737 	if (pfvf->cq_ctx)
738 		qmem_free(rvu->dev, pfvf->cq_ctx);
739 	if (pfvf->rss_ctx)
740 		qmem_free(rvu->dev, pfvf->rss_ctx);
741 	if (pfvf->nix_qints_ctx)
742 		qmem_free(rvu->dev, pfvf->nix_qints_ctx);
743 	if (pfvf->cq_ints_ctx)
744 		qmem_free(rvu->dev, pfvf->cq_ints_ctx);
745 
746 	pfvf->rq_bmap = NULL;
747 	pfvf->cq_bmap = NULL;
748 	pfvf->sq_bmap = NULL;
749 	pfvf->rq_ctx = NULL;
750 	pfvf->sq_ctx = NULL;
751 	pfvf->cq_ctx = NULL;
752 	pfvf->rss_ctx = NULL;
753 	pfvf->nix_qints_ctx = NULL;
754 	pfvf->cq_ints_ctx = NULL;
755 }
756 
757 static int nixlf_rss_ctx_init(struct rvu *rvu, int blkaddr,
758 			      struct rvu_pfvf *pfvf, int nixlf,
759 			      int rss_sz, int rss_grps, int hwctx_size,
760 			      u64 way_mask, bool tag_lsb_as_adder)
761 {
762 	int err, grp, num_indices;
763 	u64 val;
764 
765 	/* RSS is not requested for this NIXLF */
766 	if (!rss_sz)
767 		return 0;
768 	num_indices = rss_sz * rss_grps;
769 
770 	/* Alloc NIX RSS HW context memory and config the base */
771 	err = qmem_alloc(rvu->dev, &pfvf->rss_ctx, num_indices, hwctx_size);
772 	if (err)
773 		return err;
774 
775 	rvu_write64(rvu, blkaddr, NIX_AF_LFX_RSS_BASE(nixlf),
776 		    (u64)pfvf->rss_ctx->iova);
777 
778 	/* Config full RSS table size, enable RSS and caching */
779 	val = BIT_ULL(36) | BIT_ULL(4) | way_mask << 20 |
780 			ilog2(num_indices / MAX_RSS_INDIR_TBL_SIZE);
781 
782 	if (tag_lsb_as_adder)
783 		val |= BIT_ULL(5);
784 
785 	rvu_write64(rvu, blkaddr, NIX_AF_LFX_RSS_CFG(nixlf), val);
786 	/* Config RSS group offset and sizes */
787 	for (grp = 0; grp < rss_grps; grp++)
788 		rvu_write64(rvu, blkaddr, NIX_AF_LFX_RSS_GRPX(nixlf, grp),
789 			    ((ilog2(rss_sz) - 1) << 16) | (rss_sz * grp));
790 	return 0;
791 }
792 
793 static int nix_aq_enqueue_wait(struct rvu *rvu, struct rvu_block *block,
794 			       struct nix_aq_inst_s *inst)
795 {
796 	struct admin_queue *aq = block->aq;
797 	struct nix_aq_res_s *result;
798 	int timeout = 1000;
799 	u64 reg, head;
800 
801 	result = (struct nix_aq_res_s *)aq->res->base;
802 
803 	/* Get current head pointer where to append this instruction */
804 	reg = rvu_read64(rvu, block->addr, NIX_AF_AQ_STATUS);
805 	head = (reg >> 4) & AQ_PTR_MASK;
806 
807 	memcpy((void *)(aq->inst->base + (head * aq->inst->entry_sz)),
808 	       (void *)inst, aq->inst->entry_sz);
809 	memset(result, 0, sizeof(*result));
810 	/* sync into memory */
811 	wmb();
812 
813 	/* Ring the doorbell and wait for result */
814 	rvu_write64(rvu, block->addr, NIX_AF_AQ_DOOR, 1);
815 	while (result->compcode == NIX_AQ_COMP_NOTDONE) {
816 		cpu_relax();
817 		udelay(1);
818 		timeout--;
819 		if (!timeout)
820 			return -EBUSY;
821 	}
822 
823 	if (result->compcode != NIX_AQ_COMP_GOOD)
824 		/* TODO: Replace this with some error code */
825 		return -EBUSY;
826 
827 	return 0;
828 }
829 
830 static int rvu_nix_blk_aq_enq_inst(struct rvu *rvu, struct nix_hw *nix_hw,
831 				   struct nix_aq_enq_req *req,
832 				   struct nix_aq_enq_rsp *rsp)
833 {
834 	struct rvu_hwinfo *hw = rvu->hw;
835 	u16 pcifunc = req->hdr.pcifunc;
836 	int nixlf, blkaddr, rc = 0;
837 	struct nix_aq_inst_s inst;
838 	struct rvu_block *block;
839 	struct admin_queue *aq;
840 	struct rvu_pfvf *pfvf;
841 	void *ctx, *mask;
842 	bool ena;
843 	u64 cfg;
844 
845 	blkaddr = nix_hw->blkaddr;
846 	block = &hw->block[blkaddr];
847 	aq = block->aq;
848 	if (!aq) {
849 		dev_warn(rvu->dev, "%s: NIX AQ not initialized\n", __func__);
850 		return NIX_AF_ERR_AQ_ENQUEUE;
851 	}
852 
853 	pfvf = rvu_get_pfvf(rvu, pcifunc);
854 	nixlf = rvu_get_lf(rvu, block, pcifunc, 0);
855 
856 	/* Skip NIXLF check for broadcast MCE entry and bandwidth profile
857 	 * operations done by AF itself.
858 	 */
859 	if (!((!rsp && req->ctype == NIX_AQ_CTYPE_MCE) ||
860 	      (req->ctype == NIX_AQ_CTYPE_BANDPROF && !pcifunc))) {
861 		if (!pfvf->nixlf || nixlf < 0)
862 			return NIX_AF_ERR_AF_LF_INVALID;
863 	}
864 
865 	switch (req->ctype) {
866 	case NIX_AQ_CTYPE_RQ:
867 		/* Check if index exceeds max no of queues */
868 		if (!pfvf->rq_ctx || req->qidx >= pfvf->rq_ctx->qsize)
869 			rc = NIX_AF_ERR_AQ_ENQUEUE;
870 		break;
871 	case NIX_AQ_CTYPE_SQ:
872 		if (!pfvf->sq_ctx || req->qidx >= pfvf->sq_ctx->qsize)
873 			rc = NIX_AF_ERR_AQ_ENQUEUE;
874 		break;
875 	case NIX_AQ_CTYPE_CQ:
876 		if (!pfvf->cq_ctx || req->qidx >= pfvf->cq_ctx->qsize)
877 			rc = NIX_AF_ERR_AQ_ENQUEUE;
878 		break;
879 	case NIX_AQ_CTYPE_RSS:
880 		/* Check if RSS is enabled and qidx is within range */
881 		cfg = rvu_read64(rvu, blkaddr, NIX_AF_LFX_RSS_CFG(nixlf));
882 		if (!(cfg & BIT_ULL(4)) || !pfvf->rss_ctx ||
883 		    (req->qidx >= (256UL << (cfg & 0xF))))
884 			rc = NIX_AF_ERR_AQ_ENQUEUE;
885 		break;
886 	case NIX_AQ_CTYPE_MCE:
887 		cfg = rvu_read64(rvu, blkaddr, NIX_AF_RX_MCAST_CFG);
888 
889 		/* Check if index exceeds MCE list length */
890 		if (!nix_hw->mcast.mce_ctx ||
891 		    (req->qidx >= (256UL << (cfg & 0xF))))
892 			rc = NIX_AF_ERR_AQ_ENQUEUE;
893 
894 		/* Adding multicast lists for requests from PF/VFs is not
895 		 * yet supported, so ignore this.
896 		 */
897 		if (rsp)
898 			rc = NIX_AF_ERR_AQ_ENQUEUE;
899 		break;
900 	case NIX_AQ_CTYPE_BANDPROF:
901 		if (nix_verify_bandprof((struct nix_cn10k_aq_enq_req *)req,
902 					nix_hw, pcifunc))
903 			rc = NIX_AF_ERR_INVALID_BANDPROF;
904 		break;
905 	default:
906 		rc = NIX_AF_ERR_AQ_ENQUEUE;
907 	}
908 
909 	if (rc)
910 		return rc;
911 
912 	/* Check if SQ pointed SMQ belongs to this PF/VF or not */
913 	if (req->ctype == NIX_AQ_CTYPE_SQ &&
914 	    ((req->op == NIX_AQ_INSTOP_INIT && req->sq.ena) ||
915 	     (req->op == NIX_AQ_INSTOP_WRITE &&
916 	      req->sq_mask.ena && req->sq_mask.smq && req->sq.ena))) {
917 		if (!is_valid_txschq(rvu, blkaddr, NIX_TXSCH_LVL_SMQ,
918 				     pcifunc, req->sq.smq))
919 			return NIX_AF_ERR_AQ_ENQUEUE;
920 	}
921 
922 	memset(&inst, 0, sizeof(struct nix_aq_inst_s));
923 	inst.lf = nixlf;
924 	inst.cindex = req->qidx;
925 	inst.ctype = req->ctype;
926 	inst.op = req->op;
927 	/* Currently we are not supporting enqueuing multiple instructions,
928 	 * so always choose first entry in result memory.
929 	 */
930 	inst.res_addr = (u64)aq->res->iova;
931 
932 	/* Hardware uses same aq->res->base for updating result of
933 	 * previous instruction hence wait here till it is done.
934 	 */
935 	spin_lock(&aq->lock);
936 
937 	/* Clean result + context memory */
938 	memset(aq->res->base, 0, aq->res->entry_sz);
939 	/* Context needs to be written at RES_ADDR + 128 */
940 	ctx = aq->res->base + 128;
941 	/* Mask needs to be written at RES_ADDR + 256 */
942 	mask = aq->res->base + 256;
943 
944 	switch (req->op) {
945 	case NIX_AQ_INSTOP_WRITE:
946 		if (req->ctype == NIX_AQ_CTYPE_RQ)
947 			memcpy(mask, &req->rq_mask,
948 			       sizeof(struct nix_rq_ctx_s));
949 		else if (req->ctype == NIX_AQ_CTYPE_SQ)
950 			memcpy(mask, &req->sq_mask,
951 			       sizeof(struct nix_sq_ctx_s));
952 		else if (req->ctype == NIX_AQ_CTYPE_CQ)
953 			memcpy(mask, &req->cq_mask,
954 			       sizeof(struct nix_cq_ctx_s));
955 		else if (req->ctype == NIX_AQ_CTYPE_RSS)
956 			memcpy(mask, &req->rss_mask,
957 			       sizeof(struct nix_rsse_s));
958 		else if (req->ctype == NIX_AQ_CTYPE_MCE)
959 			memcpy(mask, &req->mce_mask,
960 			       sizeof(struct nix_rx_mce_s));
961 		else if (req->ctype == NIX_AQ_CTYPE_BANDPROF)
962 			memcpy(mask, &req->prof_mask,
963 			       sizeof(struct nix_bandprof_s));
964 		fallthrough;
965 	case NIX_AQ_INSTOP_INIT:
966 		if (req->ctype == NIX_AQ_CTYPE_RQ)
967 			memcpy(ctx, &req->rq, sizeof(struct nix_rq_ctx_s));
968 		else if (req->ctype == NIX_AQ_CTYPE_SQ)
969 			memcpy(ctx, &req->sq, sizeof(struct nix_sq_ctx_s));
970 		else if (req->ctype == NIX_AQ_CTYPE_CQ)
971 			memcpy(ctx, &req->cq, sizeof(struct nix_cq_ctx_s));
972 		else if (req->ctype == NIX_AQ_CTYPE_RSS)
973 			memcpy(ctx, &req->rss, sizeof(struct nix_rsse_s));
974 		else if (req->ctype == NIX_AQ_CTYPE_MCE)
975 			memcpy(ctx, &req->mce, sizeof(struct nix_rx_mce_s));
976 		else if (req->ctype == NIX_AQ_CTYPE_BANDPROF)
977 			memcpy(ctx, &req->prof, sizeof(struct nix_bandprof_s));
978 		break;
979 	case NIX_AQ_INSTOP_NOP:
980 	case NIX_AQ_INSTOP_READ:
981 	case NIX_AQ_INSTOP_LOCK:
982 	case NIX_AQ_INSTOP_UNLOCK:
983 		break;
984 	default:
985 		rc = NIX_AF_ERR_AQ_ENQUEUE;
986 		spin_unlock(&aq->lock);
987 		return rc;
988 	}
989 
990 	/* Submit the instruction to AQ */
991 	rc = nix_aq_enqueue_wait(rvu, block, &inst);
992 	if (rc) {
993 		spin_unlock(&aq->lock);
994 		return rc;
995 	}
996 
997 	/* Set RQ/SQ/CQ bitmap if respective queue hw context is enabled */
998 	if (req->op == NIX_AQ_INSTOP_INIT) {
999 		if (req->ctype == NIX_AQ_CTYPE_RQ && req->rq.ena)
1000 			__set_bit(req->qidx, pfvf->rq_bmap);
1001 		if (req->ctype == NIX_AQ_CTYPE_SQ && req->sq.ena)
1002 			__set_bit(req->qidx, pfvf->sq_bmap);
1003 		if (req->ctype == NIX_AQ_CTYPE_CQ && req->cq.ena)
1004 			__set_bit(req->qidx, pfvf->cq_bmap);
1005 	}
1006 
1007 	if (req->op == NIX_AQ_INSTOP_WRITE) {
1008 		if (req->ctype == NIX_AQ_CTYPE_RQ) {
1009 			ena = (req->rq.ena & req->rq_mask.ena) |
1010 				(test_bit(req->qidx, pfvf->rq_bmap) &
1011 				~req->rq_mask.ena);
1012 			if (ena)
1013 				__set_bit(req->qidx, pfvf->rq_bmap);
1014 			else
1015 				__clear_bit(req->qidx, pfvf->rq_bmap);
1016 		}
1017 		if (req->ctype == NIX_AQ_CTYPE_SQ) {
1018 			ena = (req->rq.ena & req->sq_mask.ena) |
1019 				(test_bit(req->qidx, pfvf->sq_bmap) &
1020 				~req->sq_mask.ena);
1021 			if (ena)
1022 				__set_bit(req->qidx, pfvf->sq_bmap);
1023 			else
1024 				__clear_bit(req->qidx, pfvf->sq_bmap);
1025 		}
1026 		if (req->ctype == NIX_AQ_CTYPE_CQ) {
1027 			ena = (req->rq.ena & req->cq_mask.ena) |
1028 				(test_bit(req->qidx, pfvf->cq_bmap) &
1029 				~req->cq_mask.ena);
1030 			if (ena)
1031 				__set_bit(req->qidx, pfvf->cq_bmap);
1032 			else
1033 				__clear_bit(req->qidx, pfvf->cq_bmap);
1034 		}
1035 	}
1036 
1037 	if (rsp) {
1038 		/* Copy read context into mailbox */
1039 		if (req->op == NIX_AQ_INSTOP_READ) {
1040 			if (req->ctype == NIX_AQ_CTYPE_RQ)
1041 				memcpy(&rsp->rq, ctx,
1042 				       sizeof(struct nix_rq_ctx_s));
1043 			else if (req->ctype == NIX_AQ_CTYPE_SQ)
1044 				memcpy(&rsp->sq, ctx,
1045 				       sizeof(struct nix_sq_ctx_s));
1046 			else if (req->ctype == NIX_AQ_CTYPE_CQ)
1047 				memcpy(&rsp->cq, ctx,
1048 				       sizeof(struct nix_cq_ctx_s));
1049 			else if (req->ctype == NIX_AQ_CTYPE_RSS)
1050 				memcpy(&rsp->rss, ctx,
1051 				       sizeof(struct nix_rsse_s));
1052 			else if (req->ctype == NIX_AQ_CTYPE_MCE)
1053 				memcpy(&rsp->mce, ctx,
1054 				       sizeof(struct nix_rx_mce_s));
1055 			else if (req->ctype == NIX_AQ_CTYPE_BANDPROF)
1056 				memcpy(&rsp->prof, ctx,
1057 				       sizeof(struct nix_bandprof_s));
1058 		}
1059 	}
1060 
1061 	spin_unlock(&aq->lock);
1062 	return 0;
1063 }
1064 
1065 static int rvu_nix_verify_aq_ctx(struct rvu *rvu, struct nix_hw *nix_hw,
1066 				 struct nix_aq_enq_req *req, u8 ctype)
1067 {
1068 	struct nix_cn10k_aq_enq_req aq_req;
1069 	struct nix_cn10k_aq_enq_rsp aq_rsp;
1070 	int rc, word;
1071 
1072 	if (req->ctype != NIX_AQ_CTYPE_CQ)
1073 		return 0;
1074 
1075 	rc = nix_aq_context_read(rvu, nix_hw, &aq_req, &aq_rsp,
1076 				 req->hdr.pcifunc, ctype, req->qidx);
1077 	if (rc) {
1078 		dev_err(rvu->dev,
1079 			"%s: Failed to fetch %s%d context of PFFUNC 0x%x\n",
1080 			__func__, nix_get_ctx_name(ctype), req->qidx,
1081 			req->hdr.pcifunc);
1082 		return rc;
1083 	}
1084 
1085 	/* Make copy of original context & mask which are required
1086 	 * for resubmission
1087 	 */
1088 	memcpy(&aq_req.cq_mask, &req->cq_mask, sizeof(struct nix_cq_ctx_s));
1089 	memcpy(&aq_req.cq, &req->cq, sizeof(struct nix_cq_ctx_s));
1090 
1091 	/* exclude fields which HW can update */
1092 	aq_req.cq_mask.cq_err       = 0;
1093 	aq_req.cq_mask.wrptr        = 0;
1094 	aq_req.cq_mask.tail         = 0;
1095 	aq_req.cq_mask.head	    = 0;
1096 	aq_req.cq_mask.avg_level    = 0;
1097 	aq_req.cq_mask.update_time  = 0;
1098 	aq_req.cq_mask.substream    = 0;
1099 
1100 	/* Context mask (cq_mask) holds mask value of fields which
1101 	 * are changed in AQ WRITE operation.
1102 	 * for example cq.drop = 0xa;
1103 	 *	       cq_mask.drop = 0xff;
1104 	 * Below logic performs '&' between cq and cq_mask so that non
1105 	 * updated fields are masked out for request and response
1106 	 * comparison
1107 	 */
1108 	for (word = 0; word < sizeof(struct nix_cq_ctx_s) / sizeof(u64);
1109 	     word++) {
1110 		*(u64 *)((u8 *)&aq_rsp.cq + word * 8) &=
1111 			(*(u64 *)((u8 *)&aq_req.cq_mask + word * 8));
1112 		*(u64 *)((u8 *)&aq_req.cq + word * 8) &=
1113 			(*(u64 *)((u8 *)&aq_req.cq_mask + word * 8));
1114 	}
1115 
1116 	if (memcmp(&aq_req.cq, &aq_rsp.cq, sizeof(struct nix_cq_ctx_s)))
1117 		return NIX_AF_ERR_AQ_CTX_RETRY_WRITE;
1118 
1119 	return 0;
1120 }
1121 
1122 static int rvu_nix_aq_enq_inst(struct rvu *rvu, struct nix_aq_enq_req *req,
1123 			       struct nix_aq_enq_rsp *rsp)
1124 {
1125 	struct nix_hw *nix_hw;
1126 	int err, retries = 5;
1127 	int blkaddr;
1128 
1129 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, req->hdr.pcifunc);
1130 	if (blkaddr < 0)
1131 		return NIX_AF_ERR_AF_LF_INVALID;
1132 
1133 	nix_hw =  get_nix_hw(rvu->hw, blkaddr);
1134 	if (!nix_hw)
1135 		return NIX_AF_ERR_INVALID_NIXBLK;
1136 
1137 retry:
1138 	err = rvu_nix_blk_aq_enq_inst(rvu, nix_hw, req, rsp);
1139 
1140 	/* HW errata 'AQ Modification to CQ could be discarded on heavy traffic'
1141 	 * As a work around perfrom CQ context read after each AQ write. If AQ
1142 	 * read shows AQ write is not updated perform AQ write again.
1143 	 */
1144 	if (!err && req->op == NIX_AQ_INSTOP_WRITE) {
1145 		err = rvu_nix_verify_aq_ctx(rvu, nix_hw, req, NIX_AQ_CTYPE_CQ);
1146 		if (err == NIX_AF_ERR_AQ_CTX_RETRY_WRITE) {
1147 			if (retries--)
1148 				goto retry;
1149 			else
1150 				return NIX_AF_ERR_CQ_CTX_WRITE_ERR;
1151 		}
1152 	}
1153 
1154 	return err;
1155 }
1156 
1157 static const char *nix_get_ctx_name(int ctype)
1158 {
1159 	switch (ctype) {
1160 	case NIX_AQ_CTYPE_CQ:
1161 		return "CQ";
1162 	case NIX_AQ_CTYPE_SQ:
1163 		return "SQ";
1164 	case NIX_AQ_CTYPE_RQ:
1165 		return "RQ";
1166 	case NIX_AQ_CTYPE_RSS:
1167 		return "RSS";
1168 	}
1169 	return "";
1170 }
1171 
1172 static int nix_lf_hwctx_disable(struct rvu *rvu, struct hwctx_disable_req *req)
1173 {
1174 	struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, req->hdr.pcifunc);
1175 	struct nix_aq_enq_req aq_req;
1176 	unsigned long *bmap;
1177 	int qidx, q_cnt = 0;
1178 	int err = 0, rc;
1179 
1180 	if (!pfvf->cq_ctx || !pfvf->sq_ctx || !pfvf->rq_ctx)
1181 		return NIX_AF_ERR_AQ_ENQUEUE;
1182 
1183 	memset(&aq_req, 0, sizeof(struct nix_aq_enq_req));
1184 	aq_req.hdr.pcifunc = req->hdr.pcifunc;
1185 
1186 	if (req->ctype == NIX_AQ_CTYPE_CQ) {
1187 		aq_req.cq.ena = 0;
1188 		aq_req.cq_mask.ena = 1;
1189 		aq_req.cq.bp_ena = 0;
1190 		aq_req.cq_mask.bp_ena = 1;
1191 		q_cnt = pfvf->cq_ctx->qsize;
1192 		bmap = pfvf->cq_bmap;
1193 	}
1194 	if (req->ctype == NIX_AQ_CTYPE_SQ) {
1195 		aq_req.sq.ena = 0;
1196 		aq_req.sq_mask.ena = 1;
1197 		q_cnt = pfvf->sq_ctx->qsize;
1198 		bmap = pfvf->sq_bmap;
1199 	}
1200 	if (req->ctype == NIX_AQ_CTYPE_RQ) {
1201 		aq_req.rq.ena = 0;
1202 		aq_req.rq_mask.ena = 1;
1203 		q_cnt = pfvf->rq_ctx->qsize;
1204 		bmap = pfvf->rq_bmap;
1205 	}
1206 
1207 	aq_req.ctype = req->ctype;
1208 	aq_req.op = NIX_AQ_INSTOP_WRITE;
1209 
1210 	for (qidx = 0; qidx < q_cnt; qidx++) {
1211 		if (!test_bit(qidx, bmap))
1212 			continue;
1213 		aq_req.qidx = qidx;
1214 		rc = rvu_nix_aq_enq_inst(rvu, &aq_req, NULL);
1215 		if (rc) {
1216 			err = rc;
1217 			dev_err(rvu->dev, "Failed to disable %s:%d context\n",
1218 				nix_get_ctx_name(req->ctype), qidx);
1219 		}
1220 	}
1221 
1222 	return err;
1223 }
1224 
1225 #ifdef CONFIG_NDC_DIS_DYNAMIC_CACHING
1226 static int nix_lf_hwctx_lockdown(struct rvu *rvu, struct nix_aq_enq_req *req)
1227 {
1228 	struct nix_aq_enq_req lock_ctx_req;
1229 	int err;
1230 
1231 	if (req->op != NIX_AQ_INSTOP_INIT)
1232 		return 0;
1233 
1234 	if (req->ctype == NIX_AQ_CTYPE_MCE ||
1235 	    req->ctype == NIX_AQ_CTYPE_DYNO)
1236 		return 0;
1237 
1238 	memset(&lock_ctx_req, 0, sizeof(struct nix_aq_enq_req));
1239 	lock_ctx_req.hdr.pcifunc = req->hdr.pcifunc;
1240 	lock_ctx_req.ctype = req->ctype;
1241 	lock_ctx_req.op = NIX_AQ_INSTOP_LOCK;
1242 	lock_ctx_req.qidx = req->qidx;
1243 	err = rvu_nix_aq_enq_inst(rvu, &lock_ctx_req, NULL);
1244 	if (err)
1245 		dev_err(rvu->dev,
1246 			"PFUNC 0x%x: Failed to lock NIX %s:%d context\n",
1247 			req->hdr.pcifunc,
1248 			nix_get_ctx_name(req->ctype), req->qidx);
1249 	return err;
1250 }
1251 
1252 int rvu_mbox_handler_nix_aq_enq(struct rvu *rvu,
1253 				struct nix_aq_enq_req *req,
1254 				struct nix_aq_enq_rsp *rsp)
1255 {
1256 	int err;
1257 
1258 	err = rvu_nix_aq_enq_inst(rvu, req, rsp);
1259 	if (!err)
1260 		err = nix_lf_hwctx_lockdown(rvu, req);
1261 	return err;
1262 }
1263 #else
1264 
1265 int rvu_mbox_handler_nix_aq_enq(struct rvu *rvu,
1266 				struct nix_aq_enq_req *req,
1267 				struct nix_aq_enq_rsp *rsp)
1268 {
1269 	return rvu_nix_aq_enq_inst(rvu, req, rsp);
1270 }
1271 #endif
1272 /* CN10K mbox handler */
1273 int rvu_mbox_handler_nix_cn10k_aq_enq(struct rvu *rvu,
1274 				      struct nix_cn10k_aq_enq_req *req,
1275 				      struct nix_cn10k_aq_enq_rsp *rsp)
1276 {
1277 	return rvu_nix_aq_enq_inst(rvu, (struct nix_aq_enq_req *)req,
1278 				  (struct nix_aq_enq_rsp *)rsp);
1279 }
1280 
1281 int rvu_mbox_handler_nix_hwctx_disable(struct rvu *rvu,
1282 				       struct hwctx_disable_req *req,
1283 				       struct msg_rsp *rsp)
1284 {
1285 	return nix_lf_hwctx_disable(rvu, req);
1286 }
1287 
1288 int rvu_mbox_handler_nix_lf_alloc(struct rvu *rvu,
1289 				  struct nix_lf_alloc_req *req,
1290 				  struct nix_lf_alloc_rsp *rsp)
1291 {
1292 	int nixlf, qints, hwctx_size, intf, err, rc = 0;
1293 	struct rvu_hwinfo *hw = rvu->hw;
1294 	u16 pcifunc = req->hdr.pcifunc;
1295 	struct rvu_block *block;
1296 	struct rvu_pfvf *pfvf;
1297 	u64 cfg, ctx_cfg;
1298 	int blkaddr;
1299 
1300 	if (!req->rq_cnt || !req->sq_cnt || !req->cq_cnt)
1301 		return NIX_AF_ERR_PARAM;
1302 
1303 	if (req->way_mask)
1304 		req->way_mask &= 0xFFFF;
1305 
1306 	pfvf = rvu_get_pfvf(rvu, pcifunc);
1307 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
1308 	if (!pfvf->nixlf || blkaddr < 0)
1309 		return NIX_AF_ERR_AF_LF_INVALID;
1310 
1311 	block = &hw->block[blkaddr];
1312 	nixlf = rvu_get_lf(rvu, block, pcifunc, 0);
1313 	if (nixlf < 0)
1314 		return NIX_AF_ERR_AF_LF_INVALID;
1315 
1316 	/* Check if requested 'NIXLF <=> NPALF' mapping is valid */
1317 	if (req->npa_func) {
1318 		/* If default, use 'this' NIXLF's PFFUNC */
1319 		if (req->npa_func == RVU_DEFAULT_PF_FUNC)
1320 			req->npa_func = pcifunc;
1321 		if (!is_pffunc_map_valid(rvu, req->npa_func, BLKTYPE_NPA))
1322 			return NIX_AF_INVAL_NPA_PF_FUNC;
1323 	}
1324 
1325 	/* Check if requested 'NIXLF <=> SSOLF' mapping is valid */
1326 	if (req->sso_func) {
1327 		/* If default, use 'this' NIXLF's PFFUNC */
1328 		if (req->sso_func == RVU_DEFAULT_PF_FUNC)
1329 			req->sso_func = pcifunc;
1330 		if (!is_pffunc_map_valid(rvu, req->sso_func, BLKTYPE_SSO))
1331 			return NIX_AF_INVAL_SSO_PF_FUNC;
1332 	}
1333 
1334 	/* If RSS is being enabled, check if requested config is valid.
1335 	 * RSS table size should be power of two, otherwise
1336 	 * RSS_GRP::OFFSET + adder might go beyond that group or
1337 	 * won't be able to use entire table.
1338 	 */
1339 	if (req->rss_sz && (req->rss_sz > MAX_RSS_INDIR_TBL_SIZE ||
1340 			    !is_power_of_2(req->rss_sz)))
1341 		return NIX_AF_ERR_RSS_SIZE_INVALID;
1342 
1343 	if (req->rss_sz &&
1344 	    (!req->rss_grps || req->rss_grps > MAX_RSS_GROUPS))
1345 		return NIX_AF_ERR_RSS_GRPS_INVALID;
1346 
1347 	/* Reset this NIX LF */
1348 	err = rvu_lf_reset(rvu, block, nixlf);
1349 	if (err) {
1350 		dev_err(rvu->dev, "Failed to reset NIX%d LF%d\n",
1351 			block->addr - BLKADDR_NIX0, nixlf);
1352 		return NIX_AF_ERR_LF_RESET;
1353 	}
1354 
1355 	ctx_cfg = rvu_read64(rvu, blkaddr, NIX_AF_CONST3);
1356 
1357 	/* Alloc NIX RQ HW context memory and config the base */
1358 	hwctx_size = 1UL << ((ctx_cfg >> 4) & 0xF);
1359 	err = qmem_alloc(rvu->dev, &pfvf->rq_ctx, req->rq_cnt, hwctx_size);
1360 	if (err)
1361 		goto free_mem;
1362 
1363 	pfvf->rq_bmap = kcalloc(req->rq_cnt, sizeof(long), GFP_KERNEL);
1364 	if (!pfvf->rq_bmap)
1365 		goto free_mem;
1366 
1367 	rvu_write64(rvu, blkaddr, NIX_AF_LFX_RQS_BASE(nixlf),
1368 		    (u64)pfvf->rq_ctx->iova);
1369 
1370 	/* Set caching and queue count in HW */
1371 	cfg = BIT_ULL(36) | (req->rq_cnt - 1) | req->way_mask << 20;
1372 	rvu_write64(rvu, blkaddr, NIX_AF_LFX_RQS_CFG(nixlf), cfg);
1373 
1374 	/* Alloc NIX SQ HW context memory and config the base */
1375 	hwctx_size = 1UL << (ctx_cfg & 0xF);
1376 	err = qmem_alloc(rvu->dev, &pfvf->sq_ctx, req->sq_cnt, hwctx_size);
1377 	if (err)
1378 		goto free_mem;
1379 
1380 	pfvf->sq_bmap = kcalloc(req->sq_cnt, sizeof(long), GFP_KERNEL);
1381 	if (!pfvf->sq_bmap)
1382 		goto free_mem;
1383 
1384 	rvu_write64(rvu, blkaddr, NIX_AF_LFX_SQS_BASE(nixlf),
1385 		    (u64)pfvf->sq_ctx->iova);
1386 
1387 	cfg = BIT_ULL(36) | (req->sq_cnt - 1) | req->way_mask << 20;
1388 	rvu_write64(rvu, blkaddr, NIX_AF_LFX_SQS_CFG(nixlf), cfg);
1389 
1390 	/* Alloc NIX CQ HW context memory and config the base */
1391 	hwctx_size = 1UL << ((ctx_cfg >> 8) & 0xF);
1392 	err = qmem_alloc(rvu->dev, &pfvf->cq_ctx, req->cq_cnt, hwctx_size);
1393 	if (err)
1394 		goto free_mem;
1395 
1396 	pfvf->cq_bmap = kcalloc(req->cq_cnt, sizeof(long), GFP_KERNEL);
1397 	if (!pfvf->cq_bmap)
1398 		goto free_mem;
1399 
1400 	rvu_write64(rvu, blkaddr, NIX_AF_LFX_CQS_BASE(nixlf),
1401 		    (u64)pfvf->cq_ctx->iova);
1402 
1403 	cfg = BIT_ULL(36) | (req->cq_cnt - 1) | req->way_mask << 20;
1404 	rvu_write64(rvu, blkaddr, NIX_AF_LFX_CQS_CFG(nixlf), cfg);
1405 
1406 	/* Initialize receive side scaling (RSS) */
1407 	hwctx_size = 1UL << ((ctx_cfg >> 12) & 0xF);
1408 	err = nixlf_rss_ctx_init(rvu, blkaddr, pfvf, nixlf, req->rss_sz,
1409 				 req->rss_grps, hwctx_size, req->way_mask,
1410 				 !!(req->flags & NIX_LF_RSS_TAG_LSB_AS_ADDER));
1411 	if (err)
1412 		goto free_mem;
1413 
1414 	/* Alloc memory for CQINT's HW contexts */
1415 	cfg = rvu_read64(rvu, blkaddr, NIX_AF_CONST2);
1416 	qints = (cfg >> 24) & 0xFFF;
1417 	hwctx_size = 1UL << ((ctx_cfg >> 24) & 0xF);
1418 	err = qmem_alloc(rvu->dev, &pfvf->cq_ints_ctx, qints, hwctx_size);
1419 	if (err)
1420 		goto free_mem;
1421 
1422 	rvu_write64(rvu, blkaddr, NIX_AF_LFX_CINTS_BASE(nixlf),
1423 		    (u64)pfvf->cq_ints_ctx->iova);
1424 
1425 	rvu_write64(rvu, blkaddr, NIX_AF_LFX_CINTS_CFG(nixlf),
1426 		    BIT_ULL(36) | req->way_mask << 20);
1427 
1428 	/* Alloc memory for QINT's HW contexts */
1429 	cfg = rvu_read64(rvu, blkaddr, NIX_AF_CONST2);
1430 	qints = (cfg >> 12) & 0xFFF;
1431 	hwctx_size = 1UL << ((ctx_cfg >> 20) & 0xF);
1432 	err = qmem_alloc(rvu->dev, &pfvf->nix_qints_ctx, qints, hwctx_size);
1433 	if (err)
1434 		goto free_mem;
1435 
1436 	rvu_write64(rvu, blkaddr, NIX_AF_LFX_QINTS_BASE(nixlf),
1437 		    (u64)pfvf->nix_qints_ctx->iova);
1438 	rvu_write64(rvu, blkaddr, NIX_AF_LFX_QINTS_CFG(nixlf),
1439 		    BIT_ULL(36) | req->way_mask << 20);
1440 
1441 	/* Setup VLANX TPID's.
1442 	 * Use VLAN1 for 802.1Q
1443 	 * and VLAN0 for 802.1AD.
1444 	 */
1445 	cfg = (0x8100ULL << 16) | 0x88A8ULL;
1446 	rvu_write64(rvu, blkaddr, NIX_AF_LFX_TX_CFG(nixlf), cfg);
1447 
1448 	/* Enable LMTST for this NIX LF */
1449 	rvu_write64(rvu, blkaddr, NIX_AF_LFX_TX_CFG2(nixlf), BIT_ULL(0));
1450 
1451 	/* Set CQE/WQE size, NPA_PF_FUNC for SQBs and also SSO_PF_FUNC */
1452 	if (req->npa_func)
1453 		cfg = req->npa_func;
1454 	if (req->sso_func)
1455 		cfg |= (u64)req->sso_func << 16;
1456 
1457 	cfg |= (u64)req->xqe_sz << 33;
1458 	rvu_write64(rvu, blkaddr, NIX_AF_LFX_CFG(nixlf), cfg);
1459 
1460 	/* Config Rx pkt length, csum checks and apad  enable / disable */
1461 	rvu_write64(rvu, blkaddr, NIX_AF_LFX_RX_CFG(nixlf), req->rx_cfg);
1462 
1463 	/* Configure pkind for TX parse config */
1464 	cfg = NPC_TX_DEF_PKIND;
1465 	rvu_write64(rvu, blkaddr, NIX_AF_LFX_TX_PARSE_CFG(nixlf), cfg);
1466 
1467 	intf = is_afvf(pcifunc) ? NIX_INTF_TYPE_LBK : NIX_INTF_TYPE_CGX;
1468 	if (is_sdp_pfvf(pcifunc))
1469 		intf = NIX_INTF_TYPE_SDP;
1470 
1471 	err = nix_interface_init(rvu, pcifunc, intf, nixlf, rsp,
1472 				 !!(req->flags & NIX_LF_LBK_BLK_SEL));
1473 	if (err)
1474 		goto free_mem;
1475 
1476 	/* Disable NPC entries as NIXLF's contexts are not initialized yet */
1477 	rvu_npc_disable_default_entries(rvu, pcifunc, nixlf);
1478 
1479 	/* Configure RX VTAG Type 7 (strip) for vf vlan */
1480 	rvu_write64(rvu, blkaddr,
1481 		    NIX_AF_LFX_RX_VTAG_TYPEX(nixlf, NIX_AF_LFX_RX_VTAG_TYPE7),
1482 		    VTAGSIZE_T4 | VTAG_STRIP);
1483 
1484 	goto exit;
1485 
1486 free_mem:
1487 	nix_ctx_free(rvu, pfvf);
1488 	rc = -ENOMEM;
1489 
1490 exit:
1491 	/* Set macaddr of this PF/VF */
1492 	ether_addr_copy(rsp->mac_addr, pfvf->mac_addr);
1493 
1494 	/* set SQB size info */
1495 	cfg = rvu_read64(rvu, blkaddr, NIX_AF_SQ_CONST);
1496 	rsp->sqb_size = (cfg >> 34) & 0xFFFF;
1497 	rsp->rx_chan_base = pfvf->rx_chan_base;
1498 	rsp->tx_chan_base = pfvf->tx_chan_base;
1499 	rsp->rx_chan_cnt = pfvf->rx_chan_cnt;
1500 	rsp->tx_chan_cnt = pfvf->tx_chan_cnt;
1501 	rsp->lso_tsov4_idx = NIX_LSO_FORMAT_IDX_TSOV4;
1502 	rsp->lso_tsov6_idx = NIX_LSO_FORMAT_IDX_TSOV6;
1503 	/* Get HW supported stat count */
1504 	cfg = rvu_read64(rvu, blkaddr, NIX_AF_CONST1);
1505 	rsp->lf_rx_stats = ((cfg >> 32) & 0xFF);
1506 	rsp->lf_tx_stats = ((cfg >> 24) & 0xFF);
1507 	/* Get count of CQ IRQs and error IRQs supported per LF */
1508 	cfg = rvu_read64(rvu, blkaddr, NIX_AF_CONST2);
1509 	rsp->qints = ((cfg >> 12) & 0xFFF);
1510 	rsp->cints = ((cfg >> 24) & 0xFFF);
1511 	rsp->cgx_links = hw->cgx_links;
1512 	rsp->lbk_links = hw->lbk_links;
1513 	rsp->sdp_links = hw->sdp_links;
1514 
1515 	return rc;
1516 }
1517 
1518 int rvu_mbox_handler_nix_lf_free(struct rvu *rvu, struct nix_lf_free_req *req,
1519 				 struct msg_rsp *rsp)
1520 {
1521 	struct rvu_hwinfo *hw = rvu->hw;
1522 	u16 pcifunc = req->hdr.pcifunc;
1523 	struct rvu_block *block;
1524 	int blkaddr, nixlf, err;
1525 	struct rvu_pfvf *pfvf;
1526 
1527 	pfvf = rvu_get_pfvf(rvu, pcifunc);
1528 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
1529 	if (!pfvf->nixlf || blkaddr < 0)
1530 		return NIX_AF_ERR_AF_LF_INVALID;
1531 
1532 	block = &hw->block[blkaddr];
1533 	nixlf = rvu_get_lf(rvu, block, pcifunc, 0);
1534 	if (nixlf < 0)
1535 		return NIX_AF_ERR_AF_LF_INVALID;
1536 
1537 	if (req->flags & NIX_LF_DISABLE_FLOWS)
1538 		rvu_npc_disable_mcam_entries(rvu, pcifunc, nixlf);
1539 	else
1540 		rvu_npc_free_mcam_entries(rvu, pcifunc, nixlf);
1541 
1542 	/* Free any tx vtag def entries used by this NIX LF */
1543 	if (!(req->flags & NIX_LF_DONT_FREE_TX_VTAG))
1544 		nix_free_tx_vtag_entries(rvu, pcifunc);
1545 
1546 	nix_interface_deinit(rvu, pcifunc, nixlf);
1547 
1548 	/* Reset this NIX LF */
1549 	err = rvu_lf_reset(rvu, block, nixlf);
1550 	if (err) {
1551 		dev_err(rvu->dev, "Failed to reset NIX%d LF%d\n",
1552 			block->addr - BLKADDR_NIX0, nixlf);
1553 		return NIX_AF_ERR_LF_RESET;
1554 	}
1555 
1556 	nix_ctx_free(rvu, pfvf);
1557 
1558 	return 0;
1559 }
1560 
1561 int rvu_mbox_handler_nix_mark_format_cfg(struct rvu *rvu,
1562 					 struct nix_mark_format_cfg  *req,
1563 					 struct nix_mark_format_cfg_rsp *rsp)
1564 {
1565 	u16 pcifunc = req->hdr.pcifunc;
1566 	struct nix_hw *nix_hw;
1567 	struct rvu_pfvf *pfvf;
1568 	int blkaddr, rc;
1569 	u32 cfg;
1570 
1571 	pfvf = rvu_get_pfvf(rvu, pcifunc);
1572 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
1573 	if (!pfvf->nixlf || blkaddr < 0)
1574 		return NIX_AF_ERR_AF_LF_INVALID;
1575 
1576 	nix_hw = get_nix_hw(rvu->hw, blkaddr);
1577 	if (!nix_hw)
1578 		return NIX_AF_ERR_INVALID_NIXBLK;
1579 
1580 	cfg = (((u32)req->offset & 0x7) << 16) |
1581 	      (((u32)req->y_mask & 0xF) << 12) |
1582 	      (((u32)req->y_val & 0xF) << 8) |
1583 	      (((u32)req->r_mask & 0xF) << 4) | ((u32)req->r_val & 0xF);
1584 
1585 	rc = rvu_nix_reserve_mark_format(rvu, nix_hw, blkaddr, cfg);
1586 	if (rc < 0) {
1587 		dev_err(rvu->dev, "No mark_format_ctl for (pf:%d, vf:%d)",
1588 			rvu_get_pf(pcifunc), pcifunc & RVU_PFVF_FUNC_MASK);
1589 		return NIX_AF_ERR_MARK_CFG_FAIL;
1590 	}
1591 
1592 	rsp->mark_format_idx = rc;
1593 	return 0;
1594 }
1595 
1596 /* Handle shaper update specially for few revisions */
1597 static bool
1598 handle_txschq_shaper_update(struct rvu *rvu, int blkaddr, int nixlf,
1599 			    int lvl, u64 reg, u64 regval)
1600 {
1601 	u64 regbase, oldval, sw_xoff = 0;
1602 	u64 dbgval, md_debug0 = 0;
1603 	unsigned long poll_tmo;
1604 	bool rate_reg = 0;
1605 	u32 schq;
1606 
1607 	regbase = reg & 0xFFFF;
1608 	schq = TXSCHQ_IDX(reg, TXSCHQ_IDX_SHIFT);
1609 
1610 	/* Check for rate register */
1611 	switch (lvl) {
1612 	case NIX_TXSCH_LVL_TL1:
1613 		md_debug0 = NIX_AF_TL1X_MD_DEBUG0(schq);
1614 		sw_xoff = NIX_AF_TL1X_SW_XOFF(schq);
1615 
1616 		rate_reg = !!(regbase == NIX_AF_TL1X_CIR(0));
1617 		break;
1618 	case NIX_TXSCH_LVL_TL2:
1619 		md_debug0 = NIX_AF_TL2X_MD_DEBUG0(schq);
1620 		sw_xoff = NIX_AF_TL2X_SW_XOFF(schq);
1621 
1622 		rate_reg = (regbase == NIX_AF_TL2X_CIR(0) ||
1623 			    regbase == NIX_AF_TL2X_PIR(0));
1624 		break;
1625 	case NIX_TXSCH_LVL_TL3:
1626 		md_debug0 = NIX_AF_TL3X_MD_DEBUG0(schq);
1627 		sw_xoff = NIX_AF_TL3X_SW_XOFF(schq);
1628 
1629 		rate_reg = (regbase == NIX_AF_TL3X_CIR(0) ||
1630 			    regbase == NIX_AF_TL3X_PIR(0));
1631 		break;
1632 	case NIX_TXSCH_LVL_TL4:
1633 		md_debug0 = NIX_AF_TL4X_MD_DEBUG0(schq);
1634 		sw_xoff = NIX_AF_TL4X_SW_XOFF(schq);
1635 
1636 		rate_reg = (regbase == NIX_AF_TL4X_CIR(0) ||
1637 			    regbase == NIX_AF_TL4X_PIR(0));
1638 		break;
1639 	case NIX_TXSCH_LVL_MDQ:
1640 		sw_xoff = NIX_AF_MDQX_SW_XOFF(schq);
1641 		rate_reg = (regbase == NIX_AF_MDQX_CIR(0) ||
1642 			    regbase == NIX_AF_MDQX_PIR(0));
1643 		break;
1644 	}
1645 
1646 	if (!rate_reg)
1647 		return false;
1648 
1649 	/* Nothing special to do when state is not toggled */
1650 	oldval = rvu_read64(rvu, blkaddr, reg);
1651 	if ((oldval & 0x1) == (regval & 0x1)) {
1652 		rvu_write64(rvu, blkaddr, reg, regval);
1653 		return true;
1654 	}
1655 
1656 	/* PIR/CIR disable */
1657 	if (!(regval & 0x1)) {
1658 		rvu_write64(rvu, blkaddr, sw_xoff, 1);
1659 		rvu_write64(rvu, blkaddr, reg, 0);
1660 		udelay(4);
1661 		rvu_write64(rvu, blkaddr, sw_xoff, 0);
1662 		return true;
1663 	}
1664 
1665 	/* PIR/CIR enable */
1666 	rvu_write64(rvu, blkaddr, sw_xoff, 1);
1667 	if (md_debug0) {
1668 		poll_tmo = jiffies + usecs_to_jiffies(10000);
1669 		/* Wait until VLD(bit32) == 1 or C_CON(bit48) == 0 */
1670 		do {
1671 			if (time_after(jiffies, poll_tmo)) {
1672 				dev_err(rvu->dev,
1673 					"NIXLF%d: TLX%u(lvl %u) CIR/PIR enable failed\n",
1674 					nixlf, schq, lvl);
1675 				goto exit;
1676 			}
1677 			usleep_range(1, 5);
1678 			dbgval = rvu_read64(rvu, blkaddr, md_debug0);
1679 		} while (!(dbgval & BIT_ULL(32)) && (dbgval & BIT_ULL(48)));
1680 	}
1681 	rvu_write64(rvu, blkaddr, reg, regval);
1682 exit:
1683 	rvu_write64(rvu, blkaddr, sw_xoff, 0);
1684 	return true;
1685 }
1686 
1687 /* Disable shaping of pkts by a scheduler queue
1688  * at a given scheduler level.
1689  */
1690 static void nix_reset_tx_shaping(struct rvu *rvu, int blkaddr,
1691 				 int nixlf, int lvl, int schq)
1692 {
1693 	struct rvu_hwinfo *hw = rvu->hw;
1694 	u64  cir_reg = 0, pir_reg = 0;
1695 	u64  cfg;
1696 
1697 	switch (lvl) {
1698 	case NIX_TXSCH_LVL_TL1:
1699 		cir_reg = NIX_AF_TL1X_CIR(schq);
1700 		pir_reg = 0; /* PIR not available at TL1 */
1701 		break;
1702 	case NIX_TXSCH_LVL_TL2:
1703 		cir_reg = NIX_AF_TL2X_CIR(schq);
1704 		pir_reg = NIX_AF_TL2X_PIR(schq);
1705 		break;
1706 	case NIX_TXSCH_LVL_TL3:
1707 		cir_reg = NIX_AF_TL3X_CIR(schq);
1708 		pir_reg = NIX_AF_TL3X_PIR(schq);
1709 		break;
1710 	case NIX_TXSCH_LVL_TL4:
1711 		cir_reg = NIX_AF_TL4X_CIR(schq);
1712 		pir_reg = NIX_AF_TL4X_PIR(schq);
1713 		break;
1714 	case NIX_TXSCH_LVL_MDQ:
1715 		cir_reg = NIX_AF_MDQX_CIR(schq);
1716 		pir_reg = NIX_AF_MDQX_PIR(schq);
1717 		break;
1718 	}
1719 
1720 	/* Shaper state toggle needs wait/poll */
1721 	if (hw->cap.nix_shaper_toggle_wait) {
1722 		if (cir_reg)
1723 			handle_txschq_shaper_update(rvu, blkaddr, nixlf,
1724 						    lvl, cir_reg, 0);
1725 		if (pir_reg)
1726 			handle_txschq_shaper_update(rvu, blkaddr, nixlf,
1727 						    lvl, pir_reg, 0);
1728 		return;
1729 	}
1730 
1731 	if (!cir_reg)
1732 		return;
1733 	cfg = rvu_read64(rvu, blkaddr, cir_reg);
1734 	rvu_write64(rvu, blkaddr, cir_reg, cfg & ~BIT_ULL(0));
1735 
1736 	if (!pir_reg)
1737 		return;
1738 	cfg = rvu_read64(rvu, blkaddr, pir_reg);
1739 	rvu_write64(rvu, blkaddr, pir_reg, cfg & ~BIT_ULL(0));
1740 }
1741 
1742 static void nix_reset_tx_linkcfg(struct rvu *rvu, int blkaddr,
1743 				 int lvl, int schq)
1744 {
1745 	struct rvu_hwinfo *hw = rvu->hw;
1746 	int link_level;
1747 	int link;
1748 
1749 	if (lvl >= hw->cap.nix_tx_aggr_lvl)
1750 		return;
1751 
1752 	/* Reset TL4's SDP link config */
1753 	if (lvl == NIX_TXSCH_LVL_TL4)
1754 		rvu_write64(rvu, blkaddr, NIX_AF_TL4X_SDP_LINK_CFG(schq), 0x00);
1755 
1756 	link_level = rvu_read64(rvu, blkaddr, NIX_AF_PSE_CHANNEL_LEVEL) & 0x01 ?
1757 			NIX_TXSCH_LVL_TL3 : NIX_TXSCH_LVL_TL2;
1758 	if (lvl != link_level)
1759 		return;
1760 
1761 	/* Reset TL2's CGX or LBK link config */
1762 	for (link = 0; link < (hw->cgx_links + hw->lbk_links); link++)
1763 		rvu_write64(rvu, blkaddr,
1764 			    NIX_AF_TL3_TL2X_LINKX_CFG(schq, link), 0x00);
1765 }
1766 
1767 static void nix_clear_tx_xoff(struct rvu *rvu, int blkaddr,
1768 			      int lvl, int schq)
1769 {
1770 	struct rvu_hwinfo *hw = rvu->hw;
1771 	u64 reg;
1772 
1773 	/* Skip this if shaping is not supported */
1774 	if (!hw->cap.nix_shaping)
1775 		return;
1776 
1777 	/* Clear level specific SW_XOFF */
1778 	switch (lvl) {
1779 	case NIX_TXSCH_LVL_TL1:
1780 		reg = NIX_AF_TL1X_SW_XOFF(schq);
1781 		break;
1782 	case NIX_TXSCH_LVL_TL2:
1783 		reg = NIX_AF_TL2X_SW_XOFF(schq);
1784 		break;
1785 	case NIX_TXSCH_LVL_TL3:
1786 		reg = NIX_AF_TL3X_SW_XOFF(schq);
1787 		break;
1788 	case NIX_TXSCH_LVL_TL4:
1789 		reg = NIX_AF_TL4X_SW_XOFF(schq);
1790 		break;
1791 	case NIX_TXSCH_LVL_MDQ:
1792 		reg = NIX_AF_MDQX_SW_XOFF(schq);
1793 		break;
1794 	default:
1795 		return;
1796 	}
1797 
1798 	rvu_write64(rvu, blkaddr, reg, 0x0);
1799 }
1800 
1801 static int nix_get_tx_link(struct rvu *rvu, u16 pcifunc)
1802 {
1803 	struct rvu_hwinfo *hw = rvu->hw;
1804 	int pf = rvu_get_pf(pcifunc);
1805 	u8 cgx_id = 0, lmac_id = 0;
1806 
1807 	if (is_afvf(pcifunc)) {/* LBK links */
1808 		return hw->cgx_links;
1809 	} else if (is_pf_cgxmapped(rvu, pf)) {
1810 		rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
1811 		return (cgx_id * hw->lmac_per_cgx) + lmac_id;
1812 	}
1813 
1814 	/* SDP link */
1815 	return hw->cgx_links + hw->lbk_links;
1816 }
1817 
1818 static void nix_get_txschq_range(struct rvu *rvu, u16 pcifunc,
1819 				 int link, int *start, int *end)
1820 {
1821 	struct rvu_hwinfo *hw = rvu->hw;
1822 	int pf = rvu_get_pf(pcifunc);
1823 
1824 	if (is_afvf(pcifunc)) { /* LBK links */
1825 		*start = hw->cap.nix_txsch_per_cgx_lmac * link;
1826 		*end = *start + hw->cap.nix_txsch_per_lbk_lmac;
1827 	} else if (is_pf_cgxmapped(rvu, pf)) { /* CGX links */
1828 		*start = hw->cap.nix_txsch_per_cgx_lmac * link;
1829 		*end = *start + hw->cap.nix_txsch_per_cgx_lmac;
1830 	} else { /* SDP link */
1831 		*start = (hw->cap.nix_txsch_per_cgx_lmac * hw->cgx_links) +
1832 			(hw->cap.nix_txsch_per_lbk_lmac * hw->lbk_links);
1833 		*end = *start + hw->cap.nix_txsch_per_sdp_lmac;
1834 	}
1835 }
1836 
1837 static int nix_check_txschq_alloc_req(struct rvu *rvu, int lvl, u16 pcifunc,
1838 				      struct nix_hw *nix_hw,
1839 				      struct nix_txsch_alloc_req *req)
1840 {
1841 	struct rvu_hwinfo *hw = rvu->hw;
1842 	int schq, req_schq, free_cnt;
1843 	struct nix_txsch *txsch;
1844 	int link, start, end;
1845 
1846 	txsch = &nix_hw->txsch[lvl];
1847 	req_schq = req->schq_contig[lvl] + req->schq[lvl];
1848 
1849 	if (!req_schq)
1850 		return 0;
1851 
1852 	link = nix_get_tx_link(rvu, pcifunc);
1853 
1854 	/* For traffic aggregating scheduler level, one queue is enough */
1855 	if (lvl >= hw->cap.nix_tx_aggr_lvl) {
1856 		if (req_schq != 1)
1857 			return NIX_AF_ERR_TLX_ALLOC_FAIL;
1858 		return 0;
1859 	}
1860 
1861 	/* Get free SCHQ count and check if request can be accomodated */
1862 	if (hw->cap.nix_fixed_txschq_mapping) {
1863 		nix_get_txschq_range(rvu, pcifunc, link, &start, &end);
1864 		schq = start + (pcifunc & RVU_PFVF_FUNC_MASK);
1865 		if (end <= txsch->schq.max && schq < end &&
1866 		    !test_bit(schq, txsch->schq.bmap))
1867 			free_cnt = 1;
1868 		else
1869 			free_cnt = 0;
1870 	} else {
1871 		free_cnt = rvu_rsrc_free_count(&txsch->schq);
1872 	}
1873 
1874 	if (free_cnt < req_schq || req_schq > MAX_TXSCHQ_PER_FUNC)
1875 		return NIX_AF_ERR_TLX_ALLOC_FAIL;
1876 
1877 	/* If contiguous queues are needed, check for availability */
1878 	if (!hw->cap.nix_fixed_txschq_mapping && req->schq_contig[lvl] &&
1879 	    !rvu_rsrc_check_contig(&txsch->schq, req->schq_contig[lvl]))
1880 		return NIX_AF_ERR_TLX_ALLOC_FAIL;
1881 
1882 	return 0;
1883 }
1884 
1885 static void nix_txsch_alloc(struct rvu *rvu, struct nix_txsch *txsch,
1886 			    struct nix_txsch_alloc_rsp *rsp,
1887 			    int lvl, int start, int end)
1888 {
1889 	struct rvu_hwinfo *hw = rvu->hw;
1890 	u16 pcifunc = rsp->hdr.pcifunc;
1891 	int idx, schq;
1892 
1893 	/* For traffic aggregating levels, queue alloc is based
1894 	 * on transmit link to which PF_FUNC is mapped to.
1895 	 */
1896 	if (lvl >= hw->cap.nix_tx_aggr_lvl) {
1897 		/* A single TL queue is allocated */
1898 		if (rsp->schq_contig[lvl]) {
1899 			rsp->schq_contig[lvl] = 1;
1900 			rsp->schq_contig_list[lvl][0] = start;
1901 		}
1902 
1903 		/* Both contig and non-contig reqs doesn't make sense here */
1904 		if (rsp->schq_contig[lvl])
1905 			rsp->schq[lvl] = 0;
1906 
1907 		if (rsp->schq[lvl]) {
1908 			rsp->schq[lvl] = 1;
1909 			rsp->schq_list[lvl][0] = start;
1910 		}
1911 		return;
1912 	}
1913 
1914 	/* Adjust the queue request count if HW supports
1915 	 * only one queue per level configuration.
1916 	 */
1917 	if (hw->cap.nix_fixed_txschq_mapping) {
1918 		idx = pcifunc & RVU_PFVF_FUNC_MASK;
1919 		schq = start + idx;
1920 		if (idx >= (end - start) || test_bit(schq, txsch->schq.bmap)) {
1921 			rsp->schq_contig[lvl] = 0;
1922 			rsp->schq[lvl] = 0;
1923 			return;
1924 		}
1925 
1926 		if (rsp->schq_contig[lvl]) {
1927 			rsp->schq_contig[lvl] = 1;
1928 			set_bit(schq, txsch->schq.bmap);
1929 			rsp->schq_contig_list[lvl][0] = schq;
1930 			rsp->schq[lvl] = 0;
1931 		} else if (rsp->schq[lvl]) {
1932 			rsp->schq[lvl] = 1;
1933 			set_bit(schq, txsch->schq.bmap);
1934 			rsp->schq_list[lvl][0] = schq;
1935 		}
1936 		return;
1937 	}
1938 
1939 	/* Allocate contiguous queue indices requesty first */
1940 	if (rsp->schq_contig[lvl]) {
1941 		schq = bitmap_find_next_zero_area(txsch->schq.bmap,
1942 						  txsch->schq.max, start,
1943 						  rsp->schq_contig[lvl], 0);
1944 		if (schq >= end)
1945 			rsp->schq_contig[lvl] = 0;
1946 		for (idx = 0; idx < rsp->schq_contig[lvl]; idx++) {
1947 			set_bit(schq, txsch->schq.bmap);
1948 			rsp->schq_contig_list[lvl][idx] = schq;
1949 			schq++;
1950 		}
1951 	}
1952 
1953 	/* Allocate non-contiguous queue indices */
1954 	if (rsp->schq[lvl]) {
1955 		idx = 0;
1956 		for (schq = start; schq < end; schq++) {
1957 			if (!test_bit(schq, txsch->schq.bmap)) {
1958 				set_bit(schq, txsch->schq.bmap);
1959 				rsp->schq_list[lvl][idx++] = schq;
1960 			}
1961 			if (idx == rsp->schq[lvl])
1962 				break;
1963 		}
1964 		/* Update how many were allocated */
1965 		rsp->schq[lvl] = idx;
1966 	}
1967 }
1968 
1969 int rvu_mbox_handler_nix_txsch_alloc(struct rvu *rvu,
1970 				     struct nix_txsch_alloc_req *req,
1971 				     struct nix_txsch_alloc_rsp *rsp)
1972 {
1973 	struct rvu_hwinfo *hw = rvu->hw;
1974 	u16 pcifunc = req->hdr.pcifunc;
1975 	int link, blkaddr, rc = 0;
1976 	int lvl, idx, start, end;
1977 	struct nix_txsch *txsch;
1978 	struct nix_hw *nix_hw;
1979 	u32 *pfvf_map;
1980 	int nixlf;
1981 	u16 schq;
1982 
1983 	rc = nix_get_nixlf(rvu, pcifunc, &nixlf, &blkaddr);
1984 	if (rc)
1985 		return rc;
1986 
1987 	nix_hw = get_nix_hw(rvu->hw, blkaddr);
1988 	if (!nix_hw)
1989 		return NIX_AF_ERR_INVALID_NIXBLK;
1990 
1991 	mutex_lock(&rvu->rsrc_lock);
1992 
1993 	/* Check if request is valid as per HW capabilities
1994 	 * and can be accomodated.
1995 	 */
1996 	for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
1997 		rc = nix_check_txschq_alloc_req(rvu, lvl, pcifunc, nix_hw, req);
1998 		if (rc)
1999 			goto err;
2000 	}
2001 
2002 	/* Allocate requested Tx scheduler queues */
2003 	for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
2004 		txsch = &nix_hw->txsch[lvl];
2005 		pfvf_map = txsch->pfvf_map;
2006 
2007 		if (!req->schq[lvl] && !req->schq_contig[lvl])
2008 			continue;
2009 
2010 		rsp->schq[lvl] = req->schq[lvl];
2011 		rsp->schq_contig[lvl] = req->schq_contig[lvl];
2012 
2013 		link = nix_get_tx_link(rvu, pcifunc);
2014 
2015 		if (lvl >= hw->cap.nix_tx_aggr_lvl) {
2016 			start = link;
2017 			end = link;
2018 		} else if (hw->cap.nix_fixed_txschq_mapping) {
2019 			nix_get_txschq_range(rvu, pcifunc, link, &start, &end);
2020 		} else {
2021 			start = 0;
2022 			end = txsch->schq.max;
2023 		}
2024 
2025 		nix_txsch_alloc(rvu, txsch, rsp, lvl, start, end);
2026 
2027 		/* Reset queue config */
2028 		for (idx = 0; idx < req->schq_contig[lvl]; idx++) {
2029 			schq = rsp->schq_contig_list[lvl][idx];
2030 			if (!(TXSCH_MAP_FLAGS(pfvf_map[schq]) &
2031 			    NIX_TXSCHQ_CFG_DONE))
2032 				pfvf_map[schq] = TXSCH_MAP(pcifunc, 0);
2033 			nix_reset_tx_linkcfg(rvu, blkaddr, lvl, schq);
2034 			nix_reset_tx_shaping(rvu, blkaddr, nixlf, lvl, schq);
2035 		}
2036 
2037 		for (idx = 0; idx < req->schq[lvl]; idx++) {
2038 			schq = rsp->schq_list[lvl][idx];
2039 			if (!(TXSCH_MAP_FLAGS(pfvf_map[schq]) &
2040 			    NIX_TXSCHQ_CFG_DONE))
2041 				pfvf_map[schq] = TXSCH_MAP(pcifunc, 0);
2042 			nix_reset_tx_linkcfg(rvu, blkaddr, lvl, schq);
2043 			nix_reset_tx_shaping(rvu, blkaddr, nixlf, lvl, schq);
2044 		}
2045 	}
2046 
2047 	rsp->aggr_level = hw->cap.nix_tx_aggr_lvl;
2048 	rsp->aggr_lvl_rr_prio = TXSCH_TL1_DFLT_RR_PRIO;
2049 	rsp->link_cfg_lvl = rvu_read64(rvu, blkaddr,
2050 				       NIX_AF_PSE_CHANNEL_LEVEL) & 0x01 ?
2051 				       NIX_TXSCH_LVL_TL3 : NIX_TXSCH_LVL_TL2;
2052 	goto exit;
2053 err:
2054 	rc = NIX_AF_ERR_TLX_ALLOC_FAIL;
2055 exit:
2056 	mutex_unlock(&rvu->rsrc_lock);
2057 	return rc;
2058 }
2059 
2060 static int nix_smq_flush(struct rvu *rvu, int blkaddr,
2061 			 int smq, u16 pcifunc, int nixlf)
2062 {
2063 	int pf = rvu_get_pf(pcifunc);
2064 	u8 cgx_id = 0, lmac_id = 0;
2065 	int err, restore_tx_en = 0;
2066 	u64 cfg;
2067 
2068 	/* enable cgx tx if disabled */
2069 	if (is_pf_cgxmapped(rvu, pf)) {
2070 		rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
2071 		restore_tx_en = !cgx_lmac_tx_enable(rvu_cgx_pdata(cgx_id, rvu),
2072 						    lmac_id, true);
2073 	}
2074 
2075 	cfg = rvu_read64(rvu, blkaddr, NIX_AF_SMQX_CFG(smq));
2076 	/* Do SMQ flush and set enqueue xoff */
2077 	cfg |= BIT_ULL(50) | BIT_ULL(49);
2078 	rvu_write64(rvu, blkaddr, NIX_AF_SMQX_CFG(smq), cfg);
2079 
2080 	/* Disable backpressure from physical link,
2081 	 * otherwise SMQ flush may stall.
2082 	 */
2083 	rvu_cgx_enadis_rx_bp(rvu, pf, false);
2084 
2085 	/* Wait for flush to complete */
2086 	err = rvu_poll_reg(rvu, blkaddr,
2087 			   NIX_AF_SMQX_CFG(smq), BIT_ULL(49), true);
2088 	if (err)
2089 		dev_err(rvu->dev,
2090 			"NIXLF%d: SMQ%d flush failed\n", nixlf, smq);
2091 
2092 	rvu_cgx_enadis_rx_bp(rvu, pf, true);
2093 	/* restore cgx tx state */
2094 	if (restore_tx_en)
2095 		cgx_lmac_tx_enable(rvu_cgx_pdata(cgx_id, rvu), lmac_id, false);
2096 	return err;
2097 }
2098 
2099 static int nix_txschq_free(struct rvu *rvu, u16 pcifunc)
2100 {
2101 	int blkaddr, nixlf, lvl, schq, err;
2102 	struct rvu_hwinfo *hw = rvu->hw;
2103 	struct nix_txsch *txsch;
2104 	struct nix_hw *nix_hw;
2105 	u16 map_func;
2106 
2107 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
2108 	if (blkaddr < 0)
2109 		return NIX_AF_ERR_AF_LF_INVALID;
2110 
2111 	nix_hw = get_nix_hw(rvu->hw, blkaddr);
2112 	if (!nix_hw)
2113 		return NIX_AF_ERR_INVALID_NIXBLK;
2114 
2115 	nixlf = rvu_get_lf(rvu, &hw->block[blkaddr], pcifunc, 0);
2116 	if (nixlf < 0)
2117 		return NIX_AF_ERR_AF_LF_INVALID;
2118 
2119 	/* Disable TL2/3 queue links and all XOFF's before SMQ flush*/
2120 	mutex_lock(&rvu->rsrc_lock);
2121 	for (lvl = NIX_TXSCH_LVL_MDQ; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
2122 		txsch = &nix_hw->txsch[lvl];
2123 
2124 		if (lvl >= hw->cap.nix_tx_aggr_lvl)
2125 			continue;
2126 
2127 		for (schq = 0; schq < txsch->schq.max; schq++) {
2128 			if (TXSCH_MAP_FUNC(txsch->pfvf_map[schq]) != pcifunc)
2129 				continue;
2130 			nix_reset_tx_linkcfg(rvu, blkaddr, lvl, schq);
2131 			nix_clear_tx_xoff(rvu, blkaddr, lvl, schq);
2132 		}
2133 	}
2134 	nix_clear_tx_xoff(rvu, blkaddr, NIX_TXSCH_LVL_TL1,
2135 			  nix_get_tx_link(rvu, pcifunc));
2136 
2137 	/* On PF cleanup, clear cfg done flag as
2138 	 * PF would have changed default config.
2139 	 */
2140 	if (!(pcifunc & RVU_PFVF_FUNC_MASK)) {
2141 		txsch = &nix_hw->txsch[NIX_TXSCH_LVL_TL1];
2142 		schq = nix_get_tx_link(rvu, pcifunc);
2143 		/* Do not clear pcifunc in txsch->pfvf_map[schq] because
2144 		 * VF might be using this TL1 queue
2145 		 */
2146 		map_func = TXSCH_MAP_FUNC(txsch->pfvf_map[schq]);
2147 		txsch->pfvf_map[schq] = TXSCH_SET_FLAG(map_func, 0x0);
2148 	}
2149 
2150 	/* Flush SMQs */
2151 	txsch = &nix_hw->txsch[NIX_TXSCH_LVL_SMQ];
2152 	for (schq = 0; schq < txsch->schq.max; schq++) {
2153 		if (TXSCH_MAP_FUNC(txsch->pfvf_map[schq]) != pcifunc)
2154 			continue;
2155 		nix_smq_flush(rvu, blkaddr, schq, pcifunc, nixlf);
2156 	}
2157 
2158 	/* Now free scheduler queues to free pool */
2159 	for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
2160 		 /* TLs above aggregation level are shared across all PF
2161 		  * and it's VFs, hence skip freeing them.
2162 		  */
2163 		if (lvl >= hw->cap.nix_tx_aggr_lvl)
2164 			continue;
2165 
2166 		txsch = &nix_hw->txsch[lvl];
2167 		for (schq = 0; schq < txsch->schq.max; schq++) {
2168 			if (TXSCH_MAP_FUNC(txsch->pfvf_map[schq]) != pcifunc)
2169 				continue;
2170 			rvu_free_rsrc(&txsch->schq, schq);
2171 			txsch->pfvf_map[schq] = TXSCH_MAP(0, NIX_TXSCHQ_FREE);
2172 		}
2173 	}
2174 	mutex_unlock(&rvu->rsrc_lock);
2175 
2176 	/* Sync cached info for this LF in NDC-TX to LLC/DRAM */
2177 	rvu_write64(rvu, blkaddr, NIX_AF_NDC_TX_SYNC, BIT_ULL(12) | nixlf);
2178 	err = rvu_poll_reg(rvu, blkaddr, NIX_AF_NDC_TX_SYNC, BIT_ULL(12), true);
2179 	if (err)
2180 		dev_err(rvu->dev, "NDC-TX sync failed for NIXLF %d\n", nixlf);
2181 
2182 	return 0;
2183 }
2184 
2185 static int nix_txschq_free_one(struct rvu *rvu,
2186 			       struct nix_txsch_free_req *req)
2187 {
2188 	struct rvu_hwinfo *hw = rvu->hw;
2189 	u16 pcifunc = req->hdr.pcifunc;
2190 	int lvl, schq, nixlf, blkaddr;
2191 	struct nix_txsch *txsch;
2192 	struct nix_hw *nix_hw;
2193 	u32 *pfvf_map;
2194 	int rc;
2195 
2196 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
2197 	if (blkaddr < 0)
2198 		return NIX_AF_ERR_AF_LF_INVALID;
2199 
2200 	nix_hw = get_nix_hw(rvu->hw, blkaddr);
2201 	if (!nix_hw)
2202 		return NIX_AF_ERR_INVALID_NIXBLK;
2203 
2204 	nixlf = rvu_get_lf(rvu, &hw->block[blkaddr], pcifunc, 0);
2205 	if (nixlf < 0)
2206 		return NIX_AF_ERR_AF_LF_INVALID;
2207 
2208 	lvl = req->schq_lvl;
2209 	schq = req->schq;
2210 	txsch = &nix_hw->txsch[lvl];
2211 
2212 	if (lvl >= hw->cap.nix_tx_aggr_lvl || schq >= txsch->schq.max)
2213 		return 0;
2214 
2215 	pfvf_map = txsch->pfvf_map;
2216 	mutex_lock(&rvu->rsrc_lock);
2217 
2218 	if (TXSCH_MAP_FUNC(pfvf_map[schq]) != pcifunc) {
2219 		rc = NIX_AF_ERR_TLX_INVALID;
2220 		goto err;
2221 	}
2222 
2223 	/* Clear SW_XOFF of this resource only.
2224 	 * For SMQ level, all path XOFF's
2225 	 * need to be made clear by user
2226 	 */
2227 	nix_clear_tx_xoff(rvu, blkaddr, lvl, schq);
2228 
2229 	/* Flush if it is a SMQ. Onus of disabling
2230 	 * TL2/3 queue links before SMQ flush is on user
2231 	 */
2232 	if (lvl == NIX_TXSCH_LVL_SMQ &&
2233 	    nix_smq_flush(rvu, blkaddr, schq, pcifunc, nixlf)) {
2234 		rc = NIX_AF_SMQ_FLUSH_FAILED;
2235 		goto err;
2236 	}
2237 
2238 	/* Free the resource */
2239 	rvu_free_rsrc(&txsch->schq, schq);
2240 	txsch->pfvf_map[schq] = TXSCH_MAP(0, NIX_TXSCHQ_FREE);
2241 	mutex_unlock(&rvu->rsrc_lock);
2242 	return 0;
2243 err:
2244 	mutex_unlock(&rvu->rsrc_lock);
2245 	return rc;
2246 }
2247 
2248 int rvu_mbox_handler_nix_txsch_free(struct rvu *rvu,
2249 				    struct nix_txsch_free_req *req,
2250 				    struct msg_rsp *rsp)
2251 {
2252 	if (req->flags & TXSCHQ_FREE_ALL)
2253 		return nix_txschq_free(rvu, req->hdr.pcifunc);
2254 	else
2255 		return nix_txschq_free_one(rvu, req);
2256 }
2257 
2258 static bool is_txschq_hierarchy_valid(struct rvu *rvu, u16 pcifunc, int blkaddr,
2259 				      int lvl, u64 reg, u64 regval)
2260 {
2261 	u64 regbase = reg & 0xFFFF;
2262 	u16 schq, parent;
2263 
2264 	if (!rvu_check_valid_reg(TXSCHQ_HWREGMAP, lvl, reg))
2265 		return false;
2266 
2267 	schq = TXSCHQ_IDX(reg, TXSCHQ_IDX_SHIFT);
2268 	/* Check if this schq belongs to this PF/VF or not */
2269 	if (!is_valid_txschq(rvu, blkaddr, lvl, pcifunc, schq))
2270 		return false;
2271 
2272 	parent = (regval >> 16) & 0x1FF;
2273 	/* Validate MDQ's TL4 parent */
2274 	if (regbase == NIX_AF_MDQX_PARENT(0) &&
2275 	    !is_valid_txschq(rvu, blkaddr, NIX_TXSCH_LVL_TL4, pcifunc, parent))
2276 		return false;
2277 
2278 	/* Validate TL4's TL3 parent */
2279 	if (regbase == NIX_AF_TL4X_PARENT(0) &&
2280 	    !is_valid_txschq(rvu, blkaddr, NIX_TXSCH_LVL_TL3, pcifunc, parent))
2281 		return false;
2282 
2283 	/* Validate TL3's TL2 parent */
2284 	if (regbase == NIX_AF_TL3X_PARENT(0) &&
2285 	    !is_valid_txschq(rvu, blkaddr, NIX_TXSCH_LVL_TL2, pcifunc, parent))
2286 		return false;
2287 
2288 	/* Validate TL2's TL1 parent */
2289 	if (regbase == NIX_AF_TL2X_PARENT(0) &&
2290 	    !is_valid_txschq(rvu, blkaddr, NIX_TXSCH_LVL_TL1, pcifunc, parent))
2291 		return false;
2292 
2293 	return true;
2294 }
2295 
2296 static bool is_txschq_shaping_valid(struct rvu_hwinfo *hw, int lvl, u64 reg)
2297 {
2298 	u64 regbase;
2299 
2300 	if (hw->cap.nix_shaping)
2301 		return true;
2302 
2303 	/* If shaping and coloring is not supported, then
2304 	 * *_CIR and *_PIR registers should not be configured.
2305 	 */
2306 	regbase = reg & 0xFFFF;
2307 
2308 	switch (lvl) {
2309 	case NIX_TXSCH_LVL_TL1:
2310 		if (regbase == NIX_AF_TL1X_CIR(0))
2311 			return false;
2312 		break;
2313 	case NIX_TXSCH_LVL_TL2:
2314 		if (regbase == NIX_AF_TL2X_CIR(0) ||
2315 		    regbase == NIX_AF_TL2X_PIR(0))
2316 			return false;
2317 		break;
2318 	case NIX_TXSCH_LVL_TL3:
2319 		if (regbase == NIX_AF_TL3X_CIR(0) ||
2320 		    regbase == NIX_AF_TL3X_PIR(0))
2321 			return false;
2322 		break;
2323 	case NIX_TXSCH_LVL_TL4:
2324 		if (regbase == NIX_AF_TL4X_CIR(0) ||
2325 		    regbase == NIX_AF_TL4X_PIR(0))
2326 			return false;
2327 		break;
2328 	case NIX_TXSCH_LVL_MDQ:
2329 		if (regbase == NIX_AF_MDQX_CIR(0) ||
2330 		    regbase == NIX_AF_MDQX_PIR(0))
2331 			return false;
2332 		break;
2333 	}
2334 	return true;
2335 }
2336 
2337 static void nix_tl1_default_cfg(struct rvu *rvu, struct nix_hw *nix_hw,
2338 				u16 pcifunc, int blkaddr)
2339 {
2340 	u32 *pfvf_map;
2341 	int schq;
2342 
2343 	schq = nix_get_tx_link(rvu, pcifunc);
2344 	pfvf_map = nix_hw->txsch[NIX_TXSCH_LVL_TL1].pfvf_map;
2345 	/* Skip if PF has already done the config */
2346 	if (TXSCH_MAP_FLAGS(pfvf_map[schq]) & NIX_TXSCHQ_CFG_DONE)
2347 		return;
2348 	rvu_write64(rvu, blkaddr, NIX_AF_TL1X_TOPOLOGY(schq),
2349 		    (TXSCH_TL1_DFLT_RR_PRIO << 1));
2350 
2351 	/* On OcteonTx2 the config was in bytes and newer silcons
2352 	 * it's changed to weight.
2353 	 */
2354 	if (!rvu->hw->cap.nix_common_dwrr_mtu)
2355 		rvu_write64(rvu, blkaddr, NIX_AF_TL1X_SCHEDULE(schq),
2356 			    TXSCH_TL1_DFLT_RR_QTM);
2357 	else
2358 		rvu_write64(rvu, blkaddr, NIX_AF_TL1X_SCHEDULE(schq),
2359 			    CN10K_MAX_DWRR_WEIGHT);
2360 
2361 	rvu_write64(rvu, blkaddr, NIX_AF_TL1X_CIR(schq), 0x00);
2362 	pfvf_map[schq] = TXSCH_SET_FLAG(pfvf_map[schq], NIX_TXSCHQ_CFG_DONE);
2363 }
2364 
2365 /* Register offset - [15:0]
2366  * Scheduler Queue number - [25:16]
2367  */
2368 #define NIX_TX_SCHQ_MASK	GENMASK_ULL(25, 0)
2369 
2370 static int nix_txschq_cfg_read(struct rvu *rvu, struct nix_hw *nix_hw,
2371 			       int blkaddr, struct nix_txschq_config *req,
2372 			       struct nix_txschq_config *rsp)
2373 {
2374 	u16 pcifunc = req->hdr.pcifunc;
2375 	int idx, schq;
2376 	u64 reg;
2377 
2378 	for (idx = 0; idx < req->num_regs; idx++) {
2379 		reg = req->reg[idx];
2380 		reg &= NIX_TX_SCHQ_MASK;
2381 		schq = TXSCHQ_IDX(reg, TXSCHQ_IDX_SHIFT);
2382 		if (!rvu_check_valid_reg(TXSCHQ_HWREGMAP, req->lvl, reg) ||
2383 		    !is_valid_txschq(rvu, blkaddr, req->lvl, pcifunc, schq))
2384 			return NIX_AF_INVAL_TXSCHQ_CFG;
2385 		rsp->regval[idx] = rvu_read64(rvu, blkaddr, reg);
2386 	}
2387 	rsp->lvl = req->lvl;
2388 	rsp->num_regs = req->num_regs;
2389 	return 0;
2390 }
2391 
2392 static void rvu_nix_tx_tl2_cfg(struct rvu *rvu, int blkaddr,
2393 			       u16 pcifunc, struct nix_txsch *txsch)
2394 {
2395 	struct rvu_hwinfo *hw = rvu->hw;
2396 	int lbk_link_start, lbk_links;
2397 	u8 pf = rvu_get_pf(pcifunc);
2398 	int schq;
2399 
2400 	if (!is_pf_cgxmapped(rvu, pf))
2401 		return;
2402 
2403 	lbk_link_start = hw->cgx_links;
2404 
2405 	for (schq = 0; schq < txsch->schq.max; schq++) {
2406 		if (TXSCH_MAP_FUNC(txsch->pfvf_map[schq]) != pcifunc)
2407 			continue;
2408 		/* Enable all LBK links with channel 63 by default so that
2409 		 * packets can be sent to LBK with a NPC TX MCAM rule
2410 		 */
2411 		lbk_links = hw->lbk_links;
2412 		while (lbk_links--)
2413 			rvu_write64(rvu, blkaddr,
2414 				    NIX_AF_TL3_TL2X_LINKX_CFG(schq,
2415 							      lbk_link_start +
2416 							      lbk_links),
2417 				    BIT_ULL(12) | RVU_SWITCH_LBK_CHAN);
2418 	}
2419 }
2420 
2421 int rvu_mbox_handler_nix_txschq_cfg(struct rvu *rvu,
2422 				    struct nix_txschq_config *req,
2423 				    struct nix_txschq_config *rsp)
2424 {
2425 	u64 reg, val, regval, schq_regbase, val_mask;
2426 	struct rvu_hwinfo *hw = rvu->hw;
2427 	u16 pcifunc = req->hdr.pcifunc;
2428 	struct nix_txsch *txsch;
2429 	struct nix_hw *nix_hw;
2430 	int blkaddr, idx, err;
2431 	int nixlf, schq;
2432 	u32 *pfvf_map;
2433 
2434 	if (req->lvl >= NIX_TXSCH_LVL_CNT ||
2435 	    req->num_regs > MAX_REGS_PER_MBOX_MSG)
2436 		return NIX_AF_INVAL_TXSCHQ_CFG;
2437 
2438 	err = nix_get_nixlf(rvu, pcifunc, &nixlf, &blkaddr);
2439 	if (err)
2440 		return err;
2441 
2442 	nix_hw = get_nix_hw(rvu->hw, blkaddr);
2443 	if (!nix_hw)
2444 		return NIX_AF_ERR_INVALID_NIXBLK;
2445 
2446 	if (req->read)
2447 		return nix_txschq_cfg_read(rvu, nix_hw, blkaddr, req, rsp);
2448 
2449 	txsch = &nix_hw->txsch[req->lvl];
2450 	pfvf_map = txsch->pfvf_map;
2451 
2452 	if (req->lvl >= hw->cap.nix_tx_aggr_lvl &&
2453 	    pcifunc & RVU_PFVF_FUNC_MASK) {
2454 		mutex_lock(&rvu->rsrc_lock);
2455 		if (req->lvl == NIX_TXSCH_LVL_TL1)
2456 			nix_tl1_default_cfg(rvu, nix_hw, pcifunc, blkaddr);
2457 		mutex_unlock(&rvu->rsrc_lock);
2458 		return 0;
2459 	}
2460 
2461 	for (idx = 0; idx < req->num_regs; idx++) {
2462 		reg = req->reg[idx];
2463 		reg &= NIX_TX_SCHQ_MASK;
2464 		regval = req->regval[idx];
2465 		schq_regbase = reg & 0xFFFF;
2466 		val_mask = req->regval_mask[idx];
2467 
2468 		if (!is_txschq_hierarchy_valid(rvu, pcifunc, blkaddr,
2469 					       txsch->lvl, reg, regval))
2470 			return NIX_AF_INVAL_TXSCHQ_CFG;
2471 
2472 		/* Check if shaping and coloring is supported */
2473 		if (!is_txschq_shaping_valid(hw, req->lvl, reg))
2474 			continue;
2475 
2476 		val = rvu_read64(rvu, blkaddr, reg);
2477 		regval = (val & val_mask) | (regval & ~val_mask);
2478 
2479 		/* Handle shaping state toggle specially */
2480 		if (hw->cap.nix_shaper_toggle_wait &&
2481 		    handle_txschq_shaper_update(rvu, blkaddr, nixlf,
2482 						req->lvl, reg, regval))
2483 			continue;
2484 
2485 		/* Replace PF/VF visible NIXLF slot with HW NIXLF id */
2486 		if (schq_regbase == NIX_AF_SMQX_CFG(0)) {
2487 			nixlf = rvu_get_lf(rvu, &hw->block[blkaddr],
2488 					   pcifunc, 0);
2489 			regval &= ~(0x7FULL << 24);
2490 			regval |= ((u64)nixlf << 24);
2491 		}
2492 
2493 		/* Clear 'BP_ENA' config, if it's not allowed */
2494 		if (!hw->cap.nix_tx_link_bp) {
2495 			if (schq_regbase == NIX_AF_TL4X_SDP_LINK_CFG(0) ||
2496 			    (schq_regbase & 0xFF00) ==
2497 			    NIX_AF_TL3_TL2X_LINKX_CFG(0, 0))
2498 				regval &= ~BIT_ULL(13);
2499 		}
2500 
2501 		/* Mark config as done for TL1 by PF */
2502 		if (schq_regbase >= NIX_AF_TL1X_SCHEDULE(0) &&
2503 		    schq_regbase <= NIX_AF_TL1X_GREEN_BYTES(0)) {
2504 			schq = TXSCHQ_IDX(reg, TXSCHQ_IDX_SHIFT);
2505 			mutex_lock(&rvu->rsrc_lock);
2506 			pfvf_map[schq] = TXSCH_SET_FLAG(pfvf_map[schq],
2507 							NIX_TXSCHQ_CFG_DONE);
2508 			mutex_unlock(&rvu->rsrc_lock);
2509 		}
2510 
2511 		/* SMQ flush is special hence split register writes such
2512 		 * that flush first and write rest of the bits later.
2513 		 */
2514 		if (schq_regbase == NIX_AF_SMQX_CFG(0) &&
2515 		    (regval & BIT_ULL(49))) {
2516 			schq = TXSCHQ_IDX(reg, TXSCHQ_IDX_SHIFT);
2517 			nix_smq_flush(rvu, blkaddr, schq, pcifunc, nixlf);
2518 			regval &= ~BIT_ULL(49);
2519 		}
2520 		rvu_write64(rvu, blkaddr, reg, regval);
2521 	}
2522 
2523 	rvu_nix_tx_tl2_cfg(rvu, blkaddr, pcifunc,
2524 			   &nix_hw->txsch[NIX_TXSCH_LVL_TL2]);
2525 	return 0;
2526 }
2527 
2528 static int nix_rx_vtag_cfg(struct rvu *rvu, int nixlf, int blkaddr,
2529 			   struct nix_vtag_config *req)
2530 {
2531 	u64 regval = req->vtag_size;
2532 
2533 	if (req->rx.vtag_type > NIX_AF_LFX_RX_VTAG_TYPE7 ||
2534 	    req->vtag_size > VTAGSIZE_T8)
2535 		return -EINVAL;
2536 
2537 	/* RX VTAG Type 7 reserved for vf vlan */
2538 	if (req->rx.vtag_type == NIX_AF_LFX_RX_VTAG_TYPE7)
2539 		return NIX_AF_ERR_RX_VTAG_INUSE;
2540 
2541 	if (req->rx.capture_vtag)
2542 		regval |= BIT_ULL(5);
2543 	if (req->rx.strip_vtag)
2544 		regval |= BIT_ULL(4);
2545 
2546 	rvu_write64(rvu, blkaddr,
2547 		    NIX_AF_LFX_RX_VTAG_TYPEX(nixlf, req->rx.vtag_type), regval);
2548 	return 0;
2549 }
2550 
2551 static int nix_tx_vtag_free(struct rvu *rvu, int blkaddr,
2552 			    u16 pcifunc, int index)
2553 {
2554 	struct nix_hw *nix_hw = get_nix_hw(rvu->hw, blkaddr);
2555 	struct nix_txvlan *vlan;
2556 
2557 	if (!nix_hw)
2558 		return NIX_AF_ERR_INVALID_NIXBLK;
2559 
2560 	vlan = &nix_hw->txvlan;
2561 	if (vlan->entry2pfvf_map[index] != pcifunc)
2562 		return NIX_AF_ERR_PARAM;
2563 
2564 	rvu_write64(rvu, blkaddr,
2565 		    NIX_AF_TX_VTAG_DEFX_DATA(index), 0x0ull);
2566 	rvu_write64(rvu, blkaddr,
2567 		    NIX_AF_TX_VTAG_DEFX_CTL(index), 0x0ull);
2568 
2569 	vlan->entry2pfvf_map[index] = 0;
2570 	rvu_free_rsrc(&vlan->rsrc, index);
2571 
2572 	return 0;
2573 }
2574 
2575 static void nix_free_tx_vtag_entries(struct rvu *rvu, u16 pcifunc)
2576 {
2577 	struct nix_txvlan *vlan;
2578 	struct nix_hw *nix_hw;
2579 	int index, blkaddr;
2580 
2581 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
2582 	if (blkaddr < 0)
2583 		return;
2584 
2585 	nix_hw = get_nix_hw(rvu->hw, blkaddr);
2586 	vlan = &nix_hw->txvlan;
2587 
2588 	mutex_lock(&vlan->rsrc_lock);
2589 	/* Scan all the entries and free the ones mapped to 'pcifunc' */
2590 	for (index = 0; index < vlan->rsrc.max; index++) {
2591 		if (vlan->entry2pfvf_map[index] == pcifunc)
2592 			nix_tx_vtag_free(rvu, blkaddr, pcifunc, index);
2593 	}
2594 	mutex_unlock(&vlan->rsrc_lock);
2595 }
2596 
2597 static int nix_tx_vtag_alloc(struct rvu *rvu, int blkaddr,
2598 			     u64 vtag, u8 size)
2599 {
2600 	struct nix_hw *nix_hw = get_nix_hw(rvu->hw, blkaddr);
2601 	struct nix_txvlan *vlan;
2602 	u64 regval;
2603 	int index;
2604 
2605 	if (!nix_hw)
2606 		return NIX_AF_ERR_INVALID_NIXBLK;
2607 
2608 	vlan = &nix_hw->txvlan;
2609 
2610 	mutex_lock(&vlan->rsrc_lock);
2611 
2612 	index = rvu_alloc_rsrc(&vlan->rsrc);
2613 	if (index < 0) {
2614 		mutex_unlock(&vlan->rsrc_lock);
2615 		return index;
2616 	}
2617 
2618 	mutex_unlock(&vlan->rsrc_lock);
2619 
2620 	regval = size ? vtag : vtag << 32;
2621 
2622 	rvu_write64(rvu, blkaddr,
2623 		    NIX_AF_TX_VTAG_DEFX_DATA(index), regval);
2624 	rvu_write64(rvu, blkaddr,
2625 		    NIX_AF_TX_VTAG_DEFX_CTL(index), size);
2626 
2627 	return index;
2628 }
2629 
2630 static int nix_tx_vtag_decfg(struct rvu *rvu, int blkaddr,
2631 			     struct nix_vtag_config *req)
2632 {
2633 	struct nix_hw *nix_hw = get_nix_hw(rvu->hw, blkaddr);
2634 	u16 pcifunc = req->hdr.pcifunc;
2635 	int idx0 = req->tx.vtag0_idx;
2636 	int idx1 = req->tx.vtag1_idx;
2637 	struct nix_txvlan *vlan;
2638 	int err = 0;
2639 
2640 	if (!nix_hw)
2641 		return NIX_AF_ERR_INVALID_NIXBLK;
2642 
2643 	vlan = &nix_hw->txvlan;
2644 	if (req->tx.free_vtag0 && req->tx.free_vtag1)
2645 		if (vlan->entry2pfvf_map[idx0] != pcifunc ||
2646 		    vlan->entry2pfvf_map[idx1] != pcifunc)
2647 			return NIX_AF_ERR_PARAM;
2648 
2649 	mutex_lock(&vlan->rsrc_lock);
2650 
2651 	if (req->tx.free_vtag0) {
2652 		err = nix_tx_vtag_free(rvu, blkaddr, pcifunc, idx0);
2653 		if (err)
2654 			goto exit;
2655 	}
2656 
2657 	if (req->tx.free_vtag1)
2658 		err = nix_tx_vtag_free(rvu, blkaddr, pcifunc, idx1);
2659 
2660 exit:
2661 	mutex_unlock(&vlan->rsrc_lock);
2662 	return err;
2663 }
2664 
2665 static int nix_tx_vtag_cfg(struct rvu *rvu, int blkaddr,
2666 			   struct nix_vtag_config *req,
2667 			   struct nix_vtag_config_rsp *rsp)
2668 {
2669 	struct nix_hw *nix_hw = get_nix_hw(rvu->hw, blkaddr);
2670 	struct nix_txvlan *vlan;
2671 	u16 pcifunc = req->hdr.pcifunc;
2672 
2673 	if (!nix_hw)
2674 		return NIX_AF_ERR_INVALID_NIXBLK;
2675 
2676 	vlan = &nix_hw->txvlan;
2677 	if (req->tx.cfg_vtag0) {
2678 		rsp->vtag0_idx =
2679 			nix_tx_vtag_alloc(rvu, blkaddr,
2680 					  req->tx.vtag0, req->vtag_size);
2681 
2682 		if (rsp->vtag0_idx < 0)
2683 			return NIX_AF_ERR_TX_VTAG_NOSPC;
2684 
2685 		vlan->entry2pfvf_map[rsp->vtag0_idx] = pcifunc;
2686 	}
2687 
2688 	if (req->tx.cfg_vtag1) {
2689 		rsp->vtag1_idx =
2690 			nix_tx_vtag_alloc(rvu, blkaddr,
2691 					  req->tx.vtag1, req->vtag_size);
2692 
2693 		if (rsp->vtag1_idx < 0)
2694 			goto err_free;
2695 
2696 		vlan->entry2pfvf_map[rsp->vtag1_idx] = pcifunc;
2697 	}
2698 
2699 	return 0;
2700 
2701 err_free:
2702 	if (req->tx.cfg_vtag0)
2703 		nix_tx_vtag_free(rvu, blkaddr, pcifunc, rsp->vtag0_idx);
2704 
2705 	return NIX_AF_ERR_TX_VTAG_NOSPC;
2706 }
2707 
2708 int rvu_mbox_handler_nix_vtag_cfg(struct rvu *rvu,
2709 				  struct nix_vtag_config *req,
2710 				  struct nix_vtag_config_rsp *rsp)
2711 {
2712 	u16 pcifunc = req->hdr.pcifunc;
2713 	int blkaddr, nixlf, err;
2714 
2715 	err = nix_get_nixlf(rvu, pcifunc, &nixlf, &blkaddr);
2716 	if (err)
2717 		return err;
2718 
2719 	if (req->cfg_type) {
2720 		/* rx vtag configuration */
2721 		err = nix_rx_vtag_cfg(rvu, nixlf, blkaddr, req);
2722 		if (err)
2723 			return NIX_AF_ERR_PARAM;
2724 	} else {
2725 		/* tx vtag configuration */
2726 		if ((req->tx.cfg_vtag0 || req->tx.cfg_vtag1) &&
2727 		    (req->tx.free_vtag0 || req->tx.free_vtag1))
2728 			return NIX_AF_ERR_PARAM;
2729 
2730 		if (req->tx.cfg_vtag0 || req->tx.cfg_vtag1)
2731 			return nix_tx_vtag_cfg(rvu, blkaddr, req, rsp);
2732 
2733 		if (req->tx.free_vtag0 || req->tx.free_vtag1)
2734 			return nix_tx_vtag_decfg(rvu, blkaddr, req);
2735 	}
2736 
2737 	return 0;
2738 }
2739 
2740 static int nix_blk_setup_mce(struct rvu *rvu, struct nix_hw *nix_hw,
2741 			     int mce, u8 op, u16 pcifunc, int next, bool eol)
2742 {
2743 	struct nix_aq_enq_req aq_req;
2744 	int err;
2745 
2746 	aq_req.hdr.pcifunc = 0;
2747 	aq_req.ctype = NIX_AQ_CTYPE_MCE;
2748 	aq_req.op = op;
2749 	aq_req.qidx = mce;
2750 
2751 	/* Use RSS with RSS index 0 */
2752 	aq_req.mce.op = 1;
2753 	aq_req.mce.index = 0;
2754 	aq_req.mce.eol = eol;
2755 	aq_req.mce.pf_func = pcifunc;
2756 	aq_req.mce.next = next;
2757 
2758 	/* All fields valid */
2759 	*(u64 *)(&aq_req.mce_mask) = ~0ULL;
2760 
2761 	err = rvu_nix_blk_aq_enq_inst(rvu, nix_hw, &aq_req, NULL);
2762 	if (err) {
2763 		dev_err(rvu->dev, "Failed to setup Bcast MCE for PF%d:VF%d\n",
2764 			rvu_get_pf(pcifunc), pcifunc & RVU_PFVF_FUNC_MASK);
2765 		return err;
2766 	}
2767 	return 0;
2768 }
2769 
2770 static int nix_update_mce_list_entry(struct nix_mce_list *mce_list,
2771 				     u16 pcifunc, bool add)
2772 {
2773 	struct mce *mce, *tail = NULL;
2774 	bool delete = false;
2775 
2776 	/* Scan through the current list */
2777 	hlist_for_each_entry(mce, &mce_list->head, node) {
2778 		/* If already exists, then delete */
2779 		if (mce->pcifunc == pcifunc && !add) {
2780 			delete = true;
2781 			break;
2782 		} else if (mce->pcifunc == pcifunc && add) {
2783 			/* entry already exists */
2784 			return 0;
2785 		}
2786 		tail = mce;
2787 	}
2788 
2789 	if (delete) {
2790 		hlist_del(&mce->node);
2791 		kfree(mce);
2792 		mce_list->count--;
2793 		return 0;
2794 	}
2795 
2796 	if (!add)
2797 		return 0;
2798 
2799 	/* Add a new one to the list, at the tail */
2800 	mce = kzalloc(sizeof(*mce), GFP_KERNEL);
2801 	if (!mce)
2802 		return -ENOMEM;
2803 	mce->pcifunc = pcifunc;
2804 	if (!tail)
2805 		hlist_add_head(&mce->node, &mce_list->head);
2806 	else
2807 		hlist_add_behind(&mce->node, &tail->node);
2808 	mce_list->count++;
2809 	return 0;
2810 }
2811 
2812 int nix_update_mce_list(struct rvu *rvu, u16 pcifunc,
2813 			struct nix_mce_list *mce_list,
2814 			int mce_idx, int mcam_index, bool add)
2815 {
2816 	int err = 0, idx, next_idx, last_idx, blkaddr, npc_blkaddr;
2817 	struct npc_mcam *mcam = &rvu->hw->mcam;
2818 	struct nix_mcast *mcast;
2819 	struct nix_hw *nix_hw;
2820 	struct mce *mce;
2821 
2822 	if (!mce_list)
2823 		return -EINVAL;
2824 
2825 	/* Get this PF/VF func's MCE index */
2826 	idx = mce_idx + (pcifunc & RVU_PFVF_FUNC_MASK);
2827 
2828 	if (idx > (mce_idx + mce_list->max)) {
2829 		dev_err(rvu->dev,
2830 			"%s: Idx %d > max MCE idx %d, for PF%d bcast list\n",
2831 			__func__, idx, mce_list->max,
2832 			pcifunc >> RVU_PFVF_PF_SHIFT);
2833 		return -EINVAL;
2834 	}
2835 
2836 	err = nix_get_struct_ptrs(rvu, pcifunc, &nix_hw, &blkaddr);
2837 	if (err)
2838 		return err;
2839 
2840 	mcast = &nix_hw->mcast;
2841 	mutex_lock(&mcast->mce_lock);
2842 
2843 	err = nix_update_mce_list_entry(mce_list, pcifunc, add);
2844 	if (err)
2845 		goto end;
2846 
2847 	/* Disable MCAM entry in NPC */
2848 	if (!mce_list->count) {
2849 		npc_blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2850 		npc_enable_mcam_entry(rvu, mcam, npc_blkaddr, mcam_index, false);
2851 		goto end;
2852 	}
2853 
2854 	/* Dump the updated list to HW */
2855 	idx = mce_idx;
2856 	last_idx = idx + mce_list->count - 1;
2857 	hlist_for_each_entry(mce, &mce_list->head, node) {
2858 		if (idx > last_idx)
2859 			break;
2860 
2861 		next_idx = idx + 1;
2862 		/* EOL should be set in last MCE */
2863 		err = nix_blk_setup_mce(rvu, nix_hw, idx, NIX_AQ_INSTOP_WRITE,
2864 					mce->pcifunc, next_idx,
2865 					(next_idx > last_idx) ? true : false);
2866 		if (err)
2867 			goto end;
2868 		idx++;
2869 	}
2870 
2871 end:
2872 	mutex_unlock(&mcast->mce_lock);
2873 	return err;
2874 }
2875 
2876 void nix_get_mce_list(struct rvu *rvu, u16 pcifunc, int type,
2877 		      struct nix_mce_list **mce_list, int *mce_idx)
2878 {
2879 	struct rvu_hwinfo *hw = rvu->hw;
2880 	struct rvu_pfvf *pfvf;
2881 
2882 	if (!hw->cap.nix_rx_multicast ||
2883 	    !is_pf_cgxmapped(rvu, rvu_get_pf(pcifunc & ~RVU_PFVF_FUNC_MASK))) {
2884 		*mce_list = NULL;
2885 		*mce_idx = 0;
2886 		return;
2887 	}
2888 
2889 	/* Get this PF/VF func's MCE index */
2890 	pfvf = rvu_get_pfvf(rvu, pcifunc & ~RVU_PFVF_FUNC_MASK);
2891 
2892 	if (type == NIXLF_BCAST_ENTRY) {
2893 		*mce_list = &pfvf->bcast_mce_list;
2894 		*mce_idx = pfvf->bcast_mce_idx;
2895 	} else if (type == NIXLF_ALLMULTI_ENTRY) {
2896 		*mce_list = &pfvf->mcast_mce_list;
2897 		*mce_idx = pfvf->mcast_mce_idx;
2898 	} else if (type == NIXLF_PROMISC_ENTRY) {
2899 		*mce_list = &pfvf->promisc_mce_list;
2900 		*mce_idx = pfvf->promisc_mce_idx;
2901 	}  else {
2902 		*mce_list = NULL;
2903 		*mce_idx = 0;
2904 	}
2905 }
2906 
2907 static int nix_update_mce_rule(struct rvu *rvu, u16 pcifunc,
2908 			       int type, bool add)
2909 {
2910 	int err = 0, nixlf, blkaddr, mcam_index, mce_idx;
2911 	struct npc_mcam *mcam = &rvu->hw->mcam;
2912 	struct rvu_hwinfo *hw = rvu->hw;
2913 	struct nix_mce_list *mce_list;
2914 	int pf;
2915 
2916 	/* skip multicast pkt replication for AF's VFs & SDP links */
2917 	if (is_afvf(pcifunc) || is_sdp_pfvf(pcifunc))
2918 		return 0;
2919 
2920 	if (!hw->cap.nix_rx_multicast)
2921 		return 0;
2922 
2923 	pf = rvu_get_pf(pcifunc);
2924 	if (!is_pf_cgxmapped(rvu, pf))
2925 		return 0;
2926 
2927 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
2928 	if (blkaddr < 0)
2929 		return -EINVAL;
2930 
2931 	nixlf = rvu_get_lf(rvu, &hw->block[blkaddr], pcifunc, 0);
2932 	if (nixlf < 0)
2933 		return -EINVAL;
2934 
2935 	nix_get_mce_list(rvu, pcifunc, type, &mce_list, &mce_idx);
2936 
2937 	mcam_index = npc_get_nixlf_mcam_index(mcam,
2938 					      pcifunc & ~RVU_PFVF_FUNC_MASK,
2939 					      nixlf, type);
2940 	err = nix_update_mce_list(rvu, pcifunc, mce_list,
2941 				  mce_idx, mcam_index, add);
2942 	return err;
2943 }
2944 
2945 static int nix_setup_mce_tables(struct rvu *rvu, struct nix_hw *nix_hw)
2946 {
2947 	struct nix_mcast *mcast = &nix_hw->mcast;
2948 	int err, pf, numvfs, idx;
2949 	struct rvu_pfvf *pfvf;
2950 	u16 pcifunc;
2951 	u64 cfg;
2952 
2953 	/* Skip PF0 (i.e AF) */
2954 	for (pf = 1; pf < (rvu->cgx_mapped_pfs + 1); pf++) {
2955 		cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_PFX_CFG(pf));
2956 		/* If PF is not enabled, nothing to do */
2957 		if (!((cfg >> 20) & 0x01))
2958 			continue;
2959 		/* Get numVFs attached to this PF */
2960 		numvfs = (cfg >> 12) & 0xFF;
2961 
2962 		pfvf = &rvu->pf[pf];
2963 
2964 		/* This NIX0/1 block mapped to PF ? */
2965 		if (pfvf->nix_blkaddr != nix_hw->blkaddr)
2966 			continue;
2967 
2968 		/* save start idx of broadcast mce list */
2969 		pfvf->bcast_mce_idx = nix_alloc_mce_list(mcast, numvfs + 1);
2970 		nix_mce_list_init(&pfvf->bcast_mce_list, numvfs + 1);
2971 
2972 		/* save start idx of multicast mce list */
2973 		pfvf->mcast_mce_idx = nix_alloc_mce_list(mcast, numvfs + 1);
2974 		nix_mce_list_init(&pfvf->mcast_mce_list, numvfs + 1);
2975 
2976 		/* save the start idx of promisc mce list */
2977 		pfvf->promisc_mce_idx = nix_alloc_mce_list(mcast, numvfs + 1);
2978 		nix_mce_list_init(&pfvf->promisc_mce_list, numvfs + 1);
2979 
2980 		for (idx = 0; idx < (numvfs + 1); idx++) {
2981 			/* idx-0 is for PF, followed by VFs */
2982 			pcifunc = (pf << RVU_PFVF_PF_SHIFT);
2983 			pcifunc |= idx;
2984 			/* Add dummy entries now, so that we don't have to check
2985 			 * for whether AQ_OP should be INIT/WRITE later on.
2986 			 * Will be updated when a NIXLF is attached/detached to
2987 			 * these PF/VFs.
2988 			 */
2989 			err = nix_blk_setup_mce(rvu, nix_hw,
2990 						pfvf->bcast_mce_idx + idx,
2991 						NIX_AQ_INSTOP_INIT,
2992 						pcifunc, 0, true);
2993 			if (err)
2994 				return err;
2995 
2996 			/* add dummy entries to multicast mce list */
2997 			err = nix_blk_setup_mce(rvu, nix_hw,
2998 						pfvf->mcast_mce_idx + idx,
2999 						NIX_AQ_INSTOP_INIT,
3000 						pcifunc, 0, true);
3001 			if (err)
3002 				return err;
3003 
3004 			/* add dummy entries to promisc mce list */
3005 			err = nix_blk_setup_mce(rvu, nix_hw,
3006 						pfvf->promisc_mce_idx + idx,
3007 						NIX_AQ_INSTOP_INIT,
3008 						pcifunc, 0, true);
3009 			if (err)
3010 				return err;
3011 		}
3012 	}
3013 	return 0;
3014 }
3015 
3016 static int nix_setup_mcast(struct rvu *rvu, struct nix_hw *nix_hw, int blkaddr)
3017 {
3018 	struct nix_mcast *mcast = &nix_hw->mcast;
3019 	struct rvu_hwinfo *hw = rvu->hw;
3020 	int err, size;
3021 
3022 	size = (rvu_read64(rvu, blkaddr, NIX_AF_CONST3) >> 16) & 0x0F;
3023 	size = (1ULL << size);
3024 
3025 	/* Alloc memory for multicast/mirror replication entries */
3026 	err = qmem_alloc(rvu->dev, &mcast->mce_ctx,
3027 			 (256UL << MC_TBL_SIZE), size);
3028 	if (err)
3029 		return -ENOMEM;
3030 
3031 	rvu_write64(rvu, blkaddr, NIX_AF_RX_MCAST_BASE,
3032 		    (u64)mcast->mce_ctx->iova);
3033 
3034 	/* Set max list length equal to max no of VFs per PF  + PF itself */
3035 	rvu_write64(rvu, blkaddr, NIX_AF_RX_MCAST_CFG,
3036 		    BIT_ULL(36) | (hw->max_vfs_per_pf << 4) | MC_TBL_SIZE);
3037 
3038 	/* Alloc memory for multicast replication buffers */
3039 	size = rvu_read64(rvu, blkaddr, NIX_AF_MC_MIRROR_CONST) & 0xFFFF;
3040 	err = qmem_alloc(rvu->dev, &mcast->mcast_buf,
3041 			 (8UL << MC_BUF_CNT), size);
3042 	if (err)
3043 		return -ENOMEM;
3044 
3045 	rvu_write64(rvu, blkaddr, NIX_AF_RX_MCAST_BUF_BASE,
3046 		    (u64)mcast->mcast_buf->iova);
3047 
3048 	/* Alloc pkind for NIX internal RX multicast/mirror replay */
3049 	mcast->replay_pkind = rvu_alloc_rsrc(&hw->pkind.rsrc);
3050 
3051 	rvu_write64(rvu, blkaddr, NIX_AF_RX_MCAST_BUF_CFG,
3052 		    BIT_ULL(63) | (mcast->replay_pkind << 24) |
3053 		    BIT_ULL(20) | MC_BUF_CNT);
3054 
3055 	mutex_init(&mcast->mce_lock);
3056 
3057 	return nix_setup_mce_tables(rvu, nix_hw);
3058 }
3059 
3060 static int nix_setup_txvlan(struct rvu *rvu, struct nix_hw *nix_hw)
3061 {
3062 	struct nix_txvlan *vlan = &nix_hw->txvlan;
3063 	int err;
3064 
3065 	/* Allocate resource bimap for tx vtag def registers*/
3066 	vlan->rsrc.max = NIX_TX_VTAG_DEF_MAX;
3067 	err = rvu_alloc_bitmap(&vlan->rsrc);
3068 	if (err)
3069 		return -ENOMEM;
3070 
3071 	/* Alloc memory for saving entry to RVU PFFUNC allocation mapping */
3072 	vlan->entry2pfvf_map = devm_kcalloc(rvu->dev, vlan->rsrc.max,
3073 					    sizeof(u16), GFP_KERNEL);
3074 	if (!vlan->entry2pfvf_map)
3075 		goto free_mem;
3076 
3077 	mutex_init(&vlan->rsrc_lock);
3078 	return 0;
3079 
3080 free_mem:
3081 	kfree(vlan->rsrc.bmap);
3082 	return -ENOMEM;
3083 }
3084 
3085 static int nix_setup_txschq(struct rvu *rvu, struct nix_hw *nix_hw, int blkaddr)
3086 {
3087 	struct nix_txsch *txsch;
3088 	int err, lvl, schq;
3089 	u64 cfg, reg;
3090 
3091 	/* Get scheduler queue count of each type and alloc
3092 	 * bitmap for each for alloc/free/attach operations.
3093 	 */
3094 	for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
3095 		txsch = &nix_hw->txsch[lvl];
3096 		txsch->lvl = lvl;
3097 		switch (lvl) {
3098 		case NIX_TXSCH_LVL_SMQ:
3099 			reg = NIX_AF_MDQ_CONST;
3100 			break;
3101 		case NIX_TXSCH_LVL_TL4:
3102 			reg = NIX_AF_TL4_CONST;
3103 			break;
3104 		case NIX_TXSCH_LVL_TL3:
3105 			reg = NIX_AF_TL3_CONST;
3106 			break;
3107 		case NIX_TXSCH_LVL_TL2:
3108 			reg = NIX_AF_TL2_CONST;
3109 			break;
3110 		case NIX_TXSCH_LVL_TL1:
3111 			reg = NIX_AF_TL1_CONST;
3112 			break;
3113 		}
3114 		cfg = rvu_read64(rvu, blkaddr, reg);
3115 		txsch->schq.max = cfg & 0xFFFF;
3116 		err = rvu_alloc_bitmap(&txsch->schq);
3117 		if (err)
3118 			return err;
3119 
3120 		/* Allocate memory for scheduler queues to
3121 		 * PF/VF pcifunc mapping info.
3122 		 */
3123 		txsch->pfvf_map = devm_kcalloc(rvu->dev, txsch->schq.max,
3124 					       sizeof(u32), GFP_KERNEL);
3125 		if (!txsch->pfvf_map)
3126 			return -ENOMEM;
3127 		for (schq = 0; schq < txsch->schq.max; schq++)
3128 			txsch->pfvf_map[schq] = TXSCH_MAP(0, NIX_TXSCHQ_FREE);
3129 	}
3130 
3131 	/* Setup a default value of 8192 as DWRR MTU */
3132 	if (rvu->hw->cap.nix_common_dwrr_mtu) {
3133 		rvu_write64(rvu, blkaddr, NIX_AF_DWRR_RPM_MTU,
3134 			    convert_bytes_to_dwrr_mtu(8192));
3135 		rvu_write64(rvu, blkaddr, NIX_AF_DWRR_SDP_MTU,
3136 			    convert_bytes_to_dwrr_mtu(8192));
3137 	}
3138 
3139 	return 0;
3140 }
3141 
3142 int rvu_nix_reserve_mark_format(struct rvu *rvu, struct nix_hw *nix_hw,
3143 				int blkaddr, u32 cfg)
3144 {
3145 	int fmt_idx;
3146 
3147 	for (fmt_idx = 0; fmt_idx < nix_hw->mark_format.in_use; fmt_idx++) {
3148 		if (nix_hw->mark_format.cfg[fmt_idx] == cfg)
3149 			return fmt_idx;
3150 	}
3151 	if (fmt_idx >= nix_hw->mark_format.total)
3152 		return -ERANGE;
3153 
3154 	rvu_write64(rvu, blkaddr, NIX_AF_MARK_FORMATX_CTL(fmt_idx), cfg);
3155 	nix_hw->mark_format.cfg[fmt_idx] = cfg;
3156 	nix_hw->mark_format.in_use++;
3157 	return fmt_idx;
3158 }
3159 
3160 static int nix_af_mark_format_setup(struct rvu *rvu, struct nix_hw *nix_hw,
3161 				    int blkaddr)
3162 {
3163 	u64 cfgs[] = {
3164 		[NIX_MARK_CFG_IP_DSCP_RED]         = 0x10003,
3165 		[NIX_MARK_CFG_IP_DSCP_YELLOW]      = 0x11200,
3166 		[NIX_MARK_CFG_IP_DSCP_YELLOW_RED]  = 0x11203,
3167 		[NIX_MARK_CFG_IP_ECN_RED]          = 0x6000c,
3168 		[NIX_MARK_CFG_IP_ECN_YELLOW]       = 0x60c00,
3169 		[NIX_MARK_CFG_IP_ECN_YELLOW_RED]   = 0x60c0c,
3170 		[NIX_MARK_CFG_VLAN_DEI_RED]        = 0x30008,
3171 		[NIX_MARK_CFG_VLAN_DEI_YELLOW]     = 0x30800,
3172 		[NIX_MARK_CFG_VLAN_DEI_YELLOW_RED] = 0x30808,
3173 	};
3174 	int i, rc;
3175 	u64 total;
3176 
3177 	total = (rvu_read64(rvu, blkaddr, NIX_AF_PSE_CONST) & 0xFF00) >> 8;
3178 	nix_hw->mark_format.total = (u8)total;
3179 	nix_hw->mark_format.cfg = devm_kcalloc(rvu->dev, total, sizeof(u32),
3180 					       GFP_KERNEL);
3181 	if (!nix_hw->mark_format.cfg)
3182 		return -ENOMEM;
3183 	for (i = 0; i < NIX_MARK_CFG_MAX; i++) {
3184 		rc = rvu_nix_reserve_mark_format(rvu, nix_hw, blkaddr, cfgs[i]);
3185 		if (rc < 0)
3186 			dev_err(rvu->dev, "Err %d in setup mark format %d\n",
3187 				i, rc);
3188 	}
3189 
3190 	return 0;
3191 }
3192 
3193 static void rvu_get_lbk_link_max_frs(struct rvu *rvu,  u16 *max_mtu)
3194 {
3195 	/* CN10K supports LBK FIFO size 72 KB */
3196 	if (rvu->hw->lbk_bufsize == 0x12000)
3197 		*max_mtu = CN10K_LBK_LINK_MAX_FRS;
3198 	else
3199 		*max_mtu = NIC_HW_MAX_FRS;
3200 }
3201 
3202 static void rvu_get_lmac_link_max_frs(struct rvu *rvu, u16 *max_mtu)
3203 {
3204 	/* RPM supports FIFO len 128 KB */
3205 	if (rvu_cgx_get_fifolen(rvu) == 0x20000)
3206 		*max_mtu = CN10K_LMAC_LINK_MAX_FRS;
3207 	else
3208 		*max_mtu = NIC_HW_MAX_FRS;
3209 }
3210 
3211 int rvu_mbox_handler_nix_get_hw_info(struct rvu *rvu, struct msg_req *req,
3212 				     struct nix_hw_info *rsp)
3213 {
3214 	u16 pcifunc = req->hdr.pcifunc;
3215 	u64 dwrr_mtu;
3216 	int blkaddr;
3217 
3218 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
3219 	if (blkaddr < 0)
3220 		return NIX_AF_ERR_AF_LF_INVALID;
3221 
3222 	if (is_afvf(pcifunc))
3223 		rvu_get_lbk_link_max_frs(rvu, &rsp->max_mtu);
3224 	else
3225 		rvu_get_lmac_link_max_frs(rvu, &rsp->max_mtu);
3226 
3227 	rsp->min_mtu = NIC_HW_MIN_FRS;
3228 
3229 	if (!rvu->hw->cap.nix_common_dwrr_mtu) {
3230 		/* Return '1' on OTx2 */
3231 		rsp->rpm_dwrr_mtu = 1;
3232 		rsp->sdp_dwrr_mtu = 1;
3233 		return 0;
3234 	}
3235 
3236 	dwrr_mtu = rvu_read64(rvu, BLKADDR_NIX0, NIX_AF_DWRR_RPM_MTU);
3237 	rsp->rpm_dwrr_mtu = convert_dwrr_mtu_to_bytes(dwrr_mtu);
3238 
3239 	dwrr_mtu = rvu_read64(rvu, BLKADDR_NIX0, NIX_AF_DWRR_SDP_MTU);
3240 	rsp->sdp_dwrr_mtu = convert_dwrr_mtu_to_bytes(dwrr_mtu);
3241 
3242 	return 0;
3243 }
3244 
3245 int rvu_mbox_handler_nix_stats_rst(struct rvu *rvu, struct msg_req *req,
3246 				   struct msg_rsp *rsp)
3247 {
3248 	u16 pcifunc = req->hdr.pcifunc;
3249 	int i, nixlf, blkaddr, err;
3250 	u64 stats;
3251 
3252 	err = nix_get_nixlf(rvu, pcifunc, &nixlf, &blkaddr);
3253 	if (err)
3254 		return err;
3255 
3256 	/* Get stats count supported by HW */
3257 	stats = rvu_read64(rvu, blkaddr, NIX_AF_CONST1);
3258 
3259 	/* Reset tx stats */
3260 	for (i = 0; i < ((stats >> 24) & 0xFF); i++)
3261 		rvu_write64(rvu, blkaddr, NIX_AF_LFX_TX_STATX(nixlf, i), 0);
3262 
3263 	/* Reset rx stats */
3264 	for (i = 0; i < ((stats >> 32) & 0xFF); i++)
3265 		rvu_write64(rvu, blkaddr, NIX_AF_LFX_RX_STATX(nixlf, i), 0);
3266 
3267 	return 0;
3268 }
3269 
3270 /* Returns the ALG index to be set into NPC_RX_ACTION */
3271 static int get_flowkey_alg_idx(struct nix_hw *nix_hw, u32 flow_cfg)
3272 {
3273 	int i;
3274 
3275 	/* Scan over exiting algo entries to find a match */
3276 	for (i = 0; i < nix_hw->flowkey.in_use; i++)
3277 		if (nix_hw->flowkey.flowkey[i] == flow_cfg)
3278 			return i;
3279 
3280 	return -ERANGE;
3281 }
3282 
3283 static int set_flowkey_fields(struct nix_rx_flowkey_alg *alg, u32 flow_cfg)
3284 {
3285 	int idx, nr_field, key_off, field_marker, keyoff_marker;
3286 	int max_key_off, max_bit_pos, group_member;
3287 	struct nix_rx_flowkey_alg *field;
3288 	struct nix_rx_flowkey_alg tmp;
3289 	u32 key_type, valid_key;
3290 	int l4_key_offset = 0;
3291 
3292 	if (!alg)
3293 		return -EINVAL;
3294 
3295 #define FIELDS_PER_ALG  5
3296 #define MAX_KEY_OFF	40
3297 	/* Clear all fields */
3298 	memset(alg, 0, sizeof(uint64_t) * FIELDS_PER_ALG);
3299 
3300 	/* Each of the 32 possible flow key algorithm definitions should
3301 	 * fall into above incremental config (except ALG0). Otherwise a
3302 	 * single NPC MCAM entry is not sufficient for supporting RSS.
3303 	 *
3304 	 * If a different definition or combination needed then NPC MCAM
3305 	 * has to be programmed to filter such pkts and it's action should
3306 	 * point to this definition to calculate flowtag or hash.
3307 	 *
3308 	 * The `for loop` goes over _all_ protocol field and the following
3309 	 * variables depicts the state machine forward progress logic.
3310 	 *
3311 	 * keyoff_marker - Enabled when hash byte length needs to be accounted
3312 	 * in field->key_offset update.
3313 	 * field_marker - Enabled when a new field needs to be selected.
3314 	 * group_member - Enabled when protocol is part of a group.
3315 	 */
3316 
3317 	keyoff_marker = 0; max_key_off = 0; group_member = 0;
3318 	nr_field = 0; key_off = 0; field_marker = 1;
3319 	field = &tmp; max_bit_pos = fls(flow_cfg);
3320 	for (idx = 0;
3321 	     idx < max_bit_pos && nr_field < FIELDS_PER_ALG &&
3322 	     key_off < MAX_KEY_OFF; idx++) {
3323 		key_type = BIT(idx);
3324 		valid_key = flow_cfg & key_type;
3325 		/* Found a field marker, reset the field values */
3326 		if (field_marker)
3327 			memset(&tmp, 0, sizeof(tmp));
3328 
3329 		field_marker = true;
3330 		keyoff_marker = true;
3331 		switch (key_type) {
3332 		case NIX_FLOW_KEY_TYPE_PORT:
3333 			field->sel_chan = true;
3334 			/* This should be set to 1, when SEL_CHAN is set */
3335 			field->bytesm1 = 1;
3336 			break;
3337 		case NIX_FLOW_KEY_TYPE_IPV4_PROTO:
3338 			field->lid = NPC_LID_LC;
3339 			field->hdr_offset = 9; /* offset */
3340 			field->bytesm1 = 0; /* 1 byte */
3341 			field->ltype_match = NPC_LT_LC_IP;
3342 			field->ltype_mask = 0xF;
3343 			break;
3344 		case NIX_FLOW_KEY_TYPE_IPV4:
3345 		case NIX_FLOW_KEY_TYPE_INNR_IPV4:
3346 			field->lid = NPC_LID_LC;
3347 			field->ltype_match = NPC_LT_LC_IP;
3348 			if (key_type == NIX_FLOW_KEY_TYPE_INNR_IPV4) {
3349 				field->lid = NPC_LID_LG;
3350 				field->ltype_match = NPC_LT_LG_TU_IP;
3351 			}
3352 			field->hdr_offset = 12; /* SIP offset */
3353 			field->bytesm1 = 7; /* SIP + DIP, 8 bytes */
3354 			field->ltype_mask = 0xF; /* Match only IPv4 */
3355 			keyoff_marker = false;
3356 			break;
3357 		case NIX_FLOW_KEY_TYPE_IPV6:
3358 		case NIX_FLOW_KEY_TYPE_INNR_IPV6:
3359 			field->lid = NPC_LID_LC;
3360 			field->ltype_match = NPC_LT_LC_IP6;
3361 			if (key_type == NIX_FLOW_KEY_TYPE_INNR_IPV6) {
3362 				field->lid = NPC_LID_LG;
3363 				field->ltype_match = NPC_LT_LG_TU_IP6;
3364 			}
3365 			field->hdr_offset = 8; /* SIP offset */
3366 			field->bytesm1 = 31; /* SIP + DIP, 32 bytes */
3367 			field->ltype_mask = 0xF; /* Match only IPv6 */
3368 			break;
3369 		case NIX_FLOW_KEY_TYPE_TCP:
3370 		case NIX_FLOW_KEY_TYPE_UDP:
3371 		case NIX_FLOW_KEY_TYPE_SCTP:
3372 		case NIX_FLOW_KEY_TYPE_INNR_TCP:
3373 		case NIX_FLOW_KEY_TYPE_INNR_UDP:
3374 		case NIX_FLOW_KEY_TYPE_INNR_SCTP:
3375 			field->lid = NPC_LID_LD;
3376 			if (key_type == NIX_FLOW_KEY_TYPE_INNR_TCP ||
3377 			    key_type == NIX_FLOW_KEY_TYPE_INNR_UDP ||
3378 			    key_type == NIX_FLOW_KEY_TYPE_INNR_SCTP)
3379 				field->lid = NPC_LID_LH;
3380 			field->bytesm1 = 3; /* Sport + Dport, 4 bytes */
3381 
3382 			/* Enum values for NPC_LID_LD and NPC_LID_LG are same,
3383 			 * so no need to change the ltype_match, just change
3384 			 * the lid for inner protocols
3385 			 */
3386 			BUILD_BUG_ON((int)NPC_LT_LD_TCP !=
3387 				     (int)NPC_LT_LH_TU_TCP);
3388 			BUILD_BUG_ON((int)NPC_LT_LD_UDP !=
3389 				     (int)NPC_LT_LH_TU_UDP);
3390 			BUILD_BUG_ON((int)NPC_LT_LD_SCTP !=
3391 				     (int)NPC_LT_LH_TU_SCTP);
3392 
3393 			if ((key_type == NIX_FLOW_KEY_TYPE_TCP ||
3394 			     key_type == NIX_FLOW_KEY_TYPE_INNR_TCP) &&
3395 			    valid_key) {
3396 				field->ltype_match |= NPC_LT_LD_TCP;
3397 				group_member = true;
3398 			} else if ((key_type == NIX_FLOW_KEY_TYPE_UDP ||
3399 				    key_type == NIX_FLOW_KEY_TYPE_INNR_UDP) &&
3400 				   valid_key) {
3401 				field->ltype_match |= NPC_LT_LD_UDP;
3402 				group_member = true;
3403 			} else if ((key_type == NIX_FLOW_KEY_TYPE_SCTP ||
3404 				    key_type == NIX_FLOW_KEY_TYPE_INNR_SCTP) &&
3405 				   valid_key) {
3406 				field->ltype_match |= NPC_LT_LD_SCTP;
3407 				group_member = true;
3408 			}
3409 			field->ltype_mask = ~field->ltype_match;
3410 			if (key_type == NIX_FLOW_KEY_TYPE_SCTP ||
3411 			    key_type == NIX_FLOW_KEY_TYPE_INNR_SCTP) {
3412 				/* Handle the case where any of the group item
3413 				 * is enabled in the group but not the final one
3414 				 */
3415 				if (group_member) {
3416 					valid_key = true;
3417 					group_member = false;
3418 				}
3419 			} else {
3420 				field_marker = false;
3421 				keyoff_marker = false;
3422 			}
3423 
3424 			/* TCP/UDP/SCTP and ESP/AH falls at same offset so
3425 			 * remember the TCP key offset of 40 byte hash key.
3426 			 */
3427 			if (key_type == NIX_FLOW_KEY_TYPE_TCP)
3428 				l4_key_offset = key_off;
3429 			break;
3430 		case NIX_FLOW_KEY_TYPE_NVGRE:
3431 			field->lid = NPC_LID_LD;
3432 			field->hdr_offset = 4; /* VSID offset */
3433 			field->bytesm1 = 2;
3434 			field->ltype_match = NPC_LT_LD_NVGRE;
3435 			field->ltype_mask = 0xF;
3436 			break;
3437 		case NIX_FLOW_KEY_TYPE_VXLAN:
3438 		case NIX_FLOW_KEY_TYPE_GENEVE:
3439 			field->lid = NPC_LID_LE;
3440 			field->bytesm1 = 2;
3441 			field->hdr_offset = 4;
3442 			field->ltype_mask = 0xF;
3443 			field_marker = false;
3444 			keyoff_marker = false;
3445 
3446 			if (key_type == NIX_FLOW_KEY_TYPE_VXLAN && valid_key) {
3447 				field->ltype_match |= NPC_LT_LE_VXLAN;
3448 				group_member = true;
3449 			}
3450 
3451 			if (key_type == NIX_FLOW_KEY_TYPE_GENEVE && valid_key) {
3452 				field->ltype_match |= NPC_LT_LE_GENEVE;
3453 				group_member = true;
3454 			}
3455 
3456 			if (key_type == NIX_FLOW_KEY_TYPE_GENEVE) {
3457 				if (group_member) {
3458 					field->ltype_mask = ~field->ltype_match;
3459 					field_marker = true;
3460 					keyoff_marker = true;
3461 					valid_key = true;
3462 					group_member = false;
3463 				}
3464 			}
3465 			break;
3466 		case NIX_FLOW_KEY_TYPE_ETH_DMAC:
3467 		case NIX_FLOW_KEY_TYPE_INNR_ETH_DMAC:
3468 			field->lid = NPC_LID_LA;
3469 			field->ltype_match = NPC_LT_LA_ETHER;
3470 			if (key_type == NIX_FLOW_KEY_TYPE_INNR_ETH_DMAC) {
3471 				field->lid = NPC_LID_LF;
3472 				field->ltype_match = NPC_LT_LF_TU_ETHER;
3473 			}
3474 			field->hdr_offset = 0;
3475 			field->bytesm1 = 5; /* DMAC 6 Byte */
3476 			field->ltype_mask = 0xF;
3477 			break;
3478 		case NIX_FLOW_KEY_TYPE_IPV6_EXT:
3479 			field->lid = NPC_LID_LC;
3480 			field->hdr_offset = 40; /* IPV6 hdr */
3481 			field->bytesm1 = 0; /* 1 Byte ext hdr*/
3482 			field->ltype_match = NPC_LT_LC_IP6_EXT;
3483 			field->ltype_mask = 0xF;
3484 			break;
3485 		case NIX_FLOW_KEY_TYPE_GTPU:
3486 			field->lid = NPC_LID_LE;
3487 			field->hdr_offset = 4;
3488 			field->bytesm1 = 3; /* 4 bytes TID*/
3489 			field->ltype_match = NPC_LT_LE_GTPU;
3490 			field->ltype_mask = 0xF;
3491 			break;
3492 		case NIX_FLOW_KEY_TYPE_VLAN:
3493 			field->lid = NPC_LID_LB;
3494 			field->hdr_offset = 2; /* Skip TPID (2-bytes) */
3495 			field->bytesm1 = 1; /* 2 Bytes (Actually 12 bits) */
3496 			field->ltype_match = NPC_LT_LB_CTAG;
3497 			field->ltype_mask = 0xF;
3498 			field->fn_mask = 1; /* Mask out the first nibble */
3499 			break;
3500 		case NIX_FLOW_KEY_TYPE_AH:
3501 		case NIX_FLOW_KEY_TYPE_ESP:
3502 			field->hdr_offset = 0;
3503 			field->bytesm1 = 7; /* SPI + sequence number */
3504 			field->ltype_mask = 0xF;
3505 			field->lid = NPC_LID_LE;
3506 			field->ltype_match = NPC_LT_LE_ESP;
3507 			if (key_type == NIX_FLOW_KEY_TYPE_AH) {
3508 				field->lid = NPC_LID_LD;
3509 				field->ltype_match = NPC_LT_LD_AH;
3510 				field->hdr_offset = 4;
3511 				keyoff_marker = false;
3512 			}
3513 			break;
3514 		}
3515 		field->ena = 1;
3516 
3517 		/* Found a valid flow key type */
3518 		if (valid_key) {
3519 			/* Use the key offset of TCP/UDP/SCTP fields
3520 			 * for ESP/AH fields.
3521 			 */
3522 			if (key_type == NIX_FLOW_KEY_TYPE_ESP ||
3523 			    key_type == NIX_FLOW_KEY_TYPE_AH)
3524 				key_off = l4_key_offset;
3525 			field->key_offset = key_off;
3526 			memcpy(&alg[nr_field], field, sizeof(*field));
3527 			max_key_off = max(max_key_off, field->bytesm1 + 1);
3528 
3529 			/* Found a field marker, get the next field */
3530 			if (field_marker)
3531 				nr_field++;
3532 		}
3533 
3534 		/* Found a keyoff marker, update the new key_off */
3535 		if (keyoff_marker) {
3536 			key_off += max_key_off;
3537 			max_key_off = 0;
3538 		}
3539 	}
3540 	/* Processed all the flow key types */
3541 	if (idx == max_bit_pos && key_off <= MAX_KEY_OFF)
3542 		return 0;
3543 	else
3544 		return NIX_AF_ERR_RSS_NOSPC_FIELD;
3545 }
3546 
3547 static int reserve_flowkey_alg_idx(struct rvu *rvu, int blkaddr, u32 flow_cfg)
3548 {
3549 	u64 field[FIELDS_PER_ALG];
3550 	struct nix_hw *hw;
3551 	int fid, rc;
3552 
3553 	hw = get_nix_hw(rvu->hw, blkaddr);
3554 	if (!hw)
3555 		return NIX_AF_ERR_INVALID_NIXBLK;
3556 
3557 	/* No room to add new flow hash algoritham */
3558 	if (hw->flowkey.in_use >= NIX_FLOW_KEY_ALG_MAX)
3559 		return NIX_AF_ERR_RSS_NOSPC_ALGO;
3560 
3561 	/* Generate algo fields for the given flow_cfg */
3562 	rc = set_flowkey_fields((struct nix_rx_flowkey_alg *)field, flow_cfg);
3563 	if (rc)
3564 		return rc;
3565 
3566 	/* Update ALGX_FIELDX register with generated fields */
3567 	for (fid = 0; fid < FIELDS_PER_ALG; fid++)
3568 		rvu_write64(rvu, blkaddr,
3569 			    NIX_AF_RX_FLOW_KEY_ALGX_FIELDX(hw->flowkey.in_use,
3570 							   fid), field[fid]);
3571 
3572 	/* Store the flow_cfg for futher lookup */
3573 	rc = hw->flowkey.in_use;
3574 	hw->flowkey.flowkey[rc] = flow_cfg;
3575 	hw->flowkey.in_use++;
3576 
3577 	return rc;
3578 }
3579 
3580 int rvu_mbox_handler_nix_rss_flowkey_cfg(struct rvu *rvu,
3581 					 struct nix_rss_flowkey_cfg *req,
3582 					 struct nix_rss_flowkey_cfg_rsp *rsp)
3583 {
3584 	u16 pcifunc = req->hdr.pcifunc;
3585 	int alg_idx, nixlf, blkaddr;
3586 	struct nix_hw *nix_hw;
3587 	int err;
3588 
3589 	err = nix_get_nixlf(rvu, pcifunc, &nixlf, &blkaddr);
3590 	if (err)
3591 		return err;
3592 
3593 	nix_hw = get_nix_hw(rvu->hw, blkaddr);
3594 	if (!nix_hw)
3595 		return NIX_AF_ERR_INVALID_NIXBLK;
3596 
3597 	alg_idx = get_flowkey_alg_idx(nix_hw, req->flowkey_cfg);
3598 	/* Failed to get algo index from the exiting list, reserve new  */
3599 	if (alg_idx < 0) {
3600 		alg_idx = reserve_flowkey_alg_idx(rvu, blkaddr,
3601 						  req->flowkey_cfg);
3602 		if (alg_idx < 0)
3603 			return alg_idx;
3604 	}
3605 	rsp->alg_idx = alg_idx;
3606 	rvu_npc_update_flowkey_alg_idx(rvu, pcifunc, nixlf, req->group,
3607 				       alg_idx, req->mcam_index);
3608 	return 0;
3609 }
3610 
3611 static int nix_rx_flowkey_alg_cfg(struct rvu *rvu, int blkaddr)
3612 {
3613 	u32 flowkey_cfg, minkey_cfg;
3614 	int alg, fid, rc;
3615 
3616 	/* Disable all flow key algx fieldx */
3617 	for (alg = 0; alg < NIX_FLOW_KEY_ALG_MAX; alg++) {
3618 		for (fid = 0; fid < FIELDS_PER_ALG; fid++)
3619 			rvu_write64(rvu, blkaddr,
3620 				    NIX_AF_RX_FLOW_KEY_ALGX_FIELDX(alg, fid),
3621 				    0);
3622 	}
3623 
3624 	/* IPv4/IPv6 SIP/DIPs */
3625 	flowkey_cfg = NIX_FLOW_KEY_TYPE_IPV4 | NIX_FLOW_KEY_TYPE_IPV6;
3626 	rc = reserve_flowkey_alg_idx(rvu, blkaddr, flowkey_cfg);
3627 	if (rc < 0)
3628 		return rc;
3629 
3630 	/* TCPv4/v6 4-tuple, SIP, DIP, Sport, Dport */
3631 	minkey_cfg = flowkey_cfg;
3632 	flowkey_cfg = minkey_cfg | NIX_FLOW_KEY_TYPE_TCP;
3633 	rc = reserve_flowkey_alg_idx(rvu, blkaddr, flowkey_cfg);
3634 	if (rc < 0)
3635 		return rc;
3636 
3637 	/* UDPv4/v6 4-tuple, SIP, DIP, Sport, Dport */
3638 	flowkey_cfg = minkey_cfg | NIX_FLOW_KEY_TYPE_UDP;
3639 	rc = reserve_flowkey_alg_idx(rvu, blkaddr, flowkey_cfg);
3640 	if (rc < 0)
3641 		return rc;
3642 
3643 	/* SCTPv4/v6 4-tuple, SIP, DIP, Sport, Dport */
3644 	flowkey_cfg = minkey_cfg | NIX_FLOW_KEY_TYPE_SCTP;
3645 	rc = reserve_flowkey_alg_idx(rvu, blkaddr, flowkey_cfg);
3646 	if (rc < 0)
3647 		return rc;
3648 
3649 	/* TCP/UDP v4/v6 4-tuple, rest IP pkts 2-tuple */
3650 	flowkey_cfg = minkey_cfg | NIX_FLOW_KEY_TYPE_TCP |
3651 			NIX_FLOW_KEY_TYPE_UDP;
3652 	rc = reserve_flowkey_alg_idx(rvu, blkaddr, flowkey_cfg);
3653 	if (rc < 0)
3654 		return rc;
3655 
3656 	/* TCP/SCTP v4/v6 4-tuple, rest IP pkts 2-tuple */
3657 	flowkey_cfg = minkey_cfg | NIX_FLOW_KEY_TYPE_TCP |
3658 			NIX_FLOW_KEY_TYPE_SCTP;
3659 	rc = reserve_flowkey_alg_idx(rvu, blkaddr, flowkey_cfg);
3660 	if (rc < 0)
3661 		return rc;
3662 
3663 	/* UDP/SCTP v4/v6 4-tuple, rest IP pkts 2-tuple */
3664 	flowkey_cfg = minkey_cfg | NIX_FLOW_KEY_TYPE_UDP |
3665 			NIX_FLOW_KEY_TYPE_SCTP;
3666 	rc = reserve_flowkey_alg_idx(rvu, blkaddr, flowkey_cfg);
3667 	if (rc < 0)
3668 		return rc;
3669 
3670 	/* TCP/UDP/SCTP v4/v6 4-tuple, rest IP pkts 2-tuple */
3671 	flowkey_cfg = minkey_cfg | NIX_FLOW_KEY_TYPE_TCP |
3672 		      NIX_FLOW_KEY_TYPE_UDP | NIX_FLOW_KEY_TYPE_SCTP;
3673 	rc = reserve_flowkey_alg_idx(rvu, blkaddr, flowkey_cfg);
3674 	if (rc < 0)
3675 		return rc;
3676 
3677 	return 0;
3678 }
3679 
3680 int rvu_mbox_handler_nix_set_mac_addr(struct rvu *rvu,
3681 				      struct nix_set_mac_addr *req,
3682 				      struct msg_rsp *rsp)
3683 {
3684 	bool from_vf = req->hdr.pcifunc & RVU_PFVF_FUNC_MASK;
3685 	u16 pcifunc = req->hdr.pcifunc;
3686 	int blkaddr, nixlf, err;
3687 	struct rvu_pfvf *pfvf;
3688 
3689 	err = nix_get_nixlf(rvu, pcifunc, &nixlf, &blkaddr);
3690 	if (err)
3691 		return err;
3692 
3693 	pfvf = rvu_get_pfvf(rvu, pcifunc);
3694 
3695 	/* untrusted VF can't overwrite admin(PF) changes */
3696 	if (!test_bit(PF_SET_VF_TRUSTED, &pfvf->flags) &&
3697 	    (from_vf && test_bit(PF_SET_VF_MAC, &pfvf->flags))) {
3698 		dev_warn(rvu->dev,
3699 			 "MAC address set by admin(PF) cannot be overwritten by untrusted VF");
3700 		return -EPERM;
3701 	}
3702 
3703 	ether_addr_copy(pfvf->mac_addr, req->mac_addr);
3704 
3705 	rvu_npc_install_ucast_entry(rvu, pcifunc, nixlf,
3706 				    pfvf->rx_chan_base, req->mac_addr);
3707 
3708 	if (test_bit(PF_SET_VF_TRUSTED, &pfvf->flags) && from_vf)
3709 		ether_addr_copy(pfvf->default_mac, req->mac_addr);
3710 
3711 	rvu_switch_update_rules(rvu, pcifunc);
3712 
3713 	return 0;
3714 }
3715 
3716 int rvu_mbox_handler_nix_get_mac_addr(struct rvu *rvu,
3717 				      struct msg_req *req,
3718 				      struct nix_get_mac_addr_rsp *rsp)
3719 {
3720 	u16 pcifunc = req->hdr.pcifunc;
3721 	struct rvu_pfvf *pfvf;
3722 
3723 	if (!is_nixlf_attached(rvu, pcifunc))
3724 		return NIX_AF_ERR_AF_LF_INVALID;
3725 
3726 	pfvf = rvu_get_pfvf(rvu, pcifunc);
3727 
3728 	ether_addr_copy(rsp->mac_addr, pfvf->mac_addr);
3729 
3730 	return 0;
3731 }
3732 
3733 int rvu_mbox_handler_nix_set_rx_mode(struct rvu *rvu, struct nix_rx_mode *req,
3734 				     struct msg_rsp *rsp)
3735 {
3736 	bool allmulti, promisc, nix_rx_multicast;
3737 	u16 pcifunc = req->hdr.pcifunc;
3738 	struct rvu_pfvf *pfvf;
3739 	int nixlf, err;
3740 
3741 	pfvf = rvu_get_pfvf(rvu, pcifunc);
3742 	promisc = req->mode & NIX_RX_MODE_PROMISC ? true : false;
3743 	allmulti = req->mode & NIX_RX_MODE_ALLMULTI ? true : false;
3744 	pfvf->use_mce_list = req->mode & NIX_RX_MODE_USE_MCE ? true : false;
3745 
3746 	nix_rx_multicast = rvu->hw->cap.nix_rx_multicast & pfvf->use_mce_list;
3747 
3748 	if (is_vf(pcifunc) && !nix_rx_multicast &&
3749 	    (promisc || allmulti)) {
3750 		dev_warn_ratelimited(rvu->dev,
3751 				     "VF promisc/multicast not supported\n");
3752 		return 0;
3753 	}
3754 
3755 	/* untrusted VF can't configure promisc/allmulti */
3756 	if (is_vf(pcifunc) && !test_bit(PF_SET_VF_TRUSTED, &pfvf->flags) &&
3757 	    (promisc || allmulti))
3758 		return 0;
3759 
3760 	err = nix_get_nixlf(rvu, pcifunc, &nixlf, NULL);
3761 	if (err)
3762 		return err;
3763 
3764 	if (nix_rx_multicast) {
3765 		/* add/del this PF_FUNC to/from mcast pkt replication list */
3766 		err = nix_update_mce_rule(rvu, pcifunc, NIXLF_ALLMULTI_ENTRY,
3767 					  allmulti);
3768 		if (err) {
3769 			dev_err(rvu->dev,
3770 				"Failed to update pcifunc 0x%x to multicast list\n",
3771 				pcifunc);
3772 			return err;
3773 		}
3774 
3775 		/* add/del this PF_FUNC to/from promisc pkt replication list */
3776 		err = nix_update_mce_rule(rvu, pcifunc, NIXLF_PROMISC_ENTRY,
3777 					  promisc);
3778 		if (err) {
3779 			dev_err(rvu->dev,
3780 				"Failed to update pcifunc 0x%x to promisc list\n",
3781 				pcifunc);
3782 			return err;
3783 		}
3784 	}
3785 
3786 	/* install/uninstall allmulti entry */
3787 	if (allmulti) {
3788 		rvu_npc_install_allmulti_entry(rvu, pcifunc, nixlf,
3789 					       pfvf->rx_chan_base);
3790 	} else {
3791 		if (!nix_rx_multicast)
3792 			rvu_npc_enable_allmulti_entry(rvu, pcifunc, nixlf, false);
3793 	}
3794 
3795 	/* install/uninstall promisc entry */
3796 	if (promisc) {
3797 		rvu_npc_install_promisc_entry(rvu, pcifunc, nixlf,
3798 					      pfvf->rx_chan_base,
3799 					      pfvf->rx_chan_cnt);
3800 	} else {
3801 		if (!nix_rx_multicast)
3802 			rvu_npc_enable_promisc_entry(rvu, pcifunc, nixlf, false);
3803 	}
3804 
3805 	return 0;
3806 }
3807 
3808 static void nix_find_link_frs(struct rvu *rvu,
3809 			      struct nix_frs_cfg *req, u16 pcifunc)
3810 {
3811 	int pf = rvu_get_pf(pcifunc);
3812 	struct rvu_pfvf *pfvf;
3813 	int maxlen, minlen;
3814 	int numvfs, hwvf;
3815 	int vf;
3816 
3817 	/* Update with requester's min/max lengths */
3818 	pfvf = rvu_get_pfvf(rvu, pcifunc);
3819 	pfvf->maxlen = req->maxlen;
3820 	if (req->update_minlen)
3821 		pfvf->minlen = req->minlen;
3822 
3823 	maxlen = req->maxlen;
3824 	minlen = req->update_minlen ? req->minlen : 0;
3825 
3826 	/* Get this PF's numVFs and starting hwvf */
3827 	rvu_get_pf_numvfs(rvu, pf, &numvfs, &hwvf);
3828 
3829 	/* For each VF, compare requested max/minlen */
3830 	for (vf = 0; vf < numvfs; vf++) {
3831 		pfvf =  &rvu->hwvf[hwvf + vf];
3832 		if (pfvf->maxlen > maxlen)
3833 			maxlen = pfvf->maxlen;
3834 		if (req->update_minlen &&
3835 		    pfvf->minlen && pfvf->minlen < minlen)
3836 			minlen = pfvf->minlen;
3837 	}
3838 
3839 	/* Compare requested max/minlen with PF's max/minlen */
3840 	pfvf = &rvu->pf[pf];
3841 	if (pfvf->maxlen > maxlen)
3842 		maxlen = pfvf->maxlen;
3843 	if (req->update_minlen &&
3844 	    pfvf->minlen && pfvf->minlen < minlen)
3845 		minlen = pfvf->minlen;
3846 
3847 	/* Update the request with max/min PF's and it's VF's max/min */
3848 	req->maxlen = maxlen;
3849 	if (req->update_minlen)
3850 		req->minlen = minlen;
3851 }
3852 
3853 static int
3854 nix_config_link_credits(struct rvu *rvu, int blkaddr, int link,
3855 			u16 pcifunc, u64 tx_credits)
3856 {
3857 	struct rvu_hwinfo *hw = rvu->hw;
3858 	int pf = rvu_get_pf(pcifunc);
3859 	u8 cgx_id = 0, lmac_id = 0;
3860 	unsigned long poll_tmo;
3861 	bool restore_tx_en = 0;
3862 	struct nix_hw *nix_hw;
3863 	u64 cfg, sw_xoff = 0;
3864 	u32 schq = 0;
3865 	u32 credits;
3866 	int rc;
3867 
3868 	nix_hw = get_nix_hw(rvu->hw, blkaddr);
3869 	if (!nix_hw)
3870 		return NIX_AF_ERR_INVALID_NIXBLK;
3871 
3872 	if (tx_credits == nix_hw->tx_credits[link])
3873 		return 0;
3874 
3875 	/* Enable cgx tx if disabled for credits to be back */
3876 	if (is_pf_cgxmapped(rvu, pf)) {
3877 		rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
3878 		restore_tx_en = !cgx_lmac_tx_enable(rvu_cgx_pdata(cgx_id, rvu),
3879 						    lmac_id, true);
3880 	}
3881 
3882 	mutex_lock(&rvu->rsrc_lock);
3883 	/* Disable new traffic to link */
3884 	if (hw->cap.nix_shaping) {
3885 		schq = nix_get_tx_link(rvu, pcifunc);
3886 		sw_xoff = rvu_read64(rvu, blkaddr, NIX_AF_TL1X_SW_XOFF(schq));
3887 		rvu_write64(rvu, blkaddr,
3888 			    NIX_AF_TL1X_SW_XOFF(schq), BIT_ULL(0));
3889 	}
3890 
3891 	rc = -EBUSY;
3892 	poll_tmo = jiffies + usecs_to_jiffies(10000);
3893 	/* Wait for credits to return */
3894 	do {
3895 		if (time_after(jiffies, poll_tmo))
3896 			goto exit;
3897 		usleep_range(100, 200);
3898 
3899 		cfg = rvu_read64(rvu, blkaddr,
3900 				 NIX_AF_TX_LINKX_NORM_CREDIT(link));
3901 		credits = (cfg >> 12) & 0xFFFFFULL;
3902 	} while (credits != nix_hw->tx_credits[link]);
3903 
3904 	cfg &= ~(0xFFFFFULL << 12);
3905 	cfg |= (tx_credits << 12);
3906 	rvu_write64(rvu, blkaddr, NIX_AF_TX_LINKX_NORM_CREDIT(link), cfg);
3907 	rc = 0;
3908 
3909 	nix_hw->tx_credits[link] = tx_credits;
3910 
3911 exit:
3912 	/* Enable traffic back */
3913 	if (hw->cap.nix_shaping && !sw_xoff)
3914 		rvu_write64(rvu, blkaddr, NIX_AF_TL1X_SW_XOFF(schq), 0);
3915 
3916 	/* Restore state of cgx tx */
3917 	if (restore_tx_en)
3918 		cgx_lmac_tx_enable(rvu_cgx_pdata(cgx_id, rvu), lmac_id, false);
3919 
3920 	mutex_unlock(&rvu->rsrc_lock);
3921 	return rc;
3922 }
3923 
3924 int rvu_mbox_handler_nix_set_hw_frs(struct rvu *rvu, struct nix_frs_cfg *req,
3925 				    struct msg_rsp *rsp)
3926 {
3927 	struct rvu_hwinfo *hw = rvu->hw;
3928 	u16 pcifunc = req->hdr.pcifunc;
3929 	int pf = rvu_get_pf(pcifunc);
3930 	int blkaddr, schq, link = -1;
3931 	struct nix_txsch *txsch;
3932 	u64 cfg, lmac_fifo_len;
3933 	struct nix_hw *nix_hw;
3934 	struct rvu_pfvf *pfvf;
3935 	u8 cgx = 0, lmac = 0;
3936 	u16 max_mtu;
3937 
3938 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
3939 	if (blkaddr < 0)
3940 		return NIX_AF_ERR_AF_LF_INVALID;
3941 
3942 	nix_hw = get_nix_hw(rvu->hw, blkaddr);
3943 	if (!nix_hw)
3944 		return NIX_AF_ERR_INVALID_NIXBLK;
3945 
3946 	if (is_afvf(pcifunc))
3947 		rvu_get_lbk_link_max_frs(rvu, &max_mtu);
3948 	else
3949 		rvu_get_lmac_link_max_frs(rvu, &max_mtu);
3950 
3951 	if (!req->sdp_link && req->maxlen > max_mtu)
3952 		return NIX_AF_ERR_FRS_INVALID;
3953 
3954 	if (req->update_minlen && req->minlen < NIC_HW_MIN_FRS)
3955 		return NIX_AF_ERR_FRS_INVALID;
3956 
3957 	/* Check if requester wants to update SMQ's */
3958 	if (!req->update_smq)
3959 		goto rx_frscfg;
3960 
3961 	/* Update min/maxlen in each of the SMQ attached to this PF/VF */
3962 	txsch = &nix_hw->txsch[NIX_TXSCH_LVL_SMQ];
3963 	mutex_lock(&rvu->rsrc_lock);
3964 	for (schq = 0; schq < txsch->schq.max; schq++) {
3965 		if (TXSCH_MAP_FUNC(txsch->pfvf_map[schq]) != pcifunc)
3966 			continue;
3967 		cfg = rvu_read64(rvu, blkaddr, NIX_AF_SMQX_CFG(schq));
3968 		cfg = (cfg & ~(0xFFFFULL << 8)) | ((u64)req->maxlen << 8);
3969 		if (req->update_minlen)
3970 			cfg = (cfg & ~0x7FULL) | ((u64)req->minlen & 0x7F);
3971 		rvu_write64(rvu, blkaddr, NIX_AF_SMQX_CFG(schq), cfg);
3972 	}
3973 	mutex_unlock(&rvu->rsrc_lock);
3974 
3975 rx_frscfg:
3976 	/* Check if config is for SDP link */
3977 	if (req->sdp_link) {
3978 		if (!hw->sdp_links)
3979 			return NIX_AF_ERR_RX_LINK_INVALID;
3980 		link = hw->cgx_links + hw->lbk_links;
3981 		goto linkcfg;
3982 	}
3983 
3984 	/* Check if the request is from CGX mapped RVU PF */
3985 	if (is_pf_cgxmapped(rvu, pf)) {
3986 		/* Get CGX and LMAC to which this PF is mapped and find link */
3987 		rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx, &lmac);
3988 		link = (cgx * hw->lmac_per_cgx) + lmac;
3989 	} else if (pf == 0) {
3990 		/* For VFs of PF0 ingress is LBK port, so config LBK link */
3991 		pfvf = rvu_get_pfvf(rvu, pcifunc);
3992 		link = hw->cgx_links + pfvf->lbkid;
3993 	}
3994 
3995 	if (link < 0)
3996 		return NIX_AF_ERR_RX_LINK_INVALID;
3997 
3998 	nix_find_link_frs(rvu, req, pcifunc);
3999 
4000 linkcfg:
4001 	cfg = rvu_read64(rvu, blkaddr, NIX_AF_RX_LINKX_CFG(link));
4002 	cfg = (cfg & ~(0xFFFFULL << 16)) | ((u64)req->maxlen << 16);
4003 	if (req->update_minlen)
4004 		cfg = (cfg & ~0xFFFFULL) | req->minlen;
4005 	rvu_write64(rvu, blkaddr, NIX_AF_RX_LINKX_CFG(link), cfg);
4006 
4007 	if (req->sdp_link || pf == 0)
4008 		return 0;
4009 
4010 	/* Update transmit credits for CGX links */
4011 	lmac_fifo_len =
4012 		rvu_cgx_get_fifolen(rvu) /
4013 		cgx_get_lmac_cnt(rvu_cgx_pdata(cgx, rvu));
4014 	return nix_config_link_credits(rvu, blkaddr, link, pcifunc,
4015 				       (lmac_fifo_len - req->maxlen) / 16);
4016 }
4017 
4018 int rvu_mbox_handler_nix_set_rx_cfg(struct rvu *rvu, struct nix_rx_cfg *req,
4019 				    struct msg_rsp *rsp)
4020 {
4021 	int nixlf, blkaddr, err;
4022 	u64 cfg;
4023 
4024 	err = nix_get_nixlf(rvu, req->hdr.pcifunc, &nixlf, &blkaddr);
4025 	if (err)
4026 		return err;
4027 
4028 	cfg = rvu_read64(rvu, blkaddr, NIX_AF_LFX_RX_CFG(nixlf));
4029 	/* Set the interface configuration */
4030 	if (req->len_verify & BIT(0))
4031 		cfg |= BIT_ULL(41);
4032 	else
4033 		cfg &= ~BIT_ULL(41);
4034 
4035 	if (req->len_verify & BIT(1))
4036 		cfg |= BIT_ULL(40);
4037 	else
4038 		cfg &= ~BIT_ULL(40);
4039 
4040 	if (req->csum_verify & BIT(0))
4041 		cfg |= BIT_ULL(37);
4042 	else
4043 		cfg &= ~BIT_ULL(37);
4044 
4045 	rvu_write64(rvu, blkaddr, NIX_AF_LFX_RX_CFG(nixlf), cfg);
4046 
4047 	return 0;
4048 }
4049 
4050 static u64 rvu_get_lbk_link_credits(struct rvu *rvu, u16 lbk_max_frs)
4051 {
4052 	/* CN10k supports 72KB FIFO size and max packet size of 64k */
4053 	if (rvu->hw->lbk_bufsize == 0x12000)
4054 		return (rvu->hw->lbk_bufsize - lbk_max_frs) / 16;
4055 
4056 	return 1600; /* 16 * max LBK datarate = 16 * 100Gbps */
4057 }
4058 
4059 static void nix_link_config(struct rvu *rvu, int blkaddr,
4060 			    struct nix_hw *nix_hw)
4061 {
4062 	struct rvu_hwinfo *hw = rvu->hw;
4063 	int cgx, lmac_cnt, slink, link;
4064 	u16 lbk_max_frs, lmac_max_frs;
4065 	u64 tx_credits, cfg;
4066 
4067 	rvu_get_lbk_link_max_frs(rvu, &lbk_max_frs);
4068 	rvu_get_lmac_link_max_frs(rvu, &lmac_max_frs);
4069 
4070 	/* Set default min/max packet lengths allowed on NIX Rx links.
4071 	 *
4072 	 * With HW reset minlen value of 60byte, HW will treat ARP pkts
4073 	 * as undersize and report them to SW as error pkts, hence
4074 	 * setting it to 40 bytes.
4075 	 */
4076 	for (link = 0; link < hw->cgx_links; link++) {
4077 		rvu_write64(rvu, blkaddr, NIX_AF_RX_LINKX_CFG(link),
4078 				((u64)lmac_max_frs << 16) | NIC_HW_MIN_FRS);
4079 	}
4080 
4081 	for (link = hw->cgx_links; link < hw->lbk_links; link++) {
4082 		rvu_write64(rvu, blkaddr, NIX_AF_RX_LINKX_CFG(link),
4083 			    ((u64)lbk_max_frs << 16) | NIC_HW_MIN_FRS);
4084 	}
4085 	if (hw->sdp_links) {
4086 		link = hw->cgx_links + hw->lbk_links;
4087 		rvu_write64(rvu, blkaddr, NIX_AF_RX_LINKX_CFG(link),
4088 			    SDP_HW_MAX_FRS << 16 | NIC_HW_MIN_FRS);
4089 	}
4090 
4091 	/* Set credits for Tx links assuming max packet length allowed.
4092 	 * This will be reconfigured based on MTU set for PF/VF.
4093 	 */
4094 	for (cgx = 0; cgx < hw->cgx; cgx++) {
4095 		lmac_cnt = cgx_get_lmac_cnt(rvu_cgx_pdata(cgx, rvu));
4096 		/* Skip when cgx is not available or lmac cnt is zero */
4097 		if (lmac_cnt <= 0)
4098 			continue;
4099 		tx_credits = ((rvu_cgx_get_fifolen(rvu) / lmac_cnt) -
4100 			       lmac_max_frs) / 16;
4101 		/* Enable credits and set credit pkt count to max allowed */
4102 		cfg =  (tx_credits << 12) | (0x1FF << 2) | BIT_ULL(1);
4103 		slink = cgx * hw->lmac_per_cgx;
4104 		for (link = slink; link < (slink + lmac_cnt); link++) {
4105 			nix_hw->tx_credits[link] = tx_credits;
4106 			rvu_write64(rvu, blkaddr,
4107 				    NIX_AF_TX_LINKX_NORM_CREDIT(link), cfg);
4108 		}
4109 	}
4110 
4111 	/* Set Tx credits for LBK link */
4112 	slink = hw->cgx_links;
4113 	for (link = slink; link < (slink + hw->lbk_links); link++) {
4114 		tx_credits = rvu_get_lbk_link_credits(rvu, lbk_max_frs);
4115 		nix_hw->tx_credits[link] = tx_credits;
4116 		/* Enable credits and set credit pkt count to max allowed */
4117 		tx_credits =  (tx_credits << 12) | (0x1FF << 2) | BIT_ULL(1);
4118 		rvu_write64(rvu, blkaddr,
4119 			    NIX_AF_TX_LINKX_NORM_CREDIT(link), tx_credits);
4120 	}
4121 }
4122 
4123 static int nix_calibrate_x2p(struct rvu *rvu, int blkaddr)
4124 {
4125 	int idx, err;
4126 	u64 status;
4127 
4128 	/* Start X2P bus calibration */
4129 	rvu_write64(rvu, blkaddr, NIX_AF_CFG,
4130 		    rvu_read64(rvu, blkaddr, NIX_AF_CFG) | BIT_ULL(9));
4131 	/* Wait for calibration to complete */
4132 	err = rvu_poll_reg(rvu, blkaddr,
4133 			   NIX_AF_STATUS, BIT_ULL(10), false);
4134 	if (err) {
4135 		dev_err(rvu->dev, "NIX X2P bus calibration failed\n");
4136 		return err;
4137 	}
4138 
4139 	status = rvu_read64(rvu, blkaddr, NIX_AF_STATUS);
4140 	/* Check if CGX devices are ready */
4141 	for (idx = 0; idx < rvu->cgx_cnt_max; idx++) {
4142 		/* Skip when cgx port is not available */
4143 		if (!rvu_cgx_pdata(idx, rvu) ||
4144 		    (status & (BIT_ULL(16 + idx))))
4145 			continue;
4146 		dev_err(rvu->dev,
4147 			"CGX%d didn't respond to NIX X2P calibration\n", idx);
4148 		err = -EBUSY;
4149 	}
4150 
4151 	/* Check if LBK is ready */
4152 	if (!(status & BIT_ULL(19))) {
4153 		dev_err(rvu->dev,
4154 			"LBK didn't respond to NIX X2P calibration\n");
4155 		err = -EBUSY;
4156 	}
4157 
4158 	/* Clear 'calibrate_x2p' bit */
4159 	rvu_write64(rvu, blkaddr, NIX_AF_CFG,
4160 		    rvu_read64(rvu, blkaddr, NIX_AF_CFG) & ~BIT_ULL(9));
4161 	if (err || (status & 0x3FFULL))
4162 		dev_err(rvu->dev,
4163 			"NIX X2P calibration failed, status 0x%llx\n", status);
4164 	if (err)
4165 		return err;
4166 	return 0;
4167 }
4168 
4169 static int nix_aq_init(struct rvu *rvu, struct rvu_block *block)
4170 {
4171 	u64 cfg;
4172 	int err;
4173 
4174 	/* Set admin queue endianness */
4175 	cfg = rvu_read64(rvu, block->addr, NIX_AF_CFG);
4176 #ifdef __BIG_ENDIAN
4177 	cfg |= BIT_ULL(8);
4178 	rvu_write64(rvu, block->addr, NIX_AF_CFG, cfg);
4179 #else
4180 	cfg &= ~BIT_ULL(8);
4181 	rvu_write64(rvu, block->addr, NIX_AF_CFG, cfg);
4182 #endif
4183 
4184 	/* Do not bypass NDC cache */
4185 	cfg = rvu_read64(rvu, block->addr, NIX_AF_NDC_CFG);
4186 	cfg &= ~0x3FFEULL;
4187 #ifdef CONFIG_NDC_DIS_DYNAMIC_CACHING
4188 	/* Disable caching of SQB aka SQEs */
4189 	cfg |= 0x04ULL;
4190 #endif
4191 	rvu_write64(rvu, block->addr, NIX_AF_NDC_CFG, cfg);
4192 
4193 	/* Result structure can be followed by RQ/SQ/CQ context at
4194 	 * RES + 128bytes and a write mask at RES + 256 bytes, depending on
4195 	 * operation type. Alloc sufficient result memory for all operations.
4196 	 */
4197 	err = rvu_aq_alloc(rvu, &block->aq,
4198 			   Q_COUNT(AQ_SIZE), sizeof(struct nix_aq_inst_s),
4199 			   ALIGN(sizeof(struct nix_aq_res_s), 128) + 256);
4200 	if (err)
4201 		return err;
4202 
4203 	rvu_write64(rvu, block->addr, NIX_AF_AQ_CFG, AQ_SIZE);
4204 	rvu_write64(rvu, block->addr,
4205 		    NIX_AF_AQ_BASE, (u64)block->aq->inst->iova);
4206 	return 0;
4207 }
4208 
4209 static void rvu_nix_setup_capabilities(struct rvu *rvu, int blkaddr)
4210 {
4211 	struct rvu_hwinfo *hw = rvu->hw;
4212 	u64 hw_const;
4213 
4214 	hw_const = rvu_read64(rvu, blkaddr, NIX_AF_CONST1);
4215 
4216 	/* On OcteonTx2 DWRR quantum is directly configured into each of
4217 	 * the transmit scheduler queues. And PF/VF drivers were free to
4218 	 * config any value upto 2^24.
4219 	 * On CN10K, HW is modified, the quantum configuration at scheduler
4220 	 * queues is in terms of weight. And SW needs to setup a base DWRR MTU
4221 	 * at NIX_AF_DWRR_RPM_MTU / NIX_AF_DWRR_SDP_MTU. HW will do
4222 	 * 'DWRR MTU * weight' to get the quantum.
4223 	 *
4224 	 * Check if HW uses a common MTU for all DWRR quantum configs.
4225 	 * On OcteonTx2 this register field is '0'.
4226 	 */
4227 	if (((hw_const >> 56) & 0x10) == 0x10)
4228 		hw->cap.nix_common_dwrr_mtu = true;
4229 }
4230 
4231 static int rvu_nix_block_init(struct rvu *rvu, struct nix_hw *nix_hw)
4232 {
4233 	const struct npc_lt_def_cfg *ltdefs;
4234 	struct rvu_hwinfo *hw = rvu->hw;
4235 	int blkaddr = nix_hw->blkaddr;
4236 	struct rvu_block *block;
4237 	int err;
4238 	u64 cfg;
4239 
4240 	block = &hw->block[blkaddr];
4241 
4242 	if (is_rvu_96xx_B0(rvu)) {
4243 		/* As per a HW errata in 96xx A0/B0 silicon, NIX may corrupt
4244 		 * internal state when conditional clocks are turned off.
4245 		 * Hence enable them.
4246 		 */
4247 		rvu_write64(rvu, blkaddr, NIX_AF_CFG,
4248 			    rvu_read64(rvu, blkaddr, NIX_AF_CFG) | 0x40ULL);
4249 
4250 		/* Set chan/link to backpressure TL3 instead of TL2 */
4251 		rvu_write64(rvu, blkaddr, NIX_AF_PSE_CHANNEL_LEVEL, 0x01);
4252 
4253 		/* Disable SQ manager's sticky mode operation (set TM6 = 0)
4254 		 * This sticky mode is known to cause SQ stalls when multiple
4255 		 * SQs are mapped to same SMQ and transmitting pkts at a time.
4256 		 */
4257 		cfg = rvu_read64(rvu, blkaddr, NIX_AF_SQM_DBG_CTL_STATUS);
4258 		cfg &= ~BIT_ULL(15);
4259 		rvu_write64(rvu, blkaddr, NIX_AF_SQM_DBG_CTL_STATUS, cfg);
4260 	}
4261 
4262 	ltdefs = rvu->kpu.lt_def;
4263 	/* Calibrate X2P bus to check if CGX/LBK links are fine */
4264 	err = nix_calibrate_x2p(rvu, blkaddr);
4265 	if (err)
4266 		return err;
4267 
4268 	/* Setup capabilities of the NIX block */
4269 	rvu_nix_setup_capabilities(rvu, blkaddr);
4270 
4271 	/* Initialize admin queue */
4272 	err = nix_aq_init(rvu, block);
4273 	if (err)
4274 		return err;
4275 
4276 	/* Restore CINT timer delay to HW reset values */
4277 	rvu_write64(rvu, blkaddr, NIX_AF_CINT_DELAY, 0x0ULL);
4278 
4279 	/* For better performance use NDC TX instead of NDC RX for SQ's SQEs" */
4280 	rvu_write64(rvu, blkaddr, NIX_AF_SEB_CFG, 0x1ULL);
4281 
4282 	if (is_block_implemented(hw, blkaddr)) {
4283 		err = nix_setup_txschq(rvu, nix_hw, blkaddr);
4284 		if (err)
4285 			return err;
4286 
4287 		err = nix_setup_ipolicers(rvu, nix_hw, blkaddr);
4288 		if (err)
4289 			return err;
4290 
4291 		err = nix_af_mark_format_setup(rvu, nix_hw, blkaddr);
4292 		if (err)
4293 			return err;
4294 
4295 		err = nix_setup_mcast(rvu, nix_hw, blkaddr);
4296 		if (err)
4297 			return err;
4298 
4299 		err = nix_setup_txvlan(rvu, nix_hw);
4300 		if (err)
4301 			return err;
4302 
4303 		/* Configure segmentation offload formats */
4304 		nix_setup_lso(rvu, nix_hw, blkaddr);
4305 
4306 		/* Config Outer/Inner L2, IP, TCP, UDP and SCTP NPC layer info.
4307 		 * This helps HW protocol checker to identify headers
4308 		 * and validate length and checksums.
4309 		 */
4310 		rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_OL2,
4311 			    (ltdefs->rx_ol2.lid << 8) | (ltdefs->rx_ol2.ltype_match << 4) |
4312 			    ltdefs->rx_ol2.ltype_mask);
4313 		rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_OIP4,
4314 			    (ltdefs->rx_oip4.lid << 8) | (ltdefs->rx_oip4.ltype_match << 4) |
4315 			    ltdefs->rx_oip4.ltype_mask);
4316 		rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_IIP4,
4317 			    (ltdefs->rx_iip4.lid << 8) | (ltdefs->rx_iip4.ltype_match << 4) |
4318 			    ltdefs->rx_iip4.ltype_mask);
4319 		rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_OIP6,
4320 			    (ltdefs->rx_oip6.lid << 8) | (ltdefs->rx_oip6.ltype_match << 4) |
4321 			    ltdefs->rx_oip6.ltype_mask);
4322 		rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_IIP6,
4323 			    (ltdefs->rx_iip6.lid << 8) | (ltdefs->rx_iip6.ltype_match << 4) |
4324 			    ltdefs->rx_iip6.ltype_mask);
4325 		rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_OTCP,
4326 			    (ltdefs->rx_otcp.lid << 8) | (ltdefs->rx_otcp.ltype_match << 4) |
4327 			    ltdefs->rx_otcp.ltype_mask);
4328 		rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_ITCP,
4329 			    (ltdefs->rx_itcp.lid << 8) | (ltdefs->rx_itcp.ltype_match << 4) |
4330 			    ltdefs->rx_itcp.ltype_mask);
4331 		rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_OUDP,
4332 			    (ltdefs->rx_oudp.lid << 8) | (ltdefs->rx_oudp.ltype_match << 4) |
4333 			    ltdefs->rx_oudp.ltype_mask);
4334 		rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_IUDP,
4335 			    (ltdefs->rx_iudp.lid << 8) | (ltdefs->rx_iudp.ltype_match << 4) |
4336 			    ltdefs->rx_iudp.ltype_mask);
4337 		rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_OSCTP,
4338 			    (ltdefs->rx_osctp.lid << 8) | (ltdefs->rx_osctp.ltype_match << 4) |
4339 			    ltdefs->rx_osctp.ltype_mask);
4340 		rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_ISCTP,
4341 			    (ltdefs->rx_isctp.lid << 8) | (ltdefs->rx_isctp.ltype_match << 4) |
4342 			    ltdefs->rx_isctp.ltype_mask);
4343 
4344 		if (!is_rvu_otx2(rvu)) {
4345 			/* Enable APAD calculation for other protocols
4346 			 * matching APAD0 and APAD1 lt def registers.
4347 			 */
4348 			rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_CST_APAD0,
4349 				    (ltdefs->rx_apad0.valid << 11) |
4350 				    (ltdefs->rx_apad0.lid << 8) |
4351 				    (ltdefs->rx_apad0.ltype_match << 4) |
4352 				    ltdefs->rx_apad0.ltype_mask);
4353 			rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_CST_APAD1,
4354 				    (ltdefs->rx_apad1.valid << 11) |
4355 				    (ltdefs->rx_apad1.lid << 8) |
4356 				    (ltdefs->rx_apad1.ltype_match << 4) |
4357 				    ltdefs->rx_apad1.ltype_mask);
4358 
4359 			/* Receive ethertype defination register defines layer
4360 			 * information in NPC_RESULT_S to identify the Ethertype
4361 			 * location in L2 header. Used for Ethertype overwriting
4362 			 * in inline IPsec flow.
4363 			 */
4364 			rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_ET(0),
4365 				    (ltdefs->rx_et[0].offset << 12) |
4366 				    (ltdefs->rx_et[0].valid << 11) |
4367 				    (ltdefs->rx_et[0].lid << 8) |
4368 				    (ltdefs->rx_et[0].ltype_match << 4) |
4369 				    ltdefs->rx_et[0].ltype_mask);
4370 			rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_ET(1),
4371 				    (ltdefs->rx_et[1].offset << 12) |
4372 				    (ltdefs->rx_et[1].valid << 11) |
4373 				    (ltdefs->rx_et[1].lid << 8) |
4374 				    (ltdefs->rx_et[1].ltype_match << 4) |
4375 				    ltdefs->rx_et[1].ltype_mask);
4376 		}
4377 
4378 		err = nix_rx_flowkey_alg_cfg(rvu, blkaddr);
4379 		if (err)
4380 			return err;
4381 
4382 		nix_hw->tx_credits = kcalloc(hw->cgx_links + hw->lbk_links,
4383 					     sizeof(u64), GFP_KERNEL);
4384 		if (!nix_hw->tx_credits)
4385 			return -ENOMEM;
4386 
4387 		/* Initialize CGX/LBK/SDP link credits, min/max pkt lengths */
4388 		nix_link_config(rvu, blkaddr, nix_hw);
4389 
4390 		/* Enable Channel backpressure */
4391 		rvu_write64(rvu, blkaddr, NIX_AF_RX_CFG, BIT_ULL(0));
4392 	}
4393 	return 0;
4394 }
4395 
4396 int rvu_nix_init(struct rvu *rvu)
4397 {
4398 	struct rvu_hwinfo *hw = rvu->hw;
4399 	struct nix_hw *nix_hw;
4400 	int blkaddr = 0, err;
4401 	int i = 0;
4402 
4403 	hw->nix = devm_kcalloc(rvu->dev, MAX_NIX_BLKS, sizeof(struct nix_hw),
4404 			       GFP_KERNEL);
4405 	if (!hw->nix)
4406 		return -ENOMEM;
4407 
4408 	blkaddr = rvu_get_next_nix_blkaddr(rvu, blkaddr);
4409 	while (blkaddr) {
4410 		nix_hw = &hw->nix[i];
4411 		nix_hw->rvu = rvu;
4412 		nix_hw->blkaddr = blkaddr;
4413 		err = rvu_nix_block_init(rvu, nix_hw);
4414 		if (err)
4415 			return err;
4416 		blkaddr = rvu_get_next_nix_blkaddr(rvu, blkaddr);
4417 		i++;
4418 	}
4419 
4420 	return 0;
4421 }
4422 
4423 static void rvu_nix_block_freemem(struct rvu *rvu, int blkaddr,
4424 				  struct rvu_block *block)
4425 {
4426 	struct nix_txsch *txsch;
4427 	struct nix_mcast *mcast;
4428 	struct nix_txvlan *vlan;
4429 	struct nix_hw *nix_hw;
4430 	int lvl;
4431 
4432 	rvu_aq_free(rvu, block->aq);
4433 
4434 	if (is_block_implemented(rvu->hw, blkaddr)) {
4435 		nix_hw = get_nix_hw(rvu->hw, blkaddr);
4436 		if (!nix_hw)
4437 			return;
4438 
4439 		for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
4440 			txsch = &nix_hw->txsch[lvl];
4441 			kfree(txsch->schq.bmap);
4442 		}
4443 
4444 		kfree(nix_hw->tx_credits);
4445 
4446 		nix_ipolicer_freemem(rvu, nix_hw);
4447 
4448 		vlan = &nix_hw->txvlan;
4449 		kfree(vlan->rsrc.bmap);
4450 		mutex_destroy(&vlan->rsrc_lock);
4451 
4452 		mcast = &nix_hw->mcast;
4453 		qmem_free(rvu->dev, mcast->mce_ctx);
4454 		qmem_free(rvu->dev, mcast->mcast_buf);
4455 		mutex_destroy(&mcast->mce_lock);
4456 	}
4457 }
4458 
4459 void rvu_nix_freemem(struct rvu *rvu)
4460 {
4461 	struct rvu_hwinfo *hw = rvu->hw;
4462 	struct rvu_block *block;
4463 	int blkaddr = 0;
4464 
4465 	blkaddr = rvu_get_next_nix_blkaddr(rvu, blkaddr);
4466 	while (blkaddr) {
4467 		block = &hw->block[blkaddr];
4468 		rvu_nix_block_freemem(rvu, blkaddr, block);
4469 		blkaddr = rvu_get_next_nix_blkaddr(rvu, blkaddr);
4470 	}
4471 }
4472 
4473 int rvu_mbox_handler_nix_lf_start_rx(struct rvu *rvu, struct msg_req *req,
4474 				     struct msg_rsp *rsp)
4475 {
4476 	u16 pcifunc = req->hdr.pcifunc;
4477 	struct rvu_pfvf *pfvf;
4478 	int nixlf, err;
4479 
4480 	err = nix_get_nixlf(rvu, pcifunc, &nixlf, NULL);
4481 	if (err)
4482 		return err;
4483 
4484 	rvu_npc_enable_default_entries(rvu, pcifunc, nixlf);
4485 
4486 	npc_mcam_enable_flows(rvu, pcifunc);
4487 
4488 	pfvf = rvu_get_pfvf(rvu, pcifunc);
4489 	set_bit(NIXLF_INITIALIZED, &pfvf->flags);
4490 
4491 	rvu_switch_update_rules(rvu, pcifunc);
4492 
4493 	return rvu_cgx_start_stop_io(rvu, pcifunc, true);
4494 }
4495 
4496 int rvu_mbox_handler_nix_lf_stop_rx(struct rvu *rvu, struct msg_req *req,
4497 				    struct msg_rsp *rsp)
4498 {
4499 	u16 pcifunc = req->hdr.pcifunc;
4500 	struct rvu_pfvf *pfvf;
4501 	int nixlf, err;
4502 
4503 	err = nix_get_nixlf(rvu, pcifunc, &nixlf, NULL);
4504 	if (err)
4505 		return err;
4506 
4507 	rvu_npc_disable_mcam_entries(rvu, pcifunc, nixlf);
4508 
4509 	pfvf = rvu_get_pfvf(rvu, pcifunc);
4510 	clear_bit(NIXLF_INITIALIZED, &pfvf->flags);
4511 
4512 	return rvu_cgx_start_stop_io(rvu, pcifunc, false);
4513 }
4514 
4515 void rvu_nix_lf_teardown(struct rvu *rvu, u16 pcifunc, int blkaddr, int nixlf)
4516 {
4517 	struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
4518 	struct hwctx_disable_req ctx_req;
4519 	int pf = rvu_get_pf(pcifunc);
4520 	struct mac_ops *mac_ops;
4521 	u8 cgx_id, lmac_id;
4522 	void *cgxd;
4523 	int err;
4524 
4525 	ctx_req.hdr.pcifunc = pcifunc;
4526 
4527 	/* Cleanup NPC MCAM entries, free Tx scheduler queues being used */
4528 	rvu_npc_disable_mcam_entries(rvu, pcifunc, nixlf);
4529 	rvu_npc_free_mcam_entries(rvu, pcifunc, nixlf);
4530 	nix_interface_deinit(rvu, pcifunc, nixlf);
4531 	nix_rx_sync(rvu, blkaddr);
4532 	nix_txschq_free(rvu, pcifunc);
4533 
4534 	clear_bit(NIXLF_INITIALIZED, &pfvf->flags);
4535 
4536 	rvu_cgx_start_stop_io(rvu, pcifunc, false);
4537 
4538 	if (pfvf->sq_ctx) {
4539 		ctx_req.ctype = NIX_AQ_CTYPE_SQ;
4540 		err = nix_lf_hwctx_disable(rvu, &ctx_req);
4541 		if (err)
4542 			dev_err(rvu->dev, "SQ ctx disable failed\n");
4543 	}
4544 
4545 	if (pfvf->rq_ctx) {
4546 		ctx_req.ctype = NIX_AQ_CTYPE_RQ;
4547 		err = nix_lf_hwctx_disable(rvu, &ctx_req);
4548 		if (err)
4549 			dev_err(rvu->dev, "RQ ctx disable failed\n");
4550 	}
4551 
4552 	if (pfvf->cq_ctx) {
4553 		ctx_req.ctype = NIX_AQ_CTYPE_CQ;
4554 		err = nix_lf_hwctx_disable(rvu, &ctx_req);
4555 		if (err)
4556 			dev_err(rvu->dev, "CQ ctx disable failed\n");
4557 	}
4558 
4559 	/* reset HW config done for Switch headers */
4560 	rvu_npc_set_parse_mode(rvu, pcifunc, OTX2_PRIV_FLAGS_DEFAULT,
4561 			       (PKIND_TX | PKIND_RX), 0, 0, 0, 0);
4562 
4563 	/* Disabling CGX and NPC config done for PTP */
4564 	if (pfvf->hw_rx_tstamp_en) {
4565 		rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
4566 		cgxd = rvu_cgx_pdata(cgx_id, rvu);
4567 		mac_ops = get_mac_ops(cgxd);
4568 		mac_ops->mac_enadis_ptp_config(cgxd, lmac_id, false);
4569 		/* Undo NPC config done for PTP */
4570 		if (npc_config_ts_kpuaction(rvu, pf, pcifunc, false))
4571 			dev_err(rvu->dev, "NPC config for PTP failed\n");
4572 		pfvf->hw_rx_tstamp_en = false;
4573 	}
4574 
4575 	nix_ctx_free(rvu, pfvf);
4576 
4577 	nix_free_all_bandprof(rvu, pcifunc);
4578 }
4579 
4580 #define NIX_AF_LFX_TX_CFG_PTP_EN	BIT_ULL(32)
4581 
4582 static int rvu_nix_lf_ptp_tx_cfg(struct rvu *rvu, u16 pcifunc, bool enable)
4583 {
4584 	struct rvu_hwinfo *hw = rvu->hw;
4585 	struct rvu_block *block;
4586 	int blkaddr, pf;
4587 	int nixlf;
4588 	u64 cfg;
4589 
4590 	pf = rvu_get_pf(pcifunc);
4591 	if (!is_mac_feature_supported(rvu, pf, RVU_LMAC_FEAT_PTP))
4592 		return 0;
4593 
4594 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
4595 	if (blkaddr < 0)
4596 		return NIX_AF_ERR_AF_LF_INVALID;
4597 
4598 	block = &hw->block[blkaddr];
4599 	nixlf = rvu_get_lf(rvu, block, pcifunc, 0);
4600 	if (nixlf < 0)
4601 		return NIX_AF_ERR_AF_LF_INVALID;
4602 
4603 	cfg = rvu_read64(rvu, blkaddr, NIX_AF_LFX_TX_CFG(nixlf));
4604 
4605 	if (enable)
4606 		cfg |= NIX_AF_LFX_TX_CFG_PTP_EN;
4607 	else
4608 		cfg &= ~NIX_AF_LFX_TX_CFG_PTP_EN;
4609 
4610 	rvu_write64(rvu, blkaddr, NIX_AF_LFX_TX_CFG(nixlf), cfg);
4611 
4612 	return 0;
4613 }
4614 
4615 int rvu_mbox_handler_nix_lf_ptp_tx_enable(struct rvu *rvu, struct msg_req *req,
4616 					  struct msg_rsp *rsp)
4617 {
4618 	return rvu_nix_lf_ptp_tx_cfg(rvu, req->hdr.pcifunc, true);
4619 }
4620 
4621 int rvu_mbox_handler_nix_lf_ptp_tx_disable(struct rvu *rvu, struct msg_req *req,
4622 					   struct msg_rsp *rsp)
4623 {
4624 	return rvu_nix_lf_ptp_tx_cfg(rvu, req->hdr.pcifunc, false);
4625 }
4626 
4627 int rvu_mbox_handler_nix_lso_format_cfg(struct rvu *rvu,
4628 					struct nix_lso_format_cfg *req,
4629 					struct nix_lso_format_cfg_rsp *rsp)
4630 {
4631 	u16 pcifunc = req->hdr.pcifunc;
4632 	struct nix_hw *nix_hw;
4633 	struct rvu_pfvf *pfvf;
4634 	int blkaddr, idx, f;
4635 	u64 reg;
4636 
4637 	pfvf = rvu_get_pfvf(rvu, pcifunc);
4638 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
4639 	if (!pfvf->nixlf || blkaddr < 0)
4640 		return NIX_AF_ERR_AF_LF_INVALID;
4641 
4642 	nix_hw = get_nix_hw(rvu->hw, blkaddr);
4643 	if (!nix_hw)
4644 		return NIX_AF_ERR_INVALID_NIXBLK;
4645 
4646 	/* Find existing matching LSO format, if any */
4647 	for (idx = 0; idx < nix_hw->lso.in_use; idx++) {
4648 		for (f = 0; f < NIX_LSO_FIELD_MAX; f++) {
4649 			reg = rvu_read64(rvu, blkaddr,
4650 					 NIX_AF_LSO_FORMATX_FIELDX(idx, f));
4651 			if (req->fields[f] != (reg & req->field_mask))
4652 				break;
4653 		}
4654 
4655 		if (f == NIX_LSO_FIELD_MAX)
4656 			break;
4657 	}
4658 
4659 	if (idx < nix_hw->lso.in_use) {
4660 		/* Match found */
4661 		rsp->lso_format_idx = idx;
4662 		return 0;
4663 	}
4664 
4665 	if (nix_hw->lso.in_use == nix_hw->lso.total)
4666 		return NIX_AF_ERR_LSO_CFG_FAIL;
4667 
4668 	rsp->lso_format_idx = nix_hw->lso.in_use++;
4669 
4670 	for (f = 0; f < NIX_LSO_FIELD_MAX; f++)
4671 		rvu_write64(rvu, blkaddr,
4672 			    NIX_AF_LSO_FORMATX_FIELDX(rsp->lso_format_idx, f),
4673 			    req->fields[f]);
4674 
4675 	return 0;
4676 }
4677 
4678 #define IPSEC_GEN_CFG_EGRP    GENMASK_ULL(50, 48)
4679 #define IPSEC_GEN_CFG_OPCODE  GENMASK_ULL(47, 32)
4680 #define IPSEC_GEN_CFG_PARAM1  GENMASK_ULL(31, 16)
4681 #define IPSEC_GEN_CFG_PARAM2  GENMASK_ULL(15, 0)
4682 
4683 #define CPT_INST_QSEL_BLOCK   GENMASK_ULL(28, 24)
4684 #define CPT_INST_QSEL_PF_FUNC GENMASK_ULL(23, 8)
4685 #define CPT_INST_QSEL_SLOT    GENMASK_ULL(7, 0)
4686 
4687 static void nix_inline_ipsec_cfg(struct rvu *rvu, struct nix_inline_ipsec_cfg *req,
4688 				 int blkaddr)
4689 {
4690 	u8 cpt_idx, cpt_blkaddr;
4691 	u64 val;
4692 
4693 	cpt_idx = (blkaddr == BLKADDR_NIX0) ? 0 : 1;
4694 	if (req->enable) {
4695 		val = 0;
4696 		/* Enable context prefetching */
4697 		if (!is_rvu_otx2(rvu))
4698 			val |= BIT_ULL(51);
4699 
4700 		/* Set OPCODE and EGRP */
4701 		val |= FIELD_PREP(IPSEC_GEN_CFG_EGRP, req->gen_cfg.egrp);
4702 		val |= FIELD_PREP(IPSEC_GEN_CFG_OPCODE, req->gen_cfg.opcode);
4703 		val |= FIELD_PREP(IPSEC_GEN_CFG_PARAM1, req->gen_cfg.param1);
4704 		val |= FIELD_PREP(IPSEC_GEN_CFG_PARAM2, req->gen_cfg.param2);
4705 
4706 		rvu_write64(rvu, blkaddr, NIX_AF_RX_IPSEC_GEN_CFG, val);
4707 
4708 		/* Set CPT queue for inline IPSec */
4709 		val = FIELD_PREP(CPT_INST_QSEL_SLOT, req->inst_qsel.cpt_slot);
4710 		val |= FIELD_PREP(CPT_INST_QSEL_PF_FUNC,
4711 				  req->inst_qsel.cpt_pf_func);
4712 
4713 		if (!is_rvu_otx2(rvu)) {
4714 			cpt_blkaddr = (cpt_idx == 0) ? BLKADDR_CPT0 :
4715 						       BLKADDR_CPT1;
4716 			val |= FIELD_PREP(CPT_INST_QSEL_BLOCK, cpt_blkaddr);
4717 		}
4718 
4719 		rvu_write64(rvu, blkaddr, NIX_AF_RX_CPTX_INST_QSEL(cpt_idx),
4720 			    val);
4721 
4722 		/* Set CPT credit */
4723 		rvu_write64(rvu, blkaddr, NIX_AF_RX_CPTX_CREDIT(cpt_idx),
4724 			    req->cpt_credit);
4725 	} else {
4726 		rvu_write64(rvu, blkaddr, NIX_AF_RX_IPSEC_GEN_CFG, 0x0);
4727 		rvu_write64(rvu, blkaddr, NIX_AF_RX_CPTX_INST_QSEL(cpt_idx),
4728 			    0x0);
4729 		rvu_write64(rvu, blkaddr, NIX_AF_RX_CPTX_CREDIT(cpt_idx),
4730 			    0x3FFFFF);
4731 	}
4732 }
4733 
4734 int rvu_mbox_handler_nix_inline_ipsec_cfg(struct rvu *rvu,
4735 					  struct nix_inline_ipsec_cfg *req,
4736 					  struct msg_rsp *rsp)
4737 {
4738 	if (!is_block_implemented(rvu->hw, BLKADDR_CPT0))
4739 		return 0;
4740 
4741 	nix_inline_ipsec_cfg(rvu, req, BLKADDR_NIX0);
4742 	if (is_block_implemented(rvu->hw, BLKADDR_CPT1))
4743 		nix_inline_ipsec_cfg(rvu, req, BLKADDR_NIX1);
4744 
4745 	return 0;
4746 }
4747 
4748 int rvu_mbox_handler_nix_inline_ipsec_lf_cfg(struct rvu *rvu,
4749 					     struct nix_inline_ipsec_lf_cfg *req,
4750 					     struct msg_rsp *rsp)
4751 {
4752 	int lf, blkaddr, err;
4753 	u64 val;
4754 
4755 	if (!is_block_implemented(rvu->hw, BLKADDR_CPT0))
4756 		return 0;
4757 
4758 	err = nix_get_nixlf(rvu, req->hdr.pcifunc, &lf, &blkaddr);
4759 	if (err)
4760 		return err;
4761 
4762 	if (req->enable) {
4763 		/* Set TT, TAG_CONST, SA_POW2_SIZE and LENM1_MAX */
4764 		val = (u64)req->ipsec_cfg0.tt << 44 |
4765 		      (u64)req->ipsec_cfg0.tag_const << 20 |
4766 		      (u64)req->ipsec_cfg0.sa_pow2_size << 16 |
4767 		      req->ipsec_cfg0.lenm1_max;
4768 
4769 		if (blkaddr == BLKADDR_NIX1)
4770 			val |= BIT_ULL(46);
4771 
4772 		rvu_write64(rvu, blkaddr, NIX_AF_LFX_RX_IPSEC_CFG0(lf), val);
4773 
4774 		/* Set SA_IDX_W and SA_IDX_MAX */
4775 		val = (u64)req->ipsec_cfg1.sa_idx_w << 32 |
4776 		      req->ipsec_cfg1.sa_idx_max;
4777 		rvu_write64(rvu, blkaddr, NIX_AF_LFX_RX_IPSEC_CFG1(lf), val);
4778 
4779 		/* Set SA base address */
4780 		rvu_write64(rvu, blkaddr, NIX_AF_LFX_RX_IPSEC_SA_BASE(lf),
4781 			    req->sa_base_addr);
4782 	} else {
4783 		rvu_write64(rvu, blkaddr, NIX_AF_LFX_RX_IPSEC_CFG0(lf), 0x0);
4784 		rvu_write64(rvu, blkaddr, NIX_AF_LFX_RX_IPSEC_CFG1(lf), 0x0);
4785 		rvu_write64(rvu, blkaddr, NIX_AF_LFX_RX_IPSEC_SA_BASE(lf),
4786 			    0x0);
4787 	}
4788 
4789 	return 0;
4790 }
4791 void rvu_nix_reset_mac(struct rvu_pfvf *pfvf, int pcifunc)
4792 {
4793 	bool from_vf = !!(pcifunc & RVU_PFVF_FUNC_MASK);
4794 
4795 	/* overwrite vf mac address with default_mac */
4796 	if (from_vf)
4797 		ether_addr_copy(pfvf->mac_addr, pfvf->default_mac);
4798 }
4799 
4800 /* NIX ingress policers or bandwidth profiles APIs */
4801 static void nix_config_rx_pkt_policer_precolor(struct rvu *rvu, int blkaddr)
4802 {
4803 	struct npc_lt_def_cfg defs, *ltdefs;
4804 
4805 	ltdefs = &defs;
4806 	memcpy(ltdefs, rvu->kpu.lt_def, sizeof(struct npc_lt_def_cfg));
4807 
4808 	/* Extract PCP and DEI fields from outer VLAN from byte offset
4809 	 * 2 from the start of LB_PTR (ie TAG).
4810 	 * VLAN0 is Outer VLAN and VLAN1 is Inner VLAN. Inner VLAN
4811 	 * fields are considered when 'Tunnel enable' is set in profile.
4812 	 */
4813 	rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_VLAN0_PCP_DEI,
4814 		    (2UL << 12) | (ltdefs->ovlan.lid << 8) |
4815 		    (ltdefs->ovlan.ltype_match << 4) |
4816 		    ltdefs->ovlan.ltype_mask);
4817 	rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_VLAN1_PCP_DEI,
4818 		    (2UL << 12) | (ltdefs->ivlan.lid << 8) |
4819 		    (ltdefs->ivlan.ltype_match << 4) |
4820 		    ltdefs->ivlan.ltype_mask);
4821 
4822 	/* DSCP field in outer and tunneled IPv4 packets */
4823 	rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_OIP4_DSCP,
4824 		    (1UL << 12) | (ltdefs->rx_oip4.lid << 8) |
4825 		    (ltdefs->rx_oip4.ltype_match << 4) |
4826 		    ltdefs->rx_oip4.ltype_mask);
4827 	rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_IIP4_DSCP,
4828 		    (1UL << 12) | (ltdefs->rx_iip4.lid << 8) |
4829 		    (ltdefs->rx_iip4.ltype_match << 4) |
4830 		    ltdefs->rx_iip4.ltype_mask);
4831 
4832 	/* DSCP field (traffic class) in outer and tunneled IPv6 packets */
4833 	rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_OIP6_DSCP,
4834 		    (1UL << 11) | (ltdefs->rx_oip6.lid << 8) |
4835 		    (ltdefs->rx_oip6.ltype_match << 4) |
4836 		    ltdefs->rx_oip6.ltype_mask);
4837 	rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_IIP6_DSCP,
4838 		    (1UL << 11) | (ltdefs->rx_iip6.lid << 8) |
4839 		    (ltdefs->rx_iip6.ltype_match << 4) |
4840 		    ltdefs->rx_iip6.ltype_mask);
4841 }
4842 
4843 static int nix_init_policer_context(struct rvu *rvu, struct nix_hw *nix_hw,
4844 				    int layer, int prof_idx)
4845 {
4846 	struct nix_cn10k_aq_enq_req aq_req;
4847 	int rc;
4848 
4849 	memset(&aq_req, 0, sizeof(struct nix_cn10k_aq_enq_req));
4850 
4851 	aq_req.qidx = (prof_idx & 0x3FFF) | (layer << 14);
4852 	aq_req.ctype = NIX_AQ_CTYPE_BANDPROF;
4853 	aq_req.op = NIX_AQ_INSTOP_INIT;
4854 
4855 	/* Context is all zeros, submit to AQ */
4856 	rc = rvu_nix_blk_aq_enq_inst(rvu, nix_hw,
4857 				     (struct nix_aq_enq_req *)&aq_req, NULL);
4858 	if (rc)
4859 		dev_err(rvu->dev, "Failed to INIT bandwidth profile layer %d profile %d\n",
4860 			layer, prof_idx);
4861 	return rc;
4862 }
4863 
4864 static int nix_setup_ipolicers(struct rvu *rvu,
4865 			       struct nix_hw *nix_hw, int blkaddr)
4866 {
4867 	struct rvu_hwinfo *hw = rvu->hw;
4868 	struct nix_ipolicer *ipolicer;
4869 	int err, layer, prof_idx;
4870 	u64 cfg;
4871 
4872 	cfg = rvu_read64(rvu, blkaddr, NIX_AF_CONST);
4873 	if (!(cfg & BIT_ULL(61))) {
4874 		hw->cap.ipolicer = false;
4875 		return 0;
4876 	}
4877 
4878 	hw->cap.ipolicer = true;
4879 	nix_hw->ipolicer = devm_kcalloc(rvu->dev, BAND_PROF_NUM_LAYERS,
4880 					sizeof(*ipolicer), GFP_KERNEL);
4881 	if (!nix_hw->ipolicer)
4882 		return -ENOMEM;
4883 
4884 	cfg = rvu_read64(rvu, blkaddr, NIX_AF_PL_CONST);
4885 
4886 	for (layer = 0; layer < BAND_PROF_NUM_LAYERS; layer++) {
4887 		ipolicer = &nix_hw->ipolicer[layer];
4888 		switch (layer) {
4889 		case BAND_PROF_LEAF_LAYER:
4890 			ipolicer->band_prof.max = cfg & 0XFFFF;
4891 			break;
4892 		case BAND_PROF_MID_LAYER:
4893 			ipolicer->band_prof.max = (cfg >> 16) & 0XFFFF;
4894 			break;
4895 		case BAND_PROF_TOP_LAYER:
4896 			ipolicer->band_prof.max = (cfg >> 32) & 0XFFFF;
4897 			break;
4898 		}
4899 
4900 		if (!ipolicer->band_prof.max)
4901 			continue;
4902 
4903 		err = rvu_alloc_bitmap(&ipolicer->band_prof);
4904 		if (err)
4905 			return err;
4906 
4907 		ipolicer->pfvf_map = devm_kcalloc(rvu->dev,
4908 						  ipolicer->band_prof.max,
4909 						  sizeof(u16), GFP_KERNEL);
4910 		if (!ipolicer->pfvf_map)
4911 			return -ENOMEM;
4912 
4913 		ipolicer->match_id = devm_kcalloc(rvu->dev,
4914 						  ipolicer->band_prof.max,
4915 						  sizeof(u16), GFP_KERNEL);
4916 		if (!ipolicer->match_id)
4917 			return -ENOMEM;
4918 
4919 		for (prof_idx = 0;
4920 		     prof_idx < ipolicer->band_prof.max; prof_idx++) {
4921 			/* Set AF as current owner for INIT ops to succeed */
4922 			ipolicer->pfvf_map[prof_idx] = 0x00;
4923 
4924 			/* There is no enable bit in the profile context,
4925 			 * so no context disable. So let's INIT them here
4926 			 * so that PF/VF later on have to just do WRITE to
4927 			 * setup policer rates and config.
4928 			 */
4929 			err = nix_init_policer_context(rvu, nix_hw,
4930 						       layer, prof_idx);
4931 			if (err)
4932 				return err;
4933 		}
4934 
4935 		/* Allocate memory for maintaining ref_counts for MID level
4936 		 * profiles, this will be needed for leaf layer profiles'
4937 		 * aggregation.
4938 		 */
4939 		if (layer != BAND_PROF_MID_LAYER)
4940 			continue;
4941 
4942 		ipolicer->ref_count = devm_kcalloc(rvu->dev,
4943 						   ipolicer->band_prof.max,
4944 						   sizeof(u16), GFP_KERNEL);
4945 	}
4946 
4947 	/* Set policer timeunit to 2us ie  (19 + 1) * 100 nsec = 2us */
4948 	rvu_write64(rvu, blkaddr, NIX_AF_PL_TS, 19);
4949 
4950 	nix_config_rx_pkt_policer_precolor(rvu, blkaddr);
4951 
4952 	return 0;
4953 }
4954 
4955 static void nix_ipolicer_freemem(struct rvu *rvu, struct nix_hw *nix_hw)
4956 {
4957 	struct nix_ipolicer *ipolicer;
4958 	int layer;
4959 
4960 	if (!rvu->hw->cap.ipolicer)
4961 		return;
4962 
4963 	for (layer = 0; layer < BAND_PROF_NUM_LAYERS; layer++) {
4964 		ipolicer = &nix_hw->ipolicer[layer];
4965 
4966 		if (!ipolicer->band_prof.max)
4967 			continue;
4968 
4969 		kfree(ipolicer->band_prof.bmap);
4970 	}
4971 }
4972 
4973 static int nix_verify_bandprof(struct nix_cn10k_aq_enq_req *req,
4974 			       struct nix_hw *nix_hw, u16 pcifunc)
4975 {
4976 	struct nix_ipolicer *ipolicer;
4977 	int layer, hi_layer, prof_idx;
4978 
4979 	/* Bits [15:14] in profile index represent layer */
4980 	layer = (req->qidx >> 14) & 0x03;
4981 	prof_idx = req->qidx & 0x3FFF;
4982 
4983 	ipolicer = &nix_hw->ipolicer[layer];
4984 	if (prof_idx >= ipolicer->band_prof.max)
4985 		return -EINVAL;
4986 
4987 	/* Check if the profile is allocated to the requesting PCIFUNC or not
4988 	 * with the exception of AF. AF is allowed to read and update contexts.
4989 	 */
4990 	if (pcifunc && ipolicer->pfvf_map[prof_idx] != pcifunc)
4991 		return -EINVAL;
4992 
4993 	/* If this profile is linked to higher layer profile then check
4994 	 * if that profile is also allocated to the requesting PCIFUNC
4995 	 * or not.
4996 	 */
4997 	if (!req->prof.hl_en)
4998 		return 0;
4999 
5000 	/* Leaf layer profile can link only to mid layer and
5001 	 * mid layer to top layer.
5002 	 */
5003 	if (layer == BAND_PROF_LEAF_LAYER)
5004 		hi_layer = BAND_PROF_MID_LAYER;
5005 	else if (layer == BAND_PROF_MID_LAYER)
5006 		hi_layer = BAND_PROF_TOP_LAYER;
5007 	else
5008 		return -EINVAL;
5009 
5010 	ipolicer = &nix_hw->ipolicer[hi_layer];
5011 	prof_idx = req->prof.band_prof_id;
5012 	if (prof_idx >= ipolicer->band_prof.max ||
5013 	    ipolicer->pfvf_map[prof_idx] != pcifunc)
5014 		return -EINVAL;
5015 
5016 	return 0;
5017 }
5018 
5019 int rvu_mbox_handler_nix_bandprof_alloc(struct rvu *rvu,
5020 					struct nix_bandprof_alloc_req *req,
5021 					struct nix_bandprof_alloc_rsp *rsp)
5022 {
5023 	int blkaddr, layer, prof, idx, err;
5024 	u16 pcifunc = req->hdr.pcifunc;
5025 	struct nix_ipolicer *ipolicer;
5026 	struct nix_hw *nix_hw;
5027 
5028 	if (!rvu->hw->cap.ipolicer)
5029 		return NIX_AF_ERR_IPOLICER_NOTSUPP;
5030 
5031 	err = nix_get_struct_ptrs(rvu, pcifunc, &nix_hw, &blkaddr);
5032 	if (err)
5033 		return err;
5034 
5035 	mutex_lock(&rvu->rsrc_lock);
5036 	for (layer = 0; layer < BAND_PROF_NUM_LAYERS; layer++) {
5037 		if (layer == BAND_PROF_INVAL_LAYER)
5038 			continue;
5039 		if (!req->prof_count[layer])
5040 			continue;
5041 
5042 		ipolicer = &nix_hw->ipolicer[layer];
5043 		for (idx = 0; idx < req->prof_count[layer]; idx++) {
5044 			/* Allocate a max of 'MAX_BANDPROF_PER_PFFUNC' profiles */
5045 			if (idx == MAX_BANDPROF_PER_PFFUNC)
5046 				break;
5047 
5048 			prof = rvu_alloc_rsrc(&ipolicer->band_prof);
5049 			if (prof < 0)
5050 				break;
5051 			rsp->prof_count[layer]++;
5052 			rsp->prof_idx[layer][idx] = prof;
5053 			ipolicer->pfvf_map[prof] = pcifunc;
5054 		}
5055 	}
5056 	mutex_unlock(&rvu->rsrc_lock);
5057 	return 0;
5058 }
5059 
5060 static int nix_free_all_bandprof(struct rvu *rvu, u16 pcifunc)
5061 {
5062 	int blkaddr, layer, prof_idx, err;
5063 	struct nix_ipolicer *ipolicer;
5064 	struct nix_hw *nix_hw;
5065 
5066 	if (!rvu->hw->cap.ipolicer)
5067 		return NIX_AF_ERR_IPOLICER_NOTSUPP;
5068 
5069 	err = nix_get_struct_ptrs(rvu, pcifunc, &nix_hw, &blkaddr);
5070 	if (err)
5071 		return err;
5072 
5073 	mutex_lock(&rvu->rsrc_lock);
5074 	/* Free all the profiles allocated to the PCIFUNC */
5075 	for (layer = 0; layer < BAND_PROF_NUM_LAYERS; layer++) {
5076 		if (layer == BAND_PROF_INVAL_LAYER)
5077 			continue;
5078 		ipolicer = &nix_hw->ipolicer[layer];
5079 
5080 		for (prof_idx = 0; prof_idx < ipolicer->band_prof.max; prof_idx++) {
5081 			if (ipolicer->pfvf_map[prof_idx] != pcifunc)
5082 				continue;
5083 
5084 			/* Clear ratelimit aggregation, if any */
5085 			if (layer == BAND_PROF_LEAF_LAYER &&
5086 			    ipolicer->match_id[prof_idx])
5087 				nix_clear_ratelimit_aggr(rvu, nix_hw, prof_idx);
5088 
5089 			ipolicer->pfvf_map[prof_idx] = 0x00;
5090 			ipolicer->match_id[prof_idx] = 0;
5091 			rvu_free_rsrc(&ipolicer->band_prof, prof_idx);
5092 		}
5093 	}
5094 	mutex_unlock(&rvu->rsrc_lock);
5095 	return 0;
5096 }
5097 
5098 int rvu_mbox_handler_nix_bandprof_free(struct rvu *rvu,
5099 				       struct nix_bandprof_free_req *req,
5100 				       struct msg_rsp *rsp)
5101 {
5102 	int blkaddr, layer, prof_idx, idx, err;
5103 	u16 pcifunc = req->hdr.pcifunc;
5104 	struct nix_ipolicer *ipolicer;
5105 	struct nix_hw *nix_hw;
5106 
5107 	if (req->free_all)
5108 		return nix_free_all_bandprof(rvu, pcifunc);
5109 
5110 	if (!rvu->hw->cap.ipolicer)
5111 		return NIX_AF_ERR_IPOLICER_NOTSUPP;
5112 
5113 	err = nix_get_struct_ptrs(rvu, pcifunc, &nix_hw, &blkaddr);
5114 	if (err)
5115 		return err;
5116 
5117 	mutex_lock(&rvu->rsrc_lock);
5118 	/* Free the requested profile indices */
5119 	for (layer = 0; layer < BAND_PROF_NUM_LAYERS; layer++) {
5120 		if (layer == BAND_PROF_INVAL_LAYER)
5121 			continue;
5122 		if (!req->prof_count[layer])
5123 			continue;
5124 
5125 		ipolicer = &nix_hw->ipolicer[layer];
5126 		for (idx = 0; idx < req->prof_count[layer]; idx++) {
5127 			prof_idx = req->prof_idx[layer][idx];
5128 			if (prof_idx >= ipolicer->band_prof.max ||
5129 			    ipolicer->pfvf_map[prof_idx] != pcifunc)
5130 				continue;
5131 
5132 			/* Clear ratelimit aggregation, if any */
5133 			if (layer == BAND_PROF_LEAF_LAYER &&
5134 			    ipolicer->match_id[prof_idx])
5135 				nix_clear_ratelimit_aggr(rvu, nix_hw, prof_idx);
5136 
5137 			ipolicer->pfvf_map[prof_idx] = 0x00;
5138 			ipolicer->match_id[prof_idx] = 0;
5139 			rvu_free_rsrc(&ipolicer->band_prof, prof_idx);
5140 			if (idx == MAX_BANDPROF_PER_PFFUNC)
5141 				break;
5142 		}
5143 	}
5144 	mutex_unlock(&rvu->rsrc_lock);
5145 	return 0;
5146 }
5147 
5148 int nix_aq_context_read(struct rvu *rvu, struct nix_hw *nix_hw,
5149 			struct nix_cn10k_aq_enq_req *aq_req,
5150 			struct nix_cn10k_aq_enq_rsp *aq_rsp,
5151 			u16 pcifunc, u8 ctype, u32 qidx)
5152 {
5153 	memset(aq_req, 0, sizeof(struct nix_cn10k_aq_enq_req));
5154 	aq_req->hdr.pcifunc = pcifunc;
5155 	aq_req->ctype = ctype;
5156 	aq_req->op = NIX_AQ_INSTOP_READ;
5157 	aq_req->qidx = qidx;
5158 
5159 	return rvu_nix_blk_aq_enq_inst(rvu, nix_hw,
5160 				       (struct nix_aq_enq_req *)aq_req,
5161 				       (struct nix_aq_enq_rsp *)aq_rsp);
5162 }
5163 
5164 static int nix_ipolicer_map_leaf_midprofs(struct rvu *rvu,
5165 					  struct nix_hw *nix_hw,
5166 					  struct nix_cn10k_aq_enq_req *aq_req,
5167 					  struct nix_cn10k_aq_enq_rsp *aq_rsp,
5168 					  u32 leaf_prof, u16 mid_prof)
5169 {
5170 	memset(aq_req, 0, sizeof(struct nix_cn10k_aq_enq_req));
5171 	aq_req->hdr.pcifunc = 0x00;
5172 	aq_req->ctype = NIX_AQ_CTYPE_BANDPROF;
5173 	aq_req->op = NIX_AQ_INSTOP_WRITE;
5174 	aq_req->qidx = leaf_prof;
5175 
5176 	aq_req->prof.band_prof_id = mid_prof;
5177 	aq_req->prof_mask.band_prof_id = GENMASK(6, 0);
5178 	aq_req->prof.hl_en = 1;
5179 	aq_req->prof_mask.hl_en = 1;
5180 
5181 	return rvu_nix_blk_aq_enq_inst(rvu, nix_hw,
5182 				       (struct nix_aq_enq_req *)aq_req,
5183 				       (struct nix_aq_enq_rsp *)aq_rsp);
5184 }
5185 
5186 int rvu_nix_setup_ratelimit_aggr(struct rvu *rvu, u16 pcifunc,
5187 				 u16 rq_idx, u16 match_id)
5188 {
5189 	int leaf_prof, mid_prof, leaf_match;
5190 	struct nix_cn10k_aq_enq_req aq_req;
5191 	struct nix_cn10k_aq_enq_rsp aq_rsp;
5192 	struct nix_ipolicer *ipolicer;
5193 	struct nix_hw *nix_hw;
5194 	int blkaddr, idx, rc;
5195 
5196 	if (!rvu->hw->cap.ipolicer)
5197 		return 0;
5198 
5199 	rc = nix_get_struct_ptrs(rvu, pcifunc, &nix_hw, &blkaddr);
5200 	if (rc)
5201 		return rc;
5202 
5203 	/* Fetch the RQ's context to see if policing is enabled */
5204 	rc = nix_aq_context_read(rvu, nix_hw, &aq_req, &aq_rsp, pcifunc,
5205 				 NIX_AQ_CTYPE_RQ, rq_idx);
5206 	if (rc) {
5207 		dev_err(rvu->dev,
5208 			"%s: Failed to fetch RQ%d context of PFFUNC 0x%x\n",
5209 			__func__, rq_idx, pcifunc);
5210 		return rc;
5211 	}
5212 
5213 	if (!aq_rsp.rq.policer_ena)
5214 		return 0;
5215 
5216 	/* Get the bandwidth profile ID mapped to this RQ */
5217 	leaf_prof = aq_rsp.rq.band_prof_id;
5218 
5219 	ipolicer = &nix_hw->ipolicer[BAND_PROF_LEAF_LAYER];
5220 	ipolicer->match_id[leaf_prof] = match_id;
5221 
5222 	/* Check if any other leaf profile is marked with same match_id */
5223 	for (idx = 0; idx < ipolicer->band_prof.max; idx++) {
5224 		if (idx == leaf_prof)
5225 			continue;
5226 		if (ipolicer->match_id[idx] != match_id)
5227 			continue;
5228 
5229 		leaf_match = idx;
5230 		break;
5231 	}
5232 
5233 	if (idx == ipolicer->band_prof.max)
5234 		return 0;
5235 
5236 	/* Fetch the matching profile's context to check if it's already
5237 	 * mapped to a mid level profile.
5238 	 */
5239 	rc = nix_aq_context_read(rvu, nix_hw, &aq_req, &aq_rsp, 0x00,
5240 				 NIX_AQ_CTYPE_BANDPROF, leaf_match);
5241 	if (rc) {
5242 		dev_err(rvu->dev,
5243 			"%s: Failed to fetch context of leaf profile %d\n",
5244 			__func__, leaf_match);
5245 		return rc;
5246 	}
5247 
5248 	ipolicer = &nix_hw->ipolicer[BAND_PROF_MID_LAYER];
5249 	if (aq_rsp.prof.hl_en) {
5250 		/* Get Mid layer prof index and map leaf_prof index
5251 		 * also such that flows that are being steered
5252 		 * to different RQs and marked with same match_id
5253 		 * are rate limited in a aggregate fashion
5254 		 */
5255 		mid_prof = aq_rsp.prof.band_prof_id;
5256 		rc = nix_ipolicer_map_leaf_midprofs(rvu, nix_hw,
5257 						    &aq_req, &aq_rsp,
5258 						    leaf_prof, mid_prof);
5259 		if (rc) {
5260 			dev_err(rvu->dev,
5261 				"%s: Failed to map leaf(%d) and mid(%d) profiles\n",
5262 				__func__, leaf_prof, mid_prof);
5263 			goto exit;
5264 		}
5265 
5266 		mutex_lock(&rvu->rsrc_lock);
5267 		ipolicer->ref_count[mid_prof]++;
5268 		mutex_unlock(&rvu->rsrc_lock);
5269 		goto exit;
5270 	}
5271 
5272 	/* Allocate a mid layer profile and
5273 	 * map both 'leaf_prof' and 'leaf_match' profiles to it.
5274 	 */
5275 	mutex_lock(&rvu->rsrc_lock);
5276 	mid_prof = rvu_alloc_rsrc(&ipolicer->band_prof);
5277 	if (mid_prof < 0) {
5278 		dev_err(rvu->dev,
5279 			"%s: Unable to allocate mid layer profile\n", __func__);
5280 		mutex_unlock(&rvu->rsrc_lock);
5281 		goto exit;
5282 	}
5283 	mutex_unlock(&rvu->rsrc_lock);
5284 	ipolicer->pfvf_map[mid_prof] = 0x00;
5285 	ipolicer->ref_count[mid_prof] = 0;
5286 
5287 	/* Initialize mid layer profile same as 'leaf_prof' */
5288 	rc = nix_aq_context_read(rvu, nix_hw, &aq_req, &aq_rsp, 0x00,
5289 				 NIX_AQ_CTYPE_BANDPROF, leaf_prof);
5290 	if (rc) {
5291 		dev_err(rvu->dev,
5292 			"%s: Failed to fetch context of leaf profile %d\n",
5293 			__func__, leaf_prof);
5294 		goto exit;
5295 	}
5296 
5297 	memset(&aq_req, 0, sizeof(struct nix_cn10k_aq_enq_req));
5298 	aq_req.hdr.pcifunc = 0x00;
5299 	aq_req.qidx = (mid_prof & 0x3FFF) | (BAND_PROF_MID_LAYER << 14);
5300 	aq_req.ctype = NIX_AQ_CTYPE_BANDPROF;
5301 	aq_req.op = NIX_AQ_INSTOP_WRITE;
5302 	memcpy(&aq_req.prof, &aq_rsp.prof, sizeof(struct nix_bandprof_s));
5303 	/* Clear higher layer enable bit in the mid profile, just in case */
5304 	aq_req.prof.hl_en = 0;
5305 	aq_req.prof_mask.hl_en = 1;
5306 
5307 	rc = rvu_nix_blk_aq_enq_inst(rvu, nix_hw,
5308 				     (struct nix_aq_enq_req *)&aq_req, NULL);
5309 	if (rc) {
5310 		dev_err(rvu->dev,
5311 			"%s: Failed to INIT context of mid layer profile %d\n",
5312 			__func__, mid_prof);
5313 		goto exit;
5314 	}
5315 
5316 	/* Map both leaf profiles to this mid layer profile */
5317 	rc = nix_ipolicer_map_leaf_midprofs(rvu, nix_hw,
5318 					    &aq_req, &aq_rsp,
5319 					    leaf_prof, mid_prof);
5320 	if (rc) {
5321 		dev_err(rvu->dev,
5322 			"%s: Failed to map leaf(%d) and mid(%d) profiles\n",
5323 			__func__, leaf_prof, mid_prof);
5324 		goto exit;
5325 	}
5326 
5327 	mutex_lock(&rvu->rsrc_lock);
5328 	ipolicer->ref_count[mid_prof]++;
5329 	mutex_unlock(&rvu->rsrc_lock);
5330 
5331 	rc = nix_ipolicer_map_leaf_midprofs(rvu, nix_hw,
5332 					    &aq_req, &aq_rsp,
5333 					    leaf_match, mid_prof);
5334 	if (rc) {
5335 		dev_err(rvu->dev,
5336 			"%s: Failed to map leaf(%d) and mid(%d) profiles\n",
5337 			__func__, leaf_match, mid_prof);
5338 		ipolicer->ref_count[mid_prof]--;
5339 		goto exit;
5340 	}
5341 
5342 	mutex_lock(&rvu->rsrc_lock);
5343 	ipolicer->ref_count[mid_prof]++;
5344 	mutex_unlock(&rvu->rsrc_lock);
5345 
5346 exit:
5347 	return rc;
5348 }
5349 
5350 /* Called with mutex rsrc_lock */
5351 static void nix_clear_ratelimit_aggr(struct rvu *rvu, struct nix_hw *nix_hw,
5352 				     u32 leaf_prof)
5353 {
5354 	struct nix_cn10k_aq_enq_req aq_req;
5355 	struct nix_cn10k_aq_enq_rsp aq_rsp;
5356 	struct nix_ipolicer *ipolicer;
5357 	u16 mid_prof;
5358 	int rc;
5359 
5360 	mutex_unlock(&rvu->rsrc_lock);
5361 
5362 	rc = nix_aq_context_read(rvu, nix_hw, &aq_req, &aq_rsp, 0x00,
5363 				 NIX_AQ_CTYPE_BANDPROF, leaf_prof);
5364 
5365 	mutex_lock(&rvu->rsrc_lock);
5366 	if (rc) {
5367 		dev_err(rvu->dev,
5368 			"%s: Failed to fetch context of leaf profile %d\n",
5369 			__func__, leaf_prof);
5370 		return;
5371 	}
5372 
5373 	if (!aq_rsp.prof.hl_en)
5374 		return;
5375 
5376 	mid_prof = aq_rsp.prof.band_prof_id;
5377 	ipolicer = &nix_hw->ipolicer[BAND_PROF_MID_LAYER];
5378 	ipolicer->ref_count[mid_prof]--;
5379 	/* If ref_count is zero, free mid layer profile */
5380 	if (!ipolicer->ref_count[mid_prof]) {
5381 		ipolicer->pfvf_map[mid_prof] = 0x00;
5382 		rvu_free_rsrc(&ipolicer->band_prof, mid_prof);
5383 	}
5384 }
5385 
5386 int rvu_mbox_handler_nix_bandprof_get_hwinfo(struct rvu *rvu, struct msg_req *req,
5387 					     struct nix_bandprof_get_hwinfo_rsp *rsp)
5388 {
5389 	struct nix_ipolicer *ipolicer;
5390 	int blkaddr, layer, err;
5391 	struct nix_hw *nix_hw;
5392 	u64 tu;
5393 
5394 	if (!rvu->hw->cap.ipolicer)
5395 		return NIX_AF_ERR_IPOLICER_NOTSUPP;
5396 
5397 	err = nix_get_struct_ptrs(rvu, req->hdr.pcifunc, &nix_hw, &blkaddr);
5398 	if (err)
5399 		return err;
5400 
5401 	/* Return number of bandwidth profiles free at each layer */
5402 	mutex_lock(&rvu->rsrc_lock);
5403 	for (layer = 0; layer < BAND_PROF_NUM_LAYERS; layer++) {
5404 		if (layer == BAND_PROF_INVAL_LAYER)
5405 			continue;
5406 
5407 		ipolicer = &nix_hw->ipolicer[layer];
5408 		rsp->prof_count[layer] = rvu_rsrc_free_count(&ipolicer->band_prof);
5409 	}
5410 	mutex_unlock(&rvu->rsrc_lock);
5411 
5412 	/* Set the policer timeunit in nanosec */
5413 	tu = rvu_read64(rvu, blkaddr, NIX_AF_PL_TS) & GENMASK_ULL(9, 0);
5414 	rsp->policer_timeunit = (tu + 1) * 100;
5415 
5416 	return 0;
5417 }
5418