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