blob: b6d4f6a23f06c9d794a5eedc4c9f79810d5b06e5 [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,
Florian Fainelli293784a2017-01-26 10:45:52 -0800113 struct dsa_port *dport, int port)
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200114{
Florian Fainelli293784a2017-01-26 10:45:52 -0800115 struct device_node *port_dn = dport->dn;
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200116 struct phy_device *phydev;
117 int ret, mode;
118
119 if (of_phy_is_fixed_link(port_dn)) {
120 ret = of_phy_register_fixed_link(port_dn);
121 if (ret) {
122 dev_err(dev, "failed to register fixed PHY\n");
123 return ret;
124 }
125 phydev = of_phy_find_device(port_dn);
126
127 mode = of_get_phy_mode(port_dn);
128 if (mode < 0)
129 mode = PHY_INTERFACE_MODE_NA;
130 phydev->interface = mode;
131
132 genphy_config_init(phydev);
133 genphy_read_status(phydev);
Vivien Didelot9d490b42016-08-23 12:38:56 -0400134 if (ds->ops->adjust_link)
135 ds->ops->adjust_link(ds, port, phydev);
Johan Hovoldfd05d7b2016-11-24 19:21:27 +0100136
137 put_device(&phydev->mdio.dev);
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200138 }
139
140 return 0;
141}
142
143static int dsa_cpu_dsa_setups(struct dsa_switch *ds, struct device *dev)
Andrew Lunn39b0c702015-08-31 15:56:49 +0200144{
Florian Fainelli293784a2017-01-26 10:45:52 -0800145 struct dsa_port *dport;
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200146 int ret, port;
Andrew Lunn39b0c702015-08-31 15:56:49 +0200147
Vivien Didelot26895e22017-01-27 15:29:37 -0500148 for (port = 0; port < ds->num_ports; port++) {
Andrew Lunn39b0c702015-08-31 15:56:49 +0200149 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
150 continue;
151
Florian Fainelli293784a2017-01-26 10:45:52 -0800152 dport = &ds->ports[port];
153 ret = dsa_cpu_dsa_setup(ds, dev, dport, port);
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200154 if (ret)
155 return ret;
Andrew Lunn39b0c702015-08-31 15:56:49 +0200156 }
157 return 0;
158}
159
Andrew Lunn39a7f2a2016-06-04 21:17:03 +0200160const struct dsa_device_ops *dsa_resolve_tag_protocol(int tag_protocol)
161{
162 const struct dsa_device_ops *ops;
163
164 if (tag_protocol >= DSA_TAG_LAST)
165 return ERR_PTR(-EINVAL);
166 ops = dsa_device_ops[tag_protocol];
167
168 if (!ops)
169 return ERR_PTR(-ENOPROTOOPT);
170
171 return ops;
172}
173
Florian Fainelli0c73c522016-06-07 16:32:42 -0700174int dsa_cpu_port_ethtool_setup(struct dsa_switch *ds)
175{
176 struct net_device *master;
177 struct ethtool_ops *cpu_ops;
178
179 master = ds->dst->master_netdev;
180 if (ds->master_netdev)
181 master = ds->master_netdev;
182
183 cpu_ops = devm_kzalloc(ds->dev, sizeof(*cpu_ops), GFP_KERNEL);
184 if (!cpu_ops)
185 return -ENOMEM;
186
187 memcpy(&ds->dst->master_ethtool_ops, master->ethtool_ops,
188 sizeof(struct ethtool_ops));
189 ds->dst->master_orig_ethtool_ops = master->ethtool_ops;
190 memcpy(cpu_ops, &ds->dst->master_ethtool_ops,
191 sizeof(struct ethtool_ops));
192 dsa_cpu_port_ethtool_init(cpu_ops);
193 master->ethtool_ops = cpu_ops;
194
195 return 0;
196}
197
198void dsa_cpu_port_ethtool_restore(struct dsa_switch *ds)
199{
200 struct net_device *master;
201
202 master = ds->dst->master_netdev;
203 if (ds->master_netdev)
204 master = ds->master_netdev;
205
206 master->ethtool_ops = ds->dst->master_orig_ethtool_ops;
207}
208
Florian Fainellidf197192015-03-05 12:35:06 -0800209static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000210{
Florian Fainellia82f67a2017-01-08 14:52:08 -0800211 const struct dsa_switch_ops *ops = ds->ops;
Florian Fainellidf197192015-03-05 12:35:06 -0800212 struct dsa_switch_tree *dst = ds->dst;
Andrew Lunnff049552016-05-10 23:27:24 +0200213 struct dsa_chip_data *cd = ds->cd;
Florian Fainellif9bf5a22013-01-21 09:58:51 +0000214 bool valid_name_found = false;
Florian Fainellidf197192015-03-05 12:35:06 -0800215 int index = ds->index;
216 int i, ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000217
218 /*
219 * Validate supplied switch configuration.
220 */
Vivien Didelot26895e22017-01-27 15:29:37 -0500221 for (i = 0; i < ds->num_ports; i++) {
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000222 char *name;
223
Andrew Lunnff049552016-05-10 23:27:24 +0200224 name = cd->port_names[i];
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000225 if (name == NULL)
226 continue;
227
228 if (!strcmp(name, "cpu")) {
Andrew Lunn23e3d612017-01-22 22:16:45 +0100229 if (dst->cpu_switch) {
Joe Perchesa2ae6002014-11-09 16:32:46 -0800230 netdev_err(dst->master_netdev,
231 "multiple cpu ports?!\n");
Vivien Didelota896eee2017-01-03 14:31:49 -0500232 return -EINVAL;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000233 }
Vivien Didelotb22de492017-01-17 20:41:38 -0500234 dst->cpu_switch = ds;
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000235 dst->cpu_port = i;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200236 ds->cpu_port_mask |= 1 << i;
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000237 } else if (!strcmp(name, "dsa")) {
238 ds->dsa_port_mask |= 1 << i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000239 } else {
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200240 ds->enabled_port_mask |= 1 << i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000241 }
Florian Fainellif9bf5a22013-01-21 09:58:51 +0000242 valid_name_found = true;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000243 }
244
Vivien Didelot26895e22017-01-27 15:29:37 -0500245 if (!valid_name_found && i == ds->num_ports)
Vivien Didelota896eee2017-01-03 14:31:49 -0500246 return -EINVAL;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000247
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700248 /* Make the built-in MII bus mask match the number of ports,
249 * switch drivers can override this later
250 */
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200251 ds->phys_mii_mask = ds->enabled_port_mask;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700252
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000253 /*
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000254 * If the CPU connects to this switch, set the switch tree
255 * tagging protocol to the preferred tagging format of this
256 * switch.
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000257 */
Vivien Didelotb22de492017-01-17 20:41:38 -0500258 if (dst->cpu_switch == ds) {
Andrew Lunn7b314362016-08-22 16:01:01 +0200259 enum dsa_tag_protocol tag_protocol;
260
Vivien Didelot9d490b42016-08-23 12:38:56 -0400261 tag_protocol = ops->get_tag_protocol(ds);
Andrew Lunn7b314362016-08-22 16:01:01 +0200262 dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol);
Vivien Didelota896eee2017-01-03 14:31:49 -0500263 if (IS_ERR(dst->tag_ops))
264 return PTR_ERR(dst->tag_ops);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000265
Andrew Lunn39a7f2a2016-06-04 21:17:03 +0200266 dst->rcv = dst->tag_ops->rcv;
Alexander Duyck50753142014-09-15 13:00:19 -0400267 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000268
Andrew Lunn66472fc2016-06-04 21:17:00 +0200269 memcpy(ds->rtable, cd->rtable, sizeof(ds->rtable));
270
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000271 /*
272 * Do basic register setup.
273 */
Vivien Didelot9d490b42016-08-23 12:38:56 -0400274 ret = ops->setup(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000275 if (ret < 0)
Vivien Didelota896eee2017-01-03 14:31:49 -0500276 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000277
Vivien Didelotf515f192017-02-03 13:20:20 -0500278 ret = dsa_switch_register_notifier(ds);
279 if (ret)
280 return ret;
281
John Crispin092183d2016-09-19 15:28:01 +0200282 if (ops->set_addr) {
283 ret = ops->set_addr(ds, dst->master_netdev->dev_addr);
284 if (ret < 0)
Vivien Didelota896eee2017-01-03 14:31:49 -0500285 return ret;
John Crispin092183d2016-09-19 15:28:01 +0200286 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000287
Vivien Didelot9d490b42016-08-23 12:38:56 -0400288 if (!ds->slave_mii_bus && ops->phy_read) {
Andrew Lunne755e492016-06-04 21:17:04 +0200289 ds->slave_mii_bus = devm_mdiobus_alloc(parent);
Vivien Didelota896eee2017-01-03 14:31:49 -0500290 if (!ds->slave_mii_bus)
291 return -ENOMEM;
Andrew Lunne755e492016-06-04 21:17:04 +0200292 dsa_slave_mii_bus_init(ds);
293
294 ret = mdiobus_register(ds->slave_mii_bus);
295 if (ret < 0)
Vivien Didelota896eee2017-01-03 14:31:49 -0500296 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000297 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000298
299 /*
300 * Create network devices for physical switch ports.
301 */
Vivien Didelot26895e22017-01-27 15:29:37 -0500302 for (i = 0; i < ds->num_ports; i++) {
Andrew Lunn189b0d92016-06-04 21:16:58 +0200303 ds->ports[i].dn = cd->port_dn[i];
304
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200305 if (!(ds->enabled_port_mask & (1 << i)))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000306 continue;
307
Andrew Lunnff049552016-05-10 23:27:24 +0200308 ret = dsa_slave_create(ds, parent, i, cd->port_names[i]);
Vivien Didelota896eee2017-01-03 14:31:49 -0500309 if (ret < 0)
Russell Kingd25b8e72015-10-03 18:09:07 +0100310 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 +0200311 index, i, cd->port_names[i], ret);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000312 }
313
Andrew Lunn39b0c702015-08-31 15:56:49 +0200314 /* Perform configuration of the CPU and DSA ports */
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200315 ret = dsa_cpu_dsa_setups(ds, parent);
Vivien Didelota896eee2017-01-03 14:31:49 -0500316 if (ret < 0)
Andrew Lunn39b0c702015-08-31 15:56:49 +0200317 netdev_err(dst->master_netdev, "[%d] : can't configure CPU and DSA ports\n",
318 index);
Andrew Lunn39b0c702015-08-31 15:56:49 +0200319
Florian Fainelli0c73c522016-06-07 16:32:42 -0700320 ret = dsa_cpu_port_ethtool_setup(ds);
321 if (ret)
322 return ret;
323
Vivien Didelota896eee2017-01-03 14:31:49 -0500324 return 0;
Florian Fainellidf197192015-03-05 12:35:06 -0800325}
326
327static struct dsa_switch *
328dsa_switch_setup(struct dsa_switch_tree *dst, int index,
329 struct device *parent, struct device *host_dev)
330{
Andrew Lunnff049552016-05-10 23:27:24 +0200331 struct dsa_chip_data *cd = dst->pd->chip + index;
Florian Fainellia82f67a2017-01-08 14:52:08 -0800332 const struct dsa_switch_ops *ops;
Florian Fainellidf197192015-03-05 12:35:06 -0800333 struct dsa_switch *ds;
334 int ret;
Vivien Didelot0209d142016-04-17 13:23:55 -0400335 const char *name;
Andrew Lunn7543a6d2016-04-13 02:40:40 +0200336 void *priv;
Florian Fainellidf197192015-03-05 12:35:06 -0800337
338 /*
339 * Probe for switch model.
340 */
Vivien Didelot9d490b42016-08-23 12:38:56 -0400341 ops = dsa_switch_probe(parent, host_dev, cd->sw_addr, &name, &priv);
342 if (!ops) {
Florian Fainellidf197192015-03-05 12:35:06 -0800343 netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n",
344 index);
345 return ERR_PTR(-EINVAL);
346 }
347 netdev_info(dst->master_netdev, "[%d]: detected a %s switch\n",
348 index, name);
349
350
351 /*
352 * Allocate and initialise switch state.
353 */
Vivien Didelota0c02162017-01-27 15:29:36 -0500354 ds = dsa_switch_alloc(parent, DSA_MAX_PORTS);
355 if (!ds)
Florian Fainelli24595342015-05-29 10:29:46 -0700356 return ERR_PTR(-ENOMEM);
Florian Fainellidf197192015-03-05 12:35:06 -0800357
358 ds->dst = dst;
359 ds->index = index;
Andrew Lunnff049552016-05-10 23:27:24 +0200360 ds->cd = cd;
Vivien Didelot9d490b42016-08-23 12:38:56 -0400361 ds->ops = ops;
Andrew Lunn7543a6d2016-04-13 02:40:40 +0200362 ds->priv = priv;
Florian Fainellidf197192015-03-05 12:35:06 -0800363
364 ret = dsa_switch_setup_one(ds, parent);
365 if (ret)
Florian Fainelli24595342015-05-29 10:29:46 -0700366 return ERR_PTR(ret);
Florian Fainellidf197192015-03-05 12:35:06 -0800367
368 return ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000369}
370
Florian Fainelli293784a2017-01-26 10:45:52 -0800371void dsa_cpu_dsa_destroy(struct dsa_port *port)
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200372{
Florian Fainelli293784a2017-01-26 10:45:52 -0800373 struct device_node *port_dn = port->dn;
374
Johan Hovold3f650472016-11-28 19:24:55 +0100375 if (of_phy_is_fixed_link(port_dn))
376 of_phy_deregister_fixed_link(port_dn);
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200377}
378
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000379static void dsa_switch_destroy(struct dsa_switch *ds)
380{
Neil Armstrongcbc5d902015-10-06 15:40:32 +0100381 int port;
382
Andrew Lunn3a445142016-03-12 00:01:38 +0100383 /* Destroy network devices for physical switch ports. */
Vivien Didelot26895e22017-01-27 15:29:37 -0500384 for (port = 0; port < ds->num_ports; port++) {
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200385 if (!(ds->enabled_port_mask & (1 << port)))
Andrew Lunn3a445142016-03-12 00:01:38 +0100386 continue;
387
Andrew Lunnc8b09802016-06-04 21:16:57 +0200388 if (!ds->ports[port].netdev)
Andrew Lunn3a445142016-03-12 00:01:38 +0100389 continue;
390
Andrew Lunnc8b09802016-06-04 21:16:57 +0200391 dsa_slave_destroy(ds->ports[port].netdev);
Andrew Lunn3a445142016-03-12 00:01:38 +0100392 }
393
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200394 /* Disable configuration of the CPU and DSA ports */
Vivien Didelot26895e22017-01-27 15:29:37 -0500395 for (port = 0; port < ds->num_ports; port++) {
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200396 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
397 continue;
Florian Fainelli293784a2017-01-26 10:45:52 -0800398 dsa_cpu_dsa_destroy(&ds->ports[port]);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200399
400 /* Clearing a bit which is not set does no harm */
401 ds->cpu_port_mask |= ~(1 << port);
402 ds->dsa_port_mask |= ~(1 << port);
Neil Armstrongcbc5d902015-10-06 15:40:32 +0100403 }
404
Vivien Didelot9d490b42016-08-23 12:38:56 -0400405 if (ds->slave_mii_bus && ds->ops->phy_read)
Andrew Lunne755e492016-06-04 21:17:04 +0200406 mdiobus_unregister(ds->slave_mii_bus);
Vivien Didelotf515f192017-02-03 13:20:20 -0500407
408 dsa_switch_unregister_notifier(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000409}
410
Thierry Redinge506d402014-10-01 13:59:00 +0200411#ifdef CONFIG_PM_SLEEP
Florian Fainelliea825e72016-08-18 15:30:12 -0700412int dsa_switch_suspend(struct dsa_switch *ds)
Florian Fainelli24462542014-09-18 17:31:22 -0700413{
414 int i, ret = 0;
415
416 /* Suspend slave network devices */
Vivien Didelot26895e22017-01-27 15:29:37 -0500417 for (i = 0; i < ds->num_ports; i++) {
Guenter Roeckd79d2102015-02-24 23:02:02 -0800418 if (!dsa_is_port_initialized(ds, i))
Florian Fainelli24462542014-09-18 17:31:22 -0700419 continue;
420
Andrew Lunnc8b09802016-06-04 21:16:57 +0200421 ret = dsa_slave_suspend(ds->ports[i].netdev);
Florian Fainelli24462542014-09-18 17:31:22 -0700422 if (ret)
423 return ret;
424 }
425
Vivien Didelot9d490b42016-08-23 12:38:56 -0400426 if (ds->ops->suspend)
427 ret = ds->ops->suspend(ds);
Florian Fainelli24462542014-09-18 17:31:22 -0700428
429 return ret;
430}
Florian Fainelliea825e72016-08-18 15:30:12 -0700431EXPORT_SYMBOL_GPL(dsa_switch_suspend);
Florian Fainelli24462542014-09-18 17:31:22 -0700432
Florian Fainelliea825e72016-08-18 15:30:12 -0700433int dsa_switch_resume(struct dsa_switch *ds)
Florian Fainelli24462542014-09-18 17:31:22 -0700434{
435 int i, ret = 0;
436
Vivien Didelot9d490b42016-08-23 12:38:56 -0400437 if (ds->ops->resume)
438 ret = ds->ops->resume(ds);
Florian Fainelli24462542014-09-18 17:31:22 -0700439
440 if (ret)
441 return ret;
442
443 /* Resume slave network devices */
Vivien Didelot26895e22017-01-27 15:29:37 -0500444 for (i = 0; i < ds->num_ports; i++) {
Guenter Roeckd79d2102015-02-24 23:02:02 -0800445 if (!dsa_is_port_initialized(ds, i))
Florian Fainelli24462542014-09-18 17:31:22 -0700446 continue;
447
Andrew Lunnc8b09802016-06-04 21:16:57 +0200448 ret = dsa_slave_resume(ds->ports[i].netdev);
Florian Fainelli24462542014-09-18 17:31:22 -0700449 if (ret)
450 return ret;
451 }
452
453 return 0;
454}
Florian Fainelliea825e72016-08-18 15:30:12 -0700455EXPORT_SYMBOL_GPL(dsa_switch_resume);
Thierry Redinge506d402014-10-01 13:59:00 +0200456#endif
Florian Fainelli24462542014-09-18 17:31:22 -0700457
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000458/* platform driver init and cleanup *****************************************/
459static int dev_is_class(struct device *dev, void *class)
460{
461 if (dev->class != NULL && !strcmp(dev->class->name, class))
462 return 1;
463
464 return 0;
465}
466
467static struct device *dev_find_class(struct device *parent, char *class)
468{
469 if (dev_is_class(parent, class)) {
470 get_device(parent);
471 return parent;
472 }
473
474 return device_find_child(parent, class, dev_is_class);
475}
476
Alexander Duyckb4d23942014-09-15 13:00:27 -0400477struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000478{
479 struct device *d;
480
481 d = dev_find_class(dev, "mdio_bus");
482 if (d != NULL) {
483 struct mii_bus *bus;
484
485 bus = to_mii_bus(d);
486 put_device(d);
487
488 return bus;
489 }
490
491 return NULL;
492}
Alexander Duyckb4d23942014-09-15 13:00:27 -0400493EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000494
Florian Fainelli14b89f32017-02-04 13:02:42 -0800495struct net_device *dsa_dev_to_net_device(struct device *dev)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000496{
497 struct device *d;
498
499 d = dev_find_class(dev, "net");
500 if (d != NULL) {
501 struct net_device *nd;
502
503 nd = to_net_dev(d);
504 dev_hold(nd);
505 put_device(d);
506
507 return nd;
508 }
509
510 return NULL;
511}
Florian Fainelli14b89f32017-02-04 13:02:42 -0800512EXPORT_SYMBOL_GPL(dsa_dev_to_net_device);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000513
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000514#ifdef CONFIG_OF
515static int dsa_of_setup_routing_table(struct dsa_platform_data *pd,
516 struct dsa_chip_data *cd,
Pavel Nakonechny30303812015-04-05 00:46:21 +0300517 int chip_index, int port_index,
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000518 struct device_node *link)
519{
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000520 const __be32 *reg;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000521 int link_sw_addr;
522 struct device_node *parent_sw;
523 int len;
524
525 parent_sw = of_get_parent(link);
526 if (!parent_sw)
527 return -EINVAL;
528
529 reg = of_get_property(parent_sw, "reg", &len);
530 if (!reg || (len != sizeof(*reg) * 2))
531 return -EINVAL;
532
Pavel Nakonechny30303812015-04-05 00:46:21 +0300533 /*
534 * Get the destination switch number from the second field of its 'reg'
535 * property, i.e. for "reg = <0x19 1>" sw_addr is '1'.
536 */
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000537 link_sw_addr = be32_to_cpup(reg + 1);
538
539 if (link_sw_addr >= pd->nr_chips)
540 return -EINVAL;
541
Pavel Nakonechny30303812015-04-05 00:46:21 +0300542 cd->rtable[link_sw_addr] = port_index;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000543
544 return 0;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000545}
546
Andrew Lunn1e72e6f2015-08-17 23:52:50 +0200547static int dsa_of_probe_links(struct dsa_platform_data *pd,
548 struct dsa_chip_data *cd,
549 int chip_index, int port_index,
550 struct device_node *port,
551 const char *port_name)
552{
553 struct device_node *link;
554 int link_index;
555 int ret;
556
557 for (link_index = 0;; link_index++) {
558 link = of_parse_phandle(port, "link", link_index);
559 if (!link)
560 break;
561
562 if (!strcmp(port_name, "dsa") && pd->nr_chips > 1) {
563 ret = dsa_of_setup_routing_table(pd, cd, chip_index,
564 port_index, link);
565 if (ret)
566 return ret;
567 }
568 }
569 return 0;
570}
571
Florian Fainelli21168242013-03-25 05:03:39 +0000572static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
573{
574 int i;
575 int port_index;
576
577 for (i = 0; i < pd->nr_chips; i++) {
578 port_index = 0;
Florian Fainelli5f64a7d2013-03-25 05:03:40 +0000579 while (port_index < DSA_MAX_PORTS) {
Fabian Frederick1f747142014-06-23 19:32:47 +0200580 kfree(pd->chip[i].port_names[port_index]);
Florian Fainelli5f64a7d2013-03-25 05:03:40 +0000581 port_index++;
582 }
Russell Kinge496ae62015-09-24 20:35:57 +0100583
584 /* Drop our reference to the MDIO bus device */
585 if (pd->chip[i].host_dev)
586 put_device(pd->chip[i].host_dev);
Florian Fainelli21168242013-03-25 05:03:39 +0000587 }
588 kfree(pd->chip);
589}
590
Florian Fainellif1a26a02015-03-05 12:35:04 -0800591static int dsa_of_probe(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000592{
Florian Fainellif1a26a02015-03-05 12:35:04 -0800593 struct device_node *np = dev->of_node;
Andrew Lunn1e72e6f2015-08-17 23:52:50 +0200594 struct device_node *child, *mdio, *ethernet, *port;
Andrew Lunn6bc6d0a2015-08-08 17:09:14 +0200595 struct mii_bus *mdio_bus, *mdio_bus_switch;
Florian Fainelli769a0202015-03-09 14:31:21 -0700596 struct net_device *ethernet_dev;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000597 struct dsa_platform_data *pd;
598 struct dsa_chip_data *cd;
599 const char *port_name;
600 int chip_index, port_index;
601 const unsigned int *sw_addr, *port_reg;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700602 u32 eeprom_len;
Florian Fainelli21168242013-03-25 05:03:39 +0000603 int ret;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000604
605 mdio = of_parse_phandle(np, "dsa,mii-bus", 0);
606 if (!mdio)
607 return -EINVAL;
608
609 mdio_bus = of_mdio_find_bus(mdio);
610 if (!mdio_bus)
Florian Fainellib324c072015-03-05 12:35:05 -0800611 return -EPROBE_DEFER;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000612
613 ethernet = of_parse_phandle(np, "dsa,ethernet", 0);
Russell Kinge496ae62015-09-24 20:35:57 +0100614 if (!ethernet) {
615 ret = -EINVAL;
616 goto out_put_mdio;
617 }
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000618
Florian Fainelli769a0202015-03-09 14:31:21 -0700619 ethernet_dev = of_find_net_device_by_node(ethernet);
Russell Kinge496ae62015-09-24 20:35:57 +0100620 if (!ethernet_dev) {
621 ret = -EPROBE_DEFER;
622 goto out_put_mdio;
623 }
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000624
625 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
Russell Kinge496ae62015-09-24 20:35:57 +0100626 if (!pd) {
627 ret = -ENOMEM;
Russell King9861f722015-09-24 20:36:33 +0100628 goto out_put_ethernet;
Russell Kinge496ae62015-09-24 20:35:57 +0100629 }
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000630
Florian Fainellif1a26a02015-03-05 12:35:04 -0800631 dev->platform_data = pd;
Florian Fainelli769a0202015-03-09 14:31:21 -0700632 pd->of_netdev = ethernet_dev;
Tobias Waldekranze04449f2015-02-05 14:54:09 +0100633 pd->nr_chips = of_get_available_child_count(np);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000634 if (pd->nr_chips > DSA_MAX_SWITCHES)
635 pd->nr_chips = DSA_MAX_SWITCHES;
636
Fabian Frederick6f2aed62014-11-14 19:38:23 +0100637 pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data),
638 GFP_KERNEL);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000639 if (!pd->chip) {
640 ret = -ENOMEM;
641 goto out_free;
642 }
643
Fabian Godehardtd1c0b472014-05-16 06:21:44 +0200644 chip_index = -1;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000645 for_each_available_child_of_node(np, child) {
Vivien Didelotd3902382016-07-06 20:03:54 -0400646 int i;
647
Fabian Godehardtd1c0b472014-05-16 06:21:44 +0200648 chip_index++;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000649 cd = &pd->chip[chip_index];
650
Florian Fainellifa981d92014-08-27 17:04:49 -0700651 cd->of_node = child;
Russell Kinge496ae62015-09-24 20:35:57 +0100652
Vivien Didelotd3902382016-07-06 20:03:54 -0400653 /* Initialize the routing table */
654 for (i = 0; i < DSA_MAX_SWITCHES; ++i)
655 cd->rtable[i] = DSA_RTABLE_NONE;
656
Russell Kinge496ae62015-09-24 20:35:57 +0100657 /* When assigning the host device, increment its refcount */
658 cd->host_dev = get_device(&mdio_bus->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000659
660 sw_addr = of_get_property(child, "reg", NULL);
661 if (!sw_addr)
662 continue;
663
664 cd->sw_addr = be32_to_cpup(sw_addr);
Florian Fainellic8cf89f2015-07-11 18:02:11 -0700665 if (cd->sw_addr >= PHY_MAX_ADDR)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000666 continue;
667
Guenter Roeck50d49642015-04-29 10:56:15 -0700668 if (!of_property_read_u32(child, "eeprom-length", &eeprom_len))
Guenter Roeck6793abb2014-10-29 10:45:01 -0700669 cd->eeprom_len = eeprom_len;
670
Andrew Lunn6bc6d0a2015-08-08 17:09:14 +0200671 mdio = of_parse_phandle(child, "mii-bus", 0);
672 if (mdio) {
673 mdio_bus_switch = of_mdio_find_bus(mdio);
674 if (!mdio_bus_switch) {
675 ret = -EPROBE_DEFER;
676 goto out_free_chip;
677 }
Russell Kinge496ae62015-09-24 20:35:57 +0100678
679 /* Drop the mdio_bus device ref, replacing the host
680 * device with the mdio_bus_switch device, keeping
681 * the refcount from of_mdio_find_bus() above.
682 */
683 put_device(cd->host_dev);
Andrew Lunn6bc6d0a2015-08-08 17:09:14 +0200684 cd->host_dev = &mdio_bus_switch->dev;
685 }
686
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000687 for_each_available_child_of_node(child, port) {
688 port_reg = of_get_property(port, "reg", NULL);
689 if (!port_reg)
690 continue;
691
692 port_index = be32_to_cpup(port_reg);
Florian Fainelli8f5063e2015-07-11 18:02:10 -0700693 if (port_index >= DSA_MAX_PORTS)
694 break;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000695
696 port_name = of_get_property(port, "label", NULL);
697 if (!port_name)
698 continue;
699
Florian Fainellibd474972014-08-27 17:04:50 -0700700 cd->port_dn[port_index] = port;
701
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000702 cd->port_names[port_index] = kstrdup(port_name,
703 GFP_KERNEL);
704 if (!cd->port_names[port_index]) {
705 ret = -ENOMEM;
706 goto out_free_chip;
707 }
708
Andrew Lunn1e72e6f2015-08-17 23:52:50 +0200709 ret = dsa_of_probe_links(pd, cd, chip_index,
710 port_index, port, port_name);
711 if (ret)
712 goto out_free_chip;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000713
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000714 }
715 }
716
Russell Kinge496ae62015-09-24 20:35:57 +0100717 /* The individual chips hold their own refcount on the mdio bus,
718 * so drop ours */
719 put_device(&mdio_bus->dev);
720
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000721 return 0;
722
723out_free_chip:
Florian Fainelli21168242013-03-25 05:03:39 +0000724 dsa_of_free_platform_data(pd);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000725out_free:
726 kfree(pd);
Florian Fainellif1a26a02015-03-05 12:35:04 -0800727 dev->platform_data = NULL;
Russell King9861f722015-09-24 20:36:33 +0100728out_put_ethernet:
729 put_device(&ethernet_dev->dev);
Russell Kinge496ae62015-09-24 20:35:57 +0100730out_put_mdio:
731 put_device(&mdio_bus->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000732 return ret;
733}
734
Florian Fainellif1a26a02015-03-05 12:35:04 -0800735static void dsa_of_remove(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000736{
Florian Fainellif1a26a02015-03-05 12:35:04 -0800737 struct dsa_platform_data *pd = dev->platform_data;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000738
Florian Fainellif1a26a02015-03-05 12:35:04 -0800739 if (!dev->of_node)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000740 return;
741
Florian Fainelli21168242013-03-25 05:03:39 +0000742 dsa_of_free_platform_data(pd);
Russell King9861f722015-09-24 20:36:33 +0100743 put_device(&pd->of_netdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000744 kfree(pd);
745}
746#else
Florian Fainellif1a26a02015-03-05 12:35:04 -0800747static inline int dsa_of_probe(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000748{
749 return 0;
750}
751
Florian Fainellif1a26a02015-03-05 12:35:04 -0800752static inline void dsa_of_remove(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000753{
754}
755#endif
756
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100757static int dsa_setup_dst(struct dsa_switch_tree *dst, struct net_device *dev,
758 struct device *parent, struct dsa_platform_data *pd)
Florian Fainellic86e59b2015-03-05 12:35:08 -0800759{
760 int i;
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100761 unsigned configured = 0;
Florian Fainellic86e59b2015-03-05 12:35:08 -0800762
763 dst->pd = pd;
764 dst->master_netdev = dev;
Florian Fainellic86e59b2015-03-05 12:35:08 -0800765 dst->cpu_port = -1;
766
767 for (i = 0; i < pd->nr_chips; i++) {
768 struct dsa_switch *ds;
769
770 ds = dsa_switch_setup(dst, i, parent, pd->chip[i].host_dev);
771 if (IS_ERR(ds)) {
772 netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n",
773 i, PTR_ERR(ds));
774 continue;
775 }
776
777 dst->ds[i] = ds;
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100778
779 ++configured;
Florian Fainellic86e59b2015-03-05 12:35:08 -0800780 }
781
782 /*
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100783 * If no switch was found, exit cleanly
784 */
785 if (!configured)
786 return -EPROBE_DEFER;
787
788 /*
Florian Fainellic86e59b2015-03-05 12:35:08 -0800789 * If we use a tagging format that doesn't have an ethertype
790 * field, make sure that all packets from this point on get
791 * sent to the tag format's receive function.
792 */
793 wmb();
794 dev->dsa_ptr = (void *)dst;
795
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100796 return 0;
Florian Fainellic86e59b2015-03-05 12:35:08 -0800797}
798
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000799static int dsa_probe(struct platform_device *pdev)
800{
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000801 struct dsa_platform_data *pd = pdev->dev.platform_data;
802 struct net_device *dev;
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000803 struct dsa_switch_tree *dst;
Florian Fainellic86e59b2015-03-05 12:35:08 -0800804 int ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000805
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000806 if (pdev->dev.of_node) {
Florian Fainellif1a26a02015-03-05 12:35:04 -0800807 ret = dsa_of_probe(&pdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000808 if (ret)
809 return ret;
810
811 pd = pdev->dev.platform_data;
812 }
813
Florian Fainelli769a0202015-03-09 14:31:21 -0700814 if (pd == NULL || (pd->netdev == NULL && pd->of_netdev == NULL))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000815 return -EINVAL;
816
Florian Fainelli769a0202015-03-09 14:31:21 -0700817 if (pd->of_netdev) {
818 dev = pd->of_netdev;
819 dev_hold(dev);
820 } else {
Florian Fainelli14b89f32017-02-04 13:02:42 -0800821 dev = dsa_dev_to_net_device(pd->netdev);
Florian Fainelli769a0202015-03-09 14:31:21 -0700822 }
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000823 if (dev == NULL) {
Florian Fainellib324c072015-03-05 12:35:05 -0800824 ret = -EPROBE_DEFER;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000825 goto out;
826 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000827
828 if (dev->dsa_ptr != NULL) {
829 dev_put(dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000830 ret = -EEXIST;
831 goto out;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000832 }
833
Neil Armstrongd4ac35d2015-10-06 15:40:37 +0100834 dst = devm_kzalloc(&pdev->dev, sizeof(*dst), GFP_KERNEL);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000835 if (dst == NULL) {
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000836 dev_put(dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000837 ret = -ENOMEM;
838 goto out;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000839 }
840
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000841 platform_set_drvdata(pdev, dst);
842
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100843 ret = dsa_setup_dst(dst, dev, &pdev->dev, pd);
Neil Armstrong679fb462015-12-07 13:57:34 +0100844 if (ret) {
845 dev_put(dev);
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100846 goto out;
Neil Armstrong679fb462015-12-07 13:57:34 +0100847 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000848
849 return 0;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000850
851out:
Florian Fainellif1a26a02015-03-05 12:35:04 -0800852 dsa_of_remove(&pdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000853
854 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000855}
856
Florian Fainellic86e59b2015-03-05 12:35:08 -0800857static void dsa_remove_dst(struct dsa_switch_tree *dst)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000858{
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000859 int i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000860
Neil Armstrong04761892016-03-08 10:36:20 +0100861 dst->master_netdev->dsa_ptr = NULL;
862
863 /* If we used a tagging format that doesn't have an ethertype
864 * field, make sure that all packets from this point get sent
865 * without the tag and go through the regular receive path.
866 */
867 wmb();
868
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000869 for (i = 0; i < dst->pd->nr_chips; i++) {
870 struct dsa_switch *ds = dst->ds[i];
871
Neil Armstrongd4ac35d2015-10-06 15:40:37 +0100872 if (ds)
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000873 dsa_switch_destroy(ds);
874 }
Neil Armstrong679fb462015-12-07 13:57:34 +0100875
Vivien Didelot9520ed82017-01-17 20:41:39 -0500876 dsa_cpu_port_ethtool_restore(dst->cpu_switch);
Florian Fainelli0c73c522016-06-07 16:32:42 -0700877
Neil Armstrong679fb462015-12-07 13:57:34 +0100878 dev_put(dst->master_netdev);
Florian Fainellic86e59b2015-03-05 12:35:08 -0800879}
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000880
Florian Fainellic86e59b2015-03-05 12:35:08 -0800881static int dsa_remove(struct platform_device *pdev)
882{
883 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
884
885 dsa_remove_dst(dst);
Florian Fainellif1a26a02015-03-05 12:35:04 -0800886 dsa_of_remove(&pdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000887
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000888 return 0;
889}
890
891static void dsa_shutdown(struct platform_device *pdev)
892{
893}
894
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700895static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
896 struct packet_type *pt, struct net_device *orig_dev)
897{
898 struct dsa_switch_tree *dst = dev->dsa_ptr;
899
900 if (unlikely(dst == NULL)) {
901 kfree_skb(skb);
902 return 0;
903 }
904
Alexander Duyck50753142014-09-15 13:00:19 -0400905 return dst->rcv(skb, dev, pt, orig_dev);
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700906}
907
Florian Fainelli61b73632014-08-29 12:42:07 -0700908static struct packet_type dsa_pack_type __read_mostly = {
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700909 .type = cpu_to_be16(ETH_P_XDSA),
910 .func = dsa_switch_rcv,
911};
912
Florian Fainelli24462542014-09-18 17:31:22 -0700913#ifdef CONFIG_PM_SLEEP
914static int dsa_suspend(struct device *d)
915{
916 struct platform_device *pdev = to_platform_device(d);
917 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
918 int i, ret = 0;
919
920 for (i = 0; i < dst->pd->nr_chips; i++) {
921 struct dsa_switch *ds = dst->ds[i];
922
923 if (ds != NULL)
924 ret = dsa_switch_suspend(ds);
925 }
926
927 return ret;
928}
929
930static int dsa_resume(struct device *d)
931{
932 struct platform_device *pdev = to_platform_device(d);
933 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
934 int i, ret = 0;
935
936 for (i = 0; i < dst->pd->nr_chips; i++) {
937 struct dsa_switch *ds = dst->ds[i];
938
939 if (ds != NULL)
940 ret = dsa_switch_resume(ds);
941 }
942
943 return ret;
944}
945#endif
946
947static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume);
948
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000949static const struct of_device_id dsa_of_match_table[] = {
950 { .compatible = "marvell,dsa", },
951 {}
952};
953MODULE_DEVICE_TABLE(of, dsa_of_match_table);
954
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000955static struct platform_driver dsa_driver = {
956 .probe = dsa_probe,
957 .remove = dsa_remove,
958 .shutdown = dsa_shutdown,
959 .driver = {
960 .name = "dsa",
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000961 .of_match_table = dsa_of_match_table,
Florian Fainelli24462542014-09-18 17:31:22 -0700962 .pm = &dsa_pm_ops,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000963 },
964};
965
966static int __init dsa_init_module(void)
967{
Ben Hutchings7df899c2011-11-25 14:35:02 +0000968 int rc;
969
Vivien Didelot88e4f0c2017-02-03 13:20:16 -0500970 rc = dsa_slave_register_notifier();
971 if (rc)
972 return rc;
Florian Fainellib73adef2015-02-24 13:15:33 -0800973
Ben Hutchings7df899c2011-11-25 14:35:02 +0000974 rc = platform_driver_register(&dsa_driver);
975 if (rc)
976 return rc;
977
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700978 dev_add_pack(&dsa_pack_type);
979
Ben Hutchings7df899c2011-11-25 14:35:02 +0000980 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000981}
982module_init(dsa_init_module);
983
984static void __exit dsa_cleanup_module(void)
985{
Vivien Didelot88e4f0c2017-02-03 13:20:16 -0500986 dsa_slave_unregister_notifier();
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700987 dev_remove_pack(&dsa_pack_type);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000988 platform_driver_unregister(&dsa_driver);
989}
990module_exit(dsa_cleanup_module);
991
Rusty Russell577d6a72011-01-24 14:32:52 -0600992MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000993MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
994MODULE_LICENSE("GPL");
995MODULE_ALIAS("platform:dsa");