nfp_shared_buf.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
  2. /* Copyright (C) 2018 Netronome Systems, Inc. */
  3. #include <linux/kernel.h>
  4. #include <net/devlink.h>
  5. #include "nfpcore/nfp_cpp.h"
  6. #include "nfpcore/nfp_nffw.h"
  7. #include "nfp_abi.h"
  8. #include "nfp_app.h"
  9. #include "nfp_main.h"
  10. static u32 nfp_shared_buf_pool_unit(struct nfp_pf *pf, unsigned int sb)
  11. {
  12. __le32 sb_id = cpu_to_le32(sb);
  13. unsigned int i;
  14. for (i = 0; i < pf->num_shared_bufs; i++)
  15. if (pf->shared_bufs[i].id == sb_id)
  16. return le32_to_cpu(pf->shared_bufs[i].pool_size_unit);
  17. WARN_ON_ONCE(1);
  18. return 0;
  19. }
  20. int nfp_shared_buf_pool_get(struct nfp_pf *pf, unsigned int sb, u16 pool_index,
  21. struct devlink_sb_pool_info *pool_info)
  22. {
  23. struct nfp_shared_buf_pool_info_get get_data;
  24. struct nfp_shared_buf_pool_id id = {
  25. .shared_buf = cpu_to_le32(sb),
  26. .pool = cpu_to_le32(pool_index),
  27. };
  28. unsigned int unit_size;
  29. int n;
  30. unit_size = nfp_shared_buf_pool_unit(pf, sb);
  31. if (!unit_size)
  32. return -EINVAL;
  33. n = nfp_mbox_cmd(pf, NFP_MBOX_POOL_GET, &id, sizeof(id),
  34. &get_data, sizeof(get_data));
  35. if (n < 0)
  36. return n;
  37. if (n < sizeof(get_data))
  38. return -EIO;
  39. pool_info->pool_type = le32_to_cpu(get_data.pool_type);
  40. pool_info->threshold_type = le32_to_cpu(get_data.threshold_type);
  41. pool_info->size = le32_to_cpu(get_data.size) * unit_size;
  42. pool_info->cell_size = unit_size;
  43. return 0;
  44. }
  45. int nfp_shared_buf_pool_set(struct nfp_pf *pf, unsigned int sb,
  46. u16 pool_index, u32 size,
  47. enum devlink_sb_threshold_type threshold_type)
  48. {
  49. struct nfp_shared_buf_pool_info_set set_data = {
  50. .id = {
  51. .shared_buf = cpu_to_le32(sb),
  52. .pool = cpu_to_le32(pool_index),
  53. },
  54. .threshold_type = cpu_to_le32(threshold_type),
  55. };
  56. unsigned int unit_size;
  57. unit_size = nfp_shared_buf_pool_unit(pf, sb);
  58. if (!unit_size || size % unit_size)
  59. return -EINVAL;
  60. set_data.size = cpu_to_le32(size / unit_size);
  61. return nfp_mbox_cmd(pf, NFP_MBOX_POOL_SET, &set_data, sizeof(set_data),
  62. NULL, 0);
  63. }
  64. int nfp_shared_buf_register(struct nfp_pf *pf)
  65. {
  66. struct devlink *devlink = priv_to_devlink(pf);
  67. unsigned int i, num_entries, entry_sz;
  68. struct nfp_cpp_area *sb_desc_area;
  69. u8 __iomem *sb_desc;
  70. int n, err;
  71. if (!pf->mbox)
  72. return 0;
  73. n = nfp_pf_rtsym_read_optional(pf, NFP_SHARED_BUF_COUNT_SYM_NAME, 0);
  74. if (n <= 0)
  75. return n;
  76. num_entries = n;
  77. sb_desc = nfp_pf_map_rtsym(pf, "sb_tbl", NFP_SHARED_BUF_TABLE_SYM_NAME,
  78. num_entries * sizeof(pf->shared_bufs[0]),
  79. &sb_desc_area);
  80. if (IS_ERR(sb_desc))
  81. return PTR_ERR(sb_desc);
  82. entry_sz = nfp_cpp_area_size(sb_desc_area) / num_entries;
  83. pf->shared_bufs = kmalloc_array(num_entries, sizeof(pf->shared_bufs[0]),
  84. GFP_KERNEL);
  85. if (!pf->shared_bufs) {
  86. err = -ENOMEM;
  87. goto err_release_area;
  88. }
  89. for (i = 0; i < num_entries; i++) {
  90. struct nfp_shared_buf *sb = &pf->shared_bufs[i];
  91. /* Entries may be larger in future FW */
  92. memcpy_fromio(sb, sb_desc + i * entry_sz, sizeof(*sb));
  93. err = devlink_sb_register(devlink,
  94. le32_to_cpu(sb->id),
  95. le32_to_cpu(sb->size),
  96. le16_to_cpu(sb->ingress_pools_count),
  97. le16_to_cpu(sb->egress_pools_count),
  98. le16_to_cpu(sb->ingress_tc_count),
  99. le16_to_cpu(sb->egress_tc_count));
  100. if (err)
  101. goto err_unreg_prev;
  102. }
  103. pf->num_shared_bufs = num_entries;
  104. nfp_cpp_area_release_free(sb_desc_area);
  105. return 0;
  106. err_unreg_prev:
  107. while (i--)
  108. devlink_sb_unregister(devlink,
  109. le32_to_cpu(pf->shared_bufs[i].id));
  110. kfree(pf->shared_bufs);
  111. err_release_area:
  112. nfp_cpp_area_release_free(sb_desc_area);
  113. return err;
  114. }
  115. void nfp_shared_buf_unregister(struct nfp_pf *pf)
  116. {
  117. struct devlink *devlink = priv_to_devlink(pf);
  118. unsigned int i;
  119. for (i = 0; i < pf->num_shared_bufs; i++)
  120. devlink_sb_unregister(devlink,
  121. le32_to_cpu(pf->shared_bufs[i].id));
  122. kfree(pf->shared_bufs);
  123. }