ath79/mikrotik: use routerbootpart partitions
[oweals/openwrt.git] / target / linux / layerscape / patches-5.4 / 701-net-0240-net-mscc-ocelot-break-out-fdb-operations-into-abstra.patch
1 From 0d1866b8c6f17a55207be651a3b3b93879fbdf1f Mon Sep 17 00:00:00 2001
2 From: Vladimir Oltean <vladimir.oltean@nxp.com>
3 Date: Sat, 9 Nov 2019 15:02:49 +0200
4 Subject: [PATCH] net: mscc: ocelot: break out fdb operations into abstract
5  implementations
6
7 To be able to implement a DSA front-end over ocelot_fdb_add,
8 ocelot_fdb_del, ocelot_fdb_dump, these need to have a simple function
9 prototype that is independent of struct net_device, netlink skb, etc.
10
11 So rename the ndo ops of the ocelot driver into
12 ocelot_port_fdb_{add,del,dump}, and have them all call the abstract
13 implementations. At the same time, refactor ocelot_port_fdb_do_dump into
14 a function whose prototype is compatible with dsa_fdb_dump_cb_t, so that
15 the do_dump implementations can live together and be called by the
16 ocelot_fdb_dump through a function pointer.
17
18 Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
19 Signed-off-by: David S. Miller <davem@davemloft.net>
20 ---
21  drivers/net/ethernet/mscc/ocelot.c | 124 +++++++++++++++++++++++--------------
22  1 file changed, 78 insertions(+), 46 deletions(-)
23
24 --- a/drivers/net/ethernet/mscc/ocelot.c
25 +++ b/drivers/net/ethernet/mscc/ocelot.c
26 @@ -21,6 +21,7 @@
27  #include <net/netevent.h>
28  #include <net/rtnetlink.h>
29  #include <net/switchdev.h>
30 +#include <net/dsa.h>
31  
32  #include "ocelot.h"
33  #include "ocelot_ace.h"
34 @@ -814,21 +815,18 @@ static void ocelot_get_stats64(struct ne
35         stats->collisions = ocelot_read(ocelot, SYS_COUNT_TX_COLLISION);
36  }
37  
38 -static int ocelot_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
39 -                         struct net_device *dev, const unsigned char *addr,
40 -                         u16 vid, u16 flags,
41 -                         struct netlink_ext_ack *extack)
42 +static int ocelot_fdb_add(struct ocelot *ocelot, int port,
43 +                         const unsigned char *addr, u16 vid)
44  {
45 -       struct ocelot_port *port = netdev_priv(dev);
46 -       struct ocelot *ocelot = port->ocelot;
47 +       struct ocelot_port *ocelot_port = ocelot->ports[port];
48  
49         if (!vid) {
50 -               if (!port->vlan_aware)
51 +               if (!ocelot_port->vlan_aware)
52                         /* If the bridge is not VLAN aware and no VID was
53                          * provided, set it to pvid to ensure the MAC entry
54                          * matches incoming untagged packets
55                          */
56 -                       vid = port->pvid;
57 +                       vid = ocelot_port->pvid;
58                 else
59                         /* If the bridge is VLAN aware a VID must be provided as
60                          * otherwise the learnt entry wouldn't match any frame.
61 @@ -836,20 +834,37 @@ static int ocelot_fdb_add(struct ndmsg *
62                         return -EINVAL;
63         }
64  
65 -       return ocelot_mact_learn(ocelot, port->chip_port, addr, vid,
66 -                                ENTRYTYPE_LOCKED);
67 +       return ocelot_mact_learn(ocelot, port, addr, vid, ENTRYTYPE_LOCKED);
68  }
69  
70 -static int ocelot_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
71 -                         struct net_device *dev,
72 -                         const unsigned char *addr, u16 vid)
73 +static int ocelot_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
74 +                              struct net_device *dev,
75 +                              const unsigned char *addr,
76 +                              u16 vid, u16 flags,
77 +                              struct netlink_ext_ack *extack)
78  {
79 -       struct ocelot_port *port = netdev_priv(dev);
80 -       struct ocelot *ocelot = port->ocelot;
81 +       struct ocelot_port *ocelot_port = netdev_priv(dev);
82 +       struct ocelot *ocelot = ocelot_port->ocelot;
83  
84 +       return ocelot_fdb_add(ocelot, ocelot_port->chip_port, addr, vid);
85 +}
86 +
87 +static int ocelot_fdb_del(struct ocelot *ocelot, int port,
88 +                         const unsigned char *addr, u16 vid)
89 +{
90         return ocelot_mact_forget(ocelot, addr, vid);
91  }
92  
93 +static int ocelot_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
94 +                              struct net_device *dev,
95 +                              const unsigned char *addr, u16 vid)
96 +{
97 +       struct ocelot_port *ocelot_port = netdev_priv(dev);
98 +       struct ocelot *ocelot = ocelot_port->ocelot;
99 +
100 +       return ocelot_fdb_del(ocelot, ocelot_port->chip_port, addr, vid);
101 +}
102 +
103  struct ocelot_dump_ctx {
104         struct net_device *dev;
105         struct sk_buff *skb;
106 @@ -857,9 +872,10 @@ struct ocelot_dump_ctx {
107         int idx;
108  };
109  
110 -static int ocelot_fdb_do_dump(struct ocelot_mact_entry *entry,
111 -                             struct ocelot_dump_ctx *dump)
112 +static int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid,
113 +                                  bool is_static, void *data)
114  {
115 +       struct ocelot_dump_ctx *dump = data;
116         u32 portid = NETLINK_CB(dump->cb->skb).portid;
117         u32 seq = dump->cb->nlh->nlmsg_seq;
118         struct nlmsghdr *nlh;
119 @@ -880,12 +896,12 @@ static int ocelot_fdb_do_dump(struct oce
120         ndm->ndm_flags   = NTF_SELF;
121         ndm->ndm_type    = 0;
122         ndm->ndm_ifindex = dump->dev->ifindex;
123 -       ndm->ndm_state   = NUD_REACHABLE;
124 +       ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
125  
126 -       if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, entry->mac))
127 +       if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
128                 goto nla_put_failure;
129  
130 -       if (entry->vid && nla_put_u16(dump->skb, NDA_VLAN, entry->vid))
131 +       if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
132                 goto nla_put_failure;
133  
134         nlmsg_end(dump->skb, nlh);
135 @@ -899,12 +915,11 @@ nla_put_failure:
136         return -EMSGSIZE;
137  }
138  
139 -static inline int ocelot_mact_read(struct ocelot_port *port, int row, int col,
140 -                                  struct ocelot_mact_entry *entry)
141 +static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
142 +                           struct ocelot_mact_entry *entry)
143  {
144 -       struct ocelot *ocelot = port->ocelot;
145 -       char mac[ETH_ALEN];
146         u32 val, dst, macl, mach;
147 +       char mac[ETH_ALEN];
148  
149         /* Set row and column to read from */
150         ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
151 @@ -927,7 +942,7 @@ static inline int ocelot_mact_read(struc
152          * do not report it.
153          */
154         dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
155 -       if (dst != port->chip_port)
156 +       if (dst != port)
157                 return -EINVAL;
158  
159         /* Get the entry's MAC address and VLAN id */
160 @@ -947,43 +962,60 @@ static inline int ocelot_mact_read(struc
161         return 0;
162  }
163  
164 -static int ocelot_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
165 -                          struct net_device *dev,
166 -                          struct net_device *filter_dev, int *idx)
167 +static int ocelot_fdb_dump(struct ocelot *ocelot, int port,
168 +                          dsa_fdb_dump_cb_t *cb, void *data)
169  {
170 -       struct ocelot_port *port = netdev_priv(dev);
171 -       int i, j, ret = 0;
172 -       struct ocelot_dump_ctx dump = {
173 -               .dev = dev,
174 -               .skb = skb,
175 -               .cb = cb,
176 -               .idx = *idx,
177 -       };
178 -
179 -       struct ocelot_mact_entry entry;
180 +       int i, j;
181  
182         /* Loop through all the mac tables entries. There are 1024 rows of 4
183          * entries.
184          */
185         for (i = 0; i < 1024; i++) {
186                 for (j = 0; j < 4; j++) {
187 -                       ret = ocelot_mact_read(port, i, j, &entry);
188 +                       struct ocelot_mact_entry entry;
189 +                       bool is_static;
190 +                       int ret;
191 +
192 +                       ret = ocelot_mact_read(ocelot, port, i, j, &entry);
193                         /* If the entry is invalid (wrong port, invalid...),
194                          * skip it.
195                          */
196                         if (ret == -EINVAL)
197                                 continue;
198                         else if (ret)
199 -                               goto end;
200 +                               return ret;
201 +
202 +                       is_static = (entry.type == ENTRYTYPE_LOCKED);
203  
204 -                       ret = ocelot_fdb_do_dump(&entry, &dump);
205 +                       ret = cb(entry.mac, entry.vid, is_static, data);
206                         if (ret)
207 -                               goto end;
208 +                               return ret;
209                 }
210         }
211  
212 -end:
213 +       return 0;
214 +}
215 +
216 +static int ocelot_port_fdb_dump(struct sk_buff *skb,
217 +                               struct netlink_callback *cb,
218 +                               struct net_device *dev,
219 +                               struct net_device *filter_dev, int *idx)
220 +{
221 +       struct ocelot_port *ocelot_port = netdev_priv(dev);
222 +       struct ocelot *ocelot = ocelot_port->ocelot;
223 +       struct ocelot_dump_ctx dump = {
224 +               .dev = dev,
225 +               .skb = skb,
226 +               .cb = cb,
227 +               .idx = *idx,
228 +       };
229 +       int ret;
230 +
231 +       ret = ocelot_fdb_dump(ocelot, ocelot_port->chip_port,
232 +                             ocelot_port_fdb_do_dump, &dump);
233 +
234         *idx = dump.idx;
235 +
236         return ret;
237  }
238  
239 @@ -1129,9 +1161,9 @@ static const struct net_device_ops ocelo
240         .ndo_get_phys_port_name         = ocelot_port_get_phys_port_name,
241         .ndo_set_mac_address            = ocelot_port_set_mac_address,
242         .ndo_get_stats64                = ocelot_get_stats64,
243 -       .ndo_fdb_add                    = ocelot_fdb_add,
244 -       .ndo_fdb_del                    = ocelot_fdb_del,
245 -       .ndo_fdb_dump                   = ocelot_fdb_dump,
246 +       .ndo_fdb_add                    = ocelot_port_fdb_add,
247 +       .ndo_fdb_del                    = ocelot_port_fdb_del,
248 +       .ndo_fdb_dump                   = ocelot_port_fdb_dump,
249         .ndo_vlan_rx_add_vid            = ocelot_vlan_rx_add_vid,
250         .ndo_vlan_rx_kill_vid           = ocelot_vlan_rx_kill_vid,
251         .ndo_set_features               = ocelot_set_features,