]>
Commit | Line | Data |
---|---|---|
687125b5 JK |
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 | ||
eec1e5ea | 6 | #include <linux/etherdevice.h> |
687125b5 JK |
7 | #include <linux/mutex.h> |
8 | #include <linux/netdevice.h> | |
9 | #include <linux/notifier.h> | |
10 | #include <linux/types.h> | |
93e71edf | 11 | #include <linux/workqueue.h> |
687125b5 JK |
12 | #include <linux/xarray.h> |
13 | #include <net/devlink.h> | |
14 | #include <net/net_namespace.h> | |
eec1e5ea JP |
15 | #include <net/rtnetlink.h> |
16 | #include <rdma/ib_verbs.h> | |
687125b5 | 17 | |
b2551b15 JP |
18 | #include "netlink_gen.h" |
19 | ||
c137743b JP |
20 | struct devlink_rel; |
21 | ||
687125b5 | 22 | #define DEVLINK_REGISTERED XA_MARK_1 |
687125b5 JK |
23 | |
24 | #define DEVLINK_RELOAD_STATS_ARRAY_SIZE \ | |
25 | (__DEVLINK_RELOAD_LIMIT_MAX * __DEVLINK_RELOAD_ACTION_MAX) | |
26 | ||
27 | struct devlink_dev_stats { | |
28 | u32 reload_stats[DEVLINK_RELOAD_STATS_ARRAY_SIZE]; | |
29 | u32 remote_reload_stats[DEVLINK_RELOAD_STATS_ARRAY_SIZE]; | |
30 | }; | |
31 | ||
32 | struct devlink { | |
33 | u32 index; | |
34 | struct xarray ports; | |
35 | struct list_head rate_list; | |
36 | struct list_head sb_list; | |
37 | struct list_head dpipe_table_list; | |
38 | struct list_head resource_list; | |
a72e17b4 | 39 | struct xarray params; |
687125b5 JK |
40 | struct list_head region_list; |
41 | struct list_head reporter_list; | |
687125b5 JK |
42 | struct devlink_dpipe_headers *dpipe_headers; |
43 | struct list_head trap_list; | |
44 | struct list_head trap_group_list; | |
45 | struct list_head trap_policer_list; | |
46 | struct list_head linecard_list; | |
687125b5 | 47 | const struct devlink_ops *ops; |
687125b5 JK |
48 | struct xarray snapshot_ids; |
49 | struct devlink_dev_stats stats; | |
50 | struct device *dev; | |
51 | possible_net_t _net; | |
52 | /* Serializes access to devlink instance specific objects such as | |
53 | * port, sb, dpipe, resource, params, region, traps and more. | |
54 | */ | |
55 | struct mutex lock; | |
56 | struct lock_class_key lock_key; | |
57 | u8 reload_failed:1; | |
58 | refcount_t refcount; | |
93e71edf | 59 | struct rcu_work rwork; |
c137743b | 60 | struct devlink_rel *rel; |
c5e1bf8a | 61 | struct xarray nested_rels; |
687125b5 JK |
62 | char priv[] __aligned(NETDEV_ALIGN); |
63 | }; | |
64 | ||
65 | extern struct xarray devlinks; | |
66 | extern struct genl_family devlink_nl_family; | |
67 | ||
68 | /* devlink instances are open to the access from the user space after | |
69 | * devlink_register() call. Such logical barrier allows us to have certain | |
70 | * expectations related to locking. | |
71 | * | |
72 | * Before *_register() - we are in initialization stage and no parallel | |
73 | * access possible to the devlink instance. All drivers perform that phase | |
74 | * by implicitly holding device_lock. | |
75 | * | |
76 | * After *_register() - users and driver can access devlink instance at | |
77 | * the same time. | |
78 | */ | |
79 | #define ASSERT_DEVLINK_REGISTERED(d) \ | |
80 | WARN_ON_ONCE(!xa_get_mark(&devlinks, (d)->index, DEVLINK_REGISTERED)) | |
81 | #define ASSERT_DEVLINK_NOT_REGISTERED(d) \ | |
82 | WARN_ON_ONCE(xa_get_mark(&devlinks, (d)->index, DEVLINK_REGISTERED)) | |
83 | ||
84 | /* Iterate over devlink pointers which were possible to get reference to. | |
85 | * devlink_put() needs to be called for each iterated devlink pointer | |
86 | * in loop body in order to release the reference. | |
87 | */ | |
687125b5 | 88 | #define devlinks_xa_for_each_registered_get(net, index, devlink) \ |
d7727819 JK |
89 | for (index = 0; (devlink = devlinks_xa_find_get(net, &index)); index++) |
90 | ||
91 | struct devlink *devlinks_xa_find_get(struct net *net, unsigned long *indexp); | |
687125b5 | 92 | |
ed539ba6 JK |
93 | static inline bool devl_is_registered(struct devlink *devlink) |
94 | { | |
63ba54a5 | 95 | devl_assert_locked(devlink); |
ed539ba6 JK |
96 | return xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED); |
97 | } | |
98 | ||
c137743b JP |
99 | typedef void devlink_rel_notify_cb_t(struct devlink *devlink, u32 obj_index); |
100 | typedef void devlink_rel_cleanup_cb_t(struct devlink *devlink, u32 obj_index, | |
101 | u32 rel_index); | |
102 | ||
103 | void devlink_rel_nested_in_clear(u32 rel_index); | |
104 | int devlink_rel_nested_in_add(u32 *rel_index, u32 devlink_index, | |
105 | u32 obj_index, devlink_rel_notify_cb_t *notify_cb, | |
106 | devlink_rel_cleanup_cb_t *cleanup_cb, | |
107 | struct devlink *devlink); | |
108 | void devlink_rel_nested_in_notify(struct devlink *devlink); | |
109 | int devlink_rel_devlink_handle_put(struct sk_buff *msg, struct devlink *devlink, | |
110 | u32 rel_index, int attrtype, | |
111 | bool *msg_updated); | |
112 | ||
687125b5 | 113 | /* Netlink */ |
623cd13b JK |
114 | #define DEVLINK_NL_FLAG_NEED_PORT BIT(0) |
115 | #define DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT BIT(1) | |
623cd13b JK |
116 | |
117 | enum devlink_multicast_groups { | |
118 | DEVLINK_MCGRP_CONFIG, | |
119 | }; | |
120 | ||
3015f822 JK |
121 | /* state held across netlink dumps */ |
122 | struct devlink_nl_dump_state { | |
731d69a6 | 123 | unsigned long instance; |
3015f822 JK |
124 | int idx; |
125 | union { | |
126 | /* DEVLINK_CMD_REGION_READ */ | |
127 | struct { | |
128 | u64 start_offset; | |
129 | }; | |
130 | /* DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET */ | |
131 | struct { | |
132 | u64 dump_ts; | |
133 | }; | |
134 | }; | |
135 | }; | |
136 | ||
491a2487 JP |
137 | typedef int devlink_nl_dump_one_func_t(struct sk_buff *msg, |
138 | struct devlink *devlink, | |
7d3c6fec JP |
139 | struct netlink_callback *cb, |
140 | int flags); | |
491a2487 | 141 | |
870c7ad4 JK |
142 | struct devlink * |
143 | devlink_get_from_attrs_lock(struct net *net, struct nlattr **attrs); | |
623cd13b | 144 | |
491a2487 JP |
145 | int devlink_nl_dumpit(struct sk_buff *msg, struct netlink_callback *cb, |
146 | devlink_nl_dump_one_func_t *dump_one); | |
07f3af66 | 147 | |
3015f822 JK |
148 | static inline struct devlink_nl_dump_state * |
149 | devlink_dump_state(struct netlink_callback *cb) | |
150 | { | |
ec8f7d49 | 151 | NL_ASSERT_DUMP_CTX_FITS(struct devlink_nl_dump_state); |
3015f822 JK |
152 | |
153 | return (struct devlink_nl_dump_state *)cb->ctx; | |
154 | } | |
155 | ||
dbeeca81 MS |
156 | static inline int |
157 | devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink) | |
158 | { | |
159 | if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name)) | |
160 | return -EMSGSIZE; | |
161 | if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev))) | |
162 | return -EMSGSIZE; | |
163 | return 0; | |
164 | } | |
165 | ||
af1f1400 | 166 | int devlink_nl_put_nested_handle(struct sk_buff *msg, struct net *net, |
1c2197c4 | 167 | struct devlink *devlink, int attrtype); |
2475ed15 JP |
168 | int devlink_nl_msg_reply_and_new(struct sk_buff **msg, struct genl_info *info); |
169 | ||
dbeeca81 | 170 | /* Notify */ |
71179ac5 JP |
171 | void devlink_notify_register(struct devlink *devlink); |
172 | void devlink_notify_unregister(struct devlink *devlink); | |
eec1e5ea JP |
173 | void devlink_ports_notify_register(struct devlink *devlink); |
174 | void devlink_ports_notify_unregister(struct devlink *devlink); | |
830c41e1 JP |
175 | void devlink_params_notify_register(struct devlink *devlink); |
176 | void devlink_params_notify_unregister(struct devlink *devlink); | |
1aa47ca1 JP |
177 | void devlink_regions_notify_register(struct devlink *devlink); |
178 | void devlink_regions_notify_unregister(struct devlink *devlink); | |
4bbdec80 JP |
179 | void devlink_trap_policers_notify_register(struct devlink *devlink); |
180 | void devlink_trap_policers_notify_unregister(struct devlink *devlink); | |
181 | void devlink_trap_groups_notify_register(struct devlink *devlink); | |
182 | void devlink_trap_groups_notify_unregister(struct devlink *devlink); | |
183 | void devlink_traps_notify_register(struct devlink *devlink); | |
184 | void devlink_traps_notify_unregister(struct devlink *devlink); | |
7cc7194e JP |
185 | void devlink_rates_notify_register(struct devlink *devlink); |
186 | void devlink_rates_notify_unregister(struct devlink *devlink); | |
9edbe6f3 JP |
187 | void devlink_linecards_notify_register(struct devlink *devlink); |
188 | void devlink_linecards_notify_unregister(struct devlink *devlink); | |
dbeeca81 | 189 | |
687125b5 | 190 | /* Ports */ |
eec1e5ea JP |
191 | #define ASSERT_DEVLINK_PORT_INITIALIZED(devlink_port) \ |
192 | WARN_ON_ONCE(!(devlink_port)->initialized) | |
193 | ||
194 | struct devlink_port *devlink_port_get_by_index(struct devlink *devlink, | |
195 | unsigned int port_index); | |
687125b5 JK |
196 | int devlink_port_netdevice_event(struct notifier_block *nb, |
197 | unsigned long event, void *ptr); | |
623cd13b JK |
198 | struct devlink_port * |
199 | devlink_port_get_from_info(struct devlink *devlink, struct genl_info *info); | |
db6b5f3e MS |
200 | struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink, |
201 | struct nlattr **attrs); | |
623cd13b | 202 | |
687125b5 JK |
203 | /* Reload */ |
204 | bool devlink_reload_actions_valid(const struct devlink_ops *ops); | |
205 | int devlink_reload(struct devlink *devlink, struct net *dest_net, | |
206 | enum devlink_reload_action action, | |
207 | enum devlink_reload_limit limit, | |
208 | u32 *actions_performed, struct netlink_ext_ack *extack); | |
209 | ||
210 | static inline bool devlink_reload_supported(const struct devlink_ops *ops) | |
211 | { | |
212 | return ops->reload_down && ops->reload_up; | |
213 | } | |
623cd13b | 214 | |
afd888c3 JP |
215 | /* Params */ |
216 | void devlink_params_driverinit_load_new(struct devlink *devlink); | |
217 | ||
c6ed7d6e MS |
218 | /* Resources */ |
219 | struct devlink_resource; | |
220 | int devlink_resources_validate(struct devlink *devlink, | |
221 | struct devlink_resource *resource, | |
222 | struct genl_info *info); | |
223 | ||
623cd13b | 224 | /* Rates */ |
af2f8c1f MS |
225 | int devlink_rate_nodes_check(struct devlink *devlink, u16 mode, |
226 | struct netlink_ext_ack *extack); | |
41a1d4d1 | 227 | |
9edbe6f3 | 228 | /* Linecards */ |
d0b7e990 | 229 | unsigned int devlink_linecard_index(struct devlink_linecard *linecard); |