ocelot.c 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331
  1. // SPDX-License-Identifier: (GPL-2.0 OR MIT)
  2. /*
  3. * Microsemi Ocelot Switch driver
  4. *
  5. * Copyright (c) 2017 Microsemi Corporation
  6. */
  7. #include <linux/dsa/ocelot.h>
  8. #include <linux/if_bridge.h>
  9. #include <linux/iopoll.h>
  10. #include <linux/phy/phy.h>
  11. #include <net/pkt_sched.h>
  12. #include <soc/mscc/ocelot_hsio.h>
  13. #include <soc/mscc/ocelot_vcap.h>
  14. #include "ocelot.h"
  15. #include "ocelot_vcap.h"
  16. #define TABLE_UPDATE_SLEEP_US 10
  17. #define TABLE_UPDATE_TIMEOUT_US 100000
  18. #define MEM_INIT_SLEEP_US 1000
  19. #define MEM_INIT_TIMEOUT_US 100000
  20. #define OCELOT_RSV_VLAN_RANGE_START 4000
  21. struct ocelot_mact_entry {
  22. u8 mac[ETH_ALEN];
  23. u16 vid;
  24. enum macaccess_entry_type type;
  25. };
  26. /* Caller must hold &ocelot->mact_lock */
  27. static inline u32 ocelot_mact_read_macaccess(struct ocelot *ocelot)
  28. {
  29. return ocelot_read(ocelot, ANA_TABLES_MACACCESS);
  30. }
  31. /* Caller must hold &ocelot->mact_lock */
  32. static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
  33. {
  34. u32 val;
  35. return readx_poll_timeout(ocelot_mact_read_macaccess,
  36. ocelot, val,
  37. (val & ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M) ==
  38. MACACCESS_CMD_IDLE,
  39. TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
  40. }
  41. /* Caller must hold &ocelot->mact_lock */
  42. static void ocelot_mact_select(struct ocelot *ocelot,
  43. const unsigned char mac[ETH_ALEN],
  44. unsigned int vid)
  45. {
  46. u32 macl = 0, mach = 0;
  47. /* Set the MAC address to handle and the vlan associated in a format
  48. * understood by the hardware.
  49. */
  50. mach |= vid << 16;
  51. mach |= mac[0] << 8;
  52. mach |= mac[1] << 0;
  53. macl |= mac[2] << 24;
  54. macl |= mac[3] << 16;
  55. macl |= mac[4] << 8;
  56. macl |= mac[5] << 0;
  57. ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA);
  58. ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA);
  59. }
  60. static int __ocelot_mact_learn(struct ocelot *ocelot, int port,
  61. const unsigned char mac[ETH_ALEN],
  62. unsigned int vid, enum macaccess_entry_type type)
  63. {
  64. u32 cmd = ANA_TABLES_MACACCESS_VALID |
  65. ANA_TABLES_MACACCESS_DEST_IDX(port) |
  66. ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
  67. ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN);
  68. unsigned int mc_ports;
  69. int err;
  70. /* Set MAC_CPU_COPY if the CPU port is used by a multicast entry */
  71. if (type == ENTRYTYPE_MACv4)
  72. mc_ports = (mac[1] << 8) | mac[2];
  73. else if (type == ENTRYTYPE_MACv6)
  74. mc_ports = (mac[0] << 8) | mac[1];
  75. else
  76. mc_ports = 0;
  77. if (mc_ports & BIT(ocelot->num_phys_ports))
  78. cmd |= ANA_TABLES_MACACCESS_MAC_CPU_COPY;
  79. ocelot_mact_select(ocelot, mac, vid);
  80. /* Issue a write command */
  81. ocelot_write(ocelot, cmd, ANA_TABLES_MACACCESS);
  82. err = ocelot_mact_wait_for_completion(ocelot);
  83. return err;
  84. }
  85. int ocelot_mact_learn(struct ocelot *ocelot, int port,
  86. const unsigned char mac[ETH_ALEN],
  87. unsigned int vid, enum macaccess_entry_type type)
  88. {
  89. int ret;
  90. mutex_lock(&ocelot->mact_lock);
  91. ret = __ocelot_mact_learn(ocelot, port, mac, vid, type);
  92. mutex_unlock(&ocelot->mact_lock);
  93. return ret;
  94. }
  95. EXPORT_SYMBOL(ocelot_mact_learn);
  96. int ocelot_mact_forget(struct ocelot *ocelot,
  97. const unsigned char mac[ETH_ALEN], unsigned int vid)
  98. {
  99. int err;
  100. mutex_lock(&ocelot->mact_lock);
  101. ocelot_mact_select(ocelot, mac, vid);
  102. /* Issue a forget command */
  103. ocelot_write(ocelot,
  104. ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
  105. ANA_TABLES_MACACCESS);
  106. err = ocelot_mact_wait_for_completion(ocelot);
  107. mutex_unlock(&ocelot->mact_lock);
  108. return err;
  109. }
  110. EXPORT_SYMBOL(ocelot_mact_forget);
  111. int ocelot_mact_lookup(struct ocelot *ocelot, int *dst_idx,
  112. const unsigned char mac[ETH_ALEN],
  113. unsigned int vid, enum macaccess_entry_type *type)
  114. {
  115. int val;
  116. mutex_lock(&ocelot->mact_lock);
  117. ocelot_mact_select(ocelot, mac, vid);
  118. /* Issue a read command with MACACCESS_VALID=1. */
  119. ocelot_write(ocelot, ANA_TABLES_MACACCESS_VALID |
  120. ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
  121. ANA_TABLES_MACACCESS);
  122. if (ocelot_mact_wait_for_completion(ocelot)) {
  123. mutex_unlock(&ocelot->mact_lock);
  124. return -ETIMEDOUT;
  125. }
  126. /* Read back the entry flags */
  127. val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
  128. mutex_unlock(&ocelot->mact_lock);
  129. if (!(val & ANA_TABLES_MACACCESS_VALID))
  130. return -ENOENT;
  131. *dst_idx = ANA_TABLES_MACACCESS_DEST_IDX_X(val);
  132. *type = ANA_TABLES_MACACCESS_ENTRYTYPE_X(val);
  133. return 0;
  134. }
  135. EXPORT_SYMBOL(ocelot_mact_lookup);
  136. int ocelot_mact_learn_streamdata(struct ocelot *ocelot, int dst_idx,
  137. const unsigned char mac[ETH_ALEN],
  138. unsigned int vid,
  139. enum macaccess_entry_type type,
  140. int sfid, int ssid)
  141. {
  142. int ret;
  143. mutex_lock(&ocelot->mact_lock);
  144. ocelot_write(ocelot,
  145. (sfid < 0 ? 0 : ANA_TABLES_STREAMDATA_SFID_VALID) |
  146. ANA_TABLES_STREAMDATA_SFID(sfid) |
  147. (ssid < 0 ? 0 : ANA_TABLES_STREAMDATA_SSID_VALID) |
  148. ANA_TABLES_STREAMDATA_SSID(ssid),
  149. ANA_TABLES_STREAMDATA);
  150. ret = __ocelot_mact_learn(ocelot, dst_idx, mac, vid, type);
  151. mutex_unlock(&ocelot->mact_lock);
  152. return ret;
  153. }
  154. EXPORT_SYMBOL(ocelot_mact_learn_streamdata);
  155. static void ocelot_mact_init(struct ocelot *ocelot)
  156. {
  157. /* Configure the learning mode entries attributes:
  158. * - Do not copy the frame to the CPU extraction queues.
  159. * - Use the vlan and mac_cpoy for dmac lookup.
  160. */
  161. ocelot_rmw(ocelot, 0,
  162. ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
  163. | ANA_AGENCTRL_LEARN_FWD_KILL
  164. | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
  165. ANA_AGENCTRL);
  166. /* Clear the MAC table. We are not concurrent with anyone, so
  167. * holding &ocelot->mact_lock is pointless.
  168. */
  169. ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
  170. }
  171. void ocelot_pll5_init(struct ocelot *ocelot)
  172. {
  173. /* Configure PLL5. This will need a proper CCF driver
  174. * The values are coming from the VTSS API for Ocelot
  175. */
  176. regmap_write(ocelot->targets[HSIO], HSIO_PLL5G_CFG4,
  177. HSIO_PLL5G_CFG4_IB_CTRL(0x7600) |
  178. HSIO_PLL5G_CFG4_IB_BIAS_CTRL(0x8));
  179. regmap_write(ocelot->targets[HSIO], HSIO_PLL5G_CFG0,
  180. HSIO_PLL5G_CFG0_CORE_CLK_DIV(0x11) |
  181. HSIO_PLL5G_CFG0_CPU_CLK_DIV(2) |
  182. HSIO_PLL5G_CFG0_ENA_BIAS |
  183. HSIO_PLL5G_CFG0_ENA_VCO_BUF |
  184. HSIO_PLL5G_CFG0_ENA_CP1 |
  185. HSIO_PLL5G_CFG0_SELCPI(2) |
  186. HSIO_PLL5G_CFG0_LOOP_BW_RES(0xe) |
  187. HSIO_PLL5G_CFG0_SELBGV820(4) |
  188. HSIO_PLL5G_CFG0_DIV4 |
  189. HSIO_PLL5G_CFG0_ENA_CLKTREE |
  190. HSIO_PLL5G_CFG0_ENA_LANE);
  191. regmap_write(ocelot->targets[HSIO], HSIO_PLL5G_CFG2,
  192. HSIO_PLL5G_CFG2_EN_RESET_FRQ_DET |
  193. HSIO_PLL5G_CFG2_EN_RESET_OVERRUN |
  194. HSIO_PLL5G_CFG2_GAIN_TEST(0x8) |
  195. HSIO_PLL5G_CFG2_ENA_AMPCTRL |
  196. HSIO_PLL5G_CFG2_PWD_AMPCTRL_N |
  197. HSIO_PLL5G_CFG2_AMPC_SEL(0x10));
  198. }
  199. EXPORT_SYMBOL(ocelot_pll5_init);
  200. static void ocelot_vcap_enable(struct ocelot *ocelot, int port)
  201. {
  202. ocelot_write_gix(ocelot, ANA_PORT_VCAP_S2_CFG_S2_ENA |
  203. ANA_PORT_VCAP_S2_CFG_S2_IP6_CFG(0xa),
  204. ANA_PORT_VCAP_S2_CFG, port);
  205. ocelot_write_gix(ocelot, ANA_PORT_VCAP_CFG_S1_ENA,
  206. ANA_PORT_VCAP_CFG, port);
  207. ocelot_rmw_gix(ocelot, REW_PORT_CFG_ES0_EN,
  208. REW_PORT_CFG_ES0_EN,
  209. REW_PORT_CFG, port);
  210. }
  211. static int ocelot_single_vlan_aware_bridge(struct ocelot *ocelot,
  212. struct netlink_ext_ack *extack)
  213. {
  214. struct net_device *bridge = NULL;
  215. int port;
  216. for (port = 0; port < ocelot->num_phys_ports; port++) {
  217. struct ocelot_port *ocelot_port = ocelot->ports[port];
  218. if (!ocelot_port || !ocelot_port->bridge ||
  219. !br_vlan_enabled(ocelot_port->bridge))
  220. continue;
  221. if (!bridge) {
  222. bridge = ocelot_port->bridge;
  223. continue;
  224. }
  225. if (bridge == ocelot_port->bridge)
  226. continue;
  227. NL_SET_ERR_MSG_MOD(extack,
  228. "Only one VLAN-aware bridge is supported");
  229. return -EBUSY;
  230. }
  231. return 0;
  232. }
  233. static inline u32 ocelot_vlant_read_vlanaccess(struct ocelot *ocelot)
  234. {
  235. return ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
  236. }
  237. static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
  238. {
  239. u32 val;
  240. return readx_poll_timeout(ocelot_vlant_read_vlanaccess,
  241. ocelot,
  242. val,
  243. (val & ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M) ==
  244. ANA_TABLES_VLANACCESS_CMD_IDLE,
  245. TABLE_UPDATE_SLEEP_US, TABLE_UPDATE_TIMEOUT_US);
  246. }
  247. static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
  248. {
  249. /* Select the VID to configure */
  250. ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
  251. ANA_TABLES_VLANTIDX);
  252. /* Set the vlan port members mask and issue a write command */
  253. ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
  254. ANA_TABLES_VLANACCESS_CMD_WRITE,
  255. ANA_TABLES_VLANACCESS);
  256. return ocelot_vlant_wait_for_completion(ocelot);
  257. }
  258. static int ocelot_port_num_untagged_vlans(struct ocelot *ocelot, int port)
  259. {
  260. struct ocelot_bridge_vlan *vlan;
  261. int num_untagged = 0;
  262. list_for_each_entry(vlan, &ocelot->vlans, list) {
  263. if (!(vlan->portmask & BIT(port)))
  264. continue;
  265. /* Ignore the VLAN added by ocelot_add_vlan_unaware_pvid(),
  266. * because this is never active in hardware at the same time as
  267. * the bridge VLANs, which only matter in VLAN-aware mode.
  268. */
  269. if (vlan->vid >= OCELOT_RSV_VLAN_RANGE_START)
  270. continue;
  271. if (vlan->untagged & BIT(port))
  272. num_untagged++;
  273. }
  274. return num_untagged;
  275. }
  276. static int ocelot_port_num_tagged_vlans(struct ocelot *ocelot, int port)
  277. {
  278. struct ocelot_bridge_vlan *vlan;
  279. int num_tagged = 0;
  280. list_for_each_entry(vlan, &ocelot->vlans, list) {
  281. if (!(vlan->portmask & BIT(port)))
  282. continue;
  283. if (!(vlan->untagged & BIT(port)))
  284. num_tagged++;
  285. }
  286. return num_tagged;
  287. }
  288. /* We use native VLAN when we have to mix egress-tagged VLANs with exactly
  289. * _one_ egress-untagged VLAN (_the_ native VLAN)
  290. */
  291. static bool ocelot_port_uses_native_vlan(struct ocelot *ocelot, int port)
  292. {
  293. return ocelot_port_num_tagged_vlans(ocelot, port) &&
  294. ocelot_port_num_untagged_vlans(ocelot, port) == 1;
  295. }
  296. static struct ocelot_bridge_vlan *
  297. ocelot_port_find_native_vlan(struct ocelot *ocelot, int port)
  298. {
  299. struct ocelot_bridge_vlan *vlan;
  300. list_for_each_entry(vlan, &ocelot->vlans, list)
  301. if (vlan->portmask & BIT(port) && vlan->untagged & BIT(port))
  302. return vlan;
  303. return NULL;
  304. }
  305. /* Keep in sync REW_TAG_CFG_TAG_CFG and, if applicable,
  306. * REW_PORT_VLAN_CFG_PORT_VID, with the bridge VLAN table and VLAN awareness
  307. * state of the port.
  308. */
  309. static void ocelot_port_manage_port_tag(struct ocelot *ocelot, int port)
  310. {
  311. struct ocelot_port *ocelot_port = ocelot->ports[port];
  312. enum ocelot_port_tag_config tag_cfg;
  313. bool uses_native_vlan = false;
  314. if (ocelot_port->vlan_aware) {
  315. uses_native_vlan = ocelot_port_uses_native_vlan(ocelot, port);
  316. if (uses_native_vlan)
  317. tag_cfg = OCELOT_PORT_TAG_NATIVE;
  318. else if (ocelot_port_num_untagged_vlans(ocelot, port))
  319. tag_cfg = OCELOT_PORT_TAG_DISABLED;
  320. else
  321. tag_cfg = OCELOT_PORT_TAG_TRUNK;
  322. } else {
  323. tag_cfg = OCELOT_PORT_TAG_DISABLED;
  324. }
  325. ocelot_rmw_gix(ocelot, REW_TAG_CFG_TAG_CFG(tag_cfg),
  326. REW_TAG_CFG_TAG_CFG_M,
  327. REW_TAG_CFG, port);
  328. if (uses_native_vlan) {
  329. struct ocelot_bridge_vlan *native_vlan;
  330. /* Not having a native VLAN is impossible, because
  331. * ocelot_port_num_untagged_vlans has returned 1.
  332. * So there is no use in checking for NULL here.
  333. */
  334. native_vlan = ocelot_port_find_native_vlan(ocelot, port);
  335. ocelot_rmw_gix(ocelot,
  336. REW_PORT_VLAN_CFG_PORT_VID(native_vlan->vid),
  337. REW_PORT_VLAN_CFG_PORT_VID_M,
  338. REW_PORT_VLAN_CFG, port);
  339. }
  340. }
  341. int ocelot_bridge_num_find(struct ocelot *ocelot,
  342. const struct net_device *bridge)
  343. {
  344. int port;
  345. for (port = 0; port < ocelot->num_phys_ports; port++) {
  346. struct ocelot_port *ocelot_port = ocelot->ports[port];
  347. if (ocelot_port && ocelot_port->bridge == bridge)
  348. return ocelot_port->bridge_num;
  349. }
  350. return -1;
  351. }
  352. EXPORT_SYMBOL_GPL(ocelot_bridge_num_find);
  353. static u16 ocelot_vlan_unaware_pvid(struct ocelot *ocelot,
  354. const struct net_device *bridge)
  355. {
  356. int bridge_num;
  357. /* Standalone ports use VID 0 */
  358. if (!bridge)
  359. return 0;
  360. bridge_num = ocelot_bridge_num_find(ocelot, bridge);
  361. if (WARN_ON(bridge_num < 0))
  362. return 0;
  363. /* VLAN-unaware bridges use a reserved VID going from 4095 downwards */
  364. return VLAN_N_VID - bridge_num - 1;
  365. }
  366. /**
  367. * ocelot_update_vlan_reclassify_rule() - Make switch aware only to bridge VLAN TPID
  368. *
  369. * @ocelot: Switch private data structure
  370. * @port: Index of ingress port
  371. *
  372. * IEEE 802.1Q-2018 clauses "5.5 C-VLAN component conformance" and "5.6 S-VLAN
  373. * component conformance" suggest that a C-VLAN component should only recognize
  374. * and filter on C-Tags, and an S-VLAN component should only recognize and
  375. * process based on C-Tags.
  376. *
  377. * In Linux, as per commit 1a0b20b25732 ("Merge branch 'bridge-next'"), C-VLAN
  378. * components are largely represented by a bridge with vlan_protocol 802.1Q,
  379. * and S-VLAN components by a bridge with vlan_protocol 802.1ad.
  380. *
  381. * Currently the driver only offloads vlan_protocol 802.1Q, but the hardware
  382. * design is non-conformant, because the switch assigns each frame to a VLAN
  383. * based on an entirely different question, as detailed in figure "Basic VLAN
  384. * Classification Flow" from its manual and reproduced below.
  385. *
  386. * Set TAG_TYPE, PCP, DEI, VID to port-default values in VLAN_CFG register
  387. * if VLAN_AWARE_ENA[port] and frame has outer tag then:
  388. * if VLAN_INNER_TAG_ENA[port] and frame has inner tag then:
  389. * TAG_TYPE = (Frame.InnerTPID <> 0x8100)
  390. * Set PCP, DEI, VID to values from inner VLAN header
  391. * else:
  392. * TAG_TYPE = (Frame.OuterTPID <> 0x8100)
  393. * Set PCP, DEI, VID to values from outer VLAN header
  394. * if VID == 0 then:
  395. * VID = VLAN_CFG.VLAN_VID
  396. *
  397. * Summarized, the switch will recognize both 802.1Q and 802.1ad TPIDs as VLAN
  398. * "with equal rights", and just set the TAG_TYPE bit to 0 (if 802.1Q) or to 1
  399. * (if 802.1ad). It will classify based on whichever of the tags is "outer", no
  400. * matter what TPID that may have (or "inner", if VLAN_INNER_TAG_ENA[port]).
  401. *
  402. * In the VLAN Table, the TAG_TYPE information is not accessible - just the
  403. * classified VID is - so it is as if each VLAN Table entry is for 2 VLANs:
  404. * C-VLAN X, and S-VLAN X.
  405. *
  406. * Whereas the Linux bridge behavior is to only filter on frames with a TPID
  407. * equal to the vlan_protocol, and treat everything else as VLAN-untagged.
  408. *
  409. * Consider an ingress packet tagged with 802.1ad VID=3 and 802.1Q VID=5,
  410. * received on a bridge vlan_filtering=1 vlan_protocol=802.1Q port. This frame
  411. * should be treated as 802.1Q-untagged, and classified to the PVID of that
  412. * bridge port. Not to VID=3, and not to VID=5.
  413. *
  414. * The VCAP IS1 TCAM has everything we need to overwrite the choices made in
  415. * the basic VLAN classification pipeline: it can match on TAG_TYPE in the key,
  416. * and it can modify the classified VID in the action. Thus, for each port
  417. * under a vlan_filtering bridge, we can insert a rule in VCAP IS1 lookup 0 to
  418. * match on 802.1ad tagged frames and modify their classified VID to the 802.1Q
  419. * PVID of the port. This effectively makes it appear to the outside world as
  420. * if those packets were processed as VLAN-untagged.
  421. *
  422. * The rule needs to be updated each time the bridge PVID changes, and needs
  423. * to be deleted if the bridge PVID is deleted, or if the port becomes
  424. * VLAN-unaware.
  425. */
  426. static int ocelot_update_vlan_reclassify_rule(struct ocelot *ocelot, int port)
  427. {
  428. unsigned long cookie = OCELOT_VCAP_IS1_VLAN_RECLASSIFY(ocelot, port);
  429. struct ocelot_vcap_block *block_vcap_is1 = &ocelot->block[VCAP_IS1];
  430. struct ocelot_port *ocelot_port = ocelot->ports[port];
  431. const struct ocelot_bridge_vlan *pvid_vlan;
  432. struct ocelot_vcap_filter *filter;
  433. int err, val, pcp, dei;
  434. bool vid_replace_ena;
  435. u16 vid;
  436. pvid_vlan = ocelot_port->pvid_vlan;
  437. vid_replace_ena = ocelot_port->vlan_aware && pvid_vlan;
  438. filter = ocelot_vcap_block_find_filter_by_id(block_vcap_is1, cookie,
  439. false);
  440. if (!vid_replace_ena) {
  441. /* If the reclassification filter doesn't need to exist, delete
  442. * it if it was previously installed, and exit doing nothing
  443. * otherwise.
  444. */
  445. if (filter)
  446. return ocelot_vcap_filter_del(ocelot, filter);
  447. return 0;
  448. }
  449. /* The reclassification rule must apply. See if it already exists
  450. * or if it must be created.
  451. */
  452. /* Treating as VLAN-untagged means using as classified VID equal to
  453. * the bridge PVID, and PCP/DEI set to the port default QoS values.
  454. */
  455. vid = pvid_vlan->vid;
  456. val = ocelot_read_gix(ocelot, ANA_PORT_QOS_CFG, port);
  457. pcp = ANA_PORT_QOS_CFG_QOS_DEFAULT_VAL_X(val);
  458. dei = !!(val & ANA_PORT_QOS_CFG_DP_DEFAULT_VAL);
  459. if (filter) {
  460. bool changed = false;
  461. /* Filter exists, just update it */
  462. if (filter->action.vid != vid) {
  463. filter->action.vid = vid;
  464. changed = true;
  465. }
  466. if (filter->action.pcp != pcp) {
  467. filter->action.pcp = pcp;
  468. changed = true;
  469. }
  470. if (filter->action.dei != dei) {
  471. filter->action.dei = dei;
  472. changed = true;
  473. }
  474. if (!changed)
  475. return 0;
  476. return ocelot_vcap_filter_replace(ocelot, filter);
  477. }
  478. /* Filter doesn't exist, create it */
  479. filter = kzalloc(sizeof(*filter), GFP_KERNEL);
  480. if (!filter)
  481. return -ENOMEM;
  482. filter->key_type = OCELOT_VCAP_KEY_ANY;
  483. filter->ingress_port_mask = BIT(port);
  484. filter->vlan.tpid = OCELOT_VCAP_BIT_1;
  485. filter->prio = 1;
  486. filter->id.cookie = cookie;
  487. filter->id.tc_offload = false;
  488. filter->block_id = VCAP_IS1;
  489. filter->type = OCELOT_VCAP_FILTER_OFFLOAD;
  490. filter->lookup = 0;
  491. filter->action.vid_replace_ena = true;
  492. filter->action.pcp_dei_ena = true;
  493. filter->action.vid = vid;
  494. filter->action.pcp = pcp;
  495. filter->action.dei = dei;
  496. err = ocelot_vcap_filter_add(ocelot, filter, NULL);
  497. if (err)
  498. kfree(filter);
  499. return err;
  500. }
  501. /* Default vlan to clasify for untagged frames (may be zero) */
  502. static int ocelot_port_set_pvid(struct ocelot *ocelot, int port,
  503. const struct ocelot_bridge_vlan *pvid_vlan)
  504. {
  505. struct ocelot_port *ocelot_port = ocelot->ports[port];
  506. u16 pvid = ocelot_vlan_unaware_pvid(ocelot, ocelot_port->bridge);
  507. u32 val = 0;
  508. ocelot_port->pvid_vlan = pvid_vlan;
  509. if (ocelot_port->vlan_aware && pvid_vlan)
  510. pvid = pvid_vlan->vid;
  511. ocelot_rmw_gix(ocelot,
  512. ANA_PORT_VLAN_CFG_VLAN_VID(pvid),
  513. ANA_PORT_VLAN_CFG_VLAN_VID_M,
  514. ANA_PORT_VLAN_CFG, port);
  515. /* If there's no pvid, we should drop not only untagged traffic (which
  516. * happens automatically), but also 802.1p traffic which gets
  517. * classified to VLAN 0, but that is always in our RX filter, so it
  518. * would get accepted were it not for this setting.
  519. *
  520. * Also, we only support the bridge 802.1Q VLAN protocol, so
  521. * 802.1ad-tagged frames (carrying S-Tags) should be considered
  522. * 802.1Q-untagged, and also dropped.
  523. */
  524. if (!pvid_vlan && ocelot_port->vlan_aware)
  525. val = ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
  526. ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA |
  527. ANA_PORT_DROP_CFG_DROP_S_TAGGED_ENA;
  528. ocelot_rmw_gix(ocelot, val,
  529. ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
  530. ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA |
  531. ANA_PORT_DROP_CFG_DROP_S_TAGGED_ENA,
  532. ANA_PORT_DROP_CFG, port);
  533. return ocelot_update_vlan_reclassify_rule(ocelot, port);
  534. }
  535. static struct ocelot_bridge_vlan *ocelot_bridge_vlan_find(struct ocelot *ocelot,
  536. u16 vid)
  537. {
  538. struct ocelot_bridge_vlan *vlan;
  539. list_for_each_entry(vlan, &ocelot->vlans, list)
  540. if (vlan->vid == vid)
  541. return vlan;
  542. return NULL;
  543. }
  544. static int ocelot_vlan_member_add(struct ocelot *ocelot, int port, u16 vid,
  545. bool untagged)
  546. {
  547. struct ocelot_bridge_vlan *vlan = ocelot_bridge_vlan_find(ocelot, vid);
  548. unsigned long portmask;
  549. int err;
  550. if (vlan) {
  551. portmask = vlan->portmask | BIT(port);
  552. err = ocelot_vlant_set_mask(ocelot, vid, portmask);
  553. if (err)
  554. return err;
  555. vlan->portmask = portmask;
  556. /* Bridge VLANs can be overwritten with a different
  557. * egress-tagging setting, so make sure to override an untagged
  558. * with a tagged VID if that's going on.
  559. */
  560. if (untagged)
  561. vlan->untagged |= BIT(port);
  562. else
  563. vlan->untagged &= ~BIT(port);
  564. return 0;
  565. }
  566. vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
  567. if (!vlan)
  568. return -ENOMEM;
  569. portmask = BIT(port);
  570. err = ocelot_vlant_set_mask(ocelot, vid, portmask);
  571. if (err) {
  572. kfree(vlan);
  573. return err;
  574. }
  575. vlan->vid = vid;
  576. vlan->portmask = portmask;
  577. if (untagged)
  578. vlan->untagged = BIT(port);
  579. INIT_LIST_HEAD(&vlan->list);
  580. list_add_tail(&vlan->list, &ocelot->vlans);
  581. return 0;
  582. }
  583. static int ocelot_vlan_member_del(struct ocelot *ocelot, int port, u16 vid)
  584. {
  585. struct ocelot_bridge_vlan *vlan = ocelot_bridge_vlan_find(ocelot, vid);
  586. unsigned long portmask;
  587. int err;
  588. if (!vlan)
  589. return 0;
  590. portmask = vlan->portmask & ~BIT(port);
  591. err = ocelot_vlant_set_mask(ocelot, vid, portmask);
  592. if (err)
  593. return err;
  594. vlan->portmask = portmask;
  595. if (vlan->portmask)
  596. return 0;
  597. list_del(&vlan->list);
  598. kfree(vlan);
  599. return 0;
  600. }
  601. static int ocelot_add_vlan_unaware_pvid(struct ocelot *ocelot, int port,
  602. const struct net_device *bridge)
  603. {
  604. u16 vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
  605. return ocelot_vlan_member_add(ocelot, port, vid, true);
  606. }
  607. static int ocelot_del_vlan_unaware_pvid(struct ocelot *ocelot, int port,
  608. const struct net_device *bridge)
  609. {
  610. u16 vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
  611. return ocelot_vlan_member_del(ocelot, port, vid);
  612. }
  613. int ocelot_port_vlan_filtering(struct ocelot *ocelot, int port,
  614. bool vlan_aware, struct netlink_ext_ack *extack)
  615. {
  616. struct ocelot_vcap_block *block = &ocelot->block[VCAP_IS1];
  617. struct ocelot_port *ocelot_port = ocelot->ports[port];
  618. struct ocelot_vcap_filter *filter;
  619. int err = 0;
  620. u32 val;
  621. list_for_each_entry(filter, &block->rules, list) {
  622. if (filter->ingress_port_mask & BIT(port) &&
  623. filter->action.vid_replace_ena) {
  624. NL_SET_ERR_MSG_MOD(extack,
  625. "Cannot change VLAN state with vlan modify rules active");
  626. return -EBUSY;
  627. }
  628. }
  629. err = ocelot_single_vlan_aware_bridge(ocelot, extack);
  630. if (err)
  631. return err;
  632. if (vlan_aware)
  633. err = ocelot_del_vlan_unaware_pvid(ocelot, port,
  634. ocelot_port->bridge);
  635. else if (ocelot_port->bridge)
  636. err = ocelot_add_vlan_unaware_pvid(ocelot, port,
  637. ocelot_port->bridge);
  638. if (err)
  639. return err;
  640. ocelot_port->vlan_aware = vlan_aware;
  641. if (vlan_aware)
  642. val = ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
  643. ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
  644. else
  645. val = 0;
  646. ocelot_rmw_gix(ocelot, val,
  647. ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
  648. ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
  649. ANA_PORT_VLAN_CFG, port);
  650. err = ocelot_port_set_pvid(ocelot, port, ocelot_port->pvid_vlan);
  651. if (err)
  652. return err;
  653. ocelot_port_manage_port_tag(ocelot, port);
  654. return 0;
  655. }
  656. EXPORT_SYMBOL(ocelot_port_vlan_filtering);
  657. int ocelot_vlan_prepare(struct ocelot *ocelot, int port, u16 vid, bool pvid,
  658. bool untagged, struct netlink_ext_ack *extack)
  659. {
  660. if (untagged) {
  661. /* We are adding an egress-tagged VLAN */
  662. if (ocelot_port_uses_native_vlan(ocelot, port)) {
  663. NL_SET_ERR_MSG_MOD(extack,
  664. "Port with egress-tagged VLANs cannot have more than one egress-untagged (native) VLAN");
  665. return -EBUSY;
  666. }
  667. } else {
  668. /* We are adding an egress-tagged VLAN */
  669. if (ocelot_port_num_untagged_vlans(ocelot, port) > 1) {
  670. NL_SET_ERR_MSG_MOD(extack,
  671. "Port with more than one egress-untagged VLAN cannot have egress-tagged VLANs");
  672. return -EBUSY;
  673. }
  674. }
  675. if (vid > OCELOT_RSV_VLAN_RANGE_START) {
  676. NL_SET_ERR_MSG_MOD(extack,
  677. "VLAN range 4000-4095 reserved for VLAN-unaware bridging");
  678. return -EBUSY;
  679. }
  680. return 0;
  681. }
  682. EXPORT_SYMBOL(ocelot_vlan_prepare);
  683. int ocelot_vlan_add(struct ocelot *ocelot, int port, u16 vid, bool pvid,
  684. bool untagged)
  685. {
  686. struct ocelot_port *ocelot_port = ocelot->ports[port];
  687. int err;
  688. /* Ignore VID 0 added to our RX filter by the 8021q module, since
  689. * that collides with OCELOT_STANDALONE_PVID and changes it from
  690. * egress-untagged to egress-tagged.
  691. */
  692. if (!vid)
  693. return 0;
  694. err = ocelot_vlan_member_add(ocelot, port, vid, untagged);
  695. if (err)
  696. return err;
  697. /* Default ingress vlan classification */
  698. if (pvid) {
  699. err = ocelot_port_set_pvid(ocelot, port,
  700. ocelot_bridge_vlan_find(ocelot, vid));
  701. if (err)
  702. return err;
  703. } else if (ocelot_port->pvid_vlan &&
  704. ocelot_bridge_vlan_find(ocelot, vid) == ocelot_port->pvid_vlan) {
  705. err = ocelot_port_set_pvid(ocelot, port, NULL);
  706. if (err)
  707. return err;
  708. }
  709. /* Untagged egress vlan clasification */
  710. ocelot_port_manage_port_tag(ocelot, port);
  711. return 0;
  712. }
  713. EXPORT_SYMBOL(ocelot_vlan_add);
  714. int ocelot_vlan_del(struct ocelot *ocelot, int port, u16 vid)
  715. {
  716. struct ocelot_port *ocelot_port = ocelot->ports[port];
  717. bool del_pvid = false;
  718. int err;
  719. if (!vid)
  720. return 0;
  721. if (ocelot_port->pvid_vlan && ocelot_port->pvid_vlan->vid == vid)
  722. del_pvid = true;
  723. err = ocelot_vlan_member_del(ocelot, port, vid);
  724. if (err)
  725. return err;
  726. /* Ingress */
  727. if (del_pvid) {
  728. err = ocelot_port_set_pvid(ocelot, port, NULL);
  729. if (err)
  730. return err;
  731. }
  732. /* Egress */
  733. ocelot_port_manage_port_tag(ocelot, port);
  734. return 0;
  735. }
  736. EXPORT_SYMBOL(ocelot_vlan_del);
  737. static void ocelot_vlan_init(struct ocelot *ocelot)
  738. {
  739. unsigned long all_ports = GENMASK(ocelot->num_phys_ports - 1, 0);
  740. u16 port, vid;
  741. /* Clear VLAN table, by default all ports are members of all VLANs */
  742. ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
  743. ANA_TABLES_VLANACCESS);
  744. ocelot_vlant_wait_for_completion(ocelot);
  745. /* Configure the port VLAN memberships */
  746. for (vid = 1; vid < VLAN_N_VID; vid++)
  747. ocelot_vlant_set_mask(ocelot, vid, 0);
  748. /* We need VID 0 to get traffic on standalone ports.
  749. * It is added automatically if the 8021q module is loaded, but we
  750. * can't rely on that since it might not be.
  751. */
  752. ocelot_vlant_set_mask(ocelot, OCELOT_STANDALONE_PVID, all_ports);
  753. /* Set vlan ingress filter mask to all ports but the CPU port by
  754. * default.
  755. */
  756. ocelot_write(ocelot, all_ports, ANA_VLANMASK);
  757. for (port = 0; port < ocelot->num_phys_ports; port++) {
  758. ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
  759. ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
  760. }
  761. }
  762. static u32 ocelot_read_eq_avail(struct ocelot *ocelot, int port)
  763. {
  764. return ocelot_read_rix(ocelot, QSYS_SW_STATUS, port);
  765. }
  766. static int ocelot_port_flush(struct ocelot *ocelot, int port)
  767. {
  768. unsigned int pause_ena;
  769. int err, val;
  770. /* Disable dequeuing from the egress queues */
  771. ocelot_rmw_rix(ocelot, QSYS_PORT_MODE_DEQUEUE_DIS,
  772. QSYS_PORT_MODE_DEQUEUE_DIS,
  773. QSYS_PORT_MODE, port);
  774. /* Disable flow control */
  775. ocelot_fields_read(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, &pause_ena);
  776. ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 0);
  777. /* Disable priority flow control */
  778. ocelot_fields_write(ocelot, port,
  779. QSYS_SWITCH_PORT_MODE_TX_PFC_ENA, 0);
  780. /* Wait at least the time it takes to receive a frame of maximum length
  781. * at the port.
  782. * Worst-case delays for 10 kilobyte jumbo frames are:
  783. * 8 ms on a 10M port
  784. * 800 μs on a 100M port
  785. * 80 μs on a 1G port
  786. * 32 μs on a 2.5G port
  787. */
  788. usleep_range(8000, 10000);
  789. /* Disable half duplex backpressure. */
  790. ocelot_rmw_rix(ocelot, 0, SYS_FRONT_PORT_MODE_HDX_MODE,
  791. SYS_FRONT_PORT_MODE, port);
  792. /* Flush the queues associated with the port. */
  793. ocelot_rmw_gix(ocelot, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG_FLUSH_ENA,
  794. REW_PORT_CFG, port);
  795. /* Enable dequeuing from the egress queues. */
  796. ocelot_rmw_rix(ocelot, 0, QSYS_PORT_MODE_DEQUEUE_DIS, QSYS_PORT_MODE,
  797. port);
  798. /* Wait until flushing is complete. */
  799. err = read_poll_timeout(ocelot_read_eq_avail, val, !val,
  800. 100, 2000000, false, ocelot, port);
  801. /* Clear flushing again. */
  802. ocelot_rmw_gix(ocelot, 0, REW_PORT_CFG_FLUSH_ENA, REW_PORT_CFG, port);
  803. /* Re-enable flow control */
  804. ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, pause_ena);
  805. return err;
  806. }
  807. int ocelot_port_configure_serdes(struct ocelot *ocelot, int port,
  808. struct device_node *portnp)
  809. {
  810. struct ocelot_port *ocelot_port = ocelot->ports[port];
  811. struct device *dev = ocelot->dev;
  812. int err;
  813. /* Ensure clock signals and speed are set on all QSGMII links */
  814. if (ocelot_port->phy_mode == PHY_INTERFACE_MODE_QSGMII)
  815. ocelot_port_rmwl(ocelot_port, 0,
  816. DEV_CLOCK_CFG_MAC_TX_RST |
  817. DEV_CLOCK_CFG_MAC_RX_RST,
  818. DEV_CLOCK_CFG);
  819. if (ocelot_port->phy_mode != PHY_INTERFACE_MODE_INTERNAL) {
  820. struct phy *serdes = of_phy_get(portnp, NULL);
  821. if (IS_ERR(serdes)) {
  822. err = PTR_ERR(serdes);
  823. dev_err_probe(dev, err,
  824. "missing SerDes phys for port %d\n",
  825. port);
  826. return err;
  827. }
  828. err = phy_set_mode_ext(serdes, PHY_MODE_ETHERNET,
  829. ocelot_port->phy_mode);
  830. of_phy_put(serdes);
  831. if (err) {
  832. dev_err(dev, "Could not SerDes mode on port %d: %pe\n",
  833. port, ERR_PTR(err));
  834. return err;
  835. }
  836. }
  837. return 0;
  838. }
  839. EXPORT_SYMBOL_GPL(ocelot_port_configure_serdes);
  840. void ocelot_phylink_mac_config(struct ocelot *ocelot, int port,
  841. unsigned int link_an_mode,
  842. const struct phylink_link_state *state)
  843. {
  844. struct ocelot_port *ocelot_port = ocelot->ports[port];
  845. /* Disable HDX fast control */
  846. ocelot_port_writel(ocelot_port, DEV_PORT_MISC_HDX_FAST_DIS,
  847. DEV_PORT_MISC);
  848. /* SGMII only for now */
  849. ocelot_port_writel(ocelot_port, PCS1G_MODE_CFG_SGMII_MODE_ENA,
  850. PCS1G_MODE_CFG);
  851. ocelot_port_writel(ocelot_port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
  852. /* Enable PCS */
  853. ocelot_port_writel(ocelot_port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
  854. /* No aneg on SGMII */
  855. ocelot_port_writel(ocelot_port, 0, PCS1G_ANEG_CFG);
  856. /* No loopback */
  857. ocelot_port_writel(ocelot_port, 0, PCS1G_LB_CFG);
  858. }
  859. EXPORT_SYMBOL_GPL(ocelot_phylink_mac_config);
  860. void ocelot_phylink_mac_link_down(struct ocelot *ocelot, int port,
  861. unsigned int link_an_mode,
  862. phy_interface_t interface,
  863. unsigned long quirks)
  864. {
  865. struct ocelot_port *ocelot_port = ocelot->ports[port];
  866. int err;
  867. ocelot_port->speed = SPEED_UNKNOWN;
  868. ocelot_port_rmwl(ocelot_port, 0, DEV_MAC_ENA_CFG_RX_ENA,
  869. DEV_MAC_ENA_CFG);
  870. if (ocelot->ops->cut_through_fwd) {
  871. mutex_lock(&ocelot->fwd_domain_lock);
  872. ocelot->ops->cut_through_fwd(ocelot);
  873. mutex_unlock(&ocelot->fwd_domain_lock);
  874. }
  875. ocelot_fields_write(ocelot, port, QSYS_SWITCH_PORT_MODE_PORT_ENA, 0);
  876. err = ocelot_port_flush(ocelot, port);
  877. if (err)
  878. dev_err(ocelot->dev, "failed to flush port %d: %d\n",
  879. port, err);
  880. /* Put the port in reset. */
  881. if (interface != PHY_INTERFACE_MODE_QSGMII ||
  882. !(quirks & OCELOT_QUIRK_QSGMII_PORTS_MUST_BE_UP))
  883. ocelot_port_rmwl(ocelot_port,
  884. DEV_CLOCK_CFG_MAC_TX_RST |
  885. DEV_CLOCK_CFG_MAC_RX_RST,
  886. DEV_CLOCK_CFG_MAC_TX_RST |
  887. DEV_CLOCK_CFG_MAC_RX_RST,
  888. DEV_CLOCK_CFG);
  889. }
  890. EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_down);
  891. void ocelot_phylink_mac_link_up(struct ocelot *ocelot, int port,
  892. struct phy_device *phydev,
  893. unsigned int link_an_mode,
  894. phy_interface_t interface,
  895. int speed, int duplex,
  896. bool tx_pause, bool rx_pause,
  897. unsigned long quirks)
  898. {
  899. struct ocelot_port *ocelot_port = ocelot->ports[port];
  900. int mac_speed, mode = 0;
  901. u32 mac_fc_cfg;
  902. ocelot_port->speed = speed;
  903. /* The MAC might be integrated in systems where the MAC speed is fixed
  904. * and it's the PCS who is performing the rate adaptation, so we have
  905. * to write "1000Mbps" into the LINK_SPEED field of DEV_CLOCK_CFG
  906. * (which is also its default value).
  907. */
  908. if ((quirks & OCELOT_QUIRK_PCS_PERFORMS_RATE_ADAPTATION) ||
  909. speed == SPEED_1000) {
  910. mac_speed = OCELOT_SPEED_1000;
  911. mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
  912. } else if (speed == SPEED_2500) {
  913. mac_speed = OCELOT_SPEED_2500;
  914. mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
  915. } else if (speed == SPEED_100) {
  916. mac_speed = OCELOT_SPEED_100;
  917. } else {
  918. mac_speed = OCELOT_SPEED_10;
  919. }
  920. if (duplex == DUPLEX_FULL)
  921. mode |= DEV_MAC_MODE_CFG_FDX_ENA;
  922. ocelot_port_writel(ocelot_port, mode, DEV_MAC_MODE_CFG);
  923. /* Take port out of reset by clearing the MAC_TX_RST, MAC_RX_RST and
  924. * PORT_RST bits in DEV_CLOCK_CFG.
  925. */
  926. ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(mac_speed),
  927. DEV_CLOCK_CFG);
  928. switch (speed) {
  929. case SPEED_10:
  930. mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_10);
  931. break;
  932. case SPEED_100:
  933. mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_100);
  934. break;
  935. case SPEED_1000:
  936. case SPEED_2500:
  937. mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(OCELOT_SPEED_1000);
  938. break;
  939. default:
  940. dev_err(ocelot->dev, "Unsupported speed on port %d: %d\n",
  941. port, speed);
  942. return;
  943. }
  944. if (rx_pause)
  945. mac_fc_cfg |= SYS_MAC_FC_CFG_RX_FC_ENA;
  946. if (tx_pause)
  947. mac_fc_cfg |= SYS_MAC_FC_CFG_TX_FC_ENA |
  948. SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
  949. SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
  950. SYS_MAC_FC_CFG_ZERO_PAUSE_ENA;
  951. /* Flow control. Link speed is only used here to evaluate the time
  952. * specification in incoming pause frames.
  953. */
  954. ocelot_write_rix(ocelot, mac_fc_cfg, SYS_MAC_FC_CFG, port);
  955. ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
  956. /* Don't attempt to send PAUSE frames on the NPI port, it's broken */
  957. if (port != ocelot->npi)
  958. ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA,
  959. tx_pause);
  960. /* Undo the effects of ocelot_phylink_mac_link_down:
  961. * enable MAC module
  962. */
  963. ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
  964. DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
  965. /* If the port supports cut-through forwarding, update the masks before
  966. * enabling forwarding on the port.
  967. */
  968. if (ocelot->ops->cut_through_fwd) {
  969. mutex_lock(&ocelot->fwd_domain_lock);
  970. /* Workaround for hardware bug - FP doesn't work
  971. * at all link speeds for all PHY modes. The function
  972. * below also calls ocelot->ops->cut_through_fwd(),
  973. * so we don't need to do it twice.
  974. */
  975. ocelot_port_update_active_preemptible_tcs(ocelot, port);
  976. mutex_unlock(&ocelot->fwd_domain_lock);
  977. }
  978. /* Core: Enable port for frame transfer */
  979. ocelot_fields_write(ocelot, port,
  980. QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
  981. }
  982. EXPORT_SYMBOL_GPL(ocelot_phylink_mac_link_up);
  983. static int ocelot_rx_frame_word(struct ocelot *ocelot, u8 grp, bool ifh,
  984. u32 *rval)
  985. {
  986. u32 bytes_valid, val;
  987. val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
  988. if (val == XTR_NOT_READY) {
  989. if (ifh)
  990. return -EIO;
  991. do {
  992. val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
  993. } while (val == XTR_NOT_READY);
  994. }
  995. switch (val) {
  996. case XTR_ABORT:
  997. return -EIO;
  998. case XTR_EOF_0:
  999. case XTR_EOF_1:
  1000. case XTR_EOF_2:
  1001. case XTR_EOF_3:
  1002. case XTR_PRUNED:
  1003. bytes_valid = XTR_VALID_BYTES(val);
  1004. val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
  1005. if (val == XTR_ESCAPE)
  1006. *rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
  1007. else
  1008. *rval = val;
  1009. return bytes_valid;
  1010. case XTR_ESCAPE:
  1011. *rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
  1012. return 4;
  1013. default:
  1014. *rval = val;
  1015. return 4;
  1016. }
  1017. }
  1018. static int ocelot_xtr_poll_xfh(struct ocelot *ocelot, int grp, u32 *xfh)
  1019. {
  1020. int i, err = 0;
  1021. for (i = 0; i < OCELOT_TAG_LEN / 4; i++) {
  1022. err = ocelot_rx_frame_word(ocelot, grp, true, &xfh[i]);
  1023. if (err != 4)
  1024. return (err < 0) ? err : -EIO;
  1025. }
  1026. return 0;
  1027. }
  1028. void ocelot_ptp_rx_timestamp(struct ocelot *ocelot, struct sk_buff *skb,
  1029. u64 timestamp)
  1030. {
  1031. struct skb_shared_hwtstamps *shhwtstamps;
  1032. u64 tod_in_ns, full_ts_in_ns;
  1033. struct timespec64 ts;
  1034. ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
  1035. tod_in_ns = ktime_set(ts.tv_sec, ts.tv_nsec);
  1036. if ((tod_in_ns & 0xffffffff) < timestamp)
  1037. full_ts_in_ns = (((tod_in_ns >> 32) - 1) << 32) |
  1038. timestamp;
  1039. else
  1040. full_ts_in_ns = (tod_in_ns & GENMASK_ULL(63, 32)) |
  1041. timestamp;
  1042. shhwtstamps = skb_hwtstamps(skb);
  1043. memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
  1044. shhwtstamps->hwtstamp = full_ts_in_ns;
  1045. }
  1046. EXPORT_SYMBOL(ocelot_ptp_rx_timestamp);
  1047. void ocelot_lock_inj_grp(struct ocelot *ocelot, int grp)
  1048. __acquires(&ocelot->inj_lock)
  1049. {
  1050. spin_lock(&ocelot->inj_lock);
  1051. }
  1052. EXPORT_SYMBOL_GPL(ocelot_lock_inj_grp);
  1053. void ocelot_unlock_inj_grp(struct ocelot *ocelot, int grp)
  1054. __releases(&ocelot->inj_lock)
  1055. {
  1056. spin_unlock(&ocelot->inj_lock);
  1057. }
  1058. EXPORT_SYMBOL_GPL(ocelot_unlock_inj_grp);
  1059. void ocelot_lock_xtr_grp(struct ocelot *ocelot, int grp)
  1060. __acquires(&ocelot->inj_lock)
  1061. {
  1062. spin_lock(&ocelot->inj_lock);
  1063. }
  1064. EXPORT_SYMBOL_GPL(ocelot_lock_xtr_grp);
  1065. void ocelot_unlock_xtr_grp(struct ocelot *ocelot, int grp)
  1066. __releases(&ocelot->inj_lock)
  1067. {
  1068. spin_unlock(&ocelot->inj_lock);
  1069. }
  1070. EXPORT_SYMBOL_GPL(ocelot_unlock_xtr_grp);
  1071. void ocelot_lock_xtr_grp_bh(struct ocelot *ocelot, int grp)
  1072. __acquires(&ocelot->xtr_lock)
  1073. {
  1074. spin_lock_bh(&ocelot->xtr_lock);
  1075. }
  1076. EXPORT_SYMBOL_GPL(ocelot_lock_xtr_grp_bh);
  1077. void ocelot_unlock_xtr_grp_bh(struct ocelot *ocelot, int grp)
  1078. __releases(&ocelot->xtr_lock)
  1079. {
  1080. spin_unlock_bh(&ocelot->xtr_lock);
  1081. }
  1082. EXPORT_SYMBOL_GPL(ocelot_unlock_xtr_grp_bh);
  1083. int ocelot_xtr_poll_frame(struct ocelot *ocelot, int grp, struct sk_buff **nskb)
  1084. {
  1085. u64 timestamp, src_port, len;
  1086. u32 xfh[OCELOT_TAG_LEN / 4];
  1087. struct net_device *dev;
  1088. struct sk_buff *skb;
  1089. int sz, buf_len;
  1090. u32 val, *buf;
  1091. int err;
  1092. lockdep_assert_held(&ocelot->xtr_lock);
  1093. err = ocelot_xtr_poll_xfh(ocelot, grp, xfh);
  1094. if (err)
  1095. return err;
  1096. ocelot_xfh_get_src_port(xfh, &src_port);
  1097. ocelot_xfh_get_len(xfh, &len);
  1098. ocelot_xfh_get_rew_val(xfh, &timestamp);
  1099. if (WARN_ON(src_port >= ocelot->num_phys_ports))
  1100. return -EINVAL;
  1101. dev = ocelot->ops->port_to_netdev(ocelot, src_port);
  1102. if (!dev)
  1103. return -EINVAL;
  1104. skb = netdev_alloc_skb(dev, len);
  1105. if (unlikely(!skb)) {
  1106. netdev_err(dev, "Unable to allocate sk_buff\n");
  1107. return -ENOMEM;
  1108. }
  1109. buf_len = len - ETH_FCS_LEN;
  1110. buf = (u32 *)skb_put(skb, buf_len);
  1111. len = 0;
  1112. do {
  1113. sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
  1114. if (sz < 0) {
  1115. err = sz;
  1116. goto out_free_skb;
  1117. }
  1118. *buf++ = val;
  1119. len += sz;
  1120. } while (len < buf_len);
  1121. /* Read the FCS */
  1122. sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
  1123. if (sz < 0) {
  1124. err = sz;
  1125. goto out_free_skb;
  1126. }
  1127. /* Update the statistics if part of the FCS was read before */
  1128. len -= ETH_FCS_LEN - sz;
  1129. if (unlikely(dev->features & NETIF_F_RXFCS)) {
  1130. buf = (u32 *)skb_put(skb, ETH_FCS_LEN);
  1131. *buf = val;
  1132. }
  1133. if (ocelot->ptp)
  1134. ocelot_ptp_rx_timestamp(ocelot, skb, timestamp);
  1135. /* Everything we see on an interface that is in the HW bridge
  1136. * has already been forwarded.
  1137. */
  1138. if (ocelot->ports[src_port]->bridge)
  1139. skb->offload_fwd_mark = 1;
  1140. skb->protocol = eth_type_trans(skb, dev);
  1141. *nskb = skb;
  1142. return 0;
  1143. out_free_skb:
  1144. kfree_skb(skb);
  1145. return err;
  1146. }
  1147. EXPORT_SYMBOL(ocelot_xtr_poll_frame);
  1148. bool ocelot_can_inject(struct ocelot *ocelot, int grp)
  1149. {
  1150. u32 val = ocelot_read(ocelot, QS_INJ_STATUS);
  1151. lockdep_assert_held(&ocelot->inj_lock);
  1152. if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))))
  1153. return false;
  1154. if (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp)))
  1155. return false;
  1156. return true;
  1157. }
  1158. EXPORT_SYMBOL(ocelot_can_inject);
  1159. /**
  1160. * ocelot_ifh_set_basic - Set basic information in Injection Frame Header
  1161. * @ifh: Pointer to Injection Frame Header memory
  1162. * @ocelot: Switch private data structure
  1163. * @port: Egress port number
  1164. * @rew_op: Egress rewriter operation for PTP
  1165. * @skb: Pointer to socket buffer (packet)
  1166. *
  1167. * Populate the Injection Frame Header with basic information for this skb: the
  1168. * analyzer bypass bit, destination port, VLAN info, egress rewriter info.
  1169. */
  1170. void ocelot_ifh_set_basic(void *ifh, struct ocelot *ocelot, int port,
  1171. u32 rew_op, struct sk_buff *skb)
  1172. {
  1173. struct ocelot_port *ocelot_port = ocelot->ports[port];
  1174. struct net_device *dev = skb->dev;
  1175. u64 vlan_tci, tag_type;
  1176. int qos_class;
  1177. ocelot_xmit_get_vlan_info(skb, ocelot_port->bridge, &vlan_tci,
  1178. &tag_type);
  1179. qos_class = netdev_get_num_tc(dev) ?
  1180. netdev_get_prio_tc_map(dev, skb->priority) : skb->priority;
  1181. memset(ifh, 0, OCELOT_TAG_LEN);
  1182. ocelot_ifh_set_bypass(ifh, 1);
  1183. ocelot_ifh_set_src(ifh, ocelot->num_phys_ports);
  1184. ocelot_ifh_set_dest(ifh, BIT_ULL(port));
  1185. ocelot_ifh_set_qos_class(ifh, qos_class);
  1186. ocelot_ifh_set_tag_type(ifh, tag_type);
  1187. ocelot_ifh_set_vlan_tci(ifh, vlan_tci);
  1188. if (rew_op)
  1189. ocelot_ifh_set_rew_op(ifh, rew_op);
  1190. }
  1191. EXPORT_SYMBOL(ocelot_ifh_set_basic);
  1192. void ocelot_port_inject_frame(struct ocelot *ocelot, int port, int grp,
  1193. u32 rew_op, struct sk_buff *skb)
  1194. {
  1195. u32 ifh[OCELOT_TAG_LEN / 4];
  1196. unsigned int i, count, last;
  1197. lockdep_assert_held(&ocelot->inj_lock);
  1198. ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
  1199. QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
  1200. ocelot_ifh_set_basic(ifh, ocelot, port, rew_op, skb);
  1201. for (i = 0; i < OCELOT_TAG_LEN / 4; i++)
  1202. ocelot_write_rix(ocelot, ifh[i], QS_INJ_WR, grp);
  1203. count = DIV_ROUND_UP(skb->len, 4);
  1204. last = skb->len % 4;
  1205. for (i = 0; i < count; i++)
  1206. ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
  1207. /* Add padding */
  1208. while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
  1209. ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
  1210. i++;
  1211. }
  1212. /* Indicate EOF and valid bytes in last word */
  1213. ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
  1214. QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
  1215. QS_INJ_CTRL_EOF,
  1216. QS_INJ_CTRL, grp);
  1217. /* Add dummy CRC */
  1218. ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
  1219. skb_tx_timestamp(skb);
  1220. skb->dev->stats.tx_packets++;
  1221. skb->dev->stats.tx_bytes += skb->len;
  1222. }
  1223. EXPORT_SYMBOL(ocelot_port_inject_frame);
  1224. void ocelot_drain_cpu_queue(struct ocelot *ocelot, int grp)
  1225. {
  1226. lockdep_assert_held(&ocelot->xtr_lock);
  1227. while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp))
  1228. ocelot_read_rix(ocelot, QS_XTR_RD, grp);
  1229. }
  1230. EXPORT_SYMBOL(ocelot_drain_cpu_queue);
  1231. int ocelot_fdb_add(struct ocelot *ocelot, int port, const unsigned char *addr,
  1232. u16 vid, const struct net_device *bridge)
  1233. {
  1234. if (!vid)
  1235. vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
  1236. return ocelot_mact_learn(ocelot, port, addr, vid, ENTRYTYPE_LOCKED);
  1237. }
  1238. EXPORT_SYMBOL(ocelot_fdb_add);
  1239. int ocelot_fdb_del(struct ocelot *ocelot, int port, const unsigned char *addr,
  1240. u16 vid, const struct net_device *bridge)
  1241. {
  1242. if (!vid)
  1243. vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
  1244. return ocelot_mact_forget(ocelot, addr, vid);
  1245. }
  1246. EXPORT_SYMBOL(ocelot_fdb_del);
  1247. /* Caller must hold &ocelot->mact_lock */
  1248. static int ocelot_mact_read(struct ocelot *ocelot, int port, int row, int col,
  1249. struct ocelot_mact_entry *entry)
  1250. {
  1251. u32 val, dst, macl, mach;
  1252. char mac[ETH_ALEN];
  1253. /* Set row and column to read from */
  1254. ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
  1255. ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
  1256. /* Issue a read command */
  1257. ocelot_write(ocelot,
  1258. ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
  1259. ANA_TABLES_MACACCESS);
  1260. if (ocelot_mact_wait_for_completion(ocelot))
  1261. return -ETIMEDOUT;
  1262. /* Read the entry flags */
  1263. val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
  1264. if (!(val & ANA_TABLES_MACACCESS_VALID))
  1265. return -EINVAL;
  1266. /* If the entry read has another port configured as its destination,
  1267. * do not report it.
  1268. */
  1269. dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
  1270. if (dst != port)
  1271. return -EINVAL;
  1272. /* Get the entry's MAC address and VLAN id */
  1273. macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
  1274. mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
  1275. mac[0] = (mach >> 8) & 0xff;
  1276. mac[1] = (mach >> 0) & 0xff;
  1277. mac[2] = (macl >> 24) & 0xff;
  1278. mac[3] = (macl >> 16) & 0xff;
  1279. mac[4] = (macl >> 8) & 0xff;
  1280. mac[5] = (macl >> 0) & 0xff;
  1281. entry->vid = (mach >> 16) & 0xfff;
  1282. ether_addr_copy(entry->mac, mac);
  1283. return 0;
  1284. }
  1285. int ocelot_mact_flush(struct ocelot *ocelot, int port)
  1286. {
  1287. int err;
  1288. mutex_lock(&ocelot->mact_lock);
  1289. /* Program ageing filter for a single port */
  1290. ocelot_write(ocelot, ANA_ANAGEFIL_PID_EN | ANA_ANAGEFIL_PID_VAL(port),
  1291. ANA_ANAGEFIL);
  1292. /* Flushing dynamic FDB entries requires two successive age scans */
  1293. ocelot_write(ocelot,
  1294. ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_AGE),
  1295. ANA_TABLES_MACACCESS);
  1296. err = ocelot_mact_wait_for_completion(ocelot);
  1297. if (err) {
  1298. mutex_unlock(&ocelot->mact_lock);
  1299. return err;
  1300. }
  1301. /* And second... */
  1302. ocelot_write(ocelot,
  1303. ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_AGE),
  1304. ANA_TABLES_MACACCESS);
  1305. err = ocelot_mact_wait_for_completion(ocelot);
  1306. /* Restore ageing filter */
  1307. ocelot_write(ocelot, 0, ANA_ANAGEFIL);
  1308. mutex_unlock(&ocelot->mact_lock);
  1309. return err;
  1310. }
  1311. EXPORT_SYMBOL_GPL(ocelot_mact_flush);
  1312. int ocelot_fdb_dump(struct ocelot *ocelot, int port,
  1313. dsa_fdb_dump_cb_t *cb, void *data)
  1314. {
  1315. int err = 0;
  1316. int i, j;
  1317. /* We could take the lock just around ocelot_mact_read, but doing so
  1318. * thousands of times in a row seems rather pointless and inefficient.
  1319. */
  1320. mutex_lock(&ocelot->mact_lock);
  1321. /* Loop through all the mac tables entries. */
  1322. for (i = 0; i < ocelot->num_mact_rows; i++) {
  1323. for (j = 0; j < 4; j++) {
  1324. struct ocelot_mact_entry entry;
  1325. bool is_static;
  1326. err = ocelot_mact_read(ocelot, port, i, j, &entry);
  1327. /* If the entry is invalid (wrong port, invalid...),
  1328. * skip it.
  1329. */
  1330. if (err == -EINVAL)
  1331. continue;
  1332. else if (err)
  1333. break;
  1334. is_static = (entry.type == ENTRYTYPE_LOCKED);
  1335. /* Hide the reserved VLANs used for
  1336. * VLAN-unaware bridging.
  1337. */
  1338. if (entry.vid > OCELOT_RSV_VLAN_RANGE_START)
  1339. entry.vid = 0;
  1340. err = cb(entry.mac, entry.vid, is_static, data);
  1341. if (err)
  1342. break;
  1343. }
  1344. }
  1345. mutex_unlock(&ocelot->mact_lock);
  1346. return err;
  1347. }
  1348. EXPORT_SYMBOL(ocelot_fdb_dump);
  1349. int ocelot_trap_add(struct ocelot *ocelot, int port,
  1350. unsigned long cookie, bool take_ts,
  1351. void (*populate)(struct ocelot_vcap_filter *f))
  1352. {
  1353. struct ocelot_vcap_block *block_vcap_is2;
  1354. struct ocelot_vcap_filter *trap;
  1355. bool new = false;
  1356. int err;
  1357. block_vcap_is2 = &ocelot->block[VCAP_IS2];
  1358. trap = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, cookie,
  1359. false);
  1360. if (!trap) {
  1361. trap = kzalloc(sizeof(*trap), GFP_KERNEL);
  1362. if (!trap)
  1363. return -ENOMEM;
  1364. populate(trap);
  1365. trap->prio = 1;
  1366. trap->id.cookie = cookie;
  1367. trap->id.tc_offload = false;
  1368. trap->block_id = VCAP_IS2;
  1369. trap->type = OCELOT_VCAP_FILTER_OFFLOAD;
  1370. trap->lookup = 0;
  1371. trap->action.cpu_copy_ena = true;
  1372. trap->action.mask_mode = OCELOT_MASK_MODE_PERMIT_DENY;
  1373. trap->action.port_mask = 0;
  1374. trap->take_ts = take_ts;
  1375. trap->is_trap = true;
  1376. new = true;
  1377. }
  1378. trap->ingress_port_mask |= BIT(port);
  1379. if (new)
  1380. err = ocelot_vcap_filter_add(ocelot, trap, NULL);
  1381. else
  1382. err = ocelot_vcap_filter_replace(ocelot, trap);
  1383. if (err) {
  1384. trap->ingress_port_mask &= ~BIT(port);
  1385. if (!trap->ingress_port_mask)
  1386. kfree(trap);
  1387. return err;
  1388. }
  1389. return 0;
  1390. }
  1391. int ocelot_trap_del(struct ocelot *ocelot, int port, unsigned long cookie)
  1392. {
  1393. struct ocelot_vcap_block *block_vcap_is2;
  1394. struct ocelot_vcap_filter *trap;
  1395. block_vcap_is2 = &ocelot->block[VCAP_IS2];
  1396. trap = ocelot_vcap_block_find_filter_by_id(block_vcap_is2, cookie,
  1397. false);
  1398. if (!trap)
  1399. return 0;
  1400. trap->ingress_port_mask &= ~BIT(port);
  1401. if (!trap->ingress_port_mask)
  1402. return ocelot_vcap_filter_del(ocelot, trap);
  1403. return ocelot_vcap_filter_replace(ocelot, trap);
  1404. }
  1405. static u32 ocelot_get_bond_mask(struct ocelot *ocelot, struct net_device *bond)
  1406. {
  1407. u32 mask = 0;
  1408. int port;
  1409. lockdep_assert_held(&ocelot->fwd_domain_lock);
  1410. for (port = 0; port < ocelot->num_phys_ports; port++) {
  1411. struct ocelot_port *ocelot_port = ocelot->ports[port];
  1412. if (!ocelot_port)
  1413. continue;
  1414. if (ocelot_port->bond == bond)
  1415. mask |= BIT(port);
  1416. }
  1417. return mask;
  1418. }
  1419. /* The logical port number of a LAG is equal to the lowest numbered physical
  1420. * port ID present in that LAG. It may change if that port ever leaves the LAG.
  1421. */
  1422. int ocelot_bond_get_id(struct ocelot *ocelot, struct net_device *bond)
  1423. {
  1424. int bond_mask = ocelot_get_bond_mask(ocelot, bond);
  1425. if (!bond_mask)
  1426. return -ENOENT;
  1427. return __ffs(bond_mask);
  1428. }
  1429. EXPORT_SYMBOL_GPL(ocelot_bond_get_id);
  1430. /* Returns the mask of user ports assigned to this DSA tag_8021q CPU port.
  1431. * Note that when CPU ports are in a LAG, the user ports are assigned to the
  1432. * 'primary' CPU port, the one whose physical port number gives the logical
  1433. * port number of the LAG.
  1434. *
  1435. * We leave PGID_SRC poorly configured for the 'secondary' CPU port in the LAG
  1436. * (to which no user port is assigned), but it appears that forwarding from
  1437. * this secondary CPU port looks at the PGID_SRC associated with the logical
  1438. * port ID that it's assigned to, which *is* configured properly.
  1439. */
  1440. static u32 ocelot_dsa_8021q_cpu_assigned_ports(struct ocelot *ocelot,
  1441. struct ocelot_port *cpu)
  1442. {
  1443. u32 mask = 0;
  1444. int port;
  1445. for (port = 0; port < ocelot->num_phys_ports; port++) {
  1446. struct ocelot_port *ocelot_port = ocelot->ports[port];
  1447. if (!ocelot_port)
  1448. continue;
  1449. if (ocelot_port->dsa_8021q_cpu == cpu)
  1450. mask |= BIT(port);
  1451. }
  1452. if (cpu->bond)
  1453. mask &= ~ocelot_get_bond_mask(ocelot, cpu->bond);
  1454. return mask;
  1455. }
  1456. /* Returns the DSA tag_8021q CPU port that the given port is assigned to,
  1457. * or the bit mask of CPU ports if said CPU port is in a LAG.
  1458. */
  1459. u32 ocelot_port_assigned_dsa_8021q_cpu_mask(struct ocelot *ocelot, int port)
  1460. {
  1461. struct ocelot_port *ocelot_port = ocelot->ports[port];
  1462. struct ocelot_port *cpu_port = ocelot_port->dsa_8021q_cpu;
  1463. if (!cpu_port)
  1464. return 0;
  1465. if (cpu_port->bond)
  1466. return ocelot_get_bond_mask(ocelot, cpu_port->bond);
  1467. return BIT(cpu_port->index);
  1468. }
  1469. EXPORT_SYMBOL_GPL(ocelot_port_assigned_dsa_8021q_cpu_mask);
  1470. u32 ocelot_get_bridge_fwd_mask(struct ocelot *ocelot, int src_port)
  1471. {
  1472. struct ocelot_port *ocelot_port = ocelot->ports[src_port];
  1473. const struct net_device *bridge;
  1474. u32 mask = 0;
  1475. int port;
  1476. if (!ocelot_port || ocelot_port->stp_state != BR_STATE_FORWARDING)
  1477. return 0;
  1478. bridge = ocelot_port->bridge;
  1479. if (!bridge)
  1480. return 0;
  1481. for (port = 0; port < ocelot->num_phys_ports; port++) {
  1482. ocelot_port = ocelot->ports[port];
  1483. if (!ocelot_port)
  1484. continue;
  1485. if (ocelot_port->stp_state == BR_STATE_FORWARDING &&
  1486. ocelot_port->bridge == bridge)
  1487. mask |= BIT(port);
  1488. }
  1489. return mask;
  1490. }
  1491. EXPORT_SYMBOL_GPL(ocelot_get_bridge_fwd_mask);
  1492. static void ocelot_apply_bridge_fwd_mask(struct ocelot *ocelot, bool joining)
  1493. {
  1494. int port;
  1495. lockdep_assert_held(&ocelot->fwd_domain_lock);
  1496. /* If cut-through forwarding is supported, update the masks before a
  1497. * port joins the forwarding domain, to avoid potential underruns if it
  1498. * has the highest speed from the new domain.
  1499. */
  1500. if (joining && ocelot->ops->cut_through_fwd)
  1501. ocelot->ops->cut_through_fwd(ocelot);
  1502. /* Apply FWD mask. The loop is needed to add/remove the current port as
  1503. * a source for the other ports.
  1504. */
  1505. for (port = 0; port < ocelot->num_phys_ports; port++) {
  1506. struct ocelot_port *ocelot_port = ocelot->ports[port];
  1507. unsigned long mask;
  1508. if (!ocelot_port) {
  1509. /* Unused ports can't send anywhere */
  1510. mask = 0;
  1511. } else if (ocelot_port->is_dsa_8021q_cpu) {
  1512. /* The DSA tag_8021q CPU ports need to be able to
  1513. * forward packets to all ports assigned to them.
  1514. */
  1515. mask = ocelot_dsa_8021q_cpu_assigned_ports(ocelot,
  1516. ocelot_port);
  1517. } else if (ocelot_port->bridge) {
  1518. struct net_device *bond = ocelot_port->bond;
  1519. mask = ocelot_get_bridge_fwd_mask(ocelot, port);
  1520. mask &= ~BIT(port);
  1521. mask |= ocelot_port_assigned_dsa_8021q_cpu_mask(ocelot,
  1522. port);
  1523. if (bond)
  1524. mask &= ~ocelot_get_bond_mask(ocelot, bond);
  1525. } else {
  1526. /* Standalone ports forward only to DSA tag_8021q CPU
  1527. * ports (if those exist), or to the hardware CPU port
  1528. * module otherwise.
  1529. */
  1530. mask = ocelot_port_assigned_dsa_8021q_cpu_mask(ocelot,
  1531. port);
  1532. }
  1533. ocelot_write_rix(ocelot, mask, ANA_PGID_PGID, PGID_SRC + port);
  1534. }
  1535. /* If cut-through forwarding is supported and a port is leaving, there
  1536. * is a chance that cut-through was disabled on the other ports due to
  1537. * the port which is leaving (it has a higher link speed). We need to
  1538. * update the cut-through masks of the remaining ports no earlier than
  1539. * after the port has left, to prevent underruns from happening between
  1540. * the cut-through update and the forwarding domain update.
  1541. */
  1542. if (!joining && ocelot->ops->cut_through_fwd)
  1543. ocelot->ops->cut_through_fwd(ocelot);
  1544. }
  1545. /* Update PGID_CPU which is the destination port mask used for whitelisting
  1546. * unicast addresses filtered towards the host. In the normal and NPI modes,
  1547. * this points to the analyzer entry for the CPU port module, while in DSA
  1548. * tag_8021q mode, it is a bit mask of all active CPU ports.
  1549. * PGID_SRC will take care of forwarding a packet from one user port to
  1550. * no more than a single CPU port.
  1551. */
  1552. static void ocelot_update_pgid_cpu(struct ocelot *ocelot)
  1553. {
  1554. int pgid_cpu = 0;
  1555. int port;
  1556. for (port = 0; port < ocelot->num_phys_ports; port++) {
  1557. struct ocelot_port *ocelot_port = ocelot->ports[port];
  1558. if (!ocelot_port || !ocelot_port->is_dsa_8021q_cpu)
  1559. continue;
  1560. pgid_cpu |= BIT(port);
  1561. }
  1562. if (!pgid_cpu)
  1563. pgid_cpu = BIT(ocelot->num_phys_ports);
  1564. ocelot_write_rix(ocelot, pgid_cpu, ANA_PGID_PGID, PGID_CPU);
  1565. }
  1566. void ocelot_port_setup_dsa_8021q_cpu(struct ocelot *ocelot, int cpu)
  1567. {
  1568. struct ocelot_port *cpu_port = ocelot->ports[cpu];
  1569. u16 vid;
  1570. mutex_lock(&ocelot->fwd_domain_lock);
  1571. cpu_port->is_dsa_8021q_cpu = true;
  1572. for (vid = OCELOT_RSV_VLAN_RANGE_START; vid < VLAN_N_VID; vid++)
  1573. ocelot_vlan_member_add(ocelot, cpu, vid, true);
  1574. ocelot_update_pgid_cpu(ocelot);
  1575. mutex_unlock(&ocelot->fwd_domain_lock);
  1576. }
  1577. EXPORT_SYMBOL_GPL(ocelot_port_setup_dsa_8021q_cpu);
  1578. void ocelot_port_teardown_dsa_8021q_cpu(struct ocelot *ocelot, int cpu)
  1579. {
  1580. struct ocelot_port *cpu_port = ocelot->ports[cpu];
  1581. u16 vid;
  1582. mutex_lock(&ocelot->fwd_domain_lock);
  1583. cpu_port->is_dsa_8021q_cpu = false;
  1584. for (vid = OCELOT_RSV_VLAN_RANGE_START; vid < VLAN_N_VID; vid++)
  1585. ocelot_vlan_member_del(ocelot, cpu_port->index, vid);
  1586. ocelot_update_pgid_cpu(ocelot);
  1587. mutex_unlock(&ocelot->fwd_domain_lock);
  1588. }
  1589. EXPORT_SYMBOL_GPL(ocelot_port_teardown_dsa_8021q_cpu);
  1590. void ocelot_port_assign_dsa_8021q_cpu(struct ocelot *ocelot, int port,
  1591. int cpu)
  1592. {
  1593. struct ocelot_port *cpu_port = ocelot->ports[cpu];
  1594. mutex_lock(&ocelot->fwd_domain_lock);
  1595. ocelot->ports[port]->dsa_8021q_cpu = cpu_port;
  1596. ocelot_apply_bridge_fwd_mask(ocelot, true);
  1597. mutex_unlock(&ocelot->fwd_domain_lock);
  1598. }
  1599. EXPORT_SYMBOL_GPL(ocelot_port_assign_dsa_8021q_cpu);
  1600. void ocelot_port_unassign_dsa_8021q_cpu(struct ocelot *ocelot, int port)
  1601. {
  1602. mutex_lock(&ocelot->fwd_domain_lock);
  1603. ocelot->ports[port]->dsa_8021q_cpu = NULL;
  1604. ocelot_apply_bridge_fwd_mask(ocelot, true);
  1605. mutex_unlock(&ocelot->fwd_domain_lock);
  1606. }
  1607. EXPORT_SYMBOL_GPL(ocelot_port_unassign_dsa_8021q_cpu);
  1608. void ocelot_bridge_stp_state_set(struct ocelot *ocelot, int port, u8 state)
  1609. {
  1610. struct ocelot_port *ocelot_port = ocelot->ports[port];
  1611. u32 learn_ena = 0;
  1612. mutex_lock(&ocelot->fwd_domain_lock);
  1613. ocelot_port->stp_state = state;
  1614. if ((state == BR_STATE_LEARNING || state == BR_STATE_FORWARDING) &&
  1615. ocelot_port->learn_ena)
  1616. learn_ena = ANA_PORT_PORT_CFG_LEARN_ENA;
  1617. ocelot_rmw_gix(ocelot, learn_ena, ANA_PORT_PORT_CFG_LEARN_ENA,
  1618. ANA_PORT_PORT_CFG, port);
  1619. ocelot_apply_bridge_fwd_mask(ocelot, state == BR_STATE_FORWARDING);
  1620. mutex_unlock(&ocelot->fwd_domain_lock);
  1621. }
  1622. EXPORT_SYMBOL(ocelot_bridge_stp_state_set);
  1623. void ocelot_set_ageing_time(struct ocelot *ocelot, unsigned int msecs)
  1624. {
  1625. unsigned int age_period = ANA_AUTOAGE_AGE_PERIOD(msecs / 2000);
  1626. /* Setting AGE_PERIOD to zero effectively disables automatic aging,
  1627. * which is clearly not what our intention is. So avoid that.
  1628. */
  1629. if (!age_period)
  1630. age_period = 1;
  1631. ocelot_rmw(ocelot, age_period, ANA_AUTOAGE_AGE_PERIOD_M, ANA_AUTOAGE);
  1632. }
  1633. EXPORT_SYMBOL(ocelot_set_ageing_time);
  1634. static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
  1635. const unsigned char *addr,
  1636. u16 vid)
  1637. {
  1638. struct ocelot_multicast *mc;
  1639. list_for_each_entry(mc, &ocelot->multicast, list) {
  1640. if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
  1641. return mc;
  1642. }
  1643. return NULL;
  1644. }
  1645. static enum macaccess_entry_type ocelot_classify_mdb(const unsigned char *addr)
  1646. {
  1647. if (addr[0] == 0x01 && addr[1] == 0x00 && addr[2] == 0x5e)
  1648. return ENTRYTYPE_MACv4;
  1649. if (addr[0] == 0x33 && addr[1] == 0x33)
  1650. return ENTRYTYPE_MACv6;
  1651. return ENTRYTYPE_LOCKED;
  1652. }
  1653. static struct ocelot_pgid *ocelot_pgid_alloc(struct ocelot *ocelot, int index,
  1654. unsigned long ports)
  1655. {
  1656. struct ocelot_pgid *pgid;
  1657. pgid = kzalloc(sizeof(*pgid), GFP_KERNEL);
  1658. if (!pgid)
  1659. return ERR_PTR(-ENOMEM);
  1660. pgid->ports = ports;
  1661. pgid->index = index;
  1662. refcount_set(&pgid->refcount, 1);
  1663. list_add_tail(&pgid->list, &ocelot->pgids);
  1664. return pgid;
  1665. }
  1666. static void ocelot_pgid_free(struct ocelot *ocelot, struct ocelot_pgid *pgid)
  1667. {
  1668. if (!refcount_dec_and_test(&pgid->refcount))
  1669. return;
  1670. list_del(&pgid->list);
  1671. kfree(pgid);
  1672. }
  1673. static struct ocelot_pgid *ocelot_mdb_get_pgid(struct ocelot *ocelot,
  1674. const struct ocelot_multicast *mc)
  1675. {
  1676. struct ocelot_pgid *pgid;
  1677. int index;
  1678. /* According to VSC7514 datasheet 3.9.1.5 IPv4 Multicast Entries and
  1679. * 3.9.1.6 IPv6 Multicast Entries, "Instead of a lookup in the
  1680. * destination mask table (PGID), the destination set is programmed as
  1681. * part of the entry MAC address.", and the DEST_IDX is set to 0.
  1682. */
  1683. if (mc->entry_type == ENTRYTYPE_MACv4 ||
  1684. mc->entry_type == ENTRYTYPE_MACv6)
  1685. return ocelot_pgid_alloc(ocelot, 0, mc->ports);
  1686. list_for_each_entry(pgid, &ocelot->pgids, list) {
  1687. /* When searching for a nonreserved multicast PGID, ignore the
  1688. * dummy PGID of zero that we have for MACv4/MACv6 entries
  1689. */
  1690. if (pgid->index && pgid->ports == mc->ports) {
  1691. refcount_inc(&pgid->refcount);
  1692. return pgid;
  1693. }
  1694. }
  1695. /* Search for a free index in the nonreserved multicast PGID area */
  1696. for_each_nonreserved_multicast_dest_pgid(ocelot, index) {
  1697. bool used = false;
  1698. list_for_each_entry(pgid, &ocelot->pgids, list) {
  1699. if (pgid->index == index) {
  1700. used = true;
  1701. break;
  1702. }
  1703. }
  1704. if (!used)
  1705. return ocelot_pgid_alloc(ocelot, index, mc->ports);
  1706. }
  1707. return ERR_PTR(-ENOSPC);
  1708. }
  1709. static void ocelot_encode_ports_to_mdb(unsigned char *addr,
  1710. struct ocelot_multicast *mc)
  1711. {
  1712. ether_addr_copy(addr, mc->addr);
  1713. if (mc->entry_type == ENTRYTYPE_MACv4) {
  1714. addr[0] = 0;
  1715. addr[1] = mc->ports >> 8;
  1716. addr[2] = mc->ports & 0xff;
  1717. } else if (mc->entry_type == ENTRYTYPE_MACv6) {
  1718. addr[0] = mc->ports >> 8;
  1719. addr[1] = mc->ports & 0xff;
  1720. }
  1721. }
  1722. int ocelot_port_mdb_add(struct ocelot *ocelot, int port,
  1723. const struct switchdev_obj_port_mdb *mdb,
  1724. const struct net_device *bridge)
  1725. {
  1726. unsigned char addr[ETH_ALEN];
  1727. struct ocelot_multicast *mc;
  1728. struct ocelot_pgid *pgid;
  1729. u16 vid = mdb->vid;
  1730. if (!vid)
  1731. vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
  1732. mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
  1733. if (!mc) {
  1734. /* New entry */
  1735. mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
  1736. if (!mc)
  1737. return -ENOMEM;
  1738. mc->entry_type = ocelot_classify_mdb(mdb->addr);
  1739. ether_addr_copy(mc->addr, mdb->addr);
  1740. mc->vid = vid;
  1741. list_add_tail(&mc->list, &ocelot->multicast);
  1742. } else {
  1743. /* Existing entry. Clean up the current port mask from
  1744. * hardware now, because we'll be modifying it.
  1745. */
  1746. ocelot_pgid_free(ocelot, mc->pgid);
  1747. ocelot_encode_ports_to_mdb(addr, mc);
  1748. ocelot_mact_forget(ocelot, addr, vid);
  1749. }
  1750. mc->ports |= BIT(port);
  1751. pgid = ocelot_mdb_get_pgid(ocelot, mc);
  1752. if (IS_ERR(pgid)) {
  1753. dev_err(ocelot->dev,
  1754. "Cannot allocate PGID for mdb %pM vid %d\n",
  1755. mc->addr, mc->vid);
  1756. devm_kfree(ocelot->dev, mc);
  1757. return PTR_ERR(pgid);
  1758. }
  1759. mc->pgid = pgid;
  1760. ocelot_encode_ports_to_mdb(addr, mc);
  1761. if (mc->entry_type != ENTRYTYPE_MACv4 &&
  1762. mc->entry_type != ENTRYTYPE_MACv6)
  1763. ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
  1764. pgid->index);
  1765. return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
  1766. mc->entry_type);
  1767. }
  1768. EXPORT_SYMBOL(ocelot_port_mdb_add);
  1769. int ocelot_port_mdb_del(struct ocelot *ocelot, int port,
  1770. const struct switchdev_obj_port_mdb *mdb,
  1771. const struct net_device *bridge)
  1772. {
  1773. unsigned char addr[ETH_ALEN];
  1774. struct ocelot_multicast *mc;
  1775. struct ocelot_pgid *pgid;
  1776. u16 vid = mdb->vid;
  1777. if (!vid)
  1778. vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
  1779. mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
  1780. if (!mc)
  1781. return -ENOENT;
  1782. ocelot_encode_ports_to_mdb(addr, mc);
  1783. ocelot_mact_forget(ocelot, addr, vid);
  1784. ocelot_pgid_free(ocelot, mc->pgid);
  1785. mc->ports &= ~BIT(port);
  1786. if (!mc->ports) {
  1787. list_del(&mc->list);
  1788. devm_kfree(ocelot->dev, mc);
  1789. return 0;
  1790. }
  1791. /* We have a PGID with fewer ports now */
  1792. pgid = ocelot_mdb_get_pgid(ocelot, mc);
  1793. if (IS_ERR(pgid))
  1794. return PTR_ERR(pgid);
  1795. mc->pgid = pgid;
  1796. ocelot_encode_ports_to_mdb(addr, mc);
  1797. if (mc->entry_type != ENTRYTYPE_MACv4 &&
  1798. mc->entry_type != ENTRYTYPE_MACv6)
  1799. ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
  1800. pgid->index);
  1801. return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
  1802. mc->entry_type);
  1803. }
  1804. EXPORT_SYMBOL(ocelot_port_mdb_del);
  1805. int ocelot_port_bridge_join(struct ocelot *ocelot, int port,
  1806. struct net_device *bridge, int bridge_num,
  1807. struct netlink_ext_ack *extack)
  1808. {
  1809. struct ocelot_port *ocelot_port = ocelot->ports[port];
  1810. int err;
  1811. err = ocelot_single_vlan_aware_bridge(ocelot, extack);
  1812. if (err)
  1813. return err;
  1814. mutex_lock(&ocelot->fwd_domain_lock);
  1815. ocelot_port->bridge = bridge;
  1816. ocelot_port->bridge_num = bridge_num;
  1817. ocelot_apply_bridge_fwd_mask(ocelot, true);
  1818. mutex_unlock(&ocelot->fwd_domain_lock);
  1819. if (br_vlan_enabled(bridge))
  1820. return 0;
  1821. return ocelot_add_vlan_unaware_pvid(ocelot, port, bridge);
  1822. }
  1823. EXPORT_SYMBOL(ocelot_port_bridge_join);
  1824. void ocelot_port_bridge_leave(struct ocelot *ocelot, int port,
  1825. struct net_device *bridge)
  1826. {
  1827. struct ocelot_port *ocelot_port = ocelot->ports[port];
  1828. mutex_lock(&ocelot->fwd_domain_lock);
  1829. if (!br_vlan_enabled(bridge))
  1830. ocelot_del_vlan_unaware_pvid(ocelot, port, bridge);
  1831. ocelot_port->bridge = NULL;
  1832. ocelot_port->bridge_num = -1;
  1833. ocelot_port_set_pvid(ocelot, port, NULL);
  1834. ocelot_port_manage_port_tag(ocelot, port);
  1835. ocelot_apply_bridge_fwd_mask(ocelot, false);
  1836. mutex_unlock(&ocelot->fwd_domain_lock);
  1837. }
  1838. EXPORT_SYMBOL(ocelot_port_bridge_leave);
  1839. static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
  1840. {
  1841. unsigned long visited = GENMASK(ocelot->num_phys_ports - 1, 0);
  1842. int i, port, lag;
  1843. /* Reset destination and aggregation PGIDS */
  1844. for_each_unicast_dest_pgid(ocelot, port)
  1845. ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
  1846. for_each_aggr_pgid(ocelot, i)
  1847. ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
  1848. ANA_PGID_PGID, i);
  1849. /* The visited ports bitmask holds the list of ports offloading any
  1850. * bonding interface. Initially we mark all these ports as unvisited,
  1851. * then every time we visit a port in this bitmask, we know that it is
  1852. * the lowest numbered port, i.e. the one whose logical ID == physical
  1853. * port ID == LAG ID. So we mark as visited all further ports in the
  1854. * bitmask that are offloading the same bonding interface. This way,
  1855. * we set up the aggregation PGIDs only once per bonding interface.
  1856. */
  1857. for (port = 0; port < ocelot->num_phys_ports; port++) {
  1858. struct ocelot_port *ocelot_port = ocelot->ports[port];
  1859. if (!ocelot_port || !ocelot_port->bond)
  1860. continue;
  1861. visited &= ~BIT(port);
  1862. }
  1863. /* Now, set PGIDs for each active LAG */
  1864. for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
  1865. struct net_device *bond = ocelot->ports[lag]->bond;
  1866. int num_active_ports = 0;
  1867. unsigned long bond_mask;
  1868. u8 aggr_idx[16];
  1869. if (!bond || (visited & BIT(lag)))
  1870. continue;
  1871. bond_mask = ocelot_get_bond_mask(ocelot, bond);
  1872. for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
  1873. struct ocelot_port *ocelot_port = ocelot->ports[port];
  1874. // Destination mask
  1875. ocelot_write_rix(ocelot, bond_mask,
  1876. ANA_PGID_PGID, port);
  1877. if (ocelot_port->lag_tx_active)
  1878. aggr_idx[num_active_ports++] = port;
  1879. }
  1880. for_each_aggr_pgid(ocelot, i) {
  1881. u32 ac;
  1882. ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
  1883. ac &= ~bond_mask;
  1884. /* Don't do division by zero if there was no active
  1885. * port. Just make all aggregation codes zero.
  1886. */
  1887. if (num_active_ports)
  1888. ac |= BIT(aggr_idx[i % num_active_ports]);
  1889. ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
  1890. }
  1891. /* Mark all ports in the same LAG as visited to avoid applying
  1892. * the same config again.
  1893. */
  1894. for (port = lag; port < ocelot->num_phys_ports; port++) {
  1895. struct ocelot_port *ocelot_port = ocelot->ports[port];
  1896. if (!ocelot_port)
  1897. continue;
  1898. if (ocelot_port->bond == bond)
  1899. visited |= BIT(port);
  1900. }
  1901. }
  1902. }
  1903. /* When offloading a bonding interface, the switch ports configured under the
  1904. * same bond must have the same logical port ID, equal to the physical port ID
  1905. * of the lowest numbered physical port in that bond. Otherwise, in standalone/
  1906. * bridged mode, each port has a logical port ID equal to its physical port ID.
  1907. */
  1908. static void ocelot_setup_logical_port_ids(struct ocelot *ocelot)
  1909. {
  1910. int port;
  1911. for (port = 0; port < ocelot->num_phys_ports; port++) {
  1912. struct ocelot_port *ocelot_port = ocelot->ports[port];
  1913. struct net_device *bond;
  1914. if (!ocelot_port)
  1915. continue;
  1916. bond = ocelot_port->bond;
  1917. if (bond) {
  1918. int lag = ocelot_bond_get_id(ocelot, bond);
  1919. ocelot_rmw_gix(ocelot,
  1920. ANA_PORT_PORT_CFG_PORTID_VAL(lag),
  1921. ANA_PORT_PORT_CFG_PORTID_VAL_M,
  1922. ANA_PORT_PORT_CFG, port);
  1923. } else {
  1924. ocelot_rmw_gix(ocelot,
  1925. ANA_PORT_PORT_CFG_PORTID_VAL(port),
  1926. ANA_PORT_PORT_CFG_PORTID_VAL_M,
  1927. ANA_PORT_PORT_CFG, port);
  1928. }
  1929. }
  1930. }
  1931. static int ocelot_migrate_mc(struct ocelot *ocelot, struct ocelot_multicast *mc,
  1932. unsigned long from_mask, unsigned long to_mask)
  1933. {
  1934. unsigned char addr[ETH_ALEN];
  1935. struct ocelot_pgid *pgid;
  1936. u16 vid = mc->vid;
  1937. dev_dbg(ocelot->dev,
  1938. "Migrating multicast %pM vid %d from port mask 0x%lx to 0x%lx\n",
  1939. mc->addr, mc->vid, from_mask, to_mask);
  1940. /* First clean up the current port mask from hardware, because
  1941. * we'll be modifying it.
  1942. */
  1943. ocelot_pgid_free(ocelot, mc->pgid);
  1944. ocelot_encode_ports_to_mdb(addr, mc);
  1945. ocelot_mact_forget(ocelot, addr, vid);
  1946. mc->ports &= ~from_mask;
  1947. mc->ports |= to_mask;
  1948. pgid = ocelot_mdb_get_pgid(ocelot, mc);
  1949. if (IS_ERR(pgid)) {
  1950. dev_err(ocelot->dev,
  1951. "Cannot allocate PGID for mdb %pM vid %d\n",
  1952. mc->addr, mc->vid);
  1953. devm_kfree(ocelot->dev, mc);
  1954. return PTR_ERR(pgid);
  1955. }
  1956. mc->pgid = pgid;
  1957. ocelot_encode_ports_to_mdb(addr, mc);
  1958. if (mc->entry_type != ENTRYTYPE_MACv4 &&
  1959. mc->entry_type != ENTRYTYPE_MACv6)
  1960. ocelot_write_rix(ocelot, pgid->ports, ANA_PGID_PGID,
  1961. pgid->index);
  1962. return ocelot_mact_learn(ocelot, pgid->index, addr, vid,
  1963. mc->entry_type);
  1964. }
  1965. int ocelot_migrate_mdbs(struct ocelot *ocelot, unsigned long from_mask,
  1966. unsigned long to_mask)
  1967. {
  1968. struct ocelot_multicast *mc;
  1969. int err;
  1970. list_for_each_entry(mc, &ocelot->multicast, list) {
  1971. if (!(mc->ports & from_mask))
  1972. continue;
  1973. err = ocelot_migrate_mc(ocelot, mc, from_mask, to_mask);
  1974. if (err)
  1975. return err;
  1976. }
  1977. return 0;
  1978. }
  1979. EXPORT_SYMBOL_GPL(ocelot_migrate_mdbs);
  1980. /* Documentation for PORTID_VAL says:
  1981. * Logical port number for front port. If port is not a member of a LLAG,
  1982. * then PORTID must be set to the physical port number.
  1983. * If port is a member of a LLAG, then PORTID must be set to the common
  1984. * PORTID_VAL used for all member ports of the LLAG.
  1985. * The value must not exceed the number of physical ports on the device.
  1986. *
  1987. * This means we have little choice but to migrate FDB entries pointing towards
  1988. * a logical port when that changes.
  1989. */
  1990. static void ocelot_migrate_lag_fdbs(struct ocelot *ocelot,
  1991. struct net_device *bond,
  1992. int lag)
  1993. {
  1994. struct ocelot_lag_fdb *fdb;
  1995. int err;
  1996. lockdep_assert_held(&ocelot->fwd_domain_lock);
  1997. list_for_each_entry(fdb, &ocelot->lag_fdbs, list) {
  1998. if (fdb->bond != bond)
  1999. continue;
  2000. err = ocelot_mact_forget(ocelot, fdb->addr, fdb->vid);
  2001. if (err) {
  2002. dev_err(ocelot->dev,
  2003. "failed to delete LAG %s FDB %pM vid %d: %pe\n",
  2004. bond->name, fdb->addr, fdb->vid, ERR_PTR(err));
  2005. }
  2006. err = ocelot_mact_learn(ocelot, lag, fdb->addr, fdb->vid,
  2007. ENTRYTYPE_LOCKED);
  2008. if (err) {
  2009. dev_err(ocelot->dev,
  2010. "failed to migrate LAG %s FDB %pM vid %d: %pe\n",
  2011. bond->name, fdb->addr, fdb->vid, ERR_PTR(err));
  2012. }
  2013. }
  2014. }
  2015. int ocelot_port_lag_join(struct ocelot *ocelot, int port,
  2016. struct net_device *bond,
  2017. struct netdev_lag_upper_info *info,
  2018. struct netlink_ext_ack *extack)
  2019. {
  2020. if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH) {
  2021. NL_SET_ERR_MSG_MOD(extack,
  2022. "Can only offload LAG using hash TX type");
  2023. return -EOPNOTSUPP;
  2024. }
  2025. mutex_lock(&ocelot->fwd_domain_lock);
  2026. ocelot->ports[port]->bond = bond;
  2027. ocelot_setup_logical_port_ids(ocelot);
  2028. ocelot_apply_bridge_fwd_mask(ocelot, true);
  2029. ocelot_set_aggr_pgids(ocelot);
  2030. mutex_unlock(&ocelot->fwd_domain_lock);
  2031. return 0;
  2032. }
  2033. EXPORT_SYMBOL(ocelot_port_lag_join);
  2034. void ocelot_port_lag_leave(struct ocelot *ocelot, int port,
  2035. struct net_device *bond)
  2036. {
  2037. int old_lag_id, new_lag_id;
  2038. mutex_lock(&ocelot->fwd_domain_lock);
  2039. old_lag_id = ocelot_bond_get_id(ocelot, bond);
  2040. ocelot->ports[port]->bond = NULL;
  2041. ocelot_setup_logical_port_ids(ocelot);
  2042. ocelot_apply_bridge_fwd_mask(ocelot, false);
  2043. ocelot_set_aggr_pgids(ocelot);
  2044. new_lag_id = ocelot_bond_get_id(ocelot, bond);
  2045. if (new_lag_id >= 0 && old_lag_id != new_lag_id)
  2046. ocelot_migrate_lag_fdbs(ocelot, bond, new_lag_id);
  2047. mutex_unlock(&ocelot->fwd_domain_lock);
  2048. }
  2049. EXPORT_SYMBOL(ocelot_port_lag_leave);
  2050. void ocelot_port_lag_change(struct ocelot *ocelot, int port, bool lag_tx_active)
  2051. {
  2052. struct ocelot_port *ocelot_port = ocelot->ports[port];
  2053. mutex_lock(&ocelot->fwd_domain_lock);
  2054. ocelot_port->lag_tx_active = lag_tx_active;
  2055. /* Rebalance the LAGs */
  2056. ocelot_set_aggr_pgids(ocelot);
  2057. mutex_unlock(&ocelot->fwd_domain_lock);
  2058. }
  2059. EXPORT_SYMBOL(ocelot_port_lag_change);
  2060. int ocelot_lag_fdb_add(struct ocelot *ocelot, struct net_device *bond,
  2061. const unsigned char *addr, u16 vid,
  2062. const struct net_device *bridge)
  2063. {
  2064. struct ocelot_lag_fdb *fdb;
  2065. int lag, err;
  2066. fdb = kzalloc(sizeof(*fdb), GFP_KERNEL);
  2067. if (!fdb)
  2068. return -ENOMEM;
  2069. mutex_lock(&ocelot->fwd_domain_lock);
  2070. if (!vid)
  2071. vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
  2072. ether_addr_copy(fdb->addr, addr);
  2073. fdb->vid = vid;
  2074. fdb->bond = bond;
  2075. lag = ocelot_bond_get_id(ocelot, bond);
  2076. err = ocelot_mact_learn(ocelot, lag, addr, vid, ENTRYTYPE_LOCKED);
  2077. if (err) {
  2078. mutex_unlock(&ocelot->fwd_domain_lock);
  2079. kfree(fdb);
  2080. return err;
  2081. }
  2082. list_add_tail(&fdb->list, &ocelot->lag_fdbs);
  2083. mutex_unlock(&ocelot->fwd_domain_lock);
  2084. return 0;
  2085. }
  2086. EXPORT_SYMBOL_GPL(ocelot_lag_fdb_add);
  2087. int ocelot_lag_fdb_del(struct ocelot *ocelot, struct net_device *bond,
  2088. const unsigned char *addr, u16 vid,
  2089. const struct net_device *bridge)
  2090. {
  2091. struct ocelot_lag_fdb *fdb, *tmp;
  2092. mutex_lock(&ocelot->fwd_domain_lock);
  2093. if (!vid)
  2094. vid = ocelot_vlan_unaware_pvid(ocelot, bridge);
  2095. list_for_each_entry_safe(fdb, tmp, &ocelot->lag_fdbs, list) {
  2096. if (!ether_addr_equal(fdb->addr, addr) || fdb->vid != vid ||
  2097. fdb->bond != bond)
  2098. continue;
  2099. ocelot_mact_forget(ocelot, addr, vid);
  2100. list_del(&fdb->list);
  2101. mutex_unlock(&ocelot->fwd_domain_lock);
  2102. kfree(fdb);
  2103. return 0;
  2104. }
  2105. mutex_unlock(&ocelot->fwd_domain_lock);
  2106. return -ENOENT;
  2107. }
  2108. EXPORT_SYMBOL_GPL(ocelot_lag_fdb_del);
  2109. /* Configure the maximum SDU (L2 payload) on RX to the value specified in @sdu.
  2110. * The length of VLAN tags is accounted for automatically via DEV_MAC_TAGS_CFG.
  2111. * In the special case that it's the NPI port that we're configuring, the
  2112. * length of the tag and optional prefix needs to be accounted for privately,
  2113. * in order to be able to sustain communication at the requested @sdu.
  2114. */
  2115. void ocelot_port_set_maxlen(struct ocelot *ocelot, int port, size_t sdu)
  2116. {
  2117. struct ocelot_port *ocelot_port = ocelot->ports[port];
  2118. int maxlen = sdu + ETH_HLEN + ETH_FCS_LEN;
  2119. int pause_start, pause_stop;
  2120. int atop, atop_tot;
  2121. if (port == ocelot->npi) {
  2122. maxlen += OCELOT_TAG_LEN;
  2123. if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
  2124. maxlen += OCELOT_SHORT_PREFIX_LEN;
  2125. else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
  2126. maxlen += OCELOT_LONG_PREFIX_LEN;
  2127. }
  2128. ocelot_port_writel(ocelot_port, maxlen, DEV_MAC_MAXLEN_CFG);
  2129. /* Set Pause watermark hysteresis */
  2130. pause_start = 6 * maxlen / OCELOT_BUFFER_CELL_SZ;
  2131. pause_stop = 4 * maxlen / OCELOT_BUFFER_CELL_SZ;
  2132. ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_START,
  2133. pause_start);
  2134. ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_STOP,
  2135. pause_stop);
  2136. /* Tail dropping watermarks */
  2137. atop_tot = (ocelot->packet_buffer_size - 9 * maxlen) /
  2138. OCELOT_BUFFER_CELL_SZ;
  2139. atop = (9 * maxlen) / OCELOT_BUFFER_CELL_SZ;
  2140. ocelot_write_rix(ocelot, ocelot->ops->wm_enc(atop), SYS_ATOP, port);
  2141. ocelot_write(ocelot, ocelot->ops->wm_enc(atop_tot), SYS_ATOP_TOT_CFG);
  2142. }
  2143. EXPORT_SYMBOL(ocelot_port_set_maxlen);
  2144. int ocelot_get_max_mtu(struct ocelot *ocelot, int port)
  2145. {
  2146. int max_mtu = 65535 - ETH_HLEN - ETH_FCS_LEN;
  2147. if (port == ocelot->npi) {
  2148. max_mtu -= OCELOT_TAG_LEN;
  2149. if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_SHORT)
  2150. max_mtu -= OCELOT_SHORT_PREFIX_LEN;
  2151. else if (ocelot->npi_inj_prefix == OCELOT_TAG_PREFIX_LONG)
  2152. max_mtu -= OCELOT_LONG_PREFIX_LEN;
  2153. }
  2154. return max_mtu;
  2155. }
  2156. EXPORT_SYMBOL(ocelot_get_max_mtu);
  2157. static void ocelot_port_set_learning(struct ocelot *ocelot, int port,
  2158. bool enabled)
  2159. {
  2160. struct ocelot_port *ocelot_port = ocelot->ports[port];
  2161. u32 val = 0;
  2162. if (enabled)
  2163. val = ANA_PORT_PORT_CFG_LEARN_ENA;
  2164. ocelot_rmw_gix(ocelot, val, ANA_PORT_PORT_CFG_LEARN_ENA,
  2165. ANA_PORT_PORT_CFG, port);
  2166. ocelot_port->learn_ena = enabled;
  2167. }
  2168. static void ocelot_port_set_ucast_flood(struct ocelot *ocelot, int port,
  2169. bool enabled)
  2170. {
  2171. u32 val = 0;
  2172. if (enabled)
  2173. val = BIT(port);
  2174. ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_UC);
  2175. }
  2176. static void ocelot_port_set_mcast_flood(struct ocelot *ocelot, int port,
  2177. bool enabled)
  2178. {
  2179. u32 val = 0;
  2180. if (enabled)
  2181. val = BIT(port);
  2182. ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MC);
  2183. ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MCIPV4);
  2184. ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_MCIPV6);
  2185. }
  2186. static void ocelot_port_set_bcast_flood(struct ocelot *ocelot, int port,
  2187. bool enabled)
  2188. {
  2189. u32 val = 0;
  2190. if (enabled)
  2191. val = BIT(port);
  2192. ocelot_rmw_rix(ocelot, val, BIT(port), ANA_PGID_PGID, PGID_BC);
  2193. }
  2194. int ocelot_port_pre_bridge_flags(struct ocelot *ocelot, int port,
  2195. struct switchdev_brport_flags flags)
  2196. {
  2197. if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
  2198. BR_BCAST_FLOOD))
  2199. return -EINVAL;
  2200. return 0;
  2201. }
  2202. EXPORT_SYMBOL(ocelot_port_pre_bridge_flags);
  2203. void ocelot_port_bridge_flags(struct ocelot *ocelot, int port,
  2204. struct switchdev_brport_flags flags)
  2205. {
  2206. if (flags.mask & BR_LEARNING)
  2207. ocelot_port_set_learning(ocelot, port,
  2208. !!(flags.val & BR_LEARNING));
  2209. if (flags.mask & BR_FLOOD)
  2210. ocelot_port_set_ucast_flood(ocelot, port,
  2211. !!(flags.val & BR_FLOOD));
  2212. if (flags.mask & BR_MCAST_FLOOD)
  2213. ocelot_port_set_mcast_flood(ocelot, port,
  2214. !!(flags.val & BR_MCAST_FLOOD));
  2215. if (flags.mask & BR_BCAST_FLOOD)
  2216. ocelot_port_set_bcast_flood(ocelot, port,
  2217. !!(flags.val & BR_BCAST_FLOOD));
  2218. }
  2219. EXPORT_SYMBOL(ocelot_port_bridge_flags);
  2220. int ocelot_port_get_default_prio(struct ocelot *ocelot, int port)
  2221. {
  2222. int val = ocelot_read_gix(ocelot, ANA_PORT_QOS_CFG, port);
  2223. return ANA_PORT_QOS_CFG_QOS_DEFAULT_VAL_X(val);
  2224. }
  2225. EXPORT_SYMBOL_GPL(ocelot_port_get_default_prio);
  2226. int ocelot_port_set_default_prio(struct ocelot *ocelot, int port, u8 prio)
  2227. {
  2228. if (prio >= OCELOT_NUM_TC)
  2229. return -ERANGE;
  2230. ocelot_rmw_gix(ocelot,
  2231. ANA_PORT_QOS_CFG_QOS_DEFAULT_VAL(prio),
  2232. ANA_PORT_QOS_CFG_QOS_DEFAULT_VAL_M,
  2233. ANA_PORT_QOS_CFG,
  2234. port);
  2235. return ocelot_update_vlan_reclassify_rule(ocelot, port);
  2236. }
  2237. EXPORT_SYMBOL_GPL(ocelot_port_set_default_prio);
  2238. int ocelot_port_get_dscp_prio(struct ocelot *ocelot, int port, u8 dscp)
  2239. {
  2240. int qos_cfg = ocelot_read_gix(ocelot, ANA_PORT_QOS_CFG, port);
  2241. int dscp_cfg = ocelot_read_rix(ocelot, ANA_DSCP_CFG, dscp);
  2242. /* Return error if DSCP prioritization isn't enabled */
  2243. if (!(qos_cfg & ANA_PORT_QOS_CFG_QOS_DSCP_ENA))
  2244. return -EOPNOTSUPP;
  2245. if (qos_cfg & ANA_PORT_QOS_CFG_DSCP_TRANSLATE_ENA) {
  2246. dscp = ANA_DSCP_CFG_DSCP_TRANSLATE_VAL_X(dscp_cfg);
  2247. /* Re-read ANA_DSCP_CFG for the translated DSCP */
  2248. dscp_cfg = ocelot_read_rix(ocelot, ANA_DSCP_CFG, dscp);
  2249. }
  2250. /* If the DSCP value is not trusted, the QoS classification falls back
  2251. * to VLAN PCP or port-based default.
  2252. */
  2253. if (!(dscp_cfg & ANA_DSCP_CFG_DSCP_TRUST_ENA))
  2254. return -EOPNOTSUPP;
  2255. return ANA_DSCP_CFG_QOS_DSCP_VAL_X(dscp_cfg);
  2256. }
  2257. EXPORT_SYMBOL_GPL(ocelot_port_get_dscp_prio);
  2258. int ocelot_port_add_dscp_prio(struct ocelot *ocelot, int port, u8 dscp, u8 prio)
  2259. {
  2260. int mask, val;
  2261. if (prio >= OCELOT_NUM_TC)
  2262. return -ERANGE;
  2263. /* There is at least one app table priority (this one), so we need to
  2264. * make sure DSCP prioritization is enabled on the port.
  2265. * Also make sure DSCP translation is disabled
  2266. * (dcbnl doesn't support it).
  2267. */
  2268. mask = ANA_PORT_QOS_CFG_QOS_DSCP_ENA |
  2269. ANA_PORT_QOS_CFG_DSCP_TRANSLATE_ENA;
  2270. ocelot_rmw_gix(ocelot, ANA_PORT_QOS_CFG_QOS_DSCP_ENA, mask,
  2271. ANA_PORT_QOS_CFG, port);
  2272. /* Trust this DSCP value and map it to the given QoS class */
  2273. val = ANA_DSCP_CFG_DSCP_TRUST_ENA | ANA_DSCP_CFG_QOS_DSCP_VAL(prio);
  2274. ocelot_write_rix(ocelot, val, ANA_DSCP_CFG, dscp);
  2275. return 0;
  2276. }
  2277. EXPORT_SYMBOL_GPL(ocelot_port_add_dscp_prio);
  2278. int ocelot_port_del_dscp_prio(struct ocelot *ocelot, int port, u8 dscp, u8 prio)
  2279. {
  2280. int dscp_cfg = ocelot_read_rix(ocelot, ANA_DSCP_CFG, dscp);
  2281. int mask, i;
  2282. /* During a "dcb app replace" command, the new app table entry will be
  2283. * added first, then the old one will be deleted. But the hardware only
  2284. * supports one QoS class per DSCP value (duh), so if we blindly delete
  2285. * the app table entry for this DSCP value, we end up deleting the
  2286. * entry with the new priority. Avoid that by checking whether user
  2287. * space wants to delete the priority which is currently configured, or
  2288. * something else which is no longer current.
  2289. */
  2290. if (ANA_DSCP_CFG_QOS_DSCP_VAL_X(dscp_cfg) != prio)
  2291. return 0;
  2292. /* Untrust this DSCP value */
  2293. ocelot_write_rix(ocelot, 0, ANA_DSCP_CFG, dscp);
  2294. for (i = 0; i < 64; i++) {
  2295. int dscp_cfg = ocelot_read_rix(ocelot, ANA_DSCP_CFG, i);
  2296. /* There are still app table entries on the port, so we need to
  2297. * keep DSCP enabled, nothing to do.
  2298. */
  2299. if (dscp_cfg & ANA_DSCP_CFG_DSCP_TRUST_ENA)
  2300. return 0;
  2301. }
  2302. /* Disable DSCP QoS classification if there isn't any trusted
  2303. * DSCP value left.
  2304. */
  2305. mask = ANA_PORT_QOS_CFG_QOS_DSCP_ENA |
  2306. ANA_PORT_QOS_CFG_DSCP_TRANSLATE_ENA;
  2307. ocelot_rmw_gix(ocelot, 0, mask, ANA_PORT_QOS_CFG, port);
  2308. return 0;
  2309. }
  2310. EXPORT_SYMBOL_GPL(ocelot_port_del_dscp_prio);
  2311. struct ocelot_mirror *ocelot_mirror_get(struct ocelot *ocelot, int to,
  2312. struct netlink_ext_ack *extack)
  2313. {
  2314. struct ocelot_mirror *m = ocelot->mirror;
  2315. if (m) {
  2316. if (m->to != to) {
  2317. NL_SET_ERR_MSG_MOD(extack,
  2318. "Mirroring already configured towards different egress port");
  2319. return ERR_PTR(-EBUSY);
  2320. }
  2321. refcount_inc(&m->refcount);
  2322. return m;
  2323. }
  2324. m = kzalloc(sizeof(*m), GFP_KERNEL);
  2325. if (!m)
  2326. return ERR_PTR(-ENOMEM);
  2327. m->to = to;
  2328. refcount_set(&m->refcount, 1);
  2329. ocelot->mirror = m;
  2330. /* Program the mirror port to hardware */
  2331. ocelot_write(ocelot, BIT(to), ANA_MIRRORPORTS);
  2332. return m;
  2333. }
  2334. void ocelot_mirror_put(struct ocelot *ocelot)
  2335. {
  2336. struct ocelot_mirror *m = ocelot->mirror;
  2337. if (!refcount_dec_and_test(&m->refcount))
  2338. return;
  2339. ocelot_write(ocelot, 0, ANA_MIRRORPORTS);
  2340. ocelot->mirror = NULL;
  2341. kfree(m);
  2342. }
  2343. int ocelot_port_mirror_add(struct ocelot *ocelot, int from, int to,
  2344. bool ingress, struct netlink_ext_ack *extack)
  2345. {
  2346. struct ocelot_mirror *m = ocelot_mirror_get(ocelot, to, extack);
  2347. if (IS_ERR(m))
  2348. return PTR_ERR(m);
  2349. if (ingress) {
  2350. ocelot_rmw_gix(ocelot, ANA_PORT_PORT_CFG_SRC_MIRROR_ENA,
  2351. ANA_PORT_PORT_CFG_SRC_MIRROR_ENA,
  2352. ANA_PORT_PORT_CFG, from);
  2353. } else {
  2354. ocelot_rmw(ocelot, BIT(from), BIT(from),
  2355. ANA_EMIRRORPORTS);
  2356. }
  2357. return 0;
  2358. }
  2359. EXPORT_SYMBOL_GPL(ocelot_port_mirror_add);
  2360. void ocelot_port_mirror_del(struct ocelot *ocelot, int from, bool ingress)
  2361. {
  2362. if (ingress) {
  2363. ocelot_rmw_gix(ocelot, 0, ANA_PORT_PORT_CFG_SRC_MIRROR_ENA,
  2364. ANA_PORT_PORT_CFG, from);
  2365. } else {
  2366. ocelot_rmw(ocelot, 0, BIT(from), ANA_EMIRRORPORTS);
  2367. }
  2368. ocelot_mirror_put(ocelot);
  2369. }
  2370. EXPORT_SYMBOL_GPL(ocelot_port_mirror_del);
  2371. static void ocelot_port_reset_mqprio(struct ocelot *ocelot, int port)
  2372. {
  2373. struct net_device *dev = ocelot->ops->port_to_netdev(ocelot, port);
  2374. netdev_reset_tc(dev);
  2375. ocelot_port_change_fp(ocelot, port, 0);
  2376. }
  2377. int ocelot_port_mqprio(struct ocelot *ocelot, int port,
  2378. struct tc_mqprio_qopt_offload *mqprio)
  2379. {
  2380. struct net_device *dev = ocelot->ops->port_to_netdev(ocelot, port);
  2381. struct netlink_ext_ack *extack = mqprio->extack;
  2382. struct tc_mqprio_qopt *qopt = &mqprio->qopt;
  2383. int num_tc = qopt->num_tc;
  2384. int tc, err;
  2385. if (!num_tc) {
  2386. ocelot_port_reset_mqprio(ocelot, port);
  2387. return 0;
  2388. }
  2389. err = netdev_set_num_tc(dev, num_tc);
  2390. if (err)
  2391. return err;
  2392. for (tc = 0; tc < num_tc; tc++) {
  2393. if (qopt->count[tc] != 1) {
  2394. NL_SET_ERR_MSG_MOD(extack,
  2395. "Only one TXQ per TC supported");
  2396. return -EINVAL;
  2397. }
  2398. err = netdev_set_tc_queue(dev, tc, 1, qopt->offset[tc]);
  2399. if (err)
  2400. goto err_reset_tc;
  2401. }
  2402. err = netif_set_real_num_tx_queues(dev, num_tc);
  2403. if (err)
  2404. goto err_reset_tc;
  2405. ocelot_port_change_fp(ocelot, port, mqprio->preemptible_tcs);
  2406. return 0;
  2407. err_reset_tc:
  2408. ocelot_port_reset_mqprio(ocelot, port);
  2409. return err;
  2410. }
  2411. EXPORT_SYMBOL_GPL(ocelot_port_mqprio);
  2412. void ocelot_init_port(struct ocelot *ocelot, int port)
  2413. {
  2414. struct ocelot_port *ocelot_port = ocelot->ports[port];
  2415. skb_queue_head_init(&ocelot_port->tx_skbs);
  2416. /* Basic L2 initialization */
  2417. /* Set MAC IFG Gaps
  2418. * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
  2419. * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
  2420. */
  2421. ocelot_port_writel(ocelot_port, DEV_MAC_IFG_CFG_TX_IFG(5),
  2422. DEV_MAC_IFG_CFG);
  2423. /* Load seed (0) and set MAC HDX late collision */
  2424. ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
  2425. DEV_MAC_HDX_CFG_SEED_LOAD,
  2426. DEV_MAC_HDX_CFG);
  2427. mdelay(1);
  2428. ocelot_port_writel(ocelot_port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
  2429. DEV_MAC_HDX_CFG);
  2430. /* Set Max Length and maximum tags allowed */
  2431. ocelot_port_set_maxlen(ocelot, port, ETH_DATA_LEN);
  2432. ocelot_port_writel(ocelot_port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
  2433. DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
  2434. DEV_MAC_TAGS_CFG_VLAN_DBL_AWR_ENA |
  2435. DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
  2436. DEV_MAC_TAGS_CFG);
  2437. /* Set SMAC of Pause frame (00:00:00:00:00:00) */
  2438. ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
  2439. ocelot_port_writel(ocelot_port, 0, DEV_MAC_FC_MAC_LOW_CFG);
  2440. /* Enable transmission of pause frames */
  2441. ocelot_fields_write(ocelot, port, SYS_PAUSE_CFG_PAUSE_ENA, 1);
  2442. /* Drop frames with multicast source address */
  2443. ocelot_rmw_gix(ocelot, ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
  2444. ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA,
  2445. ANA_PORT_DROP_CFG, port);
  2446. /* Set default VLAN and tag type to 8021Q. */
  2447. ocelot_rmw_gix(ocelot, REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q),
  2448. REW_PORT_VLAN_CFG_PORT_TPID_M,
  2449. REW_PORT_VLAN_CFG, port);
  2450. /* Disable source address learning for standalone mode */
  2451. ocelot_port_set_learning(ocelot, port, false);
  2452. /* Set the port's initial logical port ID value, enable receiving
  2453. * frames on it, and configure the MAC address learning type to
  2454. * automatic.
  2455. */
  2456. ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
  2457. ANA_PORT_PORT_CFG_RECV_ENA |
  2458. ANA_PORT_PORT_CFG_PORTID_VAL(port),
  2459. ANA_PORT_PORT_CFG, port);
  2460. /* Enable vcap lookups */
  2461. ocelot_vcap_enable(ocelot, port);
  2462. }
  2463. EXPORT_SYMBOL(ocelot_init_port);
  2464. /* Configure and enable the CPU port module, which is a set of queues
  2465. * accessible through register MMIO, frame DMA or Ethernet (in case
  2466. * NPI mode is used).
  2467. */
  2468. static void ocelot_cpu_port_init(struct ocelot *ocelot)
  2469. {
  2470. int cpu = ocelot->num_phys_ports;
  2471. /* The unicast destination PGID for the CPU port module is unused */
  2472. ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
  2473. /* Instead set up a multicast destination PGID for traffic copied to
  2474. * the CPU. Whitelisted MAC addresses like the port netdevice MAC
  2475. * addresses will be copied to the CPU via this PGID.
  2476. */
  2477. ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
  2478. ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
  2479. ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
  2480. ANA_PORT_PORT_CFG, cpu);
  2481. /* Enable CPU port module */
  2482. ocelot_fields_write(ocelot, cpu, QSYS_SWITCH_PORT_MODE_PORT_ENA, 1);
  2483. /* CPU port Injection/Extraction configuration */
  2484. ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_XTR_HDR,
  2485. OCELOT_TAG_PREFIX_NONE);
  2486. ocelot_fields_write(ocelot, cpu, SYS_PORT_MODE_INCL_INJ_HDR,
  2487. OCELOT_TAG_PREFIX_NONE);
  2488. /* Configure the CPU port to be VLAN aware */
  2489. ocelot_write_gix(ocelot,
  2490. ANA_PORT_VLAN_CFG_VLAN_VID(OCELOT_STANDALONE_PVID) |
  2491. ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
  2492. ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
  2493. ANA_PORT_VLAN_CFG, cpu);
  2494. }
  2495. static void ocelot_detect_features(struct ocelot *ocelot)
  2496. {
  2497. int mmgt, eq_ctrl;
  2498. /* For Ocelot, Felix, Seville, Serval etc, SYS:MMGT:MMGT:FREECNT holds
  2499. * the number of 240-byte free memory words (aka 4-cell chunks) and not
  2500. * 192 bytes as the documentation incorrectly says.
  2501. */
  2502. mmgt = ocelot_read(ocelot, SYS_MMGT);
  2503. ocelot->packet_buffer_size = 240 * SYS_MMGT_FREECNT(mmgt);
  2504. eq_ctrl = ocelot_read(ocelot, QSYS_EQ_CTRL);
  2505. ocelot->num_frame_refs = QSYS_MMGT_EQ_CTRL_FP_FREE_CNT(eq_ctrl);
  2506. }
  2507. static int ocelot_mem_init_status(struct ocelot *ocelot)
  2508. {
  2509. unsigned int val;
  2510. int err;
  2511. err = regmap_field_read(ocelot->regfields[SYS_RESET_CFG_MEM_INIT],
  2512. &val);
  2513. return err ?: val;
  2514. }
  2515. int ocelot_reset(struct ocelot *ocelot)
  2516. {
  2517. int err;
  2518. u32 val;
  2519. err = regmap_field_write(ocelot->regfields[SYS_RESET_CFG_MEM_INIT], 1);
  2520. if (err)
  2521. return err;
  2522. err = regmap_field_write(ocelot->regfields[SYS_RESET_CFG_MEM_ENA], 1);
  2523. if (err)
  2524. return err;
  2525. /* MEM_INIT is a self-clearing bit. Wait for it to be cleared (should be
  2526. * 100us) before enabling the switch core.
  2527. */
  2528. err = readx_poll_timeout(ocelot_mem_init_status, ocelot, val, !val,
  2529. MEM_INIT_SLEEP_US, MEM_INIT_TIMEOUT_US);
  2530. if (err)
  2531. return err;
  2532. err = regmap_field_write(ocelot->regfields[SYS_RESET_CFG_MEM_ENA], 1);
  2533. if (err)
  2534. return err;
  2535. return regmap_field_write(ocelot->regfields[SYS_RESET_CFG_CORE_ENA], 1);
  2536. }
  2537. EXPORT_SYMBOL(ocelot_reset);
  2538. int ocelot_init(struct ocelot *ocelot)
  2539. {
  2540. int i, ret;
  2541. u32 port;
  2542. if (ocelot->ops->reset) {
  2543. ret = ocelot->ops->reset(ocelot);
  2544. if (ret) {
  2545. dev_err(ocelot->dev, "Switch reset failed\n");
  2546. return ret;
  2547. }
  2548. }
  2549. mutex_init(&ocelot->mact_lock);
  2550. mutex_init(&ocelot->fwd_domain_lock);
  2551. spin_lock_init(&ocelot->ptp_clock_lock);
  2552. spin_lock_init(&ocelot->ts_id_lock);
  2553. spin_lock_init(&ocelot->inj_lock);
  2554. spin_lock_init(&ocelot->xtr_lock);
  2555. ocelot->owq = alloc_ordered_workqueue("ocelot-owq", 0);
  2556. if (!ocelot->owq)
  2557. return -ENOMEM;
  2558. ret = ocelot_stats_init(ocelot);
  2559. if (ret)
  2560. goto err_stats_init;
  2561. INIT_LIST_HEAD(&ocelot->multicast);
  2562. INIT_LIST_HEAD(&ocelot->pgids);
  2563. INIT_LIST_HEAD(&ocelot->vlans);
  2564. INIT_LIST_HEAD(&ocelot->lag_fdbs);
  2565. ocelot_detect_features(ocelot);
  2566. ocelot_mact_init(ocelot);
  2567. ocelot_vlan_init(ocelot);
  2568. ocelot_vcap_init(ocelot);
  2569. ocelot_cpu_port_init(ocelot);
  2570. if (ocelot->ops->psfp_init)
  2571. ocelot->ops->psfp_init(ocelot);
  2572. if (ocelot->mm_supported) {
  2573. ret = ocelot_mm_init(ocelot);
  2574. if (ret)
  2575. goto err_mm_init;
  2576. }
  2577. for (port = 0; port < ocelot->num_phys_ports; port++) {
  2578. /* Clear all counters (5 groups) */
  2579. ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
  2580. SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
  2581. SYS_STAT_CFG);
  2582. }
  2583. /* Only use S-Tag */
  2584. ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
  2585. /* Aggregation mode */
  2586. ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
  2587. ANA_AGGR_CFG_AC_DMAC_ENA |
  2588. ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
  2589. ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA |
  2590. ANA_AGGR_CFG_AC_IP6_FLOW_LBL_ENA |
  2591. ANA_AGGR_CFG_AC_IP6_TCPUDP_ENA,
  2592. ANA_AGGR_CFG);
  2593. /* Set MAC age time to default value. The entry is aged after
  2594. * 2*AGE_PERIOD
  2595. */
  2596. ocelot_write(ocelot,
  2597. ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
  2598. ANA_AUTOAGE);
  2599. /* Disable learning for frames discarded by VLAN ingress filtering */
  2600. regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
  2601. /* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
  2602. ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
  2603. SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
  2604. /* Setup flooding PGIDs */
  2605. for (i = 0; i < ocelot->num_flooding_pgids; i++)
  2606. ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
  2607. ANA_FLOODING_FLD_BROADCAST(PGID_BC) |
  2608. ANA_FLOODING_FLD_UNICAST(PGID_UC),
  2609. ANA_FLOODING, i);
  2610. ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
  2611. ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
  2612. ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
  2613. ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
  2614. ANA_FLOODING_IPMC);
  2615. for (port = 0; port < ocelot->num_phys_ports; port++) {
  2616. /* Transmit the frame to the local port. */
  2617. ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
  2618. /* Do not forward BPDU frames to the front ports. */
  2619. ocelot_write_gix(ocelot,
  2620. ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
  2621. ANA_PORT_CPU_FWD_BPDU_CFG,
  2622. port);
  2623. /* Ensure bridging is disabled */
  2624. ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
  2625. }
  2626. for_each_nonreserved_multicast_dest_pgid(ocelot, i) {
  2627. u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
  2628. ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
  2629. }
  2630. ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_BLACKHOLE);
  2631. /* Allow broadcast and unknown L2 multicast to the CPU. */
  2632. ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
  2633. ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
  2634. ANA_PGID_PGID, PGID_MC);
  2635. ocelot_rmw_rix(ocelot, ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
  2636. ANA_PGID_PGID_PGID(BIT(ocelot->num_phys_ports)),
  2637. ANA_PGID_PGID, PGID_BC);
  2638. ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
  2639. ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
  2640. /* Allow manual injection via DEVCPU_QS registers, and byte swap these
  2641. * registers endianness.
  2642. */
  2643. ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
  2644. QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
  2645. ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
  2646. QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
  2647. ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
  2648. ANA_CPUQ_CFG_CPUQ_LRN(2) |
  2649. ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
  2650. ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
  2651. ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
  2652. ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
  2653. ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
  2654. ANA_CPUQ_CFG_CPUQ_IGMP(6) |
  2655. ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
  2656. for (i = 0; i < 16; i++)
  2657. ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
  2658. ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
  2659. ANA_CPUQ_8021_CFG, i);
  2660. return 0;
  2661. err_mm_init:
  2662. ocelot_stats_deinit(ocelot);
  2663. err_stats_init:
  2664. destroy_workqueue(ocelot->owq);
  2665. return ret;
  2666. }
  2667. EXPORT_SYMBOL(ocelot_init);
  2668. void ocelot_deinit(struct ocelot *ocelot)
  2669. {
  2670. ocelot_stats_deinit(ocelot);
  2671. destroy_workqueue(ocelot->owq);
  2672. }
  2673. EXPORT_SYMBOL(ocelot_deinit);
  2674. void ocelot_deinit_port(struct ocelot *ocelot, int port)
  2675. {
  2676. struct ocelot_port *ocelot_port = ocelot->ports[port];
  2677. skb_queue_purge(&ocelot_port->tx_skbs);
  2678. }
  2679. EXPORT_SYMBOL(ocelot_deinit_port);
  2680. MODULE_DESCRIPTION("Microsemi Ocelot switch family library");
  2681. MODULE_LICENSE("Dual MIT/GPL");