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