mvpp2.c 118 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257
  1. /*
  2. * Driver for Marvell PPv2 network controller for Armada 375 SoC.
  3. *
  4. * Copyright (C) 2014 Marvell
  5. *
  6. * Marcin Wojtas <mw@semihalf.com>
  7. *
  8. * U-Boot version:
  9. * Copyright (C) 2016 Stefan Roese <sr@denx.de>
  10. *
  11. * This file is licensed under the terms of the GNU General Public
  12. * License version 2. This program is licensed "as is" without any
  13. * warranty of any kind, whether express or implied.
  14. */
  15. #include <common.h>
  16. #include <dm.h>
  17. #include <dm/device-internal.h>
  18. #include <dm/lists.h>
  19. #include <net.h>
  20. #include <netdev.h>
  21. #include <config.h>
  22. #include <malloc.h>
  23. #include <asm/io.h>
  24. #include <linux/errno.h>
  25. #include <phy.h>
  26. #include <miiphy.h>
  27. #include <watchdog.h>
  28. #include <asm/arch/cpu.h>
  29. #include <asm/arch/soc.h>
  30. #include <linux/compat.h>
  31. #include <linux/mbus.h>
  32. DECLARE_GLOBAL_DATA_PTR;
  33. /* Some linux -> U-Boot compatibility stuff */
  34. #define netdev_err(dev, fmt, args...) \
  35. printf(fmt, ##args)
  36. #define netdev_warn(dev, fmt, args...) \
  37. printf(fmt, ##args)
  38. #define netdev_info(dev, fmt, args...) \
  39. printf(fmt, ##args)
  40. #define netdev_dbg(dev, fmt, args...) \
  41. printf(fmt, ##args)
  42. #define ETH_ALEN 6 /* Octets in one ethernet addr */
  43. #define __verify_pcpu_ptr(ptr) \
  44. do { \
  45. const void __percpu *__vpp_verify = (typeof((ptr) + 0))NULL; \
  46. (void)__vpp_verify; \
  47. } while (0)
  48. #define VERIFY_PERCPU_PTR(__p) \
  49. ({ \
  50. __verify_pcpu_ptr(__p); \
  51. (typeof(*(__p)) __kernel __force *)(__p); \
  52. })
  53. #define per_cpu_ptr(ptr, cpu) ({ (void)(cpu); VERIFY_PERCPU_PTR(ptr); })
  54. #define smp_processor_id() 0
  55. #define num_present_cpus() 1
  56. #define for_each_present_cpu(cpu) \
  57. for ((cpu) = 0; (cpu) < 1; (cpu)++)
  58. #define NET_SKB_PAD max(32, MVPP2_CPU_D_CACHE_LINE_SIZE)
  59. #define CONFIG_NR_CPUS 1
  60. #define ETH_HLEN ETHER_HDR_SIZE /* Total octets in header */
  61. /* 2(HW hdr) 14(MAC hdr) 4(CRC) 32(extra for cache prefetch) */
  62. #define WRAP (2 + ETH_HLEN + 4 + 32)
  63. #define MTU 1500
  64. #define RX_BUFFER_SIZE (ALIGN(MTU + WRAP, ARCH_DMA_MINALIGN))
  65. #define MVPP2_SMI_TIMEOUT 10000
  66. /* RX Fifo Registers */
  67. #define MVPP2_RX_DATA_FIFO_SIZE_REG(port) (0x00 + 4 * (port))
  68. #define MVPP2_RX_ATTR_FIFO_SIZE_REG(port) (0x20 + 4 * (port))
  69. #define MVPP2_RX_MIN_PKT_SIZE_REG 0x60
  70. #define MVPP2_RX_FIFO_INIT_REG 0x64
  71. /* RX DMA Top Registers */
  72. #define MVPP2_RX_CTRL_REG(port) (0x140 + 4 * (port))
  73. #define MVPP2_RX_LOW_LATENCY_PKT_SIZE(s) (((s) & 0xfff) << 16)
  74. #define MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK BIT(31)
  75. #define MVPP2_POOL_BUF_SIZE_REG(pool) (0x180 + 4 * (pool))
  76. #define MVPP2_POOL_BUF_SIZE_OFFSET 5
  77. #define MVPP2_RXQ_CONFIG_REG(rxq) (0x800 + 4 * (rxq))
  78. #define MVPP2_SNOOP_PKT_SIZE_MASK 0x1ff
  79. #define MVPP2_SNOOP_BUF_HDR_MASK BIT(9)
  80. #define MVPP2_RXQ_POOL_SHORT_OFFS 20
  81. #define MVPP2_RXQ_POOL_SHORT_MASK 0x700000
  82. #define MVPP2_RXQ_POOL_LONG_OFFS 24
  83. #define MVPP2_RXQ_POOL_LONG_MASK 0x7000000
  84. #define MVPP2_RXQ_PACKET_OFFSET_OFFS 28
  85. #define MVPP2_RXQ_PACKET_OFFSET_MASK 0x70000000
  86. #define MVPP2_RXQ_DISABLE_MASK BIT(31)
  87. /* Parser Registers */
  88. #define MVPP2_PRS_INIT_LOOKUP_REG 0x1000
  89. #define MVPP2_PRS_PORT_LU_MAX 0xf
  90. #define MVPP2_PRS_PORT_LU_MASK(port) (0xff << ((port) * 4))
  91. #define MVPP2_PRS_PORT_LU_VAL(port, val) ((val) << ((port) * 4))
  92. #define MVPP2_PRS_INIT_OFFS_REG(port) (0x1004 + ((port) & 4))
  93. #define MVPP2_PRS_INIT_OFF_MASK(port) (0x3f << (((port) % 4) * 8))
  94. #define MVPP2_PRS_INIT_OFF_VAL(port, val) ((val) << (((port) % 4) * 8))
  95. #define MVPP2_PRS_MAX_LOOP_REG(port) (0x100c + ((port) & 4))
  96. #define MVPP2_PRS_MAX_LOOP_MASK(port) (0xff << (((port) % 4) * 8))
  97. #define MVPP2_PRS_MAX_LOOP_VAL(port, val) ((val) << (((port) % 4) * 8))
  98. #define MVPP2_PRS_TCAM_IDX_REG 0x1100
  99. #define MVPP2_PRS_TCAM_DATA_REG(idx) (0x1104 + (idx) * 4)
  100. #define MVPP2_PRS_TCAM_INV_MASK BIT(31)
  101. #define MVPP2_PRS_SRAM_IDX_REG 0x1200
  102. #define MVPP2_PRS_SRAM_DATA_REG(idx) (0x1204 + (idx) * 4)
  103. #define MVPP2_PRS_TCAM_CTRL_REG 0x1230
  104. #define MVPP2_PRS_TCAM_EN_MASK BIT(0)
  105. /* Classifier Registers */
  106. #define MVPP2_CLS_MODE_REG 0x1800
  107. #define MVPP2_CLS_MODE_ACTIVE_MASK BIT(0)
  108. #define MVPP2_CLS_PORT_WAY_REG 0x1810
  109. #define MVPP2_CLS_PORT_WAY_MASK(port) (1 << (port))
  110. #define MVPP2_CLS_LKP_INDEX_REG 0x1814
  111. #define MVPP2_CLS_LKP_INDEX_WAY_OFFS 6
  112. #define MVPP2_CLS_LKP_TBL_REG 0x1818
  113. #define MVPP2_CLS_LKP_TBL_RXQ_MASK 0xff
  114. #define MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK BIT(25)
  115. #define MVPP2_CLS_FLOW_INDEX_REG 0x1820
  116. #define MVPP2_CLS_FLOW_TBL0_REG 0x1824
  117. #define MVPP2_CLS_FLOW_TBL1_REG 0x1828
  118. #define MVPP2_CLS_FLOW_TBL2_REG 0x182c
  119. #define MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port) (0x1980 + ((port) * 4))
  120. #define MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS 3
  121. #define MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK 0x7
  122. #define MVPP2_CLS_SWFWD_P2HQ_REG(port) (0x19b0 + ((port) * 4))
  123. #define MVPP2_CLS_SWFWD_PCTRL_REG 0x19d0
  124. #define MVPP2_CLS_SWFWD_PCTRL_MASK(port) (1 << (port))
  125. /* Descriptor Manager Top Registers */
  126. #define MVPP2_RXQ_NUM_REG 0x2040
  127. #define MVPP2_RXQ_DESC_ADDR_REG 0x2044
  128. #define MVPP2_RXQ_DESC_SIZE_REG 0x2048
  129. #define MVPP2_RXQ_DESC_SIZE_MASK 0x3ff0
  130. #define MVPP2_RXQ_STATUS_UPDATE_REG(rxq) (0x3000 + 4 * (rxq))
  131. #define MVPP2_RXQ_NUM_PROCESSED_OFFSET 0
  132. #define MVPP2_RXQ_NUM_NEW_OFFSET 16
  133. #define MVPP2_RXQ_STATUS_REG(rxq) (0x3400 + 4 * (rxq))
  134. #define MVPP2_RXQ_OCCUPIED_MASK 0x3fff
  135. #define MVPP2_RXQ_NON_OCCUPIED_OFFSET 16
  136. #define MVPP2_RXQ_NON_OCCUPIED_MASK 0x3fff0000
  137. #define MVPP2_RXQ_THRESH_REG 0x204c
  138. #define MVPP2_OCCUPIED_THRESH_OFFSET 0
  139. #define MVPP2_OCCUPIED_THRESH_MASK 0x3fff
  140. #define MVPP2_RXQ_INDEX_REG 0x2050
  141. #define MVPP2_TXQ_NUM_REG 0x2080
  142. #define MVPP2_TXQ_DESC_ADDR_REG 0x2084
  143. #define MVPP2_TXQ_DESC_SIZE_REG 0x2088
  144. #define MVPP2_TXQ_DESC_SIZE_MASK 0x3ff0
  145. #define MVPP2_AGGR_TXQ_UPDATE_REG 0x2090
  146. #define MVPP2_TXQ_THRESH_REG 0x2094
  147. #define MVPP2_TRANSMITTED_THRESH_OFFSET 16
  148. #define MVPP2_TRANSMITTED_THRESH_MASK 0x3fff0000
  149. #define MVPP2_TXQ_INDEX_REG 0x2098
  150. #define MVPP2_TXQ_PREF_BUF_REG 0x209c
  151. #define MVPP2_PREF_BUF_PTR(desc) ((desc) & 0xfff)
  152. #define MVPP2_PREF_BUF_SIZE_4 (BIT(12) | BIT(13))
  153. #define MVPP2_PREF_BUF_SIZE_16 (BIT(12) | BIT(14))
  154. #define MVPP2_PREF_BUF_THRESH(val) ((val) << 17)
  155. #define MVPP2_TXQ_DRAIN_EN_MASK BIT(31)
  156. #define MVPP2_TXQ_PENDING_REG 0x20a0
  157. #define MVPP2_TXQ_PENDING_MASK 0x3fff
  158. #define MVPP2_TXQ_INT_STATUS_REG 0x20a4
  159. #define MVPP2_TXQ_SENT_REG(txq) (0x3c00 + 4 * (txq))
  160. #define MVPP2_TRANSMITTED_COUNT_OFFSET 16
  161. #define MVPP2_TRANSMITTED_COUNT_MASK 0x3fff0000
  162. #define MVPP2_TXQ_RSVD_REQ_REG 0x20b0
  163. #define MVPP2_TXQ_RSVD_REQ_Q_OFFSET 16
  164. #define MVPP2_TXQ_RSVD_RSLT_REG 0x20b4
  165. #define MVPP2_TXQ_RSVD_RSLT_MASK 0x3fff
  166. #define MVPP2_TXQ_RSVD_CLR_REG 0x20b8
  167. #define MVPP2_TXQ_RSVD_CLR_OFFSET 16
  168. #define MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu) (0x2100 + 4 * (cpu))
  169. #define MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu) (0x2140 + 4 * (cpu))
  170. #define MVPP2_AGGR_TXQ_DESC_SIZE_MASK 0x3ff0
  171. #define MVPP2_AGGR_TXQ_STATUS_REG(cpu) (0x2180 + 4 * (cpu))
  172. #define MVPP2_AGGR_TXQ_PENDING_MASK 0x3fff
  173. #define MVPP2_AGGR_TXQ_INDEX_REG(cpu) (0x21c0 + 4 * (cpu))
  174. /* MBUS bridge registers */
  175. #define MVPP2_WIN_BASE(w) (0x4000 + ((w) << 2))
  176. #define MVPP2_WIN_SIZE(w) (0x4020 + ((w) << 2))
  177. #define MVPP2_WIN_REMAP(w) (0x4040 + ((w) << 2))
  178. #define MVPP2_BASE_ADDR_ENABLE 0x4060
  179. /* Interrupt Cause and Mask registers */
  180. #define MVPP2_ISR_RX_THRESHOLD_REG(rxq) (0x5200 + 4 * (rxq))
  181. #define MVPP2_ISR_RXQ_GROUP_REG(rxq) (0x5400 + 4 * (rxq))
  182. #define MVPP2_ISR_ENABLE_REG(port) (0x5420 + 4 * (port))
  183. #define MVPP2_ISR_ENABLE_INTERRUPT(mask) ((mask) & 0xffff)
  184. #define MVPP2_ISR_DISABLE_INTERRUPT(mask) (((mask) << 16) & 0xffff0000)
  185. #define MVPP2_ISR_RX_TX_CAUSE_REG(port) (0x5480 + 4 * (port))
  186. #define MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK 0xffff
  187. #define MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK 0xff0000
  188. #define MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK BIT(24)
  189. #define MVPP2_CAUSE_FCS_ERR_MASK BIT(25)
  190. #define MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK BIT(26)
  191. #define MVPP2_CAUSE_TX_EXCEPTION_SUM_MASK BIT(29)
  192. #define MVPP2_CAUSE_RX_EXCEPTION_SUM_MASK BIT(30)
  193. #define MVPP2_CAUSE_MISC_SUM_MASK BIT(31)
  194. #define MVPP2_ISR_RX_TX_MASK_REG(port) (0x54a0 + 4 * (port))
  195. #define MVPP2_ISR_PON_RX_TX_MASK_REG 0x54bc
  196. #define MVPP2_PON_CAUSE_RXQ_OCCUP_DESC_ALL_MASK 0xffff
  197. #define MVPP2_PON_CAUSE_TXP_OCCUP_DESC_ALL_MASK 0x3fc00000
  198. #define MVPP2_PON_CAUSE_MISC_SUM_MASK BIT(31)
  199. #define MVPP2_ISR_MISC_CAUSE_REG 0x55b0
  200. /* Buffer Manager registers */
  201. #define MVPP2_BM_POOL_BASE_REG(pool) (0x6000 + ((pool) * 4))
  202. #define MVPP2_BM_POOL_BASE_ADDR_MASK 0xfffff80
  203. #define MVPP2_BM_POOL_SIZE_REG(pool) (0x6040 + ((pool) * 4))
  204. #define MVPP2_BM_POOL_SIZE_MASK 0xfff0
  205. #define MVPP2_BM_POOL_READ_PTR_REG(pool) (0x6080 + ((pool) * 4))
  206. #define MVPP2_BM_POOL_GET_READ_PTR_MASK 0xfff0
  207. #define MVPP2_BM_POOL_PTRS_NUM_REG(pool) (0x60c0 + ((pool) * 4))
  208. #define MVPP2_BM_POOL_PTRS_NUM_MASK 0xfff0
  209. #define MVPP2_BM_BPPI_READ_PTR_REG(pool) (0x6100 + ((pool) * 4))
  210. #define MVPP2_BM_BPPI_PTRS_NUM_REG(pool) (0x6140 + ((pool) * 4))
  211. #define MVPP2_BM_BPPI_PTR_NUM_MASK 0x7ff
  212. #define MVPP2_BM_BPPI_PREFETCH_FULL_MASK BIT(16)
  213. #define MVPP2_BM_POOL_CTRL_REG(pool) (0x6200 + ((pool) * 4))
  214. #define MVPP2_BM_START_MASK BIT(0)
  215. #define MVPP2_BM_STOP_MASK BIT(1)
  216. #define MVPP2_BM_STATE_MASK BIT(4)
  217. #define MVPP2_BM_LOW_THRESH_OFFS 8
  218. #define MVPP2_BM_LOW_THRESH_MASK 0x7f00
  219. #define MVPP2_BM_LOW_THRESH_VALUE(val) ((val) << \
  220. MVPP2_BM_LOW_THRESH_OFFS)
  221. #define MVPP2_BM_HIGH_THRESH_OFFS 16
  222. #define MVPP2_BM_HIGH_THRESH_MASK 0x7f0000
  223. #define MVPP2_BM_HIGH_THRESH_VALUE(val) ((val) << \
  224. MVPP2_BM_HIGH_THRESH_OFFS)
  225. #define MVPP2_BM_INTR_CAUSE_REG(pool) (0x6240 + ((pool) * 4))
  226. #define MVPP2_BM_RELEASED_DELAY_MASK BIT(0)
  227. #define MVPP2_BM_ALLOC_FAILED_MASK BIT(1)
  228. #define MVPP2_BM_BPPE_EMPTY_MASK BIT(2)
  229. #define MVPP2_BM_BPPE_FULL_MASK BIT(3)
  230. #define MVPP2_BM_AVAILABLE_BP_LOW_MASK BIT(4)
  231. #define MVPP2_BM_INTR_MASK_REG(pool) (0x6280 + ((pool) * 4))
  232. #define MVPP2_BM_PHY_ALLOC_REG(pool) (0x6400 + ((pool) * 4))
  233. #define MVPP2_BM_PHY_ALLOC_GRNTD_MASK BIT(0)
  234. #define MVPP2_BM_VIRT_ALLOC_REG 0x6440
  235. #define MVPP2_BM_PHY_RLS_REG(pool) (0x6480 + ((pool) * 4))
  236. #define MVPP2_BM_PHY_RLS_MC_BUFF_MASK BIT(0)
  237. #define MVPP2_BM_PHY_RLS_PRIO_EN_MASK BIT(1)
  238. #define MVPP2_BM_PHY_RLS_GRNTD_MASK BIT(2)
  239. #define MVPP2_BM_VIRT_RLS_REG 0x64c0
  240. #define MVPP2_BM_MC_RLS_REG 0x64c4
  241. #define MVPP2_BM_MC_ID_MASK 0xfff
  242. #define MVPP2_BM_FORCE_RELEASE_MASK BIT(12)
  243. /* TX Scheduler registers */
  244. #define MVPP2_TXP_SCHED_PORT_INDEX_REG 0x8000
  245. #define MVPP2_TXP_SCHED_Q_CMD_REG 0x8004
  246. #define MVPP2_TXP_SCHED_ENQ_MASK 0xff
  247. #define MVPP2_TXP_SCHED_DISQ_OFFSET 8
  248. #define MVPP2_TXP_SCHED_CMD_1_REG 0x8010
  249. #define MVPP2_TXP_SCHED_PERIOD_REG 0x8018
  250. #define MVPP2_TXP_SCHED_MTU_REG 0x801c
  251. #define MVPP2_TXP_MTU_MAX 0x7FFFF
  252. #define MVPP2_TXP_SCHED_REFILL_REG 0x8020
  253. #define MVPP2_TXP_REFILL_TOKENS_ALL_MASK 0x7ffff
  254. #define MVPP2_TXP_REFILL_PERIOD_ALL_MASK 0x3ff00000
  255. #define MVPP2_TXP_REFILL_PERIOD_MASK(v) ((v) << 20)
  256. #define MVPP2_TXP_SCHED_TOKEN_SIZE_REG 0x8024
  257. #define MVPP2_TXP_TOKEN_SIZE_MAX 0xffffffff
  258. #define MVPP2_TXQ_SCHED_REFILL_REG(q) (0x8040 + ((q) << 2))
  259. #define MVPP2_TXQ_REFILL_TOKENS_ALL_MASK 0x7ffff
  260. #define MVPP2_TXQ_REFILL_PERIOD_ALL_MASK 0x3ff00000
  261. #define MVPP2_TXQ_REFILL_PERIOD_MASK(v) ((v) << 20)
  262. #define MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(q) (0x8060 + ((q) << 2))
  263. #define MVPP2_TXQ_TOKEN_SIZE_MAX 0x7fffffff
  264. #define MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(q) (0x8080 + ((q) << 2))
  265. #define MVPP2_TXQ_TOKEN_CNTR_MAX 0xffffffff
  266. /* TX general registers */
  267. #define MVPP2_TX_SNOOP_REG 0x8800
  268. #define MVPP2_TX_PORT_FLUSH_REG 0x8810
  269. #define MVPP2_TX_PORT_FLUSH_MASK(port) (1 << (port))
  270. /* LMS registers */
  271. #define MVPP2_SRC_ADDR_MIDDLE 0x24
  272. #define MVPP2_SRC_ADDR_HIGH 0x28
  273. #define MVPP2_PHY_AN_CFG0_REG 0x34
  274. #define MVPP2_PHY_AN_STOP_SMI0_MASK BIT(7)
  275. #define MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG 0x305c
  276. #define MVPP2_EXT_GLOBAL_CTRL_DEFAULT 0x27
  277. /* Per-port registers */
  278. #define MVPP2_GMAC_CTRL_0_REG 0x0
  279. #define MVPP2_GMAC_PORT_EN_MASK BIT(0)
  280. #define MVPP2_GMAC_MAX_RX_SIZE_OFFS 2
  281. #define MVPP2_GMAC_MAX_RX_SIZE_MASK 0x7ffc
  282. #define MVPP2_GMAC_MIB_CNTR_EN_MASK BIT(15)
  283. #define MVPP2_GMAC_CTRL_1_REG 0x4
  284. #define MVPP2_GMAC_PERIODIC_XON_EN_MASK BIT(1)
  285. #define MVPP2_GMAC_GMII_LB_EN_MASK BIT(5)
  286. #define MVPP2_GMAC_PCS_LB_EN_BIT 6
  287. #define MVPP2_GMAC_PCS_LB_EN_MASK BIT(6)
  288. #define MVPP2_GMAC_SA_LOW_OFFS 7
  289. #define MVPP2_GMAC_CTRL_2_REG 0x8
  290. #define MVPP2_GMAC_INBAND_AN_MASK BIT(0)
  291. #define MVPP2_GMAC_PCS_ENABLE_MASK BIT(3)
  292. #define MVPP2_GMAC_PORT_RGMII_MASK BIT(4)
  293. #define MVPP2_GMAC_PORT_RESET_MASK BIT(6)
  294. #define MVPP2_GMAC_AUTONEG_CONFIG 0xc
  295. #define MVPP2_GMAC_FORCE_LINK_DOWN BIT(0)
  296. #define MVPP2_GMAC_FORCE_LINK_PASS BIT(1)
  297. #define MVPP2_GMAC_CONFIG_MII_SPEED BIT(5)
  298. #define MVPP2_GMAC_CONFIG_GMII_SPEED BIT(6)
  299. #define MVPP2_GMAC_AN_SPEED_EN BIT(7)
  300. #define MVPP2_GMAC_FC_ADV_EN BIT(9)
  301. #define MVPP2_GMAC_CONFIG_FULL_DUPLEX BIT(12)
  302. #define MVPP2_GMAC_AN_DUPLEX_EN BIT(13)
  303. #define MVPP2_GMAC_PORT_FIFO_CFG_1_REG 0x1c
  304. #define MVPP2_GMAC_TX_FIFO_MIN_TH_OFFS 6
  305. #define MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK 0x1fc0
  306. #define MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(v) (((v) << 6) & \
  307. MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK)
  308. #define MVPP2_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff
  309. /* Descriptor ring Macros */
  310. #define MVPP2_QUEUE_NEXT_DESC(q, index) \
  311. (((index) < (q)->last_desc) ? ((index) + 1) : 0)
  312. /* SMI: 0xc0054 -> offset 0x54 to lms_base */
  313. #define MVPP2_SMI 0x0054
  314. #define MVPP2_PHY_REG_MASK 0x1f
  315. /* SMI register fields */
  316. #define MVPP2_SMI_DATA_OFFS 0 /* Data */
  317. #define MVPP2_SMI_DATA_MASK (0xffff << MVPP2_SMI_DATA_OFFS)
  318. #define MVPP2_SMI_DEV_ADDR_OFFS 16 /* PHY device address */
  319. #define MVPP2_SMI_REG_ADDR_OFFS 21 /* PHY device reg addr*/
  320. #define MVPP2_SMI_OPCODE_OFFS 26 /* Write/Read opcode */
  321. #define MVPP2_SMI_OPCODE_READ (1 << MVPP2_SMI_OPCODE_OFFS)
  322. #define MVPP2_SMI_READ_VALID (1 << 27) /* Read Valid */
  323. #define MVPP2_SMI_BUSY (1 << 28) /* Busy */
  324. #define MVPP2_PHY_ADDR_MASK 0x1f
  325. #define MVPP2_PHY_REG_MASK 0x1f
  326. /* Various constants */
  327. /* Coalescing */
  328. #define MVPP2_TXDONE_COAL_PKTS_THRESH 15
  329. #define MVPP2_TXDONE_HRTIMER_PERIOD_NS 1000000UL
  330. #define MVPP2_RX_COAL_PKTS 32
  331. #define MVPP2_RX_COAL_USEC 100
  332. /* The two bytes Marvell header. Either contains a special value used
  333. * by Marvell switches when a specific hardware mode is enabled (not
  334. * supported by this driver) or is filled automatically by zeroes on
  335. * the RX side. Those two bytes being at the front of the Ethernet
  336. * header, they allow to have the IP header aligned on a 4 bytes
  337. * boundary automatically: the hardware skips those two bytes on its
  338. * own.
  339. */
  340. #define MVPP2_MH_SIZE 2
  341. #define MVPP2_ETH_TYPE_LEN 2
  342. #define MVPP2_PPPOE_HDR_SIZE 8
  343. #define MVPP2_VLAN_TAG_LEN 4
  344. /* Lbtd 802.3 type */
  345. #define MVPP2_IP_LBDT_TYPE 0xfffa
  346. #define MVPP2_CPU_D_CACHE_LINE_SIZE 32
  347. #define MVPP2_TX_CSUM_MAX_SIZE 9800
  348. /* Timeout constants */
  349. #define MVPP2_TX_DISABLE_TIMEOUT_MSEC 1000
  350. #define MVPP2_TX_PENDING_TIMEOUT_MSEC 1000
  351. #define MVPP2_TX_MTU_MAX 0x7ffff
  352. /* Maximum number of T-CONTs of PON port */
  353. #define MVPP2_MAX_TCONT 16
  354. /* Maximum number of supported ports */
  355. #define MVPP2_MAX_PORTS 4
  356. /* Maximum number of TXQs used by single port */
  357. #define MVPP2_MAX_TXQ 8
  358. /* Maximum number of RXQs used by single port */
  359. #define MVPP2_MAX_RXQ 8
  360. /* Default number of TXQs in use */
  361. #define MVPP2_DEFAULT_TXQ 1
  362. /* Dfault number of RXQs in use */
  363. #define MVPP2_DEFAULT_RXQ 1
  364. #define CONFIG_MV_ETH_RXQ 8 /* increment by 8 */
  365. /* Total number of RXQs available to all ports */
  366. #define MVPP2_RXQ_TOTAL_NUM (MVPP2_MAX_PORTS * MVPP2_MAX_RXQ)
  367. /* Max number of Rx descriptors */
  368. #define MVPP2_MAX_RXD 16
  369. /* Max number of Tx descriptors */
  370. #define MVPP2_MAX_TXD 16
  371. /* Amount of Tx descriptors that can be reserved at once by CPU */
  372. #define MVPP2_CPU_DESC_CHUNK 64
  373. /* Max number of Tx descriptors in each aggregated queue */
  374. #define MVPP2_AGGR_TXQ_SIZE 256
  375. /* Descriptor aligned size */
  376. #define MVPP2_DESC_ALIGNED_SIZE 32
  377. /* Descriptor alignment mask */
  378. #define MVPP2_TX_DESC_ALIGN (MVPP2_DESC_ALIGNED_SIZE - 1)
  379. /* RX FIFO constants */
  380. #define MVPP2_RX_FIFO_PORT_DATA_SIZE 0x2000
  381. #define MVPP2_RX_FIFO_PORT_ATTR_SIZE 0x80
  382. #define MVPP2_RX_FIFO_PORT_MIN_PKT 0x80
  383. /* RX buffer constants */
  384. #define MVPP2_SKB_SHINFO_SIZE \
  385. 0
  386. #define MVPP2_RX_PKT_SIZE(mtu) \
  387. ALIGN((mtu) + MVPP2_MH_SIZE + MVPP2_VLAN_TAG_LEN + \
  388. ETH_HLEN + ETH_FCS_LEN, MVPP2_CPU_D_CACHE_LINE_SIZE)
  389. #define MVPP2_RX_BUF_SIZE(pkt_size) ((pkt_size) + NET_SKB_PAD)
  390. #define MVPP2_RX_TOTAL_SIZE(buf_size) ((buf_size) + MVPP2_SKB_SHINFO_SIZE)
  391. #define MVPP2_RX_MAX_PKT_SIZE(total_size) \
  392. ((total_size) - NET_SKB_PAD - MVPP2_SKB_SHINFO_SIZE)
  393. #define MVPP2_BIT_TO_BYTE(bit) ((bit) / 8)
  394. /* IPv6 max L3 address size */
  395. #define MVPP2_MAX_L3_ADDR_SIZE 16
  396. /* Port flags */
  397. #define MVPP2_F_LOOPBACK BIT(0)
  398. /* Marvell tag types */
  399. enum mvpp2_tag_type {
  400. MVPP2_TAG_TYPE_NONE = 0,
  401. MVPP2_TAG_TYPE_MH = 1,
  402. MVPP2_TAG_TYPE_DSA = 2,
  403. MVPP2_TAG_TYPE_EDSA = 3,
  404. MVPP2_TAG_TYPE_VLAN = 4,
  405. MVPP2_TAG_TYPE_LAST = 5
  406. };
  407. /* Parser constants */
  408. #define MVPP2_PRS_TCAM_SRAM_SIZE 256
  409. #define MVPP2_PRS_TCAM_WORDS 6
  410. #define MVPP2_PRS_SRAM_WORDS 4
  411. #define MVPP2_PRS_FLOW_ID_SIZE 64
  412. #define MVPP2_PRS_FLOW_ID_MASK 0x3f
  413. #define MVPP2_PRS_TCAM_ENTRY_INVALID 1
  414. #define MVPP2_PRS_TCAM_DSA_TAGGED_BIT BIT(5)
  415. #define MVPP2_PRS_IPV4_HEAD 0x40
  416. #define MVPP2_PRS_IPV4_HEAD_MASK 0xf0
  417. #define MVPP2_PRS_IPV4_MC 0xe0
  418. #define MVPP2_PRS_IPV4_MC_MASK 0xf0
  419. #define MVPP2_PRS_IPV4_BC_MASK 0xff
  420. #define MVPP2_PRS_IPV4_IHL 0x5
  421. #define MVPP2_PRS_IPV4_IHL_MASK 0xf
  422. #define MVPP2_PRS_IPV6_MC 0xff
  423. #define MVPP2_PRS_IPV6_MC_MASK 0xff
  424. #define MVPP2_PRS_IPV6_HOP_MASK 0xff
  425. #define MVPP2_PRS_TCAM_PROTO_MASK 0xff
  426. #define MVPP2_PRS_TCAM_PROTO_MASK_L 0x3f
  427. #define MVPP2_PRS_DBL_VLANS_MAX 100
  428. /* Tcam structure:
  429. * - lookup ID - 4 bits
  430. * - port ID - 1 byte
  431. * - additional information - 1 byte
  432. * - header data - 8 bytes
  433. * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(5)->(0).
  434. */
  435. #define MVPP2_PRS_AI_BITS 8
  436. #define MVPP2_PRS_PORT_MASK 0xff
  437. #define MVPP2_PRS_LU_MASK 0xf
  438. #define MVPP2_PRS_TCAM_DATA_BYTE(offs) \
  439. (((offs) - ((offs) % 2)) * 2 + ((offs) % 2))
  440. #define MVPP2_PRS_TCAM_DATA_BYTE_EN(offs) \
  441. (((offs) * 2) - ((offs) % 2) + 2)
  442. #define MVPP2_PRS_TCAM_AI_BYTE 16
  443. #define MVPP2_PRS_TCAM_PORT_BYTE 17
  444. #define MVPP2_PRS_TCAM_LU_BYTE 20
  445. #define MVPP2_PRS_TCAM_EN_OFFS(offs) ((offs) + 2)
  446. #define MVPP2_PRS_TCAM_INV_WORD 5
  447. /* Tcam entries ID */
  448. #define MVPP2_PE_DROP_ALL 0
  449. #define MVPP2_PE_FIRST_FREE_TID 1
  450. #define MVPP2_PE_LAST_FREE_TID (MVPP2_PRS_TCAM_SRAM_SIZE - 31)
  451. #define MVPP2_PE_IP6_EXT_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 30)
  452. #define MVPP2_PE_MAC_MC_IP6 (MVPP2_PRS_TCAM_SRAM_SIZE - 29)
  453. #define MVPP2_PE_IP6_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 28)
  454. #define MVPP2_PE_IP4_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 27)
  455. #define MVPP2_PE_LAST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 26)
  456. #define MVPP2_PE_FIRST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 19)
  457. #define MVPP2_PE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 18)
  458. #define MVPP2_PE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 17)
  459. #define MVPP2_PE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 16)
  460. #define MVPP2_PE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 15)
  461. #define MVPP2_PE_ETYPE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 14)
  462. #define MVPP2_PE_ETYPE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 13)
  463. #define MVPP2_PE_ETYPE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 12)
  464. #define MVPP2_PE_ETYPE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 11)
  465. #define MVPP2_PE_MH_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 10)
  466. #define MVPP2_PE_DSA_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 9)
  467. #define MVPP2_PE_IP6_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 8)
  468. #define MVPP2_PE_IP4_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 7)
  469. #define MVPP2_PE_ETH_TYPE_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 6)
  470. #define MVPP2_PE_VLAN_DBL (MVPP2_PRS_TCAM_SRAM_SIZE - 5)
  471. #define MVPP2_PE_VLAN_NONE (MVPP2_PRS_TCAM_SRAM_SIZE - 4)
  472. #define MVPP2_PE_MAC_MC_ALL (MVPP2_PRS_TCAM_SRAM_SIZE - 3)
  473. #define MVPP2_PE_MAC_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 2)
  474. #define MVPP2_PE_MAC_NON_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 1)
  475. /* Sram structure
  476. * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(3)->(0).
  477. */
  478. #define MVPP2_PRS_SRAM_RI_OFFS 0
  479. #define MVPP2_PRS_SRAM_RI_WORD 0
  480. #define MVPP2_PRS_SRAM_RI_CTRL_OFFS 32
  481. #define MVPP2_PRS_SRAM_RI_CTRL_WORD 1
  482. #define MVPP2_PRS_SRAM_RI_CTRL_BITS 32
  483. #define MVPP2_PRS_SRAM_SHIFT_OFFS 64
  484. #define MVPP2_PRS_SRAM_SHIFT_SIGN_BIT 72
  485. #define MVPP2_PRS_SRAM_UDF_OFFS 73
  486. #define MVPP2_PRS_SRAM_UDF_BITS 8
  487. #define MVPP2_PRS_SRAM_UDF_MASK 0xff
  488. #define MVPP2_PRS_SRAM_UDF_SIGN_BIT 81
  489. #define MVPP2_PRS_SRAM_UDF_TYPE_OFFS 82
  490. #define MVPP2_PRS_SRAM_UDF_TYPE_MASK 0x7
  491. #define MVPP2_PRS_SRAM_UDF_TYPE_L3 1
  492. #define MVPP2_PRS_SRAM_UDF_TYPE_L4 4
  493. #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS 85
  494. #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK 0x3
  495. #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD 1
  496. #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP4_ADD 2
  497. #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP6_ADD 3
  498. #define MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS 87
  499. #define MVPP2_PRS_SRAM_OP_SEL_UDF_BITS 2
  500. #define MVPP2_PRS_SRAM_OP_SEL_UDF_MASK 0x3
  501. #define MVPP2_PRS_SRAM_OP_SEL_UDF_ADD 0
  502. #define MVPP2_PRS_SRAM_OP_SEL_UDF_IP4_ADD 2
  503. #define MVPP2_PRS_SRAM_OP_SEL_UDF_IP6_ADD 3
  504. #define MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS 89
  505. #define MVPP2_PRS_SRAM_AI_OFFS 90
  506. #define MVPP2_PRS_SRAM_AI_CTRL_OFFS 98
  507. #define MVPP2_PRS_SRAM_AI_CTRL_BITS 8
  508. #define MVPP2_PRS_SRAM_AI_MASK 0xff
  509. #define MVPP2_PRS_SRAM_NEXT_LU_OFFS 106
  510. #define MVPP2_PRS_SRAM_NEXT_LU_MASK 0xf
  511. #define MVPP2_PRS_SRAM_LU_DONE_BIT 110
  512. #define MVPP2_PRS_SRAM_LU_GEN_BIT 111
  513. /* Sram result info bits assignment */
  514. #define MVPP2_PRS_RI_MAC_ME_MASK 0x1
  515. #define MVPP2_PRS_RI_DSA_MASK 0x2
  516. #define MVPP2_PRS_RI_VLAN_MASK (BIT(2) | BIT(3))
  517. #define MVPP2_PRS_RI_VLAN_NONE 0x0
  518. #define MVPP2_PRS_RI_VLAN_SINGLE BIT(2)
  519. #define MVPP2_PRS_RI_VLAN_DOUBLE BIT(3)
  520. #define MVPP2_PRS_RI_VLAN_TRIPLE (BIT(2) | BIT(3))
  521. #define MVPP2_PRS_RI_CPU_CODE_MASK 0x70
  522. #define MVPP2_PRS_RI_CPU_CODE_RX_SPEC BIT(4)
  523. #define MVPP2_PRS_RI_L2_CAST_MASK (BIT(9) | BIT(10))
  524. #define MVPP2_PRS_RI_L2_UCAST 0x0
  525. #define MVPP2_PRS_RI_L2_MCAST BIT(9)
  526. #define MVPP2_PRS_RI_L2_BCAST BIT(10)
  527. #define MVPP2_PRS_RI_PPPOE_MASK 0x800
  528. #define MVPP2_PRS_RI_L3_PROTO_MASK (BIT(12) | BIT(13) | BIT(14))
  529. #define MVPP2_PRS_RI_L3_UN 0x0
  530. #define MVPP2_PRS_RI_L3_IP4 BIT(12)
  531. #define MVPP2_PRS_RI_L3_IP4_OPT BIT(13)
  532. #define MVPP2_PRS_RI_L3_IP4_OTHER (BIT(12) | BIT(13))
  533. #define MVPP2_PRS_RI_L3_IP6 BIT(14)
  534. #define MVPP2_PRS_RI_L3_IP6_EXT (BIT(12) | BIT(14))
  535. #define MVPP2_PRS_RI_L3_ARP (BIT(13) | BIT(14))
  536. #define MVPP2_PRS_RI_L3_ADDR_MASK (BIT(15) | BIT(16))
  537. #define MVPP2_PRS_RI_L3_UCAST 0x0
  538. #define MVPP2_PRS_RI_L3_MCAST BIT(15)
  539. #define MVPP2_PRS_RI_L3_BCAST (BIT(15) | BIT(16))
  540. #define MVPP2_PRS_RI_IP_FRAG_MASK 0x20000
  541. #define MVPP2_PRS_RI_UDF3_MASK 0x300000
  542. #define MVPP2_PRS_RI_UDF3_RX_SPECIAL BIT(21)
  543. #define MVPP2_PRS_RI_L4_PROTO_MASK 0x1c00000
  544. #define MVPP2_PRS_RI_L4_TCP BIT(22)
  545. #define MVPP2_PRS_RI_L4_UDP BIT(23)
  546. #define MVPP2_PRS_RI_L4_OTHER (BIT(22) | BIT(23))
  547. #define MVPP2_PRS_RI_UDF7_MASK 0x60000000
  548. #define MVPP2_PRS_RI_UDF7_IP6_LITE BIT(29)
  549. #define MVPP2_PRS_RI_DROP_MASK 0x80000000
  550. /* Sram additional info bits assignment */
  551. #define MVPP2_PRS_IPV4_DIP_AI_BIT BIT(0)
  552. #define MVPP2_PRS_IPV6_NO_EXT_AI_BIT BIT(0)
  553. #define MVPP2_PRS_IPV6_EXT_AI_BIT BIT(1)
  554. #define MVPP2_PRS_IPV6_EXT_AH_AI_BIT BIT(2)
  555. #define MVPP2_PRS_IPV6_EXT_AH_LEN_AI_BIT BIT(3)
  556. #define MVPP2_PRS_IPV6_EXT_AH_L4_AI_BIT BIT(4)
  557. #define MVPP2_PRS_SINGLE_VLAN_AI 0
  558. #define MVPP2_PRS_DBL_VLAN_AI_BIT BIT(7)
  559. /* DSA/EDSA type */
  560. #define MVPP2_PRS_TAGGED true
  561. #define MVPP2_PRS_UNTAGGED false
  562. #define MVPP2_PRS_EDSA true
  563. #define MVPP2_PRS_DSA false
  564. /* MAC entries, shadow udf */
  565. enum mvpp2_prs_udf {
  566. MVPP2_PRS_UDF_MAC_DEF,
  567. MVPP2_PRS_UDF_MAC_RANGE,
  568. MVPP2_PRS_UDF_L2_DEF,
  569. MVPP2_PRS_UDF_L2_DEF_COPY,
  570. MVPP2_PRS_UDF_L2_USER,
  571. };
  572. /* Lookup ID */
  573. enum mvpp2_prs_lookup {
  574. MVPP2_PRS_LU_MH,
  575. MVPP2_PRS_LU_MAC,
  576. MVPP2_PRS_LU_DSA,
  577. MVPP2_PRS_LU_VLAN,
  578. MVPP2_PRS_LU_L2,
  579. MVPP2_PRS_LU_PPPOE,
  580. MVPP2_PRS_LU_IP4,
  581. MVPP2_PRS_LU_IP6,
  582. MVPP2_PRS_LU_FLOWS,
  583. MVPP2_PRS_LU_LAST,
  584. };
  585. /* L3 cast enum */
  586. enum mvpp2_prs_l3_cast {
  587. MVPP2_PRS_L3_UNI_CAST,
  588. MVPP2_PRS_L3_MULTI_CAST,
  589. MVPP2_PRS_L3_BROAD_CAST
  590. };
  591. /* Classifier constants */
  592. #define MVPP2_CLS_FLOWS_TBL_SIZE 512
  593. #define MVPP2_CLS_FLOWS_TBL_DATA_WORDS 3
  594. #define MVPP2_CLS_LKP_TBL_SIZE 64
  595. /* BM constants */
  596. #define MVPP2_BM_POOLS_NUM 1
  597. #define MVPP2_BM_LONG_BUF_NUM 16
  598. #define MVPP2_BM_SHORT_BUF_NUM 16
  599. #define MVPP2_BM_POOL_SIZE_MAX (16*1024 - MVPP2_BM_POOL_PTR_ALIGN/4)
  600. #define MVPP2_BM_POOL_PTR_ALIGN 128
  601. #define MVPP2_BM_SWF_LONG_POOL(port) 0
  602. /* BM cookie (32 bits) definition */
  603. #define MVPP2_BM_COOKIE_POOL_OFFS 8
  604. #define MVPP2_BM_COOKIE_CPU_OFFS 24
  605. /* BM short pool packet size
  606. * These value assure that for SWF the total number
  607. * of bytes allocated for each buffer will be 512
  608. */
  609. #define MVPP2_BM_SHORT_PKT_SIZE MVPP2_RX_MAX_PKT_SIZE(512)
  610. enum mvpp2_bm_type {
  611. MVPP2_BM_FREE,
  612. MVPP2_BM_SWF_LONG,
  613. MVPP2_BM_SWF_SHORT
  614. };
  615. /* Definitions */
  616. /* Shared Packet Processor resources */
  617. struct mvpp2 {
  618. /* Shared registers' base addresses */
  619. void __iomem *base;
  620. void __iomem *lms_base;
  621. /* List of pointers to port structures */
  622. struct mvpp2_port **port_list;
  623. /* Aggregated TXQs */
  624. struct mvpp2_tx_queue *aggr_txqs;
  625. /* BM pools */
  626. struct mvpp2_bm_pool *bm_pools;
  627. /* PRS shadow table */
  628. struct mvpp2_prs_shadow *prs_shadow;
  629. /* PRS auxiliary table for double vlan entries control */
  630. bool *prs_double_vlans;
  631. /* Tclk value */
  632. u32 tclk;
  633. /* HW version */
  634. enum { MVPP21, MVPP22 } hw_version;
  635. struct mii_dev *bus;
  636. };
  637. struct mvpp2_pcpu_stats {
  638. u64 rx_packets;
  639. u64 rx_bytes;
  640. u64 tx_packets;
  641. u64 tx_bytes;
  642. };
  643. struct mvpp2_port {
  644. u8 id;
  645. int irq;
  646. struct mvpp2 *priv;
  647. /* Per-port registers' base address */
  648. void __iomem *base;
  649. struct mvpp2_rx_queue **rxqs;
  650. struct mvpp2_tx_queue **txqs;
  651. int pkt_size;
  652. u32 pending_cause_rx;
  653. /* Per-CPU port control */
  654. struct mvpp2_port_pcpu __percpu *pcpu;
  655. /* Flags */
  656. unsigned long flags;
  657. u16 tx_ring_size;
  658. u16 rx_ring_size;
  659. struct mvpp2_pcpu_stats __percpu *stats;
  660. struct phy_device *phy_dev;
  661. phy_interface_t phy_interface;
  662. int phy_node;
  663. int phyaddr;
  664. int init;
  665. unsigned int link;
  666. unsigned int duplex;
  667. unsigned int speed;
  668. struct mvpp2_bm_pool *pool_long;
  669. struct mvpp2_bm_pool *pool_short;
  670. /* Index of first port's physical RXQ */
  671. u8 first_rxq;
  672. u8 dev_addr[ETH_ALEN];
  673. };
  674. /* The mvpp2_tx_desc and mvpp2_rx_desc structures describe the
  675. * layout of the transmit and reception DMA descriptors, and their
  676. * layout is therefore defined by the hardware design
  677. */
  678. #define MVPP2_TXD_L3_OFF_SHIFT 0
  679. #define MVPP2_TXD_IP_HLEN_SHIFT 8
  680. #define MVPP2_TXD_L4_CSUM_FRAG BIT(13)
  681. #define MVPP2_TXD_L4_CSUM_NOT BIT(14)
  682. #define MVPP2_TXD_IP_CSUM_DISABLE BIT(15)
  683. #define MVPP2_TXD_PADDING_DISABLE BIT(23)
  684. #define MVPP2_TXD_L4_UDP BIT(24)
  685. #define MVPP2_TXD_L3_IP6 BIT(26)
  686. #define MVPP2_TXD_L_DESC BIT(28)
  687. #define MVPP2_TXD_F_DESC BIT(29)
  688. #define MVPP2_RXD_ERR_SUMMARY BIT(15)
  689. #define MVPP2_RXD_ERR_CODE_MASK (BIT(13) | BIT(14))
  690. #define MVPP2_RXD_ERR_CRC 0x0
  691. #define MVPP2_RXD_ERR_OVERRUN BIT(13)
  692. #define MVPP2_RXD_ERR_RESOURCE (BIT(13) | BIT(14))
  693. #define MVPP2_RXD_BM_POOL_ID_OFFS 16
  694. #define MVPP2_RXD_BM_POOL_ID_MASK (BIT(16) | BIT(17) | BIT(18))
  695. #define MVPP2_RXD_HWF_SYNC BIT(21)
  696. #define MVPP2_RXD_L4_CSUM_OK BIT(22)
  697. #define MVPP2_RXD_IP4_HEADER_ERR BIT(24)
  698. #define MVPP2_RXD_L4_TCP BIT(25)
  699. #define MVPP2_RXD_L4_UDP BIT(26)
  700. #define MVPP2_RXD_L3_IP4 BIT(28)
  701. #define MVPP2_RXD_L3_IP6 BIT(30)
  702. #define MVPP2_RXD_BUF_HDR BIT(31)
  703. struct mvpp2_tx_desc {
  704. u32 command; /* Options used by HW for packet transmitting.*/
  705. u8 packet_offset; /* the offset from the buffer beginning */
  706. u8 phys_txq; /* destination queue ID */
  707. u16 data_size; /* data size of transmitted packet in bytes */
  708. u32 buf_dma_addr; /* physical addr of transmitted buffer */
  709. u32 buf_cookie; /* cookie for access to TX buffer in tx path */
  710. u32 reserved1[3]; /* hw_cmd (for future use, BM, PON, PNC) */
  711. u32 reserved2; /* reserved (for future use) */
  712. };
  713. struct mvpp2_rx_desc {
  714. u32 status; /* info about received packet */
  715. u16 reserved1; /* parser_info (for future use, PnC) */
  716. u16 data_size; /* size of received packet in bytes */
  717. u32 buf_dma_addr; /* physical address of the buffer */
  718. u32 buf_cookie; /* cookie for access to RX buffer in rx path */
  719. u16 reserved2; /* gem_port_id (for future use, PON) */
  720. u16 reserved3; /* csum_l4 (for future use, PnC) */
  721. u8 reserved4; /* bm_qset (for future use, BM) */
  722. u8 reserved5;
  723. u16 reserved6; /* classify_info (for future use, PnC) */
  724. u32 reserved7; /* flow_id (for future use, PnC) */
  725. u32 reserved8;
  726. };
  727. /* Per-CPU Tx queue control */
  728. struct mvpp2_txq_pcpu {
  729. int cpu;
  730. /* Number of Tx DMA descriptors in the descriptor ring */
  731. int size;
  732. /* Number of currently used Tx DMA descriptor in the
  733. * descriptor ring
  734. */
  735. int count;
  736. /* Number of Tx DMA descriptors reserved for each CPU */
  737. int reserved_num;
  738. /* Index of last TX DMA descriptor that was inserted */
  739. int txq_put_index;
  740. /* Index of the TX DMA descriptor to be cleaned up */
  741. int txq_get_index;
  742. };
  743. struct mvpp2_tx_queue {
  744. /* Physical number of this Tx queue */
  745. u8 id;
  746. /* Logical number of this Tx queue */
  747. u8 log_id;
  748. /* Number of Tx DMA descriptors in the descriptor ring */
  749. int size;
  750. /* Number of currently used Tx DMA descriptor in the descriptor ring */
  751. int count;
  752. /* Per-CPU control of physical Tx queues */
  753. struct mvpp2_txq_pcpu __percpu *pcpu;
  754. u32 done_pkts_coal;
  755. /* Virtual address of thex Tx DMA descriptors array */
  756. struct mvpp2_tx_desc *descs;
  757. /* DMA address of the Tx DMA descriptors array */
  758. dma_addr_t descs_dma;
  759. /* Index of the last Tx DMA descriptor */
  760. int last_desc;
  761. /* Index of the next Tx DMA descriptor to process */
  762. int next_desc_to_proc;
  763. };
  764. struct mvpp2_rx_queue {
  765. /* RX queue number, in the range 0-31 for physical RXQs */
  766. u8 id;
  767. /* Num of rx descriptors in the rx descriptor ring */
  768. int size;
  769. u32 pkts_coal;
  770. u32 time_coal;
  771. /* Virtual address of the RX DMA descriptors array */
  772. struct mvpp2_rx_desc *descs;
  773. /* DMA address of the RX DMA descriptors array */
  774. dma_addr_t descs_dma;
  775. /* Index of the last RX DMA descriptor */
  776. int last_desc;
  777. /* Index of the next RX DMA descriptor to process */
  778. int next_desc_to_proc;
  779. /* ID of port to which physical RXQ is mapped */
  780. int port;
  781. /* Port's logic RXQ number to which physical RXQ is mapped */
  782. int logic_rxq;
  783. };
  784. union mvpp2_prs_tcam_entry {
  785. u32 word[MVPP2_PRS_TCAM_WORDS];
  786. u8 byte[MVPP2_PRS_TCAM_WORDS * 4];
  787. };
  788. union mvpp2_prs_sram_entry {
  789. u32 word[MVPP2_PRS_SRAM_WORDS];
  790. u8 byte[MVPP2_PRS_SRAM_WORDS * 4];
  791. };
  792. struct mvpp2_prs_entry {
  793. u32 index;
  794. union mvpp2_prs_tcam_entry tcam;
  795. union mvpp2_prs_sram_entry sram;
  796. };
  797. struct mvpp2_prs_shadow {
  798. bool valid;
  799. bool finish;
  800. /* Lookup ID */
  801. int lu;
  802. /* User defined offset */
  803. int udf;
  804. /* Result info */
  805. u32 ri;
  806. u32 ri_mask;
  807. };
  808. struct mvpp2_cls_flow_entry {
  809. u32 index;
  810. u32 data[MVPP2_CLS_FLOWS_TBL_DATA_WORDS];
  811. };
  812. struct mvpp2_cls_lookup_entry {
  813. u32 lkpid;
  814. u32 way;
  815. u32 data;
  816. };
  817. struct mvpp2_bm_pool {
  818. /* Pool number in the range 0-7 */
  819. int id;
  820. enum mvpp2_bm_type type;
  821. /* Buffer Pointers Pool External (BPPE) size */
  822. int size;
  823. /* Number of buffers for this pool */
  824. int buf_num;
  825. /* Pool buffer size */
  826. int buf_size;
  827. /* Packet size */
  828. int pkt_size;
  829. /* BPPE virtual base address */
  830. unsigned long *virt_addr;
  831. /* BPPE DMA base address */
  832. dma_addr_t dma_addr;
  833. /* Ports using BM pool */
  834. u32 port_map;
  835. /* Occupied buffers indicator */
  836. int in_use_thresh;
  837. };
  838. /* Static declaractions */
  839. /* Number of RXQs used by single port */
  840. static int rxq_number = MVPP2_DEFAULT_RXQ;
  841. /* Number of TXQs used by single port */
  842. static int txq_number = MVPP2_DEFAULT_TXQ;
  843. #define MVPP2_DRIVER_NAME "mvpp2"
  844. #define MVPP2_DRIVER_VERSION "1.0"
  845. /*
  846. * U-Boot internal data, mostly uncached buffers for descriptors and data
  847. */
  848. struct buffer_location {
  849. struct mvpp2_tx_desc *aggr_tx_descs;
  850. struct mvpp2_tx_desc *tx_descs;
  851. struct mvpp2_rx_desc *rx_descs;
  852. unsigned long *bm_pool[MVPP2_BM_POOLS_NUM];
  853. unsigned long *rx_buffer[MVPP2_BM_LONG_BUF_NUM];
  854. int first_rxq;
  855. };
  856. /*
  857. * All 4 interfaces use the same global buffer, since only one interface
  858. * can be enabled at once
  859. */
  860. static struct buffer_location buffer_loc;
  861. /*
  862. * Page table entries are set to 1MB, or multiples of 1MB
  863. * (not < 1MB). driver uses less bd's so use 1MB bdspace.
  864. */
  865. #define BD_SPACE (1 << 20)
  866. /* Utility/helper methods */
  867. static void mvpp2_write(struct mvpp2 *priv, u32 offset, u32 data)
  868. {
  869. writel(data, priv->base + offset);
  870. }
  871. static u32 mvpp2_read(struct mvpp2 *priv, u32 offset)
  872. {
  873. return readl(priv->base + offset);
  874. }
  875. static void mvpp2_txdesc_dma_addr_set(struct mvpp2_port *port,
  876. struct mvpp2_tx_desc *tx_desc,
  877. dma_addr_t dma_addr)
  878. {
  879. tx_desc->buf_dma_addr = dma_addr;
  880. }
  881. static void mvpp2_txdesc_size_set(struct mvpp2_port *port,
  882. struct mvpp2_tx_desc *tx_desc,
  883. size_t size)
  884. {
  885. tx_desc->data_size = size;
  886. }
  887. static void mvpp2_txdesc_txq_set(struct mvpp2_port *port,
  888. struct mvpp2_tx_desc *tx_desc,
  889. unsigned int txq)
  890. {
  891. tx_desc->phys_txq = txq;
  892. }
  893. static void mvpp2_txdesc_cmd_set(struct mvpp2_port *port,
  894. struct mvpp2_tx_desc *tx_desc,
  895. unsigned int command)
  896. {
  897. tx_desc->command = command;
  898. }
  899. static void mvpp2_txdesc_offset_set(struct mvpp2_port *port,
  900. struct mvpp2_tx_desc *tx_desc,
  901. unsigned int offset)
  902. {
  903. tx_desc->packet_offset = offset;
  904. }
  905. static dma_addr_t mvpp2_rxdesc_dma_addr_get(struct mvpp2_port *port,
  906. struct mvpp2_rx_desc *rx_desc)
  907. {
  908. return rx_desc->buf_dma_addr;
  909. }
  910. static unsigned long mvpp2_rxdesc_cookie_get(struct mvpp2_port *port,
  911. struct mvpp2_rx_desc *rx_desc)
  912. {
  913. return rx_desc->buf_cookie;
  914. }
  915. static size_t mvpp2_rxdesc_size_get(struct mvpp2_port *port,
  916. struct mvpp2_rx_desc *rx_desc)
  917. {
  918. return rx_desc->data_size;
  919. }
  920. static u32 mvpp2_rxdesc_status_get(struct mvpp2_port *port,
  921. struct mvpp2_rx_desc *rx_desc)
  922. {
  923. return rx_desc->status;
  924. }
  925. static void mvpp2_txq_inc_get(struct mvpp2_txq_pcpu *txq_pcpu)
  926. {
  927. txq_pcpu->txq_get_index++;
  928. if (txq_pcpu->txq_get_index == txq_pcpu->size)
  929. txq_pcpu->txq_get_index = 0;
  930. }
  931. /* Get number of physical egress port */
  932. static inline int mvpp2_egress_port(struct mvpp2_port *port)
  933. {
  934. return MVPP2_MAX_TCONT + port->id;
  935. }
  936. /* Get number of physical TXQ */
  937. static inline int mvpp2_txq_phys(int port, int txq)
  938. {
  939. return (MVPP2_MAX_TCONT + port) * MVPP2_MAX_TXQ + txq;
  940. }
  941. /* Parser configuration routines */
  942. /* Update parser tcam and sram hw entries */
  943. static int mvpp2_prs_hw_write(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
  944. {
  945. int i;
  946. if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
  947. return -EINVAL;
  948. /* Clear entry invalidation bit */
  949. pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] &= ~MVPP2_PRS_TCAM_INV_MASK;
  950. /* Write tcam index - indirect access */
  951. mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
  952. for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
  953. mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), pe->tcam.word[i]);
  954. /* Write sram index - indirect access */
  955. mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
  956. for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
  957. mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), pe->sram.word[i]);
  958. return 0;
  959. }
  960. /* Read tcam entry from hw */
  961. static int mvpp2_prs_hw_read(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
  962. {
  963. int i;
  964. if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
  965. return -EINVAL;
  966. /* Write tcam index - indirect access */
  967. mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
  968. pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] = mvpp2_read(priv,
  969. MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD));
  970. if (pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] & MVPP2_PRS_TCAM_INV_MASK)
  971. return MVPP2_PRS_TCAM_ENTRY_INVALID;
  972. for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
  973. pe->tcam.word[i] = mvpp2_read(priv, MVPP2_PRS_TCAM_DATA_REG(i));
  974. /* Write sram index - indirect access */
  975. mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
  976. for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
  977. pe->sram.word[i] = mvpp2_read(priv, MVPP2_PRS_SRAM_DATA_REG(i));
  978. return 0;
  979. }
  980. /* Invalidate tcam hw entry */
  981. static void mvpp2_prs_hw_inv(struct mvpp2 *priv, int index)
  982. {
  983. /* Write index - indirect access */
  984. mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
  985. mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD),
  986. MVPP2_PRS_TCAM_INV_MASK);
  987. }
  988. /* Enable shadow table entry and set its lookup ID */
  989. static void mvpp2_prs_shadow_set(struct mvpp2 *priv, int index, int lu)
  990. {
  991. priv->prs_shadow[index].valid = true;
  992. priv->prs_shadow[index].lu = lu;
  993. }
  994. /* Update ri fields in shadow table entry */
  995. static void mvpp2_prs_shadow_ri_set(struct mvpp2 *priv, int index,
  996. unsigned int ri, unsigned int ri_mask)
  997. {
  998. priv->prs_shadow[index].ri_mask = ri_mask;
  999. priv->prs_shadow[index].ri = ri;
  1000. }
  1001. /* Update lookup field in tcam sw entry */
  1002. static void mvpp2_prs_tcam_lu_set(struct mvpp2_prs_entry *pe, unsigned int lu)
  1003. {
  1004. int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_LU_BYTE);
  1005. pe->tcam.byte[MVPP2_PRS_TCAM_LU_BYTE] = lu;
  1006. pe->tcam.byte[enable_off] = MVPP2_PRS_LU_MASK;
  1007. }
  1008. /* Update mask for single port in tcam sw entry */
  1009. static void mvpp2_prs_tcam_port_set(struct mvpp2_prs_entry *pe,
  1010. unsigned int port, bool add)
  1011. {
  1012. int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
  1013. if (add)
  1014. pe->tcam.byte[enable_off] &= ~(1 << port);
  1015. else
  1016. pe->tcam.byte[enable_off] |= 1 << port;
  1017. }
  1018. /* Update port map in tcam sw entry */
  1019. static void mvpp2_prs_tcam_port_map_set(struct mvpp2_prs_entry *pe,
  1020. unsigned int ports)
  1021. {
  1022. unsigned char port_mask = MVPP2_PRS_PORT_MASK;
  1023. int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
  1024. pe->tcam.byte[MVPP2_PRS_TCAM_PORT_BYTE] = 0;
  1025. pe->tcam.byte[enable_off] &= ~port_mask;
  1026. pe->tcam.byte[enable_off] |= ~ports & MVPP2_PRS_PORT_MASK;
  1027. }
  1028. /* Obtain port map from tcam sw entry */
  1029. static unsigned int mvpp2_prs_tcam_port_map_get(struct mvpp2_prs_entry *pe)
  1030. {
  1031. int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
  1032. return ~(pe->tcam.byte[enable_off]) & MVPP2_PRS_PORT_MASK;
  1033. }
  1034. /* Set byte of data and its enable bits in tcam sw entry */
  1035. static void mvpp2_prs_tcam_data_byte_set(struct mvpp2_prs_entry *pe,
  1036. unsigned int offs, unsigned char byte,
  1037. unsigned char enable)
  1038. {
  1039. pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)] = byte;
  1040. pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)] = enable;
  1041. }
  1042. /* Get byte of data and its enable bits from tcam sw entry */
  1043. static void mvpp2_prs_tcam_data_byte_get(struct mvpp2_prs_entry *pe,
  1044. unsigned int offs, unsigned char *byte,
  1045. unsigned char *enable)
  1046. {
  1047. *byte = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)];
  1048. *enable = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)];
  1049. }
  1050. /* Set ethertype in tcam sw entry */
  1051. static void mvpp2_prs_match_etype(struct mvpp2_prs_entry *pe, int offset,
  1052. unsigned short ethertype)
  1053. {
  1054. mvpp2_prs_tcam_data_byte_set(pe, offset + 0, ethertype >> 8, 0xff);
  1055. mvpp2_prs_tcam_data_byte_set(pe, offset + 1, ethertype & 0xff, 0xff);
  1056. }
  1057. /* Set bits in sram sw entry */
  1058. static void mvpp2_prs_sram_bits_set(struct mvpp2_prs_entry *pe, int bit_num,
  1059. int val)
  1060. {
  1061. pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] |= (val << (bit_num % 8));
  1062. }
  1063. /* Clear bits in sram sw entry */
  1064. static void mvpp2_prs_sram_bits_clear(struct mvpp2_prs_entry *pe, int bit_num,
  1065. int val)
  1066. {
  1067. pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] &= ~(val << (bit_num % 8));
  1068. }
  1069. /* Update ri bits in sram sw entry */
  1070. static void mvpp2_prs_sram_ri_update(struct mvpp2_prs_entry *pe,
  1071. unsigned int bits, unsigned int mask)
  1072. {
  1073. unsigned int i;
  1074. for (i = 0; i < MVPP2_PRS_SRAM_RI_CTRL_BITS; i++) {
  1075. int ri_off = MVPP2_PRS_SRAM_RI_OFFS;
  1076. if (!(mask & BIT(i)))
  1077. continue;
  1078. if (bits & BIT(i))
  1079. mvpp2_prs_sram_bits_set(pe, ri_off + i, 1);
  1080. else
  1081. mvpp2_prs_sram_bits_clear(pe, ri_off + i, 1);
  1082. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_RI_CTRL_OFFS + i, 1);
  1083. }
  1084. }
  1085. /* Update ai bits in sram sw entry */
  1086. static void mvpp2_prs_sram_ai_update(struct mvpp2_prs_entry *pe,
  1087. unsigned int bits, unsigned int mask)
  1088. {
  1089. unsigned int i;
  1090. int ai_off = MVPP2_PRS_SRAM_AI_OFFS;
  1091. for (i = 0; i < MVPP2_PRS_SRAM_AI_CTRL_BITS; i++) {
  1092. if (!(mask & BIT(i)))
  1093. continue;
  1094. if (bits & BIT(i))
  1095. mvpp2_prs_sram_bits_set(pe, ai_off + i, 1);
  1096. else
  1097. mvpp2_prs_sram_bits_clear(pe, ai_off + i, 1);
  1098. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_AI_CTRL_OFFS + i, 1);
  1099. }
  1100. }
  1101. /* Read ai bits from sram sw entry */
  1102. static int mvpp2_prs_sram_ai_get(struct mvpp2_prs_entry *pe)
  1103. {
  1104. u8 bits;
  1105. int ai_off = MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_AI_OFFS);
  1106. int ai_en_off = ai_off + 1;
  1107. int ai_shift = MVPP2_PRS_SRAM_AI_OFFS % 8;
  1108. bits = (pe->sram.byte[ai_off] >> ai_shift) |
  1109. (pe->sram.byte[ai_en_off] << (8 - ai_shift));
  1110. return bits;
  1111. }
  1112. /* In sram sw entry set lookup ID field of the tcam key to be used in the next
  1113. * lookup interation
  1114. */
  1115. static void mvpp2_prs_sram_next_lu_set(struct mvpp2_prs_entry *pe,
  1116. unsigned int lu)
  1117. {
  1118. int sram_next_off = MVPP2_PRS_SRAM_NEXT_LU_OFFS;
  1119. mvpp2_prs_sram_bits_clear(pe, sram_next_off,
  1120. MVPP2_PRS_SRAM_NEXT_LU_MASK);
  1121. mvpp2_prs_sram_bits_set(pe, sram_next_off, lu);
  1122. }
  1123. /* In the sram sw entry set sign and value of the next lookup offset
  1124. * and the offset value generated to the classifier
  1125. */
  1126. static void mvpp2_prs_sram_shift_set(struct mvpp2_prs_entry *pe, int shift,
  1127. unsigned int op)
  1128. {
  1129. /* Set sign */
  1130. if (shift < 0) {
  1131. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
  1132. shift = 0 - shift;
  1133. } else {
  1134. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
  1135. }
  1136. /* Set value */
  1137. pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_SHIFT_OFFS)] =
  1138. (unsigned char)shift;
  1139. /* Reset and set operation */
  1140. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS,
  1141. MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK);
  1142. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS, op);
  1143. /* Set base offset as current */
  1144. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
  1145. }
  1146. /* In the sram sw entry set sign and value of the user defined offset
  1147. * generated to the classifier
  1148. */
  1149. static void mvpp2_prs_sram_offset_set(struct mvpp2_prs_entry *pe,
  1150. unsigned int type, int offset,
  1151. unsigned int op)
  1152. {
  1153. /* Set sign */
  1154. if (offset < 0) {
  1155. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
  1156. offset = 0 - offset;
  1157. } else {
  1158. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
  1159. }
  1160. /* Set value */
  1161. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_OFFS,
  1162. MVPP2_PRS_SRAM_UDF_MASK);
  1163. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_OFFS, offset);
  1164. pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
  1165. MVPP2_PRS_SRAM_UDF_BITS)] &=
  1166. ~(MVPP2_PRS_SRAM_UDF_MASK >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
  1167. pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
  1168. MVPP2_PRS_SRAM_UDF_BITS)] |=
  1169. (offset >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
  1170. /* Set offset type */
  1171. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS,
  1172. MVPP2_PRS_SRAM_UDF_TYPE_MASK);
  1173. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS, type);
  1174. /* Set offset operation */
  1175. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS,
  1176. MVPP2_PRS_SRAM_OP_SEL_UDF_MASK);
  1177. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS, op);
  1178. pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
  1179. MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] &=
  1180. ~(MVPP2_PRS_SRAM_OP_SEL_UDF_MASK >>
  1181. (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
  1182. pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
  1183. MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] |=
  1184. (op >> (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
  1185. /* Set base offset as current */
  1186. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
  1187. }
  1188. /* Find parser flow entry */
  1189. static struct mvpp2_prs_entry *mvpp2_prs_flow_find(struct mvpp2 *priv, int flow)
  1190. {
  1191. struct mvpp2_prs_entry *pe;
  1192. int tid;
  1193. pe = kzalloc(sizeof(*pe), GFP_KERNEL);
  1194. if (!pe)
  1195. return NULL;
  1196. mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
  1197. /* Go through the all entires with MVPP2_PRS_LU_FLOWS */
  1198. for (tid = MVPP2_PRS_TCAM_SRAM_SIZE - 1; tid >= 0; tid--) {
  1199. u8 bits;
  1200. if (!priv->prs_shadow[tid].valid ||
  1201. priv->prs_shadow[tid].lu != MVPP2_PRS_LU_FLOWS)
  1202. continue;
  1203. pe->index = tid;
  1204. mvpp2_prs_hw_read(priv, pe);
  1205. bits = mvpp2_prs_sram_ai_get(pe);
  1206. /* Sram store classification lookup ID in AI bits [5:0] */
  1207. if ((bits & MVPP2_PRS_FLOW_ID_MASK) == flow)
  1208. return pe;
  1209. }
  1210. kfree(pe);
  1211. return NULL;
  1212. }
  1213. /* Return first free tcam index, seeking from start to end */
  1214. static int mvpp2_prs_tcam_first_free(struct mvpp2 *priv, unsigned char start,
  1215. unsigned char end)
  1216. {
  1217. int tid;
  1218. if (start > end)
  1219. swap(start, end);
  1220. if (end >= MVPP2_PRS_TCAM_SRAM_SIZE)
  1221. end = MVPP2_PRS_TCAM_SRAM_SIZE - 1;
  1222. for (tid = start; tid <= end; tid++) {
  1223. if (!priv->prs_shadow[tid].valid)
  1224. return tid;
  1225. }
  1226. return -EINVAL;
  1227. }
  1228. /* Enable/disable dropping all mac da's */
  1229. static void mvpp2_prs_mac_drop_all_set(struct mvpp2 *priv, int port, bool add)
  1230. {
  1231. struct mvpp2_prs_entry pe;
  1232. if (priv->prs_shadow[MVPP2_PE_DROP_ALL].valid) {
  1233. /* Entry exist - update port only */
  1234. pe.index = MVPP2_PE_DROP_ALL;
  1235. mvpp2_prs_hw_read(priv, &pe);
  1236. } else {
  1237. /* Entry doesn't exist - create new */
  1238. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1239. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
  1240. pe.index = MVPP2_PE_DROP_ALL;
  1241. /* Non-promiscuous mode for all ports - DROP unknown packets */
  1242. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
  1243. MVPP2_PRS_RI_DROP_MASK);
  1244. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  1245. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  1246. /* Update shadow table */
  1247. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
  1248. /* Mask all ports */
  1249. mvpp2_prs_tcam_port_map_set(&pe, 0);
  1250. }
  1251. /* Update port mask */
  1252. mvpp2_prs_tcam_port_set(&pe, port, add);
  1253. mvpp2_prs_hw_write(priv, &pe);
  1254. }
  1255. /* Set port to promiscuous mode */
  1256. static void mvpp2_prs_mac_promisc_set(struct mvpp2 *priv, int port, bool add)
  1257. {
  1258. struct mvpp2_prs_entry pe;
  1259. /* Promiscuous mode - Accept unknown packets */
  1260. if (priv->prs_shadow[MVPP2_PE_MAC_PROMISCUOUS].valid) {
  1261. /* Entry exist - update port only */
  1262. pe.index = MVPP2_PE_MAC_PROMISCUOUS;
  1263. mvpp2_prs_hw_read(priv, &pe);
  1264. } else {
  1265. /* Entry doesn't exist - create new */
  1266. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1267. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
  1268. pe.index = MVPP2_PE_MAC_PROMISCUOUS;
  1269. /* Continue - set next lookup */
  1270. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
  1271. /* Set result info bits */
  1272. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_UCAST,
  1273. MVPP2_PRS_RI_L2_CAST_MASK);
  1274. /* Shift to ethertype */
  1275. mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
  1276. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1277. /* Mask all ports */
  1278. mvpp2_prs_tcam_port_map_set(&pe, 0);
  1279. /* Update shadow table */
  1280. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
  1281. }
  1282. /* Update port mask */
  1283. mvpp2_prs_tcam_port_set(&pe, port, add);
  1284. mvpp2_prs_hw_write(priv, &pe);
  1285. }
  1286. /* Accept multicast */
  1287. static void mvpp2_prs_mac_multi_set(struct mvpp2 *priv, int port, int index,
  1288. bool add)
  1289. {
  1290. struct mvpp2_prs_entry pe;
  1291. unsigned char da_mc;
  1292. /* Ethernet multicast address first byte is
  1293. * 0x01 for IPv4 and 0x33 for IPv6
  1294. */
  1295. da_mc = (index == MVPP2_PE_MAC_MC_ALL) ? 0x01 : 0x33;
  1296. if (priv->prs_shadow[index].valid) {
  1297. /* Entry exist - update port only */
  1298. pe.index = index;
  1299. mvpp2_prs_hw_read(priv, &pe);
  1300. } else {
  1301. /* Entry doesn't exist - create new */
  1302. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1303. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
  1304. pe.index = index;
  1305. /* Continue - set next lookup */
  1306. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
  1307. /* Set result info bits */
  1308. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_MCAST,
  1309. MVPP2_PRS_RI_L2_CAST_MASK);
  1310. /* Update tcam entry data first byte */
  1311. mvpp2_prs_tcam_data_byte_set(&pe, 0, da_mc, 0xff);
  1312. /* Shift to ethertype */
  1313. mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
  1314. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1315. /* Mask all ports */
  1316. mvpp2_prs_tcam_port_map_set(&pe, 0);
  1317. /* Update shadow table */
  1318. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
  1319. }
  1320. /* Update port mask */
  1321. mvpp2_prs_tcam_port_set(&pe, port, add);
  1322. mvpp2_prs_hw_write(priv, &pe);
  1323. }
  1324. /* Parser per-port initialization */
  1325. static void mvpp2_prs_hw_port_init(struct mvpp2 *priv, int port, int lu_first,
  1326. int lu_max, int offset)
  1327. {
  1328. u32 val;
  1329. /* Set lookup ID */
  1330. val = mvpp2_read(priv, MVPP2_PRS_INIT_LOOKUP_REG);
  1331. val &= ~MVPP2_PRS_PORT_LU_MASK(port);
  1332. val |= MVPP2_PRS_PORT_LU_VAL(port, lu_first);
  1333. mvpp2_write(priv, MVPP2_PRS_INIT_LOOKUP_REG, val);
  1334. /* Set maximum number of loops for packet received from port */
  1335. val = mvpp2_read(priv, MVPP2_PRS_MAX_LOOP_REG(port));
  1336. val &= ~MVPP2_PRS_MAX_LOOP_MASK(port);
  1337. val |= MVPP2_PRS_MAX_LOOP_VAL(port, lu_max);
  1338. mvpp2_write(priv, MVPP2_PRS_MAX_LOOP_REG(port), val);
  1339. /* Set initial offset for packet header extraction for the first
  1340. * searching loop
  1341. */
  1342. val = mvpp2_read(priv, MVPP2_PRS_INIT_OFFS_REG(port));
  1343. val &= ~MVPP2_PRS_INIT_OFF_MASK(port);
  1344. val |= MVPP2_PRS_INIT_OFF_VAL(port, offset);
  1345. mvpp2_write(priv, MVPP2_PRS_INIT_OFFS_REG(port), val);
  1346. }
  1347. /* Default flow entries initialization for all ports */
  1348. static void mvpp2_prs_def_flow_init(struct mvpp2 *priv)
  1349. {
  1350. struct mvpp2_prs_entry pe;
  1351. int port;
  1352. for (port = 0; port < MVPP2_MAX_PORTS; port++) {
  1353. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1354. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  1355. pe.index = MVPP2_PE_FIRST_DEFAULT_FLOW - port;
  1356. /* Mask all ports */
  1357. mvpp2_prs_tcam_port_map_set(&pe, 0);
  1358. /* Set flow ID*/
  1359. mvpp2_prs_sram_ai_update(&pe, port, MVPP2_PRS_FLOW_ID_MASK);
  1360. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
  1361. /* Update shadow table and hw entry */
  1362. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_FLOWS);
  1363. mvpp2_prs_hw_write(priv, &pe);
  1364. }
  1365. }
  1366. /* Set default entry for Marvell Header field */
  1367. static void mvpp2_prs_mh_init(struct mvpp2 *priv)
  1368. {
  1369. struct mvpp2_prs_entry pe;
  1370. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1371. pe.index = MVPP2_PE_MH_DEFAULT;
  1372. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MH);
  1373. mvpp2_prs_sram_shift_set(&pe, MVPP2_MH_SIZE,
  1374. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1375. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_MAC);
  1376. /* Unmask all ports */
  1377. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  1378. /* Update shadow table and hw entry */
  1379. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MH);
  1380. mvpp2_prs_hw_write(priv, &pe);
  1381. }
  1382. /* Set default entires (place holder) for promiscuous, non-promiscuous and
  1383. * multicast MAC addresses
  1384. */
  1385. static void mvpp2_prs_mac_init(struct mvpp2 *priv)
  1386. {
  1387. struct mvpp2_prs_entry pe;
  1388. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1389. /* Non-promiscuous mode for all ports - DROP unknown packets */
  1390. pe.index = MVPP2_PE_MAC_NON_PROMISCUOUS;
  1391. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
  1392. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
  1393. MVPP2_PRS_RI_DROP_MASK);
  1394. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  1395. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  1396. /* Unmask all ports */
  1397. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  1398. /* Update shadow table and hw entry */
  1399. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
  1400. mvpp2_prs_hw_write(priv, &pe);
  1401. /* place holders only - no ports */
  1402. mvpp2_prs_mac_drop_all_set(priv, 0, false);
  1403. mvpp2_prs_mac_promisc_set(priv, 0, false);
  1404. mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_ALL, 0, false);
  1405. mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_IP6, 0, false);
  1406. }
  1407. /* Match basic ethertypes */
  1408. static int mvpp2_prs_etype_init(struct mvpp2 *priv)
  1409. {
  1410. struct mvpp2_prs_entry pe;
  1411. int tid;
  1412. /* Ethertype: PPPoE */
  1413. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1414. MVPP2_PE_LAST_FREE_TID);
  1415. if (tid < 0)
  1416. return tid;
  1417. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1418. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  1419. pe.index = tid;
  1420. mvpp2_prs_match_etype(&pe, 0, PROT_PPP_SES);
  1421. mvpp2_prs_sram_shift_set(&pe, MVPP2_PPPOE_HDR_SIZE,
  1422. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1423. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
  1424. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_PPPOE_MASK,
  1425. MVPP2_PRS_RI_PPPOE_MASK);
  1426. /* Update shadow table and hw entry */
  1427. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1428. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1429. priv->prs_shadow[pe.index].finish = false;
  1430. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_PPPOE_MASK,
  1431. MVPP2_PRS_RI_PPPOE_MASK);
  1432. mvpp2_prs_hw_write(priv, &pe);
  1433. /* Ethertype: ARP */
  1434. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1435. MVPP2_PE_LAST_FREE_TID);
  1436. if (tid < 0)
  1437. return tid;
  1438. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1439. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  1440. pe.index = tid;
  1441. mvpp2_prs_match_etype(&pe, 0, PROT_ARP);
  1442. /* Generate flow in the next iteration*/
  1443. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  1444. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  1445. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_ARP,
  1446. MVPP2_PRS_RI_L3_PROTO_MASK);
  1447. /* Set L3 offset */
  1448. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  1449. MVPP2_ETH_TYPE_LEN,
  1450. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  1451. /* Update shadow table and hw entry */
  1452. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1453. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1454. priv->prs_shadow[pe.index].finish = true;
  1455. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_ARP,
  1456. MVPP2_PRS_RI_L3_PROTO_MASK);
  1457. mvpp2_prs_hw_write(priv, &pe);
  1458. /* Ethertype: LBTD */
  1459. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1460. MVPP2_PE_LAST_FREE_TID);
  1461. if (tid < 0)
  1462. return tid;
  1463. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1464. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  1465. pe.index = tid;
  1466. mvpp2_prs_match_etype(&pe, 0, MVPP2_IP_LBDT_TYPE);
  1467. /* Generate flow in the next iteration*/
  1468. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  1469. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  1470. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
  1471. MVPP2_PRS_RI_UDF3_RX_SPECIAL,
  1472. MVPP2_PRS_RI_CPU_CODE_MASK |
  1473. MVPP2_PRS_RI_UDF3_MASK);
  1474. /* Set L3 offset */
  1475. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  1476. MVPP2_ETH_TYPE_LEN,
  1477. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  1478. /* Update shadow table and hw entry */
  1479. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1480. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1481. priv->prs_shadow[pe.index].finish = true;
  1482. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
  1483. MVPP2_PRS_RI_UDF3_RX_SPECIAL,
  1484. MVPP2_PRS_RI_CPU_CODE_MASK |
  1485. MVPP2_PRS_RI_UDF3_MASK);
  1486. mvpp2_prs_hw_write(priv, &pe);
  1487. /* Ethertype: IPv4 without options */
  1488. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1489. MVPP2_PE_LAST_FREE_TID);
  1490. if (tid < 0)
  1491. return tid;
  1492. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1493. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  1494. pe.index = tid;
  1495. mvpp2_prs_match_etype(&pe, 0, PROT_IP);
  1496. mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
  1497. MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
  1498. MVPP2_PRS_IPV4_HEAD_MASK |
  1499. MVPP2_PRS_IPV4_IHL_MASK);
  1500. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
  1501. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
  1502. MVPP2_PRS_RI_L3_PROTO_MASK);
  1503. /* Skip eth_type + 4 bytes of IP header */
  1504. mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
  1505. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1506. /* Set L3 offset */
  1507. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  1508. MVPP2_ETH_TYPE_LEN,
  1509. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  1510. /* Update shadow table and hw entry */
  1511. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1512. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1513. priv->prs_shadow[pe.index].finish = false;
  1514. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4,
  1515. MVPP2_PRS_RI_L3_PROTO_MASK);
  1516. mvpp2_prs_hw_write(priv, &pe);
  1517. /* Ethertype: IPv4 with options */
  1518. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1519. MVPP2_PE_LAST_FREE_TID);
  1520. if (tid < 0)
  1521. return tid;
  1522. pe.index = tid;
  1523. /* Clear tcam data before updating */
  1524. pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(MVPP2_ETH_TYPE_LEN)] = 0x0;
  1525. pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(MVPP2_ETH_TYPE_LEN)] = 0x0;
  1526. mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
  1527. MVPP2_PRS_IPV4_HEAD,
  1528. MVPP2_PRS_IPV4_HEAD_MASK);
  1529. /* Clear ri before updating */
  1530. pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
  1531. pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
  1532. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
  1533. MVPP2_PRS_RI_L3_PROTO_MASK);
  1534. /* Update shadow table and hw entry */
  1535. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1536. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1537. priv->prs_shadow[pe.index].finish = false;
  1538. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4_OPT,
  1539. MVPP2_PRS_RI_L3_PROTO_MASK);
  1540. mvpp2_prs_hw_write(priv, &pe);
  1541. /* Ethertype: IPv6 without options */
  1542. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1543. MVPP2_PE_LAST_FREE_TID);
  1544. if (tid < 0)
  1545. return tid;
  1546. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1547. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  1548. pe.index = tid;
  1549. mvpp2_prs_match_etype(&pe, 0, PROT_IPV6);
  1550. /* Skip DIP of IPV6 header */
  1551. mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 8 +
  1552. MVPP2_MAX_L3_ADDR_SIZE,
  1553. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1554. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
  1555. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
  1556. MVPP2_PRS_RI_L3_PROTO_MASK);
  1557. /* Set L3 offset */
  1558. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  1559. MVPP2_ETH_TYPE_LEN,
  1560. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  1561. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1562. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1563. priv->prs_shadow[pe.index].finish = false;
  1564. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP6,
  1565. MVPP2_PRS_RI_L3_PROTO_MASK);
  1566. mvpp2_prs_hw_write(priv, &pe);
  1567. /* Default entry for MVPP2_PRS_LU_L2 - Unknown ethtype */
  1568. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1569. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  1570. pe.index = MVPP2_PE_ETH_TYPE_UN;
  1571. /* Unmask all ports */
  1572. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  1573. /* Generate flow in the next iteration*/
  1574. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  1575. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  1576. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
  1577. MVPP2_PRS_RI_L3_PROTO_MASK);
  1578. /* Set L3 offset even it's unknown L3 */
  1579. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  1580. MVPP2_ETH_TYPE_LEN,
  1581. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  1582. /* Update shadow table and hw entry */
  1583. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1584. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1585. priv->prs_shadow[pe.index].finish = true;
  1586. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_UN,
  1587. MVPP2_PRS_RI_L3_PROTO_MASK);
  1588. mvpp2_prs_hw_write(priv, &pe);
  1589. return 0;
  1590. }
  1591. /* Parser default initialization */
  1592. static int mvpp2_prs_default_init(struct udevice *dev,
  1593. struct mvpp2 *priv)
  1594. {
  1595. int err, index, i;
  1596. /* Enable tcam table */
  1597. mvpp2_write(priv, MVPP2_PRS_TCAM_CTRL_REG, MVPP2_PRS_TCAM_EN_MASK);
  1598. /* Clear all tcam and sram entries */
  1599. for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++) {
  1600. mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
  1601. for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
  1602. mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), 0);
  1603. mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, index);
  1604. for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
  1605. mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), 0);
  1606. }
  1607. /* Invalidate all tcam entries */
  1608. for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++)
  1609. mvpp2_prs_hw_inv(priv, index);
  1610. priv->prs_shadow = devm_kcalloc(dev, MVPP2_PRS_TCAM_SRAM_SIZE,
  1611. sizeof(struct mvpp2_prs_shadow),
  1612. GFP_KERNEL);
  1613. if (!priv->prs_shadow)
  1614. return -ENOMEM;
  1615. /* Always start from lookup = 0 */
  1616. for (index = 0; index < MVPP2_MAX_PORTS; index++)
  1617. mvpp2_prs_hw_port_init(priv, index, MVPP2_PRS_LU_MH,
  1618. MVPP2_PRS_PORT_LU_MAX, 0);
  1619. mvpp2_prs_def_flow_init(priv);
  1620. mvpp2_prs_mh_init(priv);
  1621. mvpp2_prs_mac_init(priv);
  1622. err = mvpp2_prs_etype_init(priv);
  1623. if (err)
  1624. return err;
  1625. return 0;
  1626. }
  1627. /* Compare MAC DA with tcam entry data */
  1628. static bool mvpp2_prs_mac_range_equals(struct mvpp2_prs_entry *pe,
  1629. const u8 *da, unsigned char *mask)
  1630. {
  1631. unsigned char tcam_byte, tcam_mask;
  1632. int index;
  1633. for (index = 0; index < ETH_ALEN; index++) {
  1634. mvpp2_prs_tcam_data_byte_get(pe, index, &tcam_byte, &tcam_mask);
  1635. if (tcam_mask != mask[index])
  1636. return false;
  1637. if ((tcam_mask & tcam_byte) != (da[index] & mask[index]))
  1638. return false;
  1639. }
  1640. return true;
  1641. }
  1642. /* Find tcam entry with matched pair <MAC DA, port> */
  1643. static struct mvpp2_prs_entry *
  1644. mvpp2_prs_mac_da_range_find(struct mvpp2 *priv, int pmap, const u8 *da,
  1645. unsigned char *mask, int udf_type)
  1646. {
  1647. struct mvpp2_prs_entry *pe;
  1648. int tid;
  1649. pe = kzalloc(sizeof(*pe), GFP_KERNEL);
  1650. if (!pe)
  1651. return NULL;
  1652. mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
  1653. /* Go through the all entires with MVPP2_PRS_LU_MAC */
  1654. for (tid = MVPP2_PE_FIRST_FREE_TID;
  1655. tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
  1656. unsigned int entry_pmap;
  1657. if (!priv->prs_shadow[tid].valid ||
  1658. (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
  1659. (priv->prs_shadow[tid].udf != udf_type))
  1660. continue;
  1661. pe->index = tid;
  1662. mvpp2_prs_hw_read(priv, pe);
  1663. entry_pmap = mvpp2_prs_tcam_port_map_get(pe);
  1664. if (mvpp2_prs_mac_range_equals(pe, da, mask) &&
  1665. entry_pmap == pmap)
  1666. return pe;
  1667. }
  1668. kfree(pe);
  1669. return NULL;
  1670. }
  1671. /* Update parser's mac da entry */
  1672. static int mvpp2_prs_mac_da_accept(struct mvpp2 *priv, int port,
  1673. const u8 *da, bool add)
  1674. {
  1675. struct mvpp2_prs_entry *pe;
  1676. unsigned int pmap, len, ri;
  1677. unsigned char mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  1678. int tid;
  1679. /* Scan TCAM and see if entry with this <MAC DA, port> already exist */
  1680. pe = mvpp2_prs_mac_da_range_find(priv, (1 << port), da, mask,
  1681. MVPP2_PRS_UDF_MAC_DEF);
  1682. /* No such entry */
  1683. if (!pe) {
  1684. if (!add)
  1685. return 0;
  1686. /* Create new TCAM entry */
  1687. /* Find first range mac entry*/
  1688. for (tid = MVPP2_PE_FIRST_FREE_TID;
  1689. tid <= MVPP2_PE_LAST_FREE_TID; tid++)
  1690. if (priv->prs_shadow[tid].valid &&
  1691. (priv->prs_shadow[tid].lu == MVPP2_PRS_LU_MAC) &&
  1692. (priv->prs_shadow[tid].udf ==
  1693. MVPP2_PRS_UDF_MAC_RANGE))
  1694. break;
  1695. /* Go through the all entries from first to last */
  1696. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1697. tid - 1);
  1698. if (tid < 0)
  1699. return tid;
  1700. pe = kzalloc(sizeof(*pe), GFP_KERNEL);
  1701. if (!pe)
  1702. return -1;
  1703. mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
  1704. pe->index = tid;
  1705. /* Mask all ports */
  1706. mvpp2_prs_tcam_port_map_set(pe, 0);
  1707. }
  1708. /* Update port mask */
  1709. mvpp2_prs_tcam_port_set(pe, port, add);
  1710. /* Invalidate the entry if no ports are left enabled */
  1711. pmap = mvpp2_prs_tcam_port_map_get(pe);
  1712. if (pmap == 0) {
  1713. if (add) {
  1714. kfree(pe);
  1715. return -1;
  1716. }
  1717. mvpp2_prs_hw_inv(priv, pe->index);
  1718. priv->prs_shadow[pe->index].valid = false;
  1719. kfree(pe);
  1720. return 0;
  1721. }
  1722. /* Continue - set next lookup */
  1723. mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_DSA);
  1724. /* Set match on DA */
  1725. len = ETH_ALEN;
  1726. while (len--)
  1727. mvpp2_prs_tcam_data_byte_set(pe, len, da[len], 0xff);
  1728. /* Set result info bits */
  1729. ri = MVPP2_PRS_RI_L2_UCAST | MVPP2_PRS_RI_MAC_ME_MASK;
  1730. mvpp2_prs_sram_ri_update(pe, ri, MVPP2_PRS_RI_L2_CAST_MASK |
  1731. MVPP2_PRS_RI_MAC_ME_MASK);
  1732. mvpp2_prs_shadow_ri_set(priv, pe->index, ri, MVPP2_PRS_RI_L2_CAST_MASK |
  1733. MVPP2_PRS_RI_MAC_ME_MASK);
  1734. /* Shift to ethertype */
  1735. mvpp2_prs_sram_shift_set(pe, 2 * ETH_ALEN,
  1736. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1737. /* Update shadow table and hw entry */
  1738. priv->prs_shadow[pe->index].udf = MVPP2_PRS_UDF_MAC_DEF;
  1739. mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_MAC);
  1740. mvpp2_prs_hw_write(priv, pe);
  1741. kfree(pe);
  1742. return 0;
  1743. }
  1744. static int mvpp2_prs_update_mac_da(struct mvpp2_port *port, const u8 *da)
  1745. {
  1746. int err;
  1747. /* Remove old parser entry */
  1748. err = mvpp2_prs_mac_da_accept(port->priv, port->id, port->dev_addr,
  1749. false);
  1750. if (err)
  1751. return err;
  1752. /* Add new parser entry */
  1753. err = mvpp2_prs_mac_da_accept(port->priv, port->id, da, true);
  1754. if (err)
  1755. return err;
  1756. /* Set addr in the device */
  1757. memcpy(port->dev_addr, da, ETH_ALEN);
  1758. return 0;
  1759. }
  1760. /* Set prs flow for the port */
  1761. static int mvpp2_prs_def_flow(struct mvpp2_port *port)
  1762. {
  1763. struct mvpp2_prs_entry *pe;
  1764. int tid;
  1765. pe = mvpp2_prs_flow_find(port->priv, port->id);
  1766. /* Such entry not exist */
  1767. if (!pe) {
  1768. /* Go through the all entires from last to first */
  1769. tid = mvpp2_prs_tcam_first_free(port->priv,
  1770. MVPP2_PE_LAST_FREE_TID,
  1771. MVPP2_PE_FIRST_FREE_TID);
  1772. if (tid < 0)
  1773. return tid;
  1774. pe = kzalloc(sizeof(*pe), GFP_KERNEL);
  1775. if (!pe)
  1776. return -ENOMEM;
  1777. mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
  1778. pe->index = tid;
  1779. /* Set flow ID*/
  1780. mvpp2_prs_sram_ai_update(pe, port->id, MVPP2_PRS_FLOW_ID_MASK);
  1781. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
  1782. /* Update shadow table */
  1783. mvpp2_prs_shadow_set(port->priv, pe->index, MVPP2_PRS_LU_FLOWS);
  1784. }
  1785. mvpp2_prs_tcam_port_map_set(pe, (1 << port->id));
  1786. mvpp2_prs_hw_write(port->priv, pe);
  1787. kfree(pe);
  1788. return 0;
  1789. }
  1790. /* Classifier configuration routines */
  1791. /* Update classification flow table registers */
  1792. static void mvpp2_cls_flow_write(struct mvpp2 *priv,
  1793. struct mvpp2_cls_flow_entry *fe)
  1794. {
  1795. mvpp2_write(priv, MVPP2_CLS_FLOW_INDEX_REG, fe->index);
  1796. mvpp2_write(priv, MVPP2_CLS_FLOW_TBL0_REG, fe->data[0]);
  1797. mvpp2_write(priv, MVPP2_CLS_FLOW_TBL1_REG, fe->data[1]);
  1798. mvpp2_write(priv, MVPP2_CLS_FLOW_TBL2_REG, fe->data[2]);
  1799. }
  1800. /* Update classification lookup table register */
  1801. static void mvpp2_cls_lookup_write(struct mvpp2 *priv,
  1802. struct mvpp2_cls_lookup_entry *le)
  1803. {
  1804. u32 val;
  1805. val = (le->way << MVPP2_CLS_LKP_INDEX_WAY_OFFS) | le->lkpid;
  1806. mvpp2_write(priv, MVPP2_CLS_LKP_INDEX_REG, val);
  1807. mvpp2_write(priv, MVPP2_CLS_LKP_TBL_REG, le->data);
  1808. }
  1809. /* Classifier default initialization */
  1810. static void mvpp2_cls_init(struct mvpp2 *priv)
  1811. {
  1812. struct mvpp2_cls_lookup_entry le;
  1813. struct mvpp2_cls_flow_entry fe;
  1814. int index;
  1815. /* Enable classifier */
  1816. mvpp2_write(priv, MVPP2_CLS_MODE_REG, MVPP2_CLS_MODE_ACTIVE_MASK);
  1817. /* Clear classifier flow table */
  1818. memset(&fe.data, 0, MVPP2_CLS_FLOWS_TBL_DATA_WORDS);
  1819. for (index = 0; index < MVPP2_CLS_FLOWS_TBL_SIZE; index++) {
  1820. fe.index = index;
  1821. mvpp2_cls_flow_write(priv, &fe);
  1822. }
  1823. /* Clear classifier lookup table */
  1824. le.data = 0;
  1825. for (index = 0; index < MVPP2_CLS_LKP_TBL_SIZE; index++) {
  1826. le.lkpid = index;
  1827. le.way = 0;
  1828. mvpp2_cls_lookup_write(priv, &le);
  1829. le.way = 1;
  1830. mvpp2_cls_lookup_write(priv, &le);
  1831. }
  1832. }
  1833. static void mvpp2_cls_port_config(struct mvpp2_port *port)
  1834. {
  1835. struct mvpp2_cls_lookup_entry le;
  1836. u32 val;
  1837. /* Set way for the port */
  1838. val = mvpp2_read(port->priv, MVPP2_CLS_PORT_WAY_REG);
  1839. val &= ~MVPP2_CLS_PORT_WAY_MASK(port->id);
  1840. mvpp2_write(port->priv, MVPP2_CLS_PORT_WAY_REG, val);
  1841. /* Pick the entry to be accessed in lookup ID decoding table
  1842. * according to the way and lkpid.
  1843. */
  1844. le.lkpid = port->id;
  1845. le.way = 0;
  1846. le.data = 0;
  1847. /* Set initial CPU queue for receiving packets */
  1848. le.data &= ~MVPP2_CLS_LKP_TBL_RXQ_MASK;
  1849. le.data |= port->first_rxq;
  1850. /* Disable classification engines */
  1851. le.data &= ~MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK;
  1852. /* Update lookup ID table entry */
  1853. mvpp2_cls_lookup_write(port->priv, &le);
  1854. }
  1855. /* Set CPU queue number for oversize packets */
  1856. static void mvpp2_cls_oversize_rxq_set(struct mvpp2_port *port)
  1857. {
  1858. u32 val;
  1859. mvpp2_write(port->priv, MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port->id),
  1860. port->first_rxq & MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK);
  1861. mvpp2_write(port->priv, MVPP2_CLS_SWFWD_P2HQ_REG(port->id),
  1862. (port->first_rxq >> MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS));
  1863. val = mvpp2_read(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG);
  1864. val |= MVPP2_CLS_SWFWD_PCTRL_MASK(port->id);
  1865. mvpp2_write(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG, val);
  1866. }
  1867. /* Buffer Manager configuration routines */
  1868. /* Create pool */
  1869. static int mvpp2_bm_pool_create(struct udevice *dev,
  1870. struct mvpp2 *priv,
  1871. struct mvpp2_bm_pool *bm_pool, int size)
  1872. {
  1873. u32 val;
  1874. bm_pool->virt_addr = buffer_loc.bm_pool[bm_pool->id];
  1875. bm_pool->dma_addr = (dma_addr_t)buffer_loc.bm_pool[bm_pool->id];
  1876. if (!bm_pool->virt_addr)
  1877. return -ENOMEM;
  1878. if (!IS_ALIGNED((unsigned long)bm_pool->virt_addr,
  1879. MVPP2_BM_POOL_PTR_ALIGN)) {
  1880. dev_err(&pdev->dev, "BM pool %d is not %d bytes aligned\n",
  1881. bm_pool->id, MVPP2_BM_POOL_PTR_ALIGN);
  1882. return -ENOMEM;
  1883. }
  1884. mvpp2_write(priv, MVPP2_BM_POOL_BASE_REG(bm_pool->id),
  1885. bm_pool->dma_addr);
  1886. mvpp2_write(priv, MVPP2_BM_POOL_SIZE_REG(bm_pool->id), size);
  1887. val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
  1888. val |= MVPP2_BM_START_MASK;
  1889. mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
  1890. bm_pool->type = MVPP2_BM_FREE;
  1891. bm_pool->size = size;
  1892. bm_pool->pkt_size = 0;
  1893. bm_pool->buf_num = 0;
  1894. return 0;
  1895. }
  1896. /* Set pool buffer size */
  1897. static void mvpp2_bm_pool_bufsize_set(struct mvpp2 *priv,
  1898. struct mvpp2_bm_pool *bm_pool,
  1899. int buf_size)
  1900. {
  1901. u32 val;
  1902. bm_pool->buf_size = buf_size;
  1903. val = ALIGN(buf_size, 1 << MVPP2_POOL_BUF_SIZE_OFFSET);
  1904. mvpp2_write(priv, MVPP2_POOL_BUF_SIZE_REG(bm_pool->id), val);
  1905. }
  1906. /* Free all buffers from the pool */
  1907. static void mvpp2_bm_bufs_free(struct udevice *dev, struct mvpp2 *priv,
  1908. struct mvpp2_bm_pool *bm_pool)
  1909. {
  1910. bm_pool->buf_num = 0;
  1911. }
  1912. /* Cleanup pool */
  1913. static int mvpp2_bm_pool_destroy(struct udevice *dev,
  1914. struct mvpp2 *priv,
  1915. struct mvpp2_bm_pool *bm_pool)
  1916. {
  1917. u32 val;
  1918. mvpp2_bm_bufs_free(dev, priv, bm_pool);
  1919. if (bm_pool->buf_num) {
  1920. dev_err(dev, "cannot free all buffers in pool %d\n", bm_pool->id);
  1921. return 0;
  1922. }
  1923. val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
  1924. val |= MVPP2_BM_STOP_MASK;
  1925. mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
  1926. return 0;
  1927. }
  1928. static int mvpp2_bm_pools_init(struct udevice *dev,
  1929. struct mvpp2 *priv)
  1930. {
  1931. int i, err, size;
  1932. struct mvpp2_bm_pool *bm_pool;
  1933. /* Create all pools with maximum size */
  1934. size = MVPP2_BM_POOL_SIZE_MAX;
  1935. for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
  1936. bm_pool = &priv->bm_pools[i];
  1937. bm_pool->id = i;
  1938. err = mvpp2_bm_pool_create(dev, priv, bm_pool, size);
  1939. if (err)
  1940. goto err_unroll_pools;
  1941. mvpp2_bm_pool_bufsize_set(priv, bm_pool, 0);
  1942. }
  1943. return 0;
  1944. err_unroll_pools:
  1945. dev_err(&pdev->dev, "failed to create BM pool %d, size %d\n", i, size);
  1946. for (i = i - 1; i >= 0; i--)
  1947. mvpp2_bm_pool_destroy(dev, priv, &priv->bm_pools[i]);
  1948. return err;
  1949. }
  1950. static int mvpp2_bm_init(struct udevice *dev, struct mvpp2 *priv)
  1951. {
  1952. int i, err;
  1953. for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
  1954. /* Mask BM all interrupts */
  1955. mvpp2_write(priv, MVPP2_BM_INTR_MASK_REG(i), 0);
  1956. /* Clear BM cause register */
  1957. mvpp2_write(priv, MVPP2_BM_INTR_CAUSE_REG(i), 0);
  1958. }
  1959. /* Allocate and initialize BM pools */
  1960. priv->bm_pools = devm_kcalloc(dev, MVPP2_BM_POOLS_NUM,
  1961. sizeof(struct mvpp2_bm_pool), GFP_KERNEL);
  1962. if (!priv->bm_pools)
  1963. return -ENOMEM;
  1964. err = mvpp2_bm_pools_init(dev, priv);
  1965. if (err < 0)
  1966. return err;
  1967. return 0;
  1968. }
  1969. /* Attach long pool to rxq */
  1970. static void mvpp2_rxq_long_pool_set(struct mvpp2_port *port,
  1971. int lrxq, int long_pool)
  1972. {
  1973. u32 val;
  1974. int prxq;
  1975. /* Get queue physical ID */
  1976. prxq = port->rxqs[lrxq]->id;
  1977. val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
  1978. val &= ~MVPP2_RXQ_POOL_LONG_MASK;
  1979. val |= ((long_pool << MVPP2_RXQ_POOL_LONG_OFFS) &
  1980. MVPP2_RXQ_POOL_LONG_MASK);
  1981. mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
  1982. }
  1983. /* Set pool number in a BM cookie */
  1984. static inline u32 mvpp2_bm_cookie_pool_set(u32 cookie, int pool)
  1985. {
  1986. u32 bm;
  1987. bm = cookie & ~(0xFF << MVPP2_BM_COOKIE_POOL_OFFS);
  1988. bm |= ((pool & 0xFF) << MVPP2_BM_COOKIE_POOL_OFFS);
  1989. return bm;
  1990. }
  1991. /* Get pool number from a BM cookie */
  1992. static inline int mvpp2_bm_cookie_pool_get(unsigned long cookie)
  1993. {
  1994. return (cookie >> MVPP2_BM_COOKIE_POOL_OFFS) & 0xFF;
  1995. }
  1996. /* Release buffer to BM */
  1997. static inline void mvpp2_bm_pool_put(struct mvpp2_port *port, int pool,
  1998. dma_addr_t buf_dma_addr,
  1999. unsigned long buf_phys_addr)
  2000. {
  2001. /* MVPP2_BM_VIRT_RLS_REG is not interpreted by HW, and simply
  2002. * returned in the "cookie" field of the RX
  2003. * descriptor. Instead of storing the virtual address, we
  2004. * store the physical address
  2005. */
  2006. mvpp2_write(port->priv, MVPP2_BM_VIRT_RLS_REG, buf_phys_addr);
  2007. mvpp2_write(port->priv, MVPP2_BM_PHY_RLS_REG(pool), buf_dma_addr);
  2008. }
  2009. /* Refill BM pool */
  2010. static void mvpp2_pool_refill(struct mvpp2_port *port, u32 bm,
  2011. dma_addr_t dma_addr,
  2012. phys_addr_t phys_addr)
  2013. {
  2014. int pool = mvpp2_bm_cookie_pool_get(bm);
  2015. mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
  2016. }
  2017. /* Allocate buffers for the pool */
  2018. static int mvpp2_bm_bufs_add(struct mvpp2_port *port,
  2019. struct mvpp2_bm_pool *bm_pool, int buf_num)
  2020. {
  2021. int i;
  2022. if (buf_num < 0 ||
  2023. (buf_num + bm_pool->buf_num > bm_pool->size)) {
  2024. netdev_err(port->dev,
  2025. "cannot allocate %d buffers for pool %d\n",
  2026. buf_num, bm_pool->id);
  2027. return 0;
  2028. }
  2029. for (i = 0; i < buf_num; i++) {
  2030. mvpp2_bm_pool_put(port, bm_pool->id,
  2031. (dma_addr_t)buffer_loc.rx_buffer[i],
  2032. (unsigned long)buffer_loc.rx_buffer[i]);
  2033. }
  2034. /* Update BM driver with number of buffers added to pool */
  2035. bm_pool->buf_num += i;
  2036. bm_pool->in_use_thresh = bm_pool->buf_num / 4;
  2037. return i;
  2038. }
  2039. /* Notify the driver that BM pool is being used as specific type and return the
  2040. * pool pointer on success
  2041. */
  2042. static struct mvpp2_bm_pool *
  2043. mvpp2_bm_pool_use(struct mvpp2_port *port, int pool, enum mvpp2_bm_type type,
  2044. int pkt_size)
  2045. {
  2046. struct mvpp2_bm_pool *new_pool = &port->priv->bm_pools[pool];
  2047. int num;
  2048. if (new_pool->type != MVPP2_BM_FREE && new_pool->type != type) {
  2049. netdev_err(port->dev, "mixing pool types is forbidden\n");
  2050. return NULL;
  2051. }
  2052. if (new_pool->type == MVPP2_BM_FREE)
  2053. new_pool->type = type;
  2054. /* Allocate buffers in case BM pool is used as long pool, but packet
  2055. * size doesn't match MTU or BM pool hasn't being used yet
  2056. */
  2057. if (((type == MVPP2_BM_SWF_LONG) && (pkt_size > new_pool->pkt_size)) ||
  2058. (new_pool->pkt_size == 0)) {
  2059. int pkts_num;
  2060. /* Set default buffer number or free all the buffers in case
  2061. * the pool is not empty
  2062. */
  2063. pkts_num = new_pool->buf_num;
  2064. if (pkts_num == 0)
  2065. pkts_num = type == MVPP2_BM_SWF_LONG ?
  2066. MVPP2_BM_LONG_BUF_NUM :
  2067. MVPP2_BM_SHORT_BUF_NUM;
  2068. else
  2069. mvpp2_bm_bufs_free(NULL,
  2070. port->priv, new_pool);
  2071. new_pool->pkt_size = pkt_size;
  2072. /* Allocate buffers for this pool */
  2073. num = mvpp2_bm_bufs_add(port, new_pool, pkts_num);
  2074. if (num != pkts_num) {
  2075. dev_err(dev, "pool %d: %d of %d allocated\n",
  2076. new_pool->id, num, pkts_num);
  2077. return NULL;
  2078. }
  2079. }
  2080. mvpp2_bm_pool_bufsize_set(port->priv, new_pool,
  2081. MVPP2_RX_BUF_SIZE(new_pool->pkt_size));
  2082. return new_pool;
  2083. }
  2084. /* Initialize pools for swf */
  2085. static int mvpp2_swf_bm_pool_init(struct mvpp2_port *port)
  2086. {
  2087. int rxq;
  2088. if (!port->pool_long) {
  2089. port->pool_long =
  2090. mvpp2_bm_pool_use(port, MVPP2_BM_SWF_LONG_POOL(port->id),
  2091. MVPP2_BM_SWF_LONG,
  2092. port->pkt_size);
  2093. if (!port->pool_long)
  2094. return -ENOMEM;
  2095. port->pool_long->port_map |= (1 << port->id);
  2096. for (rxq = 0; rxq < rxq_number; rxq++)
  2097. mvpp2_rxq_long_pool_set(port, rxq, port->pool_long->id);
  2098. }
  2099. return 0;
  2100. }
  2101. /* Port configuration routines */
  2102. static void mvpp2_port_mii_set(struct mvpp2_port *port)
  2103. {
  2104. u32 val;
  2105. val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
  2106. switch (port->phy_interface) {
  2107. case PHY_INTERFACE_MODE_SGMII:
  2108. val |= MVPP2_GMAC_INBAND_AN_MASK;
  2109. break;
  2110. case PHY_INTERFACE_MODE_RGMII:
  2111. val |= MVPP2_GMAC_PORT_RGMII_MASK;
  2112. default:
  2113. val &= ~MVPP2_GMAC_PCS_ENABLE_MASK;
  2114. }
  2115. writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
  2116. }
  2117. static void mvpp2_port_fc_adv_enable(struct mvpp2_port *port)
  2118. {
  2119. u32 val;
  2120. val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  2121. val |= MVPP2_GMAC_FC_ADV_EN;
  2122. writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  2123. }
  2124. static void mvpp2_port_enable(struct mvpp2_port *port)
  2125. {
  2126. u32 val;
  2127. val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
  2128. val |= MVPP2_GMAC_PORT_EN_MASK;
  2129. val |= MVPP2_GMAC_MIB_CNTR_EN_MASK;
  2130. writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
  2131. }
  2132. static void mvpp2_port_disable(struct mvpp2_port *port)
  2133. {
  2134. u32 val;
  2135. val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
  2136. val &= ~(MVPP2_GMAC_PORT_EN_MASK);
  2137. writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
  2138. }
  2139. /* Set IEEE 802.3x Flow Control Xon Packet Transmission Mode */
  2140. static void mvpp2_port_periodic_xon_disable(struct mvpp2_port *port)
  2141. {
  2142. u32 val;
  2143. val = readl(port->base + MVPP2_GMAC_CTRL_1_REG) &
  2144. ~MVPP2_GMAC_PERIODIC_XON_EN_MASK;
  2145. writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
  2146. }
  2147. /* Configure loopback port */
  2148. static void mvpp2_port_loopback_set(struct mvpp2_port *port)
  2149. {
  2150. u32 val;
  2151. val = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
  2152. if (port->speed == 1000)
  2153. val |= MVPP2_GMAC_GMII_LB_EN_MASK;
  2154. else
  2155. val &= ~MVPP2_GMAC_GMII_LB_EN_MASK;
  2156. if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
  2157. val |= MVPP2_GMAC_PCS_LB_EN_MASK;
  2158. else
  2159. val &= ~MVPP2_GMAC_PCS_LB_EN_MASK;
  2160. writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
  2161. }
  2162. static void mvpp2_port_reset(struct mvpp2_port *port)
  2163. {
  2164. u32 val;
  2165. val = readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
  2166. ~MVPP2_GMAC_PORT_RESET_MASK;
  2167. writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
  2168. while (readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
  2169. MVPP2_GMAC_PORT_RESET_MASK)
  2170. continue;
  2171. }
  2172. /* Change maximum receive size of the port */
  2173. static inline void mvpp2_gmac_max_rx_size_set(struct mvpp2_port *port)
  2174. {
  2175. u32 val;
  2176. val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
  2177. val &= ~MVPP2_GMAC_MAX_RX_SIZE_MASK;
  2178. val |= (((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
  2179. MVPP2_GMAC_MAX_RX_SIZE_OFFS);
  2180. writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
  2181. }
  2182. /* Set defaults to the MVPP2 port */
  2183. static void mvpp2_defaults_set(struct mvpp2_port *port)
  2184. {
  2185. int tx_port_num, val, queue, ptxq, lrxq;
  2186. /* Configure port to loopback if needed */
  2187. if (port->flags & MVPP2_F_LOOPBACK)
  2188. mvpp2_port_loopback_set(port);
  2189. /* Update TX FIFO MIN Threshold */
  2190. val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
  2191. val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
  2192. /* Min. TX threshold must be less than minimal packet length */
  2193. val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(64 - 4 - 2);
  2194. writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
  2195. /* Disable Legacy WRR, Disable EJP, Release from reset */
  2196. tx_port_num = mvpp2_egress_port(port);
  2197. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG,
  2198. tx_port_num);
  2199. mvpp2_write(port->priv, MVPP2_TXP_SCHED_CMD_1_REG, 0);
  2200. /* Close bandwidth for all queues */
  2201. for (queue = 0; queue < MVPP2_MAX_TXQ; queue++) {
  2202. ptxq = mvpp2_txq_phys(port->id, queue);
  2203. mvpp2_write(port->priv,
  2204. MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(ptxq), 0);
  2205. }
  2206. /* Set refill period to 1 usec, refill tokens
  2207. * and bucket size to maximum
  2208. */
  2209. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PERIOD_REG, 0xc8);
  2210. val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_REFILL_REG);
  2211. val &= ~MVPP2_TXP_REFILL_PERIOD_ALL_MASK;
  2212. val |= MVPP2_TXP_REFILL_PERIOD_MASK(1);
  2213. val |= MVPP2_TXP_REFILL_TOKENS_ALL_MASK;
  2214. mvpp2_write(port->priv, MVPP2_TXP_SCHED_REFILL_REG, val);
  2215. val = MVPP2_TXP_TOKEN_SIZE_MAX;
  2216. mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
  2217. /* Set MaximumLowLatencyPacketSize value to 256 */
  2218. mvpp2_write(port->priv, MVPP2_RX_CTRL_REG(port->id),
  2219. MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK |
  2220. MVPP2_RX_LOW_LATENCY_PKT_SIZE(256));
  2221. /* Enable Rx cache snoop */
  2222. for (lrxq = 0; lrxq < rxq_number; lrxq++) {
  2223. queue = port->rxqs[lrxq]->id;
  2224. val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
  2225. val |= MVPP2_SNOOP_PKT_SIZE_MASK |
  2226. MVPP2_SNOOP_BUF_HDR_MASK;
  2227. mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
  2228. }
  2229. }
  2230. /* Enable/disable receiving packets */
  2231. static void mvpp2_ingress_enable(struct mvpp2_port *port)
  2232. {
  2233. u32 val;
  2234. int lrxq, queue;
  2235. for (lrxq = 0; lrxq < rxq_number; lrxq++) {
  2236. queue = port->rxqs[lrxq]->id;
  2237. val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
  2238. val &= ~MVPP2_RXQ_DISABLE_MASK;
  2239. mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
  2240. }
  2241. }
  2242. static void mvpp2_ingress_disable(struct mvpp2_port *port)
  2243. {
  2244. u32 val;
  2245. int lrxq, queue;
  2246. for (lrxq = 0; lrxq < rxq_number; lrxq++) {
  2247. queue = port->rxqs[lrxq]->id;
  2248. val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
  2249. val |= MVPP2_RXQ_DISABLE_MASK;
  2250. mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
  2251. }
  2252. }
  2253. /* Enable transmit via physical egress queue
  2254. * - HW starts take descriptors from DRAM
  2255. */
  2256. static void mvpp2_egress_enable(struct mvpp2_port *port)
  2257. {
  2258. u32 qmap;
  2259. int queue;
  2260. int tx_port_num = mvpp2_egress_port(port);
  2261. /* Enable all initialized TXs. */
  2262. qmap = 0;
  2263. for (queue = 0; queue < txq_number; queue++) {
  2264. struct mvpp2_tx_queue *txq = port->txqs[queue];
  2265. if (txq->descs != NULL)
  2266. qmap |= (1 << queue);
  2267. }
  2268. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
  2269. mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG, qmap);
  2270. }
  2271. /* Disable transmit via physical egress queue
  2272. * - HW doesn't take descriptors from DRAM
  2273. */
  2274. static void mvpp2_egress_disable(struct mvpp2_port *port)
  2275. {
  2276. u32 reg_data;
  2277. int delay;
  2278. int tx_port_num = mvpp2_egress_port(port);
  2279. /* Issue stop command for active channels only */
  2280. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
  2281. reg_data = (mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG)) &
  2282. MVPP2_TXP_SCHED_ENQ_MASK;
  2283. if (reg_data != 0)
  2284. mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG,
  2285. (reg_data << MVPP2_TXP_SCHED_DISQ_OFFSET));
  2286. /* Wait for all Tx activity to terminate. */
  2287. delay = 0;
  2288. do {
  2289. if (delay >= MVPP2_TX_DISABLE_TIMEOUT_MSEC) {
  2290. netdev_warn(port->dev,
  2291. "Tx stop timed out, status=0x%08x\n",
  2292. reg_data);
  2293. break;
  2294. }
  2295. mdelay(1);
  2296. delay++;
  2297. /* Check port TX Command register that all
  2298. * Tx queues are stopped
  2299. */
  2300. reg_data = mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG);
  2301. } while (reg_data & MVPP2_TXP_SCHED_ENQ_MASK);
  2302. }
  2303. /* Rx descriptors helper methods */
  2304. /* Get number of Rx descriptors occupied by received packets */
  2305. static inline int
  2306. mvpp2_rxq_received(struct mvpp2_port *port, int rxq_id)
  2307. {
  2308. u32 val = mvpp2_read(port->priv, MVPP2_RXQ_STATUS_REG(rxq_id));
  2309. return val & MVPP2_RXQ_OCCUPIED_MASK;
  2310. }
  2311. /* Update Rx queue status with the number of occupied and available
  2312. * Rx descriptor slots.
  2313. */
  2314. static inline void
  2315. mvpp2_rxq_status_update(struct mvpp2_port *port, int rxq_id,
  2316. int used_count, int free_count)
  2317. {
  2318. /* Decrement the number of used descriptors and increment count
  2319. * increment the number of free descriptors.
  2320. */
  2321. u32 val = used_count | (free_count << MVPP2_RXQ_NUM_NEW_OFFSET);
  2322. mvpp2_write(port->priv, MVPP2_RXQ_STATUS_UPDATE_REG(rxq_id), val);
  2323. }
  2324. /* Get pointer to next RX descriptor to be processed by SW */
  2325. static inline struct mvpp2_rx_desc *
  2326. mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue *rxq)
  2327. {
  2328. int rx_desc = rxq->next_desc_to_proc;
  2329. rxq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(rxq, rx_desc);
  2330. prefetch(rxq->descs + rxq->next_desc_to_proc);
  2331. return rxq->descs + rx_desc;
  2332. }
  2333. /* Set rx queue offset */
  2334. static void mvpp2_rxq_offset_set(struct mvpp2_port *port,
  2335. int prxq, int offset)
  2336. {
  2337. u32 val;
  2338. /* Convert offset from bytes to units of 32 bytes */
  2339. offset = offset >> 5;
  2340. val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
  2341. val &= ~MVPP2_RXQ_PACKET_OFFSET_MASK;
  2342. /* Offset is in */
  2343. val |= ((offset << MVPP2_RXQ_PACKET_OFFSET_OFFS) &
  2344. MVPP2_RXQ_PACKET_OFFSET_MASK);
  2345. mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
  2346. }
  2347. /* Obtain BM cookie information from descriptor */
  2348. static u32 mvpp2_bm_cookie_build(struct mvpp2_port *port,
  2349. struct mvpp2_rx_desc *rx_desc)
  2350. {
  2351. int cpu = smp_processor_id();
  2352. int pool;
  2353. pool = (mvpp2_rxdesc_status_get(port, rx_desc) &
  2354. MVPP2_RXD_BM_POOL_ID_MASK) >>
  2355. MVPP2_RXD_BM_POOL_ID_OFFS;
  2356. return ((pool & 0xFF) << MVPP2_BM_COOKIE_POOL_OFFS) |
  2357. ((cpu & 0xFF) << MVPP2_BM_COOKIE_CPU_OFFS);
  2358. }
  2359. /* Tx descriptors helper methods */
  2360. /* Get number of Tx descriptors waiting to be transmitted by HW */
  2361. static int mvpp2_txq_pend_desc_num_get(struct mvpp2_port *port,
  2362. struct mvpp2_tx_queue *txq)
  2363. {
  2364. u32 val;
  2365. mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
  2366. val = mvpp2_read(port->priv, MVPP2_TXQ_PENDING_REG);
  2367. return val & MVPP2_TXQ_PENDING_MASK;
  2368. }
  2369. /* Get pointer to next Tx descriptor to be processed (send) by HW */
  2370. static struct mvpp2_tx_desc *
  2371. mvpp2_txq_next_desc_get(struct mvpp2_tx_queue *txq)
  2372. {
  2373. int tx_desc = txq->next_desc_to_proc;
  2374. txq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(txq, tx_desc);
  2375. return txq->descs + tx_desc;
  2376. }
  2377. /* Update HW with number of aggregated Tx descriptors to be sent */
  2378. static void mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port *port, int pending)
  2379. {
  2380. /* aggregated access - relevant TXQ number is written in TX desc */
  2381. mvpp2_write(port->priv, MVPP2_AGGR_TXQ_UPDATE_REG, pending);
  2382. }
  2383. /* Get number of sent descriptors and decrement counter.
  2384. * The number of sent descriptors is returned.
  2385. * Per-CPU access
  2386. */
  2387. static inline int mvpp2_txq_sent_desc_proc(struct mvpp2_port *port,
  2388. struct mvpp2_tx_queue *txq)
  2389. {
  2390. u32 val;
  2391. /* Reading status reg resets transmitted descriptor counter */
  2392. val = mvpp2_read(port->priv, MVPP2_TXQ_SENT_REG(txq->id));
  2393. return (val & MVPP2_TRANSMITTED_COUNT_MASK) >>
  2394. MVPP2_TRANSMITTED_COUNT_OFFSET;
  2395. }
  2396. static void mvpp2_txq_sent_counter_clear(void *arg)
  2397. {
  2398. struct mvpp2_port *port = arg;
  2399. int queue;
  2400. for (queue = 0; queue < txq_number; queue++) {
  2401. int id = port->txqs[queue]->id;
  2402. mvpp2_read(port->priv, MVPP2_TXQ_SENT_REG(id));
  2403. }
  2404. }
  2405. /* Set max sizes for Tx queues */
  2406. static void mvpp2_txp_max_tx_size_set(struct mvpp2_port *port)
  2407. {
  2408. u32 val, size, mtu;
  2409. int txq, tx_port_num;
  2410. mtu = port->pkt_size * 8;
  2411. if (mtu > MVPP2_TXP_MTU_MAX)
  2412. mtu = MVPP2_TXP_MTU_MAX;
  2413. /* WA for wrong Token bucket update: Set MTU value = 3*real MTU value */
  2414. mtu = 3 * mtu;
  2415. /* Indirect access to registers */
  2416. tx_port_num = mvpp2_egress_port(port);
  2417. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
  2418. /* Set MTU */
  2419. val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_MTU_REG);
  2420. val &= ~MVPP2_TXP_MTU_MAX;
  2421. val |= mtu;
  2422. mvpp2_write(port->priv, MVPP2_TXP_SCHED_MTU_REG, val);
  2423. /* TXP token size and all TXQs token size must be larger that MTU */
  2424. val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG);
  2425. size = val & MVPP2_TXP_TOKEN_SIZE_MAX;
  2426. if (size < mtu) {
  2427. size = mtu;
  2428. val &= ~MVPP2_TXP_TOKEN_SIZE_MAX;
  2429. val |= size;
  2430. mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
  2431. }
  2432. for (txq = 0; txq < txq_number; txq++) {
  2433. val = mvpp2_read(port->priv,
  2434. MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq));
  2435. size = val & MVPP2_TXQ_TOKEN_SIZE_MAX;
  2436. if (size < mtu) {
  2437. size = mtu;
  2438. val &= ~MVPP2_TXQ_TOKEN_SIZE_MAX;
  2439. val |= size;
  2440. mvpp2_write(port->priv,
  2441. MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq),
  2442. val);
  2443. }
  2444. }
  2445. }
  2446. /* Free Tx queue skbuffs */
  2447. static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
  2448. struct mvpp2_tx_queue *txq,
  2449. struct mvpp2_txq_pcpu *txq_pcpu, int num)
  2450. {
  2451. int i;
  2452. for (i = 0; i < num; i++)
  2453. mvpp2_txq_inc_get(txq_pcpu);
  2454. }
  2455. static inline struct mvpp2_rx_queue *mvpp2_get_rx_queue(struct mvpp2_port *port,
  2456. u32 cause)
  2457. {
  2458. int queue = fls(cause) - 1;
  2459. return port->rxqs[queue];
  2460. }
  2461. static inline struct mvpp2_tx_queue *mvpp2_get_tx_queue(struct mvpp2_port *port,
  2462. u32 cause)
  2463. {
  2464. int queue = fls(cause) - 1;
  2465. return port->txqs[queue];
  2466. }
  2467. /* Rx/Tx queue initialization/cleanup methods */
  2468. /* Allocate and initialize descriptors for aggr TXQ */
  2469. static int mvpp2_aggr_txq_init(struct udevice *dev,
  2470. struct mvpp2_tx_queue *aggr_txq,
  2471. int desc_num, int cpu,
  2472. struct mvpp2 *priv)
  2473. {
  2474. /* Allocate memory for TX descriptors */
  2475. aggr_txq->descs = buffer_loc.aggr_tx_descs;
  2476. aggr_txq->descs_dma = (dma_addr_t)buffer_loc.aggr_tx_descs;
  2477. if (!aggr_txq->descs)
  2478. return -ENOMEM;
  2479. /* Make sure descriptor address is cache line size aligned */
  2480. BUG_ON(aggr_txq->descs !=
  2481. PTR_ALIGN(aggr_txq->descs, MVPP2_CPU_D_CACHE_LINE_SIZE));
  2482. aggr_txq->last_desc = aggr_txq->size - 1;
  2483. /* Aggr TXQ no reset WA */
  2484. aggr_txq->next_desc_to_proc = mvpp2_read(priv,
  2485. MVPP2_AGGR_TXQ_INDEX_REG(cpu));
  2486. /* Set Tx descriptors queue starting address */
  2487. /* indirect access */
  2488. mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu),
  2489. aggr_txq->descs_dma);
  2490. mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu), desc_num);
  2491. return 0;
  2492. }
  2493. /* Create a specified Rx queue */
  2494. static int mvpp2_rxq_init(struct mvpp2_port *port,
  2495. struct mvpp2_rx_queue *rxq)
  2496. {
  2497. rxq->size = port->rx_ring_size;
  2498. /* Allocate memory for RX descriptors */
  2499. rxq->descs = buffer_loc.rx_descs;
  2500. rxq->descs_dma = (dma_addr_t)buffer_loc.rx_descs;
  2501. if (!rxq->descs)
  2502. return -ENOMEM;
  2503. BUG_ON(rxq->descs !=
  2504. PTR_ALIGN(rxq->descs, MVPP2_CPU_D_CACHE_LINE_SIZE));
  2505. rxq->last_desc = rxq->size - 1;
  2506. /* Zero occupied and non-occupied counters - direct access */
  2507. mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
  2508. /* Set Rx descriptors queue starting address - indirect access */
  2509. mvpp2_write(port->priv, MVPP2_RXQ_NUM_REG, rxq->id);
  2510. mvpp2_write(port->priv, MVPP2_RXQ_DESC_ADDR_REG, rxq->descs_dma);
  2511. mvpp2_write(port->priv, MVPP2_RXQ_DESC_SIZE_REG, rxq->size);
  2512. mvpp2_write(port->priv, MVPP2_RXQ_INDEX_REG, 0);
  2513. /* Set Offset */
  2514. mvpp2_rxq_offset_set(port, rxq->id, NET_SKB_PAD);
  2515. /* Add number of descriptors ready for receiving packets */
  2516. mvpp2_rxq_status_update(port, rxq->id, 0, rxq->size);
  2517. return 0;
  2518. }
  2519. /* Push packets received by the RXQ to BM pool */
  2520. static void mvpp2_rxq_drop_pkts(struct mvpp2_port *port,
  2521. struct mvpp2_rx_queue *rxq)
  2522. {
  2523. int rx_received, i;
  2524. rx_received = mvpp2_rxq_received(port, rxq->id);
  2525. if (!rx_received)
  2526. return;
  2527. for (i = 0; i < rx_received; i++) {
  2528. struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
  2529. u32 bm = mvpp2_bm_cookie_build(port, rx_desc);
  2530. mvpp2_pool_refill(port, bm,
  2531. mvpp2_rxdesc_dma_addr_get(port, rx_desc),
  2532. mvpp2_rxdesc_cookie_get(port, rx_desc));
  2533. }
  2534. mvpp2_rxq_status_update(port, rxq->id, rx_received, rx_received);
  2535. }
  2536. /* Cleanup Rx queue */
  2537. static void mvpp2_rxq_deinit(struct mvpp2_port *port,
  2538. struct mvpp2_rx_queue *rxq)
  2539. {
  2540. mvpp2_rxq_drop_pkts(port, rxq);
  2541. rxq->descs = NULL;
  2542. rxq->last_desc = 0;
  2543. rxq->next_desc_to_proc = 0;
  2544. rxq->descs_dma = 0;
  2545. /* Clear Rx descriptors queue starting address and size;
  2546. * free descriptor number
  2547. */
  2548. mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
  2549. mvpp2_write(port->priv, MVPP2_RXQ_NUM_REG, rxq->id);
  2550. mvpp2_write(port->priv, MVPP2_RXQ_DESC_ADDR_REG, 0);
  2551. mvpp2_write(port->priv, MVPP2_RXQ_DESC_SIZE_REG, 0);
  2552. }
  2553. /* Create and initialize a Tx queue */
  2554. static int mvpp2_txq_init(struct mvpp2_port *port,
  2555. struct mvpp2_tx_queue *txq)
  2556. {
  2557. u32 val;
  2558. int cpu, desc, desc_per_txq, tx_port_num;
  2559. struct mvpp2_txq_pcpu *txq_pcpu;
  2560. txq->size = port->tx_ring_size;
  2561. /* Allocate memory for Tx descriptors */
  2562. txq->descs = buffer_loc.tx_descs;
  2563. txq->descs_dma = (dma_addr_t)buffer_loc.tx_descs;
  2564. if (!txq->descs)
  2565. return -ENOMEM;
  2566. /* Make sure descriptor address is cache line size aligned */
  2567. BUG_ON(txq->descs !=
  2568. PTR_ALIGN(txq->descs, MVPP2_CPU_D_CACHE_LINE_SIZE));
  2569. txq->last_desc = txq->size - 1;
  2570. /* Set Tx descriptors queue starting address - indirect access */
  2571. mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
  2572. mvpp2_write(port->priv, MVPP2_TXQ_DESC_ADDR_REG, txq->descs_dma);
  2573. mvpp2_write(port->priv, MVPP2_TXQ_DESC_SIZE_REG, txq->size &
  2574. MVPP2_TXQ_DESC_SIZE_MASK);
  2575. mvpp2_write(port->priv, MVPP2_TXQ_INDEX_REG, 0);
  2576. mvpp2_write(port->priv, MVPP2_TXQ_RSVD_CLR_REG,
  2577. txq->id << MVPP2_TXQ_RSVD_CLR_OFFSET);
  2578. val = mvpp2_read(port->priv, MVPP2_TXQ_PENDING_REG);
  2579. val &= ~MVPP2_TXQ_PENDING_MASK;
  2580. mvpp2_write(port->priv, MVPP2_TXQ_PENDING_REG, val);
  2581. /* Calculate base address in prefetch buffer. We reserve 16 descriptors
  2582. * for each existing TXQ.
  2583. * TCONTS for PON port must be continuous from 0 to MVPP2_MAX_TCONT
  2584. * GBE ports assumed to be continious from 0 to MVPP2_MAX_PORTS
  2585. */
  2586. desc_per_txq = 16;
  2587. desc = (port->id * MVPP2_MAX_TXQ * desc_per_txq) +
  2588. (txq->log_id * desc_per_txq);
  2589. mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG,
  2590. MVPP2_PREF_BUF_PTR(desc) | MVPP2_PREF_BUF_SIZE_16 |
  2591. MVPP2_PREF_BUF_THRESH(desc_per_txq/2));
  2592. /* WRR / EJP configuration - indirect access */
  2593. tx_port_num = mvpp2_egress_port(port);
  2594. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
  2595. val = mvpp2_read(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id));
  2596. val &= ~MVPP2_TXQ_REFILL_PERIOD_ALL_MASK;
  2597. val |= MVPP2_TXQ_REFILL_PERIOD_MASK(1);
  2598. val |= MVPP2_TXQ_REFILL_TOKENS_ALL_MASK;
  2599. mvpp2_write(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id), val);
  2600. val = MVPP2_TXQ_TOKEN_SIZE_MAX;
  2601. mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq->log_id),
  2602. val);
  2603. for_each_present_cpu(cpu) {
  2604. txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
  2605. txq_pcpu->size = txq->size;
  2606. }
  2607. return 0;
  2608. }
  2609. /* Free allocated TXQ resources */
  2610. static void mvpp2_txq_deinit(struct mvpp2_port *port,
  2611. struct mvpp2_tx_queue *txq)
  2612. {
  2613. txq->descs = NULL;
  2614. txq->last_desc = 0;
  2615. txq->next_desc_to_proc = 0;
  2616. txq->descs_dma = 0;
  2617. /* Set minimum bandwidth for disabled TXQs */
  2618. mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(txq->id), 0);
  2619. /* Set Tx descriptors queue starting address and size */
  2620. mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
  2621. mvpp2_write(port->priv, MVPP2_TXQ_DESC_ADDR_REG, 0);
  2622. mvpp2_write(port->priv, MVPP2_TXQ_DESC_SIZE_REG, 0);
  2623. }
  2624. /* Cleanup Tx ports */
  2625. static void mvpp2_txq_clean(struct mvpp2_port *port, struct mvpp2_tx_queue *txq)
  2626. {
  2627. struct mvpp2_txq_pcpu *txq_pcpu;
  2628. int delay, pending, cpu;
  2629. u32 val;
  2630. mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
  2631. val = mvpp2_read(port->priv, MVPP2_TXQ_PREF_BUF_REG);
  2632. val |= MVPP2_TXQ_DRAIN_EN_MASK;
  2633. mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG, val);
  2634. /* The napi queue has been stopped so wait for all packets
  2635. * to be transmitted.
  2636. */
  2637. delay = 0;
  2638. do {
  2639. if (delay >= MVPP2_TX_PENDING_TIMEOUT_MSEC) {
  2640. netdev_warn(port->dev,
  2641. "port %d: cleaning queue %d timed out\n",
  2642. port->id, txq->log_id);
  2643. break;
  2644. }
  2645. mdelay(1);
  2646. delay++;
  2647. pending = mvpp2_txq_pend_desc_num_get(port, txq);
  2648. } while (pending);
  2649. val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
  2650. mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG, val);
  2651. for_each_present_cpu(cpu) {
  2652. txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
  2653. /* Release all packets */
  2654. mvpp2_txq_bufs_free(port, txq, txq_pcpu, txq_pcpu->count);
  2655. /* Reset queue */
  2656. txq_pcpu->count = 0;
  2657. txq_pcpu->txq_put_index = 0;
  2658. txq_pcpu->txq_get_index = 0;
  2659. }
  2660. }
  2661. /* Cleanup all Tx queues */
  2662. static void mvpp2_cleanup_txqs(struct mvpp2_port *port)
  2663. {
  2664. struct mvpp2_tx_queue *txq;
  2665. int queue;
  2666. u32 val;
  2667. val = mvpp2_read(port->priv, MVPP2_TX_PORT_FLUSH_REG);
  2668. /* Reset Tx ports and delete Tx queues */
  2669. val |= MVPP2_TX_PORT_FLUSH_MASK(port->id);
  2670. mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
  2671. for (queue = 0; queue < txq_number; queue++) {
  2672. txq = port->txqs[queue];
  2673. mvpp2_txq_clean(port, txq);
  2674. mvpp2_txq_deinit(port, txq);
  2675. }
  2676. mvpp2_txq_sent_counter_clear(port);
  2677. val &= ~MVPP2_TX_PORT_FLUSH_MASK(port->id);
  2678. mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
  2679. }
  2680. /* Cleanup all Rx queues */
  2681. static void mvpp2_cleanup_rxqs(struct mvpp2_port *port)
  2682. {
  2683. int queue;
  2684. for (queue = 0; queue < rxq_number; queue++)
  2685. mvpp2_rxq_deinit(port, port->rxqs[queue]);
  2686. }
  2687. /* Init all Rx queues for port */
  2688. static int mvpp2_setup_rxqs(struct mvpp2_port *port)
  2689. {
  2690. int queue, err;
  2691. for (queue = 0; queue < rxq_number; queue++) {
  2692. err = mvpp2_rxq_init(port, port->rxqs[queue]);
  2693. if (err)
  2694. goto err_cleanup;
  2695. }
  2696. return 0;
  2697. err_cleanup:
  2698. mvpp2_cleanup_rxqs(port);
  2699. return err;
  2700. }
  2701. /* Init all tx queues for port */
  2702. static int mvpp2_setup_txqs(struct mvpp2_port *port)
  2703. {
  2704. struct mvpp2_tx_queue *txq;
  2705. int queue, err;
  2706. for (queue = 0; queue < txq_number; queue++) {
  2707. txq = port->txqs[queue];
  2708. err = mvpp2_txq_init(port, txq);
  2709. if (err)
  2710. goto err_cleanup;
  2711. }
  2712. mvpp2_txq_sent_counter_clear(port);
  2713. return 0;
  2714. err_cleanup:
  2715. mvpp2_cleanup_txqs(port);
  2716. return err;
  2717. }
  2718. /* Adjust link */
  2719. static void mvpp2_link_event(struct mvpp2_port *port)
  2720. {
  2721. struct phy_device *phydev = port->phy_dev;
  2722. int status_change = 0;
  2723. u32 val;
  2724. if (phydev->link) {
  2725. if ((port->speed != phydev->speed) ||
  2726. (port->duplex != phydev->duplex)) {
  2727. u32 val;
  2728. val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  2729. val &= ~(MVPP2_GMAC_CONFIG_MII_SPEED |
  2730. MVPP2_GMAC_CONFIG_GMII_SPEED |
  2731. MVPP2_GMAC_CONFIG_FULL_DUPLEX |
  2732. MVPP2_GMAC_AN_SPEED_EN |
  2733. MVPP2_GMAC_AN_DUPLEX_EN);
  2734. if (phydev->duplex)
  2735. val |= MVPP2_GMAC_CONFIG_FULL_DUPLEX;
  2736. if (phydev->speed == SPEED_1000)
  2737. val |= MVPP2_GMAC_CONFIG_GMII_SPEED;
  2738. else if (phydev->speed == SPEED_100)
  2739. val |= MVPP2_GMAC_CONFIG_MII_SPEED;
  2740. writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  2741. port->duplex = phydev->duplex;
  2742. port->speed = phydev->speed;
  2743. }
  2744. }
  2745. if (phydev->link != port->link) {
  2746. if (!phydev->link) {
  2747. port->duplex = -1;
  2748. port->speed = 0;
  2749. }
  2750. port->link = phydev->link;
  2751. status_change = 1;
  2752. }
  2753. if (status_change) {
  2754. if (phydev->link) {
  2755. val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  2756. val |= (MVPP2_GMAC_FORCE_LINK_PASS |
  2757. MVPP2_GMAC_FORCE_LINK_DOWN);
  2758. writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  2759. mvpp2_egress_enable(port);
  2760. mvpp2_ingress_enable(port);
  2761. } else {
  2762. mvpp2_ingress_disable(port);
  2763. mvpp2_egress_disable(port);
  2764. }
  2765. }
  2766. }
  2767. /* Main RX/TX processing routines */
  2768. /* Display more error info */
  2769. static void mvpp2_rx_error(struct mvpp2_port *port,
  2770. struct mvpp2_rx_desc *rx_desc)
  2771. {
  2772. u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
  2773. size_t sz = mvpp2_rxdesc_size_get(port, rx_desc);
  2774. switch (status & MVPP2_RXD_ERR_CODE_MASK) {
  2775. case MVPP2_RXD_ERR_CRC:
  2776. netdev_err(port->dev, "bad rx status %08x (crc error), size=%zu\n",
  2777. status, sz);
  2778. break;
  2779. case MVPP2_RXD_ERR_OVERRUN:
  2780. netdev_err(port->dev, "bad rx status %08x (overrun error), size=%zu\n",
  2781. status, sz);
  2782. break;
  2783. case MVPP2_RXD_ERR_RESOURCE:
  2784. netdev_err(port->dev, "bad rx status %08x (resource error), size=%zu\n",
  2785. status, sz);
  2786. break;
  2787. }
  2788. }
  2789. /* Reuse skb if possible, or allocate a new skb and add it to BM pool */
  2790. static int mvpp2_rx_refill(struct mvpp2_port *port,
  2791. struct mvpp2_bm_pool *bm_pool,
  2792. u32 bm, dma_addr_t dma_addr)
  2793. {
  2794. mvpp2_pool_refill(port, bm, dma_addr, (unsigned long)dma_addr);
  2795. return 0;
  2796. }
  2797. /* Set hw internals when starting port */
  2798. static void mvpp2_start_dev(struct mvpp2_port *port)
  2799. {
  2800. mvpp2_gmac_max_rx_size_set(port);
  2801. mvpp2_txp_max_tx_size_set(port);
  2802. mvpp2_port_enable(port);
  2803. }
  2804. /* Set hw internals when stopping port */
  2805. static void mvpp2_stop_dev(struct mvpp2_port *port)
  2806. {
  2807. /* Stop new packets from arriving to RXQs */
  2808. mvpp2_ingress_disable(port);
  2809. mvpp2_egress_disable(port);
  2810. mvpp2_port_disable(port);
  2811. }
  2812. static int mvpp2_phy_connect(struct udevice *dev, struct mvpp2_port *port)
  2813. {
  2814. struct phy_device *phy_dev;
  2815. if (!port->init || port->link == 0) {
  2816. phy_dev = phy_connect(port->priv->bus, port->phyaddr, dev,
  2817. port->phy_interface);
  2818. port->phy_dev = phy_dev;
  2819. if (!phy_dev) {
  2820. netdev_err(port->dev, "cannot connect to phy\n");
  2821. return -ENODEV;
  2822. }
  2823. phy_dev->supported &= PHY_GBIT_FEATURES;
  2824. phy_dev->advertising = phy_dev->supported;
  2825. port->phy_dev = phy_dev;
  2826. port->link = 0;
  2827. port->duplex = 0;
  2828. port->speed = 0;
  2829. phy_config(phy_dev);
  2830. phy_startup(phy_dev);
  2831. if (!phy_dev->link) {
  2832. printf("%s: No link\n", phy_dev->dev->name);
  2833. return -1;
  2834. }
  2835. port->init = 1;
  2836. } else {
  2837. mvpp2_egress_enable(port);
  2838. mvpp2_ingress_enable(port);
  2839. }
  2840. return 0;
  2841. }
  2842. static int mvpp2_open(struct udevice *dev, struct mvpp2_port *port)
  2843. {
  2844. unsigned char mac_bcast[ETH_ALEN] = {
  2845. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  2846. int err;
  2847. err = mvpp2_prs_mac_da_accept(port->priv, port->id, mac_bcast, true);
  2848. if (err) {
  2849. netdev_err(dev, "mvpp2_prs_mac_da_accept BC failed\n");
  2850. return err;
  2851. }
  2852. err = mvpp2_prs_mac_da_accept(port->priv, port->id,
  2853. port->dev_addr, true);
  2854. if (err) {
  2855. netdev_err(dev, "mvpp2_prs_mac_da_accept MC failed\n");
  2856. return err;
  2857. }
  2858. err = mvpp2_prs_def_flow(port);
  2859. if (err) {
  2860. netdev_err(dev, "mvpp2_prs_def_flow failed\n");
  2861. return err;
  2862. }
  2863. /* Allocate the Rx/Tx queues */
  2864. err = mvpp2_setup_rxqs(port);
  2865. if (err) {
  2866. netdev_err(port->dev, "cannot allocate Rx queues\n");
  2867. return err;
  2868. }
  2869. err = mvpp2_setup_txqs(port);
  2870. if (err) {
  2871. netdev_err(port->dev, "cannot allocate Tx queues\n");
  2872. return err;
  2873. }
  2874. err = mvpp2_phy_connect(dev, port);
  2875. if (err < 0)
  2876. return err;
  2877. mvpp2_link_event(port);
  2878. mvpp2_start_dev(port);
  2879. return 0;
  2880. }
  2881. /* No Device ops here in U-Boot */
  2882. /* Driver initialization */
  2883. static void mvpp2_port_power_up(struct mvpp2_port *port)
  2884. {
  2885. mvpp2_port_mii_set(port);
  2886. mvpp2_port_periodic_xon_disable(port);
  2887. mvpp2_port_fc_adv_enable(port);
  2888. mvpp2_port_reset(port);
  2889. }
  2890. /* Initialize port HW */
  2891. static int mvpp2_port_init(struct udevice *dev, struct mvpp2_port *port)
  2892. {
  2893. struct mvpp2 *priv = port->priv;
  2894. struct mvpp2_txq_pcpu *txq_pcpu;
  2895. int queue, cpu, err;
  2896. if (port->first_rxq + rxq_number > MVPP2_RXQ_TOTAL_NUM)
  2897. return -EINVAL;
  2898. /* Disable port */
  2899. mvpp2_egress_disable(port);
  2900. mvpp2_port_disable(port);
  2901. port->txqs = devm_kcalloc(dev, txq_number, sizeof(*port->txqs),
  2902. GFP_KERNEL);
  2903. if (!port->txqs)
  2904. return -ENOMEM;
  2905. /* Associate physical Tx queues to this port and initialize.
  2906. * The mapping is predefined.
  2907. */
  2908. for (queue = 0; queue < txq_number; queue++) {
  2909. int queue_phy_id = mvpp2_txq_phys(port->id, queue);
  2910. struct mvpp2_tx_queue *txq;
  2911. txq = devm_kzalloc(dev, sizeof(*txq), GFP_KERNEL);
  2912. if (!txq)
  2913. return -ENOMEM;
  2914. txq->pcpu = devm_kzalloc(dev, sizeof(struct mvpp2_txq_pcpu),
  2915. GFP_KERNEL);
  2916. if (!txq->pcpu)
  2917. return -ENOMEM;
  2918. txq->id = queue_phy_id;
  2919. txq->log_id = queue;
  2920. txq->done_pkts_coal = MVPP2_TXDONE_COAL_PKTS_THRESH;
  2921. for_each_present_cpu(cpu) {
  2922. txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
  2923. txq_pcpu->cpu = cpu;
  2924. }
  2925. port->txqs[queue] = txq;
  2926. }
  2927. port->rxqs = devm_kcalloc(dev, rxq_number, sizeof(*port->rxqs),
  2928. GFP_KERNEL);
  2929. if (!port->rxqs)
  2930. return -ENOMEM;
  2931. /* Allocate and initialize Rx queue for this port */
  2932. for (queue = 0; queue < rxq_number; queue++) {
  2933. struct mvpp2_rx_queue *rxq;
  2934. /* Map physical Rx queue to port's logical Rx queue */
  2935. rxq = devm_kzalloc(dev, sizeof(*rxq), GFP_KERNEL);
  2936. if (!rxq)
  2937. return -ENOMEM;
  2938. /* Map this Rx queue to a physical queue */
  2939. rxq->id = port->first_rxq + queue;
  2940. rxq->port = port->id;
  2941. rxq->logic_rxq = queue;
  2942. port->rxqs[queue] = rxq;
  2943. }
  2944. /* Configure Rx queue group interrupt for this port */
  2945. mvpp2_write(priv, MVPP2_ISR_RXQ_GROUP_REG(port->id), CONFIG_MV_ETH_RXQ);
  2946. /* Create Rx descriptor rings */
  2947. for (queue = 0; queue < rxq_number; queue++) {
  2948. struct mvpp2_rx_queue *rxq = port->rxqs[queue];
  2949. rxq->size = port->rx_ring_size;
  2950. rxq->pkts_coal = MVPP2_RX_COAL_PKTS;
  2951. rxq->time_coal = MVPP2_RX_COAL_USEC;
  2952. }
  2953. mvpp2_ingress_disable(port);
  2954. /* Port default configuration */
  2955. mvpp2_defaults_set(port);
  2956. /* Port's classifier configuration */
  2957. mvpp2_cls_oversize_rxq_set(port);
  2958. mvpp2_cls_port_config(port);
  2959. /* Provide an initial Rx packet size */
  2960. port->pkt_size = MVPP2_RX_PKT_SIZE(PKTSIZE_ALIGN);
  2961. /* Initialize pools for swf */
  2962. err = mvpp2_swf_bm_pool_init(port);
  2963. if (err)
  2964. return err;
  2965. return 0;
  2966. }
  2967. /* Ports initialization */
  2968. static int mvpp2_port_probe(struct udevice *dev,
  2969. struct mvpp2_port *port,
  2970. int port_node,
  2971. struct mvpp2 *priv,
  2972. int *next_first_rxq)
  2973. {
  2974. int phy_node;
  2975. u32 id;
  2976. u32 phyaddr;
  2977. const char *phy_mode_str;
  2978. int phy_mode = -1;
  2979. int priv_common_regs_num = 2;
  2980. int err;
  2981. phy_node = fdtdec_lookup_phandle(gd->fdt_blob, port_node, "phy");
  2982. if (phy_node < 0) {
  2983. dev_err(&pdev->dev, "missing phy\n");
  2984. return -ENODEV;
  2985. }
  2986. phy_mode_str = fdt_getprop(gd->fdt_blob, port_node, "phy-mode", NULL);
  2987. if (phy_mode_str)
  2988. phy_mode = phy_get_interface_by_name(phy_mode_str);
  2989. if (phy_mode == -1) {
  2990. dev_err(&pdev->dev, "incorrect phy mode\n");
  2991. return -EINVAL;
  2992. }
  2993. id = fdtdec_get_int(gd->fdt_blob, port_node, "port-id", -1);
  2994. if (id == -1) {
  2995. dev_err(&pdev->dev, "missing port-id value\n");
  2996. return -EINVAL;
  2997. }
  2998. phyaddr = fdtdec_get_int(gd->fdt_blob, phy_node, "reg", 0);
  2999. port->priv = priv;
  3000. port->id = id;
  3001. port->first_rxq = *next_first_rxq;
  3002. port->phy_node = phy_node;
  3003. port->phy_interface = phy_mode;
  3004. port->phyaddr = phyaddr;
  3005. port->base = (void __iomem *)dev_get_addr_index(dev->parent,
  3006. priv_common_regs_num
  3007. + id);
  3008. if (IS_ERR(port->base))
  3009. return PTR_ERR(port->base);
  3010. port->tx_ring_size = MVPP2_MAX_TXD;
  3011. port->rx_ring_size = MVPP2_MAX_RXD;
  3012. err = mvpp2_port_init(dev, port);
  3013. if (err < 0) {
  3014. dev_err(&pdev->dev, "failed to init port %d\n", id);
  3015. return err;
  3016. }
  3017. mvpp2_port_power_up(port);
  3018. /* Increment the first Rx queue number to be used by the next port */
  3019. *next_first_rxq += CONFIG_MV_ETH_RXQ;
  3020. priv->port_list[id] = port;
  3021. return 0;
  3022. }
  3023. /* Initialize decoding windows */
  3024. static void mvpp2_conf_mbus_windows(const struct mbus_dram_target_info *dram,
  3025. struct mvpp2 *priv)
  3026. {
  3027. u32 win_enable;
  3028. int i;
  3029. for (i = 0; i < 6; i++) {
  3030. mvpp2_write(priv, MVPP2_WIN_BASE(i), 0);
  3031. mvpp2_write(priv, MVPP2_WIN_SIZE(i), 0);
  3032. if (i < 4)
  3033. mvpp2_write(priv, MVPP2_WIN_REMAP(i), 0);
  3034. }
  3035. win_enable = 0;
  3036. for (i = 0; i < dram->num_cs; i++) {
  3037. const struct mbus_dram_window *cs = dram->cs + i;
  3038. mvpp2_write(priv, MVPP2_WIN_BASE(i),
  3039. (cs->base & 0xffff0000) | (cs->mbus_attr << 8) |
  3040. dram->mbus_dram_target_id);
  3041. mvpp2_write(priv, MVPP2_WIN_SIZE(i),
  3042. (cs->size - 1) & 0xffff0000);
  3043. win_enable |= (1 << i);
  3044. }
  3045. mvpp2_write(priv, MVPP2_BASE_ADDR_ENABLE, win_enable);
  3046. }
  3047. /* Initialize Rx FIFO's */
  3048. static void mvpp2_rx_fifo_init(struct mvpp2 *priv)
  3049. {
  3050. int port;
  3051. for (port = 0; port < MVPP2_MAX_PORTS; port++) {
  3052. mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
  3053. MVPP2_RX_FIFO_PORT_DATA_SIZE);
  3054. mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
  3055. MVPP2_RX_FIFO_PORT_ATTR_SIZE);
  3056. }
  3057. mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
  3058. MVPP2_RX_FIFO_PORT_MIN_PKT);
  3059. mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
  3060. }
  3061. /* Initialize network controller common part HW */
  3062. static int mvpp2_init(struct udevice *dev, struct mvpp2 *priv)
  3063. {
  3064. const struct mbus_dram_target_info *dram_target_info;
  3065. int err, i;
  3066. u32 val;
  3067. /* Checks for hardware constraints (U-Boot uses only one rxq) */
  3068. if ((rxq_number > MVPP2_MAX_RXQ) || (txq_number > MVPP2_MAX_TXQ)) {
  3069. dev_err(&pdev->dev, "invalid queue size parameter\n");
  3070. return -EINVAL;
  3071. }
  3072. /* MBUS windows configuration */
  3073. dram_target_info = mvebu_mbus_dram_info();
  3074. if (dram_target_info)
  3075. mvpp2_conf_mbus_windows(dram_target_info, priv);
  3076. /* Disable HW PHY polling */
  3077. val = readl(priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
  3078. val |= MVPP2_PHY_AN_STOP_SMI0_MASK;
  3079. writel(val, priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
  3080. /* Allocate and initialize aggregated TXQs */
  3081. priv->aggr_txqs = devm_kcalloc(dev, num_present_cpus(),
  3082. sizeof(struct mvpp2_tx_queue),
  3083. GFP_KERNEL);
  3084. if (!priv->aggr_txqs)
  3085. return -ENOMEM;
  3086. for_each_present_cpu(i) {
  3087. priv->aggr_txqs[i].id = i;
  3088. priv->aggr_txqs[i].size = MVPP2_AGGR_TXQ_SIZE;
  3089. err = mvpp2_aggr_txq_init(dev, &priv->aggr_txqs[i],
  3090. MVPP2_AGGR_TXQ_SIZE, i, priv);
  3091. if (err < 0)
  3092. return err;
  3093. }
  3094. /* Rx Fifo Init */
  3095. mvpp2_rx_fifo_init(priv);
  3096. /* Reset Rx queue group interrupt configuration */
  3097. for (i = 0; i < MVPP2_MAX_PORTS; i++)
  3098. mvpp2_write(priv, MVPP2_ISR_RXQ_GROUP_REG(i),
  3099. CONFIG_MV_ETH_RXQ);
  3100. writel(MVPP2_EXT_GLOBAL_CTRL_DEFAULT,
  3101. priv->lms_base + MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG);
  3102. /* Allow cache snoop when transmiting packets */
  3103. mvpp2_write(priv, MVPP2_TX_SNOOP_REG, 0x1);
  3104. /* Buffer Manager initialization */
  3105. err = mvpp2_bm_init(dev, priv);
  3106. if (err < 0)
  3107. return err;
  3108. /* Parser default initialization */
  3109. err = mvpp2_prs_default_init(dev, priv);
  3110. if (err < 0)
  3111. return err;
  3112. /* Classifier default initialization */
  3113. mvpp2_cls_init(priv);
  3114. return 0;
  3115. }
  3116. /* SMI / MDIO functions */
  3117. static int smi_wait_ready(struct mvpp2 *priv)
  3118. {
  3119. u32 timeout = MVPP2_SMI_TIMEOUT;
  3120. u32 smi_reg;
  3121. /* wait till the SMI is not busy */
  3122. do {
  3123. /* read smi register */
  3124. smi_reg = readl(priv->lms_base + MVPP2_SMI);
  3125. if (timeout-- == 0) {
  3126. printf("Error: SMI busy timeout\n");
  3127. return -EFAULT;
  3128. }
  3129. } while (smi_reg & MVPP2_SMI_BUSY);
  3130. return 0;
  3131. }
  3132. /*
  3133. * mpp2_mdio_read - miiphy_read callback function.
  3134. *
  3135. * Returns 16bit phy register value, or 0xffff on error
  3136. */
  3137. static int mpp2_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
  3138. {
  3139. struct mvpp2 *priv = bus->priv;
  3140. u32 smi_reg;
  3141. u32 timeout;
  3142. /* check parameters */
  3143. if (addr > MVPP2_PHY_ADDR_MASK) {
  3144. printf("Error: Invalid PHY address %d\n", addr);
  3145. return -EFAULT;
  3146. }
  3147. if (reg > MVPP2_PHY_REG_MASK) {
  3148. printf("Err: Invalid register offset %d\n", reg);
  3149. return -EFAULT;
  3150. }
  3151. /* wait till the SMI is not busy */
  3152. if (smi_wait_ready(priv) < 0)
  3153. return -EFAULT;
  3154. /* fill the phy address and regiser offset and read opcode */
  3155. smi_reg = (addr << MVPP2_SMI_DEV_ADDR_OFFS)
  3156. | (reg << MVPP2_SMI_REG_ADDR_OFFS)
  3157. | MVPP2_SMI_OPCODE_READ;
  3158. /* write the smi register */
  3159. writel(smi_reg, priv->lms_base + MVPP2_SMI);
  3160. /* wait till read value is ready */
  3161. timeout = MVPP2_SMI_TIMEOUT;
  3162. do {
  3163. /* read smi register */
  3164. smi_reg = readl(priv->lms_base + MVPP2_SMI);
  3165. if (timeout-- == 0) {
  3166. printf("Err: SMI read ready timeout\n");
  3167. return -EFAULT;
  3168. }
  3169. } while (!(smi_reg & MVPP2_SMI_READ_VALID));
  3170. /* Wait for the data to update in the SMI register */
  3171. for (timeout = 0; timeout < MVPP2_SMI_TIMEOUT; timeout++)
  3172. ;
  3173. return readl(priv->lms_base + MVPP2_SMI) & MVPP2_SMI_DATA_MASK;
  3174. }
  3175. /*
  3176. * mpp2_mdio_write - miiphy_write callback function.
  3177. *
  3178. * Returns 0 if write succeed, -EINVAL on bad parameters
  3179. * -ETIME on timeout
  3180. */
  3181. static int mpp2_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
  3182. u16 value)
  3183. {
  3184. struct mvpp2 *priv = bus->priv;
  3185. u32 smi_reg;
  3186. /* check parameters */
  3187. if (addr > MVPP2_PHY_ADDR_MASK) {
  3188. printf("Error: Invalid PHY address %d\n", addr);
  3189. return -EFAULT;
  3190. }
  3191. if (reg > MVPP2_PHY_REG_MASK) {
  3192. printf("Err: Invalid register offset %d\n", reg);
  3193. return -EFAULT;
  3194. }
  3195. /* wait till the SMI is not busy */
  3196. if (smi_wait_ready(priv) < 0)
  3197. return -EFAULT;
  3198. /* fill the phy addr and reg offset and write opcode and data */
  3199. smi_reg = value << MVPP2_SMI_DATA_OFFS;
  3200. smi_reg |= (addr << MVPP2_SMI_DEV_ADDR_OFFS)
  3201. | (reg << MVPP2_SMI_REG_ADDR_OFFS);
  3202. smi_reg &= ~MVPP2_SMI_OPCODE_READ;
  3203. /* write the smi register */
  3204. writel(smi_reg, priv->lms_base + MVPP2_SMI);
  3205. return 0;
  3206. }
  3207. static int mvpp2_recv(struct udevice *dev, int flags, uchar **packetp)
  3208. {
  3209. struct mvpp2_port *port = dev_get_priv(dev);
  3210. struct mvpp2_rx_desc *rx_desc;
  3211. struct mvpp2_bm_pool *bm_pool;
  3212. dma_addr_t dma_addr;
  3213. u32 bm, rx_status;
  3214. int pool, rx_bytes, err;
  3215. int rx_received;
  3216. struct mvpp2_rx_queue *rxq;
  3217. u32 cause_rx_tx, cause_rx, cause_misc;
  3218. u8 *data;
  3219. cause_rx_tx = mvpp2_read(port->priv,
  3220. MVPP2_ISR_RX_TX_CAUSE_REG(port->id));
  3221. cause_rx_tx &= ~MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
  3222. cause_misc = cause_rx_tx & MVPP2_CAUSE_MISC_SUM_MASK;
  3223. if (!cause_rx_tx && !cause_misc)
  3224. return 0;
  3225. cause_rx = cause_rx_tx & MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
  3226. /* Process RX packets */
  3227. cause_rx |= port->pending_cause_rx;
  3228. rxq = mvpp2_get_rx_queue(port, cause_rx);
  3229. /* Get number of received packets and clamp the to-do */
  3230. rx_received = mvpp2_rxq_received(port, rxq->id);
  3231. /* Return if no packets are received */
  3232. if (!rx_received)
  3233. return 0;
  3234. rx_desc = mvpp2_rxq_next_desc_get(rxq);
  3235. rx_status = mvpp2_rxdesc_status_get(port, rx_desc);
  3236. rx_bytes = mvpp2_rxdesc_size_get(port, rx_desc);
  3237. rx_bytes -= MVPP2_MH_SIZE;
  3238. dma_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
  3239. bm = mvpp2_bm_cookie_build(port, rx_desc);
  3240. pool = mvpp2_bm_cookie_pool_get(bm);
  3241. bm_pool = &port->priv->bm_pools[pool];
  3242. /* In case of an error, release the requested buffer pointer
  3243. * to the Buffer Manager. This request process is controlled
  3244. * by the hardware, and the information about the buffer is
  3245. * comprised by the RX descriptor.
  3246. */
  3247. if (rx_status & MVPP2_RXD_ERR_SUMMARY) {
  3248. mvpp2_rx_error(port, rx_desc);
  3249. /* Return the buffer to the pool */
  3250. mvpp2_pool_refill(port, bm, dma_addr, dma_addr);
  3251. return 0;
  3252. }
  3253. err = mvpp2_rx_refill(port, bm_pool, bm, dma_addr);
  3254. if (err) {
  3255. netdev_err(port->dev, "failed to refill BM pools\n");
  3256. return 0;
  3257. }
  3258. /* Update Rx queue management counters */
  3259. mb();
  3260. mvpp2_rxq_status_update(port, rxq->id, 1, 1);
  3261. /* give packet to stack - skip on first n bytes */
  3262. data = (u8 *)dma_addr + 2 + 32;
  3263. if (rx_bytes <= 0)
  3264. return 0;
  3265. /*
  3266. * No cache invalidation needed here, since the rx_buffer's are
  3267. * located in a uncached memory region
  3268. */
  3269. *packetp = data;
  3270. return rx_bytes;
  3271. }
  3272. /* Drain Txq */
  3273. static void mvpp2_txq_drain(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
  3274. int enable)
  3275. {
  3276. u32 val;
  3277. mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
  3278. val = mvpp2_read(port->priv, MVPP2_TXQ_PREF_BUF_REG);
  3279. if (enable)
  3280. val |= MVPP2_TXQ_DRAIN_EN_MASK;
  3281. else
  3282. val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
  3283. mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG, val);
  3284. }
  3285. static int mvpp2_send(struct udevice *dev, void *packet, int length)
  3286. {
  3287. struct mvpp2_port *port = dev_get_priv(dev);
  3288. struct mvpp2_tx_queue *txq, *aggr_txq;
  3289. struct mvpp2_tx_desc *tx_desc;
  3290. int tx_done;
  3291. int timeout;
  3292. txq = port->txqs[0];
  3293. aggr_txq = &port->priv->aggr_txqs[smp_processor_id()];
  3294. /* Get a descriptor for the first part of the packet */
  3295. tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
  3296. mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
  3297. mvpp2_txdesc_size_set(port, tx_desc, length);
  3298. mvpp2_txdesc_offset_set(port, tx_desc,
  3299. (dma_addr_t)packet & MVPP2_TX_DESC_ALIGN);
  3300. mvpp2_txdesc_dma_addr_set(port, tx_desc,
  3301. (dma_addr_t)packet & ~MVPP2_TX_DESC_ALIGN);
  3302. /* First and Last descriptor */
  3303. mvpp2_txdesc_cmd_set(port, tx_desc,
  3304. MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE
  3305. | MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC);
  3306. /* Flush tx data */
  3307. flush_dcache_range((unsigned long)packet,
  3308. (unsigned long)packet + ALIGN(length, PKTALIGN));
  3309. /* Enable transmit */
  3310. mb();
  3311. mvpp2_aggr_txq_pend_desc_add(port, 1);
  3312. mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
  3313. timeout = 0;
  3314. do {
  3315. if (timeout++ > 10000) {
  3316. printf("timeout: packet not sent from aggregated to phys TXQ\n");
  3317. return 0;
  3318. }
  3319. tx_done = mvpp2_txq_pend_desc_num_get(port, txq);
  3320. } while (tx_done);
  3321. /* Enable TXQ drain */
  3322. mvpp2_txq_drain(port, txq, 1);
  3323. timeout = 0;
  3324. do {
  3325. if (timeout++ > 10000) {
  3326. printf("timeout: packet not sent\n");
  3327. return 0;
  3328. }
  3329. tx_done = mvpp2_txq_sent_desc_proc(port, txq);
  3330. } while (!tx_done);
  3331. /* Disable TXQ drain */
  3332. mvpp2_txq_drain(port, txq, 0);
  3333. return 0;
  3334. }
  3335. static int mvpp2_start(struct udevice *dev)
  3336. {
  3337. struct eth_pdata *pdata = dev_get_platdata(dev);
  3338. struct mvpp2_port *port = dev_get_priv(dev);
  3339. /* Load current MAC address */
  3340. memcpy(port->dev_addr, pdata->enetaddr, ETH_ALEN);
  3341. /* Reconfigure parser accept the original MAC address */
  3342. mvpp2_prs_update_mac_da(port, port->dev_addr);
  3343. mvpp2_port_power_up(port);
  3344. mvpp2_open(dev, port);
  3345. return 0;
  3346. }
  3347. static void mvpp2_stop(struct udevice *dev)
  3348. {
  3349. struct mvpp2_port *port = dev_get_priv(dev);
  3350. mvpp2_stop_dev(port);
  3351. mvpp2_cleanup_rxqs(port);
  3352. mvpp2_cleanup_txqs(port);
  3353. }
  3354. static int mvpp2_probe(struct udevice *dev)
  3355. {
  3356. struct mvpp2_port *port = dev_get_priv(dev);
  3357. struct mvpp2 *priv = dev_get_priv(dev->parent);
  3358. int err;
  3359. /* Initialize network controller */
  3360. err = mvpp2_init(dev, priv);
  3361. if (err < 0) {
  3362. dev_err(&pdev->dev, "failed to initialize controller\n");
  3363. return err;
  3364. }
  3365. return mvpp2_port_probe(dev, port, dev_of_offset(dev), priv,
  3366. &buffer_loc.first_rxq);
  3367. }
  3368. static const struct eth_ops mvpp2_ops = {
  3369. .start = mvpp2_start,
  3370. .send = mvpp2_send,
  3371. .recv = mvpp2_recv,
  3372. .stop = mvpp2_stop,
  3373. };
  3374. static struct driver mvpp2_driver = {
  3375. .name = "mvpp2",
  3376. .id = UCLASS_ETH,
  3377. .probe = mvpp2_probe,
  3378. .ops = &mvpp2_ops,
  3379. .priv_auto_alloc_size = sizeof(struct mvpp2_port),
  3380. .platdata_auto_alloc_size = sizeof(struct eth_pdata),
  3381. };
  3382. /*
  3383. * Use a MISC device to bind the n instances (child nodes) of the
  3384. * network base controller in UCLASS_ETH.
  3385. */
  3386. static int mvpp2_base_probe(struct udevice *dev)
  3387. {
  3388. struct mvpp2 *priv = dev_get_priv(dev);
  3389. struct mii_dev *bus;
  3390. void *bd_space;
  3391. u32 size = 0;
  3392. int i;
  3393. /* Save hw-version */
  3394. priv->hw_version = dev_get_driver_data(dev);
  3395. /*
  3396. * U-Boot special buffer handling:
  3397. *
  3398. * Allocate buffer area for descs and rx_buffers. This is only
  3399. * done once for all interfaces. As only one interface can
  3400. * be active. Make this area DMA-safe by disabling the D-cache
  3401. */
  3402. /* Align buffer area for descs and rx_buffers to 1MiB */
  3403. bd_space = memalign(1 << MMU_SECTION_SHIFT, BD_SPACE);
  3404. mmu_set_region_dcache_behaviour((unsigned long)bd_space,
  3405. BD_SPACE, DCACHE_OFF);
  3406. buffer_loc.aggr_tx_descs = (struct mvpp2_tx_desc *)bd_space;
  3407. size += MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE;
  3408. buffer_loc.tx_descs =
  3409. (struct mvpp2_tx_desc *)((unsigned long)bd_space + size);
  3410. size += MVPP2_MAX_TXD * MVPP2_DESC_ALIGNED_SIZE;
  3411. buffer_loc.rx_descs =
  3412. (struct mvpp2_rx_desc *)((unsigned long)bd_space + size);
  3413. size += MVPP2_MAX_RXD * MVPP2_DESC_ALIGNED_SIZE;
  3414. for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
  3415. buffer_loc.bm_pool[i] =
  3416. (unsigned long *)((unsigned long)bd_space + size);
  3417. size += MVPP2_BM_POOL_SIZE_MAX * sizeof(u32);
  3418. }
  3419. for (i = 0; i < MVPP2_BM_LONG_BUF_NUM; i++) {
  3420. buffer_loc.rx_buffer[i] =
  3421. (unsigned long *)((unsigned long)bd_space + size);
  3422. size += RX_BUFFER_SIZE;
  3423. }
  3424. /* Save base addresses for later use */
  3425. priv->base = (void *)dev_get_addr_index(dev, 0);
  3426. if (IS_ERR(priv->base))
  3427. return PTR_ERR(priv->base);
  3428. priv->lms_base = (void *)dev_get_addr_index(dev, 1);
  3429. if (IS_ERR(priv->lms_base))
  3430. return PTR_ERR(priv->lms_base);
  3431. /* Finally create and register the MDIO bus driver */
  3432. bus = mdio_alloc();
  3433. if (!bus) {
  3434. printf("Failed to allocate MDIO bus\n");
  3435. return -ENOMEM;
  3436. }
  3437. bus->read = mpp2_mdio_read;
  3438. bus->write = mpp2_mdio_write;
  3439. snprintf(bus->name, sizeof(bus->name), dev->name);
  3440. bus->priv = (void *)priv;
  3441. priv->bus = bus;
  3442. return mdio_register(bus);
  3443. }
  3444. static int mvpp2_base_bind(struct udevice *parent)
  3445. {
  3446. const void *blob = gd->fdt_blob;
  3447. int node = dev_of_offset(parent);
  3448. struct uclass_driver *drv;
  3449. struct udevice *dev;
  3450. struct eth_pdata *plat;
  3451. char *name;
  3452. int subnode;
  3453. u32 id;
  3454. /* Lookup eth driver */
  3455. drv = lists_uclass_lookup(UCLASS_ETH);
  3456. if (!drv) {
  3457. puts("Cannot find eth driver\n");
  3458. return -ENOENT;
  3459. }
  3460. fdt_for_each_subnode(subnode, blob, node) {
  3461. /* Skip disabled ports */
  3462. if (!fdtdec_get_is_enabled(blob, subnode))
  3463. continue;
  3464. plat = calloc(1, sizeof(*plat));
  3465. if (!plat)
  3466. return -ENOMEM;
  3467. id = fdtdec_get_int(blob, subnode, "port-id", -1);
  3468. name = calloc(1, 16);
  3469. sprintf(name, "mvpp2-%d", id);
  3470. /* Create child device UCLASS_ETH and bind it */
  3471. device_bind(parent, &mvpp2_driver, name, plat, subnode, &dev);
  3472. dev_set_of_offset(dev, subnode);
  3473. }
  3474. return 0;
  3475. }
  3476. static const struct udevice_id mvpp2_ids[] = {
  3477. {
  3478. .compatible = "marvell,armada-375-pp2",
  3479. .data = MVPP21,
  3480. },
  3481. { }
  3482. };
  3483. U_BOOT_DRIVER(mvpp2_base) = {
  3484. .name = "mvpp2_base",
  3485. .id = UCLASS_MISC,
  3486. .of_match = mvpp2_ids,
  3487. .bind = mvpp2_base_bind,
  3488. .probe = mvpp2_base_probe,
  3489. .priv_auto_alloc_size = sizeof(struct mvpp2),
  3490. };