blob: 6cad15da58922ad06aa663d54aebdd6fff92e0ae [file] [log] [blame]
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00001/*
2 * net/dsa/dsa.c - Hardware switch handling
Lennert Buytenheke84665c2009-03-20 09:52:09 +00003 * Copyright (c) 2008-2009 Marvell Semiconductor
Florian Fainelli5e95329b2013-03-22 10:50:50 +00004 * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
Guenter Roeck51579c32014-10-29 10:44:58 -070012#include <linux/device.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000013#include <linux/list.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000014#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Paul Gortmaker3a9a2312011-05-27 09:12:25 -040016#include <linux/module.h>
Florian Fainelli5e95329b2013-03-22 10:50:50 +000017#include <linux/of.h>
18#include <linux/of_mdio.h>
19#include <linux/of_platform.h>
Florian Fainelli769a0202015-03-09 14:31:21 -070020#include <linux/of_net.h>
Andrew Lunncc30c162015-11-20 03:56:23 +010021#include <linux/of_gpio.h>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020022#include <linux/netdevice.h>
Guenter Roeck51579c32014-10-29 10:44:58 -070023#include <linux/sysfs.h>
Neil Armstrongcbc5d902015-10-06 15:40:32 +010024#include <linux/phy_fixed.h>
Arnd Bergmann85beabf2015-11-24 12:34:49 +010025#include <linux/gpio/consumer.h>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020026#include <net/dsa.h>
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000027#include "dsa_priv.h"
28
Andrew Lunn39a7f2a2016-06-04 21:17:03 +020029static struct sk_buff *dsa_slave_notag_xmit(struct sk_buff *skb,
30 struct net_device *dev)
31{
32 /* Just return the original SKB */
33 return skb;
34}
35
36static const struct dsa_device_ops none_ops = {
37 .xmit = dsa_slave_notag_xmit,
38 .rcv = NULL,
39};
40
41const struct dsa_device_ops *dsa_device_ops[DSA_TAG_LAST] = {
42#ifdef CONFIG_NET_DSA_TAG_DSA
43 [DSA_TAG_PROTO_DSA] = &dsa_netdev_ops,
44#endif
45#ifdef CONFIG_NET_DSA_TAG_EDSA
46 [DSA_TAG_PROTO_EDSA] = &edsa_netdev_ops,
47#endif
48#ifdef CONFIG_NET_DSA_TAG_TRAILER
49 [DSA_TAG_PROTO_TRAILER] = &trailer_netdev_ops,
50#endif
51#ifdef CONFIG_NET_DSA_TAG_BRCM
52 [DSA_TAG_PROTO_BRCM] = &brcm_netdev_ops,
53#endif
John Crispincafdc452016-09-15 16:26:40 +020054#ifdef CONFIG_NET_DSA_TAG_QCA
55 [DSA_TAG_PROTO_QCA] = &qca_netdev_ops,
56#endif
Sean Wang5cd89852017-04-07 16:45:06 +080057#ifdef CONFIG_NET_DSA_TAG_MTK
58 [DSA_TAG_PROTO_MTK] = &mtk_netdev_ops,
59#endif
Andrew Lunn39a7f2a2016-06-04 21:17:03 +020060 [DSA_TAG_PROTO_NONE] = &none_ops,
61};
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000062
63/* switch driver registration ***********************************************/
64static DEFINE_MUTEX(dsa_switch_drivers_mutex);
65static LIST_HEAD(dsa_switch_drivers);
66
Florian Fainelliab3d4082017-01-08 14:52:07 -080067void register_switch_driver(struct dsa_switch_driver *drv)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000068{
69 mutex_lock(&dsa_switch_drivers_mutex);
Florian Fainelliab3d4082017-01-08 14:52:07 -080070 list_add_tail(&drv->list, &dsa_switch_drivers);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000071 mutex_unlock(&dsa_switch_drivers_mutex);
72}
Ben Hutchingsad293b82011-11-25 14:34:07 +000073EXPORT_SYMBOL_GPL(register_switch_driver);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000074
Florian Fainelliab3d4082017-01-08 14:52:07 -080075void unregister_switch_driver(struct dsa_switch_driver *drv)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000076{
77 mutex_lock(&dsa_switch_drivers_mutex);
Florian Fainelliab3d4082017-01-08 14:52:07 -080078 list_del_init(&drv->list);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000079 mutex_unlock(&dsa_switch_drivers_mutex);
80}
Ben Hutchingsad293b82011-11-25 14:34:07 +000081EXPORT_SYMBOL_GPL(unregister_switch_driver);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000082
Florian Fainellia82f67a2017-01-08 14:52:08 -080083static const struct dsa_switch_ops *
Andrew Lunnbbb8d792016-04-13 02:40:39 +020084dsa_switch_probe(struct device *parent, struct device *host_dev, int sw_addr,
Vivien Didelot0209d142016-04-17 13:23:55 -040085 const char **_name, void **priv)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000086{
Florian Fainellia82f67a2017-01-08 14:52:08 -080087 const struct dsa_switch_ops *ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000088 struct list_head *list;
Vivien Didelot0209d142016-04-17 13:23:55 -040089 const char *name;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000090
91 ret = NULL;
92 name = NULL;
93
94 mutex_lock(&dsa_switch_drivers_mutex);
95 list_for_each(list, &dsa_switch_drivers) {
Florian Fainellia82f67a2017-01-08 14:52:08 -080096 const struct dsa_switch_ops *ops;
Florian Fainelliab3d4082017-01-08 14:52:07 -080097 struct dsa_switch_driver *drv;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +000098
Florian Fainelliab3d4082017-01-08 14:52:07 -080099 drv = list_entry(list, struct dsa_switch_driver, list);
100 ops = drv->ops;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000101
Vivien Didelot9d490b42016-08-23 12:38:56 -0400102 name = ops->probe(parent, host_dev, sw_addr, priv);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000103 if (name != NULL) {
Vivien Didelot9d490b42016-08-23 12:38:56 -0400104 ret = ops;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000105 break;
106 }
107 }
108 mutex_unlock(&dsa_switch_drivers_mutex);
109
110 *_name = name;
111
112 return ret;
113}
114
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000115/* basic switch operations **************************************************/
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200116int dsa_cpu_dsa_setup(struct dsa_switch *ds, struct device *dev,
Florian Fainelli293784a2017-01-26 10:45:52 -0800117 struct dsa_port *dport, int port)
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200118{
Florian Fainelli293784a2017-01-26 10:45:52 -0800119 struct device_node *port_dn = dport->dn;
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200120 struct phy_device *phydev;
121 int ret, mode;
122
123 if (of_phy_is_fixed_link(port_dn)) {
124 ret = of_phy_register_fixed_link(port_dn);
125 if (ret) {
126 dev_err(dev, "failed to register fixed PHY\n");
127 return ret;
128 }
129 phydev = of_phy_find_device(port_dn);
130
131 mode = of_get_phy_mode(port_dn);
132 if (mode < 0)
133 mode = PHY_INTERFACE_MODE_NA;
134 phydev->interface = mode;
135
136 genphy_config_init(phydev);
137 genphy_read_status(phydev);
Vivien Didelot9d490b42016-08-23 12:38:56 -0400138 if (ds->ops->adjust_link)
139 ds->ops->adjust_link(ds, port, phydev);
Johan Hovoldfd05d7b2016-11-24 19:21:27 +0100140
141 put_device(&phydev->mdio.dev);
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200142 }
143
144 return 0;
145}
146
147static int dsa_cpu_dsa_setups(struct dsa_switch *ds, struct device *dev)
Andrew Lunn39b0c702015-08-31 15:56:49 +0200148{
Florian Fainelli293784a2017-01-26 10:45:52 -0800149 struct dsa_port *dport;
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200150 int ret, port;
Andrew Lunn39b0c702015-08-31 15:56:49 +0200151
Vivien Didelot26895e22017-01-27 15:29:37 -0500152 for (port = 0; port < ds->num_ports; port++) {
Andrew Lunn39b0c702015-08-31 15:56:49 +0200153 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
154 continue;
155
Florian Fainelli293784a2017-01-26 10:45:52 -0800156 dport = &ds->ports[port];
157 ret = dsa_cpu_dsa_setup(ds, dev, dport, port);
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200158 if (ret)
159 return ret;
Andrew Lunn39b0c702015-08-31 15:56:49 +0200160 }
161 return 0;
162}
163
Andrew Lunn39a7f2a2016-06-04 21:17:03 +0200164const struct dsa_device_ops *dsa_resolve_tag_protocol(int tag_protocol)
165{
166 const struct dsa_device_ops *ops;
167
168 if (tag_protocol >= DSA_TAG_LAST)
169 return ERR_PTR(-EINVAL);
170 ops = dsa_device_ops[tag_protocol];
171
172 if (!ops)
173 return ERR_PTR(-ENOPROTOOPT);
174
175 return ops;
176}
177
Florian Fainelli0c73c522016-06-07 16:32:42 -0700178int dsa_cpu_port_ethtool_setup(struct dsa_switch *ds)
179{
180 struct net_device *master;
181 struct ethtool_ops *cpu_ops;
182
183 master = ds->dst->master_netdev;
184 if (ds->master_netdev)
185 master = ds->master_netdev;
186
187 cpu_ops = devm_kzalloc(ds->dev, sizeof(*cpu_ops), GFP_KERNEL);
188 if (!cpu_ops)
189 return -ENOMEM;
190
191 memcpy(&ds->dst->master_ethtool_ops, master->ethtool_ops,
192 sizeof(struct ethtool_ops));
193 ds->dst->master_orig_ethtool_ops = master->ethtool_ops;
194 memcpy(cpu_ops, &ds->dst->master_ethtool_ops,
195 sizeof(struct ethtool_ops));
196 dsa_cpu_port_ethtool_init(cpu_ops);
197 master->ethtool_ops = cpu_ops;
198
199 return 0;
200}
201
202void dsa_cpu_port_ethtool_restore(struct dsa_switch *ds)
203{
204 struct net_device *master;
205
206 master = ds->dst->master_netdev;
207 if (ds->master_netdev)
208 master = ds->master_netdev;
209
210 master->ethtool_ops = ds->dst->master_orig_ethtool_ops;
211}
212
Florian Fainellidf197192015-03-05 12:35:06 -0800213static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000214{
Florian Fainellia82f67a2017-01-08 14:52:08 -0800215 const struct dsa_switch_ops *ops = ds->ops;
Florian Fainellidf197192015-03-05 12:35:06 -0800216 struct dsa_switch_tree *dst = ds->dst;
Andrew Lunnff049552016-05-10 23:27:24 +0200217 struct dsa_chip_data *cd = ds->cd;
Florian Fainellif9bf5a22013-01-21 09:58:51 +0000218 bool valid_name_found = false;
Florian Fainellidf197192015-03-05 12:35:06 -0800219 int index = ds->index;
220 int i, ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000221
222 /*
223 * Validate supplied switch configuration.
224 */
Vivien Didelot26895e22017-01-27 15:29:37 -0500225 for (i = 0; i < ds->num_ports; i++) {
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000226 char *name;
227
Andrew Lunnff049552016-05-10 23:27:24 +0200228 name = cd->port_names[i];
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000229 if (name == NULL)
230 continue;
231
232 if (!strcmp(name, "cpu")) {
Andrew Lunn23e3d612017-01-22 22:16:45 +0100233 if (dst->cpu_switch) {
Joe Perchesa2ae6002014-11-09 16:32:46 -0800234 netdev_err(dst->master_netdev,
235 "multiple cpu ports?!\n");
Vivien Didelota896eee2017-01-03 14:31:49 -0500236 return -EINVAL;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000237 }
Vivien Didelotb22de492017-01-17 20:41:38 -0500238 dst->cpu_switch = ds;
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000239 dst->cpu_port = i;
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200240 ds->cpu_port_mask |= 1 << i;
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000241 } else if (!strcmp(name, "dsa")) {
242 ds->dsa_port_mask |= 1 << i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000243 } else {
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200244 ds->enabled_port_mask |= 1 << i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000245 }
Florian Fainellif9bf5a22013-01-21 09:58:51 +0000246 valid_name_found = true;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000247 }
248
Vivien Didelot26895e22017-01-27 15:29:37 -0500249 if (!valid_name_found && i == ds->num_ports)
Vivien Didelota896eee2017-01-03 14:31:49 -0500250 return -EINVAL;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000251
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700252 /* Make the built-in MII bus mask match the number of ports,
253 * switch drivers can override this later
254 */
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200255 ds->phys_mii_mask = ds->enabled_port_mask;
Florian Fainelli0d8bcdd2014-08-27 17:04:51 -0700256
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000257 /*
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000258 * If the CPU connects to this switch, set the switch tree
259 * tagging protocol to the preferred tagging format of this
260 * switch.
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000261 */
Vivien Didelotb22de492017-01-17 20:41:38 -0500262 if (dst->cpu_switch == ds) {
Andrew Lunn7b314362016-08-22 16:01:01 +0200263 enum dsa_tag_protocol tag_protocol;
264
Vivien Didelot9d490b42016-08-23 12:38:56 -0400265 tag_protocol = ops->get_tag_protocol(ds);
Andrew Lunn7b314362016-08-22 16:01:01 +0200266 dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol);
Vivien Didelota896eee2017-01-03 14:31:49 -0500267 if (IS_ERR(dst->tag_ops))
268 return PTR_ERR(dst->tag_ops);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000269
Andrew Lunn39a7f2a2016-06-04 21:17:03 +0200270 dst->rcv = dst->tag_ops->rcv;
Alexander Duyck50753142014-09-15 13:00:19 -0400271 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000272
Andrew Lunn66472fc2016-06-04 21:17:00 +0200273 memcpy(ds->rtable, cd->rtable, sizeof(ds->rtable));
274
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000275 /*
276 * Do basic register setup.
277 */
Vivien Didelot9d490b42016-08-23 12:38:56 -0400278 ret = ops->setup(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000279 if (ret < 0)
Vivien Didelota896eee2017-01-03 14:31:49 -0500280 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000281
Vivien Didelotf515f192017-02-03 13:20:20 -0500282 ret = dsa_switch_register_notifier(ds);
283 if (ret)
284 return ret;
285
John Crispin092183d2016-09-19 15:28:01 +0200286 if (ops->set_addr) {
287 ret = ops->set_addr(ds, dst->master_netdev->dev_addr);
288 if (ret < 0)
Vivien Didelota896eee2017-01-03 14:31:49 -0500289 return ret;
John Crispin092183d2016-09-19 15:28:01 +0200290 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000291
Vivien Didelot9d490b42016-08-23 12:38:56 -0400292 if (!ds->slave_mii_bus && ops->phy_read) {
Andrew Lunne755e492016-06-04 21:17:04 +0200293 ds->slave_mii_bus = devm_mdiobus_alloc(parent);
Vivien Didelota896eee2017-01-03 14:31:49 -0500294 if (!ds->slave_mii_bus)
295 return -ENOMEM;
Andrew Lunne755e492016-06-04 21:17:04 +0200296 dsa_slave_mii_bus_init(ds);
297
298 ret = mdiobus_register(ds->slave_mii_bus);
299 if (ret < 0)
Vivien Didelota896eee2017-01-03 14:31:49 -0500300 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000301 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000302
303 /*
304 * Create network devices for physical switch ports.
305 */
Vivien Didelot26895e22017-01-27 15:29:37 -0500306 for (i = 0; i < ds->num_ports; i++) {
Andrew Lunn189b0d92016-06-04 21:16:58 +0200307 ds->ports[i].dn = cd->port_dn[i];
308
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200309 if (!(ds->enabled_port_mask & (1 << i)))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000310 continue;
311
Andrew Lunnff049552016-05-10 23:27:24 +0200312 ret = dsa_slave_create(ds, parent, i, cd->port_names[i]);
Vivien Didelota896eee2017-01-03 14:31:49 -0500313 if (ret < 0)
Russell Kingd25b8e72015-10-03 18:09:07 +0100314 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 +0200315 index, i, cd->port_names[i], ret);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000316 }
317
Andrew Lunn39b0c702015-08-31 15:56:49 +0200318 /* Perform configuration of the CPU and DSA ports */
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200319 ret = dsa_cpu_dsa_setups(ds, parent);
Vivien Didelota896eee2017-01-03 14:31:49 -0500320 if (ret < 0)
Andrew Lunn39b0c702015-08-31 15:56:49 +0200321 netdev_err(dst->master_netdev, "[%d] : can't configure CPU and DSA ports\n",
322 index);
Andrew Lunn39b0c702015-08-31 15:56:49 +0200323
Florian Fainelli0c73c522016-06-07 16:32:42 -0700324 ret = dsa_cpu_port_ethtool_setup(ds);
325 if (ret)
326 return ret;
327
Vivien Didelota896eee2017-01-03 14:31:49 -0500328 return 0;
Florian Fainellidf197192015-03-05 12:35:06 -0800329}
330
331static struct dsa_switch *
332dsa_switch_setup(struct dsa_switch_tree *dst, int index,
333 struct device *parent, struct device *host_dev)
334{
Andrew Lunnff049552016-05-10 23:27:24 +0200335 struct dsa_chip_data *cd = dst->pd->chip + index;
Florian Fainellia82f67a2017-01-08 14:52:08 -0800336 const struct dsa_switch_ops *ops;
Florian Fainellidf197192015-03-05 12:35:06 -0800337 struct dsa_switch *ds;
338 int ret;
Vivien Didelot0209d142016-04-17 13:23:55 -0400339 const char *name;
Andrew Lunn7543a6d2016-04-13 02:40:40 +0200340 void *priv;
Florian Fainellidf197192015-03-05 12:35:06 -0800341
342 /*
343 * Probe for switch model.
344 */
Vivien Didelot9d490b42016-08-23 12:38:56 -0400345 ops = dsa_switch_probe(parent, host_dev, cd->sw_addr, &name, &priv);
346 if (!ops) {
Florian Fainellidf197192015-03-05 12:35:06 -0800347 netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n",
348 index);
349 return ERR_PTR(-EINVAL);
350 }
351 netdev_info(dst->master_netdev, "[%d]: detected a %s switch\n",
352 index, name);
353
354
355 /*
356 * Allocate and initialise switch state.
357 */
Vivien Didelota0c02162017-01-27 15:29:36 -0500358 ds = dsa_switch_alloc(parent, DSA_MAX_PORTS);
359 if (!ds)
Florian Fainelli24595342015-05-29 10:29:46 -0700360 return ERR_PTR(-ENOMEM);
Florian Fainellidf197192015-03-05 12:35:06 -0800361
362 ds->dst = dst;
363 ds->index = index;
Andrew Lunnff049552016-05-10 23:27:24 +0200364 ds->cd = cd;
Vivien Didelot9d490b42016-08-23 12:38:56 -0400365 ds->ops = ops;
Andrew Lunn7543a6d2016-04-13 02:40:40 +0200366 ds->priv = priv;
Florian Fainellidf197192015-03-05 12:35:06 -0800367
368 ret = dsa_switch_setup_one(ds, parent);
369 if (ret)
Florian Fainelli24595342015-05-29 10:29:46 -0700370 return ERR_PTR(ret);
Florian Fainellidf197192015-03-05 12:35:06 -0800371
372 return ds;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000373}
374
Florian Fainelli293784a2017-01-26 10:45:52 -0800375void dsa_cpu_dsa_destroy(struct dsa_port *port)
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200376{
Florian Fainelli293784a2017-01-26 10:45:52 -0800377 struct device_node *port_dn = port->dn;
378
Johan Hovold3f650472016-11-28 19:24:55 +0100379 if (of_phy_is_fixed_link(port_dn))
380 of_phy_deregister_fixed_link(port_dn);
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200381}
382
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000383static void dsa_switch_destroy(struct dsa_switch *ds)
384{
Neil Armstrongcbc5d902015-10-06 15:40:32 +0100385 int port;
386
Andrew Lunn3a445142016-03-12 00:01:38 +0100387 /* Destroy network devices for physical switch ports. */
Vivien Didelot26895e22017-01-27 15:29:37 -0500388 for (port = 0; port < ds->num_ports; port++) {
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200389 if (!(ds->enabled_port_mask & (1 << port)))
Andrew Lunn3a445142016-03-12 00:01:38 +0100390 continue;
391
Andrew Lunnc8b09802016-06-04 21:16:57 +0200392 if (!ds->ports[port].netdev)
Andrew Lunn3a445142016-03-12 00:01:38 +0100393 continue;
394
Andrew Lunnc8b09802016-06-04 21:16:57 +0200395 dsa_slave_destroy(ds->ports[port].netdev);
Andrew Lunn3a445142016-03-12 00:01:38 +0100396 }
397
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200398 /* Disable configuration of the CPU and DSA ports */
Vivien Didelot26895e22017-01-27 15:29:37 -0500399 for (port = 0; port < ds->num_ports; port++) {
Andrew Lunn9b8e8952016-06-04 21:17:01 +0200400 if (!(dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)))
401 continue;
Florian Fainelli293784a2017-01-26 10:45:52 -0800402 dsa_cpu_dsa_destroy(&ds->ports[port]);
Andrew Lunn83c0afa2016-06-04 21:17:07 +0200403
404 /* Clearing a bit which is not set does no harm */
405 ds->cpu_port_mask |= ~(1 << port);
406 ds->dsa_port_mask |= ~(1 << port);
Neil Armstrongcbc5d902015-10-06 15:40:32 +0100407 }
408
Vivien Didelot9d490b42016-08-23 12:38:56 -0400409 if (ds->slave_mii_bus && ds->ops->phy_read)
Andrew Lunne755e492016-06-04 21:17:04 +0200410 mdiobus_unregister(ds->slave_mii_bus);
Vivien Didelotf515f192017-02-03 13:20:20 -0500411
412 dsa_switch_unregister_notifier(ds);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000413}
414
Thierry Redinge506d402014-10-01 13:59:00 +0200415#ifdef CONFIG_PM_SLEEP
Florian Fainelliea825e72016-08-18 15:30:12 -0700416int dsa_switch_suspend(struct dsa_switch *ds)
Florian Fainelli24462542014-09-18 17:31:22 -0700417{
418 int i, ret = 0;
419
420 /* Suspend slave network devices */
Vivien Didelot26895e22017-01-27 15:29:37 -0500421 for (i = 0; i < ds->num_ports; i++) {
Guenter Roeckd79d2102015-02-24 23:02:02 -0800422 if (!dsa_is_port_initialized(ds, i))
Florian Fainelli24462542014-09-18 17:31:22 -0700423 continue;
424
Andrew Lunnc8b09802016-06-04 21:16:57 +0200425 ret = dsa_slave_suspend(ds->ports[i].netdev);
Florian Fainelli24462542014-09-18 17:31:22 -0700426 if (ret)
427 return ret;
428 }
429
Vivien Didelot9d490b42016-08-23 12:38:56 -0400430 if (ds->ops->suspend)
431 ret = ds->ops->suspend(ds);
Florian Fainelli24462542014-09-18 17:31:22 -0700432
433 return ret;
434}
Florian Fainelliea825e72016-08-18 15:30:12 -0700435EXPORT_SYMBOL_GPL(dsa_switch_suspend);
Florian Fainelli24462542014-09-18 17:31:22 -0700436
Florian Fainelliea825e72016-08-18 15:30:12 -0700437int dsa_switch_resume(struct dsa_switch *ds)
Florian Fainelli24462542014-09-18 17:31:22 -0700438{
439 int i, ret = 0;
440
Vivien Didelot9d490b42016-08-23 12:38:56 -0400441 if (ds->ops->resume)
442 ret = ds->ops->resume(ds);
Florian Fainelli24462542014-09-18 17:31:22 -0700443
444 if (ret)
445 return ret;
446
447 /* Resume slave network devices */
Vivien Didelot26895e22017-01-27 15:29:37 -0500448 for (i = 0; i < ds->num_ports; i++) {
Guenter Roeckd79d2102015-02-24 23:02:02 -0800449 if (!dsa_is_port_initialized(ds, i))
Florian Fainelli24462542014-09-18 17:31:22 -0700450 continue;
451
Andrew Lunnc8b09802016-06-04 21:16:57 +0200452 ret = dsa_slave_resume(ds->ports[i].netdev);
Florian Fainelli24462542014-09-18 17:31:22 -0700453 if (ret)
454 return ret;
455 }
456
457 return 0;
458}
Florian Fainelliea825e72016-08-18 15:30:12 -0700459EXPORT_SYMBOL_GPL(dsa_switch_resume);
Thierry Redinge506d402014-10-01 13:59:00 +0200460#endif
Florian Fainelli24462542014-09-18 17:31:22 -0700461
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000462/* platform driver init and cleanup *****************************************/
463static int dev_is_class(struct device *dev, void *class)
464{
465 if (dev->class != NULL && !strcmp(dev->class->name, class))
466 return 1;
467
468 return 0;
469}
470
471static struct device *dev_find_class(struct device *parent, char *class)
472{
473 if (dev_is_class(parent, class)) {
474 get_device(parent);
475 return parent;
476 }
477
478 return device_find_child(parent, class, dev_is_class);
479}
480
Alexander Duyckb4d23942014-09-15 13:00:27 -0400481struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000482{
483 struct device *d;
484
485 d = dev_find_class(dev, "mdio_bus");
486 if (d != NULL) {
487 struct mii_bus *bus;
488
489 bus = to_mii_bus(d);
490 put_device(d);
491
492 return bus;
493 }
494
495 return NULL;
496}
Alexander Duyckb4d23942014-09-15 13:00:27 -0400497EXPORT_SYMBOL_GPL(dsa_host_dev_to_mii_bus);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000498
Florian Fainelli14b89f32017-02-04 13:02:42 -0800499struct net_device *dsa_dev_to_net_device(struct device *dev)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000500{
501 struct device *d;
502
503 d = dev_find_class(dev, "net");
504 if (d != NULL) {
505 struct net_device *nd;
506
507 nd = to_net_dev(d);
508 dev_hold(nd);
509 put_device(d);
510
511 return nd;
512 }
513
514 return NULL;
515}
Florian Fainelli14b89f32017-02-04 13:02:42 -0800516EXPORT_SYMBOL_GPL(dsa_dev_to_net_device);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000517
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000518#ifdef CONFIG_OF
519static int dsa_of_setup_routing_table(struct dsa_platform_data *pd,
520 struct dsa_chip_data *cd,
Pavel Nakonechny30303812015-04-05 00:46:21 +0300521 int chip_index, int port_index,
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000522 struct device_node *link)
523{
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000524 const __be32 *reg;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000525 int link_sw_addr;
526 struct device_node *parent_sw;
527 int len;
528
529 parent_sw = of_get_parent(link);
530 if (!parent_sw)
531 return -EINVAL;
532
533 reg = of_get_property(parent_sw, "reg", &len);
534 if (!reg || (len != sizeof(*reg) * 2))
535 return -EINVAL;
536
Pavel Nakonechny30303812015-04-05 00:46:21 +0300537 /*
538 * Get the destination switch number from the second field of its 'reg'
539 * property, i.e. for "reg = <0x19 1>" sw_addr is '1'.
540 */
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000541 link_sw_addr = be32_to_cpup(reg + 1);
542
543 if (link_sw_addr >= pd->nr_chips)
544 return -EINVAL;
545
Pavel Nakonechny30303812015-04-05 00:46:21 +0300546 cd->rtable[link_sw_addr] = port_index;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000547
548 return 0;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000549}
550
Andrew Lunn1e72e6f2015-08-17 23:52:50 +0200551static int dsa_of_probe_links(struct dsa_platform_data *pd,
552 struct dsa_chip_data *cd,
553 int chip_index, int port_index,
554 struct device_node *port,
555 const char *port_name)
556{
557 struct device_node *link;
558 int link_index;
559 int ret;
560
561 for (link_index = 0;; link_index++) {
562 link = of_parse_phandle(port, "link", link_index);
563 if (!link)
564 break;
565
566 if (!strcmp(port_name, "dsa") && pd->nr_chips > 1) {
567 ret = dsa_of_setup_routing_table(pd, cd, chip_index,
568 port_index, link);
569 if (ret)
570 return ret;
571 }
572 }
573 return 0;
574}
575
Florian Fainelli21168242013-03-25 05:03:39 +0000576static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
577{
578 int i;
579 int port_index;
580
581 for (i = 0; i < pd->nr_chips; i++) {
582 port_index = 0;
Florian Fainelli5f64a7d2013-03-25 05:03:40 +0000583 while (port_index < DSA_MAX_PORTS) {
Fabian Frederick1f747142014-06-23 19:32:47 +0200584 kfree(pd->chip[i].port_names[port_index]);
Florian Fainelli5f64a7d2013-03-25 05:03:40 +0000585 port_index++;
586 }
Russell Kinge496ae62015-09-24 20:35:57 +0100587
588 /* Drop our reference to the MDIO bus device */
589 if (pd->chip[i].host_dev)
590 put_device(pd->chip[i].host_dev);
Florian Fainelli21168242013-03-25 05:03:39 +0000591 }
592 kfree(pd->chip);
593}
594
Florian Fainellif1a26a02015-03-05 12:35:04 -0800595static int dsa_of_probe(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000596{
Florian Fainellif1a26a02015-03-05 12:35:04 -0800597 struct device_node *np = dev->of_node;
Andrew Lunn1e72e6f2015-08-17 23:52:50 +0200598 struct device_node *child, *mdio, *ethernet, *port;
Andrew Lunn6bc6d0a2015-08-08 17:09:14 +0200599 struct mii_bus *mdio_bus, *mdio_bus_switch;
Florian Fainelli769a0202015-03-09 14:31:21 -0700600 struct net_device *ethernet_dev;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000601 struct dsa_platform_data *pd;
602 struct dsa_chip_data *cd;
603 const char *port_name;
604 int chip_index, port_index;
605 const unsigned int *sw_addr, *port_reg;
Guenter Roeck6793abb2014-10-29 10:45:01 -0700606 u32 eeprom_len;
Florian Fainelli21168242013-03-25 05:03:39 +0000607 int ret;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000608
609 mdio = of_parse_phandle(np, "dsa,mii-bus", 0);
610 if (!mdio)
611 return -EINVAL;
612
613 mdio_bus = of_mdio_find_bus(mdio);
614 if (!mdio_bus)
Florian Fainellib324c072015-03-05 12:35:05 -0800615 return -EPROBE_DEFER;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000616
617 ethernet = of_parse_phandle(np, "dsa,ethernet", 0);
Russell Kinge496ae62015-09-24 20:35:57 +0100618 if (!ethernet) {
619 ret = -EINVAL;
620 goto out_put_mdio;
621 }
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000622
Florian Fainelli769a0202015-03-09 14:31:21 -0700623 ethernet_dev = of_find_net_device_by_node(ethernet);
Russell Kinge496ae62015-09-24 20:35:57 +0100624 if (!ethernet_dev) {
625 ret = -EPROBE_DEFER;
626 goto out_put_mdio;
627 }
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000628
629 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
Russell Kinge496ae62015-09-24 20:35:57 +0100630 if (!pd) {
631 ret = -ENOMEM;
Russell King9861f722015-09-24 20:36:33 +0100632 goto out_put_ethernet;
Russell Kinge496ae62015-09-24 20:35:57 +0100633 }
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000634
Florian Fainellif1a26a02015-03-05 12:35:04 -0800635 dev->platform_data = pd;
Florian Fainelli769a0202015-03-09 14:31:21 -0700636 pd->of_netdev = ethernet_dev;
Tobias Waldekranze04449f2015-02-05 14:54:09 +0100637 pd->nr_chips = of_get_available_child_count(np);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000638 if (pd->nr_chips > DSA_MAX_SWITCHES)
639 pd->nr_chips = DSA_MAX_SWITCHES;
640
Fabian Frederick6f2aed62014-11-14 19:38:23 +0100641 pd->chip = kcalloc(pd->nr_chips, sizeof(struct dsa_chip_data),
642 GFP_KERNEL);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000643 if (!pd->chip) {
644 ret = -ENOMEM;
645 goto out_free;
646 }
647
Fabian Godehardtd1c0b472014-05-16 06:21:44 +0200648 chip_index = -1;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000649 for_each_available_child_of_node(np, child) {
Vivien Didelotd3902382016-07-06 20:03:54 -0400650 int i;
651
Fabian Godehardtd1c0b472014-05-16 06:21:44 +0200652 chip_index++;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000653 cd = &pd->chip[chip_index];
654
Florian Fainellifa981d92014-08-27 17:04:49 -0700655 cd->of_node = child;
Russell Kinge496ae62015-09-24 20:35:57 +0100656
Vivien Didelotd3902382016-07-06 20:03:54 -0400657 /* Initialize the routing table */
658 for (i = 0; i < DSA_MAX_SWITCHES; ++i)
659 cd->rtable[i] = DSA_RTABLE_NONE;
660
Russell Kinge496ae62015-09-24 20:35:57 +0100661 /* When assigning the host device, increment its refcount */
662 cd->host_dev = get_device(&mdio_bus->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000663
664 sw_addr = of_get_property(child, "reg", NULL);
665 if (!sw_addr)
666 continue;
667
668 cd->sw_addr = be32_to_cpup(sw_addr);
Florian Fainellic8cf89f2015-07-11 18:02:11 -0700669 if (cd->sw_addr >= PHY_MAX_ADDR)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000670 continue;
671
Guenter Roeck50d49642015-04-29 10:56:15 -0700672 if (!of_property_read_u32(child, "eeprom-length", &eeprom_len))
Guenter Roeck6793abb2014-10-29 10:45:01 -0700673 cd->eeprom_len = eeprom_len;
674
Andrew Lunn6bc6d0a2015-08-08 17:09:14 +0200675 mdio = of_parse_phandle(child, "mii-bus", 0);
676 if (mdio) {
677 mdio_bus_switch = of_mdio_find_bus(mdio);
678 if (!mdio_bus_switch) {
679 ret = -EPROBE_DEFER;
680 goto out_free_chip;
681 }
Russell Kinge496ae62015-09-24 20:35:57 +0100682
683 /* Drop the mdio_bus device ref, replacing the host
684 * device with the mdio_bus_switch device, keeping
685 * the refcount from of_mdio_find_bus() above.
686 */
687 put_device(cd->host_dev);
Andrew Lunn6bc6d0a2015-08-08 17:09:14 +0200688 cd->host_dev = &mdio_bus_switch->dev;
689 }
690
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000691 for_each_available_child_of_node(child, port) {
692 port_reg = of_get_property(port, "reg", NULL);
693 if (!port_reg)
694 continue;
695
696 port_index = be32_to_cpup(port_reg);
Florian Fainelli8f5063e2015-07-11 18:02:10 -0700697 if (port_index >= DSA_MAX_PORTS)
698 break;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000699
700 port_name = of_get_property(port, "label", NULL);
701 if (!port_name)
702 continue;
703
Florian Fainellibd474972014-08-27 17:04:50 -0700704 cd->port_dn[port_index] = port;
705
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000706 cd->port_names[port_index] = kstrdup(port_name,
707 GFP_KERNEL);
708 if (!cd->port_names[port_index]) {
709 ret = -ENOMEM;
710 goto out_free_chip;
711 }
712
Andrew Lunn1e72e6f2015-08-17 23:52:50 +0200713 ret = dsa_of_probe_links(pd, cd, chip_index,
714 port_index, port, port_name);
715 if (ret)
716 goto out_free_chip;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000717
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000718 }
719 }
720
Russell Kinge496ae62015-09-24 20:35:57 +0100721 /* The individual chips hold their own refcount on the mdio bus,
722 * so drop ours */
723 put_device(&mdio_bus->dev);
724
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000725 return 0;
726
727out_free_chip:
Florian Fainelli21168242013-03-25 05:03:39 +0000728 dsa_of_free_platform_data(pd);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000729out_free:
730 kfree(pd);
Florian Fainellif1a26a02015-03-05 12:35:04 -0800731 dev->platform_data = NULL;
Russell King9861f722015-09-24 20:36:33 +0100732out_put_ethernet:
733 put_device(&ethernet_dev->dev);
Russell Kinge496ae62015-09-24 20:35:57 +0100734out_put_mdio:
735 put_device(&mdio_bus->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000736 return ret;
737}
738
Florian Fainellif1a26a02015-03-05 12:35:04 -0800739static void dsa_of_remove(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000740{
Florian Fainellif1a26a02015-03-05 12:35:04 -0800741 struct dsa_platform_data *pd = dev->platform_data;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000742
Florian Fainellif1a26a02015-03-05 12:35:04 -0800743 if (!dev->of_node)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000744 return;
745
Florian Fainelli21168242013-03-25 05:03:39 +0000746 dsa_of_free_platform_data(pd);
Russell King9861f722015-09-24 20:36:33 +0100747 put_device(&pd->of_netdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000748 kfree(pd);
749}
750#else
Florian Fainellif1a26a02015-03-05 12:35:04 -0800751static inline int dsa_of_probe(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000752{
753 return 0;
754}
755
Florian Fainellif1a26a02015-03-05 12:35:04 -0800756static inline void dsa_of_remove(struct device *dev)
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000757{
758}
759#endif
760
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100761static int dsa_setup_dst(struct dsa_switch_tree *dst, struct net_device *dev,
762 struct device *parent, struct dsa_platform_data *pd)
Florian Fainellic86e59b2015-03-05 12:35:08 -0800763{
764 int i;
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100765 unsigned configured = 0;
Florian Fainellic86e59b2015-03-05 12:35:08 -0800766
767 dst->pd = pd;
768 dst->master_netdev = dev;
Florian Fainellic86e59b2015-03-05 12:35:08 -0800769 dst->cpu_port = -1;
770
771 for (i = 0; i < pd->nr_chips; i++) {
772 struct dsa_switch *ds;
773
774 ds = dsa_switch_setup(dst, i, parent, pd->chip[i].host_dev);
775 if (IS_ERR(ds)) {
776 netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n",
777 i, PTR_ERR(ds));
778 continue;
779 }
780
781 dst->ds[i] = ds;
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100782
783 ++configured;
Florian Fainellic86e59b2015-03-05 12:35:08 -0800784 }
785
786 /*
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100787 * If no switch was found, exit cleanly
788 */
789 if (!configured)
790 return -EPROBE_DEFER;
791
792 /*
Florian Fainellic86e59b2015-03-05 12:35:08 -0800793 * If we use a tagging format that doesn't have an ethertype
794 * field, make sure that all packets from this point on get
795 * sent to the tag format's receive function.
796 */
797 wmb();
798 dev->dsa_ptr = (void *)dst;
799
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100800 return 0;
Florian Fainellic86e59b2015-03-05 12:35:08 -0800801}
802
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000803static int dsa_probe(struct platform_device *pdev)
804{
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000805 struct dsa_platform_data *pd = pdev->dev.platform_data;
806 struct net_device *dev;
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000807 struct dsa_switch_tree *dst;
Florian Fainellic86e59b2015-03-05 12:35:08 -0800808 int ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000809
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000810 if (pdev->dev.of_node) {
Florian Fainellif1a26a02015-03-05 12:35:04 -0800811 ret = dsa_of_probe(&pdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000812 if (ret)
813 return ret;
814
815 pd = pdev->dev.platform_data;
816 }
817
Florian Fainelli769a0202015-03-09 14:31:21 -0700818 if (pd == NULL || (pd->netdev == NULL && pd->of_netdev == NULL))
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000819 return -EINVAL;
820
Florian Fainelli769a0202015-03-09 14:31:21 -0700821 if (pd->of_netdev) {
822 dev = pd->of_netdev;
823 dev_hold(dev);
824 } else {
Florian Fainelli14b89f32017-02-04 13:02:42 -0800825 dev = dsa_dev_to_net_device(pd->netdev);
Florian Fainelli769a0202015-03-09 14:31:21 -0700826 }
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000827 if (dev == NULL) {
Florian Fainellib324c072015-03-05 12:35:05 -0800828 ret = -EPROBE_DEFER;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000829 goto out;
830 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000831
832 if (dev->dsa_ptr != NULL) {
833 dev_put(dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000834 ret = -EEXIST;
835 goto out;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000836 }
837
Neil Armstrongd4ac35d2015-10-06 15:40:37 +0100838 dst = devm_kzalloc(&pdev->dev, sizeof(*dst), GFP_KERNEL);
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000839 if (dst == NULL) {
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000840 dev_put(dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000841 ret = -ENOMEM;
842 goto out;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000843 }
844
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000845 platform_set_drvdata(pdev, dst);
846
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100847 ret = dsa_setup_dst(dst, dev, &pdev->dev, pd);
Neil Armstrong679fb462015-12-07 13:57:34 +0100848 if (ret) {
849 dev_put(dev);
Neil Armstrong4d7f3e72015-10-06 15:40:43 +0100850 goto out;
Neil Armstrong679fb462015-12-07 13:57:34 +0100851 }
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000852
853 return 0;
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000854
855out:
Florian Fainellif1a26a02015-03-05 12:35:04 -0800856 dsa_of_remove(&pdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000857
858 return ret;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000859}
860
Florian Fainellic86e59b2015-03-05 12:35:08 -0800861static void dsa_remove_dst(struct dsa_switch_tree *dst)
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000862{
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000863 int i;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000864
Neil Armstrong04761892016-03-08 10:36:20 +0100865 dst->master_netdev->dsa_ptr = NULL;
866
867 /* If we used a tagging format that doesn't have an ethertype
868 * field, make sure that all packets from this point get sent
869 * without the tag and go through the regular receive path.
870 */
871 wmb();
872
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000873 for (i = 0; i < dst->pd->nr_chips; i++) {
874 struct dsa_switch *ds = dst->ds[i];
875
Neil Armstrongd4ac35d2015-10-06 15:40:37 +0100876 if (ds)
Lennert Buytenheke84665c2009-03-20 09:52:09 +0000877 dsa_switch_destroy(ds);
878 }
Neil Armstrong679fb462015-12-07 13:57:34 +0100879
Vivien Didelot9520ed82017-01-17 20:41:39 -0500880 dsa_cpu_port_ethtool_restore(dst->cpu_switch);
Florian Fainelli0c73c522016-06-07 16:32:42 -0700881
Neil Armstrong679fb462015-12-07 13:57:34 +0100882 dev_put(dst->master_netdev);
Florian Fainellic86e59b2015-03-05 12:35:08 -0800883}
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000884
Florian Fainellic86e59b2015-03-05 12:35:08 -0800885static int dsa_remove(struct platform_device *pdev)
886{
887 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
888
889 dsa_remove_dst(dst);
Florian Fainellif1a26a02015-03-05 12:35:04 -0800890 dsa_of_remove(&pdev->dev);
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000891
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000892 return 0;
893}
894
895static void dsa_shutdown(struct platform_device *pdev)
896{
897}
898
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700899static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
900 struct packet_type *pt, struct net_device *orig_dev)
901{
902 struct dsa_switch_tree *dst = dev->dsa_ptr;
903
904 if (unlikely(dst == NULL)) {
905 kfree_skb(skb);
906 return 0;
907 }
908
Alexander Duyck50753142014-09-15 13:00:19 -0400909 return dst->rcv(skb, dev, pt, orig_dev);
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700910}
911
Florian Fainelli61b73632014-08-29 12:42:07 -0700912static struct packet_type dsa_pack_type __read_mostly = {
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700913 .type = cpu_to_be16(ETH_P_XDSA),
914 .func = dsa_switch_rcv,
915};
916
Florian Fainelli24462542014-09-18 17:31:22 -0700917#ifdef CONFIG_PM_SLEEP
918static int dsa_suspend(struct device *d)
919{
920 struct platform_device *pdev = to_platform_device(d);
921 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
922 int i, ret = 0;
923
924 for (i = 0; i < dst->pd->nr_chips; i++) {
925 struct dsa_switch *ds = dst->ds[i];
926
927 if (ds != NULL)
928 ret = dsa_switch_suspend(ds);
929 }
930
931 return ret;
932}
933
934static int dsa_resume(struct device *d)
935{
936 struct platform_device *pdev = to_platform_device(d);
937 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
938 int i, ret = 0;
939
940 for (i = 0; i < dst->pd->nr_chips; i++) {
941 struct dsa_switch *ds = dst->ds[i];
942
943 if (ds != NULL)
944 ret = dsa_switch_resume(ds);
945 }
946
947 return ret;
948}
949#endif
950
951static SIMPLE_DEV_PM_OPS(dsa_pm_ops, dsa_suspend, dsa_resume);
952
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000953static const struct of_device_id dsa_of_match_table[] = {
954 { .compatible = "marvell,dsa", },
955 {}
956};
957MODULE_DEVICE_TABLE(of, dsa_of_match_table);
958
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000959static struct platform_driver dsa_driver = {
960 .probe = dsa_probe,
961 .remove = dsa_remove,
962 .shutdown = dsa_shutdown,
963 .driver = {
964 .name = "dsa",
Florian Fainelli5e95329b2013-03-22 10:50:50 +0000965 .of_match_table = dsa_of_match_table,
Florian Fainelli24462542014-09-18 17:31:22 -0700966 .pm = &dsa_pm_ops,
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000967 },
968};
969
970static int __init dsa_init_module(void)
971{
Ben Hutchings7df899c2011-11-25 14:35:02 +0000972 int rc;
973
Vivien Didelot88e4f0c2017-02-03 13:20:16 -0500974 rc = dsa_slave_register_notifier();
975 if (rc)
976 return rc;
Florian Fainellib73adef2015-02-24 13:15:33 -0800977
Ben Hutchings7df899c2011-11-25 14:35:02 +0000978 rc = platform_driver_register(&dsa_driver);
979 if (rc)
980 return rc;
981
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700982 dev_add_pack(&dsa_pack_type);
983
Ben Hutchings7df899c2011-11-25 14:35:02 +0000984 return 0;
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000985}
986module_init(dsa_init_module);
987
988static void __exit dsa_cleanup_module(void)
989{
Vivien Didelot88e4f0c2017-02-03 13:20:16 -0500990 dsa_slave_unregister_notifier();
Florian Fainelli3e8a72d2014-08-27 17:04:46 -0700991 dev_remove_pack(&dsa_pack_type);
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000992 platform_driver_unregister(&dsa_driver);
993}
994module_exit(dsa_cleanup_module);
995
Rusty Russell577d6a72011-01-24 14:32:52 -0600996MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
Lennert Buytenhek91da11f2008-10-07 13:44:02 +0000997MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
998MODULE_LICENSE("GPL");
999MODULE_ALIAS("platform:dsa");