nfp_app.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright (C) 2017-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/bug.h>
  34. #include <linux/lockdep.h>
  35. #include <linux/rcupdate.h>
  36. #include <linux/skbuff.h>
  37. #include <linux/slab.h>
  38. #include "nfpcore/nfp_cpp.h"
  39. #include "nfpcore/nfp_nffw.h"
  40. #include "nfp_app.h"
  41. #include "nfp_main.h"
  42. #include "nfp_net.h"
  43. #include "nfp_net_repr.h"
  44. #include "nfp_port.h"
  45. static const struct nfp_app_type *apps[] = {
  46. [NFP_APP_CORE_NIC] = &app_nic,
  47. #ifdef CONFIG_BPF_SYSCALL
  48. [NFP_APP_BPF_NIC] = &app_bpf,
  49. #else
  50. [NFP_APP_BPF_NIC] = &app_nic,
  51. #endif
  52. #ifdef CONFIG_NFP_APP_FLOWER
  53. [NFP_APP_FLOWER_NIC] = &app_flower,
  54. #endif
  55. #ifdef CONFIG_NFP_APP_ABM_NIC
  56. [NFP_APP_ACTIVE_BUFFER_MGMT_NIC] = &app_abm,
  57. #endif
  58. };
  59. struct nfp_app *nfp_app_from_netdev(struct net_device *netdev)
  60. {
  61. if (nfp_netdev_is_nfp_net(netdev)) {
  62. struct nfp_net *nn = netdev_priv(netdev);
  63. return nn->app;
  64. }
  65. if (nfp_netdev_is_nfp_repr(netdev)) {
  66. struct nfp_repr *repr = netdev_priv(netdev);
  67. return repr->app;
  68. }
  69. WARN(1, "Unknown netdev type for nfp_app\n");
  70. return NULL;
  71. }
  72. const char *nfp_app_mip_name(struct nfp_app *app)
  73. {
  74. if (!app || !app->pf->mip)
  75. return "";
  76. return nfp_mip_name(app->pf->mip);
  77. }
  78. int nfp_app_ndo_init(struct net_device *netdev)
  79. {
  80. struct nfp_app *app = nfp_app_from_netdev(netdev);
  81. if (!app || !app->type->ndo_init)
  82. return 0;
  83. return app->type->ndo_init(app, netdev);
  84. }
  85. void nfp_app_ndo_uninit(struct net_device *netdev)
  86. {
  87. struct nfp_app *app = nfp_app_from_netdev(netdev);
  88. if (app && app->type->ndo_uninit)
  89. app->type->ndo_uninit(app, netdev);
  90. }
  91. u64 *nfp_app_port_get_stats(struct nfp_port *port, u64 *data)
  92. {
  93. if (!port || !port->app || !port->app->type->port_get_stats)
  94. return data;
  95. return port->app->type->port_get_stats(port->app, port, data);
  96. }
  97. int nfp_app_port_get_stats_count(struct nfp_port *port)
  98. {
  99. if (!port || !port->app || !port->app->type->port_get_stats_count)
  100. return 0;
  101. return port->app->type->port_get_stats_count(port->app, port);
  102. }
  103. u8 *nfp_app_port_get_stats_strings(struct nfp_port *port, u8 *data)
  104. {
  105. if (!port || !port->app || !port->app->type->port_get_stats_strings)
  106. return data;
  107. return port->app->type->port_get_stats_strings(port->app, port, data);
  108. }
  109. struct sk_buff *
  110. nfp_app_ctrl_msg_alloc(struct nfp_app *app, unsigned int size, gfp_t priority)
  111. {
  112. struct sk_buff *skb;
  113. if (nfp_app_ctrl_has_meta(app))
  114. size += 8;
  115. skb = alloc_skb(size, priority);
  116. if (!skb)
  117. return NULL;
  118. if (nfp_app_ctrl_has_meta(app))
  119. skb_reserve(skb, 8);
  120. return skb;
  121. }
  122. struct nfp_reprs *
  123. nfp_reprs_get_locked(struct nfp_app *app, enum nfp_repr_type type)
  124. {
  125. return rcu_dereference_protected(app->reprs[type],
  126. lockdep_is_held(&app->pf->lock));
  127. }
  128. struct nfp_reprs *
  129. nfp_app_reprs_set(struct nfp_app *app, enum nfp_repr_type type,
  130. struct nfp_reprs *reprs)
  131. {
  132. struct nfp_reprs *old;
  133. old = nfp_reprs_get_locked(app, type);
  134. rcu_assign_pointer(app->reprs[type], reprs);
  135. return old;
  136. }
  137. struct nfp_app *nfp_app_alloc(struct nfp_pf *pf, enum nfp_app_id id)
  138. {
  139. struct nfp_app *app;
  140. if (id >= ARRAY_SIZE(apps) || !apps[id]) {
  141. nfp_err(pf->cpp, "unknown FW app ID 0x%02hhx, driver too old or support for FW not built in\n", id);
  142. return ERR_PTR(-EINVAL);
  143. }
  144. if (WARN_ON(!apps[id]->name || !apps[id]->vnic_alloc))
  145. return ERR_PTR(-EINVAL);
  146. if (WARN_ON(!apps[id]->ctrl_msg_rx && apps[id]->ctrl_msg_rx_raw))
  147. return ERR_PTR(-EINVAL);
  148. app = kzalloc(sizeof(*app), GFP_KERNEL);
  149. if (!app)
  150. return ERR_PTR(-ENOMEM);
  151. app->pf = pf;
  152. app->cpp = pf->cpp;
  153. app->pdev = pf->pdev;
  154. app->type = apps[id];
  155. return app;
  156. }
  157. void nfp_app_free(struct nfp_app *app)
  158. {
  159. kfree(app);
  160. }