1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Texas Instruments switchdev Driver 4 * 5 * Copyright (C) 2019 Texas Instruments 6 * 7 */ 8 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> 14 15 #include "cpsw.h" 16 #include "cpsw_ale.h" 17 #include "cpsw_priv.h" 18 #include "cpsw_switchdev.h" 19 20 struct cpsw_switchdev_event_work { 21 struct work_struct work; 22 struct switchdev_notifier_fdb_info fdb_info; 23 struct cpsw_priv *priv; 24 unsigned long event; 25 }; 26 27 static int cpsw_port_stp_state_set(struct cpsw_priv *priv, u8 state) 28 { 29 struct cpsw_common *cpsw = priv->cpsw; 30 u8 cpsw_state; 31 int ret = 0; 32 33 switch (state) { 34 case BR_STATE_FORWARDING: 35 cpsw_state = ALE_PORT_STATE_FORWARD; 36 break; 37 case BR_STATE_LEARNING: 38 cpsw_state = ALE_PORT_STATE_LEARN; 39 break; 40 case BR_STATE_DISABLED: 41 cpsw_state = ALE_PORT_STATE_DISABLE; 42 break; 43 case BR_STATE_LISTENING: 44 case BR_STATE_BLOCKING: 45 cpsw_state = ALE_PORT_STATE_BLOCK; 46 break; 47 default: 48 return -EOPNOTSUPP; 49 } 50 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); 54 55 return ret; 56 } 57 58 static int cpsw_port_attr_br_flags_set(struct cpsw_priv *priv, 59 struct net_device *orig_dev, 60 unsigned long brport_flags) 61 { 62 struct cpsw_common *cpsw = priv->cpsw; 63 bool unreg_mcast_add = false; 64 65 if (brport_flags & BR_MCAST_FLOOD) 66 unreg_mcast_add = true; 67 dev_dbg(priv->dev, "BR_MCAST_FLOOD: %d port %u\n", 68 unreg_mcast_add, priv->emac_port); 69 70 cpsw_ale_set_unreg_mcast(cpsw->ale, BIT(priv->emac_port), 71 unreg_mcast_add); 72 73 return 0; 74 } 75 76 static int cpsw_port_attr_br_flags_pre_set(struct net_device *netdev, 77 unsigned long flags) 78 { 79 if (flags & ~(BR_LEARNING | BR_MCAST_FLOOD)) 80 return -EINVAL; 81 82 return 0; 83 } 84 85 static int cpsw_port_attr_set(struct net_device *ndev, 86 const struct switchdev_attr *attr) 87 { 88 struct cpsw_priv *priv = netdev_priv(ndev); 89 int ret; 90 91 dev_dbg(priv->dev, "attr: id %u port: %u\n", attr->id, priv->emac_port); 92 93 switch (attr->id) { 94 case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS: 95 ret = cpsw_port_attr_br_flags_pre_set(ndev, 96 attr->u.brport_flags); 97 break; 98 case SWITCHDEV_ATTR_ID_PORT_STP_STATE: 99 ret = cpsw_port_stp_state_set(priv, attr->u.stp_state); 100 dev_dbg(priv->dev, "stp state: %u\n", attr->u.stp_state); 101 break; 102 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS: 103 ret = cpsw_port_attr_br_flags_set(priv, attr->orig_dev, 104 attr->u.brport_flags); 105 break; 106 default: 107 ret = -EOPNOTSUPP; 108 break; 109 } 110 111 return ret; 112 } 113 114 static u16 cpsw_get_pvid(struct cpsw_priv *priv) 115 { 116 struct cpsw_common *cpsw = priv->cpsw; 117 u32 __iomem *port_vlan_reg; 118 u32 pvid; 119 120 if (priv->emac_port) { 121 int reg = CPSW2_PORT_VLAN; 122 123 if (cpsw->version == CPSW_VERSION_1) 124 reg = CPSW1_PORT_VLAN; 125 pvid = slave_read(cpsw->slaves + (priv->emac_port - 1), reg); 126 } else { 127 port_vlan_reg = &cpsw->host_port_regs->port_vlan; 128 pvid = readl(port_vlan_reg); 129 } 130 131 pvid = pvid & 0xfff; 132 133 return pvid; 134 } 135 136 static void cpsw_set_pvid(struct cpsw_priv *priv, u16 vid, bool cfi, u32 cos) 137 { 138 struct cpsw_common *cpsw = priv->cpsw; 139 void __iomem *port_vlan_reg; 140 u32 pvid; 141 142 pvid = vid; 143 pvid |= cfi ? BIT(12) : 0; 144 pvid |= (cos & 0x7) << 13; 145 146 if (priv->emac_port) { 147 int reg = CPSW2_PORT_VLAN; 148 149 if (cpsw->version == CPSW_VERSION_1) 150 reg = CPSW1_PORT_VLAN; 151 /* no barrier */ 152 slave_write(cpsw->slaves + (priv->emac_port - 1), pvid, reg); 153 } else { 154 /* CPU port */ 155 port_vlan_reg = &cpsw->host_port_regs->port_vlan; 156 writel(pvid, port_vlan_reg); 157 } 158 } 159 160 static int cpsw_port_vlan_add(struct cpsw_priv *priv, bool untag, bool pvid, 161 u16 vid, struct net_device *orig_dev) 162 { 163 bool cpu_port = netif_is_bridge_master(orig_dev); 164 struct cpsw_common *cpsw = priv->cpsw; 165 int unreg_mcast_mask = 0; 166 int reg_mcast_mask = 0; 167 int untag_mask = 0; 168 int port_mask; 169 int ret = 0; 170 u32 flags; 171 172 if (cpu_port) { 173 port_mask = BIT(HOST_PORT_NUM); 174 flags = orig_dev->flags; 175 unreg_mcast_mask = port_mask; 176 } else { 177 port_mask = BIT(priv->emac_port); 178 flags = priv->ndev->flags; 179 } 180 181 if (flags & IFF_MULTICAST) 182 reg_mcast_mask = port_mask; 183 184 if (untag) 185 untag_mask = port_mask; 186 187 ret = cpsw_ale_vlan_add_modify(cpsw->ale, vid, port_mask, untag_mask, 188 reg_mcast_mask, unreg_mcast_mask); 189 if (ret) { 190 dev_err(priv->dev, "Unable to add vlan\n"); 191 return ret; 192 } 193 194 if (cpu_port) 195 cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr, 196 HOST_PORT_NUM, ALE_VLAN, vid); 197 if (!pvid) 198 return ret; 199 200 cpsw_set_pvid(priv, vid, 0, 0); 201 202 dev_dbg(priv->dev, "VID add: %s: vid:%u ports:%X\n", 203 priv->ndev->name, vid, port_mask); 204 return ret; 205 } 206 207 static int cpsw_port_vlan_del(struct cpsw_priv *priv, u16 vid, 208 struct net_device *orig_dev) 209 { 210 bool cpu_port = netif_is_bridge_master(orig_dev); 211 struct cpsw_common *cpsw = priv->cpsw; 212 int port_mask; 213 int ret = 0; 214 215 if (cpu_port) 216 port_mask = BIT(HOST_PORT_NUM); 217 else 218 port_mask = BIT(priv->emac_port); 219 220 ret = cpsw_ale_vlan_del_modify(cpsw->ale, vid, port_mask); 221 if (ret != 0) 222 return ret; 223 224 /* We don't care for the return value here, error is returned only if 225 * the unicast entry is not present 226 */ 227 if (cpu_port) 228 cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, 229 HOST_PORT_NUM, ALE_VLAN, vid); 230 231 if (vid == cpsw_get_pvid(priv)) 232 cpsw_set_pvid(priv, 0, 0, 0); 233 234 /* We don't care for the return value here, error is returned only if 235 * the multicast entry is not present 236 */ 237 cpsw_ale_del_mcast(cpsw->ale, priv->ndev->broadcast, 238 port_mask, ALE_VLAN, vid); 239 dev_dbg(priv->dev, "VID del: %s: vid:%u ports:%X\n", 240 priv->ndev->name, vid, port_mask); 241 242 return ret; 243 } 244 245 static int cpsw_port_vlans_add(struct cpsw_priv *priv, 246 const struct switchdev_obj_port_vlan *vlan) 247 { 248 bool untag = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED; 249 struct net_device *orig_dev = vlan->obj.orig_dev; 250 bool cpu_port = netif_is_bridge_master(orig_dev); 251 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID; 252 253 dev_dbg(priv->dev, "VID add: %s: vid:%u flags:%X\n", 254 priv->ndev->name, vlan->vid, vlan->flags); 255 256 if (cpu_port && !(vlan->flags & BRIDGE_VLAN_INFO_BRENTRY)) 257 return 0; 258 259 return cpsw_port_vlan_add(priv, untag, pvid, vlan->vid, orig_dev); 260 } 261 262 static int cpsw_port_mdb_add(struct cpsw_priv *priv, 263 struct switchdev_obj_port_mdb *mdb) 264 265 { 266 struct net_device *orig_dev = mdb->obj.orig_dev; 267 bool cpu_port = netif_is_bridge_master(orig_dev); 268 struct cpsw_common *cpsw = priv->cpsw; 269 int port_mask; 270 int err; 271 272 if (cpu_port) 273 port_mask = BIT(HOST_PORT_NUM); 274 else 275 port_mask = BIT(priv->emac_port); 276 277 err = cpsw_ale_add_mcast(cpsw->ale, mdb->addr, port_mask, 278 ALE_VLAN, mdb->vid, 0); 279 dev_dbg(priv->dev, "MDB add: %s: vid %u:%pM ports: %X\n", 280 priv->ndev->name, mdb->vid, mdb->addr, port_mask); 281 282 return err; 283 } 284 285 static int cpsw_port_mdb_del(struct cpsw_priv *priv, 286 struct switchdev_obj_port_mdb *mdb) 287 288 { 289 struct net_device *orig_dev = mdb->obj.orig_dev; 290 bool cpu_port = netif_is_bridge_master(orig_dev); 291 struct cpsw_common *cpsw = priv->cpsw; 292 int del_mask; 293 int err; 294 295 if (cpu_port) 296 del_mask = BIT(HOST_PORT_NUM); 297 else 298 del_mask = BIT(priv->emac_port); 299 300 err = cpsw_ale_del_mcast(cpsw->ale, mdb->addr, del_mask, 301 ALE_VLAN, mdb->vid); 302 dev_dbg(priv->dev, "MDB del: %s: vid %u:%pM ports: %X\n", 303 priv->ndev->name, mdb->vid, mdb->addr, del_mask); 304 305 return err; 306 } 307 308 static int cpsw_port_obj_add(struct net_device *ndev, 309 const struct switchdev_obj *obj, 310 struct netlink_ext_ack *extack) 311 { 312 struct switchdev_obj_port_vlan *vlan = SWITCHDEV_OBJ_PORT_VLAN(obj); 313 struct switchdev_obj_port_mdb *mdb = SWITCHDEV_OBJ_PORT_MDB(obj); 314 struct cpsw_priv *priv = netdev_priv(ndev); 315 int err = 0; 316 317 dev_dbg(priv->dev, "obj_add: id %u port: %u\n", 318 obj->id, priv->emac_port); 319 320 switch (obj->id) { 321 case SWITCHDEV_OBJ_ID_PORT_VLAN: 322 err = cpsw_port_vlans_add(priv, vlan); 323 break; 324 case SWITCHDEV_OBJ_ID_PORT_MDB: 325 case SWITCHDEV_OBJ_ID_HOST_MDB: 326 err = cpsw_port_mdb_add(priv, mdb); 327 break; 328 default: 329 err = -EOPNOTSUPP; 330 break; 331 } 332 333 return err; 334 } 335 336 static int cpsw_port_obj_del(struct net_device *ndev, 337 const struct switchdev_obj *obj) 338 { 339 struct switchdev_obj_port_vlan *vlan = SWITCHDEV_OBJ_PORT_VLAN(obj); 340 struct switchdev_obj_port_mdb *mdb = SWITCHDEV_OBJ_PORT_MDB(obj); 341 struct cpsw_priv *priv = netdev_priv(ndev); 342 int err = 0; 343 344 dev_dbg(priv->dev, "obj_del: id %u port: %u\n", 345 obj->id, priv->emac_port); 346 347 switch (obj->id) { 348 case SWITCHDEV_OBJ_ID_PORT_VLAN: 349 err = cpsw_port_vlan_del(priv, vlan->vid, vlan->obj.orig_dev); 350 break; 351 case SWITCHDEV_OBJ_ID_PORT_MDB: 352 case SWITCHDEV_OBJ_ID_HOST_MDB: 353 err = cpsw_port_mdb_del(priv, mdb); 354 break; 355 default: 356 err = -EOPNOTSUPP; 357 break; 358 } 359 360 return err; 361 } 362 363 static void cpsw_fdb_offload_notify(struct net_device *ndev, 364 struct switchdev_notifier_fdb_info *rcv) 365 { 366 struct switchdev_notifier_fdb_info info; 367 368 info.addr = rcv->addr; 369 info.vid = rcv->vid; 370 info.offloaded = true; 371 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, 372 ndev, &info.info, NULL); 373 } 374 375 static void cpsw_switchdev_event_work(struct work_struct *work) 376 { 377 struct cpsw_switchdev_event_work *switchdev_work = 378 container_of(work, struct cpsw_switchdev_event_work, work); 379 struct cpsw_priv *priv = switchdev_work->priv; 380 struct switchdev_notifier_fdb_info *fdb; 381 struct cpsw_common *cpsw = priv->cpsw; 382 int port = priv->emac_port; 383 384 rtnl_lock(); 385 switch (switchdev_work->event) { 386 case SWITCHDEV_FDB_ADD_TO_DEVICE: 387 fdb = &switchdev_work->fdb_info; 388 389 dev_dbg(cpsw->dev, "cpsw_fdb_add: MACID = %pM vid = %u flags = %u %u -- port %d\n", 390 fdb->addr, fdb->vid, fdb->added_by_user, 391 fdb->offloaded, port); 392 393 if (!fdb->added_by_user) 394 break; 395 if (memcmp(priv->mac_addr, (u8 *)fdb->addr, ETH_ALEN) == 0) 396 port = HOST_PORT_NUM; 397 398 cpsw_ale_add_ucast(cpsw->ale, (u8 *)fdb->addr, port, 399 fdb->vid ? ALE_VLAN : 0, fdb->vid); 400 cpsw_fdb_offload_notify(priv->ndev, fdb); 401 break; 402 case SWITCHDEV_FDB_DEL_TO_DEVICE: 403 fdb = &switchdev_work->fdb_info; 404 405 dev_dbg(cpsw->dev, "cpsw_fdb_del: MACID = %pM vid = %u flags = %u %u -- port %d\n", 406 fdb->addr, fdb->vid, fdb->added_by_user, 407 fdb->offloaded, port); 408 409 if (!fdb->added_by_user) 410 break; 411 if (memcmp(priv->mac_addr, (u8 *)fdb->addr, ETH_ALEN) == 0) 412 port = HOST_PORT_NUM; 413 414 cpsw_ale_del_ucast(cpsw->ale, (u8 *)fdb->addr, port, 415 fdb->vid ? ALE_VLAN : 0, fdb->vid); 416 break; 417 default: 418 break; 419 } 420 rtnl_unlock(); 421 422 kfree(switchdev_work->fdb_info.addr); 423 kfree(switchdev_work); 424 dev_put(priv->ndev); 425 } 426 427 /* called under rcu_read_lock() */ 428 static int cpsw_switchdev_event(struct notifier_block *unused, 429 unsigned long event, void *ptr) 430 { 431 struct net_device *ndev = switchdev_notifier_info_to_dev(ptr); 432 struct switchdev_notifier_fdb_info *fdb_info = ptr; 433 struct cpsw_switchdev_event_work *switchdev_work; 434 struct cpsw_priv *priv = netdev_priv(ndev); 435 int err; 436 437 if (event == SWITCHDEV_PORT_ATTR_SET) { 438 err = switchdev_handle_port_attr_set(ndev, ptr, 439 cpsw_port_dev_check, 440 cpsw_port_attr_set); 441 return notifier_from_errno(err); 442 } 443 444 if (!cpsw_port_dev_check(ndev)) 445 return NOTIFY_DONE; 446 447 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC); 448 if (WARN_ON(!switchdev_work)) 449 return NOTIFY_BAD; 450 451 INIT_WORK(&switchdev_work->work, cpsw_switchdev_event_work); 452 switchdev_work->priv = priv; 453 switchdev_work->event = event; 454 455 switch (event) { 456 case SWITCHDEV_FDB_ADD_TO_DEVICE: 457 case SWITCHDEV_FDB_DEL_TO_DEVICE: 458 memcpy(&switchdev_work->fdb_info, ptr, 459 sizeof(switchdev_work->fdb_info)); 460 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC); 461 if (!switchdev_work->fdb_info.addr) 462 goto err_addr_alloc; 463 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr, 464 fdb_info->addr); 465 dev_hold(ndev); 466 break; 467 default: 468 kfree(switchdev_work); 469 return NOTIFY_DONE; 470 } 471 472 queue_work(system_long_wq, &switchdev_work->work); 473 474 return NOTIFY_DONE; 475 476 err_addr_alloc: 477 kfree(switchdev_work); 478 return NOTIFY_BAD; 479 } 480 481 static struct notifier_block cpsw_switchdev_notifier = { 482 .notifier_call = cpsw_switchdev_event, 483 }; 484 485 static int cpsw_switchdev_blocking_event(struct notifier_block *unused, 486 unsigned long event, void *ptr) 487 { 488 struct net_device *dev = switchdev_notifier_info_to_dev(ptr); 489 int err; 490 491 switch (event) { 492 case SWITCHDEV_PORT_OBJ_ADD: 493 err = switchdev_handle_port_obj_add(dev, ptr, 494 cpsw_port_dev_check, 495 cpsw_port_obj_add); 496 return notifier_from_errno(err); 497 case SWITCHDEV_PORT_OBJ_DEL: 498 err = switchdev_handle_port_obj_del(dev, ptr, 499 cpsw_port_dev_check, 500 cpsw_port_obj_del); 501 return notifier_from_errno(err); 502 case SWITCHDEV_PORT_ATTR_SET: 503 err = switchdev_handle_port_attr_set(dev, ptr, 504 cpsw_port_dev_check, 505 cpsw_port_attr_set); 506 return notifier_from_errno(err); 507 default: 508 break; 509 } 510 511 return NOTIFY_DONE; 512 } 513 514 static struct notifier_block cpsw_switchdev_bl_notifier = { 515 .notifier_call = cpsw_switchdev_blocking_event, 516 }; 517 518 int cpsw_switchdev_register_notifiers(struct cpsw_common *cpsw) 519 { 520 int ret = 0; 521 522 ret = register_switchdev_notifier(&cpsw_switchdev_notifier); 523 if (ret) { 524 dev_err(cpsw->dev, "register switchdev notifier fail ret:%d\n", 525 ret); 526 return ret; 527 } 528 529 ret = register_switchdev_blocking_notifier(&cpsw_switchdev_bl_notifier); 530 if (ret) { 531 dev_err(cpsw->dev, "register switchdev blocking notifier ret:%d\n", 532 ret); 533 unregister_switchdev_notifier(&cpsw_switchdev_notifier); 534 } 535 536 return ret; 537 } 538 539 void cpsw_switchdev_unregister_notifiers(struct cpsw_common *cpsw) 540 { 541 unregister_switchdev_blocking_notifier(&cpsw_switchdev_bl_notifier); 542 unregister_switchdev_notifier(&cpsw_switchdev_notifier); 543 } 544