nfp_port.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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/lockdep.h>
  34. #include <linux/netdevice.h>
  35. #include <net/switchdev.h>
  36. #include "nfpcore/nfp_cpp.h"
  37. #include "nfpcore/nfp_nsp.h"
  38. #include "nfp_app.h"
  39. #include "nfp_main.h"
  40. #include "nfp_net.h"
  41. #include "nfp_port.h"
  42. struct nfp_port *nfp_port_from_netdev(struct net_device *netdev)
  43. {
  44. if (nfp_netdev_is_nfp_net(netdev)) {
  45. struct nfp_net *nn = netdev_priv(netdev);
  46. return nn->port;
  47. }
  48. if (nfp_netdev_is_nfp_repr(netdev)) {
  49. struct nfp_repr *repr = netdev_priv(netdev);
  50. return repr->port;
  51. }
  52. WARN(1, "Unknown netdev type for nfp_port\n");
  53. return NULL;
  54. }
  55. static int
  56. nfp_port_attr_get(struct net_device *netdev, struct switchdev_attr *attr)
  57. {
  58. struct nfp_port *port;
  59. port = nfp_port_from_netdev(netdev);
  60. if (!port)
  61. return -EOPNOTSUPP;
  62. switch (attr->id) {
  63. case SWITCHDEV_ATTR_ID_PORT_PARENT_ID: {
  64. const u8 *serial;
  65. /* N.B: attr->u.ppid.id is binary data */
  66. attr->u.ppid.id_len = nfp_cpp_serial(port->app->cpp, &serial);
  67. memcpy(&attr->u.ppid.id, serial, attr->u.ppid.id_len);
  68. break;
  69. }
  70. default:
  71. return -EOPNOTSUPP;
  72. }
  73. return 0;
  74. }
  75. const struct switchdev_ops nfp_port_switchdev_ops = {
  76. .switchdev_port_attr_get = nfp_port_attr_get,
  77. };
  78. int nfp_port_setup_tc(struct net_device *netdev, enum tc_setup_type type,
  79. void *type_data)
  80. {
  81. struct nfp_port *port;
  82. port = nfp_port_from_netdev(netdev);
  83. if (!port)
  84. return -EOPNOTSUPP;
  85. return nfp_app_setup_tc(port->app, netdev, type, type_data);
  86. }
  87. int nfp_port_set_features(struct net_device *netdev, netdev_features_t features)
  88. {
  89. struct nfp_port *port;
  90. port = nfp_port_from_netdev(netdev);
  91. if (!port)
  92. return 0;
  93. if ((netdev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC) &&
  94. port->tc_offload_cnt) {
  95. netdev_err(netdev, "Cannot disable HW TC offload while offloads active\n");
  96. return -EBUSY;
  97. }
  98. return 0;
  99. }
  100. struct nfp_port *
  101. nfp_port_from_id(struct nfp_pf *pf, enum nfp_port_type type, unsigned int id)
  102. {
  103. struct nfp_port *port;
  104. lockdep_assert_held(&pf->lock);
  105. if (type != NFP_PORT_PHYS_PORT)
  106. return NULL;
  107. list_for_each_entry(port, &pf->ports, port_list)
  108. if (port->eth_id == id)
  109. return port;
  110. return NULL;
  111. }
  112. struct nfp_eth_table_port *__nfp_port_get_eth_port(struct nfp_port *port)
  113. {
  114. if (!port)
  115. return NULL;
  116. if (port->type != NFP_PORT_PHYS_PORT)
  117. return NULL;
  118. return port->eth_port;
  119. }
  120. struct nfp_eth_table_port *nfp_port_get_eth_port(struct nfp_port *port)
  121. {
  122. if (!__nfp_port_get_eth_port(port))
  123. return NULL;
  124. if (test_bit(NFP_PORT_CHANGED, &port->flags))
  125. if (nfp_net_refresh_eth_port(port))
  126. return NULL;
  127. return __nfp_port_get_eth_port(port);
  128. }
  129. int
  130. nfp_port_get_phys_port_name(struct net_device *netdev, char *name, size_t len)
  131. {
  132. struct nfp_eth_table_port *eth_port;
  133. struct nfp_port *port;
  134. int n;
  135. port = nfp_port_from_netdev(netdev);
  136. if (!port)
  137. return -EOPNOTSUPP;
  138. switch (port->type) {
  139. case NFP_PORT_PHYS_PORT:
  140. eth_port = __nfp_port_get_eth_port(port);
  141. if (!eth_port)
  142. return -EOPNOTSUPP;
  143. if (!eth_port->is_split)
  144. n = snprintf(name, len, "p%d", eth_port->label_port);
  145. else
  146. n = snprintf(name, len, "p%ds%d", eth_port->label_port,
  147. eth_port->label_subport);
  148. break;
  149. case NFP_PORT_PF_PORT:
  150. n = snprintf(name, len, "pf%d", port->pf_id);
  151. break;
  152. case NFP_PORT_VF_PORT:
  153. n = snprintf(name, len, "pf%dvf%d", port->pf_id, port->vf_id);
  154. break;
  155. default:
  156. return -EOPNOTSUPP;
  157. }
  158. if (n >= len)
  159. return -EINVAL;
  160. return 0;
  161. }
  162. /**
  163. * nfp_port_configure() - helper to set the interface configured bit
  164. * @netdev: net_device instance
  165. * @configed: Desired state
  166. *
  167. * Helper to set the ifup/ifdown state on the PHY only if there is a physical
  168. * interface associated with the netdev.
  169. *
  170. * Return:
  171. * 0 - configuration successful (or no change);
  172. * -ERRNO - configuration failed.
  173. */
  174. int nfp_port_configure(struct net_device *netdev, bool configed)
  175. {
  176. struct nfp_eth_table_port *eth_port;
  177. struct nfp_port *port;
  178. int err;
  179. port = nfp_port_from_netdev(netdev);
  180. eth_port = __nfp_port_get_eth_port(port);
  181. if (!eth_port)
  182. return 0;
  183. err = nfp_eth_set_configured(port->app->cpp, eth_port->index, configed);
  184. return err < 0 && err != -EOPNOTSUPP ? err : 0;
  185. }
  186. int nfp_port_init_phy_port(struct nfp_pf *pf, struct nfp_app *app,
  187. struct nfp_port *port, unsigned int id)
  188. {
  189. /* Check if vNIC has external port associated and cfg is OK */
  190. if (!pf->eth_tbl || id >= pf->eth_tbl->count) {
  191. nfp_err(app->cpp,
  192. "NSP port entries don't match vNICs (no entry %d)\n",
  193. id);
  194. return -EINVAL;
  195. }
  196. if (pf->eth_tbl->ports[id].override_changed) {
  197. nfp_warn(app->cpp,
  198. "Config changed for port #%d, reboot required before port will be operational\n",
  199. pf->eth_tbl->ports[id].index);
  200. port->type = NFP_PORT_INVALID;
  201. return 0;
  202. }
  203. port->eth_port = &pf->eth_tbl->ports[id];
  204. port->eth_id = pf->eth_tbl->ports[id].index;
  205. if (pf->mac_stats_mem)
  206. port->eth_stats =
  207. pf->mac_stats_mem + port->eth_id * NFP_MAC_STATS_SIZE;
  208. return 0;
  209. }
  210. struct nfp_port *
  211. nfp_port_alloc(struct nfp_app *app, enum nfp_port_type type,
  212. struct net_device *netdev)
  213. {
  214. struct nfp_port *port;
  215. port = kzalloc(sizeof(*port), GFP_KERNEL);
  216. if (!port)
  217. return ERR_PTR(-ENOMEM);
  218. port->netdev = netdev;
  219. port->type = type;
  220. port->app = app;
  221. list_add_tail(&port->port_list, &app->pf->ports);
  222. return port;
  223. }
  224. void nfp_port_free(struct nfp_port *port)
  225. {
  226. if (!port)
  227. return;
  228. list_del(&port->port_list);
  229. kfree(port);
  230. }