1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2 /* Microsemi Ocelot Switch driver
4 * This contains glue logic between the switchdev driver operations and the
5 * mscc_ocelot_switch_lib.
7 * Copyright (c) 2017, 2019 Microsemi Corporation
8 * Copyright 2020-2021 NXP Semiconductors
11 #include <linux/if_bridge.h>
12 #include <net/pkt_cls.h>
14 #include "ocelot_vcap.h"
16 static struct ocelot *devlink_port_to_ocelot(struct devlink_port *dlp)
18 return devlink_priv(dlp->devlink);
21 static int devlink_port_to_port(struct devlink_port *dlp)
23 struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
25 return dlp - ocelot->devlink_ports;
28 static int ocelot_devlink_sb_pool_get(struct devlink *dl,
29 unsigned int sb_index, u16 pool_index,
30 struct devlink_sb_pool_info *pool_info)
32 struct ocelot *ocelot = devlink_priv(dl);
34 return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info);
37 static int ocelot_devlink_sb_pool_set(struct devlink *dl, unsigned int sb_index,
38 u16 pool_index, u32 size,
39 enum devlink_sb_threshold_type threshold_type,
40 struct netlink_ext_ack *extack)
42 struct ocelot *ocelot = devlink_priv(dl);
44 return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size,
45 threshold_type, extack);
48 static int ocelot_devlink_sb_port_pool_get(struct devlink_port *dlp,
49 unsigned int sb_index, u16 pool_index,
52 struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
53 int port = devlink_port_to_port(dlp);
55 return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index,
59 static int ocelot_devlink_sb_port_pool_set(struct devlink_port *dlp,
60 unsigned int sb_index, u16 pool_index,
62 struct netlink_ext_ack *extack)
64 struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
65 int port = devlink_port_to_port(dlp);
67 return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index,
72 ocelot_devlink_sb_tc_pool_bind_get(struct devlink_port *dlp,
73 unsigned int sb_index, u16 tc_index,
74 enum devlink_sb_pool_type pool_type,
75 u16 *p_pool_index, u32 *p_threshold)
77 struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
78 int port = devlink_port_to_port(dlp);
80 return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index,
81 pool_type, p_pool_index,
86 ocelot_devlink_sb_tc_pool_bind_set(struct devlink_port *dlp,
87 unsigned int sb_index, u16 tc_index,
88 enum devlink_sb_pool_type pool_type,
89 u16 pool_index, u32 threshold,
90 struct netlink_ext_ack *extack)
92 struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
93 int port = devlink_port_to_port(dlp);
95 return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index,
96 pool_type, pool_index, threshold,
100 static int ocelot_devlink_sb_occ_snapshot(struct devlink *dl,
101 unsigned int sb_index)
103 struct ocelot *ocelot = devlink_priv(dl);
105 return ocelot_sb_occ_snapshot(ocelot, sb_index);
108 static int ocelot_devlink_sb_occ_max_clear(struct devlink *dl,
109 unsigned int sb_index)
111 struct ocelot *ocelot = devlink_priv(dl);
113 return ocelot_sb_occ_max_clear(ocelot, sb_index);
116 static int ocelot_devlink_sb_occ_port_pool_get(struct devlink_port *dlp,
117 unsigned int sb_index,
118 u16 pool_index, u32 *p_cur,
121 struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
122 int port = devlink_port_to_port(dlp);
124 return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index,
129 ocelot_devlink_sb_occ_tc_port_bind_get(struct devlink_port *dlp,
130 unsigned int sb_index, u16 tc_index,
131 enum devlink_sb_pool_type pool_type,
132 u32 *p_cur, u32 *p_max)
134 struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
135 int port = devlink_port_to_port(dlp);
137 return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index,
142 const struct devlink_ops ocelot_devlink_ops = {
143 .sb_pool_get = ocelot_devlink_sb_pool_get,
144 .sb_pool_set = ocelot_devlink_sb_pool_set,
145 .sb_port_pool_get = ocelot_devlink_sb_port_pool_get,
146 .sb_port_pool_set = ocelot_devlink_sb_port_pool_set,
147 .sb_tc_pool_bind_get = ocelot_devlink_sb_tc_pool_bind_get,
148 .sb_tc_pool_bind_set = ocelot_devlink_sb_tc_pool_bind_set,
149 .sb_occ_snapshot = ocelot_devlink_sb_occ_snapshot,
150 .sb_occ_max_clear = ocelot_devlink_sb_occ_max_clear,
151 .sb_occ_port_pool_get = ocelot_devlink_sb_occ_port_pool_get,
152 .sb_occ_tc_port_bind_get = ocelot_devlink_sb_occ_tc_port_bind_get,
155 int ocelot_port_devlink_init(struct ocelot *ocelot, int port,
156 enum devlink_port_flavour flavour)
158 struct devlink_port *dlp = &ocelot->devlink_ports[port];
159 int id_len = sizeof(ocelot->base_mac);
160 struct devlink *dl = ocelot->devlink;
161 struct devlink_port_attrs attrs = {};
163 memcpy(attrs.switch_id.id, &ocelot->base_mac, id_len);
164 attrs.switch_id.id_len = id_len;
165 attrs.phys.port_number = port;
166 attrs.flavour = flavour;
168 devlink_port_attrs_set(dlp, &attrs);
170 return devlink_port_register(dl, dlp, port);
173 void ocelot_port_devlink_teardown(struct ocelot *ocelot, int port)
175 struct devlink_port *dlp = &ocelot->devlink_ports[port];
177 devlink_port_unregister(dlp);
180 static struct devlink_port *ocelot_get_devlink_port(struct net_device *dev)
182 struct ocelot_port_private *priv = netdev_priv(dev);
183 struct ocelot *ocelot = priv->port.ocelot;
184 int port = priv->chip_port;
186 return &ocelot->devlink_ports[port];
189 int ocelot_setup_tc_cls_flower(struct ocelot_port_private *priv,
190 struct flow_cls_offload *f,
193 struct ocelot *ocelot = priv->port.ocelot;
194 int port = priv->chip_port;
199 switch (f->command) {
200 case FLOW_CLS_REPLACE:
201 return ocelot_cls_flower_replace(ocelot, port, f, ingress);
202 case FLOW_CLS_DESTROY:
203 return ocelot_cls_flower_destroy(ocelot, port, f, ingress);
205 return ocelot_cls_flower_stats(ocelot, port, f, ingress);
211 static int ocelot_setup_tc_cls_matchall(struct ocelot_port_private *priv,
212 struct tc_cls_matchall_offload *f,
215 struct netlink_ext_ack *extack = f->common.extack;
216 struct ocelot *ocelot = priv->port.ocelot;
217 struct ocelot_policer pol = { 0 };
218 struct flow_action_entry *action;
219 int port = priv->chip_port;
223 NL_SET_ERR_MSG_MOD(extack, "Only ingress is supported");
227 switch (f->command) {
228 case TC_CLSMATCHALL_REPLACE:
229 if (!flow_offload_has_one_action(&f->rule->action)) {
230 NL_SET_ERR_MSG_MOD(extack,
231 "Only one action is supported");
235 if (priv->tc.block_shared) {
236 NL_SET_ERR_MSG_MOD(extack,
237 "Rate limit is not supported on shared blocks");
241 action = &f->rule->action.entries[0];
243 if (action->id != FLOW_ACTION_POLICE) {
244 NL_SET_ERR_MSG_MOD(extack, "Unsupported action");
248 if (priv->tc.police_id && priv->tc.police_id != f->cookie) {
249 NL_SET_ERR_MSG_MOD(extack,
250 "Only one policer per port is supported");
254 if (action->police.rate_pkt_ps) {
255 NL_SET_ERR_MSG_MOD(extack,
256 "QoS offload not support packets per second");
260 pol.rate = (u32)div_u64(action->police.rate_bytes_ps, 1000) * 8;
261 pol.burst = action->police.burst;
263 err = ocelot_port_policer_add(ocelot, port, &pol);
265 NL_SET_ERR_MSG_MOD(extack, "Could not add policer");
269 priv->tc.police_id = f->cookie;
270 priv->tc.offload_cnt++;
272 case TC_CLSMATCHALL_DESTROY:
273 if (priv->tc.police_id != f->cookie)
276 err = ocelot_port_policer_del(ocelot, port);
278 NL_SET_ERR_MSG_MOD(extack,
279 "Could not delete policer");
282 priv->tc.police_id = 0;
283 priv->tc.offload_cnt--;
285 case TC_CLSMATCHALL_STATS:
291 static int ocelot_setup_tc_block_cb(enum tc_setup_type type,
293 void *cb_priv, bool ingress)
295 struct ocelot_port_private *priv = cb_priv;
297 if (!tc_cls_can_offload_and_chain0(priv->dev, type_data))
301 case TC_SETUP_CLSMATCHALL:
302 return ocelot_setup_tc_cls_matchall(priv, type_data, ingress);
303 case TC_SETUP_CLSFLOWER:
304 return ocelot_setup_tc_cls_flower(priv, type_data, ingress);
310 static int ocelot_setup_tc_block_cb_ig(enum tc_setup_type type,
314 return ocelot_setup_tc_block_cb(type, type_data,
318 static int ocelot_setup_tc_block_cb_eg(enum tc_setup_type type,
322 return ocelot_setup_tc_block_cb(type, type_data,
326 static LIST_HEAD(ocelot_block_cb_list);
328 static int ocelot_setup_tc_block(struct ocelot_port_private *priv,
329 struct flow_block_offload *f)
331 struct flow_block_cb *block_cb;
334 if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS) {
335 cb = ocelot_setup_tc_block_cb_ig;
336 priv->tc.block_shared = f->block_shared;
337 } else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS) {
338 cb = ocelot_setup_tc_block_cb_eg;
343 f->driver_block_list = &ocelot_block_cb_list;
345 switch (f->command) {
346 case FLOW_BLOCK_BIND:
347 if (flow_block_cb_is_busy(cb, priv, &ocelot_block_cb_list))
350 block_cb = flow_block_cb_alloc(cb, priv, priv, NULL);
351 if (IS_ERR(block_cb))
352 return PTR_ERR(block_cb);
354 flow_block_cb_add(block_cb, f);
355 list_add_tail(&block_cb->driver_list, f->driver_block_list);
357 case FLOW_BLOCK_UNBIND:
358 block_cb = flow_block_cb_lookup(f->block, cb, priv);
362 flow_block_cb_remove(block_cb, f);
363 list_del(&block_cb->driver_list);
370 static int ocelot_setup_tc(struct net_device *dev, enum tc_setup_type type,
373 struct ocelot_port_private *priv = netdev_priv(dev);
377 return ocelot_setup_tc_block(priv, type_data);
384 static void ocelot_port_adjust_link(struct net_device *dev)
386 struct ocelot_port_private *priv = netdev_priv(dev);
387 struct ocelot *ocelot = priv->port.ocelot;
388 int port = priv->chip_port;
390 ocelot_adjust_link(ocelot, port, dev->phydev);
393 static int ocelot_vlan_vid_prepare(struct net_device *dev, u16 vid, bool pvid,
396 struct ocelot_port_private *priv = netdev_priv(dev);
397 struct ocelot_port *ocelot_port = &priv->port;
398 struct ocelot *ocelot = ocelot_port->ocelot;
399 int port = priv->chip_port;
401 return ocelot_vlan_prepare(ocelot, port, vid, pvid, untagged);
404 static int ocelot_vlan_vid_add(struct net_device *dev, u16 vid, bool pvid,
407 struct ocelot_port_private *priv = netdev_priv(dev);
408 struct ocelot_port *ocelot_port = &priv->port;
409 struct ocelot *ocelot = ocelot_port->ocelot;
410 int port = priv->chip_port;
413 ret = ocelot_vlan_add(ocelot, port, vid, pvid, untagged);
417 /* Add the port MAC address to with the right VLAN information */
418 ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, vid,
424 static int ocelot_vlan_vid_del(struct net_device *dev, u16 vid)
426 struct ocelot_port_private *priv = netdev_priv(dev);
427 struct ocelot *ocelot = priv->port.ocelot;
428 int port = priv->chip_port;
431 /* 8021q removes VID 0 on module unload for all interfaces
432 * with VLAN filtering feature. We need to keep it to receive
438 ret = ocelot_vlan_del(ocelot, port, vid);
442 /* Del the port MAC address to with the right VLAN information */
443 ocelot_mact_forget(ocelot, dev->dev_addr, vid);
448 static int ocelot_port_open(struct net_device *dev)
450 struct ocelot_port_private *priv = netdev_priv(dev);
451 struct ocelot_port *ocelot_port = &priv->port;
452 struct ocelot *ocelot = ocelot_port->ocelot;
453 int port = priv->chip_port;
457 err = phy_set_mode_ext(priv->serdes, PHY_MODE_ETHERNET,
458 ocelot_port->phy_mode);
460 netdev_err(dev, "Could not set mode of SerDes\n");
465 err = phy_connect_direct(dev, priv->phy, &ocelot_port_adjust_link,
466 ocelot_port->phy_mode);
468 netdev_err(dev, "Could not attach to PHY\n");
472 dev->phydev = priv->phy;
474 phy_attached_info(priv->phy);
475 phy_start(priv->phy);
477 ocelot_port_enable(ocelot, port, priv->phy);
482 static int ocelot_port_stop(struct net_device *dev)
484 struct ocelot_port_private *priv = netdev_priv(dev);
485 struct ocelot *ocelot = priv->port.ocelot;
486 int port = priv->chip_port;
488 phy_disconnect(priv->phy);
492 ocelot_port_disable(ocelot, port);
497 static netdev_tx_t ocelot_port_xmit(struct sk_buff *skb, struct net_device *dev)
499 struct ocelot_port_private *priv = netdev_priv(dev);
500 struct ocelot_port *ocelot_port = &priv->port;
501 struct ocelot *ocelot = ocelot_port->ocelot;
502 int port = priv->chip_port;
505 if (!ocelot_can_inject(ocelot, 0))
506 return NETDEV_TX_BUSY;
508 /* Check if timestamping is needed */
509 if (ocelot->ptp && (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
510 struct sk_buff *clone = NULL;
512 if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) {
518 OCELOT_SKB_CB(skb)->clone = clone;
520 rew_op = ocelot_ptp_rew_op(skb);
523 ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
530 enum ocelot_action_type {
535 struct ocelot_mact_work_ctx {
536 struct work_struct work;
537 struct ocelot *ocelot;
538 enum ocelot_action_type type;
540 /* OCELOT_MACT_LEARN */
542 unsigned char addr[ETH_ALEN];
544 enum macaccess_entry_type entry_type;
547 /* OCELOT_MACT_FORGET */
549 unsigned char addr[ETH_ALEN];
555 #define ocelot_work_to_ctx(x) \
556 container_of((x), struct ocelot_mact_work_ctx, work)
558 static void ocelot_mact_work(struct work_struct *work)
560 struct ocelot_mact_work_ctx *w = ocelot_work_to_ctx(work);
561 struct ocelot *ocelot = w->ocelot;
564 case OCELOT_MACT_LEARN:
565 ocelot_mact_learn(ocelot, w->learn.pgid, w->learn.addr,
566 w->learn.vid, w->learn.entry_type);
568 case OCELOT_MACT_FORGET:
569 ocelot_mact_forget(ocelot, w->forget.addr, w->forget.vid);
578 static int ocelot_enqueue_mact_action(struct ocelot *ocelot,
579 const struct ocelot_mact_work_ctx *ctx)
581 struct ocelot_mact_work_ctx *w = kmemdup(ctx, sizeof(*w), GFP_ATOMIC);
587 INIT_WORK(&w->work, ocelot_mact_work);
588 queue_work(ocelot->owq, &w->work);
593 static int ocelot_mc_unsync(struct net_device *dev, const unsigned char *addr)
595 struct ocelot_port_private *priv = netdev_priv(dev);
596 struct ocelot_port *ocelot_port = &priv->port;
597 struct ocelot *ocelot = ocelot_port->ocelot;
598 struct ocelot_mact_work_ctx w;
600 ether_addr_copy(w.forget.addr, addr);
601 w.forget.vid = ocelot_port->pvid_vlan.vid;
602 w.type = OCELOT_MACT_FORGET;
604 return ocelot_enqueue_mact_action(ocelot, &w);
607 static int ocelot_mc_sync(struct net_device *dev, const unsigned char *addr)
609 struct ocelot_port_private *priv = netdev_priv(dev);
610 struct ocelot_port *ocelot_port = &priv->port;
611 struct ocelot *ocelot = ocelot_port->ocelot;
612 struct ocelot_mact_work_ctx w;
614 ether_addr_copy(w.learn.addr, addr);
615 w.learn.vid = ocelot_port->pvid_vlan.vid;
616 w.learn.pgid = PGID_CPU;
617 w.learn.entry_type = ENTRYTYPE_LOCKED;
618 w.type = OCELOT_MACT_LEARN;
620 return ocelot_enqueue_mact_action(ocelot, &w);
623 static void ocelot_set_rx_mode(struct net_device *dev)
625 struct ocelot_port_private *priv = netdev_priv(dev);
626 struct ocelot *ocelot = priv->port.ocelot;
630 /* This doesn't handle promiscuous mode because the bridge core is
631 * setting IFF_PROMISC on all slave interfaces and all frames would be
632 * forwarded to the CPU port.
634 val = GENMASK(ocelot->num_phys_ports - 1, 0);
635 for_each_nonreserved_multicast_dest_pgid(ocelot, i)
636 ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
638 __dev_mc_sync(dev, ocelot_mc_sync, ocelot_mc_unsync);
641 static int ocelot_port_set_mac_address(struct net_device *dev, void *p)
643 struct ocelot_port_private *priv = netdev_priv(dev);
644 struct ocelot_port *ocelot_port = &priv->port;
645 struct ocelot *ocelot = ocelot_port->ocelot;
646 const struct sockaddr *addr = p;
648 /* Learn the new net device MAC address in the mac table. */
649 ocelot_mact_learn(ocelot, PGID_CPU, addr->sa_data,
650 ocelot_port->pvid_vlan.vid, ENTRYTYPE_LOCKED);
651 /* Then forget the previous one. */
652 ocelot_mact_forget(ocelot, dev->dev_addr, ocelot_port->pvid_vlan.vid);
654 ether_addr_copy(dev->dev_addr, addr->sa_data);
658 static void ocelot_get_stats64(struct net_device *dev,
659 struct rtnl_link_stats64 *stats)
661 struct ocelot_port_private *priv = netdev_priv(dev);
662 struct ocelot *ocelot = priv->port.ocelot;
663 int port = priv->chip_port;
665 /* Configure the port to read the stats from */
666 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port),
670 stats->rx_bytes = ocelot_read(ocelot, SYS_COUNT_RX_OCTETS);
671 stats->rx_packets = ocelot_read(ocelot, SYS_COUNT_RX_SHORTS) +
672 ocelot_read(ocelot, SYS_COUNT_RX_FRAGMENTS) +
673 ocelot_read(ocelot, SYS_COUNT_RX_JABBERS) +
674 ocelot_read(ocelot, SYS_COUNT_RX_LONGS) +
675 ocelot_read(ocelot, SYS_COUNT_RX_64) +
676 ocelot_read(ocelot, SYS_COUNT_RX_65_127) +
677 ocelot_read(ocelot, SYS_COUNT_RX_128_255) +
678 ocelot_read(ocelot, SYS_COUNT_RX_256_1023) +
679 ocelot_read(ocelot, SYS_COUNT_RX_1024_1526) +
680 ocelot_read(ocelot, SYS_COUNT_RX_1527_MAX);
681 stats->multicast = ocelot_read(ocelot, SYS_COUNT_RX_MULTICAST);
682 stats->rx_dropped = dev->stats.rx_dropped;
685 stats->tx_bytes = ocelot_read(ocelot, SYS_COUNT_TX_OCTETS);
686 stats->tx_packets = ocelot_read(ocelot, SYS_COUNT_TX_64) +
687 ocelot_read(ocelot, SYS_COUNT_TX_65_127) +
688 ocelot_read(ocelot, SYS_COUNT_TX_128_511) +
689 ocelot_read(ocelot, SYS_COUNT_TX_512_1023) +
690 ocelot_read(ocelot, SYS_COUNT_TX_1024_1526) +
691 ocelot_read(ocelot, SYS_COUNT_TX_1527_MAX);
692 stats->tx_dropped = ocelot_read(ocelot, SYS_COUNT_TX_DROPS) +
693 ocelot_read(ocelot, SYS_COUNT_TX_AGING);
694 stats->collisions = ocelot_read(ocelot, SYS_COUNT_TX_COLLISION);
697 static int ocelot_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
698 struct net_device *dev,
699 const unsigned char *addr,
701 struct netlink_ext_ack *extack)
703 struct ocelot_port_private *priv = netdev_priv(dev);
704 struct ocelot *ocelot = priv->port.ocelot;
705 int port = priv->chip_port;
707 return ocelot_fdb_add(ocelot, port, addr, vid);
710 static int ocelot_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
711 struct net_device *dev,
712 const unsigned char *addr, u16 vid)
714 struct ocelot_port_private *priv = netdev_priv(dev);
715 struct ocelot *ocelot = priv->port.ocelot;
716 int port = priv->chip_port;
718 return ocelot_fdb_del(ocelot, port, addr, vid);
721 static int ocelot_port_fdb_dump(struct sk_buff *skb,
722 struct netlink_callback *cb,
723 struct net_device *dev,
724 struct net_device *filter_dev, int *idx)
726 struct ocelot_port_private *priv = netdev_priv(dev);
727 struct ocelot *ocelot = priv->port.ocelot;
728 struct ocelot_dump_ctx dump = {
734 int port = priv->chip_port;
737 ret = ocelot_fdb_dump(ocelot, port, ocelot_port_fdb_do_dump, &dump);
744 static int ocelot_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
747 return ocelot_vlan_vid_add(dev, vid, false, false);
750 static int ocelot_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
753 return ocelot_vlan_vid_del(dev, vid);
756 static void ocelot_vlan_mode(struct ocelot *ocelot, int port,
757 netdev_features_t features)
762 val = ocelot_read(ocelot, ANA_VLANMASK);
763 if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
767 ocelot_write(ocelot, val, ANA_VLANMASK);
770 static int ocelot_set_features(struct net_device *dev,
771 netdev_features_t features)
773 netdev_features_t changed = dev->features ^ features;
774 struct ocelot_port_private *priv = netdev_priv(dev);
775 struct ocelot *ocelot = priv->port.ocelot;
776 int port = priv->chip_port;
778 if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC) &&
779 priv->tc.offload_cnt) {
781 "Cannot disable HW TC offload while offloads active\n");
785 if (changed & NETIF_F_HW_VLAN_CTAG_FILTER)
786 ocelot_vlan_mode(ocelot, port, features);
791 static int ocelot_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
793 struct ocelot_port_private *priv = netdev_priv(dev);
794 struct ocelot *ocelot = priv->port.ocelot;
795 int port = priv->chip_port;
797 /* If the attached PHY device isn't capable of timestamping operations,
798 * use our own (when possible).
800 if (!phy_has_hwtstamp(dev->phydev) && ocelot->ptp) {
803 return ocelot_hwstamp_set(ocelot, port, ifr);
805 return ocelot_hwstamp_get(ocelot, port, ifr);
809 return phy_mii_ioctl(dev->phydev, ifr, cmd);
812 static const struct net_device_ops ocelot_port_netdev_ops = {
813 .ndo_open = ocelot_port_open,
814 .ndo_stop = ocelot_port_stop,
815 .ndo_start_xmit = ocelot_port_xmit,
816 .ndo_set_rx_mode = ocelot_set_rx_mode,
817 .ndo_set_mac_address = ocelot_port_set_mac_address,
818 .ndo_get_stats64 = ocelot_get_stats64,
819 .ndo_fdb_add = ocelot_port_fdb_add,
820 .ndo_fdb_del = ocelot_port_fdb_del,
821 .ndo_fdb_dump = ocelot_port_fdb_dump,
822 .ndo_vlan_rx_add_vid = ocelot_vlan_rx_add_vid,
823 .ndo_vlan_rx_kill_vid = ocelot_vlan_rx_kill_vid,
824 .ndo_set_features = ocelot_set_features,
825 .ndo_setup_tc = ocelot_setup_tc,
826 .ndo_do_ioctl = ocelot_ioctl,
827 .ndo_get_devlink_port = ocelot_get_devlink_port,
830 struct net_device *ocelot_port_to_netdev(struct ocelot *ocelot, int port)
832 struct ocelot_port *ocelot_port = ocelot->ports[port];
833 struct ocelot_port_private *priv;
838 priv = container_of(ocelot_port, struct ocelot_port_private, port);
843 /* Checks if the net_device instance given to us originates from our driver */
844 static bool ocelot_netdevice_dev_check(const struct net_device *dev)
846 return dev->netdev_ops == &ocelot_port_netdev_ops;
849 int ocelot_netdev_to_port(struct net_device *dev)
851 struct ocelot_port_private *priv;
853 if (!dev || !ocelot_netdevice_dev_check(dev))
856 priv = netdev_priv(dev);
858 return priv->chip_port;
861 static void ocelot_port_get_strings(struct net_device *netdev, u32 sset,
864 struct ocelot_port_private *priv = netdev_priv(netdev);
865 struct ocelot *ocelot = priv->port.ocelot;
866 int port = priv->chip_port;
868 ocelot_get_strings(ocelot, port, sset, data);
871 static void ocelot_port_get_ethtool_stats(struct net_device *dev,
872 struct ethtool_stats *stats,
875 struct ocelot_port_private *priv = netdev_priv(dev);
876 struct ocelot *ocelot = priv->port.ocelot;
877 int port = priv->chip_port;
879 ocelot_get_ethtool_stats(ocelot, port, data);
882 static int ocelot_port_get_sset_count(struct net_device *dev, int sset)
884 struct ocelot_port_private *priv = netdev_priv(dev);
885 struct ocelot *ocelot = priv->port.ocelot;
886 int port = priv->chip_port;
888 return ocelot_get_sset_count(ocelot, port, sset);
891 static int ocelot_port_get_ts_info(struct net_device *dev,
892 struct ethtool_ts_info *info)
894 struct ocelot_port_private *priv = netdev_priv(dev);
895 struct ocelot *ocelot = priv->port.ocelot;
896 int port = priv->chip_port;
899 return ethtool_op_get_ts_info(dev, info);
901 return ocelot_get_ts_info(ocelot, port, info);
904 static const struct ethtool_ops ocelot_ethtool_ops = {
905 .get_strings = ocelot_port_get_strings,
906 .get_ethtool_stats = ocelot_port_get_ethtool_stats,
907 .get_sset_count = ocelot_port_get_sset_count,
908 .get_link_ksettings = phy_ethtool_get_link_ksettings,
909 .set_link_ksettings = phy_ethtool_set_link_ksettings,
910 .get_ts_info = ocelot_port_get_ts_info,
913 static void ocelot_port_attr_stp_state_set(struct ocelot *ocelot, int port,
916 ocelot_bridge_stp_state_set(ocelot, port, state);
919 static void ocelot_port_attr_ageing_set(struct ocelot *ocelot, int port,
920 unsigned long ageing_clock_t)
922 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
923 u32 ageing_time = jiffies_to_msecs(ageing_jiffies);
925 ocelot_set_ageing_time(ocelot, ageing_time);
928 static void ocelot_port_attr_mc_set(struct ocelot *ocelot, int port, bool mc)
930 u32 cpu_fwd_mcast = ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
931 ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
932 ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA;
938 ocelot_rmw_gix(ocelot, val, cpu_fwd_mcast,
939 ANA_PORT_CPU_FWD_CFG, port);
942 static int ocelot_port_attr_set(struct net_device *dev, const void *ctx,
943 const struct switchdev_attr *attr,
944 struct netlink_ext_ack *extack)
946 struct ocelot_port_private *priv = netdev_priv(dev);
947 struct ocelot *ocelot = priv->port.ocelot;
948 int port = priv->chip_port;
951 if (ctx && ctx != priv)
955 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
956 ocelot_port_attr_stp_state_set(ocelot, port, attr->u.stp_state);
958 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
959 ocelot_port_attr_ageing_set(ocelot, port, attr->u.ageing_time);
961 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
962 ocelot_port_vlan_filtering(ocelot, port, attr->u.vlan_filtering);
964 case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
965 ocelot_port_attr_mc_set(ocelot, port, !attr->u.mc_disabled);
967 case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
968 err = ocelot_port_pre_bridge_flags(ocelot, port,
969 attr->u.brport_flags);
971 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
972 ocelot_port_bridge_flags(ocelot, port, attr->u.brport_flags);
982 static int ocelot_port_obj_add_vlan(struct net_device *dev,
983 const struct switchdev_obj_port_vlan *vlan)
985 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
986 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
989 ret = ocelot_vlan_vid_prepare(dev, vlan->vid, pvid, untagged);
993 return ocelot_vlan_vid_add(dev, vlan->vid, pvid, untagged);
996 static int ocelot_port_obj_add_mdb(struct net_device *dev,
997 const struct switchdev_obj_port_mdb *mdb)
999 struct ocelot_port_private *priv = netdev_priv(dev);
1000 struct ocelot_port *ocelot_port = &priv->port;
1001 struct ocelot *ocelot = ocelot_port->ocelot;
1002 int port = priv->chip_port;
1004 return ocelot_port_mdb_add(ocelot, port, mdb);
1007 static int ocelot_port_obj_del_mdb(struct net_device *dev,
1008 const struct switchdev_obj_port_mdb *mdb)
1010 struct ocelot_port_private *priv = netdev_priv(dev);
1011 struct ocelot_port *ocelot_port = &priv->port;
1012 struct ocelot *ocelot = ocelot_port->ocelot;
1013 int port = priv->chip_port;
1015 return ocelot_port_mdb_del(ocelot, port, mdb);
1018 static int ocelot_port_obj_mrp_add(struct net_device *dev,
1019 const struct switchdev_obj_mrp *mrp)
1021 struct ocelot_port_private *priv = netdev_priv(dev);
1022 struct ocelot_port *ocelot_port = &priv->port;
1023 struct ocelot *ocelot = ocelot_port->ocelot;
1024 int port = priv->chip_port;
1026 return ocelot_mrp_add(ocelot, port, mrp);
1029 static int ocelot_port_obj_mrp_del(struct net_device *dev,
1030 const struct switchdev_obj_mrp *mrp)
1032 struct ocelot_port_private *priv = netdev_priv(dev);
1033 struct ocelot_port *ocelot_port = &priv->port;
1034 struct ocelot *ocelot = ocelot_port->ocelot;
1035 int port = priv->chip_port;
1037 return ocelot_mrp_del(ocelot, port, mrp);
1041 ocelot_port_obj_mrp_add_ring_role(struct net_device *dev,
1042 const struct switchdev_obj_ring_role_mrp *mrp)
1044 struct ocelot_port_private *priv = netdev_priv(dev);
1045 struct ocelot_port *ocelot_port = &priv->port;
1046 struct ocelot *ocelot = ocelot_port->ocelot;
1047 int port = priv->chip_port;
1049 return ocelot_mrp_add_ring_role(ocelot, port, mrp);
1053 ocelot_port_obj_mrp_del_ring_role(struct net_device *dev,
1054 const struct switchdev_obj_ring_role_mrp *mrp)
1056 struct ocelot_port_private *priv = netdev_priv(dev);
1057 struct ocelot_port *ocelot_port = &priv->port;
1058 struct ocelot *ocelot = ocelot_port->ocelot;
1059 int port = priv->chip_port;
1061 return ocelot_mrp_del_ring_role(ocelot, port, mrp);
1064 static int ocelot_port_obj_add(struct net_device *dev, const void *ctx,
1065 const struct switchdev_obj *obj,
1066 struct netlink_ext_ack *extack)
1068 struct ocelot_port_private *priv = netdev_priv(dev);
1071 if (ctx && ctx != priv)
1075 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1076 ret = ocelot_port_obj_add_vlan(dev,
1077 SWITCHDEV_OBJ_PORT_VLAN(obj));
1079 case SWITCHDEV_OBJ_ID_PORT_MDB:
1080 ret = ocelot_port_obj_add_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
1082 case SWITCHDEV_OBJ_ID_MRP:
1083 ret = ocelot_port_obj_mrp_add(dev, SWITCHDEV_OBJ_MRP(obj));
1085 case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
1086 ret = ocelot_port_obj_mrp_add_ring_role(dev,
1087 SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
1096 static int ocelot_port_obj_del(struct net_device *dev, const void *ctx,
1097 const struct switchdev_obj *obj)
1099 struct ocelot_port_private *priv = netdev_priv(dev);
1102 if (ctx && ctx != priv)
1106 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1107 ret = ocelot_vlan_vid_del(dev,
1108 SWITCHDEV_OBJ_PORT_VLAN(obj)->vid);
1110 case SWITCHDEV_OBJ_ID_PORT_MDB:
1111 ret = ocelot_port_obj_del_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
1113 case SWITCHDEV_OBJ_ID_MRP:
1114 ret = ocelot_port_obj_mrp_del(dev, SWITCHDEV_OBJ_MRP(obj));
1116 case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
1117 ret = ocelot_port_obj_mrp_del_ring_role(dev,
1118 SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
1127 static void ocelot_inherit_brport_flags(struct ocelot *ocelot, int port,
1128 struct net_device *brport_dev)
1130 struct switchdev_brport_flags flags = {0};
1133 flags.mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
1135 for_each_set_bit(flag, &flags.mask, 32)
1136 if (br_port_flag_is_set(brport_dev, BIT(flag)))
1137 flags.val |= BIT(flag);
1139 ocelot_port_bridge_flags(ocelot, port, flags);
1142 static void ocelot_clear_brport_flags(struct ocelot *ocelot, int port)
1144 struct switchdev_brport_flags flags;
1146 flags.mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
1147 flags.val = flags.mask & ~BR_LEARNING;
1149 ocelot_port_bridge_flags(ocelot, port, flags);
1152 static int ocelot_switchdev_sync(struct ocelot *ocelot, int port,
1153 struct net_device *brport_dev,
1154 struct net_device *bridge_dev,
1155 struct netlink_ext_ack *extack)
1157 struct ocelot_port *ocelot_port = ocelot->ports[port];
1158 struct ocelot_port_private *priv;
1159 clock_t ageing_time;
1163 priv = container_of(ocelot_port, struct ocelot_port_private, port);
1165 ocelot_inherit_brport_flags(ocelot, port, brport_dev);
1167 stp_state = br_port_get_stp_state(brport_dev);
1168 ocelot_bridge_stp_state_set(ocelot, port, stp_state);
1170 err = ocelot_port_vlan_filtering(ocelot, port,
1171 br_vlan_enabled(bridge_dev));
1175 ageing_time = br_get_ageing_time(bridge_dev);
1176 ocelot_port_attr_ageing_set(ocelot, port, ageing_time);
1178 err = br_mdb_replay(bridge_dev, brport_dev, priv, true,
1179 &ocelot_switchdev_blocking_nb, extack);
1180 if (err && err != -EOPNOTSUPP)
1183 err = br_vlan_replay(bridge_dev, brport_dev, priv, true,
1184 &ocelot_switchdev_blocking_nb, extack);
1185 if (err && err != -EOPNOTSUPP)
1191 static int ocelot_switchdev_unsync(struct ocelot *ocelot, int port)
1195 err = ocelot_port_vlan_filtering(ocelot, port, false);
1199 ocelot_clear_brport_flags(ocelot, port);
1201 ocelot_bridge_stp_state_set(ocelot, port, BR_STATE_FORWARDING);
1206 static int ocelot_netdevice_bridge_join(struct net_device *dev,
1207 struct net_device *brport_dev,
1208 struct net_device *bridge,
1209 struct netlink_ext_ack *extack)
1211 struct ocelot_port_private *priv = netdev_priv(dev);
1212 struct ocelot_port *ocelot_port = &priv->port;
1213 struct ocelot *ocelot = ocelot_port->ocelot;
1214 int port = priv->chip_port;
1217 ocelot_port_bridge_join(ocelot, port, bridge);
1219 err = ocelot_switchdev_sync(ocelot, port, brport_dev, bridge, extack);
1221 goto err_switchdev_sync;
1226 ocelot_port_bridge_leave(ocelot, port, bridge);
1230 static int ocelot_netdevice_bridge_leave(struct net_device *dev,
1231 struct net_device *brport_dev,
1232 struct net_device *bridge)
1234 struct ocelot_port_private *priv = netdev_priv(dev);
1235 struct ocelot_port *ocelot_port = &priv->port;
1236 struct ocelot *ocelot = ocelot_port->ocelot;
1237 int port = priv->chip_port;
1240 err = ocelot_switchdev_unsync(ocelot, port);
1244 ocelot_port_bridge_leave(ocelot, port, bridge);
1249 static int ocelot_netdevice_lag_join(struct net_device *dev,
1250 struct net_device *bond,
1251 struct netdev_lag_upper_info *info,
1252 struct netlink_ext_ack *extack)
1254 struct ocelot_port_private *priv = netdev_priv(dev);
1255 struct ocelot_port *ocelot_port = &priv->port;
1256 struct ocelot *ocelot = ocelot_port->ocelot;
1257 struct net_device *bridge_dev;
1258 int port = priv->chip_port;
1261 err = ocelot_port_lag_join(ocelot, port, bond, info);
1262 if (err == -EOPNOTSUPP) {
1263 NL_SET_ERR_MSG_MOD(extack, "Offloading not supported");
1267 bridge_dev = netdev_master_upper_dev_get(bond);
1268 if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
1271 err = ocelot_netdevice_bridge_join(dev, bond, bridge_dev, extack);
1273 goto err_bridge_join;
1278 ocelot_port_lag_leave(ocelot, port, bond);
1282 static int ocelot_netdevice_lag_leave(struct net_device *dev,
1283 struct net_device *bond)
1285 struct ocelot_port_private *priv = netdev_priv(dev);
1286 struct ocelot_port *ocelot_port = &priv->port;
1287 struct ocelot *ocelot = ocelot_port->ocelot;
1288 struct net_device *bridge_dev;
1289 int port = priv->chip_port;
1291 ocelot_port_lag_leave(ocelot, port, bond);
1293 bridge_dev = netdev_master_upper_dev_get(bond);
1294 if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
1297 return ocelot_netdevice_bridge_leave(dev, bond, bridge_dev);
1300 static int ocelot_netdevice_changeupper(struct net_device *dev,
1301 struct netdev_notifier_changeupper_info *info)
1303 struct netlink_ext_ack *extack;
1306 extack = netdev_notifier_info_to_extack(&info->info);
1308 if (netif_is_bridge_master(info->upper_dev)) {
1310 err = ocelot_netdevice_bridge_join(dev, dev,
1314 err = ocelot_netdevice_bridge_leave(dev, dev,
1317 if (netif_is_lag_master(info->upper_dev)) {
1319 err = ocelot_netdevice_lag_join(dev, info->upper_dev,
1320 info->upper_info, extack);
1322 ocelot_netdevice_lag_leave(dev, info->upper_dev);
1325 return notifier_from_errno(err);
1328 /* Treat CHANGEUPPER events on an offloaded LAG as individual CHANGEUPPER
1329 * events for the lower physical ports of the LAG.
1330 * If the LAG upper isn't offloaded, ignore its CHANGEUPPER events.
1331 * In case the LAG joined a bridge, notify that we are offloading it and can do
1332 * forwarding in hardware towards it.
1335 ocelot_netdevice_lag_changeupper(struct net_device *dev,
1336 struct netdev_notifier_changeupper_info *info)
1338 struct net_device *lower;
1339 struct list_head *iter;
1340 int err = NOTIFY_DONE;
1342 netdev_for_each_lower_dev(dev, lower, iter) {
1343 struct ocelot_port_private *priv = netdev_priv(lower);
1344 struct ocelot_port *ocelot_port = &priv->port;
1346 if (ocelot_port->bond != dev)
1349 err = ocelot_netdevice_changeupper(lower, info);
1351 return notifier_from_errno(err);
1358 ocelot_netdevice_changelowerstate(struct net_device *dev,
1359 struct netdev_lag_lower_state_info *info)
1361 struct ocelot_port_private *priv = netdev_priv(dev);
1362 bool is_active = info->link_up && info->tx_enabled;
1363 struct ocelot_port *ocelot_port = &priv->port;
1364 struct ocelot *ocelot = ocelot_port->ocelot;
1365 int port = priv->chip_port;
1367 if (!ocelot_port->bond)
1370 if (ocelot_port->lag_tx_active == is_active)
1373 ocelot_port_lag_change(ocelot, port, is_active);
1378 static int ocelot_netdevice_event(struct notifier_block *unused,
1379 unsigned long event, void *ptr)
1381 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1384 case NETDEV_CHANGEUPPER: {
1385 struct netdev_notifier_changeupper_info *info = ptr;
1387 if (ocelot_netdevice_dev_check(dev))
1388 return ocelot_netdevice_changeupper(dev, info);
1390 if (netif_is_lag_master(dev))
1391 return ocelot_netdevice_lag_changeupper(dev, info);
1395 case NETDEV_CHANGELOWERSTATE: {
1396 struct netdev_notifier_changelowerstate_info *info = ptr;
1398 if (!ocelot_netdevice_dev_check(dev))
1401 return ocelot_netdevice_changelowerstate(dev,
1402 info->lower_state_info);
1411 struct notifier_block ocelot_netdevice_nb __read_mostly = {
1412 .notifier_call = ocelot_netdevice_event,
1415 static int ocelot_switchdev_event(struct notifier_block *unused,
1416 unsigned long event, void *ptr)
1418 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1422 case SWITCHDEV_PORT_ATTR_SET:
1423 err = switchdev_handle_port_attr_set(dev, ptr,
1424 ocelot_netdevice_dev_check,
1425 ocelot_port_attr_set);
1426 return notifier_from_errno(err);
1432 struct notifier_block ocelot_switchdev_nb __read_mostly = {
1433 .notifier_call = ocelot_switchdev_event,
1436 static int ocelot_switchdev_blocking_event(struct notifier_block *unused,
1437 unsigned long event, void *ptr)
1439 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1443 /* Blocking events. */
1444 case SWITCHDEV_PORT_OBJ_ADD:
1445 err = switchdev_handle_port_obj_add(dev, ptr,
1446 ocelot_netdevice_dev_check,
1447 ocelot_port_obj_add);
1448 return notifier_from_errno(err);
1449 case SWITCHDEV_PORT_OBJ_DEL:
1450 err = switchdev_handle_port_obj_del(dev, ptr,
1451 ocelot_netdevice_dev_check,
1452 ocelot_port_obj_del);
1453 return notifier_from_errno(err);
1454 case SWITCHDEV_PORT_ATTR_SET:
1455 err = switchdev_handle_port_attr_set(dev, ptr,
1456 ocelot_netdevice_dev_check,
1457 ocelot_port_attr_set);
1458 return notifier_from_errno(err);
1464 struct notifier_block ocelot_switchdev_blocking_nb __read_mostly = {
1465 .notifier_call = ocelot_switchdev_blocking_event,
1468 int ocelot_probe_port(struct ocelot *ocelot, int port, struct regmap *target,
1469 struct phy_device *phy)
1471 struct ocelot_port_private *priv;
1472 struct ocelot_port *ocelot_port;
1473 struct net_device *dev;
1476 dev = alloc_etherdev(sizeof(struct ocelot_port_private));
1479 SET_NETDEV_DEV(dev, ocelot->dev);
1480 priv = netdev_priv(dev);
1483 priv->chip_port = port;
1484 ocelot_port = &priv->port;
1485 ocelot_port->ocelot = ocelot;
1486 ocelot_port->target = target;
1487 ocelot->ports[port] = ocelot_port;
1489 dev->netdev_ops = &ocelot_port_netdev_ops;
1490 dev->ethtool_ops = &ocelot_ethtool_ops;
1492 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_RXFCS |
1494 dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_TC;
1496 memcpy(dev->dev_addr, ocelot->base_mac, ETH_ALEN);
1497 dev->dev_addr[ETH_ALEN - 1] += port;
1498 ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr,
1499 ocelot_port->pvid_vlan.vid, ENTRYTYPE_LOCKED);
1501 ocelot_init_port(ocelot, port);
1503 err = register_netdev(dev);
1505 dev_err(ocelot->dev, "register_netdev failed\n");
1507 ocelot->ports[port] = NULL;
1514 void ocelot_release_port(struct ocelot_port *ocelot_port)
1516 struct ocelot_port_private *priv = container_of(ocelot_port,
1517 struct ocelot_port_private,
1520 unregister_netdev(priv->dev);
1521 free_netdev(priv->dev);