blob: 1fb9cf7aaaf498b332ec9af913b0279c369a5605 [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>
Florian Fainellia86d8be2017-04-08 08:55:23 -070026#include <linux/etherdevice.h>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020027#include <net/dsa.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000028#include "dsa_priv.h"
29
Andrew Lunn39a7f2a2016-06-04 21:17:03 +020030static struct sk_buff *dsa_slave_notag_xmit(struct sk_buff *skb,
31 struct net_device *dev)
32{
33 /* Just return the original SKB */
34 return skb;
35}
36
37static const struct dsa_device_ops none_ops = {
38 .xmit = dsa_slave_notag_xmit,
39 .rcv = NULL,
40};
41
42const struct dsa_device_ops *dsa_device_ops[DSA_TAG_LAST] = {
43#ifdef CONFIG_NET_DSA_TAG_DSA
44 [DSA_TAG_PROTO_DSA] = &dsa_netdev_ops,
45#endif
46#ifdef CONFIG_NET_DSA_TAG_EDSA
47 [DSA_TAG_PROTO_EDSA] = &edsa_netdev_ops,
48#endif
49#ifdef CONFIG_NET_DSA_TAG_TRAILER
50 [DSA_TAG_PROTO_TRAILER] = &trailer_netdev_ops,
51#endif
52#ifdef CONFIG_NET_DSA_TAG_BRCM
53 [DSA_TAG_PROTO_BRCM] = &brcm_netdev_ops,
54#endif
John Crispincafdc452016-09-15 16:26:40 +020055#ifdef CONFIG_NET_DSA_TAG_QCA
56 [DSA_TAG_PROTO_QCA] = &qca_netdev_ops,
57#endif
Sean Wang5cd89852017-04-07 16:45:06 +080058#ifdef CONFIG_NET_DSA_TAG_MTK
59 [DSA_TAG_PROTO_MTK] = &mtk_netdev_ops,
60#endif
Andrew Lunn39a7f2a2016-06-04 21:17:03 +020061 [DSA_TAG_PROTO_NONE] = &none_ops,
62};
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000063
64/* switch driver registration ***********************************************/
65static DEFINE_MUTEX(dsa_switch_drivers_mutex);
66static LIST_HEAD(dsa_switch_drivers);
67
Florian Fainelliab3d4082017-01-08 14:52:07 -080068void register_switch_driver(struct dsa_switch_driver *drv)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000069{
70 mutex_lock(&dsa_switch_drivers_mutex);
Florian Fainelliab3d4082017-01-08 14:52:07 -080071 list_add_tail(&drv->list, &dsa_switch_drivers);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000072 mutex_unlock(&dsa_switch_drivers_mutex);
73}
Ben Hutchingsad293b82011-11-25 14:34:07 +000074EXPORT_SYMBOL_GPL(register_switch_driver);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000075
Florian Fainelliab3d4082017-01-08 14:52:07 -080076void unregister_switch_driver(struct dsa_switch_driver *drv)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000077{
78 mutex_lock(&dsa_switch_drivers_mutex);
Florian Fainelliab3d4082017-01-08 14:52:07 -080079 list_del_init(&drv->list);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000080 mutex_unlock(&dsa_switch_drivers_mutex);
81}
Ben Hutchingsad293b82011-11-25 14:34:07 +000082EXPORT_SYMBOL_GPL(unregister_switch_driver);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000083
Florian Fainellia82f67a2017-01-08 14:52:08 -080084static const struct dsa_switch_ops *
Andrew Lunnbbb8d792016-04-13 02:40:39 +020085dsa_switch_probe(struct device *parent, struct device *host_dev, int sw_addr,
Vivien Didelot0209d142016-04-17 13:23:55 -040086 const char **_name, void **priv)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000087{
Florian Fainellia82f67a2017-01-08 14:52:08 -080088 const struct dsa_switch_ops *ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000089 struct list_head *list;
Vivien Didelot0209d142016-04-17 13:23:55 -040090 const char *name;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000091
92 ret = NULL;
93 name = NULL;
94
95 mutex_lock(&dsa_switch_drivers_mutex);
96 list_for_each(list, &dsa_switch_drivers) {
Florian Fainellia82f67a2017-01-08 14:52:08 -080097 const struct dsa_switch_ops *ops;
Florian Fainelliab3d4082017-01-08 14:52:07 -080098 struct dsa_switch_driver *drv;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000099
Florian Fainelliab3d4082017-01-08 14:52:07 -0800100 drv = list_entry(list, struct dsa_switch_driver, list);
101 ops = drv->ops;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000102
Vivien Didelot9d490b42016-08-23 12:38:56 -0400103 name = ops->probe(parent, host_dev, sw_addr, priv);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000104 if (name != NULL) {
Vivien Didelot9d490b42016-08-23 12:38:56 -0400105 ret = ops;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000106 break;
107 }
108 }
109 mutex_unlock(&dsa_switch_drivers_mutex);
110
111 *_name = name;
112
113 return ret;
114}
115
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000116/* basic switch operations **************************************************/
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200117int dsa_cpu_dsa_setup(struct dsa_switch *ds, struct device *dev,
Florian Fainelli293784a2017-01-26 10:45:52 -0800118 struct dsa_port *dport, int port)
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200119{
Florian Fainelli293784a2017-01-26 10:45:52 -0800120 struct device_node *port_dn = dport->dn;
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200121 struct phy_device *phydev;
122 int ret, mode;
123
124 if (of_phy_is_fixed_link(port_dn)) {
125 ret = of_phy_register_fixed_link(port_dn);
126 if (ret) {
127 dev_err(dev, "failed to register fixed PHY\n");
128 return ret;
129 }
130 phydev = of_phy_find_device(port_dn);
131
132 mode = of_get_phy_mode(port_dn);
133 if (mode < 0)
134 mode = PHY_INTERFACE_MODE_NA;
135 phydev->interface = mode;
136
137 genphy_config_init(phydev);
138 genphy_read_status(phydev);
Vivien Didelot9d490b42016-08-23 12:38:56 -0400139 if (ds->ops->adjust_link)
140 ds->ops->adjust_link(ds, port, phydev);
Johan Hovoldfd05d7b2016-11-24 19:21:27 +0100141
142 put_device(&phydev->mdio.dev);
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200143 }
144
145 return 0;
146}
147
148static int dsa_cpu_dsa_setups(struct dsa_switch *ds, struct device *dev)
Andrew Lunn39b0c702015-08-31 15:56:49 +0200149{
Florian Fainelli293784a2017-01-26 10:45:52 -0800150 struct dsa_port *dport;
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200151 int ret, port;
Andrew Lunn39b0c702015-08-31 15:56:49 +0200152
Vivien Didelot26895e22017-01-27 15:29:37 -0500153 for (port = 0; port < ds->num_ports; port++) {
Andrew Lunn39b0c702015-08-31 15:56:49 +0200154 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
155 continue;
156
Florian Fainelli293784a2017-01-26 10:45:52 -0800157 dport = &ds->ports[port];
158 ret = dsa_cpu_dsa_setup(ds, dev, dport, port);
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200159 if (ret)
160 return ret;
Andrew Lunn39b0c702015-08-31 15:56:49 +0200161 }
162 return 0;
163}
164
Andrew Lunn39a7f2a2016-06-04 21:17:03 +0200165const struct dsa_device_ops *dsa_resolve_tag_protocol(int tag_protocol)
166{
167 const struct dsa_device_ops *ops;
168
169 if (tag_protocol >= DSA_TAG_LAST)
170 return ERR_PTR(-EINVAL);
171 ops = dsa_device_ops[tag_protocol];
172
173 if (!ops)
174 return ERR_PTR(-ENOPROTOOPT);
175
176 return ops;
177}
178
Florian Fainelli0c73c522016-06-07 16:32:42 -0700179int dsa_cpu_port_ethtool_setup(struct dsa_switch *ds)
180{
181 struct net_device *master;
182 struct ethtool_ops *cpu_ops;
183
184 master = ds->dst->master_netdev;
185 if (ds->master_netdev)
186 master = ds->master_netdev;
187
188 cpu_ops = devm_kzalloc(ds->dev, sizeof(*cpu_ops), GFP_KERNEL);
189 if (!cpu_ops)
190 return -ENOMEM;
191
192 memcpy(&ds->dst->master_ethtool_ops, master->ethtool_ops,
193 sizeof(struct ethtool_ops));
194 ds->dst->master_orig_ethtool_ops = master->ethtool_ops;
195 memcpy(cpu_ops, &ds->dst->master_ethtool_ops,
196 sizeof(struct ethtool_ops));
197 dsa_cpu_port_ethtool_init(cpu_ops);
198 master->ethtool_ops = cpu_ops;
199
200 return 0;
201}
202
203void dsa_cpu_port_ethtool_restore(struct dsa_switch *ds)
204{
205 struct net_device *master;
206
207 master = ds->dst->master_netdev;
208 if (ds->master_netdev)
209 master = ds->master_netdev;
210
211 master->ethtool_ops = ds->dst->master_orig_ethtool_ops;
212}
213
Florian Fainellidf197192015-03-05 12:35:06 -0800214static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000215{
Florian Fainellia82f67a2017-01-08 14:52:08 -0800216 const struct dsa_switch_ops *ops = ds->ops;
Florian Fainellidf197192015-03-05 12:35:06 -0800217 struct dsa_switch_tree *dst = ds->dst;
Andrew Lunnff049552016-05-10 23:27:24 +0200218 struct dsa_chip_data *cd = ds->cd;
Florian Fainellif9bf5a22013-01-21 09:58:51 +0000219 bool valid_name_found = false;
Florian Fainellidf197192015-03-05 12:35:06 -0800220 int index = ds->index;
221 int i, ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000222
223 /*
224 * Validate supplied switch configuration.
225 */
Vivien Didelot26895e22017-01-27 15:29:37 -0500226 for (i = 0; i < ds->num_ports; i++) {
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000227 char *name;
228
Andrew Lunnff049552016-05-10 23:27:24 +0200229 name = cd->port_names[i];
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000230 if (name == NULL)
231 continue;
232
233 if (!strcmp(name, "cpu")) {
Andrew Lunn23e3d612017-01-22 22:16:45 +0100234 if (dst->cpu_switch) {
Joe Perchesa2ae6002014-11-09 16:32:46 -0800235 netdev_err(dst->master_netdev,
236 "multiple cpu ports?!\n");
Vivien Didelota896eee2017-01-03 14:31:49 -0500237 return -EINVAL;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000238 }
Vivien Didelotb22de492017-01-17 20:41:38 -0500239 dst->cpu_switch = ds;
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000240 dst->cpu_port = i;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200241 ds->cpu_port_mask |= 1 << i;
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000242 } else if (!strcmp(name, "dsa")) {
243 ds->dsa_port_mask |= 1 << i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000244 } else {
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200245 ds->enabled_port_mask |= 1 << i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000246 }
Florian Fainellif9bf5a22013-01-21 09:58:51 +0000247 valid_name_found = true;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000248 }
249
Vivien Didelot26895e22017-01-27 15:29:37 -0500250 if (!valid_name_found && i == ds->num_ports)
Vivien Didelota896eee2017-01-03 14:31:49 -0500251 return -EINVAL;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000252
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700253 /* Make the built-in MII bus mask match the number of ports,
254 * switch drivers can override this later
255 */
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200256 ds->phys_mii_mask = ds->enabled_port_mask;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700257
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000258 /*
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000259 * If the CPU connects to this switch, set the switch tree
260 * tagging protocol to the preferred tagging format of this
261 * switch.
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000262 */
Vivien Didelotb22de492017-01-17 20:41:38 -0500263 if (dst->cpu_switch == ds) {
Andrew Lunn7b314362016-08-22 16:01:01 +0200264 enum dsa_tag_protocol tag_protocol;
265
Vivien Didelot9d490b42016-08-23 12:38:56 -0400266 tag_protocol = ops->get_tag_protocol(ds);
Andrew Lunn7b314362016-08-22 16:01:01 +0200267 dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol);
Vivien Didelota896eee2017-01-03 14:31:49 -0500268 if (IS_ERR(dst->tag_ops))
269 return PTR_ERR(dst->tag_ops);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000270
Andrew Lunn39a7f2a2016-06-04 21:17:03 +0200271 dst->rcv = dst->tag_ops->rcv;
Alexander Duyck50753142014-09-15 13:00:19 -0400272 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000273
Andrew Lunn66472fc2016-06-04 21:17:00 +0200274 memcpy(ds->rtable, cd->rtable, sizeof(ds->rtable));
275
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000276 /*
277 * Do basic register setup.
278 */
Vivien Didelot9d490b42016-08-23 12:38:56 -0400279 ret = ops->setup(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000280 if (ret < 0)
Vivien Didelota896eee2017-01-03 14:31:49 -0500281 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000282
Vivien Didelotf515f192017-02-03 13:20:20 -0500283 ret = dsa_switch_register_notifier(ds);
284 if (ret)
285 return ret;
286
John Crispin092183d2016-09-19 15:28:01 +0200287 if (ops->set_addr) {
288 ret = ops->set_addr(ds, dst->master_netdev->dev_addr);
289 if (ret < 0)
Vivien Didelota896eee2017-01-03 14:31:49 -0500290 return ret;
John Crispin092183d2016-09-19 15:28:01 +0200291 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000292
Vivien Didelot9d490b42016-08-23 12:38:56 -0400293 if (!ds->slave_mii_bus && ops->phy_read) {
Andrew Lunne755e492016-06-04 21:17:04 +0200294 ds->slave_mii_bus = devm_mdiobus_alloc(parent);
Vivien Didelota896eee2017-01-03 14:31:49 -0500295 if (!ds->slave_mii_bus)
296 return -ENOMEM;
Andrew Lunne755e492016-06-04 21:17:04 +0200297 dsa_slave_mii_bus_init(ds);
298
299 ret = mdiobus_register(ds->slave_mii_bus);
300 if (ret < 0)
Vivien Didelota896eee2017-01-03 14:31:49 -0500301 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000302 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000303
304 /*
305 * Create network devices for physical switch ports.
306 */
Vivien Didelot26895e22017-01-27 15:29:37 -0500307 for (i = 0; i < ds->num_ports; i++) {
Andrew Lunn189b0d92016-06-04 21:16:58 +0200308 ds->ports[i].dn = cd->port_dn[i];
309
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200310 if (!(ds->enabled_port_mask & (1 << i)))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000311 continue;
312
Andrew Lunnff049552016-05-10 23:27:24 +0200313 ret = dsa_slave_create(ds, parent, i, cd->port_names[i]);
Vivien Didelota896eee2017-01-03 14:31:49 -0500314 if (ret < 0)
Russell Kingd25b8e72015-10-03 18:09:07 +0100315 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 +0200316 index, i, cd->port_names[i], ret);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000317 }
318
Andrew Lunn39b0c702015-08-31 15:56:49 +0200319 /* Perform configuration of the CPU and DSA ports */
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200320 ret = dsa_cpu_dsa_setups(ds, parent);
Vivien Didelota896eee2017-01-03 14:31:49 -0500321 if (ret < 0)
Andrew Lunn39b0c702015-08-31 15:56:49 +0200322 netdev_err(dst->master_netdev, "[%d] : can't configure CPU and DSA ports\n",
323 index);
Andrew Lunn39b0c702015-08-31 15:56:49 +0200324
Florian Fainelli0c73c522016-06-07 16:32:42 -0700325 ret = dsa_cpu_port_ethtool_setup(ds);
326 if (ret)
327 return ret;
328
Vivien Didelota896eee2017-01-03 14:31:49 -0500329 return 0;
Florian Fainellidf197192015-03-05 12:35:06 -0800330}
331
332static struct dsa_switch *
333dsa_switch_setup(struct dsa_switch_tree *dst, int index,
334 struct device *parent, struct device *host_dev)
335{
Andrew Lunnff049552016-05-10 23:27:24 +0200336 struct dsa_chip_data *cd = dst->pd->chip + index;
Florian Fainellia82f67a2017-01-08 14:52:08 -0800337 const struct dsa_switch_ops *ops;
Florian Fainellidf197192015-03-05 12:35:06 -0800338 struct dsa_switch *ds;
339 int ret;
Vivien Didelot0209d142016-04-17 13:23:55 -0400340 const char *name;
Andrew Lunn7543a6d2016-04-13 02:40:40 +0200341 void *priv;
Florian Fainellidf197192015-03-05 12:35:06 -0800342
343 /*
344 * Probe for switch model.
345 */
Vivien Didelot9d490b42016-08-23 12:38:56 -0400346 ops = dsa_switch_probe(parent, host_dev, cd->sw_addr, &name, &priv);
347 if (!ops) {
Florian Fainellidf197192015-03-05 12:35:06 -0800348 netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n",
349 index);
350 return ERR_PTR(-EINVAL);
351 }
352 netdev_info(dst->master_netdev, "[%d]: detected a %s switch\n",
353 index, name);
354
355
356 /*
357 * Allocate and initialise switch state.
358 */
Vivien Didelota0c02162017-01-27 15:29:36 -0500359 ds = dsa_switch_alloc(parent, DSA_MAX_PORTS);
360 if (!ds)
Florian Fainelli24595342015-05-29 10:29:46 -0700361 return ERR_PTR(-ENOMEM);
Florian Fainellidf197192015-03-05 12:35:06 -0800362
363 ds->dst = dst;
364 ds->index = index;
Andrew Lunnff049552016-05-10 23:27:24 +0200365 ds->cd = cd;
Vivien Didelot9d490b42016-08-23 12:38:56 -0400366 ds->ops = ops;
Andrew Lunn7543a6d2016-04-13 02:40:40 +0200367 ds->priv = priv;
Florian Fainellidf197192015-03-05 12:35:06 -0800368
369 ret = dsa_switch_setup_one(ds, parent);
370 if (ret)
Florian Fainelli24595342015-05-29 10:29:46 -0700371 return ERR_PTR(ret);
Florian Fainellidf197192015-03-05 12:35:06 -0800372
373 return ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000374}
375
Florian Fainelli293784a2017-01-26 10:45:52 -0800376void dsa_cpu_dsa_destroy(struct dsa_port *port)
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200377{
Florian Fainelli293784a2017-01-26 10:45:52 -0800378 struct device_node *port_dn = port->dn;
379
Johan Hovold3f650472016-11-28 19:24:55 +0100380 if (of_phy_is_fixed_link(port_dn))
381 of_phy_deregister_fixed_link(port_dn);
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200382}
383
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000384static void dsa_switch_destroy(struct dsa_switch *ds)
385{
Neil Armstrongcbc5d902015-10-06 15:40:32 +0100386 int port;
387
Andrew Lunn3a445142016-03-12 00:01:38 +0100388 /* Destroy network devices for physical switch ports. */
Vivien Didelot26895e22017-01-27 15:29:37 -0500389 for (port = 0; port < ds->num_ports; port++) {
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200390 if (!(ds->enabled_port_mask & (1 << port)))
Andrew Lunn3a445142016-03-12 00:01:38 +0100391 continue;
392
Andrew Lunnc8b09802016-06-04 21:16:57 +0200393 if (!ds->ports[port].netdev)
Andrew Lunn3a445142016-03-12 00:01:38 +0100394 continue;
395
Andrew Lunnc8b09802016-06-04 21:16:57 +0200396 dsa_slave_destroy(ds->ports[port].netdev);
Andrew Lunn3a445142016-03-12 00:01:38 +0100397 }
398
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200399 /* Disable configuration of the CPU and DSA ports */
Vivien Didelot26895e22017-01-27 15:29:37 -0500400 for (port = 0; port < ds->num_ports; port++) {
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200401 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
402 continue;
Florian Fainelli293784a2017-01-26 10:45:52 -0800403 dsa_cpu_dsa_destroy(&ds->ports[port]);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200404
405 /* Clearing a bit which is not set does no harm */
406 ds->cpu_port_mask |= ~(1 << port);
407 ds->dsa_port_mask |= ~(1 << port);
Neil Armstrongcbc5d902015-10-06 15:40:32 +0100408 }
409
Vivien Didelot9d490b42016-08-23 12:38:56 -0400410 if (ds->slave_mii_bus && ds->ops->phy_read)
Andrew Lunne755e492016-06-04 21:17:04 +0200411 mdiobus_unregister(ds->slave_mii_bus);
Vivien Didelotf515f192017-02-03 13:20:20 -0500412
413 dsa_switch_unregister_notifier(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000414}
415
Thierry Redinge506d402014-10-01 13:59:00 +0200416#ifdef CONFIG_PM_SLEEP
Florian Fainelliea825e72016-08-18 15:30:12 -0700417int dsa_switch_suspend(struct dsa_switch *ds)
Florian Fainelli24462542014-09-18 17:31:22 -0700418{
419 int i, ret = 0;
420
421 /* Suspend slave network devices */
Vivien Didelot26895e22017-01-27 15:29:37 -0500422 for (i = 0; i < ds->num_ports; i++) {
Guenter Roeckd79d2102015-02-24 23:02:02 -0800423 if (!dsa_is_port_initialized(ds, i))
Florian Fainelli24462542014-09-18 17:31:22 -0700424 continue;
425
Andrew Lunnc8b09802016-06-04 21:16:57 +0200426 ret = dsa_slave_suspend(ds->ports[i].netdev);
Florian Fainelli24462542014-09-18 17:31:22 -0700427 if (ret)
428 return ret;
429 }
430
Vivien Didelot9d490b42016-08-23 12:38:56 -0400431 if (ds->ops->suspend)
432 ret = ds->ops->suspend(ds);
Florian Fainelli24462542014-09-18 17:31:22 -0700433
434 return ret;
435}
Florian Fainelliea825e72016-08-18 15:30:12 -0700436EXPORT_SYMBOL_GPL(dsa_switch_suspend);
Florian Fainelli24462542014-09-18 17:31:22 -0700437
Florian Fainelliea825e72016-08-18 15:30:12 -0700438int dsa_switch_resume(struct dsa_switch *ds)
Florian Fainelli24462542014-09-18 17:31:22 -0700439{
440 int i, ret = 0;
441
Vivien Didelot9d490b42016-08-23 12:38:56 -0400442 if (ds->ops->resume)
443 ret = ds->ops->resume(ds);
Florian Fainelli24462542014-09-18 17:31:22 -0700444
445 if (ret)
446 return ret;
447
448 /* Resume slave network devices */
Vivien Didelot26895e22017-01-27 15:29:37 -0500449 for (i = 0; i < ds->num_ports; i++) {
Guenter Roeckd79d2102015-02-24 23:02:02 -0800450 if (!dsa_is_port_initialized(ds, i))
Florian Fainelli24462542014-09-18 17:31:22 -0700451 continue;
452
Andrew Lunnc8b09802016-06-04 21:16:57 +0200453 ret = dsa_slave_resume(ds->ports[i].netdev);
Florian Fainelli24462542014-09-18 17:31:22 -0700454 if (ret)
455 return ret;
456 }
457
458 return 0;
459}
Florian Fainelliea825e72016-08-18 15:30:12 -0700460EXPORT_SYMBOL_GPL(dsa_switch_resume);
Thierry Redinge506d402014-10-01 13:59:00 +0200461#endif
Florian Fainelli24462542014-09-18 17:31:22 -0700462
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000463/* platform driver init and cleanup *****************************************/
464static int dev_is_class(struct device *dev, void *class)
465{
466 if (dev->class != NULL && !strcmp(dev->class->name, class))
467 return 1;
468
469 return 0;
470}
471
472static struct device *dev_find_class(struct device *parent, char *class)
473{
474 if (dev_is_class(parent, class)) {
475 get_device(parent);
476 return parent;
477 }
478
479 return device_find_child(parent, class, dev_is_class);
480}
481
Alexander Duyckb4d23942014-09-15 13:00:27 -0400482struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000483{
484 struct device *d;
485
486 d = dev_find_class(dev, "mdio_bus");
487 if (d != NULL) {
488 struct mii_bus *bus;
489
490 bus = to_mii_bus(d);
491 put_device(d);
492
493 return bus;
494 }
495
496 return NULL;
497}
Alexander Duyckb4d23942014-09-15 13:00:27 -0400498EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000499
Florian Fainelli14b89f32017-02-04 13:02:42 -0800500struct net_device *dsa_dev_to_net_device(struct device *dev)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000501{
502 struct device *d;
503
504 d = dev_find_class(dev, "net");
505 if (d != NULL) {
506 struct net_device *nd;
507
508 nd = to_net_dev(d);
509 dev_hold(nd);
510 put_device(d);
511
512 return nd;
513 }
514
515 return NULL;
516}
Florian Fainelli14b89f32017-02-04 13:02:42 -0800517EXPORT_SYMBOL_GPL(dsa_dev_to_net_device);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000518
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000519#ifdef CONFIG_OF
520static int dsa_of_setup_routing_table(struct dsa_platform_data *pd,
521 struct dsa_chip_data *cd,
Pavel Nakonechny30303812015-04-05 00:46:21 +0300522 int chip_index, int port_index,
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000523 struct device_node *link)
524{
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000525 const __be32 *reg;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000526 int link_sw_addr;
527 struct device_node *parent_sw;
528 int len;
529
530 parent_sw = of_get_parent(link);
531 if (!parent_sw)
532 return -EINVAL;
533
534 reg = of_get_property(parent_sw, "reg", &len);
535 if (!reg || (len != sizeof(*reg) * 2))
536 return -EINVAL;
537
Pavel Nakonechny30303812015-04-05 00:46:21 +0300538 /*
539 * Get the destination switch number from the second field of its 'reg'
540 * property, i.e. for "reg = <0x19 1>" sw_addr is '1'.
541 */
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000542 link_sw_addr = be32_to_cpup(reg + 1);
543
544 if (link_sw_addr >= pd->nr_chips)
545 return -EINVAL;
546
Pavel Nakonechny30303812015-04-05 00:46:21 +0300547 cd->rtable[link_sw_addr] = port_index;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000548
549 return 0;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000550}
551
Andrew Lunn1e72e6f2015-08-17 23:52:50 +0200552static int dsa_of_probe_links(struct dsa_platform_data *pd,
553 struct dsa_chip_data *cd,
554 int chip_index, int port_index,
555 struct device_node *port,
556 const char *port_name)
557{
558 struct device_node *link;
559 int link_index;
560 int ret;
561
562 for (link_index = 0;; link_index++) {
563 link = of_parse_phandle(port, "link", link_index);
564 if (!link)
565 break;
566
567 if (!strcmp(port_name, "dsa") && pd->nr_chips > 1) {
568 ret = dsa_of_setup_routing_table(pd, cd, chip_index,
569 port_index, link);
570 if (ret)
571 return ret;
572 }
573 }
574 return 0;
575}
576
Florian Fainelli21168242013-03-25 05:03:39 +0000577static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
578{
579 int i;
580 int port_index;
581
582 for (i = 0; i < pd->nr_chips; i++) {
583 port_index = 0;
Florian Fainelli5f64a7d2013-03-25 05:03:40 +0000584 while (port_index < DSA_MAX_PORTS) {
Fabian Frederick1f747142014-06-23 19:32:47 +0200585 kfree(pd->chip[i].port_names[port_index]);
Florian Fainelli5f64a7d2013-03-25 05:03:40 +0000586 port_index++;
587 }
Russell Kinge496ae62015-09-24 20:35:57 +0100588
589 /* Drop our reference to the MDIO bus device */
590 if (pd->chip[i].host_dev)
591 put_device(pd->chip[i].host_dev);
Florian Fainelli21168242013-03-25 05:03:39 +0000592 }
593 kfree(pd->chip);
594}
595
Florian Fainellif1a26a02015-03-05 12:35:04 -0800596static int dsa_of_probe(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000597{
Florian Fainellif1a26a02015-03-05 12:35:04 -0800598 struct device_node *np = dev->of_node;
Andrew Lunn1e72e6f2015-08-17 23:52:50 +0200599 struct device_node *child, *mdio, *ethernet, *port;
Andrew Lunn6bc6d0a2015-08-08 17:09:14 +0200600 struct mii_bus *mdio_bus, *mdio_bus_switch;
Florian Fainelli769a0202015-03-09 14:31:21 -0700601 struct net_device *ethernet_dev;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000602 struct dsa_platform_data *pd;
603 struct dsa_chip_data *cd;
604 const char *port_name;
605 int chip_index, port_index;
606 const unsigned int *sw_addr, *port_reg;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700607 u32 eeprom_len;
Florian Fainelli21168242013-03-25 05:03:39 +0000608 int ret;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000609
610 mdio = of_parse_phandle(np, "dsa,mii-bus", 0);
611 if (!mdio)
612 return -EINVAL;
613
614 mdio_bus = of_mdio_find_bus(mdio);
615 if (!mdio_bus)
Florian Fainellib324c072015-03-05 12:35:05 -0800616 return -EPROBE_DEFER;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000617
618 ethernet = of_parse_phandle(np, "dsa,ethernet", 0);
Russell Kinge496ae62015-09-24 20:35:57 +0100619 if (!ethernet) {
620 ret = -EINVAL;
621 goto out_put_mdio;
622 }
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000623
Florian Fainelli769a0202015-03-09 14:31:21 -0700624 ethernet_dev = of_find_net_device_by_node(ethernet);
Russell Kinge496ae62015-09-24 20:35:57 +0100625 if (!ethernet_dev) {
626 ret = -EPROBE_DEFER;
627 goto out_put_mdio;
628 }
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000629
630 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
Russell Kinge496ae62015-09-24 20:35:57 +0100631 if (!pd) {
632 ret = -ENOMEM;
Russell King9861f722015-09-24 20:36:33 +0100633 goto out_put_ethernet;
Russell Kinge496ae62015-09-24 20:35:57 +0100634 }
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000635
Florian Fainellif1a26a02015-03-05 12:35:04 -0800636 dev->platform_data = pd;
Florian Fainelli769a0202015-03-09 14:31:21 -0700637 pd->of_netdev = ethernet_dev;
Tobias Waldekranze04449f2015-02-05 14:54:09 +0100638 pd->nr_chips = of_get_available_child_count(np);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000639 if (pd->nr_chips > DSA_MAX_SWITCHES)
640 pd->nr_chips = DSA_MAX_SWITCHES;
641
Fabian Frederick6f2aed62014-11-14 19:38:23 +0100642 pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data),
643 GFP_KERNEL);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000644 if (!pd->chip) {
645 ret = -ENOMEM;
646 goto out_free;
647 }
648
Fabian Godehardtd1c0b472014-05-16 06:21:44 +0200649 chip_index = -1;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000650 for_each_available_child_of_node(np, child) {
Vivien Didelotd3902382016-07-06 20:03:54 -0400651 int i;
652
Fabian Godehardtd1c0b472014-05-16 06:21:44 +0200653 chip_index++;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000654 cd = &pd->chip[chip_index];
655
Florian Fainellifa981d92014-08-27 17:04:49 -0700656 cd->of_node = child;
Russell Kinge496ae62015-09-24 20:35:57 +0100657
Vivien Didelotd3902382016-07-06 20:03:54 -0400658 /* Initialize the routing table */
659 for (i = 0; i < DSA_MAX_SWITCHES; ++i)
660 cd->rtable[i] = DSA_RTABLE_NONE;
661
Russell Kinge496ae62015-09-24 20:35:57 +0100662 /* When assigning the host device, increment its refcount */
663 cd->host_dev = get_device(&mdio_bus->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000664
665 sw_addr = of_get_property(child, "reg", NULL);
666 if (!sw_addr)
667 continue;
668
669 cd->sw_addr = be32_to_cpup(sw_addr);
Florian Fainellic8cf89f2015-07-11 18:02:11 -0700670 if (cd->sw_addr >= PHY_MAX_ADDR)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000671 continue;
672
Guenter Roeck50d49642015-04-29 10:56:15 -0700673 if (!of_property_read_u32(child, "eeprom-length", &eeprom_len))
Guenter Roeck6793abb2014-10-29 10:45:01 -0700674 cd->eeprom_len = eeprom_len;
675
Andrew Lunn6bc6d0a2015-08-08 17:09:14 +0200676 mdio = of_parse_phandle(child, "mii-bus", 0);
677 if (mdio) {
678 mdio_bus_switch = of_mdio_find_bus(mdio);
679 if (!mdio_bus_switch) {
680 ret = -EPROBE_DEFER;
681 goto out_free_chip;
682 }
Russell Kinge496ae62015-09-24 20:35:57 +0100683
684 /* Drop the mdio_bus device ref, replacing the host
685 * device with the mdio_bus_switch device, keeping
686 * the refcount from of_mdio_find_bus() above.
687 */
688 put_device(cd->host_dev);
Andrew Lunn6bc6d0a2015-08-08 17:09:14 +0200689 cd->host_dev = &mdio_bus_switch->dev;
690 }
691
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000692 for_each_available_child_of_node(child, port) {
693 port_reg = of_get_property(port, "reg", NULL);
694 if (!port_reg)
695 continue;
696
697 port_index = be32_to_cpup(port_reg);
Florian Fainelli8f5063e2015-07-11 18:02:10 -0700698 if (port_index >= DSA_MAX_PORTS)
699 break;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000700
701 port_name = of_get_property(port, "label", NULL);
702 if (!port_name)
703 continue;
704
Florian Fainellibd474972014-08-27 17:04:50 -0700705 cd->port_dn[port_index] = port;
706
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000707 cd->port_names[port_index] = kstrdup(port_name,
708 GFP_KERNEL);
709 if (!cd->port_names[port_index]) {
710 ret = -ENOMEM;
711 goto out_free_chip;
712 }
713
Andrew Lunn1e72e6f2015-08-17 23:52:50 +0200714 ret = dsa_of_probe_links(pd, cd, chip_index,
715 port_index, port, port_name);
716 if (ret)
717 goto out_free_chip;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000718
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000719 }
720 }
721
Russell Kinge496ae62015-09-24 20:35:57 +0100722 /* The individual chips hold their own refcount on the mdio bus,
723 * so drop ours */
724 put_device(&mdio_bus->dev);
725
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000726 return 0;
727
728out_free_chip:
Florian Fainelli21168242013-03-25 05:03:39 +0000729 dsa_of_free_platform_data(pd);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000730out_free:
731 kfree(pd);
Florian Fainellif1a26a02015-03-05 12:35:04 -0800732 dev->platform_data = NULL;
Russell King9861f722015-09-24 20:36:33 +0100733out_put_ethernet:
734 put_device(&ethernet_dev->dev);
Russell Kinge496ae62015-09-24 20:35:57 +0100735out_put_mdio:
736 put_device(&mdio_bus->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000737 return ret;
738}
739
Florian Fainellif1a26a02015-03-05 12:35:04 -0800740static void dsa_of_remove(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000741{
Florian Fainellif1a26a02015-03-05 12:35:04 -0800742 struct dsa_platform_data *pd = dev->platform_data;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000743
Florian Fainellif1a26a02015-03-05 12:35:04 -0800744 if (!dev->of_node)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000745 return;
746
Florian Fainelli21168242013-03-25 05:03:39 +0000747 dsa_of_free_platform_data(pd);
Russell King9861f722015-09-24 20:36:33 +0100748 put_device(&pd->of_netdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000749 kfree(pd);
750}
751#else
Florian Fainellif1a26a02015-03-05 12:35:04 -0800752static inline int dsa_of_probe(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000753{
754 return 0;
755}
756
Florian Fainellif1a26a02015-03-05 12:35:04 -0800757static inline void dsa_of_remove(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000758{
759}
760#endif
761
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100762static int dsa_setup_dst(struct dsa_switch_tree *dst, struct net_device *dev,
763 struct device *parent, struct dsa_platform_data *pd)
Florian Fainellic86e59b2015-03-05 12:35:08 -0800764{
765 int i;
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100766 unsigned configured = 0;
Florian Fainellic86e59b2015-03-05 12:35:08 -0800767
768 dst->pd = pd;
769 dst->master_netdev = dev;
Florian Fainellic86e59b2015-03-05 12:35:08 -0800770 dst->cpu_port = -1;
771
772 for (i = 0; i < pd->nr_chips; i++) {
773 struct dsa_switch *ds;
774
775 ds = dsa_switch_setup(dst, i, parent, pd->chip[i].host_dev);
776 if (IS_ERR(ds)) {
777 netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n",
778 i, PTR_ERR(ds));
779 continue;
780 }
781
782 dst->ds[i] = ds;
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100783
784 ++configured;
Florian Fainellic86e59b2015-03-05 12:35:08 -0800785 }
786
787 /*
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100788 * If no switch was found, exit cleanly
789 */
790 if (!configured)
791 return -EPROBE_DEFER;
792
793 /*
Florian Fainellic86e59b2015-03-05 12:35:08 -0800794 * If we use a tagging format that doesn't have an ethertype
795 * field, make sure that all packets from this point on get
796 * sent to the tag format's receive function.
797 */
798 wmb();
799 dev->dsa_ptr = (void *)dst;
800
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100801 return 0;
Florian Fainellic86e59b2015-03-05 12:35:08 -0800802}
803
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000804static int dsa_probe(struct platform_device *pdev)
805{
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000806 struct dsa_platform_data *pd = pdev->dev.platform_data;
807 struct net_device *dev;
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000808 struct dsa_switch_tree *dst;
Florian Fainellic86e59b2015-03-05 12:35:08 -0800809 int ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000810
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000811 if (pdev->dev.of_node) {
Florian Fainellif1a26a02015-03-05 12:35:04 -0800812 ret = dsa_of_probe(&pdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000813 if (ret)
814 return ret;
815
816 pd = pdev->dev.platform_data;
817 }
818
Florian Fainelli769a0202015-03-09 14:31:21 -0700819 if (pd == NULL || (pd->netdev == NULL && pd->of_netdev == NULL))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000820 return -EINVAL;
821
Florian Fainelli769a0202015-03-09 14:31:21 -0700822 if (pd->of_netdev) {
823 dev = pd->of_netdev;
824 dev_hold(dev);
825 } else {
Florian Fainelli14b89f32017-02-04 13:02:42 -0800826 dev = dsa_dev_to_net_device(pd->netdev);
Florian Fainelli769a0202015-03-09 14:31:21 -0700827 }
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000828 if (dev == NULL) {
Florian Fainellib324c072015-03-05 12:35:05 -0800829 ret = -EPROBE_DEFER;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000830 goto out;
831 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000832
833 if (dev->dsa_ptr != NULL) {
834 dev_put(dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000835 ret = -EEXIST;
836 goto out;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000837 }
838
Neil Armstrongd4ac35d2015-10-06 15:40:37 +0100839 dst = devm_kzalloc(&pdev->dev, sizeof(*dst), GFP_KERNEL);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000840 if (dst == NULL) {
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000841 dev_put(dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000842 ret = -ENOMEM;
843 goto out;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000844 }
845
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000846 platform_set_drvdata(pdev, dst);
847
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100848 ret = dsa_setup_dst(dst, dev, &pdev->dev, pd);
Neil Armstrong679fb462015-12-07 13:57:34 +0100849 if (ret) {
850 dev_put(dev);
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100851 goto out;
Neil Armstrong679fb462015-12-07 13:57:34 +0100852 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000853
854 return 0;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000855
856out:
Florian Fainellif1a26a02015-03-05 12:35:04 -0800857 dsa_of_remove(&pdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000858
859 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000860}
861
Florian Fainellic86e59b2015-03-05 12:35:08 -0800862static void dsa_remove_dst(struct dsa_switch_tree *dst)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000863{
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000864 int i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000865
Neil Armstrong04761892016-03-08 10:36:20 +0100866 dst->master_netdev->dsa_ptr = NULL;
867
868 /* If we used a tagging format that doesn't have an ethertype
869 * field, make sure that all packets from this point get sent
870 * without the tag and go through the regular receive path.
871 */
872 wmb();
873
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000874 for (i = 0; i < dst->pd->nr_chips; i++) {
875 struct dsa_switch *ds = dst->ds[i];
876
Neil Armstrongd4ac35d2015-10-06 15:40:37 +0100877 if (ds)
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000878 dsa_switch_destroy(ds);
879 }
Neil Armstrong679fb462015-12-07 13:57:34 +0100880
Vivien Didelot9520ed82017-01-17 20:41:39 -0500881 dsa_cpu_port_ethtool_restore(dst->cpu_switch);
Florian Fainelli0c73c522016-06-07 16:32:42 -0700882
Neil Armstrong679fb462015-12-07 13:57:34 +0100883 dev_put(dst->master_netdev);
Florian Fainellic86e59b2015-03-05 12:35:08 -0800884}
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000885
Florian Fainellic86e59b2015-03-05 12:35:08 -0800886static int dsa_remove(struct platform_device *pdev)
887{
888 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
889
890 dsa_remove_dst(dst);
Florian Fainellif1a26a02015-03-05 12:35:04 -0800891 dsa_of_remove(&pdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000892
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000893 return 0;
894}
895
896static void dsa_shutdown(struct platform_device *pdev)
897{
898}
899
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700900static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
901 struct packet_type *pt, struct net_device *orig_dev)
902{
903 struct dsa_switch_tree *dst = dev->dsa_ptr;
Florian Fainellia86d8be2017-04-08 08:55:23 -0700904 struct sk_buff *nskb = NULL;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700905
906 if (unlikely(dst == NULL)) {
907 kfree_skb(skb);
908 return 0;
909 }
910
Florian Fainelli16c5dcb2017-04-08 08:55:22 -0700911 skb = skb_unshare(skb, GFP_ATOMIC);
912 if (!skb)
913 return 0;
914
Florian Fainellia86d8be2017-04-08 08:55:23 -0700915 nskb = dst->rcv(skb, dev, pt, orig_dev);
916 if (!nskb) {
917 kfree_skb(skb);
918 return 0;
919 }
920
921 skb = nskb;
922 skb_push(skb, ETH_HLEN);
923 skb->pkt_type = PACKET_HOST;
924 skb->protocol = eth_type_trans(skb, skb->dev);
925
926 skb->dev->stats.rx_packets++;
927 skb->dev->stats.rx_bytes += skb->len;
928
929 netif_receive_skb(skb);
930
931 return 0;
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700932}
933
Florian Fainelli61b73632014-08-29 12:42:07 -0700934static struct packet_type dsa_pack_type __read_mostly = {
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700935 .type = cpu_to_be16(ETH_P_XDSA),
936 .func = dsa_switch_rcv,
937};
938
Florian Fainelli24462542014-09-18 17:31:22 -0700939#ifdef CONFIG_PM_SLEEP
940static int dsa_suspend(struct device *d)
941{
942 struct platform_device *pdev = to_platform_device(d);
943 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
944 int i, ret = 0;
945
946 for (i = 0; i < dst->pd->nr_chips; i++) {
947 struct dsa_switch *ds = dst->ds[i];
948
949 if (ds != NULL)
950 ret = dsa_switch_suspend(ds);
951 }
952
953 return ret;
954}
955
956static int dsa_resume(struct device *d)
957{
958 struct platform_device *pdev = to_platform_device(d);
959 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
960 int i, ret = 0;
961
962 for (i = 0; i < dst->pd->nr_chips; i++) {
963 struct dsa_switch *ds = dst->ds[i];
964
965 if (ds != NULL)
966 ret = dsa_switch_resume(ds);
967 }
968
969 return ret;
970}
971#endif
972
973static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume);
974
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000975static const struct of_device_id dsa_of_match_table[] = {
976 { .compatible = "marvell,dsa", },
977 {}
978};
979MODULE_DEVICE_TABLE(of, dsa_of_match_table);
980
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000981static struct platform_driver dsa_driver = {
982 .probe = dsa_probe,
983 .remove = dsa_remove,
984 .shutdown = dsa_shutdown,
985 .driver = {
986 .name = "dsa",
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000987 .of_match_table = dsa_of_match_table,
Florian Fainelli24462542014-09-18 17:31:22 -0700988 .pm = &dsa_pm_ops,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000989 },
990};
991
992static int __init dsa_init_module(void)
993{
Ben Hutchings7df899c2011-11-25 14:35:02 +0000994 int rc;
995
Vivien Didelot88e4f0c2017-02-03 13:20:16 -0500996 rc = dsa_slave_register_notifier();
997 if (rc)
998 return rc;
Florian Fainellib73adef2015-02-24 13:15:33 -0800999
Ben Hutchings7df899c2011-11-25 14:35:02 +00001000 rc = platform_driver_register(&dsa_driver);
1001 if (rc)
1002 return rc;
1003
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001004 dev_add_pack(&dsa_pack_type);
1005
Ben Hutchings7df899c2011-11-25 14:35:02 +00001006 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001007}
1008module_init(dsa_init_module);
1009
1010static void __exit dsa_cleanup_module(void)
1011{
Vivien Didelot88e4f0c2017-02-03 13:20:16 -05001012 dsa_slave_unregister_notifier();
Florian Fainelli3e8a72d2014-08-27 17:04:46 -07001013 dev_remove_pack(&dsa_pack_type);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001014 platform_driver_unregister(&dsa_driver);
1015}
1016module_exit(dsa_cleanup_module);
1017
Rusty Russell577d6a72011-01-24 14:32:52 -06001018MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001019MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
1020MODULE_LICENSE("GPL");
1021MODULE_ALIAS("platform:dsa");