xref: /openbmc/linux/net/devlink/devl_internal.h (revision c1e0230e)
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 
6 #include <linux/mutex.h>
7 #include <linux/netdevice.h>
8 #include <linux/notifier.h>
9 #include <linux/types.h>
10 #include <linux/workqueue.h>
11 #include <linux/xarray.h>
12 #include <net/devlink.h>
13 #include <net/net_namespace.h>
14 
15 #include "netlink_gen.h"
16 
17 #define DEVLINK_REGISTERED XA_MARK_1
18 
19 #define DEVLINK_RELOAD_STATS_ARRAY_SIZE \
20 	(__DEVLINK_RELOAD_LIMIT_MAX * __DEVLINK_RELOAD_ACTION_MAX)
21 
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 
27 struct devlink {
28 	u32 index;
29 	struct xarray ports;
30 	struct list_head rate_list;
31 	struct list_head sb_list;
32 	struct list_head dpipe_table_list;
33 	struct list_head resource_list;
34 	struct xarray params;
35 	struct list_head region_list;
36 	struct list_head reporter_list;
37 	struct devlink_dpipe_headers *dpipe_headers;
38 	struct list_head trap_list;
39 	struct list_head trap_group_list;
40 	struct list_head trap_policer_list;
41 	struct list_head linecard_list;
42 	const struct devlink_ops *ops;
43 	struct xarray snapshot_ids;
44 	struct devlink_dev_stats stats;
45 	struct device *dev;
46 	possible_net_t _net;
47 	/* Serializes access to devlink instance specific objects such as
48 	 * port, sb, dpipe, resource, params, region, traps and more.
49 	 */
50 	struct mutex lock;
51 	struct lock_class_key lock_key;
52 	u8 reload_failed:1;
53 	refcount_t refcount;
54 	struct rcu_work rwork;
55 	char priv[] __aligned(NETDEV_ALIGN);
56 };
57 
58 extern struct xarray devlinks;
59 extern struct genl_family devlink_nl_family;
60 
61 /* devlink instances are open to the access from the user space after
62  * devlink_register() call. Such logical barrier allows us to have certain
63  * expectations related to locking.
64  *
65  * Before *_register() - we are in initialization stage and no parallel
66  * access possible to the devlink instance. All drivers perform that phase
67  * by implicitly holding device_lock.
68  *
69  * After *_register() - users and driver can access devlink instance at
70  * the same time.
71  */
72 #define ASSERT_DEVLINK_REGISTERED(d)                                           \
73 	WARN_ON_ONCE(!xa_get_mark(&devlinks, (d)->index, DEVLINK_REGISTERED))
74 #define ASSERT_DEVLINK_NOT_REGISTERED(d)                                       \
75 	WARN_ON_ONCE(xa_get_mark(&devlinks, (d)->index, DEVLINK_REGISTERED))
76 
77 /* Iterate over devlink pointers which were possible to get reference to.
78  * devlink_put() needs to be called for each iterated devlink pointer
79  * in loop body in order to release the reference.
80  */
81 #define devlinks_xa_for_each_registered_get(net, index, devlink)	\
82 	for (index = 0; (devlink = devlinks_xa_find_get(net, &index)); index++)
83 
84 struct devlink *devlinks_xa_find_get(struct net *net, unsigned long *indexp);
85 
86 static inline bool devl_is_registered(struct devlink *devlink)
87 {
88 	devl_assert_locked(devlink);
89 	return xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED);
90 }
91 
92 /* Netlink */
93 #define DEVLINK_NL_FLAG_NEED_PORT		BIT(0)
94 #define DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT	BIT(1)
95 
96 enum devlink_multicast_groups {
97 	DEVLINK_MCGRP_CONFIG,
98 };
99 
100 /* state held across netlink dumps */
101 struct devlink_nl_dump_state {
102 	unsigned long instance;
103 	int idx;
104 	union {
105 		/* DEVLINK_CMD_REGION_READ */
106 		struct {
107 			u64 start_offset;
108 		};
109 		/* DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET */
110 		struct {
111 			u64 dump_ts;
112 		};
113 	};
114 };
115 
116 typedef int devlink_nl_dump_one_func_t(struct sk_buff *msg,
117 				       struct devlink *devlink,
118 				       struct netlink_callback *cb,
119 				       int flags);
120 
121 extern const struct genl_small_ops devlink_nl_small_ops[40];
122 
123 struct devlink *
124 devlink_get_from_attrs_lock(struct net *net, struct nlattr **attrs);
125 
126 void devlink_notify_unregister(struct devlink *devlink);
127 void devlink_notify_register(struct devlink *devlink);
128 
129 int devlink_nl_dumpit(struct sk_buff *msg, struct netlink_callback *cb,
130 		      devlink_nl_dump_one_func_t *dump_one);
131 
132 static inline struct devlink_nl_dump_state *
133 devlink_dump_state(struct netlink_callback *cb)
134 {
135 	NL_ASSERT_DUMP_CTX_FITS(struct devlink_nl_dump_state);
136 
137 	return (struct devlink_nl_dump_state *)cb->ctx;
138 }
139 
140 static inline int
141 devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
142 {
143 	if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
144 		return -EMSGSIZE;
145 	if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
146 		return -EMSGSIZE;
147 	return 0;
148 }
149 
150 /* Notify */
151 void devlink_notify(struct devlink *devlink, enum devlink_command cmd);
152 
153 /* Ports */
154 int devlink_port_netdevice_event(struct notifier_block *nb,
155 				 unsigned long event, void *ptr);
156 
157 struct devlink_port *
158 devlink_port_get_from_info(struct devlink *devlink, struct genl_info *info);
159 struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
160 						 struct nlattr **attrs);
161 
162 /* Reload */
163 bool devlink_reload_actions_valid(const struct devlink_ops *ops);
164 int devlink_reload(struct devlink *devlink, struct net *dest_net,
165 		   enum devlink_reload_action action,
166 		   enum devlink_reload_limit limit,
167 		   u32 *actions_performed, struct netlink_ext_ack *extack);
168 
169 static inline bool devlink_reload_supported(const struct devlink_ops *ops)
170 {
171 	return ops->reload_down && ops->reload_up;
172 }
173 
174 /* Params */
175 void devlink_params_driverinit_load_new(struct devlink *devlink);
176 
177 /* Resources */
178 struct devlink_resource;
179 int devlink_resources_validate(struct devlink *devlink,
180 			       struct devlink_resource *resource,
181 			       struct genl_info *info);
182 
183 /* Rates */
184 int devlink_rate_nodes_check(struct devlink *devlink, u16 mode,
185 			     struct netlink_ext_ack *extack);
186 
187 /* Devlink nl cmds */
188 int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info);
189 int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb, struct genl_info *info);
190 int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb, struct genl_info *info);
191 int devlink_nl_cmd_flash_update(struct sk_buff *skb, struct genl_info *info);
192 int devlink_nl_cmd_selftests_run(struct sk_buff *skb, struct genl_info *info);
193 int devlink_nl_cmd_health_reporter_set_doit(struct sk_buff *skb,
194 					    struct genl_info *info);
195 int devlink_nl_cmd_health_reporter_recover_doit(struct sk_buff *skb,
196 						struct genl_info *info);
197 int devlink_nl_cmd_health_reporter_diagnose_doit(struct sk_buff *skb,
198 						 struct genl_info *info);
199 int devlink_nl_cmd_health_reporter_dump_get_dumpit(struct sk_buff *skb,
200 						   struct netlink_callback *cb);
201 int devlink_nl_cmd_health_reporter_dump_clear_doit(struct sk_buff *skb,
202 						   struct genl_info *info);
203 int devlink_nl_cmd_health_reporter_test_doit(struct sk_buff *skb,
204 					     struct genl_info *info);
205