nfp_net_sriov.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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/bitfield.h>
  34. #include <linux/errno.h>
  35. #include <linux/etherdevice.h>
  36. #include <linux/if_link.h>
  37. #include <linux/if_ether.h>
  38. #include "nfpcore/nfp_cpp.h"
  39. #include "nfp_app.h"
  40. #include "nfp_main.h"
  41. #include "nfp_net_ctrl.h"
  42. #include "nfp_net.h"
  43. #include "nfp_net_sriov.h"
  44. static int
  45. nfp_net_sriov_check(struct nfp_app *app, int vf, u16 cap, const char *msg)
  46. {
  47. u16 cap_vf;
  48. if (!app || !app->pf->vfcfg_tbl2)
  49. return -EOPNOTSUPP;
  50. cap_vf = readw(app->pf->vfcfg_tbl2 + NFP_NET_VF_CFG_MB_CAP);
  51. if ((cap_vf & cap) != cap) {
  52. nfp_warn(app->pf->cpp, "ndo_set_vf_%s not supported\n", msg);
  53. return -EOPNOTSUPP;
  54. }
  55. if (vf < 0 || vf >= app->pf->num_vfs) {
  56. nfp_warn(app->pf->cpp, "invalid VF id %d\n", vf);
  57. return -EINVAL;
  58. }
  59. return 0;
  60. }
  61. static int
  62. nfp_net_sriov_update(struct nfp_app *app, int vf, u16 update, const char *msg)
  63. {
  64. struct nfp_net *nn;
  65. int ret;
  66. /* Write update info to mailbox in VF config symbol */
  67. writeb(vf, app->pf->vfcfg_tbl2 + NFP_NET_VF_CFG_MB_VF_NUM);
  68. writew(update, app->pf->vfcfg_tbl2 + NFP_NET_VF_CFG_MB_UPD);
  69. nn = list_first_entry(&app->pf->vnics, struct nfp_net, vnic_list);
  70. /* Signal VF reconfiguration */
  71. ret = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_VF);
  72. if (ret)
  73. return ret;
  74. ret = readw(app->pf->vfcfg_tbl2 + NFP_NET_VF_CFG_MB_RET);
  75. if (ret)
  76. nfp_warn(app->pf->cpp,
  77. "FW refused VF %s update with errno: %d\n", msg, ret);
  78. return -ret;
  79. }
  80. int nfp_app_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
  81. {
  82. struct nfp_app *app = nfp_app_from_netdev(netdev);
  83. unsigned int vf_offset;
  84. int err;
  85. err = nfp_net_sriov_check(app, vf, NFP_NET_VF_CFG_MB_CAP_MAC, "mac");
  86. if (err)
  87. return err;
  88. if (is_multicast_ether_addr(mac)) {
  89. nfp_warn(app->pf->cpp,
  90. "invalid Ethernet address %pM for VF id %d\n",
  91. mac, vf);
  92. return -EINVAL;
  93. }
  94. /* Write MAC to VF entry in VF config symbol */
  95. vf_offset = NFP_NET_VF_CFG_MB_SZ + vf * NFP_NET_VF_CFG_SZ;
  96. writel(get_unaligned_be32(mac), app->pf->vfcfg_tbl2 + vf_offset);
  97. writew(get_unaligned_be16(mac + 4),
  98. app->pf->vfcfg_tbl2 + vf_offset + NFP_NET_VF_CFG_MAC_LO);
  99. err = nfp_net_sriov_update(app, vf, NFP_NET_VF_CFG_MB_UPD_MAC, "MAC");
  100. if (!err)
  101. nfp_info(app->pf->cpp,
  102. "MAC %pM set on VF %d, reload the VF driver to make this change effective.\n",
  103. mac, vf);
  104. return err;
  105. }
  106. int nfp_app_set_vf_vlan(struct net_device *netdev, int vf, u16 vlan, u8 qos,
  107. __be16 vlan_proto)
  108. {
  109. struct nfp_app *app = nfp_app_from_netdev(netdev);
  110. unsigned int vf_offset;
  111. u16 vlan_tci;
  112. int err;
  113. err = nfp_net_sriov_check(app, vf, NFP_NET_VF_CFG_MB_CAP_VLAN, "vlan");
  114. if (err)
  115. return err;
  116. if (vlan_proto != htons(ETH_P_8021Q))
  117. return -EOPNOTSUPP;
  118. if (vlan > 4095 || qos > 7) {
  119. nfp_warn(app->pf->cpp,
  120. "invalid vlan id or qos for VF id %d\n", vf);
  121. return -EINVAL;
  122. }
  123. /* Write VLAN tag to VF entry in VF config symbol */
  124. vlan_tci = FIELD_PREP(NFP_NET_VF_CFG_VLAN_VID, vlan) |
  125. FIELD_PREP(NFP_NET_VF_CFG_VLAN_QOS, qos);
  126. vf_offset = NFP_NET_VF_CFG_MB_SZ + vf * NFP_NET_VF_CFG_SZ;
  127. writew(vlan_tci, app->pf->vfcfg_tbl2 + vf_offset + NFP_NET_VF_CFG_VLAN);
  128. return nfp_net_sriov_update(app, vf, NFP_NET_VF_CFG_MB_UPD_VLAN,
  129. "vlan");
  130. }
  131. int nfp_app_set_vf_spoofchk(struct net_device *netdev, int vf, bool enable)
  132. {
  133. struct nfp_app *app = nfp_app_from_netdev(netdev);
  134. unsigned int vf_offset;
  135. u8 vf_ctrl;
  136. int err;
  137. err = nfp_net_sriov_check(app, vf, NFP_NET_VF_CFG_MB_CAP_SPOOF,
  138. "spoofchk");
  139. if (err)
  140. return err;
  141. /* Write spoof check control bit to VF entry in VF config symbol */
  142. vf_offset = NFP_NET_VF_CFG_MB_SZ + vf * NFP_NET_VF_CFG_SZ +
  143. NFP_NET_VF_CFG_CTRL;
  144. vf_ctrl = readb(app->pf->vfcfg_tbl2 + vf_offset);
  145. vf_ctrl &= ~NFP_NET_VF_CFG_CTRL_SPOOF;
  146. vf_ctrl |= FIELD_PREP(NFP_NET_VF_CFG_CTRL_SPOOF, enable);
  147. writeb(vf_ctrl, app->pf->vfcfg_tbl2 + vf_offset);
  148. return nfp_net_sriov_update(app, vf, NFP_NET_VF_CFG_MB_UPD_SPOOF,
  149. "spoofchk");
  150. }
  151. int nfp_app_set_vf_link_state(struct net_device *netdev, int vf,
  152. int link_state)
  153. {
  154. struct nfp_app *app = nfp_app_from_netdev(netdev);
  155. unsigned int vf_offset;
  156. u8 vf_ctrl;
  157. int err;
  158. err = nfp_net_sriov_check(app, vf, NFP_NET_VF_CFG_MB_CAP_LINK_STATE,
  159. "link_state");
  160. if (err)
  161. return err;
  162. switch (link_state) {
  163. case IFLA_VF_LINK_STATE_AUTO:
  164. case IFLA_VF_LINK_STATE_ENABLE:
  165. case IFLA_VF_LINK_STATE_DISABLE:
  166. break;
  167. default:
  168. return -EINVAL;
  169. }
  170. /* Write link state to VF entry in VF config symbol */
  171. vf_offset = NFP_NET_VF_CFG_MB_SZ + vf * NFP_NET_VF_CFG_SZ +
  172. NFP_NET_VF_CFG_CTRL;
  173. vf_ctrl = readb(app->pf->vfcfg_tbl2 + vf_offset);
  174. vf_ctrl &= ~NFP_NET_VF_CFG_CTRL_LINK_STATE;
  175. vf_ctrl |= FIELD_PREP(NFP_NET_VF_CFG_CTRL_LINK_STATE, link_state);
  176. writeb(vf_ctrl, app->pf->vfcfg_tbl2 + vf_offset);
  177. return nfp_net_sriov_update(app, vf, NFP_NET_VF_CFG_MB_UPD_LINK_STATE,
  178. "link state");
  179. }
  180. int nfp_app_get_vf_config(struct net_device *netdev, int vf,
  181. struct ifla_vf_info *ivi)
  182. {
  183. struct nfp_app *app = nfp_app_from_netdev(netdev);
  184. unsigned int vf_offset;
  185. u16 vlan_tci;
  186. u32 mac_hi;
  187. u16 mac_lo;
  188. u8 flags;
  189. int err;
  190. err = nfp_net_sriov_check(app, vf, 0, "");
  191. if (err)
  192. return err;
  193. vf_offset = NFP_NET_VF_CFG_MB_SZ + vf * NFP_NET_VF_CFG_SZ;
  194. mac_hi = readl(app->pf->vfcfg_tbl2 + vf_offset);
  195. mac_lo = readw(app->pf->vfcfg_tbl2 + vf_offset + NFP_NET_VF_CFG_MAC_LO);
  196. flags = readb(app->pf->vfcfg_tbl2 + vf_offset + NFP_NET_VF_CFG_CTRL);
  197. vlan_tci = readw(app->pf->vfcfg_tbl2 + vf_offset + NFP_NET_VF_CFG_VLAN);
  198. memset(ivi, 0, sizeof(*ivi));
  199. ivi->vf = vf;
  200. put_unaligned_be32(mac_hi, &ivi->mac[0]);
  201. put_unaligned_be16(mac_lo, &ivi->mac[4]);
  202. ivi->vlan = FIELD_GET(NFP_NET_VF_CFG_VLAN_VID, vlan_tci);
  203. ivi->qos = FIELD_GET(NFP_NET_VF_CFG_VLAN_QOS, vlan_tci);
  204. ivi->spoofchk = FIELD_GET(NFP_NET_VF_CFG_CTRL_SPOOF, flags);
  205. ivi->linkstate = FIELD_GET(NFP_NET_VF_CFG_CTRL_LINK_STATE, flags);
  206. return 0;
  207. }