ocelot_net.c (51524B)
1// SPDX-License-Identifier: (GPL-2.0 OR MIT) 2/* Microsemi Ocelot Switch driver 3 * 4 * This contains glue logic between the switchdev driver operations and the 5 * mscc_ocelot_switch_lib. 6 * 7 * Copyright (c) 2017, 2019 Microsemi Corporation 8 * Copyright 2020-2021 NXP 9 */ 10 11#include <linux/dsa/ocelot.h> 12#include <linux/if_bridge.h> 13#include <linux/of_net.h> 14#include <linux/phy/phy.h> 15#include <net/pkt_cls.h> 16#include "ocelot.h" 17#include "ocelot_police.h" 18#include "ocelot_vcap.h" 19#include "ocelot_fdma.h" 20 21#define OCELOT_MAC_QUIRKS OCELOT_QUIRK_QSGMII_PORTS_MUST_BE_UP 22 23static bool ocelot_netdevice_dev_check(const struct net_device *dev); 24 25static struct ocelot *devlink_port_to_ocelot(struct devlink_port *dlp) 26{ 27 return devlink_priv(dlp->devlink); 28} 29 30static int devlink_port_to_port(struct devlink_port *dlp) 31{ 32 struct ocelot *ocelot = devlink_port_to_ocelot(dlp); 33 34 return dlp - ocelot->devlink_ports; 35} 36 37static int ocelot_devlink_sb_pool_get(struct devlink *dl, 38 unsigned int sb_index, u16 pool_index, 39 struct devlink_sb_pool_info *pool_info) 40{ 41 struct ocelot *ocelot = devlink_priv(dl); 42 43 return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info); 44} 45 46static int ocelot_devlink_sb_pool_set(struct devlink *dl, unsigned int sb_index, 47 u16 pool_index, u32 size, 48 enum devlink_sb_threshold_type threshold_type, 49 struct netlink_ext_ack *extack) 50{ 51 struct ocelot *ocelot = devlink_priv(dl); 52 53 return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size, 54 threshold_type, extack); 55} 56 57static int ocelot_devlink_sb_port_pool_get(struct devlink_port *dlp, 58 unsigned int sb_index, u16 pool_index, 59 u32 *p_threshold) 60{ 61 struct ocelot *ocelot = devlink_port_to_ocelot(dlp); 62 int port = devlink_port_to_port(dlp); 63 64 return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index, 65 p_threshold); 66} 67 68static int ocelot_devlink_sb_port_pool_set(struct devlink_port *dlp, 69 unsigned int sb_index, u16 pool_index, 70 u32 threshold, 71 struct netlink_ext_ack *extack) 72{ 73 struct ocelot *ocelot = devlink_port_to_ocelot(dlp); 74 int port = devlink_port_to_port(dlp); 75 76 return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index, 77 threshold, extack); 78} 79 80static int 81ocelot_devlink_sb_tc_pool_bind_get(struct devlink_port *dlp, 82 unsigned int sb_index, u16 tc_index, 83 enum devlink_sb_pool_type pool_type, 84 u16 *p_pool_index, u32 *p_threshold) 85{ 86 struct ocelot *ocelot = devlink_port_to_ocelot(dlp); 87 int port = devlink_port_to_port(dlp); 88 89 return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index, 90 pool_type, p_pool_index, 91 p_threshold); 92} 93 94static int 95ocelot_devlink_sb_tc_pool_bind_set(struct devlink_port *dlp, 96 unsigned int sb_index, u16 tc_index, 97 enum devlink_sb_pool_type pool_type, 98 u16 pool_index, u32 threshold, 99 struct netlink_ext_ack *extack) 100{ 101 struct ocelot *ocelot = devlink_port_to_ocelot(dlp); 102 int port = devlink_port_to_port(dlp); 103 104 return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index, 105 pool_type, pool_index, threshold, 106 extack); 107} 108 109static int ocelot_devlink_sb_occ_snapshot(struct devlink *dl, 110 unsigned int sb_index) 111{ 112 struct ocelot *ocelot = devlink_priv(dl); 113 114 return ocelot_sb_occ_snapshot(ocelot, sb_index); 115} 116 117static int ocelot_devlink_sb_occ_max_clear(struct devlink *dl, 118 unsigned int sb_index) 119{ 120 struct ocelot *ocelot = devlink_priv(dl); 121 122 return ocelot_sb_occ_max_clear(ocelot, sb_index); 123} 124 125static int ocelot_devlink_sb_occ_port_pool_get(struct devlink_port *dlp, 126 unsigned int sb_index, 127 u16 pool_index, u32 *p_cur, 128 u32 *p_max) 129{ 130 struct ocelot *ocelot = devlink_port_to_ocelot(dlp); 131 int port = devlink_port_to_port(dlp); 132 133 return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index, 134 p_cur, p_max); 135} 136 137static int 138ocelot_devlink_sb_occ_tc_port_bind_get(struct devlink_port *dlp, 139 unsigned int sb_index, u16 tc_index, 140 enum devlink_sb_pool_type pool_type, 141 u32 *p_cur, u32 *p_max) 142{ 143 struct ocelot *ocelot = devlink_port_to_ocelot(dlp); 144 int port = devlink_port_to_port(dlp); 145 146 return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index, 147 tc_index, pool_type, 148 p_cur, p_max); 149} 150 151const struct devlink_ops ocelot_devlink_ops = { 152 .sb_pool_get = ocelot_devlink_sb_pool_get, 153 .sb_pool_set = ocelot_devlink_sb_pool_set, 154 .sb_port_pool_get = ocelot_devlink_sb_port_pool_get, 155 .sb_port_pool_set = ocelot_devlink_sb_port_pool_set, 156 .sb_tc_pool_bind_get = ocelot_devlink_sb_tc_pool_bind_get, 157 .sb_tc_pool_bind_set = ocelot_devlink_sb_tc_pool_bind_set, 158 .sb_occ_snapshot = ocelot_devlink_sb_occ_snapshot, 159 .sb_occ_max_clear = ocelot_devlink_sb_occ_max_clear, 160 .sb_occ_port_pool_get = ocelot_devlink_sb_occ_port_pool_get, 161 .sb_occ_tc_port_bind_get = ocelot_devlink_sb_occ_tc_port_bind_get, 162}; 163 164int ocelot_port_devlink_init(struct ocelot *ocelot, int port, 165 enum devlink_port_flavour flavour) 166{ 167 struct devlink_port *dlp = &ocelot->devlink_ports[port]; 168 int id_len = sizeof(ocelot->base_mac); 169 struct devlink *dl = ocelot->devlink; 170 struct devlink_port_attrs attrs = {}; 171 172 memset(dlp, 0, sizeof(*dlp)); 173 memcpy(attrs.switch_id.id, &ocelot->base_mac, id_len); 174 attrs.switch_id.id_len = id_len; 175 attrs.phys.port_number = port; 176 attrs.flavour = flavour; 177 178 devlink_port_attrs_set(dlp, &attrs); 179 180 return devlink_port_register(dl, dlp, port); 181} 182 183void ocelot_port_devlink_teardown(struct ocelot *ocelot, int port) 184{ 185 struct devlink_port *dlp = &ocelot->devlink_ports[port]; 186 187 devlink_port_unregister(dlp); 188} 189 190static struct devlink_port *ocelot_get_devlink_port(struct net_device *dev) 191{ 192 struct ocelot_port_private *priv = netdev_priv(dev); 193 struct ocelot *ocelot = priv->port.ocelot; 194 int port = priv->port.index; 195 196 return &ocelot->devlink_ports[port]; 197} 198 199int ocelot_setup_tc_cls_flower(struct ocelot_port_private *priv, 200 struct flow_cls_offload *f, 201 bool ingress) 202{ 203 struct ocelot *ocelot = priv->port.ocelot; 204 int port = priv->port.index; 205 206 if (!ingress) 207 return -EOPNOTSUPP; 208 209 switch (f->command) { 210 case FLOW_CLS_REPLACE: 211 return ocelot_cls_flower_replace(ocelot, port, f, ingress); 212 case FLOW_CLS_DESTROY: 213 return ocelot_cls_flower_destroy(ocelot, port, f, ingress); 214 case FLOW_CLS_STATS: 215 return ocelot_cls_flower_stats(ocelot, port, f, ingress); 216 default: 217 return -EOPNOTSUPP; 218 } 219} 220 221static int ocelot_setup_tc_cls_matchall_police(struct ocelot_port_private *priv, 222 struct tc_cls_matchall_offload *f, 223 bool ingress, 224 struct netlink_ext_ack *extack) 225{ 226 struct flow_action_entry *action = &f->rule->action.entries[0]; 227 struct ocelot *ocelot = priv->port.ocelot; 228 struct ocelot_policer pol = { 0 }; 229 int port = priv->port.index; 230 int err; 231 232 if (!ingress) { 233 NL_SET_ERR_MSG_MOD(extack, "Only ingress is supported"); 234 return -EOPNOTSUPP; 235 } 236 237 if (priv->tc.police_id && priv->tc.police_id != f->cookie) { 238 NL_SET_ERR_MSG_MOD(extack, 239 "Only one policer per port is supported"); 240 return -EEXIST; 241 } 242 243 err = ocelot_policer_validate(&f->rule->action, action, extack); 244 if (err) 245 return err; 246 247 pol.rate = (u32)div_u64(action->police.rate_bytes_ps, 1000) * 8; 248 pol.burst = action->police.burst; 249 250 err = ocelot_port_policer_add(ocelot, port, &pol); 251 if (err) { 252 NL_SET_ERR_MSG_MOD(extack, "Could not add policer"); 253 return err; 254 } 255 256 priv->tc.police_id = f->cookie; 257 priv->tc.offload_cnt++; 258 259 return 0; 260} 261 262static int ocelot_setup_tc_cls_matchall_mirred(struct ocelot_port_private *priv, 263 struct tc_cls_matchall_offload *f, 264 bool ingress, 265 struct netlink_ext_ack *extack) 266{ 267 struct flow_action *action = &f->rule->action; 268 struct ocelot *ocelot = priv->port.ocelot; 269 struct ocelot_port_private *other_priv; 270 const struct flow_action_entry *a; 271 int err; 272 273 if (f->common.protocol != htons(ETH_P_ALL)) 274 return -EOPNOTSUPP; 275 276 if (!flow_action_basic_hw_stats_check(action, extack)) 277 return -EOPNOTSUPP; 278 279 a = &action->entries[0]; 280 if (!a->dev) 281 return -EINVAL; 282 283 if (!ocelot_netdevice_dev_check(a->dev)) { 284 NL_SET_ERR_MSG_MOD(extack, 285 "Destination not an ocelot port"); 286 return -EOPNOTSUPP; 287 } 288 289 other_priv = netdev_priv(a->dev); 290 291 err = ocelot_port_mirror_add(ocelot, priv->port.index, 292 other_priv->port.index, ingress, extack); 293 if (err) 294 return err; 295 296 if (ingress) 297 priv->tc.ingress_mirred_id = f->cookie; 298 else 299 priv->tc.egress_mirred_id = f->cookie; 300 priv->tc.offload_cnt++; 301 302 return 0; 303} 304 305static int ocelot_del_tc_cls_matchall_police(struct ocelot_port_private *priv, 306 struct netlink_ext_ack *extack) 307{ 308 struct ocelot *ocelot = priv->port.ocelot; 309 int port = priv->port.index; 310 int err; 311 312 err = ocelot_port_policer_del(ocelot, port); 313 if (err) { 314 NL_SET_ERR_MSG_MOD(extack, 315 "Could not delete policer"); 316 return err; 317 } 318 319 priv->tc.police_id = 0; 320 priv->tc.offload_cnt--; 321 322 return 0; 323} 324 325static int ocelot_del_tc_cls_matchall_mirred(struct ocelot_port_private *priv, 326 bool ingress, 327 struct netlink_ext_ack *extack) 328{ 329 struct ocelot *ocelot = priv->port.ocelot; 330 int port = priv->port.index; 331 332 ocelot_port_mirror_del(ocelot, port, ingress); 333 334 if (ingress) 335 priv->tc.ingress_mirred_id = 0; 336 else 337 priv->tc.egress_mirred_id = 0; 338 priv->tc.offload_cnt--; 339 340 return 0; 341} 342 343static int ocelot_setup_tc_cls_matchall(struct ocelot_port_private *priv, 344 struct tc_cls_matchall_offload *f, 345 bool ingress) 346{ 347 struct netlink_ext_ack *extack = f->common.extack; 348 struct flow_action_entry *action; 349 350 switch (f->command) { 351 case TC_CLSMATCHALL_REPLACE: 352 if (!flow_offload_has_one_action(&f->rule->action)) { 353 NL_SET_ERR_MSG_MOD(extack, 354 "Only one action is supported"); 355 return -EOPNOTSUPP; 356 } 357 358 if (priv->tc.block_shared) { 359 NL_SET_ERR_MSG_MOD(extack, 360 "Matchall offloads not supported on shared blocks"); 361 return -EOPNOTSUPP; 362 } 363 364 action = &f->rule->action.entries[0]; 365 366 switch (action->id) { 367 case FLOW_ACTION_POLICE: 368 return ocelot_setup_tc_cls_matchall_police(priv, f, 369 ingress, 370 extack); 371 break; 372 case FLOW_ACTION_MIRRED: 373 return ocelot_setup_tc_cls_matchall_mirred(priv, f, 374 ingress, 375 extack); 376 default: 377 NL_SET_ERR_MSG_MOD(extack, "Unsupported action"); 378 return -EOPNOTSUPP; 379 } 380 381 break; 382 case TC_CLSMATCHALL_DESTROY: 383 action = &f->rule->action.entries[0]; 384 385 if (f->cookie == priv->tc.police_id) 386 return ocelot_del_tc_cls_matchall_police(priv, extack); 387 else if (f->cookie == priv->tc.ingress_mirred_id || 388 f->cookie == priv->tc.egress_mirred_id) 389 return ocelot_del_tc_cls_matchall_mirred(priv, ingress, 390 extack); 391 else 392 return -ENOENT; 393 394 break; 395 case TC_CLSMATCHALL_STATS: 396 default: 397 return -EOPNOTSUPP; 398 } 399} 400 401static int ocelot_setup_tc_block_cb(enum tc_setup_type type, 402 void *type_data, 403 void *cb_priv, bool ingress) 404{ 405 struct ocelot_port_private *priv = cb_priv; 406 407 if (!tc_cls_can_offload_and_chain0(priv->dev, type_data)) 408 return -EOPNOTSUPP; 409 410 switch (type) { 411 case TC_SETUP_CLSMATCHALL: 412 return ocelot_setup_tc_cls_matchall(priv, type_data, ingress); 413 case TC_SETUP_CLSFLOWER: 414 return ocelot_setup_tc_cls_flower(priv, type_data, ingress); 415 default: 416 return -EOPNOTSUPP; 417 } 418} 419 420static int ocelot_setup_tc_block_cb_ig(enum tc_setup_type type, 421 void *type_data, 422 void *cb_priv) 423{ 424 return ocelot_setup_tc_block_cb(type, type_data, 425 cb_priv, true); 426} 427 428static int ocelot_setup_tc_block_cb_eg(enum tc_setup_type type, 429 void *type_data, 430 void *cb_priv) 431{ 432 return ocelot_setup_tc_block_cb(type, type_data, 433 cb_priv, false); 434} 435 436static LIST_HEAD(ocelot_block_cb_list); 437 438static int ocelot_setup_tc_block(struct ocelot_port_private *priv, 439 struct flow_block_offload *f) 440{ 441 struct flow_block_cb *block_cb; 442 flow_setup_cb_t *cb; 443 444 if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS) { 445 cb = ocelot_setup_tc_block_cb_ig; 446 priv->tc.block_shared = f->block_shared; 447 } else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS) { 448 cb = ocelot_setup_tc_block_cb_eg; 449 } else { 450 return -EOPNOTSUPP; 451 } 452 453 f->driver_block_list = &ocelot_block_cb_list; 454 455 switch (f->command) { 456 case FLOW_BLOCK_BIND: 457 if (flow_block_cb_is_busy(cb, priv, &ocelot_block_cb_list)) 458 return -EBUSY; 459 460 block_cb = flow_block_cb_alloc(cb, priv, priv, NULL); 461 if (IS_ERR(block_cb)) 462 return PTR_ERR(block_cb); 463 464 flow_block_cb_add(block_cb, f); 465 list_add_tail(&block_cb->driver_list, f->driver_block_list); 466 return 0; 467 case FLOW_BLOCK_UNBIND: 468 block_cb = flow_block_cb_lookup(f->block, cb, priv); 469 if (!block_cb) 470 return -ENOENT; 471 472 flow_block_cb_remove(block_cb, f); 473 list_del(&block_cb->driver_list); 474 return 0; 475 default: 476 return -EOPNOTSUPP; 477 } 478} 479 480static int ocelot_setup_tc(struct net_device *dev, enum tc_setup_type type, 481 void *type_data) 482{ 483 struct ocelot_port_private *priv = netdev_priv(dev); 484 485 switch (type) { 486 case TC_SETUP_BLOCK: 487 return ocelot_setup_tc_block(priv, type_data); 488 default: 489 return -EOPNOTSUPP; 490 } 491 return 0; 492} 493 494static int ocelot_vlan_vid_add(struct net_device *dev, u16 vid, bool pvid, 495 bool untagged) 496{ 497 struct ocelot_port_private *priv = netdev_priv(dev); 498 struct ocelot_port *ocelot_port = &priv->port; 499 struct ocelot *ocelot = ocelot_port->ocelot; 500 int port = priv->port.index; 501 int ret; 502 503 ret = ocelot_vlan_add(ocelot, port, vid, pvid, untagged); 504 if (ret) 505 return ret; 506 507 /* Add the port MAC address to with the right VLAN information */ 508 ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, vid, 509 ENTRYTYPE_LOCKED); 510 511 return 0; 512} 513 514static int ocelot_vlan_vid_del(struct net_device *dev, u16 vid) 515{ 516 struct ocelot_port_private *priv = netdev_priv(dev); 517 struct ocelot *ocelot = priv->port.ocelot; 518 int port = priv->port.index; 519 int ret; 520 521 /* 8021q removes VID 0 on module unload for all interfaces 522 * with VLAN filtering feature. We need to keep it to receive 523 * untagged traffic. 524 */ 525 if (vid == OCELOT_STANDALONE_PVID) 526 return 0; 527 528 ret = ocelot_vlan_del(ocelot, port, vid); 529 if (ret) 530 return ret; 531 532 /* Del the port MAC address to with the right VLAN information */ 533 ocelot_mact_forget(ocelot, dev->dev_addr, vid); 534 535 return 0; 536} 537 538static int ocelot_port_open(struct net_device *dev) 539{ 540 struct ocelot_port_private *priv = netdev_priv(dev); 541 542 phylink_start(priv->phylink); 543 544 return 0; 545} 546 547static int ocelot_port_stop(struct net_device *dev) 548{ 549 struct ocelot_port_private *priv = netdev_priv(dev); 550 551 phylink_stop(priv->phylink); 552 553 return 0; 554} 555 556static netdev_tx_t ocelot_port_xmit(struct sk_buff *skb, struct net_device *dev) 557{ 558 struct ocelot_port_private *priv = netdev_priv(dev); 559 struct ocelot_port *ocelot_port = &priv->port; 560 struct ocelot *ocelot = ocelot_port->ocelot; 561 int port = priv->port.index; 562 u32 rew_op = 0; 563 564 if (!static_branch_unlikely(&ocelot_fdma_enabled) && 565 !ocelot_can_inject(ocelot, 0)) 566 return NETDEV_TX_BUSY; 567 568 /* Check if timestamping is needed */ 569 if (ocelot->ptp && (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) { 570 struct sk_buff *clone = NULL; 571 572 if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) { 573 kfree_skb(skb); 574 return NETDEV_TX_OK; 575 } 576 577 if (clone) 578 OCELOT_SKB_CB(skb)->clone = clone; 579 580 rew_op = ocelot_ptp_rew_op(skb); 581 } 582 583 if (static_branch_unlikely(&ocelot_fdma_enabled)) { 584 ocelot_fdma_inject_frame(ocelot, port, rew_op, skb, dev); 585 } else { 586 ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb); 587 588 consume_skb(skb); 589 } 590 591 return NETDEV_TX_OK; 592} 593 594enum ocelot_action_type { 595 OCELOT_MACT_LEARN, 596 OCELOT_MACT_FORGET, 597}; 598 599struct ocelot_mact_work_ctx { 600 struct work_struct work; 601 struct ocelot *ocelot; 602 enum ocelot_action_type type; 603 union { 604 /* OCELOT_MACT_LEARN */ 605 struct { 606 unsigned char addr[ETH_ALEN]; 607 u16 vid; 608 enum macaccess_entry_type entry_type; 609 int pgid; 610 } learn; 611 /* OCELOT_MACT_FORGET */ 612 struct { 613 unsigned char addr[ETH_ALEN]; 614 u16 vid; 615 } forget; 616 }; 617}; 618 619#define ocelot_work_to_ctx(x) \ 620 container_of((x), struct ocelot_mact_work_ctx, work) 621 622static void ocelot_mact_work(struct work_struct *work) 623{ 624 struct ocelot_mact_work_ctx *w = ocelot_work_to_ctx(work); 625 struct ocelot *ocelot = w->ocelot; 626 627 switch (w->type) { 628 case OCELOT_MACT_LEARN: 629 ocelot_mact_learn(ocelot, w->learn.pgid, w->learn.addr, 630 w->learn.vid, w->learn.entry_type); 631 break; 632 case OCELOT_MACT_FORGET: 633 ocelot_mact_forget(ocelot, w->forget.addr, w->forget.vid); 634 break; 635 default: 636 break; 637 } 638 639 kfree(w); 640} 641 642static int ocelot_enqueue_mact_action(struct ocelot *ocelot, 643 const struct ocelot_mact_work_ctx *ctx) 644{ 645 struct ocelot_mact_work_ctx *w = kmemdup(ctx, sizeof(*w), GFP_ATOMIC); 646 647 if (!w) 648 return -ENOMEM; 649 650 w->ocelot = ocelot; 651 INIT_WORK(&w->work, ocelot_mact_work); 652 queue_work(ocelot->owq, &w->work); 653 654 return 0; 655} 656 657static int ocelot_mc_unsync(struct net_device *dev, const unsigned char *addr) 658{ 659 struct ocelot_port_private *priv = netdev_priv(dev); 660 struct ocelot_port *ocelot_port = &priv->port; 661 struct ocelot *ocelot = ocelot_port->ocelot; 662 struct ocelot_mact_work_ctx w; 663 664 ether_addr_copy(w.forget.addr, addr); 665 w.forget.vid = OCELOT_STANDALONE_PVID; 666 w.type = OCELOT_MACT_FORGET; 667 668 return ocelot_enqueue_mact_action(ocelot, &w); 669} 670 671static int ocelot_mc_sync(struct net_device *dev, const unsigned char *addr) 672{ 673 struct ocelot_port_private *priv = netdev_priv(dev); 674 struct ocelot_port *ocelot_port = &priv->port; 675 struct ocelot *ocelot = ocelot_port->ocelot; 676 struct ocelot_mact_work_ctx w; 677 678 ether_addr_copy(w.learn.addr, addr); 679 w.learn.vid = OCELOT_STANDALONE_PVID; 680 w.learn.pgid = PGID_CPU; 681 w.learn.entry_type = ENTRYTYPE_LOCKED; 682 w.type = OCELOT_MACT_LEARN; 683 684 return ocelot_enqueue_mact_action(ocelot, &w); 685} 686 687static void ocelot_set_rx_mode(struct net_device *dev) 688{ 689 struct ocelot_port_private *priv = netdev_priv(dev); 690 struct ocelot *ocelot = priv->port.ocelot; 691 u32 val; 692 int i; 693 694 /* This doesn't handle promiscuous mode because the bridge core is 695 * setting IFF_PROMISC on all slave interfaces and all frames would be 696 * forwarded to the CPU port. 697 */ 698 val = GENMASK(ocelot->num_phys_ports - 1, 0); 699 for_each_nonreserved_multicast_dest_pgid(ocelot, i) 700 ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i); 701 702 __dev_mc_sync(dev, ocelot_mc_sync, ocelot_mc_unsync); 703} 704 705static int ocelot_port_set_mac_address(struct net_device *dev, void *p) 706{ 707 struct ocelot_port_private *priv = netdev_priv(dev); 708 struct ocelot_port *ocelot_port = &priv->port; 709 struct ocelot *ocelot = ocelot_port->ocelot; 710 const struct sockaddr *addr = p; 711 712 /* Learn the new net device MAC address in the mac table. */ 713 ocelot_mact_learn(ocelot, PGID_CPU, addr->sa_data, 714 OCELOT_STANDALONE_PVID, ENTRYTYPE_LOCKED); 715 /* Then forget the previous one. */ 716 ocelot_mact_forget(ocelot, dev->dev_addr, OCELOT_STANDALONE_PVID); 717 718 eth_hw_addr_set(dev, addr->sa_data); 719 return 0; 720} 721 722static void ocelot_get_stats64(struct net_device *dev, 723 struct rtnl_link_stats64 *stats) 724{ 725 struct ocelot_port_private *priv = netdev_priv(dev); 726 struct ocelot *ocelot = priv->port.ocelot; 727 int port = priv->port.index; 728 729 /* Configure the port to read the stats from */ 730 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port), 731 SYS_STAT_CFG); 732 733 /* Get Rx stats */ 734 stats->rx_bytes = ocelot_read(ocelot, SYS_COUNT_RX_OCTETS); 735 stats->rx_packets = ocelot_read(ocelot, SYS_COUNT_RX_SHORTS) + 736 ocelot_read(ocelot, SYS_COUNT_RX_FRAGMENTS) + 737 ocelot_read(ocelot, SYS_COUNT_RX_JABBERS) + 738 ocelot_read(ocelot, SYS_COUNT_RX_LONGS) + 739 ocelot_read(ocelot, SYS_COUNT_RX_64) + 740 ocelot_read(ocelot, SYS_COUNT_RX_65_127) + 741 ocelot_read(ocelot, SYS_COUNT_RX_128_255) + 742 ocelot_read(ocelot, SYS_COUNT_RX_256_1023) + 743 ocelot_read(ocelot, SYS_COUNT_RX_1024_1526) + 744 ocelot_read(ocelot, SYS_COUNT_RX_1527_MAX); 745 stats->multicast = ocelot_read(ocelot, SYS_COUNT_RX_MULTICAST); 746 stats->rx_dropped = dev->stats.rx_dropped; 747 748 /* Get Tx stats */ 749 stats->tx_bytes = ocelot_read(ocelot, SYS_COUNT_TX_OCTETS); 750 stats->tx_packets = ocelot_read(ocelot, SYS_COUNT_TX_64) + 751 ocelot_read(ocelot, SYS_COUNT_TX_65_127) + 752 ocelot_read(ocelot, SYS_COUNT_TX_128_511) + 753 ocelot_read(ocelot, SYS_COUNT_TX_512_1023) + 754 ocelot_read(ocelot, SYS_COUNT_TX_1024_1526) + 755 ocelot_read(ocelot, SYS_COUNT_TX_1527_MAX); 756 stats->tx_dropped = ocelot_read(ocelot, SYS_COUNT_TX_DROPS) + 757 ocelot_read(ocelot, SYS_COUNT_TX_AGING); 758 stats->collisions = ocelot_read(ocelot, SYS_COUNT_TX_COLLISION); 759} 760 761static int ocelot_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], 762 struct net_device *dev, 763 const unsigned char *addr, 764 u16 vid, u16 flags, 765 struct netlink_ext_ack *extack) 766{ 767 struct ocelot_port_private *priv = netdev_priv(dev); 768 struct ocelot_port *ocelot_port = &priv->port; 769 struct ocelot *ocelot = ocelot_port->ocelot; 770 int port = priv->port.index; 771 772 return ocelot_fdb_add(ocelot, port, addr, vid, ocelot_port->bridge); 773} 774 775static int ocelot_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[], 776 struct net_device *dev, 777 const unsigned char *addr, u16 vid, 778 struct netlink_ext_ack *extack) 779{ 780 struct ocelot_port_private *priv = netdev_priv(dev); 781 struct ocelot_port *ocelot_port = &priv->port; 782 struct ocelot *ocelot = ocelot_port->ocelot; 783 int port = priv->port.index; 784 785 return ocelot_fdb_del(ocelot, port, addr, vid, ocelot_port->bridge); 786} 787 788static int ocelot_port_fdb_dump(struct sk_buff *skb, 789 struct netlink_callback *cb, 790 struct net_device *dev, 791 struct net_device *filter_dev, int *idx) 792{ 793 struct ocelot_port_private *priv = netdev_priv(dev); 794 struct ocelot *ocelot = priv->port.ocelot; 795 struct ocelot_dump_ctx dump = { 796 .dev = dev, 797 .skb = skb, 798 .cb = cb, 799 .idx = *idx, 800 }; 801 int port = priv->port.index; 802 int ret; 803 804 ret = ocelot_fdb_dump(ocelot, port, ocelot_port_fdb_do_dump, &dump); 805 806 *idx = dump.idx; 807 808 return ret; 809} 810 811static int ocelot_vlan_rx_add_vid(struct net_device *dev, __be16 proto, 812 u16 vid) 813{ 814 return ocelot_vlan_vid_add(dev, vid, false, false); 815} 816 817static int ocelot_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, 818 u16 vid) 819{ 820 return ocelot_vlan_vid_del(dev, vid); 821} 822 823static void ocelot_vlan_mode(struct ocelot *ocelot, int port, 824 netdev_features_t features) 825{ 826 u32 val; 827 828 /* Filtering */ 829 val = ocelot_read(ocelot, ANA_VLANMASK); 830 if (features & NETIF_F_HW_VLAN_CTAG_FILTER) 831 val |= BIT(port); 832 else 833 val &= ~BIT(port); 834 ocelot_write(ocelot, val, ANA_VLANMASK); 835} 836 837static int ocelot_set_features(struct net_device *dev, 838 netdev_features_t features) 839{ 840 netdev_features_t changed = dev->features ^ features; 841 struct ocelot_port_private *priv = netdev_priv(dev); 842 struct ocelot *ocelot = priv->port.ocelot; 843 int port = priv->port.index; 844 845 if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC) && 846 priv->tc.offload_cnt) { 847 netdev_err(dev, 848 "Cannot disable HW TC offload while offloads active\n"); 849 return -EBUSY; 850 } 851 852 if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) 853 ocelot_vlan_mode(ocelot, port, features); 854 855 return 0; 856} 857 858static int ocelot_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 859{ 860 struct ocelot_port_private *priv = netdev_priv(dev); 861 struct ocelot *ocelot = priv->port.ocelot; 862 int port = priv->port.index; 863 864 /* If the attached PHY device isn't capable of timestamping operations, 865 * use our own (when possible). 866 */ 867 if (!phy_has_hwtstamp(dev->phydev) && ocelot->ptp) { 868 switch (cmd) { 869 case SIOCSHWTSTAMP: 870 return ocelot_hwstamp_set(ocelot, port, ifr); 871 case SIOCGHWTSTAMP: 872 return ocelot_hwstamp_get(ocelot, port, ifr); 873 } 874 } 875 876 return phy_mii_ioctl(dev->phydev, ifr, cmd); 877} 878 879static int ocelot_change_mtu(struct net_device *dev, int new_mtu) 880{ 881 struct ocelot_port_private *priv = netdev_priv(dev); 882 struct ocelot_port *ocelot_port = &priv->port; 883 struct ocelot *ocelot = ocelot_port->ocelot; 884 885 ocelot_port_set_maxlen(ocelot, priv->port.index, new_mtu); 886 WRITE_ONCE(dev->mtu, new_mtu); 887 888 return 0; 889} 890 891static const struct net_device_ops ocelot_port_netdev_ops = { 892 .ndo_open = ocelot_port_open, 893 .ndo_stop = ocelot_port_stop, 894 .ndo_start_xmit = ocelot_port_xmit, 895 .ndo_change_mtu = ocelot_change_mtu, 896 .ndo_set_rx_mode = ocelot_set_rx_mode, 897 .ndo_set_mac_address = ocelot_port_set_mac_address, 898 .ndo_get_stats64 = ocelot_get_stats64, 899 .ndo_fdb_add = ocelot_port_fdb_add, 900 .ndo_fdb_del = ocelot_port_fdb_del, 901 .ndo_fdb_dump = ocelot_port_fdb_dump, 902 .ndo_vlan_rx_add_vid = ocelot_vlan_rx_add_vid, 903 .ndo_vlan_rx_kill_vid = ocelot_vlan_rx_kill_vid, 904 .ndo_set_features = ocelot_set_features, 905 .ndo_setup_tc = ocelot_setup_tc, 906 .ndo_eth_ioctl = ocelot_ioctl, 907 .ndo_get_devlink_port = ocelot_get_devlink_port, 908}; 909 910struct net_device *ocelot_port_to_netdev(struct ocelot *ocelot, int port) 911{ 912 struct ocelot_port *ocelot_port = ocelot->ports[port]; 913 struct ocelot_port_private *priv; 914 915 if (!ocelot_port) 916 return NULL; 917 918 priv = container_of(ocelot_port, struct ocelot_port_private, port); 919 920 return priv->dev; 921} 922 923/* Checks if the net_device instance given to us originates from our driver */ 924static bool ocelot_netdevice_dev_check(const struct net_device *dev) 925{ 926 return dev->netdev_ops == &ocelot_port_netdev_ops; 927} 928 929int ocelot_netdev_to_port(struct net_device *dev) 930{ 931 struct ocelot_port_private *priv; 932 933 if (!dev || !ocelot_netdevice_dev_check(dev)) 934 return -EINVAL; 935 936 priv = netdev_priv(dev); 937 938 return priv->port.index; 939} 940 941static void ocelot_port_get_strings(struct net_device *netdev, u32 sset, 942 u8 *data) 943{ 944 struct ocelot_port_private *priv = netdev_priv(netdev); 945 struct ocelot *ocelot = priv->port.ocelot; 946 int port = priv->port.index; 947 948 ocelot_get_strings(ocelot, port, sset, data); 949} 950 951static void ocelot_port_get_ethtool_stats(struct net_device *dev, 952 struct ethtool_stats *stats, 953 u64 *data) 954{ 955 struct ocelot_port_private *priv = netdev_priv(dev); 956 struct ocelot *ocelot = priv->port.ocelot; 957 int port = priv->port.index; 958 959 ocelot_get_ethtool_stats(ocelot, port, data); 960} 961 962static int ocelot_port_get_sset_count(struct net_device *dev, int sset) 963{ 964 struct ocelot_port_private *priv = netdev_priv(dev); 965 struct ocelot *ocelot = priv->port.ocelot; 966 int port = priv->port.index; 967 968 return ocelot_get_sset_count(ocelot, port, sset); 969} 970 971static int ocelot_port_get_ts_info(struct net_device *dev, 972 struct ethtool_ts_info *info) 973{ 974 struct ocelot_port_private *priv = netdev_priv(dev); 975 struct ocelot *ocelot = priv->port.ocelot; 976 int port = priv->port.index; 977 978 if (!ocelot->ptp) 979 return ethtool_op_get_ts_info(dev, info); 980 981 return ocelot_get_ts_info(ocelot, port, info); 982} 983 984static const struct ethtool_ops ocelot_ethtool_ops = { 985 .get_strings = ocelot_port_get_strings, 986 .get_ethtool_stats = ocelot_port_get_ethtool_stats, 987 .get_sset_count = ocelot_port_get_sset_count, 988 .get_link_ksettings = phy_ethtool_get_link_ksettings, 989 .set_link_ksettings = phy_ethtool_set_link_ksettings, 990 .get_ts_info = ocelot_port_get_ts_info, 991}; 992 993static void ocelot_port_attr_stp_state_set(struct ocelot *ocelot, int port, 994 u8 state) 995{ 996 ocelot_bridge_stp_state_set(ocelot, port, state); 997} 998 999static void ocelot_port_attr_ageing_set(struct ocelot *ocelot, int port, 1000 unsigned long ageing_clock_t) 1001{ 1002 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t); 1003 u32 ageing_time = jiffies_to_msecs(ageing_jiffies); 1004 1005 ocelot_set_ageing_time(ocelot, ageing_time); 1006} 1007 1008static void ocelot_port_attr_mc_set(struct ocelot *ocelot, int port, bool mc) 1009{ 1010 u32 cpu_fwd_mcast = ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA | 1011 ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA | 1012 ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA; 1013 u32 val = 0; 1014 1015 if (mc) 1016 val = cpu_fwd_mcast; 1017 1018 ocelot_rmw_gix(ocelot, val, cpu_fwd_mcast, 1019 ANA_PORT_CPU_FWD_CFG, port); 1020} 1021 1022static int ocelot_port_attr_set(struct net_device *dev, const void *ctx, 1023 const struct switchdev_attr *attr, 1024 struct netlink_ext_ack *extack) 1025{ 1026 struct ocelot_port_private *priv = netdev_priv(dev); 1027 struct ocelot *ocelot = priv->port.ocelot; 1028 int port = priv->port.index; 1029 int err = 0; 1030 1031 if (ctx && ctx != priv) 1032 return 0; 1033 1034 switch (attr->id) { 1035 case SWITCHDEV_ATTR_ID_PORT_STP_STATE: 1036 ocelot_port_attr_stp_state_set(ocelot, port, attr->u.stp_state); 1037 break; 1038 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME: 1039 ocelot_port_attr_ageing_set(ocelot, port, attr->u.ageing_time); 1040 break; 1041 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING: 1042 ocelot_port_vlan_filtering(ocelot, port, attr->u.vlan_filtering, 1043 extack); 1044 break; 1045 case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED: 1046 ocelot_port_attr_mc_set(ocelot, port, !attr->u.mc_disabled); 1047 break; 1048 case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS: 1049 err = ocelot_port_pre_bridge_flags(ocelot, port, 1050 attr->u.brport_flags); 1051 break; 1052 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS: 1053 ocelot_port_bridge_flags(ocelot, port, attr->u.brport_flags); 1054 break; 1055 default: 1056 err = -EOPNOTSUPP; 1057 break; 1058 } 1059 1060 return err; 1061} 1062 1063static int ocelot_vlan_vid_prepare(struct net_device *dev, u16 vid, bool pvid, 1064 bool untagged, struct netlink_ext_ack *extack) 1065{ 1066 struct ocelot_port_private *priv = netdev_priv(dev); 1067 struct ocelot_port *ocelot_port = &priv->port; 1068 struct ocelot *ocelot = ocelot_port->ocelot; 1069 int port = priv->port.index; 1070 1071 return ocelot_vlan_prepare(ocelot, port, vid, pvid, untagged, extack); 1072} 1073 1074static int ocelot_port_obj_add_vlan(struct net_device *dev, 1075 const struct switchdev_obj_port_vlan *vlan, 1076 struct netlink_ext_ack *extack) 1077{ 1078 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED; 1079 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID; 1080 int ret; 1081 1082 ret = ocelot_vlan_vid_prepare(dev, vlan->vid, pvid, untagged, extack); 1083 if (ret) 1084 return ret; 1085 1086 return ocelot_vlan_vid_add(dev, vlan->vid, pvid, untagged); 1087} 1088 1089static int ocelot_port_obj_add_mdb(struct net_device *dev, 1090 const struct switchdev_obj_port_mdb *mdb) 1091{ 1092 struct ocelot_port_private *priv = netdev_priv(dev); 1093 struct ocelot_port *ocelot_port = &priv->port; 1094 struct ocelot *ocelot = ocelot_port->ocelot; 1095 int port = priv->port.index; 1096 1097 return ocelot_port_mdb_add(ocelot, port, mdb, ocelot_port->bridge); 1098} 1099 1100static int ocelot_port_obj_del_mdb(struct net_device *dev, 1101 const struct switchdev_obj_port_mdb *mdb) 1102{ 1103 struct ocelot_port_private *priv = netdev_priv(dev); 1104 struct ocelot_port *ocelot_port = &priv->port; 1105 struct ocelot *ocelot = ocelot_port->ocelot; 1106 int port = priv->port.index; 1107 1108 return ocelot_port_mdb_del(ocelot, port, mdb, ocelot_port->bridge); 1109} 1110 1111static int ocelot_port_obj_mrp_add(struct net_device *dev, 1112 const struct switchdev_obj_mrp *mrp) 1113{ 1114 struct ocelot_port_private *priv = netdev_priv(dev); 1115 struct ocelot_port *ocelot_port = &priv->port; 1116 struct ocelot *ocelot = ocelot_port->ocelot; 1117 int port = priv->port.index; 1118 1119 return ocelot_mrp_add(ocelot, port, mrp); 1120} 1121 1122static int ocelot_port_obj_mrp_del(struct net_device *dev, 1123 const struct switchdev_obj_mrp *mrp) 1124{ 1125 struct ocelot_port_private *priv = netdev_priv(dev); 1126 struct ocelot_port *ocelot_port = &priv->port; 1127 struct ocelot *ocelot = ocelot_port->ocelot; 1128 int port = priv->port.index; 1129 1130 return ocelot_mrp_del(ocelot, port, mrp); 1131} 1132 1133static int 1134ocelot_port_obj_mrp_add_ring_role(struct net_device *dev, 1135 const struct switchdev_obj_ring_role_mrp *mrp) 1136{ 1137 struct ocelot_port_private *priv = netdev_priv(dev); 1138 struct ocelot_port *ocelot_port = &priv->port; 1139 struct ocelot *ocelot = ocelot_port->ocelot; 1140 int port = priv->port.index; 1141 1142 return ocelot_mrp_add_ring_role(ocelot, port, mrp); 1143} 1144 1145static int 1146ocelot_port_obj_mrp_del_ring_role(struct net_device *dev, 1147 const struct switchdev_obj_ring_role_mrp *mrp) 1148{ 1149 struct ocelot_port_private *priv = netdev_priv(dev); 1150 struct ocelot_port *ocelot_port = &priv->port; 1151 struct ocelot *ocelot = ocelot_port->ocelot; 1152 int port = priv->port.index; 1153 1154 return ocelot_mrp_del_ring_role(ocelot, port, mrp); 1155} 1156 1157static int ocelot_port_obj_add(struct net_device *dev, const void *ctx, 1158 const struct switchdev_obj *obj, 1159 struct netlink_ext_ack *extack) 1160{ 1161 struct ocelot_port_private *priv = netdev_priv(dev); 1162 int ret = 0; 1163 1164 if (ctx && ctx != priv) 1165 return 0; 1166 1167 switch (obj->id) { 1168 case SWITCHDEV_OBJ_ID_PORT_VLAN: 1169 ret = ocelot_port_obj_add_vlan(dev, 1170 SWITCHDEV_OBJ_PORT_VLAN(obj), 1171 extack); 1172 break; 1173 case SWITCHDEV_OBJ_ID_PORT_MDB: 1174 ret = ocelot_port_obj_add_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj)); 1175 break; 1176 case SWITCHDEV_OBJ_ID_MRP: 1177 ret = ocelot_port_obj_mrp_add(dev, SWITCHDEV_OBJ_MRP(obj)); 1178 break; 1179 case SWITCHDEV_OBJ_ID_RING_ROLE_MRP: 1180 ret = ocelot_port_obj_mrp_add_ring_role(dev, 1181 SWITCHDEV_OBJ_RING_ROLE_MRP(obj)); 1182 break; 1183 default: 1184 return -EOPNOTSUPP; 1185 } 1186 1187 return ret; 1188} 1189 1190static int ocelot_port_obj_del(struct net_device *dev, const void *ctx, 1191 const struct switchdev_obj *obj) 1192{ 1193 struct ocelot_port_private *priv = netdev_priv(dev); 1194 int ret = 0; 1195 1196 if (ctx && ctx != priv) 1197 return 0; 1198 1199 switch (obj->id) { 1200 case SWITCHDEV_OBJ_ID_PORT_VLAN: 1201 ret = ocelot_vlan_vid_del(dev, 1202 SWITCHDEV_OBJ_PORT_VLAN(obj)->vid); 1203 break; 1204 case SWITCHDEV_OBJ_ID_PORT_MDB: 1205 ret = ocelot_port_obj_del_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj)); 1206 break; 1207 case SWITCHDEV_OBJ_ID_MRP: 1208 ret = ocelot_port_obj_mrp_del(dev, SWITCHDEV_OBJ_MRP(obj)); 1209 break; 1210 case SWITCHDEV_OBJ_ID_RING_ROLE_MRP: 1211 ret = ocelot_port_obj_mrp_del_ring_role(dev, 1212 SWITCHDEV_OBJ_RING_ROLE_MRP(obj)); 1213 break; 1214 default: 1215 return -EOPNOTSUPP; 1216 } 1217 1218 return ret; 1219} 1220 1221static void ocelot_inherit_brport_flags(struct ocelot *ocelot, int port, 1222 struct net_device *brport_dev) 1223{ 1224 struct switchdev_brport_flags flags = {0}; 1225 int flag; 1226 1227 flags.mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD; 1228 1229 for_each_set_bit(flag, &flags.mask, 32) 1230 if (br_port_flag_is_set(brport_dev, BIT(flag))) 1231 flags.val |= BIT(flag); 1232 1233 ocelot_port_bridge_flags(ocelot, port, flags); 1234} 1235 1236static void ocelot_clear_brport_flags(struct ocelot *ocelot, int port) 1237{ 1238 struct switchdev_brport_flags flags; 1239 1240 flags.mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD; 1241 flags.val = flags.mask & ~BR_LEARNING; 1242 1243 ocelot_port_bridge_flags(ocelot, port, flags); 1244} 1245 1246static int ocelot_switchdev_sync(struct ocelot *ocelot, int port, 1247 struct net_device *brport_dev, 1248 struct net_device *bridge_dev, 1249 struct netlink_ext_ack *extack) 1250{ 1251 clock_t ageing_time; 1252 u8 stp_state; 1253 1254 ocelot_inherit_brport_flags(ocelot, port, brport_dev); 1255 1256 stp_state = br_port_get_stp_state(brport_dev); 1257 ocelot_bridge_stp_state_set(ocelot, port, stp_state); 1258 1259 ageing_time = br_get_ageing_time(bridge_dev); 1260 ocelot_port_attr_ageing_set(ocelot, port, ageing_time); 1261 1262 return ocelot_port_vlan_filtering(ocelot, port, 1263 br_vlan_enabled(bridge_dev), 1264 extack); 1265} 1266 1267static int ocelot_switchdev_unsync(struct ocelot *ocelot, int port) 1268{ 1269 int err; 1270 1271 err = ocelot_port_vlan_filtering(ocelot, port, false, NULL); 1272 if (err) 1273 return err; 1274 1275 ocelot_clear_brport_flags(ocelot, port); 1276 1277 ocelot_bridge_stp_state_set(ocelot, port, BR_STATE_FORWARDING); 1278 1279 return 0; 1280} 1281 1282static int ocelot_bridge_num_get(struct ocelot *ocelot, 1283 const struct net_device *bridge_dev) 1284{ 1285 int bridge_num = ocelot_bridge_num_find(ocelot, bridge_dev); 1286 1287 if (bridge_num < 0) { 1288 /* First port that offloads this bridge */ 1289 bridge_num = find_first_zero_bit(&ocelot->bridges, 1290 ocelot->num_phys_ports); 1291 1292 set_bit(bridge_num, &ocelot->bridges); 1293 } 1294 1295 return bridge_num; 1296} 1297 1298static void ocelot_bridge_num_put(struct ocelot *ocelot, 1299 const struct net_device *bridge_dev, 1300 int bridge_num) 1301{ 1302 /* Check if the bridge is still in use, otherwise it is time 1303 * to clean it up so we can reuse this bridge_num later. 1304 */ 1305 if (!ocelot_bridge_num_find(ocelot, bridge_dev)) 1306 clear_bit(bridge_num, &ocelot->bridges); 1307} 1308 1309static int ocelot_netdevice_bridge_join(struct net_device *dev, 1310 struct net_device *brport_dev, 1311 struct net_device *bridge, 1312 struct netlink_ext_ack *extack) 1313{ 1314 struct ocelot_port_private *priv = netdev_priv(dev); 1315 struct ocelot_port *ocelot_port = &priv->port; 1316 struct ocelot *ocelot = ocelot_port->ocelot; 1317 int port = priv->port.index; 1318 int bridge_num, err; 1319 1320 bridge_num = ocelot_bridge_num_get(ocelot, bridge); 1321 1322 err = ocelot_port_bridge_join(ocelot, port, bridge, bridge_num, 1323 extack); 1324 if (err) 1325 goto err_join; 1326 1327 err = switchdev_bridge_port_offload(brport_dev, dev, priv, 1328 &ocelot_switchdev_nb, 1329 &ocelot_switchdev_blocking_nb, 1330 false, extack); 1331 if (err) 1332 goto err_switchdev_offload; 1333 1334 err = ocelot_switchdev_sync(ocelot, port, brport_dev, bridge, extack); 1335 if (err) 1336 goto err_switchdev_sync; 1337 1338 return 0; 1339 1340err_switchdev_sync: 1341 switchdev_bridge_port_unoffload(brport_dev, priv, 1342 &ocelot_switchdev_nb, 1343 &ocelot_switchdev_blocking_nb); 1344err_switchdev_offload: 1345 ocelot_port_bridge_leave(ocelot, port, bridge); 1346err_join: 1347 ocelot_bridge_num_put(ocelot, bridge, bridge_num); 1348 return err; 1349} 1350 1351static void ocelot_netdevice_pre_bridge_leave(struct net_device *dev, 1352 struct net_device *brport_dev) 1353{ 1354 struct ocelot_port_private *priv = netdev_priv(dev); 1355 1356 switchdev_bridge_port_unoffload(brport_dev, priv, 1357 &ocelot_switchdev_nb, 1358 &ocelot_switchdev_blocking_nb); 1359} 1360 1361static int ocelot_netdevice_bridge_leave(struct net_device *dev, 1362 struct net_device *brport_dev, 1363 struct net_device *bridge) 1364{ 1365 struct ocelot_port_private *priv = netdev_priv(dev); 1366 struct ocelot_port *ocelot_port = &priv->port; 1367 struct ocelot *ocelot = ocelot_port->ocelot; 1368 int bridge_num = ocelot_port->bridge_num; 1369 int port = priv->port.index; 1370 int err; 1371 1372 err = ocelot_switchdev_unsync(ocelot, port); 1373 if (err) 1374 return err; 1375 1376 ocelot_port_bridge_leave(ocelot, port, bridge); 1377 ocelot_bridge_num_put(ocelot, bridge, bridge_num); 1378 1379 return 0; 1380} 1381 1382static int ocelot_netdevice_lag_join(struct net_device *dev, 1383 struct net_device *bond, 1384 struct netdev_lag_upper_info *info, 1385 struct netlink_ext_ack *extack) 1386{ 1387 struct ocelot_port_private *priv = netdev_priv(dev); 1388 struct ocelot_port *ocelot_port = &priv->port; 1389 struct ocelot *ocelot = ocelot_port->ocelot; 1390 struct net_device *bridge_dev; 1391 int port = priv->port.index; 1392 int err; 1393 1394 err = ocelot_port_lag_join(ocelot, port, bond, info); 1395 if (err == -EOPNOTSUPP) { 1396 NL_SET_ERR_MSG_MOD(extack, "Offloading not supported"); 1397 return 0; 1398 } 1399 1400 bridge_dev = netdev_master_upper_dev_get(bond); 1401 if (!bridge_dev || !netif_is_bridge_master(bridge_dev)) 1402 return 0; 1403 1404 err = ocelot_netdevice_bridge_join(dev, bond, bridge_dev, extack); 1405 if (err) 1406 goto err_bridge_join; 1407 1408 return 0; 1409 1410err_bridge_join: 1411 ocelot_port_lag_leave(ocelot, port, bond); 1412 return err; 1413} 1414 1415static void ocelot_netdevice_pre_lag_leave(struct net_device *dev, 1416 struct net_device *bond) 1417{ 1418 struct net_device *bridge_dev; 1419 1420 bridge_dev = netdev_master_upper_dev_get(bond); 1421 if (!bridge_dev || !netif_is_bridge_master(bridge_dev)) 1422 return; 1423 1424 ocelot_netdevice_pre_bridge_leave(dev, bond); 1425} 1426 1427static int ocelot_netdevice_lag_leave(struct net_device *dev, 1428 struct net_device *bond) 1429{ 1430 struct ocelot_port_private *priv = netdev_priv(dev); 1431 struct ocelot_port *ocelot_port = &priv->port; 1432 struct ocelot *ocelot = ocelot_port->ocelot; 1433 struct net_device *bridge_dev; 1434 int port = priv->port.index; 1435 1436 ocelot_port_lag_leave(ocelot, port, bond); 1437 1438 bridge_dev = netdev_master_upper_dev_get(bond); 1439 if (!bridge_dev || !netif_is_bridge_master(bridge_dev)) 1440 return 0; 1441 1442 return ocelot_netdevice_bridge_leave(dev, bond, bridge_dev); 1443} 1444 1445static int ocelot_netdevice_changeupper(struct net_device *dev, 1446 struct net_device *brport_dev, 1447 struct netdev_notifier_changeupper_info *info) 1448{ 1449 struct netlink_ext_ack *extack; 1450 int err = 0; 1451 1452 extack = netdev_notifier_info_to_extack(&info->info); 1453 1454 if (netif_is_bridge_master(info->upper_dev)) { 1455 if (info->linking) 1456 err = ocelot_netdevice_bridge_join(dev, brport_dev, 1457 info->upper_dev, 1458 extack); 1459 else 1460 err = ocelot_netdevice_bridge_leave(dev, brport_dev, 1461 info->upper_dev); 1462 } 1463 if (netif_is_lag_master(info->upper_dev)) { 1464 if (info->linking) 1465 err = ocelot_netdevice_lag_join(dev, info->upper_dev, 1466 info->upper_info, extack); 1467 else 1468 ocelot_netdevice_lag_leave(dev, info->upper_dev); 1469 } 1470 1471 return notifier_from_errno(err); 1472} 1473 1474/* Treat CHANGEUPPER events on an offloaded LAG as individual CHANGEUPPER 1475 * events for the lower physical ports of the LAG. 1476 * If the LAG upper isn't offloaded, ignore its CHANGEUPPER events. 1477 * In case the LAG joined a bridge, notify that we are offloading it and can do 1478 * forwarding in hardware towards it. 1479 */ 1480static int 1481ocelot_netdevice_lag_changeupper(struct net_device *dev, 1482 struct netdev_notifier_changeupper_info *info) 1483{ 1484 struct net_device *lower; 1485 struct list_head *iter; 1486 int err = NOTIFY_DONE; 1487 1488 netdev_for_each_lower_dev(dev, lower, iter) { 1489 struct ocelot_port_private *priv = netdev_priv(lower); 1490 struct ocelot_port *ocelot_port = &priv->port; 1491 1492 if (ocelot_port->bond != dev) 1493 return NOTIFY_OK; 1494 1495 err = ocelot_netdevice_changeupper(lower, dev, info); 1496 if (err) 1497 return notifier_from_errno(err); 1498 } 1499 1500 return NOTIFY_DONE; 1501} 1502 1503static int 1504ocelot_netdevice_prechangeupper(struct net_device *dev, 1505 struct net_device *brport_dev, 1506 struct netdev_notifier_changeupper_info *info) 1507{ 1508 if (netif_is_bridge_master(info->upper_dev) && !info->linking) 1509 ocelot_netdevice_pre_bridge_leave(dev, brport_dev); 1510 1511 if (netif_is_lag_master(info->upper_dev) && !info->linking) 1512 ocelot_netdevice_pre_lag_leave(dev, info->upper_dev); 1513 1514 return NOTIFY_DONE; 1515} 1516 1517static int 1518ocelot_netdevice_lag_prechangeupper(struct net_device *dev, 1519 struct netdev_notifier_changeupper_info *info) 1520{ 1521 struct net_device *lower; 1522 struct list_head *iter; 1523 int err = NOTIFY_DONE; 1524 1525 netdev_for_each_lower_dev(dev, lower, iter) { 1526 struct ocelot_port_private *priv = netdev_priv(lower); 1527 struct ocelot_port *ocelot_port = &priv->port; 1528 1529 if (ocelot_port->bond != dev) 1530 return NOTIFY_OK; 1531 1532 err = ocelot_netdevice_prechangeupper(dev, lower, info); 1533 if (err) 1534 return err; 1535 } 1536 1537 return NOTIFY_DONE; 1538} 1539 1540static int 1541ocelot_netdevice_changelowerstate(struct net_device *dev, 1542 struct netdev_lag_lower_state_info *info) 1543{ 1544 struct ocelot_port_private *priv = netdev_priv(dev); 1545 bool is_active = info->link_up && info->tx_enabled; 1546 struct ocelot_port *ocelot_port = &priv->port; 1547 struct ocelot *ocelot = ocelot_port->ocelot; 1548 int port = priv->port.index; 1549 1550 if (!ocelot_port->bond) 1551 return NOTIFY_DONE; 1552 1553 if (ocelot_port->lag_tx_active == is_active) 1554 return NOTIFY_DONE; 1555 1556 ocelot_port_lag_change(ocelot, port, is_active); 1557 1558 return NOTIFY_OK; 1559} 1560 1561static int ocelot_netdevice_event(struct notifier_block *unused, 1562 unsigned long event, void *ptr) 1563{ 1564 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 1565 1566 switch (event) { 1567 case NETDEV_PRECHANGEUPPER: { 1568 struct netdev_notifier_changeupper_info *info = ptr; 1569 1570 if (ocelot_netdevice_dev_check(dev)) 1571 return ocelot_netdevice_prechangeupper(dev, dev, info); 1572 1573 if (netif_is_lag_master(dev)) 1574 return ocelot_netdevice_lag_prechangeupper(dev, info); 1575 1576 break; 1577 } 1578 case NETDEV_CHANGEUPPER: { 1579 struct netdev_notifier_changeupper_info *info = ptr; 1580 1581 if (ocelot_netdevice_dev_check(dev)) 1582 return ocelot_netdevice_changeupper(dev, dev, info); 1583 1584 if (netif_is_lag_master(dev)) 1585 return ocelot_netdevice_lag_changeupper(dev, info); 1586 1587 break; 1588 } 1589 case NETDEV_CHANGELOWERSTATE: { 1590 struct netdev_notifier_changelowerstate_info *info = ptr; 1591 1592 if (!ocelot_netdevice_dev_check(dev)) 1593 break; 1594 1595 return ocelot_netdevice_changelowerstate(dev, 1596 info->lower_state_info); 1597 } 1598 default: 1599 break; 1600 } 1601 1602 return NOTIFY_DONE; 1603} 1604 1605struct notifier_block ocelot_netdevice_nb __read_mostly = { 1606 .notifier_call = ocelot_netdevice_event, 1607}; 1608 1609static int ocelot_switchdev_event(struct notifier_block *unused, 1610 unsigned long event, void *ptr) 1611{ 1612 struct net_device *dev = switchdev_notifier_info_to_dev(ptr); 1613 int err; 1614 1615 switch (event) { 1616 case SWITCHDEV_PORT_ATTR_SET: 1617 err = switchdev_handle_port_attr_set(dev, ptr, 1618 ocelot_netdevice_dev_check, 1619 ocelot_port_attr_set); 1620 return notifier_from_errno(err); 1621 } 1622 1623 return NOTIFY_DONE; 1624} 1625 1626struct notifier_block ocelot_switchdev_nb __read_mostly = { 1627 .notifier_call = ocelot_switchdev_event, 1628}; 1629 1630static int ocelot_switchdev_blocking_event(struct notifier_block *unused, 1631 unsigned long event, void *ptr) 1632{ 1633 struct net_device *dev = switchdev_notifier_info_to_dev(ptr); 1634 int err; 1635 1636 switch (event) { 1637 /* Blocking events. */ 1638 case SWITCHDEV_PORT_OBJ_ADD: 1639 err = switchdev_handle_port_obj_add(dev, ptr, 1640 ocelot_netdevice_dev_check, 1641 ocelot_port_obj_add); 1642 return notifier_from_errno(err); 1643 case SWITCHDEV_PORT_OBJ_DEL: 1644 err = switchdev_handle_port_obj_del(dev, ptr, 1645 ocelot_netdevice_dev_check, 1646 ocelot_port_obj_del); 1647 return notifier_from_errno(err); 1648 case SWITCHDEV_PORT_ATTR_SET: 1649 err = switchdev_handle_port_attr_set(dev, ptr, 1650 ocelot_netdevice_dev_check, 1651 ocelot_port_attr_set); 1652 return notifier_from_errno(err); 1653 } 1654 1655 return NOTIFY_DONE; 1656} 1657 1658struct notifier_block ocelot_switchdev_blocking_nb __read_mostly = { 1659 .notifier_call = ocelot_switchdev_blocking_event, 1660}; 1661 1662static void vsc7514_phylink_mac_config(struct phylink_config *config, 1663 unsigned int link_an_mode, 1664 const struct phylink_link_state *state) 1665{ 1666 struct net_device *ndev = to_net_dev(config->dev); 1667 struct ocelot_port_private *priv = netdev_priv(ndev); 1668 struct ocelot_port *ocelot_port = &priv->port; 1669 1670 /* Disable HDX fast control */ 1671 ocelot_port_writel(ocelot_port, DEV_PORT_MISC_HDX_FAST_DIS, 1672 DEV_PORT_MISC); 1673 1674 /* SGMII only for now */ 1675 ocelot_port_writel(ocelot_port, PCS1G_MODE_CFG_SGMII_MODE_ENA, 1676 PCS1G_MODE_CFG); 1677 ocelot_port_writel(ocelot_port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG); 1678 1679 /* Enable PCS */ 1680 ocelot_port_writel(ocelot_port, PCS1G_CFG_PCS_ENA, PCS1G_CFG); 1681 1682 /* No aneg on SGMII */ 1683 ocelot_port_writel(ocelot_port, 0, PCS1G_ANEG_CFG); 1684 1685 /* No loopback */ 1686 ocelot_port_writel(ocelot_port, 0, PCS1G_LB_CFG); 1687} 1688 1689static void vsc7514_phylink_mac_link_down(struct phylink_config *config, 1690 unsigned int link_an_mode, 1691 phy_interface_t interface) 1692{ 1693 struct net_device *ndev = to_net_dev(config->dev); 1694 struct ocelot_port_private *priv = netdev_priv(ndev); 1695 struct ocelot *ocelot = priv->port.ocelot; 1696 int port = priv->port.index; 1697 1698 ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface, 1699 OCELOT_MAC_QUIRKS); 1700} 1701 1702static void vsc7514_phylink_mac_link_up(struct phylink_config *config, 1703 struct phy_device *phydev, 1704 unsigned int link_an_mode, 1705 phy_interface_t interface, 1706 int speed, int duplex, 1707 bool tx_pause, bool rx_pause) 1708{ 1709 struct net_device *ndev = to_net_dev(config->dev); 1710 struct ocelot_port_private *priv = netdev_priv(ndev); 1711 struct ocelot *ocelot = priv->port.ocelot; 1712 int port = priv->port.index; 1713 1714 ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode, 1715 interface, speed, duplex, 1716 tx_pause, rx_pause, OCELOT_MAC_QUIRKS); 1717} 1718 1719static const struct phylink_mac_ops ocelot_phylink_ops = { 1720 .validate = phylink_generic_validate, 1721 .mac_config = vsc7514_phylink_mac_config, 1722 .mac_link_down = vsc7514_phylink_mac_link_down, 1723 .mac_link_up = vsc7514_phylink_mac_link_up, 1724}; 1725 1726static int ocelot_port_phylink_create(struct ocelot *ocelot, int port, 1727 struct device_node *portnp) 1728{ 1729 struct ocelot_port *ocelot_port = ocelot->ports[port]; 1730 struct ocelot_port_private *priv; 1731 struct device *dev = ocelot->dev; 1732 phy_interface_t phy_mode; 1733 struct phylink *phylink; 1734 int err; 1735 1736 of_get_phy_mode(portnp, &phy_mode); 1737 /* DT bindings of internal PHY ports are broken and don't 1738 * specify a phy-mode 1739 */ 1740 if (phy_mode == PHY_INTERFACE_MODE_NA) 1741 phy_mode = PHY_INTERFACE_MODE_INTERNAL; 1742 1743 if (phy_mode != PHY_INTERFACE_MODE_SGMII && 1744 phy_mode != PHY_INTERFACE_MODE_QSGMII && 1745 phy_mode != PHY_INTERFACE_MODE_INTERNAL) { 1746 dev_err(dev, "unsupported phy mode %s for port %d\n", 1747 phy_modes(phy_mode), port); 1748 return -EINVAL; 1749 } 1750 1751 /* Ensure clock signals and speed are set on all QSGMII links */ 1752 if (phy_mode == PHY_INTERFACE_MODE_QSGMII) 1753 ocelot_port_rmwl(ocelot_port, 0, 1754 DEV_CLOCK_CFG_MAC_TX_RST | 1755 DEV_CLOCK_CFG_MAC_RX_RST, 1756 DEV_CLOCK_CFG); 1757 1758 ocelot_port->phy_mode = phy_mode; 1759 1760 if (phy_mode != PHY_INTERFACE_MODE_INTERNAL) { 1761 struct phy *serdes = of_phy_get(portnp, NULL); 1762 1763 if (IS_ERR(serdes)) { 1764 err = PTR_ERR(serdes); 1765 dev_err_probe(dev, err, 1766 "missing SerDes phys for port %d\n", 1767 port); 1768 return err; 1769 } 1770 1771 err = phy_set_mode_ext(serdes, PHY_MODE_ETHERNET, phy_mode); 1772 of_phy_put(serdes); 1773 if (err) { 1774 dev_err(dev, "Could not SerDes mode on port %d: %pe\n", 1775 port, ERR_PTR(err)); 1776 return err; 1777 } 1778 } 1779 1780 priv = container_of(ocelot_port, struct ocelot_port_private, port); 1781 1782 priv->phylink_config.dev = &priv->dev->dev; 1783 priv->phylink_config.type = PHYLINK_NETDEV; 1784 priv->phylink_config.mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE | 1785 MAC_10 | MAC_100 | MAC_1000FD | MAC_2500FD; 1786 1787 __set_bit(ocelot_port->phy_mode, 1788 priv->phylink_config.supported_interfaces); 1789 1790 phylink = phylink_create(&priv->phylink_config, 1791 of_fwnode_handle(portnp), 1792 phy_mode, &ocelot_phylink_ops); 1793 if (IS_ERR(phylink)) { 1794 err = PTR_ERR(phylink); 1795 dev_err(dev, "Could not create phylink (%pe)\n", phylink); 1796 return err; 1797 } 1798 1799 priv->phylink = phylink; 1800 1801 err = phylink_of_phy_connect(phylink, portnp, 0); 1802 if (err) { 1803 dev_err(dev, "Could not connect to PHY: %pe\n", ERR_PTR(err)); 1804 phylink_destroy(phylink); 1805 priv->phylink = NULL; 1806 return err; 1807 } 1808 1809 return 0; 1810} 1811 1812int ocelot_probe_port(struct ocelot *ocelot, int port, struct regmap *target, 1813 struct device_node *portnp) 1814{ 1815 struct ocelot_port_private *priv; 1816 struct ocelot_port *ocelot_port; 1817 struct net_device *dev; 1818 int err; 1819 1820 dev = alloc_etherdev(sizeof(struct ocelot_port_private)); 1821 if (!dev) 1822 return -ENOMEM; 1823 SET_NETDEV_DEV(dev, ocelot->dev); 1824 priv = netdev_priv(dev); 1825 priv->dev = dev; 1826 ocelot_port = &priv->port; 1827 ocelot_port->ocelot = ocelot; 1828 ocelot_port->index = port; 1829 ocelot_port->target = target; 1830 ocelot->ports[port] = ocelot_port; 1831 1832 dev->netdev_ops = &ocelot_port_netdev_ops; 1833 dev->ethtool_ops = &ocelot_ethtool_ops; 1834 dev->max_mtu = OCELOT_JUMBO_MTU; 1835 1836 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_RXFCS | 1837 NETIF_F_HW_TC; 1838 dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_TC; 1839 1840 err = of_get_ethdev_address(portnp, dev); 1841 if (err) 1842 eth_hw_addr_gen(dev, ocelot->base_mac, port); 1843 1844 ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, 1845 OCELOT_STANDALONE_PVID, ENTRYTYPE_LOCKED); 1846 1847 ocelot_init_port(ocelot, port); 1848 1849 err = ocelot_port_phylink_create(ocelot, port, portnp); 1850 if (err) 1851 goto out; 1852 1853 if (ocelot->fdma) 1854 ocelot_fdma_netdev_init(ocelot, dev); 1855 1856 err = register_netdev(dev); 1857 if (err) { 1858 dev_err(ocelot->dev, "register_netdev failed\n"); 1859 goto out_fdma_deinit; 1860 } 1861 1862 return 0; 1863 1864out_fdma_deinit: 1865 if (ocelot->fdma) 1866 ocelot_fdma_netdev_deinit(ocelot, dev); 1867out: 1868 ocelot->ports[port] = NULL; 1869 free_netdev(dev); 1870 1871 return err; 1872} 1873 1874void ocelot_release_port(struct ocelot_port *ocelot_port) 1875{ 1876 struct ocelot_port_private *priv = container_of(ocelot_port, 1877 struct ocelot_port_private, 1878 port); 1879 struct ocelot *ocelot = ocelot_port->ocelot; 1880 struct ocelot_fdma *fdma = ocelot->fdma; 1881 1882 unregister_netdev(priv->dev); 1883 1884 if (fdma) 1885 ocelot_fdma_netdev_deinit(ocelot, priv->dev); 1886 1887 if (priv->phylink) { 1888 rtnl_lock(); 1889 phylink_disconnect_phy(priv->phylink); 1890 rtnl_unlock(); 1891 1892 phylink_destroy(priv->phylink); 1893 } 1894 1895 free_netdev(priv->dev); 1896}