762-v5.11-net-dsa-mt7530-support-setting-MTU.patch 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. From 9470174e7581e75a8ebd78964997314dfc2e706c Mon Sep 17 00:00:00 2001
  2. From: DENG Qingfang <[email protected]>
  3. Date: Tue, 3 Nov 2020 13:06:18 +0800
  4. Subject: [PATCH] net: dsa: mt7530: support setting MTU
  5. MT7530/7531 has a global RX packet length register, which can be used
  6. to set MTU.
  7. Supported packet length values are 1522 (1518 if untagged), 1536,
  8. 1552, and multiple of 1024 (from 2048 to 15360).
  9. Signed-off-by: DENG Qingfang <[email protected]>
  10. Link: https://lore.kernel.org/r/[email protected]
  11. Signed-off-by: Jakub Kicinski <[email protected]>
  12. ---
  13. drivers/net/dsa/mt7530.c | 49 ++++++++++++++++++++++++++++++++++++++++
  14. drivers/net/dsa/mt7530.h | 12 ++++++++++
  15. 2 files changed, 61 insertions(+)
  16. --- a/drivers/net/dsa/mt7530.c
  17. +++ b/drivers/net/dsa/mt7530.c
  18. @@ -1015,6 +1015,53 @@ mt7530_port_disable(struct dsa_switch *d
  19. mutex_unlock(&priv->reg_mutex);
  20. }
  21. +static int
  22. +mt7530_port_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
  23. +{
  24. + struct mt7530_priv *priv = ds->priv;
  25. + struct mii_bus *bus = priv->bus;
  26. + int length;
  27. + u32 val;
  28. +
  29. + /* When a new MTU is set, DSA always set the CPU port's MTU to the
  30. + * largest MTU of the slave ports. Because the switch only has a global
  31. + * RX length register, only allowing CPU port here is enough.
  32. + */
  33. + if (!dsa_is_cpu_port(ds, port))
  34. + return 0;
  35. +
  36. + mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
  37. +
  38. + val = mt7530_mii_read(priv, MT7530_GMACCR);
  39. + val &= ~MAX_RX_PKT_LEN_MASK;
  40. +
  41. + /* RX length also includes Ethernet header, MTK tag, and FCS length */
  42. + length = new_mtu + ETH_HLEN + MTK_HDR_LEN + ETH_FCS_LEN;
  43. + if (length <= 1522) {
  44. + val |= MAX_RX_PKT_LEN_1522;
  45. + } else if (length <= 1536) {
  46. + val |= MAX_RX_PKT_LEN_1536;
  47. + } else if (length <= 1552) {
  48. + val |= MAX_RX_PKT_LEN_1552;
  49. + } else {
  50. + val &= ~MAX_RX_JUMBO_MASK;
  51. + val |= MAX_RX_JUMBO(DIV_ROUND_UP(length, 1024));
  52. + val |= MAX_RX_PKT_LEN_JUMBO;
  53. + }
  54. +
  55. + mt7530_mii_write(priv, MT7530_GMACCR, val);
  56. +
  57. + mutex_unlock(&bus->mdio_lock);
  58. +
  59. + return 0;
  60. +}
  61. +
  62. +static int
  63. +mt7530_port_max_mtu(struct dsa_switch *ds, int port)
  64. +{
  65. + return MT7530_MAX_MTU;
  66. +}
  67. +
  68. static void
  69. mt7530_stp_state_set(struct dsa_switch *ds, int port, u8 state)
  70. {
  71. @@ -2647,6 +2694,8 @@ static const struct dsa_switch_ops mt753
  72. .get_sset_count = mt7530_get_sset_count,
  73. .port_enable = mt7530_port_enable,
  74. .port_disable = mt7530_port_disable,
  75. + .port_change_mtu = mt7530_port_change_mtu,
  76. + .port_max_mtu = mt7530_port_max_mtu,
  77. .port_stp_state_set = mt7530_stp_state_set,
  78. .port_bridge_join = mt7530_port_bridge_join,
  79. .port_bridge_leave = mt7530_port_bridge_leave,
  80. --- a/drivers/net/dsa/mt7530.h
  81. +++ b/drivers/net/dsa/mt7530.h
  82. @@ -11,6 +11,9 @@
  83. #define MT7530_NUM_FDB_RECORDS 2048
  84. #define MT7530_ALL_MEMBERS 0xff
  85. +#define MTK_HDR_LEN 4
  86. +#define MT7530_MAX_MTU (15 * 1024 - ETH_HLEN - ETH_FCS_LEN - MTK_HDR_LEN)
  87. +
  88. enum mt753x_id {
  89. ID_MT7530 = 0,
  90. ID_MT7621 = 1,
  91. @@ -301,6 +304,15 @@ enum mt7530_vlan_port_attr {
  92. #define MT7531_DBG_CNT(x) (0x3018 + (x) * 0x100)
  93. #define MT7531_DIS_CLR BIT(31)
  94. +#define MT7530_GMACCR 0x30e0
  95. +#define MAX_RX_JUMBO(x) ((x) << 2)
  96. +#define MAX_RX_JUMBO_MASK GENMASK(5, 2)
  97. +#define MAX_RX_PKT_LEN_MASK GENMASK(1, 0)
  98. +#define MAX_RX_PKT_LEN_1522 0x0
  99. +#define MAX_RX_PKT_LEN_1536 0x1
  100. +#define MAX_RX_PKT_LEN_1552 0x2
  101. +#define MAX_RX_PKT_LEN_JUMBO 0x3
  102. +
  103. /* Register for MIB */
  104. #define MT7530_PORT_MIB_COUNTER(x) (0x4000 + (x) * 0x100)
  105. #define MT7530_MIB_CCR 0x4fe0