blob: e53c8ca6eb6612093c9d128f6e7b6392049fc93b [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00002/*
3 * net/dsa/slave.c - Slave device handling
Lennert Buytenheke84665c2009-03-20 09:52:09 +00004 * Copyright (c) 2008-2009 Marvell Semiconductor
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00005 */
6
7#include <linux/list.h>
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -08008#include <linux/etherdevice.h>
Florian Fainellib73adef2015-02-24 13:15:33 -08009#include <linux/netdevice.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000010#include <linux/phy.h>
Florian Fainellia2820542014-10-17 16:02:13 -070011#include <linux/phy_fixed.h>
Florian Fainelliaab9c402018-05-10 13:17:36 -070012#include <linux/phylink.h>
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070013#include <linux/of_net.h>
14#include <linux/of_mdio.h>
Andrew Lunn7f854422016-01-06 20:11:18 +010015#include <linux/mdio.h>
Florian Fainellib73adef2015-02-24 13:15:33 -080016#include <net/rtnetlink.h>
Florian Fainellif50f2122017-01-30 12:41:40 -080017#include <net/pkt_cls.h>
18#include <net/tc_act/tc_mirred.h>
Florian Fainellib73adef2015-02-24 13:15:33 -080019#include <linux/if_bridge.h>
Florian Fainelli04ff53f2015-07-31 11:42:57 -070020#include <linux/netpoll.h>
Brandon Streiff90af1052018-02-14 01:07:49 +010021#include <linux/ptp_classify.h>
Vivien Didelotea5dd342017-05-17 15:46:03 -040022
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000023#include "dsa_priv.h"
24
25/* slave mii_bus handling ***************************************************/
26static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
27{
28 struct dsa_switch *ds = bus->priv;
29
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070030 if (ds->phys_mii_mask & (1 << addr))
Vivien Didelot9d490b42016-08-23 12:38:56 -040031 return ds->ops->phy_read(ds, addr, reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000032
33 return 0xffff;
34}
35
36static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
37{
38 struct dsa_switch *ds = bus->priv;
39
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070040 if (ds->phys_mii_mask & (1 << addr))
Vivien Didelot9d490b42016-08-23 12:38:56 -040041 return ds->ops->phy_write(ds, addr, reg, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000042
43 return 0;
44}
45
46void dsa_slave_mii_bus_init(struct dsa_switch *ds)
47{
48 ds->slave_mii_bus->priv = (void *)ds;
49 ds->slave_mii_bus->name = "dsa slave smi";
50 ds->slave_mii_bus->read = dsa_slave_phy_read;
51 ds->slave_mii_bus->write = dsa_slave_phy_write;
Florian Fainelli0b7b4982016-06-07 16:32:38 -070052 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
Vivien Didelot49463b72017-11-03 19:05:21 -040053 ds->dst->index, ds->index);
Andrew Lunnc33063d2016-05-10 23:27:23 +020054 ds->slave_mii_bus->parent = ds->dev;
Vivien Didelot24df8982015-01-20 19:13:32 -050055 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000056}
57
58
59/* slave device handling ****************************************************/
Nicolas Dichtelabd2be02015-04-02 17:07:08 +020060static int dsa_slave_get_iflink(const struct net_device *dev)
Lennert Buytenhekc0840802009-03-20 09:49:49 +000061{
Vivien Didelotd0006b02017-10-16 11:12:16 -040062 return dsa_slave_to_master(dev)->ifindex;
Lennert Buytenhekc0840802009-03-20 09:49:49 +000063}
64
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000065static int dsa_slave_open(struct net_device *dev)
66{
Vivien Didelotd0006b02017-10-16 11:12:16 -040067 struct net_device *master = dsa_slave_to_master(dev);
Vivien Didelotd9450972017-10-16 11:12:15 -040068 struct dsa_port *dp = dsa_slave_to_port(dev);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080069 int err;
70
71 if (!(master->flags & IFF_UP))
72 return -ENETDOWN;
73
Joe Perches8feedbb2012-05-08 18:56:57 +000074 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
Jiri Pirkoa748ee22010-04-01 21:22:09 +000075 err = dev_uc_add(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080076 if (err < 0)
77 goto out;
78 }
79
80 if (dev->flags & IFF_ALLMULTI) {
81 err = dev_set_allmulti(master, 1);
82 if (err < 0)
83 goto del_unicast;
84 }
85 if (dev->flags & IFF_PROMISC) {
86 err = dev_set_promiscuity(master, 1);
87 if (err < 0)
88 goto clear_allmulti;
89 }
90
Russell King8640f8d2020-03-03 15:01:46 +000091 err = dsa_port_enable_rt(dp, dev->phydev);
Vivien Didelotfb8a6a22017-09-22 19:01:56 -040092 if (err)
93 goto clear_promisc;
Florian Fainellib73adef2015-02-24 13:15:33 -080094
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000095 return 0;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080096
Florian Fainellib2f2af22014-09-24 17:05:18 -070097clear_promisc:
98 if (dev->flags & IFF_PROMISC)
Gilad Ben-Yossef4fdeddf2015-06-25 16:50:13 +030099 dev_set_promiscuity(master, -1);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800100clear_allmulti:
101 if (dev->flags & IFF_ALLMULTI)
102 dev_set_allmulti(master, -1);
103del_unicast:
Joe Perches8feedbb2012-05-08 18:56:57 +0000104 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000105 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800106out:
107 return err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000108}
109
110static int dsa_slave_close(struct net_device *dev)
111{
Vivien Didelotd0006b02017-10-16 11:12:16 -0400112 struct net_device *master = dsa_slave_to_master(dev);
Vivien Didelotd9450972017-10-16 11:12:15 -0400113 struct dsa_port *dp = dsa_slave_to_port(dev);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800114
Russell King8640f8d2020-03-03 15:01:46 +0000115 dsa_port_disable_rt(dp);
Vivien Didelot6457edf2017-09-22 19:01:55 -0400116
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800117 dev_mc_unsync(master, dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000118 dev_uc_unsync(master, dev);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800119 if (dev->flags & IFF_ALLMULTI)
120 dev_set_allmulti(master, -1);
121 if (dev->flags & IFF_PROMISC)
122 dev_set_promiscuity(master, -1);
123
Joe Perches8feedbb2012-05-08 18:56:57 +0000124 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000125 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800126
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000127 return 0;
128}
129
130static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
131{
Vivien Didelotd0006b02017-10-16 11:12:16 -0400132 struct net_device *master = dsa_slave_to_master(dev);
Rundong Ge17ab4f62019-02-02 14:29:35 +0000133 if (dev->flags & IFF_UP) {
134 if (change & IFF_ALLMULTI)
135 dev_set_allmulti(master,
136 dev->flags & IFF_ALLMULTI ? 1 : -1);
137 if (change & IFF_PROMISC)
138 dev_set_promiscuity(master,
139 dev->flags & IFF_PROMISC ? 1 : -1);
140 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000141}
142
143static void dsa_slave_set_rx_mode(struct net_device *dev)
144{
Vivien Didelotd0006b02017-10-16 11:12:16 -0400145 struct net_device *master = dsa_slave_to_master(dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000146
147 dev_mc_sync(master, dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000148 dev_uc_sync(master, dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000149}
150
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800151static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000152{
Vivien Didelotd0006b02017-10-16 11:12:16 -0400153 struct net_device *master = dsa_slave_to_master(dev);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800154 struct sockaddr *addr = a;
155 int err;
156
157 if (!is_valid_ether_addr(addr->sa_data))
158 return -EADDRNOTAVAIL;
159
160 if (!(dev->flags & IFF_UP))
161 goto out;
162
Joe Perches8feedbb2012-05-08 18:56:57 +0000163 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000164 err = dev_uc_add(master, addr->sa_data);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800165 if (err < 0)
166 return err;
167 }
168
Joe Perches8feedbb2012-05-08 18:56:57 +0000169 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000170 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800171
172out:
Joe Perchesd08f1612014-01-20 09:52:20 -0800173 ether_addr_copy(dev->dev_addr, addr->sa_data);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000174
175 return 0;
176}
177
Arkadi Sharshevsky2bedde12017-08-06 16:15:49 +0300178struct dsa_slave_dump_ctx {
179 struct net_device *dev;
180 struct sk_buff *skb;
181 struct netlink_callback *cb;
182 int idx;
183};
184
185static int
186dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid,
187 bool is_static, void *data)
188{
189 struct dsa_slave_dump_ctx *dump = data;
190 u32 portid = NETLINK_CB(dump->cb->skb).portid;
191 u32 seq = dump->cb->nlh->nlmsg_seq;
192 struct nlmsghdr *nlh;
193 struct ndmsg *ndm;
194
195 if (dump->idx < dump->cb->args[2])
196 goto skip;
197
198 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
199 sizeof(*ndm), NLM_F_MULTI);
200 if (!nlh)
201 return -EMSGSIZE;
202
203 ndm = nlmsg_data(nlh);
204 ndm->ndm_family = AF_BRIDGE;
205 ndm->ndm_pad1 = 0;
206 ndm->ndm_pad2 = 0;
207 ndm->ndm_flags = NTF_SELF;
208 ndm->ndm_type = 0;
209 ndm->ndm_ifindex = dump->dev->ifindex;
210 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
211
212 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
213 goto nla_put_failure;
214
215 if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
216 goto nla_put_failure;
217
218 nlmsg_end(dump->skb, nlh);
219
220skip:
221 dump->idx++;
222 return 0;
223
224nla_put_failure:
225 nlmsg_cancel(dump->skb, nlh);
226 return -EMSGSIZE;
227}
228
229static int
230dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
231 struct net_device *dev, struct net_device *filter_dev,
232 int *idx)
233{
Vivien Didelotd9450972017-10-16 11:12:15 -0400234 struct dsa_port *dp = dsa_slave_to_port(dev);
Arkadi Sharshevsky2bedde12017-08-06 16:15:49 +0300235 struct dsa_slave_dump_ctx dump = {
236 .dev = dev,
237 .skb = skb,
238 .cb = cb,
239 .idx = *idx,
240 };
Arkadi Sharshevsky2bedde12017-08-06 16:15:49 +0300241 int err;
242
Vivien Didelotde40fc52017-09-20 19:32:14 -0400243 err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump);
Arkadi Sharshevsky2bedde12017-08-06 16:15:49 +0300244 *idx = dump.idx;
Vivien Didelotde40fc52017-09-20 19:32:14 -0400245
Arkadi Sharshevsky2bedde12017-08-06 16:15:49 +0300246 return err;
247}
248
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000249static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
250{
Brandon Streiff03363692018-02-14 01:07:48 +0100251 struct dsa_slave_priv *p = netdev_priv(dev);
252 struct dsa_switch *ds = p->dp->ds;
253 int port = p->dp->index;
254
255 /* Pass through to switch driver if it supports timestamping */
256 switch (cmd) {
257 case SIOCGHWTSTAMP:
258 if (ds->ops->port_hwtstamp_get)
259 return ds->ops->port_hwtstamp_get(ds, port, ifr);
260 break;
261 case SIOCSHWTSTAMP:
262 if (ds->ops->port_hwtstamp_set)
263 return ds->ops->port_hwtstamp_set(ds, port, ifr);
264 break;
265 }
266
Florian Fainelliaab9c402018-05-10 13:17:36 -0700267 return phylink_mii_ioctl(p->dp->pl, ifr, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000268}
269
Scott Feldman35636062015-05-10 09:47:51 -0700270static int dsa_slave_port_attr_set(struct net_device *dev,
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200271 const struct switchdev_attr *attr)
Scott Feldman35636062015-05-10 09:47:51 -0700272{
Vivien Didelotd9450972017-10-16 11:12:15 -0400273 struct dsa_port *dp = dsa_slave_to_port(dev);
Vivien Didelotb8d866a2015-09-29 12:38:36 -0400274 int ret;
Scott Feldman35636062015-05-10 09:47:51 -0700275
Tobias Waldekranz5696c8a2021-01-13 09:42:52 +0100276 if (attr->orig_dev != dev)
277 return -EOPNOTSUPP;
278
Scott Feldman35636062015-05-10 09:47:51 -0700279 switch (attr->id) {
Jiri Pirko1f868392015-10-01 11:03:42 +0200280 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200281 ret = dsa_port_set_state(dp, attr->u.stp_state);
Scott Feldman35636062015-05-10 09:47:51 -0700282 break;
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500283 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200284 ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering);
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500285 break;
Vivien Didelot34a79f62016-07-18 20:45:38 -0400286 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200287 ret = dsa_port_ageing_time(dp, attr->u.ageing_time);
Vivien Didelot34a79f62016-07-18 20:45:38 -0400288 break;
Florian Fainelliea870052019-02-20 16:58:22 -0800289 case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200290 ret = dsa_port_pre_bridge_flags(dp, attr->u.brport_flags);
Florian Fainelliea870052019-02-20 16:58:22 -0800291 break;
Russell King57652792019-02-20 15:35:04 -0800292 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200293 ret = dsa_port_bridge_flags(dp, attr->u.brport_flags);
Russell King57652792019-02-20 15:35:04 -0800294 break;
Vivien Didelot08cc83c2019-07-08 23:31:13 -0400295 case SWITCHDEV_ATTR_ID_BRIDGE_MROUTER:
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200296 ret = dsa_port_mrouter(dp->cpu_dp, attr->u.mrouter);
Vivien Didelot08cc83c2019-07-08 23:31:13 -0400297 break;
Scott Feldman35636062015-05-10 09:47:51 -0700298 default:
299 ret = -EOPNOTSUPP;
300 break;
301 }
302
303 return ret;
304}
305
Vladimir Oltean1ce39f02020-09-21 03:10:26 +0300306/* Must be called under rcu_read_lock() */
307static int
308dsa_slave_vlan_check_for_8021q_uppers(struct net_device *slave,
309 const struct switchdev_obj_port_vlan *vlan)
310{
311 struct net_device *upper_dev;
312 struct list_head *iter;
313
314 netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) {
315 u16 vid;
316
317 if (!is_vlan_dev(upper_dev))
318 continue;
319
320 vid = vlan_dev_vlan_id(upper_dev);
Vladimir Olteanb7a9e0d2021-01-09 02:01:46 +0200321 if (vid == vlan->vid)
Vladimir Oltean1ce39f02020-09-21 03:10:26 +0300322 return -EBUSY;
323 }
324
325 return 0;
326}
327
Vivien Didelotbdcff082019-08-25 13:25:17 -0400328static int dsa_slave_vlan_add(struct net_device *dev,
Vladimir Olteanffb68fc2021-01-09 02:01:48 +0200329 const struct switchdev_obj *obj)
Vivien Didelotbdcff082019-08-25 13:25:17 -0400330{
Vladimir Oltean2209158c2020-09-21 03:10:29 +0300331 struct net_device *master = dsa_slave_to_master(dev);
Vivien Didelotbdcff082019-08-25 13:25:17 -0400332 struct dsa_port *dp = dsa_slave_to_port(dev);
333 struct switchdev_obj_port_vlan vlan;
Vladimir Olteanb7a9e0d2021-01-09 02:01:46 +0200334 int err;
Vivien Didelotbdcff082019-08-25 13:25:17 -0400335
336 if (obj->orig_dev != dev)
337 return -EOPNOTSUPP;
338
Russell King54a0ed02020-05-12 20:20:25 +0300339 if (dsa_port_skip_vlan_configuration(dp))
Vivien Didelotc5335d72019-08-25 13:25:18 -0400340 return 0;
341
Vivien Didelotbdcff082019-08-25 13:25:17 -0400342 vlan = *SWITCHDEV_OBJ_PORT_VLAN(obj);
343
Vladimir Oltean1ce39f02020-09-21 03:10:26 +0300344 /* Deny adding a bridge VLAN when there is already an 802.1Q upper with
345 * the same VID.
346 */
Vladimir Olteanffb68fc2021-01-09 02:01:48 +0200347 if (br_vlan_enabled(dp->bridge_dev)) {
Vladimir Oltean1ce39f02020-09-21 03:10:26 +0300348 rcu_read_lock();
349 err = dsa_slave_vlan_check_for_8021q_uppers(dev, &vlan);
350 rcu_read_unlock();
351 if (err)
352 return err;
353 }
354
Vladimir Olteanffb68fc2021-01-09 02:01:48 +0200355 err = dsa_port_vlan_add(dp, &vlan);
Vivien Didelotbdcff082019-08-25 13:25:17 -0400356 if (err)
357 return err;
358
Vivien Didelotb9499902019-08-25 13:25:20 -0400359 /* We need the dedicated CPU port to be a member of the VLAN as well.
360 * Even though drivers often handle CPU membership in special ways,
361 * it doesn't make sense to program a PVID, so clear this flag.
362 */
363 vlan.flags &= ~BRIDGE_VLAN_INFO_PVID;
364
Vladimir Olteanffb68fc2021-01-09 02:01:48 +0200365 err = dsa_port_vlan_add(dp->cpu_dp, &vlan);
Vivien Didelot7e1741b2019-08-25 13:25:19 -0400366 if (err)
367 return err;
368
Vladimir Olteanb7a9e0d2021-01-09 02:01:46 +0200369 return vlan_vid_add(master, htons(ETH_P_8021Q), vlan.vid);
Vivien Didelotbdcff082019-08-25 13:25:17 -0400370}
371
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400372static int dsa_slave_port_obj_add(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200373 const struct switchdev_obj *obj,
Vivien Didelot79b139f2019-06-14 13:49:22 -0400374 struct netlink_ext_ack *extack)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400375{
Vivien Didelotd9450972017-10-16 11:12:15 -0400376 struct dsa_port *dp = dsa_slave_to_port(dev);
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400377 int err;
378
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200379 switch (obj->id) {
Vivien Didelot8df30252016-08-31 11:50:03 -0400380 case SWITCHDEV_OBJ_ID_PORT_MDB:
Vivien Didelot79b139f2019-06-14 13:49:22 -0400381 if (obj->orig_dev != dev)
382 return -EOPNOTSUPP;
Vladimir Olteanffb68fc2021-01-09 02:01:48 +0200383 err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
Vivien Didelot8df30252016-08-31 11:50:03 -0400384 break;
Andrew Lunn5f4dbc52017-11-09 23:11:00 +0100385 case SWITCHDEV_OBJ_ID_HOST_MDB:
386 /* DSA can directly translate this to a normal MDB add,
387 * but on the CPU port.
388 */
Vladimir Olteanffb68fc2021-01-09 02:01:48 +0200389 err = dsa_port_mdb_add(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj));
Andrew Lunn5f4dbc52017-11-09 23:11:00 +0100390 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200391 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Vladimir Olteanffb68fc2021-01-09 02:01:48 +0200392 err = dsa_slave_vlan_add(dev, obj);
Vivien Didelot11149532015-08-13 12:52:17 -0400393 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400394 default:
395 err = -EOPNOTSUPP;
396 break;
397 }
398
399 return err;
400}
401
Vivien Didelotbdcff082019-08-25 13:25:17 -0400402static int dsa_slave_vlan_del(struct net_device *dev,
403 const struct switchdev_obj *obj)
404{
Vladimir Oltean2209158c2020-09-21 03:10:29 +0300405 struct net_device *master = dsa_slave_to_master(dev);
Vivien Didelotbdcff082019-08-25 13:25:17 -0400406 struct dsa_port *dp = dsa_slave_to_port(dev);
Vladimir Oltean2209158c2020-09-21 03:10:29 +0300407 struct switchdev_obj_port_vlan *vlan;
Vladimir Olteanb7a9e0d2021-01-09 02:01:46 +0200408 int err;
Vivien Didelotbdcff082019-08-25 13:25:17 -0400409
410 if (obj->orig_dev != dev)
411 return -EOPNOTSUPP;
412
Russell King54a0ed02020-05-12 20:20:25 +0300413 if (dsa_port_skip_vlan_configuration(dp))
Vivien Didelotc5335d72019-08-25 13:25:18 -0400414 return 0;
415
Vladimir Oltean2209158c2020-09-21 03:10:29 +0300416 vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
417
Vivien Didelot7e1741b2019-08-25 13:25:19 -0400418 /* Do not deprogram the CPU port as it may be shared with other user
419 * ports which can be members of this VLAN as well.
420 */
Vladimir Oltean2209158c2020-09-21 03:10:29 +0300421 err = dsa_port_vlan_del(dp, vlan);
422 if (err)
423 return err;
424
Vladimir Olteanb7a9e0d2021-01-09 02:01:46 +0200425 vlan_vid_del(master, htons(ETH_P_8021Q), vlan->vid);
Vladimir Oltean2209158c2020-09-21 03:10:29 +0300426
427 return 0;
Vivien Didelotbdcff082019-08-25 13:25:17 -0400428}
429
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400430static int dsa_slave_port_obj_del(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200431 const struct switchdev_obj *obj)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400432{
Vivien Didelotd9450972017-10-16 11:12:15 -0400433 struct dsa_port *dp = dsa_slave_to_port(dev);
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400434 int err;
435
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200436 switch (obj->id) {
Vivien Didelot8df30252016-08-31 11:50:03 -0400437 case SWITCHDEV_OBJ_ID_PORT_MDB:
Vivien Didelot79b139f2019-06-14 13:49:22 -0400438 if (obj->orig_dev != dev)
439 return -EOPNOTSUPP;
Vivien Didelotbcebb972017-05-19 17:00:40 -0400440 err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
Vivien Didelot8df30252016-08-31 11:50:03 -0400441 break;
Andrew Lunn5f4dbc52017-11-09 23:11:00 +0100442 case SWITCHDEV_OBJ_ID_HOST_MDB:
443 /* DSA can directly translate this to a normal MDB add,
444 * but on the CPU port.
445 */
446 err = dsa_port_mdb_del(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj));
447 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200448 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Vivien Didelotbdcff082019-08-25 13:25:17 -0400449 err = dsa_slave_vlan_del(dev, obj);
Vivien Didelot11149532015-08-13 12:52:17 -0400450 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400451 default:
452 err = -EOPNOTSUPP;
453 break;
454 }
455
456 return err;
457}
458
Florian Fainelli929d6c12019-02-06 09:45:45 -0800459static int dsa_slave_get_port_parent_id(struct net_device *dev,
460 struct netdev_phys_item_id *ppid)
Florian Fainellib73adef2015-02-24 13:15:33 -0800461{
Vivien Didelotd9450972017-10-16 11:12:15 -0400462 struct dsa_port *dp = dsa_slave_to_port(dev);
463 struct dsa_switch *ds = dp->ds;
Andrew Lunna42c8e32017-11-09 22:29:51 +0100464 struct dsa_switch_tree *dst = ds->dst;
Florian Fainellib73adef2015-02-24 13:15:33 -0800465
Jiri Pirko15b04ac2019-04-03 14:24:26 +0200466 /* For non-legacy ports, devlink is used and it takes
467 * care of the name generation. This ndo implementation
468 * should be removed with legacy support.
469 */
470 if (dp->ds->devlink)
471 return -EOPNOTSUPP;
472
Florian Fainelli929d6c12019-02-06 09:45:45 -0800473 ppid->id_len = sizeof(dst->index);
474 memcpy(&ppid->id, &dst->index, ppid->id_len);
475
476 return 0;
477}
478
Vivien Didelot4fa7b712017-09-20 19:31:57 -0400479static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
480 struct sk_buff *skb)
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700481{
482#ifdef CONFIG_NET_POLL_CONTROLLER
Vivien Didelot4fa7b712017-09-20 19:31:57 -0400483 struct dsa_slave_priv *p = netdev_priv(dev);
484
Eric Dumazetf78ed222020-05-07 09:32:21 -0700485 return netpoll_send_skb(p->netpoll, skb);
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700486#else
487 BUG();
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700488 return NETDEV_TX_OK;
Eric Dumazetf78ed222020-05-07 09:32:21 -0700489#endif
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700490}
491
Brandon Streiff90af1052018-02-14 01:07:49 +0100492static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p,
493 struct sk_buff *skb)
494{
495 struct dsa_switch *ds = p->dp->ds;
496 struct sk_buff *clone;
497 unsigned int type;
498
499 type = ptp_classify_raw(skb);
500 if (type == PTP_CLASS_NONE)
501 return;
502
503 if (!ds->ops->port_txtstamp)
504 return;
505
506 clone = skb_clone_sk(skb);
507 if (!clone)
508 return;
509
Christian Eggers30abc9cd2020-11-19 12:09:06 +0100510 if (ds->ops->port_txtstamp(ds, p->dp->index, clone, type)) {
511 DSA_SKB_CB(skb)->clone = clone;
Brandon Streiff90af1052018-02-14 01:07:49 +0100512 return;
Christian Eggers30abc9cd2020-11-19 12:09:06 +0100513 }
Brandon Streiff90af1052018-02-14 01:07:49 +0100514
515 kfree_skb(clone);
516}
517
Vladimir Oltean97a69a02019-05-05 13:19:25 +0300518netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev)
519{
520 /* SKB for netpoll still need to be mangled with the protocol-specific
521 * tag to be successfully transmitted
522 */
523 if (unlikely(netpoll_tx_running(dev)))
524 return dsa_slave_netpoll_send_skb(dev, skb);
525
526 /* Queue the SKB for transmission on the parent interface, but
527 * do not modify its EtherType
528 */
529 skb->dev = dsa_slave_to_master(dev);
530 dev_queue_xmit(skb);
531
532 return NETDEV_TX_OK;
533}
534EXPORT_SYMBOL_GPL(dsa_enqueue_skb);
535
Vladimir Olteana3b0b642020-11-01 21:16:09 +0200536static int dsa_realloc_skb(struct sk_buff *skb, struct net_device *dev)
537{
538 int needed_headroom = dev->needed_headroom;
539 int needed_tailroom = dev->needed_tailroom;
540
541 /* For tail taggers, we need to pad short frames ourselves, to ensure
542 * that the tail tag does not fail at its role of being at the end of
543 * the packet, once the master interface pads the frame. Account for
544 * that pad length here, and pad later.
545 */
546 if (unlikely(needed_tailroom && skb->len < ETH_ZLEN))
547 needed_tailroom += ETH_ZLEN - skb->len;
548 /* skb_headroom() returns unsigned int... */
549 needed_headroom = max_t(int, needed_headroom - skb_headroom(skb), 0);
550 needed_tailroom = max_t(int, needed_tailroom - skb_tailroom(skb), 0);
551
552 if (likely(!needed_headroom && !needed_tailroom && !skb_cloned(skb)))
553 /* No reallocation needed, yay! */
554 return 0;
555
556 return pskb_expand_head(skb, needed_headroom, needed_tailroom,
557 GFP_ATOMIC);
558}
559
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700560static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
561{
562 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700563 struct sk_buff *nskb;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700564
Heiner Kallweit6a900622020-11-07 21:49:46 +0100565 dev_sw_netstats_tx_add(dev, 1, skb->len);
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700566
Vladimir Oltean146d4422019-06-08 15:04:27 +0300567 DSA_SKB_CB(skb)->clone = NULL;
Vladimir Oltean87671372019-05-11 23:14:45 +0300568
Brandon Streiff90af1052018-02-14 01:07:49 +0100569 /* Identify PTP protocol packets, clone them, and pass them to the
570 * switch driver
571 */
572 dsa_skb_tx_timestamp(p, skb);
573
Vladimir Olteana3b0b642020-11-01 21:16:09 +0200574 if (dsa_realloc_skb(skb, dev)) {
575 dev_kfree_skb_any(skb);
576 return NETDEV_TX_OK;
577 }
578
579 /* needed_tailroom should still be 'warm' in the cache line from
580 * dsa_realloc_skb(), which has also ensured that padding is safe.
581 */
582 if (dev->needed_tailroom)
583 eth_skb_pad(skb);
584
Vivien Didelotfe47d562017-06-01 16:07:15 -0400585 /* Transmit function may have to reallocate the original SKB,
586 * in which case it must have freed it. Only free it here on error.
587 */
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700588 nskb = p->xmit(skb, dev);
Vivien Didelotfe47d562017-06-01 16:07:15 -0400589 if (!nskb) {
Vladimir Olteana68578c22020-01-04 02:37:10 +0200590 kfree_skb(skb);
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700591 return NETDEV_TX_OK;
Vivien Didelotfe47d562017-06-01 16:07:15 -0400592 }
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700593
Vladimir Oltean97a69a02019-05-05 13:19:25 +0300594 return dsa_enqueue_skb(nskb, dev);
595}
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700596
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000597/* ethtool operations *******************************************************/
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000598
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000599static void dsa_slave_get_drvinfo(struct net_device *dev,
600 struct ethtool_drvinfo *drvinfo)
601{
Jiri Pirko7826d432013-01-06 00:44:26 +0000602 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
Jiri Pirko7826d432013-01-06 00:44:26 +0000603 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
604 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000605}
606
Guenter Roeck3d762a02014-10-29 10:45:04 -0700607static int dsa_slave_get_regs_len(struct net_device *dev)
608{
Vivien Didelotd9450972017-10-16 11:12:15 -0400609 struct dsa_port *dp = dsa_slave_to_port(dev);
610 struct dsa_switch *ds = dp->ds;
Guenter Roeck3d762a02014-10-29 10:45:04 -0700611
Vivien Didelot9d490b42016-08-23 12:38:56 -0400612 if (ds->ops->get_regs_len)
Vivien Didelotd9450972017-10-16 11:12:15 -0400613 return ds->ops->get_regs_len(ds, dp->index);
Guenter Roeck3d762a02014-10-29 10:45:04 -0700614
615 return -EOPNOTSUPP;
616}
617
618static void
619dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
620{
Vivien Didelotd9450972017-10-16 11:12:15 -0400621 struct dsa_port *dp = dsa_slave_to_port(dev);
622 struct dsa_switch *ds = dp->ds;
Guenter Roeck3d762a02014-10-29 10:45:04 -0700623
Vivien Didelot9d490b42016-08-23 12:38:56 -0400624 if (ds->ops->get_regs)
Vivien Didelotd9450972017-10-16 11:12:15 -0400625 ds->ops->get_regs(ds, dp->index, regs, _p);
Guenter Roeck3d762a02014-10-29 10:45:04 -0700626}
627
Florian Fainelliaab9c402018-05-10 13:17:36 -0700628static int dsa_slave_nway_reset(struct net_device *dev)
629{
630 struct dsa_port *dp = dsa_slave_to_port(dev);
631
632 return phylink_ethtool_nway_reset(dp->pl);
633}
634
Guenter Roeck6793abb2014-10-29 10:45:01 -0700635static int dsa_slave_get_eeprom_len(struct net_device *dev)
636{
Vivien Didelotd9450972017-10-16 11:12:15 -0400637 struct dsa_port *dp = dsa_slave_to_port(dev);
638 struct dsa_switch *ds = dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700639
Andrew Lunn0e576042016-06-04 21:16:52 +0200640 if (ds->cd && ds->cd->eeprom_len)
Andrew Lunnff049552016-05-10 23:27:24 +0200641 return ds->cd->eeprom_len;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700642
Vivien Didelot9d490b42016-08-23 12:38:56 -0400643 if (ds->ops->get_eeprom_len)
644 return ds->ops->get_eeprom_len(ds);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700645
646 return 0;
647}
648
649static int dsa_slave_get_eeprom(struct net_device *dev,
650 struct ethtool_eeprom *eeprom, u8 *data)
651{
Vivien Didelotd9450972017-10-16 11:12:15 -0400652 struct dsa_port *dp = dsa_slave_to_port(dev);
653 struct dsa_switch *ds = dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700654
Vivien Didelot9d490b42016-08-23 12:38:56 -0400655 if (ds->ops->get_eeprom)
656 return ds->ops->get_eeprom(ds, eeprom, data);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700657
658 return -EOPNOTSUPP;
659}
660
661static int dsa_slave_set_eeprom(struct net_device *dev,
662 struct ethtool_eeprom *eeprom, u8 *data)
663{
Vivien Didelotd9450972017-10-16 11:12:15 -0400664 struct dsa_port *dp = dsa_slave_to_port(dev);
665 struct dsa_switch *ds = dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700666
Vivien Didelot9d490b42016-08-23 12:38:56 -0400667 if (ds->ops->set_eeprom)
668 return ds->ops->set_eeprom(ds, eeprom, data);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700669
670 return -EOPNOTSUPP;
671}
672
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000673static void dsa_slave_get_strings(struct net_device *dev,
674 uint32_t stringset, uint8_t *data)
675{
Vivien Didelotd9450972017-10-16 11:12:15 -0400676 struct dsa_port *dp = dsa_slave_to_port(dev);
677 struct dsa_switch *ds = dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000678
679 if (stringset == ETH_SS_STATS) {
680 int len = ETH_GSTRING_LEN;
681
682 strncpy(data, "tx_packets", len);
683 strncpy(data + len, "tx_bytes", len);
684 strncpy(data + 2 * len, "rx_packets", len);
685 strncpy(data + 3 * len, "rx_bytes", len);
Vivien Didelot9d490b42016-08-23 12:38:56 -0400686 if (ds->ops->get_strings)
Florian Fainelli89f09042018-04-25 12:12:50 -0700687 ds->ops->get_strings(ds, dp->index, stringset,
688 data + 4 * len);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000689 }
690}
691
692static void dsa_slave_get_ethtool_stats(struct net_device *dev,
693 struct ethtool_stats *stats,
694 uint64_t *data)
695{
Vivien Didelotd9450972017-10-16 11:12:15 -0400696 struct dsa_port *dp = dsa_slave_to_port(dev);
Vivien Didelotd9450972017-10-16 11:12:15 -0400697 struct dsa_switch *ds = dp->ds;
Florian Fainelli5f6b4e142017-08-03 21:33:27 -0700698 struct pcpu_sw_netstats *s;
Florian Fainellif613ed62017-08-01 15:00:36 -0700699 unsigned int start;
Florian Fainelli5f6b4e142017-08-03 21:33:27 -0700700 int i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000701
Florian Fainelli5f6b4e142017-08-03 21:33:27 -0700702 for_each_possible_cpu(i) {
703 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
704
Heiner Kallweit6a900622020-11-07 21:49:46 +0100705 s = per_cpu_ptr(dev->tstats, i);
Florian Fainelli5f6b4e142017-08-03 21:33:27 -0700706 do {
707 start = u64_stats_fetch_begin_irq(&s->syncp);
708 tx_packets = s->tx_packets;
709 tx_bytes = s->tx_bytes;
710 rx_packets = s->rx_packets;
711 rx_bytes = s->rx_bytes;
712 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
713 data[0] += tx_packets;
714 data[1] += tx_bytes;
715 data[2] += rx_packets;
716 data[3] += rx_bytes;
717 }
Vivien Didelot9d490b42016-08-23 12:38:56 -0400718 if (ds->ops->get_ethtool_stats)
Vivien Didelotd9450972017-10-16 11:12:15 -0400719 ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000720}
721
722static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
723{
Vivien Didelotd9450972017-10-16 11:12:15 -0400724 struct dsa_port *dp = dsa_slave_to_port(dev);
725 struct dsa_switch *ds = dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000726
727 if (sset == ETH_SS_STATS) {
728 int count;
729
730 count = 4;
Vivien Didelot9d490b42016-08-23 12:38:56 -0400731 if (ds->ops->get_sset_count)
Florian Fainelli89f09042018-04-25 12:12:50 -0700732 count += ds->ops->get_sset_count(ds, dp->index, sset);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000733
734 return count;
735 }
736
737 return -EOPNOTSUPP;
738}
739
Florian Fainelli19e57c42014-09-18 17:31:24 -0700740static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
741{
Vivien Didelotd9450972017-10-16 11:12:15 -0400742 struct dsa_port *dp = dsa_slave_to_port(dev);
743 struct dsa_switch *ds = dp->ds;
Florian Fainelli19e57c42014-09-18 17:31:24 -0700744
Florian Fainelliaab9c402018-05-10 13:17:36 -0700745 phylink_ethtool_get_wol(dp->pl, w);
746
Vivien Didelot9d490b42016-08-23 12:38:56 -0400747 if (ds->ops->get_wol)
Vivien Didelotd9450972017-10-16 11:12:15 -0400748 ds->ops->get_wol(ds, dp->index, w);
Florian Fainelli19e57c42014-09-18 17:31:24 -0700749}
750
751static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
752{
Vivien Didelotd9450972017-10-16 11:12:15 -0400753 struct dsa_port *dp = dsa_slave_to_port(dev);
754 struct dsa_switch *ds = dp->ds;
Florian Fainelli19e57c42014-09-18 17:31:24 -0700755 int ret = -EOPNOTSUPP;
756
Florian Fainelliaab9c402018-05-10 13:17:36 -0700757 phylink_ethtool_set_wol(dp->pl, w);
758
Vivien Didelot9d490b42016-08-23 12:38:56 -0400759 if (ds->ops->set_wol)
Vivien Didelotd9450972017-10-16 11:12:15 -0400760 ret = ds->ops->set_wol(ds, dp->index, w);
Florian Fainelli19e57c42014-09-18 17:31:24 -0700761
762 return ret;
763}
764
Florian Fainelli79052882014-09-24 17:05:21 -0700765static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
766{
Vivien Didelotd9450972017-10-16 11:12:15 -0400767 struct dsa_port *dp = dsa_slave_to_port(dev);
768 struct dsa_switch *ds = dp->ds;
Florian Fainelli79052882014-09-24 17:05:21 -0700769 int ret;
770
Vivien Didelot7b9cc732017-08-01 16:32:31 -0400771 /* Port's PHY and MAC both need to be EEE capable */
Dan Carpenter00670cb2019-02-06 18:35:15 +0300772 if (!dev->phydev || !dp->pl)
Vivien Didelot7b9cc732017-08-01 16:32:31 -0400773 return -ENODEV;
774
Vivien Didelot08f50062017-08-01 16:32:41 -0400775 if (!ds->ops->set_mac_eee)
Florian Fainelli79052882014-09-24 17:05:21 -0700776 return -EOPNOTSUPP;
777
Vivien Didelotd9450972017-10-16 11:12:15 -0400778 ret = ds->ops->set_mac_eee(ds, dp->index, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700779 if (ret)
780 return ret;
781
Florian Fainelliaab9c402018-05-10 13:17:36 -0700782 return phylink_ethtool_set_eee(dp->pl, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700783}
784
785static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
786{
Vivien Didelotd9450972017-10-16 11:12:15 -0400787 struct dsa_port *dp = dsa_slave_to_port(dev);
788 struct dsa_switch *ds = dp->ds;
Florian Fainelli79052882014-09-24 17:05:21 -0700789 int ret;
790
Vivien Didelot7b9cc732017-08-01 16:32:31 -0400791 /* Port's PHY and MAC both need to be EEE capable */
Dan Carpenter00670cb2019-02-06 18:35:15 +0300792 if (!dev->phydev || !dp->pl)
Vivien Didelot7b9cc732017-08-01 16:32:31 -0400793 return -ENODEV;
794
Vivien Didelot08f50062017-08-01 16:32:41 -0400795 if (!ds->ops->get_mac_eee)
Florian Fainelli79052882014-09-24 17:05:21 -0700796 return -EOPNOTSUPP;
797
Vivien Didelotd9450972017-10-16 11:12:15 -0400798 ret = ds->ops->get_mac_eee(ds, dp->index, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700799 if (ret)
800 return ret;
801
Florian Fainelliaab9c402018-05-10 13:17:36 -0700802 return phylink_ethtool_get_eee(dp->pl, e);
803}
804
805static int dsa_slave_get_link_ksettings(struct net_device *dev,
806 struct ethtool_link_ksettings *cmd)
807{
808 struct dsa_port *dp = dsa_slave_to_port(dev);
809
810 return phylink_ethtool_ksettings_get(dp->pl, cmd);
811}
812
813static int dsa_slave_set_link_ksettings(struct net_device *dev,
814 const struct ethtool_link_ksettings *cmd)
815{
816 struct dsa_port *dp = dsa_slave_to_port(dev);
817
818 return phylink_ethtool_ksettings_set(dp->pl, cmd);
Florian Fainelli79052882014-09-24 17:05:21 -0700819}
820
Heiner Kallweita2a1a132019-10-29 22:32:48 +0100821static void dsa_slave_get_pauseparam(struct net_device *dev,
822 struct ethtool_pauseparam *pause)
823{
824 struct dsa_port *dp = dsa_slave_to_port(dev);
825
826 phylink_ethtool_get_pauseparam(dp->pl, pause);
827}
828
829static int dsa_slave_set_pauseparam(struct net_device *dev,
830 struct ethtool_pauseparam *pause)
831{
832 struct dsa_port *dp = dsa_slave_to_port(dev);
833
834 return phylink_ethtool_set_pauseparam(dp->pl, pause);
835}
836
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700837#ifdef CONFIG_NET_POLL_CONTROLLER
838static int dsa_slave_netpoll_setup(struct net_device *dev,
839 struct netpoll_info *ni)
840{
Vivien Didelotd0006b02017-10-16 11:12:16 -0400841 struct net_device *master = dsa_slave_to_master(dev);
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700842 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700843 struct netpoll *netpoll;
844 int err = 0;
845
846 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
847 if (!netpoll)
848 return -ENOMEM;
849
850 err = __netpoll_setup(netpoll, master);
851 if (err) {
852 kfree(netpoll);
853 goto out;
854 }
855
856 p->netpoll = netpoll;
857out:
858 return err;
859}
860
861static void dsa_slave_netpoll_cleanup(struct net_device *dev)
862{
863 struct dsa_slave_priv *p = netdev_priv(dev);
864 struct netpoll *netpoll = p->netpoll;
865
866 if (!netpoll)
867 return;
868
869 p->netpoll = NULL;
870
Debabrata Banerjeec9fbd712018-10-18 11:18:26 -0400871 __netpoll_free(netpoll);
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700872}
873
874static void dsa_slave_poll_controller(struct net_device *dev)
875{
876}
877#endif
878
Florian Fainelli44bb7652017-01-10 12:32:36 -0800879static int dsa_slave_get_phys_port_name(struct net_device *dev,
880 char *name, size_t len)
881{
Vivien Didelotd9450972017-10-16 11:12:15 -0400882 struct dsa_port *dp = dsa_slave_to_port(dev);
Florian Fainelli44bb7652017-01-10 12:32:36 -0800883
Jiri Pirkod4842102019-03-28 13:56:44 +0100884 /* For non-legacy ports, devlink is used and it takes
885 * care of the name generation. This ndo implementation
886 * should be removed with legacy support.
887 */
888 if (dp->ds->devlink)
889 return -EOPNOTSUPP;
890
Vivien Didelotd9450972017-10-16 11:12:15 -0400891 if (snprintf(name, len, "p%d", dp->index) >= len)
Florian Fainelli44bb7652017-01-10 12:32:36 -0800892 return -EINVAL;
Florian Fainelli3a543ef2016-12-29 14:20:56 -0800893
894 return 0;
895}
896
Florian Fainellif50f2122017-01-30 12:41:40 -0800897static struct dsa_mall_tc_entry *
Vivien Didelot4fa7b712017-09-20 19:31:57 -0400898dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
Florian Fainellif50f2122017-01-30 12:41:40 -0800899{
Vivien Didelot4fa7b712017-09-20 19:31:57 -0400900 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainellif50f2122017-01-30 12:41:40 -0800901 struct dsa_mall_tc_entry *mall_tc_entry;
902
903 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
904 if (mall_tc_entry->cookie == cookie)
905 return mall_tc_entry;
906
907 return NULL;
908}
909
Vladimir Olteane13c2072020-03-29 14:51:58 +0300910static int
911dsa_slave_add_cls_matchall_mirred(struct net_device *dev,
912 struct tc_cls_matchall_offload *cls,
913 bool ingress)
Florian Fainellif50f2122017-01-30 12:41:40 -0800914{
Vivien Didelotd9450972017-10-16 11:12:15 -0400915 struct dsa_port *dp = dsa_slave_to_port(dev);
Florian Fainellif50f2122017-01-30 12:41:40 -0800916 struct dsa_slave_priv *p = netdev_priv(dev);
Vladimir Olteane13c2072020-03-29 14:51:58 +0300917 struct dsa_mall_mirror_tc_entry *mirror;
Florian Fainellif50f2122017-01-30 12:41:40 -0800918 struct dsa_mall_tc_entry *mall_tc_entry;
Vivien Didelotd9450972017-10-16 11:12:15 -0400919 struct dsa_switch *ds = dp->ds;
Pieter Jansen van Vuuren9681e8b2019-05-04 04:46:19 -0700920 struct flow_action_entry *act;
Vivien Didelotd9450972017-10-16 11:12:15 -0400921 struct dsa_port *to_dp;
Vladimir Olteane13c2072020-03-29 14:51:58 +0300922 int err;
923
Florian Fainellif50f2122017-01-30 12:41:40 -0800924 if (!ds->ops->port_mirror_add)
Vladimir Oltean34297172020-03-29 14:51:59 +0300925 return -EOPNOTSUPP;
Florian Fainellif50f2122017-01-30 12:41:40 -0800926
Jakub Kicinski53eca1f2020-03-16 18:42:11 -0700927 if (!flow_action_basic_hw_stats_check(&cls->rule->action,
928 cls->common.extack))
Vladimir Oltean34297172020-03-29 14:51:59 +0300929 return -EOPNOTSUPP;
Jiri Pirko319a1d12020-03-07 12:40:13 +0100930
Pieter Jansen van Vuuren9681e8b2019-05-04 04:46:19 -0700931 act = &cls->rule->action.entries[0];
Florian Fainellif50f2122017-01-30 12:41:40 -0800932
Vladimir Oltean65722152020-05-04 22:58:56 +0300933 if (!act->dev)
934 return -EINVAL;
935
Vladimir Olteane13c2072020-03-29 14:51:58 +0300936 if (!dsa_slave_dev_check(act->dev))
937 return -EOPNOTSUPP;
Florian Fainellif50f2122017-01-30 12:41:40 -0800938
Vladimir Olteane13c2072020-03-29 14:51:58 +0300939 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
940 if (!mall_tc_entry)
941 return -ENOMEM;
Florian Fainellif50f2122017-01-30 12:41:40 -0800942
Vladimir Olteane13c2072020-03-29 14:51:58 +0300943 mall_tc_entry->cookie = cls->cookie;
944 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
945 mirror = &mall_tc_entry->mirror;
Florian Fainellif50f2122017-01-30 12:41:40 -0800946
Vladimir Olteane13c2072020-03-29 14:51:58 +0300947 to_dp = dsa_slave_to_port(act->dev);
Florian Fainellif50f2122017-01-30 12:41:40 -0800948
Vladimir Olteane13c2072020-03-29 14:51:58 +0300949 mirror->to_local_port = to_dp->index;
950 mirror->ingress = ingress;
Florian Fainellif50f2122017-01-30 12:41:40 -0800951
Vladimir Olteane13c2072020-03-29 14:51:58 +0300952 err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
953 if (err) {
954 kfree(mall_tc_entry);
955 return err;
Florian Fainellif50f2122017-01-30 12:41:40 -0800956 }
957
Vladimir Olteane13c2072020-03-29 14:51:58 +0300958 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
959
960 return err;
961}
962
Vladimir Oltean34297172020-03-29 14:51:59 +0300963static int
964dsa_slave_add_cls_matchall_police(struct net_device *dev,
965 struct tc_cls_matchall_offload *cls,
966 bool ingress)
967{
968 struct netlink_ext_ack *extack = cls->common.extack;
969 struct dsa_port *dp = dsa_slave_to_port(dev);
970 struct dsa_slave_priv *p = netdev_priv(dev);
971 struct dsa_mall_policer_tc_entry *policer;
972 struct dsa_mall_tc_entry *mall_tc_entry;
973 struct dsa_switch *ds = dp->ds;
974 struct flow_action_entry *act;
975 int err;
976
977 if (!ds->ops->port_policer_add) {
978 NL_SET_ERR_MSG_MOD(extack,
Jacob Kellerc75a33c2020-05-06 17:58:27 -0700979 "Policing offload not implemented");
Vladimir Oltean34297172020-03-29 14:51:59 +0300980 return -EOPNOTSUPP;
981 }
982
983 if (!ingress) {
984 NL_SET_ERR_MSG_MOD(extack,
Jacob Kellerc75a33c2020-05-06 17:58:27 -0700985 "Only supported on ingress qdisc");
Vladimir Oltean34297172020-03-29 14:51:59 +0300986 return -EOPNOTSUPP;
987 }
988
989 if (!flow_action_basic_hw_stats_check(&cls->rule->action,
990 cls->common.extack))
991 return -EOPNOTSUPP;
992
993 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) {
994 if (mall_tc_entry->type == DSA_PORT_MALL_POLICER) {
995 NL_SET_ERR_MSG_MOD(extack,
Jacob Kellerc75a33c2020-05-06 17:58:27 -0700996 "Only one port policer allowed");
Vladimir Oltean34297172020-03-29 14:51:59 +0300997 return -EEXIST;
998 }
999 }
1000
1001 act = &cls->rule->action.entries[0];
1002
1003 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1004 if (!mall_tc_entry)
1005 return -ENOMEM;
1006
1007 mall_tc_entry->cookie = cls->cookie;
1008 mall_tc_entry->type = DSA_PORT_MALL_POLICER;
1009 policer = &mall_tc_entry->policer;
1010 policer->rate_bytes_per_sec = act->police.rate_bytes_ps;
1011 policer->burst = act->police.burst;
1012
1013 err = ds->ops->port_policer_add(ds, dp->index, policer);
1014 if (err) {
1015 kfree(mall_tc_entry);
1016 return err;
1017 }
1018
1019 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1020
1021 return err;
1022}
1023
Vladimir Olteane13c2072020-03-29 14:51:58 +03001024static int dsa_slave_add_cls_matchall(struct net_device *dev,
1025 struct tc_cls_matchall_offload *cls,
1026 bool ingress)
1027{
1028 int err = -EOPNOTSUPP;
1029
1030 if (cls->common.protocol == htons(ETH_P_ALL) &&
1031 flow_offload_has_one_action(&cls->rule->action) &&
1032 cls->rule->action.entries[0].id == FLOW_ACTION_MIRRED)
1033 err = dsa_slave_add_cls_matchall_mirred(dev, cls, ingress);
Vladimir Oltean34297172020-03-29 14:51:59 +03001034 else if (flow_offload_has_one_action(&cls->rule->action) &&
1035 cls->rule->action.entries[0].id == FLOW_ACTION_POLICE)
1036 err = dsa_slave_add_cls_matchall_police(dev, cls, ingress);
Vladimir Olteane13c2072020-03-29 14:51:58 +03001037
1038 return err;
Florian Fainellif50f2122017-01-30 12:41:40 -08001039}
1040
1041static void dsa_slave_del_cls_matchall(struct net_device *dev,
1042 struct tc_cls_matchall_offload *cls)
1043{
Vivien Didelotd9450972017-10-16 11:12:15 -04001044 struct dsa_port *dp = dsa_slave_to_port(dev);
Florian Fainellif50f2122017-01-30 12:41:40 -08001045 struct dsa_mall_tc_entry *mall_tc_entry;
Vivien Didelotd9450972017-10-16 11:12:15 -04001046 struct dsa_switch *ds = dp->ds;
Florian Fainellif50f2122017-01-30 12:41:40 -08001047
Vivien Didelot4fa7b712017-09-20 19:31:57 -04001048 mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
Florian Fainellif50f2122017-01-30 12:41:40 -08001049 if (!mall_tc_entry)
1050 return;
1051
1052 list_del(&mall_tc_entry->list);
1053
1054 switch (mall_tc_entry->type) {
1055 case DSA_PORT_MALL_MIRROR:
Vladimir Oltean34297172020-03-29 14:51:59 +03001056 if (ds->ops->port_mirror_del)
1057 ds->ops->port_mirror_del(ds, dp->index,
1058 &mall_tc_entry->mirror);
1059 break;
1060 case DSA_PORT_MALL_POLICER:
1061 if (ds->ops->port_policer_del)
1062 ds->ops->port_policer_del(ds, dp->index);
Florian Fainellif50f2122017-01-30 12:41:40 -08001063 break;
1064 default:
1065 WARN_ON(1);
1066 }
1067
1068 kfree(mall_tc_entry);
1069}
1070
Jiri Pirko3fbae382017-08-07 10:15:26 +02001071static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
Jiri Pirko6b3eb752017-10-19 15:50:45 +02001072 struct tc_cls_matchall_offload *cls,
1073 bool ingress)
Florian Fainellif50f2122017-01-30 12:41:40 -08001074{
Jiri Pirko5fd9fc42017-08-07 10:15:29 +02001075 if (cls->common.chain_index)
Jiri Pirkoa5fcf8a2017-06-06 17:00:16 +02001076 return -EOPNOTSUPP;
Florian Fainellif50f2122017-01-30 12:41:40 -08001077
Jiri Pirko3fbae382017-08-07 10:15:26 +02001078 switch (cls->command) {
1079 case TC_CLSMATCHALL_REPLACE:
Jiri Pirko5fd9fc42017-08-07 10:15:29 +02001080 return dsa_slave_add_cls_matchall(dev, cls, ingress);
Jiri Pirko3fbae382017-08-07 10:15:26 +02001081 case TC_CLSMATCHALL_DESTROY:
1082 dsa_slave_del_cls_matchall(dev, cls);
1083 return 0;
1084 default:
1085 return -EOPNOTSUPP;
1086 }
1087}
1088
Vladimir Olteaned11bb12020-02-29 16:31:13 +02001089static int dsa_slave_add_cls_flower(struct net_device *dev,
1090 struct flow_cls_offload *cls,
1091 bool ingress)
1092{
1093 struct dsa_port *dp = dsa_slave_to_port(dev);
1094 struct dsa_switch *ds = dp->ds;
1095 int port = dp->index;
1096
1097 if (!ds->ops->cls_flower_add)
1098 return -EOPNOTSUPP;
1099
1100 return ds->ops->cls_flower_add(ds, port, cls, ingress);
1101}
1102
1103static int dsa_slave_del_cls_flower(struct net_device *dev,
1104 struct flow_cls_offload *cls,
1105 bool ingress)
1106{
1107 struct dsa_port *dp = dsa_slave_to_port(dev);
1108 struct dsa_switch *ds = dp->ds;
1109 int port = dp->index;
1110
1111 if (!ds->ops->cls_flower_del)
1112 return -EOPNOTSUPP;
1113
1114 return ds->ops->cls_flower_del(ds, port, cls, ingress);
1115}
1116
1117static int dsa_slave_stats_cls_flower(struct net_device *dev,
1118 struct flow_cls_offload *cls,
1119 bool ingress)
1120{
1121 struct dsa_port *dp = dsa_slave_to_port(dev);
1122 struct dsa_switch *ds = dp->ds;
1123 int port = dp->index;
1124
1125 if (!ds->ops->cls_flower_stats)
1126 return -EOPNOTSUPP;
1127
1128 return ds->ops->cls_flower_stats(ds, port, cls, ingress);
1129}
1130
1131static int dsa_slave_setup_tc_cls_flower(struct net_device *dev,
1132 struct flow_cls_offload *cls,
1133 bool ingress)
1134{
1135 switch (cls->command) {
1136 case FLOW_CLS_REPLACE:
1137 return dsa_slave_add_cls_flower(dev, cls, ingress);
1138 case FLOW_CLS_DESTROY:
1139 return dsa_slave_del_cls_flower(dev, cls, ingress);
1140 case FLOW_CLS_STATS:
1141 return dsa_slave_stats_cls_flower(dev, cls, ingress);
1142 default:
1143 return -EOPNOTSUPP;
1144 }
1145}
1146
Jiri Pirko6b3eb752017-10-19 15:50:45 +02001147static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
1148 void *cb_priv, bool ingress)
1149{
1150 struct net_device *dev = cb_priv;
1151
Jiri Pirko44ae12a2017-11-01 11:47:39 +01001152 if (!tc_can_offload(dev))
1153 return -EOPNOTSUPP;
1154
Jiri Pirko6b3eb752017-10-19 15:50:45 +02001155 switch (type) {
1156 case TC_SETUP_CLSMATCHALL:
1157 return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
Vladimir Olteaned11bb12020-02-29 16:31:13 +02001158 case TC_SETUP_CLSFLOWER:
1159 return dsa_slave_setup_tc_cls_flower(dev, type_data, ingress);
Jiri Pirko6b3eb752017-10-19 15:50:45 +02001160 default:
1161 return -EOPNOTSUPP;
1162 }
1163}
1164
1165static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
1166 void *type_data, void *cb_priv)
1167{
1168 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
1169}
1170
1171static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
1172 void *type_data, void *cb_priv)
1173{
1174 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
1175}
1176
Pablo Neira Ayuso955bcb62019-07-09 22:55:46 +02001177static LIST_HEAD(dsa_slave_block_cb_list);
1178
Jiri Pirko6b3eb752017-10-19 15:50:45 +02001179static int dsa_slave_setup_tc_block(struct net_device *dev,
Pablo Neira Ayuso955bcb62019-07-09 22:55:46 +02001180 struct flow_block_offload *f)
Jiri Pirko6b3eb752017-10-19 15:50:45 +02001181{
Pablo Neira Ayuso955bcb62019-07-09 22:55:46 +02001182 struct flow_block_cb *block_cb;
Pablo Neira Ayusoa7323312019-07-19 18:20:15 +02001183 flow_setup_cb_t *cb;
Jiri Pirko6b3eb752017-10-19 15:50:45 +02001184
Pablo Neira Ayuso32f8c402019-07-09 22:55:41 +02001185 if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
Jiri Pirko6b3eb752017-10-19 15:50:45 +02001186 cb = dsa_slave_setup_tc_block_cb_ig;
Pablo Neira Ayuso32f8c402019-07-09 22:55:41 +02001187 else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
Jiri Pirko6b3eb752017-10-19 15:50:45 +02001188 cb = dsa_slave_setup_tc_block_cb_eg;
1189 else
1190 return -EOPNOTSUPP;
1191
Pablo Neira Ayuso955bcb62019-07-09 22:55:46 +02001192 f->driver_block_list = &dsa_slave_block_cb_list;
1193
Jiri Pirko6b3eb752017-10-19 15:50:45 +02001194 switch (f->command) {
Pablo Neira Ayuso9c0e1892019-07-09 22:55:40 +02001195 case FLOW_BLOCK_BIND:
Pablo Neira Ayuso0d4fd022019-07-09 22:55:48 +02001196 if (flow_block_cb_is_busy(cb, dev, &dsa_slave_block_cb_list))
1197 return -EBUSY;
1198
Pablo Neira Ayuso0c7294d2019-07-19 18:20:14 +02001199 block_cb = flow_block_cb_alloc(cb, dev, dev, NULL);
Pablo Neira Ayuso955bcb62019-07-09 22:55:46 +02001200 if (IS_ERR(block_cb))
1201 return PTR_ERR(block_cb);
1202
1203 flow_block_cb_add(block_cb, f);
1204 list_add_tail(&block_cb->driver_list, &dsa_slave_block_cb_list);
1205 return 0;
Pablo Neira Ayuso9c0e1892019-07-09 22:55:40 +02001206 case FLOW_BLOCK_UNBIND:
Pablo Neira Ayuso14bfb132019-07-19 18:20:16 +02001207 block_cb = flow_block_cb_lookup(f->block, cb, dev);
Pablo Neira Ayuso955bcb62019-07-09 22:55:46 +02001208 if (!block_cb)
1209 return -ENOENT;
1210
1211 flow_block_cb_remove(block_cb, f);
1212 list_del(&block_cb->driver_list);
Jiri Pirko6b3eb752017-10-19 15:50:45 +02001213 return 0;
1214 default:
1215 return -EOPNOTSUPP;
1216 }
1217}
1218
Jiri Pirko3fbae382017-08-07 10:15:26 +02001219static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
Jiri Pirkode4784c2017-08-07 10:15:32 +02001220 void *type_data)
Jiri Pirko3fbae382017-08-07 10:15:26 +02001221{
Vladimir Oltean47d23af2019-09-15 04:59:59 +03001222 struct dsa_port *dp = dsa_slave_to_port(dev);
1223 struct dsa_switch *ds = dp->ds;
1224
1225 if (type == TC_SETUP_BLOCK)
Jiri Pirko6b3eb752017-10-19 15:50:45 +02001226 return dsa_slave_setup_tc_block(dev, type_data);
Vladimir Oltean47d23af2019-09-15 04:59:59 +03001227
1228 if (!ds->ops->port_setup_tc)
Jiri Pirkoa5fcf8a2017-06-06 17:00:16 +02001229 return -EOPNOTSUPP;
Vladimir Oltean47d23af2019-09-15 04:59:59 +03001230
1231 return ds->ops->port_setup_tc(ds, dp->index, type, type_data);
Florian Fainellif50f2122017-01-30 12:41:40 -08001232}
1233
Florian Fainellibf9f2642017-01-30 09:48:40 -08001234static int dsa_slave_get_rxnfc(struct net_device *dev,
1235 struct ethtool_rxnfc *nfc, u32 *rule_locs)
1236{
Vivien Didelotd9450972017-10-16 11:12:15 -04001237 struct dsa_port *dp = dsa_slave_to_port(dev);
1238 struct dsa_switch *ds = dp->ds;
Florian Fainellibf9f2642017-01-30 09:48:40 -08001239
1240 if (!ds->ops->get_rxnfc)
1241 return -EOPNOTSUPP;
1242
Vivien Didelotd9450972017-10-16 11:12:15 -04001243 return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
Florian Fainellibf9f2642017-01-30 09:48:40 -08001244}
1245
1246static int dsa_slave_set_rxnfc(struct net_device *dev,
1247 struct ethtool_rxnfc *nfc)
1248{
Vivien Didelotd9450972017-10-16 11:12:15 -04001249 struct dsa_port *dp = dsa_slave_to_port(dev);
1250 struct dsa_switch *ds = dp->ds;
Florian Fainellibf9f2642017-01-30 09:48:40 -08001251
1252 if (!ds->ops->set_rxnfc)
1253 return -EOPNOTSUPP;
1254
Vivien Didelotd9450972017-10-16 11:12:15 -04001255 return ds->ops->set_rxnfc(ds, dp->index, nfc);
Florian Fainellibf9f2642017-01-30 09:48:40 -08001256}
1257
Brandon Streiff03363692018-02-14 01:07:48 +01001258static int dsa_slave_get_ts_info(struct net_device *dev,
1259 struct ethtool_ts_info *ts)
1260{
1261 struct dsa_slave_priv *p = netdev_priv(dev);
1262 struct dsa_switch *ds = p->dp->ds;
1263
1264 if (!ds->ops->get_ts_info)
1265 return -EOPNOTSUPP;
1266
1267 return ds->ops->get_ts_info(ds, p->dp->index, ts);
1268}
1269
Florian Fainelli061f6a52019-02-20 14:35:39 -08001270static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1271 u16 vid)
1272{
Vladimir Oltean2209158c2020-09-21 03:10:29 +03001273 struct net_device *master = dsa_slave_to_master(dev);
Florian Fainelli061f6a52019-02-20 14:35:39 -08001274 struct dsa_port *dp = dsa_slave_to_port(dev);
Vladimir Oltean88236592020-09-10 19:48:57 +03001275 struct switchdev_obj_port_vlan vlan = {
1276 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
Vladimir Olteanb7a9e0d2021-01-09 02:01:46 +02001277 .vid = vid,
Vladimir Oltean88236592020-09-10 19:48:57 +03001278 /* This API only allows programming tagged, non-PVID VIDs */
1279 .flags = 0,
1280 };
Florian Fainelli061f6a52019-02-20 14:35:39 -08001281 int ret;
1282
Vladimir Oltean88236592020-09-10 19:48:57 +03001283 /* User port... */
Vladimir Olteanffb68fc2021-01-09 02:01:48 +02001284 ret = dsa_port_vlan_add(dp, &vlan);
Vladimir Oltean88236592020-09-10 19:48:57 +03001285 if (ret)
1286 return ret;
1287
1288 /* And CPU port... */
Vladimir Olteanffb68fc2021-01-09 02:01:48 +02001289 ret = dsa_port_vlan_add(dp->cpu_dp, &vlan);
Vladimir Oltean9b236d22019-08-25 22:46:29 +03001290 if (ret)
Vivien Didelot7e1741b2019-08-25 13:25:19 -04001291 return ret;
1292
Vladimir Oltean2209158c2020-09-21 03:10:29 +03001293 return vlan_vid_add(master, proto, vid);
Florian Fainelli061f6a52019-02-20 14:35:39 -08001294}
1295
1296static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1297 u16 vid)
1298{
Vladimir Oltean2209158c2020-09-21 03:10:29 +03001299 struct net_device *master = dsa_slave_to_master(dev);
Florian Fainelli061f6a52019-02-20 14:35:39 -08001300 struct dsa_port *dp = dsa_slave_to_port(dev);
Vladimir Oltean88236592020-09-10 19:48:57 +03001301 struct switchdev_obj_port_vlan vlan = {
Vladimir Olteanb7a9e0d2021-01-09 02:01:46 +02001302 .vid = vid,
Vladimir Oltean88236592020-09-10 19:48:57 +03001303 /* This API only allows programming tagged, non-PVID VIDs */
1304 .flags = 0,
1305 };
Vladimir Oltean2209158c2020-09-21 03:10:29 +03001306 int err;
Florian Fainelli061f6a52019-02-20 14:35:39 -08001307
Vivien Didelot7e1741b2019-08-25 13:25:19 -04001308 /* Do not deprogram the CPU port as it may be shared with other user
1309 * ports which can be members of this VLAN as well.
1310 */
Vladimir Oltean2209158c2020-09-21 03:10:29 +03001311 err = dsa_port_vlan_del(dp, &vlan);
1312 if (err)
1313 return err;
1314
1315 vlan_vid_del(master, proto, vid);
1316
1317 return 0;
Florian Fainelli061f6a52019-02-20 14:35:39 -08001318}
1319
Vladimir Olteanbff33f72020-03-27 21:55:43 +02001320struct dsa_hw_port {
1321 struct list_head list;
1322 struct net_device *dev;
1323 int old_mtu;
1324};
1325
1326static int dsa_hw_port_list_set_mtu(struct list_head *hw_port_list, int mtu)
1327{
1328 const struct dsa_hw_port *p;
1329 int err;
1330
1331 list_for_each_entry(p, hw_port_list, list) {
1332 if (p->dev->mtu == mtu)
1333 continue;
1334
1335 err = dev_set_mtu(p->dev, mtu);
1336 if (err)
1337 goto rollback;
1338 }
1339
1340 return 0;
1341
1342rollback:
1343 list_for_each_entry_continue_reverse(p, hw_port_list, list) {
1344 if (p->dev->mtu == p->old_mtu)
1345 continue;
1346
1347 if (dev_set_mtu(p->dev, p->old_mtu))
1348 netdev_err(p->dev, "Failed to restore MTU\n");
1349 }
1350
1351 return err;
1352}
1353
1354static void dsa_hw_port_list_free(struct list_head *hw_port_list)
1355{
1356 struct dsa_hw_port *p, *n;
1357
1358 list_for_each_entry_safe(p, n, hw_port_list, list)
1359 kfree(p);
1360}
1361
1362/* Make the hardware datapath to/from @dev limited to a common MTU */
kbuild test robotbf88dc32020-04-02 09:25:48 +08001363static void dsa_bridge_mtu_normalization(struct dsa_port *dp)
Vladimir Olteanbff33f72020-03-27 21:55:43 +02001364{
1365 struct list_head hw_port_list;
1366 struct dsa_switch_tree *dst;
1367 int min_mtu = ETH_MAX_MTU;
1368 struct dsa_port *other_dp;
1369 int err;
1370
1371 if (!dp->ds->mtu_enforcement_ingress)
1372 return;
1373
1374 if (!dp->bridge_dev)
1375 return;
1376
1377 INIT_LIST_HEAD(&hw_port_list);
1378
1379 /* Populate the list of ports that are part of the same bridge
1380 * as the newly added/modified port
1381 */
1382 list_for_each_entry(dst, &dsa_tree_list, list) {
1383 list_for_each_entry(other_dp, &dst->ports, list) {
1384 struct dsa_hw_port *hw_port;
1385 struct net_device *slave;
1386
1387 if (other_dp->type != DSA_PORT_TYPE_USER)
1388 continue;
1389
1390 if (other_dp->bridge_dev != dp->bridge_dev)
1391 continue;
1392
1393 if (!other_dp->ds->mtu_enforcement_ingress)
1394 continue;
1395
1396 slave = other_dp->slave;
1397
1398 if (min_mtu > slave->mtu)
1399 min_mtu = slave->mtu;
1400
1401 hw_port = kzalloc(sizeof(*hw_port), GFP_KERNEL);
1402 if (!hw_port)
1403 goto out;
1404
1405 hw_port->dev = slave;
1406 hw_port->old_mtu = slave->mtu;
1407
1408 list_add(&hw_port->list, &hw_port_list);
1409 }
1410 }
1411
1412 /* Attempt to configure the entire hardware bridge to the newly added
1413 * interface's MTU first, regardless of whether the intention of the
1414 * user was to raise or lower it.
1415 */
1416 err = dsa_hw_port_list_set_mtu(&hw_port_list, dp->slave->mtu);
1417 if (!err)
1418 goto out;
1419
1420 /* Clearly that didn't work out so well, so just set the minimum MTU on
1421 * all hardware bridge ports now. If this fails too, then all ports will
1422 * still have their old MTU rolled back anyway.
1423 */
1424 dsa_hw_port_list_set_mtu(&hw_port_list, min_mtu);
1425
1426out:
1427 dsa_hw_port_list_free(&hw_port_list);
1428}
1429
Vladimir Olteanbfcb8132020-03-27 21:55:42 +02001430static int dsa_slave_change_mtu(struct net_device *dev, int new_mtu)
1431{
1432 struct net_device *master = dsa_slave_to_master(dev);
1433 struct dsa_port *dp = dsa_slave_to_port(dev);
1434 struct dsa_slave_priv *p = netdev_priv(dev);
1435 struct dsa_switch *ds = p->dp->ds;
1436 struct dsa_port *cpu_dp;
1437 int port = p->dp->index;
1438 int largest_mtu = 0;
1439 int new_master_mtu;
1440 int old_master_mtu;
1441 int mtu_limit;
1442 int cpu_mtu;
1443 int err, i;
1444
1445 if (!ds->ops->port_change_mtu)
1446 return -EOPNOTSUPP;
1447
1448 for (i = 0; i < ds->num_ports; i++) {
1449 int slave_mtu;
1450
1451 if (!dsa_is_user_port(ds, i))
1452 continue;
1453
1454 /* During probe, this function will be called for each slave
1455 * device, while not all of them have been allocated. That's
1456 * ok, it doesn't change what the maximum is, so ignore it.
1457 */
1458 if (!dsa_to_port(ds, i)->slave)
1459 continue;
1460
1461 /* Pretend that we already applied the setting, which we
1462 * actually haven't (still haven't done all integrity checks)
1463 */
1464 if (i == port)
1465 slave_mtu = new_mtu;
1466 else
1467 slave_mtu = dsa_to_port(ds, i)->slave->mtu;
1468
1469 if (largest_mtu < slave_mtu)
1470 largest_mtu = slave_mtu;
1471 }
1472
1473 cpu_dp = dsa_to_port(ds, port)->cpu_dp;
1474
1475 mtu_limit = min_t(int, master->max_mtu, dev->max_mtu);
1476 old_master_mtu = master->mtu;
1477 new_master_mtu = largest_mtu + cpu_dp->tag_ops->overhead;
1478 if (new_master_mtu > mtu_limit)
1479 return -ERANGE;
1480
1481 /* If the master MTU isn't over limit, there's no need to check the CPU
1482 * MTU, since that surely isn't either.
1483 */
1484 cpu_mtu = largest_mtu;
1485
1486 /* Start applying stuff */
1487 if (new_master_mtu != old_master_mtu) {
1488 err = dev_set_mtu(master, new_master_mtu);
1489 if (err < 0)
1490 goto out_master_failed;
1491
1492 /* We only need to propagate the MTU of the CPU port to
1493 * upstream switches.
1494 */
1495 err = dsa_port_mtu_change(cpu_dp, cpu_mtu, true);
1496 if (err)
1497 goto out_cpu_failed;
1498 }
1499
1500 err = dsa_port_mtu_change(dp, new_mtu, false);
1501 if (err)
1502 goto out_port_failed;
1503
1504 dev->mtu = new_mtu;
1505
Vladimir Olteanbff33f72020-03-27 21:55:43 +02001506 dsa_bridge_mtu_normalization(dp);
1507
Vladimir Olteanbfcb8132020-03-27 21:55:42 +02001508 return 0;
1509
1510out_port_failed:
1511 if (new_master_mtu != old_master_mtu)
1512 dsa_port_mtu_change(cpu_dp, old_master_mtu -
1513 cpu_dp->tag_ops->overhead,
1514 true);
1515out_cpu_failed:
1516 if (new_master_mtu != old_master_mtu)
1517 dev_set_mtu(master, old_master_mtu);
1518out_master_failed:
1519 return err;
1520}
1521
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001522static const struct ethtool_ops dsa_slave_ethtool_ops = {
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001523 .get_drvinfo = dsa_slave_get_drvinfo,
Guenter Roeck3d762a02014-10-29 10:45:04 -07001524 .get_regs_len = dsa_slave_get_regs_len,
1525 .get_regs = dsa_slave_get_regs,
Florian Fainelliaab9c402018-05-10 13:17:36 -07001526 .nway_reset = dsa_slave_nway_reset,
Florian Fainellic4aef9f2018-05-10 13:17:34 -07001527 .get_link = ethtool_op_get_link,
Guenter Roeck6793abb2014-10-29 10:45:01 -07001528 .get_eeprom_len = dsa_slave_get_eeprom_len,
1529 .get_eeprom = dsa_slave_get_eeprom,
1530 .set_eeprom = dsa_slave_set_eeprom,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001531 .get_strings = dsa_slave_get_strings,
1532 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
1533 .get_sset_count = dsa_slave_get_sset_count,
Florian Fainelli19e57c42014-09-18 17:31:24 -07001534 .set_wol = dsa_slave_set_wol,
1535 .get_wol = dsa_slave_get_wol,
Florian Fainelli79052882014-09-24 17:05:21 -07001536 .set_eee = dsa_slave_set_eee,
1537 .get_eee = dsa_slave_get_eee,
Florian Fainelliaab9c402018-05-10 13:17:36 -07001538 .get_link_ksettings = dsa_slave_get_link_ksettings,
1539 .set_link_ksettings = dsa_slave_set_link_ksettings,
Heiner Kallweita2a1a132019-10-29 22:32:48 +01001540 .get_pauseparam = dsa_slave_get_pauseparam,
1541 .set_pauseparam = dsa_slave_set_pauseparam,
Florian Fainellibf9f2642017-01-30 09:48:40 -08001542 .get_rxnfc = dsa_slave_get_rxnfc,
1543 .set_rxnfc = dsa_slave_set_rxnfc,
Brandon Streiff03363692018-02-14 01:07:48 +01001544 .get_ts_info = dsa_slave_get_ts_info,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001545};
1546
Florian Fainelli2a93c1a2017-12-06 15:03:33 -08001547/* legacy way, bypassing the bridge *****************************************/
Vladimir Oltean4b9c9352021-01-09 01:30:54 +02001548static int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1549 struct net_device *dev,
1550 const unsigned char *addr, u16 vid,
1551 u16 flags,
1552 struct netlink_ext_ack *extack)
Florian Fainelli2a93c1a2017-12-06 15:03:33 -08001553{
1554 struct dsa_port *dp = dsa_slave_to_port(dev);
1555
1556 return dsa_port_fdb_add(dp, addr, vid);
1557}
1558
Vladimir Oltean4b9c9352021-01-09 01:30:54 +02001559static int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
1560 struct net_device *dev,
1561 const unsigned char *addr, u16 vid)
Florian Fainelli2a93c1a2017-12-06 15:03:33 -08001562{
1563 struct dsa_port *dp = dsa_slave_to_port(dev);
1564
1565 return dsa_port_fdb_del(dp, addr, vid);
1566}
1567
Jiri Pirko716efee2019-03-28 13:56:43 +01001568static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev)
1569{
1570 struct dsa_port *dp = dsa_slave_to_port(dev);
1571
1572 return dp->ds->devlink ? &dp->devlink_port : NULL;
1573}
1574
Oleksij Rempelc2ec5f22021-01-11 11:46:57 +01001575static void dsa_slave_get_stats64(struct net_device *dev,
1576 struct rtnl_link_stats64 *s)
1577{
1578 struct dsa_port *dp = dsa_slave_to_port(dev);
1579 struct dsa_switch *ds = dp->ds;
1580
1581 if (ds->ops->get_stats64)
1582 ds->ops->get_stats64(ds, dp->index, s);
1583 else
1584 dev_get_tstats64(dev, s);
1585}
1586
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001587static const struct net_device_ops dsa_slave_netdev_ops = {
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001588 .ndo_open = dsa_slave_open,
1589 .ndo_stop = dsa_slave_close,
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001590 .ndo_start_xmit = dsa_slave_xmit,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001591 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
1592 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001593 .ndo_set_mac_address = dsa_slave_set_mac_address,
Arkadi Sharshevsky37b8da12017-08-06 16:15:43 +03001594 .ndo_fdb_add = dsa_legacy_fdb_add,
1595 .ndo_fdb_del = dsa_legacy_fdb_del,
Arkadi Sharshevsky2bedde12017-08-06 16:15:49 +03001596 .ndo_fdb_dump = dsa_slave_fdb_dump,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001597 .ndo_do_ioctl = dsa_slave_ioctl,
Nicolas Dichtelabd2be02015-04-02 17:07:08 +02001598 .ndo_get_iflink = dsa_slave_get_iflink,
Florian Fainelli04ff53f2015-07-31 11:42:57 -07001599#ifdef CONFIG_NET_POLL_CONTROLLER
1600 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
1601 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
1602 .ndo_poll_controller = dsa_slave_poll_controller,
1603#endif
Florian Fainelli44bb7652017-01-10 12:32:36 -08001604 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
Florian Fainellif50f2122017-01-30 12:41:40 -08001605 .ndo_setup_tc = dsa_slave_setup_tc,
Oleksij Rempelc2ec5f22021-01-11 11:46:57 +01001606 .ndo_get_stats64 = dsa_slave_get_stats64,
Florian Fainelli929d6c12019-02-06 09:45:45 -08001607 .ndo_get_port_parent_id = dsa_slave_get_port_parent_id,
Florian Fainelli061f6a52019-02-20 14:35:39 -08001608 .ndo_vlan_rx_add_vid = dsa_slave_vlan_rx_add_vid,
1609 .ndo_vlan_rx_kill_vid = dsa_slave_vlan_rx_kill_vid,
Jiri Pirko716efee2019-03-28 13:56:43 +01001610 .ndo_get_devlink_port = dsa_slave_get_devlink_port,
Vladimir Olteanbfcb8132020-03-27 21:55:42 +02001611 .ndo_change_mtu = dsa_slave_change_mtu,
Scott Feldman98237d42015-03-15 21:07:15 -07001612};
1613
Florian Fainellif37db852015-09-23 18:19:58 -07001614static struct device_type dsa_type = {
1615 .name = "dsa",
1616};
1617
Florian Fainelliaab9c402018-05-10 13:17:36 -07001618void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
1619{
1620 const struct dsa_port *dp = dsa_to_port(ds, port);
1621
Russell King765bda92020-03-31 15:17:36 +01001622 if (dp->pl)
1623 phylink_mac_change(dp->pl, up);
Florian Fainelliaab9c402018-05-10 13:17:36 -07001624}
1625EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
1626
Russell King5c05c1d2020-04-23 17:02:56 +01001627static void dsa_slave_phylink_fixed_state(struct phylink_config *config,
Florian Fainelliaab9c402018-05-10 13:17:36 -07001628 struct phylink_link_state *state)
1629{
Russell King5c05c1d2020-04-23 17:02:56 +01001630 struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
Florian Fainelliaab9c402018-05-10 13:17:36 -07001631 struct dsa_switch *ds = dp->ds;
1632
1633 /* No need to check that this operation is valid, the callback would
1634 * not be called if it was not.
1635 */
1636 ds->ops->phylink_fixed_state(ds, dp->index, state);
Florian Fainellice31b312014-08-27 17:04:54 -07001637}
1638
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001639/* slave device setup *******************************************************/
Vivien Didelot4fa7b712017-09-20 19:31:57 -04001640static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr)
Florian Fainellic305c162015-03-10 16:57:12 -07001641{
Vivien Didelotd9450972017-10-16 11:12:15 -04001642 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
Vivien Didelotd9450972017-10-16 11:12:15 -04001643 struct dsa_switch *ds = dp->ds;
Florian Fainellic305c162015-03-10 16:57:12 -07001644
Vivien Didelot0115dcd2017-09-26 17:15:32 -04001645 slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
1646 if (!slave_dev->phydev) {
Russell Kingd25b8e72015-10-03 18:09:07 +01001647 netdev_err(slave_dev, "no phy at %d\n", addr);
Florian Fainellic305c162015-03-10 16:57:12 -07001648 return -ENODEV;
Russell Kingd25b8e72015-10-03 18:09:07 +01001649 }
Florian Fainellic305c162015-03-10 16:57:12 -07001650
Florian Fainelliaab9c402018-05-10 13:17:36 -07001651 return phylink_connect_phy(dp->pl, slave_dev->phydev);
Florian Fainellic305c162015-03-10 16:57:12 -07001652}
1653
Vivien Didelot4fa7b712017-09-20 19:31:57 -04001654static int dsa_slave_phy_setup(struct net_device *slave_dev)
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001655{
Vivien Didelotd9450972017-10-16 11:12:15 -04001656 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
Vivien Didelotd9450972017-10-16 11:12:15 -04001657 struct device_node *port_dn = dp->dn;
1658 struct dsa_switch *ds = dp->ds;
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01001659 phy_interface_t mode;
Florian Fainelli68195632014-09-19 13:07:54 -07001660 u32 phy_flags = 0;
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01001661 int ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001662
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01001663 ret = of_get_phy_mode(port_dn, &mode);
1664 if (ret)
Guenter Roeck19334922015-02-16 21:23:51 -08001665 mode = PHY_INTERFACE_MODE_NA;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001666
Ioana Ciornei44cc27e2019-05-28 20:38:12 +03001667 dp->pl_config.dev = &slave_dev->dev;
1668 dp->pl_config.type = PHYLINK_NETDEV;
1669
Russell King5c05c1d2020-04-23 17:02:56 +01001670 /* The get_fixed_state callback takes precedence over polling the
1671 * link GPIO in PHYLINK (see phylink_get_fixed_state). Only set
1672 * this if the switch provides such a callback.
1673 */
1674 if (ds->ops->phylink_fixed_state) {
1675 dp->pl_config.get_fixed_state = dsa_slave_phylink_fixed_state;
1676 dp->pl_config.poll_fixed_state = true;
1677 }
1678
Ioana Ciornei44cc27e2019-05-28 20:38:12 +03001679 dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn), mode,
Ioana Ciornei77373d42019-05-28 20:38:15 +03001680 &dsa_port_phylink_mac_ops);
Florian Fainelliaab9c402018-05-10 13:17:36 -07001681 if (IS_ERR(dp->pl)) {
1682 netdev_err(slave_dev,
1683 "error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1684 return PTR_ERR(dp->pl);
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001685 }
1686
Vivien Didelot9d490b42016-08-23 12:38:56 -04001687 if (ds->ops->get_phy_flags)
Vivien Didelotd9450972017-10-16 11:12:15 -04001688 phy_flags = ds->ops->get_phy_flags(ds, dp->index);
Florian Fainelli68195632014-09-19 13:07:54 -07001689
Florian Fainelliaab9c402018-05-10 13:17:36 -07001690 ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
Vladimir Oltean6146dd42019-03-24 01:24:07 +02001691 if (ret == -ENODEV && ds->slave_mii_bus) {
1692 /* We could not connect to a designated PHY or SFP, so try to
1693 * use the switch internal MDIO bus instead
Florian Fainelliaab9c402018-05-10 13:17:36 -07001694 */
Vivien Didelotd9450972017-10-16 11:12:15 -04001695 ret = dsa_slave_phy_connect(slave_dev, dp->index);
Russell Kingd25b8e72015-10-03 18:09:07 +01001696 if (ret) {
Florian Fainelliaab9c402018-05-10 13:17:36 -07001697 netdev_err(slave_dev,
1698 "failed to connect to port %d: %d\n",
Vivien Didelotd9450972017-10-16 11:12:15 -04001699 dp->index, ret);
Florian Fainelliaab9c402018-05-10 13:17:36 -07001700 phylink_destroy(dp->pl);
Florian Fainellic305c162015-03-10 16:57:12 -07001701 return ret;
Russell Kingd25b8e72015-10-03 18:09:07 +01001702 }
Andrew Lunnb31f65f2014-11-05 19:47:28 +01001703 }
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001704
Vladimir Oltean6146dd42019-03-24 01:24:07 +02001705 return ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001706}
1707
Cong Wang1a33e102020-05-02 22:22:19 -07001708static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1709static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1710 struct netdev_queue *txq,
1711 void *_unused)
1712{
1713 lockdep_set_class(&txq->_xmit_lock,
1714 &dsa_slave_netdev_xmit_lock_key);
1715}
1716
Florian Fainelli24462542014-09-18 17:31:22 -07001717int dsa_slave_suspend(struct net_device *slave_dev)
1718{
Florian Fainelliaab9c402018-05-10 13:17:36 -07001719 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
Florian Fainelli24462542014-09-18 17:31:22 -07001720
Florian Fainellia94c6892018-07-31 17:12:52 -07001721 if (!netif_running(slave_dev))
1722 return 0;
1723
Florian Fainellif154be22017-01-25 09:10:41 -08001724 netif_device_detach(slave_dev);
1725
Florian Fainelliaab9c402018-05-10 13:17:36 -07001726 rtnl_lock();
1727 phylink_stop(dp->pl);
1728 rtnl_unlock();
Florian Fainelli24462542014-09-18 17:31:22 -07001729
1730 return 0;
1731}
1732
1733int dsa_slave_resume(struct net_device *slave_dev)
1734{
Florian Fainelliaab9c402018-05-10 13:17:36 -07001735 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
1736
Florian Fainellia94c6892018-07-31 17:12:52 -07001737 if (!netif_running(slave_dev))
1738 return 0;
1739
Florian Fainelli24462542014-09-18 17:31:22 -07001740 netif_device_attach(slave_dev);
1741
Florian Fainelliaab9c402018-05-10 13:17:36 -07001742 rtnl_lock();
1743 phylink_start(dp->pl);
1744 rtnl_unlock();
Florian Fainelli24462542014-09-18 17:31:22 -07001745
1746 return 0;
1747}
1748
Vivien Didelot951259aa2017-10-27 15:55:19 -04001749int dsa_slave_create(struct dsa_port *port)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001750{
Vivien Didelot24a93322017-11-06 16:11:43 -05001751 const struct dsa_port *cpu_dp = port->cpu_dp;
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -04001752 struct net_device *master = cpu_dp->master;
Vivien Didelot4cfbf092017-08-05 16:20:19 -04001753 struct dsa_switch *ds = port->ds;
Vivien Didelot951259aa2017-10-27 15:55:19 -04001754 const char *name = port->name;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001755 struct net_device *slave_dev;
1756 struct dsa_slave_priv *p;
1757 int ret;
1758
Florian Fainelli55199df2017-09-03 20:27:00 -07001759 if (!ds->num_tx_queues)
1760 ds->num_tx_queues = 1;
1761
1762 slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
1763 NET_NAME_UNKNOWN, ether_setup,
1764 ds->num_tx_queues, 1);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001765 if (slave_dev == NULL)
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001766 return -ENOMEM;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001767
Vladimir Oltean9b236d22019-08-25 22:46:29 +03001768 slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1769 if (ds->ops->port_vlan_add && ds->ops->port_vlan_del)
1770 slave_dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Florian Fainellif50f2122017-01-30 12:41:40 -08001771 slave_dev->hw_features |= NETIF_F_HW_TC;
Vladimir Oltean2b86cb82020-05-27 21:08:05 +03001772 slave_dev->features |= NETIF_F_LLTX;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001773 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
Petr Å tetiar4974f9b2019-05-06 23:24:45 +02001774 if (!IS_ERR_OR_NULL(port->mac))
Xiaofei Shena2c70232019-03-29 11:04:58 +05301775 ether_addr_copy(slave_dev->dev_addr, port->mac);
1776 else
1777 eth_hw_addr_inherit(slave_dev, master);
Phil Sutter0a5f1072015-08-18 10:30:41 +02001778 slave_dev->priv_flags |= IFF_NO_QUEUE;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001779 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
Vladimir Olteanbfcb8132020-03-27 21:55:42 +02001780 if (ds->ops->port_max_mtu)
1781 slave_dev->max_mtu = ds->ops->port_max_mtu(ds, port->index);
Vladimir Olteana3b0b642020-11-01 21:16:09 +02001782 if (cpu_dp->tag_ops->tail_tag)
1783 slave_dev->needed_tailroom = cpu_dp->tag_ops->overhead;
1784 else
1785 slave_dev->needed_headroom = cpu_dp->tag_ops->overhead;
1786 /* Try to save one extra realloc later in the TX path (in the master)
1787 * by also inheriting the master's needed headroom and tailroom.
1788 * The 8021q driver also does this.
1789 */
1790 slave_dev->needed_headroom += master->needed_headroom;
1791 slave_dev->needed_tailroom += master->needed_tailroom;
Florian Fainellif37db852015-09-23 18:19:58 -07001792 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001793
Cong Wang1a33e102020-05-02 22:22:19 -07001794 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1795 NULL);
1796
Vivien Didelot4cfbf092017-08-05 16:20:19 -04001797 SET_NETDEV_DEV(slave_dev, port->ds->dev);
1798 slave_dev->dev.of_node = port->dn;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001799 slave_dev->vlan_features = master->vlan_features;
1800
1801 p = netdev_priv(slave_dev);
Heiner Kallweit6a900622020-11-07 21:49:46 +01001802 slave_dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1803 if (!slave_dev->tstats) {
Florian Fainelli5f6b4e142017-08-03 21:33:27 -07001804 free_netdev(slave_dev);
1805 return -ENOMEM;
1806 }
Alexander Lobakine131a562020-04-21 16:41:08 +03001807
1808 ret = gro_cells_init(&p->gcells, slave_dev);
1809 if (ret)
1810 goto out_free;
1811
Vivien Didelot4cfbf092017-08-05 16:20:19 -04001812 p->dp = port;
Florian Fainellif50f2122017-01-30 12:41:40 -08001813 INIT_LIST_HEAD(&p->mall_tc_list);
Vivien Didelot15240242017-09-29 17:19:18 -04001814 p->xmit = cpu_dp->tag_ops->xmit;
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -04001815 port->slave = slave_dev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001816
Vladimir Olteanbfcb8132020-03-27 21:55:42 +02001817 rtnl_lock();
1818 ret = dsa_slave_change_mtu(slave_dev, ETH_DATA_LEN);
1819 rtnl_unlock();
Vladimir Oltean4349abd2020-09-08 02:25:56 +03001820 if (ret && ret != -EOPNOTSUPP)
Rasmus Villemoesbdc40a32020-12-05 14:39:44 +01001821 dev_warn(ds->dev, "nonfatal error %d setting MTU to %d on port %d\n",
1822 ret, ETH_DATA_LEN, port->index);
Vladimir Olteanbfcb8132020-03-27 21:55:42 +02001823
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001824 netif_carrier_off(slave_dev);
1825
Vivien Didelot4fa7b712017-09-20 19:31:57 -04001826 ret = dsa_slave_phy_setup(slave_dev);
Andrew Lunn0071f562016-01-06 20:11:20 +01001827 if (ret) {
Vladimir Olteanc9ebf122020-09-08 02:06:56 +03001828 netdev_err(slave_dev,
1829 "error %d setting up PHY for tree %d, switch %d, port %d\n",
1830 ret, ds->dst->index, ds->index, port->index);
Alexander Lobakine131a562020-04-21 16:41:08 +03001831 goto out_gcells;
Florian Fainellie8044412017-09-25 15:55:53 -07001832 }
1833
Vladimir Oltean2f1e8ea2020-09-08 02:48:42 +03001834 rtnl_lock();
1835
1836 ret = register_netdevice(slave_dev);
Florian Fainellie8044412017-09-25 15:55:53 -07001837 if (ret) {
1838 netdev_err(master, "error %d registering interface %s\n",
1839 ret, slave_dev->name);
Vladimir Oltean2f1e8ea2020-09-08 02:48:42 +03001840 rtnl_unlock();
Florian Fainellie8044412017-09-25 15:55:53 -07001841 goto out_phy;
Andrew Lunn0071f562016-01-06 20:11:20 +01001842 }
1843
Vladimir Oltean2f1e8ea2020-09-08 02:48:42 +03001844 ret = netdev_upper_dev_link(master, slave_dev, NULL);
1845
1846 rtnl_unlock();
1847
1848 if (ret)
1849 goto out_unregister;
1850
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001851 return 0;
Florian Fainellie8044412017-09-25 15:55:53 -07001852
Vladimir Oltean2f1e8ea2020-09-08 02:48:42 +03001853out_unregister:
1854 unregister_netdev(slave_dev);
Florian Fainellie8044412017-09-25 15:55:53 -07001855out_phy:
Florian Fainelliaab9c402018-05-10 13:17:36 -07001856 rtnl_lock();
1857 phylink_disconnect_phy(p->dp->pl);
1858 rtnl_unlock();
1859 phylink_destroy(p->dp->pl);
Alexander Lobakine131a562020-04-21 16:41:08 +03001860out_gcells:
1861 gro_cells_destroy(&p->gcells);
Florian Fainellie8044412017-09-25 15:55:53 -07001862out_free:
Heiner Kallweit6a900622020-11-07 21:49:46 +01001863 free_percpu(slave_dev->tstats);
Florian Fainellie8044412017-09-25 15:55:53 -07001864 free_netdev(slave_dev);
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -04001865 port->slave = NULL;
Florian Fainellie8044412017-09-25 15:55:53 -07001866 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001867}
Florian Fainellib73adef2015-02-24 13:15:33 -08001868
Neil Armstrongcda5c152015-12-07 13:57:35 +01001869void dsa_slave_destroy(struct net_device *slave_dev)
1870{
Vladimir Oltean2f1e8ea2020-09-08 02:48:42 +03001871 struct net_device *master = dsa_slave_to_master(slave_dev);
Vivien Didelotd9450972017-10-16 11:12:15 -04001872 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
Neil Armstrongcda5c152015-12-07 13:57:35 +01001873 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1874
1875 netif_carrier_off(slave_dev);
Florian Fainelliaab9c402018-05-10 13:17:36 -07001876 rtnl_lock();
Vladimir Oltean2f1e8ea2020-09-08 02:48:42 +03001877 netdev_upper_dev_unlink(master, slave_dev);
1878 unregister_netdevice(slave_dev);
Florian Fainelliaab9c402018-05-10 13:17:36 -07001879 phylink_disconnect_phy(dp->pl);
1880 rtnl_unlock();
Johan Hovold881eada2016-11-28 19:25:09 +01001881
Florian Fainelliaab9c402018-05-10 13:17:36 -07001882 phylink_destroy(dp->pl);
Alexander Lobakine131a562020-04-21 16:41:08 +03001883 gro_cells_destroy(&p->gcells);
Heiner Kallweit6a900622020-11-07 21:49:46 +01001884 free_percpu(slave_dev->tstats);
Neil Armstrongcda5c152015-12-07 13:57:35 +01001885 free_netdev(slave_dev);
1886}
1887
Florian Fainelli4d776482020-01-07 21:06:05 -08001888bool dsa_slave_dev_check(const struct net_device *dev)
Florian Fainellib73adef2015-02-24 13:15:33 -08001889{
1890 return dev->netdev_ops == &dsa_slave_netdev_ops;
1891}
Vladimir Olteana5e3c9b2021-01-07 03:24:01 +02001892EXPORT_SYMBOL_GPL(dsa_slave_dev_check);
Florian Fainellib73adef2015-02-24 13:15:33 -08001893
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001894static int dsa_slave_changeupper(struct net_device *dev,
1895 struct netdev_notifier_changeupper_info *info)
Florian Fainellib73adef2015-02-24 13:15:33 -08001896{
Vivien Didelotd9450972017-10-16 11:12:15 -04001897 struct dsa_port *dp = dsa_slave_to_port(dev);
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001898 int err = NOTIFY_DONE;
Florian Fainellib73adef2015-02-24 13:15:33 -08001899
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001900 if (netif_is_bridge_master(info->upper_dev)) {
1901 if (info->linking) {
Vivien Didelot17d78022017-05-19 17:00:38 -04001902 err = dsa_port_bridge_join(dp, info->upper_dev);
Vladimir Olteanbff33f72020-03-27 21:55:43 +02001903 if (!err)
1904 dsa_bridge_mtu_normalization(dp);
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001905 err = notifier_from_errno(err);
1906 } else {
Vivien Didelot17d78022017-05-19 17:00:38 -04001907 dsa_port_bridge_leave(dp, info->upper_dev);
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001908 err = NOTIFY_OK;
Vivien Didelot6debb682016-03-13 16:21:34 -04001909 }
Vivien Didelot6debb682016-03-13 16:21:34 -04001910 }
1911
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001912 return err;
Florian Fainellib73adef2015-02-24 13:15:33 -08001913}
1914
Vladimir Olteaneb46e8d2020-09-21 03:10:24 +03001915static int
1916dsa_prevent_bridging_8021q_upper(struct net_device *dev,
1917 struct netdev_notifier_changeupper_info *info)
Florian Fainellicc1d5bd2019-02-20 14:35:38 -08001918{
1919 struct netlink_ext_ack *ext_ack;
1920 struct net_device *slave;
1921 struct dsa_port *dp;
1922
1923 ext_ack = netdev_notifier_info_to_extack(&info->info);
1924
1925 if (!is_vlan_dev(dev))
1926 return NOTIFY_DONE;
1927
1928 slave = vlan_dev_real_dev(dev);
1929 if (!dsa_slave_dev_check(slave))
1930 return NOTIFY_DONE;
1931
1932 dp = dsa_slave_to_port(slave);
1933 if (!dp->bridge_dev)
1934 return NOTIFY_DONE;
1935
1936 /* Deny enslaving a VLAN device into a VLAN-aware bridge */
1937 if (br_vlan_enabled(dp->bridge_dev) &&
1938 netif_is_bridge_master(info->upper_dev) && info->linking) {
1939 NL_SET_ERR_MSG_MOD(ext_ack,
1940 "Cannot enslave VLAN device into VLAN aware bridge");
1941 return notifier_from_errno(-EINVAL);
1942 }
1943
1944 return NOTIFY_DONE;
1945}
1946
Vladimir Oltean2b138402020-09-21 03:10:25 +03001947static int
1948dsa_slave_check_8021q_upper(struct net_device *dev,
1949 struct netdev_notifier_changeupper_info *info)
1950{
1951 struct dsa_port *dp = dsa_slave_to_port(dev);
1952 struct net_device *br = dp->bridge_dev;
1953 struct bridge_vlan_info br_info;
1954 struct netlink_ext_ack *extack;
1955 int err = NOTIFY_DONE;
1956 u16 vid;
1957
Vladimir Olteanadb256e2020-09-21 03:10:28 +03001958 if (!br || !br_vlan_enabled(br))
Vladimir Oltean2b138402020-09-21 03:10:25 +03001959 return NOTIFY_DONE;
1960
1961 extack = netdev_notifier_info_to_extack(&info->info);
1962 vid = vlan_dev_vlan_id(info->upper_dev);
1963
1964 /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
1965 * device, respectively the VID is not found, returning
1966 * 0 means success, which is a failure for us here.
1967 */
1968 err = br_vlan_get_info(br, vid, &br_info);
1969 if (err == 0) {
1970 NL_SET_ERR_MSG_MOD(extack,
1971 "This VLAN is already configured by the bridge");
1972 return notifier_from_errno(-EBUSY);
1973 }
1974
1975 return NOTIFY_DONE;
1976}
1977
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001978static int dsa_slave_netdevice_event(struct notifier_block *nb,
1979 unsigned long event, void *ptr)
Florian Fainellib73adef2015-02-24 13:15:33 -08001980{
Vivien Didelot6debb682016-03-13 16:21:34 -04001981 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Florian Fainellib73adef2015-02-24 13:15:33 -08001982
Vladimir Oltean83501292020-09-21 03:10:23 +03001983 switch (event) {
Vladimir Oltean2b138402020-09-21 03:10:25 +03001984 case NETDEV_PRECHANGEUPPER: {
1985 struct netdev_notifier_changeupper_info *info = ptr;
Vladimir Olteane358bef2020-11-03 08:10:55 +01001986 struct dsa_switch *ds;
1987 struct dsa_port *dp;
1988 int err;
Vladimir Oltean2b138402020-09-21 03:10:25 +03001989
Florian Fainellicc1d5bd2019-02-20 14:35:38 -08001990 if (!dsa_slave_dev_check(dev))
Vladimir Olteaneb46e8d2020-09-21 03:10:24 +03001991 return dsa_prevent_bridging_8021q_upper(dev, ptr);
Vladimir Oltean2b138402020-09-21 03:10:25 +03001992
Vladimir Olteane358bef2020-11-03 08:10:55 +01001993 dp = dsa_slave_to_port(dev);
1994 ds = dp->ds;
1995
1996 if (ds->ops->port_prechangeupper) {
1997 err = ds->ops->port_prechangeupper(ds, dp->index, info);
1998 if (err)
1999 return notifier_from_errno(err);
2000 }
2001
Vladimir Oltean2b138402020-09-21 03:10:25 +03002002 if (is_vlan_dev(info->upper_dev))
2003 return dsa_slave_check_8021q_upper(dev, ptr);
Vladimir Oltean83501292020-09-21 03:10:23 +03002004 break;
Vladimir Oltean2b138402020-09-21 03:10:25 +03002005 }
Vladimir Oltean83501292020-09-21 03:10:23 +03002006 case NETDEV_CHANGEUPPER:
2007 if (!dsa_slave_dev_check(dev))
2008 return NOTIFY_DONE;
Vivien Didelot8e92ab32017-02-03 13:20:17 -05002009
Vivien Didelot8e92ab32017-02-03 13:20:17 -05002010 return dsa_slave_changeupper(dev, ptr);
Florian Fainellicc1d5bd2019-02-20 14:35:38 -08002011 }
Florian Fainellib73adef2015-02-24 13:15:33 -08002012
Florian Fainellib73adef2015-02-24 13:15:33 -08002013 return NOTIFY_DONE;
2014}
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05002015
Vladimir Olteanc4bb76a2021-01-06 11:51:32 +02002016static void
2017dsa_fdb_offload_notify(struct dsa_switchdev_event_work *switchdev_work)
2018{
2019 struct dsa_switch *ds = switchdev_work->ds;
2020 struct switchdev_notifier_fdb_info info;
2021 struct dsa_port *dp;
2022
2023 if (!dsa_is_user_port(ds, switchdev_work->port))
2024 return;
2025
2026 info.addr = switchdev_work->addr;
2027 info.vid = switchdev_work->vid;
2028 info.offloaded = true;
2029 dp = dsa_to_port(ds, switchdev_work->port);
2030 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED,
2031 dp->slave, &info.info, NULL);
2032}
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03002033
2034static void dsa_slave_switchdev_event_work(struct work_struct *work)
2035{
2036 struct dsa_switchdev_event_work *switchdev_work =
2037 container_of(work, struct dsa_switchdev_event_work, work);
Vladimir Olteanc4bb76a2021-01-06 11:51:32 +02002038 struct dsa_switch *ds = switchdev_work->ds;
2039 struct dsa_port *dp;
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03002040 int err;
2041
Vladimir Olteanc4bb76a2021-01-06 11:51:32 +02002042 dp = dsa_to_port(ds, switchdev_work->port);
2043
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03002044 rtnl_lock();
2045 switch (switchdev_work->event) {
2046 case SWITCHDEV_FDB_ADD_TO_DEVICE:
Vladimir Olteanc4bb76a2021-01-06 11:51:32 +02002047 err = dsa_port_fdb_add(dp, switchdev_work->addr,
2048 switchdev_work->vid);
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03002049 if (err) {
Vladimir Olteanc4bb76a2021-01-06 11:51:32 +02002050 dev_err(ds->dev,
2051 "port %d failed to add %pM vid %d to fdb: %d\n",
2052 dp->index, switchdev_work->addr,
2053 switchdev_work->vid, err);
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03002054 break;
2055 }
Vladimir Olteanc4bb76a2021-01-06 11:51:32 +02002056 dsa_fdb_offload_notify(switchdev_work);
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03002057 break;
2058
2059 case SWITCHDEV_FDB_DEL_TO_DEVICE:
Vladimir Olteanc4bb76a2021-01-06 11:51:32 +02002060 err = dsa_port_fdb_del(dp, switchdev_work->addr,
2061 switchdev_work->vid);
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03002062 if (err) {
Vladimir Olteanc4bb76a2021-01-06 11:51:32 +02002063 dev_err(ds->dev,
2064 "port %d failed to delete %pM vid %d from fdb: %d\n",
2065 dp->index, switchdev_work->addr,
2066 switchdev_work->vid, err);
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03002067 }
Vladimir Oltean2fd18652021-01-06 11:51:31 +02002068
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03002069 break;
2070 }
2071 rtnl_unlock();
2072
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03002073 kfree(switchdev_work);
Vladimir Olteanc4bb76a2021-01-06 11:51:32 +02002074 if (dsa_is_user_port(ds, dp->index))
2075 dev_put(dp->slave);
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03002076}
2077
Vladimir Olteand5f19482021-01-06 11:51:35 +02002078static int dsa_lower_dev_walk(struct net_device *lower_dev,
2079 struct netdev_nested_priv *priv)
2080{
2081 if (dsa_slave_dev_check(lower_dev)) {
2082 priv->data = (void *)netdev_priv(lower_dev);
2083 return 1;
2084 }
2085
2086 return 0;
2087}
2088
2089static struct dsa_slave_priv *dsa_slave_dev_lower_find(struct net_device *dev)
2090{
2091 struct netdev_nested_priv priv = {
2092 .data = NULL,
2093 };
2094
2095 netdev_walk_all_lower_dev_rcu(dev, dsa_lower_dev_walk, &priv);
2096
2097 return (struct dsa_slave_priv *)priv.data;
2098}
2099
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03002100/* Called under rcu_read_lock() */
2101static int dsa_slave_switchdev_event(struct notifier_block *unused,
2102 unsigned long event, void *ptr)
2103{
2104 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
Vladimir Olteanc4bb76a2021-01-06 11:51:32 +02002105 const struct switchdev_notifier_fdb_info *fdb_info;
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03002106 struct dsa_switchdev_event_work *switchdev_work;
Vladimir Olteanc4bb76a2021-01-06 11:51:32 +02002107 struct dsa_port *dp;
Vivien Didelot79b139f2019-06-14 13:49:22 -04002108 int err;
2109
Vladimir Oltean447d2902021-01-06 11:51:33 +02002110 switch (event) {
2111 case SWITCHDEV_PORT_ATTR_SET:
Vivien Didelot79b139f2019-06-14 13:49:22 -04002112 err = switchdev_handle_port_attr_set(dev, ptr,
2113 dsa_slave_dev_check,
2114 dsa_slave_port_attr_set);
2115 return notifier_from_errno(err);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002116 case SWITCHDEV_FDB_ADD_TO_DEVICE:
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03002117 case SWITCHDEV_FDB_DEL_TO_DEVICE:
Vladimir Olteand5f19482021-01-06 11:51:35 +02002118 fdb_info = ptr;
Vladimir Oltean447d2902021-01-06 11:51:33 +02002119
Vladimir Olteand5f19482021-01-06 11:51:35 +02002120 if (dsa_slave_dev_check(dev)) {
2121 if (!fdb_info->added_by_user)
2122 return NOTIFY_OK;
2123
2124 dp = dsa_slave_to_port(dev);
2125 } else {
2126 /* Snoop addresses learnt on foreign interfaces
2127 * bridged with us, for switches that don't
2128 * automatically learn SA from CPU-injected traffic
2129 */
2130 struct net_device *br_dev;
2131 struct dsa_slave_priv *p;
2132
2133 br_dev = netdev_master_upper_dev_get_rcu(dev);
2134 if (!br_dev)
2135 return NOTIFY_DONE;
2136
2137 if (!netif_is_bridge_master(br_dev))
2138 return NOTIFY_DONE;
2139
2140 p = dsa_slave_dev_lower_find(br_dev);
2141 if (!p)
2142 return NOTIFY_DONE;
2143
2144 dp = p->dp->cpu_dp;
2145
2146 if (!dp->ds->assisted_learning_on_cpu_port)
2147 return NOTIFY_DONE;
2148 }
Vladimir Oltean447d2902021-01-06 11:51:33 +02002149
Vladimir Oltean5fb4a452021-01-06 11:51:34 +02002150 if (!dp->ds->ops->port_fdb_add || !dp->ds->ops->port_fdb_del)
2151 return NOTIFY_DONE;
2152
Vladimir Oltean447d2902021-01-06 11:51:33 +02002153 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
2154 if (!switchdev_work)
2155 return NOTIFY_BAD;
2156
2157 INIT_WORK(&switchdev_work->work,
2158 dsa_slave_switchdev_event_work);
2159 switchdev_work->ds = dp->ds;
2160 switchdev_work->port = dp->index;
2161 switchdev_work->event = event;
2162
Vladimir Olteanc4bb76a2021-01-06 11:51:32 +02002163 ether_addr_copy(switchdev_work->addr,
2164 fdb_info->addr);
2165 switchdev_work->vid = fdb_info->vid;
2166
Vladimir Olteand5f19482021-01-06 11:51:35 +02002167 /* Hold a reference on the slave for dsa_fdb_offload_notify */
2168 if (dsa_is_user_port(dp->ds, dp->index))
2169 dev_hold(dev);
Vladimir Oltean447d2902021-01-06 11:51:33 +02002170 dsa_schedule_work(&switchdev_work->work);
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03002171 break;
2172 default:
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03002173 return NOTIFY_DONE;
2174 }
2175
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03002176 return NOTIFY_OK;
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03002177}
2178
Petr Machata2b239f62018-11-22 23:29:05 +00002179static int dsa_slave_switchdev_blocking_event(struct notifier_block *unused,
2180 unsigned long event, void *ptr)
2181{
2182 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
Vivien Didelot79b139f2019-06-14 13:49:22 -04002183 int err;
Petr Machata2b239f62018-11-22 23:29:05 +00002184
2185 switch (event) {
Vivien Didelot79b139f2019-06-14 13:49:22 -04002186 case SWITCHDEV_PORT_OBJ_ADD:
2187 err = switchdev_handle_port_obj_add(dev, ptr,
2188 dsa_slave_dev_check,
2189 dsa_slave_port_obj_add);
2190 return notifier_from_errno(err);
Petr Machata2b239f62018-11-22 23:29:05 +00002191 case SWITCHDEV_PORT_OBJ_DEL:
Vivien Didelot79b139f2019-06-14 13:49:22 -04002192 err = switchdev_handle_port_obj_del(dev, ptr,
2193 dsa_slave_dev_check,
2194 dsa_slave_port_obj_del);
2195 return notifier_from_errno(err);
Florian Fainelli9ed1ece2019-02-27 11:44:27 -08002196 case SWITCHDEV_PORT_ATTR_SET:
Vivien Didelot79b139f2019-06-14 13:49:22 -04002197 err = switchdev_handle_port_attr_set(dev, ptr,
2198 dsa_slave_dev_check,
2199 dsa_slave_port_attr_set);
2200 return notifier_from_errno(err);
Petr Machata2b239f62018-11-22 23:29:05 +00002201 }
2202
2203 return NOTIFY_DONE;
2204}
2205
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05002206static struct notifier_block dsa_slave_nb __read_mostly = {
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03002207 .notifier_call = dsa_slave_netdevice_event,
2208};
2209
2210static struct notifier_block dsa_slave_switchdev_notifier = {
2211 .notifier_call = dsa_slave_switchdev_event,
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05002212};
2213
Petr Machata2b239f62018-11-22 23:29:05 +00002214static struct notifier_block dsa_slave_switchdev_blocking_notifier = {
2215 .notifier_call = dsa_slave_switchdev_blocking_event,
2216};
2217
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05002218int dsa_slave_register_notifier(void)
2219{
Petr Machata2b239f62018-11-22 23:29:05 +00002220 struct notifier_block *nb;
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03002221 int err;
2222
2223 err = register_netdevice_notifier(&dsa_slave_nb);
2224 if (err)
2225 return err;
2226
2227 err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
2228 if (err)
2229 goto err_switchdev_nb;
2230
Petr Machata2b239f62018-11-22 23:29:05 +00002231 nb = &dsa_slave_switchdev_blocking_notifier;
2232 err = register_switchdev_blocking_notifier(nb);
2233 if (err)
2234 goto err_switchdev_blocking_nb;
2235
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03002236 return 0;
2237
Petr Machata2b239f62018-11-22 23:29:05 +00002238err_switchdev_blocking_nb:
2239 unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03002240err_switchdev_nb:
2241 unregister_netdevice_notifier(&dsa_slave_nb);
2242 return err;
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05002243}
2244
2245void dsa_slave_unregister_notifier(void)
2246{
Petr Machata2b239f62018-11-22 23:29:05 +00002247 struct notifier_block *nb;
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05002248 int err;
2249
Petr Machata2b239f62018-11-22 23:29:05 +00002250 nb = &dsa_slave_switchdev_blocking_notifier;
2251 err = unregister_switchdev_blocking_notifier(nb);
2252 if (err)
2253 pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
2254
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03002255 err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
2256 if (err)
2257 pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
2258
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05002259 err = unregister_netdevice_notifier(&dsa_slave_nb);
2260 if (err)
2261 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
2262}