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/device.h> 7 #include <linux/etherdevice.h> 8 #include <linux/mutex.h> 9 #include <linux/netdevice.h> 10 #include <linux/notifier.h> 11 #include <linux/types.h> 12 #include <linux/workqueue.h> 13 #include <linux/xarray.h> 14 #include <net/devlink.h> 15 #include <net/net_namespace.h> 16 #include <net/rtnetlink.h> 17 #include <rdma/ib_verbs.h> 18 19 #include "netlink_gen.h" 20 21 struct devlink_rel; 22 23 #define DEVLINK_REGISTERED XA_MARK_1 24 25 #define DEVLINK_RELOAD_STATS_ARRAY_SIZE \ 26 (__DEVLINK_RELOAD_LIMIT_MAX * __DEVLINK_RELOAD_ACTION_MAX) 27 28 struct devlink_dev_stats { 29 u32 reload_stats[DEVLINK_RELOAD_STATS_ARRAY_SIZE]; 30 u32 remote_reload_stats[DEVLINK_RELOAD_STATS_ARRAY_SIZE]; 31 }; 32 33 struct devlink { 34 u32 index; 35 struct xarray ports; 36 struct list_head rate_list; 37 struct list_head sb_list; 38 struct list_head dpipe_table_list; 39 struct list_head resource_list; 40 struct xarray params; 41 struct list_head region_list; 42 struct list_head reporter_list; 43 struct devlink_dpipe_headers *dpipe_headers; 44 struct list_head trap_list; 45 struct list_head trap_group_list; 46 struct list_head trap_policer_list; 47 struct list_head linecard_list; 48 const struct devlink_ops *ops; 49 struct xarray snapshot_ids; 50 struct devlink_dev_stats stats; 51 struct device *dev; 52 const char *dev_name_index; 53 const struct device_driver *dev_driver; 54 possible_net_t _net; 55 /* Serializes access to devlink instance specific objects such as 56 * port, sb, dpipe, resource, params, region, traps and more. 57 */ 58 struct mutex lock; 59 struct lock_class_key lock_key; 60 u8 reload_failed:1; 61 refcount_t refcount; 62 struct rcu_work rwork; 63 struct devlink_rel *rel; 64 struct xarray nested_rels; 65 char priv[] __aligned(NETDEV_ALIGN); 66 }; 67 68 extern struct xarray devlinks; 69 extern struct genl_family devlink_nl_family; 70 71 struct devlink *__devlink_alloc(const struct devlink_ops *ops, size_t priv_size, 72 struct net *net, struct device *dev, 73 const struct device_driver *dev_driver); 74 75 #define devl_warn(devlink, format, args...) \ 76 do { \ 77 if ((devlink)->dev) \ 78 dev_warn((devlink)->dev, format, ##args); \ 79 else \ 80 pr_warn("devlink (%s): " format, \ 81 devlink_dev_name(devlink), ##args); \ 82 } while (0) 83 84 /* devlink instances are open to the access from the user space after 85 * devlink_register() call. Such logical barrier allows us to have certain 86 * expectations related to locking. 87 * 88 * Before *_register() - we are in initialization stage and no parallel 89 * access possible to the devlink instance. All drivers perform that phase 90 * by implicitly holding device_lock. 91 * 92 * After *_register() - users and driver can access devlink instance at 93 * the same time. 94 */ 95 #define ASSERT_DEVLINK_REGISTERED(d) \ 96 WARN_ON_ONCE(!xa_get_mark(&devlinks, (d)->index, DEVLINK_REGISTERED)) 97 #define ASSERT_DEVLINK_NOT_REGISTERED(d) \ 98 WARN_ON_ONCE(xa_get_mark(&devlinks, (d)->index, DEVLINK_REGISTERED)) 99 100 /* Iterate over devlink pointers which were possible to get reference to. 101 * devlink_put() needs to be called for each iterated devlink pointer 102 * in loop body in order to release the reference. 103 */ 104 #define devlinks_xa_for_each_registered_get(net, index, devlink) \ 105 for (index = 0; (devlink = devlinks_xa_find_get(net, &index)); index++) 106 107 struct devlink *devlinks_xa_find_get(struct net *net, unsigned long *indexp); 108 struct devlink *devlinks_xa_lookup_get(struct net *net, unsigned long index); 109 110 static inline bool __devl_is_registered(struct devlink *devlink) 111 { 112 return xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED); 113 } 114 115 static inline bool devl_is_registered(struct devlink *devlink) 116 { 117 devl_assert_locked(devlink); 118 return __devl_is_registered(devlink); 119 } 120 121 static inline void devl_dev_lock(struct devlink *devlink, bool dev_lock) 122 { 123 if (dev_lock && devlink->dev) 124 device_lock(devlink->dev); 125 devl_lock(devlink); 126 } 127 128 static inline void devl_dev_unlock(struct devlink *devlink, bool dev_lock) 129 { 130 devl_unlock(devlink); 131 if (dev_lock && devlink->dev) 132 device_unlock(devlink->dev); 133 } 134 135 typedef void devlink_rel_notify_cb_t(struct devlink *devlink, u32 obj_index); 136 typedef void devlink_rel_cleanup_cb_t(struct devlink *devlink, u32 obj_index, 137 u32 rel_index); 138 139 /* Returns the locked+referenced nested-in instance or NULL. */ 140 struct devlink *__must_check 141 devlink_nested_in_get_lock(struct devlink *devlink); 142 143 void devlink_rel_nested_in_clear(u32 rel_index); 144 int devlink_rel_nested_in_add(u32 *rel_index, u32 devlink_index, 145 u32 obj_index, devlink_rel_notify_cb_t *notify_cb, 146 devlink_rel_cleanup_cb_t *cleanup_cb, 147 struct devlink *devlink); 148 void devlink_rel_nested_in_notify(struct devlink *devlink); 149 int devlink_rel_devlink_handle_put(struct sk_buff *msg, struct devlink *devlink, 150 u32 rel_index, int attrtype, 151 bool *msg_updated); 152 153 /* Netlink */ 154 struct devlink_nl_ctx { 155 struct devlink *devlink; 156 struct devlink_port *devlink_port; 157 struct devlink *parent_devlink; 158 }; 159 160 static inline struct devlink_nl_ctx * 161 devlink_nl_ctx(struct genl_info *info) 162 { 163 BUILD_BUG_ON(sizeof(struct devlink_nl_ctx) > 164 sizeof_field(struct genl_info, ctx)); 165 return (struct devlink_nl_ctx *)info->ctx; 166 } 167 168 enum devlink_multicast_groups { 169 DEVLINK_MCGRP_CONFIG, 170 }; 171 172 /* state held across netlink dumps */ 173 struct devlink_nl_dump_state { 174 unsigned long instance; 175 int idx; 176 union { 177 /* DEVLINK_CMD_REGION_READ */ 178 struct { 179 u64 start_offset; 180 }; 181 /* DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET */ 182 struct { 183 u64 dump_ts; 184 }; 185 /* DEVLINK_CMD_RESOURCE_DUMP */ 186 struct { 187 u32 index; 188 bool index_valid; 189 } port_ctx; 190 }; 191 }; 192 193 typedef int devlink_nl_dump_one_func_t(struct sk_buff *msg, 194 struct devlink *devlink, 195 struct netlink_callback *cb, 196 int flags); 197 198 struct devlink * 199 devlink_get_from_attrs_lock(struct net *net, struct nlattr **attrs, 200 bool dev_lock); 201 struct devlink * 202 devlink_get_parent_from_attrs_lock(struct net *net, struct nlattr **attrs); 203 204 int devlink_nl_dumpit(struct sk_buff *msg, struct netlink_callback *cb, 205 devlink_nl_dump_one_func_t *dump_one); 206 207 static inline struct devlink_nl_dump_state * 208 devlink_dump_state(struct netlink_callback *cb) 209 { 210 NL_ASSERT_CTX_FITS(struct devlink_nl_dump_state); 211 212 return (struct devlink_nl_dump_state *)cb->ctx; 213 } 214 215 static inline int 216 devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink) 217 { 218 if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink_bus_name(devlink))) 219 return -EMSGSIZE; 220 if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, devlink_dev_name(devlink))) 221 return -EMSGSIZE; 222 if (nla_put_uint(msg, DEVLINK_ATTR_INDEX, devlink->index)) 223 return -EMSGSIZE; 224 return 0; 225 } 226 227 static inline int devlink_nl_put_u64(struct sk_buff *msg, int attrtype, u64 val) 228 { 229 return nla_put_u64_64bit(msg, attrtype, val, DEVLINK_ATTR_PAD); 230 } 231 232 int devlink_nl_put_nested_handle(struct sk_buff *msg, struct net *net, 233 struct devlink *devlink, int attrtype); 234 int devlink_nl_msg_reply_and_new(struct sk_buff **msg, struct genl_info *info); 235 236 static inline bool devlink_nl_notify_need(struct devlink *devlink) 237 { 238 return genl_has_listeners(&devlink_nl_family, devlink_net(devlink), 239 DEVLINK_MCGRP_CONFIG); 240 } 241 242 struct devlink_obj_desc { 243 struct rcu_head rcu; 244 const char *bus_name; 245 const char *dev_name; 246 unsigned int port_index; 247 bool port_index_valid; 248 unsigned int devlink_index; 249 bool devlink_index_valid; 250 long data[]; 251 }; 252 253 static inline void devlink_nl_obj_desc_init(struct devlink_obj_desc *desc, 254 struct devlink *devlink) 255 { 256 memset(desc, 0, sizeof(*desc)); 257 desc->bus_name = devlink_bus_name(devlink); 258 desc->dev_name = devlink_dev_name(devlink); 259 desc->devlink_index = devlink->index; 260 desc->devlink_index_valid = true; 261 } 262 263 static inline void devlink_nl_obj_desc_port_set(struct devlink_obj_desc *desc, 264 struct devlink_port *devlink_port) 265 { 266 desc->port_index = devlink_port->index; 267 desc->port_index_valid = true; 268 } 269 270 int devlink_nl_notify_filter(struct sock *dsk, struct sk_buff *skb, void *data); 271 272 static inline void devlink_nl_notify_send_desc(struct devlink *devlink, 273 struct sk_buff *msg, 274 struct devlink_obj_desc *desc) 275 { 276 genlmsg_multicast_netns_filtered(&devlink_nl_family, 277 devlink_net(devlink), 278 msg, 0, DEVLINK_MCGRP_CONFIG, 279 GFP_KERNEL, 280 devlink_nl_notify_filter, desc); 281 } 282 283 static inline void devlink_nl_notify_send(struct devlink *devlink, 284 struct sk_buff *msg) 285 { 286 struct devlink_obj_desc desc; 287 288 devlink_nl_obj_desc_init(&desc, devlink); 289 devlink_nl_notify_send_desc(devlink, msg, &desc); 290 } 291 292 /* Notify */ 293 void devlink_notify_register(struct devlink *devlink); 294 void devlink_notify_unregister(struct devlink *devlink); 295 void devlink_ports_notify_register(struct devlink *devlink); 296 void devlink_ports_notify_unregister(struct devlink *devlink); 297 void devlink_params_notify_register(struct devlink *devlink); 298 void devlink_params_notify_unregister(struct devlink *devlink); 299 void devlink_regions_notify_register(struct devlink *devlink); 300 void devlink_regions_notify_unregister(struct devlink *devlink); 301 void devlink_trap_policers_notify_register(struct devlink *devlink); 302 void devlink_trap_policers_notify_unregister(struct devlink *devlink); 303 void devlink_trap_groups_notify_register(struct devlink *devlink); 304 void devlink_trap_groups_notify_unregister(struct devlink *devlink); 305 void devlink_traps_notify_register(struct devlink *devlink); 306 void devlink_traps_notify_unregister(struct devlink *devlink); 307 void devlink_rates_notify_register(struct devlink *devlink); 308 void devlink_rates_notify_unregister(struct devlink *devlink); 309 void devlink_linecards_notify_register(struct devlink *devlink); 310 void devlink_linecards_notify_unregister(struct devlink *devlink); 311 312 /* Ports */ 313 #define ASSERT_DEVLINK_PORT_INITIALIZED(devlink_port) \ 314 WARN_ON_ONCE(!(devlink_port)->initialized) 315 316 struct devlink_port *devlink_port_get_by_index(struct devlink *devlink, 317 unsigned int port_index); 318 int devlink_port_netdevice_event(struct notifier_block *nb, 319 unsigned long event, void *ptr); 320 struct devlink_port * 321 devlink_port_get_from_info(struct devlink *devlink, struct genl_info *info); 322 struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink, 323 struct nlattr **attrs); 324 325 /* Reload */ 326 bool devlink_reload_actions_valid(const struct devlink_ops *ops); 327 int devlink_reload(struct devlink *devlink, struct net *dest_net, 328 enum devlink_reload_action action, 329 enum devlink_reload_limit limit, 330 u32 *actions_performed, struct netlink_ext_ack *extack); 331 332 static inline bool devlink_reload_supported(const struct devlink_ops *ops) 333 { 334 return ops->reload_down && ops->reload_up; 335 } 336 337 /* Params */ 338 void devlink_params_driverinit_load_new(struct devlink *devlink); 339 340 /* Resources */ 341 struct devlink_resource; 342 int devlink_resources_validate(struct devlink *devlink, 343 struct devlink_resource *resource, 344 struct genl_info *info); 345 346 /* Rates */ 347 bool devlink_rate_is_node(const struct devlink_rate *devlink_rate); 348 int devlink_rates_check(struct devlink *devlink, 349 bool (*rate_filter)(const struct devlink_rate *), 350 struct netlink_ext_ack *extack); 351 352 /* Linecards */ 353 unsigned int devlink_linecard_index(struct devlink_linecard *linecard); 354