ksz_common.c 7.6 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4
/*
 * Microchip switch driver main logic
 *
5
 * Copyright (C) 2017-2018 Microchip Technology Inc.
6 7 8 9
 */

#include <linux/delay.h>
#include <linux/export.h>
10
#include <linux/gpio/consumer.h>
11 12 13 14 15 16
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_data/microchip-ksz.h>
#include <linux/phy.h>
#include <linux/etherdevice.h>
#include <linux/if_bridge.h>
17
#include <linux/of_net.h>
18 19 20 21 22
#include <net/dsa.h>
#include <net/switchdev.h>

#include "ksz_priv.h"

23
void ksz_update_port_member(struct ksz_device *dev, int port)
24
{
25
	struct ksz_port *p;
26 27
	int i;

28 29 30 31 32 33 34 35 36 37 38
	for (i = 0; i < dev->port_cnt; i++) {
		if (i == port || i == dev->cpu_port)
			continue;
		p = &dev->ports[i];
		if (!(dev->member & (1 << i)))
			continue;

		/* Port is a member of the bridge and is forwarding. */
		if (p->stp_state == BR_STATE_FORWARDING &&
		    p->member != dev->member)
			dev->dev_ops->cfg_port_member(dev, i, dev->member);
39 40
	}
}
41
EXPORT_SYMBOL_GPL(ksz_update_port_member);
42

43
int ksz_phy_read16(struct dsa_switch *ds, int addr, int reg)
44 45
{
	struct ksz_device *dev = ds->priv;
46
	u16 val = 0xffff;
47

48
	dev->dev_ops->r_phy(dev, addr, reg, &val);
49 50 51

	return val;
}
52
EXPORT_SYMBOL_GPL(ksz_phy_read16);
53

54
int ksz_phy_write16(struct dsa_switch *ds, int addr, int reg, u16 val)
55 56 57
{
	struct ksz_device *dev = ds->priv;

58
	dev->dev_ops->w_phy(dev, addr, reg, val);
59 60 61

	return 0;
}
62
EXPORT_SYMBOL_GPL(ksz_phy_write16);
63

64
int ksz_sset_count(struct dsa_switch *ds, int port, int sset)
65 66 67
{
	struct ksz_device *dev = ds->priv;

68 69 70
	if (sset != ETH_SS_STATS)
		return 0;

71
	return dev->mib_cnt;
72
}
73
EXPORT_SYMBOL_GPL(ksz_sset_count);
74

75 76
int ksz_port_bridge_join(struct dsa_switch *ds, int port,
			 struct net_device *br)
77 78 79
{
	struct ksz_device *dev = ds->priv;

80
	dev->br_member |= (1 << port);
81

82 83 84
	/* port_stp_state_set() will be called after to put the port in
	 * appropriate state so there is no need to do anything.
	 */
85

86
	return 0;
87
}
88
EXPORT_SYMBOL_GPL(ksz_port_bridge_join);
89

90 91
void ksz_port_bridge_leave(struct dsa_switch *ds, int port,
			   struct net_device *br)
92 93 94
{
	struct ksz_device *dev = ds->priv;

95 96
	dev->br_member &= ~(1 << port);
	dev->member &= ~(1 << port);
97

98 99 100
	/* port_stp_state_set() will be called after to put the port in
	 * forwarding state so there is no need to do anything.
	 */
101
}
102
EXPORT_SYMBOL_GPL(ksz_port_bridge_leave);
103

104
void ksz_port_fast_age(struct dsa_switch *ds, int port)
105 106 107
{
	struct ksz_device *dev = ds->priv;

108
	dev->dev_ops->flush_dyn_mac_table(dev, port);
109
}
110
EXPORT_SYMBOL_GPL(ksz_port_fast_age);
111

112 113
int ksz_port_vlan_prepare(struct dsa_switch *ds, int port,
			  const struct switchdev_obj_port_vlan *vlan)
114 115 116 117 118
{
	/* nothing needed */

	return 0;
}
119
EXPORT_SYMBOL_GPL(ksz_port_vlan_prepare);
120

