mvpp2.c 116 KB

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