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>
23 void ksz_port_cleanup(struct ksz_device *dev, int port)
25 /* Common code for port cleanup. */
26 mutex_lock(&dev->dev_mutex);
27 dev->on_ports &= ~(1 << port);
28 dev->live_ports &= ~(1 << port);
29 mutex_unlock(&dev->dev_mutex);
31 EXPORT_SYMBOL_GPL(ksz_port_cleanup);
33 void ksz_update_port_member(struct ksz_device *dev, int port)
38 for (i = 0; i < dev->port_cnt; i++) {
39 if (i == port || i == dev->cpu_port)
42 if (!(dev->member & (1 << i)))
45 /* Port is a member of the bridge and is forwarding. */
46 if (p->stp_state == BR_STATE_FORWARDING &&
47 p->member != dev->member)
48 dev->dev_ops->cfg_port_member(dev, i, dev->member);
51 EXPORT_SYMBOL_GPL(ksz_update_port_member);
53 static void port_r_cnt(struct ksz_device *dev, int port)
55 struct ksz_port_mib *mib = &dev->ports[port].mib;
58 /* Some ports may not have MIB counters before SWITCH_COUNTER_NUM. */
59 while (mib->cnt_ptr < dev->reg_mib_cnt) {
60 dev->dev_ops->r_mib_cnt(dev, port, mib->cnt_ptr,
61 &mib->counters[mib->cnt_ptr]);
65 /* last one in storage */
66 dropped = &mib->counters[dev->mib_cnt];
68 /* Some ports may not have MIB counters after SWITCH_COUNTER_NUM. */
69 while (mib->cnt_ptr < dev->mib_cnt) {
70 dev->dev_ops->r_mib_pkt(dev, port, mib->cnt_ptr,
71 dropped, &mib->counters[mib->cnt_ptr]);
77 static void ksz_mib_read_work(struct work_struct *work)
79 struct ksz_device *dev = container_of(work, struct ksz_device,
81 struct ksz_port_mib *mib;
85 for (i = 0; i < dev->mib_port_cnt; i++) {
86 if (dsa_is_unused_port(dev->ds, i))
91 mutex_lock(&mib->cnt_mutex);
93 /* Only read MIB counters when the port is told to do.
94 * If not, read only dropped counters when link is not up.
97 const struct dsa_port *dp = dsa_to_port(dev->ds, i);
99 if (!netif_carrier_ok(dp->slave))
100 mib->cnt_ptr = dev->reg_mib_cnt;
104 mutex_unlock(&mib->cnt_mutex);
108 static void mib_monitor(struct timer_list *t)
110 struct ksz_device *dev = from_timer(dev, t, mib_read_timer);
112 mod_timer(&dev->mib_read_timer, jiffies + dev->mib_read_interval);
113 schedule_work(&dev->mib_read);
116 void ksz_init_mib_timer(struct ksz_device *dev)
120 /* Read MIB counters every 30 seconds to avoid overflow. */
121 dev->mib_read_interval = msecs_to_jiffies(30000);
123 INIT_WORK(&dev->mib_read, ksz_mib_read_work);
124 timer_setup(&dev->mib_read_timer, mib_monitor, 0);
126 for (i = 0; i < dev->mib_port_cnt; i++)
127 dev->dev_ops->port_init_cnt(dev, i);
129 /* Start the timer 2 seconds later. */
130 dev->mib_read_timer.expires = jiffies + msecs_to_jiffies(2000);
131 add_timer(&dev->mib_read_timer);
133 EXPORT_SYMBOL_GPL(ksz_init_mib_timer);
135 int ksz_phy_read16(struct dsa_switch *ds, int addr, int reg)
137 struct ksz_device *dev = ds->priv;
140 dev->dev_ops->r_phy(dev, addr, reg, &val);
144 EXPORT_SYMBOL_GPL(ksz_phy_read16);
146 int ksz_phy_write16(struct dsa_switch *ds, int addr, int reg, u16 val)
148 struct ksz_device *dev = ds->priv;
150 dev->dev_ops->w_phy(dev, addr, reg, val);
154 EXPORT_SYMBOL_GPL(ksz_phy_write16);
156 void ksz_adjust_link(struct dsa_switch *ds, int port,
157 struct phy_device *phydev)
159 struct ksz_device *dev = ds->priv;
160 struct ksz_port *p = &dev->ports[port];
162 /* Read all MIB counters when the link is going down. */
165 schedule_work(&dev->mib_read);
167 mutex_lock(&dev->dev_mutex);
169 dev->live_ports &= ~(1 << port);
171 /* Remember which port is connected and active. */
172 dev->live_ports |= (1 << port) & dev->on_ports;
173 mutex_unlock(&dev->dev_mutex);
175 EXPORT_SYMBOL_GPL(ksz_adjust_link);
177 int ksz_sset_count(struct dsa_switch *ds, int port, int sset)
179 struct ksz_device *dev = ds->priv;
181 if (sset != ETH_SS_STATS)
186 EXPORT_SYMBOL_GPL(ksz_sset_count);
188 void ksz_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *buf)
190 const struct dsa_port *dp = dsa_to_port(ds, port);
191 struct ksz_device *dev = ds->priv;
192 struct ksz_port_mib *mib;
194 mib = &dev->ports[port].mib;
195 mutex_lock(&mib->cnt_mutex);
197 /* Only read dropped counters if no link. */
198 if (!netif_carrier_ok(dp->slave))
199 mib->cnt_ptr = dev->reg_mib_cnt;
200 port_r_cnt(dev, port);
201 memcpy(buf, mib->counters, dev->mib_cnt * sizeof(u64));
202 mutex_unlock(&mib->cnt_mutex);
204 EXPORT_SYMBOL_GPL(ksz_get_ethtool_stats);
206 int ksz_port_bridge_join(struct dsa_switch *ds, int port,
207 struct net_device *br)
209 struct ksz_device *dev = ds->priv;
211 mutex_lock(&dev->dev_mutex);
212 dev->br_member |= (1 << port);
213 mutex_unlock(&dev->dev_mutex);
215 /* port_stp_state_set() will be called after to put the port in
216 * appropriate state so there is no need to do anything.
221 EXPORT_SYMBOL_GPL(ksz_port_bridge_join);
223 void ksz_port_bridge_leave(struct dsa_switch *ds, int port,
224 struct net_device *br)
226 struct ksz_device *dev = ds->priv;
228 mutex_lock(&dev->dev_mutex);
229 dev->br_member &= ~(1 << port);
230 dev->member &= ~(1 << port);
231 mutex_unlock(&dev->dev_mutex);
233 /* port_stp_state_set() will be called after to put the port in
234 * forwarding state so there is no need to do anything.
237 EXPORT_SYMBOL_GPL(ksz_port_bridge_leave);
239 void ksz_port_fast_age(struct dsa_switch *ds, int port)
241 struct ksz_device *dev = ds->priv;
243 dev->dev_ops->flush_dyn_mac_table(dev, port);
245 EXPORT_SYMBOL_GPL(ksz_port_fast_age);
247 int ksz_port_vlan_prepare(struct dsa_switch *ds, int port,
248 const struct switchdev_obj_port_vlan *vlan)
254 EXPORT_SYMBOL_GPL(ksz_port_vlan_prepare);
256 int ksz_port_fdb_dump(struct dsa_switch *ds, int port, dsa_fdb_dump_cb_t *cb,
259 struct ksz_device *dev = ds->priv;
266 struct alu_struct alu;
269 alu.is_static = false;
270 ret = dev->dev_ops->r_dyn_mac_table(dev, i, alu.mac, &fid,
273 if (!ret && (member & BIT(port))) {
274 ret = cb(alu.mac, alu.fid, alu.is_static, data);
279 } while (i < entries);
285 EXPORT_SYMBOL_GPL(ksz_port_fdb_dump);
287 int ksz_port_mdb_prepare(struct dsa_switch *ds, int port,
288 const struct switchdev_obj_port_mdb *mdb)
293 EXPORT_SYMBOL_GPL(ksz_port_mdb_prepare);
295 void ksz_port_mdb_add(struct dsa_switch *ds, int port,
296 const struct switchdev_obj_port_mdb *mdb)
298 struct ksz_device *dev = ds->priv;
299 struct alu_struct alu;
303 alu.port_forward = 0;
304 for (index = 0; index < dev->num_statics; index++) {
305 if (!dev->dev_ops->r_sta_mac_table(dev, index, &alu)) {
306 /* Found one already in static MAC table. */
307 if (!memcmp(alu.mac, mdb->addr, ETH_ALEN) &&
310 /* Remember the first empty entry. */
316 /* no available entry */
317 if (index == dev->num_statics && !empty)
321 if (index == dev->num_statics) {
323 memset(&alu, 0, sizeof(alu));
324 memcpy(alu.mac, mdb->addr, ETH_ALEN);
325 alu.is_static = true;
327 alu.port_forward |= BIT(port);
329 alu.is_use_fid = true;
331 /* Need a way to map VID to FID. */
334 dev->dev_ops->w_sta_mac_table(dev, index, &alu);
336 EXPORT_SYMBOL_GPL(ksz_port_mdb_add);
338 int ksz_port_mdb_del(struct dsa_switch *ds, int port,
339 const struct switchdev_obj_port_mdb *mdb)
341 struct ksz_device *dev = ds->priv;
342 struct alu_struct alu;
346 for (index = 0; index < dev->num_statics; index++) {
347 if (!dev->dev_ops->r_sta_mac_table(dev, index, &alu)) {
348 /* Found one already in static MAC table. */
349 if (!memcmp(alu.mac, mdb->addr, ETH_ALEN) &&
355 /* no available entry */
356 if (index == dev->num_statics)
360 alu.port_forward &= ~BIT(port);
361 if (!alu.port_forward)
362 alu.is_static = false;
363 dev->dev_ops->w_sta_mac_table(dev, index, &alu);
368 EXPORT_SYMBOL_GPL(ksz_port_mdb_del);
370 int ksz_enable_port(struct dsa_switch *ds, int port, struct phy_device *phy)
372 struct ksz_device *dev = ds->priv;
374 /* setup slave port */
375 dev->dev_ops->port_setup(dev, port, false);
376 dev->dev_ops->phy_setup(dev, port, phy);
378 /* port_stp_state_set() will be called after to enable the port so
379 * there is no need to do anything.
384 EXPORT_SYMBOL_GPL(ksz_enable_port);
386 void ksz_disable_port(struct dsa_switch *ds, int port)
388 struct ksz_device *dev = ds->priv;
390 dev->on_ports &= ~(1 << port);
391 dev->live_ports &= ~(1 << port);
393 /* port_stp_state_set() will be called after to disable the port so
394 * there is no need to do anything.
397 EXPORT_SYMBOL_GPL(ksz_disable_port);
399 struct ksz_device *ksz_switch_alloc(struct device *base, void *priv)
401 struct dsa_switch *ds;
402 struct ksz_device *swdev;
404 ds = dsa_switch_alloc(base, DSA_MAX_PORTS);
408 swdev = devm_kzalloc(base, sizeof(*swdev), GFP_KERNEL);
420 EXPORT_SYMBOL(ksz_switch_alloc);
422 int ksz_switch_register(struct ksz_device *dev,
423 const struct ksz_dev_ops *ops)
428 dev->chip_id = dev->pdata->chip_id;
430 dev->reset_gpio = devm_gpiod_get_optional(dev->dev, "reset",
432 if (IS_ERR(dev->reset_gpio))
433 return PTR_ERR(dev->reset_gpio);
435 if (dev->reset_gpio) {
436 gpiod_set_value_cansleep(dev->reset_gpio, 1);
438 gpiod_set_value_cansleep(dev->reset_gpio, 0);
441 mutex_init(&dev->dev_mutex);
442 mutex_init(&dev->stats_mutex);
443 mutex_init(&dev->alu_mutex);
444 mutex_init(&dev->vlan_mutex);
448 if (dev->dev_ops->detect(dev))
451 ret = dev->dev_ops->init(dev);
455 /* Host port interface will be self detected, or specifically set in
458 if (dev->dev->of_node) {
459 ret = of_get_phy_mode(dev->dev->of_node);
461 dev->interface = ret;
462 dev->synclko_125 = of_property_read_bool(dev->dev->of_node,
463 "microchip,synclko-125");
466 ret = dsa_register_switch(dev->ds);
468 dev->dev_ops->exit(dev);
474 EXPORT_SYMBOL(ksz_switch_register);
476 void ksz_switch_remove(struct ksz_device *dev)
479 if (dev->mib_read_timer.expires) {
480 del_timer_sync(&dev->mib_read_timer);
481 flush_work(&dev->mib_read);
484 dev->dev_ops->exit(dev);
485 dsa_unregister_switch(dev->ds);
488 gpiod_set_value_cansleep(dev->reset_gpio, 1);
491 EXPORT_SYMBOL(ksz_switch_remove);
493 MODULE_AUTHOR("Woojung Huh <Woojung.Huh@microchip.com>");
494 MODULE_DESCRIPTION("Microchip KSZ Series Switch DSA Driver");
495 MODULE_LICENSE("GPL");