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 err; 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 ethsw_port_priv *port_priv; 2237 struct netlink_ext_ack *extack; 2238 struct net_device *upper_dev; 2239 int err; 2240 2241 if (!dpaa2_switch_port_dev_check(netdev)) 2242 return 0; 2243 2244 extack = netdev_notifier_info_to_extack(&info->info); 2245 upper_dev = info->upper_dev; 2246 if (netif_is_bridge_master(upper_dev)) { 2247 err = dpaa2_switch_prechangeupper_sanity_checks(netdev, 2248 upper_dev, 2249 extack); 2250 if (err) 2251 return err; 2252 2253 if (!info->linking) 2254 dpaa2_switch_port_pre_bridge_leave(netdev); 2255 } else if (is_vlan_dev(upper_dev)) { 2256 port_priv = netdev_priv(netdev); 2257 if (port_priv->fdb->bridge_dev) { 2258 NL_SET_ERR_MSG_MOD(extack, 2259 "Cannot accept VLAN uppers while bridged"); 2260 return -EOPNOTSUPP; 2261 } 2262 } 2263 2264 return 0; 2265 } 2266 2267 static int dpaa2_switch_port_changeupper(struct net_device *netdev, 2268 struct netdev_notifier_changeupper_info *info) 2269 { 2270 struct netlink_ext_ack *extack; 2271 struct net_device *upper_dev; 2272 2273 if (!dpaa2_switch_port_dev_check(netdev)) 2274 return 0; 2275 2276 extack = netdev_notifier_info_to_extack(&info->info); 2277 2278 upper_dev = info->upper_dev; 2279 if (netif_is_bridge_master(upper_dev)) { 2280 if (info->linking) 2281 return dpaa2_switch_port_bridge_join(netdev, 2282 upper_dev, 2283 extack); 2284 else 2285 return dpaa2_switch_port_bridge_leave(netdev); 2286 } 2287 2288 return 0; 2289 } 2290 2291 static int dpaa2_switch_port_netdevice_event(struct notifier_block *nb, 2292 unsigned long event, void *ptr) 2293 { 2294 struct net_device *netdev = netdev_notifier_info_to_dev(ptr); 2295 int err = 0; 2296 2297 switch (event) { 2298 case NETDEV_PRECHANGEUPPER: 2299 err = dpaa2_switch_port_prechangeupper(netdev, ptr); 2300 if (err) 2301 return notifier_from_errno(err); 2302 2303 break; 2304 case NETDEV_CHANGEUPPER: 2305 err = dpaa2_switch_port_changeupper(netdev, ptr); 2306 if (err) 2307 return notifier_from_errno(err); 2308 2309 break; 2310 } 2311 2312 return NOTIFY_DONE; 2313 } 2314 2315 struct ethsw_switchdev_event_work { 2316 struct work_struct work; 2317 struct switchdev_notifier_fdb_info fdb_info; 2318 struct net_device *dev; 2319 unsigned long event; 2320 }; 2321 2322 static void dpaa2_switch_event_work(struct work_struct *work) 2323 { 2324 struct ethsw_switchdev_event_work *switchdev_work = 2325 container_of(work, struct ethsw_switchdev_event_work, work); 2326 struct net_device *dev = switchdev_work->dev; 2327 struct switchdev_notifier_fdb_info *fdb_info; 2328 int err; 2329 2330 rtnl_lock(); 2331 fdb_info = &switchdev_work->fdb_info; 2332 2333 switch (switchdev_work->event) { 2334 case SWITCHDEV_FDB_ADD_TO_DEVICE: 2335 if (!fdb_info->added_by_user || fdb_info->is_local) 2336 break; 2337 if (is_unicast_ether_addr(fdb_info->addr)) 2338 err = dpaa2_switch_port_fdb_add_uc(netdev_priv(dev), 2339 fdb_info->addr); 2340 else 2341 err = dpaa2_switch_port_fdb_add_mc(netdev_priv(dev), 2342 fdb_info->addr); 2343 if (err) 2344 break; 2345 fdb_info->offloaded = true; 2346 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev, 2347 &fdb_info->info, NULL); 2348 break; 2349 case SWITCHDEV_FDB_DEL_TO_DEVICE: 2350 if (!fdb_info->added_by_user || fdb_info->is_local) 2351 break; 2352 if (is_unicast_ether_addr(fdb_info->addr)) 2353 dpaa2_switch_port_fdb_del_uc(netdev_priv(dev), fdb_info->addr); 2354 else 2355 dpaa2_switch_port_fdb_del_mc(netdev_priv(dev), fdb_info->addr); 2356 break; 2357 } 2358 2359 rtnl_unlock(); 2360 kfree(switchdev_work->fdb_info.addr); 2361 kfree(switchdev_work); 2362 dev_put(dev); 2363 } 2364 2365 /* Called under rcu_read_lock() */ 2366 static int dpaa2_switch_port_event(struct notifier_block *nb, 2367 unsigned long event, void *ptr) 2368 { 2369 struct net_device *dev = switchdev_notifier_info_to_dev(ptr); 2370 struct ethsw_port_priv *port_priv = netdev_priv(dev); 2371 struct ethsw_switchdev_event_work *switchdev_work; 2372 struct switchdev_notifier_fdb_info *fdb_info = ptr; 2373 struct ethsw_core *ethsw = port_priv->ethsw_data; 2374 2375 if (event == SWITCHDEV_PORT_ATTR_SET) 2376 return dpaa2_switch_port_attr_set_event(dev, ptr); 2377 2378 if (!dpaa2_switch_port_dev_check(dev)) 2379 return NOTIFY_DONE; 2380 2381 switchdev_work = kzalloc_obj(*switchdev_work, GFP_ATOMIC); 2382 if (!switchdev_work) 2383 return NOTIFY_BAD; 2384 2385 INIT_WORK(&switchdev_work->work, dpaa2_switch_event_work); 2386 switchdev_work->dev = dev; 2387 switchdev_work->event = event; 2388 2389 switch (event) { 2390 case SWITCHDEV_FDB_ADD_TO_DEVICE: 2391 case SWITCHDEV_FDB_DEL_TO_DEVICE: 2392 memcpy(&switchdev_work->fdb_info, ptr, 2393 sizeof(switchdev_work->fdb_info)); 2394 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC); 2395 if (!switchdev_work->fdb_info.addr) 2396 goto err_addr_alloc; 2397 2398 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr, 2399 fdb_info->addr); 2400 2401 /* Take a reference on the device to avoid being freed. */ 2402 dev_hold(dev); 2403 break; 2404 default: 2405 kfree(switchdev_work); 2406 return NOTIFY_DONE; 2407 } 2408 2409 queue_work(ethsw->workqueue, &switchdev_work->work); 2410 2411 return NOTIFY_DONE; 2412 2413 err_addr_alloc: 2414 kfree(switchdev_work); 2415 return NOTIFY_BAD; 2416 } 2417 2418 static int dpaa2_switch_port_obj_event(unsigned long event, 2419 struct net_device *netdev, 2420 struct switchdev_notifier_port_obj_info *port_obj_info) 2421 { 2422 int err = -EOPNOTSUPP; 2423 2424 if (!dpaa2_switch_port_dev_check(netdev)) 2425 return NOTIFY_DONE; 2426 2427 switch (event) { 2428 case SWITCHDEV_PORT_OBJ_ADD: 2429 err = dpaa2_switch_port_obj_add(netdev, port_obj_info->obj); 2430 break; 2431 case SWITCHDEV_PORT_OBJ_DEL: 2432 err = dpaa2_switch_port_obj_del(netdev, port_obj_info->obj); 2433 break; 2434 } 2435 2436 port_obj_info->handled = true; 2437 return notifier_from_errno(err); 2438 } 2439 2440 static int dpaa2_switch_port_blocking_event(struct notifier_block *nb, 2441 unsigned long event, void *ptr) 2442 { 2443 struct net_device *dev = switchdev_notifier_info_to_dev(ptr); 2444 2445 switch (event) { 2446 case SWITCHDEV_PORT_OBJ_ADD: 2447 case SWITCHDEV_PORT_OBJ_DEL: 2448 return dpaa2_switch_port_obj_event(event, dev, ptr); 2449 case SWITCHDEV_PORT_ATTR_SET: 2450 return dpaa2_switch_port_attr_set_event(dev, ptr); 2451 } 2452 2453 return NOTIFY_DONE; 2454 } 2455 2456 /* Build a linear skb based on a single-buffer frame descriptor */ 2457 static struct sk_buff *dpaa2_switch_build_linear_skb(struct ethsw_core *ethsw, 2458 const struct dpaa2_fd *fd, 2459 void *fd_vaddr) 2460 { 2461 u16 fd_offset = dpaa2_fd_get_offset(fd); 2462 u32 fd_length = dpaa2_fd_get_len(fd); 2463 struct device *dev = ethsw->dev; 2464 struct sk_buff *skb = NULL; 2465 2466 skb = build_skb(fd_vaddr, DPAA2_SWITCH_RX_BUF_SIZE + 2467 SKB_DATA_ALIGN(sizeof(struct skb_shared_info))); 2468 if (unlikely(!skb)) { 2469 dev_err(dev, "build_skb() failed\n"); 2470 return NULL; 2471 } 2472 2473 skb_reserve(skb, fd_offset); 2474 skb_put(skb, fd_length); 2475 2476 ethsw->buf_count--; 2477 2478 return skb; 2479 } 2480 2481 static void dpaa2_switch_tx_conf(struct dpaa2_switch_fq *fq, 2482 const struct dpaa2_fd *fd) 2483 { 2484 dpaa2_switch_free_fd(fq->ethsw, fd); 2485 } 2486 2487 static void dpaa2_switch_rx(struct dpaa2_switch_fq *fq, 2488 const struct dpaa2_fd *fd) 2489 { 2490 dma_addr_t addr = dpaa2_fd_get_addr(fd); 2491 struct ethsw_core *ethsw = fq->ethsw; 2492 struct ethsw_port_priv *port_priv; 2493 struct net_device *netdev; 2494 struct vlan_ethhdr *hdr; 2495 struct sk_buff *skb; 2496 u16 vlan_tci, vid; 2497 int if_id, err; 2498 void *vaddr; 2499 2500 vaddr = dpaa2_iova_to_virt(ethsw->iommu_domain, addr); 2501 dma_unmap_page(ethsw->dev, addr, DPAA2_SWITCH_RX_BUF_SIZE, 2502 DMA_FROM_DEVICE); 2503 2504 /* get switch ingress interface ID */ 2505 if_id = upper_32_bits(dpaa2_fd_get_flc(fd)) & 0x0000FFFF; 2506 if (if_id >= ethsw->sw_attr.num_ifs) { 2507 dev_err(ethsw->dev, "Frame received from unknown interface!\n"); 2508 goto err_free_fd; 2509 } 2510 port_priv = ethsw->ports[if_id]; 2511 netdev = port_priv->netdev; 2512 2513 /* build the SKB based on the FD received */ 2514 if (dpaa2_fd_get_format(fd) != dpaa2_fd_single) { 2515 if (net_ratelimit()) { 2516 netdev_err(netdev, "Received invalid frame format\n"); 2517 goto err_free_fd; 2518 } 2519 } 2520 2521 skb = dpaa2_switch_build_linear_skb(ethsw, fd, vaddr); 2522 if (unlikely(!skb)) 2523 goto err_free_fd; 2524 2525 skb_reset_mac_header(skb); 2526 2527 /* Remove the VLAN header if the packet that we just received has a vid 2528 * equal to the port PVIDs. Since the dpaa2-switch can operate only in 2529 * VLAN-aware mode and no alterations are made on the packet when it's 2530 * redirected/mirrored to the control interface, we are sure that there 2531 * will always be a VLAN header present. 2532 */ 2533 hdr = vlan_eth_hdr(skb); 2534 vid = ntohs(hdr->h_vlan_TCI) & VLAN_VID_MASK; 2535 if (vid == port_priv->pvid) { 2536 err = __skb_vlan_pop(skb, &vlan_tci); 2537 if (err) { 2538 dev_info(ethsw->dev, "__skb_vlan_pop() returned %d", err); 2539 kfree_skb(skb); 2540 return; 2541 } 2542 } 2543 2544 skb->dev = netdev; 2545 skb->protocol = eth_type_trans(skb, skb->dev); 2546 2547 /* Setup the offload_fwd_mark only if the port is under a bridge */ 2548 skb->offload_fwd_mark = !!(port_priv->fdb->bridge_dev); 2549 2550 netif_receive_skb(skb); 2551 2552 return; 2553 2554 err_free_fd: 2555 free_pages((unsigned long)vaddr, 0); 2556 } 2557 2558 static void dpaa2_switch_detect_features(struct ethsw_core *ethsw) 2559 { 2560 ethsw->features = 0; 2561 2562 if (ethsw->major > 8 || (ethsw->major == 8 && ethsw->minor >= 6)) 2563 ethsw->features |= ETHSW_FEATURE_MAC_ADDR; 2564 } 2565 2566 static int dpaa2_switch_setup_fqs(struct ethsw_core *ethsw) 2567 { 2568 struct dpsw_ctrl_if_attr ctrl_if_attr; 2569 struct device *dev = ethsw->dev; 2570 int i = 0; 2571 int err; 2572 2573 err = dpsw_ctrl_if_get_attributes(ethsw->mc_io, 0, ethsw->dpsw_handle, 2574 &ctrl_if_attr); 2575 if (err) { 2576 dev_err(dev, "dpsw_ctrl_if_get_attributes() = %d\n", err); 2577 return err; 2578 } 2579 2580 ethsw->fq[i].fqid = ctrl_if_attr.rx_fqid; 2581 ethsw->fq[i].ethsw = ethsw; 2582 ethsw->fq[i++].type = DPSW_QUEUE_RX; 2583 2584 ethsw->fq[i].fqid = ctrl_if_attr.tx_err_conf_fqid; 2585 ethsw->fq[i].ethsw = ethsw; 2586 ethsw->fq[i++].type = DPSW_QUEUE_TX_ERR_CONF; 2587 2588 return 0; 2589 } 2590 2591 /* Free buffers acquired from the buffer pool or which were meant to 2592 * be released in the pool 2593 */ 2594 static void dpaa2_switch_free_bufs(struct ethsw_core *ethsw, u64 *buf_array, int count) 2595 { 2596 struct device *dev = ethsw->dev; 2597 void *vaddr; 2598 int i; 2599 2600 for (i = 0; i < count; i++) { 2601 vaddr = dpaa2_iova_to_virt(ethsw->iommu_domain, buf_array[i]); 2602 dma_unmap_page(dev, buf_array[i], DPAA2_SWITCH_RX_BUF_SIZE, 2603 DMA_FROM_DEVICE); 2604 free_pages((unsigned long)vaddr, 0); 2605 } 2606 } 2607 2608 /* Perform a single release command to add buffers 2609 * to the specified buffer pool 2610 */ 2611 static int dpaa2_switch_add_bufs(struct ethsw_core *ethsw, u16 bpid) 2612 { 2613 struct device *dev = ethsw->dev; 2614 u64 buf_array[BUFS_PER_CMD]; 2615 struct page *page; 2616 int retries = 0; 2617 dma_addr_t addr; 2618 int err; 2619 int i; 2620 2621 for (i = 0; i < BUFS_PER_CMD; i++) { 2622 /* Allocate one page for each Rx buffer. WRIOP sees 2623 * the entire page except for a tailroom reserved for 2624 * skb shared info 2625 */ 2626 page = dev_alloc_pages(0); 2627 if (!page) { 2628 dev_err(dev, "buffer allocation failed\n"); 2629 goto err_alloc; 2630 } 2631 2632 addr = dma_map_page(dev, page, 0, DPAA2_SWITCH_RX_BUF_SIZE, 2633 DMA_FROM_DEVICE); 2634 if (dma_mapping_error(dev, addr)) { 2635 dev_err(dev, "dma_map_single() failed\n"); 2636 goto err_map; 2637 } 2638 buf_array[i] = addr; 2639 } 2640 2641 release_bufs: 2642 /* In case the portal is busy, retry until successful or 2643 * max retries hit. 2644 */ 2645 while ((err = dpaa2_io_service_release(NULL, bpid, 2646 buf_array, i)) == -EBUSY) { 2647 if (retries++ >= DPAA2_SWITCH_SWP_BUSY_RETRIES) 2648 break; 2649 2650 cpu_relax(); 2651 } 2652 2653 /* If release command failed, clean up and bail out. */ 2654 if (err) { 2655 dpaa2_switch_free_bufs(ethsw, buf_array, i); 2656 return 0; 2657 } 2658 2659 return i; 2660 2661 err_map: 2662 __free_pages(page, 0); 2663 err_alloc: 2664 /* If we managed to allocate at least some buffers, 2665 * release them to hardware 2666 */ 2667 if (i) 2668 goto release_bufs; 2669 2670 return 0; 2671 } 2672 2673 static int dpaa2_switch_refill_bp(struct ethsw_core *ethsw) 2674 { 2675 int *count = ðsw->buf_count; 2676 int new_count; 2677 int err = 0; 2678 2679 if (unlikely(*count < DPAA2_ETHSW_REFILL_THRESH)) { 2680 do { 2681 new_count = dpaa2_switch_add_bufs(ethsw, ethsw->bpid); 2682 if (unlikely(!new_count)) { 2683 /* Out of memory; abort for now, we'll 2684 * try later on 2685 */ 2686 break; 2687 } 2688 *count += new_count; 2689 } while (*count < DPAA2_ETHSW_NUM_BUFS); 2690 2691 if (unlikely(*count < DPAA2_ETHSW_NUM_BUFS)) 2692 err = -ENOMEM; 2693 } 2694 2695 return err; 2696 } 2697 2698 static int dpaa2_switch_seed_bp(struct ethsw_core *ethsw) 2699 { 2700 int *count, ret, i; 2701 2702 for (i = 0; i < DPAA2_ETHSW_NUM_BUFS; i += BUFS_PER_CMD) { 2703 ret = dpaa2_switch_add_bufs(ethsw, ethsw->bpid); 2704 count = ðsw->buf_count; 2705 *count += ret; 2706 2707 if (unlikely(ret < BUFS_PER_CMD)) 2708 return -ENOMEM; 2709 } 2710 2711 return 0; 2712 } 2713 2714 static void dpaa2_switch_drain_bp(struct ethsw_core *ethsw) 2715 { 2716 u64 buf_array[BUFS_PER_CMD]; 2717 int ret; 2718 2719 do { 2720 ret = dpaa2_io_service_acquire(NULL, ethsw->bpid, 2721 buf_array, BUFS_PER_CMD); 2722 if (ret < 0) { 2723 dev_err(ethsw->dev, 2724 "dpaa2_io_service_acquire() = %d\n", ret); 2725 return; 2726 } 2727 dpaa2_switch_free_bufs(ethsw, buf_array, ret); 2728 2729 } while (ret); 2730 } 2731 2732 static int dpaa2_switch_setup_dpbp(struct ethsw_core *ethsw) 2733 { 2734 struct dpsw_ctrl_if_pools_cfg dpsw_ctrl_if_pools_cfg = { 0 }; 2735 struct device *dev = ethsw->dev; 2736 struct fsl_mc_device *dpbp_dev; 2737 struct dpbp_attr dpbp_attrs; 2738 int err; 2739 2740 err = fsl_mc_object_allocate(to_fsl_mc_device(dev), FSL_MC_POOL_DPBP, 2741 &dpbp_dev); 2742 if (err) { 2743 if (err == -ENXIO) 2744 err = -EPROBE_DEFER; 2745 else 2746 dev_err(dev, "DPBP device allocation failed\n"); 2747 return err; 2748 } 2749 ethsw->dpbp_dev = dpbp_dev; 2750 2751 err = dpbp_open(ethsw->mc_io, 0, dpbp_dev->obj_desc.id, 2752 &dpbp_dev->mc_handle); 2753 if (err) { 2754 dev_err(dev, "dpbp_open() failed\n"); 2755 goto err_open; 2756 } 2757 2758 err = dpbp_reset(ethsw->mc_io, 0, dpbp_dev->mc_handle); 2759 if (err) { 2760 dev_err(dev, "dpbp_reset() failed\n"); 2761 goto err_reset; 2762 } 2763 2764 err = dpbp_enable(ethsw->mc_io, 0, dpbp_dev->mc_handle); 2765 if (err) { 2766 dev_err(dev, "dpbp_enable() failed\n"); 2767 goto err_enable; 2768 } 2769 2770 err = dpbp_get_attributes(ethsw->mc_io, 0, dpbp_dev->mc_handle, 2771 &dpbp_attrs); 2772 if (err) { 2773 dev_err(dev, "dpbp_get_attributes() failed\n"); 2774 goto err_get_attr; 2775 } 2776 2777 dpsw_ctrl_if_pools_cfg.num_dpbp = 1; 2778 dpsw_ctrl_if_pools_cfg.pools[0].dpbp_id = dpbp_attrs.id; 2779 dpsw_ctrl_if_pools_cfg.pools[0].buffer_size = DPAA2_SWITCH_RX_BUF_SIZE; 2780 dpsw_ctrl_if_pools_cfg.pools[0].backup_pool = 0; 2781 2782 err = dpsw_ctrl_if_set_pools(ethsw->mc_io, 0, ethsw->dpsw_handle, 2783 &dpsw_ctrl_if_pools_cfg); 2784 if (err) { 2785 dev_err(dev, "dpsw_ctrl_if_set_pools() failed\n"); 2786 goto err_get_attr; 2787 } 2788 ethsw->bpid = dpbp_attrs.bpid; 2789 2790 return 0; 2791 2792 err_get_attr: 2793 dpbp_disable(ethsw->mc_io, 0, dpbp_dev->mc_handle); 2794 err_enable: 2795 err_reset: 2796 dpbp_close(ethsw->mc_io, 0, dpbp_dev->mc_handle); 2797 err_open: 2798 fsl_mc_object_free(dpbp_dev); 2799 return err; 2800 } 2801 2802 static void dpaa2_switch_free_dpbp(struct ethsw_core *ethsw) 2803 { 2804 dpbp_disable(ethsw->mc_io, 0, ethsw->dpbp_dev->mc_handle); 2805 dpbp_close(ethsw->mc_io, 0, ethsw->dpbp_dev->mc_handle); 2806 fsl_mc_object_free(ethsw->dpbp_dev); 2807 } 2808 2809 static int dpaa2_switch_alloc_rings(struct ethsw_core *ethsw) 2810 { 2811 int i; 2812 2813 for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++) { 2814 ethsw->fq[i].store = 2815 dpaa2_io_store_create(DPAA2_SWITCH_STORE_SIZE, 2816 ethsw->dev); 2817 if (!ethsw->fq[i].store) { 2818 dev_err(ethsw->dev, "dpaa2_io_store_create failed\n"); 2819 while (--i >= 0) 2820 dpaa2_io_store_destroy(ethsw->fq[i].store); 2821 return -ENOMEM; 2822 } 2823 } 2824 2825 return 0; 2826 } 2827 2828 static void dpaa2_switch_destroy_rings(struct ethsw_core *ethsw) 2829 { 2830 int i; 2831 2832 for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++) 2833 dpaa2_io_store_destroy(ethsw->fq[i].store); 2834 } 2835 2836 static int dpaa2_switch_pull_fq(struct dpaa2_switch_fq *fq) 2837 { 2838 int err, retries = 0; 2839 2840 /* Try to pull from the FQ while the portal is busy and we didn't hit 2841 * the maximum number fo retries 2842 */ 2843 do { 2844 err = dpaa2_io_service_pull_fq(NULL, fq->fqid, fq->store); 2845 cpu_relax(); 2846 } while (err == -EBUSY && retries++ < DPAA2_SWITCH_SWP_BUSY_RETRIES); 2847 2848 if (unlikely(err)) 2849 dev_err(fq->ethsw->dev, "dpaa2_io_service_pull err %d", err); 2850 2851 return err; 2852 } 2853 2854 /* Consume all frames pull-dequeued into the store */ 2855 static int dpaa2_switch_store_consume(struct dpaa2_switch_fq *fq) 2856 { 2857 struct ethsw_core *ethsw = fq->ethsw; 2858 int cleaned = 0, is_last; 2859 struct dpaa2_dq *dq; 2860 int retries = 0; 2861 2862 do { 2863 /* Get the next available FD from the store */ 2864 dq = dpaa2_io_store_next(fq->store, &is_last); 2865 if (unlikely(!dq)) { 2866 if (retries++ >= DPAA2_SWITCH_SWP_BUSY_RETRIES) { 2867 dev_err_once(ethsw->dev, 2868 "No valid dequeue response\n"); 2869 return -ETIMEDOUT; 2870 } 2871 continue; 2872 } 2873 2874 if (fq->type == DPSW_QUEUE_RX) 2875 dpaa2_switch_rx(fq, dpaa2_dq_fd(dq)); 2876 else 2877 dpaa2_switch_tx_conf(fq, dpaa2_dq_fd(dq)); 2878 cleaned++; 2879 2880 } while (!is_last); 2881 2882 return cleaned; 2883 } 2884 2885 /* NAPI poll routine */ 2886 static int dpaa2_switch_poll(struct napi_struct *napi, int budget) 2887 { 2888 int err, cleaned = 0, store_cleaned, work_done; 2889 struct dpaa2_switch_fq *fq; 2890 int retries = 0; 2891 2892 fq = container_of(napi, struct dpaa2_switch_fq, napi); 2893 2894 do { 2895 err = dpaa2_switch_pull_fq(fq); 2896 if (unlikely(err)) 2897 break; 2898 2899 /* Refill pool if appropriate */ 2900 dpaa2_switch_refill_bp(fq->ethsw); 2901 2902 store_cleaned = dpaa2_switch_store_consume(fq); 2903 cleaned += store_cleaned; 2904 2905 if (cleaned >= budget) { 2906 work_done = budget; 2907 goto out; 2908 } 2909 2910 } while (store_cleaned); 2911 2912 /* We didn't consume the entire budget, so finish napi and re-enable 2913 * data availability notifications 2914 */ 2915 napi_complete_done(napi, cleaned); 2916 do { 2917 err = dpaa2_io_service_rearm(NULL, &fq->nctx); 2918 cpu_relax(); 2919 } while (err == -EBUSY && retries++ < DPAA2_SWITCH_SWP_BUSY_RETRIES); 2920 2921 work_done = max(cleaned, 1); 2922 out: 2923 2924 return work_done; 2925 } 2926 2927 static void dpaa2_switch_fqdan_cb(struct dpaa2_io_notification_ctx *nctx) 2928 { 2929 struct dpaa2_switch_fq *fq; 2930 2931 fq = container_of(nctx, struct dpaa2_switch_fq, nctx); 2932 2933 napi_schedule(&fq->napi); 2934 } 2935 2936 static int dpaa2_switch_setup_dpio(struct ethsw_core *ethsw) 2937 { 2938 struct dpsw_ctrl_if_queue_cfg queue_cfg; 2939 struct dpaa2_io_notification_ctx *nctx; 2940 int err, i, j; 2941 2942 for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++) { 2943 nctx = ðsw->fq[i].nctx; 2944 2945 /* Register a new software context for the FQID. 2946 * By using NULL as the first parameter, we specify that we do 2947 * not care on which cpu are interrupts received for this queue 2948 */ 2949 nctx->is_cdan = 0; 2950 nctx->id = ethsw->fq[i].fqid; 2951 nctx->desired_cpu = DPAA2_IO_ANY_CPU; 2952 nctx->cb = dpaa2_switch_fqdan_cb; 2953 err = dpaa2_io_service_register(NULL, nctx, ethsw->dev); 2954 if (err) { 2955 err = -EPROBE_DEFER; 2956 goto err_register; 2957 } 2958 2959 queue_cfg.options = DPSW_CTRL_IF_QUEUE_OPT_DEST | 2960 DPSW_CTRL_IF_QUEUE_OPT_USER_CTX; 2961 queue_cfg.dest_cfg.dest_type = DPSW_CTRL_IF_DEST_DPIO; 2962 queue_cfg.dest_cfg.dest_id = nctx->dpio_id; 2963 queue_cfg.dest_cfg.priority = 0; 2964 queue_cfg.user_ctx = nctx->qman64; 2965 2966 err = dpsw_ctrl_if_set_queue(ethsw->mc_io, 0, 2967 ethsw->dpsw_handle, 2968 ethsw->fq[i].type, 2969 &queue_cfg); 2970 if (err) 2971 goto err_set_queue; 2972 } 2973 2974 return 0; 2975 2976 err_set_queue: 2977 dpaa2_io_service_deregister(NULL, nctx, ethsw->dev); 2978 err_register: 2979 for (j = 0; j < i; j++) 2980 dpaa2_io_service_deregister(NULL, ðsw->fq[j].nctx, 2981 ethsw->dev); 2982 2983 return err; 2984 } 2985 2986 static void dpaa2_switch_free_dpio(struct ethsw_core *ethsw) 2987 { 2988 int i; 2989 2990 for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++) 2991 dpaa2_io_service_deregister(NULL, ðsw->fq[i].nctx, 2992 ethsw->dev); 2993 } 2994 2995 static int dpaa2_switch_ctrl_if_setup(struct ethsw_core *ethsw) 2996 { 2997 int err; 2998 2999 /* setup FQs for Rx and Tx Conf */ 3000 err = dpaa2_switch_setup_fqs(ethsw); 3001 if (err) 3002 return err; 3003 3004 /* setup the buffer pool needed on the Rx path */ 3005 err = dpaa2_switch_setup_dpbp(ethsw); 3006 if (err) 3007 return err; 3008 3009 err = dpaa2_switch_alloc_rings(ethsw); 3010 if (err) 3011 goto err_free_dpbp; 3012 3013 err = dpaa2_switch_setup_dpio(ethsw); 3014 if (err) 3015 goto err_destroy_rings; 3016 3017 err = dpaa2_switch_seed_bp(ethsw); 3018 if (err) 3019 goto err_deregister_dpio; 3020 3021 err = dpsw_ctrl_if_enable(ethsw->mc_io, 0, ethsw->dpsw_handle); 3022 if (err) { 3023 dev_err(ethsw->dev, "dpsw_ctrl_if_enable err %d\n", err); 3024 goto err_drain_dpbp; 3025 } 3026 3027 return 0; 3028 3029 err_drain_dpbp: 3030 dpaa2_switch_drain_bp(ethsw); 3031 err_deregister_dpio: 3032 dpaa2_switch_free_dpio(ethsw); 3033 err_destroy_rings: 3034 dpaa2_switch_destroy_rings(ethsw); 3035 err_free_dpbp: 3036 dpaa2_switch_free_dpbp(ethsw); 3037 3038 return err; 3039 } 3040 3041 static void dpaa2_switch_remove_port(struct ethsw_core *ethsw, 3042 u16 port_idx) 3043 { 3044 struct ethsw_port_priv *port_priv = ethsw->ports[port_idx]; 3045 3046 dpaa2_switch_port_disconnect_mac(port_priv); 3047 free_netdev(port_priv->netdev); 3048 ethsw->ports[port_idx] = NULL; 3049 } 3050 3051 static int dpaa2_switch_init(struct fsl_mc_device *sw_dev) 3052 { 3053 struct device *dev = &sw_dev->dev; 3054 struct ethsw_core *ethsw = dev_get_drvdata(dev); 3055 struct dpsw_vlan_if_cfg vcfg = {0}; 3056 struct dpsw_tci_cfg tci_cfg = {0}; 3057 struct dpsw_stp_cfg stp_cfg; 3058 int err; 3059 u16 i; 3060 3061 ethsw->dev_id = sw_dev->obj_desc.id; 3062 3063 err = dpsw_open(ethsw->mc_io, 0, ethsw->dev_id, ðsw->dpsw_handle); 3064 if (err) { 3065 dev_err(dev, "dpsw_open err %d\n", err); 3066 return err; 3067 } 3068 3069 err = dpsw_get_attributes(ethsw->mc_io, 0, ethsw->dpsw_handle, 3070 ðsw->sw_attr); 3071 if (err) { 3072 dev_err(dev, "dpsw_get_attributes err %d\n", err); 3073 goto err_close; 3074 } 3075 3076 if (!ethsw->sw_attr.num_ifs) { 3077 dev_err(dev, "DPSW device has no interfaces\n"); 3078 err = -ENODEV; 3079 goto err_close; 3080 } 3081 3082 if (ethsw->sw_attr.num_ifs >= DPSW_MAX_IF) { 3083 dev_err(dev, "DPSW num_ifs %u exceeds max %u\n", 3084 ethsw->sw_attr.num_ifs, DPSW_MAX_IF); 3085 err = -EINVAL; 3086 goto err_close; 3087 } 3088 3089 err = dpsw_get_api_version(ethsw->mc_io, 0, 3090 ðsw->major, 3091 ðsw->minor); 3092 if (err) { 3093 dev_err(dev, "dpsw_get_api_version err %d\n", err); 3094 goto err_close; 3095 } 3096 3097 /* Minimum supported DPSW version check */ 3098 if (ethsw->major < DPSW_MIN_VER_MAJOR || 3099 (ethsw->major == DPSW_MIN_VER_MAJOR && 3100 ethsw->minor < DPSW_MIN_VER_MINOR)) { 3101 dev_err(dev, "DPSW version %d:%d not supported. Use firmware 10.28.0 or greater.\n", 3102 ethsw->major, ethsw->minor); 3103 err = -EOPNOTSUPP; 3104 goto err_close; 3105 } 3106 3107 if (!dpaa2_switch_supports_cpu_traffic(ethsw)) { 3108 err = -EOPNOTSUPP; 3109 goto err_close; 3110 } 3111 3112 dpaa2_switch_detect_features(ethsw); 3113 3114 err = dpsw_reset(ethsw->mc_io, 0, ethsw->dpsw_handle); 3115 if (err) { 3116 dev_err(dev, "dpsw_reset err %d\n", err); 3117 goto err_close; 3118 } 3119 3120 stp_cfg.vlan_id = DEFAULT_VLAN_ID; 3121 stp_cfg.state = DPSW_STP_STATE_FORWARDING; 3122 3123 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) { 3124 err = dpsw_if_disable(ethsw->mc_io, 0, ethsw->dpsw_handle, i); 3125 if (err) { 3126 dev_err(dev, "dpsw_if_disable err %d\n", err); 3127 goto err_close; 3128 } 3129 3130 err = dpsw_if_set_stp(ethsw->mc_io, 0, ethsw->dpsw_handle, i, 3131 &stp_cfg); 3132 if (err) { 3133 dev_err(dev, "dpsw_if_set_stp err %d for port %d\n", 3134 err, i); 3135 goto err_close; 3136 } 3137 3138 /* Switch starts with all ports configured to VLAN 1. Need to 3139 * remove this setting to allow configuration at bridge join 3140 */ 3141 vcfg.num_ifs = 1; 3142 vcfg.if_id[0] = i; 3143 err = dpsw_vlan_remove_if_untagged(ethsw->mc_io, 0, ethsw->dpsw_handle, 3144 DEFAULT_VLAN_ID, &vcfg); 3145 if (err) { 3146 dev_err(dev, "dpsw_vlan_remove_if_untagged err %d\n", 3147 err); 3148 goto err_close; 3149 } 3150 3151 tci_cfg.vlan_id = 4095; 3152 err = dpsw_if_set_tci(ethsw->mc_io, 0, ethsw->dpsw_handle, i, &tci_cfg); 3153 if (err) { 3154 dev_err(dev, "dpsw_if_set_tci err %d\n", err); 3155 goto err_close; 3156 } 3157 3158 err = dpsw_vlan_remove_if(ethsw->mc_io, 0, ethsw->dpsw_handle, 3159 DEFAULT_VLAN_ID, &vcfg); 3160 if (err) { 3161 dev_err(dev, "dpsw_vlan_remove_if err %d\n", err); 3162 goto err_close; 3163 } 3164 } 3165 3166 err = dpsw_vlan_remove(ethsw->mc_io, 0, ethsw->dpsw_handle, DEFAULT_VLAN_ID); 3167 if (err) { 3168 dev_err(dev, "dpsw_vlan_remove err %d\n", err); 3169 goto err_close; 3170 } 3171 3172 ethsw->workqueue = alloc_ordered_workqueue("%s_%d_ordered", 3173 WQ_MEM_RECLAIM, "ethsw", 3174 ethsw->sw_attr.id); 3175 if (!ethsw->workqueue) { 3176 err = -ENOMEM; 3177 goto err_close; 3178 } 3179 3180 err = dpsw_fdb_remove(ethsw->mc_io, 0, ethsw->dpsw_handle, 0); 3181 if (err) 3182 goto err_destroy_ordered_workqueue; 3183 3184 err = dpaa2_switch_ctrl_if_setup(ethsw); 3185 if (err) 3186 goto err_destroy_ordered_workqueue; 3187 3188 return 0; 3189 3190 err_destroy_ordered_workqueue: 3191 destroy_workqueue(ethsw->workqueue); 3192 3193 err_close: 3194 dpsw_close(ethsw->mc_io, 0, ethsw->dpsw_handle); 3195 return err; 3196 } 3197 3198 /* Add an ACL to redirect frames with specific destination MAC address to 3199 * control interface 3200 */ 3201 static int dpaa2_switch_port_trap_mac_addr(struct ethsw_port_priv *port_priv, 3202 const char *mac) 3203 { 3204 struct dpaa2_switch_acl_entry acl_entry = {0}; 3205 3206 /* Match on the destination MAC address */ 3207 ether_addr_copy(acl_entry.key.match.l2_dest_mac, mac); 3208 eth_broadcast_addr(acl_entry.key.mask.l2_dest_mac); 3209 3210 /* Trap to CPU */ 3211 acl_entry.cfg.precedence = 0; 3212 acl_entry.cfg.result.action = DPSW_ACL_ACTION_REDIRECT_TO_CTRL_IF; 3213 3214 return dpaa2_switch_acl_entry_add(port_priv->filter_block, &acl_entry); 3215 } 3216 3217 static int dpaa2_switch_port_init(struct ethsw_port_priv *port_priv, u16 port) 3218 { 3219 const char stpa[ETH_ALEN] = {0x01, 0x80, 0xc2, 0x00, 0x00, 0x00}; 3220 struct switchdev_obj_port_vlan vlan = { 3221 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN, 3222 .vid = DEFAULT_VLAN_ID, 3223 .flags = BRIDGE_VLAN_INFO_UNTAGGED | BRIDGE_VLAN_INFO_PVID, 3224 }; 3225 struct net_device *netdev = port_priv->netdev; 3226 struct ethsw_core *ethsw = port_priv->ethsw_data; 3227 struct dpaa2_switch_filter_block *filter_block; 3228 struct dpsw_fdb_cfg fdb_cfg = {0}; 3229 struct dpsw_if_attr dpsw_if_attr; 3230 struct dpaa2_switch_fdb *fdb; 3231 struct dpsw_acl_cfg acl_cfg; 3232 u16 fdb_id, acl_tbl_id; 3233 int err; 3234 3235 /* Get the Tx queue for this specific port */ 3236 err = dpsw_if_get_attributes(ethsw->mc_io, 0, ethsw->dpsw_handle, 3237 port_priv->idx, &dpsw_if_attr); 3238 if (err) { 3239 netdev_err(netdev, "dpsw_if_get_attributes err %d\n", err); 3240 return err; 3241 } 3242 port_priv->tx_qdid = dpsw_if_attr.qdid; 3243 3244 /* Create a FDB table for this particular switch port */ 3245 fdb_cfg.num_fdb_entries = ethsw->sw_attr.max_fdb_entries / ethsw->sw_attr.num_ifs; 3246 err = dpsw_fdb_add(ethsw->mc_io, 0, ethsw->dpsw_handle, 3247 &fdb_id, &fdb_cfg); 3248 if (err) { 3249 netdev_err(netdev, "dpsw_fdb_add err %d\n", err); 3250 return err; 3251 } 3252 3253 /* Find an unused dpaa2_switch_fdb structure and use it */ 3254 fdb = dpaa2_switch_fdb_get_unused(ethsw); 3255 fdb->fdb_id = fdb_id; 3256 fdb->in_use = true; 3257 fdb->bridge_dev = NULL; 3258 port_priv->fdb = fdb; 3259 3260 /* We need to add VLAN 1 as the PVID on this port until it is under a 3261 * bridge since the DPAA2 switch is not able to handle the traffic in a 3262 * VLAN unaware fashion 3263 */ 3264 err = dpaa2_switch_port_vlans_add(netdev, &vlan); 3265 if (err) 3266 return err; 3267 3268 /* Setup the egress flooding domains (broadcast, unknown unicast */ 3269 err = dpaa2_switch_fdb_set_egress_flood(ethsw, port_priv->fdb->fdb_id); 3270 if (err) 3271 return err; 3272 3273 /* Create an ACL table to be used by this switch port */ 3274 acl_cfg.max_entries = DPAA2_ETHSW_PORT_MAX_ACL_ENTRIES; 3275 err = dpsw_acl_add(ethsw->mc_io, 0, ethsw->dpsw_handle, 3276 &acl_tbl_id, &acl_cfg); 3277 if (err) { 3278 netdev_err(netdev, "dpsw_acl_add err %d\n", err); 3279 return err; 3280 } 3281 3282 filter_block = dpaa2_switch_filter_block_get_unused(ethsw); 3283 filter_block->ethsw = ethsw; 3284 filter_block->acl_id = acl_tbl_id; 3285 filter_block->in_use = true; 3286 filter_block->num_acl_rules = 0; 3287 INIT_LIST_HEAD(&filter_block->acl_entries); 3288 INIT_LIST_HEAD(&filter_block->mirror_entries); 3289 3290 err = dpaa2_switch_port_acl_tbl_bind(port_priv, filter_block); 3291 if (err) 3292 return err; 3293 3294 err = dpaa2_switch_port_trap_mac_addr(port_priv, stpa); 3295 if (err) 3296 return err; 3297 3298 return err; 3299 } 3300 3301 static void dpaa2_switch_ctrl_if_teardown(struct ethsw_core *ethsw) 3302 { 3303 dpsw_ctrl_if_disable(ethsw->mc_io, 0, ethsw->dpsw_handle); 3304 dpaa2_switch_free_dpio(ethsw); 3305 dpaa2_switch_destroy_rings(ethsw); 3306 dpaa2_switch_drain_bp(ethsw); 3307 dpaa2_switch_free_dpbp(ethsw); 3308 } 3309 3310 static void dpaa2_switch_teardown(struct fsl_mc_device *sw_dev) 3311 { 3312 struct device *dev = &sw_dev->dev; 3313 struct ethsw_core *ethsw = dev_get_drvdata(dev); 3314 int err; 3315 3316 dpaa2_switch_ctrl_if_teardown(ethsw); 3317 3318 destroy_workqueue(ethsw->workqueue); 3319 3320 err = dpsw_close(ethsw->mc_io, 0, ethsw->dpsw_handle); 3321 if (err) 3322 dev_warn(dev, "dpsw_close err %d\n", err); 3323 } 3324 3325 static void dpaa2_switch_remove(struct fsl_mc_device *sw_dev) 3326 { 3327 struct ethsw_core *ethsw; 3328 struct device *dev; 3329 int i; 3330 3331 dev = &sw_dev->dev; 3332 ethsw = dev_get_drvdata(dev); 3333 3334 dpaa2_switch_teardown_irqs(sw_dev); 3335 3336 dpsw_disable(ethsw->mc_io, 0, ethsw->dpsw_handle); 3337 3338 /* Unregister all the netdevs so that they are brought down and the 3339 * shared NAPI instances gets disabled. 3340 */ 3341 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) 3342 unregister_netdev(ethsw->ports[i]->netdev); 3343 3344 for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++) 3345 netif_napi_del(ðsw->fq[i].napi); 3346 3347 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) 3348 dpaa2_switch_remove_port(ethsw, i); 3349 3350 kfree(ethsw->fdbs); 3351 kfree(ethsw->filter_blocks); 3352 kfree(ethsw->ports); 3353 3354 dpaa2_switch_teardown(sw_dev); 3355 3356 fsl_mc_portal_free(ethsw->mc_io); 3357 3358 kfree(ethsw); 3359 3360 dev_set_drvdata(dev, NULL); 3361 } 3362 3363 static int dpaa2_switch_probe_port(struct ethsw_core *ethsw, 3364 u16 port_idx) 3365 { 3366 struct ethsw_port_priv *port_priv; 3367 struct device *dev = ethsw->dev; 3368 struct net_device *port_netdev; 3369 int err; 3370 3371 port_netdev = alloc_etherdev(sizeof(struct ethsw_port_priv)); 3372 if (!port_netdev) { 3373 dev_err(dev, "alloc_etherdev error\n"); 3374 return -ENOMEM; 3375 } 3376 3377 port_priv = netdev_priv(port_netdev); 3378 port_priv->netdev = port_netdev; 3379 port_priv->ethsw_data = ethsw; 3380 3381 mutex_init(&port_priv->mac_lock); 3382 3383 port_priv->idx = port_idx; 3384 port_priv->stp_state = BR_STATE_FORWARDING; 3385 3386 SET_NETDEV_DEV(port_netdev, dev); 3387 port_netdev->netdev_ops = &dpaa2_switch_port_ops; 3388 port_netdev->ethtool_ops = &dpaa2_switch_port_ethtool_ops; 3389 3390 port_netdev->needed_headroom = DPAA2_SWITCH_NEEDED_HEADROOM; 3391 3392 port_priv->bcast_flood = true; 3393 port_priv->ucast_flood = true; 3394 3395 /* Set MTU limits */ 3396 port_netdev->min_mtu = ETH_MIN_MTU; 3397 port_netdev->max_mtu = ETHSW_MAX_FRAME_LENGTH; 3398 3399 /* Populate the private port structure so that later calls to 3400 * dpaa2_switch_port_init() can use it. 3401 */ 3402 ethsw->ports[port_idx] = port_priv; 3403 3404 /* The DPAA2 switch's ingress path depends on the VLAN table, 3405 * thus we are not able to disable VLAN filtering. 3406 */ 3407 port_netdev->features = NETIF_F_HW_VLAN_CTAG_FILTER | 3408 NETIF_F_HW_VLAN_STAG_FILTER | 3409 NETIF_F_HW_TC; 3410 port_netdev->priv_flags |= IFF_LIVE_ADDR_CHANGE; 3411 3412 err = dpaa2_switch_port_init(port_priv, port_idx); 3413 if (err) 3414 goto err_port_probe; 3415 3416 err = dpaa2_switch_port_set_mac_addr(port_priv); 3417 if (err) 3418 goto err_port_probe; 3419 3420 err = dpaa2_switch_port_set_learning(port_priv, false); 3421 if (err) 3422 goto err_port_probe; 3423 port_priv->learn_ena = false; 3424 3425 err = dpaa2_switch_port_connect_mac(port_priv); 3426 if (err) 3427 goto err_port_probe; 3428 3429 return 0; 3430 3431 err_port_probe: 3432 free_netdev(port_netdev); 3433 ethsw->ports[port_idx] = NULL; 3434 3435 return err; 3436 } 3437 3438 static int dpaa2_switch_probe(struct fsl_mc_device *sw_dev) 3439 { 3440 struct device *dev = &sw_dev->dev; 3441 struct ethsw_core *ethsw; 3442 int i, err; 3443 3444 /* Allocate switch core*/ 3445 ethsw = kzalloc_obj(*ethsw); 3446 3447 if (!ethsw) 3448 return -ENOMEM; 3449 3450 ethsw->dev = dev; 3451 ethsw->iommu_domain = iommu_get_domain_for_dev(dev); 3452 dev_set_drvdata(dev, ethsw); 3453 3454 err = fsl_mc_portal_allocate(sw_dev, FSL_MC_IO_ATOMIC_CONTEXT_PORTAL, 3455 ðsw->mc_io); 3456 if (err) { 3457 if (err == -ENXIO) 3458 err = -EPROBE_DEFER; 3459 else 3460 dev_err(dev, "fsl_mc_portal_allocate err %d\n", err); 3461 goto err_free_drvdata; 3462 } 3463 3464 err = dpaa2_switch_init(sw_dev); 3465 if (err) 3466 goto err_free_cmdport; 3467 3468 ethsw->ports = kzalloc_objs(*ethsw->ports, ethsw->sw_attr.num_ifs); 3469 if (!(ethsw->ports)) { 3470 err = -ENOMEM; 3471 goto err_teardown; 3472 } 3473 3474 ethsw->fdbs = kzalloc_objs(*ethsw->fdbs, ethsw->sw_attr.num_ifs); 3475 if (!ethsw->fdbs) { 3476 err = -ENOMEM; 3477 goto err_free_ports; 3478 } 3479 3480 ethsw->filter_blocks = kzalloc_objs(*ethsw->filter_blocks, 3481 ethsw->sw_attr.num_ifs); 3482 if (!ethsw->filter_blocks) { 3483 err = -ENOMEM; 3484 goto err_free_fdbs; 3485 } 3486 3487 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) { 3488 err = dpaa2_switch_probe_port(ethsw, i); 3489 if (err) 3490 goto err_free_netdev; 3491 } 3492 3493 /* Add a NAPI instance for each of the Rx queues. The first port's 3494 * net_device will be associated with the instances since we do not have 3495 * different queues for each switch ports. 3496 */ 3497 for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++) 3498 netif_napi_add(ethsw->ports[0]->netdev, ðsw->fq[i].napi, 3499 dpaa2_switch_poll); 3500 3501 /* Setup IRQs */ 3502 err = dpaa2_switch_setup_irqs(sw_dev); 3503 if (err) 3504 goto err_stop; 3505 3506 /* By convention, if the mirror port is equal to the number of switch 3507 * interfaces, then mirroring of any kind is disabled. 3508 */ 3509 ethsw->mirror_port = ethsw->sw_attr.num_ifs; 3510 3511 /* Register the netdev only when the entire setup is done and the 3512 * switch port interfaces are ready to receive traffic 3513 */ 3514 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) { 3515 err = register_netdev(ethsw->ports[i]->netdev); 3516 if (err < 0) { 3517 dev_err(dev, "register_netdev error %d\n", err); 3518 goto err_unregister_ports; 3519 } 3520 } 3521 3522 return 0; 3523 3524 err_unregister_ports: 3525 for (i--; i >= 0; i--) 3526 unregister_netdev(ethsw->ports[i]->netdev); 3527 dpaa2_switch_teardown_irqs(sw_dev); 3528 err_stop: 3529 dpsw_disable(ethsw->mc_io, 0, ethsw->dpsw_handle); 3530 err_free_netdev: 3531 for (i--; i >= 0; i--) 3532 dpaa2_switch_remove_port(ethsw, i); 3533 kfree(ethsw->filter_blocks); 3534 err_free_fdbs: 3535 kfree(ethsw->fdbs); 3536 err_free_ports: 3537 kfree(ethsw->ports); 3538 3539 err_teardown: 3540 dpaa2_switch_teardown(sw_dev); 3541 3542 err_free_cmdport: 3543 fsl_mc_portal_free(ethsw->mc_io); 3544 3545 err_free_drvdata: 3546 kfree(ethsw); 3547 dev_set_drvdata(dev, NULL); 3548 3549 return err; 3550 } 3551 3552 static const struct fsl_mc_device_id dpaa2_switch_match_id_table[] = { 3553 { 3554 .vendor = FSL_MC_VENDOR_FREESCALE, 3555 .obj_type = "dpsw", 3556 }, 3557 { .vendor = 0x0 } 3558 }; 3559 MODULE_DEVICE_TABLE(fslmc, dpaa2_switch_match_id_table); 3560 3561 static struct fsl_mc_driver dpaa2_switch_drv = { 3562 .driver = { 3563 .name = KBUILD_MODNAME, 3564 }, 3565 .probe = dpaa2_switch_probe, 3566 .remove = dpaa2_switch_remove, 3567 .match_id_table = dpaa2_switch_match_id_table 3568 }; 3569 3570 static struct notifier_block dpaa2_switch_port_nb __read_mostly = { 3571 .notifier_call = dpaa2_switch_port_netdevice_event, 3572 }; 3573 3574 static struct notifier_block dpaa2_switch_port_switchdev_nb = { 3575 .notifier_call = dpaa2_switch_port_event, 3576 }; 3577 3578 static struct notifier_block dpaa2_switch_port_switchdev_blocking_nb = { 3579 .notifier_call = dpaa2_switch_port_blocking_event, 3580 }; 3581 3582 static int dpaa2_switch_register_notifiers(void) 3583 { 3584 int err; 3585 3586 err = register_netdevice_notifier(&dpaa2_switch_port_nb); 3587 if (err) { 3588 pr_err("dpaa2-switch: failed to register net_device notifier (%d)\n", err); 3589 return err; 3590 } 3591 3592 err = register_switchdev_notifier(&dpaa2_switch_port_switchdev_nb); 3593 if (err) { 3594 pr_err("dpaa2-switch: failed to register switchdev notifier (%d)\n", err); 3595 goto err_switchdev_nb; 3596 } 3597 3598 err = register_switchdev_blocking_notifier(&dpaa2_switch_port_switchdev_blocking_nb); 3599 if (err) { 3600 pr_err("dpaa2-switch: failed to register switchdev blocking notifier (%d)\n", err); 3601 goto err_switchdev_blocking_nb; 3602 } 3603 3604 return 0; 3605 3606 err_switchdev_blocking_nb: 3607 unregister_switchdev_notifier(&dpaa2_switch_port_switchdev_nb); 3608 err_switchdev_nb: 3609 unregister_netdevice_notifier(&dpaa2_switch_port_nb); 3610 3611 return err; 3612 } 3613 3614 static void dpaa2_switch_unregister_notifiers(void) 3615 { 3616 int err; 3617 3618 err = unregister_switchdev_blocking_notifier(&dpaa2_switch_port_switchdev_blocking_nb); 3619 if (err) 3620 pr_err("dpaa2-switch: failed to unregister switchdev blocking notifier (%d)\n", 3621 err); 3622 3623 err = unregister_switchdev_notifier(&dpaa2_switch_port_switchdev_nb); 3624 if (err) 3625 pr_err("dpaa2-switch: failed to unregister switchdev notifier (%d)\n", err); 3626 3627 err = unregister_netdevice_notifier(&dpaa2_switch_port_nb); 3628 if (err) 3629 pr_err("dpaa2-switch: failed to unregister net_device notifier (%d)\n", err); 3630 } 3631 3632 static int __init dpaa2_switch_driver_init(void) 3633 { 3634 int err; 3635 3636 err = fsl_mc_driver_register(&dpaa2_switch_drv); 3637 if (err) 3638 return err; 3639 3640 err = dpaa2_switch_register_notifiers(); 3641 if (err) { 3642 fsl_mc_driver_unregister(&dpaa2_switch_drv); 3643 return err; 3644 } 3645 3646 return 0; 3647 } 3648 3649 static void __exit dpaa2_switch_driver_exit(void) 3650 { 3651 dpaa2_switch_unregister_notifiers(); 3652 fsl_mc_driver_unregister(&dpaa2_switch_drv); 3653 } 3654 3655 module_init(dpaa2_switch_driver_init); 3656 module_exit(dpaa2_switch_driver_exit); 3657 3658 MODULE_LICENSE("GPL v2"); 3659 MODULE_DESCRIPTION("DPAA2 Ethernet Switch Driver"); 3660