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 list_head param_list; 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 u64 features; 42 struct xarray snapshot_ids; 43 struct devlink_dev_stats stats; 44 struct device *dev; 45 possible_net_t _net; 46 /* Serializes access to devlink instance specific objects such as 47 * port, sb, dpipe, resource, params, region, traps and more. 48 */ 49 struct mutex lock; 50 struct lock_class_key lock_key; 51 u8 reload_failed:1; 52 refcount_t refcount; 53 struct rcu_work rwork; 54 struct notifier_block netdevice_nb; 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 #define DEVLINK_NL_FLAG_NEED_RATE BIT(2) 96 #define DEVLINK_NL_FLAG_NEED_RATE_NODE BIT(3) 97 #define DEVLINK_NL_FLAG_NEED_LINECARD BIT(4) 98 99 enum devlink_multicast_groups { 100 DEVLINK_MCGRP_CONFIG, 101 }; 102 103 /* state held across netlink dumps */ 104 struct devlink_nl_dump_state { 105 unsigned long instance; 106 int idx; 107 union { 108 /* DEVLINK_CMD_REGION_READ */ 109 struct { 110 u64 start_offset; 111 }; 112 /* DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET */ 113 struct { 114 u64 dump_ts; 115 }; 116 }; 117 }; 118 119 struct devlink_gen_cmd { 120 int (*dump_one)(struct sk_buff *msg, struct devlink *devlink, 121 struct netlink_callback *cb); 122 }; 123 124 extern const struct genl_small_ops devlink_nl_ops[56]; 125 126 struct devlink * 127 devlink_get_from_attrs_lock(struct net *net, struct nlattr **attrs); 128 129 void devlink_notify_unregister(struct devlink *devlink); 130 void devlink_notify_register(struct devlink *devlink); 131 132 int devlink_nl_instance_iter_dump(struct sk_buff *msg, 133 struct netlink_callback *cb); 134 135 static inline struct devlink_nl_dump_state * 136 devlink_dump_state(struct netlink_callback *cb) 137 { 138 NL_ASSET_DUMP_CTX_FITS(struct devlink_nl_dump_state); 139 140 return (struct devlink_nl_dump_state *)cb->ctx; 141 } 142 143 /* gen cmds */ 144 extern const struct devlink_gen_cmd devl_gen_inst; 145 extern const struct devlink_gen_cmd devl_gen_port; 146 extern const struct devlink_gen_cmd devl_gen_sb; 147 extern const struct devlink_gen_cmd devl_gen_sb_pool; 148 extern const struct devlink_gen_cmd devl_gen_sb_port_pool; 149 extern const struct devlink_gen_cmd devl_gen_sb_tc_pool_bind; 150 extern const struct devlink_gen_cmd devl_gen_selftests; 151 extern const struct devlink_gen_cmd devl_gen_param; 152 extern const struct devlink_gen_cmd devl_gen_region; 153 extern const struct devlink_gen_cmd devl_gen_info; 154 extern const struct devlink_gen_cmd devl_gen_health_reporter; 155 extern const struct devlink_gen_cmd devl_gen_trap; 156 extern const struct devlink_gen_cmd devl_gen_trap_group; 157 extern const struct devlink_gen_cmd devl_gen_trap_policer; 158 extern const struct devlink_gen_cmd devl_gen_linecard; 159 160 /* Ports */ 161 int devlink_port_netdevice_event(struct notifier_block *nb, 162 unsigned long event, void *ptr); 163 164 struct devlink_port * 165 devlink_port_get_from_info(struct devlink *devlink, struct genl_info *info); 166 167 /* Reload */ 168 bool devlink_reload_actions_valid(const struct devlink_ops *ops); 169 int devlink_reload(struct devlink *devlink, struct net *dest_net, 170 enum devlink_reload_action action, 171 enum devlink_reload_limit limit, 172 u32 *actions_performed, struct netlink_ext_ack *extack); 173 174 static inline bool devlink_reload_supported(const struct devlink_ops *ops) 175 { 176 return ops->reload_down && ops->reload_up; 177 } 178 179 /* Line cards */ 180 struct devlink_linecard; 181 182 struct devlink_linecard * 183 devlink_linecard_get_from_info(struct devlink *devlink, struct genl_info *info); 184 185 /* Rates */ 186 extern const struct devlink_gen_cmd devl_gen_rate_get; 187 188 struct devlink_rate * 189 devlink_rate_get_from_info(struct devlink *devlink, struct genl_info *info); 190 struct devlink_rate * 191 devlink_rate_node_get_from_info(struct devlink *devlink, 192 struct genl_info *info); 193