net: lan966x: Add offload support for tbf
authorHoratiu Vultur <horatiu.vultur@microchip.com>
Sun, 25 Sep 2022 18:46:31 +0000 (20:46 +0200)
committerDavid S. Miller <davem@davemloft.net>
Wed, 28 Sep 2022 08:36:28 +0000 (09:36 +0100)
The tbf qdisc allows to attach a shaper on traffic egress on a port or
on a queue. On port they are attached directly to the root and on queue
they are attached on one of the classes of the parent qdisc.

Signed-off-by: Horatiu Vultur <horatiu.vultur@microchip.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/net/ethernet/microchip/lan966x/Makefile
drivers/net/ethernet/microchip/lan966x/lan966x_main.h
drivers/net/ethernet/microchip/lan966x/lan966x_regs.h
drivers/net/ethernet/microchip/lan966x/lan966x_tbf.c [new file with mode: 0644]
drivers/net/ethernet/microchip/lan966x/lan966x_tc.c

index cac8b39..a3a519d 100644 (file)
@@ -9,4 +9,5 @@ lan966x-switch-objs  := lan966x_main.o lan966x_phylink.o lan966x_port.o \
                        lan966x_mac.o lan966x_ethtool.o lan966x_switchdev.o \
                        lan966x_vlan.o lan966x_fdb.o lan966x_mdb.o \
                        lan966x_ptp.o lan966x_fdma.o lan966x_lag.o \
-                       lan966x_tc.o lan966x_mqprio.o lan966x_taprio.o
+                       lan966x_tc.o lan966x_mqprio.o lan966x_taprio.o \
+                       lan966x_tbf.o
index 935c116..59f5a6b 100644 (file)
@@ -9,6 +9,7 @@
 #include <linux/phy.h>
 #include <linux/phylink.h>
 #include <linux/ptp_clock_kernel.h>
+#include <net/pkt_cls.h>
 #include <net/pkt_sched.h>
 #include <net/switchdev.h>
 
@@ -81,6 +82,9 @@
 #define FDMA_INJ_CHANNEL               0
 #define FDMA_DCB_MAX                   512
 
+#define SE_IDX_QUEUE                   0  /* 0-79 : Queue scheduler elements */
+#define SE_IDX_PORT                    80 /* 80-89 : Port schedular elements */
+
 /* MAC table entry types.
  * ENTRYTYPE_NORMAL is subject to aging.
  * ENTRYTYPE_LOCKED is not subject to aging.
@@ -462,6 +466,11 @@ int lan966x_taprio_add(struct lan966x_port *port,
 int lan966x_taprio_del(struct lan966x_port *port);
 int lan966x_taprio_speed_set(struct lan966x_port *port, int speed);
 
+int lan966x_tbf_add(struct lan966x_port *port,
+                   struct tc_tbf_qopt_offload *qopt);
+int lan966x_tbf_del(struct lan966x_port *port,
+                   struct tc_tbf_qopt_offload *qopt);
+
 static inline void __iomem *lan_addr(void __iomem *base[],
                                     int id, int tinst, int tcnt,
                                     int gbase, int ginst,
index 684b08c..01fa6bb 100644 (file)
@@ -1018,6 +1018,36 @@ enum lan966x_target {
 /*      QSYS:RES_CTRL:RES_CFG */
 #define QSYS_RES_CFG(g)           __REG(TARGET_QSYS, 0, 1, 32768, g, 1024, 8, 0, 0, 1, 4)
 
+/*      QSYS:HSCH:CIR_CFG */
+#define QSYS_CIR_CFG(g)           __REG(TARGET_QSYS, 0, 1, 16384, g, 90, 128, 0, 0, 1, 4)
+
+#define QSYS_CIR_CFG_CIR_RATE                    GENMASK(20, 6)
+#define QSYS_CIR_CFG_CIR_RATE_SET(x)\
+       FIELD_PREP(QSYS_CIR_CFG_CIR_RATE, x)
+#define QSYS_CIR_CFG_CIR_RATE_GET(x)\
+       FIELD_GET(QSYS_CIR_CFG_CIR_RATE, x)
+
+#define QSYS_CIR_CFG_CIR_BURST                   GENMASK(5, 0)
+#define QSYS_CIR_CFG_CIR_BURST_SET(x)\
+       FIELD_PREP(QSYS_CIR_CFG_CIR_BURST, x)
+#define QSYS_CIR_CFG_CIR_BURST_GET(x)\
+       FIELD_GET(QSYS_CIR_CFG_CIR_BURST, x)
+
+/*      QSYS:HSCH:SE_CFG */
+#define QSYS_SE_CFG(g)            __REG(TARGET_QSYS, 0, 1, 16384, g, 90, 128, 8, 0, 1, 4)
+
+#define QSYS_SE_CFG_SE_AVB_ENA                   BIT(4)
+#define QSYS_SE_CFG_SE_AVB_ENA_SET(x)\
+       FIELD_PREP(QSYS_SE_CFG_SE_AVB_ENA, x)
+#define QSYS_SE_CFG_SE_AVB_ENA_GET(x)\
+       FIELD_GET(QSYS_SE_CFG_SE_AVB_ENA, x)
+
+#define QSYS_SE_CFG_SE_FRM_MODE                  GENMASK(3, 2)
+#define QSYS_SE_CFG_SE_FRM_MODE_SET(x)\
+       FIELD_PREP(QSYS_SE_CFG_SE_FRM_MODE, x)
+#define QSYS_SE_CFG_SE_FRM_MODE_GET(x)\
+       FIELD_GET(QSYS_SE_CFG_SE_FRM_MODE, x)
+
 /*      QSYS:TAS_CONFIG:TAS_CFG_CTRL */
 #define QSYS_TAS_CFG_CTRL         __REG(TARGET_QSYS, 0, 1, 57372, 0, 1, 12, 0, 0, 1, 4)
 
