i40e_hmc.c 11 KB

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