nfp_devlink.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (C) 2017 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/rtnetlink.h>
  34. #include <net/devlink.h>
  35. #include "nfpcore/nfp_nsp.h"
  36. #include "nfp_app.h"
  37. #include "nfp_main.h"
  38. #include "nfp_port.h"
  39. static int
  40. nfp_devlink_fill_eth_port(struct nfp_port *port,
  41. struct nfp_eth_table_port *copy)
  42. {
  43. struct nfp_eth_table_port *eth_port;
  44. eth_port = __nfp_port_get_eth_port(port);
  45. if (!eth_port)
  46. return -EINVAL;
  47. memcpy(copy, eth_port, sizeof(*eth_port));
  48. return 0;
  49. }
  50. static int
  51. nfp_devlink_fill_eth_port_from_id(struct nfp_pf *pf, unsigned int port_index,
  52. struct nfp_eth_table_port *copy)
  53. {
  54. struct nfp_port *port;
  55. port = nfp_port_from_id(pf, NFP_PORT_PHYS_PORT, port_index);
  56. return nfp_devlink_fill_eth_port(port, copy);
  57. }
  58. static int
  59. nfp_devlink_set_lanes(struct nfp_pf *pf, unsigned int idx, unsigned int lanes)
  60. {
  61. struct nfp_nsp *nsp;
  62. int ret;
  63. nsp = nfp_eth_config_start(pf->cpp, idx);
  64. if (IS_ERR(nsp))
  65. return PTR_ERR(nsp);
  66. ret = __nfp_eth_set_split(nsp, lanes);
  67. if (ret) {
  68. nfp_eth_config_cleanup_end(nsp);
  69. return ret;
  70. }
  71. ret = nfp_eth_config_commit_end(nsp);
  72. if (ret < 0)
  73. return ret;
  74. if (ret) /* no change */
  75. return 0;
  76. return nfp_net_refresh_port_table_sync(pf);
  77. }
  78. static int
  79. nfp_devlink_port_split(struct devlink *devlink, unsigned int port_index,
  80. unsigned int count, struct netlink_ext_ack *extack)
  81. {
  82. struct nfp_pf *pf = devlink_priv(devlink);
  83. struct nfp_eth_table_port eth_port;
  84. unsigned int lanes;
  85. int ret;
  86. if (count < 2)
  87. return -EINVAL;
  88. mutex_lock(&pf->lock);
  89. rtnl_lock();
  90. ret = nfp_devlink_fill_eth_port_from_id(pf, port_index, &eth_port);
  91. rtnl_unlock();
  92. if (ret)
  93. goto out;
  94. if (eth_port.is_split || eth_port.port_lanes % count) {
  95. ret = -EINVAL;
  96. goto out;
  97. }
  98. /* Special case the 100G CXP -> 2x40G split */
  99. lanes = eth_port.port_lanes / count;
  100. if (eth_port.lanes == 10 && count == 2)
  101. lanes = 8 / count;
  102. ret = nfp_devlink_set_lanes(pf, eth_port.index, lanes);
  103. out:
  104. mutex_unlock(&pf->lock);
  105. return ret;
  106. }
  107. static int
  108. nfp_devlink_port_unsplit(struct devlink *devlink, unsigned int port_index,
  109. struct netlink_ext_ack *extack)
  110. {
  111. struct nfp_pf *pf = devlink_priv(devlink);
  112. struct nfp_eth_table_port eth_port;
  113. unsigned int lanes;
  114. int ret;
  115. mutex_lock(&pf->lock);
  116. rtnl_lock();
  117. ret = nfp_devlink_fill_eth_port_from_id(pf, port_index, &eth_port);
  118. rtnl_unlock();
  119. if (ret)
  120. goto out;
  121. if (!eth_port.is_split) {
  122. ret = -EINVAL;
  123. goto out;
  124. }
  125. /* Special case the 100G CXP -> 2x40G unsplit */
  126. lanes = eth_port.port_lanes;
  127. if (eth_port.port_lanes == 8)
  128. lanes = 10;
  129. ret = nfp_devlink_set_lanes(pf, eth_port.index, lanes);
  130. out:
  131. mutex_unlock(&pf->lock);
  132. return ret;
  133. }
  134. static int
  135. nfp_devlink_sb_pool_get(struct devlink *devlink, unsigned int sb_index,
  136. u16 pool_index, struct devlink_sb_pool_info *pool_info)
  137. {
  138. struct nfp_pf *pf = devlink_priv(devlink);
  139. return nfp_shared_buf_pool_get(pf, sb_index, pool_index, pool_info);
  140. }
  141. static int
  142. nfp_devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index,
  143. u16 pool_index,
  144. u32 size, enum devlink_sb_threshold_type threshold_type)
  145. {
  146. struct nfp_pf *pf = devlink_priv(devlink);
  147. return nfp_shared_buf_pool_set(pf, sb_index, pool_index,
  148. size, threshold_type);
  149. }
  150. static int nfp_devlink_eswitch_mode_get(struct devlink *devlink, u16 *mode)
  151. {
  152. struct nfp_pf *pf = devlink_priv(devlink);
  153. return nfp_app_eswitch_mode_get(pf->app, mode);
  154. }
  155. static int nfp_devlink_eswitch_mode_set(struct devlink *devlink, u16 mode)
  156. {
  157. struct nfp_pf *pf = devlink_priv(devlink);
  158. int ret;
  159. mutex_lock(&pf->lock);
  160. ret = nfp_app_eswitch_mode_set(pf->app, mode);
  161. mutex_unlock(&pf->lock);
  162. return ret;
  163. }
  164. const struct devlink_ops nfp_devlink_ops = {
  165. .port_split = nfp_devlink_port_split,
  166. .port_unsplit = nfp_devlink_port_unsplit,
  167. .sb_pool_get = nfp_devlink_sb_pool_get,
  168. .sb_pool_set = nfp_devlink_sb_pool_set,
  169. .eswitch_mode_get = nfp_devlink_eswitch_mode_get,
  170. .eswitch_mode_set = nfp_devlink_eswitch_mode_set,
  171. };
  172. int nfp_devlink_port_register(struct nfp_app *app, struct nfp_port *port)
  173. {
  174. struct nfp_eth_table_port eth_port;
  175. struct devlink *devlink;
  176. int ret;
  177. rtnl_lock();
  178. ret = nfp_devlink_fill_eth_port(port, &eth_port);
  179. rtnl_unlock();
  180. if (ret)
  181. return ret;
  182. devlink_port_type_eth_set(&port->dl_port, port->netdev);
  183. devlink_port_attrs_set(&port->dl_port, DEVLINK_PORT_FLAVOUR_PHYSICAL,
  184. eth_port.label_port, eth_port.is_split,
  185. eth_port.label_subport);
  186. devlink = priv_to_devlink(app->pf);
  187. return devlink_port_register(devlink, &port->dl_port, port->eth_id);
  188. }
  189. void nfp_devlink_port_unregister(struct nfp_port *port)
  190. {
  191. devlink_port_unregister(&port->dl_port);
  192. }