1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * DPAA2 Ethernet Switch driver 4 * 5 * Copyright 2014-2016 Freescale Semiconductor Inc. 6 * Copyright 2017-2021 NXP 7 * 8 */ 9 10 #include <linux/module.h> 11 12 #include <linux/interrupt.h> 13 #include <linux/kthread.h> 14 #include <linux/workqueue.h> 15 #include <linux/iommu.h> 16 #include <net/pkt_cls.h> 17 18 #include <linux/fsl/mc.h> 19 20 #include "dpaa2-switch.h" 21 22 /* Minimal supported DPSW version */ 23 #define DPSW_MIN_VER_MAJOR 8 24 #define DPSW_MIN_VER_MINOR 9 25 26 #define DEFAULT_VLAN_ID 1 27 28 static u16 dpaa2_switch_port_get_fdb_id(struct ethsw_port_priv *port_priv) 29 { 30 return port_priv->fdb->fdb_id; 31 } 32 33 static struct dpaa2_switch_fdb *dpaa2_switch_fdb_get_unused(struct ethsw_core *ethsw) 34 { 35 int i; 36 37 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) 38 if (!ethsw->fdbs[i].in_use) 39 return ðsw->fdbs[i]; 40 return NULL; 41 } 42 43 static struct dpaa2_switch_filter_block * 44 dpaa2_switch_filter_block_get_unused(struct ethsw_core *ethsw) 45 { 46 int i; 47 48 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) 49 if (!ethsw->filter_blocks[i].in_use) 50 return ðsw->filter_blocks[i]; 51 return NULL; 52 } 53 54 static u16 dpaa2_switch_port_set_fdb(struct ethsw_port_priv *port_priv, 55 struct net_device *bridge_dev) 56 { 57 struct ethsw_core *ethsw = port_priv->ethsw_data; 58 struct ethsw_port_priv *other_port_priv = NULL; 59 struct dpaa2_switch_fdb *fdb; 60 struct net_device *other_dev; 61 bool last_fdb_user = true; 62 struct list_head *iter; 63 int i; 64 65 /* If we leave a bridge (bridge_dev is NULL), find an unused 66 * FDB and use that. 67 */ 68 if (!bridge_dev) { 69 /* First verify if this is the last port to leave this bridge */ 70 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) { 71 if (!ethsw->ports[i] || ethsw->ports[i] == port_priv) 72 continue; 73 if (ethsw->ports[i]->fdb == port_priv->fdb) { 74 last_fdb_user = false; 75 break; 76 } 77 } 78 79 /* If this is the last user of the FDB, just keep using it. */ 80 if (last_fdb_user) { 81 port_priv->fdb->bridge_dev = NULL; 82 return 0; 83 } 84 85 /* Since we are not the last port which leaves a bridge, 86 * acquire a new FDB and use it. The number of FDBs is sized to 87 * accommodate all switch ports as standalone, each with its 88 * private FDB, which means that dpaa2_switch_fdb_get_unused() 89 * must succeed here. WARN if not. 90 */ 91 fdb = dpaa2_switch_fdb_get_unused(port_priv->ethsw_data); 92 if (WARN_ON(!fdb)) 93 return 0; 94 95 port_priv->fdb = fdb; 96 port_priv->fdb->in_use = true; 97 port_priv->fdb->bridge_dev = NULL; 98 return 0; 99 } 100 101 /* The below call to netdev_for_each_lower_dev() demands the RTNL lock 102 * being held. Assert on it so that it's easier to catch new code 103 * paths that reach this point without the RTNL lock. 104 */ 105 ASSERT_RTNL(); 106 107 /* If part of a bridge, use the FDB of the first dpaa2 switch interface 108 * to be present in that bridge 109 */ 110 netdev_for_each_lower_dev(bridge_dev, other_dev, iter) { 111 if (!dpaa2_switch_port_dev_check(other_dev)) 112 continue; 113 114 if (other_dev == port_priv->netdev) 115 continue; 116 117 other_port_priv = netdev_priv(other_dev); 118 break; 119 } 120 121 /* The current port is about to change its FDB to the one used by the 122 * first port that joined the bridge. 123 */ 124 if (other_port_priv) { 125 /* The previous FDB is about to become unused, since the 126 * interface is no longer standalone. 127 */ 128 port_priv->fdb->in_use = false; 129 port_priv->fdb->bridge_dev = NULL; 130 131 /* Get a reference to the new FDB */ 132 port_priv->fdb = other_port_priv->fdb; 133 } 134 135 /* Keep track of the new upper bridge device */ 136 port_priv->fdb->bridge_dev = bridge_dev; 137 138 return 0; 139 } 140 141 static void dpaa2_switch_fdb_get_flood_cfg(struct ethsw_core *ethsw, u16 fdb_id, 142 enum dpsw_flood_type type, 143 struct dpsw_egress_flood_cfg *cfg) 144 { 145 int i = 0, j; 146 147 memset(cfg, 0, sizeof(*cfg)); 148 149 /* Add all the DPAA2 switch ports found in the same bridging domain to 150 * the egress flooding domain 151 */ 152 for (j = 0; j < ethsw->sw_attr.num_ifs; j++) { 153 if (!ethsw->ports[j]) 154 continue; 155 if (ethsw->ports[j]->fdb->fdb_id != fdb_id) 156 continue; 157 158 if (type == DPSW_BROADCAST && ethsw->ports[j]->bcast_flood) 159 cfg->if_id[i++] = ethsw->ports[j]->idx; 160 else if (type == DPSW_FLOODING && ethsw->ports[j]->ucast_flood) 161 cfg->if_id[i++] = ethsw->ports[j]->idx; 162 } 163 164 /* Add the CTRL interface to the egress flooding domain */ 165 cfg->if_id[i++] = ethsw->sw_attr.num_ifs; 166 167 cfg->fdb_id = fdb_id; 168 cfg->flood_type = type; 169 cfg->num_ifs = i; 170 } 171 172 static int dpaa2_switch_fdb_set_egress_flood(struct ethsw_core *ethsw, u16 fdb_id) 173 { 174 struct dpsw_egress_flood_cfg flood_cfg; 175 int err; 176 177 /* Setup broadcast flooding domain */ 178 dpaa2_switch_fdb_get_flood_cfg(ethsw, fdb_id, DPSW_BROADCAST, &flood_cfg); 179 err = dpsw_set_egress_flood(ethsw->mc_io, 0, ethsw->dpsw_handle, 180 &flood_cfg); 181 if (err) { 182 dev_err(ethsw->dev, "dpsw_set_egress_flood() = %d\n", err); 183 return err; 184 } 185 186 /* Setup unknown flooding domain */ 187 dpaa2_switch_fdb_get_flood_cfg(ethsw, fdb_id, DPSW_FLOODING, &flood_cfg); 188 err = dpsw_set_egress_flood(ethsw->mc_io, 0, ethsw->dpsw_handle, 189 &flood_cfg); 190 if (err) { 191 dev_err(ethsw->dev, "dpsw_set_egress_flood() = %d\n", err); 192 return err; 193 } 194 195 return 0; 196 } 197 198 static void *dpaa2_iova_to_virt(struct iommu_domain *domain, 199 dma_addr_t iova_addr) 200 { 201 phys_addr_t phys_addr; 202 203 phys_addr = domain ? iommu_iova_to_phys(domain, iova_addr) : iova_addr; 204 205 return phys_to_virt(phys_addr); 206 } 207 208 static int dpaa2_switch_add_vlan(struct ethsw_port_priv *port_priv, u16 vid) 209 { 210 struct ethsw_core *ethsw = port_priv->ethsw_data; 211 struct dpsw_vlan_cfg vcfg = {0}; 212 int err; 213 214 vcfg.fdb_id = dpaa2_switch_port_get_fdb_id(port_priv); 215 err = dpsw_vlan_add(ethsw->mc_io, 0, 216 ethsw->dpsw_handle, vid, &vcfg); 217 if (err) { 218 dev_err(ethsw->dev, "dpsw_vlan_add err %d\n", err); 219 return err; 220 } 221 ethsw->vlans[vid] = ETHSW_VLAN_MEMBER; 222 223 return 0; 224 } 225 226 static bool dpaa2_switch_port_is_up(struct ethsw_port_priv *port_priv) 227 { 228 struct net_device *netdev = port_priv->netdev; 229 struct dpsw_link_state state; 230 int err; 231 232 err = dpsw_if_get_link_state(port_priv->ethsw_data->mc_io, 0, 233 port_priv->ethsw_data->dpsw_handle, 234 port_priv->idx, &state); 235 if (err) { 236 netdev_err(netdev, "dpsw_if_get_link_state() err %d\n", err); 237 return true; 238 } 239 240 WARN_ONCE(state.up > 1, "Garbage read into link_state"); 241 242 return state.up ? true : false; 243 } 244 245 static int dpaa2_switch_port_set_pvid(struct ethsw_port_priv *port_priv, u16 pvid) 246 { 247 struct ethsw_core *ethsw = port_priv->ethsw_data; 248 struct net_device *netdev = port_priv->netdev; 249 struct dpsw_tci_cfg tci_cfg = { 0 }; 250 bool up; 251 int err, ret; 252 253 err = dpsw_if_get_tci(ethsw->mc_io, 0, ethsw->dpsw_handle, 254 port_priv->idx, &tci_cfg); 255 if (err) { 256 netdev_err(netdev, "dpsw_if_get_tci err %d\n", err); 257 return err; 258 } 259 260 tci_cfg.vlan_id = pvid; 261 262 /* Interface needs to be down to change PVID */ 263 up = dpaa2_switch_port_is_up(port_priv); 264 if (up) { 265 err = dpsw_if_disable(ethsw->mc_io, 0, 266 ethsw->dpsw_handle, 267 port_priv->idx); 268 if (err) { 269 netdev_err(netdev, "dpsw_if_disable err %d\n", err); 270 return err; 271 } 272 } 273 274 err = dpsw_if_set_tci(ethsw->mc_io, 0, ethsw->dpsw_handle, 275 port_priv->idx, &tci_cfg); 276 if (err) { 277 netdev_err(netdev, "dpsw_if_set_tci err %d\n", err); 278 goto set_tci_error; 279 } 280 281 /* Delete previous PVID info and mark the new one */ 282 port_priv->vlans[port_priv->pvid] &= ~ETHSW_VLAN_PVID; 283 port_priv->vlans[pvid] |= ETHSW_VLAN_PVID; 284 port_priv->pvid = pvid; 285 286 set_tci_error: 287 if (up) { 288 ret = dpsw_if_enable(ethsw->mc_io, 0, 289 ethsw->dpsw_handle, 290 port_priv->idx); 291 if (ret) { 292 netdev_err(netdev, "dpsw_if_enable err %d\n", ret); 293 return ret; 294 } 295 } 296 297 return err; 298 } 299 300 static int dpaa2_switch_port_add_vlan(struct ethsw_port_priv *port_priv, 301 u16 vid, u16 flags, bool changed) 302 { 303 struct ethsw_core *ethsw = port_priv->ethsw_data; 304 struct net_device *netdev = port_priv->netdev; 305 struct dpsw_vlan_if_cfg vcfg = {0}; 306 int err; 307 308 if (!port_priv->vlans[vid]) { 309 /* If hit, this VLAN rule will lead the packet into the FDB 310 * table specified in the vlan configuration below 311 */ 312 vcfg.num_ifs = 1; 313 vcfg.if_id[0] = port_priv->idx; 314 vcfg.fdb_id = dpaa2_switch_port_get_fdb_id(port_priv); 315 vcfg.options |= DPSW_VLAN_ADD_IF_OPT_FDB_ID; 316 err = dpsw_vlan_add_if(ethsw->mc_io, 0, ethsw->dpsw_handle, 317 vid, &vcfg); 318 if (err) { 319 netdev_err(netdev, "dpsw_vlan_add_if err %d\n", err); 320 return err; 321 } 322 323 port_priv->vlans[vid] = ETHSW_VLAN_MEMBER; 324 } 325 326 memset(&vcfg, 0, sizeof(vcfg)); 327 vcfg.num_ifs = 1; 328 vcfg.if_id[0] = port_priv->idx; 329 330 if (flags & BRIDGE_VLAN_INFO_UNTAGGED) { 331 if (!(port_priv->vlans[vid] & ETHSW_VLAN_UNTAGGED)) { 332 err = dpsw_vlan_add_if_untagged(ethsw->mc_io, 0, 333 ethsw->dpsw_handle, 334 vid, &vcfg); 335 if (err) { 336 netdev_err(netdev, 337 "dpsw_vlan_add_if_untagged err %d\n", 338 err); 339 return err; 340 } 341 port_priv->vlans[vid] |= ETHSW_VLAN_UNTAGGED; 342 } 343 } else if (changed && (port_priv->vlans[vid] & ETHSW_VLAN_UNTAGGED)) { 344 err = dpsw_vlan_remove_if_untagged(ethsw->mc_io, 0, 345 ethsw->dpsw_handle, 346 vid, &vcfg); 347 if (err) { 348 netdev_err(netdev, 349 "dpsw_vlan_remove_if_untagged err %d\n", 350 err); 351 } 352 port_priv->vlans[vid] &= ~ETHSW_VLAN_UNTAGGED; 353 } 354 355 if (flags & BRIDGE_VLAN_INFO_PVID) { 356 err = dpaa2_switch_port_set_pvid(port_priv, vid); 357 if (err) 358 return err; 359 } else if (changed && port_priv->vlans[vid] & ETHSW_VLAN_PVID) { 360 err = dpaa2_switch_port_set_pvid(port_priv, 4095); 361 if (err) 362 return err; 363 } 364 365 return 0; 366 } 367 368 static enum dpsw_stp_state br_stp_state_to_dpsw(u8 state) 369 { 370 switch (state) { 371 case BR_STATE_DISABLED: 372 return DPSW_STP_STATE_DISABLED; 373 case BR_STATE_LISTENING: 374 return DPSW_STP_STATE_LISTENING; 375 case BR_STATE_LEARNING: 376 return DPSW_STP_STATE_LEARNING; 377 case BR_STATE_FORWARDING: 378 return DPSW_STP_STATE_FORWARDING; 379 case BR_STATE_BLOCKING: 380 return DPSW_STP_STATE_BLOCKING; 381 default: 382 return DPSW_STP_STATE_DISABLED; 383 } 384 } 385 386 static int dpaa2_switch_port_set_stp_state(struct ethsw_port_priv *port_priv, u8 state) 387 { 388 struct dpsw_stp_cfg stp_cfg = {0}; 389 int err; 390 u16 vid; 391 392 if (!netif_running(port_priv->netdev) || state == port_priv->stp_state) 393 return 0; /* Nothing to do */ 394 395 stp_cfg.state = br_stp_state_to_dpsw(state); 396 for (vid = 0; vid <= VLAN_VID_MASK; vid++) { 397 if (port_priv->vlans[vid] & ETHSW_VLAN_MEMBER) { 398 stp_cfg.vlan_id = vid; 399 err = dpsw_if_set_stp(port_priv->ethsw_data->mc_io, 0, 400 port_priv->ethsw_data->dpsw_handle, 401 port_priv->idx, &stp_cfg); 402 if (err) { 403 netdev_err(port_priv->netdev, 404 "dpsw_if_set_stp err %d\n", err); 405 return err; 406 } 407 } 408 } 409 410 port_priv->stp_state = state; 411 412 return 0; 413 } 414 415 static int dpaa2_switch_dellink(struct ethsw_core *ethsw, u16 vid) 416 { 417 struct ethsw_port_priv *ppriv_local = NULL; 418 int i, err; 419 420 if (!ethsw->vlans[vid]) 421 return -ENOENT; 422 423 err = dpsw_vlan_remove(ethsw->mc_io, 0, ethsw->dpsw_handle, vid); 424 if (err) { 425 dev_err(ethsw->dev, "dpsw_vlan_remove err %d\n", err); 426 return err; 427 } 428 ethsw->vlans[vid] = 0; 429 430 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) { 431 ppriv_local = ethsw->ports[i]; 432 if (ppriv_local) 433 ppriv_local->vlans[vid] = 0; 434 } 435 436 return 0; 437 } 438 439 static int dpaa2_switch_port_fdb_add_uc(struct ethsw_port_priv *port_priv, 440 const unsigned char *addr) 441 { 442 struct dpsw_fdb_unicast_cfg entry = {0}; 443 u16 fdb_id; 444 int err; 445 446 entry.if_egress = port_priv->idx; 447 entry.type = DPSW_FDB_ENTRY_STATIC; 448 ether_addr_copy(entry.mac_addr, addr); 449 450 fdb_id = dpaa2_switch_port_get_fdb_id(port_priv); 451 err = dpsw_fdb_add_unicast(port_priv->ethsw_data->mc_io, 0, 452 port_priv->ethsw_data->dpsw_handle, 453 fdb_id, &entry); 454 if (err) 455 netdev_err(port_priv->netdev, 456 "dpsw_fdb_add_unicast err %d\n", err); 457 return err; 458 } 459 460 static int dpaa2_switch_port_fdb_del_uc(struct ethsw_port_priv *port_priv, 461 const unsigned char *addr) 462 { 463 struct dpsw_fdb_unicast_cfg entry = {0}; 464 u16 fdb_id; 465 int err; 466 467 entry.if_egress = port_priv->idx; 468 entry.type = DPSW_FDB_ENTRY_STATIC; 469 ether_addr_copy(entry.mac_addr, addr); 470 471 fdb_id = dpaa2_switch_port_get_fdb_id(port_priv); 472 err = dpsw_fdb_remove_unicast(port_priv->ethsw_data->mc_io, 0, 473 port_priv->ethsw_data->dpsw_handle, 474 fdb_id, &entry); 475 /* Silently discard error for calling multiple times the del command */ 476 if (err && err != -ENXIO) 477 netdev_err(port_priv->netdev, 478 "dpsw_fdb_remove_unicast err %d\n", err); 479 return err; 480 } 481 482 static int dpaa2_switch_port_fdb_add_mc(struct ethsw_port_priv *port_priv, 483 const unsigned char *addr) 484 { 485 struct dpsw_fdb_multicast_cfg entry = {0}; 486 u16 fdb_id; 487 int err; 488 489 ether_addr_copy(entry.mac_addr, addr); 490 entry.type = DPSW_FDB_ENTRY_STATIC; 491 entry.num_ifs = 1; 492 entry.if_id[0] = port_priv->idx; 493 494 fdb_id = dpaa2_switch_port_get_fdb_id(port_priv); 495 err = dpsw_fdb_add_multicast(port_priv->ethsw_data->mc_io, 0, 496 port_priv->ethsw_data->dpsw_handle, 497 fdb_id, &entry); 498 /* Silently discard error for calling multiple times the add command */ 499 if (err && err != -ENXIO) 500 netdev_err(port_priv->netdev, "dpsw_fdb_add_multicast err %d\n", 501 err); 502 return err; 503 } 504 505 static int dpaa2_switch_port_fdb_del_mc(struct ethsw_port_priv *port_priv, 506 const unsigned char *addr) 507 { 508 struct dpsw_fdb_multicast_cfg entry = {0}; 509 u16 fdb_id; 510 int err; 511 512 ether_addr_copy(entry.mac_addr, addr); 513 entry.type = DPSW_FDB_ENTRY_STATIC; 514 entry.num_ifs = 1; 515 entry.if_id[0] = port_priv->idx; 516 517 fdb_id = dpaa2_switch_port_get_fdb_id(port_priv); 518 err = dpsw_fdb_remove_multicast(port_priv->ethsw_data->mc_io, 0, 519 port_priv->ethsw_data->dpsw_handle, 520 fdb_id, &entry); 521 /* Silently discard error for calling multiple times the del command */ 522 if (err && err != -ENAVAIL) 523 netdev_err(port_priv->netdev, 524 "dpsw_fdb_remove_multicast err %d\n", err); 525 return err; 526 } 527 528 static void dpaa2_switch_port_get_stats(struct net_device *netdev, 529 struct rtnl_link_stats64 *stats) 530 { 531 struct ethsw_port_priv *port_priv = netdev_priv(netdev); 532 u64 tmp; 533 int err; 534 535 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0, 536 port_priv->ethsw_data->dpsw_handle, 537 port_priv->idx, 538 DPSW_CNT_ING_FRAME, &stats->rx_packets); 539 if (err) 540 goto error; 541 542 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0, 543 port_priv->ethsw_data->dpsw_handle, 544 port_priv->idx, 545 DPSW_CNT_EGR_FRAME, &stats->tx_packets); 546 if (err) 547 goto error; 548 549 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0, 550 port_priv->ethsw_data->dpsw_handle, 551 port_priv->idx, 552 DPSW_CNT_ING_BYTE, &stats->rx_bytes); 553 if (err) 554 goto error; 555 556 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0, 557 port_priv->ethsw_data->dpsw_handle, 558 port_priv->idx, 559 DPSW_CNT_EGR_BYTE, &stats->tx_bytes); 560 if (err) 561 goto error; 562 563 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0, 564 port_priv->ethsw_data->dpsw_handle, 565 port_priv->idx, 566 DPSW_CNT_ING_FRAME_DISCARD, 567 &stats->rx_dropped); 568 if (err) 569 goto error; 570 571 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0, 572 port_priv->ethsw_data->dpsw_handle, 573 port_priv->idx, 574 DPSW_CNT_ING_FLTR_FRAME, 575 &tmp); 576 if (err) 577 goto error; 578 stats->rx_dropped += tmp; 579 580 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0, 581 port_priv->ethsw_data->dpsw_handle, 582 port_priv->idx, 583 DPSW_CNT_EGR_FRAME_DISCARD, 584 &stats->tx_dropped); 585 if (err) 586 goto error; 587 588 return; 589 590 error: 591 netdev_err(netdev, "dpsw_if_get_counter err %d\n", err); 592 } 593 594 static bool dpaa2_switch_port_has_offload_stats(const struct net_device *netdev, 595 int attr_id) 596 { 597 return (attr_id == IFLA_OFFLOAD_XSTATS_CPU_HIT); 598 } 599 600 static int dpaa2_switch_port_get_offload_stats(int attr_id, 601 const struct net_device *netdev, 602 void *sp) 603 { 604 switch (attr_id) { 605 case IFLA_OFFLOAD_XSTATS_CPU_HIT: 606 dpaa2_switch_port_get_stats((struct net_device *)netdev, sp); 607 return 0; 608 } 609 610 return -EINVAL; 611 } 612 613 static int dpaa2_switch_port_change_mtu(struct net_device *netdev, int mtu) 614 { 615 struct ethsw_port_priv *port_priv = netdev_priv(netdev); 616 int err; 617 618 err = dpsw_if_set_max_frame_length(port_priv->ethsw_data->mc_io, 619 0, 620 port_priv->ethsw_data->dpsw_handle, 621 port_priv->idx, 622 (u16)ETHSW_L2_MAX_FRM(mtu)); 623 if (err) { 624 netdev_err(netdev, 625 "dpsw_if_set_max_frame_length() err %d\n", err); 626 return err; 627 } 628 629 WRITE_ONCE(netdev->mtu, mtu); 630 return 0; 631 } 632 633 static int dpaa2_switch_port_link_state_update(struct net_device *netdev) 634 { 635 struct ethsw_port_priv *port_priv = netdev_priv(netdev); 636 struct dpsw_link_state state; 637 int err; 638 639 /* When we manage the MAC/PHY using phylink there is no need 640 * to manually update the netif_carrier. 641 * We can avoid locking because we are called from the "link changed" 642 * IRQ handler, which is the same as the "endpoint changed" IRQ handler 643 * (the writer to port_priv->mac), so we cannot race with it. 644 */ 645 if (dpaa2_mac_is_type_phy(port_priv->mac)) 646 return 0; 647 648 /* Interrupts are received even though no one issued an 'ifconfig up' 649 * on the switch interface. Ignore these link state update interrupts 650 */ 651 if (!netif_running(netdev)) 652 return 0; 653 654 err = dpsw_if_get_link_state(port_priv->ethsw_data->mc_io, 0, 655 port_priv->ethsw_data->dpsw_handle, 656 port_priv->idx, &state); 657 if (err) { 658 netdev_err(netdev, "dpsw_if_get_link_state() err %d\n", err); 659 return err; 660 } 661 662 WARN_ONCE(state.up > 1, "Garbage read into link_state"); 663 664 if (state.up != port_priv->link_state) { 665 if (state.up) { 666 netif_carrier_on(netdev); 667 netif_tx_start_all_queues(netdev); 668 } else { 669 netif_carrier_off(netdev); 670 netif_tx_stop_all_queues(netdev); 671 } 672 port_priv->link_state = state.up; 673 } 674 675 return 0; 676 } 677 678 /* Manage all NAPI instances for the control interface. 679 * 680 * We only have one RX queue and one Tx Conf queue for all 681 * switch ports. Therefore, we only need to enable the NAPI instance once, the 682 * first time one of the switch ports runs .dev_open(). 683 */ 684 685 static void dpaa2_switch_enable_ctrl_if_napi(struct ethsw_core *ethsw) 686 { 687 int i; 688 689 /* Access to the ethsw->napi_users relies on the RTNL lock */ 690 ASSERT_RTNL(); 691 692 /* a new interface is using the NAPI instance */ 693 ethsw->napi_users++; 694 695 /* if there is already a user of the instance, return */ 696 if (ethsw->napi_users > 1) 697 return; 698 699 for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++) 700 napi_enable(ðsw->fq[i].napi); 701 } 702 703 static void dpaa2_switch_disable_ctrl_if_napi(struct ethsw_core *ethsw) 704 { 705 int i; 706 707 /* Access to the ethsw->napi_users relies on the RTNL lock */ 708 ASSERT_RTNL(); 709 710 /* If we are not the last interface using the NAPI, return */ 711 ethsw->napi_users--; 712 if (ethsw->napi_users) 713 return; 714 715 for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++) 716 napi_disable(ðsw->fq[i].napi); 717 } 718 719 static int dpaa2_switch_port_open(struct net_device *netdev) 720 { 721 struct ethsw_port_priv *port_priv = netdev_priv(netdev); 722 struct ethsw_core *ethsw = port_priv->ethsw_data; 723 int err; 724 725 mutex_lock(&port_priv->mac_lock); 726 727 if (!dpaa2_switch_port_is_type_phy(port_priv)) { 728 /* Explicitly set carrier off, otherwise 729 * netif_carrier_ok() will return true and cause 'ip link show' 730 * to report the LOWER_UP flag, even though the link 731 * notification wasn't even received. 732 */ 733 netif_carrier_off(netdev); 734 } 735 736 err = dpsw_if_enable(port_priv->ethsw_data->mc_io, 0, 737 port_priv->ethsw_data->dpsw_handle, 738 port_priv->idx); 739 if (err) { 740 mutex_unlock(&port_priv->mac_lock); 741 netdev_err(netdev, "dpsw_if_enable err %d\n", err); 742 return err; 743 } 744 745 dpaa2_switch_enable_ctrl_if_napi(ethsw); 746 747 if (dpaa2_switch_port_is_type_phy(port_priv)) 748 dpaa2_mac_start(port_priv->mac); 749 750 mutex_unlock(&port_priv->mac_lock); 751 752 return 0; 753 } 754 755 static int dpaa2_switch_port_stop(struct net_device *netdev) 756 { 757 struct ethsw_port_priv *port_priv = netdev_priv(netdev); 758 struct ethsw_core *ethsw = port_priv->ethsw_data; 759 int err; 760 761 mutex_lock(&port_priv->mac_lock); 762 763 if (dpaa2_switch_port_is_type_phy(port_priv)) { 764 dpaa2_mac_stop(port_priv->mac); 765 } else { 766 netif_tx_stop_all_queues(netdev); 767 netif_carrier_off(netdev); 768 } 769 770 mutex_unlock(&port_priv->mac_lock); 771 772 err = dpsw_if_disable(port_priv->ethsw_data->mc_io, 0, 773 port_priv->ethsw_data->dpsw_handle, 774 port_priv->idx); 775 if (err) { 776 netdev_err(netdev, "dpsw_if_disable err %d\n", err); 777 return err; 778 } 779 780 dpaa2_switch_disable_ctrl_if_napi(ethsw); 781 782 return 0; 783 } 784 785 static int dpaa2_switch_port_parent_id(struct net_device *dev, 786 struct netdev_phys_item_id *ppid) 787 { 788 struct ethsw_port_priv *port_priv = netdev_priv(dev); 789 790 ppid->id_len = 1; 791 ppid->id[0] = port_priv->ethsw_data->dev_id; 792 793 return 0; 794 } 795 796 static int dpaa2_switch_port_get_phys_name(struct net_device *netdev, char *name, 797 size_t len) 798 { 799 struct ethsw_port_priv *port_priv = netdev_priv(netdev); 800 int err; 801 802 err = snprintf(name, len, "p%d", port_priv->idx); 803 if (err >= len) 804 return -EINVAL; 805 806 return 0; 807 } 808 809 struct ethsw_dump_ctx { 810 struct net_device *dev; 811 struct sk_buff *skb; 812 struct netlink_callback *cb; 813 int idx; 814 }; 815 816 static int dpaa2_switch_fdb_dump_nl(struct fdb_dump_entry *entry, 817 struct ethsw_dump_ctx *dump) 818 { 819 struct ndo_fdb_dump_context *ctx = (void *)dump->cb->ctx; 820 int is_dynamic = entry->type & DPSW_FDB_ENTRY_DINAMIC; 821 u32 portid = NETLINK_CB(dump->cb->skb).portid; 822 u32 seq = dump->cb->nlh->nlmsg_seq; 823 struct nlmsghdr *nlh; 824 struct ndmsg *ndm; 825 826 if (dump->idx < ctx->fdb_idx) 827 goto skip; 828 829 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH, 830 sizeof(*ndm), NLM_F_MULTI); 831 if (!nlh) 832 return -EMSGSIZE; 833 834 ndm = nlmsg_data(nlh); 835 ndm->ndm_family = AF_BRIDGE; 836 ndm->ndm_pad1 = 0; 837 ndm->ndm_pad2 = 0; 838 ndm->ndm_flags = NTF_SELF; 839 ndm->ndm_type = 0; 840 ndm->ndm_ifindex = dump->dev->ifindex; 841 ndm->ndm_state = is_dynamic ? NUD_REACHABLE : NUD_NOARP; 842 843 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, entry->mac_addr)) 844 goto nla_put_failure; 845 846 nlmsg_end(dump->skb, nlh); 847 848 skip: 849 dump->idx++; 850 return 0; 851 852 nla_put_failure: 853 nlmsg_cancel(dump->skb, nlh); 854 return -EMSGSIZE; 855 } 856 857 static int dpaa2_switch_port_fdb_valid_entry(struct fdb_dump_entry *entry, 858 struct ethsw_port_priv *port_priv) 859 { 860 int idx = port_priv->idx; 861 int valid; 862 863 if (entry->type & DPSW_FDB_ENTRY_TYPE_UNICAST) 864 valid = entry->if_info == port_priv->idx; 865 else 866 valid = entry->if_mask[idx / 8] & BIT(idx % 8); 867 868 return valid; 869 } 870 871 static int dpaa2_switch_fdb_iterate(struct ethsw_port_priv *port_priv, 872 dpaa2_switch_fdb_cb_t cb, void *data) 873 { 874 struct net_device *net_dev = port_priv->netdev; 875 struct ethsw_core *ethsw = port_priv->ethsw_data; 876 struct device *dev = net_dev->dev.parent; 877 struct fdb_dump_entry *fdb_entries; 878 struct fdb_dump_entry fdb_entry; 879 dma_addr_t fdb_dump_iova; 880 u16 num_fdb_entries; 881 u32 fdb_dump_size; 882 int err = 0, i; 883 u8 *dma_mem; 884 u16 fdb_id; 885 886 fdb_dump_size = ethsw->sw_attr.max_fdb_entries * sizeof(fdb_entry); 887 dma_mem = kzalloc(fdb_dump_size, GFP_KERNEL); 888 if (!dma_mem) 889 return -ENOMEM; 890 891 fdb_dump_iova = dma_map_single(dev, dma_mem, fdb_dump_size, 892 DMA_FROM_DEVICE); 893 if (dma_mapping_error(dev, fdb_dump_iova)) { 894 netdev_err(net_dev, "dma_map_single() failed\n"); 895 err = -ENOMEM; 896 goto err_map; 897 } 898 899 fdb_id = dpaa2_switch_port_get_fdb_id(port_priv); 900 err = dpsw_fdb_dump(ethsw->mc_io, 0, ethsw->dpsw_handle, fdb_id, 901 fdb_dump_iova, fdb_dump_size, &num_fdb_entries); 902 if (err) { 903 netdev_err(net_dev, "dpsw_fdb_dump() = %d\n", err); 904 goto err_dump; 905 } 906 907 dma_unmap_single(dev, fdb_dump_iova, fdb_dump_size, DMA_FROM_DEVICE); 908 909 fdb_entries = (struct fdb_dump_entry *)dma_mem; 910 for (i = 0; i < num_fdb_entries; i++) { 911 fdb_entry = fdb_entries[i]; 912 913 err = cb(port_priv, &fdb_entry, data); 914 if (err) 915 goto end; 916 } 917 918 end: 919 kfree(dma_mem); 920 921 return 0; 922 923 err_dump: 924 dma_unmap_single(dev, fdb_dump_iova, fdb_dump_size, DMA_TO_DEVICE); 925 err_map: 926 kfree(dma_mem); 927 return err; 928 } 929 930 static int dpaa2_switch_fdb_entry_dump(struct ethsw_port_priv *port_priv, 931 struct fdb_dump_entry *fdb_entry, 932 void *data) 933 { 934 if (!dpaa2_switch_port_fdb_valid_entry(fdb_entry, port_priv)) 935 return 0; 936 937 return dpaa2_switch_fdb_dump_nl(fdb_entry, data); 938 } 939 940 static int dpaa2_switch_port_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb, 941 struct net_device *net_dev, 942 struct net_device *filter_dev, int *idx) 943 { 944 struct ethsw_port_priv *port_priv = netdev_priv(net_dev); 945 struct ethsw_dump_ctx dump = { 946 .dev = net_dev, 947 .skb = skb, 948 .cb = cb, 949 .idx = *idx, 950 }; 951 int err; 952 953 err = dpaa2_switch_fdb_iterate(port_priv, dpaa2_switch_fdb_entry_dump, &dump); 954 *idx = dump.idx; 955 956 return err; 957 } 958 959 static int dpaa2_switch_fdb_entry_fast_age(struct ethsw_port_priv *port_priv, 960 struct fdb_dump_entry *fdb_entry, 961 void *data __always_unused) 962 { 963 if (!dpaa2_switch_port_fdb_valid_entry(fdb_entry, port_priv)) 964 return 0; 965 966 if (!(fdb_entry->type & DPSW_FDB_ENTRY_TYPE_DYNAMIC)) 967 return 0; 968 969 if (fdb_entry->type & DPSW_FDB_ENTRY_TYPE_UNICAST) 970 dpaa2_switch_port_fdb_del_uc(port_priv, fdb_entry->mac_addr); 971 else 972 dpaa2_switch_port_fdb_del_mc(port_priv, fdb_entry->mac_addr); 973 974 return 0; 975 } 976 977 static void dpaa2_switch_port_fast_age(struct ethsw_port_priv *port_priv) 978 { 979 dpaa2_switch_fdb_iterate(port_priv, 980 dpaa2_switch_fdb_entry_fast_age, NULL); 981 } 982 983 static int dpaa2_switch_port_vlan_add(struct net_device *netdev, __be16 proto, 984 u16 vid) 985 { 986 struct switchdev_obj_port_vlan vlan = { 987 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN, 988 .vid = vid, 989 .obj.orig_dev = netdev, 990 /* This API only allows programming tagged, non-PVID VIDs */ 991 .flags = 0, 992 .changed = false, 993 }; 994 995 return dpaa2_switch_port_vlans_add(netdev, &vlan); 996 } 997 998 static int dpaa2_switch_port_vlan_kill(struct net_device *netdev, __be16 proto, 999 u16 vid) 1000 { 1001 struct switchdev_obj_port_vlan vlan = { 1002 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN, 1003 .vid = vid, 1004 .obj.orig_dev = netdev, 1005 /* This API only allows programming tagged, non-PVID VIDs */ 1006 .flags = 0, 1007 }; 1008 1009 return dpaa2_switch_port_vlans_del(netdev, &vlan); 1010 } 1011 1012 static int dpaa2_switch_port_set_mac_addr(struct ethsw_port_priv *port_priv) 1013 { 1014 struct ethsw_core *ethsw = port_priv->ethsw_data; 1015 struct net_device *net_dev = port_priv->netdev; 1016 struct device *dev = net_dev->dev.parent; 1017 u8 mac_addr[ETH_ALEN]; 1018 int err; 1019 1020 if (!(ethsw->features & ETHSW_FEATURE_MAC_ADDR)) 1021 return 0; 1022 1023 /* Get firmware address, if any */ 1024 err = dpsw_if_get_port_mac_addr(ethsw->mc_io, 0, ethsw->dpsw_handle, 1025 port_priv->idx, mac_addr); 1026 if (err) { 1027 dev_err(dev, "dpsw_if_get_port_mac_addr() failed\n"); 1028 return err; 1029 } 1030 1031 /* First check if firmware has any address configured by bootloader */ 1032 if (!is_zero_ether_addr(mac_addr)) { 1033 eth_hw_addr_set(net_dev, mac_addr); 1034 } else { 1035 /* No MAC address configured, fill in net_dev->dev_addr 1036 * with a random one 1037 */ 1038 eth_hw_addr_random(net_dev); 1039 dev_dbg_once(dev, "device(s) have all-zero hwaddr, replaced with random\n"); 1040 1041 /* Override NET_ADDR_RANDOM set by eth_hw_addr_random(); for all 1042 * practical purposes, this will be our "permanent" mac address, 1043 * at least until the next reboot. This move will also permit 1044 * register_netdevice() to properly fill up net_dev->perm_addr. 1045 */ 1046 net_dev->addr_assign_type = NET_ADDR_PERM; 1047 } 1048 1049 return 0; 1050 } 1051 1052 static void dpaa2_switch_free_fd(const struct ethsw_core *ethsw, 1053 const struct dpaa2_fd *fd) 1054 { 1055 struct device *dev = ethsw->dev; 1056 unsigned char *buffer_start; 1057 struct sk_buff **skbh, *skb; 1058 dma_addr_t fd_addr; 1059 1060 fd_addr = dpaa2_fd_get_addr(fd); 1061 skbh = dpaa2_iova_to_virt(ethsw->iommu_domain, fd_addr); 1062 1063 skb = *skbh; 1064 buffer_start = (unsigned char *)skbh; 1065 1066 dma_unmap_single(dev, fd_addr, 1067 skb_tail_pointer(skb) - buffer_start, 1068 DMA_TO_DEVICE); 1069 1070 /* Move on with skb release */ 1071 dev_kfree_skb(skb); 1072 } 1073 1074 static int dpaa2_switch_build_single_fd(struct ethsw_core *ethsw, 1075 struct sk_buff *skb, 1076 struct dpaa2_fd *fd) 1077 { 1078 struct device *dev = ethsw->dev; 1079 struct sk_buff **skbh; 1080 dma_addr_t addr; 1081 u8 *buff_start; 1082 void *hwa; 1083 1084 buff_start = PTR_ALIGN(skb->data - DPAA2_SWITCH_TX_DATA_OFFSET - 1085 DPAA2_SWITCH_TX_BUF_ALIGN, 1086 DPAA2_SWITCH_TX_BUF_ALIGN); 1087 1088 /* Clear FAS to have consistent values for TX confirmation. It is 1089 * located in the first 8 bytes of the buffer's hardware annotation 1090 * area 1091 */ 1092 hwa = buff_start + DPAA2_SWITCH_SWA_SIZE; 1093 memset(hwa, 0, 8); 1094 1095 /* Store a backpointer to the skb at the beginning of the buffer 1096 * (in the private data area) such that we can release it 1097 * on Tx confirm 1098 */ 1099 skbh = (struct sk_buff **)buff_start; 1100 *skbh = skb; 1101 1102 addr = dma_map_single(dev, buff_start, 1103 skb_tail_pointer(skb) - buff_start, 1104 DMA_TO_DEVICE); 1105 if (unlikely(dma_mapping_error(dev, addr))) 1106 return -ENOMEM; 1107 1108 /* Setup the FD fields */ 1109 memset(fd, 0, sizeof(*fd)); 1110 1111 dpaa2_fd_set_addr(fd, addr); 1112 dpaa2_fd_set_offset(fd, (u16)(skb->data - buff_start)); 1113 dpaa2_fd_set_len(fd, skb->len); 1114 dpaa2_fd_set_format(fd, dpaa2_fd_single); 1115 1116 return 0; 1117 } 1118 1119 static netdev_tx_t dpaa2_switch_port_tx(struct sk_buff *skb, 1120 struct net_device *net_dev) 1121 { 1122 struct ethsw_port_priv *port_priv = netdev_priv(net_dev); 1123 struct ethsw_core *ethsw = port_priv->ethsw_data; 1124 int retries = DPAA2_SWITCH_SWP_BUSY_RETRIES; 1125 struct dpaa2_fd fd; 1126 int err; 1127 1128 if (unlikely(skb_headroom(skb) < DPAA2_SWITCH_NEEDED_HEADROOM)) { 1129 struct sk_buff *ns; 1130 1131 ns = skb_realloc_headroom(skb, DPAA2_SWITCH_NEEDED_HEADROOM); 1132 if (unlikely(!ns)) { 1133 net_err_ratelimited("%s: Error reallocating skb headroom\n", net_dev->name); 1134 goto err_free_skb; 1135 } 1136 dev_consume_skb_any(skb); 1137 skb = ns; 1138 } 1139 1140 /* We'll be holding a back-reference to the skb until Tx confirmation */ 1141 skb = skb_unshare(skb, GFP_ATOMIC); 1142 if (unlikely(!skb)) { 1143 /* skb_unshare() has already freed the skb */ 1144 net_err_ratelimited("%s: Error copying the socket buffer\n", net_dev->name); 1145 goto err_exit; 1146 } 1147 1148 /* At this stage, we do not support non-linear skbs so just try to 1149 * linearize the skb and if that's not working, just drop the packet. 1150 */ 1151 err = skb_linearize(skb); 1152 if (err) { 1153 net_err_ratelimited("%s: skb_linearize error (%d)!\n", net_dev->name, err); 1154 goto err_free_skb; 1155 } 1156 1157 err = dpaa2_switch_build_single_fd(ethsw, skb, &fd); 1158 if (unlikely(err)) { 1159 net_err_ratelimited("%s: ethsw_build_*_fd() %d\n", net_dev->name, err); 1160 goto err_free_skb; 1161 } 1162 1163 do { 1164 err = dpaa2_io_service_enqueue_qd(NULL, 1165 port_priv->tx_qdid, 1166 8, 0, &fd); 1167 retries--; 1168 } while (err == -EBUSY && retries); 1169 1170 if (unlikely(err < 0)) { 1171 dpaa2_switch_free_fd(ethsw, &fd); 1172 goto err_exit; 1173 } 1174 1175 return NETDEV_TX_OK; 1176 1177 err_free_skb: 1178 dev_kfree_skb(skb); 1179 err_exit: 1180 return NETDEV_TX_OK; 1181 } 1182 1183 static int 1184 dpaa2_switch_setup_tc_cls_flower(struct dpaa2_switch_filter_block *filter_block, 1185 struct flow_cls_offload *f) 1186 { 1187 switch (f->command) { 1188 case FLOW_CLS_REPLACE: 1189 return dpaa2_switch_cls_flower_replace(filter_block, f); 1190 case FLOW_CLS_DESTROY: 1191 return dpaa2_switch_cls_flower_destroy(filter_block, f); 1192 default: 1193 return -EOPNOTSUPP; 1194 } 1195 } 1196 1197 static int 1198 dpaa2_switch_setup_tc_cls_matchall(struct dpaa2_switch_filter_block *block, 1199 struct tc_cls_matchall_offload *f) 1200 { 1201 switch (f->command) { 1202 case TC_CLSMATCHALL_REPLACE: 1203 return dpaa2_switch_cls_matchall_replace(block, f); 1204 case TC_CLSMATCHALL_DESTROY: 1205 return dpaa2_switch_cls_matchall_destroy(block, f); 1206 default: 1207 return -EOPNOTSUPP; 1208 } 1209 } 1210 1211 static int dpaa2_switch_port_setup_tc_block_cb_ig(enum tc_setup_type type, 1212 void *type_data, 1213 void *cb_priv) 1214 { 1215 switch (type) { 1216 case TC_SETUP_CLSFLOWER: 1217 return dpaa2_switch_setup_tc_cls_flower(cb_priv, type_data); 1218 case TC_SETUP_CLSMATCHALL: 1219 return dpaa2_switch_setup_tc_cls_matchall(cb_priv, type_data); 1220 default: 1221 return -EOPNOTSUPP; 1222 } 1223 } 1224 1225 static LIST_HEAD(dpaa2_switch_block_cb_list); 1226 1227 static int 1228 dpaa2_switch_port_acl_tbl_bind(struct ethsw_port_priv *port_priv, 1229 struct dpaa2_switch_filter_block *block) 1230 { 1231 struct ethsw_core *ethsw = port_priv->ethsw_data; 1232 struct net_device *netdev = port_priv->netdev; 1233 struct dpsw_acl_if_cfg acl_if_cfg; 1234 int err; 1235 1236 if (port_priv->filter_block) 1237 return -EINVAL; 1238 1239 acl_if_cfg.if_id[0] = port_priv->idx; 1240 acl_if_cfg.num_ifs = 1; 1241 err = dpsw_acl_add_if(ethsw->mc_io, 0, ethsw->dpsw_handle, 1242 block->acl_id, &acl_if_cfg); 1243 if (err) { 1244 netdev_err(netdev, "dpsw_acl_add_if err %d\n", err); 1245 return err; 1246 } 1247 1248 block->ports |= BIT(port_priv->idx); 1249 port_priv->filter_block = block; 1250 1251 return 0; 1252 } 1253 1254 static int 1255 dpaa2_switch_port_acl_tbl_unbind(struct ethsw_port_priv *port_priv, 1256 struct dpaa2_switch_filter_block *block) 1257 { 1258 struct ethsw_core *ethsw = port_priv->ethsw_data; 1259 struct net_device *netdev = port_priv->netdev; 1260 struct dpsw_acl_if_cfg acl_if_cfg; 1261 int err; 1262 1263 if (port_priv->filter_block != block) 1264 return -EINVAL; 1265 1266 acl_if_cfg.if_id[0] = port_priv->idx; 1267 acl_if_cfg.num_ifs = 1; 1268 err = dpsw_acl_remove_if(ethsw->mc_io, 0, ethsw->dpsw_handle, 1269 block->acl_id, &acl_if_cfg); 1270 if (err) { 1271 netdev_err(netdev, "dpsw_acl_add_if err %d\n", err); 1272 return err; 1273 } 1274 1275 block->ports &= ~BIT(port_priv->idx); 1276 port_priv->filter_block = NULL; 1277 return 0; 1278 } 1279 1280 static int dpaa2_switch_port_block_bind(struct ethsw_port_priv *port_priv, 1281 struct dpaa2_switch_filter_block *block) 1282 { 1283 struct dpaa2_switch_filter_block *old_block = port_priv->filter_block; 1284 int err; 1285 1286 /* Offload all the mirror entries found in the block on this new port 1287 * joining it. 1288 */ 1289 err = dpaa2_switch_block_offload_mirror(block, port_priv); 1290 if (err) 1291 return err; 1292 1293 /* If the port is already bound to this ACL table then do nothing. This 1294 * can happen when this port is the first one to join a tc block 1295 */ 1296 if (port_priv->filter_block == block) 1297 return 0; 1298 1299 err = dpaa2_switch_port_acl_tbl_unbind(port_priv, old_block); 1300 if (err) 1301 return err; 1302 1303 /* Mark the previous ACL table as being unused if this was the last 1304 * port that was using it. 1305 */ 1306 if (old_block->ports == 0) 1307 old_block->in_use = false; 1308 1309 return dpaa2_switch_port_acl_tbl_bind(port_priv, block); 1310 } 1311 1312 static int 1313 dpaa2_switch_port_block_unbind(struct ethsw_port_priv *port_priv, 1314 struct dpaa2_switch_filter_block *block) 1315 { 1316 struct ethsw_core *ethsw = port_priv->ethsw_data; 1317 struct dpaa2_switch_filter_block *new_block; 1318 int err; 1319 1320 /* Unoffload all the mirror entries found in the block from the 1321 * port leaving it. 1322 */ 1323 err = dpaa2_switch_block_unoffload_mirror(block, port_priv); 1324 if (err) 1325 return err; 1326 1327 /* We are the last port that leaves a block (an ACL table). 1328 * We'll continue to use this table. 1329 */ 1330 if (block->ports == BIT(port_priv->idx)) 1331 return 0; 1332 1333 err = dpaa2_switch_port_acl_tbl_unbind(port_priv, block); 1334 if (err) 1335 return err; 1336 1337 if (block->ports == 0) 1338 block->in_use = false; 1339 1340 new_block = dpaa2_switch_filter_block_get_unused(ethsw); 1341 new_block->in_use = true; 1342 return dpaa2_switch_port_acl_tbl_bind(port_priv, new_block); 1343 } 1344 1345 static int dpaa2_switch_setup_tc_block_bind(struct net_device *netdev, 1346 struct flow_block_offload *f) 1347 { 1348 struct ethsw_port_priv *port_priv = netdev_priv(netdev); 1349 struct ethsw_core *ethsw = port_priv->ethsw_data; 1350 struct dpaa2_switch_filter_block *filter_block; 1351 struct flow_block_cb *block_cb; 1352 bool register_block = false; 1353 int err; 1354 1355 block_cb = flow_block_cb_lookup(f->block, 1356 dpaa2_switch_port_setup_tc_block_cb_ig, 1357 ethsw); 1358 1359 if (!block_cb) { 1360 /* If the filter block is not already known, then this port 1361 * must be the first to join it. In this case, we can just 1362 * continue to use our private table 1363 */ 1364 filter_block = port_priv->filter_block; 1365 1366 block_cb = flow_block_cb_alloc(dpaa2_switch_port_setup_tc_block_cb_ig, 1367 ethsw, filter_block, NULL); 1368 if (IS_ERR(block_cb)) 1369 return PTR_ERR(block_cb); 1370 1371 register_block = true; 1372 } else { 1373 filter_block = flow_block_cb_priv(block_cb); 1374 } 1375 1376 flow_block_cb_incref(block_cb); 1377 err = dpaa2_switch_port_block_bind(port_priv, filter_block); 1378 if (err) 1379 goto err_block_bind; 1380 1381 if (register_block) { 1382 flow_block_cb_add(block_cb, f); 1383 list_add_tail(&block_cb->driver_list, 1384 &dpaa2_switch_block_cb_list); 1385 } 1386 1387 return 0; 1388 1389 err_block_bind: 1390 if (!flow_block_cb_decref(block_cb)) 1391 flow_block_cb_free(block_cb); 1392 return err; 1393 } 1394 1395 static void dpaa2_switch_setup_tc_block_unbind(struct net_device *netdev, 1396 struct flow_block_offload *f) 1397 { 1398 struct ethsw_port_priv *port_priv = netdev_priv(netdev); 1399 struct ethsw_core *ethsw = port_priv->ethsw_data; 1400 struct dpaa2_switch_filter_block *filter_block; 1401 struct flow_block_cb *block_cb; 1402 int err; 1403 1404 block_cb = flow_block_cb_lookup(f->block, 1405 dpaa2_switch_port_setup_tc_block_cb_ig, 1406 ethsw); 1407 if (!block_cb) 1408 return; 1409 1410 filter_block = flow_block_cb_priv(block_cb); 1411 err = dpaa2_switch_port_block_unbind(port_priv, filter_block); 1412 if (!err && !flow_block_cb_decref(block_cb)) { 1413 flow_block_cb_remove(block_cb, f); 1414 list_del(&block_cb->driver_list); 1415 } 1416 } 1417 1418 static int dpaa2_switch_setup_tc_block(struct net_device *netdev, 1419 struct flow_block_offload *f) 1420 { 1421 if (f->binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS) 1422 return -EOPNOTSUPP; 1423 1424 f->driver_block_list = &dpaa2_switch_block_cb_list; 1425 1426 switch (f->command) { 1427 case FLOW_BLOCK_BIND: 1428 return dpaa2_switch_setup_tc_block_bind(netdev, f); 1429 case FLOW_BLOCK_UNBIND: 1430 dpaa2_switch_setup_tc_block_unbind(netdev, f); 1431 return 0; 1432 default: 1433 return -EOPNOTSUPP; 1434 } 1435 } 1436 1437 static int dpaa2_switch_port_setup_tc(struct net_device *netdev, 1438 enum tc_setup_type type, 1439 void *type_data) 1440 { 1441 switch (type) { 1442 case TC_SETUP_BLOCK: { 1443 return dpaa2_switch_setup_tc_block(netdev, type_data); 1444 } 1445 default: 1446 return -EOPNOTSUPP; 1447 } 1448 1449 return 0; 1450 } 1451 1452 static const struct net_device_ops dpaa2_switch_port_ops = { 1453 .ndo_open = dpaa2_switch_port_open, 1454 .ndo_stop = dpaa2_switch_port_stop, 1455 1456 .ndo_set_mac_address = eth_mac_addr, 1457 .ndo_get_stats64 = dpaa2_switch_port_get_stats, 1458 .ndo_change_mtu = dpaa2_switch_port_change_mtu, 1459 .ndo_has_offload_stats = dpaa2_switch_port_has_offload_stats, 1460 .ndo_get_offload_stats = dpaa2_switch_port_get_offload_stats, 1461 .ndo_fdb_dump = dpaa2_switch_port_fdb_dump, 1462 .ndo_vlan_rx_add_vid = dpaa2_switch_port_vlan_add, 1463 .ndo_vlan_rx_kill_vid = dpaa2_switch_port_vlan_kill, 1464 1465 .ndo_start_xmit = dpaa2_switch_port_tx, 1466 .ndo_get_port_parent_id = dpaa2_switch_port_parent_id, 1467 .ndo_get_phys_port_name = dpaa2_switch_port_get_phys_name, 1468 .ndo_setup_tc = dpaa2_switch_port_setup_tc, 1469 }; 1470 1471 bool dpaa2_switch_port_dev_check(const struct net_device *netdev) 1472 { 1473 return netdev->netdev_ops == &dpaa2_switch_port_ops; 1474 } 1475 1476 static int dpaa2_switch_port_connect_mac(struct ethsw_port_priv *port_priv) 1477 { 1478 struct fsl_mc_device *dpsw_port_dev, *dpmac_dev; 1479 struct dpaa2_mac *mac; 1480 int err; 1481 1482 dpsw_port_dev = to_fsl_mc_device(port_priv->netdev->dev.parent); 1483 dpmac_dev = fsl_mc_get_endpoint(dpsw_port_dev, port_priv->idx); 1484 1485 if (PTR_ERR(dpmac_dev) == -EPROBE_DEFER) 1486 return PTR_ERR(dpmac_dev); 1487 1488 if (IS_ERR(dpmac_dev)) 1489 return 0; 1490 1491 if (dpmac_dev->dev.type != &fsl_mc_bus_dpmac_type) { 1492 err = 0; 1493 goto out_put_device; 1494 } 1495 1496 mac = kzalloc_obj(*mac); 1497 if (!mac) { 1498 err = -ENOMEM; 1499 goto out_put_device; 1500 } 1501 1502 mac->mc_dev = dpmac_dev; 1503 mac->mc_io = port_priv->ethsw_data->mc_io; 1504 mac->net_dev = port_priv->netdev; 1505 1506 err = dpaa2_mac_open(mac); 1507 if (err) 1508 goto err_free_mac; 1509 1510 if (dpaa2_mac_is_type_phy(mac)) { 1511 err = dpaa2_mac_connect(mac); 1512 if (err) { 1513 netdev_err(port_priv->netdev, 1514 "Error connecting to the MAC endpoint %pe\n", 1515 ERR_PTR(err)); 1516 goto err_close_mac; 1517 } 1518 } 1519 1520 mutex_lock(&port_priv->mac_lock); 1521 port_priv->mac = mac; 1522 mutex_unlock(&port_priv->mac_lock); 1523 1524 return 0; 1525 1526 err_close_mac: 1527 dpaa2_mac_close(mac); 1528 err_free_mac: 1529 kfree(mac); 1530 out_put_device: 1531 put_device(&dpmac_dev->dev); 1532 return err; 1533 } 1534 1535 static void dpaa2_switch_port_disconnect_mac(struct ethsw_port_priv *port_priv) 1536 { 1537 struct dpaa2_mac *mac; 1538 1539 mutex_lock(&port_priv->mac_lock); 1540 mac = port_priv->mac; 1541 port_priv->mac = NULL; 1542 mutex_unlock(&port_priv->mac_lock); 1543 1544 if (!mac) 1545 return; 1546 1547 if (dpaa2_mac_is_type_phy(mac)) 1548 dpaa2_mac_disconnect(mac); 1549 1550 dpaa2_mac_close(mac); 1551 kfree(mac); 1552 } 1553 1554 static irqreturn_t dpaa2_switch_irq0_handler_thread(int irq_num, void *arg) 1555 { 1556 struct device *dev = (struct device *)arg; 1557 struct ethsw_core *ethsw = dev_get_drvdata(dev); 1558 struct ethsw_port_priv *port_priv; 1559 int err, if_id; 1560 bool had_mac; 1561 u32 status; 1562 1563 err = dpsw_get_irq_status(ethsw->mc_io, 0, ethsw->dpsw_handle, 1564 DPSW_IRQ_INDEX_IF, &status); 1565 if (err) { 1566 dev_err(dev, "Can't get irq status (err %d)\n", err); 1567 goto out; 1568 } 1569 1570 if_id = (status & 0xFFFF0000) >> 16; 1571 if (if_id >= ethsw->sw_attr.num_ifs) { 1572 dev_err(dev, "Invalid if_id %d in IRQ status\n", if_id); 1573 goto out_clear; 1574 } 1575 port_priv = ethsw->ports[if_id]; 1576 1577 if (status & DPSW_IRQ_EVENT_LINK_CHANGED) 1578 dpaa2_switch_port_link_state_update(port_priv->netdev); 1579 1580 if (status & DPSW_IRQ_EVENT_ENDPOINT_CHANGED) { 1581 dpaa2_switch_port_set_mac_addr(port_priv); 1582 /* We can avoid locking because the "endpoint changed" IRQ 1583 * handler is the only one who changes priv->mac at runtime, 1584 * so we are not racing with anyone. 1585 */ 1586 had_mac = !!port_priv->mac; 1587 if (had_mac) 1588 dpaa2_switch_port_disconnect_mac(port_priv); 1589 else 1590 dpaa2_switch_port_connect_mac(port_priv); 1591 } 1592 1593 out_clear: 1594 err = dpsw_clear_irq_status(ethsw->mc_io, 0, ethsw->dpsw_handle, 1595 DPSW_IRQ_INDEX_IF, status); 1596 if (err) 1597 dev_err(dev, "Can't clear irq status (err %d)\n", err); 1598 1599 out: 1600 return IRQ_HANDLED; 1601 } 1602 1603 static int dpaa2_switch_setup_irqs(struct fsl_mc_device *sw_dev) 1604 { 1605 u32 mask = DPSW_IRQ_EVENT_LINK_CHANGED | DPSW_IRQ_EVENT_ENDPOINT_CHANGED; 1606 struct device *dev = &sw_dev->dev; 1607 struct ethsw_core *ethsw = dev_get_drvdata(dev); 1608 struct fsl_mc_device_irq *irq; 1609 int err; 1610 1611 err = fsl_mc_allocate_irqs(sw_dev); 1612 if (err) { 1613 dev_err(dev, "MC irqs allocation failed\n"); 1614 return err; 1615 } 1616 1617 if (WARN_ON(sw_dev->obj_desc.irq_count != DPSW_IRQ_NUM)) { 1618 err = -EINVAL; 1619 goto free_irq; 1620 } 1621 1622 err = dpsw_set_irq_enable(ethsw->mc_io, 0, ethsw->dpsw_handle, 1623 DPSW_IRQ_INDEX_IF, 0); 1624 if (err) { 1625 dev_err(dev, "dpsw_set_irq_enable err %d\n", err); 1626 goto free_irq; 1627 } 1628 1629 irq = sw_dev->irqs[DPSW_IRQ_INDEX_IF]; 1630 1631 err = devm_request_threaded_irq(dev, irq->virq, NULL, 1632 dpaa2_switch_irq0_handler_thread, 1633 IRQF_NO_SUSPEND | IRQF_ONESHOT, 1634 dev_name(dev), dev); 1635 if (err) { 1636 dev_err(dev, "devm_request_threaded_irq(): %d\n", err); 1637 goto free_irq; 1638 } 1639 1640 err = dpsw_set_irq_mask(ethsw->mc_io, 0, ethsw->dpsw_handle, 1641 DPSW_IRQ_INDEX_IF, mask); 1642 if (err) { 1643 dev_err(dev, "dpsw_set_irq_mask(): %d\n", err); 1644 goto free_devm_irq; 1645 } 1646 1647 err = dpsw_set_irq_enable(ethsw->mc_io, 0, ethsw->dpsw_handle, 1648 DPSW_IRQ_INDEX_IF, 1); 1649 if (err) { 1650 dev_err(dev, "dpsw_set_irq_enable(): %d\n", err); 1651 goto free_devm_irq; 1652 } 1653 1654 return 0; 1655 1656 free_devm_irq: 1657 devm_free_irq(dev, irq->virq, dev); 1658 free_irq: 1659 fsl_mc_free_irqs(sw_dev); 1660 return err; 1661 } 1662 1663 static void dpaa2_switch_teardown_irqs(struct fsl_mc_device *sw_dev) 1664 { 1665 struct device *dev = &sw_dev->dev; 1666 struct ethsw_core *ethsw = dev_get_drvdata(dev); 1667 int err; 1668 1669 err = dpsw_set_irq_enable(ethsw->mc_io, 0, ethsw->dpsw_handle, 1670 DPSW_IRQ_INDEX_IF, 0); 1671 if (err) 1672 dev_err(dev, "dpsw_set_irq_enable err %d\n", err); 1673 1674 fsl_mc_free_irqs(sw_dev); 1675 } 1676 1677 static int dpaa2_switch_port_set_learning(struct ethsw_port_priv *port_priv, bool enable) 1678 { 1679 struct ethsw_core *ethsw = port_priv->ethsw_data; 1680 enum dpsw_learning_mode learn_mode; 1681 int err; 1682 1683 if (enable) 1684 learn_mode = DPSW_LEARNING_MODE_HW; 1685 else 1686 learn_mode = DPSW_LEARNING_MODE_DIS; 1687 1688 err = dpsw_if_set_learning_mode(ethsw->mc_io, 0, ethsw->dpsw_handle, 1689 port_priv->idx, learn_mode); 1690 if (err) 1691 netdev_err(port_priv->netdev, "dpsw_if_set_learning_mode err %d\n", err); 1692 1693 if (!enable) 1694 dpaa2_switch_port_fast_age(port_priv); 1695 1696 return err; 1697 } 1698 1699 static int dpaa2_switch_port_attr_stp_state_set(struct net_device *netdev, 1700 u8 state) 1701 { 1702 struct ethsw_port_priv *port_priv = netdev_priv(netdev); 1703 int err; 1704 1705 err = dpaa2_switch_port_set_stp_state(port_priv, state); 1706 if (err) 1707 return err; 1708 1709 switch (state) { 1710 case BR_STATE_DISABLED: 1711 case BR_STATE_BLOCKING: 1712 case BR_STATE_LISTENING: 1713 err = dpaa2_switch_port_set_learning(port_priv, false); 1714 break; 1715 case BR_STATE_LEARNING: 1716 case BR_STATE_FORWARDING: 1717 err = dpaa2_switch_port_set_learning(port_priv, 1718 port_priv->learn_ena); 1719 break; 1720 } 1721 1722 return err; 1723 } 1724 1725 static int dpaa2_switch_port_flood(struct ethsw_port_priv *port_priv, 1726 struct switchdev_brport_flags flags) 1727 { 1728 struct ethsw_core *ethsw = port_priv->ethsw_data; 1729 1730 if (flags.mask & BR_BCAST_FLOOD) 1731 port_priv->bcast_flood = !!(flags.val & BR_BCAST_FLOOD); 1732 1733 if (flags.mask & BR_FLOOD) 1734 port_priv->ucast_flood = !!(flags.val & BR_FLOOD); 1735 1736 return dpaa2_switch_fdb_set_egress_flood(ethsw, port_priv->fdb->fdb_id); 1737 } 1738 1739 static int dpaa2_switch_port_pre_bridge_flags(struct net_device *netdev, 1740 struct switchdev_brport_flags flags, 1741 struct netlink_ext_ack *extack) 1742 { 1743 if (flags.mask & ~(BR_LEARNING | BR_BCAST_FLOOD | BR_FLOOD | 1744 BR_MCAST_FLOOD)) 1745 return -EINVAL; 1746 1747 if (flags.mask & (BR_FLOOD | BR_MCAST_FLOOD)) { 1748 bool multicast = !!(flags.val & BR_MCAST_FLOOD); 1749 bool unicast = !!(flags.val & BR_FLOOD); 1750 1751 if (unicast != multicast) { 1752 NL_SET_ERR_MSG_MOD(extack, 1753 "Cannot configure multicast flooding independently of unicast"); 1754 return -EINVAL; 1755 } 1756 } 1757 1758 return 0; 1759 } 1760 1761 static int dpaa2_switch_port_bridge_flags(struct net_device *netdev, 1762 struct switchdev_brport_flags flags, 1763 struct netlink_ext_ack *extack) 1764 { 1765 struct ethsw_port_priv *port_priv = netdev_priv(netdev); 1766 int err; 1767 1768 if (flags.mask & BR_LEARNING) { 1769 bool learn_ena = !!(flags.val & BR_LEARNING); 1770 1771 err = dpaa2_switch_port_set_learning(port_priv, learn_ena); 1772 if (err) 1773 return err; 1774 port_priv->learn_ena = learn_ena; 1775 } 1776 1777 if (flags.mask & (BR_BCAST_FLOOD | BR_FLOOD | BR_MCAST_FLOOD)) { 1778 err = dpaa2_switch_port_flood(port_priv, flags); 1779 if (err) 1780 return err; 1781 } 1782 1783 return 0; 1784 } 1785 1786 static int dpaa2_switch_port_attr_set(struct net_device *netdev, const void *ctx, 1787 const struct switchdev_attr *attr, 1788 struct netlink_ext_ack *extack) 1789 { 1790 int err = 0; 1791 1792 switch (attr->id) { 1793 case SWITCHDEV_ATTR_ID_PORT_STP_STATE: 1794 err = dpaa2_switch_port_attr_stp_state_set(netdev, 1795 attr->u.stp_state); 1796 break; 1797 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING: 1798 if (!attr->u.vlan_filtering) { 1799 NL_SET_ERR_MSG_MOD(extack, 1800 "The DPAA2 switch does not support VLAN-unaware operation"); 1801 return -EOPNOTSUPP; 1802 } 1803 break; 1804 case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS: 1805 err = dpaa2_switch_port_pre_bridge_flags(netdev, attr->u.brport_flags, extack); 1806 break; 1807 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS: 1808 err = dpaa2_switch_port_bridge_flags(netdev, attr->u.brport_flags, extack); 1809 break; 1810 default: 1811 err = -EOPNOTSUPP; 1812 break; 1813 } 1814 1815 return err; 1816 } 1817 1818 int dpaa2_switch_port_vlans_add(struct net_device *netdev, 1819 const struct switchdev_obj_port_vlan *vlan) 1820 { 1821 struct ethsw_port_priv *port_priv = netdev_priv(netdev); 1822 struct ethsw_core *ethsw = port_priv->ethsw_data; 1823 struct dpsw_attr *attr = ðsw->sw_attr; 1824 int err = 0; 1825 1826 if (!port_priv->ethsw_data->vlans[vlan->vid]) { 1827 /* Only check for space in case this is a new VLAN from the 1828 * DPSW perspective 1829 */ 1830 err = dpsw_get_attributes(ethsw->mc_io, 0, ethsw->dpsw_handle, 1831 ðsw->sw_attr); 1832 if (err) { 1833 netdev_err(netdev, "dpsw_get_attributes err %d\n", err); 1834 return err; 1835 } 1836 if (attr->max_vlans - attr->num_vlans < 1) 1837 return -ENOSPC; 1838 1839 /* this is a new VLAN */ 1840 err = dpaa2_switch_add_vlan(port_priv, vlan->vid); 1841 if (err) 1842 return err; 1843 1844 port_priv->ethsw_data->vlans[vlan->vid] |= ETHSW_VLAN_GLOBAL; 1845 } 1846 1847 return dpaa2_switch_port_add_vlan(port_priv, vlan->vid, vlan->flags, 1848 vlan->changed); 1849 } 1850 1851 static int dpaa2_switch_port_lookup_address(struct net_device *netdev, int is_uc, 1852 const unsigned char *addr) 1853 { 1854 struct netdev_hw_addr_list *list = (is_uc) ? &netdev->uc : &netdev->mc; 1855 struct netdev_hw_addr *ha; 1856 1857 netif_addr_lock_bh(netdev); 1858 list_for_each_entry(ha, &list->list, list) { 1859 if (ether_addr_equal(ha->addr, addr)) { 1860 netif_addr_unlock_bh(netdev); 1861 return 1; 1862 } 1863 } 1864 netif_addr_unlock_bh(netdev); 1865 return 0; 1866 } 1867 1868 static int dpaa2_switch_port_mdb_add(struct net_device *netdev, 1869 const struct switchdev_obj_port_mdb *mdb) 1870 { 1871 struct ethsw_port_priv *port_priv = netdev_priv(netdev); 1872 int err; 1873 1874 /* Check if address is already set on this port */ 1875 if (dpaa2_switch_port_lookup_address(netdev, 0, mdb->addr)) 1876 return -EEXIST; 1877 1878 err = dpaa2_switch_port_fdb_add_mc(port_priv, mdb->addr); 1879 if (err) 1880 return err; 1881 1882 err = dev_mc_add(netdev, mdb->addr); 1883 if (err) { 1884 netdev_err(netdev, "dev_mc_add err %d\n", err); 1885 dpaa2_switch_port_fdb_del_mc(port_priv, mdb->addr); 1886 } 1887 1888 return err; 1889 } 1890 1891 static int dpaa2_switch_port_obj_add(struct net_device *netdev, 1892 const struct switchdev_obj *obj) 1893 { 1894 int err; 1895 1896 switch (obj->id) { 1897 case SWITCHDEV_OBJ_ID_PORT_VLAN: 1898 err = dpaa2_switch_port_vlans_add(netdev, 1899 SWITCHDEV_OBJ_PORT_VLAN(obj)); 1900 break; 1901 case SWITCHDEV_OBJ_ID_PORT_MDB: 1902 err = dpaa2_switch_port_mdb_add(netdev, 1903 SWITCHDEV_OBJ_PORT_MDB(obj)); 1904 break; 1905 default: 1906 err = -EOPNOTSUPP; 1907 break; 1908 } 1909 1910 return err; 1911 } 1912 1913 static int dpaa2_switch_port_del_vlan(struct ethsw_port_priv *port_priv, u16 vid) 1914 { 1915 struct ethsw_core *ethsw = port_priv->ethsw_data; 1916 struct net_device *netdev = port_priv->netdev; 1917 struct dpsw_vlan_if_cfg vcfg; 1918 int i, err; 1919 1920 if (!port_priv->vlans[vid]) 1921 return -ENOENT; 1922 1923 if (port_priv->vlans[vid] & ETHSW_VLAN_PVID) { 1924 /* If we are deleting the PVID of a port, use VLAN 4095 instead 1925 * as we are sure that neither the bridge nor the 8021q module 1926 * will use it 1927 */ 1928 err = dpaa2_switch_port_set_pvid(port_priv, 4095); 1929 if (err) 1930 return err; 1931 } 1932 1933 vcfg.num_ifs = 1; 1934 vcfg.if_id[0] = port_priv->idx; 1935 if (port_priv->vlans[vid] & ETHSW_VLAN_UNTAGGED) { 1936 err = dpsw_vlan_remove_if_untagged(ethsw->mc_io, 0, 1937 ethsw->dpsw_handle, 1938 vid, &vcfg); 1939 if (err) { 1940 netdev_err(netdev, 1941 "dpsw_vlan_remove_if_untagged err %d\n", 1942 err); 1943 } 1944 port_priv->vlans[vid] &= ~ETHSW_VLAN_UNTAGGED; 1945 } 1946 1947 if (port_priv->vlans[vid] & ETHSW_VLAN_MEMBER) { 1948 err = dpsw_vlan_remove_if(ethsw->mc_io, 0, ethsw->dpsw_handle, 1949 vid, &vcfg); 1950 if (err) { 1951 netdev_err(netdev, 1952 "dpsw_vlan_remove_if err %d\n", err); 1953 return err; 1954 } 1955 port_priv->vlans[vid] &= ~ETHSW_VLAN_MEMBER; 1956 1957 /* Delete VLAN from switch if it is no longer configured on 1958 * any port 1959 */ 1960 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) { 1961 if (ethsw->ports[i] && 1962 ethsw->ports[i]->vlans[vid] & ETHSW_VLAN_MEMBER) 1963 return 0; /* Found a port member in VID */ 1964 } 1965 1966 ethsw->vlans[vid] &= ~ETHSW_VLAN_GLOBAL; 1967 1968 err = dpaa2_switch_dellink(ethsw, vid); 1969 if (err) 1970 return err; 1971 } 1972 1973 return 0; 1974 } 1975 1976 int dpaa2_switch_port_vlans_del(struct net_device *netdev, 1977 const struct switchdev_obj_port_vlan *vlan) 1978 { 1979 struct ethsw_port_priv *port_priv = netdev_priv(netdev); 1980 1981 if (netif_is_bridge_master(vlan->obj.orig_dev)) 1982 return -EOPNOTSUPP; 1983 1984 return dpaa2_switch_port_del_vlan(port_priv, vlan->vid); 1985 } 1986 1987 static int dpaa2_switch_port_mdb_del(struct net_device *netdev, 1988 const struct switchdev_obj_port_mdb *mdb) 1989 { 1990 struct ethsw_port_priv *port_priv = netdev_priv(netdev); 1991 int err; 1992 1993 if (!dpaa2_switch_port_lookup_address(netdev, 0, mdb->addr)) 1994 return -ENOENT; 1995 1996 err = dpaa2_switch_port_fdb_del_mc(port_priv, mdb->addr); 1997 if (err) 1998 return err; 1999 2000 err = dev_mc_del(netdev, mdb->addr); 2001 if (err) { 2002 netdev_err(netdev, "dev_mc_del err %d\n", err); 2003 return err; 2004 } 2005 2006 return err; 2007 } 2008 2009 static int dpaa2_switch_port_obj_del(struct net_device *netdev, 2010 const struct switchdev_obj *obj) 2011 { 2012 int err; 2013 2014 switch (obj->id) { 2015 case SWITCHDEV_OBJ_ID_PORT_VLAN: 2016 err = dpaa2_switch_port_vlans_del(netdev, SWITCHDEV_OBJ_PORT_VLAN(obj)); 2017 break; 2018 case SWITCHDEV_OBJ_ID_PORT_MDB: 2019 err = dpaa2_switch_port_mdb_del(netdev, SWITCHDEV_OBJ_PORT_MDB(obj)); 2020 break; 2021 default: 2022 err = -EOPNOTSUPP; 2023 break; 2024 } 2025 return err; 2026 } 2027 2028 static int dpaa2_switch_port_attr_set_event(struct net_device *netdev, 2029 struct switchdev_notifier_port_attr_info *ptr) 2030 { 2031 int err; 2032 2033 err = switchdev_handle_port_attr_set(netdev, ptr, 2034 dpaa2_switch_port_dev_check, 2035 dpaa2_switch_port_attr_set); 2036 return notifier_from_errno(err); 2037 } 2038 2039 static int dpaa2_switch_port_bridge_join(struct net_device *netdev, 2040 struct net_device *upper_dev, 2041 struct netlink_ext_ack *extack) 2042 { 2043 struct ethsw_port_priv *port_priv = netdev_priv(netdev); 2044 struct dpaa2_switch_fdb *old_fdb = port_priv->fdb; 2045 struct ethsw_core *ethsw = port_priv->ethsw_data; 2046 bool learn_ena; 2047 int err; 2048 2049 /* Delete the previously manually installed VLAN 1 */ 2050 err = dpaa2_switch_port_del_vlan(port_priv, 1); 2051 if (err) 2052 return err; 2053 2054 dpaa2_switch_port_set_fdb(port_priv, upper_dev); 2055 2056 /* Inherit the initial bridge port learning state */ 2057 learn_ena = br_port_flag_is_set(netdev, BR_LEARNING); 2058 err = dpaa2_switch_port_set_learning(port_priv, learn_ena); 2059 port_priv->learn_ena = learn_ena; 2060 2061 /* Setup the egress flood policy (broadcast, unknown unicast) */ 2062 err = dpaa2_switch_fdb_set_egress_flood(ethsw, port_priv->fdb->fdb_id); 2063 if (err) 2064 goto err_egress_flood; 2065 2066 /* Recreate the egress flood domain of the FDB that we just left. */ 2067 err = dpaa2_switch_fdb_set_egress_flood(ethsw, old_fdb->fdb_id); 2068 if (err) 2069 goto err_egress_flood; 2070 2071 err = switchdev_bridge_port_offload(netdev, netdev, NULL, 2072 NULL, NULL, false, extack); 2073 if (err) 2074 goto err_switchdev_offload; 2075 2076 return 0; 2077 2078 err_switchdev_offload: 2079 err_egress_flood: 2080 dpaa2_switch_port_set_fdb(port_priv, NULL); 2081 return err; 2082 } 2083 2084 static int dpaa2_switch_port_clear_rxvlan(struct net_device *vdev, int vid, void *arg) 2085 { 2086 __be16 vlan_proto = htons(ETH_P_8021Q); 2087 2088 if (vdev) 2089 vlan_proto = vlan_dev_vlan_proto(vdev); 2090 2091 return dpaa2_switch_port_vlan_kill(arg, vlan_proto, vid); 2092 } 2093 2094 static int dpaa2_switch_port_restore_rxvlan(struct net_device *vdev, int vid, void *arg) 2095 { 2096 __be16 vlan_proto = htons(ETH_P_8021Q); 2097 2098 if (vdev) 2099 vlan_proto = vlan_dev_vlan_proto(vdev); 2100 2101 return dpaa2_switch_port_vlan_add(arg, vlan_proto, vid); 2102 } 2103 2104 static void dpaa2_switch_port_pre_bridge_leave(struct net_device *netdev) 2105 { 2106 switchdev_bridge_port_unoffload(netdev, NULL, NULL, NULL); 2107 } 2108 2109 static int dpaa2_switch_port_bridge_leave(struct net_device *netdev) 2110 { 2111 struct ethsw_port_priv *port_priv = netdev_priv(netdev); 2112 struct dpaa2_switch_fdb *old_fdb = port_priv->fdb; 2113 struct ethsw_core *ethsw = port_priv->ethsw_data; 2114 int err; 2115 2116 /* First of all, fast age any learn FDB addresses on this switch port */ 2117 dpaa2_switch_port_fast_age(port_priv); 2118 2119 /* Clear all RX VLANs installed through vlan_vid_add() either as VLAN 2120 * upper devices or otherwise from the FDB table that we are about to 2121 * leave 2122 */ 2123 err = vlan_for_each(netdev, dpaa2_switch_port_clear_rxvlan, netdev); 2124 if (err) 2125 netdev_err(netdev, "Unable to clear RX VLANs from old FDB table, err (%d)\n", err); 2126 2127 dpaa2_switch_port_set_fdb(port_priv, NULL); 2128 2129 /* Restore all RX VLANs into the new FDB table that we just joined */ 2130 err = vlan_for_each(netdev, dpaa2_switch_port_restore_rxvlan, netdev); 2131 if (err) 2132 netdev_err(netdev, "Unable to restore RX VLANs to the new FDB, err (%d)\n", err); 2133 2134 /* Reset the flooding state to denote that this port can send any 2135 * packet in standalone mode. With this, we are also ensuring that any 2136 * later bridge join will have the flooding flag on. 2137 */ 2138 port_priv->bcast_flood = true; 2139 port_priv->ucast_flood = true; 2140 2141 /* Setup the egress flood policy (broadcast, unknown unicast). 2142 * When the port is not under a bridge, only the CTRL interface is part 2143 * of the flooding domain besides the actual port 2144 */ 2145 err = dpaa2_switch_fdb_set_egress_flood(ethsw, port_priv->fdb->fdb_id); 2146 if (err) 2147 return err; 2148 2149 /* Recreate the egress flood domain of the FDB that we just left */ 2150 err = dpaa2_switch_fdb_set_egress_flood(ethsw, old_fdb->fdb_id); 2151 if (err) 2152 return err; 2153 2154 /* No HW learning when not under a bridge */ 2155 err = dpaa2_switch_port_set_learning(port_priv, false); 2156 if (err) 2157 return err; 2158 port_priv->learn_ena = false; 2159 2160 /* Add the VLAN 1 as PVID when not under a bridge. We need this since 2161 * the dpaa2 switch interfaces are not capable to be VLAN unaware 2162 */ 2163 return dpaa2_switch_port_add_vlan(port_priv, DEFAULT_VLAN_ID, 2164 BRIDGE_VLAN_INFO_UNTAGGED | BRIDGE_VLAN_INFO_PVID, 2165 false); 2166 } 2167 2168 static int dpaa2_switch_prevent_bridging_with_8021q_upper(struct net_device *netdev) 2169 { 2170 struct net_device *upper_dev; 2171 struct list_head *iter; 2172 2173 /* RCU read lock not necessary because we have write-side protection 2174 * (rtnl_mutex), however a non-rcu iterator does not exist. 2175 */ 2176 netdev_for_each_upper_dev_rcu(netdev, upper_dev, iter) 2177 if (is_vlan_dev(upper_dev)) 2178 return -EOPNOTSUPP; 2179 2180 return 0; 2181 } 2182 2183 static int 2184 dpaa2_switch_prechangeupper_sanity_checks(struct net_device *netdev, 2185 struct net_device *upper_dev, 2186 struct netlink_ext_ack *extack) 2187 { 2188 struct ethsw_port_priv *port_priv = netdev_priv(netdev); 2189 struct ethsw_port_priv *other_port_priv; 2190 struct net_device *other_dev; 2191 struct list_head *iter; 2192 int err; 2193 2194 if (!br_vlan_enabled(upper_dev)) { 2195 NL_SET_ERR_MSG_MOD(extack, "Cannot join a VLAN-unaware bridge"); 2196 return -EOPNOTSUPP; 2197 } 2198 2199 err = dpaa2_switch_prevent_bridging_with_8021q_upper(netdev); 2200 if (err) { 2201 NL_SET_ERR_MSG_MOD(extack, 2202 "Cannot join a bridge while VLAN uppers are present"); 2203 return 0; 2204 } 2205 2206 netdev_for_each_lower_dev(upper_dev, other_dev, iter) { 2207 if (!dpaa2_switch_port_dev_check(other_dev)) 2208 continue; 2209 2210 other_port_priv = netdev_priv(other_dev); 2211 if (other_port_priv->ethsw_data != port_priv->ethsw_data) { 2212 NL_SET_ERR_MSG_MOD(extack, 2213 "Interface from a different DPSW is in the bridge already"); 2214 return -EINVAL; 2215 } 2216 } 2217 2218 return 0; 2219 } 2220 2221 static int dpaa2_switch_port_prechangeupper(struct net_device *netdev, 2222 struct netdev_notifier_changeupper_info *info) 2223 { 2224 struct netlink_ext_ack *extack; 2225 struct net_device *upper_dev; 2226 int err; 2227 2228 if (!dpaa2_switch_port_dev_check(netdev)) 2229 return 0; 2230 2231 extack = netdev_notifier_info_to_extack(&info->info); 2232 upper_dev = info->upper_dev; 2233 if (netif_is_bridge_master(upper_dev)) { 2234 err = dpaa2_switch_prechangeupper_sanity_checks(netdev, 2235 upper_dev, 2236 extack); 2237 if (err) 2238 return err; 2239 2240 if (!info->linking) 2241 dpaa2_switch_port_pre_bridge_leave(netdev); 2242 } 2243 2244 return 0; 2245 } 2246 2247 static int dpaa2_switch_port_changeupper(struct net_device *netdev, 2248 struct netdev_notifier_changeupper_info *info) 2249 { 2250 struct netlink_ext_ack *extack; 2251 struct net_device *upper_dev; 2252 2253 if (!dpaa2_switch_port_dev_check(netdev)) 2254 return 0; 2255 2256 extack = netdev_notifier_info_to_extack(&info->info); 2257 2258 upper_dev = info->upper_dev; 2259 if (netif_is_bridge_master(upper_dev)) { 2260 if (info->linking) 2261 return dpaa2_switch_port_bridge_join(netdev, 2262 upper_dev, 2263 extack); 2264 else 2265 return dpaa2_switch_port_bridge_leave(netdev); 2266 } 2267 2268 return 0; 2269 } 2270 2271 static int dpaa2_switch_port_netdevice_event(struct notifier_block *nb, 2272 unsigned long event, void *ptr) 2273 { 2274 struct net_device *netdev = netdev_notifier_info_to_dev(ptr); 2275 int err = 0; 2276 2277 switch (event) { 2278 case NETDEV_PRECHANGEUPPER: 2279 err = dpaa2_switch_port_prechangeupper(netdev, ptr); 2280 if (err) 2281 return notifier_from_errno(err); 2282 2283 break; 2284 case NETDEV_CHANGEUPPER: 2285 err = dpaa2_switch_port_changeupper(netdev, ptr); 2286 if (err) 2287 return notifier_from_errno(err); 2288 2289 break; 2290 } 2291 2292 return NOTIFY_DONE; 2293 } 2294 2295 struct ethsw_switchdev_event_work { 2296 struct work_struct work; 2297 struct switchdev_notifier_fdb_info fdb_info; 2298 struct net_device *dev; 2299 unsigned long event; 2300 }; 2301 2302 static void dpaa2_switch_event_work(struct work_struct *work) 2303 { 2304 struct ethsw_switchdev_event_work *switchdev_work = 2305 container_of(work, struct ethsw_switchdev_event_work, work); 2306 struct net_device *dev = switchdev_work->dev; 2307 struct switchdev_notifier_fdb_info *fdb_info; 2308 int err; 2309 2310 rtnl_lock(); 2311 fdb_info = &switchdev_work->fdb_info; 2312 2313 switch (switchdev_work->event) { 2314 case SWITCHDEV_FDB_ADD_TO_DEVICE: 2315 if (!fdb_info->added_by_user || fdb_info->is_local) 2316 break; 2317 if (is_unicast_ether_addr(fdb_info->addr)) 2318 err = dpaa2_switch_port_fdb_add_uc(netdev_priv(dev), 2319 fdb_info->addr); 2320 else 2321 err = dpaa2_switch_port_fdb_add_mc(netdev_priv(dev), 2322 fdb_info->addr); 2323 if (err) 2324 break; 2325 fdb_info->offloaded = true; 2326 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev, 2327 &fdb_info->info, NULL); 2328 break; 2329 case SWITCHDEV_FDB_DEL_TO_DEVICE: 2330 if (!fdb_info->added_by_user || fdb_info->is_local) 2331 break; 2332 if (is_unicast_ether_addr(fdb_info->addr)) 2333 dpaa2_switch_port_fdb_del_uc(netdev_priv(dev), fdb_info->addr); 2334 else 2335 dpaa2_switch_port_fdb_del_mc(netdev_priv(dev), fdb_info->addr); 2336 break; 2337 } 2338 2339 rtnl_unlock(); 2340 kfree(switchdev_work->fdb_info.addr); 2341 kfree(switchdev_work); 2342 dev_put(dev); 2343 } 2344 2345 /* Called under rcu_read_lock() */ 2346 static int dpaa2_switch_port_event(struct notifier_block *nb, 2347 unsigned long event, void *ptr) 2348 { 2349 struct net_device *dev = switchdev_notifier_info_to_dev(ptr); 2350 struct ethsw_port_priv *port_priv = netdev_priv(dev); 2351 struct ethsw_switchdev_event_work *switchdev_work; 2352 struct switchdev_notifier_fdb_info *fdb_info = ptr; 2353 struct ethsw_core *ethsw = port_priv->ethsw_data; 2354 2355 if (event == SWITCHDEV_PORT_ATTR_SET) 2356 return dpaa2_switch_port_attr_set_event(dev, ptr); 2357 2358 if (!dpaa2_switch_port_dev_check(dev)) 2359 return NOTIFY_DONE; 2360 2361 switchdev_work = kzalloc_obj(*switchdev_work, GFP_ATOMIC); 2362 if (!switchdev_work) 2363 return NOTIFY_BAD; 2364 2365 INIT_WORK(&switchdev_work->work, dpaa2_switch_event_work); 2366 switchdev_work->dev = dev; 2367 switchdev_work->event = event; 2368 2369 switch (event) { 2370 case SWITCHDEV_FDB_ADD_TO_DEVICE: 2371 case SWITCHDEV_FDB_DEL_TO_DEVICE: 2372 memcpy(&switchdev_work->fdb_info, ptr, 2373 sizeof(switchdev_work->fdb_info)); 2374 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC); 2375 if (!switchdev_work->fdb_info.addr) 2376 goto err_addr_alloc; 2377 2378 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr, 2379 fdb_info->addr); 2380 2381 /* Take a reference on the device to avoid being freed. */ 2382 dev_hold(dev); 2383 break; 2384 default: 2385 kfree(switchdev_work); 2386 return NOTIFY_DONE; 2387 } 2388 2389 queue_work(ethsw->workqueue, &switchdev_work->work); 2390 2391 return NOTIFY_DONE; 2392 2393 err_addr_alloc: 2394 kfree(switchdev_work); 2395 return NOTIFY_BAD; 2396 } 2397 2398 static int dpaa2_switch_port_obj_event(unsigned long event, 2399 struct net_device *netdev, 2400 struct switchdev_notifier_port_obj_info *port_obj_info) 2401 { 2402 int err = -EOPNOTSUPP; 2403 2404 if (!dpaa2_switch_port_dev_check(netdev)) 2405 return NOTIFY_DONE; 2406 2407 switch (event) { 2408 case SWITCHDEV_PORT_OBJ_ADD: 2409 err = dpaa2_switch_port_obj_add(netdev, port_obj_info->obj); 2410 break; 2411 case SWITCHDEV_PORT_OBJ_DEL: 2412 err = dpaa2_switch_port_obj_del(netdev, port_obj_info->obj); 2413 break; 2414 } 2415 2416 port_obj_info->handled = true; 2417 return notifier_from_errno(err); 2418 } 2419 2420 static int dpaa2_switch_port_blocking_event(struct notifier_block *nb, 2421 unsigned long event, void *ptr) 2422 { 2423 struct net_device *dev = switchdev_notifier_info_to_dev(ptr); 2424 2425 switch (event) { 2426 case SWITCHDEV_PORT_OBJ_ADD: 2427 case SWITCHDEV_PORT_OBJ_DEL: 2428 return dpaa2_switch_port_obj_event(event, dev, ptr); 2429 case SWITCHDEV_PORT_ATTR_SET: 2430 return dpaa2_switch_port_attr_set_event(dev, ptr); 2431 } 2432 2433 return NOTIFY_DONE; 2434 } 2435 2436 /* Build a linear skb based on a single-buffer frame descriptor */ 2437 static struct sk_buff *dpaa2_switch_build_linear_skb(struct ethsw_core *ethsw, 2438 const struct dpaa2_fd *fd, 2439 void *fd_vaddr) 2440 { 2441 u16 fd_offset = dpaa2_fd_get_offset(fd); 2442 u32 fd_length = dpaa2_fd_get_len(fd); 2443 struct device *dev = ethsw->dev; 2444 struct sk_buff *skb = NULL; 2445 2446 skb = build_skb(fd_vaddr, DPAA2_SWITCH_RX_BUF_SIZE + 2447 SKB_DATA_ALIGN(sizeof(struct skb_shared_info))); 2448 if (unlikely(!skb)) { 2449 dev_err(dev, "build_skb() failed\n"); 2450 return NULL; 2451 } 2452 2453 skb_reserve(skb, fd_offset); 2454 skb_put(skb, fd_length); 2455 2456 ethsw->buf_count--; 2457 2458 return skb; 2459 } 2460 2461 static void dpaa2_switch_tx_conf(struct dpaa2_switch_fq *fq, 2462 const struct dpaa2_fd *fd) 2463 { 2464 dpaa2_switch_free_fd(fq->ethsw, fd); 2465 } 2466 2467 static void dpaa2_switch_rx(struct dpaa2_switch_fq *fq, 2468 const struct dpaa2_fd *fd) 2469 { 2470 dma_addr_t addr = dpaa2_fd_get_addr(fd); 2471 struct ethsw_core *ethsw = fq->ethsw; 2472 struct ethsw_port_priv *port_priv; 2473 struct net_device *netdev; 2474 struct vlan_ethhdr *hdr; 2475 struct sk_buff *skb; 2476 u16 vlan_tci, vid; 2477 int if_id, err; 2478 void *vaddr; 2479 2480 vaddr = dpaa2_iova_to_virt(ethsw->iommu_domain, addr); 2481 dma_unmap_page(ethsw->dev, addr, DPAA2_SWITCH_RX_BUF_SIZE, 2482 DMA_FROM_DEVICE); 2483 2484 /* get switch ingress interface ID */ 2485 if_id = upper_32_bits(dpaa2_fd_get_flc(fd)) & 0x0000FFFF; 2486 if (if_id >= ethsw->sw_attr.num_ifs) { 2487 dev_err(ethsw->dev, "Frame received from unknown interface!\n"); 2488 goto err_free_fd; 2489 } 2490 port_priv = ethsw->ports[if_id]; 2491 netdev = port_priv->netdev; 2492 2493 /* build the SKB based on the FD received */ 2494 if (dpaa2_fd_get_format(fd) != dpaa2_fd_single) { 2495 if (net_ratelimit()) { 2496 netdev_err(netdev, "Received invalid frame format\n"); 2497 goto err_free_fd; 2498 } 2499 } 2500 2501 skb = dpaa2_switch_build_linear_skb(ethsw, fd, vaddr); 2502 if (unlikely(!skb)) 2503 goto err_free_fd; 2504 2505 skb_reset_mac_header(skb); 2506 2507 /* Remove the VLAN header if the packet that we just received has a vid 2508 * equal to the port PVIDs. Since the dpaa2-switch can operate only in 2509 * VLAN-aware mode and no alterations are made on the packet when it's 2510 * redirected/mirrored to the control interface, we are sure that there 2511 * will always be a VLAN header present. 2512 */ 2513 hdr = vlan_eth_hdr(skb); 2514 vid = ntohs(hdr->h_vlan_TCI) & VLAN_VID_MASK; 2515 if (vid == port_priv->pvid) { 2516 err = __skb_vlan_pop(skb, &vlan_tci); 2517 if (err) { 2518 dev_info(ethsw->dev, "__skb_vlan_pop() returned %d", err); 2519 kfree_skb(skb); 2520 return; 2521 } 2522 } 2523 2524 skb->dev = netdev; 2525 skb->protocol = eth_type_trans(skb, skb->dev); 2526 2527 /* Setup the offload_fwd_mark only if the port is under a bridge */ 2528 skb->offload_fwd_mark = !!(port_priv->fdb->bridge_dev); 2529 2530 netif_receive_skb(skb); 2531 2532 return; 2533 2534 err_free_fd: 2535 free_pages((unsigned long)vaddr, 0); 2536 } 2537 2538 static void dpaa2_switch_detect_features(struct ethsw_core *ethsw) 2539 { 2540 ethsw->features = 0; 2541 2542 if (ethsw->major > 8 || (ethsw->major == 8 && ethsw->minor >= 6)) 2543 ethsw->features |= ETHSW_FEATURE_MAC_ADDR; 2544 } 2545 2546 static int dpaa2_switch_setup_fqs(struct ethsw_core *ethsw) 2547 { 2548 struct dpsw_ctrl_if_attr ctrl_if_attr; 2549 struct device *dev = ethsw->dev; 2550 int i = 0; 2551 int err; 2552 2553 err = dpsw_ctrl_if_get_attributes(ethsw->mc_io, 0, ethsw->dpsw_handle, 2554 &ctrl_if_attr); 2555 if (err) { 2556 dev_err(dev, "dpsw_ctrl_if_get_attributes() = %d\n", err); 2557 return err; 2558 } 2559 2560 ethsw->fq[i].fqid = ctrl_if_attr.rx_fqid; 2561 ethsw->fq[i].ethsw = ethsw; 2562 ethsw->fq[i++].type = DPSW_QUEUE_RX; 2563 2564 ethsw->fq[i].fqid = ctrl_if_attr.tx_err_conf_fqid; 2565 ethsw->fq[i].ethsw = ethsw; 2566 ethsw->fq[i++].type = DPSW_QUEUE_TX_ERR_CONF; 2567 2568 return 0; 2569 } 2570 2571 /* Free buffers acquired from the buffer pool or which were meant to 2572 * be released in the pool 2573 */ 2574 static void dpaa2_switch_free_bufs(struct ethsw_core *ethsw, u64 *buf_array, int count) 2575 { 2576 struct device *dev = ethsw->dev; 2577 void *vaddr; 2578 int i; 2579 2580 for (i = 0; i < count; i++) { 2581 vaddr = dpaa2_iova_to_virt(ethsw->iommu_domain, buf_array[i]); 2582 dma_unmap_page(dev, buf_array[i], DPAA2_SWITCH_RX_BUF_SIZE, 2583 DMA_FROM_DEVICE); 2584 free_pages((unsigned long)vaddr, 0); 2585 } 2586 } 2587 2588 /* Perform a single release command to add buffers 2589 * to the specified buffer pool 2590 */ 2591 static int dpaa2_switch_add_bufs(struct ethsw_core *ethsw, u16 bpid) 2592 { 2593 struct device *dev = ethsw->dev; 2594 u64 buf_array[BUFS_PER_CMD]; 2595 struct page *page; 2596 int retries = 0; 2597 dma_addr_t addr; 2598 int err; 2599 int i; 2600 2601 for (i = 0; i < BUFS_PER_CMD; i++) { 2602 /* Allocate one page for each Rx buffer. WRIOP sees 2603 * the entire page except for a tailroom reserved for 2604 * skb shared info 2605 */ 2606 page = dev_alloc_pages(0); 2607 if (!page) { 2608 dev_err(dev, "buffer allocation failed\n"); 2609 goto err_alloc; 2610 } 2611 2612 addr = dma_map_page(dev, page, 0, DPAA2_SWITCH_RX_BUF_SIZE, 2613 DMA_FROM_DEVICE); 2614 if (dma_mapping_error(dev, addr)) { 2615 dev_err(dev, "dma_map_single() failed\n"); 2616 goto err_map; 2617 } 2618 buf_array[i] = addr; 2619 } 2620 2621 release_bufs: 2622 /* In case the portal is busy, retry until successful or 2623 * max retries hit. 2624 */ 2625 while ((err = dpaa2_io_service_release(NULL, bpid, 2626 buf_array, i)) == -EBUSY) { 2627 if (retries++ >= DPAA2_SWITCH_SWP_BUSY_RETRIES) 2628 break; 2629 2630 cpu_relax(); 2631 } 2632 2633 /* If release command failed, clean up and bail out. */ 2634 if (err) { 2635 dpaa2_switch_free_bufs(ethsw, buf_array, i); 2636 return 0; 2637 } 2638 2639 return i; 2640 2641 err_map: 2642 __free_pages(page, 0); 2643 err_alloc: 2644 /* If we managed to allocate at least some buffers, 2645 * release them to hardware 2646 */ 2647 if (i) 2648 goto release_bufs; 2649 2650 return 0; 2651 } 2652 2653 static int dpaa2_switch_refill_bp(struct ethsw_core *ethsw) 2654 { 2655 int *count = ðsw->buf_count; 2656 int new_count; 2657 int err = 0; 2658 2659 if (unlikely(*count < DPAA2_ETHSW_REFILL_THRESH)) { 2660 do { 2661 new_count = dpaa2_switch_add_bufs(ethsw, ethsw->bpid); 2662 if (unlikely(!new_count)) { 2663 /* Out of memory; abort for now, we'll 2664 * try later on 2665 */ 2666 break; 2667 } 2668 *count += new_count; 2669 } while (*count < DPAA2_ETHSW_NUM_BUFS); 2670 2671 if (unlikely(*count < DPAA2_ETHSW_NUM_BUFS)) 2672 err = -ENOMEM; 2673 } 2674 2675 return err; 2676 } 2677 2678 static int dpaa2_switch_seed_bp(struct ethsw_core *ethsw) 2679 { 2680 int *count, ret, i; 2681 2682 for (i = 0; i < DPAA2_ETHSW_NUM_BUFS; i += BUFS_PER_CMD) { 2683 ret = dpaa2_switch_add_bufs(ethsw, ethsw->bpid); 2684 count = ðsw->buf_count; 2685 *count += ret; 2686 2687 if (unlikely(ret < BUFS_PER_CMD)) 2688 return -ENOMEM; 2689 } 2690 2691 return 0; 2692 } 2693 2694 static void dpaa2_switch_drain_bp(struct ethsw_core *ethsw) 2695 { 2696 u64 buf_array[BUFS_PER_CMD]; 2697 int ret; 2698 2699 do { 2700 ret = dpaa2_io_service_acquire(NULL, ethsw->bpid, 2701 buf_array, BUFS_PER_CMD); 2702 if (ret < 0) { 2703 dev_err(ethsw->dev, 2704 "dpaa2_io_service_acquire() = %d\n", ret); 2705 return; 2706 } 2707 dpaa2_switch_free_bufs(ethsw, buf_array, ret); 2708 2709 } while (ret); 2710 } 2711 2712 static int dpaa2_switch_setup_dpbp(struct ethsw_core *ethsw) 2713 { 2714 struct dpsw_ctrl_if_pools_cfg dpsw_ctrl_if_pools_cfg = { 0 }; 2715 struct device *dev = ethsw->dev; 2716 struct fsl_mc_device *dpbp_dev; 2717 struct dpbp_attr dpbp_attrs; 2718 int err; 2719 2720 err = fsl_mc_object_allocate(to_fsl_mc_device(dev), FSL_MC_POOL_DPBP, 2721 &dpbp_dev); 2722 if (err) { 2723 if (err == -ENXIO) 2724 err = -EPROBE_DEFER; 2725 else 2726 dev_err(dev, "DPBP device allocation failed\n"); 2727 return err; 2728 } 2729 ethsw->dpbp_dev = dpbp_dev; 2730 2731 err = dpbp_open(ethsw->mc_io, 0, dpbp_dev->obj_desc.id, 2732 &dpbp_dev->mc_handle); 2733 if (err) { 2734 dev_err(dev, "dpbp_open() failed\n"); 2735 goto err_open; 2736 } 2737 2738 err = dpbp_reset(ethsw->mc_io, 0, dpbp_dev->mc_handle); 2739 if (err) { 2740 dev_err(dev, "dpbp_reset() failed\n"); 2741 goto err_reset; 2742 } 2743 2744 err = dpbp_enable(ethsw->mc_io, 0, dpbp_dev->mc_handle); 2745 if (err) { 2746 dev_err(dev, "dpbp_enable() failed\n"); 2747 goto err_enable; 2748 } 2749 2750 err = dpbp_get_attributes(ethsw->mc_io, 0, dpbp_dev->mc_handle, 2751 &dpbp_attrs); 2752 if (err) { 2753 dev_err(dev, "dpbp_get_attributes() failed\n"); 2754 goto err_get_attr; 2755 } 2756 2757 dpsw_ctrl_if_pools_cfg.num_dpbp = 1; 2758 dpsw_ctrl_if_pools_cfg.pools[0].dpbp_id = dpbp_attrs.id; 2759 dpsw_ctrl_if_pools_cfg.pools[0].buffer_size = DPAA2_SWITCH_RX_BUF_SIZE; 2760 dpsw_ctrl_if_pools_cfg.pools[0].backup_pool = 0; 2761 2762 err = dpsw_ctrl_if_set_pools(ethsw->mc_io, 0, ethsw->dpsw_handle, 2763 &dpsw_ctrl_if_pools_cfg); 2764 if (err) { 2765 dev_err(dev, "dpsw_ctrl_if_set_pools() failed\n"); 2766 goto err_get_attr; 2767 } 2768 ethsw->bpid = dpbp_attrs.bpid; 2769 2770 return 0; 2771 2772 err_get_attr: 2773 dpbp_disable(ethsw->mc_io, 0, dpbp_dev->mc_handle); 2774 err_enable: 2775 err_reset: 2776 dpbp_close(ethsw->mc_io, 0, dpbp_dev->mc_handle); 2777 err_open: 2778 fsl_mc_object_free(dpbp_dev); 2779 return err; 2780 } 2781 2782 static void dpaa2_switch_free_dpbp(struct ethsw_core *ethsw) 2783 { 2784 dpbp_disable(ethsw->mc_io, 0, ethsw->dpbp_dev->mc_handle); 2785 dpbp_close(ethsw->mc_io, 0, ethsw->dpbp_dev->mc_handle); 2786 fsl_mc_object_free(ethsw->dpbp_dev); 2787 } 2788 2789 static int dpaa2_switch_alloc_rings(struct ethsw_core *ethsw) 2790 { 2791 int i; 2792 2793 for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++) { 2794 ethsw->fq[i].store = 2795 dpaa2_io_store_create(DPAA2_SWITCH_STORE_SIZE, 2796 ethsw->dev); 2797 if (!ethsw->fq[i].store) { 2798 dev_err(ethsw->dev, "dpaa2_io_store_create failed\n"); 2799 while (--i >= 0) 2800 dpaa2_io_store_destroy(ethsw->fq[i].store); 2801 return -ENOMEM; 2802 } 2803 } 2804 2805 return 0; 2806 } 2807 2808 static void dpaa2_switch_destroy_rings(struct ethsw_core *ethsw) 2809 { 2810 int i; 2811 2812 for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++) 2813 dpaa2_io_store_destroy(ethsw->fq[i].store); 2814 } 2815 2816 static int dpaa2_switch_pull_fq(struct dpaa2_switch_fq *fq) 2817 { 2818 int err, retries = 0; 2819 2820 /* Try to pull from the FQ while the portal is busy and we didn't hit 2821 * the maximum number fo retries 2822 */ 2823 do { 2824 err = dpaa2_io_service_pull_fq(NULL, fq->fqid, fq->store); 2825 cpu_relax(); 2826 } while (err == -EBUSY && retries++ < DPAA2_SWITCH_SWP_BUSY_RETRIES); 2827 2828 if (unlikely(err)) 2829 dev_err(fq->ethsw->dev, "dpaa2_io_service_pull err %d", err); 2830 2831 return err; 2832 } 2833 2834 /* Consume all frames pull-dequeued into the store */ 2835 static int dpaa2_switch_store_consume(struct dpaa2_switch_fq *fq) 2836 { 2837 struct ethsw_core *ethsw = fq->ethsw; 2838 int cleaned = 0, is_last; 2839 struct dpaa2_dq *dq; 2840 int retries = 0; 2841 2842 do { 2843 /* Get the next available FD from the store */ 2844 dq = dpaa2_io_store_next(fq->store, &is_last); 2845 if (unlikely(!dq)) { 2846 if (retries++ >= DPAA2_SWITCH_SWP_BUSY_RETRIES) { 2847 dev_err_once(ethsw->dev, 2848 "No valid dequeue response\n"); 2849 return -ETIMEDOUT; 2850 } 2851 continue; 2852 } 2853 2854 if (fq->type == DPSW_QUEUE_RX) 2855 dpaa2_switch_rx(fq, dpaa2_dq_fd(dq)); 2856 else 2857 dpaa2_switch_tx_conf(fq, dpaa2_dq_fd(dq)); 2858 cleaned++; 2859 2860 } while (!is_last); 2861 2862 return cleaned; 2863 } 2864 2865 /* NAPI poll routine */ 2866 static int dpaa2_switch_poll(struct napi_struct *napi, int budget) 2867 { 2868 int err, cleaned = 0, store_cleaned, work_done; 2869 struct dpaa2_switch_fq *fq; 2870 int retries = 0; 2871 2872 fq = container_of(napi, struct dpaa2_switch_fq, napi); 2873 2874 do { 2875 err = dpaa2_switch_pull_fq(fq); 2876 if (unlikely(err)) 2877 break; 2878 2879 /* Refill pool if appropriate */ 2880 dpaa2_switch_refill_bp(fq->ethsw); 2881 2882 store_cleaned = dpaa2_switch_store_consume(fq); 2883 cleaned += store_cleaned; 2884 2885 if (cleaned >= budget) { 2886 work_done = budget; 2887 goto out; 2888 } 2889 2890 } while (store_cleaned); 2891 2892 /* We didn't consume the entire budget, so finish napi and re-enable 2893 * data availability notifications 2894 */ 2895 napi_complete_done(napi, cleaned); 2896 do { 2897 err = dpaa2_io_service_rearm(NULL, &fq->nctx); 2898 cpu_relax(); 2899 } while (err == -EBUSY && retries++ < DPAA2_SWITCH_SWP_BUSY_RETRIES); 2900 2901 work_done = max(cleaned, 1); 2902 out: 2903 2904 return work_done; 2905 } 2906 2907 static void dpaa2_switch_fqdan_cb(struct dpaa2_io_notification_ctx *nctx) 2908 { 2909 struct dpaa2_switch_fq *fq; 2910 2911 fq = container_of(nctx, struct dpaa2_switch_fq, nctx); 2912 2913 napi_schedule(&fq->napi); 2914 } 2915 2916 static int dpaa2_switch_setup_dpio(struct ethsw_core *ethsw) 2917 { 2918 struct dpsw_ctrl_if_queue_cfg queue_cfg; 2919 struct dpaa2_io_notification_ctx *nctx; 2920 int err, i, j; 2921 2922 for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++) { 2923 nctx = ðsw->fq[i].nctx; 2924 2925 /* Register a new software context for the FQID. 2926 * By using NULL as the first parameter, we specify that we do 2927 * not care on which cpu are interrupts received for this queue 2928 */ 2929 nctx->is_cdan = 0; 2930 nctx->id = ethsw->fq[i].fqid; 2931 nctx->desired_cpu = DPAA2_IO_ANY_CPU; 2932 nctx->cb = dpaa2_switch_fqdan_cb; 2933 err = dpaa2_io_service_register(NULL, nctx, ethsw->dev); 2934 if (err) { 2935 err = -EPROBE_DEFER; 2936 goto err_register; 2937 } 2938 2939 queue_cfg.options = DPSW_CTRL_IF_QUEUE_OPT_DEST | 2940 DPSW_CTRL_IF_QUEUE_OPT_USER_CTX; 2941 queue_cfg.dest_cfg.dest_type = DPSW_CTRL_IF_DEST_DPIO; 2942 queue_cfg.dest_cfg.dest_id = nctx->dpio_id; 2943 queue_cfg.dest_cfg.priority = 0; 2944 queue_cfg.user_ctx = nctx->qman64; 2945 2946 err = dpsw_ctrl_if_set_queue(ethsw->mc_io, 0, 2947 ethsw->dpsw_handle, 2948 ethsw->fq[i].type, 2949 &queue_cfg); 2950 if (err) 2951 goto err_set_queue; 2952 } 2953 2954 return 0; 2955 2956 err_set_queue: 2957 dpaa2_io_service_deregister(NULL, nctx, ethsw->dev); 2958 err_register: 2959 for (j = 0; j < i; j++) 2960 dpaa2_io_service_deregister(NULL, ðsw->fq[j].nctx, 2961 ethsw->dev); 2962 2963 return err; 2964 } 2965 2966 static void dpaa2_switch_free_dpio(struct ethsw_core *ethsw) 2967 { 2968 int i; 2969 2970 for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++) 2971 dpaa2_io_service_deregister(NULL, ðsw->fq[i].nctx, 2972 ethsw->dev); 2973 } 2974 2975 static int dpaa2_switch_ctrl_if_setup(struct ethsw_core *ethsw) 2976 { 2977 int err; 2978 2979 /* setup FQs for Rx and Tx Conf */ 2980 err = dpaa2_switch_setup_fqs(ethsw); 2981 if (err) 2982 return err; 2983 2984 /* setup the buffer pool needed on the Rx path */ 2985 err = dpaa2_switch_setup_dpbp(ethsw); 2986 if (err) 2987 return err; 2988 2989 err = dpaa2_switch_alloc_rings(ethsw); 2990 if (err) 2991 goto err_free_dpbp; 2992 2993 err = dpaa2_switch_setup_dpio(ethsw); 2994 if (err) 2995 goto err_destroy_rings; 2996 2997 err = dpaa2_switch_seed_bp(ethsw); 2998 if (err) 2999 goto err_deregister_dpio; 3000 3001 err = dpsw_ctrl_if_enable(ethsw->mc_io, 0, ethsw->dpsw_handle); 3002 if (err) { 3003 dev_err(ethsw->dev, "dpsw_ctrl_if_enable err %d\n", err); 3004 goto err_drain_dpbp; 3005 } 3006 3007 return 0; 3008 3009 err_drain_dpbp: 3010 dpaa2_switch_drain_bp(ethsw); 3011 err_deregister_dpio: 3012 dpaa2_switch_free_dpio(ethsw); 3013 err_destroy_rings: 3014 dpaa2_switch_destroy_rings(ethsw); 3015 err_free_dpbp: 3016 dpaa2_switch_free_dpbp(ethsw); 3017 3018 return err; 3019 } 3020 3021 static void dpaa2_switch_remove_port(struct ethsw_core *ethsw, 3022 u16 port_idx) 3023 { 3024 struct ethsw_port_priv *port_priv = ethsw->ports[port_idx]; 3025 3026 dpaa2_switch_port_disconnect_mac(port_priv); 3027 free_netdev(port_priv->netdev); 3028 ethsw->ports[port_idx] = NULL; 3029 } 3030 3031 static int dpaa2_switch_init(struct fsl_mc_device *sw_dev) 3032 { 3033 struct device *dev = &sw_dev->dev; 3034 struct ethsw_core *ethsw = dev_get_drvdata(dev); 3035 struct dpsw_vlan_if_cfg vcfg = {0}; 3036 struct dpsw_tci_cfg tci_cfg = {0}; 3037 struct dpsw_stp_cfg stp_cfg; 3038 int err; 3039 u16 i; 3040 3041 ethsw->dev_id = sw_dev->obj_desc.id; 3042 3043 err = dpsw_open(ethsw->mc_io, 0, ethsw->dev_id, ðsw->dpsw_handle); 3044 if (err) { 3045 dev_err(dev, "dpsw_open err %d\n", err); 3046 return err; 3047 } 3048 3049 err = dpsw_get_attributes(ethsw->mc_io, 0, ethsw->dpsw_handle, 3050 ðsw->sw_attr); 3051 if (err) { 3052 dev_err(dev, "dpsw_get_attributes err %d\n", err); 3053 goto err_close; 3054 } 3055 3056 if (!ethsw->sw_attr.num_ifs) { 3057 dev_err(dev, "DPSW device has no interfaces\n"); 3058 err = -ENODEV; 3059 goto err_close; 3060 } 3061 3062 if (ethsw->sw_attr.num_ifs >= DPSW_MAX_IF) { 3063 dev_err(dev, "DPSW num_ifs %u exceeds max %u\n", 3064 ethsw->sw_attr.num_ifs, DPSW_MAX_IF); 3065 err = -EINVAL; 3066 goto err_close; 3067 } 3068 3069 err = dpsw_get_api_version(ethsw->mc_io, 0, 3070 ðsw->major, 3071 ðsw->minor); 3072 if (err) { 3073 dev_err(dev, "dpsw_get_api_version err %d\n", err); 3074 goto err_close; 3075 } 3076 3077 /* Minimum supported DPSW version check */ 3078 if (ethsw->major < DPSW_MIN_VER_MAJOR || 3079 (ethsw->major == DPSW_MIN_VER_MAJOR && 3080 ethsw->minor < DPSW_MIN_VER_MINOR)) { 3081 dev_err(dev, "DPSW version %d:%d not supported. Use firmware 10.28.0 or greater.\n", 3082 ethsw->major, ethsw->minor); 3083 err = -EOPNOTSUPP; 3084 goto err_close; 3085 } 3086 3087 if (!dpaa2_switch_supports_cpu_traffic(ethsw)) { 3088 err = -EOPNOTSUPP; 3089 goto err_close; 3090 } 3091 3092 dpaa2_switch_detect_features(ethsw); 3093 3094 err = dpsw_reset(ethsw->mc_io, 0, ethsw->dpsw_handle); 3095 if (err) { 3096 dev_err(dev, "dpsw_reset err %d\n", err); 3097 goto err_close; 3098 } 3099 3100 stp_cfg.vlan_id = DEFAULT_VLAN_ID; 3101 stp_cfg.state = DPSW_STP_STATE_FORWARDING; 3102 3103 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) { 3104 err = dpsw_if_disable(ethsw->mc_io, 0, ethsw->dpsw_handle, i); 3105 if (err) { 3106 dev_err(dev, "dpsw_if_disable err %d\n", err); 3107 goto err_close; 3108 } 3109 3110 err = dpsw_if_set_stp(ethsw->mc_io, 0, ethsw->dpsw_handle, i, 3111 &stp_cfg); 3112 if (err) { 3113 dev_err(dev, "dpsw_if_set_stp err %d for port %d\n", 3114 err, i); 3115 goto err_close; 3116 } 3117 3118 /* Switch starts with all ports configured to VLAN 1. Need to 3119 * remove this setting to allow configuration at bridge join 3120 */ 3121 vcfg.num_ifs = 1; 3122 vcfg.if_id[0] = i; 3123 err = dpsw_vlan_remove_if_untagged(ethsw->mc_io, 0, ethsw->dpsw_handle, 3124 DEFAULT_VLAN_ID, &vcfg); 3125 if (err) { 3126 dev_err(dev, "dpsw_vlan_remove_if_untagged err %d\n", 3127 err); 3128 goto err_close; 3129 } 3130 3131 tci_cfg.vlan_id = 4095; 3132 err = dpsw_if_set_tci(ethsw->mc_io, 0, ethsw->dpsw_handle, i, &tci_cfg); 3133 if (err) { 3134 dev_err(dev, "dpsw_if_set_tci err %d\n", err); 3135 goto err_close; 3136 } 3137 3138 err = dpsw_vlan_remove_if(ethsw->mc_io, 0, ethsw->dpsw_handle, 3139 DEFAULT_VLAN_ID, &vcfg); 3140 if (err) { 3141 dev_err(dev, "dpsw_vlan_remove_if err %d\n", err); 3142 goto err_close; 3143 } 3144 } 3145 3146 err = dpsw_vlan_remove(ethsw->mc_io, 0, ethsw->dpsw_handle, DEFAULT_VLAN_ID); 3147 if (err) { 3148 dev_err(dev, "dpsw_vlan_remove err %d\n", err); 3149 goto err_close; 3150 } 3151 3152 ethsw->workqueue = alloc_ordered_workqueue("%s_%d_ordered", 3153 WQ_MEM_RECLAIM, "ethsw", 3154 ethsw->sw_attr.id); 3155 if (!ethsw->workqueue) { 3156 err = -ENOMEM; 3157 goto err_close; 3158 } 3159 3160 err = dpsw_fdb_remove(ethsw->mc_io, 0, ethsw->dpsw_handle, 0); 3161 if (err) 3162 goto err_destroy_ordered_workqueue; 3163 3164 err = dpaa2_switch_ctrl_if_setup(ethsw); 3165 if (err) 3166 goto err_destroy_ordered_workqueue; 3167 3168 return 0; 3169 3170 err_destroy_ordered_workqueue: 3171 destroy_workqueue(ethsw->workqueue); 3172 3173 err_close: 3174 dpsw_close(ethsw->mc_io, 0, ethsw->dpsw_handle); 3175 return err; 3176 } 3177 3178 /* Add an ACL to redirect frames with specific destination MAC address to 3179 * control interface 3180 */ 3181 static int dpaa2_switch_port_trap_mac_addr(struct ethsw_port_priv *port_priv, 3182 const char *mac) 3183 { 3184 struct dpaa2_switch_acl_entry acl_entry = {0}; 3185 3186 /* Match on the destination MAC address */ 3187 ether_addr_copy(acl_entry.key.match.l2_dest_mac, mac); 3188 eth_broadcast_addr(acl_entry.key.mask.l2_dest_mac); 3189 3190 /* Trap to CPU */ 3191 acl_entry.cfg.precedence = 0; 3192 acl_entry.cfg.result.action = DPSW_ACL_ACTION_REDIRECT_TO_CTRL_IF; 3193 3194 return dpaa2_switch_acl_entry_add(port_priv->filter_block, &acl_entry); 3195 } 3196 3197 static int dpaa2_switch_port_init(struct ethsw_port_priv *port_priv, u16 port) 3198 { 3199 const char stpa[ETH_ALEN] = {0x01, 0x80, 0xc2, 0x00, 0x00, 0x00}; 3200 struct switchdev_obj_port_vlan vlan = { 3201 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN, 3202 .vid = DEFAULT_VLAN_ID, 3203 .flags = BRIDGE_VLAN_INFO_UNTAGGED | BRIDGE_VLAN_INFO_PVID, 3204 }; 3205 struct net_device *netdev = port_priv->netdev; 3206 struct ethsw_core *ethsw = port_priv->ethsw_data; 3207 struct dpaa2_switch_filter_block *filter_block; 3208 struct dpsw_fdb_cfg fdb_cfg = {0}; 3209 struct dpsw_if_attr dpsw_if_attr; 3210 struct dpaa2_switch_fdb *fdb; 3211 struct dpsw_acl_cfg acl_cfg; 3212 u16 fdb_id, acl_tbl_id; 3213 int err; 3214 3215 /* Get the Tx queue for this specific port */ 3216 err = dpsw_if_get_attributes(ethsw->mc_io, 0, ethsw->dpsw_handle, 3217 port_priv->idx, &dpsw_if_attr); 3218 if (err) { 3219 netdev_err(netdev, "dpsw_if_get_attributes err %d\n", err); 3220 return err; 3221 } 3222 port_priv->tx_qdid = dpsw_if_attr.qdid; 3223 3224 /* Create a FDB table for this particular switch port */ 3225 fdb_cfg.num_fdb_entries = ethsw->sw_attr.max_fdb_entries / ethsw->sw_attr.num_ifs; 3226 err = dpsw_fdb_add(ethsw->mc_io, 0, ethsw->dpsw_handle, 3227 &fdb_id, &fdb_cfg); 3228 if (err) { 3229 netdev_err(netdev, "dpsw_fdb_add err %d\n", err); 3230 return err; 3231 } 3232 3233 /* Find an unused dpaa2_switch_fdb structure and use it */ 3234 fdb = dpaa2_switch_fdb_get_unused(ethsw); 3235 fdb->fdb_id = fdb_id; 3236 fdb->in_use = true; 3237 fdb->bridge_dev = NULL; 3238 port_priv->fdb = fdb; 3239 3240 /* We need to add VLAN 1 as the PVID on this port until it is under a 3241 * bridge since the DPAA2 switch is not able to handle the traffic in a 3242 * VLAN unaware fashion 3243 */ 3244 err = dpaa2_switch_port_vlans_add(netdev, &vlan); 3245 if (err) 3246 return err; 3247 3248 /* Setup the egress flooding domains (broadcast, unknown unicast */ 3249 err = dpaa2_switch_fdb_set_egress_flood(ethsw, port_priv->fdb->fdb_id); 3250 if (err) 3251 return err; 3252 3253 /* Create an ACL table to be used by this switch port */ 3254 acl_cfg.max_entries = DPAA2_ETHSW_PORT_MAX_ACL_ENTRIES; 3255 err = dpsw_acl_add(ethsw->mc_io, 0, ethsw->dpsw_handle, 3256 &acl_tbl_id, &acl_cfg); 3257 if (err) { 3258 netdev_err(netdev, "dpsw_acl_add err %d\n", err); 3259 return err; 3260 } 3261 3262 filter_block = dpaa2_switch_filter_block_get_unused(ethsw); 3263 filter_block->ethsw = ethsw; 3264 filter_block->acl_id = acl_tbl_id; 3265 filter_block->in_use = true; 3266 filter_block->num_acl_rules = 0; 3267 INIT_LIST_HEAD(&filter_block->acl_entries); 3268 INIT_LIST_HEAD(&filter_block->mirror_entries); 3269 3270 err = dpaa2_switch_port_acl_tbl_bind(port_priv, filter_block); 3271 if (err) 3272 return err; 3273 3274 err = dpaa2_switch_port_trap_mac_addr(port_priv, stpa); 3275 if (err) 3276 return err; 3277 3278 return err; 3279 } 3280 3281 static void dpaa2_switch_ctrl_if_teardown(struct ethsw_core *ethsw) 3282 { 3283 dpsw_ctrl_if_disable(ethsw->mc_io, 0, ethsw->dpsw_handle); 3284 dpaa2_switch_free_dpio(ethsw); 3285 dpaa2_switch_destroy_rings(ethsw); 3286 dpaa2_switch_drain_bp(ethsw); 3287 dpaa2_switch_free_dpbp(ethsw); 3288 } 3289 3290 static void dpaa2_switch_teardown(struct fsl_mc_device *sw_dev) 3291 { 3292 struct device *dev = &sw_dev->dev; 3293 struct ethsw_core *ethsw = dev_get_drvdata(dev); 3294 int err; 3295 3296 dpaa2_switch_ctrl_if_teardown(ethsw); 3297 3298 destroy_workqueue(ethsw->workqueue); 3299 3300 err = dpsw_close(ethsw->mc_io, 0, ethsw->dpsw_handle); 3301 if (err) 3302 dev_warn(dev, "dpsw_close err %d\n", err); 3303 } 3304 3305 static void dpaa2_switch_remove(struct fsl_mc_device *sw_dev) 3306 { 3307 struct ethsw_core *ethsw; 3308 struct device *dev; 3309 int i; 3310 3311 dev = &sw_dev->dev; 3312 ethsw = dev_get_drvdata(dev); 3313 3314 dpaa2_switch_teardown_irqs(sw_dev); 3315 3316 dpsw_disable(ethsw->mc_io, 0, ethsw->dpsw_handle); 3317 3318 /* Unregister all the netdevs so that they are brought down and the 3319 * shared NAPI instances gets disabled. 3320 */ 3321 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) 3322 unregister_netdev(ethsw->ports[i]->netdev); 3323 3324 for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++) 3325 netif_napi_del(ðsw->fq[i].napi); 3326 3327 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) 3328 dpaa2_switch_remove_port(ethsw, i); 3329 3330 kfree(ethsw->fdbs); 3331 kfree(ethsw->filter_blocks); 3332 kfree(ethsw->ports); 3333 3334 dpaa2_switch_teardown(sw_dev); 3335 3336 fsl_mc_portal_free(ethsw->mc_io); 3337 3338 kfree(ethsw); 3339 3340 dev_set_drvdata(dev, NULL); 3341 } 3342 3343 static int dpaa2_switch_probe_port(struct ethsw_core *ethsw, 3344 u16 port_idx) 3345 { 3346 struct ethsw_port_priv *port_priv; 3347 struct device *dev = ethsw->dev; 3348 struct net_device *port_netdev; 3349 int err; 3350 3351 port_netdev = alloc_etherdev(sizeof(struct ethsw_port_priv)); 3352 if (!port_netdev) { 3353 dev_err(dev, "alloc_etherdev error\n"); 3354 return -ENOMEM; 3355 } 3356 3357 port_priv = netdev_priv(port_netdev); 3358 port_priv->netdev = port_netdev; 3359 port_priv->ethsw_data = ethsw; 3360 3361 mutex_init(&port_priv->mac_lock); 3362 3363 port_priv->idx = port_idx; 3364 port_priv->stp_state = BR_STATE_FORWARDING; 3365 3366 SET_NETDEV_DEV(port_netdev, dev); 3367 port_netdev->netdev_ops = &dpaa2_switch_port_ops; 3368 port_netdev->ethtool_ops = &dpaa2_switch_port_ethtool_ops; 3369 3370 port_netdev->needed_headroom = DPAA2_SWITCH_NEEDED_HEADROOM; 3371 3372 port_priv->bcast_flood = true; 3373 port_priv->ucast_flood = true; 3374 3375 /* Set MTU limits */ 3376 port_netdev->min_mtu = ETH_MIN_MTU; 3377 port_netdev->max_mtu = ETHSW_MAX_FRAME_LENGTH; 3378 3379 /* Populate the private port structure so that later calls to 3380 * dpaa2_switch_port_init() can use it. 3381 */ 3382 ethsw->ports[port_idx] = port_priv; 3383 3384 /* The DPAA2 switch's ingress path depends on the VLAN table, 3385 * thus we are not able to disable VLAN filtering. 3386 */ 3387 port_netdev->features = NETIF_F_HW_VLAN_CTAG_FILTER | 3388 NETIF_F_HW_VLAN_STAG_FILTER | 3389 NETIF_F_HW_TC; 3390 port_netdev->priv_flags |= IFF_LIVE_ADDR_CHANGE; 3391 3392 err = dpaa2_switch_port_init(port_priv, port_idx); 3393 if (err) 3394 goto err_port_probe; 3395 3396 err = dpaa2_switch_port_set_mac_addr(port_priv); 3397 if (err) 3398 goto err_port_probe; 3399 3400 err = dpaa2_switch_port_set_learning(port_priv, false); 3401 if (err) 3402 goto err_port_probe; 3403 port_priv->learn_ena = false; 3404 3405 err = dpaa2_switch_port_connect_mac(port_priv); 3406 if (err) 3407 goto err_port_probe; 3408 3409 return 0; 3410 3411 err_port_probe: 3412 free_netdev(port_netdev); 3413 ethsw->ports[port_idx] = NULL; 3414 3415 return err; 3416 } 3417 3418 static int dpaa2_switch_probe(struct fsl_mc_device *sw_dev) 3419 { 3420 struct device *dev = &sw_dev->dev; 3421 struct ethsw_core *ethsw; 3422 int i, err; 3423 3424 /* Allocate switch core*/ 3425 ethsw = kzalloc_obj(*ethsw); 3426 3427 if (!ethsw) 3428 return -ENOMEM; 3429 3430 ethsw->dev = dev; 3431 ethsw->iommu_domain = iommu_get_domain_for_dev(dev); 3432 dev_set_drvdata(dev, ethsw); 3433 3434 err = fsl_mc_portal_allocate(sw_dev, FSL_MC_IO_ATOMIC_CONTEXT_PORTAL, 3435 ðsw->mc_io); 3436 if (err) { 3437 if (err == -ENXIO) 3438 err = -EPROBE_DEFER; 3439 else 3440 dev_err(dev, "fsl_mc_portal_allocate err %d\n", err); 3441 goto err_free_drvdata; 3442 } 3443 3444 err = dpaa2_switch_init(sw_dev); 3445 if (err) 3446 goto err_free_cmdport; 3447 3448 ethsw->ports = kzalloc_objs(*ethsw->ports, ethsw->sw_attr.num_ifs); 3449 if (!(ethsw->ports)) { 3450 err = -ENOMEM; 3451 goto err_teardown; 3452 } 3453 3454 ethsw->fdbs = kzalloc_objs(*ethsw->fdbs, ethsw->sw_attr.num_ifs); 3455 if (!ethsw->fdbs) { 3456 err = -ENOMEM; 3457 goto err_free_ports; 3458 } 3459 3460 ethsw->filter_blocks = kzalloc_objs(*ethsw->filter_blocks, 3461 ethsw->sw_attr.num_ifs); 3462 if (!ethsw->filter_blocks) { 3463 err = -ENOMEM; 3464 goto err_free_fdbs; 3465 } 3466 3467 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) { 3468 err = dpaa2_switch_probe_port(ethsw, i); 3469 if (err) 3470 goto err_free_netdev; 3471 } 3472 3473 /* Add a NAPI instance for each of the Rx queues. The first port's 3474 * net_device will be associated with the instances since we do not have 3475 * different queues for each switch ports. 3476 */ 3477 for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++) 3478 netif_napi_add(ethsw->ports[0]->netdev, ðsw->fq[i].napi, 3479 dpaa2_switch_poll); 3480 3481 /* Setup IRQs */ 3482 err = dpaa2_switch_setup_irqs(sw_dev); 3483 if (err) 3484 goto err_stop; 3485 3486 /* By convention, if the mirror port is equal to the number of switch 3487 * interfaces, then mirroring of any kind is disabled. 3488 */ 3489 ethsw->mirror_port = ethsw->sw_attr.num_ifs; 3490 3491 /* Register the netdev only when the entire setup is done and the 3492 * switch port interfaces are ready to receive traffic 3493 */ 3494 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) { 3495 err = register_netdev(ethsw->ports[i]->netdev); 3496 if (err < 0) { 3497 dev_err(dev, "register_netdev error %d\n", err); 3498 goto err_unregister_ports; 3499 } 3500 } 3501 3502 return 0; 3503 3504 err_unregister_ports: 3505 for (i--; i >= 0; i--) 3506 unregister_netdev(ethsw->ports[i]->netdev); 3507 dpaa2_switch_teardown_irqs(sw_dev); 3508 err_stop: 3509 dpsw_disable(ethsw->mc_io, 0, ethsw->dpsw_handle); 3510 err_free_netdev: 3511 for (i--; i >= 0; i--) 3512 dpaa2_switch_remove_port(ethsw, i); 3513 kfree(ethsw->filter_blocks); 3514 err_free_fdbs: 3515 kfree(ethsw->fdbs); 3516 err_free_ports: 3517 kfree(ethsw->ports); 3518 3519 err_teardown: 3520 dpaa2_switch_teardown(sw_dev); 3521 3522 err_free_cmdport: 3523 fsl_mc_portal_free(ethsw->mc_io); 3524 3525 err_free_drvdata: 3526 kfree(ethsw); 3527 dev_set_drvdata(dev, NULL); 3528 3529 return err; 3530 } 3531 3532 static const struct fsl_mc_device_id dpaa2_switch_match_id_table[] = { 3533 { 3534 .vendor = FSL_MC_VENDOR_FREESCALE, 3535 .obj_type = "dpsw", 3536 }, 3537 { .vendor = 0x0 } 3538 }; 3539 MODULE_DEVICE_TABLE(fslmc, dpaa2_switch_match_id_table); 3540 3541 static struct fsl_mc_driver dpaa2_switch_drv = { 3542 .driver = { 3543 .name = KBUILD_MODNAME, 3544 }, 3545 .probe = dpaa2_switch_probe, 3546 .remove = dpaa2_switch_remove, 3547 .match_id_table = dpaa2_switch_match_id_table 3548 }; 3549 3550 static struct notifier_block dpaa2_switch_port_nb __read_mostly = { 3551 .notifier_call = dpaa2_switch_port_netdevice_event, 3552 }; 3553 3554 static struct notifier_block dpaa2_switch_port_switchdev_nb = { 3555 .notifier_call = dpaa2_switch_port_event, 3556 }; 3557 3558 static struct notifier_block dpaa2_switch_port_switchdev_blocking_nb = { 3559 .notifier_call = dpaa2_switch_port_blocking_event, 3560 }; 3561 3562 static int dpaa2_switch_register_notifiers(void) 3563 { 3564 int err; 3565 3566 err = register_netdevice_notifier(&dpaa2_switch_port_nb); 3567 if (err) { 3568 pr_err("dpaa2-switch: failed to register net_device notifier (%d)\n", err); 3569 return err; 3570 } 3571 3572 err = register_switchdev_notifier(&dpaa2_switch_port_switchdev_nb); 3573 if (err) { 3574 pr_err("dpaa2-switch: failed to register switchdev notifier (%d)\n", err); 3575 goto err_switchdev_nb; 3576 } 3577 3578 err = register_switchdev_blocking_notifier(&dpaa2_switch_port_switchdev_blocking_nb); 3579 if (err) { 3580 pr_err("dpaa2-switch: failed to register switchdev blocking notifier (%d)\n", err); 3581 goto err_switchdev_blocking_nb; 3582 } 3583 3584 return 0; 3585 3586 err_switchdev_blocking_nb: 3587 unregister_switchdev_notifier(&dpaa2_switch_port_switchdev_nb); 3588 err_switchdev_nb: 3589 unregister_netdevice_notifier(&dpaa2_switch_port_nb); 3590 3591 return err; 3592 } 3593 3594 static void dpaa2_switch_unregister_notifiers(void) 3595 { 3596 int err; 3597 3598 err = unregister_switchdev_blocking_notifier(&dpaa2_switch_port_switchdev_blocking_nb); 3599 if (err) 3600 pr_err("dpaa2-switch: failed to unregister switchdev blocking notifier (%d)\n", 3601 err); 3602 3603 err = unregister_switchdev_notifier(&dpaa2_switch_port_switchdev_nb); 3604 if (err) 3605 pr_err("dpaa2-switch: failed to unregister switchdev notifier (%d)\n", err); 3606 3607 err = unregister_netdevice_notifier(&dpaa2_switch_port_nb); 3608 if (err) 3609 pr_err("dpaa2-switch: failed to unregister net_device notifier (%d)\n", err); 3610 } 3611 3612 static int __init dpaa2_switch_driver_init(void) 3613 { 3614 int err; 3615 3616 err = fsl_mc_driver_register(&dpaa2_switch_drv); 3617 if (err) 3618 return err; 3619 3620 err = dpaa2_switch_register_notifiers(); 3621 if (err) { 3622 fsl_mc_driver_unregister(&dpaa2_switch_drv); 3623 return err; 3624 } 3625 3626 return 0; 3627 } 3628 3629 static void __exit dpaa2_switch_driver_exit(void) 3630 { 3631 dpaa2_switch_unregister_notifiers(); 3632 fsl_mc_driver_unregister(&dpaa2_switch_drv); 3633 } 3634 3635 module_init(dpaa2_switch_driver_init); 3636 module_exit(dpaa2_switch_driver_exit); 3637 3638 MODULE_LICENSE("GPL v2"); 3639 MODULE_DESCRIPTION("DPAA2 Ethernet Switch Driver"); 3640