mvpp2.c 127 KB

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