OSDN Git Service

bfc77db18478c85000d2baac49c6ca0e4098bfde
[immortalwrt/immortalwrt.git] / target / linux / generic / backport-5.15 / 763-net-next-net-dsa-qca8k-add-LAG-support.patch
1 From def975307c01191b6f0170048c3724b0ed3348af Mon Sep 17 00:00:00 2001
2 From: Ansuel Smith <ansuelsmth@gmail.com>
3 Date: Tue, 23 Nov 2021 03:59:11 +0100
4 Subject: net: dsa: qca8k: add LAG support
5
6 Add LAG support to this switch. In Documentation this is described as
7 trunk mode. A max of 4 LAGs are supported and each can support up to 4
8 port. The current tx mode supported is Hash mode with both L2 and L2+3
9 mode.
10 When no port are present in the trunk, the trunk is disabled in the
11 switch.
12 When a port is disconnected, the traffic is redirected to the other
13 available port.
14 The hash mode is global and each LAG require to have the same hash mode
15 set. To change the hash mode when multiple LAG are configured, it's
16 required to remove each LAG and set the desired hash mode to the last.
17 An error is printed when it's asked to set a not supported hadh mode.
18
19 Signed-off-by: Ansuel Smith <ansuelsmth@gmail.com>
20 Signed-off-by: David S. Miller <davem@davemloft.net>
21 ---
22  drivers/net/dsa/qca8k.c | 177 ++++++++++++++++++++++++++++++++++++++++++++++++
23  drivers/net/dsa/qca8k.h |  33 +++++++++
24  2 files changed, 210 insertions(+)
25
26 --- a/drivers/net/dsa/qca8k.c
27 +++ b/drivers/net/dsa/qca8k.c
28 @@ -1340,6 +1340,9 @@ qca8k_setup(struct dsa_switch *ds)
29         ds->ageing_time_min = 7000;
30         ds->ageing_time_max = 458745000;
31  
32 +       /* Set max number of LAGs supported */
33 +       ds->num_lag_ids = QCA8K_NUM_LAGS;
34 +
35         return 0;
36  }
37  
38 @@ -2207,6 +2210,178 @@ qca8k_get_tag_protocol(struct dsa_switch
39         return DSA_TAG_PROTO_QCA;
40  }
41  
42 +static bool
43 +qca8k_lag_can_offload(struct dsa_switch *ds,
44 +                     struct net_device *lag,
45 +                     struct netdev_lag_upper_info *info)
46 +{
47 +       struct dsa_port *dp;
48 +       int id, members = 0;
49 +
50 +       id = dsa_lag_id(ds->dst, lag);
51 +       if (id < 0 || id >= ds->num_lag_ids)
52 +               return false;
53 +
54 +       dsa_lag_foreach_port(dp, ds->dst, lag)
55 +               /* Includes the port joining the LAG */
56 +               members++;
57 +
58 +       if (members > QCA8K_NUM_PORTS_FOR_LAG)
59 +               return false;
60 +
61 +       if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH)
62 +               return false;
63 +
64 +       if (info->hash_type != NETDEV_LAG_HASH_L2 ||
65 +           info->hash_type != NETDEV_LAG_HASH_L23)
66 +               return false;
67 +
68 +       return true;
69 +}
70 +
71 +static int
72 +qca8k_lag_setup_hash(struct dsa_switch *ds,
73 +                    struct net_device *lag,
74 +                    struct netdev_lag_upper_info *info)
75 +{
76 +       struct qca8k_priv *priv = ds->priv;
77 +       bool unique_lag = true;
78 +       int i, id;
79 +       u32 hash;
80 +
81 +       id = dsa_lag_id(ds->dst, lag);
82 +
83 +       switch (info->hash_type) {
84 +       case NETDEV_LAG_HASH_L23:
85 +               hash |= QCA8K_TRUNK_HASH_SIP_EN;
86 +               hash |= QCA8K_TRUNK_HASH_DIP_EN;
87 +               fallthrough;
88 +       case NETDEV_LAG_HASH_L2:
89 +               hash |= QCA8K_TRUNK_HASH_SA_EN;
90 +               hash |= QCA8K_TRUNK_HASH_DA_EN;
91 +               break;
92 +       default: /* We should NEVER reach this */
93 +               return -EOPNOTSUPP;
94 +       }
95 +
96 +       /* Check if we are the unique configured LAG */
97 +       dsa_lags_foreach_id(i, ds->dst)
98 +               if (i != id && dsa_lag_dev(ds->dst, i)) {
99 +                       unique_lag = false;
100 +                       break;
101 +               }
102 +
103 +       /* Hash Mode is global. Make sure the same Hash Mode
104 +        * is set to all the 4 possible lag.
105 +        * If we are the unique LAG we can set whatever hash
106 +        * mode we want.
107 +        * To change hash mode it's needed to remove all LAG
108 +        * and change the mode with the latest.
109 +        */
110 +       if (unique_lag) {
111 +               priv->lag_hash_mode = hash;
112 +       } else if (priv->lag_hash_mode != hash) {
113 +               netdev_err(lag, "Error: Mismateched Hash Mode across different lag is not supported\n");
114 +               return -EOPNOTSUPP;
115 +       }
116 +
117 +       return regmap_update_bits(priv->regmap, QCA8K_TRUNK_HASH_EN_CTRL,
118 +                                 QCA8K_TRUNK_HASH_MASK, hash);
119 +}
120 +
121 +static int
122 +qca8k_lag_refresh_portmap(struct dsa_switch *ds, int port,
123 +                         struct net_device *lag, bool delete)
124 +{
125 +       struct qca8k_priv *priv = ds->priv;
126 +       int ret, id, i;
127 +       u32 val;
128 +
129 +       id = dsa_lag_id(ds->dst, lag);
130 +
131 +       /* Read current port member */
132 +       ret = regmap_read(priv->regmap, QCA8K_REG_GOL_TRUNK_CTRL0, &val);
133 +       if (ret)
134 +               return ret;
135 +
136 +       /* Shift val to the correct trunk */
137 +       val >>= QCA8K_REG_GOL_TRUNK_SHIFT(id);
138 +       val &= QCA8K_REG_GOL_TRUNK_MEMBER_MASK;
139 +       if (delete)
140 +               val &= ~BIT(port);
141 +       else
142 +               val |= BIT(port);
143 +
144 +       /* Update port member. With empty portmap disable trunk */
145 +       ret = regmap_update_bits(priv->regmap, QCA8K_REG_GOL_TRUNK_CTRL0,
146 +                                QCA8K_REG_GOL_TRUNK_MEMBER(id) |
147 +                                QCA8K_REG_GOL_TRUNK_EN(id),
148 +                                !val << QCA8K_REG_GOL_TRUNK_SHIFT(id) |
149 +                                val << QCA8K_REG_GOL_TRUNK_SHIFT(id));
150 +
151 +       /* Search empty member if adding or port on deleting */
152 +       for (i = 0; i < QCA8K_NUM_PORTS_FOR_LAG; i++) {
153 +               ret = regmap_read(priv->regmap, QCA8K_REG_GOL_TRUNK_CTRL(id), &val);
154 +               if (ret)
155 +                       return ret;
156 +
157 +               val >>= QCA8K_REG_GOL_TRUNK_ID_MEM_ID_SHIFT(id, i);
158 +               val &= QCA8K_REG_GOL_TRUNK_ID_MEM_ID_MASK;
159 +
160 +               if (delete) {
161 +                       /* If port flagged to be disabled assume this member is
162 +                        * empty
163 +                        */
164 +                       if (val != QCA8K_REG_GOL_TRUNK_ID_MEM_ID_EN_MASK)
165 +                               continue;
166 +
167 +                       val &= QCA8K_REG_GOL_TRUNK_ID_MEM_ID_PORT_MASK;
168 +                       if (val != port)
169 +                               continue;
170 +               } else {
171 +                       /* If port flagged to be enabled assume this member is
172 +                        * already set
173 +                        */
174 +                       if (val == QCA8K_REG_GOL_TRUNK_ID_MEM_ID_EN_MASK)
175 +                               continue;
176 +               }
177 +
178 +               /* We have found the member to add/remove */
179 +               break;
180 +       }
181 +
182 +       /* Set port in the correct port mask or disable port if in delete mode */
183 +       return regmap_update_bits(priv->regmap, QCA8K_REG_GOL_TRUNK_CTRL(id),
184 +                                 QCA8K_REG_GOL_TRUNK_ID_MEM_ID_EN(id, i) |
185 +                                 QCA8K_REG_GOL_TRUNK_ID_MEM_ID_PORT(id, i),
186 +                                 !delete << QCA8K_REG_GOL_TRUNK_ID_MEM_ID_SHIFT(id, i) |
187 +                                 port << QCA8K_REG_GOL_TRUNK_ID_MEM_ID_SHIFT(id, i));
188 +}
189 +
190 +static int
191 +qca8k_port_lag_join(struct dsa_switch *ds, int port,
192 +                   struct net_device *lag,
193 +                   struct netdev_lag_upper_info *info)
194 +{
195 +       int ret;
196 +
197 +       if (!qca8k_lag_can_offload(ds, lag, info))
198 +               return -EOPNOTSUPP;
199 +
200 +       ret = qca8k_lag_setup_hash(ds, lag, info);
201 +       if (ret)
202 +               return ret;
203 +
204 +       return qca8k_lag_refresh_portmap(ds, port, lag, false);
205 +}
206 +
207 +static int
208 +qca8k_port_lag_leave(struct dsa_switch *ds, int port,
209 +                    struct net_device *lag)
210 +{
211 +       return qca8k_lag_refresh_portmap(ds, port, lag, true);
212 +}
213 +
214  static const struct dsa_switch_ops qca8k_switch_ops = {
215         .get_tag_protocol       = qca8k_get_tag_protocol,
216         .setup                  = qca8k_setup,
217 @@ -2240,6 +2415,8 @@ static const struct dsa_switch_ops qca8k
218         .phylink_mac_link_down  = qca8k_phylink_mac_link_down,
219         .phylink_mac_link_up    = qca8k_phylink_mac_link_up,
220         .get_phy_flags          = qca8k_get_phy_flags,
221 +       .port_lag_join          = qca8k_port_lag_join,
222 +       .port_lag_leave         = qca8k_port_lag_leave,
223  };
224  
225  static int qca8k_read_switch_id(struct qca8k_priv *priv)
226 --- a/drivers/net/dsa/qca8k.h
227 +++ b/drivers/net/dsa/qca8k.h
228 @@ -15,6 +15,8 @@
229  #define QCA8K_NUM_PORTS                                        7
230  #define QCA8K_NUM_CPU_PORTS                            2
231  #define QCA8K_MAX_MTU                                  9000
232 +#define QCA8K_NUM_LAGS                                 4
233 +#define QCA8K_NUM_PORTS_FOR_LAG                                4
234  
235  #define PHY_ID_QCA8327                                 0x004dd034
236  #define QCA8K_ID_QCA8327                               0x12
237 @@ -122,6 +124,14 @@
238  #define QCA8K_REG_EEE_CTRL                             0x100
239  #define  QCA8K_REG_EEE_CTRL_LPI_EN(_i)                 ((_i + 1) * 2)
240  
241 +/* TRUNK_HASH_EN registers */
242 +#define QCA8K_TRUNK_HASH_EN_CTRL                       0x270
243 +#define   QCA8K_TRUNK_HASH_SIP_EN                      BIT(3)
244 +#define   QCA8K_TRUNK_HASH_DIP_EN                      BIT(2)
245 +#define   QCA8K_TRUNK_HASH_SA_EN                       BIT(1)
246 +#define   QCA8K_TRUNK_HASH_DA_EN                       BIT(0)
247 +#define   QCA8K_TRUNK_HASH_MASK                                GENMASK(3, 0)
248 +
249  /* ACL registers */
250  #define QCA8K_REG_PORT_VLAN_CTRL0(_i)                  (0x420 + (_i * 8))
251  #define   QCA8K_PORT_VLAN_CVID_MASK                    GENMASK(27, 16)
252 @@ -204,6 +214,28 @@
253  #define   QCA8K_PORT_LOOKUP_LEARN                      BIT(20)
254  #define   QCA8K_PORT_LOOKUP_ING_MIRROR_EN              BIT(25)
255  
256 +#define QCA8K_REG_GOL_TRUNK_CTRL0                      0x700
257 +/* 4 max trunk first
258 + * first 6 bit for member bitmap
259 + * 7th bit is to enable trunk port
260 + */
261 +#define QCA8K_REG_GOL_TRUNK_SHIFT(_i)                  ((_i) * 8)
262 +#define QCA8K_REG_GOL_TRUNK_EN_MASK                    BIT(7)
263 +#define QCA8K_REG_GOL_TRUNK_EN(_i)                     (QCA8K_REG_GOL_TRUNK_EN_MASK << QCA8K_REG_GOL_TRUNK_SHIFT(_i))
264 +#define QCA8K_REG_GOL_TRUNK_MEMBER_MASK                        GENMASK(6, 0)
265 +#define QCA8K_REG_GOL_TRUNK_MEMBER(_i)                 (QCA8K_REG_GOL_TRUNK_MEMBER_MASK << QCA8K_REG_GOL_TRUNK_SHIFT(_i))
266 +/* 0x704 for TRUNK 0-1 --- 0x708 for TRUNK 2-3 */
267 +#define QCA8K_REG_GOL_TRUNK_CTRL(_i)                   (0x704 + (((_i) / 2) * 4))
268 +#define QCA8K_REG_GOL_TRUNK_ID_MEM_ID_MASK             GENMASK(3, 0)
269 +#define QCA8K_REG_GOL_TRUNK_ID_MEM_ID_EN_MASK          BIT(3)
270 +#define QCA8K_REG_GOL_TRUNK_ID_MEM_ID_PORT_MASK                GENMASK(2, 0)
271 +#define QCA8K_REG_GOL_TRUNK_ID_SHIFT(_i)               (((_i) / 2) * 16)
272 +#define QCA8K_REG_GOL_MEM_ID_SHIFT(_i)                 ((_i) * 4)
273 +/* Complex shift: FIRST shift for port THEN shift for trunk */
274 +#define QCA8K_REG_GOL_TRUNK_ID_MEM_ID_SHIFT(_i, _j)    (QCA8K_REG_GOL_MEM_ID_SHIFT(_j) + QCA8K_REG_GOL_TRUNK_ID_SHIFT(_i))
275 +#define QCA8K_REG_GOL_TRUNK_ID_MEM_ID_EN(_i, _j)       (QCA8K_REG_GOL_TRUNK_ID_MEM_ID_EN_MASK << QCA8K_REG_GOL_TRUNK_ID_MEM_ID_SHIFT(_i, _j))
276 +#define QCA8K_REG_GOL_TRUNK_ID_MEM_ID_PORT(_i, _j)     (QCA8K_REG_GOL_TRUNK_ID_MEM_ID_PORT_MASK << QCA8K_REG_GOL_TRUNK_ID_MEM_ID_SHIFT(_i, _j))
277 +
278  #define QCA8K_REG_GLOBAL_FC_THRESH                     0x800
279  #define   QCA8K_GLOBAL_FC_GOL_XON_THRES_MASK           GENMASK(24, 16)
280  #define   QCA8K_GLOBAL_FC_GOL_XON_THRES(x)             FIELD_PREP(QCA8K_GLOBAL_FC_GOL_XON_THRES_MASK, x)
281 @@ -309,6 +341,7 @@ struct qca8k_priv {
282         u8 switch_revision;
283         u8 mirror_rx;
284         u8 mirror_tx;
285 +       u8 lag_hash_mode;
286         bool legacy_phy_port_mapping;
287         struct qca8k_ports_config ports_config;
288         struct regmap *regmap;