devlink.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * include/net/devlink.h - Network physical device Netlink interface
  3. * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
  4. * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #ifndef _NET_DEVLINK_H_
  12. #define _NET_DEVLINK_H_
  13. #include <linux/device.h>
  14. #include <linux/slab.h>
  15. #include <linux/gfp.h>
  16. #include <linux/list.h>
  17. #include <linux/netdevice.h>
  18. #include <net/net_namespace.h>
  19. #include <uapi/linux/devlink.h>
  20. struct devlink_ops;
  21. struct devlink {
  22. struct list_head list;
  23. struct list_head port_list;
  24. const struct devlink_ops *ops;
  25. struct device *dev;
  26. possible_net_t _net;
  27. char priv[0] __aligned(NETDEV_ALIGN);
  28. };
  29. struct devlink_port {
  30. struct list_head list;
  31. struct devlink *devlink;
  32. unsigned index;
  33. bool registered;
  34. enum devlink_port_type type;
  35. enum devlink_port_type desired_type;
  36. void *type_dev;
  37. bool split;
  38. u32 split_group;
  39. };
  40. struct devlink_ops {
  41. size_t priv_size;
  42. int (*port_type_set)(struct devlink_port *devlink_port,
  43. enum devlink_port_type port_type);
  44. int (*port_split)(struct devlink *devlink, unsigned int port_index,
  45. unsigned int count);
  46. int (*port_unsplit)(struct devlink *devlink, unsigned int port_index);
  47. };
  48. static inline void *devlink_priv(struct devlink *devlink)
  49. {
  50. BUG_ON(!devlink);
  51. return &devlink->priv;
  52. }
  53. static inline struct devlink *priv_to_devlink(void *priv)
  54. {
  55. BUG_ON(!priv);
  56. return container_of(priv, struct devlink, priv);
  57. }
  58. struct ib_device;
  59. #if IS_ENABLED(CONFIG_NET_DEVLINK)
  60. struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size);
  61. int devlink_register(struct devlink *devlink, struct device *dev);
  62. void devlink_unregister(struct devlink *devlink);
  63. void devlink_free(struct devlink *devlink);
  64. int devlink_port_register(struct devlink *devlink,
  65. struct devlink_port *devlink_port,
  66. unsigned int port_index);
  67. void devlink_port_unregister(struct devlink_port *devlink_port);
  68. void devlink_port_type_eth_set(struct devlink_port *devlink_port,
  69. struct net_device *netdev);
  70. void devlink_port_type_ib_set(struct devlink_port *devlink_port,
  71. struct ib_device *ibdev);
  72. void devlink_port_type_clear(struct devlink_port *devlink_port);
  73. void devlink_port_split_set(struct devlink_port *devlink_port,
  74. u32 split_group);
  75. #else
  76. static inline struct devlink *devlink_alloc(const struct devlink_ops *ops,
  77. size_t priv_size)
  78. {
  79. return kzalloc(sizeof(struct devlink) + priv_size, GFP_KERNEL);
  80. }
  81. static inline int devlink_register(struct devlink *devlink, struct device *dev)
  82. {
  83. return 0;
  84. }
  85. static inline void devlink_unregister(struct devlink *devlink)
  86. {
  87. }
  88. static inline void devlink_free(struct devlink *devlink)
  89. {
  90. kfree(devlink);
  91. }
  92. static inline int devlink_port_register(struct devlink *devlink,
  93. struct devlink_port *devlink_port,
  94. unsigned int port_index)
  95. {
  96. return 0;
  97. }
  98. static inline void devlink_port_unregister(struct devlink_port *devlink_port)
  99. {
  100. }
  101. static inline void devlink_port_type_eth_set(struct devlink_port *devlink_port,
  102. struct net_device *netdev)
  103. {
  104. }
  105. static inline void devlink_port_type_ib_set(struct devlink_port *devlink_port,
  106. struct ib_device *ibdev)
  107. {
  108. }
  109. static inline void devlink_port_type_clear(struct devlink_port *devlink_port)
  110. {
  111. }
  112. static inline void devlink_port_split_set(struct devlink_port *devlink_port,
  113. u32 split_group)
  114. {
  115. }
  116. #endif
  117. #endif /* _NET_DEVLINK_H_ */