xref: /openbmc/linux/include/net/devlink.h (revision bc5aa3a0)
1 /*
2  * include/net/devlink.h - Network physical device Netlink interface
3  * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
4  * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  */
11 #ifndef _NET_DEVLINK_H_
12 #define _NET_DEVLINK_H_
13 
14 #include <linux/device.h>
15 #include <linux/slab.h>
16 #include <linux/gfp.h>
17 #include <linux/list.h>
18 #include <linux/netdevice.h>
19 #include <net/net_namespace.h>
20 #include <uapi/linux/devlink.h>
21 
22 struct devlink_ops;
23 
24 struct devlink {
25 	struct list_head list;
26 	struct list_head port_list;
27 	struct list_head sb_list;
28 	const struct devlink_ops *ops;
29 	struct device *dev;
30 	possible_net_t _net;
31 	char priv[0] __aligned(NETDEV_ALIGN);
32 };
33 
34 struct devlink_port {
35 	struct list_head list;
36 	struct devlink *devlink;
37 	unsigned index;
38 	bool registered;
39 	enum devlink_port_type type;
40 	enum devlink_port_type desired_type;
41 	void *type_dev;
42 	bool split;
43 	u32 split_group;
44 };
45 
46 struct devlink_sb_pool_info {
47 	enum devlink_sb_pool_type pool_type;
48 	u32 size;
49 	enum devlink_sb_threshold_type threshold_type;
50 };
51 
52 struct devlink_ops {
53 	size_t priv_size;
54 	int (*port_type_set)(struct devlink_port *devlink_port,
55 			     enum devlink_port_type port_type);
56 	int (*port_split)(struct devlink *devlink, unsigned int port_index,
57 			  unsigned int count);
58 	int (*port_unsplit)(struct devlink *devlink, unsigned int port_index);
59 	int (*sb_pool_get)(struct devlink *devlink, unsigned int sb_index,
60 			   u16 pool_index,
61 			   struct devlink_sb_pool_info *pool_info);
62 	int (*sb_pool_set)(struct devlink *devlink, unsigned int sb_index,
63 			   u16 pool_index, u32 size,
64 			   enum devlink_sb_threshold_type threshold_type);
65 	int (*sb_port_pool_get)(struct devlink_port *devlink_port,
66 				unsigned int sb_index, u16 pool_index,
67 				u32 *p_threshold);
68 	int (*sb_port_pool_set)(struct devlink_port *devlink_port,
69 				unsigned int sb_index, u16 pool_index,
70 				u32 threshold);
71 	int (*sb_tc_pool_bind_get)(struct devlink_port *devlink_port,
72 				   unsigned int sb_index,
73 				   u16 tc_index,
74 				   enum devlink_sb_pool_type pool_type,
75 				   u16 *p_pool_index, u32 *p_threshold);
76 	int (*sb_tc_pool_bind_set)(struct devlink_port *devlink_port,
77 				   unsigned int sb_index,
78 				   u16 tc_index,
79 				   enum devlink_sb_pool_type pool_type,
80 				   u16 pool_index, u32 threshold);
81 	int (*sb_occ_snapshot)(struct devlink *devlink,
82 			       unsigned int sb_index);
83 	int (*sb_occ_max_clear)(struct devlink *devlink,
84 				unsigned int sb_index);
85 	int (*sb_occ_port_pool_get)(struct devlink_port *devlink_port,
86 				    unsigned int sb_index, u16 pool_index,
87 				    u32 *p_cur, u32 *p_max);
88 	int (*sb_occ_tc_port_bind_get)(struct devlink_port *devlink_port,
89 				       unsigned int sb_index,
90 				       u16 tc_index,
91 				       enum devlink_sb_pool_type pool_type,
92 				       u32 *p_cur, u32 *p_max);
93 
94 	int (*eswitch_mode_get)(struct devlink *devlink, u16 *p_mode);
95 	int (*eswitch_mode_set)(struct devlink *devlink, u16 mode);
96 };
97 
98 static inline void *devlink_priv(struct devlink *devlink)
99 {
100 	BUG_ON(!devlink);
101 	return &devlink->priv;
102 }
103 
104 static inline struct devlink *priv_to_devlink(void *priv)
105 {
106 	BUG_ON(!priv);
107 	return container_of(priv, struct devlink, priv);
108 }
109 
110 struct ib_device;
111 
112 #if IS_ENABLED(CONFIG_NET_DEVLINK)
113 
114 struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size);
115 int devlink_register(struct devlink *devlink, struct device *dev);
116 void devlink_unregister(struct devlink *devlink);
117 void devlink_free(struct devlink *devlink);
118 int devlink_port_register(struct devlink *devlink,
119 			  struct devlink_port *devlink_port,
120 			  unsigned int port_index);
121 void devlink_port_unregister(struct devlink_port *devlink_port);
122 void devlink_port_type_eth_set(struct devlink_port *devlink_port,
123 			       struct net_device *netdev);
124 void devlink_port_type_ib_set(struct devlink_port *devlink_port,
125 			      struct ib_device *ibdev);
126 void devlink_port_type_clear(struct devlink_port *devlink_port);
127 void devlink_port_split_set(struct devlink_port *devlink_port,
128 			    u32 split_group);
129 int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
130 			u32 size, u16 ingress_pools_count,
131 			u16 egress_pools_count, u16 ingress_tc_count,
132 			u16 egress_tc_count);
133 void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index);
134 
135 #else
136 
137 static inline struct devlink *devlink_alloc(const struct devlink_ops *ops,
138 					    size_t priv_size)
139 {
140 	return kzalloc(sizeof(struct devlink) + priv_size, GFP_KERNEL);
141 }
142 
143 static inline int devlink_register(struct devlink *devlink, struct device *dev)
144 {
145 	return 0;
146 }
147 
148 static inline void devlink_unregister(struct devlink *devlink)
149 {
150 }
151 
152 static inline void devlink_free(struct devlink *devlink)
153 {
154 	kfree(devlink);
155 }
156 
157 static inline int devlink_port_register(struct devlink *devlink,
158 					struct devlink_port *devlink_port,
159 					unsigned int port_index)
160 {
161 	return 0;
162 }
163 
164 static inline void devlink_port_unregister(struct devlink_port *devlink_port)
165 {
166 }
167 
168 static inline void devlink_port_type_eth_set(struct devlink_port *devlink_port,
169 					     struct net_device *netdev)
170 {
171 }
172 
173 static inline void devlink_port_type_ib_set(struct devlink_port *devlink_port,
174 					    struct ib_device *ibdev)
175 {
176 }
177 
178 static inline void devlink_port_type_clear(struct devlink_port *devlink_port)
179 {
180 }
181 
182 static inline void devlink_port_split_set(struct devlink_port *devlink_port,
183 					  u32 split_group)
184 {
185 }
186 
187 static inline int devlink_sb_register(struct devlink *devlink,
188 				      unsigned int sb_index, u32 size,
189 				      u16 ingress_pools_count,
190 				      u16 egress_pools_count,
191 				      u16 ingress_tc_count,
192 				      u16 egress_tc_count)
193 {
194 	return 0;
195 }
196 
197 static inline void devlink_sb_unregister(struct devlink *devlink,
198 					 unsigned int sb_index)
199 {
200 }
201 
202 #endif
203 
204 #endif /* _NET_DEVLINK_H_ */
205