1 // SPDX-License-Identifier: GPL-2.0
3 * Microchip switch driver main logic
5 * Copyright (C) 2017-2019 Microchip Technology Inc.
8 #include <linux/delay.h>
9 #include <linux/export.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/platform_data/microchip-ksz.h>
14 #include <linux/phy.h>
15 #include <linux/etherdevice.h>
16 #include <linux/if_bridge.h>
17 #include <linux/of_net.h>
19 #include <net/switchdev.h>
22 #include "ksz_common.h"
24 void ksz_update_port_member(struct ksz_device *dev, int port)
29 for (i = 0; i < dev->port_cnt; i++) {
30 if (i == port || i == dev->cpu_port)
33 if (!(dev->member & (1 << i)))
36 /* Port is a member of the bridge and is forwarding. */
37 if (p->stp_state == BR_STATE_FORWARDING &&
38 p->member != dev->member)
39 dev->dev_ops->cfg_port_member(dev, i, dev->member);
42 EXPORT_SYMBOL_GPL(ksz_update_port_member);
44 static void port_r_cnt(struct ksz_device *dev, int port)
46 struct ksz_port_mib *mib = &dev->ports[port].mib;
49 /* Some ports may not have MIB counters before SWITCH_COUNTER_NUM. */
50 while (mib->cnt_ptr < dev->reg_mib_cnt) {
51 dev->dev_ops->r_mib_cnt(dev, port, mib->cnt_ptr,
52 &mib->counters[mib->cnt_ptr]);
56 /* last one in storage */
57 dropped = &mib->counters[dev->mib_cnt];
59 /* Some ports may not have MIB counters after SWITCH_COUNTER_NUM. */
60 while (mib->cnt_ptr < dev->mib_cnt) {
61 dev->dev_ops->r_mib_pkt(dev, port, mib->cnt_ptr,
62 dropped, &mib->counters[mib->cnt_ptr]);
68 static void ksz_mib_read_work(struct work_struct *work)
70 struct ksz_device *dev = container_of(work, struct ksz_device,
72 struct ksz_port_mib *mib;
76 for (i = 0; i < dev->mib_port_cnt; i++) {
77 if (dsa_is_unused_port(dev->ds, i))
82 mutex_lock(&mib->cnt_mutex);
84 /* Only read MIB counters when the port is told to do.
85 * If not, read only dropped counters when link is not up.
88 const struct dsa_port *dp = dsa_to_port(dev->ds, i);
90 if (!netif_carrier_ok(dp->slave))
91 mib->cnt_ptr = dev->reg_mib_cnt;
95 mutex_unlock(&mib->cnt_mutex);
99 static void mib_monitor(struct timer_list *t)
101 struct ksz_device *dev = from_timer(dev, t, mib_read_timer);
103 mod_timer(&dev->mib_read_timer, jiffies + dev->mib_read_interval);
104 schedule_work(&dev->mib_read);
107 void ksz_init_mib_timer(struct ksz_device *dev)
111 /* Read MIB counters every 30 seconds to avoid overflow. */
112 dev->mib_read_interval = msecs_to_jiffies(30000);
114 INIT_WORK(&dev->mib_read, ksz_mib_read_work);
115 timer_setup(&dev->mib_read_timer, mib_monitor, 0);
117 for (i = 0; i < dev->mib_port_cnt; i++)
118 dev->dev_ops->port_init_cnt(dev, i);
120 /* Start the timer 2 seconds later. */
121 dev->mib_read_timer.expires = jiffies + msecs_to_jiffies(2000);
122 add_timer(&dev->mib_read_timer);
124 EXPORT_SYMBOL_GPL(ksz_init_mib_timer);
126 int ksz_phy_read16(struct dsa_switch *ds, int addr, int reg)
128 struct ksz_device *dev = ds->priv;
131 dev->dev_ops->r_phy(dev, addr, reg, &val);
135 EXPORT_SYMBOL_GPL(ksz_phy_read16);
137 int ksz_phy_write16(struct dsa_switch *ds, int addr, int reg, u16 val)
139 struct ksz_device *dev = ds->priv;
141 dev->dev_ops->w_phy(dev, addr, reg, val);
145 EXPORT_SYMBOL_GPL(ksz_phy_write16);
147 void ksz_adjust_link(struct dsa_switch *ds, int port,
148 struct phy_device *phydev)
150 struct ksz_device *dev = ds->priv;
151 struct ksz_port *p = &dev->ports[port];
153 /* Read all MIB counters when the link is going down. */
156 schedule_work(&dev->mib_read);
158 mutex_lock(&dev->dev_mutex);
160 dev->live_ports &= ~(1 << port);
162 /* Remember which port is connected and active. */
163 dev->live_ports |= (1 << port) & dev->on_ports;
164 mutex_unlock(&dev->dev_mutex);
166 EXPORT_SYMBOL_GPL(ksz_adjust_link);
168 int ksz_sset_count(struct dsa_switch *ds, int port, int sset)
170 struct ksz_device *dev = ds->priv;
172 if (sset != ETH_SS_STATS)
177 EXPORT_SYMBOL_GPL(ksz_sset_count);
179 void ksz_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *buf)
181 const struct dsa_port *dp = dsa_to_port(ds, port);
182 struct ksz_device *dev = ds->priv;
183 struct ksz_port_mib *mib;
185 mib = &dev->ports[port].mib;
186 mutex_lock(&mib->cnt_mutex);
188 /* Only read dropped counters if no link. */
189 if (!netif_carrier_ok(dp->slave))
190 mib->cnt_ptr = dev->reg_mib_cnt;
191 port_r_cnt(dev, port);
192 memcpy(buf, mib->counters, dev->mib_cnt * sizeof(u64));
193 mutex_unlock(&mib->cnt_mutex);
195 EXPORT_SYMBOL_GPL(ksz_get_ethtool_stats);
197 int ksz_port_bridge_join(struct dsa_switch *ds, int port,
198 struct net_device *br)
200 struct ksz_device *dev = ds->priv;
202 mutex_lock(&dev->dev_mutex);
203 dev->br_member |= (1 << port);
204 mutex_unlock(&dev->dev_mutex);
206 /* port_stp_state_set() will be called after to put the port in
207 * appropriate state so there is no need to do anything.
212 EXPORT_SYMBOL_GPL(ksz_port_bridge_join);
214 void ksz_port_bridge_leave(struct dsa_switch *ds, int port,
215 struct net_device *br)
217 struct ksz_device *dev = ds->priv;
219 mutex_lock(&dev->dev_mutex);
220 dev->br_member &= ~(1 << port);
221 dev->member &= ~(1 << port);
222 mutex_unlock(&dev->dev_mutex);
224 /* port_stp_state_set() will be called after to put the port in
225 * forwarding state so there is no need to do anything.
228 EXPORT_SYMBOL_GPL(ksz_port_bridge_leave);
230 void ksz_port_fast_age(struct dsa_switch *ds, int port)
232 struct ksz_device *dev = ds->priv;
234 dev->dev_ops->flush_dyn_mac_table(dev, port);
236 EXPORT_SYMBOL_GPL(ksz_port_fast_age);
238 int ksz_port_vlan_prepare(struct dsa_switch *ds, int port,
239 const struct switchdev_obj_port_vlan *vlan)
245 EXPORT_SYMBOL_GPL(ksz_port_vlan_prepare);
247 int ksz_port_fdb_dump(struct dsa_switch *ds, int port, dsa_fdb_dump_cb_t *cb,
250 struct ksz_device *dev = ds->priv;
257 struct alu_struct alu;
260 alu.is_static = false;
261 ret = dev->dev_ops->r_dyn_mac_table(dev, i, alu.mac, &fid,
264 if (!ret && (member & BIT(port))) {
265 ret = cb(alu.mac, alu.fid, alu.is_static, data);
270 } while (i < entries);
276 EXPORT_SYMBOL_GPL(ksz_port_fdb_dump);
278 int ksz_port_mdb_prepare(struct dsa_switch *ds, int port,
279 const struct switchdev_obj_port_mdb *mdb)
284 EXPORT_SYMBOL_GPL(ksz_port_mdb_prepare);
286 void ksz_port_mdb_add(struct dsa_switch *ds, int port,
287 const struct switchdev_obj_port_mdb *mdb)
289 struct ksz_device *dev = ds->priv;
290 struct alu_struct alu;
294 alu.port_forward = 0;
295 for (index = 0; index < dev->num_statics; index++) {
296 if (!dev->dev_ops->r_sta_mac_table(dev, index, &alu)) {
297 /* Found one already in static MAC table. */
298 if (!memcmp(alu.mac, mdb->addr, ETH_ALEN) &&
301 /* Remember the first empty entry. */
307 /* no available entry */
308 if (index == dev->num_statics && !empty)
312 if (index == dev->num_statics) {
314 memset(&alu, 0, sizeof(alu));
315 memcpy(alu.mac, mdb->addr, ETH_ALEN);
316 alu.is_static = true;
318 alu.port_forward |= BIT(port);
320 alu.is_use_fid = true;
322 /* Need a way to map VID to FID. */
325 dev->dev_ops->w_sta_mac_table(dev, index, &alu);
327 EXPORT_SYMBOL_GPL(ksz_port_mdb_add);
329 int ksz_port_mdb_del(struct dsa_switch *ds, int port,
330 const struct switchdev_obj_port_mdb *mdb)
332 struct ksz_device *dev = ds->priv;
333 struct alu_struct alu;
337 for (index = 0; index < dev->num_statics; index++) {
338 if (!dev->dev_ops->r_sta_mac_table(dev, index, &alu)) {
339 /* Found one already in static MAC table. */
340 if (!memcmp(alu.mac, mdb->addr, ETH_ALEN) &&
346 /* no available entry */
347 if (index == dev->num_statics)
351 alu.port_forward &= ~BIT(port);
352 if (!alu.port_forward)
353 alu.is_static = false;
354 dev->dev_ops->w_sta_mac_table(dev, index, &alu);
359 EXPORT_SYMBOL_GPL(ksz_port_mdb_del);
361 int ksz_enable_port(struct dsa_switch *ds, int port, struct phy_device *phy)
363 struct ksz_device *dev = ds->priv;
365 /* setup slave port */
366 dev->dev_ops->port_setup(dev, port, false);
367 if (dev->dev_ops->phy_setup)
368 dev->dev_ops->phy_setup(dev, port, phy);
370 /* port_stp_state_set() will be called after to enable the port so
371 * there is no need to do anything.
376 EXPORT_SYMBOL_GPL(ksz_enable_port);
378 void ksz_disable_port(struct dsa_switch *ds, int port)
380 struct ksz_device *dev = ds->priv;
382 dev->on_ports &= ~(1 << port);
383 dev->live_ports &= ~(1 << port);
385 /* port_stp_state_set() will be called after to disable the port so
386 * there is no need to do anything.
389 EXPORT_SYMBOL_GPL(ksz_disable_port);
391 struct ksz_device *ksz_switch_alloc(struct device *base, void *priv)
393 struct dsa_switch *ds;
394 struct ksz_device *swdev;
396 ds = dsa_switch_alloc(base, DSA_MAX_PORTS);
400 swdev = devm_kzalloc(base, sizeof(*swdev), GFP_KERNEL);
412 EXPORT_SYMBOL(ksz_switch_alloc);
414 int ksz_switch_register(struct ksz_device *dev,
415 const struct ksz_dev_ops *ops)
420 dev->chip_id = dev->pdata->chip_id;
422 dev->reset_gpio = devm_gpiod_get_optional(dev->dev, "reset",
424 if (IS_ERR(dev->reset_gpio))
425 return PTR_ERR(dev->reset_gpio);
427 if (dev->reset_gpio) {
428 gpiod_set_value_cansleep(dev->reset_gpio, 1);
430 gpiod_set_value_cansleep(dev->reset_gpio, 0);
433 mutex_init(&dev->dev_mutex);
434 mutex_init(&dev->stats_mutex);
435 mutex_init(&dev->alu_mutex);
436 mutex_init(&dev->vlan_mutex);
440 if (dev->dev_ops->detect(dev))
443 ret = dev->dev_ops->init(dev);
447 /* Host port interface will be self detected, or specifically set in
450 if (dev->dev->of_node) {
451 ret = of_get_phy_mode(dev->dev->of_node);
453 dev->interface = ret;
454 dev->synclko_125 = of_property_read_bool(dev->dev->of_node,
455 "microchip,synclko-125");
458 ret = dsa_register_switch(dev->ds);
460 dev->dev_ops->exit(dev);
466 EXPORT_SYMBOL(ksz_switch_register);
468 void ksz_switch_remove(struct ksz_device *dev)
471 if (dev->mib_read_timer.expires) {
472 del_timer_sync(&dev->mib_read_timer);
473 flush_work(&dev->mib_read);
476 dev->dev_ops->exit(dev);
477 dsa_unregister_switch(dev->ds);
480 gpiod_set_value_cansleep(dev->reset_gpio, 1);
483 EXPORT_SYMBOL(ksz_switch_remove);
485 MODULE_AUTHOR("Woojung Huh <Woojung.Huh@microchip.com>");
486 MODULE_DESCRIPTION("Microchip KSZ Series Switch DSA Driver");
487 MODULE_LICENSE("GPL");