1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /* Copyright (c) 2017-2018 Mellanox Technologies. All rights reserved */
4 #include <linux/kernel.h>
5 #include <linux/errno.h>
6 #include <linux/netdevice.h>
7 #include <net/pkt_cls.h>
11 #include "spectrum_span.h"
14 #define MLXSW_SP_PRIO_BAND_TO_TCLASS(band) (IEEE_8021QAZ_MAX_TCS - band - 1)
15 #define MLXSW_SP_PRIO_CHILD_TO_TCLASS(child) \
16 MLXSW_SP_PRIO_BAND_TO_TCLASS((child - 1))
18 enum mlxsw_sp_qdisc_type {
19 MLXSW_SP_QDISC_NO_QDISC,
27 struct mlxsw_sp_qdisc;
29 struct mlxsw_sp_qdisc_ops {
30 enum mlxsw_sp_qdisc_type type;
31 int (*check_params)(struct mlxsw_sp_port *mlxsw_sp_port,
33 int (*replace)(struct mlxsw_sp_port *mlxsw_sp_port, u32 handle,
34 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc, void *params);
35 int (*destroy)(struct mlxsw_sp_port *mlxsw_sp_port,
36 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc);
37 int (*get_stats)(struct mlxsw_sp_port *mlxsw_sp_port,
38 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
39 struct tc_qopt_offload_stats *stats_ptr);
40 int (*get_xstats)(struct mlxsw_sp_port *mlxsw_sp_port,
41 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
43 void (*clean_stats)(struct mlxsw_sp_port *mlxsw_sp_port,
44 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc);
45 /* unoffload - to be used for a qdisc that stops being offloaded without
48 void (*unoffload)(struct mlxsw_sp_port *mlxsw_sp_port,
49 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc, void *params);
50 struct mlxsw_sp_qdisc *(*find_class)(struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
52 unsigned int num_classes;
55 struct mlxsw_sp_qdisc {
62 struct mlxsw_sp_qdisc_stats {
70 struct mlxsw_sp_qdisc_ops *ops;
71 struct mlxsw_sp_qdisc *parent;
72 struct mlxsw_sp_qdisc *qdiscs;
73 unsigned int num_classes;
76 struct mlxsw_sp_qdisc_state {
77 struct mlxsw_sp_qdisc root_qdisc;
79 /* When a PRIO or ETS are added, the invisible FIFOs in their bands are
80 * created first. When notifications for these FIFOs arrive, it is not
81 * known what qdisc their parent handle refers to. It could be a
82 * newly-created PRIO that will replace the currently-offloaded one, or
83 * it could be e.g. a RED that will be attached below it.
85 * As the notifications start to arrive, use them to note what the
86 * future parent handle is, and keep track of which child FIFOs were
87 * seen. Then when the parent is known, retroactively offload those
91 bool future_fifos[IEEE_8021QAZ_MAX_TCS];
92 struct mutex lock; /* Protects qdisc state. */
96 mlxsw_sp_qdisc_compare(struct mlxsw_sp_qdisc *mlxsw_sp_qdisc, u32 handle)
98 return mlxsw_sp_qdisc->ops && mlxsw_sp_qdisc->handle == handle;
101 static struct mlxsw_sp_qdisc *
102 mlxsw_sp_qdisc_walk(struct mlxsw_sp_qdisc *qdisc,
103 struct mlxsw_sp_qdisc *(*pre)(struct mlxsw_sp_qdisc *,
107 struct mlxsw_sp_qdisc *tmp;
111 tmp = pre(qdisc, data);
117 for (i = 0; i < qdisc->num_classes; i++) {
118 tmp = &qdisc->qdiscs[i];
120 tmp = mlxsw_sp_qdisc_walk(tmp, pre, data);
130 static struct mlxsw_sp_qdisc *
131 mlxsw_sp_qdisc_walk_cb_find(struct mlxsw_sp_qdisc *qdisc, void *data)
133 u32 parent = *(u32 *)data;
135 if (qdisc->ops && TC_H_MAJ(qdisc->handle) == TC_H_MAJ(parent)) {
136 if (qdisc->ops->find_class)
137 return qdisc->ops->find_class(qdisc, parent);
143 static struct mlxsw_sp_qdisc *
144 mlxsw_sp_qdisc_find(struct mlxsw_sp_port *mlxsw_sp_port, u32 parent,
147 struct mlxsw_sp_qdisc_state *qdisc_state = mlxsw_sp_port->qdisc;
151 if (parent == TC_H_ROOT)
152 return &qdisc_state->root_qdisc;
155 return mlxsw_sp_qdisc_walk(&qdisc_state->root_qdisc,
156 mlxsw_sp_qdisc_walk_cb_find, &parent);
159 static struct mlxsw_sp_qdisc *
160 mlxsw_sp_qdisc_walk_cb_find_by_handle(struct mlxsw_sp_qdisc *qdisc, void *data)
162 u32 handle = *(u32 *)data;
164 if (qdisc->ops && qdisc->handle == handle)
169 static struct mlxsw_sp_qdisc *
170 mlxsw_sp_qdisc_find_by_handle(struct mlxsw_sp_port *mlxsw_sp_port, u32 handle)
172 struct mlxsw_sp_qdisc_state *qdisc_state = mlxsw_sp_port->qdisc;
176 return mlxsw_sp_qdisc_walk(&qdisc_state->root_qdisc,
177 mlxsw_sp_qdisc_walk_cb_find_by_handle,
182 mlxsw_sp_qdisc_reduce_parent_backlog(struct mlxsw_sp_qdisc *mlxsw_sp_qdisc)
184 struct mlxsw_sp_qdisc *tmp;
186 for (tmp = mlxsw_sp_qdisc->parent; tmp; tmp = tmp->parent)
187 tmp->stats_base.backlog -= mlxsw_sp_qdisc->stats_base.backlog;
191 mlxsw_sp_qdisc_destroy(struct mlxsw_sp_port *mlxsw_sp_port,
192 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc)
194 struct mlxsw_sp_qdisc *root_qdisc = &mlxsw_sp_port->qdisc->root_qdisc;
201 if (root_qdisc == mlxsw_sp_qdisc) {
202 struct mlxsw_sp_hdroom hdroom = *mlxsw_sp_port->hdroom;
204 hdroom.mode = MLXSW_SP_HDROOM_MODE_DCB;
205 mlxsw_sp_hdroom_prios_reset_buf_idx(&hdroom);
206 mlxsw_sp_hdroom_bufs_reset_lossiness(&hdroom);
207 mlxsw_sp_hdroom_bufs_reset_sizes(mlxsw_sp_port, &hdroom);
208 err_hdroom = mlxsw_sp_hdroom_configure(mlxsw_sp_port, &hdroom);
211 if (!mlxsw_sp_qdisc->ops)
214 mlxsw_sp_qdisc_reduce_parent_backlog(mlxsw_sp_qdisc);
215 if (mlxsw_sp_qdisc->ops->destroy)
216 err = mlxsw_sp_qdisc->ops->destroy(mlxsw_sp_port,
218 if (mlxsw_sp_qdisc->ops->clean_stats)
219 mlxsw_sp_qdisc->ops->clean_stats(mlxsw_sp_port, mlxsw_sp_qdisc);
221 mlxsw_sp_qdisc->handle = TC_H_UNSPEC;
222 mlxsw_sp_qdisc->ops = NULL;
223 mlxsw_sp_qdisc->num_classes = 0;
224 kfree(mlxsw_sp_qdisc->qdiscs);
225 mlxsw_sp_qdisc->qdiscs = NULL;
226 return err_hdroom ?: err;
229 static int mlxsw_sp_qdisc_create(struct mlxsw_sp_port *mlxsw_sp_port,
231 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
232 struct mlxsw_sp_qdisc_ops *ops, void *params)
234 struct mlxsw_sp_qdisc *root_qdisc = &mlxsw_sp_port->qdisc->root_qdisc;
235 struct mlxsw_sp_hdroom orig_hdroom;
239 err = ops->check_params(mlxsw_sp_port, params);
243 if (ops->num_classes) {
244 mlxsw_sp_qdisc->qdiscs = kcalloc(ops->num_classes,
245 sizeof(*mlxsw_sp_qdisc->qdiscs),
247 if (!mlxsw_sp_qdisc->qdiscs)
250 for (i = 0; i < ops->num_classes; i++)
251 mlxsw_sp_qdisc->qdiscs[i].parent = mlxsw_sp_qdisc;
254 orig_hdroom = *mlxsw_sp_port->hdroom;
255 if (root_qdisc == mlxsw_sp_qdisc) {
256 struct mlxsw_sp_hdroom hdroom = orig_hdroom;
258 hdroom.mode = MLXSW_SP_HDROOM_MODE_TC;
259 mlxsw_sp_hdroom_prios_reset_buf_idx(&hdroom);
260 mlxsw_sp_hdroom_bufs_reset_lossiness(&hdroom);
261 mlxsw_sp_hdroom_bufs_reset_sizes(mlxsw_sp_port, &hdroom);
263 err = mlxsw_sp_hdroom_configure(mlxsw_sp_port, &hdroom);
265 goto err_hdroom_configure;
268 mlxsw_sp_qdisc->num_classes = ops->num_classes;
269 mlxsw_sp_qdisc->ops = ops;
270 mlxsw_sp_qdisc->handle = handle;
271 err = ops->replace(mlxsw_sp_port, handle, mlxsw_sp_qdisc, params);
278 mlxsw_sp_qdisc->handle = TC_H_UNSPEC;
279 mlxsw_sp_qdisc->ops = NULL;
280 mlxsw_sp_qdisc->num_classes = 0;
281 mlxsw_sp_hdroom_configure(mlxsw_sp_port, &orig_hdroom);
282 err_hdroom_configure:
283 kfree(mlxsw_sp_qdisc->qdiscs);
284 mlxsw_sp_qdisc->qdiscs = NULL;
289 mlxsw_sp_qdisc_change(struct mlxsw_sp_port *mlxsw_sp_port, u32 handle,
290 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc, void *params)
292 struct mlxsw_sp_qdisc_ops *ops = mlxsw_sp_qdisc->ops;
295 err = ops->check_params(mlxsw_sp_port, params);
299 err = ops->replace(mlxsw_sp_port, handle, mlxsw_sp_qdisc, params);
303 /* Check if the Qdisc changed. That includes a situation where an
304 * invisible Qdisc replaces another one, or is being added for the
307 if (mlxsw_sp_qdisc->handle != handle) {
308 if (ops->clean_stats)
309 ops->clean_stats(mlxsw_sp_port, mlxsw_sp_qdisc);
312 mlxsw_sp_qdisc->handle = handle;
317 ops->unoffload(mlxsw_sp_port, mlxsw_sp_qdisc, params);
319 mlxsw_sp_qdisc_destroy(mlxsw_sp_port, mlxsw_sp_qdisc);
324 mlxsw_sp_qdisc_replace(struct mlxsw_sp_port *mlxsw_sp_port, u32 handle,
325 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
326 struct mlxsw_sp_qdisc_ops *ops, void *params)
328 if (mlxsw_sp_qdisc->ops && mlxsw_sp_qdisc->ops->type != ops->type)
329 /* In case this location contained a different qdisc of the
330 * same type we can override the old qdisc configuration.
331 * Otherwise, we need to remove the old qdisc before setting the
334 mlxsw_sp_qdisc_destroy(mlxsw_sp_port, mlxsw_sp_qdisc);
336 if (!mlxsw_sp_qdisc->ops)
337 return mlxsw_sp_qdisc_create(mlxsw_sp_port, handle,
338 mlxsw_sp_qdisc, ops, params);
340 return mlxsw_sp_qdisc_change(mlxsw_sp_port, handle,
341 mlxsw_sp_qdisc, params);
345 mlxsw_sp_qdisc_get_stats(struct mlxsw_sp_port *mlxsw_sp_port,
346 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
347 struct tc_qopt_offload_stats *stats_ptr)
349 if (mlxsw_sp_qdisc && mlxsw_sp_qdisc->ops &&
350 mlxsw_sp_qdisc->ops->get_stats)
351 return mlxsw_sp_qdisc->ops->get_stats(mlxsw_sp_port,
359 mlxsw_sp_qdisc_get_xstats(struct mlxsw_sp_port *mlxsw_sp_port,
360 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
363 if (mlxsw_sp_qdisc && mlxsw_sp_qdisc->ops &&
364 mlxsw_sp_qdisc->ops->get_xstats)
365 return mlxsw_sp_qdisc->ops->get_xstats(mlxsw_sp_port,
373 mlxsw_sp_xstats_backlog(struct mlxsw_sp_port_xstats *xstats, int tclass_num)
375 return xstats->backlog[tclass_num] +
376 xstats->backlog[tclass_num + 8];
380 mlxsw_sp_xstats_tail_drop(struct mlxsw_sp_port_xstats *xstats, int tclass_num)
382 return xstats->tail_drop[tclass_num] +
383 xstats->tail_drop[tclass_num + 8];
387 mlxsw_sp_qdisc_bstats_per_priority_get(struct mlxsw_sp_port_xstats *xstats,
388 u8 prio_bitmap, u64 *tx_packets,
395 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
396 if (prio_bitmap & BIT(i)) {
397 *tx_packets += xstats->tx_packets[i];
398 *tx_bytes += xstats->tx_bytes[i];
404 mlxsw_sp_qdisc_collect_tc_stats(struct mlxsw_sp_port *mlxsw_sp_port,
405 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
406 u64 *p_tx_bytes, u64 *p_tx_packets,
407 u64 *p_drops, u64 *p_backlog)
409 int tclass_num = mlxsw_sp_qdisc->tclass_num;
410 struct mlxsw_sp_port_xstats *xstats;
411 u64 tx_bytes, tx_packets;
413 xstats = &mlxsw_sp_port->periodic_hw_stats.xstats;
414 mlxsw_sp_qdisc_bstats_per_priority_get(xstats,
415 mlxsw_sp_qdisc->prio_bitmap,
416 &tx_packets, &tx_bytes);
418 *p_tx_packets += tx_packets;
419 *p_tx_bytes += tx_bytes;
420 *p_drops += xstats->wred_drop[tclass_num] +
421 mlxsw_sp_xstats_tail_drop(xstats, tclass_num);
422 *p_backlog += mlxsw_sp_xstats_backlog(xstats, tclass_num);
426 mlxsw_sp_qdisc_update_stats(struct mlxsw_sp *mlxsw_sp,
427 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
428 u64 tx_bytes, u64 tx_packets,
429 u64 drops, u64 backlog,
430 struct tc_qopt_offload_stats *stats_ptr)
432 struct mlxsw_sp_qdisc_stats *stats_base = &mlxsw_sp_qdisc->stats_base;
434 tx_bytes -= stats_base->tx_bytes;
435 tx_packets -= stats_base->tx_packets;
436 drops -= stats_base->drops;
437 backlog -= stats_base->backlog;
439 _bstats_update(stats_ptr->bstats, tx_bytes, tx_packets);
440 stats_ptr->qstats->drops += drops;
441 stats_ptr->qstats->backlog += mlxsw_sp_cells_bytes(mlxsw_sp, backlog);
443 stats_base->backlog += backlog;
444 stats_base->drops += drops;
445 stats_base->tx_bytes += tx_bytes;
446 stats_base->tx_packets += tx_packets;
450 mlxsw_sp_qdisc_get_tc_stats(struct mlxsw_sp_port *mlxsw_sp_port,
451 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
452 struct tc_qopt_offload_stats *stats_ptr)
459 mlxsw_sp_qdisc_collect_tc_stats(mlxsw_sp_port, mlxsw_sp_qdisc,
460 &tx_bytes, &tx_packets,
462 mlxsw_sp_qdisc_update_stats(mlxsw_sp_port->mlxsw_sp, mlxsw_sp_qdisc,
463 tx_bytes, tx_packets, drops, backlog,
468 mlxsw_sp_tclass_congestion_enable(struct mlxsw_sp_port *mlxsw_sp_port,
469 int tclass_num, u32 min, u32 max,
470 u32 probability, bool is_wred, bool is_ecn)
472 char cwtpm_cmd[MLXSW_REG_CWTPM_LEN];
473 char cwtp_cmd[MLXSW_REG_CWTP_LEN];
474 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
477 mlxsw_reg_cwtp_pack(cwtp_cmd, mlxsw_sp_port->local_port, tclass_num);
478 mlxsw_reg_cwtp_profile_pack(cwtp_cmd, MLXSW_REG_CWTP_DEFAULT_PROFILE,
479 roundup(min, MLXSW_REG_CWTP_MIN_VALUE),
480 roundup(max, MLXSW_REG_CWTP_MIN_VALUE),
483 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(cwtp), cwtp_cmd);
487 mlxsw_reg_cwtpm_pack(cwtpm_cmd, mlxsw_sp_port->local_port, tclass_num,
488 MLXSW_REG_CWTP_DEFAULT_PROFILE, is_wred, is_ecn);
490 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(cwtpm), cwtpm_cmd);
494 mlxsw_sp_tclass_congestion_disable(struct mlxsw_sp_port *mlxsw_sp_port,
497 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
498 char cwtpm_cmd[MLXSW_REG_CWTPM_LEN];
500 mlxsw_reg_cwtpm_pack(cwtpm_cmd, mlxsw_sp_port->local_port, tclass_num,
501 MLXSW_REG_CWTPM_RESET_PROFILE, false, false);
502 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(cwtpm), cwtpm_cmd);
506 mlxsw_sp_setup_tc_qdisc_red_clean_stats(struct mlxsw_sp_port *mlxsw_sp_port,
507 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc)
509 int tclass_num = mlxsw_sp_qdisc->tclass_num;
510 struct mlxsw_sp_qdisc_stats *stats_base;
511 struct mlxsw_sp_port_xstats *xstats;
512 struct red_stats *red_base;
514 xstats = &mlxsw_sp_port->periodic_hw_stats.xstats;
515 stats_base = &mlxsw_sp_qdisc->stats_base;
516 red_base = &mlxsw_sp_qdisc->xstats_base.red;
518 mlxsw_sp_qdisc_bstats_per_priority_get(xstats,
519 mlxsw_sp_qdisc->prio_bitmap,
520 &stats_base->tx_packets,
521 &stats_base->tx_bytes);
522 red_base->prob_drop = xstats->wred_drop[tclass_num];
523 red_base->pdrop = mlxsw_sp_xstats_tail_drop(xstats, tclass_num);
525 stats_base->overlimits = red_base->prob_drop + red_base->prob_mark;
526 stats_base->drops = red_base->prob_drop + red_base->pdrop;
528 stats_base->backlog = 0;
532 mlxsw_sp_qdisc_red_destroy(struct mlxsw_sp_port *mlxsw_sp_port,
533 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc)
535 return mlxsw_sp_tclass_congestion_disable(mlxsw_sp_port,
536 mlxsw_sp_qdisc->tclass_num);
540 mlxsw_sp_qdisc_red_check_params(struct mlxsw_sp_port *mlxsw_sp_port,
543 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
544 struct tc_red_qopt_offload_params *p = params;
546 if (p->min > p->max) {
547 dev_err(mlxsw_sp->bus_info->dev,
548 "spectrum: RED: min %u is bigger then max %u\n", p->min,
552 if (p->max > MLXSW_CORE_RES_GET(mlxsw_sp->core,
553 GUARANTEED_SHARED_BUFFER)) {
554 dev_err(mlxsw_sp->bus_info->dev,
555 "spectrum: RED: max value %u is too big\n", p->max);
558 if (p->min == 0 || p->max == 0) {
559 dev_err(mlxsw_sp->bus_info->dev,
560 "spectrum: RED: 0 value is illegal for min and max\n");
567 mlxsw_sp_qdisc_red_replace(struct mlxsw_sp_port *mlxsw_sp_port, u32 handle,
568 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
571 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
572 struct tc_red_qopt_offload_params *p = params;
573 int tclass_num = mlxsw_sp_qdisc->tclass_num;
577 /* calculate probability in percentage */
578 prob = p->probability;
580 prob = DIV_ROUND_UP(prob, 1 << 16);
581 prob = DIV_ROUND_UP(prob, 1 << 16);
582 min = mlxsw_sp_bytes_cells(mlxsw_sp, p->min);
583 max = mlxsw_sp_bytes_cells(mlxsw_sp, p->max);
584 return mlxsw_sp_tclass_congestion_enable(mlxsw_sp_port, tclass_num,
586 !p->is_nodrop, p->is_ecn);
590 mlxsw_sp_qdisc_leaf_unoffload(struct mlxsw_sp_port *mlxsw_sp_port,
591 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
592 struct gnet_stats_queue *qstats)
596 backlog = mlxsw_sp_cells_bytes(mlxsw_sp_port->mlxsw_sp,
597 mlxsw_sp_qdisc->stats_base.backlog);
598 qstats->backlog -= backlog;
599 mlxsw_sp_qdisc->stats_base.backlog = 0;
603 mlxsw_sp_qdisc_red_unoffload(struct mlxsw_sp_port *mlxsw_sp_port,
604 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
607 struct tc_red_qopt_offload_params *p = params;
609 mlxsw_sp_qdisc_leaf_unoffload(mlxsw_sp_port, mlxsw_sp_qdisc, p->qstats);
613 mlxsw_sp_qdisc_get_red_xstats(struct mlxsw_sp_port *mlxsw_sp_port,
614 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
617 struct red_stats *xstats_base = &mlxsw_sp_qdisc->xstats_base.red;
618 int tclass_num = mlxsw_sp_qdisc->tclass_num;
619 struct mlxsw_sp_port_xstats *xstats;
620 struct red_stats *res = xstats_ptr;
621 int early_drops, pdrops;
623 xstats = &mlxsw_sp_port->periodic_hw_stats.xstats;
625 early_drops = xstats->wred_drop[tclass_num] - xstats_base->prob_drop;
626 pdrops = mlxsw_sp_xstats_tail_drop(xstats, tclass_num) -
629 res->pdrop += pdrops;
630 res->prob_drop += early_drops;
632 xstats_base->pdrop += pdrops;
633 xstats_base->prob_drop += early_drops;
638 mlxsw_sp_qdisc_get_red_stats(struct mlxsw_sp_port *mlxsw_sp_port,
639 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
640 struct tc_qopt_offload_stats *stats_ptr)
642 int tclass_num = mlxsw_sp_qdisc->tclass_num;
643 struct mlxsw_sp_qdisc_stats *stats_base;
644 struct mlxsw_sp_port_xstats *xstats;
647 xstats = &mlxsw_sp_port->periodic_hw_stats.xstats;
648 stats_base = &mlxsw_sp_qdisc->stats_base;
650 mlxsw_sp_qdisc_get_tc_stats(mlxsw_sp_port, mlxsw_sp_qdisc, stats_ptr);
651 overlimits = xstats->wred_drop[tclass_num] - stats_base->overlimits;
653 stats_ptr->qstats->overlimits += overlimits;
654 stats_base->overlimits += overlimits;
659 static struct mlxsw_sp_qdisc *
660 mlxsw_sp_qdisc_leaf_find_class(struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
666 #define MLXSW_SP_PORT_DEFAULT_TCLASS 0
668 static struct mlxsw_sp_qdisc_ops mlxsw_sp_qdisc_ops_red = {
669 .type = MLXSW_SP_QDISC_RED,
670 .check_params = mlxsw_sp_qdisc_red_check_params,
671 .replace = mlxsw_sp_qdisc_red_replace,
672 .unoffload = mlxsw_sp_qdisc_red_unoffload,
673 .destroy = mlxsw_sp_qdisc_red_destroy,
674 .get_stats = mlxsw_sp_qdisc_get_red_stats,
675 .get_xstats = mlxsw_sp_qdisc_get_red_xstats,
676 .clean_stats = mlxsw_sp_setup_tc_qdisc_red_clean_stats,
677 .find_class = mlxsw_sp_qdisc_leaf_find_class,
680 static int __mlxsw_sp_setup_tc_red(struct mlxsw_sp_port *mlxsw_sp_port,
681 struct tc_red_qopt_offload *p)
683 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc;
685 mlxsw_sp_qdisc = mlxsw_sp_qdisc_find(mlxsw_sp_port, p->parent, false);
689 if (p->command == TC_RED_REPLACE)
690 return mlxsw_sp_qdisc_replace(mlxsw_sp_port, p->handle,
692 &mlxsw_sp_qdisc_ops_red,
695 if (!mlxsw_sp_qdisc_compare(mlxsw_sp_qdisc, p->handle))
698 switch (p->command) {
700 return mlxsw_sp_qdisc_destroy(mlxsw_sp_port, mlxsw_sp_qdisc);
702 return mlxsw_sp_qdisc_get_xstats(mlxsw_sp_port, mlxsw_sp_qdisc,
705 return mlxsw_sp_qdisc_get_stats(mlxsw_sp_port, mlxsw_sp_qdisc,
712 int mlxsw_sp_setup_tc_red(struct mlxsw_sp_port *mlxsw_sp_port,
713 struct tc_red_qopt_offload *p)
717 mutex_lock(&mlxsw_sp_port->qdisc->lock);
718 err = __mlxsw_sp_setup_tc_red(mlxsw_sp_port, p);
719 mutex_unlock(&mlxsw_sp_port->qdisc->lock);
725 mlxsw_sp_setup_tc_qdisc_leaf_clean_stats(struct mlxsw_sp_port *mlxsw_sp_port,
726 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc)
728 u64 backlog_cells = 0;
733 mlxsw_sp_qdisc_collect_tc_stats(mlxsw_sp_port, mlxsw_sp_qdisc,
734 &tx_bytes, &tx_packets,
735 &drops, &backlog_cells);
737 mlxsw_sp_qdisc->stats_base.tx_packets = tx_packets;
738 mlxsw_sp_qdisc->stats_base.tx_bytes = tx_bytes;
739 mlxsw_sp_qdisc->stats_base.drops = drops;
740 mlxsw_sp_qdisc->stats_base.backlog = 0;
744 mlxsw_sp_qdisc_tbf_destroy(struct mlxsw_sp_port *mlxsw_sp_port,
745 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc)
747 return mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
748 MLXSW_REG_QEEC_HR_SUBGROUP,
749 mlxsw_sp_qdisc->tclass_num, 0,
750 MLXSW_REG_QEEC_MAS_DIS, 0);
754 mlxsw_sp_qdisc_tbf_bs(struct mlxsw_sp_port *mlxsw_sp_port,
755 u32 max_size, u8 *p_burst_size)
757 /* TBF burst size is configured in bytes. The ASIC burst size value is
758 * ((2 ^ bs) * 512 bits. Convert the TBF bytes to 512-bit units.
760 u32 bs512 = max_size / 64;
767 /* Demand a power of two. */
768 if ((1 << bs) != bs512)
771 if (bs < mlxsw_sp_port->mlxsw_sp->lowest_shaper_bs ||
772 bs > MLXSW_REG_QEEC_HIGHEST_SHAPER_BS)
780 mlxsw_sp_qdisc_tbf_max_size(u8 bs)
782 return (1U << bs) * 64;
786 mlxsw_sp_qdisc_tbf_rate_kbps(struct tc_tbf_qopt_offload_replace_params *p)
788 /* TBF interface is in bytes/s, whereas Spectrum ASIC is configured in
791 return div_u64(p->rate.rate_bytes_ps, 1000) * 8;
795 mlxsw_sp_qdisc_tbf_check_params(struct mlxsw_sp_port *mlxsw_sp_port,
798 struct tc_tbf_qopt_offload_replace_params *p = params;
799 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
800 u64 rate_kbps = mlxsw_sp_qdisc_tbf_rate_kbps(p);
804 if (rate_kbps >= MLXSW_REG_QEEC_MAS_DIS) {
805 dev_err(mlxsw_sp_port->mlxsw_sp->bus_info->dev,
806 "spectrum: TBF: rate of %lluKbps must be below %u\n",
807 rate_kbps, MLXSW_REG_QEEC_MAS_DIS);
811 err = mlxsw_sp_qdisc_tbf_bs(mlxsw_sp_port, p->max_size, &burst_size);
813 u8 highest_shaper_bs = MLXSW_REG_QEEC_HIGHEST_SHAPER_BS;
815 dev_err(mlxsw_sp->bus_info->dev,
816 "spectrum: TBF: invalid burst size of %u, must be a power of two between %u and %u",
818 mlxsw_sp_qdisc_tbf_max_size(mlxsw_sp->lowest_shaper_bs),
819 mlxsw_sp_qdisc_tbf_max_size(highest_shaper_bs));
827 mlxsw_sp_qdisc_tbf_replace(struct mlxsw_sp_port *mlxsw_sp_port, u32 handle,
828 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
831 struct tc_tbf_qopt_offload_replace_params *p = params;
832 u64 rate_kbps = mlxsw_sp_qdisc_tbf_rate_kbps(p);
836 err = mlxsw_sp_qdisc_tbf_bs(mlxsw_sp_port, p->max_size, &burst_size);
837 if (WARN_ON_ONCE(err))
838 /* check_params above was supposed to reject this value. */
841 /* Configure subgroup shaper, so that both UC and MC traffic is subject
842 * to shaping. That is unlike RED, however UC queue lengths are going to
843 * be different than MC ones due to different pool and quota
844 * configurations, so the configuration is not applicable. For shaper on
845 * the other hand, subjecting the overall stream to the configured
846 * shaper makes sense. Also note that that is what we do for
849 return mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
850 MLXSW_REG_QEEC_HR_SUBGROUP,
851 mlxsw_sp_qdisc->tclass_num, 0,
852 rate_kbps, burst_size);
856 mlxsw_sp_qdisc_tbf_unoffload(struct mlxsw_sp_port *mlxsw_sp_port,
857 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
860 struct tc_tbf_qopt_offload_replace_params *p = params;
862 mlxsw_sp_qdisc_leaf_unoffload(mlxsw_sp_port, mlxsw_sp_qdisc, p->qstats);
866 mlxsw_sp_qdisc_get_tbf_stats(struct mlxsw_sp_port *mlxsw_sp_port,
867 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
868 struct tc_qopt_offload_stats *stats_ptr)
870 mlxsw_sp_qdisc_get_tc_stats(mlxsw_sp_port, mlxsw_sp_qdisc,
875 static struct mlxsw_sp_qdisc_ops mlxsw_sp_qdisc_ops_tbf = {
876 .type = MLXSW_SP_QDISC_TBF,
877 .check_params = mlxsw_sp_qdisc_tbf_check_params,
878 .replace = mlxsw_sp_qdisc_tbf_replace,
879 .unoffload = mlxsw_sp_qdisc_tbf_unoffload,
880 .destroy = mlxsw_sp_qdisc_tbf_destroy,
881 .get_stats = mlxsw_sp_qdisc_get_tbf_stats,
882 .clean_stats = mlxsw_sp_setup_tc_qdisc_leaf_clean_stats,
883 .find_class = mlxsw_sp_qdisc_leaf_find_class,
886 static int __mlxsw_sp_setup_tc_tbf(struct mlxsw_sp_port *mlxsw_sp_port,
887 struct tc_tbf_qopt_offload *p)
889 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc;
891 mlxsw_sp_qdisc = mlxsw_sp_qdisc_find(mlxsw_sp_port, p->parent, false);
895 if (p->command == TC_TBF_REPLACE)
896 return mlxsw_sp_qdisc_replace(mlxsw_sp_port, p->handle,
898 &mlxsw_sp_qdisc_ops_tbf,
901 if (!mlxsw_sp_qdisc_compare(mlxsw_sp_qdisc, p->handle))
904 switch (p->command) {
906 return mlxsw_sp_qdisc_destroy(mlxsw_sp_port, mlxsw_sp_qdisc);
908 return mlxsw_sp_qdisc_get_stats(mlxsw_sp_port, mlxsw_sp_qdisc,
915 int mlxsw_sp_setup_tc_tbf(struct mlxsw_sp_port *mlxsw_sp_port,
916 struct tc_tbf_qopt_offload *p)
920 mutex_lock(&mlxsw_sp_port->qdisc->lock);
921 err = __mlxsw_sp_setup_tc_tbf(mlxsw_sp_port, p);
922 mutex_unlock(&mlxsw_sp_port->qdisc->lock);
928 mlxsw_sp_qdisc_fifo_check_params(struct mlxsw_sp_port *mlxsw_sp_port,
935 mlxsw_sp_qdisc_fifo_replace(struct mlxsw_sp_port *mlxsw_sp_port, u32 handle,
936 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
943 mlxsw_sp_qdisc_get_fifo_stats(struct mlxsw_sp_port *mlxsw_sp_port,
944 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
945 struct tc_qopt_offload_stats *stats_ptr)
947 mlxsw_sp_qdisc_get_tc_stats(mlxsw_sp_port, mlxsw_sp_qdisc,
952 static struct mlxsw_sp_qdisc_ops mlxsw_sp_qdisc_ops_fifo = {
953 .type = MLXSW_SP_QDISC_FIFO,
954 .check_params = mlxsw_sp_qdisc_fifo_check_params,
955 .replace = mlxsw_sp_qdisc_fifo_replace,
956 .get_stats = mlxsw_sp_qdisc_get_fifo_stats,
957 .clean_stats = mlxsw_sp_setup_tc_qdisc_leaf_clean_stats,
960 static int __mlxsw_sp_setup_tc_fifo(struct mlxsw_sp_port *mlxsw_sp_port,
961 struct tc_fifo_qopt_offload *p)
963 struct mlxsw_sp_qdisc_state *qdisc_state = mlxsw_sp_port->qdisc;
964 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc;
968 mlxsw_sp_qdisc = mlxsw_sp_qdisc_find(mlxsw_sp_port, p->parent, false);
969 if (!mlxsw_sp_qdisc && p->handle == TC_H_UNSPEC) {
970 parent_handle = TC_H_MAJ(p->parent);
971 if (parent_handle != qdisc_state->future_handle) {
972 /* This notifications is for a different Qdisc than
973 * previously. Wipe the future cache.
975 memset(qdisc_state->future_fifos, 0,
976 sizeof(qdisc_state->future_fifos));
977 qdisc_state->future_handle = parent_handle;
980 band = TC_H_MIN(p->parent) - 1;
981 if (band < IEEE_8021QAZ_MAX_TCS) {
982 if (p->command == TC_FIFO_REPLACE)
983 qdisc_state->future_fifos[band] = true;
984 else if (p->command == TC_FIFO_DESTROY)
985 qdisc_state->future_fifos[band] = false;
991 if (p->command == TC_FIFO_REPLACE) {
992 return mlxsw_sp_qdisc_replace(mlxsw_sp_port, p->handle,
994 &mlxsw_sp_qdisc_ops_fifo, NULL);
997 if (!mlxsw_sp_qdisc_compare(mlxsw_sp_qdisc, p->handle))
1000 switch (p->command) {
1001 case TC_FIFO_DESTROY:
1002 return mlxsw_sp_qdisc_destroy(mlxsw_sp_port, mlxsw_sp_qdisc);
1004 return mlxsw_sp_qdisc_get_stats(mlxsw_sp_port, mlxsw_sp_qdisc,
1006 case TC_FIFO_REPLACE: /* Handled above. */
1013 int mlxsw_sp_setup_tc_fifo(struct mlxsw_sp_port *mlxsw_sp_port,
1014 struct tc_fifo_qopt_offload *p)
1018 mutex_lock(&mlxsw_sp_port->qdisc->lock);
1019 err = __mlxsw_sp_setup_tc_fifo(mlxsw_sp_port, p);
1020 mutex_unlock(&mlxsw_sp_port->qdisc->lock);
1025 static int __mlxsw_sp_qdisc_ets_destroy(struct mlxsw_sp_port *mlxsw_sp_port,
1026 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc)
1030 for (i = 0; i < mlxsw_sp_qdisc->num_classes; i++) {
1031 mlxsw_sp_port_prio_tc_set(mlxsw_sp_port, i,
1032 MLXSW_SP_PORT_DEFAULT_TCLASS);
1033 mlxsw_sp_port_ets_set(mlxsw_sp_port,
1034 MLXSW_REG_QEEC_HR_SUBGROUP,
1036 mlxsw_sp_qdisc_destroy(mlxsw_sp_port,
1037 &mlxsw_sp_qdisc->qdiscs[i]);
1038 mlxsw_sp_qdisc->qdiscs[i].prio_bitmap = 0;
1045 mlxsw_sp_qdisc_prio_destroy(struct mlxsw_sp_port *mlxsw_sp_port,
1046 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc)
1048 return __mlxsw_sp_qdisc_ets_destroy(mlxsw_sp_port, mlxsw_sp_qdisc);
1052 __mlxsw_sp_qdisc_ets_check_params(unsigned int nbands)
1054 if (nbands > IEEE_8021QAZ_MAX_TCS)
1061 mlxsw_sp_qdisc_prio_check_params(struct mlxsw_sp_port *mlxsw_sp_port,
1064 struct tc_prio_qopt_offload_params *p = params;
1066 return __mlxsw_sp_qdisc_ets_check_params(p->bands);
1070 __mlxsw_sp_qdisc_ets_replace(struct mlxsw_sp_port *mlxsw_sp_port,
1071 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
1072 u32 handle, unsigned int nbands,
1073 const unsigned int *quanta,
1074 const unsigned int *weights,
1077 struct mlxsw_sp_qdisc_state *qdisc_state = mlxsw_sp_port->qdisc;
1078 struct mlxsw_sp_qdisc *child_qdisc;
1079 int tclass, i, band, backlog;
1083 for (band = 0; band < nbands; band++) {
1084 tclass = MLXSW_SP_PRIO_BAND_TO_TCLASS(band);
1085 child_qdisc = &mlxsw_sp_qdisc->qdiscs[band];
1086 old_priomap = child_qdisc->prio_bitmap;
1087 child_qdisc->prio_bitmap = 0;
1089 err = mlxsw_sp_port_ets_set(mlxsw_sp_port,
1090 MLXSW_REG_QEEC_HR_SUBGROUP,
1091 tclass, 0, !!quanta[band],
1096 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
1097 if (priomap[i] == band) {
1098 child_qdisc->prio_bitmap |= BIT(i);
1099 if (BIT(i) & old_priomap)
1101 err = mlxsw_sp_port_prio_tc_set(mlxsw_sp_port,
1108 child_qdisc->tclass_num = tclass;
1110 if (old_priomap != child_qdisc->prio_bitmap &&
1111 child_qdisc->ops && child_qdisc->ops->clean_stats) {
1112 backlog = child_qdisc->stats_base.backlog;
1113 child_qdisc->ops->clean_stats(mlxsw_sp_port,
1115 child_qdisc->stats_base.backlog = backlog;
1118 if (handle == qdisc_state->future_handle &&
1119 qdisc_state->future_fifos[band]) {
1120 err = mlxsw_sp_qdisc_replace(mlxsw_sp_port, TC_H_UNSPEC,
1122 &mlxsw_sp_qdisc_ops_fifo,
1128 for (; band < IEEE_8021QAZ_MAX_TCS; band++) {
1129 tclass = MLXSW_SP_PRIO_BAND_TO_TCLASS(band);
1130 child_qdisc = &mlxsw_sp_qdisc->qdiscs[band];
1131 child_qdisc->prio_bitmap = 0;
1132 mlxsw_sp_qdisc_destroy(mlxsw_sp_port, child_qdisc);
1133 mlxsw_sp_port_ets_set(mlxsw_sp_port,
1134 MLXSW_REG_QEEC_HR_SUBGROUP,
1135 tclass, 0, false, 0);
1138 qdisc_state->future_handle = TC_H_UNSPEC;
1139 memset(qdisc_state->future_fifos, 0, sizeof(qdisc_state->future_fifos));
1144 mlxsw_sp_qdisc_prio_replace(struct mlxsw_sp_port *mlxsw_sp_port, u32 handle,
1145 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
1148 struct tc_prio_qopt_offload_params *p = params;
1149 unsigned int zeroes[TCQ_ETS_MAX_BANDS] = {0};
1151 return __mlxsw_sp_qdisc_ets_replace(mlxsw_sp_port, mlxsw_sp_qdisc,
1152 handle, p->bands, zeroes,
1153 zeroes, p->priomap);
1157 __mlxsw_sp_qdisc_ets_unoffload(struct mlxsw_sp_port *mlxsw_sp_port,
1158 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
1159 struct gnet_stats_queue *qstats)
1163 backlog = mlxsw_sp_cells_bytes(mlxsw_sp_port->mlxsw_sp,
1164 mlxsw_sp_qdisc->stats_base.backlog);
1165 qstats->backlog -= backlog;
1169 mlxsw_sp_qdisc_prio_unoffload(struct mlxsw_sp_port *mlxsw_sp_port,
1170 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
1173 struct tc_prio_qopt_offload_params *p = params;
1175 __mlxsw_sp_qdisc_ets_unoffload(mlxsw_sp_port, mlxsw_sp_qdisc,
1180 mlxsw_sp_qdisc_get_prio_stats(struct mlxsw_sp_port *mlxsw_sp_port,
1181 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
1182 struct tc_qopt_offload_stats *stats_ptr)
1184 struct mlxsw_sp_qdisc *tc_qdisc;
1191 for (i = 0; i < mlxsw_sp_qdisc->num_classes; i++) {
1192 tc_qdisc = &mlxsw_sp_qdisc->qdiscs[i];
1193 mlxsw_sp_qdisc_collect_tc_stats(mlxsw_sp_port, tc_qdisc,
1194 &tx_bytes, &tx_packets,
1198 mlxsw_sp_qdisc_update_stats(mlxsw_sp_port->mlxsw_sp, mlxsw_sp_qdisc,
1199 tx_bytes, tx_packets, drops, backlog,
1205 mlxsw_sp_setup_tc_qdisc_prio_clean_stats(struct mlxsw_sp_port *mlxsw_sp_port,
1206 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc)
1208 struct mlxsw_sp_qdisc_stats *stats_base;
1209 struct mlxsw_sp_port_xstats *xstats;
1210 struct rtnl_link_stats64 *stats;
1213 xstats = &mlxsw_sp_port->periodic_hw_stats.xstats;
1214 stats = &mlxsw_sp_port->periodic_hw_stats.stats;
1215 stats_base = &mlxsw_sp_qdisc->stats_base;
1217 stats_base->tx_packets = stats->tx_packets;
1218 stats_base->tx_bytes = stats->tx_bytes;
1220 stats_base->drops = 0;
1221 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
1222 stats_base->drops += mlxsw_sp_xstats_tail_drop(xstats, i);
1223 stats_base->drops += xstats->wred_drop[i];
1226 mlxsw_sp_qdisc->stats_base.backlog = 0;
1229 static struct mlxsw_sp_qdisc *
1230 mlxsw_sp_qdisc_prio_find_class(struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
1233 int child_index = TC_H_MIN(parent);
1234 int band = child_index - 1;
1236 if (band < 0 || band >= mlxsw_sp_qdisc->num_classes)
1238 return &mlxsw_sp_qdisc->qdiscs[band];
1241 static struct mlxsw_sp_qdisc_ops mlxsw_sp_qdisc_ops_prio = {
1242 .type = MLXSW_SP_QDISC_PRIO,
1243 .check_params = mlxsw_sp_qdisc_prio_check_params,
1244 .replace = mlxsw_sp_qdisc_prio_replace,
1245 .unoffload = mlxsw_sp_qdisc_prio_unoffload,
1246 .destroy = mlxsw_sp_qdisc_prio_destroy,
1247 .get_stats = mlxsw_sp_qdisc_get_prio_stats,
1248 .clean_stats = mlxsw_sp_setup_tc_qdisc_prio_clean_stats,
1249 .find_class = mlxsw_sp_qdisc_prio_find_class,
1250 .num_classes = IEEE_8021QAZ_MAX_TCS,
1254 mlxsw_sp_qdisc_ets_check_params(struct mlxsw_sp_port *mlxsw_sp_port,
1257 struct tc_ets_qopt_offload_replace_params *p = params;
1259 return __mlxsw_sp_qdisc_ets_check_params(p->bands);
1263 mlxsw_sp_qdisc_ets_replace(struct mlxsw_sp_port *mlxsw_sp_port, u32 handle,
1264 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
1267 struct tc_ets_qopt_offload_replace_params *p = params;
1269 return __mlxsw_sp_qdisc_ets_replace(mlxsw_sp_port, mlxsw_sp_qdisc,
1270 handle, p->bands, p->quanta,
1271 p->weights, p->priomap);
1275 mlxsw_sp_qdisc_ets_unoffload(struct mlxsw_sp_port *mlxsw_sp_port,
1276 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
1279 struct tc_ets_qopt_offload_replace_params *p = params;
1281 __mlxsw_sp_qdisc_ets_unoffload(mlxsw_sp_port, mlxsw_sp_qdisc,
1286 mlxsw_sp_qdisc_ets_destroy(struct mlxsw_sp_port *mlxsw_sp_port,
1287 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc)
1289 return __mlxsw_sp_qdisc_ets_destroy(mlxsw_sp_port, mlxsw_sp_qdisc);
1292 static struct mlxsw_sp_qdisc_ops mlxsw_sp_qdisc_ops_ets = {
1293 .type = MLXSW_SP_QDISC_ETS,
1294 .check_params = mlxsw_sp_qdisc_ets_check_params,
1295 .replace = mlxsw_sp_qdisc_ets_replace,
1296 .unoffload = mlxsw_sp_qdisc_ets_unoffload,
1297 .destroy = mlxsw_sp_qdisc_ets_destroy,
1298 .get_stats = mlxsw_sp_qdisc_get_prio_stats,
1299 .clean_stats = mlxsw_sp_setup_tc_qdisc_prio_clean_stats,
1300 .find_class = mlxsw_sp_qdisc_prio_find_class,
1301 .num_classes = IEEE_8021QAZ_MAX_TCS,
1304 /* Linux allows linking of Qdiscs to arbitrary classes (so long as the resulting
1305 * graph is free of cycles). These operations do not change the parent handle
1306 * though, which means it can be incomplete (if there is more than one class
1307 * where the Qdisc in question is grafted) or outright wrong (if the Qdisc was
1308 * linked to a different class and then removed from the original class).
1310 * E.g. consider this sequence of operations:
1312 * # tc qdisc add dev swp1 root handle 1: prio
1313 * # tc qdisc add dev swp1 parent 1:3 handle 13: red limit 1000000 avpkt 10000
1314 * RED: set bandwidth to 10Mbit
1315 * # tc qdisc link dev swp1 handle 13: parent 1:2
1317 * At this point, both 1:2 and 1:3 have the same RED Qdisc instance as their
1318 * child. But RED will still only claim that 1:3 is its parent. If it's removed
1319 * from that band, its only parent will be 1:2, but it will continue to claim
1320 * that it is in fact 1:3.
1322 * The notification for child Qdisc replace (e.g. TC_RED_REPLACE) comes before
1323 * the notification for parent graft (e.g. TC_PRIO_GRAFT). We take the replace
1324 * notification to offload the child Qdisc, based on its parent handle, and use
1325 * the graft operation to validate that the class where the child is actually
1326 * grafted corresponds to the parent handle. If the two don't match, we
1327 * unoffload the child.
1330 __mlxsw_sp_qdisc_ets_graft(struct mlxsw_sp_port *mlxsw_sp_port,
1331 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
1332 u8 band, u32 child_handle)
1334 struct mlxsw_sp_qdisc *old_qdisc;
1336 if (band < mlxsw_sp_qdisc->num_classes &&
1337 mlxsw_sp_qdisc->qdiscs[band].handle == child_handle)
1340 if (!child_handle) {
1341 /* This is an invisible FIFO replacing the original Qdisc.
1342 * Ignore it--the original Qdisc's destroy will follow.
1347 /* See if the grafted qdisc is already offloaded on any tclass. If so,
1350 old_qdisc = mlxsw_sp_qdisc_find_by_handle(mlxsw_sp_port,
1353 mlxsw_sp_qdisc_destroy(mlxsw_sp_port, old_qdisc);
1355 mlxsw_sp_qdisc = mlxsw_sp_qdisc->ops->find_class(mlxsw_sp_qdisc, band);
1356 if (!WARN_ON(!mlxsw_sp_qdisc))
1357 mlxsw_sp_qdisc_destroy(mlxsw_sp_port, mlxsw_sp_qdisc);
1363 mlxsw_sp_qdisc_prio_graft(struct mlxsw_sp_port *mlxsw_sp_port,
1364 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc,
1365 struct tc_prio_qopt_offload_graft_params *p)
1367 return __mlxsw_sp_qdisc_ets_graft(mlxsw_sp_port, mlxsw_sp_qdisc,
1368 p->band, p->child_handle);
1371 static int __mlxsw_sp_setup_tc_prio(struct mlxsw_sp_port *mlxsw_sp_port,
1372 struct tc_prio_qopt_offload *p)
1374 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc;
1376 mlxsw_sp_qdisc = mlxsw_sp_qdisc_find(mlxsw_sp_port, p->parent, true);
1377 if (!mlxsw_sp_qdisc)
1380 if (p->command == TC_PRIO_REPLACE)
1381 return mlxsw_sp_qdisc_replace(mlxsw_sp_port, p->handle,
1383 &mlxsw_sp_qdisc_ops_prio,
1384 &p->replace_params);
1386 if (!mlxsw_sp_qdisc_compare(mlxsw_sp_qdisc, p->handle))
1389 switch (p->command) {
1390 case TC_PRIO_DESTROY:
1391 return mlxsw_sp_qdisc_destroy(mlxsw_sp_port, mlxsw_sp_qdisc);
1393 return mlxsw_sp_qdisc_get_stats(mlxsw_sp_port, mlxsw_sp_qdisc,
1396 return mlxsw_sp_qdisc_prio_graft(mlxsw_sp_port, mlxsw_sp_qdisc,
1403 int mlxsw_sp_setup_tc_prio(struct mlxsw_sp_port *mlxsw_sp_port,
1404 struct tc_prio_qopt_offload *p)
1408 mutex_lock(&mlxsw_sp_port->qdisc->lock);
1409 err = __mlxsw_sp_setup_tc_prio(mlxsw_sp_port, p);
1410 mutex_unlock(&mlxsw_sp_port->qdisc->lock);
1415 static int __mlxsw_sp_setup_tc_ets(struct mlxsw_sp_port *mlxsw_sp_port,
1416 struct tc_ets_qopt_offload *p)
1418 struct mlxsw_sp_qdisc *mlxsw_sp_qdisc;
1420 mlxsw_sp_qdisc = mlxsw_sp_qdisc_find(mlxsw_sp_port, p->parent, true);
1421 if (!mlxsw_sp_qdisc)
1424 if (p->command == TC_ETS_REPLACE)
1425 return mlxsw_sp_qdisc_replace(mlxsw_sp_port, p->handle,
1427 &mlxsw_sp_qdisc_ops_ets,
1428 &p->replace_params);
1430 if (!mlxsw_sp_qdisc_compare(mlxsw_sp_qdisc, p->handle))
1433 switch (p->command) {
1434 case TC_ETS_DESTROY:
1435 return mlxsw_sp_qdisc_destroy(mlxsw_sp_port, mlxsw_sp_qdisc);
1437 return mlxsw_sp_qdisc_get_stats(mlxsw_sp_port, mlxsw_sp_qdisc,
1440 return __mlxsw_sp_qdisc_ets_graft(mlxsw_sp_port, mlxsw_sp_qdisc,
1441 p->graft_params.band,
1442 p->graft_params.child_handle);
1448 int mlxsw_sp_setup_tc_ets(struct mlxsw_sp_port *mlxsw_sp_port,
1449 struct tc_ets_qopt_offload *p)
1453 mutex_lock(&mlxsw_sp_port->qdisc->lock);
1454 err = __mlxsw_sp_setup_tc_ets(mlxsw_sp_port, p);
1455 mutex_unlock(&mlxsw_sp_port->qdisc->lock);
1460 struct mlxsw_sp_qevent_block {
1461 struct list_head binding_list;
1462 struct list_head mall_entry_list;
1463 struct mlxsw_sp *mlxsw_sp;
1466 struct mlxsw_sp_qevent_binding {
1467 struct list_head list;
1468 struct mlxsw_sp_port *mlxsw_sp_port;
1471 enum mlxsw_sp_span_trigger span_trigger;
1474 static LIST_HEAD(mlxsw_sp_qevent_block_cb_list);
1476 static int mlxsw_sp_qevent_span_configure(struct mlxsw_sp *mlxsw_sp,
1477 struct mlxsw_sp_mall_entry *mall_entry,
1478 struct mlxsw_sp_qevent_binding *qevent_binding,
1479 const struct mlxsw_sp_span_agent_parms *agent_parms,
1482 struct mlxsw_sp_port *mlxsw_sp_port = qevent_binding->mlxsw_sp_port;
1483 struct mlxsw_sp_span_trigger_parms trigger_parms = {};
1487 err = mlxsw_sp_span_agent_get(mlxsw_sp, &span_id, agent_parms);
1491 err = mlxsw_sp_span_analyzed_port_get(mlxsw_sp_port, true);
1493 goto err_analyzed_port_get;
1495 trigger_parms.span_id = span_id;
1496 trigger_parms.probability_rate = 1;
1497 err = mlxsw_sp_span_agent_bind(mlxsw_sp, qevent_binding->span_trigger, mlxsw_sp_port,
1500 goto err_agent_bind;
1502 err = mlxsw_sp_span_trigger_enable(mlxsw_sp_port, qevent_binding->span_trigger,
1503 qevent_binding->tclass_num);
1505 goto err_trigger_enable;
1507 *p_span_id = span_id;
1511 mlxsw_sp_span_agent_unbind(mlxsw_sp, qevent_binding->span_trigger, mlxsw_sp_port,
1514 mlxsw_sp_span_analyzed_port_put(mlxsw_sp_port, true);
1515 err_analyzed_port_get:
1516 mlxsw_sp_span_agent_put(mlxsw_sp, span_id);
1520 static void mlxsw_sp_qevent_span_deconfigure(struct mlxsw_sp *mlxsw_sp,
1521 struct mlxsw_sp_qevent_binding *qevent_binding,
1524 struct mlxsw_sp_port *mlxsw_sp_port = qevent_binding->mlxsw_sp_port;
1525 struct mlxsw_sp_span_trigger_parms trigger_parms = {
1529 mlxsw_sp_span_trigger_disable(mlxsw_sp_port, qevent_binding->span_trigger,
1530 qevent_binding->tclass_num);
1531 mlxsw_sp_span_agent_unbind(mlxsw_sp, qevent_binding->span_trigger, mlxsw_sp_port,
1533 mlxsw_sp_span_analyzed_port_put(mlxsw_sp_port, true);
1534 mlxsw_sp_span_agent_put(mlxsw_sp, span_id);
1537 static int mlxsw_sp_qevent_mirror_configure(struct mlxsw_sp *mlxsw_sp,
1538 struct mlxsw_sp_mall_entry *mall_entry,
1539 struct mlxsw_sp_qevent_binding *qevent_binding)
1541 struct mlxsw_sp_span_agent_parms agent_parms = {
1542 .to_dev = mall_entry->mirror.to_dev,
1545 return mlxsw_sp_qevent_span_configure(mlxsw_sp, mall_entry, qevent_binding,
1546 &agent_parms, &mall_entry->mirror.span_id);
1549 static void mlxsw_sp_qevent_mirror_deconfigure(struct mlxsw_sp *mlxsw_sp,
1550 struct mlxsw_sp_mall_entry *mall_entry,
1551 struct mlxsw_sp_qevent_binding *qevent_binding)
1553 mlxsw_sp_qevent_span_deconfigure(mlxsw_sp, qevent_binding, mall_entry->mirror.span_id);
1556 static int mlxsw_sp_qevent_trap_configure(struct mlxsw_sp *mlxsw_sp,
1557 struct mlxsw_sp_mall_entry *mall_entry,
1558 struct mlxsw_sp_qevent_binding *qevent_binding)
1560 struct mlxsw_sp_span_agent_parms agent_parms = {
1561 .session_id = MLXSW_SP_SPAN_SESSION_ID_BUFFER,
1565 err = mlxsw_sp_trap_group_policer_hw_id_get(mlxsw_sp,
1566 DEVLINK_TRAP_GROUP_GENERIC_ID_BUFFER_DROPS,
1567 &agent_parms.policer_enable,
1568 &agent_parms.policer_id);
1572 return mlxsw_sp_qevent_span_configure(mlxsw_sp, mall_entry, qevent_binding,
1573 &agent_parms, &mall_entry->trap.span_id);
1576 static void mlxsw_sp_qevent_trap_deconfigure(struct mlxsw_sp *mlxsw_sp,
1577 struct mlxsw_sp_mall_entry *mall_entry,
1578 struct mlxsw_sp_qevent_binding *qevent_binding)
1580 mlxsw_sp_qevent_span_deconfigure(mlxsw_sp, qevent_binding, mall_entry->trap.span_id);
1583 static int mlxsw_sp_qevent_entry_configure(struct mlxsw_sp *mlxsw_sp,
1584 struct mlxsw_sp_mall_entry *mall_entry,
1585 struct mlxsw_sp_qevent_binding *qevent_binding)
1587 switch (mall_entry->type) {
1588 case MLXSW_SP_MALL_ACTION_TYPE_MIRROR:
1589 return mlxsw_sp_qevent_mirror_configure(mlxsw_sp, mall_entry, qevent_binding);
1590 case MLXSW_SP_MALL_ACTION_TYPE_TRAP:
1591 return mlxsw_sp_qevent_trap_configure(mlxsw_sp, mall_entry, qevent_binding);
1593 /* This should have been validated away. */
1599 static void mlxsw_sp_qevent_entry_deconfigure(struct mlxsw_sp *mlxsw_sp,
1600 struct mlxsw_sp_mall_entry *mall_entry,
1601 struct mlxsw_sp_qevent_binding *qevent_binding)
1603 switch (mall_entry->type) {
1604 case MLXSW_SP_MALL_ACTION_TYPE_MIRROR:
1605 return mlxsw_sp_qevent_mirror_deconfigure(mlxsw_sp, mall_entry, qevent_binding);
1606 case MLXSW_SP_MALL_ACTION_TYPE_TRAP:
1607 return mlxsw_sp_qevent_trap_deconfigure(mlxsw_sp, mall_entry, qevent_binding);
1614 static int mlxsw_sp_qevent_binding_configure(struct mlxsw_sp_qevent_block *qevent_block,
1615 struct mlxsw_sp_qevent_binding *qevent_binding)
1617 struct mlxsw_sp_mall_entry *mall_entry;
1620 list_for_each_entry(mall_entry, &qevent_block->mall_entry_list, list) {
1621 err = mlxsw_sp_qevent_entry_configure(qevent_block->mlxsw_sp, mall_entry,
1624 goto err_entry_configure;
1629 err_entry_configure:
1630 list_for_each_entry_continue_reverse(mall_entry, &qevent_block->mall_entry_list, list)
1631 mlxsw_sp_qevent_entry_deconfigure(qevent_block->mlxsw_sp, mall_entry,
1636 static void mlxsw_sp_qevent_binding_deconfigure(struct mlxsw_sp_qevent_block *qevent_block,
1637 struct mlxsw_sp_qevent_binding *qevent_binding)
1639 struct mlxsw_sp_mall_entry *mall_entry;
1641 list_for_each_entry(mall_entry, &qevent_block->mall_entry_list, list)
1642 mlxsw_sp_qevent_entry_deconfigure(qevent_block->mlxsw_sp, mall_entry,
1646 static int mlxsw_sp_qevent_block_configure(struct mlxsw_sp_qevent_block *qevent_block)
1648 struct mlxsw_sp_qevent_binding *qevent_binding;
1651 list_for_each_entry(qevent_binding, &qevent_block->binding_list, list) {
1652 err = mlxsw_sp_qevent_binding_configure(qevent_block, qevent_binding);
1654 goto err_binding_configure;
1659 err_binding_configure:
1660 list_for_each_entry_continue_reverse(qevent_binding, &qevent_block->binding_list, list)
1661 mlxsw_sp_qevent_binding_deconfigure(qevent_block, qevent_binding);
1665 static void mlxsw_sp_qevent_block_deconfigure(struct mlxsw_sp_qevent_block *qevent_block)
1667 struct mlxsw_sp_qevent_binding *qevent_binding;
1669 list_for_each_entry(qevent_binding, &qevent_block->binding_list, list)
1670 mlxsw_sp_qevent_binding_deconfigure(qevent_block, qevent_binding);
1673 static struct mlxsw_sp_mall_entry *
1674 mlxsw_sp_qevent_mall_entry_find(struct mlxsw_sp_qevent_block *block, unsigned long cookie)
1676 struct mlxsw_sp_mall_entry *mall_entry;
1678 list_for_each_entry(mall_entry, &block->mall_entry_list, list)
1679 if (mall_entry->cookie == cookie)
1685 static int mlxsw_sp_qevent_mall_replace(struct mlxsw_sp *mlxsw_sp,
1686 struct mlxsw_sp_qevent_block *qevent_block,
1687 struct tc_cls_matchall_offload *f)
1689 struct mlxsw_sp_mall_entry *mall_entry;
1690 struct flow_action_entry *act;
1693 /* It should not currently be possible to replace a matchall rule. So
1694 * this must be a new rule.
1696 if (!list_empty(&qevent_block->mall_entry_list)) {
1697 NL_SET_ERR_MSG(f->common.extack, "At most one filter supported");
1700 if (f->rule->action.num_entries != 1) {
1701 NL_SET_ERR_MSG(f->common.extack, "Only singular actions supported");
1704 if (f->common.chain_index) {
1705 NL_SET_ERR_MSG(f->common.extack, "Only chain 0 is supported");
1708 if (f->common.protocol != htons(ETH_P_ALL)) {
1709 NL_SET_ERR_MSG(f->common.extack, "Protocol matching not supported");
1713 act = &f->rule->action.entries[0];
1714 if (!(act->hw_stats & FLOW_ACTION_HW_STATS_DISABLED)) {
1715 NL_SET_ERR_MSG(f->common.extack, "HW counters not supported on qevents");
1719 mall_entry = kzalloc(sizeof(*mall_entry), GFP_KERNEL);
1722 mall_entry->cookie = f->cookie;
1724 if (act->id == FLOW_ACTION_MIRRED) {
1725 mall_entry->type = MLXSW_SP_MALL_ACTION_TYPE_MIRROR;
1726 mall_entry->mirror.to_dev = act->dev;
1727 } else if (act->id == FLOW_ACTION_TRAP) {
1728 mall_entry->type = MLXSW_SP_MALL_ACTION_TYPE_TRAP;
1730 NL_SET_ERR_MSG(f->common.extack, "Unsupported action");
1732 goto err_unsupported_action;
1735 list_add_tail(&mall_entry->list, &qevent_block->mall_entry_list);
1737 err = mlxsw_sp_qevent_block_configure(qevent_block);
1739 goto err_block_configure;
1743 err_block_configure:
1744 list_del(&mall_entry->list);
1745 err_unsupported_action:
1750 static void mlxsw_sp_qevent_mall_destroy(struct mlxsw_sp_qevent_block *qevent_block,
1751 struct tc_cls_matchall_offload *f)
1753 struct mlxsw_sp_mall_entry *mall_entry;
1755 mall_entry = mlxsw_sp_qevent_mall_entry_find(qevent_block, f->cookie);
1759 mlxsw_sp_qevent_block_deconfigure(qevent_block);
1761 list_del(&mall_entry->list);
1765 static int mlxsw_sp_qevent_block_mall_cb(struct mlxsw_sp_qevent_block *qevent_block,
1766 struct tc_cls_matchall_offload *f)
1768 struct mlxsw_sp *mlxsw_sp = qevent_block->mlxsw_sp;
1770 switch (f->command) {
1771 case TC_CLSMATCHALL_REPLACE:
1772 return mlxsw_sp_qevent_mall_replace(mlxsw_sp, qevent_block, f);
1773 case TC_CLSMATCHALL_DESTROY:
1774 mlxsw_sp_qevent_mall_destroy(qevent_block, f);
1781 static int mlxsw_sp_qevent_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
1783 struct mlxsw_sp_qevent_block *qevent_block = cb_priv;
1786 case TC_SETUP_CLSMATCHALL:
1787 return mlxsw_sp_qevent_block_mall_cb(qevent_block, type_data);
1793 static struct mlxsw_sp_qevent_block *mlxsw_sp_qevent_block_create(struct mlxsw_sp *mlxsw_sp,
1796 struct mlxsw_sp_qevent_block *qevent_block;
1798 qevent_block = kzalloc(sizeof(*qevent_block), GFP_KERNEL);
1802 INIT_LIST_HEAD(&qevent_block->binding_list);
1803 INIT_LIST_HEAD(&qevent_block->mall_entry_list);
1804 qevent_block->mlxsw_sp = mlxsw_sp;
1805 return qevent_block;
1809 mlxsw_sp_qevent_block_destroy(struct mlxsw_sp_qevent_block *qevent_block)
1811 WARN_ON(!list_empty(&qevent_block->binding_list));
1812 WARN_ON(!list_empty(&qevent_block->mall_entry_list));
1813 kfree(qevent_block);
1816 static void mlxsw_sp_qevent_block_release(void *cb_priv)
1818 struct mlxsw_sp_qevent_block *qevent_block = cb_priv;
1820 mlxsw_sp_qevent_block_destroy(qevent_block);
1823 static struct mlxsw_sp_qevent_binding *
1824 mlxsw_sp_qevent_binding_create(struct mlxsw_sp_port *mlxsw_sp_port, u32 handle, int tclass_num,
1825 enum mlxsw_sp_span_trigger span_trigger)
1827 struct mlxsw_sp_qevent_binding *binding;
1829 binding = kzalloc(sizeof(*binding), GFP_KERNEL);
1831 return ERR_PTR(-ENOMEM);
1833 binding->mlxsw_sp_port = mlxsw_sp_port;
1834 binding->handle = handle;
1835 binding->tclass_num = tclass_num;
1836 binding->span_trigger = span_trigger;
1841 mlxsw_sp_qevent_binding_destroy(struct mlxsw_sp_qevent_binding *binding)
1846 static struct mlxsw_sp_qevent_binding *
1847 mlxsw_sp_qevent_binding_lookup(struct mlxsw_sp_qevent_block *block,
1848 struct mlxsw_sp_port *mlxsw_sp_port,
1850 enum mlxsw_sp_span_trigger span_trigger)
1852 struct mlxsw_sp_qevent_binding *qevent_binding;
1854 list_for_each_entry(qevent_binding, &block->binding_list, list)
1855 if (qevent_binding->mlxsw_sp_port == mlxsw_sp_port &&
1856 qevent_binding->handle == handle &&
1857 qevent_binding->span_trigger == span_trigger)
1858 return qevent_binding;
1862 static int mlxsw_sp_setup_tc_block_qevent_bind(struct mlxsw_sp_port *mlxsw_sp_port,
1863 struct flow_block_offload *f,
1864 enum mlxsw_sp_span_trigger span_trigger)
1866 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1867 struct mlxsw_sp_qevent_binding *qevent_binding;
1868 struct mlxsw_sp_qevent_block *qevent_block;
1869 struct flow_block_cb *block_cb;
1870 struct mlxsw_sp_qdisc *qdisc;
1871 bool register_block = false;
1874 block_cb = flow_block_cb_lookup(f->block, mlxsw_sp_qevent_block_cb, mlxsw_sp);
1876 qevent_block = mlxsw_sp_qevent_block_create(mlxsw_sp, f->net);
1879 block_cb = flow_block_cb_alloc(mlxsw_sp_qevent_block_cb, mlxsw_sp, qevent_block,
1880 mlxsw_sp_qevent_block_release);
1881 if (IS_ERR(block_cb)) {
1882 mlxsw_sp_qevent_block_destroy(qevent_block);
1883 return PTR_ERR(block_cb);
1885 register_block = true;
1887 qevent_block = flow_block_cb_priv(block_cb);
1889 flow_block_cb_incref(block_cb);
1891 qdisc = mlxsw_sp_qdisc_find_by_handle(mlxsw_sp_port, f->sch->handle);
1893 NL_SET_ERR_MSG(f->extack, "Qdisc not offloaded");
1895 goto err_find_qdisc;
1898 if (WARN_ON(mlxsw_sp_qevent_binding_lookup(qevent_block, mlxsw_sp_port, f->sch->handle,
1901 goto err_binding_exists;
1904 qevent_binding = mlxsw_sp_qevent_binding_create(mlxsw_sp_port, f->sch->handle,
1905 qdisc->tclass_num, span_trigger);
1906 if (IS_ERR(qevent_binding)) {
1907 err = PTR_ERR(qevent_binding);
1908 goto err_binding_create;
1911 err = mlxsw_sp_qevent_binding_configure(qevent_block, qevent_binding);
1913 goto err_binding_configure;
1915 list_add(&qevent_binding->list, &qevent_block->binding_list);
1917 if (register_block) {
1918 flow_block_cb_add(block_cb, f);
1919 list_add_tail(&block_cb->driver_list, &mlxsw_sp_qevent_block_cb_list);
1924 err_binding_configure:
1925 mlxsw_sp_qevent_binding_destroy(qevent_binding);
1929 if (!flow_block_cb_decref(block_cb))
1930 flow_block_cb_free(block_cb);
1934 static void mlxsw_sp_setup_tc_block_qevent_unbind(struct mlxsw_sp_port *mlxsw_sp_port,
1935 struct flow_block_offload *f,
1936 enum mlxsw_sp_span_trigger span_trigger)
1938 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1939 struct mlxsw_sp_qevent_binding *qevent_binding;
1940 struct mlxsw_sp_qevent_block *qevent_block;
1941 struct flow_block_cb *block_cb;
1943 block_cb = flow_block_cb_lookup(f->block, mlxsw_sp_qevent_block_cb, mlxsw_sp);
1946 qevent_block = flow_block_cb_priv(block_cb);
1948 qevent_binding = mlxsw_sp_qevent_binding_lookup(qevent_block, mlxsw_sp_port, f->sch->handle,
1950 if (!qevent_binding)
1953 list_del(&qevent_binding->list);
1954 mlxsw_sp_qevent_binding_deconfigure(qevent_block, qevent_binding);
1955 mlxsw_sp_qevent_binding_destroy(qevent_binding);
1957 if (!flow_block_cb_decref(block_cb)) {
1958 flow_block_cb_remove(block_cb, f);
1959 list_del(&block_cb->driver_list);
1963 static int mlxsw_sp_setup_tc_block_qevent(struct mlxsw_sp_port *mlxsw_sp_port,
1964 struct flow_block_offload *f,
1965 enum mlxsw_sp_span_trigger span_trigger)
1967 f->driver_block_list = &mlxsw_sp_qevent_block_cb_list;
1969 switch (f->command) {
1970 case FLOW_BLOCK_BIND:
1971 return mlxsw_sp_setup_tc_block_qevent_bind(mlxsw_sp_port, f, span_trigger);
1972 case FLOW_BLOCK_UNBIND:
1973 mlxsw_sp_setup_tc_block_qevent_unbind(mlxsw_sp_port, f, span_trigger);
1980 int mlxsw_sp_setup_tc_block_qevent_early_drop(struct mlxsw_sp_port *mlxsw_sp_port,
1981 struct flow_block_offload *f)
1983 return mlxsw_sp_setup_tc_block_qevent(mlxsw_sp_port, f, MLXSW_SP_SPAN_TRIGGER_EARLY_DROP);
1986 int mlxsw_sp_tc_qdisc_init(struct mlxsw_sp_port *mlxsw_sp_port)
1988 struct mlxsw_sp_qdisc_state *qdisc_state;
1990 qdisc_state = kzalloc(sizeof(*qdisc_state), GFP_KERNEL);
1994 mutex_init(&qdisc_state->lock);
1995 qdisc_state->root_qdisc.prio_bitmap = 0xff;
1996 qdisc_state->root_qdisc.tclass_num = MLXSW_SP_PORT_DEFAULT_TCLASS;
1997 mlxsw_sp_port->qdisc = qdisc_state;
2001 void mlxsw_sp_tc_qdisc_fini(struct mlxsw_sp_port *mlxsw_sp_port)
2003 mutex_destroy(&mlxsw_sp_port->qdisc->lock);
2004 kfree(mlxsw_sp_port->qdisc);