nfp_shared_buf.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
  2. /*
  3. * Copyright (C) 2018 Netronome Systems, Inc.
  4. *
  5. * This software is dual licensed under the GNU General License Version 2,
  6. * June 1991 as shown in the file COPYING in the top-level directory of this
  7. * source tree or the BSD 2-Clause License provided below. You have the
  8. * option to license this software under the complete terms of either license.
  9. *
  10. * The BSD 2-Clause License:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * 1. Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * 2. Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include <linux/kernel.h>
  35. #include <net/devlink.h>
  36. #include "nfpcore/nfp_cpp.h"
  37. #include "nfpcore/nfp_nffw.h"
  38. #include "nfp_abi.h"
  39. #include "nfp_app.h"
  40. #include "nfp_main.h"
  41. static u32 nfp_shared_buf_pool_unit(struct nfp_pf *pf, unsigned int sb)
  42. {
  43. __le32 sb_id = cpu_to_le32(sb);
  44. unsigned int i;
  45. for (i = 0; i < pf->num_shared_bufs; i++)
  46. if (pf->shared_bufs[i].id == sb_id)
  47. return le32_to_cpu(pf->shared_bufs[i].pool_size_unit);
  48. WARN_ON_ONCE(1);
  49. return 0;
  50. }
  51. int nfp_shared_buf_pool_get(struct nfp_pf *pf, unsigned int sb, u16 pool_index,
  52. struct devlink_sb_pool_info *pool_info)
  53. {
  54. struct nfp_shared_buf_pool_info_get get_data;
  55. struct nfp_shared_buf_pool_id id = {
  56. .shared_buf = cpu_to_le32(sb),
  57. .pool = cpu_to_le32(pool_index),
  58. };
  59. unsigned int unit_size;
  60. int n;
  61. unit_size = nfp_shared_buf_pool_unit(pf, sb);
  62. if (!unit_size)
  63. return -EINVAL;
  64. n = nfp_mbox_cmd(pf, NFP_MBOX_POOL_GET, &id, sizeof(id),
  65. &get_data, sizeof(get_data));
  66. if (n < 0)
  67. return n;
  68. if (n < sizeof(get_data))
  69. return -EIO;
  70. pool_info->pool_type = le32_to_cpu(get_data.pool_type);
  71. pool_info->threshold_type = le32_to_cpu(get_data.threshold_type);
  72. pool_info->size = le32_to_cpu(get_data.size) * unit_size;
  73. return 0;
  74. }
  75. int nfp_shared_buf_pool_set(struct nfp_pf *pf, unsigned int sb,
  76. u16 pool_index, u32 size,
  77. enum devlink_sb_threshold_type threshold_type)
  78. {
  79. struct nfp_shared_buf_pool_info_set set_data = {
  80. .id = {
  81. .shared_buf = cpu_to_le32(sb),
  82. .pool = cpu_to_le32(pool_index),
  83. },
  84. .threshold_type = cpu_to_le32(threshold_type),
  85. };
  86. unsigned int unit_size;
  87. unit_size = nfp_shared_buf_pool_unit(pf, sb);
  88. if (!unit_size || size % unit_size)
  89. return -EINVAL;
  90. set_data.size = cpu_to_le32(size / unit_size);
  91. return nfp_mbox_cmd(pf, NFP_MBOX_POOL_SET, &set_data, sizeof(set_data),
  92. NULL, 0);
  93. }
  94. int nfp_shared_buf_register(struct nfp_pf *pf)
  95. {
  96. struct devlink *devlink = priv_to_devlink(pf);
  97. unsigned int i, num_entries, entry_sz;
  98. struct nfp_cpp_area *sb_desc_area;
  99. u8 __iomem *sb_desc;
  100. int n, err;
  101. if (!pf->mbox)
  102. return 0;
  103. n = nfp_pf_rtsym_read_optional(pf, NFP_SHARED_BUF_COUNT_SYM_NAME, 0);
  104. if (n <= 0)
  105. return n;
  106. num_entries = n;
  107. sb_desc = nfp_pf_map_rtsym(pf, "sb_tbl", NFP_SHARED_BUF_TABLE_SYM_NAME,
  108. num_entries * sizeof(pf->shared_bufs[0]),
  109. &sb_desc_area);
  110. if (IS_ERR(sb_desc))
  111. return PTR_ERR(sb_desc);
  112. entry_sz = nfp_cpp_area_size(sb_desc_area) / num_entries;
  113. pf->shared_bufs = kmalloc_array(num_entries, sizeof(pf->shared_bufs[0]),
  114. GFP_KERNEL);
  115. if (!pf->shared_bufs) {
  116. err = -ENOMEM;
  117. goto err_release_area;
  118. }
  119. for (i = 0; i < num_entries; i++) {
  120. struct nfp_shared_buf *sb = &pf->shared_bufs[i];
  121. /* Entries may be larger in future FW */
  122. memcpy_fromio(sb, sb_desc + i * entry_sz, sizeof(*sb));
  123. err = devlink_sb_register(devlink,
  124. le32_to_cpu(sb->id),
  125. le32_to_cpu(sb->size),
  126. le16_to_cpu(sb->ingress_pools_count),
  127. le16_to_cpu(sb->egress_pools_count),
  128. le16_to_cpu(sb->ingress_tc_count),
  129. le16_to_cpu(sb->egress_tc_count));
  130. if (err)
  131. goto err_unreg_prev;
  132. }
  133. pf->num_shared_bufs = num_entries;
  134. nfp_cpp_area_release_free(sb_desc_area);
  135. return 0;
  136. err_unreg_prev:
  137. while (i--)
  138. devlink_sb_unregister(devlink,
  139. le32_to_cpu(pf->shared_bufs[i].id));
  140. kfree(pf->shared_bufs);
  141. err_release_area:
  142. nfp_cpp_area_release_free(sb_desc_area);
  143. return err;
  144. }
  145. void nfp_shared_buf_unregister(struct nfp_pf *pf)
  146. {
  147. struct devlink *devlink = priv_to_devlink(pf);
  148. unsigned int i;
  149. for (i = 0; i < pf->num_shared_bufs; i++)
  150. devlink_sb_unregister(devlink,
  151. le32_to_cpu(pf->shared_bufs[i].id));
  152. kfree(pf->shared_bufs);
  153. }