blob: 91f96e1bd2ecec1db34ce9d8b56661213eb567ce [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
Florian Fainelliab3d4082017-01-08 14:52:07 -080063void register_switch_driver(struct dsa_switch_driver *drv)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000064{
65 mutex_lock(&dsa_switch_drivers_mutex);
Florian Fainelliab3d4082017-01-08 14:52:07 -080066 list_add_tail(&drv->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
Florian Fainelliab3d4082017-01-08 14:52:07 -080071void unregister_switch_driver(struct dsa_switch_driver *drv)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000072{
73 mutex_lock(&dsa_switch_drivers_mutex);
Florian Fainelliab3d4082017-01-08 14:52:07 -080074 list_del_init(&drv->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
Florian Fainellia82f67a2017-01-08 14:52:08 -080079static const 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{
Florian Fainellia82f67a2017-01-08 14:52:08 -080083 const 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) {
Florian Fainellia82f67a2017-01-08 14:52:08 -080092 const struct dsa_switch_ops *ops;
Florian Fainelliab3d4082017-01-08 14:52:07 -080093 struct dsa_switch_driver *drv;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000094
Florian Fainelliab3d4082017-01-08 14:52:07 -080095 drv = list_entry(list, struct dsa_switch_driver, list);
96 ops = drv->ops;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000097
Vivien Didelot9d490b42016-08-23 12:38:56 -040098 name = ops->probe(parent, host_dev, sw_addr, priv);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000099 if (name != NULL) {
Vivien Didelot9d490b42016-08-23 12:38:56 -0400100 ret = ops;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000101 break;
102 }
103 }
104 mutex_unlock(&dsa_switch_drivers_mutex);
105
106 *_name = name;
107
108 return ret;
109}
110
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000111/* basic switch operations **************************************************/
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200112int dsa_cpu_dsa_setup(struct dsa_switch *ds, struct device *dev,
113 struct device_node *port_dn, int port)
114{
115 struct phy_device *phydev;
116 int ret, mode;
117
118 if (of_phy_is_fixed_link(port_dn)) {
119 ret = of_phy_register_fixed_link(port_dn);
120 if (ret) {
121 dev_err(dev, "failed to register fixed PHY\n");
122 return ret;
123 }
124 phydev = of_phy_find_device(port_dn);
125
126 mode = of_get_phy_mode(port_dn);
127 if (mode < 0)
128 mode = PHY_INTERFACE_MODE_NA;
129 phydev->interface = mode;
130
131 genphy_config_init(phydev);
132 genphy_read_status(phydev);
Vivien Didelot9d490b42016-08-23 12:38:56 -0400133 if (ds->ops->adjust_link)
134 ds->ops->adjust_link(ds, port, phydev);
Johan Hovoldfd05d7b2016-11-24 19:21:27 +0100135
136 put_device(&phydev->mdio.dev);
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200137 }
138
139 return 0;
140}
141
142static int dsa_cpu_dsa_setups(struct dsa_switch *ds, struct device *dev)
Andrew Lunn39b0c702015-08-31 15:56:49 +0200143{
Andrew Lunn39b0c702015-08-31 15:56:49 +0200144 struct device_node *port_dn;
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200145 int ret, port;
Andrew Lunn39b0c702015-08-31 15:56:49 +0200146
147 for (port = 0; port < DSA_MAX_PORTS; port++) {
148 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
149 continue;
150
Andrew Lunn189b0d92016-06-04 21:16:58 +0200151 port_dn = ds->ports[port].dn;
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200152 ret = dsa_cpu_dsa_setup(ds, dev, port_dn, port);
153 if (ret)
154 return ret;
Andrew Lunn39b0c702015-08-31 15:56:49 +0200155 }
156 return 0;
157}
158
Andrew Lunn39a7f2a2016-06-04 21:17:03 +0200159const struct dsa_device_ops *dsa_resolve_tag_protocol(int tag_protocol)
160{
161 const struct dsa_device_ops *ops;
162
163 if (tag_protocol >= DSA_TAG_LAST)
164 return ERR_PTR(-EINVAL);
165 ops = dsa_device_ops[tag_protocol];
166
167 if (!ops)
168 return ERR_PTR(-ENOPROTOOPT);
169
170 return ops;
171}
172
Florian Fainelli0c73c522016-06-07 16:32:42 -0700173int dsa_cpu_port_ethtool_setup(struct dsa_switch *ds)
174{
175 struct net_device *master;
176 struct ethtool_ops *cpu_ops;
177
178 master = ds->dst->master_netdev;
179 if (ds->master_netdev)
180 master = ds->master_netdev;
181
182 cpu_ops = devm_kzalloc(ds->dev, sizeof(*cpu_ops), GFP_KERNEL);
183 if (!cpu_ops)
184 return -ENOMEM;
185
186 memcpy(&ds->dst->master_ethtool_ops, master->ethtool_ops,
187 sizeof(struct ethtool_ops));
188 ds->dst->master_orig_ethtool_ops = master->ethtool_ops;
189 memcpy(cpu_ops, &ds->dst->master_ethtool_ops,
190 sizeof(struct ethtool_ops));
191 dsa_cpu_port_ethtool_init(cpu_ops);
192 master->ethtool_ops = cpu_ops;
193
194 return 0;
195}
196
197void dsa_cpu_port_ethtool_restore(struct dsa_switch *ds)
198{
199 struct net_device *master;
200
201 master = ds->dst->master_netdev;
202 if (ds->master_netdev)
203 master = ds->master_netdev;
204
205 master->ethtool_ops = ds->dst->master_orig_ethtool_ops;
206}
207
Florian Fainellidf197192015-03-05 12:35:06 -0800208static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000209{
Florian Fainellia82f67a2017-01-08 14:52:08 -0800210 const struct dsa_switch_ops *ops = ds->ops;
Florian Fainellidf197192015-03-05 12:35:06 -0800211 struct dsa_switch_tree *dst = ds->dst;
Andrew Lunnff049552016-05-10 23:27:24 +0200212 struct dsa_chip_data *cd = ds->cd;
Florian Fainellif9bf5a22013-01-21 09:58:51 +0000213 bool valid_name_found = false;
Florian Fainellidf197192015-03-05 12:35:06 -0800214 int index = ds->index;
215 int i, ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000216
217 /*
218 * Validate supplied switch configuration.
219 */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000220 for (i = 0; i < DSA_MAX_PORTS; i++) {
221 char *name;
222
Andrew Lunnff049552016-05-10 23:27:24 +0200223 name = cd->port_names[i];
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000224 if (name == NULL)
225 continue;
226
227 if (!strcmp(name, "cpu")) {
Vivien Didelotb22de492017-01-17 20:41:38 -0500228 if (!dst->cpu_switch) {
Joe Perchesa2ae6002014-11-09 16:32:46 -0800229 netdev_err(dst->master_netdev,
230 "multiple cpu ports?!\n");
Vivien Didelota896eee2017-01-03 14:31:49 -0500231 return -EINVAL;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000232 }
Vivien Didelotb22de492017-01-17 20:41:38 -0500233 dst->cpu_switch = ds;
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000234 dst->cpu_port = i;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200235 ds->cpu_port_mask |= 1 << i;
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000236 } else if (!strcmp(name, "dsa")) {
237 ds->dsa_port_mask |= 1 << i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000238 } else {
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200239 ds->enabled_port_mask |= 1 << i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000240 }
Florian Fainellif9bf5a22013-01-21 09:58:51 +0000241 valid_name_found = true;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000242 }
243
Vivien Didelota896eee2017-01-03 14:31:49 -0500244 if (!valid_name_found && i == DSA_MAX_PORTS)
245 return -EINVAL;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000246
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700247 /* Make the built-in MII bus mask match the number of ports,
248 * switch drivers can override this later
249 */
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200250 ds->phys_mii_mask = ds->enabled_port_mask;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700251
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000252 /*
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000253 * If the CPU connects to this switch, set the switch tree
254 * tagging protocol to the preferred tagging format of this
255 * switch.
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000256 */
Vivien Didelotb22de492017-01-17 20:41:38 -0500257 if (dst->cpu_switch == ds) {
Andrew Lunn7b314362016-08-22 16:01:01 +0200258 enum dsa_tag_protocol tag_protocol;
259
Vivien Didelot9d490b42016-08-23 12:38:56 -0400260 tag_protocol = ops->get_tag_protocol(ds);
Andrew Lunn7b314362016-08-22 16:01:01 +0200261 dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol);
Vivien Didelota896eee2017-01-03 14:31:49 -0500262 if (IS_ERR(dst->tag_ops))
263 return PTR_ERR(dst->tag_ops);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000264
Andrew Lunn39a7f2a2016-06-04 21:17:03 +0200265 dst->rcv = dst->tag_ops->rcv;
Alexander Duyck50753142014-09-15 13:00:19 -0400266 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000267
Andrew Lunn66472fc2016-06-04 21:17:00 +0200268 memcpy(ds->rtable, cd->rtable, sizeof(ds->rtable));
269
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000270 /*
271 * Do basic register setup.
272 */
Vivien Didelot9d490b42016-08-23 12:38:56 -0400273 ret = ops->setup(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000274 if (ret < 0)
Vivien Didelota896eee2017-01-03 14:31:49 -0500275 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000276
John Crispin092183d2016-09-19 15:28:01 +0200277 if (ops->set_addr) {
278 ret = ops->set_addr(ds, dst->master_netdev->dev_addr);
279 if (ret < 0)
Vivien Didelota896eee2017-01-03 14:31:49 -0500280 return ret;
John Crispin092183d2016-09-19 15:28:01 +0200281 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000282
Vivien Didelot9d490b42016-08-23 12:38:56 -0400283 if (!ds->slave_mii_bus && ops->phy_read) {
Andrew Lunne755e492016-06-04 21:17:04 +0200284 ds->slave_mii_bus = devm_mdiobus_alloc(parent);
Vivien Didelota896eee2017-01-03 14:31:49 -0500285 if (!ds->slave_mii_bus)
286 return -ENOMEM;
Andrew Lunne755e492016-06-04 21:17:04 +0200287 dsa_slave_mii_bus_init(ds);
288
289 ret = mdiobus_register(ds->slave_mii_bus);
290 if (ret < 0)
Vivien Didelota896eee2017-01-03 14:31:49 -0500291 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000292 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000293
294 /*
295 * Create network devices for physical switch ports.
296 */
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000297 for (i = 0; i < DSA_MAX_PORTS; i++) {
Andrew Lunn189b0d92016-06-04 21:16:58 +0200298 ds->ports[i].dn = cd->port_dn[i];
299
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200300 if (!(ds->enabled_port_mask & (1 << i)))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000301 continue;
302
Andrew Lunnff049552016-05-10 23:27:24 +0200303 ret = dsa_slave_create(ds, parent, i, cd->port_names[i]);
Vivien Didelota896eee2017-01-03 14:31:49 -0500304 if (ret < 0)
Russell Kingd25b8e72015-10-03 18:09:07 +0100305 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 +0200306 index, i, cd->port_names[i], ret);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000307 }
308
Andrew Lunn39b0c702015-08-31 15:56:49 +0200309 /* Perform configuration of the CPU and DSA ports */
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200310 ret = dsa_cpu_dsa_setups(ds, parent);
Vivien Didelota896eee2017-01-03 14:31:49 -0500311 if (ret < 0)
Andrew Lunn39b0c702015-08-31 15:56:49 +0200312 netdev_err(dst->master_netdev, "[%d] : can't configure CPU and DSA ports\n",
313 index);
Andrew Lunn39b0c702015-08-31 15:56:49 +0200314
Florian Fainelli0c73c522016-06-07 16:32:42 -0700315 ret = dsa_cpu_port_ethtool_setup(ds);
316 if (ret)
317 return ret;
318
Vivien Didelot111427f2017-01-06 16:42:00 -0500319 dsa_hwmon_register(ds);
Guenter Roeck51579c32014-10-29 10:44:58 -0700320
Vivien Didelota896eee2017-01-03 14:31:49 -0500321 return 0;
Florian Fainellidf197192015-03-05 12:35:06 -0800322}
323
324static struct dsa_switch *
325dsa_switch_setup(struct dsa_switch_tree *dst, int index,
326 struct device *parent, struct device *host_dev)
327{
Andrew Lunnff049552016-05-10 23:27:24 +0200328 struct dsa_chip_data *cd = dst->pd->chip + index;
Florian Fainellia82f67a2017-01-08 14:52:08 -0800329 const struct dsa_switch_ops *ops;
Florian Fainellidf197192015-03-05 12:35:06 -0800330 struct dsa_switch *ds;
331 int ret;
Vivien Didelot0209d142016-04-17 13:23:55 -0400332 const char *name;
Andrew Lunn7543a6d2016-04-13 02:40:40 +0200333 void *priv;
Florian Fainellidf197192015-03-05 12:35:06 -0800334
335 /*
336 * Probe for switch model.
337 */
Vivien Didelot9d490b42016-08-23 12:38:56 -0400338 ops = dsa_switch_probe(parent, host_dev, cd->sw_addr, &name, &priv);
339 if (!ops) {
Florian Fainellidf197192015-03-05 12:35:06 -0800340 netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n",
341 index);
342 return ERR_PTR(-EINVAL);
343 }
344 netdev_info(dst->master_netdev, "[%d]: detected a %s switch\n",
345 index, name);
346
347
348 /*
349 * Allocate and initialise switch state.
350 */
Andrew Lunn5feebd02016-04-13 02:40:41 +0200351 ds = devm_kzalloc(parent, sizeof(*ds), GFP_KERNEL);
Florian Fainellidf197192015-03-05 12:35:06 -0800352 if (ds == NULL)
Florian Fainelli24595342015-05-29 10:29:46 -0700353 return ERR_PTR(-ENOMEM);
Florian Fainellidf197192015-03-05 12:35:06 -0800354
355 ds->dst = dst;
356 ds->index = index;
Andrew Lunnff049552016-05-10 23:27:24 +0200357 ds->cd = cd;
Vivien Didelot9d490b42016-08-23 12:38:56 -0400358 ds->ops = ops;
Andrew Lunn7543a6d2016-04-13 02:40:40 +0200359 ds->priv = priv;
Andrew Lunnc33063d2016-05-10 23:27:23 +0200360 ds->dev = parent;
Florian Fainellidf197192015-03-05 12:35:06 -0800361
362 ret = dsa_switch_setup_one(ds, parent);
363 if (ret)
Florian Fainelli24595342015-05-29 10:29:46 -0700364 return ERR_PTR(ret);
Florian Fainellidf197192015-03-05 12:35:06 -0800365
366 return ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000367}
368
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200369void dsa_cpu_dsa_destroy(struct device_node *port_dn)
370{
Johan Hovold3f650472016-11-28 19:24:55 +0100371 if (of_phy_is_fixed_link(port_dn))
372 of_phy_deregister_fixed_link(port_dn);
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200373}
374
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000375static void dsa_switch_destroy(struct dsa_switch *ds)
376{
Neil Armstrongcbc5d902015-10-06 15:40:32 +0100377 int port;
378
Vivien Didelot111427f2017-01-06 16:42:00 -0500379 dsa_hwmon_unregister(ds);
Neil Armstrongcbc5d902015-10-06 15:40:32 +0100380
Andrew Lunn3a445142016-03-12 00:01:38 +0100381 /* Destroy network devices for physical switch ports. */
382 for (port = 0; port < DSA_MAX_PORTS; port++) {
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200383 if (!(ds->enabled_port_mask & (1 << port)))
Andrew Lunn3a445142016-03-12 00:01:38 +0100384 continue;
385
Andrew Lunnc8b09802016-06-04 21:16:57 +0200386 if (!ds->ports[port].netdev)
Andrew Lunn3a445142016-03-12 00:01:38 +0100387 continue;
388
Andrew Lunnc8b09802016-06-04 21:16:57 +0200389 dsa_slave_destroy(ds->ports[port].netdev);
Andrew Lunn3a445142016-03-12 00:01:38 +0100390 }
391
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200392 /* Disable configuration of the CPU and DSA ports */
Neil Armstrongcbc5d902015-10-06 15:40:32 +0100393 for (port = 0; port < DSA_MAX_PORTS; port++) {
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200394 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
395 continue;
396 dsa_cpu_dsa_destroy(ds->ports[port].dn);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200397
398 /* Clearing a bit which is not set does no harm */
399 ds->cpu_port_mask |= ~(1 << port);
400 ds->dsa_port_mask |= ~(1 << port);
Neil Armstrongcbc5d902015-10-06 15:40:32 +0100401 }
402
Vivien Didelot9d490b42016-08-23 12:38:56 -0400403 if (ds->slave_mii_bus && ds->ops->phy_read)
Andrew Lunne755e492016-06-04 21:17:04 +0200404 mdiobus_unregister(ds->slave_mii_bus);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000405}
406
Thierry Redinge506d402014-10-01 13:59:00 +0200407#ifdef CONFIG_PM_SLEEP
Florian Fainelliea825e72016-08-18 15:30:12 -0700408int dsa_switch_suspend(struct dsa_switch *ds)
Florian Fainelli24462542014-09-18 17:31:22 -0700409{
410 int i, ret = 0;
411
412 /* Suspend slave network devices */
413 for (i = 0; i < DSA_MAX_PORTS; i++) {
Guenter Roeckd79d2102015-02-24 23:02:02 -0800414 if (!dsa_is_port_initialized(ds, i))
Florian Fainelli24462542014-09-18 17:31:22 -0700415 continue;
416
Andrew Lunnc8b09802016-06-04 21:16:57 +0200417 ret = dsa_slave_suspend(ds->ports[i].netdev);
Florian Fainelli24462542014-09-18 17:31:22 -0700418 if (ret)
419 return ret;
420 }
421
Vivien Didelot9d490b42016-08-23 12:38:56 -0400422 if (ds->ops->suspend)
423 ret = ds->ops->suspend(ds);
Florian Fainelli24462542014-09-18 17:31:22 -0700424
425 return ret;
426}
Florian Fainelliea825e72016-08-18 15:30:12 -0700427EXPORT_SYMBOL_GPL(dsa_switch_suspend);
Florian Fainelli24462542014-09-18 17:31:22 -0700428
Florian Fainelliea825e72016-08-18 15:30:12 -0700429int dsa_switch_resume(struct dsa_switch *ds)
Florian Fainelli24462542014-09-18 17:31:22 -0700430{
431 int i, ret = 0;
432
Vivien Didelot9d490b42016-08-23 12:38:56 -0400433 if (ds->ops->resume)
434 ret = ds->ops->resume(ds);
Florian Fainelli24462542014-09-18 17:31:22 -0700435
436 if (ret)
437 return ret;
438
439 /* Resume slave network devices */
440 for (i = 0; i < DSA_MAX_PORTS; i++) {
Guenter Roeckd79d2102015-02-24 23:02:02 -0800441 if (!dsa_is_port_initialized(ds, i))
Florian Fainelli24462542014-09-18 17:31:22 -0700442 continue;
443
Andrew Lunnc8b09802016-06-04 21:16:57 +0200444 ret = dsa_slave_resume(ds->ports[i].netdev);
Florian Fainelli24462542014-09-18 17:31:22 -0700445 if (ret)
446 return ret;
447 }
448
449 return 0;
450}
Florian Fainelliea825e72016-08-18 15:30:12 -0700451EXPORT_SYMBOL_GPL(dsa_switch_resume);
Thierry Redinge506d402014-10-01 13:59:00 +0200452#endif
Florian Fainelli24462542014-09-18 17:31:22 -0700453
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000454/* platform driver init and cleanup *****************************************/
455static int dev_is_class(struct device *dev, void *class)
456{
457 if (dev->class != NULL && !strcmp(dev->class->name, class))
458 return 1;
459
460 return 0;
461}
462
463static struct device *dev_find_class(struct device *parent, char *class)
464{
465 if (dev_is_class(parent, class)) {
466 get_device(parent);
467 return parent;
468 }
469
470 return device_find_child(parent, class, dev_is_class);
471}
472
Alexander Duyckb4d23942014-09-15 13:00:27 -0400473struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000474{
475 struct device *d;
476
477 d = dev_find_class(dev, "mdio_bus");
478 if (d != NULL) {
479 struct mii_bus *bus;
480
481 bus = to_mii_bus(d);
482 put_device(d);
483
484 return bus;
485 }
486
487 return NULL;
488}
Alexander Duyckb4d23942014-09-15 13:00:27 -0400489EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000490
491static struct net_device *dev_to_net_device(struct device *dev)
492{
493 struct device *d;
494
495 d = dev_find_class(dev, "net");
496 if (d != NULL) {
497 struct net_device *nd;
498
499 nd = to_net_dev(d);
500 dev_hold(nd);
501 put_device(d);
502
503 return nd;
504 }
505
506 return NULL;
507}
508
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000509#ifdef CONFIG_OF
510static int dsa_of_setup_routing_table(struct dsa_platform_data *pd,
511 struct dsa_chip_data *cd,
Pavel Nakonechny30303812015-04-05 00:46:21 +0300512 int chip_index, int port_index,
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000513 struct device_node *link)
514{
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000515 const __be32 *reg;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000516 int link_sw_addr;
517 struct device_node *parent_sw;
518 int len;
519
520 parent_sw = of_get_parent(link);
521 if (!parent_sw)
522 return -EINVAL;
523
524 reg = of_get_property(parent_sw, "reg", &len);
525 if (!reg || (len != sizeof(*reg) * 2))
526 return -EINVAL;
527
Pavel Nakonechny30303812015-04-05 00:46:21 +0300528 /*
529 * Get the destination switch number from the second field of its 'reg'
530 * property, i.e. for "reg = <0x19 1>" sw_addr is '1'.
531 */
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000532 link_sw_addr = be32_to_cpup(reg + 1);
533
534 if (link_sw_addr >= pd->nr_chips)
535 return -EINVAL;
536
Pavel Nakonechny30303812015-04-05 00:46:21 +0300537 cd->rtable[link_sw_addr] = port_index;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000538
539 return 0;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000540}
541
Andrew Lunn1e72e6f2015-08-17 23:52:50 +0200542static int dsa_of_probe_links(struct dsa_platform_data *pd,
543 struct dsa_chip_data *cd,
544 int chip_index, int port_index,
545 struct device_node *port,
546 const char *port_name)
547{
548 struct device_node *link;
549 int link_index;
550 int ret;
551
552 for (link_index = 0;; link_index++) {
553 link = of_parse_phandle(port, "link", link_index);
554 if (!link)
555 break;
556
557 if (!strcmp(port_name, "dsa") && pd->nr_chips > 1) {
558 ret = dsa_of_setup_routing_table(pd, cd, chip_index,
559 port_index, link);
560 if (ret)
561 return ret;
562 }
563 }
564 return 0;
565}
566
Florian Fainelli21168242013-03-25 05:03:39 +0000567static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
568{
569 int i;
570 int port_index;
571
572 for (i = 0; i < pd->nr_chips; i++) {
573 port_index = 0;
Florian Fainelli5f64a7d2013-03-25 05:03:40 +0000574 while (port_index < DSA_MAX_PORTS) {
Fabian Frederick1f747142014-06-23 19:32:47 +0200575 kfree(pd->chip[i].port_names[port_index]);
Florian Fainelli5f64a7d2013-03-25 05:03:40 +0000576 port_index++;
577 }
Russell Kinge496ae62015-09-24 20:35:57 +0100578
579 /* Drop our reference to the MDIO bus device */
580 if (pd->chip[i].host_dev)
581 put_device(pd->chip[i].host_dev);
Florian Fainelli21168242013-03-25 05:03:39 +0000582 }
583 kfree(pd->chip);
584}
585
Florian Fainellif1a26a02015-03-05 12:35:04 -0800586static int dsa_of_probe(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000587{
Florian Fainellif1a26a02015-03-05 12:35:04 -0800588 struct device_node *np = dev->of_node;
Andrew Lunn1e72e6f2015-08-17 23:52:50 +0200589 struct device_node *child, *mdio, *ethernet, *port;
Andrew Lunn6bc6d0a2015-08-08 17:09:14 +0200590 struct mii_bus *mdio_bus, *mdio_bus_switch;
Florian Fainelli769a0202015-03-09 14:31:21 -0700591 struct net_device *ethernet_dev;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000592 struct dsa_platform_data *pd;
593 struct dsa_chip_data *cd;
594 const char *port_name;
595 int chip_index, port_index;
596 const unsigned int *sw_addr, *port_reg;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700597 u32 eeprom_len;
Florian Fainelli21168242013-03-25 05:03:39 +0000598 int ret;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000599
600 mdio = of_parse_phandle(np, "dsa,mii-bus", 0);
601 if (!mdio)
602 return -EINVAL;
603
604 mdio_bus = of_mdio_find_bus(mdio);
605 if (!mdio_bus)
Florian Fainellib324c072015-03-05 12:35:05 -0800606 return -EPROBE_DEFER;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000607
608 ethernet = of_parse_phandle(np, "dsa,ethernet", 0);
Russell Kinge496ae62015-09-24 20:35:57 +0100609 if (!ethernet) {
610 ret = -EINVAL;
611 goto out_put_mdio;
612 }
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000613
Florian Fainelli769a0202015-03-09 14:31:21 -0700614 ethernet_dev = of_find_net_device_by_node(ethernet);
Russell Kinge496ae62015-09-24 20:35:57 +0100615 if (!ethernet_dev) {
616 ret = -EPROBE_DEFER;
617 goto out_put_mdio;
618 }
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000619
620 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
Russell Kinge496ae62015-09-24 20:35:57 +0100621 if (!pd) {
622 ret = -ENOMEM;
Russell King9861f722015-09-24 20:36:33 +0100623 goto out_put_ethernet;
Russell Kinge496ae62015-09-24 20:35:57 +0100624 }
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000625
Florian Fainellif1a26a02015-03-05 12:35:04 -0800626 dev->platform_data = pd;
Florian Fainelli769a0202015-03-09 14:31:21 -0700627 pd->of_netdev = ethernet_dev;
Tobias Waldekranze04449f2015-02-05 14:54:09 +0100628 pd->nr_chips = of_get_available_child_count(np);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000629 if (pd->nr_chips > DSA_MAX_SWITCHES)
630 pd->nr_chips = DSA_MAX_SWITCHES;
631
Fabian Frederick6f2aed62014-11-14 19:38:23 +0100632 pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data),
633 GFP_KERNEL);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000634 if (!pd->chip) {
635 ret = -ENOMEM;
636 goto out_free;
637 }
638
Fabian Godehardtd1c0b472014-05-16 06:21:44 +0200639 chip_index = -1;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000640 for_each_available_child_of_node(np, child) {
Vivien Didelotd3902382016-07-06 20:03:54 -0400641 int i;
642
Fabian Godehardtd1c0b472014-05-16 06:21:44 +0200643 chip_index++;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000644 cd = &pd->chip[chip_index];
645
Florian Fainellifa981d92014-08-27 17:04:49 -0700646 cd->of_node = child;
Russell Kinge496ae62015-09-24 20:35:57 +0100647
Vivien Didelotd3902382016-07-06 20:03:54 -0400648 /* Initialize the routing table */
649 for (i = 0; i < DSA_MAX_SWITCHES; ++i)
650 cd->rtable[i] = DSA_RTABLE_NONE;
651
Russell Kinge496ae62015-09-24 20:35:57 +0100652 /* When assigning the host device, increment its refcount */
653 cd->host_dev = get_device(&mdio_bus->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000654
655 sw_addr = of_get_property(child, "reg", NULL);
656 if (!sw_addr)
657 continue;
658
659 cd->sw_addr = be32_to_cpup(sw_addr);
Florian Fainellic8cf89f2015-07-11 18:02:11 -0700660 if (cd->sw_addr >= PHY_MAX_ADDR)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000661 continue;
662
Guenter Roeck50d49642015-04-29 10:56:15 -0700663 if (!of_property_read_u32(child, "eeprom-length", &eeprom_len))
Guenter Roeck6793abb2014-10-29 10:45:01 -0700664 cd->eeprom_len = eeprom_len;
665
Andrew Lunn6bc6d0a2015-08-08 17:09:14 +0200666 mdio = of_parse_phandle(child, "mii-bus", 0);
667 if (mdio) {
668 mdio_bus_switch = of_mdio_find_bus(mdio);
669 if (!mdio_bus_switch) {
670 ret = -EPROBE_DEFER;
671 goto out_free_chip;
672 }
Russell Kinge496ae62015-09-24 20:35:57 +0100673
674 /* Drop the mdio_bus device ref, replacing the host
675 * device with the mdio_bus_switch device, keeping
676 * the refcount from of_mdio_find_bus() above.
677 */
678 put_device(cd->host_dev);
Andrew Lunn6bc6d0a2015-08-08 17:09:14 +0200679 cd->host_dev = &mdio_bus_switch->dev;
680 }
681
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000682 for_each_available_child_of_node(child, port) {
683 port_reg = of_get_property(port, "reg", NULL);
684 if (!port_reg)
685 continue;
686
687 port_index = be32_to_cpup(port_reg);
Florian Fainelli8f5063e2015-07-11 18:02:10 -0700688 if (port_index >= DSA_MAX_PORTS)
689 break;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000690
691 port_name = of_get_property(port, "label", NULL);
692 if (!port_name)
693 continue;
694
Florian Fainellibd474972014-08-27 17:04:50 -0700695 cd->port_dn[port_index] = port;
696
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000697 cd->port_names[port_index] = kstrdup(port_name,
698 GFP_KERNEL);
699 if (!cd->port_names[port_index]) {
700 ret = -ENOMEM;
701 goto out_free_chip;
702 }
703
Andrew Lunn1e72e6f2015-08-17 23:52:50 +0200704 ret = dsa_of_probe_links(pd, cd, chip_index,
705 port_index, port, port_name);
706 if (ret)
707 goto out_free_chip;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000708
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000709 }
710 }
711
Russell Kinge496ae62015-09-24 20:35:57 +0100712 /* The individual chips hold their own refcount on the mdio bus,
713 * so drop ours */
714 put_device(&mdio_bus->dev);
715
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000716 return 0;
717
718out_free_chip:
Florian Fainelli21168242013-03-25 05:03:39 +0000719 dsa_of_free_platform_data(pd);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000720out_free:
721 kfree(pd);
Florian Fainellif1a26a02015-03-05 12:35:04 -0800722 dev->platform_data = NULL;
Russell King9861f722015-09-24 20:36:33 +0100723out_put_ethernet:
724 put_device(&ethernet_dev->dev);
Russell Kinge496ae62015-09-24 20:35:57 +0100725out_put_mdio:
726 put_device(&mdio_bus->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000727 return ret;
728}
729
Florian Fainellif1a26a02015-03-05 12:35:04 -0800730static void dsa_of_remove(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000731{
Florian Fainellif1a26a02015-03-05 12:35:04 -0800732 struct dsa_platform_data *pd = dev->platform_data;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000733
Florian Fainellif1a26a02015-03-05 12:35:04 -0800734 if (!dev->of_node)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000735 return;
736
Florian Fainelli21168242013-03-25 05:03:39 +0000737 dsa_of_free_platform_data(pd);
Russell King9861f722015-09-24 20:36:33 +0100738 put_device(&pd->of_netdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000739 kfree(pd);
740}
741#else
Florian Fainellif1a26a02015-03-05 12:35:04 -0800742static inline int dsa_of_probe(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000743{
744 return 0;
745}
746
Florian Fainellif1a26a02015-03-05 12:35:04 -0800747static inline void dsa_of_remove(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000748{
749}
750#endif
751
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100752static int dsa_setup_dst(struct dsa_switch_tree *dst, struct net_device *dev,
753 struct device *parent, struct dsa_platform_data *pd)
Florian Fainellic86e59b2015-03-05 12:35:08 -0800754{
755 int i;
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100756 unsigned configured = 0;
Florian Fainellic86e59b2015-03-05 12:35:08 -0800757
758 dst->pd = pd;
759 dst->master_netdev = dev;
Florian Fainellic86e59b2015-03-05 12:35:08 -0800760 dst->cpu_port = -1;
761
762 for (i = 0; i < pd->nr_chips; i++) {
763 struct dsa_switch *ds;
764
765 ds = dsa_switch_setup(dst, i, parent, pd->chip[i].host_dev);
766 if (IS_ERR(ds)) {
767 netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n",
768 i, PTR_ERR(ds));
769 continue;
770 }
771
772 dst->ds[i] = ds;
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100773
774 ++configured;
Florian Fainellic86e59b2015-03-05 12:35:08 -0800775 }
776
777 /*
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100778 * If no switch was found, exit cleanly
779 */
780 if (!configured)
781 return -EPROBE_DEFER;
782
783 /*
Florian Fainellic86e59b2015-03-05 12:35:08 -0800784 * If we use a tagging format that doesn't have an ethertype
785 * field, make sure that all packets from this point on get
786 * sent to the tag format's receive function.
787 */
788 wmb();
789 dev->dsa_ptr = (void *)dst;
790
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100791 return 0;
Florian Fainellic86e59b2015-03-05 12:35:08 -0800792}
793
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000794static int dsa_probe(struct platform_device *pdev)
795{
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000796 struct dsa_platform_data *pd = pdev->dev.platform_data;
797 struct net_device *dev;
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000798 struct dsa_switch_tree *dst;
Florian Fainellic86e59b2015-03-05 12:35:08 -0800799 int ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000800
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000801 if (pdev->dev.of_node) {
Florian Fainellif1a26a02015-03-05 12:35:04 -0800802 ret = dsa_of_probe(&pdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000803 if (ret)
804 return ret;
805
806 pd = pdev->dev.platform_data;
807 }
808
Florian Fainelli769a0202015-03-09 14:31:21 -0700809 if (pd == NULL || (pd->netdev == NULL && pd->of_netdev == NULL))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000810 return -EINVAL;
811
Florian Fainelli769a0202015-03-09 14:31:21 -0700812 if (pd->of_netdev) {
813 dev = pd->of_netdev;
814 dev_hold(dev);
815 } else {
816 dev = dev_to_net_device(pd->netdev);
817 }
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000818 if (dev == NULL) {
Florian Fainellib324c072015-03-05 12:35:05 -0800819 ret = -EPROBE_DEFER;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000820 goto out;
821 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000822
823 if (dev->dsa_ptr != NULL) {
824 dev_put(dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000825 ret = -EEXIST;
826 goto out;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000827 }
828
Neil Armstrongd4ac35d2015-10-06 15:40:37 +0100829 dst = devm_kzalloc(&pdev->dev, sizeof(*dst), GFP_KERNEL);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000830 if (dst == NULL) {
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000831 dev_put(dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000832 ret = -ENOMEM;
833 goto out;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000834 }
835
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000836 platform_set_drvdata(pdev, dst);
837
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100838 ret = dsa_setup_dst(dst, dev, &pdev->dev, pd);
Neil Armstrong679fb462015-12-07 13:57:34 +0100839 if (ret) {
840 dev_put(dev);
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100841 goto out;
Neil Armstrong679fb462015-12-07 13:57:34 +0100842 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000843
844 return 0;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000845
846out:
Florian Fainellif1a26a02015-03-05 12:35:04 -0800847 dsa_of_remove(&pdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000848
849 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000850}
851
Florian Fainellic86e59b2015-03-05 12:35:08 -0800852static void dsa_remove_dst(struct dsa_switch_tree *dst)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000853{
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000854 int i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000855
Neil Armstrong04761892016-03-08 10:36:20 +0100856 dst->master_netdev->dsa_ptr = NULL;
857
858 /* If we used a tagging format that doesn't have an ethertype
859 * field, make sure that all packets from this point get sent
860 * without the tag and go through the regular receive path.
861 */
862 wmb();
863
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000864 for (i = 0; i < dst->pd->nr_chips; i++) {
865 struct dsa_switch *ds = dst->ds[i];
866
Neil Armstrongd4ac35d2015-10-06 15:40:37 +0100867 if (ds)
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000868 dsa_switch_destroy(ds);
869 }
Neil Armstrong679fb462015-12-07 13:57:34 +0100870
Vivien Didelot9520ed82017-01-17 20:41:39 -0500871 dsa_cpu_port_ethtool_restore(dst->cpu_switch);
Florian Fainelli0c73c522016-06-07 16:32:42 -0700872
Neil Armstrong679fb462015-12-07 13:57:34 +0100873 dev_put(dst->master_netdev);
Florian Fainellic86e59b2015-03-05 12:35:08 -0800874}
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000875
Florian Fainellic86e59b2015-03-05 12:35:08 -0800876static int dsa_remove(struct platform_device *pdev)
877{
878 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
879
880 dsa_remove_dst(dst);
Florian Fainellif1a26a02015-03-05 12:35:04 -0800881 dsa_of_remove(&pdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000882
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000883 return 0;
884}
885
886static void dsa_shutdown(struct platform_device *pdev)
887{
888}
889
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700890static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
891 struct packet_type *pt, struct net_device *orig_dev)
892{
893 struct dsa_switch_tree *dst = dev->dsa_ptr;
894
895 if (unlikely(dst == NULL)) {
896 kfree_skb(skb);
897 return 0;
898 }
899
Alexander Duyck50753142014-09-15 13:00:19 -0400900 return dst->rcv(skb, dev, pt, orig_dev);
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700901}
902
Florian Fainelli61b73632014-08-29 12:42:07 -0700903static struct packet_type dsa_pack_type __read_mostly = {
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700904 .type = cpu_to_be16(ETH_P_XDSA),
905 .func = dsa_switch_rcv,
906};
907
Florian Fainellib73adef2015-02-24 13:15:33 -0800908static struct notifier_block dsa_netdevice_nb __read_mostly = {
909 .notifier_call = dsa_slave_netdevice_event,
910};
911
Florian Fainelli24462542014-09-18 17:31:22 -0700912#ifdef CONFIG_PM_SLEEP
913static int dsa_suspend(struct device *d)
914{
915 struct platform_device *pdev = to_platform_device(d);
916 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
917 int i, ret = 0;
918
919 for (i = 0; i < dst->pd->nr_chips; i++) {
920 struct dsa_switch *ds = dst->ds[i];
921
922 if (ds != NULL)
923 ret = dsa_switch_suspend(ds);
924 }
925
926 return ret;
927}
928
929static int dsa_resume(struct device *d)
930{
931 struct platform_device *pdev = to_platform_device(d);
932 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
933 int i, ret = 0;
934
935 for (i = 0; i < dst->pd->nr_chips; i++) {
936 struct dsa_switch *ds = dst->ds[i];
937
938 if (ds != NULL)
939 ret = dsa_switch_resume(ds);
940 }
941
942 return ret;
943}
944#endif
945
946static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume);
947
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000948static const struct of_device_id dsa_of_match_table[] = {
949 { .compatible = "marvell,dsa", },
950 {}
951};
952MODULE_DEVICE_TABLE(of, dsa_of_match_table);
953
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000954static struct platform_driver dsa_driver = {
955 .probe = dsa_probe,
956 .remove = dsa_remove,
957 .shutdown = dsa_shutdown,
958 .driver = {
959 .name = "dsa",
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000960 .of_match_table = dsa_of_match_table,
Florian Fainelli24462542014-09-18 17:31:22 -0700961 .pm = &dsa_pm_ops,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000962 },
963};
964
965static int __init dsa_init_module(void)
966{
Ben Hutchings7df899c2011-11-25 14:35:02 +0000967 int rc;
968
Florian Fainellib73adef2015-02-24 13:15:33 -0800969 register_netdevice_notifier(&dsa_netdevice_nb);
970
Ben Hutchings7df899c2011-11-25 14:35:02 +0000971 rc = platform_driver_register(&dsa_driver);
972 if (rc)
973 return rc;
974
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700975 dev_add_pack(&dsa_pack_type);
976
Ben Hutchings7df899c2011-11-25 14:35:02 +0000977 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000978}
979module_init(dsa_init_module);
980
981static void __exit dsa_cleanup_module(void)
982{
Florian Fainellib73adef2015-02-24 13:15:33 -0800983 unregister_netdevice_notifier(&dsa_netdevice_nb);
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700984 dev_remove_pack(&dsa_pack_type);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000985 platform_driver_unregister(&dsa_driver);
986}
987module_exit(dsa_cleanup_module);
988
Rusty Russell577d6a72011-01-24 14:32:52 -0600989MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000990MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
991MODULE_LICENSE("GPL");
992MODULE_ALIAS("platform:dsa");