1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * DSA devlink handling
4 */
5
6 #include <net/dsa.h>
7 #include <net/devlink.h>
8
9 #include "devlink.h"
10
dsa_devlink_info_get(struct devlink * dl,struct devlink_info_req * req,struct netlink_ext_ack * extack)11 static int dsa_devlink_info_get(struct devlink *dl,
12 struct devlink_info_req *req,
13 struct netlink_ext_ack *extack)
14 {
15 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
16
17 if (ds->ops->devlink_info_get)
18 return ds->ops->devlink_info_get(ds, req, extack);
19
20 return -EOPNOTSUPP;
21 }
22
dsa_devlink_sb_pool_get(struct devlink * dl,unsigned int sb_index,u16 pool_index,struct devlink_sb_pool_info * pool_info)23 static int dsa_devlink_sb_pool_get(struct devlink *dl,
24 unsigned int sb_index, u16 pool_index,
25 struct devlink_sb_pool_info *pool_info)
26 {
27 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
28
29 if (!ds->ops->devlink_sb_pool_get)
30 return -EOPNOTSUPP;
31
32 return ds->ops->devlink_sb_pool_get(ds, sb_index, pool_index,
33 pool_info);
34 }
35
dsa_devlink_sb_pool_set(struct devlink * dl,unsigned int sb_index,u16 pool_index,u32 size,enum devlink_sb_threshold_type threshold_type,struct netlink_ext_ack * extack)36 static int dsa_devlink_sb_pool_set(struct devlink *dl, unsigned int sb_index,
37 u16 pool_index, u32 size,
38 enum devlink_sb_threshold_type threshold_type,
39 struct netlink_ext_ack *extack)
40 {
41 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
42
43 if (!ds->ops->devlink_sb_pool_set)
44 return -EOPNOTSUPP;
45
46 return ds->ops->devlink_sb_pool_set(ds, sb_index, pool_index, size,
47 threshold_type, extack);
48 }
49
dsa_devlink_sb_port_pool_get(struct devlink_port * dlp,unsigned int sb_index,u16 pool_index,u32 * p_threshold)50 static int dsa_devlink_sb_port_pool_get(struct devlink_port *dlp,
51 unsigned int sb_index, u16 pool_index,
52 u32 *p_threshold)
53 {
54 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
55 int port = dsa_devlink_port_to_port(dlp);
56
57 if (!ds->ops->devlink_sb_port_pool_get)
58 return -EOPNOTSUPP;
59
60 return ds->ops->devlink_sb_port_pool_get(ds, port, sb_index,
61 pool_index, p_threshold);
62 }
63
dsa_devlink_sb_port_pool_set(struct devlink_port * dlp,unsigned int sb_index,u16 pool_index,u32 threshold,struct netlink_ext_ack * extack)64 static int dsa_devlink_sb_port_pool_set(struct devlink_port *dlp,
65 unsigned int sb_index, u16 pool_index,
66 u32 threshold,
67 struct netlink_ext_ack *extack)
68 {
69 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
70 int port = dsa_devlink_port_to_port(dlp);
71
72 if (!ds->ops->devlink_sb_port_pool_set)
73 return -EOPNOTSUPP;
74
75 return ds->ops->devlink_sb_port_pool_set(ds, port, sb_index,
76 pool_index, threshold, extack);
77 }
78
79 static int
dsa_devlink_sb_tc_pool_bind_get(struct devlink_port * dlp,unsigned int sb_index,u16 tc_index,enum devlink_sb_pool_type pool_type,u16 * p_pool_index,u32 * p_threshold)80 dsa_devlink_sb_tc_pool_bind_get(struct devlink_port *dlp,
81 unsigned int sb_index, u16 tc_index,
82 enum devlink_sb_pool_type pool_type,
83 u16 *p_pool_index, u32 *p_threshold)
84 {
85 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
86 int port = dsa_devlink_port_to_port(dlp);
87
88 if (!ds->ops->devlink_sb_tc_pool_bind_get)
89 return -EOPNOTSUPP;
90
91 return ds->ops->devlink_sb_tc_pool_bind_get(ds, port, sb_index,
92 tc_index, pool_type,
93 p_pool_index, p_threshold);
94 }
95
96 static int
dsa_devlink_sb_tc_pool_bind_set(struct devlink_port * dlp,unsigned int sb_index,u16 tc_index,enum devlink_sb_pool_type pool_type,u16 pool_index,u32 threshold,struct netlink_ext_ack * extack)97 dsa_devlink_sb_tc_pool_bind_set(struct devlink_port *dlp,
98 unsigned int sb_index, u16 tc_index,
99 enum devlink_sb_pool_type pool_type,
100 u16 pool_index, u32 threshold,
101 struct netlink_ext_ack *extack)
102 {
103 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
104 int port = dsa_devlink_port_to_port(dlp);
105
106 if (!ds->ops->devlink_sb_tc_pool_bind_set)
107 return -EOPNOTSUPP;
108
109 return ds->ops->devlink_sb_tc_pool_bind_set(ds, port, sb_index,
110 tc_index, pool_type,
111 pool_index, threshold,
112 extack);
113 }
114
dsa_devlink_sb_occ_snapshot(struct devlink * dl,unsigned int sb_index)115 static int dsa_devlink_sb_occ_snapshot(struct devlink *dl,
116 unsigned int sb_index)
117 {
118 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
119
120 if (!ds->ops->devlink_sb_occ_snapshot)
121 return -EOPNOTSUPP;
122
123 return ds->ops->devlink_sb_occ_snapshot(ds, sb_index);
124 }
125
dsa_devlink_sb_occ_max_clear(struct devlink * dl,unsigned int sb_index)126 static int dsa_devlink_sb_occ_max_clear(struct devlink *dl,
127 unsigned int sb_index)
128 {
129 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
130
131 if (!ds->ops->devlink_sb_occ_max_clear)
132 return -EOPNOTSUPP;
133
134 return ds->ops->devlink_sb_occ_max_clear(ds, sb_index);
135 }
136
dsa_devlink_sb_occ_port_pool_get(struct devlink_port * dlp,unsigned int sb_index,u16 pool_index,u32 * p_cur,u32 * p_max)137 static int dsa_devlink_sb_occ_port_pool_get(struct devlink_port *dlp,
138 unsigned int sb_index,
139 u16 pool_index, u32 *p_cur,
140 u32 *p_max)
141 {
142 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
143 int port = dsa_devlink_port_to_port(dlp);
144
145 if (!ds->ops->devlink_sb_occ_port_pool_get)
146 return -EOPNOTSUPP;
147
148 return ds->ops->devlink_sb_occ_port_pool_get(ds, port, sb_index,
149 pool_index, p_cur, p_max);
150 }
151
152 static int
dsa_devlink_sb_occ_tc_port_bind_get(struct devlink_port * dlp,unsigned int sb_index,u16 tc_index,enum devlink_sb_pool_type pool_type,u32 * p_cur,u32 * p_max)153 dsa_devlink_sb_occ_tc_port_bind_get(struct devlink_port *dlp,
154 unsigned int sb_index, u16 tc_index,
155 enum devlink_sb_pool_type pool_type,
156 u32 *p_cur, u32 *p_max)
157 {
158 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
159 int port = dsa_devlink_port_to_port(dlp);
160
161 if (!ds->ops->devlink_sb_occ_tc_port_bind_get)
162 return -EOPNOTSUPP;
163
164 return ds->ops->devlink_sb_occ_tc_port_bind_get(ds, port,
165 sb_index, tc_index,
166 pool_type, p_cur,
167 p_max);
168 }
169
170 static const struct devlink_ops dsa_devlink_ops = {
171 .info_get = dsa_devlink_info_get,
172 .sb_pool_get = dsa_devlink_sb_pool_get,
173 .sb_pool_set = dsa_devlink_sb_pool_set,
174 .sb_port_pool_get = dsa_devlink_sb_port_pool_get,
175 .sb_port_pool_set = dsa_devlink_sb_port_pool_set,
176 .sb_tc_pool_bind_get = dsa_devlink_sb_tc_pool_bind_get,
177 .sb_tc_pool_bind_set = dsa_devlink_sb_tc_pool_bind_set,
178 .sb_occ_snapshot = dsa_devlink_sb_occ_snapshot,
179 .sb_occ_max_clear = dsa_devlink_sb_occ_max_clear,
180 .sb_occ_port_pool_get = dsa_devlink_sb_occ_port_pool_get,
181 .sb_occ_tc_port_bind_get = dsa_devlink_sb_occ_tc_port_bind_get,
182 };
183
dsa_devlink_param_get(struct devlink * dl,u32 id,struct devlink_param_gset_ctx * ctx)184 int dsa_devlink_param_get(struct devlink *dl, u32 id,
185 struct devlink_param_gset_ctx *ctx)
186 {
187 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
188
189 if (!ds->ops->devlink_param_get)
190 return -EOPNOTSUPP;
191
192 return ds->ops->devlink_param_get(ds, id, ctx);
193 }
194 EXPORT_SYMBOL_GPL(dsa_devlink_param_get);
195
dsa_devlink_param_set(struct devlink * dl,u32 id,struct devlink_param_gset_ctx * ctx,struct netlink_ext_ack * extack)196 int dsa_devlink_param_set(struct devlink *dl, u32 id,
197 struct devlink_param_gset_ctx *ctx,
198 struct netlink_ext_ack *extack)
199 {
200 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
201
202 if (!ds->ops->devlink_param_set)
203 return -EOPNOTSUPP;
204
205 return ds->ops->devlink_param_set(ds, id, ctx);
206 }
207 EXPORT_SYMBOL_GPL(dsa_devlink_param_set);
208
dsa_devlink_params_register(struct dsa_switch * ds,const struct devlink_param * params,size_t params_count)209 int dsa_devlink_params_register(struct dsa_switch *ds,
210 const struct devlink_param *params,
211 size_t params_count)
212 {
213 return devlink_params_register(ds->devlink, params, params_count);
214 }
215 EXPORT_SYMBOL_GPL(dsa_devlink_params_register);
216
dsa_devlink_params_unregister(struct dsa_switch * ds,const struct devlink_param * params,size_t params_count)217 void dsa_devlink_params_unregister(struct dsa_switch *ds,
218 const struct devlink_param *params,
219 size_t params_count)
220 {
221 devlink_params_unregister(ds->devlink, params, params_count);
222 }
223 EXPORT_SYMBOL_GPL(dsa_devlink_params_unregister);
224
dsa_devlink_resource_register(struct dsa_switch * ds,const char * resource_name,u64 resource_size,u64 resource_id,u64 parent_resource_id,const struct devlink_resource_size_params * size_params)225 int dsa_devlink_resource_register(struct dsa_switch *ds,
226 const char *resource_name,
227 u64 resource_size,
228 u64 resource_id,
229 u64 parent_resource_id,
230 const struct devlink_resource_size_params *size_params)
231 {
232 return devlink_resource_register(ds->devlink, resource_name,
233 resource_size, resource_id,
234 parent_resource_id,
235 size_params);
236 }
237 EXPORT_SYMBOL_GPL(dsa_devlink_resource_register);
238
dsa_devlink_resources_unregister(struct dsa_switch * ds)239 void dsa_devlink_resources_unregister(struct dsa_switch *ds)
240 {
241 devlink_resources_unregister(ds->devlink);
242 }
243 EXPORT_SYMBOL_GPL(dsa_devlink_resources_unregister);
244
dsa_devlink_resource_occ_get_register(struct dsa_switch * ds,u64 resource_id,devlink_resource_occ_get_t * occ_get,void * occ_get_priv)245 void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds,
246 u64 resource_id,
247 devlink_resource_occ_get_t *occ_get,
248 void *occ_get_priv)
249 {
250 return devlink_resource_occ_get_register(ds->devlink, resource_id,
251 occ_get, occ_get_priv);
252 }
253 EXPORT_SYMBOL_GPL(dsa_devlink_resource_occ_get_register);
254
dsa_devlink_resource_occ_get_unregister(struct dsa_switch * ds,u64 resource_id)255 void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds,
256 u64 resource_id)
257 {
258 devlink_resource_occ_get_unregister(ds->devlink, resource_id);
259 }
260 EXPORT_SYMBOL_GPL(dsa_devlink_resource_occ_get_unregister);
261
262 struct devlink_region *
dsa_devlink_region_create(struct dsa_switch * ds,const struct devlink_region_ops * ops,u32 region_max_snapshots,u64 region_size)263 dsa_devlink_region_create(struct dsa_switch *ds,
264 const struct devlink_region_ops *ops,
265 u32 region_max_snapshots, u64 region_size)
266 {
267 return devlink_region_create(ds->devlink, ops, region_max_snapshots,
268 region_size);
269 }
270 EXPORT_SYMBOL_GPL(dsa_devlink_region_create);
271
272 struct devlink_region *
dsa_devlink_port_region_create(struct dsa_switch * ds,int port,const struct devlink_port_region_ops * ops,u32 region_max_snapshots,u64 region_size)273 dsa_devlink_port_region_create(struct dsa_switch *ds,
274 int port,
275 const struct devlink_port_region_ops *ops,
276 u32 region_max_snapshots, u64 region_size)
277 {
278 struct dsa_port *dp = dsa_to_port(ds, port);
279
280 return devlink_port_region_create(&dp->devlink_port, ops,
281 region_max_snapshots,
282 region_size);
283 }
284 EXPORT_SYMBOL_GPL(dsa_devlink_port_region_create);
285
dsa_devlink_region_destroy(struct devlink_region * region)286 void dsa_devlink_region_destroy(struct devlink_region *region)
287 {
288 devlink_region_destroy(region);
289 }
290 EXPORT_SYMBOL_GPL(dsa_devlink_region_destroy);
291
dsa_port_devlink_setup(struct dsa_port * dp)292 int dsa_port_devlink_setup(struct dsa_port *dp)
293 {
294 struct devlink_port *dlp = &dp->devlink_port;
295 struct dsa_switch_tree *dst = dp->ds->dst;
296 struct devlink_port_attrs attrs = {};
297 struct devlink *dl = dp->ds->devlink;
298 struct dsa_switch *ds = dp->ds;
299 const unsigned char *id;
300 unsigned char len;
301 int err;
302
303 memset(dlp, 0, sizeof(*dlp));
304 devlink_port_init(dl, dlp);
305
306 if (ds->ops->port_setup) {
307 err = ds->ops->port_setup(ds, dp->index);
308 if (err)
309 return err;
310 }
311
312 id = (const unsigned char *)&dst->index;
313 len = sizeof(dst->index);
314
315 attrs.phys.port_number = dp->index;
316 memcpy(attrs.switch_id.id, id, len);
317 attrs.switch_id.id_len = len;
318
319 switch (dp->type) {
320 case DSA_PORT_TYPE_UNUSED:
321 attrs.flavour = DEVLINK_PORT_FLAVOUR_UNUSED;
322 break;
323 case DSA_PORT_TYPE_CPU:
324 attrs.flavour = DEVLINK_PORT_FLAVOUR_CPU;
325 break;
326 case DSA_PORT_TYPE_DSA:
327 attrs.flavour = DEVLINK_PORT_FLAVOUR_DSA;
328 break;
329 case DSA_PORT_TYPE_USER:
330 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
331 break;
332 }
333
334 devlink_port_attrs_set(dlp, &attrs);
335 err = devlink_port_register(dl, dlp, dp->index);
336 if (err) {
337 if (ds->ops->port_teardown)
338 ds->ops->port_teardown(ds, dp->index);
339 return err;
340 }
341
342 return 0;
343 }
344
dsa_port_devlink_teardown(struct dsa_port * dp)345 void dsa_port_devlink_teardown(struct dsa_port *dp)
346 {
347 struct devlink_port *dlp = &dp->devlink_port;
348 struct dsa_switch *ds = dp->ds;
349
350 devlink_port_unregister(dlp);
351
352 if (ds->ops->port_teardown)
353 ds->ops->port_teardown(ds, dp->index);
354
355 devlink_port_fini(dlp);
356 }
357
dsa_switch_devlink_register(struct dsa_switch * ds)358 void dsa_switch_devlink_register(struct dsa_switch *ds)
359 {
360 devlink_register(ds->devlink);
361 }
362
dsa_switch_devlink_unregister(struct dsa_switch * ds)363 void dsa_switch_devlink_unregister(struct dsa_switch *ds)
364 {
365 devlink_unregister(ds->devlink);
366 }
367
dsa_switch_devlink_alloc(struct dsa_switch * ds)368 int dsa_switch_devlink_alloc(struct dsa_switch *ds)
369 {
370 struct dsa_devlink_priv *dl_priv;
371 struct devlink *dl;
372
373 /* Add the switch to devlink before calling setup, so that setup can
374 * add dpipe tables
375 */
376 dl = devlink_alloc(&dsa_devlink_ops, sizeof(*dl_priv), ds->dev);
377 if (!dl)
378 return -ENOMEM;
379
380 ds->devlink = dl;
381
382 dl_priv = devlink_priv(ds->devlink);
383 dl_priv->ds = ds;
384
385 return 0;
386 }
387
dsa_switch_devlink_free(struct dsa_switch * ds)388 void dsa_switch_devlink_free(struct dsa_switch *ds)
389 {
390 devlink_free(ds->devlink);
391 ds->devlink = NULL;
392 }
393