dpaa2-switch.c 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * DPAA2 Ethernet Switch driver
  4. *
  5. * Copyright 2014-2016 Freescale Semiconductor Inc.
  6. * Copyright 2017-2021 NXP
  7. *
  8. */
  9. #include <linux/module.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/kthread.h>
  12. #include <linux/workqueue.h>
  13. #include <linux/iommu.h>
  14. #include <net/pkt_cls.h>
  15. #include <linux/fsl/mc.h>
  16. #include "dpaa2-switch.h"
  17. /* Minimal supported DPSW version */
  18. #define DPSW_MIN_VER_MAJOR 8
  19. #define DPSW_MIN_VER_MINOR 9
  20. #define DEFAULT_VLAN_ID 1
  21. static u16 dpaa2_switch_port_get_fdb_id(struct ethsw_port_priv *port_priv)
  22. {
  23. return port_priv->fdb->fdb_id;
  24. }
  25. static struct dpaa2_switch_fdb *dpaa2_switch_fdb_get_unused(struct ethsw_core *ethsw)
  26. {
  27. int i;
  28. for (i = 0; i < ethsw->sw_attr.num_ifs; i++)
  29. if (!ethsw->fdbs[i].in_use)
  30. return &ethsw->fdbs[i];
  31. return NULL;
  32. }
  33. static struct dpaa2_switch_filter_block *
  34. dpaa2_switch_filter_block_get_unused(struct ethsw_core *ethsw)
  35. {
  36. int i;
  37. for (i = 0; i < ethsw->sw_attr.num_ifs; i++)
  38. if (!ethsw->filter_blocks[i].in_use)
  39. return &ethsw->filter_blocks[i];
  40. return NULL;
  41. }
  42. static u16 dpaa2_switch_port_set_fdb(struct ethsw_port_priv *port_priv,
  43. struct net_device *bridge_dev)
  44. {
  45. struct ethsw_port_priv *other_port_priv = NULL;
  46. struct dpaa2_switch_fdb *fdb;
  47. struct net_device *other_dev;
  48. struct list_head *iter;
  49. /* If we leave a bridge (bridge_dev is NULL), find an unused
  50. * FDB and use that.
  51. */
  52. if (!bridge_dev) {
  53. fdb = dpaa2_switch_fdb_get_unused(port_priv->ethsw_data);
  54. /* If there is no unused FDB, we must be the last port that
  55. * leaves the last bridge, all the others are standalone. We
  56. * can just keep the FDB that we already have.
  57. */
  58. if (!fdb) {
  59. port_priv->fdb->bridge_dev = NULL;
  60. return 0;
  61. }
  62. port_priv->fdb = fdb;
  63. port_priv->fdb->in_use = true;
  64. port_priv->fdb->bridge_dev = NULL;
  65. return 0;
  66. }
  67. /* The below call to netdev_for_each_lower_dev() demands the RTNL lock
  68. * being held. Assert on it so that it's easier to catch new code
  69. * paths that reach this point without the RTNL lock.
  70. */
  71. ASSERT_RTNL();
  72. /* If part of a bridge, use the FDB of the first dpaa2 switch interface
  73. * to be present in that bridge
  74. */
  75. netdev_for_each_lower_dev(bridge_dev, other_dev, iter) {
  76. if (!dpaa2_switch_port_dev_check(other_dev))
  77. continue;
  78. if (other_dev == port_priv->netdev)
  79. continue;
  80. other_port_priv = netdev_priv(other_dev);
  81. break;
  82. }
  83. /* The current port is about to change its FDB to the one used by the
  84. * first port that joined the bridge.
  85. */
  86. if (other_port_priv) {
  87. /* The previous FDB is about to become unused, since the
  88. * interface is no longer standalone.
  89. */
  90. port_priv->fdb->in_use = false;
  91. port_priv->fdb->bridge_dev = NULL;
  92. /* Get a reference to the new FDB */
  93. port_priv->fdb = other_port_priv->fdb;
  94. }
  95. /* Keep track of the new upper bridge device */
  96. port_priv->fdb->bridge_dev = bridge_dev;
  97. return 0;
  98. }
  99. static void dpaa2_switch_fdb_get_flood_cfg(struct ethsw_core *ethsw, u16 fdb_id,
  100. enum dpsw_flood_type type,
  101. struct dpsw_egress_flood_cfg *cfg)
  102. {
  103. int i = 0, j;
  104. memset(cfg, 0, sizeof(*cfg));
  105. /* Add all the DPAA2 switch ports found in the same bridging domain to
  106. * the egress flooding domain
  107. */
  108. for (j = 0; j < ethsw->sw_attr.num_ifs; j++) {
  109. if (!ethsw->ports[j])
  110. continue;
  111. if (ethsw->ports[j]->fdb->fdb_id != fdb_id)
  112. continue;
  113. if (type == DPSW_BROADCAST && ethsw->ports[j]->bcast_flood)
  114. cfg->if_id[i++] = ethsw->ports[j]->idx;
  115. else if (type == DPSW_FLOODING && ethsw->ports[j]->ucast_flood)
  116. cfg->if_id[i++] = ethsw->ports[j]->idx;
  117. }
  118. /* Add the CTRL interface to the egress flooding domain */
  119. cfg->if_id[i++] = ethsw->sw_attr.num_ifs;
  120. cfg->fdb_id = fdb_id;
  121. cfg->flood_type = type;
  122. cfg->num_ifs = i;
  123. }
  124. static int dpaa2_switch_fdb_set_egress_flood(struct ethsw_core *ethsw, u16 fdb_id)
  125. {
  126. struct dpsw_egress_flood_cfg flood_cfg;
  127. int err;
  128. /* Setup broadcast flooding domain */
  129. dpaa2_switch_fdb_get_flood_cfg(ethsw, fdb_id, DPSW_BROADCAST, &flood_cfg);
  130. err = dpsw_set_egress_flood(ethsw->mc_io, 0, ethsw->dpsw_handle,
  131. &flood_cfg);
  132. if (err) {
  133. dev_err(ethsw->dev, "dpsw_set_egress_flood() = %d\n", err);
  134. return err;
  135. }
  136. /* Setup unknown flooding domain */
  137. dpaa2_switch_fdb_get_flood_cfg(ethsw, fdb_id, DPSW_FLOODING, &flood_cfg);
  138. err = dpsw_set_egress_flood(ethsw->mc_io, 0, ethsw->dpsw_handle,
  139. &flood_cfg);
  140. if (err) {
  141. dev_err(ethsw->dev, "dpsw_set_egress_flood() = %d\n", err);
  142. return err;
  143. }
  144. return 0;
  145. }
  146. static void *dpaa2_iova_to_virt(struct iommu_domain *domain,
  147. dma_addr_t iova_addr)
  148. {
  149. phys_addr_t phys_addr;
  150. phys_addr = domain ? iommu_iova_to_phys(domain, iova_addr) : iova_addr;
  151. return phys_to_virt(phys_addr);
  152. }
  153. static int dpaa2_switch_add_vlan(struct ethsw_port_priv *port_priv, u16 vid)
  154. {
  155. struct ethsw_core *ethsw = port_priv->ethsw_data;
  156. struct dpsw_vlan_cfg vcfg = {0};
  157. int err;
  158. vcfg.fdb_id = dpaa2_switch_port_get_fdb_id(port_priv);
  159. err = dpsw_vlan_add(ethsw->mc_io, 0,
  160. ethsw->dpsw_handle, vid, &vcfg);
  161. if (err) {
  162. dev_err(ethsw->dev, "dpsw_vlan_add err %d\n", err);
  163. return err;
  164. }
  165. ethsw->vlans[vid] = ETHSW_VLAN_MEMBER;
  166. return 0;
  167. }
  168. static bool dpaa2_switch_port_is_up(struct ethsw_port_priv *port_priv)
  169. {
  170. struct net_device *netdev = port_priv->netdev;
  171. struct dpsw_link_state state;
  172. int err;
  173. err = dpsw_if_get_link_state(port_priv->ethsw_data->mc_io, 0,
  174. port_priv->ethsw_data->dpsw_handle,
  175. port_priv->idx, &state);
  176. if (err) {
  177. netdev_err(netdev, "dpsw_if_get_link_state() err %d\n", err);
  178. return true;
  179. }
  180. WARN_ONCE(state.up > 1, "Garbage read into link_state");
  181. return state.up ? true : false;
  182. }
  183. static int dpaa2_switch_port_set_pvid(struct ethsw_port_priv *port_priv, u16 pvid)
  184. {
  185. struct ethsw_core *ethsw = port_priv->ethsw_data;
  186. struct net_device *netdev = port_priv->netdev;
  187. struct dpsw_tci_cfg tci_cfg = { 0 };
  188. bool up;
  189. int err, ret;
  190. err = dpsw_if_get_tci(ethsw->mc_io, 0, ethsw->dpsw_handle,
  191. port_priv->idx, &tci_cfg);
  192. if (err) {
  193. netdev_err(netdev, "dpsw_if_get_tci err %d\n", err);
  194. return err;
  195. }
  196. tci_cfg.vlan_id = pvid;
  197. /* Interface needs to be down to change PVID */
  198. up = dpaa2_switch_port_is_up(port_priv);
  199. if (up) {
  200. err = dpsw_if_disable(ethsw->mc_io, 0,
  201. ethsw->dpsw_handle,
  202. port_priv->idx);
  203. if (err) {
  204. netdev_err(netdev, "dpsw_if_disable err %d\n", err);
  205. return err;
  206. }
  207. }
  208. err = dpsw_if_set_tci(ethsw->mc_io, 0, ethsw->dpsw_handle,
  209. port_priv->idx, &tci_cfg);
  210. if (err) {
  211. netdev_err(netdev, "dpsw_if_set_tci err %d\n", err);
  212. goto set_tci_error;
  213. }
  214. /* Delete previous PVID info and mark the new one */
  215. port_priv->vlans[port_priv->pvid] &= ~ETHSW_VLAN_PVID;
  216. port_priv->vlans[pvid] |= ETHSW_VLAN_PVID;
  217. port_priv->pvid = pvid;
  218. set_tci_error:
  219. if (up) {
  220. ret = dpsw_if_enable(ethsw->mc_io, 0,
  221. ethsw->dpsw_handle,
  222. port_priv->idx);
  223. if (ret) {
  224. netdev_err(netdev, "dpsw_if_enable err %d\n", ret);
  225. return ret;
  226. }
  227. }
  228. return err;
  229. }
  230. static int dpaa2_switch_port_add_vlan(struct ethsw_port_priv *port_priv,
  231. u16 vid, u16 flags)
  232. {
  233. struct ethsw_core *ethsw = port_priv->ethsw_data;
  234. struct net_device *netdev = port_priv->netdev;
  235. struct dpsw_vlan_if_cfg vcfg = {0};
  236. int err;
  237. if (port_priv->vlans[vid]) {
  238. netdev_err(netdev, "VLAN %d already configured\n", vid);
  239. return -EEXIST;
  240. }
  241. /* If hit, this VLAN rule will lead the packet into the FDB table
  242. * specified in the vlan configuration below
  243. */
  244. vcfg.num_ifs = 1;
  245. vcfg.if_id[0] = port_priv->idx;
  246. vcfg.fdb_id = dpaa2_switch_port_get_fdb_id(port_priv);
  247. vcfg.options |= DPSW_VLAN_ADD_IF_OPT_FDB_ID;
  248. err = dpsw_vlan_add_if(ethsw->mc_io, 0, ethsw->dpsw_handle, vid, &vcfg);
  249. if (err) {
  250. netdev_err(netdev, "dpsw_vlan_add_if err %d\n", err);
  251. return err;
  252. }
  253. port_priv->vlans[vid] = ETHSW_VLAN_MEMBER;
  254. if (flags & BRIDGE_VLAN_INFO_UNTAGGED) {
  255. err = dpsw_vlan_add_if_untagged(ethsw->mc_io, 0,
  256. ethsw->dpsw_handle,
  257. vid, &vcfg);
  258. if (err) {
  259. netdev_err(netdev,
  260. "dpsw_vlan_add_if_untagged err %d\n", err);
  261. return err;
  262. }
  263. port_priv->vlans[vid] |= ETHSW_VLAN_UNTAGGED;
  264. }
  265. if (flags & BRIDGE_VLAN_INFO_PVID) {
  266. err = dpaa2_switch_port_set_pvid(port_priv, vid);
  267. if (err)
  268. return err;
  269. }
  270. return 0;
  271. }
  272. static enum dpsw_stp_state br_stp_state_to_dpsw(u8 state)
  273. {
  274. switch (state) {
  275. case BR_STATE_DISABLED:
  276. return DPSW_STP_STATE_DISABLED;
  277. case BR_STATE_LISTENING:
  278. return DPSW_STP_STATE_LISTENING;
  279. case BR_STATE_LEARNING:
  280. return DPSW_STP_STATE_LEARNING;
  281. case BR_STATE_FORWARDING:
  282. return DPSW_STP_STATE_FORWARDING;
  283. case BR_STATE_BLOCKING:
  284. return DPSW_STP_STATE_BLOCKING;
  285. default:
  286. return DPSW_STP_STATE_DISABLED;
  287. }
  288. }
  289. static int dpaa2_switch_port_set_stp_state(struct ethsw_port_priv *port_priv, u8 state)
  290. {
  291. struct dpsw_stp_cfg stp_cfg = {0};
  292. int err;
  293. u16 vid;
  294. if (!netif_running(port_priv->netdev) || state == port_priv->stp_state)
  295. return 0; /* Nothing to do */
  296. stp_cfg.state = br_stp_state_to_dpsw(state);
  297. for (vid = 0; vid <= VLAN_VID_MASK; vid++) {
  298. if (port_priv->vlans[vid] & ETHSW_VLAN_MEMBER) {
  299. stp_cfg.vlan_id = vid;
  300. err = dpsw_if_set_stp(port_priv->ethsw_data->mc_io, 0,
  301. port_priv->ethsw_data->dpsw_handle,
  302. port_priv->idx, &stp_cfg);
  303. if (err) {
  304. netdev_err(port_priv->netdev,
  305. "dpsw_if_set_stp err %d\n", err);
  306. return err;
  307. }
  308. }
  309. }
  310. port_priv->stp_state = state;
  311. return 0;
  312. }
  313. static int dpaa2_switch_dellink(struct ethsw_core *ethsw, u16 vid)
  314. {
  315. struct ethsw_port_priv *ppriv_local = NULL;
  316. int i, err;
  317. if (!ethsw->vlans[vid])
  318. return -ENOENT;
  319. err = dpsw_vlan_remove(ethsw->mc_io, 0, ethsw->dpsw_handle, vid);
  320. if (err) {
  321. dev_err(ethsw->dev, "dpsw_vlan_remove err %d\n", err);
  322. return err;
  323. }
  324. ethsw->vlans[vid] = 0;
  325. for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
  326. ppriv_local = ethsw->ports[i];
  327. if (ppriv_local)
  328. ppriv_local->vlans[vid] = 0;
  329. }
  330. return 0;
  331. }
  332. static int dpaa2_switch_port_fdb_add_uc(struct ethsw_port_priv *port_priv,
  333. const unsigned char *addr)
  334. {
  335. struct dpsw_fdb_unicast_cfg entry = {0};
  336. u16 fdb_id;
  337. int err;
  338. entry.if_egress = port_priv->idx;
  339. entry.type = DPSW_FDB_ENTRY_STATIC;
  340. ether_addr_copy(entry.mac_addr, addr);
  341. fdb_id = dpaa2_switch_port_get_fdb_id(port_priv);
  342. err = dpsw_fdb_add_unicast(port_priv->ethsw_data->mc_io, 0,
  343. port_priv->ethsw_data->dpsw_handle,
  344. fdb_id, &entry);
  345. if (err)
  346. netdev_err(port_priv->netdev,
  347. "dpsw_fdb_add_unicast err %d\n", err);
  348. return err;
  349. }
  350. static int dpaa2_switch_port_fdb_del_uc(struct ethsw_port_priv *port_priv,
  351. const unsigned char *addr)
  352. {
  353. struct dpsw_fdb_unicast_cfg entry = {0};
  354. u16 fdb_id;
  355. int err;
  356. entry.if_egress = port_priv->idx;
  357. entry.type = DPSW_FDB_ENTRY_STATIC;
  358. ether_addr_copy(entry.mac_addr, addr);
  359. fdb_id = dpaa2_switch_port_get_fdb_id(port_priv);
  360. err = dpsw_fdb_remove_unicast(port_priv->ethsw_data->mc_io, 0,
  361. port_priv->ethsw_data->dpsw_handle,
  362. fdb_id, &entry);
  363. /* Silently discard error for calling multiple times the del command */
  364. if (err && err != -ENXIO)
  365. netdev_err(port_priv->netdev,
  366. "dpsw_fdb_remove_unicast err %d\n", err);
  367. return err;
  368. }
  369. static int dpaa2_switch_port_fdb_add_mc(struct ethsw_port_priv *port_priv,
  370. const unsigned char *addr)
  371. {
  372. struct dpsw_fdb_multicast_cfg entry = {0};
  373. u16 fdb_id;
  374. int err;
  375. ether_addr_copy(entry.mac_addr, addr);
  376. entry.type = DPSW_FDB_ENTRY_STATIC;
  377. entry.num_ifs = 1;
  378. entry.if_id[0] = port_priv->idx;
  379. fdb_id = dpaa2_switch_port_get_fdb_id(port_priv);
  380. err = dpsw_fdb_add_multicast(port_priv->ethsw_data->mc_io, 0,
  381. port_priv->ethsw_data->dpsw_handle,
  382. fdb_id, &entry);
  383. /* Silently discard error for calling multiple times the add command */
  384. if (err && err != -ENXIO)
  385. netdev_err(port_priv->netdev, "dpsw_fdb_add_multicast err %d\n",
  386. err);
  387. return err;
  388. }
  389. static int dpaa2_switch_port_fdb_del_mc(struct ethsw_port_priv *port_priv,
  390. const unsigned char *addr)
  391. {
  392. struct dpsw_fdb_multicast_cfg entry = {0};
  393. u16 fdb_id;
  394. int err;
  395. ether_addr_copy(entry.mac_addr, addr);
  396. entry.type = DPSW_FDB_ENTRY_STATIC;
  397. entry.num_ifs = 1;
  398. entry.if_id[0] = port_priv->idx;
  399. fdb_id = dpaa2_switch_port_get_fdb_id(port_priv);
  400. err = dpsw_fdb_remove_multicast(port_priv->ethsw_data->mc_io, 0,
  401. port_priv->ethsw_data->dpsw_handle,
  402. fdb_id, &entry);
  403. /* Silently discard error for calling multiple times the del command */
  404. if (err && err != -ENAVAIL)
  405. netdev_err(port_priv->netdev,
  406. "dpsw_fdb_remove_multicast err %d\n", err);
  407. return err;
  408. }
  409. static void dpaa2_switch_port_get_stats(struct net_device *netdev,
  410. struct rtnl_link_stats64 *stats)
  411. {
  412. struct ethsw_port_priv *port_priv = netdev_priv(netdev);
  413. u64 tmp;
  414. int err;
  415. err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
  416. port_priv->ethsw_data->dpsw_handle,
  417. port_priv->idx,
  418. DPSW_CNT_ING_FRAME, &stats->rx_packets);
  419. if (err)
  420. goto error;
  421. err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
  422. port_priv->ethsw_data->dpsw_handle,
  423. port_priv->idx,
  424. DPSW_CNT_EGR_FRAME, &stats->tx_packets);
  425. if (err)
  426. goto error;
  427. err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
  428. port_priv->ethsw_data->dpsw_handle,
  429. port_priv->idx,
  430. DPSW_CNT_ING_BYTE, &stats->rx_bytes);
  431. if (err)
  432. goto error;
  433. err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
  434. port_priv->ethsw_data->dpsw_handle,
  435. port_priv->idx,
  436. DPSW_CNT_EGR_BYTE, &stats->tx_bytes);
  437. if (err)
  438. goto error;
  439. err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
  440. port_priv->ethsw_data->dpsw_handle,
  441. port_priv->idx,
  442. DPSW_CNT_ING_FRAME_DISCARD,
  443. &stats->rx_dropped);
  444. if (err)
  445. goto error;
  446. err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
  447. port_priv->ethsw_data->dpsw_handle,
  448. port_priv->idx,
  449. DPSW_CNT_ING_FLTR_FRAME,
  450. &tmp);
  451. if (err)
  452. goto error;
  453. stats->rx_dropped += tmp;
  454. err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
  455. port_priv->ethsw_data->dpsw_handle,
  456. port_priv->idx,
  457. DPSW_CNT_EGR_FRAME_DISCARD,
  458. &stats->tx_dropped);
  459. if (err)
  460. goto error;
  461. return;
  462. error:
  463. netdev_err(netdev, "dpsw_if_get_counter err %d\n", err);
  464. }
  465. static bool dpaa2_switch_port_has_offload_stats(const struct net_device *netdev,
  466. int attr_id)
  467. {
  468. return (attr_id == IFLA_OFFLOAD_XSTATS_CPU_HIT);
  469. }
  470. static int dpaa2_switch_port_get_offload_stats(int attr_id,
  471. const struct net_device *netdev,
  472. void *sp)
  473. {
  474. switch (attr_id) {
  475. case IFLA_OFFLOAD_XSTATS_CPU_HIT:
  476. dpaa2_switch_port_get_stats((struct net_device *)netdev, sp);
  477. return 0;
  478. }
  479. return -EINVAL;
  480. }
  481. static int dpaa2_switch_port_change_mtu(struct net_device *netdev, int mtu)
  482. {
  483. struct ethsw_port_priv *port_priv = netdev_priv(netdev);
  484. int err;
  485. err = dpsw_if_set_max_frame_length(port_priv->ethsw_data->mc_io,
  486. 0,
  487. port_priv->ethsw_data->dpsw_handle,
  488. port_priv->idx,
  489. (u16)ETHSW_L2_MAX_FRM(mtu));
  490. if (err) {
  491. netdev_err(netdev,
  492. "dpsw_if_set_max_frame_length() err %d\n", err);
  493. return err;
  494. }
  495. WRITE_ONCE(netdev->mtu, mtu);
  496. return 0;
  497. }
  498. static int dpaa2_switch_port_link_state_update(struct net_device *netdev)
  499. {
  500. struct ethsw_port_priv *port_priv = netdev_priv(netdev);
  501. struct dpsw_link_state state;
  502. int err;
  503. /* When we manage the MAC/PHY using phylink there is no need
  504. * to manually update the netif_carrier.
  505. * We can avoid locking because we are called from the "link changed"
  506. * IRQ handler, which is the same as the "endpoint changed" IRQ handler
  507. * (the writer to port_priv->mac), so we cannot race with it.
  508. */
  509. if (dpaa2_mac_is_type_phy(port_priv->mac))
  510. return 0;
  511. /* Interrupts are received even though no one issued an 'ifconfig up'
  512. * on the switch interface. Ignore these link state update interrupts
  513. */
  514. if (!netif_running(netdev))
  515. return 0;
  516. err = dpsw_if_get_link_state(port_priv->ethsw_data->mc_io, 0,
  517. port_priv->ethsw_data->dpsw_handle,
  518. port_priv->idx, &state);
  519. if (err) {
  520. netdev_err(netdev, "dpsw_if_get_link_state() err %d\n", err);
  521. return err;
  522. }
  523. WARN_ONCE(state.up > 1, "Garbage read into link_state");
  524. if (state.up != port_priv->link_state) {
  525. if (state.up) {
  526. netif_carrier_on(netdev);
  527. netif_tx_start_all_queues(netdev);
  528. } else {
  529. netif_carrier_off(netdev);
  530. netif_tx_stop_all_queues(netdev);
  531. }
  532. port_priv->link_state = state.up;
  533. }
  534. return 0;
  535. }
  536. /* Manage all NAPI instances for the control interface.
  537. *
  538. * We only have one RX queue and one Tx Conf queue for all
  539. * switch ports. Therefore, we only need to enable the NAPI instance once, the
  540. * first time one of the switch ports runs .dev_open().
  541. */
  542. static void dpaa2_switch_enable_ctrl_if_napi(struct ethsw_core *ethsw)
  543. {
  544. int i;
  545. /* Access to the ethsw->napi_users relies on the RTNL lock */
  546. ASSERT_RTNL();
  547. /* a new interface is using the NAPI instance */
  548. ethsw->napi_users++;
  549. /* if there is already a user of the instance, return */
  550. if (ethsw->napi_users > 1)
  551. return;
  552. for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++)
  553. napi_enable(&ethsw->fq[i].napi);
  554. }
  555. static void dpaa2_switch_disable_ctrl_if_napi(struct ethsw_core *ethsw)
  556. {
  557. int i;
  558. /* Access to the ethsw->napi_users relies on the RTNL lock */
  559. ASSERT_RTNL();
  560. /* If we are not the last interface using the NAPI, return */
  561. ethsw->napi_users--;
  562. if (ethsw->napi_users)
  563. return;
  564. for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++)
  565. napi_disable(&ethsw->fq[i].napi);
  566. }
  567. static int dpaa2_switch_port_open(struct net_device *netdev)
  568. {
  569. struct ethsw_port_priv *port_priv = netdev_priv(netdev);
  570. struct ethsw_core *ethsw = port_priv->ethsw_data;
  571. int err;
  572. mutex_lock(&port_priv->mac_lock);
  573. if (!dpaa2_switch_port_is_type_phy(port_priv)) {
  574. /* Explicitly set carrier off, otherwise
  575. * netif_carrier_ok() will return true and cause 'ip link show'
  576. * to report the LOWER_UP flag, even though the link
  577. * notification wasn't even received.
  578. */
  579. netif_carrier_off(netdev);
  580. }
  581. err = dpsw_if_enable(port_priv->ethsw_data->mc_io, 0,
  582. port_priv->ethsw_data->dpsw_handle,
  583. port_priv->idx);
  584. if (err) {
  585. mutex_unlock(&port_priv->mac_lock);
  586. netdev_err(netdev, "dpsw_if_enable err %d\n", err);
  587. return err;
  588. }
  589. dpaa2_switch_enable_ctrl_if_napi(ethsw);
  590. if (dpaa2_switch_port_is_type_phy(port_priv))
  591. dpaa2_mac_start(port_priv->mac);
  592. mutex_unlock(&port_priv->mac_lock);
  593. return 0;
  594. }
  595. static int dpaa2_switch_port_stop(struct net_device *netdev)
  596. {
  597. struct ethsw_port_priv *port_priv = netdev_priv(netdev);
  598. struct ethsw_core *ethsw = port_priv->ethsw_data;
  599. int err;
  600. mutex_lock(&port_priv->mac_lock);
  601. if (dpaa2_switch_port_is_type_phy(port_priv)) {
  602. dpaa2_mac_stop(port_priv->mac);
  603. } else {
  604. netif_tx_stop_all_queues(netdev);
  605. netif_carrier_off(netdev);
  606. }
  607. mutex_unlock(&port_priv->mac_lock);
  608. err = dpsw_if_disable(port_priv->ethsw_data->mc_io, 0,
  609. port_priv->ethsw_data->dpsw_handle,
  610. port_priv->idx);
  611. if (err) {
  612. netdev_err(netdev, "dpsw_if_disable err %d\n", err);
  613. return err;
  614. }
  615. dpaa2_switch_disable_ctrl_if_napi(ethsw);
  616. return 0;
  617. }
  618. static int dpaa2_switch_port_parent_id(struct net_device *dev,
  619. struct netdev_phys_item_id *ppid)
  620. {
  621. struct ethsw_port_priv *port_priv = netdev_priv(dev);
  622. ppid->id_len = 1;
  623. ppid->id[0] = port_priv->ethsw_data->dev_id;
  624. return 0;
  625. }
  626. static int dpaa2_switch_port_get_phys_name(struct net_device *netdev, char *name,
  627. size_t len)
  628. {
  629. struct ethsw_port_priv *port_priv = netdev_priv(netdev);
  630. int err;
  631. err = snprintf(name, len, "p%d", port_priv->idx);
  632. if (err >= len)
  633. return -EINVAL;
  634. return 0;
  635. }
  636. struct ethsw_dump_ctx {
  637. struct net_device *dev;
  638. struct sk_buff *skb;
  639. struct netlink_callback *cb;
  640. int idx;
  641. };
  642. static int dpaa2_switch_fdb_dump_nl(struct fdb_dump_entry *entry,
  643. struct ethsw_dump_ctx *dump)
  644. {
  645. struct ndo_fdb_dump_context *ctx = (void *)dump->cb->ctx;
  646. int is_dynamic = entry->type & DPSW_FDB_ENTRY_DINAMIC;
  647. u32 portid = NETLINK_CB(dump->cb->skb).portid;
  648. u32 seq = dump->cb->nlh->nlmsg_seq;
  649. struct nlmsghdr *nlh;
  650. struct ndmsg *ndm;
  651. if (dump->idx < ctx->fdb_idx)
  652. goto skip;
  653. nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
  654. sizeof(*ndm), NLM_F_MULTI);
  655. if (!nlh)
  656. return -EMSGSIZE;
  657. ndm = nlmsg_data(nlh);
  658. ndm->ndm_family = AF_BRIDGE;
  659. ndm->ndm_pad1 = 0;
  660. ndm->ndm_pad2 = 0;
  661. ndm->ndm_flags = NTF_SELF;
  662. ndm->ndm_type = 0;
  663. ndm->ndm_ifindex = dump->dev->ifindex;
  664. ndm->ndm_state = is_dynamic ? NUD_REACHABLE : NUD_NOARP;
  665. if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, entry->mac_addr))
  666. goto nla_put_failure;
  667. nlmsg_end(dump->skb, nlh);
  668. skip:
  669. dump->idx++;
  670. return 0;
  671. nla_put_failure:
  672. nlmsg_cancel(dump->skb, nlh);
  673. return -EMSGSIZE;
  674. }
  675. static int dpaa2_switch_port_fdb_valid_entry(struct fdb_dump_entry *entry,
  676. struct ethsw_port_priv *port_priv)
  677. {
  678. int idx = port_priv->idx;
  679. int valid;
  680. if (entry->type & DPSW_FDB_ENTRY_TYPE_UNICAST)
  681. valid = entry->if_info == port_priv->idx;
  682. else
  683. valid = entry->if_mask[idx / 8] & BIT(idx % 8);
  684. return valid;
  685. }
  686. static int dpaa2_switch_fdb_iterate(struct ethsw_port_priv *port_priv,
  687. dpaa2_switch_fdb_cb_t cb, void *data)
  688. {
  689. struct net_device *net_dev = port_priv->netdev;
  690. struct ethsw_core *ethsw = port_priv->ethsw_data;
  691. struct device *dev = net_dev->dev.parent;
  692. struct fdb_dump_entry *fdb_entries;
  693. struct fdb_dump_entry fdb_entry;
  694. dma_addr_t fdb_dump_iova;
  695. u16 num_fdb_entries;
  696. u32 fdb_dump_size;
  697. int err = 0, i;
  698. u8 *dma_mem;
  699. u16 fdb_id;
  700. fdb_dump_size = ethsw->sw_attr.max_fdb_entries * sizeof(fdb_entry);
  701. dma_mem = kzalloc(fdb_dump_size, GFP_KERNEL);
  702. if (!dma_mem)
  703. return -ENOMEM;
  704. fdb_dump_iova = dma_map_single(dev, dma_mem, fdb_dump_size,
  705. DMA_FROM_DEVICE);
  706. if (dma_mapping_error(dev, fdb_dump_iova)) {
  707. netdev_err(net_dev, "dma_map_single() failed\n");
  708. err = -ENOMEM;
  709. goto err_map;
  710. }
  711. fdb_id = dpaa2_switch_port_get_fdb_id(port_priv);
  712. err = dpsw_fdb_dump(ethsw->mc_io, 0, ethsw->dpsw_handle, fdb_id,
  713. fdb_dump_iova, fdb_dump_size, &num_fdb_entries);
  714. if (err) {
  715. netdev_err(net_dev, "dpsw_fdb_dump() = %d\n", err);
  716. goto err_dump;
  717. }
  718. dma_unmap_single(dev, fdb_dump_iova, fdb_dump_size, DMA_FROM_DEVICE);
  719. fdb_entries = (struct fdb_dump_entry *)dma_mem;
  720. for (i = 0; i < num_fdb_entries; i++) {
  721. fdb_entry = fdb_entries[i];
  722. err = cb(port_priv, &fdb_entry, data);
  723. if (err)
  724. goto end;
  725. }
  726. end:
  727. kfree(dma_mem);
  728. return 0;
  729. err_dump:
  730. dma_unmap_single(dev, fdb_dump_iova, fdb_dump_size, DMA_TO_DEVICE);
  731. err_map:
  732. kfree(dma_mem);
  733. return err;
  734. }
  735. static int dpaa2_switch_fdb_entry_dump(struct ethsw_port_priv *port_priv,
  736. struct fdb_dump_entry *fdb_entry,
  737. void *data)
  738. {
  739. if (!dpaa2_switch_port_fdb_valid_entry(fdb_entry, port_priv))
  740. return 0;
  741. return dpaa2_switch_fdb_dump_nl(fdb_entry, data);
  742. }
  743. static int dpaa2_switch_port_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
  744. struct net_device *net_dev,
  745. struct net_device *filter_dev, int *idx)
  746. {
  747. struct ethsw_port_priv *port_priv = netdev_priv(net_dev);
  748. struct ethsw_dump_ctx dump = {
  749. .dev = net_dev,
  750. .skb = skb,
  751. .cb = cb,
  752. .idx = *idx,
  753. };
  754. int err;
  755. err = dpaa2_switch_fdb_iterate(port_priv, dpaa2_switch_fdb_entry_dump, &dump);
  756. *idx = dump.idx;
  757. return err;
  758. }
  759. static int dpaa2_switch_fdb_entry_fast_age(struct ethsw_port_priv *port_priv,
  760. struct fdb_dump_entry *fdb_entry,
  761. void *data __always_unused)
  762. {
  763. if (!dpaa2_switch_port_fdb_valid_entry(fdb_entry, port_priv))
  764. return 0;
  765. if (!(fdb_entry->type & DPSW_FDB_ENTRY_TYPE_DYNAMIC))
  766. return 0;
  767. if (fdb_entry->type & DPSW_FDB_ENTRY_TYPE_UNICAST)
  768. dpaa2_switch_port_fdb_del_uc(port_priv, fdb_entry->mac_addr);
  769. else
  770. dpaa2_switch_port_fdb_del_mc(port_priv, fdb_entry->mac_addr);
  771. return 0;
  772. }
  773. static void dpaa2_switch_port_fast_age(struct ethsw_port_priv *port_priv)
  774. {
  775. dpaa2_switch_fdb_iterate(port_priv,
  776. dpaa2_switch_fdb_entry_fast_age, NULL);
  777. }
  778. static int dpaa2_switch_port_vlan_add(struct net_device *netdev, __be16 proto,
  779. u16 vid)
  780. {
  781. struct switchdev_obj_port_vlan vlan = {
  782. .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
  783. .vid = vid,
  784. .obj.orig_dev = netdev,
  785. /* This API only allows programming tagged, non-PVID VIDs */
  786. .flags = 0,
  787. };
  788. return dpaa2_switch_port_vlans_add(netdev, &vlan);
  789. }
  790. static int dpaa2_switch_port_vlan_kill(struct net_device *netdev, __be16 proto,
  791. u16 vid)
  792. {
  793. struct switchdev_obj_port_vlan vlan = {
  794. .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
  795. .vid = vid,
  796. .obj.orig_dev = netdev,
  797. /* This API only allows programming tagged, non-PVID VIDs */
  798. .flags = 0,
  799. };
  800. return dpaa2_switch_port_vlans_del(netdev, &vlan);
  801. }
  802. static int dpaa2_switch_port_set_mac_addr(struct ethsw_port_priv *port_priv)
  803. {
  804. struct ethsw_core *ethsw = port_priv->ethsw_data;
  805. struct net_device *net_dev = port_priv->netdev;
  806. struct device *dev = net_dev->dev.parent;
  807. u8 mac_addr[ETH_ALEN];
  808. int err;
  809. if (!(ethsw->features & ETHSW_FEATURE_MAC_ADDR))
  810. return 0;
  811. /* Get firmware address, if any */
  812. err = dpsw_if_get_port_mac_addr(ethsw->mc_io, 0, ethsw->dpsw_handle,
  813. port_priv->idx, mac_addr);
  814. if (err) {
  815. dev_err(dev, "dpsw_if_get_port_mac_addr() failed\n");
  816. return err;
  817. }
  818. /* First check if firmware has any address configured by bootloader */
  819. if (!is_zero_ether_addr(mac_addr)) {
  820. eth_hw_addr_set(net_dev, mac_addr);
  821. } else {
  822. /* No MAC address configured, fill in net_dev->dev_addr
  823. * with a random one
  824. */
  825. eth_hw_addr_random(net_dev);
  826. dev_dbg_once(dev, "device(s) have all-zero hwaddr, replaced with random\n");
  827. /* Override NET_ADDR_RANDOM set by eth_hw_addr_random(); for all
  828. * practical purposes, this will be our "permanent" mac address,
  829. * at least until the next reboot. This move will also permit
  830. * register_netdevice() to properly fill up net_dev->perm_addr.
  831. */
  832. net_dev->addr_assign_type = NET_ADDR_PERM;
  833. }
  834. return 0;
  835. }
  836. static void dpaa2_switch_free_fd(const struct ethsw_core *ethsw,
  837. const struct dpaa2_fd *fd)
  838. {
  839. struct device *dev = ethsw->dev;
  840. unsigned char *buffer_start;
  841. struct sk_buff **skbh, *skb;
  842. dma_addr_t fd_addr;
  843. fd_addr = dpaa2_fd_get_addr(fd);
  844. skbh = dpaa2_iova_to_virt(ethsw->iommu_domain, fd_addr);
  845. skb = *skbh;
  846. buffer_start = (unsigned char *)skbh;
  847. dma_unmap_single(dev, fd_addr,
  848. skb_tail_pointer(skb) - buffer_start,
  849. DMA_TO_DEVICE);
  850. /* Move on with skb release */
  851. dev_kfree_skb(skb);
  852. }
  853. static int dpaa2_switch_build_single_fd(struct ethsw_core *ethsw,
  854. struct sk_buff *skb,
  855. struct dpaa2_fd *fd)
  856. {
  857. struct device *dev = ethsw->dev;
  858. struct sk_buff **skbh;
  859. dma_addr_t addr;
  860. u8 *buff_start;
  861. void *hwa;
  862. buff_start = PTR_ALIGN(skb->data - DPAA2_SWITCH_TX_DATA_OFFSET -
  863. DPAA2_SWITCH_TX_BUF_ALIGN,
  864. DPAA2_SWITCH_TX_BUF_ALIGN);
  865. /* Clear FAS to have consistent values for TX confirmation. It is
  866. * located in the first 8 bytes of the buffer's hardware annotation
  867. * area
  868. */
  869. hwa = buff_start + DPAA2_SWITCH_SWA_SIZE;
  870. memset(hwa, 0, 8);
  871. /* Store a backpointer to the skb at the beginning of the buffer
  872. * (in the private data area) such that we can release it
  873. * on Tx confirm
  874. */
  875. skbh = (struct sk_buff **)buff_start;
  876. *skbh = skb;
  877. addr = dma_map_single(dev, buff_start,
  878. skb_tail_pointer(skb) - buff_start,
  879. DMA_TO_DEVICE);
  880. if (unlikely(dma_mapping_error(dev, addr)))
  881. return -ENOMEM;
  882. /* Setup the FD fields */
  883. memset(fd, 0, sizeof(*fd));
  884. dpaa2_fd_set_addr(fd, addr);
  885. dpaa2_fd_set_offset(fd, (u16)(skb->data - buff_start));
  886. dpaa2_fd_set_len(fd, skb->len);
  887. dpaa2_fd_set_format(fd, dpaa2_fd_single);
  888. return 0;
  889. }
  890. static netdev_tx_t dpaa2_switch_port_tx(struct sk_buff *skb,
  891. struct net_device *net_dev)
  892. {
  893. struct ethsw_port_priv *port_priv = netdev_priv(net_dev);
  894. struct ethsw_core *ethsw = port_priv->ethsw_data;
  895. int retries = DPAA2_SWITCH_SWP_BUSY_RETRIES;
  896. struct dpaa2_fd fd;
  897. int err;
  898. if (unlikely(skb_headroom(skb) < DPAA2_SWITCH_NEEDED_HEADROOM)) {
  899. struct sk_buff *ns;
  900. ns = skb_realloc_headroom(skb, DPAA2_SWITCH_NEEDED_HEADROOM);
  901. if (unlikely(!ns)) {
  902. net_err_ratelimited("%s: Error reallocating skb headroom\n", net_dev->name);
  903. goto err_free_skb;
  904. }
  905. dev_consume_skb_any(skb);
  906. skb = ns;
  907. }
  908. /* We'll be holding a back-reference to the skb until Tx confirmation */
  909. skb = skb_unshare(skb, GFP_ATOMIC);
  910. if (unlikely(!skb)) {
  911. /* skb_unshare() has already freed the skb */
  912. net_err_ratelimited("%s: Error copying the socket buffer\n", net_dev->name);
  913. goto err_exit;
  914. }
  915. /* At this stage, we do not support non-linear skbs so just try to
  916. * linearize the skb and if that's not working, just drop the packet.
  917. */
  918. err = skb_linearize(skb);
  919. if (err) {
  920. net_err_ratelimited("%s: skb_linearize error (%d)!\n", net_dev->name, err);
  921. goto err_free_skb;
  922. }
  923. err = dpaa2_switch_build_single_fd(ethsw, skb, &fd);
  924. if (unlikely(err)) {
  925. net_err_ratelimited("%s: ethsw_build_*_fd() %d\n", net_dev->name, err);
  926. goto err_free_skb;
  927. }
  928. do {
  929. err = dpaa2_io_service_enqueue_qd(NULL,
  930. port_priv->tx_qdid,
  931. 8, 0, &fd);
  932. retries--;
  933. } while (err == -EBUSY && retries);
  934. if (unlikely(err < 0)) {
  935. dpaa2_switch_free_fd(ethsw, &fd);
  936. goto err_exit;
  937. }
  938. return NETDEV_TX_OK;
  939. err_free_skb:
  940. dev_kfree_skb(skb);
  941. err_exit:
  942. return NETDEV_TX_OK;
  943. }
  944. static int
  945. dpaa2_switch_setup_tc_cls_flower(struct dpaa2_switch_filter_block *filter_block,
  946. struct flow_cls_offload *f)
  947. {
  948. switch (f->command) {
  949. case FLOW_CLS_REPLACE:
  950. return dpaa2_switch_cls_flower_replace(filter_block, f);
  951. case FLOW_CLS_DESTROY:
  952. return dpaa2_switch_cls_flower_destroy(filter_block, f);
  953. default:
  954. return -EOPNOTSUPP;
  955. }
  956. }
  957. static int
  958. dpaa2_switch_setup_tc_cls_matchall(struct dpaa2_switch_filter_block *block,
  959. struct tc_cls_matchall_offload *f)
  960. {
  961. switch (f->command) {
  962. case TC_CLSMATCHALL_REPLACE:
  963. return dpaa2_switch_cls_matchall_replace(block, f);
  964. case TC_CLSMATCHALL_DESTROY:
  965. return dpaa2_switch_cls_matchall_destroy(block, f);
  966. default:
  967. return -EOPNOTSUPP;
  968. }
  969. }
  970. static int dpaa2_switch_port_setup_tc_block_cb_ig(enum tc_setup_type type,
  971. void *type_data,
  972. void *cb_priv)
  973. {
  974. switch (type) {
  975. case TC_SETUP_CLSFLOWER:
  976. return dpaa2_switch_setup_tc_cls_flower(cb_priv, type_data);
  977. case TC_SETUP_CLSMATCHALL:
  978. return dpaa2_switch_setup_tc_cls_matchall(cb_priv, type_data);
  979. default:
  980. return -EOPNOTSUPP;
  981. }
  982. }
  983. static LIST_HEAD(dpaa2_switch_block_cb_list);
  984. static int
  985. dpaa2_switch_port_acl_tbl_bind(struct ethsw_port_priv *port_priv,
  986. struct dpaa2_switch_filter_block *block)
  987. {
  988. struct ethsw_core *ethsw = port_priv->ethsw_data;
  989. struct net_device *netdev = port_priv->netdev;
  990. struct dpsw_acl_if_cfg acl_if_cfg;
  991. int err;
  992. if (port_priv->filter_block)
  993. return -EINVAL;
  994. acl_if_cfg.if_id[0] = port_priv->idx;
  995. acl_if_cfg.num_ifs = 1;
  996. err = dpsw_acl_add_if(ethsw->mc_io, 0, ethsw->dpsw_handle,
  997. block->acl_id, &acl_if_cfg);
  998. if (err) {
  999. netdev_err(netdev, "dpsw_acl_add_if err %d\n", err);
  1000. return err;
  1001. }
  1002. block->ports |= BIT(port_priv->idx);
  1003. port_priv->filter_block = block;
  1004. return 0;
  1005. }
  1006. static int
  1007. dpaa2_switch_port_acl_tbl_unbind(struct ethsw_port_priv *port_priv,
  1008. struct dpaa2_switch_filter_block *block)
  1009. {
  1010. struct ethsw_core *ethsw = port_priv->ethsw_data;
  1011. struct net_device *netdev = port_priv->netdev;
  1012. struct dpsw_acl_if_cfg acl_if_cfg;
  1013. int err;
  1014. if (port_priv->filter_block != block)
  1015. return -EINVAL;
  1016. acl_if_cfg.if_id[0] = port_priv->idx;
  1017. acl_if_cfg.num_ifs = 1;
  1018. err = dpsw_acl_remove_if(ethsw->mc_io, 0, ethsw->dpsw_handle,
  1019. block->acl_id, &acl_if_cfg);
  1020. if (err) {
  1021. netdev_err(netdev, "dpsw_acl_add_if err %d\n", err);
  1022. return err;
  1023. }
  1024. block->ports &= ~BIT(port_priv->idx);
  1025. port_priv->filter_block = NULL;
  1026. return 0;
  1027. }
  1028. static int dpaa2_switch_port_block_bind(struct ethsw_port_priv *port_priv,
  1029. struct dpaa2_switch_filter_block *block)
  1030. {
  1031. struct dpaa2_switch_filter_block *old_block = port_priv->filter_block;
  1032. int err;
  1033. /* Offload all the mirror entries found in the block on this new port
  1034. * joining it.
  1035. */
  1036. err = dpaa2_switch_block_offload_mirror(block, port_priv);
  1037. if (err)
  1038. return err;
  1039. /* If the port is already bound to this ACL table then do nothing. This
  1040. * can happen when this port is the first one to join a tc block
  1041. */
  1042. if (port_priv->filter_block == block)
  1043. return 0;
  1044. err = dpaa2_switch_port_acl_tbl_unbind(port_priv, old_block);
  1045. if (err)
  1046. return err;
  1047. /* Mark the previous ACL table as being unused if this was the last
  1048. * port that was using it.
  1049. */
  1050. if (old_block->ports == 0)
  1051. old_block->in_use = false;
  1052. return dpaa2_switch_port_acl_tbl_bind(port_priv, block);
  1053. }
  1054. static int
  1055. dpaa2_switch_port_block_unbind(struct ethsw_port_priv *port_priv,
  1056. struct dpaa2_switch_filter_block *block)
  1057. {
  1058. struct ethsw_core *ethsw = port_priv->ethsw_data;
  1059. struct dpaa2_switch_filter_block *new_block;
  1060. int err;
  1061. /* Unoffload all the mirror entries found in the block from the
  1062. * port leaving it.
  1063. */
  1064. err = dpaa2_switch_block_unoffload_mirror(block, port_priv);
  1065. if (err)
  1066. return err;
  1067. /* We are the last port that leaves a block (an ACL table).
  1068. * We'll continue to use this table.
  1069. */
  1070. if (block->ports == BIT(port_priv->idx))
  1071. return 0;
  1072. err = dpaa2_switch_port_acl_tbl_unbind(port_priv, block);
  1073. if (err)
  1074. return err;
  1075. if (block->ports == 0)
  1076. block->in_use = false;
  1077. new_block = dpaa2_switch_filter_block_get_unused(ethsw);
  1078. new_block->in_use = true;
  1079. return dpaa2_switch_port_acl_tbl_bind(port_priv, new_block);
  1080. }
  1081. static int dpaa2_switch_setup_tc_block_bind(struct net_device *netdev,
  1082. struct flow_block_offload *f)
  1083. {
  1084. struct ethsw_port_priv *port_priv = netdev_priv(netdev);
  1085. struct ethsw_core *ethsw = port_priv->ethsw_data;
  1086. struct dpaa2_switch_filter_block *filter_block;
  1087. struct flow_block_cb *block_cb;
  1088. bool register_block = false;
  1089. int err;
  1090. block_cb = flow_block_cb_lookup(f->block,
  1091. dpaa2_switch_port_setup_tc_block_cb_ig,
  1092. ethsw);
  1093. if (!block_cb) {
  1094. /* If the filter block is not already known, then this port
  1095. * must be the first to join it. In this case, we can just
  1096. * continue to use our private table
  1097. */
  1098. filter_block = port_priv->filter_block;
  1099. block_cb = flow_block_cb_alloc(dpaa2_switch_port_setup_tc_block_cb_ig,
  1100. ethsw, filter_block, NULL);
  1101. if (IS_ERR(block_cb))
  1102. return PTR_ERR(block_cb);
  1103. register_block = true;
  1104. } else {
  1105. filter_block = flow_block_cb_priv(block_cb);
  1106. }
  1107. flow_block_cb_incref(block_cb);
  1108. err = dpaa2_switch_port_block_bind(port_priv, filter_block);
  1109. if (err)
  1110. goto err_block_bind;
  1111. if (register_block) {
  1112. flow_block_cb_add(block_cb, f);
  1113. list_add_tail(&block_cb->driver_list,
  1114. &dpaa2_switch_block_cb_list);
  1115. }
  1116. return 0;
  1117. err_block_bind:
  1118. if (!flow_block_cb_decref(block_cb))
  1119. flow_block_cb_free(block_cb);
  1120. return err;
  1121. }
  1122. static void dpaa2_switch_setup_tc_block_unbind(struct net_device *netdev,
  1123. struct flow_block_offload *f)
  1124. {
  1125. struct ethsw_port_priv *port_priv = netdev_priv(netdev);
  1126. struct ethsw_core *ethsw = port_priv->ethsw_data;
  1127. struct dpaa2_switch_filter_block *filter_block;
  1128. struct flow_block_cb *block_cb;
  1129. int err;
  1130. block_cb = flow_block_cb_lookup(f->block,
  1131. dpaa2_switch_port_setup_tc_block_cb_ig,
  1132. ethsw);
  1133. if (!block_cb)
  1134. return;
  1135. filter_block = flow_block_cb_priv(block_cb);
  1136. err = dpaa2_switch_port_block_unbind(port_priv, filter_block);
  1137. if (!err && !flow_block_cb_decref(block_cb)) {
  1138. flow_block_cb_remove(block_cb, f);
  1139. list_del(&block_cb->driver_list);
  1140. }
  1141. }
  1142. static int dpaa2_switch_setup_tc_block(struct net_device *netdev,
  1143. struct flow_block_offload *f)
  1144. {
  1145. if (f->binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
  1146. return -EOPNOTSUPP;
  1147. f->driver_block_list = &dpaa2_switch_block_cb_list;
  1148. switch (f->command) {
  1149. case FLOW_BLOCK_BIND:
  1150. return dpaa2_switch_setup_tc_block_bind(netdev, f);
  1151. case FLOW_BLOCK_UNBIND:
  1152. dpaa2_switch_setup_tc_block_unbind(netdev, f);
  1153. return 0;
  1154. default:
  1155. return -EOPNOTSUPP;
  1156. }
  1157. }
  1158. static int dpaa2_switch_port_setup_tc(struct net_device *netdev,
  1159. enum tc_setup_type type,
  1160. void *type_data)
  1161. {
  1162. switch (type) {
  1163. case TC_SETUP_BLOCK: {
  1164. return dpaa2_switch_setup_tc_block(netdev, type_data);
  1165. }
  1166. default:
  1167. return -EOPNOTSUPP;
  1168. }
  1169. return 0;
  1170. }
  1171. static const struct net_device_ops dpaa2_switch_port_ops = {
  1172. .ndo_open = dpaa2_switch_port_open,
  1173. .ndo_stop = dpaa2_switch_port_stop,
  1174. .ndo_set_mac_address = eth_mac_addr,
  1175. .ndo_get_stats64 = dpaa2_switch_port_get_stats,
  1176. .ndo_change_mtu = dpaa2_switch_port_change_mtu,
  1177. .ndo_has_offload_stats = dpaa2_switch_port_has_offload_stats,
  1178. .ndo_get_offload_stats = dpaa2_switch_port_get_offload_stats,
  1179. .ndo_fdb_dump = dpaa2_switch_port_fdb_dump,
  1180. .ndo_vlan_rx_add_vid = dpaa2_switch_port_vlan_add,
  1181. .ndo_vlan_rx_kill_vid = dpaa2_switch_port_vlan_kill,
  1182. .ndo_start_xmit = dpaa2_switch_port_tx,
  1183. .ndo_get_port_parent_id = dpaa2_switch_port_parent_id,
  1184. .ndo_get_phys_port_name = dpaa2_switch_port_get_phys_name,
  1185. .ndo_setup_tc = dpaa2_switch_port_setup_tc,
  1186. };
  1187. bool dpaa2_switch_port_dev_check(const struct net_device *netdev)
  1188. {
  1189. return netdev->netdev_ops == &dpaa2_switch_port_ops;
  1190. }
  1191. static int dpaa2_switch_port_connect_mac(struct ethsw_port_priv *port_priv)
  1192. {
  1193. struct fsl_mc_device *dpsw_port_dev, *dpmac_dev;
  1194. struct dpaa2_mac *mac;
  1195. int err;
  1196. dpsw_port_dev = to_fsl_mc_device(port_priv->netdev->dev.parent);
  1197. dpmac_dev = fsl_mc_get_endpoint(dpsw_port_dev, port_priv->idx);
  1198. if (PTR_ERR(dpmac_dev) == -EPROBE_DEFER)
  1199. return PTR_ERR(dpmac_dev);
  1200. if (IS_ERR(dpmac_dev))
  1201. return 0;
  1202. if (dpmac_dev->dev.type != &fsl_mc_bus_dpmac_type) {
  1203. err = 0;
  1204. goto out_put_device;
  1205. }
  1206. mac = kzalloc_obj(*mac);
  1207. if (!mac) {
  1208. err = -ENOMEM;
  1209. goto out_put_device;
  1210. }
  1211. mac->mc_dev = dpmac_dev;
  1212. mac->mc_io = port_priv->ethsw_data->mc_io;
  1213. mac->net_dev = port_priv->netdev;
  1214. err = dpaa2_mac_open(mac);
  1215. if (err)
  1216. goto err_free_mac;
  1217. if (dpaa2_mac_is_type_phy(mac)) {
  1218. err = dpaa2_mac_connect(mac);
  1219. if (err) {
  1220. netdev_err(port_priv->netdev,
  1221. "Error connecting to the MAC endpoint %pe\n",
  1222. ERR_PTR(err));
  1223. goto err_close_mac;
  1224. }
  1225. }
  1226. mutex_lock(&port_priv->mac_lock);
  1227. port_priv->mac = mac;
  1228. mutex_unlock(&port_priv->mac_lock);
  1229. return 0;
  1230. err_close_mac:
  1231. dpaa2_mac_close(mac);
  1232. err_free_mac:
  1233. kfree(mac);
  1234. out_put_device:
  1235. put_device(&dpmac_dev->dev);
  1236. return err;
  1237. }
  1238. static void dpaa2_switch_port_disconnect_mac(struct ethsw_port_priv *port_priv)
  1239. {
  1240. struct dpaa2_mac *mac;
  1241. mutex_lock(&port_priv->mac_lock);
  1242. mac = port_priv->mac;
  1243. port_priv->mac = NULL;
  1244. mutex_unlock(&port_priv->mac_lock);
  1245. if (!mac)
  1246. return;
  1247. if (dpaa2_mac_is_type_phy(mac))
  1248. dpaa2_mac_disconnect(mac);
  1249. dpaa2_mac_close(mac);
  1250. kfree(mac);
  1251. }
  1252. static irqreturn_t dpaa2_switch_irq0_handler_thread(int irq_num, void *arg)
  1253. {
  1254. struct device *dev = (struct device *)arg;
  1255. struct ethsw_core *ethsw = dev_get_drvdata(dev);
  1256. struct ethsw_port_priv *port_priv;
  1257. int err, if_id;
  1258. bool had_mac;
  1259. u32 status;
  1260. err = dpsw_get_irq_status(ethsw->mc_io, 0, ethsw->dpsw_handle,
  1261. DPSW_IRQ_INDEX_IF, &status);
  1262. if (err) {
  1263. dev_err(dev, "Can't get irq status (err %d)\n", err);
  1264. goto out;
  1265. }
  1266. if_id = (status & 0xFFFF0000) >> 16;
  1267. if (if_id >= ethsw->sw_attr.num_ifs) {
  1268. dev_err(dev, "Invalid if_id %d in IRQ status\n", if_id);
  1269. goto out_clear;
  1270. }
  1271. port_priv = ethsw->ports[if_id];
  1272. if (status & DPSW_IRQ_EVENT_LINK_CHANGED)
  1273. dpaa2_switch_port_link_state_update(port_priv->netdev);
  1274. if (status & DPSW_IRQ_EVENT_ENDPOINT_CHANGED) {
  1275. dpaa2_switch_port_set_mac_addr(port_priv);
  1276. /* We can avoid locking because the "endpoint changed" IRQ
  1277. * handler is the only one who changes priv->mac at runtime,
  1278. * so we are not racing with anyone.
  1279. */
  1280. had_mac = !!port_priv->mac;
  1281. if (had_mac)
  1282. dpaa2_switch_port_disconnect_mac(port_priv);
  1283. else
  1284. dpaa2_switch_port_connect_mac(port_priv);
  1285. }
  1286. out_clear:
  1287. err = dpsw_clear_irq_status(ethsw->mc_io, 0, ethsw->dpsw_handle,
  1288. DPSW_IRQ_INDEX_IF, status);
  1289. if (err)
  1290. dev_err(dev, "Can't clear irq status (err %d)\n", err);
  1291. out:
  1292. return IRQ_HANDLED;
  1293. }
  1294. static int dpaa2_switch_setup_irqs(struct fsl_mc_device *sw_dev)
  1295. {
  1296. u32 mask = DPSW_IRQ_EVENT_LINK_CHANGED | DPSW_IRQ_EVENT_ENDPOINT_CHANGED;
  1297. struct device *dev = &sw_dev->dev;
  1298. struct ethsw_core *ethsw = dev_get_drvdata(dev);
  1299. struct fsl_mc_device_irq *irq;
  1300. int err;
  1301. err = fsl_mc_allocate_irqs(sw_dev);
  1302. if (err) {
  1303. dev_err(dev, "MC irqs allocation failed\n");
  1304. return err;
  1305. }
  1306. if (WARN_ON(sw_dev->obj_desc.irq_count != DPSW_IRQ_NUM)) {
  1307. err = -EINVAL;
  1308. goto free_irq;
  1309. }
  1310. err = dpsw_set_irq_enable(ethsw->mc_io, 0, ethsw->dpsw_handle,
  1311. DPSW_IRQ_INDEX_IF, 0);
  1312. if (err) {
  1313. dev_err(dev, "dpsw_set_irq_enable err %d\n", err);
  1314. goto free_irq;
  1315. }
  1316. irq = sw_dev->irqs[DPSW_IRQ_INDEX_IF];
  1317. err = devm_request_threaded_irq(dev, irq->virq, NULL,
  1318. dpaa2_switch_irq0_handler_thread,
  1319. IRQF_NO_SUSPEND | IRQF_ONESHOT,
  1320. dev_name(dev), dev);
  1321. if (err) {
  1322. dev_err(dev, "devm_request_threaded_irq(): %d\n", err);
  1323. goto free_irq;
  1324. }
  1325. err = dpsw_set_irq_mask(ethsw->mc_io, 0, ethsw->dpsw_handle,
  1326. DPSW_IRQ_INDEX_IF, mask);
  1327. if (err) {
  1328. dev_err(dev, "dpsw_set_irq_mask(): %d\n", err);
  1329. goto free_devm_irq;
  1330. }
  1331. err = dpsw_set_irq_enable(ethsw->mc_io, 0, ethsw->dpsw_handle,
  1332. DPSW_IRQ_INDEX_IF, 1);
  1333. if (err) {
  1334. dev_err(dev, "dpsw_set_irq_enable(): %d\n", err);
  1335. goto free_devm_irq;
  1336. }
  1337. return 0;
  1338. free_devm_irq:
  1339. devm_free_irq(dev, irq->virq, dev);
  1340. free_irq:
  1341. fsl_mc_free_irqs(sw_dev);
  1342. return err;
  1343. }
  1344. static void dpaa2_switch_teardown_irqs(struct fsl_mc_device *sw_dev)
  1345. {
  1346. struct device *dev = &sw_dev->dev;
  1347. struct ethsw_core *ethsw = dev_get_drvdata(dev);
  1348. int err;
  1349. err = dpsw_set_irq_enable(ethsw->mc_io, 0, ethsw->dpsw_handle,
  1350. DPSW_IRQ_INDEX_IF, 0);
  1351. if (err)
  1352. dev_err(dev, "dpsw_set_irq_enable err %d\n", err);
  1353. fsl_mc_free_irqs(sw_dev);
  1354. }
  1355. static int dpaa2_switch_port_set_learning(struct ethsw_port_priv *port_priv, bool enable)
  1356. {
  1357. struct ethsw_core *ethsw = port_priv->ethsw_data;
  1358. enum dpsw_learning_mode learn_mode;
  1359. int err;
  1360. if (enable)
  1361. learn_mode = DPSW_LEARNING_MODE_HW;
  1362. else
  1363. learn_mode = DPSW_LEARNING_MODE_DIS;
  1364. err = dpsw_if_set_learning_mode(ethsw->mc_io, 0, ethsw->dpsw_handle,
  1365. port_priv->idx, learn_mode);
  1366. if (err)
  1367. netdev_err(port_priv->netdev, "dpsw_if_set_learning_mode err %d\n", err);
  1368. if (!enable)
  1369. dpaa2_switch_port_fast_age(port_priv);
  1370. return err;
  1371. }
  1372. static int dpaa2_switch_port_attr_stp_state_set(struct net_device *netdev,
  1373. u8 state)
  1374. {
  1375. struct ethsw_port_priv *port_priv = netdev_priv(netdev);
  1376. int err;
  1377. err = dpaa2_switch_port_set_stp_state(port_priv, state);
  1378. if (err)
  1379. return err;
  1380. switch (state) {
  1381. case BR_STATE_DISABLED:
  1382. case BR_STATE_BLOCKING:
  1383. case BR_STATE_LISTENING:
  1384. err = dpaa2_switch_port_set_learning(port_priv, false);
  1385. break;
  1386. case BR_STATE_LEARNING:
  1387. case BR_STATE_FORWARDING:
  1388. err = dpaa2_switch_port_set_learning(port_priv,
  1389. port_priv->learn_ena);
  1390. break;
  1391. }
  1392. return err;
  1393. }
  1394. static int dpaa2_switch_port_flood(struct ethsw_port_priv *port_priv,
  1395. struct switchdev_brport_flags flags)
  1396. {
  1397. struct ethsw_core *ethsw = port_priv->ethsw_data;
  1398. if (flags.mask & BR_BCAST_FLOOD)
  1399. port_priv->bcast_flood = !!(flags.val & BR_BCAST_FLOOD);
  1400. if (flags.mask & BR_FLOOD)
  1401. port_priv->ucast_flood = !!(flags.val & BR_FLOOD);
  1402. return dpaa2_switch_fdb_set_egress_flood(ethsw, port_priv->fdb->fdb_id);
  1403. }
  1404. static int dpaa2_switch_port_pre_bridge_flags(struct net_device *netdev,
  1405. struct switchdev_brport_flags flags,
  1406. struct netlink_ext_ack *extack)
  1407. {
  1408. if (flags.mask & ~(BR_LEARNING | BR_BCAST_FLOOD | BR_FLOOD |
  1409. BR_MCAST_FLOOD))
  1410. return -EINVAL;
  1411. if (flags.mask & (BR_FLOOD | BR_MCAST_FLOOD)) {
  1412. bool multicast = !!(flags.val & BR_MCAST_FLOOD);
  1413. bool unicast = !!(flags.val & BR_FLOOD);
  1414. if (unicast != multicast) {
  1415. NL_SET_ERR_MSG_MOD(extack,
  1416. "Cannot configure multicast flooding independently of unicast");
  1417. return -EINVAL;
  1418. }
  1419. }
  1420. return 0;
  1421. }
  1422. static int dpaa2_switch_port_bridge_flags(struct net_device *netdev,
  1423. struct switchdev_brport_flags flags,
  1424. struct netlink_ext_ack *extack)
  1425. {
  1426. struct ethsw_port_priv *port_priv = netdev_priv(netdev);
  1427. int err;
  1428. if (flags.mask & BR_LEARNING) {
  1429. bool learn_ena = !!(flags.val & BR_LEARNING);
  1430. err = dpaa2_switch_port_set_learning(port_priv, learn_ena);
  1431. if (err)
  1432. return err;
  1433. port_priv->learn_ena = learn_ena;
  1434. }
  1435. if (flags.mask & (BR_BCAST_FLOOD | BR_FLOOD | BR_MCAST_FLOOD)) {
  1436. err = dpaa2_switch_port_flood(port_priv, flags);
  1437. if (err)
  1438. return err;
  1439. }
  1440. return 0;
  1441. }
  1442. static int dpaa2_switch_port_attr_set(struct net_device *netdev, const void *ctx,
  1443. const struct switchdev_attr *attr,
  1444. struct netlink_ext_ack *extack)
  1445. {
  1446. int err = 0;
  1447. switch (attr->id) {
  1448. case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
  1449. err = dpaa2_switch_port_attr_stp_state_set(netdev,
  1450. attr->u.stp_state);
  1451. break;
  1452. case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
  1453. if (!attr->u.vlan_filtering) {
  1454. NL_SET_ERR_MSG_MOD(extack,
  1455. "The DPAA2 switch does not support VLAN-unaware operation");
  1456. return -EOPNOTSUPP;
  1457. }
  1458. break;
  1459. case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
  1460. err = dpaa2_switch_port_pre_bridge_flags(netdev, attr->u.brport_flags, extack);
  1461. break;
  1462. case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
  1463. err = dpaa2_switch_port_bridge_flags(netdev, attr->u.brport_flags, extack);
  1464. break;
  1465. default:
  1466. err = -EOPNOTSUPP;
  1467. break;
  1468. }
  1469. return err;
  1470. }
  1471. int dpaa2_switch_port_vlans_add(struct net_device *netdev,
  1472. const struct switchdev_obj_port_vlan *vlan)
  1473. {
  1474. struct ethsw_port_priv *port_priv = netdev_priv(netdev);
  1475. struct ethsw_core *ethsw = port_priv->ethsw_data;
  1476. struct dpsw_attr *attr = &ethsw->sw_attr;
  1477. int err = 0;
  1478. /* Make sure that the VLAN is not already configured
  1479. * on the switch port
  1480. */
  1481. if (port_priv->vlans[vlan->vid] & ETHSW_VLAN_MEMBER) {
  1482. netdev_err(netdev, "VLAN %d already configured\n", vlan->vid);
  1483. return -EEXIST;
  1484. }
  1485. /* Check if there is space for a new VLAN */
  1486. err = dpsw_get_attributes(ethsw->mc_io, 0, ethsw->dpsw_handle,
  1487. &ethsw->sw_attr);
  1488. if (err) {
  1489. netdev_err(netdev, "dpsw_get_attributes err %d\n", err);
  1490. return err;
  1491. }
  1492. if (attr->max_vlans - attr->num_vlans < 1)
  1493. return -ENOSPC;
  1494. /* Check if there is space for a new VLAN */
  1495. err = dpsw_get_attributes(ethsw->mc_io, 0, ethsw->dpsw_handle,
  1496. &ethsw->sw_attr);
  1497. if (err) {
  1498. netdev_err(netdev, "dpsw_get_attributes err %d\n", err);
  1499. return err;
  1500. }
  1501. if (attr->max_vlans - attr->num_vlans < 1)
  1502. return -ENOSPC;
  1503. if (!port_priv->ethsw_data->vlans[vlan->vid]) {
  1504. /* this is a new VLAN */
  1505. err = dpaa2_switch_add_vlan(port_priv, vlan->vid);
  1506. if (err)
  1507. return err;
  1508. port_priv->ethsw_data->vlans[vlan->vid] |= ETHSW_VLAN_GLOBAL;
  1509. }
  1510. return dpaa2_switch_port_add_vlan(port_priv, vlan->vid, vlan->flags);
  1511. }
  1512. static int dpaa2_switch_port_lookup_address(struct net_device *netdev, int is_uc,
  1513. const unsigned char *addr)
  1514. {
  1515. struct netdev_hw_addr_list *list = (is_uc) ? &netdev->uc : &netdev->mc;
  1516. struct netdev_hw_addr *ha;
  1517. netif_addr_lock_bh(netdev);
  1518. list_for_each_entry(ha, &list->list, list) {
  1519. if (ether_addr_equal(ha->addr, addr)) {
  1520. netif_addr_unlock_bh(netdev);
  1521. return 1;
  1522. }
  1523. }
  1524. netif_addr_unlock_bh(netdev);
  1525. return 0;
  1526. }
  1527. static int dpaa2_switch_port_mdb_add(struct net_device *netdev,
  1528. const struct switchdev_obj_port_mdb *mdb)
  1529. {
  1530. struct ethsw_port_priv *port_priv = netdev_priv(netdev);
  1531. int err;
  1532. /* Check if address is already set on this port */
  1533. if (dpaa2_switch_port_lookup_address(netdev, 0, mdb->addr))
  1534. return -EEXIST;
  1535. err = dpaa2_switch_port_fdb_add_mc(port_priv, mdb->addr);
  1536. if (err)
  1537. return err;
  1538. err = dev_mc_add(netdev, mdb->addr);
  1539. if (err) {
  1540. netdev_err(netdev, "dev_mc_add err %d\n", err);
  1541. dpaa2_switch_port_fdb_del_mc(port_priv, mdb->addr);
  1542. }
  1543. return err;
  1544. }
  1545. static int dpaa2_switch_port_obj_add(struct net_device *netdev,
  1546. const struct switchdev_obj *obj)
  1547. {
  1548. int err;
  1549. switch (obj->id) {
  1550. case SWITCHDEV_OBJ_ID_PORT_VLAN:
  1551. err = dpaa2_switch_port_vlans_add(netdev,
  1552. SWITCHDEV_OBJ_PORT_VLAN(obj));
  1553. break;
  1554. case SWITCHDEV_OBJ_ID_PORT_MDB:
  1555. err = dpaa2_switch_port_mdb_add(netdev,
  1556. SWITCHDEV_OBJ_PORT_MDB(obj));
  1557. break;
  1558. default:
  1559. err = -EOPNOTSUPP;
  1560. break;
  1561. }
  1562. return err;
  1563. }
  1564. static int dpaa2_switch_port_del_vlan(struct ethsw_port_priv *port_priv, u16 vid)
  1565. {
  1566. struct ethsw_core *ethsw = port_priv->ethsw_data;
  1567. struct net_device *netdev = port_priv->netdev;
  1568. struct dpsw_vlan_if_cfg vcfg;
  1569. int i, err;
  1570. if (!port_priv->vlans[vid])
  1571. return -ENOENT;
  1572. if (port_priv->vlans[vid] & ETHSW_VLAN_PVID) {
  1573. /* If we are deleting the PVID of a port, use VLAN 4095 instead
  1574. * as we are sure that neither the bridge nor the 8021q module
  1575. * will use it
  1576. */
  1577. err = dpaa2_switch_port_set_pvid(port_priv, 4095);
  1578. if (err)
  1579. return err;
  1580. }
  1581. vcfg.num_ifs = 1;
  1582. vcfg.if_id[0] = port_priv->idx;
  1583. if (port_priv->vlans[vid] & ETHSW_VLAN_UNTAGGED) {
  1584. err = dpsw_vlan_remove_if_untagged(ethsw->mc_io, 0,
  1585. ethsw->dpsw_handle,
  1586. vid, &vcfg);
  1587. if (err) {
  1588. netdev_err(netdev,
  1589. "dpsw_vlan_remove_if_untagged err %d\n",
  1590. err);
  1591. }
  1592. port_priv->vlans[vid] &= ~ETHSW_VLAN_UNTAGGED;
  1593. }
  1594. if (port_priv->vlans[vid] & ETHSW_VLAN_MEMBER) {
  1595. err = dpsw_vlan_remove_if(ethsw->mc_io, 0, ethsw->dpsw_handle,
  1596. vid, &vcfg);
  1597. if (err) {
  1598. netdev_err(netdev,
  1599. "dpsw_vlan_remove_if err %d\n", err);
  1600. return err;
  1601. }
  1602. port_priv->vlans[vid] &= ~ETHSW_VLAN_MEMBER;
  1603. /* Delete VLAN from switch if it is no longer configured on
  1604. * any port
  1605. */
  1606. for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
  1607. if (ethsw->ports[i] &&
  1608. ethsw->ports[i]->vlans[vid] & ETHSW_VLAN_MEMBER)
  1609. return 0; /* Found a port member in VID */
  1610. }
  1611. ethsw->vlans[vid] &= ~ETHSW_VLAN_GLOBAL;
  1612. err = dpaa2_switch_dellink(ethsw, vid);
  1613. if (err)
  1614. return err;
  1615. }
  1616. return 0;
  1617. }
  1618. int dpaa2_switch_port_vlans_del(struct net_device *netdev,
  1619. const struct switchdev_obj_port_vlan *vlan)
  1620. {
  1621. struct ethsw_port_priv *port_priv = netdev_priv(netdev);
  1622. if (netif_is_bridge_master(vlan->obj.orig_dev))
  1623. return -EOPNOTSUPP;
  1624. return dpaa2_switch_port_del_vlan(port_priv, vlan->vid);
  1625. }
  1626. static int dpaa2_switch_port_mdb_del(struct net_device *netdev,
  1627. const struct switchdev_obj_port_mdb *mdb)
  1628. {
  1629. struct ethsw_port_priv *port_priv = netdev_priv(netdev);
  1630. int err;
  1631. if (!dpaa2_switch_port_lookup_address(netdev, 0, mdb->addr))
  1632. return -ENOENT;
  1633. err = dpaa2_switch_port_fdb_del_mc(port_priv, mdb->addr);
  1634. if (err)
  1635. return err;
  1636. err = dev_mc_del(netdev, mdb->addr);
  1637. if (err) {
  1638. netdev_err(netdev, "dev_mc_del err %d\n", err);
  1639. return err;
  1640. }
  1641. return err;
  1642. }
  1643. static int dpaa2_switch_port_obj_del(struct net_device *netdev,
  1644. const struct switchdev_obj *obj)
  1645. {
  1646. int err;
  1647. switch (obj->id) {
  1648. case SWITCHDEV_OBJ_ID_PORT_VLAN:
  1649. err = dpaa2_switch_port_vlans_del(netdev, SWITCHDEV_OBJ_PORT_VLAN(obj));
  1650. break;
  1651. case SWITCHDEV_OBJ_ID_PORT_MDB:
  1652. err = dpaa2_switch_port_mdb_del(netdev, SWITCHDEV_OBJ_PORT_MDB(obj));
  1653. break;
  1654. default:
  1655. err = -EOPNOTSUPP;
  1656. break;
  1657. }
  1658. return err;
  1659. }
  1660. static int dpaa2_switch_port_attr_set_event(struct net_device *netdev,
  1661. struct switchdev_notifier_port_attr_info *ptr)
  1662. {
  1663. int err;
  1664. err = switchdev_handle_port_attr_set(netdev, ptr,
  1665. dpaa2_switch_port_dev_check,
  1666. dpaa2_switch_port_attr_set);
  1667. return notifier_from_errno(err);
  1668. }
  1669. static int dpaa2_switch_port_bridge_join(struct net_device *netdev,
  1670. struct net_device *upper_dev,
  1671. struct netlink_ext_ack *extack)
  1672. {
  1673. struct ethsw_port_priv *port_priv = netdev_priv(netdev);
  1674. struct dpaa2_switch_fdb *old_fdb = port_priv->fdb;
  1675. struct ethsw_core *ethsw = port_priv->ethsw_data;
  1676. bool learn_ena;
  1677. int err;
  1678. /* Delete the previously manually installed VLAN 1 */
  1679. err = dpaa2_switch_port_del_vlan(port_priv, 1);
  1680. if (err)
  1681. return err;
  1682. dpaa2_switch_port_set_fdb(port_priv, upper_dev);
  1683. /* Inherit the initial bridge port learning state */
  1684. learn_ena = br_port_flag_is_set(netdev, BR_LEARNING);
  1685. err = dpaa2_switch_port_set_learning(port_priv, learn_ena);
  1686. port_priv->learn_ena = learn_ena;
  1687. /* Setup the egress flood policy (broadcast, unknown unicast) */
  1688. err = dpaa2_switch_fdb_set_egress_flood(ethsw, port_priv->fdb->fdb_id);
  1689. if (err)
  1690. goto err_egress_flood;
  1691. /* Recreate the egress flood domain of the FDB that we just left. */
  1692. err = dpaa2_switch_fdb_set_egress_flood(ethsw, old_fdb->fdb_id);
  1693. if (err)
  1694. goto err_egress_flood;
  1695. err = switchdev_bridge_port_offload(netdev, netdev, NULL,
  1696. NULL, NULL, false, extack);
  1697. if (err)
  1698. goto err_switchdev_offload;
  1699. return 0;
  1700. err_switchdev_offload:
  1701. err_egress_flood:
  1702. dpaa2_switch_port_set_fdb(port_priv, NULL);
  1703. return err;
  1704. }
  1705. static int dpaa2_switch_port_clear_rxvlan(struct net_device *vdev, int vid, void *arg)
  1706. {
  1707. __be16 vlan_proto = htons(ETH_P_8021Q);
  1708. if (vdev)
  1709. vlan_proto = vlan_dev_vlan_proto(vdev);
  1710. return dpaa2_switch_port_vlan_kill(arg, vlan_proto, vid);
  1711. }
  1712. static int dpaa2_switch_port_restore_rxvlan(struct net_device *vdev, int vid, void *arg)
  1713. {
  1714. __be16 vlan_proto = htons(ETH_P_8021Q);
  1715. if (vdev)
  1716. vlan_proto = vlan_dev_vlan_proto(vdev);
  1717. return dpaa2_switch_port_vlan_add(arg, vlan_proto, vid);
  1718. }
  1719. static void dpaa2_switch_port_pre_bridge_leave(struct net_device *netdev)
  1720. {
  1721. switchdev_bridge_port_unoffload(netdev, NULL, NULL, NULL);
  1722. }
  1723. static int dpaa2_switch_port_bridge_leave(struct net_device *netdev)
  1724. {
  1725. struct ethsw_port_priv *port_priv = netdev_priv(netdev);
  1726. struct dpaa2_switch_fdb *old_fdb = port_priv->fdb;
  1727. struct ethsw_core *ethsw = port_priv->ethsw_data;
  1728. int err;
  1729. /* First of all, fast age any learn FDB addresses on this switch port */
  1730. dpaa2_switch_port_fast_age(port_priv);
  1731. /* Clear all RX VLANs installed through vlan_vid_add() either as VLAN
  1732. * upper devices or otherwise from the FDB table that we are about to
  1733. * leave
  1734. */
  1735. err = vlan_for_each(netdev, dpaa2_switch_port_clear_rxvlan, netdev);
  1736. if (err)
  1737. netdev_err(netdev, "Unable to clear RX VLANs from old FDB table, err (%d)\n", err);
  1738. dpaa2_switch_port_set_fdb(port_priv, NULL);
  1739. /* Restore all RX VLANs into the new FDB table that we just joined */
  1740. err = vlan_for_each(netdev, dpaa2_switch_port_restore_rxvlan, netdev);
  1741. if (err)
  1742. netdev_err(netdev, "Unable to restore RX VLANs to the new FDB, err (%d)\n", err);
  1743. /* Reset the flooding state to denote that this port can send any
  1744. * packet in standalone mode. With this, we are also ensuring that any
  1745. * later bridge join will have the flooding flag on.
  1746. */
  1747. port_priv->bcast_flood = true;
  1748. port_priv->ucast_flood = true;
  1749. /* Setup the egress flood policy (broadcast, unknown unicast).
  1750. * When the port is not under a bridge, only the CTRL interface is part
  1751. * of the flooding domain besides the actual port
  1752. */
  1753. err = dpaa2_switch_fdb_set_egress_flood(ethsw, port_priv->fdb->fdb_id);
  1754. if (err)
  1755. return err;
  1756. /* Recreate the egress flood domain of the FDB that we just left */
  1757. err = dpaa2_switch_fdb_set_egress_flood(ethsw, old_fdb->fdb_id);
  1758. if (err)
  1759. return err;
  1760. /* No HW learning when not under a bridge */
  1761. err = dpaa2_switch_port_set_learning(port_priv, false);
  1762. if (err)
  1763. return err;
  1764. port_priv->learn_ena = false;
  1765. /* Add the VLAN 1 as PVID when not under a bridge. We need this since
  1766. * the dpaa2 switch interfaces are not capable to be VLAN unaware
  1767. */
  1768. return dpaa2_switch_port_add_vlan(port_priv, DEFAULT_VLAN_ID,
  1769. BRIDGE_VLAN_INFO_UNTAGGED | BRIDGE_VLAN_INFO_PVID);
  1770. }
  1771. static int dpaa2_switch_prevent_bridging_with_8021q_upper(struct net_device *netdev)
  1772. {
  1773. struct net_device *upper_dev;
  1774. struct list_head *iter;
  1775. /* RCU read lock not necessary because we have write-side protection
  1776. * (rtnl_mutex), however a non-rcu iterator does not exist.
  1777. */
  1778. netdev_for_each_upper_dev_rcu(netdev, upper_dev, iter)
  1779. if (is_vlan_dev(upper_dev))
  1780. return -EOPNOTSUPP;
  1781. return 0;
  1782. }
  1783. static int
  1784. dpaa2_switch_prechangeupper_sanity_checks(struct net_device *netdev,
  1785. struct net_device *upper_dev,
  1786. struct netlink_ext_ack *extack)
  1787. {
  1788. struct ethsw_port_priv *port_priv = netdev_priv(netdev);
  1789. struct ethsw_port_priv *other_port_priv;
  1790. struct net_device *other_dev;
  1791. struct list_head *iter;
  1792. int err;
  1793. if (!br_vlan_enabled(upper_dev)) {
  1794. NL_SET_ERR_MSG_MOD(extack, "Cannot join a VLAN-unaware bridge");
  1795. return -EOPNOTSUPP;
  1796. }
  1797. err = dpaa2_switch_prevent_bridging_with_8021q_upper(netdev);
  1798. if (err) {
  1799. NL_SET_ERR_MSG_MOD(extack,
  1800. "Cannot join a bridge while VLAN uppers are present");
  1801. return 0;
  1802. }
  1803. netdev_for_each_lower_dev(upper_dev, other_dev, iter) {
  1804. if (!dpaa2_switch_port_dev_check(other_dev))
  1805. continue;
  1806. other_port_priv = netdev_priv(other_dev);
  1807. if (other_port_priv->ethsw_data != port_priv->ethsw_data) {
  1808. NL_SET_ERR_MSG_MOD(extack,
  1809. "Interface from a different DPSW is in the bridge already");
  1810. return -EINVAL;
  1811. }
  1812. }
  1813. return 0;
  1814. }
  1815. static int dpaa2_switch_port_prechangeupper(struct net_device *netdev,
  1816. struct netdev_notifier_changeupper_info *info)
  1817. {
  1818. struct netlink_ext_ack *extack;
  1819. struct net_device *upper_dev;
  1820. int err;
  1821. if (!dpaa2_switch_port_dev_check(netdev))
  1822. return 0;
  1823. extack = netdev_notifier_info_to_extack(&info->info);
  1824. upper_dev = info->upper_dev;
  1825. if (netif_is_bridge_master(upper_dev)) {
  1826. err = dpaa2_switch_prechangeupper_sanity_checks(netdev,
  1827. upper_dev,
  1828. extack);
  1829. if (err)
  1830. return err;
  1831. if (!info->linking)
  1832. dpaa2_switch_port_pre_bridge_leave(netdev);
  1833. }
  1834. return 0;
  1835. }
  1836. static int dpaa2_switch_port_changeupper(struct net_device *netdev,
  1837. struct netdev_notifier_changeupper_info *info)
  1838. {
  1839. struct netlink_ext_ack *extack;
  1840. struct net_device *upper_dev;
  1841. if (!dpaa2_switch_port_dev_check(netdev))
  1842. return 0;
  1843. extack = netdev_notifier_info_to_extack(&info->info);
  1844. upper_dev = info->upper_dev;
  1845. if (netif_is_bridge_master(upper_dev)) {
  1846. if (info->linking)
  1847. return dpaa2_switch_port_bridge_join(netdev,
  1848. upper_dev,
  1849. extack);
  1850. else
  1851. return dpaa2_switch_port_bridge_leave(netdev);
  1852. }
  1853. return 0;
  1854. }
  1855. static int dpaa2_switch_port_netdevice_event(struct notifier_block *nb,
  1856. unsigned long event, void *ptr)
  1857. {
  1858. struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
  1859. int err = 0;
  1860. switch (event) {
  1861. case NETDEV_PRECHANGEUPPER:
  1862. err = dpaa2_switch_port_prechangeupper(netdev, ptr);
  1863. if (err)
  1864. return notifier_from_errno(err);
  1865. break;
  1866. case NETDEV_CHANGEUPPER:
  1867. err = dpaa2_switch_port_changeupper(netdev, ptr);
  1868. if (err)
  1869. return notifier_from_errno(err);
  1870. break;
  1871. }
  1872. return NOTIFY_DONE;
  1873. }
  1874. struct ethsw_switchdev_event_work {
  1875. struct work_struct work;
  1876. struct switchdev_notifier_fdb_info fdb_info;
  1877. struct net_device *dev;
  1878. unsigned long event;
  1879. };
  1880. static void dpaa2_switch_event_work(struct work_struct *work)
  1881. {
  1882. struct ethsw_switchdev_event_work *switchdev_work =
  1883. container_of(work, struct ethsw_switchdev_event_work, work);
  1884. struct net_device *dev = switchdev_work->dev;
  1885. struct switchdev_notifier_fdb_info *fdb_info;
  1886. int err;
  1887. rtnl_lock();
  1888. fdb_info = &switchdev_work->fdb_info;
  1889. switch (switchdev_work->event) {
  1890. case SWITCHDEV_FDB_ADD_TO_DEVICE:
  1891. if (!fdb_info->added_by_user || fdb_info->is_local)
  1892. break;
  1893. if (is_unicast_ether_addr(fdb_info->addr))
  1894. err = dpaa2_switch_port_fdb_add_uc(netdev_priv(dev),
  1895. fdb_info->addr);
  1896. else
  1897. err = dpaa2_switch_port_fdb_add_mc(netdev_priv(dev),
  1898. fdb_info->addr);
  1899. if (err)
  1900. break;
  1901. fdb_info->offloaded = true;
  1902. call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
  1903. &fdb_info->info, NULL);
  1904. break;
  1905. case SWITCHDEV_FDB_DEL_TO_DEVICE:
  1906. if (!fdb_info->added_by_user || fdb_info->is_local)
  1907. break;
  1908. if (is_unicast_ether_addr(fdb_info->addr))
  1909. dpaa2_switch_port_fdb_del_uc(netdev_priv(dev), fdb_info->addr);
  1910. else
  1911. dpaa2_switch_port_fdb_del_mc(netdev_priv(dev), fdb_info->addr);
  1912. break;
  1913. }
  1914. rtnl_unlock();
  1915. kfree(switchdev_work->fdb_info.addr);
  1916. kfree(switchdev_work);
  1917. dev_put(dev);
  1918. }
  1919. /* Called under rcu_read_lock() */
  1920. static int dpaa2_switch_port_event(struct notifier_block *nb,
  1921. unsigned long event, void *ptr)
  1922. {
  1923. struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
  1924. struct ethsw_port_priv *port_priv = netdev_priv(dev);
  1925. struct ethsw_switchdev_event_work *switchdev_work;
  1926. struct switchdev_notifier_fdb_info *fdb_info = ptr;
  1927. struct ethsw_core *ethsw = port_priv->ethsw_data;
  1928. if (event == SWITCHDEV_PORT_ATTR_SET)
  1929. return dpaa2_switch_port_attr_set_event(dev, ptr);
  1930. if (!dpaa2_switch_port_dev_check(dev))
  1931. return NOTIFY_DONE;
  1932. switchdev_work = kzalloc_obj(*switchdev_work, GFP_ATOMIC);
  1933. if (!switchdev_work)
  1934. return NOTIFY_BAD;
  1935. INIT_WORK(&switchdev_work->work, dpaa2_switch_event_work);
  1936. switchdev_work->dev = dev;
  1937. switchdev_work->event = event;
  1938. switch (event) {
  1939. case SWITCHDEV_FDB_ADD_TO_DEVICE:
  1940. case SWITCHDEV_FDB_DEL_TO_DEVICE:
  1941. memcpy(&switchdev_work->fdb_info, ptr,
  1942. sizeof(switchdev_work->fdb_info));
  1943. switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
  1944. if (!switchdev_work->fdb_info.addr)
  1945. goto err_addr_alloc;
  1946. ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
  1947. fdb_info->addr);
  1948. /* Take a reference on the device to avoid being freed. */
  1949. dev_hold(dev);
  1950. break;
  1951. default:
  1952. kfree(switchdev_work);
  1953. return NOTIFY_DONE;
  1954. }
  1955. queue_work(ethsw->workqueue, &switchdev_work->work);
  1956. return NOTIFY_DONE;
  1957. err_addr_alloc:
  1958. kfree(switchdev_work);
  1959. return NOTIFY_BAD;
  1960. }
  1961. static int dpaa2_switch_port_obj_event(unsigned long event,
  1962. struct net_device *netdev,
  1963. struct switchdev_notifier_port_obj_info *port_obj_info)
  1964. {
  1965. int err = -EOPNOTSUPP;
  1966. if (!dpaa2_switch_port_dev_check(netdev))
  1967. return NOTIFY_DONE;
  1968. switch (event) {
  1969. case SWITCHDEV_PORT_OBJ_ADD:
  1970. err = dpaa2_switch_port_obj_add(netdev, port_obj_info->obj);
  1971. break;
  1972. case SWITCHDEV_PORT_OBJ_DEL:
  1973. err = dpaa2_switch_port_obj_del(netdev, port_obj_info->obj);
  1974. break;
  1975. }
  1976. port_obj_info->handled = true;
  1977. return notifier_from_errno(err);
  1978. }
  1979. static int dpaa2_switch_port_blocking_event(struct notifier_block *nb,
  1980. unsigned long event, void *ptr)
  1981. {
  1982. struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
  1983. switch (event) {
  1984. case SWITCHDEV_PORT_OBJ_ADD:
  1985. case SWITCHDEV_PORT_OBJ_DEL:
  1986. return dpaa2_switch_port_obj_event(event, dev, ptr);
  1987. case SWITCHDEV_PORT_ATTR_SET:
  1988. return dpaa2_switch_port_attr_set_event(dev, ptr);
  1989. }
  1990. return NOTIFY_DONE;
  1991. }
  1992. /* Build a linear skb based on a single-buffer frame descriptor */
  1993. static struct sk_buff *dpaa2_switch_build_linear_skb(struct ethsw_core *ethsw,
  1994. const struct dpaa2_fd *fd)
  1995. {
  1996. u16 fd_offset = dpaa2_fd_get_offset(fd);
  1997. dma_addr_t addr = dpaa2_fd_get_addr(fd);
  1998. u32 fd_length = dpaa2_fd_get_len(fd);
  1999. struct device *dev = ethsw->dev;
  2000. struct sk_buff *skb = NULL;
  2001. void *fd_vaddr;
  2002. fd_vaddr = dpaa2_iova_to_virt(ethsw->iommu_domain, addr);
  2003. dma_unmap_page(dev, addr, DPAA2_SWITCH_RX_BUF_SIZE,
  2004. DMA_FROM_DEVICE);
  2005. skb = build_skb(fd_vaddr, DPAA2_SWITCH_RX_BUF_SIZE +
  2006. SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
  2007. if (unlikely(!skb)) {
  2008. dev_err(dev, "build_skb() failed\n");
  2009. return NULL;
  2010. }
  2011. skb_reserve(skb, fd_offset);
  2012. skb_put(skb, fd_length);
  2013. ethsw->buf_count--;
  2014. return skb;
  2015. }
  2016. static void dpaa2_switch_tx_conf(struct dpaa2_switch_fq *fq,
  2017. const struct dpaa2_fd *fd)
  2018. {
  2019. dpaa2_switch_free_fd(fq->ethsw, fd);
  2020. }
  2021. static void dpaa2_switch_rx(struct dpaa2_switch_fq *fq,
  2022. const struct dpaa2_fd *fd)
  2023. {
  2024. struct ethsw_core *ethsw = fq->ethsw;
  2025. struct ethsw_port_priv *port_priv;
  2026. struct net_device *netdev;
  2027. struct vlan_ethhdr *hdr;
  2028. struct sk_buff *skb;
  2029. u16 vlan_tci, vid;
  2030. int if_id, err;
  2031. /* get switch ingress interface ID */
  2032. if_id = upper_32_bits(dpaa2_fd_get_flc(fd)) & 0x0000FFFF;
  2033. if (if_id >= ethsw->sw_attr.num_ifs) {
  2034. dev_err(ethsw->dev, "Frame received from unknown interface!\n");
  2035. goto err_free_fd;
  2036. }
  2037. port_priv = ethsw->ports[if_id];
  2038. netdev = port_priv->netdev;
  2039. /* build the SKB based on the FD received */
  2040. if (dpaa2_fd_get_format(fd) != dpaa2_fd_single) {
  2041. if (net_ratelimit()) {
  2042. netdev_err(netdev, "Received invalid frame format\n");
  2043. goto err_free_fd;
  2044. }
  2045. }
  2046. skb = dpaa2_switch_build_linear_skb(ethsw, fd);
  2047. if (unlikely(!skb))
  2048. goto err_free_fd;
  2049. skb_reset_mac_header(skb);
  2050. /* Remove the VLAN header if the packet that we just received has a vid
  2051. * equal to the port PVIDs. Since the dpaa2-switch can operate only in
  2052. * VLAN-aware mode and no alterations are made on the packet when it's
  2053. * redirected/mirrored to the control interface, we are sure that there
  2054. * will always be a VLAN header present.
  2055. */
  2056. hdr = vlan_eth_hdr(skb);
  2057. vid = ntohs(hdr->h_vlan_TCI) & VLAN_VID_MASK;
  2058. if (vid == port_priv->pvid) {
  2059. err = __skb_vlan_pop(skb, &vlan_tci);
  2060. if (err) {
  2061. dev_info(ethsw->dev, "__skb_vlan_pop() returned %d", err);
  2062. goto err_free_fd;
  2063. }
  2064. }
  2065. skb->dev = netdev;
  2066. skb->protocol = eth_type_trans(skb, skb->dev);
  2067. /* Setup the offload_fwd_mark only if the port is under a bridge */
  2068. skb->offload_fwd_mark = !!(port_priv->fdb->bridge_dev);
  2069. netif_receive_skb(skb);
  2070. return;
  2071. err_free_fd:
  2072. dpaa2_switch_free_fd(ethsw, fd);
  2073. }
  2074. static void dpaa2_switch_detect_features(struct ethsw_core *ethsw)
  2075. {
  2076. ethsw->features = 0;
  2077. if (ethsw->major > 8 || (ethsw->major == 8 && ethsw->minor >= 6))
  2078. ethsw->features |= ETHSW_FEATURE_MAC_ADDR;
  2079. }
  2080. static int dpaa2_switch_setup_fqs(struct ethsw_core *ethsw)
  2081. {
  2082. struct dpsw_ctrl_if_attr ctrl_if_attr;
  2083. struct device *dev = ethsw->dev;
  2084. int i = 0;
  2085. int err;
  2086. err = dpsw_ctrl_if_get_attributes(ethsw->mc_io, 0, ethsw->dpsw_handle,
  2087. &ctrl_if_attr);
  2088. if (err) {
  2089. dev_err(dev, "dpsw_ctrl_if_get_attributes() = %d\n", err);
  2090. return err;
  2091. }
  2092. ethsw->fq[i].fqid = ctrl_if_attr.rx_fqid;
  2093. ethsw->fq[i].ethsw = ethsw;
  2094. ethsw->fq[i++].type = DPSW_QUEUE_RX;
  2095. ethsw->fq[i].fqid = ctrl_if_attr.tx_err_conf_fqid;
  2096. ethsw->fq[i].ethsw = ethsw;
  2097. ethsw->fq[i++].type = DPSW_QUEUE_TX_ERR_CONF;
  2098. return 0;
  2099. }
  2100. /* Free buffers acquired from the buffer pool or which were meant to
  2101. * be released in the pool
  2102. */
  2103. static void dpaa2_switch_free_bufs(struct ethsw_core *ethsw, u64 *buf_array, int count)
  2104. {
  2105. struct device *dev = ethsw->dev;
  2106. void *vaddr;
  2107. int i;
  2108. for (i = 0; i < count; i++) {
  2109. vaddr = dpaa2_iova_to_virt(ethsw->iommu_domain, buf_array[i]);
  2110. dma_unmap_page(dev, buf_array[i], DPAA2_SWITCH_RX_BUF_SIZE,
  2111. DMA_FROM_DEVICE);
  2112. free_pages((unsigned long)vaddr, 0);
  2113. }
  2114. }
  2115. /* Perform a single release command to add buffers
  2116. * to the specified buffer pool
  2117. */
  2118. static int dpaa2_switch_add_bufs(struct ethsw_core *ethsw, u16 bpid)
  2119. {
  2120. struct device *dev = ethsw->dev;
  2121. u64 buf_array[BUFS_PER_CMD];
  2122. struct page *page;
  2123. int retries = 0;
  2124. dma_addr_t addr;
  2125. int err;
  2126. int i;
  2127. for (i = 0; i < BUFS_PER_CMD; i++) {
  2128. /* Allocate one page for each Rx buffer. WRIOP sees
  2129. * the entire page except for a tailroom reserved for
  2130. * skb shared info
  2131. */
  2132. page = dev_alloc_pages(0);
  2133. if (!page) {
  2134. dev_err(dev, "buffer allocation failed\n");
  2135. goto err_alloc;
  2136. }
  2137. addr = dma_map_page(dev, page, 0, DPAA2_SWITCH_RX_BUF_SIZE,
  2138. DMA_FROM_DEVICE);
  2139. if (dma_mapping_error(dev, addr)) {
  2140. dev_err(dev, "dma_map_single() failed\n");
  2141. goto err_map;
  2142. }
  2143. buf_array[i] = addr;
  2144. }
  2145. release_bufs:
  2146. /* In case the portal is busy, retry until successful or
  2147. * max retries hit.
  2148. */
  2149. while ((err = dpaa2_io_service_release(NULL, bpid,
  2150. buf_array, i)) == -EBUSY) {
  2151. if (retries++ >= DPAA2_SWITCH_SWP_BUSY_RETRIES)
  2152. break;
  2153. cpu_relax();
  2154. }
  2155. /* If release command failed, clean up and bail out. */
  2156. if (err) {
  2157. dpaa2_switch_free_bufs(ethsw, buf_array, i);
  2158. return 0;
  2159. }
  2160. return i;
  2161. err_map:
  2162. __free_pages(page, 0);
  2163. err_alloc:
  2164. /* If we managed to allocate at least some buffers,
  2165. * release them to hardware
  2166. */
  2167. if (i)
  2168. goto release_bufs;
  2169. return 0;
  2170. }
  2171. static int dpaa2_switch_refill_bp(struct ethsw_core *ethsw)
  2172. {
  2173. int *count = &ethsw->buf_count;
  2174. int new_count;
  2175. int err = 0;
  2176. if (unlikely(*count < DPAA2_ETHSW_REFILL_THRESH)) {
  2177. do {
  2178. new_count = dpaa2_switch_add_bufs(ethsw, ethsw->bpid);
  2179. if (unlikely(!new_count)) {
  2180. /* Out of memory; abort for now, we'll
  2181. * try later on
  2182. */
  2183. break;
  2184. }
  2185. *count += new_count;
  2186. } while (*count < DPAA2_ETHSW_NUM_BUFS);
  2187. if (unlikely(*count < DPAA2_ETHSW_NUM_BUFS))
  2188. err = -ENOMEM;
  2189. }
  2190. return err;
  2191. }
  2192. static int dpaa2_switch_seed_bp(struct ethsw_core *ethsw)
  2193. {
  2194. int *count, ret, i;
  2195. for (i = 0; i < DPAA2_ETHSW_NUM_BUFS; i += BUFS_PER_CMD) {
  2196. ret = dpaa2_switch_add_bufs(ethsw, ethsw->bpid);
  2197. count = &ethsw->buf_count;
  2198. *count += ret;
  2199. if (unlikely(ret < BUFS_PER_CMD))
  2200. return -ENOMEM;
  2201. }
  2202. return 0;
  2203. }
  2204. static void dpaa2_switch_drain_bp(struct ethsw_core *ethsw)
  2205. {
  2206. u64 buf_array[BUFS_PER_CMD];
  2207. int ret;
  2208. do {
  2209. ret = dpaa2_io_service_acquire(NULL, ethsw->bpid,
  2210. buf_array, BUFS_PER_CMD);
  2211. if (ret < 0) {
  2212. dev_err(ethsw->dev,
  2213. "dpaa2_io_service_acquire() = %d\n", ret);
  2214. return;
  2215. }
  2216. dpaa2_switch_free_bufs(ethsw, buf_array, ret);
  2217. } while (ret);
  2218. }
  2219. static int dpaa2_switch_setup_dpbp(struct ethsw_core *ethsw)
  2220. {
  2221. struct dpsw_ctrl_if_pools_cfg dpsw_ctrl_if_pools_cfg = { 0 };
  2222. struct device *dev = ethsw->dev;
  2223. struct fsl_mc_device *dpbp_dev;
  2224. struct dpbp_attr dpbp_attrs;
  2225. int err;
  2226. err = fsl_mc_object_allocate(to_fsl_mc_device(dev), FSL_MC_POOL_DPBP,
  2227. &dpbp_dev);
  2228. if (err) {
  2229. if (err == -ENXIO)
  2230. err = -EPROBE_DEFER;
  2231. else
  2232. dev_err(dev, "DPBP device allocation failed\n");
  2233. return err;
  2234. }
  2235. ethsw->dpbp_dev = dpbp_dev;
  2236. err = dpbp_open(ethsw->mc_io, 0, dpbp_dev->obj_desc.id,
  2237. &dpbp_dev->mc_handle);
  2238. if (err) {
  2239. dev_err(dev, "dpbp_open() failed\n");
  2240. goto err_open;
  2241. }
  2242. err = dpbp_reset(ethsw->mc_io, 0, dpbp_dev->mc_handle);
  2243. if (err) {
  2244. dev_err(dev, "dpbp_reset() failed\n");
  2245. goto err_reset;
  2246. }
  2247. err = dpbp_enable(ethsw->mc_io, 0, dpbp_dev->mc_handle);
  2248. if (err) {
  2249. dev_err(dev, "dpbp_enable() failed\n");
  2250. goto err_enable;
  2251. }
  2252. err = dpbp_get_attributes(ethsw->mc_io, 0, dpbp_dev->mc_handle,
  2253. &dpbp_attrs);
  2254. if (err) {
  2255. dev_err(dev, "dpbp_get_attributes() failed\n");
  2256. goto err_get_attr;
  2257. }
  2258. dpsw_ctrl_if_pools_cfg.num_dpbp = 1;
  2259. dpsw_ctrl_if_pools_cfg.pools[0].dpbp_id = dpbp_attrs.id;
  2260. dpsw_ctrl_if_pools_cfg.pools[0].buffer_size = DPAA2_SWITCH_RX_BUF_SIZE;
  2261. dpsw_ctrl_if_pools_cfg.pools[0].backup_pool = 0;
  2262. err = dpsw_ctrl_if_set_pools(ethsw->mc_io, 0, ethsw->dpsw_handle,
  2263. &dpsw_ctrl_if_pools_cfg);
  2264. if (err) {
  2265. dev_err(dev, "dpsw_ctrl_if_set_pools() failed\n");
  2266. goto err_get_attr;
  2267. }
  2268. ethsw->bpid = dpbp_attrs.bpid;
  2269. return 0;
  2270. err_get_attr:
  2271. dpbp_disable(ethsw->mc_io, 0, dpbp_dev->mc_handle);
  2272. err_enable:
  2273. err_reset:
  2274. dpbp_close(ethsw->mc_io, 0, dpbp_dev->mc_handle);
  2275. err_open:
  2276. fsl_mc_object_free(dpbp_dev);
  2277. return err;
  2278. }
  2279. static void dpaa2_switch_free_dpbp(struct ethsw_core *ethsw)
  2280. {
  2281. dpbp_disable(ethsw->mc_io, 0, ethsw->dpbp_dev->mc_handle);
  2282. dpbp_close(ethsw->mc_io, 0, ethsw->dpbp_dev->mc_handle);
  2283. fsl_mc_object_free(ethsw->dpbp_dev);
  2284. }
  2285. static int dpaa2_switch_alloc_rings(struct ethsw_core *ethsw)
  2286. {
  2287. int i;
  2288. for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++) {
  2289. ethsw->fq[i].store =
  2290. dpaa2_io_store_create(DPAA2_SWITCH_STORE_SIZE,
  2291. ethsw->dev);
  2292. if (!ethsw->fq[i].store) {
  2293. dev_err(ethsw->dev, "dpaa2_io_store_create failed\n");
  2294. while (--i >= 0)
  2295. dpaa2_io_store_destroy(ethsw->fq[i].store);
  2296. return -ENOMEM;
  2297. }
  2298. }
  2299. return 0;
  2300. }
  2301. static void dpaa2_switch_destroy_rings(struct ethsw_core *ethsw)
  2302. {
  2303. int i;
  2304. for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++)
  2305. dpaa2_io_store_destroy(ethsw->fq[i].store);
  2306. }
  2307. static int dpaa2_switch_pull_fq(struct dpaa2_switch_fq *fq)
  2308. {
  2309. int err, retries = 0;
  2310. /* Try to pull from the FQ while the portal is busy and we didn't hit
  2311. * the maximum number fo retries
  2312. */
  2313. do {
  2314. err = dpaa2_io_service_pull_fq(NULL, fq->fqid, fq->store);
  2315. cpu_relax();
  2316. } while (err == -EBUSY && retries++ < DPAA2_SWITCH_SWP_BUSY_RETRIES);
  2317. if (unlikely(err))
  2318. dev_err(fq->ethsw->dev, "dpaa2_io_service_pull err %d", err);
  2319. return err;
  2320. }
  2321. /* Consume all frames pull-dequeued into the store */
  2322. static int dpaa2_switch_store_consume(struct dpaa2_switch_fq *fq)
  2323. {
  2324. struct ethsw_core *ethsw = fq->ethsw;
  2325. int cleaned = 0, is_last;
  2326. struct dpaa2_dq *dq;
  2327. int retries = 0;
  2328. do {
  2329. /* Get the next available FD from the store */
  2330. dq = dpaa2_io_store_next(fq->store, &is_last);
  2331. if (unlikely(!dq)) {
  2332. if (retries++ >= DPAA2_SWITCH_SWP_BUSY_RETRIES) {
  2333. dev_err_once(ethsw->dev,
  2334. "No valid dequeue response\n");
  2335. return -ETIMEDOUT;
  2336. }
  2337. continue;
  2338. }
  2339. if (fq->type == DPSW_QUEUE_RX)
  2340. dpaa2_switch_rx(fq, dpaa2_dq_fd(dq));
  2341. else
  2342. dpaa2_switch_tx_conf(fq, dpaa2_dq_fd(dq));
  2343. cleaned++;
  2344. } while (!is_last);
  2345. return cleaned;
  2346. }
  2347. /* NAPI poll routine */
  2348. static int dpaa2_switch_poll(struct napi_struct *napi, int budget)
  2349. {
  2350. int err, cleaned = 0, store_cleaned, work_done;
  2351. struct dpaa2_switch_fq *fq;
  2352. int retries = 0;
  2353. fq = container_of(napi, struct dpaa2_switch_fq, napi);
  2354. do {
  2355. err = dpaa2_switch_pull_fq(fq);
  2356. if (unlikely(err))
  2357. break;
  2358. /* Refill pool if appropriate */
  2359. dpaa2_switch_refill_bp(fq->ethsw);
  2360. store_cleaned = dpaa2_switch_store_consume(fq);
  2361. cleaned += store_cleaned;
  2362. if (cleaned >= budget) {
  2363. work_done = budget;
  2364. goto out;
  2365. }
  2366. } while (store_cleaned);
  2367. /* We didn't consume the entire budget, so finish napi and re-enable
  2368. * data availability notifications
  2369. */
  2370. napi_complete_done(napi, cleaned);
  2371. do {
  2372. err = dpaa2_io_service_rearm(NULL, &fq->nctx);
  2373. cpu_relax();
  2374. } while (err == -EBUSY && retries++ < DPAA2_SWITCH_SWP_BUSY_RETRIES);
  2375. work_done = max(cleaned, 1);
  2376. out:
  2377. return work_done;
  2378. }
  2379. static void dpaa2_switch_fqdan_cb(struct dpaa2_io_notification_ctx *nctx)
  2380. {
  2381. struct dpaa2_switch_fq *fq;
  2382. fq = container_of(nctx, struct dpaa2_switch_fq, nctx);
  2383. napi_schedule(&fq->napi);
  2384. }
  2385. static int dpaa2_switch_setup_dpio(struct ethsw_core *ethsw)
  2386. {
  2387. struct dpsw_ctrl_if_queue_cfg queue_cfg;
  2388. struct dpaa2_io_notification_ctx *nctx;
  2389. int err, i, j;
  2390. for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++) {
  2391. nctx = &ethsw->fq[i].nctx;
  2392. /* Register a new software context for the FQID.
  2393. * By using NULL as the first parameter, we specify that we do
  2394. * not care on which cpu are interrupts received for this queue
  2395. */
  2396. nctx->is_cdan = 0;
  2397. nctx->id = ethsw->fq[i].fqid;
  2398. nctx->desired_cpu = DPAA2_IO_ANY_CPU;
  2399. nctx->cb = dpaa2_switch_fqdan_cb;
  2400. err = dpaa2_io_service_register(NULL, nctx, ethsw->dev);
  2401. if (err) {
  2402. err = -EPROBE_DEFER;
  2403. goto err_register;
  2404. }
  2405. queue_cfg.options = DPSW_CTRL_IF_QUEUE_OPT_DEST |
  2406. DPSW_CTRL_IF_QUEUE_OPT_USER_CTX;
  2407. queue_cfg.dest_cfg.dest_type = DPSW_CTRL_IF_DEST_DPIO;
  2408. queue_cfg.dest_cfg.dest_id = nctx->dpio_id;
  2409. queue_cfg.dest_cfg.priority = 0;
  2410. queue_cfg.user_ctx = nctx->qman64;
  2411. err = dpsw_ctrl_if_set_queue(ethsw->mc_io, 0,
  2412. ethsw->dpsw_handle,
  2413. ethsw->fq[i].type,
  2414. &queue_cfg);
  2415. if (err)
  2416. goto err_set_queue;
  2417. }
  2418. return 0;
  2419. err_set_queue:
  2420. dpaa2_io_service_deregister(NULL, nctx, ethsw->dev);
  2421. err_register:
  2422. for (j = 0; j < i; j++)
  2423. dpaa2_io_service_deregister(NULL, &ethsw->fq[j].nctx,
  2424. ethsw->dev);
  2425. return err;
  2426. }
  2427. static void dpaa2_switch_free_dpio(struct ethsw_core *ethsw)
  2428. {
  2429. int i;
  2430. for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++)
  2431. dpaa2_io_service_deregister(NULL, &ethsw->fq[i].nctx,
  2432. ethsw->dev);
  2433. }
  2434. static int dpaa2_switch_ctrl_if_setup(struct ethsw_core *ethsw)
  2435. {
  2436. int err;
  2437. /* setup FQs for Rx and Tx Conf */
  2438. err = dpaa2_switch_setup_fqs(ethsw);
  2439. if (err)
  2440. return err;
  2441. /* setup the buffer pool needed on the Rx path */
  2442. err = dpaa2_switch_setup_dpbp(ethsw);
  2443. if (err)
  2444. return err;
  2445. err = dpaa2_switch_alloc_rings(ethsw);
  2446. if (err)
  2447. goto err_free_dpbp;
  2448. err = dpaa2_switch_setup_dpio(ethsw);
  2449. if (err)
  2450. goto err_destroy_rings;
  2451. err = dpaa2_switch_seed_bp(ethsw);
  2452. if (err)
  2453. goto err_deregister_dpio;
  2454. err = dpsw_ctrl_if_enable(ethsw->mc_io, 0, ethsw->dpsw_handle);
  2455. if (err) {
  2456. dev_err(ethsw->dev, "dpsw_ctrl_if_enable err %d\n", err);
  2457. goto err_drain_dpbp;
  2458. }
  2459. return 0;
  2460. err_drain_dpbp:
  2461. dpaa2_switch_drain_bp(ethsw);
  2462. err_deregister_dpio:
  2463. dpaa2_switch_free_dpio(ethsw);
  2464. err_destroy_rings:
  2465. dpaa2_switch_destroy_rings(ethsw);
  2466. err_free_dpbp:
  2467. dpaa2_switch_free_dpbp(ethsw);
  2468. return err;
  2469. }
  2470. static void dpaa2_switch_remove_port(struct ethsw_core *ethsw,
  2471. u16 port_idx)
  2472. {
  2473. struct ethsw_port_priv *port_priv = ethsw->ports[port_idx];
  2474. dpaa2_switch_port_disconnect_mac(port_priv);
  2475. free_netdev(port_priv->netdev);
  2476. ethsw->ports[port_idx] = NULL;
  2477. }
  2478. static int dpaa2_switch_init(struct fsl_mc_device *sw_dev)
  2479. {
  2480. struct device *dev = &sw_dev->dev;
  2481. struct ethsw_core *ethsw = dev_get_drvdata(dev);
  2482. struct dpsw_vlan_if_cfg vcfg = {0};
  2483. struct dpsw_tci_cfg tci_cfg = {0};
  2484. struct dpsw_stp_cfg stp_cfg;
  2485. int err;
  2486. u16 i;
  2487. ethsw->dev_id = sw_dev->obj_desc.id;
  2488. err = dpsw_open(ethsw->mc_io, 0, ethsw->dev_id, &ethsw->dpsw_handle);
  2489. if (err) {
  2490. dev_err(dev, "dpsw_open err %d\n", err);
  2491. return err;
  2492. }
  2493. err = dpsw_get_attributes(ethsw->mc_io, 0, ethsw->dpsw_handle,
  2494. &ethsw->sw_attr);
  2495. if (err) {
  2496. dev_err(dev, "dpsw_get_attributes err %d\n", err);
  2497. goto err_close;
  2498. }
  2499. if (!ethsw->sw_attr.num_ifs) {
  2500. dev_err(dev, "DPSW device has no interfaces\n");
  2501. err = -ENODEV;
  2502. goto err_close;
  2503. }
  2504. if (ethsw->sw_attr.num_ifs >= DPSW_MAX_IF) {
  2505. dev_err(dev, "DPSW num_ifs %u exceeds max %u\n",
  2506. ethsw->sw_attr.num_ifs, DPSW_MAX_IF);
  2507. err = -EINVAL;
  2508. goto err_close;
  2509. }
  2510. err = dpsw_get_api_version(ethsw->mc_io, 0,
  2511. &ethsw->major,
  2512. &ethsw->minor);
  2513. if (err) {
  2514. dev_err(dev, "dpsw_get_api_version err %d\n", err);
  2515. goto err_close;
  2516. }
  2517. /* Minimum supported DPSW version check */
  2518. if (ethsw->major < DPSW_MIN_VER_MAJOR ||
  2519. (ethsw->major == DPSW_MIN_VER_MAJOR &&
  2520. ethsw->minor < DPSW_MIN_VER_MINOR)) {
  2521. dev_err(dev, "DPSW version %d:%d not supported. Use firmware 10.28.0 or greater.\n",
  2522. ethsw->major, ethsw->minor);
  2523. err = -EOPNOTSUPP;
  2524. goto err_close;
  2525. }
  2526. if (!dpaa2_switch_supports_cpu_traffic(ethsw)) {
  2527. err = -EOPNOTSUPP;
  2528. goto err_close;
  2529. }
  2530. dpaa2_switch_detect_features(ethsw);
  2531. err = dpsw_reset(ethsw->mc_io, 0, ethsw->dpsw_handle);
  2532. if (err) {
  2533. dev_err(dev, "dpsw_reset err %d\n", err);
  2534. goto err_close;
  2535. }
  2536. stp_cfg.vlan_id = DEFAULT_VLAN_ID;
  2537. stp_cfg.state = DPSW_STP_STATE_FORWARDING;
  2538. for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
  2539. err = dpsw_if_disable(ethsw->mc_io, 0, ethsw->dpsw_handle, i);
  2540. if (err) {
  2541. dev_err(dev, "dpsw_if_disable err %d\n", err);
  2542. goto err_close;
  2543. }
  2544. err = dpsw_if_set_stp(ethsw->mc_io, 0, ethsw->dpsw_handle, i,
  2545. &stp_cfg);
  2546. if (err) {
  2547. dev_err(dev, "dpsw_if_set_stp err %d for port %d\n",
  2548. err, i);
  2549. goto err_close;
  2550. }
  2551. /* Switch starts with all ports configured to VLAN 1. Need to
  2552. * remove this setting to allow configuration at bridge join
  2553. */
  2554. vcfg.num_ifs = 1;
  2555. vcfg.if_id[0] = i;
  2556. err = dpsw_vlan_remove_if_untagged(ethsw->mc_io, 0, ethsw->dpsw_handle,
  2557. DEFAULT_VLAN_ID, &vcfg);
  2558. if (err) {
  2559. dev_err(dev, "dpsw_vlan_remove_if_untagged err %d\n",
  2560. err);
  2561. goto err_close;
  2562. }
  2563. tci_cfg.vlan_id = 4095;
  2564. err = dpsw_if_set_tci(ethsw->mc_io, 0, ethsw->dpsw_handle, i, &tci_cfg);
  2565. if (err) {
  2566. dev_err(dev, "dpsw_if_set_tci err %d\n", err);
  2567. goto err_close;
  2568. }
  2569. err = dpsw_vlan_remove_if(ethsw->mc_io, 0, ethsw->dpsw_handle,
  2570. DEFAULT_VLAN_ID, &vcfg);
  2571. if (err) {
  2572. dev_err(dev, "dpsw_vlan_remove_if err %d\n", err);
  2573. goto err_close;
  2574. }
  2575. }
  2576. err = dpsw_vlan_remove(ethsw->mc_io, 0, ethsw->dpsw_handle, DEFAULT_VLAN_ID);
  2577. if (err) {
  2578. dev_err(dev, "dpsw_vlan_remove err %d\n", err);
  2579. goto err_close;
  2580. }
  2581. ethsw->workqueue = alloc_ordered_workqueue("%s_%d_ordered",
  2582. WQ_MEM_RECLAIM, "ethsw",
  2583. ethsw->sw_attr.id);
  2584. if (!ethsw->workqueue) {
  2585. err = -ENOMEM;
  2586. goto err_close;
  2587. }
  2588. err = dpsw_fdb_remove(ethsw->mc_io, 0, ethsw->dpsw_handle, 0);
  2589. if (err)
  2590. goto err_destroy_ordered_workqueue;
  2591. err = dpaa2_switch_ctrl_if_setup(ethsw);
  2592. if (err)
  2593. goto err_destroy_ordered_workqueue;
  2594. return 0;
  2595. err_destroy_ordered_workqueue:
  2596. destroy_workqueue(ethsw->workqueue);
  2597. err_close:
  2598. dpsw_close(ethsw->mc_io, 0, ethsw->dpsw_handle);
  2599. return err;
  2600. }
  2601. /* Add an ACL to redirect frames with specific destination MAC address to
  2602. * control interface
  2603. */
  2604. static int dpaa2_switch_port_trap_mac_addr(struct ethsw_port_priv *port_priv,
  2605. const char *mac)
  2606. {
  2607. struct dpaa2_switch_acl_entry acl_entry = {0};
  2608. /* Match on the destination MAC address */
  2609. ether_addr_copy(acl_entry.key.match.l2_dest_mac, mac);
  2610. eth_broadcast_addr(acl_entry.key.mask.l2_dest_mac);
  2611. /* Trap to CPU */
  2612. acl_entry.cfg.precedence = 0;
  2613. acl_entry.cfg.result.action = DPSW_ACL_ACTION_REDIRECT_TO_CTRL_IF;
  2614. return dpaa2_switch_acl_entry_add(port_priv->filter_block, &acl_entry);
  2615. }
  2616. static int dpaa2_switch_port_init(struct ethsw_port_priv *port_priv, u16 port)
  2617. {
  2618. const char stpa[ETH_ALEN] = {0x01, 0x80, 0xc2, 0x00, 0x00, 0x00};
  2619. struct switchdev_obj_port_vlan vlan = {
  2620. .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
  2621. .vid = DEFAULT_VLAN_ID,
  2622. .flags = BRIDGE_VLAN_INFO_UNTAGGED | BRIDGE_VLAN_INFO_PVID,
  2623. };
  2624. struct net_device *netdev = port_priv->netdev;
  2625. struct ethsw_core *ethsw = port_priv->ethsw_data;
  2626. struct dpaa2_switch_filter_block *filter_block;
  2627. struct dpsw_fdb_cfg fdb_cfg = {0};
  2628. struct dpsw_if_attr dpsw_if_attr;
  2629. struct dpaa2_switch_fdb *fdb;
  2630. struct dpsw_acl_cfg acl_cfg;
  2631. u16 fdb_id, acl_tbl_id;
  2632. int err;
  2633. /* Get the Tx queue for this specific port */
  2634. err = dpsw_if_get_attributes(ethsw->mc_io, 0, ethsw->dpsw_handle,
  2635. port_priv->idx, &dpsw_if_attr);
  2636. if (err) {
  2637. netdev_err(netdev, "dpsw_if_get_attributes err %d\n", err);
  2638. return err;
  2639. }
  2640. port_priv->tx_qdid = dpsw_if_attr.qdid;
  2641. /* Create a FDB table for this particular switch port */
  2642. fdb_cfg.num_fdb_entries = ethsw->sw_attr.max_fdb_entries / ethsw->sw_attr.num_ifs;
  2643. err = dpsw_fdb_add(ethsw->mc_io, 0, ethsw->dpsw_handle,
  2644. &fdb_id, &fdb_cfg);
  2645. if (err) {
  2646. netdev_err(netdev, "dpsw_fdb_add err %d\n", err);
  2647. return err;
  2648. }
  2649. /* Find an unused dpaa2_switch_fdb structure and use it */
  2650. fdb = dpaa2_switch_fdb_get_unused(ethsw);
  2651. fdb->fdb_id = fdb_id;
  2652. fdb->in_use = true;
  2653. fdb->bridge_dev = NULL;
  2654. port_priv->fdb = fdb;
  2655. /* We need to add VLAN 1 as the PVID on this port until it is under a
  2656. * bridge since the DPAA2 switch is not able to handle the traffic in a
  2657. * VLAN unaware fashion
  2658. */
  2659. err = dpaa2_switch_port_vlans_add(netdev, &vlan);
  2660. if (err)
  2661. return err;
  2662. /* Setup the egress flooding domains (broadcast, unknown unicast */
  2663. err = dpaa2_switch_fdb_set_egress_flood(ethsw, port_priv->fdb->fdb_id);
  2664. if (err)
  2665. return err;
  2666. /* Create an ACL table to be used by this switch port */
  2667. acl_cfg.max_entries = DPAA2_ETHSW_PORT_MAX_ACL_ENTRIES;
  2668. err = dpsw_acl_add(ethsw->mc_io, 0, ethsw->dpsw_handle,
  2669. &acl_tbl_id, &acl_cfg);
  2670. if (err) {
  2671. netdev_err(netdev, "dpsw_acl_add err %d\n", err);
  2672. return err;
  2673. }
  2674. filter_block = dpaa2_switch_filter_block_get_unused(ethsw);
  2675. filter_block->ethsw = ethsw;
  2676. filter_block->acl_id = acl_tbl_id;
  2677. filter_block->in_use = true;
  2678. filter_block->num_acl_rules = 0;
  2679. INIT_LIST_HEAD(&filter_block->acl_entries);
  2680. INIT_LIST_HEAD(&filter_block->mirror_entries);
  2681. err = dpaa2_switch_port_acl_tbl_bind(port_priv, filter_block);
  2682. if (err)
  2683. return err;
  2684. err = dpaa2_switch_port_trap_mac_addr(port_priv, stpa);
  2685. if (err)
  2686. return err;
  2687. return err;
  2688. }
  2689. static void dpaa2_switch_ctrl_if_teardown(struct ethsw_core *ethsw)
  2690. {
  2691. dpsw_ctrl_if_disable(ethsw->mc_io, 0, ethsw->dpsw_handle);
  2692. dpaa2_switch_free_dpio(ethsw);
  2693. dpaa2_switch_destroy_rings(ethsw);
  2694. dpaa2_switch_drain_bp(ethsw);
  2695. dpaa2_switch_free_dpbp(ethsw);
  2696. }
  2697. static void dpaa2_switch_teardown(struct fsl_mc_device *sw_dev)
  2698. {
  2699. struct device *dev = &sw_dev->dev;
  2700. struct ethsw_core *ethsw = dev_get_drvdata(dev);
  2701. int err;
  2702. dpaa2_switch_ctrl_if_teardown(ethsw);
  2703. destroy_workqueue(ethsw->workqueue);
  2704. err = dpsw_close(ethsw->mc_io, 0, ethsw->dpsw_handle);
  2705. if (err)
  2706. dev_warn(dev, "dpsw_close err %d\n", err);
  2707. }
  2708. static void dpaa2_switch_remove(struct fsl_mc_device *sw_dev)
  2709. {
  2710. struct ethsw_port_priv *port_priv;
  2711. struct ethsw_core *ethsw;
  2712. struct device *dev;
  2713. int i;
  2714. dev = &sw_dev->dev;
  2715. ethsw = dev_get_drvdata(dev);
  2716. dpaa2_switch_teardown_irqs(sw_dev);
  2717. dpsw_disable(ethsw->mc_io, 0, ethsw->dpsw_handle);
  2718. for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
  2719. port_priv = ethsw->ports[i];
  2720. unregister_netdev(port_priv->netdev);
  2721. dpaa2_switch_remove_port(ethsw, i);
  2722. }
  2723. kfree(ethsw->fdbs);
  2724. kfree(ethsw->filter_blocks);
  2725. kfree(ethsw->ports);
  2726. dpaa2_switch_teardown(sw_dev);
  2727. fsl_mc_portal_free(ethsw->mc_io);
  2728. kfree(ethsw);
  2729. dev_set_drvdata(dev, NULL);
  2730. }
  2731. static int dpaa2_switch_probe_port(struct ethsw_core *ethsw,
  2732. u16 port_idx)
  2733. {
  2734. struct ethsw_port_priv *port_priv;
  2735. struct device *dev = ethsw->dev;
  2736. struct net_device *port_netdev;
  2737. int err;
  2738. port_netdev = alloc_etherdev(sizeof(struct ethsw_port_priv));
  2739. if (!port_netdev) {
  2740. dev_err(dev, "alloc_etherdev error\n");
  2741. return -ENOMEM;
  2742. }
  2743. port_priv = netdev_priv(port_netdev);
  2744. port_priv->netdev = port_netdev;
  2745. port_priv->ethsw_data = ethsw;
  2746. mutex_init(&port_priv->mac_lock);
  2747. port_priv->idx = port_idx;
  2748. port_priv->stp_state = BR_STATE_FORWARDING;
  2749. SET_NETDEV_DEV(port_netdev, dev);
  2750. port_netdev->netdev_ops = &dpaa2_switch_port_ops;
  2751. port_netdev->ethtool_ops = &dpaa2_switch_port_ethtool_ops;
  2752. port_netdev->needed_headroom = DPAA2_SWITCH_NEEDED_HEADROOM;
  2753. port_priv->bcast_flood = true;
  2754. port_priv->ucast_flood = true;
  2755. /* Set MTU limits */
  2756. port_netdev->min_mtu = ETH_MIN_MTU;
  2757. port_netdev->max_mtu = ETHSW_MAX_FRAME_LENGTH;
  2758. /* Populate the private port structure so that later calls to
  2759. * dpaa2_switch_port_init() can use it.
  2760. */
  2761. ethsw->ports[port_idx] = port_priv;
  2762. /* The DPAA2 switch's ingress path depends on the VLAN table,
  2763. * thus we are not able to disable VLAN filtering.
  2764. */
  2765. port_netdev->features = NETIF_F_HW_VLAN_CTAG_FILTER |
  2766. NETIF_F_HW_VLAN_STAG_FILTER |
  2767. NETIF_F_HW_TC;
  2768. port_netdev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  2769. err = dpaa2_switch_port_init(port_priv, port_idx);
  2770. if (err)
  2771. goto err_port_probe;
  2772. err = dpaa2_switch_port_set_mac_addr(port_priv);
  2773. if (err)
  2774. goto err_port_probe;
  2775. err = dpaa2_switch_port_set_learning(port_priv, false);
  2776. if (err)
  2777. goto err_port_probe;
  2778. port_priv->learn_ena = false;
  2779. err = dpaa2_switch_port_connect_mac(port_priv);
  2780. if (err)
  2781. goto err_port_probe;
  2782. return 0;
  2783. err_port_probe:
  2784. free_netdev(port_netdev);
  2785. ethsw->ports[port_idx] = NULL;
  2786. return err;
  2787. }
  2788. static int dpaa2_switch_probe(struct fsl_mc_device *sw_dev)
  2789. {
  2790. struct device *dev = &sw_dev->dev;
  2791. struct ethsw_core *ethsw;
  2792. int i, err;
  2793. /* Allocate switch core*/
  2794. ethsw = kzalloc_obj(*ethsw);
  2795. if (!ethsw)
  2796. return -ENOMEM;
  2797. ethsw->dev = dev;
  2798. ethsw->iommu_domain = iommu_get_domain_for_dev(dev);
  2799. dev_set_drvdata(dev, ethsw);
  2800. err = fsl_mc_portal_allocate(sw_dev, FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
  2801. &ethsw->mc_io);
  2802. if (err) {
  2803. if (err == -ENXIO)
  2804. err = -EPROBE_DEFER;
  2805. else
  2806. dev_err(dev, "fsl_mc_portal_allocate err %d\n", err);
  2807. goto err_free_drvdata;
  2808. }
  2809. err = dpaa2_switch_init(sw_dev);
  2810. if (err)
  2811. goto err_free_cmdport;
  2812. ethsw->ports = kzalloc_objs(*ethsw->ports, ethsw->sw_attr.num_ifs);
  2813. if (!(ethsw->ports)) {
  2814. err = -ENOMEM;
  2815. goto err_teardown;
  2816. }
  2817. ethsw->fdbs = kzalloc_objs(*ethsw->fdbs, ethsw->sw_attr.num_ifs);
  2818. if (!ethsw->fdbs) {
  2819. err = -ENOMEM;
  2820. goto err_free_ports;
  2821. }
  2822. ethsw->filter_blocks = kzalloc_objs(*ethsw->filter_blocks,
  2823. ethsw->sw_attr.num_ifs);
  2824. if (!ethsw->filter_blocks) {
  2825. err = -ENOMEM;
  2826. goto err_free_fdbs;
  2827. }
  2828. for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
  2829. err = dpaa2_switch_probe_port(ethsw, i);
  2830. if (err)
  2831. goto err_free_netdev;
  2832. }
  2833. /* Add a NAPI instance for each of the Rx queues. The first port's
  2834. * net_device will be associated with the instances since we do not have
  2835. * different queues for each switch ports.
  2836. */
  2837. for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++)
  2838. netif_napi_add(ethsw->ports[0]->netdev, &ethsw->fq[i].napi,
  2839. dpaa2_switch_poll);
  2840. /* Setup IRQs */
  2841. err = dpaa2_switch_setup_irqs(sw_dev);
  2842. if (err)
  2843. goto err_stop;
  2844. /* By convention, if the mirror port is equal to the number of switch
  2845. * interfaces, then mirroring of any kind is disabled.
  2846. */
  2847. ethsw->mirror_port = ethsw->sw_attr.num_ifs;
  2848. /* Register the netdev only when the entire setup is done and the
  2849. * switch port interfaces are ready to receive traffic
  2850. */
  2851. for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
  2852. err = register_netdev(ethsw->ports[i]->netdev);
  2853. if (err < 0) {
  2854. dev_err(dev, "register_netdev error %d\n", err);
  2855. goto err_unregister_ports;
  2856. }
  2857. }
  2858. return 0;
  2859. err_unregister_ports:
  2860. for (i--; i >= 0; i--)
  2861. unregister_netdev(ethsw->ports[i]->netdev);
  2862. dpaa2_switch_teardown_irqs(sw_dev);
  2863. err_stop:
  2864. dpsw_disable(ethsw->mc_io, 0, ethsw->dpsw_handle);
  2865. err_free_netdev:
  2866. for (i--; i >= 0; i--)
  2867. dpaa2_switch_remove_port(ethsw, i);
  2868. kfree(ethsw->filter_blocks);
  2869. err_free_fdbs:
  2870. kfree(ethsw->fdbs);
  2871. err_free_ports:
  2872. kfree(ethsw->ports);
  2873. err_teardown:
  2874. dpaa2_switch_teardown(sw_dev);
  2875. err_free_cmdport:
  2876. fsl_mc_portal_free(ethsw->mc_io);
  2877. err_free_drvdata:
  2878. kfree(ethsw);
  2879. dev_set_drvdata(dev, NULL);
  2880. return err;
  2881. }
  2882. static const struct fsl_mc_device_id dpaa2_switch_match_id_table[] = {
  2883. {
  2884. .vendor = FSL_MC_VENDOR_FREESCALE,
  2885. .obj_type = "dpsw",
  2886. },
  2887. { .vendor = 0x0 }
  2888. };
  2889. MODULE_DEVICE_TABLE(fslmc, dpaa2_switch_match_id_table);
  2890. static struct fsl_mc_driver dpaa2_switch_drv = {
  2891. .driver = {
  2892. .name = KBUILD_MODNAME,
  2893. },
  2894. .probe = dpaa2_switch_probe,
  2895. .remove = dpaa2_switch_remove,
  2896. .match_id_table = dpaa2_switch_match_id_table
  2897. };
  2898. static struct notifier_block dpaa2_switch_port_nb __read_mostly = {
  2899. .notifier_call = dpaa2_switch_port_netdevice_event,
  2900. };
  2901. static struct notifier_block dpaa2_switch_port_switchdev_nb = {
  2902. .notifier_call = dpaa2_switch_port_event,
  2903. };
  2904. static struct notifier_block dpaa2_switch_port_switchdev_blocking_nb = {
  2905. .notifier_call = dpaa2_switch_port_blocking_event,
  2906. };
  2907. static int dpaa2_switch_register_notifiers(void)
  2908. {
  2909. int err;
  2910. err = register_netdevice_notifier(&dpaa2_switch_port_nb);
  2911. if (err) {
  2912. pr_err("dpaa2-switch: failed to register net_device notifier (%d)\n", err);
  2913. return err;
  2914. }
  2915. err = register_switchdev_notifier(&dpaa2_switch_port_switchdev_nb);
  2916. if (err) {
  2917. pr_err("dpaa2-switch: failed to register switchdev notifier (%d)\n", err);
  2918. goto err_switchdev_nb;
  2919. }
  2920. err = register_switchdev_blocking_notifier(&dpaa2_switch_port_switchdev_blocking_nb);
  2921. if (err) {
  2922. pr_err("dpaa2-switch: failed to register switchdev blocking notifier (%d)\n", err);
  2923. goto err_switchdev_blocking_nb;
  2924. }
  2925. return 0;
  2926. err_switchdev_blocking_nb:
  2927. unregister_switchdev_notifier(&dpaa2_switch_port_switchdev_nb);
  2928. err_switchdev_nb:
  2929. unregister_netdevice_notifier(&dpaa2_switch_port_nb);
  2930. return err;
  2931. }
  2932. static void dpaa2_switch_unregister_notifiers(void)
  2933. {
  2934. int err;
  2935. err = unregister_switchdev_blocking_notifier(&dpaa2_switch_port_switchdev_blocking_nb);
  2936. if (err)
  2937. pr_err("dpaa2-switch: failed to unregister switchdev blocking notifier (%d)\n",
  2938. err);
  2939. err = unregister_switchdev_notifier(&dpaa2_switch_port_switchdev_nb);
  2940. if (err)
  2941. pr_err("dpaa2-switch: failed to unregister switchdev notifier (%d)\n", err);
  2942. err = unregister_netdevice_notifier(&dpaa2_switch_port_nb);
  2943. if (err)
  2944. pr_err("dpaa2-switch: failed to unregister net_device notifier (%d)\n", err);
  2945. }
  2946. static int __init dpaa2_switch_driver_init(void)
  2947. {
  2948. int err;
  2949. err = fsl_mc_driver_register(&dpaa2_switch_drv);
  2950. if (err)
  2951. return err;
  2952. err = dpaa2_switch_register_notifiers();
  2953. if (err) {
  2954. fsl_mc_driver_unregister(&dpaa2_switch_drv);
  2955. return err;
  2956. }
  2957. return 0;
  2958. }
  2959. static void __exit dpaa2_switch_driver_exit(void)
  2960. {
  2961. dpaa2_switch_unregister_notifiers();
  2962. fsl_mc_driver_unregister(&dpaa2_switch_drv);
  2963. }
  2964. module_init(dpaa2_switch_driver_init);
  2965. module_exit(dpaa2_switch_driver_exit);
  2966. MODULE_LICENSE("GPL v2");
  2967. MODULE_DESCRIPTION("DPAA2 Ethernet Switch Driver");