ocelot.c 76 KB

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