i40e_hmc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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;
  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. *
  111. * This function:
  112. * 1. Initializes the pd entry
  113. * 2. Adds pd_entry in the pd_table
  114. * 3. Mark the entry valid in i40e_hmc_pd_entry structure
  115. * 4. Initializes the pd_entry's ref count to 1
  116. * assumptions:
  117. * 1. The memory for pd should be pinned down, physically contiguous and
  118. * aligned on 4K boundary and zeroed memory.
  119. * 2. It should be 4K in size.
  120. **/
  121. i40e_status i40e_add_pd_table_entry(struct i40e_hw *hw,
  122. struct i40e_hmc_info *hmc_info,
  123. u32 pd_index)
  124. {
  125. i40e_status ret_code = 0;
  126. struct i40e_hmc_pd_table *pd_table;
  127. struct i40e_hmc_pd_entry *pd_entry;
  128. struct i40e_dma_mem mem;
  129. u32 sd_idx, rel_pd_idx;
  130. u64 *pd_addr;
  131. u64 page_desc;
  132. if (pd_index / I40E_HMC_PD_CNT_IN_SD >= hmc_info->sd_table.sd_cnt) {
  133. ret_code = I40E_ERR_INVALID_PAGE_DESC_INDEX;
  134. hw_dbg(hw, "i40e_add_pd_table_entry: bad pd_index\n");
  135. goto exit;
  136. }
  137. /* find corresponding sd */
  138. sd_idx = (pd_index / I40E_HMC_PD_CNT_IN_SD);
  139. if (I40E_SD_TYPE_PAGED !=
  140. hmc_info->sd_table.sd_entry[sd_idx].entry_type)
  141. goto exit;
  142. rel_pd_idx = (pd_index % I40E_HMC_PD_CNT_IN_SD);
  143. pd_table = &hmc_info->sd_table.sd_entry[sd_idx].u.pd_table;
  144. pd_entry = &pd_table->pd_entry[rel_pd_idx];
  145. if (!pd_entry->valid) {
  146. /* allocate a 4K backing page */
  147. ret_code = i40e_allocate_dma_mem(hw, &mem, i40e_mem_bp,
  148. I40E_HMC_PAGED_BP_SIZE,
  149. I40E_HMC_PD_BP_BUF_ALIGNMENT);
  150. if (ret_code)
  151. goto exit;
  152. pd_entry->bp.addr = mem;
  153. pd_entry->bp.sd_pd_index = pd_index;
  154. pd_entry->bp.entry_type = I40E_SD_TYPE_PAGED;
  155. /* Set page address and valid bit */
  156. page_desc = mem.pa | 0x1;
  157. pd_addr = (u64 *)pd_table->pd_page_addr.va;
  158. pd_addr += rel_pd_idx;
  159. /* Add the backing page physical address in the pd entry */
  160. memcpy(pd_addr, &page_desc, sizeof(u64));
  161. pd_entry->sd_index = sd_idx;
  162. pd_entry->valid = true;
  163. I40E_INC_PD_REFCNT(pd_table);
  164. }
  165. I40E_INC_BP_REFCNT(&pd_entry->bp);
  166. exit:
  167. return ret_code;
  168. }
  169. /**
  170. * i40e_remove_pd_bp - remove a backing page from a page descriptor
  171. * @hw: pointer to our HW structure
  172. * @hmc_info: pointer to the HMC configuration information structure
  173. * @idx: the page index
  174. * @is_pf: distinguishes a VF from a PF
  175. *
  176. * This function:
  177. * 1. Marks the entry in pd tabe (for paged address mode) or in sd table
  178. * (for direct address mode) invalid.
  179. * 2. Write to register PMPDINV to invalidate the backing page in FV cache
  180. * 3. Decrement the ref count for the pd _entry
  181. * assumptions:
  182. * 1. Caller can deallocate the memory used by backing storage after this
  183. * function returns.
  184. **/
  185. i40e_status i40e_remove_pd_bp(struct i40e_hw *hw,
  186. struct i40e_hmc_info *hmc_info,
  187. u32 idx)
  188. {
  189. i40e_status ret_code = 0;
  190. struct i40e_hmc_pd_entry *pd_entry;
  191. struct i40e_hmc_pd_table *pd_table;
  192. struct i40e_hmc_sd_entry *sd_entry;
  193. u32 sd_idx, rel_pd_idx;
  194. u64 *pd_addr;
  195. /* calculate index */
  196. sd_idx = idx / I40E_HMC_PD_CNT_IN_SD;
  197. rel_pd_idx = idx % I40E_HMC_PD_CNT_IN_SD;
  198. if (sd_idx >= hmc_info->sd_table.sd_cnt) {
  199. ret_code = I40E_ERR_INVALID_PAGE_DESC_INDEX;
  200. hw_dbg(hw, "i40e_remove_pd_bp: bad idx\n");
  201. goto exit;
  202. }
  203. sd_entry = &hmc_info->sd_table.sd_entry[sd_idx];
  204. if (I40E_SD_TYPE_PAGED != sd_entry->entry_type) {
  205. ret_code = I40E_ERR_INVALID_SD_TYPE;
  206. hw_dbg(hw, "i40e_remove_pd_bp: wrong sd_entry type\n");
  207. goto exit;
  208. }
  209. /* get the entry and decrease its ref counter */
  210. pd_table = &hmc_info->sd_table.sd_entry[sd_idx].u.pd_table;
  211. pd_entry = &pd_table->pd_entry[rel_pd_idx];
  212. I40E_DEC_BP_REFCNT(&pd_entry->bp);
  213. if (pd_entry->bp.ref_cnt)
  214. goto exit;
  215. /* mark the entry invalid */
  216. pd_entry->valid = false;
  217. I40E_DEC_PD_REFCNT(pd_table);
  218. pd_addr = (u64 *)pd_table->pd_page_addr.va;
  219. pd_addr += rel_pd_idx;
  220. memset(pd_addr, 0, sizeof(u64));
  221. I40E_INVALIDATE_PF_HMC_PD(hw, sd_idx, idx);
  222. /* free memory here */
  223. ret_code = i40e_free_dma_mem(hw, &(pd_entry->bp.addr));
  224. if (ret_code)
  225. goto exit;
  226. if (!pd_table->ref_cnt)
  227. i40e_free_virt_mem(hw, &pd_table->pd_entry_virt_mem);
  228. exit:
  229. return ret_code;
  230. }
  231. /**
  232. * i40e_prep_remove_sd_bp - Prepares to remove a backing page from a sd entry
  233. * @hmc_info: pointer to the HMC configuration information structure
  234. * @idx: the page index
  235. **/
  236. i40e_status i40e_prep_remove_sd_bp(struct i40e_hmc_info *hmc_info,
  237. u32 idx)
  238. {
  239. i40e_status ret_code = 0;
  240. struct i40e_hmc_sd_entry *sd_entry;
  241. /* get the entry and decrease its ref counter */
  242. sd_entry = &hmc_info->sd_table.sd_entry[idx];
  243. I40E_DEC_BP_REFCNT(&sd_entry->u.bp);
  244. if (sd_entry->u.bp.ref_cnt) {
  245. ret_code = I40E_ERR_NOT_READY;
  246. goto exit;
  247. }
  248. I40E_DEC_SD_REFCNT(&hmc_info->sd_table);
  249. /* mark the entry invalid */
  250. sd_entry->valid = false;
  251. exit:
  252. return ret_code;
  253. }
  254. /**
  255. * i40e_remove_sd_bp_new - Removes a backing page from a segment descriptor
  256. * @hw: pointer to our hw struct
  257. * @hmc_info: pointer to the HMC configuration information structure
  258. * @idx: the page index
  259. * @is_pf: used to distinguish between VF and PF
  260. **/
  261. i40e_status i40e_remove_sd_bp_new(struct i40e_hw *hw,
  262. struct i40e_hmc_info *hmc_info,
  263. u32 idx, bool is_pf)
  264. {
  265. struct i40e_hmc_sd_entry *sd_entry;
  266. i40e_status ret_code = 0;
  267. /* get the entry and decrease its ref counter */
  268. sd_entry = &hmc_info->sd_table.sd_entry[idx];
  269. if (is_pf) {
  270. I40E_CLEAR_PF_SD_ENTRY(hw, idx, I40E_SD_TYPE_DIRECT);
  271. } else {
  272. ret_code = I40E_NOT_SUPPORTED;
  273. goto exit;
  274. }
  275. ret_code = i40e_free_dma_mem(hw, &(sd_entry->u.bp.addr));
  276. if (ret_code)
  277. goto exit;
  278. exit:
  279. return ret_code;
  280. }
  281. /**
  282. * i40e_prep_remove_pd_page - Prepares to remove a PD page from sd entry.
  283. * @hmc_info: pointer to the HMC configuration information structure
  284. * @idx: segment descriptor index to find the relevant page descriptor
  285. **/
  286. i40e_status i40e_prep_remove_pd_page(struct i40e_hmc_info *hmc_info,
  287. u32 idx)
  288. {
  289. i40e_status ret_code = 0;
  290. struct i40e_hmc_sd_entry *sd_entry;
  291. sd_entry = &hmc_info->sd_table.sd_entry[idx];
  292. if (sd_entry->u.pd_table.ref_cnt) {
  293. ret_code = I40E_ERR_NOT_READY;
  294. goto exit;
  295. }
  296. /* mark the entry invalid */
  297. sd_entry->valid = false;
  298. I40E_DEC_SD_REFCNT(&hmc_info->sd_table);
  299. exit:
  300. return ret_code;
  301. }
  302. /**
  303. * i40e_remove_pd_page_new - Removes a PD page from sd entry.
  304. * @hw: pointer to our hw struct
  305. * @hmc_info: pointer to the HMC configuration information structure
  306. * @idx: segment descriptor index to find the relevant page descriptor
  307. * @is_pf: used to distinguish between VF and PF
  308. **/
  309. i40e_status i40e_remove_pd_page_new(struct i40e_hw *hw,
  310. struct i40e_hmc_info *hmc_info,
  311. u32 idx, bool is_pf)
  312. {
  313. i40e_status ret_code = 0;
  314. struct i40e_hmc_sd_entry *sd_entry;
  315. sd_entry = &hmc_info->sd_table.sd_entry[idx];
  316. if (is_pf) {
  317. I40E_CLEAR_PF_SD_ENTRY(hw, idx, I40E_SD_TYPE_PAGED);
  318. } else {
  319. ret_code = I40E_NOT_SUPPORTED;
  320. goto exit;
  321. }
  322. /* free memory here */
  323. ret_code = i40e_free_dma_mem(hw, &(sd_entry->u.pd_table.pd_page_addr));
  324. if (ret_code)
  325. goto exit;
  326. exit:
  327. return ret_code;
  328. }