nfp_app.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. #ifndef _NFP_APP_H
  34. #define _NFP_APP_H 1
  35. #include <net/devlink.h>
  36. #include <trace/events/devlink.h>
  37. #include "nfp_net_repr.h"
  38. struct bpf_prog;
  39. struct net_device;
  40. struct netdev_bpf;
  41. struct netlink_ext_ack;
  42. struct pci_dev;
  43. struct sk_buff;
  44. struct sk_buff;
  45. struct nfp_app;
  46. struct nfp_cpp;
  47. struct nfp_pf;
  48. struct nfp_repr;
  49. struct nfp_net;
  50. enum nfp_app_id {
  51. NFP_APP_CORE_NIC = 0x1,
  52. NFP_APP_BPF_NIC = 0x2,
  53. NFP_APP_FLOWER_NIC = 0x3,
  54. NFP_APP_ACTIVE_BUFFER_MGMT_NIC = 0x4,
  55. };
  56. extern const struct nfp_app_type app_nic;
  57. extern const struct nfp_app_type app_bpf;
  58. extern const struct nfp_app_type app_flower;
  59. extern const struct nfp_app_type app_abm;
  60. /**
  61. * struct nfp_app_type - application definition
  62. * @id: application ID
  63. * @name: application name
  64. * @ctrl_cap_mask: ctrl vNIC capability mask, allows disabling features like
  65. * IRQMOD which are on by default but counter-productive for
  66. * control messages which are often latency-sensitive
  67. * @ctrl_has_meta: control messages have prepend of type:5/port:CTRL
  68. *
  69. * Callbacks
  70. * @init: perform basic app checks and init
  71. * @clean: clean app state
  72. * @extra_cap: extra capabilities string
  73. * @ndo_init: vNIC and repr netdev .ndo_init
  74. * @ndo_uninit: vNIC and repr netdev .ndo_unint
  75. * @vnic_alloc: allocate vNICs (assign port types, etc.)
  76. * @vnic_free: free up app's vNIC state
  77. * @vnic_init: vNIC netdev was registered
  78. * @vnic_clean: vNIC netdev about to be unregistered
  79. * @repr_init: representor about to be registered
  80. * @repr_preclean: representor about to unregistered, executed before app
  81. * reference to the it is removed
  82. * @repr_clean: representor about to be unregistered
  83. * @repr_open: representor netdev open callback
  84. * @repr_stop: representor netdev stop callback
  85. * @check_mtu: MTU change request on a netdev (verify it is valid)
  86. * @repr_change_mtu: MTU change request on repr (make and verify change)
  87. * @port_get_stats: get extra ethtool statistics for a port
  88. * @port_get_stats_count: get count of extra statistics for a port
  89. * @port_get_stats_strings: get strings for extra statistics
  90. * @start: start application logic
  91. * @stop: stop application logic
  92. * @ctrl_msg_rx: control message handler
  93. * @ctrl_msg_rx_raw: handler for control messages from data queues
  94. * @setup_tc: setup TC ndo
  95. * @bpf: BPF ndo offload-related calls
  96. * @xdp_offload: offload an XDP program
  97. * @eswitch_mode_get: get SR-IOV eswitch mode
  98. * @eswitch_mode_set: set SR-IOV eswitch mode (under pf->lock)
  99. * @sriov_enable: app-specific sriov initialisation
  100. * @sriov_disable: app-specific sriov clean-up
  101. * @repr_get: get representor netdev
  102. */
  103. struct nfp_app_type {
  104. enum nfp_app_id id;
  105. const char *name;
  106. u32 ctrl_cap_mask;
  107. bool ctrl_has_meta;
  108. int (*init)(struct nfp_app *app);
  109. void (*clean)(struct nfp_app *app);
  110. const char *(*extra_cap)(struct nfp_app *app, struct nfp_net *nn);
  111. int (*ndo_init)(struct nfp_app *app, struct net_device *netdev);
  112. void (*ndo_uninit)(struct nfp_app *app, struct net_device *netdev);
  113. int (*vnic_alloc)(struct nfp_app *app, struct nfp_net *nn,
  114. unsigned int id);
  115. void (*vnic_free)(struct nfp_app *app, struct nfp_net *nn);
  116. int (*vnic_init)(struct nfp_app *app, struct nfp_net *nn);
  117. void (*vnic_clean)(struct nfp_app *app, struct nfp_net *nn);
  118. int (*repr_init)(struct nfp_app *app, struct net_device *netdev);
  119. void (*repr_preclean)(struct nfp_app *app, struct net_device *netdev);
  120. void (*repr_clean)(struct nfp_app *app, struct net_device *netdev);
  121. int (*repr_open)(struct nfp_app *app, struct nfp_repr *repr);
  122. int (*repr_stop)(struct nfp_app *app, struct nfp_repr *repr);
  123. int (*check_mtu)(struct nfp_app *app, struct net_device *netdev,
  124. int new_mtu);
  125. int (*repr_change_mtu)(struct nfp_app *app, struct net_device *netdev,
  126. int new_mtu);
  127. u64 *(*port_get_stats)(struct nfp_app *app,
  128. struct nfp_port *port, u64 *data);
  129. int (*port_get_stats_count)(struct nfp_app *app, struct nfp_port *port);
  130. u8 *(*port_get_stats_strings)(struct nfp_app *app,
  131. struct nfp_port *port, u8 *data);
  132. int (*start)(struct nfp_app *app);
  133. void (*stop)(struct nfp_app *app);
  134. void (*ctrl_msg_rx)(struct nfp_app *app, struct sk_buff *skb);
  135. void (*ctrl_msg_rx_raw)(struct nfp_app *app, const void *data,
  136. unsigned int len);
  137. int (*setup_tc)(struct nfp_app *app, struct net_device *netdev,
  138. enum tc_setup_type type, void *type_data);
  139. int (*bpf)(struct nfp_app *app, struct nfp_net *nn,
  140. struct netdev_bpf *xdp);
  141. int (*xdp_offload)(struct nfp_app *app, struct nfp_net *nn,
  142. struct bpf_prog *prog,
  143. struct netlink_ext_ack *extack);
  144. int (*sriov_enable)(struct nfp_app *app, int num_vfs);
  145. void (*sriov_disable)(struct nfp_app *app);
  146. enum devlink_eswitch_mode (*eswitch_mode_get)(struct nfp_app *app);
  147. int (*eswitch_mode_set)(struct nfp_app *app, u16 mode);
  148. struct net_device *(*repr_get)(struct nfp_app *app, u32 id);
  149. };
  150. /**
  151. * struct nfp_app - NFP application container
  152. * @pdev: backpointer to PCI device
  153. * @pf: backpointer to NFP PF structure
  154. * @cpp: pointer to the CPP handle
  155. * @ctrl: pointer to ctrl vNIC struct
  156. * @reprs: array of pointers to representors
  157. * @type: pointer to const application ops and info
  158. * @priv: app-specific priv data
  159. */
  160. struct nfp_app {
  161. struct pci_dev *pdev;
  162. struct nfp_pf *pf;
  163. struct nfp_cpp *cpp;
  164. struct nfp_net *ctrl;
  165. struct nfp_reprs __rcu *reprs[NFP_REPR_TYPE_MAX + 1];
  166. const struct nfp_app_type *type;
  167. void *priv;
  168. };
  169. bool __nfp_ctrl_tx(struct nfp_net *nn, struct sk_buff *skb);
  170. bool nfp_ctrl_tx(struct nfp_net *nn, struct sk_buff *skb);
  171. static inline int nfp_app_init(struct nfp_app *app)
  172. {
  173. if (!app->type->init)
  174. return 0;
  175. return app->type->init(app);
  176. }
  177. static inline void nfp_app_clean(struct nfp_app *app)
  178. {
  179. if (app->type->clean)
  180. app->type->clean(app);
  181. }
  182. int nfp_app_ndo_init(struct net_device *netdev);
  183. void nfp_app_ndo_uninit(struct net_device *netdev);
  184. static inline int nfp_app_vnic_alloc(struct nfp_app *app, struct nfp_net *nn,
  185. unsigned int id)
  186. {
  187. return app->type->vnic_alloc(app, nn, id);
  188. }
  189. static inline void nfp_app_vnic_free(struct nfp_app *app, struct nfp_net *nn)
  190. {
  191. if (app->type->vnic_free)
  192. app->type->vnic_free(app, nn);
  193. }
  194. static inline int nfp_app_vnic_init(struct nfp_app *app, struct nfp_net *nn)
  195. {
  196. if (!app->type->vnic_init)
  197. return 0;
  198. return app->type->vnic_init(app, nn);
  199. }
  200. static inline void nfp_app_vnic_clean(struct nfp_app *app, struct nfp_net *nn)
  201. {
  202. if (app->type->vnic_clean)
  203. app->type->vnic_clean(app, nn);
  204. }
  205. static inline int nfp_app_repr_open(struct nfp_app *app, struct nfp_repr *repr)
  206. {
  207. if (!app->type->repr_open)
  208. return -EINVAL;
  209. return app->type->repr_open(app, repr);
  210. }
  211. static inline int nfp_app_repr_stop(struct nfp_app *app, struct nfp_repr *repr)
  212. {
  213. if (!app->type->repr_stop)
  214. return -EINVAL;
  215. return app->type->repr_stop(app, repr);
  216. }
  217. static inline int
  218. nfp_app_repr_init(struct nfp_app *app, struct net_device *netdev)
  219. {
  220. if (!app->type->repr_init)
  221. return 0;
  222. return app->type->repr_init(app, netdev);
  223. }
  224. static inline void
  225. nfp_app_repr_preclean(struct nfp_app *app, struct net_device *netdev)
  226. {
  227. if (app->type->repr_preclean)
  228. app->type->repr_preclean(app, netdev);
  229. }
  230. static inline void
  231. nfp_app_repr_clean(struct nfp_app *app, struct net_device *netdev)
  232. {
  233. if (app->type->repr_clean)
  234. app->type->repr_clean(app, netdev);
  235. }
  236. static inline int
  237. nfp_app_check_mtu(struct nfp_app *app, struct net_device *netdev, int new_mtu)
  238. {
  239. if (!app || !app->type->check_mtu)
  240. return 0;
  241. return app->type->check_mtu(app, netdev, new_mtu);
  242. }
  243. static inline int
  244. nfp_app_repr_change_mtu(struct nfp_app *app, struct net_device *netdev,
  245. int new_mtu)
  246. {
  247. if (!app || !app->type->repr_change_mtu)
  248. return 0;
  249. return app->type->repr_change_mtu(app, netdev, new_mtu);
  250. }
  251. static inline int nfp_app_start(struct nfp_app *app, struct nfp_net *ctrl)
  252. {
  253. app->ctrl = ctrl;
  254. if (!app->type->start)
  255. return 0;
  256. return app->type->start(app);
  257. }
  258. static inline void nfp_app_stop(struct nfp_app *app)
  259. {
  260. if (!app->type->stop)
  261. return;
  262. app->type->stop(app);
  263. }
  264. static inline const char *nfp_app_name(struct nfp_app *app)
  265. {
  266. if (!app)
  267. return "";
  268. return app->type->name;
  269. }
  270. static inline bool nfp_app_needs_ctrl_vnic(struct nfp_app *app)
  271. {
  272. return app && app->type->ctrl_msg_rx;
  273. }
  274. static inline bool nfp_app_ctrl_has_meta(struct nfp_app *app)
  275. {
  276. return app->type->ctrl_has_meta;
  277. }
  278. static inline bool nfp_app_ctrl_uses_data_vnics(struct nfp_app *app)
  279. {
  280. return app && app->type->ctrl_msg_rx_raw;
  281. }
  282. static inline const char *nfp_app_extra_cap(struct nfp_app *app,
  283. struct nfp_net *nn)
  284. {
  285. if (!app || !app->type->extra_cap)
  286. return "";
  287. return app->type->extra_cap(app, nn);
  288. }
  289. static inline bool nfp_app_has_tc(struct nfp_app *app)
  290. {
  291. return app && app->type->setup_tc;
  292. }
  293. static inline int nfp_app_setup_tc(struct nfp_app *app,
  294. struct net_device *netdev,
  295. enum tc_setup_type type, void *type_data)
  296. {
  297. if (!app || !app->type->setup_tc)
  298. return -EOPNOTSUPP;
  299. return app->type->setup_tc(app, netdev, type, type_data);
  300. }
  301. static inline int nfp_app_bpf(struct nfp_app *app, struct nfp_net *nn,
  302. struct netdev_bpf *bpf)
  303. {
  304. if (!app || !app->type->bpf)
  305. return -EINVAL;
  306. return app->type->bpf(app, nn, bpf);
  307. }
  308. static inline int nfp_app_xdp_offload(struct nfp_app *app, struct nfp_net *nn,
  309. struct bpf_prog *prog,
  310. struct netlink_ext_ack *extack)
  311. {
  312. if (!app || !app->type->xdp_offload)
  313. return -EOPNOTSUPP;
  314. return app->type->xdp_offload(app, nn, prog, extack);
  315. }
  316. static inline bool __nfp_app_ctrl_tx(struct nfp_app *app, struct sk_buff *skb)
  317. {
  318. trace_devlink_hwmsg(priv_to_devlink(app->pf), false, 0,
  319. skb->data, skb->len);
  320. return __nfp_ctrl_tx(app->ctrl, skb);
  321. }
  322. static inline bool nfp_app_ctrl_tx(struct nfp_app *app, struct sk_buff *skb)
  323. {
  324. trace_devlink_hwmsg(priv_to_devlink(app->pf), false, 0,
  325. skb->data, skb->len);
  326. return nfp_ctrl_tx(app->ctrl, skb);
  327. }
  328. static inline void nfp_app_ctrl_rx(struct nfp_app *app, struct sk_buff *skb)
  329. {
  330. trace_devlink_hwmsg(priv_to_devlink(app->pf), true, 0,
  331. skb->data, skb->len);
  332. app->type->ctrl_msg_rx(app, skb);
  333. }
  334. static inline void
  335. nfp_app_ctrl_rx_raw(struct nfp_app *app, const void *data, unsigned int len)
  336. {
  337. if (!app || !app->type->ctrl_msg_rx_raw)
  338. return;
  339. trace_devlink_hwmsg(priv_to_devlink(app->pf), true, 0, data, len);
  340. app->type->ctrl_msg_rx_raw(app, data, len);
  341. }
  342. static inline int nfp_app_eswitch_mode_get(struct nfp_app *app, u16 *mode)
  343. {
  344. if (!app->type->eswitch_mode_get)
  345. return -EOPNOTSUPP;
  346. *mode = app->type->eswitch_mode_get(app);
  347. return 0;
  348. }
  349. static inline int nfp_app_eswitch_mode_set(struct nfp_app *app, u16 mode)
  350. {
  351. if (!app->type->eswitch_mode_set)
  352. return -EOPNOTSUPP;
  353. return app->type->eswitch_mode_set(app, mode);
  354. }
  355. static inline int nfp_app_sriov_enable(struct nfp_app *app, int num_vfs)
  356. {
  357. if (!app || !app->type->sriov_enable)
  358. return -EOPNOTSUPP;
  359. return app->type->sriov_enable(app, num_vfs);
  360. }
  361. static inline void nfp_app_sriov_disable(struct nfp_app *app)
  362. {
  363. if (app && app->type->sriov_disable)
  364. app->type->sriov_disable(app);
  365. }
  366. static inline struct net_device *nfp_app_repr_get(struct nfp_app *app, u32 id)
  367. {
  368. if (unlikely(!app || !app->type->repr_get))
  369. return NULL;
  370. return app->type->repr_get(app, id);
  371. }
  372. struct nfp_app *nfp_app_from_netdev(struct net_device *netdev);
  373. u64 *nfp_app_port_get_stats(struct nfp_port *port, u64 *data);
  374. int nfp_app_port_get_stats_count(struct nfp_port *port);
  375. u8 *nfp_app_port_get_stats_strings(struct nfp_port *port, u8 *data);
  376. struct nfp_reprs *
  377. nfp_reprs_get_locked(struct nfp_app *app, enum nfp_repr_type type);
  378. struct nfp_reprs *
  379. nfp_app_reprs_set(struct nfp_app *app, enum nfp_repr_type type,
  380. struct nfp_reprs *reprs);
  381. const char *nfp_app_mip_name(struct nfp_app *app);
  382. struct sk_buff *
  383. nfp_app_ctrl_msg_alloc(struct nfp_app *app, unsigned int size, gfp_t priority);
  384. struct nfp_app *nfp_app_alloc(struct nfp_pf *pf, enum nfp_app_id id);
  385. void nfp_app_free(struct nfp_app *app);
  386. /* Callbacks shared between apps */
  387. int nfp_app_nic_vnic_alloc(struct nfp_app *app, struct nfp_net *nn,
  388. unsigned int id);
  389. int nfp_app_nic_vnic_init_phy_port(struct nfp_pf *pf, struct nfp_app *app,
  390. struct nfp_net *nn, unsigned int id);
  391. #endif