mvpp2.c 120 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329
  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. /* HW TX descriptor for PPv2.1 */
  704. struct mvpp21_tx_desc {
  705. u32 command; /* Options used by HW for packet transmitting.*/
  706. u8 packet_offset; /* the offset from the buffer beginning */
  707. u8 phys_txq; /* destination queue ID */
  708. u16 data_size; /* data size of transmitted packet in bytes */
  709. u32 buf_dma_addr; /* physical addr of transmitted buffer */
  710. u32 buf_cookie; /* cookie for access to TX buffer in tx path */
  711. u32 reserved1[3]; /* hw_cmd (for future use, BM, PON, PNC) */
  712. u32 reserved2; /* reserved (for future use) */
  713. };
  714. /* HW RX descriptor for PPv2.1 */
  715. struct mvpp21_rx_desc {
  716. u32 status; /* info about received packet */
  717. u16 reserved1; /* parser_info (for future use, PnC) */
  718. u16 data_size; /* size of received packet in bytes */
  719. u32 buf_dma_addr; /* physical address of the buffer */
  720. u32 buf_cookie; /* cookie for access to RX buffer in rx path */
  721. u16 reserved2; /* gem_port_id (for future use, PON) */
  722. u16 reserved3; /* csum_l4 (for future use, PnC) */
  723. u8 reserved4; /* bm_qset (for future use, BM) */
  724. u8 reserved5;
  725. u16 reserved6; /* classify_info (for future use, PnC) */
  726. u32 reserved7; /* flow_id (for future use, PnC) */
  727. u32 reserved8;
  728. };
  729. /* HW TX descriptor for PPv2.2 */
  730. struct mvpp22_tx_desc {
  731. u32 command;
  732. u8 packet_offset;
  733. u8 phys_txq;
  734. u16 data_size;
  735. u64 reserved1;
  736. u64 buf_dma_addr_ptp;
  737. u64 buf_cookie_misc;
  738. };
  739. /* HW RX descriptor for PPv2.2 */
  740. struct mvpp22_rx_desc {
  741. u32 status;
  742. u16 reserved1;
  743. u16 data_size;
  744. u32 reserved2;
  745. u32 reserved3;
  746. u64 buf_dma_addr_key_hash;
  747. u64 buf_cookie_misc;
  748. };
  749. /* Opaque type used by the driver to manipulate the HW TX and RX
  750. * descriptors
  751. */
  752. struct mvpp2_tx_desc {
  753. union {
  754. struct mvpp21_tx_desc pp21;
  755. struct mvpp22_tx_desc pp22;
  756. };
  757. };
  758. struct mvpp2_rx_desc {
  759. union {
  760. struct mvpp21_rx_desc pp21;
  761. struct mvpp22_rx_desc pp22;
  762. };
  763. };
  764. /* Per-CPU Tx queue control */
  765. struct mvpp2_txq_pcpu {
  766. int cpu;
  767. /* Number of Tx DMA descriptors in the descriptor ring */
  768. int size;
  769. /* Number of currently used Tx DMA descriptor in the
  770. * descriptor ring
  771. */
  772. int count;
  773. /* Number of Tx DMA descriptors reserved for each CPU */
  774. int reserved_num;
  775. /* Index of last TX DMA descriptor that was inserted */
  776. int txq_put_index;
  777. /* Index of the TX DMA descriptor to be cleaned up */
  778. int txq_get_index;
  779. };
  780. struct mvpp2_tx_queue {
  781. /* Physical number of this Tx queue */
  782. u8 id;
  783. /* Logical number of this Tx queue */
  784. u8 log_id;
  785. /* Number of Tx DMA descriptors in the descriptor ring */
  786. int size;
  787. /* Number of currently used Tx DMA descriptor in the descriptor ring */
  788. int count;
  789. /* Per-CPU control of physical Tx queues */
  790. struct mvpp2_txq_pcpu __percpu *pcpu;
  791. u32 done_pkts_coal;
  792. /* Virtual address of thex Tx DMA descriptors array */
  793. struct mvpp2_tx_desc *descs;
  794. /* DMA address of the Tx DMA descriptors array */
  795. dma_addr_t descs_dma;
  796. /* Index of the last Tx DMA descriptor */
  797. int last_desc;
  798. /* Index of the next Tx DMA descriptor to process */
  799. int next_desc_to_proc;
  800. };
  801. struct mvpp2_rx_queue {
  802. /* RX queue number, in the range 0-31 for physical RXQs */
  803. u8 id;
  804. /* Num of rx descriptors in the rx descriptor ring */
  805. int size;
  806. u32 pkts_coal;
  807. u32 time_coal;
  808. /* Virtual address of the RX DMA descriptors array */
  809. struct mvpp2_rx_desc *descs;
  810. /* DMA address of the RX DMA descriptors array */
  811. dma_addr_t descs_dma;
  812. /* Index of the last RX DMA descriptor */
  813. int last_desc;
  814. /* Index of the next RX DMA descriptor to process */
  815. int next_desc_to_proc;
  816. /* ID of port to which physical RXQ is mapped */
  817. int port;
  818. /* Port's logic RXQ number to which physical RXQ is mapped */
  819. int logic_rxq;
  820. };
  821. union mvpp2_prs_tcam_entry {
  822. u32 word[MVPP2_PRS_TCAM_WORDS];
  823. u8 byte[MVPP2_PRS_TCAM_WORDS * 4];
  824. };
  825. union mvpp2_prs_sram_entry {
  826. u32 word[MVPP2_PRS_SRAM_WORDS];
  827. u8 byte[MVPP2_PRS_SRAM_WORDS * 4];
  828. };
  829. struct mvpp2_prs_entry {
  830. u32 index;
  831. union mvpp2_prs_tcam_entry tcam;
  832. union mvpp2_prs_sram_entry sram;
  833. };
  834. struct mvpp2_prs_shadow {
  835. bool valid;
  836. bool finish;
  837. /* Lookup ID */
  838. int lu;
  839. /* User defined offset */
  840. int udf;
  841. /* Result info */
  842. u32 ri;
  843. u32 ri_mask;
  844. };
  845. struct mvpp2_cls_flow_entry {
  846. u32 index;
  847. u32 data[MVPP2_CLS_FLOWS_TBL_DATA_WORDS];
  848. };
  849. struct mvpp2_cls_lookup_entry {
  850. u32 lkpid;
  851. u32 way;
  852. u32 data;
  853. };
  854. struct mvpp2_bm_pool {
  855. /* Pool number in the range 0-7 */
  856. int id;
  857. enum mvpp2_bm_type type;
  858. /* Buffer Pointers Pool External (BPPE) size */
  859. int size;
  860. /* Number of buffers for this pool */
  861. int buf_num;
  862. /* Pool buffer size */
  863. int buf_size;
  864. /* Packet size */
  865. int pkt_size;
  866. /* BPPE virtual base address */
  867. unsigned long *virt_addr;
  868. /* BPPE DMA base address */
  869. dma_addr_t dma_addr;
  870. /* Ports using BM pool */
  871. u32 port_map;
  872. /* Occupied buffers indicator */
  873. int in_use_thresh;
  874. };
  875. /* Static declaractions */
  876. /* Number of RXQs used by single port */
  877. static int rxq_number = MVPP2_DEFAULT_RXQ;
  878. /* Number of TXQs used by single port */
  879. static int txq_number = MVPP2_DEFAULT_TXQ;
  880. #define MVPP2_DRIVER_NAME "mvpp2"
  881. #define MVPP2_DRIVER_VERSION "1.0"
  882. /*
  883. * U-Boot internal data, mostly uncached buffers for descriptors and data
  884. */
  885. struct buffer_location {
  886. struct mvpp2_tx_desc *aggr_tx_descs;
  887. struct mvpp2_tx_desc *tx_descs;
  888. struct mvpp2_rx_desc *rx_descs;
  889. unsigned long *bm_pool[MVPP2_BM_POOLS_NUM];
  890. unsigned long *rx_buffer[MVPP2_BM_LONG_BUF_NUM];
  891. int first_rxq;
  892. };
  893. /*
  894. * All 4 interfaces use the same global buffer, since only one interface
  895. * can be enabled at once
  896. */
  897. static struct buffer_location buffer_loc;
  898. /*
  899. * Page table entries are set to 1MB, or multiples of 1MB
  900. * (not < 1MB). driver uses less bd's so use 1MB bdspace.
  901. */
  902. #define BD_SPACE (1 << 20)
  903. /* Utility/helper methods */
  904. static void mvpp2_write(struct mvpp2 *priv, u32 offset, u32 data)
  905. {
  906. writel(data, priv->base + offset);
  907. }
  908. static u32 mvpp2_read(struct mvpp2 *priv, u32 offset)
  909. {
  910. return readl(priv->base + offset);
  911. }
  912. static void mvpp2_txdesc_dma_addr_set(struct mvpp2_port *port,
  913. struct mvpp2_tx_desc *tx_desc,
  914. dma_addr_t dma_addr)
  915. {
  916. if (port->priv->hw_version == MVPP21) {
  917. tx_desc->pp21.buf_dma_addr = dma_addr;
  918. } else {
  919. u64 val = (u64)dma_addr;
  920. tx_desc->pp22.buf_dma_addr_ptp &= ~GENMASK_ULL(40, 0);
  921. tx_desc->pp22.buf_dma_addr_ptp |= val;
  922. }
  923. }
  924. static void mvpp2_txdesc_size_set(struct mvpp2_port *port,
  925. struct mvpp2_tx_desc *tx_desc,
  926. size_t size)
  927. {
  928. if (port->priv->hw_version == MVPP21)
  929. tx_desc->pp21.data_size = size;
  930. else
  931. tx_desc->pp22.data_size = size;
  932. }
  933. static void mvpp2_txdesc_txq_set(struct mvpp2_port *port,
  934. struct mvpp2_tx_desc *tx_desc,
  935. unsigned int txq)
  936. {
  937. if (port->priv->hw_version == MVPP21)
  938. tx_desc->pp21.phys_txq = txq;
  939. else
  940. tx_desc->pp22.phys_txq = txq;
  941. }
  942. static void mvpp2_txdesc_cmd_set(struct mvpp2_port *port,
  943. struct mvpp2_tx_desc *tx_desc,
  944. unsigned int command)
  945. {
  946. if (port->priv->hw_version == MVPP21)
  947. tx_desc->pp21.command = command;
  948. else
  949. tx_desc->pp22.command = command;
  950. }
  951. static void mvpp2_txdesc_offset_set(struct mvpp2_port *port,
  952. struct mvpp2_tx_desc *tx_desc,
  953. unsigned int offset)
  954. {
  955. if (port->priv->hw_version == MVPP21)
  956. tx_desc->pp21.packet_offset = offset;
  957. else
  958. tx_desc->pp22.packet_offset = offset;
  959. }
  960. static dma_addr_t mvpp2_rxdesc_dma_addr_get(struct mvpp2_port *port,
  961. struct mvpp2_rx_desc *rx_desc)
  962. {
  963. if (port->priv->hw_version == MVPP21)
  964. return rx_desc->pp21.buf_dma_addr;
  965. else
  966. return rx_desc->pp22.buf_dma_addr_key_hash & GENMASK_ULL(40, 0);
  967. }
  968. static unsigned long mvpp2_rxdesc_cookie_get(struct mvpp2_port *port,
  969. struct mvpp2_rx_desc *rx_desc)
  970. {
  971. if (port->priv->hw_version == MVPP21)
  972. return rx_desc->pp21.buf_cookie;
  973. else
  974. return rx_desc->pp22.buf_cookie_misc & GENMASK_ULL(40, 0);
  975. }
  976. static size_t mvpp2_rxdesc_size_get(struct mvpp2_port *port,
  977. struct mvpp2_rx_desc *rx_desc)
  978. {
  979. if (port->priv->hw_version == MVPP21)
  980. return rx_desc->pp21.data_size;
  981. else
  982. return rx_desc->pp22.data_size;
  983. }
  984. static u32 mvpp2_rxdesc_status_get(struct mvpp2_port *port,
  985. struct mvpp2_rx_desc *rx_desc)
  986. {
  987. if (port->priv->hw_version == MVPP21)
  988. return rx_desc->pp21.status;
  989. else
  990. return rx_desc->pp22.status;
  991. }
  992. static void mvpp2_txq_inc_get(struct mvpp2_txq_pcpu *txq_pcpu)
  993. {
  994. txq_pcpu->txq_get_index++;
  995. if (txq_pcpu->txq_get_index == txq_pcpu->size)
  996. txq_pcpu->txq_get_index = 0;
  997. }
  998. /* Get number of physical egress port */
  999. static inline int mvpp2_egress_port(struct mvpp2_port *port)
  1000. {
  1001. return MVPP2_MAX_TCONT + port->id;
  1002. }
  1003. /* Get number of physical TXQ */
  1004. static inline int mvpp2_txq_phys(int port, int txq)
  1005. {
  1006. return (MVPP2_MAX_TCONT + port) * MVPP2_MAX_TXQ + txq;
  1007. }
  1008. /* Parser configuration routines */
  1009. /* Update parser tcam and sram hw entries */
  1010. static int mvpp2_prs_hw_write(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
  1011. {
  1012. int i;
  1013. if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
  1014. return -EINVAL;
  1015. /* Clear entry invalidation bit */
  1016. pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] &= ~MVPP2_PRS_TCAM_INV_MASK;
  1017. /* Write tcam index - indirect access */
  1018. mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
  1019. for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
  1020. mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), pe->tcam.word[i]);
  1021. /* Write sram index - indirect access */
  1022. mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
  1023. for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
  1024. mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), pe->sram.word[i]);
  1025. return 0;
  1026. }
  1027. /* Read tcam entry from hw */
  1028. static int mvpp2_prs_hw_read(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
  1029. {
  1030. int i;
  1031. if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
  1032. return -EINVAL;
  1033. /* Write tcam index - indirect access */
  1034. mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
  1035. pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] = mvpp2_read(priv,
  1036. MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD));
  1037. if (pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] & MVPP2_PRS_TCAM_INV_MASK)
  1038. return MVPP2_PRS_TCAM_ENTRY_INVALID;
  1039. for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
  1040. pe->tcam.word[i] = mvpp2_read(priv, MVPP2_PRS_TCAM_DATA_REG(i));
  1041. /* Write sram index - indirect access */
  1042. mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
  1043. for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
  1044. pe->sram.word[i] = mvpp2_read(priv, MVPP2_PRS_SRAM_DATA_REG(i));
  1045. return 0;
  1046. }
  1047. /* Invalidate tcam hw entry */
  1048. static void mvpp2_prs_hw_inv(struct mvpp2 *priv, int index)
  1049. {
  1050. /* Write index - indirect access */
  1051. mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
  1052. mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD),
  1053. MVPP2_PRS_TCAM_INV_MASK);
  1054. }
  1055. /* Enable shadow table entry and set its lookup ID */
  1056. static void mvpp2_prs_shadow_set(struct mvpp2 *priv, int index, int lu)
  1057. {
  1058. priv->prs_shadow[index].valid = true;
  1059. priv->prs_shadow[index].lu = lu;
  1060. }
  1061. /* Update ri fields in shadow table entry */
  1062. static void mvpp2_prs_shadow_ri_set(struct mvpp2 *priv, int index,
  1063. unsigned int ri, unsigned int ri_mask)
  1064. {
  1065. priv->prs_shadow[index].ri_mask = ri_mask;
  1066. priv->prs_shadow[index].ri = ri;
  1067. }
  1068. /* Update lookup field in tcam sw entry */
  1069. static void mvpp2_prs_tcam_lu_set(struct mvpp2_prs_entry *pe, unsigned int lu)
  1070. {
  1071. int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_LU_BYTE);
  1072. pe->tcam.byte[MVPP2_PRS_TCAM_LU_BYTE] = lu;
  1073. pe->tcam.byte[enable_off] = MVPP2_PRS_LU_MASK;
  1074. }
  1075. /* Update mask for single port in tcam sw entry */
  1076. static void mvpp2_prs_tcam_port_set(struct mvpp2_prs_entry *pe,
  1077. unsigned int port, bool add)
  1078. {
  1079. int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
  1080. if (add)
  1081. pe->tcam.byte[enable_off] &= ~(1 << port);
  1082. else
  1083. pe->tcam.byte[enable_off] |= 1 << port;
  1084. }
  1085. /* Update port map in tcam sw entry */
  1086. static void mvpp2_prs_tcam_port_map_set(struct mvpp2_prs_entry *pe,
  1087. unsigned int ports)
  1088. {
  1089. unsigned char port_mask = MVPP2_PRS_PORT_MASK;
  1090. int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
  1091. pe->tcam.byte[MVPP2_PRS_TCAM_PORT_BYTE] = 0;
  1092. pe->tcam.byte[enable_off] &= ~port_mask;
  1093. pe->tcam.byte[enable_off] |= ~ports & MVPP2_PRS_PORT_MASK;
  1094. }
  1095. /* Obtain port map from tcam sw entry */
  1096. static unsigned int mvpp2_prs_tcam_port_map_get(struct mvpp2_prs_entry *pe)
  1097. {
  1098. int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
  1099. return ~(pe->tcam.byte[enable_off]) & MVPP2_PRS_PORT_MASK;
  1100. }
  1101. /* Set byte of data and its enable bits in tcam sw entry */
  1102. static void mvpp2_prs_tcam_data_byte_set(struct mvpp2_prs_entry *pe,
  1103. unsigned int offs, unsigned char byte,
  1104. unsigned char enable)
  1105. {
  1106. pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)] = byte;
  1107. pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)] = enable;
  1108. }
  1109. /* Get byte of data and its enable bits from tcam sw entry */
  1110. static void mvpp2_prs_tcam_data_byte_get(struct mvpp2_prs_entry *pe,
  1111. unsigned int offs, unsigned char *byte,
  1112. unsigned char *enable)
  1113. {
  1114. *byte = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)];
  1115. *enable = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)];
  1116. }
  1117. /* Set ethertype in tcam sw entry */
  1118. static void mvpp2_prs_match_etype(struct mvpp2_prs_entry *pe, int offset,
  1119. unsigned short ethertype)
  1120. {
  1121. mvpp2_prs_tcam_data_byte_set(pe, offset + 0, ethertype >> 8, 0xff);
  1122. mvpp2_prs_tcam_data_byte_set(pe, offset + 1, ethertype & 0xff, 0xff);
  1123. }
  1124. /* Set bits in sram sw entry */
  1125. static void mvpp2_prs_sram_bits_set(struct mvpp2_prs_entry *pe, int bit_num,
  1126. int val)
  1127. {
  1128. pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] |= (val << (bit_num % 8));
  1129. }
  1130. /* Clear bits in sram sw entry */
  1131. static void mvpp2_prs_sram_bits_clear(struct mvpp2_prs_entry *pe, int bit_num,
  1132. int val)
  1133. {
  1134. pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] &= ~(val << (bit_num % 8));
  1135. }
  1136. /* Update ri bits in sram sw entry */
  1137. static void mvpp2_prs_sram_ri_update(struct mvpp2_prs_entry *pe,
  1138. unsigned int bits, unsigned int mask)
  1139. {
  1140. unsigned int i;
  1141. for (i = 0; i < MVPP2_PRS_SRAM_RI_CTRL_BITS; i++) {
  1142. int ri_off = MVPP2_PRS_SRAM_RI_OFFS;
  1143. if (!(mask & BIT(i)))
  1144. continue;
  1145. if (bits & BIT(i))
  1146. mvpp2_prs_sram_bits_set(pe, ri_off + i, 1);
  1147. else
  1148. mvpp2_prs_sram_bits_clear(pe, ri_off + i, 1);
  1149. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_RI_CTRL_OFFS + i, 1);
  1150. }
  1151. }
  1152. /* Update ai bits in sram sw entry */
  1153. static void mvpp2_prs_sram_ai_update(struct mvpp2_prs_entry *pe,
  1154. unsigned int bits, unsigned int mask)
  1155. {
  1156. unsigned int i;
  1157. int ai_off = MVPP2_PRS_SRAM_AI_OFFS;
  1158. for (i = 0; i < MVPP2_PRS_SRAM_AI_CTRL_BITS; i++) {
  1159. if (!(mask & BIT(i)))
  1160. continue;
  1161. if (bits & BIT(i))
  1162. mvpp2_prs_sram_bits_set(pe, ai_off + i, 1);
  1163. else
  1164. mvpp2_prs_sram_bits_clear(pe, ai_off + i, 1);
  1165. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_AI_CTRL_OFFS + i, 1);
  1166. }
  1167. }
  1168. /* Read ai bits from sram sw entry */
  1169. static int mvpp2_prs_sram_ai_get(struct mvpp2_prs_entry *pe)
  1170. {
  1171. u8 bits;
  1172. int ai_off = MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_AI_OFFS);
  1173. int ai_en_off = ai_off + 1;
  1174. int ai_shift = MVPP2_PRS_SRAM_AI_OFFS % 8;
  1175. bits = (pe->sram.byte[ai_off] >> ai_shift) |
  1176. (pe->sram.byte[ai_en_off] << (8 - ai_shift));
  1177. return bits;
  1178. }
  1179. /* In sram sw entry set lookup ID field of the tcam key to be used in the next
  1180. * lookup interation
  1181. */
  1182. static void mvpp2_prs_sram_next_lu_set(struct mvpp2_prs_entry *pe,
  1183. unsigned int lu)
  1184. {
  1185. int sram_next_off = MVPP2_PRS_SRAM_NEXT_LU_OFFS;
  1186. mvpp2_prs_sram_bits_clear(pe, sram_next_off,
  1187. MVPP2_PRS_SRAM_NEXT_LU_MASK);
  1188. mvpp2_prs_sram_bits_set(pe, sram_next_off, lu);
  1189. }
  1190. /* In the sram sw entry set sign and value of the next lookup offset
  1191. * and the offset value generated to the classifier
  1192. */
  1193. static void mvpp2_prs_sram_shift_set(struct mvpp2_prs_entry *pe, int shift,
  1194. unsigned int op)
  1195. {
  1196. /* Set sign */
  1197. if (shift < 0) {
  1198. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
  1199. shift = 0 - shift;
  1200. } else {
  1201. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
  1202. }
  1203. /* Set value */
  1204. pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_SHIFT_OFFS)] =
  1205. (unsigned char)shift;
  1206. /* Reset and set operation */
  1207. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS,
  1208. MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK);
  1209. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS, op);
  1210. /* Set base offset as current */
  1211. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
  1212. }
  1213. /* In the sram sw entry set sign and value of the user defined offset
  1214. * generated to the classifier
  1215. */
  1216. static void mvpp2_prs_sram_offset_set(struct mvpp2_prs_entry *pe,
  1217. unsigned int type, int offset,
  1218. unsigned int op)
  1219. {
  1220. /* Set sign */
  1221. if (offset < 0) {
  1222. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
  1223. offset = 0 - offset;
  1224. } else {
  1225. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
  1226. }
  1227. /* Set value */
  1228. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_OFFS,
  1229. MVPP2_PRS_SRAM_UDF_MASK);
  1230. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_OFFS, offset);
  1231. pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
  1232. MVPP2_PRS_SRAM_UDF_BITS)] &=
  1233. ~(MVPP2_PRS_SRAM_UDF_MASK >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
  1234. pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
  1235. MVPP2_PRS_SRAM_UDF_BITS)] |=
  1236. (offset >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
  1237. /* Set offset type */
  1238. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS,
  1239. MVPP2_PRS_SRAM_UDF_TYPE_MASK);
  1240. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS, type);
  1241. /* Set offset operation */
  1242. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS,
  1243. MVPP2_PRS_SRAM_OP_SEL_UDF_MASK);
  1244. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS, op);
  1245. pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
  1246. MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] &=
  1247. ~(MVPP2_PRS_SRAM_OP_SEL_UDF_MASK >>
  1248. (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
  1249. pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
  1250. MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] |=
  1251. (op >> (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
  1252. /* Set base offset as current */
  1253. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
  1254. }
  1255. /* Find parser flow entry */
  1256. static struct mvpp2_prs_entry *mvpp2_prs_flow_find(struct mvpp2 *priv, int flow)
  1257. {
  1258. struct mvpp2_prs_entry *pe;
  1259. int tid;
  1260. pe = kzalloc(sizeof(*pe), GFP_KERNEL);
  1261. if (!pe)
  1262. return NULL;
  1263. mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
  1264. /* Go through the all entires with MVPP2_PRS_LU_FLOWS */
  1265. for (tid = MVPP2_PRS_TCAM_SRAM_SIZE - 1; tid >= 0; tid--) {
  1266. u8 bits;
  1267. if (!priv->prs_shadow[tid].valid ||
  1268. priv->prs_shadow[tid].lu != MVPP2_PRS_LU_FLOWS)
  1269. continue;
  1270. pe->index = tid;
  1271. mvpp2_prs_hw_read(priv, pe);
  1272. bits = mvpp2_prs_sram_ai_get(pe);
  1273. /* Sram store classification lookup ID in AI bits [5:0] */
  1274. if ((bits & MVPP2_PRS_FLOW_ID_MASK) == flow)
  1275. return pe;
  1276. }
  1277. kfree(pe);
  1278. return NULL;
  1279. }
  1280. /* Return first free tcam index, seeking from start to end */
  1281. static int mvpp2_prs_tcam_first_free(struct mvpp2 *priv, unsigned char start,
  1282. unsigned char end)
  1283. {
  1284. int tid;
  1285. if (start > end)
  1286. swap(start, end);
  1287. if (end >= MVPP2_PRS_TCAM_SRAM_SIZE)
  1288. end = MVPP2_PRS_TCAM_SRAM_SIZE - 1;
  1289. for (tid = start; tid <= end; tid++) {
  1290. if (!priv->prs_shadow[tid].valid)
  1291. return tid;
  1292. }
  1293. return -EINVAL;
  1294. }
  1295. /* Enable/disable dropping all mac da's */
  1296. static void mvpp2_prs_mac_drop_all_set(struct mvpp2 *priv, int port, bool add)
  1297. {
  1298. struct mvpp2_prs_entry pe;
  1299. if (priv->prs_shadow[MVPP2_PE_DROP_ALL].valid) {
  1300. /* Entry exist - update port only */
  1301. pe.index = MVPP2_PE_DROP_ALL;
  1302. mvpp2_prs_hw_read(priv, &pe);
  1303. } else {
  1304. /* Entry doesn't exist - create new */
  1305. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1306. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
  1307. pe.index = MVPP2_PE_DROP_ALL;
  1308. /* Non-promiscuous mode for all ports - DROP unknown packets */
  1309. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
  1310. MVPP2_PRS_RI_DROP_MASK);
  1311. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  1312. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  1313. /* Update shadow table */
  1314. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
  1315. /* Mask all ports */
  1316. mvpp2_prs_tcam_port_map_set(&pe, 0);
  1317. }
  1318. /* Update port mask */
  1319. mvpp2_prs_tcam_port_set(&pe, port, add);
  1320. mvpp2_prs_hw_write(priv, &pe);
  1321. }
  1322. /* Set port to promiscuous mode */
  1323. static void mvpp2_prs_mac_promisc_set(struct mvpp2 *priv, int port, bool add)
  1324. {
  1325. struct mvpp2_prs_entry pe;
  1326. /* Promiscuous mode - Accept unknown packets */
  1327. if (priv->prs_shadow[MVPP2_PE_MAC_PROMISCUOUS].valid) {
  1328. /* Entry exist - update port only */
  1329. pe.index = MVPP2_PE_MAC_PROMISCUOUS;
  1330. mvpp2_prs_hw_read(priv, &pe);
  1331. } else {
  1332. /* Entry doesn't exist - create new */
  1333. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1334. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
  1335. pe.index = MVPP2_PE_MAC_PROMISCUOUS;
  1336. /* Continue - set next lookup */
  1337. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
  1338. /* Set result info bits */
  1339. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_UCAST,
  1340. MVPP2_PRS_RI_L2_CAST_MASK);
  1341. /* Shift to ethertype */
  1342. mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
  1343. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1344. /* Mask all ports */
  1345. mvpp2_prs_tcam_port_map_set(&pe, 0);
  1346. /* Update shadow table */
  1347. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
  1348. }
  1349. /* Update port mask */
  1350. mvpp2_prs_tcam_port_set(&pe, port, add);
  1351. mvpp2_prs_hw_write(priv, &pe);
  1352. }
  1353. /* Accept multicast */
  1354. static void mvpp2_prs_mac_multi_set(struct mvpp2 *priv, int port, int index,
  1355. bool add)
  1356. {
  1357. struct mvpp2_prs_entry pe;
  1358. unsigned char da_mc;
  1359. /* Ethernet multicast address first byte is
  1360. * 0x01 for IPv4 and 0x33 for IPv6
  1361. */
  1362. da_mc = (index == MVPP2_PE_MAC_MC_ALL) ? 0x01 : 0x33;
  1363. if (priv->prs_shadow[index].valid) {
  1364. /* Entry exist - update port only */
  1365. pe.index = index;
  1366. mvpp2_prs_hw_read(priv, &pe);
  1367. } else {
  1368. /* Entry doesn't exist - create new */
  1369. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1370. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
  1371. pe.index = index;
  1372. /* Continue - set next lookup */
  1373. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
  1374. /* Set result info bits */
  1375. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_MCAST,
  1376. MVPP2_PRS_RI_L2_CAST_MASK);
  1377. /* Update tcam entry data first byte */
  1378. mvpp2_prs_tcam_data_byte_set(&pe, 0, da_mc, 0xff);
  1379. /* Shift to ethertype */
  1380. mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
  1381. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1382. /* Mask all ports */
  1383. mvpp2_prs_tcam_port_map_set(&pe, 0);
  1384. /* Update shadow table */
  1385. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
  1386. }
  1387. /* Update port mask */
  1388. mvpp2_prs_tcam_port_set(&pe, port, add);
  1389. mvpp2_prs_hw_write(priv, &pe);
  1390. }
  1391. /* Parser per-port initialization */
  1392. static void mvpp2_prs_hw_port_init(struct mvpp2 *priv, int port, int lu_first,
  1393. int lu_max, int offset)
  1394. {
  1395. u32 val;
  1396. /* Set lookup ID */
  1397. val = mvpp2_read(priv, MVPP2_PRS_INIT_LOOKUP_REG);
  1398. val &= ~MVPP2_PRS_PORT_LU_MASK(port);
  1399. val |= MVPP2_PRS_PORT_LU_VAL(port, lu_first);
  1400. mvpp2_write(priv, MVPP2_PRS_INIT_LOOKUP_REG, val);
  1401. /* Set maximum number of loops for packet received from port */
  1402. val = mvpp2_read(priv, MVPP2_PRS_MAX_LOOP_REG(port));
  1403. val &= ~MVPP2_PRS_MAX_LOOP_MASK(port);
  1404. val |= MVPP2_PRS_MAX_LOOP_VAL(port, lu_max);
  1405. mvpp2_write(priv, MVPP2_PRS_MAX_LOOP_REG(port), val);
  1406. /* Set initial offset for packet header extraction for the first
  1407. * searching loop
  1408. */
  1409. val = mvpp2_read(priv, MVPP2_PRS_INIT_OFFS_REG(port));
  1410. val &= ~MVPP2_PRS_INIT_OFF_MASK(port);
  1411. val |= MVPP2_PRS_INIT_OFF_VAL(port, offset);
  1412. mvpp2_write(priv, MVPP2_PRS_INIT_OFFS_REG(port), val);
  1413. }
  1414. /* Default flow entries initialization for all ports */
  1415. static void mvpp2_prs_def_flow_init(struct mvpp2 *priv)
  1416. {
  1417. struct mvpp2_prs_entry pe;
  1418. int port;
  1419. for (port = 0; port < MVPP2_MAX_PORTS; port++) {
  1420. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1421. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  1422. pe.index = MVPP2_PE_FIRST_DEFAULT_FLOW - port;
  1423. /* Mask all ports */
  1424. mvpp2_prs_tcam_port_map_set(&pe, 0);
  1425. /* Set flow ID*/
  1426. mvpp2_prs_sram_ai_update(&pe, port, MVPP2_PRS_FLOW_ID_MASK);
  1427. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
  1428. /* Update shadow table and hw entry */
  1429. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_FLOWS);
  1430. mvpp2_prs_hw_write(priv, &pe);
  1431. }
  1432. }
  1433. /* Set default entry for Marvell Header field */
  1434. static void mvpp2_prs_mh_init(struct mvpp2 *priv)
  1435. {
  1436. struct mvpp2_prs_entry pe;
  1437. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1438. pe.index = MVPP2_PE_MH_DEFAULT;
  1439. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MH);
  1440. mvpp2_prs_sram_shift_set(&pe, MVPP2_MH_SIZE,
  1441. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1442. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_MAC);
  1443. /* Unmask all ports */
  1444. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  1445. /* Update shadow table and hw entry */
  1446. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MH);
  1447. mvpp2_prs_hw_write(priv, &pe);
  1448. }
  1449. /* Set default entires (place holder) for promiscuous, non-promiscuous and
  1450. * multicast MAC addresses
  1451. */
  1452. static void mvpp2_prs_mac_init(struct mvpp2 *priv)
  1453. {
  1454. struct mvpp2_prs_entry pe;
  1455. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1456. /* Non-promiscuous mode for all ports - DROP unknown packets */
  1457. pe.index = MVPP2_PE_MAC_NON_PROMISCUOUS;
  1458. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
  1459. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
  1460. MVPP2_PRS_RI_DROP_MASK);
  1461. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  1462. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  1463. /* Unmask all ports */
  1464. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  1465. /* Update shadow table and hw entry */
  1466. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
  1467. mvpp2_prs_hw_write(priv, &pe);
  1468. /* place holders only - no ports */
  1469. mvpp2_prs_mac_drop_all_set(priv, 0, false);
  1470. mvpp2_prs_mac_promisc_set(priv, 0, false);
  1471. mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_ALL, 0, false);
  1472. mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_IP6, 0, false);
  1473. }
  1474. /* Match basic ethertypes */
  1475. static int mvpp2_prs_etype_init(struct mvpp2 *priv)
  1476. {
  1477. struct mvpp2_prs_entry pe;
  1478. int tid;
  1479. /* Ethertype: PPPoE */
  1480. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1481. MVPP2_PE_LAST_FREE_TID);
  1482. if (tid < 0)
  1483. return tid;
  1484. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1485. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  1486. pe.index = tid;
  1487. mvpp2_prs_match_etype(&pe, 0, PROT_PPP_SES);
  1488. mvpp2_prs_sram_shift_set(&pe, MVPP2_PPPOE_HDR_SIZE,
  1489. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1490. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
  1491. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_PPPOE_MASK,
  1492. MVPP2_PRS_RI_PPPOE_MASK);
  1493. /* Update shadow table and hw entry */
  1494. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1495. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1496. priv->prs_shadow[pe.index].finish = false;
  1497. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_PPPOE_MASK,
  1498. MVPP2_PRS_RI_PPPOE_MASK);
  1499. mvpp2_prs_hw_write(priv, &pe);
  1500. /* Ethertype: ARP */
  1501. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1502. MVPP2_PE_LAST_FREE_TID);
  1503. if (tid < 0)
  1504. return tid;
  1505. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1506. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  1507. pe.index = tid;
  1508. mvpp2_prs_match_etype(&pe, 0, PROT_ARP);
  1509. /* Generate flow in the next iteration*/
  1510. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  1511. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  1512. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_ARP,
  1513. MVPP2_PRS_RI_L3_PROTO_MASK);
  1514. /* Set L3 offset */
  1515. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  1516. MVPP2_ETH_TYPE_LEN,
  1517. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  1518. /* Update shadow table and hw entry */
  1519. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1520. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1521. priv->prs_shadow[pe.index].finish = true;
  1522. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_ARP,
  1523. MVPP2_PRS_RI_L3_PROTO_MASK);
  1524. mvpp2_prs_hw_write(priv, &pe);
  1525. /* Ethertype: LBTD */
  1526. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1527. MVPP2_PE_LAST_FREE_TID);
  1528. if (tid < 0)
  1529. return tid;
  1530. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1531. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  1532. pe.index = tid;
  1533. mvpp2_prs_match_etype(&pe, 0, MVPP2_IP_LBDT_TYPE);
  1534. /* Generate flow in the next iteration*/
  1535. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  1536. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  1537. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
  1538. MVPP2_PRS_RI_UDF3_RX_SPECIAL,
  1539. MVPP2_PRS_RI_CPU_CODE_MASK |
  1540. MVPP2_PRS_RI_UDF3_MASK);
  1541. /* Set L3 offset */
  1542. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  1543. MVPP2_ETH_TYPE_LEN,
  1544. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  1545. /* Update shadow table and hw entry */
  1546. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1547. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1548. priv->prs_shadow[pe.index].finish = true;
  1549. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
  1550. MVPP2_PRS_RI_UDF3_RX_SPECIAL,
  1551. MVPP2_PRS_RI_CPU_CODE_MASK |
  1552. MVPP2_PRS_RI_UDF3_MASK);
  1553. mvpp2_prs_hw_write(priv, &pe);
  1554. /* Ethertype: IPv4 without options */
  1555. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1556. MVPP2_PE_LAST_FREE_TID);
  1557. if (tid < 0)
  1558. return tid;
  1559. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1560. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  1561. pe.index = tid;
  1562. mvpp2_prs_match_etype(&pe, 0, PROT_IP);
  1563. mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
  1564. MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
  1565. MVPP2_PRS_IPV4_HEAD_MASK |
  1566. MVPP2_PRS_IPV4_IHL_MASK);
  1567. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
  1568. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
  1569. MVPP2_PRS_RI_L3_PROTO_MASK);
  1570. /* Skip eth_type + 4 bytes of IP header */
  1571. mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
  1572. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1573. /* Set L3 offset */
  1574. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  1575. MVPP2_ETH_TYPE_LEN,
  1576. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  1577. /* Update shadow table and hw entry */
  1578. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1579. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1580. priv->prs_shadow[pe.index].finish = false;
  1581. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4,
  1582. MVPP2_PRS_RI_L3_PROTO_MASK);
  1583. mvpp2_prs_hw_write(priv, &pe);
  1584. /* Ethertype: IPv4 with options */
  1585. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1586. MVPP2_PE_LAST_FREE_TID);
  1587. if (tid < 0)
  1588. return tid;
  1589. pe.index = tid;
  1590. /* Clear tcam data before updating */
  1591. pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(MVPP2_ETH_TYPE_LEN)] = 0x0;
  1592. pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(MVPP2_ETH_TYPE_LEN)] = 0x0;
  1593. mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
  1594. MVPP2_PRS_IPV4_HEAD,
  1595. MVPP2_PRS_IPV4_HEAD_MASK);
  1596. /* Clear ri before updating */
  1597. pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
  1598. pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
  1599. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
  1600. MVPP2_PRS_RI_L3_PROTO_MASK);
  1601. /* Update shadow table and hw entry */
  1602. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1603. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1604. priv->prs_shadow[pe.index].finish = false;
  1605. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4_OPT,
  1606. MVPP2_PRS_RI_L3_PROTO_MASK);
  1607. mvpp2_prs_hw_write(priv, &pe);
  1608. /* Ethertype: IPv6 without options */
  1609. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1610. MVPP2_PE_LAST_FREE_TID);
  1611. if (tid < 0)
  1612. return tid;
  1613. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1614. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  1615. pe.index = tid;
  1616. mvpp2_prs_match_etype(&pe, 0, PROT_IPV6);
  1617. /* Skip DIP of IPV6 header */
  1618. mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 8 +
  1619. MVPP2_MAX_L3_ADDR_SIZE,
  1620. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1621. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
  1622. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
  1623. MVPP2_PRS_RI_L3_PROTO_MASK);
  1624. /* Set L3 offset */
  1625. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  1626. MVPP2_ETH_TYPE_LEN,
  1627. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  1628. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1629. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1630. priv->prs_shadow[pe.index].finish = false;
  1631. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP6,
  1632. MVPP2_PRS_RI_L3_PROTO_MASK);
  1633. mvpp2_prs_hw_write(priv, &pe);
  1634. /* Default entry for MVPP2_PRS_LU_L2 - Unknown ethtype */
  1635. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1636. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  1637. pe.index = MVPP2_PE_ETH_TYPE_UN;
  1638. /* Unmask all ports */
  1639. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  1640. /* Generate flow in the next iteration*/
  1641. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  1642. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  1643. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
  1644. MVPP2_PRS_RI_L3_PROTO_MASK);
  1645. /* Set L3 offset even it's unknown L3 */
  1646. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  1647. MVPP2_ETH_TYPE_LEN,
  1648. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  1649. /* Update shadow table and hw entry */
  1650. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1651. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1652. priv->prs_shadow[pe.index].finish = true;
  1653. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_UN,
  1654. MVPP2_PRS_RI_L3_PROTO_MASK);
  1655. mvpp2_prs_hw_write(priv, &pe);
  1656. return 0;
  1657. }
  1658. /* Parser default initialization */
  1659. static int mvpp2_prs_default_init(struct udevice *dev,
  1660. struct mvpp2 *priv)
  1661. {
  1662. int err, index, i;
  1663. /* Enable tcam table */
  1664. mvpp2_write(priv, MVPP2_PRS_TCAM_CTRL_REG, MVPP2_PRS_TCAM_EN_MASK);
  1665. /* Clear all tcam and sram entries */
  1666. for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++) {
  1667. mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
  1668. for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
  1669. mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), 0);
  1670. mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, index);
  1671. for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
  1672. mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), 0);
  1673. }
  1674. /* Invalidate all tcam entries */
  1675. for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++)
  1676. mvpp2_prs_hw_inv(priv, index);
  1677. priv->prs_shadow = devm_kcalloc(dev, MVPP2_PRS_TCAM_SRAM_SIZE,
  1678. sizeof(struct mvpp2_prs_shadow),
  1679. GFP_KERNEL);
  1680. if (!priv->prs_shadow)
  1681. return -ENOMEM;
  1682. /* Always start from lookup = 0 */
  1683. for (index = 0; index < MVPP2_MAX_PORTS; index++)
  1684. mvpp2_prs_hw_port_init(priv, index, MVPP2_PRS_LU_MH,
  1685. MVPP2_PRS_PORT_LU_MAX, 0);
  1686. mvpp2_prs_def_flow_init(priv);
  1687. mvpp2_prs_mh_init(priv);
  1688. mvpp2_prs_mac_init(priv);
  1689. err = mvpp2_prs_etype_init(priv);
  1690. if (err)
  1691. return err;
  1692. return 0;
  1693. }
  1694. /* Compare MAC DA with tcam entry data */
  1695. static bool mvpp2_prs_mac_range_equals(struct mvpp2_prs_entry *pe,
  1696. const u8 *da, unsigned char *mask)
  1697. {
  1698. unsigned char tcam_byte, tcam_mask;
  1699. int index;
  1700. for (index = 0; index < ETH_ALEN; index++) {
  1701. mvpp2_prs_tcam_data_byte_get(pe, index, &tcam_byte, &tcam_mask);
  1702. if (tcam_mask != mask[index])
  1703. return false;
  1704. if ((tcam_mask & tcam_byte) != (da[index] & mask[index]))
  1705. return false;
  1706. }
  1707. return true;
  1708. }
  1709. /* Find tcam entry with matched pair <MAC DA, port> */
  1710. static struct mvpp2_prs_entry *
  1711. mvpp2_prs_mac_da_range_find(struct mvpp2 *priv, int pmap, const u8 *da,
  1712. unsigned char *mask, int udf_type)
  1713. {
  1714. struct mvpp2_prs_entry *pe;
  1715. int tid;
  1716. pe = kzalloc(sizeof(*pe), GFP_KERNEL);
  1717. if (!pe)
  1718. return NULL;
  1719. mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
  1720. /* Go through the all entires with MVPP2_PRS_LU_MAC */
  1721. for (tid = MVPP2_PE_FIRST_FREE_TID;
  1722. tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
  1723. unsigned int entry_pmap;
  1724. if (!priv->prs_shadow[tid].valid ||
  1725. (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
  1726. (priv->prs_shadow[tid].udf != udf_type))
  1727. continue;
  1728. pe->index = tid;
  1729. mvpp2_prs_hw_read(priv, pe);
  1730. entry_pmap = mvpp2_prs_tcam_port_map_get(pe);
  1731. if (mvpp2_prs_mac_range_equals(pe, da, mask) &&
  1732. entry_pmap == pmap)
  1733. return pe;
  1734. }
  1735. kfree(pe);
  1736. return NULL;
  1737. }
  1738. /* Update parser's mac da entry */
  1739. static int mvpp2_prs_mac_da_accept(struct mvpp2 *priv, int port,
  1740. const u8 *da, bool add)
  1741. {
  1742. struct mvpp2_prs_entry *pe;
  1743. unsigned int pmap, len, ri;
  1744. unsigned char mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  1745. int tid;
  1746. /* Scan TCAM and see if entry with this <MAC DA, port> already exist */
  1747. pe = mvpp2_prs_mac_da_range_find(priv, (1 << port), da, mask,
  1748. MVPP2_PRS_UDF_MAC_DEF);
  1749. /* No such entry */
  1750. if (!pe) {
  1751. if (!add)
  1752. return 0;
  1753. /* Create new TCAM entry */
  1754. /* Find first range mac entry*/
  1755. for (tid = MVPP2_PE_FIRST_FREE_TID;
  1756. tid <= MVPP2_PE_LAST_FREE_TID; tid++)
  1757. if (priv->prs_shadow[tid].valid &&
  1758. (priv->prs_shadow[tid].lu == MVPP2_PRS_LU_MAC) &&
  1759. (priv->prs_shadow[tid].udf ==
  1760. MVPP2_PRS_UDF_MAC_RANGE))
  1761. break;
  1762. /* Go through the all entries from first to last */
  1763. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1764. tid - 1);
  1765. if (tid < 0)
  1766. return tid;
  1767. pe = kzalloc(sizeof(*pe), GFP_KERNEL);
  1768. if (!pe)
  1769. return -1;
  1770. mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
  1771. pe->index = tid;
  1772. /* Mask all ports */
  1773. mvpp2_prs_tcam_port_map_set(pe, 0);
  1774. }
  1775. /* Update port mask */
  1776. mvpp2_prs_tcam_port_set(pe, port, add);
  1777. /* Invalidate the entry if no ports are left enabled */
  1778. pmap = mvpp2_prs_tcam_port_map_get(pe);
  1779. if (pmap == 0) {
  1780. if (add) {
  1781. kfree(pe);
  1782. return -1;
  1783. }
  1784. mvpp2_prs_hw_inv(priv, pe->index);
  1785. priv->prs_shadow[pe->index].valid = false;
  1786. kfree(pe);
  1787. return 0;
  1788. }
  1789. /* Continue - set next lookup */
  1790. mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_DSA);
  1791. /* Set match on DA */
  1792. len = ETH_ALEN;
  1793. while (len--)
  1794. mvpp2_prs_tcam_data_byte_set(pe, len, da[len], 0xff);
  1795. /* Set result info bits */
  1796. ri = MVPP2_PRS_RI_L2_UCAST | MVPP2_PRS_RI_MAC_ME_MASK;
  1797. mvpp2_prs_sram_ri_update(pe, ri, MVPP2_PRS_RI_L2_CAST_MASK |
  1798. MVPP2_PRS_RI_MAC_ME_MASK);
  1799. mvpp2_prs_shadow_ri_set(priv, pe->index, ri, MVPP2_PRS_RI_L2_CAST_MASK |
  1800. MVPP2_PRS_RI_MAC_ME_MASK);
  1801. /* Shift to ethertype */
  1802. mvpp2_prs_sram_shift_set(pe, 2 * ETH_ALEN,
  1803. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1804. /* Update shadow table and hw entry */
  1805. priv->prs_shadow[pe->index].udf = MVPP2_PRS_UDF_MAC_DEF;
  1806. mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_MAC);
  1807. mvpp2_prs_hw_write(priv, pe);
  1808. kfree(pe);
  1809. return 0;
  1810. }
  1811. static int mvpp2_prs_update_mac_da(struct mvpp2_port *port, const u8 *da)
  1812. {
  1813. int err;
  1814. /* Remove old parser entry */
  1815. err = mvpp2_prs_mac_da_accept(port->priv, port->id, port->dev_addr,
  1816. false);
  1817. if (err)
  1818. return err;
  1819. /* Add new parser entry */
  1820. err = mvpp2_prs_mac_da_accept(port->priv, port->id, da, true);
  1821. if (err)
  1822. return err;
  1823. /* Set addr in the device */
  1824. memcpy(port->dev_addr, da, ETH_ALEN);
  1825. return 0;
  1826. }
  1827. /* Set prs flow for the port */
  1828. static int mvpp2_prs_def_flow(struct mvpp2_port *port)
  1829. {
  1830. struct mvpp2_prs_entry *pe;
  1831. int tid;
  1832. pe = mvpp2_prs_flow_find(port->priv, port->id);
  1833. /* Such entry not exist */
  1834. if (!pe) {
  1835. /* Go through the all entires from last to first */
  1836. tid = mvpp2_prs_tcam_first_free(port->priv,
  1837. MVPP2_PE_LAST_FREE_TID,
  1838. MVPP2_PE_FIRST_FREE_TID);
  1839. if (tid < 0)
  1840. return tid;
  1841. pe = kzalloc(sizeof(*pe), GFP_KERNEL);
  1842. if (!pe)
  1843. return -ENOMEM;
  1844. mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
  1845. pe->index = tid;
  1846. /* Set flow ID*/
  1847. mvpp2_prs_sram_ai_update(pe, port->id, MVPP2_PRS_FLOW_ID_MASK);
  1848. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
  1849. /* Update shadow table */
  1850. mvpp2_prs_shadow_set(port->priv, pe->index, MVPP2_PRS_LU_FLOWS);
  1851. }
  1852. mvpp2_prs_tcam_port_map_set(pe, (1 << port->id));
  1853. mvpp2_prs_hw_write(port->priv, pe);
  1854. kfree(pe);
  1855. return 0;
  1856. }
  1857. /* Classifier configuration routines */
  1858. /* Update classification flow table registers */
  1859. static void mvpp2_cls_flow_write(struct mvpp2 *priv,
  1860. struct mvpp2_cls_flow_entry *fe)
  1861. {
  1862. mvpp2_write(priv, MVPP2_CLS_FLOW_INDEX_REG, fe->index);
  1863. mvpp2_write(priv, MVPP2_CLS_FLOW_TBL0_REG, fe->data[0]);
  1864. mvpp2_write(priv, MVPP2_CLS_FLOW_TBL1_REG, fe->data[1]);
  1865. mvpp2_write(priv, MVPP2_CLS_FLOW_TBL2_REG, fe->data[2]);
  1866. }
  1867. /* Update classification lookup table register */
  1868. static void mvpp2_cls_lookup_write(struct mvpp2 *priv,
  1869. struct mvpp2_cls_lookup_entry *le)
  1870. {
  1871. u32 val;
  1872. val = (le->way << MVPP2_CLS_LKP_INDEX_WAY_OFFS) | le->lkpid;
  1873. mvpp2_write(priv, MVPP2_CLS_LKP_INDEX_REG, val);
  1874. mvpp2_write(priv, MVPP2_CLS_LKP_TBL_REG, le->data);
  1875. }
  1876. /* Classifier default initialization */
  1877. static void mvpp2_cls_init(struct mvpp2 *priv)
  1878. {
  1879. struct mvpp2_cls_lookup_entry le;
  1880. struct mvpp2_cls_flow_entry fe;
  1881. int index;
  1882. /* Enable classifier */
  1883. mvpp2_write(priv, MVPP2_CLS_MODE_REG, MVPP2_CLS_MODE_ACTIVE_MASK);
  1884. /* Clear classifier flow table */
  1885. memset(&fe.data, 0, MVPP2_CLS_FLOWS_TBL_DATA_WORDS);
  1886. for (index = 0; index < MVPP2_CLS_FLOWS_TBL_SIZE; index++) {
  1887. fe.index = index;
  1888. mvpp2_cls_flow_write(priv, &fe);
  1889. }
  1890. /* Clear classifier lookup table */
  1891. le.data = 0;
  1892. for (index = 0; index < MVPP2_CLS_LKP_TBL_SIZE; index++) {
  1893. le.lkpid = index;
  1894. le.way = 0;
  1895. mvpp2_cls_lookup_write(priv, &le);
  1896. le.way = 1;
  1897. mvpp2_cls_lookup_write(priv, &le);
  1898. }
  1899. }
  1900. static void mvpp2_cls_port_config(struct mvpp2_port *port)
  1901. {
  1902. struct mvpp2_cls_lookup_entry le;
  1903. u32 val;
  1904. /* Set way for the port */
  1905. val = mvpp2_read(port->priv, MVPP2_CLS_PORT_WAY_REG);
  1906. val &= ~MVPP2_CLS_PORT_WAY_MASK(port->id);
  1907. mvpp2_write(port->priv, MVPP2_CLS_PORT_WAY_REG, val);
  1908. /* Pick the entry to be accessed in lookup ID decoding table
  1909. * according to the way and lkpid.
  1910. */
  1911. le.lkpid = port->id;
  1912. le.way = 0;
  1913. le.data = 0;
  1914. /* Set initial CPU queue for receiving packets */
  1915. le.data &= ~MVPP2_CLS_LKP_TBL_RXQ_MASK;
  1916. le.data |= port->first_rxq;
  1917. /* Disable classification engines */
  1918. le.data &= ~MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK;
  1919. /* Update lookup ID table entry */
  1920. mvpp2_cls_lookup_write(port->priv, &le);
  1921. }
  1922. /* Set CPU queue number for oversize packets */
  1923. static void mvpp2_cls_oversize_rxq_set(struct mvpp2_port *port)
  1924. {
  1925. u32 val;
  1926. mvpp2_write(port->priv, MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port->id),
  1927. port->first_rxq & MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK);
  1928. mvpp2_write(port->priv, MVPP2_CLS_SWFWD_P2HQ_REG(port->id),
  1929. (port->first_rxq >> MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS));
  1930. val = mvpp2_read(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG);
  1931. val |= MVPP2_CLS_SWFWD_PCTRL_MASK(port->id);
  1932. mvpp2_write(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG, val);
  1933. }
  1934. /* Buffer Manager configuration routines */
  1935. /* Create pool */
  1936. static int mvpp2_bm_pool_create(struct udevice *dev,
  1937. struct mvpp2 *priv,
  1938. struct mvpp2_bm_pool *bm_pool, int size)
  1939. {
  1940. u32 val;
  1941. bm_pool->virt_addr = buffer_loc.bm_pool[bm_pool->id];
  1942. bm_pool->dma_addr = (dma_addr_t)buffer_loc.bm_pool[bm_pool->id];
  1943. if (!bm_pool->virt_addr)
  1944. return -ENOMEM;
  1945. if (!IS_ALIGNED((unsigned long)bm_pool->virt_addr,
  1946. MVPP2_BM_POOL_PTR_ALIGN)) {
  1947. dev_err(&pdev->dev, "BM pool %d is not %d bytes aligned\n",
  1948. bm_pool->id, MVPP2_BM_POOL_PTR_ALIGN);
  1949. return -ENOMEM;
  1950. }
  1951. mvpp2_write(priv, MVPP2_BM_POOL_BASE_REG(bm_pool->id),
  1952. bm_pool->dma_addr);
  1953. mvpp2_write(priv, MVPP2_BM_POOL_SIZE_REG(bm_pool->id), size);
  1954. val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
  1955. val |= MVPP2_BM_START_MASK;
  1956. mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
  1957. bm_pool->type = MVPP2_BM_FREE;
  1958. bm_pool->size = size;
  1959. bm_pool->pkt_size = 0;
  1960. bm_pool->buf_num = 0;
  1961. return 0;
  1962. }
  1963. /* Set pool buffer size */
  1964. static void mvpp2_bm_pool_bufsize_set(struct mvpp2 *priv,
  1965. struct mvpp2_bm_pool *bm_pool,
  1966. int buf_size)
  1967. {
  1968. u32 val;
  1969. bm_pool->buf_size = buf_size;
  1970. val = ALIGN(buf_size, 1 << MVPP2_POOL_BUF_SIZE_OFFSET);
  1971. mvpp2_write(priv, MVPP2_POOL_BUF_SIZE_REG(bm_pool->id), val);
  1972. }
  1973. /* Free all buffers from the pool */
  1974. static void mvpp2_bm_bufs_free(struct udevice *dev, struct mvpp2 *priv,
  1975. struct mvpp2_bm_pool *bm_pool)
  1976. {
  1977. bm_pool->buf_num = 0;
  1978. }
  1979. /* Cleanup pool */
  1980. static int mvpp2_bm_pool_destroy(struct udevice *dev,
  1981. struct mvpp2 *priv,
  1982. struct mvpp2_bm_pool *bm_pool)
  1983. {
  1984. u32 val;
  1985. mvpp2_bm_bufs_free(dev, priv, bm_pool);
  1986. if (bm_pool->buf_num) {
  1987. dev_err(dev, "cannot free all buffers in pool %d\n", bm_pool->id);
  1988. return 0;
  1989. }
  1990. val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
  1991. val |= MVPP2_BM_STOP_MASK;
  1992. mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
  1993. return 0;
  1994. }
  1995. static int mvpp2_bm_pools_init(struct udevice *dev,
  1996. struct mvpp2 *priv)
  1997. {
  1998. int i, err, size;
  1999. struct mvpp2_bm_pool *bm_pool;
  2000. /* Create all pools with maximum size */
  2001. size = MVPP2_BM_POOL_SIZE_MAX;
  2002. for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
  2003. bm_pool = &priv->bm_pools[i];
  2004. bm_pool->id = i;
  2005. err = mvpp2_bm_pool_create(dev, priv, bm_pool, size);
  2006. if (err)
  2007. goto err_unroll_pools;
  2008. mvpp2_bm_pool_bufsize_set(priv, bm_pool, 0);
  2009. }
  2010. return 0;
  2011. err_unroll_pools:
  2012. dev_err(&pdev->dev, "failed to create BM pool %d, size %d\n", i, size);
  2013. for (i = i - 1; i >= 0; i--)
  2014. mvpp2_bm_pool_destroy(dev, priv, &priv->bm_pools[i]);
  2015. return err;
  2016. }
  2017. static int mvpp2_bm_init(struct udevice *dev, struct mvpp2 *priv)
  2018. {
  2019. int i, err;
  2020. for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
  2021. /* Mask BM all interrupts */
  2022. mvpp2_write(priv, MVPP2_BM_INTR_MASK_REG(i), 0);
  2023. /* Clear BM cause register */
  2024. mvpp2_write(priv, MVPP2_BM_INTR_CAUSE_REG(i), 0);
  2025. }
  2026. /* Allocate and initialize BM pools */
  2027. priv->bm_pools = devm_kcalloc(dev, MVPP2_BM_POOLS_NUM,
  2028. sizeof(struct mvpp2_bm_pool), GFP_KERNEL);
  2029. if (!priv->bm_pools)
  2030. return -ENOMEM;
  2031. err = mvpp2_bm_pools_init(dev, priv);
  2032. if (err < 0)
  2033. return err;
  2034. return 0;
  2035. }
  2036. /* Attach long pool to rxq */
  2037. static void mvpp2_rxq_long_pool_set(struct mvpp2_port *port,
  2038. int lrxq, int long_pool)
  2039. {
  2040. u32 val;
  2041. int prxq;
  2042. /* Get queue physical ID */
  2043. prxq = port->rxqs[lrxq]->id;
  2044. val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
  2045. val &= ~MVPP2_RXQ_POOL_LONG_MASK;
  2046. val |= ((long_pool << MVPP2_RXQ_POOL_LONG_OFFS) &
  2047. MVPP2_RXQ_POOL_LONG_MASK);
  2048. mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
  2049. }
  2050. /* Set pool number in a BM cookie */
  2051. static inline u32 mvpp2_bm_cookie_pool_set(u32 cookie, int pool)
  2052. {
  2053. u32 bm;
  2054. bm = cookie & ~(0xFF << MVPP2_BM_COOKIE_POOL_OFFS);
  2055. bm |= ((pool & 0xFF) << MVPP2_BM_COOKIE_POOL_OFFS);
  2056. return bm;
  2057. }
  2058. /* Get pool number from a BM cookie */
  2059. static inline int mvpp2_bm_cookie_pool_get(unsigned long cookie)
  2060. {
  2061. return (cookie >> MVPP2_BM_COOKIE_POOL_OFFS) & 0xFF;
  2062. }
  2063. /* Release buffer to BM */
  2064. static inline void mvpp2_bm_pool_put(struct mvpp2_port *port, int pool,
  2065. dma_addr_t buf_dma_addr,
  2066. unsigned long buf_phys_addr)
  2067. {
  2068. /* MVPP2_BM_VIRT_RLS_REG is not interpreted by HW, and simply
  2069. * returned in the "cookie" field of the RX
  2070. * descriptor. Instead of storing the virtual address, we
  2071. * store the physical address
  2072. */
  2073. mvpp2_write(port->priv, MVPP2_BM_VIRT_RLS_REG, buf_phys_addr);
  2074. mvpp2_write(port->priv, MVPP2_BM_PHY_RLS_REG(pool), buf_dma_addr);
  2075. }
  2076. /* Refill BM pool */
  2077. static void mvpp2_pool_refill(struct mvpp2_port *port, u32 bm,
  2078. dma_addr_t dma_addr,
  2079. phys_addr_t phys_addr)
  2080. {
  2081. int pool = mvpp2_bm_cookie_pool_get(bm);
  2082. mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
  2083. }
  2084. /* Allocate buffers for the pool */
  2085. static int mvpp2_bm_bufs_add(struct mvpp2_port *port,
  2086. struct mvpp2_bm_pool *bm_pool, int buf_num)
  2087. {
  2088. int i;
  2089. if (buf_num < 0 ||
  2090. (buf_num + bm_pool->buf_num > bm_pool->size)) {
  2091. netdev_err(port->dev,
  2092. "cannot allocate %d buffers for pool %d\n",
  2093. buf_num, bm_pool->id);
  2094. return 0;
  2095. }
  2096. for (i = 0; i < buf_num; i++) {
  2097. mvpp2_bm_pool_put(port, bm_pool->id,
  2098. (dma_addr_t)buffer_loc.rx_buffer[i],
  2099. (unsigned long)buffer_loc.rx_buffer[i]);
  2100. }
  2101. /* Update BM driver with number of buffers added to pool */
  2102. bm_pool->buf_num += i;
  2103. bm_pool->in_use_thresh = bm_pool->buf_num / 4;
  2104. return i;
  2105. }
  2106. /* Notify the driver that BM pool is being used as specific type and return the
  2107. * pool pointer on success
  2108. */
  2109. static struct mvpp2_bm_pool *
  2110. mvpp2_bm_pool_use(struct mvpp2_port *port, int pool, enum mvpp2_bm_type type,
  2111. int pkt_size)
  2112. {
  2113. struct mvpp2_bm_pool *new_pool = &port->priv->bm_pools[pool];
  2114. int num;
  2115. if (new_pool->type != MVPP2_BM_FREE && new_pool->type != type) {
  2116. netdev_err(port->dev, "mixing pool types is forbidden\n");
  2117. return NULL;
  2118. }
  2119. if (new_pool->type == MVPP2_BM_FREE)
  2120. new_pool->type = type;
  2121. /* Allocate buffers in case BM pool is used as long pool, but packet
  2122. * size doesn't match MTU or BM pool hasn't being used yet
  2123. */
  2124. if (((type == MVPP2_BM_SWF_LONG) && (pkt_size > new_pool->pkt_size)) ||
  2125. (new_pool->pkt_size == 0)) {
  2126. int pkts_num;
  2127. /* Set default buffer number or free all the buffers in case
  2128. * the pool is not empty
  2129. */
  2130. pkts_num = new_pool->buf_num;
  2131. if (pkts_num == 0)
  2132. pkts_num = type == MVPP2_BM_SWF_LONG ?
  2133. MVPP2_BM_LONG_BUF_NUM :
  2134. MVPP2_BM_SHORT_BUF_NUM;
  2135. else
  2136. mvpp2_bm_bufs_free(NULL,
  2137. port->priv, new_pool);
  2138. new_pool->pkt_size = pkt_size;
  2139. /* Allocate buffers for this pool */
  2140. num = mvpp2_bm_bufs_add(port, new_pool, pkts_num);
  2141. if (num != pkts_num) {
  2142. dev_err(dev, "pool %d: %d of %d allocated\n",
  2143. new_pool->id, num, pkts_num);
  2144. return NULL;
  2145. }
  2146. }
  2147. mvpp2_bm_pool_bufsize_set(port->priv, new_pool,
  2148. MVPP2_RX_BUF_SIZE(new_pool->pkt_size));
  2149. return new_pool;
  2150. }
  2151. /* Initialize pools for swf */
  2152. static int mvpp2_swf_bm_pool_init(struct mvpp2_port *port)
  2153. {
  2154. int rxq;
  2155. if (!port->pool_long) {
  2156. port->pool_long =
  2157. mvpp2_bm_pool_use(port, MVPP2_BM_SWF_LONG_POOL(port->id),
  2158. MVPP2_BM_SWF_LONG,
  2159. port->pkt_size);
  2160. if (!port->pool_long)
  2161. return -ENOMEM;
  2162. port->pool_long->port_map |= (1 << port->id);
  2163. for (rxq = 0; rxq < rxq_number; rxq++)
  2164. mvpp2_rxq_long_pool_set(port, rxq, port->pool_long->id);
  2165. }
  2166. return 0;
  2167. }
  2168. /* Port configuration routines */
  2169. static void mvpp2_port_mii_set(struct mvpp2_port *port)
  2170. {
  2171. u32 val;
  2172. val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
  2173. switch (port->phy_interface) {
  2174. case PHY_INTERFACE_MODE_SGMII:
  2175. val |= MVPP2_GMAC_INBAND_AN_MASK;
  2176. break;
  2177. case PHY_INTERFACE_MODE_RGMII:
  2178. val |= MVPP2_GMAC_PORT_RGMII_MASK;
  2179. default:
  2180. val &= ~MVPP2_GMAC_PCS_ENABLE_MASK;
  2181. }
  2182. writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
  2183. }
  2184. static void mvpp2_port_fc_adv_enable(struct mvpp2_port *port)
  2185. {
  2186. u32 val;
  2187. val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  2188. val |= MVPP2_GMAC_FC_ADV_EN;
  2189. writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  2190. }
  2191. static void mvpp2_port_enable(struct mvpp2_port *port)
  2192. {
  2193. u32 val;
  2194. val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
  2195. val |= MVPP2_GMAC_PORT_EN_MASK;
  2196. val |= MVPP2_GMAC_MIB_CNTR_EN_MASK;
  2197. writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
  2198. }
  2199. static void mvpp2_port_disable(struct mvpp2_port *port)
  2200. {
  2201. u32 val;
  2202. val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
  2203. val &= ~(MVPP2_GMAC_PORT_EN_MASK);
  2204. writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
  2205. }
  2206. /* Set IEEE 802.3x Flow Control Xon Packet Transmission Mode */
  2207. static void mvpp2_port_periodic_xon_disable(struct mvpp2_port *port)
  2208. {
  2209. u32 val;
  2210. val = readl(port->base + MVPP2_GMAC_CTRL_1_REG) &
  2211. ~MVPP2_GMAC_PERIODIC_XON_EN_MASK;
  2212. writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
  2213. }
  2214. /* Configure loopback port */
  2215. static void mvpp2_port_loopback_set(struct mvpp2_port *port)
  2216. {
  2217. u32 val;
  2218. val = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
  2219. if (port->speed == 1000)
  2220. val |= MVPP2_GMAC_GMII_LB_EN_MASK;
  2221. else
  2222. val &= ~MVPP2_GMAC_GMII_LB_EN_MASK;
  2223. if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
  2224. val |= MVPP2_GMAC_PCS_LB_EN_MASK;
  2225. else
  2226. val &= ~MVPP2_GMAC_PCS_LB_EN_MASK;
  2227. writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
  2228. }
  2229. static void mvpp2_port_reset(struct mvpp2_port *port)
  2230. {
  2231. u32 val;
  2232. val = readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
  2233. ~MVPP2_GMAC_PORT_RESET_MASK;
  2234. writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
  2235. while (readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
  2236. MVPP2_GMAC_PORT_RESET_MASK)
  2237. continue;
  2238. }
  2239. /* Change maximum receive size of the port */
  2240. static inline void mvpp2_gmac_max_rx_size_set(struct mvpp2_port *port)
  2241. {
  2242. u32 val;
  2243. val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
  2244. val &= ~MVPP2_GMAC_MAX_RX_SIZE_MASK;
  2245. val |= (((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
  2246. MVPP2_GMAC_MAX_RX_SIZE_OFFS);
  2247. writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
  2248. }
  2249. /* Set defaults to the MVPP2 port */
  2250. static void mvpp2_defaults_set(struct mvpp2_port *port)
  2251. {
  2252. int tx_port_num, val, queue, ptxq, lrxq;
  2253. /* Configure port to loopback if needed */
  2254. if (port->flags & MVPP2_F_LOOPBACK)
  2255. mvpp2_port_loopback_set(port);
  2256. /* Update TX FIFO MIN Threshold */
  2257. val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
  2258. val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
  2259. /* Min. TX threshold must be less than minimal packet length */
  2260. val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(64 - 4 - 2);
  2261. writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
  2262. /* Disable Legacy WRR, Disable EJP, Release from reset */
  2263. tx_port_num = mvpp2_egress_port(port);
  2264. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG,
  2265. tx_port_num);
  2266. mvpp2_write(port->priv, MVPP2_TXP_SCHED_CMD_1_REG, 0);
  2267. /* Close bandwidth for all queues */
  2268. for (queue = 0; queue < MVPP2_MAX_TXQ; queue++) {
  2269. ptxq = mvpp2_txq_phys(port->id, queue);
  2270. mvpp2_write(port->priv,
  2271. MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(ptxq), 0);
  2272. }
  2273. /* Set refill period to 1 usec, refill tokens
  2274. * and bucket size to maximum
  2275. */
  2276. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PERIOD_REG, 0xc8);
  2277. val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_REFILL_REG);
  2278. val &= ~MVPP2_TXP_REFILL_PERIOD_ALL_MASK;
  2279. val |= MVPP2_TXP_REFILL_PERIOD_MASK(1);
  2280. val |= MVPP2_TXP_REFILL_TOKENS_ALL_MASK;
  2281. mvpp2_write(port->priv, MVPP2_TXP_SCHED_REFILL_REG, val);
  2282. val = MVPP2_TXP_TOKEN_SIZE_MAX;
  2283. mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
  2284. /* Set MaximumLowLatencyPacketSize value to 256 */
  2285. mvpp2_write(port->priv, MVPP2_RX_CTRL_REG(port->id),
  2286. MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK |
  2287. MVPP2_RX_LOW_LATENCY_PKT_SIZE(256));
  2288. /* Enable Rx cache snoop */
  2289. for (lrxq = 0; lrxq < rxq_number; lrxq++) {
  2290. queue = port->rxqs[lrxq]->id;
  2291. val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
  2292. val |= MVPP2_SNOOP_PKT_SIZE_MASK |
  2293. MVPP2_SNOOP_BUF_HDR_MASK;
  2294. mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
  2295. }
  2296. }
  2297. /* Enable/disable receiving packets */
  2298. static void mvpp2_ingress_enable(struct mvpp2_port *port)
  2299. {
  2300. u32 val;
  2301. int lrxq, queue;
  2302. for (lrxq = 0; lrxq < rxq_number; lrxq++) {
  2303. queue = port->rxqs[lrxq]->id;
  2304. val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
  2305. val &= ~MVPP2_RXQ_DISABLE_MASK;
  2306. mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
  2307. }
  2308. }
  2309. static void mvpp2_ingress_disable(struct mvpp2_port *port)
  2310. {
  2311. u32 val;
  2312. int lrxq, queue;
  2313. for (lrxq = 0; lrxq < rxq_number; lrxq++) {
  2314. queue = port->rxqs[lrxq]->id;
  2315. val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
  2316. val |= MVPP2_RXQ_DISABLE_MASK;
  2317. mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
  2318. }
  2319. }
  2320. /* Enable transmit via physical egress queue
  2321. * - HW starts take descriptors from DRAM
  2322. */
  2323. static void mvpp2_egress_enable(struct mvpp2_port *port)
  2324. {
  2325. u32 qmap;
  2326. int queue;
  2327. int tx_port_num = mvpp2_egress_port(port);
  2328. /* Enable all initialized TXs. */
  2329. qmap = 0;
  2330. for (queue = 0; queue < txq_number; queue++) {
  2331. struct mvpp2_tx_queue *txq = port->txqs[queue];
  2332. if (txq->descs != NULL)
  2333. qmap |= (1 << queue);
  2334. }
  2335. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
  2336. mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG, qmap);
  2337. }
  2338. /* Disable transmit via physical egress queue
  2339. * - HW doesn't take descriptors from DRAM
  2340. */
  2341. static void mvpp2_egress_disable(struct mvpp2_port *port)
  2342. {
  2343. u32 reg_data;
  2344. int delay;
  2345. int tx_port_num = mvpp2_egress_port(port);
  2346. /* Issue stop command for active channels only */
  2347. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
  2348. reg_data = (mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG)) &
  2349. MVPP2_TXP_SCHED_ENQ_MASK;
  2350. if (reg_data != 0)
  2351. mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG,
  2352. (reg_data << MVPP2_TXP_SCHED_DISQ_OFFSET));
  2353. /* Wait for all Tx activity to terminate. */
  2354. delay = 0;
  2355. do {
  2356. if (delay >= MVPP2_TX_DISABLE_TIMEOUT_MSEC) {
  2357. netdev_warn(port->dev,
  2358. "Tx stop timed out, status=0x%08x\n",
  2359. reg_data);
  2360. break;
  2361. }
  2362. mdelay(1);
  2363. delay++;
  2364. /* Check port TX Command register that all
  2365. * Tx queues are stopped
  2366. */
  2367. reg_data = mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG);
  2368. } while (reg_data & MVPP2_TXP_SCHED_ENQ_MASK);
  2369. }
  2370. /* Rx descriptors helper methods */
  2371. /* Get number of Rx descriptors occupied by received packets */
  2372. static inline int
  2373. mvpp2_rxq_received(struct mvpp2_port *port, int rxq_id)
  2374. {
  2375. u32 val = mvpp2_read(port->priv, MVPP2_RXQ_STATUS_REG(rxq_id));
  2376. return val & MVPP2_RXQ_OCCUPIED_MASK;
  2377. }
  2378. /* Update Rx queue status with the number of occupied and available
  2379. * Rx descriptor slots.
  2380. */
  2381. static inline void
  2382. mvpp2_rxq_status_update(struct mvpp2_port *port, int rxq_id,
  2383. int used_count, int free_count)
  2384. {
  2385. /* Decrement the number of used descriptors and increment count
  2386. * increment the number of free descriptors.
  2387. */
  2388. u32 val = used_count | (free_count << MVPP2_RXQ_NUM_NEW_OFFSET);
  2389. mvpp2_write(port->priv, MVPP2_RXQ_STATUS_UPDATE_REG(rxq_id), val);
  2390. }
  2391. /* Get pointer to next RX descriptor to be processed by SW */
  2392. static inline struct mvpp2_rx_desc *
  2393. mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue *rxq)
  2394. {
  2395. int rx_desc = rxq->next_desc_to_proc;
  2396. rxq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(rxq, rx_desc);
  2397. prefetch(rxq->descs + rxq->next_desc_to_proc);
  2398. return rxq->descs + rx_desc;
  2399. }
  2400. /* Set rx queue offset */
  2401. static void mvpp2_rxq_offset_set(struct mvpp2_port *port,
  2402. int prxq, int offset)
  2403. {
  2404. u32 val;
  2405. /* Convert offset from bytes to units of 32 bytes */
  2406. offset = offset >> 5;
  2407. val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
  2408. val &= ~MVPP2_RXQ_PACKET_OFFSET_MASK;
  2409. /* Offset is in */
  2410. val |= ((offset << MVPP2_RXQ_PACKET_OFFSET_OFFS) &
  2411. MVPP2_RXQ_PACKET_OFFSET_MASK);
  2412. mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
  2413. }
  2414. /* Obtain BM cookie information from descriptor */
  2415. static u32 mvpp2_bm_cookie_build(struct mvpp2_port *port,
  2416. struct mvpp2_rx_desc *rx_desc)
  2417. {
  2418. int cpu = smp_processor_id();
  2419. int pool;
  2420. pool = (mvpp2_rxdesc_status_get(port, rx_desc) &
  2421. MVPP2_RXD_BM_POOL_ID_MASK) >>
  2422. MVPP2_RXD_BM_POOL_ID_OFFS;
  2423. return ((pool & 0xFF) << MVPP2_BM_COOKIE_POOL_OFFS) |
  2424. ((cpu & 0xFF) << MVPP2_BM_COOKIE_CPU_OFFS);
  2425. }
  2426. /* Tx descriptors helper methods */
  2427. /* Get number of Tx descriptors waiting to be transmitted by HW */
  2428. static int mvpp2_txq_pend_desc_num_get(struct mvpp2_port *port,
  2429. struct mvpp2_tx_queue *txq)
  2430. {
  2431. u32 val;
  2432. mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
  2433. val = mvpp2_read(port->priv, MVPP2_TXQ_PENDING_REG);
  2434. return val & MVPP2_TXQ_PENDING_MASK;
  2435. }
  2436. /* Get pointer to next Tx descriptor to be processed (send) by HW */
  2437. static struct mvpp2_tx_desc *
  2438. mvpp2_txq_next_desc_get(struct mvpp2_tx_queue *txq)
  2439. {
  2440. int tx_desc = txq->next_desc_to_proc;
  2441. txq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(txq, tx_desc);
  2442. return txq->descs + tx_desc;
  2443. }
  2444. /* Update HW with number of aggregated Tx descriptors to be sent */
  2445. static void mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port *port, int pending)
  2446. {
  2447. /* aggregated access - relevant TXQ number is written in TX desc */
  2448. mvpp2_write(port->priv, MVPP2_AGGR_TXQ_UPDATE_REG, pending);
  2449. }
  2450. /* Get number of sent descriptors and decrement counter.
  2451. * The number of sent descriptors is returned.
  2452. * Per-CPU access
  2453. */
  2454. static inline int mvpp2_txq_sent_desc_proc(struct mvpp2_port *port,
  2455. struct mvpp2_tx_queue *txq)
  2456. {
  2457. u32 val;
  2458. /* Reading status reg resets transmitted descriptor counter */
  2459. val = mvpp2_read(port->priv, MVPP2_TXQ_SENT_REG(txq->id));
  2460. return (val & MVPP2_TRANSMITTED_COUNT_MASK) >>
  2461. MVPP2_TRANSMITTED_COUNT_OFFSET;
  2462. }
  2463. static void mvpp2_txq_sent_counter_clear(void *arg)
  2464. {
  2465. struct mvpp2_port *port = arg;
  2466. int queue;
  2467. for (queue = 0; queue < txq_number; queue++) {
  2468. int id = port->txqs[queue]->id;
  2469. mvpp2_read(port->priv, MVPP2_TXQ_SENT_REG(id));
  2470. }
  2471. }
  2472. /* Set max sizes for Tx queues */
  2473. static void mvpp2_txp_max_tx_size_set(struct mvpp2_port *port)
  2474. {
  2475. u32 val, size, mtu;
  2476. int txq, tx_port_num;
  2477. mtu = port->pkt_size * 8;
  2478. if (mtu > MVPP2_TXP_MTU_MAX)
  2479. mtu = MVPP2_TXP_MTU_MAX;
  2480. /* WA for wrong Token bucket update: Set MTU value = 3*real MTU value */
  2481. mtu = 3 * mtu;
  2482. /* Indirect access to registers */
  2483. tx_port_num = mvpp2_egress_port(port);
  2484. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
  2485. /* Set MTU */
  2486. val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_MTU_REG);
  2487. val &= ~MVPP2_TXP_MTU_MAX;
  2488. val |= mtu;
  2489. mvpp2_write(port->priv, MVPP2_TXP_SCHED_MTU_REG, val);
  2490. /* TXP token size and all TXQs token size must be larger that MTU */
  2491. val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG);
  2492. size = val & MVPP2_TXP_TOKEN_SIZE_MAX;
  2493. if (size < mtu) {
  2494. size = mtu;
  2495. val &= ~MVPP2_TXP_TOKEN_SIZE_MAX;
  2496. val |= size;
  2497. mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
  2498. }
  2499. for (txq = 0; txq < txq_number; txq++) {
  2500. val = mvpp2_read(port->priv,
  2501. MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq));
  2502. size = val & MVPP2_TXQ_TOKEN_SIZE_MAX;
  2503. if (size < mtu) {
  2504. size = mtu;
  2505. val &= ~MVPP2_TXQ_TOKEN_SIZE_MAX;
  2506. val |= size;
  2507. mvpp2_write(port->priv,
  2508. MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq),
  2509. val);
  2510. }
  2511. }
  2512. }
  2513. /* Free Tx queue skbuffs */
  2514. static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
  2515. struct mvpp2_tx_queue *txq,
  2516. struct mvpp2_txq_pcpu *txq_pcpu, int num)
  2517. {
  2518. int i;
  2519. for (i = 0; i < num; i++)
  2520. mvpp2_txq_inc_get(txq_pcpu);
  2521. }
  2522. static inline struct mvpp2_rx_queue *mvpp2_get_rx_queue(struct mvpp2_port *port,
  2523. u32 cause)
  2524. {
  2525. int queue = fls(cause) - 1;
  2526. return port->rxqs[queue];
  2527. }
  2528. static inline struct mvpp2_tx_queue *mvpp2_get_tx_queue(struct mvpp2_port *port,
  2529. u32 cause)
  2530. {
  2531. int queue = fls(cause) - 1;
  2532. return port->txqs[queue];
  2533. }
  2534. /* Rx/Tx queue initialization/cleanup methods */
  2535. /* Allocate and initialize descriptors for aggr TXQ */
  2536. static int mvpp2_aggr_txq_init(struct udevice *dev,
  2537. struct mvpp2_tx_queue *aggr_txq,
  2538. int desc_num, int cpu,
  2539. struct mvpp2 *priv)
  2540. {
  2541. /* Allocate memory for TX descriptors */
  2542. aggr_txq->descs = buffer_loc.aggr_tx_descs;
  2543. aggr_txq->descs_dma = (dma_addr_t)buffer_loc.aggr_tx_descs;
  2544. if (!aggr_txq->descs)
  2545. return -ENOMEM;
  2546. /* Make sure descriptor address is cache line size aligned */
  2547. BUG_ON(aggr_txq->descs !=
  2548. PTR_ALIGN(aggr_txq->descs, MVPP2_CPU_D_CACHE_LINE_SIZE));
  2549. aggr_txq->last_desc = aggr_txq->size - 1;
  2550. /* Aggr TXQ no reset WA */
  2551. aggr_txq->next_desc_to_proc = mvpp2_read(priv,
  2552. MVPP2_AGGR_TXQ_INDEX_REG(cpu));
  2553. /* Set Tx descriptors queue starting address */
  2554. /* indirect access */
  2555. mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu),
  2556. aggr_txq->descs_dma);
  2557. mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu), desc_num);
  2558. return 0;
  2559. }
  2560. /* Create a specified Rx queue */
  2561. static int mvpp2_rxq_init(struct mvpp2_port *port,
  2562. struct mvpp2_rx_queue *rxq)
  2563. {
  2564. rxq->size = port->rx_ring_size;
  2565. /* Allocate memory for RX descriptors */
  2566. rxq->descs = buffer_loc.rx_descs;
  2567. rxq->descs_dma = (dma_addr_t)buffer_loc.rx_descs;
  2568. if (!rxq->descs)
  2569. return -ENOMEM;
  2570. BUG_ON(rxq->descs !=
  2571. PTR_ALIGN(rxq->descs, MVPP2_CPU_D_CACHE_LINE_SIZE));
  2572. rxq->last_desc = rxq->size - 1;
  2573. /* Zero occupied and non-occupied counters - direct access */
  2574. mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
  2575. /* Set Rx descriptors queue starting address - indirect access */
  2576. mvpp2_write(port->priv, MVPP2_RXQ_NUM_REG, rxq->id);
  2577. mvpp2_write(port->priv, MVPP2_RXQ_DESC_ADDR_REG, rxq->descs_dma);
  2578. mvpp2_write(port->priv, MVPP2_RXQ_DESC_SIZE_REG, rxq->size);
  2579. mvpp2_write(port->priv, MVPP2_RXQ_INDEX_REG, 0);
  2580. /* Set Offset */
  2581. mvpp2_rxq_offset_set(port, rxq->id, NET_SKB_PAD);
  2582. /* Add number of descriptors ready for receiving packets */
  2583. mvpp2_rxq_status_update(port, rxq->id, 0, rxq->size);
  2584. return 0;
  2585. }
  2586. /* Push packets received by the RXQ to BM pool */
  2587. static void mvpp2_rxq_drop_pkts(struct mvpp2_port *port,
  2588. struct mvpp2_rx_queue *rxq)
  2589. {
  2590. int rx_received, i;
  2591. rx_received = mvpp2_rxq_received(port, rxq->id);
  2592. if (!rx_received)
  2593. return;
  2594. for (i = 0; i < rx_received; i++) {
  2595. struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
  2596. u32 bm = mvpp2_bm_cookie_build(port, rx_desc);
  2597. mvpp2_pool_refill(port, bm,
  2598. mvpp2_rxdesc_dma_addr_get(port, rx_desc),
  2599. mvpp2_rxdesc_cookie_get(port, rx_desc));
  2600. }
  2601. mvpp2_rxq_status_update(port, rxq->id, rx_received, rx_received);
  2602. }
  2603. /* Cleanup Rx queue */
  2604. static void mvpp2_rxq_deinit(struct mvpp2_port *port,
  2605. struct mvpp2_rx_queue *rxq)
  2606. {
  2607. mvpp2_rxq_drop_pkts(port, rxq);
  2608. rxq->descs = NULL;
  2609. rxq->last_desc = 0;
  2610. rxq->next_desc_to_proc = 0;
  2611. rxq->descs_dma = 0;
  2612. /* Clear Rx descriptors queue starting address and size;
  2613. * free descriptor number
  2614. */
  2615. mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
  2616. mvpp2_write(port->priv, MVPP2_RXQ_NUM_REG, rxq->id);
  2617. mvpp2_write(port->priv, MVPP2_RXQ_DESC_ADDR_REG, 0);
  2618. mvpp2_write(port->priv, MVPP2_RXQ_DESC_SIZE_REG, 0);
  2619. }
  2620. /* Create and initialize a Tx queue */
  2621. static int mvpp2_txq_init(struct mvpp2_port *port,
  2622. struct mvpp2_tx_queue *txq)
  2623. {
  2624. u32 val;
  2625. int cpu, desc, desc_per_txq, tx_port_num;
  2626. struct mvpp2_txq_pcpu *txq_pcpu;
  2627. txq->size = port->tx_ring_size;
  2628. /* Allocate memory for Tx descriptors */
  2629. txq->descs = buffer_loc.tx_descs;
  2630. txq->descs_dma = (dma_addr_t)buffer_loc.tx_descs;
  2631. if (!txq->descs)
  2632. return -ENOMEM;
  2633. /* Make sure descriptor address is cache line size aligned */
  2634. BUG_ON(txq->descs !=
  2635. PTR_ALIGN(txq->descs, MVPP2_CPU_D_CACHE_LINE_SIZE));
  2636. txq->last_desc = txq->size - 1;
  2637. /* Set Tx descriptors queue starting address - indirect access */
  2638. mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
  2639. mvpp2_write(port->priv, MVPP2_TXQ_DESC_ADDR_REG, txq->descs_dma);
  2640. mvpp2_write(port->priv, MVPP2_TXQ_DESC_SIZE_REG, txq->size &
  2641. MVPP2_TXQ_DESC_SIZE_MASK);
  2642. mvpp2_write(port->priv, MVPP2_TXQ_INDEX_REG, 0);
  2643. mvpp2_write(port->priv, MVPP2_TXQ_RSVD_CLR_REG,
  2644. txq->id << MVPP2_TXQ_RSVD_CLR_OFFSET);
  2645. val = mvpp2_read(port->priv, MVPP2_TXQ_PENDING_REG);
  2646. val &= ~MVPP2_TXQ_PENDING_MASK;
  2647. mvpp2_write(port->priv, MVPP2_TXQ_PENDING_REG, val);
  2648. /* Calculate base address in prefetch buffer. We reserve 16 descriptors
  2649. * for each existing TXQ.
  2650. * TCONTS for PON port must be continuous from 0 to MVPP2_MAX_TCONT
  2651. * GBE ports assumed to be continious from 0 to MVPP2_MAX_PORTS
  2652. */
  2653. desc_per_txq = 16;
  2654. desc = (port->id * MVPP2_MAX_TXQ * desc_per_txq) +
  2655. (txq->log_id * desc_per_txq);
  2656. mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG,
  2657. MVPP2_PREF_BUF_PTR(desc) | MVPP2_PREF_BUF_SIZE_16 |
  2658. MVPP2_PREF_BUF_THRESH(desc_per_txq/2));
  2659. /* WRR / EJP configuration - indirect access */
  2660. tx_port_num = mvpp2_egress_port(port);
  2661. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
  2662. val = mvpp2_read(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id));
  2663. val &= ~MVPP2_TXQ_REFILL_PERIOD_ALL_MASK;
  2664. val |= MVPP2_TXQ_REFILL_PERIOD_MASK(1);
  2665. val |= MVPP2_TXQ_REFILL_TOKENS_ALL_MASK;
  2666. mvpp2_write(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id), val);
  2667. val = MVPP2_TXQ_TOKEN_SIZE_MAX;
  2668. mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq->log_id),
  2669. val);
  2670. for_each_present_cpu(cpu) {
  2671. txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
  2672. txq_pcpu->size = txq->size;
  2673. }
  2674. return 0;
  2675. }
  2676. /* Free allocated TXQ resources */
  2677. static void mvpp2_txq_deinit(struct mvpp2_port *port,
  2678. struct mvpp2_tx_queue *txq)
  2679. {
  2680. txq->descs = NULL;
  2681. txq->last_desc = 0;
  2682. txq->next_desc_to_proc = 0;
  2683. txq->descs_dma = 0;
  2684. /* Set minimum bandwidth for disabled TXQs */
  2685. mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(txq->id), 0);
  2686. /* Set Tx descriptors queue starting address and size */
  2687. mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
  2688. mvpp2_write(port->priv, MVPP2_TXQ_DESC_ADDR_REG, 0);
  2689. mvpp2_write(port->priv, MVPP2_TXQ_DESC_SIZE_REG, 0);
  2690. }
  2691. /* Cleanup Tx ports */
  2692. static void mvpp2_txq_clean(struct mvpp2_port *port, struct mvpp2_tx_queue *txq)
  2693. {
  2694. struct mvpp2_txq_pcpu *txq_pcpu;
  2695. int delay, pending, cpu;
  2696. u32 val;
  2697. mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
  2698. val = mvpp2_read(port->priv, MVPP2_TXQ_PREF_BUF_REG);
  2699. val |= MVPP2_TXQ_DRAIN_EN_MASK;
  2700. mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG, val);
  2701. /* The napi queue has been stopped so wait for all packets
  2702. * to be transmitted.
  2703. */
  2704. delay = 0;
  2705. do {
  2706. if (delay >= MVPP2_TX_PENDING_TIMEOUT_MSEC) {
  2707. netdev_warn(port->dev,
  2708. "port %d: cleaning queue %d timed out\n",
  2709. port->id, txq->log_id);
  2710. break;
  2711. }
  2712. mdelay(1);
  2713. delay++;
  2714. pending = mvpp2_txq_pend_desc_num_get(port, txq);
  2715. } while (pending);
  2716. val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
  2717. mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG, val);
  2718. for_each_present_cpu(cpu) {
  2719. txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
  2720. /* Release all packets */
  2721. mvpp2_txq_bufs_free(port, txq, txq_pcpu, txq_pcpu->count);
  2722. /* Reset queue */
  2723. txq_pcpu->count = 0;
  2724. txq_pcpu->txq_put_index = 0;
  2725. txq_pcpu->txq_get_index = 0;
  2726. }
  2727. }
  2728. /* Cleanup all Tx queues */
  2729. static void mvpp2_cleanup_txqs(struct mvpp2_port *port)
  2730. {
  2731. struct mvpp2_tx_queue *txq;
  2732. int queue;
  2733. u32 val;
  2734. val = mvpp2_read(port->priv, MVPP2_TX_PORT_FLUSH_REG);
  2735. /* Reset Tx ports and delete Tx queues */
  2736. val |= MVPP2_TX_PORT_FLUSH_MASK(port->id);
  2737. mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
  2738. for (queue = 0; queue < txq_number; queue++) {
  2739. txq = port->txqs[queue];
  2740. mvpp2_txq_clean(port, txq);
  2741. mvpp2_txq_deinit(port, txq);
  2742. }
  2743. mvpp2_txq_sent_counter_clear(port);
  2744. val &= ~MVPP2_TX_PORT_FLUSH_MASK(port->id);
  2745. mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
  2746. }
  2747. /* Cleanup all Rx queues */
  2748. static void mvpp2_cleanup_rxqs(struct mvpp2_port *port)
  2749. {
  2750. int queue;
  2751. for (queue = 0; queue < rxq_number; queue++)
  2752. mvpp2_rxq_deinit(port, port->rxqs[queue]);
  2753. }
  2754. /* Init all Rx queues for port */
  2755. static int mvpp2_setup_rxqs(struct mvpp2_port *port)
  2756. {
  2757. int queue, err;
  2758. for (queue = 0; queue < rxq_number; queue++) {
  2759. err = mvpp2_rxq_init(port, port->rxqs[queue]);
  2760. if (err)
  2761. goto err_cleanup;
  2762. }
  2763. return 0;
  2764. err_cleanup:
  2765. mvpp2_cleanup_rxqs(port);
  2766. return err;
  2767. }
  2768. /* Init all tx queues for port */
  2769. static int mvpp2_setup_txqs(struct mvpp2_port *port)
  2770. {
  2771. struct mvpp2_tx_queue *txq;
  2772. int queue, err;
  2773. for (queue = 0; queue < txq_number; queue++) {
  2774. txq = port->txqs[queue];
  2775. err = mvpp2_txq_init(port, txq);
  2776. if (err)
  2777. goto err_cleanup;
  2778. }
  2779. mvpp2_txq_sent_counter_clear(port);
  2780. return 0;
  2781. err_cleanup:
  2782. mvpp2_cleanup_txqs(port);
  2783. return err;
  2784. }
  2785. /* Adjust link */
  2786. static void mvpp2_link_event(struct mvpp2_port *port)
  2787. {
  2788. struct phy_device *phydev = port->phy_dev;
  2789. int status_change = 0;
  2790. u32 val;
  2791. if (phydev->link) {
  2792. if ((port->speed != phydev->speed) ||
  2793. (port->duplex != phydev->duplex)) {
  2794. u32 val;
  2795. val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  2796. val &= ~(MVPP2_GMAC_CONFIG_MII_SPEED |
  2797. MVPP2_GMAC_CONFIG_GMII_SPEED |
  2798. MVPP2_GMAC_CONFIG_FULL_DUPLEX |
  2799. MVPP2_GMAC_AN_SPEED_EN |
  2800. MVPP2_GMAC_AN_DUPLEX_EN);
  2801. if (phydev->duplex)
  2802. val |= MVPP2_GMAC_CONFIG_FULL_DUPLEX;
  2803. if (phydev->speed == SPEED_1000)
  2804. val |= MVPP2_GMAC_CONFIG_GMII_SPEED;
  2805. else if (phydev->speed == SPEED_100)
  2806. val |= MVPP2_GMAC_CONFIG_MII_SPEED;
  2807. writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  2808. port->duplex = phydev->duplex;
  2809. port->speed = phydev->speed;
  2810. }
  2811. }
  2812. if (phydev->link != port->link) {
  2813. if (!phydev->link) {
  2814. port->duplex = -1;
  2815. port->speed = 0;
  2816. }
  2817. port->link = phydev->link;
  2818. status_change = 1;
  2819. }
  2820. if (status_change) {
  2821. if (phydev->link) {
  2822. val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  2823. val |= (MVPP2_GMAC_FORCE_LINK_PASS |
  2824. MVPP2_GMAC_FORCE_LINK_DOWN);
  2825. writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  2826. mvpp2_egress_enable(port);
  2827. mvpp2_ingress_enable(port);
  2828. } else {
  2829. mvpp2_ingress_disable(port);
  2830. mvpp2_egress_disable(port);
  2831. }
  2832. }
  2833. }
  2834. /* Main RX/TX processing routines */
  2835. /* Display more error info */
  2836. static void mvpp2_rx_error(struct mvpp2_port *port,
  2837. struct mvpp2_rx_desc *rx_desc)
  2838. {
  2839. u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
  2840. size_t sz = mvpp2_rxdesc_size_get(port, rx_desc);
  2841. switch (status & MVPP2_RXD_ERR_CODE_MASK) {
  2842. case MVPP2_RXD_ERR_CRC:
  2843. netdev_err(port->dev, "bad rx status %08x (crc error), size=%zu\n",
  2844. status, sz);
  2845. break;
  2846. case MVPP2_RXD_ERR_OVERRUN:
  2847. netdev_err(port->dev, "bad rx status %08x (overrun error), size=%zu\n",
  2848. status, sz);
  2849. break;
  2850. case MVPP2_RXD_ERR_RESOURCE:
  2851. netdev_err(port->dev, "bad rx status %08x (resource error), size=%zu\n",
  2852. status, sz);
  2853. break;
  2854. }
  2855. }
  2856. /* Reuse skb if possible, or allocate a new skb and add it to BM pool */
  2857. static int mvpp2_rx_refill(struct mvpp2_port *port,
  2858. struct mvpp2_bm_pool *bm_pool,
  2859. u32 bm, dma_addr_t dma_addr)
  2860. {
  2861. mvpp2_pool_refill(port, bm, dma_addr, (unsigned long)dma_addr);
  2862. return 0;
  2863. }
  2864. /* Set hw internals when starting port */
  2865. static void mvpp2_start_dev(struct mvpp2_port *port)
  2866. {
  2867. mvpp2_gmac_max_rx_size_set(port);
  2868. mvpp2_txp_max_tx_size_set(port);
  2869. mvpp2_port_enable(port);
  2870. }
  2871. /* Set hw internals when stopping port */
  2872. static void mvpp2_stop_dev(struct mvpp2_port *port)
  2873. {
  2874. /* Stop new packets from arriving to RXQs */
  2875. mvpp2_ingress_disable(port);
  2876. mvpp2_egress_disable(port);
  2877. mvpp2_port_disable(port);
  2878. }
  2879. static int mvpp2_phy_connect(struct udevice *dev, struct mvpp2_port *port)
  2880. {
  2881. struct phy_device *phy_dev;
  2882. if (!port->init || port->link == 0) {
  2883. phy_dev = phy_connect(port->priv->bus, port->phyaddr, dev,
  2884. port->phy_interface);
  2885. port->phy_dev = phy_dev;
  2886. if (!phy_dev) {
  2887. netdev_err(port->dev, "cannot connect to phy\n");
  2888. return -ENODEV;
  2889. }
  2890. phy_dev->supported &= PHY_GBIT_FEATURES;
  2891. phy_dev->advertising = phy_dev->supported;
  2892. port->phy_dev = phy_dev;
  2893. port->link = 0;
  2894. port->duplex = 0;
  2895. port->speed = 0;
  2896. phy_config(phy_dev);
  2897. phy_startup(phy_dev);
  2898. if (!phy_dev->link) {
  2899. printf("%s: No link\n", phy_dev->dev->name);
  2900. return -1;
  2901. }
  2902. port->init = 1;
  2903. } else {
  2904. mvpp2_egress_enable(port);
  2905. mvpp2_ingress_enable(port);
  2906. }
  2907. return 0;
  2908. }
  2909. static int mvpp2_open(struct udevice *dev, struct mvpp2_port *port)
  2910. {
  2911. unsigned char mac_bcast[ETH_ALEN] = {
  2912. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  2913. int err;
  2914. err = mvpp2_prs_mac_da_accept(port->priv, port->id, mac_bcast, true);
  2915. if (err) {
  2916. netdev_err(dev, "mvpp2_prs_mac_da_accept BC failed\n");
  2917. return err;
  2918. }
  2919. err = mvpp2_prs_mac_da_accept(port->priv, port->id,
  2920. port->dev_addr, true);
  2921. if (err) {
  2922. netdev_err(dev, "mvpp2_prs_mac_da_accept MC failed\n");
  2923. return err;
  2924. }
  2925. err = mvpp2_prs_def_flow(port);
  2926. if (err) {
  2927. netdev_err(dev, "mvpp2_prs_def_flow failed\n");
  2928. return err;
  2929. }
  2930. /* Allocate the Rx/Tx queues */
  2931. err = mvpp2_setup_rxqs(port);
  2932. if (err) {
  2933. netdev_err(port->dev, "cannot allocate Rx queues\n");
  2934. return err;
  2935. }
  2936. err = mvpp2_setup_txqs(port);
  2937. if (err) {
  2938. netdev_err(port->dev, "cannot allocate Tx queues\n");
  2939. return err;
  2940. }
  2941. err = mvpp2_phy_connect(dev, port);
  2942. if (err < 0)
  2943. return err;
  2944. mvpp2_link_event(port);
  2945. mvpp2_start_dev(port);
  2946. return 0;
  2947. }
  2948. /* No Device ops here in U-Boot */
  2949. /* Driver initialization */
  2950. static void mvpp2_port_power_up(struct mvpp2_port *port)
  2951. {
  2952. mvpp2_port_mii_set(port);
  2953. mvpp2_port_periodic_xon_disable(port);
  2954. mvpp2_port_fc_adv_enable(port);
  2955. mvpp2_port_reset(port);
  2956. }
  2957. /* Initialize port HW */
  2958. static int mvpp2_port_init(struct udevice *dev, struct mvpp2_port *port)
  2959. {
  2960. struct mvpp2 *priv = port->priv;
  2961. struct mvpp2_txq_pcpu *txq_pcpu;
  2962. int queue, cpu, err;
  2963. if (port->first_rxq + rxq_number > MVPP2_RXQ_TOTAL_NUM)
  2964. return -EINVAL;
  2965. /* Disable port */
  2966. mvpp2_egress_disable(port);
  2967. mvpp2_port_disable(port);
  2968. port->txqs = devm_kcalloc(dev, txq_number, sizeof(*port->txqs),
  2969. GFP_KERNEL);
  2970. if (!port->txqs)
  2971. return -ENOMEM;
  2972. /* Associate physical Tx queues to this port and initialize.
  2973. * The mapping is predefined.
  2974. */
  2975. for (queue = 0; queue < txq_number; queue++) {
  2976. int queue_phy_id = mvpp2_txq_phys(port->id, queue);
  2977. struct mvpp2_tx_queue *txq;
  2978. txq = devm_kzalloc(dev, sizeof(*txq), GFP_KERNEL);
  2979. if (!txq)
  2980. return -ENOMEM;
  2981. txq->pcpu = devm_kzalloc(dev, sizeof(struct mvpp2_txq_pcpu),
  2982. GFP_KERNEL);
  2983. if (!txq->pcpu)
  2984. return -ENOMEM;
  2985. txq->id = queue_phy_id;
  2986. txq->log_id = queue;
  2987. txq->done_pkts_coal = MVPP2_TXDONE_COAL_PKTS_THRESH;
  2988. for_each_present_cpu(cpu) {
  2989. txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
  2990. txq_pcpu->cpu = cpu;
  2991. }
  2992. port->txqs[queue] = txq;
  2993. }
  2994. port->rxqs = devm_kcalloc(dev, rxq_number, sizeof(*port->rxqs),
  2995. GFP_KERNEL);
  2996. if (!port->rxqs)
  2997. return -ENOMEM;
  2998. /* Allocate and initialize Rx queue for this port */
  2999. for (queue = 0; queue < rxq_number; queue++) {
  3000. struct mvpp2_rx_queue *rxq;
  3001. /* Map physical Rx queue to port's logical Rx queue */
  3002. rxq = devm_kzalloc(dev, sizeof(*rxq), GFP_KERNEL);
  3003. if (!rxq)
  3004. return -ENOMEM;
  3005. /* Map this Rx queue to a physical queue */
  3006. rxq->id = port->first_rxq + queue;
  3007. rxq->port = port->id;
  3008. rxq->logic_rxq = queue;
  3009. port->rxqs[queue] = rxq;
  3010. }
  3011. /* Configure Rx queue group interrupt for this port */
  3012. mvpp2_write(priv, MVPP2_ISR_RXQ_GROUP_REG(port->id), CONFIG_MV_ETH_RXQ);
  3013. /* Create Rx descriptor rings */
  3014. for (queue = 0; queue < rxq_number; queue++) {
  3015. struct mvpp2_rx_queue *rxq = port->rxqs[queue];
  3016. rxq->size = port->rx_ring_size;
  3017. rxq->pkts_coal = MVPP2_RX_COAL_PKTS;
  3018. rxq->time_coal = MVPP2_RX_COAL_USEC;
  3019. }
  3020. mvpp2_ingress_disable(port);
  3021. /* Port default configuration */
  3022. mvpp2_defaults_set(port);
  3023. /* Port's classifier configuration */
  3024. mvpp2_cls_oversize_rxq_set(port);
  3025. mvpp2_cls_port_config(port);
  3026. /* Provide an initial Rx packet size */
  3027. port->pkt_size = MVPP2_RX_PKT_SIZE(PKTSIZE_ALIGN);
  3028. /* Initialize pools for swf */
  3029. err = mvpp2_swf_bm_pool_init(port);
  3030. if (err)
  3031. return err;
  3032. return 0;
  3033. }
  3034. /* Ports initialization */
  3035. static int mvpp2_port_probe(struct udevice *dev,
  3036. struct mvpp2_port *port,
  3037. int port_node,
  3038. struct mvpp2 *priv,
  3039. int *next_first_rxq)
  3040. {
  3041. int phy_node;
  3042. u32 id;
  3043. u32 phyaddr;
  3044. const char *phy_mode_str;
  3045. int phy_mode = -1;
  3046. int priv_common_regs_num = 2;
  3047. int err;
  3048. phy_node = fdtdec_lookup_phandle(gd->fdt_blob, port_node, "phy");
  3049. if (phy_node < 0) {
  3050. dev_err(&pdev->dev, "missing phy\n");
  3051. return -ENODEV;
  3052. }
  3053. phy_mode_str = fdt_getprop(gd->fdt_blob, port_node, "phy-mode", NULL);
  3054. if (phy_mode_str)
  3055. phy_mode = phy_get_interface_by_name(phy_mode_str);
  3056. if (phy_mode == -1) {
  3057. dev_err(&pdev->dev, "incorrect phy mode\n");
  3058. return -EINVAL;
  3059. }
  3060. id = fdtdec_get_int(gd->fdt_blob, port_node, "port-id", -1);
  3061. if (id == -1) {
  3062. dev_err(&pdev->dev, "missing port-id value\n");
  3063. return -EINVAL;
  3064. }
  3065. phyaddr = fdtdec_get_int(gd->fdt_blob, phy_node, "reg", 0);
  3066. port->priv = priv;
  3067. port->id = id;
  3068. port->first_rxq = *next_first_rxq;
  3069. port->phy_node = phy_node;
  3070. port->phy_interface = phy_mode;
  3071. port->phyaddr = phyaddr;
  3072. port->base = (void __iomem *)dev_get_addr_index(dev->parent,
  3073. priv_common_regs_num
  3074. + id);
  3075. if (IS_ERR(port->base))
  3076. return PTR_ERR(port->base);
  3077. port->tx_ring_size = MVPP2_MAX_TXD;
  3078. port->rx_ring_size = MVPP2_MAX_RXD;
  3079. err = mvpp2_port_init(dev, port);
  3080. if (err < 0) {
  3081. dev_err(&pdev->dev, "failed to init port %d\n", id);
  3082. return err;
  3083. }
  3084. mvpp2_port_power_up(port);
  3085. /* Increment the first Rx queue number to be used by the next port */
  3086. *next_first_rxq += CONFIG_MV_ETH_RXQ;
  3087. priv->port_list[id] = port;
  3088. return 0;
  3089. }
  3090. /* Initialize decoding windows */
  3091. static void mvpp2_conf_mbus_windows(const struct mbus_dram_target_info *dram,
  3092. struct mvpp2 *priv)
  3093. {
  3094. u32 win_enable;
  3095. int i;
  3096. for (i = 0; i < 6; i++) {
  3097. mvpp2_write(priv, MVPP2_WIN_BASE(i), 0);
  3098. mvpp2_write(priv, MVPP2_WIN_SIZE(i), 0);
  3099. if (i < 4)
  3100. mvpp2_write(priv, MVPP2_WIN_REMAP(i), 0);
  3101. }
  3102. win_enable = 0;
  3103. for (i = 0; i < dram->num_cs; i++) {
  3104. const struct mbus_dram_window *cs = dram->cs + i;
  3105. mvpp2_write(priv, MVPP2_WIN_BASE(i),
  3106. (cs->base & 0xffff0000) | (cs->mbus_attr << 8) |
  3107. dram->mbus_dram_target_id);
  3108. mvpp2_write(priv, MVPP2_WIN_SIZE(i),
  3109. (cs->size - 1) & 0xffff0000);
  3110. win_enable |= (1 << i);
  3111. }
  3112. mvpp2_write(priv, MVPP2_BASE_ADDR_ENABLE, win_enable);
  3113. }
  3114. /* Initialize Rx FIFO's */
  3115. static void mvpp2_rx_fifo_init(struct mvpp2 *priv)
  3116. {
  3117. int port;
  3118. for (port = 0; port < MVPP2_MAX_PORTS; port++) {
  3119. mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
  3120. MVPP2_RX_FIFO_PORT_DATA_SIZE);
  3121. mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
  3122. MVPP2_RX_FIFO_PORT_ATTR_SIZE);
  3123. }
  3124. mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
  3125. MVPP2_RX_FIFO_PORT_MIN_PKT);
  3126. mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
  3127. }
  3128. /* Initialize network controller common part HW */
  3129. static int mvpp2_init(struct udevice *dev, struct mvpp2 *priv)
  3130. {
  3131. const struct mbus_dram_target_info *dram_target_info;
  3132. int err, i;
  3133. u32 val;
  3134. /* Checks for hardware constraints (U-Boot uses only one rxq) */
  3135. if ((rxq_number > MVPP2_MAX_RXQ) || (txq_number > MVPP2_MAX_TXQ)) {
  3136. dev_err(&pdev->dev, "invalid queue size parameter\n");
  3137. return -EINVAL;
  3138. }
  3139. /* MBUS windows configuration */
  3140. dram_target_info = mvebu_mbus_dram_info();
  3141. if (dram_target_info)
  3142. mvpp2_conf_mbus_windows(dram_target_info, priv);
  3143. /* Disable HW PHY polling */
  3144. val = readl(priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
  3145. val |= MVPP2_PHY_AN_STOP_SMI0_MASK;
  3146. writel(val, priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
  3147. /* Allocate and initialize aggregated TXQs */
  3148. priv->aggr_txqs = devm_kcalloc(dev, num_present_cpus(),
  3149. sizeof(struct mvpp2_tx_queue),
  3150. GFP_KERNEL);
  3151. if (!priv->aggr_txqs)
  3152. return -ENOMEM;
  3153. for_each_present_cpu(i) {
  3154. priv->aggr_txqs[i].id = i;
  3155. priv->aggr_txqs[i].size = MVPP2_AGGR_TXQ_SIZE;
  3156. err = mvpp2_aggr_txq_init(dev, &priv->aggr_txqs[i],
  3157. MVPP2_AGGR_TXQ_SIZE, i, priv);
  3158. if (err < 0)
  3159. return err;
  3160. }
  3161. /* Rx Fifo Init */
  3162. mvpp2_rx_fifo_init(priv);
  3163. /* Reset Rx queue group interrupt configuration */
  3164. for (i = 0; i < MVPP2_MAX_PORTS; i++)
  3165. mvpp2_write(priv, MVPP2_ISR_RXQ_GROUP_REG(i),
  3166. CONFIG_MV_ETH_RXQ);
  3167. writel(MVPP2_EXT_GLOBAL_CTRL_DEFAULT,
  3168. priv->lms_base + MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG);
  3169. /* Allow cache snoop when transmiting packets */
  3170. mvpp2_write(priv, MVPP2_TX_SNOOP_REG, 0x1);
  3171. /* Buffer Manager initialization */
  3172. err = mvpp2_bm_init(dev, priv);
  3173. if (err < 0)
  3174. return err;
  3175. /* Parser default initialization */
  3176. err = mvpp2_prs_default_init(dev, priv);
  3177. if (err < 0)
  3178. return err;
  3179. /* Classifier default initialization */
  3180. mvpp2_cls_init(priv);
  3181. return 0;
  3182. }
  3183. /* SMI / MDIO functions */
  3184. static int smi_wait_ready(struct mvpp2 *priv)
  3185. {
  3186. u32 timeout = MVPP2_SMI_TIMEOUT;
  3187. u32 smi_reg;
  3188. /* wait till the SMI is not busy */
  3189. do {
  3190. /* read smi register */
  3191. smi_reg = readl(priv->lms_base + MVPP2_SMI);
  3192. if (timeout-- == 0) {
  3193. printf("Error: SMI busy timeout\n");
  3194. return -EFAULT;
  3195. }
  3196. } while (smi_reg & MVPP2_SMI_BUSY);
  3197. return 0;
  3198. }
  3199. /*
  3200. * mpp2_mdio_read - miiphy_read callback function.
  3201. *
  3202. * Returns 16bit phy register value, or 0xffff on error
  3203. */
  3204. static int mpp2_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
  3205. {
  3206. struct mvpp2 *priv = bus->priv;
  3207. u32 smi_reg;
  3208. u32 timeout;
  3209. /* check parameters */
  3210. if (addr > MVPP2_PHY_ADDR_MASK) {
  3211. printf("Error: Invalid PHY address %d\n", addr);
  3212. return -EFAULT;
  3213. }
  3214. if (reg > MVPP2_PHY_REG_MASK) {
  3215. printf("Err: Invalid register offset %d\n", reg);
  3216. return -EFAULT;
  3217. }
  3218. /* wait till the SMI is not busy */
  3219. if (smi_wait_ready(priv) < 0)
  3220. return -EFAULT;
  3221. /* fill the phy address and regiser offset and read opcode */
  3222. smi_reg = (addr << MVPP2_SMI_DEV_ADDR_OFFS)
  3223. | (reg << MVPP2_SMI_REG_ADDR_OFFS)
  3224. | MVPP2_SMI_OPCODE_READ;
  3225. /* write the smi register */
  3226. writel(smi_reg, priv->lms_base + MVPP2_SMI);
  3227. /* wait till read value is ready */
  3228. timeout = MVPP2_SMI_TIMEOUT;
  3229. do {
  3230. /* read smi register */
  3231. smi_reg = readl(priv->lms_base + MVPP2_SMI);
  3232. if (timeout-- == 0) {
  3233. printf("Err: SMI read ready timeout\n");
  3234. return -EFAULT;
  3235. }
  3236. } while (!(smi_reg & MVPP2_SMI_READ_VALID));
  3237. /* Wait for the data to update in the SMI register */
  3238. for (timeout = 0; timeout < MVPP2_SMI_TIMEOUT; timeout++)
  3239. ;
  3240. return readl(priv->lms_base + MVPP2_SMI) & MVPP2_SMI_DATA_MASK;
  3241. }
  3242. /*
  3243. * mpp2_mdio_write - miiphy_write callback function.
  3244. *
  3245. * Returns 0 if write succeed, -EINVAL on bad parameters
  3246. * -ETIME on timeout
  3247. */
  3248. static int mpp2_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
  3249. u16 value)
  3250. {
  3251. struct mvpp2 *priv = bus->priv;
  3252. u32 smi_reg;
  3253. /* check parameters */
  3254. if (addr > MVPP2_PHY_ADDR_MASK) {
  3255. printf("Error: Invalid PHY address %d\n", addr);
  3256. return -EFAULT;
  3257. }
  3258. if (reg > MVPP2_PHY_REG_MASK) {
  3259. printf("Err: Invalid register offset %d\n", reg);
  3260. return -EFAULT;
  3261. }
  3262. /* wait till the SMI is not busy */
  3263. if (smi_wait_ready(priv) < 0)
  3264. return -EFAULT;
  3265. /* fill the phy addr and reg offset and write opcode and data */
  3266. smi_reg = value << MVPP2_SMI_DATA_OFFS;
  3267. smi_reg |= (addr << MVPP2_SMI_DEV_ADDR_OFFS)
  3268. | (reg << MVPP2_SMI_REG_ADDR_OFFS);
  3269. smi_reg &= ~MVPP2_SMI_OPCODE_READ;
  3270. /* write the smi register */
  3271. writel(smi_reg, priv->lms_base + MVPP2_SMI);
  3272. return 0;
  3273. }
  3274. static int mvpp2_recv(struct udevice *dev, int flags, uchar **packetp)
  3275. {
  3276. struct mvpp2_port *port = dev_get_priv(dev);
  3277. struct mvpp2_rx_desc *rx_desc;
  3278. struct mvpp2_bm_pool *bm_pool;
  3279. dma_addr_t dma_addr;
  3280. u32 bm, rx_status;
  3281. int pool, rx_bytes, err;
  3282. int rx_received;
  3283. struct mvpp2_rx_queue *rxq;
  3284. u32 cause_rx_tx, cause_rx, cause_misc;
  3285. u8 *data;
  3286. cause_rx_tx = mvpp2_read(port->priv,
  3287. MVPP2_ISR_RX_TX_CAUSE_REG(port->id));
  3288. cause_rx_tx &= ~MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
  3289. cause_misc = cause_rx_tx & MVPP2_CAUSE_MISC_SUM_MASK;
  3290. if (!cause_rx_tx && !cause_misc)
  3291. return 0;
  3292. cause_rx = cause_rx_tx & MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
  3293. /* Process RX packets */
  3294. cause_rx |= port->pending_cause_rx;
  3295. rxq = mvpp2_get_rx_queue(port, cause_rx);
  3296. /* Get number of received packets and clamp the to-do */
  3297. rx_received = mvpp2_rxq_received(port, rxq->id);
  3298. /* Return if no packets are received */
  3299. if (!rx_received)
  3300. return 0;
  3301. rx_desc = mvpp2_rxq_next_desc_get(rxq);
  3302. rx_status = mvpp2_rxdesc_status_get(port, rx_desc);
  3303. rx_bytes = mvpp2_rxdesc_size_get(port, rx_desc);
  3304. rx_bytes -= MVPP2_MH_SIZE;
  3305. dma_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
  3306. bm = mvpp2_bm_cookie_build(port, rx_desc);
  3307. pool = mvpp2_bm_cookie_pool_get(bm);
  3308. bm_pool = &port->priv->bm_pools[pool];
  3309. /* In case of an error, release the requested buffer pointer
  3310. * to the Buffer Manager. This request process is controlled
  3311. * by the hardware, and the information about the buffer is
  3312. * comprised by the RX descriptor.
  3313. */
  3314. if (rx_status & MVPP2_RXD_ERR_SUMMARY) {
  3315. mvpp2_rx_error(port, rx_desc);
  3316. /* Return the buffer to the pool */
  3317. mvpp2_pool_refill(port, bm, dma_addr, dma_addr);
  3318. return 0;
  3319. }
  3320. err = mvpp2_rx_refill(port, bm_pool, bm, dma_addr);
  3321. if (err) {
  3322. netdev_err(port->dev, "failed to refill BM pools\n");
  3323. return 0;
  3324. }
  3325. /* Update Rx queue management counters */
  3326. mb();
  3327. mvpp2_rxq_status_update(port, rxq->id, 1, 1);
  3328. /* give packet to stack - skip on first n bytes */
  3329. data = (u8 *)dma_addr + 2 + 32;
  3330. if (rx_bytes <= 0)
  3331. return 0;
  3332. /*
  3333. * No cache invalidation needed here, since the rx_buffer's are
  3334. * located in a uncached memory region
  3335. */
  3336. *packetp = data;
  3337. return rx_bytes;
  3338. }
  3339. /* Drain Txq */
  3340. static void mvpp2_txq_drain(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
  3341. int enable)
  3342. {
  3343. u32 val;
  3344. mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
  3345. val = mvpp2_read(port->priv, MVPP2_TXQ_PREF_BUF_REG);
  3346. if (enable)
  3347. val |= MVPP2_TXQ_DRAIN_EN_MASK;
  3348. else
  3349. val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
  3350. mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG, val);
  3351. }
  3352. static int mvpp2_send(struct udevice *dev, void *packet, int length)
  3353. {
  3354. struct mvpp2_port *port = dev_get_priv(dev);
  3355. struct mvpp2_tx_queue *txq, *aggr_txq;
  3356. struct mvpp2_tx_desc *tx_desc;
  3357. int tx_done;
  3358. int timeout;
  3359. txq = port->txqs[0];
  3360. aggr_txq = &port->priv->aggr_txqs[smp_processor_id()];
  3361. /* Get a descriptor for the first part of the packet */
  3362. tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
  3363. mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
  3364. mvpp2_txdesc_size_set(port, tx_desc, length);
  3365. mvpp2_txdesc_offset_set(port, tx_desc,
  3366. (dma_addr_t)packet & MVPP2_TX_DESC_ALIGN);
  3367. mvpp2_txdesc_dma_addr_set(port, tx_desc,
  3368. (dma_addr_t)packet & ~MVPP2_TX_DESC_ALIGN);
  3369. /* First and Last descriptor */
  3370. mvpp2_txdesc_cmd_set(port, tx_desc,
  3371. MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE
  3372. | MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC);
  3373. /* Flush tx data */
  3374. flush_dcache_range((unsigned long)packet,
  3375. (unsigned long)packet + ALIGN(length, PKTALIGN));
  3376. /* Enable transmit */
  3377. mb();
  3378. mvpp2_aggr_txq_pend_desc_add(port, 1);
  3379. mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
  3380. timeout = 0;
  3381. do {
  3382. if (timeout++ > 10000) {
  3383. printf("timeout: packet not sent from aggregated to phys TXQ\n");
  3384. return 0;
  3385. }
  3386. tx_done = mvpp2_txq_pend_desc_num_get(port, txq);
  3387. } while (tx_done);
  3388. /* Enable TXQ drain */
  3389. mvpp2_txq_drain(port, txq, 1);
  3390. timeout = 0;
  3391. do {
  3392. if (timeout++ > 10000) {
  3393. printf("timeout: packet not sent\n");
  3394. return 0;
  3395. }
  3396. tx_done = mvpp2_txq_sent_desc_proc(port, txq);
  3397. } while (!tx_done);
  3398. /* Disable TXQ drain */
  3399. mvpp2_txq_drain(port, txq, 0);
  3400. return 0;
  3401. }
  3402. static int mvpp2_start(struct udevice *dev)
  3403. {
  3404. struct eth_pdata *pdata = dev_get_platdata(dev);
  3405. struct mvpp2_port *port = dev_get_priv(dev);
  3406. /* Load current MAC address */
  3407. memcpy(port->dev_addr, pdata->enetaddr, ETH_ALEN);
  3408. /* Reconfigure parser accept the original MAC address */
  3409. mvpp2_prs_update_mac_da(port, port->dev_addr);
  3410. mvpp2_port_power_up(port);
  3411. mvpp2_open(dev, port);
  3412. return 0;
  3413. }
  3414. static void mvpp2_stop(struct udevice *dev)
  3415. {
  3416. struct mvpp2_port *port = dev_get_priv(dev);
  3417. mvpp2_stop_dev(port);
  3418. mvpp2_cleanup_rxqs(port);
  3419. mvpp2_cleanup_txqs(port);
  3420. }
  3421. static int mvpp2_probe(struct udevice *dev)
  3422. {
  3423. struct mvpp2_port *port = dev_get_priv(dev);
  3424. struct mvpp2 *priv = dev_get_priv(dev->parent);
  3425. int err;
  3426. /* Initialize network controller */
  3427. err = mvpp2_init(dev, priv);
  3428. if (err < 0) {
  3429. dev_err(&pdev->dev, "failed to initialize controller\n");
  3430. return err;
  3431. }
  3432. return mvpp2_port_probe(dev, port, dev_of_offset(dev), priv,
  3433. &buffer_loc.first_rxq);
  3434. }
  3435. static const struct eth_ops mvpp2_ops = {
  3436. .start = mvpp2_start,
  3437. .send = mvpp2_send,
  3438. .recv = mvpp2_recv,
  3439. .stop = mvpp2_stop,
  3440. };
  3441. static struct driver mvpp2_driver = {
  3442. .name = "mvpp2",
  3443. .id = UCLASS_ETH,
  3444. .probe = mvpp2_probe,
  3445. .ops = &mvpp2_ops,
  3446. .priv_auto_alloc_size = sizeof(struct mvpp2_port),
  3447. .platdata_auto_alloc_size = sizeof(struct eth_pdata),
  3448. };
  3449. /*
  3450. * Use a MISC device to bind the n instances (child nodes) of the
  3451. * network base controller in UCLASS_ETH.
  3452. */
  3453. static int mvpp2_base_probe(struct udevice *dev)
  3454. {
  3455. struct mvpp2 *priv = dev_get_priv(dev);
  3456. struct mii_dev *bus;
  3457. void *bd_space;
  3458. u32 size = 0;
  3459. int i;
  3460. /* Save hw-version */
  3461. priv->hw_version = dev_get_driver_data(dev);
  3462. /*
  3463. * U-Boot special buffer handling:
  3464. *
  3465. * Allocate buffer area for descs and rx_buffers. This is only
  3466. * done once for all interfaces. As only one interface can
  3467. * be active. Make this area DMA-safe by disabling the D-cache
  3468. */
  3469. /* Align buffer area for descs and rx_buffers to 1MiB */
  3470. bd_space = memalign(1 << MMU_SECTION_SHIFT, BD_SPACE);
  3471. mmu_set_region_dcache_behaviour((unsigned long)bd_space,
  3472. BD_SPACE, DCACHE_OFF);
  3473. buffer_loc.aggr_tx_descs = (struct mvpp2_tx_desc *)bd_space;
  3474. size += MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE;
  3475. buffer_loc.tx_descs =
  3476. (struct mvpp2_tx_desc *)((unsigned long)bd_space + size);
  3477. size += MVPP2_MAX_TXD * MVPP2_DESC_ALIGNED_SIZE;
  3478. buffer_loc.rx_descs =
  3479. (struct mvpp2_rx_desc *)((unsigned long)bd_space + size);
  3480. size += MVPP2_MAX_RXD * MVPP2_DESC_ALIGNED_SIZE;
  3481. for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
  3482. buffer_loc.bm_pool[i] =
  3483. (unsigned long *)((unsigned long)bd_space + size);
  3484. size += MVPP2_BM_POOL_SIZE_MAX * sizeof(u32);
  3485. }
  3486. for (i = 0; i < MVPP2_BM_LONG_BUF_NUM; i++) {
  3487. buffer_loc.rx_buffer[i] =
  3488. (unsigned long *)((unsigned long)bd_space + size);
  3489. size += RX_BUFFER_SIZE;
  3490. }
  3491. /* Save base addresses for later use */
  3492. priv->base = (void *)dev_get_addr_index(dev, 0);
  3493. if (IS_ERR(priv->base))
  3494. return PTR_ERR(priv->base);
  3495. priv->lms_base = (void *)dev_get_addr_index(dev, 1);
  3496. if (IS_ERR(priv->lms_base))
  3497. return PTR_ERR(priv->lms_base);
  3498. /* Finally create and register the MDIO bus driver */
  3499. bus = mdio_alloc();
  3500. if (!bus) {
  3501. printf("Failed to allocate MDIO bus\n");
  3502. return -ENOMEM;
  3503. }
  3504. bus->read = mpp2_mdio_read;
  3505. bus->write = mpp2_mdio_write;
  3506. snprintf(bus->name, sizeof(bus->name), dev->name);
  3507. bus->priv = (void *)priv;
  3508. priv->bus = bus;
  3509. return mdio_register(bus);
  3510. }
  3511. static int mvpp2_base_bind(struct udevice *parent)
  3512. {
  3513. const void *blob = gd->fdt_blob;
  3514. int node = dev_of_offset(parent);
  3515. struct uclass_driver *drv;
  3516. struct udevice *dev;
  3517. struct eth_pdata *plat;
  3518. char *name;
  3519. int subnode;
  3520. u32 id;
  3521. /* Lookup eth driver */
  3522. drv = lists_uclass_lookup(UCLASS_ETH);
  3523. if (!drv) {
  3524. puts("Cannot find eth driver\n");
  3525. return -ENOENT;
  3526. }
  3527. fdt_for_each_subnode(subnode, blob, node) {
  3528. /* Skip disabled ports */
  3529. if (!fdtdec_get_is_enabled(blob, subnode))
  3530. continue;
  3531. plat = calloc(1, sizeof(*plat));
  3532. if (!plat)
  3533. return -ENOMEM;
  3534. id = fdtdec_get_int(blob, subnode, "port-id", -1);
  3535. name = calloc(1, 16);
  3536. sprintf(name, "mvpp2-%d", id);
  3537. /* Create child device UCLASS_ETH and bind it */
  3538. device_bind(parent, &mvpp2_driver, name, plat, subnode, &dev);
  3539. dev_set_of_offset(dev, subnode);
  3540. }
  3541. return 0;
  3542. }
  3543. static const struct udevice_id mvpp2_ids[] = {
  3544. {
  3545. .compatible = "marvell,armada-375-pp2",
  3546. .data = MVPP21,
  3547. },
  3548. { }
  3549. };
  3550. U_BOOT_DRIVER(mvpp2_base) = {
  3551. .name = "mvpp2_base",
  3552. .id = UCLASS_MISC,
  3553. .of_match = mvpp2_ids,
  3554. .bind = mvpp2_base_bind,
  3555. .probe = mvpp2_base_probe,
  3556. .priv_auto_alloc_size = sizeof(struct mvpp2),
  3557. };