i40e_hmc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*******************************************************************************
  3. *
  4. * Intel Ethernet Controller XL710 Family Linux Driver
  5. * Copyright(c) 2013 - 2014 Intel Corporation.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms and conditions of the GNU General Public License,
  9. * version 2, as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * The full GNU General Public License is included in this distribution in
  20. * the file called "COPYING".
  21. *
  22. * Contact Information:
  23. * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  24. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  25. *
  26. ******************************************************************************/
  27. #include "i40e_osdep.h"
  28. #include "i40e_register.h"
  29. #include "i40e_status.h"
  30. #include "i40e_alloc.h"
  31. #include "i40e_hmc.h"
  32. #include "i40e_type.h"
  33. /**
  34. * i40e_add_sd_table_entry - Adds a segment descriptor to the table
  35. * @hw: pointer to our hw struct
  36. * @hmc_info: pointer to the HMC configuration information struct
  37. * @sd_index: segment descriptor index to manipulate
  38. * @type: what type of segment descriptor we're manipulating
  39. * @direct_mode_sz: size to alloc in direct mode
  40. **/
  41. i40e_status i40e_add_sd_table_entry(struct i40e_hw *hw,
  42. struct i40e_hmc_info *hmc_info,
  43. u32 sd_index,
  44. enum i40e_sd_entry_type type,
  45. u64 direct_mode_sz)
  46. {
  47. enum i40e_memory_type mem_type __attribute__((unused));
  48. struct i40e_hmc_sd_entry *sd_entry;
  49. bool dma_mem_alloc_done = false;
  50. struct i40e_dma_mem mem;
  51. i40e_status ret_code = I40E_SUCCESS;
  52. u64 alloc_len;
  53. if (NULL == hmc_info->sd_table.sd_entry) {
  54. ret_code = I40E_ERR_BAD_PTR;
  55. hw_dbg(hw, "i40e_add_sd_table_entry: bad sd_entry\n");
  56. goto exit;
  57. }
  58. if (sd_index >= hmc_info->sd_table.sd_cnt) {
  59. ret_code = I40E_ERR_INVALID_SD_INDEX;
  60. hw_dbg(hw, "i40e_add_sd_table_entry: bad sd_index\n");
  61. goto exit;
  62. }
  63. sd_entry = &hmc_info->sd_table.sd_entry[sd_index];
  64. if (!sd_entry->valid) {
  65. if (I40E_SD_TYPE_PAGED == type) {
  66. mem_type = i40e_mem_pd;
  67. alloc_len = I40E_HMC_PAGED_BP_SIZE;
  68. } else {
  69. mem_type = i40e_mem_bp_jumbo;
  70. alloc_len = direct_mode_sz;
  71. }
  72. /* allocate a 4K pd page or 2M backing page */
  73. ret_code = i40e_allocate_dma_mem(hw, &mem, mem_type, alloc_len,
  74. I40E_HMC_PD_BP_BUF_ALIGNMENT);
  75. if (ret_code)
  76. goto exit;
  77. dma_mem_alloc_done = true;
  78. if (I40E_SD_TYPE_PAGED == type) {
  79. ret_code = i40e_allocate_virt_mem(hw,
  80. &sd_entry->u.pd_table.pd_entry_virt_mem,
  81. sizeof(struct i40e_hmc_pd_entry) * 512);
  82. if (ret_code)
  83. goto exit;
  84. sd_entry->u.pd_table.pd_entry =
  85. (struct i40e_hmc_pd_entry *)
  86. sd_entry->u.pd_table.pd_entry_virt_mem.va;
  87. sd_entry->u.pd_table.pd_page_addr = mem;
  88. } else {
  89. sd_entry->u.bp.addr = mem;
  90. sd_entry->u.bp.sd_pd_index = sd_index;
  91. }
  92. /* initialize the sd entry */
  93. hmc_info->sd_table.sd_entry[sd_index].entry_type = type;
  94. /* increment the ref count */
  95. I40E_INC_SD_REFCNT(&hmc_info->sd_table);
  96. }
  97. /* Increment backing page reference count */
  98. if (I40E_SD_TYPE_DIRECT == sd_entry->entry_type)
  99. I40E_INC_BP_REFCNT(&sd_entry->u.bp);
  100. exit:
  101. if (ret_code)
  102. if (dma_mem_alloc_done)
  103. i40e_free_dma_mem(hw, &mem);
  104. return ret_code;
  105. }
  106. /**
  107. * i40e_add_pd_table_entry - Adds page descriptor to the specified table
  108. * @hw: pointer to our HW structure
  109. * @hmc_info: pointer to the HMC configuration information structure
  110. * @pd_index: which page descriptor index to manipulate
  111. * @rsrc_pg: if not NULL, use preallocated page instead of allocating new one.
  112. *
  113. * This function:
  114. * 1. Initializes the pd entry
  115. * 2. Adds pd_entry in the pd_table
  116. * 3. Mark the entry valid in i40e_hmc_pd_entry structure
  117. * 4. Initializes the pd_entry's ref count to 1
  118. * assumptions:
  119. * 1. The memory for pd should be pinned down, physically contiguous and
  120. * aligned on 4K boundary and zeroed memory.
  121. * 2. It should be 4K in size.
  122. **/
  123. i40e_status i40e_add_pd_table_entry(struct i40e_hw *hw,
  124. struct i40e_hmc_info *hmc_info,
  125. u32 pd_index,
  126. struct i40e_dma_mem *rsrc_pg)
  127. {
  128. i40e_status ret_code = 0;
  129. struct i40e_hmc_pd_table *pd_table;
  130. struct i40e_hmc_pd_entry *pd_entry;
  131. struct i40e_dma_mem mem;
  132. struct i40e_dma_mem *page = &mem;
  133. u32 sd_idx, rel_pd_idx;
  134. u64 *pd_addr;
  135. u64 page_desc;
  136. if (pd_index / I40E_HMC_PD_CNT_IN_SD >= hmc_info->sd_table.sd_cnt) {
  137. ret_code = I40E_ERR_INVALID_PAGE_DESC_INDEX;
  138. hw_dbg(hw, "i40e_add_pd_table_entry: bad pd_index\n");
  139. goto exit;
  140. }
  141. /* find corresponding sd */
  142. sd_idx = (pd_index / I40E_HMC_PD_CNT_IN_SD);
  143. if (I40E_SD_TYPE_PAGED !=
  144. hmc_info->sd_table.sd_entry[sd_idx].entry_type)
  145. goto exit;
  146. rel_pd_idx = (pd_index % I40E_HMC_PD_CNT_IN_SD);
  147. pd_table = &hmc_info->sd_table.sd_entry[sd_idx].u.pd_table;
  148. pd_entry = &pd_table->pd_entry[rel_pd_idx];
  149. if (!pd_entry->valid) {
  150. if (rsrc_pg) {
  151. pd_entry->rsrc_pg = true;
  152. page = rsrc_pg;
  153. } else {
  154. /* allocate a 4K backing page */
  155. ret_code = i40e_allocate_dma_mem(hw, page, i40e_mem_bp,
  156. I40E_HMC_PAGED_BP_SIZE,
  157. I40E_HMC_PD_BP_BUF_ALIGNMENT);
  158. if (ret_code)
  159. goto exit;
  160. pd_entry->rsrc_pg = false;
  161. }
  162. pd_entry->bp.addr = *page;
  163. pd_entry->bp.sd_pd_index = pd_index;
  164. pd_entry->bp.entry_type = I40E_SD_TYPE_PAGED;
  165. /* Set page address and valid bit */
  166. page_desc = page->pa | 0x1;
  167. pd_addr = (u64 *)pd_table->pd_page_addr.va;
  168. pd_addr += rel_pd_idx;
  169. /* Add the backing page physical address in the pd entry */
  170. memcpy(pd_addr, &page_desc, sizeof(u64));
  171. pd_entry->sd_index = sd_idx;
  172. pd_entry->valid = true;
  173. I40E_INC_PD_REFCNT(pd_table);
  174. }
  175. I40E_INC_BP_REFCNT(&pd_entry->bp);
  176. exit:
  177. return ret_code;
  178. }
  179. /**
  180. * i40e_remove_pd_bp - remove a backing page from a page descriptor
  181. * @hw: pointer to our HW structure
  182. * @hmc_info: pointer to the HMC configuration information structure
  183. * @idx: the page index
  184. * @is_pf: distinguishes a VF from a PF
  185. *
  186. * This function:
  187. * 1. Marks the entry in pd tabe (for paged address mode) or in sd table
  188. * (for direct address mode) invalid.
  189. * 2. Write to register PMPDINV to invalidate the backing page in FV cache
  190. * 3. Decrement the ref count for the pd _entry
  191. * assumptions:
  192. * 1. Caller can deallocate the memory used by backing storage after this
  193. * function returns.
  194. **/
  195. i40e_status i40e_remove_pd_bp(struct i40e_hw *hw,
  196. struct i40e_hmc_info *hmc_info,
  197. u32 idx)
  198. {
  199. i40e_status ret_code = 0;
  200. struct i40e_hmc_pd_entry *pd_entry;
  201. struct i40e_hmc_pd_table *pd_table;
  202. struct i40e_hmc_sd_entry *sd_entry;
  203. u32 sd_idx, rel_pd_idx;
  204. u64 *pd_addr;
  205. /* calculate index */
  206. sd_idx = idx / I40E_HMC_PD_CNT_IN_SD;
  207. rel_pd_idx = idx % I40E_HMC_PD_CNT_IN_SD;
  208. if (sd_idx >= hmc_info->sd_table.sd_cnt) {
  209. ret_code = I40E_ERR_INVALID_PAGE_DESC_INDEX;
  210. hw_dbg(hw, "i40e_remove_pd_bp: bad idx\n");
  211. goto exit;
  212. }
  213. sd_entry = &hmc_info->sd_table.sd_entry[sd_idx];
  214. if (I40E_SD_TYPE_PAGED != sd_entry->entry_type) {
  215. ret_code = I40E_ERR_INVALID_SD_TYPE;
  216. hw_dbg(hw, "i40e_remove_pd_bp: wrong sd_entry type\n");
  217. goto exit;
  218. }
  219. /* get the entry and decrease its ref counter */
  220. pd_table = &hmc_info->sd_table.sd_entry[sd_idx].u.pd_table;
  221. pd_entry = &pd_table->pd_entry[rel_pd_idx];
  222. I40E_DEC_BP_REFCNT(&pd_entry->bp);
  223. if (pd_entry->bp.ref_cnt)
  224. goto exit;
  225. /* mark the entry invalid */
  226. pd_entry->valid = false;
  227. I40E_DEC_PD_REFCNT(pd_table);
  228. pd_addr = (u64 *)pd_table->pd_page_addr.va;
  229. pd_addr += rel_pd_idx;
  230. memset(pd_addr, 0, sizeof(u64));
  231. I40E_INVALIDATE_PF_HMC_PD(hw, sd_idx, idx);
  232. /* free memory here */
  233. if (!pd_entry->rsrc_pg)
  234. ret_code = i40e_free_dma_mem(hw, &pd_entry->bp.addr);
  235. if (ret_code)
  236. goto exit;
  237. if (!pd_table->ref_cnt)
  238. i40e_free_virt_mem(hw, &pd_table->pd_entry_virt_mem);
  239. exit:
  240. return ret_code;
  241. }
  242. /**
  243. * i40e_prep_remove_sd_bp - Prepares to remove a backing page from a sd entry
  244. * @hmc_info: pointer to the HMC configuration information structure
  245. * @idx: the page index
  246. **/
  247. i40e_status i40e_prep_remove_sd_bp(struct i40e_hmc_info *hmc_info,
  248. u32 idx)
  249. {
  250. i40e_status ret_code = 0;
  251. struct i40e_hmc_sd_entry *sd_entry;
  252. /* get the entry and decrease its ref counter */
  253. sd_entry = &hmc_info->sd_table.sd_entry[idx];
  254. I40E_DEC_BP_REFCNT(&sd_entry->u.bp);
  255. if (sd_entry->u.bp.ref_cnt) {
  256. ret_code = I40E_ERR_NOT_READY;
  257. goto exit;
  258. }
  259. I40E_DEC_SD_REFCNT(&hmc_info->sd_table);
  260. /* mark the entry invalid */
  261. sd_entry->valid = false;
  262. exit:
  263. return ret_code;
  264. }
  265. /**
  266. * i40e_remove_sd_bp_new - Removes a backing page from a segment descriptor
  267. * @hw: pointer to our hw struct
  268. * @hmc_info: pointer to the HMC configuration information structure
  269. * @idx: the page index
  270. * @is_pf: used to distinguish between VF and PF
  271. **/
  272. i40e_status i40e_remove_sd_bp_new(struct i40e_hw *hw,
  273. struct i40e_hmc_info *hmc_info,
  274. u32 idx, bool is_pf)
  275. {
  276. struct i40e_hmc_sd_entry *sd_entry;
  277. if (!is_pf)
  278. return I40E_NOT_SUPPORTED;
  279. /* get the entry and decrease its ref counter */
  280. sd_entry = &hmc_info->sd_table.sd_entry[idx];
  281. I40E_CLEAR_PF_SD_ENTRY(hw, idx, I40E_SD_TYPE_DIRECT);
  282. return i40e_free_dma_mem(hw, &sd_entry->u.bp.addr);
  283. }
  284. /**
  285. * i40e_prep_remove_pd_page - Prepares to remove a PD page from sd entry.
  286. * @hmc_info: pointer to the HMC configuration information structure
  287. * @idx: segment descriptor index to find the relevant page descriptor
  288. **/
  289. i40e_status i40e_prep_remove_pd_page(struct i40e_hmc_info *hmc_info,
  290. u32 idx)
  291. {
  292. i40e_status ret_code = 0;
  293. struct i40e_hmc_sd_entry *sd_entry;
  294. sd_entry = &hmc_info->sd_table.sd_entry[idx];
  295. if (sd_entry->u.pd_table.ref_cnt) {
  296. ret_code = I40E_ERR_NOT_READY;
  297. goto exit;
  298. }
  299. /* mark the entry invalid */
  300. sd_entry->valid = false;
  301. I40E_DEC_SD_REFCNT(&hmc_info->sd_table);
  302. exit:
  303. return ret_code;
  304. }
  305. /**
  306. * i40e_remove_pd_page_new - Removes a PD page from sd entry.
  307. * @hw: pointer to our hw struct
  308. * @hmc_info: pointer to the HMC configuration information structure
  309. * @idx: segment descriptor index to find the relevant page descriptor
  310. * @is_pf: used to distinguish between VF and PF
  311. **/
  312. i40e_status i40e_remove_pd_page_new(struct i40e_hw *hw,
  313. struct i40e_hmc_info *hmc_info,
  314. u32 idx, bool is_pf)
  315. {
  316. struct i40e_hmc_sd_entry *sd_entry;
  317. if (!is_pf)
  318. return I40E_NOT_SUPPORTED;
  319. sd_entry = &hmc_info->sd_table.sd_entry[idx];
  320. I40E_CLEAR_PF_SD_ENTRY(hw, idx, I40E_SD_TYPE_PAGED);
  321. return i40e_free_dma_mem(hw, &sd_entry->u.pd_table.pd_page_addr);
  322. }