dpaa2-switch.c 88 KB

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