1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright 2019 NXP Semiconductors 3 * 4 * This is an umbrella module for all network switches that are 5 * register-compatible with Ocelot and that perform I/O to their host CPU 6 * through an NPI (Node Processor Interface) Ethernet port. 7 */ 8 #include <uapi/linux/if_bridge.h> 9 #include <soc/mscc/ocelot_vcap.h> 10 #include <soc/mscc/ocelot_qsys.h> 11 #include <soc/mscc/ocelot_sys.h> 12 #include <soc/mscc/ocelot_dev.h> 13 #include <soc/mscc/ocelot_ana.h> 14 #include <soc/mscc/ocelot_ptp.h> 15 #include <soc/mscc/ocelot.h> 16 #include <linux/platform_device.h> 17 #include <linux/packing.h> 18 #include <linux/module.h> 19 #include <linux/of_net.h> 20 #include <linux/pci.h> 21 #include <linux/of.h> 22 #include <linux/pcs-lynx.h> 23 #include <net/pkt_sched.h> 24 #include <net/dsa.h> 25 #include "felix.h" 26 27 static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds, 28 int port, 29 enum dsa_tag_protocol mp) 30 { 31 return DSA_TAG_PROTO_OCELOT; 32 } 33 34 static int felix_set_ageing_time(struct dsa_switch *ds, 35 unsigned int ageing_time) 36 { 37 struct ocelot *ocelot = ds->priv; 38 39 ocelot_set_ageing_time(ocelot, ageing_time); 40 41 return 0; 42 } 43 44 static int felix_fdb_dump(struct dsa_switch *ds, int port, 45 dsa_fdb_dump_cb_t *cb, void *data) 46 { 47 struct ocelot *ocelot = ds->priv; 48 49 return ocelot_fdb_dump(ocelot, port, cb, data); 50 } 51 52 static int felix_fdb_add(struct dsa_switch *ds, int port, 53 const unsigned char *addr, u16 vid) 54 { 55 struct ocelot *ocelot = ds->priv; 56 57 return ocelot_fdb_add(ocelot, port, addr, vid); 58 } 59 60 static int felix_fdb_del(struct dsa_switch *ds, int port, 61 const unsigned char *addr, u16 vid) 62 { 63 struct ocelot *ocelot = ds->priv; 64 65 return ocelot_fdb_del(ocelot, port, addr, vid); 66 } 67 68 /* This callback needs to be present */ 69 static int felix_mdb_prepare(struct dsa_switch *ds, int port, 70 const struct switchdev_obj_port_mdb *mdb) 71 { 72 return 0; 73 } 74 75 static void felix_mdb_add(struct dsa_switch *ds, int port, 76 const struct switchdev_obj_port_mdb *mdb) 77 { 78 struct ocelot *ocelot = ds->priv; 79 80 ocelot_port_mdb_add(ocelot, port, mdb); 81 } 82 83 static int felix_mdb_del(struct dsa_switch *ds, int port, 84 const struct switchdev_obj_port_mdb *mdb) 85 { 86 struct ocelot *ocelot = ds->priv; 87 88 return ocelot_port_mdb_del(ocelot, port, mdb); 89 } 90 91 static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port, 92 u8 state) 93 { 94 struct ocelot *ocelot = ds->priv; 95 96 return ocelot_bridge_stp_state_set(ocelot, port, state); 97 } 98 99 static int felix_bridge_join(struct dsa_switch *ds, int port, 100 struct net_device *br) 101 { 102 struct ocelot *ocelot = ds->priv; 103 104 return ocelot_port_bridge_join(ocelot, port, br); 105 } 106 107 static void felix_bridge_leave(struct dsa_switch *ds, int port, 108 struct net_device *br) 109 { 110 struct ocelot *ocelot = ds->priv; 111 112 ocelot_port_bridge_leave(ocelot, port, br); 113 } 114 115 static int felix_vlan_prepare(struct dsa_switch *ds, int port, 116 const struct switchdev_obj_port_vlan *vlan) 117 { 118 struct ocelot *ocelot = ds->priv; 119 u16 vid, flags = vlan->flags; 120 int err; 121 122 /* Ocelot switches copy frames as-is to the CPU, so the flags: 123 * egress-untagged or not, pvid or not, make no difference. This 124 * behavior is already better than what DSA just tries to approximate 125 * when it installs the VLAN with the same flags on the CPU port. 126 * Just accept any configuration, and don't let ocelot deny installing 127 * multiple native VLANs on the NPI port, because the switch doesn't 128 * look at the port tag settings towards the NPI interface anyway. 129 */ 130 if (port == ocelot->npi) 131 return 0; 132 133 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) { 134 err = ocelot_vlan_prepare(ocelot, port, vid, 135 flags & BRIDGE_VLAN_INFO_PVID, 136 flags & BRIDGE_VLAN_INFO_UNTAGGED); 137 if (err) 138 return err; 139 } 140 141 return 0; 142 } 143 144 static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled, 145 struct switchdev_trans *trans) 146 { 147 struct ocelot *ocelot = ds->priv; 148 149 return ocelot_port_vlan_filtering(ocelot, port, enabled, trans); 150 } 151 152 static void felix_vlan_add(struct dsa_switch *ds, int port, 153 const struct switchdev_obj_port_vlan *vlan) 154 { 155 struct ocelot *ocelot = ds->priv; 156 u16 flags = vlan->flags; 157 u16 vid; 158 int err; 159 160 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) { 161 err = ocelot_vlan_add(ocelot, port, vid, 162 flags & BRIDGE_VLAN_INFO_PVID, 163 flags & BRIDGE_VLAN_INFO_UNTAGGED); 164 if (err) { 165 dev_err(ds->dev, "Failed to add VLAN %d to port %d: %d\n", 166 vid, port, err); 167 return; 168 } 169 } 170 } 171 172 static int felix_vlan_del(struct dsa_switch *ds, int port, 173 const struct switchdev_obj_port_vlan *vlan) 174 { 175 struct ocelot *ocelot = ds->priv; 176 u16 vid; 177 int err; 178 179 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) { 180 err = ocelot_vlan_del(ocelot, port, vid); 181 if (err) { 182 dev_err(ds->dev, "Failed to remove VLAN %d from port %d: %d\n", 183 vid, port, err); 184 return err; 185 } 186 } 187 return 0; 188 } 189 190 static int felix_port_enable(struct dsa_switch *ds, int port, 191 struct phy_device *phy) 192 { 193 struct ocelot *ocelot = ds->priv; 194 195 ocelot_port_enable(ocelot, port, phy); 196 197 return 0; 198 } 199 200 static void felix_port_disable(struct dsa_switch *ds, int port) 201 { 202 struct ocelot *ocelot = ds->priv; 203 204 return ocelot_port_disable(ocelot, port); 205 } 206 207 static void felix_phylink_validate(struct dsa_switch *ds, int port, 208 unsigned long *supported, 209 struct phylink_link_state *state) 210 { 211 struct ocelot *ocelot = ds->priv; 212 struct felix *felix = ocelot_to_felix(ocelot); 213 214 if (felix->info->phylink_validate) 215 felix->info->phylink_validate(ocelot, port, supported, state); 216 } 217 218 static void felix_phylink_mac_config(struct dsa_switch *ds, int port, 219 unsigned int link_an_mode, 220 const struct phylink_link_state *state) 221 { 222 struct ocelot *ocelot = ds->priv; 223 struct felix *felix = ocelot_to_felix(ocelot); 224 struct dsa_port *dp = dsa_to_port(ds, port); 225 226 if (felix->pcs[port]) 227 phylink_set_pcs(dp->pl, &felix->pcs[port]->pcs); 228 } 229 230 static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port, 231 unsigned int link_an_mode, 232 phy_interface_t interface) 233 { 234 struct ocelot *ocelot = ds->priv; 235 struct ocelot_port *ocelot_port = ocelot->ports[port]; 236 237 ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG); 238 ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0); 239 } 240 241 static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port, 242 unsigned int link_an_mode, 243 phy_interface_t interface, 244 struct phy_device *phydev, 245 int speed, int duplex, 246 bool tx_pause, bool rx_pause) 247 { 248 struct ocelot *ocelot = ds->priv; 249 struct ocelot_port *ocelot_port = ocelot->ports[port]; 250 struct felix *felix = ocelot_to_felix(ocelot); 251 u32 mac_fc_cfg; 252 253 /* Take port out of reset by clearing the MAC_TX_RST, MAC_RX_RST and 254 * PORT_RST bits in DEV_CLOCK_CFG. Note that the way this system is 255 * integrated is that the MAC speed is fixed and it's the PCS who is 256 * performing the rate adaptation, so we have to write "1000Mbps" into 257 * the LINK_SPEED field of DEV_CLOCK_CFG (which is also its default 258 * value). 259 */ 260 ocelot_port_writel(ocelot_port, 261 DEV_CLOCK_CFG_LINK_SPEED(OCELOT_SPEED_1000), 262 DEV_CLOCK_CFG); 263 264 switch (speed) { 265 case SPEED_10: 266 mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(3); 267 break; 268 case SPEED_100: 269 mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(2); 270 break; 271 case SPEED_1000: 272 case SPEED_2500: 273 mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(1); 274 break; 275 default: 276 dev_err(ocelot->dev, "Unsupported speed on port %d: %d\n", 277 port, speed); 278 return; 279 } 280 281 /* handle Rx pause in all cases, with 2500base-X this is used for rate 282 * adaptation. 283 */ 284 mac_fc_cfg |= SYS_MAC_FC_CFG_RX_FC_ENA; 285 286 if (tx_pause) 287 mac_fc_cfg |= SYS_MAC_FC_CFG_TX_FC_ENA | 288 SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) | 289 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) | 290 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA; 291 292 /* Flow control. Link speed is only used here to evaluate the time 293 * specification in incoming pause frames. 294 */ 295 ocelot_write_rix(ocelot, mac_fc_cfg, SYS_MAC_FC_CFG, port); 296 297 ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port); 298 299 /* Undo the effects of felix_phylink_mac_link_down: 300 * enable MAC module 301 */ 302 ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA | 303 DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG); 304 305 /* Enable receiving frames on the port, and activate auto-learning of 306 * MAC addresses. 307 */ 308 ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO | 309 ANA_PORT_PORT_CFG_RECV_ENA | 310 ANA_PORT_PORT_CFG_PORTID_VAL(port), 311 ANA_PORT_PORT_CFG, port); 312 313 /* Core: Enable port for frame transfer */ 314 ocelot_fields_write(ocelot, port, 315 QSYS_SWITCH_PORT_MODE_PORT_ENA, 1); 316 317 if (felix->info->port_sched_speed_set) 318 felix->info->port_sched_speed_set(ocelot, port, speed); 319 } 320 321 static void felix_port_qos_map_init(struct ocelot *ocelot, int port) 322 { 323 int i; 324 325 ocelot_rmw_gix(ocelot, 326 ANA_PORT_QOS_CFG_QOS_PCP_ENA, 327 ANA_PORT_QOS_CFG_QOS_PCP_ENA, 328 ANA_PORT_QOS_CFG, 329 port); 330 331 for (i = 0; i < FELIX_NUM_TC * 2; i++) { 332 ocelot_rmw_ix(ocelot, 333 (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) | 334 ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i), 335 ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL | 336 ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M, 337 ANA_PORT_PCP_DEI_MAP, 338 port, i); 339 } 340 } 341 342 static void felix_get_strings(struct dsa_switch *ds, int port, 343 u32 stringset, u8 *data) 344 { 345 struct ocelot *ocelot = ds->priv; 346 347 return ocelot_get_strings(ocelot, port, stringset, data); 348 } 349 350 static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data) 351 { 352 struct ocelot *ocelot = ds->priv; 353 354 ocelot_get_ethtool_stats(ocelot, port, data); 355 } 356 357 static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset) 358 { 359 struct ocelot *ocelot = ds->priv; 360 361 return ocelot_get_sset_count(ocelot, port, sset); 362 } 363 364 static int felix_get_ts_info(struct dsa_switch *ds, int port, 365 struct ethtool_ts_info *info) 366 { 367 struct ocelot *ocelot = ds->priv; 368 369 return ocelot_get_ts_info(ocelot, port, info); 370 } 371 372 static int felix_parse_ports_node(struct felix *felix, 373 struct device_node *ports_node, 374 phy_interface_t *port_phy_modes) 375 { 376 struct ocelot *ocelot = &felix->ocelot; 377 struct device *dev = felix->ocelot.dev; 378 struct device_node *child; 379 380 for_each_available_child_of_node(ports_node, child) { 381 phy_interface_t phy_mode; 382 u32 port; 383 int err; 384 385 /* Get switch port number from DT */ 386 if (of_property_read_u32(child, "reg", &port) < 0) { 387 dev_err(dev, "Port number not defined in device tree " 388 "(property \"reg\")\n"); 389 of_node_put(child); 390 return -ENODEV; 391 } 392 393 /* Get PHY mode from DT */ 394 err = of_get_phy_mode(child, &phy_mode); 395 if (err) { 396 dev_err(dev, "Failed to read phy-mode or " 397 "phy-interface-type property for port %d\n", 398 port); 399 of_node_put(child); 400 return -ENODEV; 401 } 402 403 err = felix->info->prevalidate_phy_mode(ocelot, port, phy_mode); 404 if (err < 0) { 405 dev_err(dev, "Unsupported PHY mode %s on port %d\n", 406 phy_modes(phy_mode), port); 407 of_node_put(child); 408 return err; 409 } 410 411 port_phy_modes[port] = phy_mode; 412 } 413 414 return 0; 415 } 416 417 static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes) 418 { 419 struct device *dev = felix->ocelot.dev; 420 struct device_node *switch_node; 421 struct device_node *ports_node; 422 int err; 423 424 switch_node = dev->of_node; 425 426 ports_node = of_get_child_by_name(switch_node, "ports"); 427 if (!ports_node) { 428 dev_err(dev, "Incorrect bindings: absent \"ports\" node\n"); 429 return -ENODEV; 430 } 431 432 err = felix_parse_ports_node(felix, ports_node, port_phy_modes); 433 of_node_put(ports_node); 434 435 return err; 436 } 437 438 static int felix_init_structs(struct felix *felix, int num_phys_ports) 439 { 440 struct ocelot *ocelot = &felix->ocelot; 441 phy_interface_t *port_phy_modes; 442 struct resource res; 443 int port, i, err; 444 445 ocelot->num_phys_ports = num_phys_ports; 446 ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports, 447 sizeof(struct ocelot_port *), GFP_KERNEL); 448 if (!ocelot->ports) 449 return -ENOMEM; 450 451 ocelot->map = felix->info->map; 452 ocelot->stats_layout = felix->info->stats_layout; 453 ocelot->num_stats = felix->info->num_stats; 454 ocelot->shared_queue_sz = felix->info->shared_queue_sz; 455 ocelot->num_mact_rows = felix->info->num_mact_rows; 456 ocelot->vcap = felix->info->vcap; 457 ocelot->ops = felix->info->ops; 458 ocelot->inj_prefix = OCELOT_TAG_PREFIX_SHORT; 459 ocelot->xtr_prefix = OCELOT_TAG_PREFIX_SHORT; 460 461 port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t), 462 GFP_KERNEL); 463 if (!port_phy_modes) 464 return -ENOMEM; 465 466 err = felix_parse_dt(felix, port_phy_modes); 467 if (err) { 468 kfree(port_phy_modes); 469 return err; 470 } 471 472 for (i = 0; i < TARGET_MAX; i++) { 473 struct regmap *target; 474 475 if (!felix->info->target_io_res[i].name) 476 continue; 477 478 memcpy(&res, &felix->info->target_io_res[i], sizeof(res)); 479 res.flags = IORESOURCE_MEM; 480 res.start += felix->switch_base; 481 res.end += felix->switch_base; 482 483 target = ocelot_regmap_init(ocelot, &res); 484 if (IS_ERR(target)) { 485 dev_err(ocelot->dev, 486 "Failed to map device memory space\n"); 487 kfree(port_phy_modes); 488 return PTR_ERR(target); 489 } 490 491 ocelot->targets[i] = target; 492 } 493 494 err = ocelot_regfields_init(ocelot, felix->info->regfields); 495 if (err) { 496 dev_err(ocelot->dev, "failed to init reg fields map\n"); 497 kfree(port_phy_modes); 498 return err; 499 } 500 501 for (port = 0; port < num_phys_ports; port++) { 502 struct ocelot_port *ocelot_port; 503 struct regmap *target; 504 u8 *template; 505 506 ocelot_port = devm_kzalloc(ocelot->dev, 507 sizeof(struct ocelot_port), 508 GFP_KERNEL); 509 if (!ocelot_port) { 510 dev_err(ocelot->dev, 511 "failed to allocate port memory\n"); 512 kfree(port_phy_modes); 513 return -ENOMEM; 514 } 515 516 memcpy(&res, &felix->info->port_io_res[port], sizeof(res)); 517 res.flags = IORESOURCE_MEM; 518 res.start += felix->switch_base; 519 res.end += felix->switch_base; 520 521 target = ocelot_regmap_init(ocelot, &res); 522 if (IS_ERR(target)) { 523 dev_err(ocelot->dev, 524 "Failed to map memory space for port %d\n", 525 port); 526 kfree(port_phy_modes); 527 return PTR_ERR(target); 528 } 529 530 template = devm_kzalloc(ocelot->dev, OCELOT_TOTAL_TAG_LEN, 531 GFP_KERNEL); 532 if (!template) { 533 dev_err(ocelot->dev, 534 "Failed to allocate memory for DSA tag\n"); 535 kfree(port_phy_modes); 536 return -ENOMEM; 537 } 538 539 ocelot_port->phy_mode = port_phy_modes[port]; 540 ocelot_port->ocelot = ocelot; 541 ocelot_port->target = target; 542 ocelot_port->xmit_template = template; 543 ocelot->ports[port] = ocelot_port; 544 545 felix->info->xmit_template_populate(ocelot, port); 546 } 547 548 kfree(port_phy_modes); 549 550 if (felix->info->mdio_bus_alloc) { 551 err = felix->info->mdio_bus_alloc(ocelot); 552 if (err < 0) 553 return err; 554 } 555 556 return 0; 557 } 558 559 /* The CPU port module is connected to the Node Processor Interface (NPI). This 560 * is the mode through which frames can be injected from and extracted to an 561 * external CPU, over Ethernet. 562 */ 563 static void felix_npi_port_init(struct ocelot *ocelot, int port) 564 { 565 ocelot->npi = port; 566 567 ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M | 568 QSYS_EXT_CPU_CFG_EXT_CPU_PORT(port), 569 QSYS_EXT_CPU_CFG); 570 571 /* NPI port Injection/Extraction configuration */ 572 ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR, 573 ocelot->xtr_prefix); 574 ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR, 575 ocelot->inj_prefix); 576 577 /* Disable transmission of pause frames */ 578 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0); 579 } 580 581 /* Hardware initialization done here so that we can allocate structures with 582 * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing 583 * us to allocate structures twice (leak memory) and map PCI memory twice 584 * (which will not work). 585 */ 586 static int felix_setup(struct dsa_switch *ds) 587 { 588 struct ocelot *ocelot = ds->priv; 589 struct felix *felix = ocelot_to_felix(ocelot); 590 int port, err; 591 int tc; 592 593 err = felix_init_structs(felix, ds->num_ports); 594 if (err) 595 return err; 596 597 err = ocelot_init(ocelot); 598 if (err) 599 return err; 600 601 if (ocelot->ptp) { 602 err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps); 603 if (err) { 604 dev_err(ocelot->dev, 605 "Timestamp initialization failed\n"); 606 ocelot->ptp = 0; 607 } 608 } 609 610 for (port = 0; port < ds->num_ports; port++) { 611 ocelot_init_port(ocelot, port); 612 613 if (dsa_is_cpu_port(ds, port)) 614 felix_npi_port_init(ocelot, port); 615 616 /* Set the default QoS Classification based on PCP and DEI 617 * bits of vlan tag. 618 */ 619 felix_port_qos_map_init(ocelot, port); 620 } 621 622 /* Include the CPU port module in the forwarding mask for unknown 623 * unicast - the hardware default value for ANA_FLOODING_FLD_UNICAST 624 * excludes BIT(ocelot->num_phys_ports), and so does ocelot_init, since 625 * Ocelot relies on whitelisting MAC addresses towards PGID_CPU. 626 */ 627 ocelot_write_rix(ocelot, 628 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)), 629 ANA_PGID_PGID, PGID_UC); 630 /* Setup the per-traffic class flooding PGIDs */ 631 for (tc = 0; tc < FELIX_NUM_TC; tc++) 632 ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) | 633 ANA_FLOODING_FLD_BROADCAST(PGID_MC) | 634 ANA_FLOODING_FLD_UNICAST(PGID_UC), 635 ANA_FLOODING, tc); 636 637 ds->mtu_enforcement_ingress = true; 638 ds->configure_vlan_while_not_filtering = true; 639 640 return 0; 641 } 642 643 static void felix_teardown(struct dsa_switch *ds) 644 { 645 struct ocelot *ocelot = ds->priv; 646 struct felix *felix = ocelot_to_felix(ocelot); 647 int port; 648 649 if (felix->info->mdio_bus_free) 650 felix->info->mdio_bus_free(ocelot); 651 652 for (port = 0; port < ocelot->num_phys_ports; port++) 653 ocelot_deinit_port(ocelot, port); 654 ocelot_deinit_timestamp(ocelot); 655 /* stop workqueue thread */ 656 ocelot_deinit(ocelot); 657 } 658 659 static int felix_hwtstamp_get(struct dsa_switch *ds, int port, 660 struct ifreq *ifr) 661 { 662 struct ocelot *ocelot = ds->priv; 663 664 return ocelot_hwstamp_get(ocelot, port, ifr); 665 } 666 667 static int felix_hwtstamp_set(struct dsa_switch *ds, int port, 668 struct ifreq *ifr) 669 { 670 struct ocelot *ocelot = ds->priv; 671 672 return ocelot_hwstamp_set(ocelot, port, ifr); 673 } 674 675 static bool felix_rxtstamp(struct dsa_switch *ds, int port, 676 struct sk_buff *skb, unsigned int type) 677 { 678 struct skb_shared_hwtstamps *shhwtstamps; 679 struct ocelot *ocelot = ds->priv; 680 u8 *extraction = skb->data - ETH_HLEN - OCELOT_TAG_LEN; 681 u32 tstamp_lo, tstamp_hi; 682 struct timespec64 ts; 683 u64 tstamp, val; 684 685 ocelot_ptp_gettime64(&ocelot->ptp_info, &ts); 686 tstamp = ktime_set(ts.tv_sec, ts.tv_nsec); 687 688 packing(extraction, &val, 116, 85, OCELOT_TAG_LEN, UNPACK, 0); 689 tstamp_lo = (u32)val; 690 691 tstamp_hi = tstamp >> 32; 692 if ((tstamp & 0xffffffff) < tstamp_lo) 693 tstamp_hi--; 694 695 tstamp = ((u64)tstamp_hi << 32) | tstamp_lo; 696 697 shhwtstamps = skb_hwtstamps(skb); 698 memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps)); 699 shhwtstamps->hwtstamp = tstamp; 700 return false; 701 } 702 703 static bool felix_txtstamp(struct dsa_switch *ds, int port, 704 struct sk_buff *clone, unsigned int type) 705 { 706 struct ocelot *ocelot = ds->priv; 707 struct ocelot_port *ocelot_port = ocelot->ports[port]; 708 709 if (ocelot->ptp && (skb_shinfo(clone)->tx_flags & SKBTX_HW_TSTAMP) && 710 ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) { 711 ocelot_port_add_txtstamp_skb(ocelot, port, clone); 712 return true; 713 } 714 715 return false; 716 } 717 718 static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu) 719 { 720 struct ocelot *ocelot = ds->priv; 721 722 ocelot_port_set_maxlen(ocelot, port, new_mtu); 723 724 return 0; 725 } 726 727 static int felix_get_max_mtu(struct dsa_switch *ds, int port) 728 { 729 struct ocelot *ocelot = ds->priv; 730 731 return ocelot_get_max_mtu(ocelot, port); 732 } 733 734 static int felix_cls_flower_add(struct dsa_switch *ds, int port, 735 struct flow_cls_offload *cls, bool ingress) 736 { 737 struct ocelot *ocelot = ds->priv; 738 739 return ocelot_cls_flower_replace(ocelot, port, cls, ingress); 740 } 741 742 static int felix_cls_flower_del(struct dsa_switch *ds, int port, 743 struct flow_cls_offload *cls, bool ingress) 744 { 745 struct ocelot *ocelot = ds->priv; 746 747 return ocelot_cls_flower_destroy(ocelot, port, cls, ingress); 748 } 749 750 static int felix_cls_flower_stats(struct dsa_switch *ds, int port, 751 struct flow_cls_offload *cls, bool ingress) 752 { 753 struct ocelot *ocelot = ds->priv; 754 755 return ocelot_cls_flower_stats(ocelot, port, cls, ingress); 756 } 757 758 static int felix_port_policer_add(struct dsa_switch *ds, int port, 759 struct dsa_mall_policer_tc_entry *policer) 760 { 761 struct ocelot *ocelot = ds->priv; 762 struct ocelot_policer pol = { 763 .rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8, 764 .burst = policer->burst, 765 }; 766 767 return ocelot_port_policer_add(ocelot, port, &pol); 768 } 769 770 static void felix_port_policer_del(struct dsa_switch *ds, int port) 771 { 772 struct ocelot *ocelot = ds->priv; 773 774 ocelot_port_policer_del(ocelot, port); 775 } 776 777 static int felix_port_setup_tc(struct dsa_switch *ds, int port, 778 enum tc_setup_type type, 779 void *type_data) 780 { 781 struct ocelot *ocelot = ds->priv; 782 struct felix *felix = ocelot_to_felix(ocelot); 783 784 if (felix->info->port_setup_tc) 785 return felix->info->port_setup_tc(ds, port, type, type_data); 786 else 787 return -EOPNOTSUPP; 788 } 789 790 const struct dsa_switch_ops felix_switch_ops = { 791 .get_tag_protocol = felix_get_tag_protocol, 792 .setup = felix_setup, 793 .teardown = felix_teardown, 794 .set_ageing_time = felix_set_ageing_time, 795 .get_strings = felix_get_strings, 796 .get_ethtool_stats = felix_get_ethtool_stats, 797 .get_sset_count = felix_get_sset_count, 798 .get_ts_info = felix_get_ts_info, 799 .phylink_validate = felix_phylink_validate, 800 .phylink_mac_config = felix_phylink_mac_config, 801 .phylink_mac_link_down = felix_phylink_mac_link_down, 802 .phylink_mac_link_up = felix_phylink_mac_link_up, 803 .port_enable = felix_port_enable, 804 .port_disable = felix_port_disable, 805 .port_fdb_dump = felix_fdb_dump, 806 .port_fdb_add = felix_fdb_add, 807 .port_fdb_del = felix_fdb_del, 808 .port_mdb_prepare = felix_mdb_prepare, 809 .port_mdb_add = felix_mdb_add, 810 .port_mdb_del = felix_mdb_del, 811 .port_bridge_join = felix_bridge_join, 812 .port_bridge_leave = felix_bridge_leave, 813 .port_stp_state_set = felix_bridge_stp_state_set, 814 .port_vlan_prepare = felix_vlan_prepare, 815 .port_vlan_filtering = felix_vlan_filtering, 816 .port_vlan_add = felix_vlan_add, 817 .port_vlan_del = felix_vlan_del, 818 .port_hwtstamp_get = felix_hwtstamp_get, 819 .port_hwtstamp_set = felix_hwtstamp_set, 820 .port_rxtstamp = felix_rxtstamp, 821 .port_txtstamp = felix_txtstamp, 822 .port_change_mtu = felix_change_mtu, 823 .port_max_mtu = felix_get_max_mtu, 824 .port_policer_add = felix_port_policer_add, 825 .port_policer_del = felix_port_policer_del, 826 .cls_flower_add = felix_cls_flower_add, 827 .cls_flower_del = felix_cls_flower_del, 828 .cls_flower_stats = felix_cls_flower_stats, 829 .port_setup_tc = felix_port_setup_tc, 830 }; 831 832 struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port) 833 { 834 struct felix *felix = ocelot_to_felix(ocelot); 835 struct dsa_switch *ds = felix->ds; 836 837 if (!dsa_is_user_port(ds, port)) 838 return NULL; 839 840 return dsa_to_port(ds, port)->slave; 841 } 842 843 int felix_netdev_to_port(struct net_device *dev) 844 { 845 struct dsa_port *dp; 846 847 dp = dsa_port_from_netdev(dev); 848 if (IS_ERR(dp)) 849 return -EINVAL; 850 851 return dp->index; 852 } 853