nfp_devlink.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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)
  81. {
  82. struct nfp_pf *pf = devlink_priv(devlink);
  83. struct nfp_eth_table_port eth_port;
  84. int ret;
  85. if (count < 2)
  86. return -EINVAL;
  87. mutex_lock(&pf->lock);
  88. rtnl_lock();
  89. ret = nfp_devlink_fill_eth_port_from_id(pf, port_index, &eth_port);
  90. rtnl_unlock();
  91. if (ret)
  92. goto out;
  93. if (eth_port.is_split || eth_port.port_lanes % count) {
  94. ret = -EINVAL;
  95. goto out;
  96. }
  97. ret = nfp_devlink_set_lanes(pf, eth_port.index,
  98. eth_port.port_lanes / count);
  99. out:
  100. mutex_unlock(&pf->lock);
  101. return ret;
  102. }
  103. static int
  104. nfp_devlink_port_unsplit(struct devlink *devlink, unsigned int port_index)
  105. {
  106. struct nfp_pf *pf = devlink_priv(devlink);
  107. struct nfp_eth_table_port eth_port;
  108. int ret;
  109. mutex_lock(&pf->lock);
  110. rtnl_lock();
  111. ret = nfp_devlink_fill_eth_port_from_id(pf, port_index, &eth_port);
  112. rtnl_unlock();
  113. if (ret)
  114. goto out;
  115. if (!eth_port.is_split) {
  116. ret = -EINVAL;
  117. goto out;
  118. }
  119. ret = nfp_devlink_set_lanes(pf, eth_port.index, eth_port.port_lanes);
  120. out:
  121. mutex_unlock(&pf->lock);
  122. return ret;
  123. }
  124. static int nfp_devlink_eswitch_mode_get(struct devlink *devlink, u16 *mode)
  125. {
  126. struct nfp_pf *pf = devlink_priv(devlink);
  127. return nfp_app_eswitch_mode_get(pf->app, mode);
  128. }
  129. const struct devlink_ops nfp_devlink_ops = {
  130. .port_split = nfp_devlink_port_split,
  131. .port_unsplit = nfp_devlink_port_unsplit,
  132. .eswitch_mode_get = nfp_devlink_eswitch_mode_get,
  133. };
  134. int nfp_devlink_port_register(struct nfp_app *app, struct nfp_port *port)
  135. {
  136. struct nfp_eth_table_port eth_port;
  137. struct devlink *devlink;
  138. int ret;
  139. rtnl_lock();
  140. ret = nfp_devlink_fill_eth_port(port, &eth_port);
  141. rtnl_unlock();
  142. if (ret)
  143. return ret;
  144. devlink_port_type_eth_set(&port->dl_port, port->netdev);
  145. if (eth_port.is_split)
  146. devlink_port_split_set(&port->dl_port, eth_port.label_port);
  147. devlink = priv_to_devlink(app->pf);
  148. return devlink_port_register(devlink, &port->dl_port, port->eth_id);
  149. }
  150. void nfp_devlink_port_unregister(struct nfp_port *port)
  151. {
  152. devlink_port_unregister(&port->dl_port);
  153. }