121 122
int ksz_port_fdb_dump(struct dsa_switch *ds, int port, dsa_fdb_dump_cb_t *cb,
		      void *data)
123 124
{
	struct ksz_device *dev = ds->priv;
125
	int ret = 0;
126 127 128 129 130
	u16 i = 0;
	u16 entries = 0;
	u8 timestamp = 0;
	u8 fid;
	u8 member;
131 132 133
	struct alu_struct alu;

	do {
134 135 136 137 138
		alu.is_static = false;
		ret = dev->dev_ops->r_dyn_mac_table(dev, i, alu.mac, &fid,
						    &member, &timestamp,
						    &entries);
		if (!ret && (member & BIT(port))) {
139
			ret = cb(alu.mac, alu.fid, alu.is_static, data);
140
			if (ret)
141
				break;
142
		}
143 144 145 146
		i++;
	} while (i < entries);
	if (i >= entries)
		ret = 0;
147 148 149

	return ret;
}
150
EXPORT_SYMBOL_GPL(ksz_port_fdb_dump);
151

152 153
int ksz_port_mdb_prepare(struct dsa_switch *ds, int port,
			 const struct switchdev_obj_port_mdb *mdb)
154 155 156 157
{
	/* nothing to do */
	return 0;
}
158
EXPORT_SYMBOL_GPL(ksz_port_mdb_prepare);
159

160 161
void ksz_port_mdb_add(struct dsa_switch *ds, int port,
		      const struct switchdev_obj_port_mdb *mdb)
162 163
{
	struct ksz_device *dev = ds->priv;
164
	struct alu_struct alu;
165
	int index;
166
	int empty = 0;
167

168
	alu.port_forward = 0;
169
	for (index = 0; index < dev->num_statics; index++) {
170 171 172 173
		if (!dev->dev_ops->r_sta_mac_table(dev, index, &alu)) {
			/* Found one already in static MAC table. */
			if (!memcmp(alu.mac, mdb->addr, ETH_ALEN) &&
			    alu.fid == mdb->vid)
174
				break;
175 176 177
		/* Remember the first empty entry. */
		} else if (!empty) {
			empty = index + 1;
178 179 180 181
		}
	}

	/* no available entry */
182 183
	if (index == dev->num_statics && !empty)
		return;
184 185

	/* add entry */
186 187 188 189 190 191 192 193 194
	if (index == dev->num_statics) {
		index = empty - 1;
		memset(&alu, 0, sizeof(alu));
		memcpy(alu.mac, mdb->addr, ETH_ALEN);
		alu.is_static = true;
	}
	alu.port_forward |= BIT(port);
	if (mdb->vid) {
		alu.is_use_fid = true;
195

196 197 198 199
		/* Need a way to map VID to FID. */
		alu.fid = mdb->vid;
	}
	dev->dev_ops->w_sta_mac_table(dev, index, &alu);
200
}
201
EXPORT_SYMBOL_GPL(ksz_port_mdb_add);
202

203 204
int ksz_port_mdb_del(struct dsa_switch *ds, int port,
		     const struct switchdev_obj_port_mdb *mdb)
205 206
{
	struct ksz_device *dev = ds->priv;
207
	struct alu_struct alu;
208 209 210 211
	int index;
	int ret = 0;

	for (index = 0; index < dev->num_statics; index++) {
212 213 214 215
		if (!dev->dev_ops->r_sta_mac_table(dev, index, &alu)) {
			/* Found one already in static MAC table. */
			if (!memcmp(alu.mac, mdb->addr, ETH_ALEN) &&
			    alu.fid == mdb->vid)
216 217 218 219 220
				break;
		}
	}

	/* no available entry */
221
	if (index == dev->num_statics)
222 223 224
		goto exit;

	/* clear port */
225 226 227 228
	alu.port_forward &= ~BIT(port);
	if (!alu.port_forward)
		alu.is_static = false;
	dev->dev_ops->w_sta_mac_table(dev, index, &alu);
229 230 231 232

exit:
	return ret;
}
233
EXPORT_SYMBOL_GPL(ksz_port_mdb_del);
234

