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