1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
4 * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
7 #include <net/genetlink.h>
8 #define CREATE_TRACE_POINTS
9 #include <trace/events/devlink.h>
11 #include "devl_internal.h"
13 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwmsg);
14 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwerr);
15 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_trap_report);
17 DEFINE_XARRAY_FLAGS(devlinks, XA_FLAGS_ALLOC);
19 void *devlink_priv(struct devlink *devlink)
21 return &devlink->priv;
23 EXPORT_SYMBOL_GPL(devlink_priv);
25 struct devlink *priv_to_devlink(void *priv)
27 return container_of(priv, struct devlink, priv);
29 EXPORT_SYMBOL_GPL(priv_to_devlink);
31 struct device *devlink_to_dev(const struct devlink *devlink)
35 EXPORT_SYMBOL_GPL(devlink_to_dev);
37 struct net *devlink_net(const struct devlink *devlink)
39 return read_pnet(&devlink->_net);
41 EXPORT_SYMBOL_GPL(devlink_net);
43 void devl_assert_locked(struct devlink *devlink)
45 lockdep_assert_held(&devlink->lock);
47 EXPORT_SYMBOL_GPL(devl_assert_locked);
50 /* For use in conjunction with LOCKDEP only e.g. rcu_dereference_protected() */
51 bool devl_lock_is_held(struct devlink *devlink)
53 return lockdep_is_held(&devlink->lock);
55 EXPORT_SYMBOL_GPL(devl_lock_is_held);
58 void devl_lock(struct devlink *devlink)
60 mutex_lock(&devlink->lock);
62 EXPORT_SYMBOL_GPL(devl_lock);
64 int devl_trylock(struct devlink *devlink)
66 return mutex_trylock(&devlink->lock);
68 EXPORT_SYMBOL_GPL(devl_trylock);
70 void devl_unlock(struct devlink *devlink)
72 mutex_unlock(&devlink->lock);
74 EXPORT_SYMBOL_GPL(devl_unlock);
77 * devlink_try_get() - try to obtain a reference on a devlink instance
78 * @devlink: instance to reference
80 * Obtain a reference on a devlink instance. A reference on a devlink instance
81 * only implies that it's safe to take the instance lock. It does not imply
82 * that the instance is registered, use devl_is_registered() after taking
83 * the instance lock to check registration status.
85 struct devlink *__must_check devlink_try_get(struct devlink *devlink)
87 if (refcount_inc_not_zero(&devlink->refcount))
92 static void devlink_release(struct work_struct *work)
94 struct devlink *devlink;
96 devlink = container_of(to_rcu_work(work), struct devlink, rwork);
98 mutex_destroy(&devlink->lock);
99 lockdep_unregister_key(&devlink->lock_key);
103 void devlink_put(struct devlink *devlink)
105 if (refcount_dec_and_test(&devlink->refcount))
106 queue_rcu_work(system_wq, &devlink->rwork);
109 struct devlink *devlinks_xa_find_get(struct net *net, unsigned long *indexp)
111 struct devlink *devlink = NULL;
115 devlink = xa_find(&devlinks, indexp, ULONG_MAX, DEVLINK_REGISTERED);
119 if (!devlink_try_get(devlink))
121 if (!net_eq(devlink_net(devlink), net)) {
122 devlink_put(devlink);
135 * devl_register - Register devlink instance
138 int devl_register(struct devlink *devlink)
140 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
141 devl_assert_locked(devlink);
143 xa_set_mark(&devlinks, devlink->index, DEVLINK_REGISTERED);
144 devlink_notify_register(devlink);
148 EXPORT_SYMBOL_GPL(devl_register);
150 void devlink_register(struct devlink *devlink)
153 devl_register(devlink);
154 devl_unlock(devlink);
156 EXPORT_SYMBOL_GPL(devlink_register);
159 * devl_unregister - Unregister devlink instance
162 void devl_unregister(struct devlink *devlink)
164 ASSERT_DEVLINK_REGISTERED(devlink);
165 devl_assert_locked(devlink);
167 devlink_notify_unregister(devlink);
168 xa_clear_mark(&devlinks, devlink->index, DEVLINK_REGISTERED);
170 EXPORT_SYMBOL_GPL(devl_unregister);
172 void devlink_unregister(struct devlink *devlink)
175 devl_unregister(devlink);
176 devl_unlock(devlink);
178 EXPORT_SYMBOL_GPL(devlink_unregister);
181 * devlink_alloc_ns - Allocate new devlink instance resources
182 * in specific namespace
185 * @priv_size: size of user private data
186 * @net: net namespace
187 * @dev: parent device
189 * Allocate new devlink instance resources, including devlink index
192 struct devlink *devlink_alloc_ns(const struct devlink_ops *ops,
193 size_t priv_size, struct net *net,
196 struct devlink *devlink;
200 WARN_ON(!ops || !dev);
201 if (!devlink_reload_actions_valid(ops))
204 devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
208 ret = xa_alloc_cyclic(&devlinks, &devlink->index, devlink, xa_limit_31b,
209 &last_id, GFP_KERNEL);
215 xa_init_flags(&devlink->ports, XA_FLAGS_ALLOC);
216 xa_init_flags(&devlink->params, XA_FLAGS_ALLOC);
217 xa_init_flags(&devlink->snapshot_ids, XA_FLAGS_ALLOC);
218 write_pnet(&devlink->_net, net);
219 INIT_LIST_HEAD(&devlink->rate_list);
220 INIT_LIST_HEAD(&devlink->linecard_list);
221 INIT_LIST_HEAD(&devlink->sb_list);
222 INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
223 INIT_LIST_HEAD(&devlink->resource_list);
224 INIT_LIST_HEAD(&devlink->region_list);
225 INIT_LIST_HEAD(&devlink->reporter_list);
226 INIT_LIST_HEAD(&devlink->trap_list);
227 INIT_LIST_HEAD(&devlink->trap_group_list);
228 INIT_LIST_HEAD(&devlink->trap_policer_list);
229 INIT_RCU_WORK(&devlink->rwork, devlink_release);
230 lockdep_register_key(&devlink->lock_key);
231 mutex_init(&devlink->lock);
232 lockdep_set_class(&devlink->lock, &devlink->lock_key);
233 refcount_set(&devlink->refcount, 1);
241 EXPORT_SYMBOL_GPL(devlink_alloc_ns);
244 * devlink_free - Free devlink instance resources
248 void devlink_free(struct devlink *devlink)
250 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
252 WARN_ON(!list_empty(&devlink->trap_policer_list));
253 WARN_ON(!list_empty(&devlink->trap_group_list));
254 WARN_ON(!list_empty(&devlink->trap_list));
255 WARN_ON(!list_empty(&devlink->reporter_list));
256 WARN_ON(!list_empty(&devlink->region_list));
257 WARN_ON(!list_empty(&devlink->resource_list));
258 WARN_ON(!list_empty(&devlink->dpipe_table_list));
259 WARN_ON(!list_empty(&devlink->sb_list));
260 WARN_ON(!list_empty(&devlink->rate_list));
261 WARN_ON(!list_empty(&devlink->linecard_list));
262 WARN_ON(!xa_empty(&devlink->ports));
264 xa_destroy(&devlink->snapshot_ids);
265 xa_destroy(&devlink->params);
266 xa_destroy(&devlink->ports);
268 xa_erase(&devlinks, devlink->index);
270 devlink_put(devlink);
272 EXPORT_SYMBOL_GPL(devlink_free);
274 static void __net_exit devlink_pernet_pre_exit(struct net *net)
276 struct devlink *devlink;
277 u32 actions_performed;
281 /* In case network namespace is getting destroyed, reload
282 * all devlink instances from this namespace into init_net.
284 devlinks_xa_for_each_registered_get(net, index, devlink) {
287 if (devl_is_registered(devlink))
288 err = devlink_reload(devlink, &init_net,
289 DEVLINK_RELOAD_ACTION_DRIVER_REINIT,
290 DEVLINK_RELOAD_LIMIT_UNSPEC,
291 &actions_performed, NULL);
292 devl_unlock(devlink);
293 devlink_put(devlink);
294 if (err && err != -EOPNOTSUPP)
295 pr_warn("Failed to reload devlink instance into init_net\n");
299 static struct pernet_operations devlink_pernet_ops __net_initdata = {
300 .pre_exit = devlink_pernet_pre_exit,
303 static struct notifier_block devlink_port_netdevice_nb = {
304 .notifier_call = devlink_port_netdevice_event,
307 static int __init devlink_init(void)
311 err = genl_register_family(&devlink_nl_family);
314 err = register_pernet_subsys(&devlink_pernet_ops);
317 err = register_netdevice_notifier(&devlink_port_netdevice_nb);
324 subsys_initcall(devlink_init);