235
int ksz_enable_port(struct dsa_switch *ds, int port, struct phy_device *phy)
236 237 238
{
	struct ksz_device *dev = ds->priv;

239 240
	/* setup slave port */
	dev->dev_ops->port_setup(dev, port, false);
241

242 243 244
	/* port_stp_state_set() will be called after to enable the port so
	 * there is no need to do anything.
	 */
245 246 247

	return 0;
}
248
EXPORT_SYMBOL_GPL(ksz_enable_port);
249

250
void ksz_disable_port(struct dsa_switch *ds, int port, struct phy_device *phy)
251 252 253
{
	struct ksz_device *dev = ds->priv;

254 255
	dev->on_ports &= ~(1 << port);
	dev->live_ports &= ~(1 << port);
256

257 258 259
	/* port_stp_state_set() will be called after to disable the port so
	 * there is no need to do anything.
	 */
260
}
261
EXPORT_SYMBOL_GPL(ksz_disable_port);
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288

struct ksz_device *ksz_switch_alloc(struct device *base,
				    const struct ksz_io_ops *ops,
				    void *priv)
{
	struct dsa_switch *ds;
	struct ksz_device *swdev;

	ds = dsa_switch_alloc(base, DSA_MAX_PORTS);
	if (!ds)
		return NULL;

	swdev = devm_kzalloc(base, sizeof(*swdev), GFP_KERNEL);
	if (!swdev)
		return NULL;

	ds->priv = swdev;
	swdev->dev = base;

	swdev->ds = ds;
	swdev->priv = priv;
	swdev->ops = ops;

	return swdev;
}
EXPORT_SYMBOL(ksz_switch_alloc);

289 290
int ksz_switch_register(struct ksz_device *dev,
			const struct ksz_dev_ops *ops)
291 292 293 294 295 296
{
	int ret;

	if (dev->pdata)
		dev->chip_id = dev->pdata->chip_id;

297 298 299 300 301 302 303 304 305 306 307
	dev->reset_gpio = devm_gpiod_get_optional(dev->dev, "reset",
						  GPIOD_OUT_LOW);
	if (IS_ERR(dev->reset_gpio))
		return PTR_ERR(dev->reset_gpio);

	if (dev->reset_gpio) {
		gpiod_set_value(dev->reset_gpio, 1);
		mdelay(10);
		gpiod_set_value(dev->reset_gpio, 0);
	}

308 309 310 311 312
	mutex_init(&dev->reg_mutex);
	mutex_init(&dev->stats_mutex);
	mutex_init(&dev->alu_mutex);
	mutex_init(&dev->vlan_mutex);

313 314 315
	dev->dev_ops = ops;

	if (dev->dev_ops->detect(dev))
316 317
		return -EINVAL;

318
	ret = dev->dev_ops->init(dev);
319 320 321
	if (ret)
		return ret;

322 323 324 325 326 327 328 329 330 331 332 333 334 335
	dev->interface = PHY_INTERFACE_MODE_MII;
	if (dev->dev->of_node) {
		ret = of_get_phy_mode(dev->dev->of_node);
		if (ret >= 0)
			dev->interface = ret;
	}

	ret = dsa_register_switch(dev->ds);
	if (ret) {
		dev->dev_ops->exit(dev);
		return ret;
	}

	return 0;
336 337 338 339 340
}
EXPORT_SYMBOL(ksz_switch_register);

void ksz_switch_remove(struct ksz_device *dev)
{
341
	dev->dev_ops->exit(dev);
342
	dsa_unregister_switch(dev->ds);
343 344 345 346

	if (dev->reset_gpio)
		gpiod_set_value(dev->reset_gpio, 1);

347 348 349 350 351 352
}
EXPORT_SYMBOL(ksz_switch_remove);

MODULE_AUTHOR("Woojung Huh <Woojung.Huh@microchip.com>");
MODULE_DESCRIPTION("Microchip KSZ Series Switch DSA Driver");
MODULE_LICENSE("GPL");