1 // SPDX-License-Identifier: GPL-2.0
3 * DPAA2 Ethernet Switch driver
5 * Copyright 2014-2016 Freescale Semiconductor Inc.
6 * Copyright 2017-2018 NXP
10 #include <linux/module.h>
12 #include <linux/interrupt.h>
13 #include <linux/msi.h>
14 #include <linux/kthread.h>
15 #include <linux/workqueue.h>
17 #include <linux/fsl/mc.h>
21 static struct workqueue_struct *ethsw_owq;
23 /* Minimal supported DPSW version */
24 #define DPSW_MIN_VER_MAJOR 8
25 #define DPSW_MIN_VER_MINOR 0
27 #define DEFAULT_VLAN_ID 1
29 static int ethsw_add_vlan(struct ethsw_core *ethsw, u16 vid)
33 struct dpsw_vlan_cfg vcfg = {
37 if (ethsw->vlans[vid]) {
38 dev_err(ethsw->dev, "VLAN already configured\n");
42 err = dpsw_vlan_add(ethsw->mc_io, 0,
43 ethsw->dpsw_handle, vid, &vcfg);
45 dev_err(ethsw->dev, "dpsw_vlan_add err %d\n", err);
48 ethsw->vlans[vid] = ETHSW_VLAN_MEMBER;
53 static int ethsw_port_set_pvid(struct ethsw_port_priv *port_priv, u16 pvid)
55 struct ethsw_core *ethsw = port_priv->ethsw_data;
56 struct net_device *netdev = port_priv->netdev;
57 struct dpsw_tci_cfg tci_cfg = { 0 };
61 err = dpsw_if_get_tci(ethsw->mc_io, 0, ethsw->dpsw_handle,
62 port_priv->idx, &tci_cfg);
64 netdev_err(netdev, "dpsw_if_get_tci err %d\n", err);
68 tci_cfg.vlan_id = pvid;
70 /* Interface needs to be down to change PVID */
71 is_oper = netif_oper_up(netdev);
73 err = dpsw_if_disable(ethsw->mc_io, 0,
77 netdev_err(netdev, "dpsw_if_disable err %d\n", err);
82 err = dpsw_if_set_tci(ethsw->mc_io, 0, ethsw->dpsw_handle,
83 port_priv->idx, &tci_cfg);
85 netdev_err(netdev, "dpsw_if_set_tci err %d\n", err);
89 /* Delete previous PVID info and mark the new one */
90 port_priv->vlans[port_priv->pvid] &= ~ETHSW_VLAN_PVID;
91 port_priv->vlans[pvid] |= ETHSW_VLAN_PVID;
92 port_priv->pvid = pvid;
96 ret = dpsw_if_enable(ethsw->mc_io, 0,
100 netdev_err(netdev, "dpsw_if_enable err %d\n", ret);
108 static int ethsw_port_add_vlan(struct ethsw_port_priv *port_priv,
111 struct ethsw_core *ethsw = port_priv->ethsw_data;
112 struct net_device *netdev = port_priv->netdev;
113 struct dpsw_vlan_if_cfg vcfg;
116 if (port_priv->vlans[vid]) {
117 netdev_warn(netdev, "VLAN %d already configured\n", vid);
122 vcfg.if_id[0] = port_priv->idx;
123 err = dpsw_vlan_add_if(ethsw->mc_io, 0, ethsw->dpsw_handle, vid, &vcfg);
125 netdev_err(netdev, "dpsw_vlan_add_if err %d\n", err);
129 port_priv->vlans[vid] = ETHSW_VLAN_MEMBER;
131 if (flags & BRIDGE_VLAN_INFO_UNTAGGED) {
132 err = dpsw_vlan_add_if_untagged(ethsw->mc_io, 0,
137 "dpsw_vlan_add_if_untagged err %d\n", err);
140 port_priv->vlans[vid] |= ETHSW_VLAN_UNTAGGED;
143 if (flags & BRIDGE_VLAN_INFO_PVID) {
144 err = ethsw_port_set_pvid(port_priv, vid);
152 static int ethsw_set_learning(struct ethsw_core *ethsw, u8 flag)
154 enum dpsw_fdb_learning_mode learn_mode;
158 learn_mode = DPSW_FDB_LEARNING_MODE_HW;
160 learn_mode = DPSW_FDB_LEARNING_MODE_DIS;
162 err = dpsw_fdb_set_learning_mode(ethsw->mc_io, 0, ethsw->dpsw_handle, 0,
165 dev_err(ethsw->dev, "dpsw_fdb_set_learning_mode err %d\n", err);
168 ethsw->learning = !!flag;
173 static int ethsw_port_set_flood(struct ethsw_port_priv *port_priv, u8 flag)
177 err = dpsw_if_set_flooding(port_priv->ethsw_data->mc_io, 0,
178 port_priv->ethsw_data->dpsw_handle,
179 port_priv->idx, flag);
181 netdev_err(port_priv->netdev,
182 "dpsw_if_set_flooding err %d\n", err);
185 port_priv->flood = !!flag;
190 static int ethsw_port_set_stp_state(struct ethsw_port_priv *port_priv, u8 state)
192 struct dpsw_stp_cfg stp_cfg = {
193 .vlan_id = DEFAULT_VLAN_ID,
198 if (!netif_oper_up(port_priv->netdev) || state == port_priv->stp_state)
199 return 0; /* Nothing to do */
201 err = dpsw_if_set_stp(port_priv->ethsw_data->mc_io, 0,
202 port_priv->ethsw_data->dpsw_handle,
203 port_priv->idx, &stp_cfg);
205 netdev_err(port_priv->netdev,
206 "dpsw_if_set_stp err %d\n", err);
210 port_priv->stp_state = state;
215 static int ethsw_dellink_switch(struct ethsw_core *ethsw, u16 vid)
217 struct ethsw_port_priv *ppriv_local = NULL;
220 if (!ethsw->vlans[vid])
223 err = dpsw_vlan_remove(ethsw->mc_io, 0, ethsw->dpsw_handle, vid);
225 dev_err(ethsw->dev, "dpsw_vlan_remove err %d\n", err);
228 ethsw->vlans[vid] = 0;
230 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
231 ppriv_local = ethsw->ports[i];
232 ppriv_local->vlans[vid] = 0;
238 static int ethsw_port_fdb_add_uc(struct ethsw_port_priv *port_priv,
239 const unsigned char *addr)
241 struct dpsw_fdb_unicast_cfg entry = {0};
244 entry.if_egress = port_priv->idx;
245 entry.type = DPSW_FDB_ENTRY_STATIC;
246 ether_addr_copy(entry.mac_addr, addr);
248 err = dpsw_fdb_add_unicast(port_priv->ethsw_data->mc_io, 0,
249 port_priv->ethsw_data->dpsw_handle,
252 netdev_err(port_priv->netdev,
253 "dpsw_fdb_add_unicast err %d\n", err);
257 static int ethsw_port_fdb_del_uc(struct ethsw_port_priv *port_priv,
258 const unsigned char *addr)
260 struct dpsw_fdb_unicast_cfg entry = {0};
263 entry.if_egress = port_priv->idx;
264 entry.type = DPSW_FDB_ENTRY_STATIC;
265 ether_addr_copy(entry.mac_addr, addr);
267 err = dpsw_fdb_remove_unicast(port_priv->ethsw_data->mc_io, 0,
268 port_priv->ethsw_data->dpsw_handle,
270 /* Silently discard error for calling multiple times the del command */
271 if (err && err != -ENXIO)
272 netdev_err(port_priv->netdev,
273 "dpsw_fdb_remove_unicast err %d\n", err);
277 static int ethsw_port_fdb_add_mc(struct ethsw_port_priv *port_priv,
278 const unsigned char *addr)
280 struct dpsw_fdb_multicast_cfg entry = {0};
283 ether_addr_copy(entry.mac_addr, addr);
284 entry.type = DPSW_FDB_ENTRY_STATIC;
286 entry.if_id[0] = port_priv->idx;
288 err = dpsw_fdb_add_multicast(port_priv->ethsw_data->mc_io, 0,
289 port_priv->ethsw_data->dpsw_handle,
291 /* Silently discard error for calling multiple times the add command */
292 if (err && err != -ENXIO)
293 netdev_err(port_priv->netdev, "dpsw_fdb_add_multicast err %d\n",
298 static int ethsw_port_fdb_del_mc(struct ethsw_port_priv *port_priv,
299 const unsigned char *addr)
301 struct dpsw_fdb_multicast_cfg entry = {0};
304 ether_addr_copy(entry.mac_addr, addr);
305 entry.type = DPSW_FDB_ENTRY_STATIC;
307 entry.if_id[0] = port_priv->idx;
309 err = dpsw_fdb_remove_multicast(port_priv->ethsw_data->mc_io, 0,
310 port_priv->ethsw_data->dpsw_handle,
312 /* Silently discard error for calling multiple times the del command */
313 if (err && err != -ENAVAIL)
314 netdev_err(port_priv->netdev,
315 "dpsw_fdb_remove_multicast err %d\n", err);
319 static void port_get_stats(struct net_device *netdev,
320 struct rtnl_link_stats64 *stats)
322 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
326 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
327 port_priv->ethsw_data->dpsw_handle,
329 DPSW_CNT_ING_FRAME, &stats->rx_packets);
333 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
334 port_priv->ethsw_data->dpsw_handle,
336 DPSW_CNT_EGR_FRAME, &stats->tx_packets);
340 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
341 port_priv->ethsw_data->dpsw_handle,
343 DPSW_CNT_ING_BYTE, &stats->rx_bytes);
347 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
348 port_priv->ethsw_data->dpsw_handle,
350 DPSW_CNT_EGR_BYTE, &stats->tx_bytes);
354 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
355 port_priv->ethsw_data->dpsw_handle,
357 DPSW_CNT_ING_FRAME_DISCARD,
362 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
363 port_priv->ethsw_data->dpsw_handle,
365 DPSW_CNT_ING_FLTR_FRAME,
369 stats->rx_dropped += tmp;
371 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
372 port_priv->ethsw_data->dpsw_handle,
374 DPSW_CNT_EGR_FRAME_DISCARD,
382 netdev_err(netdev, "dpsw_if_get_counter err %d\n", err);
385 static bool port_has_offload_stats(const struct net_device *netdev,
388 return (attr_id == IFLA_OFFLOAD_XSTATS_CPU_HIT);
391 static int port_get_offload_stats(int attr_id,
392 const struct net_device *netdev,
396 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
397 port_get_stats((struct net_device *)netdev, sp);
404 static int port_change_mtu(struct net_device *netdev, int mtu)
406 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
409 err = dpsw_if_set_max_frame_length(port_priv->ethsw_data->mc_io,
411 port_priv->ethsw_data->dpsw_handle,
413 (u16)ETHSW_L2_MAX_FRM(mtu));
416 "dpsw_if_set_max_frame_length() err %d\n", err);
424 static int port_carrier_state_sync(struct net_device *netdev)
426 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
427 struct dpsw_link_state state;
430 err = dpsw_if_get_link_state(port_priv->ethsw_data->mc_io, 0,
431 port_priv->ethsw_data->dpsw_handle,
432 port_priv->idx, &state);
434 netdev_err(netdev, "dpsw_if_get_link_state() err %d\n", err);
438 WARN_ONCE(state.up > 1, "Garbage read into link_state");
440 if (state.up != port_priv->link_state) {
442 netif_carrier_on(netdev);
444 netif_carrier_off(netdev);
445 port_priv->link_state = state.up;
450 static int port_open(struct net_device *netdev)
452 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
455 /* No need to allow Tx as control interface is disabled */
456 netif_tx_stop_all_queues(netdev);
458 err = dpsw_if_enable(port_priv->ethsw_data->mc_io, 0,
459 port_priv->ethsw_data->dpsw_handle,
462 netdev_err(netdev, "dpsw_if_enable err %d\n", err);
466 /* sync carrier state */
467 err = port_carrier_state_sync(netdev);
470 "port_carrier_state_sync err %d\n", err);
471 goto err_carrier_sync;
477 dpsw_if_disable(port_priv->ethsw_data->mc_io, 0,
478 port_priv->ethsw_data->dpsw_handle,
483 static int port_stop(struct net_device *netdev)
485 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
488 err = dpsw_if_disable(port_priv->ethsw_data->mc_io, 0,
489 port_priv->ethsw_data->dpsw_handle,
492 netdev_err(netdev, "dpsw_if_disable err %d\n", err);
499 static netdev_tx_t port_dropframe(struct sk_buff *skb,
500 struct net_device *netdev)
502 /* we don't support I/O for now, drop the frame */
503 dev_kfree_skb_any(skb);
508 static const struct net_device_ops ethsw_port_ops = {
509 .ndo_open = port_open,
510 .ndo_stop = port_stop,
512 .ndo_set_mac_address = eth_mac_addr,
513 .ndo_change_mtu = port_change_mtu,
514 .ndo_has_offload_stats = port_has_offload_stats,
515 .ndo_get_offload_stats = port_get_offload_stats,
517 .ndo_start_xmit = port_dropframe,
520 static void ethsw_links_state_update(struct ethsw_core *ethsw)
524 for (i = 0; i < ethsw->sw_attr.num_ifs; i++)
525 port_carrier_state_sync(ethsw->ports[i]->netdev);
528 static irqreturn_t ethsw_irq0_handler_thread(int irq_num, void *arg)
530 struct device *dev = (struct device *)arg;
531 struct ethsw_core *ethsw = dev_get_drvdata(dev);
533 /* Mask the events and the if_id reserved bits to be cleared on read */
534 u32 status = DPSW_IRQ_EVENT_LINK_CHANGED | 0xFFFF0000;
537 err = dpsw_get_irq_status(ethsw->mc_io, 0, ethsw->dpsw_handle,
538 DPSW_IRQ_INDEX_IF, &status);
540 dev_err(dev, "Can't get irq status (err %d)", err);
542 err = dpsw_clear_irq_status(ethsw->mc_io, 0, ethsw->dpsw_handle,
543 DPSW_IRQ_INDEX_IF, 0xFFFFFFFF);
545 dev_err(dev, "Can't clear irq status (err %d)", err);
549 if (status & DPSW_IRQ_EVENT_LINK_CHANGED)
550 ethsw_links_state_update(ethsw);
556 static int ethsw_setup_irqs(struct fsl_mc_device *sw_dev)
558 struct device *dev = &sw_dev->dev;
559 struct ethsw_core *ethsw = dev_get_drvdata(dev);
560 u32 mask = DPSW_IRQ_EVENT_LINK_CHANGED;
561 struct fsl_mc_device_irq *irq;
564 err = fsl_mc_allocate_irqs(sw_dev);
566 dev_err(dev, "MC irqs allocation failed\n");
570 if (WARN_ON(sw_dev->obj_desc.irq_count != DPSW_IRQ_NUM)) {
575 err = dpsw_set_irq_enable(ethsw->mc_io, 0, ethsw->dpsw_handle,
576 DPSW_IRQ_INDEX_IF, 0);
578 dev_err(dev, "dpsw_set_irq_enable err %d\n", err);
582 irq = sw_dev->irqs[DPSW_IRQ_INDEX_IF];
584 err = devm_request_threaded_irq(dev, irq->msi_desc->irq,
586 ethsw_irq0_handler_thread,
587 IRQF_NO_SUSPEND | IRQF_ONESHOT,
590 dev_err(dev, "devm_request_threaded_irq(): %d", err);
594 err = dpsw_set_irq_mask(ethsw->mc_io, 0, ethsw->dpsw_handle,
595 DPSW_IRQ_INDEX_IF, mask);
597 dev_err(dev, "dpsw_set_irq_mask(): %d", err);
601 err = dpsw_set_irq_enable(ethsw->mc_io, 0, ethsw->dpsw_handle,
602 DPSW_IRQ_INDEX_IF, 1);
604 dev_err(dev, "dpsw_set_irq_enable(): %d", err);
611 devm_free_irq(dev, irq->msi_desc->irq, dev);
613 fsl_mc_free_irqs(sw_dev);
617 static void ethsw_teardown_irqs(struct fsl_mc_device *sw_dev)
619 struct device *dev = &sw_dev->dev;
620 struct ethsw_core *ethsw = dev_get_drvdata(dev);
623 err = dpsw_set_irq_enable(ethsw->mc_io, 0, ethsw->dpsw_handle,
624 DPSW_IRQ_INDEX_IF, 0);
626 dev_err(dev, "dpsw_set_irq_enable err %d\n", err);
628 fsl_mc_free_irqs(sw_dev);
631 static int swdev_port_attr_get(struct net_device *netdev,
632 struct switchdev_attr *attr)
634 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
637 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
638 attr->u.ppid.id_len = 1;
639 attr->u.ppid.id[0] = port_priv->ethsw_data->dev_id;
641 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
642 attr->u.brport_flags =
643 (port_priv->ethsw_data->learning ? BR_LEARNING : 0) |
644 (port_priv->flood ? BR_FLOOD : 0);
646 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS_SUPPORT:
647 attr->u.brport_flags_support = BR_LEARNING | BR_FLOOD;
656 static int port_attr_stp_state_set(struct net_device *netdev,
657 struct switchdev_trans *trans,
660 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
662 if (switchdev_trans_ph_prepare(trans))
665 return ethsw_port_set_stp_state(port_priv, state);
668 static int port_attr_br_flags_set(struct net_device *netdev,
669 struct switchdev_trans *trans,
672 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
675 if (switchdev_trans_ph_prepare(trans))
678 /* Learning is enabled per switch */
679 err = ethsw_set_learning(port_priv->ethsw_data, flags & BR_LEARNING);
683 err = ethsw_port_set_flood(port_priv, flags & BR_FLOOD);
689 static int swdev_port_attr_set(struct net_device *netdev,
690 const struct switchdev_attr *attr,
691 struct switchdev_trans *trans)
696 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
697 err = port_attr_stp_state_set(netdev, trans,
700 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
701 err = port_attr_br_flags_set(netdev, trans,
702 attr->u.brport_flags);
704 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
705 /* VLANs are supported by default */
715 static int port_vlans_add(struct net_device *netdev,
716 const struct switchdev_obj_port_vlan *vlan,
717 struct switchdev_trans *trans)
719 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
722 if (netif_is_bridge_master(vlan->obj.orig_dev))
725 if (switchdev_trans_ph_prepare(trans))
728 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
729 if (!port_priv->ethsw_data->vlans[vid]) {
730 /* this is a new VLAN */
731 err = ethsw_add_vlan(port_priv->ethsw_data, vid);
735 port_priv->ethsw_data->vlans[vid] |= ETHSW_VLAN_GLOBAL;
737 err = ethsw_port_add_vlan(port_priv, vid, vlan->flags);
745 static int port_lookup_address(struct net_device *netdev, int is_uc,
746 const unsigned char *addr)
748 struct netdev_hw_addr_list *list = (is_uc) ? &netdev->uc : &netdev->mc;
749 struct netdev_hw_addr *ha;
751 netif_addr_lock_bh(netdev);
752 list_for_each_entry(ha, &list->list, list) {
753 if (ether_addr_equal(ha->addr, addr)) {
754 netif_addr_unlock_bh(netdev);
758 netif_addr_unlock_bh(netdev);
762 static int port_mdb_add(struct net_device *netdev,
763 const struct switchdev_obj_port_mdb *mdb,
764 struct switchdev_trans *trans)
766 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
769 if (switchdev_trans_ph_prepare(trans))
772 /* Check if address is already set on this port */
773 if (port_lookup_address(netdev, 0, mdb->addr))
776 err = ethsw_port_fdb_add_mc(port_priv, mdb->addr);
780 err = dev_mc_add(netdev, mdb->addr);
782 netdev_err(netdev, "dev_mc_add err %d\n", err);
783 ethsw_port_fdb_del_mc(port_priv, mdb->addr);
789 static int swdev_port_obj_add(struct net_device *netdev,
790 const struct switchdev_obj *obj,
791 struct switchdev_trans *trans)
796 case SWITCHDEV_OBJ_ID_PORT_VLAN:
797 err = port_vlans_add(netdev,
798 SWITCHDEV_OBJ_PORT_VLAN(obj),
801 case SWITCHDEV_OBJ_ID_PORT_MDB:
802 err = port_mdb_add(netdev,
803 SWITCHDEV_OBJ_PORT_MDB(obj),
814 static int ethsw_port_del_vlan(struct ethsw_port_priv *port_priv, u16 vid)
816 struct ethsw_core *ethsw = port_priv->ethsw_data;
817 struct net_device *netdev = port_priv->netdev;
818 struct dpsw_vlan_if_cfg vcfg;
821 if (!port_priv->vlans[vid])
824 if (port_priv->vlans[vid] & ETHSW_VLAN_PVID) {
825 err = ethsw_port_set_pvid(port_priv, 0);
831 vcfg.if_id[0] = port_priv->idx;
832 if (port_priv->vlans[vid] & ETHSW_VLAN_UNTAGGED) {
833 err = dpsw_vlan_remove_if_untagged(ethsw->mc_io, 0,
838 "dpsw_vlan_remove_if_untagged err %d\n",
841 port_priv->vlans[vid] &= ~ETHSW_VLAN_UNTAGGED;
844 if (port_priv->vlans[vid] & ETHSW_VLAN_MEMBER) {
845 err = dpsw_vlan_remove_if(ethsw->mc_io, 0, ethsw->dpsw_handle,
849 "dpsw_vlan_remove_if err %d\n", err);
852 port_priv->vlans[vid] &= ~ETHSW_VLAN_MEMBER;
854 /* Delete VLAN from switch if it is no longer configured on
857 for (i = 0; i < ethsw->sw_attr.num_ifs; i++)
858 if (ethsw->ports[i]->vlans[vid] & ETHSW_VLAN_MEMBER)
859 return 0; /* Found a port member in VID */
861 ethsw->vlans[vid] &= ~ETHSW_VLAN_GLOBAL;
863 err = ethsw_dellink_switch(ethsw, vid);
871 static int port_vlans_del(struct net_device *netdev,
872 const struct switchdev_obj_port_vlan *vlan)
874 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
877 if (netif_is_bridge_master(vlan->obj.orig_dev))
880 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
881 err = ethsw_port_del_vlan(port_priv, vid);
889 static int port_mdb_del(struct net_device *netdev,
890 const struct switchdev_obj_port_mdb *mdb)
892 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
895 if (!port_lookup_address(netdev, 0, mdb->addr))
898 err = ethsw_port_fdb_del_mc(port_priv, mdb->addr);
902 err = dev_mc_del(netdev, mdb->addr);
904 netdev_err(netdev, "dev_mc_del err %d\n", err);
911 static int swdev_port_obj_del(struct net_device *netdev,
912 const struct switchdev_obj *obj)
917 case SWITCHDEV_OBJ_ID_PORT_VLAN:
918 err = port_vlans_del(netdev, SWITCHDEV_OBJ_PORT_VLAN(obj));
920 case SWITCHDEV_OBJ_ID_PORT_MDB:
921 err = port_mdb_del(netdev, SWITCHDEV_OBJ_PORT_MDB(obj));
930 static const struct switchdev_ops ethsw_port_switchdev_ops = {
931 .switchdev_port_attr_get = swdev_port_attr_get,
932 .switchdev_port_attr_set = swdev_port_attr_set,
933 .switchdev_port_obj_add = swdev_port_obj_add,
934 .switchdev_port_obj_del = swdev_port_obj_del,
937 /* For the moment, only flood setting needs to be updated */
938 static int port_bridge_join(struct net_device *netdev,
939 struct net_device *upper_dev)
941 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
942 struct ethsw_core *ethsw = port_priv->ethsw_data;
945 for (i = 0; i < ethsw->sw_attr.num_ifs; i++)
946 if (ethsw->ports[i]->bridge_dev &&
947 (ethsw->ports[i]->bridge_dev != upper_dev)) {
949 "Another switch port is connected to %s\n",
950 ethsw->ports[i]->bridge_dev->name);
954 /* Enable flooding */
955 err = ethsw_port_set_flood(port_priv, 1);
957 port_priv->bridge_dev = upper_dev;
962 static int port_bridge_leave(struct net_device *netdev)
964 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
967 /* Disable flooding */
968 err = ethsw_port_set_flood(port_priv, 0);
970 port_priv->bridge_dev = NULL;
975 static int port_netdevice_event(struct notifier_block *unused,
976 unsigned long event, void *ptr)
978 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
979 struct netdev_notifier_changeupper_info *info = ptr;
980 struct net_device *upper_dev;
983 if (netdev->netdev_ops != ðsw_port_ops)
986 /* Handle just upper dev link/unlink for the moment */
987 if (event == NETDEV_CHANGEUPPER) {
988 upper_dev = info->upper_dev;
989 if (netif_is_bridge_master(upper_dev)) {
991 err = port_bridge_join(netdev, upper_dev);
993 err = port_bridge_leave(netdev);
997 return notifier_from_errno(err);
1000 static struct notifier_block port_nb __read_mostly = {
1001 .notifier_call = port_netdevice_event,
1004 struct ethsw_switchdev_event_work {
1005 struct work_struct work;
1006 struct switchdev_notifier_fdb_info fdb_info;
1007 struct net_device *dev;
1008 unsigned long event;
1011 static void ethsw_switchdev_event_work(struct work_struct *work)
1013 struct ethsw_switchdev_event_work *switchdev_work =
1014 container_of(work, struct ethsw_switchdev_event_work, work);
1015 struct net_device *dev = switchdev_work->dev;
1016 struct switchdev_notifier_fdb_info *fdb_info;
1017 struct ethsw_port_priv *port_priv;
1020 port_priv = netdev_priv(dev);
1021 fdb_info = &switchdev_work->fdb_info;
1023 switch (switchdev_work->event) {
1024 case SWITCHDEV_FDB_ADD_TO_DEVICE:
1025 if (is_unicast_ether_addr(fdb_info->addr))
1026 ethsw_port_fdb_add_uc(netdev_priv(dev), fdb_info->addr);
1028 ethsw_port_fdb_add_mc(netdev_priv(dev), fdb_info->addr);
1030 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1031 if (is_unicast_ether_addr(fdb_info->addr))
1032 ethsw_port_fdb_del_uc(netdev_priv(dev), fdb_info->addr);
1034 ethsw_port_fdb_del_mc(netdev_priv(dev), fdb_info->addr);
1039 kfree(switchdev_work->fdb_info.addr);
1040 kfree(switchdev_work);
1044 /* Called under rcu_read_lock() */
1045 static int port_switchdev_event(struct notifier_block *unused,
1046 unsigned long event, void *ptr)
1048 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1049 struct ethsw_switchdev_event_work *switchdev_work;
1050 struct switchdev_notifier_fdb_info *fdb_info = ptr;
1052 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
1053 if (!switchdev_work)
1056 INIT_WORK(&switchdev_work->work, ethsw_switchdev_event_work);
1057 switchdev_work->dev = dev;
1058 switchdev_work->event = event;
1061 case SWITCHDEV_FDB_ADD_TO_DEVICE:
1062 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1063 memcpy(&switchdev_work->fdb_info, ptr,
1064 sizeof(switchdev_work->fdb_info));
1065 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
1066 if (!switchdev_work->fdb_info.addr)
1067 goto err_addr_alloc;
1069 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
1072 /* Take a reference on the device to avoid being freed. */
1076 kfree(switchdev_work);
1080 queue_work(ethsw_owq, &switchdev_work->work);
1085 kfree(switchdev_work);
1089 static struct notifier_block port_switchdev_nb = {
1090 .notifier_call = port_switchdev_event,
1093 static int ethsw_register_notifier(struct device *dev)
1097 err = register_netdevice_notifier(&port_nb);
1099 dev_err(dev, "Failed to register netdev notifier\n");
1103 err = register_switchdev_notifier(&port_switchdev_nb);
1105 dev_err(dev, "Failed to register switchdev notifier\n");
1106 goto err_switchdev_nb;
1112 unregister_netdevice_notifier(&port_nb);
1116 static int ethsw_open(struct ethsw_core *ethsw)
1118 struct ethsw_port_priv *port_priv = NULL;
1121 err = dpsw_enable(ethsw->mc_io, 0, ethsw->dpsw_handle);
1123 dev_err(ethsw->dev, "dpsw_enable err %d\n", err);
1127 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
1128 port_priv = ethsw->ports[i];
1129 err = dev_open(port_priv->netdev);
1131 netdev_err(port_priv->netdev, "dev_open err %d\n", err);
1139 static int ethsw_stop(struct ethsw_core *ethsw)
1141 struct ethsw_port_priv *port_priv = NULL;
1144 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
1145 port_priv = ethsw->ports[i];
1146 dev_close(port_priv->netdev);
1149 err = dpsw_disable(ethsw->mc_io, 0, ethsw->dpsw_handle);
1151 dev_err(ethsw->dev, "dpsw_disable err %d\n", err);
1158 static int ethsw_init(struct fsl_mc_device *sw_dev)
1160 struct device *dev = &sw_dev->dev;
1161 struct ethsw_core *ethsw = dev_get_drvdata(dev);
1162 u16 version_major, version_minor, i;
1163 struct dpsw_stp_cfg stp_cfg;
1166 ethsw->dev_id = sw_dev->obj_desc.id;
1168 err = dpsw_open(ethsw->mc_io, 0, ethsw->dev_id, ðsw->dpsw_handle);
1170 dev_err(dev, "dpsw_open err %d\n", err);
1174 err = dpsw_get_attributes(ethsw->mc_io, 0, ethsw->dpsw_handle,
1177 dev_err(dev, "dpsw_get_attributes err %d\n", err);
1181 err = dpsw_get_api_version(ethsw->mc_io, 0,
1185 dev_err(dev, "dpsw_get_api_version err %d\n", err);
1189 /* Minimum supported DPSW version check */
1190 if (version_major < DPSW_MIN_VER_MAJOR ||
1191 (version_major == DPSW_MIN_VER_MAJOR &&
1192 version_minor < DPSW_MIN_VER_MINOR)) {
1193 dev_err(dev, "DPSW version %d:%d not supported. Use %d.%d or greater.\n",
1196 DPSW_MIN_VER_MAJOR, DPSW_MIN_VER_MINOR);
1201 err = dpsw_reset(ethsw->mc_io, 0, ethsw->dpsw_handle);
1203 dev_err(dev, "dpsw_reset err %d\n", err);
1207 err = dpsw_fdb_set_learning_mode(ethsw->mc_io, 0, ethsw->dpsw_handle, 0,
1208 DPSW_FDB_LEARNING_MODE_HW);
1210 dev_err(dev, "dpsw_fdb_set_learning_mode err %d\n", err);
1214 stp_cfg.vlan_id = DEFAULT_VLAN_ID;
1215 stp_cfg.state = DPSW_STP_STATE_FORWARDING;
1217 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
1218 err = dpsw_if_set_stp(ethsw->mc_io, 0, ethsw->dpsw_handle, i,
1221 dev_err(dev, "dpsw_if_set_stp err %d for port %d\n",
1226 err = dpsw_if_set_broadcast(ethsw->mc_io, 0,
1227 ethsw->dpsw_handle, i, 1);
1230 "dpsw_if_set_broadcast err %d for port %d\n",
1236 ethsw_owq = alloc_ordered_workqueue("%s_ordered", WQ_MEM_RECLAIM,
1243 err = ethsw_register_notifier(dev);
1245 goto err_destroy_ordered_workqueue;
1249 err_destroy_ordered_workqueue:
1250 destroy_workqueue(ethsw_owq);
1253 dpsw_close(ethsw->mc_io, 0, ethsw->dpsw_handle);
1257 static int ethsw_port_init(struct ethsw_port_priv *port_priv, u16 port)
1259 const char def_mcast[ETH_ALEN] = {0x01, 0x00, 0x5e, 0x00, 0x00, 0x01};
1260 struct net_device *netdev = port_priv->netdev;
1261 struct ethsw_core *ethsw = port_priv->ethsw_data;
1262 struct dpsw_vlan_if_cfg vcfg;
1265 /* Switch starts with all ports configured to VLAN 1. Need to
1266 * remove this setting to allow configuration at bridge join
1269 vcfg.if_id[0] = port_priv->idx;
1271 err = dpsw_vlan_remove_if_untagged(ethsw->mc_io, 0, ethsw->dpsw_handle,
1272 DEFAULT_VLAN_ID, &vcfg);
1274 netdev_err(netdev, "dpsw_vlan_remove_if_untagged err %d\n",
1279 err = ethsw_port_set_pvid(port_priv, 0);
1283 err = dpsw_vlan_remove_if(ethsw->mc_io, 0, ethsw->dpsw_handle,
1284 DEFAULT_VLAN_ID, &vcfg);
1286 netdev_err(netdev, "dpsw_vlan_remove_if err %d\n", err);
1290 err = ethsw_port_fdb_add_mc(port_priv, def_mcast);
1295 static void ethsw_unregister_notifier(struct device *dev)
1299 err = unregister_switchdev_notifier(&port_switchdev_nb);
1302 "Failed to unregister switchdev notifier (%d)\n", err);
1304 err = unregister_netdevice_notifier(&port_nb);
1307 "Failed to unregister netdev notifier (%d)\n", err);
1310 static void ethsw_takedown(struct fsl_mc_device *sw_dev)
1312 struct device *dev = &sw_dev->dev;
1313 struct ethsw_core *ethsw = dev_get_drvdata(dev);
1316 ethsw_unregister_notifier(dev);
1318 err = dpsw_close(ethsw->mc_io, 0, ethsw->dpsw_handle);
1320 dev_warn(dev, "dpsw_close err %d\n", err);
1323 static int ethsw_remove(struct fsl_mc_device *sw_dev)
1325 struct ethsw_port_priv *port_priv;
1326 struct ethsw_core *ethsw;
1331 ethsw = dev_get_drvdata(dev);
1333 ethsw_teardown_irqs(sw_dev);
1335 destroy_workqueue(ethsw_owq);
1341 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
1342 port_priv = ethsw->ports[i];
1343 unregister_netdev(port_priv->netdev);
1344 free_netdev(port_priv->netdev);
1346 kfree(ethsw->ports);
1348 ethsw_takedown(sw_dev);
1349 fsl_mc_portal_free(ethsw->mc_io);
1353 dev_set_drvdata(dev, NULL);
1358 static int ethsw_probe_port(struct ethsw_core *ethsw, u16 port_idx)
1360 struct ethsw_port_priv *port_priv;
1361 struct device *dev = ethsw->dev;
1362 struct net_device *port_netdev;
1365 port_netdev = alloc_etherdev(sizeof(struct ethsw_port_priv));
1367 dev_err(dev, "alloc_etherdev error\n");
1371 port_priv = netdev_priv(port_netdev);
1372 port_priv->netdev = port_netdev;
1373 port_priv->ethsw_data = ethsw;
1375 port_priv->idx = port_idx;
1376 port_priv->stp_state = BR_STATE_FORWARDING;
1378 /* Flooding is implicitly enabled */
1379 port_priv->flood = true;
1381 SET_NETDEV_DEV(port_netdev, dev);
1382 port_netdev->netdev_ops = ðsw_port_ops;
1383 port_netdev->ethtool_ops = ðsw_port_ethtool_ops;
1384 port_netdev->switchdev_ops = ðsw_port_switchdev_ops;
1386 /* Set MTU limits */
1387 port_netdev->min_mtu = ETH_MIN_MTU;
1388 port_netdev->max_mtu = ETHSW_MAX_FRAME_LENGTH;
1390 err = register_netdev(port_netdev);
1392 dev_err(dev, "register_netdev error %d\n", err);
1393 free_netdev(port_netdev);
1397 ethsw->ports[port_idx] = port_priv;
1399 return ethsw_port_init(port_priv, port_idx);
1402 static int ethsw_probe(struct fsl_mc_device *sw_dev)
1404 struct device *dev = &sw_dev->dev;
1405 struct ethsw_core *ethsw;
1408 /* Allocate switch core*/
1409 ethsw = kzalloc(sizeof(*ethsw), GFP_KERNEL);
1415 dev_set_drvdata(dev, ethsw);
1417 err = fsl_mc_portal_allocate(sw_dev, 0, ðsw->mc_io);
1420 err = -EPROBE_DEFER;
1422 dev_err(dev, "fsl_mc_portal_allocate err %d\n", err);
1423 goto err_free_drvdata;
1426 err = ethsw_init(sw_dev);
1428 goto err_free_cmdport;
1430 /* DEFAULT_VLAN_ID is implicitly configured on the switch */
1431 ethsw->vlans[DEFAULT_VLAN_ID] = ETHSW_VLAN_MEMBER;
1433 /* Learning is implicitly enabled */
1434 ethsw->learning = true;
1436 ethsw->ports = kcalloc(ethsw->sw_attr.num_ifs, sizeof(*ethsw->ports),
1438 if (!(ethsw->ports)) {
1443 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
1444 err = ethsw_probe_port(ethsw, i);
1446 goto err_free_ports;
1449 /* Switch starts up enabled */
1451 err = ethsw_open(ethsw);
1454 goto err_free_ports;
1457 err = ethsw_setup_irqs(sw_dev);
1461 dev_info(dev, "probed %d port switch\n", ethsw->sw_attr.num_ifs);
1470 /* Cleanup registered ports only */
1471 for (i--; i >= 0; i--) {
1472 unregister_netdev(ethsw->ports[i]->netdev);
1473 free_netdev(ethsw->ports[i]->netdev);
1475 kfree(ethsw->ports);
1478 ethsw_takedown(sw_dev);
1481 fsl_mc_portal_free(ethsw->mc_io);
1485 dev_set_drvdata(dev, NULL);
1490 static const struct fsl_mc_device_id ethsw_match_id_table[] = {
1492 .vendor = FSL_MC_VENDOR_FREESCALE,
1497 MODULE_DEVICE_TABLE(fslmc, ethsw_match_id_table);
1499 static struct fsl_mc_driver eth_sw_drv = {
1501 .name = KBUILD_MODNAME,
1502 .owner = THIS_MODULE,
1504 .probe = ethsw_probe,
1505 .remove = ethsw_remove,
1506 .match_id_table = ethsw_match_id_table
1509 module_fsl_mc_driver(eth_sw_drv);
1511 MODULE_LICENSE("GPL v2");
1512 MODULE_DESCRIPTION("DPAA2 Ethernet Switch Driver");