xref: /linux/drivers/net/ethernet/marvell/octeontx2/af/rvu.c (revision ab93e0dd72c37d378dd936f031ffb83ff2bd87ce)
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 #include "cn20k/reg.h"
246e54e1c5SHariprasad Kelam #include "cn20k/api.h"
2554494aa5SSunil Goutham 
2654494aa5SSunil Goutham #define DRV_NAME	"rvu_af"
27756051e2SSunil Goutham #define DRV_STRING      "Marvell OcteonTX2 RVU Admin Function Driver"
28756051e2SSunil Goutham 
29756051e2SSunil Goutham static void rvu_set_msix_offset(struct rvu *rvu, struct rvu_pfvf *pfvf,
30756051e2SSunil Goutham 				struct rvu_block *block, int lf);
31c554f9c1SGeetha sowjanya static void rvu_clear_msix_offset(struct rvu *rvu, struct rvu_pfvf *pfvf,
32756051e2SSunil Goutham 				  struct rvu_block *block, int lf);
339bdc47a6STomasz Duszynski static void __rvu_flr_handler(struct rvu *rvu, u16 pcifunc);
349bdc47a6STomasz Duszynski 
359bdc47a6STomasz Duszynski static int rvu_mbox_init(struct rvu *rvu, struct mbox_wq_info *mw,
369bdc47a6STomasz Duszynski 			 int type, int num,
379bdc47a6STomasz Duszynski 			 void (mbox_handler)(struct work_struct *),
389bdc47a6STomasz Duszynski 			 void (mbox_up_handler)(struct work_struct *));
399bdc47a6STomasz Duszynski static irqreturn_t rvu_mbox_pf_intr_handler(int irq, void *rvu_irq);
409bdc47a6STomasz Duszynski static irqreturn_t rvu_mbox_intr_handler(int irq, void *rvu_irq);
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)15951b2804cSSuman Ghosh 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(rvu->pdev, 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(rvu->pdev, 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_numvfs(struct rvu * rvu,int pf,int * numvfs,int * hwvf)403114a767eSSunil Goutham void rvu_get_pf_numvfs(struct rvu *rvu, int pf, int *numvfs, int *hwvf)
404114a767eSSunil Goutham {
405114a767eSSunil Goutham 	u64 cfg;
406114a767eSSunil Goutham 
407114a767eSSunil Goutham 	/* Get numVFs attached to this PF and first HWVF */
408756051e2SSunil Goutham 	cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_PFX_CFG(pf));
409756051e2SSunil Goutham 	if (numvfs)
410756051e2SSunil Goutham 		*numvfs = (cfg >> 12) & 0xFF;
411756051e2SSunil Goutham 	if (hwvf)
412756051e2SSunil Goutham 		*hwvf = cfg & 0xFFF;
413756051e2SSunil Goutham }
4149986066dSSubbaraya Sundeep 
rvu_get_hwvf(struct rvu * rvu,int pcifunc)415756051e2SSunil Goutham int rvu_get_hwvf(struct rvu *rvu, int pcifunc)
4169986066dSSubbaraya Sundeep {
417756051e2SSunil Goutham 	int pf, func;
418756051e2SSunil Goutham 	u64 cfg;
419756051e2SSunil Goutham 
4206c635f78SGeetha sowjanya 	pf = rvu_get_pf(rvu->pdev, pcifunc);
421114a767eSSunil Goutham 	func = pcifunc & RVU_PFVF_FUNC_MASK;
422114a767eSSunil Goutham 
423114a767eSSunil Goutham 	/* Get first HWVF attached to this PF */
424114a767eSSunil Goutham 	cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_PFX_CFG(pf));
425114a767eSSunil Goutham 
426114a767eSSunil Goutham 	return ((cfg & 0xFFF) + func - 1);
427114a767eSSunil Goutham }
428114a767eSSunil Goutham 
rvu_get_pfvf(struct rvu * rvu,int pcifunc)429114a767eSSunil Goutham struct rvu_pfvf *rvu_get_pfvf(struct rvu *rvu, int pcifunc)
430114a767eSSunil Goutham {
431114a767eSSunil Goutham 	/* Check if it is a PF or VF */
432114a767eSSunil Goutham 	if (pcifunc & RVU_PFVF_FUNC_MASK)
433114a767eSSunil Goutham 		return &rvu->hwvf[rvu_get_hwvf(rvu, pcifunc)];
434114a767eSSunil Goutham 	else
435114a767eSSunil Goutham 		return &rvu->pf[rvu_get_pf(rvu->pdev, pcifunc)];
436114a767eSSunil Goutham }
437114a767eSSunil Goutham 
is_pf_func_valid(struct rvu * rvu,u16 pcifunc)438114a767eSSunil Goutham static bool is_pf_func_valid(struct rvu *rvu, u16 pcifunc)
439114a767eSSunil Goutham {
440114a767eSSunil Goutham 	int pf, vf, nvfs;
441114a767eSSunil Goutham 	u64 cfg;
442114a767eSSunil Goutham 
443f325d3f4SSunil Goutham 	pf = rvu_get_pf(rvu->pdev, pcifunc);
444f325d3f4SSunil Goutham 	if (pf >= rvu->hw->total_pfs)
445f325d3f4SSunil Goutham 		return false;
446f325d3f4SSunil Goutham 
447f325d3f4SSunil Goutham 	if (!(pcifunc & RVU_PFVF_FUNC_MASK))
448f325d3f4SSunil Goutham 		return true;
449f325d3f4SSunil Goutham 
450f325d3f4SSunil Goutham 	/* Check if VF is within number of VFs attached to this PF */
451f325d3f4SSunil Goutham 	vf = (pcifunc & RVU_PFVF_FUNC_MASK) - 1;
452f325d3f4SSunil Goutham 	cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_PFX_CFG(pf));
453f325d3f4SSunil Goutham 	nvfs = (cfg >> 12) & 0xFF;
454f325d3f4SSunil Goutham 	if (vf >= nvfs)
455f325d3f4SSunil Goutham 		return false;
456f325d3f4SSunil Goutham 
457f325d3f4SSunil Goutham 	return true;
458f325d3f4SSunil Goutham }
459f325d3f4SSunil Goutham 
is_block_implemented(struct rvu_hwinfo * hw,int blkaddr)460f325d3f4SSunil Goutham bool is_block_implemented(struct rvu_hwinfo *hw, int blkaddr)
461f325d3f4SSunil Goutham {
462f325d3f4SSunil Goutham 	struct rvu_block *block;
463f325d3f4SSunil Goutham 
464f325d3f4SSunil Goutham 	if (blkaddr < BLKADDR_RVUM || blkaddr >= BLK_COUNT)
465746ea742SSunil Goutham 		return false;
466746ea742SSunil Goutham 
467746ea742SSunil Goutham 	block = &hw->block[blkaddr];
468746ea742SSunil Goutham 	return block->implemented;
469746ea742SSunil Goutham }
470746ea742SSunil Goutham 
rvu_check_block_implemented(struct rvu * rvu)471746ea742SSunil Goutham static void rvu_check_block_implemented(struct rvu *rvu)
472746ea742SSunil Goutham {
473746ea742SSunil Goutham 	struct rvu_hwinfo *hw = rvu->hw;
474746ea742SSunil Goutham 	struct rvu_block *block;
475746ea742SSunil Goutham 	int blkid;
47654d55781SSunil Goutham 	u64 cfg;
47754d55781SSunil Goutham 
47854d55781SSunil Goutham 	/* For each block check if 'implemented' bit is set */
47954d55781SSunil Goutham 	for (blkid = 0; blkid < BLK_COUNT; blkid++) {
48054d55781SSunil Goutham 		block = &hw->block[blkid];
48154d55781SSunil Goutham 		cfg = rvupf_read64(rvu, RVU_PF_BLOCK_ADDRX_DISC(blkid));
48254d55781SSunil Goutham 		if (cfg & BIT_ULL(11))
48354d55781SSunil Goutham 			block->implemented = true;
48454d55781SSunil Goutham 	}
48554d55781SSunil Goutham }
48654d55781SSunil Goutham 
rvu_setup_rvum_blk_revid(struct rvu * rvu)48754d55781SSunil Goutham static void rvu_setup_rvum_blk_revid(struct rvu *rvu)
48854d55781SSunil Goutham {
48954d55781SSunil Goutham 	rvu_write64(rvu, BLKADDR_RVUM,
49054d55781SSunil Goutham 		    RVU_PRIV_BLOCK_TYPEX_REV(BLKTYPE_RVUM),
49154d55781SSunil Goutham 		    RVU_BLK_RVUM_REVID);
4928315f9b2SSunil Goutham }
4938315f9b2SSunil Goutham 
rvu_clear_rvum_blk_revid(struct rvu * rvu)4948315f9b2SSunil Goutham static void rvu_clear_rvum_blk_revid(struct rvu *rvu)
4958315f9b2SSunil Goutham {
4968315f9b2SSunil Goutham 	rvu_write64(rvu, BLKADDR_RVUM,
4978315f9b2SSunil Goutham 		    RVU_PRIV_BLOCK_TYPEX_REV(BLKTYPE_RVUM), 0x00);
4988315f9b2SSunil Goutham }
4998315f9b2SSunil Goutham 
rvu_lf_reset(struct rvu * rvu,struct rvu_block * block,int lf)5008315f9b2SSunil Goutham int rvu_lf_reset(struct rvu *rvu, struct rvu_block *block, int lf)
5018315f9b2SSunil Goutham {
5028315f9b2SSunil Goutham 	int err;
5038315f9b2SSunil Goutham 
5048315f9b2SSunil Goutham 	if (!block->implemented)
5053fa4c323SSunil Goutham 		return 0;
5063fa4c323SSunil Goutham 
5073fa4c323SSunil Goutham 	rvu_write64(rvu, block->addr, block->lfreset_reg, lf | BIT_ULL(12));
5083fa4c323SSunil Goutham 	err = rvu_poll_reg(rvu, block->addr, block->lfreset_reg, BIT_ULL(12),
5093fa4c323SSunil Goutham 			   true);
5103fa4c323SSunil Goutham 	return err;
5113fa4c323SSunil Goutham }
5123fa4c323SSunil Goutham 
rvu_block_reset(struct rvu * rvu,int blkaddr,u64 rst_reg)5133fa4c323SSunil Goutham static void rvu_block_reset(struct rvu *rvu, int blkaddr, u64 rst_reg)
5143fa4c323SSunil Goutham {
5153fa4c323SSunil Goutham 	struct rvu_block *block = &rvu->hw->block[blkaddr];
5163fa4c323SSunil Goutham 	int err;
5173fa4c323SSunil Goutham 
51854d55781SSunil Goutham 	if (!block->implemented)
51954d55781SSunil Goutham 		return;
52054d55781SSunil Goutham 
521c0fa2cffSGeetha sowjanya 	rvu_write64(rvu, blkaddr, rst_reg, BIT_ULL(0));
52254d55781SSunil Goutham 	err = rvu_poll_reg(rvu, blkaddr, rst_reg, BIT_ULL(63), true);
52354d55781SSunil Goutham 	if (err) {
52454d55781SSunil Goutham 		dev_err(rvu->dev, "HW block:%d reset timeout retrying again\n", blkaddr);
52554d55781SSunil Goutham 		while (rvu_poll_reg(rvu, blkaddr, rst_reg, BIT_ULL(63), true) == -EBUSY)
52654d55781SSunil Goutham 			;
527c0fa2cffSGeetha sowjanya 	}
52803ffbc99SGeetha sowjanya }
52903ffbc99SGeetha sowjanya 
rvu_reset_all_blocks(struct rvu * rvu)53003ffbc99SGeetha sowjanya static void rvu_reset_all_blocks(struct rvu *rvu)
53103ffbc99SGeetha sowjanya {
53203ffbc99SGeetha sowjanya 	/* Do a HW reset of all RVU blocks */
53354d55781SSunil Goutham 	rvu_block_reset(rvu, BLKADDR_NPA, NPA_AF_BLK_RST);
53454d55781SSunil Goutham 	rvu_block_reset(rvu, BLKADDR_NIX0, NIX_AF_BLK_RST);
53554d55781SSunil Goutham 	rvu_block_reset(rvu, BLKADDR_NIX1, NIX_AF_BLK_RST);
53654d55781SSunil Goutham 	rvu_block_reset(rvu, BLKADDR_NPC, NPC_AF_BLK_RST);
53754d55781SSunil Goutham 	rvu_block_reset(rvu, BLKADDR_SSO, SSO_AF_BLK_RST);
53854d55781SSunil Goutham 	rvu_block_reset(rvu, BLKADDR_TIM, TIM_AF_BLK_RST);
53954d55781SSunil Goutham 	rvu_block_reset(rvu, BLKADDR_CPT0, CPT_AF_BLK_RST);
5409932fb72SRakesh Babu 	rvu_block_reset(rvu, BLKADDR_CPT1, CPT_AF_BLK_RST);
54154d55781SSunil Goutham 	rvu_block_reset(rvu, BLKADDR_NDC_NIX0_RX, NDC_AF_BLK_RST);
54254d55781SSunil Goutham 	rvu_block_reset(rvu, BLKADDR_NDC_NIX0_TX, NDC_AF_BLK_RST);
54354d55781SSunil Goutham 	rvu_block_reset(rvu, BLKADDR_NDC_NIX1_RX, NDC_AF_BLK_RST);
54454d55781SSunil Goutham 	rvu_block_reset(rvu, BLKADDR_NDC_NIX1_TX, NDC_AF_BLK_RST);
5459932fb72SRakesh Babu 	rvu_block_reset(rvu, BLKADDR_NDC_NPA0, NDC_AF_BLK_RST);
546c5a797e0SPrakash Brahmajyosyula }
547c5a797e0SPrakash Brahmajyosyula 
rvu_scan_block(struct rvu * rvu,struct rvu_block * block)5489932fb72SRakesh Babu static void rvu_scan_block(struct rvu *rvu, struct rvu_block *block)
5499932fb72SRakesh Babu {
550c5a797e0SPrakash Brahmajyosyula 	struct rvu_pfvf *pfvf;
55154d55781SSunil Goutham 	u64 cfg;
55254d55781SSunil Goutham 	int lf;
553114a767eSSunil Goutham 
554114a767eSSunil Goutham 	for (lf = 0; lf < block->lf.max; lf++) {
555114a767eSSunil Goutham 		cfg = rvu_read64(rvu, block->addr,
556114a767eSSunil Goutham 				 block->lfcfg_reg | (lf << block->lfshift));
557114a767eSSunil Goutham 		if (!(cfg & BIT_ULL(63)))
558114a767eSSunil Goutham 			continue;
559114a767eSSunil Goutham 
560114a767eSSunil Goutham 		/* Set this resource as being used */
561114a767eSSunil Goutham 		__set_bit(lf, block->lf.bmap);
562114a767eSSunil Goutham 
563114a767eSSunil Goutham 		/* Get, to whom this LF is attached */
564114a767eSSunil Goutham 		pfvf = rvu_get_pfvf(rvu, (cfg >> 8) & 0xFFFF);
565114a767eSSunil Goutham 		rvu_update_rsrc_map(rvu, pfvf, block,
566114a767eSSunil Goutham 				    (cfg >> 8) & 0xFFFF, lf, true);
567114a767eSSunil Goutham 
568114a767eSSunil Goutham 		/* Set start MSIX vector for this LF within this PF/VF */
569114a767eSSunil Goutham 		rvu_set_msix_offset(rvu, pfvf, block, lf);
570114a767eSSunil Goutham 	}
571114a767eSSunil Goutham }
572756051e2SSunil Goutham 
rvu_check_min_msix_vec(struct rvu * rvu,int nvecs,int pf,int vf)573756051e2SSunil Goutham static void rvu_check_min_msix_vec(struct rvu *rvu, int nvecs, int pf, int vf)
574756051e2SSunil Goutham {
575114a767eSSunil Goutham 	int min_vecs;
576114a767eSSunil Goutham 
577114a767eSSunil Goutham 	if (!vf)
578756051e2SSunil Goutham 		goto check_pf;
579756051e2SSunil Goutham 
580756051e2SSunil Goutham 	if (!nvecs) {
581756051e2SSunil Goutham 		dev_warn(rvu->dev,
582756051e2SSunil Goutham 			 "PF%d:VF%d is configured with zero msix vectors, %d\n",
583756051e2SSunil Goutham 			 pf, vf - 1, nvecs);
584756051e2SSunil Goutham 	}
585756051e2SSunil Goutham 	return;
586756051e2SSunil Goutham 
587756051e2SSunil Goutham check_pf:
588756051e2SSunil Goutham 	if (pf == 0)
589756051e2SSunil Goutham 		min_vecs = RVU_AF_INT_VEC_CNT + RVU_PF_INT_VEC_CNT;
590756051e2SSunil Goutham 	else
591756051e2SSunil Goutham 		min_vecs = RVU_PF_INT_VEC_CNT;
592756051e2SSunil Goutham 
593756051e2SSunil Goutham 	if (!(nvecs < min_vecs))
594756051e2SSunil Goutham 		return;
595756051e2SSunil Goutham 	dev_warn(rvu->dev,
596756051e2SSunil Goutham 		 "PF%d is configured with too few vectors, %d, min is %d\n",
597756051e2SSunil Goutham 		 pf, nvecs, min_vecs);
598756051e2SSunil Goutham }
599756051e2SSunil Goutham 
rvu_setup_msix_resources(struct rvu * rvu)600756051e2SSunil Goutham static int rvu_setup_msix_resources(struct rvu *rvu)
601756051e2SSunil Goutham {
602756051e2SSunil Goutham 	struct rvu_hwinfo *hw = rvu->hw;
603756051e2SSunil Goutham 	int pf, vf, numvfs, hwvf, err;
604756051e2SSunil Goutham 	int nvecs, offset, max_msix;
605756051e2SSunil Goutham 	struct rvu_pfvf *pfvf;
606756051e2SSunil Goutham 	u64 cfg, phy_addr;
607756051e2SSunil Goutham 	dma_addr_t iova;
608756051e2SSunil Goutham 
60934b34ee0SGeetha sowjanya 	for (pf = 0; pf < hw->total_pfs; pf++) {
610756051e2SSunil Goutham 		cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_PFX_CFG(pf));
61134b34ee0SGeetha sowjanya 		/* If PF is not enabled, nothing to do */
61234b34ee0SGeetha sowjanya 		if (!((cfg >> 20) & 0x01))
613756051e2SSunil Goutham 			continue;
614756051e2SSunil Goutham 
615756051e2SSunil Goutham 		rvu_get_pf_numvfs(rvu, pf, &numvfs, &hwvf);
616756051e2SSunil Goutham 
617756051e2SSunil Goutham 		pfvf = &rvu->pf[pf];
618756051e2SSunil Goutham 		/* Get num of MSIX vectors attached to this PF */
619756051e2SSunil Goutham 		cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_PFX_MSIX_CFG(pf));
620756051e2SSunil Goutham 		pfvf->msix.max = ((cfg >> 32) & 0xFFF) + 1;
621756051e2SSunil Goutham 		rvu_check_min_msix_vec(rvu, pfvf->msix.max, pf, 0);
622756051e2SSunil Goutham 
623756051e2SSunil Goutham 		/* Alloc msix bitmap for this PF */
624756051e2SSunil Goutham 		err = rvu_alloc_bitmap(&pfvf->msix);
625756051e2SSunil Goutham 		if (err)
626756051e2SSunil Goutham 			return err;
627756051e2SSunil Goutham 
628756051e2SSunil Goutham 		/* Allocate memory for MSIX vector to RVU block LF mapping */
629756051e2SSunil Goutham 		pfvf->msix_lfmap = devm_kcalloc(rvu->dev, pfvf->msix.max,
630756051e2SSunil Goutham 						sizeof(u16), GFP_KERNEL);
631756051e2SSunil Goutham 		if (!pfvf->msix_lfmap)
632756051e2SSunil Goutham 			return -ENOMEM;
633756051e2SSunil Goutham 
634756051e2SSunil Goutham 		/* For PF0 (AF) firmware will set msix vector offsets for
635756051e2SSunil Goutham 		 * AF, block AF and PF0_INT vectors, so jump to VFs.
636756051e2SSunil Goutham 		 */
637756051e2SSunil Goutham 		if (!pf)
638756051e2SSunil Goutham 			goto setup_vfmsix;
639756051e2SSunil Goutham 
640756051e2SSunil Goutham 		/* Set MSIX offset for PF's 'RVU_PF_INT_VEC' vectors.
641756051e2SSunil Goutham 		 * These are allocated on driver init and never freed,
642756051e2SSunil Goutham 		 * so no need to set 'msix_lfmap' for these.
643756051e2SSunil Goutham 		 */
644756051e2SSunil Goutham 		cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_PFX_INT_CFG(pf));
645756051e2SSunil Goutham 		nvecs = (cfg >> 12) & 0xFF;
646756051e2SSunil Goutham 		cfg &= ~0x7FFULL;
647756051e2SSunil Goutham 		offset = rvu_alloc_rsrc_contig(&pfvf->msix, nvecs);
648756051e2SSunil Goutham 		rvu_write64(rvu, BLKADDR_RVUM,
649756051e2SSunil Goutham 			    RVU_PRIV_PFX_INT_CFG(pf), cfg | offset);
650756051e2SSunil Goutham setup_vfmsix:
651756051e2SSunil Goutham 		/* Alloc msix bitmap for VFs */
652756051e2SSunil Goutham 		for (vf = 0; vf < numvfs; vf++) {
653756051e2SSunil Goutham 			pfvf =  &rvu->hwvf[hwvf + vf];
654756051e2SSunil Goutham 			/* Get num of MSIX vectors attached to this VF */
655756051e2SSunil Goutham 			cfg = rvu_read64(rvu, BLKADDR_RVUM,
656756051e2SSunil Goutham 					 RVU_PRIV_PFX_MSIX_CFG(pf));
657756051e2SSunil Goutham 			pfvf->msix.max = (cfg & 0xFFF) + 1;
658756051e2SSunil Goutham 			rvu_check_min_msix_vec(rvu, pfvf->msix.max, pf, vf + 1);
659756051e2SSunil Goutham 
660756051e2SSunil Goutham 			/* Alloc msix bitmap for this VF */
661756051e2SSunil Goutham 			err = rvu_alloc_bitmap(&pfvf->msix);
662756051e2SSunil Goutham 			if (err)
663756051e2SSunil Goutham 				return err;
664756051e2SSunil Goutham 
665756051e2SSunil Goutham 			pfvf->msix_lfmap =
666756051e2SSunil Goutham 				devm_kcalloc(rvu->dev, pfvf->msix.max,
667756051e2SSunil Goutham 					     sizeof(u16), GFP_KERNEL);
668756051e2SSunil Goutham 			if (!pfvf->msix_lfmap)
669756051e2SSunil Goutham 				return -ENOMEM;
670756051e2SSunil Goutham 
671756051e2SSunil Goutham 			/* Set MSIX offset for HWVF's 'RVU_VF_INT_VEC' vectors.
672756051e2SSunil Goutham 			 * These are allocated on driver init and never freed,
673756051e2SSunil Goutham 			 * so no need to set 'msix_lfmap' for these.
674756051e2SSunil Goutham 			 */
675756051e2SSunil Goutham 			cfg = rvu_read64(rvu, BLKADDR_RVUM,
676756051e2SSunil Goutham 					 RVU_PRIV_HWVFX_INT_CFG(hwvf + vf));
677756051e2SSunil Goutham 			nvecs = (cfg >> 12) & 0xFF;
678756051e2SSunil Goutham 			cfg &= ~0x7FFULL;
679756051e2SSunil Goutham 			offset = rvu_alloc_rsrc_contig(&pfvf->msix, nvecs);
680756051e2SSunil Goutham 			rvu_write64(rvu, BLKADDR_RVUM,
681756051e2SSunil Goutham 				    RVU_PRIV_HWVFX_INT_CFG(hwvf + vf),
682756051e2SSunil Goutham 				    cfg | offset);
683756051e2SSunil Goutham 		}
684756051e2SSunil Goutham 	}
685756051e2SSunil Goutham 
686756051e2SSunil Goutham 	/* HW interprets RVU_AF_MSIXTR_BASE address as an IOVA, hence
687756051e2SSunil Goutham 	 * create an IOMMU mapping for the physical address configured by
688756051e2SSunil Goutham 	 * firmware and reconfig RVU_AF_MSIXTR_BASE with IOVA.
689756051e2SSunil Goutham 	 */
690756051e2SSunil Goutham 	cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_CONST);
69134b34ee0SGeetha sowjanya 	max_msix = cfg & 0xFFFFF;
69246eb3c10SBjorn Helgaas 	if (rvu->fwdata && rvu->fwdata->msixtr_base)
69334b34ee0SGeetha sowjanya 		phy_addr = rvu->fwdata->msixtr_base;
69434b34ee0SGeetha sowjanya 	else
69534b34ee0SGeetha sowjanya 		phy_addr = rvu_read64(rvu, BLKADDR_RVUM, RVU_AF_MSIXTR_BASE);
69634b34ee0SGeetha sowjanya 
6974f4eebf2SLinu Cherian 	iova = dma_map_resource(rvu->dev, phy_addr,
6984f4eebf2SLinu Cherian 				max_msix * PCI_MSIX_ENTRY_SIZE,
6994f4eebf2SLinu Cherian 				DMA_BIDIRECTIONAL, 0);
70034b34ee0SGeetha sowjanya 
7014f4eebf2SLinu Cherian 	if (dma_mapping_error(rvu->dev, iova))
70234b34ee0SGeetha sowjanya 		return -ENOMEM;
70334b34ee0SGeetha sowjanya 
70434b34ee0SGeetha sowjanya 	rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_MSIXTR_BASE, (u64)iova);
70534b34ee0SGeetha sowjanya 	rvu->msix_base_iova = iova;
70634b34ee0SGeetha sowjanya 	rvu->msixtr_base_phy = phy_addr;
70734b34ee0SGeetha sowjanya 
70834b34ee0SGeetha sowjanya 	return 0;
70934b34ee0SGeetha sowjanya }
71034b34ee0SGeetha sowjanya 
rvu_reset_msix(struct rvu * rvu)7114f4eebf2SLinu Cherian static void rvu_reset_msix(struct rvu *rvu)
71234b34ee0SGeetha sowjanya {
713756051e2SSunil Goutham 	/* Restore msixtr base register */
714756051e2SSunil Goutham 	rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_MSIXTR_BASE,
715756051e2SSunil Goutham 		    rvu->msixtr_base_phy);
7164f4eebf2SLinu Cherian }
7174f4eebf2SLinu Cherian 
rvu_free_hw_resources(struct rvu * rvu)7184f4eebf2SLinu Cherian static void rvu_free_hw_resources(struct rvu *rvu)
7194f4eebf2SLinu Cherian {
7204f4eebf2SLinu Cherian 	struct rvu_hwinfo *hw = rvu->hw;
7214f4eebf2SLinu Cherian 	struct rvu_block *block;
7224f4eebf2SLinu Cherian 	struct rvu_pfvf  *pfvf;
7231054a622SSunil Goutham 	int id, max_msix;
7241054a622SSunil Goutham 	u64 cfg;
7251054a622SSunil Goutham 
7261054a622SSunil Goutham 	rvu_npa_freemem(rvu);
727756051e2SSunil Goutham 	rvu_npc_freemem(rvu);
72834b34ee0SGeetha sowjanya 	rvu_nix_freemem(rvu);
72934b34ee0SGeetha sowjanya 
7301054a622SSunil Goutham 	/* Free block LF bitmaps */
7317a37245eSSunil Goutham 	for (id = 0; id < BLK_COUNT; id++) {
73223923ea4SSunil Goutham 		block = &hw->block[id];
733aba53d5dSSunil Goutham 		kfree(block->lf.bmap);
7347a37245eSSunil Goutham 	}
735756051e2SSunil Goutham 
7361054a622SSunil Goutham 	/* Free MSIX bitmaps */
7371054a622SSunil Goutham 	for (id = 0; id < hw->total_pfs; id++) {
7381054a622SSunil Goutham 		pfvf = &rvu->pf[id];
7391054a622SSunil Goutham 		kfree(pfvf->msix.bmap);
740756051e2SSunil Goutham 	}
741756051e2SSunil Goutham 
742756051e2SSunil Goutham 	for (id = 0; id < hw->total_vfs; id++) {
743756051e2SSunil Goutham 		pfvf = &rvu->hwvf[id];
744756051e2SSunil Goutham 		kfree(pfvf->msix.bmap);
745756051e2SSunil Goutham 	}
746756051e2SSunil Goutham 
747756051e2SSunil Goutham 	/* Unmap MSIX vector base IOVA mapping */
748756051e2SSunil Goutham 	if (!rvu->msix_base_iova)
749756051e2SSunil Goutham 		return;
750756051e2SSunil Goutham 	cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_CONST);
75134b34ee0SGeetha sowjanya 	max_msix = cfg & 0xFFFFF;
75234b34ee0SGeetha sowjanya 	dma_unmap_resource(rvu->dev, rvu->msix_base_iova,
75334b34ee0SGeetha sowjanya 			   max_msix * PCI_MSIX_ENTRY_SIZE,
75434b34ee0SGeetha sowjanya 			   DMA_BIDIRECTIONAL, 0);
75534b34ee0SGeetha sowjanya 
75634b34ee0SGeetha sowjanya 	rvu_reset_msix(rvu);
75734b34ee0SGeetha sowjanya 	mutex_destroy(&rvu->rsrc_lock);
75834b34ee0SGeetha sowjanya 
75934b34ee0SGeetha sowjanya 	/* Free the QINT/CINT memory */
7600964fc8fSStanislaw Kardach 	pfvf = &rvu->pf[RVU_AFPF];
7614f4eebf2SLinu Cherian 	qmem_free(rvu->dev, pfvf->nix_qints_ctx);
7620964fc8fSStanislaw Kardach 	qmem_free(rvu->dev, pfvf->cq_ints_ctx);
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;
820b8b85d04SHariprasad Kelam 
821b8b85d04SHariprasad Kelam 	BUILD_BUG_ON(offsetof(struct rvu_fwdata, cgx_fw_data) > FWDATA_CGX_LMAC_OFFSET);
8224f4eebf2SLinu Cherian 	rvu->fwdata = ioremap_wc(fwdbase, sizeof(struct rvu_fwdata));
8234f4eebf2SLinu Cherian 	if (!rvu->fwdata)
8244f4eebf2SLinu Cherian 		goto fail;
8254f4eebf2SLinu Cherian 	if (!is_rvu_fwdata_valid(rvu)) {
8264f4eebf2SLinu Cherian 		dev_err(rvu->dev,
8274f4eebf2SLinu Cherian 			"Mismatch in 'fwdata' struct btw kernel and firmware\n");
8284f4eebf2SLinu Cherian 		iounmap(rvu->fwdata);
8294f4eebf2SLinu Cherian 		rvu->fwdata = NULL;
8304f4eebf2SLinu Cherian 		return -EINVAL;
8314f4eebf2SLinu Cherian 	}
8324f4eebf2SLinu Cherian 	return 0;
8334f4eebf2SLinu Cherian fail:
8344f4eebf2SLinu Cherian 	dev_info(rvu->dev, "Unable to fetch 'fwdata' from firmware\n");
8354f4eebf2SLinu Cherian 	return -EIO;
8364f4eebf2SLinu Cherian }
8374f4eebf2SLinu Cherian 
rvu_fwdata_exit(struct rvu * rvu)8384f4eebf2SLinu Cherian static void rvu_fwdata_exit(struct rvu *rvu)
8394f4eebf2SLinu Cherian {
8404f4eebf2SLinu Cherian 	if (rvu->fwdata)
8414f4eebf2SLinu Cherian 		iounmap(rvu->fwdata);
8424f4eebf2SLinu Cherian }
8434f4eebf2SLinu Cherian 
rvu_setup_nix_hw_resource(struct rvu * rvu,int blkaddr)8449932fb72SRakesh Babu static int rvu_setup_nix_hw_resource(struct rvu *rvu, int blkaddr)
8459932fb72SRakesh Babu {
8469932fb72SRakesh Babu 	struct rvu_hwinfo *hw = rvu->hw;
8479932fb72SRakesh Babu 	struct rvu_block *block;
8489932fb72SRakesh Babu 	int blkid;
8499932fb72SRakesh Babu 	u64 cfg;
8509932fb72SRakesh Babu 
8519932fb72SRakesh Babu 	/* Init NIX LF's bitmap */
8529932fb72SRakesh Babu 	block = &hw->block[blkaddr];
8539932fb72SRakesh Babu 	if (!block->implemented)
8549932fb72SRakesh Babu 		return 0;
8559932fb72SRakesh Babu 	blkid = (blkaddr == BLKADDR_NIX0) ? 0 : 1;
8569932fb72SRakesh Babu 	cfg = rvu_read64(rvu, blkaddr, NIX_AF_CONST2);
8579932fb72SRakesh Babu 	block->lf.max = cfg & 0xFFF;
8589932fb72SRakesh Babu 	block->addr = blkaddr;
8599932fb72SRakesh Babu 	block->type = BLKTYPE_NIX;
8609932fb72SRakesh Babu 	block->lfshift = 8;
8619932fb72SRakesh Babu 	block->lookup_reg = NIX_AF_RVU_LF_CFG_DEBUG;
8629932fb72SRakesh Babu 	block->pf_lfcnt_reg = RVU_PRIV_PFX_NIXX_CFG(blkid);
8639932fb72SRakesh Babu 	block->vf_lfcnt_reg = RVU_PRIV_HWVFX_NIXX_CFG(blkid);
8649932fb72SRakesh Babu 	block->lfcfg_reg = NIX_PRIV_LFX_CFG;
8659932fb72SRakesh Babu 	block->msixcfg_reg = NIX_PRIV_LFX_INT_CFG;
8669932fb72SRakesh Babu 	block->lfreset_reg = NIX_AF_LF_RST;
86748260907SSrujana Challa 	block->rvu = rvu;
8689932fb72SRakesh Babu 	sprintf(block->name, "NIX%d", blkid);
869221f3dffSRakesh Babu 	rvu->nix_blkaddr[blkid] = blkaddr;
8709932fb72SRakesh Babu 	return rvu_alloc_bitmap(&block->lf);
8719932fb72SRakesh Babu }
8729932fb72SRakesh Babu 
rvu_setup_cpt_hw_resource(struct rvu * rvu,int blkaddr)8739932fb72SRakesh Babu static int rvu_setup_cpt_hw_resource(struct rvu *rvu, int blkaddr)
8749932fb72SRakesh Babu {
8759932fb72SRakesh Babu 	struct rvu_hwinfo *hw = rvu->hw;
8769932fb72SRakesh Babu 	struct rvu_block *block;
8779932fb72SRakesh Babu 	int blkid;
8789932fb72SRakesh Babu 	u64 cfg;
8799932fb72SRakesh Babu 
8809932fb72SRakesh Babu 	/* Init CPT LF's bitmap */
8819932fb72SRakesh Babu 	block = &hw->block[blkaddr];
8829932fb72SRakesh Babu 	if (!block->implemented)
8839932fb72SRakesh Babu 		return 0;
8849932fb72SRakesh Babu 	blkid = (blkaddr == BLKADDR_CPT0) ? 0 : 1;
8859932fb72SRakesh Babu 	cfg = rvu_read64(rvu, blkaddr, CPT_AF_CONSTANTS0);
8869932fb72SRakesh Babu 	block->lf.max = cfg & 0xFF;
8879932fb72SRakesh Babu 	block->addr = blkaddr;
8889932fb72SRakesh Babu 	block->type = BLKTYPE_CPT;
8899932fb72SRakesh Babu 	block->multislot = true;
8909932fb72SRakesh Babu 	block->lfshift = 3;
8919932fb72SRakesh Babu 	block->lookup_reg = CPT_AF_RVU_LF_CFG_DEBUG;
8929932fb72SRakesh Babu 	block->pf_lfcnt_reg = RVU_PRIV_PFX_CPTX_CFG(blkid);
8939932fb72SRakesh Babu 	block->vf_lfcnt_reg = RVU_PRIV_HWVFX_CPTX_CFG(blkid);
8949932fb72SRakesh Babu 	block->lfcfg_reg = CPT_PRIV_LFX_CFG;
8959932fb72SRakesh Babu 	block->msixcfg_reg = CPT_PRIV_LFX_INT_CFG;
8969932fb72SRakesh Babu 	block->lfreset_reg = CPT_AF_LF_RST;
89748260907SSrujana Challa 	block->rvu = rvu;
8989932fb72SRakesh Babu 	sprintf(block->name, "CPT%d", blkid);
8999932fb72SRakesh Babu 	return rvu_alloc_bitmap(&block->lf);
9009932fb72SRakesh Babu }
9019932fb72SRakesh Babu 
rvu_get_lbk_bufsize(struct rvu * rvu)9026e54e1c5SHariprasad Kelam static void rvu_get_lbk_bufsize(struct rvu *rvu)
9036e54e1c5SHariprasad Kelam {
9046e54e1c5SHariprasad Kelam 	struct pci_dev *pdev = NULL;
9056e54e1c5SHariprasad Kelam 	void __iomem *base;
9066e54e1c5SHariprasad Kelam 	u64 lbk_const;
9076e54e1c5SHariprasad Kelam 
9086e54e1c5SHariprasad Kelam 	pdev = pci_get_device(PCI_VENDOR_ID_CAVIUM,
9096e54e1c5SHariprasad Kelam 			      PCI_DEVID_OCTEONTX2_LBK, pdev);
9106e54e1c5SHariprasad Kelam 	if (!pdev)
9116e54e1c5SHariprasad Kelam 		return;
9126e54e1c5SHariprasad Kelam 
9136e54e1c5SHariprasad Kelam 	base = pci_ioremap_bar(pdev, 0);
9146e54e1c5SHariprasad Kelam 	if (!base)
9156e54e1c5SHariprasad Kelam 		goto err_put;
9166e54e1c5SHariprasad Kelam 
9176e54e1c5SHariprasad Kelam 	lbk_const = readq(base + LBK_CONST);
9186e54e1c5SHariprasad Kelam 
9196e54e1c5SHariprasad Kelam 	/* cache fifo size */
9206e54e1c5SHariprasad Kelam 	rvu->hw->lbk_bufsize = FIELD_GET(LBK_CONST_BUF_SIZE, lbk_const);
9216e54e1c5SHariprasad Kelam 
9226e54e1c5SHariprasad Kelam 	iounmap(base);
9236e54e1c5SHariprasad Kelam err_put:
9246e54e1c5SHariprasad Kelam 	pci_dev_put(pdev);
9256e54e1c5SHariprasad Kelam }
9266e54e1c5SHariprasad Kelam 
rvu_setup_hw_resources(struct rvu * rvu)9271054a622SSunil Goutham static int rvu_setup_hw_resources(struct rvu *rvu)
9281054a622SSunil Goutham {
9291054a622SSunil Goutham 	struct rvu_hwinfo *hw = rvu->hw;
9301054a622SSunil Goutham 	struct rvu_block *block;
931114a767eSSunil Goutham 	int blkid, err;
9321054a622SSunil Goutham 	u64 cfg;
9331054a622SSunil Goutham 
9341054a622SSunil Goutham 	/* Get HW supported max RVU PF & VF count */
9351054a622SSunil Goutham 	cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_CONST);
9361054a622SSunil Goutham 	hw->total_pfs = (cfg >> 32) & 0xFF;
9371054a622SSunil Goutham 	hw->total_vfs = (cfg >> 20) & 0xFFF;
9381054a622SSunil Goutham 	hw->max_vfs_per_pf = (cfg >> 40) & 0xFF;
9391054a622SSunil Goutham 
940074ac38dSPavan Nikhilesh 	if (!is_rvu_otx2(rvu))
941074ac38dSPavan Nikhilesh 		rvu_apr_block_cn10k_init(rvu);
942074ac38dSPavan Nikhilesh 
9431054a622SSunil Goutham 	/* Init NPA LF's bitmap */
9441054a622SSunil Goutham 	block = &hw->block[BLKADDR_NPA];
9451054a622SSunil Goutham 	if (!block->implemented)
9461054a622SSunil Goutham 		goto nix;
9471054a622SSunil Goutham 	cfg = rvu_read64(rvu, BLKADDR_NPA, NPA_AF_CONST);
9481054a622SSunil Goutham 	block->lf.max = (cfg >> 16) & 0xFFF;
9491054a622SSunil Goutham 	block->addr = BLKADDR_NPA;
950114a767eSSunil Goutham 	block->type = BLKTYPE_NPA;
9511054a622SSunil Goutham 	block->lfshift = 8;
9521054a622SSunil Goutham 	block->lookup_reg = NPA_AF_RVU_LF_CFG_DEBUG;
9531054a622SSunil Goutham 	block->pf_lfcnt_reg = RVU_PRIV_PFX_NPA_CFG;
9541054a622SSunil Goutham 	block->vf_lfcnt_reg = RVU_PRIV_HWVFX_NPA_CFG;
9551054a622SSunil Goutham 	block->lfcfg_reg = NPA_PRIV_LFX_CFG;
9561054a622SSunil Goutham 	block->msixcfg_reg = NPA_PRIV_LFX_INT_CFG;
9571054a622SSunil Goutham 	block->lfreset_reg = NPA_AF_LF_RST;
95848260907SSrujana Challa 	block->rvu = rvu;
9591054a622SSunil Goutham 	sprintf(block->name, "NPA");
9601054a622SSunil Goutham 	err = rvu_alloc_bitmap(&block->lf);
961a83bdadaSSunil Goutham 	if (err) {
962a83bdadaSSunil Goutham 		dev_err(rvu->dev,
963a83bdadaSSunil Goutham 			"%s: Failed to allocate NPA LF bitmap\n", __func__);
9641054a622SSunil Goutham 		return err;
965a83bdadaSSunil Goutham 	}
9661054a622SSunil Goutham 
9671054a622SSunil Goutham nix:
9689932fb72SRakesh Babu 	err = rvu_setup_nix_hw_resource(rvu, BLKADDR_NIX0);
969a83bdadaSSunil Goutham 	if (err) {
970a83bdadaSSunil Goutham 		dev_err(rvu->dev,
971a83bdadaSSunil Goutham 			"%s: Failed to allocate NIX0 LFs bitmap\n", __func__);
9729932fb72SRakesh Babu 		return err;
973a83bdadaSSunil Goutham 	}
974a83bdadaSSunil Goutham 
9759932fb72SRakesh Babu 	err = rvu_setup_nix_hw_resource(rvu, BLKADDR_NIX1);
976a83bdadaSSunil Goutham 	if (err) {
977a83bdadaSSunil Goutham 		dev_err(rvu->dev,
978a83bdadaSSunil Goutham 			"%s: Failed to allocate NIX1 LFs bitmap\n", __func__);
9791054a622SSunil Goutham 		return err;
980a83bdadaSSunil Goutham 	}
9811054a622SSunil Goutham 
9821054a622SSunil Goutham 	/* Init SSO group's bitmap */
9831054a622SSunil Goutham 	block = &hw->block[BLKADDR_SSO];
9841054a622SSunil Goutham 	if (!block->implemented)
9851054a622SSunil Goutham 		goto ssow;
9861054a622SSunil Goutham 	cfg = rvu_read64(rvu, BLKADDR_SSO, SSO_AF_CONST);
9871054a622SSunil Goutham 	block->lf.max = cfg & 0xFFFF;
9881054a622SSunil Goutham 	block->addr = BLKADDR_SSO;
989114a767eSSunil Goutham 	block->type = BLKTYPE_SSO;
9901054a622SSunil Goutham 	block->multislot = true;
9911054a622SSunil Goutham 	block->lfshift = 3;
9921054a622SSunil Goutham 	block->lookup_reg = SSO_AF_RVU_LF_CFG_DEBUG;
9931054a622SSunil Goutham 	block->pf_lfcnt_reg = RVU_PRIV_PFX_SSO_CFG;
9941054a622SSunil Goutham 	block->vf_lfcnt_reg = RVU_PRIV_HWVFX_SSO_CFG;
9951054a622SSunil Goutham 	block->lfcfg_reg = SSO_PRIV_LFX_HWGRP_CFG;
9961054a622SSunil Goutham 	block->msixcfg_reg = SSO_PRIV_LFX_HWGRP_INT_CFG;
9971054a622SSunil Goutham 	block->lfreset_reg = SSO_AF_LF_HWGRP_RST;
99848260907SSrujana Challa 	block->rvu = rvu;
9991054a622SSunil Goutham 	sprintf(block->name, "SSO GROUP");
10001054a622SSunil Goutham 	err = rvu_alloc_bitmap(&block->lf);
1001a83bdadaSSunil Goutham 	if (err) {
1002a83bdadaSSunil Goutham 		dev_err(rvu->dev,
1003a83bdadaSSunil Goutham 			"%s: Failed to allocate SSO LF bitmap\n", __func__);
10041054a622SSunil Goutham 		return err;
1005a83bdadaSSunil Goutham 	}
10061054a622SSunil Goutham 
10071054a622SSunil Goutham ssow:
10081054a622SSunil Goutham 	/* Init SSO workslot's bitmap */
10091054a622SSunil Goutham 	block = &hw->block[BLKADDR_SSOW];
10101054a622SSunil Goutham 	if (!block->implemented)
10111054a622SSunil Goutham 		goto tim;
10121054a622SSunil Goutham 	block->lf.max = (cfg >> 56) & 0xFF;
10131054a622SSunil Goutham 	block->addr = BLKADDR_SSOW;
1014114a767eSSunil Goutham 	block->type = BLKTYPE_SSOW;
10151054a622SSunil Goutham 	block->multislot = true;
10161054a622SSunil Goutham 	block->lfshift = 3;
10171054a622SSunil Goutham 	block->lookup_reg = SSOW_AF_RVU_LF_HWS_CFG_DEBUG;
10181054a622SSunil Goutham 	block->pf_lfcnt_reg = RVU_PRIV_PFX_SSOW_CFG;
10191054a622SSunil Goutham 	block->vf_lfcnt_reg = RVU_PRIV_HWVFX_SSOW_CFG;
10201054a622SSunil Goutham 	block->lfcfg_reg = SSOW_PRIV_LFX_HWS_CFG;
10211054a622SSunil Goutham 	block->msixcfg_reg = SSOW_PRIV_LFX_HWS_INT_CFG;
10221054a622SSunil Goutham 	block->lfreset_reg = SSOW_AF_LF_HWS_RST;
102348260907SSrujana Challa 	block->rvu = rvu;
10241054a622SSunil Goutham 	sprintf(block->name, "SSOWS");
10251054a622SSunil Goutham 	err = rvu_alloc_bitmap(&block->lf);
1026a83bdadaSSunil Goutham 	if (err) {
1027a83bdadaSSunil Goutham 		dev_err(rvu->dev,
1028a83bdadaSSunil Goutham 			"%s: Failed to allocate SSOW LF bitmap\n", __func__);
10291054a622SSunil Goutham 		return err;
1030a83bdadaSSunil Goutham 	}
10311054a622SSunil Goutham 
10321054a622SSunil Goutham tim:
10331054a622SSunil Goutham 	/* Init TIM LF's bitmap */
10341054a622SSunil Goutham 	block = &hw->block[BLKADDR_TIM];
10351054a622SSunil Goutham 	if (!block->implemented)
10361054a622SSunil Goutham 		goto cpt;
10371054a622SSunil Goutham 	cfg = rvu_read64(rvu, BLKADDR_TIM, TIM_AF_CONST);
10381054a622SSunil Goutham 	block->lf.max = cfg & 0xFFFF;
10391054a622SSunil Goutham 	block->addr = BLKADDR_TIM;
1040114a767eSSunil Goutham 	block->type = BLKTYPE_TIM;
10411054a622SSunil Goutham 	block->multislot = true;
10421054a622SSunil Goutham 	block->lfshift = 3;
10431054a622SSunil Goutham 	block->lookup_reg = TIM_AF_RVU_LF_CFG_DEBUG;
10441054a622SSunil Goutham 	block->pf_lfcnt_reg = RVU_PRIV_PFX_TIM_CFG;
10451054a622SSunil Goutham 	block->vf_lfcnt_reg = RVU_PRIV_HWVFX_TIM_CFG;
10461054a622SSunil Goutham 	block->lfcfg_reg = TIM_PRIV_LFX_CFG;
10471054a622SSunil Goutham 	block->msixcfg_reg = TIM_PRIV_LFX_INT_CFG;
10481054a622SSunil Goutham 	block->lfreset_reg = TIM_AF_LF_RST;
104948260907SSrujana Challa 	block->rvu = rvu;
10501054a622SSunil Goutham 	sprintf(block->name, "TIM");
10511054a622SSunil Goutham 	err = rvu_alloc_bitmap(&block->lf);
1052a83bdadaSSunil Goutham 	if (err) {
1053a83bdadaSSunil Goutham 		dev_err(rvu->dev,
1054a83bdadaSSunil Goutham 			"%s: Failed to allocate TIM LF bitmap\n", __func__);
10551054a622SSunil Goutham 		return err;
1056a83bdadaSSunil Goutham 	}
10571054a622SSunil Goutham 
10581054a622SSunil Goutham cpt:
10599932fb72SRakesh Babu 	err = rvu_setup_cpt_hw_resource(rvu, BLKADDR_CPT0);
1060a83bdadaSSunil Goutham 	if (err) {
1061a83bdadaSSunil Goutham 		dev_err(rvu->dev,
1062a83bdadaSSunil Goutham 			"%s: Failed to allocate CPT0 LF bitmap\n", __func__);
10639932fb72SRakesh Babu 		return err;
1064a83bdadaSSunil Goutham 	}
10659932fb72SRakesh Babu 	err = rvu_setup_cpt_hw_resource(rvu, BLKADDR_CPT1);
1066a83bdadaSSunil Goutham 	if (err) {
1067a83bdadaSSunil Goutham 		dev_err(rvu->dev,
1068a83bdadaSSunil Goutham 			"%s: Failed to allocate CPT1 LF bitmap\n", __func__);
1069a83bdadaSSunil Goutham 		return err;
1070a83bdadaSSunil Goutham 	}
10711054a622SSunil Goutham 
1072114a767eSSunil Goutham 	/* Allocate memory for PFVF data */
1073114a767eSSunil Goutham 	rvu->pf = devm_kcalloc(rvu->dev, hw->total_pfs,
1074114a767eSSunil Goutham 			       sizeof(struct rvu_pfvf), GFP_KERNEL);
1075a83bdadaSSunil Goutham 	if (!rvu->pf) {
1076a83bdadaSSunil Goutham 		dev_err(rvu->dev,
1077a83bdadaSSunil Goutham 			"%s: Failed to allocate memory for PF's rvu_pfvf struct\n", __func__);
1078114a767eSSunil Goutham 		return -ENOMEM;
1079a83bdadaSSunil Goutham 	}
1080114a767eSSunil Goutham 
1081114a767eSSunil Goutham 	rvu->hwvf = devm_kcalloc(rvu->dev, hw->total_vfs,
1082114a767eSSunil Goutham 				 sizeof(struct rvu_pfvf), GFP_KERNEL);
1083a83bdadaSSunil Goutham 	if (!rvu->hwvf) {
1084a83bdadaSSunil Goutham 		dev_err(rvu->dev,
1085a83bdadaSSunil Goutham 			"%s: Failed to allocate memory for VF's rvu_pfvf struct\n", __func__);
1086114a767eSSunil Goutham 		return -ENOMEM;
1087a83bdadaSSunil Goutham 	}
1088114a767eSSunil Goutham 
10890964fc8fSStanislaw Kardach 	mutex_init(&rvu->rsrc_lock);
1090756051e2SSunil Goutham 
10914f4eebf2SLinu Cherian 	rvu_fwdata_init(rvu);
10924f4eebf2SLinu Cherian 
1093756051e2SSunil Goutham 	err = rvu_setup_msix_resources(rvu);
1094a83bdadaSSunil Goutham 	if (err) {
1095a83bdadaSSunil Goutham 		dev_err(rvu->dev,
1096a83bdadaSSunil Goutham 			"%s: Failed to setup MSIX resources\n", __func__);
1097756051e2SSunil Goutham 		return err;
1098a83bdadaSSunil Goutham 	}
1099756051e2SSunil Goutham 
1100114a767eSSunil Goutham 	for (blkid = 0; blkid < BLK_COUNT; blkid++) {
1101114a767eSSunil Goutham 		block = &hw->block[blkid];
1102114a767eSSunil Goutham 		if (!block->lf.bmap)
1103114a767eSSunil Goutham 			continue;
1104114a767eSSunil Goutham 
1105114a767eSSunil Goutham 		/* Allocate memory for block LF/slot to pcifunc mapping info */
1106114a767eSSunil Goutham 		block->fn_map = devm_kcalloc(rvu->dev, block->lf.max,
1107114a767eSSunil Goutham 					     sizeof(u16), GFP_KERNEL);
11084f4eebf2SLinu Cherian 		if (!block->fn_map) {
11094f4eebf2SLinu Cherian 			err = -ENOMEM;
11104f4eebf2SLinu Cherian 			goto msix_err;
11114f4eebf2SLinu Cherian 		}
1112114a767eSSunil Goutham 
1113114a767eSSunil Goutham 		/* Scan all blocks to check if low level firmware has
1114114a767eSSunil Goutham 		 * already provisioned any of the resources to a PF/VF.
1115114a767eSSunil Goutham 		 */
1116114a767eSSunil Goutham 		rvu_scan_block(rvu, block);
1117114a767eSSunil Goutham 	}
1118114a767eSSunil Goutham 
1119242da439SSubbaraya Sundeep 	err = rvu_set_channels_base(rvu);
1120242da439SSubbaraya Sundeep 	if (err)
1121242da439SSubbaraya Sundeep 		goto msix_err;
1122242da439SSubbaraya Sundeep 
112323923ea4SSunil Goutham 	err = rvu_npc_init(rvu);
1124a83bdadaSSunil Goutham 	if (err) {
1125a83bdadaSSunil Goutham 		dev_err(rvu->dev, "%s: Failed to initialize npc\n", __func__);
11264f4eebf2SLinu Cherian 		goto npc_err;
1127a83bdadaSSunil Goutham 	}
112844990aaaSLinu Cherian 
112944990aaaSLinu Cherian 	err = rvu_cgx_init(rvu);
1130a83bdadaSSunil Goutham 	if (err) {
1131a83bdadaSSunil Goutham 		dev_err(rvu->dev, "%s: Failed to initialize cgx\n", __func__);
11324f4eebf2SLinu Cherian 		goto cgx_err;
1133a83bdadaSSunil Goutham 	}
11344f4eebf2SLinu Cherian 
11353571fe07SRatheesh Kannoth 	err = rvu_npc_exact_init(rvu);
11363571fe07SRatheesh Kannoth 	if (err) {
11373571fe07SRatheesh Kannoth 		dev_err(rvu->dev, "failed to initialize exact match table\n");
11383571fe07SRatheesh Kannoth 		return err;
11393571fe07SRatheesh Kannoth 	}
11403571fe07SRatheesh Kannoth 
11414f4eebf2SLinu Cherian 	/* Assign MACs for CGX mapped functions */
11424f4eebf2SLinu Cherian 	rvu_setup_pfvf_macaddress(rvu);
114323923ea4SSunil Goutham 
11447a37245eSSunil Goutham 	err = rvu_npa_init(rvu);
1145a83bdadaSSunil Goutham 	if (err) {
1146a83bdadaSSunil Goutham 		dev_err(rvu->dev, "%s: Failed to initialize npa\n", __func__);
11474f4eebf2SLinu Cherian 		goto npa_err;
1148a83bdadaSSunil Goutham 	}
11497a37245eSSunil Goutham 
11506e54e1c5SHariprasad Kelam 	rvu_get_lbk_bufsize(rvu);
11516e54e1c5SHariprasad Kelam 
1152aba53d5dSSunil Goutham 	err = rvu_nix_init(rvu);
1153a83bdadaSSunil Goutham 	if (err) {
1154a83bdadaSSunil Goutham 		dev_err(rvu->dev, "%s: Failed to initialize nix\n", __func__);
11554f4eebf2SLinu Cherian 		goto nix_err;
1156a83bdadaSSunil Goutham 	}
1157aba53d5dSSunil Goutham 
1158fe1939bbSRadha Mohan Chintakuntla 	err = rvu_sdp_init(rvu);
1159fe1939bbSRadha Mohan Chintakuntla 	if (err) {
1160fe1939bbSRadha Mohan Chintakuntla 		dev_err(rvu->dev, "%s: Failed to initialize sdp\n", __func__);
1161fe1939bbSRadha Mohan Chintakuntla 		goto nix_err;
1162fe1939bbSRadha Mohan Chintakuntla 	}
1163fe1939bbSRadha Mohan Chintakuntla 
1164242da439SSubbaraya Sundeep 	rvu_program_channels(rvu);
1165762ca6eeSHariprasad Kelam 	cgx_start_linkup(rvu);
1166242da439SSubbaraya Sundeep 
1167ca7f49ffSGeetha sowjanya 	err = rvu_mcs_init(rvu);
1168ca7f49ffSGeetha sowjanya 	if (err) {
1169ca7f49ffSGeetha sowjanya 		dev_err(rvu->dev, "%s: Failed to initialize mcs\n", __func__);
1170ca7f49ffSGeetha sowjanya 		goto nix_err;
1171ca7f49ffSGeetha sowjanya 	}
1172ca7f49ffSGeetha sowjanya 
117341b166e5SSrujana Challa 	err = rvu_cpt_init(rvu);
117441b166e5SSrujana Challa 	if (err) {
117541b166e5SSrujana Challa 		dev_err(rvu->dev, "%s: Failed to initialize cpt\n", __func__);
117641b166e5SSrujana Challa 		goto mcs_err;
117741b166e5SSrujana Challa 	}
117841b166e5SSrujana Challa 
11797a37245eSSunil Goutham 	return 0;
118044990aaaSLinu Cherian 
118141b166e5SSrujana Challa mcs_err:
118241b166e5SSrujana Challa 	rvu_mcs_exit(rvu);
11834f4eebf2SLinu Cherian nix_err:
11844f4eebf2SLinu Cherian 	rvu_nix_freemem(rvu);
11854f4eebf2SLinu Cherian npa_err:
11864f4eebf2SLinu Cherian 	rvu_npa_freemem(rvu);
118744990aaaSLinu Cherian cgx_err:
118844990aaaSLinu Cherian 	rvu_cgx_exit(rvu);
11894f4eebf2SLinu Cherian npc_err:
11904f4eebf2SLinu Cherian 	rvu_npc_freemem(rvu);
11914f4eebf2SLinu Cherian 	rvu_fwdata_exit(rvu);
11924f4eebf2SLinu Cherian msix_err:
11934f4eebf2SLinu Cherian 	rvu_reset_msix(rvu);
119444990aaaSLinu Cherian 	return err;
11957a37245eSSunil Goutham }
11967a37245eSSunil Goutham 
11977a37245eSSunil Goutham /* NPA and NIX admin queue APIs */
rvu_aq_free(struct rvu * rvu,struct admin_queue * aq)11987a37245eSSunil Goutham void rvu_aq_free(struct rvu *rvu, struct admin_queue *aq)
11997a37245eSSunil Goutham {
12007a37245eSSunil Goutham 	if (!aq)
12017a37245eSSunil Goutham 		return;
12027a37245eSSunil Goutham 
12037a37245eSSunil Goutham 	qmem_free(rvu->dev, aq->inst);
12047a37245eSSunil Goutham 	qmem_free(rvu->dev, aq->res);
12057a37245eSSunil Goutham 	devm_kfree(rvu->dev, aq);
12067a37245eSSunil Goutham }
12077a37245eSSunil Goutham 
rvu_aq_alloc(struct rvu * rvu,struct admin_queue ** ad_queue,int qsize,int inst_size,int res_size)12087a37245eSSunil Goutham int rvu_aq_alloc(struct rvu *rvu, struct admin_queue **ad_queue,
12097a37245eSSunil Goutham 		 int qsize, int inst_size, int res_size)
12107a37245eSSunil Goutham {
12117a37245eSSunil Goutham 	struct admin_queue *aq;
12127a37245eSSunil Goutham 	int err;
12137a37245eSSunil Goutham 
12147a37245eSSunil Goutham 	*ad_queue = devm_kzalloc(rvu->dev, sizeof(*aq), GFP_KERNEL);
12157a37245eSSunil Goutham 	if (!*ad_queue)
12167a37245eSSunil Goutham 		return -ENOMEM;
12177a37245eSSunil Goutham 	aq = *ad_queue;
12187a37245eSSunil Goutham 
12197a37245eSSunil Goutham 	/* Alloc memory for instructions i.e AQ */
12207a37245eSSunil Goutham 	err = qmem_alloc(rvu->dev, &aq->inst, qsize, inst_size);
12217a37245eSSunil Goutham 	if (err) {
12227a37245eSSunil Goutham 		devm_kfree(rvu->dev, aq);
12237a37245eSSunil Goutham 		return err;
12247a37245eSSunil Goutham 	}
12257a37245eSSunil Goutham 
12267a37245eSSunil Goutham 	/* Alloc memory for results */
12277a37245eSSunil Goutham 	err = qmem_alloc(rvu->dev, &aq->res, qsize, res_size);
12287a37245eSSunil Goutham 	if (err) {
12297a37245eSSunil Goutham 		rvu_aq_free(rvu, aq);
12307a37245eSSunil Goutham 		return err;
12317a37245eSSunil Goutham 	}
12327a37245eSSunil Goutham 
12337a37245eSSunil Goutham 	spin_lock_init(&aq->lock);
12341054a622SSunil Goutham 	return 0;
12351054a622SSunil Goutham }
12361054a622SSunil Goutham 
rvu_mbox_handler_ready(struct rvu * rvu,struct msg_req * req,struct ready_msg_rsp * rsp)1237c6614738SSubbaraya Sundeep int rvu_mbox_handler_ready(struct rvu *rvu, struct msg_req *req,
12381f154625SAleksey Makarov 			   struct ready_msg_rsp *rsp)
12391f154625SAleksey Makarov {
12404f4eebf2SLinu Cherian 	if (rvu->fwdata) {
12414f4eebf2SLinu Cherian 		rsp->rclk_freq = rvu->fwdata->rclk;
12424f4eebf2SLinu Cherian 		rsp->sclk_freq = rvu->fwdata->sclk;
12434f4eebf2SLinu Cherian 	}
12441f154625SAleksey Makarov 	return 0;
12451f154625SAleksey Makarov }
12461f154625SAleksey Makarov 
1247746ea742SSunil Goutham /* Get current count of a RVU block's LF/slots
1248746ea742SSunil Goutham  * provisioned to a given RVU func.
1249746ea742SSunil Goutham  */
rvu_get_rsrc_mapcount(struct rvu_pfvf * pfvf,int blkaddr)1250cdd41e87SSubbaraya Sundeep u16 rvu_get_rsrc_mapcount(struct rvu_pfvf *pfvf, int blkaddr)
1251cdd41e87SSubbaraya Sundeep {
1252cdd41e87SSubbaraya Sundeep 	switch (blkaddr) {
1253cdd41e87SSubbaraya Sundeep 	case BLKADDR_NPA:
1254cdd41e87SSubbaraya Sundeep 		return pfvf->npalf ? 1 : 0;
1255cdd41e87SSubbaraya Sundeep 	case BLKADDR_NIX0:
1256cdd41e87SSubbaraya Sundeep 	case BLKADDR_NIX1:
1257cdd41e87SSubbaraya Sundeep 		return pfvf->nixlf ? 1 : 0;
1258cdd41e87SSubbaraya Sundeep 	case BLKADDR_SSO:
1259cdd41e87SSubbaraya Sundeep 		return pfvf->sso;
1260cdd41e87SSubbaraya Sundeep 	case BLKADDR_SSOW:
1261cdd41e87SSubbaraya Sundeep 		return pfvf->ssow;
1262cdd41e87SSubbaraya Sundeep 	case BLKADDR_TIM:
1263cdd41e87SSubbaraya Sundeep 		return pfvf->timlfs;
1264cdd41e87SSubbaraya Sundeep 	case BLKADDR_CPT0:
1265cdd41e87SSubbaraya Sundeep 		return pfvf->cptlfs;
1266cdd41e87SSubbaraya Sundeep 	case BLKADDR_CPT1:
1267cdd41e87SSubbaraya Sundeep 		return pfvf->cpt1_lfs;
1268cdd41e87SSubbaraya Sundeep 	}
1269cdd41e87SSubbaraya Sundeep 	return 0;
1270cdd41e87SSubbaraya Sundeep }
1271cdd41e87SSubbaraya Sundeep 
1272cdd41e87SSubbaraya Sundeep /* Return true if LFs of block type are attached to pcifunc */
is_blktype_attached(struct rvu_pfvf * pfvf,int blktype)1273cdd41e87SSubbaraya Sundeep static bool is_blktype_attached(struct rvu_pfvf *pfvf, int blktype)
1274746ea742SSunil Goutham {
1275746ea742SSunil Goutham 	switch (blktype) {
1276746ea742SSunil Goutham 	case BLKTYPE_NPA:
1277746ea742SSunil Goutham 		return pfvf->npalf ? 1 : 0;
1278746ea742SSunil Goutham 	case BLKTYPE_NIX:
1279746ea742SSunil Goutham 		return pfvf->nixlf ? 1 : 0;
1280746ea742SSunil Goutham 	case BLKTYPE_SSO:
1281cdd41e87SSubbaraya Sundeep 		return !!pfvf->sso;
1282746ea742SSunil Goutham 	case BLKTYPE_SSOW:
1283cdd41e87SSubbaraya Sundeep 		return !!pfvf->ssow;
1284746ea742SSunil Goutham 	case BLKTYPE_TIM:
1285cdd41e87SSubbaraya Sundeep 		return !!pfvf->timlfs;
1286746ea742SSunil Goutham 	case BLKTYPE_CPT:
1287cdd41e87SSubbaraya Sundeep 		return pfvf->cptlfs || pfvf->cpt1_lfs;
1288746ea742SSunil Goutham 	}
1289cdd41e87SSubbaraya Sundeep 
1290cdd41e87SSubbaraya Sundeep 	return false;
1291746ea742SSunil Goutham }
1292746ea742SSunil Goutham 
is_pffunc_map_valid(struct rvu * rvu,u16 pcifunc,int blktype)1293f325d3f4SSunil Goutham bool is_pffunc_map_valid(struct rvu *rvu, u16 pcifunc, int blktype)
1294f325d3f4SSunil Goutham {
1295f325d3f4SSunil Goutham 	struct rvu_pfvf *pfvf;
1296f325d3f4SSunil Goutham 
1297f325d3f4SSunil Goutham 	if (!is_pf_func_valid(rvu, pcifunc))
1298f325d3f4SSunil Goutham 		return false;
1299f325d3f4SSunil Goutham 
1300f325d3f4SSunil Goutham 	pfvf = rvu_get_pfvf(rvu, pcifunc);
1301f325d3f4SSunil Goutham 
1302f325d3f4SSunil Goutham 	/* Check if this PFFUNC has a LF of type blktype attached */
1303cdd41e87SSubbaraya Sundeep 	if (!is_blktype_attached(pfvf, blktype))
1304f325d3f4SSunil Goutham 		return false;
1305f325d3f4SSunil Goutham 
1306f325d3f4SSunil Goutham 	return true;
1307f325d3f4SSunil Goutham }
1308f325d3f4SSunil Goutham 
rvu_lookup_rsrc(struct rvu * rvu,struct rvu_block * block,int pcifunc,int slot)1309746ea742SSunil Goutham static int rvu_lookup_rsrc(struct rvu *rvu, struct rvu_block *block,
1310746ea742SSunil Goutham 			   int pcifunc, int slot)
1311746ea742SSunil Goutham {
1312746ea742SSunil Goutham 	u64 val;
1313746ea742SSunil Goutham 
1314746ea742SSunil Goutham 	val = ((u64)pcifunc << 24) | (slot << 16) | (1ULL << 13);
1315746ea742SSunil Goutham 	rvu_write64(rvu, block->addr, block->lookup_reg, val);
1316746ea742SSunil Goutham 	/* Wait for the lookup to finish */
1317746ea742SSunil Goutham 	/* TODO: put some timeout here */
1318746ea742SSunil Goutham 	while (rvu_read64(rvu, block->addr, block->lookup_reg) & (1ULL << 13))
1319746ea742SSunil Goutham 		;
1320746ea742SSunil Goutham 
1321746ea742SSunil Goutham 	val = rvu_read64(rvu, block->addr, block->lookup_reg);
1322746ea742SSunil Goutham 
1323746ea742SSunil Goutham 	/* Check LF valid bit */
1324746ea742SSunil Goutham 	if (!(val & (1ULL << 12)))
1325746ea742SSunil Goutham 		return -1;
1326746ea742SSunil Goutham 
1327746ea742SSunil Goutham 	return (val & 0xFFF);
1328746ea742SSunil Goutham }
1329746ea742SSunil Goutham 
rvu_get_blkaddr_from_slot(struct rvu * rvu,int blktype,u16 pcifunc,u16 global_slot,u16 * slot_in_block)13304b5a3ab1SSrujana Challa int rvu_get_blkaddr_from_slot(struct rvu *rvu, int blktype, u16 pcifunc,
13314b5a3ab1SSrujana Challa 			      u16 global_slot, u16 *slot_in_block)
13324b5a3ab1SSrujana Challa {
13334b5a3ab1SSrujana Challa 	struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
13344b5a3ab1SSrujana Challa 	int numlfs, total_lfs = 0, nr_blocks = 0;
13354b5a3ab1SSrujana Challa 	int i, num_blkaddr[BLK_COUNT] = { 0 };
13364b5a3ab1SSrujana Challa 	struct rvu_block *block;
13373503e673SColin Ian King 	int blkaddr;
13384b5a3ab1SSrujana Challa 	u16 start_slot;
13394b5a3ab1SSrujana Challa 
13404b5a3ab1SSrujana Challa 	if (!is_blktype_attached(pfvf, blktype))
13414b5a3ab1SSrujana Challa 		return -ENODEV;
13424b5a3ab1SSrujana Challa 
13434b5a3ab1SSrujana Challa 	/* Get all the block addresses from which LFs are attached to
13444b5a3ab1SSrujana Challa 	 * the given pcifunc in num_blkaddr[].
13454b5a3ab1SSrujana Challa 	 */
13464b5a3ab1SSrujana Challa 	for (blkaddr = BLKADDR_RVUM; blkaddr < BLK_COUNT; blkaddr++) {
13474b5a3ab1SSrujana Challa 		block = &rvu->hw->block[blkaddr];
13484b5a3ab1SSrujana Challa 		if (block->type != blktype)
13494b5a3ab1SSrujana Challa 			continue;
13504b5a3ab1SSrujana Challa 		if (!is_block_implemented(rvu->hw, blkaddr))
13514b5a3ab1SSrujana Challa 			continue;
13524b5a3ab1SSrujana Challa 
13534b5a3ab1SSrujana Challa 		numlfs = rvu_get_rsrc_mapcount(pfvf, blkaddr);
13544b5a3ab1SSrujana Challa 		if (numlfs) {
13554b5a3ab1SSrujana Challa 			total_lfs += numlfs;
13564b5a3ab1SSrujana Challa 			num_blkaddr[nr_blocks] = blkaddr;
13574b5a3ab1SSrujana Challa 			nr_blocks++;
13584b5a3ab1SSrujana Challa 		}
13594b5a3ab1SSrujana Challa 	}
13604b5a3ab1SSrujana Challa 
13614b5a3ab1SSrujana Challa 	if (global_slot >= total_lfs)
13624b5a3ab1SSrujana Challa 		return -ENODEV;
13634b5a3ab1SSrujana Challa 
13644b5a3ab1SSrujana Challa 	/* Based on the given global slot number retrieve the
13654b5a3ab1SSrujana Challa 	 * correct block address out of all attached block
13664b5a3ab1SSrujana Challa 	 * addresses and slot number in that block.
13674b5a3ab1SSrujana Challa 	 */
13684b5a3ab1SSrujana Challa 	total_lfs = 0;
13694b5a3ab1SSrujana Challa 	blkaddr = -ENODEV;
13704b5a3ab1SSrujana Challa 	for (i = 0; i < nr_blocks; i++) {
13714b5a3ab1SSrujana Challa 		numlfs = rvu_get_rsrc_mapcount(pfvf, num_blkaddr[i]);
13724b5a3ab1SSrujana Challa 		total_lfs += numlfs;
13734b5a3ab1SSrujana Challa 		if (global_slot < total_lfs) {
13744b5a3ab1SSrujana Challa 			blkaddr = num_blkaddr[i];
13754b5a3ab1SSrujana Challa 			start_slot = total_lfs - numlfs;
13764b5a3ab1SSrujana Challa 			*slot_in_block = global_slot - start_slot;
13774b5a3ab1SSrujana Challa 			break;
13784b5a3ab1SSrujana Challa 		}
13794b5a3ab1SSrujana Challa 	}
13804b5a3ab1SSrujana Challa 
13814b5a3ab1SSrujana Challa 	return blkaddr;
13824b5a3ab1SSrujana Challa }
13834b5a3ab1SSrujana Challa 
rvu_detach_block(struct rvu * rvu,int pcifunc,int blktype)1384746ea742SSunil Goutham static void rvu_detach_block(struct rvu *rvu, int pcifunc, int blktype)
1385746ea742SSunil Goutham {
1386746ea742SSunil Goutham 	struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
1387746ea742SSunil Goutham 	struct rvu_hwinfo *hw = rvu->hw;
1388746ea742SSunil Goutham 	struct rvu_block *block;
1389746ea742SSunil Goutham 	int slot, lf, num_lfs;
1390746ea742SSunil Goutham 	int blkaddr;
1391746ea742SSunil Goutham 
1392746ea742SSunil Goutham 	blkaddr = rvu_get_blkaddr(rvu, blktype, pcifunc);
1393746ea742SSunil Goutham 	if (blkaddr < 0)
1394746ea742SSunil Goutham 		return;
1395746ea742SSunil Goutham 
13964f88ed2cSHariprasad Kelam 
1397746ea742SSunil Goutham 	block = &hw->block[blkaddr];
1398746ea742SSunil Goutham 
1399cdd41e87SSubbaraya Sundeep 	num_lfs = rvu_get_rsrc_mapcount(pfvf, block->addr);
1400746ea742SSunil Goutham 	if (!num_lfs)
1401746ea742SSunil Goutham 		return;
1402746ea742SSunil Goutham 
1403746ea742SSunil Goutham 	for (slot = 0; slot < num_lfs; slot++) {
1404746ea742SSunil Goutham 		lf = rvu_lookup_rsrc(rvu, block, pcifunc, slot);
1405746ea742SSunil Goutham 		if (lf < 0) /* This should never happen */
1406746ea742SSunil Goutham 			continue;
1407746ea742SSunil Goutham 
1408bb91f754SHariprasad Kelam 		if (blktype == BLKTYPE_NIX) {
1409bb91f754SHariprasad Kelam 			rvu_nix_reset_mac(pfvf, pcifunc);
1410bb91f754SHariprasad Kelam 			rvu_npc_clear_ucast_entry(rvu, pcifunc, lf);
1411bb91f754SHariprasad Kelam 		}
1412746ea742SSunil Goutham 		/* Disable the LF */
1413746ea742SSunil Goutham 		rvu_write64(rvu, blkaddr, block->lfcfg_reg |
1414746ea742SSunil Goutham 			    (lf << block->lfshift), 0x00ULL);
1415746ea742SSunil Goutham 
1416746ea742SSunil Goutham 		/* Update SW maintained mapping info as well */
1417746ea742SSunil Goutham 		rvu_update_rsrc_map(rvu, pfvf, block,
1418746ea742SSunil Goutham 				    pcifunc, lf, false);
1419746ea742SSunil Goutham 
1420746ea742SSunil Goutham 		/* Free the resource */
1421746ea742SSunil Goutham 		rvu_free_rsrc(&block->lf, lf);
1422756051e2SSunil Goutham 
1423756051e2SSunil Goutham 		/* Clear MSIX vector offset for this LF */
1424756051e2SSunil Goutham 		rvu_clear_msix_offset(rvu, pfvf, block, lf);
1425746ea742SSunil Goutham 	}
1426746ea742SSunil Goutham }
1427746ea742SSunil Goutham 
rvu_detach_rsrcs(struct rvu * rvu,struct rsrc_detach * detach,u16 pcifunc)1428746ea742SSunil Goutham static int rvu_detach_rsrcs(struct rvu *rvu, struct rsrc_detach *detach,
1429746ea742SSunil Goutham 			    u16 pcifunc)
1430746ea742SSunil Goutham {
1431746ea742SSunil Goutham 	struct rvu_hwinfo *hw = rvu->hw;
1432eec6f752SYueHaibing 	bool detach_all = true;
1433746ea742SSunil Goutham 	struct rvu_block *block;
1434eec6f752SYueHaibing 	int blkid;
1435746ea742SSunil Goutham 
14360964fc8fSStanislaw Kardach 	mutex_lock(&rvu->rsrc_lock);
1437746ea742SSunil Goutham 
1438746ea742SSunil Goutham 	/* Check for partial resource detach */
1439746ea742SSunil Goutham 	if (detach && detach->partial)
1440746ea742SSunil Goutham 		detach_all = false;
1441746ea742SSunil Goutham 
1442746ea742SSunil Goutham 	/* Check for RVU block's LFs attached to this func,
1443746ea742SSunil Goutham 	 * if so, detach them.
1444746ea742SSunil Goutham 	 */
1445746ea742SSunil Goutham 	for (blkid = 0; blkid < BLK_COUNT; blkid++) {
1446746ea742SSunil Goutham 		block = &hw->block[blkid];
1447746ea742SSunil Goutham 		if (!block->lf.bmap)
1448746ea742SSunil Goutham 			continue;
1449746ea742SSunil Goutham 		if (!detach_all && detach) {
1450746ea742SSunil Goutham 			if (blkid == BLKADDR_NPA && !detach->npalf)
1451746ea742SSunil Goutham 				continue;
1452746ea742SSunil Goutham 			else if ((blkid == BLKADDR_NIX0) && !detach->nixlf)
1453746ea742SSunil Goutham 				continue;
1454a84cdceaSSubbaraya Sundeep 			else if ((blkid == BLKADDR_NIX1) && !detach->nixlf)
1455a84cdceaSSubbaraya Sundeep 				continue;
1456746ea742SSunil Goutham 			else if ((blkid == BLKADDR_SSO) && !detach->sso)
1457746ea742SSunil Goutham 				continue;
1458746ea742SSunil Goutham 			else if ((blkid == BLKADDR_SSOW) && !detach->ssow)
1459746ea742SSunil Goutham 				continue;
1460746ea742SSunil Goutham 			else if ((blkid == BLKADDR_TIM) && !detach->timlfs)
1461746ea742SSunil Goutham 				continue;
1462746ea742SSunil Goutham 			else if ((blkid == BLKADDR_CPT0) && !detach->cptlfs)
1463746ea742SSunil Goutham 				continue;
1464a84cdceaSSubbaraya Sundeep 			else if ((blkid == BLKADDR_CPT1) && !detach->cptlfs)
1465a84cdceaSSubbaraya Sundeep 				continue;
1466746ea742SSunil Goutham 		}
1467746ea742SSunil Goutham 		rvu_detach_block(rvu, pcifunc, block->type);
1468746ea742SSunil Goutham 	}
1469746ea742SSunil Goutham 
14700964fc8fSStanislaw Kardach 	mutex_unlock(&rvu->rsrc_lock);
1471746ea742SSunil Goutham 	return 0;
1472746ea742SSunil Goutham }
1473746ea742SSunil Goutham 
rvu_mbox_handler_detach_resources(struct rvu * rvu,struct rsrc_detach * detach,struct msg_rsp * rsp)1474c6614738SSubbaraya Sundeep int rvu_mbox_handler_detach_resources(struct rvu *rvu,
1475746ea742SSunil Goutham 				      struct rsrc_detach *detach,
1476746ea742SSunil Goutham 				      struct msg_rsp *rsp)
1477746ea742SSunil Goutham {
1478746ea742SSunil Goutham 	return rvu_detach_rsrcs(rvu, detach, detach->hdr.pcifunc);
1479746ea742SSunil Goutham }
1480746ea742SSunil Goutham 
rvu_get_nix_blkaddr(struct rvu * rvu,u16 pcifunc)148123109f8dSSubbaraya Sundeep int rvu_get_nix_blkaddr(struct rvu *rvu, u16 pcifunc)
1482c5a73b63SSubbaraya Sundeep {
1483c5a73b63SSubbaraya Sundeep 	struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
1484c5a73b63SSubbaraya Sundeep 	int blkaddr = BLKADDR_NIX0, vf;
1485c5a73b63SSubbaraya Sundeep 	struct rvu_pfvf *pf;
1486c5a73b63SSubbaraya Sundeep 
1487fe1939bbSRadha Mohan Chintakuntla 	pf = rvu_get_pfvf(rvu, pcifunc & ~RVU_PFVF_FUNC_MASK);
1488fe1939bbSRadha Mohan Chintakuntla 
1489c5a73b63SSubbaraya Sundeep 	/* All CGX mapped PFs are set with assigned NIX block during init */
1490c5a73b63SSubbaraya Sundeep 	if (is_pf_cgxmapped(rvu, rvu_get_pf(rvu->pdev, pcifunc))) {
1491c5a73b63SSubbaraya Sundeep 		blkaddr = pf->nix_blkaddr;
1492ae703539SGeetha sowjanya 	} else if (is_lbk_vf(rvu, pcifunc)) {
1493c5a73b63SSubbaraya Sundeep 		vf = pcifunc - 1;
1494c5a73b63SSubbaraya Sundeep 		/* Assign NIX based on VF number. All even numbered VFs get
1495c5a73b63SSubbaraya Sundeep 		 * NIX0 and odd numbered gets NIX1
1496c5a73b63SSubbaraya Sundeep 		 */
1497c5a73b63SSubbaraya Sundeep 		blkaddr = (vf & 1) ? BLKADDR_NIX1 : BLKADDR_NIX0;
1498c5a73b63SSubbaraya Sundeep 		/* NIX1 is not present on all silicons */
1499c5a73b63SSubbaraya Sundeep 		if (!is_block_implemented(rvu->hw, BLKADDR_NIX1))
1500c5a73b63SSubbaraya Sundeep 			blkaddr = BLKADDR_NIX0;
1501c5a73b63SSubbaraya Sundeep 	}
1502c5a73b63SSubbaraya Sundeep 
1503fe1939bbSRadha Mohan Chintakuntla 	/* if SDP1 then the blkaddr is NIX1 */
1504fe1939bbSRadha Mohan Chintakuntla 	if (is_sdp_pfvf(rvu, pcifunc) && pf->sdp_info->node_id == 1)
1505fe1939bbSRadha Mohan Chintakuntla 		blkaddr = BLKADDR_NIX1;
1506fe1939bbSRadha Mohan Chintakuntla 
1507c5a73b63SSubbaraya Sundeep 	switch (blkaddr) {
1508c5a73b63SSubbaraya Sundeep 	case BLKADDR_NIX1:
1509c5a73b63SSubbaraya Sundeep 		pfvf->nix_blkaddr = BLKADDR_NIX1;
15101c1935c9SSubbaraya Sundeep 		pfvf->nix_rx_intf = NIX_INTFX_RX(1);
15111c1935c9SSubbaraya Sundeep 		pfvf->nix_tx_intf = NIX_INTFX_TX(1);
1512c5a73b63SSubbaraya Sundeep 		break;
1513c5a73b63SSubbaraya Sundeep 	case BLKADDR_NIX0:
1514c5a73b63SSubbaraya Sundeep 	default:
1515c5a73b63SSubbaraya Sundeep 		pfvf->nix_blkaddr = BLKADDR_NIX0;
15161c1935c9SSubbaraya Sundeep 		pfvf->nix_rx_intf = NIX_INTFX_RX(0);
15171c1935c9SSubbaraya Sundeep 		pfvf->nix_tx_intf = NIX_INTFX_TX(0);
1518c5a73b63SSubbaraya Sundeep 		break;
1519c5a73b63SSubbaraya Sundeep 	}
1520c5a73b63SSubbaraya Sundeep 
1521c5a73b63SSubbaraya Sundeep 	return pfvf->nix_blkaddr;
1522c5a73b63SSubbaraya Sundeep }
1523c5a73b63SSubbaraya Sundeep 
rvu_get_attach_blkaddr(struct rvu * rvu,int blktype,u16 pcifunc,struct rsrc_attach * attach)1524a84cdceaSSubbaraya Sundeep static int rvu_get_attach_blkaddr(struct rvu *rvu, int blktype,
1525a84cdceaSSubbaraya Sundeep 				  u16 pcifunc, struct rsrc_attach *attach)
1526c5a73b63SSubbaraya Sundeep {
1527c5a73b63SSubbaraya Sundeep 	int blkaddr;
1528c5a73b63SSubbaraya Sundeep 
1529c5a73b63SSubbaraya Sundeep 	switch (blktype) {
1530c5a73b63SSubbaraya Sundeep 	case BLKTYPE_NIX:
1531c5a73b63SSubbaraya Sundeep 		blkaddr = rvu_get_nix_blkaddr(rvu, pcifunc);
1532c5a73b63SSubbaraya Sundeep 		break;
1533a84cdceaSSubbaraya Sundeep 	case BLKTYPE_CPT:
1534a84cdceaSSubbaraya Sundeep 		if (attach->hdr.ver < RVU_MULTI_BLK_VER)
1535a84cdceaSSubbaraya Sundeep 			return rvu_get_blkaddr(rvu, blktype, 0);
1536a84cdceaSSubbaraya Sundeep 		blkaddr = attach->cpt_blkaddr ? attach->cpt_blkaddr :
1537a84cdceaSSubbaraya Sundeep 			  BLKADDR_CPT0;
1538a84cdceaSSubbaraya Sundeep 		if (blkaddr != BLKADDR_CPT0 && blkaddr != BLKADDR_CPT1)
1539a84cdceaSSubbaraya Sundeep 			return -ENODEV;
1540a84cdceaSSubbaraya Sundeep 		break;
1541c5a73b63SSubbaraya Sundeep 	default:
1542c5a73b63SSubbaraya Sundeep 		return rvu_get_blkaddr(rvu, blktype, 0);
1543fc6f89ddSXu Wang 	}
1544c5a73b63SSubbaraya Sundeep 
1545c5a73b63SSubbaraya Sundeep 	if (is_block_implemented(rvu->hw, blkaddr))
1546c5a73b63SSubbaraya Sundeep 		return blkaddr;
1547c5a73b63SSubbaraya Sundeep 
1548c5a73b63SSubbaraya Sundeep 	return -ENODEV;
1549c5a73b63SSubbaraya Sundeep }
1550c5a73b63SSubbaraya Sundeep 
rvu_attach_block(struct rvu * rvu,int pcifunc,int blktype,int num_lfs,struct rsrc_attach * attach)1551a84cdceaSSubbaraya Sundeep static void rvu_attach_block(struct rvu *rvu, int pcifunc, int blktype,
1552a84cdceaSSubbaraya Sundeep 			     int num_lfs, struct rsrc_attach *attach)
1553746ea742SSunil Goutham {
1554746ea742SSunil Goutham 	struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
1555746ea742SSunil Goutham 	struct rvu_hwinfo *hw = rvu->hw;
1556746ea742SSunil Goutham 	struct rvu_block *block;
1557746ea742SSunil Goutham 	int slot, lf;
1558746ea742SSunil Goutham 	int blkaddr;
1559746ea742SSunil Goutham 	u64 cfg;
1560746ea742SSunil Goutham 
1561746ea742SSunil Goutham 	if (!num_lfs)
1562746ea742SSunil Goutham 		return;
1563746ea742SSunil Goutham 
1564a84cdceaSSubbaraya Sundeep 	blkaddr = rvu_get_attach_blkaddr(rvu, blktype, pcifunc, attach);
1565746ea742SSunil Goutham 	if (blkaddr < 0)
1566746ea742SSunil Goutham 		return;
1567746ea742SSunil Goutham 
1568746ea742SSunil Goutham 	block = &hw->block[blkaddr];
1569746ea742SSunil Goutham 	if (!block->lf.bmap)
1570746ea742SSunil Goutham 		return;
1571746ea742SSunil Goutham 
1572746ea742SSunil Goutham 	for (slot = 0; slot < num_lfs; slot++) {
1573746ea742SSunil Goutham 		/* Allocate the resource */
1574746ea742SSunil Goutham 		lf = rvu_alloc_rsrc(&block->lf);
1575746ea742SSunil Goutham 		if (lf < 0)
1576746ea742SSunil Goutham 			return;
1577746ea742SSunil Goutham 
1578746ea742SSunil Goutham 		cfg = (1ULL << 63) | (pcifunc << 8) | slot;
1579746ea742SSunil Goutham 		rvu_write64(rvu, blkaddr, block->lfcfg_reg |
1580746ea742SSunil Goutham 			    (lf << block->lfshift), cfg);
1581746ea742SSunil Goutham 		rvu_update_rsrc_map(rvu, pfvf, block,
1582746ea742SSunil Goutham 				    pcifunc, lf, true);
1583756051e2SSunil Goutham 
1584756051e2SSunil Goutham 		/* Set start MSIX vector for this LF within this PF/VF */
1585756051e2SSunil Goutham 		rvu_set_msix_offset(rvu, pfvf, block, lf);
1586746ea742SSunil Goutham 	}
1587746ea742SSunil Goutham }
1588746ea742SSunil Goutham 
rvu_check_rsrc_availability(struct rvu * rvu,struct rsrc_attach * req,u16 pcifunc)1589746ea742SSunil Goutham static int rvu_check_rsrc_availability(struct rvu *rvu,
1590746ea742SSunil Goutham 				       struct rsrc_attach *req, u16 pcifunc)
1591746ea742SSunil Goutham {
1592746ea742SSunil Goutham 	struct rvu_pfvf *pfvf = rvu_get_pfvf(rvu, pcifunc);
1593c5a73b63SSubbaraya Sundeep 	int free_lfs, mappedlfs, blkaddr;
1594746ea742SSunil Goutham 	struct rvu_hwinfo *hw = rvu->hw;
1595746ea742SSunil Goutham 	struct rvu_block *block;
1596746ea742SSunil Goutham 
1597746ea742SSunil Goutham 	/* Only one NPA LF can be attached */
1598cdd41e87SSubbaraya Sundeep 	if (req->npalf && !is_blktype_attached(pfvf, BLKTYPE_NPA)) {
1599746ea742SSunil Goutham 		block = &hw->block[BLKADDR_NPA];
1600746ea742SSunil Goutham 		free_lfs = rvu_rsrc_free_count(&block->lf);
1601746ea742SSunil Goutham 		if (!free_lfs)
1602746ea742SSunil Goutham 			goto fail;
1603746ea742SSunil Goutham 	} else if (req->npalf) {
1604746ea742SSunil Goutham 		dev_err(&rvu->pdev->dev,
1605746ea742SSunil Goutham 			"Func 0x%x: Invalid req, already has NPA\n",
1606746ea742SSunil Goutham 			 pcifunc);
1607746ea742SSunil Goutham 		return -EINVAL;
1608746ea742SSunil Goutham 	}
1609746ea742SSunil Goutham 
1610746ea742SSunil Goutham 	/* Only one NIX LF can be attached */
1611cdd41e87SSubbaraya Sundeep 	if (req->nixlf && !is_blktype_attached(pfvf, BLKTYPE_NIX)) {
1612a84cdceaSSubbaraya Sundeep 		blkaddr = rvu_get_attach_blkaddr(rvu, BLKTYPE_NIX,
1613a84cdceaSSubbaraya Sundeep 						 pcifunc, req);
1614c5a73b63SSubbaraya Sundeep 		if (blkaddr < 0)
1615c5a73b63SSubbaraya Sundeep 			return blkaddr;
1616c5a73b63SSubbaraya Sundeep 		block = &hw->block[blkaddr];
1617746ea742SSunil Goutham 		free_lfs = rvu_rsrc_free_count(&block->lf);
1618746ea742SSunil Goutham 		if (!free_lfs)
1619746ea742SSunil Goutham 			goto fail;
1620746ea742SSunil Goutham 	} else if (req->nixlf) {
1621746ea742SSunil Goutham 		dev_err(&rvu->pdev->dev,
1622746ea742SSunil Goutham 			"Func 0x%x: Invalid req, already has NIX\n",
1623746ea742SSunil Goutham 			pcifunc);
1624746ea742SSunil Goutham 		return -EINVAL;
1625746ea742SSunil Goutham 	}
1626746ea742SSunil Goutham 
1627746ea742SSunil Goutham 	if (req->sso) {
1628746ea742SSunil Goutham 		block = &hw->block[BLKADDR_SSO];
1629746ea742SSunil Goutham 		/* Is request within limits ? */
1630746ea742SSunil Goutham 		if (req->sso > block->lf.max) {
1631746ea742SSunil Goutham 			dev_err(&rvu->pdev->dev,
1632746ea742SSunil Goutham 				"Func 0x%x: Invalid SSO req, %d > max %d\n",
1633746ea742SSunil Goutham 				 pcifunc, req->sso, block->lf.max);
1634746ea742SSunil Goutham 			return -EINVAL;
1635746ea742SSunil Goutham 		}
1636cdd41e87SSubbaraya Sundeep 		mappedlfs = rvu_get_rsrc_mapcount(pfvf, block->addr);
1637746ea742SSunil Goutham 		free_lfs = rvu_rsrc_free_count(&block->lf);
1638746ea742SSunil Goutham 		/* Check if additional resources are available */
1639746ea742SSunil Goutham 		if (req->sso > mappedlfs &&
1640746ea742SSunil Goutham 		    ((req->sso - mappedlfs) > free_lfs))
1641746ea742SSunil Goutham 			goto fail;
1642746ea742SSunil Goutham 	}
1643746ea742SSunil Goutham 
1644746ea742SSunil Goutham 	if (req->ssow) {
1645746ea742SSunil Goutham 		block = &hw->block[BLKADDR_SSOW];
1646746ea742SSunil Goutham 		if (req->ssow > block->lf.max) {
1647746ea742SSunil Goutham 			dev_err(&rvu->pdev->dev,
1648746ea742SSunil Goutham 				"Func 0x%x: Invalid SSOW req, %d > max %d\n",
1649442e26afSAleksandr Mishin 				 pcifunc, req->ssow, block->lf.max);
1650746ea742SSunil Goutham 			return -EINVAL;
1651746ea742SSunil Goutham 		}
1652cdd41e87SSubbaraya Sundeep 		mappedlfs = rvu_get_rsrc_mapcount(pfvf, block->addr);
1653746ea742SSunil Goutham 		free_lfs = rvu_rsrc_free_count(&block->lf);
1654746ea742SSunil Goutham 		if (req->ssow > mappedlfs &&
1655746ea742SSunil Goutham 		    ((req->ssow - mappedlfs) > free_lfs))
1656746ea742SSunil Goutham 			goto fail;
1657746ea742SSunil Goutham 	}
1658746ea742SSunil Goutham 
1659746ea742SSunil Goutham 	if (req->timlfs) {
1660746ea742SSunil Goutham 		block = &hw->block[BLKADDR_TIM];
1661746ea742SSunil Goutham 		if (req->timlfs > block->lf.max) {
1662746ea742SSunil Goutham 			dev_err(&rvu->pdev->dev,
1663746ea742SSunil Goutham 				"Func 0x%x: Invalid TIMLF req, %d > max %d\n",
1664746ea742SSunil Goutham 				 pcifunc, req->timlfs, block->lf.max);
1665746ea742SSunil Goutham 			return -EINVAL;
1666746ea742SSunil Goutham 		}
1667cdd41e87SSubbaraya Sundeep 		mappedlfs = rvu_get_rsrc_mapcount(pfvf, block->addr);
1668746ea742SSunil Goutham 		free_lfs = rvu_rsrc_free_count(&block->lf);
1669746ea742SSunil Goutham 		if (req->timlfs > mappedlfs &&
1670746ea742SSunil Goutham 		    ((req->timlfs - mappedlfs) > free_lfs))
1671746ea742SSunil Goutham 			goto fail;
1672746ea742SSunil Goutham 	}
1673746ea742SSunil Goutham 
1674746ea742SSunil Goutham 	if (req->cptlfs) {
1675a84cdceaSSubbaraya Sundeep 		blkaddr = rvu_get_attach_blkaddr(rvu, BLKTYPE_CPT,
1676a84cdceaSSubbaraya Sundeep 						 pcifunc, req);
1677a84cdceaSSubbaraya Sundeep 		if (blkaddr < 0)
1678a84cdceaSSubbaraya Sundeep 			return blkaddr;
1679a84cdceaSSubbaraya Sundeep 		block = &hw->block[blkaddr];
1680746ea742SSunil Goutham 		if (req->cptlfs > block->lf.max) {
1681746ea742SSunil Goutham 			dev_err(&rvu->pdev->dev,
1682746ea742SSunil Goutham 				"Func 0x%x: Invalid CPTLF req, %d > max %d\n",
1683746ea742SSunil Goutham 				 pcifunc, req->cptlfs, block->lf.max);
1684746ea742SSunil Goutham 			return -EINVAL;
1685746ea742SSunil Goutham 		}
1686cdd41e87SSubbaraya Sundeep 		mappedlfs = rvu_get_rsrc_mapcount(pfvf, block->addr);
1687746ea742SSunil Goutham 		free_lfs = rvu_rsrc_free_count(&block->lf);
1688746ea742SSunil Goutham 		if (req->cptlfs > mappedlfs &&
1689746ea742SSunil Goutham 		    ((req->cptlfs - mappedlfs) > free_lfs))
1690746ea742SSunil Goutham 			goto fail;
1691746ea742SSunil Goutham 	}
1692746ea742SSunil Goutham 
1693746ea742SSunil Goutham 	return 0;
1694746ea742SSunil Goutham 
1695746ea742SSunil Goutham fail:
1696746ea742SSunil Goutham 	dev_info(rvu->dev, "Request for %s failed\n", block->name);
1697746ea742SSunil Goutham 	return -ENOSPC;
1698746ea742SSunil Goutham }
1699746ea742SSunil Goutham 
rvu_attach_from_same_block(struct rvu * rvu,int blktype,struct rsrc_attach * attach)1700a84cdceaSSubbaraya Sundeep static bool rvu_attach_from_same_block(struct rvu *rvu, int blktype,
1701a84cdceaSSubbaraya Sundeep 				       struct rsrc_attach *attach)
1702a84cdceaSSubbaraya Sundeep {
1703a84cdceaSSubbaraya Sundeep 	int blkaddr, num_lfs;
1704a84cdceaSSubbaraya Sundeep 
1705a84cdceaSSubbaraya Sundeep 	blkaddr = rvu_get_attach_blkaddr(rvu, blktype,
1706a84cdceaSSubbaraya Sundeep 					 attach->hdr.pcifunc, attach);
1707a84cdceaSSubbaraya Sundeep 	if (blkaddr < 0)
1708a84cdceaSSubbaraya Sundeep 		return false;
1709a84cdceaSSubbaraya Sundeep 
1710a84cdceaSSubbaraya Sundeep 	num_lfs = rvu_get_rsrc_mapcount(rvu_get_pfvf(rvu, attach->hdr.pcifunc),
1711a84cdceaSSubbaraya Sundeep 					blkaddr);
1712a84cdceaSSubbaraya Sundeep 	/* Requester already has LFs from given block ? */
1713a84cdceaSSubbaraya Sundeep 	return !!num_lfs;
1714a84cdceaSSubbaraya Sundeep }
1715a84cdceaSSubbaraya Sundeep 
rvu_mbox_handler_attach_resources(struct rvu * rvu,struct rsrc_attach * attach,struct msg_rsp * rsp)1716c6614738SSubbaraya Sundeep int rvu_mbox_handler_attach_resources(struct rvu *rvu,
1717746ea742SSunil Goutham 				      struct rsrc_attach *attach,
1718746ea742SSunil Goutham 				      struct msg_rsp *rsp)
1719746ea742SSunil Goutham {
1720746ea742SSunil Goutham 	u16 pcifunc = attach->hdr.pcifunc;
1721eec6f752SYueHaibing 	int err;
1722746ea742SSunil Goutham 
1723746ea742SSunil Goutham 	/* If first request, detach all existing attached resources */
1724746ea742SSunil Goutham 	if (!attach->modify)
1725746ea742SSunil Goutham 		rvu_detach_rsrcs(rvu, NULL, pcifunc);
1726746ea742SSunil Goutham 
17270964fc8fSStanislaw Kardach 	mutex_lock(&rvu->rsrc_lock);
1728746ea742SSunil Goutham 
1729746ea742SSunil Goutham 	/* Check if the request can be accommodated */
1730746ea742SSunil Goutham 	err = rvu_check_rsrc_availability(rvu, attach, pcifunc);
1731746ea742SSunil Goutham 	if (err)
1732746ea742SSunil Goutham 		goto exit;
1733746ea742SSunil Goutham 
1734746ea742SSunil Goutham 	/* Now attach the requested resources */
1735746ea742SSunil Goutham 	if (attach->npalf)
1736a84cdceaSSubbaraya Sundeep 		rvu_attach_block(rvu, pcifunc, BLKTYPE_NPA, 1, attach);
1737746ea742SSunil Goutham 
1738746ea742SSunil Goutham 	if (attach->nixlf)
1739a84cdceaSSubbaraya Sundeep 		rvu_attach_block(rvu, pcifunc, BLKTYPE_NIX, 1, attach);
1740746ea742SSunil Goutham 
1741746ea742SSunil Goutham 	if (attach->sso) {
1742746ea742SSunil Goutham 		/* RVU func doesn't know which exact LF or slot is attached
1743746ea742SSunil Goutham 		 * to it, it always sees as slot 0,1,2. So for a 'modify'
1744746ea742SSunil Goutham 		 * request, simply detach all existing attached LFs/slots
1745746ea742SSunil Goutham 		 * and attach a fresh.
1746746ea742SSunil Goutham 		 */
1747746ea742SSunil Goutham 		if (attach->modify)
1748746ea742SSunil Goutham 			rvu_detach_block(rvu, pcifunc, BLKTYPE_SSO);
1749a84cdceaSSubbaraya Sundeep 		rvu_attach_block(rvu, pcifunc, BLKTYPE_SSO,
1750a84cdceaSSubbaraya Sundeep 				 attach->sso, attach);
1751746ea742SSunil Goutham 	}
1752746ea742SSunil Goutham 
1753746ea742SSunil Goutham 	if (attach->ssow) {
1754746ea742SSunil Goutham 		if (attach->modify)
1755746ea742SSunil Goutham 			rvu_detach_block(rvu, pcifunc, BLKTYPE_SSOW);
1756a84cdceaSSubbaraya Sundeep 		rvu_attach_block(rvu, pcifunc, BLKTYPE_SSOW,
1757a84cdceaSSubbaraya Sundeep 				 attach->ssow, attach);
1758746ea742SSunil Goutham 	}
1759746ea742SSunil Goutham 
1760746ea742SSunil Goutham 	if (attach->timlfs) {
1761746ea742SSunil Goutham 		if (attach->modify)
1762746ea742SSunil Goutham 			rvu_detach_block(rvu, pcifunc, BLKTYPE_TIM);
1763a84cdceaSSubbaraya Sundeep 		rvu_attach_block(rvu, pcifunc, BLKTYPE_TIM,
1764a84cdceaSSubbaraya Sundeep 				 attach->timlfs, attach);
1765746ea742SSunil Goutham 	}
1766746ea742SSunil Goutham 
1767746ea742SSunil Goutham 	if (attach->cptlfs) {
1768a84cdceaSSubbaraya Sundeep 		if (attach->modify &&
1769a84cdceaSSubbaraya Sundeep 		    rvu_attach_from_same_block(rvu, BLKTYPE_CPT, attach))
1770746ea742SSunil Goutham 			rvu_detach_block(rvu, pcifunc, BLKTYPE_CPT);
1771a84cdceaSSubbaraya Sundeep 		rvu_attach_block(rvu, pcifunc, BLKTYPE_CPT,
1772a84cdceaSSubbaraya Sundeep 				 attach->cptlfs, attach);
1773746ea742SSunil Goutham 	}
1774746ea742SSunil Goutham 
1775746ea742SSunil Goutham exit:
17760964fc8fSStanislaw Kardach 	mutex_unlock(&rvu->rsrc_lock);
1777746ea742SSunil Goutham 	return err;
1778746ea742SSunil Goutham }
1779746ea742SSunil Goutham 
rvu_get_msix_offset(struct rvu * rvu,struct rvu_pfvf * pfvf,int blkaddr,int lf)1780756051e2SSunil Goutham static u16 rvu_get_msix_offset(struct rvu *rvu, struct rvu_pfvf *pfvf,
1781756051e2SSunil Goutham 			       int blkaddr, int lf)
1782756051e2SSunil Goutham {
1783756051e2SSunil Goutham 	u16 vec;
1784756051e2SSunil Goutham 
1785756051e2SSunil Goutham 	if (lf < 0)
1786756051e2SSunil Goutham 		return MSIX_VECTOR_INVALID;
1787756051e2SSunil Goutham 
1788756051e2SSunil Goutham 	for (vec = 0; vec < pfvf->msix.max; vec++) {
1789756051e2SSunil Goutham 		if (pfvf->msix_lfmap[vec] == MSIX_BLKLF(blkaddr, lf))
1790756051e2SSunil Goutham 			return vec;
1791756051e2SSunil Goutham 	}
1792756051e2SSunil Goutham 	return MSIX_VECTOR_INVALID;
1793756051e2SSunil Goutham }
1794756051e2SSunil Goutham 
rvu_set_msix_offset(struct rvu * rvu,struct rvu_pfvf * pfvf,struct rvu_block * block,int lf)1795756051e2SSunil Goutham static void rvu_set_msix_offset(struct rvu *rvu, struct rvu_pfvf *pfvf,
1796756051e2SSunil Goutham 				struct rvu_block *block, int lf)
1797756051e2SSunil Goutham {
1798756051e2SSunil Goutham 	u16 nvecs, vec, offset;
1799756051e2SSunil Goutham 	u64 cfg;
1800756051e2SSunil Goutham 
1801756051e2SSunil Goutham 	cfg = rvu_read64(rvu, block->addr, block->msixcfg_reg |
1802756051e2SSunil Goutham 			 (lf << block->lfshift));
1803756051e2SSunil Goutham 	nvecs = (cfg >> 12) & 0xFF;
1804756051e2SSunil Goutham 
1805756051e2SSunil Goutham 	/* Check and alloc MSIX vectors, must be contiguous */
1806756051e2SSunil Goutham 	if (!rvu_rsrc_check_contig(&pfvf->msix, nvecs))
1807756051e2SSunil Goutham 		return;
1808756051e2SSunil Goutham 
1809756051e2SSunil Goutham 	offset = rvu_alloc_rsrc_contig(&pfvf->msix, nvecs);
1810756051e2SSunil Goutham 
1811756051e2SSunil Goutham 	/* Config MSIX offset in LF */
1812756051e2SSunil Goutham 	rvu_write64(rvu, block->addr, block->msixcfg_reg |
1813756051e2SSunil Goutham 		    (lf << block->lfshift), (cfg & ~0x7FFULL) | offset);
1814756051e2SSunil Goutham 
1815756051e2SSunil Goutham 	/* Update the bitmap as well */
1816756051e2SSunil Goutham 	for (vec = 0; vec < nvecs; vec++)
1817756051e2SSunil Goutham 		pfvf->msix_lfmap[offset + vec] = MSIX_BLKLF(block->addr, lf);
1818756051e2SSunil Goutham }
1819756051e2SSunil Goutham 
rvu_clear_msix_offset(struct rvu * rvu,struct rvu_pfvf * pfvf,struct rvu_block * block,int lf)1820756051e2SSunil Goutham static void rvu_clear_msix_offset(struct rvu *rvu, struct rvu_pfvf *pfvf,
1821756051e2SSunil Goutham 				  struct rvu_block *block, int lf)
1822756051e2SSunil Goutham {
1823756051e2SSunil Goutham 	u16 nvecs, vec, offset;
1824756051e2SSunil Goutham 	u64 cfg;
1825756051e2SSunil Goutham 
1826756051e2SSunil Goutham 	cfg = rvu_read64(rvu, block->addr, block->msixcfg_reg |
1827756051e2SSunil Goutham 			 (lf << block->lfshift));
1828756051e2SSunil Goutham 	nvecs = (cfg >> 12) & 0xFF;
1829756051e2SSunil Goutham 
1830756051e2SSunil Goutham 	/* Clear MSIX offset in LF */
1831756051e2SSunil Goutham 	rvu_write64(rvu, block->addr, block->msixcfg_reg |
1832756051e2SSunil Goutham 		    (lf << block->lfshift), cfg & ~0x7FFULL);
1833756051e2SSunil Goutham 
1834756051e2SSunil Goutham 	offset = rvu_get_msix_offset(rvu, pfvf, block->addr, lf);
1835756051e2SSunil Goutham 
1836756051e2SSunil Goutham 	/* Update the mapping */
1837756051e2SSunil Goutham 	for (vec = 0; vec < nvecs; vec++)
1838756051e2SSunil Goutham 		pfvf->msix_lfmap[offset + vec] = 0;
1839756051e2SSunil Goutham 
1840756051e2SSunil Goutham 	/* Free the same in MSIX bitmap */
1841756051e2SSunil Goutham 	rvu_free_rsrc_contig(&pfvf->msix, nvecs, offset);
1842756051e2SSunil Goutham }
1843756051e2SSunil Goutham 
rvu_mbox_handler_msix_offset(struct rvu * rvu,struct msg_req * req,struct msix_offset_rsp * rsp)1844c6614738SSubbaraya Sundeep int rvu_mbox_handler_msix_offset(struct rvu *rvu, struct msg_req *req,
1845756051e2SSunil Goutham 				 struct msix_offset_rsp *rsp)
1846756051e2SSunil Goutham {
1847756051e2SSunil Goutham 	struct rvu_hwinfo *hw = rvu->hw;
1848756051e2SSunil Goutham 	u16 pcifunc = req->hdr.pcifunc;
1849756051e2SSunil Goutham 	struct rvu_pfvf *pfvf;
1850a84cdceaSSubbaraya Sundeep 	int lf, slot, blkaddr;
1851756051e2SSunil Goutham 
1852756051e2SSunil Goutham 	pfvf = rvu_get_pfvf(rvu, pcifunc);
1853756051e2SSunil Goutham 	if (!pfvf->msix.bmap)
1854756051e2SSunil Goutham 		return 0;
1855756051e2SSunil Goutham 
1856756051e2SSunil Goutham 	/* Set MSIX offsets for each block's LFs attached to this PF/VF */
1857756051e2SSunil Goutham 	lf = rvu_get_lf(rvu, &hw->block[BLKADDR_NPA], pcifunc, 0);
1858756051e2SSunil Goutham 	rsp->npa_msixoff = rvu_get_msix_offset(rvu, pfvf, BLKADDR_NPA, lf);
1859756051e2SSunil Goutham 
1860a84cdceaSSubbaraya Sundeep 	/* Get BLKADDR from which LFs are attached to pcifunc */
1861a84cdceaSSubbaraya Sundeep 	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
1862a84cdceaSSubbaraya Sundeep 	if (blkaddr < 0) {
1863a84cdceaSSubbaraya Sundeep 		rsp->nix_msixoff = MSIX_VECTOR_INVALID;
1864a84cdceaSSubbaraya Sundeep 	} else {
1865a84cdceaSSubbaraya Sundeep 		lf = rvu_get_lf(rvu, &hw->block[blkaddr], pcifunc, 0);
1866a84cdceaSSubbaraya Sundeep 		rsp->nix_msixoff = rvu_get_msix_offset(rvu, pfvf, blkaddr, lf);
1867a84cdceaSSubbaraya Sundeep 	}
1868756051e2SSunil Goutham 
1869756051e2SSunil Goutham 	rsp->sso = pfvf->sso;
1870756051e2SSunil Goutham 	for (slot = 0; slot < rsp->sso; slot++) {
1871756051e2SSunil Goutham 		lf = rvu_get_lf(rvu, &hw->block[BLKADDR_SSO], pcifunc, slot);
1872756051e2SSunil Goutham 		rsp->sso_msixoff[slot] =
1873756051e2SSunil Goutham 			rvu_get_msix_offset(rvu, pfvf, BLKADDR_SSO, lf);
1874756051e2SSunil Goutham 	}
1875756051e2SSunil Goutham 
1876756051e2SSunil Goutham 	rsp->ssow = pfvf->ssow;
1877756051e2SSunil Goutham 	for (slot = 0; slot < rsp->ssow; slot++) {
1878756051e2SSunil Goutham 		lf = rvu_get_lf(rvu, &hw->block[BLKADDR_SSOW], pcifunc, slot);
1879756051e2SSunil Goutham 		rsp->ssow_msixoff[slot] =
1880756051e2SSunil Goutham 			rvu_get_msix_offset(rvu, pfvf, BLKADDR_SSOW, lf);
1881756051e2SSunil Goutham 	}
1882756051e2SSunil Goutham 
1883756051e2SSunil Goutham 	rsp->timlfs = pfvf->timlfs;
1884756051e2SSunil Goutham 	for (slot = 0; slot < rsp->timlfs; slot++) {
1885756051e2SSunil Goutham 		lf = rvu_get_lf(rvu, &hw->block[BLKADDR_TIM], pcifunc, slot);
1886756051e2SSunil Goutham 		rsp->timlf_msixoff[slot] =
1887756051e2SSunil Goutham 			rvu_get_msix_offset(rvu, pfvf, BLKADDR_TIM, lf);
1888756051e2SSunil Goutham 	}
1889756051e2SSunil Goutham 
1890756051e2SSunil Goutham 	rsp->cptlfs = pfvf->cptlfs;
1891756051e2SSunil Goutham 	for (slot = 0; slot < rsp->cptlfs; slot++) {
1892756051e2SSunil Goutham 		lf = rvu_get_lf(rvu, &hw->block[BLKADDR_CPT0], pcifunc, slot);
1893756051e2SSunil Goutham 		rsp->cptlf_msixoff[slot] =
1894756051e2SSunil Goutham 			rvu_get_msix_offset(rvu, pfvf, BLKADDR_CPT0, lf);
1895756051e2SSunil Goutham 	}
1896a84cdceaSSubbaraya Sundeep 
1897a84cdceaSSubbaraya Sundeep 	rsp->cpt1_lfs = pfvf->cpt1_lfs;
1898a84cdceaSSubbaraya Sundeep 	for (slot = 0; slot < rsp->cpt1_lfs; slot++) {
1899a84cdceaSSubbaraya Sundeep 		lf = rvu_get_lf(rvu, &hw->block[BLKADDR_CPT1], pcifunc, slot);
1900a84cdceaSSubbaraya Sundeep 		rsp->cpt1_lf_msixoff[slot] =
1901a84cdceaSSubbaraya Sundeep 			rvu_get_msix_offset(rvu, pfvf, BLKADDR_CPT1, lf);
1902a84cdceaSSubbaraya Sundeep 	}
1903a84cdceaSSubbaraya Sundeep 
1904756051e2SSunil Goutham 	return 0;
1905756051e2SSunil Goutham }
1906756051e2SSunil Goutham 
rvu_mbox_handler_free_rsrc_cnt(struct rvu * rvu,struct msg_req * req,struct free_rsrcs_rsp * rsp)1907275e5d17SGeorge Cherian int rvu_mbox_handler_free_rsrc_cnt(struct rvu *rvu, struct msg_req *req,
1908275e5d17SGeorge Cherian 				   struct free_rsrcs_rsp *rsp)
1909275e5d17SGeorge Cherian {
1910275e5d17SGeorge Cherian 	struct rvu_hwinfo *hw = rvu->hw;
1911275e5d17SGeorge Cherian 	struct rvu_block *block;
1912275e5d17SGeorge Cherian 	struct nix_txsch *txsch;
1913275e5d17SGeorge Cherian 	struct nix_hw *nix_hw;
1914275e5d17SGeorge Cherian 
1915275e5d17SGeorge Cherian 	mutex_lock(&rvu->rsrc_lock);
1916275e5d17SGeorge Cherian 
1917275e5d17SGeorge Cherian 	block = &hw->block[BLKADDR_NPA];
1918275e5d17SGeorge Cherian 	rsp->npa = rvu_rsrc_free_count(&block->lf);
1919275e5d17SGeorge Cherian 
1920275e5d17SGeorge Cherian 	block = &hw->block[BLKADDR_NIX0];
1921275e5d17SGeorge Cherian 	rsp->nix = rvu_rsrc_free_count(&block->lf);
1922275e5d17SGeorge Cherian 
1923275e5d17SGeorge Cherian 	block = &hw->block[BLKADDR_NIX1];
1924275e5d17SGeorge Cherian 	rsp->nix1 = rvu_rsrc_free_count(&block->lf);
1925275e5d17SGeorge Cherian 
1926275e5d17SGeorge Cherian 	block = &hw->block[BLKADDR_SSO];
1927275e5d17SGeorge Cherian 	rsp->sso = rvu_rsrc_free_count(&block->lf);
1928275e5d17SGeorge Cherian 
1929275e5d17SGeorge Cherian 	block = &hw->block[BLKADDR_SSOW];
1930275e5d17SGeorge Cherian 	rsp->ssow = rvu_rsrc_free_count(&block->lf);
1931275e5d17SGeorge Cherian 
1932275e5d17SGeorge Cherian 	block = &hw->block[BLKADDR_TIM];
1933275e5d17SGeorge Cherian 	rsp->tim = rvu_rsrc_free_count(&block->lf);
1934275e5d17SGeorge Cherian 
1935275e5d17SGeorge Cherian 	block = &hw->block[BLKADDR_CPT0];
1936275e5d17SGeorge Cherian 	rsp->cpt = rvu_rsrc_free_count(&block->lf);
1937275e5d17SGeorge Cherian 
1938275e5d17SGeorge Cherian 	block = &hw->block[BLKADDR_CPT1];
1939275e5d17SGeorge Cherian 	rsp->cpt1 = rvu_rsrc_free_count(&block->lf);
1940275e5d17SGeorge Cherian 
1941275e5d17SGeorge Cherian 	if (rvu->hw->cap.nix_fixed_txschq_mapping) {
1942275e5d17SGeorge Cherian 		rsp->schq[NIX_TXSCH_LVL_SMQ] = 1;
1943275e5d17SGeorge Cherian 		rsp->schq[NIX_TXSCH_LVL_TL4] = 1;
1944275e5d17SGeorge Cherian 		rsp->schq[NIX_TXSCH_LVL_TL3] = 1;
1945275e5d17SGeorge Cherian 		rsp->schq[NIX_TXSCH_LVL_TL2] = 1;
1946275e5d17SGeorge Cherian 		/* NIX1 */
1947275e5d17SGeorge Cherian 		if (!is_block_implemented(rvu->hw, BLKADDR_NIX1))
1948275e5d17SGeorge Cherian 			goto out;
1949275e5d17SGeorge Cherian 		rsp->schq_nix1[NIX_TXSCH_LVL_SMQ] = 1;
1950275e5d17SGeorge Cherian 		rsp->schq_nix1[NIX_TXSCH_LVL_TL4] = 1;
1951275e5d17SGeorge Cherian 		rsp->schq_nix1[NIX_TXSCH_LVL_TL3] = 1;
1952275e5d17SGeorge Cherian 		rsp->schq_nix1[NIX_TXSCH_LVL_TL2] = 1;
1953275e5d17SGeorge Cherian 	} else {
1954275e5d17SGeorge Cherian 		nix_hw = get_nix_hw(hw, BLKADDR_NIX0);
1955275e5d17SGeorge Cherian 		txsch = &nix_hw->txsch[NIX_TXSCH_LVL_SMQ];
1956275e5d17SGeorge Cherian 		rsp->schq[NIX_TXSCH_LVL_SMQ] =
1957275e5d17SGeorge Cherian 				rvu_rsrc_free_count(&txsch->schq);
1958275e5d17SGeorge Cherian 
1959275e5d17SGeorge Cherian 		txsch = &nix_hw->txsch[NIX_TXSCH_LVL_TL4];
1960275e5d17SGeorge Cherian 		rsp->schq[NIX_TXSCH_LVL_TL4] =
1961275e5d17SGeorge Cherian 				rvu_rsrc_free_count(&txsch->schq);
1962275e5d17SGeorge Cherian 
1963275e5d17SGeorge Cherian 		txsch = &nix_hw->txsch[NIX_TXSCH_LVL_TL3];
1964275e5d17SGeorge Cherian 		rsp->schq[NIX_TXSCH_LVL_TL3] =
1965275e5d17SGeorge Cherian 				rvu_rsrc_free_count(&txsch->schq);
1966275e5d17SGeorge Cherian 
1967275e5d17SGeorge Cherian 		txsch = &nix_hw->txsch[NIX_TXSCH_LVL_TL2];
1968275e5d17SGeorge Cherian 		rsp->schq[NIX_TXSCH_LVL_TL2] =
1969275e5d17SGeorge Cherian 				rvu_rsrc_free_count(&txsch->schq);
1970275e5d17SGeorge Cherian 
1971275e5d17SGeorge Cherian 		if (!is_block_implemented(rvu->hw, BLKADDR_NIX1))
1972275e5d17SGeorge Cherian 			goto out;
1973275e5d17SGeorge Cherian 
1974275e5d17SGeorge Cherian 		nix_hw = get_nix_hw(hw, BLKADDR_NIX1);
1975275e5d17SGeorge Cherian 		txsch = &nix_hw->txsch[NIX_TXSCH_LVL_SMQ];
1976275e5d17SGeorge Cherian 		rsp->schq_nix1[NIX_TXSCH_LVL_SMQ] =
1977275e5d17SGeorge Cherian 				rvu_rsrc_free_count(&txsch->schq);
1978275e5d17SGeorge Cherian 
1979275e5d17SGeorge Cherian 		txsch = &nix_hw->txsch[NIX_TXSCH_LVL_TL4];
1980275e5d17SGeorge Cherian 		rsp->schq_nix1[NIX_TXSCH_LVL_TL4] =
1981275e5d17SGeorge Cherian 				rvu_rsrc_free_count(&txsch->schq);
1982275e5d17SGeorge Cherian 
1983275e5d17SGeorge Cherian 		txsch = &nix_hw->txsch[NIX_TXSCH_LVL_TL3];
1984275e5d17SGeorge Cherian 		rsp->schq_nix1[NIX_TXSCH_LVL_TL3] =
1985275e5d17SGeorge Cherian 				rvu_rsrc_free_count(&txsch->schq);
1986275e5d17SGeorge Cherian 
1987275e5d17SGeorge Cherian 		txsch = &nix_hw->txsch[NIX_TXSCH_LVL_TL2];
1988275e5d17SGeorge Cherian 		rsp->schq_nix1[NIX_TXSCH_LVL_TL2] =
1989275e5d17SGeorge Cherian 				rvu_rsrc_free_count(&txsch->schq);
1990275e5d17SGeorge Cherian 	}
1991275e5d17SGeorge Cherian 
1992275e5d17SGeorge Cherian 	rsp->schq_nix1[NIX_TXSCH_LVL_TL1] = 1;
1993275e5d17SGeorge Cherian out:
1994275e5d17SGeorge Cherian 	rsp->schq[NIX_TXSCH_LVL_TL1] = 1;
1995275e5d17SGeorge Cherian 	mutex_unlock(&rvu->rsrc_lock);
1996275e5d17SGeorge Cherian 
1997275e5d17SGeorge Cherian 	return 0;
1998275e5d17SGeorge Cherian }
1999275e5d17SGeorge Cherian 
rvu_mbox_handler_vf_flr(struct rvu * rvu,struct msg_req * req,struct msg_rsp * rsp)2000c6614738SSubbaraya Sundeep int rvu_mbox_handler_vf_flr(struct rvu *rvu, struct msg_req *req,
2001c554f9c1SGeetha sowjanya 			    struct msg_rsp *rsp)
2002c554f9c1SGeetha sowjanya {
2003c554f9c1SGeetha sowjanya 	u16 pcifunc = req->hdr.pcifunc;
2004c554f9c1SGeetha sowjanya 	u16 vf, numvfs;
2005c554f9c1SGeetha sowjanya 	u64 cfg;
2006c554f9c1SGeetha sowjanya 
2007c554f9c1SGeetha sowjanya 	vf = pcifunc & RVU_PFVF_FUNC_MASK;
2008c554f9c1SGeetha sowjanya 	cfg = rvu_read64(rvu, BLKADDR_RVUM,
2009c554f9c1SGeetha sowjanya 			 RVU_PRIV_PFX_CFG(rvu_get_pf(rvu->pdev, pcifunc)));
2010c554f9c1SGeetha sowjanya 	numvfs = (cfg >> 12) & 0xFF;
2011c554f9c1SGeetha sowjanya 
2012c554f9c1SGeetha sowjanya 	if (vf && vf <= numvfs)
2013c554f9c1SGeetha sowjanya 		__rvu_flr_handler(rvu, pcifunc);
2014c554f9c1SGeetha sowjanya 	else
2015c554f9c1SGeetha sowjanya 		return RVU_INVALID_VF_ID;
2016c554f9c1SGeetha sowjanya 
2017c554f9c1SGeetha sowjanya 	return 0;
2018c554f9c1SGeetha sowjanya }
2019c554f9c1SGeetha sowjanya 
rvu_ndc_sync(struct rvu * rvu,int lfblkaddr,int lfidx,u64 lfoffset)202042c45ac1SNithin Dabilpuram int rvu_ndc_sync(struct rvu *rvu, int lfblkaddr, int lfidx, u64 lfoffset)
202142c45ac1SNithin Dabilpuram {
202242c45ac1SNithin Dabilpuram 	/* Sync cached info for this LF in NDC to LLC/DRAM */
202342c45ac1SNithin Dabilpuram 	rvu_write64(rvu, lfblkaddr, lfoffset, BIT_ULL(12) | lfidx);
202442c45ac1SNithin Dabilpuram 	return rvu_poll_reg(rvu, lfblkaddr, lfoffset, BIT_ULL(12), true);
202542c45ac1SNithin Dabilpuram }
202642c45ac1SNithin Dabilpuram 
rvu_mbox_handler_get_hw_cap(struct rvu * rvu,struct msg_req * req,struct get_hw_cap_rsp * rsp)20275d9b976dSSunil Goutham int rvu_mbox_handler_get_hw_cap(struct rvu *rvu, struct msg_req *req,
20285d9b976dSSunil Goutham 				struct get_hw_cap_rsp *rsp)
20295d9b976dSSunil Goutham {
20305d9b976dSSunil Goutham 	struct rvu_hwinfo *hw = rvu->hw;
20315d9b976dSSunil Goutham 
20325d9b976dSSunil Goutham 	rsp->nix_fixed_txschq_mapping = hw->cap.nix_fixed_txschq_mapping;
20335d9b976dSSunil Goutham 	rsp->nix_shaping = hw->cap.nix_shaping;
203456d9f5fdSRatheesh Kannoth 	rsp->npc_hash_extract = hw->cap.npc_hash_extract;
20355d9b976dSSunil Goutham 
2036*73203837SSubbaraya Sundeep 	if (rvu->mcs_blk_cnt)
2037*73203837SSubbaraya Sundeep 		rsp->hw_caps = HW_CAP_MACSEC;
2038*73203837SSubbaraya Sundeep 
20395d9b976dSSunil Goutham 	return 0;
20405d9b976dSSunil Goutham }
20415d9b976dSSunil Goutham 
rvu_mbox_handler_set_vf_perm(struct rvu * rvu,struct set_vf_perm * req,struct msg_rsp * rsp)2042bd4302b8SHariprasad Kelam int rvu_mbox_handler_set_vf_perm(struct rvu *rvu, struct set_vf_perm *req,
2043bd4302b8SHariprasad Kelam 				 struct msg_rsp *rsp)
2044bd4302b8SHariprasad Kelam {
2045bd4302b8SHariprasad Kelam 	struct rvu_hwinfo *hw = rvu->hw;
2046bd4302b8SHariprasad Kelam 	u16 pcifunc = req->hdr.pcifunc;
2047bd4302b8SHariprasad Kelam 	struct rvu_pfvf *pfvf;
2048bd4302b8SHariprasad Kelam 	int blkaddr, nixlf;
2049bd4302b8SHariprasad Kelam 	u16 target;
2050bd4302b8SHariprasad Kelam 
2051bd4302b8SHariprasad Kelam 	/* Only PF can add VF permissions */
2052ae703539SGeetha sowjanya 	if ((pcifunc & RVU_PFVF_FUNC_MASK) || is_lbk_vf(rvu, pcifunc))
2053bd4302b8SHariprasad Kelam 		return -EOPNOTSUPP;
2054bd4302b8SHariprasad Kelam 
2055bd4302b8SHariprasad Kelam 	target = (pcifunc & ~RVU_PFVF_FUNC_MASK) | (req->vf + 1);
2056bd4302b8SHariprasad Kelam 	pfvf = rvu_get_pfvf(rvu, target);
2057bd4302b8SHariprasad Kelam 
2058bd4302b8SHariprasad Kelam 	if (req->flags & RESET_VF_PERM) {
2059bd4302b8SHariprasad Kelam 		pfvf->flags &= RVU_CLEAR_VF_PERM;
2060bd4302b8SHariprasad Kelam 	} else if (test_bit(PF_SET_VF_TRUSTED, &pfvf->flags) ^
2061bd4302b8SHariprasad Kelam 		 (req->flags & VF_TRUSTED)) {
2062bd4302b8SHariprasad Kelam 		change_bit(PF_SET_VF_TRUSTED, &pfvf->flags);
2063bd4302b8SHariprasad Kelam 		/* disable multicast and promisc entries */
2064bd4302b8SHariprasad Kelam 		if (!test_bit(PF_SET_VF_TRUSTED, &pfvf->flags)) {
2065bd4302b8SHariprasad Kelam 			blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, target);
2066bd4302b8SHariprasad Kelam 			if (blkaddr < 0)
2067bd4302b8SHariprasad Kelam 				return 0;
2068bd4302b8SHariprasad Kelam 			nixlf = rvu_get_lf(rvu, &hw->block[blkaddr],
2069bd4302b8SHariprasad Kelam 					   target, 0);
2070bd4302b8SHariprasad Kelam 			if (nixlf < 0)
2071bd4302b8SHariprasad Kelam 				return 0;
2072bd4302b8SHariprasad Kelam 			npc_enadis_default_mce_entry(rvu, target, nixlf,
2073bd4302b8SHariprasad Kelam 						     NIXLF_ALLMULTI_ENTRY,
2074bd4302b8SHariprasad Kelam 						     false);
2075bd4302b8SHariprasad Kelam 			npc_enadis_default_mce_entry(rvu, target, nixlf,
2076bd4302b8SHariprasad Kelam 						     NIXLF_PROMISC_ENTRY,
2077bd4302b8SHariprasad Kelam 						     false);
2078bd4302b8SHariprasad Kelam 		}
2079bd4302b8SHariprasad Kelam 	}
2080bd4302b8SHariprasad Kelam 
2081bd4302b8SHariprasad Kelam 	return 0;
2082bd4302b8SHariprasad Kelam }
2083bd4302b8SHariprasad Kelam 
rvu_mbox_handler_ndc_sync_op(struct rvu * rvu,struct ndc_sync_op * req,struct msg_rsp * rsp)208442c45ac1SNithin Dabilpuram int rvu_mbox_handler_ndc_sync_op(struct rvu *rvu,
208542c45ac1SNithin Dabilpuram 				 struct ndc_sync_op *req,
208642c45ac1SNithin Dabilpuram 				 struct msg_rsp *rsp)
208742c45ac1SNithin Dabilpuram {
208842c45ac1SNithin Dabilpuram 	struct rvu_hwinfo *hw = rvu->hw;
208942c45ac1SNithin Dabilpuram 	u16 pcifunc = req->hdr.pcifunc;
209042c45ac1SNithin Dabilpuram 	int err, lfidx, lfblkaddr;
209142c45ac1SNithin Dabilpuram 
209242c45ac1SNithin Dabilpuram 	if (req->npa_lf_sync) {
209342c45ac1SNithin Dabilpuram 		/* Get NPA LF data */
209442c45ac1SNithin Dabilpuram 		lfblkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NPA, pcifunc);
209542c45ac1SNithin Dabilpuram 		if (lfblkaddr < 0)
209642c45ac1SNithin Dabilpuram 			return NPA_AF_ERR_AF_LF_INVALID;
209742c45ac1SNithin Dabilpuram 
209842c45ac1SNithin Dabilpuram 		lfidx = rvu_get_lf(rvu, &hw->block[lfblkaddr], pcifunc, 0);
209942c45ac1SNithin Dabilpuram 		if (lfidx < 0)
210042c45ac1SNithin Dabilpuram 			return NPA_AF_ERR_AF_LF_INVALID;
210142c45ac1SNithin Dabilpuram 
210242c45ac1SNithin Dabilpuram 		/* Sync NPA NDC */
210342c45ac1SNithin Dabilpuram 		err = rvu_ndc_sync(rvu, lfblkaddr,
210442c45ac1SNithin Dabilpuram 				   lfidx, NPA_AF_NDC_SYNC);
210542c45ac1SNithin Dabilpuram 		if (err)
210642c45ac1SNithin Dabilpuram 			dev_err(rvu->dev,
210742c45ac1SNithin Dabilpuram 				"NDC-NPA sync failed for LF %u\n", lfidx);
210842c45ac1SNithin Dabilpuram 	}
210942c45ac1SNithin Dabilpuram 
211042c45ac1SNithin Dabilpuram 	if (!req->nix_lf_tx_sync && !req->nix_lf_rx_sync)
211142c45ac1SNithin Dabilpuram 		return 0;
211242c45ac1SNithin Dabilpuram 
211342c45ac1SNithin Dabilpuram 	/* Get NIX LF data */
211442c45ac1SNithin Dabilpuram 	lfblkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
211542c45ac1SNithin Dabilpuram 	if (lfblkaddr < 0)
211642c45ac1SNithin Dabilpuram 		return NIX_AF_ERR_AF_LF_INVALID;
211742c45ac1SNithin Dabilpuram 
211842c45ac1SNithin Dabilpuram 	lfidx = rvu_get_lf(rvu, &hw->block[lfblkaddr], pcifunc, 0);
211942c45ac1SNithin Dabilpuram 	if (lfidx < 0)
212042c45ac1SNithin Dabilpuram 		return NIX_AF_ERR_AF_LF_INVALID;
212142c45ac1SNithin Dabilpuram 
212242c45ac1SNithin Dabilpuram 	if (req->nix_lf_tx_sync) {
212342c45ac1SNithin Dabilpuram 		/* Sync NIX TX NDC */
212442c45ac1SNithin Dabilpuram 		err = rvu_ndc_sync(rvu, lfblkaddr,
212542c45ac1SNithin Dabilpuram 				   lfidx, NIX_AF_NDC_TX_SYNC);
212642c45ac1SNithin Dabilpuram 		if (err)
212742c45ac1SNithin Dabilpuram 			dev_err(rvu->dev,
212842c45ac1SNithin Dabilpuram 				"NDC-NIX-TX sync fail for LF %u\n", lfidx);
212942c45ac1SNithin Dabilpuram 	}
213042c45ac1SNithin Dabilpuram 
213142c45ac1SNithin Dabilpuram 	if (req->nix_lf_rx_sync) {
213242c45ac1SNithin Dabilpuram 		/* Sync NIX RX NDC */
213342c45ac1SNithin Dabilpuram 		err = rvu_ndc_sync(rvu, lfblkaddr,
213442c45ac1SNithin Dabilpuram 				   lfidx, NIX_AF_NDC_RX_SYNC);
213542c45ac1SNithin Dabilpuram 		if (err)
213642c45ac1SNithin Dabilpuram 			dev_err(rvu->dev,
213742c45ac1SNithin Dabilpuram 				"NDC-NIX-RX sync failed for LF %u\n", lfidx);
213842c45ac1SNithin Dabilpuram 	}
213942c45ac1SNithin Dabilpuram 
214042c45ac1SNithin Dabilpuram 	return 0;
214142c45ac1SNithin Dabilpuram }
214242c45ac1SNithin Dabilpuram 
rvu_process_mbox_msg(struct otx2_mbox * mbox,int devid,struct mbox_msghdr * req)21439bdc47a6STomasz Duszynski static int rvu_process_mbox_msg(struct otx2_mbox *mbox, int devid,
21447304ac45SSunil Goutham 				struct mbox_msghdr *req)
21457304ac45SSunil Goutham {
21469bdc47a6STomasz Duszynski 	struct rvu *rvu = pci_get_drvdata(mbox->pdev);
21479bdc47a6STomasz Duszynski 
21487304ac45SSunil Goutham 	/* Check if valid, if not reply with a invalid msg */
21497304ac45SSunil Goutham 	if (req->sig != OTX2_MBOX_REQ_SIG)
21507304ac45SSunil Goutham 		goto bad_message;
21517304ac45SSunil Goutham 
21521f154625SAleksey Makarov 	switch (req->id) {
2153eac66686SSunil Goutham #define M(_name, _id, _fn_name, _req_type, _rsp_type)			\
21541f154625SAleksey Makarov 	case _id: {							\
21551f154625SAleksey Makarov 		struct _rsp_type *rsp;					\
21561f154625SAleksey Makarov 		int err;						\
21571f154625SAleksey Makarov 									\
21581f154625SAleksey Makarov 		rsp = (struct _rsp_type *)otx2_mbox_alloc_msg(		\
21599bdc47a6STomasz Duszynski 			mbox, devid,					\
21601f154625SAleksey Makarov 			sizeof(struct _rsp_type));			\
21619bdc47a6STomasz Duszynski 		/* some handlers should complete even if reply */	\
21629bdc47a6STomasz Duszynski 		/* could not be allocated */				\
21639bdc47a6STomasz Duszynski 		if (!rsp &&						\
21649bdc47a6STomasz Duszynski 		    _id != MBOX_MSG_DETACH_RESOURCES &&			\
21659bdc47a6STomasz Duszynski 		    _id != MBOX_MSG_NIX_TXSCH_FREE &&			\
21669bdc47a6STomasz Duszynski 		    _id != MBOX_MSG_VF_FLR)				\
21679bdc47a6STomasz Duszynski 			return -ENOMEM;					\
21681f154625SAleksey Makarov 		if (rsp) {						\
21691f154625SAleksey Makarov 			rsp->hdr.id = _id;				\
21701f154625SAleksey Makarov 			rsp->hdr.sig = OTX2_MBOX_RSP_SIG;		\
21711f154625SAleksey Makarov 			rsp->hdr.pcifunc = req->pcifunc;		\
21721f154625SAleksey Makarov 			rsp->hdr.rc = 0;				\
21731f154625SAleksey Makarov 		}							\
21741f154625SAleksey Makarov 									\
2175eac66686SSunil Goutham 		err = rvu_mbox_handler_ ## _fn_name(rvu,		\
21761f154625SAleksey Makarov 						    (struct _req_type *)req, \
21771f154625SAleksey Makarov 						    rsp);		\
21781f154625SAleksey Makarov 		if (rsp && err)						\
21791f154625SAleksey Makarov 			rsp->hdr.rc = err;				\
21801f154625SAleksey Makarov 									\
218127d27a06SSubbaraya Sundeep 		trace_otx2_msg_process(mbox->pdev, _id, err, req->pcifunc); \
21821f154625SAleksey Makarov 		return rsp ? err : -ENOMEM;				\
21831f154625SAleksey Makarov 	}
21841f154625SAleksey Makarov MBOX_MESSAGES
21851f154625SAleksey Makarov #undef M
21869bdc47a6STomasz Duszynski 
21877304ac45SSunil Goutham bad_message:
21881f154625SAleksey Makarov 	default:
21899bdc47a6STomasz Duszynski 		otx2_reply_invalid_msg(mbox, devid, req->pcifunc, req->id);
21907304ac45SSunil Goutham 		return -ENODEV;
21917304ac45SSunil Goutham 	}
21921f154625SAleksey Makarov }
21937304ac45SSunil Goutham 
__rvu_mbox_handler(struct rvu_work * mwork,int type,bool poll)2194a88e0f93SSubbaraya Sundeep static void __rvu_mbox_handler(struct rvu_work *mwork, int type, bool poll)
21957304ac45SSunil Goutham {
21967304ac45SSunil Goutham 	struct rvu *rvu = mwork->rvu;
21979bdc47a6STomasz Duszynski 	int offset, err, id, devid;
21987304ac45SSunil Goutham 	struct otx2_mbox_dev *mdev;
21997304ac45SSunil Goutham 	struct mbox_hdr *req_hdr;
22007304ac45SSunil Goutham 	struct mbox_msghdr *msg;
22019bdc47a6STomasz Duszynski 	struct mbox_wq_info *mw;
22027304ac45SSunil Goutham 	struct otx2_mbox *mbox;
22037304ac45SSunil Goutham 
22049bdc47a6STomasz Duszynski 	switch (type) {
22059bdc47a6STomasz Duszynski 	case TYPE_AFPF:
22069bdc47a6STomasz Duszynski 		mw = &rvu->afpf_wq_info;
22079bdc47a6STomasz Duszynski 		break;
22089bdc47a6STomasz Duszynski 	case TYPE_AFVF:
22099bdc47a6STomasz Duszynski 		mw = &rvu->afvf_wq_info;
22109bdc47a6STomasz Duszynski 		break;
22119bdc47a6STomasz Duszynski 	default:
22129bdc47a6STomasz Duszynski 		return;
22139bdc47a6STomasz Duszynski 	}
22149bdc47a6STomasz Duszynski 
22159bdc47a6STomasz Duszynski 	devid = mwork - mw->mbox_wrk;
22169bdc47a6STomasz Duszynski 	mbox = &mw->mbox;
22179bdc47a6STomasz Duszynski 	mdev = &mbox->dev[devid];
22187304ac45SSunil Goutham 
22197304ac45SSunil Goutham 	/* Process received mbox messages */
22207304ac45SSunil Goutham 	req_hdr = mdev->mbase + mbox->rx_start;
2221a36740f6SSunil Goutham 	if (mw->mbox_wrk[devid].num_msgs == 0)
22227304ac45SSunil Goutham 		return;
22237304ac45SSunil Goutham 
22247304ac45SSunil Goutham 	offset = mbox->rx_start + ALIGN(sizeof(*req_hdr), MBOX_MSG_ALIGN);
22257304ac45SSunil Goutham 
2226a36740f6SSunil Goutham 	if (req_hdr->sig && !(is_rvu_otx2(rvu) || is_cn20k(rvu->pdev))) {
22277304ac45SSunil Goutham 		req_hdr->opt_msg = mw->mbox_wrk[devid].num_msgs;
22287304ac45SSunil Goutham 		rvu_write64(rvu, BLKADDR_NIX0, RVU_AF_BAR2_SEL,
22299bdc47a6STomasz Duszynski 			    RVU_AF_BAR2_PFID);
22309bdc47a6STomasz Duszynski 		if (type == TYPE_AFPF)
22319bdc47a6STomasz Duszynski 			rvu_write64(rvu, BLKADDR_NIX0,
22329bdc47a6STomasz Duszynski 				    AF_BAR2_ALIASX(0, NIX_CINTX_INT_W1S(devid)),
22339bdc47a6STomasz Duszynski 				    0x1);
22349bdc47a6STomasz Duszynski 		else
22359bdc47a6STomasz Duszynski 			rvu_write64(rvu, BLKADDR_NIX0,
22369bdc47a6STomasz Duszynski 				    AF_BAR2_ALIASX(0, NIX_QINTX_CNT(devid)),
22379bdc47a6STomasz Duszynski 				    0x1);
22389bdc47a6STomasz Duszynski 		usleep_range(5000, 6000);
22399bdc47a6STomasz Duszynski 		goto done;
22409bdc47a6STomasz Duszynski 	}
22419bdc47a6STomasz Duszynski 
22429bdc47a6STomasz Duszynski 	for (id = 0; id < mw->mbox_wrk[devid].num_msgs; id++) {
22439bdc47a6STomasz Duszynski 		msg = mdev->mbase + offset;
22447304ac45SSunil Goutham 
22457304ac45SSunil Goutham 		/* Set which PF/VF sent this message based on mbox IRQ */
22467304ac45SSunil Goutham 		switch (type) {
22477304ac45SSunil Goutham 		case TYPE_AFPF:
22487304ac45SSunil Goutham 			msg->pcifunc &= rvu_pcifunc_pf_mask(rvu->pdev);
22497304ac45SSunil Goutham 			msg->pcifunc |= rvu_make_pcifunc(rvu->pdev, devid, 0);
22507304ac45SSunil Goutham 			break;
22519bdc47a6STomasz Duszynski 		case TYPE_AFVF:
2252a36740f6SSunil Goutham 			msg->pcifunc &=
22537304ac45SSunil Goutham 				~(RVU_PFVF_FUNC_MASK << RVU_PFVF_FUNC_SHIFT);
22547304ac45SSunil Goutham 			msg->pcifunc |= (devid << RVU_PFVF_FUNC_SHIFT) + 1;
22557304ac45SSunil Goutham 			break;
22569bdc47a6STomasz Duszynski 		}
22579bdc47a6STomasz Duszynski 
22587304ac45SSunil Goutham 		err = rvu_process_mbox_msg(mbox, devid, msg);
2259a36740f6SSunil Goutham 		if (!err) {
22607304ac45SSunil Goutham 			offset = mbox->rx_start + msg->next_msgoff;
2261a88e0f93SSubbaraya Sundeep 			continue;
2262a88e0f93SSubbaraya Sundeep 		}
2263a88e0f93SSubbaraya Sundeep 
22649bdc47a6STomasz Duszynski 		if (msg->pcifunc & RVU_PFVF_FUNC_MASK)
22659bdc47a6STomasz Duszynski 			dev_warn(rvu->dev, "Error %d when processing message %s (0x%x) from PF%d:VF%d\n",
22667304ac45SSunil Goutham 				 err, otx2_mbox_id2name(msg->id),
22677304ac45SSunil Goutham 				 msg->id, rvu_get_pf(rvu->pdev, msg->pcifunc),
22689bdc47a6STomasz Duszynski 				 (msg->pcifunc & RVU_PFVF_FUNC_MASK) - 1);
226961071a87SLinu Cherian 		else
227061071a87SLinu Cherian 			dev_warn(rvu->dev, "Error %d when processing message %s (0x%x) from PF%d\n",
2271a88e0f93SSubbaraya Sundeep 				 err, otx2_mbox_id2name(msg->id),
22729bdc47a6STomasz Duszynski 				 msg->id, devid);
2273a88e0f93SSubbaraya Sundeep 	}
2274a88e0f93SSubbaraya Sundeep done:
2275a88e0f93SSubbaraya Sundeep 	mw->mbox_wrk[devid].num_msgs = 0;
22769bdc47a6STomasz Duszynski 
22779bdc47a6STomasz Duszynski 	if (!is_cn20k(mbox->pdev) && poll)
22789bdc47a6STomasz Duszynski 		otx2_mbox_wait_for_zero(mbox, devid);
22799bdc47a6STomasz Duszynski 
22809bdc47a6STomasz Duszynski 	/* Send mbox responses to VF/PF */
22819bdc47a6STomasz Duszynski 	otx2_mbox_msg_send(mbox, devid);
2282a88e0f93SSubbaraya Sundeep }
22839bdc47a6STomasz Duszynski 
rvu_afpf_mbox_handler(struct work_struct * work)22849bdc47a6STomasz Duszynski static inline void rvu_afpf_mbox_handler(struct work_struct *work)
22859bdc47a6STomasz Duszynski {
22869bdc47a6STomasz Duszynski 	struct rvu_work *mwork = container_of(work, struct rvu_work, work);
228761071a87SLinu Cherian 	struct rvu *rvu = mwork->rvu;
228861071a87SLinu Cherian 
228961071a87SLinu Cherian 	mutex_lock(&rvu->mbox_lock);
229061071a87SLinu Cherian 	__rvu_mbox_handler(mwork, TYPE_AFPF, true);
22919bdc47a6STomasz Duszynski 	mutex_unlock(&rvu->mbox_lock);
229261071a87SLinu Cherian }
22939bdc47a6STomasz Duszynski 
rvu_afvf_mbox_handler(struct work_struct * work)229461071a87SLinu Cherian static inline void rvu_afvf_mbox_handler(struct work_struct *work)
22959bdc47a6STomasz Duszynski {
22969bdc47a6STomasz Duszynski 	struct rvu_work *mwork = container_of(work, struct rvu_work, work);
22979bdc47a6STomasz Duszynski 
22989bdc47a6STomasz Duszynski 	__rvu_mbox_handler(mwork, TYPE_AFVF, false);
22999bdc47a6STomasz Duszynski }
23009bdc47a6STomasz Duszynski 
__rvu_mbox_up_handler(struct rvu_work * mwork,int type)23019bdc47a6STomasz Duszynski static void __rvu_mbox_up_handler(struct rvu_work *mwork, int type)
23029bdc47a6STomasz Duszynski {
23039bdc47a6STomasz Duszynski 	struct rvu *rvu = mwork->rvu;
23049bdc47a6STomasz Duszynski 	struct otx2_mbox_dev *mdev;
23059bdc47a6STomasz Duszynski 	struct mbox_hdr *rsp_hdr;
23069bdc47a6STomasz Duszynski 	struct mbox_msghdr *msg;
23079bdc47a6STomasz Duszynski 	struct mbox_wq_info *mw;
23089bdc47a6STomasz Duszynski 	struct otx2_mbox *mbox;
230961071a87SLinu Cherian 	int offset, id, devid;
231061071a87SLinu Cherian 
2311a36740f6SSunil Goutham 	switch (type) {
231261071a87SLinu Cherian 	case TYPE_AFPF:
231361071a87SLinu Cherian 		mw = &rvu->afpf_wq_info;
231461071a87SLinu Cherian 		break;
231561071a87SLinu Cherian 	case TYPE_AFVF:
231661071a87SLinu Cherian 		mw = &rvu->afvf_wq_info;
231761071a87SLinu Cherian 		break;
2318a36740f6SSunil Goutham 	default:
231961071a87SLinu Cherian 		return;
232061071a87SLinu Cherian 	}
232161071a87SLinu Cherian 
232261071a87SLinu Cherian 	devid = mwork - mw->mbox_wrk_up;
232361071a87SLinu Cherian 	mbox = &mw->mbox_up;
232461071a87SLinu Cherian 	mdev = &mbox->dev[devid];
232561071a87SLinu Cherian 
232661071a87SLinu Cherian 	rsp_hdr = mdev->mbase + mbox->rx_start;
232761071a87SLinu Cherian 	if (mw->mbox_wrk_up[devid].up_num_msgs == 0) {
232861071a87SLinu Cherian 		dev_warn(rvu->dev, "mbox up handler: num_msgs = 0\n");
232961071a87SLinu Cherian 		return;
233061071a87SLinu Cherian 	}
233161071a87SLinu Cherian 
233261071a87SLinu Cherian 	offset = mbox->rx_start + ALIGN(sizeof(*rsp_hdr), MBOX_MSG_ALIGN);
233361071a87SLinu Cherian 
233461071a87SLinu Cherian 	for (id = 0; id < mw->mbox_wrk_up[devid].up_num_msgs; id++) {
233561071a87SLinu Cherian 		msg = mdev->mbase + offset;
233661071a87SLinu Cherian 
233761071a87SLinu Cherian 		if (msg->id >= MBOX_MSG_MAX) {
233861071a87SLinu Cherian 			dev_err(rvu->dev,
233961071a87SLinu Cherian 				"Mbox msg with unknown ID 0x%x\n", msg->id);
234061071a87SLinu Cherian 			goto end;
234161071a87SLinu Cherian 		}
234261071a87SLinu Cherian 
234361071a87SLinu Cherian 		if (msg->sig != OTX2_MBOX_RSP_SIG) {
234461071a87SLinu Cherian 			dev_err(rvu->dev,
234561071a87SLinu Cherian 				"Mbox msg with wrong signature %x, ID 0x%x\n",
234661071a87SLinu Cherian 				msg->sig, msg->id);
234761071a87SLinu Cherian 			goto end;
2348a36740f6SSunil Goutham 		}
234961071a87SLinu Cherian 
23509bdc47a6STomasz Duszynski 		switch (msg->id) {
235161071a87SLinu Cherian 		case MBOX_MSG_CGX_LINK_EVENT:
235261071a87SLinu Cherian 			break;
23539bdc47a6STomasz Duszynski 		default:
23547304ac45SSunil Goutham 			if (msg->rc)
23559bdc47a6STomasz Duszynski 				dev_err(rvu->dev,
23567304ac45SSunil Goutham 					"Mbox msg response has err %d, ID 0x%x\n",
23579bdc47a6STomasz Duszynski 					msg->rc, msg->id);
23589bdc47a6STomasz Duszynski 			break;
23599bdc47a6STomasz Duszynski 		}
23609bdc47a6STomasz Duszynski end:
23619bdc47a6STomasz Duszynski 		offset = mbox->rx_start + msg->next_msgoff;
23629bdc47a6STomasz Duszynski 		mdev->msgs_acked++;
23639bdc47a6STomasz Duszynski 	}
23649bdc47a6STomasz Duszynski 	mw->mbox_wrk_up[devid].up_num_msgs = 0;
23659bdc47a6STomasz Duszynski 
23669bdc47a6STomasz Duszynski 	otx2_mbox_reset(mbox, devid);
236798c56111SSubbaraya Sundeep }
23685eb1b722SRatheesh Kannoth 
rvu_afpf_mbox_up_handler(struct work_struct * work)236998c56111SSubbaraya Sundeep static inline void rvu_afpf_mbox_up_handler(struct work_struct *work)
237098c56111SSubbaraya Sundeep {
237198c56111SSubbaraya Sundeep 	struct rvu_work *mwork = container_of(work, struct rvu_work, work);
237298c56111SSubbaraya Sundeep 
237398c56111SSubbaraya Sundeep 	__rvu_mbox_up_handler(mwork, TYPE_AFPF);
237498c56111SSubbaraya Sundeep }
237598c56111SSubbaraya Sundeep 
rvu_afvf_mbox_up_handler(struct work_struct * work)237698c56111SSubbaraya Sundeep static inline void rvu_afvf_mbox_up_handler(struct work_struct *work)
237798c56111SSubbaraya Sundeep {
237898c56111SSubbaraya Sundeep 	struct rvu_work *mwork = container_of(work, struct rvu_work, work);
237998c56111SSubbaraya Sundeep 
23805eb1b722SRatheesh Kannoth 	__rvu_mbox_up_handler(mwork, TYPE_AFVF);
23815eb1b722SRatheesh Kannoth }
23825eb1b722SRatheesh Kannoth 
rvu_get_mbox_regions(struct rvu * rvu,void __iomem ** mbox_addr,int num,int type,unsigned long * pf_bmap)238398c56111SSubbaraya Sundeep static int rvu_get_mbox_regions(struct rvu *rvu, void __iomem **mbox_addr,
238498c56111SSubbaraya Sundeep 				int num, int type, unsigned long *pf_bmap)
238598c56111SSubbaraya Sundeep {
238698c56111SSubbaraya Sundeep 	struct rvu_hwinfo *hw = rvu->hw;
238798c56111SSubbaraya Sundeep 	int region;
238898c56111SSubbaraya Sundeep 	u64 bar4;
238998c56111SSubbaraya Sundeep 
239098c56111SSubbaraya Sundeep 	/* For cn20k platform AF mailbox region is allocated by software
239198c56111SSubbaraya Sundeep 	 * and the corresponding IOVA is programmed in hardware unlike earlier
239298c56111SSubbaraya Sundeep 	 * silicons where software uses the hardware region after ioremap.
239398c56111SSubbaraya Sundeep 	 */
239498c56111SSubbaraya Sundeep 	if (is_cn20k(rvu->pdev))
239598c56111SSubbaraya Sundeep 		return cn20k_rvu_get_mbox_regions(rvu, (void *)mbox_addr,
239698c56111SSubbaraya Sundeep 						  num, type, pf_bmap);
239798c56111SSubbaraya Sundeep 
239898c56111SSubbaraya Sundeep 	/* For cn10k platform VF mailbox regions of a PF follows after the
239998c56111SSubbaraya Sundeep 	 * PF <-> AF mailbox region. Whereas for Octeontx2 it is read from
240098c56111SSubbaraya Sundeep 	 * RVU_PF_VF_BAR4_ADDR register.
240198c56111SSubbaraya Sundeep 	 */
240298c56111SSubbaraya Sundeep 	if (type == TYPE_AFVF) {
240398c56111SSubbaraya Sundeep 		for (region = 0; region < num; region++) {
24045eb1b722SRatheesh Kannoth 			if (!test_bit(region, pf_bmap))
24055eb1b722SRatheesh Kannoth 				continue;
24065eb1b722SRatheesh Kannoth 
240798c56111SSubbaraya Sundeep 			if (hw->cap.per_pf_mbox_regs) {
240898c56111SSubbaraya Sundeep 				bar4 = rvu_read64(rvu, BLKADDR_RVUM,
240998c56111SSubbaraya Sundeep 						  RVU_AF_PFX_BAR4_ADDR(0)) +
241098c56111SSubbaraya Sundeep 						  MBOX_SIZE;
241198c56111SSubbaraya Sundeep 				bar4 += region * MBOX_SIZE;
241298c56111SSubbaraya Sundeep 			} else {
241398c56111SSubbaraya Sundeep 				bar4 = rvupf_read64(rvu, RVU_PF_VF_BAR4_ADDR);
241498c56111SSubbaraya Sundeep 				bar4 += region * MBOX_SIZE;
241598c56111SSubbaraya Sundeep 			}
241698c56111SSubbaraya Sundeep 			mbox_addr[region] = ioremap_wc(bar4, MBOX_SIZE);
241798c56111SSubbaraya Sundeep 			if (!mbox_addr[region])
241898c56111SSubbaraya Sundeep 				goto error;
241998c56111SSubbaraya Sundeep 		}
242098c56111SSubbaraya Sundeep 		return 0;
242198c56111SSubbaraya Sundeep 	}
242298c56111SSubbaraya Sundeep 
242398c56111SSubbaraya Sundeep 	/* For cn10k platform AF <-> PF mailbox region of a PF is read from per
242498c56111SSubbaraya Sundeep 	 * PF registers. Whereas for Octeontx2 it is read from
242598c56111SSubbaraya Sundeep 	 * RVU_AF_PF_BAR4_ADDR register.
242698c56111SSubbaraya Sundeep 	 */
24279bdc47a6STomasz Duszynski 	for (region = 0; region < num; region++) {
24289bdc47a6STomasz Duszynski 		if (!test_bit(region, pf_bmap))
24299bdc47a6STomasz Duszynski 			continue;
24309bdc47a6STomasz Duszynski 
24319bdc47a6STomasz Duszynski 		if (hw->cap.per_pf_mbox_regs) {
243298c56111SSubbaraya Sundeep 			bar4 = rvu_read64(rvu, BLKADDR_RVUM,
243398c56111SSubbaraya Sundeep 					  RVU_AF_PFX_BAR4_ADDR(region));
24349bdc47a6STomasz Duszynski 		} else {
24355eb1b722SRatheesh Kannoth 			bar4 = rvu_read64(rvu, BLKADDR_RVUM,
243698c56111SSubbaraya Sundeep 					  RVU_AF_PF_BAR4_ADDR);
24379bdc47a6STomasz Duszynski 			bar4 += region * MBOX_SIZE;
24385eb1b722SRatheesh Kannoth 		}
24395eb1b722SRatheesh Kannoth 		mbox_addr[region] = ioremap_wc(bar4, MBOX_SIZE);
24405eb1b722SRatheesh Kannoth 		if (!mbox_addr[region])
24415eb1b722SRatheesh Kannoth 			goto error;
24425eb1b722SRatheesh Kannoth 	}
24435eb1b722SRatheesh Kannoth 	return 0;
24445eb1b722SRatheesh Kannoth 
24455eb1b722SRatheesh Kannoth error:
24465eb1b722SRatheesh Kannoth 	while (region--)
24475eb1b722SRatheesh Kannoth 		iounmap(mbox_addr[region]);
24485eb1b722SRatheesh Kannoth 	return -ENOMEM;
24495eb1b722SRatheesh Kannoth }
24505eb1b722SRatheesh Kannoth 
24515eb1b722SRatheesh Kannoth static struct mbox_ops rvu_mbox_ops = {
24525eb1b722SRatheesh Kannoth 	.pf_intr_handler = rvu_mbox_pf_intr_handler,
24535eb1b722SRatheesh Kannoth 	.afvf_intr_handler = rvu_mbox_intr_handler,
24545eb1b722SRatheesh Kannoth };
24555eb1b722SRatheesh Kannoth 
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 *))245698c56111SSubbaraya Sundeep static int rvu_mbox_init(struct rvu *rvu, struct mbox_wq_info *mw,
2457a88e0f93SSubbaraya Sundeep 			 int type, int num,
2458a88e0f93SSubbaraya Sundeep 			 void (mbox_handler)(struct work_struct *),
245998c56111SSubbaraya Sundeep 			 void (mbox_up_handler)(struct work_struct *))
24605eb1b722SRatheesh Kannoth {
24615eb1b722SRatheesh Kannoth 	void __iomem **mbox_regions;
24625eb1b722SRatheesh Kannoth 	struct ng_rvu *ng_rvu_mbox;
24635eb1b722SRatheesh Kannoth 	int err, i, dir, dir_up;
24649bdc47a6STomasz Duszynski 	void __iomem *reg_base;
24659bdc47a6STomasz Duszynski 	struct rvu_work *mwork;
24669bdc47a6STomasz Duszynski 	unsigned long *pf_bmap;
24679bdc47a6STomasz Duszynski 	const char *name;
24689bdc47a6STomasz Duszynski 	u64 cfg;
24699bdc47a6STomasz Duszynski 
24709bdc47a6STomasz Duszynski 	pf_bmap = bitmap_zalloc(num, GFP_KERNEL);
24715eb1b722SRatheesh Kannoth 	if (!pf_bmap)
247298c56111SSubbaraya Sundeep 		return -ENOMEM;
247398c56111SSubbaraya Sundeep 
24749bdc47a6STomasz Duszynski 	ng_rvu_mbox = kzalloc(sizeof(*ng_rvu_mbox), GFP_KERNEL);
24759bdc47a6STomasz Duszynski 	if (!ng_rvu_mbox) {
24769bdc47a6STomasz Duszynski 		err = -ENOMEM;
24779bdc47a6STomasz Duszynski 		goto free_bitmap;
24789bdc47a6STomasz Duszynski 	}
24799bdc47a6STomasz Duszynski 
24805eb1b722SRatheesh Kannoth 	/* RVU VFs */
248198c56111SSubbaraya Sundeep 	if (type == TYPE_AFVF)
248298c56111SSubbaraya Sundeep 		bitmap_set(pf_bmap, 0, num);
24839bdc47a6STomasz Duszynski 
24849bdc47a6STomasz Duszynski 	if (type == TYPE_AFPF) {
2485e07a097bSZhou Qingyang 		/* Mark enabled PFs in bitmap */
24869bdc47a6STomasz Duszynski 		for (i = 0; i < num; i++) {
24879bdc47a6STomasz Duszynski 			cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_PFX_CFG(i));
248892218f10SSimon Horman 			if (cfg & BIT_ULL(20))
24897304ac45SSunil Goutham 				set_bit(i, pf_bmap);
249092218f10SSimon Horman 		}
249198c56111SSubbaraya Sundeep 	}
249298c56111SSubbaraya Sundeep 
249398c56111SSubbaraya Sundeep 	rvu->ng_rvu = ng_rvu_mbox;
249498c56111SSubbaraya Sundeep 
24957304ac45SSunil Goutham 	rvu->ng_rvu->rvu_mbox_ops = &rvu_mbox_ops;
24969bdc47a6STomasz Duszynski 
24977304ac45SSunil Goutham 	err = cn20k_rvu_mbox_init(rvu, type, num);
24989bdc47a6STomasz Duszynski 	if (err)
24997304ac45SSunil Goutham 		goto free_mem;
25007304ac45SSunil Goutham 
25017304ac45SSunil Goutham 	mutex_init(&rvu->mbox_lock);
25027304ac45SSunil Goutham 
25039bdc47a6STomasz Duszynski 	mbox_regions = kcalloc(num, sizeof(void __iomem *), GFP_KERNEL);
250461071a87SLinu Cherian 	if (!mbox_regions) {
25059bdc47a6STomasz Duszynski 		err = -ENOMEM;
250661071a87SLinu Cherian 		goto free_qmem;
250761071a87SLinu Cherian 	}
250861071a87SLinu Cherian 
250961071a87SLinu Cherian 	switch (type) {
251098c56111SSubbaraya Sundeep 	case TYPE_AFPF:
25115eb1b722SRatheesh Kannoth 		name = "rvu_afpf_mailbox";
25127304ac45SSunil Goutham 		dir = MBOX_DIR_AFPF;
25137304ac45SSunil Goutham 		dir_up = MBOX_DIR_AFPF_UP;
25147304ac45SSunil Goutham 		reg_base = rvu->afreg_base;
251598c56111SSubbaraya Sundeep 		err = rvu_get_mbox_regions(rvu, mbox_regions, num, TYPE_AFPF, pf_bmap);
25165eb1b722SRatheesh Kannoth 		if (err)
251761071a87SLinu Cherian 			goto free_regions;
251861071a87SLinu Cherian 		break;
251961071a87SLinu Cherian 	case TYPE_AFVF:
25209bdc47a6STomasz Duszynski 		name = "rvu_afvf_mailbox";
25215eb1b722SRatheesh Kannoth 		dir = MBOX_DIR_PFVF;
25225eb1b722SRatheesh Kannoth 		dir_up = MBOX_DIR_PFVF_UP;
25235eb1b722SRatheesh Kannoth 		reg_base = rvu->pfreg_base;
25249bdc47a6STomasz Duszynski 		err = rvu_get_mbox_regions(rvu, mbox_regions, num, TYPE_AFVF, pf_bmap);
25257304ac45SSunil Goutham 		if (err)
25269bdc47a6STomasz Duszynski 			goto free_regions;
25277304ac45SSunil Goutham 		break;
25289bdc47a6STomasz Duszynski 	default:
252961071a87SLinu Cherian 		err = -EINVAL;
25309bdc47a6STomasz Duszynski 		goto free_regions;
253161071a87SLinu Cherian 	}
25325eb1b722SRatheesh Kannoth 
253398c56111SSubbaraya Sundeep 	mw->mbox_wq = alloc_workqueue("%s",
25347304ac45SSunil Goutham 				      WQ_HIGHPRI | WQ_MEM_RECLAIM,
25359bdc47a6STomasz Duszynski 				      num, name);
253698c56111SSubbaraya Sundeep 	if (!mw->mbox_wq) {
253798c56111SSubbaraya Sundeep 		err = -ENOMEM;
253898c56111SSubbaraya Sundeep 		goto unmap_regions;
253998c56111SSubbaraya Sundeep 	}
254098c56111SSubbaraya Sundeep 
25415eb1b722SRatheesh Kannoth 	mw->mbox_wrk = devm_kcalloc(rvu->dev, num,
25425eb1b722SRatheesh Kannoth 				    sizeof(struct rvu_work), GFP_KERNEL);
25437304ac45SSunil Goutham 	if (!mw->mbox_wrk) {
25447304ac45SSunil Goutham 		err = -ENOMEM;
25457304ac45SSunil Goutham 		goto exit;
25469bdc47a6STomasz Duszynski 	}
25477304ac45SSunil Goutham 
254898c56111SSubbaraya Sundeep 	mw->mbox_wrk_up = devm_kcalloc(rvu->dev, num,
254998c56111SSubbaraya Sundeep 				       sizeof(struct rvu_work), GFP_KERNEL);
255098c56111SSubbaraya Sundeep 	if (!mw->mbox_wrk_up) {
255198c56111SSubbaraya Sundeep 		err = -ENOMEM;
25529bdc47a6STomasz Duszynski 		goto exit;
25539bdc47a6STomasz Duszynski 	}
25549bdc47a6STomasz Duszynski 
25557304ac45SSunil Goutham 	err = otx2_mbox_regions_init(&mw->mbox, mbox_regions, rvu->pdev,
25567304ac45SSunil Goutham 				     reg_base, dir, num, pf_bmap);
255798c56111SSubbaraya Sundeep 	if (err)
255898c56111SSubbaraya Sundeep 		goto exit;
255998c56111SSubbaraya Sundeep 
256098c56111SSubbaraya Sundeep 	err = otx2_mbox_regions_init(&mw->mbox_up, mbox_regions, rvu->pdev,
256198c56111SSubbaraya Sundeep 				     reg_base, dir_up, num, pf_bmap);
25627304ac45SSunil Goutham 	if (err)
25639bdc47a6STomasz Duszynski 		goto exit;
25649bdc47a6STomasz Duszynski 
25659bdc47a6STomasz Duszynski 	for (i = 0; i < num; i++) {
25669bdc47a6STomasz Duszynski 		if (!test_bit(i, pf_bmap))
25679bdc47a6STomasz Duszynski 			continue;
25689bdc47a6STomasz Duszynski 
25699bdc47a6STomasz Duszynski 		mwork = &mw->mbox_wrk[i];
25709bdc47a6STomasz Duszynski 		mwork->rvu = rvu;
25719bdc47a6STomasz Duszynski 		INIT_WORK(&mwork->work, mbox_handler);
25729bdc47a6STomasz Duszynski 
25739bdc47a6STomasz Duszynski 		mwork = &mw->mbox_wrk_up[i];
25749bdc47a6STomasz Duszynski 		mwork->rvu = rvu;
25759bdc47a6STomasz Duszynski 		INIT_WORK(&mwork->work, mbox_up_handler);
25769bdc47a6STomasz Duszynski 	}
25779bdc47a6STomasz Duszynski 
25789bdc47a6STomasz Duszynski 	kfree(mbox_regions);
25799bdc47a6STomasz Duszynski 	bitmap_free(pf_bmap);
25809bdc47a6STomasz Duszynski 
25819bdc47a6STomasz Duszynski 	return 0;
25829bdc47a6STomasz Duszynski 
25839bdc47a6STomasz Duszynski exit:
2584a36740f6SSunil Goutham 	destroy_workqueue(mw->mbox_wq);
2585a36740f6SSunil Goutham unmap_regions:
2586a36740f6SSunil Goutham 	while (num--)
2587a36740f6SSunil Goutham 		iounmap((void __iomem *)mbox_regions[num]);
2588a36740f6SSunil Goutham free_regions:
2589a36740f6SSunil Goutham 	kfree(mbox_regions);
2590a36740f6SSunil Goutham free_qmem:
2591a36740f6SSunil Goutham 	cn20k_free_mbox_memory(rvu);
2592a36740f6SSunil Goutham free_mem:
2593a36740f6SSunil Goutham 	kfree(rvu->ng_rvu);
2594a36740f6SSunil Goutham free_bitmap:
2595a36740f6SSunil Goutham 	bitmap_free(pf_bmap);
2596a36740f6SSunil Goutham 	return err;
25979bdc47a6STomasz Duszynski }
25989bdc47a6STomasz Duszynski 
rvu_mbox_destroy(struct mbox_wq_info * mw)25999bdc47a6STomasz Duszynski static void rvu_mbox_destroy(struct mbox_wq_info *mw)
2600a36740f6SSunil Goutham {
2601a36740f6SSunil Goutham 	struct otx2_mbox *mbox = &mw->mbox;
2602a36740f6SSunil Goutham 	struct otx2_mbox_dev *mdev;
26039bdc47a6STomasz Duszynski 	int devid;
26049bdc47a6STomasz Duszynski 
26057304ac45SSunil Goutham 	if (mw->mbox_wq) {
2606a36740f6SSunil Goutham 		destroy_workqueue(mw->mbox_wq);
26077304ac45SSunil Goutham 		mw->mbox_wq = NULL;
260850e60de3SSubbaraya Sundeep 	}
26097304ac45SSunil Goutham 
26107304ac45SSunil Goutham 	for (devid = 0; devid < mbox->ndevs; devid++) {
26117304ac45SSunil Goutham 		mdev = &mbox->dev[devid];
26127304ac45SSunil Goutham 		if (mdev->hwbase)
26137304ac45SSunil Goutham 			iounmap((void __iomem *)mdev->hwbase);
26147304ac45SSunil Goutham 	}
26157304ac45SSunil Goutham 
261649142d12SSubbaraya Sundeep 	otx2_mbox_destroy(&mw->mbox);
261749142d12SSubbaraya Sundeep 	otx2_mbox_destroy(&mw->mbox_up);
26187304ac45SSunil Goutham }
26197304ac45SSunil Goutham 
rvu_queue_work(struct mbox_wq_info * mw,int first,int mdevs,u64 intr)26209bdc47a6STomasz Duszynski void rvu_queue_work(struct mbox_wq_info *mw, int first,
26217304ac45SSunil Goutham 		    int mdevs, u64 intr)
26229bdc47a6STomasz Duszynski {
26239bdc47a6STomasz Duszynski 	struct otx2_mbox_dev *mdev;
262450e60de3SSubbaraya Sundeep 	struct otx2_mbox *mbox;
262550e60de3SSubbaraya Sundeep 	struct mbox_hdr *hdr;
262650e60de3SSubbaraya Sundeep 	int i;
262750e60de3SSubbaraya Sundeep 
262850e60de3SSubbaraya Sundeep 	for (i = first; i < mdevs; i++) {
262950e60de3SSubbaraya Sundeep 		/* start from 0 */
263050e60de3SSubbaraya Sundeep 		if (!(intr & BIT_ULL(i - first)))
263150e60de3SSubbaraya Sundeep 			continue;
263250e60de3SSubbaraya Sundeep 
263350e60de3SSubbaraya Sundeep 		mbox = &mw->mbox;
263450e60de3SSubbaraya Sundeep 		mdev = &mbox->dev[i];
263550e60de3SSubbaraya Sundeep 		hdr = mdev->mbase + mbox->rx_start;
26369bdc47a6STomasz Duszynski 
26379bdc47a6STomasz Duszynski 		/*The hdr->num_msgs is set to zero immediately in the interrupt
26389bdc47a6STomasz Duszynski 		 * handler to  ensure that it holds a correct value next time
26399bdc47a6STomasz Duszynski 		 * when the interrupt handler is called.
26409bdc47a6STomasz Duszynski 		 * pf->mbox.num_msgs holds the data for use in pfaf_mbox_handler
26419bdc47a6STomasz Duszynski 		 * pf>mbox.up_num_msgs holds the data for use in
26420fdba88aSGeetha sowjanya 		 * pfaf_mbox_up_handler.
26437304ac45SSunil Goutham 		 */
26449bdc47a6STomasz Duszynski 
26459bdc47a6STomasz Duszynski 		if (hdr->num_msgs) {
26469bdc47a6STomasz Duszynski 			mw->mbox_wrk[i].num_msgs = hdr->num_msgs;
264749142d12SSubbaraya Sundeep 			hdr->num_msgs = 0;
264849142d12SSubbaraya Sundeep 			queue_work(mw->mbox_wq, &mw->mbox_wrk[i].work);
26499bdc47a6STomasz Duszynski 		}
26509bdc47a6STomasz Duszynski 		mbox = &mw->mbox_up;
26517304ac45SSunil Goutham 		mdev = &mbox->dev[i];
26527304ac45SSunil Goutham 		hdr = mdev->mbase + mbox->rx_start;
26537304ac45SSunil Goutham 		if (hdr->num_msgs) {
26547304ac45SSunil Goutham 			mw->mbox_wrk_up[i].up_num_msgs = hdr->num_msgs;
26557304ac45SSunil Goutham 			hdr->num_msgs = 0;
26567304ac45SSunil Goutham 			queue_work(mw->mbox_wq, &mw->mbox_wrk_up[i].work);
26577304ac45SSunil Goutham 		}
26587304ac45SSunil Goutham 	}
26597304ac45SSunil Goutham }
26607304ac45SSunil Goutham 
rvu_mbox_pf_intr_handler(int irq,void * rvu_irq)26617304ac45SSunil Goutham static irqreturn_t rvu_mbox_pf_intr_handler(int irq, void *rvu_irq)
26627304ac45SSunil Goutham {
26637304ac45SSunil Goutham 	struct rvu *rvu = (struct rvu *)rvu_irq;
26647304ac45SSunil Goutham 	u64 intr;
26657304ac45SSunil Goutham 
26667304ac45SSunil Goutham 	intr = rvu_read64(rvu, BLKADDR_RVUM, RVU_AF_PFAF_MBOX_INT);
26677304ac45SSunil Goutham 	/* Clear interrupts */
2668c554f9c1SGeetha sowjanya 	rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFAF_MBOX_INT, intr);
2669c554f9c1SGeetha sowjanya 	if (intr)
2670c554f9c1SGeetha sowjanya 		trace_otx2_msg_interrupt(rvu->pdev, "PF(s) to AF", intr);
2671c554f9c1SGeetha sowjanya 
2672c554f9c1SGeetha sowjanya 	/* Sync with mbox memory region */
2673c554f9c1SGeetha sowjanya 	rmb();
2674c554f9c1SGeetha sowjanya 
2675c554f9c1SGeetha sowjanya 	rvu_queue_work(&rvu->afpf_wq_info, 0, rvu->hw->total_pfs, intr);
2676cdd41e87SSubbaraya Sundeep 
2677c554f9c1SGeetha sowjanya 	return IRQ_HANDLED;
2678c554f9c1SGeetha sowjanya }
2679c554f9c1SGeetha sowjanya 
rvu_mbox_intr_handler(int irq,void * rvu_irq)2680c554f9c1SGeetha sowjanya static irqreturn_t rvu_mbox_intr_handler(int irq, void *rvu_irq)
2681c554f9c1SGeetha sowjanya {
2682c554f9c1SGeetha sowjanya 	struct rvu *rvu = (struct rvu *)rvu_irq;
2683c554f9c1SGeetha sowjanya 	int vfs = rvu->vfs;
2684c554f9c1SGeetha sowjanya 	u64 intr;
26859932fb72SRakesh Babu 
2686c554f9c1SGeetha sowjanya 	/* Sync with mbox memory region */
2687c554f9c1SGeetha sowjanya 	rmb();
2688c554f9c1SGeetha sowjanya 
2689c57c58fdSSrujana Challa 	/* Handle VF interrupts */
2690c57c58fdSSrujana Challa 	if (vfs > 64) {
26917054d39cSNithin Dabilpuram 		intr = rvupf_read64(rvu, RVU_PF_VFPF_MBOX_INTX(1));
26927054d39cSNithin Dabilpuram 		rvupf_write64(rvu, RVU_PF_VFPF_MBOX_INTX(1), intr);
2693c554f9c1SGeetha sowjanya 
2694c554f9c1SGeetha sowjanya 		rvu_queue_work(&rvu->afvf_wq_info, 64, vfs, intr);
2695c554f9c1SGeetha sowjanya 		vfs = 64;
2696c554f9c1SGeetha sowjanya 	}
2697c554f9c1SGeetha sowjanya 
2698c554f9c1SGeetha sowjanya 	intr = rvupf_read64(rvu, RVU_PF_VFPF_MBOX_INTX(0));
2699c554f9c1SGeetha sowjanya 	rvupf_write64(rvu, RVU_PF_VFPF_MBOX_INTX(0), intr);
2700c554f9c1SGeetha sowjanya 	if (intr)
2701c554f9c1SGeetha sowjanya 		trace_otx2_msg_interrupt(rvu->pdev, "VF(s) to AF", intr);
2702c554f9c1SGeetha sowjanya 
2703c554f9c1SGeetha sowjanya 	rvu_queue_work(&rvu->afvf_wq_info, 0, vfs, intr);
2704bab9eed5SRatheesh Kannoth 
2705bab9eed5SRatheesh Kannoth 	return IRQ_HANDLED;
2706bab9eed5SRatheesh Kannoth }
2707c554f9c1SGeetha sowjanya 
rvu_enable_mbox_intr(struct rvu * rvu)2708c554f9c1SGeetha sowjanya static void rvu_enable_mbox_intr(struct rvu *rvu)
2709c554f9c1SGeetha sowjanya {
2710c554f9c1SGeetha sowjanya 	struct rvu_hwinfo *hw = rvu->hw;
2711c554f9c1SGeetha sowjanya 
2712c554f9c1SGeetha sowjanya 	if (is_cn20k(rvu->pdev)) {
271351b2804cSSuman Ghosh 		cn20k_rvu_enable_mbox_intr(rvu);
2714d6212d2eSGeetha sowjanya 		return;
2715d6212d2eSGeetha sowjanya 	}
2716d6212d2eSGeetha sowjanya 
271751b2804cSSuman Ghosh 	/* Clear spurious irqs, if any */
271851b2804cSSuman Ghosh 	rvu_write64(rvu, BLKADDR_RVUM,
271951b2804cSSuman Ghosh 		    RVU_AF_PFAF_MBOX_INT, INTR_MASK(hw->total_pfs));
2720c554f9c1SGeetha sowjanya 
27219932fb72SRakesh Babu 	/* Enable mailbox interrupt for all PFs except PF0 i.e AF itself */
2722c554f9c1SGeetha sowjanya 	rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFAF_MBOX_INT_ENA_W1S,
27239932fb72SRakesh Babu 		    INTR_MASK(hw->total_pfs) & ~1ULL);
2724c554f9c1SGeetha sowjanya }
2725c554f9c1SGeetha sowjanya 
rvu_blklf_teardown(struct rvu * rvu,u16 pcifunc,u8 blkaddr)2726c554f9c1SGeetha sowjanya static void rvu_blklf_teardown(struct rvu *rvu, u16 pcifunc, u8 blkaddr)
2727c554f9c1SGeetha sowjanya {
2728873a1e3dSHarman Kalra 	struct rvu_block *block;
2729c554f9c1SGeetha sowjanya 	int slot, lf, num_lfs;
27303f8fe40aSSubbaraya Sundeep 	int err;
27313f8fe40aSSubbaraya Sundeep 
27323f8fe40aSSubbaraya Sundeep 	block = &rvu->hw->block[blkaddr];
27333f8fe40aSSubbaraya Sundeep 	num_lfs = rvu_get_rsrc_mapcount(rvu_get_pfvf(rvu, pcifunc),
27343f8fe40aSSubbaraya Sundeep 					block->addr);
27352e3e94c2SHariprasad Kelam 	if (!num_lfs)
27363f8fe40aSSubbaraya Sundeep 		return;
2737d431abd0SGeetha sowjanya 	for (slot = 0; slot < num_lfs; slot++) {
2738d431abd0SGeetha sowjanya 		lf = rvu_get_lf(rvu, block, pcifunc, slot);
2739d431abd0SGeetha sowjanya 		if (lf < 0)
2740c554f9c1SGeetha sowjanya 			continue;
2741c554f9c1SGeetha sowjanya 
2742c554f9c1SGeetha sowjanya 		/* Cleanup LF and reset it */
2743465ed9c1SSunil Goutham 		if (block->addr == BLKADDR_NIX0 || block->addr == BLKADDR_NIX1)
2744465ed9c1SSunil Goutham 			rvu_nix_lf_teardown(rvu, pcifunc, block->addr, lf);
2745465ed9c1SSunil Goutham 		else if (block->addr == BLKADDR_NPA)
2746465ed9c1SSunil Goutham 			rvu_npa_lf_teardown(rvu, pcifunc, lf);
2747465ed9c1SSunil Goutham 		else if ((block->addr == BLKADDR_CPT0) ||
2748465ed9c1SSunil Goutham 			 (block->addr == BLKADDR_CPT1))
2749465ed9c1SSunil Goutham 			rvu_cpt_lf_teardown(rvu, pcifunc, block->addr, lf,
2750465ed9c1SSunil Goutham 					    slot);
2751465ed9c1SSunil Goutham 
2752465ed9c1SSunil Goutham 		err = rvu_lf_reset(rvu, block, lf);
2753465ed9c1SSunil Goutham 		if (err) {
2754465ed9c1SSunil Goutham 			dev_err(rvu->dev, "Failed to reset blkaddr %d LF%d\n",
2755465ed9c1SSunil Goutham 				block->addr, lf);
2756465ed9c1SSunil Goutham 		}
2757465ed9c1SSunil Goutham 	}
2758465ed9c1SSunil Goutham }
2759465ed9c1SSunil Goutham 
__rvu_flr_handler(struct rvu * rvu,u16 pcifunc)27609fe4ebf7SGeetha sowjanya static void __rvu_flr_handler(struct rvu *rvu, u16 pcifunc)
27619fe4ebf7SGeetha sowjanya {
27629fe4ebf7SGeetha sowjanya 	if (rvu_npc_exact_has_match_table(rvu))
27639fe4ebf7SGeetha sowjanya 		rvu_npc_exact_reset(rvu, pcifunc);
2764c554f9c1SGeetha sowjanya 
2765c554f9c1SGeetha sowjanya 	mutex_lock(&rvu->flr_lock);
2766c554f9c1SGeetha sowjanya 	/* Reset order should reflect inter-block dependencies:
27679fe4ebf7SGeetha sowjanya 	 * 1. Reset any packet/work sources (NIX, CPT, TIM)
27689fe4ebf7SGeetha sowjanya 	 * 2. Flush and reset SSO/SSOW
2769465ed9c1SSunil Goutham 	 * 3. Cleanup pools (NPA)
2770465ed9c1SSunil Goutham 	 */
2771465ed9c1SSunil Goutham 
2772465ed9c1SSunil Goutham 	/* Free allocated BPIDs */
27739fe4ebf7SGeetha sowjanya 	rvu_nix_flr_free_bpids(rvu, pcifunc);
2774c554f9c1SGeetha sowjanya 
2775c554f9c1SGeetha sowjanya 	/* Free multicast/mirror node associated with the 'pcifunc' */
2776c554f9c1SGeetha sowjanya 	rvu_nix_mcast_flr_free_entries(rvu, pcifunc);
2777c554f9c1SGeetha sowjanya 
2778c554f9c1SGeetha sowjanya 	rvu_blklf_teardown(rvu, pcifunc, BLKADDR_NIX0);
2779c554f9c1SGeetha sowjanya 	rvu_blklf_teardown(rvu, pcifunc, BLKADDR_NIX1);
2780c554f9c1SGeetha sowjanya 	rvu_blklf_teardown(rvu, pcifunc, BLKADDR_CPT0);
2781c554f9c1SGeetha sowjanya 	rvu_blklf_teardown(rvu, pcifunc, BLKADDR_CPT1);
2782c554f9c1SGeetha sowjanya 	rvu_blklf_teardown(rvu, pcifunc, BLKADDR_TIM);
27839fe4ebf7SGeetha sowjanya 	rvu_blklf_teardown(rvu, pcifunc, BLKADDR_SSOW);
27849fe4ebf7SGeetha sowjanya 	rvu_blklf_teardown(rvu, pcifunc, BLKADDR_SSO);
27859fe4ebf7SGeetha sowjanya 	rvu_blklf_teardown(rvu, pcifunc, BLKADDR_NPA);
27869fe4ebf7SGeetha sowjanya 	rvu_reset_lmt_map_tbl(rvu, pcifunc);
27879fe4ebf7SGeetha sowjanya 	rvu_detach_rsrcs(rvu, NULL, pcifunc);
27889fe4ebf7SGeetha sowjanya 	/* In scenarios where PF/VF drivers detach NIXLF without freeing MCAM
27899fe4ebf7SGeetha sowjanya 	 * entries, check and free the MCAM entries explicitly to avoid leak.
2790465ed9c1SSunil Goutham 	 * Since LF is detached use LF number as -1.
2791465ed9c1SSunil Goutham 	 */
2792465ed9c1SSunil Goutham 	rvu_npc_free_mcam_entries(rvu, pcifunc, -1);
2793465ed9c1SSunil Goutham 	rvu_mac_reset(rvu, pcifunc);
2794465ed9c1SSunil Goutham 
2795465ed9c1SSunil Goutham 	if (rvu->mcs_blk_cnt)
2796465ed9c1SSunil Goutham 		rvu_mcs_flr_handler(rvu, pcifunc);
2797465ed9c1SSunil Goutham 
2798465ed9c1SSunil Goutham 	mutex_unlock(&rvu->flr_lock);
2799465ed9c1SSunil Goutham }
2800465ed9c1SSunil Goutham 
rvu_afvf_flr_handler(struct rvu * rvu,int vf)2801465ed9c1SSunil Goutham static void rvu_afvf_flr_handler(struct rvu *rvu, int vf)
2802465ed9c1SSunil Goutham {
2803465ed9c1SSunil Goutham 	int reg = 0;
2804465ed9c1SSunil Goutham 
2805465ed9c1SSunil Goutham 	/* pcifunc = 0(PF0) | (vf + 1) */
2806465ed9c1SSunil Goutham 	__rvu_flr_handler(rvu, vf + 1);
2807465ed9c1SSunil Goutham 
2808906999c9SNithin Dabilpuram 	if (vf >= 64) {
2809906999c9SNithin Dabilpuram 		reg = 1;
2810906999c9SNithin Dabilpuram 		vf = vf - 64;
2811465ed9c1SSunil Goutham 	}
2812465ed9c1SSunil Goutham 
2813465ed9c1SSunil Goutham 	/* Signal FLR finish and enable IRQ */
28149fe4ebf7SGeetha sowjanya 	rvupf_write64(rvu, RVU_PF_VFTRPENDX(reg), BIT_ULL(vf));
28159fe4ebf7SGeetha sowjanya 	rvupf_write64(rvu, RVU_PF_VFFLR_INT_ENA_W1SX(reg), BIT_ULL(vf));
28169fe4ebf7SGeetha sowjanya }
28179fe4ebf7SGeetha sowjanya 
rvu_flr_handler(struct work_struct * work)28189fe4ebf7SGeetha sowjanya static void rvu_flr_handler(struct work_struct *work)
28199fe4ebf7SGeetha sowjanya {
28209fe4ebf7SGeetha sowjanya 	struct rvu_work *flrwork = container_of(work, struct rvu_work, work);
2821465ed9c1SSunil Goutham 	struct rvu *rvu = flrwork->rvu;
2822465ed9c1SSunil Goutham 	u16 pcifunc, numvfs, vf;
28239fe4ebf7SGeetha sowjanya 	u64 cfg;
28249fe4ebf7SGeetha sowjanya 	int pf;
28259fe4ebf7SGeetha sowjanya 
28269fe4ebf7SGeetha sowjanya 	pf = flrwork - rvu->flr_wrk;
28279fe4ebf7SGeetha sowjanya 	if (pf >= rvu->hw->total_pfs) {
28289fe4ebf7SGeetha sowjanya 		rvu_afvf_flr_handler(rvu, pf - rvu->hw->total_pfs);
28299fe4ebf7SGeetha sowjanya 		return;
28309fe4ebf7SGeetha sowjanya 	}
28319fe4ebf7SGeetha sowjanya 
2832906999c9SNithin Dabilpuram 	cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_PFX_CFG(pf));
2833906999c9SNithin Dabilpuram 	numvfs = (cfg >> 12) & 0xFF;
28349fe4ebf7SGeetha sowjanya 	pcifunc  = rvu_make_pcifunc(rvu->pdev, pf, 0);
28359fe4ebf7SGeetha sowjanya 
2836465ed9c1SSunil Goutham 	for (vf = 0; vf < numvfs; vf++)
2837465ed9c1SSunil Goutham 		__rvu_flr_handler(rvu, (pcifunc | (vf + 1)));
2838465ed9c1SSunil Goutham 
2839465ed9c1SSunil Goutham 	__rvu_flr_handler(rvu, pcifunc);
2840465ed9c1SSunil Goutham 
2841465ed9c1SSunil Goutham 	/* Signal FLR finish */
28429fe4ebf7SGeetha sowjanya 	rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFTRPEND, BIT_ULL(pf));
28439fe4ebf7SGeetha sowjanya 
28449fe4ebf7SGeetha sowjanya 	/* Enable interrupt */
28454fa91210SLinu Cherian 	rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFFLR_INT_ENA_W1S,  BIT_ULL(pf));
28464fa91210SLinu Cherian }
28474fa91210SLinu Cherian 
rvu_afvf_queue_flr_work(struct rvu * rvu,int start_vf,int numvfs)28484fa91210SLinu Cherian static void rvu_afvf_queue_flr_work(struct rvu *rvu, int start_vf, int numvfs)
28494fa91210SLinu Cherian {
28504fa91210SLinu Cherian 	int dev, vf, reg = 0;
28514fa91210SLinu Cherian 	u64 intr;
28524fa91210SLinu Cherian 
28534fa91210SLinu Cherian 	if (start_vf >= 64)
28544fa91210SLinu Cherian 		reg = 1;
28554fa91210SLinu Cherian 
28564fa91210SLinu Cherian 	intr = rvupf_read64(rvu, RVU_PF_VFFLR_INTX(reg));
28574fa91210SLinu Cherian 	if (!intr)
28584fa91210SLinu Cherian 		return;
28594fa91210SLinu Cherian 
28604fa91210SLinu Cherian 	for (vf = 0; vf < numvfs; vf++) {
28614fa91210SLinu Cherian 		if (!(intr & BIT_ULL(vf)))
28624fa91210SLinu Cherian 			continue;
28634fa91210SLinu Cherian 		/* Clear and disable the interrupt */
28644fa91210SLinu Cherian 		rvupf_write64(rvu, RVU_PF_VFFLR_INTX(reg), BIT_ULL(vf));
28654fa91210SLinu Cherian 		rvupf_write64(rvu, RVU_PF_VFFLR_INT_ENA_W1CX(reg), BIT_ULL(vf));
28664fa91210SLinu Cherian 
28674fa91210SLinu Cherian 		dev = vf + start_vf + rvu->hw->total_pfs;
28684fa91210SLinu Cherian 		queue_work(rvu->flr_wq, &rvu->flr_wrk[dev].work);
28694fa91210SLinu Cherian 	}
28704fa91210SLinu Cherian }
28714fa91210SLinu Cherian 
rvu_flr_intr_handler(int irq,void * rvu_irq)28724fa91210SLinu Cherian static irqreturn_t rvu_flr_intr_handler(int irq, void *rvu_irq)
28734fa91210SLinu Cherian {
28744fa91210SLinu Cherian 	struct rvu *rvu = (struct rvu *)rvu_irq;
28754fa91210SLinu Cherian 	u64 intr;
28764fa91210SLinu Cherian 	u8  pf;
28774fa91210SLinu Cherian 
28784fa91210SLinu Cherian 	intr = rvu_read64(rvu, BLKADDR_RVUM, RVU_AF_PFFLR_INT);
28794fa91210SLinu Cherian 	if (!intr)
28804fa91210SLinu Cherian 		goto afvf_flr;
28814fa91210SLinu Cherian 
28824fa91210SLinu Cherian 	for (pf = 0; pf < rvu->hw->total_pfs; pf++) {
28834fa91210SLinu Cherian 		if (intr & (1ULL << pf)) {
28844fa91210SLinu Cherian 			/* clear interrupt */
28854fa91210SLinu Cherian 			rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFFLR_INT,
28864fa91210SLinu Cherian 				    BIT_ULL(pf));
28874fa91210SLinu Cherian 			/* Disable the interrupt */
28884fa91210SLinu Cherian 			rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFFLR_INT_ENA_W1C,
28894fa91210SLinu Cherian 				    BIT_ULL(pf));
28904fa91210SLinu Cherian 			/* PF is already dead do only AF related operations */
28914fa91210SLinu Cherian 			queue_work(rvu->flr_wq, &rvu->flr_wrk[pf].work);
28924fa91210SLinu Cherian 		}
28934fa91210SLinu Cherian 	}
28944fa91210SLinu Cherian 
28954fa91210SLinu Cherian afvf_flr:
28964fa91210SLinu Cherian 	rvu_afvf_queue_flr_work(rvu, 0, 64);
28974fa91210SLinu Cherian 	if (rvu->vfs > 64)
28984fa91210SLinu Cherian 		rvu_afvf_queue_flr_work(rvu, 64, rvu->vfs - 64);
28994fa91210SLinu Cherian 
29004fa91210SLinu Cherian 	return IRQ_HANDLED;
29014fa91210SLinu Cherian }
29024fa91210SLinu Cherian 
rvu_me_handle_vfset(struct rvu * rvu,int idx,u64 intr)29034fa91210SLinu Cherian static void rvu_me_handle_vfset(struct rvu *rvu, int idx, u64 intr)
29044fa91210SLinu Cherian {
29054fa91210SLinu Cherian 	int vf;
29067304ac45SSunil Goutham 
29077304ac45SSunil Goutham 	/* Nothing to be done here other than clearing the
29087304ac45SSunil Goutham 	 * TRPEND bit.
29097304ac45SSunil Goutham 	 */
291048260907SSrujana Challa 	for (vf = 0; vf < 64; vf++) {
291148260907SSrujana Challa 		if (intr & (1ULL << vf)) {
29127304ac45SSunil Goutham 			/* clear the trpend due to ME(master enable) */
29137304ac45SSunil Goutham 			rvupf_write64(rvu, RVU_PF_VFTRPENDX(idx), BIT_ULL(vf));
29147304ac45SSunil Goutham 			/* clear interrupt */
29157304ac45SSunil Goutham 			rvupf_write64(rvu, RVU_PF_VFME_INTX(idx), BIT_ULL(vf));
29169fe4ebf7SGeetha sowjanya 		}
29179fe4ebf7SGeetha sowjanya 	}
29189fe4ebf7SGeetha sowjanya }
29199fe4ebf7SGeetha sowjanya 
29204fa91210SLinu Cherian /* Handles ME interrupts from VFs of AF */
rvu_me_vf_intr_handler(int irq,void * rvu_irq)29214fa91210SLinu Cherian static irqreturn_t rvu_me_vf_intr_handler(int irq, void *rvu_irq)
29224fa91210SLinu Cherian {
29234fa91210SLinu Cherian 	struct rvu *rvu = (struct rvu *)rvu_irq;
29247304ac45SSunil Goutham 	int vfset;
2925ae2619ddSGeetha sowjanya 	u64 intr;
29267304ac45SSunil Goutham 
2927ae2619ddSGeetha sowjanya 	intr = rvu_read64(rvu, BLKADDR_RVUM, RVU_AF_PFME_INT);
2928ae2619ddSGeetha sowjanya 
29297304ac45SSunil Goutham 	for (vfset = 0; vfset <= 1; vfset++) {
29307304ac45SSunil Goutham 		intr = rvupf_read64(rvu, RVU_PF_VFME_INTX(vfset));
29317304ac45SSunil Goutham 		if (intr)
29327304ac45SSunil Goutham 			rvu_me_handle_vfset(rvu, vfset, intr);
29337304ac45SSunil Goutham 	}
29347304ac45SSunil Goutham 
29359bd6caf3STomasz Duszynski 	return IRQ_HANDLED;
29369bd6caf3STomasz Duszynski }
29379bd6caf3STomasz Duszynski 
29389bd6caf3STomasz Duszynski /* Handles ME interrupts from PFs */
rvu_me_pf_intr_handler(int irq,void * rvu_irq)29399bd6caf3STomasz Duszynski static irqreturn_t rvu_me_pf_intr_handler(int irq, void *rvu_irq)
29409bd6caf3STomasz Duszynski {
29419bd6caf3STomasz Duszynski 	struct rvu *rvu = (struct rvu *)rvu_irq;
29429bd6caf3STomasz Duszynski 	u64 intr;
29439bd6caf3STomasz Duszynski 	u8  pf;
29449bd6caf3STomasz Duszynski 
29459bd6caf3STomasz Duszynski 	intr = rvu_read64(rvu, BLKADDR_RVUM, RVU_AF_PFME_INT);
29469bd6caf3STomasz Duszynski 
29479bd6caf3STomasz Duszynski 	/* Nothing to be done here other than clearing the
29489bd6caf3STomasz Duszynski 	 * TRPEND bit.
29499bd6caf3STomasz Duszynski 	 */
29509bd6caf3STomasz Duszynski 	for (pf = 0; pf < rvu->hw->total_pfs; pf++) {
29517304ac45SSunil Goutham 		if (intr & (1ULL << pf)) {
29527304ac45SSunil Goutham 			/* clear the trpend due to ME(master enable) */
2953465ed9c1SSunil Goutham 			rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFTRPEND,
29547304ac45SSunil Goutham 				    BIT_ULL(pf));
29557304ac45SSunil Goutham 			/* clear interrupt */
29567304ac45SSunil Goutham 			rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFME_INT,
29577304ac45SSunil Goutham 				    BIT_ULL(pf));
29587304ac45SSunil Goutham 		}
29597304ac45SSunil Goutham 	}
29607304ac45SSunil Goutham 
29617304ac45SSunil Goutham 	return IRQ_HANDLED;
29627304ac45SSunil Goutham }
29637304ac45SSunil Goutham 
rvu_unregister_interrupts(struct rvu * rvu)29647304ac45SSunil Goutham static void rvu_unregister_interrupts(struct rvu *rvu)
29657304ac45SSunil Goutham {
29667304ac45SSunil Goutham 	int irq;
29677304ac45SSunil Goutham 
29687304ac45SSunil Goutham 	rvu_cpt_unregister_interrupts(rvu);
29697304ac45SSunil Goutham 
29707304ac45SSunil Goutham 	if (!is_cn20k(rvu->pdev))
29717304ac45SSunil Goutham 		/* Disable the Mbox interrupt */
29727304ac45SSunil Goutham 		rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFAF_MBOX_INT_ENA_W1C,
29737304ac45SSunil Goutham 			    INTR_MASK(rvu->hw->total_pfs) & ~1ULL);
29747304ac45SSunil Goutham 	else
29757304ac45SSunil Goutham 		cn20k_rvu_unregister_interrupts(rvu);
29767304ac45SSunil Goutham 
29777304ac45SSunil Goutham 	/* Disable the PF FLR interrupt */
29787304ac45SSunil Goutham 	rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFFLR_INT_ENA_W1C,
29797304ac45SSunil Goutham 		    INTR_MASK(rvu->hw->total_pfs) & ~1ULL);
298050e60de3SSubbaraya Sundeep 
29817304ac45SSunil Goutham 	/* Disable the PF ME interrupt */
29827304ac45SSunil Goutham 	rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFME_INT_ENA_W1C,
29837304ac45SSunil Goutham 		    INTR_MASK(rvu->hw->total_pfs) & ~1ULL);
29847304ac45SSunil Goutham 
29857304ac45SSunil Goutham 	for (irq = 0; irq < rvu->num_vec; irq++) {
29867304ac45SSunil Goutham 		if (rvu->irq_allocated[irq]) {
29877304ac45SSunil Goutham 			free_irq(pci_irq_vector(rvu->pdev, irq), rvu);
29887304ac45SSunil Goutham 			rvu->irq_allocated[irq] = false;
29897304ac45SSunil Goutham 		}
29907304ac45SSunil Goutham 	}
29917304ac45SSunil Goutham 
29927304ac45SSunil Goutham 	pci_free_irq_vectors(rvu->pdev);
29939fe4ebf7SGeetha sowjanya 	rvu->num_vec = 0;
29949fe4ebf7SGeetha sowjanya }
29959fe4ebf7SGeetha sowjanya 
rvu_afvf_msix_vectors_num_ok(struct rvu * rvu)29969fe4ebf7SGeetha sowjanya static int rvu_afvf_msix_vectors_num_ok(struct rvu *rvu)
29979fe4ebf7SGeetha sowjanya {
29989fe4ebf7SGeetha sowjanya 	struct rvu_pfvf *pfvf = &rvu->pf[0];
29999fe4ebf7SGeetha sowjanya 	int offset;
30009fe4ebf7SGeetha sowjanya 
30019fe4ebf7SGeetha sowjanya 	pfvf = &rvu->pf[0];
30029fe4ebf7SGeetha sowjanya 	offset = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_PFX_INT_CFG(0)) & 0x3ff;
30039fe4ebf7SGeetha sowjanya 
30049fe4ebf7SGeetha sowjanya 	/* Make sure there are enough MSIX vectors configured so that
30059fe4ebf7SGeetha sowjanya 	 * VF interrupts can be handled. Offset equal to zero means
30069fe4ebf7SGeetha sowjanya 	 * that PF vectors are not configured and overlapping AF vectors.
30079fe4ebf7SGeetha sowjanya 	 */
30089fe4ebf7SGeetha sowjanya 	if (is_cn20k(rvu->pdev))
30099fe4ebf7SGeetha sowjanya 		return (pfvf->msix.max >= RVU_AF_CN20K_INT_VEC_CNT +
30109fe4ebf7SGeetha sowjanya 			RVU_MBOX_PF_INT_VEC_CNT) && offset;
30119fe4ebf7SGeetha sowjanya 
30129fe4ebf7SGeetha sowjanya 	return (pfvf->msix.max >= RVU_AF_INT_VEC_CNT + RVU_PF_INT_VEC_CNT) &&
30139fe4ebf7SGeetha sowjanya 	       offset;
30144fa91210SLinu Cherian }
30154fa91210SLinu Cherian 
rvu_register_interrupts(struct rvu * rvu)30164fa91210SLinu Cherian static int rvu_register_interrupts(struct rvu *rvu)
30174fa91210SLinu Cherian {
30184fa91210SLinu Cherian 	int ret, offset, pf_vec_start;
30194fa91210SLinu Cherian 
30204fa91210SLinu Cherian 	rvu->num_vec = pci_msix_vec_count(rvu->pdev);
30214fa91210SLinu Cherian 
30224fa91210SLinu Cherian 	rvu->irq_name = devm_kmalloc_array(rvu->dev, rvu->num_vec,
30234fa91210SLinu Cherian 					   NAME_SIZE, GFP_KERNEL);
30244fa91210SLinu Cherian 	if (!rvu->irq_name)
30254fa91210SLinu Cherian 		return -ENOMEM;
30264fa91210SLinu Cherian 
3027549c35ecSSunil Goutham 	rvu->irq_allocated = devm_kcalloc(rvu->dev, rvu->num_vec,
3028549c35ecSSunil Goutham 					  sizeof(bool), GFP_KERNEL);
3029549c35ecSSunil Goutham 	if (!rvu->irq_allocated)
30304fa91210SLinu Cherian 		return -ENOMEM;
30314fa91210SLinu Cherian 
30324fa91210SLinu Cherian 	/* Enable MSI-X */
30334fa91210SLinu Cherian 	ret = pci_alloc_irq_vectors(rvu->pdev, rvu->num_vec,
30344fa91210SLinu Cherian 				    rvu->num_vec, PCI_IRQ_MSIX);
30354fa91210SLinu Cherian 	if (ret < 0) {
30364fa91210SLinu Cherian 		dev_err(rvu->dev,
30379bd6caf3STomasz Duszynski 			"RVUAF: Request for %d msix vectors failed, ret %d\n",
30389bd6caf3STomasz Duszynski 			rvu->num_vec, ret);
30399bd6caf3STomasz Duszynski 		return ret;
30409bd6caf3STomasz Duszynski 	}
3041465ed9c1SSunil Goutham 
3042465ed9c1SSunil Goutham 	if (!is_cn20k(rvu->pdev)) {
30439bd6caf3STomasz Duszynski 		/* Register mailbox interrupt handler */
30449bd6caf3STomasz Duszynski 		sprintf(&rvu->irq_name[RVU_AF_INT_VEC_MBOX * NAME_SIZE],
3045465ed9c1SSunil Goutham 			"RVUAF Mbox");
30469bd6caf3STomasz Duszynski 		ret = request_irq(pci_irq_vector
30479bd6caf3STomasz Duszynski 				  (rvu->pdev, RVU_AF_INT_VEC_MBOX),
30489bd6caf3STomasz Duszynski 				  rvu->ng_rvu->rvu_mbox_ops->pf_intr_handler, 0,
30499bd6caf3STomasz Duszynski 				  &rvu->irq_name[RVU_AF_INT_VEC_MBOX *
30509bd6caf3STomasz Duszynski 				  NAME_SIZE], rvu);
30519bd6caf3STomasz Duszynski 		if (ret) {
30529bd6caf3STomasz Duszynski 			dev_err(rvu->dev,
30539bd6caf3STomasz Duszynski 				"RVUAF: IRQ registration failed for mbox\n");
30549bd6caf3STomasz Duszynski 			goto fail;
30559bd6caf3STomasz Duszynski 		}
30569bd6caf3STomasz Duszynski 
30579bd6caf3STomasz Duszynski 		rvu->irq_allocated[RVU_AF_INT_VEC_MBOX] = true;
30589bd6caf3STomasz Duszynski 	} else {
30599bd6caf3STomasz Duszynski 		ret = cn20k_register_afpf_mbox_intr(rvu);
3060465ed9c1SSunil Goutham 		if (ret) {
30619bd6caf3STomasz Duszynski 			dev_err(rvu->dev,
30629bd6caf3STomasz Duszynski 				"RVUAF: IRQ registration failed for mbox\n");
30639bd6caf3STomasz Duszynski 			goto fail;
30649bd6caf3STomasz Duszynski 		}
30659bd6caf3STomasz Duszynski 	}
30669bd6caf3STomasz Duszynski 
30679bd6caf3STomasz Duszynski 	/* Enable mailbox interrupts from all PFs */
30689bd6caf3STomasz Duszynski 	rvu_enable_mbox_intr(rvu);
30699bd6caf3STomasz Duszynski 
30709bd6caf3STomasz Duszynski 	/* Register FLR interrupt handler */
30719bd6caf3STomasz Duszynski 	sprintf(&rvu->irq_name[RVU_AF_INT_VEC_PFFLR * NAME_SIZE],
3072465ed9c1SSunil Goutham 		"RVUAF FLR");
3073465ed9c1SSunil Goutham 	ret = request_irq(pci_irq_vector(rvu->pdev, RVU_AF_INT_VEC_PFFLR),
3074465ed9c1SSunil Goutham 			  rvu_flr_intr_handler, 0,
3075465ed9c1SSunil Goutham 			  &rvu->irq_name[RVU_AF_INT_VEC_PFFLR * NAME_SIZE],
3076465ed9c1SSunil Goutham 			  rvu);
3077465ed9c1SSunil Goutham 	if (ret) {
3078465ed9c1SSunil Goutham 		dev_err(rvu->dev,
3079465ed9c1SSunil Goutham 			"RVUAF: IRQ registration failed for FLR\n");
3080465ed9c1SSunil Goutham 		goto fail;
3081465ed9c1SSunil Goutham 	}
3082465ed9c1SSunil Goutham 	rvu->irq_allocated[RVU_AF_INT_VEC_PFFLR] = true;
3083465ed9c1SSunil Goutham 
3084465ed9c1SSunil Goutham 	/* Enable FLR interrupt for all PFs*/
3085465ed9c1SSunil Goutham 	rvu_write64(rvu, BLKADDR_RVUM,
3086465ed9c1SSunil Goutham 		    RVU_AF_PFFLR_INT, INTR_MASK(rvu->hw->total_pfs));
3087465ed9c1SSunil Goutham 
3088465ed9c1SSunil Goutham 	rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFFLR_INT_ENA_W1S,
3089465ed9c1SSunil Goutham 		    INTR_MASK(rvu->hw->total_pfs) & ~1ULL);
3090465ed9c1SSunil Goutham 
3091465ed9c1SSunil Goutham 	/* Register ME interrupt handler */
3092465ed9c1SSunil Goutham 	sprintf(&rvu->irq_name[RVU_AF_INT_VEC_PFME * NAME_SIZE],
3093465ed9c1SSunil Goutham 		"RVUAF ME");
3094465ed9c1SSunil Goutham 	ret = request_irq(pci_irq_vector(rvu->pdev, RVU_AF_INT_VEC_PFME),
3095465ed9c1SSunil Goutham 			  rvu_me_pf_intr_handler, 0,
3096465ed9c1SSunil Goutham 			  &rvu->irq_name[RVU_AF_INT_VEC_PFME * NAME_SIZE],
30974fa91210SLinu Cherian 			  rvu);
30984fa91210SLinu Cherian 	if (ret) {
30994fa91210SLinu Cherian 		dev_err(rvu->dev,
31004fa91210SLinu Cherian 			"RVUAF: IRQ registration failed for ME\n");
31014fa91210SLinu Cherian 	}
31024fa91210SLinu Cherian 	rvu->irq_allocated[RVU_AF_INT_VEC_PFME] = true;
31034fa91210SLinu Cherian 
31044fa91210SLinu Cherian 	/* Clear TRPEND bit for all PF */
31054fa91210SLinu Cherian 	rvu_write64(rvu, BLKADDR_RVUM,
31064fa91210SLinu Cherian 		    RVU_AF_PFTRPEND, INTR_MASK(rvu->hw->total_pfs));
31074fa91210SLinu Cherian 	/* Enable ME interrupt for all PFs*/
31084fa91210SLinu Cherian 	rvu_write64(rvu, BLKADDR_RVUM,
31094fa91210SLinu Cherian 		    RVU_AF_PFME_INT, INTR_MASK(rvu->hw->total_pfs));
31104fa91210SLinu Cherian 
31114fa91210SLinu Cherian 	rvu_write64(rvu, BLKADDR_RVUM, RVU_AF_PFME_INT_ENA_W1S,
31124fa91210SLinu Cherian 		    INTR_MASK(rvu->hw->total_pfs) & ~1ULL);
31134fa91210SLinu Cherian 
31144fa91210SLinu Cherian 	if (!rvu_afvf_msix_vectors_num_ok(rvu))
31154fa91210SLinu Cherian 		return 0;
31164fa91210SLinu Cherian 
31174fa91210SLinu Cherian 	/* Get PF MSIX vectors offset. */
31184fa91210SLinu Cherian 	pf_vec_start = rvu_read64(rvu, BLKADDR_RVUM,
31194fa91210SLinu Cherian 				  RVU_PRIV_PFX_INT_CFG(0)) & 0x3ff;
31204fa91210SLinu Cherian 	if (!is_cn20k(rvu->pdev)) {
312148260907SSrujana Challa 		/* Register MBOX0 interrupt. */
312248260907SSrujana Challa 		offset = pf_vec_start + RVU_PF_INT_VEC_VFPF_MBOX0;
312348260907SSrujana Challa 		sprintf(&rvu->irq_name[offset * NAME_SIZE], "RVUAFVF Mbox0");
312448260907SSrujana Challa 		ret = request_irq(pci_irq_vector(rvu->pdev, offset),
312548260907SSrujana Challa 				  rvu->ng_rvu->rvu_mbox_ops->afvf_intr_handler, 0,
31267304ac45SSunil Goutham 				  &rvu->irq_name[offset * NAME_SIZE],
31277304ac45SSunil Goutham 				  rvu);
31287304ac45SSunil Goutham 		if (ret)
3129465ed9c1SSunil Goutham 			dev_err(rvu->dev,
31307304ac45SSunil Goutham 				"RVUAF: IRQ registration failed for Mbox0\n");
31317304ac45SSunil Goutham 
31327304ac45SSunil Goutham 		rvu->irq_allocated[offset] = true;
31339fe4ebf7SGeetha sowjanya 
31349fe4ebf7SGeetha sowjanya 		/* Register MBOX1 interrupt. MBOX1 IRQ number follows MBOX0 so
31359fe4ebf7SGeetha sowjanya 		 * simply increment current offset by 1.
31369fe4ebf7SGeetha sowjanya 		 */
31379fe4ebf7SGeetha sowjanya 		offset = pf_vec_start + RVU_PF_INT_VEC_VFPF_MBOX1;
31389fe4ebf7SGeetha sowjanya 		sprintf(&rvu->irq_name[offset * NAME_SIZE], "RVUAFVF Mbox1");
31399fe4ebf7SGeetha sowjanya 		ret = request_irq(pci_irq_vector(rvu->pdev, offset),
31409fe4ebf7SGeetha sowjanya 				  rvu->ng_rvu->rvu_mbox_ops->afvf_intr_handler, 0,
31419fe4ebf7SGeetha sowjanya 				  &rvu->irq_name[offset * NAME_SIZE],
31429fe4ebf7SGeetha sowjanya 				  rvu);
3143465ed9c1SSunil Goutham 		if (ret)
31449fe4ebf7SGeetha sowjanya 			dev_err(rvu->dev,
31459fe4ebf7SGeetha sowjanya 				"RVUAF: IRQ registration failed for Mbox1\n");
31469fe4ebf7SGeetha sowjanya 
31479fe4ebf7SGeetha sowjanya 		rvu->irq_allocated[offset] = true;
3148465ed9c1SSunil Goutham 	} else {
31499fe4ebf7SGeetha sowjanya 		ret = cn20k_register_afvf_mbox_intr(rvu, pf_vec_start);
31509fe4ebf7SGeetha sowjanya 		if (ret)
31519fe4ebf7SGeetha sowjanya 			dev_err(rvu->dev,
31529fe4ebf7SGeetha sowjanya 				"RVUAF: IRQ registration failed for Mbox\n");
31539fe4ebf7SGeetha sowjanya 	}
3154289f9746STejun Heo 
3155289f9746STejun Heo 	/* Register FLR interrupt handler for AF's VFs */
31569fe4ebf7SGeetha sowjanya 	offset = pf_vec_start + RVU_PF_INT_VEC_VFFLR0;
31579fe4ebf7SGeetha sowjanya 	sprintf(&rvu->irq_name[offset * NAME_SIZE], "RVUAFVF FLR0");
31589fe4ebf7SGeetha sowjanya 	ret = request_irq(pci_irq_vector(rvu->pdev, offset),
3159465ed9c1SSunil Goutham 			  rvu_flr_intr_handler, 0,
3160465ed9c1SSunil Goutham 			  &rvu->irq_name[offset * NAME_SIZE], rvu);
31619fe4ebf7SGeetha sowjanya 	if (ret) {
31629fe4ebf7SGeetha sowjanya 		dev_err(rvu->dev,
31639fe4ebf7SGeetha sowjanya 			"RVUAF: IRQ registration failed for RVUAFVF FLR0\n");
31649fe4ebf7SGeetha sowjanya 		goto fail;
31659fe4ebf7SGeetha sowjanya 	}
31669fe4ebf7SGeetha sowjanya 	rvu->irq_allocated[offset] = true;
3167465ed9c1SSunil Goutham 
3168465ed9c1SSunil Goutham 	offset = pf_vec_start + RVU_PF_INT_VEC_VFFLR1;
3169465ed9c1SSunil Goutham 	sprintf(&rvu->irq_name[offset * NAME_SIZE], "RVUAFVF FLR1");
31709fe4ebf7SGeetha sowjanya 	ret = request_irq(pci_irq_vector(rvu->pdev, offset),
31719fe4ebf7SGeetha sowjanya 			  rvu_flr_intr_handler, 0,
31729fe4ebf7SGeetha sowjanya 			  &rvu->irq_name[offset * NAME_SIZE], rvu);
31739fe4ebf7SGeetha sowjanya 	if (ret) {
31749fe4ebf7SGeetha sowjanya 		dev_err(rvu->dev,
31759fe4ebf7SGeetha sowjanya 			"RVUAF: IRQ registration failed for RVUAFVF FLR1\n");
31769fe4ebf7SGeetha sowjanya 		goto fail;
3177465ed9c1SSunil Goutham 	}
31789bd6caf3STomasz Duszynski 	rvu->irq_allocated[offset] = true;
31799bd6caf3STomasz Duszynski 
31809bd6caf3STomasz Duszynski 	/* Register ME interrupt handler for AF's VFs */
31819bd6caf3STomasz Duszynski 	offset = pf_vec_start + RVU_PF_INT_VEC_VFME0;
3182465ed9c1SSunil Goutham 	sprintf(&rvu->irq_name[offset * NAME_SIZE], "RVUAFVF ME0");
31834fa91210SLinu Cherian 	ret = request_irq(pci_irq_vector(rvu->pdev, offset),
3184465ed9c1SSunil Goutham 			  rvu_me_vf_intr_handler, 0,
3185465ed9c1SSunil Goutham 			  &rvu->irq_name[offset * NAME_SIZE], rvu);
3186465ed9c1SSunil Goutham 	if (ret) {
31879bd6caf3STomasz Duszynski 		dev_err(rvu->dev,
31889bd6caf3STomasz Duszynski 			"RVUAF: IRQ registration failed for RVUAFVF ME0\n");
3189465ed9c1SSunil Goutham 		goto fail;
31904fa91210SLinu Cherian 	}
31919bd6caf3STomasz Duszynski 	rvu->irq_allocated[offset] = true;
31929bd6caf3STomasz Duszynski 
3193465ed9c1SSunil Goutham 	offset = pf_vec_start + RVU_PF_INT_VEC_VFME1;
31949bd6caf3STomasz Duszynski 	sprintf(&rvu->irq_name[offset * NAME_SIZE], "RVUAFVF ME1");
31959bd6caf3STomasz Duszynski 	ret = request_irq(pci_irq_vector(rvu->pdev, offset),
31969bd6caf3STomasz Duszynski 			  rvu_me_vf_intr_handler, 0,
31979bd6caf3STomasz Duszynski 			  &rvu->irq_name[offset * NAME_SIZE], rvu);
31989bd6caf3STomasz Duszynski 	if (ret) {
31999bd6caf3STomasz Duszynski 		dev_err(rvu->dev,
3200465ed9c1SSunil Goutham 			"RVUAF: IRQ registration failed for RVUAFVF ME1\n");
32019bd6caf3STomasz Duszynski 		goto fail;
32029bd6caf3STomasz Duszynski 	}
32039bd6caf3STomasz Duszynski 	rvu->irq_allocated[offset] = true;
3204465ed9c1SSunil Goutham 
3205465ed9c1SSunil Goutham 	ret = rvu_cpt_register_interrupts(rvu);
3206465ed9c1SSunil Goutham 	if (ret)
32074fa91210SLinu Cherian 		goto fail;
3208465ed9c1SSunil Goutham 
32099bd6caf3STomasz Duszynski 	return 0;
32109bd6caf3STomasz Duszynski 
32119bd6caf3STomasz Duszynski fail:
32129bd6caf3STomasz Duszynski 	rvu_unregister_interrupts(rvu);
32139bd6caf3STomasz Duszynski 	return ret;
32149bd6caf3STomasz Duszynski }
32159bd6caf3STomasz Duszynski 
rvu_flr_wq_destroy(struct rvu * rvu)3216465ed9c1SSunil Goutham static void rvu_flr_wq_destroy(struct rvu *rvu)
3217465ed9c1SSunil Goutham {
3218465ed9c1SSunil Goutham 	if (rvu->flr_wq) {
32194fa91210SLinu Cherian 		destroy_workqueue(rvu->flr_wq);
32209bd6caf3STomasz Duszynski 		rvu->flr_wq = NULL;
32219bd6caf3STomasz Duszynski 	}
3222041a1c17SSubbaraya Sundeep }
32239bd6caf3STomasz Duszynski 
rvu_flr_init(struct rvu * rvu)32249bd6caf3STomasz Duszynski static int rvu_flr_init(struct rvu *rvu)
32259bd6caf3STomasz Duszynski {
32269bd6caf3STomasz Duszynski 	int dev, num_devs;
32279bd6caf3STomasz Duszynski 	u64 cfg;
32289bd6caf3STomasz Duszynski 	int pf;
32299bd6caf3STomasz Duszynski 
32309bd6caf3STomasz Duszynski 	/* Enable FLR for all PFs*/
32319bd6caf3STomasz Duszynski 	for (pf = 0; pf < rvu->hw->total_pfs; pf++) {
32329bd6caf3STomasz Duszynski 		cfg = rvu_read64(rvu, BLKADDR_RVUM, RVU_PRIV_PFX_CFG(pf));
32339bd6caf3STomasz Duszynski 		rvu_write64(rvu, BLKADDR_RVUM, RVU_PRIV_PFX_CFG(pf),
32349bd6caf3STomasz Duszynski 			    cfg | BIT_ULL(22));
32359bd6caf3STomasz Duszynski 	}
32369bd6caf3STomasz Duszynski 
32379bd6caf3STomasz Duszynski 	rvu->flr_wq = alloc_ordered_workqueue("rvu_afpf_flr",
32389bd6caf3STomasz Duszynski 					      WQ_HIGHPRI | WQ_MEM_RECLAIM);
32399bd6caf3STomasz Duszynski 	if (!rvu->flr_wq)
32409bd6caf3STomasz Duszynski 		return -ENOMEM;
32419bd6caf3STomasz Duszynski 
32429bd6caf3STomasz Duszynski 	num_devs = rvu->hw->total_pfs + pci_sriov_get_totalvfs(rvu->pdev);
32439bd6caf3STomasz Duszynski 	rvu->flr_wrk = devm_kcalloc(rvu->dev, num_devs,
32449bd6caf3STomasz Duszynski 				    sizeof(struct rvu_work), GFP_KERNEL);
32459bd6caf3STomasz Duszynski 	if (!rvu->flr_wrk) {
32469bd6caf3STomasz Duszynski 		destroy_workqueue(rvu->flr_wq);
32479bd6caf3STomasz Duszynski 		return -ENOMEM;
32489bd6caf3STomasz Duszynski 	}
32499bd6caf3STomasz Duszynski 
3250ae703539SGeetha sowjanya 	for (dev = 0; dev < num_devs; dev++) {
32519bd6caf3STomasz Duszynski 		rvu->flr_wrk[dev].rvu = rvu;
32529bd6caf3STomasz Duszynski 		INIT_WORK(&rvu->flr_wrk[dev].work, rvu_flr_handler);
32539bd6caf3STomasz Duszynski 	}
32549bd6caf3STomasz Duszynski 
32559bd6caf3STomasz Duszynski 	mutex_init(&rvu->flr_lock);
32569bd6caf3STomasz Duszynski 
32579bd6caf3STomasz Duszynski 	return 0;
3258ae703539SGeetha sowjanya }
3259ae703539SGeetha sowjanya 
rvu_disable_afvf_intr(struct rvu * rvu)3260ae703539SGeetha sowjanya static void rvu_disable_afvf_intr(struct rvu *rvu)
3261ae703539SGeetha sowjanya {
3262ae703539SGeetha sowjanya 	int vfs = rvu->vfs;
3263ae703539SGeetha sowjanya 
3264041a1c17SSubbaraya Sundeep 	if (is_cn20k(rvu->pdev))
32659bd6caf3STomasz Duszynski 		return cn20k_rvu_disable_afvf_intr(rvu, vfs);
32669bd6caf3STomasz Duszynski 
32679bd6caf3STomasz Duszynski 	rvupf_write64(rvu, RVU_PF_VFPF_MBOX_INT_ENA_W1CX(0), INTR_MASK(vfs));
32689bd6caf3STomasz Duszynski 	rvupf_write64(rvu, RVU_PF_VFFLR_INT_ENA_W1CX(0), INTR_MASK(vfs));
32699bd6caf3STomasz Duszynski 	rvupf_write64(rvu, RVU_PF_VFME_INT_ENA_W1CX(0), INTR_MASK(vfs));
32709bd6caf3STomasz Duszynski 	if (vfs <= 64)
32719bd6caf3STomasz Duszynski 		return;
32729bd6caf3STomasz Duszynski 
32739bd6caf3STomasz Duszynski 	rvupf_write64(rvu, RVU_PF_VFPF_MBOX_INT_ENA_W1CX(1),
32749bd6caf3STomasz Duszynski 		      INTR_MASK(vfs - 64));
32759bd6caf3STomasz Duszynski 	rvupf_write64(rvu, RVU_PF_VFFLR_INT_ENA_W1CX(1), INTR_MASK(vfs - 64));
32769bd6caf3STomasz Duszynski 	rvupf_write64(rvu, RVU_PF_VFME_INT_ENA_W1CX(1), INTR_MASK(vfs - 64));
3277fa2bf6baSSubbaraya Sundeep }
3278fa2bf6baSSubbaraya Sundeep 
rvu_enable_afvf_intr(struct rvu * rvu)3279fa2bf6baSSubbaraya Sundeep static void rvu_enable_afvf_intr(struct rvu *rvu)
3280fa2bf6baSSubbaraya Sundeep {
3281fa2bf6baSSubbaraya Sundeep 	int vfs = rvu->vfs;
3282fa2bf6baSSubbaraya Sundeep 
32839bd6caf3STomasz Duszynski 	if (is_cn20k(rvu->pdev))
32849bd6caf3STomasz Duszynski 		return cn20k_rvu_enable_afvf_intr(rvu, vfs);
32859bd6caf3STomasz Duszynski 
32869bd6caf3STomasz Duszynski 	/* Clear any pending interrupts and enable AF VF interrupts for
32879bd6caf3STomasz Duszynski 	 * the first 64 VFs.
32889bd6caf3STomasz Duszynski 	 */
32899bd6caf3STomasz Duszynski 	/* Mbox */
32909bd6caf3STomasz Duszynski 	rvupf_write64(rvu, RVU_PF_VFPF_MBOX_INTX(0), INTR_MASK(vfs));
32919bd6caf3STomasz Duszynski 	rvupf_write64(rvu, RVU_PF_VFPF_MBOX_INT_ENA_W1SX(0), INTR_MASK(vfs));
32929bd6caf3STomasz Duszynski 
32939bd6caf3STomasz Duszynski 	/* FLR */
3294465ed9c1SSunil Goutham 	rvupf_write64(rvu, RVU_PF_VFFLR_INTX(0), INTR_MASK(vfs));
32959bd6caf3STomasz Duszynski 	rvupf_write64(rvu, RVU_PF_VFFLR_INT_ENA_W1SX(0), INTR_MASK(vfs));
32969bd6caf3STomasz Duszynski 	rvupf_write64(rvu, RVU_PF_VFME_INT_ENA_W1SX(0), INTR_MASK(vfs));
32979bd6caf3STomasz Duszynski 
32989bd6caf3STomasz Duszynski 	/* Same for remaining VFs, if any. */
32999bd6caf3STomasz Duszynski 	if (vfs <= 64)
3300465ed9c1SSunil Goutham 		return;
33019bd6caf3STomasz Duszynski 
33029bd6caf3STomasz Duszynski 	rvupf_write64(rvu, RVU_PF_VFPF_MBOX_INTX(1), INTR_MASK(vfs - 64));
33039bd6caf3STomasz Duszynski 	rvupf_write64(rvu, RVU_PF_VFPF_MBOX_INT_ENA_W1SX(1),
33049bd6caf3STomasz Duszynski 		      INTR_MASK(vfs - 64));
33059bd6caf3STomasz Duszynski 
33069bd6caf3STomasz Duszynski 	rvupf_write64(rvu, RVU_PF_VFFLR_INTX(1), INTR_MASK(vfs - 64));
33079bd6caf3STomasz Duszynski 	rvupf_write64(rvu, RVU_PF_VFFLR_INT_ENA_W1SX(1), INTR_MASK(vfs - 64));
33089bd6caf3STomasz Duszynski 	rvupf_write64(rvu, RVU_PF_VFME_INT_ENA_W1SX(1), INTR_MASK(vfs - 64));
33099bd6caf3STomasz Duszynski }
3310465ed9c1SSunil Goutham 
rvu_get_num_lbk_chans(void)33119bd6caf3STomasz Duszynski int rvu_get_num_lbk_chans(void)
33129bd6caf3STomasz Duszynski {
33139bd6caf3STomasz Duszynski 	struct pci_dev *pdev;
33149bd6caf3STomasz Duszynski 	void __iomem *base;
331523705adbSVamsi Attunuru 	int ret = -EIO;
331623705adbSVamsi Attunuru 
331723705adbSVamsi Attunuru 	pdev = pci_get_device(PCI_VENDOR_ID_CAVIUM, PCI_DEVID_OCTEONTX2_LBK,
331823705adbSVamsi Attunuru 			      NULL);
331923705adbSVamsi Attunuru 	if (!pdev)
332023705adbSVamsi Attunuru 		goto err;
33213a724415SStanislaw Kardach 
33223a724415SStanislaw Kardach 	base = pci_ioremap_bar(pdev, 0);
332323705adbSVamsi Attunuru 	if (!base)
332423705adbSVamsi Attunuru 		goto err_put;
332554494aa5SSunil Goutham 
332654494aa5SSunil Goutham 	/* Read number of available LBK channels from LBK(0)_CONST register. */
332754494aa5SSunil Goutham 	ret = (readq(base + 0x10) >> 32) & 0xffff;
332854494aa5SSunil Goutham 	iounmap(base);
332954494aa5SSunil Goutham err_put:
333054494aa5SSunil Goutham 	pci_dev_put(pdev);
333154494aa5SSunil Goutham err:
333254494aa5SSunil Goutham 	return ret;
333354494aa5SSunil Goutham }
333454494aa5SSunil Goutham 
rvu_enable_sriov(struct rvu * rvu)333554d55781SSunil Goutham static int rvu_enable_sriov(struct rvu *rvu)
333654d55781SSunil Goutham {
333754d55781SSunil Goutham 	struct pci_dev *pdev = rvu->pdev;
333854d55781SSunil Goutham 	int err, chans, vfs;
333954d55781SSunil Goutham 	int pos = 0;
334054d55781SSunil Goutham 
334154494aa5SSunil Goutham 	if (!rvu_afvf_msix_vectors_num_ok(rvu)) {
334254494aa5SSunil Goutham 		dev_warn(&pdev->dev,
334354494aa5SSunil Goutham 			 "Skipping SRIOV enablement since not enough IRQs are available\n");
334454494aa5SSunil Goutham 		return 0;
334554494aa5SSunil Goutham 	}
334654494aa5SSunil Goutham 
334754494aa5SSunil Goutham 	/* Get RVU VFs device id */
334854494aa5SSunil Goutham 	pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
334954494aa5SSunil Goutham 	if (!pos)
335054494aa5SSunil Goutham 		return 0;
335154494aa5SSunil Goutham 	pci_read_config_word(pdev, pos + PCI_SRIOV_VF_DID, &rvu->vf_devid);
335254494aa5SSunil Goutham 
335354494aa5SSunil Goutham 	chans = rvu_get_num_lbk_chans();
335454494aa5SSunil Goutham 	if (chans < 0)
335554494aa5SSunil Goutham 		return chans;
335654494aa5SSunil Goutham 
3357549c35ecSSunil Goutham 	vfs = pci_sriov_get_totalvfs(pdev);
335854494aa5SSunil Goutham 
3359549c35ecSSunil Goutham 	/* Limit VFs in case we have more VFs than LBK channels available. */
336054494aa5SSunil Goutham 	if (vfs > chans)
336154494aa5SSunil Goutham 		vfs = chans;
336254494aa5SSunil Goutham 
3363549c35ecSSunil Goutham 	if (!vfs)
336454494aa5SSunil Goutham 		return 0;
33654086f2a0SAleksey Makarov 
33664086f2a0SAleksey Makarov 	/* LBK channel number 63 is used for switching packets between
33674086f2a0SAleksey Makarov 	 * CGX mapped VFs. Hence limit LBK pairs till 62 only.
33687709fbd4SSai Krishna 	 */
33694086f2a0SAleksey Makarov 	if (vfs > 62)
33704086f2a0SAleksey Makarov 		vfs = 62;
33714086f2a0SAleksey Makarov 
33724086f2a0SAleksey Makarov 	/* Save VFs number for reference in VF interrupts handlers.
337354494aa5SSunil Goutham 	 * Since interrupts might start arriving during SRIOV enablement
337454494aa5SSunil Goutham 	 * ordinary API cannot be used to get number of enabled VFs.
337554494aa5SSunil Goutham 	 */
337654494aa5SSunil Goutham 	rvu->vfs = vfs;
337754494aa5SSunil Goutham 
337854494aa5SSunil Goutham 	err = rvu_mbox_init(rvu, &rvu->afvf_wq_info, TYPE_AFVF, vfs,
33794086f2a0SAleksey Makarov 			    rvu_afvf_mbox_handler, rvu_afvf_mbox_up_handler);
338054494aa5SSunil Goutham 	if (err)
338154494aa5SSunil Goutham 		return err;
338223705adbSVamsi Attunuru 
338323705adbSVamsi Attunuru 	rvu_enable_afvf_intr(rvu);
338423705adbSVamsi Attunuru 	/* Make sure IRQs are enabled before SRIOV. */
338554d55781SSunil Goutham 	mb();
338654d55781SSunil Goutham 
338754d55781SSunil Goutham 	err = pci_enable_sriov(pdev, vfs);
338854d55781SSunil Goutham 	if (err) {
338954d55781SSunil Goutham 		rvu_disable_afvf_intr(rvu);
33905d9b976dSSunil Goutham 		rvu_mbox_destroy(&rvu->afvf_wq_info);
33915d9b976dSSunil Goutham 		return err;
33921054a622SSunil Goutham 	}
33931054a622SSunil Goutham 
33944086f2a0SAleksey Makarov 	return 0;
33951054a622SSunil Goutham }
33969bdc47a6STomasz Duszynski 
rvu_disable_sriov(struct rvu * rvu)33979bdc47a6STomasz Duszynski static void rvu_disable_sriov(struct rvu *rvu)
33989bdc47a6STomasz Duszynski {
33999bdc47a6STomasz Duszynski 	rvu_disable_afvf_intr(rvu);
3400a83bdadaSSunil Goutham 	rvu_mbox_destroy(&rvu->afvf_wq_info);
3401a83bdadaSSunil Goutham 	pci_disable_sriov(rvu->pdev);
34027304ac45SSunil Goutham }
3403a83bdadaSSunil Goutham 
rvu_update_module_params(struct rvu * rvu)34047304ac45SSunil Goutham static void rvu_update_module_params(struct rvu *rvu)
34059fe4ebf7SGeetha sowjanya {
3406a83bdadaSSunil Goutham 	const char *default_pfl_name = "default";
3407a83bdadaSSunil Goutham 
340844990aaaSLinu Cherian 	strscpy(rvu->mkex_pfl_name,
3409a83bdadaSSunil Goutham 		mkex_profile ? mkex_profile : default_pfl_name, MKEX_NAME_LEN);
34107304ac45SSunil Goutham 	strscpy(rvu->kpu_pfl_name,
34119fe4ebf7SGeetha sowjanya 		kpu_profile ? kpu_profile : default_pfl_name, KPU_NAME_LEN);
3412a83bdadaSSunil Goutham }
3413a83bdadaSSunil Goutham 
rvu_probe(struct pci_dev * pdev,const struct pci_device_id * id)34149fe4ebf7SGeetha sowjanya static int rvu_probe(struct pci_dev *pdev, const struct pci_device_id *id)
3415a83bdadaSSunil Goutham {
34169fe4ebf7SGeetha sowjanya 	struct device *dev = &pdev->dev;
3417fae06da4SGeorge Cherian 	struct rvu *rvu;
3418a83bdadaSSunil Goutham 	int    err;
3419a83bdadaSSunil Goutham 
3420fae06da4SGeorge Cherian 	rvu = devm_kzalloc(dev, sizeof(*rvu), GFP_KERNEL);
3421a83bdadaSSunil Goutham 	if (!rvu)
3422fae06da4SGeorge Cherian 		return -ENOMEM;
34238315f9b2SSunil Goutham 
34248315f9b2SSunil Goutham 	rvu->hw = devm_kzalloc(dev, sizeof(struct rvu_hwinfo), GFP_KERNEL);
34259bd6caf3STomasz Duszynski 	if (!rvu->hw) {
34269bd6caf3STomasz Duszynski 		devm_kfree(dev, rvu);
3427a83bdadaSSunil Goutham 		return -ENOMEM;
3428a83bdadaSSunil Goutham 	}
3429fae06da4SGeorge Cherian 
3430a83bdadaSSunil Goutham 	pci_set_drvdata(pdev, rvu);
34319bd6caf3STomasz Duszynski 	rvu->pdev = pdev;
343223205e6dSChristina Jacob 	rvu->dev = &pdev->dev;
343323205e6dSChristina Jacob 
343423205e6dSChristina Jacob 	err = pci_enable_device(pdev);
343523109f8dSSubbaraya Sundeep 	if (err) {
343623109f8dSSubbaraya Sundeep 		dev_err(dev, "Failed to enable PCI device\n");
3437e266f663SSubbaraya Sundeep 		goto err_freemem;
3438bdf79b12SSai Krishna 	}
343999bbc4aeSYi Guo 
3440e266f663SSubbaraya Sundeep 	err = pci_request_regions(pdev, DRV_NAME);
344154494aa5SSunil Goutham 	if (err) {
3442fae06da4SGeorge Cherian 		dev_err(dev, "PCI request regions failed 0x%x\n", err);
3443fae06da4SGeorge Cherian 		goto err_disable_device;
34449bd6caf3STomasz Duszynski 	}
34459bd6caf3STomasz Duszynski 
34469fe4ebf7SGeetha sowjanya 	err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
34479fe4ebf7SGeetha sowjanya 	if (err) {
34487304ac45SSunil Goutham 		dev_err(dev, "DMA mask config failed, abort\n");
34499bdc47a6STomasz Duszynski 		goto err_release_regions;
34507304ac45SSunil Goutham 	}
345144990aaaSLinu Cherian 
34524f4eebf2SLinu Cherian 	pci_set_master(pdev);
34536c635f78SGeetha sowjanya 
34547304ac45SSunil Goutham 	rvu->ptp = ptp_get();
34557304ac45SSunil Goutham 	if (IS_ERR(rvu->ptp)) {
34568315f9b2SSunil Goutham 		err = PTR_ERR(rvu->ptp);
34574086f2a0SAleksey Makarov 		if (err)
34584086f2a0SAleksey Makarov 			goto err_release_regions;
345954494aa5SSunil Goutham 		rvu->ptp = NULL;
346054494aa5SSunil Goutham 	}
346154494aa5SSunil Goutham 
346254494aa5SSunil Goutham 	/* Map Admin function CSRs */
346354494aa5SSunil Goutham 	rvu->afreg_base = pcim_iomap(pdev, PCI_AF_REG_BAR_NUM, 0);
346454494aa5SSunil Goutham 	rvu->pfreg_base = pcim_iomap(pdev, PCI_PF_REG_BAR_NUM, 0);
346554d55781SSunil Goutham 	if (!rvu->afreg_base || !rvu->pfreg_base) {
346654494aa5SSunil Goutham 		dev_err(dev, "Unable to map admin function CSRs, aborting\n");
346754494aa5SSunil Goutham 		err = -ENOMEM;
346854494aa5SSunil Goutham 		goto err_put_ptp;
346954494aa5SSunil Goutham 	}
347054494aa5SSunil Goutham 
347154494aa5SSunil Goutham 	/* Store module params in rvu structure */
347254494aa5SSunil Goutham 	rvu_update_module_params(rvu);
347354494aa5SSunil Goutham 
347423205e6dSChristina Jacob 	/* Check which blocks the HW supports */
3475fae06da4SGeorge Cherian 	rvu_check_block_implemented(rvu);
3476ae2619ddSGeetha sowjanya 
34779fe4ebf7SGeetha sowjanya 	rvu_reset_all_blocks(rvu);
347844990aaaSLinu Cherian 
34794f4eebf2SLinu Cherian 	rvu_setup_hw_capabilities(rvu);
34806c635f78SGeetha sowjanya 
34819bdc47a6STomasz Duszynski 	err = rvu_setup_hw_resources(rvu);
34829bd6caf3STomasz Duszynski 	if (err)
34831054a622SSunil Goutham 		goto err_put_ptp;
34841054a622SSunil Goutham 
34858315f9b2SSunil Goutham 	/* Init mailbox btw AF and PFs */
34864086f2a0SAleksey Makarov 	err = rvu_mbox_init(rvu, &rvu->afpf_wq_info, TYPE_AFPF,
348754494aa5SSunil Goutham 			    rvu->hw->total_pfs, rvu_afpf_mbox_handler,
348854494aa5SSunil Goutham 			    rvu_afpf_mbox_up_handler);
348954494aa5SSunil Goutham 	if (err) {
349054494aa5SSunil Goutham 		dev_err(dev, "%s: Failed to initialize mbox\n", __func__);
349154d55781SSunil Goutham 		goto err_hwsetup;
349254494aa5SSunil Goutham 	}
349354494aa5SSunil Goutham 
349454494aa5SSunil Goutham 	err = rvu_flr_init(rvu);
349554494aa5SSunil Goutham 	if (err) {
349654494aa5SSunil Goutham 		dev_err(dev, "%s: Failed to initialize flr\n", __func__);
349754494aa5SSunil Goutham 		goto err_mbox;
349854494aa5SSunil Goutham 	}
349954494aa5SSunil Goutham 
350054494aa5SSunil Goutham 	err = rvu_register_interrupts(rvu);
350154494aa5SSunil Goutham 	if (err) {
350254494aa5SSunil Goutham 		dev_err(dev, "%s: Failed to register interrupts\n", __func__);
350354494aa5SSunil Goutham 		goto err_flr;
35048e22f040SSunil Goutham 	}
35058e22f040SSunil Goutham 
350654494aa5SSunil Goutham 	err = rvu_register_dl(rvu);
350754494aa5SSunil Goutham 	if (err) {
35088e22f040SSunil Goutham 		dev_err(dev, "%s: Failed to register devlink\n", __func__);
35098e22f040SSunil Goutham 		goto err_irq;
35108e22f040SSunil Goutham 	}
35118e22f040SSunil Goutham 
35124086f2a0SAleksey Makarov 	rvu_setup_rvum_blk_revid(rvu);
35134086f2a0SAleksey Makarov 
35144086f2a0SAleksey Makarov 	/* Enable AF's VFs (if any) */
35154086f2a0SAleksey Makarov 	err = rvu_enable_sriov(rvu);
3516ca7f49ffSGeetha sowjanya 	if (err) {
3517ca7f49ffSGeetha sowjanya 		dev_err(dev, "%s: Failed to enable sriov\n", __func__);
3518ca7f49ffSGeetha sowjanya 		goto err_dl;
3519ca7f49ffSGeetha sowjanya 	}
35208e22f040SSunil Goutham 
35218e22f040SSunil Goutham 	/* Initialize debugfs */
35224086f2a0SAleksey Makarov 	rvu_dbg_init(rvu);
35234086f2a0SAleksey Makarov 
35244086f2a0SAleksey Makarov 	mutex_init(&rvu->rswitch.switch_lock);
35254086f2a0SAleksey Makarov 
3526ca7f49ffSGeetha sowjanya 	if (rvu->fwdata)
3527ca7f49ffSGeetha sowjanya 		ptp_start(rvu, rvu->fwdata->sclk, rvu->fwdata->ptp_ext_clk_rate,
35284086f2a0SAleksey Makarov 			  rvu->fwdata->ptp_ext_tstamp);
35294086f2a0SAleksey Makarov 
35308e22f040SSunil Goutham 	/* Alloc CINT and QINT memory */
35318e22f040SSunil Goutham 	rvu_alloc_cint_qint_mem(rvu, &rvu->pf[RVU_AFPF], BLKADDR_NIX0,
35328e22f040SSunil Goutham 				(rvu->hw->block[BLKADDR_NIX0].lf.max));
353354494aa5SSunil Goutham 	return 0;
353454494aa5SSunil Goutham err_dl:
353554494aa5SSunil Goutham 	rvu_unregister_dl(rvu);
353654494aa5SSunil Goutham err_irq:
353754494aa5SSunil Goutham 	rvu_unregister_interrupts(rvu);
3538ca7f49ffSGeetha sowjanya err_flr:
35394086f2a0SAleksey Makarov 	rvu_flr_wq_destroy(rvu);
35408e22f040SSunil Goutham err_mbox:
354154494aa5SSunil Goutham 	rvu_mbox_destroy(&rvu->afpf_wq_info);
354254494aa5SSunil Goutham err_hwsetup:
354354494aa5SSunil Goutham 	rvu_cgx_exit(rvu);
354454494aa5SSunil Goutham 	rvu_fwdata_exit(rvu);
3545 	rvu_mcs_exit(rvu);
3546 	rvu_reset_all_blocks(rvu);
3547 	rvu_free_hw_resources(rvu);
3548 	rvu_clear_rvum_blk_revid(rvu);
3549 err_put_ptp:
3550 	ptp_put(rvu->ptp);
3551 err_release_regions:
3552 	pci_release_regions(pdev);
3553 err_disable_device:
3554 	pci_disable_device(pdev);
3555 err_freemem:
3556 	pci_set_drvdata(pdev, NULL);
3557 	devm_kfree(&pdev->dev, rvu->hw);
3558 	devm_kfree(dev, rvu);
3559 	return err;
3560 }
3561 
rvu_remove(struct pci_dev * pdev)3562 static void rvu_remove(struct pci_dev *pdev)
3563 {
3564 	struct rvu *rvu = pci_get_drvdata(pdev);
3565 
3566 	rvu_dbg_exit(rvu);
3567 	rvu_unregister_dl(rvu);
3568 	rvu_unregister_interrupts(rvu);
3569 	rvu_flr_wq_destroy(rvu);
3570 	rvu_cgx_exit(rvu);
3571 	rvu_fwdata_exit(rvu);
3572 	rvu_mcs_exit(rvu);
3573 	rvu_mbox_destroy(&rvu->afpf_wq_info);
3574 	rvu_disable_sriov(rvu);
3575 	rvu_reset_all_blocks(rvu);
3576 	rvu_free_hw_resources(rvu);
3577 	rvu_clear_rvum_blk_revid(rvu);
3578 	ptp_put(rvu->ptp);
3579 	pci_release_regions(pdev);
3580 	pci_disable_device(pdev);
3581 	pci_set_drvdata(pdev, NULL);
3582 
3583 	devm_kfree(&pdev->dev, rvu->hw);
3584 	if (is_cn20k(rvu->pdev))
3585 		cn20k_free_mbox_memory(rvu);
3586 	kfree(rvu->ng_rvu);
3587 	devm_kfree(&pdev->dev, rvu);
3588 }
3589 
3590 static struct pci_driver rvu_driver = {
3591 	.name = DRV_NAME,
3592 	.id_table = rvu_id_table,
3593 	.probe = rvu_probe,
3594 	.remove = rvu_remove,
3595 };
3596 
rvu_init_module(void)3597 static int __init rvu_init_module(void)
3598 {
3599 	int err;
3600 
3601 	pr_info("%s: %s\n", DRV_NAME, DRV_STRING);
3602 
3603 	err = pci_register_driver(&cgx_driver);
3604 	if (err < 0)
3605 		return err;
3606 
3607 	err = pci_register_driver(&ptp_driver);
3608 	if (err < 0)
3609 		goto ptp_err;
3610 
3611 	err = pci_register_driver(&mcs_driver);
3612 	if (err < 0)
3613 		goto mcs_err;
3614 
3615 	err =  pci_register_driver(&rvu_driver);
3616 	if (err < 0)
3617 		goto rvu_err;
3618 
3619 	return 0;
3620 rvu_err:
3621 	pci_unregister_driver(&mcs_driver);
3622 mcs_err:
3623 	pci_unregister_driver(&ptp_driver);
3624 ptp_err:
3625 	pci_unregister_driver(&cgx_driver);
3626 
3627 	return err;
3628 }
3629 
rvu_cleanup_module(void)3630 static void __exit rvu_cleanup_module(void)
3631 {
3632 	pci_unregister_driver(&rvu_driver);
3633 	pci_unregister_driver(&mcs_driver);
3634 	pci_unregister_driver(&ptp_driver);
3635 	pci_unregister_driver(&cgx_driver);
3636 }
3637 
3638 module_init(rvu_init_module);
3639 module_exit(rvu_cleanup_module);
3640