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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 int ret; 233 234 devl_lock(ds->devlink); 235 ret = devl_resource_register(ds->devlink, resource_name, resource_size, 236 resource_id, parent_resource_id, 237 size_params); 238 devl_unlock(ds->devlink); 239 240 return ret; 241 } 242 EXPORT_SYMBOL_GPL(dsa_devlink_resource_register); 243 244 void dsa_devlink_resources_unregister(struct dsa_switch *ds) 245 { 246 devlink_resources_unregister(ds->devlink); 247 } 248 EXPORT_SYMBOL_GPL(dsa_devlink_resources_unregister); 249 250 void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds, 251 u64 resource_id, 252 devlink_resource_occ_get_t *occ_get, 253 void *occ_get_priv) 254 { 255 devl_lock(ds->devlink); 256 devl_resource_occ_get_register(ds->devlink, resource_id, occ_get, 257 occ_get_priv); 258 devl_unlock(ds->devlink); 259 } 260 EXPORT_SYMBOL_GPL(dsa_devlink_resource_occ_get_register); 261 262 void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds, 263 u64 resource_id) 264 { 265 devl_lock(ds->devlink); 266 devl_resource_occ_get_unregister(ds->devlink, resource_id); 267 devl_unlock(ds->devlink); 268 } 269 EXPORT_SYMBOL_GPL(dsa_devlink_resource_occ_get_unregister); 270 271 struct devlink_region * 272 dsa_devlink_region_create(struct dsa_switch *ds, 273 const struct devlink_region_ops *ops, 274 u32 region_max_snapshots, u64 region_size) 275 { 276 return devlink_region_create(ds->devlink, ops, region_max_snapshots, 277 region_size); 278 } 279 EXPORT_SYMBOL_GPL(dsa_devlink_region_create); 280 281 struct devlink_region * 282 dsa_devlink_port_region_create(struct dsa_switch *ds, 283 int port, 284 const struct devlink_port_region_ops *ops, 285 u32 region_max_snapshots, u64 region_size) 286 { 287 struct dsa_port *dp = dsa_to_port(ds, port); 288 289 return devlink_port_region_create(&dp->devlink_port, ops, 290 region_max_snapshots, 291 region_size); 292 } 293 EXPORT_SYMBOL_GPL(dsa_devlink_port_region_create); 294 295 void dsa_devlink_region_destroy(struct devlink_region *region) 296 { 297 devlink_region_destroy(region); 298 } 299 EXPORT_SYMBOL_GPL(dsa_devlink_region_destroy); 300 301 int dsa_port_devlink_setup(struct dsa_port *dp) 302 { 303 struct devlink_port *dlp = &dp->devlink_port; 304 struct dsa_switch_tree *dst = dp->ds->dst; 305 struct devlink_port_attrs attrs = {}; 306 struct devlink *dl = dp->ds->devlink; 307 struct dsa_switch *ds = dp->ds; 308 const unsigned char *id; 309 unsigned char len; 310 int err; 311 312 memset(dlp, 0, sizeof(*dlp)); 313 devlink_port_init(dl, dlp); 314 315 if (ds->ops->port_setup) { 316 err = ds->ops->port_setup(ds, dp->index); 317 if (err) 318 return err; 319 } 320 321 id = (const unsigned char *)&dst->index; 322 len = sizeof(dst->index); 323 324 attrs.phys.port_number = dp->index; 325 memcpy(attrs.switch_id.id, id, len); 326 attrs.switch_id.id_len = len; 327 328 switch (dp->type) { 329 case DSA_PORT_TYPE_UNUSED: 330 attrs.flavour = DEVLINK_PORT_FLAVOUR_UNUSED; 331 break; 332 case DSA_PORT_TYPE_CPU: 333 attrs.flavour = DEVLINK_PORT_FLAVOUR_CPU; 334 break; 335 case DSA_PORT_TYPE_DSA: 336 attrs.flavour = DEVLINK_PORT_FLAVOUR_DSA; 337 break; 338 case DSA_PORT_TYPE_USER: 339 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL; 340 break; 341 } 342 343 devlink_port_attrs_set(dlp, &attrs); 344 err = devlink_port_register(dl, dlp, dp->index); 345 if (err) { 346 if (ds->ops->port_teardown) 347 ds->ops->port_teardown(ds, dp->index); 348 return err; 349 } 350 351 return 0; 352 } 353 354 void dsa_port_devlink_teardown(struct dsa_port *dp) 355 { 356 struct devlink_port *dlp = &dp->devlink_port; 357 struct dsa_switch *ds = dp->ds; 358 359 devlink_port_unregister(dlp); 360 361 if (ds->ops->port_teardown) 362 ds->ops->port_teardown(ds, dp->index); 363 364 devlink_port_fini(dlp); 365 } 366 367 void dsa_switch_devlink_register(struct dsa_switch *ds) 368 { 369 devlink_register(ds->devlink); 370 } 371 372 void dsa_switch_devlink_unregister(struct dsa_switch *ds) 373 { 374 devlink_unregister(ds->devlink); 375 } 376 377 int dsa_switch_devlink_alloc(struct dsa_switch *ds) 378 { 379 struct dsa_devlink_priv *dl_priv; 380 struct devlink *dl; 381 382 /* Add the switch to devlink before calling setup, so that setup can 383 * add dpipe tables 384 */ 385 dl = devlink_alloc(&dsa_devlink_ops, sizeof(*dl_priv), ds->dev); 386 if (!dl) 387 return -ENOMEM; 388 389 ds->devlink = dl; 390 391 dl_priv = devlink_priv(ds->devlink); 392 dl_priv->ds = ds; 393 394 return 0; 395 } 396 397 void dsa_switch_devlink_free(struct dsa_switch *ds) 398 { 399 devlink_free(ds->devlink); 400 ds->devlink = NULL; 401 } 402