diff --git a/drivers/net/ethernet/microchip/lan966x/lan966x_tbf.c b/drivers/net/ethernet/microchip/lan966x/lan966x_tbf.c
new file mode 100644 (file)
index 0000000..4555a35
--- /dev/null
@@ -0,0 +1,85 @@
+// SPDX-License-Identifier: GPL-2.0+
+
+#include "lan966x_main.h"
+
+int lan966x_tbf_add(struct lan966x_port *port,
+                   struct tc_tbf_qopt_offload *qopt)
+{
+       struct lan966x *lan966x = port->lan966x;
+       bool root = qopt->parent == TC_H_ROOT;
+       u32 queue = 0;
+       u32 cir, cbs;
+       u32 se_idx;
+
+       if (!root) {
+               queue = TC_H_MIN(qopt->parent) - 1;
+               if (queue >= NUM_PRIO_QUEUES)
+                       return -EOPNOTSUPP;
+       }
+
+       if (root)
+               se_idx = SE_IDX_PORT + port->chip_port;
+       else
+               se_idx = SE_IDX_QUEUE + port->chip_port * NUM_PRIO_QUEUES + queue;
+
+       cir = div_u64(qopt->replace_params.rate.rate_bytes_ps, 1000) * 8;
+       cbs = qopt->replace_params.max_size;
+
+       /* Rate unit is 100 kbps */
+       cir = DIV_ROUND_UP(cir, 100);
+       /* Avoid using zero rate */
+       cir = cir ?: 1;
+       /* Burst unit is 4kB */
+       cbs = DIV_ROUND_UP(cbs, 4096);
+       /* Avoid using zero burst */
+       cbs = cbs ?: 1;
+
+       /* Check that actually the result can be written */
+       if (cir > GENMASK(15, 0) ||
+           cbs > GENMASK(6, 0))
+               return -EINVAL;
+
+       lan_rmw(QSYS_SE_CFG_SE_AVB_ENA_SET(0) |
+               QSYS_SE_CFG_SE_FRM_MODE_SET(1),
+               QSYS_SE_CFG_SE_AVB_ENA |
+               QSYS_SE_CFG_SE_FRM_MODE,
+               lan966x, QSYS_SE_CFG(se_idx));
+
+       lan_wr(QSYS_CIR_CFG_CIR_RATE_SET(cir) |
+              QSYS_CIR_CFG_CIR_BURST_SET(cbs),
+              lan966x, QSYS_CIR_CFG(se_idx));
+
+       return 0;
+}
+
+int lan966x_tbf_del(struct lan966x_port *port,
+                   struct tc_tbf_qopt_offload *qopt)
+{
+       struct lan966x *lan966x = port->lan966x;
+       bool root = qopt->parent == TC_H_ROOT;
+       u32 queue = 0;
+       u32 se_idx;
+
+       if (!root) {
+               queue = TC_H_MIN(qopt->parent) - 1;
+               if (queue >= NUM_PRIO_QUEUES)
+                       return -EOPNOTSUPP;
+       }
+
+       if (root)
+               se_idx = SE_IDX_PORT + port->chip_port;
+       else
+               se_idx = SE_IDX_QUEUE + port->chip_port * NUM_PRIO_QUEUES + queue;
+
+       lan_rmw(QSYS_SE_CFG_SE_AVB_ENA_SET(0) |
+               QSYS_SE_CFG_SE_FRM_MODE_SET(0),
+               QSYS_SE_CFG_SE_AVB_ENA |
+               QSYS_SE_CFG_SE_FRM_MODE,
+               lan966x, QSYS_SE_CFG(se_idx));
+
+       lan_wr(QSYS_CIR_CFG_CIR_RATE_SET(0) |
+              QSYS_CIR_CFG_CIR_BURST_SET(0),
+              lan966x, QSYS_CIR_CFG(se_idx));
+
+       return 0;
+}
index cabc563..ca03b78 100644 (file)
@@ -22,6 +22,21 @@ static int lan966x_tc_setup_qdisc_taprio(struct lan966x_port *port,
                                lan966x_taprio_del(port);
 }
 
+static int lan966x_tc_setup_qdisc_tbf(struct lan966x_port *port,
+                                     struct tc_tbf_qopt_offload *qopt)
+{
+       switch (qopt->command) {
+       case TC_TBF_REPLACE:
+               return lan966x_tbf_add(port, qopt);
+       case TC_TBF_DESTROY:
+               return lan966x_tbf_del(port, qopt);
+       default:
+               return -EOPNOTSUPP;
+       }
+
+       return -EOPNOTSUPP;
+}
+
 int lan966x_tc_setup(struct net_device *dev, enum tc_setup_type type,
                     void *type_data)
 {
@@ -32,6 +47,8 @@ int lan966x_tc_setup(struct net_device *dev, enum tc_setup_type type,
                return lan966x_tc_setup_qdisc_mqprio(port, type_data);
        case TC_SETUP_QDISC_TAPRIO:
                return lan966x_tc_setup_qdisc_taprio(port, type_data);
+       case TC_SETUP_QDISC_TBF:
+               return lan966x_tc_setup_qdisc_tbf(port, type_data);
        default:
                return -EOPNOTSUPP;
        }