Skip to content

Commit 57d9014

Browse files
ikhorndavem330
authored andcommitted
net: ethernet: ti: cpsw: add CBS Qdisc offload
The cpsw has up to 4 FIFOs per port and upper 3 FIFOs can feed rate limited queue with shaping. In order to set and enable shaping for those 3 FIFOs queues the network device with CBS qdisc attached is needed. The CBS configuration is added for dual-emac/single port mode only, but potentially can be used in switch mode also, based on switchdev for instance. Despite the FIFO shapers can work w/o cpdma level shapers the base usage must be in combine with cpdma level shapers as described in TRM, that are set as maximum rates for interface queues with sysfs. One of the possible configuration with txq shapers and CBS shapers: Configured with echo RATE > /sys/class/net/eth0/queues/tx-0/tx_maxrate /--------------------------------------------------- / / cpdma level shapers +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ | c7 | | c6 | | c5 | | c4 | | c3 | | c2 | | c1 | | c0 | \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \/ \/ \/ \/ \/ \/ \/ \/ +---------|------|------|------|-------------------------------------+ | +----+ | | +---+ | | | +----+ | | | | v v v v | | +----+ +----+ +----+ +----+ p p+----+ +----+ +----+ +----+ | | | | | | | | | | o o| | | | | | | | | | | f3 | | f2 | | f1 | | f0 | r CPSW r| f3 | | f2 | | f1 | | f0 | | | | | | | | | | | t t| | | | | | | | | | \ / \ / \ / \ / 0 1\ / \ / \ / \ / | | \ X \ / \ / \ / \ / \ / \ / \ / | | \/ \ \/ \/ \/ \/ \/ \/ \/ | +-------\------------------------------------------------------------+ \ \ FIFO shaper, set with CBS offload added in this patch, \ FIFO0 cannot be rate limited ------------------------------------------------------ CBS shaper configuration is supposed to be used with root MQPRIO Qdisc offload allowing to add sk_prio->tc->txq maps that direct traffic to appropriate tx queue and maps L2 priority to FIFO shaper. The CBS shaper is intended to be used for AVB where L2 priority (pcp field) is used to differentiate class of traffic. So additionally vlan needs to be created with appropriate egress sk_prio->l2 prio map. If CBS has several tx queues assigned to it, the sum of their bandwidth has not overlap bandwidth set for CBS. It's recomended the CBS bandwidth to be a little bit more. The CBS shaper is configured with CBS qdisc offload interface using tc tool from iproute2 packet. For instance: $ tc qdisc replace dev eth0 handle 100: parent root mqprio num_tc 3 \ map 2 2 1 0 2 2 2 2 2 2 2 2 2 2 2 2 queues 1@0 1@1 2@2 hw 1 $ tc -g class show dev eth0 +---(100:ffe2) mqprio |    +---(100:3) mqprio |    +---(100:4) mqprio |     +---(100:ffe1) mqprio |    +---(100:2) mqprio |     +---(100:ffe0) mqprio     +---(100:1) mqprio $ tc qdisc add dev eth0 parent 100:1 cbs locredit -1440 \ hicredit 60 sendslope -960000 idleslope 40000 offload 1 $ tc qdisc add dev eth0 parent 100:2 cbs locredit -1470 \ hicredit 62 sendslope -980000 idleslope 20000 offload 1 The above code set CBS shapers for tc0 and tc1, for that txq0 and txq1 is used. Pay attention, the real set bandwidth can differ a bit due to discreteness of configuration parameters. Here parameters like locredit, hicredit and sendslope are ignored internally and are supposed to be set with assumption that maximum frame size for frame - 1500. It's supposed that interface speed is not changed while reconnection, not always is true, so inform user in case speed of interface was changed, as it can impact on dependent shapers configuration. For more examples see Documentation. Reviewed-by: Ilias Apalodimas <[email protected]> Reviewed-by: Grygorii Strashko <[email protected]> Signed-off-by: Ivan Khoronzhuk <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 7929a66 commit 57d9014

File tree

1 file changed

+221
-0
lines changed

