blob: 18561af7a8f1da0ec92773376ecac9a72a64751c [file] [log] [blame]
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001/*
2 * net/dsa/slave.c - Slave device handling
Lennert Buytenheke84665c2009-03-20 09:52:09 +00003 * Copyright (c) 2008-2009 Marvell Semiconductor
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 */
10
11#include <linux/list.h>
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080012#include <linux/etherdevice.h>
Florian Fainellib73adef2015-02-24 13:15:33 -080013#include <linux/netdevice.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000014#include <linux/phy.h>
Florian Fainellia2820542014-10-17 16:02:13 -070015#include <linux/phy_fixed.h>
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070016#include <linux/of_net.h>
17#include <linux/of_mdio.h>
Andrew Lunn7f854422016-01-06 20:11:18 +010018#include <linux/mdio.h>
Florian Fainellib73adef2015-02-24 13:15:33 -080019#include <net/rtnetlink.h>
Florian Fainellif50f2122017-01-30 12:41:40 -080020#include <net/pkt_cls.h>
21#include <net/tc_act/tc_mirred.h>
Florian Fainellib73adef2015-02-24 13:15:33 -080022#include <linux/if_bridge.h>
Florian Fainelli04ff53f2015-07-31 11:42:57 -070023#include <linux/netpoll.h>
Brandon Streiff90af1052018-02-14 01:07:49 +010024#include <linux/ptp_classify.h>
Vivien Didelotea5dd342017-05-17 15:46:03 -040025
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000026#include "dsa_priv.h"
27
Florian Fainellif50f2122017-01-30 12:41:40 -080028static bool dsa_slave_dev_check(struct net_device *dev);
29
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000030/* slave mii_bus handling ***************************************************/
31static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
32{
33 struct dsa_switch *ds = bus->priv;
34
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070035 if (ds->phys_mii_mask & (1 << addr))
Vivien Didelot9d490b42016-08-23 12:38:56 -040036 return ds->ops->phy_read(ds, addr, reg);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000037
38 return 0xffff;
39}
40
41static int dsa_slave_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
42{
43 struct dsa_switch *ds = bus->priv;
44
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -070045 if (ds->phys_mii_mask & (1 << addr))
Vivien Didelot9d490b42016-08-23 12:38:56 -040046 return ds->ops->phy_write(ds, addr, reg, val);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000047
48 return 0;
49}
50
51void dsa_slave_mii_bus_init(struct dsa_switch *ds)
52{
53 ds->slave_mii_bus->priv = (void *)ds;
54 ds->slave_mii_bus->name = "dsa slave smi";
55 ds->slave_mii_bus->read = dsa_slave_phy_read;
56 ds->slave_mii_bus->write = dsa_slave_phy_write;
Florian Fainelli0b7b4982016-06-07 16:32:38 -070057 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
Vivien Didelot49463b72017-11-03 19:05:21 -040058 ds->dst->index, ds->index);
Andrew Lunnc33063d2016-05-10 23:27:23 +020059 ds->slave_mii_bus->parent = ds->dev;
Vivien Didelot24df8982015-01-20 19:13:32 -050060 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000061}
62
63
64/* slave device handling ****************************************************/
Nicolas Dichtelabd2be02015-04-02 17:07:08 +020065static int dsa_slave_get_iflink(const struct net_device *dev)
Lennert Buytenhekc0840802009-03-20 09:49:49 +000066{
Vivien Didelotd0006b02017-10-16 11:12:16 -040067 return dsa_slave_to_master(dev)->ifindex;
Lennert Buytenhekc0840802009-03-20 09:49:49 +000068}
69
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000070static int dsa_slave_open(struct net_device *dev)
71{
Vivien Didelotd0006b02017-10-16 11:12:16 -040072 struct net_device *master = dsa_slave_to_master(dev);
Vivien Didelotd9450972017-10-16 11:12:15 -040073 struct dsa_port *dp = dsa_slave_to_port(dev);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080074 int err;
75
76 if (!(master->flags & IFF_UP))
77 return -ENETDOWN;
78
Joe Perches8feedbb2012-05-08 18:56:57 +000079 if (!ether_addr_equal(dev->dev_addr, master->dev_addr)) {
Jiri Pirkoa748ee22010-04-01 21:22:09 +000080 err = dev_uc_add(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -080081 if (err < 0)
82 goto out;
83 }
84
85 if (dev->flags & IFF_ALLMULTI) {
86 err = dev_set_allmulti(master, 1);
87 if (err < 0)
88 goto del_unicast;
89 }
90 if (dev->flags & IFF_PROMISC) {
91 err = dev_set_promiscuity(master, 1);
92 if (err < 0)
93 goto clear_allmulti;
94 }
95
Vivien Didelot0115dcd2017-09-26 17:15:32 -040096 err = dsa_port_enable(dp, dev->phydev);
Vivien Didelotfb8a6a22017-09-22 19:01:56 -040097 if (err)
98 goto clear_promisc;
Florian Fainellib73adef2015-02-24 13:15:33 -080099
Vivien Didelot0115dcd2017-09-26 17:15:32 -0400100 if (dev->phydev)
101 phy_start(dev->phydev);
Florian Fainellif7f1de52014-09-24 17:05:17 -0700102
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000103 return 0;
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800104
Florian Fainellib2f2af22014-09-24 17:05:18 -0700105clear_promisc:
106 if (dev->flags & IFF_PROMISC)
Gilad Ben-Yossef4fdeddf2015-06-25 16:50:13 +0300107 dev_set_promiscuity(master, -1);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800108clear_allmulti:
109 if (dev->flags & IFF_ALLMULTI)
110 dev_set_allmulti(master, -1);
111del_unicast:
Joe Perches8feedbb2012-05-08 18:56:57 +0000112 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000113 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800114out:
115 return err;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000116}
117
118static int dsa_slave_close(struct net_device *dev)
119{
Vivien Didelotd0006b02017-10-16 11:12:16 -0400120 struct net_device *master = dsa_slave_to_master(dev);
Vivien Didelotd9450972017-10-16 11:12:15 -0400121 struct dsa_port *dp = dsa_slave_to_port(dev);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800122
Vivien Didelot0115dcd2017-09-26 17:15:32 -0400123 if (dev->phydev)
124 phy_stop(dev->phydev);
Florian Fainellif7f1de52014-09-24 17:05:17 -0700125
Vivien Didelot0115dcd2017-09-26 17:15:32 -0400126 dsa_port_disable(dp, dev->phydev);
Vivien Didelot6457edf2017-09-22 19:01:55 -0400127
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800128 dev_mc_unsync(master, dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000129 dev_uc_unsync(master, dev);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800130 if (dev->flags & IFF_ALLMULTI)
131 dev_set_allmulti(master, -1);
132 if (dev->flags & IFF_PROMISC)
133 dev_set_promiscuity(master, -1);
134
Joe Perches8feedbb2012-05-08 18:56:57 +0000135 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000136 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800137
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000138 return 0;
139}
140
141static void dsa_slave_change_rx_flags(struct net_device *dev, int change)
142{
Vivien Didelotd0006b02017-10-16 11:12:16 -0400143 struct net_device *master = dsa_slave_to_master(dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000144
145 if (change & IFF_ALLMULTI)
146 dev_set_allmulti(master, dev->flags & IFF_ALLMULTI ? 1 : -1);
147 if (change & IFF_PROMISC)
148 dev_set_promiscuity(master, dev->flags & IFF_PROMISC ? 1 : -1);
149}
150
151static void dsa_slave_set_rx_mode(struct net_device *dev)
152{
Vivien Didelotd0006b02017-10-16 11:12:16 -0400153 struct net_device *master = dsa_slave_to_master(dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000154
155 dev_mc_sync(master, dev);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000156 dev_uc_sync(master, dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000157}
158
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800159static int dsa_slave_set_mac_address(struct net_device *dev, void *a)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000160{
Vivien Didelotd0006b02017-10-16 11:12:16 -0400161 struct net_device *master = dsa_slave_to_master(dev);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800162 struct sockaddr *addr = a;
163 int err;
164
165 if (!is_valid_ether_addr(addr->sa_data))
166 return -EADDRNOTAVAIL;
167
168 if (!(dev->flags & IFF_UP))
169 goto out;
170
Joe Perches8feedbb2012-05-08 18:56:57 +0000171 if (!ether_addr_equal(addr->sa_data, master->dev_addr)) {
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000172 err = dev_uc_add(master, addr->sa_data);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800173 if (err < 0)
174 return err;
175 }
176
Joe Perches8feedbb2012-05-08 18:56:57 +0000177 if (!ether_addr_equal(dev->dev_addr, master->dev_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000178 dev_uc_del(master, dev->dev_addr);
Lennert Buytenhekdf02c6f2008-11-10 21:53:12 -0800179
180out:
Joe Perchesd08f1612014-01-20 09:52:20 -0800181 ether_addr_copy(dev->dev_addr, addr->sa_data);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000182
183 return 0;
184}
185
Arkadi Sharshevsky2bedde12017-08-06 16:15:49 +0300186struct dsa_slave_dump_ctx {
187 struct net_device *dev;
188 struct sk_buff *skb;
189 struct netlink_callback *cb;
190 int idx;
191};
192
193static int
194dsa_slave_port_fdb_do_dump(const unsigned char *addr, u16 vid,
195 bool is_static, void *data)
196{
197 struct dsa_slave_dump_ctx *dump = data;
198 u32 portid = NETLINK_CB(dump->cb->skb).portid;
199 u32 seq = dump->cb->nlh->nlmsg_seq;
200 struct nlmsghdr *nlh;
201 struct ndmsg *ndm;
202
203 if (dump->idx < dump->cb->args[2])
204 goto skip;
205
206 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
207 sizeof(*ndm), NLM_F_MULTI);
208 if (!nlh)
209 return -EMSGSIZE;
210
211 ndm = nlmsg_data(nlh);
212 ndm->ndm_family = AF_BRIDGE;
213 ndm->ndm_pad1 = 0;
214 ndm->ndm_pad2 = 0;
215 ndm->ndm_flags = NTF_SELF;
216 ndm->ndm_type = 0;
217 ndm->ndm_ifindex = dump->dev->ifindex;
218 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
219
220 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
221 goto nla_put_failure;
222
223 if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
224 goto nla_put_failure;
225
226 nlmsg_end(dump->skb, nlh);
227
228skip:
229 dump->idx++;
230 return 0;
231
232nla_put_failure:
233 nlmsg_cancel(dump->skb, nlh);
234 return -EMSGSIZE;
235}
236
237static int
238dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
239 struct net_device *dev, struct net_device *filter_dev,
240 int *idx)
241{
Vivien Didelotd9450972017-10-16 11:12:15 -0400242 struct dsa_port *dp = dsa_slave_to_port(dev);
Arkadi Sharshevsky2bedde12017-08-06 16:15:49 +0300243 struct dsa_slave_dump_ctx dump = {
244 .dev = dev,
245 .skb = skb,
246 .cb = cb,
247 .idx = *idx,
248 };
Arkadi Sharshevsky2bedde12017-08-06 16:15:49 +0300249 int err;
250
Vivien Didelotde40fc52017-09-20 19:32:14 -0400251 err = dsa_port_fdb_dump(dp, dsa_slave_port_fdb_do_dump, &dump);
Arkadi Sharshevsky2bedde12017-08-06 16:15:49 +0300252 *idx = dump.idx;
Vivien Didelotde40fc52017-09-20 19:32:14 -0400253
Arkadi Sharshevsky2bedde12017-08-06 16:15:49 +0300254 return err;
255}
256
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000257static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
258{
Brandon Streiff03363692018-02-14 01:07:48 +0100259 struct dsa_slave_priv *p = netdev_priv(dev);
260 struct dsa_switch *ds = p->dp->ds;
261 int port = p->dp->index;
262
263 /* Pass through to switch driver if it supports timestamping */
264 switch (cmd) {
265 case SIOCGHWTSTAMP:
266 if (ds->ops->port_hwtstamp_get)
267 return ds->ops->port_hwtstamp_get(ds, port, ifr);
268 break;
269 case SIOCSHWTSTAMP:
270 if (ds->ops->port_hwtstamp_set)
271 return ds->ops->port_hwtstamp_set(ds, port, ifr);
272 break;
273 }
274
Vivien Didelot0115dcd2017-09-26 17:15:32 -0400275 if (!dev->phydev)
Vivien Didelotf4344e02017-09-26 17:15:31 -0400276 return -ENODEV;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000277
Vivien Didelot0115dcd2017-09-26 17:15:32 -0400278 return phy_mii_ioctl(dev->phydev, ifr, cmd);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000279}
280
Scott Feldman35636062015-05-10 09:47:51 -0700281static int dsa_slave_port_attr_set(struct net_device *dev,
Jiri Pirkof7fadf32015-10-14 19:40:49 +0200282 const struct switchdev_attr *attr,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200283 struct switchdev_trans *trans)
Scott Feldman35636062015-05-10 09:47:51 -0700284{
Vivien Didelotd9450972017-10-16 11:12:15 -0400285 struct dsa_port *dp = dsa_slave_to_port(dev);
Vivien Didelotb8d866a2015-09-29 12:38:36 -0400286 int ret;
Scott Feldman35636062015-05-10 09:47:51 -0700287
288 switch (attr->id) {
Jiri Pirko1f868392015-10-01 11:03:42 +0200289 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
Vivien Didelotfd364542017-05-19 17:00:36 -0400290 ret = dsa_port_set_state(dp, attr->u.stp_state, trans);
Scott Feldman35636062015-05-10 09:47:51 -0700291 break;
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500292 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
Vivien Didelotc02c4172017-05-19 17:00:42 -0400293 ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
294 trans);
Vivien Didelotfb2daba2016-02-26 13:16:00 -0500295 break;
Vivien Didelot34a79f62016-07-18 20:45:38 -0400296 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
Vivien Didelot072bb192017-05-19 17:00:43 -0400297 ret = dsa_port_ageing_time(dp, attr->u.ageing_time, trans);
Vivien Didelot34a79f62016-07-18 20:45:38 -0400298 break;
Scott Feldman35636062015-05-10 09:47:51 -0700299 default:
300 ret = -EOPNOTSUPP;
301 break;
302 }
303
304 return ret;
305}
306
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400307static int dsa_slave_port_obj_add(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200308 const struct switchdev_obj *obj,
Jiri Pirko7ea6eb32015-09-24 10:02:41 +0200309 struct switchdev_trans *trans)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400310{
Vivien Didelotd9450972017-10-16 11:12:15 -0400311 struct dsa_port *dp = dsa_slave_to_port(dev);
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400312 int err;
313
314 /* For the prepare phase, ensure the full set of changes is feasable in
315 * one go in order to signal a failure properly. If an operation is not
316 * supported, return -EOPNOTSUPP.
317 */
318
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200319 switch (obj->id) {
Vivien Didelot8df30252016-08-31 11:50:03 -0400320 case SWITCHDEV_OBJ_ID_PORT_MDB:
Vivien Didelotbcebb972017-05-19 17:00:40 -0400321 err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj), trans);
Vivien Didelot8df30252016-08-31 11:50:03 -0400322 break;
Andrew Lunn5f4dbc52017-11-09 23:11:00 +0100323 case SWITCHDEV_OBJ_ID_HOST_MDB:
324 /* DSA can directly translate this to a normal MDB add,
325 * but on the CPU port.
326 */
327 err = dsa_port_mdb_add(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj),
328 trans);
329 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200330 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Vivien Didelot01676d12017-05-19 17:00:41 -0400331 err = dsa_port_vlan_add(dp, SWITCHDEV_OBJ_PORT_VLAN(obj),
332 trans);
Vivien Didelot11149532015-08-13 12:52:17 -0400333 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400334 default:
335 err = -EOPNOTSUPP;
336 break;
337 }
338
339 return err;
340}
341
342static int dsa_slave_port_obj_del(struct net_device *dev,
Jiri Pirko648b4a92015-10-01 11:03:45 +0200343 const struct switchdev_obj *obj)
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400344{
Vivien Didelotd9450972017-10-16 11:12:15 -0400345 struct dsa_port *dp = dsa_slave_to_port(dev);
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400346 int err;
347
Jiri Pirko9e8f4a52015-10-01 11:03:46 +0200348 switch (obj->id) {
Vivien Didelot8df30252016-08-31 11:50:03 -0400349 case SWITCHDEV_OBJ_ID_PORT_MDB:
Vivien Didelotbcebb972017-05-19 17:00:40 -0400350 err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
Vivien Didelot8df30252016-08-31 11:50:03 -0400351 break;
Andrew Lunn5f4dbc52017-11-09 23:11:00 +0100352 case SWITCHDEV_OBJ_ID_HOST_MDB:
353 /* DSA can directly translate this to a normal MDB add,
354 * but on the CPU port.
355 */
356 err = dsa_port_mdb_del(dp->cpu_dp, SWITCHDEV_OBJ_PORT_MDB(obj));
357 break;
Jiri Pirko57d80832015-10-01 11:03:41 +0200358 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Vivien Didelot01676d12017-05-19 17:00:41 -0400359 err = dsa_port_vlan_del(dp, SWITCHDEV_OBJ_PORT_VLAN(obj));
Vivien Didelot11149532015-08-13 12:52:17 -0400360 break;
Vivien Didelotba14d9e2015-08-10 09:09:53 -0400361 default:
362 err = -EOPNOTSUPP;
363 break;
364 }
365
366 return err;
367}
368
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700369static int dsa_slave_port_attr_get(struct net_device *dev,
370 struct switchdev_attr *attr)
Florian Fainellib73adef2015-02-24 13:15:33 -0800371{
Vivien Didelotd9450972017-10-16 11:12:15 -0400372 struct dsa_port *dp = dsa_slave_to_port(dev);
373 struct dsa_switch *ds = dp->ds;
Andrew Lunna42c8e32017-11-09 22:29:51 +0100374 struct dsa_switch_tree *dst = ds->dst;
Florian Fainellib73adef2015-02-24 13:15:33 -0800375
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700376 switch (attr->id) {
Jiri Pirko1f868392015-10-01 11:03:42 +0200377 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
Andrew Lunna42c8e32017-11-09 22:29:51 +0100378 attr->u.ppid.id_len = sizeof(dst->index);
379 memcpy(&attr->u.ppid.id, &dst->index, attr->u.ppid.id_len);
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700380 break;
Arkadi Sharshevskyc9e21052017-08-06 16:15:44 +0300381 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS_SUPPORT:
382 attr->u.brport_flags_support = 0;
383 break;
Scott Feldmanf8e20a92015-05-10 09:47:49 -0700384 default:
385 return -EOPNOTSUPP;
386 }
Florian Fainellib73adef2015-02-24 13:15:33 -0800387
388 return 0;
389}
390
Vivien Didelot4fa7b712017-09-20 19:31:57 -0400391static inline netdev_tx_t dsa_slave_netpoll_send_skb(struct net_device *dev,
392 struct sk_buff *skb)
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700393{
394#ifdef CONFIG_NET_POLL_CONTROLLER
Vivien Didelot4fa7b712017-09-20 19:31:57 -0400395 struct dsa_slave_priv *p = netdev_priv(dev);
396
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700397 if (p->netpoll)
398 netpoll_send_skb(p->netpoll, skb);
399#else
400 BUG();
401#endif
402 return NETDEV_TX_OK;
403}
404
Brandon Streiff90af1052018-02-14 01:07:49 +0100405static void dsa_skb_tx_timestamp(struct dsa_slave_priv *p,
406 struct sk_buff *skb)
407{
408 struct dsa_switch *ds = p->dp->ds;
409 struct sk_buff *clone;
410 unsigned int type;
411
412 type = ptp_classify_raw(skb);
413 if (type == PTP_CLASS_NONE)
414 return;
415
416 if (!ds->ops->port_txtstamp)
417 return;
418
419 clone = skb_clone_sk(skb);
420 if (!clone)
421 return;
422
423 if (ds->ops->port_txtstamp(ds, p->dp->index, clone, type))
424 return;
425
426 kfree_skb(clone);
427}
428
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700429static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
430{
431 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelli5f6b4e142017-08-03 21:33:27 -0700432 struct pcpu_sw_netstats *s;
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700433 struct sk_buff *nskb;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700434
Florian Fainelli5f6b4e142017-08-03 21:33:27 -0700435 s = this_cpu_ptr(p->stats64);
436 u64_stats_update_begin(&s->syncp);
437 s->tx_packets++;
438 s->tx_bytes += skb->len;
439 u64_stats_update_end(&s->syncp);
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700440
Brandon Streiff90af1052018-02-14 01:07:49 +0100441 /* Identify PTP protocol packets, clone them, and pass them to the
442 * switch driver
443 */
444 dsa_skb_tx_timestamp(p, skb);
445
Vivien Didelotfe47d562017-06-01 16:07:15 -0400446 /* Transmit function may have to reallocate the original SKB,
447 * in which case it must have freed it. Only free it here on error.
448 */
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700449 nskb = p->xmit(skb, dev);
Vivien Didelotfe47d562017-06-01 16:07:15 -0400450 if (!nskb) {
451 kfree_skb(skb);
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700452 return NETDEV_TX_OK;
Vivien Didelotfe47d562017-06-01 16:07:15 -0400453 }
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700454
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700455 /* SKB for netpoll still need to be mangled with the protocol-specific
456 * tag to be successfully transmitted
457 */
458 if (unlikely(netpoll_tx_running(dev)))
Vivien Didelot4fa7b712017-09-20 19:31:57 -0400459 return dsa_slave_netpoll_send_skb(dev, nskb);
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700460
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700461 /* Queue the SKB for transmission on the parent interface, but
462 * do not modify its EtherType
463 */
Vivien Didelotd0006b02017-10-16 11:12:16 -0400464 nskb->dev = dsa_slave_to_master(dev);
Florian Fainelli4ed70ce2015-07-31 11:42:56 -0700465 dev_queue_xmit(nskb);
Florian Fainelli5aed85c2014-08-27 17:04:52 -0700466
467 return NETDEV_TX_OK;
468}
469
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000470/* ethtool operations *******************************************************/
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000471
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000472static void dsa_slave_get_drvinfo(struct net_device *dev,
473 struct ethtool_drvinfo *drvinfo)
474{
Jiri Pirko7826d432013-01-06 00:44:26 +0000475 strlcpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
Jiri Pirko7826d432013-01-06 00:44:26 +0000476 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
477 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000478}
479
Guenter Roeck3d762a02014-10-29 10:45:04 -0700480static int dsa_slave_get_regs_len(struct net_device *dev)
481{
Vivien Didelotd9450972017-10-16 11:12:15 -0400482 struct dsa_port *dp = dsa_slave_to_port(dev);
483 struct dsa_switch *ds = dp->ds;
Guenter Roeck3d762a02014-10-29 10:45:04 -0700484
Vivien Didelot9d490b42016-08-23 12:38:56 -0400485 if (ds->ops->get_regs_len)
Vivien Didelotd9450972017-10-16 11:12:15 -0400486 return ds->ops->get_regs_len(ds, dp->index);
Guenter Roeck3d762a02014-10-29 10:45:04 -0700487
488 return -EOPNOTSUPP;
489}
490
491static void
492dsa_slave_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
493{
Vivien Didelotd9450972017-10-16 11:12:15 -0400494 struct dsa_port *dp = dsa_slave_to_port(dev);
495 struct dsa_switch *ds = dp->ds;
Guenter Roeck3d762a02014-10-29 10:45:04 -0700496
Vivien Didelot9d490b42016-08-23 12:38:56 -0400497 if (ds->ops->get_regs)
Vivien Didelotd9450972017-10-16 11:12:15 -0400498 ds->ops->get_regs(ds, dp->index, regs, _p);
Guenter Roeck3d762a02014-10-29 10:45:04 -0700499}
500
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000501static u32 dsa_slave_get_link(struct net_device *dev)
502{
Vivien Didelot0115dcd2017-09-26 17:15:32 -0400503 if (!dev->phydev)
Vivien Didelotf4344e02017-09-26 17:15:31 -0400504 return -ENODEV;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000505
Vivien Didelot0115dcd2017-09-26 17:15:32 -0400506 genphy_update_link(dev->phydev);
Vivien Didelotf4344e02017-09-26 17:15:31 -0400507
Vivien Didelot0115dcd2017-09-26 17:15:32 -0400508 return dev->phydev->link;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000509}
510
Guenter Roeck6793abb2014-10-29 10:45:01 -0700511static int dsa_slave_get_eeprom_len(struct net_device *dev)
512{
Vivien Didelotd9450972017-10-16 11:12:15 -0400513 struct dsa_port *dp = dsa_slave_to_port(dev);
514 struct dsa_switch *ds = dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700515
Andrew Lunn0e576042016-06-04 21:16:52 +0200516 if (ds->cd && ds->cd->eeprom_len)
Andrew Lunnff049552016-05-10 23:27:24 +0200517 return ds->cd->eeprom_len;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700518
Vivien Didelot9d490b42016-08-23 12:38:56 -0400519 if (ds->ops->get_eeprom_len)
520 return ds->ops->get_eeprom_len(ds);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700521
522 return 0;
523}
524
525static int dsa_slave_get_eeprom(struct net_device *dev,
526 struct ethtool_eeprom *eeprom, u8 *data)
527{
Vivien Didelotd9450972017-10-16 11:12:15 -0400528 struct dsa_port *dp = dsa_slave_to_port(dev);
529 struct dsa_switch *ds = dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700530
Vivien Didelot9d490b42016-08-23 12:38:56 -0400531 if (ds->ops->get_eeprom)
532 return ds->ops->get_eeprom(ds, eeprom, data);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700533
534 return -EOPNOTSUPP;
535}
536
537static int dsa_slave_set_eeprom(struct net_device *dev,
538 struct ethtool_eeprom *eeprom, u8 *data)
539{
Vivien Didelotd9450972017-10-16 11:12:15 -0400540 struct dsa_port *dp = dsa_slave_to_port(dev);
541 struct dsa_switch *ds = dp->ds;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700542
Vivien Didelot9d490b42016-08-23 12:38:56 -0400543 if (ds->ops->set_eeprom)
544 return ds->ops->set_eeprom(ds, eeprom, data);
Guenter Roeck6793abb2014-10-29 10:45:01 -0700545
546 return -EOPNOTSUPP;
547}
548
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000549static void dsa_slave_get_strings(struct net_device *dev,
550 uint32_t stringset, uint8_t *data)
551{
Vivien Didelotd9450972017-10-16 11:12:15 -0400552 struct dsa_port *dp = dsa_slave_to_port(dev);
553 struct dsa_switch *ds = dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000554
555 if (stringset == ETH_SS_STATS) {
556 int len = ETH_GSTRING_LEN;
557
558 strncpy(data, "tx_packets", len);
559 strncpy(data + len, "tx_bytes", len);
560 strncpy(data + 2 * len, "rx_packets", len);
561 strncpy(data + 3 * len, "rx_bytes", len);
Vivien Didelot9d490b42016-08-23 12:38:56 -0400562 if (ds->ops->get_strings)
Vivien Didelotd9450972017-10-16 11:12:15 -0400563 ds->ops->get_strings(ds, dp->index, data + 4 * len);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000564 }
565}
566
567static void dsa_slave_get_ethtool_stats(struct net_device *dev,
568 struct ethtool_stats *stats,
569 uint64_t *data)
570{
Vivien Didelotd9450972017-10-16 11:12:15 -0400571 struct dsa_port *dp = dsa_slave_to_port(dev);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000572 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotd9450972017-10-16 11:12:15 -0400573 struct dsa_switch *ds = dp->ds;
Florian Fainelli5f6b4e142017-08-03 21:33:27 -0700574 struct pcpu_sw_netstats *s;
Florian Fainellif613ed62017-08-01 15:00:36 -0700575 unsigned int start;
Florian Fainelli5f6b4e142017-08-03 21:33:27 -0700576 int i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000577
Florian Fainelli5f6b4e142017-08-03 21:33:27 -0700578 for_each_possible_cpu(i) {
579 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
580
581 s = per_cpu_ptr(p->stats64, i);
582 do {
583 start = u64_stats_fetch_begin_irq(&s->syncp);
584 tx_packets = s->tx_packets;
585 tx_bytes = s->tx_bytes;
586 rx_packets = s->rx_packets;
587 rx_bytes = s->rx_bytes;
588 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
589 data[0] += tx_packets;
590 data[1] += tx_bytes;
591 data[2] += rx_packets;
592 data[3] += rx_bytes;
593 }
Vivien Didelot9d490b42016-08-23 12:38:56 -0400594 if (ds->ops->get_ethtool_stats)
Vivien Didelotd9450972017-10-16 11:12:15 -0400595 ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000596}
597
598static int dsa_slave_get_sset_count(struct net_device *dev, int sset)
599{
Vivien Didelotd9450972017-10-16 11:12:15 -0400600 struct dsa_port *dp = dsa_slave_to_port(dev);
601 struct dsa_switch *ds = dp->ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000602
603 if (sset == ETH_SS_STATS) {
604 int count;
605
606 count = 4;
Vivien Didelot9d490b42016-08-23 12:38:56 -0400607 if (ds->ops->get_sset_count)
Andrew Lunn88c06052018-03-01 02:02:27 +0100608 count += ds->ops->get_sset_count(ds, dp->index);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000609
610 return count;
611 }
612
613 return -EOPNOTSUPP;
614}
615
Florian Fainelli19e57c42014-09-18 17:31:24 -0700616static void dsa_slave_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
617{
Vivien Didelotd9450972017-10-16 11:12:15 -0400618 struct dsa_port *dp = dsa_slave_to_port(dev);
619 struct dsa_switch *ds = dp->ds;
Florian Fainelli19e57c42014-09-18 17:31:24 -0700620
Vivien Didelot9d490b42016-08-23 12:38:56 -0400621 if (ds->ops->get_wol)
Vivien Didelotd9450972017-10-16 11:12:15 -0400622 ds->ops->get_wol(ds, dp->index, w);
Florian Fainelli19e57c42014-09-18 17:31:24 -0700623}
624
625static int dsa_slave_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
626{
Vivien Didelotd9450972017-10-16 11:12:15 -0400627 struct dsa_port *dp = dsa_slave_to_port(dev);
628 struct dsa_switch *ds = dp->ds;
Florian Fainelli19e57c42014-09-18 17:31:24 -0700629 int ret = -EOPNOTSUPP;
630
Vivien Didelot9d490b42016-08-23 12:38:56 -0400631 if (ds->ops->set_wol)
Vivien Didelotd9450972017-10-16 11:12:15 -0400632 ret = ds->ops->set_wol(ds, dp->index, w);
Florian Fainelli19e57c42014-09-18 17:31:24 -0700633
634 return ret;
635}
636
Florian Fainelli79052882014-09-24 17:05:21 -0700637static int dsa_slave_set_eee(struct net_device *dev, struct ethtool_eee *e)
638{
Vivien Didelotd9450972017-10-16 11:12:15 -0400639 struct dsa_port *dp = dsa_slave_to_port(dev);
640 struct dsa_switch *ds = dp->ds;
Florian Fainelli79052882014-09-24 17:05:21 -0700641 int ret;
642
Vivien Didelot7b9cc732017-08-01 16:32:31 -0400643 /* Port's PHY and MAC both need to be EEE capable */
Vivien Didelot0115dcd2017-09-26 17:15:32 -0400644 if (!dev->phydev)
Vivien Didelot7b9cc732017-08-01 16:32:31 -0400645 return -ENODEV;
646
Vivien Didelot08f50062017-08-01 16:32:41 -0400647 if (!ds->ops->set_mac_eee)
Florian Fainelli79052882014-09-24 17:05:21 -0700648 return -EOPNOTSUPP;
649
Vivien Didelotd9450972017-10-16 11:12:15 -0400650 ret = ds->ops->set_mac_eee(ds, dp->index, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700651 if (ret)
652 return ret;
653
Vivien Didelotc48f7eb2017-08-01 16:32:38 -0400654 if (e->eee_enabled) {
Vivien Didelot0115dcd2017-09-26 17:15:32 -0400655 ret = phy_init_eee(dev->phydev, 0);
Vivien Didelotc48f7eb2017-08-01 16:32:38 -0400656 if (ret)
657 return ret;
658 }
659
Vivien Didelot0115dcd2017-09-26 17:15:32 -0400660 return phy_ethtool_set_eee(dev->phydev, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700661}
662
663static int dsa_slave_get_eee(struct net_device *dev, struct ethtool_eee *e)
664{
Vivien Didelotd9450972017-10-16 11:12:15 -0400665 struct dsa_port *dp = dsa_slave_to_port(dev);
666 struct dsa_switch *ds = dp->ds;
Florian Fainelli79052882014-09-24 17:05:21 -0700667 int ret;
668
Vivien Didelot7b9cc732017-08-01 16:32:31 -0400669 /* Port's PHY and MAC both need to be EEE capable */
Vivien Didelot0115dcd2017-09-26 17:15:32 -0400670 if (!dev->phydev)
Vivien Didelot7b9cc732017-08-01 16:32:31 -0400671 return -ENODEV;
672
Vivien Didelot08f50062017-08-01 16:32:41 -0400673 if (!ds->ops->get_mac_eee)
Florian Fainelli79052882014-09-24 17:05:21 -0700674 return -EOPNOTSUPP;
675
Vivien Didelotd9450972017-10-16 11:12:15 -0400676 ret = ds->ops->get_mac_eee(ds, dp->index, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700677 if (ret)
678 return ret;
679
Vivien Didelot0115dcd2017-09-26 17:15:32 -0400680 return phy_ethtool_get_eee(dev->phydev, e);
Florian Fainelli79052882014-09-24 17:05:21 -0700681}
682
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700683#ifdef CONFIG_NET_POLL_CONTROLLER
684static int dsa_slave_netpoll_setup(struct net_device *dev,
685 struct netpoll_info *ni)
686{
Vivien Didelotd0006b02017-10-16 11:12:16 -0400687 struct net_device *master = dsa_slave_to_master(dev);
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700688 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelli04ff53f2015-07-31 11:42:57 -0700689 struct netpoll *netpoll;
690 int err = 0;
691
692 netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
693 if (!netpoll)
694 return -ENOMEM;
695
696 err = __netpoll_setup(netpoll, master);
697 if (err) {
698 kfree(netpoll);
699 goto out;
700 }
701
702 p->netpoll = netpoll;
703out:
704 return err;
705}
706
707static void dsa_slave_netpoll_cleanup(struct net_device *dev)
708{
709 struct dsa_slave_priv *p = netdev_priv(dev);
710 struct netpoll *netpoll = p->netpoll;
711
712 if (!netpoll)
713 return;
714
715 p->netpoll = NULL;
716
717 __netpoll_free_async(netpoll);
718}
719
720static void dsa_slave_poll_controller(struct net_device *dev)
721{
722}
723#endif
724
Florian Fainelli44bb7652017-01-10 12:32:36 -0800725static int dsa_slave_get_phys_port_name(struct net_device *dev,
726 char *name, size_t len)
727{
Vivien Didelotd9450972017-10-16 11:12:15 -0400728 struct dsa_port *dp = dsa_slave_to_port(dev);
Florian Fainelli44bb7652017-01-10 12:32:36 -0800729
Vivien Didelotd9450972017-10-16 11:12:15 -0400730 if (snprintf(name, len, "p%d", dp->index) >= len)
Florian Fainelli44bb7652017-01-10 12:32:36 -0800731 return -EINVAL;
Florian Fainelli3a543ef2016-12-29 14:20:56 -0800732
733 return 0;
734}
735
Florian Fainellif50f2122017-01-30 12:41:40 -0800736static struct dsa_mall_tc_entry *
Vivien Didelot4fa7b712017-09-20 19:31:57 -0400737dsa_slave_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
Florian Fainellif50f2122017-01-30 12:41:40 -0800738{
Vivien Didelot4fa7b712017-09-20 19:31:57 -0400739 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainellif50f2122017-01-30 12:41:40 -0800740 struct dsa_mall_tc_entry *mall_tc_entry;
741
742 list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
743 if (mall_tc_entry->cookie == cookie)
744 return mall_tc_entry;
745
746 return NULL;
747}
748
749static int dsa_slave_add_cls_matchall(struct net_device *dev,
Florian Fainellif50f2122017-01-30 12:41:40 -0800750 struct tc_cls_matchall_offload *cls,
751 bool ingress)
752{
Vivien Didelotd9450972017-10-16 11:12:15 -0400753 struct dsa_port *dp = dsa_slave_to_port(dev);
Florian Fainellif50f2122017-01-30 12:41:40 -0800754 struct dsa_slave_priv *p = netdev_priv(dev);
755 struct dsa_mall_tc_entry *mall_tc_entry;
Jiri Pirko5fd9fc42017-08-07 10:15:29 +0200756 __be16 protocol = cls->common.protocol;
Vivien Didelotd9450972017-10-16 11:12:15 -0400757 struct dsa_switch *ds = dp->ds;
Florian Fainellif50f2122017-01-30 12:41:40 -0800758 struct net_device *to_dev;
759 const struct tc_action *a;
Vivien Didelotd9450972017-10-16 11:12:15 -0400760 struct dsa_port *to_dp;
Florian Fainellif50f2122017-01-30 12:41:40 -0800761 int err = -EOPNOTSUPP;
762 LIST_HEAD(actions);
Florian Fainellif50f2122017-01-30 12:41:40 -0800763
764 if (!ds->ops->port_mirror_add)
765 return err;
766
Jiri Pirko3bcc0ce2017-08-04 14:28:58 +0200767 if (!tcf_exts_has_one_action(cls->exts))
Florian Fainellif50f2122017-01-30 12:41:40 -0800768 return err;
769
770 tcf_exts_to_list(cls->exts, &actions);
771 a = list_first_entry(&actions, struct tc_action, list);
772
773 if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
774 struct dsa_mall_mirror_tc_entry *mirror;
775
Cong Wang9f8a7392017-12-05 16:17:26 -0800776 to_dev = tcf_mirred_dev(a);
Florian Fainellif50f2122017-01-30 12:41:40 -0800777 if (!to_dev)
778 return -EINVAL;
779
780 if (!dsa_slave_dev_check(to_dev))
781 return -EOPNOTSUPP;
782
783 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
784 if (!mall_tc_entry)
785 return -ENOMEM;
786
787 mall_tc_entry->cookie = cls->cookie;
788 mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
789 mirror = &mall_tc_entry->mirror;
790
Vivien Didelotd9450972017-10-16 11:12:15 -0400791 to_dp = dsa_slave_to_port(to_dev);
Florian Fainellif50f2122017-01-30 12:41:40 -0800792
Vivien Didelotd9450972017-10-16 11:12:15 -0400793 mirror->to_local_port = to_dp->index;
Florian Fainellif50f2122017-01-30 12:41:40 -0800794 mirror->ingress = ingress;
795
Vivien Didelotd9450972017-10-16 11:12:15 -0400796 err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress);
Florian Fainellif50f2122017-01-30 12:41:40 -0800797 if (err) {
798 kfree(mall_tc_entry);
799 return err;
800 }
801
802 list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
803 }
804
805 return 0;
806}
807
808static void dsa_slave_del_cls_matchall(struct net_device *dev,
809 struct tc_cls_matchall_offload *cls)
810{
Vivien Didelotd9450972017-10-16 11:12:15 -0400811 struct dsa_port *dp = dsa_slave_to_port(dev);
Florian Fainellif50f2122017-01-30 12:41:40 -0800812 struct dsa_mall_tc_entry *mall_tc_entry;
Vivien Didelotd9450972017-10-16 11:12:15 -0400813 struct dsa_switch *ds = dp->ds;
Florian Fainellif50f2122017-01-30 12:41:40 -0800814
815 if (!ds->ops->port_mirror_del)
816 return;
817
Vivien Didelot4fa7b712017-09-20 19:31:57 -0400818 mall_tc_entry = dsa_slave_mall_tc_entry_find(dev, cls->cookie);
Florian Fainellif50f2122017-01-30 12:41:40 -0800819 if (!mall_tc_entry)
820 return;
821
822 list_del(&mall_tc_entry->list);
823
824 switch (mall_tc_entry->type) {
825 case DSA_PORT_MALL_MIRROR:
Vivien Didelotd9450972017-10-16 11:12:15 -0400826 ds->ops->port_mirror_del(ds, dp->index, &mall_tc_entry->mirror);
Florian Fainellif50f2122017-01-30 12:41:40 -0800827 break;
828 default:
829 WARN_ON(1);
830 }
831
832 kfree(mall_tc_entry);
833}
834
Jiri Pirko3fbae382017-08-07 10:15:26 +0200835static int dsa_slave_setup_tc_cls_matchall(struct net_device *dev,
Jiri Pirko6b3eb752017-10-19 15:50:45 +0200836 struct tc_cls_matchall_offload *cls,
837 bool ingress)
Florian Fainellif50f2122017-01-30 12:41:40 -0800838{
Jiri Pirko5fd9fc42017-08-07 10:15:29 +0200839 if (cls->common.chain_index)
Jiri Pirkoa5fcf8a2017-06-06 17:00:16 +0200840 return -EOPNOTSUPP;
Florian Fainellif50f2122017-01-30 12:41:40 -0800841
Jiri Pirko3fbae382017-08-07 10:15:26 +0200842 switch (cls->command) {
843 case TC_CLSMATCHALL_REPLACE:
Jiri Pirko5fd9fc42017-08-07 10:15:29 +0200844 return dsa_slave_add_cls_matchall(dev, cls, ingress);
Jiri Pirko3fbae382017-08-07 10:15:26 +0200845 case TC_CLSMATCHALL_DESTROY:
846 dsa_slave_del_cls_matchall(dev, cls);
847 return 0;
848 default:
849 return -EOPNOTSUPP;
850 }
851}
852
Jiri Pirko6b3eb752017-10-19 15:50:45 +0200853static int dsa_slave_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
854 void *cb_priv, bool ingress)
855{
856 struct net_device *dev = cb_priv;
857
Jiri Pirko44ae12a2017-11-01 11:47:39 +0100858 if (!tc_can_offload(dev))
859 return -EOPNOTSUPP;
860
Jiri Pirko6b3eb752017-10-19 15:50:45 +0200861 switch (type) {
862 case TC_SETUP_CLSMATCHALL:
863 return dsa_slave_setup_tc_cls_matchall(dev, type_data, ingress);
864 default:
865 return -EOPNOTSUPP;
866 }
867}
868
869static int dsa_slave_setup_tc_block_cb_ig(enum tc_setup_type type,
870 void *type_data, void *cb_priv)
871{
872 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, true);
873}
874
875static int dsa_slave_setup_tc_block_cb_eg(enum tc_setup_type type,
876 void *type_data, void *cb_priv)
877{
878 return dsa_slave_setup_tc_block_cb(type, type_data, cb_priv, false);
879}
880
881static int dsa_slave_setup_tc_block(struct net_device *dev,
882 struct tc_block_offload *f)
883{
884 tc_setup_cb_t *cb;
885
886 if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
887 cb = dsa_slave_setup_tc_block_cb_ig;
888 else if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
889 cb = dsa_slave_setup_tc_block_cb_eg;
890 else
891 return -EOPNOTSUPP;
892
893 switch (f->command) {
894 case TC_BLOCK_BIND:
895 return tcf_block_cb_register(f->block, cb, dev, dev);
896 case TC_BLOCK_UNBIND:
897 tcf_block_cb_unregister(f->block, cb, dev);
898 return 0;
899 default:
900 return -EOPNOTSUPP;
901 }
902}
903
Jiri Pirko3fbae382017-08-07 10:15:26 +0200904static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
Jiri Pirkode4784c2017-08-07 10:15:32 +0200905 void *type_data)
Jiri Pirko3fbae382017-08-07 10:15:26 +0200906{
Jiri Pirko2572ac52017-08-07 10:15:17 +0200907 switch (type) {
Jiri Pirko6b3eb752017-10-19 15:50:45 +0200908 case TC_SETUP_BLOCK:
909 return dsa_slave_setup_tc_block(dev, type_data);
Florian Fainellif50f2122017-01-30 12:41:40 -0800910 default:
Jiri Pirkoa5fcf8a2017-06-06 17:00:16 +0200911 return -EOPNOTSUPP;
Florian Fainellif50f2122017-01-30 12:41:40 -0800912 }
Florian Fainellif50f2122017-01-30 12:41:40 -0800913}
914
Florian Fainellif613ed62017-08-01 15:00:36 -0700915static void dsa_slave_get_stats64(struct net_device *dev,
916 struct rtnl_link_stats64 *stats)
917{
918 struct dsa_slave_priv *p = netdev_priv(dev);
Florian Fainelli5f6b4e142017-08-03 21:33:27 -0700919 struct pcpu_sw_netstats *s;
Florian Fainellif613ed62017-08-01 15:00:36 -0700920 unsigned int start;
Florian Fainelli5f6b4e142017-08-03 21:33:27 -0700921 int i;
Florian Fainellif613ed62017-08-01 15:00:36 -0700922
923 netdev_stats_to_stats64(stats, &dev->stats);
Florian Fainelli5f6b4e142017-08-03 21:33:27 -0700924 for_each_possible_cpu(i) {
925 u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
926
927 s = per_cpu_ptr(p->stats64, i);
928 do {
929 start = u64_stats_fetch_begin_irq(&s->syncp);
930 tx_packets = s->tx_packets;
931 tx_bytes = s->tx_bytes;
932 rx_packets = s->rx_packets;
933 rx_bytes = s->rx_bytes;
934 } while (u64_stats_fetch_retry_irq(&s->syncp, start));
935
936 stats->tx_packets += tx_packets;
937 stats->tx_bytes += tx_bytes;
938 stats->rx_packets += rx_packets;
939 stats->rx_bytes += rx_bytes;
940 }
Florian Fainellif613ed62017-08-01 15:00:36 -0700941}
942
Florian Fainellibf9f2642017-01-30 09:48:40 -0800943static int dsa_slave_get_rxnfc(struct net_device *dev,
944 struct ethtool_rxnfc *nfc, u32 *rule_locs)
945{
Vivien Didelotd9450972017-10-16 11:12:15 -0400946 struct dsa_port *dp = dsa_slave_to_port(dev);
947 struct dsa_switch *ds = dp->ds;
Florian Fainellibf9f2642017-01-30 09:48:40 -0800948
949 if (!ds->ops->get_rxnfc)
950 return -EOPNOTSUPP;
951
Vivien Didelotd9450972017-10-16 11:12:15 -0400952 return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
Florian Fainellibf9f2642017-01-30 09:48:40 -0800953}
954
955static int dsa_slave_set_rxnfc(struct net_device *dev,
956 struct ethtool_rxnfc *nfc)
957{
Vivien Didelotd9450972017-10-16 11:12:15 -0400958 struct dsa_port *dp = dsa_slave_to_port(dev);
959 struct dsa_switch *ds = dp->ds;
Florian Fainellibf9f2642017-01-30 09:48:40 -0800960
961 if (!ds->ops->set_rxnfc)
962 return -EOPNOTSUPP;
963
Vivien Didelotd9450972017-10-16 11:12:15 -0400964 return ds->ops->set_rxnfc(ds, dp->index, nfc);
Florian Fainellibf9f2642017-01-30 09:48:40 -0800965}
966
Brandon Streiff03363692018-02-14 01:07:48 +0100967static int dsa_slave_get_ts_info(struct net_device *dev,
968 struct ethtool_ts_info *ts)
969{
970 struct dsa_slave_priv *p = netdev_priv(dev);
971 struct dsa_switch *ds = p->dp->ds;
972
973 if (!ds->ops->get_ts_info)
974 return -EOPNOTSUPP;
975
976 return ds->ops->get_ts_info(ds, p->dp->index, ts);
977}
978
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000979static const struct ethtool_ops dsa_slave_ethtool_ops = {
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000980 .get_drvinfo = dsa_slave_get_drvinfo,
Guenter Roeck3d762a02014-10-29 10:45:04 -0700981 .get_regs_len = dsa_slave_get_regs_len,
982 .get_regs = dsa_slave_get_regs,
Vivien Didelot69b2c1622017-09-26 17:15:35 -0400983 .nway_reset = phy_ethtool_nway_reset,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000984 .get_link = dsa_slave_get_link,
Guenter Roeck6793abb2014-10-29 10:45:01 -0700985 .get_eeprom_len = dsa_slave_get_eeprom_len,
986 .get_eeprom = dsa_slave_get_eeprom,
987 .set_eeprom = dsa_slave_set_eeprom,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000988 .get_strings = dsa_slave_get_strings,
989 .get_ethtool_stats = dsa_slave_get_ethtool_stats,
990 .get_sset_count = dsa_slave_get_sset_count,
Florian Fainelli19e57c42014-09-18 17:31:24 -0700991 .set_wol = dsa_slave_set_wol,
992 .get_wol = dsa_slave_get_wol,
Florian Fainelli79052882014-09-24 17:05:21 -0700993 .set_eee = dsa_slave_set_eee,
994 .get_eee = dsa_slave_get_eee,
Vivien Didelot771df312017-09-26 17:15:33 -0400995 .get_link_ksettings = phy_ethtool_get_link_ksettings,
Vivien Didelotaa62a8c2017-09-26 17:15:34 -0400996 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Florian Fainellibf9f2642017-01-30 09:48:40 -0800997 .get_rxnfc = dsa_slave_get_rxnfc,
998 .set_rxnfc = dsa_slave_set_rxnfc,
Brandon Streiff03363692018-02-14 01:07:48 +0100999 .get_ts_info = dsa_slave_get_ts_info,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001000};
1001
Florian Fainelli2a93c1a2017-12-06 15:03:33 -08001002/* legacy way, bypassing the bridge *****************************************/
1003int dsa_legacy_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
1004 struct net_device *dev,
1005 const unsigned char *addr, u16 vid,
1006 u16 flags)
1007{
1008 struct dsa_port *dp = dsa_slave_to_port(dev);
1009
1010 return dsa_port_fdb_add(dp, addr, vid);
1011}
1012
1013int dsa_legacy_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
1014 struct net_device *dev,
1015 const unsigned char *addr, u16 vid)
1016{
1017 struct dsa_port *dp = dsa_slave_to_port(dev);
1018
1019 return dsa_port_fdb_del(dp, addr, vid);
1020}
1021
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001022static const struct net_device_ops dsa_slave_netdev_ops = {
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001023 .ndo_open = dsa_slave_open,
1024 .ndo_stop = dsa_slave_close,
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001025 .ndo_start_xmit = dsa_slave_xmit,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001026 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
1027 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001028 .ndo_set_mac_address = dsa_slave_set_mac_address,
Arkadi Sharshevsky37b8da12017-08-06 16:15:43 +03001029 .ndo_fdb_add = dsa_legacy_fdb_add,
1030 .ndo_fdb_del = dsa_legacy_fdb_del,
Arkadi Sharshevsky2bedde12017-08-06 16:15:49 +03001031 .ndo_fdb_dump = dsa_slave_fdb_dump,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001032 .ndo_do_ioctl = dsa_slave_ioctl,
Nicolas Dichtelabd2be02015-04-02 17:07:08 +02001033 .ndo_get_iflink = dsa_slave_get_iflink,
Florian Fainelli04ff53f2015-07-31 11:42:57 -07001034#ifdef CONFIG_NET_POLL_CONTROLLER
1035 .ndo_netpoll_setup = dsa_slave_netpoll_setup,
1036 .ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
1037 .ndo_poll_controller = dsa_slave_poll_controller,
1038#endif
Florian Fainelli44bb7652017-01-10 12:32:36 -08001039 .ndo_get_phys_port_name = dsa_slave_get_phys_port_name,
Florian Fainellif50f2122017-01-30 12:41:40 -08001040 .ndo_setup_tc = dsa_slave_setup_tc,
Florian Fainellif613ed62017-08-01 15:00:36 -07001041 .ndo_get_stats64 = dsa_slave_get_stats64,
Scott Feldman98237d42015-03-15 21:07:15 -07001042};
1043
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07001044static const struct switchdev_ops dsa_slave_switchdev_ops = {
Scott Feldmanf8e20a92015-05-10 09:47:49 -07001045 .switchdev_port_attr_get = dsa_slave_port_attr_get,
Scott Feldman35636062015-05-10 09:47:51 -07001046 .switchdev_port_attr_set = dsa_slave_port_attr_set,
Vivien Didelotba14d9e2015-08-10 09:09:53 -04001047 .switchdev_port_obj_add = dsa_slave_port_obj_add,
1048 .switchdev_port_obj_del = dsa_slave_port_obj_del,
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001049};
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001050
Florian Fainellif37db852015-09-23 18:19:58 -07001051static struct device_type dsa_type = {
1052 .name = "dsa",
1053};
1054
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001055static void dsa_slave_adjust_link(struct net_device *dev)
1056{
Vivien Didelotd9450972017-10-16 11:12:15 -04001057 struct dsa_port *dp = dsa_slave_to_port(dev);
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001058 struct dsa_slave_priv *p = netdev_priv(dev);
Vivien Didelotd9450972017-10-16 11:12:15 -04001059 struct dsa_switch *ds = dp->ds;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001060 unsigned int status_changed = 0;
1061
Vivien Didelot0115dcd2017-09-26 17:15:32 -04001062 if (p->old_link != dev->phydev->link) {
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001063 status_changed = 1;
Vivien Didelot0115dcd2017-09-26 17:15:32 -04001064 p->old_link = dev->phydev->link;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001065 }
1066
Vivien Didelot0115dcd2017-09-26 17:15:32 -04001067 if (p->old_duplex != dev->phydev->duplex) {
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001068 status_changed = 1;
Vivien Didelot0115dcd2017-09-26 17:15:32 -04001069 p->old_duplex = dev->phydev->duplex;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001070 }
1071
Vivien Didelot0115dcd2017-09-26 17:15:32 -04001072 if (p->old_pause != dev->phydev->pause) {
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001073 status_changed = 1;
Vivien Didelot0115dcd2017-09-26 17:15:32 -04001074 p->old_pause = dev->phydev->pause;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001075 }
1076
Vivien Didelot9d490b42016-08-23 12:38:56 -04001077 if (ds->ops->adjust_link && status_changed)
Vivien Didelotd9450972017-10-16 11:12:15 -04001078 ds->ops->adjust_link(ds, dp->index, dev->phydev);
Florian Fainelliec9436b2014-08-27 17:04:53 -07001079
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001080 if (status_changed)
Vivien Didelot0115dcd2017-09-26 17:15:32 -04001081 phy_print_status(dev->phydev);
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001082}
1083
Florian Fainellice31b312014-08-27 17:04:54 -07001084static int dsa_slave_fixed_link_update(struct net_device *dev,
1085 struct fixed_phy_status *status)
1086{
Andrew Lunnb71be352016-03-12 00:01:37 +01001087 struct dsa_switch *ds;
Vivien Didelotd9450972017-10-16 11:12:15 -04001088 struct dsa_port *dp;
Florian Fainellice31b312014-08-27 17:04:54 -07001089
Andrew Lunnb71be352016-03-12 00:01:37 +01001090 if (dev) {
Vivien Didelotd9450972017-10-16 11:12:15 -04001091 dp = dsa_slave_to_port(dev);
1092 ds = dp->ds;
Vivien Didelot9d490b42016-08-23 12:38:56 -04001093 if (ds->ops->fixed_link_update)
Vivien Didelotd9450972017-10-16 11:12:15 -04001094 ds->ops->fixed_link_update(ds, dp->index, status);
Andrew Lunnb71be352016-03-12 00:01:37 +01001095 }
Florian Fainellice31b312014-08-27 17:04:54 -07001096
1097 return 0;
1098}
1099
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001100/* slave device setup *******************************************************/
Vivien Didelot4fa7b712017-09-20 19:31:57 -04001101static int dsa_slave_phy_connect(struct net_device *slave_dev, int addr)
Florian Fainellic305c162015-03-10 16:57:12 -07001102{
Vivien Didelotd9450972017-10-16 11:12:15 -04001103 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
Vivien Didelot4fa7b712017-09-20 19:31:57 -04001104 struct dsa_slave_priv *p = netdev_priv(slave_dev);
Vivien Didelotd9450972017-10-16 11:12:15 -04001105 struct dsa_switch *ds = dp->ds;
Florian Fainellic305c162015-03-10 16:57:12 -07001106
Vivien Didelot0115dcd2017-09-26 17:15:32 -04001107 slave_dev->phydev = mdiobus_get_phy(ds->slave_mii_bus, addr);
1108 if (!slave_dev->phydev) {
Russell Kingd25b8e72015-10-03 18:09:07 +01001109 netdev_err(slave_dev, "no phy at %d\n", addr);
Florian Fainellic305c162015-03-10 16:57:12 -07001110 return -ENODEV;
Russell Kingd25b8e72015-10-03 18:09:07 +01001111 }
Florian Fainellic305c162015-03-10 16:57:12 -07001112
1113 /* Use already configured phy mode */
Florian Fainelli211c5042015-08-08 12:58:57 -07001114 if (p->phy_interface == PHY_INTERFACE_MODE_NA)
Vivien Didelot0115dcd2017-09-26 17:15:32 -04001115 p->phy_interface = slave_dev->phydev->interface;
1116
1117 return phy_connect_direct(slave_dev, slave_dev->phydev,
1118 dsa_slave_adjust_link, p->phy_interface);
Florian Fainellic305c162015-03-10 16:57:12 -07001119}
1120
Vivien Didelot4fa7b712017-09-20 19:31:57 -04001121static int dsa_slave_phy_setup(struct net_device *slave_dev)
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001122{
Vivien Didelotd9450972017-10-16 11:12:15 -04001123 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
Vivien Didelot4fa7b712017-09-20 19:31:57 -04001124 struct dsa_slave_priv *p = netdev_priv(slave_dev);
Vivien Didelotd9450972017-10-16 11:12:15 -04001125 struct device_node *port_dn = dp->dn;
1126 struct dsa_switch *ds = dp->ds;
1127 struct device_node *phy_dn;
Florian Fainellice31b312014-08-27 17:04:54 -07001128 bool phy_is_fixed = false;
Florian Fainelli68195632014-09-19 13:07:54 -07001129 u32 phy_flags = 0;
Guenter Roeck19334922015-02-16 21:23:51 -08001130 int mode, ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001131
Guenter Roeck19334922015-02-16 21:23:51 -08001132 mode = of_get_phy_mode(port_dn);
1133 if (mode < 0)
1134 mode = PHY_INTERFACE_MODE_NA;
1135 p->phy_interface = mode;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001136
1137 phy_dn = of_parse_phandle(port_dn, "phy-handle", 0);
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001138 if (!phy_dn && of_phy_is_fixed_link(port_dn)) {
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001139 /* In the case of a fixed PHY, the DT node associated
1140 * to the fixed PHY is the Port DT node
1141 */
1142 ret = of_phy_register_fixed_link(port_dn);
1143 if (ret) {
Russell Kingd25b8e72015-10-03 18:09:07 +01001144 netdev_err(slave_dev, "failed to register fixed PHY: %d\n", ret);
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001145 return ret;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001146 }
Florian Fainellice31b312014-08-27 17:04:54 -07001147 phy_is_fixed = true;
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001148 phy_dn = of_node_get(port_dn);
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001149 }
1150
Vivien Didelot9d490b42016-08-23 12:38:56 -04001151 if (ds->ops->get_phy_flags)
Vivien Didelotd9450972017-10-16 11:12:15 -04001152 phy_flags = ds->ops->get_phy_flags(ds, dp->index);
Florian Fainelli68195632014-09-19 13:07:54 -07001153
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001154 if (phy_dn) {
Florian Fainelli399ba772017-10-25 17:32:05 -07001155 slave_dev->phydev = of_phy_connect(slave_dev, phy_dn,
1156 dsa_slave_adjust_link,
1157 phy_flags,
1158 p->phy_interface);
Johan Hovold0d8f3c62016-11-28 19:24:54 +01001159 of_node_put(phy_dn);
Florian Fainellicd28a1a2015-03-10 16:57:13 -07001160 }
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001161
Vivien Didelot0115dcd2017-09-26 17:15:32 -04001162 if (slave_dev->phydev && phy_is_fixed)
1163 fixed_phy_set_link_update(slave_dev->phydev,
1164 dsa_slave_fixed_link_update);
Florian Fainellice31b312014-08-27 17:04:54 -07001165
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001166 /* We could not connect to a designated PHY, so use the switch internal
1167 * MDIO bus instead
1168 */
Vivien Didelot0115dcd2017-09-26 17:15:32 -04001169 if (!slave_dev->phydev) {
Vivien Didelotd9450972017-10-16 11:12:15 -04001170 ret = dsa_slave_phy_connect(slave_dev, dp->index);
Russell Kingd25b8e72015-10-03 18:09:07 +01001171 if (ret) {
Vivien Didelotafdcf152017-01-27 15:29:39 -05001172 netdev_err(slave_dev, "failed to connect to port %d: %d\n",
Vivien Didelotd9450972017-10-16 11:12:15 -04001173 dp->index, ret);
Johan Hovold881eada2016-11-28 19:25:09 +01001174 if (phy_is_fixed)
1175 of_phy_deregister_fixed_link(port_dn);
Florian Fainellic305c162015-03-10 16:57:12 -07001176 return ret;
Russell Kingd25b8e72015-10-03 18:09:07 +01001177 }
Andrew Lunnb31f65f2014-11-05 19:47:28 +01001178 }
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001179
Vivien Didelot0115dcd2017-09-26 17:15:32 -04001180 phy_attached_info(slave_dev->phydev);
Andrew Lunn22209432016-01-06 20:11:13 +01001181
Florian Fainelli9697f1c2014-12-11 12:49:16 -08001182 return 0;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001183}
1184
Andrew Lunn448b4482015-05-06 01:09:56 +02001185static struct lock_class_key dsa_slave_netdev_xmit_lock_key;
1186static void dsa_slave_set_lockdep_class_one(struct net_device *dev,
1187 struct netdev_queue *txq,
1188 void *_unused)
1189{
1190 lockdep_set_class(&txq->_xmit_lock,
1191 &dsa_slave_netdev_xmit_lock_key);
1192}
1193
Florian Fainelli24462542014-09-18 17:31:22 -07001194int dsa_slave_suspend(struct net_device *slave_dev)
1195{
1196 struct dsa_slave_priv *p = netdev_priv(slave_dev);
1197
Florian Fainellif154be22017-01-25 09:10:41 -08001198 netif_device_detach(slave_dev);
1199
Vivien Didelot0115dcd2017-09-26 17:15:32 -04001200 if (slave_dev->phydev) {
1201 phy_stop(slave_dev->phydev);
Florian Fainelli24462542014-09-18 17:31:22 -07001202 p->old_pause = -1;
1203 p->old_link = -1;
1204 p->old_duplex = -1;
Vivien Didelot0115dcd2017-09-26 17:15:32 -04001205 phy_suspend(slave_dev->phydev);
Florian Fainelli24462542014-09-18 17:31:22 -07001206 }
1207
1208 return 0;
1209}
1210
1211int dsa_slave_resume(struct net_device *slave_dev)
1212{
Florian Fainelli24462542014-09-18 17:31:22 -07001213 netif_device_attach(slave_dev);
1214
Vivien Didelot0115dcd2017-09-26 17:15:32 -04001215 if (slave_dev->phydev) {
1216 phy_resume(slave_dev->phydev);
1217 phy_start(slave_dev->phydev);
Florian Fainelli24462542014-09-18 17:31:22 -07001218 }
1219
1220 return 0;
1221}
1222
Vivien Didelot6158eaa2017-10-16 11:12:14 -04001223static void dsa_slave_notify(struct net_device *dev, unsigned long val)
1224{
Vivien Didelotd0006b02017-10-16 11:12:16 -04001225 struct net_device *master = dsa_slave_to_master(dev);
Vivien Didelotd9450972017-10-16 11:12:15 -04001226 struct dsa_port *dp = dsa_slave_to_port(dev);
Vivien Didelot6158eaa2017-10-16 11:12:14 -04001227 struct dsa_notifier_register_info rinfo = {
1228 .switch_number = dp->ds->index,
1229 .port_number = dp->index,
1230 .master = master,
1231 .info.dev = dev,
1232 };
1233
1234 call_dsa_notifiers(val, dev, &rinfo.info);
1235}
1236
Vivien Didelot951259aa2017-10-27 15:55:19 -04001237int dsa_slave_create(struct dsa_port *port)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001238{
Vivien Didelot24a93322017-11-06 16:11:43 -05001239 const struct dsa_port *cpu_dp = port->cpu_dp;
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -04001240 struct net_device *master = cpu_dp->master;
Vivien Didelot4cfbf092017-08-05 16:20:19 -04001241 struct dsa_switch *ds = port->ds;
Vivien Didelot951259aa2017-10-27 15:55:19 -04001242 const char *name = port->name;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001243 struct net_device *slave_dev;
1244 struct dsa_slave_priv *p;
1245 int ret;
1246
Florian Fainelli55199df2017-09-03 20:27:00 -07001247 if (!ds->num_tx_queues)
1248 ds->num_tx_queues = 1;
1249
1250 slave_dev = alloc_netdev_mqs(sizeof(struct dsa_slave_priv), name,
1251 NET_NAME_UNKNOWN, ether_setup,
1252 ds->num_tx_queues, 1);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001253 if (slave_dev == NULL)
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001254 return -ENOMEM;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001255
Florian Fainellif50f2122017-01-30 12:41:40 -08001256 slave_dev->features = master->vlan_features | NETIF_F_HW_TC;
1257 slave_dev->hw_features |= NETIF_F_HW_TC;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001258 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
Bjørn Mork2fcc8002013-08-30 18:08:46 +02001259 eth_hw_addr_inherit(slave_dev, master);
Phil Sutter0a5f1072015-08-18 10:30:41 +02001260 slave_dev->priv_flags |= IFF_NO_QUEUE;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001261 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
Jiri Pirko9d47c0a2015-05-10 09:47:47 -07001262 slave_dev->switchdev_ops = &dsa_slave_switchdev_ops;
Jarod Wilson8b1efc02016-10-20 23:25:27 -04001263 slave_dev->min_mtu = 0;
1264 slave_dev->max_mtu = ETH_MAX_MTU;
Florian Fainellif37db852015-09-23 18:19:58 -07001265 SET_NETDEV_DEVTYPE(slave_dev, &dsa_type);
Stephen Hemmingerd442ad42009-01-06 16:45:26 -08001266
Andrew Lunn448b4482015-05-06 01:09:56 +02001267 netdev_for_each_tx_queue(slave_dev, dsa_slave_set_lockdep_class_one,
1268 NULL);
1269
Vivien Didelot4cfbf092017-08-05 16:20:19 -04001270 SET_NETDEV_DEV(slave_dev, port->ds->dev);
1271 slave_dev->dev.of_node = port->dn;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001272 slave_dev->vlan_features = master->vlan_features;
1273
1274 p = netdev_priv(slave_dev);
Florian Fainelli5f6b4e142017-08-03 21:33:27 -07001275 p->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
1276 if (!p->stats64) {
1277 free_netdev(slave_dev);
1278 return -ENOMEM;
1279 }
Vivien Didelot4cfbf092017-08-05 16:20:19 -04001280 p->dp = port;
Florian Fainellif50f2122017-01-30 12:41:40 -08001281 INIT_LIST_HEAD(&p->mall_tc_list);
Vivien Didelot15240242017-09-29 17:19:18 -04001282 p->xmit = cpu_dp->tag_ops->xmit;
Alexander Duyck50753142014-09-15 13:00:19 -04001283
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -07001284 p->old_pause = -1;
1285 p->old_link = -1;
1286 p->old_duplex = -1;
1287
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -04001288 port->slave = slave_dev;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001289
1290 netif_carrier_off(slave_dev);
1291
Vivien Didelot4fa7b712017-09-20 19:31:57 -04001292 ret = dsa_slave_phy_setup(slave_dev);
Andrew Lunn0071f562016-01-06 20:11:20 +01001293 if (ret) {
1294 netdev_err(master, "error %d setting up slave phy\n", ret);
Florian Fainellie8044412017-09-25 15:55:53 -07001295 goto out_free;
1296 }
1297
Vivien Didelot6158eaa2017-10-16 11:12:14 -04001298 dsa_slave_notify(slave_dev, DSA_PORT_REGISTER);
Florian Fainelli60724d42017-10-11 10:57:48 -07001299
Florian Fainellie8044412017-09-25 15:55:53 -07001300 ret = register_netdev(slave_dev);
1301 if (ret) {
1302 netdev_err(master, "error %d registering interface %s\n",
1303 ret, slave_dev->name);
1304 goto out_phy;
Andrew Lunn0071f562016-01-06 20:11:20 +01001305 }
1306
Guenter Roeckd87d6f42015-02-24 13:15:32 -08001307 return 0;
Florian Fainellie8044412017-09-25 15:55:53 -07001308
1309out_phy:
David S. Miller53954cf2017-10-05 17:57:03 -07001310 phy_disconnect(slave_dev->phydev);
Vivien Didelotd9450972017-10-16 11:12:15 -04001311 if (of_phy_is_fixed_link(port->dn))
1312 of_phy_deregister_fixed_link(port->dn);
Florian Fainellie8044412017-09-25 15:55:53 -07001313out_free:
1314 free_percpu(p->stats64);
1315 free_netdev(slave_dev);
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -04001316 port->slave = NULL;
Florian Fainellie8044412017-09-25 15:55:53 -07001317 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001318}
Florian Fainellib73adef2015-02-24 13:15:33 -08001319
Neil Armstrongcda5c152015-12-07 13:57:35 +01001320void dsa_slave_destroy(struct net_device *slave_dev)
1321{
Vivien Didelotd9450972017-10-16 11:12:15 -04001322 struct dsa_port *dp = dsa_slave_to_port(slave_dev);
Neil Armstrongcda5c152015-12-07 13:57:35 +01001323 struct dsa_slave_priv *p = netdev_priv(slave_dev);
Vivien Didelotd9450972017-10-16 11:12:15 -04001324 struct device_node *port_dn = dp->dn;
Neil Armstrongcda5c152015-12-07 13:57:35 +01001325
1326 netif_carrier_off(slave_dev);
Vivien Didelot0115dcd2017-09-26 17:15:32 -04001327 if (slave_dev->phydev) {
1328 phy_disconnect(slave_dev->phydev);
Johan Hovold881eada2016-11-28 19:25:09 +01001329
1330 if (of_phy_is_fixed_link(port_dn))
1331 of_phy_deregister_fixed_link(port_dn);
1332 }
Vivien Didelot6158eaa2017-10-16 11:12:14 -04001333 dsa_slave_notify(slave_dev, DSA_PORT_UNREGISTER);
Neil Armstrongcda5c152015-12-07 13:57:35 +01001334 unregister_netdev(slave_dev);
Florian Fainelli5f6b4e142017-08-03 21:33:27 -07001335 free_percpu(p->stats64);
Neil Armstrongcda5c152015-12-07 13:57:35 +01001336 free_netdev(slave_dev);
1337}
1338
Florian Fainellib73adef2015-02-24 13:15:33 -08001339static bool dsa_slave_dev_check(struct net_device *dev)
1340{
1341 return dev->netdev_ops == &dsa_slave_netdev_ops;
1342}
1343
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001344static int dsa_slave_changeupper(struct net_device *dev,
1345 struct netdev_notifier_changeupper_info *info)
Florian Fainellib73adef2015-02-24 13:15:33 -08001346{
Vivien Didelotd9450972017-10-16 11:12:15 -04001347 struct dsa_port *dp = dsa_slave_to_port(dev);
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001348 int err = NOTIFY_DONE;
Florian Fainellib73adef2015-02-24 13:15:33 -08001349
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001350 if (netif_is_bridge_master(info->upper_dev)) {
1351 if (info->linking) {
Vivien Didelot17d78022017-05-19 17:00:38 -04001352 err = dsa_port_bridge_join(dp, info->upper_dev);
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001353 err = notifier_from_errno(err);
1354 } else {
Vivien Didelot17d78022017-05-19 17:00:38 -04001355 dsa_port_bridge_leave(dp, info->upper_dev);
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001356 err = NOTIFY_OK;
Vivien Didelot6debb682016-03-13 16:21:34 -04001357 }
Vivien Didelot6debb682016-03-13 16:21:34 -04001358 }
1359
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001360 return err;
Florian Fainellib73adef2015-02-24 13:15:33 -08001361}
1362
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001363static int dsa_slave_netdevice_event(struct notifier_block *nb,
1364 unsigned long event, void *ptr)
Florian Fainellib73adef2015-02-24 13:15:33 -08001365{
Vivien Didelot6debb682016-03-13 16:21:34 -04001366 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Florian Fainellib73adef2015-02-24 13:15:33 -08001367
Florian Fainelli53bade82017-09-19 18:00:37 -07001368 if (!dsa_slave_dev_check(dev))
Vivien Didelot8e92ab32017-02-03 13:20:17 -05001369 return NOTIFY_DONE;
1370
1371 if (event == NETDEV_CHANGEUPPER)
1372 return dsa_slave_changeupper(dev, ptr);
Florian Fainellib73adef2015-02-24 13:15:33 -08001373
Florian Fainellib73adef2015-02-24 13:15:33 -08001374 return NOTIFY_DONE;
1375}
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001376
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03001377struct dsa_switchdev_event_work {
1378 struct work_struct work;
1379 struct switchdev_notifier_fdb_info fdb_info;
1380 struct net_device *dev;
1381 unsigned long event;
1382};
1383
1384static void dsa_slave_switchdev_event_work(struct work_struct *work)
1385{
1386 struct dsa_switchdev_event_work *switchdev_work =
1387 container_of(work, struct dsa_switchdev_event_work, work);
1388 struct net_device *dev = switchdev_work->dev;
1389 struct switchdev_notifier_fdb_info *fdb_info;
Vivien Didelotd9450972017-10-16 11:12:15 -04001390 struct dsa_port *dp = dsa_slave_to_port(dev);
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03001391 int err;
1392
1393 rtnl_lock();
1394 switch (switchdev_work->event) {
1395 case SWITCHDEV_FDB_ADD_TO_DEVICE:
1396 fdb_info = &switchdev_work->fdb_info;
Vivien Didelotd9450972017-10-16 11:12:15 -04001397 err = dsa_port_fdb_add(dp, fdb_info->addr, fdb_info->vid);
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03001398 if (err) {
1399 netdev_dbg(dev, "fdb add failed err=%d\n", err);
1400 break;
1401 }
1402 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
1403 &fdb_info->info);
1404 break;
1405
1406 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1407 fdb_info = &switchdev_work->fdb_info;
Vivien Didelotd9450972017-10-16 11:12:15 -04001408 err = dsa_port_fdb_del(dp, fdb_info->addr, fdb_info->vid);
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03001409 if (err) {
1410 netdev_dbg(dev, "fdb del failed err=%d\n", err);
1411 dev_close(dev);
1412 }
1413 break;
1414 }
1415 rtnl_unlock();
1416
1417 kfree(switchdev_work->fdb_info.addr);
1418 kfree(switchdev_work);
1419 dev_put(dev);
1420}
1421
1422static int
1423dsa_slave_switchdev_fdb_work_init(struct dsa_switchdev_event_work *
1424 switchdev_work,
1425 const struct switchdev_notifier_fdb_info *
1426 fdb_info)
1427{
1428 memcpy(&switchdev_work->fdb_info, fdb_info,
1429 sizeof(switchdev_work->fdb_info));
1430 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
1431 if (!switchdev_work->fdb_info.addr)
1432 return -ENOMEM;
1433 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
1434 fdb_info->addr);
1435 return 0;
1436}
1437
1438/* Called under rcu_read_lock() */
1439static int dsa_slave_switchdev_event(struct notifier_block *unused,
1440 unsigned long event, void *ptr)
1441{
1442 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1443 struct dsa_switchdev_event_work *switchdev_work;
1444
1445 if (!dsa_slave_dev_check(dev))
1446 return NOTIFY_DONE;
1447
1448 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
1449 if (!switchdev_work)
1450 return NOTIFY_BAD;
1451
1452 INIT_WORK(&switchdev_work->work,
1453 dsa_slave_switchdev_event_work);
1454 switchdev_work->dev = dev;
1455 switchdev_work->event = event;
1456
1457 switch (event) {
1458 case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
1459 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1460 if (dsa_slave_switchdev_fdb_work_init(switchdev_work,
1461 ptr))
1462 goto err_fdb_work_init;
1463 dev_hold(dev);
1464 break;
1465 default:
1466 kfree(switchdev_work);
1467 return NOTIFY_DONE;
1468 }
1469
1470 dsa_schedule_work(&switchdev_work->work);
1471 return NOTIFY_OK;
1472
1473err_fdb_work_init:
1474 kfree(switchdev_work);
1475 return NOTIFY_BAD;
1476}
1477
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001478static struct notifier_block dsa_slave_nb __read_mostly = {
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03001479 .notifier_call = dsa_slave_netdevice_event,
1480};
1481
1482static struct notifier_block dsa_slave_switchdev_notifier = {
1483 .notifier_call = dsa_slave_switchdev_event,
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001484};
1485
1486int dsa_slave_register_notifier(void)
1487{
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03001488 int err;
1489
1490 err = register_netdevice_notifier(&dsa_slave_nb);
1491 if (err)
1492 return err;
1493
1494 err = register_switchdev_notifier(&dsa_slave_switchdev_notifier);
1495 if (err)
1496 goto err_switchdev_nb;
1497
1498 return 0;
1499
1500err_switchdev_nb:
1501 unregister_netdevice_notifier(&dsa_slave_nb);
1502 return err;
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001503}
1504
1505void dsa_slave_unregister_notifier(void)
1506{
1507 int err;
1508
Arkadi Sharshevskyc9eb3e02017-08-06 16:15:42 +03001509 err = unregister_switchdev_notifier(&dsa_slave_switchdev_notifier);
1510 if (err)
1511 pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
1512
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001513 err = unregister_netdevice_notifier(&dsa_slave_nb);
1514 if (err)
1515 pr_err("DSA: failed to unregister slave notifier (%d)\n", err);
1516}