blob: 95d1a756202c45b6b1332163fd83d2c3e0c123c4 [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>
Florian Fainelli5e95329b2013-03-22 10:50:50 +000017#include <linux/of.h>
18#include <linux/of_mdio.h>
19#include <linux/of_platform.h>
Florian Fainelli769a0202015-03-09 14:31:21 -070020#include <linux/of_net.h>
Andrew Lunncc30c162015-11-20 03:56:23 +010021#include <linux/of_gpio.h>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020022#include <linux/netdevice.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>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020026#include <net/dsa.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000027#include "dsa_priv.h"
28
Andrew Lunn39a7f2a2016-06-04 21:17:03 +020029static struct sk_buff *dsa_slave_notag_xmit(struct sk_buff *skb,
30 struct net_device *dev)
31{
32 /* Just return the original SKB */
33 return skb;
34}
35
36static const struct dsa_device_ops none_ops = {
37 .xmit = dsa_slave_notag_xmit,
38 .rcv = NULL,
39};
40
41const struct dsa_device_ops *dsa_device_ops[DSA_TAG_LAST] = {
42#ifdef CONFIG_NET_DSA_TAG_DSA
43 [DSA_TAG_PROTO_DSA] = &dsa_netdev_ops,
44#endif
45#ifdef CONFIG_NET_DSA_TAG_EDSA
46 [DSA_TAG_PROTO_EDSA] = &edsa_netdev_ops,
47#endif
48#ifdef CONFIG_NET_DSA_TAG_TRAILER
49 [DSA_TAG_PROTO_TRAILER] = &trailer_netdev_ops,
50#endif
51#ifdef CONFIG_NET_DSA_TAG_BRCM
52 [DSA_TAG_PROTO_BRCM] = &brcm_netdev_ops,
53#endif
John Crispincafdc452016-09-15 16:26:40 +020054#ifdef CONFIG_NET_DSA_TAG_QCA
55 [DSA_TAG_PROTO_QCA] = &qca_netdev_ops,
56#endif
Andrew Lunn39a7f2a2016-06-04 21:17:03 +020057 [DSA_TAG_PROTO_NONE] = &none_ops,
58};
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000059
60/* switch driver registration ***********************************************/
61static DEFINE_MUTEX(dsa_switch_drivers_mutex);
62static LIST_HEAD(dsa_switch_drivers);
63
Florian Fainelliab3d4082017-01-08 14:52:07 -080064void register_switch_driver(struct dsa_switch_driver *drv)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000065{
66 mutex_lock(&dsa_switch_drivers_mutex);
Florian Fainelliab3d4082017-01-08 14:52:07 -080067 list_add_tail(&drv->list, &dsa_switch_drivers);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000068 mutex_unlock(&dsa_switch_drivers_mutex);
69}
Ben Hutchingsad293b82011-11-25 14:34:07 +000070EXPORT_SYMBOL_GPL(register_switch_driver);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000071
Florian Fainelliab3d4082017-01-08 14:52:07 -080072void unregister_switch_driver(struct dsa_switch_driver *drv)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000073{
74 mutex_lock(&dsa_switch_drivers_mutex);
Florian Fainelliab3d4082017-01-08 14:52:07 -080075 list_del_init(&drv->list);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000076 mutex_unlock(&dsa_switch_drivers_mutex);
77}
Ben Hutchingsad293b82011-11-25 14:34:07 +000078EXPORT_SYMBOL_GPL(unregister_switch_driver);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000079
Florian Fainellia82f67a2017-01-08 14:52:08 -080080static const struct dsa_switch_ops *
Andrew Lunnbbb8d792016-04-13 02:40:39 +020081dsa_switch_probe(struct device *parent, struct device *host_dev, int sw_addr,
Vivien Didelot0209d142016-04-17 13:23:55 -040082 const char **_name, void **priv)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000083{
Florian Fainellia82f67a2017-01-08 14:52:08 -080084 const struct dsa_switch_ops *ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000085 struct list_head *list;
Vivien Didelot0209d142016-04-17 13:23:55 -040086 const char *name;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000087
88 ret = NULL;
89 name = NULL;
90
91 mutex_lock(&dsa_switch_drivers_mutex);
92 list_for_each(list, &dsa_switch_drivers) {
Florian Fainellia82f67a2017-01-08 14:52:08 -080093 const struct dsa_switch_ops *ops;
Florian Fainelliab3d4082017-01-08 14:52:07 -080094 struct dsa_switch_driver *drv;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000095
Florian Fainelliab3d4082017-01-08 14:52:07 -080096 drv = list_entry(list, struct dsa_switch_driver, list);
97 ops = drv->ops;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000098
Vivien Didelot9d490b42016-08-23 12:38:56 -040099 name = ops->probe(parent, host_dev, sw_addr, priv);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000100 if (name != NULL) {
Vivien Didelot9d490b42016-08-23 12:38:56 -0400101 ret = ops;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000102 break;
103 }
104 }
105 mutex_unlock(&dsa_switch_drivers_mutex);
106
107 *_name = name;
108
109 return ret;
110}
111
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000112/* basic switch operations **************************************************/
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200113int dsa_cpu_dsa_setup(struct dsa_switch *ds, struct device *dev,
Florian Fainelli293784a2017-01-26 10:45:52 -0800114 struct dsa_port *dport, int port)
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200115{
Florian Fainelli293784a2017-01-26 10:45:52 -0800116 struct device_node *port_dn = dport->dn;
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200117 struct phy_device *phydev;
118 int ret, mode;
119
120 if (of_phy_is_fixed_link(port_dn)) {
121 ret = of_phy_register_fixed_link(port_dn);
122 if (ret) {
123 dev_err(dev, "failed to register fixed PHY\n");
124 return ret;
125 }
126 phydev = of_phy_find_device(port_dn);
127
128 mode = of_get_phy_mode(port_dn);
129 if (mode < 0)
130 mode = PHY_INTERFACE_MODE_NA;
131 phydev->interface = mode;
132
133 genphy_config_init(phydev);
134 genphy_read_status(phydev);
Vivien Didelot9d490b42016-08-23 12:38:56 -0400135 if (ds->ops->adjust_link)
136 ds->ops->adjust_link(ds, port, phydev);
Johan Hovoldfd05d7b2016-11-24 19:21:27 +0100137
138 put_device(&phydev->mdio.dev);
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200139 }
140
141 return 0;
142}
143
144static int dsa_cpu_dsa_setups(struct dsa_switch *ds, struct device *dev)
Andrew Lunn39b0c702015-08-31 15:56:49 +0200145{
Florian Fainelli293784a2017-01-26 10:45:52 -0800146 struct dsa_port *dport;
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200147 int ret, port;
Andrew Lunn39b0c702015-08-31 15:56:49 +0200148
Vivien Didelot26895e22017-01-27 15:29:37 -0500149 for (port = 0; port < ds->num_ports; port++) {
Andrew Lunn39b0c702015-08-31 15:56:49 +0200150 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
151 continue;
152
Florian Fainelli293784a2017-01-26 10:45:52 -0800153 dport = &ds->ports[port];
154 ret = dsa_cpu_dsa_setup(ds, dev, dport, port);
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200155 if (ret)
156 return ret;
Andrew Lunn39b0c702015-08-31 15:56:49 +0200157 }
158 return 0;
159}
160
Andrew Lunn39a7f2a2016-06-04 21:17:03 +0200161const struct dsa_device_ops *dsa_resolve_tag_protocol(int tag_protocol)
162{
163 const struct dsa_device_ops *ops;
164
165 if (tag_protocol >= DSA_TAG_LAST)
166 return ERR_PTR(-EINVAL);
167 ops = dsa_device_ops[tag_protocol];
168
169 if (!ops)
170 return ERR_PTR(-ENOPROTOOPT);
171
172 return ops;
173}
174
Florian Fainelli0c73c522016-06-07 16:32:42 -0700175int dsa_cpu_port_ethtool_setup(struct dsa_switch *ds)
176{
177 struct net_device *master;
178 struct ethtool_ops *cpu_ops;
179
180 master = ds->dst->master_netdev;
181 if (ds->master_netdev)
182 master = ds->master_netdev;
183
184 cpu_ops = devm_kzalloc(ds->dev, sizeof(*cpu_ops), GFP_KERNEL);
185 if (!cpu_ops)
186 return -ENOMEM;
187
188 memcpy(&ds->dst->master_ethtool_ops, master->ethtool_ops,
189 sizeof(struct ethtool_ops));
190 ds->dst->master_orig_ethtool_ops = master->ethtool_ops;
191 memcpy(cpu_ops, &ds->dst->master_ethtool_ops,
192 sizeof(struct ethtool_ops));
193 dsa_cpu_port_ethtool_init(cpu_ops);
194 master->ethtool_ops = cpu_ops;
195
196 return 0;
197}
198
199void dsa_cpu_port_ethtool_restore(struct dsa_switch *ds)
200{
201 struct net_device *master;
202
203 master = ds->dst->master_netdev;
204 if (ds->master_netdev)
205 master = ds->master_netdev;
206
207 master->ethtool_ops = ds->dst->master_orig_ethtool_ops;
208}
209
Florian Fainellidf197192015-03-05 12:35:06 -0800210static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000211{
Florian Fainellia82f67a2017-01-08 14:52:08 -0800212 const struct dsa_switch_ops *ops = ds->ops;
Florian Fainellidf197192015-03-05 12:35:06 -0800213 struct dsa_switch_tree *dst = ds->dst;
Andrew Lunnff049552016-05-10 23:27:24 +0200214 struct dsa_chip_data *cd = ds->cd;
Florian Fainellif9bf5a22013-01-21 09:58:51 +0000215 bool valid_name_found = false;
Florian Fainellidf197192015-03-05 12:35:06 -0800216 int index = ds->index;
217 int i, ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000218
219 /*
220 * Validate supplied switch configuration.
221 */
Vivien Didelot26895e22017-01-27 15:29:37 -0500222 for (i = 0; i < ds->num_ports; i++) {
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000223 char *name;
224
Andrew Lunnff049552016-05-10 23:27:24 +0200225 name = cd->port_names[i];
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000226 if (name == NULL)
227 continue;
228
229 if (!strcmp(name, "cpu")) {
Andrew Lunn23e3d612017-01-22 22:16:45 +0100230 if (dst->cpu_switch) {
Joe Perchesa2ae6002014-11-09 16:32:46 -0800231 netdev_err(dst->master_netdev,
232 "multiple cpu ports?!\n");
Vivien Didelota896eee2017-01-03 14:31:49 -0500233 return -EINVAL;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000234 }
Vivien Didelotb22de492017-01-17 20:41:38 -0500235 dst->cpu_switch = ds;
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000236 dst->cpu_port = i;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200237 ds->cpu_port_mask |= 1 << i;
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000238 } else if (!strcmp(name, "dsa")) {
239 ds->dsa_port_mask |= 1 << i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000240 } else {
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200241 ds->enabled_port_mask |= 1 << i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000242 }
Florian Fainellif9bf5a22013-01-21 09:58:51 +0000243 valid_name_found = true;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000244 }
245
Vivien Didelot26895e22017-01-27 15:29:37 -0500246 if (!valid_name_found && i == ds->num_ports)
Vivien Didelota896eee2017-01-03 14:31:49 -0500247 return -EINVAL;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000248
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700249 /* Make the built-in MII bus mask match the number of ports,
250 * switch drivers can override this later
251 */
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200252 ds->phys_mii_mask = ds->enabled_port_mask;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700253
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000254 /*
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000255 * If the CPU connects to this switch, set the switch tree
256 * tagging protocol to the preferred tagging format of this
257 * switch.
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000258 */
Vivien Didelotb22de492017-01-17 20:41:38 -0500259 if (dst->cpu_switch == ds) {
Andrew Lunn7b314362016-08-22 16:01:01 +0200260 enum dsa_tag_protocol tag_protocol;
261
Vivien Didelot9d490b42016-08-23 12:38:56 -0400262 tag_protocol = ops->get_tag_protocol(ds);
Andrew Lunn7b314362016-08-22 16:01:01 +0200263 dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol);
Vivien Didelota896eee2017-01-03 14:31:49 -0500264 if (IS_ERR(dst->tag_ops))
265 return PTR_ERR(dst->tag_ops);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000266
Andrew Lunn39a7f2a2016-06-04 21:17:03 +0200267 dst->rcv = dst->tag_ops->rcv;
Alexander Duyck50753142014-09-15 13:00:19 -0400268 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000269
Andrew Lunn66472fc2016-06-04 21:17:00 +0200270 memcpy(ds->rtable, cd->rtable, sizeof(ds->rtable));
271
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000272 /*
273 * Do basic register setup.
274 */
Vivien Didelot9d490b42016-08-23 12:38:56 -0400275 ret = ops->setup(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000276 if (ret < 0)
Vivien Didelota896eee2017-01-03 14:31:49 -0500277 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000278
Vivien Didelotf515f192017-02-03 13:20:20 -0500279 ret = dsa_switch_register_notifier(ds);
280 if (ret)
281 return ret;
282
John Crispin092183d2016-09-19 15:28:01 +0200283 if (ops->set_addr) {
284 ret = ops->set_addr(ds, dst->master_netdev->dev_addr);
285 if (ret < 0)
Vivien Didelota896eee2017-01-03 14:31:49 -0500286 return ret;
John Crispin092183d2016-09-19 15:28:01 +0200287 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000288
Vivien Didelot9d490b42016-08-23 12:38:56 -0400289 if (!ds->slave_mii_bus && ops->phy_read) {
Andrew Lunne755e492016-06-04 21:17:04 +0200290 ds->slave_mii_bus = devm_mdiobus_alloc(parent);
Vivien Didelota896eee2017-01-03 14:31:49 -0500291 if (!ds->slave_mii_bus)
292 return -ENOMEM;
Andrew Lunne755e492016-06-04 21:17:04 +0200293 dsa_slave_mii_bus_init(ds);
294
295 ret = mdiobus_register(ds->slave_mii_bus);
296 if (ret < 0)
Vivien Didelota896eee2017-01-03 14:31:49 -0500297 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000298 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000299
300 /*
301 * Create network devices for physical switch ports.
302 */
Vivien Didelot26895e22017-01-27 15:29:37 -0500303 for (i = 0; i < ds->num_ports; i++) {
Andrew Lunn189b0d92016-06-04 21:16:58 +0200304 ds->ports[i].dn = cd->port_dn[i];
305
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200306 if (!(ds->enabled_port_mask & (1 << i)))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000307 continue;
308
Andrew Lunnff049552016-05-10 23:27:24 +0200309 ret = dsa_slave_create(ds, parent, i, cd->port_names[i]);
Vivien Didelota896eee2017-01-03 14:31:49 -0500310 if (ret < 0)
Russell Kingd25b8e72015-10-03 18:09:07 +0100311 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 +0200312 index, i, cd->port_names[i], ret);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000313 }
314
Andrew Lunn39b0c702015-08-31 15:56:49 +0200315 /* Perform configuration of the CPU and DSA ports */
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200316 ret = dsa_cpu_dsa_setups(ds, parent);
Vivien Didelota896eee2017-01-03 14:31:49 -0500317 if (ret < 0)
Andrew Lunn39b0c702015-08-31 15:56:49 +0200318 netdev_err(dst->master_netdev, "[%d] : can't configure CPU and DSA ports\n",
319 index);
Andrew Lunn39b0c702015-08-31 15:56:49 +0200320
Florian Fainelli0c73c522016-06-07 16:32:42 -0700321 ret = dsa_cpu_port_ethtool_setup(ds);
322 if (ret)
323 return ret;
324
Vivien Didelota896eee2017-01-03 14:31:49 -0500325 return 0;
Florian Fainellidf197192015-03-05 12:35:06 -0800326}
327
328static struct dsa_switch *
329dsa_switch_setup(struct dsa_switch_tree *dst, int index,
330 struct device *parent, struct device *host_dev)
331{
Andrew Lunnff049552016-05-10 23:27:24 +0200332 struct dsa_chip_data *cd = dst->pd->chip + index;
Florian Fainellia82f67a2017-01-08 14:52:08 -0800333 const struct dsa_switch_ops *ops;
Florian Fainellidf197192015-03-05 12:35:06 -0800334 struct dsa_switch *ds;
335 int ret;
Vivien Didelot0209d142016-04-17 13:23:55 -0400336 const char *name;
Andrew Lunn7543a6d2016-04-13 02:40:40 +0200337 void *priv;
Florian Fainellidf197192015-03-05 12:35:06 -0800338
339 /*
340 * Probe for switch model.
341 */
Vivien Didelot9d490b42016-08-23 12:38:56 -0400342 ops = dsa_switch_probe(parent, host_dev, cd->sw_addr, &name, &priv);
343 if (!ops) {
Florian Fainellidf197192015-03-05 12:35:06 -0800344 netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n",
345 index);
346 return ERR_PTR(-EINVAL);
347 }
348 netdev_info(dst->master_netdev, "[%d]: detected a %s switch\n",
349 index, name);
350
351
352 /*
353 * Allocate and initialise switch state.
354 */
Vivien Didelota0c02162017-01-27 15:29:36 -0500355 ds = dsa_switch_alloc(parent, DSA_MAX_PORTS);
356 if (!ds)
Florian Fainelli24595342015-05-29 10:29:46 -0700357 return ERR_PTR(-ENOMEM);
Florian Fainellidf197192015-03-05 12:35:06 -0800358
359 ds->dst = dst;
360 ds->index = index;
Andrew Lunnff049552016-05-10 23:27:24 +0200361 ds->cd = cd;
Vivien Didelot9d490b42016-08-23 12:38:56 -0400362 ds->ops = ops;
Andrew Lunn7543a6d2016-04-13 02:40:40 +0200363 ds->priv = priv;
Florian Fainellidf197192015-03-05 12:35:06 -0800364
365 ret = dsa_switch_setup_one(ds, parent);
366 if (ret)
Florian Fainelli24595342015-05-29 10:29:46 -0700367 return ERR_PTR(ret);
Florian Fainellidf197192015-03-05 12:35:06 -0800368
369 return ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000370}
371
Florian Fainelli293784a2017-01-26 10:45:52 -0800372void dsa_cpu_dsa_destroy(struct dsa_port *port)
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200373{
Florian Fainelli293784a2017-01-26 10:45:52 -0800374 struct device_node *port_dn = port->dn;
375
Johan Hovold3f650472016-11-28 19:24:55 +0100376 if (of_phy_is_fixed_link(port_dn))
377 of_phy_deregister_fixed_link(port_dn);
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200378}
379
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000380static void dsa_switch_destroy(struct dsa_switch *ds)
381{
Neil Armstrongcbc5d902015-10-06 15:40:32 +0100382 int port;
383
Andrew Lunn3a445142016-03-12 00:01:38 +0100384 /* Destroy network devices for physical switch ports. */
Vivien Didelot26895e22017-01-27 15:29:37 -0500385 for (port = 0; port < ds->num_ports; port++) {
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200386 if (!(ds->enabled_port_mask & (1 << port)))
Andrew Lunn3a445142016-03-12 00:01:38 +0100387 continue;
388
Andrew Lunnc8b09802016-06-04 21:16:57 +0200389 if (!ds->ports[port].netdev)
Andrew Lunn3a445142016-03-12 00:01:38 +0100390 continue;
391
Andrew Lunnc8b09802016-06-04 21:16:57 +0200392 dsa_slave_destroy(ds->ports[port].netdev);
Andrew Lunn3a445142016-03-12 00:01:38 +0100393 }
394
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200395 /* Disable configuration of the CPU and DSA ports */
Vivien Didelot26895e22017-01-27 15:29:37 -0500396 for (port = 0; port < ds->num_ports; port++) {
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200397 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
398 continue;
Florian Fainelli293784a2017-01-26 10:45:52 -0800399 dsa_cpu_dsa_destroy(&ds->ports[port]);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200400
401 /* Clearing a bit which is not set does no harm */
402 ds->cpu_port_mask |= ~(1 << port);
403 ds->dsa_port_mask |= ~(1 << port);
Neil Armstrongcbc5d902015-10-06 15:40:32 +0100404 }
405
Vivien Didelot9d490b42016-08-23 12:38:56 -0400406 if (ds->slave_mii_bus && ds->ops->phy_read)
Andrew Lunne755e492016-06-04 21:17:04 +0200407 mdiobus_unregister(ds->slave_mii_bus);
Vivien Didelotf515f192017-02-03 13:20:20 -0500408
409 dsa_switch_unregister_notifier(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000410}
411
Thierry Redinge506d402014-10-01 13:59:00 +0200412#ifdef CONFIG_PM_SLEEP
Florian Fainelliea825e72016-08-18 15:30:12 -0700413int dsa_switch_suspend(struct dsa_switch *ds)
Florian Fainelli24462542014-09-18 17:31:22 -0700414{
415 int i, ret = 0;
416
417 /* Suspend slave network devices */
Vivien Didelot26895e22017-01-27 15:29:37 -0500418 for (i = 0; i < ds->num_ports; i++) {
Guenter Roeckd79d2102015-02-24 23:02:02 -0800419 if (!dsa_is_port_initialized(ds, i))
Florian Fainelli24462542014-09-18 17:31:22 -0700420 continue;
421
Andrew Lunnc8b09802016-06-04 21:16:57 +0200422 ret = dsa_slave_suspend(ds->ports[i].netdev);
Florian Fainelli24462542014-09-18 17:31:22 -0700423 if (ret)
424 return ret;
425 }
426
Vivien Didelot9d490b42016-08-23 12:38:56 -0400427 if (ds->ops->suspend)
428 ret = ds->ops->suspend(ds);
Florian Fainelli24462542014-09-18 17:31:22 -0700429
430 return ret;
431}
Florian Fainelliea825e72016-08-18 15:30:12 -0700432EXPORT_SYMBOL_GPL(dsa_switch_suspend);
Florian Fainelli24462542014-09-18 17:31:22 -0700433
Florian Fainelliea825e72016-08-18 15:30:12 -0700434int dsa_switch_resume(struct dsa_switch *ds)
Florian Fainelli24462542014-09-18 17:31:22 -0700435{
436 int i, ret = 0;
437
Vivien Didelot9d490b42016-08-23 12:38:56 -0400438 if (ds->ops->resume)
439 ret = ds->ops->resume(ds);
Florian Fainelli24462542014-09-18 17:31:22 -0700440
441 if (ret)
442 return ret;
443
444 /* Resume slave network devices */
Vivien Didelot26895e22017-01-27 15:29:37 -0500445 for (i = 0; i < ds->num_ports; i++) {
Guenter Roeckd79d2102015-02-24 23:02:02 -0800446 if (!dsa_is_port_initialized(ds, i))
Florian Fainelli24462542014-09-18 17:31:22 -0700447 continue;
448
Andrew Lunnc8b09802016-06-04 21:16:57 +0200449 ret = dsa_slave_resume(ds->ports[i].netdev);
Florian Fainelli24462542014-09-18 17:31:22 -0700450 if (ret)
451 return ret;
452 }
453
454 return 0;
455}
Florian Fainelliea825e72016-08-18 15:30:12 -0700456EXPORT_SYMBOL_GPL(dsa_switch_resume);
Thierry Redinge506d402014-10-01 13:59:00 +0200457#endif
Florian Fainelli24462542014-09-18 17:31:22 -0700458
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000459/* platform driver init and cleanup *****************************************/
460static int dev_is_class(struct device *dev, void *class)
461{
462 if (dev->class != NULL && !strcmp(dev->class->name, class))
463 return 1;
464
465 return 0;
466}
467
468static struct device *dev_find_class(struct device *parent, char *class)
469{
470 if (dev_is_class(parent, class)) {
471 get_device(parent);
472 return parent;
473 }
474
475 return device_find_child(parent, class, dev_is_class);
476}
477
Alexander Duyckb4d23942014-09-15 13:00:27 -0400478struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000479{
480 struct device *d;
481
482 d = dev_find_class(dev, "mdio_bus");
483 if (d != NULL) {
484 struct mii_bus *bus;
485
486 bus = to_mii_bus(d);
487 put_device(d);
488
489 return bus;
490 }
491
492 return NULL;
493}
Alexander Duyckb4d23942014-09-15 13:00:27 -0400494EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000495
Florian Fainelli14b89f32017-02-04 13:02:42 -0800496struct net_device *dsa_dev_to_net_device(struct device *dev)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000497{
498 struct device *d;
499
500 d = dev_find_class(dev, "net");
501 if (d != NULL) {
502 struct net_device *nd;
503
504 nd = to_net_dev(d);
505 dev_hold(nd);
506 put_device(d);
507
508 return nd;
509 }
510
511 return NULL;
512}
Florian Fainelli14b89f32017-02-04 13:02:42 -0800513EXPORT_SYMBOL_GPL(dsa_dev_to_net_device);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000514
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000515#ifdef CONFIG_OF
516static int dsa_of_setup_routing_table(struct dsa_platform_data *pd,
517 struct dsa_chip_data *cd,
Pavel Nakonechny30303812015-04-05 00:46:21 +0300518 int chip_index, int port_index,
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000519 struct device_node *link)
520{
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000521 const __be32 *reg;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000522 int link_sw_addr;
523 struct device_node *parent_sw;
524 int len;
525
526 parent_sw = of_get_parent(link);
527 if (!parent_sw)
528 return -EINVAL;
529
530 reg = of_get_property(parent_sw, "reg", &len);
531 if (!reg || (len != sizeof(*reg) * 2))
532 return -EINVAL;
533
Pavel Nakonechny30303812015-04-05 00:46:21 +0300534 /*
535 * Get the destination switch number from the second field of its 'reg'
536 * property, i.e. for "reg = <0x19 1>" sw_addr is '1'.
537 */
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000538 link_sw_addr = be32_to_cpup(reg + 1);
539
540 if (link_sw_addr >= pd->nr_chips)
541 return -EINVAL;
542
Pavel Nakonechny30303812015-04-05 00:46:21 +0300543 cd->rtable[link_sw_addr] = port_index;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000544
545 return 0;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000546}
547
Andrew Lunn1e72e6f2015-08-17 23:52:50 +0200548static int dsa_of_probe_links(struct dsa_platform_data *pd,
549 struct dsa_chip_data *cd,
550 int chip_index, int port_index,
551 struct device_node *port,
552 const char *port_name)
553{
554 struct device_node *link;
555 int link_index;
556 int ret;
557
558 for (link_index = 0;; link_index++) {
559 link = of_parse_phandle(port, "link", link_index);
560 if (!link)
561 break;
562
563 if (!strcmp(port_name, "dsa") && pd->nr_chips > 1) {
564 ret = dsa_of_setup_routing_table(pd, cd, chip_index,
565 port_index, link);
566 if (ret)
567 return ret;
568 }
569 }
570 return 0;
571}
572
Florian Fainelli21168242013-03-25 05:03:39 +0000573static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
574{
575 int i;
576 int port_index;
577
578 for (i = 0; i < pd->nr_chips; i++) {
579 port_index = 0;
Florian Fainelli5f64a7d2013-03-25 05:03:40 +0000580 while (port_index < DSA_MAX_PORTS) {
Fabian Frederick1f747142014-06-23 19:32:47 +0200581 kfree(pd->chip[i].port_names[port_index]);
Florian Fainelli5f64a7d2013-03-25 05:03:40 +0000582 port_index++;
583 }
Russell Kinge496ae62015-09-24 20:35:57 +0100584
585 /* Drop our reference to the MDIO bus device */
586 if (pd->chip[i].host_dev)
587 put_device(pd->chip[i].host_dev);
Florian Fainelli21168242013-03-25 05:03:39 +0000588 }
589 kfree(pd->chip);
590}
591
Florian Fainellif1a26a02015-03-05 12:35:04 -0800592static int dsa_of_probe(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000593{
Florian Fainellif1a26a02015-03-05 12:35:04 -0800594 struct device_node *np = dev->of_node;
Andrew Lunn1e72e6f2015-08-17 23:52:50 +0200595 struct device_node *child, *mdio, *ethernet, *port;
Andrew Lunn6bc6d0a2015-08-08 17:09:14 +0200596 struct mii_bus *mdio_bus, *mdio_bus_switch;
Florian Fainelli769a0202015-03-09 14:31:21 -0700597 struct net_device *ethernet_dev;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000598 struct dsa_platform_data *pd;
599 struct dsa_chip_data *cd;
600 const char *port_name;
601 int chip_index, port_index;
602 const unsigned int *sw_addr, *port_reg;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700603 u32 eeprom_len;
Florian Fainelli21168242013-03-25 05:03:39 +0000604 int ret;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000605
606 mdio = of_parse_phandle(np, "dsa,mii-bus", 0);
607 if (!mdio)
608 return -EINVAL;
609
610 mdio_bus = of_mdio_find_bus(mdio);
611 if (!mdio_bus)
Florian Fainellib324c072015-03-05 12:35:05 -0800612 return -EPROBE_DEFER;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000613
614 ethernet = of_parse_phandle(np, "dsa,ethernet", 0);
Russell Kinge496ae62015-09-24 20:35:57 +0100615 if (!ethernet) {
616 ret = -EINVAL;
617 goto out_put_mdio;
618 }
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000619
Florian Fainelli769a0202015-03-09 14:31:21 -0700620 ethernet_dev = of_find_net_device_by_node(ethernet);
Russell Kinge496ae62015-09-24 20:35:57 +0100621 if (!ethernet_dev) {
622 ret = -EPROBE_DEFER;
623 goto out_put_mdio;
624 }
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000625
626 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
Russell Kinge496ae62015-09-24 20:35:57 +0100627 if (!pd) {
628 ret = -ENOMEM;
Russell King9861f722015-09-24 20:36:33 +0100629 goto out_put_ethernet;
Russell Kinge496ae62015-09-24 20:35:57 +0100630 }
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000631
Florian Fainellif1a26a02015-03-05 12:35:04 -0800632 dev->platform_data = pd;
Florian Fainelli769a0202015-03-09 14:31:21 -0700633 pd->of_netdev = ethernet_dev;
Tobias Waldekranze04449f2015-02-05 14:54:09 +0100634 pd->nr_chips = of_get_available_child_count(np);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000635 if (pd->nr_chips > DSA_MAX_SWITCHES)
636 pd->nr_chips = DSA_MAX_SWITCHES;
637
Fabian Frederick6f2aed62014-11-14 19:38:23 +0100638 pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data),
639 GFP_KERNEL);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000640 if (!pd->chip) {
641 ret = -ENOMEM;
642 goto out_free;
643 }
644
Fabian Godehardtd1c0b472014-05-16 06:21:44 +0200645 chip_index = -1;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000646 for_each_available_child_of_node(np, child) {
Vivien Didelotd3902382016-07-06 20:03:54 -0400647 int i;
648
Fabian Godehardtd1c0b472014-05-16 06:21:44 +0200649 chip_index++;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000650 cd = &pd->chip[chip_index];
651
Florian Fainellifa981d92014-08-27 17:04:49 -0700652 cd->of_node = child;
Russell Kinge496ae62015-09-24 20:35:57 +0100653
Vivien Didelotd3902382016-07-06 20:03:54 -0400654 /* Initialize the routing table */
655 for (i = 0; i < DSA_MAX_SWITCHES; ++i)
656 cd->rtable[i] = DSA_RTABLE_NONE;
657
Russell Kinge496ae62015-09-24 20:35:57 +0100658 /* When assigning the host device, increment its refcount */
659 cd->host_dev = get_device(&mdio_bus->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000660
661 sw_addr = of_get_property(child, "reg", NULL);
662 if (!sw_addr)
663 continue;
664
665 cd->sw_addr = be32_to_cpup(sw_addr);
Florian Fainellic8cf89f2015-07-11 18:02:11 -0700666 if (cd->sw_addr >= PHY_MAX_ADDR)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000667 continue;
668
Guenter Roeck50d49642015-04-29 10:56:15 -0700669 if (!of_property_read_u32(child, "eeprom-length", &eeprom_len))
Guenter Roeck6793abb2014-10-29 10:45:01 -0700670 cd->eeprom_len = eeprom_len;
671
Andrew Lunn6bc6d0a2015-08-08 17:09:14 +0200672 mdio = of_parse_phandle(child, "mii-bus", 0);
673 if (mdio) {
674 mdio_bus_switch = of_mdio_find_bus(mdio);
675 if (!mdio_bus_switch) {
676 ret = -EPROBE_DEFER;
677 goto out_free_chip;
678 }
Russell Kinge496ae62015-09-24 20:35:57 +0100679
680 /* Drop the mdio_bus device ref, replacing the host
681 * device with the mdio_bus_switch device, keeping
682 * the refcount from of_mdio_find_bus() above.
683 */
684 put_device(cd->host_dev);
Andrew Lunn6bc6d0a2015-08-08 17:09:14 +0200685 cd->host_dev = &mdio_bus_switch->dev;
686 }
687
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000688 for_each_available_child_of_node(child, port) {
689 port_reg = of_get_property(port, "reg", NULL);
690 if (!port_reg)
691 continue;
692
693 port_index = be32_to_cpup(port_reg);
Florian Fainelli8f5063e2015-07-11 18:02:10 -0700694 if (port_index >= DSA_MAX_PORTS)
695 break;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000696
697 port_name = of_get_property(port, "label", NULL);
698 if (!port_name)
699 continue;
700
Florian Fainellibd474972014-08-27 17:04:50 -0700701 cd->port_dn[port_index] = port;
702
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000703 cd->port_names[port_index] = kstrdup(port_name,
704 GFP_KERNEL);
705 if (!cd->port_names[port_index]) {
706 ret = -ENOMEM;
707 goto out_free_chip;
708 }
709
Andrew Lunn1e72e6f2015-08-17 23:52:50 +0200710 ret = dsa_of_probe_links(pd, cd, chip_index,
711 port_index, port, port_name);
712 if (ret)
713 goto out_free_chip;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000714
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000715 }
716 }
717
Russell Kinge496ae62015-09-24 20:35:57 +0100718 /* The individual chips hold their own refcount on the mdio bus,
719 * so drop ours */
720 put_device(&mdio_bus->dev);
721
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000722 return 0;
723
724out_free_chip:
Florian Fainelli21168242013-03-25 05:03:39 +0000725 dsa_of_free_platform_data(pd);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000726out_free:
727 kfree(pd);
Florian Fainellif1a26a02015-03-05 12:35:04 -0800728 dev->platform_data = NULL;
Russell King9861f722015-09-24 20:36:33 +0100729out_put_ethernet:
730 put_device(&ethernet_dev->dev);
Russell Kinge496ae62015-09-24 20:35:57 +0100731out_put_mdio:
732 put_device(&mdio_bus->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000733 return ret;
734}
735
Florian Fainellif1a26a02015-03-05 12:35:04 -0800736static void dsa_of_remove(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000737{
Florian Fainellif1a26a02015-03-05 12:35:04 -0800738 struct dsa_platform_data *pd = dev->platform_data;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000739
Florian Fainellif1a26a02015-03-05 12:35:04 -0800740 if (!dev->of_node)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000741 return;
742
Florian Fainelli21168242013-03-25 05:03:39 +0000743 dsa_of_free_platform_data(pd);
Russell King9861f722015-09-24 20:36:33 +0100744 put_device(&pd->of_netdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000745 kfree(pd);
746}
747#else
Florian Fainellif1a26a02015-03-05 12:35:04 -0800748static inline int dsa_of_probe(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000749{
750 return 0;
751}
752
Florian Fainellif1a26a02015-03-05 12:35:04 -0800753static inline void dsa_of_remove(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000754{
755}
756#endif
757
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100758static int dsa_setup_dst(struct dsa_switch_tree *dst, struct net_device *dev,
759 struct device *parent, struct dsa_platform_data *pd)
Florian Fainellic86e59b2015-03-05 12:35:08 -0800760{
761 int i;
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100762 unsigned configured = 0;
Florian Fainellic86e59b2015-03-05 12:35:08 -0800763
764 dst->pd = pd;
765 dst->master_netdev = dev;
Florian Fainellic86e59b2015-03-05 12:35:08 -0800766 dst->cpu_port = -1;
767
768 for (i = 0; i < pd->nr_chips; i++) {
769 struct dsa_switch *ds;
770
771 ds = dsa_switch_setup(dst, i, parent, pd->chip[i].host_dev);
772 if (IS_ERR(ds)) {
773 netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n",
774 i, PTR_ERR(ds));
775 continue;
776 }
777
778 dst->ds[i] = ds;
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100779
780 ++configured;
Florian Fainellic86e59b2015-03-05 12:35:08 -0800781 }
782
783 /*
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100784 * If no switch was found, exit cleanly
785 */
786 if (!configured)
787 return -EPROBE_DEFER;
788
789 /*
Florian Fainellic86e59b2015-03-05 12:35:08 -0800790 * If we use a tagging format that doesn't have an ethertype
791 * field, make sure that all packets from this point on get
792 * sent to the tag format's receive function.
793 */
794 wmb();
795 dev->dsa_ptr = (void *)dst;
796
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100797 return 0;
Florian Fainellic86e59b2015-03-05 12:35:08 -0800798}
799
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000800static int dsa_probe(struct platform_device *pdev)
801{
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000802 struct dsa_platform_data *pd = pdev->dev.platform_data;
803 struct net_device *dev;
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000804 struct dsa_switch_tree *dst;
Florian Fainellic86e59b2015-03-05 12:35:08 -0800805 int ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000806
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000807 if (pdev->dev.of_node) {
Florian Fainellif1a26a02015-03-05 12:35:04 -0800808 ret = dsa_of_probe(&pdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000809 if (ret)
810 return ret;
811
812 pd = pdev->dev.platform_data;
813 }
814
Florian Fainelli769a0202015-03-09 14:31:21 -0700815 if (pd == NULL || (pd->netdev == NULL && pd->of_netdev == NULL))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000816 return -EINVAL;
817
Florian Fainelli769a0202015-03-09 14:31:21 -0700818 if (pd->of_netdev) {
819 dev = pd->of_netdev;
820 dev_hold(dev);
821 } else {
Florian Fainelli14b89f32017-02-04 13:02:42 -0800822 dev = dsa_dev_to_net_device(pd->netdev);
Florian Fainelli769a0202015-03-09 14:31:21 -0700823 }
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000824 if (dev == NULL) {
Florian Fainellib324c072015-03-05 12:35:05 -0800825 ret = -EPROBE_DEFER;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000826 goto out;
827 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000828
829 if (dev->dsa_ptr != NULL) {
830 dev_put(dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000831 ret = -EEXIST;
832 goto out;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000833 }
834
Neil Armstrongd4ac35d2015-10-06 15:40:37 +0100835 dst = devm_kzalloc(&pdev->dev, sizeof(*dst), GFP_KERNEL);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000836 if (dst == NULL) {
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000837 dev_put(dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000838 ret = -ENOMEM;
839 goto out;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000840 }
841
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000842 platform_set_drvdata(pdev, dst);
843
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100844 ret = dsa_setup_dst(dst, dev, &pdev->dev, pd);
Neil Armstrong679fb462015-12-07 13:57:34 +0100845 if (ret) {
846 dev_put(dev);
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100847 goto out;
Neil Armstrong679fb462015-12-07 13:57:34 +0100848 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000849
850 return 0;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000851
852out:
Florian Fainellif1a26a02015-03-05 12:35:04 -0800853 dsa_of_remove(&pdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000854
855 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000856}
857
Florian Fainellic86e59b2015-03-05 12:35:08 -0800858static void dsa_remove_dst(struct dsa_switch_tree *dst)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000859{
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000860 int i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000861
Neil Armstrong04761892016-03-08 10:36:20 +0100862 dst->master_netdev->dsa_ptr = NULL;
863
864 /* If we used a tagging format that doesn't have an ethertype
865 * field, make sure that all packets from this point get sent
866 * without the tag and go through the regular receive path.
867 */
868 wmb();
869
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000870 for (i = 0; i < dst->pd->nr_chips; i++) {
871 struct dsa_switch *ds = dst->ds[i];
872
Neil Armstrongd4ac35d2015-10-06 15:40:37 +0100873 if (ds)
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000874 dsa_switch_destroy(ds);
875 }
Neil Armstrong679fb462015-12-07 13:57:34 +0100876
Vivien Didelot9520ed82017-01-17 20:41:39 -0500877 dsa_cpu_port_ethtool_restore(dst->cpu_switch);
Florian Fainelli0c73c522016-06-07 16:32:42 -0700878
Neil Armstrong679fb462015-12-07 13:57:34 +0100879 dev_put(dst->master_netdev);
Florian Fainellic86e59b2015-03-05 12:35:08 -0800880}
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000881
Florian Fainellic86e59b2015-03-05 12:35:08 -0800882static int dsa_remove(struct platform_device *pdev)
883{
884 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
885
886 dsa_remove_dst(dst);
Florian Fainellif1a26a02015-03-05 12:35:04 -0800887 dsa_of_remove(&pdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000888
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000889 return 0;
890}
891
892static void dsa_shutdown(struct platform_device *pdev)
893{
894}
895
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700896static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
897 struct packet_type *pt, struct net_device *orig_dev)
898{
899 struct dsa_switch_tree *dst = dev->dsa_ptr;
900
901 if (unlikely(dst == NULL)) {
902 kfree_skb(skb);
903 return 0;
904 }
905
Alexander Duyck50753142014-09-15 13:00:19 -0400906 return dst->rcv(skb, dev, pt, orig_dev);
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700907}
908
Florian Fainelli61b73632014-08-29 12:42:07 -0700909static struct packet_type dsa_pack_type __read_mostly = {
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700910 .type = cpu_to_be16(ETH_P_XDSA),
911 .func = dsa_switch_rcv,
912};
913
Florian Fainelli24462542014-09-18 17:31:22 -0700914#ifdef CONFIG_PM_SLEEP
915static int dsa_suspend(struct device *d)
916{
917 struct platform_device *pdev = to_platform_device(d);
918 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
919 int i, ret = 0;
920
921 for (i = 0; i < dst->pd->nr_chips; i++) {
922 struct dsa_switch *ds = dst->ds[i];
923
924 if (ds != NULL)
925 ret = dsa_switch_suspend(ds);
926 }
927
928 return ret;
929}
930
931static int dsa_resume(struct device *d)
932{
933 struct platform_device *pdev = to_platform_device(d);
934 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
935 int i, ret = 0;
936
937 for (i = 0; i < dst->pd->nr_chips; i++) {
938 struct dsa_switch *ds = dst->ds[i];
939
940 if (ds != NULL)
941 ret = dsa_switch_resume(ds);
942 }
943
944 return ret;
945}
946#endif
947
948static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume);
949
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000950static const struct of_device_id dsa_of_match_table[] = {
951 { .compatible = "marvell,dsa", },
952 {}
953};
954MODULE_DEVICE_TABLE(of, dsa_of_match_table);
955
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000956static struct platform_driver dsa_driver = {
957 .probe = dsa_probe,
958 .remove = dsa_remove,
959 .shutdown = dsa_shutdown,
960 .driver = {
961 .name = "dsa",
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000962 .of_match_table = dsa_of_match_table,
Florian Fainelli24462542014-09-18 17:31:22 -0700963 .pm = &dsa_pm_ops,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000964 },
965};
966
967static int __init dsa_init_module(void)
968{
Ben Hutchings7df899c2011-11-25 14:35:02 +0000969 int rc;
970
Vivien Didelot88e4f0c2017-02-03 13:20:16 -0500971 rc = dsa_slave_register_notifier();
972 if (rc)
973 return rc;
Florian Fainellib73adef2015-02-24 13:15:33 -0800974
Ben Hutchings7df899c2011-11-25 14:35:02 +0000975 rc = platform_driver_register(&dsa_driver);
976 if (rc)
977 return rc;
978
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700979 dev_add_pack(&dsa_pack_type);
980
Ben Hutchings7df899c2011-11-25 14:35:02 +0000981 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000982}
983module_init(dsa_init_module);
984
985static void __exit dsa_cleanup_module(void)
986{
Vivien Didelot88e4f0c2017-02-03 13:20:16 -0500987 dsa_slave_unregister_notifier();
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700988 dev_remove_pack(&dsa_pack_type);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000989 platform_driver_unregister(&dsa_driver);
990}
991module_exit(dsa_cleanup_module);
992
Rusty Russell577d6a72011-01-24 14:32:52 -0600993MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000994MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
995MODULE_LICENSE("GPL");
996MODULE_ALIAS("platform:dsa");