1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell RVU Admin Function driver
4 * Copyright (C) 2018 Marvell.
8 #include <linux/module.h>
11 #include "rvu_struct.h"
17 #include "lmac_common.h"
18 #include "rvu_npc_hash.h"
20 static void nix_free_tx_vtag_entries(struct rvu *rvu, u16 pcifunc);
21 static int rvu_nix_get_bpid(struct rvu *rvu, struct nix_bp_cfg_req *req,
22 int type, int chan_id);
23 static int nix_update_mce_rule(struct rvu *rvu, u16 pcifunc,
25 static int nix_setup_ipolicers(struct rvu *rvu,
26 struct nix_hw *nix_hw, int blkaddr);
27 static void nix_ipolicer_freemem(struct rvu *rvu, struct nix_hw *nix_hw);
28 static int nix_verify_bandprof(struct nix_cn10k_aq_enq_req *req,
29 struct nix_hw *nix_hw, u16 pcifunc);
30 static int nix_free_all_bandprof(struct rvu *rvu, u16 pcifunc);
31 static void nix_clear_ratelimit_aggr(struct rvu *rvu, struct nix_hw *nix_hw,
33 static const char *nix_get_ctx_name(int ctype);
59 enum nix_makr_fmt_indexes {
60 NIX_MARK_CFG_IP_DSCP_RED,
61 NIX_MARK_CFG_IP_DSCP_YELLOW,
62 NIX_MARK_CFG_IP_DSCP_YELLOW_RED,
63 NIX_MARK_CFG_IP_ECN_RED,
64 NIX_MARK_CFG_IP_ECN_YELLOW,
65 NIX_MARK_CFG_IP_ECN_YELLOW_RED,
66 NIX_MARK_CFG_VLAN_DEI_RED,
67 NIX_MARK_CFG_VLAN_DEI_YELLOW,
68 NIX_MARK_CFG_VLAN_DEI_YELLOW_RED,
72 /* For now considering MC resources needed for broadcast
73 * pkt replication only. i.e 256 HWVFs + 12 PFs.
75 #define MC_TBL_SIZE MC_TBL_SZ_2K
76 #define MC_BUF_CNT MC_BUF_CNT_1024
78 #define MC_TX_MAX 2048
81 struct hlist_node node;
90 int rvu_get_next_nix_blkaddr(struct rvu *rvu, int blkaddr)
94 /*If blkaddr is 0, return the first nix block address*/
96 return rvu->nix_blkaddr[blkaddr];
98 while (i + 1 < MAX_NIX_BLKS) {
99 if (rvu->nix_blkaddr[i] == blkaddr)
100 return rvu->nix_blkaddr[i + 1];
107 bool is_nixlf_attached(struct rvu *rvu, u16 pcifunc)
109 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
112 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
113 if (!pfvf->nixlf || blkaddr < 0)
118 int rvu_get_nixlf_count(struct rvu *rvu)
120 int blkaddr = 0, max = 0;
121 struct rvu_block *block;
123 blkaddr = rvu_get_next_nix_blkaddr(rvu, blkaddr);
125 block = &rvu->hw->block[blkaddr];
126 max += block->lf.max;
127 blkaddr = rvu_get_next_nix_blkaddr(rvu, blkaddr);
132 int nix_get_nixlf(struct rvu *rvu, u16 pcifunc, int *nixlf, int *nix_blkaddr)
134 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
135 struct rvu_hwinfo *hw = rvu->hw;
138 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
139 if (!pfvf->nixlf || blkaddr < 0)
140 return NIX_AF_ERR_AF_LF_INVALID;
142 *nixlf = rvu_get_lf(rvu, &hw->block[blkaddr], pcifunc, 0);
144 return NIX_AF_ERR_AF_LF_INVALID;
147 *nix_blkaddr = blkaddr;
152 int nix_get_struct_ptrs(struct rvu *rvu, u16 pcifunc,
153 struct nix_hw **nix_hw, int *blkaddr)
155 struct rvu_pfvf *pfvf;
157 pfvf = rvu_get_pfvf(rvu, pcifunc);
158 *blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
159 if (!pfvf->nixlf || *blkaddr < 0)
160 return NIX_AF_ERR_AF_LF_INVALID;
162 *nix_hw = get_nix_hw(rvu->hw, *blkaddr);
164 return NIX_AF_ERR_INVALID_NIXBLK;
168 static void nix_mce_list_init(struct nix_mce_list *list, int max)
170 INIT_HLIST_HEAD(&list->head);
175 static int nix_alloc_mce_list(struct nix_mcast *mcast, int count, u8 dir)
177 struct rsrc_bmap *mce_counter;
183 mce_counter = &mcast->mce_counter[dir];
184 if (!rvu_rsrc_check_contig(mce_counter, count))
187 idx = rvu_alloc_rsrc_contig(mce_counter, count);
191 static void nix_free_mce_list(struct nix_mcast *mcast, int count, int start, u8 dir)
193 struct rsrc_bmap *mce_counter;
198 mce_counter = &mcast->mce_counter[dir];
199 rvu_free_rsrc_contig(mce_counter, count, start);
202 struct nix_hw *get_nix_hw(struct rvu_hwinfo *hw, int blkaddr)
204 int nix_blkaddr = 0, i = 0;
205 struct rvu *rvu = hw->rvu;
207 nix_blkaddr = rvu_get_next_nix_blkaddr(rvu, nix_blkaddr);
208 while (nix_blkaddr) {
209 if (blkaddr == nix_blkaddr && hw->nix)
211 nix_blkaddr = rvu_get_next_nix_blkaddr(rvu, nix_blkaddr);
217 int nix_get_dwrr_mtu_reg(struct rvu_hwinfo *hw, int smq_link_type)
219 if (hw->cap.nix_multiple_dwrr_mtu)
220 return NIX_AF_DWRR_MTUX(smq_link_type);
222 if (smq_link_type == SMQ_LINK_TYPE_SDP)
223 return NIX_AF_DWRR_SDP_MTU;
225 /* Here it's same reg for RPM and LBK */
226 return NIX_AF_DWRR_RPM_MTU;
229 u32 convert_dwrr_mtu_to_bytes(u8 dwrr_mtu)
233 /* MTU used for DWRR calculation is in power of 2 up until 64K bytes.
234 * Value of 4 is reserved for MTU value of 9728 bytes.
235 * Value of 5 is reserved for MTU value of 10240 bytes.
243 return BIT_ULL(dwrr_mtu);
249 u32 convert_bytes_to_dwrr_mtu(u32 bytes)
251 /* MTU used for DWRR calculation is in power of 2 up until 64K bytes.
252 * Value of 4 is reserved for MTU value of 9728 bytes.
253 * Value of 5 is reserved for MTU value of 10240 bytes.
255 if (bytes > BIT_ULL(16))
270 static void nix_rx_sync(struct rvu *rvu, int blkaddr)
274 /* Sync all in flight RX packets to LLC/DRAM */
275 rvu_write64(rvu, blkaddr, NIX_AF_RX_SW_SYNC, BIT_ULL(0));
276 err = rvu_poll_reg(rvu, blkaddr, NIX_AF_RX_SW_SYNC, BIT_ULL(0), true);
278 dev_err(rvu->dev, "SYNC1: NIX RX software sync failed\n");
280 /* SW_SYNC ensures all existing transactions are finished and pkts
281 * are written to LLC/DRAM, queues should be teared down after
282 * successful SW_SYNC. Due to a HW errata, in some rare scenarios
283 * an existing transaction might end after SW_SYNC operation. To
284 * ensure operation is fully done, do the SW_SYNC twice.
286 rvu_write64(rvu, blkaddr, NIX_AF_RX_SW_SYNC, BIT_ULL(0));
287 err = rvu_poll_reg(rvu, blkaddr, NIX_AF_RX_SW_SYNC, BIT_ULL(0), true);
289 dev_err(rvu->dev, "SYNC2: NIX RX software sync failed\n");
292 static bool is_valid_txschq(struct rvu *rvu, int blkaddr,
293 int lvl, u16 pcifunc, u16 schq)
295 struct rvu_hwinfo *hw = rvu->hw;
296 struct nix_txsch *txsch;
297 struct nix_hw *nix_hw;
300 nix_hw = get_nix_hw(rvu->hw, blkaddr);
304 txsch = &nix_hw->txsch[lvl];
305 /* Check out of bounds */
306 if (schq >= txsch->schq.max)
309 mutex_lock(&rvu->rsrc_lock);
310 map_func = TXSCH_MAP_FUNC(txsch->pfvf_map[schq]);
311 mutex_unlock(&rvu->rsrc_lock);
313 /* TLs aggegating traffic are shared across PF and VFs */
314 if (lvl >= hw->cap.nix_tx_aggr_lvl) {
315 if (rvu_get_pf(map_func) != rvu_get_pf(pcifunc))
321 if (map_func != pcifunc)
327 static int nix_interface_init(struct rvu *rvu, u16 pcifunc, int type, int nixlf,
328 struct nix_lf_alloc_rsp *rsp, bool loop)
330 struct rvu_pfvf *parent_pf, *pfvf = rvu_get_pfvf(rvu, pcifunc);
331 u16 req_chan_base, req_chan_end, req_chan_cnt;
332 struct rvu_hwinfo *hw = rvu->hw;
333 struct sdp_node_info *sdp_info;
334 int pkind, pf, vf, lbkid, vfid;
339 pf = rvu_get_pf(pcifunc);
340 if (!is_pf_cgxmapped(rvu, pf) && type != NIX_INTF_TYPE_LBK &&
341 type != NIX_INTF_TYPE_SDP)
345 case NIX_INTF_TYPE_CGX:
346 pfvf->cgx_lmac = rvu->pf2cgxlmac_map[pf];
347 rvu_get_cgx_lmac_id(pfvf->cgx_lmac, &cgx_id, &lmac_id);
349 pkind = rvu_npc_get_pkind(rvu, pf);
352 "PF_Func 0x%x: Invalid pkind\n", pcifunc);
355 pfvf->rx_chan_base = rvu_nix_chan_cgx(rvu, cgx_id, lmac_id, 0);
356 pfvf->tx_chan_base = pfvf->rx_chan_base;
357 pfvf->rx_chan_cnt = 1;
358 pfvf->tx_chan_cnt = 1;
359 rsp->tx_link = cgx_id * hw->lmac_per_cgx + lmac_id;
361 cgx_set_pkind(rvu_cgx_pdata(cgx_id, rvu), lmac_id, pkind);
362 rvu_npc_set_pkind(rvu, pkind, pfvf);
365 case NIX_INTF_TYPE_LBK:
366 vf = (pcifunc & RVU_PFVF_FUNC_MASK) - 1;
368 /* If NIX1 block is present on the silicon then NIXes are
369 * assigned alternatively for lbk interfaces. NIX0 should
370 * send packets on lbk link 1 channels and NIX1 should send
371 * on lbk link 0 channels for the communication between
375 if (rvu->hw->lbk_links > 1)
376 lbkid = vf & 0x1 ? 0 : 1;
378 /* By default NIX0 is configured to send packet on lbk link 1
379 * (which corresponds to LBK1), same packet will receive on
380 * NIX1 over lbk link 0. If NIX1 sends packet on lbk link 0
381 * (which corresponds to LBK2) packet will receive on NIX0 lbk
383 * But if lbk links for NIX0 and NIX1 are negated, i.e NIX0
384 * transmits and receives on lbk link 0, whick corresponds
385 * to LBK1 block, back to back connectivity between NIX and
386 * LBK can be achieved (which is similar to 96xx)
389 * NIX0 lbk link 1 (LBK2) 1 (LBK1)
390 * NIX0 lbk link 0 (LBK0) 0 (LBK0)
391 * NIX1 lbk link 0 (LBK1) 0 (LBK2)
392 * NIX1 lbk link 1 (LBK3) 1 (LBK3)
397 /* Note that AF's VFs work in pairs and talk over consecutive
398 * loopback channels.Therefore if odd number of AF VFs are
399 * enabled then the last VF remains with no pair.
401 pfvf->rx_chan_base = rvu_nix_chan_lbk(rvu, lbkid, vf);
402 pfvf->tx_chan_base = vf & 0x1 ?
403 rvu_nix_chan_lbk(rvu, lbkid, vf - 1) :
404 rvu_nix_chan_lbk(rvu, lbkid, vf + 1);
405 pfvf->rx_chan_cnt = 1;
406 pfvf->tx_chan_cnt = 1;
407 rsp->tx_link = hw->cgx_links + lbkid;
409 rvu_npc_set_pkind(rvu, NPC_RX_LBK_PKIND, pfvf);
410 rvu_npc_install_promisc_entry(rvu, pcifunc, nixlf,
415 case NIX_INTF_TYPE_SDP:
416 from_vf = !!(pcifunc & RVU_PFVF_FUNC_MASK);
417 parent_pf = &rvu->pf[rvu_get_pf(pcifunc)];
418 sdp_info = parent_pf->sdp_info;
420 dev_err(rvu->dev, "Invalid sdp_info pointer\n");
424 req_chan_base = rvu_nix_chan_sdp(rvu, 0) + sdp_info->pf_srn +
425 sdp_info->num_pf_rings;
426 vf = (pcifunc & RVU_PFVF_FUNC_MASK) - 1;
427 for (vfid = 0; vfid < vf; vfid++)
428 req_chan_base += sdp_info->vf_rings[vfid];
429 req_chan_cnt = sdp_info->vf_rings[vf];
430 req_chan_end = req_chan_base + req_chan_cnt - 1;
431 if (req_chan_base < rvu_nix_chan_sdp(rvu, 0) ||
432 req_chan_end > rvu_nix_chan_sdp(rvu, 255)) {
434 "PF_Func 0x%x: Invalid channel base and count\n",
439 req_chan_base = rvu_nix_chan_sdp(rvu, 0) + sdp_info->pf_srn;
440 req_chan_cnt = sdp_info->num_pf_rings;
443 pfvf->rx_chan_base = req_chan_base;
444 pfvf->rx_chan_cnt = req_chan_cnt;
445 pfvf->tx_chan_base = pfvf->rx_chan_base;
446 pfvf->tx_chan_cnt = pfvf->rx_chan_cnt;
448 rsp->tx_link = hw->cgx_links + hw->lbk_links;
449 rvu_npc_install_promisc_entry(rvu, pcifunc, nixlf,
455 /* Add a UCAST forwarding rule in MCAM with this NIXLF attached
456 * RVU PF/VF's MAC address.
458 rvu_npc_install_ucast_entry(rvu, pcifunc, nixlf,
459 pfvf->rx_chan_base, pfvf->mac_addr);
461 /* Add this PF_FUNC to bcast pkt replication list */
462 err = nix_update_mce_rule(rvu, pcifunc, NIXLF_BCAST_ENTRY, true);
465 "Bcast list, failed to enable PF_FUNC 0x%x\n",
469 /* Install MCAM rule matching Ethernet broadcast mac address */
470 rvu_npc_install_bcast_match_entry(rvu, pcifunc,
471 nixlf, pfvf->rx_chan_base);
473 pfvf->maxlen = NIC_HW_MIN_FRS;
474 pfvf->minlen = NIC_HW_MIN_FRS;
479 static void nix_interface_deinit(struct rvu *rvu, u16 pcifunc, u8 nixlf)
481 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
487 /* Remove this PF_FUNC from bcast pkt replication list */
488 err = nix_update_mce_rule(rvu, pcifunc, NIXLF_BCAST_ENTRY, false);
491 "Bcast list, failed to disable PF_FUNC 0x%x\n",
495 /* Free and disable any MCAM entries used by this NIX LF */
496 rvu_npc_disable_mcam_entries(rvu, pcifunc, nixlf);
498 /* Disable DMAC filters used */
499 rvu_cgx_disable_dmac_entries(rvu, pcifunc);
502 #define NIX_BPIDS_PER_LMAC 8
503 #define NIX_BPIDS_PER_CPT 1
504 static int nix_setup_bpids(struct rvu *rvu, struct nix_hw *hw, int blkaddr)
506 struct nix_bp *bp = &hw->bp;
510 cfg = rvu_read64(rvu, blkaddr, NIX_AF_CONST1);
511 max_bpids = FIELD_GET(NIX_CONST_MAX_BPIDS, cfg);
513 /* Reserve the BPIds for CGX and SDP */
514 bp->cgx_bpid_cnt = rvu->hw->cgx_links * NIX_BPIDS_PER_LMAC;
515 bp->sdp_bpid_cnt = rvu->hw->sdp_links * FIELD_GET(NIX_CONST_SDP_CHANS, cfg);
516 bp->free_pool_base = bp->cgx_bpid_cnt + bp->sdp_bpid_cnt +
518 bp->bpids.max = max_bpids - bp->free_pool_base;
520 err = rvu_alloc_bitmap(&bp->bpids);
524 bp->fn_map = devm_kcalloc(rvu->dev, bp->bpids.max,
525 sizeof(u16), GFP_KERNEL);
529 bp->intf_map = devm_kcalloc(rvu->dev, bp->bpids.max,
530 sizeof(u8), GFP_KERNEL);
534 bp->ref_cnt = devm_kcalloc(rvu->dev, bp->bpids.max,
535 sizeof(u8), GFP_KERNEL);
542 void rvu_nix_flr_free_bpids(struct rvu *rvu, u16 pcifunc)
544 int blkaddr, bpid, err;
545 struct nix_hw *nix_hw;
548 if (!is_lbk_vf(rvu, pcifunc))
551 err = nix_get_struct_ptrs(rvu, pcifunc, &nix_hw, &blkaddr);
557 mutex_lock(&rvu->rsrc_lock);
558 for (bpid = 0; bpid < bp->bpids.max; bpid++) {
559 if (bp->fn_map[bpid] == pcifunc) {
561 if (bp->ref_cnt[bpid])
563 rvu_free_rsrc(&bp->bpids, bpid);
564 bp->fn_map[bpid] = 0;
567 mutex_unlock(&rvu->rsrc_lock);
570 int rvu_mbox_handler_nix_bp_disable(struct rvu *rvu,
571 struct nix_bp_cfg_req *req,
574 u16 pcifunc = req->hdr.pcifunc;
575 int blkaddr, pf, type, err;
576 u16 chan_base, chan, bpid;
577 struct rvu_pfvf *pfvf;
578 struct nix_hw *nix_hw;
582 pf = rvu_get_pf(pcifunc);
583 type = is_lbk_vf(rvu, pcifunc) ? NIX_INTF_TYPE_LBK : NIX_INTF_TYPE_CGX;
584 if (!is_pf_cgxmapped(rvu, pf) && type != NIX_INTF_TYPE_LBK)
587 pfvf = rvu_get_pfvf(rvu, pcifunc);
588 err = nix_get_struct_ptrs(rvu, pcifunc, &nix_hw, &blkaddr);
593 chan_base = pfvf->rx_chan_base + req->chan_base;
594 for (chan = chan_base; chan < (chan_base + req->chan_cnt); chan++) {
595 cfg = rvu_read64(rvu, blkaddr, NIX_AF_RX_CHANX_CFG(chan));
596 rvu_write64(rvu, blkaddr, NIX_AF_RX_CHANX_CFG(chan),
599 if (type == NIX_INTF_TYPE_LBK) {
600 bpid = cfg & GENMASK(8, 0);
601 mutex_lock(&rvu->rsrc_lock);
602 rvu_free_rsrc(&bp->bpids, bpid - bp->free_pool_base);
603 for (bpid = 0; bpid < bp->bpids.max; bpid++) {
604 if (bp->fn_map[bpid] == pcifunc) {
605 bp->fn_map[bpid] = 0;
606 bp->ref_cnt[bpid] = 0;
609 mutex_unlock(&rvu->rsrc_lock);
615 static int rvu_nix_get_bpid(struct rvu *rvu, struct nix_bp_cfg_req *req,
616 int type, int chan_id)
618 int bpid, blkaddr, sdp_chan_base, err;
619 struct rvu_hwinfo *hw = rvu->hw;
620 struct rvu_pfvf *pfvf;
621 struct nix_hw *nix_hw;
625 pfvf = rvu_get_pfvf(rvu, req->hdr.pcifunc);
627 err = nix_get_struct_ptrs(rvu, req->hdr.pcifunc, &nix_hw, &blkaddr);
633 /* Backpressure IDs range division
634 * CGX channles are mapped to (0 - 191) BPIDs
635 * LBK channles are mapped to (192 - 255) BPIDs
636 * SDP channles are mapped to (256 - 511) BPIDs
638 * Lmac channles and bpids mapped as follows
639 * cgx(0)_lmac(0)_chan(0 - 15) = bpid(0 - 15)
640 * cgx(0)_lmac(1)_chan(0 - 15) = bpid(16 - 31) ....
641 * cgx(1)_lmac(0)_chan(0 - 15) = bpid(64 - 79) ....
644 case NIX_INTF_TYPE_CGX:
645 if ((req->chan_base + req->chan_cnt) > NIX_BPIDS_PER_LMAC)
646 return NIX_AF_ERR_INVALID_BPID_REQ;
647 rvu_get_cgx_lmac_id(pfvf->cgx_lmac, &cgx_id, &lmac_id);
648 /* Assign bpid based on cgx, lmac and chan id */
649 bpid = (cgx_id * hw->lmac_per_cgx * NIX_BPIDS_PER_LMAC) +
650 (lmac_id * NIX_BPIDS_PER_LMAC) + req->chan_base;
652 if (req->bpid_per_chan)
654 if (bpid > bp->cgx_bpid_cnt)
655 return NIX_AF_ERR_INVALID_BPID;
658 case NIX_INTF_TYPE_LBK:
659 /* Alloc bpid from the free pool */
660 mutex_lock(&rvu->rsrc_lock);
661 bpid = rvu_alloc_rsrc(&bp->bpids);
663 mutex_unlock(&rvu->rsrc_lock);
664 return NIX_AF_ERR_INVALID_BPID;
666 bp->fn_map[bpid] = req->hdr.pcifunc;
668 bpid += bp->free_pool_base;
669 mutex_unlock(&rvu->rsrc_lock);
671 case NIX_INTF_TYPE_SDP:
672 if ((req->chan_base + req->chan_cnt) > bp->sdp_bpid_cnt)
673 return NIX_AF_ERR_INVALID_BPID_REQ;
675 /* Handle usecase of 2 SDP blocks */
676 if (!hw->cap.programmable_chans)
677 sdp_chan_base = pfvf->rx_chan_base - NIX_CHAN_SDP_CH_START;
679 sdp_chan_base = pfvf->rx_chan_base - hw->sdp_chan_base;
681 bpid = bp->cgx_bpid_cnt + req->chan_base + sdp_chan_base;
682 if (req->bpid_per_chan)
685 if (bpid > (bp->cgx_bpid_cnt + bp->sdp_bpid_cnt))
686 return NIX_AF_ERR_INVALID_BPID;
694 int rvu_mbox_handler_nix_bp_enable(struct rvu *rvu,
695 struct nix_bp_cfg_req *req,
696 struct nix_bp_cfg_rsp *rsp)
698 int blkaddr, pf, type, chan_id = 0;
699 u16 pcifunc = req->hdr.pcifunc;
700 struct rvu_pfvf *pfvf;
705 pf = rvu_get_pf(pcifunc);
706 type = is_lbk_vf(rvu, pcifunc) ? NIX_INTF_TYPE_LBK : NIX_INTF_TYPE_CGX;
707 if (is_sdp_pfvf(pcifunc))
708 type = NIX_INTF_TYPE_SDP;
710 /* Enable backpressure only for CGX mapped PFs and LBK/SDP interface */
711 if (!is_pf_cgxmapped(rvu, pf) && type != NIX_INTF_TYPE_LBK &&
712 type != NIX_INTF_TYPE_SDP)
715 pfvf = rvu_get_pfvf(rvu, pcifunc);
716 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
718 bpid_base = rvu_nix_get_bpid(rvu, req, type, chan_id);
719 chan_base = pfvf->rx_chan_base + req->chan_base;
722 for (chan = chan_base; chan < (chan_base + req->chan_cnt); chan++) {
724 dev_warn(rvu->dev, "Fail to enable backpressure\n");
728 cfg = rvu_read64(rvu, blkaddr, NIX_AF_RX_CHANX_CFG(chan));
729 cfg &= ~GENMASK_ULL(8, 0);
730 rvu_write64(rvu, blkaddr, NIX_AF_RX_CHANX_CFG(chan),
731 cfg | (bpid & GENMASK_ULL(8, 0)) | BIT_ULL(16));
733 bpid = rvu_nix_get_bpid(rvu, req, type, chan_id);
736 for (chan = 0; chan < req->chan_cnt; chan++) {
737 /* Map channel and bpid assign to it */
738 rsp->chan_bpid[chan] = ((req->chan_base + chan) & 0x7F) << 10 |
740 if (req->bpid_per_chan)
743 rsp->chan_cnt = req->chan_cnt;
748 static void nix_setup_lso_tso_l3(struct rvu *rvu, int blkaddr,
749 u64 format, bool v4, u64 *fidx)
751 struct nix_lso_format field = {0};
753 /* IP's Length field */
754 field.layer = NIX_TXLAYER_OL3;
755 /* In ipv4, length field is at offset 2 bytes, for ipv6 it's 4 */
756 field.offset = v4 ? 2 : 4;
757 field.sizem1 = 1; /* i.e 2 bytes */
758 field.alg = NIX_LSOALG_ADD_PAYLEN;
759 rvu_write64(rvu, blkaddr,
760 NIX_AF_LSO_FORMATX_FIELDX(format, (*fidx)++),
763 /* No ID field in IPv6 header */
768 field.layer = NIX_TXLAYER_OL3;
770 field.sizem1 = 1; /* i.e 2 bytes */
771 field.alg = NIX_LSOALG_ADD_SEGNUM;
772 rvu_write64(rvu, blkaddr,
773 NIX_AF_LSO_FORMATX_FIELDX(format, (*fidx)++),
777 static void nix_setup_lso_tso_l4(struct rvu *rvu, int blkaddr,
778 u64 format, u64 *fidx)
780 struct nix_lso_format field = {0};
782 /* TCP's sequence number field */
783 field.layer = NIX_TXLAYER_OL4;
785 field.sizem1 = 3; /* i.e 4 bytes */
786 field.alg = NIX_LSOALG_ADD_OFFSET;
787 rvu_write64(rvu, blkaddr,
788 NIX_AF_LSO_FORMATX_FIELDX(format, (*fidx)++),
791 /* TCP's flags field */
792 field.layer = NIX_TXLAYER_OL4;
794 field.sizem1 = 1; /* 2 bytes */
795 field.alg = NIX_LSOALG_TCP_FLAGS;
796 rvu_write64(rvu, blkaddr,
797 NIX_AF_LSO_FORMATX_FIELDX(format, (*fidx)++),
801 static void nix_setup_lso(struct rvu *rvu, struct nix_hw *nix_hw, int blkaddr)
803 u64 cfg, idx, fidx = 0;
805 /* Get max HW supported format indices */
806 cfg = (rvu_read64(rvu, blkaddr, NIX_AF_CONST1) >> 48) & 0xFF;
807 nix_hw->lso.total = cfg;
810 cfg = rvu_read64(rvu, blkaddr, NIX_AF_LSO_CFG);
811 /* For TSO, set first and middle segment flags to
812 * mask out PSH, RST & FIN flags in TCP packet
814 cfg &= ~((0xFFFFULL << 32) | (0xFFFFULL << 16));
815 cfg |= (0xFFF2ULL << 32) | (0xFFF2ULL << 16);
816 rvu_write64(rvu, blkaddr, NIX_AF_LSO_CFG, cfg | BIT_ULL(63));
818 /* Setup default static LSO formats
820 * Configure format fields for TCPv4 segmentation offload
822 idx = NIX_LSO_FORMAT_IDX_TSOV4;
823 nix_setup_lso_tso_l3(rvu, blkaddr, idx, true, &fidx);
824 nix_setup_lso_tso_l4(rvu, blkaddr, idx, &fidx);
826 /* Set rest of the fields to NOP */
827 for (; fidx < 8; fidx++) {
828 rvu_write64(rvu, blkaddr,
829 NIX_AF_LSO_FORMATX_FIELDX(idx, fidx), 0x0ULL);
831 nix_hw->lso.in_use++;
833 /* Configure format fields for TCPv6 segmentation offload */
834 idx = NIX_LSO_FORMAT_IDX_TSOV6;
836 nix_setup_lso_tso_l3(rvu, blkaddr, idx, false, &fidx);
837 nix_setup_lso_tso_l4(rvu, blkaddr, idx, &fidx);
839 /* Set rest of the fields to NOP */
840 for (; fidx < 8; fidx++) {
841 rvu_write64(rvu, blkaddr,
842 NIX_AF_LSO_FORMATX_FIELDX(idx, fidx), 0x0ULL);
844 nix_hw->lso.in_use++;
847 static void nix_ctx_free(struct rvu *rvu, struct rvu_pfvf *pfvf)
849 kfree(pfvf->rq_bmap);
850 kfree(pfvf->sq_bmap);
851 kfree(pfvf->cq_bmap);
853 qmem_free(rvu->dev, pfvf->rq_ctx);
855 qmem_free(rvu->dev, pfvf->sq_ctx);
857 qmem_free(rvu->dev, pfvf->cq_ctx);
859 qmem_free(rvu->dev, pfvf->rss_ctx);
860 if (pfvf->nix_qints_ctx)
861 qmem_free(rvu->dev, pfvf->nix_qints_ctx);
862 if (pfvf->cq_ints_ctx)
863 qmem_free(rvu->dev, pfvf->cq_ints_ctx);
865 pfvf->rq_bmap = NULL;
866 pfvf->cq_bmap = NULL;
867 pfvf->sq_bmap = NULL;
871 pfvf->rss_ctx = NULL;
872 pfvf->nix_qints_ctx = NULL;
873 pfvf->cq_ints_ctx = NULL;
876 static int nixlf_rss_ctx_init(struct rvu *rvu, int blkaddr,
877 struct rvu_pfvf *pfvf, int nixlf,
878 int rss_sz, int rss_grps, int hwctx_size,
879 u64 way_mask, bool tag_lsb_as_adder)
881 int err, grp, num_indices;
884 /* RSS is not requested for this NIXLF */
887 num_indices = rss_sz * rss_grps;
889 /* Alloc NIX RSS HW context memory and config the base */
890 err = qmem_alloc(rvu->dev, &pfvf->rss_ctx, num_indices, hwctx_size);
894 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RSS_BASE(nixlf),
895 (u64)pfvf->rss_ctx->iova);
897 /* Config full RSS table size, enable RSS and caching */
898 val = BIT_ULL(36) | BIT_ULL(4) | way_mask << 20 |
899 ilog2(num_indices / MAX_RSS_INDIR_TBL_SIZE);
901 if (tag_lsb_as_adder)
904 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RSS_CFG(nixlf), val);
905 /* Config RSS group offset and sizes */
906 for (grp = 0; grp < rss_grps; grp++)
907 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RSS_GRPX(nixlf, grp),
908 ((ilog2(rss_sz) - 1) << 16) | (rss_sz * grp));
912 static int nix_aq_enqueue_wait(struct rvu *rvu, struct rvu_block *block,
913 struct nix_aq_inst_s *inst)
915 struct admin_queue *aq = block->aq;
916 struct nix_aq_res_s *result;
921 result = (struct nix_aq_res_s *)aq->res->base;
923 /* Get current head pointer where to append this instruction */
924 reg = rvu_read64(rvu, block->addr, NIX_AF_AQ_STATUS);
925 head = (reg >> 4) & AQ_PTR_MASK;
927 memcpy((void *)(aq->inst->base + (head * aq->inst->entry_sz)),
928 (void *)inst, aq->inst->entry_sz);
929 memset(result, 0, sizeof(*result));
930 /* sync into memory */
933 /* Ring the doorbell and wait for result */
934 rvu_write64(rvu, block->addr, NIX_AF_AQ_DOOR, 1);
935 while (result->compcode == NIX_AQ_COMP_NOTDONE) {
943 if (result->compcode != NIX_AQ_COMP_GOOD) {
944 /* TODO: Replace this with some error code */
945 if (result->compcode == NIX_AQ_COMP_CTX_FAULT ||
946 result->compcode == NIX_AQ_COMP_LOCKERR ||
947 result->compcode == NIX_AQ_COMP_CTX_POISON) {
948 ret = rvu_ndc_fix_locked_cacheline(rvu, BLKADDR_NDC_NIX0_RX);
949 ret |= rvu_ndc_fix_locked_cacheline(rvu, BLKADDR_NDC_NIX0_TX);
950 ret |= rvu_ndc_fix_locked_cacheline(rvu, BLKADDR_NDC_NIX1_RX);
951 ret |= rvu_ndc_fix_locked_cacheline(rvu, BLKADDR_NDC_NIX1_TX);
954 "%s: Not able to unlock cachelines\n", __func__);
963 static void nix_get_aq_req_smq(struct rvu *rvu, struct nix_aq_enq_req *req,
964 u16 *smq, u16 *smq_mask)
966 struct nix_cn10k_aq_enq_req *aq_req;
968 if (!is_rvu_otx2(rvu)) {
969 aq_req = (struct nix_cn10k_aq_enq_req *)req;
970 *smq = aq_req->sq.smq;
971 *smq_mask = aq_req->sq_mask.smq;
974 *smq_mask = req->sq_mask.smq;
978 static int rvu_nix_blk_aq_enq_inst(struct rvu *rvu, struct nix_hw *nix_hw,
979 struct nix_aq_enq_req *req,
980 struct nix_aq_enq_rsp *rsp)
982 struct rvu_hwinfo *hw = rvu->hw;
983 u16 pcifunc = req->hdr.pcifunc;
984 int nixlf, blkaddr, rc = 0;
985 struct nix_aq_inst_s inst;
986 struct rvu_block *block;
987 struct admin_queue *aq;
988 struct rvu_pfvf *pfvf;
994 blkaddr = nix_hw->blkaddr;
995 block = &hw->block[blkaddr];
998 dev_warn(rvu->dev, "%s: NIX AQ not initialized\n", __func__);
999 return NIX_AF_ERR_AQ_ENQUEUE;
1002 pfvf = rvu_get_pfvf(rvu, pcifunc);
1003 nixlf = rvu_get_lf(rvu, block, pcifunc, 0);
1005 /* Skip NIXLF check for broadcast MCE entry and bandwidth profile
1006 * operations done by AF itself.
1008 if (!((!rsp && req->ctype == NIX_AQ_CTYPE_MCE) ||
1009 (req->ctype == NIX_AQ_CTYPE_BANDPROF && !pcifunc))) {
1010 if (!pfvf->nixlf || nixlf < 0)
1011 return NIX_AF_ERR_AF_LF_INVALID;
1014 switch (req->ctype) {
1015 case NIX_AQ_CTYPE_RQ:
1016 /* Check if index exceeds max no of queues */
1017 if (!pfvf->rq_ctx || req->qidx >= pfvf->rq_ctx->qsize)
1018 rc = NIX_AF_ERR_AQ_ENQUEUE;
1020 case NIX_AQ_CTYPE_SQ:
1021 if (!pfvf->sq_ctx || req->qidx >= pfvf->sq_ctx->qsize)
1022 rc = NIX_AF_ERR_AQ_ENQUEUE;
1024 case NIX_AQ_CTYPE_CQ:
1025 if (!pfvf->cq_ctx || req->qidx >= pfvf->cq_ctx->qsize)
1026 rc = NIX_AF_ERR_AQ_ENQUEUE;
1028 case NIX_AQ_CTYPE_RSS:
1029 /* Check if RSS is enabled and qidx is within range */
1030 cfg = rvu_read64(rvu, blkaddr, NIX_AF_LFX_RSS_CFG(nixlf));
1031 if (!(cfg & BIT_ULL(4)) || !pfvf->rss_ctx ||
1032 (req->qidx >= (256UL << (cfg & 0xF))))
1033 rc = NIX_AF_ERR_AQ_ENQUEUE;
1035 case NIX_AQ_CTYPE_MCE:
1036 cfg = rvu_read64(rvu, blkaddr, NIX_AF_RX_MCAST_CFG);
1038 /* Check if index exceeds MCE list length */
1039 if (!nix_hw->mcast.mce_ctx ||
1040 (req->qidx >= (256UL << (cfg & 0xF))))
1041 rc = NIX_AF_ERR_AQ_ENQUEUE;
1043 /* Adding multicast lists for requests from PF/VFs is not
1044 * yet supported, so ignore this.
1047 rc = NIX_AF_ERR_AQ_ENQUEUE;
1049 case NIX_AQ_CTYPE_BANDPROF:
1050 if (nix_verify_bandprof((struct nix_cn10k_aq_enq_req *)req,
1052 rc = NIX_AF_ERR_INVALID_BANDPROF;
1055 rc = NIX_AF_ERR_AQ_ENQUEUE;
1061 nix_get_aq_req_smq(rvu, req, &smq, &smq_mask);
1062 /* Check if SQ pointed SMQ belongs to this PF/VF or not */
1063 if (req->ctype == NIX_AQ_CTYPE_SQ &&
1064 ((req->op == NIX_AQ_INSTOP_INIT && req->sq.ena) ||
1065 (req->op == NIX_AQ_INSTOP_WRITE &&
1066 req->sq_mask.ena && req->sq.ena && smq_mask))) {
1067 if (!is_valid_txschq(rvu, blkaddr, NIX_TXSCH_LVL_SMQ,
1069 return NIX_AF_ERR_AQ_ENQUEUE;
1072 memset(&inst, 0, sizeof(struct nix_aq_inst_s));
1074 inst.cindex = req->qidx;
1075 inst.ctype = req->ctype;
1077 /* Currently we are not supporting enqueuing multiple instructions,
1078 * so always choose first entry in result memory.
1080 inst.res_addr = (u64)aq->res->iova;
1082 /* Hardware uses same aq->res->base for updating result of
1083 * previous instruction hence wait here till it is done.
1085 spin_lock(&aq->lock);
1087 /* Clean result + context memory */
1088 memset(aq->res->base, 0, aq->res->entry_sz);
1089 /* Context needs to be written at RES_ADDR + 128 */
1090 ctx = aq->res->base + 128;
1091 /* Mask needs to be written at RES_ADDR + 256 */
1092 mask = aq->res->base + 256;
1095 case NIX_AQ_INSTOP_WRITE:
1096 if (req->ctype == NIX_AQ_CTYPE_RQ)
1097 memcpy(mask, &req->rq_mask,
1098 sizeof(struct nix_rq_ctx_s));
1099 else if (req->ctype == NIX_AQ_CTYPE_SQ)
1100 memcpy(mask, &req->sq_mask,
1101 sizeof(struct nix_sq_ctx_s));
1102 else if (req->ctype == NIX_AQ_CTYPE_CQ)
1103 memcpy(mask, &req->cq_mask,
1104 sizeof(struct nix_cq_ctx_s));
1105 else if (req->ctype == NIX_AQ_CTYPE_RSS)
1106 memcpy(mask, &req->rss_mask,
1107 sizeof(struct nix_rsse_s));
1108 else if (req->ctype == NIX_AQ_CTYPE_MCE)
1109 memcpy(mask, &req->mce_mask,
1110 sizeof(struct nix_rx_mce_s));
1111 else if (req->ctype == NIX_AQ_CTYPE_BANDPROF)
1112 memcpy(mask, &req->prof_mask,
1113 sizeof(struct nix_bandprof_s));
1115 case NIX_AQ_INSTOP_INIT:
1116 if (req->ctype == NIX_AQ_CTYPE_RQ)
1117 memcpy(ctx, &req->rq, sizeof(struct nix_rq_ctx_s));
1118 else if (req->ctype == NIX_AQ_CTYPE_SQ)
1119 memcpy(ctx, &req->sq, sizeof(struct nix_sq_ctx_s));
1120 else if (req->ctype == NIX_AQ_CTYPE_CQ)
1121 memcpy(ctx, &req->cq, sizeof(struct nix_cq_ctx_s));
1122 else if (req->ctype == NIX_AQ_CTYPE_RSS)
1123 memcpy(ctx, &req->rss, sizeof(struct nix_rsse_s));
1124 else if (req->ctype == NIX_AQ_CTYPE_MCE)
1125 memcpy(ctx, &req->mce, sizeof(struct nix_rx_mce_s));
1126 else if (req->ctype == NIX_AQ_CTYPE_BANDPROF)
1127 memcpy(ctx, &req->prof, sizeof(struct nix_bandprof_s));
1129 case NIX_AQ_INSTOP_NOP:
1130 case NIX_AQ_INSTOP_READ:
1131 case NIX_AQ_INSTOP_LOCK:
1132 case NIX_AQ_INSTOP_UNLOCK:
1135 rc = NIX_AF_ERR_AQ_ENQUEUE;
1136 spin_unlock(&aq->lock);
1140 /* Submit the instruction to AQ */
1141 rc = nix_aq_enqueue_wait(rvu, block, &inst);
1143 spin_unlock(&aq->lock);
1147 /* Set RQ/SQ/CQ bitmap if respective queue hw context is enabled */
1148 if (req->op == NIX_AQ_INSTOP_INIT) {
1149 if (req->ctype == NIX_AQ_CTYPE_RQ && req->rq.ena)
1150 __set_bit(req->qidx, pfvf->rq_bmap);
1151 if (req->ctype == NIX_AQ_CTYPE_SQ && req->sq.ena)
1152 __set_bit(req->qidx, pfvf->sq_bmap);
1153 if (req->ctype == NIX_AQ_CTYPE_CQ && req->cq.ena)
1154 __set_bit(req->qidx, pfvf->cq_bmap);
1157 if (req->op == NIX_AQ_INSTOP_WRITE) {
1158 if (req->ctype == NIX_AQ_CTYPE_RQ) {
1159 ena = (req->rq.ena & req->rq_mask.ena) |
1160 (test_bit(req->qidx, pfvf->rq_bmap) &
1163 __set_bit(req->qidx, pfvf->rq_bmap);
1165 __clear_bit(req->qidx, pfvf->rq_bmap);
1167 if (req->ctype == NIX_AQ_CTYPE_SQ) {
1168 ena = (req->rq.ena & req->sq_mask.ena) |
1169 (test_bit(req->qidx, pfvf->sq_bmap) &
1172 __set_bit(req->qidx, pfvf->sq_bmap);
1174 __clear_bit(req->qidx, pfvf->sq_bmap);
1176 if (req->ctype == NIX_AQ_CTYPE_CQ) {
1177 ena = (req->rq.ena & req->cq_mask.ena) |
1178 (test_bit(req->qidx, pfvf->cq_bmap) &
1181 __set_bit(req->qidx, pfvf->cq_bmap);
1183 __clear_bit(req->qidx, pfvf->cq_bmap);
1188 /* Copy read context into mailbox */
1189 if (req->op == NIX_AQ_INSTOP_READ) {
1190 if (req->ctype == NIX_AQ_CTYPE_RQ)
1191 memcpy(&rsp->rq, ctx,
1192 sizeof(struct nix_rq_ctx_s));
1193 else if (req->ctype == NIX_AQ_CTYPE_SQ)
1194 memcpy(&rsp->sq, ctx,
1195 sizeof(struct nix_sq_ctx_s));
1196 else if (req->ctype == NIX_AQ_CTYPE_CQ)
1197 memcpy(&rsp->cq, ctx,
1198 sizeof(struct nix_cq_ctx_s));
1199 else if (req->ctype == NIX_AQ_CTYPE_RSS)
1200 memcpy(&rsp->rss, ctx,
1201 sizeof(struct nix_rsse_s));
1202 else if (req->ctype == NIX_AQ_CTYPE_MCE)
1203 memcpy(&rsp->mce, ctx,
1204 sizeof(struct nix_rx_mce_s));
1205 else if (req->ctype == NIX_AQ_CTYPE_BANDPROF)
1206 memcpy(&rsp->prof, ctx,
1207 sizeof(struct nix_bandprof_s));
1211 spin_unlock(&aq->lock);
1215 static int rvu_nix_verify_aq_ctx(struct rvu *rvu, struct nix_hw *nix_hw,
1216 struct nix_aq_enq_req *req, u8 ctype)
1218 struct nix_cn10k_aq_enq_req aq_req;
1219 struct nix_cn10k_aq_enq_rsp aq_rsp;
1222 if (req->ctype != NIX_AQ_CTYPE_CQ)
1225 rc = nix_aq_context_read(rvu, nix_hw, &aq_req, &aq_rsp,
1226 req->hdr.pcifunc, ctype, req->qidx);
1229 "%s: Failed to fetch %s%d context of PFFUNC 0x%x\n",
1230 __func__, nix_get_ctx_name(ctype), req->qidx,
1235 /* Make copy of original context & mask which are required
1238 memcpy(&aq_req.cq_mask, &req->cq_mask, sizeof(struct nix_cq_ctx_s));
1239 memcpy(&aq_req.cq, &req->cq, sizeof(struct nix_cq_ctx_s));
1241 /* exclude fields which HW can update */
1242 aq_req.cq_mask.cq_err = 0;
1243 aq_req.cq_mask.wrptr = 0;
1244 aq_req.cq_mask.tail = 0;
1245 aq_req.cq_mask.head = 0;
1246 aq_req.cq_mask.avg_level = 0;
1247 aq_req.cq_mask.update_time = 0;
1248 aq_req.cq_mask.substream = 0;
1250 /* Context mask (cq_mask) holds mask value of fields which
1251 * are changed in AQ WRITE operation.
1252 * for example cq.drop = 0xa;
1253 * cq_mask.drop = 0xff;
1254 * Below logic performs '&' between cq and cq_mask so that non
1255 * updated fields are masked out for request and response
1258 for (word = 0; word < sizeof(struct nix_cq_ctx_s) / sizeof(u64);
1260 *(u64 *)((u8 *)&aq_rsp.cq + word * 8) &=
1261 (*(u64 *)((u8 *)&aq_req.cq_mask + word * 8));
1262 *(u64 *)((u8 *)&aq_req.cq + word * 8) &=
1263 (*(u64 *)((u8 *)&aq_req.cq_mask + word * 8));
1266 if (memcmp(&aq_req.cq, &aq_rsp.cq, sizeof(struct nix_cq_ctx_s)))
1267 return NIX_AF_ERR_AQ_CTX_RETRY_WRITE;
1272 static int rvu_nix_aq_enq_inst(struct rvu *rvu, struct nix_aq_enq_req *req,
1273 struct nix_aq_enq_rsp *rsp)
1275 struct nix_hw *nix_hw;
1276 int err, retries = 5;
1279 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, req->hdr.pcifunc);
1281 return NIX_AF_ERR_AF_LF_INVALID;
1283 nix_hw = get_nix_hw(rvu->hw, blkaddr);
1285 return NIX_AF_ERR_INVALID_NIXBLK;
1288 err = rvu_nix_blk_aq_enq_inst(rvu, nix_hw, req, rsp);
1290 /* HW errata 'AQ Modification to CQ could be discarded on heavy traffic'
1291 * As a work around perfrom CQ context read after each AQ write. If AQ
1292 * read shows AQ write is not updated perform AQ write again.
1294 if (!err && req->op == NIX_AQ_INSTOP_WRITE) {
1295 err = rvu_nix_verify_aq_ctx(rvu, nix_hw, req, NIX_AQ_CTYPE_CQ);
1296 if (err == NIX_AF_ERR_AQ_CTX_RETRY_WRITE) {
1300 return NIX_AF_ERR_CQ_CTX_WRITE_ERR;
1307 static const char *nix_get_ctx_name(int ctype)
1310 case NIX_AQ_CTYPE_CQ:
1312 case NIX_AQ_CTYPE_SQ:
1314 case NIX_AQ_CTYPE_RQ:
1316 case NIX_AQ_CTYPE_RSS:
1322 static int nix_lf_hwctx_disable(struct rvu *rvu, struct hwctx_disable_req *req)
1324 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, req->hdr.pcifunc);
1325 struct nix_aq_enq_req aq_req;
1326 unsigned long *bmap;
1327 int qidx, q_cnt = 0;
1330 if (!pfvf->cq_ctx || !pfvf->sq_ctx || !pfvf->rq_ctx)
1331 return NIX_AF_ERR_AQ_ENQUEUE;
1333 memset(&aq_req, 0, sizeof(struct nix_aq_enq_req));
1334 aq_req.hdr.pcifunc = req->hdr.pcifunc;
1336 if (req->ctype == NIX_AQ_CTYPE_CQ) {
1338 aq_req.cq_mask.ena = 1;
1339 aq_req.cq.bp_ena = 0;
1340 aq_req.cq_mask.bp_ena = 1;
1341 q_cnt = pfvf->cq_ctx->qsize;
1342 bmap = pfvf->cq_bmap;
1344 if (req->ctype == NIX_AQ_CTYPE_SQ) {
1346 aq_req.sq_mask.ena = 1;
1347 q_cnt = pfvf->sq_ctx->qsize;
1348 bmap = pfvf->sq_bmap;
1350 if (req->ctype == NIX_AQ_CTYPE_RQ) {
1352 aq_req.rq_mask.ena = 1;
1353 q_cnt = pfvf->rq_ctx->qsize;
1354 bmap = pfvf->rq_bmap;
1357 aq_req.ctype = req->ctype;
1358 aq_req.op = NIX_AQ_INSTOP_WRITE;
1360 for (qidx = 0; qidx < q_cnt; qidx++) {
1361 if (!test_bit(qidx, bmap))
1364 rc = rvu_nix_aq_enq_inst(rvu, &aq_req, NULL);
1367 dev_err(rvu->dev, "Failed to disable %s:%d context\n",
1368 nix_get_ctx_name(req->ctype), qidx);
1375 #ifdef CONFIG_NDC_DIS_DYNAMIC_CACHING
1376 static int nix_lf_hwctx_lockdown(struct rvu *rvu, struct nix_aq_enq_req *req)
1378 struct nix_aq_enq_req lock_ctx_req;
1381 if (req->op != NIX_AQ_INSTOP_INIT)
1384 if (req->ctype == NIX_AQ_CTYPE_MCE ||
1385 req->ctype == NIX_AQ_CTYPE_DYNO)
1388 memset(&lock_ctx_req, 0, sizeof(struct nix_aq_enq_req));
1389 lock_ctx_req.hdr.pcifunc = req->hdr.pcifunc;
1390 lock_ctx_req.ctype = req->ctype;
1391 lock_ctx_req.op = NIX_AQ_INSTOP_LOCK;
1392 lock_ctx_req.qidx = req->qidx;
1393 err = rvu_nix_aq_enq_inst(rvu, &lock_ctx_req, NULL);
1396 "PFUNC 0x%x: Failed to lock NIX %s:%d context\n",
1398 nix_get_ctx_name(req->ctype), req->qidx);
1402 int rvu_mbox_handler_nix_aq_enq(struct rvu *rvu,
1403 struct nix_aq_enq_req *req,
1404 struct nix_aq_enq_rsp *rsp)
1408 err = rvu_nix_aq_enq_inst(rvu, req, rsp);
1410 err = nix_lf_hwctx_lockdown(rvu, req);
1415 int rvu_mbox_handler_nix_aq_enq(struct rvu *rvu,
1416 struct nix_aq_enq_req *req,
1417 struct nix_aq_enq_rsp *rsp)
1419 return rvu_nix_aq_enq_inst(rvu, req, rsp);
1422 /* CN10K mbox handler */
1423 int rvu_mbox_handler_nix_cn10k_aq_enq(struct rvu *rvu,
1424 struct nix_cn10k_aq_enq_req *req,
1425 struct nix_cn10k_aq_enq_rsp *rsp)
1427 return rvu_nix_aq_enq_inst(rvu, (struct nix_aq_enq_req *)req,
1428 (struct nix_aq_enq_rsp *)rsp);
1431 int rvu_mbox_handler_nix_hwctx_disable(struct rvu *rvu,
1432 struct hwctx_disable_req *req,
1433 struct msg_rsp *rsp)
1435 return nix_lf_hwctx_disable(rvu, req);
1438 int rvu_mbox_handler_nix_lf_alloc(struct rvu *rvu,
1439 struct nix_lf_alloc_req *req,
1440 struct nix_lf_alloc_rsp *rsp)
1442 int nixlf, qints, hwctx_size, intf, err, rc = 0;
1443 struct rvu_hwinfo *hw = rvu->hw;
1444 u16 pcifunc = req->hdr.pcifunc;
1445 struct rvu_block *block;
1446 struct rvu_pfvf *pfvf;
1450 if (!req->rq_cnt || !req->sq_cnt || !req->cq_cnt)
1451 return NIX_AF_ERR_PARAM;
1454 req->way_mask &= 0xFFFF;
1456 pfvf = rvu_get_pfvf(rvu, pcifunc);
1457 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
1458 if (!pfvf->nixlf || blkaddr < 0)
1459 return NIX_AF_ERR_AF_LF_INVALID;
1461 block = &hw->block[blkaddr];
1462 nixlf = rvu_get_lf(rvu, block, pcifunc, 0);
1464 return NIX_AF_ERR_AF_LF_INVALID;
1466 /* Check if requested 'NIXLF <=> NPALF' mapping is valid */
1467 if (req->npa_func) {
1468 /* If default, use 'this' NIXLF's PFFUNC */
1469 if (req->npa_func == RVU_DEFAULT_PF_FUNC)
1470 req->npa_func = pcifunc;
1471 if (!is_pffunc_map_valid(rvu, req->npa_func, BLKTYPE_NPA))
1472 return NIX_AF_INVAL_NPA_PF_FUNC;
1475 /* Check if requested 'NIXLF <=> SSOLF' mapping is valid */
1476 if (req->sso_func) {
1477 /* If default, use 'this' NIXLF's PFFUNC */
1478 if (req->sso_func == RVU_DEFAULT_PF_FUNC)
1479 req->sso_func = pcifunc;
1480 if (!is_pffunc_map_valid(rvu, req->sso_func, BLKTYPE_SSO))
1481 return NIX_AF_INVAL_SSO_PF_FUNC;
1484 /* If RSS is being enabled, check if requested config is valid.
1485 * RSS table size should be power of two, otherwise
1486 * RSS_GRP::OFFSET + adder might go beyond that group or
1487 * won't be able to use entire table.
1489 if (req->rss_sz && (req->rss_sz > MAX_RSS_INDIR_TBL_SIZE ||
1490 !is_power_of_2(req->rss_sz)))
1491 return NIX_AF_ERR_RSS_SIZE_INVALID;
1494 (!req->rss_grps || req->rss_grps > MAX_RSS_GROUPS))
1495 return NIX_AF_ERR_RSS_GRPS_INVALID;
1497 /* Reset this NIX LF */
1498 err = rvu_lf_reset(rvu, block, nixlf);
1500 dev_err(rvu->dev, "Failed to reset NIX%d LF%d\n",
1501 block->addr - BLKADDR_NIX0, nixlf);
1502 return NIX_AF_ERR_LF_RESET;
1505 ctx_cfg = rvu_read64(rvu, blkaddr, NIX_AF_CONST3);
1507 /* Alloc NIX RQ HW context memory and config the base */
1508 hwctx_size = 1UL << ((ctx_cfg >> 4) & 0xF);
1509 err = qmem_alloc(rvu->dev, &pfvf->rq_ctx, req->rq_cnt, hwctx_size);
1513 pfvf->rq_bmap = kcalloc(req->rq_cnt, sizeof(long), GFP_KERNEL);
1517 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RQS_BASE(nixlf),
1518 (u64)pfvf->rq_ctx->iova);
1520 /* Set caching and queue count in HW */
1521 cfg = BIT_ULL(36) | (req->rq_cnt - 1) | req->way_mask << 20;
1522 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RQS_CFG(nixlf), cfg);
1524 /* Alloc NIX SQ HW context memory and config the base */
1525 hwctx_size = 1UL << (ctx_cfg & 0xF);
1526 err = qmem_alloc(rvu->dev, &pfvf->sq_ctx, req->sq_cnt, hwctx_size);
1530 pfvf->sq_bmap = kcalloc(req->sq_cnt, sizeof(long), GFP_KERNEL);
1534 rvu_write64(rvu, blkaddr, NIX_AF_LFX_SQS_BASE(nixlf),
1535 (u64)pfvf->sq_ctx->iova);
1537 cfg = BIT_ULL(36) | (req->sq_cnt - 1) | req->way_mask << 20;
1538 rvu_write64(rvu, blkaddr, NIX_AF_LFX_SQS_CFG(nixlf), cfg);
1540 /* Alloc NIX CQ HW context memory and config the base */
1541 hwctx_size = 1UL << ((ctx_cfg >> 8) & 0xF);
1542 err = qmem_alloc(rvu->dev, &pfvf->cq_ctx, req->cq_cnt, hwctx_size);
1546 pfvf->cq_bmap = kcalloc(req->cq_cnt, sizeof(long), GFP_KERNEL);
1550 rvu_write64(rvu, blkaddr, NIX_AF_LFX_CQS_BASE(nixlf),
1551 (u64)pfvf->cq_ctx->iova);
1553 cfg = BIT_ULL(36) | (req->cq_cnt - 1) | req->way_mask << 20;
1554 rvu_write64(rvu, blkaddr, NIX_AF_LFX_CQS_CFG(nixlf), cfg);
1556 /* Initialize receive side scaling (RSS) */
1557 hwctx_size = 1UL << ((ctx_cfg >> 12) & 0xF);
1558 err = nixlf_rss_ctx_init(rvu, blkaddr, pfvf, nixlf, req->rss_sz,
1559 req->rss_grps, hwctx_size, req->way_mask,
1560 !!(req->flags & NIX_LF_RSS_TAG_LSB_AS_ADDER));
1564 /* Alloc memory for CQINT's HW contexts */
1565 cfg = rvu_read64(rvu, blkaddr, NIX_AF_CONST2);
1566 qints = (cfg >> 24) & 0xFFF;
1567 hwctx_size = 1UL << ((ctx_cfg >> 24) & 0xF);
1568 err = qmem_alloc(rvu->dev, &pfvf->cq_ints_ctx, qints, hwctx_size);
1572 rvu_write64(rvu, blkaddr, NIX_AF_LFX_CINTS_BASE(nixlf),
1573 (u64)pfvf->cq_ints_ctx->iova);
1575 rvu_write64(rvu, blkaddr, NIX_AF_LFX_CINTS_CFG(nixlf),
1576 BIT_ULL(36) | req->way_mask << 20);
1578 /* Alloc memory for QINT's HW contexts */
1579 cfg = rvu_read64(rvu, blkaddr, NIX_AF_CONST2);
1580 qints = (cfg >> 12) & 0xFFF;
1581 hwctx_size = 1UL << ((ctx_cfg >> 20) & 0xF);
1582 err = qmem_alloc(rvu->dev, &pfvf->nix_qints_ctx, qints, hwctx_size);
1586 rvu_write64(rvu, blkaddr, NIX_AF_LFX_QINTS_BASE(nixlf),
1587 (u64)pfvf->nix_qints_ctx->iova);
1588 rvu_write64(rvu, blkaddr, NIX_AF_LFX_QINTS_CFG(nixlf),
1589 BIT_ULL(36) | req->way_mask << 20);
1591 /* Setup VLANX TPID's.
1592 * Use VLAN1 for 802.1Q
1593 * and VLAN0 for 802.1AD.
1595 cfg = (0x8100ULL << 16) | 0x88A8ULL;
1596 rvu_write64(rvu, blkaddr, NIX_AF_LFX_TX_CFG(nixlf), cfg);
1598 /* Enable LMTST for this NIX LF */
1599 rvu_write64(rvu, blkaddr, NIX_AF_LFX_TX_CFG2(nixlf), BIT_ULL(0));
1601 /* Set CQE/WQE size, NPA_PF_FUNC for SQBs and also SSO_PF_FUNC */
1603 cfg = req->npa_func;
1605 cfg |= (u64)req->sso_func << 16;
1607 cfg |= (u64)req->xqe_sz << 33;
1608 rvu_write64(rvu, blkaddr, NIX_AF_LFX_CFG(nixlf), cfg);
1610 /* Config Rx pkt length, csum checks and apad enable / disable */
1611 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RX_CFG(nixlf), req->rx_cfg);
1613 /* Configure pkind for TX parse config */
1614 cfg = NPC_TX_DEF_PKIND;
1615 rvu_write64(rvu, blkaddr, NIX_AF_LFX_TX_PARSE_CFG(nixlf), cfg);
1617 intf = is_lbk_vf(rvu, pcifunc) ? NIX_INTF_TYPE_LBK : NIX_INTF_TYPE_CGX;
1618 if (is_sdp_pfvf(pcifunc))
1619 intf = NIX_INTF_TYPE_SDP;
1621 err = nix_interface_init(rvu, pcifunc, intf, nixlf, rsp,
1622 !!(req->flags & NIX_LF_LBK_BLK_SEL));
1626 /* Disable NPC entries as NIXLF's contexts are not initialized yet */
1627 rvu_npc_disable_default_entries(rvu, pcifunc, nixlf);
1629 /* Configure RX VTAG Type 7 (strip) for vf vlan */
1630 rvu_write64(rvu, blkaddr,
1631 NIX_AF_LFX_RX_VTAG_TYPEX(nixlf, NIX_AF_LFX_RX_VTAG_TYPE7),
1632 VTAGSIZE_T4 | VTAG_STRIP);
1637 nix_ctx_free(rvu, pfvf);
1641 /* Set macaddr of this PF/VF */
1642 ether_addr_copy(rsp->mac_addr, pfvf->mac_addr);
1644 /* set SQB size info */
1645 cfg = rvu_read64(rvu, blkaddr, NIX_AF_SQ_CONST);
1646 rsp->sqb_size = (cfg >> 34) & 0xFFFF;
1647 rsp->rx_chan_base = pfvf->rx_chan_base;
1648 rsp->tx_chan_base = pfvf->tx_chan_base;
1649 rsp->rx_chan_cnt = pfvf->rx_chan_cnt;
1650 rsp->tx_chan_cnt = pfvf->tx_chan_cnt;
1651 rsp->lso_tsov4_idx = NIX_LSO_FORMAT_IDX_TSOV4;
1652 rsp->lso_tsov6_idx = NIX_LSO_FORMAT_IDX_TSOV6;
1653 /* Get HW supported stat count */
1654 cfg = rvu_read64(rvu, blkaddr, NIX_AF_CONST1);
1655 rsp->lf_rx_stats = ((cfg >> 32) & 0xFF);
1656 rsp->lf_tx_stats = ((cfg >> 24) & 0xFF);
1657 /* Get count of CQ IRQs and error IRQs supported per LF */
1658 cfg = rvu_read64(rvu, blkaddr, NIX_AF_CONST2);
1659 rsp->qints = ((cfg >> 12) & 0xFFF);
1660 rsp->cints = ((cfg >> 24) & 0xFFF);
1661 rsp->cgx_links = hw->cgx_links;
1662 rsp->lbk_links = hw->lbk_links;
1663 rsp->sdp_links = hw->sdp_links;
1668 int rvu_mbox_handler_nix_lf_free(struct rvu *rvu, struct nix_lf_free_req *req,
1669 struct msg_rsp *rsp)
1671 struct rvu_hwinfo *hw = rvu->hw;
1672 u16 pcifunc = req->hdr.pcifunc;
1673 struct rvu_block *block;
1674 int blkaddr, nixlf, err;
1675 struct rvu_pfvf *pfvf;
1677 pfvf = rvu_get_pfvf(rvu, pcifunc);
1678 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
1679 if (!pfvf->nixlf || blkaddr < 0)
1680 return NIX_AF_ERR_AF_LF_INVALID;
1682 block = &hw->block[blkaddr];
1683 nixlf = rvu_get_lf(rvu, block, pcifunc, 0);
1685 return NIX_AF_ERR_AF_LF_INVALID;
1687 if (req->flags & NIX_LF_DISABLE_FLOWS)
1688 rvu_npc_disable_mcam_entries(rvu, pcifunc, nixlf);
1690 rvu_npc_free_mcam_entries(rvu, pcifunc, nixlf);
1692 /* Free any tx vtag def entries used by this NIX LF */
1693 if (!(req->flags & NIX_LF_DONT_FREE_TX_VTAG))
1694 nix_free_tx_vtag_entries(rvu, pcifunc);
1696 nix_interface_deinit(rvu, pcifunc, nixlf);
1698 /* Reset this NIX LF */
1699 err = rvu_lf_reset(rvu, block, nixlf);
1701 dev_err(rvu->dev, "Failed to reset NIX%d LF%d\n",
1702 block->addr - BLKADDR_NIX0, nixlf);
1703 return NIX_AF_ERR_LF_RESET;
1706 nix_ctx_free(rvu, pfvf);
1711 int rvu_mbox_handler_nix_mark_format_cfg(struct rvu *rvu,
1712 struct nix_mark_format_cfg *req,
1713 struct nix_mark_format_cfg_rsp *rsp)
1715 u16 pcifunc = req->hdr.pcifunc;
1716 struct nix_hw *nix_hw;
1717 struct rvu_pfvf *pfvf;
1721 pfvf = rvu_get_pfvf(rvu, pcifunc);
1722 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
1723 if (!pfvf->nixlf || blkaddr < 0)
1724 return NIX_AF_ERR_AF_LF_INVALID;
1726 nix_hw = get_nix_hw(rvu->hw, blkaddr);
1728 return NIX_AF_ERR_INVALID_NIXBLK;
1730 cfg = (((u32)req->offset & 0x7) << 16) |
1731 (((u32)req->y_mask & 0xF) << 12) |
1732 (((u32)req->y_val & 0xF) << 8) |
1733 (((u32)req->r_mask & 0xF) << 4) | ((u32)req->r_val & 0xF);
1735 rc = rvu_nix_reserve_mark_format(rvu, nix_hw, blkaddr, cfg);
1737 dev_err(rvu->dev, "No mark_format_ctl for (pf:%d, vf:%d)",
1738 rvu_get_pf(pcifunc), pcifunc & RVU_PFVF_FUNC_MASK);
1739 return NIX_AF_ERR_MARK_CFG_FAIL;
1742 rsp->mark_format_idx = rc;
1746 /* Handle shaper update specially for few revisions */
1748 handle_txschq_shaper_update(struct rvu *rvu, int blkaddr, int nixlf,
1749 int lvl, u64 reg, u64 regval)
1751 u64 regbase, oldval, sw_xoff = 0;
1752 u64 dbgval, md_debug0 = 0;
1753 unsigned long poll_tmo;
1757 regbase = reg & 0xFFFF;
1758 schq = TXSCHQ_IDX(reg, TXSCHQ_IDX_SHIFT);
1760 /* Check for rate register */
1762 case NIX_TXSCH_LVL_TL1:
1763 md_debug0 = NIX_AF_TL1X_MD_DEBUG0(schq);
1764 sw_xoff = NIX_AF_TL1X_SW_XOFF(schq);
1766 rate_reg = !!(regbase == NIX_AF_TL1X_CIR(0));
1768 case NIX_TXSCH_LVL_TL2:
1769 md_debug0 = NIX_AF_TL2X_MD_DEBUG0(schq);
1770 sw_xoff = NIX_AF_TL2X_SW_XOFF(schq);
1772 rate_reg = (regbase == NIX_AF_TL2X_CIR(0) ||
1773 regbase == NIX_AF_TL2X_PIR(0));
1775 case NIX_TXSCH_LVL_TL3:
1776 md_debug0 = NIX_AF_TL3X_MD_DEBUG0(schq);
1777 sw_xoff = NIX_AF_TL3X_SW_XOFF(schq);
1779 rate_reg = (regbase == NIX_AF_TL3X_CIR(0) ||
1780 regbase == NIX_AF_TL3X_PIR(0));
1782 case NIX_TXSCH_LVL_TL4:
1783 md_debug0 = NIX_AF_TL4X_MD_DEBUG0(schq);
1784 sw_xoff = NIX_AF_TL4X_SW_XOFF(schq);
1786 rate_reg = (regbase == NIX_AF_TL4X_CIR(0) ||
1787 regbase == NIX_AF_TL4X_PIR(0));
1789 case NIX_TXSCH_LVL_MDQ:
1790 sw_xoff = NIX_AF_MDQX_SW_XOFF(schq);
1791 rate_reg = (regbase == NIX_AF_MDQX_CIR(0) ||
1792 regbase == NIX_AF_MDQX_PIR(0));
1799 /* Nothing special to do when state is not toggled */
1800 oldval = rvu_read64(rvu, blkaddr, reg);
1801 if ((oldval & 0x1) == (regval & 0x1)) {
1802 rvu_write64(rvu, blkaddr, reg, regval);
1806 /* PIR/CIR disable */
1807 if (!(regval & 0x1)) {
1808 rvu_write64(rvu, blkaddr, sw_xoff, 1);
1809 rvu_write64(rvu, blkaddr, reg, 0);
1811 rvu_write64(rvu, blkaddr, sw_xoff, 0);
1815 /* PIR/CIR enable */
1816 rvu_write64(rvu, blkaddr, sw_xoff, 1);
1818 poll_tmo = jiffies + usecs_to_jiffies(10000);
1819 /* Wait until VLD(bit32) == 1 or C_CON(bit48) == 0 */
1821 if (time_after(jiffies, poll_tmo)) {
1823 "NIXLF%d: TLX%u(lvl %u) CIR/PIR enable failed\n",
1828 dbgval = rvu_read64(rvu, blkaddr, md_debug0);
1829 } while (!(dbgval & BIT_ULL(32)) && (dbgval & BIT_ULL(48)));
1831 rvu_write64(rvu, blkaddr, reg, regval);
1833 rvu_write64(rvu, blkaddr, sw_xoff, 0);
1837 static void nix_reset_tx_schedule(struct rvu *rvu, int blkaddr,
1840 u64 tlx_parent = 0, tlx_schedule = 0;
1843 case NIX_TXSCH_LVL_TL2:
1844 tlx_parent = NIX_AF_TL2X_PARENT(schq);
1845 tlx_schedule = NIX_AF_TL2X_SCHEDULE(schq);
1847 case NIX_TXSCH_LVL_TL3:
1848 tlx_parent = NIX_AF_TL3X_PARENT(schq);
1849 tlx_schedule = NIX_AF_TL3X_SCHEDULE(schq);
1851 case NIX_TXSCH_LVL_TL4:
1852 tlx_parent = NIX_AF_TL4X_PARENT(schq);
1853 tlx_schedule = NIX_AF_TL4X_SCHEDULE(schq);
1855 case NIX_TXSCH_LVL_MDQ:
1856 /* no need to reset SMQ_CFG as HW clears this CSR
1859 tlx_parent = NIX_AF_MDQX_PARENT(schq);
1860 tlx_schedule = NIX_AF_MDQX_SCHEDULE(schq);
1867 rvu_write64(rvu, blkaddr, tlx_parent, 0x0);
1870 rvu_write64(rvu, blkaddr, tlx_schedule, 0x0);
1873 /* Disable shaping of pkts by a scheduler queue
1874 * at a given scheduler level.
1876 static void nix_reset_tx_shaping(struct rvu *rvu, int blkaddr,
1877 int nixlf, int lvl, int schq)
1879 struct rvu_hwinfo *hw = rvu->hw;
1880 u64 cir_reg = 0, pir_reg = 0;
1884 case NIX_TXSCH_LVL_TL1:
1885 cir_reg = NIX_AF_TL1X_CIR(schq);
1886 pir_reg = 0; /* PIR not available at TL1 */
1888 case NIX_TXSCH_LVL_TL2:
1889 cir_reg = NIX_AF_TL2X_CIR(schq);
1890 pir_reg = NIX_AF_TL2X_PIR(schq);
1892 case NIX_TXSCH_LVL_TL3:
1893 cir_reg = NIX_AF_TL3X_CIR(schq);
1894 pir_reg = NIX_AF_TL3X_PIR(schq);
1896 case NIX_TXSCH_LVL_TL4:
1897 cir_reg = NIX_AF_TL4X_CIR(schq);
1898 pir_reg = NIX_AF_TL4X_PIR(schq);
1900 case NIX_TXSCH_LVL_MDQ:
1901 cir_reg = NIX_AF_MDQX_CIR(schq);
1902 pir_reg = NIX_AF_MDQX_PIR(schq);
1906 /* Shaper state toggle needs wait/poll */
1907 if (hw->cap.nix_shaper_toggle_wait) {
1909 handle_txschq_shaper_update(rvu, blkaddr, nixlf,
1912 handle_txschq_shaper_update(rvu, blkaddr, nixlf,
1919 cfg = rvu_read64(rvu, blkaddr, cir_reg);
1920 rvu_write64(rvu, blkaddr, cir_reg, cfg & ~BIT_ULL(0));
1924 cfg = rvu_read64(rvu, blkaddr, pir_reg);
1925 rvu_write64(rvu, blkaddr, pir_reg, cfg & ~BIT_ULL(0));
1928 static void nix_reset_tx_linkcfg(struct rvu *rvu, int blkaddr,
1931 struct rvu_hwinfo *hw = rvu->hw;
1935 if (lvl >= hw->cap.nix_tx_aggr_lvl)
1938 /* Reset TL4's SDP link config */
1939 if (lvl == NIX_TXSCH_LVL_TL4)
1940 rvu_write64(rvu, blkaddr, NIX_AF_TL4X_SDP_LINK_CFG(schq), 0x00);
1942 link_level = rvu_read64(rvu, blkaddr, NIX_AF_PSE_CHANNEL_LEVEL) & 0x01 ?
1943 NIX_TXSCH_LVL_TL3 : NIX_TXSCH_LVL_TL2;
1944 if (lvl != link_level)
1947 /* Reset TL2's CGX or LBK link config */
1948 for (link = 0; link < (hw->cgx_links + hw->lbk_links); link++)
1949 rvu_write64(rvu, blkaddr,
1950 NIX_AF_TL3_TL2X_LINKX_CFG(schq, link), 0x00);
1953 static void nix_clear_tx_xoff(struct rvu *rvu, int blkaddr,
1956 struct rvu_hwinfo *hw = rvu->hw;
1959 /* Skip this if shaping is not supported */
1960 if (!hw->cap.nix_shaping)
1963 /* Clear level specific SW_XOFF */
1965 case NIX_TXSCH_LVL_TL1:
1966 reg = NIX_AF_TL1X_SW_XOFF(schq);
1968 case NIX_TXSCH_LVL_TL2:
1969 reg = NIX_AF_TL2X_SW_XOFF(schq);
1971 case NIX_TXSCH_LVL_TL3:
1972 reg = NIX_AF_TL3X_SW_XOFF(schq);
1974 case NIX_TXSCH_LVL_TL4:
1975 reg = NIX_AF_TL4X_SW_XOFF(schq);
1977 case NIX_TXSCH_LVL_MDQ:
1978 reg = NIX_AF_MDQX_SW_XOFF(schq);
1984 rvu_write64(rvu, blkaddr, reg, 0x0);
1987 static int nix_get_tx_link(struct rvu *rvu, u16 pcifunc)
1989 struct rvu_hwinfo *hw = rvu->hw;
1990 int pf = rvu_get_pf(pcifunc);
1991 u8 cgx_id = 0, lmac_id = 0;
1993 if (is_lbk_vf(rvu, pcifunc)) {/* LBK links */
1994 return hw->cgx_links;
1995 } else if (is_pf_cgxmapped(rvu, pf)) {
1996 rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
1997 return (cgx_id * hw->lmac_per_cgx) + lmac_id;
2001 return hw->cgx_links + hw->lbk_links;
2004 static void nix_get_txschq_range(struct rvu *rvu, u16 pcifunc,
2005 int link, int *start, int *end)
2007 struct rvu_hwinfo *hw = rvu->hw;
2008 int pf = rvu_get_pf(pcifunc);
2010 if (is_lbk_vf(rvu, pcifunc)) { /* LBK links */
2011 *start = hw->cap.nix_txsch_per_cgx_lmac * link;
2012 *end = *start + hw->cap.nix_txsch_per_lbk_lmac;
2013 } else if (is_pf_cgxmapped(rvu, pf)) { /* CGX links */
2014 *start = hw->cap.nix_txsch_per_cgx_lmac * link;
2015 *end = *start + hw->cap.nix_txsch_per_cgx_lmac;
2016 } else { /* SDP link */
2017 *start = (hw->cap.nix_txsch_per_cgx_lmac * hw->cgx_links) +
2018 (hw->cap.nix_txsch_per_lbk_lmac * hw->lbk_links);
2019 *end = *start + hw->cap.nix_txsch_per_sdp_lmac;
2023 static int nix_check_txschq_alloc_req(struct rvu *rvu, int lvl, u16 pcifunc,
2024 struct nix_hw *nix_hw,
2025 struct nix_txsch_alloc_req *req)
2027 struct rvu_hwinfo *hw = rvu->hw;
2028 int schq, req_schq, free_cnt;
2029 struct nix_txsch *txsch;
2030 int link, start, end;
2032 txsch = &nix_hw->txsch[lvl];
2033 req_schq = req->schq_contig[lvl] + req->schq[lvl];
2038 link = nix_get_tx_link(rvu, pcifunc);
2040 /* For traffic aggregating scheduler level, one queue is enough */
2041 if (lvl >= hw->cap.nix_tx_aggr_lvl) {
2043 return NIX_AF_ERR_TLX_ALLOC_FAIL;
2047 /* Get free SCHQ count and check if request can be accomodated */
2048 if (hw->cap.nix_fixed_txschq_mapping) {
2049 nix_get_txschq_range(rvu, pcifunc, link, &start, &end);
2050 schq = start + (pcifunc & RVU_PFVF_FUNC_MASK);
2051 if (end <= txsch->schq.max && schq < end &&
2052 !test_bit(schq, txsch->schq.bmap))
2057 free_cnt = rvu_rsrc_free_count(&txsch->schq);
2060 if (free_cnt < req_schq || req->schq[lvl] > MAX_TXSCHQ_PER_FUNC ||
2061 req->schq_contig[lvl] > MAX_TXSCHQ_PER_FUNC)
2062 return NIX_AF_ERR_TLX_ALLOC_FAIL;
2064 /* If contiguous queues are needed, check for availability */
2065 if (!hw->cap.nix_fixed_txschq_mapping && req->schq_contig[lvl] &&
2066 !rvu_rsrc_check_contig(&txsch->schq, req->schq_contig[lvl]))
2067 return NIX_AF_ERR_TLX_ALLOC_FAIL;
2072 static void nix_txsch_alloc(struct rvu *rvu, struct nix_txsch *txsch,
2073 struct nix_txsch_alloc_rsp *rsp,
2074 int lvl, int start, int end)
2076 struct rvu_hwinfo *hw = rvu->hw;
2077 u16 pcifunc = rsp->hdr.pcifunc;
2080 /* For traffic aggregating levels, queue alloc is based
2081 * on transmit link to which PF_FUNC is mapped to.
2083 if (lvl >= hw->cap.nix_tx_aggr_lvl) {
2084 /* A single TL queue is allocated */
2085 if (rsp->schq_contig[lvl]) {
2086 rsp->schq_contig[lvl] = 1;
2087 rsp->schq_contig_list[lvl][0] = start;
2090 /* Both contig and non-contig reqs doesn't make sense here */
2091 if (rsp->schq_contig[lvl])
2094 if (rsp->schq[lvl]) {
2096 rsp->schq_list[lvl][0] = start;
2101 /* Adjust the queue request count if HW supports
2102 * only one queue per level configuration.
2104 if (hw->cap.nix_fixed_txschq_mapping) {
2105 idx = pcifunc & RVU_PFVF_FUNC_MASK;
2107 if (idx >= (end - start) || test_bit(schq, txsch->schq.bmap)) {
2108 rsp->schq_contig[lvl] = 0;
2113 if (rsp->schq_contig[lvl]) {
2114 rsp->schq_contig[lvl] = 1;
2115 set_bit(schq, txsch->schq.bmap);
2116 rsp->schq_contig_list[lvl][0] = schq;
2118 } else if (rsp->schq[lvl]) {
2120 set_bit(schq, txsch->schq.bmap);
2121 rsp->schq_list[lvl][0] = schq;
2126 /* Allocate contiguous queue indices requesty first */
2127 if (rsp->schq_contig[lvl]) {
2128 schq = bitmap_find_next_zero_area(txsch->schq.bmap,
2129 txsch->schq.max, start,
2130 rsp->schq_contig[lvl], 0);
2132 rsp->schq_contig[lvl] = 0;
2133 for (idx = 0; idx < rsp->schq_contig[lvl]; idx++) {
2134 set_bit(schq, txsch->schq.bmap);
2135 rsp->schq_contig_list[lvl][idx] = schq;
2140 /* Allocate non-contiguous queue indices */
2141 if (rsp->schq[lvl]) {
2143 for (schq = start; schq < end; schq++) {
2144 if (!test_bit(schq, txsch->schq.bmap)) {
2145 set_bit(schq, txsch->schq.bmap);
2146 rsp->schq_list[lvl][idx++] = schq;
2148 if (idx == rsp->schq[lvl])
2151 /* Update how many were allocated */
2152 rsp->schq[lvl] = idx;
2156 int rvu_mbox_handler_nix_txsch_alloc(struct rvu *rvu,
2157 struct nix_txsch_alloc_req *req,
2158 struct nix_txsch_alloc_rsp *rsp)
2160 struct rvu_hwinfo *hw = rvu->hw;
2161 u16 pcifunc = req->hdr.pcifunc;
2162 int link, blkaddr, rc = 0;
2163 int lvl, idx, start, end;
2164 struct nix_txsch *txsch;
2165 struct nix_hw *nix_hw;
2170 rc = nix_get_nixlf(rvu, pcifunc, &nixlf, &blkaddr);
2174 nix_hw = get_nix_hw(rvu->hw, blkaddr);
2176 return NIX_AF_ERR_INVALID_NIXBLK;
2178 mutex_lock(&rvu->rsrc_lock);
2180 /* Check if request is valid as per HW capabilities
2181 * and can be accomodated.
2183 for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
2184 rc = nix_check_txschq_alloc_req(rvu, lvl, pcifunc, nix_hw, req);
2189 /* Allocate requested Tx scheduler queues */
2190 for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
2191 txsch = &nix_hw->txsch[lvl];
2192 pfvf_map = txsch->pfvf_map;
2194 if (!req->schq[lvl] && !req->schq_contig[lvl])
2197 rsp->schq[lvl] = req->schq[lvl];
2198 rsp->schq_contig[lvl] = req->schq_contig[lvl];
2200 link = nix_get_tx_link(rvu, pcifunc);
2202 if (lvl >= hw->cap.nix_tx_aggr_lvl) {
2205 } else if (hw->cap.nix_fixed_txschq_mapping) {
2206 nix_get_txschq_range(rvu, pcifunc, link, &start, &end);
2209 end = txsch->schq.max;
2212 nix_txsch_alloc(rvu, txsch, rsp, lvl, start, end);
2214 /* Reset queue config */
2215 for (idx = 0; idx < req->schq_contig[lvl]; idx++) {
2216 schq = rsp->schq_contig_list[lvl][idx];
2217 if (!(TXSCH_MAP_FLAGS(pfvf_map[schq]) &
2218 NIX_TXSCHQ_CFG_DONE))
2219 pfvf_map[schq] = TXSCH_MAP(pcifunc, 0);
2220 nix_reset_tx_linkcfg(rvu, blkaddr, lvl, schq);
2221 nix_reset_tx_shaping(rvu, blkaddr, nixlf, lvl, schq);
2222 nix_reset_tx_schedule(rvu, blkaddr, lvl, schq);
2225 for (idx = 0; idx < req->schq[lvl]; idx++) {
2226 schq = rsp->schq_list[lvl][idx];
2227 if (!(TXSCH_MAP_FLAGS(pfvf_map[schq]) &
2228 NIX_TXSCHQ_CFG_DONE))
2229 pfvf_map[schq] = TXSCH_MAP(pcifunc, 0);
2230 nix_reset_tx_linkcfg(rvu, blkaddr, lvl, schq);
2231 nix_reset_tx_shaping(rvu, blkaddr, nixlf, lvl, schq);
2232 nix_reset_tx_schedule(rvu, blkaddr, lvl, schq);
2236 rsp->aggr_level = hw->cap.nix_tx_aggr_lvl;
2237 rsp->aggr_lvl_rr_prio = TXSCH_TL1_DFLT_RR_PRIO;
2238 rsp->link_cfg_lvl = rvu_read64(rvu, blkaddr,
2239 NIX_AF_PSE_CHANNEL_LEVEL) & 0x01 ?
2240 NIX_TXSCH_LVL_TL3 : NIX_TXSCH_LVL_TL2;
2243 rc = NIX_AF_ERR_TLX_ALLOC_FAIL;
2245 mutex_unlock(&rvu->rsrc_lock);
2249 static void nix_smq_flush_fill_ctx(struct rvu *rvu, int blkaddr, int smq,
2250 struct nix_smq_flush_ctx *smq_flush_ctx)
2252 struct nix_smq_tree_ctx *smq_tree_ctx;
2253 u64 parent_off, regval;
2257 smq_flush_ctx->smq = smq;
2260 for (lvl = NIX_TXSCH_LVL_SMQ; lvl <= NIX_TXSCH_LVL_TL1; lvl++) {
2261 smq_tree_ctx = &smq_flush_ctx->smq_tree_ctx[lvl];
2262 if (lvl == NIX_TXSCH_LVL_TL1) {
2263 smq_flush_ctx->tl1_schq = schq;
2264 smq_tree_ctx->cir_off = NIX_AF_TL1X_CIR(schq);
2265 smq_tree_ctx->pir_off = 0;
2266 smq_tree_ctx->pir_val = 0;
2268 } else if (lvl == NIX_TXSCH_LVL_TL2) {
2269 smq_flush_ctx->tl2_schq = schq;
2270 smq_tree_ctx->cir_off = NIX_AF_TL2X_CIR(schq);
2271 smq_tree_ctx->pir_off = NIX_AF_TL2X_PIR(schq);
2272 parent_off = NIX_AF_TL2X_PARENT(schq);
2273 } else if (lvl == NIX_TXSCH_LVL_TL3) {
2274 smq_tree_ctx->cir_off = NIX_AF_TL3X_CIR(schq);
2275 smq_tree_ctx->pir_off = NIX_AF_TL3X_PIR(schq);
2276 parent_off = NIX_AF_TL3X_PARENT(schq);
2277 } else if (lvl == NIX_TXSCH_LVL_TL4) {
2278 smq_tree_ctx->cir_off = NIX_AF_TL4X_CIR(schq);
2279 smq_tree_ctx->pir_off = NIX_AF_TL4X_PIR(schq);
2280 parent_off = NIX_AF_TL4X_PARENT(schq);
2281 } else if (lvl == NIX_TXSCH_LVL_MDQ) {
2282 smq_tree_ctx->cir_off = NIX_AF_MDQX_CIR(schq);
2283 smq_tree_ctx->pir_off = NIX_AF_MDQX_PIR(schq);
2284 parent_off = NIX_AF_MDQX_PARENT(schq);
2286 /* save cir/pir register values */
2287 smq_tree_ctx->cir_val = rvu_read64(rvu, blkaddr, smq_tree_ctx->cir_off);
2288 if (smq_tree_ctx->pir_off)
2289 smq_tree_ctx->pir_val = rvu_read64(rvu, blkaddr, smq_tree_ctx->pir_off);
2291 /* get parent txsch node */
2293 regval = rvu_read64(rvu, blkaddr, parent_off);
2294 schq = (regval >> 16) & 0x1FF;
2299 static void nix_smq_flush_enadis_xoff(struct rvu *rvu, int blkaddr,
2300 struct nix_smq_flush_ctx *smq_flush_ctx, bool enable)
2302 struct nix_txsch *txsch;
2303 struct nix_hw *nix_hw;
2307 nix_hw = get_nix_hw(rvu->hw, blkaddr);
2311 /* loop through all TL2s with matching PF_FUNC */
2312 txsch = &nix_hw->txsch[NIX_TXSCH_LVL_TL2];
2313 for (tl2 = 0; tl2 < txsch->schq.max; tl2++) {
2314 /* skip the smq(flush) TL2 */
2315 if (tl2 == smq_flush_ctx->tl2_schq)
2317 /* skip unused TL2s */
2318 if (TXSCH_MAP_FLAGS(txsch->pfvf_map[tl2]) & NIX_TXSCHQ_FREE)
2320 /* skip if PF_FUNC doesn't match */
2321 if ((TXSCH_MAP_FUNC(txsch->pfvf_map[tl2]) & ~RVU_PFVF_FUNC_MASK) !=
2322 (TXSCH_MAP_FUNC(txsch->pfvf_map[smq_flush_ctx->tl2_schq] &
2323 ~RVU_PFVF_FUNC_MASK)))
2325 /* enable/disable XOFF */
2326 regoff = NIX_AF_TL2X_SW_XOFF(tl2);
2328 rvu_write64(rvu, blkaddr, regoff, 0x1);
2330 rvu_write64(rvu, blkaddr, regoff, 0x0);
2334 static void nix_smq_flush_enadis_rate(struct rvu *rvu, int blkaddr,
2335 struct nix_smq_flush_ctx *smq_flush_ctx, bool enable)
2337 u64 cir_off, pir_off, cir_val, pir_val;
2338 struct nix_smq_tree_ctx *smq_tree_ctx;
2341 for (lvl = NIX_TXSCH_LVL_SMQ; lvl <= NIX_TXSCH_LVL_TL1; lvl++) {
2342 smq_tree_ctx = &smq_flush_ctx->smq_tree_ctx[lvl];
2343 cir_off = smq_tree_ctx->cir_off;
2344 cir_val = smq_tree_ctx->cir_val;
2345 pir_off = smq_tree_ctx->pir_off;
2346 pir_val = smq_tree_ctx->pir_val;
2349 rvu_write64(rvu, blkaddr, cir_off, cir_val);
2350 if (lvl != NIX_TXSCH_LVL_TL1)
2351 rvu_write64(rvu, blkaddr, pir_off, pir_val);
2353 rvu_write64(rvu, blkaddr, cir_off, 0x0);
2354 if (lvl != NIX_TXSCH_LVL_TL1)
2355 rvu_write64(rvu, blkaddr, pir_off, 0x0);
2360 static int nix_smq_flush(struct rvu *rvu, int blkaddr,
2361 int smq, u16 pcifunc, int nixlf)
2363 struct nix_smq_flush_ctx *smq_flush_ctx;
2364 int pf = rvu_get_pf(pcifunc);
2365 u8 cgx_id = 0, lmac_id = 0;
2366 int err, restore_tx_en = 0;
2369 if (!is_rvu_otx2(rvu)) {
2370 /* Skip SMQ flush if pkt count is zero */
2371 cfg = rvu_read64(rvu, blkaddr, NIX_AF_MDQX_IN_MD_COUNT(smq));
2376 /* enable cgx tx if disabled */
2377 if (is_pf_cgxmapped(rvu, pf)) {
2378 rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
2379 restore_tx_en = !rvu_cgx_config_tx(rvu_cgx_pdata(cgx_id, rvu),
2383 /* XOFF all TL2s whose parent TL1 matches SMQ tree TL1 */
2384 smq_flush_ctx = kzalloc(sizeof(*smq_flush_ctx), GFP_KERNEL);
2387 nix_smq_flush_fill_ctx(rvu, blkaddr, smq, smq_flush_ctx);
2388 nix_smq_flush_enadis_xoff(rvu, blkaddr, smq_flush_ctx, true);
2389 nix_smq_flush_enadis_rate(rvu, blkaddr, smq_flush_ctx, false);
2391 cfg = rvu_read64(rvu, blkaddr, NIX_AF_SMQX_CFG(smq));
2392 /* Do SMQ flush and set enqueue xoff */
2393 cfg |= BIT_ULL(50) | BIT_ULL(49);
2394 rvu_write64(rvu, blkaddr, NIX_AF_SMQX_CFG(smq), cfg);
2396 /* Disable backpressure from physical link,
2397 * otherwise SMQ flush may stall.
2399 rvu_cgx_enadis_rx_bp(rvu, pf, false);
2401 /* Wait for flush to complete */
2402 err = rvu_poll_reg(rvu, blkaddr,
2403 NIX_AF_SMQX_CFG(smq), BIT_ULL(49), true);
2406 "NIXLF%d: SMQ%d flush failed, txlink might be busy\n",
2409 /* clear XOFF on TL2s */
2410 nix_smq_flush_enadis_rate(rvu, blkaddr, smq_flush_ctx, true);
2411 nix_smq_flush_enadis_xoff(rvu, blkaddr, smq_flush_ctx, false);
2412 kfree(smq_flush_ctx);
2414 rvu_cgx_enadis_rx_bp(rvu, pf, true);
2415 /* restore cgx tx state */
2417 rvu_cgx_config_tx(rvu_cgx_pdata(cgx_id, rvu), lmac_id, false);
2421 static int nix_txschq_free(struct rvu *rvu, u16 pcifunc)
2423 int blkaddr, nixlf, lvl, schq, err;
2424 struct rvu_hwinfo *hw = rvu->hw;
2425 struct nix_txsch *txsch;
2426 struct nix_hw *nix_hw;
2429 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
2431 return NIX_AF_ERR_AF_LF_INVALID;
2433 nix_hw = get_nix_hw(rvu->hw, blkaddr);
2435 return NIX_AF_ERR_INVALID_NIXBLK;
2437 nixlf = rvu_get_lf(rvu, &hw->block[blkaddr], pcifunc, 0);
2439 return NIX_AF_ERR_AF_LF_INVALID;
2441 /* Disable TL2/3 queue links and all XOFF's before SMQ flush*/
2442 mutex_lock(&rvu->rsrc_lock);
2443 for (lvl = NIX_TXSCH_LVL_MDQ; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
2444 txsch = &nix_hw->txsch[lvl];
2446 if (lvl >= hw->cap.nix_tx_aggr_lvl)
2449 for (schq = 0; schq < txsch->schq.max; schq++) {
2450 if (TXSCH_MAP_FUNC(txsch->pfvf_map[schq]) != pcifunc)
2452 nix_reset_tx_linkcfg(rvu, blkaddr, lvl, schq);
2453 nix_clear_tx_xoff(rvu, blkaddr, lvl, schq);
2454 nix_reset_tx_shaping(rvu, blkaddr, nixlf, lvl, schq);
2457 nix_clear_tx_xoff(rvu, blkaddr, NIX_TXSCH_LVL_TL1,
2458 nix_get_tx_link(rvu, pcifunc));
2460 /* On PF cleanup, clear cfg done flag as
2461 * PF would have changed default config.
2463 if (!(pcifunc & RVU_PFVF_FUNC_MASK)) {
2464 txsch = &nix_hw->txsch[NIX_TXSCH_LVL_TL1];
2465 schq = nix_get_tx_link(rvu, pcifunc);
2466 /* Do not clear pcifunc in txsch->pfvf_map[schq] because
2467 * VF might be using this TL1 queue
2469 map_func = TXSCH_MAP_FUNC(txsch->pfvf_map[schq]);
2470 txsch->pfvf_map[schq] = TXSCH_SET_FLAG(map_func, 0x0);
2474 txsch = &nix_hw->txsch[NIX_TXSCH_LVL_SMQ];
2475 for (schq = 0; schq < txsch->schq.max; schq++) {
2476 if (TXSCH_MAP_FUNC(txsch->pfvf_map[schq]) != pcifunc)
2478 nix_smq_flush(rvu, blkaddr, schq, pcifunc, nixlf);
2481 /* Now free scheduler queues to free pool */
2482 for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
2483 /* TLs above aggregation level are shared across all PF
2484 * and it's VFs, hence skip freeing them.
2486 if (lvl >= hw->cap.nix_tx_aggr_lvl)
2489 txsch = &nix_hw->txsch[lvl];
2490 for (schq = 0; schq < txsch->schq.max; schq++) {
2491 if (TXSCH_MAP_FUNC(txsch->pfvf_map[schq]) != pcifunc)
2493 nix_reset_tx_schedule(rvu, blkaddr, lvl, schq);
2494 rvu_free_rsrc(&txsch->schq, schq);
2495 txsch->pfvf_map[schq] = TXSCH_MAP(0, NIX_TXSCHQ_FREE);
2498 mutex_unlock(&rvu->rsrc_lock);
2500 /* Sync cached info for this LF in NDC-TX to LLC/DRAM */
2501 rvu_write64(rvu, blkaddr, NIX_AF_NDC_TX_SYNC, BIT_ULL(12) | nixlf);
2502 err = rvu_poll_reg(rvu, blkaddr, NIX_AF_NDC_TX_SYNC, BIT_ULL(12), true);
2504 dev_err(rvu->dev, "NDC-TX sync failed for NIXLF %d\n", nixlf);
2509 static int nix_txschq_free_one(struct rvu *rvu,
2510 struct nix_txsch_free_req *req)
2512 struct rvu_hwinfo *hw = rvu->hw;
2513 u16 pcifunc = req->hdr.pcifunc;
2514 int lvl, schq, nixlf, blkaddr;
2515 struct nix_txsch *txsch;
2516 struct nix_hw *nix_hw;
2520 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
2522 return NIX_AF_ERR_AF_LF_INVALID;
2524 nix_hw = get_nix_hw(rvu->hw, blkaddr);
2526 return NIX_AF_ERR_INVALID_NIXBLK;
2528 nixlf = rvu_get_lf(rvu, &hw->block[blkaddr], pcifunc, 0);
2530 return NIX_AF_ERR_AF_LF_INVALID;
2532 lvl = req->schq_lvl;
2534 txsch = &nix_hw->txsch[lvl];
2536 if (lvl >= hw->cap.nix_tx_aggr_lvl || schq >= txsch->schq.max)
2539 pfvf_map = txsch->pfvf_map;
2540 mutex_lock(&rvu->rsrc_lock);
2542 if (TXSCH_MAP_FUNC(pfvf_map[schq]) != pcifunc) {
2543 rc = NIX_AF_ERR_TLX_INVALID;
2547 /* Clear SW_XOFF of this resource only.
2548 * For SMQ level, all path XOFF's
2549 * need to be made clear by user
2551 nix_clear_tx_xoff(rvu, blkaddr, lvl, schq);
2553 nix_reset_tx_linkcfg(rvu, blkaddr, lvl, schq);
2554 nix_reset_tx_shaping(rvu, blkaddr, nixlf, lvl, schq);
2556 /* Flush if it is a SMQ. Onus of disabling
2557 * TL2/3 queue links before SMQ flush is on user
2559 if (lvl == NIX_TXSCH_LVL_SMQ &&
2560 nix_smq_flush(rvu, blkaddr, schq, pcifunc, nixlf)) {
2561 rc = NIX_AF_SMQ_FLUSH_FAILED;
2565 nix_reset_tx_schedule(rvu, blkaddr, lvl, schq);
2567 /* Free the resource */
2568 rvu_free_rsrc(&txsch->schq, schq);
2569 txsch->pfvf_map[schq] = TXSCH_MAP(0, NIX_TXSCHQ_FREE);
2570 mutex_unlock(&rvu->rsrc_lock);
2573 mutex_unlock(&rvu->rsrc_lock);
2577 int rvu_mbox_handler_nix_txsch_free(struct rvu *rvu,
2578 struct nix_txsch_free_req *req,
2579 struct msg_rsp *rsp)
2581 if (req->flags & TXSCHQ_FREE_ALL)
2582 return nix_txschq_free(rvu, req->hdr.pcifunc);
2584 return nix_txschq_free_one(rvu, req);
2587 static bool is_txschq_hierarchy_valid(struct rvu *rvu, u16 pcifunc, int blkaddr,
2588 int lvl, u64 reg, u64 regval)
2590 u64 regbase = reg & 0xFFFF;
2593 if (!rvu_check_valid_reg(TXSCHQ_HWREGMAP, lvl, reg))
2596 schq = TXSCHQ_IDX(reg, TXSCHQ_IDX_SHIFT);
2597 /* Check if this schq belongs to this PF/VF or not */
2598 if (!is_valid_txschq(rvu, blkaddr, lvl, pcifunc, schq))
2601 parent = (regval >> 16) & 0x1FF;
2602 /* Validate MDQ's TL4 parent */
2603 if (regbase == NIX_AF_MDQX_PARENT(0) &&
2604 !is_valid_txschq(rvu, blkaddr, NIX_TXSCH_LVL_TL4, pcifunc, parent))
2607 /* Validate TL4's TL3 parent */
2608 if (regbase == NIX_AF_TL4X_PARENT(0) &&
2609 !is_valid_txschq(rvu, blkaddr, NIX_TXSCH_LVL_TL3, pcifunc, parent))
2612 /* Validate TL3's TL2 parent */
2613 if (regbase == NIX_AF_TL3X_PARENT(0) &&
2614 !is_valid_txschq(rvu, blkaddr, NIX_TXSCH_LVL_TL2, pcifunc, parent))
2617 /* Validate TL2's TL1 parent */
2618 if (regbase == NIX_AF_TL2X_PARENT(0) &&
2619 !is_valid_txschq(rvu, blkaddr, NIX_TXSCH_LVL_TL1, pcifunc, parent))
2625 static bool is_txschq_shaping_valid(struct rvu_hwinfo *hw, int lvl, u64 reg)
2629 if (hw->cap.nix_shaping)
2632 /* If shaping and coloring is not supported, then
2633 * *_CIR and *_PIR registers should not be configured.
2635 regbase = reg & 0xFFFF;
2638 case NIX_TXSCH_LVL_TL1:
2639 if (regbase == NIX_AF_TL1X_CIR(0))
2642 case NIX_TXSCH_LVL_TL2:
2643 if (regbase == NIX_AF_TL2X_CIR(0) ||
2644 regbase == NIX_AF_TL2X_PIR(0))
2647 case NIX_TXSCH_LVL_TL3:
2648 if (regbase == NIX_AF_TL3X_CIR(0) ||
2649 regbase == NIX_AF_TL3X_PIR(0))
2652 case NIX_TXSCH_LVL_TL4:
2653 if (regbase == NIX_AF_TL4X_CIR(0) ||
2654 regbase == NIX_AF_TL4X_PIR(0))
2657 case NIX_TXSCH_LVL_MDQ:
2658 if (regbase == NIX_AF_MDQX_CIR(0) ||
2659 regbase == NIX_AF_MDQX_PIR(0))
2666 static void nix_tl1_default_cfg(struct rvu *rvu, struct nix_hw *nix_hw,
2667 u16 pcifunc, int blkaddr)
2672 schq = nix_get_tx_link(rvu, pcifunc);
2673 pfvf_map = nix_hw->txsch[NIX_TXSCH_LVL_TL1].pfvf_map;
2674 /* Skip if PF has already done the config */
2675 if (TXSCH_MAP_FLAGS(pfvf_map[schq]) & NIX_TXSCHQ_CFG_DONE)
2677 rvu_write64(rvu, blkaddr, NIX_AF_TL1X_TOPOLOGY(schq),
2678 (TXSCH_TL1_DFLT_RR_PRIO << 1));
2680 /* On OcteonTx2 the config was in bytes and newer silcons
2681 * it's changed to weight.
2683 if (!rvu->hw->cap.nix_common_dwrr_mtu)
2684 rvu_write64(rvu, blkaddr, NIX_AF_TL1X_SCHEDULE(schq),
2685 TXSCH_TL1_DFLT_RR_QTM);
2687 rvu_write64(rvu, blkaddr, NIX_AF_TL1X_SCHEDULE(schq),
2688 CN10K_MAX_DWRR_WEIGHT);
2690 rvu_write64(rvu, blkaddr, NIX_AF_TL1X_CIR(schq), 0x00);
2691 pfvf_map[schq] = TXSCH_SET_FLAG(pfvf_map[schq], NIX_TXSCHQ_CFG_DONE);
2694 /* Register offset - [15:0]
2695 * Scheduler Queue number - [25:16]
2697 #define NIX_TX_SCHQ_MASK GENMASK_ULL(25, 0)
2699 static int nix_txschq_cfg_read(struct rvu *rvu, struct nix_hw *nix_hw,
2700 int blkaddr, struct nix_txschq_config *req,
2701 struct nix_txschq_config *rsp)
2703 u16 pcifunc = req->hdr.pcifunc;
2707 for (idx = 0; idx < req->num_regs; idx++) {
2708 reg = req->reg[idx];
2709 reg &= NIX_TX_SCHQ_MASK;
2710 schq = TXSCHQ_IDX(reg, TXSCHQ_IDX_SHIFT);
2711 if (!rvu_check_valid_reg(TXSCHQ_HWREGMAP, req->lvl, reg) ||
2712 !is_valid_txschq(rvu, blkaddr, req->lvl, pcifunc, schq))
2713 return NIX_AF_INVAL_TXSCHQ_CFG;
2714 rsp->regval[idx] = rvu_read64(rvu, blkaddr, reg);
2716 rsp->lvl = req->lvl;
2717 rsp->num_regs = req->num_regs;
2721 void rvu_nix_tx_tl2_cfg(struct rvu *rvu, int blkaddr, u16 pcifunc,
2722 struct nix_txsch *txsch, bool enable)
2724 struct rvu_hwinfo *hw = rvu->hw;
2725 int lbk_link_start, lbk_links;
2726 u8 pf = rvu_get_pf(pcifunc);
2730 if (!is_pf_cgxmapped(rvu, pf))
2733 cfg = enable ? (BIT_ULL(12) | RVU_SWITCH_LBK_CHAN) : 0;
2734 lbk_link_start = hw->cgx_links;
2736 for (schq = 0; schq < txsch->schq.max; schq++) {
2737 if (TXSCH_MAP_FUNC(txsch->pfvf_map[schq]) != pcifunc)
2739 /* Enable all LBK links with channel 63 by default so that
2740 * packets can be sent to LBK with a NPC TX MCAM rule
2742 lbk_links = hw->lbk_links;
2744 rvu_write64(rvu, blkaddr,
2745 NIX_AF_TL3_TL2X_LINKX_CFG(schq,
2751 int rvu_mbox_handler_nix_txschq_cfg(struct rvu *rvu,
2752 struct nix_txschq_config *req,
2753 struct nix_txschq_config *rsp)
2755 u64 reg, val, regval, schq_regbase, val_mask;
2756 struct rvu_hwinfo *hw = rvu->hw;
2757 u16 pcifunc = req->hdr.pcifunc;
2758 struct nix_txsch *txsch;
2759 struct nix_hw *nix_hw;
2760 int blkaddr, idx, err;
2764 if (req->lvl >= NIX_TXSCH_LVL_CNT ||
2765 req->num_regs > MAX_REGS_PER_MBOX_MSG)
2766 return NIX_AF_INVAL_TXSCHQ_CFG;
2768 err = nix_get_nixlf(rvu, pcifunc, &nixlf, &blkaddr);
2772 nix_hw = get_nix_hw(rvu->hw, blkaddr);
2774 return NIX_AF_ERR_INVALID_NIXBLK;
2777 return nix_txschq_cfg_read(rvu, nix_hw, blkaddr, req, rsp);
2779 txsch = &nix_hw->txsch[req->lvl];
2780 pfvf_map = txsch->pfvf_map;
2782 if (req->lvl >= hw->cap.nix_tx_aggr_lvl &&
2783 pcifunc & RVU_PFVF_FUNC_MASK) {
2784 mutex_lock(&rvu->rsrc_lock);
2785 if (req->lvl == NIX_TXSCH_LVL_TL1)
2786 nix_tl1_default_cfg(rvu, nix_hw, pcifunc, blkaddr);
2787 mutex_unlock(&rvu->rsrc_lock);
2791 for (idx = 0; idx < req->num_regs; idx++) {
2792 reg = req->reg[idx];
2793 reg &= NIX_TX_SCHQ_MASK;
2794 regval = req->regval[idx];
2795 schq_regbase = reg & 0xFFFF;
2796 val_mask = req->regval_mask[idx];
2798 if (!is_txschq_hierarchy_valid(rvu, pcifunc, blkaddr,
2799 txsch->lvl, reg, regval))
2800 return NIX_AF_INVAL_TXSCHQ_CFG;
2802 /* Check if shaping and coloring is supported */
2803 if (!is_txschq_shaping_valid(hw, req->lvl, reg))
2806 val = rvu_read64(rvu, blkaddr, reg);
2807 regval = (val & val_mask) | (regval & ~val_mask);
2809 /* Handle shaping state toggle specially */
2810 if (hw->cap.nix_shaper_toggle_wait &&
2811 handle_txschq_shaper_update(rvu, blkaddr, nixlf,
2812 req->lvl, reg, regval))
2815 /* Replace PF/VF visible NIXLF slot with HW NIXLF id */
2816 if (schq_regbase == NIX_AF_SMQX_CFG(0)) {
2817 nixlf = rvu_get_lf(rvu, &hw->block[blkaddr],
2819 regval &= ~(0x7FULL << 24);
2820 regval |= ((u64)nixlf << 24);
2823 /* Clear 'BP_ENA' config, if it's not allowed */
2824 if (!hw->cap.nix_tx_link_bp) {
2825 if (schq_regbase == NIX_AF_TL4X_SDP_LINK_CFG(0) ||
2826 (schq_regbase & 0xFF00) ==
2827 NIX_AF_TL3_TL2X_LINKX_CFG(0, 0))
2828 regval &= ~BIT_ULL(13);
2831 /* Mark config as done for TL1 by PF */
2832 if (schq_regbase >= NIX_AF_TL1X_SCHEDULE(0) &&
2833 schq_regbase <= NIX_AF_TL1X_GREEN_BYTES(0)) {
2834 schq = TXSCHQ_IDX(reg, TXSCHQ_IDX_SHIFT);
2835 mutex_lock(&rvu->rsrc_lock);
2836 pfvf_map[schq] = TXSCH_SET_FLAG(pfvf_map[schq],
2837 NIX_TXSCHQ_CFG_DONE);
2838 mutex_unlock(&rvu->rsrc_lock);
2841 /* SMQ flush is special hence split register writes such
2842 * that flush first and write rest of the bits later.
2844 if (schq_regbase == NIX_AF_SMQX_CFG(0) &&
2845 (regval & BIT_ULL(49))) {
2846 schq = TXSCHQ_IDX(reg, TXSCHQ_IDX_SHIFT);
2847 nix_smq_flush(rvu, blkaddr, schq, pcifunc, nixlf);
2848 regval &= ~BIT_ULL(49);
2850 rvu_write64(rvu, blkaddr, reg, regval);
2856 static int nix_rx_vtag_cfg(struct rvu *rvu, int nixlf, int blkaddr,
2857 struct nix_vtag_config *req)
2859 u64 regval = req->vtag_size;
2861 if (req->rx.vtag_type > NIX_AF_LFX_RX_VTAG_TYPE7 ||
2862 req->vtag_size > VTAGSIZE_T8)
2865 /* RX VTAG Type 7 reserved for vf vlan */
2866 if (req->rx.vtag_type == NIX_AF_LFX_RX_VTAG_TYPE7)
2867 return NIX_AF_ERR_RX_VTAG_INUSE;
2869 if (req->rx.capture_vtag)
2870 regval |= BIT_ULL(5);
2871 if (req->rx.strip_vtag)
2872 regval |= BIT_ULL(4);
2874 rvu_write64(rvu, blkaddr,
2875 NIX_AF_LFX_RX_VTAG_TYPEX(nixlf, req->rx.vtag_type), regval);
2879 static int nix_tx_vtag_free(struct rvu *rvu, int blkaddr,
2880 u16 pcifunc, int index)
2882 struct nix_hw *nix_hw = get_nix_hw(rvu->hw, blkaddr);
2883 struct nix_txvlan *vlan;
2886 return NIX_AF_ERR_INVALID_NIXBLK;
2888 vlan = &nix_hw->txvlan;
2889 if (vlan->entry2pfvf_map[index] != pcifunc)
2890 return NIX_AF_ERR_PARAM;
2892 rvu_write64(rvu, blkaddr,
2893 NIX_AF_TX_VTAG_DEFX_DATA(index), 0x0ull);
2894 rvu_write64(rvu, blkaddr,
2895 NIX_AF_TX_VTAG_DEFX_CTL(index), 0x0ull);
2897 vlan->entry2pfvf_map[index] = 0;
2898 rvu_free_rsrc(&vlan->rsrc, index);
2903 static void nix_free_tx_vtag_entries(struct rvu *rvu, u16 pcifunc)
2905 struct nix_txvlan *vlan;
2906 struct nix_hw *nix_hw;
2909 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
2913 nix_hw = get_nix_hw(rvu->hw, blkaddr);
2917 vlan = &nix_hw->txvlan;
2919 mutex_lock(&vlan->rsrc_lock);
2920 /* Scan all the entries and free the ones mapped to 'pcifunc' */
2921 for (index = 0; index < vlan->rsrc.max; index++) {
2922 if (vlan->entry2pfvf_map[index] == pcifunc)
2923 nix_tx_vtag_free(rvu, blkaddr, pcifunc, index);
2925 mutex_unlock(&vlan->rsrc_lock);
2928 static int nix_tx_vtag_alloc(struct rvu *rvu, int blkaddr,
2931 struct nix_hw *nix_hw = get_nix_hw(rvu->hw, blkaddr);
2932 struct nix_txvlan *vlan;
2937 return NIX_AF_ERR_INVALID_NIXBLK;
2939 vlan = &nix_hw->txvlan;
2941 mutex_lock(&vlan->rsrc_lock);
2943 index = rvu_alloc_rsrc(&vlan->rsrc);
2945 mutex_unlock(&vlan->rsrc_lock);
2949 mutex_unlock(&vlan->rsrc_lock);
2951 regval = size ? vtag : vtag << 32;
2953 rvu_write64(rvu, blkaddr,
2954 NIX_AF_TX_VTAG_DEFX_DATA(index), regval);
2955 rvu_write64(rvu, blkaddr,
2956 NIX_AF_TX_VTAG_DEFX_CTL(index), size);
2961 static int nix_tx_vtag_decfg(struct rvu *rvu, int blkaddr,
2962 struct nix_vtag_config *req)
2964 struct nix_hw *nix_hw = get_nix_hw(rvu->hw, blkaddr);
2965 u16 pcifunc = req->hdr.pcifunc;
2966 int idx0 = req->tx.vtag0_idx;
2967 int idx1 = req->tx.vtag1_idx;
2968 struct nix_txvlan *vlan;
2972 return NIX_AF_ERR_INVALID_NIXBLK;
2974 vlan = &nix_hw->txvlan;
2975 if (req->tx.free_vtag0 && req->tx.free_vtag1)
2976 if (vlan->entry2pfvf_map[idx0] != pcifunc ||
2977 vlan->entry2pfvf_map[idx1] != pcifunc)
2978 return NIX_AF_ERR_PARAM;
2980 mutex_lock(&vlan->rsrc_lock);
2982 if (req->tx.free_vtag0) {
2983 err = nix_tx_vtag_free(rvu, blkaddr, pcifunc, idx0);
2988 if (req->tx.free_vtag1)
2989 err = nix_tx_vtag_free(rvu, blkaddr, pcifunc, idx1);
2992 mutex_unlock(&vlan->rsrc_lock);
2996 static int nix_tx_vtag_cfg(struct rvu *rvu, int blkaddr,
2997 struct nix_vtag_config *req,
2998 struct nix_vtag_config_rsp *rsp)
3000 struct nix_hw *nix_hw = get_nix_hw(rvu->hw, blkaddr);
3001 struct nix_txvlan *vlan;
3002 u16 pcifunc = req->hdr.pcifunc;
3005 return NIX_AF_ERR_INVALID_NIXBLK;
3007 vlan = &nix_hw->txvlan;
3008 if (req->tx.cfg_vtag0) {
3010 nix_tx_vtag_alloc(rvu, blkaddr,
3011 req->tx.vtag0, req->vtag_size);
3013 if (rsp->vtag0_idx < 0)
3014 return NIX_AF_ERR_TX_VTAG_NOSPC;
3016 vlan->entry2pfvf_map[rsp->vtag0_idx] = pcifunc;
3019 if (req->tx.cfg_vtag1) {
3021 nix_tx_vtag_alloc(rvu, blkaddr,
3022 req->tx.vtag1, req->vtag_size);
3024 if (rsp->vtag1_idx < 0)
3027 vlan->entry2pfvf_map[rsp->vtag1_idx] = pcifunc;
3033 if (req->tx.cfg_vtag0)
3034 nix_tx_vtag_free(rvu, blkaddr, pcifunc, rsp->vtag0_idx);
3036 return NIX_AF_ERR_TX_VTAG_NOSPC;
3039 int rvu_mbox_handler_nix_vtag_cfg(struct rvu *rvu,
3040 struct nix_vtag_config *req,
3041 struct nix_vtag_config_rsp *rsp)
3043 u16 pcifunc = req->hdr.pcifunc;
3044 int blkaddr, nixlf, err;
3046 err = nix_get_nixlf(rvu, pcifunc, &nixlf, &blkaddr);
3050 if (req->cfg_type) {
3051 /* rx vtag configuration */
3052 err = nix_rx_vtag_cfg(rvu, nixlf, blkaddr, req);
3054 return NIX_AF_ERR_PARAM;
3056 /* tx vtag configuration */
3057 if ((req->tx.cfg_vtag0 || req->tx.cfg_vtag1) &&
3058 (req->tx.free_vtag0 || req->tx.free_vtag1))
3059 return NIX_AF_ERR_PARAM;
3061 if (req->tx.cfg_vtag0 || req->tx.cfg_vtag1)
3062 return nix_tx_vtag_cfg(rvu, blkaddr, req, rsp);
3064 if (req->tx.free_vtag0 || req->tx.free_vtag1)
3065 return nix_tx_vtag_decfg(rvu, blkaddr, req);
3071 static int nix_blk_setup_mce(struct rvu *rvu, struct nix_hw *nix_hw,
3072 int mce, u8 op, u16 pcifunc, int next,
3073 int index, u8 mce_op, bool eol)
3075 struct nix_aq_enq_req aq_req;
3078 aq_req.hdr.pcifunc = 0;
3079 aq_req.ctype = NIX_AQ_CTYPE_MCE;
3083 /* Use RSS with RSS index 0 */
3084 aq_req.mce.op = mce_op;
3085 aq_req.mce.index = index;
3086 aq_req.mce.eol = eol;
3087 aq_req.mce.pf_func = pcifunc;
3088 aq_req.mce.next = next;
3090 /* All fields valid */
3091 *(u64 *)(&aq_req.mce_mask) = ~0ULL;
3093 err = rvu_nix_blk_aq_enq_inst(rvu, nix_hw, &aq_req, NULL);
3095 dev_err(rvu->dev, "Failed to setup Bcast MCE for PF%d:VF%d\n",
3096 rvu_get_pf(pcifunc), pcifunc & RVU_PFVF_FUNC_MASK);
3102 static void nix_delete_mcast_mce_list(struct nix_mce_list *mce_list)
3104 struct hlist_node *tmp;
3107 /* Scan through the current list */
3108 hlist_for_each_entry_safe(mce, tmp, &mce_list->head, node) {
3109 hlist_del(&mce->node);
3113 mce_list->count = 0;
3117 static int nix_get_last_mce_list_index(struct nix_mcast_grp_elem *elem)
3119 return elem->mce_start_index + elem->mcast_mce_list.count - 1;
3122 static int nix_update_ingress_mce_list_hw(struct rvu *rvu,
3123 struct nix_hw *nix_hw,
3124 struct nix_mcast_grp_elem *elem)
3126 int idx, last_idx, next_idx, err;
3127 struct nix_mce_list *mce_list;
3128 struct mce *mce, *prev_mce;
3130 mce_list = &elem->mcast_mce_list;
3131 idx = elem->mce_start_index;
3132 last_idx = nix_get_last_mce_list_index(elem);
3133 hlist_for_each_entry(mce, &mce_list->head, node) {
3137 if (!mce->is_active) {
3138 if (idx == elem->mce_start_index) {
3141 elem->mce_start_index = idx;
3143 } else if (idx == last_idx) {
3144 err = nix_blk_setup_mce(rvu, nix_hw, idx - 1, NIX_AQ_INSTOP_WRITE,
3145 prev_mce->pcifunc, next_idx,
3146 prev_mce->rq_rss_index,
3147 prev_mce->dest_type,
3157 /* EOL should be set in last MCE */
3158 err = nix_blk_setup_mce(rvu, nix_hw, idx, NIX_AQ_INSTOP_WRITE,
3159 mce->pcifunc, next_idx,
3160 mce->rq_rss_index, mce->dest_type,
3161 (next_idx > last_idx) ? true : false);
3172 static void nix_update_egress_mce_list_hw(struct rvu *rvu,
3173 struct nix_hw *nix_hw,
3174 struct nix_mcast_grp_elem *elem)
3176 struct nix_mce_list *mce_list;
3177 int idx, last_idx, next_idx;
3178 struct mce *mce, *prev_mce;
3182 mce_list = &elem->mcast_mce_list;
3183 idx = elem->mce_start_index;
3184 last_idx = nix_get_last_mce_list_index(elem);
3185 hlist_for_each_entry(mce, &mce_list->head, node) {
3189 if (!mce->is_active) {
3190 if (idx == elem->mce_start_index) {
3193 elem->mce_start_index = idx;
3195 } else if (idx == last_idx) {
3196 regval = (next_idx << 16) | (1 << 12) | prev_mce->channel;
3197 rvu_write64(rvu, nix_hw->blkaddr,
3198 NIX_AF_TX_MCASTX(idx - 1),
3206 /* EOL should be set in last MCE */
3207 if (next_idx > last_idx)
3210 regval = (next_idx << 16) | (eol << 12) | mce->channel;
3211 rvu_write64(rvu, nix_hw->blkaddr,
3212 NIX_AF_TX_MCASTX(idx),
3219 static int nix_del_mce_list_entry(struct rvu *rvu,
3220 struct nix_hw *nix_hw,
3221 struct nix_mcast_grp_elem *elem,
3222 struct nix_mcast_grp_update_req *req)
3224 u32 num_entry = req->num_mce_entry;
3225 struct nix_mce_list *mce_list;
3230 mce_list = &elem->mcast_mce_list;
3231 for (i = 0; i < num_entry; i++) {
3233 hlist_for_each_entry(mce, &mce_list->head, node) {
3234 /* If already exists, then delete */
3235 if (mce->pcifunc == req->pcifunc[i]) {
3236 hlist_del(&mce->node);
3245 return NIX_AF_ERR_INVALID_MCAST_DEL_REQ;
3248 mce_list->max = mce_list->count;
3249 /* Dump the updated list to HW */
3250 if (elem->dir == NIX_MCAST_INGRESS)
3251 return nix_update_ingress_mce_list_hw(rvu, nix_hw, elem);
3253 nix_update_egress_mce_list_hw(rvu, nix_hw, elem);
3257 static int nix_add_mce_list_entry(struct rvu *rvu,
3258 struct nix_hw *nix_hw,
3259 struct nix_mcast_grp_elem *elem,
3260 struct nix_mcast_grp_update_req *req)
3262 u32 num_entry = req->num_mce_entry;
3263 struct nix_mce_list *mce_list;
3264 struct hlist_node *tmp;
3268 mce_list = &elem->mcast_mce_list;
3269 for (i = 0; i < num_entry; i++) {
3270 mce = kzalloc(sizeof(*mce), GFP_KERNEL);
3274 mce->pcifunc = req->pcifunc[i];
3275 mce->channel = req->channel[i];
3276 mce->rq_rss_index = req->rq_rss_index[i];
3277 mce->dest_type = req->dest_type[i];
3279 hlist_add_head(&mce->node, &mce_list->head);
3283 mce_list->max += num_entry;
3285 /* Dump the updated list to HW */
3286 if (elem->dir == NIX_MCAST_INGRESS)
3287 return nix_update_ingress_mce_list_hw(rvu, nix_hw, elem);
3289 nix_update_egress_mce_list_hw(rvu, nix_hw, elem);
3293 hlist_for_each_entry_safe(mce, tmp, &mce_list->head, node) {
3294 hlist_del(&mce->node);
3302 static int nix_update_mce_list_entry(struct nix_mce_list *mce_list,
3303 u16 pcifunc, bool add)
3305 struct mce *mce, *tail = NULL;
3306 bool delete = false;
3308 /* Scan through the current list */
3309 hlist_for_each_entry(mce, &mce_list->head, node) {
3310 /* If already exists, then delete */
3311 if (mce->pcifunc == pcifunc && !add) {
3314 } else if (mce->pcifunc == pcifunc && add) {
3315 /* entry already exists */
3322 hlist_del(&mce->node);
3331 /* Add a new one to the list, at the tail */
3332 mce = kzalloc(sizeof(*mce), GFP_KERNEL);
3335 mce->pcifunc = pcifunc;
3337 hlist_add_head(&mce->node, &mce_list->head);
3339 hlist_add_behind(&mce->node, &tail->node);
3344 int nix_update_mce_list(struct rvu *rvu, u16 pcifunc,
3345 struct nix_mce_list *mce_list,
3346 int mce_idx, int mcam_index, bool add)
3348 int err = 0, idx, next_idx, last_idx, blkaddr, npc_blkaddr;
3349 struct npc_mcam *mcam = &rvu->hw->mcam;
3350 struct nix_mcast *mcast;
3351 struct nix_hw *nix_hw;
3357 /* Get this PF/VF func's MCE index */
3358 idx = mce_idx + (pcifunc & RVU_PFVF_FUNC_MASK);
3360 if (idx > (mce_idx + mce_list->max)) {
3362 "%s: Idx %d > max MCE idx %d, for PF%d bcast list\n",
3363 __func__, idx, mce_list->max,
3364 pcifunc >> RVU_PFVF_PF_SHIFT);
3368 err = nix_get_struct_ptrs(rvu, pcifunc, &nix_hw, &blkaddr);
3372 mcast = &nix_hw->mcast;
3373 mutex_lock(&mcast->mce_lock);
3375 err = nix_update_mce_list_entry(mce_list, pcifunc, add);
3379 /* Disable MCAM entry in NPC */
3380 if (!mce_list->count) {
3381 npc_blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
3382 npc_enable_mcam_entry(rvu, mcam, npc_blkaddr, mcam_index, false);
3386 /* Dump the updated list to HW */
3388 last_idx = idx + mce_list->count - 1;
3389 hlist_for_each_entry(mce, &mce_list->head, node) {
3394 /* EOL should be set in last MCE */
3395 err = nix_blk_setup_mce(rvu, nix_hw, idx, NIX_AQ_INSTOP_WRITE,
3396 mce->pcifunc, next_idx,
3398 (next_idx > last_idx) ? true : false);
3405 mutex_unlock(&mcast->mce_lock);
3409 void nix_get_mce_list(struct rvu *rvu, u16 pcifunc, int type,
3410 struct nix_mce_list **mce_list, int *mce_idx)
3412 struct rvu_hwinfo *hw = rvu->hw;
3413 struct rvu_pfvf *pfvf;
3415 if (!hw->cap.nix_rx_multicast ||
3416 !is_pf_cgxmapped(rvu, rvu_get_pf(pcifunc & ~RVU_PFVF_FUNC_MASK))) {
3422 /* Get this PF/VF func's MCE index */
3423 pfvf = rvu_get_pfvf(rvu, pcifunc & ~RVU_PFVF_FUNC_MASK);
3425 if (type == NIXLF_BCAST_ENTRY) {
3426 *mce_list = &pfvf->bcast_mce_list;
3427 *mce_idx = pfvf->bcast_mce_idx;
3428 } else if (type == NIXLF_ALLMULTI_ENTRY) {
3429 *mce_list = &pfvf->mcast_mce_list;
3430 *mce_idx = pfvf->mcast_mce_idx;
3431 } else if (type == NIXLF_PROMISC_ENTRY) {
3432 *mce_list = &pfvf->promisc_mce_list;
3433 *mce_idx = pfvf->promisc_mce_idx;
3440 static int nix_update_mce_rule(struct rvu *rvu, u16 pcifunc,
3443 int err = 0, nixlf, blkaddr, mcam_index, mce_idx;
3444 struct npc_mcam *mcam = &rvu->hw->mcam;
3445 struct rvu_hwinfo *hw = rvu->hw;
3446 struct nix_mce_list *mce_list;
3449 /* skip multicast pkt replication for AF's VFs & SDP links */
3450 if (is_lbk_vf(rvu, pcifunc) || is_sdp_pfvf(pcifunc))
3453 if (!hw->cap.nix_rx_multicast)
3456 pf = rvu_get_pf(pcifunc);
3457 if (!is_pf_cgxmapped(rvu, pf))
3460 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
3464 nixlf = rvu_get_lf(rvu, &hw->block[blkaddr], pcifunc, 0);
3468 nix_get_mce_list(rvu, pcifunc, type, &mce_list, &mce_idx);
3470 mcam_index = npc_get_nixlf_mcam_index(mcam,
3471 pcifunc & ~RVU_PFVF_FUNC_MASK,
3473 err = nix_update_mce_list(rvu, pcifunc, mce_list,
3474 mce_idx, mcam_index, add);
3478 static void nix_setup_mcast_grp(struct nix_hw *nix_hw)
3480 struct nix_mcast_grp *mcast_grp = &nix_hw->mcast_grp;
3482 INIT_LIST_HEAD(&mcast_grp->mcast_grp_head);
3483 mutex_init(&mcast_grp->mcast_grp_lock);
3484 mcast_grp->next_grp_index = 1;
3485 mcast_grp->count = 0;
3488 static int nix_setup_mce_tables(struct rvu *rvu, struct nix_hw *nix_hw)
3490 struct nix_mcast *mcast = &nix_hw->mcast;
3491 int err, pf, numvfs, idx;
3492 struct rvu_pfvf *pfvf;
3496 /* Skip PF0 (i.e AF) */
3497 for (pf = 1; pf < (rvu->cgx_mapped_pfs + 1); pf++) {
3498 cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_PFX_CFG(pf));
3499 /* If PF is not enabled, nothing to do */
3500 if (!((cfg >> 20) & 0x01))
3502 /* Get numVFs attached to this PF */
3503 numvfs = (cfg >> 12) & 0xFF;
3505 pfvf = &rvu->pf[pf];
3507 /* This NIX0/1 block mapped to PF ? */
3508 if (pfvf->nix_blkaddr != nix_hw->blkaddr)
3511 /* save start idx of broadcast mce list */
3512 pfvf->bcast_mce_idx = nix_alloc_mce_list(mcast, numvfs + 1, NIX_MCAST_INGRESS);
3513 nix_mce_list_init(&pfvf->bcast_mce_list, numvfs + 1);
3515 /* save start idx of multicast mce list */
3516 pfvf->mcast_mce_idx = nix_alloc_mce_list(mcast, numvfs + 1, NIX_MCAST_INGRESS);
3517 nix_mce_list_init(&pfvf->mcast_mce_list, numvfs + 1);
3519 /* save the start idx of promisc mce list */
3520 pfvf->promisc_mce_idx = nix_alloc_mce_list(mcast, numvfs + 1, NIX_MCAST_INGRESS);
3521 nix_mce_list_init(&pfvf->promisc_mce_list, numvfs + 1);
3523 for (idx = 0; idx < (numvfs + 1); idx++) {
3524 /* idx-0 is for PF, followed by VFs */
3525 pcifunc = (pf << RVU_PFVF_PF_SHIFT);
3527 /* Add dummy entries now, so that we don't have to check
3528 * for whether AQ_OP should be INIT/WRITE later on.
3529 * Will be updated when a NIXLF is attached/detached to
3532 err = nix_blk_setup_mce(rvu, nix_hw,
3533 pfvf->bcast_mce_idx + idx,
3535 pcifunc, 0, 0, 1, true);
3539 /* add dummy entries to multicast mce list */
3540 err = nix_blk_setup_mce(rvu, nix_hw,
3541 pfvf->mcast_mce_idx + idx,
3543 pcifunc, 0, 0, 1, true);
3547 /* add dummy entries to promisc mce list */
3548 err = nix_blk_setup_mce(rvu, nix_hw,
3549 pfvf->promisc_mce_idx + idx,
3551 pcifunc, 0, 0, 1, true);
3559 static int nix_setup_mcast(struct rvu *rvu, struct nix_hw *nix_hw, int blkaddr)
3561 struct nix_mcast *mcast = &nix_hw->mcast;
3562 struct rvu_hwinfo *hw = rvu->hw;
3565 size = (rvu_read64(rvu, blkaddr, NIX_AF_CONST3) >> 16) & 0x0F;
3566 size = BIT_ULL(size);
3568 /* Allocate bitmap for rx mce entries */
3569 mcast->mce_counter[NIX_MCAST_INGRESS].max = 256UL << MC_TBL_SIZE;
3570 err = rvu_alloc_bitmap(&mcast->mce_counter[NIX_MCAST_INGRESS]);
3574 /* Allocate bitmap for tx mce entries */
3575 mcast->mce_counter[NIX_MCAST_EGRESS].max = MC_TX_MAX;
3576 err = rvu_alloc_bitmap(&mcast->mce_counter[NIX_MCAST_EGRESS]);
3578 rvu_free_bitmap(&mcast->mce_counter[NIX_MCAST_INGRESS]);
3582 /* Alloc memory for multicast/mirror replication entries */
3583 err = qmem_alloc(rvu->dev, &mcast->mce_ctx,
3584 mcast->mce_counter[NIX_MCAST_INGRESS].max, size);
3586 rvu_free_bitmap(&mcast->mce_counter[NIX_MCAST_INGRESS]);
3587 rvu_free_bitmap(&mcast->mce_counter[NIX_MCAST_EGRESS]);
3591 rvu_write64(rvu, blkaddr, NIX_AF_RX_MCAST_BASE,
3592 (u64)mcast->mce_ctx->iova);
3594 /* Set max list length equal to max no of VFs per PF + PF itself */
3595 rvu_write64(rvu, blkaddr, NIX_AF_RX_MCAST_CFG,
3596 BIT_ULL(36) | (hw->max_vfs_per_pf << 4) | MC_TBL_SIZE);
3598 /* Alloc memory for multicast replication buffers */
3599 size = rvu_read64(rvu, blkaddr, NIX_AF_MC_MIRROR_CONST) & 0xFFFF;
3600 err = qmem_alloc(rvu->dev, &mcast->mcast_buf,
3601 (8UL << MC_BUF_CNT), size);
3603 rvu_free_bitmap(&mcast->mce_counter[NIX_MCAST_INGRESS]);
3604 rvu_free_bitmap(&mcast->mce_counter[NIX_MCAST_EGRESS]);
3608 rvu_write64(rvu, blkaddr, NIX_AF_RX_MCAST_BUF_BASE,
3609 (u64)mcast->mcast_buf->iova);
3611 /* Alloc pkind for NIX internal RX multicast/mirror replay */
3612 mcast->replay_pkind = rvu_alloc_rsrc(&hw->pkind.rsrc);
3614 rvu_write64(rvu, blkaddr, NIX_AF_RX_MCAST_BUF_CFG,
3615 BIT_ULL(63) | (mcast->replay_pkind << 24) |
3616 BIT_ULL(20) | MC_BUF_CNT);
3618 mutex_init(&mcast->mce_lock);
3620 nix_setup_mcast_grp(nix_hw);
3622 return nix_setup_mce_tables(rvu, nix_hw);
3625 static int nix_setup_txvlan(struct rvu *rvu, struct nix_hw *nix_hw)
3627 struct nix_txvlan *vlan = &nix_hw->txvlan;
3630 /* Allocate resource bimap for tx vtag def registers*/
3631 vlan->rsrc.max = NIX_TX_VTAG_DEF_MAX;
3632 err = rvu_alloc_bitmap(&vlan->rsrc);
3636 /* Alloc memory for saving entry to RVU PFFUNC allocation mapping */
3637 vlan->entry2pfvf_map = devm_kcalloc(rvu->dev, vlan->rsrc.max,
3638 sizeof(u16), GFP_KERNEL);
3639 if (!vlan->entry2pfvf_map)
3642 mutex_init(&vlan->rsrc_lock);
3646 kfree(vlan->rsrc.bmap);
3650 static int nix_setup_txschq(struct rvu *rvu, struct nix_hw *nix_hw, int blkaddr)
3652 struct nix_txsch *txsch;
3656 /* Get scheduler queue count of each type and alloc
3657 * bitmap for each for alloc/free/attach operations.
3659 for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
3660 txsch = &nix_hw->txsch[lvl];
3663 case NIX_TXSCH_LVL_SMQ:
3664 reg = NIX_AF_MDQ_CONST;
3666 case NIX_TXSCH_LVL_TL4:
3667 reg = NIX_AF_TL4_CONST;
3669 case NIX_TXSCH_LVL_TL3:
3670 reg = NIX_AF_TL3_CONST;
3672 case NIX_TXSCH_LVL_TL2:
3673 reg = NIX_AF_TL2_CONST;
3675 case NIX_TXSCH_LVL_TL1:
3676 reg = NIX_AF_TL1_CONST;
3679 cfg = rvu_read64(rvu, blkaddr, reg);
3680 txsch->schq.max = cfg & 0xFFFF;
3681 err = rvu_alloc_bitmap(&txsch->schq);
3685 /* Allocate memory for scheduler queues to
3686 * PF/VF pcifunc mapping info.
3688 txsch->pfvf_map = devm_kcalloc(rvu->dev, txsch->schq.max,
3689 sizeof(u32), GFP_KERNEL);
3690 if (!txsch->pfvf_map)
3692 for (schq = 0; schq < txsch->schq.max; schq++)
3693 txsch->pfvf_map[schq] = TXSCH_MAP(0, NIX_TXSCHQ_FREE);
3696 /* Setup a default value of 8192 as DWRR MTU */
3697 if (rvu->hw->cap.nix_common_dwrr_mtu ||
3698 rvu->hw->cap.nix_multiple_dwrr_mtu) {
3699 rvu_write64(rvu, blkaddr,
3700 nix_get_dwrr_mtu_reg(rvu->hw, SMQ_LINK_TYPE_RPM),
3701 convert_bytes_to_dwrr_mtu(8192));
3702 rvu_write64(rvu, blkaddr,
3703 nix_get_dwrr_mtu_reg(rvu->hw, SMQ_LINK_TYPE_LBK),
3704 convert_bytes_to_dwrr_mtu(8192));
3705 rvu_write64(rvu, blkaddr,
3706 nix_get_dwrr_mtu_reg(rvu->hw, SMQ_LINK_TYPE_SDP),
3707 convert_bytes_to_dwrr_mtu(8192));
3713 int rvu_nix_reserve_mark_format(struct rvu *rvu, struct nix_hw *nix_hw,
3714 int blkaddr, u32 cfg)
3718 for (fmt_idx = 0; fmt_idx < nix_hw->mark_format.in_use; fmt_idx++) {
3719 if (nix_hw->mark_format.cfg[fmt_idx] == cfg)
3722 if (fmt_idx >= nix_hw->mark_format.total)
3725 rvu_write64(rvu, blkaddr, NIX_AF_MARK_FORMATX_CTL(fmt_idx), cfg);
3726 nix_hw->mark_format.cfg[fmt_idx] = cfg;
3727 nix_hw->mark_format.in_use++;
3731 static int nix_af_mark_format_setup(struct rvu *rvu, struct nix_hw *nix_hw,
3735 [NIX_MARK_CFG_IP_DSCP_RED] = 0x10003,
3736 [NIX_MARK_CFG_IP_DSCP_YELLOW] = 0x11200,
3737 [NIX_MARK_CFG_IP_DSCP_YELLOW_RED] = 0x11203,
3738 [NIX_MARK_CFG_IP_ECN_RED] = 0x6000c,
3739 [NIX_MARK_CFG_IP_ECN_YELLOW] = 0x60c00,
3740 [NIX_MARK_CFG_IP_ECN_YELLOW_RED] = 0x60c0c,
3741 [NIX_MARK_CFG_VLAN_DEI_RED] = 0x30008,
3742 [NIX_MARK_CFG_VLAN_DEI_YELLOW] = 0x30800,
3743 [NIX_MARK_CFG_VLAN_DEI_YELLOW_RED] = 0x30808,
3748 total = (rvu_read64(rvu, blkaddr, NIX_AF_PSE_CONST) & 0xFF00) >> 8;
3749 nix_hw->mark_format.total = (u8)total;
3750 nix_hw->mark_format.cfg = devm_kcalloc(rvu->dev, total, sizeof(u32),
3752 if (!nix_hw->mark_format.cfg)
3754 for (i = 0; i < NIX_MARK_CFG_MAX; i++) {
3755 rc = rvu_nix_reserve_mark_format(rvu, nix_hw, blkaddr, cfgs[i]);
3757 dev_err(rvu->dev, "Err %d in setup mark format %d\n",
3764 static void rvu_get_lbk_link_max_frs(struct rvu *rvu, u16 *max_mtu)
3766 /* CN10K supports LBK FIFO size 72 KB */
3767 if (rvu->hw->lbk_bufsize == 0x12000)
3768 *max_mtu = CN10K_LBK_LINK_MAX_FRS;
3770 *max_mtu = NIC_HW_MAX_FRS;
3773 static void rvu_get_lmac_link_max_frs(struct rvu *rvu, u16 *max_mtu)
3775 int fifo_size = rvu_cgx_get_fifolen(rvu);
3777 /* RPM supports FIFO len 128 KB and RPM2 supports double the
3778 * FIFO len to accommodate 8 LMACS
3780 if (fifo_size == 0x20000 || fifo_size == 0x40000)
3781 *max_mtu = CN10K_LMAC_LINK_MAX_FRS;
3783 *max_mtu = NIC_HW_MAX_FRS;
3786 int rvu_mbox_handler_nix_get_hw_info(struct rvu *rvu, struct msg_req *req,
3787 struct nix_hw_info *rsp)
3789 u16 pcifunc = req->hdr.pcifunc;
3793 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
3795 return NIX_AF_ERR_AF_LF_INVALID;
3797 if (is_lbk_vf(rvu, pcifunc))
3798 rvu_get_lbk_link_max_frs(rvu, &rsp->max_mtu);
3800 rvu_get_lmac_link_max_frs(rvu, &rsp->max_mtu);
3802 rsp->min_mtu = NIC_HW_MIN_FRS;
3804 if (!rvu->hw->cap.nix_common_dwrr_mtu &&
3805 !rvu->hw->cap.nix_multiple_dwrr_mtu) {
3806 /* Return '1' on OTx2 */
3807 rsp->rpm_dwrr_mtu = 1;
3808 rsp->sdp_dwrr_mtu = 1;
3809 rsp->lbk_dwrr_mtu = 1;
3813 /* Return DWRR_MTU for TLx_SCHEDULE[RR_WEIGHT] config */
3814 dwrr_mtu = rvu_read64(rvu, blkaddr,
3815 nix_get_dwrr_mtu_reg(rvu->hw, SMQ_LINK_TYPE_RPM));
3816 rsp->rpm_dwrr_mtu = convert_dwrr_mtu_to_bytes(dwrr_mtu);
3818 dwrr_mtu = rvu_read64(rvu, blkaddr,
3819 nix_get_dwrr_mtu_reg(rvu->hw, SMQ_LINK_TYPE_SDP));
3820 rsp->sdp_dwrr_mtu = convert_dwrr_mtu_to_bytes(dwrr_mtu);
3822 dwrr_mtu = rvu_read64(rvu, blkaddr,
3823 nix_get_dwrr_mtu_reg(rvu->hw, SMQ_LINK_TYPE_LBK));
3824 rsp->lbk_dwrr_mtu = convert_dwrr_mtu_to_bytes(dwrr_mtu);
3829 int rvu_mbox_handler_nix_stats_rst(struct rvu *rvu, struct msg_req *req,
3830 struct msg_rsp *rsp)
3832 u16 pcifunc = req->hdr.pcifunc;
3833 int i, nixlf, blkaddr, err;
3836 err = nix_get_nixlf(rvu, pcifunc, &nixlf, &blkaddr);
3840 /* Get stats count supported by HW */
3841 stats = rvu_read64(rvu, blkaddr, NIX_AF_CONST1);
3843 /* Reset tx stats */
3844 for (i = 0; i < ((stats >> 24) & 0xFF); i++)
3845 rvu_write64(rvu, blkaddr, NIX_AF_LFX_TX_STATX(nixlf, i), 0);
3847 /* Reset rx stats */
3848 for (i = 0; i < ((stats >> 32) & 0xFF); i++)
3849 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RX_STATX(nixlf, i), 0);
3854 /* Returns the ALG index to be set into NPC_RX_ACTION */
3855 static int get_flowkey_alg_idx(struct nix_hw *nix_hw, u32 flow_cfg)
3859 /* Scan over exiting algo entries to find a match */
3860 for (i = 0; i < nix_hw->flowkey.in_use; i++)
3861 if (nix_hw->flowkey.flowkey[i] == flow_cfg)
3867 static int set_flowkey_fields(struct nix_rx_flowkey_alg *alg, u32 flow_cfg)
3869 int idx, nr_field, key_off, field_marker, keyoff_marker;
3870 int max_key_off, max_bit_pos, group_member;
3871 struct nix_rx_flowkey_alg *field;
3872 struct nix_rx_flowkey_alg tmp;
3873 u32 key_type, valid_key;
3875 int l4_key_offset = 0;
3880 #define FIELDS_PER_ALG 5
3881 #define MAX_KEY_OFF 40
3882 /* Clear all fields */
3883 memset(alg, 0, sizeof(uint64_t) * FIELDS_PER_ALG);
3885 /* Each of the 32 possible flow key algorithm definitions should
3886 * fall into above incremental config (except ALG0). Otherwise a
3887 * single NPC MCAM entry is not sufficient for supporting RSS.
3889 * If a different definition or combination needed then NPC MCAM
3890 * has to be programmed to filter such pkts and it's action should
3891 * point to this definition to calculate flowtag or hash.
3893 * The `for loop` goes over _all_ protocol field and the following
3894 * variables depicts the state machine forward progress logic.
3896 * keyoff_marker - Enabled when hash byte length needs to be accounted
3897 * in field->key_offset update.
3898 * field_marker - Enabled when a new field needs to be selected.
3899 * group_member - Enabled when protocol is part of a group.
3902 /* Last 4 bits (31:28) are reserved to specify SRC, DST
3903 * selection for L3, L4 i.e IPV[4,6]_SRC, IPV[4,6]_DST,
3904 * [TCP,UDP,SCTP]_SRC, [TCP,UDP,SCTP]_DST
3905 * 31 => L3_SRC, 30 => L3_DST, 29 => L4_SRC, 28 => L4_DST
3907 l3_l4_src_dst = flow_cfg;
3908 /* Reset these 4 bits, so that these won't be part of key */
3909 flow_cfg &= NIX_FLOW_KEY_TYPE_L3_L4_MASK;
3911 keyoff_marker = 0; max_key_off = 0; group_member = 0;
3912 nr_field = 0; key_off = 0; field_marker = 1;
3913 field = &tmp; max_bit_pos = fls(flow_cfg);
3915 idx < max_bit_pos && nr_field < FIELDS_PER_ALG &&
3916 key_off < MAX_KEY_OFF; idx++) {
3917 key_type = BIT(idx);
3918 valid_key = flow_cfg & key_type;
3919 /* Found a field marker, reset the field values */
3921 memset(&tmp, 0, sizeof(tmp));
3923 field_marker = true;
3924 keyoff_marker = true;
3926 case NIX_FLOW_KEY_TYPE_PORT:
3927 field->sel_chan = true;
3928 /* This should be set to 1, when SEL_CHAN is set */
3931 case NIX_FLOW_KEY_TYPE_IPV4_PROTO:
3932 field->lid = NPC_LID_LC;
3933 field->hdr_offset = 9; /* offset */
3934 field->bytesm1 = 0; /* 1 byte */
3935 field->ltype_match = NPC_LT_LC_IP;
3936 field->ltype_mask = 0xF;
3938 case NIX_FLOW_KEY_TYPE_IPV4:
3939 case NIX_FLOW_KEY_TYPE_INNR_IPV4:
3940 field->lid = NPC_LID_LC;
3941 field->ltype_match = NPC_LT_LC_IP;
3942 if (key_type == NIX_FLOW_KEY_TYPE_INNR_IPV4) {
3943 field->lid = NPC_LID_LG;
3944 field->ltype_match = NPC_LT_LG_TU_IP;
3946 field->hdr_offset = 12; /* SIP offset */
3947 field->bytesm1 = 7; /* SIP + DIP, 8 bytes */
3950 if (l3_l4_src_dst & NIX_FLOW_KEY_TYPE_L3_SRC_ONLY)
3951 field->bytesm1 = 3; /* SIP, 4 bytes */
3953 if (l3_l4_src_dst & NIX_FLOW_KEY_TYPE_L3_DST_ONLY) {
3954 /* Both SIP + DIP */
3955 if (field->bytesm1 == 3) {
3956 field->bytesm1 = 7; /* SIP + DIP, 8B */
3959 field->hdr_offset = 16; /* DIP off */
3960 field->bytesm1 = 3; /* DIP, 4 bytes */
3964 field->ltype_mask = 0xF; /* Match only IPv4 */
3965 keyoff_marker = false;
3967 case NIX_FLOW_KEY_TYPE_IPV6:
3968 case NIX_FLOW_KEY_TYPE_INNR_IPV6:
3969 field->lid = NPC_LID_LC;
3970 field->ltype_match = NPC_LT_LC_IP6;
3971 if (key_type == NIX_FLOW_KEY_TYPE_INNR_IPV6) {
3972 field->lid = NPC_LID_LG;
3973 field->ltype_match = NPC_LT_LG_TU_IP6;
3975 field->hdr_offset = 8; /* SIP offset */
3976 field->bytesm1 = 31; /* SIP + DIP, 32 bytes */
3979 if (l3_l4_src_dst & NIX_FLOW_KEY_TYPE_L3_SRC_ONLY)
3980 field->bytesm1 = 15; /* SIP, 16 bytes */
3982 if (l3_l4_src_dst & NIX_FLOW_KEY_TYPE_L3_DST_ONLY) {
3983 /* Both SIP + DIP */
3984 if (field->bytesm1 == 15) {
3985 /* SIP + DIP, 32 bytes */
3986 field->bytesm1 = 31;
3989 field->hdr_offset = 24; /* DIP off */
3990 field->bytesm1 = 15; /* DIP,16 bytes */
3993 field->ltype_mask = 0xF; /* Match only IPv6 */
3995 case NIX_FLOW_KEY_TYPE_TCP:
3996 case NIX_FLOW_KEY_TYPE_UDP:
3997 case NIX_FLOW_KEY_TYPE_SCTP:
3998 case NIX_FLOW_KEY_TYPE_INNR_TCP:
3999 case NIX_FLOW_KEY_TYPE_INNR_UDP:
4000 case NIX_FLOW_KEY_TYPE_INNR_SCTP:
4001 field->lid = NPC_LID_LD;
4002 if (key_type == NIX_FLOW_KEY_TYPE_INNR_TCP ||
4003 key_type == NIX_FLOW_KEY_TYPE_INNR_UDP ||
4004 key_type == NIX_FLOW_KEY_TYPE_INNR_SCTP)
4005 field->lid = NPC_LID_LH;
4006 field->bytesm1 = 3; /* Sport + Dport, 4 bytes */
4008 if (l3_l4_src_dst & NIX_FLOW_KEY_TYPE_L4_SRC_ONLY)
4009 field->bytesm1 = 1; /* SRC, 2 bytes */
4011 if (l3_l4_src_dst & NIX_FLOW_KEY_TYPE_L4_DST_ONLY) {
4012 /* Both SRC + DST */
4013 if (field->bytesm1 == 1) {
4014 /* SRC + DST, 4 bytes */
4018 field->hdr_offset = 2; /* DST off */
4019 field->bytesm1 = 1; /* DST, 2 bytes */
4023 /* Enum values for NPC_LID_LD and NPC_LID_LG are same,
4024 * so no need to change the ltype_match, just change
4025 * the lid for inner protocols
4027 BUILD_BUG_ON((int)NPC_LT_LD_TCP !=
4028 (int)NPC_LT_LH_TU_TCP);
4029 BUILD_BUG_ON((int)NPC_LT_LD_UDP !=
4030 (int)NPC_LT_LH_TU_UDP);
4031 BUILD_BUG_ON((int)NPC_LT_LD_SCTP !=
4032 (int)NPC_LT_LH_TU_SCTP);
4034 if ((key_type == NIX_FLOW_KEY_TYPE_TCP ||
4035 key_type == NIX_FLOW_KEY_TYPE_INNR_TCP) &&
4037 field->ltype_match |= NPC_LT_LD_TCP;
4038 group_member = true;
4039 } else if ((key_type == NIX_FLOW_KEY_TYPE_UDP ||
4040 key_type == NIX_FLOW_KEY_TYPE_INNR_UDP) &&
4042 field->ltype_match |= NPC_LT_LD_UDP;
4043 group_member = true;
4044 } else if ((key_type == NIX_FLOW_KEY_TYPE_SCTP ||
4045 key_type == NIX_FLOW_KEY_TYPE_INNR_SCTP) &&
4047 field->ltype_match |= NPC_LT_LD_SCTP;
4048 group_member = true;
4050 field->ltype_mask = ~field->ltype_match;
4051 if (key_type == NIX_FLOW_KEY_TYPE_SCTP ||
4052 key_type == NIX_FLOW_KEY_TYPE_INNR_SCTP) {
4053 /* Handle the case where any of the group item
4054 * is enabled in the group but not the final one
4058 group_member = false;
4061 field_marker = false;
4062 keyoff_marker = false;
4065 /* TCP/UDP/SCTP and ESP/AH falls at same offset so
4066 * remember the TCP key offset of 40 byte hash key.
4068 if (key_type == NIX_FLOW_KEY_TYPE_TCP)
4069 l4_key_offset = key_off;
4071 case NIX_FLOW_KEY_TYPE_NVGRE:
4072 field->lid = NPC_LID_LD;
4073 field->hdr_offset = 4; /* VSID offset */
4075 field->ltype_match = NPC_LT_LD_NVGRE;
4076 field->ltype_mask = 0xF;
4078 case NIX_FLOW_KEY_TYPE_VXLAN:
4079 case NIX_FLOW_KEY_TYPE_GENEVE:
4080 field->lid = NPC_LID_LE;
4082 field->hdr_offset = 4;
4083 field->ltype_mask = 0xF;
4084 field_marker = false;
4085 keyoff_marker = false;
4087 if (key_type == NIX_FLOW_KEY_TYPE_VXLAN && valid_key) {
4088 field->ltype_match |= NPC_LT_LE_VXLAN;
4089 group_member = true;
4092 if (key_type == NIX_FLOW_KEY_TYPE_GENEVE && valid_key) {
4093 field->ltype_match |= NPC_LT_LE_GENEVE;
4094 group_member = true;
4097 if (key_type == NIX_FLOW_KEY_TYPE_GENEVE) {
4099 field->ltype_mask = ~field->ltype_match;
4100 field_marker = true;
4101 keyoff_marker = true;
4103 group_member = false;
4107 case NIX_FLOW_KEY_TYPE_ETH_DMAC:
4108 case NIX_FLOW_KEY_TYPE_INNR_ETH_DMAC:
4109 field->lid = NPC_LID_LA;
4110 field->ltype_match = NPC_LT_LA_ETHER;
4111 if (key_type == NIX_FLOW_KEY_TYPE_INNR_ETH_DMAC) {
4112 field->lid = NPC_LID_LF;
4113 field->ltype_match = NPC_LT_LF_TU_ETHER;
4115 field->hdr_offset = 0;
4116 field->bytesm1 = 5; /* DMAC 6 Byte */
4117 field->ltype_mask = 0xF;
4119 case NIX_FLOW_KEY_TYPE_IPV6_EXT:
4120 field->lid = NPC_LID_LC;
4121 field->hdr_offset = 40; /* IPV6 hdr */
4122 field->bytesm1 = 0; /* 1 Byte ext hdr*/
4123 field->ltype_match = NPC_LT_LC_IP6_EXT;
4124 field->ltype_mask = 0xF;
4126 case NIX_FLOW_KEY_TYPE_GTPU:
4127 field->lid = NPC_LID_LE;
4128 field->hdr_offset = 4;
4129 field->bytesm1 = 3; /* 4 bytes TID*/
4130 field->ltype_match = NPC_LT_LE_GTPU;
4131 field->ltype_mask = 0xF;
4133 case NIX_FLOW_KEY_TYPE_CUSTOM0:
4134 field->lid = NPC_LID_LC;
4135 field->hdr_offset = 6;
4136 field->bytesm1 = 1; /* 2 Bytes*/
4137 field->ltype_match = NPC_LT_LC_CUSTOM0;
4138 field->ltype_mask = 0xF;
4140 case NIX_FLOW_KEY_TYPE_VLAN:
4141 field->lid = NPC_LID_LB;
4142 field->hdr_offset = 2; /* Skip TPID (2-bytes) */
4143 field->bytesm1 = 1; /* 2 Bytes (Actually 12 bits) */
4144 field->ltype_match = NPC_LT_LB_CTAG;
4145 field->ltype_mask = 0xF;
4146 field->fn_mask = 1; /* Mask out the first nibble */
4148 case NIX_FLOW_KEY_TYPE_AH:
4149 case NIX_FLOW_KEY_TYPE_ESP:
4150 field->hdr_offset = 0;
4151 field->bytesm1 = 7; /* SPI + sequence number */
4152 field->ltype_mask = 0xF;
4153 field->lid = NPC_LID_LE;
4154 field->ltype_match = NPC_LT_LE_ESP;
4155 if (key_type == NIX_FLOW_KEY_TYPE_AH) {
4156 field->lid = NPC_LID_LD;
4157 field->ltype_match = NPC_LT_LD_AH;
4158 field->hdr_offset = 4;
4159 keyoff_marker = false;
4165 /* Found a valid flow key type */
4167 /* Use the key offset of TCP/UDP/SCTP fields
4168 * for ESP/AH fields.
4170 if (key_type == NIX_FLOW_KEY_TYPE_ESP ||
4171 key_type == NIX_FLOW_KEY_TYPE_AH)
4172 key_off = l4_key_offset;
4173 field->key_offset = key_off;
4174 memcpy(&alg[nr_field], field, sizeof(*field));
4175 max_key_off = max(max_key_off, field->bytesm1 + 1);
4177 /* Found a field marker, get the next field */
4182 /* Found a keyoff marker, update the new key_off */
4183 if (keyoff_marker) {
4184 key_off += max_key_off;
4188 /* Processed all the flow key types */
4189 if (idx == max_bit_pos && key_off <= MAX_KEY_OFF)
4192 return NIX_AF_ERR_RSS_NOSPC_FIELD;
4195 static int reserve_flowkey_alg_idx(struct rvu *rvu, int blkaddr, u32 flow_cfg)
4197 u64 field[FIELDS_PER_ALG];
4201 hw = get_nix_hw(rvu->hw, blkaddr);
4203 return NIX_AF_ERR_INVALID_NIXBLK;
4205 /* No room to add new flow hash algoritham */
4206 if (hw->flowkey.in_use >= NIX_FLOW_KEY_ALG_MAX)
4207 return NIX_AF_ERR_RSS_NOSPC_ALGO;
4209 /* Generate algo fields for the given flow_cfg */
4210 rc = set_flowkey_fields((struct nix_rx_flowkey_alg *)field, flow_cfg);
4214 /* Update ALGX_FIELDX register with generated fields */
4215 for (fid = 0; fid < FIELDS_PER_ALG; fid++)
4216 rvu_write64(rvu, blkaddr,
4217 NIX_AF_RX_FLOW_KEY_ALGX_FIELDX(hw->flowkey.in_use,
4220 /* Store the flow_cfg for futher lookup */
4221 rc = hw->flowkey.in_use;
4222 hw->flowkey.flowkey[rc] = flow_cfg;
4223 hw->flowkey.in_use++;
4228 int rvu_mbox_handler_nix_rss_flowkey_cfg(struct rvu *rvu,
4229 struct nix_rss_flowkey_cfg *req,
4230 struct nix_rss_flowkey_cfg_rsp *rsp)
4232 u16 pcifunc = req->hdr.pcifunc;
4233 int alg_idx, nixlf, blkaddr;
4234 struct nix_hw *nix_hw;
4237 err = nix_get_nixlf(rvu, pcifunc, &nixlf, &blkaddr);
4241 nix_hw = get_nix_hw(rvu->hw, blkaddr);
4243 return NIX_AF_ERR_INVALID_NIXBLK;
4245 alg_idx = get_flowkey_alg_idx(nix_hw, req->flowkey_cfg);
4246 /* Failed to get algo index from the exiting list, reserve new */
4248 alg_idx = reserve_flowkey_alg_idx(rvu, blkaddr,
4253 rsp->alg_idx = alg_idx;
4254 rvu_npc_update_flowkey_alg_idx(rvu, pcifunc, nixlf, req->group,
4255 alg_idx, req->mcam_index);
4259 static int nix_rx_flowkey_alg_cfg(struct rvu *rvu, int blkaddr)
4261 u32 flowkey_cfg, minkey_cfg;
4264 /* Disable all flow key algx fieldx */
4265 for (alg = 0; alg < NIX_FLOW_KEY_ALG_MAX; alg++) {
4266 for (fid = 0; fid < FIELDS_PER_ALG; fid++)
4267 rvu_write64(rvu, blkaddr,
4268 NIX_AF_RX_FLOW_KEY_ALGX_FIELDX(alg, fid),
4272 /* IPv4/IPv6 SIP/DIPs */
4273 flowkey_cfg = NIX_FLOW_KEY_TYPE_IPV4 | NIX_FLOW_KEY_TYPE_IPV6;
4274 rc = reserve_flowkey_alg_idx(rvu, blkaddr, flowkey_cfg);
4278 /* TCPv4/v6 4-tuple, SIP, DIP, Sport, Dport */
4279 minkey_cfg = flowkey_cfg;
4280 flowkey_cfg = minkey_cfg | NIX_FLOW_KEY_TYPE_TCP;
4281 rc = reserve_flowkey_alg_idx(rvu, blkaddr, flowkey_cfg);
4285 /* UDPv4/v6 4-tuple, SIP, DIP, Sport, Dport */
4286 flowkey_cfg = minkey_cfg | NIX_FLOW_KEY_TYPE_UDP;
4287 rc = reserve_flowkey_alg_idx(rvu, blkaddr, flowkey_cfg);
4291 /* SCTPv4/v6 4-tuple, SIP, DIP, Sport, Dport */
4292 flowkey_cfg = minkey_cfg | NIX_FLOW_KEY_TYPE_SCTP;
4293 rc = reserve_flowkey_alg_idx(rvu, blkaddr, flowkey_cfg);
4297 /* TCP/UDP v4/v6 4-tuple, rest IP pkts 2-tuple */
4298 flowkey_cfg = minkey_cfg | NIX_FLOW_KEY_TYPE_TCP |
4299 NIX_FLOW_KEY_TYPE_UDP;
4300 rc = reserve_flowkey_alg_idx(rvu, blkaddr, flowkey_cfg);
4304 /* TCP/SCTP v4/v6 4-tuple, rest IP pkts 2-tuple */
4305 flowkey_cfg = minkey_cfg | NIX_FLOW_KEY_TYPE_TCP |
4306 NIX_FLOW_KEY_TYPE_SCTP;
4307 rc = reserve_flowkey_alg_idx(rvu, blkaddr, flowkey_cfg);
4311 /* UDP/SCTP v4/v6 4-tuple, rest IP pkts 2-tuple */
4312 flowkey_cfg = minkey_cfg | NIX_FLOW_KEY_TYPE_UDP |
4313 NIX_FLOW_KEY_TYPE_SCTP;
4314 rc = reserve_flowkey_alg_idx(rvu, blkaddr, flowkey_cfg);
4318 /* TCP/UDP/SCTP v4/v6 4-tuple, rest IP pkts 2-tuple */
4319 flowkey_cfg = minkey_cfg | NIX_FLOW_KEY_TYPE_TCP |
4320 NIX_FLOW_KEY_TYPE_UDP | NIX_FLOW_KEY_TYPE_SCTP;
4321 rc = reserve_flowkey_alg_idx(rvu, blkaddr, flowkey_cfg);
4328 int rvu_mbox_handler_nix_set_mac_addr(struct rvu *rvu,
4329 struct nix_set_mac_addr *req,
4330 struct msg_rsp *rsp)
4332 bool from_vf = req->hdr.pcifunc & RVU_PFVF_FUNC_MASK;
4333 u16 pcifunc = req->hdr.pcifunc;
4334 int blkaddr, nixlf, err;
4335 struct rvu_pfvf *pfvf;
4337 err = nix_get_nixlf(rvu, pcifunc, &nixlf, &blkaddr);
4341 pfvf = rvu_get_pfvf(rvu, pcifunc);
4343 /* untrusted VF can't overwrite admin(PF) changes */
4344 if (!test_bit(PF_SET_VF_TRUSTED, &pfvf->flags) &&
4345 (from_vf && test_bit(PF_SET_VF_MAC, &pfvf->flags))) {
4347 "MAC address set by admin(PF) cannot be overwritten by untrusted VF");
4351 ether_addr_copy(pfvf->mac_addr, req->mac_addr);
4353 rvu_npc_install_ucast_entry(rvu, pcifunc, nixlf,
4354 pfvf->rx_chan_base, req->mac_addr);
4356 if (test_bit(PF_SET_VF_TRUSTED, &pfvf->flags) && from_vf)
4357 ether_addr_copy(pfvf->default_mac, req->mac_addr);
4359 rvu_switch_update_rules(rvu, pcifunc);
4364 int rvu_mbox_handler_nix_get_mac_addr(struct rvu *rvu,
4365 struct msg_req *req,
4366 struct nix_get_mac_addr_rsp *rsp)
4368 u16 pcifunc = req->hdr.pcifunc;
4369 struct rvu_pfvf *pfvf;
4371 if (!is_nixlf_attached(rvu, pcifunc))
4372 return NIX_AF_ERR_AF_LF_INVALID;
4374 pfvf = rvu_get_pfvf(rvu, pcifunc);
4376 ether_addr_copy(rsp->mac_addr, pfvf->mac_addr);
4381 int rvu_mbox_handler_nix_set_rx_mode(struct rvu *rvu, struct nix_rx_mode *req,
4382 struct msg_rsp *rsp)
4384 bool allmulti, promisc, nix_rx_multicast;
4385 u16 pcifunc = req->hdr.pcifunc;
4386 struct rvu_pfvf *pfvf;
4389 pfvf = rvu_get_pfvf(rvu, pcifunc);
4390 promisc = req->mode & NIX_RX_MODE_PROMISC ? true : false;
4391 allmulti = req->mode & NIX_RX_MODE_ALLMULTI ? true : false;
4392 pfvf->use_mce_list = req->mode & NIX_RX_MODE_USE_MCE ? true : false;
4394 nix_rx_multicast = rvu->hw->cap.nix_rx_multicast & pfvf->use_mce_list;
4396 if (is_vf(pcifunc) && !nix_rx_multicast &&
4397 (promisc || allmulti)) {
4398 dev_warn_ratelimited(rvu->dev,
4399 "VF promisc/multicast not supported\n");
4403 /* untrusted VF can't configure promisc/allmulti */
4404 if (is_vf(pcifunc) && !test_bit(PF_SET_VF_TRUSTED, &pfvf->flags) &&
4405 (promisc || allmulti))
4408 err = nix_get_nixlf(rvu, pcifunc, &nixlf, NULL);
4412 if (nix_rx_multicast) {
4413 /* add/del this PF_FUNC to/from mcast pkt replication list */
4414 err = nix_update_mce_rule(rvu, pcifunc, NIXLF_ALLMULTI_ENTRY,
4418 "Failed to update pcifunc 0x%x to multicast list\n",
4423 /* add/del this PF_FUNC to/from promisc pkt replication list */
4424 err = nix_update_mce_rule(rvu, pcifunc, NIXLF_PROMISC_ENTRY,
4428 "Failed to update pcifunc 0x%x to promisc list\n",
4434 /* install/uninstall allmulti entry */
4436 rvu_npc_install_allmulti_entry(rvu, pcifunc, nixlf,
4437 pfvf->rx_chan_base);
4439 if (!nix_rx_multicast)
4440 rvu_npc_enable_allmulti_entry(rvu, pcifunc, nixlf, false);
4443 /* install/uninstall promisc entry */
4445 rvu_npc_install_promisc_entry(rvu, pcifunc, nixlf,
4449 if (!nix_rx_multicast)
4450 rvu_npc_enable_promisc_entry(rvu, pcifunc, nixlf, false);
4455 static void nix_find_link_frs(struct rvu *rvu,
4456 struct nix_frs_cfg *req, u16 pcifunc)
4458 int pf = rvu_get_pf(pcifunc);
4459 struct rvu_pfvf *pfvf;
4464 /* Update with requester's min/max lengths */
4465 pfvf = rvu_get_pfvf(rvu, pcifunc);
4466 pfvf->maxlen = req->maxlen;
4467 if (req->update_minlen)
4468 pfvf->minlen = req->minlen;
4470 maxlen = req->maxlen;
4471 minlen = req->update_minlen ? req->minlen : 0;
4473 /* Get this PF's numVFs and starting hwvf */
4474 rvu_get_pf_numvfs(rvu, pf, &numvfs, &hwvf);
4476 /* For each VF, compare requested max/minlen */
4477 for (vf = 0; vf < numvfs; vf++) {
4478 pfvf = &rvu->hwvf[hwvf + vf];
4479 if (pfvf->maxlen > maxlen)
4480 maxlen = pfvf->maxlen;
4481 if (req->update_minlen &&
4482 pfvf->minlen && pfvf->minlen < minlen)
4483 minlen = pfvf->minlen;
4486 /* Compare requested max/minlen with PF's max/minlen */
4487 pfvf = &rvu->pf[pf];
4488 if (pfvf->maxlen > maxlen)
4489 maxlen = pfvf->maxlen;
4490 if (req->update_minlen &&
4491 pfvf->minlen && pfvf->minlen < minlen)
4492 minlen = pfvf->minlen;
4494 /* Update the request with max/min PF's and it's VF's max/min */
4495 req->maxlen = maxlen;
4496 if (req->update_minlen)
4497 req->minlen = minlen;
4500 int rvu_mbox_handler_nix_set_hw_frs(struct rvu *rvu, struct nix_frs_cfg *req,
4501 struct msg_rsp *rsp)
4503 struct rvu_hwinfo *hw = rvu->hw;
4504 u16 pcifunc = req->hdr.pcifunc;
4505 int pf = rvu_get_pf(pcifunc);
4506 int blkaddr, link = -1;
4507 struct nix_hw *nix_hw;
4508 struct rvu_pfvf *pfvf;
4509 u8 cgx = 0, lmac = 0;
4513 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
4515 return NIX_AF_ERR_AF_LF_INVALID;
4517 nix_hw = get_nix_hw(rvu->hw, blkaddr);
4519 return NIX_AF_ERR_INVALID_NIXBLK;
4521 if (is_lbk_vf(rvu, pcifunc))
4522 rvu_get_lbk_link_max_frs(rvu, &max_mtu);
4524 rvu_get_lmac_link_max_frs(rvu, &max_mtu);
4526 if (!req->sdp_link && req->maxlen > max_mtu)
4527 return NIX_AF_ERR_FRS_INVALID;
4529 if (req->update_minlen && req->minlen < NIC_HW_MIN_FRS)
4530 return NIX_AF_ERR_FRS_INVALID;
4532 /* Check if config is for SDP link */
4533 if (req->sdp_link) {
4535 return NIX_AF_ERR_RX_LINK_INVALID;
4536 link = hw->cgx_links + hw->lbk_links;
4540 /* Check if the request is from CGX mapped RVU PF */
4541 if (is_pf_cgxmapped(rvu, pf)) {
4542 /* Get CGX and LMAC to which this PF is mapped and find link */
4543 rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx, &lmac);
4544 link = (cgx * hw->lmac_per_cgx) + lmac;
4545 } else if (pf == 0) {
4546 /* For VFs of PF0 ingress is LBK port, so config LBK link */
4547 pfvf = rvu_get_pfvf(rvu, pcifunc);
4548 link = hw->cgx_links + pfvf->lbkid;
4552 return NIX_AF_ERR_RX_LINK_INVALID;
4555 nix_find_link_frs(rvu, req, pcifunc);
4557 cfg = rvu_read64(rvu, blkaddr, NIX_AF_RX_LINKX_CFG(link));
4558 cfg = (cfg & ~(0xFFFFULL << 16)) | ((u64)req->maxlen << 16);
4559 if (req->update_minlen)
4560 cfg = (cfg & ~0xFFFFULL) | req->minlen;
4561 rvu_write64(rvu, blkaddr, NIX_AF_RX_LINKX_CFG(link), cfg);
4566 int rvu_mbox_handler_nix_set_rx_cfg(struct rvu *rvu, struct nix_rx_cfg *req,
4567 struct msg_rsp *rsp)
4569 int nixlf, blkaddr, err;
4572 err = nix_get_nixlf(rvu, req->hdr.pcifunc, &nixlf, &blkaddr);
4576 cfg = rvu_read64(rvu, blkaddr, NIX_AF_LFX_RX_CFG(nixlf));
4577 /* Set the interface configuration */
4578 if (req->len_verify & BIT(0))
4581 cfg &= ~BIT_ULL(41);
4583 if (req->len_verify & BIT(1))
4586 cfg &= ~BIT_ULL(40);
4588 if (req->len_verify & NIX_RX_DROP_RE)
4591 cfg &= ~BIT_ULL(32);
4593 if (req->csum_verify & BIT(0))
4596 cfg &= ~BIT_ULL(37);
4598 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RX_CFG(nixlf), cfg);
4603 static u64 rvu_get_lbk_link_credits(struct rvu *rvu, u16 lbk_max_frs)
4605 return 1600; /* 16 * max LBK datarate = 16 * 100Gbps */
4608 static void nix_link_config(struct rvu *rvu, int blkaddr,
4609 struct nix_hw *nix_hw)
4611 struct rvu_hwinfo *hw = rvu->hw;
4612 int cgx, lmac_cnt, slink, link;
4613 u16 lbk_max_frs, lmac_max_frs;
4614 unsigned long lmac_bmap;
4615 u64 tx_credits, cfg;
4619 rvu_get_lbk_link_max_frs(rvu, &lbk_max_frs);
4620 rvu_get_lmac_link_max_frs(rvu, &lmac_max_frs);
4622 /* Set default min/max packet lengths allowed on NIX Rx links.
4624 * With HW reset minlen value of 60byte, HW will treat ARP pkts
4625 * as undersize and report them to SW as error pkts, hence
4626 * setting it to 40 bytes.
4628 for (link = 0; link < hw->cgx_links; link++) {
4629 rvu_write64(rvu, blkaddr, NIX_AF_RX_LINKX_CFG(link),
4630 ((u64)lmac_max_frs << 16) | NIC_HW_MIN_FRS);
4633 for (link = hw->cgx_links; link < hw->lbk_links; link++) {
4634 rvu_write64(rvu, blkaddr, NIX_AF_RX_LINKX_CFG(link),
4635 ((u64)lbk_max_frs << 16) | NIC_HW_MIN_FRS);
4637 if (hw->sdp_links) {
4638 link = hw->cgx_links + hw->lbk_links;
4639 rvu_write64(rvu, blkaddr, NIX_AF_RX_LINKX_CFG(link),
4640 SDP_HW_MAX_FRS << 16 | NIC_HW_MIN_FRS);
4643 /* Get MCS external bypass status for CN10K-B */
4644 if (mcs_get_blkcnt() == 1) {
4645 /* Adjust for 2 credits when external bypass is disabled */
4646 nix_hw->cc_mcs_cnt = is_mcs_bypass(0) ? 0 : 2;
4649 /* Set credits for Tx links assuming max packet length allowed.
4650 * This will be reconfigured based on MTU set for PF/VF.
4652 for (cgx = 0; cgx < hw->cgx; cgx++) {
4653 lmac_cnt = cgx_get_lmac_cnt(rvu_cgx_pdata(cgx, rvu));
4654 /* Skip when cgx is not available or lmac cnt is zero */
4657 slink = cgx * hw->lmac_per_cgx;
4659 /* Get LMAC id's from bitmap */
4660 lmac_bmap = cgx_get_lmac_bmap(rvu_cgx_pdata(cgx, rvu));
4661 for_each_set_bit(iter, &lmac_bmap, rvu->hw->lmac_per_cgx) {
4662 lmac_fifo_len = rvu_cgx_get_lmac_fifolen(rvu, cgx, iter);
4663 if (!lmac_fifo_len) {
4665 "%s: Failed to get CGX/RPM%d:LMAC%d FIFO size\n",
4666 __func__, cgx, iter);
4669 tx_credits = (lmac_fifo_len - lmac_max_frs) / 16;
4670 /* Enable credits and set credit pkt count to max allowed */
4671 cfg = (tx_credits << 12) | (0x1FF << 2) | BIT_ULL(1);
4672 cfg |= FIELD_PREP(NIX_AF_LINKX_MCS_CNT_MASK, nix_hw->cc_mcs_cnt);
4674 link = iter + slink;
4675 nix_hw->tx_credits[link] = tx_credits;
4676 rvu_write64(rvu, blkaddr,
4677 NIX_AF_TX_LINKX_NORM_CREDIT(link), cfg);
4681 /* Set Tx credits for LBK link */
4682 slink = hw->cgx_links;
4683 for (link = slink; link < (slink + hw->lbk_links); link++) {
4684 tx_credits = rvu_get_lbk_link_credits(rvu, lbk_max_frs);
4685 nix_hw->tx_credits[link] = tx_credits;
4686 /* Enable credits and set credit pkt count to max allowed */
4687 tx_credits = (tx_credits << 12) | (0x1FF << 2) | BIT_ULL(1);
4688 rvu_write64(rvu, blkaddr,
4689 NIX_AF_TX_LINKX_NORM_CREDIT(link), tx_credits);
4693 static int nix_calibrate_x2p(struct rvu *rvu, int blkaddr)
4698 /* Start X2P bus calibration */
4699 rvu_write64(rvu, blkaddr, NIX_AF_CFG,
4700 rvu_read64(rvu, blkaddr, NIX_AF_CFG) | BIT_ULL(9));
4701 /* Wait for calibration to complete */
4702 err = rvu_poll_reg(rvu, blkaddr,
4703 NIX_AF_STATUS, BIT_ULL(10), false);
4705 dev_err(rvu->dev, "NIX X2P bus calibration failed\n");
4709 status = rvu_read64(rvu, blkaddr, NIX_AF_STATUS);
4710 /* Check if CGX devices are ready */
4711 for (idx = 0; idx < rvu->cgx_cnt_max; idx++) {
4712 /* Skip when cgx port is not available */
4713 if (!rvu_cgx_pdata(idx, rvu) ||
4714 (status & (BIT_ULL(16 + idx))))
4717 "CGX%d didn't respond to NIX X2P calibration\n", idx);
4721 /* Check if LBK is ready */
4722 if (!(status & BIT_ULL(19))) {
4724 "LBK didn't respond to NIX X2P calibration\n");
4728 /* Clear 'calibrate_x2p' bit */
4729 rvu_write64(rvu, blkaddr, NIX_AF_CFG,
4730 rvu_read64(rvu, blkaddr, NIX_AF_CFG) & ~BIT_ULL(9));
4731 if (err || (status & 0x3FFULL))
4733 "NIX X2P calibration failed, status 0x%llx\n", status);
4739 static int nix_aq_init(struct rvu *rvu, struct rvu_block *block)
4744 /* Set admin queue endianness */
4745 cfg = rvu_read64(rvu, block->addr, NIX_AF_CFG);
4748 rvu_write64(rvu, block->addr, NIX_AF_CFG, cfg);
4751 rvu_write64(rvu, block->addr, NIX_AF_CFG, cfg);
4754 /* Do not bypass NDC cache */
4755 cfg = rvu_read64(rvu, block->addr, NIX_AF_NDC_CFG);
4757 #ifdef CONFIG_NDC_DIS_DYNAMIC_CACHING
4758 /* Disable caching of SQB aka SQEs */
4761 rvu_write64(rvu, block->addr, NIX_AF_NDC_CFG, cfg);
4763 /* Result structure can be followed by RQ/SQ/CQ context at
4764 * RES + 128bytes and a write mask at RES + 256 bytes, depending on
4765 * operation type. Alloc sufficient result memory for all operations.
4767 err = rvu_aq_alloc(rvu, &block->aq,
4768 Q_COUNT(AQ_SIZE), sizeof(struct nix_aq_inst_s),
4769 ALIGN(sizeof(struct nix_aq_res_s), 128) + 256);
4773 rvu_write64(rvu, block->addr, NIX_AF_AQ_CFG, AQ_SIZE);
4774 rvu_write64(rvu, block->addr,
4775 NIX_AF_AQ_BASE, (u64)block->aq->inst->iova);
4779 static void rvu_nix_setup_capabilities(struct rvu *rvu, int blkaddr)
4781 struct rvu_hwinfo *hw = rvu->hw;
4784 hw_const = rvu_read64(rvu, blkaddr, NIX_AF_CONST1);
4786 /* On OcteonTx2 DWRR quantum is directly configured into each of
4787 * the transmit scheduler queues. And PF/VF drivers were free to
4788 * config any value upto 2^24.
4789 * On CN10K, HW is modified, the quantum configuration at scheduler
4790 * queues is in terms of weight. And SW needs to setup a base DWRR MTU
4791 * at NIX_AF_DWRR_RPM_MTU / NIX_AF_DWRR_SDP_MTU. HW will do
4792 * 'DWRR MTU * weight' to get the quantum.
4794 * Check if HW uses a common MTU for all DWRR quantum configs.
4795 * On OcteonTx2 this register field is '0'.
4797 if ((((hw_const >> 56) & 0x10) == 0x10) && !(hw_const & BIT_ULL(61)))
4798 hw->cap.nix_common_dwrr_mtu = true;
4800 if (hw_const & BIT_ULL(61))
4801 hw->cap.nix_multiple_dwrr_mtu = true;
4804 static int rvu_nix_block_init(struct rvu *rvu, struct nix_hw *nix_hw)
4806 const struct npc_lt_def_cfg *ltdefs;
4807 struct rvu_hwinfo *hw = rvu->hw;
4808 int blkaddr = nix_hw->blkaddr;
4809 struct rvu_block *block;
4813 block = &hw->block[blkaddr];
4815 if (is_rvu_96xx_B0(rvu)) {
4816 /* As per a HW errata in 96xx A0/B0 silicon, NIX may corrupt
4817 * internal state when conditional clocks are turned off.
4818 * Hence enable them.
4820 rvu_write64(rvu, blkaddr, NIX_AF_CFG,
4821 rvu_read64(rvu, blkaddr, NIX_AF_CFG) | 0x40ULL);
4823 /* Set chan/link to backpressure TL3 instead of TL2 */
4824 rvu_write64(rvu, blkaddr, NIX_AF_PSE_CHANNEL_LEVEL, 0x01);
4826 /* Disable SQ manager's sticky mode operation (set TM6 = 0)
4827 * This sticky mode is known to cause SQ stalls when multiple
4828 * SQs are mapped to same SMQ and transmitting pkts at a time.
4830 cfg = rvu_read64(rvu, blkaddr, NIX_AF_SQM_DBG_CTL_STATUS);
4831 cfg &= ~BIT_ULL(15);
4832 rvu_write64(rvu, blkaddr, NIX_AF_SQM_DBG_CTL_STATUS, cfg);
4835 ltdefs = rvu->kpu.lt_def;
4836 /* Calibrate X2P bus to check if CGX/LBK links are fine */
4837 err = nix_calibrate_x2p(rvu, blkaddr);
4841 /* Setup capabilities of the NIX block */
4842 rvu_nix_setup_capabilities(rvu, blkaddr);
4844 /* Initialize admin queue */
4845 err = nix_aq_init(rvu, block);
4849 /* Restore CINT timer delay to HW reset values */
4850 rvu_write64(rvu, blkaddr, NIX_AF_CINT_DELAY, 0x0ULL);
4852 cfg = rvu_read64(rvu, blkaddr, NIX_AF_SEB_CFG);
4854 /* For better performance use NDC TX instead of NDC RX for SQ's SQEs" */
4856 if (!is_rvu_otx2(rvu))
4857 cfg |= NIX_PTP_1STEP_EN;
4859 rvu_write64(rvu, blkaddr, NIX_AF_SEB_CFG, cfg);
4861 if (!is_rvu_otx2(rvu))
4862 rvu_nix_block_cn10k_init(rvu, nix_hw);
4864 if (is_block_implemented(hw, blkaddr)) {
4865 err = nix_setup_txschq(rvu, nix_hw, blkaddr);
4869 err = nix_setup_ipolicers(rvu, nix_hw, blkaddr);
4873 err = nix_af_mark_format_setup(rvu, nix_hw, blkaddr);
4877 err = nix_setup_mcast(rvu, nix_hw, blkaddr);
4881 err = nix_setup_txvlan(rvu, nix_hw);
4885 err = nix_setup_bpids(rvu, nix_hw, blkaddr);
4889 /* Configure segmentation offload formats */
4890 nix_setup_lso(rvu, nix_hw, blkaddr);
4892 /* Config Outer/Inner L2, IP, TCP, UDP and SCTP NPC layer info.
4893 * This helps HW protocol checker to identify headers
4894 * and validate length and checksums.
4896 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_OL2,
4897 (ltdefs->rx_ol2.lid << 8) | (ltdefs->rx_ol2.ltype_match << 4) |
4898 ltdefs->rx_ol2.ltype_mask);
4899 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_OIP4,
4900 (ltdefs->rx_oip4.lid << 8) | (ltdefs->rx_oip4.ltype_match << 4) |
4901 ltdefs->rx_oip4.ltype_mask);
4902 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_IIP4,
4903 (ltdefs->rx_iip4.lid << 8) | (ltdefs->rx_iip4.ltype_match << 4) |
4904 ltdefs->rx_iip4.ltype_mask);
4905 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_OIP6,
4906 (ltdefs->rx_oip6.lid << 8) | (ltdefs->rx_oip6.ltype_match << 4) |
4907 ltdefs->rx_oip6.ltype_mask);
4908 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_IIP6,
4909 (ltdefs->rx_iip6.lid << 8) | (ltdefs->rx_iip6.ltype_match << 4) |
4910 ltdefs->rx_iip6.ltype_mask);
4911 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_OTCP,
4912 (ltdefs->rx_otcp.lid << 8) | (ltdefs->rx_otcp.ltype_match << 4) |
4913 ltdefs->rx_otcp.ltype_mask);
4914 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_ITCP,
4915 (ltdefs->rx_itcp.lid << 8) | (ltdefs->rx_itcp.ltype_match << 4) |
4916 ltdefs->rx_itcp.ltype_mask);
4917 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_OUDP,
4918 (ltdefs->rx_oudp.lid << 8) | (ltdefs->rx_oudp.ltype_match << 4) |
4919 ltdefs->rx_oudp.ltype_mask);
4920 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_IUDP,
4921 (ltdefs->rx_iudp.lid << 8) | (ltdefs->rx_iudp.ltype_match << 4) |
4922 ltdefs->rx_iudp.ltype_mask);
4923 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_OSCTP,
4924 (ltdefs->rx_osctp.lid << 8) | (ltdefs->rx_osctp.ltype_match << 4) |
4925 ltdefs->rx_osctp.ltype_mask);
4926 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_ISCTP,
4927 (ltdefs->rx_isctp.lid << 8) | (ltdefs->rx_isctp.ltype_match << 4) |
4928 ltdefs->rx_isctp.ltype_mask);
4930 if (!is_rvu_otx2(rvu)) {
4931 /* Enable APAD calculation for other protocols
4932 * matching APAD0 and APAD1 lt def registers.
4934 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_CST_APAD0,
4935 (ltdefs->rx_apad0.valid << 11) |
4936 (ltdefs->rx_apad0.lid << 8) |
4937 (ltdefs->rx_apad0.ltype_match << 4) |
4938 ltdefs->rx_apad0.ltype_mask);
4939 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_CST_APAD1,
4940 (ltdefs->rx_apad1.valid << 11) |
4941 (ltdefs->rx_apad1.lid << 8) |
4942 (ltdefs->rx_apad1.ltype_match << 4) |
4943 ltdefs->rx_apad1.ltype_mask);
4945 /* Receive ethertype defination register defines layer
4946 * information in NPC_RESULT_S to identify the Ethertype
4947 * location in L2 header. Used for Ethertype overwriting
4948 * in inline IPsec flow.
4950 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_ET(0),
4951 (ltdefs->rx_et[0].offset << 12) |
4952 (ltdefs->rx_et[0].valid << 11) |
4953 (ltdefs->rx_et[0].lid << 8) |
4954 (ltdefs->rx_et[0].ltype_match << 4) |
4955 ltdefs->rx_et[0].ltype_mask);
4956 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_ET(1),
4957 (ltdefs->rx_et[1].offset << 12) |
4958 (ltdefs->rx_et[1].valid << 11) |
4959 (ltdefs->rx_et[1].lid << 8) |
4960 (ltdefs->rx_et[1].ltype_match << 4) |
4961 ltdefs->rx_et[1].ltype_mask);
4964 err = nix_rx_flowkey_alg_cfg(rvu, blkaddr);
4968 nix_hw->tx_credits = kcalloc(hw->cgx_links + hw->lbk_links,
4969 sizeof(u64), GFP_KERNEL);
4970 if (!nix_hw->tx_credits)
4973 /* Initialize CGX/LBK/SDP link credits, min/max pkt lengths */
4974 nix_link_config(rvu, blkaddr, nix_hw);
4976 /* Enable Channel backpressure */
4977 rvu_write64(rvu, blkaddr, NIX_AF_RX_CFG, BIT_ULL(0));
4982 int rvu_nix_init(struct rvu *rvu)
4984 struct rvu_hwinfo *hw = rvu->hw;
4985 struct nix_hw *nix_hw;
4986 int blkaddr = 0, err;
4989 hw->nix = devm_kcalloc(rvu->dev, MAX_NIX_BLKS, sizeof(struct nix_hw),
4994 blkaddr = rvu_get_next_nix_blkaddr(rvu, blkaddr);
4996 nix_hw = &hw->nix[i];
4998 nix_hw->blkaddr = blkaddr;
4999 err = rvu_nix_block_init(rvu, nix_hw);
5002 blkaddr = rvu_get_next_nix_blkaddr(rvu, blkaddr);
5009 static void rvu_nix_block_freemem(struct rvu *rvu, int blkaddr,
5010 struct rvu_block *block)
5012 struct nix_txsch *txsch;
5013 struct nix_mcast *mcast;
5014 struct nix_txvlan *vlan;
5015 struct nix_hw *nix_hw;
5018 rvu_aq_free(rvu, block->aq);
5020 if (is_block_implemented(rvu->hw, blkaddr)) {
5021 nix_hw = get_nix_hw(rvu->hw, blkaddr);
5025 for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
5026 txsch = &nix_hw->txsch[lvl];
5027 kfree(txsch->schq.bmap);
5030 kfree(nix_hw->tx_credits);
5032 nix_ipolicer_freemem(rvu, nix_hw);
5034 vlan = &nix_hw->txvlan;
5035 kfree(vlan->rsrc.bmap);
5036 mutex_destroy(&vlan->rsrc_lock);
5038 mcast = &nix_hw->mcast;
5039 qmem_free(rvu->dev, mcast->mce_ctx);
5040 qmem_free(rvu->dev, mcast->mcast_buf);
5041 mutex_destroy(&mcast->mce_lock);
5045 void rvu_nix_freemem(struct rvu *rvu)
5047 struct rvu_hwinfo *hw = rvu->hw;
5048 struct rvu_block *block;
5051 blkaddr = rvu_get_next_nix_blkaddr(rvu, blkaddr);
5053 block = &hw->block[blkaddr];
5054 rvu_nix_block_freemem(rvu, blkaddr, block);
5055 blkaddr = rvu_get_next_nix_blkaddr(rvu, blkaddr);
5059 static void nix_mcast_update_action(struct rvu *rvu,
5060 struct nix_mcast_grp_elem *elem)
5062 struct npc_mcam *mcam = &rvu->hw->mcam;
5063 struct nix_rx_action rx_action = { 0 };
5064 struct nix_tx_action tx_action = { 0 };
5067 npc_blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
5068 if (elem->dir == NIX_MCAST_INGRESS) {
5069 *(u64 *)&rx_action = npc_get_mcam_action(rvu, mcam,
5072 rx_action.index = elem->mce_start_index;
5073 npc_set_mcam_action(rvu, mcam, npc_blkaddr, elem->mcam_index,
5074 *(u64 *)&rx_action);
5076 *(u64 *)&tx_action = npc_get_mcam_action(rvu, mcam,
5079 tx_action.index = elem->mce_start_index;
5080 npc_set_mcam_action(rvu, mcam, npc_blkaddr, elem->mcam_index,
5081 *(u64 *)&tx_action);
5085 static void nix_mcast_update_mce_entry(struct rvu *rvu, u16 pcifunc, u8 is_active)
5087 struct nix_mcast_grp_elem *elem;
5088 struct nix_mcast_grp *mcast_grp;
5089 struct nix_hw *nix_hw;
5092 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
5093 nix_hw = get_nix_hw(rvu->hw, blkaddr);
5097 mcast_grp = &nix_hw->mcast_grp;
5099 mutex_lock(&mcast_grp->mcast_grp_lock);
5100 list_for_each_entry(elem, &mcast_grp->mcast_grp_head, list) {
5101 struct nix_mce_list *mce_list;
5104 /* Iterate the group elements and disable the element which
5105 * received the disable request.
5107 mce_list = &elem->mcast_mce_list;
5108 hlist_for_each_entry(mce, &mce_list->head, node) {
5109 if (mce->pcifunc == pcifunc) {
5110 mce->is_active = is_active;
5115 /* Dump the updated list to HW */
5116 if (elem->dir == NIX_MCAST_INGRESS)
5117 nix_update_ingress_mce_list_hw(rvu, nix_hw, elem);
5119 nix_update_egress_mce_list_hw(rvu, nix_hw, elem);
5121 /* Update the multicast index in NPC rule */
5122 nix_mcast_update_action(rvu, elem);
5124 mutex_unlock(&mcast_grp->mcast_grp_lock);
5127 int rvu_mbox_handler_nix_lf_start_rx(struct rvu *rvu, struct msg_req *req,
5128 struct msg_rsp *rsp)
5130 u16 pcifunc = req->hdr.pcifunc;
5131 struct rvu_pfvf *pfvf;
5134 err = nix_get_nixlf(rvu, pcifunc, &nixlf, NULL);
5138 /* Enable the interface if it is in any multicast list */
5139 nix_mcast_update_mce_entry(rvu, pcifunc, 1);
5141 rvu_npc_enable_default_entries(rvu, pcifunc, nixlf);
5143 npc_mcam_enable_flows(rvu, pcifunc);
5145 pfvf = rvu_get_pfvf(rvu, pcifunc);
5146 set_bit(NIXLF_INITIALIZED, &pfvf->flags);
5148 rvu_switch_update_rules(rvu, pcifunc);
5150 return rvu_cgx_start_stop_io(rvu, pcifunc, true);
5153 int rvu_mbox_handler_nix_lf_stop_rx(struct rvu *rvu, struct msg_req *req,
5154 struct msg_rsp *rsp)
5156 u16 pcifunc = req->hdr.pcifunc;
5157 struct rvu_pfvf *pfvf;
5160 err = nix_get_nixlf(rvu, pcifunc, &nixlf, NULL);
5164 rvu_npc_disable_mcam_entries(rvu, pcifunc, nixlf);
5165 /* Disable the interface if it is in any multicast list */
5166 nix_mcast_update_mce_entry(rvu, pcifunc, 0);
5169 pfvf = rvu_get_pfvf(rvu, pcifunc);
5170 clear_bit(NIXLF_INITIALIZED, &pfvf->flags);
5172 err = rvu_cgx_start_stop_io(rvu, pcifunc, false);
5176 rvu_cgx_tx_enable(rvu, pcifunc, true);
5181 #define RX_SA_BASE GENMASK_ULL(52, 7)
5183 void rvu_nix_lf_teardown(struct rvu *rvu, u16 pcifunc, int blkaddr, int nixlf)
5185 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
5186 struct hwctx_disable_req ctx_req;
5187 int pf = rvu_get_pf(pcifunc);
5188 struct mac_ops *mac_ops;
5194 ctx_req.hdr.pcifunc = pcifunc;
5196 /* Cleanup NPC MCAM entries, free Tx scheduler queues being used */
5197 rvu_npc_disable_mcam_entries(rvu, pcifunc, nixlf);
5198 rvu_npc_free_mcam_entries(rvu, pcifunc, nixlf);
5199 nix_interface_deinit(rvu, pcifunc, nixlf);
5200 nix_rx_sync(rvu, blkaddr);
5201 nix_txschq_free(rvu, pcifunc);
5203 clear_bit(NIXLF_INITIALIZED, &pfvf->flags);
5205 rvu_cgx_start_stop_io(rvu, pcifunc, false);
5208 ctx_req.ctype = NIX_AQ_CTYPE_SQ;
5209 err = nix_lf_hwctx_disable(rvu, &ctx_req);
5211 dev_err(rvu->dev, "SQ ctx disable failed\n");
5215 ctx_req.ctype = NIX_AQ_CTYPE_RQ;
5216 err = nix_lf_hwctx_disable(rvu, &ctx_req);
5218 dev_err(rvu->dev, "RQ ctx disable failed\n");
5222 ctx_req.ctype = NIX_AQ_CTYPE_CQ;
5223 err = nix_lf_hwctx_disable(rvu, &ctx_req);
5225 dev_err(rvu->dev, "CQ ctx disable failed\n");
5228 /* reset HW config done for Switch headers */
5229 rvu_npc_set_parse_mode(rvu, pcifunc, OTX2_PRIV_FLAGS_DEFAULT,
5230 (PKIND_TX | PKIND_RX), 0, 0, 0, 0);
5232 /* Disabling CGX and NPC config done for PTP */
5233 if (pfvf->hw_rx_tstamp_en) {
5234 rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
5235 cgxd = rvu_cgx_pdata(cgx_id, rvu);
5236 mac_ops = get_mac_ops(cgxd);
5237 mac_ops->mac_enadis_ptp_config(cgxd, lmac_id, false);
5238 /* Undo NPC config done for PTP */
5239 if (npc_config_ts_kpuaction(rvu, pf, pcifunc, false))
5240 dev_err(rvu->dev, "NPC config for PTP failed\n");
5241 pfvf->hw_rx_tstamp_en = false;
5244 /* reset priority flow control config */
5245 rvu_cgx_prio_flow_ctrl_cfg(rvu, pcifunc, 0, 0, 0);
5247 /* reset 802.3x flow control config */
5248 rvu_cgx_cfg_pause_frm(rvu, pcifunc, 0, 0);
5250 nix_ctx_free(rvu, pfvf);
5252 nix_free_all_bandprof(rvu, pcifunc);
5254 sa_base = rvu_read64(rvu, blkaddr, NIX_AF_LFX_RX_IPSEC_SA_BASE(nixlf));
5255 if (FIELD_GET(RX_SA_BASE, sa_base)) {
5256 err = rvu_cpt_ctx_flush(rvu, pcifunc);
5259 "CPT ctx flush failed with error: %d\n", err);
5263 #define NIX_AF_LFX_TX_CFG_PTP_EN BIT_ULL(32)
5265 static int rvu_nix_lf_ptp_tx_cfg(struct rvu *rvu, u16 pcifunc, bool enable)
5267 struct rvu_hwinfo *hw = rvu->hw;
5268 struct rvu_block *block;
5273 pf = rvu_get_pf(pcifunc);
5274 if (!is_mac_feature_supported(rvu, pf, RVU_LMAC_FEAT_PTP))
5277 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
5279 return NIX_AF_ERR_AF_LF_INVALID;
5281 block = &hw->block[blkaddr];
5282 nixlf = rvu_get_lf(rvu, block, pcifunc, 0);
5284 return NIX_AF_ERR_AF_LF_INVALID;
5286 cfg = rvu_read64(rvu, blkaddr, NIX_AF_LFX_TX_CFG(nixlf));
5289 cfg |= NIX_AF_LFX_TX_CFG_PTP_EN;
5291 cfg &= ~NIX_AF_LFX_TX_CFG_PTP_EN;
5293 rvu_write64(rvu, blkaddr, NIX_AF_LFX_TX_CFG(nixlf), cfg);
5298 int rvu_mbox_handler_nix_lf_ptp_tx_enable(struct rvu *rvu, struct msg_req *req,
5299 struct msg_rsp *rsp)
5301 return rvu_nix_lf_ptp_tx_cfg(rvu, req->hdr.pcifunc, true);
5304 int rvu_mbox_handler_nix_lf_ptp_tx_disable(struct rvu *rvu, struct msg_req *req,
5305 struct msg_rsp *rsp)
5307 return rvu_nix_lf_ptp_tx_cfg(rvu, req->hdr.pcifunc, false);
5310 int rvu_mbox_handler_nix_lso_format_cfg(struct rvu *rvu,
5311 struct nix_lso_format_cfg *req,
5312 struct nix_lso_format_cfg_rsp *rsp)
5314 u16 pcifunc = req->hdr.pcifunc;
5315 struct nix_hw *nix_hw;
5316 struct rvu_pfvf *pfvf;
5317 int blkaddr, idx, f;
5320 pfvf = rvu_get_pfvf(rvu, pcifunc);
5321 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
5322 if (!pfvf->nixlf || blkaddr < 0)
5323 return NIX_AF_ERR_AF_LF_INVALID;
5325 nix_hw = get_nix_hw(rvu->hw, blkaddr);
5327 return NIX_AF_ERR_INVALID_NIXBLK;
5329 /* Find existing matching LSO format, if any */
5330 for (idx = 0; idx < nix_hw->lso.in_use; idx++) {
5331 for (f = 0; f < NIX_LSO_FIELD_MAX; f++) {
5332 reg = rvu_read64(rvu, blkaddr,
5333 NIX_AF_LSO_FORMATX_FIELDX(idx, f));
5334 if (req->fields[f] != (reg & req->field_mask))
5338 if (f == NIX_LSO_FIELD_MAX)
5342 if (idx < nix_hw->lso.in_use) {
5344 rsp->lso_format_idx = idx;
5348 if (nix_hw->lso.in_use == nix_hw->lso.total)
5349 return NIX_AF_ERR_LSO_CFG_FAIL;
5351 rsp->lso_format_idx = nix_hw->lso.in_use++;
5353 for (f = 0; f < NIX_LSO_FIELD_MAX; f++)
5354 rvu_write64(rvu, blkaddr,
5355 NIX_AF_LSO_FORMATX_FIELDX(rsp->lso_format_idx, f),
5361 #define IPSEC_GEN_CFG_EGRP GENMASK_ULL(50, 48)
5362 #define IPSEC_GEN_CFG_OPCODE GENMASK_ULL(47, 32)
5363 #define IPSEC_GEN_CFG_PARAM1 GENMASK_ULL(31, 16)
5364 #define IPSEC_GEN_CFG_PARAM2 GENMASK_ULL(15, 0)
5366 #define CPT_INST_QSEL_BLOCK GENMASK_ULL(28, 24)
5367 #define CPT_INST_QSEL_PF_FUNC GENMASK_ULL(23, 8)
5368 #define CPT_INST_QSEL_SLOT GENMASK_ULL(7, 0)
5370 #define CPT_INST_CREDIT_TH GENMASK_ULL(53, 32)
5371 #define CPT_INST_CREDIT_BPID GENMASK_ULL(30, 22)
5372 #define CPT_INST_CREDIT_CNT GENMASK_ULL(21, 0)
5374 static void nix_inline_ipsec_cfg(struct rvu *rvu, struct nix_inline_ipsec_cfg *req,
5377 u8 cpt_idx, cpt_blkaddr;
5380 cpt_idx = (blkaddr == BLKADDR_NIX0) ? 0 : 1;
5383 /* Enable context prefetching */
5384 if (!is_rvu_otx2(rvu))
5387 /* Set OPCODE and EGRP */
5388 val |= FIELD_PREP(IPSEC_GEN_CFG_EGRP, req->gen_cfg.egrp);
5389 val |= FIELD_PREP(IPSEC_GEN_CFG_OPCODE, req->gen_cfg.opcode);
5390 val |= FIELD_PREP(IPSEC_GEN_CFG_PARAM1, req->gen_cfg.param1);
5391 val |= FIELD_PREP(IPSEC_GEN_CFG_PARAM2, req->gen_cfg.param2);
5393 rvu_write64(rvu, blkaddr, NIX_AF_RX_IPSEC_GEN_CFG, val);
5395 /* Set CPT queue for inline IPSec */
5396 val = FIELD_PREP(CPT_INST_QSEL_SLOT, req->inst_qsel.cpt_slot);
5397 val |= FIELD_PREP(CPT_INST_QSEL_PF_FUNC,
5398 req->inst_qsel.cpt_pf_func);
5400 if (!is_rvu_otx2(rvu)) {
5401 cpt_blkaddr = (cpt_idx == 0) ? BLKADDR_CPT0 :
5403 val |= FIELD_PREP(CPT_INST_QSEL_BLOCK, cpt_blkaddr);
5406 rvu_write64(rvu, blkaddr, NIX_AF_RX_CPTX_INST_QSEL(cpt_idx),
5409 /* Set CPT credit */
5410 val = rvu_read64(rvu, blkaddr, NIX_AF_RX_CPTX_CREDIT(cpt_idx));
5411 if ((val & 0x3FFFFF) != 0x3FFFFF)
5412 rvu_write64(rvu, blkaddr, NIX_AF_RX_CPTX_CREDIT(cpt_idx),
5415 val = FIELD_PREP(CPT_INST_CREDIT_CNT, req->cpt_credit);
5416 val |= FIELD_PREP(CPT_INST_CREDIT_BPID, req->bpid);
5417 val |= FIELD_PREP(CPT_INST_CREDIT_TH, req->credit_th);
5418 rvu_write64(rvu, blkaddr, NIX_AF_RX_CPTX_CREDIT(cpt_idx), val);
5420 rvu_write64(rvu, blkaddr, NIX_AF_RX_IPSEC_GEN_CFG, 0x0);
5421 rvu_write64(rvu, blkaddr, NIX_AF_RX_CPTX_INST_QSEL(cpt_idx),
5423 val = rvu_read64(rvu, blkaddr, NIX_AF_RX_CPTX_CREDIT(cpt_idx));
5424 if ((val & 0x3FFFFF) != 0x3FFFFF)
5425 rvu_write64(rvu, blkaddr, NIX_AF_RX_CPTX_CREDIT(cpt_idx),
5430 int rvu_mbox_handler_nix_inline_ipsec_cfg(struct rvu *rvu,
5431 struct nix_inline_ipsec_cfg *req,
5432 struct msg_rsp *rsp)
5434 if (!is_block_implemented(rvu->hw, BLKADDR_CPT0))
5437 nix_inline_ipsec_cfg(rvu, req, BLKADDR_NIX0);
5438 if (is_block_implemented(rvu->hw, BLKADDR_CPT1))
5439 nix_inline_ipsec_cfg(rvu, req, BLKADDR_NIX1);
5444 int rvu_mbox_handler_nix_read_inline_ipsec_cfg(struct rvu *rvu,
5445 struct msg_req *req,
5446 struct nix_inline_ipsec_cfg *rsp)
5451 if (!is_block_implemented(rvu->hw, BLKADDR_CPT0))
5454 val = rvu_read64(rvu, BLKADDR_NIX0, NIX_AF_RX_IPSEC_GEN_CFG);
5455 rsp->gen_cfg.egrp = FIELD_GET(IPSEC_GEN_CFG_EGRP, val);
5456 rsp->gen_cfg.opcode = FIELD_GET(IPSEC_GEN_CFG_OPCODE, val);
5457 rsp->gen_cfg.param1 = FIELD_GET(IPSEC_GEN_CFG_PARAM1, val);
5458 rsp->gen_cfg.param2 = FIELD_GET(IPSEC_GEN_CFG_PARAM2, val);
5460 val = rvu_read64(rvu, BLKADDR_NIX0, NIX_AF_RX_CPTX_CREDIT(0));
5461 rsp->cpt_credit = FIELD_GET(CPT_INST_CREDIT_CNT, val);
5462 rsp->credit_th = FIELD_GET(CPT_INST_CREDIT_TH, val);
5463 rsp->bpid = FIELD_GET(CPT_INST_CREDIT_BPID, val);
5468 int rvu_mbox_handler_nix_inline_ipsec_lf_cfg(struct rvu *rvu,
5469 struct nix_inline_ipsec_lf_cfg *req,
5470 struct msg_rsp *rsp)
5472 int lf, blkaddr, err;
5475 if (!is_block_implemented(rvu->hw, BLKADDR_CPT0))
5478 err = nix_get_nixlf(rvu, req->hdr.pcifunc, &lf, &blkaddr);
5483 /* Set TT, TAG_CONST, SA_POW2_SIZE and LENM1_MAX */
5484 val = (u64)req->ipsec_cfg0.tt << 44 |
5485 (u64)req->ipsec_cfg0.tag_const << 20 |
5486 (u64)req->ipsec_cfg0.sa_pow2_size << 16 |
5487 req->ipsec_cfg0.lenm1_max;
5489 if (blkaddr == BLKADDR_NIX1)
5492 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RX_IPSEC_CFG0(lf), val);
5494 /* Set SA_IDX_W and SA_IDX_MAX */
5495 val = (u64)req->ipsec_cfg1.sa_idx_w << 32 |
5496 req->ipsec_cfg1.sa_idx_max;
5497 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RX_IPSEC_CFG1(lf), val);
5499 /* Set SA base address */
5500 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RX_IPSEC_SA_BASE(lf),
5503 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RX_IPSEC_CFG0(lf), 0x0);
5504 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RX_IPSEC_CFG1(lf), 0x0);
5505 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RX_IPSEC_SA_BASE(lf),
5512 void rvu_nix_reset_mac(struct rvu_pfvf *pfvf, int pcifunc)
5514 bool from_vf = !!(pcifunc & RVU_PFVF_FUNC_MASK);
5516 /* overwrite vf mac address with default_mac */
5518 ether_addr_copy(pfvf->mac_addr, pfvf->default_mac);
5521 /* NIX ingress policers or bandwidth profiles APIs */
5522 static void nix_config_rx_pkt_policer_precolor(struct rvu *rvu, int blkaddr)
5524 struct npc_lt_def_cfg defs, *ltdefs;
5527 memcpy(ltdefs, rvu->kpu.lt_def, sizeof(struct npc_lt_def_cfg));
5529 /* Extract PCP and DEI fields from outer VLAN from byte offset
5530 * 2 from the start of LB_PTR (ie TAG).
5531 * VLAN0 is Outer VLAN and VLAN1 is Inner VLAN. Inner VLAN
5532 * fields are considered when 'Tunnel enable' is set in profile.
5534 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_VLAN0_PCP_DEI,
5535 (2UL << 12) | (ltdefs->ovlan.lid << 8) |
5536 (ltdefs->ovlan.ltype_match << 4) |
5537 ltdefs->ovlan.ltype_mask);
5538 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_VLAN1_PCP_DEI,
5539 (2UL << 12) | (ltdefs->ivlan.lid << 8) |
5540 (ltdefs->ivlan.ltype_match << 4) |
5541 ltdefs->ivlan.ltype_mask);
5543 /* DSCP field in outer and tunneled IPv4 packets */
5544 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_OIP4_DSCP,
5545 (1UL << 12) | (ltdefs->rx_oip4.lid << 8) |
5546 (ltdefs->rx_oip4.ltype_match << 4) |
5547 ltdefs->rx_oip4.ltype_mask);
5548 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_IIP4_DSCP,
5549 (1UL << 12) | (ltdefs->rx_iip4.lid << 8) |
5550 (ltdefs->rx_iip4.ltype_match << 4) |
5551 ltdefs->rx_iip4.ltype_mask);
5553 /* DSCP field (traffic class) in outer and tunneled IPv6 packets */
5554 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_OIP6_DSCP,
5555 (1UL << 11) | (ltdefs->rx_oip6.lid << 8) |
5556 (ltdefs->rx_oip6.ltype_match << 4) |
5557 ltdefs->rx_oip6.ltype_mask);
5558 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_IIP6_DSCP,
5559 (1UL << 11) | (ltdefs->rx_iip6.lid << 8) |
5560 (ltdefs->rx_iip6.ltype_match << 4) |
5561 ltdefs->rx_iip6.ltype_mask);
5564 static int nix_init_policer_context(struct rvu *rvu, struct nix_hw *nix_hw,
5565 int layer, int prof_idx)
5567 struct nix_cn10k_aq_enq_req aq_req;
5570 memset(&aq_req, 0, sizeof(struct nix_cn10k_aq_enq_req));
5572 aq_req.qidx = (prof_idx & 0x3FFF) | (layer << 14);
5573 aq_req.ctype = NIX_AQ_CTYPE_BANDPROF;
5574 aq_req.op = NIX_AQ_INSTOP_INIT;
5576 /* Context is all zeros, submit to AQ */
5577 rc = rvu_nix_blk_aq_enq_inst(rvu, nix_hw,
5578 (struct nix_aq_enq_req *)&aq_req, NULL);
5580 dev_err(rvu->dev, "Failed to INIT bandwidth profile layer %d profile %d\n",
5585 static int nix_setup_ipolicers(struct rvu *rvu,
5586 struct nix_hw *nix_hw, int blkaddr)
5588 struct rvu_hwinfo *hw = rvu->hw;
5589 struct nix_ipolicer *ipolicer;
5590 int err, layer, prof_idx;
5593 cfg = rvu_read64(rvu, blkaddr, NIX_AF_CONST);
5594 if (!(cfg & BIT_ULL(61))) {
5595 hw->cap.ipolicer = false;
5599 hw->cap.ipolicer = true;
5600 nix_hw->ipolicer = devm_kcalloc(rvu->dev, BAND_PROF_NUM_LAYERS,
5601 sizeof(*ipolicer), GFP_KERNEL);
5602 if (!nix_hw->ipolicer)
5605 cfg = rvu_read64(rvu, blkaddr, NIX_AF_PL_CONST);
5607 for (layer = 0; layer < BAND_PROF_NUM_LAYERS; layer++) {
5608 ipolicer = &nix_hw->ipolicer[layer];
5610 case BAND_PROF_LEAF_LAYER:
5611 ipolicer->band_prof.max = cfg & 0XFFFF;
5613 case BAND_PROF_MID_LAYER:
5614 ipolicer->band_prof.max = (cfg >> 16) & 0XFFFF;
5616 case BAND_PROF_TOP_LAYER:
5617 ipolicer->band_prof.max = (cfg >> 32) & 0XFFFF;
5621 if (!ipolicer->band_prof.max)
5624 err = rvu_alloc_bitmap(&ipolicer->band_prof);
5628 ipolicer->pfvf_map = devm_kcalloc(rvu->dev,
5629 ipolicer->band_prof.max,
5630 sizeof(u16), GFP_KERNEL);
5631 if (!ipolicer->pfvf_map)
5634 ipolicer->match_id = devm_kcalloc(rvu->dev,
5635 ipolicer->band_prof.max,
5636 sizeof(u16), GFP_KERNEL);
5637 if (!ipolicer->match_id)
5641 prof_idx < ipolicer->band_prof.max; prof_idx++) {
5642 /* Set AF as current owner for INIT ops to succeed */
5643 ipolicer->pfvf_map[prof_idx] = 0x00;
5645 /* There is no enable bit in the profile context,
5646 * so no context disable. So let's INIT them here
5647 * so that PF/VF later on have to just do WRITE to
5648 * setup policer rates and config.
5650 err = nix_init_policer_context(rvu, nix_hw,
5656 /* Allocate memory for maintaining ref_counts for MID level
5657 * profiles, this will be needed for leaf layer profiles'
5660 if (layer != BAND_PROF_MID_LAYER)
5663 ipolicer->ref_count = devm_kcalloc(rvu->dev,
5664 ipolicer->band_prof.max,
5665 sizeof(u16), GFP_KERNEL);
5666 if (!ipolicer->ref_count)
5670 /* Set policer timeunit to 2us ie (19 + 1) * 100 nsec = 2us */
5671 rvu_write64(rvu, blkaddr, NIX_AF_PL_TS, 19);
5673 nix_config_rx_pkt_policer_precolor(rvu, blkaddr);
5678 static void nix_ipolicer_freemem(struct rvu *rvu, struct nix_hw *nix_hw)
5680 struct nix_ipolicer *ipolicer;
5683 if (!rvu->hw->cap.ipolicer)
5686 for (layer = 0; layer < BAND_PROF_NUM_LAYERS; layer++) {
5687 ipolicer = &nix_hw->ipolicer[layer];
5689 if (!ipolicer->band_prof.max)
5692 kfree(ipolicer->band_prof.bmap);
5696 static int nix_verify_bandprof(struct nix_cn10k_aq_enq_req *req,
5697 struct nix_hw *nix_hw, u16 pcifunc)
5699 struct nix_ipolicer *ipolicer;
5700 int layer, hi_layer, prof_idx;
5702 /* Bits [15:14] in profile index represent layer */
5703 layer = (req->qidx >> 14) & 0x03;
5704 prof_idx = req->qidx & 0x3FFF;
5706 ipolicer = &nix_hw->ipolicer[layer];
5707 if (prof_idx >= ipolicer->band_prof.max)
5710 /* Check if the profile is allocated to the requesting PCIFUNC or not
5711 * with the exception of AF. AF is allowed to read and update contexts.
5713 if (pcifunc && ipolicer->pfvf_map[prof_idx] != pcifunc)
5716 /* If this profile is linked to higher layer profile then check
5717 * if that profile is also allocated to the requesting PCIFUNC
5720 if (!req->prof.hl_en)
5723 /* Leaf layer profile can link only to mid layer and
5724 * mid layer to top layer.
5726 if (layer == BAND_PROF_LEAF_LAYER)
5727 hi_layer = BAND_PROF_MID_LAYER;
5728 else if (layer == BAND_PROF_MID_LAYER)
5729 hi_layer = BAND_PROF_TOP_LAYER;
5733 ipolicer = &nix_hw->ipolicer[hi_layer];
5734 prof_idx = req->prof.band_prof_id;
5735 if (prof_idx >= ipolicer->band_prof.max ||
5736 ipolicer->pfvf_map[prof_idx] != pcifunc)
5742 int rvu_mbox_handler_nix_bandprof_alloc(struct rvu *rvu,
5743 struct nix_bandprof_alloc_req *req,
5744 struct nix_bandprof_alloc_rsp *rsp)
5746 int blkaddr, layer, prof, idx, err;
5747 u16 pcifunc = req->hdr.pcifunc;
5748 struct nix_ipolicer *ipolicer;
5749 struct nix_hw *nix_hw;
5751 if (!rvu->hw->cap.ipolicer)
5752 return NIX_AF_ERR_IPOLICER_NOTSUPP;
5754 err = nix_get_struct_ptrs(rvu, pcifunc, &nix_hw, &blkaddr);
5758 mutex_lock(&rvu->rsrc_lock);
5759 for (layer = 0; layer < BAND_PROF_NUM_LAYERS; layer++) {
5760 if (layer == BAND_PROF_INVAL_LAYER)
5762 if (!req->prof_count[layer])
5765 ipolicer = &nix_hw->ipolicer[layer];
5766 for (idx = 0; idx < req->prof_count[layer]; idx++) {
5767 /* Allocate a max of 'MAX_BANDPROF_PER_PFFUNC' profiles */
5768 if (idx == MAX_BANDPROF_PER_PFFUNC)
5771 prof = rvu_alloc_rsrc(&ipolicer->band_prof);
5774 rsp->prof_count[layer]++;
5775 rsp->prof_idx[layer][idx] = prof;
5776 ipolicer->pfvf_map[prof] = pcifunc;
5779 mutex_unlock(&rvu->rsrc_lock);
5783 static int nix_free_all_bandprof(struct rvu *rvu, u16 pcifunc)
5785 int blkaddr, layer, prof_idx, err;
5786 struct nix_ipolicer *ipolicer;
5787 struct nix_hw *nix_hw;
5789 if (!rvu->hw->cap.ipolicer)
5790 return NIX_AF_ERR_IPOLICER_NOTSUPP;
5792 err = nix_get_struct_ptrs(rvu, pcifunc, &nix_hw, &blkaddr);
5796 mutex_lock(&rvu->rsrc_lock);
5797 /* Free all the profiles allocated to the PCIFUNC */
5798 for (layer = 0; layer < BAND_PROF_NUM_LAYERS; layer++) {
5799 if (layer == BAND_PROF_INVAL_LAYER)
5801 ipolicer = &nix_hw->ipolicer[layer];
5803 for (prof_idx = 0; prof_idx < ipolicer->band_prof.max; prof_idx++) {
5804 if (ipolicer->pfvf_map[prof_idx] != pcifunc)
5807 /* Clear ratelimit aggregation, if any */
5808 if (layer == BAND_PROF_LEAF_LAYER &&
5809 ipolicer->match_id[prof_idx])
5810 nix_clear_ratelimit_aggr(rvu, nix_hw, prof_idx);
5812 ipolicer->pfvf_map[prof_idx] = 0x00;
5813 ipolicer->match_id[prof_idx] = 0;
5814 rvu_free_rsrc(&ipolicer->band_prof, prof_idx);
5817 mutex_unlock(&rvu->rsrc_lock);
5821 int rvu_mbox_handler_nix_bandprof_free(struct rvu *rvu,
5822 struct nix_bandprof_free_req *req,
5823 struct msg_rsp *rsp)
5825 int blkaddr, layer, prof_idx, idx, err;
5826 u16 pcifunc = req->hdr.pcifunc;
5827 struct nix_ipolicer *ipolicer;
5828 struct nix_hw *nix_hw;
5831 return nix_free_all_bandprof(rvu, pcifunc);
5833 if (!rvu->hw->cap.ipolicer)
5834 return NIX_AF_ERR_IPOLICER_NOTSUPP;
5836 err = nix_get_struct_ptrs(rvu, pcifunc, &nix_hw, &blkaddr);
5840 mutex_lock(&rvu->rsrc_lock);
5841 /* Free the requested profile indices */
5842 for (layer = 0; layer < BAND_PROF_NUM_LAYERS; layer++) {
5843 if (layer == BAND_PROF_INVAL_LAYER)
5845 if (!req->prof_count[layer])
5848 ipolicer = &nix_hw->ipolicer[layer];
5849 for (idx = 0; idx < req->prof_count[layer]; idx++) {
5850 if (idx == MAX_BANDPROF_PER_PFFUNC)
5852 prof_idx = req->prof_idx[layer][idx];
5853 if (prof_idx >= ipolicer->band_prof.max ||
5854 ipolicer->pfvf_map[prof_idx] != pcifunc)
5857 /* Clear ratelimit aggregation, if any */
5858 if (layer == BAND_PROF_LEAF_LAYER &&
5859 ipolicer->match_id[prof_idx])
5860 nix_clear_ratelimit_aggr(rvu, nix_hw, prof_idx);
5862 ipolicer->pfvf_map[prof_idx] = 0x00;
5863 ipolicer->match_id[prof_idx] = 0;
5864 rvu_free_rsrc(&ipolicer->band_prof, prof_idx);
5867 mutex_unlock(&rvu->rsrc_lock);
5871 int nix_aq_context_read(struct rvu *rvu, struct nix_hw *nix_hw,
5872 struct nix_cn10k_aq_enq_req *aq_req,
5873 struct nix_cn10k_aq_enq_rsp *aq_rsp,
5874 u16 pcifunc, u8 ctype, u32 qidx)
5876 memset(aq_req, 0, sizeof(struct nix_cn10k_aq_enq_req));
5877 aq_req->hdr.pcifunc = pcifunc;
5878 aq_req->ctype = ctype;
5879 aq_req->op = NIX_AQ_INSTOP_READ;
5880 aq_req->qidx = qidx;
5882 return rvu_nix_blk_aq_enq_inst(rvu, nix_hw,
5883 (struct nix_aq_enq_req *)aq_req,
5884 (struct nix_aq_enq_rsp *)aq_rsp);
5887 static int nix_ipolicer_map_leaf_midprofs(struct rvu *rvu,
5888 struct nix_hw *nix_hw,
5889 struct nix_cn10k_aq_enq_req *aq_req,
5890 struct nix_cn10k_aq_enq_rsp *aq_rsp,
5891 u32 leaf_prof, u16 mid_prof)
5893 memset(aq_req, 0, sizeof(struct nix_cn10k_aq_enq_req));
5894 aq_req->hdr.pcifunc = 0x00;
5895 aq_req->ctype = NIX_AQ_CTYPE_BANDPROF;
5896 aq_req->op = NIX_AQ_INSTOP_WRITE;
5897 aq_req->qidx = leaf_prof;
5899 aq_req->prof.band_prof_id = mid_prof;
5900 aq_req->prof_mask.band_prof_id = GENMASK(6, 0);
5901 aq_req->prof.hl_en = 1;
5902 aq_req->prof_mask.hl_en = 1;
5904 return rvu_nix_blk_aq_enq_inst(rvu, nix_hw,
5905 (struct nix_aq_enq_req *)aq_req,
5906 (struct nix_aq_enq_rsp *)aq_rsp);
5909 int rvu_nix_setup_ratelimit_aggr(struct rvu *rvu, u16 pcifunc,
5910 u16 rq_idx, u16 match_id)
5912 int leaf_prof, mid_prof, leaf_match;
5913 struct nix_cn10k_aq_enq_req aq_req;
5914 struct nix_cn10k_aq_enq_rsp aq_rsp;
5915 struct nix_ipolicer *ipolicer;
5916 struct nix_hw *nix_hw;
5917 int blkaddr, idx, rc;
5919 if (!rvu->hw->cap.ipolicer)
5922 rc = nix_get_struct_ptrs(rvu, pcifunc, &nix_hw, &blkaddr);
5926 /* Fetch the RQ's context to see if policing is enabled */
5927 rc = nix_aq_context_read(rvu, nix_hw, &aq_req, &aq_rsp, pcifunc,
5928 NIX_AQ_CTYPE_RQ, rq_idx);
5931 "%s: Failed to fetch RQ%d context of PFFUNC 0x%x\n",
5932 __func__, rq_idx, pcifunc);
5936 if (!aq_rsp.rq.policer_ena)
5939 /* Get the bandwidth profile ID mapped to this RQ */
5940 leaf_prof = aq_rsp.rq.band_prof_id;
5942 ipolicer = &nix_hw->ipolicer[BAND_PROF_LEAF_LAYER];
5943 ipolicer->match_id[leaf_prof] = match_id;
5945 /* Check if any other leaf profile is marked with same match_id */
5946 for (idx = 0; idx < ipolicer->band_prof.max; idx++) {
5947 if (idx == leaf_prof)
5949 if (ipolicer->match_id[idx] != match_id)
5956 if (idx == ipolicer->band_prof.max)
5959 /* Fetch the matching profile's context to check if it's already
5960 * mapped to a mid level profile.
5962 rc = nix_aq_context_read(rvu, nix_hw, &aq_req, &aq_rsp, 0x00,
5963 NIX_AQ_CTYPE_BANDPROF, leaf_match);
5966 "%s: Failed to fetch context of leaf profile %d\n",
5967 __func__, leaf_match);
5971 ipolicer = &nix_hw->ipolicer[BAND_PROF_MID_LAYER];
5972 if (aq_rsp.prof.hl_en) {
5973 /* Get Mid layer prof index and map leaf_prof index
5974 * also such that flows that are being steered
5975 * to different RQs and marked with same match_id
5976 * are rate limited in a aggregate fashion
5978 mid_prof = aq_rsp.prof.band_prof_id;
5979 rc = nix_ipolicer_map_leaf_midprofs(rvu, nix_hw,
5981 leaf_prof, mid_prof);
5984 "%s: Failed to map leaf(%d) and mid(%d) profiles\n",
5985 __func__, leaf_prof, mid_prof);
5989 mutex_lock(&rvu->rsrc_lock);
5990 ipolicer->ref_count[mid_prof]++;
5991 mutex_unlock(&rvu->rsrc_lock);
5995 /* Allocate a mid layer profile and
5996 * map both 'leaf_prof' and 'leaf_match' profiles to it.
5998 mutex_lock(&rvu->rsrc_lock);
5999 mid_prof = rvu_alloc_rsrc(&ipolicer->band_prof);
6002 "%s: Unable to allocate mid layer profile\n", __func__);
6003 mutex_unlock(&rvu->rsrc_lock);
6006 mutex_unlock(&rvu->rsrc_lock);
6007 ipolicer->pfvf_map[mid_prof] = 0x00;
6008 ipolicer->ref_count[mid_prof] = 0;
6010 /* Initialize mid layer profile same as 'leaf_prof' */
6011 rc = nix_aq_context_read(rvu, nix_hw, &aq_req, &aq_rsp, 0x00,
6012 NIX_AQ_CTYPE_BANDPROF, leaf_prof);
6015 "%s: Failed to fetch context of leaf profile %d\n",
6016 __func__, leaf_prof);
6020 memset(&aq_req, 0, sizeof(struct nix_cn10k_aq_enq_req));
6021 aq_req.hdr.pcifunc = 0x00;
6022 aq_req.qidx = (mid_prof & 0x3FFF) | (BAND_PROF_MID_LAYER << 14);
6023 aq_req.ctype = NIX_AQ_CTYPE_BANDPROF;
6024 aq_req.op = NIX_AQ_INSTOP_WRITE;
6025 memcpy(&aq_req.prof, &aq_rsp.prof, sizeof(struct nix_bandprof_s));
6026 memset((char *)&aq_req.prof_mask, 0xff, sizeof(struct nix_bandprof_s));
6027 /* Clear higher layer enable bit in the mid profile, just in case */
6028 aq_req.prof.hl_en = 0;
6029 aq_req.prof_mask.hl_en = 1;
6031 rc = rvu_nix_blk_aq_enq_inst(rvu, nix_hw,
6032 (struct nix_aq_enq_req *)&aq_req, NULL);
6035 "%s: Failed to INIT context of mid layer profile %d\n",
6036 __func__, mid_prof);
6040 /* Map both leaf profiles to this mid layer profile */
6041 rc = nix_ipolicer_map_leaf_midprofs(rvu, nix_hw,
6043 leaf_prof, mid_prof);
6046 "%s: Failed to map leaf(%d) and mid(%d) profiles\n",
6047 __func__, leaf_prof, mid_prof);
6051 mutex_lock(&rvu->rsrc_lock);
6052 ipolicer->ref_count[mid_prof]++;
6053 mutex_unlock(&rvu->rsrc_lock);
6055 rc = nix_ipolicer_map_leaf_midprofs(rvu, nix_hw,
6057 leaf_match, mid_prof);
6060 "%s: Failed to map leaf(%d) and mid(%d) profiles\n",
6061 __func__, leaf_match, mid_prof);
6062 ipolicer->ref_count[mid_prof]--;
6066 mutex_lock(&rvu->rsrc_lock);
6067 ipolicer->ref_count[mid_prof]++;
6068 mutex_unlock(&rvu->rsrc_lock);
6074 /* Called with mutex rsrc_lock */
6075 static void nix_clear_ratelimit_aggr(struct rvu *rvu, struct nix_hw *nix_hw,
6078 struct nix_cn10k_aq_enq_req aq_req;
6079 struct nix_cn10k_aq_enq_rsp aq_rsp;
6080 struct nix_ipolicer *ipolicer;
6084 mutex_unlock(&rvu->rsrc_lock);
6086 rc = nix_aq_context_read(rvu, nix_hw, &aq_req, &aq_rsp, 0x00,
6087 NIX_AQ_CTYPE_BANDPROF, leaf_prof);
6089 mutex_lock(&rvu->rsrc_lock);
6092 "%s: Failed to fetch context of leaf profile %d\n",
6093 __func__, leaf_prof);
6097 if (!aq_rsp.prof.hl_en)
6100 mid_prof = aq_rsp.prof.band_prof_id;
6101 ipolicer = &nix_hw->ipolicer[BAND_PROF_MID_LAYER];
6102 ipolicer->ref_count[mid_prof]--;
6103 /* If ref_count is zero, free mid layer profile */
6104 if (!ipolicer->ref_count[mid_prof]) {
6105 ipolicer->pfvf_map[mid_prof] = 0x00;
6106 rvu_free_rsrc(&ipolicer->band_prof, mid_prof);
6110 int rvu_mbox_handler_nix_bandprof_get_hwinfo(struct rvu *rvu, struct msg_req *req,
6111 struct nix_bandprof_get_hwinfo_rsp *rsp)
6113 struct nix_ipolicer *ipolicer;
6114 int blkaddr, layer, err;
6115 struct nix_hw *nix_hw;
6118 if (!rvu->hw->cap.ipolicer)
6119 return NIX_AF_ERR_IPOLICER_NOTSUPP;
6121 err = nix_get_struct_ptrs(rvu, req->hdr.pcifunc, &nix_hw, &blkaddr);
6125 /* Return number of bandwidth profiles free at each layer */
6126 mutex_lock(&rvu->rsrc_lock);
6127 for (layer = 0; layer < BAND_PROF_NUM_LAYERS; layer++) {
6128 if (layer == BAND_PROF_INVAL_LAYER)
6131 ipolicer = &nix_hw->ipolicer[layer];
6132 rsp->prof_count[layer] = rvu_rsrc_free_count(&ipolicer->band_prof);
6134 mutex_unlock(&rvu->rsrc_lock);
6136 /* Set the policer timeunit in nanosec */
6137 tu = rvu_read64(rvu, blkaddr, NIX_AF_PL_TS) & GENMASK_ULL(9, 0);
6138 rsp->policer_timeunit = (tu + 1) * 100;
6143 static struct nix_mcast_grp_elem *rvu_nix_mcast_find_grp_elem(struct nix_mcast_grp *mcast_grp,
6146 struct nix_mcast_grp_elem *iter;
6147 bool is_found = false;
6149 list_for_each_entry(iter, &mcast_grp->mcast_grp_head, list) {
6150 if (iter->mcast_grp_idx == mcast_grp_idx) {
6162 int rvu_nix_mcast_get_mce_index(struct rvu *rvu, u16 pcifunc, u32 mcast_grp_idx)
6164 struct nix_mcast_grp_elem *elem;
6165 struct nix_mcast_grp *mcast_grp;
6166 struct nix_hw *nix_hw;
6169 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
6170 nix_hw = get_nix_hw(rvu->hw, blkaddr);
6172 return NIX_AF_ERR_INVALID_NIXBLK;
6174 mcast_grp = &nix_hw->mcast_grp;
6175 mutex_lock(&mcast_grp->mcast_grp_lock);
6176 elem = rvu_nix_mcast_find_grp_elem(mcast_grp, mcast_grp_idx);
6178 ret = NIX_AF_ERR_INVALID_MCAST_GRP;
6180 ret = elem->mce_start_index;
6182 mutex_unlock(&mcast_grp->mcast_grp_lock);
6186 void rvu_nix_mcast_flr_free_entries(struct rvu *rvu, u16 pcifunc)
6188 struct nix_mcast_grp_destroy_req dreq = { 0 };
6189 struct nix_mcast_grp_update_req ureq = { 0 };
6190 struct nix_mcast_grp_update_rsp ursp = { 0 };
6191 struct nix_mcast_grp_elem *elem, *tmp;
6192 struct nix_mcast_grp *mcast_grp;
6193 struct nix_hw *nix_hw;
6196 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
6197 nix_hw = get_nix_hw(rvu->hw, blkaddr);
6201 mcast_grp = &nix_hw->mcast_grp;
6203 mutex_lock(&mcast_grp->mcast_grp_lock);
6204 list_for_each_entry_safe(elem, tmp, &mcast_grp->mcast_grp_head, list) {
6205 struct nix_mce_list *mce_list;
6206 struct hlist_node *tmp;
6209 /* If the pcifunc which created the multicast/mirror
6210 * group received an FLR, then delete the entire group.
6212 if (elem->pcifunc == pcifunc) {
6214 dreq.hdr.pcifunc = elem->pcifunc;
6215 dreq.mcast_grp_idx = elem->mcast_grp_idx;
6217 rvu_mbox_handler_nix_mcast_grp_destroy(rvu, &dreq, NULL);
6221 /* Iterate the group elements and delete the element which
6224 mce_list = &elem->mcast_mce_list;
6225 hlist_for_each_entry_safe(mce, tmp, &mce_list->head, node) {
6226 if (mce->pcifunc == pcifunc) {
6227 ureq.hdr.pcifunc = pcifunc;
6228 ureq.num_mce_entry = 1;
6229 ureq.mcast_grp_idx = elem->mcast_grp_idx;
6230 ureq.op = NIX_MCAST_OP_DEL_ENTRY;
6231 ureq.pcifunc[0] = pcifunc;
6233 rvu_mbox_handler_nix_mcast_grp_update(rvu, &ureq, &ursp);
6238 mutex_unlock(&mcast_grp->mcast_grp_lock);
6241 int rvu_nix_mcast_update_mcam_entry(struct rvu *rvu, u16 pcifunc,
6242 u32 mcast_grp_idx, u16 mcam_index)
6244 struct nix_mcast_grp_elem *elem;
6245 struct nix_mcast_grp *mcast_grp;
6246 struct nix_hw *nix_hw;
6247 int blkaddr, ret = 0;
6249 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
6250 nix_hw = get_nix_hw(rvu->hw, blkaddr);
6252 return NIX_AF_ERR_INVALID_NIXBLK;
6254 mcast_grp = &nix_hw->mcast_grp;
6255 mutex_lock(&mcast_grp->mcast_grp_lock);
6256 elem = rvu_nix_mcast_find_grp_elem(mcast_grp, mcast_grp_idx);
6258 ret = NIX_AF_ERR_INVALID_MCAST_GRP;
6260 elem->mcam_index = mcam_index;
6262 mutex_unlock(&mcast_grp->mcast_grp_lock);
6266 int rvu_mbox_handler_nix_mcast_grp_create(struct rvu *rvu,
6267 struct nix_mcast_grp_create_req *req,
6268 struct nix_mcast_grp_create_rsp *rsp)
6270 struct nix_mcast_grp_elem *elem;
6271 struct nix_mcast_grp *mcast_grp;
6272 struct nix_hw *nix_hw;
6275 err = nix_get_struct_ptrs(rvu, req->hdr.pcifunc, &nix_hw, &blkaddr);
6279 mcast_grp = &nix_hw->mcast_grp;
6280 elem = kzalloc(sizeof(*elem), GFP_KERNEL);
6284 INIT_HLIST_HEAD(&elem->mcast_mce_list.head);
6285 elem->mcam_index = -1;
6286 elem->mce_start_index = -1;
6287 elem->pcifunc = req->hdr.pcifunc;
6288 elem->dir = req->dir;
6289 elem->mcast_grp_idx = mcast_grp->next_grp_index++;
6291 mutex_lock(&mcast_grp->mcast_grp_lock);
6292 list_add_tail(&elem->list, &mcast_grp->mcast_grp_head);
6294 mutex_unlock(&mcast_grp->mcast_grp_lock);
6296 rsp->mcast_grp_idx = elem->mcast_grp_idx;
6300 int rvu_mbox_handler_nix_mcast_grp_destroy(struct rvu *rvu,
6301 struct nix_mcast_grp_destroy_req *req,
6302 struct msg_rsp *rsp)
6304 struct npc_delete_flow_req uninstall_req = { 0 };
6305 struct npc_delete_flow_rsp uninstall_rsp = { 0 };
6306 struct nix_mcast_grp_elem *elem;
6307 struct nix_mcast_grp *mcast_grp;
6308 int blkaddr, err, ret = 0;
6309 struct nix_mcast *mcast;
6310 struct nix_hw *nix_hw;
6312 err = nix_get_struct_ptrs(rvu, req->hdr.pcifunc, &nix_hw, &blkaddr);
6316 mcast_grp = &nix_hw->mcast_grp;
6318 /* If AF is requesting for the deletion,
6319 * then AF is already taking the lock
6322 mutex_lock(&mcast_grp->mcast_grp_lock);
6324 elem = rvu_nix_mcast_find_grp_elem(mcast_grp, req->mcast_grp_idx);
6326 ret = NIX_AF_ERR_INVALID_MCAST_GRP;
6330 /* If no mce entries are associated with the group
6331 * then just remove it from the global list.
6333 if (!elem->mcast_mce_list.count)
6336 /* Delete the associated mcam entry and
6337 * remove all mce entries from the group
6339 mcast = &nix_hw->mcast;
6340 mutex_lock(&mcast->mce_lock);
6341 if (elem->mcam_index != -1) {
6342 uninstall_req.hdr.pcifunc = req->hdr.pcifunc;
6343 uninstall_req.entry = elem->mcam_index;
6344 rvu_mbox_handler_npc_delete_flow(rvu, &uninstall_req, &uninstall_rsp);
6347 nix_free_mce_list(mcast, elem->mcast_mce_list.count,
6348 elem->mce_start_index, elem->dir);
6349 nix_delete_mcast_mce_list(&elem->mcast_mce_list);
6350 mutex_unlock(&mcast->mce_lock);
6353 list_del(&elem->list);
6359 mutex_unlock(&mcast_grp->mcast_grp_lock);
6364 int rvu_mbox_handler_nix_mcast_grp_update(struct rvu *rvu,
6365 struct nix_mcast_grp_update_req *req,
6366 struct nix_mcast_grp_update_rsp *rsp)
6368 struct nix_mcast_grp_destroy_req dreq = { 0 };
6369 struct npc_mcam *mcam = &rvu->hw->mcam;
6370 struct nix_mcast_grp_elem *elem;
6371 struct nix_mcast_grp *mcast_grp;
6372 int blkaddr, err, npc_blkaddr;
6373 u16 prev_count, new_count;
6374 struct nix_mcast *mcast;
6375 struct nix_hw *nix_hw;
6378 if (!req->num_mce_entry)
6381 err = nix_get_struct_ptrs(rvu, req->hdr.pcifunc, &nix_hw, &blkaddr);
6385 mcast_grp = &nix_hw->mcast_grp;
6387 /* If AF is requesting for the updation,
6388 * then AF is already taking the lock
6391 mutex_lock(&mcast_grp->mcast_grp_lock);
6393 elem = rvu_nix_mcast_find_grp_elem(mcast_grp, req->mcast_grp_idx);
6395 ret = NIX_AF_ERR_INVALID_MCAST_GRP;
6399 /* If any pcifunc matches the group's pcifunc, then we can
6400 * delete the entire group.
6402 if (req->op == NIX_MCAST_OP_DEL_ENTRY) {
6403 for (i = 0; i < req->num_mce_entry; i++) {
6404 if (elem->pcifunc == req->pcifunc[i]) {
6406 dreq.hdr.pcifunc = elem->pcifunc;
6407 dreq.mcast_grp_idx = elem->mcast_grp_idx;
6409 rvu_mbox_handler_nix_mcast_grp_destroy(rvu, &dreq, NULL);
6416 mcast = &nix_hw->mcast;
6417 mutex_lock(&mcast->mce_lock);
6418 npc_blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
6419 if (elem->mcam_index != -1)
6420 npc_enable_mcam_entry(rvu, mcam, npc_blkaddr, elem->mcam_index, false);
6422 prev_count = elem->mcast_mce_list.count;
6423 if (req->op == NIX_MCAST_OP_ADD_ENTRY) {
6424 new_count = prev_count + req->num_mce_entry;
6426 nix_free_mce_list(mcast, prev_count, elem->mce_start_index, elem->dir);
6428 elem->mce_start_index = nix_alloc_mce_list(mcast, new_count, elem->dir);
6430 /* It is possible not to get contiguous memory */
6431 if (elem->mce_start_index < 0) {
6432 if (elem->mcam_index != -1) {
6433 npc_enable_mcam_entry(rvu, mcam, npc_blkaddr,
6434 elem->mcam_index, true);
6435 ret = NIX_AF_ERR_NON_CONTIG_MCE_LIST;
6440 ret = nix_add_mce_list_entry(rvu, nix_hw, elem, req);
6442 nix_free_mce_list(mcast, new_count, elem->mce_start_index, elem->dir);
6444 elem->mce_start_index = nix_alloc_mce_list(mcast,
6448 if (elem->mcam_index != -1)
6449 npc_enable_mcam_entry(rvu, mcam, npc_blkaddr,
6450 elem->mcam_index, true);
6455 if (!prev_count || prev_count < req->num_mce_entry) {
6456 if (elem->mcam_index != -1)
6457 npc_enable_mcam_entry(rvu, mcam, npc_blkaddr,
6458 elem->mcam_index, true);
6459 ret = NIX_AF_ERR_INVALID_MCAST_DEL_REQ;
6463 nix_free_mce_list(mcast, prev_count, elem->mce_start_index, elem->dir);
6464 new_count = prev_count - req->num_mce_entry;
6465 elem->mce_start_index = nix_alloc_mce_list(mcast, new_count, elem->dir);
6466 ret = nix_del_mce_list_entry(rvu, nix_hw, elem, req);
6468 nix_free_mce_list(mcast, new_count, elem->mce_start_index, elem->dir);
6469 elem->mce_start_index = nix_alloc_mce_list(mcast, prev_count, elem->dir);
6470 if (elem->mcam_index != -1)
6471 npc_enable_mcam_entry(rvu, mcam,
6480 if (elem->mcam_index == -1) {
6481 rsp->mce_start_index = elem->mce_start_index;
6486 nix_mcast_update_action(rvu, elem);
6487 npc_enable_mcam_entry(rvu, mcam, npc_blkaddr, elem->mcam_index, true);
6488 rsp->mce_start_index = elem->mce_start_index;
6492 mutex_unlock(&mcast->mce_lock);
6496 mutex_unlock(&mcast_grp->mcast_grp_lock);