xref: /openbmc/linux/net/devlink/devl_internal.h (revision ae7d45fb)
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 #define DEVLINK_REGISTERED XA_MARK_1
16 
17 #define DEVLINK_RELOAD_STATS_ARRAY_SIZE \
18 	(__DEVLINK_RELOAD_LIMIT_MAX * __DEVLINK_RELOAD_ACTION_MAX)
19 
20 struct devlink_dev_stats {
21 	u32 reload_stats[DEVLINK_RELOAD_STATS_ARRAY_SIZE];
22 	u32 remote_reload_stats[DEVLINK_RELOAD_STATS_ARRAY_SIZE];
23 };
24 
25 struct devlink {
26 	u32 index;
27 	struct xarray ports;
28 	struct list_head rate_list;
29 	struct list_head sb_list;
30 	struct list_head dpipe_table_list;
31 	struct list_head resource_list;
32 	struct xarray params;
33 	struct list_head region_list;
34 	struct list_head reporter_list;
35 	struct devlink_dpipe_headers *dpipe_headers;
36 	struct list_head trap_list;
37 	struct list_head trap_group_list;
38 	struct list_head trap_policer_list;
39 	struct list_head linecard_list;
40 	const struct devlink_ops *ops;
41 	struct xarray snapshot_ids;
42 	struct devlink_dev_stats stats;
43 	struct device *dev;
44 	possible_net_t _net;
45 	/* Serializes access to devlink instance specific objects such as
46 	 * port, sb, dpipe, resource, params, region, traps and more.
47 	 */
48 	struct mutex lock;
49 	struct lock_class_key lock_key;
50 	u8 reload_failed:1;
51 	refcount_t refcount;
52 	struct rcu_work rwork;
53 	struct notifier_block netdevice_nb;
54 	char priv[] __aligned(NETDEV_ALIGN);
55 };
56 
57 extern struct xarray devlinks;
58 extern struct genl_family devlink_nl_family;
59 
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 
76 /* Iterate over devlink pointers which were possible to get reference to.
77  * devlink_put() needs to be called for each iterated devlink pointer
78  * in loop body in order to release the reference.
79  */
80 #define devlinks_xa_for_each_registered_get(net, index, devlink)	\
81 	for (index = 0; (devlink = devlinks_xa_find_get(net, &index)); index++)
82 
83 struct devlink *devlinks_xa_find_get(struct net *net, unsigned long *indexp);
84 
85 static inline bool devl_is_registered(struct devlink *devlink)
86 {
87 	devl_assert_locked(devlink);
88 	return xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED);
89 }
90 
91 /* Netlink */
92 #define DEVLINK_NL_FLAG_NEED_PORT		BIT(0)
93 #define DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT	BIT(1)
94 #define DEVLINK_NL_FLAG_NEED_RATE		BIT(2)
95 #define DEVLINK_NL_FLAG_NEED_RATE_NODE		BIT(3)
96 #define DEVLINK_NL_FLAG_NEED_LINECARD		BIT(4)
97 
98 enum devlink_multicast_groups {
99 	DEVLINK_MCGRP_CONFIG,
100 };
101 
102 /* state held across netlink dumps */
103 struct devlink_nl_dump_state {
104 	unsigned long instance;
105 	int idx;
106 	union {
107 		/* DEVLINK_CMD_REGION_READ */
108 		struct {
109 			u64 start_offset;
110 		};
111 		/* DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET */
112 		struct {
113 			u64 dump_ts;
114 		};
115 	};
116 };
117 
118 struct devlink_cmd {
119 	int (*dump_one)(struct sk_buff *msg, struct devlink *devlink,
120 			struct netlink_callback *cb);
121 };
122 
123 extern const struct genl_small_ops devlink_nl_ops[56];
124 
125 struct devlink *
126 devlink_get_from_attrs_lock(struct net *net, struct nlattr **attrs);
127 
128 void devlink_notify_unregister(struct devlink *devlink);
129 void devlink_notify_register(struct devlink *devlink);
130 
131 int devlink_nl_instance_iter_dumpit(struct sk_buff *msg,
132 				    struct netlink_callback *cb);
133 
134 static inline struct devlink_nl_dump_state *
135 devlink_dump_state(struct netlink_callback *cb)
136 {
137 	NL_ASSERT_DUMP_CTX_FITS(struct devlink_nl_dump_state);
138 
139 	return (struct devlink_nl_dump_state *)cb->ctx;
140 }
141 
142 static inline int
143 devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
144 {
145 	if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
146 		return -EMSGSIZE;
147 	if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
148 		return -EMSGSIZE;
149 	return 0;
150 }
151 
152 /* Commands */
153 extern const struct devlink_cmd devl_cmd_get;
154 extern const struct devlink_cmd devl_cmd_port_get;
155 extern const struct devlink_cmd devl_cmd_sb_get;
156 extern const struct devlink_cmd devl_cmd_sb_pool_get;
157 extern const struct devlink_cmd devl_cmd_sb_port_pool_get;
158 extern const struct devlink_cmd devl_cmd_sb_tc_pool_bind_get;
159 extern const struct devlink_cmd devl_cmd_param_get;
160 extern const struct devlink_cmd devl_cmd_region_get;
161 extern const struct devlink_cmd devl_cmd_info_get;
162 extern const struct devlink_cmd devl_cmd_health_reporter_get;
163 extern const struct devlink_cmd devl_cmd_trap_get;
164 extern const struct devlink_cmd devl_cmd_trap_group_get;
165 extern const struct devlink_cmd devl_cmd_trap_policer_get;
166 extern const struct devlink_cmd devl_cmd_rate_get;
167 extern const struct devlink_cmd devl_cmd_linecard_get;
168 extern const struct devlink_cmd devl_cmd_selftests_get;
169 
170 /* Notify */
171 void devlink_notify(struct devlink *devlink, enum devlink_command cmd);
172 
173 /* Ports */
174 int devlink_port_netdevice_event(struct notifier_block *nb,
175 				 unsigned long event, void *ptr);
176 
177 struct devlink_port *
178 devlink_port_get_from_info(struct devlink *devlink, struct genl_info *info);
179 struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
180 						 struct nlattr **attrs);
181 
182 /* Reload */
183 bool devlink_reload_actions_valid(const struct devlink_ops *ops);
184 int devlink_reload(struct devlink *devlink, struct net *dest_net,
185 		   enum devlink_reload_action action,
186 		   enum devlink_reload_limit limit,
187 		   u32 *actions_performed, struct netlink_ext_ack *extack);
188 
189 static inline bool devlink_reload_supported(const struct devlink_ops *ops)
190 {
191 	return ops->reload_down && ops->reload_up;
192 }
193 
194 /* Params */
195 void devlink_params_driverinit_load_new(struct devlink *devlink);
196 
197 /* Resources */
198 struct devlink_resource;
199 int devlink_resources_validate(struct devlink *devlink,
200 			       struct devlink_resource *resource,
201 			       struct genl_info *info);
202 
203 /* Line cards */
204 struct devlink_linecard;
205 
206 struct devlink_linecard *
207 devlink_linecard_get_from_info(struct devlink *devlink, struct genl_info *info);
208 
209 /* Rates */
210 int devlink_rate_nodes_check(struct devlink *devlink, u16 mode,
211 			     struct netlink_ext_ack *extack);
212 struct devlink_rate *
213 devlink_rate_get_from_info(struct devlink *devlink, struct genl_info *info);
214 struct devlink_rate *
215 devlink_rate_node_get_from_info(struct devlink *devlink,
216 				struct genl_info *info);
217 /* Devlink nl cmds */
218 int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info);
219 int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info);
220 int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb, struct genl_info *info);
221 int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb, struct genl_info *info);
222 int devlink_nl_cmd_info_get_doit(struct sk_buff *skb, struct genl_info *info);
223 int devlink_nl_cmd_flash_update(struct sk_buff *skb, struct genl_info *info);
224 int devlink_nl_cmd_selftests_get_doit(struct sk_buff *skb, struct genl_info *info);
225 int devlink_nl_cmd_selftests_run(struct sk_buff *skb, struct genl_info *info);
226 int devlink_nl_cmd_health_reporter_get_doit(struct sk_buff *skb,
227 					    struct genl_info *info);
228 int devlink_nl_cmd_health_reporter_set_doit(struct sk_buff *skb,
229 					    struct genl_info *info);
230 int devlink_nl_cmd_health_reporter_recover_doit(struct sk_buff *skb,
231 						struct genl_info *info);
232 int devlink_nl_cmd_health_reporter_diagnose_doit(struct sk_buff *skb,
233 						 struct genl_info *info);
234 int devlink_nl_cmd_health_reporter_dump_get_dumpit(struct sk_buff *skb,
235 						   struct netlink_callback *cb);
236 int devlink_nl_cmd_health_reporter_dump_clear_doit(struct sk_buff *skb,
237 						   struct genl_info *info);
238 int devlink_nl_cmd_health_reporter_test_doit(struct sk_buff *skb,
239 					     struct genl_info *info);
240