xref: /openbmc/linux/drivers/net/ethernet/marvell/octeontx2/af/rvu.c (revision b694e3c604e999343258c49e574abd7be012e726)
154494aa5SSunil Goutham // SPDX-License-Identifier: GPL-2.0
2c7cd6c5aSSunil Goutham /* Marvell RVU Admin Function driver
354494aa5SSunil Goutham  *
4c7cd6c5aSSunil Goutham  * Copyright (C) 2018 Marvell.
554494aa5SSunil Goutham  *
654494aa5SSunil Goutham  */
754494aa5SSunil Goutham 
854494aa5SSunil Goutham #include <linux/module.h>
954494aa5SSunil Goutham #include <linux/interrupt.h>
1054494aa5SSunil Goutham #include <linux/delay.h>
1154494aa5SSunil Goutham #include <linux/irq.h>
1254494aa5SSunil Goutham #include <linux/pci.h>
1354494aa5SSunil Goutham #include <linux/sysfs.h>
1454494aa5SSunil Goutham 
158e22f040SSunil Goutham #include "cgx.h"
1654494aa5SSunil Goutham #include "rvu.h"
1754d55781SSunil Goutham #include "rvu_reg.h"
184086f2a0SAleksey Makarov #include "ptp.h"
19ca7f49ffSGeetha sowjanya #include "mcs.h"
2054494aa5SSunil Goutham 
2149142d12SSubbaraya Sundeep #include "rvu_trace.h"
22b747923aSRatheesh Kannoth #include "rvu_npc_hash.h"
2349142d12SSubbaraya Sundeep 
246e54e1c5SHariprasad Kelam #define DRV_NAME	"rvu_af"
2554494aa5SSunil Goutham #define DRV_STRING      "Marvell OcteonTX2 RVU Admin Function Driver"
2654494aa5SSunil Goutham 
27756051e2SSunil Goutham static void rvu_set_msix_offset(struct rvu *rvu, struct rvu_pfvf *pfvf,
28756051e2SSunil Goutham 				struct rvu_block *block, int lf);
29756051e2SSunil Goutham static void rvu_clear_msix_offset(struct rvu *rvu, struct rvu_pfvf *pfvf,
30756051e2SSunil Goutham 				  struct rvu_block *block, int lf);
31c554f9c1SGeetha sowjanya static void __rvu_flr_handler(struct rvu *rvu, u16 pcifunc);
32756051e2SSunil Goutham 
339bdc47a6STomasz Duszynski static int rvu_mbox_init(struct rvu *rvu, struct mbox_wq_info *mw,
349bdc47a6STomasz Duszynski 			 int type, int num,
359bdc47a6STomasz Duszynski 			 void (mbox_handler)(struct work_struct *),
369bdc47a6STomasz Duszynski 			 void (mbox_up_handler)(struct work_struct *));
379bdc47a6STomasz Duszynski enum {
389bdc47a6STomasz Duszynski 	TYPE_AFVF,
399bdc47a6STomasz Duszynski 	TYPE_AFPF,
409bdc47a6STomasz Duszynski };
419bdc47a6STomasz Duszynski 
4254494aa5SSunil Goutham /* Supported devices */
4354494aa5SSunil Goutham static const struct pci_device_id rvu_id_table[] = {
4454494aa5SSunil Goutham 	{ PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVID_OCTEONTX2_RVU_AF) },
4554494aa5SSunil Goutham 	{ 0, }  /* end of table */
4654494aa5SSunil Goutham };
4754494aa5SSunil Goutham 
48fc992e33SSunil Goutham MODULE_AUTHOR("Sunil Goutham <sgoutham@marvell.com>");
4954494aa5SSunil Goutham MODULE_DESCRIPTION(DRV_STRING);
5054494aa5SSunil Goutham MODULE_LICENSE("GPL v2");
5154494aa5SSunil Goutham MODULE_DEVICE_TABLE(pci, rvu_id_table);
5254494aa5SSunil Goutham 
5323705adbSVamsi Attunuru static char *mkex_profile; /* MKEX profile name */
5423705adbSVamsi Attunuru module_param(mkex_profile, charp, 0000);
5523705adbSVamsi Attunuru MODULE_PARM_DESC(mkex_profile, "MKEX profile name string");
5623705adbSVamsi Attunuru 
573a724415SStanislaw Kardach static char *kpu_profile; /* KPU profile name */
583a724415SStanislaw Kardach module_param(kpu_profile, charp, 0000);
593a724415SStanislaw Kardach MODULE_PARM_DESC(kpu_profile, "KPU profile name string");
603a724415SStanislaw Kardach 
rvu_setup_hw_capabilities(struct rvu * rvu)615d9b976dSSunil Goutham static void rvu_setup_hw_capabilities(struct rvu *rvu)
625d9b976dSSunil Goutham {
635d9b976dSSunil Goutham 	struct rvu_hwinfo *hw = rvu->hw;
645d9b976dSSunil Goutham 
655d9b976dSSunil Goutham 	hw->cap.nix_tx_aggr_lvl = NIX_TXSCH_LVL_TL1;
665d9b976dSSunil Goutham 	hw->cap.nix_fixed_txschq_mapping = false;
675d9b976dSSunil Goutham 	hw->cap.nix_shaping = true;
685d9b976dSSunil Goutham 	hw->cap.nix_tx_link_bp = true;
69561e8752SSunil Goutham 	hw->cap.nix_rx_multicast = true;
70d0641163SNithin Dabilpuram 	hw->cap.nix_shaper_toggle_wait = false;
7156d9f5fdSRatheesh Kannoth 	hw->cap.npc_hash_extract = false;
72b747923aSRatheesh Kannoth 	hw->cap.npc_exact_match_enabled = false;
73221f3dffSRakesh Babu 	hw->rvu = rvu;
745d9b976dSSunil Goutham 
75d0641163SNithin Dabilpuram 	if (is_rvu_pre_96xx_C0(rvu)) {
765d9b976dSSunil Goutham 		hw->cap.nix_fixed_txschq_mapping = true;
775d9b976dSSunil Goutham 		hw->cap.nix_txsch_per_cgx_lmac = 4;
785d9b976dSSunil Goutham 		hw->cap.nix_txsch_per_lbk_lmac = 132;
795d9b976dSSunil Goutham 		hw->cap.nix_txsch_per_sdp_lmac = 76;
805d9b976dSSunil Goutham 		hw->cap.nix_shaping = false;
815d9b976dSSunil Goutham 		hw->cap.nix_tx_link_bp = false;
82d0641163SNithin Dabilpuram 		if (is_rvu_96xx_A0(rvu) || is_rvu_95xx_A0(rvu))
83561e8752SSunil Goutham 			hw->cap.nix_rx_multicast = false;
845d9b976dSSunil Goutham 	}
85d0641163SNithin Dabilpuram 	if (!is_rvu_pre_96xx_C0(rvu))
86d0641163SNithin Dabilpuram 		hw->cap.nix_shaper_toggle_wait = true;
8798c56111SSubbaraya Sundeep 
8898c56111SSubbaraya Sundeep 	if (!is_rvu_otx2(rvu))
8998c56111SSubbaraya Sundeep 		hw->cap.per_pf_mbox_regs = true;
9056d9f5fdSRatheesh Kannoth 
9156d9f5fdSRatheesh Kannoth 	if (is_rvu_npc_hash_extract_en(rvu))
9256d9f5fdSRatheesh Kannoth 		hw->cap.npc_hash_extract = true;
935d9b976dSSunil Goutham }
945d9b976dSSunil Goutham 
9554d55781SSunil Goutham /* Poll a RVU block's register 'offset', for a 'zero'
9654d55781SSunil Goutham  * or 'nonzero' at bits specified by 'mask'
9754d55781SSunil Goutham  */
rvu_poll_reg(struct rvu * rvu,u64 block,u64 offset,u64 mask,bool zero)9854d55781SSunil Goutham int rvu_poll_reg(struct rvu *rvu, u64 block, u64 offset, u64 mask, bool zero)
9954d55781SSunil Goutham {
10021274aa1SSmadar Fuks 	unsigned long timeout = jiffies + usecs_to_jiffies(20000);
10121274aa1SSmadar Fuks 	bool twice = false;
10254d55781SSunil Goutham 	void __iomem *reg;
10354d55781SSunil Goutham 	u64 reg_val;
10454d55781SSunil Goutham 
10554d55781SSunil Goutham 	reg = rvu->afreg_base + ((block << 28) | offset);
106dc819c1bSSunil Goutham again:
10754d55781SSunil Goutham 	reg_val = readq(reg);
10854d55781SSunil Goutham 	if (zero && !(reg_val & mask))
10954d55781SSunil Goutham 		return 0;
11054d55781SSunil Goutham 	if (!zero && (reg_val & mask))
11154d55781SSunil Goutham 		return 0;
112dc819c1bSSunil Goutham 	if (time_before(jiffies, timeout)) {
1136ca3ee2fSSunil Goutham 		usleep_range(1, 5);
114dc819c1bSSunil Goutham 		goto again;
11554d55781SSunil Goutham 	}
11621274aa1SSmadar Fuks 	/* In scenarios where CPU is scheduled out before checking
11721274aa1SSmadar Fuks 	 * 'time_before' (above) and gets scheduled in such that
11821274aa1SSmadar Fuks 	 * jiffies are beyond timeout value, then check again if HW is
11921274aa1SSmadar Fuks 	 * done with the operation in the meantime.
12021274aa1SSmadar Fuks 	 */
12121274aa1SSmadar Fuks 	if (!twice) {
12221274aa1SSmadar Fuks 		twice = true;
12321274aa1SSmadar Fuks 		goto again;
12421274aa1SSmadar Fuks 	}
12554d55781SSunil Goutham 	return -EBUSY;
12654d55781SSunil Goutham }
12754d55781SSunil Goutham 
rvu_alloc_rsrc(struct rsrc_bmap * rsrc)128746ea742SSunil Goutham int rvu_alloc_rsrc(struct rsrc_bmap *rsrc)
129746ea742SSunil Goutham {
130746ea742SSunil Goutham 	int id;
131746ea742SSunil Goutham 
132746ea742SSunil Goutham 	if (!rsrc->bmap)
133746ea742SSunil Goutham 		return -EINVAL;
134746ea742SSunil Goutham 
135746ea742SSunil Goutham 	id = find_first_zero_bit(rsrc->bmap, rsrc->max);
136746ea742SSunil Goutham 	if (id >= rsrc->max)
137746ea742SSunil Goutham 		return -ENOSPC;
138746ea742SSunil Goutham 
139746ea742SSunil Goutham 	__set_bit(id, rsrc->bmap);
140746ea742SSunil Goutham 
141746ea742SSunil Goutham 	return id;
142746ea742SSunil Goutham }
143746ea742SSunil Goutham 
rvu_alloc_rsrc_contig(struct rsrc_bmap * rsrc,int nrsrc)144a3e7121cSSunil Goutham int rvu_alloc_rsrc_contig(struct rsrc_bmap *rsrc, int nrsrc)
145756051e2SSunil Goutham {
146756051e2SSunil Goutham 	int start;
147756051e2SSunil Goutham 
148756051e2SSunil Goutham 	if (!rsrc->bmap)
149756051e2SSunil Goutham 		return -EINVAL;
150756051e2SSunil Goutham 
151756051e2SSunil Goutham 	start = bitmap_find_next_zero_area(rsrc->bmap, rsrc->max, 0, nrsrc, 0);
152756051e2SSunil Goutham 	if (start >= rsrc->max)
153756051e2SSunil Goutham 		return -ENOSPC;
154756051e2SSunil Goutham 
155756051e2SSunil Goutham 	bitmap_set(rsrc->bmap, start, nrsrc);
156756051e2SSunil Goutham 	return start;
157756051e2SSunil Goutham }
158756051e2SSunil Goutham 
rvu_free_rsrc_contig(struct rsrc_bmap * rsrc,int nrsrc,int start)159756051e2SSunil Goutham static void rvu_free_rsrc_contig(struct rsrc_bmap *rsrc, int nrsrc, int start)
160756051e2SSunil Goutham {
161756051e2SSunil Goutham 	if (!rsrc->bmap)
162756051e2SSunil Goutham 		return;
163756051e2SSunil Goutham 	if (start >= rsrc->max)
164756051e2SSunil Goutham 		return;
165756051e2SSunil Goutham 
166756051e2SSunil Goutham 	bitmap_clear(rsrc->bmap, start, nrsrc);
167756051e2SSunil Goutham }
168756051e2SSunil Goutham 
rvu_rsrc_check_contig(struct rsrc_bmap * rsrc,int nrsrc)169a3e7121cSSunil Goutham bool rvu_rsrc_check_contig(struct rsrc_bmap *rsrc, int nrsrc)
170756051e2SSunil Goutham {
171756051e2SSunil Goutham 	int start;
172756051e2SSunil Goutham 
173756051e2SSunil Goutham 	if (!rsrc->bmap)
174756051e2SSunil Goutham 		return false;
175756051e2SSunil Goutham 
176756051e2SSunil Goutham 	start = bitmap_find_next_zero_area(rsrc->bmap, rsrc->max, 0, nrsrc, 0);
177756051e2SSunil Goutham 	if (start >= rsrc->max)
178756051e2SSunil Goutham 		return false;
179756051e2SSunil Goutham 
180756051e2SSunil Goutham 	return true;
181756051e2SSunil Goutham }
182756051e2SSunil Goutham 
rvu_free_rsrc(struct rsrc_bmap * rsrc,int id)183746ea742SSunil Goutham void rvu_free_rsrc(struct rsrc_bmap *rsrc, int id)
184746ea742SSunil Goutham {
185746ea742SSunil Goutham 	if (!rsrc->bmap)
186746ea742SSunil Goutham 		return;
187746ea742SSunil Goutham 
188746ea742SSunil Goutham 	__clear_bit(id, rsrc->bmap);
189746ea742SSunil Goutham }
190746ea742SSunil Goutham 
rvu_rsrc_free_count(struct rsrc_bmap * rsrc)191746ea742SSunil Goutham int rvu_rsrc_free_count(struct rsrc_bmap *rsrc)
192746ea742SSunil Goutham {
193746ea742SSunil Goutham 	int used;
194746ea742SSunil Goutham 
195746ea742SSunil Goutham 	if (!rsrc->bmap)
196746ea742SSunil Goutham 		return 0;
197746ea742SSunil Goutham 
198746ea742SSunil Goutham 	used = bitmap_weight(rsrc->bmap, rsrc->max);
199746ea742SSunil Goutham 	return (rsrc->max - used);
200746ea742SSunil Goutham }
201746ea742SSunil Goutham 
is_rsrc_free(struct rsrc_bmap * rsrc,int id)202e7d89717SSunil Goutham bool is_rsrc_free(struct rsrc_bmap *rsrc, int id)
203e7d89717SSunil Goutham {
204e7d89717SSunil Goutham 	if (!rsrc->bmap)
205e7d89717SSunil Goutham 		return false;
206e7d89717SSunil Goutham 
207e7d89717SSunil Goutham 	return !test_bit(id, rsrc->bmap);
208e7d89717SSunil Goutham }
209e7d89717SSunil Goutham 
rvu_alloc_bitmap(struct rsrc_bmap * rsrc)2101054a622SSunil Goutham int rvu_alloc_bitmap(struct rsrc_bmap *rsrc)
2111054a622SSunil Goutham {
2121054a622SSunil Goutham 	rsrc->bmap = kcalloc(BITS_TO_LONGS(rsrc->max),
2131054a622SSunil Goutham 			     sizeof(long), GFP_KERNEL);
2141054a622SSunil Goutham 	if (!rsrc->bmap)
2151054a622SSunil Goutham 		return -ENOMEM;
2161054a622SSunil Goutham 	return 0;
2171054a622SSunil Goutham }
2181054a622SSunil Goutham 
rvu_free_bitmap(struct rsrc_bmap * rsrc)219d863ca67SChristophe JAILLET void rvu_free_bitmap(struct rsrc_bmap *rsrc)
220d863ca67SChristophe JAILLET {
221d863ca67SChristophe JAILLET 	kfree(rsrc->bmap);
222d863ca67SChristophe JAILLET }
223d863ca67SChristophe JAILLET 
224756051e2SSunil Goutham /* Get block LF's HW index from a PF_FUNC's block slot number */
rvu_get_lf(struct rvu * rvu,struct rvu_block * block,u16 pcifunc,u16 slot)225756051e2SSunil Goutham int rvu_get_lf(struct rvu *rvu, struct rvu_block *block, u16 pcifunc, u16 slot)
226756051e2SSunil Goutham {
227756051e2SSunil Goutham 	u16 match = 0;
228756051e2SSunil Goutham 	int lf;
229756051e2SSunil Goutham 
2300964fc8fSStanislaw Kardach 	mutex_lock(&rvu->rsrc_lock);
231756051e2SSunil Goutham 	for (lf = 0; lf < block->lf.max; lf++) {
232756051e2SSunil Goutham 		if (block->fn_map[lf] == pcifunc) {
233756051e2SSunil Goutham 			if (slot == match) {
2340964fc8fSStanislaw Kardach 				mutex_unlock(&rvu->rsrc_lock);
235756051e2SSunil Goutham 				return lf;
236756051e2SSunil Goutham 			}
237756051e2SSunil Goutham 			match++;
238756051e2SSunil Goutham 		}
239756051e2SSunil Goutham 	}
2400964fc8fSStanislaw Kardach 	mutex_unlock(&rvu->rsrc_lock);
241756051e2SSunil Goutham 	return -ENODEV;
242756051e2SSunil Goutham }
243756051e2SSunil Goutham 
244746ea742SSunil Goutham /* Convert BLOCK_TYPE_E to a BLOCK_ADDR_E.
245746ea742SSunil Goutham  * Some silicon variants of OcteonTX2 supports
246746ea742SSunil Goutham  * multiple blocks of same type.
247746ea742SSunil Goutham  *
248746ea742SSunil Goutham  * @pcifunc has to be zero when no LF is yet attached.
2499932fb72SRakesh Babu  *
2509932fb72SRakesh Babu  * For a pcifunc if LFs are attached from multiple blocks of same type, then
2519932fb72SRakesh Babu  * return blkaddr of first encountered block.
252746ea742SSunil Goutham  */
rvu_get_blkaddr(struct rvu * rvu,int blktype,u16 pcifunc)253746ea742SSunil Goutham int rvu_get_blkaddr(struct rvu *rvu, int blktype, u16 pcifunc)
254746ea742SSunil Goutham {
255746ea742SSunil Goutham 	int devnum, blkaddr = -ENODEV;
256746ea742SSunil Goutham 	u64 cfg, reg;
257746ea742SSunil Goutham 	bool is_pf;
258746ea742SSunil Goutham 
259746ea742SSunil Goutham 	switch (blktype) {
26023923ea4SSunil Goutham 	case BLKTYPE_NPC:
26123923ea4SSunil Goutham 		blkaddr = BLKADDR_NPC;
26223923ea4SSunil Goutham 		goto exit;
263746ea742SSunil Goutham 	case BLKTYPE_NPA:
264746ea742SSunil Goutham 		blkaddr = BLKADDR_NPA;
265746ea742SSunil Goutham 		goto exit;
266746ea742SSunil Goutham 	case BLKTYPE_NIX:
267746ea742SSunil Goutham 		/* For now assume NIX0 */
268746ea742SSunil Goutham 		if (!pcifunc) {
269746ea742SSunil Goutham 			blkaddr = BLKADDR_NIX0;
270746ea742SSunil Goutham 			goto exit;
271746ea742SSunil Goutham 		}
272746ea742SSunil Goutham 		break;
273746ea742SSunil Goutham 	case BLKTYPE_SSO:
274746ea742SSunil Goutham 		blkaddr = BLKADDR_SSO;
275746ea742SSunil Goutham 		goto exit;
276746ea742SSunil Goutham 	case BLKTYPE_SSOW:
277746ea742SSunil Goutham 		blkaddr = BLKADDR_SSOW;
278746ea742SSunil Goutham 		goto exit;
279746ea742SSunil Goutham 	case BLKTYPE_TIM:
280746ea742SSunil Goutham 		blkaddr = BLKADDR_TIM;
281746ea742SSunil Goutham 		goto exit;
282746ea742SSunil Goutham 	case BLKTYPE_CPT:
283746ea742SSunil Goutham 		/* For now assume CPT0 */
284746ea742SSunil Goutham 		if (!pcifunc) {
285746ea742SSunil Goutham 			blkaddr = BLKADDR_CPT0;
286746ea742SSunil Goutham 			goto exit;
287746ea742SSunil Goutham 		}
288746ea742SSunil Goutham 		break;
289746ea742SSunil Goutham 	}
290746ea742SSunil Goutham 
291746ea742SSunil Goutham 	/* Check if this is a RVU PF or VF */
292746ea742SSunil Goutham 	if (pcifunc & RVU_PFVF_FUNC_MASK) {
293746ea742SSunil Goutham 		is_pf = false;
294746ea742SSunil Goutham 		devnum = rvu_get_hwvf(rvu, pcifunc);
295746ea742SSunil Goutham 	} else {
296746ea742SSunil Goutham 		is_pf = true;
297746ea742SSunil Goutham 		devnum = rvu_get_pf(pcifunc);
298746ea742SSunil Goutham 	}
299746ea742SSunil Goutham 
3009932fb72SRakesh Babu 	/* Check if the 'pcifunc' has a NIX LF from 'BLKADDR_NIX0' or
3019932fb72SRakesh Babu 	 * 'BLKADDR_NIX1'.
3029932fb72SRakesh Babu 	 */
303746ea742SSunil Goutham 	if (blktype == BLKTYPE_NIX) {
3049932fb72SRakesh Babu 		reg = is_pf ? RVU_PRIV_PFX_NIXX_CFG(0) :
3059932fb72SRakesh Babu 			RVU_PRIV_HWVFX_NIXX_CFG(0);
306746ea742SSunil Goutham 		cfg = rvu_read64(rvu, BLKADDR_RVUM, reg | (devnum << 16));
3079932fb72SRakesh Babu 		if (cfg) {
308746ea742SSunil Goutham 			blkaddr = BLKADDR_NIX0;
3099932fb72SRakesh Babu 			goto exit;
310746ea742SSunil Goutham 		}
311746ea742SSunil Goutham 
3129932fb72SRakesh Babu 		reg = is_pf ? RVU_PRIV_PFX_NIXX_CFG(1) :
3139932fb72SRakesh Babu 			RVU_PRIV_HWVFX_NIXX_CFG(1);
314746ea742SSunil Goutham 		cfg = rvu_read64(rvu, BLKADDR_RVUM, reg | (devnum << 16));
315746ea742SSunil Goutham 		if (cfg)
3169932fb72SRakesh Babu 			blkaddr = BLKADDR_NIX1;
3179932fb72SRakesh Babu 	}
3189932fb72SRakesh Babu 
3199932fb72SRakesh Babu 	if (blktype == BLKTYPE_CPT) {
3209932fb72SRakesh Babu 		reg = is_pf ? RVU_PRIV_PFX_CPTX_CFG(0) :
3219932fb72SRakesh Babu 			RVU_PRIV_HWVFX_CPTX_CFG(0);
3229932fb72SRakesh Babu 		cfg = rvu_read64(rvu, BLKADDR_RVUM, reg | (devnum << 16));
3239932fb72SRakesh Babu 		if (cfg) {
324746ea742SSunil Goutham 			blkaddr = BLKADDR_CPT0;
3259932fb72SRakesh Babu 			goto exit;
3269932fb72SRakesh Babu 		}
3279932fb72SRakesh Babu 
3289932fb72SRakesh Babu 		reg = is_pf ? RVU_PRIV_PFX_CPTX_CFG(1) :
3299932fb72SRakesh Babu 			RVU_PRIV_HWVFX_CPTX_CFG(1);
3309932fb72SRakesh Babu 		cfg = rvu_read64(rvu, BLKADDR_RVUM, reg | (devnum << 16));
3319932fb72SRakesh Babu 		if (cfg)
3329932fb72SRakesh Babu 			blkaddr = BLKADDR_CPT1;
333746ea742SSunil Goutham 	}
334746ea742SSunil Goutham 
335746ea742SSunil Goutham exit:
336746ea742SSunil Goutham 	if (is_block_implemented(rvu->hw, blkaddr))
337746ea742SSunil Goutham 		return blkaddr;
338746ea742SSunil Goutham 	return -ENODEV;
339746ea742SSunil Goutham }
340746ea742SSunil Goutham 
rvu_update_rsrc_map(struct rvu * rvu,struct rvu_pfvf * pfvf,struct rvu_block * block,u16 pcifunc,u16 lf,bool attach)341114a767eSSunil Goutham static void rvu_update_rsrc_map(struct rvu *rvu, struct rvu_pfvf *pfvf,
342114a767eSSunil Goutham 				struct rvu_block *block, u16 pcifunc,
343114a767eSSunil Goutham 				u16 lf, bool attach)
344114a767eSSunil Goutham {
345114a767eSSunil Goutham 	int devnum, num_lfs = 0;
346114a767eSSunil Goutham 	bool is_pf;
347114a767eSSunil Goutham 	u64 reg;
348114a767eSSunil Goutham 
349114a767eSSunil Goutham 	if (lf >= block->lf.max) {
350114a767eSSunil Goutham 		dev_err(&rvu->pdev->dev,
351114a767eSSunil Goutham 			"%s: FATAL: LF %d is >= %s's max lfs i.e %d\n",
352114a767eSSunil Goutham 			__func__, lf, block->name, block->lf.max);
353114a767eSSunil Goutham 		return;
354114a767eSSunil Goutham 	}
355114a767eSSunil Goutham 
356114a767eSSunil Goutham 	/* Check if this is for a RVU PF or VF */
357114a767eSSunil Goutham 	if (pcifunc & RVU_PFVF_FUNC_MASK) {
358114a767eSSunil Goutham 		is_pf = false;
359114a767eSSunil Goutham 		devnum = rvu_get_hwvf(rvu, pcifunc);
360114a767eSSunil Goutham 	} else {
361114a767eSSunil Goutham 		is_pf = true;
362114a767eSSunil Goutham 		devnum = rvu_get_pf(pcifunc);
363114a767eSSunil Goutham 	}
364114a767eSSunil Goutham 
365114a767eSSunil Goutham 	block->fn_map[lf] = attach ? pcifunc : 0;
366114a767eSSunil Goutham 
367cdd41e87SSubbaraya Sundeep 	switch (block->addr) {
368cdd41e87SSubbaraya Sundeep 	case BLKADDR_NPA:
369114a767eSSunil Goutham 		pfvf->npalf = attach ? true : false;
370114a767eSSunil Goutham 		num_lfs = pfvf->npalf;
371114a767eSSunil Goutham 		break;
372cdd41e87SSubbaraya Sundeep 	case BLKADDR_NIX0:
373cdd41e87SSubbaraya Sundeep 	case BLKADDR_NIX1:
374114a767eSSunil Goutham 		pfvf->nixlf = attach ? true : false;
375114a767eSSunil Goutham 		num_lfs = pfvf->nixlf;
376114a767eSSunil Goutham 		break;
377cdd41e87SSubbaraya Sundeep 	case BLKADDR_SSO:
378114a767eSSunil Goutham 		attach ? pfvf->sso++ : pfvf->sso--;
379114a767eSSunil Goutham 		num_lfs = pfvf->sso;
380114a767eSSunil Goutham 		break;
381cdd41e87SSubbaraya Sundeep 	case BLKADDR_SSOW:
382114a767eSSunil Goutham 		attach ? pfvf->ssow++ : pfvf->ssow--;
383114a767eSSunil Goutham 		num_lfs = pfvf->ssow;
384114a767eSSunil Goutham 		break;
385cdd41e87SSubbaraya Sundeep 	case BLKADDR_TIM:
386114a767eSSunil Goutham 		attach ? pfvf->timlfs++ : pfvf->timlfs--;
387114a767eSSunil Goutham 		num_lfs = pfvf->timlfs;
388114a767eSSunil Goutham 		break;
389cdd41e87SSubbaraya Sundeep 	case BLKADDR_CPT0:
390114a767eSSunil Goutham 		attach ? pfvf->cptlfs++ : pfvf->cptlfs--;
391114a767eSSunil Goutham 		num_lfs = pfvf->cptlfs;
392114a767eSSunil Goutham 		break;
393cdd41e87SSubbaraya Sundeep 	case BLKADDR_CPT1:
394cdd41e87SSubbaraya Sundeep 		attach ? pfvf->cpt1_lfs++ : pfvf->cpt1_lfs--;
395cdd41e87SSubbaraya Sundeep 		num_lfs = pfvf->cpt1_lfs;
396cdd41e87SSubbaraya Sundeep 		break;
397114a767eSSunil Goutham 	}
398114a767eSSunil Goutham 
399114a767eSSunil Goutham 	reg = is_pf ? block->pf_lfcnt_reg : block->vf_lfcnt_reg;
400114a767eSSunil Goutham 	rvu_write64(rvu, BLKADDR_RVUM, reg | (devnum << 16), num_lfs);
401114a767eSSunil Goutham }
402114a767eSSunil Goutham 
rvu_get_pf(u16 pcifunc)403114a767eSSunil Goutham inline int rvu_get_pf(u16 pcifunc)
404114a767eSSunil Goutham {
405114a767eSSunil Goutham 	return (pcifunc >> RVU_PFVF_PF_SHIFT) & RVU_PFVF_PF_MASK;
406114a767eSSunil Goutham }
407114a767eSSunil Goutham 
rvu_get_pf_numvfs(struct rvu * rvu,int pf,int * numvfs,int * hwvf)408756051e2SSunil Goutham void rvu_get_pf_numvfs(struct rvu *rvu, int pf, int *numvfs, int *hwvf)
409756051e2SSunil Goutham {
410756051e2SSunil Goutham 	u64 cfg;
411756051e2SSunil Goutham 
412756051e2SSunil Goutham 	/* Get numVFs attached to this PF and first HWVF */
413756051e2SSunil Goutham 	cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_PFX_CFG(pf));
4149986066dSSubbaraya Sundeep 	if (numvfs)
415756051e2SSunil Goutham 		*numvfs = (cfg >> 12) & 0xFF;
4169986066dSSubbaraya Sundeep 	if (hwvf)
417756051e2SSunil Goutham 		*hwvf = cfg & 0xFFF;
418756051e2SSunil Goutham }
419756051e2SSunil Goutham 
rvu_get_hwvf(struct rvu * rvu,int pcifunc)4206c635f78SGeetha sowjanya int rvu_get_hwvf(struct rvu *rvu, int pcifunc)
421114a767eSSunil Goutham {
422114a767eSSunil Goutham 	int pf, func;
423114a767eSSunil Goutham 	u64 cfg;
424114a767eSSunil Goutham 
425114a767eSSunil Goutham 	pf = rvu_get_pf(pcifunc);
426114a767eSSunil Goutham 	func = pcifunc & RVU_PFVF_FUNC_MASK;
427114a767eSSunil Goutham 
428114a767eSSunil Goutham 	/* Get first HWVF attached to this PF */
429114a767eSSunil Goutham 	cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_PFX_CFG(pf));
430114a767eSSunil Goutham 
431114a767eSSunil Goutham 	return ((cfg & 0xFFF) + func - 1);
432114a767eSSunil Goutham }
433114a767eSSunil Goutham 
rvu_get_pfvf(struct rvu * rvu,int pcifunc)434114a767eSSunil Goutham struct rvu_pfvf *rvu_get_pfvf(struct rvu *rvu, int pcifunc)
435114a767eSSunil Goutham {
436114a767eSSunil Goutham 	/* Check if it is a PF or VF */
437114a767eSSunil Goutham 	if (pcifunc & RVU_PFVF_FUNC_MASK)
438114a767eSSunil Goutham 		return &rvu->hwvf[rvu_get_hwvf(rvu, pcifunc)];
439114a767eSSunil Goutham 	else
440114a767eSSunil Goutham 		return &rvu->pf[rvu_get_pf(pcifunc)];
441114a767eSSunil Goutham }
442114a767eSSunil Goutham 
is_pf_func_valid(struct rvu * rvu,u16 pcifunc)443f325d3f4SSunil Goutham static bool is_pf_func_valid(struct rvu *rvu, u16 pcifunc)
444f325d3f4SSunil Goutham {
445f325d3f4SSunil Goutham 	int pf, vf, nvfs;
446f325d3f4SSunil Goutham 	u64 cfg;
447f325d3f4SSunil Goutham 
448f325d3f4SSunil Goutham 	pf = rvu_get_pf(pcifunc);
449f325d3f4SSunil Goutham 	if (pf >= rvu->hw->total_pfs)
450f325d3f4SSunil Goutham 		return false;
451f325d3f4SSunil Goutham 
452f325d3f4SSunil Goutham 	if (!(pcifunc & RVU_PFVF_FUNC_MASK))
453f325d3f4SSunil Goutham 		return true;
454f325d3f4SSunil Goutham 
455f325d3f4SSunil Goutham 	/* Check if VF is within number of VFs attached to this PF */
456f325d3f4SSunil Goutham 	vf = (pcifunc & RVU_PFVF_FUNC_MASK) - 1;
457f325d3f4SSunil Goutham 	cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_PFX_CFG(pf));
458f325d3f4SSunil Goutham 	nvfs = (cfg >> 12) & 0xFF;
459f325d3f4SSunil Goutham 	if (vf >= nvfs)
460f325d3f4SSunil Goutham 		return false;
461f325d3f4SSunil Goutham 
462f325d3f4SSunil Goutham 	return true;
463f325d3f4SSunil Goutham }
464f325d3f4SSunil Goutham 
is_block_implemented(struct rvu_hwinfo * hw,int blkaddr)465746ea742SSunil Goutham bool is_block_implemented(struct rvu_hwinfo *hw, int blkaddr)
466746ea742SSunil Goutham {
467746ea742SSunil Goutham 	struct rvu_block *block;
468746ea742SSunil Goutham 
469746ea742SSunil Goutham 	if (blkaddr < BLKADDR_RVUM || blkaddr >= BLK_COUNT)
470746ea742SSunil Goutham 		return false;
471746ea742SSunil Goutham 
472746ea742SSunil Goutham 	block = &hw->block[blkaddr];
473746ea742SSunil Goutham 	return block->implemented;
474746ea742SSunil Goutham }
475746ea742SSunil Goutham 
rvu_check_block_implemented(struct rvu * rvu)47654d55781SSunil Goutham static void rvu_check_block_implemented(struct rvu *rvu)
47754d55781SSunil Goutham {
47854d55781SSunil Goutham 	struct rvu_hwinfo *hw = rvu->hw;
47954d55781SSunil Goutham 	struct rvu_block *block;
48054d55781SSunil Goutham 	int blkid;
48154d55781SSunil Goutham 	u64 cfg;
48254d55781SSunil Goutham 
48354d55781SSunil Goutham 	/* For each block check if 'implemented' bit is set */
48454d55781SSunil Goutham 	for (blkid = 0; blkid < BLK_COUNT; blkid++) {
48554d55781SSunil Goutham 		block = &hw->block[blkid];
48654d55781SSunil Goutham 		cfg = rvupf_read64(rvu, RVU_PF_BLOCK_ADDRX_DISC(blkid));
48754d55781SSunil Goutham 		if (cfg & BIT_ULL(11))
48854d55781SSunil Goutham 			block->implemented = true;
48954d55781SSunil Goutham 	}
49054d55781SSunil Goutham }
49154d55781SSunil Goutham 
rvu_setup_rvum_blk_revid(struct rvu * rvu)4928315f9b2SSunil Goutham static void rvu_setup_rvum_blk_revid(struct rvu *rvu)
4938315f9b2SSunil Goutham {
4948315f9b2SSunil Goutham 	rvu_write64(rvu, BLKADDR_RVUM,
4958315f9b2SSunil Goutham 		    RVU_PRIV_BLOCK_TYPEX_REV(BLKTYPE_RVUM),
4968315f9b2SSunil Goutham 		    RVU_BLK_RVUM_REVID);
4978315f9b2SSunil Goutham }
4988315f9b2SSunil Goutham 
rvu_clear_rvum_blk_revid(struct rvu * rvu)4998315f9b2SSunil Goutham static void rvu_clear_rvum_blk_revid(struct rvu *rvu)
5008315f9b2SSunil Goutham {
5018315f9b2SSunil Goutham 	rvu_write64(rvu, BLKADDR_RVUM,
5028315f9b2SSunil Goutham 		    RVU_PRIV_BLOCK_TYPEX_REV(BLKTYPE_RVUM), 0x00);
5038315f9b2SSunil Goutham }
5048315f9b2SSunil Goutham 
rvu_lf_reset(struct rvu * rvu,struct rvu_block * block,int lf)5053fa4c323SSunil Goutham int rvu_lf_reset(struct rvu *rvu, struct rvu_block *block, int lf)
5063fa4c323SSunil Goutham {
5073fa4c323SSunil Goutham 	int err;
5083fa4c323SSunil Goutham 
5093fa4c323SSunil Goutham 	if (!block->implemented)
5103fa4c323SSunil Goutham 		return 0;
5113fa4c323SSunil Goutham 
5123fa4c323SSunil Goutham 	rvu_write64(rvu, block->addr, block->lfreset_reg, lf | BIT_ULL(12));
5133fa4c323SSunil Goutham 	err = rvu_poll_reg(rvu, block->addr, block->lfreset_reg, BIT_ULL(12),
5143fa4c323SSunil Goutham 			   true);
5153fa4c323SSunil Goutham 	return err;
5163fa4c323SSunil Goutham }
5173fa4c323SSunil Goutham 
rvu_block_reset(struct rvu * rvu,int blkaddr,u64 rst_reg)51854d55781SSunil Goutham static void rvu_block_reset(struct rvu *rvu, int blkaddr, u64 rst_reg)
51954d55781SSunil Goutham {
52054d55781SSunil Goutham 	struct rvu_block *block = &rvu->hw->block[blkaddr];
521c0fa2cffSGeetha sowjanya 	int err;
52254d55781SSunil Goutham 
52354d55781SSunil Goutham 	if (!block->implemented)
52454d55781SSunil Goutham 		return;
52554d55781SSunil Goutham 
52654d55781SSunil Goutham 	rvu_write64(rvu, blkaddr, rst_reg, BIT_ULL(0));
527c0fa2cffSGeetha sowjanya 	err = rvu_poll_reg(rvu, blkaddr, rst_reg, BIT_ULL(63), true);
52803ffbc99SGeetha sowjanya 	if (err) {
52903ffbc99SGeetha sowjanya 		dev_err(rvu->dev, "HW block:%d reset timeout retrying again\n", blkaddr);
53003ffbc99SGeetha sowjanya 		while (rvu_poll_reg(rvu, blkaddr, rst_reg, BIT_ULL(63), true) == -EBUSY)
53103ffbc99SGeetha sowjanya 			;
53203ffbc99SGeetha sowjanya 	}
53354d55781SSunil Goutham }
53454d55781SSunil Goutham 
rvu_reset_all_blocks(struct rvu * rvu)53554d55781SSunil Goutham static void rvu_reset_all_blocks(struct rvu *rvu)
53654d55781SSunil Goutham {
53754d55781SSunil Goutham 	/* Do a HW reset of all RVU blocks */
53854d55781SSunil Goutham 	rvu_block_reset(rvu, BLKADDR_NPA, NPA_AF_BLK_RST);
53954d55781SSunil Goutham 	rvu_block_reset(rvu, BLKADDR_NIX0, NIX_AF_BLK_RST);
5409932fb72SRakesh Babu 	rvu_block_reset(rvu, BLKADDR_NIX1, NIX_AF_BLK_RST);
54154d55781SSunil Goutham 	rvu_block_reset(rvu, BLKADDR_NPC, NPC_AF_BLK_RST);
54254d55781SSunil Goutham 	rvu_block_reset(rvu, BLKADDR_SSO, SSO_AF_BLK_RST);
54354d55781SSunil Goutham 	rvu_block_reset(rvu, BLKADDR_TIM, TIM_AF_BLK_RST);
54454d55781SSunil Goutham 	rvu_block_reset(rvu, BLKADDR_CPT0, CPT_AF_BLK_RST);
5459932fb72SRakesh Babu 	rvu_block_reset(rvu, BLKADDR_CPT1, CPT_AF_BLK_RST);
546c5a797e0SPrakash Brahmajyosyula 	rvu_block_reset(rvu, BLKADDR_NDC_NIX0_RX, NDC_AF_BLK_RST);
547c5a797e0SPrakash Brahmajyosyula 	rvu_block_reset(rvu, BLKADDR_NDC_NIX0_TX, NDC_AF_BLK_RST);
5489932fb72SRakesh Babu 	rvu_block_reset(rvu, BLKADDR_NDC_NIX1_RX, NDC_AF_BLK_RST);
5499932fb72SRakesh Babu 	rvu_block_reset(rvu, BLKADDR_NDC_NIX1_TX, NDC_AF_BLK_RST);
550c5a797e0SPrakash Brahmajyosyula 	rvu_block_reset(rvu, BLKADDR_NDC_NPA0, NDC_AF_BLK_RST);
55154d55781SSunil Goutham }
55254d55781SSunil Goutham 
rvu_scan_block(struct rvu * rvu,struct rvu_block * block)553114a767eSSunil Goutham static void rvu_scan_block(struct rvu *rvu, struct rvu_block *block)
554114a767eSSunil Goutham {
555114a767eSSunil Goutham 	struct rvu_pfvf *pfvf;
556114a767eSSunil Goutham 	u64 cfg;
557114a767eSSunil Goutham 	int lf;
558114a767eSSunil Goutham 
559114a767eSSunil Goutham 	for (lf = 0; lf < block->lf.max; lf++) {
560114a767eSSunil Goutham 		cfg = rvu_read64(rvu, block->addr,
561114a767eSSunil Goutham 				 block->lfcfg_reg | (lf << block->lfshift));
562114a767eSSunil Goutham 		if (!(cfg & BIT_ULL(63)))
563114a767eSSunil Goutham 			continue;
564114a767eSSunil Goutham 
565114a767eSSunil Goutham 		/* Set this resource as being used */
566114a767eSSunil Goutham 		__set_bit(lf, block->lf.bmap);
567114a767eSSunil Goutham 
568114a767eSSunil Goutham 		/* Get, to whom this LF is attached */
569114a767eSSunil Goutham 		pfvf = rvu_get_pfvf(rvu, (cfg >> 8) & 0xFFFF);
570114a767eSSunil Goutham 		rvu_update_rsrc_map(rvu, pfvf, block,
571114a767eSSunil Goutham 				    (cfg >> 8) & 0xFFFF, lf, true);
572756051e2SSunil Goutham 
573756051e2SSunil Goutham 		/* Set start MSIX vector for this LF within this PF/VF */
574756051e2SSunil Goutham 		rvu_set_msix_offset(rvu, pfvf, block, lf);
575114a767eSSunil Goutham 	}
576114a767eSSunil Goutham }
577114a767eSSunil Goutham 
rvu_check_min_msix_vec(struct rvu * rvu,int nvecs,int pf,int vf)578756051e2SSunil Goutham static void rvu_check_min_msix_vec(struct rvu *rvu, int nvecs, int pf, int vf)
579756051e2SSunil Goutham {
580756051e2SSunil Goutham 	int min_vecs;
581756051e2SSunil Goutham 
582756051e2SSunil Goutham 	if (!vf)
583756051e2SSunil Goutham 		goto check_pf;
584756051e2SSunil Goutham 
585756051e2SSunil Goutham 	if (!nvecs) {
586756051e2SSunil Goutham 		dev_warn(rvu->dev,
587756051e2SSunil Goutham 			 "PF%d:VF%d is configured with zero msix vectors, %d\n",
588756051e2SSunil Goutham 			 pf, vf - 1, nvecs);
589756051e2SSunil Goutham 	}
590756051e2SSunil Goutham 	return;
591756051e2SSunil Goutham 
592756051e2SSunil Goutham check_pf:
593756051e2SSunil Goutham 	if (pf == 0)
594756051e2SSunil Goutham 		min_vecs = RVU_AF_INT_VEC_CNT + RVU_PF_INT_VEC_CNT;
595756051e2SSunil Goutham 	else
596756051e2SSunil Goutham 		min_vecs = RVU_PF_INT_VEC_CNT;
597756051e2SSunil Goutham 
598756051e2SSunil Goutham 	if (!(nvecs < min_vecs))
599756051e2SSunil Goutham 		return;
600756051e2SSunil Goutham 	dev_warn(rvu->dev,
601756051e2SSunil Goutham 		 "PF%d is configured with too few vectors, %d, min is %d\n",
602756051e2SSunil Goutham 		 pf, nvecs, min_vecs);
603756051e2SSunil Goutham }
604756051e2SSunil Goutham 
rvu_setup_msix_resources(struct rvu * rvu)605756051e2SSunil Goutham static int rvu_setup_msix_resources(struct rvu *rvu)
606756051e2SSunil Goutham {
607756051e2SSunil Goutham 	struct rvu_hwinfo *hw = rvu->hw;
608756051e2SSunil Goutham 	int pf, vf, numvfs, hwvf, err;
60934b34ee0SGeetha sowjanya 	int nvecs, offset, max_msix;
610756051e2SSunil Goutham 	struct rvu_pfvf *pfvf;
61134b34ee0SGeetha sowjanya 	u64 cfg, phy_addr;
61234b34ee0SGeetha sowjanya 	dma_addr_t iova;
613756051e2SSunil Goutham 
614756051e2SSunil Goutham 	for (pf = 0; pf < hw->total_pfs; pf++) {
615756051e2SSunil Goutham 		cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_PFX_CFG(pf));
616756051e2SSunil Goutham 		/* If PF is not enabled, nothing to do */
617756051e2SSunil Goutham 		if (!((cfg >> 20) & 0x01))
618756051e2SSunil Goutham 			continue;
619756051e2SSunil Goutham 
620756051e2SSunil Goutham 		rvu_get_pf_numvfs(rvu, pf, &numvfs, &hwvf);
621756051e2SSunil Goutham 
622756051e2SSunil Goutham 		pfvf = &rvu->pf[pf];
623756051e2SSunil Goutham 		/* Get num of MSIX vectors attached to this PF */
624756051e2SSunil Goutham 		cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_PFX_MSIX_CFG(pf));
625756051e2SSunil Goutham 		pfvf->msix.max = ((cfg >> 32) & 0xFFF) + 1;
626756051e2SSunil Goutham 		rvu_check_min_msix_vec(rvu, pfvf->msix.max, pf, 0);
627756051e2SSunil Goutham 
628756051e2SSunil Goutham 		/* Alloc msix bitmap for this PF */
629756051e2SSunil Goutham 		err = rvu_alloc_bitmap(&pfvf->msix);
630756051e2SSunil Goutham 		if (err)
631756051e2SSunil Goutham 			return err;
632756051e2SSunil Goutham 
633756051e2SSunil Goutham 		/* Allocate memory for MSIX vector to RVU block LF mapping */
634756051e2SSunil Goutham 		pfvf->msix_lfmap = devm_kcalloc(rvu->dev, pfvf->msix.max,
635756051e2SSunil Goutham 						sizeof(u16), GFP_KERNEL);
636756051e2SSunil Goutham 		if (!pfvf->msix_lfmap)
637756051e2SSunil Goutham 			return -ENOMEM;
638756051e2SSunil Goutham 
639756051e2SSunil Goutham 		/* For PF0 (AF) firmware will set msix vector offsets for
640756051e2SSunil Goutham 		 * AF, block AF and PF0_INT vectors, so jump to VFs.
641756051e2SSunil Goutham 		 */
642756051e2SSunil Goutham 		if (!pf)
643756051e2SSunil Goutham 			goto setup_vfmsix;
644756051e2SSunil Goutham 
645756051e2SSunil Goutham 		/* Set MSIX offset for PF's 'RVU_PF_INT_VEC' vectors.
646756051e2SSunil Goutham 		 * These are allocated on driver init and never freed,
647756051e2SSunil Goutham 		 * so no need to set 'msix_lfmap' for these.
648756051e2SSunil Goutham 		 */
649756051e2SSunil Goutham 		cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_PFX_INT_CFG(pf));
650756051e2SSunil Goutham 		nvecs = (cfg >> 12) & 0xFF;
651756051e2SSunil Goutham 		cfg &= ~0x7FFULL;
652756051e2SSunil Goutham 		offset = rvu_alloc_rsrc_contig(&pfvf->msix, nvecs);
653756051e2SSunil Goutham 		rvu_write64(rvu, BLKADDR_RVUM,
654756051e2SSunil Goutham 			    RVU_PRIV_PFX_INT_CFG(pf), cfg | offset);
655756051e2SSunil Goutham setup_vfmsix:
656756051e2SSunil Goutham 		/* Alloc msix bitmap for VFs */
657756051e2SSunil Goutham 		for (vf = 0; vf < numvfs; vf++) {
658756051e2SSunil Goutham 			pfvf =  &rvu->hwvf[hwvf + vf];
659756051e2SSunil Goutham 			/* Get num of MSIX vectors attached to this VF */
660756051e2SSunil Goutham 			cfg = rvu_read64(rvu, BLKADDR_RVUM,
661756051e2SSunil Goutham 					 RVU_PRIV_PFX_MSIX_CFG(pf));
662756051e2SSunil Goutham 			pfvf->msix.max = (cfg & 0xFFF) + 1;
663756051e2SSunil Goutham 			rvu_check_min_msix_vec(rvu, pfvf->msix.max, pf, vf + 1);
664756051e2SSunil Goutham 
665756051e2SSunil Goutham 			/* Alloc msix bitmap for this VF */
666756051e2SSunil Goutham 			err = rvu_alloc_bitmap(&pfvf->msix);
667756051e2SSunil Goutham 			if (err)
668756051e2SSunil Goutham 				return err;
669756051e2SSunil Goutham 
670756051e2SSunil Goutham 			pfvf->msix_lfmap =
671756051e2SSunil Goutham 				devm_kcalloc(rvu->dev, pfvf->msix.max,
672756051e2SSunil Goutham 					     sizeof(u16), GFP_KERNEL);
673756051e2SSunil Goutham 			if (!pfvf->msix_lfmap)
674756051e2SSunil Goutham 				return -ENOMEM;
675756051e2SSunil Goutham 
676756051e2SSunil Goutham 			/* Set MSIX offset for HWVF's 'RVU_VF_INT_VEC' vectors.
677756051e2SSunil Goutham 			 * These are allocated on driver init and never freed,
678756051e2SSunil Goutham 			 * so no need to set 'msix_lfmap' for these.
679756051e2SSunil Goutham 			 */
680756051e2SSunil Goutham 			cfg = rvu_read64(rvu, BLKADDR_RVUM,
681756051e2SSunil Goutham 					 RVU_PRIV_HWVFX_INT_CFG(hwvf + vf));
682756051e2SSunil Goutham 			nvecs = (cfg >> 12) & 0xFF;
683756051e2SSunil Goutham 			cfg &= ~0x7FFULL;
684756051e2SSunil Goutham 			offset = rvu_alloc_rsrc_contig(&pfvf->msix, nvecs);
685756051e2SSunil Goutham 			rvu_write64(rvu, BLKADDR_RVUM,
686756051e2SSunil Goutham 				    RVU_PRIV_HWVFX_INT_CFG(hwvf + vf),
687756051e2SSunil Goutham 				    cfg | offset);
688756051e2SSunil Goutham 		}
689756051e2SSunil Goutham 	}
690756051e2SSunil Goutham 
69134b34ee0SGeetha sowjanya 	/* HW interprets RVU_AF_MSIXTR_BASE address as an IOVA, hence
69246eb3c10SBjorn Helgaas 	 * create an IOMMU mapping for the physical address configured by
69334b34ee0SGeetha sowjanya 	 * firmware and reconfig RVU_AF_MSIXTR_BASE with IOVA.
69434b34ee0SGeetha sowjanya 	 */
69534b34ee0SGeetha sowjanya 	cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_CONST);
69634b34ee0SGeetha sowjanya 	max_msix = cfg & 0xFFFFF;
6974f4eebf2SLinu Cherian 	if (rvu->fwdata && rvu->fwdata->msixtr_base)
6984f4eebf2SLinu Cherian 		phy_addr = rvu->fwdata->msixtr_base;
6994f4eebf2SLinu Cherian 	else
70034b34ee0SGeetha sowjanya 		phy_addr = rvu_read64(rvu, BLKADDR_RVUM, RVU_AF_MSIXTR_BASE);
7014f4eebf2SLinu Cherian 
70234b34ee0SGeetha sowjanya 	iova = dma_map_resource(rvu->dev, phy_addr,
70334b34ee0SGeetha sowjanya 				max_msix * PCI_MSIX_ENTRY_SIZE,
70434b34ee0SGeetha sowjanya 				DMA_BIDIRECTIONAL, 0);
70534b34ee0SGeetha sowjanya 
70634b34ee0SGeetha sowjanya 	if (dma_mapping_error(rvu->dev, iova))
70734b34ee0SGeetha sowjanya 		return -ENOMEM;
70834b34ee0SGeetha sowjanya 
70934b34ee0SGeetha sowjanya 	rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_MSIXTR_BASE, (u64)iova);
71034b34ee0SGeetha sowjanya 	rvu->msix_base_iova = iova;
7114f4eebf2SLinu Cherian 	rvu->msixtr_base_phy = phy_addr;
71234b34ee0SGeetha sowjanya 
713756051e2SSunil Goutham 	return 0;
714756051e2SSunil Goutham }
715756051e2SSunil Goutham 
rvu_reset_msix(struct rvu * rvu)7164f4eebf2SLinu Cherian static void rvu_reset_msix(struct rvu *rvu)
7174f4eebf2SLinu Cherian {
7184f4eebf2SLinu Cherian 	/* Restore msixtr base register */
7194f4eebf2SLinu Cherian 	rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_MSIXTR_BASE,
7204f4eebf2SLinu Cherian 		    rvu->msixtr_base_phy);
7214f4eebf2SLinu Cherian }
7224f4eebf2SLinu Cherian 
rvu_free_hw_resources(struct rvu * rvu)7231054a622SSunil Goutham static void rvu_free_hw_resources(struct rvu *rvu)
7241054a622SSunil Goutham {
7251054a622SSunil Goutham 	struct rvu_hwinfo *hw = rvu->hw;
7261054a622SSunil Goutham 	struct rvu_block *block;
727756051e2SSunil Goutham 	struct rvu_pfvf  *pfvf;
72834b34ee0SGeetha sowjanya 	int id, max_msix;
72934b34ee0SGeetha sowjanya 	u64 cfg;
7301054a622SSunil Goutham 
7317a37245eSSunil Goutham 	rvu_npa_freemem(rvu);
73223923ea4SSunil Goutham 	rvu_npc_freemem(rvu);
733aba53d5dSSunil Goutham 	rvu_nix_freemem(rvu);
7347a37245eSSunil Goutham 
735756051e2SSunil Goutham 	/* Free block LF bitmaps */
7361054a622SSunil Goutham 	for (id = 0; id < BLK_COUNT; id++) {
7371054a622SSunil Goutham 		block = &hw->block[id];
7381054a622SSunil Goutham 		kfree(block->lf.bmap);
7391054a622SSunil Goutham 	}
740756051e2SSunil Goutham 
741756051e2SSunil Goutham 	/* Free MSIX bitmaps */
742756051e2SSunil Goutham 	for (id = 0; id < hw->total_pfs; id++) {
743756051e2SSunil Goutham 		pfvf = &rvu->pf[id];
744756051e2SSunil Goutham 		kfree(pfvf->msix.bmap);
745756051e2SSunil Goutham 	}
746756051e2SSunil Goutham 
747756051e2SSunil Goutham 	for (id = 0; id < hw->total_vfs; id++) {
748756051e2SSunil Goutham 		pfvf = &rvu->hwvf[id];
749756051e2SSunil Goutham 		kfree(pfvf->msix.bmap);
750756051e2SSunil Goutham 	}
75134b34ee0SGeetha sowjanya 
75234b34ee0SGeetha sowjanya 	/* Unmap MSIX vector base IOVA mapping */
75334b34ee0SGeetha sowjanya 	if (!rvu->msix_base_iova)
75434b34ee0SGeetha sowjanya 		return;
75534b34ee0SGeetha sowjanya 	cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_CONST);
75634b34ee0SGeetha sowjanya 	max_msix = cfg & 0xFFFFF;
75734b34ee0SGeetha sowjanya 	dma_unmap_resource(rvu->dev, rvu->msix_base_iova,
75834b34ee0SGeetha sowjanya 			   max_msix * PCI_MSIX_ENTRY_SIZE,
75934b34ee0SGeetha sowjanya 			   DMA_BIDIRECTIONAL, 0);
7600964fc8fSStanislaw Kardach 
7614f4eebf2SLinu Cherian 	rvu_reset_msix(rvu);
7620964fc8fSStanislaw Kardach 	mutex_destroy(&rvu->rsrc_lock);
7631054a622SSunil Goutham }
7641054a622SSunil Goutham 
rvu_setup_pfvf_macaddress(struct rvu * rvu)7654f4eebf2SLinu Cherian static void rvu_setup_pfvf_macaddress(struct rvu *rvu)
7664f4eebf2SLinu Cherian {
7674f4eebf2SLinu Cherian 	struct rvu_hwinfo *hw = rvu->hw;
7684f4eebf2SLinu Cherian 	int pf, vf, numvfs, hwvf;
7694f4eebf2SLinu Cherian 	struct rvu_pfvf *pfvf;
7704f4eebf2SLinu Cherian 	u64 *mac;
7714f4eebf2SLinu Cherian 
7724f4eebf2SLinu Cherian 	for (pf = 0; pf < hw->total_pfs; pf++) {
7734f88ed2cSHariprasad Kelam 		/* For PF0(AF), Assign MAC address to only VFs (LBKVFs) */
7744f88ed2cSHariprasad Kelam 		if (!pf)
7754f88ed2cSHariprasad Kelam 			goto lbkvf;
7764f88ed2cSHariprasad Kelam 
7774f4eebf2SLinu Cherian 		if (!is_pf_cgxmapped(rvu, pf))
7784f4eebf2SLinu Cherian 			continue;
7794f4eebf2SLinu Cherian 		/* Assign MAC address to PF */
7804f4eebf2SLinu Cherian 		pfvf = &rvu->pf[pf];
7814f4eebf2SLinu Cherian 		if (rvu->fwdata && pf < PF_MACNUM_MAX) {
7824f4eebf2SLinu Cherian 			mac = &rvu->fwdata->pf_macs[pf];
7834f4eebf2SLinu Cherian 			if (*mac)
7844f4eebf2SLinu Cherian 				u64_to_ether_addr(*mac, pfvf->mac_addr);
7854f4eebf2SLinu Cherian 			else
7864f4eebf2SLinu Cherian 				eth_random_addr(pfvf->mac_addr);
7874f4eebf2SLinu Cherian 		} else {
7884f4eebf2SLinu Cherian 			eth_random_addr(pfvf->mac_addr);
7894f4eebf2SLinu Cherian 		}
7904f88ed2cSHariprasad Kelam 		ether_addr_copy(pfvf->default_mac, pfvf->mac_addr);
7914f4eebf2SLinu Cherian 
7924f88ed2cSHariprasad Kelam lbkvf:
7934f4eebf2SLinu Cherian 		/* Assign MAC address to VFs*/
7944f4eebf2SLinu Cherian 		rvu_get_pf_numvfs(rvu, pf, &numvfs, &hwvf);
7954f4eebf2SLinu Cherian 		for (vf = 0; vf < numvfs; vf++, hwvf++) {
7964f4eebf2SLinu Cherian 			pfvf = &rvu->hwvf[hwvf];
7974f4eebf2SLinu Cherian 			if (rvu->fwdata && hwvf < VF_MACNUM_MAX) {
7984f4eebf2SLinu Cherian 				mac = &rvu->fwdata->vf_macs[hwvf];
7994f4eebf2SLinu Cherian 				if (*mac)
8004f4eebf2SLinu Cherian 					u64_to_ether_addr(*mac, pfvf->mac_addr);
8014f4eebf2SLinu Cherian 				else
8024f4eebf2SLinu Cherian 					eth_random_addr(pfvf->mac_addr);
8034f4eebf2SLinu Cherian 			} else {
8044f4eebf2SLinu Cherian 				eth_random_addr(pfvf->mac_addr);
8054f4eebf2SLinu Cherian 			}
8064f88ed2cSHariprasad Kelam 			ether_addr_copy(pfvf->default_mac, pfvf->mac_addr);
8074f4eebf2SLinu Cherian 		}
8084f4eebf2SLinu Cherian 	}
8094f4eebf2SLinu Cherian }
8104f4eebf2SLinu Cherian 
rvu_fwdata_init(struct rvu * rvu)8114f4eebf2SLinu Cherian static int rvu_fwdata_init(struct rvu *rvu)
8124f4eebf2SLinu Cherian {
8134f4eebf2SLinu Cherian 	u64 fwdbase;
8144f4eebf2SLinu Cherian 	int err;
8154f4eebf2SLinu Cherian 
8164f4eebf2SLinu Cherian 	/* Get firmware data base address */
8174f4eebf2SLinu Cherian 	err = cgx_get_fwdata_base(&fwdbase);
8184f4eebf2SLinu Cherian 	if (err)
8194f4eebf2SLinu Cherian 		goto fail;
8204f4eebf2SLinu Cherian 	rvu->fwdata = ioremap_wc(fwdbase, sizeof(struct rvu_fwdata));
8214f4eebf2SLinu Cherian 	if (!rvu->fwdata)
8224f4eebf2SLinu Cherian 		goto fail;
8234f4eebf2SLinu Cherian 	if (!is_rvu_fwdata_valid(rvu)) {
8244f4eebf2SLinu Cherian 		dev_err(rvu->dev,
8254f4eebf2SLinu Cherian 			"Mismatch in 'fwdata' struct btw kernel and firmware\n");
8264f4eebf2SLinu Cherian 		iounmap(rvu->fwdata);
8274f4eebf2SLinu Cherian 		rvu->fwdata = NULL;
8284f4eebf2SLinu Cherian 		return -EINVAL;
8294f4eebf2SLinu Cherian 	}
8304f4eebf2SLinu Cherian 	return 0;
8314f4eebf2SLinu Cherian fail:
8324f4eebf2SLinu Cherian 	dev_info(rvu->dev, "Unable to fetch 'fwdata' from firmware\n");
8334f4eebf2SLinu Cherian 	return -EIO;
8344f4eebf2SLinu Cherian }
8354f4eebf2SLinu Cherian 
rvu_fwdata_exit(struct rvu * rvu)8364f4eebf2SLinu Cherian static void rvu_fwdata_exit(struct rvu *rvu)
8374f4eebf2SLinu Cherian {
8384f4eebf2SLinu Cherian 	if (rvu->fwdata)
8394f4eebf2SLinu Cherian 		iounmap(rvu->fwdata);
8404f4eebf2SLinu Cherian }
8414f4eebf2SLinu Cherian 
rvu_setup_nix_hw_resource(struct rvu * rvu,int blkaddr)8429932fb72SRakesh Babu static int rvu_setup_nix_hw_resource(struct rvu *rvu, int blkaddr)
8439932fb72SRakesh Babu {
8449932fb72SRakesh Babu 	struct rvu_hwinfo *hw = rvu->hw;
8459932fb72SRakesh Babu 	struct rvu_block *block;
8469932fb72SRakesh Babu 	int blkid;
8479932fb72SRakesh Babu 	u64 cfg;
8489932fb72SRakesh Babu 
8499932fb72SRakesh Babu 	/* Init NIX LF's bitmap */
8509932fb72SRakesh Babu 	block = &hw->block[blkaddr];
8519932fb72SRakesh Babu 	if (!block->implemented)
8529932fb72SRakesh Babu 		return 0;
8539932fb72SRakesh Babu 	blkid = (blkaddr == BLKADDR_NIX0) ? 0 : 1;
8549932fb72SRakesh Babu 	cfg = rvu_read64(rvu, blkaddr, NIX_AF_CONST2);
8559932fb72SRakesh Babu 	block->lf.max = cfg & 0xFFF;
8569932fb72SRakesh Babu 	block->addr = blkaddr;
8579932fb72SRakesh Babu 	block->type = BLKTYPE_NIX;
8589932fb72SRakesh Babu 	block->lfshift = 8;
8599932fb72SRakesh Babu 	block->lookup_reg = NIX_AF_RVU_LF_CFG_DEBUG;
8609932fb72SRakesh Babu 	block->pf_lfcnt_reg = RVU_PRIV_PFX_NIXX_CFG(blkid);
8619932fb72SRakesh Babu 	block->vf_lfcnt_reg = RVU_PRIV_HWVFX_NIXX_CFG(blkid);
8629932fb72SRakesh Babu 	block->lfcfg_reg = NIX_PRIV_LFX_CFG;
8639932fb72SRakesh Babu 	block->msixcfg_reg = NIX_PRIV_LFX_INT_CFG;
8649932fb72SRakesh Babu 	block->lfreset_reg = NIX_AF_LF_RST;
86548260907SSrujana Challa 	block->rvu = rvu;
8669932fb72SRakesh Babu 	sprintf(block->name, "NIX%d", blkid);
867221f3dffSRakesh Babu 	rvu->nix_blkaddr[blkid] = blkaddr;
8689932fb72SRakesh Babu 	return rvu_alloc_bitmap(&block->lf);
8699932fb72SRakesh Babu }
8709932fb72SRakesh Babu 
rvu_setup_cpt_hw_resource(struct rvu * rvu,int blkaddr)8719932fb72SRakesh Babu static int rvu_setup_cpt_hw_resource(struct rvu *rvu, int blkaddr)
8729932fb72SRakesh Babu {
8739932fb72SRakesh Babu 	struct rvu_hwinfo *hw = rvu->hw;
8749932fb72SRakesh Babu 	struct rvu_block *block;
8759932fb72SRakesh Babu 	int blkid;
8769932fb72SRakesh Babu 	u64 cfg;
8779932fb72SRakesh Babu 
8789932fb72SRakesh Babu 	/* Init CPT LF's bitmap */
8799932fb72SRakesh Babu 	block = &hw->block[blkaddr];
8809932fb72SRakesh Babu 	if (!block->implemented)
8819932fb72SRakesh Babu 		return 0;
8829932fb72SRakesh Babu 	blkid = (blkaddr == BLKADDR_CPT0) ? 0 : 1;
8839932fb72SRakesh Babu 	cfg = rvu_read64(rvu, blkaddr, CPT_AF_CONSTANTS0);
8849932fb72SRakesh Babu 	block->lf.max = cfg & 0xFF;
8859932fb72SRakesh Babu 	block->addr = blkaddr;
8869932fb72SRakesh Babu 	block->type = BLKTYPE_CPT;
8879932fb72SRakesh Babu 	block->multislot = true;
8889932fb72SRakesh Babu 	block->lfshift = 3;
8899932fb72SRakesh Babu 	block->lookup_reg = CPT_AF_RVU_LF_CFG_DEBUG;
8909932fb72SRakesh Babu 	block->pf_lfcnt_reg = RVU_PRIV_PFX_CPTX_CFG(blkid);
8919932fb72SRakesh Babu 	block->vf_lfcnt_reg = RVU_PRIV_HWVFX_CPTX_CFG(blkid);
8929932fb72SRakesh Babu 	block->lfcfg_reg = CPT_PRIV_LFX_CFG;
8939932fb72SRakesh Babu 	block->msixcfg_reg = CPT_PRIV_LFX_INT_CFG;
8949932fb72SRakesh Babu 	block->lfreset_reg = CPT_AF_LF_RST;
89548260907SSrujana Challa 	block->rvu = rvu;
8969932fb72SRakesh Babu 	sprintf(block->name, "CPT%d", blkid);
8979932fb72SRakesh Babu 	return rvu_alloc_bitmap(&block->lf);
8989932fb72SRakesh Babu }
8999932fb72SRakesh Babu 
rvu_get_lbk_bufsize(struct rvu * rvu)9006e54e1c5SHariprasad Kelam static void rvu_get_lbk_bufsize(struct rvu *rvu)
9016e54e1c5SHariprasad Kelam {
9026e54e1c5SHariprasad Kelam 	struct pci_dev *pdev = NULL;
9036e54e1c5SHariprasad Kelam 	void __iomem *base;
9046e54e1c5SHariprasad Kelam 	u64 lbk_const;
9056e54e1c5SHariprasad Kelam 
9066e54e1c5SHariprasad Kelam 	pdev = pci_get_device(PCI_VENDOR_ID_CAVIUM,
9076e54e1c5SHariprasad Kelam 			      PCI_DEVID_OCTEONTX2_LBK, pdev);
9086e54e1c5SHariprasad Kelam 	if (!pdev)
9096e54e1c5SHariprasad Kelam 		return;
9106e54e1c5SHariprasad Kelam 
9116e54e1c5SHariprasad Kelam 	base = pci_ioremap_bar(pdev, 0);
9126e54e1c5SHariprasad Kelam 	if (!base)
9136e54e1c5SHariprasad Kelam 		goto err_put;
9146e54e1c5SHariprasad Kelam 
9156e54e1c5SHariprasad Kelam 	lbk_const = readq(base + LBK_CONST);
9166e54e1c5SHariprasad Kelam 
9176e54e1c5SHariprasad Kelam 	/* cache fifo size */
9186e54e1c5SHariprasad Kelam 	rvu->hw->lbk_bufsize = FIELD_GET(LBK_CONST_BUF_SIZE, lbk_const);
9196e54e1c5SHariprasad Kelam 
9206e54e1c5SHariprasad Kelam 	iounmap(base);
9216e54e1c5SHariprasad Kelam err_put:
9226e54e1c5SHariprasad Kelam 	pci_dev_put(pdev);
9236e54e1c5SHariprasad Kelam }
9246e54e1c5SHariprasad Kelam 
rvu_setup_hw_resources(struct rvu * rvu)9251054a622SSunil Goutham static int rvu_setup_hw_resources(struct rvu *rvu)
9261054a622SSunil Goutham {
9271054a622SSunil Goutham 	struct rvu_hwinfo *hw = rvu->hw;
9281054a622SSunil Goutham 	struct rvu_block *block;
929114a767eSSunil Goutham 	int blkid, err;
9301054a622SSunil Goutham 	u64 cfg;
9311054a622SSunil Goutham 
9321054a622SSunil Goutham 	/* Get HW supported max RVU PF & VF count */
9331054a622SSunil Goutham 	cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_CONST);
9341054a622SSunil Goutham 	hw->total_pfs = (cfg >> 32) & 0xFF;
9351054a622SSunil Goutham 	hw->total_vfs = (cfg >> 20) & 0xFFF;
9361054a622SSunil Goutham 	hw->max_vfs_per_pf = (cfg >> 40) & 0xFF;
9371054a622SSunil Goutham 
9381054a622SSunil Goutham 	/* Init NPA LF's bitmap */
9391054a622SSunil Goutham 	block = &hw->block[BLKADDR_NPA];
9401054a622SSunil Goutham 	if (!block->implemented)
9411054a622SSunil Goutham 		goto nix;
9421054a622SSunil Goutham 	cfg = rvu_read64(rvu, BLKADDR_NPA, NPA_AF_CONST);
9431054a622SSunil Goutham 	block->lf.max = (cfg >> 16) & 0xFFF;
9441054a622SSunil Goutham 	block->addr = BLKADDR_NPA;
945114a767eSSunil Goutham 	block->type = BLKTYPE_NPA;
9461054a622SSunil Goutham 	block->lfshift = 8;
9471054a622SSunil Goutham 	block->lookup_reg = NPA_AF_RVU_LF_CFG_DEBUG;
9481054a622SSunil Goutham 	block->pf_lfcnt_reg = RVU_PRIV_PFX_NPA_CFG;
9491054a622SSunil Goutham 	block->vf_lfcnt_reg = RVU_PRIV_HWVFX_NPA_CFG;
9501054a622SSunil Goutham 	block->lfcfg_reg = NPA_PRIV_LFX_CFG;
9511054a622SSunil Goutham 	block->msixcfg_reg = NPA_PRIV_LFX_INT_CFG;
9521054a622SSunil Goutham 	block->lfreset_reg = NPA_AF_LF_RST;
95348260907SSrujana Challa 	block->rvu = rvu;
9541054a622SSunil Goutham 	sprintf(block->name, "NPA");
9551054a622SSunil Goutham 	err = rvu_alloc_bitmap(&block->lf);
956a83bdadaSSunil Goutham 	if (err) {
957a83bdadaSSunil Goutham 		dev_err(rvu->dev,
958a83bdadaSSunil Goutham 			"%s: Failed to allocate NPA LF bitmap\n", __func__);
9591054a622SSunil Goutham 		return err;
960a83bdadaSSunil Goutham 	}
9611054a622SSunil Goutham 
9621054a622SSunil Goutham nix:
9639932fb72SRakesh Babu 	err = rvu_setup_nix_hw_resource(rvu, BLKADDR_NIX0);
964a83bdadaSSunil Goutham 	if (err) {
965a83bdadaSSunil Goutham 		dev_err(rvu->dev,
966a83bdadaSSunil Goutham 			"%s: Failed to allocate NIX0 LFs bitmap\n", __func__);
9679932fb72SRakesh Babu 		return err;
968a83bdadaSSunil Goutham 	}
969a83bdadaSSunil Goutham 
9709932fb72SRakesh Babu 	err = rvu_setup_nix_hw_resource(rvu, BLKADDR_NIX1);
971a83bdadaSSunil Goutham 	if (err) {
972a83bdadaSSunil Goutham 		dev_err(rvu->dev,
973a83bdadaSSunil Goutham 			"%s: Failed to allocate NIX1 LFs bitmap\n", __func__);
9741054a622SSunil Goutham 		return err;
975a83bdadaSSunil Goutham 	}
9761054a622SSunil Goutham 
9771054a622SSunil Goutham 	/* Init SSO group's bitmap */
9781054a622SSunil Goutham 	block = &hw->block[BLKADDR_SSO];
9791054a622SSunil Goutham 	if (!block->implemented)
9801054a622SSunil Goutham 		goto ssow;
9811054a622SSunil Goutham 	cfg = rvu_read64(rvu, BLKADDR_SSO, SSO_AF_CONST);
9821054a622SSunil Goutham 	block->lf.max = cfg & 0xFFFF;
9831054a622SSunil Goutham 	block->addr = BLKADDR_SSO;
984114a767eSSunil Goutham 	block->type = BLKTYPE_SSO;
9851054a622SSunil Goutham 	block->multislot = true;
9861054a622SSunil Goutham 	block->lfshift = 3;
9871054a622SSunil Goutham 	block->lookup_reg = SSO_AF_RVU_LF_CFG_DEBUG;
9881054a622SSunil Goutham 	block->pf_lfcnt_reg = RVU_PRIV_PFX_SSO_CFG;
9891054a622SSunil Goutham 	block->vf_lfcnt_reg = RVU_PRIV_HWVFX_SSO_CFG;
9901054a622SSunil Goutham 	block->lfcfg_reg = SSO_PRIV_LFX_HWGRP_CFG;
9911054a622SSunil Goutham 	block->msixcfg_reg = SSO_PRIV_LFX_HWGRP_INT_CFG;
9921054a622SSunil Goutham 	block->lfreset_reg = SSO_AF_LF_HWGRP_RST;
99348260907SSrujana Challa 	block->rvu = rvu;
9941054a622SSunil Goutham 	sprintf(block->name, "SSO GROUP");
9951054a622SSunil Goutham 	err = rvu_alloc_bitmap(&block->lf);
996a83bdadaSSunil Goutham 	if (err) {
997a83bdadaSSunil Goutham 		dev_err(rvu->dev,
998a83bdadaSSunil Goutham 			"%s: Failed to allocate SSO LF bitmap\n", __func__);
9991054a622SSunil Goutham 		return err;
1000a83bdadaSSunil Goutham 	}
10011054a622SSunil Goutham 
10021054a622SSunil Goutham ssow:
10031054a622SSunil Goutham 	/* Init SSO workslot's bitmap */
10041054a622SSunil Goutham 	block = &hw->block[BLKADDR_SSOW];
10051054a622SSunil Goutham 	if (!block->implemented)
10061054a622SSunil Goutham 		goto tim;
10071054a622SSunil Goutham 	block->lf.max = (cfg >> 56) & 0xFF;
10081054a622SSunil Goutham 	block->addr = BLKADDR_SSOW;
1009114a767eSSunil Goutham 	block->type = BLKTYPE_SSOW;
10101054a622SSunil Goutham 	block->multislot = true;
10111054a622SSunil Goutham 	block->lfshift = 3;
10121054a622SSunil Goutham 	block->lookup_reg = SSOW_AF_RVU_LF_HWS_CFG_DEBUG;
10131054a622SSunil Goutham 	block->pf_lfcnt_reg = RVU_PRIV_PFX_SSOW_CFG;
10141054a622SSunil Goutham 	block->vf_lfcnt_reg = RVU_PRIV_HWVFX_SSOW_CFG;
10151054a622SSunil Goutham 	block->lfcfg_reg = SSOW_PRIV_LFX_HWS_CFG;
10161054a622SSunil Goutham 	block->msixcfg_reg = SSOW_PRIV_LFX_HWS_INT_CFG;
10171054a622SSunil Goutham 	block->lfreset_reg = SSOW_AF_LF_HWS_RST;
101848260907SSrujana Challa 	block->rvu = rvu;
10191054a622SSunil Goutham 	sprintf(block->name, "SSOWS");
10201054a622SSunil Goutham 	err = rvu_alloc_bitmap(&block->lf);
1021a83bdadaSSunil Goutham 	if (err) {
1022a83bdadaSSunil Goutham 		dev_err(rvu->dev,
1023a83bdadaSSunil Goutham 			"%s: Failed to allocate SSOW LF bitmap\n", __func__);
10241054a622SSunil Goutham 		return err;
1025a83bdadaSSunil Goutham 	}
10261054a622SSunil Goutham 
10271054a622SSunil Goutham tim:
10281054a622SSunil Goutham 	/* Init TIM LF's bitmap */
10291054a622SSunil Goutham 	block = &hw->block[BLKADDR_TIM];
10301054a622SSunil Goutham 	if (!block->implemented)
10311054a622SSunil Goutham 		goto cpt;
10321054a622SSunil Goutham 	cfg = rvu_read64(rvu, BLKADDR_TIM, TIM_AF_CONST);
10331054a622SSunil Goutham 	block->lf.max = cfg & 0xFFFF;
10341054a622SSunil Goutham 	block->addr = BLKADDR_TIM;
1035114a767eSSunil Goutham 	block->type = BLKTYPE_TIM;
10361054a622SSunil Goutham 	block->multislot = true;
10371054a622SSunil Goutham 	block->lfshift = 3;
10381054a622SSunil Goutham 	block->lookup_reg = TIM_AF_RVU_LF_CFG_DEBUG;
10391054a622SSunil Goutham 	block->pf_lfcnt_reg = RVU_PRIV_PFX_TIM_CFG;
10401054a622SSunil Goutham 	block->vf_lfcnt_reg = RVU_PRIV_HWVFX_TIM_CFG;
10411054a622SSunil Goutham 	block->lfcfg_reg = TIM_PRIV_LFX_CFG;
10421054a622SSunil Goutham 	block->msixcfg_reg = TIM_PRIV_LFX_INT_CFG;
10431054a622SSunil Goutham 	block->lfreset_reg = TIM_AF_LF_RST;
104448260907SSrujana Challa 	block->rvu = rvu;
10451054a622SSunil Goutham 	sprintf(block->name, "TIM");
10461054a622SSunil Goutham 	err = rvu_alloc_bitmap(&block->lf);
1047a83bdadaSSunil Goutham 	if (err) {
1048a83bdadaSSunil Goutham 		dev_err(rvu->dev,
1049a83bdadaSSunil Goutham 			"%s: Failed to allocate TIM LF bitmap\n", __func__);
10501054a622SSunil Goutham 		return err;
1051a83bdadaSSunil Goutham 	}
10521054a622SSunil Goutham 
10531054a622SSunil Goutham cpt:
10549932fb72SRakesh Babu 	err = rvu_setup_cpt_hw_resource(rvu, BLKADDR_CPT0);
1055a83bdadaSSunil Goutham 	if (err) {
1056a83bdadaSSunil Goutham 		dev_err(rvu->dev,
1057a83bdadaSSunil Goutham 			"%s: Failed to allocate CPT0 LF bitmap\n", __func__);
10589932fb72SRakesh Babu 		return err;
1059a83bdadaSSunil Goutham 	}
10609932fb72SRakesh Babu 	err = rvu_setup_cpt_hw_resource(rvu, BLKADDR_CPT1);
1061a83bdadaSSunil Goutham 	if (err) {
1062a83bdadaSSunil Goutham 		dev_err(rvu->dev,
1063a83bdadaSSunil Goutham 			"%s: Failed to allocate CPT1 LF bitmap\n", __func__);
1064a83bdadaSSunil Goutham 		return err;
1065a83bdadaSSunil Goutham 	}
10661054a622SSunil Goutham 
1067114a767eSSunil Goutham 	/* Allocate memory for PFVF data */
1068114a767eSSunil Goutham 	rvu->pf = devm_kcalloc(rvu->dev, hw->total_pfs,
1069114a767eSSunil Goutham 			       sizeof(struct rvu_pfvf), GFP_KERNEL);
1070a83bdadaSSunil Goutham 	if (!rvu->pf) {
1071a83bdadaSSunil Goutham 		dev_err(rvu->dev,
1072a83bdadaSSunil Goutham 			"%s: Failed to allocate memory for PF's rvu_pfvf struct\n", __func__);
1073114a767eSSunil Goutham 		return -ENOMEM;
1074a83bdadaSSunil Goutham 	}
1075114a767eSSunil Goutham 
1076114a767eSSunil Goutham 	rvu->hwvf = devm_kcalloc(rvu->dev, hw->total_vfs,
1077114a767eSSunil Goutham 				 sizeof(struct rvu_pfvf), GFP_KERNEL);
1078a83bdadaSSunil Goutham 	if (!rvu->hwvf) {
1079a83bdadaSSunil Goutham 		dev_err(rvu->dev,
1080a83bdadaSSunil Goutham 			"%s: Failed to allocate memory for VF's rvu_pfvf struct\n", __func__);
1081114a767eSSunil Goutham 		return -ENOMEM;
1082a83bdadaSSunil Goutham 	}
1083114a767eSSunil Goutham 
10840964fc8fSStanislaw Kardach 	mutex_init(&rvu->rsrc_lock);
1085756051e2SSunil Goutham 
10864f4eebf2SLinu Cherian 	rvu_fwdata_init(rvu);
10874f4eebf2SLinu Cherian 
1088756051e2SSunil Goutham 	err = rvu_setup_msix_resources(rvu);
1089a83bdadaSSunil Goutham 	if (err) {
1090a83bdadaSSunil Goutham 		dev_err(rvu->dev,
1091a83bdadaSSunil Goutham 			"%s: Failed to setup MSIX resources\n", __func__);
1092756051e2SSunil Goutham 		return err;
1093a83bdadaSSunil Goutham 	}
1094756051e2SSunil Goutham 
1095114a767eSSunil Goutham 	for (blkid = 0; blkid < BLK_COUNT; blkid++) {
1096114a767eSSunil Goutham 		block = &hw->block[blkid];
1097114a767eSSunil Goutham 		if (!block->lf.bmap)
1098114a767eSSunil Goutham 			continue;
1099114a767eSSunil Goutham 
1100114a767eSSunil Goutham 		/* Allocate memory for block LF/slot to pcifunc mapping info */
1101114a767eSSunil Goutham 		block->fn_map = devm_kcalloc(rvu->dev, block->lf.max,
1102114a767eSSunil Goutham 					     sizeof(u16), GFP_KERNEL);
11034f4eebf2SLinu Cherian 		if (!block->fn_map) {
11044f4eebf2SLinu Cherian 			err = -ENOMEM;
11054f4eebf2SLinu Cherian 			goto msix_err;
11064f4eebf2SLinu Cherian 		}
1107114a767eSSunil Goutham 
1108114a767eSSunil Goutham 		/* Scan all blocks to check if low level firmware has
1109114a767eSSunil Goutham 		 * already provisioned any of the resources to a PF/VF.
1110114a767eSSunil Goutham 		 */
1111114a767eSSunil Goutham 		rvu_scan_block(rvu, block);
1112114a767eSSunil Goutham 	}
1113114a767eSSunil Goutham 
1114242da439SSubbaraya Sundeep 	err = rvu_set_channels_base(rvu);
1115242da439SSubbaraya Sundeep 	if (err)
1116242da439SSubbaraya Sundeep 		goto msix_err;
1117242da439SSubbaraya Sundeep 
111823923ea4SSunil Goutham 	err = rvu_npc_init(rvu);
1119a83bdadaSSunil Goutham 	if (err) {
1120a83bdadaSSunil Goutham 		dev_err(rvu->dev, "%s: Failed to initialize npc\n", __func__);
11214f4eebf2SLinu Cherian 		goto npc_err;
1122a83bdadaSSunil Goutham 	}
112344990aaaSLinu Cherian 
112444990aaaSLinu Cherian 	err = rvu_cgx_init(rvu);
1125a83bdadaSSunil Goutham 	if (err) {
1126a83bdadaSSunil Goutham 		dev_err(rvu->dev, "%s: Failed to initialize cgx\n", __func__);
11274f4eebf2SLinu Cherian 		goto cgx_err;
1128a83bdadaSSunil Goutham 	}
11294f4eebf2SLinu Cherian 
11303571fe07SRatheesh Kannoth 	err = rvu_npc_exact_init(rvu);
11313571fe07SRatheesh Kannoth 	if (err) {
11323571fe07SRatheesh Kannoth 		dev_err(rvu->dev, "failed to initialize exact match table\n");
11333571fe07SRatheesh Kannoth 		return err;
11343571fe07SRatheesh Kannoth 	}
11353571fe07SRatheesh Kannoth 
11364f4eebf2SLinu Cherian 	/* Assign MACs for CGX mapped functions */
11374f4eebf2SLinu Cherian 	rvu_setup_pfvf_macaddress(rvu);
113823923ea4SSunil Goutham 
11397a37245eSSunil Goutham 	err = rvu_npa_init(rvu);
1140a83bdadaSSunil Goutham 	if (err) {
1141a83bdadaSSunil Goutham 		dev_err(rvu->dev, "%s: Failed to initialize npa\n", __func__);
11424f4eebf2SLinu Cherian 		goto npa_err;
1143a83bdadaSSunil Goutham 	}
11447a37245eSSunil Goutham 
11456e54e1c5SHariprasad Kelam 	rvu_get_lbk_bufsize(rvu);
11466e54e1c5SHariprasad Kelam 
1147aba53d5dSSunil Goutham 	err = rvu_nix_init(rvu);
1148a83bdadaSSunil Goutham 	if (err) {
1149a83bdadaSSunil Goutham 		dev_err(rvu->dev, "%s: Failed to initialize nix\n", __func__);
11504f4eebf2SLinu Cherian 		goto nix_err;
1151a83bdadaSSunil Goutham 	}
1152aba53d5dSSunil Goutham 
1153fe1939bbSRadha Mohan Chintakuntla 	err = rvu_sdp_init(rvu);
1154fe1939bbSRadha Mohan Chintakuntla 	if (err) {
1155fe1939bbSRadha Mohan Chintakuntla 		dev_err(rvu->dev, "%s: Failed to initialize sdp\n", __func__);
1156fe1939bbSRadha Mohan Chintakuntla 		goto nix_err;
1157fe1939bbSRadha Mohan Chintakuntla 	}
1158fe1939bbSRadha Mohan Chintakuntla 
1159242da439SSubbaraya Sundeep 	rvu_program_channels(rvu);
1160fed89cfaSHariprasad Kelam 	cgx_start_linkup(rvu);
1161242da439SSubbaraya Sundeep 
1162ca7f49ffSGeetha sowjanya 	err = rvu_mcs_init(rvu);
1163ca7f49ffSGeetha sowjanya 	if (err) {
1164ca7f49ffSGeetha sowjanya 		dev_err(rvu->dev, "%s: Failed to initialize mcs\n", __func__);
1165ca7f49ffSGeetha sowjanya 		goto nix_err;
1166ca7f49ffSGeetha sowjanya 	}
1167ca7f49ffSGeetha sowjanya 
116841b166e5SSrujana Challa 	err = rvu_cpt_init(rvu);
116941b166e5SSrujana Challa 	if (err) {
117041b166e5SSrujana Challa 		dev_err(rvu->dev, "%s: Failed to initialize cpt\n", __func__);
117141b166e5SSrujana Challa 		goto mcs_err;
117241b166e5SSrujana Challa 	}
117341b166e5SSrujana Challa 
11747a37245eSSunil Goutham 	return 0;
117544990aaaSLinu Cherian 
117641b166e5SSrujana Challa mcs_err:
117741b166e5SSrujana Challa 	rvu_mcs_exit(rvu);
11784f4eebf2SLinu Cherian nix_err:
11794f4eebf2SLinu Cherian 	rvu_nix_freemem(rvu);
11804f4eebf2SLinu Cherian npa_err:
11814f4eebf2SLinu Cherian 	rvu_npa_freemem(rvu);
118244990aaaSLinu Cherian cgx_err:
118344990aaaSLinu Cherian 	rvu_cgx_exit(rvu);
11844f4eebf2SLinu Cherian npc_err:
11854f4eebf2SLinu Cherian 	rvu_npc_freemem(rvu);
11864f4eebf2SLinu Cherian 	rvu_fwdata_exit(rvu);
11874f4eebf2SLinu Cherian msix_err:
11884f4eebf2SLinu Cherian 	rvu_reset_msix(rvu);
118944990aaaSLinu Cherian 	return err;
11907a37245eSSunil Goutham }
11917a37245eSSunil Goutham 
11927a37245eSSunil Goutham /* NPA and NIX admin queue APIs */
rvu_aq_free(struct rvu * rvu,struct admin_queue * aq)11937a37245eSSunil Goutham void rvu_aq_free(struct rvu *rvu, struct admin_queue *aq)
11947a37245eSSunil Goutham {
11957a37245eSSunil Goutham 	if (!aq)
11967a37245eSSunil Goutham 		return;
11977a37245eSSunil Goutham 
11987a37245eSSunil Goutham 	qmem_free(rvu->dev, aq->inst);
11997a37245eSSunil Goutham 	qmem_free(rvu->dev, aq->res);
12007a37245eSSunil Goutham 	devm_kfree(rvu->dev, aq);
12017a37245eSSunil Goutham }
12027a37245eSSunil Goutham 
rvu_aq_alloc(struct rvu * rvu,struct admin_queue ** ad_queue,int qsize,int inst_size,int res_size)12037a37245eSSunil Goutham int rvu_aq_alloc(struct rvu *rvu, struct admin_queue **ad_queue,
12047a37245eSSunil Goutham 		 int qsize, int inst_size, int res_size)
12057a37245eSSunil Goutham {
12067a37245eSSunil Goutham 	struct admin_queue *aq;
12077a37245eSSunil Goutham 	int err;
12087a37245eSSunil Goutham 
12097a37245eSSunil Goutham 	*ad_queue = devm_kzalloc(rvu->dev, sizeof(*aq), GFP_KERNEL);
12107a37245eSSunil Goutham 	if (!*ad_queue)
12117a37245eSSunil Goutham 		return -ENOMEM;
12127a37245eSSunil Goutham 	aq = *ad_queue;
12137a37245eSSunil Goutham 
12147a37245eSSunil Goutham 	/* Alloc memory for instructions i.e AQ */
12157a37245eSSunil Goutham 	err = qmem_alloc(rvu->dev, &aq->inst, qsize, inst_size);
12167a37245eSSunil Goutham 	if (err) {
12177a37245eSSunil Goutham 		devm_kfree(rvu->dev, aq);
12187a37245eSSunil Goutham 		return err;
12197a37245eSSunil Goutham 	}
12207a37245eSSunil Goutham 
12217a37245eSSunil Goutham 	/* Alloc memory for results */
12227a37245eSSunil Goutham 	err = qmem_alloc(rvu->dev, &aq->res, qsize, res_size);
12237a37245eSSunil Goutham 	if (err) {
12247a37245eSSunil Goutham 		rvu_aq_free(rvu, aq);
12257a37245eSSunil Goutham 		return err;
12267a37245eSSunil Goutham 	}
12277a37245eSSunil Goutham 
12287a37245eSSunil Goutham 	spin_lock_init(&aq->lock);
12291054a622SSunil Goutham 	return 0;
12301054a622SSunil Goutham }
12311054a622SSunil Goutham 
rvu_mbox_handler_ready(struct rvu * rvu,struct msg_req * req,struct ready_msg_rsp * rsp)1232c6614738SSubbaraya Sundeep int rvu_mbox_handler_ready(struct rvu *rvu, struct msg_req *req,
12331f154625SAleksey Makarov 			   struct ready_msg_rsp *rsp)
12341f154625SAleksey Makarov {
12354f4eebf2SLinu Cherian 	if (rvu->fwdata) {
12364f4eebf2SLinu Cherian 		rsp->rclk_freq = rvu->fwdata->rclk;
12374f4eebf2SLinu Cherian 		rsp->sclk_freq = rvu->fwdata->sclk;
12384f4eebf2SLinu Cherian 	}
12391f154625SAleksey Makarov 	return 0;
12401f154625SAleksey Makarov }
12411f154625SAleksey Makarov 
1242746ea742SSunil Goutham /* Get current count of a RVU block's LF/slots
1243746ea742SSunil Goutham  * provisioned to a given RVU func.
1244746ea742SSunil Goutham  */
rvu_get_rsrc_mapcount(struct rvu_pfvf * pfvf,int blkaddr)1245cdd41e87SSubbaraya Sundeep u16 rvu_get_rsrc_mapcount(struct rvu_pfvf *pfvf, int blkaddr)
1246cdd41e87SSubbaraya Sundeep {
1247cdd41e87SSubbaraya Sundeep 	switch (blkaddr) {
1248cdd41e87SSubbaraya Sundeep 	case BLKADDR_NPA:
1249cdd41e87SSubbaraya Sundeep 		return pfvf->npalf ? 1 : 0;
1250cdd41e87SSubbaraya Sundeep 	case BLKADDR_NIX0:
1251cdd41e87SSubbaraya Sundeep 	case BLKADDR_NIX1:
1252cdd41e87SSubbaraya Sundeep 		return pfvf->nixlf ? 1 : 0;
1253cdd41e87SSubbaraya Sundeep 	case BLKADDR_SSO:
1254cdd41e87SSubbaraya Sundeep 		return pfvf->sso;
1255cdd41e87SSubbaraya Sundeep 	case BLKADDR_SSOW:
1256cdd41e87SSubbaraya Sundeep 		return pfvf->ssow;
1257cdd41e87SSubbaraya Sundeep 	case BLKADDR_TIM:
1258cdd41e87SSubbaraya Sundeep 		return pfvf->timlfs;
1259cdd41e87SSubbaraya Sundeep 	case BLKADDR_CPT0:
1260cdd41e87SSubbaraya Sundeep 		return pfvf->cptlfs;
1261cdd41e87SSubbaraya Sundeep 	case BLKADDR_CPT1:
1262cdd41e87SSubbaraya Sundeep 		return pfvf->cpt1_lfs;
1263cdd41e87SSubbaraya Sundeep 	}
1264cdd41e87SSubbaraya Sundeep 	return 0;
1265cdd41e87SSubbaraya Sundeep }
1266cdd41e87SSubbaraya Sundeep 
1267cdd41e87SSubbaraya Sundeep /* Return true if LFs of block type are attached to pcifunc */
is_blktype_attached(struct rvu_pfvf * pfvf,int blktype)1268cdd41e87SSubbaraya Sundeep static bool is_blktype_attached(struct rvu_pfvf *pfvf, int blktype)
1269746ea742SSunil Goutham {
1270746ea742SSunil Goutham 	switch (blktype) {
1271746ea742SSunil Goutham 	case BLKTYPE_NPA:
1272746ea742SSunil Goutham 		return pfvf->npalf ? 1 : 0;
1273746ea742SSunil Goutham 	case BLKTYPE_NIX:
1274746ea742SSunil Goutham 		return pfvf->nixlf ? 1 : 0;
1275746ea742SSunil Goutham 	case BLKTYPE_SSO:
1276cdd41e87SSubbaraya Sundeep 		return !!pfvf->sso;
1277746ea742SSunil Goutham 	case BLKTYPE_SSOW:
1278cdd41e87SSubbaraya Sundeep 		return !!pfvf->ssow;
1279746ea742SSunil Goutham 	case BLKTYPE_TIM:
1280cdd41e87SSubbaraya Sundeep 		return !!pfvf->timlfs;
1281746ea742SSunil Goutham 	case BLKTYPE_CPT:
1282cdd41e87SSubbaraya Sundeep 		return pfvf->cptlfs || pfvf->cpt1_lfs;
1283746ea742SSunil Goutham 	}
1284cdd41e87SSubbaraya Sundeep 
1285cdd41e87SSubbaraya Sundeep 	return false;
1286746ea742SSunil Goutham }
1287746ea742SSunil Goutham 
is_pffunc_map_valid(struct rvu * rvu,u16 pcifunc,int blktype)1288f325d3f4SSunil Goutham bool is_pffunc_map_valid(struct rvu *rvu, u16 pcifunc, int blktype)
1289f325d3f4SSunil Goutham {
1290f325d3f4SSunil Goutham 	struct rvu_pfvf *pfvf;
1291f325d3f4SSunil Goutham 
1292f325d3f4SSunil Goutham 	if (!is_pf_func_valid(rvu, pcifunc))
1293f325d3f4SSunil Goutham 		return false;
1294f325d3f4SSunil Goutham 
1295f325d3f4SSunil Goutham 	pfvf = rvu_get_pfvf(rvu, pcifunc);
1296f325d3f4SSunil Goutham 
1297f325d3f4SSunil Goutham 	/* Check if this PFFUNC has a LF of type blktype attached */
1298cdd41e87SSubbaraya Sundeep 	if (!is_blktype_attached(pfvf, blktype))
1299f325d3f4SSunil Goutham 		return false;
1300f325d3f4SSunil Goutham 
1301f325d3f4SSunil Goutham 	return true;
1302f325d3f4SSunil Goutham }
1303f325d3f4SSunil Goutham 
rvu_lookup_rsrc(struct rvu * rvu,struct rvu_block * block,int pcifunc,int slot)1304746ea742SSunil Goutham static int rvu_lookup_rsrc(struct rvu *rvu, struct rvu_block *block,
1305746ea742SSunil Goutham 			   int pcifunc, int slot)
1306746ea742SSunil Goutham {
1307746ea742SSunil Goutham 	u64 val;
1308746ea742SSunil Goutham 
1309746ea742SSunil Goutham 	val = ((u64)pcifunc << 24) | (slot << 16) | (1ULL << 13);
1310746ea742SSunil Goutham 	rvu_write64(rvu, block->addr, block->lookup_reg, val);
1311746ea742SSunil Goutham 	/* Wait for the lookup to finish */
1312746ea742SSunil Goutham 	/* TODO: put some timeout here */
1313746ea742SSunil Goutham 	while (rvu_read64(rvu, block->addr, block->lookup_reg) & (1ULL << 13))
1314746ea742SSunil Goutham 		;
1315746ea742SSunil Goutham 
1316746ea742SSunil Goutham 	val = rvu_read64(rvu, block->addr, block->lookup_reg);
1317746ea742SSunil Goutham 
1318746ea742SSunil Goutham 	/* Check LF valid bit */
1319746ea742SSunil Goutham 	if (!(val & (1ULL << 12)))
1320746ea742SSunil Goutham 		return -1;
1321746ea742SSunil Goutham 
1322746ea742SSunil Goutham 	return (val & 0xFFF);
1323746ea742SSunil Goutham }
1324746ea742SSunil Goutham 
rvu_get_blkaddr_from_slot(struct rvu * rvu,int blktype,u16 pcifunc,u16 global_slot,u16 * slot_in_block)13254b5a3ab1SSrujana Challa int rvu_get_blkaddr_from_slot(struct rvu *rvu, int blktype, u16 pcifunc,
13264b5a3ab1SSrujana Challa 			      u16 global_slot, u16 *slot_in_block)
13274b5a3ab1SSrujana Challa {
13284b5a3ab1SSrujana Challa 	struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
13294b5a3ab1SSrujana Challa 	int numlfs, total_lfs = 0, nr_blocks = 0;
13304b5a3ab1SSrujana Challa 	int i, num_blkaddr[BLK_COUNT] = { 0 };
13314b5a3ab1SSrujana Challa 	struct rvu_block *block;
13323503e673SColin Ian King 	int blkaddr;
13334b5a3ab1SSrujana Challa 	u16 start_slot;
13344b5a3ab1SSrujana Challa 
13354b5a3ab1SSrujana Challa 	if (!is_blktype_attached(pfvf, blktype))
13364b5a3ab1SSrujana Challa 		return -ENODEV;
13374b5a3ab1SSrujana Challa 
13384b5a3ab1SSrujana Challa 	/* Get all the block addresses from which LFs are attached to
13394b5a3ab1SSrujana Challa 	 * the given pcifunc in num_blkaddr[].
13404b5a3ab1SSrujana Challa 	 */
13414b5a3ab1SSrujana Challa 	for (blkaddr = BLKADDR_RVUM; blkaddr < BLK_COUNT; blkaddr++) {
13424b5a3ab1SSrujana Challa 		block = &rvu->hw->block[blkaddr];
13434b5a3ab1SSrujana Challa 		if (block->type != blktype)
13444b5a3ab1SSrujana Challa 			continue;
13454b5a3ab1SSrujana Challa 		if (!is_block_implemented(rvu->hw, blkaddr))
13464b5a3ab1SSrujana Challa 			continue;
13474b5a3ab1SSrujana Challa 
13484b5a3ab1SSrujana Challa 		numlfs = rvu_get_rsrc_mapcount(pfvf, blkaddr);
13494b5a3ab1SSrujana Challa 		if (numlfs) {
13504b5a3ab1SSrujana Challa 			total_lfs += numlfs;
13514b5a3ab1SSrujana Challa 			num_blkaddr[nr_blocks] = blkaddr;
13524b5a3ab1SSrujana Challa 			nr_blocks++;
13534b5a3ab1SSrujana Challa 		}
13544b5a3ab1SSrujana Challa 	}
13554b5a3ab1SSrujana Challa 
13564b5a3ab1SSrujana Challa 	if (global_slot >= total_lfs)
13574b5a3ab1SSrujana Challa 		return -ENODEV;
13584b5a3ab1SSrujana Challa 
13594b5a3ab1SSrujana Challa 	/* Based on the given global slot number retrieve the
13604b5a3ab1SSrujana Challa 	 * correct block address out of all attached block
13614b5a3ab1SSrujana Challa 	 * addresses and slot number in that block.
13624b5a3ab1SSrujana Challa 	 */
13634b5a3ab1SSrujana Challa 	total_lfs = 0;
13644b5a3ab1SSrujana Challa 	blkaddr = -ENODEV;
13654b5a3ab1SSrujana Challa 	for (i = 0; i < nr_blocks; i++) {
13664b5a3ab1SSrujana Challa 		numlfs = rvu_get_rsrc_mapcount(pfvf, num_blkaddr[i]);
13674b5a3ab1SSrujana Challa 		total_lfs += numlfs;
13684b5a3ab1SSrujana Challa 		if (global_slot < total_lfs) {
13694b5a3ab1SSrujana Challa 			blkaddr = num_blkaddr[i];
13704b5a3ab1SSrujana Challa 			start_slot = total_lfs - numlfs;
13714b5a3ab1SSrujana Challa 			*slot_in_block = global_slot - start_slot;
13724b5a3ab1SSrujana Challa 			break;
13734b5a3ab1SSrujana Challa 		}
13744b5a3ab1SSrujana Challa 	}
13754b5a3ab1SSrujana Challa 
13764b5a3ab1SSrujana Challa 	return blkaddr;
13774b5a3ab1SSrujana Challa }
13784b5a3ab1SSrujana Challa 
rvu_detach_block(struct rvu * rvu,int pcifunc,int blktype)1379746ea742SSunil Goutham static void rvu_detach_block(struct rvu *rvu, int pcifunc, int blktype)
1380746ea742SSunil Goutham {
1381746ea742SSunil Goutham 	struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
1382746ea742SSunil Goutham 	struct rvu_hwinfo *hw = rvu->hw;
1383746ea742SSunil Goutham 	struct rvu_block *block;
1384746ea742SSunil Goutham 	int slot, lf, num_lfs;
1385746ea742SSunil Goutham 	int blkaddr;
1386746ea742SSunil Goutham 
1387746ea742SSunil Goutham 	blkaddr = rvu_get_blkaddr(rvu, blktype, pcifunc);
1388746ea742SSunil Goutham 	if (blkaddr < 0)
1389746ea742SSunil Goutham 		return;
1390746ea742SSunil Goutham 
13914f88ed2cSHariprasad Kelam 	if (blktype == BLKTYPE_NIX)
13924f88ed2cSHariprasad Kelam 		rvu_nix_reset_mac(pfvf, pcifunc);
13934f88ed2cSHariprasad Kelam 
1394746ea742SSunil Goutham 	block = &hw->block[blkaddr];
1395746ea742SSunil Goutham 
1396cdd41e87SSubbaraya Sundeep 	num_lfs = rvu_get_rsrc_mapcount(pfvf, block->addr);
1397746ea742SSunil Goutham 	if (!num_lfs)
1398746ea742SSunil Goutham 		return;
1399746ea742SSunil Goutham 
1400746ea742SSunil Goutham 	for (slot = 0; slot < num_lfs; slot++) {
1401746ea742SSunil Goutham 		lf = rvu_lookup_rsrc(rvu, block, pcifunc, slot);
1402746ea742SSunil Goutham 		if (lf < 0) /* This should never happen */
1403746ea742SSunil Goutham 			continue;
1404746ea742SSunil Goutham 
1405746ea742SSunil Goutham 		/* Disable the LF */
1406746ea742SSunil Goutham 		rvu_write64(rvu, blkaddr, block->lfcfg_reg |
1407746ea742SSunil Goutham 			    (lf << block->lfshift), 0x00ULL);
1408746ea742SSunil Goutham 
1409746ea742SSunil Goutham 		/* Update SW maintained mapping info as well */
1410746ea742SSunil Goutham 		rvu_update_rsrc_map(rvu, pfvf, block,
1411746ea742SSunil Goutham 				    pcifunc, lf, false);
1412746ea742SSunil Goutham 
1413746ea742SSunil Goutham 		/* Free the resource */
1414746ea742SSunil Goutham 		rvu_free_rsrc(&block->lf, lf);
1415756051e2SSunil Goutham 
1416756051e2SSunil Goutham 		/* Clear MSIX vector offset for this LF */
1417756051e2SSunil Goutham 		rvu_clear_msix_offset(rvu, pfvf, block, lf);
1418746ea742SSunil Goutham 	}
1419746ea742SSunil Goutham }
1420746ea742SSunil Goutham 
rvu_detach_rsrcs(struct rvu * rvu,struct rsrc_detach * detach,u16 pcifunc)1421746ea742SSunil Goutham static int rvu_detach_rsrcs(struct rvu *rvu, struct rsrc_detach *detach,
1422746ea742SSunil Goutham 			    u16 pcifunc)
1423746ea742SSunil Goutham {
1424746ea742SSunil Goutham 	struct rvu_hwinfo *hw = rvu->hw;
1425eec6f752SYueHaibing 	bool detach_all = true;
1426746ea742SSunil Goutham 	struct rvu_block *block;
1427eec6f752SYueHaibing 	int blkid;
1428746ea742SSunil Goutham 
14290964fc8fSStanislaw Kardach 	mutex_lock(&rvu->rsrc_lock);
1430746ea742SSunil Goutham 
1431746ea742SSunil Goutham 	/* Check for partial resource detach */
1432746ea742SSunil Goutham 	if (detach && detach->partial)
1433746ea742SSunil Goutham 		detach_all = false;
1434746ea742SSunil Goutham 
1435746ea742SSunil Goutham 	/* Check for RVU block's LFs attached to this func,
1436746ea742SSunil Goutham 	 * if so, detach them.
1437746ea742SSunil Goutham 	 */
1438746ea742SSunil Goutham 	for (blkid = 0; blkid < BLK_COUNT; blkid++) {
1439746ea742SSunil Goutham 		block = &hw->block[blkid];
1440746ea742SSunil Goutham 		if (!block->lf.bmap)
1441746ea742SSunil Goutham 			continue;
1442746ea742SSunil Goutham 		if (!detach_all && detach) {
1443746ea742SSunil Goutham 			if (blkid == BLKADDR_NPA && !detach->npalf)
1444746ea742SSunil Goutham 				continue;
1445746ea742SSunil Goutham 			else if ((blkid == BLKADDR_NIX0) && !detach->nixlf)
1446746ea742SSunil Goutham 				continue;
1447a84cdceaSSubbaraya Sundeep 			else if ((blkid == BLKADDR_NIX1) && !detach->nixlf)
1448a84cdceaSSubbaraya Sundeep 				continue;
1449746ea742SSunil Goutham 			else if ((blkid == BLKADDR_SSO) && !detach->sso)
1450746ea742SSunil Goutham 				continue;
1451746ea742SSunil Goutham 			else if ((blkid == BLKADDR_SSOW) && !detach->ssow)
1452746ea742SSunil Goutham 				continue;
1453746ea742SSunil Goutham 			else if ((blkid == BLKADDR_TIM) && !detach->timlfs)
1454746ea742SSunil Goutham 				continue;
1455746ea742SSunil Goutham 			else if ((blkid == BLKADDR_CPT0) && !detach->cptlfs)
1456746ea742SSunil Goutham 				continue;
1457a84cdceaSSubbaraya Sundeep 			else if ((blkid == BLKADDR_CPT1) && !detach->cptlfs)
1458a84cdceaSSubbaraya Sundeep 				continue;
1459746ea742SSunil Goutham 		}
1460746ea742SSunil Goutham 		rvu_detach_block(rvu, pcifunc, block->type);
1461746ea742SSunil Goutham 	}
1462746ea742SSunil Goutham 
14630964fc8fSStanislaw Kardach 	mutex_unlock(&rvu->rsrc_lock);
1464746ea742SSunil Goutham 	return 0;
1465746ea742SSunil Goutham }
1466746ea742SSunil Goutham 
rvu_mbox_handler_detach_resources(struct rvu * rvu,struct rsrc_detach * detach,struct msg_rsp * rsp)1467c6614738SSubbaraya Sundeep int rvu_mbox_handler_detach_resources(struct rvu *rvu,
1468746ea742SSunil Goutham 				      struct rsrc_detach *detach,
1469746ea742SSunil Goutham 				      struct msg_rsp *rsp)
1470746ea742SSunil Goutham {
1471746ea742SSunil Goutham 	return rvu_detach_rsrcs(rvu, detach, detach->hdr.pcifunc);
1472746ea742SSunil Goutham }
1473746ea742SSunil Goutham 
rvu_get_nix_blkaddr(struct rvu * rvu,u16 pcifunc)147423109f8dSSubbaraya Sundeep int rvu_get_nix_blkaddr(struct rvu *rvu, u16 pcifunc)
1475c5a73b63SSubbaraya Sundeep {
1476c5a73b63SSubbaraya Sundeep 	struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
1477c5a73b63SSubbaraya Sundeep 	int blkaddr = BLKADDR_NIX0, vf;
1478c5a73b63SSubbaraya Sundeep 	struct rvu_pfvf *pf;
1479c5a73b63SSubbaraya Sundeep 
1480fe1939bbSRadha Mohan Chintakuntla 	pf = rvu_get_pfvf(rvu, pcifunc & ~RVU_PFVF_FUNC_MASK);
1481fe1939bbSRadha Mohan Chintakuntla 
1482c5a73b63SSubbaraya Sundeep 	/* All CGX mapped PFs are set with assigned NIX block during init */
1483c5a73b63SSubbaraya Sundeep 	if (is_pf_cgxmapped(rvu, rvu_get_pf(pcifunc))) {
1484c5a73b63SSubbaraya Sundeep 		blkaddr = pf->nix_blkaddr;
1485c5a73b63SSubbaraya Sundeep 	} else if (is_afvf(pcifunc)) {
1486c5a73b63SSubbaraya Sundeep 		vf = pcifunc - 1;
1487c5a73b63SSubbaraya Sundeep 		/* Assign NIX based on VF number. All even numbered VFs get
1488c5a73b63SSubbaraya Sundeep 		 * NIX0 and odd numbered gets NIX1
1489c5a73b63SSubbaraya Sundeep 		 */
1490c5a73b63SSubbaraya Sundeep 		blkaddr = (vf & 1) ? BLKADDR_NIX1 : BLKADDR_NIX0;
1491c5a73b63SSubbaraya Sundeep 		/* NIX1 is not present on all silicons */
1492c5a73b63SSubbaraya Sundeep 		if (!is_block_implemented(rvu->hw, BLKADDR_NIX1))
1493c5a73b63SSubbaraya Sundeep 			blkaddr = BLKADDR_NIX0;
1494c5a73b63SSubbaraya Sundeep 	}
1495c5a73b63SSubbaraya Sundeep 
1496fe1939bbSRadha Mohan Chintakuntla 	/* if SDP1 then the blkaddr is NIX1 */
1497fe1939bbSRadha Mohan Chintakuntla 	if (is_sdp_pfvf(pcifunc) && pf->sdp_info->node_id == 1)
1498fe1939bbSRadha Mohan Chintakuntla 		blkaddr = BLKADDR_NIX1;
1499fe1939bbSRadha Mohan Chintakuntla 
1500c5a73b63SSubbaraya Sundeep 	switch (blkaddr) {
1501c5a73b63SSubbaraya Sundeep 	case BLKADDR_NIX1:
1502c5a73b63SSubbaraya Sundeep 		pfvf->nix_blkaddr = BLKADDR_NIX1;
15031c1935c9SSubbaraya Sundeep 		pfvf->nix_rx_intf = NIX_INTFX_RX(1);
15041c1935c9SSubbaraya Sundeep 		pfvf->nix_tx_intf = NIX_INTFX_TX(1);
1505c5a73b63SSubbaraya Sundeep 		break;
1506c5a73b63SSubbaraya Sundeep 	case BLKADDR_NIX0:
1507c5a73b63SSubbaraya Sundeep 	default:
1508c5a73b63SSubbaraya Sundeep 		pfvf->nix_blkaddr = BLKADDR_NIX0;
15091c1935c9SSubbaraya Sundeep 		pfvf->nix_rx_intf = NIX_INTFX_RX(0);
15101c1935c9SSubbaraya Sundeep 		pfvf->nix_tx_intf = NIX_INTFX_TX(0);
1511c5a73b63SSubbaraya Sundeep 		break;
1512c5a73b63SSubbaraya Sundeep 	}
1513c5a73b63SSubbaraya Sundeep 
1514c5a73b63SSubbaraya Sundeep 	return pfvf->nix_blkaddr;
1515c5a73b63SSubbaraya Sundeep }
1516c5a73b63SSubbaraya Sundeep 
rvu_get_attach_blkaddr(struct rvu * rvu,int blktype,u16 pcifunc,struct rsrc_attach * attach)1517a84cdceaSSubbaraya Sundeep static int rvu_get_attach_blkaddr(struct rvu *rvu, int blktype,
1518a84cdceaSSubbaraya Sundeep 				  u16 pcifunc, struct rsrc_attach *attach)
1519c5a73b63SSubbaraya Sundeep {
1520c5a73b63SSubbaraya Sundeep 	int blkaddr;
1521c5a73b63SSubbaraya Sundeep 
1522c5a73b63SSubbaraya Sundeep 	switch (blktype) {
1523c5a73b63SSubbaraya Sundeep 	case BLKTYPE_NIX:
1524c5a73b63SSubbaraya Sundeep 		blkaddr = rvu_get_nix_blkaddr(rvu, pcifunc);
1525c5a73b63SSubbaraya Sundeep 		break;
1526a84cdceaSSubbaraya Sundeep 	case BLKTYPE_CPT:
1527a84cdceaSSubbaraya Sundeep 		if (attach->hdr.ver < RVU_MULTI_BLK_VER)
1528a84cdceaSSubbaraya Sundeep 			return rvu_get_blkaddr(rvu, blktype, 0);
1529a84cdceaSSubbaraya Sundeep 		blkaddr = attach->cpt_blkaddr ? attach->cpt_blkaddr :
1530a84cdceaSSubbaraya Sundeep 			  BLKADDR_CPT0;
1531a84cdceaSSubbaraya Sundeep 		if (blkaddr != BLKADDR_CPT0 && blkaddr != BLKADDR_CPT1)
1532a84cdceaSSubbaraya Sundeep 			return -ENODEV;
1533a84cdceaSSubbaraya Sundeep 		break;
1534c5a73b63SSubbaraya Sundeep 	default:
1535c5a73b63SSubbaraya Sundeep 		return rvu_get_blkaddr(rvu, blktype, 0);
1536fc6f89ddSXu Wang 	}
1537c5a73b63SSubbaraya Sundeep 
1538c5a73b63SSubbaraya Sundeep 	if (is_block_implemented(rvu->hw, blkaddr))
1539c5a73b63SSubbaraya Sundeep 		return blkaddr;
1540c5a73b63SSubbaraya Sundeep 
1541c5a73b63SSubbaraya Sundeep 	return -ENODEV;
1542c5a73b63SSubbaraya Sundeep }
1543c5a73b63SSubbaraya Sundeep 
rvu_attach_block(struct rvu * rvu,int pcifunc,int blktype,int num_lfs,struct rsrc_attach * attach)1544a84cdceaSSubbaraya Sundeep static void rvu_attach_block(struct rvu *rvu, int pcifunc, int blktype,
1545a84cdceaSSubbaraya Sundeep 			     int num_lfs, struct rsrc_attach *attach)
1546746ea742SSunil Goutham {
1547746ea742SSunil Goutham 	struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
1548746ea742SSunil Goutham 	struct rvu_hwinfo *hw = rvu->hw;
1549746ea742SSunil Goutham 	struct rvu_block *block;
1550746ea742SSunil Goutham 	int slot, lf;
1551746ea742SSunil Goutham 	int blkaddr;
1552746ea742SSunil Goutham 	u64 cfg;
1553746ea742SSunil Goutham 
1554746ea742SSunil Goutham 	if (!num_lfs)
1555746ea742SSunil Goutham 		return;
1556746ea742SSunil Goutham 
1557a84cdceaSSubbaraya Sundeep 	blkaddr = rvu_get_attach_blkaddr(rvu, blktype, pcifunc, attach);
1558746ea742SSunil Goutham 	if (blkaddr < 0)
1559746ea742SSunil Goutham 		return;
1560746ea742SSunil Goutham 
1561746ea742SSunil Goutham 	block = &hw->block[blkaddr];
1562746ea742SSunil Goutham 	if (!block->lf.bmap)
1563746ea742SSunil Goutham 		return;
1564746ea742SSunil Goutham 
1565746ea742SSunil Goutham 	for (slot = 0; slot < num_lfs; slot++) {
1566746ea742SSunil Goutham 		/* Allocate the resource */
1567746ea742SSunil Goutham 		lf = rvu_alloc_rsrc(&block->lf);
1568746ea742SSunil Goutham 		if (lf < 0)
1569746ea742SSunil Goutham 			return;
1570746ea742SSunil Goutham 
1571746ea742SSunil Goutham 		cfg = (1ULL << 63) | (pcifunc << 8) | slot;
1572746ea742SSunil Goutham 		rvu_write64(rvu, blkaddr, block->lfcfg_reg |
1573746ea742SSunil Goutham 			    (lf << block->lfshift), cfg);
1574746ea742SSunil Goutham 		rvu_update_rsrc_map(rvu, pfvf, block,
1575746ea742SSunil Goutham 				    pcifunc, lf, true);
1576756051e2SSunil Goutham 
1577756051e2SSunil Goutham 		/* Set start MSIX vector for this LF within this PF/VF */
1578756051e2SSunil Goutham 		rvu_set_msix_offset(rvu, pfvf, block, lf);
1579746ea742SSunil Goutham 	}
1580746ea742SSunil Goutham }
1581746ea742SSunil Goutham 
rvu_check_rsrc_availability(struct rvu * rvu,struct rsrc_attach * req,u16 pcifunc)1582746ea742SSunil Goutham static int rvu_check_rsrc_availability(struct rvu *rvu,
1583746ea742SSunil Goutham 				       struct rsrc_attach *req, u16 pcifunc)
1584746ea742SSunil Goutham {
1585746ea742SSunil Goutham 	struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
1586c5a73b63SSubbaraya Sundeep 	int free_lfs, mappedlfs, blkaddr;
1587746ea742SSunil Goutham 	struct rvu_hwinfo *hw = rvu->hw;
1588746ea742SSunil Goutham 	struct rvu_block *block;
1589746ea742SSunil Goutham 
1590746ea742SSunil Goutham 	/* Only one NPA LF can be attached */
1591cdd41e87SSubbaraya Sundeep 	if (req->npalf && !is_blktype_attached(pfvf, BLKTYPE_NPA)) {
1592746ea742SSunil Goutham 		block = &hw->block[BLKADDR_NPA];
1593746ea742SSunil Goutham 		free_lfs = rvu_rsrc_free_count(&block->lf);
1594746ea742SSunil Goutham 		if (!free_lfs)
1595746ea742SSunil Goutham 			goto fail;
1596746ea742SSunil Goutham 	} else if (req->npalf) {
1597746ea742SSunil Goutham 		dev_err(&rvu->pdev->dev,
1598746ea742SSunil Goutham 			"Func 0x%x: Invalid req, already has NPA\n",
1599746ea742SSunil Goutham 			 pcifunc);
1600746ea742SSunil Goutham 		return -EINVAL;
1601746ea742SSunil Goutham 	}
1602746ea742SSunil Goutham 
1603746ea742SSunil Goutham 	/* Only one NIX LF can be attached */
1604cdd41e87SSubbaraya Sundeep 	if (req->nixlf && !is_blktype_attached(pfvf, BLKTYPE_NIX)) {
1605a84cdceaSSubbaraya Sundeep 		blkaddr = rvu_get_attach_blkaddr(rvu, BLKTYPE_NIX,
1606a84cdceaSSubbaraya Sundeep 						 pcifunc, req);
1607c5a73b63SSubbaraya Sundeep 		if (blkaddr < 0)
1608c5a73b63SSubbaraya Sundeep 			return blkaddr;
1609c5a73b63SSubbaraya Sundeep 		block = &hw->block[blkaddr];
1610746ea742SSunil Goutham 		free_lfs = rvu_rsrc_free_count(&block->lf);
1611746ea742SSunil Goutham 		if (!free_lfs)
1612746ea742SSunil Goutham 			goto fail;
1613746ea742SSunil Goutham 	} else if (req->nixlf) {
1614746ea742SSunil Goutham 		dev_err(&rvu->pdev->dev,
1615746ea742SSunil Goutham 			"Func 0x%x: Invalid req, already has NIX\n",
1616746ea742SSunil Goutham 			pcifunc);
1617746ea742SSunil Goutham 		return -EINVAL;
1618746ea742SSunil Goutham 	}
1619746ea742SSunil Goutham 
1620746ea742SSunil Goutham 	if (req->sso) {
1621746ea742SSunil Goutham 		block = &hw->block[BLKADDR_SSO];
1622746ea742SSunil Goutham 		/* Is request within limits ? */
1623746ea742SSunil Goutham 		if (req->sso > block->lf.max) {
1624746ea742SSunil Goutham 			dev_err(&rvu->pdev->dev,
1625746ea742SSunil Goutham 				"Func 0x%x: Invalid SSO req, %d > max %d\n",
1626746ea742SSunil Goutham 				 pcifunc, req->sso, block->lf.max);
1627746ea742SSunil Goutham 			return -EINVAL;
1628746ea742SSunil Goutham 		}
1629cdd41e87SSubbaraya Sundeep 		mappedlfs = rvu_get_rsrc_mapcount(pfvf, block->addr);
1630746ea742SSunil Goutham 		free_lfs = rvu_rsrc_free_count(&block->lf);
1631746ea742SSunil Goutham 		/* Check if additional resources are available */
1632746ea742SSunil Goutham 		if (req->sso > mappedlfs &&
1633746ea742SSunil Goutham 		    ((req->sso - mappedlfs) > free_lfs))
1634746ea742SSunil Goutham 			goto fail;
1635746ea742SSunil Goutham 	}
1636746ea742SSunil Goutham 
1637746ea742SSunil Goutham 	if (req->ssow) {
1638746ea742SSunil Goutham 		block = &hw->block[BLKADDR_SSOW];
1639746ea742SSunil Goutham 		if (req->ssow > block->lf.max) {
1640746ea742SSunil Goutham 			dev_err(&rvu->pdev->dev,
1641746ea742SSunil Goutham 				"Func 0x%x: Invalid SSOW req, %d > max %d\n",
164213bf7ce3SAleksandr Mishin 				 pcifunc, req->ssow, block->lf.max);
1643746ea742SSunil Goutham 			return -EINVAL;
1644746ea742SSunil Goutham 		}
1645cdd41e87SSubbaraya Sundeep 		mappedlfs = rvu_get_rsrc_mapcount(pfvf, block->addr);
1646746ea742SSunil Goutham 		free_lfs = rvu_rsrc_free_count(&block->lf);
1647746ea742SSunil Goutham 		if (req->ssow > mappedlfs &&
1648746ea742SSunil Goutham 		    ((req->ssow - mappedlfs) > free_lfs))
1649746ea742SSunil Goutham 			goto fail;
1650746ea742SSunil Goutham 	}
1651746ea742SSunil Goutham 
1652746ea742SSunil Goutham 	if (req->timlfs) {
1653746ea742SSunil Goutham 		block = &hw->block[BLKADDR_TIM];
1654746ea742SSunil Goutham 		if (req->timlfs > block->lf.max) {
1655746ea742SSunil Goutham 			dev_err(&rvu->pdev->dev,
1656746ea742SSunil Goutham 				"Func 0x%x: Invalid TIMLF req, %d > max %d\n",
1657746ea742SSunil Goutham 				 pcifunc, req->timlfs, block->lf.max);
1658746ea742SSunil Goutham 			return -EINVAL;
1659746ea742SSunil Goutham 		}
1660cdd41e87SSubbaraya Sundeep 		mappedlfs = rvu_get_rsrc_mapcount(pfvf, block->addr);
1661746ea742SSunil Goutham 		free_lfs = rvu_rsrc_free_count(&block->lf);
1662746ea742SSunil Goutham 		if (req->timlfs > mappedlfs &&
1663746ea742SSunil Goutham 		    ((req->timlfs - mappedlfs) > free_lfs))
1664746ea742SSunil Goutham 			goto fail;
1665746ea742SSunil Goutham 	}
1666746ea742SSunil Goutham 
1667746ea742SSunil Goutham 	if (req->cptlfs) {
1668a84cdceaSSubbaraya Sundeep 		blkaddr = rvu_get_attach_blkaddr(rvu, BLKTYPE_CPT,
1669a84cdceaSSubbaraya Sundeep 						 pcifunc, req);
1670a84cdceaSSubbaraya Sundeep 		if (blkaddr < 0)
1671a84cdceaSSubbaraya Sundeep 			return blkaddr;
1672a84cdceaSSubbaraya Sundeep 		block = &hw->block[blkaddr];
1673746ea742SSunil Goutham 		if (req->cptlfs > block->lf.max) {
1674746ea742SSunil Goutham 			dev_err(&rvu->pdev->dev,
1675746ea742SSunil Goutham 				"Func 0x%x: Invalid CPTLF req, %d > max %d\n",
1676746ea742SSunil Goutham 				 pcifunc, req->cptlfs, block->lf.max);
1677746ea742SSunil Goutham 			return -EINVAL;
1678746ea742SSunil Goutham 		}
1679cdd41e87SSubbaraya Sundeep 		mappedlfs = rvu_get_rsrc_mapcount(pfvf, block->addr);
1680746ea742SSunil Goutham 		free_lfs = rvu_rsrc_free_count(&block->lf);
1681746ea742SSunil Goutham 		if (req->cptlfs > mappedlfs &&
1682746ea742SSunil Goutham 		    ((req->cptlfs - mappedlfs) > free_lfs))
1683746ea742SSunil Goutham 			goto fail;
1684746ea742SSunil Goutham 	}
1685746ea742SSunil Goutham 
1686746ea742SSunil Goutham 	return 0;
1687746ea742SSunil Goutham 
1688746ea742SSunil Goutham fail:
1689746ea742SSunil Goutham 	dev_info(rvu->dev, "Request for %s failed\n", block->name);
1690746ea742SSunil Goutham 	return -ENOSPC;
1691746ea742SSunil Goutham }
1692746ea742SSunil Goutham 
rvu_attach_from_same_block(struct rvu * rvu,int blktype,struct rsrc_attach * attach)1693a84cdceaSSubbaraya Sundeep static bool rvu_attach_from_same_block(struct rvu *rvu, int blktype,
1694a84cdceaSSubbaraya Sundeep 				       struct rsrc_attach *attach)
1695a84cdceaSSubbaraya Sundeep {
1696a84cdceaSSubbaraya Sundeep 	int blkaddr, num_lfs;
1697a84cdceaSSubbaraya Sundeep 
1698a84cdceaSSubbaraya Sundeep 	blkaddr = rvu_get_attach_blkaddr(rvu, blktype,
1699a84cdceaSSubbaraya Sundeep 					 attach->hdr.pcifunc, attach);
1700a84cdceaSSubbaraya Sundeep 	if (blkaddr < 0)
1701a84cdceaSSubbaraya Sundeep 		return false;
1702a84cdceaSSubbaraya Sundeep 
1703a84cdceaSSubbaraya Sundeep 	num_lfs = rvu_get_rsrc_mapcount(rvu_get_pfvf(rvu, attach->hdr.pcifunc),
1704a84cdceaSSubbaraya Sundeep 					blkaddr);
1705a84cdceaSSubbaraya Sundeep 	/* Requester already has LFs from given block ? */
1706a84cdceaSSubbaraya Sundeep 	return !!num_lfs;
1707a84cdceaSSubbaraya Sundeep }
1708a84cdceaSSubbaraya Sundeep 
rvu_mbox_handler_attach_resources(struct rvu * rvu,struct rsrc_attach * attach,struct msg_rsp * rsp)1709c6614738SSubbaraya Sundeep int rvu_mbox_handler_attach_resources(struct rvu *rvu,
1710746ea742SSunil Goutham 				      struct rsrc_attach *attach,
1711746ea742SSunil Goutham 				      struct msg_rsp *rsp)
1712746ea742SSunil Goutham {
1713746ea742SSunil Goutham 	u16 pcifunc = attach->hdr.pcifunc;
1714eec6f752SYueHaibing 	int err;
1715746ea742SSunil Goutham 
1716746ea742SSunil Goutham 	/* If first request, detach all existing attached resources */
1717746ea742SSunil Goutham 	if (!attach->modify)
1718746ea742SSunil Goutham 		rvu_detach_rsrcs(rvu, NULL, pcifunc);
1719746ea742SSunil Goutham 
17200964fc8fSStanislaw Kardach 	mutex_lock(&rvu->rsrc_lock);
1721746ea742SSunil Goutham 
1722746ea742SSunil Goutham 	/* Check if the request can be accommodated */
1723746ea742SSunil Goutham 	err = rvu_check_rsrc_availability(rvu, attach, pcifunc);
1724746ea742SSunil Goutham 	if (err)
1725746ea742SSunil Goutham 		goto exit;
1726746ea742SSunil Goutham 
1727746ea742SSunil Goutham 	/* Now attach the requested resources */
1728746ea742SSunil Goutham 	if (attach->npalf)
1729a84cdceaSSubbaraya Sundeep 		rvu_attach_block(rvu, pcifunc, BLKTYPE_NPA, 1, attach);
1730746ea742SSunil Goutham 
1731746ea742SSunil Goutham 	if (attach->nixlf)
1732a84cdceaSSubbaraya Sundeep 		rvu_attach_block(rvu, pcifunc, BLKTYPE_NIX, 1, attach);
1733746ea742SSunil Goutham 
1734746ea742SSunil Goutham 	if (attach->sso) {
1735746ea742SSunil Goutham 		/* RVU func doesn't know which exact LF or slot is attached
1736746ea742SSunil Goutham 		 * to it, it always sees as slot 0,1,2. So for a 'modify'
1737746ea742SSunil Goutham 		 * request, simply detach all existing attached LFs/slots
1738746ea742SSunil Goutham 		 * and attach a fresh.
1739746ea742SSunil Goutham 		 */
1740746ea742SSunil Goutham 		if (attach->modify)
1741746ea742SSunil Goutham 			rvu_detach_block(rvu, pcifunc, BLKTYPE_SSO);
1742a84cdceaSSubbaraya Sundeep 		rvu_attach_block(rvu, pcifunc, BLKTYPE_SSO,
1743a84cdceaSSubbaraya Sundeep 				 attach->sso, attach);
1744746ea742SSunil Goutham 	}
1745746ea742SSunil Goutham 
1746746ea742SSunil Goutham 	if (attach->ssow) {
1747746ea742SSunil Goutham 		if (attach->modify)
1748746ea742SSunil Goutham 			rvu_detach_block(rvu, pcifunc, BLKTYPE_SSOW);
1749a84cdceaSSubbaraya Sundeep 		rvu_attach_block(rvu, pcifunc, BLKTYPE_SSOW,
1750a84cdceaSSubbaraya Sundeep 				 attach->ssow, attach);
1751746ea742SSunil Goutham 	}
1752746ea742SSunil Goutham 
1753746ea742SSunil Goutham 	if (attach->timlfs) {
1754746ea742SSunil Goutham 		if (attach->modify)
1755746ea742SSunil Goutham 			rvu_detach_block(rvu, pcifunc, BLKTYPE_TIM);
1756a84cdceaSSubbaraya Sundeep 		rvu_attach_block(rvu, pcifunc, BLKTYPE_TIM,
1757a84cdceaSSubbaraya Sundeep 				 attach->timlfs, attach);
1758746ea742SSunil Goutham 	}
1759746ea742SSunil Goutham 
1760746ea742SSunil Goutham 	if (attach->cptlfs) {
1761a84cdceaSSubbaraya Sundeep 		if (attach->modify &&
1762a84cdceaSSubbaraya Sundeep 		    rvu_attach_from_same_block(rvu, BLKTYPE_CPT, attach))
1763746ea742SSunil Goutham 			rvu_detach_block(rvu, pcifunc, BLKTYPE_CPT);
1764a84cdceaSSubbaraya Sundeep 		rvu_attach_block(rvu, pcifunc, BLKTYPE_CPT,
1765a84cdceaSSubbaraya Sundeep 				 attach->cptlfs, attach);
1766746ea742SSunil Goutham 	}
1767746ea742SSunil Goutham 
1768746ea742SSunil Goutham exit:
17690964fc8fSStanislaw Kardach 	mutex_unlock(&rvu->rsrc_lock);
1770746ea742SSunil Goutham 	return err;
1771746ea742SSunil Goutham }
1772746ea742SSunil Goutham 
rvu_get_msix_offset(struct rvu * rvu,struct rvu_pfvf * pfvf,int blkaddr,int lf)1773756051e2SSunil Goutham static u16 rvu_get_msix_offset(struct rvu *rvu, struct rvu_pfvf *pfvf,
1774756051e2SSunil Goutham 			       int blkaddr, int lf)
1775756051e2SSunil Goutham {
1776756051e2SSunil Goutham 	u16 vec;
1777756051e2SSunil Goutham 
1778756051e2SSunil Goutham 	if (lf < 0)
1779756051e2SSunil Goutham 		return MSIX_VECTOR_INVALID;
1780756051e2SSunil Goutham 
1781756051e2SSunil Goutham 	for (vec = 0; vec < pfvf->msix.max; vec++) {
1782756051e2SSunil Goutham 		if (pfvf->msix_lfmap[vec] == MSIX_BLKLF(blkaddr, lf))
1783756051e2SSunil Goutham 			return vec;
1784756051e2SSunil Goutham 	}
1785756051e2SSunil Goutham 	return MSIX_VECTOR_INVALID;
1786756051e2SSunil Goutham }
1787756051e2SSunil Goutham 
rvu_set_msix_offset(struct rvu * rvu,struct rvu_pfvf * pfvf,struct rvu_block * block,int lf)1788756051e2SSunil Goutham static void rvu_set_msix_offset(struct rvu *rvu, struct rvu_pfvf *pfvf,
1789756051e2SSunil Goutham 				struct rvu_block *block, int lf)
1790756051e2SSunil Goutham {
1791756051e2SSunil Goutham 	u16 nvecs, vec, offset;
1792756051e2SSunil Goutham 	u64 cfg;
1793756051e2SSunil Goutham 
1794756051e2SSunil Goutham 	cfg = rvu_read64(rvu, block->addr, block->msixcfg_reg |
1795756051e2SSunil Goutham 			 (lf << block->lfshift));
1796756051e2SSunil Goutham 	nvecs = (cfg >> 12) & 0xFF;
1797756051e2SSunil Goutham 
1798756051e2SSunil Goutham 	/* Check and alloc MSIX vectors, must be contiguous */
1799756051e2SSunil Goutham 	if (!rvu_rsrc_check_contig(&pfvf->msix, nvecs))
1800756051e2SSunil Goutham 		return;
1801756051e2SSunil Goutham 
1802756051e2SSunil Goutham 	offset = rvu_alloc_rsrc_contig(&pfvf->msix, nvecs);
1803756051e2SSunil Goutham 
1804756051e2SSunil Goutham 	/* Config MSIX offset in LF */
1805756051e2SSunil Goutham 	rvu_write64(rvu, block->addr, block->msixcfg_reg |
1806756051e2SSunil Goutham 		    (lf << block->lfshift), (cfg & ~0x7FFULL) | offset);
1807756051e2SSunil Goutham 
1808756051e2SSunil Goutham 	/* Update the bitmap as well */
1809756051e2SSunil Goutham 	for (vec = 0; vec < nvecs; vec++)
1810756051e2SSunil Goutham 		pfvf->msix_lfmap[offset + vec] = MSIX_BLKLF(block->addr, lf);
1811756051e2SSunil Goutham }
1812756051e2SSunil Goutham 
rvu_clear_msix_offset(struct rvu * rvu,struct rvu_pfvf * pfvf,struct rvu_block * block,int lf)1813756051e2SSunil Goutham static void rvu_clear_msix_offset(struct rvu *rvu, struct rvu_pfvf *pfvf,
1814756051e2SSunil Goutham 				  struct rvu_block *block, int lf)
1815756051e2SSunil Goutham {
1816756051e2SSunil Goutham 	u16 nvecs, vec, offset;
1817756051e2SSunil Goutham 	u64 cfg;
1818756051e2SSunil Goutham 
1819756051e2SSunil Goutham 	cfg = rvu_read64(rvu, block->addr, block->msixcfg_reg |
1820756051e2SSunil Goutham 			 (lf << block->lfshift));
1821756051e2SSunil Goutham 	nvecs = (cfg >> 12) & 0xFF;
1822756051e2SSunil Goutham 
1823756051e2SSunil Goutham 	/* Clear MSIX offset in LF */
1824756051e2SSunil Goutham 	rvu_write64(rvu, block->addr, block->msixcfg_reg |
1825756051e2SSunil Goutham 		    (lf << block->lfshift), cfg & ~0x7FFULL);
1826756051e2SSunil Goutham 
1827756051e2SSunil Goutham 	offset = rvu_get_msix_offset(rvu, pfvf, block->addr, lf);
1828756051e2SSunil Goutham 
1829756051e2SSunil Goutham 	/* Update the mapping */
1830756051e2SSunil Goutham 	for (vec = 0; vec < nvecs; vec++)
1831756051e2SSunil Goutham 		pfvf->msix_lfmap[offset + vec] = 0;
1832756051e2SSunil Goutham 
1833756051e2SSunil Goutham 	/* Free the same in MSIX bitmap */
1834756051e2SSunil Goutham 	rvu_free_rsrc_contig(&pfvf->msix, nvecs, offset);
1835756051e2SSunil Goutham }
1836756051e2SSunil Goutham 
rvu_mbox_handler_msix_offset(struct rvu * rvu,struct msg_req * req,struct msix_offset_rsp * rsp)1837c6614738SSubbaraya Sundeep int rvu_mbox_handler_msix_offset(struct rvu *rvu, struct msg_req *req,
1838756051e2SSunil Goutham 				 struct msix_offset_rsp *rsp)
1839756051e2SSunil Goutham {
1840756051e2SSunil Goutham 	struct rvu_hwinfo *hw = rvu->hw;
1841756051e2SSunil Goutham 	u16 pcifunc = req->hdr.pcifunc;
1842756051e2SSunil Goutham 	struct rvu_pfvf *pfvf;
1843a84cdceaSSubbaraya Sundeep 	int lf, slot, blkaddr;
1844756051e2SSunil Goutham 
1845756051e2SSunil Goutham 	pfvf = rvu_get_pfvf(rvu, pcifunc);
1846756051e2SSunil Goutham 	if (!pfvf->msix.bmap)
1847756051e2SSunil Goutham 		return 0;
1848756051e2SSunil Goutham 
1849756051e2SSunil Goutham 	/* Set MSIX offsets for each block's LFs attached to this PF/VF */
1850756051e2SSunil Goutham 	lf = rvu_get_lf(rvu, &hw->block[BLKADDR_NPA], pcifunc, 0);
1851756051e2SSunil Goutham 	rsp->npa_msixoff = rvu_get_msix_offset(rvu, pfvf, BLKADDR_NPA, lf);
1852756051e2SSunil Goutham 
1853a84cdceaSSubbaraya Sundeep 	/* Get BLKADDR from which LFs are attached to pcifunc */
1854a84cdceaSSubbaraya Sundeep 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
1855a84cdceaSSubbaraya Sundeep 	if (blkaddr < 0) {
1856a84cdceaSSubbaraya Sundeep 		rsp->nix_msixoff = MSIX_VECTOR_INVALID;
1857a84cdceaSSubbaraya Sundeep 	} else {
1858a84cdceaSSubbaraya Sundeep 		lf = rvu_get_lf(rvu, &hw->block[blkaddr], pcifunc, 0);
1859a84cdceaSSubbaraya Sundeep 		rsp->nix_msixoff = rvu_get_msix_offset(rvu, pfvf, blkaddr, lf);
1860a84cdceaSSubbaraya Sundeep 	}
1861756051e2SSunil Goutham 
1862756051e2SSunil Goutham 	rsp->sso = pfvf->sso;
1863756051e2SSunil Goutham 	for (slot = 0; slot < rsp->sso; slot++) {
1864756051e2SSunil Goutham 		lf = rvu_get_lf(rvu, &hw->block[BLKADDR_SSO], pcifunc, slot);
1865756051e2SSunil Goutham 		rsp->sso_msixoff[slot] =
1866756051e2SSunil Goutham 			rvu_get_msix_offset(rvu, pfvf, BLKADDR_SSO, lf);
1867756051e2SSunil Goutham 	}
1868756051e2SSunil Goutham 
1869756051e2SSunil Goutham 	rsp->ssow = pfvf->ssow;
1870756051e2SSunil Goutham 	for (slot = 0; slot < rsp->ssow; slot++) {
1871756051e2SSunil Goutham 		lf = rvu_get_lf(rvu, &hw->block[BLKADDR_SSOW], pcifunc, slot);
1872756051e2SSunil Goutham 		rsp->ssow_msixoff[slot] =
1873756051e2SSunil Goutham 			rvu_get_msix_offset(rvu, pfvf, BLKADDR_SSOW, lf);
1874756051e2SSunil Goutham 	}
1875756051e2SSunil Goutham 
1876756051e2SSunil Goutham 	rsp->timlfs = pfvf->timlfs;
1877756051e2SSunil Goutham 	for (slot = 0; slot < rsp->timlfs; slot++) {
1878756051e2SSunil Goutham 		lf = rvu_get_lf(rvu, &hw->block[BLKADDR_TIM], pcifunc, slot);
1879756051e2SSunil Goutham 		rsp->timlf_msixoff[slot] =
1880756051e2SSunil Goutham 			rvu_get_msix_offset(rvu, pfvf, BLKADDR_TIM, lf);
1881756051e2SSunil Goutham 	}
1882756051e2SSunil Goutham 
1883756051e2SSunil Goutham 	rsp->cptlfs = pfvf->cptlfs;
1884756051e2SSunil Goutham 	for (slot = 0; slot < rsp->cptlfs; slot++) {
1885756051e2SSunil Goutham 		lf = rvu_get_lf(rvu, &hw->block[BLKADDR_CPT0], pcifunc, slot);
1886756051e2SSunil Goutham 		rsp->cptlf_msixoff[slot] =
1887756051e2SSunil Goutham 			rvu_get_msix_offset(rvu, pfvf, BLKADDR_CPT0, lf);
1888756051e2SSunil Goutham 	}
1889a84cdceaSSubbaraya Sundeep 
1890a84cdceaSSubbaraya Sundeep 	rsp->cpt1_lfs = pfvf->cpt1_lfs;
1891a84cdceaSSubbaraya Sundeep 	for (slot = 0; slot < rsp->cpt1_lfs; slot++) {
1892a84cdceaSSubbaraya Sundeep 		lf = rvu_get_lf(rvu, &hw->block[BLKADDR_CPT1], pcifunc, slot);
1893a84cdceaSSubbaraya Sundeep 		rsp->cpt1_lf_msixoff[slot] =
1894a84cdceaSSubbaraya Sundeep 			rvu_get_msix_offset(rvu, pfvf, BLKADDR_CPT1, lf);
1895a84cdceaSSubbaraya Sundeep 	}
1896a84cdceaSSubbaraya Sundeep 
1897756051e2SSunil Goutham 	return 0;
1898756051e2SSunil Goutham }
1899756051e2SSunil Goutham 
rvu_mbox_handler_free_rsrc_cnt(struct rvu * rvu,struct msg_req * req,struct free_rsrcs_rsp * rsp)1900275e5d17SGeorge Cherian int rvu_mbox_handler_free_rsrc_cnt(struct rvu *rvu, struct msg_req *req,
1901275e5d17SGeorge Cherian 				   struct free_rsrcs_rsp *rsp)
1902275e5d17SGeorge Cherian {
1903275e5d17SGeorge Cherian 	struct rvu_hwinfo *hw = rvu->hw;
1904275e5d17SGeorge Cherian 	struct rvu_block *block;
1905275e5d17SGeorge Cherian 	struct nix_txsch *txsch;
1906275e5d17SGeorge Cherian 	struct nix_hw *nix_hw;
1907275e5d17SGeorge Cherian 
1908275e5d17SGeorge Cherian 	mutex_lock(&rvu->rsrc_lock);
1909275e5d17SGeorge Cherian 
1910275e5d17SGeorge Cherian 	block = &hw->block[BLKADDR_NPA];
1911275e5d17SGeorge Cherian 	rsp->npa = rvu_rsrc_free_count(&block->lf);
1912275e5d17SGeorge Cherian 
1913275e5d17SGeorge Cherian 	block = &hw->block[BLKADDR_NIX0];
1914275e5d17SGeorge Cherian 	rsp->nix = rvu_rsrc_free_count(&block->lf);
1915275e5d17SGeorge Cherian 
1916275e5d17SGeorge Cherian 	block = &hw->block[BLKADDR_NIX1];
1917275e5d17SGeorge Cherian 	rsp->nix1 = rvu_rsrc_free_count(&block->lf);
1918275e5d17SGeorge Cherian 
1919275e5d17SGeorge Cherian 	block = &hw->block[BLKADDR_SSO];
1920275e5d17SGeorge Cherian 	rsp->sso = rvu_rsrc_free_count(&block->lf);
1921275e5d17SGeorge Cherian 
1922275e5d17SGeorge Cherian 	block = &hw->block[BLKADDR_SSOW];
1923275e5d17SGeorge Cherian 	rsp->ssow = rvu_rsrc_free_count(&block->lf);
1924275e5d17SGeorge Cherian 
1925275e5d17SGeorge Cherian 	block = &hw->block[BLKADDR_TIM];
1926275e5d17SGeorge Cherian 	rsp->tim = rvu_rsrc_free_count(&block->lf);
1927275e5d17SGeorge Cherian 
1928275e5d17SGeorge Cherian 	block = &hw->block[BLKADDR_CPT0];
1929275e5d17SGeorge Cherian 	rsp->cpt = rvu_rsrc_free_count(&block->lf);
1930275e5d17SGeorge Cherian 
1931275e5d17SGeorge Cherian 	block = &hw->block[BLKADDR_CPT1];
1932275e5d17SGeorge Cherian 	rsp->cpt1 = rvu_rsrc_free_count(&block->lf);
1933275e5d17SGeorge Cherian 
1934275e5d17SGeorge Cherian 	if (rvu->hw->cap.nix_fixed_txschq_mapping) {
1935275e5d17SGeorge Cherian 		rsp->schq[NIX_TXSCH_LVL_SMQ] = 1;
1936275e5d17SGeorge Cherian 		rsp->schq[NIX_TXSCH_LVL_TL4] = 1;
1937275e5d17SGeorge Cherian 		rsp->schq[NIX_TXSCH_LVL_TL3] = 1;
1938275e5d17SGeorge Cherian 		rsp->schq[NIX_TXSCH_LVL_TL2] = 1;
1939275e5d17SGeorge Cherian 		/* NIX1 */
1940275e5d17SGeorge Cherian 		if (!is_block_implemented(rvu->hw, BLKADDR_NIX1))
1941275e5d17SGeorge Cherian 			goto out;
1942275e5d17SGeorge Cherian 		rsp->schq_nix1[NIX_TXSCH_LVL_SMQ] = 1;
1943275e5d17SGeorge Cherian 		rsp->schq_nix1[NIX_TXSCH_LVL_TL4] = 1;
1944275e5d17SGeorge Cherian 		rsp->schq_nix1[NIX_TXSCH_LVL_TL3] = 1;
1945275e5d17SGeorge Cherian 		rsp->schq_nix1[NIX_TXSCH_LVL_TL2] = 1;
1946275e5d17SGeorge Cherian 	} else {
1947275e5d17SGeorge Cherian 		nix_hw = get_nix_hw(hw, BLKADDR_NIX0);
1948275e5d17SGeorge Cherian 		txsch = &nix_hw->txsch[NIX_TXSCH_LVL_SMQ];
1949275e5d17SGeorge Cherian 		rsp->schq[NIX_TXSCH_LVL_SMQ] =
1950275e5d17SGeorge Cherian 				rvu_rsrc_free_count(&txsch->schq);
1951275e5d17SGeorge Cherian 
1952275e5d17SGeorge Cherian 		txsch = &nix_hw->txsch[NIX_TXSCH_LVL_TL4];
1953275e5d17SGeorge Cherian 		rsp->schq[NIX_TXSCH_LVL_TL4] =
1954275e5d17SGeorge Cherian 				rvu_rsrc_free_count(&txsch->schq);
1955275e5d17SGeorge Cherian 
1956275e5d17SGeorge Cherian 		txsch = &nix_hw->txsch[NIX_TXSCH_LVL_TL3];
1957275e5d17SGeorge Cherian 		rsp->schq[NIX_TXSCH_LVL_TL3] =
1958275e5d17SGeorge Cherian 				rvu_rsrc_free_count(&txsch->schq);
1959275e5d17SGeorge Cherian 
1960275e5d17SGeorge Cherian 		txsch = &nix_hw->txsch[NIX_TXSCH_LVL_TL2];
1961275e5d17SGeorge Cherian 		rsp->schq[NIX_TXSCH_LVL_TL2] =
1962275e5d17SGeorge Cherian 				rvu_rsrc_free_count(&txsch->schq);
1963275e5d17SGeorge Cherian 
1964275e5d17SGeorge Cherian 		if (!is_block_implemented(rvu->hw, BLKADDR_NIX1))
1965275e5d17SGeorge Cherian 			goto out;
1966275e5d17SGeorge Cherian 
1967275e5d17SGeorge Cherian 		nix_hw = get_nix_hw(hw, BLKADDR_NIX1);
1968275e5d17SGeorge Cherian 		txsch = &nix_hw->txsch[NIX_TXSCH_LVL_SMQ];
1969275e5d17SGeorge Cherian 		rsp->schq_nix1[NIX_TXSCH_LVL_SMQ] =
1970275e5d17SGeorge Cherian 				rvu_rsrc_free_count(&txsch->schq);
1971275e5d17SGeorge Cherian 
1972275e5d17SGeorge Cherian 		txsch = &nix_hw->txsch[NIX_TXSCH_LVL_TL4];
1973275e5d17SGeorge Cherian 		rsp->schq_nix1[NIX_TXSCH_LVL_TL4] =
1974275e5d17SGeorge Cherian 				rvu_rsrc_free_count(&txsch->schq);
1975275e5d17SGeorge Cherian 
1976275e5d17SGeorge Cherian 		txsch = &nix_hw->txsch[NIX_TXSCH_LVL_TL3];
1977275e5d17SGeorge Cherian 		rsp->schq_nix1[NIX_TXSCH_LVL_TL3] =
1978275e5d17SGeorge Cherian 				rvu_rsrc_free_count(&txsch->schq);
1979275e5d17SGeorge Cherian 
1980275e5d17SGeorge Cherian 		txsch = &nix_hw->txsch[NIX_TXSCH_LVL_TL2];
1981275e5d17SGeorge Cherian 		rsp->schq_nix1[NIX_TXSCH_LVL_TL2] =
1982275e5d17SGeorge Cherian 				rvu_rsrc_free_count(&txsch->schq);
1983275e5d17SGeorge Cherian 	}
1984275e5d17SGeorge Cherian 
1985275e5d17SGeorge Cherian 	rsp->schq_nix1[NIX_TXSCH_LVL_TL1] = 1;
1986275e5d17SGeorge Cherian out:
1987275e5d17SGeorge Cherian 	rsp->schq[NIX_TXSCH_LVL_TL1] = 1;
1988275e5d17SGeorge Cherian 	mutex_unlock(&rvu->rsrc_lock);
1989275e5d17SGeorge Cherian 
1990275e5d17SGeorge Cherian 	return 0;
1991275e5d17SGeorge Cherian }
1992275e5d17SGeorge Cherian 
rvu_mbox_handler_vf_flr(struct rvu * rvu,struct msg_req * req,struct msg_rsp * rsp)1993c6614738SSubbaraya Sundeep int rvu_mbox_handler_vf_flr(struct rvu *rvu, struct msg_req *req,
1994c554f9c1SGeetha sowjanya 			    struct msg_rsp *rsp)
1995c554f9c1SGeetha sowjanya {
1996c554f9c1SGeetha sowjanya 	u16 pcifunc = req->hdr.pcifunc;
1997c554f9c1SGeetha sowjanya 	u16 vf, numvfs;
1998c554f9c1SGeetha sowjanya 	u64 cfg;
1999c554f9c1SGeetha sowjanya 
2000c554f9c1SGeetha sowjanya 	vf = pcifunc & RVU_PFVF_FUNC_MASK;
2001c554f9c1SGeetha sowjanya 	cfg = rvu_read64(rvu, BLKADDR_RVUM,
2002c554f9c1SGeetha sowjanya 			 RVU_PRIV_PFX_CFG(rvu_get_pf(pcifunc)));
2003c554f9c1SGeetha sowjanya 	numvfs = (cfg >> 12) & 0xFF;
2004c554f9c1SGeetha sowjanya 
2005c554f9c1SGeetha sowjanya 	if (vf && vf <= numvfs)
2006c554f9c1SGeetha sowjanya 		__rvu_flr_handler(rvu, pcifunc);
2007c554f9c1SGeetha sowjanya 	else
2008c554f9c1SGeetha sowjanya 		return RVU_INVALID_VF_ID;
2009c554f9c1SGeetha sowjanya 
2010c554f9c1SGeetha sowjanya 	return 0;
2011c554f9c1SGeetha sowjanya }
2012c554f9c1SGeetha sowjanya 
rvu_mbox_handler_get_hw_cap(struct rvu * rvu,struct msg_req * req,struct get_hw_cap_rsp * rsp)20135d9b976dSSunil Goutham int rvu_mbox_handler_get_hw_cap(struct rvu *rvu, struct msg_req *req,
20145d9b976dSSunil Goutham 				struct get_hw_cap_rsp *rsp)
20155d9b976dSSunil Goutham {
20165d9b976dSSunil Goutham 	struct rvu_hwinfo *hw = rvu->hw;
20175d9b976dSSunil Goutham 
20185d9b976dSSunil Goutham 	rsp->nix_fixed_txschq_mapping = hw->cap.nix_fixed_txschq_mapping;
20195d9b976dSSunil Goutham 	rsp->nix_shaping = hw->cap.nix_shaping;
202056d9f5fdSRatheesh Kannoth 	rsp->npc_hash_extract = hw->cap.npc_hash_extract;
20215d9b976dSSunil Goutham 
20225d9b976dSSunil Goutham 	return 0;
20235d9b976dSSunil Goutham }
20245d9b976dSSunil Goutham 
rvu_mbox_handler_set_vf_perm(struct rvu * rvu,struct set_vf_perm * req,struct msg_rsp * rsp)2025bd4302b8SHariprasad Kelam int rvu_mbox_handler_set_vf_perm(struct rvu *rvu, struct set_vf_perm *req,
2026bd4302b8SHariprasad Kelam 				 struct msg_rsp *rsp)
2027bd4302b8SHariprasad Kelam {
2028bd4302b8SHariprasad Kelam 	struct rvu_hwinfo *hw = rvu->hw;
2029bd4302b8SHariprasad Kelam 	u16 pcifunc = req->hdr.pcifunc;
2030bd4302b8SHariprasad Kelam 	struct rvu_pfvf *pfvf;
2031bd4302b8SHariprasad Kelam 	int blkaddr, nixlf;
2032bd4302b8SHariprasad Kelam 	u16 target;
2033bd4302b8SHariprasad Kelam 
2034bd4302b8SHariprasad Kelam 	/* Only PF can add VF permissions */
2035bd4302b8SHariprasad Kelam 	if ((pcifunc & RVU_PFVF_FUNC_MASK) || is_afvf(pcifunc))
2036bd4302b8SHariprasad Kelam 		return -EOPNOTSUPP;
2037bd4302b8SHariprasad Kelam 
2038bd4302b8SHariprasad Kelam 	target = (pcifunc & ~RVU_PFVF_FUNC_MASK) | (req->vf + 1);
2039bd4302b8SHariprasad Kelam 	pfvf = rvu_get_pfvf(rvu, target);
2040bd4302b8SHariprasad Kelam 
2041bd4302b8SHariprasad Kelam 	if (req->flags & RESET_VF_PERM) {
2042bd4302b8SHariprasad Kelam 		pfvf->flags &= RVU_CLEAR_VF_PERM;
2043bd4302b8SHariprasad Kelam 	} else if (test_bit(PF_SET_VF_TRUSTED, &pfvf->flags) ^
2044bd4302b8SHariprasad Kelam 		 (req->flags & VF_TRUSTED)) {
2045bd4302b8SHariprasad Kelam 		change_bit(PF_SET_VF_TRUSTED, &pfvf->flags);
2046bd4302b8SHariprasad Kelam 		/* disable multicast and promisc entries */
2047bd4302b8SHariprasad Kelam 		if (!test_bit(PF_SET_VF_TRUSTED, &pfvf->flags)) {
2048bd4302b8SHariprasad Kelam 			blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, target);
2049bd4302b8SHariprasad Kelam 			if (blkaddr < 0)
2050bd4302b8SHariprasad Kelam 				return 0;
2051bd4302b8SHariprasad Kelam 			nixlf = rvu_get_lf(rvu, &hw->block[blkaddr],
2052bd4302b8SHariprasad Kelam 					   target, 0);
2053bd4302b8SHariprasad Kelam 			if (nixlf < 0)
2054bd4302b8SHariprasad Kelam 				return 0;
2055bd4302b8SHariprasad Kelam 			npc_enadis_default_mce_entry(rvu, target, nixlf,
2056bd4302b8SHariprasad Kelam 						     NIXLF_ALLMULTI_ENTRY,
2057bd4302b8SHariprasad Kelam 						     false);
2058bd4302b8SHariprasad Kelam 			npc_enadis_default_mce_entry(rvu, target, nixlf,
2059bd4302b8SHariprasad Kelam 						     NIXLF_PROMISC_ENTRY,
2060bd4302b8SHariprasad Kelam 						     false);
2061bd4302b8SHariprasad Kelam 		}
2062bd4302b8SHariprasad Kelam 	}
2063bd4302b8SHariprasad Kelam 
2064bd4302b8SHariprasad Kelam 	return 0;
2065bd4302b8SHariprasad Kelam }
2066bd4302b8SHariprasad Kelam 
rvu_process_mbox_msg(struct otx2_mbox * mbox,int devid,struct mbox_msghdr * req)20679bdc47a6STomasz Duszynski static int rvu_process_mbox_msg(struct otx2_mbox *mbox, int devid,
20687304ac45SSunil Goutham 				struct mbox_msghdr *req)
20697304ac45SSunil Goutham {
20709bdc47a6STomasz Duszynski 	struct rvu *rvu = pci_get_drvdata(mbox->pdev);
20719bdc47a6STomasz Duszynski 
20727304ac45SSunil Goutham 	/* Check if valid, if not reply with a invalid msg */
20737304ac45SSunil Goutham 	if (req->sig != OTX2_MBOX_REQ_SIG)
20747304ac45SSunil Goutham 		goto bad_message;
20757304ac45SSunil Goutham 
20761f154625SAleksey Makarov 	switch (req->id) {
2077eac66686SSunil Goutham #define M(_name, _id, _fn_name, _req_type, _rsp_type)			\
20781f154625SAleksey Makarov 	case _id: {							\
20791f154625SAleksey Makarov 		struct _rsp_type *rsp;					\
20801f154625SAleksey Makarov 		int err;						\
20811f154625SAleksey Makarov 									\
20821f154625SAleksey Makarov 		rsp = (struct _rsp_type *)otx2_mbox_alloc_msg(		\
20839bdc47a6STomasz Duszynski 			mbox, devid,					\
20841f154625SAleksey Makarov 			sizeof(struct _rsp_type));			\
20859bdc47a6STomasz Duszynski 		/* some handlers should complete even if reply */	\
20869bdc47a6STomasz Duszynski 		/* could not be allocated */				\
20879bdc47a6STomasz Duszynski 		if (!rsp &&						\
20889bdc47a6STomasz Duszynski 		    _id != MBOX_MSG_DETACH_RESOURCES &&			\
20899bdc47a6STomasz Duszynski 		    _id != MBOX_MSG_NIX_TXSCH_FREE &&			\
20909bdc47a6STomasz Duszynski 		    _id != MBOX_MSG_VF_FLR)				\
20919bdc47a6STomasz Duszynski 			return -ENOMEM;					\
20921f154625SAleksey Makarov 		if (rsp) {						\
20931f154625SAleksey Makarov 			rsp->hdr.id = _id;				\
20941f154625SAleksey Makarov 			rsp->hdr.sig = OTX2_MBOX_RSP_SIG;		\
20951f154625SAleksey Makarov 			rsp->hdr.pcifunc = req->pcifunc;		\
20961f154625SAleksey Makarov 			rsp->hdr.rc = 0;				\
20971f154625SAleksey Makarov 		}							\
20981f154625SAleksey Makarov 									\
2099eac66686SSunil Goutham 		err = rvu_mbox_handler_ ## _fn_name(rvu,		\
21001f154625SAleksey Makarov 						    (struct _req_type *)req, \
21011f154625SAleksey Makarov 						    rsp);		\
21021f154625SAleksey Makarov 		if (rsp && err)						\
21031f154625SAleksey Makarov 			rsp->hdr.rc = err;				\
21041f154625SAleksey Makarov 									\
210549142d12SSubbaraya Sundeep 		trace_otx2_msg_process(mbox->pdev, _id, err);		\
21061f154625SAleksey Makarov 		return rsp ? err : -ENOMEM;				\
21071f154625SAleksey Makarov 	}
21081f154625SAleksey Makarov MBOX_MESSAGES
21091f154625SAleksey Makarov #undef M
21109bdc47a6STomasz Duszynski 
21117304ac45SSunil Goutham bad_message:
21121f154625SAleksey Makarov 	default:
21139bdc47a6STomasz Duszynski 		otx2_reply_invalid_msg(mbox, devid, req->pcifunc, req->id);
21147304ac45SSunil Goutham 		return -ENODEV;
21157304ac45SSunil Goutham 	}
21161f154625SAleksey Makarov }
21177304ac45SSunil Goutham 
__rvu_mbox_handler(struct rvu_work * mwork,int type,bool poll)2118c6354b85SSubbaraya Sundeep static void __rvu_mbox_handler(struct rvu_work *mwork, int type, bool poll)
21197304ac45SSunil Goutham {
21207304ac45SSunil Goutham 	struct rvu *rvu = mwork->rvu;
21219bdc47a6STomasz Duszynski 	int offset, err, id, devid;
21227304ac45SSunil Goutham 	struct otx2_mbox_dev *mdev;
21237304ac45SSunil Goutham 	struct mbox_hdr *req_hdr;
21247304ac45SSunil Goutham 	struct mbox_msghdr *msg;
21259bdc47a6STomasz Duszynski 	struct mbox_wq_info *mw;
21267304ac45SSunil Goutham 	struct otx2_mbox *mbox;
21277304ac45SSunil Goutham 
21289bdc47a6STomasz Duszynski 	switch (type) {
21299bdc47a6STomasz Duszynski 	case TYPE_AFPF:
21309bdc47a6STomasz Duszynski 		mw = &rvu->afpf_wq_info;
21319bdc47a6STomasz Duszynski 		break;
21329bdc47a6STomasz Duszynski 	case TYPE_AFVF:
21339bdc47a6STomasz Duszynski 		mw = &rvu->afvf_wq_info;
21349bdc47a6STomasz Duszynski 		break;
21359bdc47a6STomasz Duszynski 	default:
21369bdc47a6STomasz Duszynski 		return;
21379bdc47a6STomasz Duszynski 	}
21389bdc47a6STomasz Duszynski 
21399bdc47a6STomasz Duszynski 	devid = mwork - mw->mbox_wrk;
21409bdc47a6STomasz Duszynski 	mbox = &mw->mbox;
21419bdc47a6STomasz Duszynski 	mdev = &mbox->dev[devid];
21427304ac45SSunil Goutham 
21437304ac45SSunil Goutham 	/* Process received mbox messages */
21447304ac45SSunil Goutham 	req_hdr = mdev->mbase + mbox->rx_start;
2145a36740f6SSunil Goutham 	if (mw->mbox_wrk[devid].num_msgs == 0)
21467304ac45SSunil Goutham 		return;
21477304ac45SSunil Goutham 
21487304ac45SSunil Goutham 	offset = mbox->rx_start + ALIGN(sizeof(*req_hdr), MBOX_MSG_ALIGN);
21497304ac45SSunil Goutham 
2150a36740f6SSunil Goutham 	for (id = 0; id < mw->mbox_wrk[devid].num_msgs; id++) {
21517304ac45SSunil Goutham 		msg = mdev->mbase + offset;
21527304ac45SSunil Goutham 
21539bdc47a6STomasz Duszynski 		/* Set which PF/VF sent this message based on mbox IRQ */
21549bdc47a6STomasz Duszynski 		switch (type) {
21559bdc47a6STomasz Duszynski 		case TYPE_AFPF:
21569bdc47a6STomasz Duszynski 			msg->pcifunc &=
21579bdc47a6STomasz Duszynski 				~(RVU_PFVF_PF_MASK << RVU_PFVF_PF_SHIFT);
21589bdc47a6STomasz Duszynski 			msg->pcifunc |= (devid << RVU_PFVF_PF_SHIFT);
21599bdc47a6STomasz Duszynski 			break;
21609bdc47a6STomasz Duszynski 		case TYPE_AFVF:
21619bdc47a6STomasz Duszynski 			msg->pcifunc &=
21629bdc47a6STomasz Duszynski 				~(RVU_PFVF_FUNC_MASK << RVU_PFVF_FUNC_SHIFT);
21639bdc47a6STomasz Duszynski 			msg->pcifunc |= (devid << RVU_PFVF_FUNC_SHIFT) + 1;
21649bdc47a6STomasz Duszynski 			break;
21659bdc47a6STomasz Duszynski 		}
21669bdc47a6STomasz Duszynski 
21679bdc47a6STomasz Duszynski 		err = rvu_process_mbox_msg(mbox, devid, msg);
21687304ac45SSunil Goutham 		if (!err) {
21697304ac45SSunil Goutham 			offset = mbox->rx_start + msg->next_msgoff;
21707304ac45SSunil Goutham 			continue;
21717304ac45SSunil Goutham 		}
21727304ac45SSunil Goutham 
21737304ac45SSunil Goutham 		if (msg->pcifunc & RVU_PFVF_FUNC_MASK)
21747304ac45SSunil Goutham 			dev_warn(rvu->dev, "Error %d when processing message %s (0x%x) from PF%d:VF%d\n",
21759bdc47a6STomasz Duszynski 				 err, otx2_mbox_id2name(msg->id),
2176a36740f6SSunil Goutham 				 msg->id, rvu_get_pf(msg->pcifunc),
21777304ac45SSunil Goutham 				 (msg->pcifunc & RVU_PFVF_FUNC_MASK) - 1);
21787304ac45SSunil Goutham 		else
21797304ac45SSunil Goutham 			dev_warn(rvu->dev, "Error %d when processing message %s (0x%x) from PF%d\n",
21809bdc47a6STomasz Duszynski 				 err, otx2_mbox_id2name(msg->id),
21819bdc47a6STomasz Duszynski 				 msg->id, devid);
21827304ac45SSunil Goutham 	}
2183a36740f6SSunil Goutham 	mw->mbox_wrk[devid].num_msgs = 0;
21847304ac45SSunil Goutham 
2185c6354b85SSubbaraya Sundeep 	if (poll)
2186c6354b85SSubbaraya Sundeep 		otx2_mbox_wait_for_zero(mbox, devid);
2187c6354b85SSubbaraya Sundeep 
21889bdc47a6STomasz Duszynski 	/* Send mbox responses to VF/PF */
21899bdc47a6STomasz Duszynski 	otx2_mbox_msg_send(mbox, devid);
21907304ac45SSunil Goutham }
21917304ac45SSunil Goutham 
rvu_afpf_mbox_handler(struct work_struct * work)21929bdc47a6STomasz Duszynski static inline void rvu_afpf_mbox_handler(struct work_struct *work)
219361071a87SLinu Cherian {
219461071a87SLinu Cherian 	struct rvu_work *mwork = container_of(work, struct rvu_work, work);
2195c6354b85SSubbaraya Sundeep 	struct rvu *rvu = mwork->rvu;
21969bdc47a6STomasz Duszynski 
2197c6354b85SSubbaraya Sundeep 	mutex_lock(&rvu->mbox_lock);
2198c6354b85SSubbaraya Sundeep 	__rvu_mbox_handler(mwork, TYPE_AFPF, true);
2199c6354b85SSubbaraya Sundeep 	mutex_unlock(&rvu->mbox_lock);
22009bdc47a6STomasz Duszynski }
22019bdc47a6STomasz Duszynski 
rvu_afvf_mbox_handler(struct work_struct * work)22029bdc47a6STomasz Duszynski static inline void rvu_afvf_mbox_handler(struct work_struct *work)
22039bdc47a6STomasz Duszynski {
22049bdc47a6STomasz Duszynski 	struct rvu_work *mwork = container_of(work, struct rvu_work, work);
22059bdc47a6STomasz Duszynski 
2206c6354b85SSubbaraya Sundeep 	__rvu_mbox_handler(mwork, TYPE_AFVF, false);
22079bdc47a6STomasz Duszynski }
22089bdc47a6STomasz Duszynski 
__rvu_mbox_up_handler(struct rvu_work * mwork,int type)22099bdc47a6STomasz Duszynski static void __rvu_mbox_up_handler(struct rvu_work *mwork, int type)
22109bdc47a6STomasz Duszynski {
221161071a87SLinu Cherian 	struct rvu *rvu = mwork->rvu;
221261071a87SLinu Cherian 	struct otx2_mbox_dev *mdev;
221361071a87SLinu Cherian 	struct mbox_hdr *rsp_hdr;
221461071a87SLinu Cherian 	struct mbox_msghdr *msg;
22159bdc47a6STomasz Duszynski 	struct mbox_wq_info *mw;
221661071a87SLinu Cherian 	struct otx2_mbox *mbox;
22179bdc47a6STomasz Duszynski 	int offset, id, devid;
221861071a87SLinu Cherian 
22199bdc47a6STomasz Duszynski 	switch (type) {
22209bdc47a6STomasz Duszynski 	case TYPE_AFPF:
22219bdc47a6STomasz Duszynski 		mw = &rvu->afpf_wq_info;
22229bdc47a6STomasz Duszynski 		break;
22239bdc47a6STomasz Duszynski 	case TYPE_AFVF:
22249bdc47a6STomasz Duszynski 		mw = &rvu->afvf_wq_info;
22259bdc47a6STomasz Duszynski 		break;
22269bdc47a6STomasz Duszynski 	default:
22279bdc47a6STomasz Duszynski 		return;
22289bdc47a6STomasz Duszynski 	}
22299bdc47a6STomasz Duszynski 
22309bdc47a6STomasz Duszynski 	devid = mwork - mw->mbox_wrk_up;
22319bdc47a6STomasz Duszynski 	mbox = &mw->mbox_up;
22329bdc47a6STomasz Duszynski 	mdev = &mbox->dev[devid];
223361071a87SLinu Cherian 
223461071a87SLinu Cherian 	rsp_hdr = mdev->mbase + mbox->rx_start;
2235a36740f6SSunil Goutham 	if (mw->mbox_wrk_up[devid].up_num_msgs == 0) {
223661071a87SLinu Cherian 		dev_warn(rvu->dev, "mbox up handler: num_msgs = 0\n");
223761071a87SLinu Cherian 		return;
223861071a87SLinu Cherian 	}
223961071a87SLinu Cherian 
224061071a87SLinu Cherian 	offset = mbox->rx_start + ALIGN(sizeof(*rsp_hdr), MBOX_MSG_ALIGN);
224161071a87SLinu Cherian 
2242a36740f6SSunil Goutham 	for (id = 0; id < mw->mbox_wrk_up[devid].up_num_msgs; id++) {
224361071a87SLinu Cherian 		msg = mdev->mbase + offset;
224461071a87SLinu Cherian 
224561071a87SLinu Cherian 		if (msg->id >= MBOX_MSG_MAX) {
224661071a87SLinu Cherian 			dev_err(rvu->dev,
224761071a87SLinu Cherian 				"Mbox msg with unknown ID 0x%x\n", msg->id);
224861071a87SLinu Cherian 			goto end;
224961071a87SLinu Cherian 		}
225061071a87SLinu Cherian 
225161071a87SLinu Cherian 		if (msg->sig != OTX2_MBOX_RSP_SIG) {
225261071a87SLinu Cherian 			dev_err(rvu->dev,
225361071a87SLinu Cherian 				"Mbox msg with wrong signature %x, ID 0x%x\n",
225461071a87SLinu Cherian 				msg->sig, msg->id);
225561071a87SLinu Cherian 			goto end;
225661071a87SLinu Cherian 		}
225761071a87SLinu Cherian 
225861071a87SLinu Cherian 		switch (msg->id) {
225961071a87SLinu Cherian 		case MBOX_MSG_CGX_LINK_EVENT:
226061071a87SLinu Cherian 			break;
226161071a87SLinu Cherian 		default:
226261071a87SLinu Cherian 			if (msg->rc)
226361071a87SLinu Cherian 				dev_err(rvu->dev,
226461071a87SLinu Cherian 					"Mbox msg response has err %d, ID 0x%x\n",
226561071a87SLinu Cherian 					msg->rc, msg->id);
226661071a87SLinu Cherian 			break;
226761071a87SLinu Cherian 		}
226861071a87SLinu Cherian end:
226961071a87SLinu Cherian 		offset = mbox->rx_start + msg->next_msgoff;
227061071a87SLinu Cherian 		mdev->msgs_acked++;
227161071a87SLinu Cherian 	}
2272a36740f6SSunil Goutham 	mw->mbox_wrk_up[devid].up_num_msgs = 0;
227361071a87SLinu Cherian 
22749bdc47a6STomasz Duszynski 	otx2_mbox_reset(mbox, devid);
227561071a87SLinu Cherian }
227661071a87SLinu Cherian 
rvu_afpf_mbox_up_handler(struct work_struct * work)22779bdc47a6STomasz Duszynski static inline void rvu_afpf_mbox_up_handler(struct work_struct *work)
22787304ac45SSunil Goutham {
22799bdc47a6STomasz Duszynski 	struct rvu_work *mwork = container_of(work, struct rvu_work, work);
22807304ac45SSunil Goutham 
22819bdc47a6STomasz Duszynski 	__rvu_mbox_up_handler(mwork, TYPE_AFPF);
22829bdc47a6STomasz Duszynski }
22839bdc47a6STomasz Duszynski 
rvu_afvf_mbox_up_handler(struct work_struct * work)22849bdc47a6STomasz Duszynski static inline void rvu_afvf_mbox_up_handler(struct work_struct *work)
22859bdc47a6STomasz Duszynski {
22869bdc47a6STomasz Duszynski 	struct rvu_work *mwork = container_of(work, struct rvu_work, work);
22879bdc47a6STomasz Duszynski 
22889bdc47a6STomasz Duszynski 	__rvu_mbox_up_handler(mwork, TYPE_AFVF);
22899bdc47a6STomasz Duszynski }
22909bdc47a6STomasz Duszynski 
rvu_get_mbox_regions(struct rvu * rvu,void ** mbox_addr,int num,int type,unsigned long * pf_bmap)229198c56111SSubbaraya Sundeep static int rvu_get_mbox_regions(struct rvu *rvu, void **mbox_addr,
22925eb1b722SRatheesh Kannoth 				int num, int type, unsigned long *pf_bmap)
229398c56111SSubbaraya Sundeep {
229498c56111SSubbaraya Sundeep 	struct rvu_hwinfo *hw = rvu->hw;
229598c56111SSubbaraya Sundeep 	int region;
229698c56111SSubbaraya Sundeep 	u64 bar4;
229798c56111SSubbaraya Sundeep 
229898c56111SSubbaraya Sundeep 	/* For cn10k platform VF mailbox regions of a PF follows after the
229998c56111SSubbaraya Sundeep 	 * PF <-> AF mailbox region. Whereas for Octeontx2 it is read from
230098c56111SSubbaraya Sundeep 	 * RVU_PF_VF_BAR4_ADDR register.
230198c56111SSubbaraya Sundeep 	 */
230298c56111SSubbaraya Sundeep 	if (type == TYPE_AFVF) {
230398c56111SSubbaraya Sundeep 		for (region = 0; region < num; region++) {
23045eb1b722SRatheesh Kannoth 			if (!test_bit(region, pf_bmap))
23055eb1b722SRatheesh Kannoth 				continue;
23065eb1b722SRatheesh Kannoth 
230798c56111SSubbaraya Sundeep 			if (hw->cap.per_pf_mbox_regs) {
230898c56111SSubbaraya Sundeep 				bar4 = rvu_read64(rvu, BLKADDR_RVUM,
230998c56111SSubbaraya Sundeep 						  RVU_AF_PFX_BAR4_ADDR(0)) +
231098c56111SSubbaraya Sundeep 						  MBOX_SIZE;
231198c56111SSubbaraya Sundeep 				bar4 += region * MBOX_SIZE;
231298c56111SSubbaraya Sundeep 			} else {
231398c56111SSubbaraya Sundeep 				bar4 = rvupf_read64(rvu, RVU_PF_VF_BAR4_ADDR);
231498c56111SSubbaraya Sundeep 				bar4 += region * MBOX_SIZE;
231598c56111SSubbaraya Sundeep 			}
231698c56111SSubbaraya Sundeep 			mbox_addr[region] = (void *)ioremap_wc(bar4, MBOX_SIZE);
231798c56111SSubbaraya Sundeep 			if (!mbox_addr[region])
231898c56111SSubbaraya Sundeep 				goto error;
231998c56111SSubbaraya Sundeep 		}
232098c56111SSubbaraya Sundeep 		return 0;
232198c56111SSubbaraya Sundeep 	}
232298c56111SSubbaraya Sundeep 
232398c56111SSubbaraya Sundeep 	/* For cn10k platform AF <-> PF mailbox region of a PF is read from per
232498c56111SSubbaraya Sundeep 	 * PF registers. Whereas for Octeontx2 it is read from
232598c56111SSubbaraya Sundeep 	 * RVU_AF_PF_BAR4_ADDR register.
232698c56111SSubbaraya Sundeep 	 */
232798c56111SSubbaraya Sundeep 	for (region = 0; region < num; region++) {
23285eb1b722SRatheesh Kannoth 		if (!test_bit(region, pf_bmap))
23295eb1b722SRatheesh Kannoth 			continue;
23305eb1b722SRatheesh Kannoth 
233198c56111SSubbaraya Sundeep 		if (hw->cap.per_pf_mbox_regs) {
233298c56111SSubbaraya Sundeep 			bar4 = rvu_read64(rvu, BLKADDR_RVUM,
233398c56111SSubbaraya Sundeep 					  RVU_AF_PFX_BAR4_ADDR(region));
233498c56111SSubbaraya Sundeep 		} else {
233598c56111SSubbaraya Sundeep 			bar4 = rvu_read64(rvu, BLKADDR_RVUM,
233698c56111SSubbaraya Sundeep 					  RVU_AF_PF_BAR4_ADDR);
233798c56111SSubbaraya Sundeep 			bar4 += region * MBOX_SIZE;
233898c56111SSubbaraya Sundeep 		}
233998c56111SSubbaraya Sundeep 		mbox_addr[region] = (void *)ioremap_wc(bar4, MBOX_SIZE);
234098c56111SSubbaraya Sundeep 		if (!mbox_addr[region])
234198c56111SSubbaraya Sundeep 			goto error;
234298c56111SSubbaraya Sundeep 	}
234398c56111SSubbaraya Sundeep 	return 0;
234498c56111SSubbaraya Sundeep 
234598c56111SSubbaraya Sundeep error:
234698c56111SSubbaraya Sundeep 	while (region--)
234798c56111SSubbaraya Sundeep 		iounmap((void __iomem *)mbox_addr[region]);
234898c56111SSubbaraya Sundeep 	return -ENOMEM;
234998c56111SSubbaraya Sundeep }
235098c56111SSubbaraya Sundeep 
rvu_mbox_init(struct rvu * rvu,struct mbox_wq_info * mw,int type,int num,void (mbox_handler)(struct work_struct *),void (mbox_up_handler)(struct work_struct *))23519bdc47a6STomasz Duszynski static int rvu_mbox_init(struct rvu *rvu, struct mbox_wq_info *mw,
23529bdc47a6STomasz Duszynski 			 int type, int num,
23539bdc47a6STomasz Duszynski 			 void (mbox_handler)(struct work_struct *),
23549bdc47a6STomasz Duszynski 			 void (mbox_up_handler)(struct work_struct *))
23559bdc47a6STomasz Duszynski {
235698c56111SSubbaraya Sundeep 	int err = -EINVAL, i, dir, dir_up;
235798c56111SSubbaraya Sundeep 	void __iomem *reg_base;
23589bdc47a6STomasz Duszynski 	struct rvu_work *mwork;
23595eb1b722SRatheesh Kannoth 	unsigned long *pf_bmap;
236098c56111SSubbaraya Sundeep 	void **mbox_regions;
23619bdc47a6STomasz Duszynski 	const char *name;
23625eb1b722SRatheesh Kannoth 	u64 cfg;
23635eb1b722SRatheesh Kannoth 
23645eb1b722SRatheesh Kannoth 	pf_bmap = bitmap_zalloc(num, GFP_KERNEL);
23655eb1b722SRatheesh Kannoth 	if (!pf_bmap)
23665eb1b722SRatheesh Kannoth 		return -ENOMEM;
23675eb1b722SRatheesh Kannoth 
23685eb1b722SRatheesh Kannoth 	/* RVU VFs */
23695eb1b722SRatheesh Kannoth 	if (type == TYPE_AFVF)
23705eb1b722SRatheesh Kannoth 		bitmap_set(pf_bmap, 0, num);
23715eb1b722SRatheesh Kannoth 
23725eb1b722SRatheesh Kannoth 	if (type == TYPE_AFPF) {
23735eb1b722SRatheesh Kannoth 		/* Mark enabled PFs in bitmap */
23745eb1b722SRatheesh Kannoth 		for (i = 0; i < num; i++) {
23755eb1b722SRatheesh Kannoth 			cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_PFX_CFG(i));
23765eb1b722SRatheesh Kannoth 			if (cfg & BIT_ULL(20))
23775eb1b722SRatheesh Kannoth 				set_bit(i, pf_bmap);
23785eb1b722SRatheesh Kannoth 		}
23795eb1b722SRatheesh Kannoth 	}
238098c56111SSubbaraya Sundeep 
2381c6354b85SSubbaraya Sundeep 	mutex_init(&rvu->mbox_lock);
2382c6354b85SSubbaraya Sundeep 
238398c56111SSubbaraya Sundeep 	mbox_regions = kcalloc(num, sizeof(void *), GFP_KERNEL);
23845eb1b722SRatheesh Kannoth 	if (!mbox_regions) {
23855eb1b722SRatheesh Kannoth 		err = -ENOMEM;
23865eb1b722SRatheesh Kannoth 		goto free_bitmap;
23875eb1b722SRatheesh Kannoth 	}
23889bdc47a6STomasz Duszynski 
23899bdc47a6STomasz Duszynski 	switch (type) {
23909bdc47a6STomasz Duszynski 	case TYPE_AFPF:
23919bdc47a6STomasz Duszynski 		name = "rvu_afpf_mailbox";
23929bdc47a6STomasz Duszynski 		dir = MBOX_DIR_AFPF;
23939bdc47a6STomasz Duszynski 		dir_up = MBOX_DIR_AFPF_UP;
23949bdc47a6STomasz Duszynski 		reg_base = rvu->afreg_base;
23955eb1b722SRatheesh Kannoth 		err = rvu_get_mbox_regions(rvu, mbox_regions, num, TYPE_AFPF, pf_bmap);
239698c56111SSubbaraya Sundeep 		if (err)
239798c56111SSubbaraya Sundeep 			goto free_regions;
23989bdc47a6STomasz Duszynski 		break;
23999bdc47a6STomasz Duszynski 	case TYPE_AFVF:
24009bdc47a6STomasz Duszynski 		name = "rvu_afvf_mailbox";
24019bdc47a6STomasz Duszynski 		dir = MBOX_DIR_PFVF;
24029bdc47a6STomasz Duszynski 		dir_up = MBOX_DIR_PFVF_UP;
24039bdc47a6STomasz Duszynski 		reg_base = rvu->pfreg_base;
24045eb1b722SRatheesh Kannoth 		err = rvu_get_mbox_regions(rvu, mbox_regions, num, TYPE_AFVF, pf_bmap);
240598c56111SSubbaraya Sundeep 		if (err)
240698c56111SSubbaraya Sundeep 			goto free_regions;
24079bdc47a6STomasz Duszynski 		break;
24089bdc47a6STomasz Duszynski 	default:
2409e07a097bSZhou Qingyang 		goto free_regions;
24109bdc47a6STomasz Duszynski 	}
24119bdc47a6STomasz Duszynski 
24129bdc47a6STomasz Duszynski 	mw->mbox_wq = alloc_workqueue(name,
24137304ac45SSunil Goutham 				      WQ_UNBOUND | WQ_HIGHPRI | WQ_MEM_RECLAIM,
24149bdc47a6STomasz Duszynski 				      num);
241598c56111SSubbaraya Sundeep 	if (!mw->mbox_wq) {
241698c56111SSubbaraya Sundeep 		err = -ENOMEM;
241798c56111SSubbaraya Sundeep 		goto unmap_regions;
241898c56111SSubbaraya Sundeep 	}
24197304ac45SSunil Goutham 
24209bdc47a6STomasz Duszynski 	mw->mbox_wrk = devm_kcalloc(rvu->dev, num,
24217304ac45SSunil Goutham 				    sizeof(struct rvu_work), GFP_KERNEL);
24229bdc47a6STomasz Duszynski 	if (!mw->mbox_wrk) {
24237304ac45SSunil Goutham 		err = -ENOMEM;
24247304ac45SSunil Goutham 		goto exit;
24257304ac45SSunil Goutham 	}
24267304ac45SSunil Goutham 
24279bdc47a6STomasz Duszynski 	mw->mbox_wrk_up = devm_kcalloc(rvu->dev, num,
242861071a87SLinu Cherian 				       sizeof(struct rvu_work), GFP_KERNEL);
24299bdc47a6STomasz Duszynski 	if (!mw->mbox_wrk_up) {
243061071a87SLinu Cherian 		err = -ENOMEM;
243161071a87SLinu Cherian 		goto exit;
243261071a87SLinu Cherian 	}
243361071a87SLinu Cherian 
243498c56111SSubbaraya Sundeep 	err = otx2_mbox_regions_init(&mw->mbox, mbox_regions, rvu->pdev,
24355eb1b722SRatheesh Kannoth 				     reg_base, dir, num, pf_bmap);
24367304ac45SSunil Goutham 	if (err)
24377304ac45SSunil Goutham 		goto exit;
24387304ac45SSunil Goutham 
243998c56111SSubbaraya Sundeep 	err = otx2_mbox_regions_init(&mw->mbox_up, mbox_regions, rvu->pdev,
24405eb1b722SRatheesh Kannoth 				     reg_base, dir_up, num, pf_bmap);
244161071a87SLinu Cherian 	if (err)
244261071a87SLinu Cherian 		goto exit;
244361071a87SLinu Cherian 
24449bdc47a6STomasz Duszynski 	for (i = 0; i < num; i++) {
24455eb1b722SRatheesh Kannoth 		if (!test_bit(i, pf_bmap))
24465eb1b722SRatheesh Kannoth 			continue;
24475eb1b722SRatheesh Kannoth 
24489bdc47a6STomasz Duszynski 		mwork = &mw->mbox_wrk[i];
24497304ac45SSunil Goutham 		mwork->rvu = rvu;
24509bdc47a6STomasz Duszynski 		INIT_WORK(&mwork->work, mbox_handler);
24517304ac45SSunil Goutham 
24529bdc47a6STomasz Duszynski 		mwork = &mw->mbox_wrk_up[i];
245361071a87SLinu Cherian 		mwork->rvu = rvu;
24549bdc47a6STomasz Duszynski 		INIT_WORK(&mwork->work, mbox_up_handler);
245561071a87SLinu Cherian 	}
24565eb1b722SRatheesh Kannoth 	goto free_regions;
245798c56111SSubbaraya Sundeep 
24587304ac45SSunil Goutham exit:
24599bdc47a6STomasz Duszynski 	destroy_workqueue(mw->mbox_wq);
246098c56111SSubbaraya Sundeep unmap_regions:
246198c56111SSubbaraya Sundeep 	while (num--)
246298c56111SSubbaraya Sundeep 		iounmap((void __iomem *)mbox_regions[num]);
246398c56111SSubbaraya Sundeep free_regions:
246498c56111SSubbaraya Sundeep 	kfree(mbox_regions);
24655eb1b722SRatheesh Kannoth free_bitmap:
24665eb1b722SRatheesh Kannoth 	bitmap_free(pf_bmap);
24677304ac45SSunil Goutham 	return err;
24687304ac45SSunil Goutham }
24697304ac45SSunil Goutham 
rvu_mbox_destroy(struct mbox_wq_info * mw)24709bdc47a6STomasz Duszynski static void rvu_mbox_destroy(struct mbox_wq_info *mw)
24717304ac45SSunil Goutham {
247298c56111SSubbaraya Sundeep 	struct otx2_mbox *mbox = &mw->mbox;
247398c56111SSubbaraya Sundeep 	struct otx2_mbox_dev *mdev;
247498c56111SSubbaraya Sundeep 	int devid;
247598c56111SSubbaraya Sundeep 
24769bdc47a6STomasz Duszynski 	if (mw->mbox_wq) {
24779bdc47a6STomasz Duszynski 		destroy_workqueue(mw->mbox_wq);
24789bdc47a6STomasz Duszynski 		mw->mbox_wq = NULL;
24797304ac45SSunil Goutham 	}
24807304ac45SSunil Goutham 
248198c56111SSubbaraya Sundeep 	for (devid = 0; devid < mbox->ndevs; devid++) {
248298c56111SSubbaraya Sundeep 		mdev = &mbox->dev[devid];
248398c56111SSubbaraya Sundeep 		if (mdev->hwbase)
248498c56111SSubbaraya Sundeep 			iounmap((void __iomem *)mdev->hwbase);
248598c56111SSubbaraya Sundeep 	}
24867304ac45SSunil Goutham 
24879bdc47a6STomasz Duszynski 	otx2_mbox_destroy(&mw->mbox);
24889bdc47a6STomasz Duszynski 	otx2_mbox_destroy(&mw->mbox_up);
24899bdc47a6STomasz Duszynski }
24909bdc47a6STomasz Duszynski 
rvu_queue_work(struct mbox_wq_info * mw,int first,int mdevs,u64 intr)24919bdc47a6STomasz Duszynski static void rvu_queue_work(struct mbox_wq_info *mw, int first,
24929bdc47a6STomasz Duszynski 			   int mdevs, u64 intr)
24939bdc47a6STomasz Duszynski {
24949bdc47a6STomasz Duszynski 	struct otx2_mbox_dev *mdev;
24959bdc47a6STomasz Duszynski 	struct otx2_mbox *mbox;
24969bdc47a6STomasz Duszynski 	struct mbox_hdr *hdr;
24979bdc47a6STomasz Duszynski 	int i;
24989bdc47a6STomasz Duszynski 
24999bdc47a6STomasz Duszynski 	for (i = first; i < mdevs; i++) {
25009bdc47a6STomasz Duszynski 		/* start from 0 */
25019bdc47a6STomasz Duszynski 		if (!(intr & BIT_ULL(i - first)))
25029bdc47a6STomasz Duszynski 			continue;
25039bdc47a6STomasz Duszynski 
25049bdc47a6STomasz Duszynski 		mbox = &mw->mbox;
25059bdc47a6STomasz Duszynski 		mdev = &mbox->dev[i];
25069bdc47a6STomasz Duszynski 		hdr = mdev->mbase + mbox->rx_start;
25079bdc47a6STomasz Duszynski 
2508a36740f6SSunil Goutham 		/*The hdr->num_msgs is set to zero immediately in the interrupt
2509a36740f6SSunil Goutham 		 * handler to  ensure that it holds a correct value next time
2510a36740f6SSunil Goutham 		 * when the interrupt handler is called.
2511a36740f6SSunil Goutham 		 * pf->mbox.num_msgs holds the data for use in pfaf_mbox_handler
2512a36740f6SSunil Goutham 		 * pf>mbox.up_num_msgs holds the data for use in
2513a36740f6SSunil Goutham 		 * pfaf_mbox_up_handler.
2514a36740f6SSunil Goutham 		 */
2515a36740f6SSunil Goutham 
2516a36740f6SSunil Goutham 		if (hdr->num_msgs) {
2517a36740f6SSunil Goutham 			mw->mbox_wrk[i].num_msgs = hdr->num_msgs;
2518a36740f6SSunil Goutham 			hdr->num_msgs = 0;
2519a36740f6SSunil Goutham 			queue_work(mw->mbox_wq, &mw->mbox_wrk[i].work);
2520a36740f6SSunil Goutham 		}
25219bdc47a6STomasz Duszynski 		mbox = &mw->mbox_up;
25229bdc47a6STomasz Duszynski 		mdev = &mbox->dev[i];
25239bdc47a6STomasz Duszynski 		hdr = mdev->mbase + mbox->rx_start;
2524a36740f6SSunil Goutham 		if (hdr->num_msgs) {
2525a36740f6SSunil Goutham 			mw->mbox_wrk_up[i].up_num_msgs = hdr->num_msgs;
2526a36740f6SSunil Goutham 			hdr->num_msgs = 0;
25279bdc47a6STomasz Duszynski 			queue_work(mw->mbox_wq, &mw->mbox_wrk_up[i].work);
25289bdc47a6STomasz Duszynski 		}
25297304ac45SSunil Goutham 	}
2530a36740f6SSunil Goutham }
25317304ac45SSunil Goutham 
rvu_mbox_pf_intr_handler(int irq,void * rvu_irq)2532dc29dd00SSubbaraya Sundeep static irqreturn_t rvu_mbox_pf_intr_handler(int irq, void *rvu_irq)
25337304ac45SSunil Goutham {
25347304ac45SSunil Goutham 	struct rvu *rvu = (struct rvu *)rvu_irq;
25357304ac45SSunil Goutham 	u64 intr;
25367304ac45SSunil Goutham 
25377304ac45SSunil Goutham 	intr = rvu_read64(rvu, BLKADDR_RVUM, RVU_AF_PFAF_MBOX_INT);
25387304ac45SSunil Goutham 	/* Clear interrupts */
25397304ac45SSunil Goutham 	rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFAF_MBOX_INT, intr);
254049142d12SSubbaraya Sundeep 	if (intr)
254149142d12SSubbaraya Sundeep 		trace_otx2_msg_interrupt(rvu->pdev, "PF(s) to AF", intr);
25427304ac45SSunil Goutham 
25437304ac45SSunil Goutham 	/* Sync with mbox memory region */
25449bdc47a6STomasz Duszynski 	rmb();
25457304ac45SSunil Goutham 
25469bdc47a6STomasz Duszynski 	rvu_queue_work(&rvu->afpf_wq_info, 0, rvu->hw->total_pfs, intr);
25479bdc47a6STomasz Duszynski 
2548dc29dd00SSubbaraya Sundeep 	return IRQ_HANDLED;
2549dc29dd00SSubbaraya Sundeep }
2550dc29dd00SSubbaraya Sundeep 
rvu_mbox_intr_handler(int irq,void * rvu_irq)2551dc29dd00SSubbaraya Sundeep static irqreturn_t rvu_mbox_intr_handler(int irq, void *rvu_irq)
2552dc29dd00SSubbaraya Sundeep {
2553dc29dd00SSubbaraya Sundeep 	struct rvu *rvu = (struct rvu *)rvu_irq;
2554dc29dd00SSubbaraya Sundeep 	int vfs = rvu->vfs;
2555dc29dd00SSubbaraya Sundeep 	u64 intr;
2556dc29dd00SSubbaraya Sundeep 
2557dc29dd00SSubbaraya Sundeep 	/* Sync with mbox memory region */
2558dc29dd00SSubbaraya Sundeep 	rmb();
2559dc29dd00SSubbaraya Sundeep 
25609bdc47a6STomasz Duszynski 	/* Handle VF interrupts */
25619bdc47a6STomasz Duszynski 	if (vfs > 64) {
25629bdc47a6STomasz Duszynski 		intr = rvupf_read64(rvu, RVU_PF_VFPF_MBOX_INTX(1));
25639bdc47a6STomasz Duszynski 		rvupf_write64(rvu, RVU_PF_VFPF_MBOX_INTX(1), intr);
25649bdc47a6STomasz Duszynski 
25659bdc47a6STomasz Duszynski 		rvu_queue_work(&rvu->afvf_wq_info, 64, vfs, intr);
2566*aa042c45SGeetha sowjanya 		vfs = 64;
25677304ac45SSunil Goutham 	}
25689bdc47a6STomasz Duszynski 
25699bdc47a6STomasz Duszynski 	intr = rvupf_read64(rvu, RVU_PF_VFPF_MBOX_INTX(0));
25709bdc47a6STomasz Duszynski 	rvupf_write64(rvu, RVU_PF_VFPF_MBOX_INTX(0), intr);
257149142d12SSubbaraya Sundeep 	if (intr)
257249142d12SSubbaraya Sundeep 		trace_otx2_msg_interrupt(rvu->pdev, "VF(s) to AF", intr);
25739bdc47a6STomasz Duszynski 
25749bdc47a6STomasz Duszynski 	rvu_queue_work(&rvu->afvf_wq_info, 0, vfs, intr);
25757304ac45SSunil Goutham 
25767304ac45SSunil Goutham 	return IRQ_HANDLED;
25777304ac45SSunil Goutham }
25787304ac45SSunil Goutham 
rvu_enable_mbox_intr(struct rvu * rvu)25797304ac45SSunil Goutham static void rvu_enable_mbox_intr(struct rvu *rvu)
25807304ac45SSunil Goutham {
25817304ac45SSunil Goutham 	struct rvu_hwinfo *hw = rvu->hw;
25827304ac45SSunil Goutham 
25837304ac45SSunil Goutham 	/* Clear spurious irqs, if any */
25847304ac45SSunil Goutham 	rvu_write64(rvu, BLKADDR_RVUM,
25857304ac45SSunil Goutham 		    RVU_AF_PFAF_MBOX_INT, INTR_MASK(hw->total_pfs));
25867304ac45SSunil Goutham 
25877304ac45SSunil Goutham 	/* Enable mailbox interrupt for all PFs except PF0 i.e AF itself */
25887304ac45SSunil Goutham 	rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFAF_MBOX_INT_ENA_W1S,
25897304ac45SSunil Goutham 		    INTR_MASK(hw->total_pfs) & ~1ULL);
25907304ac45SSunil Goutham }
25917304ac45SSunil Goutham 
rvu_blklf_teardown(struct rvu * rvu,u16 pcifunc,u8 blkaddr)2592c554f9c1SGeetha sowjanya static void rvu_blklf_teardown(struct rvu *rvu, u16 pcifunc, u8 blkaddr)
2593c554f9c1SGeetha sowjanya {
2594c554f9c1SGeetha sowjanya 	struct rvu_block *block;
2595c554f9c1SGeetha sowjanya 	int slot, lf, num_lfs;
2596c554f9c1SGeetha sowjanya 	int err;
2597c554f9c1SGeetha sowjanya 
2598c554f9c1SGeetha sowjanya 	block = &rvu->hw->block[blkaddr];
2599c554f9c1SGeetha sowjanya 	num_lfs = rvu_get_rsrc_mapcount(rvu_get_pfvf(rvu, pcifunc),
2600cdd41e87SSubbaraya Sundeep 					block->addr);
2601c554f9c1SGeetha sowjanya 	if (!num_lfs)
2602c554f9c1SGeetha sowjanya 		return;
2603c554f9c1SGeetha sowjanya 	for (slot = 0; slot < num_lfs; slot++) {
2604c554f9c1SGeetha sowjanya 		lf = rvu_get_lf(rvu, block, pcifunc, slot);
2605c554f9c1SGeetha sowjanya 		if (lf < 0)
2606c554f9c1SGeetha sowjanya 			continue;
2607c554f9c1SGeetha sowjanya 
2608c554f9c1SGeetha sowjanya 		/* Cleanup LF and reset it */
26099932fb72SRakesh Babu 		if (block->addr == BLKADDR_NIX0 || block->addr == BLKADDR_NIX1)
2610c554f9c1SGeetha sowjanya 			rvu_nix_lf_teardown(rvu, pcifunc, block->addr, lf);
2611c554f9c1SGeetha sowjanya 		else if (block->addr == BLKADDR_NPA)
2612c554f9c1SGeetha sowjanya 			rvu_npa_lf_teardown(rvu, pcifunc, lf);
2613c57c58fdSSrujana Challa 		else if ((block->addr == BLKADDR_CPT0) ||
2614c57c58fdSSrujana Challa 			 (block->addr == BLKADDR_CPT1))
26157054d39cSNithin Dabilpuram 			rvu_cpt_lf_teardown(rvu, pcifunc, block->addr, lf,
26167054d39cSNithin Dabilpuram 					    slot);
2617c554f9c1SGeetha sowjanya 
2618c554f9c1SGeetha sowjanya 		err = rvu_lf_reset(rvu, block, lf);
2619c554f9c1SGeetha sowjanya 		if (err) {
2620c554f9c1SGeetha sowjanya 			dev_err(rvu->dev, "Failed to reset blkaddr %d LF%d\n",
2621c554f9c1SGeetha sowjanya 				block->addr, lf);
2622c554f9c1SGeetha sowjanya 		}
2623c554f9c1SGeetha sowjanya 	}
2624c554f9c1SGeetha sowjanya }
2625c554f9c1SGeetha sowjanya 
__rvu_flr_handler(struct rvu * rvu,u16 pcifunc)2626c554f9c1SGeetha sowjanya static void __rvu_flr_handler(struct rvu *rvu, u16 pcifunc)
2627c554f9c1SGeetha sowjanya {
2628bab9eed5SRatheesh Kannoth 	if (rvu_npc_exact_has_match_table(rvu))
2629bab9eed5SRatheesh Kannoth 		rvu_npc_exact_reset(rvu, pcifunc);
2630bab9eed5SRatheesh Kannoth 
2631c554f9c1SGeetha sowjanya 	mutex_lock(&rvu->flr_lock);
2632c554f9c1SGeetha sowjanya 	/* Reset order should reflect inter-block dependencies:
2633c554f9c1SGeetha sowjanya 	 * 1. Reset any packet/work sources (NIX, CPT, TIM)
2634c554f9c1SGeetha sowjanya 	 * 2. Flush and reset SSO/SSOW
2635c554f9c1SGeetha sowjanya 	 * 3. Cleanup pools (NPA)
2636c554f9c1SGeetha sowjanya 	 */
2637c554f9c1SGeetha sowjanya 	rvu_blklf_teardown(rvu, pcifunc, BLKADDR_NIX0);
26389932fb72SRakesh Babu 	rvu_blklf_teardown(rvu, pcifunc, BLKADDR_NIX1);
2639c554f9c1SGeetha sowjanya 	rvu_blklf_teardown(rvu, pcifunc, BLKADDR_CPT0);
26409932fb72SRakesh Babu 	rvu_blklf_teardown(rvu, pcifunc, BLKADDR_CPT1);
2641c554f9c1SGeetha sowjanya 	rvu_blklf_teardown(rvu, pcifunc, BLKADDR_TIM);
2642c554f9c1SGeetha sowjanya 	rvu_blklf_teardown(rvu, pcifunc, BLKADDR_SSOW);
2643c554f9c1SGeetha sowjanya 	rvu_blklf_teardown(rvu, pcifunc, BLKADDR_SSO);
2644c554f9c1SGeetha sowjanya 	rvu_blklf_teardown(rvu, pcifunc, BLKADDR_NPA);
2645873a1e3dSHarman Kalra 	rvu_reset_lmt_map_tbl(rvu, pcifunc);
2646c554f9c1SGeetha sowjanya 	rvu_detach_rsrcs(rvu, NULL, pcifunc);
26473f8fe40aSSubbaraya Sundeep 	/* In scenarios where PF/VF drivers detach NIXLF without freeing MCAM
26483f8fe40aSSubbaraya Sundeep 	 * entries, check and free the MCAM entries explicitly to avoid leak.
26493f8fe40aSSubbaraya Sundeep 	 * Since LF is detached use LF number as -1.
26503f8fe40aSSubbaraya Sundeep 	 */
26513f8fe40aSSubbaraya Sundeep 	rvu_npc_free_mcam_entries(rvu, pcifunc, -1);
26522e3e94c2SHariprasad Kelam 	rvu_mac_reset(rvu, pcifunc);
26533f8fe40aSSubbaraya Sundeep 
2654cd1045acSGeetha sowjanya 	if (rvu->mcs_blk_cnt)
2655cd1045acSGeetha sowjanya 		rvu_mcs_flr_handler(rvu, pcifunc);
2656cd1045acSGeetha sowjanya 
2657c554f9c1SGeetha sowjanya 	mutex_unlock(&rvu->flr_lock);
2658c554f9c1SGeetha sowjanya }
2659c554f9c1SGeetha sowjanya 
rvu_afvf_flr_handler(struct rvu * rvu,int vf)2660465ed9c1SSunil Goutham static void rvu_afvf_flr_handler(struct rvu *rvu, int vf)
2661465ed9c1SSunil Goutham {
2662465ed9c1SSunil Goutham 	int reg = 0;
2663465ed9c1SSunil Goutham 
2664465ed9c1SSunil Goutham 	/* pcifunc = 0(PF0) | (vf + 1) */
2665465ed9c1SSunil Goutham 	__rvu_flr_handler(rvu, vf + 1);
2666465ed9c1SSunil Goutham 
2667465ed9c1SSunil Goutham 	if (vf >= 64) {
2668465ed9c1SSunil Goutham 		reg = 1;
2669465ed9c1SSunil Goutham 		vf = vf - 64;
2670465ed9c1SSunil Goutham 	}
2671465ed9c1SSunil Goutham 
2672465ed9c1SSunil Goutham 	/* Signal FLR finish and enable IRQ */
2673465ed9c1SSunil Goutham 	rvupf_write64(rvu, RVU_PF_VFTRPENDX(reg), BIT_ULL(vf));
2674465ed9c1SSunil Goutham 	rvupf_write64(rvu, RVU_PF_VFFLR_INT_ENA_W1SX(reg), BIT_ULL(vf));
2675465ed9c1SSunil Goutham }
2676465ed9c1SSunil Goutham 
rvu_flr_handler(struct work_struct * work)26779fe4ebf7SGeetha sowjanya static void rvu_flr_handler(struct work_struct *work)
26789fe4ebf7SGeetha sowjanya {
26799fe4ebf7SGeetha sowjanya 	struct rvu_work *flrwork = container_of(work, struct rvu_work, work);
26809fe4ebf7SGeetha sowjanya 	struct rvu *rvu = flrwork->rvu;
2681c554f9c1SGeetha sowjanya 	u16 pcifunc, numvfs, vf;
2682c554f9c1SGeetha sowjanya 	u64 cfg;
2683c554f9c1SGeetha sowjanya 	int pf;
26849fe4ebf7SGeetha sowjanya 
26859fe4ebf7SGeetha sowjanya 	pf = flrwork - rvu->flr_wrk;
2686465ed9c1SSunil Goutham 	if (pf >= rvu->hw->total_pfs) {
2687465ed9c1SSunil Goutham 		rvu_afvf_flr_handler(rvu, pf - rvu->hw->total_pfs);
2688465ed9c1SSunil Goutham 		return;
2689465ed9c1SSunil Goutham 	}
26909fe4ebf7SGeetha sowjanya 
2691c554f9c1SGeetha sowjanya 	cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_PFX_CFG(pf));
2692c554f9c1SGeetha sowjanya 	numvfs = (cfg >> 12) & 0xFF;
2693c554f9c1SGeetha sowjanya 	pcifunc  = pf << RVU_PFVF_PF_SHIFT;
2694c554f9c1SGeetha sowjanya 
2695c554f9c1SGeetha sowjanya 	for (vf = 0; vf < numvfs; vf++)
2696c554f9c1SGeetha sowjanya 		__rvu_flr_handler(rvu, (pcifunc | (vf + 1)));
2697c554f9c1SGeetha sowjanya 
2698c554f9c1SGeetha sowjanya 	__rvu_flr_handler(rvu, pcifunc);
2699c554f9c1SGeetha sowjanya 
27009fe4ebf7SGeetha sowjanya 	/* Signal FLR finish */
27019fe4ebf7SGeetha sowjanya 	rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFTRPEND, BIT_ULL(pf));
27029fe4ebf7SGeetha sowjanya 
27039fe4ebf7SGeetha sowjanya 	/* Enable interrupt */
27049fe4ebf7SGeetha sowjanya 	rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFFLR_INT_ENA_W1S,  BIT_ULL(pf));
27059fe4ebf7SGeetha sowjanya }
27069fe4ebf7SGeetha sowjanya 
rvu_afvf_queue_flr_work(struct rvu * rvu,int start_vf,int numvfs)2707465ed9c1SSunil Goutham static void rvu_afvf_queue_flr_work(struct rvu *rvu, int start_vf, int numvfs)
2708465ed9c1SSunil Goutham {
2709465ed9c1SSunil Goutham 	int dev, vf, reg = 0;
2710465ed9c1SSunil Goutham 	u64 intr;
2711465ed9c1SSunil Goutham 
2712465ed9c1SSunil Goutham 	if (start_vf >= 64)
2713465ed9c1SSunil Goutham 		reg = 1;
2714465ed9c1SSunil Goutham 
2715465ed9c1SSunil Goutham 	intr = rvupf_read64(rvu, RVU_PF_VFFLR_INTX(reg));
2716465ed9c1SSunil Goutham 	if (!intr)
2717465ed9c1SSunil Goutham 		return;
2718465ed9c1SSunil Goutham 
2719465ed9c1SSunil Goutham 	for (vf = 0; vf < numvfs; vf++) {
2720465ed9c1SSunil Goutham 		if (!(intr & BIT_ULL(vf)))
2721465ed9c1SSunil Goutham 			continue;
2722465ed9c1SSunil Goutham 		/* Clear and disable the interrupt */
2723465ed9c1SSunil Goutham 		rvupf_write64(rvu, RVU_PF_VFFLR_INTX(reg), BIT_ULL(vf));
2724465ed9c1SSunil Goutham 		rvupf_write64(rvu, RVU_PF_VFFLR_INT_ENA_W1CX(reg), BIT_ULL(vf));
2725906999c9SNithin Dabilpuram 
2726906999c9SNithin Dabilpuram 		dev = vf + start_vf + rvu->hw->total_pfs;
2727906999c9SNithin Dabilpuram 		queue_work(rvu->flr_wq, &rvu->flr_wrk[dev].work);
2728465ed9c1SSunil Goutham 	}
2729465ed9c1SSunil Goutham }
2730465ed9c1SSunil Goutham 
rvu_flr_intr_handler(int irq,void * rvu_irq)27319fe4ebf7SGeetha sowjanya static irqreturn_t rvu_flr_intr_handler(int irq, void *rvu_irq)
27329fe4ebf7SGeetha sowjanya {
27339fe4ebf7SGeetha sowjanya 	struct rvu *rvu = (struct rvu *)rvu_irq;
27349fe4ebf7SGeetha sowjanya 	u64 intr;
27359fe4ebf7SGeetha sowjanya 	u8  pf;
27369fe4ebf7SGeetha sowjanya 
27379fe4ebf7SGeetha sowjanya 	intr = rvu_read64(rvu, BLKADDR_RVUM, RVU_AF_PFFLR_INT);
2738465ed9c1SSunil Goutham 	if (!intr)
2739465ed9c1SSunil Goutham 		goto afvf_flr;
27409fe4ebf7SGeetha sowjanya 
27419fe4ebf7SGeetha sowjanya 	for (pf = 0; pf < rvu->hw->total_pfs; pf++) {
27429fe4ebf7SGeetha sowjanya 		if (intr & (1ULL << pf)) {
27439fe4ebf7SGeetha sowjanya 			/* clear interrupt */
27449fe4ebf7SGeetha sowjanya 			rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFFLR_INT,
27459fe4ebf7SGeetha sowjanya 				    BIT_ULL(pf));
27469fe4ebf7SGeetha sowjanya 			/* Disable the interrupt */
27479fe4ebf7SGeetha sowjanya 			rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFFLR_INT_ENA_W1C,
27489fe4ebf7SGeetha sowjanya 				    BIT_ULL(pf));
2749906999c9SNithin Dabilpuram 			/* PF is already dead do only AF related operations */
2750906999c9SNithin Dabilpuram 			queue_work(rvu->flr_wq, &rvu->flr_wrk[pf].work);
27519fe4ebf7SGeetha sowjanya 		}
27529fe4ebf7SGeetha sowjanya 	}
2753465ed9c1SSunil Goutham 
2754465ed9c1SSunil Goutham afvf_flr:
2755465ed9c1SSunil Goutham 	rvu_afvf_queue_flr_work(rvu, 0, 64);
2756465ed9c1SSunil Goutham 	if (rvu->vfs > 64)
2757465ed9c1SSunil Goutham 		rvu_afvf_queue_flr_work(rvu, 64, rvu->vfs - 64);
2758465ed9c1SSunil Goutham 
27599fe4ebf7SGeetha sowjanya 	return IRQ_HANDLED;
27609fe4ebf7SGeetha sowjanya }
27619fe4ebf7SGeetha sowjanya 
rvu_me_handle_vfset(struct rvu * rvu,int idx,u64 intr)27624fa91210SLinu Cherian static void rvu_me_handle_vfset(struct rvu *rvu, int idx, u64 intr)
27634fa91210SLinu Cherian {
27644fa91210SLinu Cherian 	int vf;
27654fa91210SLinu Cherian 
27664fa91210SLinu Cherian 	/* Nothing to be done here other than clearing the
27674fa91210SLinu Cherian 	 * TRPEND bit.
27684fa91210SLinu Cherian 	 */
27694fa91210SLinu Cherian 	for (vf = 0; vf < 64; vf++) {
27704fa91210SLinu Cherian 		if (intr & (1ULL << vf)) {
27714fa91210SLinu Cherian 			/* clear the trpend due to ME(master enable) */
27724fa91210SLinu Cherian 			rvupf_write64(rvu, RVU_PF_VFTRPENDX(idx), BIT_ULL(vf));
27734fa91210SLinu Cherian 			/* clear interrupt */
27744fa91210SLinu Cherian 			rvupf_write64(rvu, RVU_PF_VFME_INTX(idx), BIT_ULL(vf));
27754fa91210SLinu Cherian 		}
27764fa91210SLinu Cherian 	}
27774fa91210SLinu Cherian }
27784fa91210SLinu Cherian 
27794fa91210SLinu Cherian /* Handles ME interrupts from VFs of AF */
rvu_me_vf_intr_handler(int irq,void * rvu_irq)27804fa91210SLinu Cherian static irqreturn_t rvu_me_vf_intr_handler(int irq, void *rvu_irq)
27814fa91210SLinu Cherian {
27824fa91210SLinu Cherian 	struct rvu *rvu = (struct rvu *)rvu_irq;
27834fa91210SLinu Cherian 	int vfset;
27844fa91210SLinu Cherian 	u64 intr;
27854fa91210SLinu Cherian 
27864fa91210SLinu Cherian 	intr = rvu_read64(rvu, BLKADDR_RVUM, RVU_AF_PFME_INT);
27874fa91210SLinu Cherian 
27884fa91210SLinu Cherian 	for (vfset = 0; vfset <= 1; vfset++) {
27894fa91210SLinu Cherian 		intr = rvupf_read64(rvu, RVU_PF_VFME_INTX(vfset));
27904fa91210SLinu Cherian 		if (intr)
27914fa91210SLinu Cherian 			rvu_me_handle_vfset(rvu, vfset, intr);
27924fa91210SLinu Cherian 	}
27934fa91210SLinu Cherian 
27944fa91210SLinu Cherian 	return IRQ_HANDLED;
27954fa91210SLinu Cherian }
27964fa91210SLinu Cherian 
27974fa91210SLinu Cherian /* Handles ME interrupts from PFs */
rvu_me_pf_intr_handler(int irq,void * rvu_irq)27984fa91210SLinu Cherian static irqreturn_t rvu_me_pf_intr_handler(int irq, void *rvu_irq)
27994fa91210SLinu Cherian {
28004fa91210SLinu Cherian 	struct rvu *rvu = (struct rvu *)rvu_irq;
28014fa91210SLinu Cherian 	u64 intr;
28024fa91210SLinu Cherian 	u8  pf;
28034fa91210SLinu Cherian 
28044fa91210SLinu Cherian 	intr = rvu_read64(rvu, BLKADDR_RVUM, RVU_AF_PFME_INT);
28054fa91210SLinu Cherian 
28064fa91210SLinu Cherian 	/* Nothing to be done here other than clearing the
28074fa91210SLinu Cherian 	 * TRPEND bit.
28084fa91210SLinu Cherian 	 */
28094fa91210SLinu Cherian 	for (pf = 0; pf < rvu->hw->total_pfs; pf++) {
28104fa91210SLinu Cherian 		if (intr & (1ULL << pf)) {
28114fa91210SLinu Cherian 			/* clear the trpend due to ME(master enable) */
28124fa91210SLinu Cherian 			rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFTRPEND,
28134fa91210SLinu Cherian 				    BIT_ULL(pf));
28144fa91210SLinu Cherian 			/* clear interrupt */
28154fa91210SLinu Cherian 			rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFME_INT,
28164fa91210SLinu Cherian 				    BIT_ULL(pf));
28174fa91210SLinu Cherian 		}
28184fa91210SLinu Cherian 	}
28194fa91210SLinu Cherian 
28204fa91210SLinu Cherian 	return IRQ_HANDLED;
28214fa91210SLinu Cherian }
28224fa91210SLinu Cherian 
rvu_unregister_interrupts(struct rvu * rvu)28237304ac45SSunil Goutham static void rvu_unregister_interrupts(struct rvu *rvu)
28247304ac45SSunil Goutham {
28257304ac45SSunil Goutham 	int irq;
28267304ac45SSunil Goutham 
282748260907SSrujana Challa 	rvu_cpt_unregister_interrupts(rvu);
282848260907SSrujana Challa 
28297304ac45SSunil Goutham 	/* Disable the Mbox interrupt */
28307304ac45SSunil Goutham 	rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFAF_MBOX_INT_ENA_W1C,
28317304ac45SSunil Goutham 		    INTR_MASK(rvu->hw->total_pfs) & ~1ULL);
28327304ac45SSunil Goutham 
28339fe4ebf7SGeetha sowjanya 	/* Disable the PF FLR interrupt */
28349fe4ebf7SGeetha sowjanya 	rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFFLR_INT_ENA_W1C,
28359fe4ebf7SGeetha sowjanya 		    INTR_MASK(rvu->hw->total_pfs) & ~1ULL);
28369fe4ebf7SGeetha sowjanya 
28374fa91210SLinu Cherian 	/* Disable the PF ME interrupt */
28384fa91210SLinu Cherian 	rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFME_INT_ENA_W1C,
28394fa91210SLinu Cherian 		    INTR_MASK(rvu->hw->total_pfs) & ~1ULL);
28404fa91210SLinu Cherian 
28417304ac45SSunil Goutham 	for (irq = 0; irq < rvu->num_vec; irq++) {
2842ae2619ddSGeetha sowjanya 		if (rvu->irq_allocated[irq]) {
28437304ac45SSunil Goutham 			free_irq(pci_irq_vector(rvu->pdev, irq), rvu);
2844ae2619ddSGeetha sowjanya 			rvu->irq_allocated[irq] = false;
2845ae2619ddSGeetha sowjanya 		}
28467304ac45SSunil Goutham 	}
28477304ac45SSunil Goutham 
28487304ac45SSunil Goutham 	pci_free_irq_vectors(rvu->pdev);
28497304ac45SSunil Goutham 	rvu->num_vec = 0;
28507304ac45SSunil Goutham }
28517304ac45SSunil Goutham 
rvu_afvf_msix_vectors_num_ok(struct rvu * rvu)28529bd6caf3STomasz Duszynski static int rvu_afvf_msix_vectors_num_ok(struct rvu *rvu)
28539bd6caf3STomasz Duszynski {
28549bd6caf3STomasz Duszynski 	struct rvu_pfvf *pfvf = &rvu->pf[0];
28559bd6caf3STomasz Duszynski 	int offset;
28569bd6caf3STomasz Duszynski 
28579bd6caf3STomasz Duszynski 	pfvf = &rvu->pf[0];
28589bd6caf3STomasz Duszynski 	offset = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_PFX_INT_CFG(0)) & 0x3ff;
28599bd6caf3STomasz Duszynski 
28609bd6caf3STomasz Duszynski 	/* Make sure there are enough MSIX vectors configured so that
28619bd6caf3STomasz Duszynski 	 * VF interrupts can be handled. Offset equal to zero means
28629bd6caf3STomasz Duszynski 	 * that PF vectors are not configured and overlapping AF vectors.
28639bd6caf3STomasz Duszynski 	 */
28649bd6caf3STomasz Duszynski 	return (pfvf->msix.max >= RVU_AF_INT_VEC_CNT + RVU_PF_INT_VEC_CNT) &&
28659bd6caf3STomasz Duszynski 	       offset;
28669bd6caf3STomasz Duszynski }
28679bd6caf3STomasz Duszynski 
rvu_register_interrupts(struct rvu * rvu)28687304ac45SSunil Goutham static int rvu_register_interrupts(struct rvu *rvu)
28697304ac45SSunil Goutham {
2870465ed9c1SSunil Goutham 	int ret, offset, pf_vec_start;
28717304ac45SSunil Goutham 
28727304ac45SSunil Goutham 	rvu->num_vec = pci_msix_vec_count(rvu->pdev);
28737304ac45SSunil Goutham 
28747304ac45SSunil Goutham 	rvu->irq_name = devm_kmalloc_array(rvu->dev, rvu->num_vec,
28757304ac45SSunil Goutham 					   NAME_SIZE, GFP_KERNEL);
28767304ac45SSunil Goutham 	if (!rvu->irq_name)
28777304ac45SSunil Goutham 		return -ENOMEM;
28787304ac45SSunil Goutham 
28797304ac45SSunil Goutham 	rvu->irq_allocated = devm_kcalloc(rvu->dev, rvu->num_vec,
28807304ac45SSunil Goutham 					  sizeof(bool), GFP_KERNEL);
28817304ac45SSunil Goutham 	if (!rvu->irq_allocated)
28827304ac45SSunil Goutham 		return -ENOMEM;
28837304ac45SSunil Goutham 
28847304ac45SSunil Goutham 	/* Enable MSI-X */
28857304ac45SSunil Goutham 	ret = pci_alloc_irq_vectors(rvu->pdev, rvu->num_vec,
28867304ac45SSunil Goutham 				    rvu->num_vec, PCI_IRQ_MSIX);
28877304ac45SSunil Goutham 	if (ret < 0) {
28887304ac45SSunil Goutham 		dev_err(rvu->dev,
28897304ac45SSunil Goutham 			"RVUAF: Request for %d msix vectors failed, ret %d\n",
28907304ac45SSunil Goutham 			rvu->num_vec, ret);
28917304ac45SSunil Goutham 		return ret;
28927304ac45SSunil Goutham 	}
28937304ac45SSunil Goutham 
28947304ac45SSunil Goutham 	/* Register mailbox interrupt handler */
28957304ac45SSunil Goutham 	sprintf(&rvu->irq_name[RVU_AF_INT_VEC_MBOX * NAME_SIZE], "RVUAF Mbox");
28967304ac45SSunil Goutham 	ret = request_irq(pci_irq_vector(rvu->pdev, RVU_AF_INT_VEC_MBOX),
2897dc29dd00SSubbaraya Sundeep 			  rvu_mbox_pf_intr_handler, 0,
28987304ac45SSunil Goutham 			  &rvu->irq_name[RVU_AF_INT_VEC_MBOX * NAME_SIZE], rvu);
28997304ac45SSunil Goutham 	if (ret) {
29007304ac45SSunil Goutham 		dev_err(rvu->dev,
29017304ac45SSunil Goutham 			"RVUAF: IRQ registration failed for mbox irq\n");
29027304ac45SSunil Goutham 		goto fail;
29037304ac45SSunil Goutham 	}
29047304ac45SSunil Goutham 
29057304ac45SSunil Goutham 	rvu->irq_allocated[RVU_AF_INT_VEC_MBOX] = true;
29067304ac45SSunil Goutham 
29077304ac45SSunil Goutham 	/* Enable mailbox interrupts from all PFs */
29087304ac45SSunil Goutham 	rvu_enable_mbox_intr(rvu);
29097304ac45SSunil Goutham 
29109fe4ebf7SGeetha sowjanya 	/* Register FLR interrupt handler */
29119fe4ebf7SGeetha sowjanya 	sprintf(&rvu->irq_name[RVU_AF_INT_VEC_PFFLR * NAME_SIZE],
29129fe4ebf7SGeetha sowjanya 		"RVUAF FLR");
29139fe4ebf7SGeetha sowjanya 	ret = request_irq(pci_irq_vector(rvu->pdev, RVU_AF_INT_VEC_PFFLR),
29149fe4ebf7SGeetha sowjanya 			  rvu_flr_intr_handler, 0,
29159fe4ebf7SGeetha sowjanya 			  &rvu->irq_name[RVU_AF_INT_VEC_PFFLR * NAME_SIZE],
29169fe4ebf7SGeetha sowjanya 			  rvu);
29179fe4ebf7SGeetha sowjanya 	if (ret) {
29189fe4ebf7SGeetha sowjanya 		dev_err(rvu->dev,
29199fe4ebf7SGeetha sowjanya 			"RVUAF: IRQ registration failed for FLR\n");
29209fe4ebf7SGeetha sowjanya 		goto fail;
29219fe4ebf7SGeetha sowjanya 	}
29229fe4ebf7SGeetha sowjanya 	rvu->irq_allocated[RVU_AF_INT_VEC_PFFLR] = true;
29239fe4ebf7SGeetha sowjanya 
29249fe4ebf7SGeetha sowjanya 	/* Enable FLR interrupt for all PFs*/
29259fe4ebf7SGeetha sowjanya 	rvu_write64(rvu, BLKADDR_RVUM,
29269fe4ebf7SGeetha sowjanya 		    RVU_AF_PFFLR_INT, INTR_MASK(rvu->hw->total_pfs));
29279fe4ebf7SGeetha sowjanya 
29289fe4ebf7SGeetha sowjanya 	rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFFLR_INT_ENA_W1S,
29299fe4ebf7SGeetha sowjanya 		    INTR_MASK(rvu->hw->total_pfs) & ~1ULL);
29309fe4ebf7SGeetha sowjanya 
29314fa91210SLinu Cherian 	/* Register ME interrupt handler */
29324fa91210SLinu Cherian 	sprintf(&rvu->irq_name[RVU_AF_INT_VEC_PFME * NAME_SIZE],
29334fa91210SLinu Cherian 		"RVUAF ME");
29344fa91210SLinu Cherian 	ret = request_irq(pci_irq_vector(rvu->pdev, RVU_AF_INT_VEC_PFME),
29354fa91210SLinu Cherian 			  rvu_me_pf_intr_handler, 0,
29364fa91210SLinu Cherian 			  &rvu->irq_name[RVU_AF_INT_VEC_PFME * NAME_SIZE],
29374fa91210SLinu Cherian 			  rvu);
29384fa91210SLinu Cherian 	if (ret) {
29394fa91210SLinu Cherian 		dev_err(rvu->dev,
29404fa91210SLinu Cherian 			"RVUAF: IRQ registration failed for ME\n");
29414fa91210SLinu Cherian 	}
29424fa91210SLinu Cherian 	rvu->irq_allocated[RVU_AF_INT_VEC_PFME] = true;
29434fa91210SLinu Cherian 
2944549c35ecSSunil Goutham 	/* Clear TRPEND bit for all PF */
2945549c35ecSSunil Goutham 	rvu_write64(rvu, BLKADDR_RVUM,
2946549c35ecSSunil Goutham 		    RVU_AF_PFTRPEND, INTR_MASK(rvu->hw->total_pfs));
29474fa91210SLinu Cherian 	/* Enable ME interrupt for all PFs*/
29484fa91210SLinu Cherian 	rvu_write64(rvu, BLKADDR_RVUM,
29494fa91210SLinu Cherian 		    RVU_AF_PFME_INT, INTR_MASK(rvu->hw->total_pfs));
29504fa91210SLinu Cherian 
29514fa91210SLinu Cherian 	rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFME_INT_ENA_W1S,
29524fa91210SLinu Cherian 		    INTR_MASK(rvu->hw->total_pfs) & ~1ULL);
29534fa91210SLinu Cherian 
29549bd6caf3STomasz Duszynski 	if (!rvu_afvf_msix_vectors_num_ok(rvu))
29559bd6caf3STomasz Duszynski 		return 0;
29569bd6caf3STomasz Duszynski 
29579bd6caf3STomasz Duszynski 	/* Get PF MSIX vectors offset. */
2958465ed9c1SSunil Goutham 	pf_vec_start = rvu_read64(rvu, BLKADDR_RVUM,
2959465ed9c1SSunil Goutham 				  RVU_PRIV_PFX_INT_CFG(0)) & 0x3ff;
29609bd6caf3STomasz Duszynski 
29619bd6caf3STomasz Duszynski 	/* Register MBOX0 interrupt. */
2962465ed9c1SSunil Goutham 	offset = pf_vec_start + RVU_PF_INT_VEC_VFPF_MBOX0;
29639bd6caf3STomasz Duszynski 	sprintf(&rvu->irq_name[offset * NAME_SIZE], "RVUAFVF Mbox0");
29649bd6caf3STomasz Duszynski 	ret = request_irq(pci_irq_vector(rvu->pdev, offset),
29659bd6caf3STomasz Duszynski 			  rvu_mbox_intr_handler, 0,
29669bd6caf3STomasz Duszynski 			  &rvu->irq_name[offset * NAME_SIZE],
29679bd6caf3STomasz Duszynski 			  rvu);
29689bd6caf3STomasz Duszynski 	if (ret)
29699bd6caf3STomasz Duszynski 		dev_err(rvu->dev,
29709bd6caf3STomasz Duszynski 			"RVUAF: IRQ registration failed for Mbox0\n");
29719bd6caf3STomasz Duszynski 
29729bd6caf3STomasz Duszynski 	rvu->irq_allocated[offset] = true;
29739bd6caf3STomasz Duszynski 
29749bd6caf3STomasz Duszynski 	/* Register MBOX1 interrupt. MBOX1 IRQ number follows MBOX0 so
29759bd6caf3STomasz Duszynski 	 * simply increment current offset by 1.
29769bd6caf3STomasz Duszynski 	 */
2977465ed9c1SSunil Goutham 	offset = pf_vec_start + RVU_PF_INT_VEC_VFPF_MBOX1;
29789bd6caf3STomasz Duszynski 	sprintf(&rvu->irq_name[offset * NAME_SIZE], "RVUAFVF Mbox1");
29799bd6caf3STomasz Duszynski 	ret = request_irq(pci_irq_vector(rvu->pdev, offset),
29809bd6caf3STomasz Duszynski 			  rvu_mbox_intr_handler, 0,
29819bd6caf3STomasz Duszynski 			  &rvu->irq_name[offset * NAME_SIZE],
29829bd6caf3STomasz Duszynski 			  rvu);
29839bd6caf3STomasz Duszynski 	if (ret)
29849bd6caf3STomasz Duszynski 		dev_err(rvu->dev,
29859bd6caf3STomasz Duszynski 			"RVUAF: IRQ registration failed for Mbox1\n");
29869bd6caf3STomasz Duszynski 
29879bd6caf3STomasz Duszynski 	rvu->irq_allocated[offset] = true;
29889bd6caf3STomasz Duszynski 
2989465ed9c1SSunil Goutham 	/* Register FLR interrupt handler for AF's VFs */
2990465ed9c1SSunil Goutham 	offset = pf_vec_start + RVU_PF_INT_VEC_VFFLR0;
2991465ed9c1SSunil Goutham 	sprintf(&rvu->irq_name[offset * NAME_SIZE], "RVUAFVF FLR0");
2992465ed9c1SSunil Goutham 	ret = request_irq(pci_irq_vector(rvu->pdev, offset),
2993465ed9c1SSunil Goutham 			  rvu_flr_intr_handler, 0,
2994465ed9c1SSunil Goutham 			  &rvu->irq_name[offset * NAME_SIZE], rvu);
2995465ed9c1SSunil Goutham 	if (ret) {
2996465ed9c1SSunil Goutham 		dev_err(rvu->dev,
2997465ed9c1SSunil Goutham 			"RVUAF: IRQ registration failed for RVUAFVF FLR0\n");
2998465ed9c1SSunil Goutham 		goto fail;
2999465ed9c1SSunil Goutham 	}
3000465ed9c1SSunil Goutham 	rvu->irq_allocated[offset] = true;
3001465ed9c1SSunil Goutham 
3002465ed9c1SSunil Goutham 	offset = pf_vec_start + RVU_PF_INT_VEC_VFFLR1;
3003465ed9c1SSunil Goutham 	sprintf(&rvu->irq_name[offset * NAME_SIZE], "RVUAFVF FLR1");
3004465ed9c1SSunil Goutham 	ret = request_irq(pci_irq_vector(rvu->pdev, offset),
3005465ed9c1SSunil Goutham 			  rvu_flr_intr_handler, 0,
3006465ed9c1SSunil Goutham 			  &rvu->irq_name[offset * NAME_SIZE], rvu);
3007465ed9c1SSunil Goutham 	if (ret) {
3008465ed9c1SSunil Goutham 		dev_err(rvu->dev,
3009465ed9c1SSunil Goutham 			"RVUAF: IRQ registration failed for RVUAFVF FLR1\n");
3010465ed9c1SSunil Goutham 		goto fail;
3011465ed9c1SSunil Goutham 	}
3012465ed9c1SSunil Goutham 	rvu->irq_allocated[offset] = true;
3013465ed9c1SSunil Goutham 
30144fa91210SLinu Cherian 	/* Register ME interrupt handler for AF's VFs */
30154fa91210SLinu Cherian 	offset = pf_vec_start + RVU_PF_INT_VEC_VFME0;
30164fa91210SLinu Cherian 	sprintf(&rvu->irq_name[offset * NAME_SIZE], "RVUAFVF ME0");
30174fa91210SLinu Cherian 	ret = request_irq(pci_irq_vector(rvu->pdev, offset),
30184fa91210SLinu Cherian 			  rvu_me_vf_intr_handler, 0,
30194fa91210SLinu Cherian 			  &rvu->irq_name[offset * NAME_SIZE], rvu);
30204fa91210SLinu Cherian 	if (ret) {
30214fa91210SLinu Cherian 		dev_err(rvu->dev,
30224fa91210SLinu Cherian 			"RVUAF: IRQ registration failed for RVUAFVF ME0\n");
30234fa91210SLinu Cherian 		goto fail;
30244fa91210SLinu Cherian 	}
30254fa91210SLinu Cherian 	rvu->irq_allocated[offset] = true;
30264fa91210SLinu Cherian 
30274fa91210SLinu Cherian 	offset = pf_vec_start + RVU_PF_INT_VEC_VFME1;
30284fa91210SLinu Cherian 	sprintf(&rvu->irq_name[offset * NAME_SIZE], "RVUAFVF ME1");
30294fa91210SLinu Cherian 	ret = request_irq(pci_irq_vector(rvu->pdev, offset),
30304fa91210SLinu Cherian 			  rvu_me_vf_intr_handler, 0,
30314fa91210SLinu Cherian 			  &rvu->irq_name[offset * NAME_SIZE], rvu);
30324fa91210SLinu Cherian 	if (ret) {
30334fa91210SLinu Cherian 		dev_err(rvu->dev,
30344fa91210SLinu Cherian 			"RVUAF: IRQ registration failed for RVUAFVF ME1\n");
30354fa91210SLinu Cherian 		goto fail;
30364fa91210SLinu Cherian 	}
30374fa91210SLinu Cherian 	rvu->irq_allocated[offset] = true;
303848260907SSrujana Challa 
303948260907SSrujana Challa 	ret = rvu_cpt_register_interrupts(rvu);
304048260907SSrujana Challa 	if (ret)
304148260907SSrujana Challa 		goto fail;
304248260907SSrujana Challa 
30437304ac45SSunil Goutham 	return 0;
30447304ac45SSunil Goutham 
30457304ac45SSunil Goutham fail:
3046465ed9c1SSunil Goutham 	rvu_unregister_interrupts(rvu);
30477304ac45SSunil Goutham 	return ret;
30487304ac45SSunil Goutham }
30497304ac45SSunil Goutham 
rvu_flr_wq_destroy(struct rvu * rvu)30509fe4ebf7SGeetha sowjanya static void rvu_flr_wq_destroy(struct rvu *rvu)
30519fe4ebf7SGeetha sowjanya {
30529fe4ebf7SGeetha sowjanya 	if (rvu->flr_wq) {
30539fe4ebf7SGeetha sowjanya 		destroy_workqueue(rvu->flr_wq);
30549fe4ebf7SGeetha sowjanya 		rvu->flr_wq = NULL;
30559fe4ebf7SGeetha sowjanya 	}
30569fe4ebf7SGeetha sowjanya }
30579fe4ebf7SGeetha sowjanya 
rvu_flr_init(struct rvu * rvu)30589fe4ebf7SGeetha sowjanya static int rvu_flr_init(struct rvu *rvu)
30599fe4ebf7SGeetha sowjanya {
3060465ed9c1SSunil Goutham 	int dev, num_devs;
30619fe4ebf7SGeetha sowjanya 	u64 cfg;
30629fe4ebf7SGeetha sowjanya 	int pf;
30639fe4ebf7SGeetha sowjanya 
30649fe4ebf7SGeetha sowjanya 	/* Enable FLR for all PFs*/
3065465ed9c1SSunil Goutham 	for (pf = 0; pf < rvu->hw->total_pfs; pf++) {
30669fe4ebf7SGeetha sowjanya 		cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_PFX_CFG(pf));
30679fe4ebf7SGeetha sowjanya 		rvu_write64(rvu, BLKADDR_RVUM, RVU_PRIV_PFX_CFG(pf),
30689fe4ebf7SGeetha sowjanya 			    cfg | BIT_ULL(22));
30699fe4ebf7SGeetha sowjanya 	}
30709fe4ebf7SGeetha sowjanya 
3071289f9746STejun Heo 	rvu->flr_wq = alloc_ordered_workqueue("rvu_afpf_flr",
3072289f9746STejun Heo 					      WQ_HIGHPRI | WQ_MEM_RECLAIM);
30739fe4ebf7SGeetha sowjanya 	if (!rvu->flr_wq)
30749fe4ebf7SGeetha sowjanya 		return -ENOMEM;
30759fe4ebf7SGeetha sowjanya 
3076465ed9c1SSunil Goutham 	num_devs = rvu->hw->total_pfs + pci_sriov_get_totalvfs(rvu->pdev);
3077465ed9c1SSunil Goutham 	rvu->flr_wrk = devm_kcalloc(rvu->dev, num_devs,
30789fe4ebf7SGeetha sowjanya 				    sizeof(struct rvu_work), GFP_KERNEL);
30799fe4ebf7SGeetha sowjanya 	if (!rvu->flr_wrk) {
30809fe4ebf7SGeetha sowjanya 		destroy_workqueue(rvu->flr_wq);
30819fe4ebf7SGeetha sowjanya 		return -ENOMEM;
30829fe4ebf7SGeetha sowjanya 	}
30839fe4ebf7SGeetha sowjanya 
3084465ed9c1SSunil Goutham 	for (dev = 0; dev < num_devs; dev++) {
3085465ed9c1SSunil Goutham 		rvu->flr_wrk[dev].rvu = rvu;
3086465ed9c1SSunil Goutham 		INIT_WORK(&rvu->flr_wrk[dev].work, rvu_flr_handler);
30879fe4ebf7SGeetha sowjanya 	}
30889fe4ebf7SGeetha sowjanya 
30899fe4ebf7SGeetha sowjanya 	mutex_init(&rvu->flr_lock);
30909fe4ebf7SGeetha sowjanya 
30919fe4ebf7SGeetha sowjanya 	return 0;
30929fe4ebf7SGeetha sowjanya }
30939fe4ebf7SGeetha sowjanya 
rvu_disable_afvf_intr(struct rvu * rvu)3094465ed9c1SSunil Goutham static void rvu_disable_afvf_intr(struct rvu *rvu)
30959bd6caf3STomasz Duszynski {
30969bd6caf3STomasz Duszynski 	int vfs = rvu->vfs;
30979bd6caf3STomasz Duszynski 
30989bd6caf3STomasz Duszynski 	rvupf_write64(rvu, RVU_PF_VFPF_MBOX_INT_ENA_W1CX(0), INTR_MASK(vfs));
3099465ed9c1SSunil Goutham 	rvupf_write64(rvu, RVU_PF_VFFLR_INT_ENA_W1CX(0), INTR_MASK(vfs));
31004fa91210SLinu Cherian 	rvupf_write64(rvu, RVU_PF_VFME_INT_ENA_W1CX(0), INTR_MASK(vfs));
3101465ed9c1SSunil Goutham 	if (vfs <= 64)
3102465ed9c1SSunil Goutham 		return;
3103465ed9c1SSunil Goutham 
31049bd6caf3STomasz Duszynski 	rvupf_write64(rvu, RVU_PF_VFPF_MBOX_INT_ENA_W1CX(1),
31059bd6caf3STomasz Duszynski 		      INTR_MASK(vfs - 64));
3106465ed9c1SSunil Goutham 	rvupf_write64(rvu, RVU_PF_VFFLR_INT_ENA_W1CX(1), INTR_MASK(vfs - 64));
31074fa91210SLinu Cherian 	rvupf_write64(rvu, RVU_PF_VFME_INT_ENA_W1CX(1), INTR_MASK(vfs - 64));
31089bd6caf3STomasz Duszynski }
31099bd6caf3STomasz Duszynski 
rvu_enable_afvf_intr(struct rvu * rvu)3110465ed9c1SSunil Goutham static void rvu_enable_afvf_intr(struct rvu *rvu)
31119bd6caf3STomasz Duszynski {
31129bd6caf3STomasz Duszynski 	int vfs = rvu->vfs;
31139bd6caf3STomasz Duszynski 
31149bd6caf3STomasz Duszynski 	/* Clear any pending interrupts and enable AF VF interrupts for
31159bd6caf3STomasz Duszynski 	 * the first 64 VFs.
31169bd6caf3STomasz Duszynski 	 */
3117465ed9c1SSunil Goutham 	/* Mbox */
31189bd6caf3STomasz Duszynski 	rvupf_write64(rvu, RVU_PF_VFPF_MBOX_INTX(0), INTR_MASK(vfs));
31199bd6caf3STomasz Duszynski 	rvupf_write64(rvu, RVU_PF_VFPF_MBOX_INT_ENA_W1SX(0), INTR_MASK(vfs));
31209bd6caf3STomasz Duszynski 
3121465ed9c1SSunil Goutham 	/* FLR */
3122465ed9c1SSunil Goutham 	rvupf_write64(rvu, RVU_PF_VFFLR_INTX(0), INTR_MASK(vfs));
3123465ed9c1SSunil Goutham 	rvupf_write64(rvu, RVU_PF_VFFLR_INT_ENA_W1SX(0), INTR_MASK(vfs));
31244fa91210SLinu Cherian 	rvupf_write64(rvu, RVU_PF_VFME_INT_ENA_W1SX(0), INTR_MASK(vfs));
3125465ed9c1SSunil Goutham 
31269bd6caf3STomasz Duszynski 	/* Same for remaining VFs, if any. */
31279bd6caf3STomasz Duszynski 	if (vfs <= 64)
31289bd6caf3STomasz Duszynski 		return;
31299bd6caf3STomasz Duszynski 
31309bd6caf3STomasz Duszynski 	rvupf_write64(rvu, RVU_PF_VFPF_MBOX_INTX(1), INTR_MASK(vfs - 64));
31319bd6caf3STomasz Duszynski 	rvupf_write64(rvu, RVU_PF_VFPF_MBOX_INT_ENA_W1SX(1),
31329bd6caf3STomasz Duszynski 		      INTR_MASK(vfs - 64));
3133465ed9c1SSunil Goutham 
3134465ed9c1SSunil Goutham 	rvupf_write64(rvu, RVU_PF_VFFLR_INTX(1), INTR_MASK(vfs - 64));
3135465ed9c1SSunil Goutham 	rvupf_write64(rvu, RVU_PF_VFFLR_INT_ENA_W1SX(1), INTR_MASK(vfs - 64));
31364fa91210SLinu Cherian 	rvupf_write64(rvu, RVU_PF_VFME_INT_ENA_W1SX(1), INTR_MASK(vfs - 64));
31379bd6caf3STomasz Duszynski }
31389bd6caf3STomasz Duszynski 
rvu_get_num_lbk_chans(void)3139041a1c17SSubbaraya Sundeep int rvu_get_num_lbk_chans(void)
31409bd6caf3STomasz Duszynski {
31419bd6caf3STomasz Duszynski 	struct pci_dev *pdev;
31429bd6caf3STomasz Duszynski 	void __iomem *base;
31439bd6caf3STomasz Duszynski 	int ret = -EIO;
31449bd6caf3STomasz Duszynski 
31459bd6caf3STomasz Duszynski 	pdev = pci_get_device(PCI_VENDOR_ID_CAVIUM, PCI_DEVID_OCTEONTX2_LBK,
31469bd6caf3STomasz Duszynski 			      NULL);
31479bd6caf3STomasz Duszynski 	if (!pdev)
31489bd6caf3STomasz Duszynski 		goto err;
31499bd6caf3STomasz Duszynski 
31509bd6caf3STomasz Duszynski 	base = pci_ioremap_bar(pdev, 0);
31519bd6caf3STomasz Duszynski 	if (!base)
31529bd6caf3STomasz Duszynski 		goto err_put;
31539bd6caf3STomasz Duszynski 
31549bd6caf3STomasz Duszynski 	/* Read number of available LBK channels from LBK(0)_CONST register. */
31559bd6caf3STomasz Duszynski 	ret = (readq(base + 0x10) >> 32) & 0xffff;
31569bd6caf3STomasz Duszynski 	iounmap(base);
31579bd6caf3STomasz Duszynski err_put:
31589bd6caf3STomasz Duszynski 	pci_dev_put(pdev);
31599bd6caf3STomasz Duszynski err:
31609bd6caf3STomasz Duszynski 	return ret;
31619bd6caf3STomasz Duszynski }
31629bd6caf3STomasz Duszynski 
rvu_enable_sriov(struct rvu * rvu)31639bd6caf3STomasz Duszynski static int rvu_enable_sriov(struct rvu *rvu)
31649bd6caf3STomasz Duszynski {
31659bd6caf3STomasz Duszynski 	struct pci_dev *pdev = rvu->pdev;
31669bd6caf3STomasz Duszynski 	int err, chans, vfs;
31679bd6caf3STomasz Duszynski 
31689bd6caf3STomasz Duszynski 	if (!rvu_afvf_msix_vectors_num_ok(rvu)) {
31699bd6caf3STomasz Duszynski 		dev_warn(&pdev->dev,
31709bd6caf3STomasz Duszynski 			 "Skipping SRIOV enablement since not enough IRQs are available\n");
31719bd6caf3STomasz Duszynski 		return 0;
31729bd6caf3STomasz Duszynski 	}
31739bd6caf3STomasz Duszynski 
3174041a1c17SSubbaraya Sundeep 	chans = rvu_get_num_lbk_chans();
31759bd6caf3STomasz Duszynski 	if (chans < 0)
31769bd6caf3STomasz Duszynski 		return chans;
31779bd6caf3STomasz Duszynski 
31789bd6caf3STomasz Duszynski 	vfs = pci_sriov_get_totalvfs(pdev);
31799bd6caf3STomasz Duszynski 
31809bd6caf3STomasz Duszynski 	/* Limit VFs in case we have more VFs than LBK channels available. */
31819bd6caf3STomasz Duszynski 	if (vfs > chans)
31829bd6caf3STomasz Duszynski 		vfs = chans;
31839bd6caf3STomasz Duszynski 
31849bd6caf3STomasz Duszynski 	if (!vfs)
31859bd6caf3STomasz Duszynski 		return 0;
31869bd6caf3STomasz Duszynski 
3187fa2bf6baSSubbaraya Sundeep 	/* LBK channel number 63 is used for switching packets between
3188fa2bf6baSSubbaraya Sundeep 	 * CGX mapped VFs. Hence limit LBK pairs till 62 only.
3189fa2bf6baSSubbaraya Sundeep 	 */
3190fa2bf6baSSubbaraya Sundeep 	if (vfs > 62)
3191fa2bf6baSSubbaraya Sundeep 		vfs = 62;
3192fa2bf6baSSubbaraya Sundeep 
31939bd6caf3STomasz Duszynski 	/* Save VFs number for reference in VF interrupts handlers.
31949bd6caf3STomasz Duszynski 	 * Since interrupts might start arriving during SRIOV enablement
31959bd6caf3STomasz Duszynski 	 * ordinary API cannot be used to get number of enabled VFs.
31969bd6caf3STomasz Duszynski 	 */
31979bd6caf3STomasz Duszynski 	rvu->vfs = vfs;
31989bd6caf3STomasz Duszynski 
31999bd6caf3STomasz Duszynski 	err = rvu_mbox_init(rvu, &rvu->afvf_wq_info, TYPE_AFVF, vfs,
32009bd6caf3STomasz Duszynski 			    rvu_afvf_mbox_handler, rvu_afvf_mbox_up_handler);
32019bd6caf3STomasz Duszynski 	if (err)
32029bd6caf3STomasz Duszynski 		return err;
32039bd6caf3STomasz Duszynski 
3204465ed9c1SSunil Goutham 	rvu_enable_afvf_intr(rvu);
32059bd6caf3STomasz Duszynski 	/* Make sure IRQs are enabled before SRIOV. */
32069bd6caf3STomasz Duszynski 	mb();
32079bd6caf3STomasz Duszynski 
32089bd6caf3STomasz Duszynski 	err = pci_enable_sriov(pdev, vfs);
32099bd6caf3STomasz Duszynski 	if (err) {
3210465ed9c1SSunil Goutham 		rvu_disable_afvf_intr(rvu);
32119bd6caf3STomasz Duszynski 		rvu_mbox_destroy(&rvu->afvf_wq_info);
32129bd6caf3STomasz Duszynski 		return err;
32139bd6caf3STomasz Duszynski 	}
32149bd6caf3STomasz Duszynski 
32159bd6caf3STomasz Duszynski 	return 0;
32169bd6caf3STomasz Duszynski }
32179bd6caf3STomasz Duszynski 
rvu_disable_sriov(struct rvu * rvu)32189bd6caf3STomasz Duszynski static void rvu_disable_sriov(struct rvu *rvu)
32199bd6caf3STomasz Duszynski {
3220465ed9c1SSunil Goutham 	rvu_disable_afvf_intr(rvu);
32219bd6caf3STomasz Duszynski 	rvu_mbox_destroy(&rvu->afvf_wq_info);
32229bd6caf3STomasz Duszynski 	pci_disable_sriov(rvu->pdev);
32239bd6caf3STomasz Duszynski }
32249bd6caf3STomasz Duszynski 
rvu_update_module_params(struct rvu * rvu)322523705adbSVamsi Attunuru static void rvu_update_module_params(struct rvu *rvu)
322623705adbSVamsi Attunuru {
322723705adbSVamsi Attunuru 	const char *default_pfl_name = "default";
322823705adbSVamsi Attunuru 
322923705adbSVamsi Attunuru 	strscpy(rvu->mkex_pfl_name,
323023705adbSVamsi Attunuru 		mkex_profile ? mkex_profile : default_pfl_name, MKEX_NAME_LEN);
32313a724415SStanislaw Kardach 	strscpy(rvu->kpu_pfl_name,
32323a724415SStanislaw Kardach 		kpu_profile ? kpu_profile : default_pfl_name, KPU_NAME_LEN);
323323705adbSVamsi Attunuru }
323423705adbSVamsi Attunuru 
rvu_probe(struct pci_dev * pdev,const struct pci_device_id * id)323554494aa5SSunil Goutham static int rvu_probe(struct pci_dev *pdev, const struct pci_device_id *id)
323654494aa5SSunil Goutham {
323754494aa5SSunil Goutham 	struct device *dev = &pdev->dev;
323854494aa5SSunil Goutham 	struct rvu *rvu;
323954494aa5SSunil Goutham 	int    err;
324054494aa5SSunil Goutham 
324154494aa5SSunil Goutham 	rvu = devm_kzalloc(dev, sizeof(*rvu), GFP_KERNEL);
324254494aa5SSunil Goutham 	if (!rvu)
324354494aa5SSunil Goutham 		return -ENOMEM;
324454494aa5SSunil Goutham 
324554d55781SSunil Goutham 	rvu->hw = devm_kzalloc(dev, sizeof(struct rvu_hwinfo), GFP_KERNEL);
324654d55781SSunil Goutham 	if (!rvu->hw) {
324754d55781SSunil Goutham 		devm_kfree(dev, rvu);
324854d55781SSunil Goutham 		return -ENOMEM;
324954d55781SSunil Goutham 	}
325054d55781SSunil Goutham 
325154494aa5SSunil Goutham 	pci_set_drvdata(pdev, rvu);
325254494aa5SSunil Goutham 	rvu->pdev = pdev;
325354494aa5SSunil Goutham 	rvu->dev = &pdev->dev;
325454494aa5SSunil Goutham 
325554494aa5SSunil Goutham 	err = pci_enable_device(pdev);
325654494aa5SSunil Goutham 	if (err) {
325754494aa5SSunil Goutham 		dev_err(dev, "Failed to enable PCI device\n");
325854494aa5SSunil Goutham 		goto err_freemem;
325954494aa5SSunil Goutham 	}
326054494aa5SSunil Goutham 
326154494aa5SSunil Goutham 	err = pci_request_regions(pdev, DRV_NAME);
326254494aa5SSunil Goutham 	if (err) {
326354494aa5SSunil Goutham 		dev_err(dev, "PCI request regions failed 0x%x\n", err);
326454494aa5SSunil Goutham 		goto err_disable_device;
326554494aa5SSunil Goutham 	}
326654494aa5SSunil Goutham 
3267549c35ecSSunil Goutham 	err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
326854494aa5SSunil Goutham 	if (err) {
3269549c35ecSSunil Goutham 		dev_err(dev, "DMA mask config failed, abort\n");
327054494aa5SSunil Goutham 		goto err_release_regions;
327154494aa5SSunil Goutham 	}
327254494aa5SSunil Goutham 
3273549c35ecSSunil Goutham 	pci_set_master(pdev);
327454494aa5SSunil Goutham 
32754086f2a0SAleksey Makarov 	rvu->ptp = ptp_get();
32764086f2a0SAleksey Makarov 	if (IS_ERR(rvu->ptp)) {
32774086f2a0SAleksey Makarov 		err = PTR_ERR(rvu->ptp);
32787709fbd4SSai Krishna 		if (err)
32794086f2a0SAleksey Makarov 			goto err_release_regions;
32804086f2a0SAleksey Makarov 		rvu->ptp = NULL;
32814086f2a0SAleksey Makarov 	}
32824086f2a0SAleksey Makarov 
328354494aa5SSunil Goutham 	/* Map Admin function CSRs */
328454494aa5SSunil Goutham 	rvu->afreg_base = pcim_iomap(pdev, PCI_AF_REG_BAR_NUM, 0);
328554494aa5SSunil Goutham 	rvu->pfreg_base = pcim_iomap(pdev, PCI_PF_REG_BAR_NUM, 0);
328654494aa5SSunil Goutham 	if (!rvu->afreg_base || !rvu->pfreg_base) {
328754494aa5SSunil Goutham 		dev_err(dev, "Unable to map admin function CSRs, aborting\n");
328854494aa5SSunil Goutham 		err = -ENOMEM;
32894086f2a0SAleksey Makarov 		goto err_put_ptp;
329054494aa5SSunil Goutham 	}
329154494aa5SSunil Goutham 
329223705adbSVamsi Attunuru 	/* Store module params in rvu structure */
329323705adbSVamsi Attunuru 	rvu_update_module_params(rvu);
329423705adbSVamsi Attunuru 
329554d55781SSunil Goutham 	/* Check which blocks the HW supports */
329654d55781SSunil Goutham 	rvu_check_block_implemented(rvu);
329754d55781SSunil Goutham 
329854d55781SSunil Goutham 	rvu_reset_all_blocks(rvu);
329954d55781SSunil Goutham 
33005d9b976dSSunil Goutham 	rvu_setup_hw_capabilities(rvu);
33015d9b976dSSunil Goutham 
33021054a622SSunil Goutham 	err = rvu_setup_hw_resources(rvu);
33031054a622SSunil Goutham 	if (err)
33044086f2a0SAleksey Makarov 		goto err_put_ptp;
33051054a622SSunil Goutham 
33069bdc47a6STomasz Duszynski 	/* Init mailbox btw AF and PFs */
33079bdc47a6STomasz Duszynski 	err = rvu_mbox_init(rvu, &rvu->afpf_wq_info, TYPE_AFPF,
33089bdc47a6STomasz Duszynski 			    rvu->hw->total_pfs, rvu_afpf_mbox_handler,
33099bdc47a6STomasz Duszynski 			    rvu_afpf_mbox_up_handler);
3310a83bdadaSSunil Goutham 	if (err) {
3311a83bdadaSSunil Goutham 		dev_err(dev, "%s: Failed to initialize mbox\n", __func__);
33127304ac45SSunil Goutham 		goto err_hwsetup;
3313a83bdadaSSunil Goutham 	}
33147304ac45SSunil Goutham 
33159fe4ebf7SGeetha sowjanya 	err = rvu_flr_init(rvu);
3316a83bdadaSSunil Goutham 	if (err) {
3317a83bdadaSSunil Goutham 		dev_err(dev, "%s: Failed to initialize flr\n", __func__);
331844990aaaSLinu Cherian 		goto err_mbox;
3319a83bdadaSSunil Goutham 	}
33207304ac45SSunil Goutham 
33219fe4ebf7SGeetha sowjanya 	err = rvu_register_interrupts(rvu);
3322a83bdadaSSunil Goutham 	if (err) {
3323a83bdadaSSunil Goutham 		dev_err(dev, "%s: Failed to register interrupts\n", __func__);
33249fe4ebf7SGeetha sowjanya 		goto err_flr;
3325a83bdadaSSunil Goutham 	}
33269fe4ebf7SGeetha sowjanya 
3327fae06da4SGeorge Cherian 	err = rvu_register_dl(rvu);
3328a83bdadaSSunil Goutham 	if (err) {
3329a83bdadaSSunil Goutham 		dev_err(dev, "%s: Failed to register devlink\n", __func__);
3330fae06da4SGeorge Cherian 		goto err_irq;
3331a83bdadaSSunil Goutham 	}
3332fae06da4SGeorge Cherian 
33338315f9b2SSunil Goutham 	rvu_setup_rvum_blk_revid(rvu);
33348315f9b2SSunil Goutham 
33359bd6caf3STomasz Duszynski 	/* Enable AF's VFs (if any) */
33369bd6caf3STomasz Duszynski 	err = rvu_enable_sriov(rvu);
3337a83bdadaSSunil Goutham 	if (err) {
3338a83bdadaSSunil Goutham 		dev_err(dev, "%s: Failed to enable sriov\n", __func__);
3339fae06da4SGeorge Cherian 		goto err_dl;
3340a83bdadaSSunil Goutham 	}
33419bd6caf3STomasz Duszynski 
334223205e6dSChristina Jacob 	/* Initialize debugfs */
334323205e6dSChristina Jacob 	rvu_dbg_init(rvu);
334423205e6dSChristina Jacob 
334523109f8dSSubbaraya Sundeep 	mutex_init(&rvu->rswitch.switch_lock);
334623109f8dSSubbaraya Sundeep 
3347e266f663SSubbaraya Sundeep 	if (rvu->fwdata)
3348bdf79b12SSai Krishna 		ptp_start(rvu, rvu->fwdata->sclk, rvu->fwdata->ptp_ext_clk_rate,
334999bbc4aeSYi Guo 			  rvu->fwdata->ptp_ext_tstamp);
3350e266f663SSubbaraya Sundeep 
335154494aa5SSunil Goutham 	return 0;
3352fae06da4SGeorge Cherian err_dl:
3353fae06da4SGeorge Cherian 	rvu_unregister_dl(rvu);
33549bd6caf3STomasz Duszynski err_irq:
33559bd6caf3STomasz Duszynski 	rvu_unregister_interrupts(rvu);
33569fe4ebf7SGeetha sowjanya err_flr:
33579fe4ebf7SGeetha sowjanya 	rvu_flr_wq_destroy(rvu);
33587304ac45SSunil Goutham err_mbox:
33599bdc47a6STomasz Duszynski 	rvu_mbox_destroy(&rvu->afpf_wq_info);
33607304ac45SSunil Goutham err_hwsetup:
336144990aaaSLinu Cherian 	rvu_cgx_exit(rvu);
33624f4eebf2SLinu Cherian 	rvu_fwdata_exit(rvu);
33636c635f78SGeetha sowjanya 	rvu_mcs_exit(rvu);
33647304ac45SSunil Goutham 	rvu_reset_all_blocks(rvu);
33657304ac45SSunil Goutham 	rvu_free_hw_resources(rvu);
33668315f9b2SSunil Goutham 	rvu_clear_rvum_blk_revid(rvu);
33674086f2a0SAleksey Makarov err_put_ptp:
33684086f2a0SAleksey Makarov 	ptp_put(rvu->ptp);
336954494aa5SSunil Goutham err_release_regions:
337054494aa5SSunil Goutham 	pci_release_regions(pdev);
337154494aa5SSunil Goutham err_disable_device:
337254494aa5SSunil Goutham 	pci_disable_device(pdev);
337354494aa5SSunil Goutham err_freemem:
337454494aa5SSunil Goutham 	pci_set_drvdata(pdev, NULL);
337554d55781SSunil Goutham 	devm_kfree(&pdev->dev, rvu->hw);
337654494aa5SSunil Goutham 	devm_kfree(dev, rvu);
337754494aa5SSunil Goutham 	return err;
337854494aa5SSunil Goutham }
337954494aa5SSunil Goutham 
rvu_remove(struct pci_dev * pdev)338054494aa5SSunil Goutham static void rvu_remove(struct pci_dev *pdev)
338154494aa5SSunil Goutham {
338254494aa5SSunil Goutham 	struct rvu *rvu = pci_get_drvdata(pdev);
338354494aa5SSunil Goutham 
338423205e6dSChristina Jacob 	rvu_dbg_exit(rvu);
3385fae06da4SGeorge Cherian 	rvu_unregister_dl(rvu);
3386ae2619ddSGeetha sowjanya 	rvu_unregister_interrupts(rvu);
33879fe4ebf7SGeetha sowjanya 	rvu_flr_wq_destroy(rvu);
338844990aaaSLinu Cherian 	rvu_cgx_exit(rvu);
33894f4eebf2SLinu Cherian 	rvu_fwdata_exit(rvu);
33906c635f78SGeetha sowjanya 	rvu_mcs_exit(rvu);
33919bdc47a6STomasz Duszynski 	rvu_mbox_destroy(&rvu->afpf_wq_info);
33929bd6caf3STomasz Duszynski 	rvu_disable_sriov(rvu);
33931054a622SSunil Goutham 	rvu_reset_all_blocks(rvu);
33941054a622SSunil Goutham 	rvu_free_hw_resources(rvu);
33958315f9b2SSunil Goutham 	rvu_clear_rvum_blk_revid(rvu);
33964086f2a0SAleksey Makarov 	ptp_put(rvu->ptp);
339754494aa5SSunil Goutham 	pci_release_regions(pdev);
339854494aa5SSunil Goutham 	pci_disable_device(pdev);
339954494aa5SSunil Goutham 	pci_set_drvdata(pdev, NULL);
340054494aa5SSunil Goutham 
340154d55781SSunil Goutham 	devm_kfree(&pdev->dev, rvu->hw);
340254494aa5SSunil Goutham 	devm_kfree(&pdev->dev, rvu);
340354494aa5SSunil Goutham }
340454494aa5SSunil Goutham 
340554494aa5SSunil Goutham static struct pci_driver rvu_driver = {
340654494aa5SSunil Goutham 	.name = DRV_NAME,
340754494aa5SSunil Goutham 	.id_table = rvu_id_table,
340854494aa5SSunil Goutham 	.probe = rvu_probe,
340954494aa5SSunil Goutham 	.remove = rvu_remove,
341054494aa5SSunil Goutham };
341154494aa5SSunil Goutham 
rvu_init_module(void)341254494aa5SSunil Goutham static int __init rvu_init_module(void)
341354494aa5SSunil Goutham {
34148e22f040SSunil Goutham 	int err;
34158e22f040SSunil Goutham 
341654494aa5SSunil Goutham 	pr_info("%s: %s\n", DRV_NAME, DRV_STRING);
341754494aa5SSunil Goutham 
34188e22f040SSunil Goutham 	err = pci_register_driver(&cgx_driver);
34198e22f040SSunil Goutham 	if (err < 0)
34208e22f040SSunil Goutham 		return err;
34218e22f040SSunil Goutham 
34224086f2a0SAleksey Makarov 	err = pci_register_driver(&ptp_driver);
34234086f2a0SAleksey Makarov 	if (err < 0)
34244086f2a0SAleksey Makarov 		goto ptp_err;
34254086f2a0SAleksey Makarov 
3426ca7f49ffSGeetha sowjanya 	err = pci_register_driver(&mcs_driver);
3427ca7f49ffSGeetha sowjanya 	if (err < 0)
3428ca7f49ffSGeetha sowjanya 		goto mcs_err;
3429ca7f49ffSGeetha sowjanya 
34308e22f040SSunil Goutham 	err =  pci_register_driver(&rvu_driver);
34318e22f040SSunil Goutham 	if (err < 0)
34324086f2a0SAleksey Makarov 		goto rvu_err;
34334086f2a0SAleksey Makarov 
34344086f2a0SAleksey Makarov 	return 0;
34354086f2a0SAleksey Makarov rvu_err:
3436ca7f49ffSGeetha sowjanya 	pci_unregister_driver(&mcs_driver);
3437ca7f49ffSGeetha sowjanya mcs_err:
34384086f2a0SAleksey Makarov 	pci_unregister_driver(&ptp_driver);
34394086f2a0SAleksey Makarov ptp_err:
34408e22f040SSunil Goutham 	pci_unregister_driver(&cgx_driver);
34418e22f040SSunil Goutham 
34428e22f040SSunil Goutham 	return err;
344354494aa5SSunil Goutham }
344454494aa5SSunil Goutham 
rvu_cleanup_module(void)344554494aa5SSunil Goutham static void __exit rvu_cleanup_module(void)
344654494aa5SSunil Goutham {
344754494aa5SSunil Goutham 	pci_unregister_driver(&rvu_driver);
3448ca7f49ffSGeetha sowjanya 	pci_unregister_driver(&mcs_driver);
34494086f2a0SAleksey Makarov 	pci_unregister_driver(&ptp_driver);
34508e22f040SSunil Goutham 	pci_unregister_driver(&cgx_driver);
345154494aa5SSunil Goutham }
345254494aa5SSunil Goutham 
345354494aa5SSunil Goutham module_init(rvu_init_module);
345454494aa5SSunil Goutham module_exit(rvu_cleanup_module);
3455