| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379 |
- // SPDX-License-Identifier: GPL-2.0
- /* Copyright 2019-2021 NXP
- *
- * This is an umbrella module for all network switches that are
- * register-compatible with Ocelot and that perform I/O to their host CPU
- * through an NPI (Node Processor Interface) Ethernet port.
- */
- #include <uapi/linux/if_bridge.h>
- #include <soc/mscc/ocelot_vcap.h>
- #include <soc/mscc/ocelot_qsys.h>
- #include <soc/mscc/ocelot_sys.h>
- #include <soc/mscc/ocelot_dev.h>
- #include <soc/mscc/ocelot_ana.h>
- #include <soc/mscc/ocelot_ptp.h>
- #include <soc/mscc/ocelot.h>
- #include <linux/dsa/8021q.h>
- #include <linux/dsa/ocelot.h>
- #include <linux/platform_device.h>
- #include <linux/ptp_classify.h>
- #include <linux/module.h>
- #include <linux/of_net.h>
- #include <linux/pci.h>
- #include <linux/of.h>
- #include <net/pkt_sched.h>
- #include <net/dsa.h>
- #include "felix.h"
- /* Translate the DSA database API into the ocelot switch library API,
- * which uses VID 0 for all ports that aren't part of a bridge,
- * and expects the bridge_dev to be NULL in that case.
- */
- static struct net_device *felix_classify_db(struct dsa_db db)
- {
- switch (db.type) {
- case DSA_DB_PORT:
- case DSA_DB_LAG:
- return NULL;
- case DSA_DB_BRIDGE:
- return db.bridge.dev;
- default:
- return ERR_PTR(-EOPNOTSUPP);
- }
- }
- static int felix_cpu_port_for_conduit(struct dsa_switch *ds,
- struct net_device *conduit)
- {
- struct ocelot *ocelot = ds->priv;
- struct dsa_port *cpu_dp;
- int lag;
- if (netif_is_lag_master(conduit)) {
- mutex_lock(&ocelot->fwd_domain_lock);
- lag = ocelot_bond_get_id(ocelot, conduit);
- mutex_unlock(&ocelot->fwd_domain_lock);
- return lag;
- }
- cpu_dp = conduit->dsa_ptr;
- return cpu_dp->index;
- }
- /**
- * felix_update_tag_8021q_rx_rule - Update VCAP ES0 tag_8021q rule after
- * vlan_filtering change
- * @outer_tagging_rule: Pointer to VCAP filter on which the update is performed
- * @vlan_filtering: Current bridge VLAN filtering setting
- *
- * Source port identification for tag_8021q is done using VCAP ES0 rules on the
- * CPU port(s). The ES0 tag B (inner tag from the packet) can be configured as
- * either:
- * - push_inner_tag=0: the inner tag is never pushed into the frame
- * (and we lose info about the classified VLAN). This is
- * good when the classified VLAN is a discardable quantity
- * for the software RX path: it is either set to
- * OCELOT_STANDALONE_PVID, or to
- * ocelot_vlan_unaware_pvid(bridge).
- * - push_inner_tag=1: the inner tag is always pushed. This is good when the
- * classified VLAN is not a discardable quantity (the port
- * is under a VLAN-aware bridge, and software needs to
- * continue processing the packet in the same VLAN as the
- * hardware).
- * The point is that what is good for a VLAN-unaware port is not good for a
- * VLAN-aware port, and vice versa. Thus, the RX tagging rules must be kept in
- * sync with the VLAN filtering state of the port.
- */
- static void
- felix_update_tag_8021q_rx_rule(struct ocelot_vcap_filter *outer_tagging_rule,
- bool vlan_filtering)
- {
- if (vlan_filtering)
- outer_tagging_rule->action.push_inner_tag = OCELOT_ES0_TAG;
- else
- outer_tagging_rule->action.push_inner_tag = OCELOT_NO_ES0_TAG;
- }
- /* Set up VCAP ES0 rules for pushing a tag_8021q VLAN towards the CPU such that
- * the tagger can perform RX source port identification.
- */
- static int felix_tag_8021q_vlan_add_rx(struct dsa_switch *ds, int port,
- int upstream, u16 vid,
- bool vlan_filtering)
- {
- struct ocelot_vcap_filter *outer_tagging_rule;
- struct ocelot *ocelot = ds->priv;
- unsigned long cookie;
- int key_length, err;
- key_length = ocelot->vcap[VCAP_ES0].keys[VCAP_ES0_IGR_PORT].length;
- outer_tagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter),
- GFP_KERNEL);
- if (!outer_tagging_rule)
- return -ENOMEM;
- cookie = OCELOT_VCAP_ES0_TAG_8021Q_RXVLAN(ocelot, port, upstream);
- outer_tagging_rule->key_type = OCELOT_VCAP_KEY_ANY;
- outer_tagging_rule->prio = 1;
- outer_tagging_rule->id.cookie = cookie;
- outer_tagging_rule->id.tc_offload = false;
- outer_tagging_rule->block_id = VCAP_ES0;
- outer_tagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
- outer_tagging_rule->lookup = 0;
- outer_tagging_rule->ingress_port.value = port;
- outer_tagging_rule->ingress_port.mask = GENMASK(key_length - 1, 0);
- outer_tagging_rule->egress_port.value = upstream;
- outer_tagging_rule->egress_port.mask = GENMASK(key_length - 1, 0);
- outer_tagging_rule->action.push_outer_tag = OCELOT_ES0_TAG;
- outer_tagging_rule->action.tag_a_tpid_sel = OCELOT_TAG_TPID_SEL_8021AD;
- outer_tagging_rule->action.tag_a_vid_sel = 1;
- outer_tagging_rule->action.vid_a_val = vid;
- felix_update_tag_8021q_rx_rule(outer_tagging_rule, vlan_filtering);
- outer_tagging_rule->action.tag_b_tpid_sel = OCELOT_TAG_TPID_SEL_8021Q;
- /* Leave TAG_B_VID_SEL at 0 (Classified VID + VID_B_VAL). Since we also
- * leave VID_B_VAL at 0, this makes ES0 tag B (the inner tag) equal to
- * the classified VID, which we need to see in the DSA tagger's receive
- * path. Note: the inner tag is only visible in the packet when pushed
- * (push_inner_tag == OCELOT_ES0_TAG).
- */
- err = ocelot_vcap_filter_add(ocelot, outer_tagging_rule, NULL);
- if (err)
- kfree(outer_tagging_rule);
- return err;
- }
- static int felix_tag_8021q_vlan_del_rx(struct dsa_switch *ds, int port,
- int upstream, u16 vid)
- {
- struct ocelot_vcap_filter *outer_tagging_rule;
- struct ocelot_vcap_block *block_vcap_es0;
- struct ocelot *ocelot = ds->priv;
- unsigned long cookie;
- block_vcap_es0 = &ocelot->block[VCAP_ES0];
- cookie = OCELOT_VCAP_ES0_TAG_8021Q_RXVLAN(ocelot, port, upstream);
- outer_tagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_es0,
- cookie, false);
- if (!outer_tagging_rule)
- return -ENOENT;
- return ocelot_vcap_filter_del(ocelot, outer_tagging_rule);
- }
- /* Set up VCAP IS1 rules for stripping the tag_8021q VLAN on TX and VCAP IS2
- * rules for steering those tagged packets towards the correct destination port
- */
- static int felix_tag_8021q_vlan_add_tx(struct dsa_switch *ds, int port,
- u16 vid)
- {
- struct ocelot_vcap_filter *untagging_rule, *redirect_rule;
- unsigned long cpu_ports = dsa_cpu_ports(ds);
- struct ocelot *ocelot = ds->priv;
- unsigned long cookie;
- int err;
- untagging_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL);
- if (!untagging_rule)
- return -ENOMEM;
- redirect_rule = kzalloc(sizeof(struct ocelot_vcap_filter), GFP_KERNEL);
- if (!redirect_rule) {
- kfree(untagging_rule);
- return -ENOMEM;
- }
- cookie = OCELOT_VCAP_IS1_TAG_8021Q_TXVLAN(ocelot, port);
- untagging_rule->key_type = OCELOT_VCAP_KEY_ANY;
- untagging_rule->ingress_port_mask = cpu_ports;
- untagging_rule->vlan.vid.value = vid;
- untagging_rule->vlan.vid.mask = VLAN_VID_MASK;
- untagging_rule->prio = 1;
- untagging_rule->id.cookie = cookie;
- untagging_rule->id.tc_offload = false;
- untagging_rule->block_id = VCAP_IS1;
- untagging_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
- untagging_rule->lookup = 0;
- untagging_rule->action.vlan_pop_cnt_ena = true;
- untagging_rule->action.vlan_pop_cnt = 1;
- untagging_rule->action.pag_override_mask = 0xff;
- untagging_rule->action.pag_val = port;
- err = ocelot_vcap_filter_add(ocelot, untagging_rule, NULL);
- if (err) {
- kfree(untagging_rule);
- kfree(redirect_rule);
- return err;
- }
- cookie = OCELOT_VCAP_IS2_TAG_8021Q_TXVLAN(ocelot, port);
- redirect_rule->key_type = OCELOT_VCAP_KEY_ANY;
- redirect_rule->ingress_port_mask = cpu_ports;
- redirect_rule->pag = port;
- redirect_rule->prio = 1;
- redirect_rule->id.cookie = cookie;
- redirect_rule->id.tc_offload = false;
- redirect_rule->block_id = VCAP_IS2;
- redirect_rule->type = OCELOT_VCAP_FILTER_OFFLOAD;
- redirect_rule->lookup = 0;
- redirect_rule->action.mask_mode = OCELOT_MASK_MODE_REDIRECT;
- redirect_rule->action.port_mask = BIT(port);
- err = ocelot_vcap_filter_add(ocelot, redirect_rule, NULL);
- if (err) {
- ocelot_vcap_filter_del(ocelot, untagging_rule);
- kfree(redirect_rule);
- return err;
- }
- return 0;
- }
- static int felix_tag_8021q_vlan_del_tx(struct dsa_switch *ds, int port, u16 vid)
- {
- struct ocelot_vcap_filter *untagging_rule, *redirect_rule;
- struct ocelot_vcap_block *block_vcap_is1;
- struct ocelot_vcap_block *block_vcap_is2;
- struct ocelot *ocelot = ds->priv;
- unsigned long cookie;
- int err;
- block_vcap_is1 = &ocelot->block[VCAP_IS1];
- block_vcap_is2 = &ocelot->block[VCAP_IS2];
- cookie = OCELOT_VCAP_IS1_TAG_8021Q_TXVLAN(ocelot, port);
- untagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is1,
- cookie, false);
- if (!untagging_rule)
- return -ENOENT;
- err = ocelot_vcap_filter_del(ocelot, untagging_rule);
- if (err)
- return err;
- cookie = OCELOT_VCAP_IS2_TAG_8021Q_TXVLAN(ocelot, port);
- redirect_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_is2,
- cookie, false);
- if (!redirect_rule)
- return -ENOENT;
- return ocelot_vcap_filter_del(ocelot, redirect_rule);
- }
- static int felix_tag_8021q_vlan_add(struct dsa_switch *ds, int port, u16 vid,
- u16 flags)
- {
- struct dsa_port *dp = dsa_to_port(ds, port);
- struct dsa_port *cpu_dp;
- int err;
- /* tag_8021q.c assumes we are implementing this via port VLAN
- * membership, which we aren't. So we don't need to add any VCAP filter
- * for the CPU port.
- */
- if (!dsa_port_is_user(dp))
- return 0;
- dsa_switch_for_each_cpu_port(cpu_dp, ds) {
- err = felix_tag_8021q_vlan_add_rx(ds, port, cpu_dp->index, vid,
- dsa_port_is_vlan_filtering(dp));
- if (err)
- return err;
- }
- err = felix_tag_8021q_vlan_add_tx(ds, port, vid);
- if (err)
- goto add_tx_failed;
- return 0;
- add_tx_failed:
- dsa_switch_for_each_cpu_port(cpu_dp, ds)
- felix_tag_8021q_vlan_del_rx(ds, port, cpu_dp->index, vid);
- return err;
- }
- static int felix_tag_8021q_vlan_del(struct dsa_switch *ds, int port, u16 vid)
- {
- struct dsa_port *dp = dsa_to_port(ds, port);
- struct dsa_port *cpu_dp;
- int err;
- if (!dsa_port_is_user(dp))
- return 0;
- dsa_switch_for_each_cpu_port(cpu_dp, ds) {
- err = felix_tag_8021q_vlan_del_rx(ds, port, cpu_dp->index, vid);
- if (err)
- return err;
- }
- err = felix_tag_8021q_vlan_del_tx(ds, port, vid);
- if (err)
- goto del_tx_failed;
- return 0;
- del_tx_failed:
- dsa_switch_for_each_cpu_port(cpu_dp, ds)
- felix_tag_8021q_vlan_add_rx(ds, port, cpu_dp->index, vid,
- dsa_port_is_vlan_filtering(dp));
- return err;
- }
- static int felix_update_tag_8021q_rx_rules(struct dsa_switch *ds, int port,
- bool vlan_filtering)
- {
- struct ocelot_vcap_filter *outer_tagging_rule;
- struct ocelot_vcap_block *block_vcap_es0;
- struct ocelot *ocelot = ds->priv;
- struct dsa_port *cpu_dp;
- unsigned long cookie;
- int err;
- block_vcap_es0 = &ocelot->block[VCAP_ES0];
- dsa_switch_for_each_cpu_port(cpu_dp, ds) {
- cookie = OCELOT_VCAP_ES0_TAG_8021Q_RXVLAN(ocelot, port,
- cpu_dp->index);
- outer_tagging_rule = ocelot_vcap_block_find_filter_by_id(block_vcap_es0,
- cookie, false);
- felix_update_tag_8021q_rx_rule(outer_tagging_rule, vlan_filtering);
- err = ocelot_vcap_filter_replace(ocelot, outer_tagging_rule);
- if (err)
- return err;
- }
- return 0;
- }
- static int felix_trap_get_cpu_port(struct dsa_switch *ds,
- const struct ocelot_vcap_filter *trap)
- {
- struct dsa_port *dp;
- int first_port;
- if (WARN_ON(!trap->ingress_port_mask))
- return -1;
- first_port = __ffs(trap->ingress_port_mask);
- dp = dsa_to_port(ds, first_port);
- return dp->cpu_dp->index;
- }
- /* On switches with no extraction IRQ wired, trapped packets need to be
- * replicated over Ethernet as well, otherwise we'd get no notification of
- * their arrival when using the ocelot-8021q tagging protocol.
- */
- static int felix_update_trapping_destinations(struct dsa_switch *ds,
- bool using_tag_8021q)
- {
- struct ocelot *ocelot = ds->priv;
- struct felix *felix = ocelot_to_felix(ocelot);
- struct ocelot_vcap_block *block_vcap_is2;
- struct ocelot_vcap_filter *trap;
- enum ocelot_mask_mode mask_mode;
- unsigned long port_mask;
- bool cpu_copy_ena;
- int err;
- if (!felix->info->quirk_no_xtr_irq)
- return 0;
- /* We are sure that "cpu" was found, otherwise
- * dsa_tree_setup_default_cpu() would have failed earlier.
- */
- block_vcap_is2 = &ocelot->block[VCAP_IS2];
- /* Make sure all traps are set up for that destination */
- list_for_each_entry(trap, &block_vcap_is2->rules, list) {
- if (!trap->is_trap)
- continue;
- /* Figure out the current trapping destination */
- if (using_tag_8021q) {
- /* Redirect to the tag_8021q CPU port. If timestamps
- * are necessary, also copy trapped packets to the CPU
- * port module.
- */
- mask_mode = OCELOT_MASK_MODE_REDIRECT;
- port_mask = BIT(felix_trap_get_cpu_port(ds, trap));
- cpu_copy_ena = !!trap->take_ts;
- } else {
- /* Trap packets only to the CPU port module, which is
- * redirected to the NPI port (the DSA CPU port)
- */
- mask_mode = OCELOT_MASK_MODE_PERMIT_DENY;
- port_mask = 0;
- cpu_copy_ena = true;
- }
- if (trap->action.mask_mode == mask_mode &&
- trap->action.port_mask == port_mask &&
- trap->action.cpu_copy_ena == cpu_copy_ena)
- continue;
- trap->action.mask_mode = mask_mode;
- trap->action.port_mask = port_mask;
- trap->action.cpu_copy_ena = cpu_copy_ena;
- err = ocelot_vcap_filter_replace(ocelot, trap);
- if (err)
- return err;
- }
- return 0;
- }
- /* The CPU port module is connected to the Node Processor Interface (NPI). This
- * is the mode through which frames can be injected from and extracted to an
- * external CPU, over Ethernet. In NXP SoCs, the "external CPU" is the ARM CPU
- * running Linux, and this forms a DSA setup together with the enetc or fman
- * DSA conduit.
- */
- static void felix_npi_port_init(struct ocelot *ocelot, int port)
- {
- ocelot->npi = port;
- ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPUQ_MSK_M |
- QSYS_EXT_CPU_CFG_EXT_CPU_PORT(port),
- QSYS_EXT_CPU_CFG);
- /* NPI port Injection/Extraction configuration */
- ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
- ocelot->npi_xtr_prefix);
- ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
- ocelot->npi_inj_prefix);
- /* Disable transmission of pause frames */
- ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
- }
- static void felix_npi_port_deinit(struct ocelot *ocelot, int port)
- {
- /* Restore hardware defaults */
- int unused_port = ocelot->num_phys_ports + 2;
- ocelot->npi = -1;
- ocelot_write(ocelot, QSYS_EXT_CPU_CFG_EXT_CPU_PORT(unused_port),
- QSYS_EXT_CPU_CFG);
- ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_XTR_HDR,
- OCELOT_TAG_PREFIX_DISABLED);
- ocelot_fields_write(ocelot, port, SYS_PORT_MODE_INCL_INJ_HDR,
- OCELOT_TAG_PREFIX_DISABLED);
- /* Enable transmission of pause frames */
- ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
- }
- static int felix_tag_npi_setup(struct dsa_switch *ds)
- {
- struct dsa_port *dp, *first_cpu_dp = NULL;
- struct ocelot *ocelot = ds->priv;
- dsa_switch_for_each_user_port(dp, ds) {
- if (first_cpu_dp && dp->cpu_dp != first_cpu_dp) {
- dev_err(ds->dev, "Multiple NPI ports not supported\n");
- return -EINVAL;
- }
- first_cpu_dp = dp->cpu_dp;
- }
- if (!first_cpu_dp)
- return -EINVAL;
- felix_npi_port_init(ocelot, first_cpu_dp->index);
- return 0;
- }
- static void felix_tag_npi_teardown(struct dsa_switch *ds)
- {
- struct ocelot *ocelot = ds->priv;
- felix_npi_port_deinit(ocelot, ocelot->npi);
- }
- static unsigned long felix_tag_npi_get_host_fwd_mask(struct dsa_switch *ds)
- {
- struct ocelot *ocelot = ds->priv;
- return BIT(ocelot->num_phys_ports);
- }
- static int felix_tag_npi_change_conduit(struct dsa_switch *ds, int port,
- struct net_device *conduit,
- struct netlink_ext_ack *extack)
- {
- struct dsa_port *dp = dsa_to_port(ds, port), *other_dp;
- struct ocelot *ocelot = ds->priv;
- if (netif_is_lag_master(conduit)) {
- NL_SET_ERR_MSG_MOD(extack,
- "LAG DSA conduit only supported using ocelot-8021q");
- return -EOPNOTSUPP;
- }
- /* Changing the NPI port breaks user ports still assigned to the old
- * one, so only allow it while they're down, and don't allow them to
- * come back up until they're all changed to the new one.
- */
- dsa_switch_for_each_user_port(other_dp, ds) {
- struct net_device *user = other_dp->user;
- if (other_dp != dp && (user->flags & IFF_UP) &&
- dsa_port_to_conduit(other_dp) != conduit) {
- NL_SET_ERR_MSG_MOD(extack,
- "Cannot change while old conduit still has users");
- return -EOPNOTSUPP;
- }
- }
- felix_npi_port_deinit(ocelot, ocelot->npi);
- felix_npi_port_init(ocelot, felix_cpu_port_for_conduit(ds, conduit));
- return 0;
- }
- /* Alternatively to using the NPI functionality, that same hardware MAC
- * connected internally to the enetc or fman DSA conduit can be configured to
- * use the software-defined tag_8021q frame format. As far as the hardware is
- * concerned, it thinks it is a "dumb switch" - the queues of the CPU port
- * module are now disconnected from it, but can still be accessed through
- * register-based MMIO.
- */
- static const struct felix_tag_proto_ops felix_tag_npi_proto_ops = {
- .setup = felix_tag_npi_setup,
- .teardown = felix_tag_npi_teardown,
- .get_host_fwd_mask = felix_tag_npi_get_host_fwd_mask,
- .change_conduit = felix_tag_npi_change_conduit,
- };
- static int felix_tag_8021q_setup(struct dsa_switch *ds)
- {
- struct ocelot *ocelot = ds->priv;
- struct dsa_port *dp;
- int err;
- err = dsa_tag_8021q_register(ds, htons(ETH_P_8021AD));
- if (err)
- return err;
- dsa_switch_for_each_cpu_port(dp, ds)
- ocelot_port_setup_dsa_8021q_cpu(ocelot, dp->index);
- dsa_switch_for_each_user_port(dp, ds)
- ocelot_port_assign_dsa_8021q_cpu(ocelot, dp->index,
- dp->cpu_dp->index);
- dsa_switch_for_each_available_port(dp, ds)
- /* This overwrites ocelot_init():
- * Do not forward BPDU frames to the CPU port module,
- * for 2 reasons:
- * - When these packets are injected from the tag_8021q
- * CPU port, we want them to go out, not loop back
- * into the system.
- * - STP traffic ingressing on a user port should go to
- * the tag_8021q CPU port, not to the hardware CPU
- * port module.
- */
- ocelot_write_gix(ocelot,
- ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0),
- ANA_PORT_CPU_FWD_BPDU_CFG, dp->index);
- /* The ownership of the CPU port module's queues might have just been
- * transferred to the tag_8021q tagger from the NPI-based tagger.
- * So there might still be all sorts of crap in the queues. On the
- * other hand, the MMIO-based matching of PTP frames is very brittle,
- * so we need to be careful that there are no extra frames to be
- * dequeued over MMIO, since we would never know to discard them.
- */
- ocelot_lock_xtr_grp_bh(ocelot, 0);
- ocelot_drain_cpu_queue(ocelot, 0);
- ocelot_unlock_xtr_grp_bh(ocelot, 0);
- /* Problem: when using push_inner_tag=1 for ES0 tag B, we lose info
- * about whether the received packets were VLAN-tagged on the wire,
- * since they are always tagged on egress towards the CPU port.
- *
- * Since using push_inner_tag=1 is unavoidable for VLAN-aware bridges,
- * we must work around the fallout by untagging in software to make
- * untagged reception work more or less as expected.
- */
- ds->untag_vlan_aware_bridge_pvid = true;
- return 0;
- }
- static void felix_tag_8021q_teardown(struct dsa_switch *ds)
- {
- struct ocelot *ocelot = ds->priv;
- struct dsa_port *dp;
- dsa_switch_for_each_available_port(dp, ds)
- /* Restore the logic from ocelot_init:
- * do not forward BPDU frames to the front ports.
- */
- ocelot_write_gix(ocelot,
- ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
- ANA_PORT_CPU_FWD_BPDU_CFG,
- dp->index);
- dsa_switch_for_each_user_port(dp, ds)
- ocelot_port_unassign_dsa_8021q_cpu(ocelot, dp->index);
- dsa_switch_for_each_cpu_port(dp, ds)
- ocelot_port_teardown_dsa_8021q_cpu(ocelot, dp->index);
- dsa_tag_8021q_unregister(ds);
- ds->untag_vlan_aware_bridge_pvid = false;
- }
- static unsigned long felix_tag_8021q_get_host_fwd_mask(struct dsa_switch *ds)
- {
- return dsa_cpu_ports(ds);
- }
- static int felix_tag_8021q_change_conduit(struct dsa_switch *ds, int port,
- struct net_device *conduit,
- struct netlink_ext_ack *extack)
- {
- int cpu = felix_cpu_port_for_conduit(ds, conduit);
- struct ocelot *ocelot = ds->priv;
- ocelot_port_unassign_dsa_8021q_cpu(ocelot, port);
- ocelot_port_assign_dsa_8021q_cpu(ocelot, port, cpu);
- return felix_update_trapping_destinations(ds, true);
- }
- static const struct felix_tag_proto_ops felix_tag_8021q_proto_ops = {
- .setup = felix_tag_8021q_setup,
- .teardown = felix_tag_8021q_teardown,
- .get_host_fwd_mask = felix_tag_8021q_get_host_fwd_mask,
- .change_conduit = felix_tag_8021q_change_conduit,
- };
- static void felix_set_host_flood(struct dsa_switch *ds, unsigned long mask,
- bool uc, bool mc, bool bc)
- {
- struct ocelot *ocelot = ds->priv;
- unsigned long val;
- val = uc ? mask : 0;
- ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_UC);
- val = mc ? mask : 0;
- ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_MC);
- ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_MCIPV4);
- ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_MCIPV6);
- val = bc ? mask : 0;
- ocelot_rmw_rix(ocelot, val, mask, ANA_PGID_PGID, PGID_BC);
- }
- static void
- felix_migrate_host_flood(struct dsa_switch *ds,
- const struct felix_tag_proto_ops *proto_ops,
- const struct felix_tag_proto_ops *old_proto_ops)
- {
- struct ocelot *ocelot = ds->priv;
- struct felix *felix = ocelot_to_felix(ocelot);
- unsigned long mask;
- if (old_proto_ops) {
- mask = old_proto_ops->get_host_fwd_mask(ds);
- felix_set_host_flood(ds, mask, false, false, false);
- }
- mask = proto_ops->get_host_fwd_mask(ds);
- felix_set_host_flood(ds, mask, !!felix->host_flood_uc_mask,
- !!felix->host_flood_mc_mask, true);
- }
- static int felix_migrate_mdbs(struct dsa_switch *ds,
- const struct felix_tag_proto_ops *proto_ops,
- const struct felix_tag_proto_ops *old_proto_ops)
- {
- struct ocelot *ocelot = ds->priv;
- unsigned long from, to;
- if (!old_proto_ops)
- return 0;
- from = old_proto_ops->get_host_fwd_mask(ds);
- to = proto_ops->get_host_fwd_mask(ds);
- return ocelot_migrate_mdbs(ocelot, from, to);
- }
- /* Configure the shared hardware resources for a transition between
- * @old_proto_ops and @proto_ops.
- * Manual migration is needed because as far as DSA is concerned, no change of
- * the CPU port is taking place here, just of the tagging protocol.
- */
- static int
- felix_tag_proto_setup_shared(struct dsa_switch *ds,
- const struct felix_tag_proto_ops *proto_ops,
- const struct felix_tag_proto_ops *old_proto_ops)
- {
- bool using_tag_8021q = (proto_ops == &felix_tag_8021q_proto_ops);
- int err;
- err = felix_migrate_mdbs(ds, proto_ops, old_proto_ops);
- if (err)
- return err;
- felix_update_trapping_destinations(ds, using_tag_8021q);
- felix_migrate_host_flood(ds, proto_ops, old_proto_ops);
- return 0;
- }
- /* This always leaves the switch in a consistent state, because although the
- * tag_8021q setup can fail, the NPI setup can't. So either the change is made,
- * or the restoration is guaranteed to work.
- */
- static int felix_change_tag_protocol(struct dsa_switch *ds,
- enum dsa_tag_protocol proto)
- {
- const struct felix_tag_proto_ops *old_proto_ops, *proto_ops;
- struct ocelot *ocelot = ds->priv;
- struct felix *felix = ocelot_to_felix(ocelot);
- int err;
- switch (proto) {
- case DSA_TAG_PROTO_SEVILLE:
- case DSA_TAG_PROTO_OCELOT:
- proto_ops = &felix_tag_npi_proto_ops;
- break;
- case DSA_TAG_PROTO_OCELOT_8021Q:
- proto_ops = &felix_tag_8021q_proto_ops;
- break;
- default:
- return -EPROTONOSUPPORT;
- }
- old_proto_ops = felix->tag_proto_ops;
- if (proto_ops == old_proto_ops)
- return 0;
- err = proto_ops->setup(ds);
- if (err)
- goto setup_failed;
- err = felix_tag_proto_setup_shared(ds, proto_ops, old_proto_ops);
- if (err)
- goto setup_shared_failed;
- if (old_proto_ops)
- old_proto_ops->teardown(ds);
- felix->tag_proto_ops = proto_ops;
- felix->tag_proto = proto;
- return 0;
- setup_shared_failed:
- proto_ops->teardown(ds);
- setup_failed:
- return err;
- }
- static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds,
- int port,
- enum dsa_tag_protocol mp)
- {
- struct ocelot *ocelot = ds->priv;
- struct felix *felix = ocelot_to_felix(ocelot);
- return felix->tag_proto;
- }
- static void felix_port_set_host_flood(struct dsa_switch *ds, int port,
- bool uc, bool mc)
- {
- struct ocelot *ocelot = ds->priv;
- struct felix *felix = ocelot_to_felix(ocelot);
- unsigned long mask;
- if (uc)
- felix->host_flood_uc_mask |= BIT(port);
- else
- felix->host_flood_uc_mask &= ~BIT(port);
- if (mc)
- felix->host_flood_mc_mask |= BIT(port);
- else
- felix->host_flood_mc_mask &= ~BIT(port);
- mask = felix->tag_proto_ops->get_host_fwd_mask(ds);
- felix_set_host_flood(ds, mask, !!felix->host_flood_uc_mask,
- !!felix->host_flood_mc_mask, true);
- }
- static int felix_port_change_conduit(struct dsa_switch *ds, int port,
- struct net_device *conduit,
- struct netlink_ext_ack *extack)
- {
- struct ocelot *ocelot = ds->priv;
- struct felix *felix = ocelot_to_felix(ocelot);
- return felix->tag_proto_ops->change_conduit(ds, port, conduit, extack);
- }
- static int felix_set_ageing_time(struct dsa_switch *ds,
- unsigned int ageing_time)
- {
- struct ocelot *ocelot = ds->priv;
- ocelot_set_ageing_time(ocelot, ageing_time);
- return 0;
- }
- static void felix_port_fast_age(struct dsa_switch *ds, int port)
- {
- struct ocelot *ocelot = ds->priv;
- int err;
- err = ocelot_mact_flush(ocelot, port);
- if (err)
- dev_err(ds->dev, "Flushing MAC table on port %d returned %pe\n",
- port, ERR_PTR(err));
- }
- static int felix_fdb_dump(struct dsa_switch *ds, int port,
- dsa_fdb_dump_cb_t *cb, void *data)
- {
- struct ocelot *ocelot = ds->priv;
- return ocelot_fdb_dump(ocelot, port, cb, data);
- }
- static int felix_fdb_add(struct dsa_switch *ds, int port,
- const unsigned char *addr, u16 vid,
- struct dsa_db db)
- {
- struct net_device *bridge_dev = felix_classify_db(db);
- struct dsa_port *dp = dsa_to_port(ds, port);
- struct ocelot *ocelot = ds->priv;
- if (IS_ERR(bridge_dev))
- return PTR_ERR(bridge_dev);
- if (dsa_port_is_cpu(dp) && !bridge_dev &&
- dsa_fdb_present_in_other_db(ds, port, addr, vid, db))
- return 0;
- if (dsa_port_is_cpu(dp))
- port = PGID_CPU;
- return ocelot_fdb_add(ocelot, port, addr, vid, bridge_dev);
- }
- static int felix_fdb_del(struct dsa_switch *ds, int port,
- const unsigned char *addr, u16 vid,
- struct dsa_db db)
- {
- struct net_device *bridge_dev = felix_classify_db(db);
- struct dsa_port *dp = dsa_to_port(ds, port);
- struct ocelot *ocelot = ds->priv;
- if (IS_ERR(bridge_dev))
- return PTR_ERR(bridge_dev);
- if (dsa_port_is_cpu(dp) && !bridge_dev &&
- dsa_fdb_present_in_other_db(ds, port, addr, vid, db))
- return 0;
- if (dsa_port_is_cpu(dp))
- port = PGID_CPU;
- return ocelot_fdb_del(ocelot, port, addr, vid, bridge_dev);
- }
- static int felix_lag_fdb_add(struct dsa_switch *ds, struct dsa_lag lag,
- const unsigned char *addr, u16 vid,
- struct dsa_db db)
- {
- struct net_device *bridge_dev = felix_classify_db(db);
- struct ocelot *ocelot = ds->priv;
- if (IS_ERR(bridge_dev))
- return PTR_ERR(bridge_dev);
- return ocelot_lag_fdb_add(ocelot, lag.dev, addr, vid, bridge_dev);
- }
- static int felix_lag_fdb_del(struct dsa_switch *ds, struct dsa_lag lag,
- const unsigned char *addr, u16 vid,
- struct dsa_db db)
- {
- struct net_device *bridge_dev = felix_classify_db(db);
- struct ocelot *ocelot = ds->priv;
- if (IS_ERR(bridge_dev))
- return PTR_ERR(bridge_dev);
- return ocelot_lag_fdb_del(ocelot, lag.dev, addr, vid, bridge_dev);
- }
- static int felix_mdb_add(struct dsa_switch *ds, int port,
- const struct switchdev_obj_port_mdb *mdb,
- struct dsa_db db)
- {
- struct net_device *bridge_dev = felix_classify_db(db);
- struct ocelot *ocelot = ds->priv;
- if (IS_ERR(bridge_dev))
- return PTR_ERR(bridge_dev);
- if (dsa_is_cpu_port(ds, port) && !bridge_dev &&
- dsa_mdb_present_in_other_db(ds, port, mdb, db))
- return 0;
- if (port == ocelot->npi)
- port = ocelot->num_phys_ports;
- return ocelot_port_mdb_add(ocelot, port, mdb, bridge_dev);
- }
- static int felix_mdb_del(struct dsa_switch *ds, int port,
- const struct switchdev_obj_port_mdb *mdb,
- struct dsa_db db)
- {
- struct net_device *bridge_dev = felix_classify_db(db);
- struct ocelot *ocelot = ds->priv;
- if (IS_ERR(bridge_dev))
- return PTR_ERR(bridge_dev);
- if (dsa_is_cpu_port(ds, port) && !bridge_dev &&
- dsa_mdb_present_in_other_db(ds, port, mdb, db))
- return 0;
- if (port == ocelot->npi)
- port = ocelot->num_phys_ports;
- return ocelot_port_mdb_del(ocelot, port, mdb, bridge_dev);
- }
- static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port,
- u8 state)
- {
- struct ocelot *ocelot = ds->priv;
- return ocelot_bridge_stp_state_set(ocelot, port, state);
- }
- static int felix_pre_bridge_flags(struct dsa_switch *ds, int port,
- struct switchdev_brport_flags val,
- struct netlink_ext_ack *extack)
- {
- struct ocelot *ocelot = ds->priv;
- return ocelot_port_pre_bridge_flags(ocelot, port, val);
- }
- static int felix_bridge_flags(struct dsa_switch *ds, int port,
- struct switchdev_brport_flags val,
- struct netlink_ext_ack *extack)
- {
- struct ocelot *ocelot = ds->priv;
- if (port == ocelot->npi)
- port = ocelot->num_phys_ports;
- ocelot_port_bridge_flags(ocelot, port, val);
- return 0;
- }
- static int felix_bridge_join(struct dsa_switch *ds, int port,
- struct dsa_bridge bridge, bool *tx_fwd_offload,
- struct netlink_ext_ack *extack)
- {
- struct ocelot *ocelot = ds->priv;
- return ocelot_port_bridge_join(ocelot, port, bridge.dev, bridge.num,
- extack);
- }
- static void felix_bridge_leave(struct dsa_switch *ds, int port,
- struct dsa_bridge bridge)
- {
- struct ocelot *ocelot = ds->priv;
- ocelot_port_bridge_leave(ocelot, port, bridge.dev);
- }
- static int felix_lag_join(struct dsa_switch *ds, int port,
- struct dsa_lag lag,
- struct netdev_lag_upper_info *info,
- struct netlink_ext_ack *extack)
- {
- struct ocelot *ocelot = ds->priv;
- int err;
- err = ocelot_port_lag_join(ocelot, port, lag.dev, info, extack);
- if (err)
- return err;
- /* Update the logical LAG port that serves as tag_8021q CPU port */
- if (!dsa_is_cpu_port(ds, port))
- return 0;
- return felix_port_change_conduit(ds, port, lag.dev, extack);
- }
- static int felix_lag_leave(struct dsa_switch *ds, int port,
- struct dsa_lag lag)
- {
- struct ocelot *ocelot = ds->priv;
- ocelot_port_lag_leave(ocelot, port, lag.dev);
- /* Update the logical LAG port that serves as tag_8021q CPU port */
- if (!dsa_is_cpu_port(ds, port))
- return 0;
- return felix_port_change_conduit(ds, port, lag.dev, NULL);
- }
- static int felix_lag_change(struct dsa_switch *ds, int port)
- {
- struct dsa_port *dp = dsa_to_port(ds, port);
- struct ocelot *ocelot = ds->priv;
- ocelot_port_lag_change(ocelot, port, dp->lag_tx_enabled);
- return 0;
- }
- static int felix_vlan_prepare(struct dsa_switch *ds, int port,
- const struct switchdev_obj_port_vlan *vlan,
- struct netlink_ext_ack *extack)
- {
- struct ocelot *ocelot = ds->priv;
- u16 flags = vlan->flags;
- /* Ocelot switches copy frames as-is to the CPU, so the flags:
- * egress-untagged or not, pvid or not, make no difference. This
- * behavior is already better than what DSA just tries to approximate
- * when it installs the VLAN with the same flags on the CPU port.
- * Just accept any configuration, and don't let ocelot deny installing
- * multiple native VLANs on the NPI port, because the switch doesn't
- * look at the port tag settings towards the NPI interface anyway.
- */
- if (port == ocelot->npi)
- return 0;
- return ocelot_vlan_prepare(ocelot, port, vlan->vid,
- flags & BRIDGE_VLAN_INFO_PVID,
- flags & BRIDGE_VLAN_INFO_UNTAGGED,
- extack);
- }
- static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled,
- struct netlink_ext_ack *extack)
- {
- struct ocelot *ocelot = ds->priv;
- bool using_tag_8021q;
- struct felix *felix;
- int err;
- err = ocelot_port_vlan_filtering(ocelot, port, enabled, extack);
- if (err)
- return err;
- felix = ocelot_to_felix(ocelot);
- using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
- if (using_tag_8021q) {
- err = felix_update_tag_8021q_rx_rules(ds, port, enabled);
- if (err)
- return err;
- }
- return 0;
- }
- static int felix_vlan_add(struct dsa_switch *ds, int port,
- const struct switchdev_obj_port_vlan *vlan,
- struct netlink_ext_ack *extack)
- {
- struct ocelot *ocelot = ds->priv;
- u16 flags = vlan->flags;
- int err;
- err = felix_vlan_prepare(ds, port, vlan, extack);
- if (err)
- return err;
- return ocelot_vlan_add(ocelot, port, vlan->vid,
- flags & BRIDGE_VLAN_INFO_PVID,
- flags & BRIDGE_VLAN_INFO_UNTAGGED);
- }
- static int felix_vlan_del(struct dsa_switch *ds, int port,
- const struct switchdev_obj_port_vlan *vlan)
- {
- struct ocelot *ocelot = ds->priv;
- return ocelot_vlan_del(ocelot, port, vlan->vid);
- }
- static void felix_phylink_get_caps(struct dsa_switch *ds, int port,
- struct phylink_config *config)
- {
- struct ocelot *ocelot = ds->priv;
- config->mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
- MAC_10 | MAC_100 | MAC_1000FD |
- MAC_2500FD;
- __set_bit(ocelot->ports[port]->phy_mode,
- config->supported_interfaces);
- }
- static void felix_phylink_mac_config(struct phylink_config *config,
- unsigned int mode,
- const struct phylink_link_state *state)
- {
- struct dsa_port *dp = dsa_phylink_to_port(config);
- struct ocelot *ocelot = dp->ds->priv;
- int port = dp->index;
- struct felix *felix;
- felix = ocelot_to_felix(ocelot);
- if (felix->info->phylink_mac_config)
- felix->info->phylink_mac_config(ocelot, port, mode, state);
- }
- static struct phylink_pcs *
- felix_phylink_mac_select_pcs(struct phylink_config *config,
- phy_interface_t iface)
- {
- struct dsa_port *dp = dsa_phylink_to_port(config);
- struct ocelot *ocelot = dp->ds->priv;
- struct phylink_pcs *pcs = NULL;
- int port = dp->index;
- struct felix *felix;
- felix = ocelot_to_felix(ocelot);
- if (felix->pcs && felix->pcs[port])
- pcs = felix->pcs[port];
- return pcs;
- }
- static void felix_phylink_mac_link_down(struct phylink_config *config,
- unsigned int link_an_mode,
- phy_interface_t interface)
- {
- struct dsa_port *dp = dsa_phylink_to_port(config);
- struct ocelot *ocelot = dp->ds->priv;
- int port = dp->index;
- struct felix *felix;
- felix = ocelot_to_felix(ocelot);
- ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface,
- felix->info->quirks);
- }
- static void felix_phylink_mac_link_up(struct phylink_config *config,
- struct phy_device *phydev,
- unsigned int link_an_mode,
- phy_interface_t interface,
- int speed, int duplex,
- bool tx_pause, bool rx_pause)
- {
- struct dsa_port *dp = dsa_phylink_to_port(config);
- struct ocelot *ocelot = dp->ds->priv;
- int port = dp->index;
- struct felix *felix;
- felix = ocelot_to_felix(ocelot);
- ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode,
- interface, speed, duplex, tx_pause, rx_pause,
- felix->info->quirks);
- if (felix->info->port_sched_speed_set)
- felix->info->port_sched_speed_set(ocelot, port, speed);
- }
- static int felix_port_enable(struct dsa_switch *ds, int port,
- struct phy_device *phydev)
- {
- struct dsa_port *dp = dsa_to_port(ds, port);
- struct ocelot *ocelot = ds->priv;
- if (!dsa_port_is_user(dp))
- return 0;
- if (ocelot->npi >= 0) {
- struct net_device *conduit = dsa_port_to_conduit(dp);
- if (felix_cpu_port_for_conduit(ds, conduit) != ocelot->npi) {
- dev_err(ds->dev, "Multiple conduits are not allowed\n");
- return -EINVAL;
- }
- }
- return 0;
- }
- static void felix_port_qos_map_init(struct ocelot *ocelot, int port)
- {
- int i;
- ocelot_rmw_gix(ocelot,
- ANA_PORT_QOS_CFG_QOS_PCP_ENA,
- ANA_PORT_QOS_CFG_QOS_PCP_ENA,
- ANA_PORT_QOS_CFG,
- port);
- for (i = 0; i < OCELOT_NUM_TC * 2; i++) {
- ocelot_rmw_ix(ocelot,
- (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) |
- ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i),
- ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL |
- ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M,
- ANA_PORT_PCP_DEI_MAP,
- port, i);
- }
- }
- static void felix_get_stats64(struct dsa_switch *ds, int port,
- struct rtnl_link_stats64 *stats)
- {
- struct ocelot *ocelot = ds->priv;
- ocelot_port_get_stats64(ocelot, port, stats);
- }
- static void felix_get_pause_stats(struct dsa_switch *ds, int port,
- struct ethtool_pause_stats *pause_stats)
- {
- struct ocelot *ocelot = ds->priv;
- ocelot_port_get_pause_stats(ocelot, port, pause_stats);
- }
- static void felix_get_rmon_stats(struct dsa_switch *ds, int port,
- struct ethtool_rmon_stats *rmon_stats,
- const struct ethtool_rmon_hist_range **ranges)
- {
- struct ocelot *ocelot = ds->priv;
- ocelot_port_get_rmon_stats(ocelot, port, rmon_stats, ranges);
- }
- static void felix_get_eth_ctrl_stats(struct dsa_switch *ds, int port,
- struct ethtool_eth_ctrl_stats *ctrl_stats)
- {
- struct ocelot *ocelot = ds->priv;
- ocelot_port_get_eth_ctrl_stats(ocelot, port, ctrl_stats);
- }
- static void felix_get_eth_mac_stats(struct dsa_switch *ds, int port,
- struct ethtool_eth_mac_stats *mac_stats)
- {
- struct ocelot *ocelot = ds->priv;
- ocelot_port_get_eth_mac_stats(ocelot, port, mac_stats);
- }
- static void felix_get_eth_phy_stats(struct dsa_switch *ds, int port,
- struct ethtool_eth_phy_stats *phy_stats)
- {
- struct ocelot *ocelot = ds->priv;
- ocelot_port_get_eth_phy_stats(ocelot, port, phy_stats);
- }
- static void felix_get_strings(struct dsa_switch *ds, int port,
- u32 stringset, u8 *data)
- {
- struct ocelot *ocelot = ds->priv;
- return ocelot_get_strings(ocelot, port, stringset, data);
- }
- static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data)
- {
- struct ocelot *ocelot = ds->priv;
- ocelot_get_ethtool_stats(ocelot, port, data);
- }
- static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset)
- {
- struct ocelot *ocelot = ds->priv;
- return ocelot_get_sset_count(ocelot, port, sset);
- }
- static int felix_get_ts_info(struct dsa_switch *ds, int port,
- struct kernel_ethtool_ts_info *info)
- {
- struct ocelot *ocelot = ds->priv;
- return ocelot_get_ts_info(ocelot, port, info);
- }
- static const u32 felix_phy_match_table[PHY_INTERFACE_MODE_MAX] = {
- [PHY_INTERFACE_MODE_INTERNAL] = OCELOT_PORT_MODE_INTERNAL,
- [PHY_INTERFACE_MODE_SGMII] = OCELOT_PORT_MODE_SGMII,
- [PHY_INTERFACE_MODE_QSGMII] = OCELOT_PORT_MODE_QSGMII,
- [PHY_INTERFACE_MODE_USXGMII] = OCELOT_PORT_MODE_USXGMII,
- [PHY_INTERFACE_MODE_1000BASEX] = OCELOT_PORT_MODE_1000BASEX,
- [PHY_INTERFACE_MODE_2500BASEX] = OCELOT_PORT_MODE_2500BASEX,
- };
- static int felix_validate_phy_mode(struct felix *felix, int port,
- phy_interface_t phy_mode)
- {
- u32 modes = felix->info->port_modes[port];
- if (felix_phy_match_table[phy_mode] & modes)
- return 0;
- return -EOPNOTSUPP;
- }
- static int felix_parse_ports_node(struct felix *felix,
- struct device_node *ports_node,
- phy_interface_t *port_phy_modes)
- {
- struct device *dev = felix->ocelot.dev;
- for_each_available_child_of_node_scoped(ports_node, child) {
- phy_interface_t phy_mode;
- u32 port;
- int err;
- /* Get switch port number from DT */
- if (of_property_read_u32(child, "reg", &port) < 0) {
- dev_err(dev, "Port number not defined in device tree "
- "(property \"reg\")\n");
- return -ENODEV;
- }
- /* Get PHY mode from DT */
- err = of_get_phy_mode(child, &phy_mode);
- if (err) {
- dev_err(dev, "Failed to read phy-mode or "
- "phy-interface-type property for port %d\n",
- port);
- return -ENODEV;
- }
- err = felix_validate_phy_mode(felix, port, phy_mode);
- if (err < 0) {
- dev_info(dev, "Unsupported PHY mode %s on port %d\n",
- phy_modes(phy_mode), port);
- /* Leave port_phy_modes[port] = 0, which is also
- * PHY_INTERFACE_MODE_NA. This will perform a
- * best-effort to bring up as many ports as possible.
- */
- continue;
- }
- port_phy_modes[port] = phy_mode;
- }
- return 0;
- }
- static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes)
- {
- struct device *dev = felix->ocelot.dev;
- struct device_node *switch_node;
- struct device_node *ports_node;
- int err;
- switch_node = dev->of_node;
- ports_node = of_get_child_by_name(switch_node, "ports");
- if (!ports_node)
- ports_node = of_get_child_by_name(switch_node, "ethernet-ports");
- if (!ports_node) {
- dev_err(dev, "Incorrect bindings: absent \"ports\" or \"ethernet-ports\" node\n");
- return -ENODEV;
- }
- err = felix_parse_ports_node(felix, ports_node, port_phy_modes);
- of_node_put(ports_node);
- return err;
- }
- static struct regmap *felix_request_regmap_by_name(struct felix *felix,
- const char *resource_name)
- {
- struct ocelot *ocelot = &felix->ocelot;
- struct resource res;
- int i;
- /* In an MFD configuration, regmaps are registered directly to the
- * parent device before the child devices are probed, so there is no
- * need to initialize a new one.
- */
- if (!felix->info->resources)
- return dev_get_regmap(ocelot->dev->parent, resource_name);
- for (i = 0; i < felix->info->num_resources; i++) {
- if (strcmp(resource_name, felix->info->resources[i].name))
- continue;
- memcpy(&res, &felix->info->resources[i], sizeof(res));
- res.start += felix->switch_base;
- res.end += felix->switch_base;
- return ocelot_regmap_init(ocelot, &res);
- }
- return ERR_PTR(-ENOENT);
- }
- static struct regmap *felix_request_regmap(struct felix *felix,
- enum ocelot_target target)
- {
- const char *resource_name = felix->info->resource_names[target];
- /* If the driver didn't provide a resource name for the target,
- * the resource is optional.
- */
- if (!resource_name)
- return NULL;
- return felix_request_regmap_by_name(felix, resource_name);
- }
- static struct regmap *felix_request_port_regmap(struct felix *felix, int port)
- {
- char resource_name[32];
- sprintf(resource_name, "port%d", port);
- return felix_request_regmap_by_name(felix, resource_name);
- }
- static int felix_init_structs(struct felix *felix, int num_phys_ports)
- {
- struct ocelot *ocelot = &felix->ocelot;
- phy_interface_t *port_phy_modes;
- struct regmap *target;
- int port, i, err;
- ocelot->num_phys_ports = num_phys_ports;
- ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports,
- sizeof(struct ocelot_port *), GFP_KERNEL);
- if (!ocelot->ports)
- return -ENOMEM;
- ocelot->map = felix->info->map;
- ocelot->num_mact_rows = felix->info->num_mact_rows;
- ocelot->vcap = felix->info->vcap;
- ocelot->vcap_pol.base = felix->info->vcap_pol_base;
- ocelot->vcap_pol.max = felix->info->vcap_pol_max;
- ocelot->vcap_pol.base2 = felix->info->vcap_pol_base2;
- ocelot->vcap_pol.max2 = felix->info->vcap_pol_max2;
- ocelot->ops = felix->info->ops;
- ocelot->npi_inj_prefix = OCELOT_TAG_PREFIX_SHORT;
- ocelot->npi_xtr_prefix = OCELOT_TAG_PREFIX_SHORT;
- ocelot->devlink = felix->ds->devlink;
- port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t),
- GFP_KERNEL);
- if (!port_phy_modes)
- return -ENOMEM;
- err = felix_parse_dt(felix, port_phy_modes);
- if (err) {
- kfree(port_phy_modes);
- return err;
- }
- for (i = 0; i < TARGET_MAX; i++) {
- target = felix_request_regmap(felix, i);
- if (IS_ERR(target)) {
- dev_err(ocelot->dev,
- "Failed to map device memory space: %pe\n",
- target);
- kfree(port_phy_modes);
- return PTR_ERR(target);
- }
- ocelot->targets[i] = target;
- }
- err = ocelot_regfields_init(ocelot, felix->info->regfields);
- if (err) {
- dev_err(ocelot->dev, "failed to init reg fields map\n");
- kfree(port_phy_modes);
- return err;
- }
- for (port = 0; port < num_phys_ports; port++) {
- struct ocelot_port *ocelot_port;
- ocelot_port = devm_kzalloc(ocelot->dev,
- sizeof(struct ocelot_port),
- GFP_KERNEL);
- if (!ocelot_port) {
- dev_err(ocelot->dev,
- "failed to allocate port memory\n");
- kfree(port_phy_modes);
- return -ENOMEM;
- }
- target = felix_request_port_regmap(felix, port);
- if (IS_ERR(target)) {
- dev_err(ocelot->dev,
- "Failed to map memory space for port %d: %pe\n",
- port, target);
- kfree(port_phy_modes);
- return PTR_ERR(target);
- }
- ocelot_port->phy_mode = port_phy_modes[port];
- ocelot_port->ocelot = ocelot;
- ocelot_port->target = target;
- ocelot_port->index = port;
- ocelot->ports[port] = ocelot_port;
- }
- kfree(port_phy_modes);
- if (felix->info->mdio_bus_alloc) {
- err = felix->info->mdio_bus_alloc(ocelot);
- if (err < 0)
- return err;
- }
- return 0;
- }
- static void ocelot_port_purge_txtstamp_skb(struct ocelot *ocelot, int port,
- struct sk_buff *skb)
- {
- struct ocelot_port *ocelot_port = ocelot->ports[port];
- struct sk_buff *clone = OCELOT_SKB_CB(skb)->clone;
- struct sk_buff *skb_match = NULL, *skb_tmp;
- unsigned long flags;
- if (!clone)
- return;
- spin_lock_irqsave(&ocelot_port->tx_skbs.lock, flags);
- skb_queue_walk_safe(&ocelot_port->tx_skbs, skb, skb_tmp) {
- if (skb != clone)
- continue;
- __skb_unlink(skb, &ocelot_port->tx_skbs);
- skb_match = skb;
- break;
- }
- spin_unlock_irqrestore(&ocelot_port->tx_skbs.lock, flags);
- WARN_ONCE(!skb_match,
- "Could not find skb clone in TX timestamping list\n");
- }
- #define work_to_xmit_work(w) \
- container_of((w), struct felix_deferred_xmit_work, work)
- static void felix_port_deferred_xmit(struct kthread_work *work)
- {
- struct felix_deferred_xmit_work *xmit_work = work_to_xmit_work(work);
- struct dsa_switch *ds = xmit_work->dp->ds;
- struct sk_buff *skb = xmit_work->skb;
- u32 rew_op = ocelot_ptp_rew_op(skb);
- struct ocelot *ocelot = ds->priv;
- int port = xmit_work->dp->index;
- int retries = 10;
- ocelot_lock_inj_grp(ocelot, 0);
- do {
- if (ocelot_can_inject(ocelot, 0))
- break;
- cpu_relax();
- } while (--retries);
- if (!retries) {
- ocelot_unlock_inj_grp(ocelot, 0);
- dev_err(ocelot->dev, "port %d failed to inject skb\n",
- port);
- ocelot_port_purge_txtstamp_skb(ocelot, port, skb);
- kfree_skb(skb);
- return;
- }
- ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
- ocelot_unlock_inj_grp(ocelot, 0);
- consume_skb(skb);
- kfree(xmit_work);
- }
- static int felix_connect_tag_protocol(struct dsa_switch *ds,
- enum dsa_tag_protocol proto)
- {
- struct ocelot_8021q_tagger_data *tagger_data;
- switch (proto) {
- case DSA_TAG_PROTO_OCELOT_8021Q:
- tagger_data = ocelot_8021q_tagger_data(ds);
- tagger_data->xmit_work_fn = felix_port_deferred_xmit;
- return 0;
- case DSA_TAG_PROTO_OCELOT:
- case DSA_TAG_PROTO_SEVILLE:
- return 0;
- default:
- return -EPROTONOSUPPORT;
- }
- }
- static int felix_setup(struct dsa_switch *ds)
- {
- struct ocelot *ocelot = ds->priv;
- struct felix *felix = ocelot_to_felix(ocelot);
- struct dsa_port *dp;
- int err;
- err = felix_init_structs(felix, ds->num_ports);
- if (err)
- return err;
- if (ocelot->targets[HSIO])
- ocelot_pll5_init(ocelot);
- err = ocelot_init(ocelot);
- if (err)
- goto out_mdiobus_free;
- if (ocelot->ptp) {
- err = ocelot_init_timestamp(ocelot, felix->info->ptp_caps);
- if (err) {
- dev_err(ocelot->dev,
- "Timestamp initialization failed\n");
- ocelot->ptp = 0;
- }
- }
- dsa_switch_for_each_available_port(dp, ds) {
- ocelot_init_port(ocelot, dp->index);
- if (felix->info->configure_serdes)
- felix->info->configure_serdes(ocelot, dp->index,
- dp->dn);
- /* Set the default QoS Classification based on PCP and DEI
- * bits of vlan tag.
- */
- felix_port_qos_map_init(ocelot, dp->index);
- }
- if (felix->info->request_irq) {
- err = felix->info->request_irq(ocelot);
- if (err) {
- dev_err(ocelot->dev, "Failed to request IRQ: %pe\n",
- ERR_PTR(err));
- goto out_deinit_ports;
- }
- }
- err = ocelot_devlink_sb_register(ocelot);
- if (err)
- goto out_deinit_ports;
- /* The initial tag protocol is NPI which won't fail during initial
- * setup, there's no real point in checking for errors.
- */
- felix_change_tag_protocol(ds, felix->tag_proto);
- ds->mtu_enforcement_ingress = true;
- ds->assisted_learning_on_cpu_port = true;
- ds->fdb_isolation = true;
- ds->max_num_bridges = ds->num_ports;
- return 0;
- out_deinit_ports:
- dsa_switch_for_each_available_port(dp, ds)
- ocelot_deinit_port(ocelot, dp->index);
- ocelot_deinit_timestamp(ocelot);
- ocelot_deinit(ocelot);
- out_mdiobus_free:
- if (felix->info->mdio_bus_free)
- felix->info->mdio_bus_free(ocelot);
- return err;
- }
- static void felix_teardown(struct dsa_switch *ds)
- {
- struct ocelot *ocelot = ds->priv;
- struct felix *felix = ocelot_to_felix(ocelot);
- struct dsa_port *dp;
- rtnl_lock();
- if (felix->tag_proto_ops)
- felix->tag_proto_ops->teardown(ds);
- rtnl_unlock();
- dsa_switch_for_each_available_port(dp, ds)
- ocelot_deinit_port(ocelot, dp->index);
- ocelot_devlink_sb_unregister(ocelot);
- ocelot_deinit_timestamp(ocelot);
- ocelot_deinit(ocelot);
- if (felix->info->mdio_bus_free)
- felix->info->mdio_bus_free(ocelot);
- }
- static int felix_hwtstamp_get(struct dsa_switch *ds, int port,
- struct ifreq *ifr)
- {
- struct ocelot *ocelot = ds->priv;
- return ocelot_hwstamp_get(ocelot, port, ifr);
- }
- static int felix_hwtstamp_set(struct dsa_switch *ds, int port,
- struct ifreq *ifr)
- {
- struct ocelot *ocelot = ds->priv;
- struct felix *felix = ocelot_to_felix(ocelot);
- bool using_tag_8021q;
- int err;
- err = ocelot_hwstamp_set(ocelot, port, ifr);
- if (err)
- return err;
- using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
- return felix_update_trapping_destinations(ds, using_tag_8021q);
- }
- static bool felix_check_xtr_pkt(struct ocelot *ocelot)
- {
- struct felix *felix = ocelot_to_felix(ocelot);
- int err = 0, grp = 0;
- if (felix->tag_proto != DSA_TAG_PROTO_OCELOT_8021Q)
- return false;
- if (!felix->info->quirk_no_xtr_irq)
- return false;
- ocelot_lock_xtr_grp(ocelot, grp);
- while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) {
- struct sk_buff *skb;
- unsigned int type;
- err = ocelot_xtr_poll_frame(ocelot, grp, &skb);
- if (err)
- goto out;
- /* We trap to the CPU port module all PTP frames, but
- * felix_rxtstamp() only gets called for event frames.
- * So we need to avoid sending duplicate general
- * message frames by running a second BPF classifier
- * here and dropping those.
- */
- __skb_push(skb, ETH_HLEN);
- type = ptp_classify_raw(skb);
- __skb_pull(skb, ETH_HLEN);
- if (type == PTP_CLASS_NONE) {
- kfree_skb(skb);
- continue;
- }
- netif_rx(skb);
- }
- out:
- if (err < 0) {
- dev_err_ratelimited(ocelot->dev,
- "Error during packet extraction: %pe\n",
- ERR_PTR(err));
- ocelot_drain_cpu_queue(ocelot, 0);
- }
- ocelot_unlock_xtr_grp(ocelot, grp);
- return true;
- }
- static bool felix_rxtstamp(struct dsa_switch *ds, int port,
- struct sk_buff *skb, unsigned int type)
- {
- u32 tstamp_lo = OCELOT_SKB_CB(skb)->tstamp_lo;
- struct skb_shared_hwtstamps *shhwtstamps;
- struct ocelot *ocelot = ds->priv;
- struct timespec64 ts;
- u32 tstamp_hi;
- u64 tstamp;
- switch (type & PTP_CLASS_PMASK) {
- case PTP_CLASS_L2:
- if (!(ocelot->ports[port]->trap_proto & OCELOT_PROTO_PTP_L2))
- return false;
- break;
- case PTP_CLASS_IPV4:
- case PTP_CLASS_IPV6:
- if (!(ocelot->ports[port]->trap_proto & OCELOT_PROTO_PTP_L4))
- return false;
- break;
- }
- /* If the "no XTR IRQ" workaround is in use, tell DSA to defer this skb
- * for RX timestamping. Then free it, and poll for its copy through
- * MMIO in the CPU port module, and inject that into the stack from
- * ocelot_xtr_poll().
- */
- if (felix_check_xtr_pkt(ocelot)) {
- kfree_skb(skb);
- return true;
- }
- ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
- tstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
- tstamp_hi = tstamp >> 32;
- if ((tstamp & 0xffffffff) < tstamp_lo)
- tstamp_hi--;
- tstamp = ((u64)tstamp_hi << 32) | tstamp_lo;
- shhwtstamps = skb_hwtstamps(skb);
- memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
- shhwtstamps->hwtstamp = tstamp;
- return false;
- }
- static void felix_txtstamp(struct dsa_switch *ds, int port,
- struct sk_buff *skb)
- {
- struct ocelot *ocelot = ds->priv;
- struct sk_buff *clone = NULL;
- if (!ocelot->ptp)
- return;
- if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) {
- dev_err_ratelimited(ds->dev,
- "port %d delivering skb without TX timestamp\n",
- port);
- return;
- }
- if (clone)
- OCELOT_SKB_CB(skb)->clone = clone;
- }
- static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
- {
- struct ocelot *ocelot = ds->priv;
- struct ocelot_port *ocelot_port = ocelot->ports[port];
- ocelot_port_set_maxlen(ocelot, port, new_mtu);
- mutex_lock(&ocelot->fwd_domain_lock);
- if (ocelot_port->taprio && ocelot->ops->tas_guard_bands_update)
- ocelot->ops->tas_guard_bands_update(ocelot, port);
- mutex_unlock(&ocelot->fwd_domain_lock);
- return 0;
- }
- static int felix_get_max_mtu(struct dsa_switch *ds, int port)
- {
- struct ocelot *ocelot = ds->priv;
- return ocelot_get_max_mtu(ocelot, port);
- }
- static int felix_cls_flower_add(struct dsa_switch *ds, int port,
- struct flow_cls_offload *cls, bool ingress)
- {
- struct ocelot *ocelot = ds->priv;
- struct felix *felix = ocelot_to_felix(ocelot);
- bool using_tag_8021q;
- int err;
- err = ocelot_cls_flower_replace(ocelot, port, cls, ingress);
- if (err)
- return err;
- using_tag_8021q = felix->tag_proto == DSA_TAG_PROTO_OCELOT_8021Q;
- return felix_update_trapping_destinations(ds, using_tag_8021q);
- }
- static int felix_cls_flower_del(struct dsa_switch *ds, int port,
- struct flow_cls_offload *cls, bool ingress)
- {
- struct ocelot *ocelot = ds->priv;
- return ocelot_cls_flower_destroy(ocelot, port, cls, ingress);
- }
- static int felix_cls_flower_stats(struct dsa_switch *ds, int port,
- struct flow_cls_offload *cls, bool ingress)
- {
- struct ocelot *ocelot = ds->priv;
- return ocelot_cls_flower_stats(ocelot, port, cls, ingress);
- }
- static int felix_port_policer_add(struct dsa_switch *ds, int port,
- struct dsa_mall_policer_tc_entry *policer)
- {
- struct ocelot *ocelot = ds->priv;
- struct ocelot_policer pol = {
- .rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8,
- .burst = policer->burst,
- };
- return ocelot_port_policer_add(ocelot, port, &pol);
- }
- static void felix_port_policer_del(struct dsa_switch *ds, int port)
- {
- struct ocelot *ocelot = ds->priv;
- ocelot_port_policer_del(ocelot, port);
- }
- static int felix_port_mirror_add(struct dsa_switch *ds, int port,
- struct dsa_mall_mirror_tc_entry *mirror,
- bool ingress, struct netlink_ext_ack *extack)
- {
- struct ocelot *ocelot = ds->priv;
- return ocelot_port_mirror_add(ocelot, port, mirror->to_local_port,
- ingress, extack);
- }
- static void felix_port_mirror_del(struct dsa_switch *ds, int port,
- struct dsa_mall_mirror_tc_entry *mirror)
- {
- struct ocelot *ocelot = ds->priv;
- ocelot_port_mirror_del(ocelot, port, mirror->ingress);
- }
- static int felix_port_setup_tc(struct dsa_switch *ds, int port,
- enum tc_setup_type type,
- void *type_data)
- {
- struct ocelot *ocelot = ds->priv;
- struct felix *felix = ocelot_to_felix(ocelot);
- if (felix->info->port_setup_tc)
- return felix->info->port_setup_tc(ds, port, type, type_data);
- else
- return -EOPNOTSUPP;
- }
- static int felix_sb_pool_get(struct dsa_switch *ds, unsigned int sb_index,
- u16 pool_index,
- struct devlink_sb_pool_info *pool_info)
- {
- struct ocelot *ocelot = ds->priv;
- return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info);
- }
- static int felix_sb_pool_set(struct dsa_switch *ds, unsigned int sb_index,
- u16 pool_index, u32 size,
- enum devlink_sb_threshold_type threshold_type,
- struct netlink_ext_ack *extack)
- {
- struct ocelot *ocelot = ds->priv;
- return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size,
- threshold_type, extack);
- }
- static int felix_sb_port_pool_get(struct dsa_switch *ds, int port,
- unsigned int sb_index, u16 pool_index,
- u32 *p_threshold)
- {
- struct ocelot *ocelot = ds->priv;
- return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index,
- p_threshold);
- }
- static int felix_sb_port_pool_set(struct dsa_switch *ds, int port,
- unsigned int sb_index, u16 pool_index,
- u32 threshold, struct netlink_ext_ack *extack)
- {
- struct ocelot *ocelot = ds->priv;
- return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index,
- threshold, extack);
- }
- static int felix_sb_tc_pool_bind_get(struct dsa_switch *ds, int port,
- unsigned int sb_index, u16 tc_index,
- enum devlink_sb_pool_type pool_type,
- u16 *p_pool_index, u32 *p_threshold)
- {
- struct ocelot *ocelot = ds->priv;
- return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index,
- pool_type, p_pool_index,
- p_threshold);
- }
- static int felix_sb_tc_pool_bind_set(struct dsa_switch *ds, int port,
- unsigned int sb_index, u16 tc_index,
- enum devlink_sb_pool_type pool_type,
- u16 pool_index, u32 threshold,
- struct netlink_ext_ack *extack)
- {
- struct ocelot *ocelot = ds->priv;
- return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index,
- pool_type, pool_index, threshold,
- extack);
- }
- static int felix_sb_occ_snapshot(struct dsa_switch *ds,
- unsigned int sb_index)
- {
- struct ocelot *ocelot = ds->priv;
- return ocelot_sb_occ_snapshot(ocelot, sb_index);
- }
- static int felix_sb_occ_max_clear(struct dsa_switch *ds,
- unsigned int sb_index)
- {
- struct ocelot *ocelot = ds->priv;
- return ocelot_sb_occ_max_clear(ocelot, sb_index);
- }
- static int felix_sb_occ_port_pool_get(struct dsa_switch *ds, int port,
- unsigned int sb_index, u16 pool_index,
- u32 *p_cur, u32 *p_max)
- {
- struct ocelot *ocelot = ds->priv;
- return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index,
- p_cur, p_max);
- }
- static int felix_sb_occ_tc_port_bind_get(struct dsa_switch *ds, int port,
- unsigned int sb_index, u16 tc_index,
- enum devlink_sb_pool_type pool_type,
- u32 *p_cur, u32 *p_max)
- {
- struct ocelot *ocelot = ds->priv;
- return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index, tc_index,
- pool_type, p_cur, p_max);
- }
- static int felix_mrp_add(struct dsa_switch *ds, int port,
- const struct switchdev_obj_mrp *mrp)
- {
- struct ocelot *ocelot = ds->priv;
- return ocelot_mrp_add(ocelot, port, mrp);
- }
- static int felix_mrp_del(struct dsa_switch *ds, int port,
- const struct switchdev_obj_mrp *mrp)
- {
- struct ocelot *ocelot = ds->priv;
- return ocelot_mrp_add(ocelot, port, mrp);
- }
- static int
- felix_mrp_add_ring_role(struct dsa_switch *ds, int port,
- const struct switchdev_obj_ring_role_mrp *mrp)
- {
- struct ocelot *ocelot = ds->priv;
- return ocelot_mrp_add_ring_role(ocelot, port, mrp);
- }
- static int
- felix_mrp_del_ring_role(struct dsa_switch *ds, int port,
- const struct switchdev_obj_ring_role_mrp *mrp)
- {
- struct ocelot *ocelot = ds->priv;
- return ocelot_mrp_del_ring_role(ocelot, port, mrp);
- }
- static int felix_port_get_default_prio(struct dsa_switch *ds, int port)
- {
- struct ocelot *ocelot = ds->priv;
- return ocelot_port_get_default_prio(ocelot, port);
- }
- static int felix_port_set_default_prio(struct dsa_switch *ds, int port,
- u8 prio)
- {
- struct ocelot *ocelot = ds->priv;
- return ocelot_port_set_default_prio(ocelot, port, prio);
- }
- static int felix_port_get_dscp_prio(struct dsa_switch *ds, int port, u8 dscp)
- {
- struct ocelot *ocelot = ds->priv;
- return ocelot_port_get_dscp_prio(ocelot, port, dscp);
- }
- static int felix_port_add_dscp_prio(struct dsa_switch *ds, int port, u8 dscp,
- u8 prio)
- {
- struct ocelot *ocelot = ds->priv;
- return ocelot_port_add_dscp_prio(ocelot, port, dscp, prio);
- }
- static int felix_port_del_dscp_prio(struct dsa_switch *ds, int port, u8 dscp,
- u8 prio)
- {
- struct ocelot *ocelot = ds->priv;
- return ocelot_port_del_dscp_prio(ocelot, port, dscp, prio);
- }
- static int felix_get_mm(struct dsa_switch *ds, int port,
- struct ethtool_mm_state *state)
- {
- struct ocelot *ocelot = ds->priv;
- return ocelot_port_get_mm(ocelot, port, state);
- }
- static int felix_set_mm(struct dsa_switch *ds, int port,
- struct ethtool_mm_cfg *cfg,
- struct netlink_ext_ack *extack)
- {
- struct ocelot *ocelot = ds->priv;
- return ocelot_port_set_mm(ocelot, port, cfg, extack);
- }
- static void felix_get_mm_stats(struct dsa_switch *ds, int port,
- struct ethtool_mm_stats *stats)
- {
- struct ocelot *ocelot = ds->priv;
- ocelot_port_get_mm_stats(ocelot, port, stats);
- }
- static const struct phylink_mac_ops felix_phylink_mac_ops = {
- .mac_select_pcs = felix_phylink_mac_select_pcs,
- .mac_config = felix_phylink_mac_config,
- .mac_link_down = felix_phylink_mac_link_down,
- .mac_link_up = felix_phylink_mac_link_up,
- };
- static const struct dsa_switch_ops felix_switch_ops = {
- .get_tag_protocol = felix_get_tag_protocol,
- .change_tag_protocol = felix_change_tag_protocol,
- .connect_tag_protocol = felix_connect_tag_protocol,
- .setup = felix_setup,
- .teardown = felix_teardown,
- .set_ageing_time = felix_set_ageing_time,
- .get_mm = felix_get_mm,
- .set_mm = felix_set_mm,
- .get_mm_stats = felix_get_mm_stats,
- .get_stats64 = felix_get_stats64,
- .get_pause_stats = felix_get_pause_stats,
- .get_rmon_stats = felix_get_rmon_stats,
- .get_eth_ctrl_stats = felix_get_eth_ctrl_stats,
- .get_eth_mac_stats = felix_get_eth_mac_stats,
- .get_eth_phy_stats = felix_get_eth_phy_stats,
- .get_strings = felix_get_strings,
- .get_ethtool_stats = felix_get_ethtool_stats,
- .get_sset_count = felix_get_sset_count,
- .get_ts_info = felix_get_ts_info,
- .phylink_get_caps = felix_phylink_get_caps,
- .port_enable = felix_port_enable,
- .port_fast_age = felix_port_fast_age,
- .port_fdb_dump = felix_fdb_dump,
- .port_fdb_add = felix_fdb_add,
- .port_fdb_del = felix_fdb_del,
- .lag_fdb_add = felix_lag_fdb_add,
- .lag_fdb_del = felix_lag_fdb_del,
- .port_mdb_add = felix_mdb_add,
- .port_mdb_del = felix_mdb_del,
- .port_pre_bridge_flags = felix_pre_bridge_flags,
- .port_bridge_flags = felix_bridge_flags,
- .port_bridge_join = felix_bridge_join,
- .port_bridge_leave = felix_bridge_leave,
- .port_lag_join = felix_lag_join,
- .port_lag_leave = felix_lag_leave,
- .port_lag_change = felix_lag_change,
- .port_stp_state_set = felix_bridge_stp_state_set,
- .port_vlan_filtering = felix_vlan_filtering,
- .port_vlan_add = felix_vlan_add,
- .port_vlan_del = felix_vlan_del,
- .port_hwtstamp_get = felix_hwtstamp_get,
- .port_hwtstamp_set = felix_hwtstamp_set,
- .port_rxtstamp = felix_rxtstamp,
- .port_txtstamp = felix_txtstamp,
- .port_change_mtu = felix_change_mtu,
- .port_max_mtu = felix_get_max_mtu,
- .port_policer_add = felix_port_policer_add,
- .port_policer_del = felix_port_policer_del,
- .port_mirror_add = felix_port_mirror_add,
- .port_mirror_del = felix_port_mirror_del,
- .cls_flower_add = felix_cls_flower_add,
- .cls_flower_del = felix_cls_flower_del,
- .cls_flower_stats = felix_cls_flower_stats,
- .port_setup_tc = felix_port_setup_tc,
- .devlink_sb_pool_get = felix_sb_pool_get,
- .devlink_sb_pool_set = felix_sb_pool_set,
- .devlink_sb_port_pool_get = felix_sb_port_pool_get,
- .devlink_sb_port_pool_set = felix_sb_port_pool_set,
- .devlink_sb_tc_pool_bind_get = felix_sb_tc_pool_bind_get,
- .devlink_sb_tc_pool_bind_set = felix_sb_tc_pool_bind_set,
- .devlink_sb_occ_snapshot = felix_sb_occ_snapshot,
- .devlink_sb_occ_max_clear = felix_sb_occ_max_clear,
- .devlink_sb_occ_port_pool_get = felix_sb_occ_port_pool_get,
- .devlink_sb_occ_tc_port_bind_get= felix_sb_occ_tc_port_bind_get,
- .port_mrp_add = felix_mrp_add,
- .port_mrp_del = felix_mrp_del,
- .port_mrp_add_ring_role = felix_mrp_add_ring_role,
- .port_mrp_del_ring_role = felix_mrp_del_ring_role,
- .tag_8021q_vlan_add = felix_tag_8021q_vlan_add,
- .tag_8021q_vlan_del = felix_tag_8021q_vlan_del,
- .port_get_default_prio = felix_port_get_default_prio,
- .port_set_default_prio = felix_port_set_default_prio,
- .port_get_dscp_prio = felix_port_get_dscp_prio,
- .port_add_dscp_prio = felix_port_add_dscp_prio,
- .port_del_dscp_prio = felix_port_del_dscp_prio,
- .port_set_host_flood = felix_port_set_host_flood,
- .port_change_conduit = felix_port_change_conduit,
- };
- int felix_register_switch(struct device *dev, resource_size_t switch_base,
- int num_flooding_pgids, bool ptp,
- bool mm_supported,
- enum dsa_tag_protocol init_tag_proto,
- const struct felix_info *info)
- {
- struct dsa_switch *ds;
- struct ocelot *ocelot;
- struct felix *felix;
- int err;
- felix = devm_kzalloc(dev, sizeof(*felix), GFP_KERNEL);
- if (!felix)
- return -ENOMEM;
- ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
- if (!ds)
- return -ENOMEM;
- dev_set_drvdata(dev, felix);
- ocelot = &felix->ocelot;
- ocelot->dev = dev;
- ocelot->num_flooding_pgids = num_flooding_pgids;
- ocelot->ptp = ptp;
- ocelot->mm_supported = mm_supported;
- felix->info = info;
- felix->switch_base = switch_base;
- felix->ds = ds;
- felix->tag_proto = init_tag_proto;
- ds->dev = dev;
- ds->num_ports = info->num_ports;
- ds->num_tx_queues = OCELOT_NUM_TC;
- ds->ops = &felix_switch_ops;
- ds->phylink_mac_ops = &felix_phylink_mac_ops;
- ds->priv = ocelot;
- err = dsa_register_switch(ds);
- if (err)
- dev_err_probe(dev, err, "Failed to register DSA switch\n");
- return err;
- }
- EXPORT_SYMBOL_GPL(felix_register_switch);
- struct net_device *felix_port_to_netdev(struct ocelot *ocelot, int port)
- {
- struct felix *felix = ocelot_to_felix(ocelot);
- struct dsa_switch *ds = felix->ds;
- if (!dsa_is_user_port(ds, port))
- return NULL;
- return dsa_to_port(ds, port)->user;
- }
- EXPORT_SYMBOL_GPL(felix_port_to_netdev);
- int felix_netdev_to_port(struct net_device *dev)
- {
- struct dsa_port *dp;
- dp = dsa_port_from_netdev(dev);
- if (IS_ERR(dp))
- return -EINVAL;
- return dp->index;
- }
- EXPORT_SYMBOL_GPL(felix_netdev_to_port);
- MODULE_DESCRIPTION("Felix DSA library");
- MODULE_LICENSE("GPL");
|