blob: cda787ebad1524613c2fbb9f9d9b5cbb7de40569 [file] [log] [blame]
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001/*
2 * net/dsa/dsa.c - Hardware switch handling
Lennert Buytenheke84665c2009-03-20 09:52:09 +00003 * Copyright (c) 2008-2009 Marvell Semiconductor
Florian Fainelli5e95329b2013-03-22 10:50:50 +00004 * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
Guenter Roeck51579c32014-10-29 10:44:58 -070012#include <linux/device.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000013#include <linux/list.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000014#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Paul Gortmaker3a9a2312011-05-27 09:12:25 -040016#include <linux/module.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000017#include <net/dsa.h>
Florian Fainelli5e95329b2013-03-22 10:50:50 +000018#include <linux/of.h>
19#include <linux/of_mdio.h>
20#include <linux/of_platform.h>
Florian Fainelli769a0202015-03-09 14:31:21 -070021#include <linux/of_net.h>
Andrew Lunncc30c162015-11-20 03:56:23 +010022#include <linux/of_gpio.h>
Guenter Roeck51579c32014-10-29 10:44:58 -070023#include <linux/sysfs.h>
Neil Armstrongcbc5d902015-10-06 15:40:32 +010024#include <linux/phy_fixed.h>
Arnd Bergmann85beabf2015-11-24 12:34:49 +010025#include <linux/gpio/consumer.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000026#include "dsa_priv.h"
27
Andrew Lunn39a7f2a2016-06-04 21:17:03 +020028static struct sk_buff *dsa_slave_notag_xmit(struct sk_buff *skb,
29 struct net_device *dev)
30{
31 /* Just return the original SKB */
32 return skb;
33}
34
35static const struct dsa_device_ops none_ops = {
36 .xmit = dsa_slave_notag_xmit,
37 .rcv = NULL,
38};
39
40const struct dsa_device_ops *dsa_device_ops[DSA_TAG_LAST] = {
41#ifdef CONFIG_NET_DSA_TAG_DSA
42 [DSA_TAG_PROTO_DSA] = &dsa_netdev_ops,
43#endif
44#ifdef CONFIG_NET_DSA_TAG_EDSA
45 [DSA_TAG_PROTO_EDSA] = &edsa_netdev_ops,
46#endif
47#ifdef CONFIG_NET_DSA_TAG_TRAILER
48 [DSA_TAG_PROTO_TRAILER] = &trailer_netdev_ops,
49#endif
50#ifdef CONFIG_NET_DSA_TAG_BRCM
51 [DSA_TAG_PROTO_BRCM] = &brcm_netdev_ops,
52#endif
John Crispincafdc452016-09-15 16:26:40 +020053#ifdef CONFIG_NET_DSA_TAG_QCA
54 [DSA_TAG_PROTO_QCA] = &qca_netdev_ops,
55#endif
Andrew Lunn39a7f2a2016-06-04 21:17:03 +020056 [DSA_TAG_PROTO_NONE] = &none_ops,
57};
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000058
59/* switch driver registration ***********************************************/
60static DEFINE_MUTEX(dsa_switch_drivers_mutex);
61static LIST_HEAD(dsa_switch_drivers);
62
Vivien Didelot9d490b42016-08-23 12:38:56 -040063void register_switch_driver(struct dsa_switch_ops *ops)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000064{
65 mutex_lock(&dsa_switch_drivers_mutex);
Vivien Didelot9d490b42016-08-23 12:38:56 -040066 list_add_tail(&ops->list, &dsa_switch_drivers);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000067 mutex_unlock(&dsa_switch_drivers_mutex);
68}
Ben Hutchingsad293b82011-11-25 14:34:07 +000069EXPORT_SYMBOL_GPL(register_switch_driver);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000070
Vivien Didelot9d490b42016-08-23 12:38:56 -040071void unregister_switch_driver(struct dsa_switch_ops *ops)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000072{
73 mutex_lock(&dsa_switch_drivers_mutex);
Vivien Didelot9d490b42016-08-23 12:38:56 -040074 list_del_init(&ops->list);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000075 mutex_unlock(&dsa_switch_drivers_mutex);
76}
Ben Hutchingsad293b82011-11-25 14:34:07 +000077EXPORT_SYMBOL_GPL(unregister_switch_driver);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000078
Vivien Didelot9d490b42016-08-23 12:38:56 -040079static struct dsa_switch_ops *
Andrew Lunnbbb8d792016-04-13 02:40:39 +020080dsa_switch_probe(struct device *parent, struct device *host_dev, int sw_addr,
Vivien Didelot0209d142016-04-17 13:23:55 -040081 const char **_name, void **priv)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000082{
Vivien Didelot9d490b42016-08-23 12:38:56 -040083 struct dsa_switch_ops *ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000084 struct list_head *list;
Vivien Didelot0209d142016-04-17 13:23:55 -040085 const char *name;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000086
87 ret = NULL;
88 name = NULL;
89
90 mutex_lock(&dsa_switch_drivers_mutex);
91 list_for_each(list, &dsa_switch_drivers) {
Vivien Didelot9d490b42016-08-23 12:38:56 -040092 struct dsa_switch_ops *ops;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000093
Vivien Didelot9d490b42016-08-23 12:38:56 -040094 ops = list_entry(list, struct dsa_switch_ops, list);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000095
Vivien Didelot9d490b42016-08-23 12:38:56 -040096 name = ops->probe(parent, host_dev, sw_addr, priv);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000097 if (name != NULL) {
Vivien Didelot9d490b42016-08-23 12:38:56 -040098 ret = ops;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000099 break;
100 }
101 }
102 mutex_unlock(&dsa_switch_drivers_mutex);
103
104 *_name = name;
105
106 return ret;
107}
108
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000109/* basic switch operations **************************************************/
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200110int dsa_cpu_dsa_setup(struct dsa_switch *ds, struct device *dev,
111 struct device_node *port_dn, int port)
112{
113 struct phy_device *phydev;
114 int ret, mode;
115
116 if (of_phy_is_fixed_link(port_dn)) {
117 ret = of_phy_register_fixed_link(port_dn);
118 if (ret) {
119 dev_err(dev, "failed to register fixed PHY\n");
120 return ret;
121 }
122 phydev = of_phy_find_device(port_dn);
123
124 mode = of_get_phy_mode(port_dn);
125 if (mode < 0)
126 mode = PHY_INTERFACE_MODE_NA;
127 phydev->interface = mode;
128
129 genphy_config_init(phydev);
130 genphy_read_status(phydev);
Vivien Didelot9d490b42016-08-23 12:38:56 -0400131 if (ds->ops->adjust_link)
132 ds->ops->adjust_link(ds, port, phydev);
Johan Hovoldfd05d7b2016-11-24 19:21:27 +0100133
134 put_device(&phydev->mdio.dev);
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200135 }
136
137 return 0;
138}
139
140static int dsa_cpu_dsa_setups(struct dsa_switch *ds, struct device *dev)
Andrew Lunn39b0c702015-08-31 15:56:49 +0200141{
Andrew Lunn39b0c702015-08-31 15:56:49 +0200142 struct device_node *port_dn;
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200143 int ret, port;
Andrew Lunn39b0c702015-08-31 15:56:49 +0200144
145 for (port = 0; port < DSA_MAX_PORTS; port++) {
146 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
147 continue;
148
Andrew Lunn189b0d92016-06-04 21:16:58 +0200149 port_dn = ds->ports[port].dn;
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200150 ret = dsa_cpu_dsa_setup(ds, dev, port_dn, port);
151 if (ret)
152 return ret;
Andrew Lunn39b0c702015-08-31 15:56:49 +0200153 }
154 return 0;
155}
156
Andrew Lunn39a7f2a2016-06-04 21:17:03 +0200157const struct dsa_device_ops *dsa_resolve_tag_protocol(int tag_protocol)
158{
159 const struct dsa_device_ops *ops;
160
161 if (tag_protocol >= DSA_TAG_LAST)
162 return ERR_PTR(-EINVAL);
163 ops = dsa_device_ops[tag_protocol];
164
165 if (!ops)
166 return ERR_PTR(-ENOPROTOOPT);
167
168 return ops;
169}
170
Florian Fainelli0c73c522016-06-07 16:32:42 -0700171int dsa_cpu_port_ethtool_setup(struct dsa_switch *ds)
172{
173 struct net_device *master;
174 struct ethtool_ops *cpu_ops;
175
176 master = ds->dst->master_netdev;
177 if (ds->master_netdev)
178 master = ds->master_netdev;
179
180 cpu_ops = devm_kzalloc(ds->dev, sizeof(*cpu_ops), GFP_KERNEL);
181 if (!cpu_ops)
182 return -ENOMEM;
183
184 memcpy(&ds->dst->master_ethtool_ops, master->ethtool_ops,
185 sizeof(struct ethtool_ops));
186 ds->dst->master_orig_ethtool_ops = master->ethtool_ops;
187 memcpy(cpu_ops, &ds->dst->master_ethtool_ops,
188 sizeof(struct ethtool_ops));
189 dsa_cpu_port_ethtool_init(cpu_ops);
190 master->ethtool_ops = cpu_ops;
191
192 return 0;
193}
194
195void dsa_cpu_port_ethtool_restore(struct dsa_switch *ds)
196{
197 struct net_device *master;
198
199 master = ds->dst->master_netdev;
200 if (ds->master_netdev)
201 master = ds->master_netdev;
202
203 master->ethtool_ops = ds->dst->master_orig_ethtool_ops;
204}
205
Florian Fainellidf197192015-03-05 12:35:06 -0800206static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000207{
Vivien Didelot9d490b42016-08-23 12:38:56 -0400208 struct dsa_switch_ops *ops = ds->ops;
Florian Fainellidf197192015-03-05 12:35:06 -0800209 struct dsa_switch_tree *dst = ds->dst;
Andrew Lunnff049552016-05-10 23:27:24 +0200210 struct dsa_chip_data *cd = ds->cd;
Florian Fainellif9bf5a22013-01-21 09:58:51 +0000211 bool valid_name_found = false;
Florian Fainellidf197192015-03-05 12:35:06 -0800212 int index = ds->index;
213 int i, ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000214
215 /*
216 * Validate supplied switch configuration.
217 */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000218 for (i = 0; i < DSA_MAX_PORTS; i++) {
219 char *name;
220
Andrew Lunnff049552016-05-10 23:27:24 +0200221 name = cd->port_names[i];
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000222 if (name == NULL)
223 continue;
224
225 if (!strcmp(name, "cpu")) {
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000226 if (dst->cpu_switch != -1) {
Joe Perchesa2ae6002014-11-09 16:32:46 -0800227 netdev_err(dst->master_netdev,
228 "multiple cpu ports?!\n");
Vivien Didelota896eee2017-01-03 14:31:49 -0500229 return -EINVAL;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000230 }
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000231 dst->cpu_switch = index;
232 dst->cpu_port = i;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200233 ds->cpu_port_mask |= 1 << i;
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000234 } else if (!strcmp(name, "dsa")) {
235 ds->dsa_port_mask |= 1 << i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000236 } else {
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200237 ds->enabled_port_mask |= 1 << i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000238 }
Florian Fainellif9bf5a22013-01-21 09:58:51 +0000239 valid_name_found = true;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000240 }
241
Vivien Didelota896eee2017-01-03 14:31:49 -0500242 if (!valid_name_found && i == DSA_MAX_PORTS)
243 return -EINVAL;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000244
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700245 /* Make the built-in MII bus mask match the number of ports,
246 * switch drivers can override this later
247 */
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200248 ds->phys_mii_mask = ds->enabled_port_mask;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700249
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000250 /*
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000251 * If the CPU connects to this switch, set the switch tree
252 * tagging protocol to the preferred tagging format of this
253 * switch.
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000254 */
Alexander Duyck50753142014-09-15 13:00:19 -0400255 if (dst->cpu_switch == index) {
Andrew Lunn7b314362016-08-22 16:01:01 +0200256 enum dsa_tag_protocol tag_protocol;
257
Vivien Didelot9d490b42016-08-23 12:38:56 -0400258 tag_protocol = ops->get_tag_protocol(ds);
Andrew Lunn7b314362016-08-22 16:01:01 +0200259 dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol);
Vivien Didelota896eee2017-01-03 14:31:49 -0500260 if (IS_ERR(dst->tag_ops))
261 return PTR_ERR(dst->tag_ops);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000262
Andrew Lunn39a7f2a2016-06-04 21:17:03 +0200263 dst->rcv = dst->tag_ops->rcv;
Alexander Duyck50753142014-09-15 13:00:19 -0400264 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000265
Andrew Lunn66472fc2016-06-04 21:17:00 +0200266 memcpy(ds->rtable, cd->rtable, sizeof(ds->rtable));
267
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000268 /*
269 * Do basic register setup.
270 */
Vivien Didelot9d490b42016-08-23 12:38:56 -0400271 ret = ops->setup(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000272 if (ret < 0)
Vivien Didelota896eee2017-01-03 14:31:49 -0500273 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000274
John Crispin092183d2016-09-19 15:28:01 +0200275 if (ops->set_addr) {
276 ret = ops->set_addr(ds, dst->master_netdev->dev_addr);
277 if (ret < 0)
Vivien Didelota896eee2017-01-03 14:31:49 -0500278 return ret;
John Crispin092183d2016-09-19 15:28:01 +0200279 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000280
Vivien Didelot9d490b42016-08-23 12:38:56 -0400281 if (!ds->slave_mii_bus && ops->phy_read) {
Andrew Lunne755e492016-06-04 21:17:04 +0200282 ds->slave_mii_bus = devm_mdiobus_alloc(parent);
Vivien Didelota896eee2017-01-03 14:31:49 -0500283 if (!ds->slave_mii_bus)
284 return -ENOMEM;
Andrew Lunne755e492016-06-04 21:17:04 +0200285 dsa_slave_mii_bus_init(ds);
286
287 ret = mdiobus_register(ds->slave_mii_bus);
288 if (ret < 0)
Vivien Didelota896eee2017-01-03 14:31:49 -0500289 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000290 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000291
292 /*
293 * Create network devices for physical switch ports.
294 */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000295 for (i = 0; i < DSA_MAX_PORTS; i++) {
Andrew Lunn189b0d92016-06-04 21:16:58 +0200296 ds->ports[i].dn = cd->port_dn[i];
297
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200298 if (!(ds->enabled_port_mask & (1 << i)))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000299 continue;
300
Andrew Lunnff049552016-05-10 23:27:24 +0200301 ret = dsa_slave_create(ds, parent, i, cd->port_names[i]);
Vivien Didelota896eee2017-01-03 14:31:49 -0500302 if (ret < 0)
Russell Kingd25b8e72015-10-03 18:09:07 +0100303 netdev_err(dst->master_netdev, "[%d]: can't create dsa slave device for port %d(%s): %d\n",
Andrew Lunnff049552016-05-10 23:27:24 +0200304 index, i, cd->port_names[i], ret);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000305 }
306
Andrew Lunn39b0c702015-08-31 15:56:49 +0200307 /* Perform configuration of the CPU and DSA ports */
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200308 ret = dsa_cpu_dsa_setups(ds, parent);
Vivien Didelota896eee2017-01-03 14:31:49 -0500309 if (ret < 0)
Andrew Lunn39b0c702015-08-31 15:56:49 +0200310 netdev_err(dst->master_netdev, "[%d] : can't configure CPU and DSA ports\n",
311 index);
Andrew Lunn39b0c702015-08-31 15:56:49 +0200312
Florian Fainelli0c73c522016-06-07 16:32:42 -0700313 ret = dsa_cpu_port_ethtool_setup(ds);
314 if (ret)
315 return ret;
316
Vivien Didelot111427f2017-01-06 16:42:00 -0500317 dsa_hwmon_register(ds);
Guenter Roeck51579c32014-10-29 10:44:58 -0700318
Vivien Didelota896eee2017-01-03 14:31:49 -0500319 return 0;
Florian Fainellidf197192015-03-05 12:35:06 -0800320}
321
322static struct dsa_switch *
323dsa_switch_setup(struct dsa_switch_tree *dst, int index,
324 struct device *parent, struct device *host_dev)
325{
Andrew Lunnff049552016-05-10 23:27:24 +0200326 struct dsa_chip_data *cd = dst->pd->chip + index;
Vivien Didelot9d490b42016-08-23 12:38:56 -0400327 struct dsa_switch_ops *ops;
Florian Fainellidf197192015-03-05 12:35:06 -0800328 struct dsa_switch *ds;
329 int ret;
Vivien Didelot0209d142016-04-17 13:23:55 -0400330 const char *name;
Andrew Lunn7543a6d2016-04-13 02:40:40 +0200331 void *priv;
Florian Fainellidf197192015-03-05 12:35:06 -0800332
333 /*
334 * Probe for switch model.
335 */
Vivien Didelot9d490b42016-08-23 12:38:56 -0400336 ops = dsa_switch_probe(parent, host_dev, cd->sw_addr, &name, &priv);
337 if (!ops) {
Florian Fainellidf197192015-03-05 12:35:06 -0800338 netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n",
339 index);
340 return ERR_PTR(-EINVAL);
341 }
342 netdev_info(dst->master_netdev, "[%d]: detected a %s switch\n",
343 index, name);
344
345
346 /*
347 * Allocate and initialise switch state.
348 */
Andrew Lunn5feebd02016-04-13 02:40:41 +0200349 ds = devm_kzalloc(parent, sizeof(*ds), GFP_KERNEL);
Florian Fainellidf197192015-03-05 12:35:06 -0800350 if (ds == NULL)
Florian Fainelli24595342015-05-29 10:29:46 -0700351 return ERR_PTR(-ENOMEM);
Florian Fainellidf197192015-03-05 12:35:06 -0800352
353 ds->dst = dst;
354 ds->index = index;
Andrew Lunnff049552016-05-10 23:27:24 +0200355 ds->cd = cd;
Vivien Didelot9d490b42016-08-23 12:38:56 -0400356 ds->ops = ops;
Andrew Lunn7543a6d2016-04-13 02:40:40 +0200357 ds->priv = priv;
Andrew Lunnc33063d2016-05-10 23:27:23 +0200358 ds->dev = parent;
Florian Fainellidf197192015-03-05 12:35:06 -0800359
360 ret = dsa_switch_setup_one(ds, parent);
361 if (ret)
Florian Fainelli24595342015-05-29 10:29:46 -0700362 return ERR_PTR(ret);
Florian Fainellidf197192015-03-05 12:35:06 -0800363
364 return ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000365}
366
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200367void dsa_cpu_dsa_destroy(struct device_node *port_dn)
368{
Johan Hovold3f650472016-11-28 19:24:55 +0100369 if (of_phy_is_fixed_link(port_dn))
370 of_phy_deregister_fixed_link(port_dn);
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200371}
372
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000373static void dsa_switch_destroy(struct dsa_switch *ds)
374{
Neil Armstrongcbc5d902015-10-06 15:40:32 +0100375 int port;
376
Vivien Didelot111427f2017-01-06 16:42:00 -0500377 dsa_hwmon_unregister(ds);
Neil Armstrongcbc5d902015-10-06 15:40:32 +0100378
Andrew Lunn3a445142016-03-12 00:01:38 +0100379 /* Destroy network devices for physical switch ports. */
380 for (port = 0; port < DSA_MAX_PORTS; port++) {
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200381 if (!(ds->enabled_port_mask & (1 << port)))
Andrew Lunn3a445142016-03-12 00:01:38 +0100382 continue;
383
Andrew Lunnc8b09802016-06-04 21:16:57 +0200384 if (!ds->ports[port].netdev)
Andrew Lunn3a445142016-03-12 00:01:38 +0100385 continue;
386
Andrew Lunnc8b09802016-06-04 21:16:57 +0200387 dsa_slave_destroy(ds->ports[port].netdev);
Andrew Lunn3a445142016-03-12 00:01:38 +0100388 }
389
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200390 /* Disable configuration of the CPU and DSA ports */
Neil Armstrongcbc5d902015-10-06 15:40:32 +0100391 for (port = 0; port < DSA_MAX_PORTS; port++) {
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200392 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
393 continue;
394 dsa_cpu_dsa_destroy(ds->ports[port].dn);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200395
396 /* Clearing a bit which is not set does no harm */
397 ds->cpu_port_mask |= ~(1 << port);
398 ds->dsa_port_mask |= ~(1 << port);
Neil Armstrongcbc5d902015-10-06 15:40:32 +0100399 }
400
Vivien Didelot9d490b42016-08-23 12:38:56 -0400401 if (ds->slave_mii_bus && ds->ops->phy_read)
Andrew Lunne755e492016-06-04 21:17:04 +0200402 mdiobus_unregister(ds->slave_mii_bus);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000403}
404
Thierry Redinge506d402014-10-01 13:59:00 +0200405#ifdef CONFIG_PM_SLEEP
Florian Fainelliea825e72016-08-18 15:30:12 -0700406int dsa_switch_suspend(struct dsa_switch *ds)
Florian Fainelli24462542014-09-18 17:31:22 -0700407{
408 int i, ret = 0;
409
410 /* Suspend slave network devices */
411 for (i = 0; i < DSA_MAX_PORTS; i++) {
Guenter Roeckd79d2102015-02-24 23:02:02 -0800412 if (!dsa_is_port_initialized(ds, i))
Florian Fainelli24462542014-09-18 17:31:22 -0700413 continue;
414
Andrew Lunnc8b09802016-06-04 21:16:57 +0200415 ret = dsa_slave_suspend(ds->ports[i].netdev);
Florian Fainelli24462542014-09-18 17:31:22 -0700416 if (ret)
417 return ret;
418 }
419
Vivien Didelot9d490b42016-08-23 12:38:56 -0400420 if (ds->ops->suspend)
421 ret = ds->ops->suspend(ds);
Florian Fainelli24462542014-09-18 17:31:22 -0700422
423 return ret;
424}
Florian Fainelliea825e72016-08-18 15:30:12 -0700425EXPORT_SYMBOL_GPL(dsa_switch_suspend);
Florian Fainelli24462542014-09-18 17:31:22 -0700426
Florian Fainelliea825e72016-08-18 15:30:12 -0700427int dsa_switch_resume(struct dsa_switch *ds)
Florian Fainelli24462542014-09-18 17:31:22 -0700428{
429 int i, ret = 0;
430
Vivien Didelot9d490b42016-08-23 12:38:56 -0400431 if (ds->ops->resume)
432 ret = ds->ops->resume(ds);
Florian Fainelli24462542014-09-18 17:31:22 -0700433
434 if (ret)
435 return ret;
436
437 /* Resume slave network devices */
438 for (i = 0; i < DSA_MAX_PORTS; i++) {
Guenter Roeckd79d2102015-02-24 23:02:02 -0800439 if (!dsa_is_port_initialized(ds, i))
Florian Fainelli24462542014-09-18 17:31:22 -0700440 continue;
441
Andrew Lunnc8b09802016-06-04 21:16:57 +0200442 ret = dsa_slave_resume(ds->ports[i].netdev);
Florian Fainelli24462542014-09-18 17:31:22 -0700443 if (ret)
444 return ret;
445 }
446
447 return 0;
448}
Florian Fainelliea825e72016-08-18 15:30:12 -0700449EXPORT_SYMBOL_GPL(dsa_switch_resume);
Thierry Redinge506d402014-10-01 13:59:00 +0200450#endif
Florian Fainelli24462542014-09-18 17:31:22 -0700451
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000452/* platform driver init and cleanup *****************************************/
453static int dev_is_class(struct device *dev, void *class)
454{
455 if (dev->class != NULL && !strcmp(dev->class->name, class))
456 return 1;
457
458 return 0;
459}
460
461static struct device *dev_find_class(struct device *parent, char *class)
462{
463 if (dev_is_class(parent, class)) {
464 get_device(parent);
465 return parent;
466 }
467
468 return device_find_child(parent, class, dev_is_class);
469}
470
Alexander Duyckb4d23942014-09-15 13:00:27 -0400471struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000472{
473 struct device *d;
474
475 d = dev_find_class(dev, "mdio_bus");
476 if (d != NULL) {
477 struct mii_bus *bus;
478
479 bus = to_mii_bus(d);
480 put_device(d);
481
482 return bus;
483 }
484
485 return NULL;
486}
Alexander Duyckb4d23942014-09-15 13:00:27 -0400487EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000488
489static struct net_device *dev_to_net_device(struct device *dev)
490{
491 struct device *d;
492
493 d = dev_find_class(dev, "net");
494 if (d != NULL) {
495 struct net_device *nd;
496
497 nd = to_net_dev(d);
498 dev_hold(nd);
499 put_device(d);
500
501 return nd;
502 }
503
504 return NULL;
505}
506
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000507#ifdef CONFIG_OF
508static int dsa_of_setup_routing_table(struct dsa_platform_data *pd,
509 struct dsa_chip_data *cd,
Pavel Nakonechny30303812015-04-05 00:46:21 +0300510 int chip_index, int port_index,
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000511 struct device_node *link)
512{
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000513 const __be32 *reg;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000514 int link_sw_addr;
515 struct device_node *parent_sw;
516 int len;
517
518 parent_sw = of_get_parent(link);
519 if (!parent_sw)
520 return -EINVAL;
521
522 reg = of_get_property(parent_sw, "reg", &len);
523 if (!reg || (len != sizeof(*reg) * 2))
524 return -EINVAL;
525
Pavel Nakonechny30303812015-04-05 00:46:21 +0300526 /*
527 * Get the destination switch number from the second field of its 'reg'
528 * property, i.e. for "reg = <0x19 1>" sw_addr is '1'.
529 */
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000530 link_sw_addr = be32_to_cpup(reg + 1);
531
532 if (link_sw_addr >= pd->nr_chips)
533 return -EINVAL;
534
Pavel Nakonechny30303812015-04-05 00:46:21 +0300535 cd->rtable[link_sw_addr] = port_index;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000536
537 return 0;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000538}
539
Andrew Lunn1e72e6f2015-08-17 23:52:50 +0200540static int dsa_of_probe_links(struct dsa_platform_data *pd,
541 struct dsa_chip_data *cd,
542 int chip_index, int port_index,
543 struct device_node *port,
544 const char *port_name)
545{
546 struct device_node *link;
547 int link_index;
548 int ret;
549
550 for (link_index = 0;; link_index++) {
551 link = of_parse_phandle(port, "link", link_index);
552 if (!link)
553 break;
554
555 if (!strcmp(port_name, "dsa") && pd->nr_chips > 1) {
556 ret = dsa_of_setup_routing_table(pd, cd, chip_index,
557 port_index, link);
558 if (ret)
559 return ret;
560 }
561 }
562 return 0;
563}
564
Florian Fainelli21168242013-03-25 05:03:39 +0000565static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
566{
567 int i;
568 int port_index;
569
570 for (i = 0; i < pd->nr_chips; i++) {
571 port_index = 0;
Florian Fainelli5f64a7d2013-03-25 05:03:40 +0000572 while (port_index < DSA_MAX_PORTS) {
Fabian Frederick1f747142014-06-23 19:32:47 +0200573 kfree(pd->chip[i].port_names[port_index]);
Florian Fainelli5f64a7d2013-03-25 05:03:40 +0000574 port_index++;
575 }
Russell Kinge496ae62015-09-24 20:35:57 +0100576
577 /* Drop our reference to the MDIO bus device */
578 if (pd->chip[i].host_dev)
579 put_device(pd->chip[i].host_dev);
Florian Fainelli21168242013-03-25 05:03:39 +0000580 }
581 kfree(pd->chip);
582}
583
Florian Fainellif1a26a02015-03-05 12:35:04 -0800584static int dsa_of_probe(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000585{
Florian Fainellif1a26a02015-03-05 12:35:04 -0800586 struct device_node *np = dev->of_node;
Andrew Lunn1e72e6f2015-08-17 23:52:50 +0200587 struct device_node *child, *mdio, *ethernet, *port;
Andrew Lunn6bc6d0a2015-08-08 17:09:14 +0200588 struct mii_bus *mdio_bus, *mdio_bus_switch;
Florian Fainelli769a0202015-03-09 14:31:21 -0700589 struct net_device *ethernet_dev;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000590 struct dsa_platform_data *pd;
591 struct dsa_chip_data *cd;
592 const char *port_name;
593 int chip_index, port_index;
594 const unsigned int *sw_addr, *port_reg;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700595 u32 eeprom_len;
Florian Fainelli21168242013-03-25 05:03:39 +0000596 int ret;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000597
598 mdio = of_parse_phandle(np, "dsa,mii-bus", 0);
599 if (!mdio)
600 return -EINVAL;
601
602 mdio_bus = of_mdio_find_bus(mdio);
603 if (!mdio_bus)
Florian Fainellib324c072015-03-05 12:35:05 -0800604 return -EPROBE_DEFER;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000605
606 ethernet = of_parse_phandle(np, "dsa,ethernet", 0);
Russell Kinge496ae62015-09-24 20:35:57 +0100607 if (!ethernet) {
608 ret = -EINVAL;
609 goto out_put_mdio;
610 }
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000611
Florian Fainelli769a0202015-03-09 14:31:21 -0700612 ethernet_dev = of_find_net_device_by_node(ethernet);
Russell Kinge496ae62015-09-24 20:35:57 +0100613 if (!ethernet_dev) {
614 ret = -EPROBE_DEFER;
615 goto out_put_mdio;
616 }
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000617
618 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
Russell Kinge496ae62015-09-24 20:35:57 +0100619 if (!pd) {
620 ret = -ENOMEM;
Russell King9861f722015-09-24 20:36:33 +0100621 goto out_put_ethernet;
Russell Kinge496ae62015-09-24 20:35:57 +0100622 }
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000623
Florian Fainellif1a26a02015-03-05 12:35:04 -0800624 dev->platform_data = pd;
Florian Fainelli769a0202015-03-09 14:31:21 -0700625 pd->of_netdev = ethernet_dev;
Tobias Waldekranze04449f2015-02-05 14:54:09 +0100626 pd->nr_chips = of_get_available_child_count(np);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000627 if (pd->nr_chips > DSA_MAX_SWITCHES)
628 pd->nr_chips = DSA_MAX_SWITCHES;
629
Fabian Frederick6f2aed62014-11-14 19:38:23 +0100630 pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data),
631 GFP_KERNEL);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000632 if (!pd->chip) {
633 ret = -ENOMEM;
634 goto out_free;
635 }
636
Fabian Godehardtd1c0b472014-05-16 06:21:44 +0200637 chip_index = -1;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000638 for_each_available_child_of_node(np, child) {
Vivien Didelotd3902382016-07-06 20:03:54 -0400639 int i;
640
Fabian Godehardtd1c0b472014-05-16 06:21:44 +0200641 chip_index++;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000642 cd = &pd->chip[chip_index];
643
Florian Fainellifa981d92014-08-27 17:04:49 -0700644 cd->of_node = child;
Russell Kinge496ae62015-09-24 20:35:57 +0100645
Vivien Didelotd3902382016-07-06 20:03:54 -0400646 /* Initialize the routing table */
647 for (i = 0; i < DSA_MAX_SWITCHES; ++i)
648 cd->rtable[i] = DSA_RTABLE_NONE;
649
Russell Kinge496ae62015-09-24 20:35:57 +0100650 /* When assigning the host device, increment its refcount */
651 cd->host_dev = get_device(&mdio_bus->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000652
653 sw_addr = of_get_property(child, "reg", NULL);
654 if (!sw_addr)
655 continue;
656
657 cd->sw_addr = be32_to_cpup(sw_addr);
Florian Fainellic8cf89f2015-07-11 18:02:11 -0700658 if (cd->sw_addr >= PHY_MAX_ADDR)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000659 continue;
660
Guenter Roeck50d49642015-04-29 10:56:15 -0700661 if (!of_property_read_u32(child, "eeprom-length", &eeprom_len))
Guenter Roeck6793abb2014-10-29 10:45:01 -0700662 cd->eeprom_len = eeprom_len;
663
Andrew Lunn6bc6d0a2015-08-08 17:09:14 +0200664 mdio = of_parse_phandle(child, "mii-bus", 0);
665 if (mdio) {
666 mdio_bus_switch = of_mdio_find_bus(mdio);
667 if (!mdio_bus_switch) {
668 ret = -EPROBE_DEFER;
669 goto out_free_chip;
670 }
Russell Kinge496ae62015-09-24 20:35:57 +0100671
672 /* Drop the mdio_bus device ref, replacing the host
673 * device with the mdio_bus_switch device, keeping
674 * the refcount from of_mdio_find_bus() above.
675 */
676 put_device(cd->host_dev);
Andrew Lunn6bc6d0a2015-08-08 17:09:14 +0200677 cd->host_dev = &mdio_bus_switch->dev;
678 }
679
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000680 for_each_available_child_of_node(child, port) {
681 port_reg = of_get_property(port, "reg", NULL);
682 if (!port_reg)
683 continue;
684
685 port_index = be32_to_cpup(port_reg);
Florian Fainelli8f5063e2015-07-11 18:02:10 -0700686 if (port_index >= DSA_MAX_PORTS)
687 break;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000688
689 port_name = of_get_property(port, "label", NULL);
690 if (!port_name)
691 continue;
692
Florian Fainellibd474972014-08-27 17:04:50 -0700693 cd->port_dn[port_index] = port;
694
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000695 cd->port_names[port_index] = kstrdup(port_name,
696 GFP_KERNEL);
697 if (!cd->port_names[port_index]) {
698 ret = -ENOMEM;
699 goto out_free_chip;
700 }
701
Andrew Lunn1e72e6f2015-08-17 23:52:50 +0200702 ret = dsa_of_probe_links(pd, cd, chip_index,
703 port_index, port, port_name);
704 if (ret)
705 goto out_free_chip;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000706
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000707 }
708 }
709
Russell Kinge496ae62015-09-24 20:35:57 +0100710 /* The individual chips hold their own refcount on the mdio bus,
711 * so drop ours */
712 put_device(&mdio_bus->dev);
713
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000714 return 0;
715
716out_free_chip:
Florian Fainelli21168242013-03-25 05:03:39 +0000717 dsa_of_free_platform_data(pd);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000718out_free:
719 kfree(pd);
Florian Fainellif1a26a02015-03-05 12:35:04 -0800720 dev->platform_data = NULL;
Russell King9861f722015-09-24 20:36:33 +0100721out_put_ethernet:
722 put_device(&ethernet_dev->dev);
Russell Kinge496ae62015-09-24 20:35:57 +0100723out_put_mdio:
724 put_device(&mdio_bus->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000725 return ret;
726}
727
Florian Fainellif1a26a02015-03-05 12:35:04 -0800728static void dsa_of_remove(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000729{
Florian Fainellif1a26a02015-03-05 12:35:04 -0800730 struct dsa_platform_data *pd = dev->platform_data;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000731
Florian Fainellif1a26a02015-03-05 12:35:04 -0800732 if (!dev->of_node)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000733 return;
734
Florian Fainelli21168242013-03-25 05:03:39 +0000735 dsa_of_free_platform_data(pd);
Russell King9861f722015-09-24 20:36:33 +0100736 put_device(&pd->of_netdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000737 kfree(pd);
738}
739#else
Florian Fainellif1a26a02015-03-05 12:35:04 -0800740static inline int dsa_of_probe(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000741{
742 return 0;
743}
744
Florian Fainellif1a26a02015-03-05 12:35:04 -0800745static inline void dsa_of_remove(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000746{
747}
748#endif
749
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100750static int dsa_setup_dst(struct dsa_switch_tree *dst, struct net_device *dev,
751 struct device *parent, struct dsa_platform_data *pd)
Florian Fainellic86e59b2015-03-05 12:35:08 -0800752{
753 int i;
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100754 unsigned configured = 0;
Florian Fainellic86e59b2015-03-05 12:35:08 -0800755
756 dst->pd = pd;
757 dst->master_netdev = dev;
758 dst->cpu_switch = -1;
759 dst->cpu_port = -1;
760
761 for (i = 0; i < pd->nr_chips; i++) {
762 struct dsa_switch *ds;
763
764 ds = dsa_switch_setup(dst, i, parent, pd->chip[i].host_dev);
765 if (IS_ERR(ds)) {
766 netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n",
767 i, PTR_ERR(ds));
768 continue;
769 }
770
771 dst->ds[i] = ds;
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100772
773 ++configured;
Florian Fainellic86e59b2015-03-05 12:35:08 -0800774 }
775
776 /*
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100777 * If no switch was found, exit cleanly
778 */
779 if (!configured)
780 return -EPROBE_DEFER;
781
782 /*
Florian Fainellic86e59b2015-03-05 12:35:08 -0800783 * If we use a tagging format that doesn't have an ethertype
784 * field, make sure that all packets from this point on get
785 * sent to the tag format's receive function.
786 */
787 wmb();
788 dev->dsa_ptr = (void *)dst;
789
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100790 return 0;
Florian Fainellic86e59b2015-03-05 12:35:08 -0800791}
792
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000793static int dsa_probe(struct platform_device *pdev)
794{
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000795 struct dsa_platform_data *pd = pdev->dev.platform_data;
796 struct net_device *dev;
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000797 struct dsa_switch_tree *dst;
Florian Fainellic86e59b2015-03-05 12:35:08 -0800798 int ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000799
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000800 if (pdev->dev.of_node) {
Florian Fainellif1a26a02015-03-05 12:35:04 -0800801 ret = dsa_of_probe(&pdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000802 if (ret)
803 return ret;
804
805 pd = pdev->dev.platform_data;
806 }
807
Florian Fainelli769a0202015-03-09 14:31:21 -0700808 if (pd == NULL || (pd->netdev == NULL && pd->of_netdev == NULL))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000809 return -EINVAL;
810
Florian Fainelli769a0202015-03-09 14:31:21 -0700811 if (pd->of_netdev) {
812 dev = pd->of_netdev;
813 dev_hold(dev);
814 } else {
815 dev = dev_to_net_device(pd->netdev);
816 }
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000817 if (dev == NULL) {
Florian Fainellib324c072015-03-05 12:35:05 -0800818 ret = -EPROBE_DEFER;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000819 goto out;
820 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000821
822 if (dev->dsa_ptr != NULL) {
823 dev_put(dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000824 ret = -EEXIST;
825 goto out;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000826 }
827
Neil Armstrongd4ac35d2015-10-06 15:40:37 +0100828 dst = devm_kzalloc(&pdev->dev, sizeof(*dst), GFP_KERNEL);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000829 if (dst == NULL) {
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000830 dev_put(dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000831 ret = -ENOMEM;
832 goto out;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000833 }
834
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000835 platform_set_drvdata(pdev, dst);
836
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100837 ret = dsa_setup_dst(dst, dev, &pdev->dev, pd);
Neil Armstrong679fb462015-12-07 13:57:34 +0100838 if (ret) {
839 dev_put(dev);
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100840 goto out;
Neil Armstrong679fb462015-12-07 13:57:34 +0100841 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000842
843 return 0;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000844
845out:
Florian Fainellif1a26a02015-03-05 12:35:04 -0800846 dsa_of_remove(&pdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000847
848 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000849}
850
Florian Fainellic86e59b2015-03-05 12:35:08 -0800851static void dsa_remove_dst(struct dsa_switch_tree *dst)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000852{
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000853 int i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000854
Neil Armstrong04761892016-03-08 10:36:20 +0100855 dst->master_netdev->dsa_ptr = NULL;
856
857 /* If we used a tagging format that doesn't have an ethertype
858 * field, make sure that all packets from this point get sent
859 * without the tag and go through the regular receive path.
860 */
861 wmb();
862
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000863 for (i = 0; i < dst->pd->nr_chips; i++) {
864 struct dsa_switch *ds = dst->ds[i];
865
Neil Armstrongd4ac35d2015-10-06 15:40:37 +0100866 if (ds)
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000867 dsa_switch_destroy(ds);
868 }
Neil Armstrong679fb462015-12-07 13:57:34 +0100869
Florian Fainelli0c73c522016-06-07 16:32:42 -0700870 dsa_cpu_port_ethtool_restore(dst->ds[0]);
871
Neil Armstrong679fb462015-12-07 13:57:34 +0100872 dev_put(dst->master_netdev);
Florian Fainellic86e59b2015-03-05 12:35:08 -0800873}
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000874
Florian Fainellic86e59b2015-03-05 12:35:08 -0800875static int dsa_remove(struct platform_device *pdev)
876{
877 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
878
879 dsa_remove_dst(dst);
Florian Fainellif1a26a02015-03-05 12:35:04 -0800880 dsa_of_remove(&pdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000881
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000882 return 0;
883}
884
885static void dsa_shutdown(struct platform_device *pdev)
886{
887}
888
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700889static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
890 struct packet_type *pt, struct net_device *orig_dev)
891{
892 struct dsa_switch_tree *dst = dev->dsa_ptr;
893
894 if (unlikely(dst == NULL)) {
895 kfree_skb(skb);
896 return 0;
897 }
898
Alexander Duyck50753142014-09-15 13:00:19 -0400899 return dst->rcv(skb, dev, pt, orig_dev);
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700900}
901
Florian Fainelli61b73632014-08-29 12:42:07 -0700902static struct packet_type dsa_pack_type __read_mostly = {
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700903 .type = cpu_to_be16(ETH_P_XDSA),
904 .func = dsa_switch_rcv,
905};
906
Florian Fainellib73adef2015-02-24 13:15:33 -0800907static struct notifier_block dsa_netdevice_nb __read_mostly = {
908 .notifier_call = dsa_slave_netdevice_event,
909};
910
Florian Fainelli24462542014-09-18 17:31:22 -0700911#ifdef CONFIG_PM_SLEEP
912static int dsa_suspend(struct device *d)
913{
914 struct platform_device *pdev = to_platform_device(d);
915 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
916 int i, ret = 0;
917
918 for (i = 0; i < dst->pd->nr_chips; i++) {
919 struct dsa_switch *ds = dst->ds[i];
920
921 if (ds != NULL)
922 ret = dsa_switch_suspend(ds);
923 }
924
925 return ret;
926}
927
928static int dsa_resume(struct device *d)
929{
930 struct platform_device *pdev = to_platform_device(d);
931 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
932 int i, ret = 0;
933
934 for (i = 0; i < dst->pd->nr_chips; i++) {
935 struct dsa_switch *ds = dst->ds[i];
936
937 if (ds != NULL)
938 ret = dsa_switch_resume(ds);
939 }
940
941 return ret;
942}
943#endif
944
945static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume);
946
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000947static const struct of_device_id dsa_of_match_table[] = {
948 { .compatible = "marvell,dsa", },
949 {}
950};
951MODULE_DEVICE_TABLE(of, dsa_of_match_table);
952
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000953static struct platform_driver dsa_driver = {
954 .probe = dsa_probe,
955 .remove = dsa_remove,
956 .shutdown = dsa_shutdown,
957 .driver = {
958 .name = "dsa",
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000959 .of_match_table = dsa_of_match_table,
Florian Fainelli24462542014-09-18 17:31:22 -0700960 .pm = &dsa_pm_ops,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000961 },
962};
963
964static int __init dsa_init_module(void)
965{
Ben Hutchings7df899c2011-11-25 14:35:02 +0000966 int rc;
967
Florian Fainellib73adef2015-02-24 13:15:33 -0800968 register_netdevice_notifier(&dsa_netdevice_nb);
969
Ben Hutchings7df899c2011-11-25 14:35:02 +0000970 rc = platform_driver_register(&dsa_driver);
971 if (rc)
972 return rc;
973
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700974 dev_add_pack(&dsa_pack_type);
975
Ben Hutchings7df899c2011-11-25 14:35:02 +0000976 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000977}
978module_init(dsa_init_module);
979
980static void __exit dsa_cleanup_module(void)
981{
Florian Fainellib73adef2015-02-24 13:15:33 -0800982 unregister_netdevice_notifier(&dsa_netdevice_nb);
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700983 dev_remove_pack(&dsa_pack_type);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000984 platform_driver_unregister(&dsa_driver);
985}
986module_exit(dsa_cleanup_module);
987
Rusty Russell577d6a72011-01-24 14:32:52 -0600988MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000989MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
990MODULE_LICENSE("GPL");
991MODULE_ALIAS("platform:dsa");