devl_internal.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /* Copyright (c) 2016 Mellanox Technologies. All rights reserved.
  3. * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
  4. */
  5. #include <linux/device.h>
  6. #include <linux/etherdevice.h>
  7. #include <linux/mutex.h>
  8. #include <linux/netdevice.h>
  9. #include <linux/notifier.h>
  10. #include <linux/types.h>
  11. #include <linux/workqueue.h>
  12. #include <linux/xarray.h>
  13. #include <net/devlink.h>
  14. #include <net/net_namespace.h>
  15. #include <net/rtnetlink.h>
  16. #include <rdma/ib_verbs.h>
  17. #include "netlink_gen.h"
  18. struct devlink_rel;
  19. #define DEVLINK_REGISTERED XA_MARK_1
  20. #define DEVLINK_RELOAD_STATS_ARRAY_SIZE \
  21. (__DEVLINK_RELOAD_LIMIT_MAX * __DEVLINK_RELOAD_ACTION_MAX)
  22. struct devlink_dev_stats {
  23. u32 reload_stats[DEVLINK_RELOAD_STATS_ARRAY_SIZE];
  24. u32 remote_reload_stats[DEVLINK_RELOAD_STATS_ARRAY_SIZE];
  25. };
  26. struct devlink {
  27. u32 index;
  28. struct xarray ports;
  29. struct list_head rate_list;
  30. struct list_head sb_list;
  31. struct list_head dpipe_table_list;
  32. struct list_head resource_list;
  33. struct xarray params;
  34. struct list_head region_list;
  35. struct list_head reporter_list;
  36. struct devlink_dpipe_headers *dpipe_headers;
  37. struct list_head trap_list;
  38. struct list_head trap_group_list;
  39. struct list_head trap_policer_list;
  40. struct list_head linecard_list;
  41. const struct devlink_ops *ops;
  42. struct xarray snapshot_ids;
  43. struct devlink_dev_stats stats;
  44. struct device *dev;
  45. possible_net_t _net;
  46. /* Serializes access to devlink instance specific objects such as
  47. * port, sb, dpipe, resource, params, region, traps and more.
  48. */
  49. struct mutex lock;
  50. struct lock_class_key lock_key;
  51. u8 reload_failed:1;
  52. refcount_t refcount;
  53. struct rcu_work rwork;
  54. struct devlink_rel *rel;
  55. struct xarray nested_rels;
  56. char priv[] __aligned(NETDEV_ALIGN);
  57. };
  58. extern struct xarray devlinks;
  59. extern struct genl_family devlink_nl_family;
  60. /* devlink instances are open to the access from the user space after
  61. * devlink_register() call. Such logical barrier allows us to have certain
  62. * expectations related to locking.
  63. *
  64. * Before *_register() - we are in initialization stage and no parallel
  65. * access possible to the devlink instance. All drivers perform that phase
  66. * by implicitly holding device_lock.
  67. *
  68. * After *_register() - users and driver can access devlink instance at
  69. * the same time.
  70. */
  71. #define ASSERT_DEVLINK_REGISTERED(d) \
  72. WARN_ON_ONCE(!xa_get_mark(&devlinks, (d)->index, DEVLINK_REGISTERED))
  73. #define ASSERT_DEVLINK_NOT_REGISTERED(d) \
  74. WARN_ON_ONCE(xa_get_mark(&devlinks, (d)->index, DEVLINK_REGISTERED))
  75. /* Iterate over devlink pointers which were possible to get reference to.
  76. * devlink_put() needs to be called for each iterated devlink pointer
  77. * in loop body in order to release the reference.
  78. */
  79. #define devlinks_xa_for_each_registered_get(net, index, devlink) \
  80. for (index = 0; (devlink = devlinks_xa_find_get(net, &index)); index++)
  81. struct devlink *devlinks_xa_find_get(struct net *net, unsigned long *indexp);
  82. static inline bool __devl_is_registered(struct devlink *devlink)
  83. {
  84. return xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED);
  85. }
  86. static inline bool devl_is_registered(struct devlink *devlink)
  87. {
  88. devl_assert_locked(devlink);
  89. return __devl_is_registered(devlink);
  90. }
  91. static inline void devl_dev_lock(struct devlink *devlink, bool dev_lock)
  92. {
  93. if (dev_lock)
  94. device_lock(devlink->dev);
  95. devl_lock(devlink);
  96. }
  97. static inline void devl_dev_unlock(struct devlink *devlink, bool dev_lock)
  98. {
  99. devl_unlock(devlink);
  100. if (dev_lock)
  101. device_unlock(devlink->dev);
  102. }
  103. typedef void devlink_rel_notify_cb_t(struct devlink *devlink, u32 obj_index);
  104. typedef void devlink_rel_cleanup_cb_t(struct devlink *devlink, u32 obj_index,
  105. u32 rel_index);
  106. void devlink_rel_nested_in_clear(u32 rel_index);
  107. int devlink_rel_nested_in_add(u32 *rel_index, u32 devlink_index,
  108. u32 obj_index, devlink_rel_notify_cb_t *notify_cb,
  109. devlink_rel_cleanup_cb_t *cleanup_cb,
  110. struct devlink *devlink);
  111. void devlink_rel_nested_in_notify(struct devlink *devlink);
  112. int devlink_rel_devlink_handle_put(struct sk_buff *msg, struct devlink *devlink,
  113. u32 rel_index, int attrtype,
  114. bool *msg_updated);
  115. /* Netlink */
  116. enum devlink_multicast_groups {
  117. DEVLINK_MCGRP_CONFIG,
  118. };
  119. /* state held across netlink dumps */
  120. struct devlink_nl_dump_state {
  121. unsigned long instance;
  122. int idx;
  123. union {
  124. /* DEVLINK_CMD_REGION_READ */
  125. struct {
  126. u64 start_offset;
  127. };
  128. /* DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET */
  129. struct {
  130. u64 dump_ts;
  131. };
  132. };
  133. };
  134. typedef int devlink_nl_dump_one_func_t(struct sk_buff *msg,
  135. struct devlink *devlink,
  136. struct netlink_callback *cb,
  137. int flags);
  138. struct devlink *
  139. devlink_get_from_attrs_lock(struct net *net, struct nlattr **attrs,
  140. bool dev_lock);
  141. int devlink_nl_dumpit(struct sk_buff *msg, struct netlink_callback *cb,
  142. devlink_nl_dump_one_func_t *dump_one);
  143. static inline struct devlink_nl_dump_state *
  144. devlink_dump_state(struct netlink_callback *cb)
  145. {
  146. NL_ASSERT_DUMP_CTX_FITS(struct devlink_nl_dump_state);
  147. return (struct devlink_nl_dump_state *)cb->ctx;
  148. }
  149. static inline int
  150. devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
  151. {
  152. if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
  153. return -EMSGSIZE;
  154. if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
  155. return -EMSGSIZE;
  156. return 0;
  157. }
  158. int devlink_nl_put_nested_handle(struct sk_buff *msg, struct net *net,
  159. struct devlink *devlink, int attrtype);
  160. int devlink_nl_msg_reply_and_new(struct sk_buff **msg, struct genl_info *info);
  161. static inline bool devlink_nl_notify_need(struct devlink *devlink)
  162. {
  163. return genl_has_listeners(&devlink_nl_family, devlink_net(devlink),
  164. DEVLINK_MCGRP_CONFIG);
  165. }
  166. struct devlink_obj_desc {
  167. struct rcu_head rcu;
  168. const char *bus_name;
  169. const char *dev_name;
  170. unsigned int port_index;
  171. bool port_index_valid;
  172. long data[];
  173. };
  174. static inline void devlink_nl_obj_desc_init(struct devlink_obj_desc *desc,
  175. struct devlink *devlink)
  176. {
  177. memset(desc, 0, sizeof(*desc));
  178. desc->bus_name = devlink->dev->bus->name;
  179. desc->dev_name = dev_name(devlink->dev);
  180. }
  181. static inline void devlink_nl_obj_desc_port_set(struct devlink_obj_desc *desc,
  182. struct devlink_port *devlink_port)
  183. {
  184. desc->port_index = devlink_port->index;
  185. desc->port_index_valid = true;
  186. }
  187. int devlink_nl_notify_filter(struct sock *dsk, struct sk_buff *skb, void *data);
  188. static inline void devlink_nl_notify_send_desc(struct devlink *devlink,
  189. struct sk_buff *msg,
  190. struct devlink_obj_desc *desc)
  191. {
  192. genlmsg_multicast_netns_filtered(&devlink_nl_family,
  193. devlink_net(devlink),
  194. msg, 0, DEVLINK_MCGRP_CONFIG,
  195. GFP_KERNEL,
  196. devlink_nl_notify_filter, desc);
  197. }
  198. static inline void devlink_nl_notify_send(struct devlink *devlink,
  199. struct sk_buff *msg)
  200. {
  201. struct devlink_obj_desc desc;
  202. devlink_nl_obj_desc_init(&desc, devlink);
  203. devlink_nl_notify_send_desc(devlink, msg, &desc);
  204. }
  205. /* Notify */
  206. void devlink_notify_register(struct devlink *devlink);
  207. void devlink_notify_unregister(struct devlink *devlink);
  208. void devlink_ports_notify_register(struct devlink *devlink);
  209. void devlink_ports_notify_unregister(struct devlink *devlink);
  210. void devlink_params_notify_register(struct devlink *devlink);
  211. void devlink_params_notify_unregister(struct devlink *devlink);
  212. void devlink_regions_notify_register(struct devlink *devlink);
  213. void devlink_regions_notify_unregister(struct devlink *devlink);
  214. void devlink_trap_policers_notify_register(struct devlink *devlink);
  215. void devlink_trap_policers_notify_unregister(struct devlink *devlink);
  216. void devlink_trap_groups_notify_register(struct devlink *devlink);
  217. void devlink_trap_groups_notify_unregister(struct devlink *devlink);
  218. void devlink_traps_notify_register(struct devlink *devlink);
  219. void devlink_traps_notify_unregister(struct devlink *devlink);
  220. void devlink_rates_notify_register(struct devlink *devlink);
  221. void devlink_rates_notify_unregister(struct devlink *devlink);
  222. void devlink_linecards_notify_register(struct devlink *devlink);
  223. void devlink_linecards_notify_unregister(struct devlink *devlink);
  224. /* Ports */
  225. #define ASSERT_DEVLINK_PORT_INITIALIZED(devlink_port) \
  226. WARN_ON_ONCE(!(devlink_port)->initialized)
  227. struct devlink_port *devlink_port_get_by_index(struct devlink *devlink,
  228. unsigned int port_index);
  229. int devlink_port_netdevice_event(struct notifier_block *nb,
  230. unsigned long event, void *ptr);
  231. struct devlink_port *
  232. devlink_port_get_from_info(struct devlink *devlink, struct genl_info *info);
  233. struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
  234. struct nlattr **attrs);
  235. /* Reload */
  236. bool devlink_reload_actions_valid(const struct devlink_ops *ops);
  237. int devlink_reload(struct devlink *devlink, struct net *dest_net,
  238. enum devlink_reload_action action,
  239. enum devlink_reload_limit limit,
  240. u32 *actions_performed, struct netlink_ext_ack *extack);
  241. static inline bool devlink_reload_supported(const struct devlink_ops *ops)
  242. {
  243. return ops->reload_down && ops->reload_up;
  244. }
  245. /* Params */
  246. void devlink_params_driverinit_load_new(struct devlink *devlink);
  247. /* Resources */
  248. struct devlink_resource;
  249. int devlink_resources_validate(struct devlink *devlink,
  250. struct devlink_resource *resource,
  251. struct genl_info *info);
  252. /* Rates */
  253. int devlink_rate_nodes_check(struct devlink *devlink, u16 mode,
  254. struct netlink_ext_ack *extack);
  255. /* Linecards */
  256. unsigned int devlink_linecard_index(struct devlink_linecard *linecard);