hmc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
  2. /* Copyright (c) 2015 - 2021 Intel Corporation */
  3. #include "osdep.h"
  4. #include "hmc.h"
  5. #include "defs.h"
  6. #include "type.h"
  7. #include "protos.h"
  8. /**
  9. * irdma_find_sd_index_limit - finds segment descriptor index limit
  10. * @hmc_info: pointer to the HMC configuration information structure
  11. * @type: type of HMC resources we're searching
  12. * @idx: starting index for the object
  13. * @cnt: number of objects we're trying to create
  14. * @sd_idx: pointer to return index of the segment descriptor in question
  15. * @sd_limit: pointer to return the maximum number of segment descriptors
  16. *
  17. * This function calculates the segment descriptor index and index limit
  18. * for the resource defined by irdma_hmc_rsrc_type.
  19. */
  20. static void irdma_find_sd_index_limit(struct irdma_hmc_info *hmc_info, u32 type,
  21. u32 idx, u32 cnt, u32 *sd_idx,
  22. u32 *sd_limit)
  23. {
  24. u64 fpm_addr, fpm_limit;
  25. fpm_addr = hmc_info->hmc_obj[(type)].base +
  26. hmc_info->hmc_obj[type].size * idx;
  27. fpm_limit = fpm_addr + hmc_info->hmc_obj[type].size * cnt;
  28. *sd_idx = (u32)(fpm_addr / IRDMA_HMC_DIRECT_BP_SIZE);
  29. *sd_limit = (u32)((fpm_limit - 1) / IRDMA_HMC_DIRECT_BP_SIZE);
  30. *sd_limit += 1;
  31. }
  32. /**
  33. * irdma_find_pd_index_limit - finds page descriptor index limit
  34. * @hmc_info: pointer to the HMC configuration information struct
  35. * @type: HMC resource type we're examining
  36. * @idx: starting index for the object
  37. * @cnt: number of objects we're trying to create
  38. * @pd_idx: pointer to return page descriptor index
  39. * @pd_limit: pointer to return page descriptor index limit
  40. *
  41. * Calculates the page descriptor index and index limit for the resource
  42. * defined by irdma_hmc_rsrc_type.
  43. */
  44. static void irdma_find_pd_index_limit(struct irdma_hmc_info *hmc_info, u32 type,
  45. u32 idx, u32 cnt, u32 *pd_idx,
  46. u32 *pd_limit)
  47. {
  48. u64 fpm_adr, fpm_limit;
  49. fpm_adr = hmc_info->hmc_obj[type].base +
  50. hmc_info->hmc_obj[type].size * idx;
  51. fpm_limit = fpm_adr + (hmc_info)->hmc_obj[(type)].size * (cnt);
  52. *pd_idx = (u32)(fpm_adr / IRDMA_HMC_PAGED_BP_SIZE);
  53. *pd_limit = (u32)((fpm_limit - 1) / IRDMA_HMC_PAGED_BP_SIZE);
  54. *pd_limit += 1;
  55. }
  56. /**
  57. * irdma_set_sd_entry - setup entry for sd programming
  58. * @pa: physical addr
  59. * @idx: sd index
  60. * @type: paged or direct sd
  61. * @entry: sd entry ptr
  62. */
  63. static void irdma_set_sd_entry(u64 pa, u32 idx, enum irdma_sd_entry_type type,
  64. struct irdma_update_sd_entry *entry)
  65. {
  66. entry->data = pa |
  67. FIELD_PREP(IRDMA_PFHMC_SDDATALOW_PMSDBPCOUNT, IRDMA_HMC_MAX_BP_COUNT) |
  68. FIELD_PREP(IRDMA_PFHMC_SDDATALOW_PMSDTYPE,
  69. type == IRDMA_SD_TYPE_PAGED ? 0 : 1) |
  70. FIELD_PREP(IRDMA_PFHMC_SDDATALOW_PMSDVALID, 1);
  71. entry->cmd = idx | FIELD_PREP(IRDMA_PFHMC_SDCMD_PMSDWR, 1) | BIT(15);
  72. }
  73. /**
  74. * irdma_clr_sd_entry - setup entry for sd clear
  75. * @idx: sd index
  76. * @type: paged or direct sd
  77. * @entry: sd entry ptr
  78. */
  79. static void irdma_clr_sd_entry(u32 idx, enum irdma_sd_entry_type type,
  80. struct irdma_update_sd_entry *entry)
  81. {
  82. entry->data = FIELD_PREP(IRDMA_PFHMC_SDDATALOW_PMSDBPCOUNT, IRDMA_HMC_MAX_BP_COUNT) |
  83. FIELD_PREP(IRDMA_PFHMC_SDDATALOW_PMSDTYPE,
  84. type == IRDMA_SD_TYPE_PAGED ? 0 : 1);
  85. entry->cmd = idx | FIELD_PREP(IRDMA_PFHMC_SDCMD_PMSDWR, 1) | BIT(15);
  86. }
  87. /**
  88. * irdma_invalidate_pf_hmc_pd - Invalidates the pd cache in the hardware for PF
  89. * @dev: pointer to our device struct
  90. * @sd_idx: segment descriptor index
  91. * @pd_idx: page descriptor index
  92. */
  93. static inline void irdma_invalidate_pf_hmc_pd(struct irdma_sc_dev *dev, u32 sd_idx,
  94. u32 pd_idx)
  95. {
  96. u32 val = FIELD_PREP(IRDMA_PFHMC_PDINV_PMSDIDX, sd_idx) |
  97. FIELD_PREP(IRDMA_PFHMC_PDINV_PMSDPARTSEL, 1) |
  98. FIELD_PREP(IRDMA_PFHMC_PDINV_PMPDIDX, pd_idx);
  99. writel(val, dev->hw_regs[IRDMA_PFHMC_PDINV]);
  100. }
  101. /**
  102. * irdma_hmc_sd_one - setup 1 sd entry for cqp
  103. * @dev: pointer to the device structure
  104. * @hmc_fn_id: hmc's function id
  105. * @pa: physical addr
  106. * @sd_idx: sd index
  107. * @type: paged or direct sd
  108. * @setsd: flag to set or clear sd
  109. */
  110. int irdma_hmc_sd_one(struct irdma_sc_dev *dev, u8 hmc_fn_id, u64 pa, u32 sd_idx,
  111. enum irdma_sd_entry_type type, bool setsd)
  112. {
  113. struct irdma_update_sds_info sdinfo;
  114. sdinfo.cnt = 1;
  115. sdinfo.hmc_fn_id = hmc_fn_id;
  116. if (setsd)
  117. irdma_set_sd_entry(pa, sd_idx, type, sdinfo.entry);
  118. else
  119. irdma_clr_sd_entry(sd_idx, type, sdinfo.entry);
  120. return dev->cqp->process_cqp_sds(dev, &sdinfo);
  121. }
  122. /**
  123. * irdma_hmc_sd_grp - setup group of sd entries for cqp
  124. * @dev: pointer to the device structure
  125. * @hmc_info: pointer to the HMC configuration information struct
  126. * @sd_index: sd index
  127. * @sd_cnt: number of sd entries
  128. * @setsd: flag to set or clear sd
  129. */
  130. static int irdma_hmc_sd_grp(struct irdma_sc_dev *dev,
  131. struct irdma_hmc_info *hmc_info, u32 sd_index,
  132. u32 sd_cnt, bool setsd)
  133. {
  134. struct irdma_hmc_sd_entry *sd_entry;
  135. struct irdma_update_sds_info sdinfo = {};
  136. u64 pa;
  137. u32 i;
  138. int ret_code = 0;
  139. sdinfo.hmc_fn_id = hmc_info->hmc_fn_id;
  140. for (i = sd_index; i < sd_index + sd_cnt; i++) {
  141. sd_entry = &hmc_info->sd_table.sd_entry[i];
  142. if (!sd_entry || (!sd_entry->valid && setsd) ||
  143. (sd_entry->valid && !setsd))
  144. continue;
  145. if (setsd) {
  146. pa = (sd_entry->entry_type == IRDMA_SD_TYPE_PAGED) ?
  147. sd_entry->u.pd_table.pd_page_addr.pa :
  148. sd_entry->u.bp.addr.pa;
  149. irdma_set_sd_entry(pa, i, sd_entry->entry_type,
  150. &sdinfo.entry[sdinfo.cnt]);
  151. } else {
  152. irdma_clr_sd_entry(i, sd_entry->entry_type,
  153. &sdinfo.entry[sdinfo.cnt]);
  154. }
  155. sdinfo.cnt++;
  156. if (sdinfo.cnt == IRDMA_MAX_SD_ENTRIES) {
  157. ret_code = dev->cqp->process_cqp_sds(dev, &sdinfo);
  158. if (ret_code) {
  159. ibdev_dbg(to_ibdev(dev),
  160. "HMC: sd_programming failed err=%d\n",
  161. ret_code);
  162. return ret_code;
  163. }
  164. sdinfo.cnt = 0;
  165. }
  166. }
  167. if (sdinfo.cnt)
  168. ret_code = dev->cqp->process_cqp_sds(dev, &sdinfo);
  169. return ret_code;
  170. }
  171. /**
  172. * irdma_hmc_finish_add_sd_reg - program sd entries for objects
  173. * @dev: pointer to the device structure
  174. * @info: create obj info
  175. */
  176. static int irdma_hmc_finish_add_sd_reg(struct irdma_sc_dev *dev,
  177. struct irdma_hmc_create_obj_info *info)
  178. {
  179. if (info->start_idx >= info->hmc_info->hmc_obj[info->rsrc_type].cnt)
  180. return -EINVAL;
  181. if ((info->start_idx + info->count) >
  182. info->hmc_info->hmc_obj[info->rsrc_type].cnt)
  183. return -EINVAL;
  184. if (!info->add_sd_cnt)
  185. return 0;
  186. return irdma_hmc_sd_grp(dev, info->hmc_info,
  187. info->hmc_info->sd_indexes[0], info->add_sd_cnt,
  188. true);
  189. }
  190. /**
  191. * irdma_sc_create_hmc_obj - allocate backing store for hmc objects
  192. * @dev: pointer to the device structure
  193. * @info: pointer to irdma_hmc_create_obj_info struct
  194. *
  195. * This will allocate memory for PDs and backing pages and populate
  196. * the sd and pd entries.
  197. */
  198. int irdma_sc_create_hmc_obj(struct irdma_sc_dev *dev,
  199. struct irdma_hmc_create_obj_info *info)
  200. {
  201. struct irdma_hmc_sd_entry *sd_entry;
  202. u32 sd_idx, sd_lmt;
  203. u32 pd_idx = 0, pd_lmt = 0;
  204. u32 pd_idx1 = 0, pd_lmt1 = 0;
  205. u32 i, j;
  206. bool pd_error = false;
  207. int ret_code = 0;
  208. if (info->start_idx >= info->hmc_info->hmc_obj[info->rsrc_type].cnt)
  209. return -EINVAL;
  210. if ((info->start_idx + info->count) >
  211. info->hmc_info->hmc_obj[info->rsrc_type].cnt) {
  212. ibdev_dbg(to_ibdev(dev),
  213. "HMC: error type %u, start = %u, req cnt %u, cnt = %u\n",
  214. info->rsrc_type, info->start_idx, info->count,
  215. info->hmc_info->hmc_obj[info->rsrc_type].cnt);
  216. return -EINVAL;
  217. }
  218. irdma_find_sd_index_limit(info->hmc_info, info->rsrc_type,
  219. info->start_idx, info->count, &sd_idx,
  220. &sd_lmt);
  221. if (sd_idx >= info->hmc_info->sd_table.sd_cnt ||
  222. sd_lmt > info->hmc_info->sd_table.sd_cnt) {
  223. return -EINVAL;
  224. }
  225. irdma_find_pd_index_limit(info->hmc_info, info->rsrc_type,
  226. info->start_idx, info->count, &pd_idx,
  227. &pd_lmt);
  228. for (j = sd_idx; j < sd_lmt; j++) {
  229. ret_code = irdma_add_sd_table_entry(dev->hw, info->hmc_info, j,
  230. info->entry_type,
  231. IRDMA_HMC_DIRECT_BP_SIZE);
  232. if (ret_code)
  233. goto exit_sd_error;
  234. sd_entry = &info->hmc_info->sd_table.sd_entry[j];
  235. if (sd_entry->entry_type == IRDMA_SD_TYPE_PAGED &&
  236. (dev->hmc_info == info->hmc_info &&
  237. info->rsrc_type != IRDMA_HMC_IW_PBLE)) {
  238. pd_idx1 = max(pd_idx, (j * IRDMA_HMC_MAX_BP_COUNT));
  239. pd_lmt1 = min(pd_lmt, (j + 1) * IRDMA_HMC_MAX_BP_COUNT);
  240. for (i = pd_idx1; i < pd_lmt1; i++) {
  241. /* update the pd table entry */
  242. ret_code = irdma_add_pd_table_entry(dev,
  243. info->hmc_info,
  244. i, NULL);
  245. if (ret_code) {
  246. pd_error = true;
  247. break;
  248. }
  249. }
  250. if (pd_error) {
  251. while (i && (i > pd_idx1)) {
  252. irdma_remove_pd_bp(dev, info->hmc_info,
  253. i - 1);
  254. i--;
  255. }
  256. }
  257. }
  258. if (sd_entry->valid)
  259. continue;
  260. info->hmc_info->sd_indexes[info->add_sd_cnt] = (u16)j;
  261. info->add_sd_cnt++;
  262. sd_entry->valid = true;
  263. }
  264. return irdma_hmc_finish_add_sd_reg(dev, info);
  265. exit_sd_error:
  266. while (j && (j > sd_idx)) {
  267. sd_entry = &info->hmc_info->sd_table.sd_entry[j - 1];
  268. switch (sd_entry->entry_type) {
  269. case IRDMA_SD_TYPE_PAGED:
  270. pd_idx1 = max(pd_idx, (j - 1) * IRDMA_HMC_MAX_BP_COUNT);
  271. pd_lmt1 = min(pd_lmt, (j * IRDMA_HMC_MAX_BP_COUNT));
  272. for (i = pd_idx1; i < pd_lmt1; i++)
  273. irdma_prep_remove_pd_page(info->hmc_info, i);
  274. break;
  275. case IRDMA_SD_TYPE_DIRECT:
  276. irdma_prep_remove_pd_page(info->hmc_info, (j - 1));
  277. break;
  278. default:
  279. ret_code = -EINVAL;
  280. break;
  281. }
  282. j--;
  283. }
  284. return ret_code;
  285. }
  286. /**
  287. * irdma_finish_del_sd_reg - delete sd entries for objects
  288. * @dev: pointer to the device structure
  289. * @info: dele obj info
  290. * @reset: true if called before reset
  291. */
  292. static int irdma_finish_del_sd_reg(struct irdma_sc_dev *dev,
  293. struct irdma_hmc_del_obj_info *info,
  294. bool reset)
  295. {
  296. struct irdma_hmc_sd_entry *sd_entry;
  297. int ret_code = 0;
  298. u32 i, sd_idx;
  299. struct irdma_dma_mem *mem;
  300. if (!reset)
  301. ret_code = irdma_hmc_sd_grp(dev, info->hmc_info,
  302. info->hmc_info->sd_indexes[0],
  303. info->del_sd_cnt, false);
  304. if (ret_code)
  305. ibdev_dbg(to_ibdev(dev), "HMC: error cqp sd sd_grp\n");
  306. for (i = 0; i < info->del_sd_cnt; i++) {
  307. sd_idx = info->hmc_info->sd_indexes[i];
  308. sd_entry = &info->hmc_info->sd_table.sd_entry[sd_idx];
  309. mem = (sd_entry->entry_type == IRDMA_SD_TYPE_PAGED) ?
  310. &sd_entry->u.pd_table.pd_page_addr :
  311. &sd_entry->u.bp.addr;
  312. if (!mem || !mem->va) {
  313. ibdev_dbg(to_ibdev(dev), "HMC: error cqp sd mem\n");
  314. } else {
  315. dma_free_coherent(dev->hw->device, mem->size, mem->va,
  316. mem->pa);
  317. mem->va = NULL;
  318. }
  319. }
  320. return ret_code;
  321. }
  322. /**
  323. * irdma_sc_del_hmc_obj - remove pe hmc objects
  324. * @dev: pointer to the device structure
  325. * @info: pointer to irdma_hmc_del_obj_info struct
  326. * @reset: true if called before reset
  327. *
  328. * This will de-populate the SDs and PDs. It frees
  329. * the memory for PDS and backing storage. After this function is returned,
  330. * caller should deallocate memory allocated previously for
  331. * book-keeping information about PDs and backing storage.
  332. */
  333. int irdma_sc_del_hmc_obj(struct irdma_sc_dev *dev,
  334. struct irdma_hmc_del_obj_info *info, bool reset)
  335. {
  336. struct irdma_hmc_pd_table *pd_table;
  337. u32 sd_idx, sd_lmt;
  338. u32 pd_idx, pd_lmt, rel_pd_idx;
  339. u32 i, j;
  340. int ret_code = 0;
  341. if (info->start_idx >= info->hmc_info->hmc_obj[info->rsrc_type].cnt) {
  342. ibdev_dbg(to_ibdev(dev),
  343. "HMC: error start_idx[%04d] >= [type %04d].cnt[%04d]\n",
  344. info->start_idx, info->rsrc_type,
  345. info->hmc_info->hmc_obj[info->rsrc_type].cnt);
  346. return -EINVAL;
  347. }
  348. if ((info->start_idx + info->count) >
  349. info->hmc_info->hmc_obj[info->rsrc_type].cnt) {
  350. ibdev_dbg(to_ibdev(dev),
  351. "HMC: error start_idx[%04d] + count %04d >= [type %04d].cnt[%04d]\n",
  352. info->start_idx, info->count, info->rsrc_type,
  353. info->hmc_info->hmc_obj[info->rsrc_type].cnt);
  354. return -EINVAL;
  355. }
  356. irdma_find_pd_index_limit(info->hmc_info, info->rsrc_type,
  357. info->start_idx, info->count, &pd_idx,
  358. &pd_lmt);
  359. for (j = pd_idx; j < pd_lmt; j++) {
  360. sd_idx = j / IRDMA_HMC_PD_CNT_IN_SD;
  361. if (!info->hmc_info->sd_table.sd_entry[sd_idx].valid)
  362. continue;
  363. if (info->hmc_info->sd_table.sd_entry[sd_idx].entry_type !=
  364. IRDMA_SD_TYPE_PAGED)
  365. continue;
  366. rel_pd_idx = j % IRDMA_HMC_PD_CNT_IN_SD;
  367. pd_table = &info->hmc_info->sd_table.sd_entry[sd_idx].u.pd_table;
  368. if (pd_table->pd_entry &&
  369. pd_table->pd_entry[rel_pd_idx].valid) {
  370. ret_code = irdma_remove_pd_bp(dev, info->hmc_info, j);
  371. if (ret_code) {
  372. ibdev_dbg(to_ibdev(dev),
  373. "HMC: remove_pd_bp error\n");
  374. return ret_code;
  375. }
  376. }
  377. }
  378. irdma_find_sd_index_limit(info->hmc_info, info->rsrc_type,
  379. info->start_idx, info->count, &sd_idx,
  380. &sd_lmt);
  381. if (sd_idx >= info->hmc_info->sd_table.sd_cnt ||
  382. sd_lmt > info->hmc_info->sd_table.sd_cnt) {
  383. ibdev_dbg(to_ibdev(dev), "HMC: invalid sd_idx\n");
  384. return -EINVAL;
  385. }
  386. for (i = sd_idx; i < sd_lmt; i++) {
  387. pd_table = &info->hmc_info->sd_table.sd_entry[i].u.pd_table;
  388. if (!info->hmc_info->sd_table.sd_entry[i].valid)
  389. continue;
  390. switch (info->hmc_info->sd_table.sd_entry[i].entry_type) {
  391. case IRDMA_SD_TYPE_DIRECT:
  392. ret_code = irdma_prep_remove_sd_bp(info->hmc_info, i);
  393. if (!ret_code) {
  394. info->hmc_info->sd_indexes[info->del_sd_cnt] =
  395. (u16)i;
  396. info->del_sd_cnt++;
  397. }
  398. break;
  399. case IRDMA_SD_TYPE_PAGED:
  400. ret_code = irdma_prep_remove_pd_page(info->hmc_info, i);
  401. if (ret_code)
  402. break;
  403. if (dev->hmc_info != info->hmc_info &&
  404. info->rsrc_type == IRDMA_HMC_IW_PBLE &&
  405. pd_table->pd_entry) {
  406. kfree(pd_table->pd_entry_virt_mem.va);
  407. pd_table->pd_entry = NULL;
  408. }
  409. info->hmc_info->sd_indexes[info->del_sd_cnt] = (u16)i;
  410. info->del_sd_cnt++;
  411. break;
  412. default:
  413. break;
  414. }
  415. }
  416. return irdma_finish_del_sd_reg(dev, info, reset);
  417. }
  418. /**
  419. * irdma_add_sd_table_entry - Adds a segment descriptor to the table
  420. * @hw: pointer to our hw struct
  421. * @hmc_info: pointer to the HMC configuration information struct
  422. * @sd_index: segment descriptor index to manipulate
  423. * @type: what type of segment descriptor we're manipulating
  424. * @direct_mode_sz: size to alloc in direct mode
  425. */
  426. int irdma_add_sd_table_entry(struct irdma_hw *hw,
  427. struct irdma_hmc_info *hmc_info, u32 sd_index,
  428. enum irdma_sd_entry_type type, u64 direct_mode_sz)
  429. {
  430. struct irdma_hmc_sd_entry *sd_entry;
  431. struct irdma_dma_mem dma_mem;
  432. u64 alloc_len;
  433. sd_entry = &hmc_info->sd_table.sd_entry[sd_index];
  434. if (!sd_entry->valid) {
  435. if (type == IRDMA_SD_TYPE_PAGED)
  436. alloc_len = IRDMA_HMC_PAGED_BP_SIZE;
  437. else
  438. alloc_len = direct_mode_sz;
  439. /* allocate a 4K pd page or 2M backing page */
  440. dma_mem.size = ALIGN(alloc_len, IRDMA_HMC_PD_BP_BUF_ALIGNMENT);
  441. dma_mem.va = dma_alloc_coherent(hw->device, dma_mem.size,
  442. &dma_mem.pa, GFP_KERNEL);
  443. if (!dma_mem.va)
  444. return -ENOMEM;
  445. if (type == IRDMA_SD_TYPE_PAGED) {
  446. struct irdma_virt_mem *vmem =
  447. &sd_entry->u.pd_table.pd_entry_virt_mem;
  448. vmem->size = sizeof(struct irdma_hmc_pd_entry) * 512;
  449. vmem->va = kzalloc(vmem->size, GFP_KERNEL);
  450. if (!vmem->va) {
  451. dma_free_coherent(hw->device, dma_mem.size,
  452. dma_mem.va, dma_mem.pa);
  453. dma_mem.va = NULL;
  454. return -ENOMEM;
  455. }
  456. sd_entry->u.pd_table.pd_entry = vmem->va;
  457. memcpy(&sd_entry->u.pd_table.pd_page_addr, &dma_mem,
  458. sizeof(sd_entry->u.pd_table.pd_page_addr));
  459. } else {
  460. memcpy(&sd_entry->u.bp.addr, &dma_mem,
  461. sizeof(sd_entry->u.bp.addr));
  462. sd_entry->u.bp.sd_pd_index = sd_index;
  463. }
  464. hmc_info->sd_table.sd_entry[sd_index].entry_type = type;
  465. hmc_info->sd_table.use_cnt++;
  466. }
  467. if (sd_entry->entry_type == IRDMA_SD_TYPE_DIRECT)
  468. sd_entry->u.bp.use_cnt++;
  469. return 0;
  470. }
  471. /**
  472. * irdma_add_pd_table_entry - Adds page descriptor to the specified table
  473. * @dev: pointer to our device structure
  474. * @hmc_info: pointer to the HMC configuration information structure
  475. * @pd_index: which page descriptor index to manipulate
  476. * @rsrc_pg: if not NULL, use preallocated page instead of allocating new one.
  477. *
  478. * This function:
  479. * 1. Initializes the pd entry
  480. * 2. Adds pd_entry in the pd_table
  481. * 3. Mark the entry valid in irdma_hmc_pd_entry structure
  482. * 4. Initializes the pd_entry's ref count to 1
  483. * assumptions:
  484. * 1. The memory for pd should be pinned down, physically contiguous and
  485. * aligned on 4K boundary and zeroed memory.
  486. * 2. It should be 4K in size.
  487. */
  488. int irdma_add_pd_table_entry(struct irdma_sc_dev *dev,
  489. struct irdma_hmc_info *hmc_info, u32 pd_index,
  490. struct irdma_dma_mem *rsrc_pg)
  491. {
  492. struct irdma_hmc_pd_table *pd_table;
  493. struct irdma_hmc_pd_entry *pd_entry;
  494. struct irdma_dma_mem mem;
  495. struct irdma_dma_mem *page = &mem;
  496. u32 sd_idx, rel_pd_idx;
  497. u64 *pd_addr;
  498. u64 page_desc;
  499. if (pd_index / IRDMA_HMC_PD_CNT_IN_SD >= hmc_info->sd_table.sd_cnt)
  500. return -EINVAL;
  501. sd_idx = (pd_index / IRDMA_HMC_PD_CNT_IN_SD);
  502. if (hmc_info->sd_table.sd_entry[sd_idx].entry_type !=
  503. IRDMA_SD_TYPE_PAGED)
  504. return 0;
  505. rel_pd_idx = (pd_index % IRDMA_HMC_PD_CNT_IN_SD);
  506. pd_table = &hmc_info->sd_table.sd_entry[sd_idx].u.pd_table;
  507. pd_entry = &pd_table->pd_entry[rel_pd_idx];
  508. if (!pd_entry->valid) {
  509. if (rsrc_pg) {
  510. pd_entry->rsrc_pg = true;
  511. page = rsrc_pg;
  512. } else {
  513. page->size = ALIGN(IRDMA_HMC_PAGED_BP_SIZE,
  514. IRDMA_HMC_PD_BP_BUF_ALIGNMENT);
  515. page->va = dma_alloc_coherent(dev->hw->device,
  516. page->size, &page->pa,
  517. GFP_KERNEL);
  518. if (!page->va)
  519. return -ENOMEM;
  520. pd_entry->rsrc_pg = false;
  521. }
  522. memcpy(&pd_entry->bp.addr, page, sizeof(pd_entry->bp.addr));
  523. pd_entry->bp.sd_pd_index = pd_index;
  524. pd_entry->bp.entry_type = IRDMA_SD_TYPE_PAGED;
  525. page_desc = page->pa | 0x1;
  526. pd_addr = pd_table->pd_page_addr.va;
  527. pd_addr += rel_pd_idx;
  528. memcpy(pd_addr, &page_desc, sizeof(*pd_addr));
  529. pd_entry->sd_index = sd_idx;
  530. pd_entry->valid = true;
  531. pd_table->use_cnt++;
  532. irdma_invalidate_pf_hmc_pd(dev, sd_idx, rel_pd_idx);
  533. }
  534. pd_entry->bp.use_cnt++;
  535. return 0;
  536. }
  537. /**
  538. * irdma_remove_pd_bp - remove a backing page from a page descriptor
  539. * @dev: pointer to our HW structure
  540. * @hmc_info: pointer to the HMC configuration information structure
  541. * @idx: the page index
  542. *
  543. * This function:
  544. * 1. Marks the entry in pd table (for paged address mode) or in sd table
  545. * (for direct address mode) invalid.
  546. * 2. Write to register PMPDINV to invalidate the backing page in FV cache
  547. * 3. Decrement the ref count for the pd _entry
  548. * assumptions:
  549. * 1. Caller can deallocate the memory used by backing storage after this
  550. * function returns.
  551. */
  552. int irdma_remove_pd_bp(struct irdma_sc_dev *dev,
  553. struct irdma_hmc_info *hmc_info, u32 idx)
  554. {
  555. struct irdma_hmc_pd_entry *pd_entry;
  556. struct irdma_hmc_pd_table *pd_table;
  557. struct irdma_hmc_sd_entry *sd_entry;
  558. u32 sd_idx, rel_pd_idx;
  559. struct irdma_dma_mem *mem;
  560. u64 *pd_addr;
  561. sd_idx = idx / IRDMA_HMC_PD_CNT_IN_SD;
  562. rel_pd_idx = idx % IRDMA_HMC_PD_CNT_IN_SD;
  563. if (sd_idx >= hmc_info->sd_table.sd_cnt)
  564. return -EINVAL;
  565. sd_entry = &hmc_info->sd_table.sd_entry[sd_idx];
  566. if (sd_entry->entry_type != IRDMA_SD_TYPE_PAGED)
  567. return -EINVAL;
  568. pd_table = &hmc_info->sd_table.sd_entry[sd_idx].u.pd_table;
  569. pd_entry = &pd_table->pd_entry[rel_pd_idx];
  570. if (--pd_entry->bp.use_cnt)
  571. return 0;
  572. pd_entry->valid = false;
  573. pd_table->use_cnt--;
  574. pd_addr = pd_table->pd_page_addr.va;
  575. pd_addr += rel_pd_idx;
  576. memset(pd_addr, 0, sizeof(u64));
  577. irdma_invalidate_pf_hmc_pd(dev, sd_idx, idx);
  578. if (!pd_entry->rsrc_pg) {
  579. mem = &pd_entry->bp.addr;
  580. if (!mem || !mem->va)
  581. return -EINVAL;
  582. dma_free_coherent(dev->hw->device, mem->size, mem->va,
  583. mem->pa);
  584. mem->va = NULL;
  585. }
  586. if (!pd_table->use_cnt)
  587. kfree(pd_table->pd_entry_virt_mem.va);
  588. return 0;
  589. }
  590. /**
  591. * irdma_prep_remove_sd_bp - Prepares to remove a backing page from a sd entry
  592. * @hmc_info: pointer to the HMC configuration information structure
  593. * @idx: the page index
  594. */
  595. int irdma_prep_remove_sd_bp(struct irdma_hmc_info *hmc_info, u32 idx)
  596. {
  597. struct irdma_hmc_sd_entry *sd_entry;
  598. sd_entry = &hmc_info->sd_table.sd_entry[idx];
  599. if (--sd_entry->u.bp.use_cnt)
  600. return -EBUSY;
  601. hmc_info->sd_table.use_cnt--;
  602. sd_entry->valid = false;
  603. return 0;
  604. }
  605. /**
  606. * irdma_prep_remove_pd_page - Prepares to remove a PD page from sd entry.
  607. * @hmc_info: pointer to the HMC configuration information structure
  608. * @idx: segment descriptor index to find the relevant page descriptor
  609. */
  610. int irdma_prep_remove_pd_page(struct irdma_hmc_info *hmc_info, u32 idx)
  611. {
  612. struct irdma_hmc_sd_entry *sd_entry;
  613. sd_entry = &hmc_info->sd_table.sd_entry[idx];
  614. if (sd_entry->u.pd_table.use_cnt)
  615. return -EBUSY;
  616. sd_entry->valid = false;
  617. hmc_info->sd_table.use_cnt--;
  618. return 0;
  619. }