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 bool ethsw_port_is_up(struct ethsw_port_priv *port_priv)
48 struct net_device *netdev = port_priv->netdev;
49 struct dpsw_link_state state;
52 err = dpsw_if_get_link_state(port_priv->ethsw_data->mc_io, 0,
53 port_priv->ethsw_data->dpsw_handle,
54 port_priv->idx, &state);
56 netdev_err(netdev, "dpsw_if_get_link_state() err %d\n", err);
60 WARN_ONCE(state.up > 1, "Garbage read into link_state");
62 return state.up ? true : false;
65 static int ethsw_port_set_pvid(struct ethsw_port_priv *port_priv, u16 pvid)
67 struct ethsw_core *ethsw = port_priv->ethsw_data;
68 struct net_device *netdev = port_priv->netdev;
69 struct dpsw_tci_cfg tci_cfg = { 0 };
73 err = dpsw_if_get_tci(ethsw->mc_io, 0, ethsw->dpsw_handle,
74 port_priv->idx, &tci_cfg);
76 netdev_err(netdev, "dpsw_if_get_tci err %d\n", err);
80 tci_cfg.vlan_id = pvid;
82 /* Interface needs to be down to change PVID */
83 up = ethsw_port_is_up(port_priv);
85 err = dpsw_if_disable(ethsw->mc_io, 0,
89 netdev_err(netdev, "dpsw_if_disable err %d\n", err);
94 err = dpsw_if_set_tci(ethsw->mc_io, 0, ethsw->dpsw_handle,
95 port_priv->idx, &tci_cfg);
97 netdev_err(netdev, "dpsw_if_set_tci err %d\n", err);
101 /* Delete previous PVID info and mark the new one */
102 port_priv->vlans[port_priv->pvid] &= ~ETHSW_VLAN_PVID;
103 port_priv->vlans[pvid] |= ETHSW_VLAN_PVID;
104 port_priv->pvid = pvid;
108 ret = dpsw_if_enable(ethsw->mc_io, 0,
112 netdev_err(netdev, "dpsw_if_enable err %d\n", ret);
120 static int ethsw_port_add_vlan(struct ethsw_port_priv *port_priv,
123 struct ethsw_core *ethsw = port_priv->ethsw_data;
124 struct net_device *netdev = port_priv->netdev;
125 struct dpsw_vlan_if_cfg vcfg;
128 if (port_priv->vlans[vid]) {
129 netdev_warn(netdev, "VLAN %d already configured\n", vid);
134 vcfg.if_id[0] = port_priv->idx;
135 err = dpsw_vlan_add_if(ethsw->mc_io, 0, ethsw->dpsw_handle, vid, &vcfg);
137 netdev_err(netdev, "dpsw_vlan_add_if err %d\n", err);
141 port_priv->vlans[vid] = ETHSW_VLAN_MEMBER;
143 if (flags & BRIDGE_VLAN_INFO_UNTAGGED) {
144 err = dpsw_vlan_add_if_untagged(ethsw->mc_io, 0,
149 "dpsw_vlan_add_if_untagged err %d\n", err);
152 port_priv->vlans[vid] |= ETHSW_VLAN_UNTAGGED;
155 if (flags & BRIDGE_VLAN_INFO_PVID) {
156 err = ethsw_port_set_pvid(port_priv, vid);
164 static int ethsw_set_learning(struct ethsw_core *ethsw, bool enable)
166 enum dpsw_fdb_learning_mode learn_mode;
170 learn_mode = DPSW_FDB_LEARNING_MODE_HW;
172 learn_mode = DPSW_FDB_LEARNING_MODE_DIS;
174 err = dpsw_fdb_set_learning_mode(ethsw->mc_io, 0, ethsw->dpsw_handle, 0,
177 dev_err(ethsw->dev, "dpsw_fdb_set_learning_mode err %d\n", err);
180 ethsw->learning = enable;
185 static int ethsw_port_set_flood(struct ethsw_port_priv *port_priv, bool enable)
189 err = dpsw_if_set_flooding(port_priv->ethsw_data->mc_io, 0,
190 port_priv->ethsw_data->dpsw_handle,
191 port_priv->idx, enable);
193 netdev_err(port_priv->netdev,
194 "dpsw_if_set_flooding err %d\n", err);
197 port_priv->flood = enable;
202 static int ethsw_port_set_stp_state(struct ethsw_port_priv *port_priv, u8 state)
204 struct dpsw_stp_cfg stp_cfg = {
210 if (!netif_running(port_priv->netdev) || state == port_priv->stp_state)
211 return 0; /* Nothing to do */
213 for (vid = 0; vid <= VLAN_VID_MASK; vid++) {
214 if (port_priv->vlans[vid] & ETHSW_VLAN_MEMBER) {
215 stp_cfg.vlan_id = vid;
216 err = dpsw_if_set_stp(port_priv->ethsw_data->mc_io, 0,
217 port_priv->ethsw_data->dpsw_handle,
218 port_priv->idx, &stp_cfg);
220 netdev_err(port_priv->netdev,
221 "dpsw_if_set_stp err %d\n", err);
227 port_priv->stp_state = state;
232 static int ethsw_dellink_switch(struct ethsw_core *ethsw, u16 vid)
234 struct ethsw_port_priv *ppriv_local = NULL;
237 if (!ethsw->vlans[vid])
240 err = dpsw_vlan_remove(ethsw->mc_io, 0, ethsw->dpsw_handle, vid);
242 dev_err(ethsw->dev, "dpsw_vlan_remove err %d\n", err);
245 ethsw->vlans[vid] = 0;
247 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
248 ppriv_local = ethsw->ports[i];
249 ppriv_local->vlans[vid] = 0;
255 static int ethsw_port_fdb_add_uc(struct ethsw_port_priv *port_priv,
256 const unsigned char *addr)
258 struct dpsw_fdb_unicast_cfg entry = {0};
261 entry.if_egress = port_priv->idx;
262 entry.type = DPSW_FDB_ENTRY_STATIC;
263 ether_addr_copy(entry.mac_addr, addr);
265 err = dpsw_fdb_add_unicast(port_priv->ethsw_data->mc_io, 0,
266 port_priv->ethsw_data->dpsw_handle,
269 netdev_err(port_priv->netdev,
270 "dpsw_fdb_add_unicast err %d\n", err);
274 static int ethsw_port_fdb_del_uc(struct ethsw_port_priv *port_priv,
275 const unsigned char *addr)
277 struct dpsw_fdb_unicast_cfg entry = {0};
280 entry.if_egress = port_priv->idx;
281 entry.type = DPSW_FDB_ENTRY_STATIC;
282 ether_addr_copy(entry.mac_addr, addr);
284 err = dpsw_fdb_remove_unicast(port_priv->ethsw_data->mc_io, 0,
285 port_priv->ethsw_data->dpsw_handle,
287 /* Silently discard error for calling multiple times the del command */
288 if (err && err != -ENXIO)
289 netdev_err(port_priv->netdev,
290 "dpsw_fdb_remove_unicast err %d\n", err);
294 static int ethsw_port_fdb_add_mc(struct ethsw_port_priv *port_priv,
295 const unsigned char *addr)
297 struct dpsw_fdb_multicast_cfg entry = {0};
300 ether_addr_copy(entry.mac_addr, addr);
301 entry.type = DPSW_FDB_ENTRY_STATIC;
303 entry.if_id[0] = port_priv->idx;
305 err = dpsw_fdb_add_multicast(port_priv->ethsw_data->mc_io, 0,
306 port_priv->ethsw_data->dpsw_handle,
308 /* Silently discard error for calling multiple times the add command */
309 if (err && err != -ENXIO)
310 netdev_err(port_priv->netdev, "dpsw_fdb_add_multicast err %d\n",
315 static int ethsw_port_fdb_del_mc(struct ethsw_port_priv *port_priv,
316 const unsigned char *addr)
318 struct dpsw_fdb_multicast_cfg entry = {0};
321 ether_addr_copy(entry.mac_addr, addr);
322 entry.type = DPSW_FDB_ENTRY_STATIC;
324 entry.if_id[0] = port_priv->idx;
326 err = dpsw_fdb_remove_multicast(port_priv->ethsw_data->mc_io, 0,
327 port_priv->ethsw_data->dpsw_handle,
329 /* Silently discard error for calling multiple times the del command */
330 if (err && err != -ENAVAIL)
331 netdev_err(port_priv->netdev,
332 "dpsw_fdb_remove_multicast err %d\n", err);
336 static int port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
337 struct net_device *dev, const unsigned char *addr,
339 struct netlink_ext_ack *extack)
341 if (is_unicast_ether_addr(addr))
342 return ethsw_port_fdb_add_uc(netdev_priv(dev),
345 return ethsw_port_fdb_add_mc(netdev_priv(dev),
349 static int port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
350 struct net_device *dev,
351 const unsigned char *addr, u16 vid)
353 if (is_unicast_ether_addr(addr))
354 return ethsw_port_fdb_del_uc(netdev_priv(dev),
357 return ethsw_port_fdb_del_mc(netdev_priv(dev),
361 static void port_get_stats(struct net_device *netdev,
362 struct rtnl_link_stats64 *stats)
364 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
368 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
369 port_priv->ethsw_data->dpsw_handle,
371 DPSW_CNT_ING_FRAME, &stats->rx_packets);
375 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
376 port_priv->ethsw_data->dpsw_handle,
378 DPSW_CNT_EGR_FRAME, &stats->tx_packets);
382 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
383 port_priv->ethsw_data->dpsw_handle,
385 DPSW_CNT_ING_BYTE, &stats->rx_bytes);
389 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
390 port_priv->ethsw_data->dpsw_handle,
392 DPSW_CNT_EGR_BYTE, &stats->tx_bytes);
396 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
397 port_priv->ethsw_data->dpsw_handle,
399 DPSW_CNT_ING_FRAME_DISCARD,
404 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
405 port_priv->ethsw_data->dpsw_handle,
407 DPSW_CNT_ING_FLTR_FRAME,
411 stats->rx_dropped += tmp;
413 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
414 port_priv->ethsw_data->dpsw_handle,
416 DPSW_CNT_EGR_FRAME_DISCARD,
424 netdev_err(netdev, "dpsw_if_get_counter err %d\n", err);
427 static bool port_has_offload_stats(const struct net_device *netdev,
430 return (attr_id == IFLA_OFFLOAD_XSTATS_CPU_HIT);
433 static int port_get_offload_stats(int attr_id,
434 const struct net_device *netdev,
438 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
439 port_get_stats((struct net_device *)netdev, sp);
446 static int port_change_mtu(struct net_device *netdev, int mtu)
448 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
451 err = dpsw_if_set_max_frame_length(port_priv->ethsw_data->mc_io,
453 port_priv->ethsw_data->dpsw_handle,
455 (u16)ETHSW_L2_MAX_FRM(mtu));
458 "dpsw_if_set_max_frame_length() err %d\n", err);
466 static int port_carrier_state_sync(struct net_device *netdev)
468 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
469 struct dpsw_link_state state;
472 /* Interrupts are received even though no one issued an 'ifconfig up'
473 * on the switch interface. Ignore these link state update interrupts
475 if (!netif_running(netdev))
478 err = dpsw_if_get_link_state(port_priv->ethsw_data->mc_io, 0,
479 port_priv->ethsw_data->dpsw_handle,
480 port_priv->idx, &state);
482 netdev_err(netdev, "dpsw_if_get_link_state() err %d\n", err);
486 WARN_ONCE(state.up > 1, "Garbage read into link_state");
488 if (state.up != port_priv->link_state) {
490 netif_carrier_on(netdev);
492 netif_carrier_off(netdev);
493 port_priv->link_state = state.up;
499 static int port_open(struct net_device *netdev)
501 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
504 /* No need to allow Tx as control interface is disabled */
505 netif_tx_stop_all_queues(netdev);
507 /* Explicitly set carrier off, otherwise
508 * netif_carrier_ok() will return true and cause 'ip link show'
509 * to report the LOWER_UP flag, even though the link
510 * notification wasn't even received.
512 netif_carrier_off(netdev);
514 err = dpsw_if_enable(port_priv->ethsw_data->mc_io, 0,
515 port_priv->ethsw_data->dpsw_handle,
518 netdev_err(netdev, "dpsw_if_enable err %d\n", err);
522 /* sync carrier state */
523 err = port_carrier_state_sync(netdev);
526 "port_carrier_state_sync err %d\n", err);
527 goto err_carrier_sync;
533 dpsw_if_disable(port_priv->ethsw_data->mc_io, 0,
534 port_priv->ethsw_data->dpsw_handle,
539 static int port_stop(struct net_device *netdev)
541 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
544 err = dpsw_if_disable(port_priv->ethsw_data->mc_io, 0,
545 port_priv->ethsw_data->dpsw_handle,
548 netdev_err(netdev, "dpsw_if_disable err %d\n", err);
555 static netdev_tx_t port_dropframe(struct sk_buff *skb,
556 struct net_device *netdev)
558 /* we don't support I/O for now, drop the frame */
559 dev_kfree_skb_any(skb);
564 static int swdev_get_port_parent_id(struct net_device *dev,
565 struct netdev_phys_item_id *ppid)
567 struct ethsw_port_priv *port_priv = netdev_priv(dev);
570 ppid->id[0] = port_priv->ethsw_data->dev_id;
575 static int port_get_phys_name(struct net_device *netdev, char *name,
578 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
581 err = snprintf(name, len, "p%d", port_priv->idx);
588 struct ethsw_dump_ctx {
589 struct net_device *dev;
591 struct netlink_callback *cb;
595 static int ethsw_fdb_do_dump(struct fdb_dump_entry *entry,
596 struct ethsw_dump_ctx *dump)
598 int is_dynamic = entry->type & DPSW_FDB_ENTRY_DINAMIC;
599 u32 portid = NETLINK_CB(dump->cb->skb).portid;
600 u32 seq = dump->cb->nlh->nlmsg_seq;
601 struct nlmsghdr *nlh;
604 if (dump->idx < dump->cb->args[2])
607 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
608 sizeof(*ndm), NLM_F_MULTI);
612 ndm = nlmsg_data(nlh);
613 ndm->ndm_family = AF_BRIDGE;
616 ndm->ndm_flags = NTF_SELF;
618 ndm->ndm_ifindex = dump->dev->ifindex;
619 ndm->ndm_state = is_dynamic ? NUD_REACHABLE : NUD_NOARP;
621 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, entry->mac_addr))
622 goto nla_put_failure;
624 nlmsg_end(dump->skb, nlh);
631 nlmsg_cancel(dump->skb, nlh);
635 static int port_fdb_valid_entry(struct fdb_dump_entry *entry,
636 struct ethsw_port_priv *port_priv)
638 int idx = port_priv->idx;
641 if (entry->type & DPSW_FDB_ENTRY_TYPE_UNICAST)
642 valid = entry->if_info == port_priv->idx;
644 valid = entry->if_mask[idx / 8] & BIT(idx % 8);
649 static int port_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
650 struct net_device *net_dev,
651 struct net_device *filter_dev, int *idx)
653 struct ethsw_port_priv *port_priv = netdev_priv(net_dev);
654 struct ethsw_core *ethsw = port_priv->ethsw_data;
655 struct device *dev = net_dev->dev.parent;
656 struct fdb_dump_entry *fdb_entries;
657 struct fdb_dump_entry fdb_entry;
658 struct ethsw_dump_ctx dump = {
664 dma_addr_t fdb_dump_iova;
670 fdb_dump_size = ethsw->sw_attr.max_fdb_entries * sizeof(fdb_entry);
671 dma_mem = kzalloc(fdb_dump_size, GFP_KERNEL);
675 fdb_dump_iova = dma_map_single(dev, dma_mem, fdb_dump_size,
677 if (dma_mapping_error(dev, fdb_dump_iova)) {
678 netdev_err(net_dev, "dma_map_single() failed\n");
683 err = dpsw_fdb_dump(ethsw->mc_io, 0, ethsw->dpsw_handle, 0,
684 fdb_dump_iova, fdb_dump_size, &num_fdb_entries);
686 netdev_err(net_dev, "dpsw_fdb_dump() = %d\n", err);
690 dma_unmap_single(dev, fdb_dump_iova, fdb_dump_size, DMA_FROM_DEVICE);
692 fdb_entries = (struct fdb_dump_entry *)dma_mem;
693 for (i = 0; i < num_fdb_entries; i++) {
694 fdb_entry = fdb_entries[i];
696 if (!port_fdb_valid_entry(&fdb_entry, port_priv))
699 err = ethsw_fdb_do_dump(&fdb_entry, &dump);
712 dma_unmap_single(dev, fdb_dump_iova, fdb_dump_size, DMA_TO_DEVICE);
718 static int ethsw_port_set_mac_addr(struct ethsw_port_priv *port_priv)
720 struct ethsw_core *ethsw = port_priv->ethsw_data;
721 struct net_device *net_dev = port_priv->netdev;
722 struct device *dev = net_dev->dev.parent;
723 u8 mac_addr[ETH_ALEN];
726 if (!(ethsw->features & ETHSW_FEATURE_MAC_ADDR))
729 /* Get firmware address, if any */
730 err = dpsw_if_get_port_mac_addr(ethsw->mc_io, 0, ethsw->dpsw_handle,
731 port_priv->idx, mac_addr);
733 dev_err(dev, "dpsw_if_get_port_mac_addr() failed\n");
737 /* First check if firmware has any address configured by bootloader */
738 if (!is_zero_ether_addr(mac_addr)) {
739 memcpy(net_dev->dev_addr, mac_addr, net_dev->addr_len);
741 /* No MAC address configured, fill in net_dev->dev_addr
744 eth_hw_addr_random(net_dev);
745 dev_dbg_once(dev, "device(s) have all-zero hwaddr, replaced with random\n");
747 /* Override NET_ADDR_RANDOM set by eth_hw_addr_random(); for all
748 * practical purposes, this will be our "permanent" mac address,
749 * at least until the next reboot. This move will also permit
750 * register_netdevice() to properly fill up net_dev->perm_addr.
752 net_dev->addr_assign_type = NET_ADDR_PERM;
758 static const struct net_device_ops ethsw_port_ops = {
759 .ndo_open = port_open,
760 .ndo_stop = port_stop,
762 .ndo_set_mac_address = eth_mac_addr,
763 .ndo_get_stats64 = port_get_stats,
764 .ndo_change_mtu = port_change_mtu,
765 .ndo_has_offload_stats = port_has_offload_stats,
766 .ndo_get_offload_stats = port_get_offload_stats,
767 .ndo_fdb_add = port_fdb_add,
768 .ndo_fdb_del = port_fdb_del,
769 .ndo_fdb_dump = port_fdb_dump,
771 .ndo_start_xmit = port_dropframe,
772 .ndo_get_port_parent_id = swdev_get_port_parent_id,
773 .ndo_get_phys_port_name = port_get_phys_name,
776 static bool ethsw_port_dev_check(const struct net_device *netdev,
777 struct notifier_block *nb)
779 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
781 if (netdev->netdev_ops == ðsw_port_ops &&
782 (!nb || &port_priv->ethsw_data->port_nb == nb ||
783 &port_priv->ethsw_data->port_switchdev_nb == nb ||
784 &port_priv->ethsw_data->port_switchdevb_nb == nb))
790 static void ethsw_links_state_update(struct ethsw_core *ethsw)
794 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
795 port_carrier_state_sync(ethsw->ports[i]->netdev);
796 ethsw_port_set_mac_addr(ethsw->ports[i]);
800 static irqreturn_t ethsw_irq0_handler_thread(int irq_num, void *arg)
802 struct device *dev = (struct device *)arg;
803 struct ethsw_core *ethsw = dev_get_drvdata(dev);
805 /* Mask the events and the if_id reserved bits to be cleared on read */
806 u32 status = DPSW_IRQ_EVENT_LINK_CHANGED | 0xFFFF0000;
809 err = dpsw_get_irq_status(ethsw->mc_io, 0, ethsw->dpsw_handle,
810 DPSW_IRQ_INDEX_IF, &status);
812 dev_err(dev, "Can't get irq status (err %d)\n", err);
814 err = dpsw_clear_irq_status(ethsw->mc_io, 0, ethsw->dpsw_handle,
815 DPSW_IRQ_INDEX_IF, 0xFFFFFFFF);
817 dev_err(dev, "Can't clear irq status (err %d)\n", err);
821 if (status & DPSW_IRQ_EVENT_LINK_CHANGED)
822 ethsw_links_state_update(ethsw);
828 static int ethsw_setup_irqs(struct fsl_mc_device *sw_dev)
830 struct device *dev = &sw_dev->dev;
831 struct ethsw_core *ethsw = dev_get_drvdata(dev);
832 u32 mask = DPSW_IRQ_EVENT_LINK_CHANGED;
833 struct fsl_mc_device_irq *irq;
836 err = fsl_mc_allocate_irqs(sw_dev);
838 dev_err(dev, "MC irqs allocation failed\n");
842 if (WARN_ON(sw_dev->obj_desc.irq_count != DPSW_IRQ_NUM)) {
847 err = dpsw_set_irq_enable(ethsw->mc_io, 0, ethsw->dpsw_handle,
848 DPSW_IRQ_INDEX_IF, 0);
850 dev_err(dev, "dpsw_set_irq_enable err %d\n", err);
854 irq = sw_dev->irqs[DPSW_IRQ_INDEX_IF];
856 err = devm_request_threaded_irq(dev, irq->msi_desc->irq,
858 ethsw_irq0_handler_thread,
859 IRQF_NO_SUSPEND | IRQF_ONESHOT,
862 dev_err(dev, "devm_request_threaded_irq(): %d\n", err);
866 err = dpsw_set_irq_mask(ethsw->mc_io, 0, ethsw->dpsw_handle,
867 DPSW_IRQ_INDEX_IF, mask);
869 dev_err(dev, "dpsw_set_irq_mask(): %d\n", err);
873 err = dpsw_set_irq_enable(ethsw->mc_io, 0, ethsw->dpsw_handle,
874 DPSW_IRQ_INDEX_IF, 1);
876 dev_err(dev, "dpsw_set_irq_enable(): %d\n", err);
883 devm_free_irq(dev, irq->msi_desc->irq, dev);
885 fsl_mc_free_irqs(sw_dev);
889 static void ethsw_teardown_irqs(struct fsl_mc_device *sw_dev)
891 struct device *dev = &sw_dev->dev;
892 struct ethsw_core *ethsw = dev_get_drvdata(dev);
895 err = dpsw_set_irq_enable(ethsw->mc_io, 0, ethsw->dpsw_handle,
896 DPSW_IRQ_INDEX_IF, 0);
898 dev_err(dev, "dpsw_set_irq_enable err %d\n", err);
900 fsl_mc_free_irqs(sw_dev);
903 static int port_attr_stp_state_set(struct net_device *netdev,
904 struct switchdev_trans *trans,
907 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
909 if (switchdev_trans_ph_prepare(trans))
912 return ethsw_port_set_stp_state(port_priv, state);
915 static int port_attr_br_flags_pre_set(struct net_device *netdev,
916 struct switchdev_trans *trans,
919 if (flags & ~(BR_LEARNING | BR_FLOOD))
925 static int port_attr_br_flags_set(struct net_device *netdev,
926 struct switchdev_trans *trans,
929 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
932 if (switchdev_trans_ph_prepare(trans))
935 /* Learning is enabled per switch */
936 err = ethsw_set_learning(port_priv->ethsw_data,
937 !!(flags & BR_LEARNING));
941 err = ethsw_port_set_flood(port_priv, !!(flags & BR_FLOOD));
947 static int swdev_port_attr_set(struct net_device *netdev,
948 const struct switchdev_attr *attr,
949 struct switchdev_trans *trans)
954 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
955 err = port_attr_stp_state_set(netdev, trans,
958 case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
959 err = port_attr_br_flags_pre_set(netdev, trans,
960 attr->u.brport_flags);
962 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
963 err = port_attr_br_flags_set(netdev, trans,
964 attr->u.brport_flags);
966 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
967 /* VLANs are supported by default */
977 static int port_vlans_add(struct net_device *netdev,
978 const struct switchdev_obj_port_vlan *vlan,
979 struct switchdev_trans *trans)
981 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
982 struct ethsw_core *ethsw = port_priv->ethsw_data;
983 struct dpsw_attr *attr = ðsw->sw_attr;
984 int vid, err = 0, new_vlans = 0;
986 if (switchdev_trans_ph_prepare(trans)) {
987 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++)
988 if (!port_priv->ethsw_data->vlans[vid])
991 /* Check if there is space for a new VLAN */
992 err = dpsw_get_attributes(ethsw->mc_io, 0, ethsw->dpsw_handle,
995 netdev_err(netdev, "dpsw_get_attributes err %d\n", err);
998 if (attr->max_vlans - attr->num_vlans < new_vlans)
1004 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
1005 if (!port_priv->ethsw_data->vlans[vid]) {
1006 /* this is a new VLAN */
1007 err = ethsw_add_vlan(port_priv->ethsw_data, vid);
1011 port_priv->ethsw_data->vlans[vid] |= ETHSW_VLAN_GLOBAL;
1013 err = ethsw_port_add_vlan(port_priv, vid, vlan->flags);
1021 static int port_lookup_address(struct net_device *netdev, int is_uc,
1022 const unsigned char *addr)
1024 struct netdev_hw_addr_list *list = (is_uc) ? &netdev->uc : &netdev->mc;
1025 struct netdev_hw_addr *ha;
1027 netif_addr_lock_bh(netdev);
1028 list_for_each_entry(ha, &list->list, list) {
1029 if (ether_addr_equal(ha->addr, addr)) {
1030 netif_addr_unlock_bh(netdev);
1034 netif_addr_unlock_bh(netdev);
1038 static int port_mdb_add(struct net_device *netdev,
1039 const struct switchdev_obj_port_mdb *mdb,
1040 struct switchdev_trans *trans)
1042 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1045 if (switchdev_trans_ph_prepare(trans))
1048 /* Check if address is already set on this port */
1049 if (port_lookup_address(netdev, 0, mdb->addr))
1052 err = ethsw_port_fdb_add_mc(port_priv, mdb->addr);
1056 err = dev_mc_add(netdev, mdb->addr);
1058 netdev_err(netdev, "dev_mc_add err %d\n", err);
1059 ethsw_port_fdb_del_mc(port_priv, mdb->addr);
1065 static int swdev_port_obj_add(struct net_device *netdev,
1066 const struct switchdev_obj *obj,
1067 struct switchdev_trans *trans)
1072 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1073 err = port_vlans_add(netdev,
1074 SWITCHDEV_OBJ_PORT_VLAN(obj),
1077 case SWITCHDEV_OBJ_ID_PORT_MDB:
1078 err = port_mdb_add(netdev,
1079 SWITCHDEV_OBJ_PORT_MDB(obj),
1090 static int ethsw_port_del_vlan(struct ethsw_port_priv *port_priv, u16 vid)
1092 struct ethsw_core *ethsw = port_priv->ethsw_data;
1093 struct net_device *netdev = port_priv->netdev;
1094 struct dpsw_vlan_if_cfg vcfg;
1097 if (!port_priv->vlans[vid])
1100 if (port_priv->vlans[vid] & ETHSW_VLAN_PVID) {
1101 err = ethsw_port_set_pvid(port_priv, 0);
1107 vcfg.if_id[0] = port_priv->idx;
1108 if (port_priv->vlans[vid] & ETHSW_VLAN_UNTAGGED) {
1109 err = dpsw_vlan_remove_if_untagged(ethsw->mc_io, 0,
1114 "dpsw_vlan_remove_if_untagged err %d\n",
1117 port_priv->vlans[vid] &= ~ETHSW_VLAN_UNTAGGED;
1120 if (port_priv->vlans[vid] & ETHSW_VLAN_MEMBER) {
1121 err = dpsw_vlan_remove_if(ethsw->mc_io, 0, ethsw->dpsw_handle,
1125 "dpsw_vlan_remove_if err %d\n", err);
1128 port_priv->vlans[vid] &= ~ETHSW_VLAN_MEMBER;
1130 /* Delete VLAN from switch if it is no longer configured on
1133 for (i = 0; i < ethsw->sw_attr.num_ifs; i++)
1134 if (ethsw->ports[i]->vlans[vid] & ETHSW_VLAN_MEMBER)
1135 return 0; /* Found a port member in VID */
1137 ethsw->vlans[vid] &= ~ETHSW_VLAN_GLOBAL;
1139 err = ethsw_dellink_switch(ethsw, vid);
1147 static int port_vlans_del(struct net_device *netdev,
1148 const struct switchdev_obj_port_vlan *vlan)
1150 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1153 if (netif_is_bridge_master(vlan->obj.orig_dev))
1156 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
1157 err = ethsw_port_del_vlan(port_priv, vid);
1165 static int port_mdb_del(struct net_device *netdev,
1166 const struct switchdev_obj_port_mdb *mdb)
1168 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1171 if (!port_lookup_address(netdev, 0, mdb->addr))
1174 err = ethsw_port_fdb_del_mc(port_priv, mdb->addr);
1178 err = dev_mc_del(netdev, mdb->addr);
1180 netdev_err(netdev, "dev_mc_del err %d\n", err);
1187 static int swdev_port_obj_del(struct net_device *netdev,
1188 const struct switchdev_obj *obj)
1193 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1194 err = port_vlans_del(netdev, SWITCHDEV_OBJ_PORT_VLAN(obj));
1196 case SWITCHDEV_OBJ_ID_PORT_MDB:
1197 err = port_mdb_del(netdev, SWITCHDEV_OBJ_PORT_MDB(obj));
1207 ethsw_switchdev_port_attr_set_event(struct net_device *netdev,
1208 struct switchdev_notifier_port_attr_info
1213 err = swdev_port_attr_set(netdev, port_attr_info->attr,
1214 port_attr_info->trans);
1216 port_attr_info->handled = true;
1217 return notifier_from_errno(err);
1220 /* For the moment, only flood setting needs to be updated */
1221 static int port_bridge_join(struct net_device *netdev,
1222 struct net_device *upper_dev)
1224 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1225 struct ethsw_core *ethsw = port_priv->ethsw_data;
1226 struct ethsw_port_priv *other_port_priv;
1227 struct net_device *other_dev;
1228 struct list_head *iter;
1231 for (i = 0; i < ethsw->sw_attr.num_ifs; i++)
1232 if (ethsw->ports[i]->bridge_dev &&
1233 (ethsw->ports[i]->bridge_dev != upper_dev)) {
1235 "Only one bridge supported per DPSW object!\n");
1239 netdev_for_each_lower_dev(upper_dev, other_dev, iter) {
1240 if (!ethsw_port_dev_check(other_dev, NULL))
1243 other_port_priv = netdev_priv(other_dev);
1244 if (other_port_priv->ethsw_data != port_priv->ethsw_data) {
1246 "Interface from a different DPSW is in the bridge already!\n");
1251 /* Enable flooding */
1252 err = ethsw_port_set_flood(port_priv, 1);
1254 port_priv->bridge_dev = upper_dev;
1259 static int port_bridge_leave(struct net_device *netdev)
1261 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1264 /* Disable flooding */
1265 err = ethsw_port_set_flood(port_priv, 0);
1267 port_priv->bridge_dev = NULL;
1272 static int port_netdevice_event(struct notifier_block *nb,
1273 unsigned long event, void *ptr)
1275 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
1276 struct netdev_notifier_changeupper_info *info = ptr;
1277 struct net_device *upper_dev;
1280 if (!ethsw_port_dev_check(netdev, nb))
1283 /* Handle just upper dev link/unlink for the moment */
1284 if (event == NETDEV_CHANGEUPPER) {
1285 upper_dev = info->upper_dev;
1286 if (netif_is_bridge_master(upper_dev)) {
1288 err = port_bridge_join(netdev, upper_dev);
1290 err = port_bridge_leave(netdev);
1294 return notifier_from_errno(err);
1297 struct ethsw_switchdev_event_work {
1298 struct work_struct work;
1299 struct switchdev_notifier_fdb_info fdb_info;
1300 struct net_device *dev;
1301 unsigned long event;
1304 static void ethsw_switchdev_event_work(struct work_struct *work)
1306 struct ethsw_switchdev_event_work *switchdev_work =
1307 container_of(work, struct ethsw_switchdev_event_work, work);
1308 struct net_device *dev = switchdev_work->dev;
1309 struct switchdev_notifier_fdb_info *fdb_info;
1313 fdb_info = &switchdev_work->fdb_info;
1315 switch (switchdev_work->event) {
1316 case SWITCHDEV_FDB_ADD_TO_DEVICE:
1317 if (!fdb_info->added_by_user)
1319 if (is_unicast_ether_addr(fdb_info->addr))
1320 err = ethsw_port_fdb_add_uc(netdev_priv(dev),
1323 err = ethsw_port_fdb_add_mc(netdev_priv(dev),
1327 fdb_info->offloaded = true;
1328 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
1329 &fdb_info->info, NULL);
1331 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1332 if (!fdb_info->added_by_user)
1334 if (is_unicast_ether_addr(fdb_info->addr))
1335 ethsw_port_fdb_del_uc(netdev_priv(dev), fdb_info->addr);
1337 ethsw_port_fdb_del_mc(netdev_priv(dev), fdb_info->addr);
1342 kfree(switchdev_work->fdb_info.addr);
1343 kfree(switchdev_work);
1347 /* Called under rcu_read_lock() */
1348 static int port_switchdev_event(struct notifier_block *nb,
1349 unsigned long event, void *ptr)
1351 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1352 struct ethsw_port_priv *port_priv = netdev_priv(dev);
1353 struct ethsw_switchdev_event_work *switchdev_work;
1354 struct switchdev_notifier_fdb_info *fdb_info = ptr;
1355 struct ethsw_core *ethsw = port_priv->ethsw_data;
1357 if (!ethsw_port_dev_check(dev, nb))
1360 if (event == SWITCHDEV_PORT_ATTR_SET)
1361 return ethsw_switchdev_port_attr_set_event(dev, ptr);
1363 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
1364 if (!switchdev_work)
1367 INIT_WORK(&switchdev_work->work, ethsw_switchdev_event_work);
1368 switchdev_work->dev = dev;
1369 switchdev_work->event = event;
1372 case SWITCHDEV_FDB_ADD_TO_DEVICE:
1373 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1374 memcpy(&switchdev_work->fdb_info, ptr,
1375 sizeof(switchdev_work->fdb_info));
1376 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
1377 if (!switchdev_work->fdb_info.addr)
1378 goto err_addr_alloc;
1380 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
1383 /* Take a reference on the device to avoid being freed. */
1387 kfree(switchdev_work);
1391 queue_work(ethsw->workqueue, &switchdev_work->work);
1396 kfree(switchdev_work);
1401 ethsw_switchdev_port_obj_event(unsigned long event, struct net_device *netdev,
1402 struct switchdev_notifier_port_obj_info
1405 int err = -EOPNOTSUPP;
1408 case SWITCHDEV_PORT_OBJ_ADD:
1409 err = swdev_port_obj_add(netdev, port_obj_info->obj,
1410 port_obj_info->trans);
1412 case SWITCHDEV_PORT_OBJ_DEL:
1413 err = swdev_port_obj_del(netdev, port_obj_info->obj);
1417 port_obj_info->handled = true;
1418 return notifier_from_errno(err);
1421 static int port_switchdev_blocking_event(struct notifier_block *nb,
1422 unsigned long event, void *ptr)
1424 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1426 if (!ethsw_port_dev_check(dev, nb))
1430 case SWITCHDEV_PORT_OBJ_ADD:
1431 case SWITCHDEV_PORT_OBJ_DEL:
1432 return ethsw_switchdev_port_obj_event(event, dev, ptr);
1433 case SWITCHDEV_PORT_ATTR_SET:
1434 return ethsw_switchdev_port_attr_set_event(dev, ptr);
1440 static int ethsw_register_notifier(struct device *dev)
1442 struct ethsw_core *ethsw = dev_get_drvdata(dev);
1445 ethsw->port_nb.notifier_call = port_netdevice_event;
1446 err = register_netdevice_notifier(ðsw->port_nb);
1448 dev_err(dev, "Failed to register netdev notifier\n");
1452 ethsw->port_switchdev_nb.notifier_call = port_switchdev_event;
1453 err = register_switchdev_notifier(ðsw->port_switchdev_nb);
1455 dev_err(dev, "Failed to register switchdev notifier\n");
1456 goto err_switchdev_nb;
1459 ethsw->port_switchdevb_nb.notifier_call = port_switchdev_blocking_event;
1460 err = register_switchdev_blocking_notifier(ðsw->port_switchdevb_nb);
1462 dev_err(dev, "Failed to register switchdev blocking notifier\n");
1463 goto err_switchdev_blocking_nb;
1468 err_switchdev_blocking_nb:
1469 unregister_switchdev_notifier(ðsw->port_switchdev_nb);
1471 unregister_netdevice_notifier(ðsw->port_nb);
1475 static void ethsw_detect_features(struct ethsw_core *ethsw)
1477 ethsw->features = 0;
1479 if (ethsw->major > 8 || (ethsw->major == 8 && ethsw->minor >= 6))
1480 ethsw->features |= ETHSW_FEATURE_MAC_ADDR;
1483 static int ethsw_init(struct fsl_mc_device *sw_dev)
1485 struct device *dev = &sw_dev->dev;
1486 struct ethsw_core *ethsw = dev_get_drvdata(dev);
1487 struct dpsw_stp_cfg stp_cfg;
1491 ethsw->dev_id = sw_dev->obj_desc.id;
1493 err = dpsw_open(ethsw->mc_io, 0, ethsw->dev_id, ðsw->dpsw_handle);
1495 dev_err(dev, "dpsw_open err %d\n", err);
1499 err = dpsw_get_attributes(ethsw->mc_io, 0, ethsw->dpsw_handle,
1502 dev_err(dev, "dpsw_get_attributes err %d\n", err);
1506 err = dpsw_get_api_version(ethsw->mc_io, 0,
1510 dev_err(dev, "dpsw_get_api_version err %d\n", err);
1514 /* Minimum supported DPSW version check */
1515 if (ethsw->major < DPSW_MIN_VER_MAJOR ||
1516 (ethsw->major == DPSW_MIN_VER_MAJOR &&
1517 ethsw->minor < DPSW_MIN_VER_MINOR)) {
1518 dev_err(dev, "DPSW version %d:%d not supported. Use %d.%d or greater.\n",
1521 DPSW_MIN_VER_MAJOR, DPSW_MIN_VER_MINOR);
1526 ethsw_detect_features(ethsw);
1528 err = dpsw_reset(ethsw->mc_io, 0, ethsw->dpsw_handle);
1530 dev_err(dev, "dpsw_reset err %d\n", err);
1534 err = dpsw_fdb_set_learning_mode(ethsw->mc_io, 0, ethsw->dpsw_handle, 0,
1535 DPSW_FDB_LEARNING_MODE_HW);
1537 dev_err(dev, "dpsw_fdb_set_learning_mode err %d\n", err);
1541 stp_cfg.vlan_id = DEFAULT_VLAN_ID;
1542 stp_cfg.state = DPSW_STP_STATE_FORWARDING;
1544 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
1545 err = dpsw_if_set_stp(ethsw->mc_io, 0, ethsw->dpsw_handle, i,
1548 dev_err(dev, "dpsw_if_set_stp err %d for port %d\n",
1553 err = dpsw_if_set_broadcast(ethsw->mc_io, 0,
1554 ethsw->dpsw_handle, i, 1);
1557 "dpsw_if_set_broadcast err %d for port %d\n",
1563 ethsw->workqueue = alloc_ordered_workqueue("%s_%d_ordered",
1564 WQ_MEM_RECLAIM, "ethsw",
1566 if (!ethsw->workqueue) {
1571 err = ethsw_register_notifier(dev);
1573 goto err_destroy_ordered_workqueue;
1577 err_destroy_ordered_workqueue:
1578 destroy_workqueue(ethsw->workqueue);
1581 dpsw_close(ethsw->mc_io, 0, ethsw->dpsw_handle);
1585 static int ethsw_port_init(struct ethsw_port_priv *port_priv, u16 port)
1587 struct net_device *netdev = port_priv->netdev;
1588 struct ethsw_core *ethsw = port_priv->ethsw_data;
1589 struct dpsw_vlan_if_cfg vcfg;
1592 /* Switch starts with all ports configured to VLAN 1. Need to
1593 * remove this setting to allow configuration at bridge join
1596 vcfg.if_id[0] = port_priv->idx;
1598 err = dpsw_vlan_remove_if_untagged(ethsw->mc_io, 0, ethsw->dpsw_handle,
1599 DEFAULT_VLAN_ID, &vcfg);
1601 netdev_err(netdev, "dpsw_vlan_remove_if_untagged err %d\n",
1606 err = ethsw_port_set_pvid(port_priv, 0);
1610 err = dpsw_vlan_remove_if(ethsw->mc_io, 0, ethsw->dpsw_handle,
1611 DEFAULT_VLAN_ID, &vcfg);
1613 netdev_err(netdev, "dpsw_vlan_remove_if err %d\n", err);
1618 static void ethsw_unregister_notifier(struct device *dev)
1620 struct ethsw_core *ethsw = dev_get_drvdata(dev);
1621 struct notifier_block *nb;
1624 nb = ðsw->port_switchdevb_nb;
1625 err = unregister_switchdev_blocking_notifier(nb);
1628 "Failed to unregister switchdev blocking notifier (%d)\n",
1631 err = unregister_switchdev_notifier(ðsw->port_switchdev_nb);
1634 "Failed to unregister switchdev notifier (%d)\n", err);
1636 err = unregister_netdevice_notifier(ðsw->port_nb);
1639 "Failed to unregister netdev notifier (%d)\n", err);
1642 static void ethsw_takedown(struct fsl_mc_device *sw_dev)
1644 struct device *dev = &sw_dev->dev;
1645 struct ethsw_core *ethsw = dev_get_drvdata(dev);
1648 ethsw_unregister_notifier(dev);
1650 err = dpsw_close(ethsw->mc_io, 0, ethsw->dpsw_handle);
1652 dev_warn(dev, "dpsw_close err %d\n", err);
1655 static int ethsw_remove(struct fsl_mc_device *sw_dev)
1657 struct ethsw_port_priv *port_priv;
1658 struct ethsw_core *ethsw;
1663 ethsw = dev_get_drvdata(dev);
1665 ethsw_teardown_irqs(sw_dev);
1667 dpsw_disable(ethsw->mc_io, 0, ethsw->dpsw_handle);
1669 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
1670 port_priv = ethsw->ports[i];
1671 unregister_netdev(port_priv->netdev);
1672 free_netdev(port_priv->netdev);
1674 kfree(ethsw->ports);
1676 ethsw_takedown(sw_dev);
1678 destroy_workqueue(ethsw->workqueue);
1680 fsl_mc_portal_free(ethsw->mc_io);
1684 dev_set_drvdata(dev, NULL);
1689 static int ethsw_probe_port(struct ethsw_core *ethsw, u16 port_idx)
1691 struct ethsw_port_priv *port_priv;
1692 struct device *dev = ethsw->dev;
1693 struct net_device *port_netdev;
1696 port_netdev = alloc_etherdev(sizeof(struct ethsw_port_priv));
1698 dev_err(dev, "alloc_etherdev error\n");
1702 port_priv = netdev_priv(port_netdev);
1703 port_priv->netdev = port_netdev;
1704 port_priv->ethsw_data = ethsw;
1706 port_priv->idx = port_idx;
1707 port_priv->stp_state = BR_STATE_FORWARDING;
1709 /* Flooding is implicitly enabled */
1710 port_priv->flood = true;
1712 SET_NETDEV_DEV(port_netdev, dev);
1713 port_netdev->netdev_ops = ðsw_port_ops;
1714 port_netdev->ethtool_ops = &dpaa2_switch_port_ethtool_ops;
1716 /* Set MTU limits */
1717 port_netdev->min_mtu = ETH_MIN_MTU;
1718 port_netdev->max_mtu = ETHSW_MAX_FRAME_LENGTH;
1720 err = ethsw_port_init(port_priv, port_idx);
1722 goto err_port_probe;
1724 err = ethsw_port_set_mac_addr(port_priv);
1726 goto err_port_probe;
1728 err = register_netdev(port_netdev);
1730 dev_err(dev, "register_netdev error %d\n", err);
1731 goto err_port_probe;
1734 ethsw->ports[port_idx] = port_priv;
1739 free_netdev(port_netdev);
1744 static int ethsw_probe(struct fsl_mc_device *sw_dev)
1746 struct device *dev = &sw_dev->dev;
1747 struct ethsw_core *ethsw;
1750 /* Allocate switch core*/
1751 ethsw = kzalloc(sizeof(*ethsw), GFP_KERNEL);
1757 dev_set_drvdata(dev, ethsw);
1759 err = fsl_mc_portal_allocate(sw_dev, FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
1763 err = -EPROBE_DEFER;
1765 dev_err(dev, "fsl_mc_portal_allocate err %d\n", err);
1766 goto err_free_drvdata;
1769 err = ethsw_init(sw_dev);
1771 goto err_free_cmdport;
1773 /* DEFAULT_VLAN_ID is implicitly configured on the switch */
1774 ethsw->vlans[DEFAULT_VLAN_ID] = ETHSW_VLAN_MEMBER;
1776 /* Learning is implicitly enabled */
1777 ethsw->learning = true;
1779 ethsw->ports = kcalloc(ethsw->sw_attr.num_ifs, sizeof(*ethsw->ports),
1781 if (!(ethsw->ports)) {
1786 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
1787 err = ethsw_probe_port(ethsw, i);
1789 goto err_free_ports;
1792 err = dpsw_enable(ethsw->mc_io, 0, ethsw->dpsw_handle);
1794 dev_err(ethsw->dev, "dpsw_enable err %d\n", err);
1795 goto err_free_ports;
1798 /* Make sure the switch ports are disabled at probe time */
1799 for (i = 0; i < ethsw->sw_attr.num_ifs; i++)
1800 dpsw_if_disable(ethsw->mc_io, 0, ethsw->dpsw_handle, i);
1803 err = ethsw_setup_irqs(sw_dev);
1807 dev_info(dev, "probed %d port switch\n", ethsw->sw_attr.num_ifs);
1811 dpsw_disable(ethsw->mc_io, 0, ethsw->dpsw_handle);
1814 /* Cleanup registered ports only */
1815 for (i--; i >= 0; i--) {
1816 unregister_netdev(ethsw->ports[i]->netdev);
1817 free_netdev(ethsw->ports[i]->netdev);
1819 kfree(ethsw->ports);
1822 ethsw_takedown(sw_dev);
1825 fsl_mc_portal_free(ethsw->mc_io);
1829 dev_set_drvdata(dev, NULL);
1834 static const struct fsl_mc_device_id ethsw_match_id_table[] = {
1836 .vendor = FSL_MC_VENDOR_FREESCALE,
1841 MODULE_DEVICE_TABLE(fslmc, ethsw_match_id_table);
1843 static struct fsl_mc_driver eth_sw_drv = {
1845 .name = KBUILD_MODNAME,
1846 .owner = THIS_MODULE,
1848 .probe = ethsw_probe,
1849 .remove = ethsw_remove,
1850 .match_id_table = ethsw_match_id_table
1853 module_fsl_mc_driver(eth_sw_drv);
1855 MODULE_LICENSE("GPL v2");
1856 MODULE_DESCRIPTION("DPAA2 Ethernet Switch Driver");