1 file changed

+221
-0
lines changed

drivers/net/ethernet/ti/cpsw.c

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
#include "cpts.h"
4747
#include "davinci_cpdma.h"
4848

49+
#include <net/pkt_sched.h>
50+
4951
#define CPSW_DEBUG (NETIF_MSG_HW | NETIF_MSG_WOL | \
5052
NETIF_MSG_DRV | NETIF_MSG_LINK | \
5153
NETIF_MSG_IFUP | NETIF_MSG_INTR | \
@@ -154,8 +156,12 @@ do { \
154156
#define IRQ_NUM 2
155157
#define CPSW_MAX_QUEUES 8
156158
#define CPSW_CPDMA_DESCS_POOL_SIZE_DEFAULT 256
159+
#define CPSW_FIFO_QUEUE_TYPE_SHIFT 16
160+
#define CPSW_FIFO_SHAPE_EN_SHIFT 16
161+
#define CPSW_FIFO_RATE_EN_SHIFT 20
157162
#define CPSW_TC_NUM 4
158163
#define CPSW_FIFO_SHAPERS_NUM (CPSW_TC_NUM - 1)
164+
#define CPSW_PCT_MASK 0x7f
159165

160166
#define CPSW_RX_VLAN_ENCAP_HDR_PRIO_SHIFT 29
161167
#define CPSW_RX_VLAN_ENCAP_HDR_PRIO_MSK GENMASK(2, 0)
@@ -458,6 +464,8 @@ struct cpsw_priv {
458464
bool rx_pause;
459465
bool tx_pause;
460466
bool mqprio_hw;
467+
int fifo_bw[CPSW_TC_NUM];
468+
int shp_cfg_speed;
461469
u32 emac_port;
462470
struct cpsw_common *cpsw;
463471
};
@@ -1082,6 +1090,38 @@ static void cpsw_set_slave_mac(struct cpsw_slave *slave,
10821090
slave_write(slave, mac_lo(priv->mac_addr), SA_LO);
10831091
}
10841092

1093+
static bool cpsw_shp_is_off(struct cpsw_priv *priv)
1094+
{
1095+
struct cpsw_common *cpsw = priv->cpsw;
1096+
struct cpsw_slave *slave;
1097+
u32 shift, mask, val;
1098+
1099+
val = readl_relaxed(&cpsw->regs->ptype);
1100+
1101+
slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
1102+
shift = CPSW_FIFO_SHAPE_EN_SHIFT + 3 * slave->slave_num;
1103+
mask = 7 << shift;
1104+
val = val & mask;
1105+
1106+
return !val;
1107+
}
1108+
1109+
static void cpsw_fifo_shp_on(struct cpsw_priv *priv, int fifo, int on)
1110+
{
1111+
struct cpsw_common *cpsw = priv->cpsw;
1112+
struct cpsw_slave *slave;
1113+
u32 shift, mask, val;
1114+
1115+
val = readl_relaxed(&cpsw->regs->ptype);
1116+
1117+
slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
1118+
shift = CPSW_FIFO_SHAPE_EN_SHIFT + 3 * slave->slave_num;
1119+
mask = (1 << --fifo) << shift;
1120+
val = on ? val | mask : val & ~mask;
1121+
1122+
writel_relaxed(val, &cpsw->regs->ptype);
1123+
}
1124+
10851125
static void _cpsw_adjust_link(struct cpsw_slave *slave,
10861126
struct cpsw_priv *priv, bool *link)
10871127
{
@@ -1121,6 +1161,12 @@ static void _cpsw_adjust_link(struct cpsw_slave *slave,
11211161
mac_control |= BIT(4);
11221162

11231163
*link = true;
1164+
1165+
if (priv->shp_cfg_speed &&
1166+
priv->shp_cfg_speed != slave->phy->speed &&
1167+
!cpsw_shp_is_off(priv))
1168+
dev_warn(priv->dev,
1169+
"Speed was changed, CBS shaper speeds are changed!");
11241170
} else {
11251171
mac_control = 0;
11261172
/* disable forwarding */
@@ -1590,6 +1636,178 @@ static int cpsw_tc_to_fifo(int tc, int num_tc)
15901636
return CPSW_FIFO_SHAPERS_NUM - tc;
15911637
}
15921638

1639+
static int cpsw_set_fifo_bw(struct cpsw_priv *priv, int fifo, int bw)
1640+
{
1641+
struct cpsw_common *cpsw = priv->cpsw;
1642+
u32 val = 0, send_pct, shift;
1643+
struct cpsw_slave *slave;
1644+
int pct = 0, i;
1645+
1646+
if (bw > priv->shp_cfg_speed * 1000)
1647+
goto err;
1648+
1649+
/* shaping has to stay enabled for highest fifos linearly
1650+
* and fifo bw no more then interface can allow
1651+
*/
1652+
slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
1653+
send_pct = slave_read(slave, SEND_PERCENT);
1654+
for (i = CPSW_FIFO_SHAPERS_NUM; i > 0; i--) {
1655+
if (!bw) {
1656+
if (i >= fifo || !priv->fifo_bw[i])
1657+
continue;
1658+
1659+
dev_warn(priv->dev, "Prev FIFO%d is shaped", i);
1660+
continue;
1661+
}
1662+
1663+
if (!priv->fifo_bw[i] && i > fifo) {
1664+
dev_err(priv->dev, "Upper FIFO%d is not shaped", i);
1665+
return -EINVAL;
1666+
}
1667+
1668+
shift = (i - 1) * 8;
1669+
if (i == fifo) {
1670+
send_pct &= ~(CPSW_PCT_MASK << shift);
1671+
val = DIV_ROUND_UP(bw, priv->shp_cfg_speed * 10);
1672+
if (!val)
1673+
val = 1;
1674+
1675+
send_pct |= val << shift;
1676+
pct += val;
1677+
continue;
1678+
}
1679+
1680+
if (priv->fifo_bw[i])
1681+
pct += (send_pct >> shift) & CPSW_PCT_MASK;
1682+
}
1683+
1684+
if (pct >= 100)
1685+
goto err;
1686+
1687+
slave_write(slave, send_pct, SEND_PERCENT);
1688+
priv->fifo_bw[fifo] = bw;
1689+
1690+
dev_warn(priv->dev, "set FIFO%d bw = %d\n", fifo,
1691+
DIV_ROUND_CLOSEST(val * priv->shp_cfg_speed, 100));
1692+
1693+
return 0;
1694+
err:
1695+
dev_err(priv->dev, "Bandwidth doesn't fit in tc configuration");
1696+
return -EINVAL;
1697+
}
1698+
1699+
static int cpsw_set_fifo_rlimit(struct cpsw_priv *priv, int fifo, int bw)
1700+
{
1701+
struct cpsw_common *cpsw = priv->cpsw;
1702+
struct cpsw_slave *slave;
1703+
u32 tx_in_ctl_rg, val;
1704+
int ret;
1705+
1706+
ret = cpsw_set_fifo_bw(priv, fifo, bw);
1707+
if (ret)
1708+
return ret;
1709+
1710+
slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
1711+
tx_in_ctl_rg = cpsw->version == CPSW_VERSION_1 ?
1712+
CPSW1_TX_IN_CTL : CPSW2_TX_IN_CTL;
1713+
1714+
if (!bw)
1715+
cpsw_fifo_shp_on(priv, fifo, bw);
1716+
1717+
val = slave_read(slave, tx_in_ctl_rg);
1718+
if (cpsw_shp_is_off(priv)) {
1719+
/* disable FIFOs rate limited queues */
1720+
val &= ~(0xf << CPSW_FIFO_RATE_EN_SHIFT);
1721+
1722+
/* set type of FIFO queues to normal priority mode */
1723+
val &= ~(3 << CPSW_FIFO_QUEUE_TYPE_SHIFT);
1724+
1725+
/* set type of FIFO queues to be rate limited */
1726+
if (bw)
1727+
val |= 2 << CPSW_FIFO_QUEUE_TYPE_SHIFT;
1728+
else
1729+
priv->shp_cfg_speed = 0;
1730+
}
1731+
1732+
/* toggle a FIFO rate limited queue */
1733+
if (bw)
1734+
val |= BIT(fifo + CPSW_FIFO_RATE_EN_SHIFT);
1735+
else
1736+
val &= ~BIT(fifo + CPSW_FIFO_RATE_EN_SHIFT);
1737+
slave_write(slave, val, tx_in_ctl_rg);
1738+
1739+
/* FIFO transmit shape enable */
1740+
cpsw_fifo_shp_on(priv, fifo, bw);
1741+
return 0;
1742+
}
1743+
1744+
/* Defaults:
1745+
* class A - prio 3
1746+
* class B - prio 2
1747+
* shaping for class A should be set first
1748+
*/
1749+
static int cpsw_set_cbs(struct net_device *ndev,
1750+
struct tc_cbs_qopt_offload *qopt)
1751+
{
1752+
struct cpsw_priv *priv = netdev_priv(ndev);
1753+
struct cpsw_common *cpsw = priv->cpsw;
1754+
struct cpsw_slave *slave;
1755+
int prev_speed = 0;
1756+
int tc, ret, fifo;
1757+
u32 bw = 0;
1758+
1759+
tc = netdev_txq_to_tc(priv->ndev, qopt->queue);
1760+
1761+
/* enable channels in backward order, as highest FIFOs must be rate
1762+
* limited first and for compliance with CPDMA rate limited channels
1763+
* that also used in bacward order. FIFO0 cannot be rate limited.
1764+
*/
1765+
fifo = cpsw_tc_to_fifo(tc, ndev->num_tc);
1766+
if (!fifo) {
1767+
dev_err(priv->dev, "Last tc%d can't be rate limited", tc);
1768+
return -EINVAL;
1769+
}
1770+
1771+
/* do nothing, it's disabled anyway */
1772+
if (!qopt->enable && !priv->fifo_bw[fifo])
1773+
return 0;
1774+
1775+
/* shapers can be set if link speed is known */
1776+
slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
1777+
if (slave->phy && slave->phy->link) {
1778+
if (priv->shp_cfg_speed &&
1779+
priv->shp_cfg_speed != slave->phy->speed)
1780+
prev_speed = priv->shp_cfg_speed;
1781+
1782+
priv->shp_cfg_speed = slave->phy->speed;
1783+
}
1784+
1785+
if (!priv->shp_cfg_speed) {
1786+
dev_err(priv->dev, "Link speed is not known");
1787+
return -1;
1788+
}
1789+
1790+
ret = pm_runtime_get_sync(cpsw->dev);
1791+
if (ret < 0) {
1792+
pm_runtime_put_noidle(cpsw->dev);
1793+
return ret;
1794+
}
1795+
1796+
bw = qopt->enable ? qopt->idleslope : 0;
1797+
ret = cpsw_set_fifo_rlimit(priv, fifo, bw);
1798+
if (ret) {
1799+
priv->shp_cfg_speed = prev_speed;
1800+
prev_speed = 0;
1801+
}
1802+
1803+
if (bw && prev_speed)
1804+
dev_warn(priv->dev,
1805+
"Speed was changed, CBS shaper speeds are changed!");
1806+
1807+
pm_runtime_put_sync(cpsw->dev);
1808+
return ret;
1809+
}
1810+
15931811
static int cpsw_ndo_open(struct net_device *ndev)
15941812
{
15951813
struct cpsw_priv *priv = netdev_priv(ndev);
@@ -2264,6 +2482,9 @@ static int cpsw_ndo_setup_tc(struct net_device *ndev, enum tc_setup_type type,
22642482
void *type_data)
22652483
{
22662484
switch (type) {
2485+
case TC_SETUP_QDISC_CBS:
2486+
return cpsw_set_cbs(ndev, type_data);
2487+
22672488
case TC_SETUP_QDISC_MQPRIO:
22682489
return cpsw_set_mqprio(ndev, type_data);
22692490

0 commit comments

Comments
 (0)