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++) {
88 mutex_lock(&mib->cnt_mutex);
90 /* Only read MIB counters when the port is told to do.
91 * If not, read only dropped counters when link is not up.
94 const struct dsa_port *dp = dsa_to_port(dev->ds, i);
96 if (!netif_carrier_ok(dp->slave))
97 mib->cnt_ptr = dev->reg_mib_cnt;
101 mutex_unlock(&mib->cnt_mutex);
105 static void mib_monitor(struct timer_list *t)
107 struct ksz_device *dev = from_timer(dev, t, mib_read_timer);
109 mod_timer(&dev->mib_read_timer, jiffies + dev->mib_read_interval);
110 schedule_work(&dev->mib_read);
113 void ksz_init_mib_timer(struct ksz_device *dev)
117 /* Read MIB counters every 30 seconds to avoid overflow. */
118 dev->mib_read_interval = msecs_to_jiffies(30000);
120 INIT_WORK(&dev->mib_read, ksz_mib_read_work);
121 timer_setup(&dev->mib_read_timer, mib_monitor, 0);
123 for (i = 0; i < dev->mib_port_cnt; i++)
124 dev->dev_ops->port_init_cnt(dev, i);
126 /* Start the timer 2 seconds later. */
127 dev->mib_read_timer.expires = jiffies + msecs_to_jiffies(2000);
128 add_timer(&dev->mib_read_timer);
130 EXPORT_SYMBOL_GPL(ksz_init_mib_timer);
132 int ksz_phy_read16(struct dsa_switch *ds, int addr, int reg)
134 struct ksz_device *dev = ds->priv;
137 dev->dev_ops->r_phy(dev, addr, reg, &val);
141 EXPORT_SYMBOL_GPL(ksz_phy_read16);
143 int ksz_phy_write16(struct dsa_switch *ds, int addr, int reg, u16 val)
145 struct ksz_device *dev = ds->priv;
147 dev->dev_ops->w_phy(dev, addr, reg, val);
151 EXPORT_SYMBOL_GPL(ksz_phy_write16);
153 void ksz_adjust_link(struct dsa_switch *ds, int port,
154 struct phy_device *phydev)
156 struct ksz_device *dev = ds->priv;
157 struct ksz_port *p = &dev->ports[port];
159 /* Read all MIB counters when the link is going down. */
162 schedule_work(&dev->mib_read);
164 mutex_lock(&dev->dev_mutex);
166 dev->live_ports &= ~(1 << port);
168 /* Remember which port is connected and active. */
169 dev->live_ports |= (1 << port) & dev->on_ports;
170 mutex_unlock(&dev->dev_mutex);
172 EXPORT_SYMBOL_GPL(ksz_adjust_link);
174 int ksz_sset_count(struct dsa_switch *ds, int port, int sset)
176 struct ksz_device *dev = ds->priv;
178 if (sset != ETH_SS_STATS)
183 EXPORT_SYMBOL_GPL(ksz_sset_count);
185 void ksz_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *buf)
187 const struct dsa_port *dp = dsa_to_port(ds, port);
188 struct ksz_device *dev = ds->priv;
189 struct ksz_port_mib *mib;
191 mib = &dev->ports[port].mib;
192 mutex_lock(&mib->cnt_mutex);
194 /* Only read dropped counters if no link. */
195 if (!netif_carrier_ok(dp->slave))
196 mib->cnt_ptr = dev->reg_mib_cnt;
197 port_r_cnt(dev, port);
198 memcpy(buf, mib->counters, dev->mib_cnt * sizeof(u64));
199 mutex_unlock(&mib->cnt_mutex);
201 EXPORT_SYMBOL_GPL(ksz_get_ethtool_stats);
203 int ksz_port_bridge_join(struct dsa_switch *ds, int port,
204 struct net_device *br)
206 struct ksz_device *dev = ds->priv;
208 mutex_lock(&dev->dev_mutex);
209 dev->br_member |= (1 << port);
210 mutex_unlock(&dev->dev_mutex);
212 /* port_stp_state_set() will be called after to put the port in
213 * appropriate state so there is no need to do anything.
218 EXPORT_SYMBOL_GPL(ksz_port_bridge_join);
220 void ksz_port_bridge_leave(struct dsa_switch *ds, int port,
221 struct net_device *br)
223 struct ksz_device *dev = ds->priv;
225 mutex_lock(&dev->dev_mutex);
226 dev->br_member &= ~(1 << port);
227 dev->member &= ~(1 << port);
228 mutex_unlock(&dev->dev_mutex);
230 /* port_stp_state_set() will be called after to put the port in
231 * forwarding state so there is no need to do anything.
234 EXPORT_SYMBOL_GPL(ksz_port_bridge_leave);
236 void ksz_port_fast_age(struct dsa_switch *ds, int port)
238 struct ksz_device *dev = ds->priv;
240 dev->dev_ops->flush_dyn_mac_table(dev, port);
242 EXPORT_SYMBOL_GPL(ksz_port_fast_age);
244 int ksz_port_vlan_prepare(struct dsa_switch *ds, int port,
245 const struct switchdev_obj_port_vlan *vlan)
251 EXPORT_SYMBOL_GPL(ksz_port_vlan_prepare);
253 int ksz_port_fdb_dump(struct dsa_switch *ds, int port, dsa_fdb_dump_cb_t *cb,
256 struct ksz_device *dev = ds->priv;
263 struct alu_struct alu;
266 alu.is_static = false;
267 ret = dev->dev_ops->r_dyn_mac_table(dev, i, alu.mac, &fid,
270 if (!ret && (member & BIT(port))) {
271 ret = cb(alu.mac, alu.fid, alu.is_static, data);
276 } while (i < entries);
282 EXPORT_SYMBOL_GPL(ksz_port_fdb_dump);
284 int ksz_port_mdb_prepare(struct dsa_switch *ds, int port,
285 const struct switchdev_obj_port_mdb *mdb)
290 EXPORT_SYMBOL_GPL(ksz_port_mdb_prepare);
292 void ksz_port_mdb_add(struct dsa_switch *ds, int port,
293 const struct switchdev_obj_port_mdb *mdb)
295 struct ksz_device *dev = ds->priv;
296 struct alu_struct alu;
300 alu.port_forward = 0;
301 for (index = 0; index < dev->num_statics; index++) {
302 if (!dev->dev_ops->r_sta_mac_table(dev, index, &alu)) {
303 /* Found one already in static MAC table. */
304 if (!memcmp(alu.mac, mdb->addr, ETH_ALEN) &&
307 /* Remember the first empty entry. */
313 /* no available entry */
314 if (index == dev->num_statics && !empty)
318 if (index == dev->num_statics) {
320 memset(&alu, 0, sizeof(alu));
321 memcpy(alu.mac, mdb->addr, ETH_ALEN);
322 alu.is_static = true;
324 alu.port_forward |= BIT(port);
326 alu.is_use_fid = true;
328 /* Need a way to map VID to FID. */
331 dev->dev_ops->w_sta_mac_table(dev, index, &alu);
333 EXPORT_SYMBOL_GPL(ksz_port_mdb_add);
335 int ksz_port_mdb_del(struct dsa_switch *ds, int port,
336 const struct switchdev_obj_port_mdb *mdb)
338 struct ksz_device *dev = ds->priv;
339 struct alu_struct alu;
343 for (index = 0; index < dev->num_statics; index++) {
344 if (!dev->dev_ops->r_sta_mac_table(dev, index, &alu)) {
345 /* Found one already in static MAC table. */
346 if (!memcmp(alu.mac, mdb->addr, ETH_ALEN) &&
352 /* no available entry */
353 if (index == dev->num_statics)
357 alu.port_forward &= ~BIT(port);
358 if (!alu.port_forward)
359 alu.is_static = false;
360 dev->dev_ops->w_sta_mac_table(dev, index, &alu);
365 EXPORT_SYMBOL_GPL(ksz_port_mdb_del);
367 int ksz_enable_port(struct dsa_switch *ds, int port, struct phy_device *phy)
369 struct ksz_device *dev = ds->priv;
371 /* setup slave port */
372 dev->dev_ops->port_setup(dev, port, false);
373 dev->dev_ops->phy_setup(dev, port, phy);
375 /* port_stp_state_set() will be called after to enable the port so
376 * there is no need to do anything.
381 EXPORT_SYMBOL_GPL(ksz_enable_port);
383 void ksz_disable_port(struct dsa_switch *ds, int port, struct phy_device *phy)
385 struct ksz_device *dev = ds->priv;
387 dev->on_ports &= ~(1 << port);
388 dev->live_ports &= ~(1 << port);
390 /* port_stp_state_set() will be called after to disable the port so
391 * there is no need to do anything.
394 EXPORT_SYMBOL_GPL(ksz_disable_port);
396 struct ksz_device *ksz_switch_alloc(struct device *base,
397 const struct ksz_io_ops *ops,
400 struct dsa_switch *ds;
401 struct ksz_device *swdev;
403 ds = dsa_switch_alloc(base, DSA_MAX_PORTS);
407 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(dev->reset_gpio, 1);
438 gpiod_set_value(dev->reset_gpio, 0);
441 mutex_init(&dev->dev_mutex);
442 mutex_init(&dev->reg_mutex);
443 mutex_init(&dev->stats_mutex);
444 mutex_init(&dev->alu_mutex);
445 mutex_init(&dev->vlan_mutex);
449 if (dev->dev_ops->detect(dev))
452 ret = dev->dev_ops->init(dev);
456 dev->interface = PHY_INTERFACE_MODE_MII;
457 if (dev->dev->of_node) {
458 ret = of_get_phy_mode(dev->dev->of_node);
460 dev->interface = ret;
463 ret = dsa_register_switch(dev->ds);
465 dev->dev_ops->exit(dev);
471 EXPORT_SYMBOL(ksz_switch_register);
473 void ksz_switch_remove(struct ksz_device *dev)
476 if (dev->mib_read_timer.expires) {
477 del_timer_sync(&dev->mib_read_timer);
478 flush_work(&dev->mib_read);
481 dev->dev_ops->exit(dev);
482 dsa_unregister_switch(dev->ds);
485 gpiod_set_value(dev->reset_gpio, 1);
488 EXPORT_SYMBOL(ksz_switch_remove);
490 MODULE_AUTHOR("Woojung Huh <Woojung.Huh@microchip.com>");
491 MODULE_DESCRIPTION("Microchip KSZ Series Switch DSA Driver");
492 MODULE_LICENSE("GPL");