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_fdb_set_learning_mode 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. */
1079 queue_work(ethsw_owq
, &switchdev_work
->work
);
1084 kfree(switchdev_work
);
1088 static struct notifier_block port_switchdev_nb
= {
1089 .notifier_call
= port_switchdev_event
,
1092 static int ethsw_register_notifier(struct device
*dev
)
1096 err
= register_netdevice_notifier(&port_nb
);
1098 dev_err(dev
, "Failed to register netdev notifier\n");
1102 err
= register_switchdev_notifier(&port_switchdev_nb
);
1104 dev_err(dev
, "Failed to register switchdev notifier\n");
1105 goto err_switchdev_nb
;
1111 unregister_netdevice_notifier(&port_nb
);
1115 static int ethsw_open(struct ethsw_core
*ethsw
)
1117 struct ethsw_port_priv
*port_priv
= NULL
;
1120 err
= dpsw_enable(ethsw
->mc_io
, 0, ethsw
->dpsw_handle
);
1122 dev_err(ethsw
->dev
, "dpsw_enable err %d\n", err
);
1126 for (i
= 0; i
< ethsw
->sw_attr
.num_ifs
; i
++) {
1127 port_priv
= ethsw
->ports
[i
];
1128 err
= dev_open(port_priv
->netdev
);
1130 netdev_err(port_priv
->netdev
, "dev_open err %d\n", err
);
1138 static int ethsw_stop(struct ethsw_core
*ethsw
)
1140 struct ethsw_port_priv
*port_priv
= NULL
;
1143 for (i
= 0; i
< ethsw
->sw_attr
.num_ifs
; i
++) {
1144 port_priv
= ethsw
->ports
[i
];
1145 dev_close(port_priv
->netdev
);
1148 err
= dpsw_disable(ethsw
->mc_io
, 0, ethsw
->dpsw_handle
);
1150 dev_err(ethsw
->dev
, "dpsw_disable err %d\n", err
);
1157 static int ethsw_init(struct fsl_mc_device
*sw_dev
)
1159 struct device
*dev
= &sw_dev
->dev
;
1160 struct ethsw_core
*ethsw
= dev_get_drvdata(dev
);
1161 u16 version_major
, version_minor
, i
;
1162 struct dpsw_stp_cfg stp_cfg
;
1165 ethsw
->dev_id
= sw_dev
->obj_desc
.id
;
1167 err
= dpsw_open(ethsw
->mc_io
, 0, ethsw
->dev_id
, ðsw
->dpsw_handle
);
1169 dev_err(dev
, "dpsw_open err %d\n", err
);
1173 err
= dpsw_get_attributes(ethsw
->mc_io
, 0, ethsw
->dpsw_handle
,
1176 dev_err(dev
, "dpsw_get_attributes err %d\n", err
);
1180 err
= dpsw_get_api_version(ethsw
->mc_io
, 0,
1184 dev_err(dev
, "dpsw_get_api_version err %d\n", err
);
1188 /* Minimum supported DPSW version check */
1189 if (version_major
< DPSW_MIN_VER_MAJOR
||
1190 (version_major
== DPSW_MIN_VER_MAJOR
&&
1191 version_minor
< DPSW_MIN_VER_MINOR
)) {
1192 dev_err(dev
, "DPSW version %d:%d not supported. Use %d.%d or greater.\n",
1195 DPSW_MIN_VER_MAJOR
, DPSW_MIN_VER_MINOR
);
1200 err
= dpsw_reset(ethsw
->mc_io
, 0, ethsw
->dpsw_handle
);
1202 dev_err(dev
, "dpsw_reset err %d\n", err
);
1206 err
= dpsw_fdb_set_learning_mode(ethsw
->mc_io
, 0, ethsw
->dpsw_handle
, 0,
1207 DPSW_FDB_LEARNING_MODE_HW
);
1209 dev_err(dev
, "dpsw_fdb_set_learning_mode err %d\n", err
);
1213 stp_cfg
.vlan_id
= DEFAULT_VLAN_ID
;
1214 stp_cfg
.state
= DPSW_STP_STATE_FORWARDING
;
1216 for (i
= 0; i
< ethsw
->sw_attr
.num_ifs
; i
++) {
1217 err
= dpsw_if_set_stp(ethsw
->mc_io
, 0, ethsw
->dpsw_handle
, i
,
1220 dev_err(dev
, "dpsw_if_set_stp err %d for port %d\n",
1225 err
= dpsw_if_set_broadcast(ethsw
->mc_io
, 0,
1226 ethsw
->dpsw_handle
, i
, 1);
1229 "dpsw_if_set_broadcast err %d for port %d\n",
1235 ethsw_owq
= alloc_ordered_workqueue("%s_ordered", WQ_MEM_RECLAIM
,
1242 err
= ethsw_register_notifier(dev
);
1244 goto err_destroy_ordered_workqueue
;
1248 err_destroy_ordered_workqueue
:
1249 destroy_workqueue(ethsw_owq
);
1252 dpsw_close(ethsw
->mc_io
, 0, ethsw
->dpsw_handle
);
1256 static int ethsw_port_init(struct ethsw_port_priv
*port_priv
, u16 port
)
1258 const char def_mcast
[ETH_ALEN
] = {0x01, 0x00, 0x5e, 0x00, 0x00, 0x01};
1259 struct net_device
*netdev
= port_priv
->netdev
;
1260 struct ethsw_core
*ethsw
= port_priv
->ethsw_data
;
1261 struct dpsw_vlan_if_cfg vcfg
;
1264 /* Switch starts with all ports configured to VLAN 1. Need to
1265 * remove this setting to allow configuration at bridge join
1268 vcfg
.if_id
[0] = port_priv
->idx
;
1270 err
= dpsw_vlan_remove_if_untagged(ethsw
->mc_io
, 0, ethsw
->dpsw_handle
,
1271 DEFAULT_VLAN_ID
, &vcfg
);
1273 netdev_err(netdev
, "dpsw_vlan_remove_if_untagged err %d\n",
1278 err
= ethsw_port_set_pvid(port_priv
, 0);
1282 err
= dpsw_vlan_remove_if(ethsw
->mc_io
, 0, ethsw
->dpsw_handle
,
1283 DEFAULT_VLAN_ID
, &vcfg
);
1285 netdev_err(netdev
, "dpsw_vlan_remove_if err %d\n", err
);
1289 err
= ethsw_port_fdb_add_mc(port_priv
, def_mcast
);
1294 static void ethsw_unregister_notifier(struct device
*dev
)
1298 err
= unregister_switchdev_notifier(&port_switchdev_nb
);
1301 "Failed to unregister switchdev notifier (%d)\n", err
);
1303 err
= unregister_netdevice_notifier(&port_nb
);
1306 "Failed to unregister netdev notifier (%d)\n", err
);
1309 static void ethsw_takedown(struct fsl_mc_device
*sw_dev
)
1311 struct device
*dev
= &sw_dev
->dev
;
1312 struct ethsw_core
*ethsw
= dev_get_drvdata(dev
);
1315 ethsw_unregister_notifier(dev
);
1317 err
= dpsw_close(ethsw
->mc_io
, 0, ethsw
->dpsw_handle
);
1319 dev_warn(dev
, "dpsw_close err %d\n", err
);
1322 static int ethsw_remove(struct fsl_mc_device
*sw_dev
)
1324 struct ethsw_port_priv
*port_priv
;
1325 struct ethsw_core
*ethsw
;
1330 ethsw
= dev_get_drvdata(dev
);
1332 ethsw_teardown_irqs(sw_dev
);
1334 destroy_workqueue(ethsw_owq
);
1340 for (i
= 0; i
< ethsw
->sw_attr
.num_ifs
; i
++) {
1341 port_priv
= ethsw
->ports
[i
];
1342 unregister_netdev(port_priv
->netdev
);
1343 free_netdev(port_priv
->netdev
);
1345 kfree(ethsw
->ports
);
1347 ethsw_takedown(sw_dev
);
1348 fsl_mc_portal_free(ethsw
->mc_io
);
1352 dev_set_drvdata(dev
, NULL
);
1357 static int ethsw_probe_port(struct ethsw_core
*ethsw
, u16 port_idx
)
1359 struct ethsw_port_priv
*port_priv
;
1360 struct device
*dev
= ethsw
->dev
;
1361 struct net_device
*port_netdev
;
1364 port_netdev
= alloc_etherdev(sizeof(struct ethsw_port_priv
));
1366 dev_err(dev
, "alloc_etherdev error\n");
1370 port_priv
= netdev_priv(port_netdev
);
1371 port_priv
->netdev
= port_netdev
;
1372 port_priv
->ethsw_data
= ethsw
;
1374 port_priv
->idx
= port_idx
;
1375 port_priv
->stp_state
= BR_STATE_FORWARDING
;
1377 /* Flooding is implicitly enabled */
1378 port_priv
->flood
= true;
1380 SET_NETDEV_DEV(port_netdev
, dev
);
1381 port_netdev
->netdev_ops
= ðsw_port_ops
;
1382 port_netdev
->ethtool_ops
= ðsw_port_ethtool_ops
;
1383 port_netdev
->switchdev_ops
= ðsw_port_switchdev_ops
;
1385 /* Set MTU limits */
1386 port_netdev
->min_mtu
= ETH_MIN_MTU
;
1387 port_netdev
->max_mtu
= ETHSW_MAX_FRAME_LENGTH
;
1389 err
= register_netdev(port_netdev
);
1391 dev_err(dev
, "register_netdev error %d\n", err
);
1392 free_netdev(port_netdev
);
1396 ethsw
->ports
[port_idx
] = port_priv
;
1398 return ethsw_port_init(port_priv
, port_idx
);
1401 static int ethsw_probe(struct fsl_mc_device
*sw_dev
)
1403 struct device
*dev
= &sw_dev
->dev
;
1404 struct ethsw_core
*ethsw
;
1407 /* Allocate switch core*/
1408 ethsw
= kzalloc(sizeof(*ethsw
), GFP_KERNEL
);
1414 dev_set_drvdata(dev
, ethsw
);
1416 err
= fsl_mc_portal_allocate(sw_dev
, 0, ðsw
->mc_io
);
1419 err
= -EPROBE_DEFER
;
1421 dev_err(dev
, "fsl_mc_portal_allocate err %d\n", err
);
1422 goto err_free_drvdata
;
1425 err
= ethsw_init(sw_dev
);
1427 goto err_free_cmdport
;
1429 /* DEFAULT_VLAN_ID is implicitly configured on the switch */
1430 ethsw
->vlans
[DEFAULT_VLAN_ID
] = ETHSW_VLAN_MEMBER
;
1432 /* Learning is implicitly enabled */
1433 ethsw
->learning
= true;
1435 ethsw
->ports
= kcalloc(ethsw
->sw_attr
.num_ifs
, sizeof(*ethsw
->ports
),
1437 if (!(ethsw
->ports
)) {
1442 for (i
= 0; i
< ethsw
->sw_attr
.num_ifs
; i
++) {
1443 err
= ethsw_probe_port(ethsw
, i
);
1445 goto err_free_ports
;
1448 /* Switch starts up enabled */
1450 err
= ethsw_open(ethsw
);
1453 goto err_free_ports
;
1456 err
= ethsw_setup_irqs(sw_dev
);
1460 dev_info(dev
, "probed %d port switch\n", ethsw
->sw_attr
.num_ifs
);
1469 /* Cleanup registered ports only */
1470 for (i
--; i
>= 0; i
--) {
1471 unregister_netdev(ethsw
->ports
[i
]->netdev
);
1472 free_netdev(ethsw
->ports
[i
]->netdev
);
1474 kfree(ethsw
->ports
);
1477 ethsw_takedown(sw_dev
);
1480 fsl_mc_portal_free(ethsw
->mc_io
);
1484 dev_set_drvdata(dev
, NULL
);
1489 static const struct fsl_mc_device_id ethsw_match_id_table
[] = {
1491 .vendor
= FSL_MC_VENDOR_FREESCALE
,
1496 MODULE_DEVICE_TABLE(fslmc
, ethsw_match_id_table
);
1498 static struct fsl_mc_driver eth_sw_drv
= {
1500 .name
= KBUILD_MODNAME
,
1501 .owner
= THIS_MODULE
,
1503 .probe
= ethsw_probe
,
1504 .remove
= ethsw_remove
,
1505 .match_id_table
= ethsw_match_id_table
1508 module_fsl_mc_driver(eth_sw_drv
);
1510 MODULE_LICENSE("GPL v2");
1511 MODULE_DESCRIPTION("DPAA2 Ethernet Switch Driver");