156a62fc8SJesse Brandeburg /*******************************************************************************
256a62fc8SJesse Brandeburg  *
356a62fc8SJesse Brandeburg  * Intel Ethernet Controller XL710 Family Linux Driver
4dc641b73SGreg Rose  * Copyright(c) 2013 - 2014 Intel Corporation.
556a62fc8SJesse Brandeburg  *
656a62fc8SJesse Brandeburg  * This program is free software; you can redistribute it and/or modify it
756a62fc8SJesse Brandeburg  * under the terms and conditions of the GNU General Public License,
856a62fc8SJesse Brandeburg  * version 2, as published by the Free Software Foundation.
956a62fc8SJesse Brandeburg  *
1056a62fc8SJesse Brandeburg  * This program is distributed in the hope it will be useful, but WITHOUT
1156a62fc8SJesse Brandeburg  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1256a62fc8SJesse Brandeburg  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
1356a62fc8SJesse Brandeburg  * more details.
1456a62fc8SJesse Brandeburg  *
15dc641b73SGreg Rose  * You should have received a copy of the GNU General Public License along
16dc641b73SGreg Rose  * with this program.  If not, see <http://www.gnu.org/licenses/>.
1756a62fc8SJesse Brandeburg  *
1856a62fc8SJesse Brandeburg  * The full GNU General Public License is included in this distribution in
1956a62fc8SJesse Brandeburg  * the file called "COPYING".
2056a62fc8SJesse Brandeburg  *
2156a62fc8SJesse Brandeburg  * Contact Information:
2256a62fc8SJesse Brandeburg  * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
2356a62fc8SJesse Brandeburg  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
2456a62fc8SJesse Brandeburg  *
2556a62fc8SJesse Brandeburg  ******************************************************************************/
2656a62fc8SJesse Brandeburg 
2756a62fc8SJesse Brandeburg #ifndef _I40E_HMC_H_
2856a62fc8SJesse Brandeburg #define _I40E_HMC_H_
2956a62fc8SJesse Brandeburg 
3056a62fc8SJesse Brandeburg #define I40E_HMC_MAX_BP_COUNT 512
3156a62fc8SJesse Brandeburg 
3256a62fc8SJesse Brandeburg /* forward-declare the HW struct for the compiler */
3356a62fc8SJesse Brandeburg struct i40e_hw;
3456a62fc8SJesse Brandeburg 
3556a62fc8SJesse Brandeburg #define I40E_HMC_INFO_SIGNATURE		0x484D5347 /* HMSG */
3656a62fc8SJesse Brandeburg #define I40E_HMC_PD_CNT_IN_SD		512
3756a62fc8SJesse Brandeburg #define I40E_HMC_DIRECT_BP_SIZE		0x200000 /* 2M */
3856a62fc8SJesse Brandeburg #define I40E_HMC_PAGED_BP_SIZE		4096
3956a62fc8SJesse Brandeburg #define I40E_HMC_PD_BP_BUF_ALIGNMENT	4096
4056a62fc8SJesse Brandeburg #define I40E_FIRST_VF_FPM_ID		16
4156a62fc8SJesse Brandeburg 
4256a62fc8SJesse Brandeburg struct i40e_hmc_obj_info {
4356a62fc8SJesse Brandeburg 	u64 base;	/* base addr in FPM */
4456a62fc8SJesse Brandeburg 	u32 max_cnt;	/* max count available for this hmc func */
4556a62fc8SJesse Brandeburg 	u32 cnt;	/* count of objects driver actually wants to create */
4656a62fc8SJesse Brandeburg 	u64 size;	/* size in bytes of one object */
4756a62fc8SJesse Brandeburg };
4856a62fc8SJesse Brandeburg 
4956a62fc8SJesse Brandeburg enum i40e_sd_entry_type {
5056a62fc8SJesse Brandeburg 	I40E_SD_TYPE_INVALID = 0,
5156a62fc8SJesse Brandeburg 	I40E_SD_TYPE_PAGED   = 1,
5256a62fc8SJesse Brandeburg 	I40E_SD_TYPE_DIRECT  = 2
5356a62fc8SJesse Brandeburg };
5456a62fc8SJesse Brandeburg 
5556a62fc8SJesse Brandeburg struct i40e_hmc_bp {
5656a62fc8SJesse Brandeburg 	enum i40e_sd_entry_type entry_type;
5756a62fc8SJesse Brandeburg 	struct i40e_dma_mem addr; /* populate to be used by hw */
5856a62fc8SJesse Brandeburg 	u32 sd_pd_index;
5956a62fc8SJesse Brandeburg 	u32 ref_cnt;
6056a62fc8SJesse Brandeburg };
6156a62fc8SJesse Brandeburg 
6256a62fc8SJesse Brandeburg struct i40e_hmc_pd_entry {
6356a62fc8SJesse Brandeburg 	struct i40e_hmc_bp bp;
6456a62fc8SJesse Brandeburg 	u32 sd_index;
653bbf0faaSFaisal Latif 	bool rsrc_pg;
6656a62fc8SJesse Brandeburg 	bool valid;
6756a62fc8SJesse Brandeburg };
6856a62fc8SJesse Brandeburg 
6956a62fc8SJesse Brandeburg struct i40e_hmc_pd_table {
7056a62fc8SJesse Brandeburg 	struct i40e_dma_mem pd_page_addr; /* populate to be used by hw */
7156a62fc8SJesse Brandeburg 	struct i40e_hmc_pd_entry  *pd_entry; /* [512] for sw book keeping */
7256a62fc8SJesse Brandeburg 	struct i40e_virt_mem pd_entry_virt_mem; /* virt mem for pd_entry */
7356a62fc8SJesse Brandeburg 
7456a62fc8SJesse Brandeburg 	u32 ref_cnt;
7556a62fc8SJesse Brandeburg 	u32 sd_index;
7656a62fc8SJesse Brandeburg };
7756a62fc8SJesse Brandeburg 
7856a62fc8SJesse Brandeburg struct i40e_hmc_sd_entry {
7956a62fc8SJesse Brandeburg 	enum i40e_sd_entry_type entry_type;
8056a62fc8SJesse Brandeburg 	bool valid;
8156a62fc8SJesse Brandeburg 
8256a62fc8SJesse Brandeburg 	union {
8356a62fc8SJesse Brandeburg 		struct i40e_hmc_pd_table pd_table;
8456a62fc8SJesse Brandeburg 		struct i40e_hmc_bp bp;
8556a62fc8SJesse Brandeburg 	} u;
8656a62fc8SJesse Brandeburg };
8756a62fc8SJesse Brandeburg 
8856a62fc8SJesse Brandeburg struct i40e_hmc_sd_table {
8956a62fc8SJesse Brandeburg 	struct i40e_virt_mem addr; /* used to track sd_entry allocations */
9056a62fc8SJesse Brandeburg 	u32 sd_cnt;
9156a62fc8SJesse Brandeburg 	u32 ref_cnt;
9256a62fc8SJesse Brandeburg 	struct i40e_hmc_sd_entry *sd_entry; /* (sd_cnt*512) entries max */
9356a62fc8SJesse Brandeburg };
9456a62fc8SJesse Brandeburg 
9556a62fc8SJesse Brandeburg struct i40e_hmc_info {
9656a62fc8SJesse Brandeburg 	u32 signature;
9756a62fc8SJesse Brandeburg 	/* equals to pci func num for PF and dynamically allocated for VFs */
9856a62fc8SJesse Brandeburg 	u8 hmc_fn_id;
9956a62fc8SJesse Brandeburg 	u16 first_sd_index; /* index of the first available SD */
10056a62fc8SJesse Brandeburg 
10156a62fc8SJesse Brandeburg 	/* hmc objects */
10256a62fc8SJesse Brandeburg 	struct i40e_hmc_obj_info *hmc_obj;
10356a62fc8SJesse Brandeburg 	struct i40e_virt_mem hmc_obj_virt_mem;
10456a62fc8SJesse Brandeburg 	struct i40e_hmc_sd_table sd_table;
10556a62fc8SJesse Brandeburg };
10656a62fc8SJesse Brandeburg 
10756a62fc8SJesse Brandeburg #define I40E_INC_SD_REFCNT(sd_table)	((sd_table)->ref_cnt++)
10856a62fc8SJesse Brandeburg #define I40E_INC_PD_REFCNT(pd_table)	((pd_table)->ref_cnt++)
10956a62fc8SJesse Brandeburg #define I40E_INC_BP_REFCNT(bp)		((bp)->ref_cnt++)
11056a62fc8SJesse Brandeburg 
11156a62fc8SJesse Brandeburg #define I40E_DEC_SD_REFCNT(sd_table)	((sd_table)->ref_cnt--)
11256a62fc8SJesse Brandeburg #define I40E_DEC_PD_REFCNT(pd_table)	((pd_table)->ref_cnt--)
11356a62fc8SJesse Brandeburg #define I40E_DEC_BP_REFCNT(bp)		((bp)->ref_cnt--)
11456a62fc8SJesse Brandeburg 
11556a62fc8SJesse Brandeburg /**
11656a62fc8SJesse Brandeburg  * I40E_SET_PF_SD_ENTRY - marks the sd entry as valid in the hardware
11756a62fc8SJesse Brandeburg  * @hw: pointer to our hw struct
11856a62fc8SJesse Brandeburg  * @pa: pointer to physical address
11956a62fc8SJesse Brandeburg  * @sd_index: segment descriptor index
12056a62fc8SJesse Brandeburg  * @type: if sd entry is direct or paged
12156a62fc8SJesse Brandeburg  **/
12256a62fc8SJesse Brandeburg #define I40E_SET_PF_SD_ENTRY(hw, pa, sd_index, type)			\
12356a62fc8SJesse Brandeburg {									\
12456a62fc8SJesse Brandeburg 	u32 val1, val2, val3;						\
12556a62fc8SJesse Brandeburg 	val1 = (u32)(upper_32_bits(pa));				\
12656a62fc8SJesse Brandeburg 	val2 = (u32)(pa) | (I40E_HMC_MAX_BP_COUNT <<			\
12756a62fc8SJesse Brandeburg 		 I40E_PFHMC_SDDATALOW_PMSDBPCOUNT_SHIFT) |		\
12856a62fc8SJesse Brandeburg 		((((type) == I40E_SD_TYPE_PAGED) ? 0 : 1) <<		\
12956a62fc8SJesse Brandeburg 		I40E_PFHMC_SDDATALOW_PMSDTYPE_SHIFT) |			\
13056a62fc8SJesse Brandeburg 		(1 << I40E_PFHMC_SDDATALOW_PMSDVALID_SHIFT);		\
13130fe8ad3SPaul M Stillwell Jr 	val3 = (sd_index) | (1u << I40E_PFHMC_SDCMD_PMSDWR_SHIFT);	\
13256a62fc8SJesse Brandeburg 	wr32((hw), I40E_PFHMC_SDDATAHIGH, val1);			\
13356a62fc8SJesse Brandeburg 	wr32((hw), I40E_PFHMC_SDDATALOW, val2);				\
13456a62fc8SJesse Brandeburg 	wr32((hw), I40E_PFHMC_SDCMD, val3);				\
13556a62fc8SJesse Brandeburg }
13656a62fc8SJesse Brandeburg 
13756a62fc8SJesse Brandeburg /**
13856a62fc8SJesse Brandeburg  * I40E_CLEAR_PF_SD_ENTRY - marks the sd entry as invalid in the hardware
13956a62fc8SJesse Brandeburg  * @hw: pointer to our hw struct
14056a62fc8SJesse Brandeburg  * @sd_index: segment descriptor index
14156a62fc8SJesse Brandeburg  * @type: if sd entry is direct or paged
14256a62fc8SJesse Brandeburg  **/
14356a62fc8SJesse Brandeburg #define I40E_CLEAR_PF_SD_ENTRY(hw, sd_index, type)			\
14456a62fc8SJesse Brandeburg {									\
14556a62fc8SJesse Brandeburg 	u32 val2, val3;							\
14656a62fc8SJesse Brandeburg 	val2 = (I40E_HMC_MAX_BP_COUNT <<				\
14756a62fc8SJesse Brandeburg 		I40E_PFHMC_SDDATALOW_PMSDBPCOUNT_SHIFT) |		\
14856a62fc8SJesse Brandeburg 		((((type) == I40E_SD_TYPE_PAGED) ? 0 : 1) <<		\
14956a62fc8SJesse Brandeburg 		I40E_PFHMC_SDDATALOW_PMSDTYPE_SHIFT);			\
15030fe8ad3SPaul M Stillwell Jr 	val3 = (sd_index) | (1u << I40E_PFHMC_SDCMD_PMSDWR_SHIFT);	\
15156a62fc8SJesse Brandeburg 	wr32((hw), I40E_PFHMC_SDDATAHIGH, 0);				\
15256a62fc8SJesse Brandeburg 	wr32((hw), I40E_PFHMC_SDDATALOW, val2);				\
15356a62fc8SJesse Brandeburg 	wr32((hw), I40E_PFHMC_SDCMD, val3);				\
15456a62fc8SJesse Brandeburg }
15556a62fc8SJesse Brandeburg 
15656a62fc8SJesse Brandeburg /**
15756a62fc8SJesse Brandeburg  * I40E_INVALIDATE_PF_HMC_PD - Invalidates the pd cache in the hardware
15856a62fc8SJesse Brandeburg  * @hw: pointer to our hw struct
15956a62fc8SJesse Brandeburg  * @sd_idx: segment descriptor index
16056a62fc8SJesse Brandeburg  * @pd_idx: page descriptor index
16156a62fc8SJesse Brandeburg  **/
16256a62fc8SJesse Brandeburg #define I40E_INVALIDATE_PF_HMC_PD(hw, sd_idx, pd_idx)			\
16356a62fc8SJesse Brandeburg 	wr32((hw), I40E_PFHMC_PDINV,					\
16456a62fc8SJesse Brandeburg 	    (((sd_idx) << I40E_PFHMC_PDINV_PMSDIDX_SHIFT) |		\
16556a62fc8SJesse Brandeburg 	     ((pd_idx) << I40E_PFHMC_PDINV_PMPDIDX_SHIFT)))
16656a62fc8SJesse Brandeburg 
16756a62fc8SJesse Brandeburg /**
16856a62fc8SJesse Brandeburg  * I40E_FIND_SD_INDEX_LIMIT - finds segment descriptor index limit
16956a62fc8SJesse Brandeburg  * @hmc_info: pointer to the HMC configuration information structure
17056a62fc8SJesse Brandeburg  * @type: type of HMC resources we're searching
17156a62fc8SJesse Brandeburg  * @index: starting index for the object
17256a62fc8SJesse Brandeburg  * @cnt: number of objects we're trying to create
17356a62fc8SJesse Brandeburg  * @sd_idx: pointer to return index of the segment descriptor in question
17456a62fc8SJesse Brandeburg  * @sd_limit: pointer to return the maximum number of segment descriptors
17556a62fc8SJesse Brandeburg  *
17656a62fc8SJesse Brandeburg  * This function calculates the segment descriptor index and index limit
17756a62fc8SJesse Brandeburg  * for the resource defined by i40e_hmc_rsrc_type.
17856a62fc8SJesse Brandeburg  **/
17956a62fc8SJesse Brandeburg #define I40E_FIND_SD_INDEX_LIMIT(hmc_info, type, index, cnt, sd_idx, sd_limit)\
18056a62fc8SJesse Brandeburg {									\
18156a62fc8SJesse Brandeburg 	u64 fpm_addr, fpm_limit;					\
18256a62fc8SJesse Brandeburg 	fpm_addr = (hmc_info)->hmc_obj[(type)].base +			\
18356a62fc8SJesse Brandeburg 		   (hmc_info)->hmc_obj[(type)].size * (index);		\
18456a62fc8SJesse Brandeburg 	fpm_limit = fpm_addr + (hmc_info)->hmc_obj[(type)].size * (cnt);\
18556a62fc8SJesse Brandeburg 	*(sd_idx) = (u32)(fpm_addr / I40E_HMC_DIRECT_BP_SIZE);		\
18656a62fc8SJesse Brandeburg 	*(sd_limit) = (u32)((fpm_limit - 1) / I40E_HMC_DIRECT_BP_SIZE);	\
18756a62fc8SJesse Brandeburg 	/* add one more to the limit to correct our range */		\
18856a62fc8SJesse Brandeburg 	*(sd_limit) += 1;						\
18956a62fc8SJesse Brandeburg }
19056a62fc8SJesse Brandeburg 
19156a62fc8SJesse Brandeburg /**
19256a62fc8SJesse Brandeburg  * I40E_FIND_PD_INDEX_LIMIT - finds page descriptor index limit
19356a62fc8SJesse Brandeburg  * @hmc_info: pointer to the HMC configuration information struct
19456a62fc8SJesse Brandeburg  * @type: HMC resource type we're examining
19556a62fc8SJesse Brandeburg  * @idx: starting index for the object
19656a62fc8SJesse Brandeburg  * @cnt: number of objects we're trying to create
19756a62fc8SJesse Brandeburg  * @pd_index: pointer to return page descriptor index
19856a62fc8SJesse Brandeburg  * @pd_limit: pointer to return page descriptor index limit
19956a62fc8SJesse Brandeburg  *
20056a62fc8SJesse Brandeburg  * Calculates the page descriptor index and index limit for the resource
20156a62fc8SJesse Brandeburg  * defined by i40e_hmc_rsrc_type.
20256a62fc8SJesse Brandeburg  **/
20356a62fc8SJesse Brandeburg #define I40E_FIND_PD_INDEX_LIMIT(hmc_info, type, idx, cnt, pd_index, pd_limit)\
20456a62fc8SJesse Brandeburg {									\
20556a62fc8SJesse Brandeburg 	u64 fpm_adr, fpm_limit;						\
20656a62fc8SJesse Brandeburg 	fpm_adr = (hmc_info)->hmc_obj[(type)].base +			\
20756a62fc8SJesse Brandeburg 		  (hmc_info)->hmc_obj[(type)].size * (idx);		\
20856a62fc8SJesse Brandeburg 	fpm_limit = fpm_adr + (hmc_info)->hmc_obj[(type)].size * (cnt);	\
20956a62fc8SJesse Brandeburg 	*(pd_index) = (u32)(fpm_adr / I40E_HMC_PAGED_BP_SIZE);		\
21056a62fc8SJesse Brandeburg 	*(pd_limit) = (u32)((fpm_limit - 1) / I40E_HMC_PAGED_BP_SIZE);	\
21156a62fc8SJesse Brandeburg 	/* add one more to the limit to correct our range */		\
21256a62fc8SJesse Brandeburg 	*(pd_limit) += 1;						\
21356a62fc8SJesse Brandeburg }
21456a62fc8SJesse Brandeburg i40e_status i40e_add_sd_table_entry(struct i40e_hw *hw,
21556a62fc8SJesse Brandeburg 					      struct i40e_hmc_info *hmc_info,
21656a62fc8SJesse Brandeburg 					      u32 sd_index,
21756a62fc8SJesse Brandeburg 					      enum i40e_sd_entry_type type,
21856a62fc8SJesse Brandeburg 					      u64 direct_mode_sz);
21956a62fc8SJesse Brandeburg 
22056a62fc8SJesse Brandeburg i40e_status i40e_add_pd_table_entry(struct i40e_hw *hw,
22156a62fc8SJesse Brandeburg 					      struct i40e_hmc_info *hmc_info,
2223bbf0faaSFaisal Latif 					      u32 pd_index,
2233bbf0faaSFaisal Latif 					      struct i40e_dma_mem *rsrc_pg);
22456a62fc8SJesse Brandeburg i40e_status i40e_remove_pd_bp(struct i40e_hw *hw,
22556a62fc8SJesse Brandeburg 					struct i40e_hmc_info *hmc_info,
226467d729aSAnjali Singhai Jain 					u32 idx);
22756a62fc8SJesse Brandeburg i40e_status i40e_prep_remove_sd_bp(struct i40e_hmc_info *hmc_info,
22856a62fc8SJesse Brandeburg 					     u32 idx);
22956a62fc8SJesse Brandeburg i40e_status i40e_remove_sd_bp_new(struct i40e_hw *hw,
23056a62fc8SJesse Brandeburg 					    struct i40e_hmc_info *hmc_info,
23156a62fc8SJesse Brandeburg 					    u32 idx, bool is_pf);
23256a62fc8SJesse Brandeburg i40e_status i40e_prep_remove_pd_page(struct i40e_hmc_info *hmc_info,
23356a62fc8SJesse Brandeburg 					       u32 idx);
23456a62fc8SJesse Brandeburg i40e_status i40e_remove_pd_page_new(struct i40e_hw *hw,
23556a62fc8SJesse Brandeburg 					      struct i40e_hmc_info *hmc_info,
23656a62fc8SJesse Brandeburg 					      u32 idx, bool is_pf);
23756a62fc8SJesse Brandeburg 
23856a62fc8SJesse Brandeburg #endif /* _I40E_HMC_H_ */
239