1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell OcteonTx2 RVU Admin Function driver
4 * Copyright (C) 2018 Marvell International Ltd.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/module.h>
12 #include <linux/interrupt.h>
13 #include <linux/delay.h>
14 #include <linux/irq.h>
15 #include <linux/pci.h>
16 #include <linux/sysfs.h>
23 #include "rvu_trace.h"
25 #define DRV_NAME "rvu_af"
26 #define DRV_STRING "Marvell OcteonTX2 RVU Admin Function Driver"
28 static int rvu_get_hwvf(struct rvu *rvu, int pcifunc);
30 static void rvu_set_msix_offset(struct rvu *rvu, struct rvu_pfvf *pfvf,
31 struct rvu_block *block, int lf);
32 static void rvu_clear_msix_offset(struct rvu *rvu, struct rvu_pfvf *pfvf,
33 struct rvu_block *block, int lf);
34 static void __rvu_flr_handler(struct rvu *rvu, u16 pcifunc);
36 static int rvu_mbox_init(struct rvu *rvu, struct mbox_wq_info *mw,
38 void (mbox_handler)(struct work_struct *),
39 void (mbox_up_handler)(struct work_struct *));
45 /* Supported devices */
46 static const struct pci_device_id rvu_id_table[] = {
47 { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVID_OCTEONTX2_RVU_AF) },
48 { 0, } /* end of table */
51 MODULE_AUTHOR("Sunil Goutham <sgoutham@marvell.com>");
52 MODULE_DESCRIPTION(DRV_STRING);
53 MODULE_LICENSE("GPL v2");
54 MODULE_DEVICE_TABLE(pci, rvu_id_table);
56 static char *mkex_profile; /* MKEX profile name */
57 module_param(mkex_profile, charp, 0000);
58 MODULE_PARM_DESC(mkex_profile, "MKEX profile name string");
60 static char *kpu_profile; /* KPU profile name */
61 module_param(kpu_profile, charp, 0000);
62 MODULE_PARM_DESC(kpu_profile, "KPU profile name string");
64 static void rvu_setup_hw_capabilities(struct rvu *rvu)
66 struct rvu_hwinfo *hw = rvu->hw;
68 hw->cap.nix_tx_aggr_lvl = NIX_TXSCH_LVL_TL1;
69 hw->cap.nix_fixed_txschq_mapping = false;
70 hw->cap.nix_shaping = true;
71 hw->cap.nix_tx_link_bp = true;
72 hw->cap.nix_rx_multicast = true;
75 if (is_rvu_96xx_B0(rvu)) {
76 hw->cap.nix_fixed_txschq_mapping = true;
77 hw->cap.nix_txsch_per_cgx_lmac = 4;
78 hw->cap.nix_txsch_per_lbk_lmac = 132;
79 hw->cap.nix_txsch_per_sdp_lmac = 76;
80 hw->cap.nix_shaping = false;
81 hw->cap.nix_tx_link_bp = false;
82 if (is_rvu_96xx_A0(rvu))
83 hw->cap.nix_rx_multicast = false;
86 if (!is_rvu_otx2(rvu))
87 hw->cap.per_pf_mbox_regs = true;
90 /* Poll a RVU block's register 'offset', for a 'zero'
91 * or 'nonzero' at bits specified by 'mask'
93 int rvu_poll_reg(struct rvu *rvu, u64 block, u64 offset, u64 mask, bool zero)
95 unsigned long timeout = jiffies + usecs_to_jiffies(10000);
99 reg = rvu->afreg_base + ((block << 28) | offset);
101 reg_val = readq(reg);
102 if (zero && !(reg_val & mask))
104 if (!zero && (reg_val & mask))
106 if (time_before(jiffies, timeout)) {
113 int rvu_alloc_rsrc(struct rsrc_bmap *rsrc)
120 id = find_first_zero_bit(rsrc->bmap, rsrc->max);
124 __set_bit(id, rsrc->bmap);
129 int rvu_alloc_rsrc_contig(struct rsrc_bmap *rsrc, int nrsrc)
136 start = bitmap_find_next_zero_area(rsrc->bmap, rsrc->max, 0, nrsrc, 0);
137 if (start >= rsrc->max)
140 bitmap_set(rsrc->bmap, start, nrsrc);
144 static void rvu_free_rsrc_contig(struct rsrc_bmap *rsrc, int nrsrc, int start)
148 if (start >= rsrc->max)
151 bitmap_clear(rsrc->bmap, start, nrsrc);
154 bool rvu_rsrc_check_contig(struct rsrc_bmap *rsrc, int nrsrc)
161 start = bitmap_find_next_zero_area(rsrc->bmap, rsrc->max, 0, nrsrc, 0);
162 if (start >= rsrc->max)
168 void rvu_free_rsrc(struct rsrc_bmap *rsrc, int id)
173 __clear_bit(id, rsrc->bmap);
176 int rvu_rsrc_free_count(struct rsrc_bmap *rsrc)
183 used = bitmap_weight(rsrc->bmap, rsrc->max);
184 return (rsrc->max - used);
187 bool is_rsrc_free(struct rsrc_bmap *rsrc, int id)
192 return !test_bit(id, rsrc->bmap);
195 int rvu_alloc_bitmap(struct rsrc_bmap *rsrc)
197 rsrc->bmap = kcalloc(BITS_TO_LONGS(rsrc->max),
198 sizeof(long), GFP_KERNEL);
204 /* Get block LF's HW index from a PF_FUNC's block slot number */
205 int rvu_get_lf(struct rvu *rvu, struct rvu_block *block, u16 pcifunc, u16 slot)
210 mutex_lock(&rvu->rsrc_lock);
211 for (lf = 0; lf < block->lf.max; lf++) {
212 if (block->fn_map[lf] == pcifunc) {
214 mutex_unlock(&rvu->rsrc_lock);
220 mutex_unlock(&rvu->rsrc_lock);
224 /* Convert BLOCK_TYPE_E to a BLOCK_ADDR_E.
225 * Some silicon variants of OcteonTX2 supports
226 * multiple blocks of same type.
228 * @pcifunc has to be zero when no LF is yet attached.
230 * For a pcifunc if LFs are attached from multiple blocks of same type, then
231 * return blkaddr of first encountered block.
233 int rvu_get_blkaddr(struct rvu *rvu, int blktype, u16 pcifunc)
235 int devnum, blkaddr = -ENODEV;
241 blkaddr = BLKADDR_NPC;
244 blkaddr = BLKADDR_NPA;
247 /* For now assume NIX0 */
249 blkaddr = BLKADDR_NIX0;
254 blkaddr = BLKADDR_SSO;
257 blkaddr = BLKADDR_SSOW;
260 blkaddr = BLKADDR_TIM;
263 /* For now assume CPT0 */
265 blkaddr = BLKADDR_CPT0;
271 /* Check if this is a RVU PF or VF */
272 if (pcifunc & RVU_PFVF_FUNC_MASK) {
274 devnum = rvu_get_hwvf(rvu, pcifunc);
277 devnum = rvu_get_pf(pcifunc);
280 /* Check if the 'pcifunc' has a NIX LF from 'BLKADDR_NIX0' or
283 if (blktype == BLKTYPE_NIX) {
284 reg = is_pf ? RVU_PRIV_PFX_NIXX_CFG(0) :
285 RVU_PRIV_HWVFX_NIXX_CFG(0);
286 cfg = rvu_read64(rvu, BLKADDR_RVUM, reg | (devnum << 16));
288 blkaddr = BLKADDR_NIX0;
292 reg = is_pf ? RVU_PRIV_PFX_NIXX_CFG(1) :
293 RVU_PRIV_HWVFX_NIXX_CFG(1);
294 cfg = rvu_read64(rvu, BLKADDR_RVUM, reg | (devnum << 16));
296 blkaddr = BLKADDR_NIX1;
299 if (blktype == BLKTYPE_CPT) {
300 reg = is_pf ? RVU_PRIV_PFX_CPTX_CFG(0) :
301 RVU_PRIV_HWVFX_CPTX_CFG(0);
302 cfg = rvu_read64(rvu, BLKADDR_RVUM, reg | (devnum << 16));
304 blkaddr = BLKADDR_CPT0;
308 reg = is_pf ? RVU_PRIV_PFX_CPTX_CFG(1) :
309 RVU_PRIV_HWVFX_CPTX_CFG(1);
310 cfg = rvu_read64(rvu, BLKADDR_RVUM, reg | (devnum << 16));
312 blkaddr = BLKADDR_CPT1;
316 if (is_block_implemented(rvu->hw, blkaddr))
321 static void rvu_update_rsrc_map(struct rvu *rvu, struct rvu_pfvf *pfvf,
322 struct rvu_block *block, u16 pcifunc,
325 int devnum, num_lfs = 0;
329 if (lf >= block->lf.max) {
330 dev_err(&rvu->pdev->dev,
331 "%s: FATAL: LF %d is >= %s's max lfs i.e %d\n",
332 __func__, lf, block->name, block->lf.max);
336 /* Check if this is for a RVU PF or VF */
337 if (pcifunc & RVU_PFVF_FUNC_MASK) {
339 devnum = rvu_get_hwvf(rvu, pcifunc);
342 devnum = rvu_get_pf(pcifunc);
345 block->fn_map[lf] = attach ? pcifunc : 0;
347 switch (block->addr) {
349 pfvf->npalf = attach ? true : false;
350 num_lfs = pfvf->npalf;
354 pfvf->nixlf = attach ? true : false;
355 num_lfs = pfvf->nixlf;
358 attach ? pfvf->sso++ : pfvf->sso--;
362 attach ? pfvf->ssow++ : pfvf->ssow--;
363 num_lfs = pfvf->ssow;
366 attach ? pfvf->timlfs++ : pfvf->timlfs--;
367 num_lfs = pfvf->timlfs;
370 attach ? pfvf->cptlfs++ : pfvf->cptlfs--;
371 num_lfs = pfvf->cptlfs;
374 attach ? pfvf->cpt1_lfs++ : pfvf->cpt1_lfs--;
375 num_lfs = pfvf->cpt1_lfs;
379 reg = is_pf ? block->pf_lfcnt_reg : block->vf_lfcnt_reg;
380 rvu_write64(rvu, BLKADDR_RVUM, reg | (devnum << 16), num_lfs);
383 inline int rvu_get_pf(u16 pcifunc)
385 return (pcifunc >> RVU_PFVF_PF_SHIFT) & RVU_PFVF_PF_MASK;
388 void rvu_get_pf_numvfs(struct rvu *rvu, int pf, int *numvfs, int *hwvf)
392 /* Get numVFs attached to this PF and first HWVF */
393 cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_PFX_CFG(pf));
394 *numvfs = (cfg >> 12) & 0xFF;
398 static int rvu_get_hwvf(struct rvu *rvu, int pcifunc)
403 pf = rvu_get_pf(pcifunc);
404 func = pcifunc & RVU_PFVF_FUNC_MASK;
406 /* Get first HWVF attached to this PF */
407 cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_PFX_CFG(pf));
409 return ((cfg & 0xFFF) + func - 1);
412 struct rvu_pfvf *rvu_get_pfvf(struct rvu *rvu, int pcifunc)
414 /* Check if it is a PF or VF */
415 if (pcifunc & RVU_PFVF_FUNC_MASK)
416 return &rvu->hwvf[rvu_get_hwvf(rvu, pcifunc)];
418 return &rvu->pf[rvu_get_pf(pcifunc)];
421 static bool is_pf_func_valid(struct rvu *rvu, u16 pcifunc)
426 pf = rvu_get_pf(pcifunc);
427 if (pf >= rvu->hw->total_pfs)
430 if (!(pcifunc & RVU_PFVF_FUNC_MASK))
433 /* Check if VF is within number of VFs attached to this PF */
434 vf = (pcifunc & RVU_PFVF_FUNC_MASK) - 1;
435 cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_PFX_CFG(pf));
436 nvfs = (cfg >> 12) & 0xFF;
443 bool is_block_implemented(struct rvu_hwinfo *hw, int blkaddr)
445 struct rvu_block *block;
447 if (blkaddr < BLKADDR_RVUM || blkaddr >= BLK_COUNT)
450 block = &hw->block[blkaddr];
451 return block->implemented;
454 static void rvu_check_block_implemented(struct rvu *rvu)
456 struct rvu_hwinfo *hw = rvu->hw;
457 struct rvu_block *block;
461 /* For each block check if 'implemented' bit is set */
462 for (blkid = 0; blkid < BLK_COUNT; blkid++) {
463 block = &hw->block[blkid];
464 cfg = rvupf_read64(rvu, RVU_PF_BLOCK_ADDRX_DISC(blkid));
465 if (cfg & BIT_ULL(11))
466 block->implemented = true;
470 static void rvu_setup_rvum_blk_revid(struct rvu *rvu)
472 rvu_write64(rvu, BLKADDR_RVUM,
473 RVU_PRIV_BLOCK_TYPEX_REV(BLKTYPE_RVUM),
477 static void rvu_clear_rvum_blk_revid(struct rvu *rvu)
479 rvu_write64(rvu, BLKADDR_RVUM,
480 RVU_PRIV_BLOCK_TYPEX_REV(BLKTYPE_RVUM), 0x00);
483 int rvu_lf_reset(struct rvu *rvu, struct rvu_block *block, int lf)
487 if (!block->implemented)
490 rvu_write64(rvu, block->addr, block->lfreset_reg, lf | BIT_ULL(12));
491 err = rvu_poll_reg(rvu, block->addr, block->lfreset_reg, BIT_ULL(12),
496 static void rvu_block_reset(struct rvu *rvu, int blkaddr, u64 rst_reg)
498 struct rvu_block *block = &rvu->hw->block[blkaddr];
500 if (!block->implemented)
503 rvu_write64(rvu, blkaddr, rst_reg, BIT_ULL(0));
504 rvu_poll_reg(rvu, blkaddr, rst_reg, BIT_ULL(63), true);
507 static void rvu_reset_all_blocks(struct rvu *rvu)
509 /* Do a HW reset of all RVU blocks */
510 rvu_block_reset(rvu, BLKADDR_NPA, NPA_AF_BLK_RST);
511 rvu_block_reset(rvu, BLKADDR_NIX0, NIX_AF_BLK_RST);
512 rvu_block_reset(rvu, BLKADDR_NIX1, NIX_AF_BLK_RST);
513 rvu_block_reset(rvu, BLKADDR_NPC, NPC_AF_BLK_RST);
514 rvu_block_reset(rvu, BLKADDR_SSO, SSO_AF_BLK_RST);
515 rvu_block_reset(rvu, BLKADDR_TIM, TIM_AF_BLK_RST);
516 rvu_block_reset(rvu, BLKADDR_CPT0, CPT_AF_BLK_RST);
517 rvu_block_reset(rvu, BLKADDR_CPT1, CPT_AF_BLK_RST);
518 rvu_block_reset(rvu, BLKADDR_NDC_NIX0_RX, NDC_AF_BLK_RST);
519 rvu_block_reset(rvu, BLKADDR_NDC_NIX0_TX, NDC_AF_BLK_RST);
520 rvu_block_reset(rvu, BLKADDR_NDC_NIX1_RX, NDC_AF_BLK_RST);
521 rvu_block_reset(rvu, BLKADDR_NDC_NIX1_TX, NDC_AF_BLK_RST);
522 rvu_block_reset(rvu, BLKADDR_NDC_NPA0, NDC_AF_BLK_RST);
525 static void rvu_scan_block(struct rvu *rvu, struct rvu_block *block)
527 struct rvu_pfvf *pfvf;
531 for (lf = 0; lf < block->lf.max; lf++) {
532 cfg = rvu_read64(rvu, block->addr,
533 block->lfcfg_reg | (lf << block->lfshift));
534 if (!(cfg & BIT_ULL(63)))
537 /* Set this resource as being used */
538 __set_bit(lf, block->lf.bmap);
540 /* Get, to whom this LF is attached */
541 pfvf = rvu_get_pfvf(rvu, (cfg >> 8) & 0xFFFF);
542 rvu_update_rsrc_map(rvu, pfvf, block,
543 (cfg >> 8) & 0xFFFF, lf, true);
545 /* Set start MSIX vector for this LF within this PF/VF */
546 rvu_set_msix_offset(rvu, pfvf, block, lf);
550 static void rvu_check_min_msix_vec(struct rvu *rvu, int nvecs, int pf, int vf)
559 "PF%d:VF%d is configured with zero msix vectors, %d\n",
566 min_vecs = RVU_AF_INT_VEC_CNT + RVU_PF_INT_VEC_CNT;
568 min_vecs = RVU_PF_INT_VEC_CNT;
570 if (!(nvecs < min_vecs))
573 "PF%d is configured with too few vectors, %d, min is %d\n",
574 pf, nvecs, min_vecs);
577 static int rvu_setup_msix_resources(struct rvu *rvu)
579 struct rvu_hwinfo *hw = rvu->hw;
580 int pf, vf, numvfs, hwvf, err;
581 int nvecs, offset, max_msix;
582 struct rvu_pfvf *pfvf;
586 for (pf = 0; pf < hw->total_pfs; pf++) {
587 cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_PFX_CFG(pf));
588 /* If PF is not enabled, nothing to do */
589 if (!((cfg >> 20) & 0x01))
592 rvu_get_pf_numvfs(rvu, pf, &numvfs, &hwvf);
595 /* Get num of MSIX vectors attached to this PF */
596 cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_PFX_MSIX_CFG(pf));
597 pfvf->msix.max = ((cfg >> 32) & 0xFFF) + 1;
598 rvu_check_min_msix_vec(rvu, pfvf->msix.max, pf, 0);
600 /* Alloc msix bitmap for this PF */
601 err = rvu_alloc_bitmap(&pfvf->msix);
605 /* Allocate memory for MSIX vector to RVU block LF mapping */
606 pfvf->msix_lfmap = devm_kcalloc(rvu->dev, pfvf->msix.max,
607 sizeof(u16), GFP_KERNEL);
608 if (!pfvf->msix_lfmap)
611 /* For PF0 (AF) firmware will set msix vector offsets for
612 * AF, block AF and PF0_INT vectors, so jump to VFs.
617 /* Set MSIX offset for PF's 'RVU_PF_INT_VEC' vectors.
618 * These are allocated on driver init and never freed,
619 * so no need to set 'msix_lfmap' for these.
621 cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_PFX_INT_CFG(pf));
622 nvecs = (cfg >> 12) & 0xFF;
624 offset = rvu_alloc_rsrc_contig(&pfvf->msix, nvecs);
625 rvu_write64(rvu, BLKADDR_RVUM,
626 RVU_PRIV_PFX_INT_CFG(pf), cfg | offset);
628 /* Alloc msix bitmap for VFs */
629 for (vf = 0; vf < numvfs; vf++) {
630 pfvf = &rvu->hwvf[hwvf + vf];
631 /* Get num of MSIX vectors attached to this VF */
632 cfg = rvu_read64(rvu, BLKADDR_RVUM,
633 RVU_PRIV_PFX_MSIX_CFG(pf));
634 pfvf->msix.max = (cfg & 0xFFF) + 1;
635 rvu_check_min_msix_vec(rvu, pfvf->msix.max, pf, vf + 1);
637 /* Alloc msix bitmap for this VF */
638 err = rvu_alloc_bitmap(&pfvf->msix);
643 devm_kcalloc(rvu->dev, pfvf->msix.max,
644 sizeof(u16), GFP_KERNEL);
645 if (!pfvf->msix_lfmap)
648 /* Set MSIX offset for HWVF's 'RVU_VF_INT_VEC' vectors.
649 * These are allocated on driver init and never freed,
650 * so no need to set 'msix_lfmap' for these.
652 cfg = rvu_read64(rvu, BLKADDR_RVUM,
653 RVU_PRIV_HWVFX_INT_CFG(hwvf + vf));
654 nvecs = (cfg >> 12) & 0xFF;
656 offset = rvu_alloc_rsrc_contig(&pfvf->msix, nvecs);
657 rvu_write64(rvu, BLKADDR_RVUM,
658 RVU_PRIV_HWVFX_INT_CFG(hwvf + vf),
663 /* HW interprets RVU_AF_MSIXTR_BASE address as an IOVA, hence
664 * create an IOMMU mapping for the physical address configured by
665 * firmware and reconfig RVU_AF_MSIXTR_BASE with IOVA.
667 cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_CONST);
668 max_msix = cfg & 0xFFFFF;
669 if (rvu->fwdata && rvu->fwdata->msixtr_base)
670 phy_addr = rvu->fwdata->msixtr_base;
672 phy_addr = rvu_read64(rvu, BLKADDR_RVUM, RVU_AF_MSIXTR_BASE);
674 iova = dma_map_resource(rvu->dev, phy_addr,
675 max_msix * PCI_MSIX_ENTRY_SIZE,
676 DMA_BIDIRECTIONAL, 0);
678 if (dma_mapping_error(rvu->dev, iova))
681 rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_MSIXTR_BASE, (u64)iova);
682 rvu->msix_base_iova = iova;
683 rvu->msixtr_base_phy = phy_addr;
688 static void rvu_reset_msix(struct rvu *rvu)
690 /* Restore msixtr base register */
691 rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_MSIXTR_BASE,
692 rvu->msixtr_base_phy);
695 static void rvu_free_hw_resources(struct rvu *rvu)
697 struct rvu_hwinfo *hw = rvu->hw;
698 struct rvu_block *block;
699 struct rvu_pfvf *pfvf;
703 rvu_npa_freemem(rvu);
704 rvu_npc_freemem(rvu);
705 rvu_nix_freemem(rvu);
707 /* Free block LF bitmaps */
708 for (id = 0; id < BLK_COUNT; id++) {
709 block = &hw->block[id];
710 kfree(block->lf.bmap);
713 /* Free MSIX bitmaps */
714 for (id = 0; id < hw->total_pfs; id++) {
716 kfree(pfvf->msix.bmap);
719 for (id = 0; id < hw->total_vfs; id++) {
720 pfvf = &rvu->hwvf[id];
721 kfree(pfvf->msix.bmap);
724 /* Unmap MSIX vector base IOVA mapping */
725 if (!rvu->msix_base_iova)
727 cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_CONST);
728 max_msix = cfg & 0xFFFFF;
729 dma_unmap_resource(rvu->dev, rvu->msix_base_iova,
730 max_msix * PCI_MSIX_ENTRY_SIZE,
731 DMA_BIDIRECTIONAL, 0);
734 mutex_destroy(&rvu->rsrc_lock);
737 static void rvu_setup_pfvf_macaddress(struct rvu *rvu)
739 struct rvu_hwinfo *hw = rvu->hw;
740 int pf, vf, numvfs, hwvf;
741 struct rvu_pfvf *pfvf;
744 for (pf = 0; pf < hw->total_pfs; pf++) {
745 /* For PF0(AF), Assign MAC address to only VFs (LBKVFs) */
749 if (!is_pf_cgxmapped(rvu, pf))
751 /* Assign MAC address to PF */
753 if (rvu->fwdata && pf < PF_MACNUM_MAX) {
754 mac = &rvu->fwdata->pf_macs[pf];
756 u64_to_ether_addr(*mac, pfvf->mac_addr);
758 eth_random_addr(pfvf->mac_addr);
760 eth_random_addr(pfvf->mac_addr);
762 ether_addr_copy(pfvf->default_mac, pfvf->mac_addr);
765 /* Assign MAC address to VFs*/
766 rvu_get_pf_numvfs(rvu, pf, &numvfs, &hwvf);
767 for (vf = 0; vf < numvfs; vf++, hwvf++) {
768 pfvf = &rvu->hwvf[hwvf];
769 if (rvu->fwdata && hwvf < VF_MACNUM_MAX) {
770 mac = &rvu->fwdata->vf_macs[hwvf];
772 u64_to_ether_addr(*mac, pfvf->mac_addr);
774 eth_random_addr(pfvf->mac_addr);
776 eth_random_addr(pfvf->mac_addr);
778 ether_addr_copy(pfvf->default_mac, pfvf->mac_addr);
783 static int rvu_fwdata_init(struct rvu *rvu)
788 /* Get firmware data base address */
789 err = cgx_get_fwdata_base(&fwdbase);
792 rvu->fwdata = ioremap_wc(fwdbase, sizeof(struct rvu_fwdata));
795 if (!is_rvu_fwdata_valid(rvu)) {
797 "Mismatch in 'fwdata' struct btw kernel and firmware\n");
798 iounmap(rvu->fwdata);
804 dev_info(rvu->dev, "Unable to fetch 'fwdata' from firmware\n");
808 static void rvu_fwdata_exit(struct rvu *rvu)
811 iounmap(rvu->fwdata);
814 static int rvu_setup_nix_hw_resource(struct rvu *rvu, int blkaddr)
816 struct rvu_hwinfo *hw = rvu->hw;
817 struct rvu_block *block;
821 /* Init NIX LF's bitmap */
822 block = &hw->block[blkaddr];
823 if (!block->implemented)
825 blkid = (blkaddr == BLKADDR_NIX0) ? 0 : 1;
826 cfg = rvu_read64(rvu, blkaddr, NIX_AF_CONST2);
827 block->lf.max = cfg & 0xFFF;
828 block->addr = blkaddr;
829 block->type = BLKTYPE_NIX;
831 block->lookup_reg = NIX_AF_RVU_LF_CFG_DEBUG;
832 block->pf_lfcnt_reg = RVU_PRIV_PFX_NIXX_CFG(blkid);
833 block->vf_lfcnt_reg = RVU_PRIV_HWVFX_NIXX_CFG(blkid);
834 block->lfcfg_reg = NIX_PRIV_LFX_CFG;
835 block->msixcfg_reg = NIX_PRIV_LFX_INT_CFG;
836 block->lfreset_reg = NIX_AF_LF_RST;
837 sprintf(block->name, "NIX%d", blkid);
838 rvu->nix_blkaddr[blkid] = blkaddr;
839 return rvu_alloc_bitmap(&block->lf);
842 static int rvu_setup_cpt_hw_resource(struct rvu *rvu, int blkaddr)
844 struct rvu_hwinfo *hw = rvu->hw;
845 struct rvu_block *block;
849 /* Init CPT LF's bitmap */
850 block = &hw->block[blkaddr];
851 if (!block->implemented)
853 blkid = (blkaddr == BLKADDR_CPT0) ? 0 : 1;
854 cfg = rvu_read64(rvu, blkaddr, CPT_AF_CONSTANTS0);
855 block->lf.max = cfg & 0xFF;
856 block->addr = blkaddr;
857 block->type = BLKTYPE_CPT;
858 block->multislot = true;
860 block->lookup_reg = CPT_AF_RVU_LF_CFG_DEBUG;
861 block->pf_lfcnt_reg = RVU_PRIV_PFX_CPTX_CFG(blkid);
862 block->vf_lfcnt_reg = RVU_PRIV_HWVFX_CPTX_CFG(blkid);
863 block->lfcfg_reg = CPT_PRIV_LFX_CFG;
864 block->msixcfg_reg = CPT_PRIV_LFX_INT_CFG;
865 block->lfreset_reg = CPT_AF_LF_RST;
866 sprintf(block->name, "CPT%d", blkid);
867 return rvu_alloc_bitmap(&block->lf);
870 static void rvu_get_lbk_bufsize(struct rvu *rvu)
872 struct pci_dev *pdev = NULL;
876 pdev = pci_get_device(PCI_VENDOR_ID_CAVIUM,
877 PCI_DEVID_OCTEONTX2_LBK, pdev);
881 base = pci_ioremap_bar(pdev, 0);
885 lbk_const = readq(base + LBK_CONST);
887 /* cache fifo size */
888 rvu->hw->lbk_bufsize = FIELD_GET(LBK_CONST_BUF_SIZE, lbk_const);
895 static int rvu_setup_hw_resources(struct rvu *rvu)
897 struct rvu_hwinfo *hw = rvu->hw;
898 struct rvu_block *block;
902 /* Get HW supported max RVU PF & VF count */
903 cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_CONST);
904 hw->total_pfs = (cfg >> 32) & 0xFF;
905 hw->total_vfs = (cfg >> 20) & 0xFFF;
906 hw->max_vfs_per_pf = (cfg >> 40) & 0xFF;
908 /* Init NPA LF's bitmap */
909 block = &hw->block[BLKADDR_NPA];
910 if (!block->implemented)
912 cfg = rvu_read64(rvu, BLKADDR_NPA, NPA_AF_CONST);
913 block->lf.max = (cfg >> 16) & 0xFFF;
914 block->addr = BLKADDR_NPA;
915 block->type = BLKTYPE_NPA;
917 block->lookup_reg = NPA_AF_RVU_LF_CFG_DEBUG;
918 block->pf_lfcnt_reg = RVU_PRIV_PFX_NPA_CFG;
919 block->vf_lfcnt_reg = RVU_PRIV_HWVFX_NPA_CFG;
920 block->lfcfg_reg = NPA_PRIV_LFX_CFG;
921 block->msixcfg_reg = NPA_PRIV_LFX_INT_CFG;
922 block->lfreset_reg = NPA_AF_LF_RST;
923 sprintf(block->name, "NPA");
924 err = rvu_alloc_bitmap(&block->lf);
929 err = rvu_setup_nix_hw_resource(rvu, BLKADDR_NIX0);
932 err = rvu_setup_nix_hw_resource(rvu, BLKADDR_NIX1);
936 /* Init SSO group's bitmap */
937 block = &hw->block[BLKADDR_SSO];
938 if (!block->implemented)
940 cfg = rvu_read64(rvu, BLKADDR_SSO, SSO_AF_CONST);
941 block->lf.max = cfg & 0xFFFF;
942 block->addr = BLKADDR_SSO;
943 block->type = BLKTYPE_SSO;
944 block->multislot = true;
946 block->lookup_reg = SSO_AF_RVU_LF_CFG_DEBUG;
947 block->pf_lfcnt_reg = RVU_PRIV_PFX_SSO_CFG;
948 block->vf_lfcnt_reg = RVU_PRIV_HWVFX_SSO_CFG;
949 block->lfcfg_reg = SSO_PRIV_LFX_HWGRP_CFG;
950 block->msixcfg_reg = SSO_PRIV_LFX_HWGRP_INT_CFG;
951 block->lfreset_reg = SSO_AF_LF_HWGRP_RST;
952 sprintf(block->name, "SSO GROUP");
953 err = rvu_alloc_bitmap(&block->lf);
958 /* Init SSO workslot's bitmap */
959 block = &hw->block[BLKADDR_SSOW];
960 if (!block->implemented)
962 block->lf.max = (cfg >> 56) & 0xFF;
963 block->addr = BLKADDR_SSOW;
964 block->type = BLKTYPE_SSOW;
965 block->multislot = true;
967 block->lookup_reg = SSOW_AF_RVU_LF_HWS_CFG_DEBUG;
968 block->pf_lfcnt_reg = RVU_PRIV_PFX_SSOW_CFG;
969 block->vf_lfcnt_reg = RVU_PRIV_HWVFX_SSOW_CFG;
970 block->lfcfg_reg = SSOW_PRIV_LFX_HWS_CFG;
971 block->msixcfg_reg = SSOW_PRIV_LFX_HWS_INT_CFG;
972 block->lfreset_reg = SSOW_AF_LF_HWS_RST;
973 sprintf(block->name, "SSOWS");
974 err = rvu_alloc_bitmap(&block->lf);
979 /* Init TIM LF's bitmap */
980 block = &hw->block[BLKADDR_TIM];
981 if (!block->implemented)
983 cfg = rvu_read64(rvu, BLKADDR_TIM, TIM_AF_CONST);
984 block->lf.max = cfg & 0xFFFF;
985 block->addr = BLKADDR_TIM;
986 block->type = BLKTYPE_TIM;
987 block->multislot = true;
989 block->lookup_reg = TIM_AF_RVU_LF_CFG_DEBUG;
990 block->pf_lfcnt_reg = RVU_PRIV_PFX_TIM_CFG;
991 block->vf_lfcnt_reg = RVU_PRIV_HWVFX_TIM_CFG;
992 block->lfcfg_reg = TIM_PRIV_LFX_CFG;
993 block->msixcfg_reg = TIM_PRIV_LFX_INT_CFG;
994 block->lfreset_reg = TIM_AF_LF_RST;
995 sprintf(block->name, "TIM");
996 err = rvu_alloc_bitmap(&block->lf);
1001 err = rvu_setup_cpt_hw_resource(rvu, BLKADDR_CPT0);
1004 err = rvu_setup_cpt_hw_resource(rvu, BLKADDR_CPT1);
1008 /* Allocate memory for PFVF data */
1009 rvu->pf = devm_kcalloc(rvu->dev, hw->total_pfs,
1010 sizeof(struct rvu_pfvf), GFP_KERNEL);
1014 rvu->hwvf = devm_kcalloc(rvu->dev, hw->total_vfs,
1015 sizeof(struct rvu_pfvf), GFP_KERNEL);
1019 mutex_init(&rvu->rsrc_lock);
1021 rvu_fwdata_init(rvu);
1023 err = rvu_setup_msix_resources(rvu);
1027 for (blkid = 0; blkid < BLK_COUNT; blkid++) {
1028 block = &hw->block[blkid];
1029 if (!block->lf.bmap)
1032 /* Allocate memory for block LF/slot to pcifunc mapping info */
1033 block->fn_map = devm_kcalloc(rvu->dev, block->lf.max,
1034 sizeof(u16), GFP_KERNEL);
1035 if (!block->fn_map) {
1040 /* Scan all blocks to check if low level firmware has
1041 * already provisioned any of the resources to a PF/VF.
1043 rvu_scan_block(rvu, block);
1046 err = rvu_set_channels_base(rvu);
1050 err = rvu_npc_init(rvu);
1054 err = rvu_cgx_init(rvu);
1058 /* Assign MACs for CGX mapped functions */
1059 rvu_setup_pfvf_macaddress(rvu);
1061 err = rvu_npa_init(rvu);
1065 rvu_get_lbk_bufsize(rvu);
1067 err = rvu_nix_init(rvu);
1071 rvu_program_channels(rvu);
1076 rvu_nix_freemem(rvu);
1078 rvu_npa_freemem(rvu);
1082 rvu_npc_freemem(rvu);
1083 rvu_fwdata_exit(rvu);
1085 rvu_reset_msix(rvu);
1089 /* NPA and NIX admin queue APIs */
1090 void rvu_aq_free(struct rvu *rvu, struct admin_queue *aq)
1095 qmem_free(rvu->dev, aq->inst);
1096 qmem_free(rvu->dev, aq->res);
1097 devm_kfree(rvu->dev, aq);
1100 int rvu_aq_alloc(struct rvu *rvu, struct admin_queue **ad_queue,
1101 int qsize, int inst_size, int res_size)
1103 struct admin_queue *aq;
1106 *ad_queue = devm_kzalloc(rvu->dev, sizeof(*aq), GFP_KERNEL);
1111 /* Alloc memory for instructions i.e AQ */
1112 err = qmem_alloc(rvu->dev, &aq->inst, qsize, inst_size);
1114 devm_kfree(rvu->dev, aq);
1118 /* Alloc memory for results */
1119 err = qmem_alloc(rvu->dev, &aq->res, qsize, res_size);
1121 rvu_aq_free(rvu, aq);
1125 spin_lock_init(&aq->lock);
1129 int rvu_mbox_handler_ready(struct rvu *rvu, struct msg_req *req,
1130 struct ready_msg_rsp *rsp)
1133 rsp->rclk_freq = rvu->fwdata->rclk;
1134 rsp->sclk_freq = rvu->fwdata->sclk;
1139 /* Get current count of a RVU block's LF/slots
1140 * provisioned to a given RVU func.
1142 u16 rvu_get_rsrc_mapcount(struct rvu_pfvf *pfvf, int blkaddr)
1146 return pfvf->npalf ? 1 : 0;
1149 return pfvf->nixlf ? 1 : 0;
1155 return pfvf->timlfs;
1157 return pfvf->cptlfs;
1159 return pfvf->cpt1_lfs;
1164 /* Return true if LFs of block type are attached to pcifunc */
1165 static bool is_blktype_attached(struct rvu_pfvf *pfvf, int blktype)
1169 return pfvf->npalf ? 1 : 0;
1171 return pfvf->nixlf ? 1 : 0;
1175 return !!pfvf->ssow;
1177 return !!pfvf->timlfs;
1179 return pfvf->cptlfs || pfvf->cpt1_lfs;
1185 bool is_pffunc_map_valid(struct rvu *rvu, u16 pcifunc, int blktype)
1187 struct rvu_pfvf *pfvf;
1189 if (!is_pf_func_valid(rvu, pcifunc))
1192 pfvf = rvu_get_pfvf(rvu, pcifunc);
1194 /* Check if this PFFUNC has a LF of type blktype attached */
1195 if (!is_blktype_attached(pfvf, blktype))
1201 static int rvu_lookup_rsrc(struct rvu *rvu, struct rvu_block *block,
1202 int pcifunc, int slot)
1206 val = ((u64)pcifunc << 24) | (slot << 16) | (1ULL << 13);
1207 rvu_write64(rvu, block->addr, block->lookup_reg, val);
1208 /* Wait for the lookup to finish */
1209 /* TODO: put some timeout here */
1210 while (rvu_read64(rvu, block->addr, block->lookup_reg) & (1ULL << 13))
1213 val = rvu_read64(rvu, block->addr, block->lookup_reg);
1215 /* Check LF valid bit */
1216 if (!(val & (1ULL << 12)))
1219 return (val & 0xFFF);
1222 static void rvu_detach_block(struct rvu *rvu, int pcifunc, int blktype)
1224 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
1225 struct rvu_hwinfo *hw = rvu->hw;
1226 struct rvu_block *block;
1227 int slot, lf, num_lfs;
1230 blkaddr = rvu_get_blkaddr(rvu, blktype, pcifunc);
1234 if (blktype == BLKTYPE_NIX)
1235 rvu_nix_reset_mac(pfvf, pcifunc);
1237 block = &hw->block[blkaddr];
1239 num_lfs = rvu_get_rsrc_mapcount(pfvf, block->addr);
1243 for (slot = 0; slot < num_lfs; slot++) {
1244 lf = rvu_lookup_rsrc(rvu, block, pcifunc, slot);
1245 if (lf < 0) /* This should never happen */
1248 /* Disable the LF */
1249 rvu_write64(rvu, blkaddr, block->lfcfg_reg |
1250 (lf << block->lfshift), 0x00ULL);
1252 /* Update SW maintained mapping info as well */
1253 rvu_update_rsrc_map(rvu, pfvf, block,
1254 pcifunc, lf, false);
1256 /* Free the resource */
1257 rvu_free_rsrc(&block->lf, lf);
1259 /* Clear MSIX vector offset for this LF */
1260 rvu_clear_msix_offset(rvu, pfvf, block, lf);
1264 static int rvu_detach_rsrcs(struct rvu *rvu, struct rsrc_detach *detach,
1267 struct rvu_hwinfo *hw = rvu->hw;
1268 bool detach_all = true;
1269 struct rvu_block *block;
1272 mutex_lock(&rvu->rsrc_lock);
1274 /* Check for partial resource detach */
1275 if (detach && detach->partial)
1278 /* Check for RVU block's LFs attached to this func,
1279 * if so, detach them.
1281 for (blkid = 0; blkid < BLK_COUNT; blkid++) {
1282 block = &hw->block[blkid];
1283 if (!block->lf.bmap)
1285 if (!detach_all && detach) {
1286 if (blkid == BLKADDR_NPA && !detach->npalf)
1288 else if ((blkid == BLKADDR_NIX0) && !detach->nixlf)
1290 else if ((blkid == BLKADDR_NIX1) && !detach->nixlf)
1292 else if ((blkid == BLKADDR_SSO) && !detach->sso)
1294 else if ((blkid == BLKADDR_SSOW) && !detach->ssow)
1296 else if ((blkid == BLKADDR_TIM) && !detach->timlfs)
1298 else if ((blkid == BLKADDR_CPT0) && !detach->cptlfs)
1300 else if ((blkid == BLKADDR_CPT1) && !detach->cptlfs)
1303 rvu_detach_block(rvu, pcifunc, block->type);
1306 mutex_unlock(&rvu->rsrc_lock);
1310 int rvu_mbox_handler_detach_resources(struct rvu *rvu,
1311 struct rsrc_detach *detach,
1312 struct msg_rsp *rsp)
1314 return rvu_detach_rsrcs(rvu, detach, detach->hdr.pcifunc);
1317 static int rvu_get_nix_blkaddr(struct rvu *rvu, u16 pcifunc)
1319 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
1320 int blkaddr = BLKADDR_NIX0, vf;
1321 struct rvu_pfvf *pf;
1323 /* All CGX mapped PFs are set with assigned NIX block during init */
1324 if (is_pf_cgxmapped(rvu, rvu_get_pf(pcifunc))) {
1325 pf = rvu_get_pfvf(rvu, pcifunc & ~RVU_PFVF_FUNC_MASK);
1326 blkaddr = pf->nix_blkaddr;
1327 } else if (is_afvf(pcifunc)) {
1329 /* Assign NIX based on VF number. All even numbered VFs get
1330 * NIX0 and odd numbered gets NIX1
1332 blkaddr = (vf & 1) ? BLKADDR_NIX1 : BLKADDR_NIX0;
1333 /* NIX1 is not present on all silicons */
1334 if (!is_block_implemented(rvu->hw, BLKADDR_NIX1))
1335 blkaddr = BLKADDR_NIX0;
1340 pfvf->nix_blkaddr = BLKADDR_NIX1;
1341 pfvf->nix_rx_intf = NIX_INTFX_RX(1);
1342 pfvf->nix_tx_intf = NIX_INTFX_TX(1);
1346 pfvf->nix_blkaddr = BLKADDR_NIX0;
1347 pfvf->nix_rx_intf = NIX_INTFX_RX(0);
1348 pfvf->nix_tx_intf = NIX_INTFX_TX(0);
1352 return pfvf->nix_blkaddr;
1355 static int rvu_get_attach_blkaddr(struct rvu *rvu, int blktype,
1356 u16 pcifunc, struct rsrc_attach *attach)
1362 blkaddr = rvu_get_nix_blkaddr(rvu, pcifunc);
1365 if (attach->hdr.ver < RVU_MULTI_BLK_VER)
1366 return rvu_get_blkaddr(rvu, blktype, 0);
1367 blkaddr = attach->cpt_blkaddr ? attach->cpt_blkaddr :
1369 if (blkaddr != BLKADDR_CPT0 && blkaddr != BLKADDR_CPT1)
1373 return rvu_get_blkaddr(rvu, blktype, 0);
1376 if (is_block_implemented(rvu->hw, blkaddr))
1382 static void rvu_attach_block(struct rvu *rvu, int pcifunc, int blktype,
1383 int num_lfs, struct rsrc_attach *attach)
1385 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
1386 struct rvu_hwinfo *hw = rvu->hw;
1387 struct rvu_block *block;
1395 blkaddr = rvu_get_attach_blkaddr(rvu, blktype, pcifunc, attach);
1399 block = &hw->block[blkaddr];
1400 if (!block->lf.bmap)
1403 for (slot = 0; slot < num_lfs; slot++) {
1404 /* Allocate the resource */
1405 lf = rvu_alloc_rsrc(&block->lf);
1409 cfg = (1ULL << 63) | (pcifunc << 8) | slot;
1410 rvu_write64(rvu, blkaddr, block->lfcfg_reg |
1411 (lf << block->lfshift), cfg);
1412 rvu_update_rsrc_map(rvu, pfvf, block,
1415 /* Set start MSIX vector for this LF within this PF/VF */
1416 rvu_set_msix_offset(rvu, pfvf, block, lf);
1420 static int rvu_check_rsrc_availability(struct rvu *rvu,
1421 struct rsrc_attach *req, u16 pcifunc)
1423 struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
1424 int free_lfs, mappedlfs, blkaddr;
1425 struct rvu_hwinfo *hw = rvu->hw;
1426 struct rvu_block *block;
1428 /* Only one NPA LF can be attached */
1429 if (req->npalf && !is_blktype_attached(pfvf, BLKTYPE_NPA)) {
1430 block = &hw->block[BLKADDR_NPA];
1431 free_lfs = rvu_rsrc_free_count(&block->lf);
1434 } else if (req->npalf) {
1435 dev_err(&rvu->pdev->dev,
1436 "Func 0x%x: Invalid req, already has NPA\n",
1441 /* Only one NIX LF can be attached */
1442 if (req->nixlf && !is_blktype_attached(pfvf, BLKTYPE_NIX)) {
1443 blkaddr = rvu_get_attach_blkaddr(rvu, BLKTYPE_NIX,
1447 block = &hw->block[blkaddr];
1448 free_lfs = rvu_rsrc_free_count(&block->lf);
1451 } else if (req->nixlf) {
1452 dev_err(&rvu->pdev->dev,
1453 "Func 0x%x: Invalid req, already has NIX\n",
1459 block = &hw->block[BLKADDR_SSO];
1460 /* Is request within limits ? */
1461 if (req->sso > block->lf.max) {
1462 dev_err(&rvu->pdev->dev,
1463 "Func 0x%x: Invalid SSO req, %d > max %d\n",
1464 pcifunc, req->sso, block->lf.max);
1467 mappedlfs = rvu_get_rsrc_mapcount(pfvf, block->addr);
1468 free_lfs = rvu_rsrc_free_count(&block->lf);
1469 /* Check if additional resources are available */
1470 if (req->sso > mappedlfs &&
1471 ((req->sso - mappedlfs) > free_lfs))
1476 block = &hw->block[BLKADDR_SSOW];
1477 if (req->ssow > block->lf.max) {
1478 dev_err(&rvu->pdev->dev,
1479 "Func 0x%x: Invalid SSOW req, %d > max %d\n",
1480 pcifunc, req->sso, block->lf.max);
1483 mappedlfs = rvu_get_rsrc_mapcount(pfvf, block->addr);
1484 free_lfs = rvu_rsrc_free_count(&block->lf);
1485 if (req->ssow > mappedlfs &&
1486 ((req->ssow - mappedlfs) > free_lfs))
1491 block = &hw->block[BLKADDR_TIM];
1492 if (req->timlfs > block->lf.max) {
1493 dev_err(&rvu->pdev->dev,
1494 "Func 0x%x: Invalid TIMLF req, %d > max %d\n",
1495 pcifunc, req->timlfs, block->lf.max);
1498 mappedlfs = rvu_get_rsrc_mapcount(pfvf, block->addr);
1499 free_lfs = rvu_rsrc_free_count(&block->lf);
1500 if (req->timlfs > mappedlfs &&
1501 ((req->timlfs - mappedlfs) > free_lfs))
1506 blkaddr = rvu_get_attach_blkaddr(rvu, BLKTYPE_CPT,
1510 block = &hw->block[blkaddr];
1511 if (req->cptlfs > block->lf.max) {
1512 dev_err(&rvu->pdev->dev,
1513 "Func 0x%x: Invalid CPTLF req, %d > max %d\n",
1514 pcifunc, req->cptlfs, block->lf.max);
1517 mappedlfs = rvu_get_rsrc_mapcount(pfvf, block->addr);
1518 free_lfs = rvu_rsrc_free_count(&block->lf);
1519 if (req->cptlfs > mappedlfs &&
1520 ((req->cptlfs - mappedlfs) > free_lfs))
1527 dev_info(rvu->dev, "Request for %s failed\n", block->name);
1531 static bool rvu_attach_from_same_block(struct rvu *rvu, int blktype,
1532 struct rsrc_attach *attach)
1534 int blkaddr, num_lfs;
1536 blkaddr = rvu_get_attach_blkaddr(rvu, blktype,
1537 attach->hdr.pcifunc, attach);
1541 num_lfs = rvu_get_rsrc_mapcount(rvu_get_pfvf(rvu, attach->hdr.pcifunc),
1543 /* Requester already has LFs from given block ? */
1547 int rvu_mbox_handler_attach_resources(struct rvu *rvu,
1548 struct rsrc_attach *attach,
1549 struct msg_rsp *rsp)
1551 u16 pcifunc = attach->hdr.pcifunc;
1554 /* If first request, detach all existing attached resources */
1555 if (!attach->modify)
1556 rvu_detach_rsrcs(rvu, NULL, pcifunc);
1558 mutex_lock(&rvu->rsrc_lock);
1560 /* Check if the request can be accommodated */
1561 err = rvu_check_rsrc_availability(rvu, attach, pcifunc);
1565 /* Now attach the requested resources */
1567 rvu_attach_block(rvu, pcifunc, BLKTYPE_NPA, 1, attach);
1570 rvu_attach_block(rvu, pcifunc, BLKTYPE_NIX, 1, attach);
1573 /* RVU func doesn't know which exact LF or slot is attached
1574 * to it, it always sees as slot 0,1,2. So for a 'modify'
1575 * request, simply detach all existing attached LFs/slots
1576 * and attach a fresh.
1579 rvu_detach_block(rvu, pcifunc, BLKTYPE_SSO);
1580 rvu_attach_block(rvu, pcifunc, BLKTYPE_SSO,
1581 attach->sso, attach);
1586 rvu_detach_block(rvu, pcifunc, BLKTYPE_SSOW);
1587 rvu_attach_block(rvu, pcifunc, BLKTYPE_SSOW,
1588 attach->ssow, attach);
1591 if (attach->timlfs) {
1593 rvu_detach_block(rvu, pcifunc, BLKTYPE_TIM);
1594 rvu_attach_block(rvu, pcifunc, BLKTYPE_TIM,
1595 attach->timlfs, attach);
1598 if (attach->cptlfs) {
1599 if (attach->modify &&
1600 rvu_attach_from_same_block(rvu, BLKTYPE_CPT, attach))
1601 rvu_detach_block(rvu, pcifunc, BLKTYPE_CPT);
1602 rvu_attach_block(rvu, pcifunc, BLKTYPE_CPT,
1603 attach->cptlfs, attach);
1607 mutex_unlock(&rvu->rsrc_lock);
1611 static u16 rvu_get_msix_offset(struct rvu *rvu, struct rvu_pfvf *pfvf,
1612 int blkaddr, int lf)
1617 return MSIX_VECTOR_INVALID;
1619 for (vec = 0; vec < pfvf->msix.max; vec++) {
1620 if (pfvf->msix_lfmap[vec] == MSIX_BLKLF(blkaddr, lf))
1623 return MSIX_VECTOR_INVALID;
1626 static void rvu_set_msix_offset(struct rvu *rvu, struct rvu_pfvf *pfvf,
1627 struct rvu_block *block, int lf)
1629 u16 nvecs, vec, offset;
1632 cfg = rvu_read64(rvu, block->addr, block->msixcfg_reg |
1633 (lf << block->lfshift));
1634 nvecs = (cfg >> 12) & 0xFF;
1636 /* Check and alloc MSIX vectors, must be contiguous */
1637 if (!rvu_rsrc_check_contig(&pfvf->msix, nvecs))
1640 offset = rvu_alloc_rsrc_contig(&pfvf->msix, nvecs);
1642 /* Config MSIX offset in LF */
1643 rvu_write64(rvu, block->addr, block->msixcfg_reg |
1644 (lf << block->lfshift), (cfg & ~0x7FFULL) | offset);
1646 /* Update the bitmap as well */
1647 for (vec = 0; vec < nvecs; vec++)
1648 pfvf->msix_lfmap[offset + vec] = MSIX_BLKLF(block->addr, lf);
1651 static void rvu_clear_msix_offset(struct rvu *rvu, struct rvu_pfvf *pfvf,
1652 struct rvu_block *block, int lf)
1654 u16 nvecs, vec, offset;
1657 cfg = rvu_read64(rvu, block->addr, block->msixcfg_reg |
1658 (lf << block->lfshift));
1659 nvecs = (cfg >> 12) & 0xFF;
1661 /* Clear MSIX offset in LF */
1662 rvu_write64(rvu, block->addr, block->msixcfg_reg |
1663 (lf << block->lfshift), cfg & ~0x7FFULL);
1665 offset = rvu_get_msix_offset(rvu, pfvf, block->addr, lf);
1667 /* Update the mapping */
1668 for (vec = 0; vec < nvecs; vec++)
1669 pfvf->msix_lfmap[offset + vec] = 0;
1671 /* Free the same in MSIX bitmap */
1672 rvu_free_rsrc_contig(&pfvf->msix, nvecs, offset);
1675 int rvu_mbox_handler_msix_offset(struct rvu *rvu, struct msg_req *req,
1676 struct msix_offset_rsp *rsp)
1678 struct rvu_hwinfo *hw = rvu->hw;
1679 u16 pcifunc = req->hdr.pcifunc;
1680 struct rvu_pfvf *pfvf;
1681 int lf, slot, blkaddr;
1683 pfvf = rvu_get_pfvf(rvu, pcifunc);
1684 if (!pfvf->msix.bmap)
1687 /* Set MSIX offsets for each block's LFs attached to this PF/VF */
1688 lf = rvu_get_lf(rvu, &hw->block[BLKADDR_NPA], pcifunc, 0);
1689 rsp->npa_msixoff = rvu_get_msix_offset(rvu, pfvf, BLKADDR_NPA, lf);
1691 /* Get BLKADDR from which LFs are attached to pcifunc */
1692 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
1694 rsp->nix_msixoff = MSIX_VECTOR_INVALID;
1696 lf = rvu_get_lf(rvu, &hw->block[blkaddr], pcifunc, 0);
1697 rsp->nix_msixoff = rvu_get_msix_offset(rvu, pfvf, blkaddr, lf);
1700 rsp->sso = pfvf->sso;
1701 for (slot = 0; slot < rsp->sso; slot++) {
1702 lf = rvu_get_lf(rvu, &hw->block[BLKADDR_SSO], pcifunc, slot);
1703 rsp->sso_msixoff[slot] =
1704 rvu_get_msix_offset(rvu, pfvf, BLKADDR_SSO, lf);
1707 rsp->ssow = pfvf->ssow;
1708 for (slot = 0; slot < rsp->ssow; slot++) {
1709 lf = rvu_get_lf(rvu, &hw->block[BLKADDR_SSOW], pcifunc, slot);
1710 rsp->ssow_msixoff[slot] =
1711 rvu_get_msix_offset(rvu, pfvf, BLKADDR_SSOW, lf);
1714 rsp->timlfs = pfvf->timlfs;
1715 for (slot = 0; slot < rsp->timlfs; slot++) {
1716 lf = rvu_get_lf(rvu, &hw->block[BLKADDR_TIM], pcifunc, slot);
1717 rsp->timlf_msixoff[slot] =
1718 rvu_get_msix_offset(rvu, pfvf, BLKADDR_TIM, lf);
1721 rsp->cptlfs = pfvf->cptlfs;
1722 for (slot = 0; slot < rsp->cptlfs; slot++) {
1723 lf = rvu_get_lf(rvu, &hw->block[BLKADDR_CPT0], pcifunc, slot);
1724 rsp->cptlf_msixoff[slot] =
1725 rvu_get_msix_offset(rvu, pfvf, BLKADDR_CPT0, lf);
1728 rsp->cpt1_lfs = pfvf->cpt1_lfs;
1729 for (slot = 0; slot < rsp->cpt1_lfs; slot++) {
1730 lf = rvu_get_lf(rvu, &hw->block[BLKADDR_CPT1], pcifunc, slot);
1731 rsp->cpt1_lf_msixoff[slot] =
1732 rvu_get_msix_offset(rvu, pfvf, BLKADDR_CPT1, lf);
1738 int rvu_mbox_handler_vf_flr(struct rvu *rvu, struct msg_req *req,
1739 struct msg_rsp *rsp)
1741 u16 pcifunc = req->hdr.pcifunc;
1745 vf = pcifunc & RVU_PFVF_FUNC_MASK;
1746 cfg = rvu_read64(rvu, BLKADDR_RVUM,
1747 RVU_PRIV_PFX_CFG(rvu_get_pf(pcifunc)));
1748 numvfs = (cfg >> 12) & 0xFF;
1750 if (vf && vf <= numvfs)
1751 __rvu_flr_handler(rvu, pcifunc);
1753 return RVU_INVALID_VF_ID;
1758 int rvu_mbox_handler_get_hw_cap(struct rvu *rvu, struct msg_req *req,
1759 struct get_hw_cap_rsp *rsp)
1761 struct rvu_hwinfo *hw = rvu->hw;
1763 rsp->nix_fixed_txschq_mapping = hw->cap.nix_fixed_txschq_mapping;
1764 rsp->nix_shaping = hw->cap.nix_shaping;
1769 int rvu_mbox_handler_set_vf_perm(struct rvu *rvu, struct set_vf_perm *req,
1770 struct msg_rsp *rsp)
1772 struct rvu_hwinfo *hw = rvu->hw;
1773 u16 pcifunc = req->hdr.pcifunc;
1774 struct rvu_pfvf *pfvf;
1778 /* Only PF can add VF permissions */
1779 if ((pcifunc & RVU_PFVF_FUNC_MASK) || is_afvf(pcifunc))
1782 target = (pcifunc & ~RVU_PFVF_FUNC_MASK) | (req->vf + 1);
1783 pfvf = rvu_get_pfvf(rvu, target);
1785 if (req->flags & RESET_VF_PERM) {
1786 pfvf->flags &= RVU_CLEAR_VF_PERM;
1787 } else if (test_bit(PF_SET_VF_TRUSTED, &pfvf->flags) ^
1788 (req->flags & VF_TRUSTED)) {
1789 change_bit(PF_SET_VF_TRUSTED, &pfvf->flags);
1790 /* disable multicast and promisc entries */
1791 if (!test_bit(PF_SET_VF_TRUSTED, &pfvf->flags)) {
1792 blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, target);
1795 nixlf = rvu_get_lf(rvu, &hw->block[blkaddr],
1799 npc_enadis_default_mce_entry(rvu, target, nixlf,
1800 NIXLF_ALLMULTI_ENTRY,
1802 npc_enadis_default_mce_entry(rvu, target, nixlf,
1803 NIXLF_PROMISC_ENTRY,
1811 static int rvu_process_mbox_msg(struct otx2_mbox *mbox, int devid,
1812 struct mbox_msghdr *req)
1814 struct rvu *rvu = pci_get_drvdata(mbox->pdev);
1816 /* Check if valid, if not reply with a invalid msg */
1817 if (req->sig != OTX2_MBOX_REQ_SIG)
1821 #define M(_name, _id, _fn_name, _req_type, _rsp_type) \
1823 struct _rsp_type *rsp; \
1826 rsp = (struct _rsp_type *)otx2_mbox_alloc_msg( \
1828 sizeof(struct _rsp_type)); \
1829 /* some handlers should complete even if reply */ \
1830 /* could not be allocated */ \
1832 _id != MBOX_MSG_DETACH_RESOURCES && \
1833 _id != MBOX_MSG_NIX_TXSCH_FREE && \
1834 _id != MBOX_MSG_VF_FLR) \
1837 rsp->hdr.id = _id; \
1838 rsp->hdr.sig = OTX2_MBOX_RSP_SIG; \
1839 rsp->hdr.pcifunc = req->pcifunc; \
1843 err = rvu_mbox_handler_ ## _fn_name(rvu, \
1844 (struct _req_type *)req, \
1847 rsp->hdr.rc = err; \
1849 trace_otx2_msg_process(mbox->pdev, _id, err); \
1850 return rsp ? err : -ENOMEM; \
1857 otx2_reply_invalid_msg(mbox, devid, req->pcifunc, req->id);
1862 static void __rvu_mbox_handler(struct rvu_work *mwork, int type)
1864 struct rvu *rvu = mwork->rvu;
1865 int offset, err, id, devid;
1866 struct otx2_mbox_dev *mdev;
1867 struct mbox_hdr *req_hdr;
1868 struct mbox_msghdr *msg;
1869 struct mbox_wq_info *mw;
1870 struct otx2_mbox *mbox;
1874 mw = &rvu->afpf_wq_info;
1877 mw = &rvu->afvf_wq_info;
1883 devid = mwork - mw->mbox_wrk;
1885 mdev = &mbox->dev[devid];
1887 /* Process received mbox messages */
1888 req_hdr = mdev->mbase + mbox->rx_start;
1889 if (mw->mbox_wrk[devid].num_msgs == 0)
1892 offset = mbox->rx_start + ALIGN(sizeof(*req_hdr), MBOX_MSG_ALIGN);
1894 for (id = 0; id < mw->mbox_wrk[devid].num_msgs; id++) {
1895 msg = mdev->mbase + offset;
1897 /* Set which PF/VF sent this message based on mbox IRQ */
1901 ~(RVU_PFVF_PF_MASK << RVU_PFVF_PF_SHIFT);
1902 msg->pcifunc |= (devid << RVU_PFVF_PF_SHIFT);
1906 ~(RVU_PFVF_FUNC_MASK << RVU_PFVF_FUNC_SHIFT);
1907 msg->pcifunc |= (devid << RVU_PFVF_FUNC_SHIFT) + 1;
1911 err = rvu_process_mbox_msg(mbox, devid, msg);
1913 offset = mbox->rx_start + msg->next_msgoff;
1917 if (msg->pcifunc & RVU_PFVF_FUNC_MASK)
1918 dev_warn(rvu->dev, "Error %d when processing message %s (0x%x) from PF%d:VF%d\n",
1919 err, otx2_mbox_id2name(msg->id),
1920 msg->id, rvu_get_pf(msg->pcifunc),
1921 (msg->pcifunc & RVU_PFVF_FUNC_MASK) - 1);
1923 dev_warn(rvu->dev, "Error %d when processing message %s (0x%x) from PF%d\n",
1924 err, otx2_mbox_id2name(msg->id),
1927 mw->mbox_wrk[devid].num_msgs = 0;
1929 /* Send mbox responses to VF/PF */
1930 otx2_mbox_msg_send(mbox, devid);
1933 static inline void rvu_afpf_mbox_handler(struct work_struct *work)
1935 struct rvu_work *mwork = container_of(work, struct rvu_work, work);
1937 __rvu_mbox_handler(mwork, TYPE_AFPF);
1940 static inline void rvu_afvf_mbox_handler(struct work_struct *work)
1942 struct rvu_work *mwork = container_of(work, struct rvu_work, work);
1944 __rvu_mbox_handler(mwork, TYPE_AFVF);
1947 static void __rvu_mbox_up_handler(struct rvu_work *mwork, int type)
1949 struct rvu *rvu = mwork->rvu;
1950 struct otx2_mbox_dev *mdev;
1951 struct mbox_hdr *rsp_hdr;
1952 struct mbox_msghdr *msg;
1953 struct mbox_wq_info *mw;
1954 struct otx2_mbox *mbox;
1955 int offset, id, devid;
1959 mw = &rvu->afpf_wq_info;
1962 mw = &rvu->afvf_wq_info;
1968 devid = mwork - mw->mbox_wrk_up;
1969 mbox = &mw->mbox_up;
1970 mdev = &mbox->dev[devid];
1972 rsp_hdr = mdev->mbase + mbox->rx_start;
1973 if (mw->mbox_wrk_up[devid].up_num_msgs == 0) {
1974 dev_warn(rvu->dev, "mbox up handler: num_msgs = 0\n");
1978 offset = mbox->rx_start + ALIGN(sizeof(*rsp_hdr), MBOX_MSG_ALIGN);
1980 for (id = 0; id < mw->mbox_wrk_up[devid].up_num_msgs; id++) {
1981 msg = mdev->mbase + offset;
1983 if (msg->id >= MBOX_MSG_MAX) {
1985 "Mbox msg with unknown ID 0x%x\n", msg->id);
1989 if (msg->sig != OTX2_MBOX_RSP_SIG) {
1991 "Mbox msg with wrong signature %x, ID 0x%x\n",
1997 case MBOX_MSG_CGX_LINK_EVENT:
2002 "Mbox msg response has err %d, ID 0x%x\n",
2007 offset = mbox->rx_start + msg->next_msgoff;
2010 mw->mbox_wrk_up[devid].up_num_msgs = 0;
2012 otx2_mbox_reset(mbox, devid);
2015 static inline void rvu_afpf_mbox_up_handler(struct work_struct *work)
2017 struct rvu_work *mwork = container_of(work, struct rvu_work, work);
2019 __rvu_mbox_up_handler(mwork, TYPE_AFPF);
2022 static inline void rvu_afvf_mbox_up_handler(struct work_struct *work)
2024 struct rvu_work *mwork = container_of(work, struct rvu_work, work);
2026 __rvu_mbox_up_handler(mwork, TYPE_AFVF);
2029 static int rvu_get_mbox_regions(struct rvu *rvu, void **mbox_addr,
2032 struct rvu_hwinfo *hw = rvu->hw;
2036 /* For cn10k platform VF mailbox regions of a PF follows after the
2037 * PF <-> AF mailbox region. Whereas for Octeontx2 it is read from
2038 * RVU_PF_VF_BAR4_ADDR register.
2040 if (type == TYPE_AFVF) {
2041 for (region = 0; region < num; region++) {
2042 if (hw->cap.per_pf_mbox_regs) {
2043 bar4 = rvu_read64(rvu, BLKADDR_RVUM,
2044 RVU_AF_PFX_BAR4_ADDR(0)) +
2046 bar4 += region * MBOX_SIZE;
2048 bar4 = rvupf_read64(rvu, RVU_PF_VF_BAR4_ADDR);
2049 bar4 += region * MBOX_SIZE;
2051 mbox_addr[region] = (void *)ioremap_wc(bar4, MBOX_SIZE);
2052 if (!mbox_addr[region])
2058 /* For cn10k platform AF <-> PF mailbox region of a PF is read from per
2059 * PF registers. Whereas for Octeontx2 it is read from
2060 * RVU_AF_PF_BAR4_ADDR register.
2062 for (region = 0; region < num; region++) {
2063 if (hw->cap.per_pf_mbox_regs) {
2064 bar4 = rvu_read64(rvu, BLKADDR_RVUM,
2065 RVU_AF_PFX_BAR4_ADDR(region));
2067 bar4 = rvu_read64(rvu, BLKADDR_RVUM,
2068 RVU_AF_PF_BAR4_ADDR);
2069 bar4 += region * MBOX_SIZE;
2071 mbox_addr[region] = (void *)ioremap_wc(bar4, MBOX_SIZE);
2072 if (!mbox_addr[region])
2079 iounmap((void __iomem *)mbox_addr[region]);
2083 static int rvu_mbox_init(struct rvu *rvu, struct mbox_wq_info *mw,
2085 void (mbox_handler)(struct work_struct *),
2086 void (mbox_up_handler)(struct work_struct *))
2088 int err = -EINVAL, i, dir, dir_up;
2089 void __iomem *reg_base;
2090 struct rvu_work *mwork;
2091 void **mbox_regions;
2094 mbox_regions = kcalloc(num, sizeof(void *), GFP_KERNEL);
2100 name = "rvu_afpf_mailbox";
2101 dir = MBOX_DIR_AFPF;
2102 dir_up = MBOX_DIR_AFPF_UP;
2103 reg_base = rvu->afreg_base;
2104 err = rvu_get_mbox_regions(rvu, mbox_regions, num, TYPE_AFPF);
2109 name = "rvu_afvf_mailbox";
2110 dir = MBOX_DIR_PFVF;
2111 dir_up = MBOX_DIR_PFVF_UP;
2112 reg_base = rvu->pfreg_base;
2113 err = rvu_get_mbox_regions(rvu, mbox_regions, num, TYPE_AFVF);
2121 mw->mbox_wq = alloc_workqueue(name,
2122 WQ_UNBOUND | WQ_HIGHPRI | WQ_MEM_RECLAIM,
2129 mw->mbox_wrk = devm_kcalloc(rvu->dev, num,
2130 sizeof(struct rvu_work), GFP_KERNEL);
2131 if (!mw->mbox_wrk) {
2136 mw->mbox_wrk_up = devm_kcalloc(rvu->dev, num,
2137 sizeof(struct rvu_work), GFP_KERNEL);
2138 if (!mw->mbox_wrk_up) {
2143 err = otx2_mbox_regions_init(&mw->mbox, mbox_regions, rvu->pdev,
2144 reg_base, dir, num);
2148 err = otx2_mbox_regions_init(&mw->mbox_up, mbox_regions, rvu->pdev,
2149 reg_base, dir_up, num);
2153 for (i = 0; i < num; i++) {
2154 mwork = &mw->mbox_wrk[i];
2156 INIT_WORK(&mwork->work, mbox_handler);
2158 mwork = &mw->mbox_wrk_up[i];
2160 INIT_WORK(&mwork->work, mbox_up_handler);
2162 kfree(mbox_regions);
2166 destroy_workqueue(mw->mbox_wq);
2169 iounmap((void __iomem *)mbox_regions[num]);
2171 kfree(mbox_regions);
2175 static void rvu_mbox_destroy(struct mbox_wq_info *mw)
2177 struct otx2_mbox *mbox = &mw->mbox;
2178 struct otx2_mbox_dev *mdev;
2182 flush_workqueue(mw->mbox_wq);
2183 destroy_workqueue(mw->mbox_wq);
2187 for (devid = 0; devid < mbox->ndevs; devid++) {
2188 mdev = &mbox->dev[devid];
2190 iounmap((void __iomem *)mdev->hwbase);
2193 otx2_mbox_destroy(&mw->mbox);
2194 otx2_mbox_destroy(&mw->mbox_up);
2197 static void rvu_queue_work(struct mbox_wq_info *mw, int first,
2198 int mdevs, u64 intr)
2200 struct otx2_mbox_dev *mdev;
2201 struct otx2_mbox *mbox;
2202 struct mbox_hdr *hdr;
2205 for (i = first; i < mdevs; i++) {
2207 if (!(intr & BIT_ULL(i - first)))
2211 mdev = &mbox->dev[i];
2212 hdr = mdev->mbase + mbox->rx_start;
2214 /*The hdr->num_msgs is set to zero immediately in the interrupt
2215 * handler to ensure that it holds a correct value next time
2216 * when the interrupt handler is called.
2217 * pf->mbox.num_msgs holds the data for use in pfaf_mbox_handler
2218 * pf>mbox.up_num_msgs holds the data for use in
2219 * pfaf_mbox_up_handler.
2222 if (hdr->num_msgs) {
2223 mw->mbox_wrk[i].num_msgs = hdr->num_msgs;
2225 queue_work(mw->mbox_wq, &mw->mbox_wrk[i].work);
2227 mbox = &mw->mbox_up;
2228 mdev = &mbox->dev[i];
2229 hdr = mdev->mbase + mbox->rx_start;
2230 if (hdr->num_msgs) {
2231 mw->mbox_wrk_up[i].up_num_msgs = hdr->num_msgs;
2233 queue_work(mw->mbox_wq, &mw->mbox_wrk_up[i].work);
2238 static irqreturn_t rvu_mbox_intr_handler(int irq, void *rvu_irq)
2240 struct rvu *rvu = (struct rvu *)rvu_irq;
2244 intr = rvu_read64(rvu, BLKADDR_RVUM, RVU_AF_PFAF_MBOX_INT);
2245 /* Clear interrupts */
2246 rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFAF_MBOX_INT, intr);
2248 trace_otx2_msg_interrupt(rvu->pdev, "PF(s) to AF", intr);
2250 /* Sync with mbox memory region */
2253 rvu_queue_work(&rvu->afpf_wq_info, 0, rvu->hw->total_pfs, intr);
2255 /* Handle VF interrupts */
2257 intr = rvupf_read64(rvu, RVU_PF_VFPF_MBOX_INTX(1));
2258 rvupf_write64(rvu, RVU_PF_VFPF_MBOX_INTX(1), intr);
2260 rvu_queue_work(&rvu->afvf_wq_info, 64, vfs, intr);
2264 intr = rvupf_read64(rvu, RVU_PF_VFPF_MBOX_INTX(0));
2265 rvupf_write64(rvu, RVU_PF_VFPF_MBOX_INTX(0), intr);
2267 trace_otx2_msg_interrupt(rvu->pdev, "VF(s) to AF", intr);
2269 rvu_queue_work(&rvu->afvf_wq_info, 0, vfs, intr);
2274 static void rvu_enable_mbox_intr(struct rvu *rvu)
2276 struct rvu_hwinfo *hw = rvu->hw;
2278 /* Clear spurious irqs, if any */
2279 rvu_write64(rvu, BLKADDR_RVUM,
2280 RVU_AF_PFAF_MBOX_INT, INTR_MASK(hw->total_pfs));
2282 /* Enable mailbox interrupt for all PFs except PF0 i.e AF itself */
2283 rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFAF_MBOX_INT_ENA_W1S,
2284 INTR_MASK(hw->total_pfs) & ~1ULL);
2287 static void rvu_blklf_teardown(struct rvu *rvu, u16 pcifunc, u8 blkaddr)
2289 struct rvu_block *block;
2290 int slot, lf, num_lfs;
2293 block = &rvu->hw->block[blkaddr];
2294 num_lfs = rvu_get_rsrc_mapcount(rvu_get_pfvf(rvu, pcifunc),
2298 for (slot = 0; slot < num_lfs; slot++) {
2299 lf = rvu_get_lf(rvu, block, pcifunc, slot);
2303 /* Cleanup LF and reset it */
2304 if (block->addr == BLKADDR_NIX0 || block->addr == BLKADDR_NIX1)
2305 rvu_nix_lf_teardown(rvu, pcifunc, block->addr, lf);
2306 else if (block->addr == BLKADDR_NPA)
2307 rvu_npa_lf_teardown(rvu, pcifunc, lf);
2308 else if ((block->addr == BLKADDR_CPT0) ||
2309 (block->addr == BLKADDR_CPT1))
2310 rvu_cpt_lf_teardown(rvu, pcifunc, lf, slot);
2312 err = rvu_lf_reset(rvu, block, lf);
2314 dev_err(rvu->dev, "Failed to reset blkaddr %d LF%d\n",
2320 static void __rvu_flr_handler(struct rvu *rvu, u16 pcifunc)
2322 mutex_lock(&rvu->flr_lock);
2323 /* Reset order should reflect inter-block dependencies:
2324 * 1. Reset any packet/work sources (NIX, CPT, TIM)
2325 * 2. Flush and reset SSO/SSOW
2326 * 3. Cleanup pools (NPA)
2328 rvu_blklf_teardown(rvu, pcifunc, BLKADDR_NIX0);
2329 rvu_blklf_teardown(rvu, pcifunc, BLKADDR_NIX1);
2330 rvu_blklf_teardown(rvu, pcifunc, BLKADDR_CPT0);
2331 rvu_blklf_teardown(rvu, pcifunc, BLKADDR_CPT1);
2332 rvu_blklf_teardown(rvu, pcifunc, BLKADDR_TIM);
2333 rvu_blklf_teardown(rvu, pcifunc, BLKADDR_SSOW);
2334 rvu_blklf_teardown(rvu, pcifunc, BLKADDR_SSO);
2335 rvu_blklf_teardown(rvu, pcifunc, BLKADDR_NPA);
2336 rvu_reset_lmt_map_tbl(rvu, pcifunc);
2337 rvu_detach_rsrcs(rvu, NULL, pcifunc);
2338 mutex_unlock(&rvu->flr_lock);
2341 static void rvu_afvf_flr_handler(struct rvu *rvu, int vf)
2345 /* pcifunc = 0(PF0) | (vf + 1) */
2346 __rvu_flr_handler(rvu, vf + 1);
2353 /* Signal FLR finish and enable IRQ */
2354 rvupf_write64(rvu, RVU_PF_VFTRPENDX(reg), BIT_ULL(vf));
2355 rvupf_write64(rvu, RVU_PF_VFFLR_INT_ENA_W1SX(reg), BIT_ULL(vf));
2358 static void rvu_flr_handler(struct work_struct *work)
2360 struct rvu_work *flrwork = container_of(work, struct rvu_work, work);
2361 struct rvu *rvu = flrwork->rvu;
2362 u16 pcifunc, numvfs, vf;
2366 pf = flrwork - rvu->flr_wrk;
2367 if (pf >= rvu->hw->total_pfs) {
2368 rvu_afvf_flr_handler(rvu, pf - rvu->hw->total_pfs);
2372 cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_PFX_CFG(pf));
2373 numvfs = (cfg >> 12) & 0xFF;
2374 pcifunc = pf << RVU_PFVF_PF_SHIFT;
2376 for (vf = 0; vf < numvfs; vf++)
2377 __rvu_flr_handler(rvu, (pcifunc | (vf + 1)));
2379 __rvu_flr_handler(rvu, pcifunc);
2381 /* Signal FLR finish */
2382 rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFTRPEND, BIT_ULL(pf));
2384 /* Enable interrupt */
2385 rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFFLR_INT_ENA_W1S, BIT_ULL(pf));
2388 static void rvu_afvf_queue_flr_work(struct rvu *rvu, int start_vf, int numvfs)
2390 int dev, vf, reg = 0;
2396 intr = rvupf_read64(rvu, RVU_PF_VFFLR_INTX(reg));
2400 for (vf = 0; vf < numvfs; vf++) {
2401 if (!(intr & BIT_ULL(vf)))
2403 dev = vf + start_vf + rvu->hw->total_pfs;
2404 queue_work(rvu->flr_wq, &rvu->flr_wrk[dev].work);
2405 /* Clear and disable the interrupt */
2406 rvupf_write64(rvu, RVU_PF_VFFLR_INTX(reg), BIT_ULL(vf));
2407 rvupf_write64(rvu, RVU_PF_VFFLR_INT_ENA_W1CX(reg), BIT_ULL(vf));
2411 static irqreturn_t rvu_flr_intr_handler(int irq, void *rvu_irq)
2413 struct rvu *rvu = (struct rvu *)rvu_irq;
2417 intr = rvu_read64(rvu, BLKADDR_RVUM, RVU_AF_PFFLR_INT);
2421 for (pf = 0; pf < rvu->hw->total_pfs; pf++) {
2422 if (intr & (1ULL << pf)) {
2423 /* PF is already dead do only AF related operations */
2424 queue_work(rvu->flr_wq, &rvu->flr_wrk[pf].work);
2425 /* clear interrupt */
2426 rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFFLR_INT,
2428 /* Disable the interrupt */
2429 rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFFLR_INT_ENA_W1C,
2435 rvu_afvf_queue_flr_work(rvu, 0, 64);
2437 rvu_afvf_queue_flr_work(rvu, 64, rvu->vfs - 64);
2442 static void rvu_me_handle_vfset(struct rvu *rvu, int idx, u64 intr)
2446 /* Nothing to be done here other than clearing the
2449 for (vf = 0; vf < 64; vf++) {
2450 if (intr & (1ULL << vf)) {
2451 /* clear the trpend due to ME(master enable) */
2452 rvupf_write64(rvu, RVU_PF_VFTRPENDX(idx), BIT_ULL(vf));
2453 /* clear interrupt */
2454 rvupf_write64(rvu, RVU_PF_VFME_INTX(idx), BIT_ULL(vf));
2459 /* Handles ME interrupts from VFs of AF */
2460 static irqreturn_t rvu_me_vf_intr_handler(int irq, void *rvu_irq)
2462 struct rvu *rvu = (struct rvu *)rvu_irq;
2466 intr = rvu_read64(rvu, BLKADDR_RVUM, RVU_AF_PFME_INT);
2468 for (vfset = 0; vfset <= 1; vfset++) {
2469 intr = rvupf_read64(rvu, RVU_PF_VFME_INTX(vfset));
2471 rvu_me_handle_vfset(rvu, vfset, intr);
2477 /* Handles ME interrupts from PFs */
2478 static irqreturn_t rvu_me_pf_intr_handler(int irq, void *rvu_irq)
2480 struct rvu *rvu = (struct rvu *)rvu_irq;
2484 intr = rvu_read64(rvu, BLKADDR_RVUM, RVU_AF_PFME_INT);
2486 /* Nothing to be done here other than clearing the
2489 for (pf = 0; pf < rvu->hw->total_pfs; pf++) {
2490 if (intr & (1ULL << pf)) {
2491 /* clear the trpend due to ME(master enable) */
2492 rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFTRPEND,
2494 /* clear interrupt */
2495 rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFME_INT,
2503 static void rvu_unregister_interrupts(struct rvu *rvu)
2507 /* Disable the Mbox interrupt */
2508 rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFAF_MBOX_INT_ENA_W1C,
2509 INTR_MASK(rvu->hw->total_pfs) & ~1ULL);
2511 /* Disable the PF FLR interrupt */
2512 rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFFLR_INT_ENA_W1C,
2513 INTR_MASK(rvu->hw->total_pfs) & ~1ULL);
2515 /* Disable the PF ME interrupt */
2516 rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFME_INT_ENA_W1C,
2517 INTR_MASK(rvu->hw->total_pfs) & ~1ULL);
2519 for (irq = 0; irq < rvu->num_vec; irq++) {
2520 if (rvu->irq_allocated[irq]) {
2521 free_irq(pci_irq_vector(rvu->pdev, irq), rvu);
2522 rvu->irq_allocated[irq] = false;
2526 pci_free_irq_vectors(rvu->pdev);
2530 static int rvu_afvf_msix_vectors_num_ok(struct rvu *rvu)
2532 struct rvu_pfvf *pfvf = &rvu->pf[0];
2536 offset = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_PFX_INT_CFG(0)) & 0x3ff;
2538 /* Make sure there are enough MSIX vectors configured so that
2539 * VF interrupts can be handled. Offset equal to zero means
2540 * that PF vectors are not configured and overlapping AF vectors.
2542 return (pfvf->msix.max >= RVU_AF_INT_VEC_CNT + RVU_PF_INT_VEC_CNT) &&
2546 static int rvu_register_interrupts(struct rvu *rvu)
2548 int ret, offset, pf_vec_start;
2550 rvu->num_vec = pci_msix_vec_count(rvu->pdev);
2552 rvu->irq_name = devm_kmalloc_array(rvu->dev, rvu->num_vec,
2553 NAME_SIZE, GFP_KERNEL);
2557 rvu->irq_allocated = devm_kcalloc(rvu->dev, rvu->num_vec,
2558 sizeof(bool), GFP_KERNEL);
2559 if (!rvu->irq_allocated)
2563 ret = pci_alloc_irq_vectors(rvu->pdev, rvu->num_vec,
2564 rvu->num_vec, PCI_IRQ_MSIX);
2567 "RVUAF: Request for %d msix vectors failed, ret %d\n",
2572 /* Register mailbox interrupt handler */
2573 sprintf(&rvu->irq_name[RVU_AF_INT_VEC_MBOX * NAME_SIZE], "RVUAF Mbox");
2574 ret = request_irq(pci_irq_vector(rvu->pdev, RVU_AF_INT_VEC_MBOX),
2575 rvu_mbox_intr_handler, 0,
2576 &rvu->irq_name[RVU_AF_INT_VEC_MBOX * NAME_SIZE], rvu);
2579 "RVUAF: IRQ registration failed for mbox irq\n");
2583 rvu->irq_allocated[RVU_AF_INT_VEC_MBOX] = true;
2585 /* Enable mailbox interrupts from all PFs */
2586 rvu_enable_mbox_intr(rvu);
2588 /* Register FLR interrupt handler */
2589 sprintf(&rvu->irq_name[RVU_AF_INT_VEC_PFFLR * NAME_SIZE],
2591 ret = request_irq(pci_irq_vector(rvu->pdev, RVU_AF_INT_VEC_PFFLR),
2592 rvu_flr_intr_handler, 0,
2593 &rvu->irq_name[RVU_AF_INT_VEC_PFFLR * NAME_SIZE],
2597 "RVUAF: IRQ registration failed for FLR\n");
2600 rvu->irq_allocated[RVU_AF_INT_VEC_PFFLR] = true;
2602 /* Enable FLR interrupt for all PFs*/
2603 rvu_write64(rvu, BLKADDR_RVUM,
2604 RVU_AF_PFFLR_INT, INTR_MASK(rvu->hw->total_pfs));
2606 rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFFLR_INT_ENA_W1S,
2607 INTR_MASK(rvu->hw->total_pfs) & ~1ULL);
2609 /* Register ME interrupt handler */
2610 sprintf(&rvu->irq_name[RVU_AF_INT_VEC_PFME * NAME_SIZE],
2612 ret = request_irq(pci_irq_vector(rvu->pdev, RVU_AF_INT_VEC_PFME),
2613 rvu_me_pf_intr_handler, 0,
2614 &rvu->irq_name[RVU_AF_INT_VEC_PFME * NAME_SIZE],
2618 "RVUAF: IRQ registration failed for ME\n");
2620 rvu->irq_allocated[RVU_AF_INT_VEC_PFME] = true;
2622 /* Clear TRPEND bit for all PF */
2623 rvu_write64(rvu, BLKADDR_RVUM,
2624 RVU_AF_PFTRPEND, INTR_MASK(rvu->hw->total_pfs));
2625 /* Enable ME interrupt for all PFs*/
2626 rvu_write64(rvu, BLKADDR_RVUM,
2627 RVU_AF_PFME_INT, INTR_MASK(rvu->hw->total_pfs));
2629 rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFME_INT_ENA_W1S,
2630 INTR_MASK(rvu->hw->total_pfs) & ~1ULL);
2632 if (!rvu_afvf_msix_vectors_num_ok(rvu))
2635 /* Get PF MSIX vectors offset. */
2636 pf_vec_start = rvu_read64(rvu, BLKADDR_RVUM,
2637 RVU_PRIV_PFX_INT_CFG(0)) & 0x3ff;
2639 /* Register MBOX0 interrupt. */
2640 offset = pf_vec_start + RVU_PF_INT_VEC_VFPF_MBOX0;
2641 sprintf(&rvu->irq_name[offset * NAME_SIZE], "RVUAFVF Mbox0");
2642 ret = request_irq(pci_irq_vector(rvu->pdev, offset),
2643 rvu_mbox_intr_handler, 0,
2644 &rvu->irq_name[offset * NAME_SIZE],
2648 "RVUAF: IRQ registration failed for Mbox0\n");
2650 rvu->irq_allocated[offset] = true;
2652 /* Register MBOX1 interrupt. MBOX1 IRQ number follows MBOX0 so
2653 * simply increment current offset by 1.
2655 offset = pf_vec_start + RVU_PF_INT_VEC_VFPF_MBOX1;
2656 sprintf(&rvu->irq_name[offset * NAME_SIZE], "RVUAFVF Mbox1");
2657 ret = request_irq(pci_irq_vector(rvu->pdev, offset),
2658 rvu_mbox_intr_handler, 0,
2659 &rvu->irq_name[offset * NAME_SIZE],
2663 "RVUAF: IRQ registration failed for Mbox1\n");
2665 rvu->irq_allocated[offset] = true;
2667 /* Register FLR interrupt handler for AF's VFs */
2668 offset = pf_vec_start + RVU_PF_INT_VEC_VFFLR0;
2669 sprintf(&rvu->irq_name[offset * NAME_SIZE], "RVUAFVF FLR0");
2670 ret = request_irq(pci_irq_vector(rvu->pdev, offset),
2671 rvu_flr_intr_handler, 0,
2672 &rvu->irq_name[offset * NAME_SIZE], rvu);
2675 "RVUAF: IRQ registration failed for RVUAFVF FLR0\n");
2678 rvu->irq_allocated[offset] = true;
2680 offset = pf_vec_start + RVU_PF_INT_VEC_VFFLR1;
2681 sprintf(&rvu->irq_name[offset * NAME_SIZE], "RVUAFVF FLR1");
2682 ret = request_irq(pci_irq_vector(rvu->pdev, offset),
2683 rvu_flr_intr_handler, 0,
2684 &rvu->irq_name[offset * NAME_SIZE], rvu);
2687 "RVUAF: IRQ registration failed for RVUAFVF FLR1\n");
2690 rvu->irq_allocated[offset] = true;
2692 /* Register ME interrupt handler for AF's VFs */
2693 offset = pf_vec_start + RVU_PF_INT_VEC_VFME0;
2694 sprintf(&rvu->irq_name[offset * NAME_SIZE], "RVUAFVF ME0");
2695 ret = request_irq(pci_irq_vector(rvu->pdev, offset),
2696 rvu_me_vf_intr_handler, 0,
2697 &rvu->irq_name[offset * NAME_SIZE], rvu);
2700 "RVUAF: IRQ registration failed for RVUAFVF ME0\n");
2703 rvu->irq_allocated[offset] = true;
2705 offset = pf_vec_start + RVU_PF_INT_VEC_VFME1;
2706 sprintf(&rvu->irq_name[offset * NAME_SIZE], "RVUAFVF ME1");
2707 ret = request_irq(pci_irq_vector(rvu->pdev, offset),
2708 rvu_me_vf_intr_handler, 0,
2709 &rvu->irq_name[offset * NAME_SIZE], rvu);
2712 "RVUAF: IRQ registration failed for RVUAFVF ME1\n");
2715 rvu->irq_allocated[offset] = true;
2719 rvu_unregister_interrupts(rvu);
2723 static void rvu_flr_wq_destroy(struct rvu *rvu)
2726 flush_workqueue(rvu->flr_wq);
2727 destroy_workqueue(rvu->flr_wq);
2732 static int rvu_flr_init(struct rvu *rvu)
2738 /* Enable FLR for all PFs*/
2739 for (pf = 0; pf < rvu->hw->total_pfs; pf++) {
2740 cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_PFX_CFG(pf));
2741 rvu_write64(rvu, BLKADDR_RVUM, RVU_PRIV_PFX_CFG(pf),
2745 rvu->flr_wq = alloc_workqueue("rvu_afpf_flr",
2746 WQ_UNBOUND | WQ_HIGHPRI | WQ_MEM_RECLAIM,
2751 num_devs = rvu->hw->total_pfs + pci_sriov_get_totalvfs(rvu->pdev);
2752 rvu->flr_wrk = devm_kcalloc(rvu->dev, num_devs,
2753 sizeof(struct rvu_work), GFP_KERNEL);
2754 if (!rvu->flr_wrk) {
2755 destroy_workqueue(rvu->flr_wq);
2759 for (dev = 0; dev < num_devs; dev++) {
2760 rvu->flr_wrk[dev].rvu = rvu;
2761 INIT_WORK(&rvu->flr_wrk[dev].work, rvu_flr_handler);
2764 mutex_init(&rvu->flr_lock);
2769 static void rvu_disable_afvf_intr(struct rvu *rvu)
2773 rvupf_write64(rvu, RVU_PF_VFPF_MBOX_INT_ENA_W1CX(0), INTR_MASK(vfs));
2774 rvupf_write64(rvu, RVU_PF_VFFLR_INT_ENA_W1CX(0), INTR_MASK(vfs));
2775 rvupf_write64(rvu, RVU_PF_VFME_INT_ENA_W1CX(0), INTR_MASK(vfs));
2779 rvupf_write64(rvu, RVU_PF_VFPF_MBOX_INT_ENA_W1CX(1),
2780 INTR_MASK(vfs - 64));
2781 rvupf_write64(rvu, RVU_PF_VFFLR_INT_ENA_W1CX(1), INTR_MASK(vfs - 64));
2782 rvupf_write64(rvu, RVU_PF_VFME_INT_ENA_W1CX(1), INTR_MASK(vfs - 64));
2785 static void rvu_enable_afvf_intr(struct rvu *rvu)
2789 /* Clear any pending interrupts and enable AF VF interrupts for
2793 rvupf_write64(rvu, RVU_PF_VFPF_MBOX_INTX(0), INTR_MASK(vfs));
2794 rvupf_write64(rvu, RVU_PF_VFPF_MBOX_INT_ENA_W1SX(0), INTR_MASK(vfs));
2797 rvupf_write64(rvu, RVU_PF_VFFLR_INTX(0), INTR_MASK(vfs));
2798 rvupf_write64(rvu, RVU_PF_VFFLR_INT_ENA_W1SX(0), INTR_MASK(vfs));
2799 rvupf_write64(rvu, RVU_PF_VFME_INT_ENA_W1SX(0), INTR_MASK(vfs));
2801 /* Same for remaining VFs, if any. */
2805 rvupf_write64(rvu, RVU_PF_VFPF_MBOX_INTX(1), INTR_MASK(vfs - 64));
2806 rvupf_write64(rvu, RVU_PF_VFPF_MBOX_INT_ENA_W1SX(1),
2807 INTR_MASK(vfs - 64));
2809 rvupf_write64(rvu, RVU_PF_VFFLR_INTX(1), INTR_MASK(vfs - 64));
2810 rvupf_write64(rvu, RVU_PF_VFFLR_INT_ENA_W1SX(1), INTR_MASK(vfs - 64));
2811 rvupf_write64(rvu, RVU_PF_VFME_INT_ENA_W1SX(1), INTR_MASK(vfs - 64));
2814 int rvu_get_num_lbk_chans(void)
2816 struct pci_dev *pdev;
2820 pdev = pci_get_device(PCI_VENDOR_ID_CAVIUM, PCI_DEVID_OCTEONTX2_LBK,
2825 base = pci_ioremap_bar(pdev, 0);
2829 /* Read number of available LBK channels from LBK(0)_CONST register. */
2830 ret = (readq(base + 0x10) >> 32) & 0xffff;
2838 static int rvu_enable_sriov(struct rvu *rvu)
2840 struct pci_dev *pdev = rvu->pdev;
2841 int err, chans, vfs;
2843 if (!rvu_afvf_msix_vectors_num_ok(rvu)) {
2844 dev_warn(&pdev->dev,
2845 "Skipping SRIOV enablement since not enough IRQs are available\n");
2849 chans = rvu_get_num_lbk_chans();
2853 vfs = pci_sriov_get_totalvfs(pdev);
2855 /* Limit VFs in case we have more VFs than LBK channels available. */
2862 /* Save VFs number for reference in VF interrupts handlers.
2863 * Since interrupts might start arriving during SRIOV enablement
2864 * ordinary API cannot be used to get number of enabled VFs.
2868 err = rvu_mbox_init(rvu, &rvu->afvf_wq_info, TYPE_AFVF, vfs,
2869 rvu_afvf_mbox_handler, rvu_afvf_mbox_up_handler);
2873 rvu_enable_afvf_intr(rvu);
2874 /* Make sure IRQs are enabled before SRIOV. */
2877 err = pci_enable_sriov(pdev, vfs);
2879 rvu_disable_afvf_intr(rvu);
2880 rvu_mbox_destroy(&rvu->afvf_wq_info);
2887 static void rvu_disable_sriov(struct rvu *rvu)
2889 rvu_disable_afvf_intr(rvu);
2890 rvu_mbox_destroy(&rvu->afvf_wq_info);
2891 pci_disable_sriov(rvu->pdev);
2894 static void rvu_update_module_params(struct rvu *rvu)
2896 const char *default_pfl_name = "default";
2898 strscpy(rvu->mkex_pfl_name,
2899 mkex_profile ? mkex_profile : default_pfl_name, MKEX_NAME_LEN);
2900 strscpy(rvu->kpu_pfl_name,
2901 kpu_profile ? kpu_profile : default_pfl_name, KPU_NAME_LEN);
2904 static int rvu_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2906 struct device *dev = &pdev->dev;
2910 rvu = devm_kzalloc(dev, sizeof(*rvu), GFP_KERNEL);
2914 rvu->hw = devm_kzalloc(dev, sizeof(struct rvu_hwinfo), GFP_KERNEL);
2916 devm_kfree(dev, rvu);
2920 pci_set_drvdata(pdev, rvu);
2922 rvu->dev = &pdev->dev;
2924 err = pci_enable_device(pdev);
2926 dev_err(dev, "Failed to enable PCI device\n");
2930 err = pci_request_regions(pdev, DRV_NAME);
2932 dev_err(dev, "PCI request regions failed 0x%x\n", err);
2933 goto err_disable_device;
2936 err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
2938 dev_err(dev, "DMA mask config failed, abort\n");
2939 goto err_release_regions;
2942 pci_set_master(pdev);
2944 rvu->ptp = ptp_get();
2945 if (IS_ERR(rvu->ptp)) {
2946 err = PTR_ERR(rvu->ptp);
2947 if (err == -EPROBE_DEFER)
2948 goto err_release_regions;
2952 /* Map Admin function CSRs */
2953 rvu->afreg_base = pcim_iomap(pdev, PCI_AF_REG_BAR_NUM, 0);
2954 rvu->pfreg_base = pcim_iomap(pdev, PCI_PF_REG_BAR_NUM, 0);
2955 if (!rvu->afreg_base || !rvu->pfreg_base) {
2956 dev_err(dev, "Unable to map admin function CSRs, aborting\n");
2961 /* Store module params in rvu structure */
2962 rvu_update_module_params(rvu);
2964 /* Check which blocks the HW supports */
2965 rvu_check_block_implemented(rvu);
2967 rvu_reset_all_blocks(rvu);
2969 rvu_setup_hw_capabilities(rvu);
2971 err = rvu_setup_hw_resources(rvu);
2975 /* Init mailbox btw AF and PFs */
2976 err = rvu_mbox_init(rvu, &rvu->afpf_wq_info, TYPE_AFPF,
2977 rvu->hw->total_pfs, rvu_afpf_mbox_handler,
2978 rvu_afpf_mbox_up_handler);
2982 err = rvu_flr_init(rvu);
2986 err = rvu_register_interrupts(rvu);
2990 err = rvu_register_dl(rvu);
2994 rvu_setup_rvum_blk_revid(rvu);
2996 /* Enable AF's VFs (if any) */
2997 err = rvu_enable_sriov(rvu);
3001 /* Initialize debugfs */
3006 rvu_unregister_dl(rvu);
3008 rvu_unregister_interrupts(rvu);
3010 rvu_flr_wq_destroy(rvu);
3012 rvu_mbox_destroy(&rvu->afpf_wq_info);
3015 rvu_fwdata_exit(rvu);
3016 rvu_reset_all_blocks(rvu);
3017 rvu_free_hw_resources(rvu);
3018 rvu_clear_rvum_blk_revid(rvu);
3021 err_release_regions:
3022 pci_release_regions(pdev);
3024 pci_disable_device(pdev);
3026 pci_set_drvdata(pdev, NULL);
3027 devm_kfree(&pdev->dev, rvu->hw);
3028 devm_kfree(dev, rvu);
3032 static void rvu_remove(struct pci_dev *pdev)
3034 struct rvu *rvu = pci_get_drvdata(pdev);
3037 rvu_unregister_dl(rvu);
3038 rvu_unregister_interrupts(rvu);
3039 rvu_flr_wq_destroy(rvu);
3041 rvu_fwdata_exit(rvu);
3042 rvu_mbox_destroy(&rvu->afpf_wq_info);
3043 rvu_disable_sriov(rvu);
3044 rvu_reset_all_blocks(rvu);
3045 rvu_free_hw_resources(rvu);
3046 rvu_clear_rvum_blk_revid(rvu);
3048 pci_release_regions(pdev);
3049 pci_disable_device(pdev);
3050 pci_set_drvdata(pdev, NULL);
3052 devm_kfree(&pdev->dev, rvu->hw);
3053 devm_kfree(&pdev->dev, rvu);
3056 static struct pci_driver rvu_driver = {
3058 .id_table = rvu_id_table,
3060 .remove = rvu_remove,
3063 static int __init rvu_init_module(void)
3067 pr_info("%s: %s\n", DRV_NAME, DRV_STRING);
3069 err = pci_register_driver(&cgx_driver);
3073 err = pci_register_driver(&ptp_driver);
3077 err = pci_register_driver(&rvu_driver);
3083 pci_unregister_driver(&ptp_driver);
3085 pci_unregister_driver(&cgx_driver);
3090 static void __exit rvu_cleanup_module(void)
3092 pci_unregister_driver(&rvu_driver);
3093 pci_unregister_driver(&ptp_driver);
3094 pci_unregister_driver(&cgx_driver);
3097 module_init(rvu_init_module);
3098 module_exit(rvu_cleanup_module);