1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright 2019-2021 NXP 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/dsa/8021q.h> 17 #include <linux/dsa/ocelot.h> 18 #include <linux/platform_device.h> 19 #include <linux/ptp_classify.h> 20 #include <linux/module.h> 21 #include <linux/of_net.h> 22 #include <linux/pci.h> 23 #include <linux/of.h> 24 #include <net/pkt_sched.h> 25 #include <net/dsa.h> 26 #include "felix.h" 27 28 /* Set up VCAP ES0 rules for pushing a tag_8021q VLAN towards the CPU such that 29 * the tagger can perform RX source port identification. 30 */ 31 static int felix_tag_8021q_vlan_add_rx(struct felix *felix, int port, u16 vid) 32 { 33 struct ocelot_vcap_filter *outer_tagging_rule; 34 struct ocelot *ocelot = &felix->ocelot; 35 struct dsa_switch *ds = felix->ds; 36 int key_length, upstream, err; 37 38 key_length = ocelot->vcap[VCAP_ES0].keys[VCAP_ES0_IGR_PORT].length; 39 upstream = dsa_upstream_port(ds, port); 40 41 outer_tagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter), 42 GFP_KERNEL); 43 if (!outer_tagging_rule) 44 return -ENOMEM; 45 46 outer_tagging_rule->key_type = OCELOT_VCAP_KEY_ANY; 47 outer_tagging_rule->prio = 1; 48 outer_tagging_rule->id.cookie = OCELOT_VCAP_ES0_TAG_8021Q_RXVLAN(ocelot, port); 49 outer_tagging_rule->id.tc_offload = false; 50 outer_tagging_rule->block_id = VCAP_ES0; 51 outer_tagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD; 52 outer_tagging_rule->lookup = 0; 53 outer_tagging_rule->ingress_port.value = port; 54 outer_tagging_rule->ingress_port.mask = GENMASK(key_length - 1, 0); 55 outer_tagging_rule->egress_port.value = upstream; 56 outer_tagging_rule->egress_port.mask = GENMASK(key_length - 1, 0); 57 outer_tagging_rule->action.push_outer_tag = OCELOT_ES0_TAG; 58 outer_tagging_rule->action.tag_a_tpid_sel = OCELOT_TAG_TPID_SEL_8021AD; 59 outer_tagging_rule->action.tag_a_vid_sel = 1; 60 outer_tagging_rule->action.vid_a_val = vid; 61 62 err = ocelot_vcap_filter_add(ocelot, outer_tagging_rule, NULL); 63 if (err) 64 kfree(outer_tagging_rule); 65 66 return err; 67 } 68 69 static int felix_tag_8021q_vlan_del_rx(struct felix *felix, int port, u16 vid) 70 { 71 struct ocelot_vcap_filter *outer_tagging_rule; 72 struct ocelot_vcap_block *block_vcap_es0; 73 struct ocelot *ocelot = &felix->ocelot; 74 75 block_vcap_es0 = &ocelot->block[VCAP_ES0]; 76 77 outer_tagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_es0, 78 port, false); 79 if (!outer_tagging_rule) 80 return -ENOENT; 81 82 return ocelot_vcap_filter_del(ocelot, outer_tagging_rule); 83 } 84 85 /* Set up VCAP IS1 rules for stripping the tag_8021q VLAN on TX and VCAP IS2 86 * rules for steering those tagged packets towards the correct destination port 87 */ 88 static int felix_tag_8021q_vlan_add_tx(struct felix *felix, int port, u16 vid) 89 { 90 struct ocelot_vcap_filter *untagging_rule, *redirect_rule; 91 struct ocelot *ocelot = &felix->ocelot; 92 struct dsa_switch *ds = felix->ds; 93 int upstream, err; 94 95 untagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL); 96 if (!untagging_rule) 97 return -ENOMEM; 98 99 redirect_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL); 100 if (!redirect_rule) { 101 kfree(untagging_rule); 102 return -ENOMEM; 103 } 104 105 upstream = dsa_upstream_port(ds, port); 106 107 untagging_rule->key_type = OCELOT_VCAP_KEY_ANY; 108 untagging_rule->ingress_port_mask = BIT(upstream); 109 untagging_rule->vlan.vid.value = vid; 110 untagging_rule->vlan.vid.mask = VLAN_VID_MASK; 111 untagging_rule->prio = 1; 112 untagging_rule->id.cookie = OCELOT_VCAP_IS1_TAG_8021Q_TXVLAN(ocelot, port); 113 untagging_rule->id.tc_offload = false; 114 untagging_rule->block_id = VCAP_IS1; 115 untagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD; 116 untagging_rule->lookup = 0; 117 untagging_rule->action.vlan_pop_cnt_ena = true; 118 untagging_rule->action.vlan_pop_cnt = 1; 119 untagging_rule->action.pag_override_mask = 0xff; 120 untagging_rule->action.pag_val = port; 121 122 err = ocelot_vcap_filter_add(ocelot, untagging_rule, NULL); 123 if (err) { 124 kfree(untagging_rule); 125 kfree(redirect_rule); 126 return err; 127 } 128 129 redirect_rule->key_type = OCELOT_VCAP_KEY_ANY; 130 redirect_rule->ingress_port_mask = BIT(upstream); 131 redirect_rule->pag = port; 132 redirect_rule->prio = 1; 133 redirect_rule->id.cookie = OCELOT_VCAP_IS2_TAG_8021Q_TXVLAN(ocelot, port); 134 redirect_rule->id.tc_offload = false; 135 redirect_rule->block_id = VCAP_IS2; 136 redirect_rule->type = OCELOT_VCAP_FILTER_OFFLOAD; 137 redirect_rule->lookup = 0; 138 redirect_rule->action.mask_mode = OCELOT_MASK_MODE_REDIRECT; 139 redirect_rule->action.port_mask = BIT(port); 140 141 err = ocelot_vcap_filter_add(ocelot, redirect_rule, NULL); 142 if (err) { 143 ocelot_vcap_filter_del(ocelot, untagging_rule); 144 kfree(redirect_rule); 145 return err; 146 } 147 148 return 0; 149 } 150 151 static int felix_tag_8021q_vlan_del_tx(struct felix *felix, int port, u16 vid) 152 { 153 struct ocelot_vcap_filter *untagging_rule, *redirect_rule; 154 struct ocelot_vcap_block *block_vcap_is1; 155 struct ocelot_vcap_block *block_vcap_is2; 156 struct ocelot *ocelot = &felix->ocelot; 157 int err; 158 159 block_vcap_is1 = &ocelot->block[VCAP_IS1]; 160 block_vcap_is2 = &ocelot->block[VCAP_IS2]; 161 162 untagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is1, 163 port, false); 164 if (!untagging_rule) 165 return -ENOENT; 166 167 err = ocelot_vcap_filter_del(ocelot, untagging_rule); 168 if (err) 169 return err; 170 171 redirect_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, 172 port, false); 173 if (!redirect_rule) 174 return -ENOENT; 175 176 return ocelot_vcap_filter_del(ocelot, redirect_rule); 177 } 178 179 static int felix_tag_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid, 180 u16 flags) 181 { 182 struct ocelot *ocelot = ds->priv; 183 int err; 184 185 /* tag_8021q.c assumes we are implementing this via port VLAN 186 * membership, which we aren't. So we don't need to add any VCAP filter 187 * for the CPU port. 188 */ 189 if (!dsa_is_user_port(ds, port)) 190 return 0; 191 192 err = felix_tag_8021q_vlan_add_rx(ocelot_to_felix(ocelot), port, vid); 193 if (err) 194 return err; 195 196 err = felix_tag_8021q_vlan_add_tx(ocelot_to_felix(ocelot), port, vid); 197 if (err) { 198 felix_tag_8021q_vlan_del_rx(ocelot_to_felix(ocelot), port, vid); 199 return err; 200 } 201 202 return 0; 203 } 204 205 static int felix_tag_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid) 206 { 207 struct ocelot *ocelot = ds->priv; 208 int err; 209 210 if (!dsa_is_user_port(ds, port)) 211 return 0; 212 213 err = felix_tag_8021q_vlan_del_rx(ocelot_to_felix(ocelot), port, vid); 214 if (err) 215 return err; 216 217 err = felix_tag_8021q_vlan_del_tx(ocelot_to_felix(ocelot), port, vid); 218 if (err) { 219 felix_tag_8021q_vlan_add_rx(ocelot_to_felix(ocelot), port, vid); 220 return err; 221 } 222 223 return 0; 224 } 225 226 /* Alternatively to using the NPI functionality, that same hardware MAC 227 * connected internally to the enetc or fman DSA master can be configured to 228 * use the software-defined tag_8021q frame format. As far as the hardware is 229 * concerned, it thinks it is a "dumb switch" - the queues of the CPU port 230 * module are now disconnected from it, but can still be accessed through 231 * register-based MMIO. 232 */ 233 static void felix_8021q_cpu_port_init(struct ocelot *ocelot, int port) 234 { 235 mutex_lock(&ocelot->fwd_domain_lock); 236 237 ocelot->ports[port]->is_dsa_8021q_cpu = true; 238 ocelot->npi = -1; 239 240 /* Overwrite PGID_CPU with the non-tagging port */ 241 ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, PGID_CPU); 242 243 ocelot_apply_bridge_fwd_mask(ocelot, true); 244 245 mutex_unlock(&ocelot->fwd_domain_lock); 246 } 247 248 static void felix_8021q_cpu_port_deinit(struct ocelot *ocelot, int port) 249 { 250 mutex_lock(&ocelot->fwd_domain_lock); 251 252 ocelot->ports[port]->is_dsa_8021q_cpu = false; 253 254 /* Restore PGID_CPU */ 255 ocelot_write_rix(ocelot, BIT(ocelot->num_phys_ports), ANA_PGID_PGID, 256 PGID_CPU); 257 258 ocelot_apply_bridge_fwd_mask(ocelot, true); 259 260 mutex_unlock(&ocelot->fwd_domain_lock); 261 } 262 263 /* On switches with no extraction IRQ wired, trapped packets need to be 264 * replicated over Ethernet as well, otherwise we'd get no notification of 265 * their arrival when using the ocelot-8021q tagging protocol. 266 */ 267 static int felix_update_trapping_destinations(struct dsa_switch *ds, 268 bool using_tag_8021q) 269 { 270 struct ocelot *ocelot = ds->priv; 271 struct felix *felix = ocelot_to_felix(ocelot); 272 struct ocelot_vcap_filter *trap; 273 enum ocelot_mask_mode mask_mode; 274 unsigned long port_mask; 275 struct dsa_port *dp; 276 bool cpu_copy_ena; 277 int cpu = -1, err; 278 279 if (!felix->info->quirk_no_xtr_irq) 280 return 0; 281 282 /* Figure out the current CPU port */ 283 dsa_switch_for_each_cpu_port(dp, ds) { 284 cpu = dp->index; 285 break; 286 } 287 288 /* We are sure that "cpu" was found, otherwise 289 * dsa_tree_setup_default_cpu() would have failed earlier. 290 */ 291 292 /* Make sure all traps are set up for that destination */ 293 list_for_each_entry(trap, &ocelot->traps, trap_list) { 294 /* Figure out the current trapping destination */ 295 if (using_tag_8021q) { 296 /* Redirect to the tag_8021q CPU port. If timestamps 297 * are necessary, also copy trapped packets to the CPU 298 * port module. 299 */ 300 mask_mode = OCELOT_MASK_MODE_REDIRECT; 301 port_mask = BIT(cpu); 302 cpu_copy_ena = !!trap->take_ts; 303 } else { 304 /* Trap packets only to the CPU port module, which is 305 * redirected to the NPI port (the DSA CPU port) 306 */ 307 mask_mode = OCELOT_MASK_MODE_PERMIT_DENY; 308 port_mask = 0; 309 cpu_copy_ena = true; 310 } 311 312 if (trap->action.mask_mode == mask_mode && 313 trap->action.port_mask == port_mask && 314 trap->action.cpu_copy_ena == cpu_copy_ena) 315 continue; 316 317 trap->action.mask_mode = mask_mode; 318 trap->action.port_mask = port_mask; 319 trap->action.cpu_copy_ena = cpu_copy_ena; 320 321 err = ocelot_vcap_filter_replace(ocelot, trap); 322 if (err) 323 return err; 324 } 325 326 return 0; 327 } 328 329 static int felix_setup_tag_8021q(struct dsa_switch *ds, int cpu) 330 { 331 struct ocelot *ocelot = ds->priv; 332 unsigned long cpu_flood; 333 struct dsa_port *dp; 334 int err; 335 336 felix_8021q_cpu_port_init(ocelot, cpu); 337 338 dsa_switch_for_each_available_port(dp, ds) { 339 /* This overwrites ocelot_init(): 340 * Do not forward BPDU frames to the CPU port module, 341 * for 2 reasons: 342 * - When these packets are injected from the tag_8021q 343 * CPU port, we want them to go out, not loop back 344 * into the system. 345 * - STP traffic ingressing on a user port should go to 346 * the tag_8021q CPU port, not to the hardware CPU 347 * port module. 348 */ 349 ocelot_write_gix(ocelot, 350 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0), 351 ANA_PORT_CPU_FWD_BPDU_CFG, dp->index); 352 } 353 354 /* In tag_8021q mode, the CPU port module is unused, except for PTP 355 * frames. So we want to disable flooding of any kind to the CPU port 356 * module, since packets going there will end in a black hole. 357 */ 358 cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)); 359 ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_UC); 360 ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_MC); 361 ocelot_rmw_rix(ocelot, 0, cpu_flood, ANA_PGID_PGID, PGID_BC); 362 363 err = dsa_tag_8021q_register(ds, htons(ETH_P_8021AD)); 364 if (err) 365 return err; 366 367 err = felix_update_trapping_destinations(ds, true); 368 if (err) 369 goto out_tag_8021q_unregister; 370 371 /* The ownership of the CPU port module's queues might have just been 372 * transferred to the tag_8021q tagger from the NPI-based tagger. 373 * So there might still be all sorts of crap in the queues. On the 374 * other hand, the MMIO-based matching of PTP frames is very brittle, 375 * so we need to be careful that there are no extra frames to be 376 * dequeued over MMIO, since we would never know to discard them. 377 */ 378 ocelot_drain_cpu_queue(ocelot, 0); 379 380 return 0; 381 382 out_tag_8021q_unregister: 383 dsa_tag_8021q_unregister(ds); 384 return err; 385 } 386 387 static void felix_teardown_tag_8021q(struct dsa_switch *ds, int cpu) 388 { 389 struct ocelot *ocelot = ds->priv; 390 struct dsa_port *dp; 391 int err; 392 393 err = felix_update_trapping_destinations(ds, false); 394 if (err) 395 dev_err(ds->dev, "felix_teardown_mmio_filtering returned %d", 396 err); 397 398 dsa_tag_8021q_unregister(ds); 399 400 dsa_switch_for_each_available_port(dp, ds) { 401 /* Restore the logic from ocelot_init: 402 * do not forward BPDU frames to the front ports. 403 */ 404 ocelot_write_gix(ocelot, 405 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff), 406 ANA_PORT_CPU_FWD_BPDU_CFG, 407 dp->index); 408 } 409 410 felix_8021q_cpu_port_deinit(ocelot, cpu); 411 } 412 413 /* The CPU port module is connected to the Node Processor Interface (NPI). This 414 * is the mode through which frames can be injected from and extracted to an 415 * external CPU, over Ethernet. In NXP SoCs, the "external CPU" is the ARM CPU 416 * running Linux, and this forms a DSA setup together with the enetc or fman 417 * DSA master. 418 */ 419 static void felix_npi_port_init(struct ocelot *ocelot, int port) 420 { 421 ocelot->npi = port; 422 423 ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M | 424 QSYS_EXT_CPU_CFG_EXT_CPU_PORT(port), 425 QSYS_EXT_CPU_CFG); 426 427 /* NPI port Injection/Extraction configuration */ 428 ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR, 429 ocelot->npi_xtr_prefix); 430 ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR, 431 ocelot->npi_inj_prefix); 432 433 /* Disable transmission of pause frames */ 434 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0); 435 } 436 437 static void felix_npi_port_deinit(struct ocelot *ocelot, int port) 438 { 439 /* Restore hardware defaults */ 440 int unused_port = ocelot->num_phys_ports + 2; 441 442 ocelot->npi = -1; 443 444 ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPU_PORT(unused_port), 445 QSYS_EXT_CPU_CFG); 446 447 ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR, 448 OCELOT_TAG_PREFIX_DISABLED); 449 ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR, 450 OCELOT_TAG_PREFIX_DISABLED); 451 452 /* Enable transmission of pause frames */ 453 ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1); 454 } 455 456 static int felix_setup_tag_npi(struct dsa_switch *ds, int cpu) 457 { 458 struct ocelot *ocelot = ds->priv; 459 unsigned long cpu_flood; 460 461 felix_npi_port_init(ocelot, cpu); 462 463 /* Include the CPU port module (and indirectly, the NPI port) 464 * in the forwarding mask for unknown unicast - the hardware 465 * default value for ANA_FLOODING_FLD_UNICAST excludes 466 * BIT(ocelot->num_phys_ports), and so does ocelot_init, 467 * since Ocelot relies on whitelisting MAC addresses towards 468 * PGID_CPU. 469 * We do this because DSA does not yet perform RX filtering, 470 * and the NPI port does not perform source address learning, 471 * so traffic sent to Linux is effectively unknown from the 472 * switch's perspective. 473 */ 474 cpu_flood = ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)); 475 ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_UC); 476 ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_MC); 477 ocelot_rmw_rix(ocelot, cpu_flood, cpu_flood, ANA_PGID_PGID, PGID_BC); 478 479 return 0; 480 } 481 482 static void felix_teardown_tag_npi(struct dsa_switch *ds, int cpu) 483 { 484 struct ocelot *ocelot = ds->priv; 485 486 felix_npi_port_deinit(ocelot, cpu); 487 } 488 489 static int felix_set_tag_protocol(struct dsa_switch *ds, int cpu, 490 enum dsa_tag_protocol proto) 491 { 492 int err; 493 494 switch (proto) { 495 case DSA_TAG_PROTO_SEVILLE: 496 case DSA_TAG_PROTO_OCELOT: 497 err = felix_setup_tag_npi(ds, cpu); 498 break; 499 case DSA_TAG_PROTO_OCELOT_8021Q: 500 err = felix_setup_tag_8021q(ds, cpu); 501 break; 502 default: 503 err = -EPROTONOSUPPORT; 504 } 505 506 return err; 507 } 508 509 static void felix_del_tag_protocol(struct dsa_switch *ds, int cpu, 510 enum dsa_tag_protocol proto) 511 { 512 switch (proto) { 513 case DSA_TAG_PROTO_SEVILLE: 514 case DSA_TAG_PROTO_OCELOT: 515 felix_teardown_tag_npi(ds, cpu); 516 break; 517 case DSA_TAG_PROTO_OCELOT_8021Q: 518 felix_teardown_tag_8021q(ds, cpu); 519 break; 520 default: 521 break; 522 } 523 } 524 525 /* This always leaves the switch in a consistent state, because although the 526 * tag_8021q setup can fail, the NPI setup can't. So either the change is made, 527 * or the restoration is guaranteed to work. 528 */ 529 static int felix_change_tag_protocol(struct dsa_switch *ds, int cpu, 530 enum dsa_tag_protocol proto) 531 { 532 struct ocelot *ocelot = ds->priv; 533 struct felix *felix = ocelot_to_felix(ocelot); 534 enum dsa_tag_protocol old_proto = felix->tag_proto; 535 int err; 536 537 if (proto != DSA_TAG_PROTO_SEVILLE && 538 proto != DSA_TAG_PROTO_OCELOT && 539 proto != DSA_TAG_PROTO_OCELOT_8021Q) 540 return -EPROTONOSUPPORT; 541 542 felix_del_tag_protocol(ds, cpu, old_proto); 543 544 err = felix_set_tag_protocol(ds, cpu, proto); 545 if (err) { 546 felix_set_tag_protocol(ds, cpu, old_proto); 547 return err; 548 } 549 550 felix->tag_proto = proto; 551 552 return 0; 553 } 554 555 static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds, 556 int port, 557 enum dsa_tag_protocol mp) 558 { 559 struct ocelot *ocelot = ds->priv; 560 struct felix *felix = ocelot_to_felix(ocelot); 561 562 return felix->tag_proto; 563 } 564 565 static int felix_set_ageing_time(struct dsa_switch *ds, 566 unsigned int ageing_time) 567 { 568 struct ocelot *ocelot = ds->priv; 569 570 ocelot_set_ageing_time(ocelot, ageing_time); 571 572 return 0; 573 } 574 575 static void felix_port_fast_age(struct dsa_switch *ds, int port) 576 { 577 struct ocelot *ocelot = ds->priv; 578 int err; 579 580 err = ocelot_mact_flush(ocelot, port); 581 if (err) 582 dev_err(ds->dev, "Flushing MAC table on port %d returned %pe\n", 583 port, ERR_PTR(err)); 584 } 585 586 static int felix_fdb_dump(struct dsa_switch *ds, int port, 587 dsa_fdb_dump_cb_t *cb, void *data) 588 { 589 struct ocelot *ocelot = ds->priv; 590 591 return ocelot_fdb_dump(ocelot, port, cb, data); 592 } 593 594 static int felix_fdb_add(struct dsa_switch *ds, int port, 595 const unsigned char *addr, u16 vid, 596 struct dsa_db db) 597 { 598 struct ocelot *ocelot = ds->priv; 599 600 return ocelot_fdb_add(ocelot, port, addr, vid); 601 } 602 603 static int felix_fdb_del(struct dsa_switch *ds, int port, 604 const unsigned char *addr, u16 vid, 605 struct dsa_db db) 606 { 607 struct ocelot *ocelot = ds->priv; 608 609 return ocelot_fdb_del(ocelot, port, addr, vid); 610 } 611 612 static int felix_lag_fdb_add(struct dsa_switch *ds, struct dsa_lag lag, 613 const unsigned char *addr, u16 vid, 614 struct dsa_db db) 615 { 616 struct ocelot *ocelot = ds->priv; 617 618 return ocelot_lag_fdb_add(ocelot, lag.dev, addr, vid); 619 } 620 621 static int felix_lag_fdb_del(struct dsa_switch *ds, struct dsa_lag lag, 622 const unsigned char *addr, u16 vid, 623 struct dsa_db db) 624 { 625 struct ocelot *ocelot = ds->priv; 626 627 return ocelot_lag_fdb_del(ocelot, lag.dev, addr, vid); 628 } 629 630 static int felix_mdb_add(struct dsa_switch *ds, int port, 631 const struct switchdev_obj_port_mdb *mdb, 632 struct dsa_db db) 633 { 634 struct ocelot *ocelot = ds->priv; 635 636 return ocelot_port_mdb_add(ocelot, port, mdb); 637 } 638 639 static int felix_mdb_del(struct dsa_switch *ds, int port, 640 const struct switchdev_obj_port_mdb *mdb, 641 struct dsa_db db) 642 { 643 struct ocelot *ocelot = ds->priv; 644 645 return ocelot_port_mdb_del(ocelot, port, mdb); 646 } 647 648 static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port, 649 u8 state) 650 { 651 struct ocelot *ocelot = ds->priv; 652 653 return ocelot_bridge_stp_state_set(ocelot, port, state); 654 } 655 656 static int felix_pre_bridge_flags(struct dsa_switch *ds, int port, 657 struct switchdev_brport_flags val, 658 struct netlink_ext_ack *extack) 659 { 660 struct ocelot *ocelot = ds->priv; 661 662 return ocelot_port_pre_bridge_flags(ocelot, port, val); 663 } 664 665 static int felix_bridge_flags(struct dsa_switch *ds, int port, 666 struct switchdev_brport_flags val, 667 struct netlink_ext_ack *extack) 668 { 669 struct ocelot *ocelot = ds->priv; 670 671 ocelot_port_bridge_flags(ocelot, port, val); 672 673 return 0; 674 } 675 676 static int felix_bridge_join(struct dsa_switch *ds, int port, 677 struct dsa_bridge bridge, bool *tx_fwd_offload, 678 struct netlink_ext_ack *extack) 679 { 680 struct ocelot *ocelot = ds->priv; 681 682 ocelot_port_bridge_join(ocelot, port, bridge.dev); 683 684 return 0; 685 } 686 687 static void felix_bridge_leave(struct dsa_switch *ds, int port, 688 struct dsa_bridge bridge) 689 { 690 struct ocelot *ocelot = ds->priv; 691 692 ocelot_port_bridge_leave(ocelot, port, bridge.dev); 693 } 694 695 static int felix_lag_join(struct dsa_switch *ds, int port, 696 struct dsa_lag lag, 697 struct netdev_lag_upper_info *info) 698 { 699 struct ocelot *ocelot = ds->priv; 700 701 return ocelot_port_lag_join(ocelot, port, lag.dev, info); 702 } 703 704 static int felix_lag_leave(struct dsa_switch *ds, int port, 705 struct dsa_lag lag) 706 { 707 struct ocelot *ocelot = ds->priv; 708 709 ocelot_port_lag_leave(ocelot, port, lag.dev); 710 711 return 0; 712 } 713 714 static int felix_lag_change(struct dsa_switch *ds, int port) 715 { 716 struct dsa_port *dp = dsa_to_port(ds, port); 717 struct ocelot *ocelot = ds->priv; 718 719 ocelot_port_lag_change(ocelot, port, dp->lag_tx_enabled); 720 721 return 0; 722 } 723 724 static int felix_vlan_prepare(struct dsa_switch *ds, int port, 725 const struct switchdev_obj_port_vlan *vlan, 726 struct netlink_ext_ack *extack) 727 { 728 struct ocelot *ocelot = ds->priv; 729 u16 flags = vlan->flags; 730 731 /* Ocelot switches copy frames as-is to the CPU, so the flags: 732 * egress-untagged or not, pvid or not, make no difference. This 733 * behavior is already better than what DSA just tries to approximate 734 * when it installs the VLAN with the same flags on the CPU port. 735 * Just accept any configuration, and don't let ocelot deny installing 736 * multiple native VLANs on the NPI port, because the switch doesn't 737 * look at the port tag settings towards the NPI interface anyway. 738 */ 739 if (port == ocelot->npi) 740 return 0; 741 742 return ocelot_vlan_prepare(ocelot, port, vlan->vid, 743 flags & BRIDGE_VLAN_INFO_PVID, 744 flags & BRIDGE_VLAN_INFO_UNTAGGED, 745 extack); 746 } 747 748 static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled, 749 struct netlink_ext_ack *extack) 750 { 751 struct ocelot *ocelot = ds->priv; 752 753 return ocelot_port_vlan_filtering(ocelot, port, enabled, extack); 754 } 755 756 static int felix_vlan_add(struct dsa_switch *ds, int port, 757 const struct switchdev_obj_port_vlan *vlan, 758 struct netlink_ext_ack *extack) 759 { 760 struct ocelot *ocelot = ds->priv; 761 u16 flags = vlan->flags; 762 int err; 763 764 err = felix_vlan_prepare(ds, port, vlan, extack); 765 if (err) 766 return err; 767 768 return ocelot_vlan_add(ocelot, port, vlan->vid, 769 flags & BRIDGE_VLAN_INFO_PVID, 770 flags & BRIDGE_VLAN_INFO_UNTAGGED); 771 } 772 773 static int felix_vlan_del(struct dsa_switch *ds, int port, 774 const struct switchdev_obj_port_vlan *vlan) 775 { 776 struct ocelot *ocelot = ds->priv; 777 778 return ocelot_vlan_del(ocelot, port, vlan->vid); 779 } 780 781 static void felix_phylink_get_caps(struct dsa_switch *ds, int port, 782 struct phylink_config *config) 783 { 784 struct ocelot *ocelot = ds->priv; 785 786 /* This driver does not make use of the speed, duplex, pause or the 787 * advertisement in its mac_config, so it is safe to mark this driver 788 * as non-legacy. 789 */ 790 config->legacy_pre_march2020 = false; 791 792 __set_bit(ocelot->ports[port]->phy_mode, 793 config->supported_interfaces); 794 } 795 796 static void felix_phylink_validate(struct dsa_switch *ds, int port, 797 unsigned long *supported, 798 struct phylink_link_state *state) 799 { 800 struct ocelot *ocelot = ds->priv; 801 struct felix *felix = ocelot_to_felix(ocelot); 802 803 if (felix->info->phylink_validate) 804 felix->info->phylink_validate(ocelot, port, supported, state); 805 } 806 807 static struct phylink_pcs *felix_phylink_mac_select_pcs(struct dsa_switch *ds, 808 int port, 809 phy_interface_t iface) 810 { 811 struct ocelot *ocelot = ds->priv; 812 struct felix *felix = ocelot_to_felix(ocelot); 813 struct phylink_pcs *pcs = NULL; 814 815 if (felix->pcs && felix->pcs[port]) 816 pcs = felix->pcs[port]; 817 818 return pcs; 819 } 820 821 static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port, 822 unsigned int link_an_mode, 823 phy_interface_t interface) 824 { 825 struct ocelot *ocelot = ds->priv; 826 827 ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface, 828 FELIX_MAC_QUIRKS); 829 } 830 831 static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port, 832 unsigned int link_an_mode, 833 phy_interface_t interface, 834 struct phy_device *phydev, 835 int speed, int duplex, 836 bool tx_pause, bool rx_pause) 837 { 838 struct ocelot *ocelot = ds->priv; 839 struct felix *felix = ocelot_to_felix(ocelot); 840 841 ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode, 842 interface, speed, duplex, tx_pause, rx_pause, 843 FELIX_MAC_QUIRKS); 844 845 if (felix->info->port_sched_speed_set) 846 felix->info->port_sched_speed_set(ocelot, port, speed); 847 } 848 849 static void felix_port_qos_map_init(struct ocelot *ocelot, int port) 850 { 851 int i; 852 853 ocelot_rmw_gix(ocelot, 854 ANA_PORT_QOS_CFG_QOS_PCP_ENA, 855 ANA_PORT_QOS_CFG_QOS_PCP_ENA, 856 ANA_PORT_QOS_CFG, 857 port); 858 859 for (i = 0; i < OCELOT_NUM_TC * 2; i++) { 860 ocelot_rmw_ix(ocelot, 861 (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) | 862 ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i), 863 ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL | 864 ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M, 865 ANA_PORT_PCP_DEI_MAP, 866 port, i); 867 } 868 } 869 870 static void felix_get_strings(struct dsa_switch *ds, int port, 871 u32 stringset, u8 *data) 872 { 873 struct ocelot *ocelot = ds->priv; 874 875 return ocelot_get_strings(ocelot, port, stringset, data); 876 } 877 878 static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data) 879 { 880 struct ocelot *ocelot = ds->priv; 881 882 ocelot_get_ethtool_stats(ocelot, port, data); 883 } 884 885 static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset) 886 { 887 struct ocelot *ocelot = ds->priv; 888 889 return ocelot_get_sset_count(ocelot, port, sset); 890 } 891 892 static int felix_get_ts_info(struct dsa_switch *ds, int port, 893 struct ethtool_ts_info *info) 894 { 895 struct ocelot *ocelot = ds->priv; 896 897 return ocelot_get_ts_info(ocelot, port, info); 898 } 899 900 static int felix_parse_ports_node(struct felix *felix, 901 struct device_node *ports_node, 902 phy_interface_t *port_phy_modes) 903 { 904 struct ocelot *ocelot = &felix->ocelot; 905 struct device *dev = felix->ocelot.dev; 906 struct device_node *child; 907 908 for_each_available_child_of_node(ports_node, child) { 909 phy_interface_t phy_mode; 910 u32 port; 911 int err; 912 913 /* Get switch port number from DT */ 914 if (of_property_read_u32(child, "reg", &port) < 0) { 915 dev_err(dev, "Port number not defined in device tree " 916 "(property \"reg\")\n"); 917 of_node_put(child); 918 return -ENODEV; 919 } 920 921 /* Get PHY mode from DT */ 922 err = of_get_phy_mode(child, &phy_mode); 923 if (err) { 924 dev_err(dev, "Failed to read phy-mode or " 925 "phy-interface-type property for port %d\n", 926 port); 927 of_node_put(child); 928 return -ENODEV; 929 } 930 931 err = felix->info->prevalidate_phy_mode(ocelot, port, phy_mode); 932 if (err < 0) { 933 dev_err(dev, "Unsupported PHY mode %s on port %d\n", 934 phy_modes(phy_mode), port); 935 of_node_put(child); 936 return err; 937 } 938 939 port_phy_modes[port] = phy_mode; 940 } 941 942 return 0; 943 } 944 945 static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes) 946 { 947 struct device *dev = felix->ocelot.dev; 948 struct device_node *switch_node; 949 struct device_node *ports_node; 950 int err; 951 952 switch_node = dev->of_node; 953 954 ports_node = of_get_child_by_name(switch_node, "ports"); 955 if (!ports_node) 956 ports_node = of_get_child_by_name(switch_node, "ethernet-ports"); 957 if (!ports_node) { 958 dev_err(dev, "Incorrect bindings: absent \"ports\" or \"ethernet-ports\" node\n"); 959 return -ENODEV; 960 } 961 962 err = felix_parse_ports_node(felix, ports_node, port_phy_modes); 963 of_node_put(ports_node); 964 965 return err; 966 } 967 968 static int felix_init_structs(struct felix *felix, int num_phys_ports) 969 { 970 struct ocelot *ocelot = &felix->ocelot; 971 phy_interface_t *port_phy_modes; 972 struct resource res; 973 int port, i, err; 974 975 ocelot->num_phys_ports = num_phys_ports; 976 ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports, 977 sizeof(struct ocelot_port *), GFP_KERNEL); 978 if (!ocelot->ports) 979 return -ENOMEM; 980 981 ocelot->map = felix->info->map; 982 ocelot->stats_layout = felix->info->stats_layout; 983 ocelot->num_stats = felix->info->num_stats; 984 ocelot->num_mact_rows = felix->info->num_mact_rows; 985 ocelot->vcap = felix->info->vcap; 986 ocelot->vcap_pol.base = felix->info->vcap_pol_base; 987 ocelot->vcap_pol.max = felix->info->vcap_pol_max; 988 ocelot->vcap_pol.base2 = felix->info->vcap_pol_base2; 989 ocelot->vcap_pol.max2 = felix->info->vcap_pol_max2; 990 ocelot->ops = felix->info->ops; 991 ocelot->npi_inj_prefix = OCELOT_TAG_PREFIX_SHORT; 992 ocelot->npi_xtr_prefix = OCELOT_TAG_PREFIX_SHORT; 993 ocelot->devlink = felix->ds->devlink; 994 995 port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t), 996 GFP_KERNEL); 997 if (!port_phy_modes) 998 return -ENOMEM; 999 1000 err = felix_parse_dt(felix, port_phy_modes); 1001 if (err) { 1002 kfree(port_phy_modes); 1003 return err; 1004 } 1005 1006 for (i = 0; i < TARGET_MAX; i++) { 1007 struct regmap *target; 1008 1009 if (!felix->info->target_io_res[i].name) 1010 continue; 1011 1012 memcpy(&res, &felix->info->target_io_res[i], sizeof(res)); 1013 res.flags = IORESOURCE_MEM; 1014 res.start += felix->switch_base; 1015 res.end += felix->switch_base; 1016 1017 target = felix->info->init_regmap(ocelot, &res); 1018 if (IS_ERR(target)) { 1019 dev_err(ocelot->dev, 1020 "Failed to map device memory space\n"); 1021 kfree(port_phy_modes); 1022 return PTR_ERR(target); 1023 } 1024 1025 ocelot->targets[i] = target; 1026 } 1027 1028 err = ocelot_regfields_init(ocelot, felix->info->regfields); 1029 if (err) { 1030 dev_err(ocelot->dev, "failed to init reg fields map\n"); 1031 kfree(port_phy_modes); 1032 return err; 1033 } 1034 1035 for (port = 0; port < num_phys_ports; port++) { 1036 struct ocelot_port *ocelot_port; 1037 struct regmap *target; 1038 1039 ocelot_port = devm_kzalloc(ocelot->dev, 1040 sizeof(struct ocelot_port), 1041 GFP_KERNEL); 1042 if (!ocelot_port) { 1043 dev_err(ocelot->dev, 1044 "failed to allocate port memory\n"); 1045 kfree(port_phy_modes); 1046 return -ENOMEM; 1047 } 1048 1049 memcpy(&res, &felix->info->port_io_res[port], sizeof(res)); 1050 res.flags = IORESOURCE_MEM; 1051 res.start += felix->switch_base; 1052 res.end += felix->switch_base; 1053 1054 target = felix->info->init_regmap(ocelot, &res); 1055 if (IS_ERR(target)) { 1056 dev_err(ocelot->dev, 1057 "Failed to map memory space for port %d\n", 1058 port); 1059 kfree(port_phy_modes); 1060 return PTR_ERR(target); 1061 } 1062 1063 ocelot_port->phy_mode = port_phy_modes[port]; 1064 ocelot_port->ocelot = ocelot; 1065 ocelot_port->target = target; 1066 ocelot->ports[port] = ocelot_port; 1067 } 1068 1069 kfree(port_phy_modes); 1070 1071 if (felix->info->mdio_bus_alloc) { 1072 err = felix->info->mdio_bus_alloc(ocelot); 1073 if (err < 0) 1074 return err; 1075 } 1076 1077 return 0; 1078 } 1079 1080 static void ocelot_port_purge_txtstamp_skb(struct ocelot *ocelot, int port, 1081 struct sk_buff *skb) 1082 { 1083 struct ocelot_port *ocelot_port = ocelot->ports[port]; 1084 struct sk_buff *clone = OCELOT_SKB_CB(skb)->clone; 1085 struct sk_buff *skb_match = NULL, *skb_tmp; 1086 unsigned long flags; 1087 1088 if (!clone) 1089 return; 1090 1091 spin_lock_irqsave(&ocelot_port->tx_skbs.lock, flags); 1092 1093 skb_queue_walk_safe(&ocelot_port->tx_skbs, skb, skb_tmp) { 1094 if (skb != clone) 1095 continue; 1096 __skb_unlink(skb, &ocelot_port->tx_skbs); 1097 skb_match = skb; 1098 break; 1099 } 1100 1101 spin_unlock_irqrestore(&ocelot_port->tx_skbs.lock, flags); 1102 1103 WARN_ONCE(!skb_match, 1104 "Could not find skb clone in TX timestamping list\n"); 1105 } 1106 1107 #define work_to_xmit_work(w) \ 1108 container_of((w), struct felix_deferred_xmit_work, work) 1109 1110 static void felix_port_deferred_xmit(struct kthread_work *work) 1111 { 1112 struct felix_deferred_xmit_work *xmit_work = work_to_xmit_work(work); 1113 struct dsa_switch *ds = xmit_work->dp->ds; 1114 struct sk_buff *skb = xmit_work->skb; 1115 u32 rew_op = ocelot_ptp_rew_op(skb); 1116 struct ocelot *ocelot = ds->priv; 1117 int port = xmit_work->dp->index; 1118 int retries = 10; 1119 1120 do { 1121 if (ocelot_can_inject(ocelot, 0)) 1122 break; 1123 1124 cpu_relax(); 1125 } while (--retries); 1126 1127 if (!retries) { 1128 dev_err(ocelot->dev, "port %d failed to inject skb\n", 1129 port); 1130 ocelot_port_purge_txtstamp_skb(ocelot, port, skb); 1131 kfree_skb(skb); 1132 return; 1133 } 1134 1135 ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb); 1136 1137 consume_skb(skb); 1138 kfree(xmit_work); 1139 } 1140 1141 static int felix_connect_tag_protocol(struct dsa_switch *ds, 1142 enum dsa_tag_protocol proto) 1143 { 1144 struct ocelot_8021q_tagger_data *tagger_data; 1145 1146 switch (proto) { 1147 case DSA_TAG_PROTO_OCELOT_8021Q: 1148 tagger_data = ocelot_8021q_tagger_data(ds); 1149 tagger_data->xmit_work_fn = felix_port_deferred_xmit; 1150 return 0; 1151 case DSA_TAG_PROTO_OCELOT: 1152 case DSA_TAG_PROTO_SEVILLE: 1153 return 0; 1154 default: 1155 return -EPROTONOSUPPORT; 1156 } 1157 } 1158 1159 /* Hardware initialization done here so that we can allocate structures with 1160 * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing 1161 * us to allocate structures twice (leak memory) and map PCI memory twice 1162 * (which will not work). 1163 */ 1164 static int felix_setup(struct dsa_switch *ds) 1165 { 1166 struct ocelot *ocelot = ds->priv; 1167 struct felix *felix = ocelot_to_felix(ocelot); 1168 struct dsa_port *dp; 1169 int err; 1170 1171 err = felix_init_structs(felix, ds->num_ports); 1172 if (err) 1173 return err; 1174 1175 err = ocelot_init(ocelot); 1176 if (err) 1177 goto out_mdiobus_free; 1178 1179 if (ocelot->ptp) { 1180 err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps); 1181 if (err) { 1182 dev_err(ocelot->dev, 1183 "Timestamp initialization failed\n"); 1184 ocelot->ptp = 0; 1185 } 1186 } 1187 1188 dsa_switch_for_each_available_port(dp, ds) { 1189 ocelot_init_port(ocelot, dp->index); 1190 1191 /* Set the default QoS Classification based on PCP and DEI 1192 * bits of vlan tag. 1193 */ 1194 felix_port_qos_map_init(ocelot, dp->index); 1195 } 1196 1197 err = ocelot_devlink_sb_register(ocelot); 1198 if (err) 1199 goto out_deinit_ports; 1200 1201 dsa_switch_for_each_cpu_port(dp, ds) { 1202 /* The initial tag protocol is NPI which always returns 0, so 1203 * there's no real point in checking for errors. 1204 */ 1205 felix_set_tag_protocol(ds, dp->index, felix->tag_proto); 1206 break; 1207 } 1208 1209 ds->mtu_enforcement_ingress = true; 1210 ds->assisted_learning_on_cpu_port = true; 1211 1212 return 0; 1213 1214 out_deinit_ports: 1215 dsa_switch_for_each_available_port(dp, ds) 1216 ocelot_deinit_port(ocelot, dp->index); 1217 1218 ocelot_deinit_timestamp(ocelot); 1219 ocelot_deinit(ocelot); 1220 1221 out_mdiobus_free: 1222 if (felix->info->mdio_bus_free) 1223 felix->info->mdio_bus_free(ocelot); 1224 1225 return err; 1226 } 1227 1228 static void felix_teardown(struct dsa_switch *ds) 1229 { 1230 struct ocelot *ocelot = ds->priv; 1231 struct felix *felix = ocelot_to_felix(ocelot); 1232 struct dsa_port *dp; 1233 1234 dsa_switch_for_each_cpu_port(dp, ds) { 1235 felix_del_tag_protocol(ds, dp->index, felix->tag_proto); 1236 break; 1237 } 1238 1239 dsa_switch_for_each_available_port(dp, ds) 1240 ocelot_deinit_port(ocelot, dp->index); 1241 1242 ocelot_devlink_sb_unregister(ocelot); 1243 ocelot_deinit_timestamp(ocelot); 1244 ocelot_deinit(ocelot); 1245 1246 if (felix->info->mdio_bus_free) 1247 felix->info->mdio_bus_free(ocelot); 1248 } 1249 1250 static int felix_hwtstamp_get(struct dsa_switch *ds, int port, 1251 struct ifreq *ifr) 1252 { 1253 struct ocelot *ocelot = ds->priv; 1254 1255 return ocelot_hwstamp_get(ocelot, port, ifr); 1256 } 1257 1258 static int felix_hwtstamp_set(struct dsa_switch *ds, int port, 1259 struct ifreq *ifr) 1260 { 1261 struct ocelot *ocelot = ds->priv; 1262 struct felix *felix = ocelot_to_felix(ocelot); 1263 bool using_tag_8021q; 1264 int err; 1265 1266 err = ocelot_hwstamp_set(ocelot, port, ifr); 1267 if (err) 1268 return err; 1269 1270 using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q; 1271 1272 return felix_update_trapping_destinations(ds, using_tag_8021q); 1273 } 1274 1275 static bool felix_check_xtr_pkt(struct ocelot *ocelot, unsigned int ptp_type) 1276 { 1277 struct felix *felix = ocelot_to_felix(ocelot); 1278 int err, grp = 0; 1279 1280 if (felix->tag_proto != DSA_TAG_PROTO_OCELOT_8021Q) 1281 return false; 1282 1283 if (!felix->info->quirk_no_xtr_irq) 1284 return false; 1285 1286 if (ptp_type == PTP_CLASS_NONE) 1287 return false; 1288 1289 while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) { 1290 struct sk_buff *skb; 1291 unsigned int type; 1292 1293 err = ocelot_xtr_poll_frame(ocelot, grp, &skb); 1294 if (err) 1295 goto out; 1296 1297 /* We trap to the CPU port module all PTP frames, but 1298 * felix_rxtstamp() only gets called for event frames. 1299 * So we need to avoid sending duplicate general 1300 * message frames by running a second BPF classifier 1301 * here and dropping those. 1302 */ 1303 __skb_push(skb, ETH_HLEN); 1304 1305 type = ptp_classify_raw(skb); 1306 1307 __skb_pull(skb, ETH_HLEN); 1308 1309 if (type == PTP_CLASS_NONE) { 1310 kfree_skb(skb); 1311 continue; 1312 } 1313 1314 netif_rx(skb); 1315 } 1316 1317 out: 1318 if (err < 0) 1319 ocelot_drain_cpu_queue(ocelot, 0); 1320 1321 return true; 1322 } 1323 1324 static bool felix_rxtstamp(struct dsa_switch *ds, int port, 1325 struct sk_buff *skb, unsigned int type) 1326 { 1327 u32 tstamp_lo = OCELOT_SKB_CB(skb)->tstamp_lo; 1328 struct skb_shared_hwtstamps *shhwtstamps; 1329 struct ocelot *ocelot = ds->priv; 1330 struct timespec64 ts; 1331 u32 tstamp_hi; 1332 u64 tstamp; 1333 1334 /* If the "no XTR IRQ" workaround is in use, tell DSA to defer this skb 1335 * for RX timestamping. Then free it, and poll for its copy through 1336 * MMIO in the CPU port module, and inject that into the stack from 1337 * ocelot_xtr_poll(). 1338 */ 1339 if (felix_check_xtr_pkt(ocelot, type)) { 1340 kfree_skb(skb); 1341 return true; 1342 } 1343 1344 ocelot_ptp_gettime64(&ocelot->ptp_info, &ts); 1345 tstamp = ktime_set(ts.tv_sec, ts.tv_nsec); 1346 1347 tstamp_hi = tstamp >> 32; 1348 if ((tstamp & 0xffffffff) < tstamp_lo) 1349 tstamp_hi--; 1350 1351 tstamp = ((u64)tstamp_hi << 32) | tstamp_lo; 1352 1353 shhwtstamps = skb_hwtstamps(skb); 1354 memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps)); 1355 shhwtstamps->hwtstamp = tstamp; 1356 return false; 1357 } 1358 1359 static void felix_txtstamp(struct dsa_switch *ds, int port, 1360 struct sk_buff *skb) 1361 { 1362 struct ocelot *ocelot = ds->priv; 1363 struct sk_buff *clone = NULL; 1364 1365 if (!ocelot->ptp) 1366 return; 1367 1368 if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) { 1369 dev_err_ratelimited(ds->dev, 1370 "port %d delivering skb without TX timestamp\n", 1371 port); 1372 return; 1373 } 1374 1375 if (clone) 1376 OCELOT_SKB_CB(skb)->clone = clone; 1377 } 1378 1379 static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu) 1380 { 1381 struct ocelot *ocelot = ds->priv; 1382 1383 ocelot_port_set_maxlen(ocelot, port, new_mtu); 1384 1385 return 0; 1386 } 1387 1388 static int felix_get_max_mtu(struct dsa_switch *ds, int port) 1389 { 1390 struct ocelot *ocelot = ds->priv; 1391 1392 return ocelot_get_max_mtu(ocelot, port); 1393 } 1394 1395 static int felix_cls_flower_add(struct dsa_switch *ds, int port, 1396 struct flow_cls_offload *cls, bool ingress) 1397 { 1398 struct ocelot *ocelot = ds->priv; 1399 struct felix *felix = ocelot_to_felix(ocelot); 1400 bool using_tag_8021q; 1401 int err; 1402 1403 err = ocelot_cls_flower_replace(ocelot, port, cls, ingress); 1404 if (err) 1405 return err; 1406 1407 using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q; 1408 1409 return felix_update_trapping_destinations(ds, using_tag_8021q); 1410 } 1411 1412 static int felix_cls_flower_del(struct dsa_switch *ds, int port, 1413 struct flow_cls_offload *cls, bool ingress) 1414 { 1415 struct ocelot *ocelot = ds->priv; 1416 1417 return ocelot_cls_flower_destroy(ocelot, port, cls, ingress); 1418 } 1419 1420 static int felix_cls_flower_stats(struct dsa_switch *ds, int port, 1421 struct flow_cls_offload *cls, bool ingress) 1422 { 1423 struct ocelot *ocelot = ds->priv; 1424 1425 return ocelot_cls_flower_stats(ocelot, port, cls, ingress); 1426 } 1427 1428 static int felix_port_policer_add(struct dsa_switch *ds, int port, 1429 struct dsa_mall_policer_tc_entry *policer) 1430 { 1431 struct ocelot *ocelot = ds->priv; 1432 struct ocelot_policer pol = { 1433 .rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8, 1434 .burst = policer->burst, 1435 }; 1436 1437 return ocelot_port_policer_add(ocelot, port, &pol); 1438 } 1439 1440 static void felix_port_policer_del(struct dsa_switch *ds, int port) 1441 { 1442 struct ocelot *ocelot = ds->priv; 1443 1444 ocelot_port_policer_del(ocelot, port); 1445 } 1446 1447 static int felix_port_setup_tc(struct dsa_switch *ds, int port, 1448 enum tc_setup_type type, 1449 void *type_data) 1450 { 1451 struct ocelot *ocelot = ds->priv; 1452 struct felix *felix = ocelot_to_felix(ocelot); 1453 1454 if (felix->info->port_setup_tc) 1455 return felix->info->port_setup_tc(ds, port, type, type_data); 1456 else 1457 return -EOPNOTSUPP; 1458 } 1459 1460 static int felix_sb_pool_get(struct dsa_switch *ds, unsigned int sb_index, 1461 u16 pool_index, 1462 struct devlink_sb_pool_info *pool_info) 1463 { 1464 struct ocelot *ocelot = ds->priv; 1465 1466 return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info); 1467 } 1468 1469 static int felix_sb_pool_set(struct dsa_switch *ds, unsigned int sb_index, 1470 u16 pool_index, u32 size, 1471 enum devlink_sb_threshold_type threshold_type, 1472 struct netlink_ext_ack *extack) 1473 { 1474 struct ocelot *ocelot = ds->priv; 1475 1476 return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size, 1477 threshold_type, extack); 1478 } 1479 1480 static int felix_sb_port_pool_get(struct dsa_switch *ds, int port, 1481 unsigned int sb_index, u16 pool_index, 1482 u32 *p_threshold) 1483 { 1484 struct ocelot *ocelot = ds->priv; 1485 1486 return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index, 1487 p_threshold); 1488 } 1489 1490 static int felix_sb_port_pool_set(struct dsa_switch *ds, int port, 1491 unsigned int sb_index, u16 pool_index, 1492 u32 threshold, struct netlink_ext_ack *extack) 1493 { 1494 struct ocelot *ocelot = ds->priv; 1495 1496 return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index, 1497 threshold, extack); 1498 } 1499 1500 static int felix_sb_tc_pool_bind_get(struct dsa_switch *ds, int port, 1501 unsigned int sb_index, u16 tc_index, 1502 enum devlink_sb_pool_type pool_type, 1503 u16 *p_pool_index, u32 *p_threshold) 1504 { 1505 struct ocelot *ocelot = ds->priv; 1506 1507 return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index, 1508 pool_type, p_pool_index, 1509 p_threshold); 1510 } 1511 1512 static int felix_sb_tc_pool_bind_set(struct dsa_switch *ds, int port, 1513 unsigned int sb_index, u16 tc_index, 1514 enum devlink_sb_pool_type pool_type, 1515 u16 pool_index, u32 threshold, 1516 struct netlink_ext_ack *extack) 1517 { 1518 struct ocelot *ocelot = ds->priv; 1519 1520 return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index, 1521 pool_type, pool_index, threshold, 1522 extack); 1523 } 1524 1525 static int felix_sb_occ_snapshot(struct dsa_switch *ds, 1526 unsigned int sb_index) 1527 { 1528 struct ocelot *ocelot = ds->priv; 1529 1530 return ocelot_sb_occ_snapshot(ocelot, sb_index); 1531 } 1532 1533 static int felix_sb_occ_max_clear(struct dsa_switch *ds, 1534 unsigned int sb_index) 1535 { 1536 struct ocelot *ocelot = ds->priv; 1537 1538 return ocelot_sb_occ_max_clear(ocelot, sb_index); 1539 } 1540 1541 static int felix_sb_occ_port_pool_get(struct dsa_switch *ds, int port, 1542 unsigned int sb_index, u16 pool_index, 1543 u32 *p_cur, u32 *p_max) 1544 { 1545 struct ocelot *ocelot = ds->priv; 1546 1547 return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index, 1548 p_cur, p_max); 1549 } 1550 1551 static int felix_sb_occ_tc_port_bind_get(struct dsa_switch *ds, int port, 1552 unsigned int sb_index, u16 tc_index, 1553 enum devlink_sb_pool_type pool_type, 1554 u32 *p_cur, u32 *p_max) 1555 { 1556 struct ocelot *ocelot = ds->priv; 1557 1558 return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index, tc_index, 1559 pool_type, p_cur, p_max); 1560 } 1561 1562 static int felix_mrp_add(struct dsa_switch *ds, int port, 1563 const struct switchdev_obj_mrp *mrp) 1564 { 1565 struct ocelot *ocelot = ds->priv; 1566 1567 return ocelot_mrp_add(ocelot, port, mrp); 1568 } 1569 1570 static int felix_mrp_del(struct dsa_switch *ds, int port, 1571 const struct switchdev_obj_mrp *mrp) 1572 { 1573 struct ocelot *ocelot = ds->priv; 1574 1575 return ocelot_mrp_add(ocelot, port, mrp); 1576 } 1577 1578 static int 1579 felix_mrp_add_ring_role(struct dsa_switch *ds, int port, 1580 const struct switchdev_obj_ring_role_mrp *mrp) 1581 { 1582 struct ocelot *ocelot = ds->priv; 1583 1584 return ocelot_mrp_add_ring_role(ocelot, port, mrp); 1585 } 1586 1587 static int 1588 felix_mrp_del_ring_role(struct dsa_switch *ds, int port, 1589 const struct switchdev_obj_ring_role_mrp *mrp) 1590 { 1591 struct ocelot *ocelot = ds->priv; 1592 1593 return ocelot_mrp_del_ring_role(ocelot, port, mrp); 1594 } 1595 1596 const struct dsa_switch_ops felix_switch_ops = { 1597 .get_tag_protocol = felix_get_tag_protocol, 1598 .change_tag_protocol = felix_change_tag_protocol, 1599 .connect_tag_protocol = felix_connect_tag_protocol, 1600 .setup = felix_setup, 1601 .teardown = felix_teardown, 1602 .set_ageing_time = felix_set_ageing_time, 1603 .get_strings = felix_get_strings, 1604 .get_ethtool_stats = felix_get_ethtool_stats, 1605 .get_sset_count = felix_get_sset_count, 1606 .get_ts_info = felix_get_ts_info, 1607 .phylink_get_caps = felix_phylink_get_caps, 1608 .phylink_validate = felix_phylink_validate, 1609 .phylink_mac_select_pcs = felix_phylink_mac_select_pcs, 1610 .phylink_mac_link_down = felix_phylink_mac_link_down, 1611 .phylink_mac_link_up = felix_phylink_mac_link_up, 1612 .port_fast_age = felix_port_fast_age, 1613 .port_fdb_dump = felix_fdb_dump, 1614 .port_fdb_add = felix_fdb_add, 1615 .port_fdb_del = felix_fdb_del, 1616 .lag_fdb_add = felix_lag_fdb_add, 1617 .lag_fdb_del = felix_lag_fdb_del, 1618 .port_mdb_add = felix_mdb_add, 1619 .port_mdb_del = felix_mdb_del, 1620 .port_pre_bridge_flags = felix_pre_bridge_flags, 1621 .port_bridge_flags = felix_bridge_flags, 1622 .port_bridge_join = felix_bridge_join, 1623 .port_bridge_leave = felix_bridge_leave, 1624 .port_lag_join = felix_lag_join, 1625 .port_lag_leave = felix_lag_leave, 1626 .port_lag_change = felix_lag_change, 1627 .port_stp_state_set = felix_bridge_stp_state_set, 1628 .port_vlan_filtering = felix_vlan_filtering, 1629 .port_vlan_add = felix_vlan_add, 1630 .port_vlan_del = felix_vlan_del, 1631 .port_hwtstamp_get = felix_hwtstamp_get, 1632 .port_hwtstamp_set = felix_hwtstamp_set, 1633 .port_rxtstamp = felix_rxtstamp, 1634 .port_txtstamp = felix_txtstamp, 1635 .port_change_mtu = felix_change_mtu, 1636 .port_max_mtu = felix_get_max_mtu, 1637 .port_policer_add = felix_port_policer_add, 1638 .port_policer_del = felix_port_policer_del, 1639 .cls_flower_add = felix_cls_flower_add, 1640 .cls_flower_del = felix_cls_flower_del, 1641 .cls_flower_stats = felix_cls_flower_stats, 1642 .port_setup_tc = felix_port_setup_tc, 1643 .devlink_sb_pool_get = felix_sb_pool_get, 1644 .devlink_sb_pool_set = felix_sb_pool_set, 1645 .devlink_sb_port_pool_get = felix_sb_port_pool_get, 1646 .devlink_sb_port_pool_set = felix_sb_port_pool_set, 1647 .devlink_sb_tc_pool_bind_get = felix_sb_tc_pool_bind_get, 1648 .devlink_sb_tc_pool_bind_set = felix_sb_tc_pool_bind_set, 1649 .devlink_sb_occ_snapshot = felix_sb_occ_snapshot, 1650 .devlink_sb_occ_max_clear = felix_sb_occ_max_clear, 1651 .devlink_sb_occ_port_pool_get = felix_sb_occ_port_pool_get, 1652 .devlink_sb_occ_tc_port_bind_get= felix_sb_occ_tc_port_bind_get, 1653 .port_mrp_add = felix_mrp_add, 1654 .port_mrp_del = felix_mrp_del, 1655 .port_mrp_add_ring_role = felix_mrp_add_ring_role, 1656 .port_mrp_del_ring_role = felix_mrp_del_ring_role, 1657 .tag_8021q_vlan_add = felix_tag_8021q_vlan_add, 1658 .tag_8021q_vlan_del = felix_tag_8021q_vlan_del, 1659 }; 1660 1661 struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port) 1662 { 1663 struct felix *felix = ocelot_to_felix(ocelot); 1664 struct dsa_switch *ds = felix->ds; 1665 1666 if (!dsa_is_user_port(ds, port)) 1667 return NULL; 1668 1669 return dsa_to_port(ds, port)->slave; 1670 } 1671 1672 int felix_netdev_to_port(struct net_device *dev) 1673 { 1674 struct dsa_port *dp; 1675 1676 dp = dsa_port_from_netdev(dev); 1677 if (IS_ERR(dp)) 1678 return -EINVAL; 1679 1680 return dp->index; 1681 } 1682