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"
16 #include "lmac_common.h"
17 #include "rvu_npc_hash.h"
19 static void nix_free_tx_vtag_entries(struct rvu *rvu, u16 pcifunc);
20 static int rvu_nix_get_bpid(struct rvu *rvu, struct nix_bp_cfg_req *req,
21 int type, int chan_id);
22 static int nix_update_mce_rule(struct rvu *rvu, u16 pcifunc,
24 static int nix_setup_ipolicers(struct rvu *rvu,
25 struct nix_hw *nix_hw, int blkaddr);
26 static void nix_ipolicer_freemem(struct rvu *rvu, struct nix_hw *nix_hw);
27 static int nix_verify_bandprof(struct nix_cn10k_aq_enq_req *req,
28 struct nix_hw *nix_hw, u16 pcifunc);
29 static int nix_free_all_bandprof(struct rvu *rvu, u16 pcifunc);
30 static void nix_clear_ratelimit_aggr(struct rvu *rvu, struct nix_hw *nix_hw,
32 static const char *nix_get_ctx_name(int ctype);
58 enum nix_makr_fmt_indexes {
59 NIX_MARK_CFG_IP_DSCP_RED,
60 NIX_MARK_CFG_IP_DSCP_YELLOW,
61 NIX_MARK_CFG_IP_DSCP_YELLOW_RED,
62 NIX_MARK_CFG_IP_ECN_RED,
63 NIX_MARK_CFG_IP_ECN_YELLOW,
64 NIX_MARK_CFG_IP_ECN_YELLOW_RED,
65 NIX_MARK_CFG_VLAN_DEI_RED,
66 NIX_MARK_CFG_VLAN_DEI_YELLOW,
67 NIX_MARK_CFG_VLAN_DEI_YELLOW_RED,
71 /* For now considering MC resources needed for broadcast
72 * pkt replication only. i.e 256 HWVFs + 12 PFs.
74 #define MC_TBL_SIZE MC_TBL_SZ_512
75 #define MC_BUF_CNT MC_BUF_CNT_128
78 struct hlist_node node;
82 int rvu_get_next_nix_blkaddr(struct rvu *rvu, int blkaddr)
86 /*If blkaddr is 0, return the first nix block address*/
88 return rvu->nix_blkaddr[blkaddr];
90 while (i + 1 < MAX_NIX_BLKS) {
91 if (rvu->nix_blkaddr[i] == blkaddr)
92 return rvu->nix_blkaddr[i + 1];
99 bool is_nixlf_attached(struct rvu *rvu, u16 pcifunc)
101 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
104 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
105 if (!pfvf->nixlf || blkaddr < 0)
110 int rvu_get_nixlf_count(struct rvu *rvu)
112 int blkaddr = 0, max = 0;
113 struct rvu_block *block;
115 blkaddr = rvu_get_next_nix_blkaddr(rvu, blkaddr);
117 block = &rvu->hw->block[blkaddr];
118 max += block->lf.max;
119 blkaddr = rvu_get_next_nix_blkaddr(rvu, blkaddr);
124 int nix_get_nixlf(struct rvu *rvu, u16 pcifunc, int *nixlf, int *nix_blkaddr)
126 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
127 struct rvu_hwinfo *hw = rvu->hw;
130 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
131 if (!pfvf->nixlf || blkaddr < 0)
132 return NIX_AF_ERR_AF_LF_INVALID;
134 *nixlf = rvu_get_lf(rvu, &hw->block[blkaddr], pcifunc, 0);
136 return NIX_AF_ERR_AF_LF_INVALID;
139 *nix_blkaddr = blkaddr;
144 int nix_get_struct_ptrs(struct rvu *rvu, u16 pcifunc,
145 struct nix_hw **nix_hw, int *blkaddr)
147 struct rvu_pfvf *pfvf;
149 pfvf = rvu_get_pfvf(rvu, pcifunc);
150 *blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
151 if (!pfvf->nixlf || *blkaddr < 0)
152 return NIX_AF_ERR_AF_LF_INVALID;
154 *nix_hw = get_nix_hw(rvu->hw, *blkaddr);
156 return NIX_AF_ERR_INVALID_NIXBLK;
160 static void nix_mce_list_init(struct nix_mce_list *list, int max)
162 INIT_HLIST_HEAD(&list->head);
167 static u16 nix_alloc_mce_list(struct nix_mcast *mcast, int count)
174 idx = mcast->next_free_mce;
175 mcast->next_free_mce += count;
179 struct nix_hw *get_nix_hw(struct rvu_hwinfo *hw, int blkaddr)
181 int nix_blkaddr = 0, i = 0;
182 struct rvu *rvu = hw->rvu;
184 nix_blkaddr = rvu_get_next_nix_blkaddr(rvu, nix_blkaddr);
185 while (nix_blkaddr) {
186 if (blkaddr == nix_blkaddr && hw->nix)
188 nix_blkaddr = rvu_get_next_nix_blkaddr(rvu, nix_blkaddr);
194 u32 convert_dwrr_mtu_to_bytes(u8 dwrr_mtu)
198 /* MTU used for DWRR calculation is in power of 2 up until 64K bytes.
199 * Value of 4 is reserved for MTU value of 9728 bytes.
200 * Value of 5 is reserved for MTU value of 10240 bytes.
208 return BIT_ULL(dwrr_mtu);
214 u32 convert_bytes_to_dwrr_mtu(u32 bytes)
216 /* MTU used for DWRR calculation is in power of 2 up until 64K bytes.
217 * Value of 4 is reserved for MTU value of 9728 bytes.
218 * Value of 5 is reserved for MTU value of 10240 bytes.
220 if (bytes > BIT_ULL(16))
235 static void nix_rx_sync(struct rvu *rvu, int blkaddr)
239 /* Sync all in flight RX packets to LLC/DRAM */
240 rvu_write64(rvu, blkaddr, NIX_AF_RX_SW_SYNC, BIT_ULL(0));
241 err = rvu_poll_reg(rvu, blkaddr, NIX_AF_RX_SW_SYNC, BIT_ULL(0), true);
243 dev_err(rvu->dev, "SYNC1: NIX RX software sync failed\n");
245 /* SW_SYNC ensures all existing transactions are finished and pkts
246 * are written to LLC/DRAM, queues should be teared down after
247 * successful SW_SYNC. Due to a HW errata, in some rare scenarios
248 * an existing transaction might end after SW_SYNC operation. To
249 * ensure operation is fully done, do the SW_SYNC twice.
251 rvu_write64(rvu, blkaddr, NIX_AF_RX_SW_SYNC, BIT_ULL(0));
252 err = rvu_poll_reg(rvu, blkaddr, NIX_AF_RX_SW_SYNC, BIT_ULL(0), true);
254 dev_err(rvu->dev, "SYNC2: NIX RX software sync failed\n");
257 static bool is_valid_txschq(struct rvu *rvu, int blkaddr,
258 int lvl, u16 pcifunc, u16 schq)
260 struct rvu_hwinfo *hw = rvu->hw;
261 struct nix_txsch *txsch;
262 struct nix_hw *nix_hw;
265 nix_hw = get_nix_hw(rvu->hw, blkaddr);
269 txsch = &nix_hw->txsch[lvl];
270 /* Check out of bounds */
271 if (schq >= txsch->schq.max)
274 mutex_lock(&rvu->rsrc_lock);
275 map_func = TXSCH_MAP_FUNC(txsch->pfvf_map[schq]);
276 mutex_unlock(&rvu->rsrc_lock);
278 /* TLs aggegating traffic are shared across PF and VFs */
279 if (lvl >= hw->cap.nix_tx_aggr_lvl) {
280 if (rvu_get_pf(map_func) != rvu_get_pf(pcifunc))
286 if (map_func != pcifunc)
292 static int nix_interface_init(struct rvu *rvu, u16 pcifunc, int type, int nixlf,
293 struct nix_lf_alloc_rsp *rsp, bool loop)
295 struct rvu_pfvf *parent_pf, *pfvf = rvu_get_pfvf(rvu, pcifunc);
296 u16 req_chan_base, req_chan_end, req_chan_cnt;
297 struct rvu_hwinfo *hw = rvu->hw;
298 struct sdp_node_info *sdp_info;
299 int pkind, pf, vf, lbkid, vfid;
304 pf = rvu_get_pf(pcifunc);
305 if (!is_pf_cgxmapped(rvu, pf) && type != NIX_INTF_TYPE_LBK &&
306 type != NIX_INTF_TYPE_SDP)
310 case NIX_INTF_TYPE_CGX:
311 pfvf->cgx_lmac = rvu->pf2cgxlmac_map[pf];
312 rvu_get_cgx_lmac_id(pfvf->cgx_lmac, &cgx_id, &lmac_id);
314 pkind = rvu_npc_get_pkind(rvu, pf);
317 "PF_Func 0x%x: Invalid pkind\n", pcifunc);
320 pfvf->rx_chan_base = rvu_nix_chan_cgx(rvu, cgx_id, lmac_id, 0);
321 pfvf->tx_chan_base = pfvf->rx_chan_base;
322 pfvf->rx_chan_cnt = 1;
323 pfvf->tx_chan_cnt = 1;
324 rsp->tx_link = cgx_id * hw->lmac_per_cgx + lmac_id;
326 cgx_set_pkind(rvu_cgx_pdata(cgx_id, rvu), lmac_id, pkind);
327 rvu_npc_set_pkind(rvu, pkind, pfvf);
330 case NIX_INTF_TYPE_LBK:
331 vf = (pcifunc & RVU_PFVF_FUNC_MASK) - 1;
333 /* If NIX1 block is present on the silicon then NIXes are
334 * assigned alternatively for lbk interfaces. NIX0 should
335 * send packets on lbk link 1 channels and NIX1 should send
336 * on lbk link 0 channels for the communication between
340 if (rvu->hw->lbk_links > 1)
341 lbkid = vf & 0x1 ? 0 : 1;
343 /* By default NIX0 is configured to send packet on lbk link 1
344 * (which corresponds to LBK1), same packet will receive on
345 * NIX1 over lbk link 0. If NIX1 sends packet on lbk link 0
346 * (which corresponds to LBK2) packet will receive on NIX0 lbk
348 * But if lbk links for NIX0 and NIX1 are negated, i.e NIX0
349 * transmits and receives on lbk link 0, whick corresponds
350 * to LBK1 block, back to back connectivity between NIX and
351 * LBK can be achieved (which is similar to 96xx)
354 * NIX0 lbk link 1 (LBK2) 1 (LBK1)
355 * NIX0 lbk link 0 (LBK0) 0 (LBK0)
356 * NIX1 lbk link 0 (LBK1) 0 (LBK2)
357 * NIX1 lbk link 1 (LBK3) 1 (LBK3)
362 /* Note that AF's VFs work in pairs and talk over consecutive
363 * loopback channels.Therefore if odd number of AF VFs are
364 * enabled then the last VF remains with no pair.
366 pfvf->rx_chan_base = rvu_nix_chan_lbk(rvu, lbkid, vf);
367 pfvf->tx_chan_base = vf & 0x1 ?
368 rvu_nix_chan_lbk(rvu, lbkid, vf - 1) :
369 rvu_nix_chan_lbk(rvu, lbkid, vf + 1);
370 pfvf->rx_chan_cnt = 1;
371 pfvf->tx_chan_cnt = 1;
372 rsp->tx_link = hw->cgx_links + lbkid;
374 rvu_npc_set_pkind(rvu, NPC_RX_LBK_PKIND, pfvf);
375 rvu_npc_install_promisc_entry(rvu, pcifunc, nixlf,
380 case NIX_INTF_TYPE_SDP:
381 from_vf = !!(pcifunc & RVU_PFVF_FUNC_MASK);
382 parent_pf = &rvu->pf[rvu_get_pf(pcifunc)];
383 sdp_info = parent_pf->sdp_info;
385 dev_err(rvu->dev, "Invalid sdp_info pointer\n");
389 req_chan_base = rvu_nix_chan_sdp(rvu, 0) + sdp_info->pf_srn +
390 sdp_info->num_pf_rings;
391 vf = (pcifunc & RVU_PFVF_FUNC_MASK) - 1;
392 for (vfid = 0; vfid < vf; vfid++)
393 req_chan_base += sdp_info->vf_rings[vfid];
394 req_chan_cnt = sdp_info->vf_rings[vf];
395 req_chan_end = req_chan_base + req_chan_cnt - 1;
396 if (req_chan_base < rvu_nix_chan_sdp(rvu, 0) ||
397 req_chan_end > rvu_nix_chan_sdp(rvu, 255)) {
399 "PF_Func 0x%x: Invalid channel base and count\n",
404 req_chan_base = rvu_nix_chan_sdp(rvu, 0) + sdp_info->pf_srn;
405 req_chan_cnt = sdp_info->num_pf_rings;
408 pfvf->rx_chan_base = req_chan_base;
409 pfvf->rx_chan_cnt = req_chan_cnt;
410 pfvf->tx_chan_base = pfvf->rx_chan_base;
411 pfvf->tx_chan_cnt = pfvf->rx_chan_cnt;
413 rsp->tx_link = hw->cgx_links + hw->lbk_links;
414 rvu_npc_install_promisc_entry(rvu, pcifunc, nixlf,
420 /* Add a UCAST forwarding rule in MCAM with this NIXLF attached
421 * RVU PF/VF's MAC address.
423 rvu_npc_install_ucast_entry(rvu, pcifunc, nixlf,
424 pfvf->rx_chan_base, pfvf->mac_addr);
426 /* Add this PF_FUNC to bcast pkt replication list */
427 err = nix_update_mce_rule(rvu, pcifunc, NIXLF_BCAST_ENTRY, true);
430 "Bcast list, failed to enable PF_FUNC 0x%x\n",
434 /* Install MCAM rule matching Ethernet broadcast mac address */
435 rvu_npc_install_bcast_match_entry(rvu, pcifunc,
436 nixlf, pfvf->rx_chan_base);
438 pfvf->maxlen = NIC_HW_MIN_FRS;
439 pfvf->minlen = NIC_HW_MIN_FRS;
444 static void nix_interface_deinit(struct rvu *rvu, u16 pcifunc, u8 nixlf)
446 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
452 /* Remove this PF_FUNC from bcast pkt replication list */
453 err = nix_update_mce_rule(rvu, pcifunc, NIXLF_BCAST_ENTRY, false);
456 "Bcast list, failed to disable PF_FUNC 0x%x\n",
460 /* Free and disable any MCAM entries used by this NIX LF */
461 rvu_npc_disable_mcam_entries(rvu, pcifunc, nixlf);
463 /* Disable DMAC filters used */
464 rvu_cgx_disable_dmac_entries(rvu, pcifunc);
467 int rvu_mbox_handler_nix_bp_disable(struct rvu *rvu,
468 struct nix_bp_cfg_req *req,
471 u16 pcifunc = req->hdr.pcifunc;
472 struct rvu_pfvf *pfvf;
473 int blkaddr, pf, type;
477 pf = rvu_get_pf(pcifunc);
478 type = is_afvf(pcifunc) ? NIX_INTF_TYPE_LBK : NIX_INTF_TYPE_CGX;
479 if (!is_pf_cgxmapped(rvu, pf) && type != NIX_INTF_TYPE_LBK)
482 pfvf = rvu_get_pfvf(rvu, pcifunc);
483 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
485 chan_base = pfvf->rx_chan_base + req->chan_base;
486 for (chan = chan_base; chan < (chan_base + req->chan_cnt); chan++) {
487 cfg = rvu_read64(rvu, blkaddr, NIX_AF_RX_CHANX_CFG(chan));
488 rvu_write64(rvu, blkaddr, NIX_AF_RX_CHANX_CFG(chan),
494 static int rvu_nix_get_bpid(struct rvu *rvu, struct nix_bp_cfg_req *req,
495 int type, int chan_id)
497 int bpid, blkaddr, lmac_chan_cnt, sdp_chan_cnt;
498 u16 cgx_bpid_cnt, lbk_bpid_cnt, sdp_bpid_cnt;
499 struct rvu_hwinfo *hw = rvu->hw;
500 struct rvu_pfvf *pfvf;
504 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, req->hdr.pcifunc);
505 cfg = rvu_read64(rvu, blkaddr, NIX_AF_CONST);
506 lmac_chan_cnt = cfg & 0xFF;
508 cgx_bpid_cnt = hw->cgx_links * lmac_chan_cnt;
509 lbk_bpid_cnt = hw->lbk_links * ((cfg >> 16) & 0xFF);
511 cfg = rvu_read64(rvu, blkaddr, NIX_AF_CONST1);
512 sdp_chan_cnt = cfg & 0xFFF;
513 sdp_bpid_cnt = hw->sdp_links * sdp_chan_cnt;
515 pfvf = rvu_get_pfvf(rvu, req->hdr.pcifunc);
517 /* Backpressure IDs range division
518 * CGX channles are mapped to (0 - 191) BPIDs
519 * LBK channles are mapped to (192 - 255) BPIDs
520 * SDP channles are mapped to (256 - 511) BPIDs
522 * Lmac channles and bpids mapped as follows
523 * cgx(0)_lmac(0)_chan(0 - 15) = bpid(0 - 15)
524 * cgx(0)_lmac(1)_chan(0 - 15) = bpid(16 - 31) ....
525 * cgx(1)_lmac(0)_chan(0 - 15) = bpid(64 - 79) ....
528 case NIX_INTF_TYPE_CGX:
529 if ((req->chan_base + req->chan_cnt) > 16)
531 rvu_get_cgx_lmac_id(pfvf->cgx_lmac, &cgx_id, &lmac_id);
532 /* Assign bpid based on cgx, lmac and chan id */
533 bpid = (cgx_id * hw->lmac_per_cgx * lmac_chan_cnt) +
534 (lmac_id * lmac_chan_cnt) + req->chan_base;
536 if (req->bpid_per_chan)
538 if (bpid > cgx_bpid_cnt)
542 case NIX_INTF_TYPE_LBK:
543 if ((req->chan_base + req->chan_cnt) > 63)
545 bpid = cgx_bpid_cnt + req->chan_base;
546 if (req->bpid_per_chan)
548 if (bpid > (cgx_bpid_cnt + lbk_bpid_cnt))
551 case NIX_INTF_TYPE_SDP:
552 if ((req->chan_base + req->chan_cnt) > 255)
555 bpid = sdp_bpid_cnt + req->chan_base;
556 if (req->bpid_per_chan)
559 if (bpid > (cgx_bpid_cnt + lbk_bpid_cnt + sdp_bpid_cnt))
568 int rvu_mbox_handler_nix_bp_enable(struct rvu *rvu,
569 struct nix_bp_cfg_req *req,
570 struct nix_bp_cfg_rsp *rsp)
572 int blkaddr, pf, type, chan_id = 0;
573 u16 pcifunc = req->hdr.pcifunc;
574 struct rvu_pfvf *pfvf;
579 pf = rvu_get_pf(pcifunc);
580 type = is_afvf(pcifunc) ? NIX_INTF_TYPE_LBK : NIX_INTF_TYPE_CGX;
581 if (is_sdp_pfvf(pcifunc))
582 type = NIX_INTF_TYPE_SDP;
584 /* Enable backpressure only for CGX mapped PFs and LBK/SDP interface */
585 if (!is_pf_cgxmapped(rvu, pf) && type != NIX_INTF_TYPE_LBK &&
586 type != NIX_INTF_TYPE_SDP)
589 pfvf = rvu_get_pfvf(rvu, pcifunc);
590 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
592 bpid_base = rvu_nix_get_bpid(rvu, req, type, chan_id);
593 chan_base = pfvf->rx_chan_base + req->chan_base;
596 for (chan = chan_base; chan < (chan_base + req->chan_cnt); chan++) {
598 dev_warn(rvu->dev, "Fail to enable backpressure\n");
602 cfg = rvu_read64(rvu, blkaddr, NIX_AF_RX_CHANX_CFG(chan));
603 cfg &= ~GENMASK_ULL(8, 0);
604 rvu_write64(rvu, blkaddr, NIX_AF_RX_CHANX_CFG(chan),
605 cfg | (bpid & GENMASK_ULL(8, 0)) | BIT_ULL(16));
607 bpid = rvu_nix_get_bpid(rvu, req, type, chan_id);
610 for (chan = 0; chan < req->chan_cnt; chan++) {
611 /* Map channel and bpid assign to it */
612 rsp->chan_bpid[chan] = ((req->chan_base + chan) & 0x7F) << 10 |
614 if (req->bpid_per_chan)
617 rsp->chan_cnt = req->chan_cnt;
622 static void nix_setup_lso_tso_l3(struct rvu *rvu, int blkaddr,
623 u64 format, bool v4, u64 *fidx)
625 struct nix_lso_format field = {0};
627 /* IP's Length field */
628 field.layer = NIX_TXLAYER_OL3;
629 /* In ipv4, length field is at offset 2 bytes, for ipv6 it's 4 */
630 field.offset = v4 ? 2 : 4;
631 field.sizem1 = 1; /* i.e 2 bytes */
632 field.alg = NIX_LSOALG_ADD_PAYLEN;
633 rvu_write64(rvu, blkaddr,
634 NIX_AF_LSO_FORMATX_FIELDX(format, (*fidx)++),
637 /* No ID field in IPv6 header */
642 field.layer = NIX_TXLAYER_OL3;
644 field.sizem1 = 1; /* i.e 2 bytes */
645 field.alg = NIX_LSOALG_ADD_SEGNUM;
646 rvu_write64(rvu, blkaddr,
647 NIX_AF_LSO_FORMATX_FIELDX(format, (*fidx)++),
651 static void nix_setup_lso_tso_l4(struct rvu *rvu, int blkaddr,
652 u64 format, u64 *fidx)
654 struct nix_lso_format field = {0};
656 /* TCP's sequence number field */
657 field.layer = NIX_TXLAYER_OL4;
659 field.sizem1 = 3; /* i.e 4 bytes */
660 field.alg = NIX_LSOALG_ADD_OFFSET;
661 rvu_write64(rvu, blkaddr,
662 NIX_AF_LSO_FORMATX_FIELDX(format, (*fidx)++),
665 /* TCP's flags field */
666 field.layer = NIX_TXLAYER_OL4;
668 field.sizem1 = 1; /* 2 bytes */
669 field.alg = NIX_LSOALG_TCP_FLAGS;
670 rvu_write64(rvu, blkaddr,
671 NIX_AF_LSO_FORMATX_FIELDX(format, (*fidx)++),
675 static void nix_setup_lso(struct rvu *rvu, struct nix_hw *nix_hw, int blkaddr)
677 u64 cfg, idx, fidx = 0;
679 /* Get max HW supported format indices */
680 cfg = (rvu_read64(rvu, blkaddr, NIX_AF_CONST1) >> 48) & 0xFF;
681 nix_hw->lso.total = cfg;
684 cfg = rvu_read64(rvu, blkaddr, NIX_AF_LSO_CFG);
685 /* For TSO, set first and middle segment flags to
686 * mask out PSH, RST & FIN flags in TCP packet
688 cfg &= ~((0xFFFFULL << 32) | (0xFFFFULL << 16));
689 cfg |= (0xFFF2ULL << 32) | (0xFFF2ULL << 16);
690 rvu_write64(rvu, blkaddr, NIX_AF_LSO_CFG, cfg | BIT_ULL(63));
692 /* Setup default static LSO formats
694 * Configure format fields for TCPv4 segmentation offload
696 idx = NIX_LSO_FORMAT_IDX_TSOV4;
697 nix_setup_lso_tso_l3(rvu, blkaddr, idx, true, &fidx);
698 nix_setup_lso_tso_l4(rvu, blkaddr, idx, &fidx);
700 /* Set rest of the fields to NOP */
701 for (; fidx < 8; fidx++) {
702 rvu_write64(rvu, blkaddr,
703 NIX_AF_LSO_FORMATX_FIELDX(idx, fidx), 0x0ULL);
705 nix_hw->lso.in_use++;
707 /* Configure format fields for TCPv6 segmentation offload */
708 idx = NIX_LSO_FORMAT_IDX_TSOV6;
710 nix_setup_lso_tso_l3(rvu, blkaddr, idx, false, &fidx);
711 nix_setup_lso_tso_l4(rvu, blkaddr, idx, &fidx);
713 /* Set rest of the fields to NOP */
714 for (; fidx < 8; fidx++) {
715 rvu_write64(rvu, blkaddr,
716 NIX_AF_LSO_FORMATX_FIELDX(idx, fidx), 0x0ULL);
718 nix_hw->lso.in_use++;
721 static void nix_ctx_free(struct rvu *rvu, struct rvu_pfvf *pfvf)
723 kfree(pfvf->rq_bmap);
724 kfree(pfvf->sq_bmap);
725 kfree(pfvf->cq_bmap);
727 qmem_free(rvu->dev, pfvf->rq_ctx);
729 qmem_free(rvu->dev, pfvf->sq_ctx);
731 qmem_free(rvu->dev, pfvf->cq_ctx);
733 qmem_free(rvu->dev, pfvf->rss_ctx);
734 if (pfvf->nix_qints_ctx)
735 qmem_free(rvu->dev, pfvf->nix_qints_ctx);
736 if (pfvf->cq_ints_ctx)
737 qmem_free(rvu->dev, pfvf->cq_ints_ctx);
739 pfvf->rq_bmap = NULL;
740 pfvf->cq_bmap = NULL;
741 pfvf->sq_bmap = NULL;
745 pfvf->rss_ctx = NULL;
746 pfvf->nix_qints_ctx = NULL;
747 pfvf->cq_ints_ctx = NULL;
750 static int nixlf_rss_ctx_init(struct rvu *rvu, int blkaddr,
751 struct rvu_pfvf *pfvf, int nixlf,
752 int rss_sz, int rss_grps, int hwctx_size,
753 u64 way_mask, bool tag_lsb_as_adder)
755 int err, grp, num_indices;
758 /* RSS is not requested for this NIXLF */
761 num_indices = rss_sz * rss_grps;
763 /* Alloc NIX RSS HW context memory and config the base */
764 err = qmem_alloc(rvu->dev, &pfvf->rss_ctx, num_indices, hwctx_size);
768 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RSS_BASE(nixlf),
769 (u64)pfvf->rss_ctx->iova);
771 /* Config full RSS table size, enable RSS and caching */
772 val = BIT_ULL(36) | BIT_ULL(4) | way_mask << 20 |
773 ilog2(num_indices / MAX_RSS_INDIR_TBL_SIZE);
775 if (tag_lsb_as_adder)
778 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RSS_CFG(nixlf), val);
779 /* Config RSS group offset and sizes */
780 for (grp = 0; grp < rss_grps; grp++)
781 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RSS_GRPX(nixlf, grp),
782 ((ilog2(rss_sz) - 1) << 16) | (rss_sz * grp));
786 static int nix_aq_enqueue_wait(struct rvu *rvu, struct rvu_block *block,
787 struct nix_aq_inst_s *inst)
789 struct admin_queue *aq = block->aq;
790 struct nix_aq_res_s *result;
795 result = (struct nix_aq_res_s *)aq->res->base;
797 /* Get current head pointer where to append this instruction */
798 reg = rvu_read64(rvu, block->addr, NIX_AF_AQ_STATUS);
799 head = (reg >> 4) & AQ_PTR_MASK;
801 memcpy((void *)(aq->inst->base + (head * aq->inst->entry_sz)),
802 (void *)inst, aq->inst->entry_sz);
803 memset(result, 0, sizeof(*result));
804 /* sync into memory */
807 /* Ring the doorbell and wait for result */
808 rvu_write64(rvu, block->addr, NIX_AF_AQ_DOOR, 1);
809 while (result->compcode == NIX_AQ_COMP_NOTDONE) {
817 if (result->compcode != NIX_AQ_COMP_GOOD) {
818 /* TODO: Replace this with some error code */
819 if (result->compcode == NIX_AQ_COMP_CTX_FAULT ||
820 result->compcode == NIX_AQ_COMP_LOCKERR ||
821 result->compcode == NIX_AQ_COMP_CTX_POISON) {
822 ret = rvu_ndc_fix_locked_cacheline(rvu, BLKADDR_NDC_NIX0_RX);
823 ret |= rvu_ndc_fix_locked_cacheline(rvu, BLKADDR_NDC_NIX0_TX);
824 ret |= rvu_ndc_fix_locked_cacheline(rvu, BLKADDR_NDC_NIX1_RX);
825 ret |= rvu_ndc_fix_locked_cacheline(rvu, BLKADDR_NDC_NIX1_TX);
828 "%s: Not able to unlock cachelines\n", __func__);
837 static int rvu_nix_blk_aq_enq_inst(struct rvu *rvu, struct nix_hw *nix_hw,
838 struct nix_aq_enq_req *req,
839 struct nix_aq_enq_rsp *rsp)
841 struct rvu_hwinfo *hw = rvu->hw;
842 u16 pcifunc = req->hdr.pcifunc;
843 int nixlf, blkaddr, rc = 0;
844 struct nix_aq_inst_s inst;
845 struct rvu_block *block;
846 struct admin_queue *aq;
847 struct rvu_pfvf *pfvf;
852 blkaddr = nix_hw->blkaddr;
853 block = &hw->block[blkaddr];
856 dev_warn(rvu->dev, "%s: NIX AQ not initialized\n", __func__);
857 return NIX_AF_ERR_AQ_ENQUEUE;
860 pfvf = rvu_get_pfvf(rvu, pcifunc);
861 nixlf = rvu_get_lf(rvu, block, pcifunc, 0);
863 /* Skip NIXLF check for broadcast MCE entry and bandwidth profile
864 * operations done by AF itself.
866 if (!((!rsp && req->ctype == NIX_AQ_CTYPE_MCE) ||
867 (req->ctype == NIX_AQ_CTYPE_BANDPROF && !pcifunc))) {
868 if (!pfvf->nixlf || nixlf < 0)
869 return NIX_AF_ERR_AF_LF_INVALID;
872 switch (req->ctype) {
873 case NIX_AQ_CTYPE_RQ:
874 /* Check if index exceeds max no of queues */
875 if (!pfvf->rq_ctx || req->qidx >= pfvf->rq_ctx->qsize)
876 rc = NIX_AF_ERR_AQ_ENQUEUE;
878 case NIX_AQ_CTYPE_SQ:
879 if (!pfvf->sq_ctx || req->qidx >= pfvf->sq_ctx->qsize)
880 rc = NIX_AF_ERR_AQ_ENQUEUE;
882 case NIX_AQ_CTYPE_CQ:
883 if (!pfvf->cq_ctx || req->qidx >= pfvf->cq_ctx->qsize)
884 rc = NIX_AF_ERR_AQ_ENQUEUE;
886 case NIX_AQ_CTYPE_RSS:
887 /* Check if RSS is enabled and qidx is within range */
888 cfg = rvu_read64(rvu, blkaddr, NIX_AF_LFX_RSS_CFG(nixlf));
889 if (!(cfg & BIT_ULL(4)) || !pfvf->rss_ctx ||
890 (req->qidx >= (256UL << (cfg & 0xF))))
891 rc = NIX_AF_ERR_AQ_ENQUEUE;
893 case NIX_AQ_CTYPE_MCE:
894 cfg = rvu_read64(rvu, blkaddr, NIX_AF_RX_MCAST_CFG);
896 /* Check if index exceeds MCE list length */
897 if (!nix_hw->mcast.mce_ctx ||
898 (req->qidx >= (256UL << (cfg & 0xF))))
899 rc = NIX_AF_ERR_AQ_ENQUEUE;
901 /* Adding multicast lists for requests from PF/VFs is not
902 * yet supported, so ignore this.
905 rc = NIX_AF_ERR_AQ_ENQUEUE;
907 case NIX_AQ_CTYPE_BANDPROF:
908 if (nix_verify_bandprof((struct nix_cn10k_aq_enq_req *)req,
910 rc = NIX_AF_ERR_INVALID_BANDPROF;
913 rc = NIX_AF_ERR_AQ_ENQUEUE;
919 /* Check if SQ pointed SMQ belongs to this PF/VF or not */
920 if (req->ctype == NIX_AQ_CTYPE_SQ &&
921 ((req->op == NIX_AQ_INSTOP_INIT && req->sq.ena) ||
922 (req->op == NIX_AQ_INSTOP_WRITE &&
923 req->sq_mask.ena && req->sq_mask.smq && req->sq.ena))) {
924 if (!is_valid_txschq(rvu, blkaddr, NIX_TXSCH_LVL_SMQ,
925 pcifunc, req->sq.smq))
926 return NIX_AF_ERR_AQ_ENQUEUE;
929 memset(&inst, 0, sizeof(struct nix_aq_inst_s));
931 inst.cindex = req->qidx;
932 inst.ctype = req->ctype;
934 /* Currently we are not supporting enqueuing multiple instructions,
935 * so always choose first entry in result memory.
937 inst.res_addr = (u64)aq->res->iova;
939 /* Hardware uses same aq->res->base for updating result of
940 * previous instruction hence wait here till it is done.
942 spin_lock(&aq->lock);
944 /* Clean result + context memory */
945 memset(aq->res->base, 0, aq->res->entry_sz);
946 /* Context needs to be written at RES_ADDR + 128 */
947 ctx = aq->res->base + 128;
948 /* Mask needs to be written at RES_ADDR + 256 */
949 mask = aq->res->base + 256;
952 case NIX_AQ_INSTOP_WRITE:
953 if (req->ctype == NIX_AQ_CTYPE_RQ)
954 memcpy(mask, &req->rq_mask,
955 sizeof(struct nix_rq_ctx_s));
956 else if (req->ctype == NIX_AQ_CTYPE_SQ)
957 memcpy(mask, &req->sq_mask,
958 sizeof(struct nix_sq_ctx_s));
959 else if (req->ctype == NIX_AQ_CTYPE_CQ)
960 memcpy(mask, &req->cq_mask,
961 sizeof(struct nix_cq_ctx_s));
962 else if (req->ctype == NIX_AQ_CTYPE_RSS)
963 memcpy(mask, &req->rss_mask,
964 sizeof(struct nix_rsse_s));
965 else if (req->ctype == NIX_AQ_CTYPE_MCE)
966 memcpy(mask, &req->mce_mask,
967 sizeof(struct nix_rx_mce_s));
968 else if (req->ctype == NIX_AQ_CTYPE_BANDPROF)
969 memcpy(mask, &req->prof_mask,
970 sizeof(struct nix_bandprof_s));
972 case NIX_AQ_INSTOP_INIT:
973 if (req->ctype == NIX_AQ_CTYPE_RQ)
974 memcpy(ctx, &req->rq, sizeof(struct nix_rq_ctx_s));
975 else if (req->ctype == NIX_AQ_CTYPE_SQ)
976 memcpy(ctx, &req->sq, sizeof(struct nix_sq_ctx_s));
977 else if (req->ctype == NIX_AQ_CTYPE_CQ)
978 memcpy(ctx, &req->cq, sizeof(struct nix_cq_ctx_s));
979 else if (req->ctype == NIX_AQ_CTYPE_RSS)
980 memcpy(ctx, &req->rss, sizeof(struct nix_rsse_s));
981 else if (req->ctype == NIX_AQ_CTYPE_MCE)
982 memcpy(ctx, &req->mce, sizeof(struct nix_rx_mce_s));
983 else if (req->ctype == NIX_AQ_CTYPE_BANDPROF)
984 memcpy(ctx, &req->prof, sizeof(struct nix_bandprof_s));
986 case NIX_AQ_INSTOP_NOP:
987 case NIX_AQ_INSTOP_READ:
988 case NIX_AQ_INSTOP_LOCK:
989 case NIX_AQ_INSTOP_UNLOCK:
992 rc = NIX_AF_ERR_AQ_ENQUEUE;
993 spin_unlock(&aq->lock);
997 /* Submit the instruction to AQ */
998 rc = nix_aq_enqueue_wait(rvu, block, &inst);
1000 spin_unlock(&aq->lock);
1004 /* Set RQ/SQ/CQ bitmap if respective queue hw context is enabled */
1005 if (req->op == NIX_AQ_INSTOP_INIT) {
1006 if (req->ctype == NIX_AQ_CTYPE_RQ && req->rq.ena)
1007 __set_bit(req->qidx, pfvf->rq_bmap);
1008 if (req->ctype == NIX_AQ_CTYPE_SQ && req->sq.ena)
1009 __set_bit(req->qidx, pfvf->sq_bmap);
1010 if (req->ctype == NIX_AQ_CTYPE_CQ && req->cq.ena)
1011 __set_bit(req->qidx, pfvf->cq_bmap);
1014 if (req->op == NIX_AQ_INSTOP_WRITE) {
1015 if (req->ctype == NIX_AQ_CTYPE_RQ) {
1016 ena = (req->rq.ena & req->rq_mask.ena) |
1017 (test_bit(req->qidx, pfvf->rq_bmap) &
1020 __set_bit(req->qidx, pfvf->rq_bmap);
1022 __clear_bit(req->qidx, pfvf->rq_bmap);
1024 if (req->ctype == NIX_AQ_CTYPE_SQ) {
1025 ena = (req->rq.ena & req->sq_mask.ena) |
1026 (test_bit(req->qidx, pfvf->sq_bmap) &
1029 __set_bit(req->qidx, pfvf->sq_bmap);
1031 __clear_bit(req->qidx, pfvf->sq_bmap);
1033 if (req->ctype == NIX_AQ_CTYPE_CQ) {
1034 ena = (req->rq.ena & req->cq_mask.ena) |
1035 (test_bit(req->qidx, pfvf->cq_bmap) &
1038 __set_bit(req->qidx, pfvf->cq_bmap);
1040 __clear_bit(req->qidx, pfvf->cq_bmap);
1045 /* Copy read context into mailbox */
1046 if (req->op == NIX_AQ_INSTOP_READ) {
1047 if (req->ctype == NIX_AQ_CTYPE_RQ)
1048 memcpy(&rsp->rq, ctx,
1049 sizeof(struct nix_rq_ctx_s));
1050 else if (req->ctype == NIX_AQ_CTYPE_SQ)
1051 memcpy(&rsp->sq, ctx,
1052 sizeof(struct nix_sq_ctx_s));
1053 else if (req->ctype == NIX_AQ_CTYPE_CQ)
1054 memcpy(&rsp->cq, ctx,
1055 sizeof(struct nix_cq_ctx_s));
1056 else if (req->ctype == NIX_AQ_CTYPE_RSS)
1057 memcpy(&rsp->rss, ctx,
1058 sizeof(struct nix_rsse_s));
1059 else if (req->ctype == NIX_AQ_CTYPE_MCE)
1060 memcpy(&rsp->mce, ctx,
1061 sizeof(struct nix_rx_mce_s));
1062 else if (req->ctype == NIX_AQ_CTYPE_BANDPROF)
1063 memcpy(&rsp->prof, ctx,
1064 sizeof(struct nix_bandprof_s));
1068 spin_unlock(&aq->lock);
1072 static int rvu_nix_verify_aq_ctx(struct rvu *rvu, struct nix_hw *nix_hw,
1073 struct nix_aq_enq_req *req, u8 ctype)
1075 struct nix_cn10k_aq_enq_req aq_req;
1076 struct nix_cn10k_aq_enq_rsp aq_rsp;
1079 if (req->ctype != NIX_AQ_CTYPE_CQ)
1082 rc = nix_aq_context_read(rvu, nix_hw, &aq_req, &aq_rsp,
1083 req->hdr.pcifunc, ctype, req->qidx);
1086 "%s: Failed to fetch %s%d context of PFFUNC 0x%x\n",
1087 __func__, nix_get_ctx_name(ctype), req->qidx,
1092 /* Make copy of original context & mask which are required
1095 memcpy(&aq_req.cq_mask, &req->cq_mask, sizeof(struct nix_cq_ctx_s));
1096 memcpy(&aq_req.cq, &req->cq, sizeof(struct nix_cq_ctx_s));
1098 /* exclude fields which HW can update */
1099 aq_req.cq_mask.cq_err = 0;
1100 aq_req.cq_mask.wrptr = 0;
1101 aq_req.cq_mask.tail = 0;
1102 aq_req.cq_mask.head = 0;
1103 aq_req.cq_mask.avg_level = 0;
1104 aq_req.cq_mask.update_time = 0;
1105 aq_req.cq_mask.substream = 0;
1107 /* Context mask (cq_mask) holds mask value of fields which
1108 * are changed in AQ WRITE operation.
1109 * for example cq.drop = 0xa;
1110 * cq_mask.drop = 0xff;
1111 * Below logic performs '&' between cq and cq_mask so that non
1112 * updated fields are masked out for request and response
1115 for (word = 0; word < sizeof(struct nix_cq_ctx_s) / sizeof(u64);
1117 *(u64 *)((u8 *)&aq_rsp.cq + word * 8) &=
1118 (*(u64 *)((u8 *)&aq_req.cq_mask + word * 8));
1119 *(u64 *)((u8 *)&aq_req.cq + word * 8) &=
1120 (*(u64 *)((u8 *)&aq_req.cq_mask + word * 8));
1123 if (memcmp(&aq_req.cq, &aq_rsp.cq, sizeof(struct nix_cq_ctx_s)))
1124 return NIX_AF_ERR_AQ_CTX_RETRY_WRITE;
1129 static int rvu_nix_aq_enq_inst(struct rvu *rvu, struct nix_aq_enq_req *req,
1130 struct nix_aq_enq_rsp *rsp)
1132 struct nix_hw *nix_hw;
1133 int err, retries = 5;
1136 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, req->hdr.pcifunc);
1138 return NIX_AF_ERR_AF_LF_INVALID;
1140 nix_hw = get_nix_hw(rvu->hw, blkaddr);
1142 return NIX_AF_ERR_INVALID_NIXBLK;
1145 err = rvu_nix_blk_aq_enq_inst(rvu, nix_hw, req, rsp);
1147 /* HW errata 'AQ Modification to CQ could be discarded on heavy traffic'
1148 * As a work around perfrom CQ context read after each AQ write. If AQ
1149 * read shows AQ write is not updated perform AQ write again.
1151 if (!err && req->op == NIX_AQ_INSTOP_WRITE) {
1152 err = rvu_nix_verify_aq_ctx(rvu, nix_hw, req, NIX_AQ_CTYPE_CQ);
1153 if (err == NIX_AF_ERR_AQ_CTX_RETRY_WRITE) {
1157 return NIX_AF_ERR_CQ_CTX_WRITE_ERR;
1164 static const char *nix_get_ctx_name(int ctype)
1167 case NIX_AQ_CTYPE_CQ:
1169 case NIX_AQ_CTYPE_SQ:
1171 case NIX_AQ_CTYPE_RQ:
1173 case NIX_AQ_CTYPE_RSS:
1179 static int nix_lf_hwctx_disable(struct rvu *rvu, struct hwctx_disable_req *req)
1181 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, req->hdr.pcifunc);
1182 struct nix_aq_enq_req aq_req;
1183 unsigned long *bmap;
1184 int qidx, q_cnt = 0;
1187 if (!pfvf->cq_ctx || !pfvf->sq_ctx || !pfvf->rq_ctx)
1188 return NIX_AF_ERR_AQ_ENQUEUE;
1190 memset(&aq_req, 0, sizeof(struct nix_aq_enq_req));
1191 aq_req.hdr.pcifunc = req->hdr.pcifunc;
1193 if (req->ctype == NIX_AQ_CTYPE_CQ) {
1195 aq_req.cq_mask.ena = 1;
1196 aq_req.cq.bp_ena = 0;
1197 aq_req.cq_mask.bp_ena = 1;
1198 q_cnt = pfvf->cq_ctx->qsize;
1199 bmap = pfvf->cq_bmap;
1201 if (req->ctype == NIX_AQ_CTYPE_SQ) {
1203 aq_req.sq_mask.ena = 1;
1204 q_cnt = pfvf->sq_ctx->qsize;
1205 bmap = pfvf->sq_bmap;
1207 if (req->ctype == NIX_AQ_CTYPE_RQ) {
1209 aq_req.rq_mask.ena = 1;
1210 q_cnt = pfvf->rq_ctx->qsize;
1211 bmap = pfvf->rq_bmap;
1214 aq_req.ctype = req->ctype;
1215 aq_req.op = NIX_AQ_INSTOP_WRITE;
1217 for (qidx = 0; qidx < q_cnt; qidx++) {
1218 if (!test_bit(qidx, bmap))
1221 rc = rvu_nix_aq_enq_inst(rvu, &aq_req, NULL);
1224 dev_err(rvu->dev, "Failed to disable %s:%d context\n",
1225 nix_get_ctx_name(req->ctype), qidx);
1232 #ifdef CONFIG_NDC_DIS_DYNAMIC_CACHING
1233 static int nix_lf_hwctx_lockdown(struct rvu *rvu, struct nix_aq_enq_req *req)
1235 struct nix_aq_enq_req lock_ctx_req;
1238 if (req->op != NIX_AQ_INSTOP_INIT)
1241 if (req->ctype == NIX_AQ_CTYPE_MCE ||
1242 req->ctype == NIX_AQ_CTYPE_DYNO)
1245 memset(&lock_ctx_req, 0, sizeof(struct nix_aq_enq_req));
1246 lock_ctx_req.hdr.pcifunc = req->hdr.pcifunc;
1247 lock_ctx_req.ctype = req->ctype;
1248 lock_ctx_req.op = NIX_AQ_INSTOP_LOCK;
1249 lock_ctx_req.qidx = req->qidx;
1250 err = rvu_nix_aq_enq_inst(rvu, &lock_ctx_req, NULL);
1253 "PFUNC 0x%x: Failed to lock NIX %s:%d context\n",
1255 nix_get_ctx_name(req->ctype), req->qidx);
1259 int rvu_mbox_handler_nix_aq_enq(struct rvu *rvu,
1260 struct nix_aq_enq_req *req,
1261 struct nix_aq_enq_rsp *rsp)
1265 err = rvu_nix_aq_enq_inst(rvu, req, rsp);
1267 err = nix_lf_hwctx_lockdown(rvu, req);
1272 int rvu_mbox_handler_nix_aq_enq(struct rvu *rvu,
1273 struct nix_aq_enq_req *req,
1274 struct nix_aq_enq_rsp *rsp)
1276 return rvu_nix_aq_enq_inst(rvu, req, rsp);
1279 /* CN10K mbox handler */
1280 int rvu_mbox_handler_nix_cn10k_aq_enq(struct rvu *rvu,
1281 struct nix_cn10k_aq_enq_req *req,
1282 struct nix_cn10k_aq_enq_rsp *rsp)
1284 return rvu_nix_aq_enq_inst(rvu, (struct nix_aq_enq_req *)req,
1285 (struct nix_aq_enq_rsp *)rsp);
1288 int rvu_mbox_handler_nix_hwctx_disable(struct rvu *rvu,
1289 struct hwctx_disable_req *req,
1290 struct msg_rsp *rsp)
1292 return nix_lf_hwctx_disable(rvu, req);
1295 int rvu_mbox_handler_nix_lf_alloc(struct rvu *rvu,
1296 struct nix_lf_alloc_req *req,
1297 struct nix_lf_alloc_rsp *rsp)
1299 int nixlf, qints, hwctx_size, intf, err, rc = 0;
1300 struct rvu_hwinfo *hw = rvu->hw;
1301 u16 pcifunc = req->hdr.pcifunc;
1302 struct rvu_block *block;
1303 struct rvu_pfvf *pfvf;
1307 if (!req->rq_cnt || !req->sq_cnt || !req->cq_cnt)
1308 return NIX_AF_ERR_PARAM;
1311 req->way_mask &= 0xFFFF;
1313 pfvf = rvu_get_pfvf(rvu, pcifunc);
1314 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
1315 if (!pfvf->nixlf || blkaddr < 0)
1316 return NIX_AF_ERR_AF_LF_INVALID;
1318 block = &hw->block[blkaddr];
1319 nixlf = rvu_get_lf(rvu, block, pcifunc, 0);
1321 return NIX_AF_ERR_AF_LF_INVALID;
1323 /* Check if requested 'NIXLF <=> NPALF' mapping is valid */
1324 if (req->npa_func) {
1325 /* If default, use 'this' NIXLF's PFFUNC */
1326 if (req->npa_func == RVU_DEFAULT_PF_FUNC)
1327 req->npa_func = pcifunc;
1328 if (!is_pffunc_map_valid(rvu, req->npa_func, BLKTYPE_NPA))
1329 return NIX_AF_INVAL_NPA_PF_FUNC;
1332 /* Check if requested 'NIXLF <=> SSOLF' mapping is valid */
1333 if (req->sso_func) {
1334 /* If default, use 'this' NIXLF's PFFUNC */
1335 if (req->sso_func == RVU_DEFAULT_PF_FUNC)
1336 req->sso_func = pcifunc;
1337 if (!is_pffunc_map_valid(rvu, req->sso_func, BLKTYPE_SSO))
1338 return NIX_AF_INVAL_SSO_PF_FUNC;
1341 /* If RSS is being enabled, check if requested config is valid.
1342 * RSS table size should be power of two, otherwise
1343 * RSS_GRP::OFFSET + adder might go beyond that group or
1344 * won't be able to use entire table.
1346 if (req->rss_sz && (req->rss_sz > MAX_RSS_INDIR_TBL_SIZE ||
1347 !is_power_of_2(req->rss_sz)))
1348 return NIX_AF_ERR_RSS_SIZE_INVALID;
1351 (!req->rss_grps || req->rss_grps > MAX_RSS_GROUPS))
1352 return NIX_AF_ERR_RSS_GRPS_INVALID;
1354 /* Reset this NIX LF */
1355 err = rvu_lf_reset(rvu, block, nixlf);
1357 dev_err(rvu->dev, "Failed to reset NIX%d LF%d\n",
1358 block->addr - BLKADDR_NIX0, nixlf);
1359 return NIX_AF_ERR_LF_RESET;
1362 ctx_cfg = rvu_read64(rvu, blkaddr, NIX_AF_CONST3);
1364 /* Alloc NIX RQ HW context memory and config the base */
1365 hwctx_size = 1UL << ((ctx_cfg >> 4) & 0xF);
1366 err = qmem_alloc(rvu->dev, &pfvf->rq_ctx, req->rq_cnt, hwctx_size);
1370 pfvf->rq_bmap = kcalloc(req->rq_cnt, sizeof(long), GFP_KERNEL);
1374 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RQS_BASE(nixlf),
1375 (u64)pfvf->rq_ctx->iova);
1377 /* Set caching and queue count in HW */
1378 cfg = BIT_ULL(36) | (req->rq_cnt - 1) | req->way_mask << 20;
1379 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RQS_CFG(nixlf), cfg);
1381 /* Alloc NIX SQ HW context memory and config the base */
1382 hwctx_size = 1UL << (ctx_cfg & 0xF);
1383 err = qmem_alloc(rvu->dev, &pfvf->sq_ctx, req->sq_cnt, hwctx_size);
1387 pfvf->sq_bmap = kcalloc(req->sq_cnt, sizeof(long), GFP_KERNEL);
1391 rvu_write64(rvu, blkaddr, NIX_AF_LFX_SQS_BASE(nixlf),
1392 (u64)pfvf->sq_ctx->iova);
1394 cfg = BIT_ULL(36) | (req->sq_cnt - 1) | req->way_mask << 20;
1395 rvu_write64(rvu, blkaddr, NIX_AF_LFX_SQS_CFG(nixlf), cfg);
1397 /* Alloc NIX CQ HW context memory and config the base */
1398 hwctx_size = 1UL << ((ctx_cfg >> 8) & 0xF);
1399 err = qmem_alloc(rvu->dev, &pfvf->cq_ctx, req->cq_cnt, hwctx_size);
1403 pfvf->cq_bmap = kcalloc(req->cq_cnt, sizeof(long), GFP_KERNEL);
1407 rvu_write64(rvu, blkaddr, NIX_AF_LFX_CQS_BASE(nixlf),
1408 (u64)pfvf->cq_ctx->iova);
1410 cfg = BIT_ULL(36) | (req->cq_cnt - 1) | req->way_mask << 20;
1411 rvu_write64(rvu, blkaddr, NIX_AF_LFX_CQS_CFG(nixlf), cfg);
1413 /* Initialize receive side scaling (RSS) */
1414 hwctx_size = 1UL << ((ctx_cfg >> 12) & 0xF);
1415 err = nixlf_rss_ctx_init(rvu, blkaddr, pfvf, nixlf, req->rss_sz,
1416 req->rss_grps, hwctx_size, req->way_mask,
1417 !!(req->flags & NIX_LF_RSS_TAG_LSB_AS_ADDER));
1421 /* Alloc memory for CQINT's HW contexts */
1422 cfg = rvu_read64(rvu, blkaddr, NIX_AF_CONST2);
1423 qints = (cfg >> 24) & 0xFFF;
1424 hwctx_size = 1UL << ((ctx_cfg >> 24) & 0xF);
1425 err = qmem_alloc(rvu->dev, &pfvf->cq_ints_ctx, qints, hwctx_size);
1429 rvu_write64(rvu, blkaddr, NIX_AF_LFX_CINTS_BASE(nixlf),
1430 (u64)pfvf->cq_ints_ctx->iova);
1432 rvu_write64(rvu, blkaddr, NIX_AF_LFX_CINTS_CFG(nixlf),
1433 BIT_ULL(36) | req->way_mask << 20);
1435 /* Alloc memory for QINT's HW contexts */
1436 cfg = rvu_read64(rvu, blkaddr, NIX_AF_CONST2);
1437 qints = (cfg >> 12) & 0xFFF;
1438 hwctx_size = 1UL << ((ctx_cfg >> 20) & 0xF);
1439 err = qmem_alloc(rvu->dev, &pfvf->nix_qints_ctx, qints, hwctx_size);
1443 rvu_write64(rvu, blkaddr, NIX_AF_LFX_QINTS_BASE(nixlf),
1444 (u64)pfvf->nix_qints_ctx->iova);
1445 rvu_write64(rvu, blkaddr, NIX_AF_LFX_QINTS_CFG(nixlf),
1446 BIT_ULL(36) | req->way_mask << 20);
1448 /* Setup VLANX TPID's.
1449 * Use VLAN1 for 802.1Q
1450 * and VLAN0 for 802.1AD.
1452 cfg = (0x8100ULL << 16) | 0x88A8ULL;
1453 rvu_write64(rvu, blkaddr, NIX_AF_LFX_TX_CFG(nixlf), cfg);
1455 /* Enable LMTST for this NIX LF */
1456 rvu_write64(rvu, blkaddr, NIX_AF_LFX_TX_CFG2(nixlf), BIT_ULL(0));
1458 /* Set CQE/WQE size, NPA_PF_FUNC for SQBs and also SSO_PF_FUNC */
1460 cfg = req->npa_func;
1462 cfg |= (u64)req->sso_func << 16;
1464 cfg |= (u64)req->xqe_sz << 33;
1465 rvu_write64(rvu, blkaddr, NIX_AF_LFX_CFG(nixlf), cfg);
1467 /* Config Rx pkt length, csum checks and apad enable / disable */
1468 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RX_CFG(nixlf), req->rx_cfg);
1470 /* Configure pkind for TX parse config */
1471 cfg = NPC_TX_DEF_PKIND;
1472 rvu_write64(rvu, blkaddr, NIX_AF_LFX_TX_PARSE_CFG(nixlf), cfg);
1474 intf = is_afvf(pcifunc) ? NIX_INTF_TYPE_LBK : NIX_INTF_TYPE_CGX;
1475 if (is_sdp_pfvf(pcifunc))
1476 intf = NIX_INTF_TYPE_SDP;
1478 err = nix_interface_init(rvu, pcifunc, intf, nixlf, rsp,
1479 !!(req->flags & NIX_LF_LBK_BLK_SEL));
1483 /* Disable NPC entries as NIXLF's contexts are not initialized yet */
1484 rvu_npc_disable_default_entries(rvu, pcifunc, nixlf);
1486 /* Configure RX VTAG Type 7 (strip) for vf vlan */
1487 rvu_write64(rvu, blkaddr,
1488 NIX_AF_LFX_RX_VTAG_TYPEX(nixlf, NIX_AF_LFX_RX_VTAG_TYPE7),
1489 VTAGSIZE_T4 | VTAG_STRIP);
1494 nix_ctx_free(rvu, pfvf);
1498 /* Set macaddr of this PF/VF */
1499 ether_addr_copy(rsp->mac_addr, pfvf->mac_addr);
1501 /* set SQB size info */
1502 cfg = rvu_read64(rvu, blkaddr, NIX_AF_SQ_CONST);
1503 rsp->sqb_size = (cfg >> 34) & 0xFFFF;
1504 rsp->rx_chan_base = pfvf->rx_chan_base;
1505 rsp->tx_chan_base = pfvf->tx_chan_base;
1506 rsp->rx_chan_cnt = pfvf->rx_chan_cnt;
1507 rsp->tx_chan_cnt = pfvf->tx_chan_cnt;
1508 rsp->lso_tsov4_idx = NIX_LSO_FORMAT_IDX_TSOV4;
1509 rsp->lso_tsov6_idx = NIX_LSO_FORMAT_IDX_TSOV6;
1510 /* Get HW supported stat count */
1511 cfg = rvu_read64(rvu, blkaddr, NIX_AF_CONST1);
1512 rsp->lf_rx_stats = ((cfg >> 32) & 0xFF);
1513 rsp->lf_tx_stats = ((cfg >> 24) & 0xFF);
1514 /* Get count of CQ IRQs and error IRQs supported per LF */
1515 cfg = rvu_read64(rvu, blkaddr, NIX_AF_CONST2);
1516 rsp->qints = ((cfg >> 12) & 0xFFF);
1517 rsp->cints = ((cfg >> 24) & 0xFFF);
1518 rsp->cgx_links = hw->cgx_links;
1519 rsp->lbk_links = hw->lbk_links;
1520 rsp->sdp_links = hw->sdp_links;
1525 int rvu_mbox_handler_nix_lf_free(struct rvu *rvu, struct nix_lf_free_req *req,
1526 struct msg_rsp *rsp)
1528 struct rvu_hwinfo *hw = rvu->hw;
1529 u16 pcifunc = req->hdr.pcifunc;
1530 struct rvu_block *block;
1531 int blkaddr, nixlf, err;
1532 struct rvu_pfvf *pfvf;
1534 pfvf = rvu_get_pfvf(rvu, pcifunc);
1535 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
1536 if (!pfvf->nixlf || blkaddr < 0)
1537 return NIX_AF_ERR_AF_LF_INVALID;
1539 block = &hw->block[blkaddr];
1540 nixlf = rvu_get_lf(rvu, block, pcifunc, 0);
1542 return NIX_AF_ERR_AF_LF_INVALID;
1544 if (req->flags & NIX_LF_DISABLE_FLOWS)
1545 rvu_npc_disable_mcam_entries(rvu, pcifunc, nixlf);
1547 rvu_npc_free_mcam_entries(rvu, pcifunc, nixlf);
1549 /* Free any tx vtag def entries used by this NIX LF */
1550 if (!(req->flags & NIX_LF_DONT_FREE_TX_VTAG))
1551 nix_free_tx_vtag_entries(rvu, pcifunc);
1553 nix_interface_deinit(rvu, pcifunc, nixlf);
1555 /* Reset this NIX LF */
1556 err = rvu_lf_reset(rvu, block, nixlf);
1558 dev_err(rvu->dev, "Failed to reset NIX%d LF%d\n",
1559 block->addr - BLKADDR_NIX0, nixlf);
1560 return NIX_AF_ERR_LF_RESET;
1563 nix_ctx_free(rvu, pfvf);
1568 int rvu_mbox_handler_nix_mark_format_cfg(struct rvu *rvu,
1569 struct nix_mark_format_cfg *req,
1570 struct nix_mark_format_cfg_rsp *rsp)
1572 u16 pcifunc = req->hdr.pcifunc;
1573 struct nix_hw *nix_hw;
1574 struct rvu_pfvf *pfvf;
1578 pfvf = rvu_get_pfvf(rvu, pcifunc);
1579 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
1580 if (!pfvf->nixlf || blkaddr < 0)
1581 return NIX_AF_ERR_AF_LF_INVALID;
1583 nix_hw = get_nix_hw(rvu->hw, blkaddr);
1585 return NIX_AF_ERR_INVALID_NIXBLK;
1587 cfg = (((u32)req->offset & 0x7) << 16) |
1588 (((u32)req->y_mask & 0xF) << 12) |
1589 (((u32)req->y_val & 0xF) << 8) |
1590 (((u32)req->r_mask & 0xF) << 4) | ((u32)req->r_val & 0xF);
1592 rc = rvu_nix_reserve_mark_format(rvu, nix_hw, blkaddr, cfg);
1594 dev_err(rvu->dev, "No mark_format_ctl for (pf:%d, vf:%d)",
1595 rvu_get_pf(pcifunc), pcifunc & RVU_PFVF_FUNC_MASK);
1596 return NIX_AF_ERR_MARK_CFG_FAIL;
1599 rsp->mark_format_idx = rc;
1603 /* Handle shaper update specially for few revisions */
1605 handle_txschq_shaper_update(struct rvu *rvu, int blkaddr, int nixlf,
1606 int lvl, u64 reg, u64 regval)
1608 u64 regbase, oldval, sw_xoff = 0;
1609 u64 dbgval, md_debug0 = 0;
1610 unsigned long poll_tmo;
1614 regbase = reg & 0xFFFF;
1615 schq = TXSCHQ_IDX(reg, TXSCHQ_IDX_SHIFT);
1617 /* Check for rate register */
1619 case NIX_TXSCH_LVL_TL1:
1620 md_debug0 = NIX_AF_TL1X_MD_DEBUG0(schq);
1621 sw_xoff = NIX_AF_TL1X_SW_XOFF(schq);
1623 rate_reg = !!(regbase == NIX_AF_TL1X_CIR(0));
1625 case NIX_TXSCH_LVL_TL2:
1626 md_debug0 = NIX_AF_TL2X_MD_DEBUG0(schq);
1627 sw_xoff = NIX_AF_TL2X_SW_XOFF(schq);
1629 rate_reg = (regbase == NIX_AF_TL2X_CIR(0) ||
1630 regbase == NIX_AF_TL2X_PIR(0));
1632 case NIX_TXSCH_LVL_TL3:
1633 md_debug0 = NIX_AF_TL3X_MD_DEBUG0(schq);
1634 sw_xoff = NIX_AF_TL3X_SW_XOFF(schq);
1636 rate_reg = (regbase == NIX_AF_TL3X_CIR(0) ||
1637 regbase == NIX_AF_TL3X_PIR(0));
1639 case NIX_TXSCH_LVL_TL4:
1640 md_debug0 = NIX_AF_TL4X_MD_DEBUG0(schq);
1641 sw_xoff = NIX_AF_TL4X_SW_XOFF(schq);
1643 rate_reg = (regbase == NIX_AF_TL4X_CIR(0) ||
1644 regbase == NIX_AF_TL4X_PIR(0));
1646 case NIX_TXSCH_LVL_MDQ:
1647 sw_xoff = NIX_AF_MDQX_SW_XOFF(schq);
1648 rate_reg = (regbase == NIX_AF_MDQX_CIR(0) ||
1649 regbase == NIX_AF_MDQX_PIR(0));
1656 /* Nothing special to do when state is not toggled */
1657 oldval = rvu_read64(rvu, blkaddr, reg);
1658 if ((oldval & 0x1) == (regval & 0x1)) {
1659 rvu_write64(rvu, blkaddr, reg, regval);
1663 /* PIR/CIR disable */
1664 if (!(regval & 0x1)) {
1665 rvu_write64(rvu, blkaddr, sw_xoff, 1);
1666 rvu_write64(rvu, blkaddr, reg, 0);
1668 rvu_write64(rvu, blkaddr, sw_xoff, 0);
1672 /* PIR/CIR enable */
1673 rvu_write64(rvu, blkaddr, sw_xoff, 1);
1675 poll_tmo = jiffies + usecs_to_jiffies(10000);
1676 /* Wait until VLD(bit32) == 1 or C_CON(bit48) == 0 */
1678 if (time_after(jiffies, poll_tmo)) {
1680 "NIXLF%d: TLX%u(lvl %u) CIR/PIR enable failed\n",
1685 dbgval = rvu_read64(rvu, blkaddr, md_debug0);
1686 } while (!(dbgval & BIT_ULL(32)) && (dbgval & BIT_ULL(48)));
1688 rvu_write64(rvu, blkaddr, reg, regval);
1690 rvu_write64(rvu, blkaddr, sw_xoff, 0);
1694 /* Disable shaping of pkts by a scheduler queue
1695 * at a given scheduler level.
1697 static void nix_reset_tx_shaping(struct rvu *rvu, int blkaddr,
1698 int nixlf, int lvl, int schq)
1700 struct rvu_hwinfo *hw = rvu->hw;
1701 u64 cir_reg = 0, pir_reg = 0;
1705 case NIX_TXSCH_LVL_TL1:
1706 cir_reg = NIX_AF_TL1X_CIR(schq);
1707 pir_reg = 0; /* PIR not available at TL1 */
1709 case NIX_TXSCH_LVL_TL2:
1710 cir_reg = NIX_AF_TL2X_CIR(schq);
1711 pir_reg = NIX_AF_TL2X_PIR(schq);
1713 case NIX_TXSCH_LVL_TL3:
1714 cir_reg = NIX_AF_TL3X_CIR(schq);
1715 pir_reg = NIX_AF_TL3X_PIR(schq);
1717 case NIX_TXSCH_LVL_TL4:
1718 cir_reg = NIX_AF_TL4X_CIR(schq);
1719 pir_reg = NIX_AF_TL4X_PIR(schq);
1721 case NIX_TXSCH_LVL_MDQ:
1722 cir_reg = NIX_AF_MDQX_CIR(schq);
1723 pir_reg = NIX_AF_MDQX_PIR(schq);
1727 /* Shaper state toggle needs wait/poll */
1728 if (hw->cap.nix_shaper_toggle_wait) {
1730 handle_txschq_shaper_update(rvu, blkaddr, nixlf,
1733 handle_txschq_shaper_update(rvu, blkaddr, nixlf,
1740 cfg = rvu_read64(rvu, blkaddr, cir_reg);
1741 rvu_write64(rvu, blkaddr, cir_reg, cfg & ~BIT_ULL(0));
1745 cfg = rvu_read64(rvu, blkaddr, pir_reg);
1746 rvu_write64(rvu, blkaddr, pir_reg, cfg & ~BIT_ULL(0));
1749 static void nix_reset_tx_linkcfg(struct rvu *rvu, int blkaddr,
1752 struct rvu_hwinfo *hw = rvu->hw;
1756 if (lvl >= hw->cap.nix_tx_aggr_lvl)
1759 /* Reset TL4's SDP link config */
1760 if (lvl == NIX_TXSCH_LVL_TL4)
1761 rvu_write64(rvu, blkaddr, NIX_AF_TL4X_SDP_LINK_CFG(schq), 0x00);
1763 link_level = rvu_read64(rvu, blkaddr, NIX_AF_PSE_CHANNEL_LEVEL) & 0x01 ?
1764 NIX_TXSCH_LVL_TL3 : NIX_TXSCH_LVL_TL2;
1765 if (lvl != link_level)
1768 /* Reset TL2's CGX or LBK link config */
1769 for (link = 0; link < (hw->cgx_links + hw->lbk_links); link++)
1770 rvu_write64(rvu, blkaddr,
1771 NIX_AF_TL3_TL2X_LINKX_CFG(schq, link), 0x00);
1774 static void nix_clear_tx_xoff(struct rvu *rvu, int blkaddr,
1777 struct rvu_hwinfo *hw = rvu->hw;
1780 /* Skip this if shaping is not supported */
1781 if (!hw->cap.nix_shaping)
1784 /* Clear level specific SW_XOFF */
1786 case NIX_TXSCH_LVL_TL1:
1787 reg = NIX_AF_TL1X_SW_XOFF(schq);
1789 case NIX_TXSCH_LVL_TL2:
1790 reg = NIX_AF_TL2X_SW_XOFF(schq);
1792 case NIX_TXSCH_LVL_TL3:
1793 reg = NIX_AF_TL3X_SW_XOFF(schq);
1795 case NIX_TXSCH_LVL_TL4:
1796 reg = NIX_AF_TL4X_SW_XOFF(schq);
1798 case NIX_TXSCH_LVL_MDQ:
1799 reg = NIX_AF_MDQX_SW_XOFF(schq);
1805 rvu_write64(rvu, blkaddr, reg, 0x0);
1808 static int nix_get_tx_link(struct rvu *rvu, u16 pcifunc)
1810 struct rvu_hwinfo *hw = rvu->hw;
1811 int pf = rvu_get_pf(pcifunc);
1812 u8 cgx_id = 0, lmac_id = 0;
1814 if (is_afvf(pcifunc)) {/* LBK links */
1815 return hw->cgx_links;
1816 } else if (is_pf_cgxmapped(rvu, pf)) {
1817 rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
1818 return (cgx_id * hw->lmac_per_cgx) + lmac_id;
1822 return hw->cgx_links + hw->lbk_links;
1825 static void nix_get_txschq_range(struct rvu *rvu, u16 pcifunc,
1826 int link, int *start, int *end)
1828 struct rvu_hwinfo *hw = rvu->hw;
1829 int pf = rvu_get_pf(pcifunc);
1831 if (is_afvf(pcifunc)) { /* LBK links */
1832 *start = hw->cap.nix_txsch_per_cgx_lmac * link;
1833 *end = *start + hw->cap.nix_txsch_per_lbk_lmac;
1834 } else if (is_pf_cgxmapped(rvu, pf)) { /* CGX links */
1835 *start = hw->cap.nix_txsch_per_cgx_lmac * link;
1836 *end = *start + hw->cap.nix_txsch_per_cgx_lmac;
1837 } else { /* SDP link */
1838 *start = (hw->cap.nix_txsch_per_cgx_lmac * hw->cgx_links) +
1839 (hw->cap.nix_txsch_per_lbk_lmac * hw->lbk_links);
1840 *end = *start + hw->cap.nix_txsch_per_sdp_lmac;
1844 static int nix_check_txschq_alloc_req(struct rvu *rvu, int lvl, u16 pcifunc,
1845 struct nix_hw *nix_hw,
1846 struct nix_txsch_alloc_req *req)
1848 struct rvu_hwinfo *hw = rvu->hw;
1849 int schq, req_schq, free_cnt;
1850 struct nix_txsch *txsch;
1851 int link, start, end;
1853 txsch = &nix_hw->txsch[lvl];
1854 req_schq = req->schq_contig[lvl] + req->schq[lvl];
1859 link = nix_get_tx_link(rvu, pcifunc);
1861 /* For traffic aggregating scheduler level, one queue is enough */
1862 if (lvl >= hw->cap.nix_tx_aggr_lvl) {
1864 return NIX_AF_ERR_TLX_ALLOC_FAIL;
1868 /* Get free SCHQ count and check if request can be accomodated */
1869 if (hw->cap.nix_fixed_txschq_mapping) {
1870 nix_get_txschq_range(rvu, pcifunc, link, &start, &end);
1871 schq = start + (pcifunc & RVU_PFVF_FUNC_MASK);
1872 if (end <= txsch->schq.max && schq < end &&
1873 !test_bit(schq, txsch->schq.bmap))
1878 free_cnt = rvu_rsrc_free_count(&txsch->schq);
1881 if (free_cnt < req_schq || req_schq > MAX_TXSCHQ_PER_FUNC)
1882 return NIX_AF_ERR_TLX_ALLOC_FAIL;
1884 /* If contiguous queues are needed, check for availability */
1885 if (!hw->cap.nix_fixed_txschq_mapping && req->schq_contig[lvl] &&
1886 !rvu_rsrc_check_contig(&txsch->schq, req->schq_contig[lvl]))
1887 return NIX_AF_ERR_TLX_ALLOC_FAIL;
1892 static void nix_txsch_alloc(struct rvu *rvu, struct nix_txsch *txsch,
1893 struct nix_txsch_alloc_rsp *rsp,
1894 int lvl, int start, int end)
1896 struct rvu_hwinfo *hw = rvu->hw;
1897 u16 pcifunc = rsp->hdr.pcifunc;
1900 /* For traffic aggregating levels, queue alloc is based
1901 * on transmit link to which PF_FUNC is mapped to.
1903 if (lvl >= hw->cap.nix_tx_aggr_lvl) {
1904 /* A single TL queue is allocated */
1905 if (rsp->schq_contig[lvl]) {
1906 rsp->schq_contig[lvl] = 1;
1907 rsp->schq_contig_list[lvl][0] = start;
1910 /* Both contig and non-contig reqs doesn't make sense here */
1911 if (rsp->schq_contig[lvl])
1914 if (rsp->schq[lvl]) {
1916 rsp->schq_list[lvl][0] = start;
1921 /* Adjust the queue request count if HW supports
1922 * only one queue per level configuration.
1924 if (hw->cap.nix_fixed_txschq_mapping) {
1925 idx = pcifunc & RVU_PFVF_FUNC_MASK;
1927 if (idx >= (end - start) || test_bit(schq, txsch->schq.bmap)) {
1928 rsp->schq_contig[lvl] = 0;
1933 if (rsp->schq_contig[lvl]) {
1934 rsp->schq_contig[lvl] = 1;
1935 set_bit(schq, txsch->schq.bmap);
1936 rsp->schq_contig_list[lvl][0] = schq;
1938 } else if (rsp->schq[lvl]) {
1940 set_bit(schq, txsch->schq.bmap);
1941 rsp->schq_list[lvl][0] = schq;
1946 /* Allocate contiguous queue indices requesty first */
1947 if (rsp->schq_contig[lvl]) {
1948 schq = bitmap_find_next_zero_area(txsch->schq.bmap,
1949 txsch->schq.max, start,
1950 rsp->schq_contig[lvl], 0);
1952 rsp->schq_contig[lvl] = 0;
1953 for (idx = 0; idx < rsp->schq_contig[lvl]; idx++) {
1954 set_bit(schq, txsch->schq.bmap);
1955 rsp->schq_contig_list[lvl][idx] = schq;
1960 /* Allocate non-contiguous queue indices */
1961 if (rsp->schq[lvl]) {
1963 for (schq = start; schq < end; schq++) {
1964 if (!test_bit(schq, txsch->schq.bmap)) {
1965 set_bit(schq, txsch->schq.bmap);
1966 rsp->schq_list[lvl][idx++] = schq;
1968 if (idx == rsp->schq[lvl])
1971 /* Update how many were allocated */
1972 rsp->schq[lvl] = idx;
1976 int rvu_mbox_handler_nix_txsch_alloc(struct rvu *rvu,
1977 struct nix_txsch_alloc_req *req,
1978 struct nix_txsch_alloc_rsp *rsp)
1980 struct rvu_hwinfo *hw = rvu->hw;
1981 u16 pcifunc = req->hdr.pcifunc;
1982 int link, blkaddr, rc = 0;
1983 int lvl, idx, start, end;
1984 struct nix_txsch *txsch;
1985 struct nix_hw *nix_hw;
1990 rc = nix_get_nixlf(rvu, pcifunc, &nixlf, &blkaddr);
1994 nix_hw = get_nix_hw(rvu->hw, blkaddr);
1996 return NIX_AF_ERR_INVALID_NIXBLK;
1998 mutex_lock(&rvu->rsrc_lock);
2000 /* Check if request is valid as per HW capabilities
2001 * and can be accomodated.
2003 for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
2004 rc = nix_check_txschq_alloc_req(rvu, lvl, pcifunc, nix_hw, req);
2009 /* Allocate requested Tx scheduler queues */
2010 for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
2011 txsch = &nix_hw->txsch[lvl];
2012 pfvf_map = txsch->pfvf_map;
2014 if (!req->schq[lvl] && !req->schq_contig[lvl])
2017 rsp->schq[lvl] = req->schq[lvl];
2018 rsp->schq_contig[lvl] = req->schq_contig[lvl];
2020 link = nix_get_tx_link(rvu, pcifunc);
2022 if (lvl >= hw->cap.nix_tx_aggr_lvl) {
2025 } else if (hw->cap.nix_fixed_txschq_mapping) {
2026 nix_get_txschq_range(rvu, pcifunc, link, &start, &end);
2029 end = txsch->schq.max;
2032 nix_txsch_alloc(rvu, txsch, rsp, lvl, start, end);
2034 /* Reset queue config */
2035 for (idx = 0; idx < req->schq_contig[lvl]; idx++) {
2036 schq = rsp->schq_contig_list[lvl][idx];
2037 if (!(TXSCH_MAP_FLAGS(pfvf_map[schq]) &
2038 NIX_TXSCHQ_CFG_DONE))
2039 pfvf_map[schq] = TXSCH_MAP(pcifunc, 0);
2040 nix_reset_tx_linkcfg(rvu, blkaddr, lvl, schq);
2041 nix_reset_tx_shaping(rvu, blkaddr, nixlf, lvl, schq);
2044 for (idx = 0; idx < req->schq[lvl]; idx++) {
2045 schq = rsp->schq_list[lvl][idx];
2046 if (!(TXSCH_MAP_FLAGS(pfvf_map[schq]) &
2047 NIX_TXSCHQ_CFG_DONE))
2048 pfvf_map[schq] = TXSCH_MAP(pcifunc, 0);
2049 nix_reset_tx_linkcfg(rvu, blkaddr, lvl, schq);
2050 nix_reset_tx_shaping(rvu, blkaddr, nixlf, lvl, schq);
2054 rsp->aggr_level = hw->cap.nix_tx_aggr_lvl;
2055 rsp->aggr_lvl_rr_prio = TXSCH_TL1_DFLT_RR_PRIO;
2056 rsp->link_cfg_lvl = rvu_read64(rvu, blkaddr,
2057 NIX_AF_PSE_CHANNEL_LEVEL) & 0x01 ?
2058 NIX_TXSCH_LVL_TL3 : NIX_TXSCH_LVL_TL2;
2061 rc = NIX_AF_ERR_TLX_ALLOC_FAIL;
2063 mutex_unlock(&rvu->rsrc_lock);
2067 static int nix_smq_flush(struct rvu *rvu, int blkaddr,
2068 int smq, u16 pcifunc, int nixlf)
2070 int pf = rvu_get_pf(pcifunc);
2071 u8 cgx_id = 0, lmac_id = 0;
2072 int err, restore_tx_en = 0;
2075 if (!is_rvu_otx2(rvu)) {
2076 /* Skip SMQ flush if pkt count is zero */
2077 cfg = rvu_read64(rvu, blkaddr, NIX_AF_MDQX_IN_MD_COUNT(smq));
2082 /* enable cgx tx if disabled */
2083 if (is_pf_cgxmapped(rvu, pf)) {
2084 rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
2085 restore_tx_en = !rvu_cgx_config_tx(rvu_cgx_pdata(cgx_id, rvu),
2089 cfg = rvu_read64(rvu, blkaddr, NIX_AF_SMQX_CFG(smq));
2090 /* Do SMQ flush and set enqueue xoff */
2091 cfg |= BIT_ULL(50) | BIT_ULL(49);
2092 rvu_write64(rvu, blkaddr, NIX_AF_SMQX_CFG(smq), cfg);
2094 /* Disable backpressure from physical link,
2095 * otherwise SMQ flush may stall.
2097 rvu_cgx_enadis_rx_bp(rvu, pf, false);
2099 /* Wait for flush to complete */
2100 err = rvu_poll_reg(rvu, blkaddr,
2101 NIX_AF_SMQX_CFG(smq), BIT_ULL(49), true);
2104 "NIXLF%d: SMQ%d flush failed\n", nixlf, smq);
2106 rvu_cgx_enadis_rx_bp(rvu, pf, true);
2107 /* restore cgx tx state */
2109 rvu_cgx_config_tx(rvu_cgx_pdata(cgx_id, rvu), lmac_id, false);
2113 static int nix_txschq_free(struct rvu *rvu, u16 pcifunc)
2115 int blkaddr, nixlf, lvl, schq, err;
2116 struct rvu_hwinfo *hw = rvu->hw;
2117 struct nix_txsch *txsch;
2118 struct nix_hw *nix_hw;
2121 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
2123 return NIX_AF_ERR_AF_LF_INVALID;
2125 nix_hw = get_nix_hw(rvu->hw, blkaddr);
2127 return NIX_AF_ERR_INVALID_NIXBLK;
2129 nixlf = rvu_get_lf(rvu, &hw->block[blkaddr], pcifunc, 0);
2131 return NIX_AF_ERR_AF_LF_INVALID;
2133 /* Disable TL2/3 queue links and all XOFF's before SMQ flush*/
2134 mutex_lock(&rvu->rsrc_lock);
2135 for (lvl = NIX_TXSCH_LVL_MDQ; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
2136 txsch = &nix_hw->txsch[lvl];
2138 if (lvl >= hw->cap.nix_tx_aggr_lvl)
2141 for (schq = 0; schq < txsch->schq.max; schq++) {
2142 if (TXSCH_MAP_FUNC(txsch->pfvf_map[schq]) != pcifunc)
2144 nix_reset_tx_linkcfg(rvu, blkaddr, lvl, schq);
2145 nix_clear_tx_xoff(rvu, blkaddr, lvl, schq);
2148 nix_clear_tx_xoff(rvu, blkaddr, NIX_TXSCH_LVL_TL1,
2149 nix_get_tx_link(rvu, pcifunc));
2151 /* On PF cleanup, clear cfg done flag as
2152 * PF would have changed default config.
2154 if (!(pcifunc & RVU_PFVF_FUNC_MASK)) {
2155 txsch = &nix_hw->txsch[NIX_TXSCH_LVL_TL1];
2156 schq = nix_get_tx_link(rvu, pcifunc);
2157 /* Do not clear pcifunc in txsch->pfvf_map[schq] because
2158 * VF might be using this TL1 queue
2160 map_func = TXSCH_MAP_FUNC(txsch->pfvf_map[schq]);
2161 txsch->pfvf_map[schq] = TXSCH_SET_FLAG(map_func, 0x0);
2165 txsch = &nix_hw->txsch[NIX_TXSCH_LVL_SMQ];
2166 for (schq = 0; schq < txsch->schq.max; schq++) {
2167 if (TXSCH_MAP_FUNC(txsch->pfvf_map[schq]) != pcifunc)
2169 nix_smq_flush(rvu, blkaddr, schq, pcifunc, nixlf);
2172 /* Now free scheduler queues to free pool */
2173 for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
2174 /* TLs above aggregation level are shared across all PF
2175 * and it's VFs, hence skip freeing them.
2177 if (lvl >= hw->cap.nix_tx_aggr_lvl)
2180 txsch = &nix_hw->txsch[lvl];
2181 for (schq = 0; schq < txsch->schq.max; schq++) {
2182 if (TXSCH_MAP_FUNC(txsch->pfvf_map[schq]) != pcifunc)
2184 rvu_free_rsrc(&txsch->schq, schq);
2185 txsch->pfvf_map[schq] = TXSCH_MAP(0, NIX_TXSCHQ_FREE);
2188 mutex_unlock(&rvu->rsrc_lock);
2190 /* Sync cached info for this LF in NDC-TX to LLC/DRAM */
2191 rvu_write64(rvu, blkaddr, NIX_AF_NDC_TX_SYNC, BIT_ULL(12) | nixlf);
2192 err = rvu_poll_reg(rvu, blkaddr, NIX_AF_NDC_TX_SYNC, BIT_ULL(12), true);
2194 dev_err(rvu->dev, "NDC-TX sync failed for NIXLF %d\n", nixlf);
2199 static int nix_txschq_free_one(struct rvu *rvu,
2200 struct nix_txsch_free_req *req)
2202 struct rvu_hwinfo *hw = rvu->hw;
2203 u16 pcifunc = req->hdr.pcifunc;
2204 int lvl, schq, nixlf, blkaddr;
2205 struct nix_txsch *txsch;
2206 struct nix_hw *nix_hw;
2210 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
2212 return NIX_AF_ERR_AF_LF_INVALID;
2214 nix_hw = get_nix_hw(rvu->hw, blkaddr);
2216 return NIX_AF_ERR_INVALID_NIXBLK;
2218 nixlf = rvu_get_lf(rvu, &hw->block[blkaddr], pcifunc, 0);
2220 return NIX_AF_ERR_AF_LF_INVALID;
2222 lvl = req->schq_lvl;
2224 txsch = &nix_hw->txsch[lvl];
2226 if (lvl >= hw->cap.nix_tx_aggr_lvl || schq >= txsch->schq.max)
2229 pfvf_map = txsch->pfvf_map;
2230 mutex_lock(&rvu->rsrc_lock);
2232 if (TXSCH_MAP_FUNC(pfvf_map[schq]) != pcifunc) {
2233 rc = NIX_AF_ERR_TLX_INVALID;
2237 /* Clear SW_XOFF of this resource only.
2238 * For SMQ level, all path XOFF's
2239 * need to be made clear by user
2241 nix_clear_tx_xoff(rvu, blkaddr, lvl, schq);
2243 /* Flush if it is a SMQ. Onus of disabling
2244 * TL2/3 queue links before SMQ flush is on user
2246 if (lvl == NIX_TXSCH_LVL_SMQ &&
2247 nix_smq_flush(rvu, blkaddr, schq, pcifunc, nixlf)) {
2248 rc = NIX_AF_SMQ_FLUSH_FAILED;
2252 /* Free the resource */
2253 rvu_free_rsrc(&txsch->schq, schq);
2254 txsch->pfvf_map[schq] = TXSCH_MAP(0, NIX_TXSCHQ_FREE);
2255 mutex_unlock(&rvu->rsrc_lock);
2258 mutex_unlock(&rvu->rsrc_lock);
2262 int rvu_mbox_handler_nix_txsch_free(struct rvu *rvu,
2263 struct nix_txsch_free_req *req,
2264 struct msg_rsp *rsp)
2266 if (req->flags & TXSCHQ_FREE_ALL)
2267 return nix_txschq_free(rvu, req->hdr.pcifunc);
2269 return nix_txschq_free_one(rvu, req);
2272 static bool is_txschq_hierarchy_valid(struct rvu *rvu, u16 pcifunc, int blkaddr,
2273 int lvl, u64 reg, u64 regval)
2275 u64 regbase = reg & 0xFFFF;
2278 if (!rvu_check_valid_reg(TXSCHQ_HWREGMAP, lvl, reg))
2281 schq = TXSCHQ_IDX(reg, TXSCHQ_IDX_SHIFT);
2282 /* Check if this schq belongs to this PF/VF or not */
2283 if (!is_valid_txschq(rvu, blkaddr, lvl, pcifunc, schq))
2286 parent = (regval >> 16) & 0x1FF;
2287 /* Validate MDQ's TL4 parent */
2288 if (regbase == NIX_AF_MDQX_PARENT(0) &&
2289 !is_valid_txschq(rvu, blkaddr, NIX_TXSCH_LVL_TL4, pcifunc, parent))
2292 /* Validate TL4's TL3 parent */
2293 if (regbase == NIX_AF_TL4X_PARENT(0) &&
2294 !is_valid_txschq(rvu, blkaddr, NIX_TXSCH_LVL_TL3, pcifunc, parent))
2297 /* Validate TL3's TL2 parent */
2298 if (regbase == NIX_AF_TL3X_PARENT(0) &&
2299 !is_valid_txschq(rvu, blkaddr, NIX_TXSCH_LVL_TL2, pcifunc, parent))
2302 /* Validate TL2's TL1 parent */
2303 if (regbase == NIX_AF_TL2X_PARENT(0) &&
2304 !is_valid_txschq(rvu, blkaddr, NIX_TXSCH_LVL_TL1, pcifunc, parent))
2310 static bool is_txschq_shaping_valid(struct rvu_hwinfo *hw, int lvl, u64 reg)
2314 if (hw->cap.nix_shaping)
2317 /* If shaping and coloring is not supported, then
2318 * *_CIR and *_PIR registers should not be configured.
2320 regbase = reg & 0xFFFF;
2323 case NIX_TXSCH_LVL_TL1:
2324 if (regbase == NIX_AF_TL1X_CIR(0))
2327 case NIX_TXSCH_LVL_TL2:
2328 if (regbase == NIX_AF_TL2X_CIR(0) ||
2329 regbase == NIX_AF_TL2X_PIR(0))
2332 case NIX_TXSCH_LVL_TL3:
2333 if (regbase == NIX_AF_TL3X_CIR(0) ||
2334 regbase == NIX_AF_TL3X_PIR(0))
2337 case NIX_TXSCH_LVL_TL4:
2338 if (regbase == NIX_AF_TL4X_CIR(0) ||
2339 regbase == NIX_AF_TL4X_PIR(0))
2342 case NIX_TXSCH_LVL_MDQ:
2343 if (regbase == NIX_AF_MDQX_CIR(0) ||
2344 regbase == NIX_AF_MDQX_PIR(0))
2351 static void nix_tl1_default_cfg(struct rvu *rvu, struct nix_hw *nix_hw,
2352 u16 pcifunc, int blkaddr)
2357 schq = nix_get_tx_link(rvu, pcifunc);
2358 pfvf_map = nix_hw->txsch[NIX_TXSCH_LVL_TL1].pfvf_map;
2359 /* Skip if PF has already done the config */
2360 if (TXSCH_MAP_FLAGS(pfvf_map[schq]) & NIX_TXSCHQ_CFG_DONE)
2362 rvu_write64(rvu, blkaddr, NIX_AF_TL1X_TOPOLOGY(schq),
2363 (TXSCH_TL1_DFLT_RR_PRIO << 1));
2365 /* On OcteonTx2 the config was in bytes and newer silcons
2366 * it's changed to weight.
2368 if (!rvu->hw->cap.nix_common_dwrr_mtu)
2369 rvu_write64(rvu, blkaddr, NIX_AF_TL1X_SCHEDULE(schq),
2370 TXSCH_TL1_DFLT_RR_QTM);
2372 rvu_write64(rvu, blkaddr, NIX_AF_TL1X_SCHEDULE(schq),
2373 CN10K_MAX_DWRR_WEIGHT);
2375 rvu_write64(rvu, blkaddr, NIX_AF_TL1X_CIR(schq), 0x00);
2376 pfvf_map[schq] = TXSCH_SET_FLAG(pfvf_map[schq], NIX_TXSCHQ_CFG_DONE);
2379 /* Register offset - [15:0]
2380 * Scheduler Queue number - [25:16]
2382 #define NIX_TX_SCHQ_MASK GENMASK_ULL(25, 0)
2384 static int nix_txschq_cfg_read(struct rvu *rvu, struct nix_hw *nix_hw,
2385 int blkaddr, struct nix_txschq_config *req,
2386 struct nix_txschq_config *rsp)
2388 u16 pcifunc = req->hdr.pcifunc;
2392 for (idx = 0; idx < req->num_regs; idx++) {
2393 reg = req->reg[idx];
2394 reg &= NIX_TX_SCHQ_MASK;
2395 schq = TXSCHQ_IDX(reg, TXSCHQ_IDX_SHIFT);
2396 if (!rvu_check_valid_reg(TXSCHQ_HWREGMAP, req->lvl, reg) ||
2397 !is_valid_txschq(rvu, blkaddr, req->lvl, pcifunc, schq))
2398 return NIX_AF_INVAL_TXSCHQ_CFG;
2399 rsp->regval[idx] = rvu_read64(rvu, blkaddr, reg);
2401 rsp->lvl = req->lvl;
2402 rsp->num_regs = req->num_regs;
2406 static void rvu_nix_tx_tl2_cfg(struct rvu *rvu, int blkaddr,
2407 u16 pcifunc, struct nix_txsch *txsch)
2409 struct rvu_hwinfo *hw = rvu->hw;
2410 int lbk_link_start, lbk_links;
2411 u8 pf = rvu_get_pf(pcifunc);
2414 if (!is_pf_cgxmapped(rvu, pf))
2417 lbk_link_start = hw->cgx_links;
2419 for (schq = 0; schq < txsch->schq.max; schq++) {
2420 if (TXSCH_MAP_FUNC(txsch->pfvf_map[schq]) != pcifunc)
2422 /* Enable all LBK links with channel 63 by default so that
2423 * packets can be sent to LBK with a NPC TX MCAM rule
2425 lbk_links = hw->lbk_links;
2427 rvu_write64(rvu, blkaddr,
2428 NIX_AF_TL3_TL2X_LINKX_CFG(schq,
2431 BIT_ULL(12) | RVU_SWITCH_LBK_CHAN);
2435 int rvu_mbox_handler_nix_txschq_cfg(struct rvu *rvu,
2436 struct nix_txschq_config *req,
2437 struct nix_txschq_config *rsp)
2439 u64 reg, val, regval, schq_regbase, val_mask;
2440 struct rvu_hwinfo *hw = rvu->hw;
2441 u16 pcifunc = req->hdr.pcifunc;
2442 struct nix_txsch *txsch;
2443 struct nix_hw *nix_hw;
2444 int blkaddr, idx, err;
2448 if (req->lvl >= NIX_TXSCH_LVL_CNT ||
2449 req->num_regs > MAX_REGS_PER_MBOX_MSG)
2450 return NIX_AF_INVAL_TXSCHQ_CFG;
2452 err = nix_get_nixlf(rvu, pcifunc, &nixlf, &blkaddr);
2456 nix_hw = get_nix_hw(rvu->hw, blkaddr);
2458 return NIX_AF_ERR_INVALID_NIXBLK;
2461 return nix_txschq_cfg_read(rvu, nix_hw, blkaddr, req, rsp);
2463 txsch = &nix_hw->txsch[req->lvl];
2464 pfvf_map = txsch->pfvf_map;
2466 if (req->lvl >= hw->cap.nix_tx_aggr_lvl &&
2467 pcifunc & RVU_PFVF_FUNC_MASK) {
2468 mutex_lock(&rvu->rsrc_lock);
2469 if (req->lvl == NIX_TXSCH_LVL_TL1)
2470 nix_tl1_default_cfg(rvu, nix_hw, pcifunc, blkaddr);
2471 mutex_unlock(&rvu->rsrc_lock);
2475 for (idx = 0; idx < req->num_regs; idx++) {
2476 reg = req->reg[idx];
2477 reg &= NIX_TX_SCHQ_MASK;
2478 regval = req->regval[idx];
2479 schq_regbase = reg & 0xFFFF;
2480 val_mask = req->regval_mask[idx];
2482 if (!is_txschq_hierarchy_valid(rvu, pcifunc, blkaddr,
2483 txsch->lvl, reg, regval))
2484 return NIX_AF_INVAL_TXSCHQ_CFG;
2486 /* Check if shaping and coloring is supported */
2487 if (!is_txschq_shaping_valid(hw, req->lvl, reg))
2490 val = rvu_read64(rvu, blkaddr, reg);
2491 regval = (val & val_mask) | (regval & ~val_mask);
2493 /* Handle shaping state toggle specially */
2494 if (hw->cap.nix_shaper_toggle_wait &&
2495 handle_txschq_shaper_update(rvu, blkaddr, nixlf,
2496 req->lvl, reg, regval))
2499 /* Replace PF/VF visible NIXLF slot with HW NIXLF id */
2500 if (schq_regbase == NIX_AF_SMQX_CFG(0)) {
2501 nixlf = rvu_get_lf(rvu, &hw->block[blkaddr],
2503 regval &= ~(0x7FULL << 24);
2504 regval |= ((u64)nixlf << 24);
2507 /* Clear 'BP_ENA' config, if it's not allowed */
2508 if (!hw->cap.nix_tx_link_bp) {
2509 if (schq_regbase == NIX_AF_TL4X_SDP_LINK_CFG(0) ||
2510 (schq_regbase & 0xFF00) ==
2511 NIX_AF_TL3_TL2X_LINKX_CFG(0, 0))
2512 regval &= ~BIT_ULL(13);
2515 /* Mark config as done for TL1 by PF */
2516 if (schq_regbase >= NIX_AF_TL1X_SCHEDULE(0) &&
2517 schq_regbase <= NIX_AF_TL1X_GREEN_BYTES(0)) {
2518 schq = TXSCHQ_IDX(reg, TXSCHQ_IDX_SHIFT);
2519 mutex_lock(&rvu->rsrc_lock);
2520 pfvf_map[schq] = TXSCH_SET_FLAG(pfvf_map[schq],
2521 NIX_TXSCHQ_CFG_DONE);
2522 mutex_unlock(&rvu->rsrc_lock);
2525 /* SMQ flush is special hence split register writes such
2526 * that flush first and write rest of the bits later.
2528 if (schq_regbase == NIX_AF_SMQX_CFG(0) &&
2529 (regval & BIT_ULL(49))) {
2530 schq = TXSCHQ_IDX(reg, TXSCHQ_IDX_SHIFT);
2531 nix_smq_flush(rvu, blkaddr, schq, pcifunc, nixlf);
2532 regval &= ~BIT_ULL(49);
2534 rvu_write64(rvu, blkaddr, reg, regval);
2537 rvu_nix_tx_tl2_cfg(rvu, blkaddr, pcifunc,
2538 &nix_hw->txsch[NIX_TXSCH_LVL_TL2]);
2542 static int nix_rx_vtag_cfg(struct rvu *rvu, int nixlf, int blkaddr,
2543 struct nix_vtag_config *req)
2545 u64 regval = req->vtag_size;
2547 if (req->rx.vtag_type > NIX_AF_LFX_RX_VTAG_TYPE7 ||
2548 req->vtag_size > VTAGSIZE_T8)
2551 /* RX VTAG Type 7 reserved for vf vlan */
2552 if (req->rx.vtag_type == NIX_AF_LFX_RX_VTAG_TYPE7)
2553 return NIX_AF_ERR_RX_VTAG_INUSE;
2555 if (req->rx.capture_vtag)
2556 regval |= BIT_ULL(5);
2557 if (req->rx.strip_vtag)
2558 regval |= BIT_ULL(4);
2560 rvu_write64(rvu, blkaddr,
2561 NIX_AF_LFX_RX_VTAG_TYPEX(nixlf, req->rx.vtag_type), regval);
2565 static int nix_tx_vtag_free(struct rvu *rvu, int blkaddr,
2566 u16 pcifunc, int index)
2568 struct nix_hw *nix_hw = get_nix_hw(rvu->hw, blkaddr);
2569 struct nix_txvlan *vlan;
2572 return NIX_AF_ERR_INVALID_NIXBLK;
2574 vlan = &nix_hw->txvlan;
2575 if (vlan->entry2pfvf_map[index] != pcifunc)
2576 return NIX_AF_ERR_PARAM;
2578 rvu_write64(rvu, blkaddr,
2579 NIX_AF_TX_VTAG_DEFX_DATA(index), 0x0ull);
2580 rvu_write64(rvu, blkaddr,
2581 NIX_AF_TX_VTAG_DEFX_CTL(index), 0x0ull);
2583 vlan->entry2pfvf_map[index] = 0;
2584 rvu_free_rsrc(&vlan->rsrc, index);
2589 static void nix_free_tx_vtag_entries(struct rvu *rvu, u16 pcifunc)
2591 struct nix_txvlan *vlan;
2592 struct nix_hw *nix_hw;
2595 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
2599 nix_hw = get_nix_hw(rvu->hw, blkaddr);
2603 vlan = &nix_hw->txvlan;
2605 mutex_lock(&vlan->rsrc_lock);
2606 /* Scan all the entries and free the ones mapped to 'pcifunc' */
2607 for (index = 0; index < vlan->rsrc.max; index++) {
2608 if (vlan->entry2pfvf_map[index] == pcifunc)
2609 nix_tx_vtag_free(rvu, blkaddr, pcifunc, index);
2611 mutex_unlock(&vlan->rsrc_lock);
2614 static int nix_tx_vtag_alloc(struct rvu *rvu, int blkaddr,
2617 struct nix_hw *nix_hw = get_nix_hw(rvu->hw, blkaddr);
2618 struct nix_txvlan *vlan;
2623 return NIX_AF_ERR_INVALID_NIXBLK;
2625 vlan = &nix_hw->txvlan;
2627 mutex_lock(&vlan->rsrc_lock);
2629 index = rvu_alloc_rsrc(&vlan->rsrc);
2631 mutex_unlock(&vlan->rsrc_lock);
2635 mutex_unlock(&vlan->rsrc_lock);
2637 regval = size ? vtag : vtag << 32;
2639 rvu_write64(rvu, blkaddr,
2640 NIX_AF_TX_VTAG_DEFX_DATA(index), regval);
2641 rvu_write64(rvu, blkaddr,
2642 NIX_AF_TX_VTAG_DEFX_CTL(index), size);
2647 static int nix_tx_vtag_decfg(struct rvu *rvu, int blkaddr,
2648 struct nix_vtag_config *req)
2650 struct nix_hw *nix_hw = get_nix_hw(rvu->hw, blkaddr);
2651 u16 pcifunc = req->hdr.pcifunc;
2652 int idx0 = req->tx.vtag0_idx;
2653 int idx1 = req->tx.vtag1_idx;
2654 struct nix_txvlan *vlan;
2658 return NIX_AF_ERR_INVALID_NIXBLK;
2660 vlan = &nix_hw->txvlan;
2661 if (req->tx.free_vtag0 && req->tx.free_vtag1)
2662 if (vlan->entry2pfvf_map[idx0] != pcifunc ||
2663 vlan->entry2pfvf_map[idx1] != pcifunc)
2664 return NIX_AF_ERR_PARAM;
2666 mutex_lock(&vlan->rsrc_lock);
2668 if (req->tx.free_vtag0) {
2669 err = nix_tx_vtag_free(rvu, blkaddr, pcifunc, idx0);
2674 if (req->tx.free_vtag1)
2675 err = nix_tx_vtag_free(rvu, blkaddr, pcifunc, idx1);
2678 mutex_unlock(&vlan->rsrc_lock);
2682 static int nix_tx_vtag_cfg(struct rvu *rvu, int blkaddr,
2683 struct nix_vtag_config *req,
2684 struct nix_vtag_config_rsp *rsp)
2686 struct nix_hw *nix_hw = get_nix_hw(rvu->hw, blkaddr);
2687 struct nix_txvlan *vlan;
2688 u16 pcifunc = req->hdr.pcifunc;
2691 return NIX_AF_ERR_INVALID_NIXBLK;
2693 vlan = &nix_hw->txvlan;
2694 if (req->tx.cfg_vtag0) {
2696 nix_tx_vtag_alloc(rvu, blkaddr,
2697 req->tx.vtag0, req->vtag_size);
2699 if (rsp->vtag0_idx < 0)
2700 return NIX_AF_ERR_TX_VTAG_NOSPC;
2702 vlan->entry2pfvf_map[rsp->vtag0_idx] = pcifunc;
2705 if (req->tx.cfg_vtag1) {
2707 nix_tx_vtag_alloc(rvu, blkaddr,
2708 req->tx.vtag1, req->vtag_size);
2710 if (rsp->vtag1_idx < 0)
2713 vlan->entry2pfvf_map[rsp->vtag1_idx] = pcifunc;
2719 if (req->tx.cfg_vtag0)
2720 nix_tx_vtag_free(rvu, blkaddr, pcifunc, rsp->vtag0_idx);
2722 return NIX_AF_ERR_TX_VTAG_NOSPC;
2725 int rvu_mbox_handler_nix_vtag_cfg(struct rvu *rvu,
2726 struct nix_vtag_config *req,
2727 struct nix_vtag_config_rsp *rsp)
2729 u16 pcifunc = req->hdr.pcifunc;
2730 int blkaddr, nixlf, err;
2732 err = nix_get_nixlf(rvu, pcifunc, &nixlf, &blkaddr);
2736 if (req->cfg_type) {
2737 /* rx vtag configuration */
2738 err = nix_rx_vtag_cfg(rvu, nixlf, blkaddr, req);
2740 return NIX_AF_ERR_PARAM;
2742 /* tx vtag configuration */
2743 if ((req->tx.cfg_vtag0 || req->tx.cfg_vtag1) &&
2744 (req->tx.free_vtag0 || req->tx.free_vtag1))
2745 return NIX_AF_ERR_PARAM;
2747 if (req->tx.cfg_vtag0 || req->tx.cfg_vtag1)
2748 return nix_tx_vtag_cfg(rvu, blkaddr, req, rsp);
2750 if (req->tx.free_vtag0 || req->tx.free_vtag1)
2751 return nix_tx_vtag_decfg(rvu, blkaddr, req);
2757 static int nix_blk_setup_mce(struct rvu *rvu, struct nix_hw *nix_hw,
2758 int mce, u8 op, u16 pcifunc, int next, bool eol)
2760 struct nix_aq_enq_req aq_req;
2763 aq_req.hdr.pcifunc = 0;
2764 aq_req.ctype = NIX_AQ_CTYPE_MCE;
2768 /* Use RSS with RSS index 0 */
2770 aq_req.mce.index = 0;
2771 aq_req.mce.eol = eol;
2772 aq_req.mce.pf_func = pcifunc;
2773 aq_req.mce.next = next;
2775 /* All fields valid */
2776 *(u64 *)(&aq_req.mce_mask) = ~0ULL;
2778 err = rvu_nix_blk_aq_enq_inst(rvu, nix_hw, &aq_req, NULL);
2780 dev_err(rvu->dev, "Failed to setup Bcast MCE for PF%d:VF%d\n",
2781 rvu_get_pf(pcifunc), pcifunc & RVU_PFVF_FUNC_MASK);
2787 static int nix_update_mce_list_entry(struct nix_mce_list *mce_list,
2788 u16 pcifunc, bool add)
2790 struct mce *mce, *tail = NULL;
2791 bool delete = false;
2793 /* Scan through the current list */
2794 hlist_for_each_entry(mce, &mce_list->head, node) {
2795 /* If already exists, then delete */
2796 if (mce->pcifunc == pcifunc && !add) {
2799 } else if (mce->pcifunc == pcifunc && add) {
2800 /* entry already exists */
2807 hlist_del(&mce->node);
2816 /* Add a new one to the list, at the tail */
2817 mce = kzalloc(sizeof(*mce), GFP_KERNEL);
2820 mce->pcifunc = pcifunc;
2822 hlist_add_head(&mce->node, &mce_list->head);
2824 hlist_add_behind(&mce->node, &tail->node);
2829 int nix_update_mce_list(struct rvu *rvu, u16 pcifunc,
2830 struct nix_mce_list *mce_list,
2831 int mce_idx, int mcam_index, bool add)
2833 int err = 0, idx, next_idx, last_idx, blkaddr, npc_blkaddr;
2834 struct npc_mcam *mcam = &rvu->hw->mcam;
2835 struct nix_mcast *mcast;
2836 struct nix_hw *nix_hw;
2842 /* Get this PF/VF func's MCE index */
2843 idx = mce_idx + (pcifunc & RVU_PFVF_FUNC_MASK);
2845 if (idx > (mce_idx + mce_list->max)) {
2847 "%s: Idx %d > max MCE idx %d, for PF%d bcast list\n",
2848 __func__, idx, mce_list->max,
2849 pcifunc >> RVU_PFVF_PF_SHIFT);
2853 err = nix_get_struct_ptrs(rvu, pcifunc, &nix_hw, &blkaddr);
2857 mcast = &nix_hw->mcast;
2858 mutex_lock(&mcast->mce_lock);
2860 err = nix_update_mce_list_entry(mce_list, pcifunc, add);
2864 /* Disable MCAM entry in NPC */
2865 if (!mce_list->count) {
2866 npc_blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPC, 0);
2867 npc_enable_mcam_entry(rvu, mcam, npc_blkaddr, mcam_index, false);
2871 /* Dump the updated list to HW */
2873 last_idx = idx + mce_list->count - 1;
2874 hlist_for_each_entry(mce, &mce_list->head, node) {
2879 /* EOL should be set in last MCE */
2880 err = nix_blk_setup_mce(rvu, nix_hw, idx, NIX_AQ_INSTOP_WRITE,
2881 mce->pcifunc, next_idx,
2882 (next_idx > last_idx) ? true : false);
2889 mutex_unlock(&mcast->mce_lock);
2893 void nix_get_mce_list(struct rvu *rvu, u16 pcifunc, int type,
2894 struct nix_mce_list **mce_list, int *mce_idx)
2896 struct rvu_hwinfo *hw = rvu->hw;
2897 struct rvu_pfvf *pfvf;
2899 if (!hw->cap.nix_rx_multicast ||
2900 !is_pf_cgxmapped(rvu, rvu_get_pf(pcifunc & ~RVU_PFVF_FUNC_MASK))) {
2906 /* Get this PF/VF func's MCE index */
2907 pfvf = rvu_get_pfvf(rvu, pcifunc & ~RVU_PFVF_FUNC_MASK);
2909 if (type == NIXLF_BCAST_ENTRY) {
2910 *mce_list = &pfvf->bcast_mce_list;
2911 *mce_idx = pfvf->bcast_mce_idx;
2912 } else if (type == NIXLF_ALLMULTI_ENTRY) {
2913 *mce_list = &pfvf->mcast_mce_list;
2914 *mce_idx = pfvf->mcast_mce_idx;
2915 } else if (type == NIXLF_PROMISC_ENTRY) {
2916 *mce_list = &pfvf->promisc_mce_list;
2917 *mce_idx = pfvf->promisc_mce_idx;
2924 static int nix_update_mce_rule(struct rvu *rvu, u16 pcifunc,
2927 int err = 0, nixlf, blkaddr, mcam_index, mce_idx;
2928 struct npc_mcam *mcam = &rvu->hw->mcam;
2929 struct rvu_hwinfo *hw = rvu->hw;
2930 struct nix_mce_list *mce_list;
2933 /* skip multicast pkt replication for AF's VFs & SDP links */
2934 if (is_afvf(pcifunc) || is_sdp_pfvf(pcifunc))
2937 if (!hw->cap.nix_rx_multicast)
2940 pf = rvu_get_pf(pcifunc);
2941 if (!is_pf_cgxmapped(rvu, pf))
2944 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
2948 nixlf = rvu_get_lf(rvu, &hw->block[blkaddr], pcifunc, 0);
2952 nix_get_mce_list(rvu, pcifunc, type, &mce_list, &mce_idx);
2954 mcam_index = npc_get_nixlf_mcam_index(mcam,
2955 pcifunc & ~RVU_PFVF_FUNC_MASK,
2957 err = nix_update_mce_list(rvu, pcifunc, mce_list,
2958 mce_idx, mcam_index, add);
2962 static int nix_setup_mce_tables(struct rvu *rvu, struct nix_hw *nix_hw)
2964 struct nix_mcast *mcast = &nix_hw->mcast;
2965 int err, pf, numvfs, idx;
2966 struct rvu_pfvf *pfvf;
2970 /* Skip PF0 (i.e AF) */
2971 for (pf = 1; pf < (rvu->cgx_mapped_pfs + 1); pf++) {
2972 cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_PFX_CFG(pf));
2973 /* If PF is not enabled, nothing to do */
2974 if (!((cfg >> 20) & 0x01))
2976 /* Get numVFs attached to this PF */
2977 numvfs = (cfg >> 12) & 0xFF;
2979 pfvf = &rvu->pf[pf];
2981 /* This NIX0/1 block mapped to PF ? */
2982 if (pfvf->nix_blkaddr != nix_hw->blkaddr)
2985 /* save start idx of broadcast mce list */
2986 pfvf->bcast_mce_idx = nix_alloc_mce_list(mcast, numvfs + 1);
2987 nix_mce_list_init(&pfvf->bcast_mce_list, numvfs + 1);
2989 /* save start idx of multicast mce list */
2990 pfvf->mcast_mce_idx = nix_alloc_mce_list(mcast, numvfs + 1);
2991 nix_mce_list_init(&pfvf->mcast_mce_list, numvfs + 1);
2993 /* save the start idx of promisc mce list */
2994 pfvf->promisc_mce_idx = nix_alloc_mce_list(mcast, numvfs + 1);
2995 nix_mce_list_init(&pfvf->promisc_mce_list, numvfs + 1);
2997 for (idx = 0; idx < (numvfs + 1); idx++) {
2998 /* idx-0 is for PF, followed by VFs */
2999 pcifunc = (pf << RVU_PFVF_PF_SHIFT);
3001 /* Add dummy entries now, so that we don't have to check
3002 * for whether AQ_OP should be INIT/WRITE later on.
3003 * Will be updated when a NIXLF is attached/detached to
3006 err = nix_blk_setup_mce(rvu, nix_hw,
3007 pfvf->bcast_mce_idx + idx,
3013 /* add dummy entries to multicast mce list */
3014 err = nix_blk_setup_mce(rvu, nix_hw,
3015 pfvf->mcast_mce_idx + idx,
3021 /* add dummy entries to promisc mce list */
3022 err = nix_blk_setup_mce(rvu, nix_hw,
3023 pfvf->promisc_mce_idx + idx,
3033 static int nix_setup_mcast(struct rvu *rvu, struct nix_hw *nix_hw, int blkaddr)
3035 struct nix_mcast *mcast = &nix_hw->mcast;
3036 struct rvu_hwinfo *hw = rvu->hw;
3039 size = (rvu_read64(rvu, blkaddr, NIX_AF_CONST3) >> 16) & 0x0F;
3040 size = (1ULL << size);
3042 /* Alloc memory for multicast/mirror replication entries */
3043 err = qmem_alloc(rvu->dev, &mcast->mce_ctx,
3044 (256UL << MC_TBL_SIZE), size);
3048 rvu_write64(rvu, blkaddr, NIX_AF_RX_MCAST_BASE,
3049 (u64)mcast->mce_ctx->iova);
3051 /* Set max list length equal to max no of VFs per PF + PF itself */
3052 rvu_write64(rvu, blkaddr, NIX_AF_RX_MCAST_CFG,
3053 BIT_ULL(36) | (hw->max_vfs_per_pf << 4) | MC_TBL_SIZE);
3055 /* Alloc memory for multicast replication buffers */
3056 size = rvu_read64(rvu, blkaddr, NIX_AF_MC_MIRROR_CONST) & 0xFFFF;
3057 err = qmem_alloc(rvu->dev, &mcast->mcast_buf,
3058 (8UL << MC_BUF_CNT), size);
3062 rvu_write64(rvu, blkaddr, NIX_AF_RX_MCAST_BUF_BASE,
3063 (u64)mcast->mcast_buf->iova);
3065 /* Alloc pkind for NIX internal RX multicast/mirror replay */
3066 mcast->replay_pkind = rvu_alloc_rsrc(&hw->pkind.rsrc);
3068 rvu_write64(rvu, blkaddr, NIX_AF_RX_MCAST_BUF_CFG,
3069 BIT_ULL(63) | (mcast->replay_pkind << 24) |
3070 BIT_ULL(20) | MC_BUF_CNT);
3072 mutex_init(&mcast->mce_lock);
3074 return nix_setup_mce_tables(rvu, nix_hw);
3077 static int nix_setup_txvlan(struct rvu *rvu, struct nix_hw *nix_hw)
3079 struct nix_txvlan *vlan = &nix_hw->txvlan;
3082 /* Allocate resource bimap for tx vtag def registers*/
3083 vlan->rsrc.max = NIX_TX_VTAG_DEF_MAX;
3084 err = rvu_alloc_bitmap(&vlan->rsrc);
3088 /* Alloc memory for saving entry to RVU PFFUNC allocation mapping */
3089 vlan->entry2pfvf_map = devm_kcalloc(rvu->dev, vlan->rsrc.max,
3090 sizeof(u16), GFP_KERNEL);
3091 if (!vlan->entry2pfvf_map)
3094 mutex_init(&vlan->rsrc_lock);
3098 kfree(vlan->rsrc.bmap);
3102 static int nix_setup_txschq(struct rvu *rvu, struct nix_hw *nix_hw, int blkaddr)
3104 struct nix_txsch *txsch;
3108 /* Get scheduler queue count of each type and alloc
3109 * bitmap for each for alloc/free/attach operations.
3111 for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
3112 txsch = &nix_hw->txsch[lvl];
3115 case NIX_TXSCH_LVL_SMQ:
3116 reg = NIX_AF_MDQ_CONST;
3118 case NIX_TXSCH_LVL_TL4:
3119 reg = NIX_AF_TL4_CONST;
3121 case NIX_TXSCH_LVL_TL3:
3122 reg = NIX_AF_TL3_CONST;
3124 case NIX_TXSCH_LVL_TL2:
3125 reg = NIX_AF_TL2_CONST;
3127 case NIX_TXSCH_LVL_TL1:
3128 reg = NIX_AF_TL1_CONST;
3131 cfg = rvu_read64(rvu, blkaddr, reg);
3132 txsch->schq.max = cfg & 0xFFFF;
3133 err = rvu_alloc_bitmap(&txsch->schq);
3137 /* Allocate memory for scheduler queues to
3138 * PF/VF pcifunc mapping info.
3140 txsch->pfvf_map = devm_kcalloc(rvu->dev, txsch->schq.max,
3141 sizeof(u32), GFP_KERNEL);
3142 if (!txsch->pfvf_map)
3144 for (schq = 0; schq < txsch->schq.max; schq++)
3145 txsch->pfvf_map[schq] = TXSCH_MAP(0, NIX_TXSCHQ_FREE);
3148 /* Setup a default value of 8192 as DWRR MTU */
3149 if (rvu->hw->cap.nix_common_dwrr_mtu) {
3150 rvu_write64(rvu, blkaddr, NIX_AF_DWRR_RPM_MTU,
3151 convert_bytes_to_dwrr_mtu(8192));
3152 rvu_write64(rvu, blkaddr, NIX_AF_DWRR_SDP_MTU,
3153 convert_bytes_to_dwrr_mtu(8192));
3159 int rvu_nix_reserve_mark_format(struct rvu *rvu, struct nix_hw *nix_hw,
3160 int blkaddr, u32 cfg)
3164 for (fmt_idx = 0; fmt_idx < nix_hw->mark_format.in_use; fmt_idx++) {
3165 if (nix_hw->mark_format.cfg[fmt_idx] == cfg)
3168 if (fmt_idx >= nix_hw->mark_format.total)
3171 rvu_write64(rvu, blkaddr, NIX_AF_MARK_FORMATX_CTL(fmt_idx), cfg);
3172 nix_hw->mark_format.cfg[fmt_idx] = cfg;
3173 nix_hw->mark_format.in_use++;
3177 static int nix_af_mark_format_setup(struct rvu *rvu, struct nix_hw *nix_hw,
3181 [NIX_MARK_CFG_IP_DSCP_RED] = 0x10003,
3182 [NIX_MARK_CFG_IP_DSCP_YELLOW] = 0x11200,
3183 [NIX_MARK_CFG_IP_DSCP_YELLOW_RED] = 0x11203,
3184 [NIX_MARK_CFG_IP_ECN_RED] = 0x6000c,
3185 [NIX_MARK_CFG_IP_ECN_YELLOW] = 0x60c00,
3186 [NIX_MARK_CFG_IP_ECN_YELLOW_RED] = 0x60c0c,
3187 [NIX_MARK_CFG_VLAN_DEI_RED] = 0x30008,
3188 [NIX_MARK_CFG_VLAN_DEI_YELLOW] = 0x30800,
3189 [NIX_MARK_CFG_VLAN_DEI_YELLOW_RED] = 0x30808,
3194 total = (rvu_read64(rvu, blkaddr, NIX_AF_PSE_CONST) & 0xFF00) >> 8;
3195 nix_hw->mark_format.total = (u8)total;
3196 nix_hw->mark_format.cfg = devm_kcalloc(rvu->dev, total, sizeof(u32),
3198 if (!nix_hw->mark_format.cfg)
3200 for (i = 0; i < NIX_MARK_CFG_MAX; i++) {
3201 rc = rvu_nix_reserve_mark_format(rvu, nix_hw, blkaddr, cfgs[i]);
3203 dev_err(rvu->dev, "Err %d in setup mark format %d\n",
3210 static void rvu_get_lbk_link_max_frs(struct rvu *rvu, u16 *max_mtu)
3212 /* CN10K supports LBK FIFO size 72 KB */
3213 if (rvu->hw->lbk_bufsize == 0x12000)
3214 *max_mtu = CN10K_LBK_LINK_MAX_FRS;
3216 *max_mtu = NIC_HW_MAX_FRS;
3219 static void rvu_get_lmac_link_max_frs(struct rvu *rvu, u16 *max_mtu)
3221 int fifo_size = rvu_cgx_get_fifolen(rvu);
3223 /* RPM supports FIFO len 128 KB and RPM2 supports double the
3224 * FIFO len to accommodate 8 LMACS
3226 if (fifo_size == 0x20000 || fifo_size == 0x40000)
3227 *max_mtu = CN10K_LMAC_LINK_MAX_FRS;
3229 *max_mtu = NIC_HW_MAX_FRS;
3232 int rvu_mbox_handler_nix_get_hw_info(struct rvu *rvu, struct msg_req *req,
3233 struct nix_hw_info *rsp)
3235 u16 pcifunc = req->hdr.pcifunc;
3239 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
3241 return NIX_AF_ERR_AF_LF_INVALID;
3243 if (is_afvf(pcifunc))
3244 rvu_get_lbk_link_max_frs(rvu, &rsp->max_mtu);
3246 rvu_get_lmac_link_max_frs(rvu, &rsp->max_mtu);
3248 rsp->min_mtu = NIC_HW_MIN_FRS;
3250 if (!rvu->hw->cap.nix_common_dwrr_mtu) {
3251 /* Return '1' on OTx2 */
3252 rsp->rpm_dwrr_mtu = 1;
3253 rsp->sdp_dwrr_mtu = 1;
3257 dwrr_mtu = rvu_read64(rvu, BLKADDR_NIX0, NIX_AF_DWRR_RPM_MTU);
3258 rsp->rpm_dwrr_mtu = convert_dwrr_mtu_to_bytes(dwrr_mtu);
3260 dwrr_mtu = rvu_read64(rvu, BLKADDR_NIX0, NIX_AF_DWRR_SDP_MTU);
3261 rsp->sdp_dwrr_mtu = convert_dwrr_mtu_to_bytes(dwrr_mtu);
3266 int rvu_mbox_handler_nix_stats_rst(struct rvu *rvu, struct msg_req *req,
3267 struct msg_rsp *rsp)
3269 u16 pcifunc = req->hdr.pcifunc;
3270 int i, nixlf, blkaddr, err;
3273 err = nix_get_nixlf(rvu, pcifunc, &nixlf, &blkaddr);
3277 /* Get stats count supported by HW */
3278 stats = rvu_read64(rvu, blkaddr, NIX_AF_CONST1);
3280 /* Reset tx stats */
3281 for (i = 0; i < ((stats >> 24) & 0xFF); i++)
3282 rvu_write64(rvu, blkaddr, NIX_AF_LFX_TX_STATX(nixlf, i), 0);
3284 /* Reset rx stats */
3285 for (i = 0; i < ((stats >> 32) & 0xFF); i++)
3286 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RX_STATX(nixlf, i), 0);
3291 /* Returns the ALG index to be set into NPC_RX_ACTION */
3292 static int get_flowkey_alg_idx(struct nix_hw *nix_hw, u32 flow_cfg)
3296 /* Scan over exiting algo entries to find a match */
3297 for (i = 0; i < nix_hw->flowkey.in_use; i++)
3298 if (nix_hw->flowkey.flowkey[i] == flow_cfg)
3304 static int set_flowkey_fields(struct nix_rx_flowkey_alg *alg, u32 flow_cfg)
3306 int idx, nr_field, key_off, field_marker, keyoff_marker;
3307 int max_key_off, max_bit_pos, group_member;
3308 struct nix_rx_flowkey_alg *field;
3309 struct nix_rx_flowkey_alg tmp;
3310 u32 key_type, valid_key;
3311 int l4_key_offset = 0;
3316 #define FIELDS_PER_ALG 5
3317 #define MAX_KEY_OFF 40
3318 /* Clear all fields */
3319 memset(alg, 0, sizeof(uint64_t) * FIELDS_PER_ALG);
3321 /* Each of the 32 possible flow key algorithm definitions should
3322 * fall into above incremental config (except ALG0). Otherwise a
3323 * single NPC MCAM entry is not sufficient for supporting RSS.
3325 * If a different definition or combination needed then NPC MCAM
3326 * has to be programmed to filter such pkts and it's action should
3327 * point to this definition to calculate flowtag or hash.
3329 * The `for loop` goes over _all_ protocol field and the following
3330 * variables depicts the state machine forward progress logic.
3332 * keyoff_marker - Enabled when hash byte length needs to be accounted
3333 * in field->key_offset update.
3334 * field_marker - Enabled when a new field needs to be selected.
3335 * group_member - Enabled when protocol is part of a group.
3338 keyoff_marker = 0; max_key_off = 0; group_member = 0;
3339 nr_field = 0; key_off = 0; field_marker = 1;
3340 field = &tmp; max_bit_pos = fls(flow_cfg);
3342 idx < max_bit_pos && nr_field < FIELDS_PER_ALG &&
3343 key_off < MAX_KEY_OFF; idx++) {
3344 key_type = BIT(idx);
3345 valid_key = flow_cfg & key_type;
3346 /* Found a field marker, reset the field values */
3348 memset(&tmp, 0, sizeof(tmp));
3350 field_marker = true;
3351 keyoff_marker = true;
3353 case NIX_FLOW_KEY_TYPE_PORT:
3354 field->sel_chan = true;
3355 /* This should be set to 1, when SEL_CHAN is set */
3358 case NIX_FLOW_KEY_TYPE_IPV4_PROTO:
3359 field->lid = NPC_LID_LC;
3360 field->hdr_offset = 9; /* offset */
3361 field->bytesm1 = 0; /* 1 byte */
3362 field->ltype_match = NPC_LT_LC_IP;
3363 field->ltype_mask = 0xF;
3365 case NIX_FLOW_KEY_TYPE_IPV4:
3366 case NIX_FLOW_KEY_TYPE_INNR_IPV4:
3367 field->lid = NPC_LID_LC;
3368 field->ltype_match = NPC_LT_LC_IP;
3369 if (key_type == NIX_FLOW_KEY_TYPE_INNR_IPV4) {
3370 field->lid = NPC_LID_LG;
3371 field->ltype_match = NPC_LT_LG_TU_IP;
3373 field->hdr_offset = 12; /* SIP offset */
3374 field->bytesm1 = 7; /* SIP + DIP, 8 bytes */
3375 field->ltype_mask = 0xF; /* Match only IPv4 */
3376 keyoff_marker = false;
3378 case NIX_FLOW_KEY_TYPE_IPV6:
3379 case NIX_FLOW_KEY_TYPE_INNR_IPV6:
3380 field->lid = NPC_LID_LC;
3381 field->ltype_match = NPC_LT_LC_IP6;
3382 if (key_type == NIX_FLOW_KEY_TYPE_INNR_IPV6) {
3383 field->lid = NPC_LID_LG;
3384 field->ltype_match = NPC_LT_LG_TU_IP6;
3386 field->hdr_offset = 8; /* SIP offset */
3387 field->bytesm1 = 31; /* SIP + DIP, 32 bytes */
3388 field->ltype_mask = 0xF; /* Match only IPv6 */
3390 case NIX_FLOW_KEY_TYPE_TCP:
3391 case NIX_FLOW_KEY_TYPE_UDP:
3392 case NIX_FLOW_KEY_TYPE_SCTP:
3393 case NIX_FLOW_KEY_TYPE_INNR_TCP:
3394 case NIX_FLOW_KEY_TYPE_INNR_UDP:
3395 case NIX_FLOW_KEY_TYPE_INNR_SCTP:
3396 field->lid = NPC_LID_LD;
3397 if (key_type == NIX_FLOW_KEY_TYPE_INNR_TCP ||
3398 key_type == NIX_FLOW_KEY_TYPE_INNR_UDP ||
3399 key_type == NIX_FLOW_KEY_TYPE_INNR_SCTP)
3400 field->lid = NPC_LID_LH;
3401 field->bytesm1 = 3; /* Sport + Dport, 4 bytes */
3403 /* Enum values for NPC_LID_LD and NPC_LID_LG are same,
3404 * so no need to change the ltype_match, just change
3405 * the lid for inner protocols
3407 BUILD_BUG_ON((int)NPC_LT_LD_TCP !=
3408 (int)NPC_LT_LH_TU_TCP);
3409 BUILD_BUG_ON((int)NPC_LT_LD_UDP !=
3410 (int)NPC_LT_LH_TU_UDP);
3411 BUILD_BUG_ON((int)NPC_LT_LD_SCTP !=
3412 (int)NPC_LT_LH_TU_SCTP);
3414 if ((key_type == NIX_FLOW_KEY_TYPE_TCP ||
3415 key_type == NIX_FLOW_KEY_TYPE_INNR_TCP) &&
3417 field->ltype_match |= NPC_LT_LD_TCP;
3418 group_member = true;
3419 } else if ((key_type == NIX_FLOW_KEY_TYPE_UDP ||
3420 key_type == NIX_FLOW_KEY_TYPE_INNR_UDP) &&
3422 field->ltype_match |= NPC_LT_LD_UDP;
3423 group_member = true;
3424 } else if ((key_type == NIX_FLOW_KEY_TYPE_SCTP ||
3425 key_type == NIX_FLOW_KEY_TYPE_INNR_SCTP) &&
3427 field->ltype_match |= NPC_LT_LD_SCTP;
3428 group_member = true;
3430 field->ltype_mask = ~field->ltype_match;
3431 if (key_type == NIX_FLOW_KEY_TYPE_SCTP ||
3432 key_type == NIX_FLOW_KEY_TYPE_INNR_SCTP) {
3433 /* Handle the case where any of the group item
3434 * is enabled in the group but not the final one
3438 group_member = false;
3441 field_marker = false;
3442 keyoff_marker = false;
3445 /* TCP/UDP/SCTP and ESP/AH falls at same offset so
3446 * remember the TCP key offset of 40 byte hash key.
3448 if (key_type == NIX_FLOW_KEY_TYPE_TCP)
3449 l4_key_offset = key_off;
3451 case NIX_FLOW_KEY_TYPE_NVGRE:
3452 field->lid = NPC_LID_LD;
3453 field->hdr_offset = 4; /* VSID offset */
3455 field->ltype_match = NPC_LT_LD_NVGRE;
3456 field->ltype_mask = 0xF;
3458 case NIX_FLOW_KEY_TYPE_VXLAN:
3459 case NIX_FLOW_KEY_TYPE_GENEVE:
3460 field->lid = NPC_LID_LE;
3462 field->hdr_offset = 4;
3463 field->ltype_mask = 0xF;
3464 field_marker = false;
3465 keyoff_marker = false;
3467 if (key_type == NIX_FLOW_KEY_TYPE_VXLAN && valid_key) {
3468 field->ltype_match |= NPC_LT_LE_VXLAN;
3469 group_member = true;
3472 if (key_type == NIX_FLOW_KEY_TYPE_GENEVE && valid_key) {
3473 field->ltype_match |= NPC_LT_LE_GENEVE;
3474 group_member = true;
3477 if (key_type == NIX_FLOW_KEY_TYPE_GENEVE) {
3479 field->ltype_mask = ~field->ltype_match;
3480 field_marker = true;
3481 keyoff_marker = true;
3483 group_member = false;
3487 case NIX_FLOW_KEY_TYPE_ETH_DMAC:
3488 case NIX_FLOW_KEY_TYPE_INNR_ETH_DMAC:
3489 field->lid = NPC_LID_LA;
3490 field->ltype_match = NPC_LT_LA_ETHER;
3491 if (key_type == NIX_FLOW_KEY_TYPE_INNR_ETH_DMAC) {
3492 field->lid = NPC_LID_LF;
3493 field->ltype_match = NPC_LT_LF_TU_ETHER;
3495 field->hdr_offset = 0;
3496 field->bytesm1 = 5; /* DMAC 6 Byte */
3497 field->ltype_mask = 0xF;
3499 case NIX_FLOW_KEY_TYPE_IPV6_EXT:
3500 field->lid = NPC_LID_LC;
3501 field->hdr_offset = 40; /* IPV6 hdr */
3502 field->bytesm1 = 0; /* 1 Byte ext hdr*/
3503 field->ltype_match = NPC_LT_LC_IP6_EXT;
3504 field->ltype_mask = 0xF;
3506 case NIX_FLOW_KEY_TYPE_GTPU:
3507 field->lid = NPC_LID_LE;
3508 field->hdr_offset = 4;
3509 field->bytesm1 = 3; /* 4 bytes TID*/
3510 field->ltype_match = NPC_LT_LE_GTPU;
3511 field->ltype_mask = 0xF;
3513 case NIX_FLOW_KEY_TYPE_VLAN:
3514 field->lid = NPC_LID_LB;
3515 field->hdr_offset = 2; /* Skip TPID (2-bytes) */
3516 field->bytesm1 = 1; /* 2 Bytes (Actually 12 bits) */
3517 field->ltype_match = NPC_LT_LB_CTAG;
3518 field->ltype_mask = 0xF;
3519 field->fn_mask = 1; /* Mask out the first nibble */
3521 case NIX_FLOW_KEY_TYPE_AH:
3522 case NIX_FLOW_KEY_TYPE_ESP:
3523 field->hdr_offset = 0;
3524 field->bytesm1 = 7; /* SPI + sequence number */
3525 field->ltype_mask = 0xF;
3526 field->lid = NPC_LID_LE;
3527 field->ltype_match = NPC_LT_LE_ESP;
3528 if (key_type == NIX_FLOW_KEY_TYPE_AH) {
3529 field->lid = NPC_LID_LD;
3530 field->ltype_match = NPC_LT_LD_AH;
3531 field->hdr_offset = 4;
3532 keyoff_marker = false;
3538 /* Found a valid flow key type */
3540 /* Use the key offset of TCP/UDP/SCTP fields
3541 * for ESP/AH fields.
3543 if (key_type == NIX_FLOW_KEY_TYPE_ESP ||
3544 key_type == NIX_FLOW_KEY_TYPE_AH)
3545 key_off = l4_key_offset;
3546 field->key_offset = key_off;
3547 memcpy(&alg[nr_field], field, sizeof(*field));
3548 max_key_off = max(max_key_off, field->bytesm1 + 1);
3550 /* Found a field marker, get the next field */
3555 /* Found a keyoff marker, update the new key_off */
3556 if (keyoff_marker) {
3557 key_off += max_key_off;
3561 /* Processed all the flow key types */
3562 if (idx == max_bit_pos && key_off <= MAX_KEY_OFF)
3565 return NIX_AF_ERR_RSS_NOSPC_FIELD;
3568 static int reserve_flowkey_alg_idx(struct rvu *rvu, int blkaddr, u32 flow_cfg)
3570 u64 field[FIELDS_PER_ALG];
3574 hw = get_nix_hw(rvu->hw, blkaddr);
3576 return NIX_AF_ERR_INVALID_NIXBLK;
3578 /* No room to add new flow hash algoritham */
3579 if (hw->flowkey.in_use >= NIX_FLOW_KEY_ALG_MAX)
3580 return NIX_AF_ERR_RSS_NOSPC_ALGO;
3582 /* Generate algo fields for the given flow_cfg */
3583 rc = set_flowkey_fields((struct nix_rx_flowkey_alg *)field, flow_cfg);
3587 /* Update ALGX_FIELDX register with generated fields */
3588 for (fid = 0; fid < FIELDS_PER_ALG; fid++)
3589 rvu_write64(rvu, blkaddr,
3590 NIX_AF_RX_FLOW_KEY_ALGX_FIELDX(hw->flowkey.in_use,
3593 /* Store the flow_cfg for futher lookup */
3594 rc = hw->flowkey.in_use;
3595 hw->flowkey.flowkey[rc] = flow_cfg;
3596 hw->flowkey.in_use++;
3601 int rvu_mbox_handler_nix_rss_flowkey_cfg(struct rvu *rvu,
3602 struct nix_rss_flowkey_cfg *req,
3603 struct nix_rss_flowkey_cfg_rsp *rsp)
3605 u16 pcifunc = req->hdr.pcifunc;
3606 int alg_idx, nixlf, blkaddr;
3607 struct nix_hw *nix_hw;
3610 err = nix_get_nixlf(rvu, pcifunc, &nixlf, &blkaddr);
3614 nix_hw = get_nix_hw(rvu->hw, blkaddr);
3616 return NIX_AF_ERR_INVALID_NIXBLK;
3618 alg_idx = get_flowkey_alg_idx(nix_hw, req->flowkey_cfg);
3619 /* Failed to get algo index from the exiting list, reserve new */
3621 alg_idx = reserve_flowkey_alg_idx(rvu, blkaddr,
3626 rsp->alg_idx = alg_idx;
3627 rvu_npc_update_flowkey_alg_idx(rvu, pcifunc, nixlf, req->group,
3628 alg_idx, req->mcam_index);
3632 static int nix_rx_flowkey_alg_cfg(struct rvu *rvu, int blkaddr)
3634 u32 flowkey_cfg, minkey_cfg;
3637 /* Disable all flow key algx fieldx */
3638 for (alg = 0; alg < NIX_FLOW_KEY_ALG_MAX; alg++) {
3639 for (fid = 0; fid < FIELDS_PER_ALG; fid++)
3640 rvu_write64(rvu, blkaddr,
3641 NIX_AF_RX_FLOW_KEY_ALGX_FIELDX(alg, fid),
3645 /* IPv4/IPv6 SIP/DIPs */
3646 flowkey_cfg = NIX_FLOW_KEY_TYPE_IPV4 | NIX_FLOW_KEY_TYPE_IPV6;
3647 rc = reserve_flowkey_alg_idx(rvu, blkaddr, flowkey_cfg);
3651 /* TCPv4/v6 4-tuple, SIP, DIP, Sport, Dport */
3652 minkey_cfg = flowkey_cfg;
3653 flowkey_cfg = minkey_cfg | NIX_FLOW_KEY_TYPE_TCP;
3654 rc = reserve_flowkey_alg_idx(rvu, blkaddr, flowkey_cfg);
3658 /* UDPv4/v6 4-tuple, SIP, DIP, Sport, Dport */
3659 flowkey_cfg = minkey_cfg | NIX_FLOW_KEY_TYPE_UDP;
3660 rc = reserve_flowkey_alg_idx(rvu, blkaddr, flowkey_cfg);
3664 /* SCTPv4/v6 4-tuple, SIP, DIP, Sport, Dport */
3665 flowkey_cfg = minkey_cfg | NIX_FLOW_KEY_TYPE_SCTP;
3666 rc = reserve_flowkey_alg_idx(rvu, blkaddr, flowkey_cfg);
3670 /* TCP/UDP v4/v6 4-tuple, rest IP pkts 2-tuple */
3671 flowkey_cfg = minkey_cfg | NIX_FLOW_KEY_TYPE_TCP |
3672 NIX_FLOW_KEY_TYPE_UDP;
3673 rc = reserve_flowkey_alg_idx(rvu, blkaddr, flowkey_cfg);
3677 /* TCP/SCTP v4/v6 4-tuple, rest IP pkts 2-tuple */
3678 flowkey_cfg = minkey_cfg | NIX_FLOW_KEY_TYPE_TCP |
3679 NIX_FLOW_KEY_TYPE_SCTP;
3680 rc = reserve_flowkey_alg_idx(rvu, blkaddr, flowkey_cfg);
3684 /* UDP/SCTP v4/v6 4-tuple, rest IP pkts 2-tuple */
3685 flowkey_cfg = minkey_cfg | NIX_FLOW_KEY_TYPE_UDP |
3686 NIX_FLOW_KEY_TYPE_SCTP;
3687 rc = reserve_flowkey_alg_idx(rvu, blkaddr, flowkey_cfg);
3691 /* TCP/UDP/SCTP v4/v6 4-tuple, rest IP pkts 2-tuple */
3692 flowkey_cfg = minkey_cfg | NIX_FLOW_KEY_TYPE_TCP |
3693 NIX_FLOW_KEY_TYPE_UDP | NIX_FLOW_KEY_TYPE_SCTP;
3694 rc = reserve_flowkey_alg_idx(rvu, blkaddr, flowkey_cfg);
3701 int rvu_mbox_handler_nix_set_mac_addr(struct rvu *rvu,
3702 struct nix_set_mac_addr *req,
3703 struct msg_rsp *rsp)
3705 bool from_vf = req->hdr.pcifunc & RVU_PFVF_FUNC_MASK;
3706 u16 pcifunc = req->hdr.pcifunc;
3707 int blkaddr, nixlf, err;
3708 struct rvu_pfvf *pfvf;
3710 err = nix_get_nixlf(rvu, pcifunc, &nixlf, &blkaddr);
3714 pfvf = rvu_get_pfvf(rvu, pcifunc);
3716 /* untrusted VF can't overwrite admin(PF) changes */
3717 if (!test_bit(PF_SET_VF_TRUSTED, &pfvf->flags) &&
3718 (from_vf && test_bit(PF_SET_VF_MAC, &pfvf->flags))) {
3720 "MAC address set by admin(PF) cannot be overwritten by untrusted VF");
3724 ether_addr_copy(pfvf->mac_addr, req->mac_addr);
3726 rvu_npc_install_ucast_entry(rvu, pcifunc, nixlf,
3727 pfvf->rx_chan_base, req->mac_addr);
3729 if (test_bit(PF_SET_VF_TRUSTED, &pfvf->flags) && from_vf)
3730 ether_addr_copy(pfvf->default_mac, req->mac_addr);
3732 rvu_switch_update_rules(rvu, pcifunc);
3737 int rvu_mbox_handler_nix_get_mac_addr(struct rvu *rvu,
3738 struct msg_req *req,
3739 struct nix_get_mac_addr_rsp *rsp)
3741 u16 pcifunc = req->hdr.pcifunc;
3742 struct rvu_pfvf *pfvf;
3744 if (!is_nixlf_attached(rvu, pcifunc))
3745 return NIX_AF_ERR_AF_LF_INVALID;
3747 pfvf = rvu_get_pfvf(rvu, pcifunc);
3749 ether_addr_copy(rsp->mac_addr, pfvf->mac_addr);
3754 int rvu_mbox_handler_nix_set_rx_mode(struct rvu *rvu, struct nix_rx_mode *req,
3755 struct msg_rsp *rsp)
3757 bool allmulti, promisc, nix_rx_multicast;
3758 u16 pcifunc = req->hdr.pcifunc;
3759 struct rvu_pfvf *pfvf;
3762 pfvf = rvu_get_pfvf(rvu, pcifunc);
3763 promisc = req->mode & NIX_RX_MODE_PROMISC ? true : false;
3764 allmulti = req->mode & NIX_RX_MODE_ALLMULTI ? true : false;
3765 pfvf->use_mce_list = req->mode & NIX_RX_MODE_USE_MCE ? true : false;
3767 nix_rx_multicast = rvu->hw->cap.nix_rx_multicast & pfvf->use_mce_list;
3769 if (is_vf(pcifunc) && !nix_rx_multicast &&
3770 (promisc || allmulti)) {
3771 dev_warn_ratelimited(rvu->dev,
3772 "VF promisc/multicast not supported\n");
3776 /* untrusted VF can't configure promisc/allmulti */
3777 if (is_vf(pcifunc) && !test_bit(PF_SET_VF_TRUSTED, &pfvf->flags) &&
3778 (promisc || allmulti))
3781 err = nix_get_nixlf(rvu, pcifunc, &nixlf, NULL);
3785 if (nix_rx_multicast) {
3786 /* add/del this PF_FUNC to/from mcast pkt replication list */
3787 err = nix_update_mce_rule(rvu, pcifunc, NIXLF_ALLMULTI_ENTRY,
3791 "Failed to update pcifunc 0x%x to multicast list\n",
3796 /* add/del this PF_FUNC to/from promisc pkt replication list */
3797 err = nix_update_mce_rule(rvu, pcifunc, NIXLF_PROMISC_ENTRY,
3801 "Failed to update pcifunc 0x%x to promisc list\n",
3807 /* install/uninstall allmulti entry */
3809 rvu_npc_install_allmulti_entry(rvu, pcifunc, nixlf,
3810 pfvf->rx_chan_base);
3812 if (!nix_rx_multicast)
3813 rvu_npc_enable_allmulti_entry(rvu, pcifunc, nixlf, false);
3816 /* install/uninstall promisc entry */
3818 rvu_npc_install_promisc_entry(rvu, pcifunc, nixlf,
3822 if (rvu_npc_exact_has_match_table(rvu))
3823 rvu_npc_exact_promisc_enable(rvu, pcifunc);
3825 if (!nix_rx_multicast)
3826 rvu_npc_enable_promisc_entry(rvu, pcifunc, nixlf, false);
3828 if (rvu_npc_exact_has_match_table(rvu))
3829 rvu_npc_exact_promisc_disable(rvu, pcifunc);
3835 static void nix_find_link_frs(struct rvu *rvu,
3836 struct nix_frs_cfg *req, u16 pcifunc)
3838 int pf = rvu_get_pf(pcifunc);
3839 struct rvu_pfvf *pfvf;
3844 /* Update with requester's min/max lengths */
3845 pfvf = rvu_get_pfvf(rvu, pcifunc);
3846 pfvf->maxlen = req->maxlen;
3847 if (req->update_minlen)
3848 pfvf->minlen = req->minlen;
3850 maxlen = req->maxlen;
3851 minlen = req->update_minlen ? req->minlen : 0;
3853 /* Get this PF's numVFs and starting hwvf */
3854 rvu_get_pf_numvfs(rvu, pf, &numvfs, &hwvf);
3856 /* For each VF, compare requested max/minlen */
3857 for (vf = 0; vf < numvfs; vf++) {
3858 pfvf = &rvu->hwvf[hwvf + vf];
3859 if (pfvf->maxlen > maxlen)
3860 maxlen = pfvf->maxlen;
3861 if (req->update_minlen &&
3862 pfvf->minlen && pfvf->minlen < minlen)
3863 minlen = pfvf->minlen;
3866 /* Compare requested max/minlen with PF's max/minlen */
3867 pfvf = &rvu->pf[pf];
3868 if (pfvf->maxlen > maxlen)
3869 maxlen = pfvf->maxlen;
3870 if (req->update_minlen &&
3871 pfvf->minlen && pfvf->minlen < minlen)
3872 minlen = pfvf->minlen;
3874 /* Update the request with max/min PF's and it's VF's max/min */
3875 req->maxlen = maxlen;
3876 if (req->update_minlen)
3877 req->minlen = minlen;
3881 nix_config_link_credits(struct rvu *rvu, int blkaddr, int link,
3882 u16 pcifunc, u64 tx_credits)
3884 struct rvu_hwinfo *hw = rvu->hw;
3885 int pf = rvu_get_pf(pcifunc);
3886 u8 cgx_id = 0, lmac_id = 0;
3887 unsigned long poll_tmo;
3888 bool restore_tx_en = 0;
3889 struct nix_hw *nix_hw;
3890 u64 cfg, sw_xoff = 0;
3895 nix_hw = get_nix_hw(rvu->hw, blkaddr);
3897 return NIX_AF_ERR_INVALID_NIXBLK;
3899 if (tx_credits == nix_hw->tx_credits[link])
3902 /* Enable cgx tx if disabled for credits to be back */
3903 if (is_pf_cgxmapped(rvu, pf)) {
3904 rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
3905 restore_tx_en = !rvu_cgx_config_tx(rvu_cgx_pdata(cgx_id, rvu),
3909 mutex_lock(&rvu->rsrc_lock);
3910 /* Disable new traffic to link */
3911 if (hw->cap.nix_shaping) {
3912 schq = nix_get_tx_link(rvu, pcifunc);
3913 sw_xoff = rvu_read64(rvu, blkaddr, NIX_AF_TL1X_SW_XOFF(schq));
3914 rvu_write64(rvu, blkaddr,
3915 NIX_AF_TL1X_SW_XOFF(schq), BIT_ULL(0));
3918 rc = NIX_AF_ERR_LINK_CREDITS;
3919 poll_tmo = jiffies + usecs_to_jiffies(200000);
3920 /* Wait for credits to return */
3922 if (time_after(jiffies, poll_tmo))
3924 usleep_range(100, 200);
3926 cfg = rvu_read64(rvu, blkaddr,
3927 NIX_AF_TX_LINKX_NORM_CREDIT(link));
3928 credits = (cfg >> 12) & 0xFFFFFULL;
3929 } while (credits != nix_hw->tx_credits[link]);
3931 cfg &= ~(0xFFFFFULL << 12);
3932 cfg |= (tx_credits << 12);
3933 rvu_write64(rvu, blkaddr, NIX_AF_TX_LINKX_NORM_CREDIT(link), cfg);
3936 nix_hw->tx_credits[link] = tx_credits;
3939 /* Enable traffic back */
3940 if (hw->cap.nix_shaping && !sw_xoff)
3941 rvu_write64(rvu, blkaddr, NIX_AF_TL1X_SW_XOFF(schq), 0);
3943 /* Restore state of cgx tx */
3945 rvu_cgx_config_tx(rvu_cgx_pdata(cgx_id, rvu), lmac_id, false);
3947 mutex_unlock(&rvu->rsrc_lock);
3951 int rvu_mbox_handler_nix_set_hw_frs(struct rvu *rvu, struct nix_frs_cfg *req,
3952 struct msg_rsp *rsp)
3954 struct rvu_hwinfo *hw = rvu->hw;
3955 u16 pcifunc = req->hdr.pcifunc;
3956 int pf = rvu_get_pf(pcifunc);
3957 int blkaddr, schq, link = -1;
3958 struct nix_txsch *txsch;
3959 u64 cfg, lmac_fifo_len;
3960 struct nix_hw *nix_hw;
3961 struct rvu_pfvf *pfvf;
3962 u8 cgx = 0, lmac = 0;
3965 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
3967 return NIX_AF_ERR_AF_LF_INVALID;
3969 nix_hw = get_nix_hw(rvu->hw, blkaddr);
3971 return NIX_AF_ERR_INVALID_NIXBLK;
3973 if (is_afvf(pcifunc))
3974 rvu_get_lbk_link_max_frs(rvu, &max_mtu);
3976 rvu_get_lmac_link_max_frs(rvu, &max_mtu);
3978 if (!req->sdp_link && req->maxlen > max_mtu)
3979 return NIX_AF_ERR_FRS_INVALID;
3981 if (req->update_minlen && req->minlen < NIC_HW_MIN_FRS)
3982 return NIX_AF_ERR_FRS_INVALID;
3984 /* Check if requester wants to update SMQ's */
3985 if (!req->update_smq)
3988 /* Update min/maxlen in each of the SMQ attached to this PF/VF */
3989 txsch = &nix_hw->txsch[NIX_TXSCH_LVL_SMQ];
3990 mutex_lock(&rvu->rsrc_lock);
3991 for (schq = 0; schq < txsch->schq.max; schq++) {
3992 if (TXSCH_MAP_FUNC(txsch->pfvf_map[schq]) != pcifunc)
3994 cfg = rvu_read64(rvu, blkaddr, NIX_AF_SMQX_CFG(schq));
3995 cfg = (cfg & ~(0xFFFFULL << 8)) | ((u64)req->maxlen << 8);
3996 if (req->update_minlen)
3997 cfg = (cfg & ~0x7FULL) | ((u64)req->minlen & 0x7F);
3998 rvu_write64(rvu, blkaddr, NIX_AF_SMQX_CFG(schq), cfg);
4000 mutex_unlock(&rvu->rsrc_lock);
4003 /* Check if config is for SDP link */
4004 if (req->sdp_link) {
4006 return NIX_AF_ERR_RX_LINK_INVALID;
4007 link = hw->cgx_links + hw->lbk_links;
4011 /* Check if the request is from CGX mapped RVU PF */
4012 if (is_pf_cgxmapped(rvu, pf)) {
4013 /* Get CGX and LMAC to which this PF is mapped and find link */
4014 rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx, &lmac);
4015 link = (cgx * hw->lmac_per_cgx) + lmac;
4016 } else if (pf == 0) {
4017 /* For VFs of PF0 ingress is LBK port, so config LBK link */
4018 pfvf = rvu_get_pfvf(rvu, pcifunc);
4019 link = hw->cgx_links + pfvf->lbkid;
4023 return NIX_AF_ERR_RX_LINK_INVALID;
4025 nix_find_link_frs(rvu, req, pcifunc);
4028 cfg = rvu_read64(rvu, blkaddr, NIX_AF_RX_LINKX_CFG(link));
4029 cfg = (cfg & ~(0xFFFFULL << 16)) | ((u64)req->maxlen << 16);
4030 if (req->update_minlen)
4031 cfg = (cfg & ~0xFFFFULL) | req->minlen;
4032 rvu_write64(rvu, blkaddr, NIX_AF_RX_LINKX_CFG(link), cfg);
4034 if (req->sdp_link || pf == 0)
4037 /* Update transmit credits for CGX links */
4038 lmac_fifo_len = rvu_cgx_get_lmac_fifolen(rvu, cgx, lmac);
4039 if (!lmac_fifo_len) {
4041 "%s: Failed to get CGX/RPM%d:LMAC%d FIFO size\n",
4042 __func__, cgx, lmac);
4045 return nix_config_link_credits(rvu, blkaddr, link, pcifunc,
4046 (lmac_fifo_len - req->maxlen) / 16);
4049 int rvu_mbox_handler_nix_set_rx_cfg(struct rvu *rvu, struct nix_rx_cfg *req,
4050 struct msg_rsp *rsp)
4052 int nixlf, blkaddr, err;
4055 err = nix_get_nixlf(rvu, req->hdr.pcifunc, &nixlf, &blkaddr);
4059 cfg = rvu_read64(rvu, blkaddr, NIX_AF_LFX_RX_CFG(nixlf));
4060 /* Set the interface configuration */
4061 if (req->len_verify & BIT(0))
4064 cfg &= ~BIT_ULL(41);
4066 if (req->len_verify & BIT(1))
4069 cfg &= ~BIT_ULL(40);
4071 if (req->csum_verify & BIT(0))
4074 cfg &= ~BIT_ULL(37);
4076 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RX_CFG(nixlf), cfg);
4081 static u64 rvu_get_lbk_link_credits(struct rvu *rvu, u16 lbk_max_frs)
4083 /* CN10k supports 72KB FIFO size and max packet size of 64k */
4084 if (rvu->hw->lbk_bufsize == 0x12000)
4085 return (rvu->hw->lbk_bufsize - lbk_max_frs) / 16;
4087 return 1600; /* 16 * max LBK datarate = 16 * 100Gbps */
4090 static void nix_link_config(struct rvu *rvu, int blkaddr,
4091 struct nix_hw *nix_hw)
4093 struct rvu_hwinfo *hw = rvu->hw;
4094 int cgx, lmac_cnt, slink, link;
4095 u16 lbk_max_frs, lmac_max_frs;
4096 unsigned long lmac_bmap;
4097 u64 tx_credits, cfg;
4101 rvu_get_lbk_link_max_frs(rvu, &lbk_max_frs);
4102 rvu_get_lmac_link_max_frs(rvu, &lmac_max_frs);
4104 /* Set default min/max packet lengths allowed on NIX Rx links.
4106 * With HW reset minlen value of 60byte, HW will treat ARP pkts
4107 * as undersize and report them to SW as error pkts, hence
4108 * setting it to 40 bytes.
4110 for (link = 0; link < hw->cgx_links; link++) {
4111 rvu_write64(rvu, blkaddr, NIX_AF_RX_LINKX_CFG(link),
4112 ((u64)lmac_max_frs << 16) | NIC_HW_MIN_FRS);
4115 for (link = hw->cgx_links; link < hw->lbk_links; link++) {
4116 rvu_write64(rvu, blkaddr, NIX_AF_RX_LINKX_CFG(link),
4117 ((u64)lbk_max_frs << 16) | NIC_HW_MIN_FRS);
4119 if (hw->sdp_links) {
4120 link = hw->cgx_links + hw->lbk_links;
4121 rvu_write64(rvu, blkaddr, NIX_AF_RX_LINKX_CFG(link),
4122 SDP_HW_MAX_FRS << 16 | NIC_HW_MIN_FRS);
4125 /* Set credits for Tx links assuming max packet length allowed.
4126 * This will be reconfigured based on MTU set for PF/VF.
4128 for (cgx = 0; cgx < hw->cgx; cgx++) {
4129 lmac_cnt = cgx_get_lmac_cnt(rvu_cgx_pdata(cgx, rvu));
4130 /* Skip when cgx is not available or lmac cnt is zero */
4133 slink = cgx * hw->lmac_per_cgx;
4135 /* Get LMAC id's from bitmap */
4136 lmac_bmap = cgx_get_lmac_bmap(rvu_cgx_pdata(cgx, rvu));
4137 for_each_set_bit(iter, &lmac_bmap, rvu->hw->lmac_per_cgx) {
4138 lmac_fifo_len = rvu_cgx_get_lmac_fifolen(rvu, cgx, iter);
4139 if (!lmac_fifo_len) {
4141 "%s: Failed to get CGX/RPM%d:LMAC%d FIFO size\n",
4142 __func__, cgx, iter);
4145 tx_credits = (lmac_fifo_len - lmac_max_frs) / 16;
4146 /* Enable credits and set credit pkt count to max allowed */
4147 cfg = (tx_credits << 12) | (0x1FF << 2) | BIT_ULL(1);
4149 link = iter + slink;
4150 nix_hw->tx_credits[link] = tx_credits;
4151 rvu_write64(rvu, blkaddr,
4152 NIX_AF_TX_LINKX_NORM_CREDIT(link), cfg);
4156 /* Set Tx credits for LBK link */
4157 slink = hw->cgx_links;
4158 for (link = slink; link < (slink + hw->lbk_links); link++) {
4159 tx_credits = rvu_get_lbk_link_credits(rvu, lbk_max_frs);
4160 nix_hw->tx_credits[link] = tx_credits;
4161 /* Enable credits and set credit pkt count to max allowed */
4162 tx_credits = (tx_credits << 12) | (0x1FF << 2) | BIT_ULL(1);
4163 rvu_write64(rvu, blkaddr,
4164 NIX_AF_TX_LINKX_NORM_CREDIT(link), tx_credits);
4168 static int nix_calibrate_x2p(struct rvu *rvu, int blkaddr)
4173 /* Start X2P bus calibration */
4174 rvu_write64(rvu, blkaddr, NIX_AF_CFG,
4175 rvu_read64(rvu, blkaddr, NIX_AF_CFG) | BIT_ULL(9));
4176 /* Wait for calibration to complete */
4177 err = rvu_poll_reg(rvu, blkaddr,
4178 NIX_AF_STATUS, BIT_ULL(10), false);
4180 dev_err(rvu->dev, "NIX X2P bus calibration failed\n");
4184 status = rvu_read64(rvu, blkaddr, NIX_AF_STATUS);
4185 /* Check if CGX devices are ready */
4186 for (idx = 0; idx < rvu->cgx_cnt_max; idx++) {
4187 /* Skip when cgx port is not available */
4188 if (!rvu_cgx_pdata(idx, rvu) ||
4189 (status & (BIT_ULL(16 + idx))))
4192 "CGX%d didn't respond to NIX X2P calibration\n", idx);
4196 /* Check if LBK is ready */
4197 if (!(status & BIT_ULL(19))) {
4199 "LBK didn't respond to NIX X2P calibration\n");
4203 /* Clear 'calibrate_x2p' bit */
4204 rvu_write64(rvu, blkaddr, NIX_AF_CFG,
4205 rvu_read64(rvu, blkaddr, NIX_AF_CFG) & ~BIT_ULL(9));
4206 if (err || (status & 0x3FFULL))
4208 "NIX X2P calibration failed, status 0x%llx\n", status);
4214 static int nix_aq_init(struct rvu *rvu, struct rvu_block *block)
4219 /* Set admin queue endianness */
4220 cfg = rvu_read64(rvu, block->addr, NIX_AF_CFG);
4223 rvu_write64(rvu, block->addr, NIX_AF_CFG, cfg);
4226 rvu_write64(rvu, block->addr, NIX_AF_CFG, cfg);
4229 /* Do not bypass NDC cache */
4230 cfg = rvu_read64(rvu, block->addr, NIX_AF_NDC_CFG);
4232 #ifdef CONFIG_NDC_DIS_DYNAMIC_CACHING
4233 /* Disable caching of SQB aka SQEs */
4236 rvu_write64(rvu, block->addr, NIX_AF_NDC_CFG, cfg);
4238 /* Result structure can be followed by RQ/SQ/CQ context at
4239 * RES + 128bytes and a write mask at RES + 256 bytes, depending on
4240 * operation type. Alloc sufficient result memory for all operations.
4242 err = rvu_aq_alloc(rvu, &block->aq,
4243 Q_COUNT(AQ_SIZE), sizeof(struct nix_aq_inst_s),
4244 ALIGN(sizeof(struct nix_aq_res_s), 128) + 256);
4248 rvu_write64(rvu, block->addr, NIX_AF_AQ_CFG, AQ_SIZE);
4249 rvu_write64(rvu, block->addr,
4250 NIX_AF_AQ_BASE, (u64)block->aq->inst->iova);
4254 static void rvu_nix_setup_capabilities(struct rvu *rvu, int blkaddr)
4256 struct rvu_hwinfo *hw = rvu->hw;
4259 hw_const = rvu_read64(rvu, blkaddr, NIX_AF_CONST1);
4261 /* On OcteonTx2 DWRR quantum is directly configured into each of
4262 * the transmit scheduler queues. And PF/VF drivers were free to
4263 * config any value upto 2^24.
4264 * On CN10K, HW is modified, the quantum configuration at scheduler
4265 * queues is in terms of weight. And SW needs to setup a base DWRR MTU
4266 * at NIX_AF_DWRR_RPM_MTU / NIX_AF_DWRR_SDP_MTU. HW will do
4267 * 'DWRR MTU * weight' to get the quantum.
4269 * Check if HW uses a common MTU for all DWRR quantum configs.
4270 * On OcteonTx2 this register field is '0'.
4272 if (((hw_const >> 56) & 0x10) == 0x10)
4273 hw->cap.nix_common_dwrr_mtu = true;
4276 static int rvu_nix_block_init(struct rvu *rvu, struct nix_hw *nix_hw)
4278 const struct npc_lt_def_cfg *ltdefs;
4279 struct rvu_hwinfo *hw = rvu->hw;
4280 int blkaddr = nix_hw->blkaddr;
4281 struct rvu_block *block;
4285 block = &hw->block[blkaddr];
4287 if (is_rvu_96xx_B0(rvu)) {
4288 /* As per a HW errata in 96xx A0/B0 silicon, NIX may corrupt
4289 * internal state when conditional clocks are turned off.
4290 * Hence enable them.
4292 rvu_write64(rvu, blkaddr, NIX_AF_CFG,
4293 rvu_read64(rvu, blkaddr, NIX_AF_CFG) | 0x40ULL);
4295 /* Set chan/link to backpressure TL3 instead of TL2 */
4296 rvu_write64(rvu, blkaddr, NIX_AF_PSE_CHANNEL_LEVEL, 0x01);
4298 /* Disable SQ manager's sticky mode operation (set TM6 = 0)
4299 * This sticky mode is known to cause SQ stalls when multiple
4300 * SQs are mapped to same SMQ and transmitting pkts at a time.
4302 cfg = rvu_read64(rvu, blkaddr, NIX_AF_SQM_DBG_CTL_STATUS);
4303 cfg &= ~BIT_ULL(15);
4304 rvu_write64(rvu, blkaddr, NIX_AF_SQM_DBG_CTL_STATUS, cfg);
4307 ltdefs = rvu->kpu.lt_def;
4308 /* Calibrate X2P bus to check if CGX/LBK links are fine */
4309 err = nix_calibrate_x2p(rvu, blkaddr);
4313 /* Setup capabilities of the NIX block */
4314 rvu_nix_setup_capabilities(rvu, blkaddr);
4316 /* Initialize admin queue */
4317 err = nix_aq_init(rvu, block);
4321 /* Restore CINT timer delay to HW reset values */
4322 rvu_write64(rvu, blkaddr, NIX_AF_CINT_DELAY, 0x0ULL);
4324 cfg = rvu_read64(rvu, blkaddr, NIX_AF_SEB_CFG);
4326 /* For better performance use NDC TX instead of NDC RX for SQ's SQEs" */
4328 if (!is_rvu_otx2(rvu))
4329 cfg |= NIX_PTP_1STEP_EN;
4331 rvu_write64(rvu, blkaddr, NIX_AF_SEB_CFG, cfg);
4333 if (!is_rvu_otx2(rvu))
4334 rvu_nix_block_cn10k_init(rvu, nix_hw);
4336 if (is_block_implemented(hw, blkaddr)) {
4337 err = nix_setup_txschq(rvu, nix_hw, blkaddr);
4341 err = nix_setup_ipolicers(rvu, nix_hw, blkaddr);
4345 err = nix_af_mark_format_setup(rvu, nix_hw, blkaddr);
4349 err = nix_setup_mcast(rvu, nix_hw, blkaddr);
4353 err = nix_setup_txvlan(rvu, nix_hw);
4357 /* Configure segmentation offload formats */
4358 nix_setup_lso(rvu, nix_hw, blkaddr);
4360 /* Config Outer/Inner L2, IP, TCP, UDP and SCTP NPC layer info.
4361 * This helps HW protocol checker to identify headers
4362 * and validate length and checksums.
4364 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_OL2,
4365 (ltdefs->rx_ol2.lid << 8) | (ltdefs->rx_ol2.ltype_match << 4) |
4366 ltdefs->rx_ol2.ltype_mask);
4367 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_OIP4,
4368 (ltdefs->rx_oip4.lid << 8) | (ltdefs->rx_oip4.ltype_match << 4) |
4369 ltdefs->rx_oip4.ltype_mask);
4370 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_IIP4,
4371 (ltdefs->rx_iip4.lid << 8) | (ltdefs->rx_iip4.ltype_match << 4) |
4372 ltdefs->rx_iip4.ltype_mask);
4373 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_OIP6,
4374 (ltdefs->rx_oip6.lid << 8) | (ltdefs->rx_oip6.ltype_match << 4) |
4375 ltdefs->rx_oip6.ltype_mask);
4376 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_IIP6,
4377 (ltdefs->rx_iip6.lid << 8) | (ltdefs->rx_iip6.ltype_match << 4) |
4378 ltdefs->rx_iip6.ltype_mask);
4379 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_OTCP,
4380 (ltdefs->rx_otcp.lid << 8) | (ltdefs->rx_otcp.ltype_match << 4) |
4381 ltdefs->rx_otcp.ltype_mask);
4382 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_ITCP,
4383 (ltdefs->rx_itcp.lid << 8) | (ltdefs->rx_itcp.ltype_match << 4) |
4384 ltdefs->rx_itcp.ltype_mask);
4385 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_OUDP,
4386 (ltdefs->rx_oudp.lid << 8) | (ltdefs->rx_oudp.ltype_match << 4) |
4387 ltdefs->rx_oudp.ltype_mask);
4388 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_IUDP,
4389 (ltdefs->rx_iudp.lid << 8) | (ltdefs->rx_iudp.ltype_match << 4) |
4390 ltdefs->rx_iudp.ltype_mask);
4391 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_OSCTP,
4392 (ltdefs->rx_osctp.lid << 8) | (ltdefs->rx_osctp.ltype_match << 4) |
4393 ltdefs->rx_osctp.ltype_mask);
4394 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_ISCTP,
4395 (ltdefs->rx_isctp.lid << 8) | (ltdefs->rx_isctp.ltype_match << 4) |
4396 ltdefs->rx_isctp.ltype_mask);
4398 if (!is_rvu_otx2(rvu)) {
4399 /* Enable APAD calculation for other protocols
4400 * matching APAD0 and APAD1 lt def registers.
4402 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_CST_APAD0,
4403 (ltdefs->rx_apad0.valid << 11) |
4404 (ltdefs->rx_apad0.lid << 8) |
4405 (ltdefs->rx_apad0.ltype_match << 4) |
4406 ltdefs->rx_apad0.ltype_mask);
4407 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_CST_APAD1,
4408 (ltdefs->rx_apad1.valid << 11) |
4409 (ltdefs->rx_apad1.lid << 8) |
4410 (ltdefs->rx_apad1.ltype_match << 4) |
4411 ltdefs->rx_apad1.ltype_mask);
4413 /* Receive ethertype defination register defines layer
4414 * information in NPC_RESULT_S to identify the Ethertype
4415 * location in L2 header. Used for Ethertype overwriting
4416 * in inline IPsec flow.
4418 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_ET(0),
4419 (ltdefs->rx_et[0].offset << 12) |
4420 (ltdefs->rx_et[0].valid << 11) |
4421 (ltdefs->rx_et[0].lid << 8) |
4422 (ltdefs->rx_et[0].ltype_match << 4) |
4423 ltdefs->rx_et[0].ltype_mask);
4424 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_ET(1),
4425 (ltdefs->rx_et[1].offset << 12) |
4426 (ltdefs->rx_et[1].valid << 11) |
4427 (ltdefs->rx_et[1].lid << 8) |
4428 (ltdefs->rx_et[1].ltype_match << 4) |
4429 ltdefs->rx_et[1].ltype_mask);
4432 err = nix_rx_flowkey_alg_cfg(rvu, blkaddr);
4436 nix_hw->tx_credits = kcalloc(hw->cgx_links + hw->lbk_links,
4437 sizeof(u64), GFP_KERNEL);
4438 if (!nix_hw->tx_credits)
4441 /* Initialize CGX/LBK/SDP link credits, min/max pkt lengths */
4442 nix_link_config(rvu, blkaddr, nix_hw);
4444 /* Enable Channel backpressure */
4445 rvu_write64(rvu, blkaddr, NIX_AF_RX_CFG, BIT_ULL(0));
4450 int rvu_nix_init(struct rvu *rvu)
4452 struct rvu_hwinfo *hw = rvu->hw;
4453 struct nix_hw *nix_hw;
4454 int blkaddr = 0, err;
4457 hw->nix = devm_kcalloc(rvu->dev, MAX_NIX_BLKS, sizeof(struct nix_hw),
4462 blkaddr = rvu_get_next_nix_blkaddr(rvu, blkaddr);
4464 nix_hw = &hw->nix[i];
4466 nix_hw->blkaddr = blkaddr;
4467 err = rvu_nix_block_init(rvu, nix_hw);
4470 blkaddr = rvu_get_next_nix_blkaddr(rvu, blkaddr);
4477 static void rvu_nix_block_freemem(struct rvu *rvu, int blkaddr,
4478 struct rvu_block *block)
4480 struct nix_txsch *txsch;
4481 struct nix_mcast *mcast;
4482 struct nix_txvlan *vlan;
4483 struct nix_hw *nix_hw;
4486 rvu_aq_free(rvu, block->aq);
4488 if (is_block_implemented(rvu->hw, blkaddr)) {
4489 nix_hw = get_nix_hw(rvu->hw, blkaddr);
4493 for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
4494 txsch = &nix_hw->txsch[lvl];
4495 kfree(txsch->schq.bmap);
4498 kfree(nix_hw->tx_credits);
4500 nix_ipolicer_freemem(rvu, nix_hw);
4502 vlan = &nix_hw->txvlan;
4503 kfree(vlan->rsrc.bmap);
4504 mutex_destroy(&vlan->rsrc_lock);
4506 mcast = &nix_hw->mcast;
4507 qmem_free(rvu->dev, mcast->mce_ctx);
4508 qmem_free(rvu->dev, mcast->mcast_buf);
4509 mutex_destroy(&mcast->mce_lock);
4513 void rvu_nix_freemem(struct rvu *rvu)
4515 struct rvu_hwinfo *hw = rvu->hw;
4516 struct rvu_block *block;
4519 blkaddr = rvu_get_next_nix_blkaddr(rvu, blkaddr);
4521 block = &hw->block[blkaddr];
4522 rvu_nix_block_freemem(rvu, blkaddr, block);
4523 blkaddr = rvu_get_next_nix_blkaddr(rvu, blkaddr);
4527 int rvu_mbox_handler_nix_lf_start_rx(struct rvu *rvu, struct msg_req *req,
4528 struct msg_rsp *rsp)
4530 u16 pcifunc = req->hdr.pcifunc;
4531 struct rvu_pfvf *pfvf;
4534 err = nix_get_nixlf(rvu, pcifunc, &nixlf, NULL);
4538 rvu_npc_enable_default_entries(rvu, pcifunc, nixlf);
4540 npc_mcam_enable_flows(rvu, pcifunc);
4542 pfvf = rvu_get_pfvf(rvu, pcifunc);
4543 set_bit(NIXLF_INITIALIZED, &pfvf->flags);
4545 rvu_switch_update_rules(rvu, pcifunc);
4547 return rvu_cgx_start_stop_io(rvu, pcifunc, true);
4550 int rvu_mbox_handler_nix_lf_stop_rx(struct rvu *rvu, struct msg_req *req,
4551 struct msg_rsp *rsp)
4553 u16 pcifunc = req->hdr.pcifunc;
4554 struct rvu_pfvf *pfvf;
4557 err = nix_get_nixlf(rvu, pcifunc, &nixlf, NULL);
4561 rvu_npc_disable_mcam_entries(rvu, pcifunc, nixlf);
4563 pfvf = rvu_get_pfvf(rvu, pcifunc);
4564 clear_bit(NIXLF_INITIALIZED, &pfvf->flags);
4566 return rvu_cgx_start_stop_io(rvu, pcifunc, false);
4569 #define RX_SA_BASE GENMASK_ULL(52, 7)
4571 void rvu_nix_lf_teardown(struct rvu *rvu, u16 pcifunc, int blkaddr, int nixlf)
4573 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
4574 struct hwctx_disable_req ctx_req;
4575 int pf = rvu_get_pf(pcifunc);
4576 struct mac_ops *mac_ops;
4582 ctx_req.hdr.pcifunc = pcifunc;
4584 /* Cleanup NPC MCAM entries, free Tx scheduler queues being used */
4585 rvu_npc_disable_mcam_entries(rvu, pcifunc, nixlf);
4586 rvu_npc_free_mcam_entries(rvu, pcifunc, nixlf);
4587 nix_interface_deinit(rvu, pcifunc, nixlf);
4588 nix_rx_sync(rvu, blkaddr);
4589 nix_txschq_free(rvu, pcifunc);
4591 clear_bit(NIXLF_INITIALIZED, &pfvf->flags);
4593 rvu_cgx_start_stop_io(rvu, pcifunc, false);
4596 ctx_req.ctype = NIX_AQ_CTYPE_SQ;
4597 err = nix_lf_hwctx_disable(rvu, &ctx_req);
4599 dev_err(rvu->dev, "SQ ctx disable failed\n");
4603 ctx_req.ctype = NIX_AQ_CTYPE_RQ;
4604 err = nix_lf_hwctx_disable(rvu, &ctx_req);
4606 dev_err(rvu->dev, "RQ ctx disable failed\n");
4610 ctx_req.ctype = NIX_AQ_CTYPE_CQ;
4611 err = nix_lf_hwctx_disable(rvu, &ctx_req);
4613 dev_err(rvu->dev, "CQ ctx disable failed\n");
4616 /* reset HW config done for Switch headers */
4617 rvu_npc_set_parse_mode(rvu, pcifunc, OTX2_PRIV_FLAGS_DEFAULT,
4618 (PKIND_TX | PKIND_RX), 0, 0, 0, 0);
4620 /* Disabling CGX and NPC config done for PTP */
4621 if (pfvf->hw_rx_tstamp_en) {
4622 rvu_get_cgx_lmac_id(rvu->pf2cgxlmac_map[pf], &cgx_id, &lmac_id);
4623 cgxd = rvu_cgx_pdata(cgx_id, rvu);
4624 mac_ops = get_mac_ops(cgxd);
4625 mac_ops->mac_enadis_ptp_config(cgxd, lmac_id, false);
4626 /* Undo NPC config done for PTP */
4627 if (npc_config_ts_kpuaction(rvu, pf, pcifunc, false))
4628 dev_err(rvu->dev, "NPC config for PTP failed\n");
4629 pfvf->hw_rx_tstamp_en = false;
4632 /* reset priority flow control config */
4633 rvu_cgx_prio_flow_ctrl_cfg(rvu, pcifunc, 0, 0, 0);
4635 /* reset 802.3x flow control config */
4636 rvu_cgx_cfg_pause_frm(rvu, pcifunc, 0, 0);
4638 nix_ctx_free(rvu, pfvf);
4640 nix_free_all_bandprof(rvu, pcifunc);
4642 sa_base = rvu_read64(rvu, blkaddr, NIX_AF_LFX_RX_IPSEC_SA_BASE(nixlf));
4643 if (FIELD_GET(RX_SA_BASE, sa_base)) {
4644 err = rvu_cpt_ctx_flush(rvu, pcifunc);
4647 "CPT ctx flush failed with error: %d\n", err);
4651 #define NIX_AF_LFX_TX_CFG_PTP_EN BIT_ULL(32)
4653 static int rvu_nix_lf_ptp_tx_cfg(struct rvu *rvu, u16 pcifunc, bool enable)
4655 struct rvu_hwinfo *hw = rvu->hw;
4656 struct rvu_block *block;
4661 pf = rvu_get_pf(pcifunc);
4662 if (!is_mac_feature_supported(rvu, pf, RVU_LMAC_FEAT_PTP))
4665 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
4667 return NIX_AF_ERR_AF_LF_INVALID;
4669 block = &hw->block[blkaddr];
4670 nixlf = rvu_get_lf(rvu, block, pcifunc, 0);
4672 return NIX_AF_ERR_AF_LF_INVALID;
4674 cfg = rvu_read64(rvu, blkaddr, NIX_AF_LFX_TX_CFG(nixlf));
4677 cfg |= NIX_AF_LFX_TX_CFG_PTP_EN;
4679 cfg &= ~NIX_AF_LFX_TX_CFG_PTP_EN;
4681 rvu_write64(rvu, blkaddr, NIX_AF_LFX_TX_CFG(nixlf), cfg);
4686 int rvu_mbox_handler_nix_lf_ptp_tx_enable(struct rvu *rvu, struct msg_req *req,
4687 struct msg_rsp *rsp)
4689 return rvu_nix_lf_ptp_tx_cfg(rvu, req->hdr.pcifunc, true);
4692 int rvu_mbox_handler_nix_lf_ptp_tx_disable(struct rvu *rvu, struct msg_req *req,
4693 struct msg_rsp *rsp)
4695 return rvu_nix_lf_ptp_tx_cfg(rvu, req->hdr.pcifunc, false);
4698 int rvu_mbox_handler_nix_lso_format_cfg(struct rvu *rvu,
4699 struct nix_lso_format_cfg *req,
4700 struct nix_lso_format_cfg_rsp *rsp)
4702 u16 pcifunc = req->hdr.pcifunc;
4703 struct nix_hw *nix_hw;
4704 struct rvu_pfvf *pfvf;
4705 int blkaddr, idx, f;
4708 pfvf = rvu_get_pfvf(rvu, pcifunc);
4709 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
4710 if (!pfvf->nixlf || blkaddr < 0)
4711 return NIX_AF_ERR_AF_LF_INVALID;
4713 nix_hw = get_nix_hw(rvu->hw, blkaddr);
4715 return NIX_AF_ERR_INVALID_NIXBLK;
4717 /* Find existing matching LSO format, if any */
4718 for (idx = 0; idx < nix_hw->lso.in_use; idx++) {
4719 for (f = 0; f < NIX_LSO_FIELD_MAX; f++) {
4720 reg = rvu_read64(rvu, blkaddr,
4721 NIX_AF_LSO_FORMATX_FIELDX(idx, f));
4722 if (req->fields[f] != (reg & req->field_mask))
4726 if (f == NIX_LSO_FIELD_MAX)
4730 if (idx < nix_hw->lso.in_use) {
4732 rsp->lso_format_idx = idx;
4736 if (nix_hw->lso.in_use == nix_hw->lso.total)
4737 return NIX_AF_ERR_LSO_CFG_FAIL;
4739 rsp->lso_format_idx = nix_hw->lso.in_use++;
4741 for (f = 0; f < NIX_LSO_FIELD_MAX; f++)
4742 rvu_write64(rvu, blkaddr,
4743 NIX_AF_LSO_FORMATX_FIELDX(rsp->lso_format_idx, f),
4749 #define IPSEC_GEN_CFG_EGRP GENMASK_ULL(50, 48)
4750 #define IPSEC_GEN_CFG_OPCODE GENMASK_ULL(47, 32)
4751 #define IPSEC_GEN_CFG_PARAM1 GENMASK_ULL(31, 16)
4752 #define IPSEC_GEN_CFG_PARAM2 GENMASK_ULL(15, 0)
4754 #define CPT_INST_QSEL_BLOCK GENMASK_ULL(28, 24)
4755 #define CPT_INST_QSEL_PF_FUNC GENMASK_ULL(23, 8)
4756 #define CPT_INST_QSEL_SLOT GENMASK_ULL(7, 0)
4758 #define CPT_INST_CREDIT_TH GENMASK_ULL(53, 32)
4759 #define CPT_INST_CREDIT_BPID GENMASK_ULL(30, 22)
4760 #define CPT_INST_CREDIT_CNT GENMASK_ULL(21, 0)
4762 static void nix_inline_ipsec_cfg(struct rvu *rvu, struct nix_inline_ipsec_cfg *req,
4765 u8 cpt_idx, cpt_blkaddr;
4768 cpt_idx = (blkaddr == BLKADDR_NIX0) ? 0 : 1;
4771 /* Enable context prefetching */
4772 if (!is_rvu_otx2(rvu))
4775 /* Set OPCODE and EGRP */
4776 val |= FIELD_PREP(IPSEC_GEN_CFG_EGRP, req->gen_cfg.egrp);
4777 val |= FIELD_PREP(IPSEC_GEN_CFG_OPCODE, req->gen_cfg.opcode);
4778 val |= FIELD_PREP(IPSEC_GEN_CFG_PARAM1, req->gen_cfg.param1);
4779 val |= FIELD_PREP(IPSEC_GEN_CFG_PARAM2, req->gen_cfg.param2);
4781 rvu_write64(rvu, blkaddr, NIX_AF_RX_IPSEC_GEN_CFG, val);
4783 /* Set CPT queue for inline IPSec */
4784 val = FIELD_PREP(CPT_INST_QSEL_SLOT, req->inst_qsel.cpt_slot);
4785 val |= FIELD_PREP(CPT_INST_QSEL_PF_FUNC,
4786 req->inst_qsel.cpt_pf_func);
4788 if (!is_rvu_otx2(rvu)) {
4789 cpt_blkaddr = (cpt_idx == 0) ? BLKADDR_CPT0 :
4791 val |= FIELD_PREP(CPT_INST_QSEL_BLOCK, cpt_blkaddr);
4794 rvu_write64(rvu, blkaddr, NIX_AF_RX_CPTX_INST_QSEL(cpt_idx),
4797 /* Set CPT credit */
4798 val = rvu_read64(rvu, blkaddr, NIX_AF_RX_CPTX_CREDIT(cpt_idx));
4799 if ((val & 0x3FFFFF) != 0x3FFFFF)
4800 rvu_write64(rvu, blkaddr, NIX_AF_RX_CPTX_CREDIT(cpt_idx),
4803 val = FIELD_PREP(CPT_INST_CREDIT_CNT, req->cpt_credit);
4804 val |= FIELD_PREP(CPT_INST_CREDIT_BPID, req->bpid);
4805 val |= FIELD_PREP(CPT_INST_CREDIT_TH, req->credit_th);
4806 rvu_write64(rvu, blkaddr, NIX_AF_RX_CPTX_CREDIT(cpt_idx), val);
4808 rvu_write64(rvu, blkaddr, NIX_AF_RX_IPSEC_GEN_CFG, 0x0);
4809 rvu_write64(rvu, blkaddr, NIX_AF_RX_CPTX_INST_QSEL(cpt_idx),
4811 val = rvu_read64(rvu, blkaddr, NIX_AF_RX_CPTX_CREDIT(cpt_idx));
4812 if ((val & 0x3FFFFF) != 0x3FFFFF)
4813 rvu_write64(rvu, blkaddr, NIX_AF_RX_CPTX_CREDIT(cpt_idx),
4818 int rvu_mbox_handler_nix_inline_ipsec_cfg(struct rvu *rvu,
4819 struct nix_inline_ipsec_cfg *req,
4820 struct msg_rsp *rsp)
4822 if (!is_block_implemented(rvu->hw, BLKADDR_CPT0))
4825 nix_inline_ipsec_cfg(rvu, req, BLKADDR_NIX0);
4826 if (is_block_implemented(rvu->hw, BLKADDR_CPT1))
4827 nix_inline_ipsec_cfg(rvu, req, BLKADDR_NIX1);
4832 int rvu_mbox_handler_nix_read_inline_ipsec_cfg(struct rvu *rvu,
4833 struct msg_req *req,
4834 struct nix_inline_ipsec_cfg *rsp)
4839 if (!is_block_implemented(rvu->hw, BLKADDR_CPT0))
4842 val = rvu_read64(rvu, BLKADDR_NIX0, NIX_AF_RX_IPSEC_GEN_CFG);
4843 rsp->gen_cfg.egrp = FIELD_GET(IPSEC_GEN_CFG_EGRP, val);
4844 rsp->gen_cfg.opcode = FIELD_GET(IPSEC_GEN_CFG_OPCODE, val);
4845 rsp->gen_cfg.param1 = FIELD_GET(IPSEC_GEN_CFG_PARAM1, val);
4846 rsp->gen_cfg.param2 = FIELD_GET(IPSEC_GEN_CFG_PARAM2, val);
4848 val = rvu_read64(rvu, BLKADDR_NIX0, NIX_AF_RX_CPTX_CREDIT(0));
4849 rsp->cpt_credit = FIELD_GET(CPT_INST_CREDIT_CNT, val);
4850 rsp->credit_th = FIELD_GET(CPT_INST_CREDIT_TH, val);
4851 rsp->bpid = FIELD_GET(CPT_INST_CREDIT_BPID, val);
4856 int rvu_mbox_handler_nix_inline_ipsec_lf_cfg(struct rvu *rvu,
4857 struct nix_inline_ipsec_lf_cfg *req,
4858 struct msg_rsp *rsp)
4860 int lf, blkaddr, err;
4863 if (!is_block_implemented(rvu->hw, BLKADDR_CPT0))
4866 err = nix_get_nixlf(rvu, req->hdr.pcifunc, &lf, &blkaddr);
4871 /* Set TT, TAG_CONST, SA_POW2_SIZE and LENM1_MAX */
4872 val = (u64)req->ipsec_cfg0.tt << 44 |
4873 (u64)req->ipsec_cfg0.tag_const << 20 |
4874 (u64)req->ipsec_cfg0.sa_pow2_size << 16 |
4875 req->ipsec_cfg0.lenm1_max;
4877 if (blkaddr == BLKADDR_NIX1)
4880 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RX_IPSEC_CFG0(lf), val);
4882 /* Set SA_IDX_W and SA_IDX_MAX */
4883 val = (u64)req->ipsec_cfg1.sa_idx_w << 32 |
4884 req->ipsec_cfg1.sa_idx_max;
4885 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RX_IPSEC_CFG1(lf), val);
4887 /* Set SA base address */
4888 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RX_IPSEC_SA_BASE(lf),
4891 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RX_IPSEC_CFG0(lf), 0x0);
4892 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RX_IPSEC_CFG1(lf), 0x0);
4893 rvu_write64(rvu, blkaddr, NIX_AF_LFX_RX_IPSEC_SA_BASE(lf),
4900 void rvu_nix_reset_mac(struct rvu_pfvf *pfvf, int pcifunc)
4902 bool from_vf = !!(pcifunc & RVU_PFVF_FUNC_MASK);
4904 /* overwrite vf mac address with default_mac */
4906 ether_addr_copy(pfvf->mac_addr, pfvf->default_mac);
4909 /* NIX ingress policers or bandwidth profiles APIs */
4910 static void nix_config_rx_pkt_policer_precolor(struct rvu *rvu, int blkaddr)
4912 struct npc_lt_def_cfg defs, *ltdefs;
4915 memcpy(ltdefs, rvu->kpu.lt_def, sizeof(struct npc_lt_def_cfg));
4917 /* Extract PCP and DEI fields from outer VLAN from byte offset
4918 * 2 from the start of LB_PTR (ie TAG).
4919 * VLAN0 is Outer VLAN and VLAN1 is Inner VLAN. Inner VLAN
4920 * fields are considered when 'Tunnel enable' is set in profile.
4922 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_VLAN0_PCP_DEI,
4923 (2UL << 12) | (ltdefs->ovlan.lid << 8) |
4924 (ltdefs->ovlan.ltype_match << 4) |
4925 ltdefs->ovlan.ltype_mask);
4926 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_VLAN1_PCP_DEI,
4927 (2UL << 12) | (ltdefs->ivlan.lid << 8) |
4928 (ltdefs->ivlan.ltype_match << 4) |
4929 ltdefs->ivlan.ltype_mask);
4931 /* DSCP field in outer and tunneled IPv4 packets */
4932 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_OIP4_DSCP,
4933 (1UL << 12) | (ltdefs->rx_oip4.lid << 8) |
4934 (ltdefs->rx_oip4.ltype_match << 4) |
4935 ltdefs->rx_oip4.ltype_mask);
4936 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_IIP4_DSCP,
4937 (1UL << 12) | (ltdefs->rx_iip4.lid << 8) |
4938 (ltdefs->rx_iip4.ltype_match << 4) |
4939 ltdefs->rx_iip4.ltype_mask);
4941 /* DSCP field (traffic class) in outer and tunneled IPv6 packets */
4942 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_OIP6_DSCP,
4943 (1UL << 11) | (ltdefs->rx_oip6.lid << 8) |
4944 (ltdefs->rx_oip6.ltype_match << 4) |
4945 ltdefs->rx_oip6.ltype_mask);
4946 rvu_write64(rvu, blkaddr, NIX_AF_RX_DEF_IIP6_DSCP,
4947 (1UL << 11) | (ltdefs->rx_iip6.lid << 8) |
4948 (ltdefs->rx_iip6.ltype_match << 4) |
4949 ltdefs->rx_iip6.ltype_mask);
4952 static int nix_init_policer_context(struct rvu *rvu, struct nix_hw *nix_hw,
4953 int layer, int prof_idx)
4955 struct nix_cn10k_aq_enq_req aq_req;
4958 memset(&aq_req, 0, sizeof(struct nix_cn10k_aq_enq_req));
4960 aq_req.qidx = (prof_idx & 0x3FFF) | (layer << 14);
4961 aq_req.ctype = NIX_AQ_CTYPE_BANDPROF;
4962 aq_req.op = NIX_AQ_INSTOP_INIT;
4964 /* Context is all zeros, submit to AQ */
4965 rc = rvu_nix_blk_aq_enq_inst(rvu, nix_hw,
4966 (struct nix_aq_enq_req *)&aq_req, NULL);
4968 dev_err(rvu->dev, "Failed to INIT bandwidth profile layer %d profile %d\n",
4973 static int nix_setup_ipolicers(struct rvu *rvu,
4974 struct nix_hw *nix_hw, int blkaddr)
4976 struct rvu_hwinfo *hw = rvu->hw;
4977 struct nix_ipolicer *ipolicer;
4978 int err, layer, prof_idx;
4981 cfg = rvu_read64(rvu, blkaddr, NIX_AF_CONST);
4982 if (!(cfg & BIT_ULL(61))) {
4983 hw->cap.ipolicer = false;
4987 hw->cap.ipolicer = true;
4988 nix_hw->ipolicer = devm_kcalloc(rvu->dev, BAND_PROF_NUM_LAYERS,
4989 sizeof(*ipolicer), GFP_KERNEL);
4990 if (!nix_hw->ipolicer)
4993 cfg = rvu_read64(rvu, blkaddr, NIX_AF_PL_CONST);
4995 for (layer = 0; layer < BAND_PROF_NUM_LAYERS; layer++) {
4996 ipolicer = &nix_hw->ipolicer[layer];
4998 case BAND_PROF_LEAF_LAYER:
4999 ipolicer->band_prof.max = cfg & 0XFFFF;
5001 case BAND_PROF_MID_LAYER:
5002 ipolicer->band_prof.max = (cfg >> 16) & 0XFFFF;
5004 case BAND_PROF_TOP_LAYER:
5005 ipolicer->band_prof.max = (cfg >> 32) & 0XFFFF;
5009 if (!ipolicer->band_prof.max)
5012 err = rvu_alloc_bitmap(&ipolicer->band_prof);
5016 ipolicer->pfvf_map = devm_kcalloc(rvu->dev,
5017 ipolicer->band_prof.max,
5018 sizeof(u16), GFP_KERNEL);
5019 if (!ipolicer->pfvf_map)
5022 ipolicer->match_id = devm_kcalloc(rvu->dev,
5023 ipolicer->band_prof.max,
5024 sizeof(u16), GFP_KERNEL);
5025 if (!ipolicer->match_id)
5029 prof_idx < ipolicer->band_prof.max; prof_idx++) {
5030 /* Set AF as current owner for INIT ops to succeed */
5031 ipolicer->pfvf_map[prof_idx] = 0x00;
5033 /* There is no enable bit in the profile context,
5034 * so no context disable. So let's INIT them here
5035 * so that PF/VF later on have to just do WRITE to
5036 * setup policer rates and config.
5038 err = nix_init_policer_context(rvu, nix_hw,
5044 /* Allocate memory for maintaining ref_counts for MID level
5045 * profiles, this will be needed for leaf layer profiles'
5048 if (layer != BAND_PROF_MID_LAYER)
5051 ipolicer->ref_count = devm_kcalloc(rvu->dev,
5052 ipolicer->band_prof.max,
5053 sizeof(u16), GFP_KERNEL);
5054 if (!ipolicer->ref_count)
5058 /* Set policer timeunit to 2us ie (19 + 1) * 100 nsec = 2us */
5059 rvu_write64(rvu, blkaddr, NIX_AF_PL_TS, 19);
5061 nix_config_rx_pkt_policer_precolor(rvu, blkaddr);
5066 static void nix_ipolicer_freemem(struct rvu *rvu, struct nix_hw *nix_hw)
5068 struct nix_ipolicer *ipolicer;
5071 if (!rvu->hw->cap.ipolicer)
5074 for (layer = 0; layer < BAND_PROF_NUM_LAYERS; layer++) {
5075 ipolicer = &nix_hw->ipolicer[layer];
5077 if (!ipolicer->band_prof.max)
5080 kfree(ipolicer->band_prof.bmap);
5084 static int nix_verify_bandprof(struct nix_cn10k_aq_enq_req *req,
5085 struct nix_hw *nix_hw, u16 pcifunc)
5087 struct nix_ipolicer *ipolicer;
5088 int layer, hi_layer, prof_idx;
5090 /* Bits [15:14] in profile index represent layer */
5091 layer = (req->qidx >> 14) & 0x03;
5092 prof_idx = req->qidx & 0x3FFF;
5094 ipolicer = &nix_hw->ipolicer[layer];
5095 if (prof_idx >= ipolicer->band_prof.max)
5098 /* Check if the profile is allocated to the requesting PCIFUNC or not
5099 * with the exception of AF. AF is allowed to read and update contexts.
5101 if (pcifunc && ipolicer->pfvf_map[prof_idx] != pcifunc)
5104 /* If this profile is linked to higher layer profile then check
5105 * if that profile is also allocated to the requesting PCIFUNC
5108 if (!req->prof.hl_en)
5111 /* Leaf layer profile can link only to mid layer and
5112 * mid layer to top layer.
5114 if (layer == BAND_PROF_LEAF_LAYER)
5115 hi_layer = BAND_PROF_MID_LAYER;
5116 else if (layer == BAND_PROF_MID_LAYER)
5117 hi_layer = BAND_PROF_TOP_LAYER;
5121 ipolicer = &nix_hw->ipolicer[hi_layer];
5122 prof_idx = req->prof.band_prof_id;
5123 if (prof_idx >= ipolicer->band_prof.max ||
5124 ipolicer->pfvf_map[prof_idx] != pcifunc)
5130 int rvu_mbox_handler_nix_bandprof_alloc(struct rvu *rvu,
5131 struct nix_bandprof_alloc_req *req,
5132 struct nix_bandprof_alloc_rsp *rsp)
5134 int blkaddr, layer, prof, idx, err;
5135 u16 pcifunc = req->hdr.pcifunc;
5136 struct nix_ipolicer *ipolicer;
5137 struct nix_hw *nix_hw;
5139 if (!rvu->hw->cap.ipolicer)
5140 return NIX_AF_ERR_IPOLICER_NOTSUPP;
5142 err = nix_get_struct_ptrs(rvu, pcifunc, &nix_hw, &blkaddr);
5146 mutex_lock(&rvu->rsrc_lock);
5147 for (layer = 0; layer < BAND_PROF_NUM_LAYERS; layer++) {
5148 if (layer == BAND_PROF_INVAL_LAYER)
5150 if (!req->prof_count[layer])
5153 ipolicer = &nix_hw->ipolicer[layer];
5154 for (idx = 0; idx < req->prof_count[layer]; idx++) {
5155 /* Allocate a max of 'MAX_BANDPROF_PER_PFFUNC' profiles */
5156 if (idx == MAX_BANDPROF_PER_PFFUNC)
5159 prof = rvu_alloc_rsrc(&ipolicer->band_prof);
5162 rsp->prof_count[layer]++;
5163 rsp->prof_idx[layer][idx] = prof;
5164 ipolicer->pfvf_map[prof] = pcifunc;
5167 mutex_unlock(&rvu->rsrc_lock);
5171 static int nix_free_all_bandprof(struct rvu *rvu, u16 pcifunc)
5173 int blkaddr, layer, prof_idx, err;
5174 struct nix_ipolicer *ipolicer;
5175 struct nix_hw *nix_hw;
5177 if (!rvu->hw->cap.ipolicer)
5178 return NIX_AF_ERR_IPOLICER_NOTSUPP;
5180 err = nix_get_struct_ptrs(rvu, pcifunc, &nix_hw, &blkaddr);
5184 mutex_lock(&rvu->rsrc_lock);
5185 /* Free all the profiles allocated to the PCIFUNC */
5186 for (layer = 0; layer < BAND_PROF_NUM_LAYERS; layer++) {
5187 if (layer == BAND_PROF_INVAL_LAYER)
5189 ipolicer = &nix_hw->ipolicer[layer];
5191 for (prof_idx = 0; prof_idx < ipolicer->band_prof.max; prof_idx++) {
5192 if (ipolicer->pfvf_map[prof_idx] != pcifunc)
5195 /* Clear ratelimit aggregation, if any */
5196 if (layer == BAND_PROF_LEAF_LAYER &&
5197 ipolicer->match_id[prof_idx])
5198 nix_clear_ratelimit_aggr(rvu, nix_hw, prof_idx);
5200 ipolicer->pfvf_map[prof_idx] = 0x00;
5201 ipolicer->match_id[prof_idx] = 0;
5202 rvu_free_rsrc(&ipolicer->band_prof, prof_idx);
5205 mutex_unlock(&rvu->rsrc_lock);
5209 int rvu_mbox_handler_nix_bandprof_free(struct rvu *rvu,
5210 struct nix_bandprof_free_req *req,
5211 struct msg_rsp *rsp)
5213 int blkaddr, layer, prof_idx, idx, err;
5214 u16 pcifunc = req->hdr.pcifunc;
5215 struct nix_ipolicer *ipolicer;
5216 struct nix_hw *nix_hw;
5219 return nix_free_all_bandprof(rvu, pcifunc);
5221 if (!rvu->hw->cap.ipolicer)
5222 return NIX_AF_ERR_IPOLICER_NOTSUPP;
5224 err = nix_get_struct_ptrs(rvu, pcifunc, &nix_hw, &blkaddr);
5228 mutex_lock(&rvu->rsrc_lock);
5229 /* Free the requested profile indices */
5230 for (layer = 0; layer < BAND_PROF_NUM_LAYERS; layer++) {
5231 if (layer == BAND_PROF_INVAL_LAYER)
5233 if (!req->prof_count[layer])
5236 ipolicer = &nix_hw->ipolicer[layer];
5237 for (idx = 0; idx < req->prof_count[layer]; idx++) {
5238 prof_idx = req->prof_idx[layer][idx];
5239 if (prof_idx >= ipolicer->band_prof.max ||
5240 ipolicer->pfvf_map[prof_idx] != pcifunc)
5243 /* Clear ratelimit aggregation, if any */
5244 if (layer == BAND_PROF_LEAF_LAYER &&
5245 ipolicer->match_id[prof_idx])
5246 nix_clear_ratelimit_aggr(rvu, nix_hw, prof_idx);
5248 ipolicer->pfvf_map[prof_idx] = 0x00;
5249 ipolicer->match_id[prof_idx] = 0;
5250 rvu_free_rsrc(&ipolicer->band_prof, prof_idx);
5251 if (idx == MAX_BANDPROF_PER_PFFUNC)
5255 mutex_unlock(&rvu->rsrc_lock);
5259 int nix_aq_context_read(struct rvu *rvu, struct nix_hw *nix_hw,
5260 struct nix_cn10k_aq_enq_req *aq_req,
5261 struct nix_cn10k_aq_enq_rsp *aq_rsp,
5262 u16 pcifunc, u8 ctype, u32 qidx)
5264 memset(aq_req, 0, sizeof(struct nix_cn10k_aq_enq_req));
5265 aq_req->hdr.pcifunc = pcifunc;
5266 aq_req->ctype = ctype;
5267 aq_req->op = NIX_AQ_INSTOP_READ;
5268 aq_req->qidx = qidx;
5270 return rvu_nix_blk_aq_enq_inst(rvu, nix_hw,
5271 (struct nix_aq_enq_req *)aq_req,
5272 (struct nix_aq_enq_rsp *)aq_rsp);
5275 static int nix_ipolicer_map_leaf_midprofs(struct rvu *rvu,
5276 struct nix_hw *nix_hw,
5277 struct nix_cn10k_aq_enq_req *aq_req,
5278 struct nix_cn10k_aq_enq_rsp *aq_rsp,
5279 u32 leaf_prof, u16 mid_prof)
5281 memset(aq_req, 0, sizeof(struct nix_cn10k_aq_enq_req));
5282 aq_req->hdr.pcifunc = 0x00;
5283 aq_req->ctype = NIX_AQ_CTYPE_BANDPROF;
5284 aq_req->op = NIX_AQ_INSTOP_WRITE;
5285 aq_req->qidx = leaf_prof;
5287 aq_req->prof.band_prof_id = mid_prof;
5288 aq_req->prof_mask.band_prof_id = GENMASK(6, 0);
5289 aq_req->prof.hl_en = 1;
5290 aq_req->prof_mask.hl_en = 1;
5292 return rvu_nix_blk_aq_enq_inst(rvu, nix_hw,
5293 (struct nix_aq_enq_req *)aq_req,
5294 (struct nix_aq_enq_rsp *)aq_rsp);
5297 int rvu_nix_setup_ratelimit_aggr(struct rvu *rvu, u16 pcifunc,
5298 u16 rq_idx, u16 match_id)
5300 int leaf_prof, mid_prof, leaf_match;
5301 struct nix_cn10k_aq_enq_req aq_req;
5302 struct nix_cn10k_aq_enq_rsp aq_rsp;
5303 struct nix_ipolicer *ipolicer;
5304 struct nix_hw *nix_hw;
5305 int blkaddr, idx, rc;
5307 if (!rvu->hw->cap.ipolicer)
5310 rc = nix_get_struct_ptrs(rvu, pcifunc, &nix_hw, &blkaddr);
5314 /* Fetch the RQ's context to see if policing is enabled */
5315 rc = nix_aq_context_read(rvu, nix_hw, &aq_req, &aq_rsp, pcifunc,
5316 NIX_AQ_CTYPE_RQ, rq_idx);
5319 "%s: Failed to fetch RQ%d context of PFFUNC 0x%x\n",
5320 __func__, rq_idx, pcifunc);
5324 if (!aq_rsp.rq.policer_ena)
5327 /* Get the bandwidth profile ID mapped to this RQ */
5328 leaf_prof = aq_rsp.rq.band_prof_id;
5330 ipolicer = &nix_hw->ipolicer[BAND_PROF_LEAF_LAYER];
5331 ipolicer->match_id[leaf_prof] = match_id;
5333 /* Check if any other leaf profile is marked with same match_id */
5334 for (idx = 0; idx < ipolicer->band_prof.max; idx++) {
5335 if (idx == leaf_prof)
5337 if (ipolicer->match_id[idx] != match_id)
5344 if (idx == ipolicer->band_prof.max)
5347 /* Fetch the matching profile's context to check if it's already
5348 * mapped to a mid level profile.
5350 rc = nix_aq_context_read(rvu, nix_hw, &aq_req, &aq_rsp, 0x00,
5351 NIX_AQ_CTYPE_BANDPROF, leaf_match);
5354 "%s: Failed to fetch context of leaf profile %d\n",
5355 __func__, leaf_match);
5359 ipolicer = &nix_hw->ipolicer[BAND_PROF_MID_LAYER];
5360 if (aq_rsp.prof.hl_en) {
5361 /* Get Mid layer prof index and map leaf_prof index
5362 * also such that flows that are being steered
5363 * to different RQs and marked with same match_id
5364 * are rate limited in a aggregate fashion
5366 mid_prof = aq_rsp.prof.band_prof_id;
5367 rc = nix_ipolicer_map_leaf_midprofs(rvu, nix_hw,
5369 leaf_prof, mid_prof);
5372 "%s: Failed to map leaf(%d) and mid(%d) profiles\n",
5373 __func__, leaf_prof, mid_prof);
5377 mutex_lock(&rvu->rsrc_lock);
5378 ipolicer->ref_count[mid_prof]++;
5379 mutex_unlock(&rvu->rsrc_lock);
5383 /* Allocate a mid layer profile and
5384 * map both 'leaf_prof' and 'leaf_match' profiles to it.
5386 mutex_lock(&rvu->rsrc_lock);
5387 mid_prof = rvu_alloc_rsrc(&ipolicer->band_prof);
5390 "%s: Unable to allocate mid layer profile\n", __func__);
5391 mutex_unlock(&rvu->rsrc_lock);
5394 mutex_unlock(&rvu->rsrc_lock);
5395 ipolicer->pfvf_map[mid_prof] = 0x00;
5396 ipolicer->ref_count[mid_prof] = 0;
5398 /* Initialize mid layer profile same as 'leaf_prof' */
5399 rc = nix_aq_context_read(rvu, nix_hw, &aq_req, &aq_rsp, 0x00,
5400 NIX_AQ_CTYPE_BANDPROF, leaf_prof);
5403 "%s: Failed to fetch context of leaf profile %d\n",
5404 __func__, leaf_prof);
5408 memset(&aq_req, 0, sizeof(struct nix_cn10k_aq_enq_req));
5409 aq_req.hdr.pcifunc = 0x00;
5410 aq_req.qidx = (mid_prof & 0x3FFF) | (BAND_PROF_MID_LAYER << 14);
5411 aq_req.ctype = NIX_AQ_CTYPE_BANDPROF;
5412 aq_req.op = NIX_AQ_INSTOP_WRITE;
5413 memcpy(&aq_req.prof, &aq_rsp.prof, sizeof(struct nix_bandprof_s));
5414 memset((char *)&aq_req.prof_mask, 0xff, sizeof(struct nix_bandprof_s));
5415 /* Clear higher layer enable bit in the mid profile, just in case */
5416 aq_req.prof.hl_en = 0;
5417 aq_req.prof_mask.hl_en = 1;
5419 rc = rvu_nix_blk_aq_enq_inst(rvu, nix_hw,
5420 (struct nix_aq_enq_req *)&aq_req, NULL);
5423 "%s: Failed to INIT context of mid layer profile %d\n",
5424 __func__, mid_prof);
5428 /* Map both leaf profiles to this mid layer profile */
5429 rc = nix_ipolicer_map_leaf_midprofs(rvu, nix_hw,
5431 leaf_prof, mid_prof);
5434 "%s: Failed to map leaf(%d) and mid(%d) profiles\n",
5435 __func__, leaf_prof, mid_prof);
5439 mutex_lock(&rvu->rsrc_lock);
5440 ipolicer->ref_count[mid_prof]++;
5441 mutex_unlock(&rvu->rsrc_lock);
5443 rc = nix_ipolicer_map_leaf_midprofs(rvu, nix_hw,
5445 leaf_match, mid_prof);
5448 "%s: Failed to map leaf(%d) and mid(%d) profiles\n",
5449 __func__, leaf_match, mid_prof);
5450 ipolicer->ref_count[mid_prof]--;
5454 mutex_lock(&rvu->rsrc_lock);
5455 ipolicer->ref_count[mid_prof]++;
5456 mutex_unlock(&rvu->rsrc_lock);
5462 /* Called with mutex rsrc_lock */
5463 static void nix_clear_ratelimit_aggr(struct rvu *rvu, struct nix_hw *nix_hw,
5466 struct nix_cn10k_aq_enq_req aq_req;
5467 struct nix_cn10k_aq_enq_rsp aq_rsp;
5468 struct nix_ipolicer *ipolicer;
5472 mutex_unlock(&rvu->rsrc_lock);
5474 rc = nix_aq_context_read(rvu, nix_hw, &aq_req, &aq_rsp, 0x00,
5475 NIX_AQ_CTYPE_BANDPROF, leaf_prof);
5477 mutex_lock(&rvu->rsrc_lock);
5480 "%s: Failed to fetch context of leaf profile %d\n",
5481 __func__, leaf_prof);
5485 if (!aq_rsp.prof.hl_en)
5488 mid_prof = aq_rsp.prof.band_prof_id;
5489 ipolicer = &nix_hw->ipolicer[BAND_PROF_MID_LAYER];
5490 ipolicer->ref_count[mid_prof]--;
5491 /* If ref_count is zero, free mid layer profile */
5492 if (!ipolicer->ref_count[mid_prof]) {
5493 ipolicer->pfvf_map[mid_prof] = 0x00;
5494 rvu_free_rsrc(&ipolicer->band_prof, mid_prof);
5498 int rvu_mbox_handler_nix_bandprof_get_hwinfo(struct rvu *rvu, struct msg_req *req,
5499 struct nix_bandprof_get_hwinfo_rsp *rsp)
5501 struct nix_ipolicer *ipolicer;
5502 int blkaddr, layer, err;
5503 struct nix_hw *nix_hw;
5506 if (!rvu->hw->cap.ipolicer)
5507 return NIX_AF_ERR_IPOLICER_NOTSUPP;
5509 err = nix_get_struct_ptrs(rvu, req->hdr.pcifunc, &nix_hw, &blkaddr);
5513 /* Return number of bandwidth profiles free at each layer */
5514 mutex_lock(&rvu->rsrc_lock);
5515 for (layer = 0; layer < BAND_PROF_NUM_LAYERS; layer++) {
5516 if (layer == BAND_PROF_INVAL_LAYER)
5519 ipolicer = &nix_hw->ipolicer[layer];
5520 rsp->prof_count[layer] = rvu_rsrc_free_count(&ipolicer->band_prof);
5522 mutex_unlock(&rvu->rsrc_lock);
5524 /* Set the policer timeunit in nanosec */
5525 tu = rvu_read64(rvu, blkaddr, NIX_AF_PL_TS) & GENMASK_ULL(9, 0);
5526 rsp->policer_timeunit = (tu + 1) * 100;