i40e_hmc.c 11 KB

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