1 // SPDX-License-Identifier: GPL-2.0-or-later
7 #include <net/devlink.h>
11 static int dsa_devlink_info_get(struct devlink *dl,
12 struct devlink_info_req *req,
13 struct netlink_ext_ack *extack)
15 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
17 if (ds->ops->devlink_info_get)
18 return ds->ops->devlink_info_get(ds, req, extack);
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)
27 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
29 if (!ds->ops->devlink_sb_pool_get)
32 return ds->ops->devlink_sb_pool_get(ds, sb_index, pool_index,
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)
41 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
43 if (!ds->ops->devlink_sb_pool_set)
46 return ds->ops->devlink_sb_pool_set(ds, sb_index, pool_index, size,
47 threshold_type, extack);
50 static int dsa_devlink_sb_port_pool_get(struct devlink_port *dlp,
51 unsigned int sb_index, u16 pool_index,
54 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
55 int port = dsa_devlink_port_to_port(dlp);
57 if (!ds->ops->devlink_sb_port_pool_get)
60 return ds->ops->devlink_sb_port_pool_get(ds, port, sb_index,
61 pool_index, p_threshold);
64 static int dsa_devlink_sb_port_pool_set(struct devlink_port *dlp,
65 unsigned int sb_index, u16 pool_index,
67 struct netlink_ext_ack *extack)
69 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
70 int port = dsa_devlink_port_to_port(dlp);
72 if (!ds->ops->devlink_sb_port_pool_set)
75 return ds->ops->devlink_sb_port_pool_set(ds, port, sb_index,
76 pool_index, threshold, extack);
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)
85 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
86 int port = dsa_devlink_port_to_port(dlp);
88 if (!ds->ops->devlink_sb_tc_pool_bind_get)
91 return ds->ops->devlink_sb_tc_pool_bind_get(ds, port, sb_index,
93 p_pool_index, p_threshold);
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)
103 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
104 int port = dsa_devlink_port_to_port(dlp);
106 if (!ds->ops->devlink_sb_tc_pool_bind_set)
109 return ds->ops->devlink_sb_tc_pool_bind_set(ds, port, sb_index,
111 pool_index, threshold,
115 static int dsa_devlink_sb_occ_snapshot(struct devlink *dl,
116 unsigned int sb_index)
118 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
120 if (!ds->ops->devlink_sb_occ_snapshot)
123 return ds->ops->devlink_sb_occ_snapshot(ds, sb_index);
126 static int dsa_devlink_sb_occ_max_clear(struct devlink *dl,
127 unsigned int sb_index)
129 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
131 if (!ds->ops->devlink_sb_occ_max_clear)
134 return ds->ops->devlink_sb_occ_max_clear(ds, sb_index);
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,
142 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
143 int port = dsa_devlink_port_to_port(dlp);
145 if (!ds->ops->devlink_sb_occ_port_pool_get)
148 return ds->ops->devlink_sb_occ_port_pool_get(ds, port, sb_index,
149 pool_index, p_cur, 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)
158 struct dsa_switch *ds = dsa_devlink_port_to_ds(dlp);
159 int port = dsa_devlink_port_to_port(dlp);
161 if (!ds->ops->devlink_sb_occ_tc_port_bind_get)
164 return ds->ops->devlink_sb_occ_tc_port_bind_get(ds, port,
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,
184 int dsa_devlink_param_get(struct devlink *dl, u32 id,
185 struct devlink_param_gset_ctx *ctx)
187 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
189 if (!ds->ops->devlink_param_get)
192 return ds->ops->devlink_param_get(ds, id, ctx);
194 EXPORT_SYMBOL_GPL(dsa_devlink_param_get);
196 int dsa_devlink_param_set(struct devlink *dl, u32 id,
197 struct devlink_param_gset_ctx *ctx)
199 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
201 if (!ds->ops->devlink_param_set)
204 return ds->ops->devlink_param_set(ds, id, ctx);
206 EXPORT_SYMBOL_GPL(dsa_devlink_param_set);
208 int dsa_devlink_params_register(struct dsa_switch *ds,
209 const struct devlink_param *params,
212 return devlink_params_register(ds->devlink, params, params_count);
214 EXPORT_SYMBOL_GPL(dsa_devlink_params_register);
216 void dsa_devlink_params_unregister(struct dsa_switch *ds,
217 const struct devlink_param *params,
220 devlink_params_unregister(ds->devlink, params, params_count);
222 EXPORT_SYMBOL_GPL(dsa_devlink_params_unregister);
224 int dsa_devlink_resource_register(struct dsa_switch *ds,
225 const char *resource_name,
228 u64 parent_resource_id,
229 const struct devlink_resource_size_params *size_params)
231 return devlink_resource_register(ds->devlink, resource_name,
232 resource_size, resource_id,
236 EXPORT_SYMBOL_GPL(dsa_devlink_resource_register);
238 void dsa_devlink_resources_unregister(struct dsa_switch *ds)
240 devlink_resources_unregister(ds->devlink);
242 EXPORT_SYMBOL_GPL(dsa_devlink_resources_unregister);
244 void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds,
246 devlink_resource_occ_get_t *occ_get,
249 return devlink_resource_occ_get_register(ds->devlink, resource_id,
250 occ_get, occ_get_priv);
252 EXPORT_SYMBOL_GPL(dsa_devlink_resource_occ_get_register);
254 void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds,
257 devlink_resource_occ_get_unregister(ds->devlink, resource_id);
259 EXPORT_SYMBOL_GPL(dsa_devlink_resource_occ_get_unregister);
261 struct devlink_region *
262 dsa_devlink_region_create(struct dsa_switch *ds,
263 const struct devlink_region_ops *ops,
264 u32 region_max_snapshots, u64 region_size)
266 return devlink_region_create(ds->devlink, ops, region_max_snapshots,
269 EXPORT_SYMBOL_GPL(dsa_devlink_region_create);
271 struct devlink_region *
272 dsa_devlink_port_region_create(struct dsa_switch *ds,
274 const struct devlink_port_region_ops *ops,
275 u32 region_max_snapshots, u64 region_size)
277 struct dsa_port *dp = dsa_to_port(ds, port);
279 return devlink_port_region_create(&dp->devlink_port, ops,
280 region_max_snapshots,
283 EXPORT_SYMBOL_GPL(dsa_devlink_port_region_create);
285 void dsa_devlink_region_destroy(struct devlink_region *region)
287 devlink_region_destroy(region);
289 EXPORT_SYMBOL_GPL(dsa_devlink_region_destroy);
291 int dsa_port_devlink_setup(struct dsa_port *dp)
293 struct devlink_port *dlp = &dp->devlink_port;
294 struct dsa_switch_tree *dst = dp->ds->dst;
295 struct devlink_port_attrs attrs = {};
296 struct devlink *dl = dp->ds->devlink;
297 struct dsa_switch *ds = dp->ds;
298 const unsigned char *id;
302 memset(dlp, 0, sizeof(*dlp));
303 devlink_port_init(dl, dlp);
305 if (ds->ops->port_setup) {
306 err = ds->ops->port_setup(ds, dp->index);
311 id = (const unsigned char *)&dst->index;
312 len = sizeof(dst->index);
314 attrs.phys.port_number = dp->index;
315 memcpy(attrs.switch_id.id, id, len);
316 attrs.switch_id.id_len = len;
319 case DSA_PORT_TYPE_UNUSED:
320 attrs.flavour = DEVLINK_PORT_FLAVOUR_UNUSED;
322 case DSA_PORT_TYPE_CPU:
323 attrs.flavour = DEVLINK_PORT_FLAVOUR_CPU;
325 case DSA_PORT_TYPE_DSA:
326 attrs.flavour = DEVLINK_PORT_FLAVOUR_DSA;
328 case DSA_PORT_TYPE_USER:
329 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
333 devlink_port_attrs_set(dlp, &attrs);
334 err = devlink_port_register(dl, dlp, dp->index);
336 if (ds->ops->port_teardown)
337 ds->ops->port_teardown(ds, dp->index);
344 void dsa_port_devlink_teardown(struct dsa_port *dp)
346 struct devlink_port *dlp = &dp->devlink_port;
347 struct dsa_switch *ds = dp->ds;
349 devlink_port_unregister(dlp);
351 if (ds->ops->port_teardown)
352 ds->ops->port_teardown(ds, dp->index);
354 devlink_port_fini(dlp);
357 void dsa_switch_devlink_register(struct dsa_switch *ds)
359 devlink_register(ds->devlink);
362 void dsa_switch_devlink_unregister(struct dsa_switch *ds)
364 devlink_unregister(ds->devlink);
367 int dsa_switch_devlink_alloc(struct dsa_switch *ds)
369 struct dsa_devlink_priv *dl_priv;
372 /* Add the switch to devlink before calling setup, so that setup can
375 dl = devlink_alloc(&dsa_devlink_ops, sizeof(*dl_priv), ds->dev);
381 dl_priv = devlink_priv(ds->devlink);
387 void dsa_switch_devlink_free(struct dsa_switch *ds)
389 devlink_free(ds->devlink);