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 /* Minimal supported DPSW version */
22 #define DPSW_MIN_VER_MAJOR 8
23 #define DPSW_MIN_VER_MINOR 1
25 #define DEFAULT_VLAN_ID 1
27 static int ethsw_add_vlan(struct ethsw_core *ethsw, u16 vid)
31 struct dpsw_vlan_cfg vcfg = {
35 err = dpsw_vlan_add(ethsw->mc_io, 0,
36 ethsw->dpsw_handle, vid, &vcfg);
38 dev_err(ethsw->dev, "dpsw_vlan_add err %d\n", err);
41 ethsw->vlans[vid] = ETHSW_VLAN_MEMBER;
46 static int ethsw_port_set_pvid(struct ethsw_port_priv *port_priv, u16 pvid)
48 struct ethsw_core *ethsw = port_priv->ethsw_data;
49 struct net_device *netdev = port_priv->netdev;
50 struct dpsw_tci_cfg tci_cfg = { 0 };
54 err = dpsw_if_get_tci(ethsw->mc_io, 0, ethsw->dpsw_handle,
55 port_priv->idx, &tci_cfg);
57 netdev_err(netdev, "dpsw_if_get_tci err %d\n", err);
61 tci_cfg.vlan_id = pvid;
63 /* Interface needs to be down to change PVID */
64 is_oper = netif_oper_up(netdev);
66 err = dpsw_if_disable(ethsw->mc_io, 0,
70 netdev_err(netdev, "dpsw_if_disable err %d\n", err);
75 err = dpsw_if_set_tci(ethsw->mc_io, 0, ethsw->dpsw_handle,
76 port_priv->idx, &tci_cfg);
78 netdev_err(netdev, "dpsw_if_set_tci err %d\n", err);
82 /* Delete previous PVID info and mark the new one */
83 port_priv->vlans[port_priv->pvid] &= ~ETHSW_VLAN_PVID;
84 port_priv->vlans[pvid] |= ETHSW_VLAN_PVID;
85 port_priv->pvid = pvid;
89 ret = dpsw_if_enable(ethsw->mc_io, 0,
93 netdev_err(netdev, "dpsw_if_enable err %d\n", ret);
101 static int ethsw_port_add_vlan(struct ethsw_port_priv *port_priv,
104 struct ethsw_core *ethsw = port_priv->ethsw_data;
105 struct net_device *netdev = port_priv->netdev;
106 struct dpsw_vlan_if_cfg vcfg;
109 if (port_priv->vlans[vid]) {
110 netdev_warn(netdev, "VLAN %d already configured\n", vid);
115 vcfg.if_id[0] = port_priv->idx;
116 err = dpsw_vlan_add_if(ethsw->mc_io, 0, ethsw->dpsw_handle, vid, &vcfg);
118 netdev_err(netdev, "dpsw_vlan_add_if err %d\n", err);
122 port_priv->vlans[vid] = ETHSW_VLAN_MEMBER;
124 if (flags & BRIDGE_VLAN_INFO_UNTAGGED) {
125 err = dpsw_vlan_add_if_untagged(ethsw->mc_io, 0,
130 "dpsw_vlan_add_if_untagged err %d\n", err);
133 port_priv->vlans[vid] |= ETHSW_VLAN_UNTAGGED;
136 if (flags & BRIDGE_VLAN_INFO_PVID) {
137 err = ethsw_port_set_pvid(port_priv, vid);
145 static int ethsw_set_learning(struct ethsw_core *ethsw, bool enable)
147 enum dpsw_fdb_learning_mode learn_mode;
151 learn_mode = DPSW_FDB_LEARNING_MODE_HW;
153 learn_mode = DPSW_FDB_LEARNING_MODE_DIS;
155 err = dpsw_fdb_set_learning_mode(ethsw->mc_io, 0, ethsw->dpsw_handle, 0,
158 dev_err(ethsw->dev, "dpsw_fdb_set_learning_mode err %d\n", err);
161 ethsw->learning = enable;
166 static int ethsw_port_set_flood(struct ethsw_port_priv *port_priv, bool enable)
170 err = dpsw_if_set_flooding(port_priv->ethsw_data->mc_io, 0,
171 port_priv->ethsw_data->dpsw_handle,
172 port_priv->idx, enable);
174 netdev_err(port_priv->netdev,
175 "dpsw_if_set_flooding err %d\n", err);
178 port_priv->flood = enable;
183 static int ethsw_port_set_stp_state(struct ethsw_port_priv *port_priv, u8 state)
185 struct dpsw_stp_cfg stp_cfg = {
186 .vlan_id = DEFAULT_VLAN_ID,
191 if (!netif_oper_up(port_priv->netdev) || state == port_priv->stp_state)
192 return 0; /* Nothing to do */
194 err = dpsw_if_set_stp(port_priv->ethsw_data->mc_io, 0,
195 port_priv->ethsw_data->dpsw_handle,
196 port_priv->idx, &stp_cfg);
198 netdev_err(port_priv->netdev,
199 "dpsw_if_set_stp err %d\n", err);
203 port_priv->stp_state = state;
208 static int ethsw_dellink_switch(struct ethsw_core *ethsw, u16 vid)
210 struct ethsw_port_priv *ppriv_local = NULL;
213 if (!ethsw->vlans[vid])
216 err = dpsw_vlan_remove(ethsw->mc_io, 0, ethsw->dpsw_handle, vid);
218 dev_err(ethsw->dev, "dpsw_vlan_remove err %d\n", err);
221 ethsw->vlans[vid] = 0;
223 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
224 ppriv_local = ethsw->ports[i];
225 ppriv_local->vlans[vid] = 0;
231 static int ethsw_port_fdb_add_uc(struct ethsw_port_priv *port_priv,
232 const unsigned char *addr)
234 struct dpsw_fdb_unicast_cfg entry = {0};
237 entry.if_egress = port_priv->idx;
238 entry.type = DPSW_FDB_ENTRY_STATIC;
239 ether_addr_copy(entry.mac_addr, addr);
241 err = dpsw_fdb_add_unicast(port_priv->ethsw_data->mc_io, 0,
242 port_priv->ethsw_data->dpsw_handle,
245 netdev_err(port_priv->netdev,
246 "dpsw_fdb_add_unicast err %d\n", err);
250 static int ethsw_port_fdb_del_uc(struct ethsw_port_priv *port_priv,
251 const unsigned char *addr)
253 struct dpsw_fdb_unicast_cfg entry = {0};
256 entry.if_egress = port_priv->idx;
257 entry.type = DPSW_FDB_ENTRY_STATIC;
258 ether_addr_copy(entry.mac_addr, addr);
260 err = dpsw_fdb_remove_unicast(port_priv->ethsw_data->mc_io, 0,
261 port_priv->ethsw_data->dpsw_handle,
263 /* Silently discard error for calling multiple times the del command */
264 if (err && err != -ENXIO)
265 netdev_err(port_priv->netdev,
266 "dpsw_fdb_remove_unicast err %d\n", err);
270 static int ethsw_port_fdb_add_mc(struct ethsw_port_priv *port_priv,
271 const unsigned char *addr)
273 struct dpsw_fdb_multicast_cfg entry = {0};
276 ether_addr_copy(entry.mac_addr, addr);
277 entry.type = DPSW_FDB_ENTRY_STATIC;
279 entry.if_id[0] = port_priv->idx;
281 err = dpsw_fdb_add_multicast(port_priv->ethsw_data->mc_io, 0,
282 port_priv->ethsw_data->dpsw_handle,
284 /* Silently discard error for calling multiple times the add command */
285 if (err && err != -ENXIO)
286 netdev_err(port_priv->netdev, "dpsw_fdb_add_multicast err %d\n",
291 static int ethsw_port_fdb_del_mc(struct ethsw_port_priv *port_priv,
292 const unsigned char *addr)
294 struct dpsw_fdb_multicast_cfg entry = {0};
297 ether_addr_copy(entry.mac_addr, addr);
298 entry.type = DPSW_FDB_ENTRY_STATIC;
300 entry.if_id[0] = port_priv->idx;
302 err = dpsw_fdb_remove_multicast(port_priv->ethsw_data->mc_io, 0,
303 port_priv->ethsw_data->dpsw_handle,
305 /* Silently discard error for calling multiple times the del command */
306 if (err && err != -ENAVAIL)
307 netdev_err(port_priv->netdev,
308 "dpsw_fdb_remove_multicast err %d\n", err);
312 static int port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
313 struct net_device *dev, const unsigned char *addr,
315 struct netlink_ext_ack *extack)
317 if (is_unicast_ether_addr(addr))
318 return ethsw_port_fdb_add_uc(netdev_priv(dev),
321 return ethsw_port_fdb_add_mc(netdev_priv(dev),
325 static int port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
326 struct net_device *dev,
327 const unsigned char *addr, u16 vid)
329 if (is_unicast_ether_addr(addr))
330 return ethsw_port_fdb_del_uc(netdev_priv(dev),
333 return ethsw_port_fdb_del_mc(netdev_priv(dev),
337 static void port_get_stats(struct net_device *netdev,
338 struct rtnl_link_stats64 *stats)
340 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
344 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
345 port_priv->ethsw_data->dpsw_handle,
347 DPSW_CNT_ING_FRAME, &stats->rx_packets);
351 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
352 port_priv->ethsw_data->dpsw_handle,
354 DPSW_CNT_EGR_FRAME, &stats->tx_packets);
358 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
359 port_priv->ethsw_data->dpsw_handle,
361 DPSW_CNT_ING_BYTE, &stats->rx_bytes);
365 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
366 port_priv->ethsw_data->dpsw_handle,
368 DPSW_CNT_EGR_BYTE, &stats->tx_bytes);
372 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
373 port_priv->ethsw_data->dpsw_handle,
375 DPSW_CNT_ING_FRAME_DISCARD,
380 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
381 port_priv->ethsw_data->dpsw_handle,
383 DPSW_CNT_ING_FLTR_FRAME,
387 stats->rx_dropped += tmp;
389 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
390 port_priv->ethsw_data->dpsw_handle,
392 DPSW_CNT_EGR_FRAME_DISCARD,
400 netdev_err(netdev, "dpsw_if_get_counter err %d\n", err);
403 static bool port_has_offload_stats(const struct net_device *netdev,
406 return (attr_id == IFLA_OFFLOAD_XSTATS_CPU_HIT);
409 static int port_get_offload_stats(int attr_id,
410 const struct net_device *netdev,
414 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
415 port_get_stats((struct net_device *)netdev, sp);
422 static int port_change_mtu(struct net_device *netdev, int mtu)
424 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
427 err = dpsw_if_set_max_frame_length(port_priv->ethsw_data->mc_io,
429 port_priv->ethsw_data->dpsw_handle,
431 (u16)ETHSW_L2_MAX_FRM(mtu));
434 "dpsw_if_set_max_frame_length() err %d\n", err);
442 static int port_carrier_state_sync(struct net_device *netdev)
444 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
445 struct dpsw_link_state state;
448 err = dpsw_if_get_link_state(port_priv->ethsw_data->mc_io, 0,
449 port_priv->ethsw_data->dpsw_handle,
450 port_priv->idx, &state);
452 netdev_err(netdev, "dpsw_if_get_link_state() err %d\n", err);
456 WARN_ONCE(state.up > 1, "Garbage read into link_state");
458 if (state.up != port_priv->link_state) {
460 netif_carrier_on(netdev);
462 netif_carrier_off(netdev);
463 port_priv->link_state = state.up;
468 static int port_open(struct net_device *netdev)
470 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
473 /* No need to allow Tx as control interface is disabled */
474 netif_tx_stop_all_queues(netdev);
476 /* Explicitly set carrier off, otherwise
477 * netif_carrier_ok() will return true and cause 'ip link show'
478 * to report the LOWER_UP flag, even though the link
479 * notification wasn't even received.
481 netif_carrier_off(netdev);
483 err = dpsw_if_enable(port_priv->ethsw_data->mc_io, 0,
484 port_priv->ethsw_data->dpsw_handle,
487 netdev_err(netdev, "dpsw_if_enable err %d\n", err);
491 /* sync carrier state */
492 err = port_carrier_state_sync(netdev);
495 "port_carrier_state_sync err %d\n", err);
496 goto err_carrier_sync;
502 dpsw_if_disable(port_priv->ethsw_data->mc_io, 0,
503 port_priv->ethsw_data->dpsw_handle,
508 static int port_stop(struct net_device *netdev)
510 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
513 err = dpsw_if_disable(port_priv->ethsw_data->mc_io, 0,
514 port_priv->ethsw_data->dpsw_handle,
517 netdev_err(netdev, "dpsw_if_disable err %d\n", err);
524 static netdev_tx_t port_dropframe(struct sk_buff *skb,
525 struct net_device *netdev)
527 /* we don't support I/O for now, drop the frame */
528 dev_kfree_skb_any(skb);
533 static int swdev_get_port_parent_id(struct net_device *dev,
534 struct netdev_phys_item_id *ppid)
536 struct ethsw_port_priv *port_priv = netdev_priv(dev);
539 ppid->id[0] = port_priv->ethsw_data->dev_id;
544 static int port_get_phys_name(struct net_device *netdev, char *name,
547 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
550 err = snprintf(name, len, "p%d", port_priv->idx);
557 struct ethsw_dump_ctx {
558 struct net_device *dev;
560 struct netlink_callback *cb;
564 static int ethsw_fdb_do_dump(struct fdb_dump_entry *entry,
565 struct ethsw_dump_ctx *dump)
567 int is_dynamic = entry->type & DPSW_FDB_ENTRY_DINAMIC;
568 u32 portid = NETLINK_CB(dump->cb->skb).portid;
569 u32 seq = dump->cb->nlh->nlmsg_seq;
570 struct nlmsghdr *nlh;
573 if (dump->idx < dump->cb->args[2])
576 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
577 sizeof(*ndm), NLM_F_MULTI);
581 ndm = nlmsg_data(nlh);
582 ndm->ndm_family = AF_BRIDGE;
585 ndm->ndm_flags = NTF_SELF;
587 ndm->ndm_ifindex = dump->dev->ifindex;
588 ndm->ndm_state = is_dynamic ? NUD_REACHABLE : NUD_NOARP;
590 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, entry->mac_addr))
591 goto nla_put_failure;
593 nlmsg_end(dump->skb, nlh);
600 nlmsg_cancel(dump->skb, nlh);
604 static int port_fdb_valid_entry(struct fdb_dump_entry *entry,
605 struct ethsw_port_priv *port_priv)
607 int idx = port_priv->idx;
610 if (entry->type & DPSW_FDB_ENTRY_TYPE_UNICAST)
611 valid = entry->if_info == port_priv->idx;
613 valid = entry->if_mask[idx / 8] & BIT(idx % 8);
618 static int port_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
619 struct net_device *net_dev,
620 struct net_device *filter_dev, int *idx)
622 struct ethsw_port_priv *port_priv = netdev_priv(net_dev);
623 struct ethsw_core *ethsw = port_priv->ethsw_data;
624 struct device *dev = net_dev->dev.parent;
625 struct fdb_dump_entry *fdb_entries;
626 struct fdb_dump_entry fdb_entry;
627 struct ethsw_dump_ctx dump = {
633 dma_addr_t fdb_dump_iova;
639 fdb_dump_size = ethsw->sw_attr.max_fdb_entries * sizeof(fdb_entry);
640 dma_mem = kzalloc(fdb_dump_size, GFP_KERNEL);
644 fdb_dump_iova = dma_map_single(dev, dma_mem, fdb_dump_size,
646 if (dma_mapping_error(dev, fdb_dump_iova)) {
647 netdev_err(net_dev, "dma_map_single() failed\n");
652 err = dpsw_fdb_dump(ethsw->mc_io, 0, ethsw->dpsw_handle, 0,
653 fdb_dump_iova, fdb_dump_size, &num_fdb_entries);
655 netdev_err(net_dev, "dpsw_fdb_dump() = %d\n", err);
659 dma_unmap_single(dev, fdb_dump_iova, fdb_dump_size, DMA_FROM_DEVICE);
661 fdb_entries = (struct fdb_dump_entry *)dma_mem;
662 for (i = 0; i < num_fdb_entries; i++) {
663 fdb_entry = fdb_entries[i];
665 if (!port_fdb_valid_entry(&fdb_entry, port_priv))
668 err = ethsw_fdb_do_dump(&fdb_entry, &dump);
681 dma_unmap_single(dev, fdb_dump_iova, fdb_dump_size, DMA_TO_DEVICE);
687 static const struct net_device_ops ethsw_port_ops = {
688 .ndo_open = port_open,
689 .ndo_stop = port_stop,
691 .ndo_set_mac_address = eth_mac_addr,
692 .ndo_get_stats64 = port_get_stats,
693 .ndo_change_mtu = port_change_mtu,
694 .ndo_has_offload_stats = port_has_offload_stats,
695 .ndo_get_offload_stats = port_get_offload_stats,
696 .ndo_fdb_add = port_fdb_add,
697 .ndo_fdb_del = port_fdb_del,
698 .ndo_fdb_dump = port_fdb_dump,
700 .ndo_start_xmit = port_dropframe,
701 .ndo_get_port_parent_id = swdev_get_port_parent_id,
702 .ndo_get_phys_port_name = port_get_phys_name,
705 static void ethsw_links_state_update(struct ethsw_core *ethsw)
709 for (i = 0; i < ethsw->sw_attr.num_ifs; i++)
710 port_carrier_state_sync(ethsw->ports[i]->netdev);
713 static irqreturn_t ethsw_irq0_handler_thread(int irq_num, void *arg)
715 struct device *dev = (struct device *)arg;
716 struct ethsw_core *ethsw = dev_get_drvdata(dev);
718 /* Mask the events and the if_id reserved bits to be cleared on read */
719 u32 status = DPSW_IRQ_EVENT_LINK_CHANGED | 0xFFFF0000;
722 err = dpsw_get_irq_status(ethsw->mc_io, 0, ethsw->dpsw_handle,
723 DPSW_IRQ_INDEX_IF, &status);
725 dev_err(dev, "Can't get irq status (err %d)\n", err);
727 err = dpsw_clear_irq_status(ethsw->mc_io, 0, ethsw->dpsw_handle,
728 DPSW_IRQ_INDEX_IF, 0xFFFFFFFF);
730 dev_err(dev, "Can't clear irq status (err %d)\n", err);
734 if (status & DPSW_IRQ_EVENT_LINK_CHANGED)
735 ethsw_links_state_update(ethsw);
741 static int ethsw_setup_irqs(struct fsl_mc_device *sw_dev)
743 struct device *dev = &sw_dev->dev;
744 struct ethsw_core *ethsw = dev_get_drvdata(dev);
745 u32 mask = DPSW_IRQ_EVENT_LINK_CHANGED;
746 struct fsl_mc_device_irq *irq;
749 err = fsl_mc_allocate_irqs(sw_dev);
751 dev_err(dev, "MC irqs allocation failed\n");
755 if (WARN_ON(sw_dev->obj_desc.irq_count != DPSW_IRQ_NUM)) {
760 err = dpsw_set_irq_enable(ethsw->mc_io, 0, ethsw->dpsw_handle,
761 DPSW_IRQ_INDEX_IF, 0);
763 dev_err(dev, "dpsw_set_irq_enable err %d\n", err);
767 irq = sw_dev->irqs[DPSW_IRQ_INDEX_IF];
769 err = devm_request_threaded_irq(dev, irq->msi_desc->irq,
771 ethsw_irq0_handler_thread,
772 IRQF_NO_SUSPEND | IRQF_ONESHOT,
775 dev_err(dev, "devm_request_threaded_irq(): %d\n", err);
779 err = dpsw_set_irq_mask(ethsw->mc_io, 0, ethsw->dpsw_handle,
780 DPSW_IRQ_INDEX_IF, mask);
782 dev_err(dev, "dpsw_set_irq_mask(): %d\n", err);
786 err = dpsw_set_irq_enable(ethsw->mc_io, 0, ethsw->dpsw_handle,
787 DPSW_IRQ_INDEX_IF, 1);
789 dev_err(dev, "dpsw_set_irq_enable(): %d\n", err);
796 devm_free_irq(dev, irq->msi_desc->irq, dev);
798 fsl_mc_free_irqs(sw_dev);
802 static void ethsw_teardown_irqs(struct fsl_mc_device *sw_dev)
804 struct device *dev = &sw_dev->dev;
805 struct ethsw_core *ethsw = dev_get_drvdata(dev);
808 err = dpsw_set_irq_enable(ethsw->mc_io, 0, ethsw->dpsw_handle,
809 DPSW_IRQ_INDEX_IF, 0);
811 dev_err(dev, "dpsw_set_irq_enable err %d\n", err);
813 fsl_mc_free_irqs(sw_dev);
816 static int port_attr_stp_state_set(struct net_device *netdev,
817 struct switchdev_trans *trans,
820 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
822 if (switchdev_trans_ph_prepare(trans))
825 return ethsw_port_set_stp_state(port_priv, state);
828 static int port_attr_br_flags_pre_set(struct net_device *netdev,
829 struct switchdev_trans *trans,
832 if (flags & ~(BR_LEARNING | BR_FLOOD))
838 static int port_attr_br_flags_set(struct net_device *netdev,
839 struct switchdev_trans *trans,
842 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
845 if (switchdev_trans_ph_prepare(trans))
848 /* Learning is enabled per switch */
849 err = ethsw_set_learning(port_priv->ethsw_data,
850 !!(flags & BR_LEARNING));
854 err = ethsw_port_set_flood(port_priv, !!(flags & BR_FLOOD));
860 static int swdev_port_attr_set(struct net_device *netdev,
861 const struct switchdev_attr *attr,
862 struct switchdev_trans *trans)
867 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
868 err = port_attr_stp_state_set(netdev, trans,
871 case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
872 err = port_attr_br_flags_pre_set(netdev, trans,
873 attr->u.brport_flags);
875 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
876 err = port_attr_br_flags_set(netdev, trans,
877 attr->u.brport_flags);
879 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
880 /* VLANs are supported by default */
890 static int port_vlans_add(struct net_device *netdev,
891 const struct switchdev_obj_port_vlan *vlan,
892 struct switchdev_trans *trans)
894 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
897 if (switchdev_trans_ph_prepare(trans))
900 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
901 if (!port_priv->ethsw_data->vlans[vid]) {
902 /* this is a new VLAN */
903 err = ethsw_add_vlan(port_priv->ethsw_data, vid);
907 port_priv->ethsw_data->vlans[vid] |= ETHSW_VLAN_GLOBAL;
909 err = ethsw_port_add_vlan(port_priv, vid, vlan->flags);
917 static int port_lookup_address(struct net_device *netdev, int is_uc,
918 const unsigned char *addr)
920 struct netdev_hw_addr_list *list = (is_uc) ? &netdev->uc : &netdev->mc;
921 struct netdev_hw_addr *ha;
923 netif_addr_lock_bh(netdev);
924 list_for_each_entry(ha, &list->list, list) {
925 if (ether_addr_equal(ha->addr, addr)) {
926 netif_addr_unlock_bh(netdev);
930 netif_addr_unlock_bh(netdev);
934 static int port_mdb_add(struct net_device *netdev,
935 const struct switchdev_obj_port_mdb *mdb,
936 struct switchdev_trans *trans)
938 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
941 if (switchdev_trans_ph_prepare(trans))
944 /* Check if address is already set on this port */
945 if (port_lookup_address(netdev, 0, mdb->addr))
948 err = ethsw_port_fdb_add_mc(port_priv, mdb->addr);
952 err = dev_mc_add(netdev, mdb->addr);
954 netdev_err(netdev, "dev_mc_add err %d\n", err);
955 ethsw_port_fdb_del_mc(port_priv, mdb->addr);
961 static int swdev_port_obj_add(struct net_device *netdev,
962 const struct switchdev_obj *obj,
963 struct switchdev_trans *trans)
968 case SWITCHDEV_OBJ_ID_PORT_VLAN:
969 err = port_vlans_add(netdev,
970 SWITCHDEV_OBJ_PORT_VLAN(obj),
973 case SWITCHDEV_OBJ_ID_PORT_MDB:
974 err = port_mdb_add(netdev,
975 SWITCHDEV_OBJ_PORT_MDB(obj),
986 static int ethsw_port_del_vlan(struct ethsw_port_priv *port_priv, u16 vid)
988 struct ethsw_core *ethsw = port_priv->ethsw_data;
989 struct net_device *netdev = port_priv->netdev;
990 struct dpsw_vlan_if_cfg vcfg;
993 if (!port_priv->vlans[vid])
996 if (port_priv->vlans[vid] & ETHSW_VLAN_PVID) {
997 err = ethsw_port_set_pvid(port_priv, 0);
1003 vcfg.if_id[0] = port_priv->idx;
1004 if (port_priv->vlans[vid] & ETHSW_VLAN_UNTAGGED) {
1005 err = dpsw_vlan_remove_if_untagged(ethsw->mc_io, 0,
1010 "dpsw_vlan_remove_if_untagged err %d\n",
1013 port_priv->vlans[vid] &= ~ETHSW_VLAN_UNTAGGED;
1016 if (port_priv->vlans[vid] & ETHSW_VLAN_MEMBER) {
1017 err = dpsw_vlan_remove_if(ethsw->mc_io, 0, ethsw->dpsw_handle,
1021 "dpsw_vlan_remove_if err %d\n", err);
1024 port_priv->vlans[vid] &= ~ETHSW_VLAN_MEMBER;
1026 /* Delete VLAN from switch if it is no longer configured on
1029 for (i = 0; i < ethsw->sw_attr.num_ifs; i++)
1030 if (ethsw->ports[i]->vlans[vid] & ETHSW_VLAN_MEMBER)
1031 return 0; /* Found a port member in VID */
1033 ethsw->vlans[vid] &= ~ETHSW_VLAN_GLOBAL;
1035 err = ethsw_dellink_switch(ethsw, vid);
1043 static int port_vlans_del(struct net_device *netdev,
1044 const struct switchdev_obj_port_vlan *vlan)
1046 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1049 if (netif_is_bridge_master(vlan->obj.orig_dev))
1052 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
1053 err = ethsw_port_del_vlan(port_priv, vid);
1061 static int port_mdb_del(struct net_device *netdev,
1062 const struct switchdev_obj_port_mdb *mdb)
1064 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1067 if (!port_lookup_address(netdev, 0, mdb->addr))
1070 err = ethsw_port_fdb_del_mc(port_priv, mdb->addr);
1074 err = dev_mc_del(netdev, mdb->addr);
1076 netdev_err(netdev, "dev_mc_del err %d\n", err);
1083 static int swdev_port_obj_del(struct net_device *netdev,
1084 const struct switchdev_obj *obj)
1089 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1090 err = port_vlans_del(netdev, SWITCHDEV_OBJ_PORT_VLAN(obj));
1092 case SWITCHDEV_OBJ_ID_PORT_MDB:
1093 err = port_mdb_del(netdev, SWITCHDEV_OBJ_PORT_MDB(obj));
1103 ethsw_switchdev_port_attr_set_event(struct net_device *netdev,
1104 struct switchdev_notifier_port_attr_info
1109 err = swdev_port_attr_set(netdev, port_attr_info->attr,
1110 port_attr_info->trans);
1112 port_attr_info->handled = true;
1113 return notifier_from_errno(err);
1116 /* For the moment, only flood setting needs to be updated */
1117 static int port_bridge_join(struct net_device *netdev,
1118 struct net_device *upper_dev)
1120 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1121 struct ethsw_core *ethsw = port_priv->ethsw_data;
1124 for (i = 0; i < ethsw->sw_attr.num_ifs; i++)
1125 if (ethsw->ports[i]->bridge_dev &&
1126 (ethsw->ports[i]->bridge_dev != upper_dev)) {
1128 "Only one bridge supported per DPSW object!\n");
1132 /* Enable flooding */
1133 err = ethsw_port_set_flood(port_priv, 1);
1135 port_priv->bridge_dev = upper_dev;
1140 static int port_bridge_leave(struct net_device *netdev)
1142 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1145 /* Disable flooding */
1146 err = ethsw_port_set_flood(port_priv, 0);
1148 port_priv->bridge_dev = NULL;
1153 static bool ethsw_port_dev_check(const struct net_device *netdev)
1155 return netdev->netdev_ops == ðsw_port_ops;
1158 static int port_netdevice_event(struct notifier_block *unused,
1159 unsigned long event, void *ptr)
1161 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
1162 struct netdev_notifier_changeupper_info *info = ptr;
1163 struct net_device *upper_dev;
1166 if (!ethsw_port_dev_check(netdev))
1169 /* Handle just upper dev link/unlink for the moment */
1170 if (event == NETDEV_CHANGEUPPER) {
1171 upper_dev = info->upper_dev;
1172 if (netif_is_bridge_master(upper_dev)) {
1174 err = port_bridge_join(netdev, upper_dev);
1176 err = port_bridge_leave(netdev);
1180 return notifier_from_errno(err);
1183 struct ethsw_switchdev_event_work {
1184 struct work_struct work;
1185 struct switchdev_notifier_fdb_info fdb_info;
1186 struct net_device *dev;
1187 unsigned long event;
1190 static void ethsw_switchdev_event_work(struct work_struct *work)
1192 struct ethsw_switchdev_event_work *switchdev_work =
1193 container_of(work, struct ethsw_switchdev_event_work, work);
1194 struct net_device *dev = switchdev_work->dev;
1195 struct switchdev_notifier_fdb_info *fdb_info;
1199 fdb_info = &switchdev_work->fdb_info;
1201 switch (switchdev_work->event) {
1202 case SWITCHDEV_FDB_ADD_TO_DEVICE:
1203 if (!fdb_info->added_by_user)
1205 if (is_unicast_ether_addr(fdb_info->addr))
1206 err = ethsw_port_fdb_add_uc(netdev_priv(dev),
1209 err = ethsw_port_fdb_add_mc(netdev_priv(dev),
1213 fdb_info->offloaded = true;
1214 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
1215 &fdb_info->info, NULL);
1217 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1218 if (!fdb_info->added_by_user)
1220 if (is_unicast_ether_addr(fdb_info->addr))
1221 ethsw_port_fdb_del_uc(netdev_priv(dev), fdb_info->addr);
1223 ethsw_port_fdb_del_mc(netdev_priv(dev), fdb_info->addr);
1228 kfree(switchdev_work->fdb_info.addr);
1229 kfree(switchdev_work);
1233 /* Called under rcu_read_lock() */
1234 static int port_switchdev_event(struct notifier_block *unused,
1235 unsigned long event, void *ptr)
1237 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1238 struct ethsw_port_priv *port_priv = netdev_priv(dev);
1239 struct ethsw_switchdev_event_work *switchdev_work;
1240 struct switchdev_notifier_fdb_info *fdb_info = ptr;
1241 struct ethsw_core *ethsw = port_priv->ethsw_data;
1243 if (!ethsw_port_dev_check(dev))
1246 if (event == SWITCHDEV_PORT_ATTR_SET)
1247 return ethsw_switchdev_port_attr_set_event(dev, ptr);
1249 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
1250 if (!switchdev_work)
1253 INIT_WORK(&switchdev_work->work, ethsw_switchdev_event_work);
1254 switchdev_work->dev = dev;
1255 switchdev_work->event = event;
1258 case SWITCHDEV_FDB_ADD_TO_DEVICE:
1259 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1260 memcpy(&switchdev_work->fdb_info, ptr,
1261 sizeof(switchdev_work->fdb_info));
1262 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
1263 if (!switchdev_work->fdb_info.addr)
1264 goto err_addr_alloc;
1266 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
1269 /* Take a reference on the device to avoid being freed. */
1273 kfree(switchdev_work);
1277 queue_work(ethsw->workqueue, &switchdev_work->work);
1282 kfree(switchdev_work);
1287 ethsw_switchdev_port_obj_event(unsigned long event, struct net_device *netdev,
1288 struct switchdev_notifier_port_obj_info
1291 int err = -EOPNOTSUPP;
1294 case SWITCHDEV_PORT_OBJ_ADD:
1295 err = swdev_port_obj_add(netdev, port_obj_info->obj,
1296 port_obj_info->trans);
1298 case SWITCHDEV_PORT_OBJ_DEL:
1299 err = swdev_port_obj_del(netdev, port_obj_info->obj);
1303 port_obj_info->handled = true;
1304 return notifier_from_errno(err);
1307 static int port_switchdev_blocking_event(struct notifier_block *unused,
1308 unsigned long event, void *ptr)
1310 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1312 if (!ethsw_port_dev_check(dev))
1316 case SWITCHDEV_PORT_OBJ_ADD: /* fall through */
1317 case SWITCHDEV_PORT_OBJ_DEL:
1318 return ethsw_switchdev_port_obj_event(event, dev, ptr);
1319 case SWITCHDEV_PORT_ATTR_SET:
1320 return ethsw_switchdev_port_attr_set_event(dev, ptr);
1326 static int ethsw_register_notifier(struct device *dev)
1328 struct ethsw_core *ethsw = dev_get_drvdata(dev);
1331 ethsw->port_nb.notifier_call = port_netdevice_event;
1332 err = register_netdevice_notifier(ðsw->port_nb);
1334 dev_err(dev, "Failed to register netdev notifier\n");
1338 ethsw->port_switchdev_nb.notifier_call = port_switchdev_event;
1339 err = register_switchdev_notifier(ðsw->port_switchdev_nb);
1341 dev_err(dev, "Failed to register switchdev notifier\n");
1342 goto err_switchdev_nb;
1345 ethsw->port_switchdevb_nb.notifier_call = port_switchdev_blocking_event;
1346 err = register_switchdev_blocking_notifier(ðsw->port_switchdevb_nb);
1348 dev_err(dev, "Failed to register switchdev blocking notifier\n");
1349 goto err_switchdev_blocking_nb;
1354 err_switchdev_blocking_nb:
1355 unregister_switchdev_notifier(ðsw->port_switchdev_nb);
1357 unregister_netdevice_notifier(ðsw->port_nb);
1361 static int ethsw_init(struct fsl_mc_device *sw_dev)
1363 struct device *dev = &sw_dev->dev;
1364 struct ethsw_core *ethsw = dev_get_drvdata(dev);
1365 u16 version_major, version_minor, i;
1366 struct dpsw_stp_cfg stp_cfg;
1369 ethsw->dev_id = sw_dev->obj_desc.id;
1371 err = dpsw_open(ethsw->mc_io, 0, ethsw->dev_id, ðsw->dpsw_handle);
1373 dev_err(dev, "dpsw_open err %d\n", err);
1377 err = dpsw_get_attributes(ethsw->mc_io, 0, ethsw->dpsw_handle,
1380 dev_err(dev, "dpsw_get_attributes err %d\n", err);
1384 err = dpsw_get_api_version(ethsw->mc_io, 0,
1388 dev_err(dev, "dpsw_get_api_version err %d\n", err);
1392 /* Minimum supported DPSW version check */
1393 if (version_major < DPSW_MIN_VER_MAJOR ||
1394 (version_major == DPSW_MIN_VER_MAJOR &&
1395 version_minor < DPSW_MIN_VER_MINOR)) {
1396 dev_err(dev, "DPSW version %d:%d not supported. Use %d.%d or greater.\n",
1399 DPSW_MIN_VER_MAJOR, DPSW_MIN_VER_MINOR);
1404 err = dpsw_reset(ethsw->mc_io, 0, ethsw->dpsw_handle);
1406 dev_err(dev, "dpsw_reset err %d\n", err);
1410 err = dpsw_fdb_set_learning_mode(ethsw->mc_io, 0, ethsw->dpsw_handle, 0,
1411 DPSW_FDB_LEARNING_MODE_HW);
1413 dev_err(dev, "dpsw_fdb_set_learning_mode err %d\n", err);
1417 stp_cfg.vlan_id = DEFAULT_VLAN_ID;
1418 stp_cfg.state = DPSW_STP_STATE_FORWARDING;
1420 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
1421 err = dpsw_if_set_stp(ethsw->mc_io, 0, ethsw->dpsw_handle, i,
1424 dev_err(dev, "dpsw_if_set_stp err %d for port %d\n",
1429 err = dpsw_if_set_broadcast(ethsw->mc_io, 0,
1430 ethsw->dpsw_handle, i, 1);
1433 "dpsw_if_set_broadcast err %d for port %d\n",
1439 ethsw->workqueue = alloc_ordered_workqueue("%s_%d_ordered",
1440 WQ_MEM_RECLAIM, "ethsw",
1442 if (!ethsw->workqueue) {
1447 err = ethsw_register_notifier(dev);
1449 goto err_destroy_ordered_workqueue;
1453 err_destroy_ordered_workqueue:
1454 destroy_workqueue(ethsw->workqueue);
1457 dpsw_close(ethsw->mc_io, 0, ethsw->dpsw_handle);
1461 static int ethsw_port_init(struct ethsw_port_priv *port_priv, u16 port)
1463 struct net_device *netdev = port_priv->netdev;
1464 struct ethsw_core *ethsw = port_priv->ethsw_data;
1465 struct dpsw_vlan_if_cfg vcfg;
1468 /* Switch starts with all ports configured to VLAN 1. Need to
1469 * remove this setting to allow configuration at bridge join
1472 vcfg.if_id[0] = port_priv->idx;
1474 err = dpsw_vlan_remove_if_untagged(ethsw->mc_io, 0, ethsw->dpsw_handle,
1475 DEFAULT_VLAN_ID, &vcfg);
1477 netdev_err(netdev, "dpsw_vlan_remove_if_untagged err %d\n",
1482 err = ethsw_port_set_pvid(port_priv, 0);
1486 err = dpsw_vlan_remove_if(ethsw->mc_io, 0, ethsw->dpsw_handle,
1487 DEFAULT_VLAN_ID, &vcfg);
1489 netdev_err(netdev, "dpsw_vlan_remove_if err %d\n", err);
1494 static void ethsw_unregister_notifier(struct device *dev)
1496 struct ethsw_core *ethsw = dev_get_drvdata(dev);
1497 struct notifier_block *nb;
1500 nb = ðsw->port_switchdevb_nb;
1501 err = unregister_switchdev_blocking_notifier(nb);
1504 "Failed to unregister switchdev blocking notifier (%d)\n",
1507 err = unregister_switchdev_notifier(ðsw->port_switchdev_nb);
1510 "Failed to unregister switchdev notifier (%d)\n", err);
1512 err = unregister_netdevice_notifier(ðsw->port_nb);
1515 "Failed to unregister netdev notifier (%d)\n", err);
1518 static void ethsw_takedown(struct fsl_mc_device *sw_dev)
1520 struct device *dev = &sw_dev->dev;
1521 struct ethsw_core *ethsw = dev_get_drvdata(dev);
1524 ethsw_unregister_notifier(dev);
1526 err = dpsw_close(ethsw->mc_io, 0, ethsw->dpsw_handle);
1528 dev_warn(dev, "dpsw_close err %d\n", err);
1531 static int ethsw_remove(struct fsl_mc_device *sw_dev)
1533 struct ethsw_port_priv *port_priv;
1534 struct ethsw_core *ethsw;
1539 ethsw = dev_get_drvdata(dev);
1541 ethsw_teardown_irqs(sw_dev);
1543 destroy_workqueue(ethsw->workqueue);
1545 dpsw_disable(ethsw->mc_io, 0, ethsw->dpsw_handle);
1547 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
1548 port_priv = ethsw->ports[i];
1549 unregister_netdev(port_priv->netdev);
1550 free_netdev(port_priv->netdev);
1552 kfree(ethsw->ports);
1554 ethsw_takedown(sw_dev);
1555 fsl_mc_portal_free(ethsw->mc_io);
1559 dev_set_drvdata(dev, NULL);
1564 static int ethsw_probe_port(struct ethsw_core *ethsw, u16 port_idx)
1566 struct ethsw_port_priv *port_priv;
1567 struct device *dev = ethsw->dev;
1568 struct net_device *port_netdev;
1571 port_netdev = alloc_etherdev(sizeof(struct ethsw_port_priv));
1573 dev_err(dev, "alloc_etherdev error\n");
1577 port_priv = netdev_priv(port_netdev);
1578 port_priv->netdev = port_netdev;
1579 port_priv->ethsw_data = ethsw;
1581 port_priv->idx = port_idx;
1582 port_priv->stp_state = BR_STATE_FORWARDING;
1584 /* Flooding is implicitly enabled */
1585 port_priv->flood = true;
1587 SET_NETDEV_DEV(port_netdev, dev);
1588 port_netdev->netdev_ops = ðsw_port_ops;
1589 port_netdev->ethtool_ops = ðsw_port_ethtool_ops;
1591 /* Set MTU limits */
1592 port_netdev->min_mtu = ETH_MIN_MTU;
1593 port_netdev->max_mtu = ETHSW_MAX_FRAME_LENGTH;
1595 err = ethsw_port_init(port_priv, port_idx);
1597 goto err_port_probe;
1599 err = register_netdev(port_netdev);
1601 dev_err(dev, "register_netdev error %d\n", err);
1602 goto err_port_probe;
1605 ethsw->ports[port_idx] = port_priv;
1610 free_netdev(port_netdev);
1615 static int ethsw_probe(struct fsl_mc_device *sw_dev)
1617 struct device *dev = &sw_dev->dev;
1618 struct ethsw_core *ethsw;
1621 /* Allocate switch core*/
1622 ethsw = kzalloc(sizeof(*ethsw), GFP_KERNEL);
1628 dev_set_drvdata(dev, ethsw);
1630 err = fsl_mc_portal_allocate(sw_dev, FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
1634 err = -EPROBE_DEFER;
1636 dev_err(dev, "fsl_mc_portal_allocate err %d\n", err);
1637 goto err_free_drvdata;
1640 err = ethsw_init(sw_dev);
1642 goto err_free_cmdport;
1644 /* DEFAULT_VLAN_ID is implicitly configured on the switch */
1645 ethsw->vlans[DEFAULT_VLAN_ID] = ETHSW_VLAN_MEMBER;
1647 /* Learning is implicitly enabled */
1648 ethsw->learning = true;
1650 ethsw->ports = kcalloc(ethsw->sw_attr.num_ifs, sizeof(*ethsw->ports),
1652 if (!(ethsw->ports)) {
1657 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
1658 err = ethsw_probe_port(ethsw, i);
1660 goto err_free_ports;
1663 err = dpsw_enable(ethsw->mc_io, 0, ethsw->dpsw_handle);
1665 dev_err(ethsw->dev, "dpsw_enable err %d\n", err);
1666 goto err_free_ports;
1670 err = ethsw_setup_irqs(sw_dev);
1674 dev_info(dev, "probed %d port switch\n", ethsw->sw_attr.num_ifs);
1678 dpsw_disable(ethsw->mc_io, 0, ethsw->dpsw_handle);
1681 /* Cleanup registered ports only */
1682 for (i--; i >= 0; i--) {
1683 unregister_netdev(ethsw->ports[i]->netdev);
1684 free_netdev(ethsw->ports[i]->netdev);
1686 kfree(ethsw->ports);
1689 ethsw_takedown(sw_dev);
1692 fsl_mc_portal_free(ethsw->mc_io);
1696 dev_set_drvdata(dev, NULL);
1701 static const struct fsl_mc_device_id ethsw_match_id_table[] = {
1703 .vendor = FSL_MC_VENDOR_FREESCALE,
1708 MODULE_DEVICE_TABLE(fslmc, ethsw_match_id_table);
1710 static struct fsl_mc_driver eth_sw_drv = {
1712 .name = KBUILD_MODNAME,
1713 .owner = THIS_MODULE,
1715 .probe = ethsw_probe,
1716 .remove = ethsw_remove,
1717 .match_id_table = ethsw_match_id_table
1720 module_fsl_mc_driver(eth_sw_drv);
1722 MODULE_LICENSE("GPL v2");
1723 MODULE_DESCRIPTION("DPAA2 Ethernet Switch Driver");