1 // SPDX-License-Identifier: GPL-2.0
3 * Texas Instruments switchdev Driver
5 * Copyright (C) 2019 Texas Instruments
9 #include <linux/etherdevice.h>
10 #include <linux/if_bridge.h>
11 #include <linux/netdevice.h>
12 #include <linux/workqueue.h>
13 #include <net/switchdev.h>
17 #include "cpsw_priv.h"
18 #include "cpsw_switchdev.h"
20 struct cpsw_switchdev_event_work {
21 struct work_struct work;
22 struct switchdev_notifier_fdb_info fdb_info;
23 struct cpsw_priv *priv;
27 static int cpsw_port_stp_state_set(struct cpsw_priv *priv, u8 state)
29 struct cpsw_common *cpsw = priv->cpsw;
34 case BR_STATE_FORWARDING:
35 cpsw_state = ALE_PORT_STATE_FORWARD;
37 case BR_STATE_LEARNING:
38 cpsw_state = ALE_PORT_STATE_LEARN;
40 case BR_STATE_DISABLED:
41 cpsw_state = ALE_PORT_STATE_DISABLE;
43 case BR_STATE_LISTENING:
44 case BR_STATE_BLOCKING:
45 cpsw_state = ALE_PORT_STATE_BLOCK;
51 ret = cpsw_ale_control_set(cpsw->ale, priv->emac_port,
52 ALE_PORT_STATE, cpsw_state);
53 dev_dbg(priv->dev, "ale state: %u\n", cpsw_state);
58 static int cpsw_port_attr_br_flags_set(struct cpsw_priv *priv,
59 struct net_device *orig_dev,
60 struct switchdev_brport_flags flags)
62 struct cpsw_common *cpsw = priv->cpsw;
64 if (flags.mask & BR_MCAST_FLOOD) {
65 bool unreg_mcast_add = false;
67 if (flags.val & BR_MCAST_FLOOD)
68 unreg_mcast_add = true;
70 dev_dbg(priv->dev, "BR_MCAST_FLOOD: %d port %u\n",
71 unreg_mcast_add, priv->emac_port);
73 cpsw_ale_set_unreg_mcast(cpsw->ale, BIT(priv->emac_port),
80 static int cpsw_port_attr_br_flags_pre_set(struct net_device *netdev,
81 struct switchdev_brport_flags flags)
83 if (flags.mask & ~(BR_LEARNING | BR_MCAST_FLOOD))
89 static int cpsw_port_attr_set(struct net_device *ndev, const void *ctx,
90 const struct switchdev_attr *attr,
91 struct netlink_ext_ack *extack)
93 struct cpsw_priv *priv = netdev_priv(ndev);
96 dev_dbg(priv->dev, "attr: id %u port: %u\n", attr->id, priv->emac_port);
99 case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
100 ret = cpsw_port_attr_br_flags_pre_set(ndev,
101 attr->u.brport_flags);
103 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
104 ret = cpsw_port_stp_state_set(priv, attr->u.stp_state);
105 dev_dbg(priv->dev, "stp state: %u\n", attr->u.stp_state);
107 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
108 ret = cpsw_port_attr_br_flags_set(priv, attr->orig_dev,
109 attr->u.brport_flags);
119 static u16 cpsw_get_pvid(struct cpsw_priv *priv)
121 struct cpsw_common *cpsw = priv->cpsw;
122 u32 __iomem *port_vlan_reg;
125 if (priv->emac_port) {
126 int reg = CPSW2_PORT_VLAN;
128 if (cpsw->version == CPSW_VERSION_1)
129 reg = CPSW1_PORT_VLAN;
130 pvid = slave_read(cpsw->slaves + (priv->emac_port - 1), reg);
132 port_vlan_reg = &cpsw->host_port_regs->port_vlan;
133 pvid = readl(port_vlan_reg);
141 static void cpsw_set_pvid(struct cpsw_priv *priv, u16 vid, bool cfi, u32 cos)
143 struct cpsw_common *cpsw = priv->cpsw;
144 void __iomem *port_vlan_reg;
148 pvid |= cfi ? BIT(12) : 0;
149 pvid |= (cos & 0x7) << 13;
151 if (priv->emac_port) {
152 int reg = CPSW2_PORT_VLAN;
154 if (cpsw->version == CPSW_VERSION_1)
155 reg = CPSW1_PORT_VLAN;
157 slave_write(cpsw->slaves + (priv->emac_port - 1), pvid, reg);
160 port_vlan_reg = &cpsw->host_port_regs->port_vlan;
161 writel(pvid, port_vlan_reg);
165 static int cpsw_port_vlan_add(struct cpsw_priv *priv, bool untag, bool pvid,
166 u16 vid, struct net_device *orig_dev)
168 bool cpu_port = netif_is_bridge_master(orig_dev);
169 struct cpsw_common *cpsw = priv->cpsw;
170 int unreg_mcast_mask = 0;
171 int reg_mcast_mask = 0;
178 port_mask = BIT(HOST_PORT_NUM);
179 flags = orig_dev->flags;
180 unreg_mcast_mask = port_mask;
182 port_mask = BIT(priv->emac_port);
183 flags = priv->ndev->flags;
186 if (flags & IFF_MULTICAST)
187 reg_mcast_mask = port_mask;
190 untag_mask = port_mask;
192 ret = cpsw_ale_vlan_add_modify(cpsw->ale, vid, port_mask, untag_mask,
193 reg_mcast_mask, unreg_mcast_mask);
195 dev_err(priv->dev, "Unable to add vlan\n");
200 cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
201 HOST_PORT_NUM, ALE_VLAN, vid);
205 cpsw_set_pvid(priv, vid, 0, 0);
207 dev_dbg(priv->dev, "VID add: %s: vid:%u ports:%X\n",
208 priv->ndev->name, vid, port_mask);
212 static int cpsw_port_vlan_del(struct cpsw_priv *priv, u16 vid,
213 struct net_device *orig_dev)
215 bool cpu_port = netif_is_bridge_master(orig_dev);
216 struct cpsw_common *cpsw = priv->cpsw;
221 port_mask = BIT(HOST_PORT_NUM);
223 port_mask = BIT(priv->emac_port);
225 ret = cpsw_ale_vlan_del_modify(cpsw->ale, vid, port_mask);
229 /* We don't care for the return value here, error is returned only if
230 * the unicast entry is not present
233 cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
234 HOST_PORT_NUM, ALE_VLAN, vid);
236 if (vid == cpsw_get_pvid(priv))
237 cpsw_set_pvid(priv, 0, 0, 0);
239 /* We don't care for the return value here, error is returned only if
240 * the multicast entry is not present
242 cpsw_ale_del_mcast(cpsw->ale, priv->ndev->broadcast,
243 port_mask, ALE_VLAN, vid);
244 dev_dbg(priv->dev, "VID del: %s: vid:%u ports:%X\n",
245 priv->ndev->name, vid, port_mask);
250 static int cpsw_port_vlans_add(struct cpsw_priv *priv,
251 const struct switchdev_obj_port_vlan *vlan)
253 bool untag = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
254 struct net_device *orig_dev = vlan->obj.orig_dev;
255 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
257 dev_dbg(priv->dev, "VID add: %s: vid:%u flags:%X\n",
258 priv->ndev->name, vlan->vid, vlan->flags);
260 return cpsw_port_vlan_add(priv, untag, pvid, vlan->vid, orig_dev);
263 static int cpsw_port_mdb_add(struct cpsw_priv *priv,
264 struct switchdev_obj_port_mdb *mdb)
267 struct net_device *orig_dev = mdb->obj.orig_dev;
268 bool cpu_port = netif_is_bridge_master(orig_dev);
269 struct cpsw_common *cpsw = priv->cpsw;
274 port_mask = BIT(HOST_PORT_NUM);
276 port_mask = BIT(priv->emac_port);
278 err = cpsw_ale_add_mcast(cpsw->ale, mdb->addr, port_mask,
279 ALE_VLAN, mdb->vid, 0);
280 dev_dbg(priv->dev, "MDB add: %s: vid %u:%pM ports: %X\n",
281 priv->ndev->name, mdb->vid, mdb->addr, port_mask);
286 static int cpsw_port_mdb_del(struct cpsw_priv *priv,
287 struct switchdev_obj_port_mdb *mdb)
290 struct net_device *orig_dev = mdb->obj.orig_dev;
291 bool cpu_port = netif_is_bridge_master(orig_dev);
292 struct cpsw_common *cpsw = priv->cpsw;
297 del_mask = BIT(HOST_PORT_NUM);
299 del_mask = BIT(priv->emac_port);
301 err = cpsw_ale_del_mcast(cpsw->ale, mdb->addr, del_mask,
303 dev_dbg(priv->dev, "MDB del: %s: vid %u:%pM ports: %X\n",
304 priv->ndev->name, mdb->vid, mdb->addr, del_mask);
309 static int cpsw_port_obj_add(struct net_device *ndev, const void *ctx,
310 const struct switchdev_obj *obj,
311 struct netlink_ext_ack *extack)
313 struct switchdev_obj_port_vlan *vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
314 struct switchdev_obj_port_mdb *mdb = SWITCHDEV_OBJ_PORT_MDB(obj);
315 struct cpsw_priv *priv = netdev_priv(ndev);
318 dev_dbg(priv->dev, "obj_add: id %u port: %u\n",
319 obj->id, priv->emac_port);
322 case SWITCHDEV_OBJ_ID_PORT_VLAN:
323 err = cpsw_port_vlans_add(priv, vlan);
325 case SWITCHDEV_OBJ_ID_PORT_MDB:
326 case SWITCHDEV_OBJ_ID_HOST_MDB:
327 err = cpsw_port_mdb_add(priv, mdb);
337 static int cpsw_port_obj_del(struct net_device *ndev, const void *ctx,
338 const struct switchdev_obj *obj)
340 struct switchdev_obj_port_vlan *vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
341 struct switchdev_obj_port_mdb *mdb = SWITCHDEV_OBJ_PORT_MDB(obj);
342 struct cpsw_priv *priv = netdev_priv(ndev);
345 dev_dbg(priv->dev, "obj_del: id %u port: %u\n",
346 obj->id, priv->emac_port);
349 case SWITCHDEV_OBJ_ID_PORT_VLAN:
350 err = cpsw_port_vlan_del(priv, vlan->vid, vlan->obj.orig_dev);
352 case SWITCHDEV_OBJ_ID_PORT_MDB:
353 case SWITCHDEV_OBJ_ID_HOST_MDB:
354 err = cpsw_port_mdb_del(priv, mdb);
364 static void cpsw_fdb_offload_notify(struct net_device *ndev,
365 struct switchdev_notifier_fdb_info *rcv)
367 struct switchdev_notifier_fdb_info info = {};
369 info.addr = rcv->addr;
371 info.offloaded = true;
372 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED,
373 ndev, &info.info, NULL);
376 static void cpsw_switchdev_event_work(struct work_struct *work)
378 struct cpsw_switchdev_event_work *switchdev_work =
379 container_of(work, struct cpsw_switchdev_event_work, work);
380 struct cpsw_priv *priv = switchdev_work->priv;
381 struct switchdev_notifier_fdb_info *fdb;
382 struct cpsw_common *cpsw = priv->cpsw;
383 int port = priv->emac_port;
386 switch (switchdev_work->event) {
387 case SWITCHDEV_FDB_ADD_TO_DEVICE:
388 fdb = &switchdev_work->fdb_info;
390 dev_dbg(cpsw->dev, "cpsw_fdb_add: MACID = %pM vid = %u flags = %u %u -- port %d\n",
391 fdb->addr, fdb->vid, fdb->added_by_user,
392 fdb->offloaded, port);
394 if (!fdb->added_by_user || fdb->is_local)
396 if (memcmp(priv->mac_addr, (u8 *)fdb->addr, ETH_ALEN) == 0)
397 port = HOST_PORT_NUM;
399 cpsw_ale_add_ucast(cpsw->ale, (u8 *)fdb->addr, port,
400 fdb->vid ? ALE_VLAN : 0, fdb->vid);
401 cpsw_fdb_offload_notify(priv->ndev, fdb);
403 case SWITCHDEV_FDB_DEL_TO_DEVICE:
404 fdb = &switchdev_work->fdb_info;
406 dev_dbg(cpsw->dev, "cpsw_fdb_del: MACID = %pM vid = %u flags = %u %u -- port %d\n",
407 fdb->addr, fdb->vid, fdb->added_by_user,
408 fdb->offloaded, port);
410 if (!fdb->added_by_user || fdb->is_local)
412 if (memcmp(priv->mac_addr, (u8 *)fdb->addr, ETH_ALEN) == 0)
413 port = HOST_PORT_NUM;
415 cpsw_ale_del_ucast(cpsw->ale, (u8 *)fdb->addr, port,
416 fdb->vid ? ALE_VLAN : 0, fdb->vid);
423 kfree(switchdev_work->fdb_info.addr);
424 kfree(switchdev_work);
428 /* called under rcu_read_lock() */
429 static int cpsw_switchdev_event(struct notifier_block *unused,
430 unsigned long event, void *ptr)
432 struct net_device *ndev = switchdev_notifier_info_to_dev(ptr);
433 struct switchdev_notifier_fdb_info *fdb_info = ptr;
434 struct cpsw_switchdev_event_work *switchdev_work;
435 struct cpsw_priv *priv = netdev_priv(ndev);
438 if (event == SWITCHDEV_PORT_ATTR_SET) {
439 err = switchdev_handle_port_attr_set(ndev, ptr,
442 return notifier_from_errno(err);
445 if (!cpsw_port_dev_check(ndev))
448 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
449 if (WARN_ON(!switchdev_work))
452 INIT_WORK(&switchdev_work->work, cpsw_switchdev_event_work);
453 switchdev_work->priv = priv;
454 switchdev_work->event = event;
457 case SWITCHDEV_FDB_ADD_TO_DEVICE:
458 case SWITCHDEV_FDB_DEL_TO_DEVICE:
459 memcpy(&switchdev_work->fdb_info, ptr,
460 sizeof(switchdev_work->fdb_info));
461 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
462 if (!switchdev_work->fdb_info.addr)
464 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
469 kfree(switchdev_work);
473 queue_work(system_long_wq, &switchdev_work->work);
478 kfree(switchdev_work);
482 static struct notifier_block cpsw_switchdev_notifier = {
483 .notifier_call = cpsw_switchdev_event,
486 static int cpsw_switchdev_blocking_event(struct notifier_block *unused,
487 unsigned long event, void *ptr)
489 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
493 case SWITCHDEV_PORT_OBJ_ADD:
494 err = switchdev_handle_port_obj_add(dev, ptr,
497 return notifier_from_errno(err);
498 case SWITCHDEV_PORT_OBJ_DEL:
499 err = switchdev_handle_port_obj_del(dev, ptr,
502 return notifier_from_errno(err);
503 case SWITCHDEV_PORT_ATTR_SET:
504 err = switchdev_handle_port_attr_set(dev, ptr,
507 return notifier_from_errno(err);
515 static struct notifier_block cpsw_switchdev_bl_notifier = {
516 .notifier_call = cpsw_switchdev_blocking_event,
519 int cpsw_switchdev_register_notifiers(struct cpsw_common *cpsw)
523 ret = register_switchdev_notifier(&cpsw_switchdev_notifier);
525 dev_err(cpsw->dev, "register switchdev notifier fail ret:%d\n",
530 ret = register_switchdev_blocking_notifier(&cpsw_switchdev_bl_notifier);
532 dev_err(cpsw->dev, "register switchdev blocking notifier ret:%d\n",
534 unregister_switchdev_notifier(&cpsw_switchdev_notifier);
540 void cpsw_switchdev_unregister_notifiers(struct cpsw_common *cpsw)
542 unregister_switchdev_blocking_notifier(&cpsw_switchdev_bl_notifier);
543 unregister_switchdev_notifier(&cpsw_switchdev_notifier);