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/msi.h> 14 #include <linux/kthread.h> 15 #include <linux/workqueue.h> 16 #include <linux/iommu.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 u16 dpaa2_switch_port_set_fdb(struct ethsw_port_priv *port_priv, 44 struct net_device *bridge_dev) 45 { 46 struct ethsw_port_priv *other_port_priv = NULL; 47 struct dpaa2_switch_fdb *fdb; 48 struct net_device *other_dev; 49 struct list_head *iter; 50 51 /* If we leave a bridge (bridge_dev is NULL), find an unused 52 * FDB and use that. 53 */ 54 if (!bridge_dev) { 55 fdb = dpaa2_switch_fdb_get_unused(port_priv->ethsw_data); 56 57 /* If there is no unused FDB, we must be the last port that 58 * leaves the last bridge, all the others are standalone. We 59 * can just keep the FDB that we already have. 60 */ 61 62 if (!fdb) { 63 port_priv->fdb->bridge_dev = NULL; 64 return 0; 65 } 66 67 port_priv->fdb = fdb; 68 port_priv->fdb->in_use = true; 69 port_priv->fdb->bridge_dev = NULL; 70 return 0; 71 } 72 73 /* The below call to netdev_for_each_lower_dev() demands the RTNL lock 74 * being held. Assert on it so that it's easier to catch new code 75 * paths that reach this point without the RTNL lock. 76 */ 77 ASSERT_RTNL(); 78 79 /* If part of a bridge, use the FDB of the first dpaa2 switch interface 80 * to be present in that bridge 81 */ 82 netdev_for_each_lower_dev(bridge_dev, other_dev, iter) { 83 if (!dpaa2_switch_port_dev_check(other_dev)) 84 continue; 85 86 if (other_dev == port_priv->netdev) 87 continue; 88 89 other_port_priv = netdev_priv(other_dev); 90 break; 91 } 92 93 /* The current port is about to change its FDB to the one used by the 94 * first port that joined the bridge. 95 */ 96 if (other_port_priv) { 97 /* The previous FDB is about to become unused, since the 98 * interface is no longer standalone. 99 */ 100 port_priv->fdb->in_use = false; 101 port_priv->fdb->bridge_dev = NULL; 102 103 /* Get a reference to the new FDB */ 104 port_priv->fdb = other_port_priv->fdb; 105 } 106 107 /* Keep track of the new upper bridge device */ 108 port_priv->fdb->bridge_dev = bridge_dev; 109 110 return 0; 111 } 112 113 static void dpaa2_switch_fdb_get_flood_cfg(struct ethsw_core *ethsw, u16 fdb_id, 114 enum dpsw_flood_type type, 115 struct dpsw_egress_flood_cfg *cfg) 116 { 117 int i = 0, j; 118 119 memset(cfg, 0, sizeof(*cfg)); 120 121 /* Add all the DPAA2 switch ports found in the same bridging domain to 122 * the egress flooding domain 123 */ 124 for (j = 0; j < ethsw->sw_attr.num_ifs; j++) { 125 if (!ethsw->ports[j]) 126 continue; 127 if (ethsw->ports[j]->fdb->fdb_id != fdb_id) 128 continue; 129 130 cfg->if_id[i++] = ethsw->ports[j]->idx; 131 } 132 133 /* Add the CTRL interface to the egress flooding domain */ 134 cfg->if_id[i++] = ethsw->sw_attr.num_ifs; 135 136 cfg->fdb_id = fdb_id; 137 cfg->flood_type = type; 138 cfg->num_ifs = i; 139 } 140 141 static int dpaa2_switch_fdb_set_egress_flood(struct ethsw_core *ethsw, u16 fdb_id) 142 { 143 struct dpsw_egress_flood_cfg flood_cfg; 144 int err; 145 146 /* Setup broadcast flooding domain */ 147 dpaa2_switch_fdb_get_flood_cfg(ethsw, fdb_id, DPSW_BROADCAST, &flood_cfg); 148 err = dpsw_set_egress_flood(ethsw->mc_io, 0, ethsw->dpsw_handle, 149 &flood_cfg); 150 if (err) { 151 dev_err(ethsw->dev, "dpsw_set_egress_flood() = %d\n", err); 152 return err; 153 } 154 155 /* Setup unknown flooding domain */ 156 dpaa2_switch_fdb_get_flood_cfg(ethsw, fdb_id, DPSW_FLOODING, &flood_cfg); 157 err = dpsw_set_egress_flood(ethsw->mc_io, 0, ethsw->dpsw_handle, 158 &flood_cfg); 159 if (err) { 160 dev_err(ethsw->dev, "dpsw_set_egress_flood() = %d\n", err); 161 return err; 162 } 163 164 return 0; 165 } 166 167 static void *dpaa2_iova_to_virt(struct iommu_domain *domain, 168 dma_addr_t iova_addr) 169 { 170 phys_addr_t phys_addr; 171 172 phys_addr = domain ? iommu_iova_to_phys(domain, iova_addr) : iova_addr; 173 174 return phys_to_virt(phys_addr); 175 } 176 177 static int dpaa2_switch_add_vlan(struct ethsw_port_priv *port_priv, u16 vid) 178 { 179 struct ethsw_core *ethsw = port_priv->ethsw_data; 180 struct dpsw_vlan_cfg vcfg = {0}; 181 int err; 182 183 vcfg.fdb_id = dpaa2_switch_port_get_fdb_id(port_priv); 184 err = dpsw_vlan_add(ethsw->mc_io, 0, 185 ethsw->dpsw_handle, vid, &vcfg); 186 if (err) { 187 dev_err(ethsw->dev, "dpsw_vlan_add err %d\n", err); 188 return err; 189 } 190 ethsw->vlans[vid] = ETHSW_VLAN_MEMBER; 191 192 return 0; 193 } 194 195 static bool dpaa2_switch_port_is_up(struct ethsw_port_priv *port_priv) 196 { 197 struct net_device *netdev = port_priv->netdev; 198 struct dpsw_link_state state; 199 int err; 200 201 err = dpsw_if_get_link_state(port_priv->ethsw_data->mc_io, 0, 202 port_priv->ethsw_data->dpsw_handle, 203 port_priv->idx, &state); 204 if (err) { 205 netdev_err(netdev, "dpsw_if_get_link_state() err %d\n", err); 206 return true; 207 } 208 209 WARN_ONCE(state.up > 1, "Garbage read into link_state"); 210 211 return state.up ? true : false; 212 } 213 214 static int dpaa2_switch_port_set_pvid(struct ethsw_port_priv *port_priv, u16 pvid) 215 { 216 struct ethsw_core *ethsw = port_priv->ethsw_data; 217 struct net_device *netdev = port_priv->netdev; 218 struct dpsw_tci_cfg tci_cfg = { 0 }; 219 bool up; 220 int err, ret; 221 222 err = dpsw_if_get_tci(ethsw->mc_io, 0, ethsw->dpsw_handle, 223 port_priv->idx, &tci_cfg); 224 if (err) { 225 netdev_err(netdev, "dpsw_if_get_tci err %d\n", err); 226 return err; 227 } 228 229 tci_cfg.vlan_id = pvid; 230 231 /* Interface needs to be down to change PVID */ 232 up = dpaa2_switch_port_is_up(port_priv); 233 if (up) { 234 err = dpsw_if_disable(ethsw->mc_io, 0, 235 ethsw->dpsw_handle, 236 port_priv->idx); 237 if (err) { 238 netdev_err(netdev, "dpsw_if_disable err %d\n", err); 239 return err; 240 } 241 } 242 243 err = dpsw_if_set_tci(ethsw->mc_io, 0, ethsw->dpsw_handle, 244 port_priv->idx, &tci_cfg); 245 if (err) { 246 netdev_err(netdev, "dpsw_if_set_tci err %d\n", err); 247 goto set_tci_error; 248 } 249 250 /* Delete previous PVID info and mark the new one */ 251 port_priv->vlans[port_priv->pvid] &= ~ETHSW_VLAN_PVID; 252 port_priv->vlans[pvid] |= ETHSW_VLAN_PVID; 253 port_priv->pvid = pvid; 254 255 set_tci_error: 256 if (up) { 257 ret = dpsw_if_enable(ethsw->mc_io, 0, 258 ethsw->dpsw_handle, 259 port_priv->idx); 260 if (ret) { 261 netdev_err(netdev, "dpsw_if_enable err %d\n", ret); 262 return ret; 263 } 264 } 265 266 return err; 267 } 268 269 static int dpaa2_switch_port_add_vlan(struct ethsw_port_priv *port_priv, 270 u16 vid, u16 flags) 271 { 272 struct ethsw_core *ethsw = port_priv->ethsw_data; 273 struct net_device *netdev = port_priv->netdev; 274 struct dpsw_vlan_if_cfg vcfg = {0}; 275 int err; 276 277 if (port_priv->vlans[vid]) { 278 netdev_warn(netdev, "VLAN %d already configured\n", vid); 279 return -EEXIST; 280 } 281 282 /* If hit, this VLAN rule will lead the packet into the FDB table 283 * specified in the vlan configuration below 284 */ 285 vcfg.num_ifs = 1; 286 vcfg.if_id[0] = port_priv->idx; 287 vcfg.fdb_id = dpaa2_switch_port_get_fdb_id(port_priv); 288 vcfg.options |= DPSW_VLAN_ADD_IF_OPT_FDB_ID; 289 err = dpsw_vlan_add_if(ethsw->mc_io, 0, ethsw->dpsw_handle, vid, &vcfg); 290 if (err) { 291 netdev_err(netdev, "dpsw_vlan_add_if err %d\n", err); 292 return err; 293 } 294 295 port_priv->vlans[vid] = ETHSW_VLAN_MEMBER; 296 297 if (flags & BRIDGE_VLAN_INFO_UNTAGGED) { 298 err = dpsw_vlan_add_if_untagged(ethsw->mc_io, 0, 299 ethsw->dpsw_handle, 300 vid, &vcfg); 301 if (err) { 302 netdev_err(netdev, 303 "dpsw_vlan_add_if_untagged err %d\n", err); 304 return err; 305 } 306 port_priv->vlans[vid] |= ETHSW_VLAN_UNTAGGED; 307 } 308 309 if (flags & BRIDGE_VLAN_INFO_PVID) { 310 err = dpaa2_switch_port_set_pvid(port_priv, vid); 311 if (err) 312 return err; 313 } 314 315 return 0; 316 } 317 318 static int dpaa2_switch_port_set_stp_state(struct ethsw_port_priv *port_priv, u8 state) 319 { 320 struct dpsw_stp_cfg stp_cfg = { 321 .state = state, 322 }; 323 int err; 324 u16 vid; 325 326 if (!netif_running(port_priv->netdev) || state == port_priv->stp_state) 327 return 0; /* Nothing to do */ 328 329 for (vid = 0; vid <= VLAN_VID_MASK; vid++) { 330 if (port_priv->vlans[vid] & ETHSW_VLAN_MEMBER) { 331 stp_cfg.vlan_id = vid; 332 err = dpsw_if_set_stp(port_priv->ethsw_data->mc_io, 0, 333 port_priv->ethsw_data->dpsw_handle, 334 port_priv->idx, &stp_cfg); 335 if (err) { 336 netdev_err(port_priv->netdev, 337 "dpsw_if_set_stp err %d\n", err); 338 return err; 339 } 340 } 341 } 342 343 port_priv->stp_state = state; 344 345 return 0; 346 } 347 348 static int dpaa2_switch_dellink(struct ethsw_core *ethsw, u16 vid) 349 { 350 struct ethsw_port_priv *ppriv_local = NULL; 351 int i, err; 352 353 if (!ethsw->vlans[vid]) 354 return -ENOENT; 355 356 err = dpsw_vlan_remove(ethsw->mc_io, 0, ethsw->dpsw_handle, vid); 357 if (err) { 358 dev_err(ethsw->dev, "dpsw_vlan_remove err %d\n", err); 359 return err; 360 } 361 ethsw->vlans[vid] = 0; 362 363 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) { 364 ppriv_local = ethsw->ports[i]; 365 ppriv_local->vlans[vid] = 0; 366 } 367 368 return 0; 369 } 370 371 static int dpaa2_switch_port_fdb_add_uc(struct ethsw_port_priv *port_priv, 372 const unsigned char *addr) 373 { 374 struct dpsw_fdb_unicast_cfg entry = {0}; 375 u16 fdb_id; 376 int err; 377 378 entry.if_egress = port_priv->idx; 379 entry.type = DPSW_FDB_ENTRY_STATIC; 380 ether_addr_copy(entry.mac_addr, addr); 381 382 fdb_id = dpaa2_switch_port_get_fdb_id(port_priv); 383 err = dpsw_fdb_add_unicast(port_priv->ethsw_data->mc_io, 0, 384 port_priv->ethsw_data->dpsw_handle, 385 fdb_id, &entry); 386 if (err) 387 netdev_err(port_priv->netdev, 388 "dpsw_fdb_add_unicast err %d\n", err); 389 return err; 390 } 391 392 static int dpaa2_switch_port_fdb_del_uc(struct ethsw_port_priv *port_priv, 393 const unsigned char *addr) 394 { 395 struct dpsw_fdb_unicast_cfg entry = {0}; 396 u16 fdb_id; 397 int err; 398 399 entry.if_egress = port_priv->idx; 400 entry.type = DPSW_FDB_ENTRY_STATIC; 401 ether_addr_copy(entry.mac_addr, addr); 402 403 fdb_id = dpaa2_switch_port_get_fdb_id(port_priv); 404 err = dpsw_fdb_remove_unicast(port_priv->ethsw_data->mc_io, 0, 405 port_priv->ethsw_data->dpsw_handle, 406 fdb_id, &entry); 407 /* Silently discard error for calling multiple times the del command */ 408 if (err && err != -ENXIO) 409 netdev_err(port_priv->netdev, 410 "dpsw_fdb_remove_unicast err %d\n", err); 411 return err; 412 } 413 414 static int dpaa2_switch_port_fdb_add_mc(struct ethsw_port_priv *port_priv, 415 const unsigned char *addr) 416 { 417 struct dpsw_fdb_multicast_cfg entry = {0}; 418 u16 fdb_id; 419 int err; 420 421 ether_addr_copy(entry.mac_addr, addr); 422 entry.type = DPSW_FDB_ENTRY_STATIC; 423 entry.num_ifs = 1; 424 entry.if_id[0] = port_priv->idx; 425 426 fdb_id = dpaa2_switch_port_get_fdb_id(port_priv); 427 err = dpsw_fdb_add_multicast(port_priv->ethsw_data->mc_io, 0, 428 port_priv->ethsw_data->dpsw_handle, 429 fdb_id, &entry); 430 /* Silently discard error for calling multiple times the add command */ 431 if (err && err != -ENXIO) 432 netdev_err(port_priv->netdev, "dpsw_fdb_add_multicast err %d\n", 433 err); 434 return err; 435 } 436 437 static int dpaa2_switch_port_fdb_del_mc(struct ethsw_port_priv *port_priv, 438 const unsigned char *addr) 439 { 440 struct dpsw_fdb_multicast_cfg entry = {0}; 441 u16 fdb_id; 442 int err; 443 444 ether_addr_copy(entry.mac_addr, addr); 445 entry.type = DPSW_FDB_ENTRY_STATIC; 446 entry.num_ifs = 1; 447 entry.if_id[0] = port_priv->idx; 448 449 fdb_id = dpaa2_switch_port_get_fdb_id(port_priv); 450 err = dpsw_fdb_remove_multicast(port_priv->ethsw_data->mc_io, 0, 451 port_priv->ethsw_data->dpsw_handle, 452 fdb_id, &entry); 453 /* Silently discard error for calling multiple times the del command */ 454 if (err && err != -ENAVAIL) 455 netdev_err(port_priv->netdev, 456 "dpsw_fdb_remove_multicast err %d\n", err); 457 return err; 458 } 459 460 static void dpaa2_switch_port_get_stats(struct net_device *netdev, 461 struct rtnl_link_stats64 *stats) 462 { 463 struct ethsw_port_priv *port_priv = netdev_priv(netdev); 464 u64 tmp; 465 int err; 466 467 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0, 468 port_priv->ethsw_data->dpsw_handle, 469 port_priv->idx, 470 DPSW_CNT_ING_FRAME, &stats->rx_packets); 471 if (err) 472 goto error; 473 474 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0, 475 port_priv->ethsw_data->dpsw_handle, 476 port_priv->idx, 477 DPSW_CNT_EGR_FRAME, &stats->tx_packets); 478 if (err) 479 goto error; 480 481 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0, 482 port_priv->ethsw_data->dpsw_handle, 483 port_priv->idx, 484 DPSW_CNT_ING_BYTE, &stats->rx_bytes); 485 if (err) 486 goto error; 487 488 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0, 489 port_priv->ethsw_data->dpsw_handle, 490 port_priv->idx, 491 DPSW_CNT_EGR_BYTE, &stats->tx_bytes); 492 if (err) 493 goto error; 494 495 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0, 496 port_priv->ethsw_data->dpsw_handle, 497 port_priv->idx, 498 DPSW_CNT_ING_FRAME_DISCARD, 499 &stats->rx_dropped); 500 if (err) 501 goto error; 502 503 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0, 504 port_priv->ethsw_data->dpsw_handle, 505 port_priv->idx, 506 DPSW_CNT_ING_FLTR_FRAME, 507 &tmp); 508 if (err) 509 goto error; 510 stats->rx_dropped += tmp; 511 512 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0, 513 port_priv->ethsw_data->dpsw_handle, 514 port_priv->idx, 515 DPSW_CNT_EGR_FRAME_DISCARD, 516 &stats->tx_dropped); 517 if (err) 518 goto error; 519 520 return; 521 522 error: 523 netdev_err(netdev, "dpsw_if_get_counter err %d\n", err); 524 } 525 526 static bool dpaa2_switch_port_has_offload_stats(const struct net_device *netdev, 527 int attr_id) 528 { 529 return (attr_id == IFLA_OFFLOAD_XSTATS_CPU_HIT); 530 } 531 532 static int dpaa2_switch_port_get_offload_stats(int attr_id, 533 const struct net_device *netdev, 534 void *sp) 535 { 536 switch (attr_id) { 537 case IFLA_OFFLOAD_XSTATS_CPU_HIT: 538 dpaa2_switch_port_get_stats((struct net_device *)netdev, sp); 539 return 0; 540 } 541 542 return -EINVAL; 543 } 544 545 static int dpaa2_switch_port_change_mtu(struct net_device *netdev, int mtu) 546 { 547 struct ethsw_port_priv *port_priv = netdev_priv(netdev); 548 int err; 549 550 err = dpsw_if_set_max_frame_length(port_priv->ethsw_data->mc_io, 551 0, 552 port_priv->ethsw_data->dpsw_handle, 553 port_priv->idx, 554 (u16)ETHSW_L2_MAX_FRM(mtu)); 555 if (err) { 556 netdev_err(netdev, 557 "dpsw_if_set_max_frame_length() err %d\n", err); 558 return err; 559 } 560 561 netdev->mtu = mtu; 562 return 0; 563 } 564 565 static int dpaa2_switch_port_carrier_state_sync(struct net_device *netdev) 566 { 567 struct ethsw_port_priv *port_priv = netdev_priv(netdev); 568 struct dpsw_link_state state; 569 int err; 570 571 /* Interrupts are received even though no one issued an 'ifconfig up' 572 * on the switch interface. Ignore these link state update interrupts 573 */ 574 if (!netif_running(netdev)) 575 return 0; 576 577 err = dpsw_if_get_link_state(port_priv->ethsw_data->mc_io, 0, 578 port_priv->ethsw_data->dpsw_handle, 579 port_priv->idx, &state); 580 if (err) { 581 netdev_err(netdev, "dpsw_if_get_link_state() err %d\n", err); 582 return err; 583 } 584 585 WARN_ONCE(state.up > 1, "Garbage read into link_state"); 586 587 if (state.up != port_priv->link_state) { 588 if (state.up) { 589 netif_carrier_on(netdev); 590 netif_tx_start_all_queues(netdev); 591 } else { 592 netif_carrier_off(netdev); 593 netif_tx_stop_all_queues(netdev); 594 } 595 port_priv->link_state = state.up; 596 } 597 598 return 0; 599 } 600 601 /* Manage all NAPI instances for the control interface. 602 * 603 * We only have one RX queue and one Tx Conf queue for all 604 * switch ports. Therefore, we only need to enable the NAPI instance once, the 605 * first time one of the switch ports runs .dev_open(). 606 */ 607 608 static void dpaa2_switch_enable_ctrl_if_napi(struct ethsw_core *ethsw) 609 { 610 int i; 611 612 /* Access to the ethsw->napi_users relies on the RTNL lock */ 613 ASSERT_RTNL(); 614 615 /* a new interface is using the NAPI instance */ 616 ethsw->napi_users++; 617 618 /* if there is already a user of the instance, return */ 619 if (ethsw->napi_users > 1) 620 return; 621 622 for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++) 623 napi_enable(ðsw->fq[i].napi); 624 } 625 626 static void dpaa2_switch_disable_ctrl_if_napi(struct ethsw_core *ethsw) 627 { 628 int i; 629 630 /* Access to the ethsw->napi_users relies on the RTNL lock */ 631 ASSERT_RTNL(); 632 633 /* If we are not the last interface using the NAPI, return */ 634 ethsw->napi_users--; 635 if (ethsw->napi_users) 636 return; 637 638 for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++) 639 napi_disable(ðsw->fq[i].napi); 640 } 641 642 static int dpaa2_switch_port_open(struct net_device *netdev) 643 { 644 struct ethsw_port_priv *port_priv = netdev_priv(netdev); 645 struct ethsw_core *ethsw = port_priv->ethsw_data; 646 int err; 647 648 /* Explicitly set carrier off, otherwise 649 * netif_carrier_ok() will return true and cause 'ip link show' 650 * to report the LOWER_UP flag, even though the link 651 * notification wasn't even received. 652 */ 653 netif_carrier_off(netdev); 654 655 err = dpsw_if_enable(port_priv->ethsw_data->mc_io, 0, 656 port_priv->ethsw_data->dpsw_handle, 657 port_priv->idx); 658 if (err) { 659 netdev_err(netdev, "dpsw_if_enable err %d\n", err); 660 return err; 661 } 662 663 /* sync carrier state */ 664 err = dpaa2_switch_port_carrier_state_sync(netdev); 665 if (err) { 666 netdev_err(netdev, 667 "dpaa2_switch_port_carrier_state_sync err %d\n", err); 668 goto err_carrier_sync; 669 } 670 671 dpaa2_switch_enable_ctrl_if_napi(ethsw); 672 673 return 0; 674 675 err_carrier_sync: 676 dpsw_if_disable(port_priv->ethsw_data->mc_io, 0, 677 port_priv->ethsw_data->dpsw_handle, 678 port_priv->idx); 679 return err; 680 } 681 682 static int dpaa2_switch_port_stop(struct net_device *netdev) 683 { 684 struct ethsw_port_priv *port_priv = netdev_priv(netdev); 685 struct ethsw_core *ethsw = port_priv->ethsw_data; 686 int err; 687 688 err = dpsw_if_disable(port_priv->ethsw_data->mc_io, 0, 689 port_priv->ethsw_data->dpsw_handle, 690 port_priv->idx); 691 if (err) { 692 netdev_err(netdev, "dpsw_if_disable err %d\n", err); 693 return err; 694 } 695 696 dpaa2_switch_disable_ctrl_if_napi(ethsw); 697 698 return 0; 699 } 700 701 static int dpaa2_switch_port_parent_id(struct net_device *dev, 702 struct netdev_phys_item_id *ppid) 703 { 704 struct ethsw_port_priv *port_priv = netdev_priv(dev); 705 706 ppid->id_len = 1; 707 ppid->id[0] = port_priv->ethsw_data->dev_id; 708 709 return 0; 710 } 711 712 static int dpaa2_switch_port_get_phys_name(struct net_device *netdev, char *name, 713 size_t len) 714 { 715 struct ethsw_port_priv *port_priv = netdev_priv(netdev); 716 int err; 717 718 err = snprintf(name, len, "p%d", port_priv->idx); 719 if (err >= len) 720 return -EINVAL; 721 722 return 0; 723 } 724 725 struct ethsw_dump_ctx { 726 struct net_device *dev; 727 struct sk_buff *skb; 728 struct netlink_callback *cb; 729 int idx; 730 }; 731 732 static int dpaa2_switch_fdb_dump_nl(struct fdb_dump_entry *entry, 733 struct ethsw_dump_ctx *dump) 734 { 735 int is_dynamic = entry->type & DPSW_FDB_ENTRY_DINAMIC; 736 u32 portid = NETLINK_CB(dump->cb->skb).portid; 737 u32 seq = dump->cb->nlh->nlmsg_seq; 738 struct nlmsghdr *nlh; 739 struct ndmsg *ndm; 740 741 if (dump->idx < dump->cb->args[2]) 742 goto skip; 743 744 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH, 745 sizeof(*ndm), NLM_F_MULTI); 746 if (!nlh) 747 return -EMSGSIZE; 748 749 ndm = nlmsg_data(nlh); 750 ndm->ndm_family = AF_BRIDGE; 751 ndm->ndm_pad1 = 0; 752 ndm->ndm_pad2 = 0; 753 ndm->ndm_flags = NTF_SELF; 754 ndm->ndm_type = 0; 755 ndm->ndm_ifindex = dump->dev->ifindex; 756 ndm->ndm_state = is_dynamic ? NUD_REACHABLE : NUD_NOARP; 757 758 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, entry->mac_addr)) 759 goto nla_put_failure; 760 761 nlmsg_end(dump->skb, nlh); 762 763 skip: 764 dump->idx++; 765 return 0; 766 767 nla_put_failure: 768 nlmsg_cancel(dump->skb, nlh); 769 return -EMSGSIZE; 770 } 771 772 static int dpaa2_switch_port_fdb_valid_entry(struct fdb_dump_entry *entry, 773 struct ethsw_port_priv *port_priv) 774 { 775 int idx = port_priv->idx; 776 int valid; 777 778 if (entry->type & DPSW_FDB_ENTRY_TYPE_UNICAST) 779 valid = entry->if_info == port_priv->idx; 780 else 781 valid = entry->if_mask[idx / 8] & BIT(idx % 8); 782 783 return valid; 784 } 785 786 static int dpaa2_switch_fdb_iterate(struct ethsw_port_priv *port_priv, 787 dpaa2_switch_fdb_cb_t cb, void *data) 788 { 789 struct net_device *net_dev = port_priv->netdev; 790 struct ethsw_core *ethsw = port_priv->ethsw_data; 791 struct device *dev = net_dev->dev.parent; 792 struct fdb_dump_entry *fdb_entries; 793 struct fdb_dump_entry fdb_entry; 794 dma_addr_t fdb_dump_iova; 795 u16 num_fdb_entries; 796 u32 fdb_dump_size; 797 int err = 0, i; 798 u8 *dma_mem; 799 u16 fdb_id; 800 801 fdb_dump_size = ethsw->sw_attr.max_fdb_entries * sizeof(fdb_entry); 802 dma_mem = kzalloc(fdb_dump_size, GFP_KERNEL); 803 if (!dma_mem) 804 return -ENOMEM; 805 806 fdb_dump_iova = dma_map_single(dev, dma_mem, fdb_dump_size, 807 DMA_FROM_DEVICE); 808 if (dma_mapping_error(dev, fdb_dump_iova)) { 809 netdev_err(net_dev, "dma_map_single() failed\n"); 810 err = -ENOMEM; 811 goto err_map; 812 } 813 814 fdb_id = dpaa2_switch_port_get_fdb_id(port_priv); 815 err = dpsw_fdb_dump(ethsw->mc_io, 0, ethsw->dpsw_handle, fdb_id, 816 fdb_dump_iova, fdb_dump_size, &num_fdb_entries); 817 if (err) { 818 netdev_err(net_dev, "dpsw_fdb_dump() = %d\n", err); 819 goto err_dump; 820 } 821 822 dma_unmap_single(dev, fdb_dump_iova, fdb_dump_size, DMA_FROM_DEVICE); 823 824 fdb_entries = (struct fdb_dump_entry *)dma_mem; 825 for (i = 0; i < num_fdb_entries; i++) { 826 fdb_entry = fdb_entries[i]; 827 828 err = cb(port_priv, &fdb_entry, data); 829 if (err) 830 goto end; 831 } 832 833 end: 834 kfree(dma_mem); 835 836 return 0; 837 838 err_dump: 839 dma_unmap_single(dev, fdb_dump_iova, fdb_dump_size, DMA_TO_DEVICE); 840 err_map: 841 kfree(dma_mem); 842 return err; 843 } 844 845 static int dpaa2_switch_fdb_entry_dump(struct ethsw_port_priv *port_priv, 846 struct fdb_dump_entry *fdb_entry, 847 void *data) 848 { 849 if (!dpaa2_switch_port_fdb_valid_entry(fdb_entry, port_priv)) 850 return 0; 851 852 return dpaa2_switch_fdb_dump_nl(fdb_entry, data); 853 } 854 855 static int dpaa2_switch_port_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb, 856 struct net_device *net_dev, 857 struct net_device *filter_dev, int *idx) 858 { 859 struct ethsw_port_priv *port_priv = netdev_priv(net_dev); 860 struct ethsw_dump_ctx dump = { 861 .dev = net_dev, 862 .skb = skb, 863 .cb = cb, 864 .idx = *idx, 865 }; 866 int err; 867 868 err = dpaa2_switch_fdb_iterate(port_priv, dpaa2_switch_fdb_entry_dump, &dump); 869 *idx = dump.idx; 870 871 return err; 872 } 873 874 static int dpaa2_switch_fdb_entry_fast_age(struct ethsw_port_priv *port_priv, 875 struct fdb_dump_entry *fdb_entry, 876 void *data __always_unused) 877 { 878 if (!dpaa2_switch_port_fdb_valid_entry(fdb_entry, port_priv)) 879 return 0; 880 881 if (!(fdb_entry->type & DPSW_FDB_ENTRY_TYPE_DYNAMIC)) 882 return 0; 883 884 if (fdb_entry->type & DPSW_FDB_ENTRY_TYPE_UNICAST) 885 dpaa2_switch_port_fdb_del_uc(port_priv, fdb_entry->mac_addr); 886 else 887 dpaa2_switch_port_fdb_del_mc(port_priv, fdb_entry->mac_addr); 888 889 return 0; 890 } 891 892 static void dpaa2_switch_port_fast_age(struct ethsw_port_priv *port_priv) 893 { 894 dpaa2_switch_fdb_iterate(port_priv, 895 dpaa2_switch_fdb_entry_fast_age, NULL); 896 } 897 898 static int dpaa2_switch_port_vlan_add(struct net_device *netdev, __be16 proto, 899 u16 vid) 900 { 901 struct switchdev_obj_port_vlan vlan = { 902 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN, 903 .vid = vid, 904 .obj.orig_dev = netdev, 905 /* This API only allows programming tagged, non-PVID VIDs */ 906 .flags = 0, 907 }; 908 909 return dpaa2_switch_port_vlans_add(netdev, &vlan); 910 } 911 912 static int dpaa2_switch_port_vlan_kill(struct net_device *netdev, __be16 proto, 913 u16 vid) 914 { 915 struct switchdev_obj_port_vlan vlan = { 916 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN, 917 .vid = vid, 918 .obj.orig_dev = netdev, 919 /* This API only allows programming tagged, non-PVID VIDs */ 920 .flags = 0, 921 }; 922 923 return dpaa2_switch_port_vlans_del(netdev, &vlan); 924 } 925 926 static int dpaa2_switch_port_set_mac_addr(struct ethsw_port_priv *port_priv) 927 { 928 struct ethsw_core *ethsw = port_priv->ethsw_data; 929 struct net_device *net_dev = port_priv->netdev; 930 struct device *dev = net_dev->dev.parent; 931 u8 mac_addr[ETH_ALEN]; 932 int err; 933 934 if (!(ethsw->features & ETHSW_FEATURE_MAC_ADDR)) 935 return 0; 936 937 /* Get firmware address, if any */ 938 err = dpsw_if_get_port_mac_addr(ethsw->mc_io, 0, ethsw->dpsw_handle, 939 port_priv->idx, mac_addr); 940 if (err) { 941 dev_err(dev, "dpsw_if_get_port_mac_addr() failed\n"); 942 return err; 943 } 944 945 /* First check if firmware has any address configured by bootloader */ 946 if (!is_zero_ether_addr(mac_addr)) { 947 memcpy(net_dev->dev_addr, mac_addr, net_dev->addr_len); 948 } else { 949 /* No MAC address configured, fill in net_dev->dev_addr 950 * with a random one 951 */ 952 eth_hw_addr_random(net_dev); 953 dev_dbg_once(dev, "device(s) have all-zero hwaddr, replaced with random\n"); 954 955 /* Override NET_ADDR_RANDOM set by eth_hw_addr_random(); for all 956 * practical purposes, this will be our "permanent" mac address, 957 * at least until the next reboot. This move will also permit 958 * register_netdevice() to properly fill up net_dev->perm_addr. 959 */ 960 net_dev->addr_assign_type = NET_ADDR_PERM; 961 } 962 963 return 0; 964 } 965 966 static void dpaa2_switch_free_fd(const struct ethsw_core *ethsw, 967 const struct dpaa2_fd *fd) 968 { 969 struct device *dev = ethsw->dev; 970 unsigned char *buffer_start; 971 struct sk_buff **skbh, *skb; 972 dma_addr_t fd_addr; 973 974 fd_addr = dpaa2_fd_get_addr(fd); 975 skbh = dpaa2_iova_to_virt(ethsw->iommu_domain, fd_addr); 976 977 skb = *skbh; 978 buffer_start = (unsigned char *)skbh; 979 980 dma_unmap_single(dev, fd_addr, 981 skb_tail_pointer(skb) - buffer_start, 982 DMA_TO_DEVICE); 983 984 /* Move on with skb release */ 985 dev_kfree_skb(skb); 986 } 987 988 static int dpaa2_switch_build_single_fd(struct ethsw_core *ethsw, 989 struct sk_buff *skb, 990 struct dpaa2_fd *fd) 991 { 992 struct device *dev = ethsw->dev; 993 struct sk_buff **skbh; 994 dma_addr_t addr; 995 u8 *buff_start; 996 void *hwa; 997 998 buff_start = PTR_ALIGN(skb->data - DPAA2_SWITCH_TX_DATA_OFFSET - 999 DPAA2_SWITCH_TX_BUF_ALIGN, 1000 DPAA2_SWITCH_TX_BUF_ALIGN); 1001 1002 /* Clear FAS to have consistent values for TX confirmation. It is 1003 * located in the first 8 bytes of the buffer's hardware annotation 1004 * area 1005 */ 1006 hwa = buff_start + DPAA2_SWITCH_SWA_SIZE; 1007 memset(hwa, 0, 8); 1008 1009 /* Store a backpointer to the skb at the beginning of the buffer 1010 * (in the private data area) such that we can release it 1011 * on Tx confirm 1012 */ 1013 skbh = (struct sk_buff **)buff_start; 1014 *skbh = skb; 1015 1016 addr = dma_map_single(dev, buff_start, 1017 skb_tail_pointer(skb) - buff_start, 1018 DMA_TO_DEVICE); 1019 if (unlikely(dma_mapping_error(dev, addr))) 1020 return -ENOMEM; 1021 1022 /* Setup the FD fields */ 1023 memset(fd, 0, sizeof(*fd)); 1024 1025 dpaa2_fd_set_addr(fd, addr); 1026 dpaa2_fd_set_offset(fd, (u16)(skb->data - buff_start)); 1027 dpaa2_fd_set_len(fd, skb->len); 1028 dpaa2_fd_set_format(fd, dpaa2_fd_single); 1029 1030 return 0; 1031 } 1032 1033 static netdev_tx_t dpaa2_switch_port_tx(struct sk_buff *skb, 1034 struct net_device *net_dev) 1035 { 1036 struct ethsw_port_priv *port_priv = netdev_priv(net_dev); 1037 struct ethsw_core *ethsw = port_priv->ethsw_data; 1038 int retries = DPAA2_SWITCH_SWP_BUSY_RETRIES; 1039 struct dpaa2_fd fd; 1040 int err; 1041 1042 if (unlikely(skb_headroom(skb) < DPAA2_SWITCH_NEEDED_HEADROOM)) { 1043 struct sk_buff *ns; 1044 1045 ns = skb_realloc_headroom(skb, DPAA2_SWITCH_NEEDED_HEADROOM); 1046 if (unlikely(!ns)) { 1047 net_err_ratelimited("%s: Error reallocating skb headroom\n", net_dev->name); 1048 goto err_free_skb; 1049 } 1050 dev_consume_skb_any(skb); 1051 skb = ns; 1052 } 1053 1054 /* We'll be holding a back-reference to the skb until Tx confirmation */ 1055 skb = skb_unshare(skb, GFP_ATOMIC); 1056 if (unlikely(!skb)) { 1057 /* skb_unshare() has already freed the skb */ 1058 net_err_ratelimited("%s: Error copying the socket buffer\n", net_dev->name); 1059 goto err_exit; 1060 } 1061 1062 /* At this stage, we do not support non-linear skbs so just try to 1063 * linearize the skb and if that's not working, just drop the packet. 1064 */ 1065 err = skb_linearize(skb); 1066 if (err) { 1067 net_err_ratelimited("%s: skb_linearize error (%d)!\n", net_dev->name, err); 1068 goto err_free_skb; 1069 } 1070 1071 err = dpaa2_switch_build_single_fd(ethsw, skb, &fd); 1072 if (unlikely(err)) { 1073 net_err_ratelimited("%s: ethsw_build_*_fd() %d\n", net_dev->name, err); 1074 goto err_free_skb; 1075 } 1076 1077 do { 1078 err = dpaa2_io_service_enqueue_qd(NULL, 1079 port_priv->tx_qdid, 1080 8, 0, &fd); 1081 retries--; 1082 } while (err == -EBUSY && retries); 1083 1084 if (unlikely(err < 0)) { 1085 dpaa2_switch_free_fd(ethsw, &fd); 1086 goto err_exit; 1087 } 1088 1089 return NETDEV_TX_OK; 1090 1091 err_free_skb: 1092 dev_kfree_skb(skb); 1093 err_exit: 1094 return NETDEV_TX_OK; 1095 } 1096 1097 static const struct net_device_ops dpaa2_switch_port_ops = { 1098 .ndo_open = dpaa2_switch_port_open, 1099 .ndo_stop = dpaa2_switch_port_stop, 1100 1101 .ndo_set_mac_address = eth_mac_addr, 1102 .ndo_get_stats64 = dpaa2_switch_port_get_stats, 1103 .ndo_change_mtu = dpaa2_switch_port_change_mtu, 1104 .ndo_has_offload_stats = dpaa2_switch_port_has_offload_stats, 1105 .ndo_get_offload_stats = dpaa2_switch_port_get_offload_stats, 1106 .ndo_fdb_dump = dpaa2_switch_port_fdb_dump, 1107 .ndo_vlan_rx_add_vid = dpaa2_switch_port_vlan_add, 1108 .ndo_vlan_rx_kill_vid = dpaa2_switch_port_vlan_kill, 1109 1110 .ndo_start_xmit = dpaa2_switch_port_tx, 1111 .ndo_get_port_parent_id = dpaa2_switch_port_parent_id, 1112 .ndo_get_phys_port_name = dpaa2_switch_port_get_phys_name, 1113 }; 1114 1115 bool dpaa2_switch_port_dev_check(const struct net_device *netdev) 1116 { 1117 return netdev->netdev_ops == &dpaa2_switch_port_ops; 1118 } 1119 1120 static void dpaa2_switch_links_state_update(struct ethsw_core *ethsw) 1121 { 1122 int i; 1123 1124 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) { 1125 dpaa2_switch_port_carrier_state_sync(ethsw->ports[i]->netdev); 1126 dpaa2_switch_port_set_mac_addr(ethsw->ports[i]); 1127 } 1128 } 1129 1130 static irqreturn_t dpaa2_switch_irq0_handler_thread(int irq_num, void *arg) 1131 { 1132 struct device *dev = (struct device *)arg; 1133 struct ethsw_core *ethsw = dev_get_drvdata(dev); 1134 1135 /* Mask the events and the if_id reserved bits to be cleared on read */ 1136 u32 status = DPSW_IRQ_EVENT_LINK_CHANGED | 0xFFFF0000; 1137 int err; 1138 1139 err = dpsw_get_irq_status(ethsw->mc_io, 0, ethsw->dpsw_handle, 1140 DPSW_IRQ_INDEX_IF, &status); 1141 if (err) { 1142 dev_err(dev, "Can't get irq status (err %d)\n", err); 1143 1144 err = dpsw_clear_irq_status(ethsw->mc_io, 0, ethsw->dpsw_handle, 1145 DPSW_IRQ_INDEX_IF, 0xFFFFFFFF); 1146 if (err) 1147 dev_err(dev, "Can't clear irq status (err %d)\n", err); 1148 goto out; 1149 } 1150 1151 if (status & DPSW_IRQ_EVENT_LINK_CHANGED) 1152 dpaa2_switch_links_state_update(ethsw); 1153 1154 out: 1155 return IRQ_HANDLED; 1156 } 1157 1158 static int dpaa2_switch_setup_irqs(struct fsl_mc_device *sw_dev) 1159 { 1160 struct device *dev = &sw_dev->dev; 1161 struct ethsw_core *ethsw = dev_get_drvdata(dev); 1162 u32 mask = DPSW_IRQ_EVENT_LINK_CHANGED; 1163 struct fsl_mc_device_irq *irq; 1164 int err; 1165 1166 err = fsl_mc_allocate_irqs(sw_dev); 1167 if (err) { 1168 dev_err(dev, "MC irqs allocation failed\n"); 1169 return err; 1170 } 1171 1172 if (WARN_ON(sw_dev->obj_desc.irq_count != DPSW_IRQ_NUM)) { 1173 err = -EINVAL; 1174 goto free_irq; 1175 } 1176 1177 err = dpsw_set_irq_enable(ethsw->mc_io, 0, ethsw->dpsw_handle, 1178 DPSW_IRQ_INDEX_IF, 0); 1179 if (err) { 1180 dev_err(dev, "dpsw_set_irq_enable err %d\n", err); 1181 goto free_irq; 1182 } 1183 1184 irq = sw_dev->irqs[DPSW_IRQ_INDEX_IF]; 1185 1186 err = devm_request_threaded_irq(dev, irq->msi_desc->irq, 1187 NULL, 1188 dpaa2_switch_irq0_handler_thread, 1189 IRQF_NO_SUSPEND | IRQF_ONESHOT, 1190 dev_name(dev), dev); 1191 if (err) { 1192 dev_err(dev, "devm_request_threaded_irq(): %d\n", err); 1193 goto free_irq; 1194 } 1195 1196 err = dpsw_set_irq_mask(ethsw->mc_io, 0, ethsw->dpsw_handle, 1197 DPSW_IRQ_INDEX_IF, mask); 1198 if (err) { 1199 dev_err(dev, "dpsw_set_irq_mask(): %d\n", err); 1200 goto free_devm_irq; 1201 } 1202 1203 err = dpsw_set_irq_enable(ethsw->mc_io, 0, ethsw->dpsw_handle, 1204 DPSW_IRQ_INDEX_IF, 1); 1205 if (err) { 1206 dev_err(dev, "dpsw_set_irq_enable(): %d\n", err); 1207 goto free_devm_irq; 1208 } 1209 1210 return 0; 1211 1212 free_devm_irq: 1213 devm_free_irq(dev, irq->msi_desc->irq, dev); 1214 free_irq: 1215 fsl_mc_free_irqs(sw_dev); 1216 return err; 1217 } 1218 1219 static void dpaa2_switch_teardown_irqs(struct fsl_mc_device *sw_dev) 1220 { 1221 struct device *dev = &sw_dev->dev; 1222 struct ethsw_core *ethsw = dev_get_drvdata(dev); 1223 int err; 1224 1225 err = dpsw_set_irq_enable(ethsw->mc_io, 0, ethsw->dpsw_handle, 1226 DPSW_IRQ_INDEX_IF, 0); 1227 if (err) 1228 dev_err(dev, "dpsw_set_irq_enable err %d\n", err); 1229 1230 fsl_mc_free_irqs(sw_dev); 1231 } 1232 1233 static int dpaa2_switch_port_attr_stp_state_set(struct net_device *netdev, 1234 u8 state) 1235 { 1236 struct ethsw_port_priv *port_priv = netdev_priv(netdev); 1237 1238 return dpaa2_switch_port_set_stp_state(port_priv, state); 1239 } 1240 1241 static int dpaa2_switch_port_attr_set(struct net_device *netdev, 1242 const struct switchdev_attr *attr, 1243 struct netlink_ext_ack *extack) 1244 { 1245 int err = 0; 1246 1247 switch (attr->id) { 1248 case SWITCHDEV_ATTR_ID_PORT_STP_STATE: 1249 err = dpaa2_switch_port_attr_stp_state_set(netdev, 1250 attr->u.stp_state); 1251 break; 1252 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING: 1253 if (!attr->u.vlan_filtering) { 1254 NL_SET_ERR_MSG_MOD(extack, 1255 "The DPAA2 switch does not support VLAN-unaware operation"); 1256 return -EOPNOTSUPP; 1257 } 1258 break; 1259 default: 1260 err = -EOPNOTSUPP; 1261 break; 1262 } 1263 1264 return err; 1265 } 1266 1267 int dpaa2_switch_port_vlans_add(struct net_device *netdev, 1268 const struct switchdev_obj_port_vlan *vlan) 1269 { 1270 struct ethsw_port_priv *port_priv = netdev_priv(netdev); 1271 struct ethsw_core *ethsw = port_priv->ethsw_data; 1272 struct dpsw_attr *attr = ðsw->sw_attr; 1273 int err = 0; 1274 1275 /* Make sure that the VLAN is not already configured 1276 * on the switch port 1277 */ 1278 if (port_priv->vlans[vlan->vid] & ETHSW_VLAN_MEMBER) 1279 return -EEXIST; 1280 1281 /* Check if there is space for a new VLAN */ 1282 err = dpsw_get_attributes(ethsw->mc_io, 0, ethsw->dpsw_handle, 1283 ðsw->sw_attr); 1284 if (err) { 1285 netdev_err(netdev, "dpsw_get_attributes err %d\n", err); 1286 return err; 1287 } 1288 if (attr->max_vlans - attr->num_vlans < 1) 1289 return -ENOSPC; 1290 1291 /* Check if there is space for a new VLAN */ 1292 err = dpsw_get_attributes(ethsw->mc_io, 0, ethsw->dpsw_handle, 1293 ðsw->sw_attr); 1294 if (err) { 1295 netdev_err(netdev, "dpsw_get_attributes err %d\n", err); 1296 return err; 1297 } 1298 if (attr->max_vlans - attr->num_vlans < 1) 1299 return -ENOSPC; 1300 1301 if (!port_priv->ethsw_data->vlans[vlan->vid]) { 1302 /* this is a new VLAN */ 1303 err = dpaa2_switch_add_vlan(port_priv, vlan->vid); 1304 if (err) 1305 return err; 1306 1307 port_priv->ethsw_data->vlans[vlan->vid] |= ETHSW_VLAN_GLOBAL; 1308 } 1309 1310 return dpaa2_switch_port_add_vlan(port_priv, vlan->vid, vlan->flags); 1311 } 1312 1313 static int dpaa2_switch_port_lookup_address(struct net_device *netdev, int is_uc, 1314 const unsigned char *addr) 1315 { 1316 struct netdev_hw_addr_list *list = (is_uc) ? &netdev->uc : &netdev->mc; 1317 struct netdev_hw_addr *ha; 1318 1319 netif_addr_lock_bh(netdev); 1320 list_for_each_entry(ha, &list->list, list) { 1321 if (ether_addr_equal(ha->addr, addr)) { 1322 netif_addr_unlock_bh(netdev); 1323 return 1; 1324 } 1325 } 1326 netif_addr_unlock_bh(netdev); 1327 return 0; 1328 } 1329 1330 static int dpaa2_switch_port_mdb_add(struct net_device *netdev, 1331 const struct switchdev_obj_port_mdb *mdb) 1332 { 1333 struct ethsw_port_priv *port_priv = netdev_priv(netdev); 1334 int err; 1335 1336 /* Check if address is already set on this port */ 1337 if (dpaa2_switch_port_lookup_address(netdev, 0, mdb->addr)) 1338 return -EEXIST; 1339 1340 err = dpaa2_switch_port_fdb_add_mc(port_priv, mdb->addr); 1341 if (err) 1342 return err; 1343 1344 err = dev_mc_add(netdev, mdb->addr); 1345 if (err) { 1346 netdev_err(netdev, "dev_mc_add err %d\n", err); 1347 dpaa2_switch_port_fdb_del_mc(port_priv, mdb->addr); 1348 } 1349 1350 return err; 1351 } 1352 1353 static int dpaa2_switch_port_obj_add(struct net_device *netdev, 1354 const struct switchdev_obj *obj) 1355 { 1356 int err; 1357 1358 switch (obj->id) { 1359 case SWITCHDEV_OBJ_ID_PORT_VLAN: 1360 err = dpaa2_switch_port_vlans_add(netdev, 1361 SWITCHDEV_OBJ_PORT_VLAN(obj)); 1362 break; 1363 case SWITCHDEV_OBJ_ID_PORT_MDB: 1364 err = dpaa2_switch_port_mdb_add(netdev, 1365 SWITCHDEV_OBJ_PORT_MDB(obj)); 1366 break; 1367 default: 1368 err = -EOPNOTSUPP; 1369 break; 1370 } 1371 1372 return err; 1373 } 1374 1375 static int dpaa2_switch_port_del_vlan(struct ethsw_port_priv *port_priv, u16 vid) 1376 { 1377 struct ethsw_core *ethsw = port_priv->ethsw_data; 1378 struct net_device *netdev = port_priv->netdev; 1379 struct dpsw_vlan_if_cfg vcfg; 1380 int i, err; 1381 1382 if (!port_priv->vlans[vid]) 1383 return -ENOENT; 1384 1385 if (port_priv->vlans[vid] & ETHSW_VLAN_PVID) { 1386 /* If we are deleting the PVID of a port, use VLAN 4095 instead 1387 * as we are sure that neither the bridge nor the 8021q module 1388 * will use it 1389 */ 1390 err = dpaa2_switch_port_set_pvid(port_priv, 4095); 1391 if (err) 1392 return err; 1393 } 1394 1395 vcfg.num_ifs = 1; 1396 vcfg.if_id[0] = port_priv->idx; 1397 if (port_priv->vlans[vid] & ETHSW_VLAN_UNTAGGED) { 1398 err = dpsw_vlan_remove_if_untagged(ethsw->mc_io, 0, 1399 ethsw->dpsw_handle, 1400 vid, &vcfg); 1401 if (err) { 1402 netdev_err(netdev, 1403 "dpsw_vlan_remove_if_untagged err %d\n", 1404 err); 1405 } 1406 port_priv->vlans[vid] &= ~ETHSW_VLAN_UNTAGGED; 1407 } 1408 1409 if (port_priv->vlans[vid] & ETHSW_VLAN_MEMBER) { 1410 err = dpsw_vlan_remove_if(ethsw->mc_io, 0, ethsw->dpsw_handle, 1411 vid, &vcfg); 1412 if (err) { 1413 netdev_err(netdev, 1414 "dpsw_vlan_remove_if err %d\n", err); 1415 return err; 1416 } 1417 port_priv->vlans[vid] &= ~ETHSW_VLAN_MEMBER; 1418 1419 /* Delete VLAN from switch if it is no longer configured on 1420 * any port 1421 */ 1422 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) 1423 if (ethsw->ports[i]->vlans[vid] & ETHSW_VLAN_MEMBER) 1424 return 0; /* Found a port member in VID */ 1425 1426 ethsw->vlans[vid] &= ~ETHSW_VLAN_GLOBAL; 1427 1428 err = dpaa2_switch_dellink(ethsw, vid); 1429 if (err) 1430 return err; 1431 } 1432 1433 return 0; 1434 } 1435 1436 int dpaa2_switch_port_vlans_del(struct net_device *netdev, 1437 const struct switchdev_obj_port_vlan *vlan) 1438 { 1439 struct ethsw_port_priv *port_priv = netdev_priv(netdev); 1440 1441 if (netif_is_bridge_master(vlan->obj.orig_dev)) 1442 return -EOPNOTSUPP; 1443 1444 return dpaa2_switch_port_del_vlan(port_priv, vlan->vid); 1445 } 1446 1447 static int dpaa2_switch_port_mdb_del(struct net_device *netdev, 1448 const struct switchdev_obj_port_mdb *mdb) 1449 { 1450 struct ethsw_port_priv *port_priv = netdev_priv(netdev); 1451 int err; 1452 1453 if (!dpaa2_switch_port_lookup_address(netdev, 0, mdb->addr)) 1454 return -ENOENT; 1455 1456 err = dpaa2_switch_port_fdb_del_mc(port_priv, mdb->addr); 1457 if (err) 1458 return err; 1459 1460 err = dev_mc_del(netdev, mdb->addr); 1461 if (err) { 1462 netdev_err(netdev, "dev_mc_del err %d\n", err); 1463 return err; 1464 } 1465 1466 return err; 1467 } 1468 1469 static int dpaa2_switch_port_obj_del(struct net_device *netdev, 1470 const struct switchdev_obj *obj) 1471 { 1472 int err; 1473 1474 switch (obj->id) { 1475 case SWITCHDEV_OBJ_ID_PORT_VLAN: 1476 err = dpaa2_switch_port_vlans_del(netdev, SWITCHDEV_OBJ_PORT_VLAN(obj)); 1477 break; 1478 case SWITCHDEV_OBJ_ID_PORT_MDB: 1479 err = dpaa2_switch_port_mdb_del(netdev, SWITCHDEV_OBJ_PORT_MDB(obj)); 1480 break; 1481 default: 1482 err = -EOPNOTSUPP; 1483 break; 1484 } 1485 return err; 1486 } 1487 1488 static int dpaa2_switch_port_attr_set_event(struct net_device *netdev, 1489 struct switchdev_notifier_port_attr_info *ptr) 1490 { 1491 int err; 1492 1493 err = switchdev_handle_port_attr_set(netdev, ptr, 1494 dpaa2_switch_port_dev_check, 1495 dpaa2_switch_port_attr_set); 1496 return notifier_from_errno(err); 1497 } 1498 1499 static int dpaa2_switch_port_bridge_join(struct net_device *netdev, 1500 struct net_device *upper_dev) 1501 { 1502 struct ethsw_port_priv *port_priv = netdev_priv(netdev); 1503 struct ethsw_core *ethsw = port_priv->ethsw_data; 1504 struct ethsw_port_priv *other_port_priv; 1505 struct net_device *other_dev; 1506 struct list_head *iter; 1507 int err; 1508 1509 netdev_for_each_lower_dev(upper_dev, other_dev, iter) { 1510 if (!dpaa2_switch_port_dev_check(other_dev)) 1511 continue; 1512 1513 other_port_priv = netdev_priv(other_dev); 1514 if (other_port_priv->ethsw_data != port_priv->ethsw_data) { 1515 netdev_err(netdev, 1516 "Interface from a different DPSW is in the bridge already!\n"); 1517 return -EINVAL; 1518 } 1519 } 1520 1521 /* Delete the previously manually installed VLAN 1 */ 1522 err = dpaa2_switch_port_del_vlan(port_priv, 1); 1523 if (err) 1524 return err; 1525 1526 dpaa2_switch_port_set_fdb(port_priv, upper_dev); 1527 1528 /* Setup the egress flood policy (broadcast, unknown unicast) */ 1529 err = dpaa2_switch_fdb_set_egress_flood(ethsw, port_priv->fdb->fdb_id); 1530 if (err) 1531 goto err_egress_flood; 1532 1533 return 0; 1534 1535 err_egress_flood: 1536 dpaa2_switch_port_set_fdb(port_priv, NULL); 1537 return err; 1538 } 1539 1540 static int dpaa2_switch_port_clear_rxvlan(struct net_device *vdev, int vid, void *arg) 1541 { 1542 __be16 vlan_proto = htons(ETH_P_8021Q); 1543 1544 if (vdev) 1545 vlan_proto = vlan_dev_vlan_proto(vdev); 1546 1547 return dpaa2_switch_port_vlan_kill(arg, vlan_proto, vid); 1548 } 1549 1550 static int dpaa2_switch_port_restore_rxvlan(struct net_device *vdev, int vid, void *arg) 1551 { 1552 __be16 vlan_proto = htons(ETH_P_8021Q); 1553 1554 if (vdev) 1555 vlan_proto = vlan_dev_vlan_proto(vdev); 1556 1557 return dpaa2_switch_port_vlan_add(arg, vlan_proto, vid); 1558 } 1559 1560 static int dpaa2_switch_port_bridge_leave(struct net_device *netdev) 1561 { 1562 struct ethsw_port_priv *port_priv = netdev_priv(netdev); 1563 struct dpaa2_switch_fdb *old_fdb = port_priv->fdb; 1564 struct ethsw_core *ethsw = port_priv->ethsw_data; 1565 int err; 1566 1567 /* First of all, fast age any learn FDB addresses on this switch port */ 1568 dpaa2_switch_port_fast_age(port_priv); 1569 1570 /* Clear all RX VLANs installed through vlan_vid_add() either as VLAN 1571 * upper devices or otherwise from the FDB table that we are about to 1572 * leave 1573 */ 1574 err = vlan_for_each(netdev, dpaa2_switch_port_clear_rxvlan, netdev); 1575 if (err) 1576 netdev_err(netdev, "Unable to clear RX VLANs from old FDB table, err (%d)\n", err); 1577 1578 dpaa2_switch_port_set_fdb(port_priv, NULL); 1579 1580 /* Restore all RX VLANs into the new FDB table that we just joined */ 1581 err = vlan_for_each(netdev, dpaa2_switch_port_restore_rxvlan, netdev); 1582 if (err) 1583 netdev_err(netdev, "Unable to restore RX VLANs to the new FDB, err (%d)\n", err); 1584 1585 /* Setup the egress flood policy (broadcast, unknown unicast). 1586 * When the port is not under a bridge, only the CTRL interface is part 1587 * of the flooding domain besides the actual port 1588 */ 1589 err = dpaa2_switch_fdb_set_egress_flood(ethsw, port_priv->fdb->fdb_id); 1590 if (err) 1591 return err; 1592 1593 /* Recreate the egress flood domain of the FDB that we just left */ 1594 err = dpaa2_switch_fdb_set_egress_flood(ethsw, old_fdb->fdb_id); 1595 if (err) 1596 return err; 1597 1598 /* Add the VLAN 1 as PVID when not under a bridge. We need this since 1599 * the dpaa2 switch interfaces are not capable to be VLAN unaware 1600 */ 1601 return dpaa2_switch_port_add_vlan(port_priv, DEFAULT_VLAN_ID, 1602 BRIDGE_VLAN_INFO_UNTAGGED | BRIDGE_VLAN_INFO_PVID); 1603 } 1604 1605 static int dpaa2_switch_prevent_bridging_with_8021q_upper(struct net_device *netdev) 1606 { 1607 struct net_device *upper_dev; 1608 struct list_head *iter; 1609 1610 /* RCU read lock not necessary because we have write-side protection 1611 * (rtnl_mutex), however a non-rcu iterator does not exist. 1612 */ 1613 netdev_for_each_upper_dev_rcu(netdev, upper_dev, iter) 1614 if (is_vlan_dev(upper_dev)) 1615 return -EOPNOTSUPP; 1616 1617 return 0; 1618 } 1619 1620 static int dpaa2_switch_port_netdevice_event(struct notifier_block *nb, 1621 unsigned long event, void *ptr) 1622 { 1623 struct net_device *netdev = netdev_notifier_info_to_dev(ptr); 1624 struct netdev_notifier_changeupper_info *info = ptr; 1625 struct netlink_ext_ack *extack; 1626 struct net_device *upper_dev; 1627 int err = 0; 1628 1629 if (!dpaa2_switch_port_dev_check(netdev)) 1630 return NOTIFY_DONE; 1631 1632 extack = netdev_notifier_info_to_extack(&info->info); 1633 1634 switch (event) { 1635 case NETDEV_PRECHANGEUPPER: 1636 upper_dev = info->upper_dev; 1637 if (!netif_is_bridge_master(upper_dev)) 1638 break; 1639 1640 if (!br_vlan_enabled(upper_dev)) { 1641 NL_SET_ERR_MSG_MOD(extack, "Cannot join a VLAN-unaware bridge"); 1642 err = -EOPNOTSUPP; 1643 goto out; 1644 } 1645 1646 err = dpaa2_switch_prevent_bridging_with_8021q_upper(netdev); 1647 if (err) { 1648 NL_SET_ERR_MSG_MOD(extack, 1649 "Cannot join a bridge while VLAN uppers are present"); 1650 goto out; 1651 } 1652 1653 break; 1654 case NETDEV_CHANGEUPPER: 1655 upper_dev = info->upper_dev; 1656 if (netif_is_bridge_master(upper_dev)) { 1657 if (info->linking) 1658 err = dpaa2_switch_port_bridge_join(netdev, upper_dev); 1659 else 1660 err = dpaa2_switch_port_bridge_leave(netdev); 1661 } 1662 break; 1663 } 1664 1665 out: 1666 return notifier_from_errno(err); 1667 } 1668 1669 struct ethsw_switchdev_event_work { 1670 struct work_struct work; 1671 struct switchdev_notifier_fdb_info fdb_info; 1672 struct net_device *dev; 1673 unsigned long event; 1674 }; 1675 1676 static void dpaa2_switch_event_work(struct work_struct *work) 1677 { 1678 struct ethsw_switchdev_event_work *switchdev_work = 1679 container_of(work, struct ethsw_switchdev_event_work, work); 1680 struct net_device *dev = switchdev_work->dev; 1681 struct switchdev_notifier_fdb_info *fdb_info; 1682 int err; 1683 1684 rtnl_lock(); 1685 fdb_info = &switchdev_work->fdb_info; 1686 1687 switch (switchdev_work->event) { 1688 case SWITCHDEV_FDB_ADD_TO_DEVICE: 1689 if (!fdb_info->added_by_user) 1690 break; 1691 if (is_unicast_ether_addr(fdb_info->addr)) 1692 err = dpaa2_switch_port_fdb_add_uc(netdev_priv(dev), 1693 fdb_info->addr); 1694 else 1695 err = dpaa2_switch_port_fdb_add_mc(netdev_priv(dev), 1696 fdb_info->addr); 1697 if (err) 1698 break; 1699 fdb_info->offloaded = true; 1700 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev, 1701 &fdb_info->info, NULL); 1702 break; 1703 case SWITCHDEV_FDB_DEL_TO_DEVICE: 1704 if (!fdb_info->added_by_user) 1705 break; 1706 if (is_unicast_ether_addr(fdb_info->addr)) 1707 dpaa2_switch_port_fdb_del_uc(netdev_priv(dev), fdb_info->addr); 1708 else 1709 dpaa2_switch_port_fdb_del_mc(netdev_priv(dev), fdb_info->addr); 1710 break; 1711 } 1712 1713 rtnl_unlock(); 1714 kfree(switchdev_work->fdb_info.addr); 1715 kfree(switchdev_work); 1716 dev_put(dev); 1717 } 1718 1719 /* Called under rcu_read_lock() */ 1720 static int dpaa2_switch_port_event(struct notifier_block *nb, 1721 unsigned long event, void *ptr) 1722 { 1723 struct net_device *dev = switchdev_notifier_info_to_dev(ptr); 1724 struct ethsw_port_priv *port_priv = netdev_priv(dev); 1725 struct ethsw_switchdev_event_work *switchdev_work; 1726 struct switchdev_notifier_fdb_info *fdb_info = ptr; 1727 struct ethsw_core *ethsw = port_priv->ethsw_data; 1728 1729 if (event == SWITCHDEV_PORT_ATTR_SET) 1730 return dpaa2_switch_port_attr_set_event(dev, ptr); 1731 1732 if (!dpaa2_switch_port_dev_check(dev)) 1733 return NOTIFY_DONE; 1734 1735 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC); 1736 if (!switchdev_work) 1737 return NOTIFY_BAD; 1738 1739 INIT_WORK(&switchdev_work->work, dpaa2_switch_event_work); 1740 switchdev_work->dev = dev; 1741 switchdev_work->event = event; 1742 1743 switch (event) { 1744 case SWITCHDEV_FDB_ADD_TO_DEVICE: 1745 case SWITCHDEV_FDB_DEL_TO_DEVICE: 1746 memcpy(&switchdev_work->fdb_info, ptr, 1747 sizeof(switchdev_work->fdb_info)); 1748 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC); 1749 if (!switchdev_work->fdb_info.addr) 1750 goto err_addr_alloc; 1751 1752 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr, 1753 fdb_info->addr); 1754 1755 /* Take a reference on the device to avoid being freed. */ 1756 dev_hold(dev); 1757 break; 1758 default: 1759 kfree(switchdev_work); 1760 return NOTIFY_DONE; 1761 } 1762 1763 queue_work(ethsw->workqueue, &switchdev_work->work); 1764 1765 return NOTIFY_DONE; 1766 1767 err_addr_alloc: 1768 kfree(switchdev_work); 1769 return NOTIFY_BAD; 1770 } 1771 1772 static int dpaa2_switch_port_obj_event(unsigned long event, 1773 struct net_device *netdev, 1774 struct switchdev_notifier_port_obj_info *port_obj_info) 1775 { 1776 int err = -EOPNOTSUPP; 1777 1778 if (!dpaa2_switch_port_dev_check(netdev)) 1779 return NOTIFY_DONE; 1780 1781 switch (event) { 1782 case SWITCHDEV_PORT_OBJ_ADD: 1783 err = dpaa2_switch_port_obj_add(netdev, port_obj_info->obj); 1784 break; 1785 case SWITCHDEV_PORT_OBJ_DEL: 1786 err = dpaa2_switch_port_obj_del(netdev, port_obj_info->obj); 1787 break; 1788 } 1789 1790 port_obj_info->handled = true; 1791 return notifier_from_errno(err); 1792 } 1793 1794 static int dpaa2_switch_port_blocking_event(struct notifier_block *nb, 1795 unsigned long event, void *ptr) 1796 { 1797 struct net_device *dev = switchdev_notifier_info_to_dev(ptr); 1798 1799 switch (event) { 1800 case SWITCHDEV_PORT_OBJ_ADD: 1801 case SWITCHDEV_PORT_OBJ_DEL: 1802 return dpaa2_switch_port_obj_event(event, dev, ptr); 1803 case SWITCHDEV_PORT_ATTR_SET: 1804 return dpaa2_switch_port_attr_set_event(dev, ptr); 1805 } 1806 1807 return NOTIFY_DONE; 1808 } 1809 1810 /* Build a linear skb based on a single-buffer frame descriptor */ 1811 static struct sk_buff *dpaa2_switch_build_linear_skb(struct ethsw_core *ethsw, 1812 const struct dpaa2_fd *fd) 1813 { 1814 u16 fd_offset = dpaa2_fd_get_offset(fd); 1815 dma_addr_t addr = dpaa2_fd_get_addr(fd); 1816 u32 fd_length = dpaa2_fd_get_len(fd); 1817 struct device *dev = ethsw->dev; 1818 struct sk_buff *skb = NULL; 1819 void *fd_vaddr; 1820 1821 fd_vaddr = dpaa2_iova_to_virt(ethsw->iommu_domain, addr); 1822 dma_unmap_page(dev, addr, DPAA2_SWITCH_RX_BUF_SIZE, 1823 DMA_FROM_DEVICE); 1824 1825 skb = build_skb(fd_vaddr, DPAA2_SWITCH_RX_BUF_SIZE + 1826 SKB_DATA_ALIGN(sizeof(struct skb_shared_info))); 1827 if (unlikely(!skb)) { 1828 dev_err(dev, "build_skb() failed\n"); 1829 return NULL; 1830 } 1831 1832 skb_reserve(skb, fd_offset); 1833 skb_put(skb, fd_length); 1834 1835 ethsw->buf_count--; 1836 1837 return skb; 1838 } 1839 1840 static void dpaa2_switch_tx_conf(struct dpaa2_switch_fq *fq, 1841 const struct dpaa2_fd *fd) 1842 { 1843 dpaa2_switch_free_fd(fq->ethsw, fd); 1844 } 1845 1846 static void dpaa2_switch_rx(struct dpaa2_switch_fq *fq, 1847 const struct dpaa2_fd *fd) 1848 { 1849 struct ethsw_core *ethsw = fq->ethsw; 1850 struct ethsw_port_priv *port_priv; 1851 struct net_device *netdev; 1852 struct vlan_ethhdr *hdr; 1853 struct sk_buff *skb; 1854 u16 vlan_tci, vid; 1855 int if_id, err; 1856 1857 /* get switch ingress interface ID */ 1858 if_id = upper_32_bits(dpaa2_fd_get_flc(fd)) & 0x0000FFFF; 1859 1860 if (if_id >= ethsw->sw_attr.num_ifs) { 1861 dev_err(ethsw->dev, "Frame received from unknown interface!\n"); 1862 goto err_free_fd; 1863 } 1864 port_priv = ethsw->ports[if_id]; 1865 netdev = port_priv->netdev; 1866 1867 /* build the SKB based on the FD received */ 1868 if (dpaa2_fd_get_format(fd) != dpaa2_fd_single) { 1869 if (net_ratelimit()) { 1870 netdev_err(netdev, "Received invalid frame format\n"); 1871 goto err_free_fd; 1872 } 1873 } 1874 1875 skb = dpaa2_switch_build_linear_skb(ethsw, fd); 1876 if (unlikely(!skb)) 1877 goto err_free_fd; 1878 1879 skb_reset_mac_header(skb); 1880 1881 /* Remove the VLAN header if the packet that we just received has a vid 1882 * equal to the port PVIDs. Since the dpaa2-switch can operate only in 1883 * VLAN-aware mode and no alterations are made on the packet when it's 1884 * redirected/mirrored to the control interface, we are sure that there 1885 * will always be a VLAN header present. 1886 */ 1887 hdr = vlan_eth_hdr(skb); 1888 vid = ntohs(hdr->h_vlan_TCI) & VLAN_VID_MASK; 1889 if (vid == port_priv->pvid) { 1890 err = __skb_vlan_pop(skb, &vlan_tci); 1891 if (err) { 1892 dev_info(ethsw->dev, "__skb_vlan_pop() returned %d", err); 1893 goto err_free_fd; 1894 } 1895 } 1896 1897 skb->dev = netdev; 1898 skb->protocol = eth_type_trans(skb, skb->dev); 1899 1900 netif_receive_skb(skb); 1901 1902 return; 1903 1904 err_free_fd: 1905 dpaa2_switch_free_fd(ethsw, fd); 1906 } 1907 1908 static void dpaa2_switch_detect_features(struct ethsw_core *ethsw) 1909 { 1910 ethsw->features = 0; 1911 1912 if (ethsw->major > 8 || (ethsw->major == 8 && ethsw->minor >= 6)) 1913 ethsw->features |= ETHSW_FEATURE_MAC_ADDR; 1914 } 1915 1916 static int dpaa2_switch_setup_fqs(struct ethsw_core *ethsw) 1917 { 1918 struct dpsw_ctrl_if_attr ctrl_if_attr; 1919 struct device *dev = ethsw->dev; 1920 int i = 0; 1921 int err; 1922 1923 err = dpsw_ctrl_if_get_attributes(ethsw->mc_io, 0, ethsw->dpsw_handle, 1924 &ctrl_if_attr); 1925 if (err) { 1926 dev_err(dev, "dpsw_ctrl_if_get_attributes() = %d\n", err); 1927 return err; 1928 } 1929 1930 ethsw->fq[i].fqid = ctrl_if_attr.rx_fqid; 1931 ethsw->fq[i].ethsw = ethsw; 1932 ethsw->fq[i++].type = DPSW_QUEUE_RX; 1933 1934 ethsw->fq[i].fqid = ctrl_if_attr.tx_err_conf_fqid; 1935 ethsw->fq[i].ethsw = ethsw; 1936 ethsw->fq[i++].type = DPSW_QUEUE_TX_ERR_CONF; 1937 1938 return 0; 1939 } 1940 1941 /* Free buffers acquired from the buffer pool or which were meant to 1942 * be released in the pool 1943 */ 1944 static void dpaa2_switch_free_bufs(struct ethsw_core *ethsw, u64 *buf_array, int count) 1945 { 1946 struct device *dev = ethsw->dev; 1947 void *vaddr; 1948 int i; 1949 1950 for (i = 0; i < count; i++) { 1951 vaddr = dpaa2_iova_to_virt(ethsw->iommu_domain, buf_array[i]); 1952 dma_unmap_page(dev, buf_array[i], DPAA2_SWITCH_RX_BUF_SIZE, 1953 DMA_FROM_DEVICE); 1954 free_pages((unsigned long)vaddr, 0); 1955 } 1956 } 1957 1958 /* Perform a single release command to add buffers 1959 * to the specified buffer pool 1960 */ 1961 static int dpaa2_switch_add_bufs(struct ethsw_core *ethsw, u16 bpid) 1962 { 1963 struct device *dev = ethsw->dev; 1964 u64 buf_array[BUFS_PER_CMD]; 1965 struct page *page; 1966 int retries = 0; 1967 dma_addr_t addr; 1968 int err; 1969 int i; 1970 1971 for (i = 0; i < BUFS_PER_CMD; i++) { 1972 /* Allocate one page for each Rx buffer. WRIOP sees 1973 * the entire page except for a tailroom reserved for 1974 * skb shared info 1975 */ 1976 page = dev_alloc_pages(0); 1977 if (!page) { 1978 dev_err(dev, "buffer allocation failed\n"); 1979 goto err_alloc; 1980 } 1981 1982 addr = dma_map_page(dev, page, 0, DPAA2_SWITCH_RX_BUF_SIZE, 1983 DMA_FROM_DEVICE); 1984 if (dma_mapping_error(dev, addr)) { 1985 dev_err(dev, "dma_map_single() failed\n"); 1986 goto err_map; 1987 } 1988 buf_array[i] = addr; 1989 } 1990 1991 release_bufs: 1992 /* In case the portal is busy, retry until successful or 1993 * max retries hit. 1994 */ 1995 while ((err = dpaa2_io_service_release(NULL, bpid, 1996 buf_array, i)) == -EBUSY) { 1997 if (retries++ >= DPAA2_SWITCH_SWP_BUSY_RETRIES) 1998 break; 1999 2000 cpu_relax(); 2001 } 2002 2003 /* If release command failed, clean up and bail out. */ 2004 if (err) { 2005 dpaa2_switch_free_bufs(ethsw, buf_array, i); 2006 return 0; 2007 } 2008 2009 return i; 2010 2011 err_map: 2012 __free_pages(page, 0); 2013 err_alloc: 2014 /* If we managed to allocate at least some buffers, 2015 * release them to hardware 2016 */ 2017 if (i) 2018 goto release_bufs; 2019 2020 return 0; 2021 } 2022 2023 static int dpaa2_switch_refill_bp(struct ethsw_core *ethsw) 2024 { 2025 int *count = ðsw->buf_count; 2026 int new_count; 2027 int err = 0; 2028 2029 if (unlikely(*count < DPAA2_ETHSW_REFILL_THRESH)) { 2030 do { 2031 new_count = dpaa2_switch_add_bufs(ethsw, ethsw->bpid); 2032 if (unlikely(!new_count)) { 2033 /* Out of memory; abort for now, we'll 2034 * try later on 2035 */ 2036 break; 2037 } 2038 *count += new_count; 2039 } while (*count < DPAA2_ETHSW_NUM_BUFS); 2040 2041 if (unlikely(*count < DPAA2_ETHSW_NUM_BUFS)) 2042 err = -ENOMEM; 2043 } 2044 2045 return err; 2046 } 2047 2048 static int dpaa2_switch_seed_bp(struct ethsw_core *ethsw) 2049 { 2050 int *count, i; 2051 2052 for (i = 0; i < DPAA2_ETHSW_NUM_BUFS; i += BUFS_PER_CMD) { 2053 count = ðsw->buf_count; 2054 *count += dpaa2_switch_add_bufs(ethsw, ethsw->bpid); 2055 2056 if (unlikely(*count < BUFS_PER_CMD)) 2057 return -ENOMEM; 2058 } 2059 2060 return 0; 2061 } 2062 2063 static void dpaa2_switch_drain_bp(struct ethsw_core *ethsw) 2064 { 2065 u64 buf_array[BUFS_PER_CMD]; 2066 int ret; 2067 2068 do { 2069 ret = dpaa2_io_service_acquire(NULL, ethsw->bpid, 2070 buf_array, BUFS_PER_CMD); 2071 if (ret < 0) { 2072 dev_err(ethsw->dev, 2073 "dpaa2_io_service_acquire() = %d\n", ret); 2074 return; 2075 } 2076 dpaa2_switch_free_bufs(ethsw, buf_array, ret); 2077 2078 } while (ret); 2079 } 2080 2081 static int dpaa2_switch_setup_dpbp(struct ethsw_core *ethsw) 2082 { 2083 struct dpsw_ctrl_if_pools_cfg dpsw_ctrl_if_pools_cfg = { 0 }; 2084 struct device *dev = ethsw->dev; 2085 struct fsl_mc_device *dpbp_dev; 2086 struct dpbp_attr dpbp_attrs; 2087 int err; 2088 2089 err = fsl_mc_object_allocate(to_fsl_mc_device(dev), FSL_MC_POOL_DPBP, 2090 &dpbp_dev); 2091 if (err) { 2092 if (err == -ENXIO) 2093 err = -EPROBE_DEFER; 2094 else 2095 dev_err(dev, "DPBP device allocation failed\n"); 2096 return err; 2097 } 2098 ethsw->dpbp_dev = dpbp_dev; 2099 2100 err = dpbp_open(ethsw->mc_io, 0, dpbp_dev->obj_desc.id, 2101 &dpbp_dev->mc_handle); 2102 if (err) { 2103 dev_err(dev, "dpbp_open() failed\n"); 2104 goto err_open; 2105 } 2106 2107 err = dpbp_reset(ethsw->mc_io, 0, dpbp_dev->mc_handle); 2108 if (err) { 2109 dev_err(dev, "dpbp_reset() failed\n"); 2110 goto err_reset; 2111 } 2112 2113 err = dpbp_enable(ethsw->mc_io, 0, dpbp_dev->mc_handle); 2114 if (err) { 2115 dev_err(dev, "dpbp_enable() failed\n"); 2116 goto err_enable; 2117 } 2118 2119 err = dpbp_get_attributes(ethsw->mc_io, 0, dpbp_dev->mc_handle, 2120 &dpbp_attrs); 2121 if (err) { 2122 dev_err(dev, "dpbp_get_attributes() failed\n"); 2123 goto err_get_attr; 2124 } 2125 2126 dpsw_ctrl_if_pools_cfg.num_dpbp = 1; 2127 dpsw_ctrl_if_pools_cfg.pools[0].dpbp_id = dpbp_attrs.id; 2128 dpsw_ctrl_if_pools_cfg.pools[0].buffer_size = DPAA2_SWITCH_RX_BUF_SIZE; 2129 dpsw_ctrl_if_pools_cfg.pools[0].backup_pool = 0; 2130 2131 err = dpsw_ctrl_if_set_pools(ethsw->mc_io, 0, ethsw->dpsw_handle, 2132 &dpsw_ctrl_if_pools_cfg); 2133 if (err) { 2134 dev_err(dev, "dpsw_ctrl_if_set_pools() failed\n"); 2135 goto err_get_attr; 2136 } 2137 ethsw->bpid = dpbp_attrs.id; 2138 2139 return 0; 2140 2141 err_get_attr: 2142 dpbp_disable(ethsw->mc_io, 0, dpbp_dev->mc_handle); 2143 err_enable: 2144 err_reset: 2145 dpbp_close(ethsw->mc_io, 0, dpbp_dev->mc_handle); 2146 err_open: 2147 fsl_mc_object_free(dpbp_dev); 2148 return err; 2149 } 2150 2151 static void dpaa2_switch_free_dpbp(struct ethsw_core *ethsw) 2152 { 2153 dpbp_disable(ethsw->mc_io, 0, ethsw->dpbp_dev->mc_handle); 2154 dpbp_close(ethsw->mc_io, 0, ethsw->dpbp_dev->mc_handle); 2155 fsl_mc_object_free(ethsw->dpbp_dev); 2156 } 2157 2158 static int dpaa2_switch_alloc_rings(struct ethsw_core *ethsw) 2159 { 2160 int i; 2161 2162 for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++) { 2163 ethsw->fq[i].store = 2164 dpaa2_io_store_create(DPAA2_SWITCH_STORE_SIZE, 2165 ethsw->dev); 2166 if (!ethsw->fq[i].store) { 2167 dev_err(ethsw->dev, "dpaa2_io_store_create failed\n"); 2168 while (--i >= 0) 2169 dpaa2_io_store_destroy(ethsw->fq[i].store); 2170 return -ENOMEM; 2171 } 2172 } 2173 2174 return 0; 2175 } 2176 2177 static void dpaa2_switch_destroy_rings(struct ethsw_core *ethsw) 2178 { 2179 int i; 2180 2181 for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++) 2182 dpaa2_io_store_destroy(ethsw->fq[i].store); 2183 } 2184 2185 static int dpaa2_switch_pull_fq(struct dpaa2_switch_fq *fq) 2186 { 2187 int err, retries = 0; 2188 2189 /* Try to pull from the FQ while the portal is busy and we didn't hit 2190 * the maximum number fo retries 2191 */ 2192 do { 2193 err = dpaa2_io_service_pull_fq(NULL, fq->fqid, fq->store); 2194 cpu_relax(); 2195 } while (err == -EBUSY && retries++ < DPAA2_SWITCH_SWP_BUSY_RETRIES); 2196 2197 if (unlikely(err)) 2198 dev_err(fq->ethsw->dev, "dpaa2_io_service_pull err %d", err); 2199 2200 return err; 2201 } 2202 2203 /* Consume all frames pull-dequeued into the store */ 2204 static int dpaa2_switch_store_consume(struct dpaa2_switch_fq *fq) 2205 { 2206 struct ethsw_core *ethsw = fq->ethsw; 2207 int cleaned = 0, is_last; 2208 struct dpaa2_dq *dq; 2209 int retries = 0; 2210 2211 do { 2212 /* Get the next available FD from the store */ 2213 dq = dpaa2_io_store_next(fq->store, &is_last); 2214 if (unlikely(!dq)) { 2215 if (retries++ >= DPAA2_SWITCH_SWP_BUSY_RETRIES) { 2216 dev_err_once(ethsw->dev, 2217 "No valid dequeue response\n"); 2218 return -ETIMEDOUT; 2219 } 2220 continue; 2221 } 2222 2223 if (fq->type == DPSW_QUEUE_RX) 2224 dpaa2_switch_rx(fq, dpaa2_dq_fd(dq)); 2225 else 2226 dpaa2_switch_tx_conf(fq, dpaa2_dq_fd(dq)); 2227 cleaned++; 2228 2229 } while (!is_last); 2230 2231 return cleaned; 2232 } 2233 2234 /* NAPI poll routine */ 2235 static int dpaa2_switch_poll(struct napi_struct *napi, int budget) 2236 { 2237 int err, cleaned = 0, store_cleaned, work_done; 2238 struct dpaa2_switch_fq *fq; 2239 int retries = 0; 2240 2241 fq = container_of(napi, struct dpaa2_switch_fq, napi); 2242 2243 do { 2244 err = dpaa2_switch_pull_fq(fq); 2245 if (unlikely(err)) 2246 break; 2247 2248 /* Refill pool if appropriate */ 2249 dpaa2_switch_refill_bp(fq->ethsw); 2250 2251 store_cleaned = dpaa2_switch_store_consume(fq); 2252 cleaned += store_cleaned; 2253 2254 if (cleaned >= budget) { 2255 work_done = budget; 2256 goto out; 2257 } 2258 2259 } while (store_cleaned); 2260 2261 /* We didn't consume the entire budget, so finish napi and re-enable 2262 * data availability notifications 2263 */ 2264 napi_complete_done(napi, cleaned); 2265 do { 2266 err = dpaa2_io_service_rearm(NULL, &fq->nctx); 2267 cpu_relax(); 2268 } while (err == -EBUSY && retries++ < DPAA2_SWITCH_SWP_BUSY_RETRIES); 2269 2270 work_done = max(cleaned, 1); 2271 out: 2272 2273 return work_done; 2274 } 2275 2276 static void dpaa2_switch_fqdan_cb(struct dpaa2_io_notification_ctx *nctx) 2277 { 2278 struct dpaa2_switch_fq *fq; 2279 2280 fq = container_of(nctx, struct dpaa2_switch_fq, nctx); 2281 2282 napi_schedule(&fq->napi); 2283 } 2284 2285 static int dpaa2_switch_setup_dpio(struct ethsw_core *ethsw) 2286 { 2287 struct dpsw_ctrl_if_queue_cfg queue_cfg; 2288 struct dpaa2_io_notification_ctx *nctx; 2289 int err, i, j; 2290 2291 for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++) { 2292 nctx = ðsw->fq[i].nctx; 2293 2294 /* Register a new software context for the FQID. 2295 * By using NULL as the first parameter, we specify that we do 2296 * not care on which cpu are interrupts received for this queue 2297 */ 2298 nctx->is_cdan = 0; 2299 nctx->id = ethsw->fq[i].fqid; 2300 nctx->desired_cpu = DPAA2_IO_ANY_CPU; 2301 nctx->cb = dpaa2_switch_fqdan_cb; 2302 err = dpaa2_io_service_register(NULL, nctx, ethsw->dev); 2303 if (err) { 2304 err = -EPROBE_DEFER; 2305 goto err_register; 2306 } 2307 2308 queue_cfg.options = DPSW_CTRL_IF_QUEUE_OPT_DEST | 2309 DPSW_CTRL_IF_QUEUE_OPT_USER_CTX; 2310 queue_cfg.dest_cfg.dest_type = DPSW_CTRL_IF_DEST_DPIO; 2311 queue_cfg.dest_cfg.dest_id = nctx->dpio_id; 2312 queue_cfg.dest_cfg.priority = 0; 2313 queue_cfg.user_ctx = nctx->qman64; 2314 2315 err = dpsw_ctrl_if_set_queue(ethsw->mc_io, 0, 2316 ethsw->dpsw_handle, 2317 ethsw->fq[i].type, 2318 &queue_cfg); 2319 if (err) 2320 goto err_set_queue; 2321 } 2322 2323 return 0; 2324 2325 err_set_queue: 2326 dpaa2_io_service_deregister(NULL, nctx, ethsw->dev); 2327 err_register: 2328 for (j = 0; j < i; j++) 2329 dpaa2_io_service_deregister(NULL, ðsw->fq[j].nctx, 2330 ethsw->dev); 2331 2332 return err; 2333 } 2334 2335 static void dpaa2_switch_free_dpio(struct ethsw_core *ethsw) 2336 { 2337 int i; 2338 2339 for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++) 2340 dpaa2_io_service_deregister(NULL, ðsw->fq[i].nctx, 2341 ethsw->dev); 2342 } 2343 2344 static int dpaa2_switch_ctrl_if_setup(struct ethsw_core *ethsw) 2345 { 2346 int err; 2347 2348 /* setup FQs for Rx and Tx Conf */ 2349 err = dpaa2_switch_setup_fqs(ethsw); 2350 if (err) 2351 return err; 2352 2353 /* setup the buffer pool needed on the Rx path */ 2354 err = dpaa2_switch_setup_dpbp(ethsw); 2355 if (err) 2356 return err; 2357 2358 err = dpaa2_switch_seed_bp(ethsw); 2359 if (err) 2360 goto err_free_dpbp; 2361 2362 err = dpaa2_switch_alloc_rings(ethsw); 2363 if (err) 2364 goto err_drain_dpbp; 2365 2366 err = dpaa2_switch_setup_dpio(ethsw); 2367 if (err) 2368 goto err_destroy_rings; 2369 2370 err = dpsw_ctrl_if_enable(ethsw->mc_io, 0, ethsw->dpsw_handle); 2371 if (err) { 2372 dev_err(ethsw->dev, "dpsw_ctrl_if_enable err %d\n", err); 2373 goto err_deregister_dpio; 2374 } 2375 2376 return 0; 2377 2378 err_deregister_dpio: 2379 dpaa2_switch_free_dpio(ethsw); 2380 err_destroy_rings: 2381 dpaa2_switch_destroy_rings(ethsw); 2382 err_drain_dpbp: 2383 dpaa2_switch_drain_bp(ethsw); 2384 err_free_dpbp: 2385 dpaa2_switch_free_dpbp(ethsw); 2386 2387 return err; 2388 } 2389 2390 static int dpaa2_switch_init(struct fsl_mc_device *sw_dev) 2391 { 2392 struct device *dev = &sw_dev->dev; 2393 struct ethsw_core *ethsw = dev_get_drvdata(dev); 2394 struct dpsw_vlan_if_cfg vcfg = {0}; 2395 struct dpsw_tci_cfg tci_cfg = {0}; 2396 struct dpsw_stp_cfg stp_cfg; 2397 int err; 2398 u16 i; 2399 2400 ethsw->dev_id = sw_dev->obj_desc.id; 2401 2402 err = dpsw_open(ethsw->mc_io, 0, ethsw->dev_id, ðsw->dpsw_handle); 2403 if (err) { 2404 dev_err(dev, "dpsw_open err %d\n", err); 2405 return err; 2406 } 2407 2408 err = dpsw_get_attributes(ethsw->mc_io, 0, ethsw->dpsw_handle, 2409 ðsw->sw_attr); 2410 if (err) { 2411 dev_err(dev, "dpsw_get_attributes err %d\n", err); 2412 goto err_close; 2413 } 2414 2415 err = dpsw_get_api_version(ethsw->mc_io, 0, 2416 ðsw->major, 2417 ðsw->minor); 2418 if (err) { 2419 dev_err(dev, "dpsw_get_api_version err %d\n", err); 2420 goto err_close; 2421 } 2422 2423 /* Minimum supported DPSW version check */ 2424 if (ethsw->major < DPSW_MIN_VER_MAJOR || 2425 (ethsw->major == DPSW_MIN_VER_MAJOR && 2426 ethsw->minor < DPSW_MIN_VER_MINOR)) { 2427 dev_err(dev, "DPSW version %d:%d not supported. Use firmware 10.28.0 or greater.\n", 2428 ethsw->major, ethsw->minor); 2429 err = -EOPNOTSUPP; 2430 goto err_close; 2431 } 2432 2433 if (!dpaa2_switch_supports_cpu_traffic(ethsw)) { 2434 err = -EOPNOTSUPP; 2435 goto err_close; 2436 } 2437 2438 dpaa2_switch_detect_features(ethsw); 2439 2440 err = dpsw_reset(ethsw->mc_io, 0, ethsw->dpsw_handle); 2441 if (err) { 2442 dev_err(dev, "dpsw_reset err %d\n", err); 2443 goto err_close; 2444 } 2445 2446 stp_cfg.vlan_id = DEFAULT_VLAN_ID; 2447 stp_cfg.state = DPSW_STP_STATE_FORWARDING; 2448 2449 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) { 2450 err = dpsw_if_disable(ethsw->mc_io, 0, ethsw->dpsw_handle, i); 2451 if (err) { 2452 dev_err(dev, "dpsw_if_disable err %d\n", err); 2453 goto err_close; 2454 } 2455 2456 err = dpsw_if_set_stp(ethsw->mc_io, 0, ethsw->dpsw_handle, i, 2457 &stp_cfg); 2458 if (err) { 2459 dev_err(dev, "dpsw_if_set_stp err %d for port %d\n", 2460 err, i); 2461 goto err_close; 2462 } 2463 2464 /* Switch starts with all ports configured to VLAN 1. Need to 2465 * remove this setting to allow configuration at bridge join 2466 */ 2467 vcfg.num_ifs = 1; 2468 vcfg.if_id[0] = i; 2469 err = dpsw_vlan_remove_if_untagged(ethsw->mc_io, 0, ethsw->dpsw_handle, 2470 DEFAULT_VLAN_ID, &vcfg); 2471 if (err) { 2472 dev_err(dev, "dpsw_vlan_remove_if_untagged err %d\n", 2473 err); 2474 goto err_close; 2475 } 2476 2477 tci_cfg.vlan_id = 4095; 2478 err = dpsw_if_set_tci(ethsw->mc_io, 0, ethsw->dpsw_handle, i, &tci_cfg); 2479 if (err) { 2480 dev_err(dev, "dpsw_if_set_tci err %d\n", err); 2481 goto err_close; 2482 } 2483 2484 err = dpsw_vlan_remove_if(ethsw->mc_io, 0, ethsw->dpsw_handle, 2485 DEFAULT_VLAN_ID, &vcfg); 2486 if (err) { 2487 dev_err(dev, "dpsw_vlan_remove_if err %d\n", err); 2488 goto err_close; 2489 } 2490 } 2491 2492 err = dpsw_vlan_remove(ethsw->mc_io, 0, ethsw->dpsw_handle, DEFAULT_VLAN_ID); 2493 if (err) { 2494 dev_err(dev, "dpsw_vlan_remove err %d\n", err); 2495 goto err_close; 2496 } 2497 2498 ethsw->workqueue = alloc_ordered_workqueue("%s_%d_ordered", 2499 WQ_MEM_RECLAIM, "ethsw", 2500 ethsw->sw_attr.id); 2501 if (!ethsw->workqueue) { 2502 err = -ENOMEM; 2503 goto err_close; 2504 } 2505 2506 err = dpsw_fdb_remove(ethsw->mc_io, 0, ethsw->dpsw_handle, 0); 2507 if (err) 2508 goto err_destroy_ordered_workqueue; 2509 2510 err = dpaa2_switch_ctrl_if_setup(ethsw); 2511 if (err) 2512 goto err_destroy_ordered_workqueue; 2513 2514 return 0; 2515 2516 err_destroy_ordered_workqueue: 2517 destroy_workqueue(ethsw->workqueue); 2518 2519 err_close: 2520 dpsw_close(ethsw->mc_io, 0, ethsw->dpsw_handle); 2521 return err; 2522 } 2523 2524 static int dpaa2_switch_port_init(struct ethsw_port_priv *port_priv, u16 port) 2525 { 2526 struct switchdev_obj_port_vlan vlan = { 2527 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN, 2528 .vid = DEFAULT_VLAN_ID, 2529 .flags = BRIDGE_VLAN_INFO_UNTAGGED | BRIDGE_VLAN_INFO_PVID, 2530 }; 2531 struct net_device *netdev = port_priv->netdev; 2532 struct ethsw_core *ethsw = port_priv->ethsw_data; 2533 struct dpsw_fdb_cfg fdb_cfg = {0}; 2534 struct dpaa2_switch_fdb *fdb; 2535 struct dpsw_if_attr dpsw_if_attr; 2536 u16 fdb_id; 2537 int err; 2538 2539 /* Get the Tx queue for this specific port */ 2540 err = dpsw_if_get_attributes(ethsw->mc_io, 0, ethsw->dpsw_handle, 2541 port_priv->idx, &dpsw_if_attr); 2542 if (err) { 2543 netdev_err(netdev, "dpsw_if_get_attributes err %d\n", err); 2544 return err; 2545 } 2546 port_priv->tx_qdid = dpsw_if_attr.qdid; 2547 2548 /* Create a FDB table for this particular switch port */ 2549 fdb_cfg.num_fdb_entries = ethsw->sw_attr.max_fdb_entries / ethsw->sw_attr.num_ifs; 2550 err = dpsw_fdb_add(ethsw->mc_io, 0, ethsw->dpsw_handle, 2551 &fdb_id, &fdb_cfg); 2552 if (err) { 2553 netdev_err(netdev, "dpsw_fdb_add err %d\n", err); 2554 return err; 2555 } 2556 2557 /* Find an unused dpaa2_switch_fdb structure and use it */ 2558 fdb = dpaa2_switch_fdb_get_unused(ethsw); 2559 fdb->fdb_id = fdb_id; 2560 fdb->in_use = true; 2561 fdb->bridge_dev = NULL; 2562 port_priv->fdb = fdb; 2563 2564 /* We need to add VLAN 1 as the PVID on this port until it is under a 2565 * bridge since the DPAA2 switch is not able to handle the traffic in a 2566 * VLAN unaware fashion 2567 */ 2568 err = dpaa2_switch_port_vlans_add(netdev, &vlan); 2569 if (err) 2570 return err; 2571 2572 /* Setup the egress flooding domains (broadcast, unknown unicast */ 2573 err = dpaa2_switch_fdb_set_egress_flood(ethsw, port_priv->fdb->fdb_id); 2574 if (err) 2575 return err; 2576 2577 return err; 2578 } 2579 2580 static void dpaa2_switch_takedown(struct fsl_mc_device *sw_dev) 2581 { 2582 struct device *dev = &sw_dev->dev; 2583 struct ethsw_core *ethsw = dev_get_drvdata(dev); 2584 int err; 2585 2586 err = dpsw_close(ethsw->mc_io, 0, ethsw->dpsw_handle); 2587 if (err) 2588 dev_warn(dev, "dpsw_close err %d\n", err); 2589 } 2590 2591 static void dpaa2_switch_ctrl_if_teardown(struct ethsw_core *ethsw) 2592 { 2593 dpsw_ctrl_if_disable(ethsw->mc_io, 0, ethsw->dpsw_handle); 2594 dpaa2_switch_free_dpio(ethsw); 2595 dpaa2_switch_destroy_rings(ethsw); 2596 dpaa2_switch_drain_bp(ethsw); 2597 dpaa2_switch_free_dpbp(ethsw); 2598 } 2599 2600 static int dpaa2_switch_remove(struct fsl_mc_device *sw_dev) 2601 { 2602 struct ethsw_port_priv *port_priv; 2603 struct ethsw_core *ethsw; 2604 struct device *dev; 2605 int i; 2606 2607 dev = &sw_dev->dev; 2608 ethsw = dev_get_drvdata(dev); 2609 2610 dpaa2_switch_ctrl_if_teardown(ethsw); 2611 2612 dpaa2_switch_teardown_irqs(sw_dev); 2613 2614 dpsw_disable(ethsw->mc_io, 0, ethsw->dpsw_handle); 2615 2616 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) { 2617 port_priv = ethsw->ports[i]; 2618 unregister_netdev(port_priv->netdev); 2619 free_netdev(port_priv->netdev); 2620 } 2621 2622 kfree(ethsw->fdbs); 2623 kfree(ethsw->ports); 2624 2625 dpaa2_switch_takedown(sw_dev); 2626 2627 destroy_workqueue(ethsw->workqueue); 2628 2629 fsl_mc_portal_free(ethsw->mc_io); 2630 2631 kfree(ethsw); 2632 2633 dev_set_drvdata(dev, NULL); 2634 2635 return 0; 2636 } 2637 2638 static int dpaa2_switch_probe_port(struct ethsw_core *ethsw, 2639 u16 port_idx) 2640 { 2641 struct ethsw_port_priv *port_priv; 2642 struct device *dev = ethsw->dev; 2643 struct net_device *port_netdev; 2644 int err; 2645 2646 port_netdev = alloc_etherdev(sizeof(struct ethsw_port_priv)); 2647 if (!port_netdev) { 2648 dev_err(dev, "alloc_etherdev error\n"); 2649 return -ENOMEM; 2650 } 2651 2652 port_priv = netdev_priv(port_netdev); 2653 port_priv->netdev = port_netdev; 2654 port_priv->ethsw_data = ethsw; 2655 2656 port_priv->idx = port_idx; 2657 port_priv->stp_state = BR_STATE_FORWARDING; 2658 2659 SET_NETDEV_DEV(port_netdev, dev); 2660 port_netdev->netdev_ops = &dpaa2_switch_port_ops; 2661 port_netdev->ethtool_ops = &dpaa2_switch_port_ethtool_ops; 2662 2663 port_netdev->needed_headroom = DPAA2_SWITCH_NEEDED_HEADROOM; 2664 2665 /* Set MTU limits */ 2666 port_netdev->min_mtu = ETH_MIN_MTU; 2667 port_netdev->max_mtu = ETHSW_MAX_FRAME_LENGTH; 2668 2669 /* Populate the private port structure so that later calls to 2670 * dpaa2_switch_port_init() can use it. 2671 */ 2672 ethsw->ports[port_idx] = port_priv; 2673 2674 /* The DPAA2 switch's ingress path depends on the VLAN table, 2675 * thus we are not able to disable VLAN filtering. 2676 */ 2677 port_netdev->features = NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER; 2678 2679 err = dpaa2_switch_port_init(port_priv, port_idx); 2680 if (err) 2681 goto err_port_probe; 2682 2683 err = dpaa2_switch_port_set_mac_addr(port_priv); 2684 if (err) 2685 goto err_port_probe; 2686 2687 return 0; 2688 2689 err_port_probe: 2690 free_netdev(port_netdev); 2691 ethsw->ports[port_idx] = NULL; 2692 2693 return err; 2694 } 2695 2696 static int dpaa2_switch_probe(struct fsl_mc_device *sw_dev) 2697 { 2698 struct device *dev = &sw_dev->dev; 2699 struct ethsw_core *ethsw; 2700 int i, err; 2701 2702 /* Allocate switch core*/ 2703 ethsw = kzalloc(sizeof(*ethsw), GFP_KERNEL); 2704 2705 if (!ethsw) 2706 return -ENOMEM; 2707 2708 ethsw->dev = dev; 2709 ethsw->iommu_domain = iommu_get_domain_for_dev(dev); 2710 dev_set_drvdata(dev, ethsw); 2711 2712 err = fsl_mc_portal_allocate(sw_dev, FSL_MC_IO_ATOMIC_CONTEXT_PORTAL, 2713 ðsw->mc_io); 2714 if (err) { 2715 if (err == -ENXIO) 2716 err = -EPROBE_DEFER; 2717 else 2718 dev_err(dev, "fsl_mc_portal_allocate err %d\n", err); 2719 goto err_free_drvdata; 2720 } 2721 2722 err = dpaa2_switch_init(sw_dev); 2723 if (err) 2724 goto err_free_cmdport; 2725 2726 ethsw->ports = kcalloc(ethsw->sw_attr.num_ifs, sizeof(*ethsw->ports), 2727 GFP_KERNEL); 2728 if (!(ethsw->ports)) { 2729 err = -ENOMEM; 2730 goto err_takedown; 2731 } 2732 2733 ethsw->fdbs = kcalloc(ethsw->sw_attr.num_ifs, sizeof(*ethsw->fdbs), 2734 GFP_KERNEL); 2735 if (!ethsw->fdbs) { 2736 err = -ENOMEM; 2737 goto err_free_ports; 2738 } 2739 2740 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) { 2741 err = dpaa2_switch_probe_port(ethsw, i); 2742 if (err) 2743 goto err_free_netdev; 2744 } 2745 2746 /* Add a NAPI instance for each of the Rx queues. The first port's 2747 * net_device will be associated with the instances since we do not have 2748 * different queues for each switch ports. 2749 */ 2750 for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++) 2751 netif_napi_add(ethsw->ports[0]->netdev, 2752 ðsw->fq[i].napi, dpaa2_switch_poll, 2753 NAPI_POLL_WEIGHT); 2754 2755 err = dpsw_enable(ethsw->mc_io, 0, ethsw->dpsw_handle); 2756 if (err) { 2757 dev_err(ethsw->dev, "dpsw_enable err %d\n", err); 2758 goto err_free_netdev; 2759 } 2760 2761 /* Setup IRQs */ 2762 err = dpaa2_switch_setup_irqs(sw_dev); 2763 if (err) 2764 goto err_stop; 2765 2766 /* Register the netdev only when the entire setup is done and the 2767 * switch port interfaces are ready to receive traffic 2768 */ 2769 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) { 2770 err = register_netdev(ethsw->ports[i]->netdev); 2771 if (err < 0) { 2772 dev_err(dev, "register_netdev error %d\n", err); 2773 goto err_unregister_ports; 2774 } 2775 } 2776 2777 return 0; 2778 2779 err_unregister_ports: 2780 for (i--; i >= 0; i--) 2781 unregister_netdev(ethsw->ports[i]->netdev); 2782 dpaa2_switch_teardown_irqs(sw_dev); 2783 err_stop: 2784 dpsw_disable(ethsw->mc_io, 0, ethsw->dpsw_handle); 2785 err_free_netdev: 2786 for (i--; i >= 0; i--) 2787 free_netdev(ethsw->ports[i]->netdev); 2788 kfree(ethsw->fdbs); 2789 err_free_ports: 2790 kfree(ethsw->ports); 2791 2792 err_takedown: 2793 dpaa2_switch_takedown(sw_dev); 2794 2795 err_free_cmdport: 2796 fsl_mc_portal_free(ethsw->mc_io); 2797 2798 err_free_drvdata: 2799 kfree(ethsw); 2800 dev_set_drvdata(dev, NULL); 2801 2802 return err; 2803 } 2804 2805 static const struct fsl_mc_device_id dpaa2_switch_match_id_table[] = { 2806 { 2807 .vendor = FSL_MC_VENDOR_FREESCALE, 2808 .obj_type = "dpsw", 2809 }, 2810 { .vendor = 0x0 } 2811 }; 2812 MODULE_DEVICE_TABLE(fslmc, dpaa2_switch_match_id_table); 2813 2814 static struct fsl_mc_driver dpaa2_switch_drv = { 2815 .driver = { 2816 .name = KBUILD_MODNAME, 2817 .owner = THIS_MODULE, 2818 }, 2819 .probe = dpaa2_switch_probe, 2820 .remove = dpaa2_switch_remove, 2821 .match_id_table = dpaa2_switch_match_id_table 2822 }; 2823 2824 static struct notifier_block dpaa2_switch_port_nb __read_mostly = { 2825 .notifier_call = dpaa2_switch_port_netdevice_event, 2826 }; 2827 2828 static struct notifier_block dpaa2_switch_port_switchdev_nb = { 2829 .notifier_call = dpaa2_switch_port_event, 2830 }; 2831 2832 static struct notifier_block dpaa2_switch_port_switchdev_blocking_nb = { 2833 .notifier_call = dpaa2_switch_port_blocking_event, 2834 }; 2835 2836 static int dpaa2_switch_register_notifiers(void) 2837 { 2838 int err; 2839 2840 err = register_netdevice_notifier(&dpaa2_switch_port_nb); 2841 if (err) { 2842 pr_err("dpaa2-switch: failed to register net_device notifier (%d)\n", err); 2843 return err; 2844 } 2845 2846 err = register_switchdev_notifier(&dpaa2_switch_port_switchdev_nb); 2847 if (err) { 2848 pr_err("dpaa2-switch: failed to register switchdev notifier (%d)\n", err); 2849 goto err_switchdev_nb; 2850 } 2851 2852 err = register_switchdev_blocking_notifier(&dpaa2_switch_port_switchdev_blocking_nb); 2853 if (err) { 2854 pr_err("dpaa2-switch: failed to register switchdev blocking notifier (%d)\n", err); 2855 goto err_switchdev_blocking_nb; 2856 } 2857 2858 return 0; 2859 2860 err_switchdev_blocking_nb: 2861 unregister_switchdev_notifier(&dpaa2_switch_port_switchdev_nb); 2862 err_switchdev_nb: 2863 unregister_netdevice_notifier(&dpaa2_switch_port_nb); 2864 2865 return err; 2866 } 2867 2868 static void dpaa2_switch_unregister_notifiers(void) 2869 { 2870 int err; 2871 2872 err = unregister_switchdev_blocking_notifier(&dpaa2_switch_port_switchdev_blocking_nb); 2873 if (err) 2874 pr_err("dpaa2-switch: failed to unregister switchdev blocking notifier (%d)\n", 2875 err); 2876 2877 err = unregister_switchdev_notifier(&dpaa2_switch_port_switchdev_nb); 2878 if (err) 2879 pr_err("dpaa2-switch: failed to unregister switchdev notifier (%d)\n", err); 2880 2881 err = unregister_netdevice_notifier(&dpaa2_switch_port_nb); 2882 if (err) 2883 pr_err("dpaa2-switch: failed to unregister net_device notifier (%d)\n", err); 2884 } 2885 2886 static int __init dpaa2_switch_driver_init(void) 2887 { 2888 int err; 2889 2890 err = fsl_mc_driver_register(&dpaa2_switch_drv); 2891 if (err) 2892 return err; 2893 2894 err = dpaa2_switch_register_notifiers(); 2895 if (err) { 2896 fsl_mc_driver_unregister(&dpaa2_switch_drv); 2897 return err; 2898 } 2899 2900 return 0; 2901 } 2902 2903 static void __exit dpaa2_switch_driver_exit(void) 2904 { 2905 dpaa2_switch_unregister_notifiers(); 2906 fsl_mc_driver_unregister(&dpaa2_switch_drv); 2907 } 2908 2909 module_init(dpaa2_switch_driver_init); 2910 module_exit(dpaa2_switch_driver_exit); 2911 2912 MODULE_LICENSE("GPL v2"); 2913 MODULE_DESCRIPTION("DPAA2 Ethernet Switch Driver"); 2914