nfp_net_ctrl.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (C) 2018 Netronome Systems, Inc.
  3. *
  4. * This software is dual licensed under the GNU General License Version 2,
  5. * June 1991 as shown in the file COPYING in the top-level directory of this
  6. * source tree or the BSD 2-Clause License provided below. You have the
  7. * option to license this software under the complete terms of either license.
  8. *
  9. * The BSD 2-Clause License:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * 2. Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/bitfield.h>
  34. #include <linux/device.h>
  35. #include <linux/kernel.h>
  36. #include <linux/types.h>
  37. #include "nfp_net_ctrl.h"
  38. #include "nfp_net.h"
  39. static void nfp_net_tlv_caps_reset(struct nfp_net_tlv_caps *caps)
  40. {
  41. memset(caps, 0, sizeof(*caps));
  42. caps->me_freq_mhz = 1200;
  43. caps->mbox_off = NFP_NET_CFG_MBOX_BASE;
  44. caps->mbox_len = NFP_NET_CFG_MBOX_VAL_MAX_SZ;
  45. }
  46. int nfp_net_tlv_caps_parse(struct device *dev, u8 __iomem *ctrl_mem,
  47. struct nfp_net_tlv_caps *caps)
  48. {
  49. u8 __iomem *data = ctrl_mem + NFP_NET_CFG_TLV_BASE;
  50. u8 __iomem *end = ctrl_mem + NFP_NET_CFG_BAR_SZ;
  51. u32 hdr;
  52. nfp_net_tlv_caps_reset(caps);
  53. hdr = readl(data);
  54. if (!hdr)
  55. return 0;
  56. while (true) {
  57. unsigned int length, offset;
  58. u32 hdr = readl(data);
  59. length = FIELD_GET(NFP_NET_CFG_TLV_HEADER_LENGTH, hdr);
  60. offset = data - ctrl_mem;
  61. /* Advance past the header */
  62. data += 4;
  63. if (length % NFP_NET_CFG_TLV_LENGTH_INC) {
  64. dev_err(dev, "TLV size not multiple of %u len:%u\n",
  65. NFP_NET_CFG_TLV_LENGTH_INC, length);
  66. return -EINVAL;
  67. }
  68. if (data + length > end) {
  69. dev_err(dev, "oversized TLV offset:%u len:%u\n",
  70. offset, length);
  71. return -EINVAL;
  72. }
  73. switch (FIELD_GET(NFP_NET_CFG_TLV_HEADER_TYPE, hdr)) {
  74. case NFP_NET_CFG_TLV_TYPE_UNKNOWN:
  75. dev_err(dev, "NULL TLV at offset:%u\n", offset);
  76. return -EINVAL;
  77. case NFP_NET_CFG_TLV_TYPE_RESERVED:
  78. break;
  79. case NFP_NET_CFG_TLV_TYPE_END:
  80. if (!length)
  81. return 0;
  82. dev_err(dev, "END TLV should be empty, has len:%d\n",
  83. length);
  84. return -EINVAL;
  85. case NFP_NET_CFG_TLV_TYPE_ME_FREQ:
  86. if (length != 4) {
  87. dev_err(dev,
  88. "ME FREQ TLV should be 4B, is %dB\n",
  89. length);
  90. return -EINVAL;
  91. }
  92. caps->me_freq_mhz = readl(data);
  93. break;
  94. case NFP_NET_CFG_TLV_TYPE_MBOX:
  95. if (!length) {
  96. caps->mbox_off = 0;
  97. caps->mbox_len = 0;
  98. } else {
  99. caps->mbox_off = data - ctrl_mem;
  100. caps->mbox_len = length;
  101. }
  102. break;
  103. default:
  104. if (!FIELD_GET(NFP_NET_CFG_TLV_HEADER_REQUIRED, hdr))
  105. break;
  106. dev_err(dev, "unknown TLV type:%u offset:%u len:%u\n",
  107. FIELD_GET(NFP_NET_CFG_TLV_HEADER_TYPE, hdr),
  108. offset, length);
  109. return -EINVAL;
  110. }
  111. data += length;
  112. if (data + 4 > end) {
  113. dev_err(dev, "reached end of BAR without END TLV\n");
  114. return -EINVAL;
  115. }
  116. }
  117. /* Not reached */
  118. return -EINVAL;
  119. }