Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright(c) 2013 - 2018 Intel Corporation. */
0003 
0004 #include "i40e.h"
0005 #include "i40e_osdep.h"
0006 #include "i40e_register.h"
0007 #include "i40e_status.h"
0008 #include "i40e_alloc.h"
0009 #include "i40e_hmc.h"
0010 #include "i40e_type.h"
0011 
0012 /**
0013  * i40e_add_sd_table_entry - Adds a segment descriptor to the table
0014  * @hw: pointer to our hw struct
0015  * @hmc_info: pointer to the HMC configuration information struct
0016  * @sd_index: segment descriptor index to manipulate
0017  * @type: what type of segment descriptor we're manipulating
0018  * @direct_mode_sz: size to alloc in direct mode
0019  **/
0020 i40e_status i40e_add_sd_table_entry(struct i40e_hw *hw,
0021                           struct i40e_hmc_info *hmc_info,
0022                           u32 sd_index,
0023                           enum i40e_sd_entry_type type,
0024                           u64 direct_mode_sz)
0025 {
0026     enum i40e_memory_type mem_type __attribute__((unused));
0027     struct i40e_hmc_sd_entry *sd_entry;
0028     bool dma_mem_alloc_done = false;
0029     struct i40e_dma_mem mem;
0030     i40e_status ret_code = I40E_SUCCESS;
0031     u64 alloc_len;
0032 
0033     if (NULL == hmc_info->sd_table.sd_entry) {
0034         ret_code = I40E_ERR_BAD_PTR;
0035         hw_dbg(hw, "i40e_add_sd_table_entry: bad sd_entry\n");
0036         goto exit;
0037     }
0038 
0039     if (sd_index >= hmc_info->sd_table.sd_cnt) {
0040         ret_code = I40E_ERR_INVALID_SD_INDEX;
0041         hw_dbg(hw, "i40e_add_sd_table_entry: bad sd_index\n");
0042         goto exit;
0043     }
0044 
0045     sd_entry = &hmc_info->sd_table.sd_entry[sd_index];
0046     if (!sd_entry->valid) {
0047         if (I40E_SD_TYPE_PAGED == type) {
0048             mem_type = i40e_mem_pd;
0049             alloc_len = I40E_HMC_PAGED_BP_SIZE;
0050         } else {
0051             mem_type = i40e_mem_bp_jumbo;
0052             alloc_len = direct_mode_sz;
0053         }
0054 
0055         /* allocate a 4K pd page or 2M backing page */
0056         ret_code = i40e_allocate_dma_mem(hw, &mem, mem_type, alloc_len,
0057                          I40E_HMC_PD_BP_BUF_ALIGNMENT);
0058         if (ret_code)
0059             goto exit;
0060         dma_mem_alloc_done = true;
0061         if (I40E_SD_TYPE_PAGED == type) {
0062             ret_code = i40e_allocate_virt_mem(hw,
0063                     &sd_entry->u.pd_table.pd_entry_virt_mem,
0064                     sizeof(struct i40e_hmc_pd_entry) * 512);
0065             if (ret_code)
0066                 goto exit;
0067             sd_entry->u.pd_table.pd_entry =
0068                 (struct i40e_hmc_pd_entry *)
0069                 sd_entry->u.pd_table.pd_entry_virt_mem.va;
0070             sd_entry->u.pd_table.pd_page_addr = mem;
0071         } else {
0072             sd_entry->u.bp.addr = mem;
0073             sd_entry->u.bp.sd_pd_index = sd_index;
0074         }
0075         /* initialize the sd entry */
0076         hmc_info->sd_table.sd_entry[sd_index].entry_type = type;
0077 
0078         /* increment the ref count */
0079         I40E_INC_SD_REFCNT(&hmc_info->sd_table);
0080     }
0081     /* Increment backing page reference count */
0082     if (I40E_SD_TYPE_DIRECT == sd_entry->entry_type)
0083         I40E_INC_BP_REFCNT(&sd_entry->u.bp);
0084 exit:
0085     if (ret_code)
0086         if (dma_mem_alloc_done)
0087             i40e_free_dma_mem(hw, &mem);
0088 
0089     return ret_code;
0090 }
0091 
0092 /**
0093  * i40e_add_pd_table_entry - Adds page descriptor to the specified table
0094  * @hw: pointer to our HW structure
0095  * @hmc_info: pointer to the HMC configuration information structure
0096  * @pd_index: which page descriptor index to manipulate
0097  * @rsrc_pg: if not NULL, use preallocated page instead of allocating new one.
0098  *
0099  * This function:
0100  *  1. Initializes the pd entry
0101  *  2. Adds pd_entry in the pd_table
0102  *  3. Mark the entry valid in i40e_hmc_pd_entry structure
0103  *  4. Initializes the pd_entry's ref count to 1
0104  * assumptions:
0105  *  1. The memory for pd should be pinned down, physically contiguous and
0106  *     aligned on 4K boundary and zeroed memory.
0107  *  2. It should be 4K in size.
0108  **/
0109 i40e_status i40e_add_pd_table_entry(struct i40e_hw *hw,
0110                           struct i40e_hmc_info *hmc_info,
0111                           u32 pd_index,
0112                           struct i40e_dma_mem *rsrc_pg)
0113 {
0114     i40e_status ret_code = 0;
0115     struct i40e_hmc_pd_table *pd_table;
0116     struct i40e_hmc_pd_entry *pd_entry;
0117     struct i40e_dma_mem mem;
0118     struct i40e_dma_mem *page = &mem;
0119     u32 sd_idx, rel_pd_idx;
0120     u64 *pd_addr;
0121     u64 page_desc;
0122 
0123     if (pd_index / I40E_HMC_PD_CNT_IN_SD >= hmc_info->sd_table.sd_cnt) {
0124         ret_code = I40E_ERR_INVALID_PAGE_DESC_INDEX;
0125         hw_dbg(hw, "i40e_add_pd_table_entry: bad pd_index\n");
0126         goto exit;
0127     }
0128 
0129     /* find corresponding sd */
0130     sd_idx = (pd_index / I40E_HMC_PD_CNT_IN_SD);
0131     if (I40E_SD_TYPE_PAGED !=
0132         hmc_info->sd_table.sd_entry[sd_idx].entry_type)
0133         goto exit;
0134 
0135     rel_pd_idx = (pd_index % I40E_HMC_PD_CNT_IN_SD);
0136     pd_table = &hmc_info->sd_table.sd_entry[sd_idx].u.pd_table;
0137     pd_entry = &pd_table->pd_entry[rel_pd_idx];
0138     if (!pd_entry->valid) {
0139         if (rsrc_pg) {
0140             pd_entry->rsrc_pg = true;
0141             page = rsrc_pg;
0142         } else {
0143             /* allocate a 4K backing page */
0144             ret_code = i40e_allocate_dma_mem(hw, page, i40e_mem_bp,
0145                         I40E_HMC_PAGED_BP_SIZE,
0146                         I40E_HMC_PD_BP_BUF_ALIGNMENT);
0147             if (ret_code)
0148                 goto exit;
0149             pd_entry->rsrc_pg = false;
0150         }
0151 
0152         pd_entry->bp.addr = *page;
0153         pd_entry->bp.sd_pd_index = pd_index;
0154         pd_entry->bp.entry_type = I40E_SD_TYPE_PAGED;
0155         /* Set page address and valid bit */
0156         page_desc = page->pa | 0x1;
0157 
0158         pd_addr = (u64 *)pd_table->pd_page_addr.va;
0159         pd_addr += rel_pd_idx;
0160 
0161         /* Add the backing page physical address in the pd entry */
0162         memcpy(pd_addr, &page_desc, sizeof(u64));
0163 
0164         pd_entry->sd_index = sd_idx;
0165         pd_entry->valid = true;
0166         I40E_INC_PD_REFCNT(pd_table);
0167     }
0168     I40E_INC_BP_REFCNT(&pd_entry->bp);
0169 exit:
0170     return ret_code;
0171 }
0172 
0173 /**
0174  * i40e_remove_pd_bp - remove a backing page from a page descriptor
0175  * @hw: pointer to our HW structure
0176  * @hmc_info: pointer to the HMC configuration information structure
0177  * @idx: the page index
0178  *
0179  * This function:
0180  *  1. Marks the entry in pd tabe (for paged address mode) or in sd table
0181  *     (for direct address mode) invalid.
0182  *  2. Write to register PMPDINV to invalidate the backing page in FV cache
0183  *  3. Decrement the ref count for the pd _entry
0184  * assumptions:
0185  *  1. Caller can deallocate the memory used by backing storage after this
0186  *     function returns.
0187  **/
0188 i40e_status i40e_remove_pd_bp(struct i40e_hw *hw,
0189                     struct i40e_hmc_info *hmc_info,
0190                     u32 idx)
0191 {
0192     i40e_status ret_code = 0;
0193     struct i40e_hmc_pd_entry *pd_entry;
0194     struct i40e_hmc_pd_table *pd_table;
0195     struct i40e_hmc_sd_entry *sd_entry;
0196     u32 sd_idx, rel_pd_idx;
0197     u64 *pd_addr;
0198 
0199     /* calculate index */
0200     sd_idx = idx / I40E_HMC_PD_CNT_IN_SD;
0201     rel_pd_idx = idx % I40E_HMC_PD_CNT_IN_SD;
0202     if (sd_idx >= hmc_info->sd_table.sd_cnt) {
0203         ret_code = I40E_ERR_INVALID_PAGE_DESC_INDEX;
0204         hw_dbg(hw, "i40e_remove_pd_bp: bad idx\n");
0205         goto exit;
0206     }
0207     sd_entry = &hmc_info->sd_table.sd_entry[sd_idx];
0208     if (I40E_SD_TYPE_PAGED != sd_entry->entry_type) {
0209         ret_code = I40E_ERR_INVALID_SD_TYPE;
0210         hw_dbg(hw, "i40e_remove_pd_bp: wrong sd_entry type\n");
0211         goto exit;
0212     }
0213     /* get the entry and decrease its ref counter */
0214     pd_table = &hmc_info->sd_table.sd_entry[sd_idx].u.pd_table;
0215     pd_entry = &pd_table->pd_entry[rel_pd_idx];
0216     I40E_DEC_BP_REFCNT(&pd_entry->bp);
0217     if (pd_entry->bp.ref_cnt)
0218         goto exit;
0219 
0220     /* mark the entry invalid */
0221     pd_entry->valid = false;
0222     I40E_DEC_PD_REFCNT(pd_table);
0223     pd_addr = (u64 *)pd_table->pd_page_addr.va;
0224     pd_addr += rel_pd_idx;
0225     memset(pd_addr, 0, sizeof(u64));
0226     I40E_INVALIDATE_PF_HMC_PD(hw, sd_idx, idx);
0227 
0228     /* free memory here */
0229     if (!pd_entry->rsrc_pg)
0230         ret_code = i40e_free_dma_mem(hw, &pd_entry->bp.addr);
0231     if (ret_code)
0232         goto exit;
0233     if (!pd_table->ref_cnt)
0234         i40e_free_virt_mem(hw, &pd_table->pd_entry_virt_mem);
0235 exit:
0236     return ret_code;
0237 }
0238 
0239 /**
0240  * i40e_prep_remove_sd_bp - Prepares to remove a backing page from a sd entry
0241  * @hmc_info: pointer to the HMC configuration information structure
0242  * @idx: the page index
0243  **/
0244 i40e_status i40e_prep_remove_sd_bp(struct i40e_hmc_info *hmc_info,
0245                          u32 idx)
0246 {
0247     i40e_status ret_code = 0;
0248     struct i40e_hmc_sd_entry *sd_entry;
0249 
0250     /* get the entry and decrease its ref counter */
0251     sd_entry = &hmc_info->sd_table.sd_entry[idx];
0252     I40E_DEC_BP_REFCNT(&sd_entry->u.bp);
0253     if (sd_entry->u.bp.ref_cnt) {
0254         ret_code = I40E_ERR_NOT_READY;
0255         goto exit;
0256     }
0257     I40E_DEC_SD_REFCNT(&hmc_info->sd_table);
0258 
0259     /* mark the entry invalid */
0260     sd_entry->valid = false;
0261 exit:
0262     return ret_code;
0263 }
0264 
0265 /**
0266  * i40e_remove_sd_bp_new - Removes a backing page from a segment descriptor
0267  * @hw: pointer to our hw struct
0268  * @hmc_info: pointer to the HMC configuration information structure
0269  * @idx: the page index
0270  * @is_pf: used to distinguish between VF and PF
0271  **/
0272 i40e_status i40e_remove_sd_bp_new(struct i40e_hw *hw,
0273                         struct i40e_hmc_info *hmc_info,
0274                         u32 idx, bool is_pf)
0275 {
0276     struct i40e_hmc_sd_entry *sd_entry;
0277 
0278     if (!is_pf)
0279         return I40E_NOT_SUPPORTED;
0280 
0281     /* get the entry and decrease its ref counter */
0282     sd_entry = &hmc_info->sd_table.sd_entry[idx];
0283     I40E_CLEAR_PF_SD_ENTRY(hw, idx, I40E_SD_TYPE_DIRECT);
0284 
0285     return i40e_free_dma_mem(hw, &sd_entry->u.bp.addr);
0286 }
0287 
0288 /**
0289  * i40e_prep_remove_pd_page - Prepares to remove a PD page from sd entry.
0290  * @hmc_info: pointer to the HMC configuration information structure
0291  * @idx: segment descriptor index to find the relevant page descriptor
0292  **/
0293 i40e_status i40e_prep_remove_pd_page(struct i40e_hmc_info *hmc_info,
0294                            u32 idx)
0295 {
0296     i40e_status ret_code = 0;
0297     struct i40e_hmc_sd_entry *sd_entry;
0298 
0299     sd_entry = &hmc_info->sd_table.sd_entry[idx];
0300 
0301     if (sd_entry->u.pd_table.ref_cnt) {
0302         ret_code = I40E_ERR_NOT_READY;
0303         goto exit;
0304     }
0305 
0306     /* mark the entry invalid */
0307     sd_entry->valid = false;
0308 
0309     I40E_DEC_SD_REFCNT(&hmc_info->sd_table);
0310 exit:
0311     return ret_code;
0312 }
0313 
0314 /**
0315  * i40e_remove_pd_page_new - Removes a PD page from sd entry.
0316  * @hw: pointer to our hw struct
0317  * @hmc_info: pointer to the HMC configuration information structure
0318  * @idx: segment descriptor index to find the relevant page descriptor
0319  * @is_pf: used to distinguish between VF and PF
0320  **/
0321 i40e_status i40e_remove_pd_page_new(struct i40e_hw *hw,
0322                           struct i40e_hmc_info *hmc_info,
0323                           u32 idx, bool is_pf)
0324 {
0325     struct i40e_hmc_sd_entry *sd_entry;
0326 
0327     if (!is_pf)
0328         return I40E_NOT_SUPPORTED;
0329 
0330     sd_entry = &hmc_info->sd_table.sd_entry[idx];
0331     I40E_CLEAR_PF_SD_ENTRY(hw, idx, I40E_SD_TYPE_PAGED);
0332 
0333     return  i40e_free_dma_mem(hw, &sd_entry->u.pd_table.pd_page_addr);
0334 }