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