mvpp2.c 180 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537
  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. * This file is licensed under the terms of the GNU General Public
  9. * License version 2. This program is licensed "as is" without any
  10. * warranty of any kind, whether express or implied.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/etherdevice.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/skbuff.h>
  17. #include <linux/inetdevice.h>
  18. #include <linux/mbus.h>
  19. #include <linux/module.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/cpumask.h>
  22. #include <linux/of.h>
  23. #include <linux/of_irq.h>
  24. #include <linux/of_mdio.h>
  25. #include <linux/of_net.h>
  26. #include <linux/of_address.h>
  27. #include <linux/phy.h>
  28. #include <linux/clk.h>
  29. #include <linux/hrtimer.h>
  30. #include <linux/ktime.h>
  31. #include <uapi/linux/ppp_defs.h>
  32. #include <net/ip.h>
  33. #include <net/ipv6.h>
  34. /* RX Fifo Registers */
  35. #define MVPP2_RX_DATA_FIFO_SIZE_REG(port) (0x00 + 4 * (port))
  36. #define MVPP2_RX_ATTR_FIFO_SIZE_REG(port) (0x20 + 4 * (port))
  37. #define MVPP2_RX_MIN_PKT_SIZE_REG 0x60
  38. #define MVPP2_RX_FIFO_INIT_REG 0x64
  39. /* RX DMA Top Registers */
  40. #define MVPP2_RX_CTRL_REG(port) (0x140 + 4 * (port))
  41. #define MVPP2_RX_LOW_LATENCY_PKT_SIZE(s) (((s) & 0xfff) << 16)
  42. #define MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK BIT(31)
  43. #define MVPP2_POOL_BUF_SIZE_REG(pool) (0x180 + 4 * (pool))
  44. #define MVPP2_POOL_BUF_SIZE_OFFSET 5
  45. #define MVPP2_RXQ_CONFIG_REG(rxq) (0x800 + 4 * (rxq))
  46. #define MVPP2_SNOOP_PKT_SIZE_MASK 0x1ff
  47. #define MVPP2_SNOOP_BUF_HDR_MASK BIT(9)
  48. #define MVPP2_RXQ_POOL_SHORT_OFFS 20
  49. #define MVPP2_RXQ_POOL_SHORT_MASK 0x700000
  50. #define MVPP2_RXQ_POOL_LONG_OFFS 24
  51. #define MVPP2_RXQ_POOL_LONG_MASK 0x7000000
  52. #define MVPP2_RXQ_PACKET_OFFSET_OFFS 28
  53. #define MVPP2_RXQ_PACKET_OFFSET_MASK 0x70000000
  54. #define MVPP2_RXQ_DISABLE_MASK BIT(31)
  55. /* Parser Registers */
  56. #define MVPP2_PRS_INIT_LOOKUP_REG 0x1000
  57. #define MVPP2_PRS_PORT_LU_MAX 0xf
  58. #define MVPP2_PRS_PORT_LU_MASK(port) (0xff << ((port) * 4))
  59. #define MVPP2_PRS_PORT_LU_VAL(port, val) ((val) << ((port) * 4))
  60. #define MVPP2_PRS_INIT_OFFS_REG(port) (0x1004 + ((port) & 4))
  61. #define MVPP2_PRS_INIT_OFF_MASK(port) (0x3f << (((port) % 4) * 8))
  62. #define MVPP2_PRS_INIT_OFF_VAL(port, val) ((val) << (((port) % 4) * 8))
  63. #define MVPP2_PRS_MAX_LOOP_REG(port) (0x100c + ((port) & 4))
  64. #define MVPP2_PRS_MAX_LOOP_MASK(port) (0xff << (((port) % 4) * 8))
  65. #define MVPP2_PRS_MAX_LOOP_VAL(port, val) ((val) << (((port) % 4) * 8))
  66. #define MVPP2_PRS_TCAM_IDX_REG 0x1100
  67. #define MVPP2_PRS_TCAM_DATA_REG(idx) (0x1104 + (idx) * 4)
  68. #define MVPP2_PRS_TCAM_INV_MASK BIT(31)
  69. #define MVPP2_PRS_SRAM_IDX_REG 0x1200
  70. #define MVPP2_PRS_SRAM_DATA_REG(idx) (0x1204 + (idx) * 4)
  71. #define MVPP2_PRS_TCAM_CTRL_REG 0x1230
  72. #define MVPP2_PRS_TCAM_EN_MASK BIT(0)
  73. /* Classifier Registers */
  74. #define MVPP2_CLS_MODE_REG 0x1800
  75. #define MVPP2_CLS_MODE_ACTIVE_MASK BIT(0)
  76. #define MVPP2_CLS_PORT_WAY_REG 0x1810
  77. #define MVPP2_CLS_PORT_WAY_MASK(port) (1 << (port))
  78. #define MVPP2_CLS_LKP_INDEX_REG 0x1814
  79. #define MVPP2_CLS_LKP_INDEX_WAY_OFFS 6
  80. #define MVPP2_CLS_LKP_TBL_REG 0x1818
  81. #define MVPP2_CLS_LKP_TBL_RXQ_MASK 0xff
  82. #define MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK BIT(25)
  83. #define MVPP2_CLS_FLOW_INDEX_REG 0x1820
  84. #define MVPP2_CLS_FLOW_TBL0_REG 0x1824
  85. #define MVPP2_CLS_FLOW_TBL1_REG 0x1828
  86. #define MVPP2_CLS_FLOW_TBL2_REG 0x182c
  87. #define MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port) (0x1980 + ((port) * 4))
  88. #define MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS 3
  89. #define MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK 0x7
  90. #define MVPP2_CLS_SWFWD_P2HQ_REG(port) (0x19b0 + ((port) * 4))
  91. #define MVPP2_CLS_SWFWD_PCTRL_REG 0x19d0
  92. #define MVPP2_CLS_SWFWD_PCTRL_MASK(port) (1 << (port))
  93. /* Descriptor Manager Top Registers */
  94. #define MVPP2_RXQ_NUM_REG 0x2040
  95. #define MVPP2_RXQ_DESC_ADDR_REG 0x2044
  96. #define MVPP2_RXQ_DESC_SIZE_REG 0x2048
  97. #define MVPP2_RXQ_DESC_SIZE_MASK 0x3ff0
  98. #define MVPP2_RXQ_STATUS_UPDATE_REG(rxq) (0x3000 + 4 * (rxq))
  99. #define MVPP2_RXQ_NUM_PROCESSED_OFFSET 0
  100. #define MVPP2_RXQ_NUM_NEW_OFFSET 16
  101. #define MVPP2_RXQ_STATUS_REG(rxq) (0x3400 + 4 * (rxq))
  102. #define MVPP2_RXQ_OCCUPIED_MASK 0x3fff
  103. #define MVPP2_RXQ_NON_OCCUPIED_OFFSET 16
  104. #define MVPP2_RXQ_NON_OCCUPIED_MASK 0x3fff0000
  105. #define MVPP2_RXQ_THRESH_REG 0x204c
  106. #define MVPP2_OCCUPIED_THRESH_OFFSET 0
  107. #define MVPP2_OCCUPIED_THRESH_MASK 0x3fff
  108. #define MVPP2_RXQ_INDEX_REG 0x2050
  109. #define MVPP2_TXQ_NUM_REG 0x2080
  110. #define MVPP2_TXQ_DESC_ADDR_REG 0x2084
  111. #define MVPP2_TXQ_DESC_SIZE_REG 0x2088
  112. #define MVPP2_TXQ_DESC_SIZE_MASK 0x3ff0
  113. #define MVPP2_AGGR_TXQ_UPDATE_REG 0x2090
  114. #define MVPP2_TXQ_THRESH_REG 0x2094
  115. #define MVPP2_TRANSMITTED_THRESH_OFFSET 16
  116. #define MVPP2_TRANSMITTED_THRESH_MASK 0x3fff0000
  117. #define MVPP2_TXQ_INDEX_REG 0x2098
  118. #define MVPP2_TXQ_PREF_BUF_REG 0x209c
  119. #define MVPP2_PREF_BUF_PTR(desc) ((desc) & 0xfff)
  120. #define MVPP2_PREF_BUF_SIZE_4 (BIT(12) | BIT(13))
  121. #define MVPP2_PREF_BUF_SIZE_16 (BIT(12) | BIT(14))
  122. #define MVPP2_PREF_BUF_THRESH(val) ((val) << 17)
  123. #define MVPP2_TXQ_DRAIN_EN_MASK BIT(31)
  124. #define MVPP2_TXQ_PENDING_REG 0x20a0
  125. #define MVPP2_TXQ_PENDING_MASK 0x3fff
  126. #define MVPP2_TXQ_INT_STATUS_REG 0x20a4
  127. #define MVPP2_TXQ_SENT_REG(txq) (0x3c00 + 4 * (txq))
  128. #define MVPP2_TRANSMITTED_COUNT_OFFSET 16
  129. #define MVPP2_TRANSMITTED_COUNT_MASK 0x3fff0000
  130. #define MVPP2_TXQ_RSVD_REQ_REG 0x20b0
  131. #define MVPP2_TXQ_RSVD_REQ_Q_OFFSET 16
  132. #define MVPP2_TXQ_RSVD_RSLT_REG 0x20b4
  133. #define MVPP2_TXQ_RSVD_RSLT_MASK 0x3fff
  134. #define MVPP2_TXQ_RSVD_CLR_REG 0x20b8
  135. #define MVPP2_TXQ_RSVD_CLR_OFFSET 16
  136. #define MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu) (0x2100 + 4 * (cpu))
  137. #define MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu) (0x2140 + 4 * (cpu))
  138. #define MVPP2_AGGR_TXQ_DESC_SIZE_MASK 0x3ff0
  139. #define MVPP2_AGGR_TXQ_STATUS_REG(cpu) (0x2180 + 4 * (cpu))
  140. #define MVPP2_AGGR_TXQ_PENDING_MASK 0x3fff
  141. #define MVPP2_AGGR_TXQ_INDEX_REG(cpu) (0x21c0 + 4 * (cpu))
  142. /* MBUS bridge registers */
  143. #define MVPP2_WIN_BASE(w) (0x4000 + ((w) << 2))
  144. #define MVPP2_WIN_SIZE(w) (0x4020 + ((w) << 2))
  145. #define MVPP2_WIN_REMAP(w) (0x4040 + ((w) << 2))
  146. #define MVPP2_BASE_ADDR_ENABLE 0x4060
  147. /* Interrupt Cause and Mask registers */
  148. #define MVPP2_ISR_RX_THRESHOLD_REG(rxq) (0x5200 + 4 * (rxq))
  149. #define MVPP2_MAX_ISR_RX_THRESHOLD 0xfffff0
  150. #define MVPP2_ISR_RXQ_GROUP_REG(rxq) (0x5400 + 4 * (rxq))
  151. #define MVPP2_ISR_ENABLE_REG(port) (0x5420 + 4 * (port))
  152. #define MVPP2_ISR_ENABLE_INTERRUPT(mask) ((mask) & 0xffff)
  153. #define MVPP2_ISR_DISABLE_INTERRUPT(mask) (((mask) << 16) & 0xffff0000)
  154. #define MVPP2_ISR_RX_TX_CAUSE_REG(port) (0x5480 + 4 * (port))
  155. #define MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK 0xffff
  156. #define MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK 0xff0000
  157. #define MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK BIT(24)
  158. #define MVPP2_CAUSE_FCS_ERR_MASK BIT(25)
  159. #define MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK BIT(26)
  160. #define MVPP2_CAUSE_TX_EXCEPTION_SUM_MASK BIT(29)
  161. #define MVPP2_CAUSE_RX_EXCEPTION_SUM_MASK BIT(30)
  162. #define MVPP2_CAUSE_MISC_SUM_MASK BIT(31)
  163. #define MVPP2_ISR_RX_TX_MASK_REG(port) (0x54a0 + 4 * (port))
  164. #define MVPP2_ISR_PON_RX_TX_MASK_REG 0x54bc
  165. #define MVPP2_PON_CAUSE_RXQ_OCCUP_DESC_ALL_MASK 0xffff
  166. #define MVPP2_PON_CAUSE_TXP_OCCUP_DESC_ALL_MASK 0x3fc00000
  167. #define MVPP2_PON_CAUSE_MISC_SUM_MASK BIT(31)
  168. #define MVPP2_ISR_MISC_CAUSE_REG 0x55b0
  169. /* Buffer Manager registers */
  170. #define MVPP2_BM_POOL_BASE_REG(pool) (0x6000 + ((pool) * 4))
  171. #define MVPP2_BM_POOL_BASE_ADDR_MASK 0xfffff80
  172. #define MVPP2_BM_POOL_SIZE_REG(pool) (0x6040 + ((pool) * 4))
  173. #define MVPP2_BM_POOL_SIZE_MASK 0xfff0
  174. #define MVPP2_BM_POOL_READ_PTR_REG(pool) (0x6080 + ((pool) * 4))
  175. #define MVPP2_BM_POOL_GET_READ_PTR_MASK 0xfff0
  176. #define MVPP2_BM_POOL_PTRS_NUM_REG(pool) (0x60c0 + ((pool) * 4))
  177. #define MVPP2_BM_POOL_PTRS_NUM_MASK 0xfff0
  178. #define MVPP2_BM_BPPI_READ_PTR_REG(pool) (0x6100 + ((pool) * 4))
  179. #define MVPP2_BM_BPPI_PTRS_NUM_REG(pool) (0x6140 + ((pool) * 4))
  180. #define MVPP2_BM_BPPI_PTR_NUM_MASK 0x7ff
  181. #define MVPP2_BM_BPPI_PREFETCH_FULL_MASK BIT(16)
  182. #define MVPP2_BM_POOL_CTRL_REG(pool) (0x6200 + ((pool) * 4))
  183. #define MVPP2_BM_START_MASK BIT(0)
  184. #define MVPP2_BM_STOP_MASK BIT(1)
  185. #define MVPP2_BM_STATE_MASK BIT(4)
  186. #define MVPP2_BM_LOW_THRESH_OFFS 8
  187. #define MVPP2_BM_LOW_THRESH_MASK 0x7f00
  188. #define MVPP2_BM_LOW_THRESH_VALUE(val) ((val) << \
  189. MVPP2_BM_LOW_THRESH_OFFS)
  190. #define MVPP2_BM_HIGH_THRESH_OFFS 16
  191. #define MVPP2_BM_HIGH_THRESH_MASK 0x7f0000
  192. #define MVPP2_BM_HIGH_THRESH_VALUE(val) ((val) << \
  193. MVPP2_BM_HIGH_THRESH_OFFS)
  194. #define MVPP2_BM_INTR_CAUSE_REG(pool) (0x6240 + ((pool) * 4))
  195. #define MVPP2_BM_RELEASED_DELAY_MASK BIT(0)
  196. #define MVPP2_BM_ALLOC_FAILED_MASK BIT(1)
  197. #define MVPP2_BM_BPPE_EMPTY_MASK BIT(2)
  198. #define MVPP2_BM_BPPE_FULL_MASK BIT(3)
  199. #define MVPP2_BM_AVAILABLE_BP_LOW_MASK BIT(4)
  200. #define MVPP2_BM_INTR_MASK_REG(pool) (0x6280 + ((pool) * 4))
  201. #define MVPP2_BM_PHY_ALLOC_REG(pool) (0x6400 + ((pool) * 4))
  202. #define MVPP2_BM_PHY_ALLOC_GRNTD_MASK BIT(0)
  203. #define MVPP2_BM_VIRT_ALLOC_REG 0x6440
  204. #define MVPP2_BM_PHY_RLS_REG(pool) (0x6480 + ((pool) * 4))
  205. #define MVPP2_BM_PHY_RLS_MC_BUFF_MASK BIT(0)
  206. #define MVPP2_BM_PHY_RLS_PRIO_EN_MASK BIT(1)
  207. #define MVPP2_BM_PHY_RLS_GRNTD_MASK BIT(2)
  208. #define MVPP2_BM_VIRT_RLS_REG 0x64c0
  209. #define MVPP2_BM_MC_RLS_REG 0x64c4
  210. #define MVPP2_BM_MC_ID_MASK 0xfff
  211. #define MVPP2_BM_FORCE_RELEASE_MASK BIT(12)
  212. /* TX Scheduler registers */
  213. #define MVPP2_TXP_SCHED_PORT_INDEX_REG 0x8000
  214. #define MVPP2_TXP_SCHED_Q_CMD_REG 0x8004
  215. #define MVPP2_TXP_SCHED_ENQ_MASK 0xff
  216. #define MVPP2_TXP_SCHED_DISQ_OFFSET 8
  217. #define MVPP2_TXP_SCHED_CMD_1_REG 0x8010
  218. #define MVPP2_TXP_SCHED_PERIOD_REG 0x8018
  219. #define MVPP2_TXP_SCHED_MTU_REG 0x801c
  220. #define MVPP2_TXP_MTU_MAX 0x7FFFF
  221. #define MVPP2_TXP_SCHED_REFILL_REG 0x8020
  222. #define MVPP2_TXP_REFILL_TOKENS_ALL_MASK 0x7ffff
  223. #define MVPP2_TXP_REFILL_PERIOD_ALL_MASK 0x3ff00000
  224. #define MVPP2_TXP_REFILL_PERIOD_MASK(v) ((v) << 20)
  225. #define MVPP2_TXP_SCHED_TOKEN_SIZE_REG 0x8024
  226. #define MVPP2_TXP_TOKEN_SIZE_MAX 0xffffffff
  227. #define MVPP2_TXQ_SCHED_REFILL_REG(q) (0x8040 + ((q) << 2))
  228. #define MVPP2_TXQ_REFILL_TOKENS_ALL_MASK 0x7ffff
  229. #define MVPP2_TXQ_REFILL_PERIOD_ALL_MASK 0x3ff00000
  230. #define MVPP2_TXQ_REFILL_PERIOD_MASK(v) ((v) << 20)
  231. #define MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(q) (0x8060 + ((q) << 2))
  232. #define MVPP2_TXQ_TOKEN_SIZE_MAX 0x7fffffff
  233. #define MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(q) (0x8080 + ((q) << 2))
  234. #define MVPP2_TXQ_TOKEN_CNTR_MAX 0xffffffff
  235. /* TX general registers */
  236. #define MVPP2_TX_SNOOP_REG 0x8800
  237. #define MVPP2_TX_PORT_FLUSH_REG 0x8810
  238. #define MVPP2_TX_PORT_FLUSH_MASK(port) (1 << (port))
  239. /* LMS registers */
  240. #define MVPP2_SRC_ADDR_MIDDLE 0x24
  241. #define MVPP2_SRC_ADDR_HIGH 0x28
  242. #define MVPP2_PHY_AN_CFG0_REG 0x34
  243. #define MVPP2_PHY_AN_STOP_SMI0_MASK BIT(7)
  244. #define MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG 0x305c
  245. #define MVPP2_EXT_GLOBAL_CTRL_DEFAULT 0x27
  246. /* Per-port registers */
  247. #define MVPP2_GMAC_CTRL_0_REG 0x0
  248. #define MVPP2_GMAC_PORT_EN_MASK BIT(0)
  249. #define MVPP2_GMAC_MAX_RX_SIZE_OFFS 2
  250. #define MVPP2_GMAC_MAX_RX_SIZE_MASK 0x7ffc
  251. #define MVPP2_GMAC_MIB_CNTR_EN_MASK BIT(15)
  252. #define MVPP2_GMAC_CTRL_1_REG 0x4
  253. #define MVPP2_GMAC_PERIODIC_XON_EN_MASK BIT(1)
  254. #define MVPP2_GMAC_GMII_LB_EN_MASK BIT(5)
  255. #define MVPP2_GMAC_PCS_LB_EN_BIT 6
  256. #define MVPP2_GMAC_PCS_LB_EN_MASK BIT(6)
  257. #define MVPP2_GMAC_SA_LOW_OFFS 7
  258. #define MVPP2_GMAC_CTRL_2_REG 0x8
  259. #define MVPP2_GMAC_INBAND_AN_MASK BIT(0)
  260. #define MVPP2_GMAC_PCS_ENABLE_MASK BIT(3)
  261. #define MVPP2_GMAC_PORT_RGMII_MASK BIT(4)
  262. #define MVPP2_GMAC_PORT_RESET_MASK BIT(6)
  263. #define MVPP2_GMAC_AUTONEG_CONFIG 0xc
  264. #define MVPP2_GMAC_FORCE_LINK_DOWN BIT(0)
  265. #define MVPP2_GMAC_FORCE_LINK_PASS BIT(1)
  266. #define MVPP2_GMAC_CONFIG_MII_SPEED BIT(5)
  267. #define MVPP2_GMAC_CONFIG_GMII_SPEED BIT(6)
  268. #define MVPP2_GMAC_AN_SPEED_EN BIT(7)
  269. #define MVPP2_GMAC_FC_ADV_EN BIT(9)
  270. #define MVPP2_GMAC_CONFIG_FULL_DUPLEX BIT(12)
  271. #define MVPP2_GMAC_AN_DUPLEX_EN BIT(13)
  272. #define MVPP2_GMAC_PORT_FIFO_CFG_1_REG 0x1c
  273. #define MVPP2_GMAC_TX_FIFO_MIN_TH_OFFS 6
  274. #define MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK 0x1fc0
  275. #define MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(v) (((v) << 6) & \
  276. MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK)
  277. #define MVPP2_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff
  278. /* Descriptor ring Macros */
  279. #define MVPP2_QUEUE_NEXT_DESC(q, index) \
  280. (((index) < (q)->last_desc) ? ((index) + 1) : 0)
  281. /* Various constants */
  282. /* Coalescing */
  283. #define MVPP2_TXDONE_COAL_PKTS_THRESH 15
  284. #define MVPP2_TXDONE_HRTIMER_PERIOD_NS 1000000UL
  285. #define MVPP2_RX_COAL_PKTS 32
  286. #define MVPP2_RX_COAL_USEC 100
  287. /* The two bytes Marvell header. Either contains a special value used
  288. * by Marvell switches when a specific hardware mode is enabled (not
  289. * supported by this driver) or is filled automatically by zeroes on
  290. * the RX side. Those two bytes being at the front of the Ethernet
  291. * header, they allow to have the IP header aligned on a 4 bytes
  292. * boundary automatically: the hardware skips those two bytes on its
  293. * own.
  294. */
  295. #define MVPP2_MH_SIZE 2
  296. #define MVPP2_ETH_TYPE_LEN 2
  297. #define MVPP2_PPPOE_HDR_SIZE 8
  298. #define MVPP2_VLAN_TAG_LEN 4
  299. /* Lbtd 802.3 type */
  300. #define MVPP2_IP_LBDT_TYPE 0xfffa
  301. #define MVPP2_TX_CSUM_MAX_SIZE 9800
  302. /* Timeout constants */
  303. #define MVPP2_TX_DISABLE_TIMEOUT_MSEC 1000
  304. #define MVPP2_TX_PENDING_TIMEOUT_MSEC 1000
  305. #define MVPP2_TX_MTU_MAX 0x7ffff
  306. /* Maximum number of T-CONTs of PON port */
  307. #define MVPP2_MAX_TCONT 16
  308. /* Maximum number of supported ports */
  309. #define MVPP2_MAX_PORTS 4
  310. /* Maximum number of TXQs used by single port */
  311. #define MVPP2_MAX_TXQ 8
  312. /* Maximum number of RXQs used by single port */
  313. #define MVPP2_MAX_RXQ 8
  314. /* Dfault number of RXQs in use */
  315. #define MVPP2_DEFAULT_RXQ 4
  316. /* Total number of RXQs available to all ports */
  317. #define MVPP2_RXQ_TOTAL_NUM (MVPP2_MAX_PORTS * MVPP2_MAX_RXQ)
  318. /* Max number of Rx descriptors */
  319. #define MVPP2_MAX_RXD 128
  320. /* Max number of Tx descriptors */
  321. #define MVPP2_MAX_TXD 1024
  322. /* Amount of Tx descriptors that can be reserved at once by CPU */
  323. #define MVPP2_CPU_DESC_CHUNK 64
  324. /* Max number of Tx descriptors in each aggregated queue */
  325. #define MVPP2_AGGR_TXQ_SIZE 256
  326. /* Descriptor aligned size */
  327. #define MVPP2_DESC_ALIGNED_SIZE 32
  328. /* Descriptor alignment mask */
  329. #define MVPP2_TX_DESC_ALIGN (MVPP2_DESC_ALIGNED_SIZE - 1)
  330. /* RX FIFO constants */
  331. #define MVPP2_RX_FIFO_PORT_DATA_SIZE 0x2000
  332. #define MVPP2_RX_FIFO_PORT_ATTR_SIZE 0x80
  333. #define MVPP2_RX_FIFO_PORT_MIN_PKT 0x80
  334. /* RX buffer constants */
  335. #define MVPP2_SKB_SHINFO_SIZE \
  336. SKB_DATA_ALIGN(sizeof(struct skb_shared_info))
  337. #define MVPP2_RX_PKT_SIZE(mtu) \
  338. ALIGN((mtu) + MVPP2_MH_SIZE + MVPP2_VLAN_TAG_LEN + \
  339. ETH_HLEN + ETH_FCS_LEN, cache_line_size())
  340. #define MVPP2_RX_BUF_SIZE(pkt_size) ((pkt_size) + NET_SKB_PAD)
  341. #define MVPP2_RX_TOTAL_SIZE(buf_size) ((buf_size) + MVPP2_SKB_SHINFO_SIZE)
  342. #define MVPP2_RX_MAX_PKT_SIZE(total_size) \
  343. ((total_size) - NET_SKB_PAD - MVPP2_SKB_SHINFO_SIZE)
  344. #define MVPP2_BIT_TO_BYTE(bit) ((bit) / 8)
  345. /* IPv6 max L3 address size */
  346. #define MVPP2_MAX_L3_ADDR_SIZE 16
  347. /* Port flags */
  348. #define MVPP2_F_LOOPBACK BIT(0)
  349. /* Marvell tag types */
  350. enum mvpp2_tag_type {
  351. MVPP2_TAG_TYPE_NONE = 0,
  352. MVPP2_TAG_TYPE_MH = 1,
  353. MVPP2_TAG_TYPE_DSA = 2,
  354. MVPP2_TAG_TYPE_EDSA = 3,
  355. MVPP2_TAG_TYPE_VLAN = 4,
  356. MVPP2_TAG_TYPE_LAST = 5
  357. };
  358. /* Parser constants */
  359. #define MVPP2_PRS_TCAM_SRAM_SIZE 256
  360. #define MVPP2_PRS_TCAM_WORDS 6
  361. #define MVPP2_PRS_SRAM_WORDS 4
  362. #define MVPP2_PRS_FLOW_ID_SIZE 64
  363. #define MVPP2_PRS_FLOW_ID_MASK 0x3f
  364. #define MVPP2_PRS_TCAM_ENTRY_INVALID 1
  365. #define MVPP2_PRS_TCAM_DSA_TAGGED_BIT BIT(5)
  366. #define MVPP2_PRS_IPV4_HEAD 0x40
  367. #define MVPP2_PRS_IPV4_HEAD_MASK 0xf0
  368. #define MVPP2_PRS_IPV4_MC 0xe0
  369. #define MVPP2_PRS_IPV4_MC_MASK 0xf0
  370. #define MVPP2_PRS_IPV4_BC_MASK 0xff
  371. #define MVPP2_PRS_IPV4_IHL 0x5
  372. #define MVPP2_PRS_IPV4_IHL_MASK 0xf
  373. #define MVPP2_PRS_IPV6_MC 0xff
  374. #define MVPP2_PRS_IPV6_MC_MASK 0xff
  375. #define MVPP2_PRS_IPV6_HOP_MASK 0xff
  376. #define MVPP2_PRS_TCAM_PROTO_MASK 0xff
  377. #define MVPP2_PRS_TCAM_PROTO_MASK_L 0x3f
  378. #define MVPP2_PRS_DBL_VLANS_MAX 100
  379. /* Tcam structure:
  380. * - lookup ID - 4 bits
  381. * - port ID - 1 byte
  382. * - additional information - 1 byte
  383. * - header data - 8 bytes
  384. * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(5)->(0).
  385. */
  386. #define MVPP2_PRS_AI_BITS 8
  387. #define MVPP2_PRS_PORT_MASK 0xff
  388. #define MVPP2_PRS_LU_MASK 0xf
  389. #define MVPP2_PRS_TCAM_DATA_BYTE(offs) \
  390. (((offs) - ((offs) % 2)) * 2 + ((offs) % 2))
  391. #define MVPP2_PRS_TCAM_DATA_BYTE_EN(offs) \
  392. (((offs) * 2) - ((offs) % 2) + 2)
  393. #define MVPP2_PRS_TCAM_AI_BYTE 16
  394. #define MVPP2_PRS_TCAM_PORT_BYTE 17
  395. #define MVPP2_PRS_TCAM_LU_BYTE 20
  396. #define MVPP2_PRS_TCAM_EN_OFFS(offs) ((offs) + 2)
  397. #define MVPP2_PRS_TCAM_INV_WORD 5
  398. /* Tcam entries ID */
  399. #define MVPP2_PE_DROP_ALL 0
  400. #define MVPP2_PE_FIRST_FREE_TID 1
  401. #define MVPP2_PE_LAST_FREE_TID (MVPP2_PRS_TCAM_SRAM_SIZE - 31)
  402. #define MVPP2_PE_IP6_EXT_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 30)
  403. #define MVPP2_PE_MAC_MC_IP6 (MVPP2_PRS_TCAM_SRAM_SIZE - 29)
  404. #define MVPP2_PE_IP6_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 28)
  405. #define MVPP2_PE_IP4_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 27)
  406. #define MVPP2_PE_LAST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 26)
  407. #define MVPP2_PE_FIRST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 19)
  408. #define MVPP2_PE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 18)
  409. #define MVPP2_PE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 17)
  410. #define MVPP2_PE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 16)
  411. #define MVPP2_PE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 15)
  412. #define MVPP2_PE_ETYPE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 14)
  413. #define MVPP2_PE_ETYPE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 13)
  414. #define MVPP2_PE_ETYPE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 12)
  415. #define MVPP2_PE_ETYPE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 11)
  416. #define MVPP2_PE_MH_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 10)
  417. #define MVPP2_PE_DSA_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 9)
  418. #define MVPP2_PE_IP6_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 8)
  419. #define MVPP2_PE_IP4_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 7)
  420. #define MVPP2_PE_ETH_TYPE_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 6)
  421. #define MVPP2_PE_VLAN_DBL (MVPP2_PRS_TCAM_SRAM_SIZE - 5)
  422. #define MVPP2_PE_VLAN_NONE (MVPP2_PRS_TCAM_SRAM_SIZE - 4)
  423. #define MVPP2_PE_MAC_MC_ALL (MVPP2_PRS_TCAM_SRAM_SIZE - 3)
  424. #define MVPP2_PE_MAC_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 2)
  425. #define MVPP2_PE_MAC_NON_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 1)
  426. /* Sram structure
  427. * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(3)->(0).
  428. */
  429. #define MVPP2_PRS_SRAM_RI_OFFS 0
  430. #define MVPP2_PRS_SRAM_RI_WORD 0
  431. #define MVPP2_PRS_SRAM_RI_CTRL_OFFS 32
  432. #define MVPP2_PRS_SRAM_RI_CTRL_WORD 1
  433. #define MVPP2_PRS_SRAM_RI_CTRL_BITS 32
  434. #define MVPP2_PRS_SRAM_SHIFT_OFFS 64
  435. #define MVPP2_PRS_SRAM_SHIFT_SIGN_BIT 72
  436. #define MVPP2_PRS_SRAM_UDF_OFFS 73
  437. #define MVPP2_PRS_SRAM_UDF_BITS 8
  438. #define MVPP2_PRS_SRAM_UDF_MASK 0xff
  439. #define MVPP2_PRS_SRAM_UDF_SIGN_BIT 81
  440. #define MVPP2_PRS_SRAM_UDF_TYPE_OFFS 82
  441. #define MVPP2_PRS_SRAM_UDF_TYPE_MASK 0x7
  442. #define MVPP2_PRS_SRAM_UDF_TYPE_L3 1
  443. #define MVPP2_PRS_SRAM_UDF_TYPE_L4 4
  444. #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS 85
  445. #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK 0x3
  446. #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD 1
  447. #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP4_ADD 2
  448. #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP6_ADD 3
  449. #define MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS 87
  450. #define MVPP2_PRS_SRAM_OP_SEL_UDF_BITS 2
  451. #define MVPP2_PRS_SRAM_OP_SEL_UDF_MASK 0x3
  452. #define MVPP2_PRS_SRAM_OP_SEL_UDF_ADD 0
  453. #define MVPP2_PRS_SRAM_OP_SEL_UDF_IP4_ADD 2
  454. #define MVPP2_PRS_SRAM_OP_SEL_UDF_IP6_ADD 3
  455. #define MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS 89
  456. #define MVPP2_PRS_SRAM_AI_OFFS 90
  457. #define MVPP2_PRS_SRAM_AI_CTRL_OFFS 98
  458. #define MVPP2_PRS_SRAM_AI_CTRL_BITS 8
  459. #define MVPP2_PRS_SRAM_AI_MASK 0xff
  460. #define MVPP2_PRS_SRAM_NEXT_LU_OFFS 106
  461. #define MVPP2_PRS_SRAM_NEXT_LU_MASK 0xf
  462. #define MVPP2_PRS_SRAM_LU_DONE_BIT 110
  463. #define MVPP2_PRS_SRAM_LU_GEN_BIT 111
  464. /* Sram result info bits assignment */
  465. #define MVPP2_PRS_RI_MAC_ME_MASK 0x1
  466. #define MVPP2_PRS_RI_DSA_MASK 0x2
  467. #define MVPP2_PRS_RI_VLAN_MASK (BIT(2) | BIT(3))
  468. #define MVPP2_PRS_RI_VLAN_NONE 0x0
  469. #define MVPP2_PRS_RI_VLAN_SINGLE BIT(2)
  470. #define MVPP2_PRS_RI_VLAN_DOUBLE BIT(3)
  471. #define MVPP2_PRS_RI_VLAN_TRIPLE (BIT(2) | BIT(3))
  472. #define MVPP2_PRS_RI_CPU_CODE_MASK 0x70
  473. #define MVPP2_PRS_RI_CPU_CODE_RX_SPEC BIT(4)
  474. #define MVPP2_PRS_RI_L2_CAST_MASK (BIT(9) | BIT(10))
  475. #define MVPP2_PRS_RI_L2_UCAST 0x0
  476. #define MVPP2_PRS_RI_L2_MCAST BIT(9)
  477. #define MVPP2_PRS_RI_L2_BCAST BIT(10)
  478. #define MVPP2_PRS_RI_PPPOE_MASK 0x800
  479. #define MVPP2_PRS_RI_L3_PROTO_MASK (BIT(12) | BIT(13) | BIT(14))
  480. #define MVPP2_PRS_RI_L3_UN 0x0
  481. #define MVPP2_PRS_RI_L3_IP4 BIT(12)
  482. #define MVPP2_PRS_RI_L3_IP4_OPT BIT(13)
  483. #define MVPP2_PRS_RI_L3_IP4_OTHER (BIT(12) | BIT(13))
  484. #define MVPP2_PRS_RI_L3_IP6 BIT(14)
  485. #define MVPP2_PRS_RI_L3_IP6_EXT (BIT(12) | BIT(14))
  486. #define MVPP2_PRS_RI_L3_ARP (BIT(13) | BIT(14))
  487. #define MVPP2_PRS_RI_L3_ADDR_MASK (BIT(15) | BIT(16))
  488. #define MVPP2_PRS_RI_L3_UCAST 0x0
  489. #define MVPP2_PRS_RI_L3_MCAST BIT(15)
  490. #define MVPP2_PRS_RI_L3_BCAST (BIT(15) | BIT(16))
  491. #define MVPP2_PRS_RI_IP_FRAG_MASK 0x20000
  492. #define MVPP2_PRS_RI_UDF3_MASK 0x300000
  493. #define MVPP2_PRS_RI_UDF3_RX_SPECIAL BIT(21)
  494. #define MVPP2_PRS_RI_L4_PROTO_MASK 0x1c00000
  495. #define MVPP2_PRS_RI_L4_TCP BIT(22)
  496. #define MVPP2_PRS_RI_L4_UDP BIT(23)
  497. #define MVPP2_PRS_RI_L4_OTHER (BIT(22) | BIT(23))
  498. #define MVPP2_PRS_RI_UDF7_MASK 0x60000000
  499. #define MVPP2_PRS_RI_UDF7_IP6_LITE BIT(29)
  500. #define MVPP2_PRS_RI_DROP_MASK 0x80000000
  501. /* Sram additional info bits assignment */
  502. #define MVPP2_PRS_IPV4_DIP_AI_BIT BIT(0)
  503. #define MVPP2_PRS_IPV6_NO_EXT_AI_BIT BIT(0)
  504. #define MVPP2_PRS_IPV6_EXT_AI_BIT BIT(1)
  505. #define MVPP2_PRS_IPV6_EXT_AH_AI_BIT BIT(2)
  506. #define MVPP2_PRS_IPV6_EXT_AH_LEN_AI_BIT BIT(3)
  507. #define MVPP2_PRS_IPV6_EXT_AH_L4_AI_BIT BIT(4)
  508. #define MVPP2_PRS_SINGLE_VLAN_AI 0
  509. #define MVPP2_PRS_DBL_VLAN_AI_BIT BIT(7)
  510. /* DSA/EDSA type */
  511. #define MVPP2_PRS_TAGGED true
  512. #define MVPP2_PRS_UNTAGGED false
  513. #define MVPP2_PRS_EDSA true
  514. #define MVPP2_PRS_DSA false
  515. /* MAC entries, shadow udf */
  516. enum mvpp2_prs_udf {
  517. MVPP2_PRS_UDF_MAC_DEF,
  518. MVPP2_PRS_UDF_MAC_RANGE,
  519. MVPP2_PRS_UDF_L2_DEF,
  520. MVPP2_PRS_UDF_L2_DEF_COPY,
  521. MVPP2_PRS_UDF_L2_USER,
  522. };
  523. /* Lookup ID */
  524. enum mvpp2_prs_lookup {
  525. MVPP2_PRS_LU_MH,
  526. MVPP2_PRS_LU_MAC,
  527. MVPP2_PRS_LU_DSA,
  528. MVPP2_PRS_LU_VLAN,
  529. MVPP2_PRS_LU_L2,
  530. MVPP2_PRS_LU_PPPOE,
  531. MVPP2_PRS_LU_IP4,
  532. MVPP2_PRS_LU_IP6,
  533. MVPP2_PRS_LU_FLOWS,
  534. MVPP2_PRS_LU_LAST,
  535. };
  536. /* L3 cast enum */
  537. enum mvpp2_prs_l3_cast {
  538. MVPP2_PRS_L3_UNI_CAST,
  539. MVPP2_PRS_L3_MULTI_CAST,
  540. MVPP2_PRS_L3_BROAD_CAST
  541. };
  542. /* Classifier constants */
  543. #define MVPP2_CLS_FLOWS_TBL_SIZE 512
  544. #define MVPP2_CLS_FLOWS_TBL_DATA_WORDS 3
  545. #define MVPP2_CLS_LKP_TBL_SIZE 64
  546. /* BM constants */
  547. #define MVPP2_BM_POOLS_NUM 8
  548. #define MVPP2_BM_LONG_BUF_NUM 1024
  549. #define MVPP2_BM_SHORT_BUF_NUM 2048
  550. #define MVPP2_BM_POOL_SIZE_MAX (16*1024 - MVPP2_BM_POOL_PTR_ALIGN/4)
  551. #define MVPP2_BM_POOL_PTR_ALIGN 128
  552. #define MVPP2_BM_SWF_LONG_POOL(port) ((port > 2) ? 2 : port)
  553. #define MVPP2_BM_SWF_SHORT_POOL 3
  554. /* BM cookie (32 bits) definition */
  555. #define MVPP2_BM_COOKIE_POOL_OFFS 8
  556. #define MVPP2_BM_COOKIE_CPU_OFFS 24
  557. /* BM short pool packet size
  558. * These value assure that for SWF the total number
  559. * of bytes allocated for each buffer will be 512
  560. */
  561. #define MVPP2_BM_SHORT_PKT_SIZE MVPP2_RX_MAX_PKT_SIZE(512)
  562. enum mvpp2_bm_type {
  563. MVPP2_BM_FREE,
  564. MVPP2_BM_SWF_LONG,
  565. MVPP2_BM_SWF_SHORT
  566. };
  567. /* Definitions */
  568. /* Shared Packet Processor resources */
  569. struct mvpp2 {
  570. /* Shared registers' base addresses */
  571. void __iomem *base;
  572. void __iomem *lms_base;
  573. /* Common clocks */
  574. struct clk *pp_clk;
  575. struct clk *gop_clk;
  576. /* List of pointers to port structures */
  577. struct mvpp2_port **port_list;
  578. /* Aggregated TXQs */
  579. struct mvpp2_tx_queue *aggr_txqs;
  580. /* BM pools */
  581. struct mvpp2_bm_pool *bm_pools;
  582. /* PRS shadow table */
  583. struct mvpp2_prs_shadow *prs_shadow;
  584. /* PRS auxiliary table for double vlan entries control */
  585. bool *prs_double_vlans;
  586. /* Tclk value */
  587. u32 tclk;
  588. };
  589. struct mvpp2_pcpu_stats {
  590. struct u64_stats_sync syncp;
  591. u64 rx_packets;
  592. u64 rx_bytes;
  593. u64 tx_packets;
  594. u64 tx_bytes;
  595. };
  596. /* Per-CPU port control */
  597. struct mvpp2_port_pcpu {
  598. struct hrtimer tx_done_timer;
  599. bool timer_scheduled;
  600. /* Tasklet for egress finalization */
  601. struct tasklet_struct tx_done_tasklet;
  602. };
  603. struct mvpp2_port {
  604. u8 id;
  605. int irq;
  606. struct mvpp2 *priv;
  607. /* Per-port registers' base address */
  608. void __iomem *base;
  609. struct mvpp2_rx_queue **rxqs;
  610. struct mvpp2_tx_queue **txqs;
  611. struct net_device *dev;
  612. int pkt_size;
  613. u32 pending_cause_rx;
  614. struct napi_struct napi;
  615. /* Per-CPU port control */
  616. struct mvpp2_port_pcpu __percpu *pcpu;
  617. /* Flags */
  618. unsigned long flags;
  619. u16 tx_ring_size;
  620. u16 rx_ring_size;
  621. struct mvpp2_pcpu_stats __percpu *stats;
  622. phy_interface_t phy_interface;
  623. struct device_node *phy_node;
  624. unsigned int link;
  625. unsigned int duplex;
  626. unsigned int speed;
  627. struct mvpp2_bm_pool *pool_long;
  628. struct mvpp2_bm_pool *pool_short;
  629. /* Index of first port's physical RXQ */
  630. u8 first_rxq;
  631. };
  632. /* The mvpp2_tx_desc and mvpp2_rx_desc structures describe the
  633. * layout of the transmit and reception DMA descriptors, and their
  634. * layout is therefore defined by the hardware design
  635. */
  636. #define MVPP2_TXD_L3_OFF_SHIFT 0
  637. #define MVPP2_TXD_IP_HLEN_SHIFT 8
  638. #define MVPP2_TXD_L4_CSUM_FRAG BIT(13)
  639. #define MVPP2_TXD_L4_CSUM_NOT BIT(14)
  640. #define MVPP2_TXD_IP_CSUM_DISABLE BIT(15)
  641. #define MVPP2_TXD_PADDING_DISABLE BIT(23)
  642. #define MVPP2_TXD_L4_UDP BIT(24)
  643. #define MVPP2_TXD_L3_IP6 BIT(26)
  644. #define MVPP2_TXD_L_DESC BIT(28)
  645. #define MVPP2_TXD_F_DESC BIT(29)
  646. #define MVPP2_RXD_ERR_SUMMARY BIT(15)
  647. #define MVPP2_RXD_ERR_CODE_MASK (BIT(13) | BIT(14))
  648. #define MVPP2_RXD_ERR_CRC 0x0
  649. #define MVPP2_RXD_ERR_OVERRUN BIT(13)
  650. #define MVPP2_RXD_ERR_RESOURCE (BIT(13) | BIT(14))
  651. #define MVPP2_RXD_BM_POOL_ID_OFFS 16
  652. #define MVPP2_RXD_BM_POOL_ID_MASK (BIT(16) | BIT(17) | BIT(18))
  653. #define MVPP2_RXD_HWF_SYNC BIT(21)
  654. #define MVPP2_RXD_L4_CSUM_OK BIT(22)
  655. #define MVPP2_RXD_IP4_HEADER_ERR BIT(24)
  656. #define MVPP2_RXD_L4_TCP BIT(25)
  657. #define MVPP2_RXD_L4_UDP BIT(26)
  658. #define MVPP2_RXD_L3_IP4 BIT(28)
  659. #define MVPP2_RXD_L3_IP6 BIT(30)
  660. #define MVPP2_RXD_BUF_HDR BIT(31)
  661. struct mvpp2_tx_desc {
  662. u32 command; /* Options used by HW for packet transmitting.*/
  663. u8 packet_offset; /* the offset from the buffer beginning */
  664. u8 phys_txq; /* destination queue ID */
  665. u16 data_size; /* data size of transmitted packet in bytes */
  666. u32 buf_phys_addr; /* physical addr of transmitted buffer */
  667. u32 buf_cookie; /* cookie for access to TX buffer in tx path */
  668. u32 reserved1[3]; /* hw_cmd (for future use, BM, PON, PNC) */
  669. u32 reserved2; /* reserved (for future use) */
  670. };
  671. struct mvpp2_rx_desc {
  672. u32 status; /* info about received packet */
  673. u16 reserved1; /* parser_info (for future use, PnC) */
  674. u16 data_size; /* size of received packet in bytes */
  675. u32 buf_phys_addr; /* physical address of the buffer */
  676. u32 buf_cookie; /* cookie for access to RX buffer in rx path */
  677. u16 reserved2; /* gem_port_id (for future use, PON) */
  678. u16 reserved3; /* csum_l4 (for future use, PnC) */
  679. u8 reserved4; /* bm_qset (for future use, BM) */
  680. u8 reserved5;
  681. u16 reserved6; /* classify_info (for future use, PnC) */
  682. u32 reserved7; /* flow_id (for future use, PnC) */
  683. u32 reserved8;
  684. };
  685. struct mvpp2_txq_pcpu_buf {
  686. /* Transmitted SKB */
  687. struct sk_buff *skb;
  688. /* Physical address of transmitted buffer */
  689. dma_addr_t phys;
  690. /* Size transmitted */
  691. size_t size;
  692. };
  693. /* Per-CPU Tx queue control */
  694. struct mvpp2_txq_pcpu {
  695. int cpu;
  696. /* Number of Tx DMA descriptors in the descriptor ring */
  697. int size;
  698. /* Number of currently used Tx DMA descriptor in the
  699. * descriptor ring
  700. */
  701. int count;
  702. /* Number of Tx DMA descriptors reserved for each CPU */
  703. int reserved_num;
  704. /* Infos about transmitted buffers */
  705. struct mvpp2_txq_pcpu_buf *buffs;
  706. /* Index of last TX DMA descriptor that was inserted */
  707. int txq_put_index;
  708. /* Index of the TX DMA descriptor to be cleaned up */
  709. int txq_get_index;
  710. };
  711. struct mvpp2_tx_queue {
  712. /* Physical number of this Tx queue */
  713. u8 id;
  714. /* Logical number of this Tx queue */
  715. u8 log_id;
  716. /* Number of Tx DMA descriptors in the descriptor ring */
  717. int size;
  718. /* Number of currently used Tx DMA descriptor in the descriptor ring */
  719. int count;
  720. /* Per-CPU control of physical Tx queues */
  721. struct mvpp2_txq_pcpu __percpu *pcpu;
  722. u32 done_pkts_coal;
  723. /* Virtual address of thex Tx DMA descriptors array */
  724. struct mvpp2_tx_desc *descs;
  725. /* DMA address of the Tx DMA descriptors array */
  726. dma_addr_t descs_phys;
  727. /* Index of the last Tx DMA descriptor */
  728. int last_desc;
  729. /* Index of the next Tx DMA descriptor to process */
  730. int next_desc_to_proc;
  731. };
  732. struct mvpp2_rx_queue {
  733. /* RX queue number, in the range 0-31 for physical RXQs */
  734. u8 id;
  735. /* Num of rx descriptors in the rx descriptor ring */
  736. int size;
  737. u32 pkts_coal;
  738. u32 time_coal;
  739. /* Virtual address of the RX DMA descriptors array */
  740. struct mvpp2_rx_desc *descs;
  741. /* DMA address of the RX DMA descriptors array */
  742. dma_addr_t descs_phys;
  743. /* Index of the last RX DMA descriptor */
  744. int last_desc;
  745. /* Index of the next RX DMA descriptor to process */
  746. int next_desc_to_proc;
  747. /* ID of port to which physical RXQ is mapped */
  748. int port;
  749. /* Port's logic RXQ number to which physical RXQ is mapped */
  750. int logic_rxq;
  751. };
  752. union mvpp2_prs_tcam_entry {
  753. u32 word[MVPP2_PRS_TCAM_WORDS];
  754. u8 byte[MVPP2_PRS_TCAM_WORDS * 4];
  755. };
  756. union mvpp2_prs_sram_entry {
  757. u32 word[MVPP2_PRS_SRAM_WORDS];
  758. u8 byte[MVPP2_PRS_SRAM_WORDS * 4];
  759. };
  760. struct mvpp2_prs_entry {
  761. u32 index;
  762. union mvpp2_prs_tcam_entry tcam;
  763. union mvpp2_prs_sram_entry sram;
  764. };
  765. struct mvpp2_prs_shadow {
  766. bool valid;
  767. bool finish;
  768. /* Lookup ID */
  769. int lu;
  770. /* User defined offset */
  771. int udf;
  772. /* Result info */
  773. u32 ri;
  774. u32 ri_mask;
  775. };
  776. struct mvpp2_cls_flow_entry {
  777. u32 index;
  778. u32 data[MVPP2_CLS_FLOWS_TBL_DATA_WORDS];
  779. };
  780. struct mvpp2_cls_lookup_entry {
  781. u32 lkpid;
  782. u32 way;
  783. u32 data;
  784. };
  785. struct mvpp2_bm_pool {
  786. /* Pool number in the range 0-7 */
  787. int id;
  788. enum mvpp2_bm_type type;
  789. /* Buffer Pointers Pool External (BPPE) size */
  790. int size;
  791. /* Number of buffers for this pool */
  792. int buf_num;
  793. /* Pool buffer size */
  794. int buf_size;
  795. /* Packet size */
  796. int pkt_size;
  797. int frag_size;
  798. /* BPPE virtual base address */
  799. u32 *virt_addr;
  800. /* BPPE physical base address */
  801. dma_addr_t phys_addr;
  802. /* Ports using BM pool */
  803. u32 port_map;
  804. };
  805. struct mvpp2_buff_hdr {
  806. u32 next_buff_phys_addr;
  807. u32 next_buff_virt_addr;
  808. u16 byte_count;
  809. u16 info;
  810. u8 reserved1; /* bm_qset (for future use, BM) */
  811. };
  812. /* Buffer header info bits */
  813. #define MVPP2_B_HDR_INFO_MC_ID_MASK 0xfff
  814. #define MVPP2_B_HDR_INFO_MC_ID(info) ((info) & MVPP2_B_HDR_INFO_MC_ID_MASK)
  815. #define MVPP2_B_HDR_INFO_LAST_OFFS 12
  816. #define MVPP2_B_HDR_INFO_LAST_MASK BIT(12)
  817. #define MVPP2_B_HDR_INFO_IS_LAST(info) \
  818. ((info & MVPP2_B_HDR_INFO_LAST_MASK) >> MVPP2_B_HDR_INFO_LAST_OFFS)
  819. /* Static declaractions */
  820. /* Number of RXQs used by single port */
  821. static int rxq_number = MVPP2_DEFAULT_RXQ;
  822. /* Number of TXQs used by single port */
  823. static int txq_number = MVPP2_MAX_TXQ;
  824. #define MVPP2_DRIVER_NAME "mvpp2"
  825. #define MVPP2_DRIVER_VERSION "1.0"
  826. /* Utility/helper methods */
  827. static void mvpp2_write(struct mvpp2 *priv, u32 offset, u32 data)
  828. {
  829. writel(data, priv->base + offset);
  830. }
  831. static u32 mvpp2_read(struct mvpp2 *priv, u32 offset)
  832. {
  833. return readl(priv->base + offset);
  834. }
  835. static void mvpp2_txq_inc_get(struct mvpp2_txq_pcpu *txq_pcpu)
  836. {
  837. txq_pcpu->txq_get_index++;
  838. if (txq_pcpu->txq_get_index == txq_pcpu->size)
  839. txq_pcpu->txq_get_index = 0;
  840. }
  841. static void mvpp2_txq_inc_put(struct mvpp2_txq_pcpu *txq_pcpu,
  842. struct sk_buff *skb,
  843. struct mvpp2_tx_desc *tx_desc)
  844. {
  845. struct mvpp2_txq_pcpu_buf *tx_buf =
  846. txq_pcpu->buffs + txq_pcpu->txq_put_index;
  847. tx_buf->skb = skb;
  848. tx_buf->size = tx_desc->data_size;
  849. tx_buf->phys = tx_desc->buf_phys_addr + tx_desc->packet_offset;
  850. txq_pcpu->txq_put_index++;
  851. if (txq_pcpu->txq_put_index == txq_pcpu->size)
  852. txq_pcpu->txq_put_index = 0;
  853. }
  854. /* Get number of physical egress port */
  855. static inline int mvpp2_egress_port(struct mvpp2_port *port)
  856. {
  857. return MVPP2_MAX_TCONT + port->id;
  858. }
  859. /* Get number of physical TXQ */
  860. static inline int mvpp2_txq_phys(int port, int txq)
  861. {
  862. return (MVPP2_MAX_TCONT + port) * MVPP2_MAX_TXQ + txq;
  863. }
  864. /* Parser configuration routines */
  865. /* Update parser tcam and sram hw entries */
  866. static int mvpp2_prs_hw_write(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
  867. {
  868. int i;
  869. if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
  870. return -EINVAL;
  871. /* Clear entry invalidation bit */
  872. pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] &= ~MVPP2_PRS_TCAM_INV_MASK;
  873. /* Write tcam index - indirect access */
  874. mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
  875. for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
  876. mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), pe->tcam.word[i]);
  877. /* Write sram index - indirect access */
  878. mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
  879. for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
  880. mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), pe->sram.word[i]);
  881. return 0;
  882. }
  883. /* Read tcam entry from hw */
  884. static int mvpp2_prs_hw_read(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
  885. {
  886. int i;
  887. if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
  888. return -EINVAL;
  889. /* Write tcam index - indirect access */
  890. mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
  891. pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] = mvpp2_read(priv,
  892. MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD));
  893. if (pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] & MVPP2_PRS_TCAM_INV_MASK)
  894. return MVPP2_PRS_TCAM_ENTRY_INVALID;
  895. for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
  896. pe->tcam.word[i] = mvpp2_read(priv, MVPP2_PRS_TCAM_DATA_REG(i));
  897. /* Write sram index - indirect access */
  898. mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
  899. for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
  900. pe->sram.word[i] = mvpp2_read(priv, MVPP2_PRS_SRAM_DATA_REG(i));
  901. return 0;
  902. }
  903. /* Invalidate tcam hw entry */
  904. static void mvpp2_prs_hw_inv(struct mvpp2 *priv, int index)
  905. {
  906. /* Write index - indirect access */
  907. mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
  908. mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD),
  909. MVPP2_PRS_TCAM_INV_MASK);
  910. }
  911. /* Enable shadow table entry and set its lookup ID */
  912. static void mvpp2_prs_shadow_set(struct mvpp2 *priv, int index, int lu)
  913. {
  914. priv->prs_shadow[index].valid = true;
  915. priv->prs_shadow[index].lu = lu;
  916. }
  917. /* Update ri fields in shadow table entry */
  918. static void mvpp2_prs_shadow_ri_set(struct mvpp2 *priv, int index,
  919. unsigned int ri, unsigned int ri_mask)
  920. {
  921. priv->prs_shadow[index].ri_mask = ri_mask;
  922. priv->prs_shadow[index].ri = ri;
  923. }
  924. /* Update lookup field in tcam sw entry */
  925. static void mvpp2_prs_tcam_lu_set(struct mvpp2_prs_entry *pe, unsigned int lu)
  926. {
  927. int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_LU_BYTE);
  928. pe->tcam.byte[MVPP2_PRS_TCAM_LU_BYTE] = lu;
  929. pe->tcam.byte[enable_off] = MVPP2_PRS_LU_MASK;
  930. }
  931. /* Update mask for single port in tcam sw entry */
  932. static void mvpp2_prs_tcam_port_set(struct mvpp2_prs_entry *pe,
  933. unsigned int port, bool add)
  934. {
  935. int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
  936. if (add)
  937. pe->tcam.byte[enable_off] &= ~(1 << port);
  938. else
  939. pe->tcam.byte[enable_off] |= 1 << port;
  940. }
  941. /* Update port map in tcam sw entry */
  942. static void mvpp2_prs_tcam_port_map_set(struct mvpp2_prs_entry *pe,
  943. unsigned int ports)
  944. {
  945. unsigned char port_mask = MVPP2_PRS_PORT_MASK;
  946. int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
  947. pe->tcam.byte[MVPP2_PRS_TCAM_PORT_BYTE] = 0;
  948. pe->tcam.byte[enable_off] &= ~port_mask;
  949. pe->tcam.byte[enable_off] |= ~ports & MVPP2_PRS_PORT_MASK;
  950. }
  951. /* Obtain port map from tcam sw entry */
  952. static unsigned int mvpp2_prs_tcam_port_map_get(struct mvpp2_prs_entry *pe)
  953. {
  954. int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
  955. return ~(pe->tcam.byte[enable_off]) & MVPP2_PRS_PORT_MASK;
  956. }
  957. /* Set byte of data and its enable bits in tcam sw entry */
  958. static void mvpp2_prs_tcam_data_byte_set(struct mvpp2_prs_entry *pe,
  959. unsigned int offs, unsigned char byte,
  960. unsigned char enable)
  961. {
  962. pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)] = byte;
  963. pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)] = enable;
  964. }
  965. /* Get byte of data and its enable bits from tcam sw entry */
  966. static void mvpp2_prs_tcam_data_byte_get(struct mvpp2_prs_entry *pe,
  967. unsigned int offs, unsigned char *byte,
  968. unsigned char *enable)
  969. {
  970. *byte = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)];
  971. *enable = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)];
  972. }
  973. /* Compare tcam data bytes with a pattern */
  974. static bool mvpp2_prs_tcam_data_cmp(struct mvpp2_prs_entry *pe, int offs,
  975. u16 data)
  976. {
  977. int off = MVPP2_PRS_TCAM_DATA_BYTE(offs);
  978. u16 tcam_data;
  979. tcam_data = (8 << pe->tcam.byte[off + 1]) | pe->tcam.byte[off];
  980. if (tcam_data != data)
  981. return false;
  982. return true;
  983. }
  984. /* Update ai bits in tcam sw entry */
  985. static void mvpp2_prs_tcam_ai_update(struct mvpp2_prs_entry *pe,
  986. unsigned int bits, unsigned int enable)
  987. {
  988. int i, ai_idx = MVPP2_PRS_TCAM_AI_BYTE;
  989. for (i = 0; i < MVPP2_PRS_AI_BITS; i++) {
  990. if (!(enable & BIT(i)))
  991. continue;
  992. if (bits & BIT(i))
  993. pe->tcam.byte[ai_idx] |= 1 << i;
  994. else
  995. pe->tcam.byte[ai_idx] &= ~(1 << i);
  996. }
  997. pe->tcam.byte[MVPP2_PRS_TCAM_EN_OFFS(ai_idx)] |= enable;
  998. }
  999. /* Get ai bits from tcam sw entry */
  1000. static int mvpp2_prs_tcam_ai_get(struct mvpp2_prs_entry *pe)
  1001. {
  1002. return pe->tcam.byte[MVPP2_PRS_TCAM_AI_BYTE];
  1003. }
  1004. /* Set ethertype in tcam sw entry */
  1005. static void mvpp2_prs_match_etype(struct mvpp2_prs_entry *pe, int offset,
  1006. unsigned short ethertype)
  1007. {
  1008. mvpp2_prs_tcam_data_byte_set(pe, offset + 0, ethertype >> 8, 0xff);
  1009. mvpp2_prs_tcam_data_byte_set(pe, offset + 1, ethertype & 0xff, 0xff);
  1010. }
  1011. /* Set bits in sram sw entry */
  1012. static void mvpp2_prs_sram_bits_set(struct mvpp2_prs_entry *pe, int bit_num,
  1013. int val)
  1014. {
  1015. pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] |= (val << (bit_num % 8));
  1016. }
  1017. /* Clear bits in sram sw entry */
  1018. static void mvpp2_prs_sram_bits_clear(struct mvpp2_prs_entry *pe, int bit_num,
  1019. int val)
  1020. {
  1021. pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] &= ~(val << (bit_num % 8));
  1022. }
  1023. /* Update ri bits in sram sw entry */
  1024. static void mvpp2_prs_sram_ri_update(struct mvpp2_prs_entry *pe,
  1025. unsigned int bits, unsigned int mask)
  1026. {
  1027. unsigned int i;
  1028. for (i = 0; i < MVPP2_PRS_SRAM_RI_CTRL_BITS; i++) {
  1029. int ri_off = MVPP2_PRS_SRAM_RI_OFFS;
  1030. if (!(mask & BIT(i)))
  1031. continue;
  1032. if (bits & BIT(i))
  1033. mvpp2_prs_sram_bits_set(pe, ri_off + i, 1);
  1034. else
  1035. mvpp2_prs_sram_bits_clear(pe, ri_off + i, 1);
  1036. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_RI_CTRL_OFFS + i, 1);
  1037. }
  1038. }
  1039. /* Obtain ri bits from sram sw entry */
  1040. static int mvpp2_prs_sram_ri_get(struct mvpp2_prs_entry *pe)
  1041. {
  1042. return pe->sram.word[MVPP2_PRS_SRAM_RI_WORD];
  1043. }
  1044. /* Update ai bits in sram sw entry */
  1045. static void mvpp2_prs_sram_ai_update(struct mvpp2_prs_entry *pe,
  1046. unsigned int bits, unsigned int mask)
  1047. {
  1048. unsigned int i;
  1049. int ai_off = MVPP2_PRS_SRAM_AI_OFFS;
  1050. for (i = 0; i < MVPP2_PRS_SRAM_AI_CTRL_BITS; i++) {
  1051. if (!(mask & BIT(i)))
  1052. continue;
  1053. if (bits & BIT(i))
  1054. mvpp2_prs_sram_bits_set(pe, ai_off + i, 1);
  1055. else
  1056. mvpp2_prs_sram_bits_clear(pe, ai_off + i, 1);
  1057. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_AI_CTRL_OFFS + i, 1);
  1058. }
  1059. }
  1060. /* Read ai bits from sram sw entry */
  1061. static int mvpp2_prs_sram_ai_get(struct mvpp2_prs_entry *pe)
  1062. {
  1063. u8 bits;
  1064. int ai_off = MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_AI_OFFS);
  1065. int ai_en_off = ai_off + 1;
  1066. int ai_shift = MVPP2_PRS_SRAM_AI_OFFS % 8;
  1067. bits = (pe->sram.byte[ai_off] >> ai_shift) |
  1068. (pe->sram.byte[ai_en_off] << (8 - ai_shift));
  1069. return bits;
  1070. }
  1071. /* In sram sw entry set lookup ID field of the tcam key to be used in the next
  1072. * lookup interation
  1073. */
  1074. static void mvpp2_prs_sram_next_lu_set(struct mvpp2_prs_entry *pe,
  1075. unsigned int lu)
  1076. {
  1077. int sram_next_off = MVPP2_PRS_SRAM_NEXT_LU_OFFS;
  1078. mvpp2_prs_sram_bits_clear(pe, sram_next_off,
  1079. MVPP2_PRS_SRAM_NEXT_LU_MASK);
  1080. mvpp2_prs_sram_bits_set(pe, sram_next_off, lu);
  1081. }
  1082. /* In the sram sw entry set sign and value of the next lookup offset
  1083. * and the offset value generated to the classifier
  1084. */
  1085. static void mvpp2_prs_sram_shift_set(struct mvpp2_prs_entry *pe, int shift,
  1086. unsigned int op)
  1087. {
  1088. /* Set sign */
  1089. if (shift < 0) {
  1090. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
  1091. shift = 0 - shift;
  1092. } else {
  1093. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
  1094. }
  1095. /* Set value */
  1096. pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_SHIFT_OFFS)] =
  1097. (unsigned char)shift;
  1098. /* Reset and set operation */
  1099. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS,
  1100. MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK);
  1101. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS, op);
  1102. /* Set base offset as current */
  1103. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
  1104. }
  1105. /* In the sram sw entry set sign and value of the user defined offset
  1106. * generated to the classifier
  1107. */
  1108. static void mvpp2_prs_sram_offset_set(struct mvpp2_prs_entry *pe,
  1109. unsigned int type, int offset,
  1110. unsigned int op)
  1111. {
  1112. /* Set sign */
  1113. if (offset < 0) {
  1114. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
  1115. offset = 0 - offset;
  1116. } else {
  1117. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
  1118. }
  1119. /* Set value */
  1120. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_OFFS,
  1121. MVPP2_PRS_SRAM_UDF_MASK);
  1122. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_OFFS, offset);
  1123. pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
  1124. MVPP2_PRS_SRAM_UDF_BITS)] &=
  1125. ~(MVPP2_PRS_SRAM_UDF_MASK >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
  1126. pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
  1127. MVPP2_PRS_SRAM_UDF_BITS)] |=
  1128. (offset >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
  1129. /* Set offset type */
  1130. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS,
  1131. MVPP2_PRS_SRAM_UDF_TYPE_MASK);
  1132. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS, type);
  1133. /* Set offset operation */
  1134. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS,
  1135. MVPP2_PRS_SRAM_OP_SEL_UDF_MASK);
  1136. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS, op);
  1137. pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
  1138. MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] &=
  1139. ~(MVPP2_PRS_SRAM_OP_SEL_UDF_MASK >>
  1140. (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
  1141. pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
  1142. MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] |=
  1143. (op >> (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
  1144. /* Set base offset as current */
  1145. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
  1146. }
  1147. /* Find parser flow entry */
  1148. static struct mvpp2_prs_entry *mvpp2_prs_flow_find(struct mvpp2 *priv, int flow)
  1149. {
  1150. struct mvpp2_prs_entry *pe;
  1151. int tid;
  1152. pe = kzalloc(sizeof(*pe), GFP_KERNEL);
  1153. if (!pe)
  1154. return NULL;
  1155. mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
  1156. /* Go through the all entires with MVPP2_PRS_LU_FLOWS */
  1157. for (tid = MVPP2_PRS_TCAM_SRAM_SIZE - 1; tid >= 0; tid--) {
  1158. u8 bits;
  1159. if (!priv->prs_shadow[tid].valid ||
  1160. priv->prs_shadow[tid].lu != MVPP2_PRS_LU_FLOWS)
  1161. continue;
  1162. pe->index = tid;
  1163. mvpp2_prs_hw_read(priv, pe);
  1164. bits = mvpp2_prs_sram_ai_get(pe);
  1165. /* Sram store classification lookup ID in AI bits [5:0] */
  1166. if ((bits & MVPP2_PRS_FLOW_ID_MASK) == flow)
  1167. return pe;
  1168. }
  1169. kfree(pe);
  1170. return NULL;
  1171. }
  1172. /* Return first free tcam index, seeking from start to end */
  1173. static int mvpp2_prs_tcam_first_free(struct mvpp2 *priv, unsigned char start,
  1174. unsigned char end)
  1175. {
  1176. int tid;
  1177. if (start > end)
  1178. swap(start, end);
  1179. if (end >= MVPP2_PRS_TCAM_SRAM_SIZE)
  1180. end = MVPP2_PRS_TCAM_SRAM_SIZE - 1;
  1181. for (tid = start; tid <= end; tid++) {
  1182. if (!priv->prs_shadow[tid].valid)
  1183. return tid;
  1184. }
  1185. return -EINVAL;
  1186. }
  1187. /* Enable/disable dropping all mac da's */
  1188. static void mvpp2_prs_mac_drop_all_set(struct mvpp2 *priv, int port, bool add)
  1189. {
  1190. struct mvpp2_prs_entry pe;
  1191. if (priv->prs_shadow[MVPP2_PE_DROP_ALL].valid) {
  1192. /* Entry exist - update port only */
  1193. pe.index = MVPP2_PE_DROP_ALL;
  1194. mvpp2_prs_hw_read(priv, &pe);
  1195. } else {
  1196. /* Entry doesn't exist - create new */
  1197. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1198. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
  1199. pe.index = MVPP2_PE_DROP_ALL;
  1200. /* Non-promiscuous mode for all ports - DROP unknown packets */
  1201. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
  1202. MVPP2_PRS_RI_DROP_MASK);
  1203. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  1204. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  1205. /* Update shadow table */
  1206. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
  1207. /* Mask all ports */
  1208. mvpp2_prs_tcam_port_map_set(&pe, 0);
  1209. }
  1210. /* Update port mask */
  1211. mvpp2_prs_tcam_port_set(&pe, port, add);
  1212. mvpp2_prs_hw_write(priv, &pe);
  1213. }
  1214. /* Set port to promiscuous mode */
  1215. static void mvpp2_prs_mac_promisc_set(struct mvpp2 *priv, int port, bool add)
  1216. {
  1217. struct mvpp2_prs_entry pe;
  1218. /* Promiscuous mode - Accept unknown packets */
  1219. if (priv->prs_shadow[MVPP2_PE_MAC_PROMISCUOUS].valid) {
  1220. /* Entry exist - update port only */
  1221. pe.index = MVPP2_PE_MAC_PROMISCUOUS;
  1222. mvpp2_prs_hw_read(priv, &pe);
  1223. } else {
  1224. /* Entry doesn't exist - create new */
  1225. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1226. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
  1227. pe.index = MVPP2_PE_MAC_PROMISCUOUS;
  1228. /* Continue - set next lookup */
  1229. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
  1230. /* Set result info bits */
  1231. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_UCAST,
  1232. MVPP2_PRS_RI_L2_CAST_MASK);
  1233. /* Shift to ethertype */
  1234. mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
  1235. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1236. /* Mask all ports */
  1237. mvpp2_prs_tcam_port_map_set(&pe, 0);
  1238. /* Update shadow table */
  1239. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
  1240. }
  1241. /* Update port mask */
  1242. mvpp2_prs_tcam_port_set(&pe, port, add);
  1243. mvpp2_prs_hw_write(priv, &pe);
  1244. }
  1245. /* Accept multicast */
  1246. static void mvpp2_prs_mac_multi_set(struct mvpp2 *priv, int port, int index,
  1247. bool add)
  1248. {
  1249. struct mvpp2_prs_entry pe;
  1250. unsigned char da_mc;
  1251. /* Ethernet multicast address first byte is
  1252. * 0x01 for IPv4 and 0x33 for IPv6
  1253. */
  1254. da_mc = (index == MVPP2_PE_MAC_MC_ALL) ? 0x01 : 0x33;
  1255. if (priv->prs_shadow[index].valid) {
  1256. /* Entry exist - update port only */
  1257. pe.index = index;
  1258. mvpp2_prs_hw_read(priv, &pe);
  1259. } else {
  1260. /* Entry doesn't exist - create new */
  1261. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1262. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
  1263. pe.index = index;
  1264. /* Continue - set next lookup */
  1265. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
  1266. /* Set result info bits */
  1267. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_MCAST,
  1268. MVPP2_PRS_RI_L2_CAST_MASK);
  1269. /* Update tcam entry data first byte */
  1270. mvpp2_prs_tcam_data_byte_set(&pe, 0, da_mc, 0xff);
  1271. /* Shift to ethertype */
  1272. mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
  1273. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1274. /* Mask all ports */
  1275. mvpp2_prs_tcam_port_map_set(&pe, 0);
  1276. /* Update shadow table */
  1277. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
  1278. }
  1279. /* Update port mask */
  1280. mvpp2_prs_tcam_port_set(&pe, port, add);
  1281. mvpp2_prs_hw_write(priv, &pe);
  1282. }
  1283. /* Set entry for dsa packets */
  1284. static void mvpp2_prs_dsa_tag_set(struct mvpp2 *priv, int port, bool add,
  1285. bool tagged, bool extend)
  1286. {
  1287. struct mvpp2_prs_entry pe;
  1288. int tid, shift;
  1289. if (extend) {
  1290. tid = tagged ? MVPP2_PE_EDSA_TAGGED : MVPP2_PE_EDSA_UNTAGGED;
  1291. shift = 8;
  1292. } else {
  1293. tid = tagged ? MVPP2_PE_DSA_TAGGED : MVPP2_PE_DSA_UNTAGGED;
  1294. shift = 4;
  1295. }
  1296. if (priv->prs_shadow[tid].valid) {
  1297. /* Entry exist - update port only */
  1298. pe.index = tid;
  1299. mvpp2_prs_hw_read(priv, &pe);
  1300. } else {
  1301. /* Entry doesn't exist - create new */
  1302. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1303. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
  1304. pe.index = tid;
  1305. /* Shift 4 bytes if DSA tag or 8 bytes in case of EDSA tag*/
  1306. mvpp2_prs_sram_shift_set(&pe, shift,
  1307. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1308. /* Update shadow table */
  1309. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
  1310. if (tagged) {
  1311. /* Set tagged bit in DSA tag */
  1312. mvpp2_prs_tcam_data_byte_set(&pe, 0,
  1313. MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
  1314. MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
  1315. /* Clear all ai bits for next iteration */
  1316. mvpp2_prs_sram_ai_update(&pe, 0,
  1317. MVPP2_PRS_SRAM_AI_MASK);
  1318. /* If packet is tagged continue check vlans */
  1319. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
  1320. } else {
  1321. /* Set result info bits to 'no vlans' */
  1322. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
  1323. MVPP2_PRS_RI_VLAN_MASK);
  1324. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
  1325. }
  1326. /* Mask all ports */
  1327. mvpp2_prs_tcam_port_map_set(&pe, 0);
  1328. }
  1329. /* Update port mask */
  1330. mvpp2_prs_tcam_port_set(&pe, port, add);
  1331. mvpp2_prs_hw_write(priv, &pe);
  1332. }
  1333. /* Set entry for dsa ethertype */
  1334. static void mvpp2_prs_dsa_tag_ethertype_set(struct mvpp2 *priv, int port,
  1335. bool add, bool tagged, bool extend)
  1336. {
  1337. struct mvpp2_prs_entry pe;
  1338. int tid, shift, port_mask;
  1339. if (extend) {
  1340. tid = tagged ? MVPP2_PE_ETYPE_EDSA_TAGGED :
  1341. MVPP2_PE_ETYPE_EDSA_UNTAGGED;
  1342. port_mask = 0;
  1343. shift = 8;
  1344. } else {
  1345. tid = tagged ? MVPP2_PE_ETYPE_DSA_TAGGED :
  1346. MVPP2_PE_ETYPE_DSA_UNTAGGED;
  1347. port_mask = MVPP2_PRS_PORT_MASK;
  1348. shift = 4;
  1349. }
  1350. if (priv->prs_shadow[tid].valid) {
  1351. /* Entry exist - update port only */
  1352. pe.index = tid;
  1353. mvpp2_prs_hw_read(priv, &pe);
  1354. } else {
  1355. /* Entry doesn't exist - create new */
  1356. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1357. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
  1358. pe.index = tid;
  1359. /* Set ethertype */
  1360. mvpp2_prs_match_etype(&pe, 0, ETH_P_EDSA);
  1361. mvpp2_prs_match_etype(&pe, 2, 0);
  1362. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DSA_MASK,
  1363. MVPP2_PRS_RI_DSA_MASK);
  1364. /* Shift ethertype + 2 byte reserved + tag*/
  1365. mvpp2_prs_sram_shift_set(&pe, 2 + MVPP2_ETH_TYPE_LEN + shift,
  1366. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1367. /* Update shadow table */
  1368. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
  1369. if (tagged) {
  1370. /* Set tagged bit in DSA tag */
  1371. mvpp2_prs_tcam_data_byte_set(&pe,
  1372. MVPP2_ETH_TYPE_LEN + 2 + 3,
  1373. MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
  1374. MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
  1375. /* Clear all ai bits for next iteration */
  1376. mvpp2_prs_sram_ai_update(&pe, 0,
  1377. MVPP2_PRS_SRAM_AI_MASK);
  1378. /* If packet is tagged continue check vlans */
  1379. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
  1380. } else {
  1381. /* Set result info bits to 'no vlans' */
  1382. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
  1383. MVPP2_PRS_RI_VLAN_MASK);
  1384. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
  1385. }
  1386. /* Mask/unmask all ports, depending on dsa type */
  1387. mvpp2_prs_tcam_port_map_set(&pe, port_mask);
  1388. }
  1389. /* Update port mask */
  1390. mvpp2_prs_tcam_port_set(&pe, port, add);
  1391. mvpp2_prs_hw_write(priv, &pe);
  1392. }
  1393. /* Search for existing single/triple vlan entry */
  1394. static struct mvpp2_prs_entry *mvpp2_prs_vlan_find(struct mvpp2 *priv,
  1395. unsigned short tpid, int ai)
  1396. {
  1397. struct mvpp2_prs_entry *pe;
  1398. int tid;
  1399. pe = kzalloc(sizeof(*pe), GFP_KERNEL);
  1400. if (!pe)
  1401. return NULL;
  1402. mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
  1403. /* Go through the all entries with MVPP2_PRS_LU_VLAN */
  1404. for (tid = MVPP2_PE_FIRST_FREE_TID;
  1405. tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
  1406. unsigned int ri_bits, ai_bits;
  1407. bool match;
  1408. if (!priv->prs_shadow[tid].valid ||
  1409. priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
  1410. continue;
  1411. pe->index = tid;
  1412. mvpp2_prs_hw_read(priv, pe);
  1413. match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid));
  1414. if (!match)
  1415. continue;
  1416. /* Get vlan type */
  1417. ri_bits = mvpp2_prs_sram_ri_get(pe);
  1418. ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
  1419. /* Get current ai value from tcam */
  1420. ai_bits = mvpp2_prs_tcam_ai_get(pe);
  1421. /* Clear double vlan bit */
  1422. ai_bits &= ~MVPP2_PRS_DBL_VLAN_AI_BIT;
  1423. if (ai != ai_bits)
  1424. continue;
  1425. if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
  1426. ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
  1427. return pe;
  1428. }
  1429. kfree(pe);
  1430. return NULL;
  1431. }
  1432. /* Add/update single/triple vlan entry */
  1433. static int mvpp2_prs_vlan_add(struct mvpp2 *priv, unsigned short tpid, int ai,
  1434. unsigned int port_map)
  1435. {
  1436. struct mvpp2_prs_entry *pe;
  1437. int tid_aux, tid;
  1438. int ret = 0;
  1439. pe = mvpp2_prs_vlan_find(priv, tpid, ai);
  1440. if (!pe) {
  1441. /* Create new tcam entry */
  1442. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_LAST_FREE_TID,
  1443. MVPP2_PE_FIRST_FREE_TID);
  1444. if (tid < 0)
  1445. return tid;
  1446. pe = kzalloc(sizeof(*pe), GFP_KERNEL);
  1447. if (!pe)
  1448. return -ENOMEM;
  1449. /* Get last double vlan tid */
  1450. for (tid_aux = MVPP2_PE_LAST_FREE_TID;
  1451. tid_aux >= MVPP2_PE_FIRST_FREE_TID; tid_aux--) {
  1452. unsigned int ri_bits;
  1453. if (!priv->prs_shadow[tid_aux].valid ||
  1454. priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
  1455. continue;
  1456. pe->index = tid_aux;
  1457. mvpp2_prs_hw_read(priv, pe);
  1458. ri_bits = mvpp2_prs_sram_ri_get(pe);
  1459. if ((ri_bits & MVPP2_PRS_RI_VLAN_MASK) ==
  1460. MVPP2_PRS_RI_VLAN_DOUBLE)
  1461. break;
  1462. }
  1463. if (tid <= tid_aux) {
  1464. ret = -EINVAL;
  1465. goto error;
  1466. }
  1467. memset(pe, 0 , sizeof(struct mvpp2_prs_entry));
  1468. mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
  1469. pe->index = tid;
  1470. mvpp2_prs_match_etype(pe, 0, tpid);
  1471. mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_L2);
  1472. /* Shift 4 bytes - skip 1 vlan tag */
  1473. mvpp2_prs_sram_shift_set(pe, MVPP2_VLAN_TAG_LEN,
  1474. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1475. /* Clear all ai bits for next iteration */
  1476. mvpp2_prs_sram_ai_update(pe, 0, MVPP2_PRS_SRAM_AI_MASK);
  1477. if (ai == MVPP2_PRS_SINGLE_VLAN_AI) {
  1478. mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_SINGLE,
  1479. MVPP2_PRS_RI_VLAN_MASK);
  1480. } else {
  1481. ai |= MVPP2_PRS_DBL_VLAN_AI_BIT;
  1482. mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_TRIPLE,
  1483. MVPP2_PRS_RI_VLAN_MASK);
  1484. }
  1485. mvpp2_prs_tcam_ai_update(pe, ai, MVPP2_PRS_SRAM_AI_MASK);
  1486. mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
  1487. }
  1488. /* Update ports' mask */
  1489. mvpp2_prs_tcam_port_map_set(pe, port_map);
  1490. mvpp2_prs_hw_write(priv, pe);
  1491. error:
  1492. kfree(pe);
  1493. return ret;
  1494. }
  1495. /* Get first free double vlan ai number */
  1496. static int mvpp2_prs_double_vlan_ai_free_get(struct mvpp2 *priv)
  1497. {
  1498. int i;
  1499. for (i = 1; i < MVPP2_PRS_DBL_VLANS_MAX; i++) {
  1500. if (!priv->prs_double_vlans[i])
  1501. return i;
  1502. }
  1503. return -EINVAL;
  1504. }
  1505. /* Search for existing double vlan entry */
  1506. static struct mvpp2_prs_entry *mvpp2_prs_double_vlan_find(struct mvpp2 *priv,
  1507. unsigned short tpid1,
  1508. unsigned short tpid2)
  1509. {
  1510. struct mvpp2_prs_entry *pe;
  1511. int tid;
  1512. pe = kzalloc(sizeof(*pe), GFP_KERNEL);
  1513. if (!pe)
  1514. return NULL;
  1515. mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
  1516. /* Go through the all entries with MVPP2_PRS_LU_VLAN */
  1517. for (tid = MVPP2_PE_FIRST_FREE_TID;
  1518. tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
  1519. unsigned int ri_mask;
  1520. bool match;
  1521. if (!priv->prs_shadow[tid].valid ||
  1522. priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
  1523. continue;
  1524. pe->index = tid;
  1525. mvpp2_prs_hw_read(priv, pe);
  1526. match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid1))
  1527. && mvpp2_prs_tcam_data_cmp(pe, 4, swab16(tpid2));
  1528. if (!match)
  1529. continue;
  1530. ri_mask = mvpp2_prs_sram_ri_get(pe) & MVPP2_PRS_RI_VLAN_MASK;
  1531. if (ri_mask == MVPP2_PRS_RI_VLAN_DOUBLE)
  1532. return pe;
  1533. }
  1534. kfree(pe);
  1535. return NULL;
  1536. }
  1537. /* Add or update double vlan entry */
  1538. static int mvpp2_prs_double_vlan_add(struct mvpp2 *priv, unsigned short tpid1,
  1539. unsigned short tpid2,
  1540. unsigned int port_map)
  1541. {
  1542. struct mvpp2_prs_entry *pe;
  1543. int tid_aux, tid, ai, ret = 0;
  1544. pe = mvpp2_prs_double_vlan_find(priv, tpid1, tpid2);
  1545. if (!pe) {
  1546. /* Create new tcam entry */
  1547. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1548. MVPP2_PE_LAST_FREE_TID);
  1549. if (tid < 0)
  1550. return tid;
  1551. pe = kzalloc(sizeof(*pe), GFP_KERNEL);
  1552. if (!pe)
  1553. return -ENOMEM;
  1554. /* Set ai value for new double vlan entry */
  1555. ai = mvpp2_prs_double_vlan_ai_free_get(priv);
  1556. if (ai < 0) {
  1557. ret = ai;
  1558. goto error;
  1559. }
  1560. /* Get first single/triple vlan tid */
  1561. for (tid_aux = MVPP2_PE_FIRST_FREE_TID;
  1562. tid_aux <= MVPP2_PE_LAST_FREE_TID; tid_aux++) {
  1563. unsigned int ri_bits;
  1564. if (!priv->prs_shadow[tid_aux].valid ||
  1565. priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
  1566. continue;
  1567. pe->index = tid_aux;
  1568. mvpp2_prs_hw_read(priv, pe);
  1569. ri_bits = mvpp2_prs_sram_ri_get(pe);
  1570. ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
  1571. if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
  1572. ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
  1573. break;
  1574. }
  1575. if (tid >= tid_aux) {
  1576. ret = -ERANGE;
  1577. goto error;
  1578. }
  1579. memset(pe, 0, sizeof(struct mvpp2_prs_entry));
  1580. mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
  1581. pe->index = tid;
  1582. priv->prs_double_vlans[ai] = true;
  1583. mvpp2_prs_match_etype(pe, 0, tpid1);
  1584. mvpp2_prs_match_etype(pe, 4, tpid2);
  1585. mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_VLAN);
  1586. /* Shift 8 bytes - skip 2 vlan tags */
  1587. mvpp2_prs_sram_shift_set(pe, 2 * MVPP2_VLAN_TAG_LEN,
  1588. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1589. mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_DOUBLE,
  1590. MVPP2_PRS_RI_VLAN_MASK);
  1591. mvpp2_prs_sram_ai_update(pe, ai | MVPP2_PRS_DBL_VLAN_AI_BIT,
  1592. MVPP2_PRS_SRAM_AI_MASK);
  1593. mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
  1594. }
  1595. /* Update ports' mask */
  1596. mvpp2_prs_tcam_port_map_set(pe, port_map);
  1597. mvpp2_prs_hw_write(priv, pe);
  1598. error:
  1599. kfree(pe);
  1600. return ret;
  1601. }
  1602. /* IPv4 header parsing for fragmentation and L4 offset */
  1603. static int mvpp2_prs_ip4_proto(struct mvpp2 *priv, unsigned short proto,
  1604. unsigned int ri, unsigned int ri_mask)
  1605. {
  1606. struct mvpp2_prs_entry pe;
  1607. int tid;
  1608. if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
  1609. (proto != IPPROTO_IGMP))
  1610. return -EINVAL;
  1611. /* Fragmented packet */
  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_IP4);
  1618. pe.index = tid;
  1619. /* Set next lu to IPv4 */
  1620. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
  1621. mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1622. /* Set L4 offset */
  1623. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
  1624. sizeof(struct iphdr) - 4,
  1625. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  1626. mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
  1627. MVPP2_PRS_IPV4_DIP_AI_BIT);
  1628. mvpp2_prs_sram_ri_update(&pe, ri | MVPP2_PRS_RI_IP_FRAG_MASK,
  1629. ri_mask | MVPP2_PRS_RI_IP_FRAG_MASK);
  1630. mvpp2_prs_tcam_data_byte_set(&pe, 5, proto, MVPP2_PRS_TCAM_PROTO_MASK);
  1631. mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
  1632. /* Unmask all ports */
  1633. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  1634. /* Update shadow table and hw entry */
  1635. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
  1636. mvpp2_prs_hw_write(priv, &pe);
  1637. /* Not fragmented packet */
  1638. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1639. MVPP2_PE_LAST_FREE_TID);
  1640. if (tid < 0)
  1641. return tid;
  1642. pe.index = tid;
  1643. /* Clear ri before updating */
  1644. pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
  1645. pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
  1646. mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
  1647. mvpp2_prs_tcam_data_byte_set(&pe, 2, 0x00, MVPP2_PRS_TCAM_PROTO_MASK_L);
  1648. mvpp2_prs_tcam_data_byte_set(&pe, 3, 0x00, MVPP2_PRS_TCAM_PROTO_MASK);
  1649. /* Update shadow table and hw entry */
  1650. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
  1651. mvpp2_prs_hw_write(priv, &pe);
  1652. return 0;
  1653. }
  1654. /* IPv4 L3 multicast or broadcast */
  1655. static int mvpp2_prs_ip4_cast(struct mvpp2 *priv, unsigned short l3_cast)
  1656. {
  1657. struct mvpp2_prs_entry pe;
  1658. int mask, tid;
  1659. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1660. MVPP2_PE_LAST_FREE_TID);
  1661. if (tid < 0)
  1662. return tid;
  1663. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1664. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
  1665. pe.index = tid;
  1666. switch (l3_cast) {
  1667. case MVPP2_PRS_L3_MULTI_CAST:
  1668. mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV4_MC,
  1669. MVPP2_PRS_IPV4_MC_MASK);
  1670. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
  1671. MVPP2_PRS_RI_L3_ADDR_MASK);
  1672. break;
  1673. case MVPP2_PRS_L3_BROAD_CAST:
  1674. mask = MVPP2_PRS_IPV4_BC_MASK;
  1675. mvpp2_prs_tcam_data_byte_set(&pe, 0, mask, mask);
  1676. mvpp2_prs_tcam_data_byte_set(&pe, 1, mask, mask);
  1677. mvpp2_prs_tcam_data_byte_set(&pe, 2, mask, mask);
  1678. mvpp2_prs_tcam_data_byte_set(&pe, 3, mask, mask);
  1679. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_BCAST,
  1680. MVPP2_PRS_RI_L3_ADDR_MASK);
  1681. break;
  1682. default:
  1683. return -EINVAL;
  1684. }
  1685. /* Finished: go to flowid generation */
  1686. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  1687. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  1688. mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
  1689. MVPP2_PRS_IPV4_DIP_AI_BIT);
  1690. /* Unmask all ports */
  1691. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  1692. /* Update shadow table and hw entry */
  1693. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
  1694. mvpp2_prs_hw_write(priv, &pe);
  1695. return 0;
  1696. }
  1697. /* Set entries for protocols over IPv6 */
  1698. static int mvpp2_prs_ip6_proto(struct mvpp2 *priv, unsigned short proto,
  1699. unsigned int ri, unsigned int ri_mask)
  1700. {
  1701. struct mvpp2_prs_entry pe;
  1702. int tid;
  1703. if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
  1704. (proto != IPPROTO_ICMPV6) && (proto != IPPROTO_IPIP))
  1705. return -EINVAL;
  1706. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1707. MVPP2_PE_LAST_FREE_TID);
  1708. if (tid < 0)
  1709. return tid;
  1710. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1711. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
  1712. pe.index = tid;
  1713. /* Finished: go to flowid generation */
  1714. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  1715. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  1716. mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
  1717. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
  1718. sizeof(struct ipv6hdr) - 6,
  1719. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  1720. mvpp2_prs_tcam_data_byte_set(&pe, 0, proto, MVPP2_PRS_TCAM_PROTO_MASK);
  1721. mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
  1722. MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
  1723. /* Unmask all ports */
  1724. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  1725. /* Write HW */
  1726. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
  1727. mvpp2_prs_hw_write(priv, &pe);
  1728. return 0;
  1729. }
  1730. /* IPv6 L3 multicast entry */
  1731. static int mvpp2_prs_ip6_cast(struct mvpp2 *priv, unsigned short l3_cast)
  1732. {
  1733. struct mvpp2_prs_entry pe;
  1734. int tid;
  1735. if (l3_cast != MVPP2_PRS_L3_MULTI_CAST)
  1736. return -EINVAL;
  1737. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1738. MVPP2_PE_LAST_FREE_TID);
  1739. if (tid < 0)
  1740. return tid;
  1741. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1742. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
  1743. pe.index = tid;
  1744. /* Finished: go to flowid generation */
  1745. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
  1746. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
  1747. MVPP2_PRS_RI_L3_ADDR_MASK);
  1748. mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
  1749. MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
  1750. /* Shift back to IPv6 NH */
  1751. mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1752. mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV6_MC,
  1753. MVPP2_PRS_IPV6_MC_MASK);
  1754. mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
  1755. /* Unmask all ports */
  1756. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  1757. /* Update shadow table and hw entry */
  1758. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
  1759. mvpp2_prs_hw_write(priv, &pe);
  1760. return 0;
  1761. }
  1762. /* Parser per-port initialization */
  1763. static void mvpp2_prs_hw_port_init(struct mvpp2 *priv, int port, int lu_first,
  1764. int lu_max, int offset)
  1765. {
  1766. u32 val;
  1767. /* Set lookup ID */
  1768. val = mvpp2_read(priv, MVPP2_PRS_INIT_LOOKUP_REG);
  1769. val &= ~MVPP2_PRS_PORT_LU_MASK(port);
  1770. val |= MVPP2_PRS_PORT_LU_VAL(port, lu_first);
  1771. mvpp2_write(priv, MVPP2_PRS_INIT_LOOKUP_REG, val);
  1772. /* Set maximum number of loops for packet received from port */
  1773. val = mvpp2_read(priv, MVPP2_PRS_MAX_LOOP_REG(port));
  1774. val &= ~MVPP2_PRS_MAX_LOOP_MASK(port);
  1775. val |= MVPP2_PRS_MAX_LOOP_VAL(port, lu_max);
  1776. mvpp2_write(priv, MVPP2_PRS_MAX_LOOP_REG(port), val);
  1777. /* Set initial offset for packet header extraction for the first
  1778. * searching loop
  1779. */
  1780. val = mvpp2_read(priv, MVPP2_PRS_INIT_OFFS_REG(port));
  1781. val &= ~MVPP2_PRS_INIT_OFF_MASK(port);
  1782. val |= MVPP2_PRS_INIT_OFF_VAL(port, offset);
  1783. mvpp2_write(priv, MVPP2_PRS_INIT_OFFS_REG(port), val);
  1784. }
  1785. /* Default flow entries initialization for all ports */
  1786. static void mvpp2_prs_def_flow_init(struct mvpp2 *priv)
  1787. {
  1788. struct mvpp2_prs_entry pe;
  1789. int port;
  1790. for (port = 0; port < MVPP2_MAX_PORTS; port++) {
  1791. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1792. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  1793. pe.index = MVPP2_PE_FIRST_DEFAULT_FLOW - port;
  1794. /* Mask all ports */
  1795. mvpp2_prs_tcam_port_map_set(&pe, 0);
  1796. /* Set flow ID*/
  1797. mvpp2_prs_sram_ai_update(&pe, port, MVPP2_PRS_FLOW_ID_MASK);
  1798. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
  1799. /* Update shadow table and hw entry */
  1800. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_FLOWS);
  1801. mvpp2_prs_hw_write(priv, &pe);
  1802. }
  1803. }
  1804. /* Set default entry for Marvell Header field */
  1805. static void mvpp2_prs_mh_init(struct mvpp2 *priv)
  1806. {
  1807. struct mvpp2_prs_entry pe;
  1808. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1809. pe.index = MVPP2_PE_MH_DEFAULT;
  1810. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MH);
  1811. mvpp2_prs_sram_shift_set(&pe, MVPP2_MH_SIZE,
  1812. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1813. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_MAC);
  1814. /* Unmask all ports */
  1815. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  1816. /* Update shadow table and hw entry */
  1817. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MH);
  1818. mvpp2_prs_hw_write(priv, &pe);
  1819. }
  1820. /* Set default entires (place holder) for promiscuous, non-promiscuous and
  1821. * multicast MAC addresses
  1822. */
  1823. static void mvpp2_prs_mac_init(struct mvpp2 *priv)
  1824. {
  1825. struct mvpp2_prs_entry pe;
  1826. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1827. /* Non-promiscuous mode for all ports - DROP unknown packets */
  1828. pe.index = MVPP2_PE_MAC_NON_PROMISCUOUS;
  1829. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
  1830. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
  1831. MVPP2_PRS_RI_DROP_MASK);
  1832. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  1833. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  1834. /* Unmask all ports */
  1835. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  1836. /* Update shadow table and hw entry */
  1837. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
  1838. mvpp2_prs_hw_write(priv, &pe);
  1839. /* place holders only - no ports */
  1840. mvpp2_prs_mac_drop_all_set(priv, 0, false);
  1841. mvpp2_prs_mac_promisc_set(priv, 0, false);
  1842. mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_ALL, 0, false);
  1843. mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_IP6, 0, false);
  1844. }
  1845. /* Set default entries for various types of dsa packets */
  1846. static void mvpp2_prs_dsa_init(struct mvpp2 *priv)
  1847. {
  1848. struct mvpp2_prs_entry pe;
  1849. /* None tagged EDSA entry - place holder */
  1850. mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
  1851. MVPP2_PRS_EDSA);
  1852. /* Tagged EDSA entry - place holder */
  1853. mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
  1854. /* None tagged DSA entry - place holder */
  1855. mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
  1856. MVPP2_PRS_DSA);
  1857. /* Tagged DSA entry - place holder */
  1858. mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
  1859. /* None tagged EDSA ethertype entry - place holder*/
  1860. mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
  1861. MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
  1862. /* Tagged EDSA ethertype entry - place holder*/
  1863. mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
  1864. MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
  1865. /* None tagged DSA ethertype entry */
  1866. mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
  1867. MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
  1868. /* Tagged DSA ethertype entry */
  1869. mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
  1870. MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
  1871. /* Set default entry, in case DSA or EDSA tag not found */
  1872. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1873. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
  1874. pe.index = MVPP2_PE_DSA_DEFAULT;
  1875. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
  1876. /* Shift 0 bytes */
  1877. mvpp2_prs_sram_shift_set(&pe, 0, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1878. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
  1879. /* Clear all sram ai bits for next iteration */
  1880. mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
  1881. /* Unmask all ports */
  1882. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  1883. mvpp2_prs_hw_write(priv, &pe);
  1884. }
  1885. /* Match basic ethertypes */
  1886. static int mvpp2_prs_etype_init(struct mvpp2 *priv)
  1887. {
  1888. struct mvpp2_prs_entry pe;
  1889. int tid;
  1890. /* Ethertype: PPPoE */
  1891. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1892. MVPP2_PE_LAST_FREE_TID);
  1893. if (tid < 0)
  1894. return tid;
  1895. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1896. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  1897. pe.index = tid;
  1898. mvpp2_prs_match_etype(&pe, 0, ETH_P_PPP_SES);
  1899. mvpp2_prs_sram_shift_set(&pe, MVPP2_PPPOE_HDR_SIZE,
  1900. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1901. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
  1902. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_PPPOE_MASK,
  1903. MVPP2_PRS_RI_PPPOE_MASK);
  1904. /* Update shadow table and hw entry */
  1905. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1906. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1907. priv->prs_shadow[pe.index].finish = false;
  1908. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_PPPOE_MASK,
  1909. MVPP2_PRS_RI_PPPOE_MASK);
  1910. mvpp2_prs_hw_write(priv, &pe);
  1911. /* Ethertype: ARP */
  1912. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1913. MVPP2_PE_LAST_FREE_TID);
  1914. if (tid < 0)
  1915. return tid;
  1916. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1917. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  1918. pe.index = tid;
  1919. mvpp2_prs_match_etype(&pe, 0, ETH_P_ARP);
  1920. /* Generate flow in the next iteration*/
  1921. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  1922. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  1923. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_ARP,
  1924. MVPP2_PRS_RI_L3_PROTO_MASK);
  1925. /* Set L3 offset */
  1926. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  1927. MVPP2_ETH_TYPE_LEN,
  1928. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  1929. /* Update shadow table and hw entry */
  1930. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1931. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1932. priv->prs_shadow[pe.index].finish = true;
  1933. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_ARP,
  1934. MVPP2_PRS_RI_L3_PROTO_MASK);
  1935. mvpp2_prs_hw_write(priv, &pe);
  1936. /* Ethertype: LBTD */
  1937. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1938. MVPP2_PE_LAST_FREE_TID);
  1939. if (tid < 0)
  1940. return tid;
  1941. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1942. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  1943. pe.index = tid;
  1944. mvpp2_prs_match_etype(&pe, 0, MVPP2_IP_LBDT_TYPE);
  1945. /* Generate flow in the next iteration*/
  1946. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  1947. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  1948. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
  1949. MVPP2_PRS_RI_UDF3_RX_SPECIAL,
  1950. MVPP2_PRS_RI_CPU_CODE_MASK |
  1951. MVPP2_PRS_RI_UDF3_MASK);
  1952. /* Set L3 offset */
  1953. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  1954. MVPP2_ETH_TYPE_LEN,
  1955. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  1956. /* Update shadow table and hw entry */
  1957. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1958. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1959. priv->prs_shadow[pe.index].finish = true;
  1960. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
  1961. MVPP2_PRS_RI_UDF3_RX_SPECIAL,
  1962. MVPP2_PRS_RI_CPU_CODE_MASK |
  1963. MVPP2_PRS_RI_UDF3_MASK);
  1964. mvpp2_prs_hw_write(priv, &pe);
  1965. /* Ethertype: IPv4 without options */
  1966. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1967. MVPP2_PE_LAST_FREE_TID);
  1968. if (tid < 0)
  1969. return tid;
  1970. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  1971. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  1972. pe.index = tid;
  1973. mvpp2_prs_match_etype(&pe, 0, ETH_P_IP);
  1974. mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
  1975. MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
  1976. MVPP2_PRS_IPV4_HEAD_MASK |
  1977. MVPP2_PRS_IPV4_IHL_MASK);
  1978. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
  1979. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
  1980. MVPP2_PRS_RI_L3_PROTO_MASK);
  1981. /* Skip eth_type + 4 bytes of IP header */
  1982. mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
  1983. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1984. /* Set L3 offset */
  1985. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  1986. MVPP2_ETH_TYPE_LEN,
  1987. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  1988. /* Update shadow table and hw entry */
  1989. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  1990. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  1991. priv->prs_shadow[pe.index].finish = false;
  1992. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4,
  1993. MVPP2_PRS_RI_L3_PROTO_MASK);
  1994. mvpp2_prs_hw_write(priv, &pe);
  1995. /* Ethertype: IPv4 with options */
  1996. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1997. MVPP2_PE_LAST_FREE_TID);
  1998. if (tid < 0)
  1999. return tid;
  2000. pe.index = tid;
  2001. /* Clear tcam data before updating */
  2002. pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(MVPP2_ETH_TYPE_LEN)] = 0x0;
  2003. pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(MVPP2_ETH_TYPE_LEN)] = 0x0;
  2004. mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
  2005. MVPP2_PRS_IPV4_HEAD,
  2006. MVPP2_PRS_IPV4_HEAD_MASK);
  2007. /* Clear ri before updating */
  2008. pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
  2009. pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
  2010. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
  2011. MVPP2_PRS_RI_L3_PROTO_MASK);
  2012. /* Update shadow table and hw entry */
  2013. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  2014. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  2015. priv->prs_shadow[pe.index].finish = false;
  2016. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4_OPT,
  2017. MVPP2_PRS_RI_L3_PROTO_MASK);
  2018. mvpp2_prs_hw_write(priv, &pe);
  2019. /* Ethertype: IPv6 without options */
  2020. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  2021. MVPP2_PE_LAST_FREE_TID);
  2022. if (tid < 0)
  2023. return tid;
  2024. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  2025. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  2026. pe.index = tid;
  2027. mvpp2_prs_match_etype(&pe, 0, ETH_P_IPV6);
  2028. /* Skip DIP of IPV6 header */
  2029. mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 8 +
  2030. MVPP2_MAX_L3_ADDR_SIZE,
  2031. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  2032. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
  2033. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
  2034. MVPP2_PRS_RI_L3_PROTO_MASK);
  2035. /* Set L3 offset */
  2036. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  2037. MVPP2_ETH_TYPE_LEN,
  2038. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  2039. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  2040. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  2041. priv->prs_shadow[pe.index].finish = false;
  2042. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP6,
  2043. MVPP2_PRS_RI_L3_PROTO_MASK);
  2044. mvpp2_prs_hw_write(priv, &pe);
  2045. /* Default entry for MVPP2_PRS_LU_L2 - Unknown ethtype */
  2046. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  2047. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  2048. pe.index = MVPP2_PE_ETH_TYPE_UN;
  2049. /* Unmask all ports */
  2050. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  2051. /* Generate flow in the next iteration*/
  2052. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  2053. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  2054. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
  2055. MVPP2_PRS_RI_L3_PROTO_MASK);
  2056. /* Set L3 offset even it's unknown L3 */
  2057. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  2058. MVPP2_ETH_TYPE_LEN,
  2059. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  2060. /* Update shadow table and hw entry */
  2061. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  2062. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  2063. priv->prs_shadow[pe.index].finish = true;
  2064. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_UN,
  2065. MVPP2_PRS_RI_L3_PROTO_MASK);
  2066. mvpp2_prs_hw_write(priv, &pe);
  2067. return 0;
  2068. }
  2069. /* Configure vlan entries and detect up to 2 successive VLAN tags.
  2070. * Possible options:
  2071. * 0x8100, 0x88A8
  2072. * 0x8100, 0x8100
  2073. * 0x8100
  2074. * 0x88A8
  2075. */
  2076. static int mvpp2_prs_vlan_init(struct platform_device *pdev, struct mvpp2 *priv)
  2077. {
  2078. struct mvpp2_prs_entry pe;
  2079. int err;
  2080. priv->prs_double_vlans = devm_kcalloc(&pdev->dev, sizeof(bool),
  2081. MVPP2_PRS_DBL_VLANS_MAX,
  2082. GFP_KERNEL);
  2083. if (!priv->prs_double_vlans)
  2084. return -ENOMEM;
  2085. /* Double VLAN: 0x8100, 0x88A8 */
  2086. err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021AD,
  2087. MVPP2_PRS_PORT_MASK);
  2088. if (err)
  2089. return err;
  2090. /* Double VLAN: 0x8100, 0x8100 */
  2091. err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021Q,
  2092. MVPP2_PRS_PORT_MASK);
  2093. if (err)
  2094. return err;
  2095. /* Single VLAN: 0x88a8 */
  2096. err = mvpp2_prs_vlan_add(priv, ETH_P_8021AD, MVPP2_PRS_SINGLE_VLAN_AI,
  2097. MVPP2_PRS_PORT_MASK);
  2098. if (err)
  2099. return err;
  2100. /* Single VLAN: 0x8100 */
  2101. err = mvpp2_prs_vlan_add(priv, ETH_P_8021Q, MVPP2_PRS_SINGLE_VLAN_AI,
  2102. MVPP2_PRS_PORT_MASK);
  2103. if (err)
  2104. return err;
  2105. /* Set default double vlan entry */
  2106. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  2107. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
  2108. pe.index = MVPP2_PE_VLAN_DBL;
  2109. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
  2110. /* Clear ai for next iterations */
  2111. mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
  2112. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_DOUBLE,
  2113. MVPP2_PRS_RI_VLAN_MASK);
  2114. mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_DBL_VLAN_AI_BIT,
  2115. MVPP2_PRS_DBL_VLAN_AI_BIT);
  2116. /* Unmask all ports */
  2117. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  2118. /* Update shadow table and hw entry */
  2119. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
  2120. mvpp2_prs_hw_write(priv, &pe);
  2121. /* Set default vlan none entry */
  2122. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  2123. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
  2124. pe.index = MVPP2_PE_VLAN_NONE;
  2125. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
  2126. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
  2127. MVPP2_PRS_RI_VLAN_MASK);
  2128. /* Unmask all ports */
  2129. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  2130. /* Update shadow table and hw entry */
  2131. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
  2132. mvpp2_prs_hw_write(priv, &pe);
  2133. return 0;
  2134. }
  2135. /* Set entries for PPPoE ethertype */
  2136. static int mvpp2_prs_pppoe_init(struct mvpp2 *priv)
  2137. {
  2138. struct mvpp2_prs_entry pe;
  2139. int tid;
  2140. /* IPv4 over PPPoE with options */
  2141. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  2142. MVPP2_PE_LAST_FREE_TID);
  2143. if (tid < 0)
  2144. return tid;
  2145. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  2146. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
  2147. pe.index = tid;
  2148. mvpp2_prs_match_etype(&pe, 0, PPP_IP);
  2149. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
  2150. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
  2151. MVPP2_PRS_RI_L3_PROTO_MASK);
  2152. /* Skip eth_type + 4 bytes of IP header */
  2153. mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
  2154. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  2155. /* Set L3 offset */
  2156. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  2157. MVPP2_ETH_TYPE_LEN,
  2158. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  2159. /* Update shadow table and hw entry */
  2160. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
  2161. mvpp2_prs_hw_write(priv, &pe);
  2162. /* IPv4 over PPPoE without options */
  2163. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  2164. MVPP2_PE_LAST_FREE_TID);
  2165. if (tid < 0)
  2166. return tid;
  2167. pe.index = tid;
  2168. mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
  2169. MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
  2170. MVPP2_PRS_IPV4_HEAD_MASK |
  2171. MVPP2_PRS_IPV4_IHL_MASK);
  2172. /* Clear ri before updating */
  2173. pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
  2174. pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
  2175. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
  2176. MVPP2_PRS_RI_L3_PROTO_MASK);
  2177. /* Update shadow table and hw entry */
  2178. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
  2179. mvpp2_prs_hw_write(priv, &pe);
  2180. /* IPv6 over PPPoE */
  2181. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  2182. MVPP2_PE_LAST_FREE_TID);
  2183. if (tid < 0)
  2184. return tid;
  2185. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  2186. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
  2187. pe.index = tid;
  2188. mvpp2_prs_match_etype(&pe, 0, PPP_IPV6);
  2189. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
  2190. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
  2191. MVPP2_PRS_RI_L3_PROTO_MASK);
  2192. /* Skip eth_type + 4 bytes of IPv6 header */
  2193. mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
  2194. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  2195. /* Set L3 offset */
  2196. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  2197. MVPP2_ETH_TYPE_LEN,
  2198. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  2199. /* Update shadow table and hw entry */
  2200. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
  2201. mvpp2_prs_hw_write(priv, &pe);
  2202. /* Non-IP over PPPoE */
  2203. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  2204. MVPP2_PE_LAST_FREE_TID);
  2205. if (tid < 0)
  2206. return tid;
  2207. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  2208. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
  2209. pe.index = tid;
  2210. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
  2211. MVPP2_PRS_RI_L3_PROTO_MASK);
  2212. /* Finished: go to flowid generation */
  2213. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  2214. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  2215. /* Set L3 offset even if it's unknown L3 */
  2216. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  2217. MVPP2_ETH_TYPE_LEN,
  2218. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  2219. /* Update shadow table and hw entry */
  2220. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
  2221. mvpp2_prs_hw_write(priv, &pe);
  2222. return 0;
  2223. }
  2224. /* Initialize entries for IPv4 */
  2225. static int mvpp2_prs_ip4_init(struct mvpp2 *priv)
  2226. {
  2227. struct mvpp2_prs_entry pe;
  2228. int err;
  2229. /* Set entries for TCP, UDP and IGMP over IPv4 */
  2230. err = mvpp2_prs_ip4_proto(priv, IPPROTO_TCP, MVPP2_PRS_RI_L4_TCP,
  2231. MVPP2_PRS_RI_L4_PROTO_MASK);
  2232. if (err)
  2233. return err;
  2234. err = mvpp2_prs_ip4_proto(priv, IPPROTO_UDP, MVPP2_PRS_RI_L4_UDP,
  2235. MVPP2_PRS_RI_L4_PROTO_MASK);
  2236. if (err)
  2237. return err;
  2238. err = mvpp2_prs_ip4_proto(priv, IPPROTO_IGMP,
  2239. MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
  2240. MVPP2_PRS_RI_UDF3_RX_SPECIAL,
  2241. MVPP2_PRS_RI_CPU_CODE_MASK |
  2242. MVPP2_PRS_RI_UDF3_MASK);
  2243. if (err)
  2244. return err;
  2245. /* IPv4 Broadcast */
  2246. err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_BROAD_CAST);
  2247. if (err)
  2248. return err;
  2249. /* IPv4 Multicast */
  2250. err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
  2251. if (err)
  2252. return err;
  2253. /* Default IPv4 entry for unknown protocols */
  2254. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  2255. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
  2256. pe.index = MVPP2_PE_IP4_PROTO_UN;
  2257. /* Set next lu to IPv4 */
  2258. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
  2259. mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  2260. /* Set L4 offset */
  2261. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
  2262. sizeof(struct iphdr) - 4,
  2263. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  2264. mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
  2265. MVPP2_PRS_IPV4_DIP_AI_BIT);
  2266. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
  2267. MVPP2_PRS_RI_L4_PROTO_MASK);
  2268. mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
  2269. /* Unmask all ports */
  2270. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  2271. /* Update shadow table and hw entry */
  2272. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
  2273. mvpp2_prs_hw_write(priv, &pe);
  2274. /* Default IPv4 entry for unicast address */
  2275. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  2276. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
  2277. pe.index = MVPP2_PE_IP4_ADDR_UN;
  2278. /* Finished: go to flowid generation */
  2279. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  2280. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  2281. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
  2282. MVPP2_PRS_RI_L3_ADDR_MASK);
  2283. mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
  2284. MVPP2_PRS_IPV4_DIP_AI_BIT);
  2285. /* Unmask all ports */
  2286. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  2287. /* Update shadow table and hw entry */
  2288. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
  2289. mvpp2_prs_hw_write(priv, &pe);
  2290. return 0;
  2291. }
  2292. /* Initialize entries for IPv6 */
  2293. static int mvpp2_prs_ip6_init(struct mvpp2 *priv)
  2294. {
  2295. struct mvpp2_prs_entry pe;
  2296. int tid, err;
  2297. /* Set entries for TCP, UDP and ICMP over IPv6 */
  2298. err = mvpp2_prs_ip6_proto(priv, IPPROTO_TCP,
  2299. MVPP2_PRS_RI_L4_TCP,
  2300. MVPP2_PRS_RI_L4_PROTO_MASK);
  2301. if (err)
  2302. return err;
  2303. err = mvpp2_prs_ip6_proto(priv, IPPROTO_UDP,
  2304. MVPP2_PRS_RI_L4_UDP,
  2305. MVPP2_PRS_RI_L4_PROTO_MASK);
  2306. if (err)
  2307. return err;
  2308. err = mvpp2_prs_ip6_proto(priv, IPPROTO_ICMPV6,
  2309. MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
  2310. MVPP2_PRS_RI_UDF3_RX_SPECIAL,
  2311. MVPP2_PRS_RI_CPU_CODE_MASK |
  2312. MVPP2_PRS_RI_UDF3_MASK);
  2313. if (err)
  2314. return err;
  2315. /* IPv4 is the last header. This is similar case as 6-TCP or 17-UDP */
  2316. /* Result Info: UDF7=1, DS lite */
  2317. err = mvpp2_prs_ip6_proto(priv, IPPROTO_IPIP,
  2318. MVPP2_PRS_RI_UDF7_IP6_LITE,
  2319. MVPP2_PRS_RI_UDF7_MASK);
  2320. if (err)
  2321. return err;
  2322. /* IPv6 multicast */
  2323. err = mvpp2_prs_ip6_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
  2324. if (err)
  2325. return err;
  2326. /* Entry for checking hop limit */
  2327. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  2328. MVPP2_PE_LAST_FREE_TID);
  2329. if (tid < 0)
  2330. return tid;
  2331. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  2332. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
  2333. pe.index = tid;
  2334. /* Finished: go to flowid generation */
  2335. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  2336. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  2337. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN |
  2338. MVPP2_PRS_RI_DROP_MASK,
  2339. MVPP2_PRS_RI_L3_PROTO_MASK |
  2340. MVPP2_PRS_RI_DROP_MASK);
  2341. mvpp2_prs_tcam_data_byte_set(&pe, 1, 0x00, MVPP2_PRS_IPV6_HOP_MASK);
  2342. mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
  2343. MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
  2344. /* Update shadow table and hw entry */
  2345. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
  2346. mvpp2_prs_hw_write(priv, &pe);
  2347. /* Default IPv6 entry for unknown protocols */
  2348. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  2349. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
  2350. pe.index = MVPP2_PE_IP6_PROTO_UN;
  2351. /* Finished: go to flowid generation */
  2352. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  2353. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  2354. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
  2355. MVPP2_PRS_RI_L4_PROTO_MASK);
  2356. /* Set L4 offset relatively to our current place */
  2357. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
  2358. sizeof(struct ipv6hdr) - 4,
  2359. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  2360. mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
  2361. MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
  2362. /* Unmask all ports */
  2363. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  2364. /* Update shadow table and hw entry */
  2365. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
  2366. mvpp2_prs_hw_write(priv, &pe);
  2367. /* Default IPv6 entry for unknown ext protocols */
  2368. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  2369. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
  2370. pe.index = MVPP2_PE_IP6_EXT_PROTO_UN;
  2371. /* Finished: go to flowid generation */
  2372. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  2373. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  2374. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
  2375. MVPP2_PRS_RI_L4_PROTO_MASK);
  2376. mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_EXT_AI_BIT,
  2377. MVPP2_PRS_IPV6_EXT_AI_BIT);
  2378. /* Unmask all ports */
  2379. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  2380. /* Update shadow table and hw entry */
  2381. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
  2382. mvpp2_prs_hw_write(priv, &pe);
  2383. /* Default IPv6 entry for unicast address */
  2384. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  2385. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
  2386. pe.index = MVPP2_PE_IP6_ADDR_UN;
  2387. /* Finished: go to IPv6 again */
  2388. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
  2389. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
  2390. MVPP2_PRS_RI_L3_ADDR_MASK);
  2391. mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
  2392. MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
  2393. /* Shift back to IPV6 NH */
  2394. mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  2395. mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
  2396. /* Unmask all ports */
  2397. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  2398. /* Update shadow table and hw entry */
  2399. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
  2400. mvpp2_prs_hw_write(priv, &pe);
  2401. return 0;
  2402. }
  2403. /* Parser default initialization */
  2404. static int mvpp2_prs_default_init(struct platform_device *pdev,
  2405. struct mvpp2 *priv)
  2406. {
  2407. int err, index, i;
  2408. /* Enable tcam table */
  2409. mvpp2_write(priv, MVPP2_PRS_TCAM_CTRL_REG, MVPP2_PRS_TCAM_EN_MASK);
  2410. /* Clear all tcam and sram entries */
  2411. for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++) {
  2412. mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
  2413. for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
  2414. mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), 0);
  2415. mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, index);
  2416. for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
  2417. mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), 0);
  2418. }
  2419. /* Invalidate all tcam entries */
  2420. for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++)
  2421. mvpp2_prs_hw_inv(priv, index);
  2422. priv->prs_shadow = devm_kcalloc(&pdev->dev, MVPP2_PRS_TCAM_SRAM_SIZE,
  2423. sizeof(struct mvpp2_prs_shadow),
  2424. GFP_KERNEL);
  2425. if (!priv->prs_shadow)
  2426. return -ENOMEM;
  2427. /* Always start from lookup = 0 */
  2428. for (index = 0; index < MVPP2_MAX_PORTS; index++)
  2429. mvpp2_prs_hw_port_init(priv, index, MVPP2_PRS_LU_MH,
  2430. MVPP2_PRS_PORT_LU_MAX, 0);
  2431. mvpp2_prs_def_flow_init(priv);
  2432. mvpp2_prs_mh_init(priv);
  2433. mvpp2_prs_mac_init(priv);
  2434. mvpp2_prs_dsa_init(priv);
  2435. err = mvpp2_prs_etype_init(priv);
  2436. if (err)
  2437. return err;
  2438. err = mvpp2_prs_vlan_init(pdev, priv);
  2439. if (err)
  2440. return err;
  2441. err = mvpp2_prs_pppoe_init(priv);
  2442. if (err)
  2443. return err;
  2444. err = mvpp2_prs_ip6_init(priv);
  2445. if (err)
  2446. return err;
  2447. err = mvpp2_prs_ip4_init(priv);
  2448. if (err)
  2449. return err;
  2450. return 0;
  2451. }
  2452. /* Compare MAC DA with tcam entry data */
  2453. static bool mvpp2_prs_mac_range_equals(struct mvpp2_prs_entry *pe,
  2454. const u8 *da, unsigned char *mask)
  2455. {
  2456. unsigned char tcam_byte, tcam_mask;
  2457. int index;
  2458. for (index = 0; index < ETH_ALEN; index++) {
  2459. mvpp2_prs_tcam_data_byte_get(pe, index, &tcam_byte, &tcam_mask);
  2460. if (tcam_mask != mask[index])
  2461. return false;
  2462. if ((tcam_mask & tcam_byte) != (da[index] & mask[index]))
  2463. return false;
  2464. }
  2465. return true;
  2466. }
  2467. /* Find tcam entry with matched pair <MAC DA, port> */
  2468. static struct mvpp2_prs_entry *
  2469. mvpp2_prs_mac_da_range_find(struct mvpp2 *priv, int pmap, const u8 *da,
  2470. unsigned char *mask, int udf_type)
  2471. {
  2472. struct mvpp2_prs_entry *pe;
  2473. int tid;
  2474. pe = kzalloc(sizeof(*pe), GFP_KERNEL);
  2475. if (!pe)
  2476. return NULL;
  2477. mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
  2478. /* Go through the all entires with MVPP2_PRS_LU_MAC */
  2479. for (tid = MVPP2_PE_FIRST_FREE_TID;
  2480. tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
  2481. unsigned int entry_pmap;
  2482. if (!priv->prs_shadow[tid].valid ||
  2483. (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
  2484. (priv->prs_shadow[tid].udf != udf_type))
  2485. continue;
  2486. pe->index = tid;
  2487. mvpp2_prs_hw_read(priv, pe);
  2488. entry_pmap = mvpp2_prs_tcam_port_map_get(pe);
  2489. if (mvpp2_prs_mac_range_equals(pe, da, mask) &&
  2490. entry_pmap == pmap)
  2491. return pe;
  2492. }
  2493. kfree(pe);
  2494. return NULL;
  2495. }
  2496. /* Update parser's mac da entry */
  2497. static int mvpp2_prs_mac_da_accept(struct mvpp2 *priv, int port,
  2498. const u8 *da, bool add)
  2499. {
  2500. struct mvpp2_prs_entry *pe;
  2501. unsigned int pmap, len, ri;
  2502. unsigned char mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  2503. int tid;
  2504. /* Scan TCAM and see if entry with this <MAC DA, port> already exist */
  2505. pe = mvpp2_prs_mac_da_range_find(priv, (1 << port), da, mask,
  2506. MVPP2_PRS_UDF_MAC_DEF);
  2507. /* No such entry */
  2508. if (!pe) {
  2509. if (!add)
  2510. return 0;
  2511. /* Create new TCAM entry */
  2512. /* Find first range mac entry*/
  2513. for (tid = MVPP2_PE_FIRST_FREE_TID;
  2514. tid <= MVPP2_PE_LAST_FREE_TID; tid++)
  2515. if (priv->prs_shadow[tid].valid &&
  2516. (priv->prs_shadow[tid].lu == MVPP2_PRS_LU_MAC) &&
  2517. (priv->prs_shadow[tid].udf ==
  2518. MVPP2_PRS_UDF_MAC_RANGE))
  2519. break;
  2520. /* Go through the all entries from first to last */
  2521. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  2522. tid - 1);
  2523. if (tid < 0)
  2524. return tid;
  2525. pe = kzalloc(sizeof(*pe), GFP_KERNEL);
  2526. if (!pe)
  2527. return -ENOMEM;
  2528. mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
  2529. pe->index = tid;
  2530. /* Mask all ports */
  2531. mvpp2_prs_tcam_port_map_set(pe, 0);
  2532. }
  2533. /* Update port mask */
  2534. mvpp2_prs_tcam_port_set(pe, port, add);
  2535. /* Invalidate the entry if no ports are left enabled */
  2536. pmap = mvpp2_prs_tcam_port_map_get(pe);
  2537. if (pmap == 0) {
  2538. if (add) {
  2539. kfree(pe);
  2540. return -EINVAL;
  2541. }
  2542. mvpp2_prs_hw_inv(priv, pe->index);
  2543. priv->prs_shadow[pe->index].valid = false;
  2544. kfree(pe);
  2545. return 0;
  2546. }
  2547. /* Continue - set next lookup */
  2548. mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_DSA);
  2549. /* Set match on DA */
  2550. len = ETH_ALEN;
  2551. while (len--)
  2552. mvpp2_prs_tcam_data_byte_set(pe, len, da[len], 0xff);
  2553. /* Set result info bits */
  2554. if (is_broadcast_ether_addr(da))
  2555. ri = MVPP2_PRS_RI_L2_BCAST;
  2556. else if (is_multicast_ether_addr(da))
  2557. ri = MVPP2_PRS_RI_L2_MCAST;
  2558. else
  2559. ri = MVPP2_PRS_RI_L2_UCAST | MVPP2_PRS_RI_MAC_ME_MASK;
  2560. mvpp2_prs_sram_ri_update(pe, ri, MVPP2_PRS_RI_L2_CAST_MASK |
  2561. MVPP2_PRS_RI_MAC_ME_MASK);
  2562. mvpp2_prs_shadow_ri_set(priv, pe->index, ri, MVPP2_PRS_RI_L2_CAST_MASK |
  2563. MVPP2_PRS_RI_MAC_ME_MASK);
  2564. /* Shift to ethertype */
  2565. mvpp2_prs_sram_shift_set(pe, 2 * ETH_ALEN,
  2566. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  2567. /* Update shadow table and hw entry */
  2568. priv->prs_shadow[pe->index].udf = MVPP2_PRS_UDF_MAC_DEF;
  2569. mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_MAC);
  2570. mvpp2_prs_hw_write(priv, pe);
  2571. kfree(pe);
  2572. return 0;
  2573. }
  2574. static int mvpp2_prs_update_mac_da(struct net_device *dev, const u8 *da)
  2575. {
  2576. struct mvpp2_port *port = netdev_priv(dev);
  2577. int err;
  2578. /* Remove old parser entry */
  2579. err = mvpp2_prs_mac_da_accept(port->priv, port->id, dev->dev_addr,
  2580. false);
  2581. if (err)
  2582. return err;
  2583. /* Add new parser entry */
  2584. err = mvpp2_prs_mac_da_accept(port->priv, port->id, da, true);
  2585. if (err)
  2586. return err;
  2587. /* Set addr in the device */
  2588. ether_addr_copy(dev->dev_addr, da);
  2589. return 0;
  2590. }
  2591. /* Delete all port's multicast simple (not range) entries */
  2592. static void mvpp2_prs_mcast_del_all(struct mvpp2 *priv, int port)
  2593. {
  2594. struct mvpp2_prs_entry pe;
  2595. int index, tid;
  2596. for (tid = MVPP2_PE_FIRST_FREE_TID;
  2597. tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
  2598. unsigned char da[ETH_ALEN], da_mask[ETH_ALEN];
  2599. if (!priv->prs_shadow[tid].valid ||
  2600. (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
  2601. (priv->prs_shadow[tid].udf != MVPP2_PRS_UDF_MAC_DEF))
  2602. continue;
  2603. /* Only simple mac entries */
  2604. pe.index = tid;
  2605. mvpp2_prs_hw_read(priv, &pe);
  2606. /* Read mac addr from entry */
  2607. for (index = 0; index < ETH_ALEN; index++)
  2608. mvpp2_prs_tcam_data_byte_get(&pe, index, &da[index],
  2609. &da_mask[index]);
  2610. if (is_multicast_ether_addr(da) && !is_broadcast_ether_addr(da))
  2611. /* Delete this entry */
  2612. mvpp2_prs_mac_da_accept(priv, port, da, false);
  2613. }
  2614. }
  2615. static int mvpp2_prs_tag_mode_set(struct mvpp2 *priv, int port, int type)
  2616. {
  2617. switch (type) {
  2618. case MVPP2_TAG_TYPE_EDSA:
  2619. /* Add port to EDSA entries */
  2620. mvpp2_prs_dsa_tag_set(priv, port, true,
  2621. MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
  2622. mvpp2_prs_dsa_tag_set(priv, port, true,
  2623. MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
  2624. /* Remove port from DSA entries */
  2625. mvpp2_prs_dsa_tag_set(priv, port, false,
  2626. MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
  2627. mvpp2_prs_dsa_tag_set(priv, port, false,
  2628. MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
  2629. break;
  2630. case MVPP2_TAG_TYPE_DSA:
  2631. /* Add port to DSA entries */
  2632. mvpp2_prs_dsa_tag_set(priv, port, true,
  2633. MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
  2634. mvpp2_prs_dsa_tag_set(priv, port, true,
  2635. MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
  2636. /* Remove port from EDSA entries */
  2637. mvpp2_prs_dsa_tag_set(priv, port, false,
  2638. MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
  2639. mvpp2_prs_dsa_tag_set(priv, port, false,
  2640. MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
  2641. break;
  2642. case MVPP2_TAG_TYPE_MH:
  2643. case MVPP2_TAG_TYPE_NONE:
  2644. /* Remove port form EDSA and DSA entries */
  2645. mvpp2_prs_dsa_tag_set(priv, port, false,
  2646. MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
  2647. mvpp2_prs_dsa_tag_set(priv, port, false,
  2648. MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
  2649. mvpp2_prs_dsa_tag_set(priv, port, false,
  2650. MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
  2651. mvpp2_prs_dsa_tag_set(priv, port, false,
  2652. MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
  2653. break;
  2654. default:
  2655. if ((type < 0) || (type > MVPP2_TAG_TYPE_EDSA))
  2656. return -EINVAL;
  2657. }
  2658. return 0;
  2659. }
  2660. /* Set prs flow for the port */
  2661. static int mvpp2_prs_def_flow(struct mvpp2_port *port)
  2662. {
  2663. struct mvpp2_prs_entry *pe;
  2664. int tid;
  2665. pe = mvpp2_prs_flow_find(port->priv, port->id);
  2666. /* Such entry not exist */
  2667. if (!pe) {
  2668. /* Go through the all entires from last to first */
  2669. tid = mvpp2_prs_tcam_first_free(port->priv,
  2670. MVPP2_PE_LAST_FREE_TID,
  2671. MVPP2_PE_FIRST_FREE_TID);
  2672. if (tid < 0)
  2673. return tid;
  2674. pe = kzalloc(sizeof(*pe), GFP_KERNEL);
  2675. if (!pe)
  2676. return -ENOMEM;
  2677. mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
  2678. pe->index = tid;
  2679. /* Set flow ID*/
  2680. mvpp2_prs_sram_ai_update(pe, port->id, MVPP2_PRS_FLOW_ID_MASK);
  2681. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
  2682. /* Update shadow table */
  2683. mvpp2_prs_shadow_set(port->priv, pe->index, MVPP2_PRS_LU_FLOWS);
  2684. }
  2685. mvpp2_prs_tcam_port_map_set(pe, (1 << port->id));
  2686. mvpp2_prs_hw_write(port->priv, pe);
  2687. kfree(pe);
  2688. return 0;
  2689. }
  2690. /* Classifier configuration routines */
  2691. /* Update classification flow table registers */
  2692. static void mvpp2_cls_flow_write(struct mvpp2 *priv,
  2693. struct mvpp2_cls_flow_entry *fe)
  2694. {
  2695. mvpp2_write(priv, MVPP2_CLS_FLOW_INDEX_REG, fe->index);
  2696. mvpp2_write(priv, MVPP2_CLS_FLOW_TBL0_REG, fe->data[0]);
  2697. mvpp2_write(priv, MVPP2_CLS_FLOW_TBL1_REG, fe->data[1]);
  2698. mvpp2_write(priv, MVPP2_CLS_FLOW_TBL2_REG, fe->data[2]);
  2699. }
  2700. /* Update classification lookup table register */
  2701. static void mvpp2_cls_lookup_write(struct mvpp2 *priv,
  2702. struct mvpp2_cls_lookup_entry *le)
  2703. {
  2704. u32 val;
  2705. val = (le->way << MVPP2_CLS_LKP_INDEX_WAY_OFFS) | le->lkpid;
  2706. mvpp2_write(priv, MVPP2_CLS_LKP_INDEX_REG, val);
  2707. mvpp2_write(priv, MVPP2_CLS_LKP_TBL_REG, le->data);
  2708. }
  2709. /* Classifier default initialization */
  2710. static void mvpp2_cls_init(struct mvpp2 *priv)
  2711. {
  2712. struct mvpp2_cls_lookup_entry le;
  2713. struct mvpp2_cls_flow_entry fe;
  2714. int index;
  2715. /* Enable classifier */
  2716. mvpp2_write(priv, MVPP2_CLS_MODE_REG, MVPP2_CLS_MODE_ACTIVE_MASK);
  2717. /* Clear classifier flow table */
  2718. memset(&fe.data, 0, sizeof(fe.data));
  2719. for (index = 0; index < MVPP2_CLS_FLOWS_TBL_SIZE; index++) {
  2720. fe.index = index;
  2721. mvpp2_cls_flow_write(priv, &fe);
  2722. }
  2723. /* Clear classifier lookup table */
  2724. le.data = 0;
  2725. for (index = 0; index < MVPP2_CLS_LKP_TBL_SIZE; index++) {
  2726. le.lkpid = index;
  2727. le.way = 0;
  2728. mvpp2_cls_lookup_write(priv, &le);
  2729. le.way = 1;
  2730. mvpp2_cls_lookup_write(priv, &le);
  2731. }
  2732. }
  2733. static void mvpp2_cls_port_config(struct mvpp2_port *port)
  2734. {
  2735. struct mvpp2_cls_lookup_entry le;
  2736. u32 val;
  2737. /* Set way for the port */
  2738. val = mvpp2_read(port->priv, MVPP2_CLS_PORT_WAY_REG);
  2739. val &= ~MVPP2_CLS_PORT_WAY_MASK(port->id);
  2740. mvpp2_write(port->priv, MVPP2_CLS_PORT_WAY_REG, val);
  2741. /* Pick the entry to be accessed in lookup ID decoding table
  2742. * according to the way and lkpid.
  2743. */
  2744. le.lkpid = port->id;
  2745. le.way = 0;
  2746. le.data = 0;
  2747. /* Set initial CPU queue for receiving packets */
  2748. le.data &= ~MVPP2_CLS_LKP_TBL_RXQ_MASK;
  2749. le.data |= port->first_rxq;
  2750. /* Disable classification engines */
  2751. le.data &= ~MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK;
  2752. /* Update lookup ID table entry */
  2753. mvpp2_cls_lookup_write(port->priv, &le);
  2754. }
  2755. /* Set CPU queue number for oversize packets */
  2756. static void mvpp2_cls_oversize_rxq_set(struct mvpp2_port *port)
  2757. {
  2758. u32 val;
  2759. mvpp2_write(port->priv, MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port->id),
  2760. port->first_rxq & MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK);
  2761. mvpp2_write(port->priv, MVPP2_CLS_SWFWD_P2HQ_REG(port->id),
  2762. (port->first_rxq >> MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS));
  2763. val = mvpp2_read(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG);
  2764. val |= MVPP2_CLS_SWFWD_PCTRL_MASK(port->id);
  2765. mvpp2_write(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG, val);
  2766. }
  2767. static void *mvpp2_frag_alloc(const struct mvpp2_bm_pool *pool)
  2768. {
  2769. if (likely(pool->frag_size <= PAGE_SIZE))
  2770. return netdev_alloc_frag(pool->frag_size);
  2771. else
  2772. return kmalloc(pool->frag_size, GFP_ATOMIC);
  2773. }
  2774. static void mvpp2_frag_free(const struct mvpp2_bm_pool *pool, void *data)
  2775. {
  2776. if (likely(pool->frag_size <= PAGE_SIZE))
  2777. skb_free_frag(data);
  2778. else
  2779. kfree(data);
  2780. }
  2781. /* Buffer Manager configuration routines */
  2782. /* Create pool */
  2783. static int mvpp2_bm_pool_create(struct platform_device *pdev,
  2784. struct mvpp2 *priv,
  2785. struct mvpp2_bm_pool *bm_pool, int size)
  2786. {
  2787. int size_bytes;
  2788. u32 val;
  2789. size_bytes = sizeof(u32) * size;
  2790. bm_pool->virt_addr = dma_alloc_coherent(&pdev->dev, size_bytes,
  2791. &bm_pool->phys_addr,
  2792. GFP_KERNEL);
  2793. if (!bm_pool->virt_addr)
  2794. return -ENOMEM;
  2795. if (!IS_ALIGNED((unsigned long)bm_pool->virt_addr,
  2796. MVPP2_BM_POOL_PTR_ALIGN)) {
  2797. dma_free_coherent(&pdev->dev, size_bytes, bm_pool->virt_addr,
  2798. bm_pool->phys_addr);
  2799. dev_err(&pdev->dev, "BM pool %d is not %d bytes aligned\n",
  2800. bm_pool->id, MVPP2_BM_POOL_PTR_ALIGN);
  2801. return -ENOMEM;
  2802. }
  2803. mvpp2_write(priv, MVPP2_BM_POOL_BASE_REG(bm_pool->id),
  2804. bm_pool->phys_addr);
  2805. mvpp2_write(priv, MVPP2_BM_POOL_SIZE_REG(bm_pool->id), size);
  2806. val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
  2807. val |= MVPP2_BM_START_MASK;
  2808. mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
  2809. bm_pool->type = MVPP2_BM_FREE;
  2810. bm_pool->size = size;
  2811. bm_pool->pkt_size = 0;
  2812. bm_pool->buf_num = 0;
  2813. return 0;
  2814. }
  2815. /* Set pool buffer size */
  2816. static void mvpp2_bm_pool_bufsize_set(struct mvpp2 *priv,
  2817. struct mvpp2_bm_pool *bm_pool,
  2818. int buf_size)
  2819. {
  2820. u32 val;
  2821. bm_pool->buf_size = buf_size;
  2822. val = ALIGN(buf_size, 1 << MVPP2_POOL_BUF_SIZE_OFFSET);
  2823. mvpp2_write(priv, MVPP2_POOL_BUF_SIZE_REG(bm_pool->id), val);
  2824. }
  2825. /* Free all buffers from the pool */
  2826. static void mvpp2_bm_bufs_free(struct device *dev, struct mvpp2 *priv,
  2827. struct mvpp2_bm_pool *bm_pool)
  2828. {
  2829. int i;
  2830. for (i = 0; i < bm_pool->buf_num; i++) {
  2831. dma_addr_t buf_phys_addr;
  2832. unsigned long vaddr;
  2833. /* Get buffer virtual address (indirect access) */
  2834. buf_phys_addr = mvpp2_read(priv,
  2835. MVPP2_BM_PHY_ALLOC_REG(bm_pool->id));
  2836. vaddr = mvpp2_read(priv, MVPP2_BM_VIRT_ALLOC_REG);
  2837. dma_unmap_single(dev, buf_phys_addr,
  2838. bm_pool->buf_size, DMA_FROM_DEVICE);
  2839. if (!vaddr)
  2840. break;
  2841. mvpp2_frag_free(bm_pool, (void *)vaddr);
  2842. }
  2843. /* Update BM driver with number of buffers removed from pool */
  2844. bm_pool->buf_num -= i;
  2845. }
  2846. /* Cleanup pool */
  2847. static int mvpp2_bm_pool_destroy(struct platform_device *pdev,
  2848. struct mvpp2 *priv,
  2849. struct mvpp2_bm_pool *bm_pool)
  2850. {
  2851. u32 val;
  2852. mvpp2_bm_bufs_free(&pdev->dev, priv, bm_pool);
  2853. if (bm_pool->buf_num) {
  2854. WARN(1, "cannot free all buffers in pool %d\n", bm_pool->id);
  2855. return 0;
  2856. }
  2857. val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
  2858. val |= MVPP2_BM_STOP_MASK;
  2859. mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
  2860. dma_free_coherent(&pdev->dev, sizeof(u32) * bm_pool->size,
  2861. bm_pool->virt_addr,
  2862. bm_pool->phys_addr);
  2863. return 0;
  2864. }
  2865. static int mvpp2_bm_pools_init(struct platform_device *pdev,
  2866. struct mvpp2 *priv)
  2867. {
  2868. int i, err, size;
  2869. struct mvpp2_bm_pool *bm_pool;
  2870. /* Create all pools with maximum size */
  2871. size = MVPP2_BM_POOL_SIZE_MAX;
  2872. for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
  2873. bm_pool = &priv->bm_pools[i];
  2874. bm_pool->id = i;
  2875. err = mvpp2_bm_pool_create(pdev, priv, bm_pool, size);
  2876. if (err)
  2877. goto err_unroll_pools;
  2878. mvpp2_bm_pool_bufsize_set(priv, bm_pool, 0);
  2879. }
  2880. return 0;
  2881. err_unroll_pools:
  2882. dev_err(&pdev->dev, "failed to create BM pool %d, size %d\n", i, size);
  2883. for (i = i - 1; i >= 0; i--)
  2884. mvpp2_bm_pool_destroy(pdev, priv, &priv->bm_pools[i]);
  2885. return err;
  2886. }
  2887. static int mvpp2_bm_init(struct platform_device *pdev, struct mvpp2 *priv)
  2888. {
  2889. int i, err;
  2890. for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
  2891. /* Mask BM all interrupts */
  2892. mvpp2_write(priv, MVPP2_BM_INTR_MASK_REG(i), 0);
  2893. /* Clear BM cause register */
  2894. mvpp2_write(priv, MVPP2_BM_INTR_CAUSE_REG(i), 0);
  2895. }
  2896. /* Allocate and initialize BM pools */
  2897. priv->bm_pools = devm_kcalloc(&pdev->dev, MVPP2_BM_POOLS_NUM,
  2898. sizeof(struct mvpp2_bm_pool), GFP_KERNEL);
  2899. if (!priv->bm_pools)
  2900. return -ENOMEM;
  2901. err = mvpp2_bm_pools_init(pdev, priv);
  2902. if (err < 0)
  2903. return err;
  2904. return 0;
  2905. }
  2906. /* Attach long pool to rxq */
  2907. static void mvpp2_rxq_long_pool_set(struct mvpp2_port *port,
  2908. int lrxq, int long_pool)
  2909. {
  2910. u32 val;
  2911. int prxq;
  2912. /* Get queue physical ID */
  2913. prxq = port->rxqs[lrxq]->id;
  2914. val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
  2915. val &= ~MVPP2_RXQ_POOL_LONG_MASK;
  2916. val |= ((long_pool << MVPP2_RXQ_POOL_LONG_OFFS) &
  2917. MVPP2_RXQ_POOL_LONG_MASK);
  2918. mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
  2919. }
  2920. /* Attach short pool to rxq */
  2921. static void mvpp2_rxq_short_pool_set(struct mvpp2_port *port,
  2922. int lrxq, int short_pool)
  2923. {
  2924. u32 val;
  2925. int prxq;
  2926. /* Get queue physical ID */
  2927. prxq = port->rxqs[lrxq]->id;
  2928. val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
  2929. val &= ~MVPP2_RXQ_POOL_SHORT_MASK;
  2930. val |= ((short_pool << MVPP2_RXQ_POOL_SHORT_OFFS) &
  2931. MVPP2_RXQ_POOL_SHORT_MASK);
  2932. mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
  2933. }
  2934. static void *mvpp2_buf_alloc(struct mvpp2_port *port,
  2935. struct mvpp2_bm_pool *bm_pool,
  2936. dma_addr_t *buf_phys_addr,
  2937. gfp_t gfp_mask)
  2938. {
  2939. dma_addr_t phys_addr;
  2940. void *data;
  2941. data = mvpp2_frag_alloc(bm_pool);
  2942. if (!data)
  2943. return NULL;
  2944. phys_addr = dma_map_single(port->dev->dev.parent, data,
  2945. MVPP2_RX_BUF_SIZE(bm_pool->pkt_size),
  2946. DMA_FROM_DEVICE);
  2947. if (unlikely(dma_mapping_error(port->dev->dev.parent, phys_addr))) {
  2948. mvpp2_frag_free(bm_pool, data);
  2949. return NULL;
  2950. }
  2951. *buf_phys_addr = phys_addr;
  2952. return data;
  2953. }
  2954. /* Set pool number in a BM cookie */
  2955. static inline u32 mvpp2_bm_cookie_pool_set(u32 cookie, int pool)
  2956. {
  2957. u32 bm;
  2958. bm = cookie & ~(0xFF << MVPP2_BM_COOKIE_POOL_OFFS);
  2959. bm |= ((pool & 0xFF) << MVPP2_BM_COOKIE_POOL_OFFS);
  2960. return bm;
  2961. }
  2962. /* Get pool number from a BM cookie */
  2963. static inline int mvpp2_bm_cookie_pool_get(unsigned long cookie)
  2964. {
  2965. return (cookie >> MVPP2_BM_COOKIE_POOL_OFFS) & 0xFF;
  2966. }
  2967. /* Release buffer to BM */
  2968. static inline void mvpp2_bm_pool_put(struct mvpp2_port *port, int pool,
  2969. dma_addr_t buf_phys_addr,
  2970. unsigned long buf_virt_addr)
  2971. {
  2972. mvpp2_write(port->priv, MVPP2_BM_VIRT_RLS_REG, buf_virt_addr);
  2973. mvpp2_write(port->priv, MVPP2_BM_PHY_RLS_REG(pool), buf_phys_addr);
  2974. }
  2975. /* Release multicast buffer */
  2976. static void mvpp2_bm_pool_mc_put(struct mvpp2_port *port, int pool,
  2977. dma_addr_t buf_phys_addr,
  2978. unsigned long buf_virt_addr,
  2979. int mc_id)
  2980. {
  2981. u32 val = 0;
  2982. val |= (mc_id & MVPP2_BM_MC_ID_MASK);
  2983. mvpp2_write(port->priv, MVPP2_BM_MC_RLS_REG, val);
  2984. mvpp2_bm_pool_put(port, pool,
  2985. buf_phys_addr | MVPP2_BM_PHY_RLS_MC_BUFF_MASK,
  2986. buf_virt_addr);
  2987. }
  2988. /* Refill BM pool */
  2989. static void mvpp2_pool_refill(struct mvpp2_port *port, u32 bm,
  2990. dma_addr_t phys_addr,
  2991. unsigned long cookie)
  2992. {
  2993. int pool = mvpp2_bm_cookie_pool_get(bm);
  2994. mvpp2_bm_pool_put(port, pool, phys_addr, cookie);
  2995. }
  2996. /* Allocate buffers for the pool */
  2997. static int mvpp2_bm_bufs_add(struct mvpp2_port *port,
  2998. struct mvpp2_bm_pool *bm_pool, int buf_num)
  2999. {
  3000. int i, buf_size, total_size;
  3001. dma_addr_t phys_addr;
  3002. void *buf;
  3003. buf_size = MVPP2_RX_BUF_SIZE(bm_pool->pkt_size);
  3004. total_size = MVPP2_RX_TOTAL_SIZE(buf_size);
  3005. if (buf_num < 0 ||
  3006. (buf_num + bm_pool->buf_num > bm_pool->size)) {
  3007. netdev_err(port->dev,
  3008. "cannot allocate %d buffers for pool %d\n",
  3009. buf_num, bm_pool->id);
  3010. return 0;
  3011. }
  3012. for (i = 0; i < buf_num; i++) {
  3013. buf = mvpp2_buf_alloc(port, bm_pool, &phys_addr, GFP_KERNEL);
  3014. if (!buf)
  3015. break;
  3016. mvpp2_bm_pool_put(port, bm_pool->id, phys_addr,
  3017. (unsigned long)buf);
  3018. }
  3019. /* Update BM driver with number of buffers added to pool */
  3020. bm_pool->buf_num += i;
  3021. netdev_dbg(port->dev,
  3022. "%s pool %d: pkt_size=%4d, buf_size=%4d, total_size=%4d\n",
  3023. bm_pool->type == MVPP2_BM_SWF_SHORT ? "short" : " long",
  3024. bm_pool->id, bm_pool->pkt_size, buf_size, total_size);
  3025. netdev_dbg(port->dev,
  3026. "%s pool %d: %d of %d buffers added\n",
  3027. bm_pool->type == MVPP2_BM_SWF_SHORT ? "short" : " long",
  3028. bm_pool->id, i, buf_num);
  3029. return i;
  3030. }
  3031. /* Notify the driver that BM pool is being used as specific type and return the
  3032. * pool pointer on success
  3033. */
  3034. static struct mvpp2_bm_pool *
  3035. mvpp2_bm_pool_use(struct mvpp2_port *port, int pool, enum mvpp2_bm_type type,
  3036. int pkt_size)
  3037. {
  3038. struct mvpp2_bm_pool *new_pool = &port->priv->bm_pools[pool];
  3039. int num;
  3040. if (new_pool->type != MVPP2_BM_FREE && new_pool->type != type) {
  3041. netdev_err(port->dev, "mixing pool types is forbidden\n");
  3042. return NULL;
  3043. }
  3044. if (new_pool->type == MVPP2_BM_FREE)
  3045. new_pool->type = type;
  3046. /* Allocate buffers in case BM pool is used as long pool, but packet
  3047. * size doesn't match MTU or BM pool hasn't being used yet
  3048. */
  3049. if (((type == MVPP2_BM_SWF_LONG) && (pkt_size > new_pool->pkt_size)) ||
  3050. (new_pool->pkt_size == 0)) {
  3051. int pkts_num;
  3052. /* Set default buffer number or free all the buffers in case
  3053. * the pool is not empty
  3054. */
  3055. pkts_num = new_pool->buf_num;
  3056. if (pkts_num == 0)
  3057. pkts_num = type == MVPP2_BM_SWF_LONG ?
  3058. MVPP2_BM_LONG_BUF_NUM :
  3059. MVPP2_BM_SHORT_BUF_NUM;
  3060. else
  3061. mvpp2_bm_bufs_free(port->dev->dev.parent,
  3062. port->priv, new_pool);
  3063. new_pool->pkt_size = pkt_size;
  3064. new_pool->frag_size =
  3065. SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
  3066. MVPP2_SKB_SHINFO_SIZE;
  3067. /* Allocate buffers for this pool */
  3068. num = mvpp2_bm_bufs_add(port, new_pool, pkts_num);
  3069. if (num != pkts_num) {
  3070. WARN(1, "pool %d: %d of %d allocated\n",
  3071. new_pool->id, num, pkts_num);
  3072. return NULL;
  3073. }
  3074. }
  3075. mvpp2_bm_pool_bufsize_set(port->priv, new_pool,
  3076. MVPP2_RX_BUF_SIZE(new_pool->pkt_size));
  3077. return new_pool;
  3078. }
  3079. /* Initialize pools for swf */
  3080. static int mvpp2_swf_bm_pool_init(struct mvpp2_port *port)
  3081. {
  3082. int rxq;
  3083. if (!port->pool_long) {
  3084. port->pool_long =
  3085. mvpp2_bm_pool_use(port, MVPP2_BM_SWF_LONG_POOL(port->id),
  3086. MVPP2_BM_SWF_LONG,
  3087. port->pkt_size);
  3088. if (!port->pool_long)
  3089. return -ENOMEM;
  3090. port->pool_long->port_map |= (1 << port->id);
  3091. for (rxq = 0; rxq < rxq_number; rxq++)
  3092. mvpp2_rxq_long_pool_set(port, rxq, port->pool_long->id);
  3093. }
  3094. if (!port->pool_short) {
  3095. port->pool_short =
  3096. mvpp2_bm_pool_use(port, MVPP2_BM_SWF_SHORT_POOL,
  3097. MVPP2_BM_SWF_SHORT,
  3098. MVPP2_BM_SHORT_PKT_SIZE);
  3099. if (!port->pool_short)
  3100. return -ENOMEM;
  3101. port->pool_short->port_map |= (1 << port->id);
  3102. for (rxq = 0; rxq < rxq_number; rxq++)
  3103. mvpp2_rxq_short_pool_set(port, rxq,
  3104. port->pool_short->id);
  3105. }
  3106. return 0;
  3107. }
  3108. static int mvpp2_bm_update_mtu(struct net_device *dev, int mtu)
  3109. {
  3110. struct mvpp2_port *port = netdev_priv(dev);
  3111. struct mvpp2_bm_pool *port_pool = port->pool_long;
  3112. int num, pkts_num = port_pool->buf_num;
  3113. int pkt_size = MVPP2_RX_PKT_SIZE(mtu);
  3114. /* Update BM pool with new buffer size */
  3115. mvpp2_bm_bufs_free(dev->dev.parent, port->priv, port_pool);
  3116. if (port_pool->buf_num) {
  3117. WARN(1, "cannot free all buffers in pool %d\n", port_pool->id);
  3118. return -EIO;
  3119. }
  3120. port_pool->pkt_size = pkt_size;
  3121. port_pool->frag_size = SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
  3122. MVPP2_SKB_SHINFO_SIZE;
  3123. num = mvpp2_bm_bufs_add(port, port_pool, pkts_num);
  3124. if (num != pkts_num) {
  3125. WARN(1, "pool %d: %d of %d allocated\n",
  3126. port_pool->id, num, pkts_num);
  3127. return -EIO;
  3128. }
  3129. mvpp2_bm_pool_bufsize_set(port->priv, port_pool,
  3130. MVPP2_RX_BUF_SIZE(port_pool->pkt_size));
  3131. dev->mtu = mtu;
  3132. netdev_update_features(dev);
  3133. return 0;
  3134. }
  3135. static inline void mvpp2_interrupts_enable(struct mvpp2_port *port)
  3136. {
  3137. int cpu, cpu_mask = 0;
  3138. for_each_present_cpu(cpu)
  3139. cpu_mask |= 1 << cpu;
  3140. mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
  3141. MVPP2_ISR_ENABLE_INTERRUPT(cpu_mask));
  3142. }
  3143. static inline void mvpp2_interrupts_disable(struct mvpp2_port *port)
  3144. {
  3145. int cpu, cpu_mask = 0;
  3146. for_each_present_cpu(cpu)
  3147. cpu_mask |= 1 << cpu;
  3148. mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
  3149. MVPP2_ISR_DISABLE_INTERRUPT(cpu_mask));
  3150. }
  3151. /* Mask the current CPU's Rx/Tx interrupts */
  3152. static void mvpp2_interrupts_mask(void *arg)
  3153. {
  3154. struct mvpp2_port *port = arg;
  3155. mvpp2_write(port->priv, MVPP2_ISR_RX_TX_MASK_REG(port->id), 0);
  3156. }
  3157. /* Unmask the current CPU's Rx/Tx interrupts */
  3158. static void mvpp2_interrupts_unmask(void *arg)
  3159. {
  3160. struct mvpp2_port *port = arg;
  3161. mvpp2_write(port->priv, MVPP2_ISR_RX_TX_MASK_REG(port->id),
  3162. (MVPP2_CAUSE_MISC_SUM_MASK |
  3163. MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK));
  3164. }
  3165. /* Port configuration routines */
  3166. static void mvpp2_port_mii_set(struct mvpp2_port *port)
  3167. {
  3168. u32 val;
  3169. val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
  3170. switch (port->phy_interface) {
  3171. case PHY_INTERFACE_MODE_SGMII:
  3172. val |= MVPP2_GMAC_INBAND_AN_MASK;
  3173. break;
  3174. case PHY_INTERFACE_MODE_RGMII:
  3175. val |= MVPP2_GMAC_PORT_RGMII_MASK;
  3176. default:
  3177. val &= ~MVPP2_GMAC_PCS_ENABLE_MASK;
  3178. }
  3179. writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
  3180. }
  3181. static void mvpp2_port_fc_adv_enable(struct mvpp2_port *port)
  3182. {
  3183. u32 val;
  3184. val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  3185. val |= MVPP2_GMAC_FC_ADV_EN;
  3186. writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  3187. }
  3188. static void mvpp2_port_enable(struct mvpp2_port *port)
  3189. {
  3190. u32 val;
  3191. val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
  3192. val |= MVPP2_GMAC_PORT_EN_MASK;
  3193. val |= MVPP2_GMAC_MIB_CNTR_EN_MASK;
  3194. writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
  3195. }
  3196. static void mvpp2_port_disable(struct mvpp2_port *port)
  3197. {
  3198. u32 val;
  3199. val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
  3200. val &= ~(MVPP2_GMAC_PORT_EN_MASK);
  3201. writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
  3202. }
  3203. /* Set IEEE 802.3x Flow Control Xon Packet Transmission Mode */
  3204. static void mvpp2_port_periodic_xon_disable(struct mvpp2_port *port)
  3205. {
  3206. u32 val;
  3207. val = readl(port->base + MVPP2_GMAC_CTRL_1_REG) &
  3208. ~MVPP2_GMAC_PERIODIC_XON_EN_MASK;
  3209. writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
  3210. }
  3211. /* Configure loopback port */
  3212. static void mvpp2_port_loopback_set(struct mvpp2_port *port)
  3213. {
  3214. u32 val;
  3215. val = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
  3216. if (port->speed == 1000)
  3217. val |= MVPP2_GMAC_GMII_LB_EN_MASK;
  3218. else
  3219. val &= ~MVPP2_GMAC_GMII_LB_EN_MASK;
  3220. if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
  3221. val |= MVPP2_GMAC_PCS_LB_EN_MASK;
  3222. else
  3223. val &= ~MVPP2_GMAC_PCS_LB_EN_MASK;
  3224. writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
  3225. }
  3226. static void mvpp2_port_reset(struct mvpp2_port *port)
  3227. {
  3228. u32 val;
  3229. val = readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
  3230. ~MVPP2_GMAC_PORT_RESET_MASK;
  3231. writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
  3232. while (readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
  3233. MVPP2_GMAC_PORT_RESET_MASK)
  3234. continue;
  3235. }
  3236. /* Change maximum receive size of the port */
  3237. static inline void mvpp2_gmac_max_rx_size_set(struct mvpp2_port *port)
  3238. {
  3239. u32 val;
  3240. val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
  3241. val &= ~MVPP2_GMAC_MAX_RX_SIZE_MASK;
  3242. val |= (((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
  3243. MVPP2_GMAC_MAX_RX_SIZE_OFFS);
  3244. writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
  3245. }
  3246. /* Set defaults to the MVPP2 port */
  3247. static void mvpp2_defaults_set(struct mvpp2_port *port)
  3248. {
  3249. int tx_port_num, val, queue, ptxq, lrxq;
  3250. /* Configure port to loopback if needed */
  3251. if (port->flags & MVPP2_F_LOOPBACK)
  3252. mvpp2_port_loopback_set(port);
  3253. /* Update TX FIFO MIN Threshold */
  3254. val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
  3255. val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
  3256. /* Min. TX threshold must be less than minimal packet length */
  3257. val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(64 - 4 - 2);
  3258. writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
  3259. /* Disable Legacy WRR, Disable EJP, Release from reset */
  3260. tx_port_num = mvpp2_egress_port(port);
  3261. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG,
  3262. tx_port_num);
  3263. mvpp2_write(port->priv, MVPP2_TXP_SCHED_CMD_1_REG, 0);
  3264. /* Close bandwidth for all queues */
  3265. for (queue = 0; queue < MVPP2_MAX_TXQ; queue++) {
  3266. ptxq = mvpp2_txq_phys(port->id, queue);
  3267. mvpp2_write(port->priv,
  3268. MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(ptxq), 0);
  3269. }
  3270. /* Set refill period to 1 usec, refill tokens
  3271. * and bucket size to maximum
  3272. */
  3273. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PERIOD_REG,
  3274. port->priv->tclk / USEC_PER_SEC);
  3275. val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_REFILL_REG);
  3276. val &= ~MVPP2_TXP_REFILL_PERIOD_ALL_MASK;
  3277. val |= MVPP2_TXP_REFILL_PERIOD_MASK(1);
  3278. val |= MVPP2_TXP_REFILL_TOKENS_ALL_MASK;
  3279. mvpp2_write(port->priv, MVPP2_TXP_SCHED_REFILL_REG, val);
  3280. val = MVPP2_TXP_TOKEN_SIZE_MAX;
  3281. mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
  3282. /* Set MaximumLowLatencyPacketSize value to 256 */
  3283. mvpp2_write(port->priv, MVPP2_RX_CTRL_REG(port->id),
  3284. MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK |
  3285. MVPP2_RX_LOW_LATENCY_PKT_SIZE(256));
  3286. /* Enable Rx cache snoop */
  3287. for (lrxq = 0; lrxq < rxq_number; lrxq++) {
  3288. queue = port->rxqs[lrxq]->id;
  3289. val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
  3290. val |= MVPP2_SNOOP_PKT_SIZE_MASK |
  3291. MVPP2_SNOOP_BUF_HDR_MASK;
  3292. mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
  3293. }
  3294. /* At default, mask all interrupts to all present cpus */
  3295. mvpp2_interrupts_disable(port);
  3296. }
  3297. /* Enable/disable receiving packets */
  3298. static void mvpp2_ingress_enable(struct mvpp2_port *port)
  3299. {
  3300. u32 val;
  3301. int lrxq, queue;
  3302. for (lrxq = 0; lrxq < rxq_number; lrxq++) {
  3303. queue = port->rxqs[lrxq]->id;
  3304. val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
  3305. val &= ~MVPP2_RXQ_DISABLE_MASK;
  3306. mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
  3307. }
  3308. }
  3309. static void mvpp2_ingress_disable(struct mvpp2_port *port)
  3310. {
  3311. u32 val;
  3312. int lrxq, queue;
  3313. for (lrxq = 0; lrxq < rxq_number; lrxq++) {
  3314. queue = port->rxqs[lrxq]->id;
  3315. val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
  3316. val |= MVPP2_RXQ_DISABLE_MASK;
  3317. mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
  3318. }
  3319. }
  3320. /* Enable transmit via physical egress queue
  3321. * - HW starts take descriptors from DRAM
  3322. */
  3323. static void mvpp2_egress_enable(struct mvpp2_port *port)
  3324. {
  3325. u32 qmap;
  3326. int queue;
  3327. int tx_port_num = mvpp2_egress_port(port);
  3328. /* Enable all initialized TXs. */
  3329. qmap = 0;
  3330. for (queue = 0; queue < txq_number; queue++) {
  3331. struct mvpp2_tx_queue *txq = port->txqs[queue];
  3332. if (txq->descs != NULL)
  3333. qmap |= (1 << queue);
  3334. }
  3335. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
  3336. mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG, qmap);
  3337. }
  3338. /* Disable transmit via physical egress queue
  3339. * - HW doesn't take descriptors from DRAM
  3340. */
  3341. static void mvpp2_egress_disable(struct mvpp2_port *port)
  3342. {
  3343. u32 reg_data;
  3344. int delay;
  3345. int tx_port_num = mvpp2_egress_port(port);
  3346. /* Issue stop command for active channels only */
  3347. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
  3348. reg_data = (mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG)) &
  3349. MVPP2_TXP_SCHED_ENQ_MASK;
  3350. if (reg_data != 0)
  3351. mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG,
  3352. (reg_data << MVPP2_TXP_SCHED_DISQ_OFFSET));
  3353. /* Wait for all Tx activity to terminate. */
  3354. delay = 0;
  3355. do {
  3356. if (delay >= MVPP2_TX_DISABLE_TIMEOUT_MSEC) {
  3357. netdev_warn(port->dev,
  3358. "Tx stop timed out, status=0x%08x\n",
  3359. reg_data);
  3360. break;
  3361. }
  3362. mdelay(1);
  3363. delay++;
  3364. /* Check port TX Command register that all
  3365. * Tx queues are stopped
  3366. */
  3367. reg_data = mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG);
  3368. } while (reg_data & MVPP2_TXP_SCHED_ENQ_MASK);
  3369. }
  3370. /* Rx descriptors helper methods */
  3371. /* Get number of Rx descriptors occupied by received packets */
  3372. static inline int
  3373. mvpp2_rxq_received(struct mvpp2_port *port, int rxq_id)
  3374. {
  3375. u32 val = mvpp2_read(port->priv, MVPP2_RXQ_STATUS_REG(rxq_id));
  3376. return val & MVPP2_RXQ_OCCUPIED_MASK;
  3377. }
  3378. /* Update Rx queue status with the number of occupied and available
  3379. * Rx descriptor slots.
  3380. */
  3381. static inline void
  3382. mvpp2_rxq_status_update(struct mvpp2_port *port, int rxq_id,
  3383. int used_count, int free_count)
  3384. {
  3385. /* Decrement the number of used descriptors and increment count
  3386. * increment the number of free descriptors.
  3387. */
  3388. u32 val = used_count | (free_count << MVPP2_RXQ_NUM_NEW_OFFSET);
  3389. mvpp2_write(port->priv, MVPP2_RXQ_STATUS_UPDATE_REG(rxq_id), val);
  3390. }
  3391. /* Get pointer to next RX descriptor to be processed by SW */
  3392. static inline struct mvpp2_rx_desc *
  3393. mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue *rxq)
  3394. {
  3395. int rx_desc = rxq->next_desc_to_proc;
  3396. rxq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(rxq, rx_desc);
  3397. prefetch(rxq->descs + rxq->next_desc_to_proc);
  3398. return rxq->descs + rx_desc;
  3399. }
  3400. /* Set rx queue offset */
  3401. static void mvpp2_rxq_offset_set(struct mvpp2_port *port,
  3402. int prxq, int offset)
  3403. {
  3404. u32 val;
  3405. /* Convert offset from bytes to units of 32 bytes */
  3406. offset = offset >> 5;
  3407. val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
  3408. val &= ~MVPP2_RXQ_PACKET_OFFSET_MASK;
  3409. /* Offset is in */
  3410. val |= ((offset << MVPP2_RXQ_PACKET_OFFSET_OFFS) &
  3411. MVPP2_RXQ_PACKET_OFFSET_MASK);
  3412. mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
  3413. }
  3414. /* Obtain BM cookie information from descriptor */
  3415. static u32 mvpp2_bm_cookie_build(struct mvpp2_rx_desc *rx_desc)
  3416. {
  3417. int pool = (rx_desc->status & MVPP2_RXD_BM_POOL_ID_MASK) >>
  3418. MVPP2_RXD_BM_POOL_ID_OFFS;
  3419. int cpu = smp_processor_id();
  3420. return ((pool & 0xFF) << MVPP2_BM_COOKIE_POOL_OFFS) |
  3421. ((cpu & 0xFF) << MVPP2_BM_COOKIE_CPU_OFFS);
  3422. }
  3423. /* Tx descriptors helper methods */
  3424. /* Get number of Tx descriptors waiting to be transmitted by HW */
  3425. static int mvpp2_txq_pend_desc_num_get(struct mvpp2_port *port,
  3426. struct mvpp2_tx_queue *txq)
  3427. {
  3428. u32 val;
  3429. mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
  3430. val = mvpp2_read(port->priv, MVPP2_TXQ_PENDING_REG);
  3431. return val & MVPP2_TXQ_PENDING_MASK;
  3432. }
  3433. /* Get pointer to next Tx descriptor to be processed (send) by HW */
  3434. static struct mvpp2_tx_desc *
  3435. mvpp2_txq_next_desc_get(struct mvpp2_tx_queue *txq)
  3436. {
  3437. int tx_desc = txq->next_desc_to_proc;
  3438. txq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(txq, tx_desc);
  3439. return txq->descs + tx_desc;
  3440. }
  3441. /* Update HW with number of aggregated Tx descriptors to be sent */
  3442. static void mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port *port, int pending)
  3443. {
  3444. /* aggregated access - relevant TXQ number is written in TX desc */
  3445. mvpp2_write(port->priv, MVPP2_AGGR_TXQ_UPDATE_REG, pending);
  3446. }
  3447. /* Check if there are enough free descriptors in aggregated txq.
  3448. * If not, update the number of occupied descriptors and repeat the check.
  3449. */
  3450. static int mvpp2_aggr_desc_num_check(struct mvpp2 *priv,
  3451. struct mvpp2_tx_queue *aggr_txq, int num)
  3452. {
  3453. if ((aggr_txq->count + num) > aggr_txq->size) {
  3454. /* Update number of occupied aggregated Tx descriptors */
  3455. int cpu = smp_processor_id();
  3456. u32 val = mvpp2_read(priv, MVPP2_AGGR_TXQ_STATUS_REG(cpu));
  3457. aggr_txq->count = val & MVPP2_AGGR_TXQ_PENDING_MASK;
  3458. }
  3459. if ((aggr_txq->count + num) > aggr_txq->size)
  3460. return -ENOMEM;
  3461. return 0;
  3462. }
  3463. /* Reserved Tx descriptors allocation request */
  3464. static int mvpp2_txq_alloc_reserved_desc(struct mvpp2 *priv,
  3465. struct mvpp2_tx_queue *txq, int num)
  3466. {
  3467. u32 val;
  3468. val = (txq->id << MVPP2_TXQ_RSVD_REQ_Q_OFFSET) | num;
  3469. mvpp2_write(priv, MVPP2_TXQ_RSVD_REQ_REG, val);
  3470. val = mvpp2_read(priv, MVPP2_TXQ_RSVD_RSLT_REG);
  3471. return val & MVPP2_TXQ_RSVD_RSLT_MASK;
  3472. }
  3473. /* Check if there are enough reserved descriptors for transmission.
  3474. * If not, request chunk of reserved descriptors and check again.
  3475. */
  3476. static int mvpp2_txq_reserved_desc_num_proc(struct mvpp2 *priv,
  3477. struct mvpp2_tx_queue *txq,
  3478. struct mvpp2_txq_pcpu *txq_pcpu,
  3479. int num)
  3480. {
  3481. int req, cpu, desc_count;
  3482. if (txq_pcpu->reserved_num >= num)
  3483. return 0;
  3484. /* Not enough descriptors reserved! Update the reserved descriptor
  3485. * count and check again.
  3486. */
  3487. desc_count = 0;
  3488. /* Compute total of used descriptors */
  3489. for_each_present_cpu(cpu) {
  3490. struct mvpp2_txq_pcpu *txq_pcpu_aux;
  3491. txq_pcpu_aux = per_cpu_ptr(txq->pcpu, cpu);
  3492. desc_count += txq_pcpu_aux->count;
  3493. desc_count += txq_pcpu_aux->reserved_num;
  3494. }
  3495. req = max(MVPP2_CPU_DESC_CHUNK, num - txq_pcpu->reserved_num);
  3496. desc_count += req;
  3497. if (desc_count >
  3498. (txq->size - (num_present_cpus() * MVPP2_CPU_DESC_CHUNK)))
  3499. return -ENOMEM;
  3500. txq_pcpu->reserved_num += mvpp2_txq_alloc_reserved_desc(priv, txq, req);
  3501. /* OK, the descriptor cound has been updated: check again. */
  3502. if (txq_pcpu->reserved_num < num)
  3503. return -ENOMEM;
  3504. return 0;
  3505. }
  3506. /* Release the last allocated Tx descriptor. Useful to handle DMA
  3507. * mapping failures in the Tx path.
  3508. */
  3509. static void mvpp2_txq_desc_put(struct mvpp2_tx_queue *txq)
  3510. {
  3511. if (txq->next_desc_to_proc == 0)
  3512. txq->next_desc_to_proc = txq->last_desc - 1;
  3513. else
  3514. txq->next_desc_to_proc--;
  3515. }
  3516. /* Set Tx descriptors fields relevant for CSUM calculation */
  3517. static u32 mvpp2_txq_desc_csum(int l3_offs, int l3_proto,
  3518. int ip_hdr_len, int l4_proto)
  3519. {
  3520. u32 command;
  3521. /* fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
  3522. * G_L4_chk, L4_type required only for checksum calculation
  3523. */
  3524. command = (l3_offs << MVPP2_TXD_L3_OFF_SHIFT);
  3525. command |= (ip_hdr_len << MVPP2_TXD_IP_HLEN_SHIFT);
  3526. command |= MVPP2_TXD_IP_CSUM_DISABLE;
  3527. if (l3_proto == swab16(ETH_P_IP)) {
  3528. command &= ~MVPP2_TXD_IP_CSUM_DISABLE; /* enable IPv4 csum */
  3529. command &= ~MVPP2_TXD_L3_IP6; /* enable IPv4 */
  3530. } else {
  3531. command |= MVPP2_TXD_L3_IP6; /* enable IPv6 */
  3532. }
  3533. if (l4_proto == IPPROTO_TCP) {
  3534. command &= ~MVPP2_TXD_L4_UDP; /* enable TCP */
  3535. command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
  3536. } else if (l4_proto == IPPROTO_UDP) {
  3537. command |= MVPP2_TXD_L4_UDP; /* enable UDP */
  3538. command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
  3539. } else {
  3540. command |= MVPP2_TXD_L4_CSUM_NOT;
  3541. }
  3542. return command;
  3543. }
  3544. /* Get number of sent descriptors and decrement counter.
  3545. * The number of sent descriptors is returned.
  3546. * Per-CPU access
  3547. */
  3548. static inline int mvpp2_txq_sent_desc_proc(struct mvpp2_port *port,
  3549. struct mvpp2_tx_queue *txq)
  3550. {
  3551. u32 val;
  3552. /* Reading status reg resets transmitted descriptor counter */
  3553. val = mvpp2_read(port->priv, MVPP2_TXQ_SENT_REG(txq->id));
  3554. return (val & MVPP2_TRANSMITTED_COUNT_MASK) >>
  3555. MVPP2_TRANSMITTED_COUNT_OFFSET;
  3556. }
  3557. static void mvpp2_txq_sent_counter_clear(void *arg)
  3558. {
  3559. struct mvpp2_port *port = arg;
  3560. int queue;
  3561. for (queue = 0; queue < txq_number; queue++) {
  3562. int id = port->txqs[queue]->id;
  3563. mvpp2_read(port->priv, MVPP2_TXQ_SENT_REG(id));
  3564. }
  3565. }
  3566. /* Set max sizes for Tx queues */
  3567. static void mvpp2_txp_max_tx_size_set(struct mvpp2_port *port)
  3568. {
  3569. u32 val, size, mtu;
  3570. int txq, tx_port_num;
  3571. mtu = port->pkt_size * 8;
  3572. if (mtu > MVPP2_TXP_MTU_MAX)
  3573. mtu = MVPP2_TXP_MTU_MAX;
  3574. /* WA for wrong Token bucket update: Set MTU value = 3*real MTU value */
  3575. mtu = 3 * mtu;
  3576. /* Indirect access to registers */
  3577. tx_port_num = mvpp2_egress_port(port);
  3578. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
  3579. /* Set MTU */
  3580. val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_MTU_REG);
  3581. val &= ~MVPP2_TXP_MTU_MAX;
  3582. val |= mtu;
  3583. mvpp2_write(port->priv, MVPP2_TXP_SCHED_MTU_REG, val);
  3584. /* TXP token size and all TXQs token size must be larger that MTU */
  3585. val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG);
  3586. size = val & MVPP2_TXP_TOKEN_SIZE_MAX;
  3587. if (size < mtu) {
  3588. size = mtu;
  3589. val &= ~MVPP2_TXP_TOKEN_SIZE_MAX;
  3590. val |= size;
  3591. mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
  3592. }
  3593. for (txq = 0; txq < txq_number; txq++) {
  3594. val = mvpp2_read(port->priv,
  3595. MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq));
  3596. size = val & MVPP2_TXQ_TOKEN_SIZE_MAX;
  3597. if (size < mtu) {
  3598. size = mtu;
  3599. val &= ~MVPP2_TXQ_TOKEN_SIZE_MAX;
  3600. val |= size;
  3601. mvpp2_write(port->priv,
  3602. MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq),
  3603. val);
  3604. }
  3605. }
  3606. }
  3607. /* Set the number of packets that will be received before Rx interrupt
  3608. * will be generated by HW.
  3609. */
  3610. static void mvpp2_rx_pkts_coal_set(struct mvpp2_port *port,
  3611. struct mvpp2_rx_queue *rxq)
  3612. {
  3613. if (rxq->pkts_coal > MVPP2_OCCUPIED_THRESH_MASK)
  3614. rxq->pkts_coal = MVPP2_OCCUPIED_THRESH_MASK;
  3615. mvpp2_write(port->priv, MVPP2_RXQ_NUM_REG, rxq->id);
  3616. mvpp2_write(port->priv, MVPP2_RXQ_THRESH_REG,
  3617. rxq->pkts_coal);
  3618. }
  3619. static u32 mvpp2_usec_to_cycles(u32 usec, unsigned long clk_hz)
  3620. {
  3621. u64 tmp = (u64)clk_hz * usec;
  3622. do_div(tmp, USEC_PER_SEC);
  3623. return tmp > U32_MAX ? U32_MAX : tmp;
  3624. }
  3625. static u32 mvpp2_cycles_to_usec(u32 cycles, unsigned long clk_hz)
  3626. {
  3627. u64 tmp = (u64)cycles * USEC_PER_SEC;
  3628. do_div(tmp, clk_hz);
  3629. return tmp > U32_MAX ? U32_MAX : tmp;
  3630. }
  3631. /* Set the time delay in usec before Rx interrupt */
  3632. static void mvpp2_rx_time_coal_set(struct mvpp2_port *port,
  3633. struct mvpp2_rx_queue *rxq)
  3634. {
  3635. unsigned long freq = port->priv->tclk;
  3636. u32 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
  3637. if (val > MVPP2_MAX_ISR_RX_THRESHOLD) {
  3638. rxq->time_coal =
  3639. mvpp2_cycles_to_usec(MVPP2_MAX_ISR_RX_THRESHOLD, freq);
  3640. /* re-evaluate to get actual register value */
  3641. val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
  3642. }
  3643. mvpp2_write(port->priv, MVPP2_ISR_RX_THRESHOLD_REG(rxq->id), val);
  3644. }
  3645. /* Free Tx queue skbuffs */
  3646. static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
  3647. struct mvpp2_tx_queue *txq,
  3648. struct mvpp2_txq_pcpu *txq_pcpu, int num)
  3649. {
  3650. int i;
  3651. for (i = 0; i < num; i++) {
  3652. struct mvpp2_txq_pcpu_buf *tx_buf =
  3653. txq_pcpu->buffs + txq_pcpu->txq_get_index;
  3654. dma_unmap_single(port->dev->dev.parent, tx_buf->phys,
  3655. tx_buf->size, DMA_TO_DEVICE);
  3656. if (tx_buf->skb)
  3657. dev_kfree_skb_any(tx_buf->skb);
  3658. mvpp2_txq_inc_get(txq_pcpu);
  3659. }
  3660. }
  3661. static inline struct mvpp2_rx_queue *mvpp2_get_rx_queue(struct mvpp2_port *port,
  3662. u32 cause)
  3663. {
  3664. int queue = fls(cause) - 1;
  3665. return port->rxqs[queue];
  3666. }
  3667. static inline struct mvpp2_tx_queue *mvpp2_get_tx_queue(struct mvpp2_port *port,
  3668. u32 cause)
  3669. {
  3670. int queue = fls(cause) - 1;
  3671. return port->txqs[queue];
  3672. }
  3673. /* Handle end of transmission */
  3674. static void mvpp2_txq_done(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
  3675. struct mvpp2_txq_pcpu *txq_pcpu)
  3676. {
  3677. struct netdev_queue *nq = netdev_get_tx_queue(port->dev, txq->log_id);
  3678. int tx_done;
  3679. if (txq_pcpu->cpu != smp_processor_id())
  3680. netdev_err(port->dev, "wrong cpu on the end of Tx processing\n");
  3681. tx_done = mvpp2_txq_sent_desc_proc(port, txq);
  3682. if (!tx_done)
  3683. return;
  3684. mvpp2_txq_bufs_free(port, txq, txq_pcpu, tx_done);
  3685. txq_pcpu->count -= tx_done;
  3686. if (netif_tx_queue_stopped(nq))
  3687. if (txq_pcpu->size - txq_pcpu->count >= MAX_SKB_FRAGS + 1)
  3688. netif_tx_wake_queue(nq);
  3689. }
  3690. static unsigned int mvpp2_tx_done(struct mvpp2_port *port, u32 cause)
  3691. {
  3692. struct mvpp2_tx_queue *txq;
  3693. struct mvpp2_txq_pcpu *txq_pcpu;
  3694. unsigned int tx_todo = 0;
  3695. while (cause) {
  3696. txq = mvpp2_get_tx_queue(port, cause);
  3697. if (!txq)
  3698. break;
  3699. txq_pcpu = this_cpu_ptr(txq->pcpu);
  3700. if (txq_pcpu->count) {
  3701. mvpp2_txq_done(port, txq, txq_pcpu);
  3702. tx_todo += txq_pcpu->count;
  3703. }
  3704. cause &= ~(1 << txq->log_id);
  3705. }
  3706. return tx_todo;
  3707. }
  3708. /* Rx/Tx queue initialization/cleanup methods */
  3709. /* Allocate and initialize descriptors for aggr TXQ */
  3710. static int mvpp2_aggr_txq_init(struct platform_device *pdev,
  3711. struct mvpp2_tx_queue *aggr_txq,
  3712. int desc_num, int cpu,
  3713. struct mvpp2 *priv)
  3714. {
  3715. /* Allocate memory for TX descriptors */
  3716. aggr_txq->descs = dma_alloc_coherent(&pdev->dev,
  3717. desc_num * MVPP2_DESC_ALIGNED_SIZE,
  3718. &aggr_txq->descs_phys, GFP_KERNEL);
  3719. if (!aggr_txq->descs)
  3720. return -ENOMEM;
  3721. aggr_txq->last_desc = aggr_txq->size - 1;
  3722. /* Aggr TXQ no reset WA */
  3723. aggr_txq->next_desc_to_proc = mvpp2_read(priv,
  3724. MVPP2_AGGR_TXQ_INDEX_REG(cpu));
  3725. /* Set Tx descriptors queue starting address */
  3726. /* indirect access */
  3727. mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu),
  3728. aggr_txq->descs_phys);
  3729. mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu), desc_num);
  3730. return 0;
  3731. }
  3732. /* Create a specified Rx queue */
  3733. static int mvpp2_rxq_init(struct mvpp2_port *port,
  3734. struct mvpp2_rx_queue *rxq)
  3735. {
  3736. rxq->size = port->rx_ring_size;
  3737. /* Allocate memory for RX descriptors */
  3738. rxq->descs = dma_alloc_coherent(port->dev->dev.parent,
  3739. rxq->size * MVPP2_DESC_ALIGNED_SIZE,
  3740. &rxq->descs_phys, GFP_KERNEL);
  3741. if (!rxq->descs)
  3742. return -ENOMEM;
  3743. rxq->last_desc = rxq->size - 1;
  3744. /* Zero occupied and non-occupied counters - direct access */
  3745. mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
  3746. /* Set Rx descriptors queue starting address - indirect access */
  3747. mvpp2_write(port->priv, MVPP2_RXQ_NUM_REG, rxq->id);
  3748. mvpp2_write(port->priv, MVPP2_RXQ_DESC_ADDR_REG, rxq->descs_phys);
  3749. mvpp2_write(port->priv, MVPP2_RXQ_DESC_SIZE_REG, rxq->size);
  3750. mvpp2_write(port->priv, MVPP2_RXQ_INDEX_REG, 0);
  3751. /* Set Offset */
  3752. mvpp2_rxq_offset_set(port, rxq->id, NET_SKB_PAD);
  3753. /* Set coalescing pkts and time */
  3754. mvpp2_rx_pkts_coal_set(port, rxq);
  3755. mvpp2_rx_time_coal_set(port, rxq);
  3756. /* Add number of descriptors ready for receiving packets */
  3757. mvpp2_rxq_status_update(port, rxq->id, 0, rxq->size);
  3758. return 0;
  3759. }
  3760. /* Push packets received by the RXQ to BM pool */
  3761. static void mvpp2_rxq_drop_pkts(struct mvpp2_port *port,
  3762. struct mvpp2_rx_queue *rxq)
  3763. {
  3764. int rx_received, i;
  3765. rx_received = mvpp2_rxq_received(port, rxq->id);
  3766. if (!rx_received)
  3767. return;
  3768. for (i = 0; i < rx_received; i++) {
  3769. struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
  3770. u32 bm = mvpp2_bm_cookie_build(rx_desc);
  3771. mvpp2_pool_refill(port, bm, rx_desc->buf_phys_addr,
  3772. rx_desc->buf_cookie);
  3773. }
  3774. mvpp2_rxq_status_update(port, rxq->id, rx_received, rx_received);
  3775. }
  3776. /* Cleanup Rx queue */
  3777. static void mvpp2_rxq_deinit(struct mvpp2_port *port,
  3778. struct mvpp2_rx_queue *rxq)
  3779. {
  3780. mvpp2_rxq_drop_pkts(port, rxq);
  3781. if (rxq->descs)
  3782. dma_free_coherent(port->dev->dev.parent,
  3783. rxq->size * MVPP2_DESC_ALIGNED_SIZE,
  3784. rxq->descs,
  3785. rxq->descs_phys);
  3786. rxq->descs = NULL;
  3787. rxq->last_desc = 0;
  3788. rxq->next_desc_to_proc = 0;
  3789. rxq->descs_phys = 0;
  3790. /* Clear Rx descriptors queue starting address and size;
  3791. * free descriptor number
  3792. */
  3793. mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
  3794. mvpp2_write(port->priv, MVPP2_RXQ_NUM_REG, rxq->id);
  3795. mvpp2_write(port->priv, MVPP2_RXQ_DESC_ADDR_REG, 0);
  3796. mvpp2_write(port->priv, MVPP2_RXQ_DESC_SIZE_REG, 0);
  3797. }
  3798. /* Create and initialize a Tx queue */
  3799. static int mvpp2_txq_init(struct mvpp2_port *port,
  3800. struct mvpp2_tx_queue *txq)
  3801. {
  3802. u32 val;
  3803. int cpu, desc, desc_per_txq, tx_port_num;
  3804. struct mvpp2_txq_pcpu *txq_pcpu;
  3805. txq->size = port->tx_ring_size;
  3806. /* Allocate memory for Tx descriptors */
  3807. txq->descs = dma_alloc_coherent(port->dev->dev.parent,
  3808. txq->size * MVPP2_DESC_ALIGNED_SIZE,
  3809. &txq->descs_phys, GFP_KERNEL);
  3810. if (!txq->descs)
  3811. return -ENOMEM;
  3812. txq->last_desc = txq->size - 1;
  3813. /* Set Tx descriptors queue starting address - indirect access */
  3814. mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
  3815. mvpp2_write(port->priv, MVPP2_TXQ_DESC_ADDR_REG, txq->descs_phys);
  3816. mvpp2_write(port->priv, MVPP2_TXQ_DESC_SIZE_REG, txq->size &
  3817. MVPP2_TXQ_DESC_SIZE_MASK);
  3818. mvpp2_write(port->priv, MVPP2_TXQ_INDEX_REG, 0);
  3819. mvpp2_write(port->priv, MVPP2_TXQ_RSVD_CLR_REG,
  3820. txq->id << MVPP2_TXQ_RSVD_CLR_OFFSET);
  3821. val = mvpp2_read(port->priv, MVPP2_TXQ_PENDING_REG);
  3822. val &= ~MVPP2_TXQ_PENDING_MASK;
  3823. mvpp2_write(port->priv, MVPP2_TXQ_PENDING_REG, val);
  3824. /* Calculate base address in prefetch buffer. We reserve 16 descriptors
  3825. * for each existing TXQ.
  3826. * TCONTS for PON port must be continuous from 0 to MVPP2_MAX_TCONT
  3827. * GBE ports assumed to be continious from 0 to MVPP2_MAX_PORTS
  3828. */
  3829. desc_per_txq = 16;
  3830. desc = (port->id * MVPP2_MAX_TXQ * desc_per_txq) +
  3831. (txq->log_id * desc_per_txq);
  3832. mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG,
  3833. MVPP2_PREF_BUF_PTR(desc) | MVPP2_PREF_BUF_SIZE_16 |
  3834. MVPP2_PREF_BUF_THRESH(desc_per_txq/2));
  3835. /* WRR / EJP configuration - indirect access */
  3836. tx_port_num = mvpp2_egress_port(port);
  3837. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
  3838. val = mvpp2_read(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id));
  3839. val &= ~MVPP2_TXQ_REFILL_PERIOD_ALL_MASK;
  3840. val |= MVPP2_TXQ_REFILL_PERIOD_MASK(1);
  3841. val |= MVPP2_TXQ_REFILL_TOKENS_ALL_MASK;
  3842. mvpp2_write(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id), val);
  3843. val = MVPP2_TXQ_TOKEN_SIZE_MAX;
  3844. mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq->log_id),
  3845. val);
  3846. for_each_present_cpu(cpu) {
  3847. txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
  3848. txq_pcpu->size = txq->size;
  3849. txq_pcpu->buffs = kmalloc(txq_pcpu->size *
  3850. sizeof(struct mvpp2_txq_pcpu_buf),
  3851. GFP_KERNEL);
  3852. if (!txq_pcpu->buffs)
  3853. goto error;
  3854. txq_pcpu->count = 0;
  3855. txq_pcpu->reserved_num = 0;
  3856. txq_pcpu->txq_put_index = 0;
  3857. txq_pcpu->txq_get_index = 0;
  3858. }
  3859. return 0;
  3860. error:
  3861. for_each_present_cpu(cpu) {
  3862. txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
  3863. kfree(txq_pcpu->buffs);
  3864. }
  3865. dma_free_coherent(port->dev->dev.parent,
  3866. txq->size * MVPP2_DESC_ALIGNED_SIZE,
  3867. txq->descs, txq->descs_phys);
  3868. return -ENOMEM;
  3869. }
  3870. /* Free allocated TXQ resources */
  3871. static void mvpp2_txq_deinit(struct mvpp2_port *port,
  3872. struct mvpp2_tx_queue *txq)
  3873. {
  3874. struct mvpp2_txq_pcpu *txq_pcpu;
  3875. int cpu;
  3876. for_each_present_cpu(cpu) {
  3877. txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
  3878. kfree(txq_pcpu->buffs);
  3879. }
  3880. if (txq->descs)
  3881. dma_free_coherent(port->dev->dev.parent,
  3882. txq->size * MVPP2_DESC_ALIGNED_SIZE,
  3883. txq->descs, txq->descs_phys);
  3884. txq->descs = NULL;
  3885. txq->last_desc = 0;
  3886. txq->next_desc_to_proc = 0;
  3887. txq->descs_phys = 0;
  3888. /* Set minimum bandwidth for disabled TXQs */
  3889. mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(txq->id), 0);
  3890. /* Set Tx descriptors queue starting address and size */
  3891. mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
  3892. mvpp2_write(port->priv, MVPP2_TXQ_DESC_ADDR_REG, 0);
  3893. mvpp2_write(port->priv, MVPP2_TXQ_DESC_SIZE_REG, 0);
  3894. }
  3895. /* Cleanup Tx ports */
  3896. static void mvpp2_txq_clean(struct mvpp2_port *port, struct mvpp2_tx_queue *txq)
  3897. {
  3898. struct mvpp2_txq_pcpu *txq_pcpu;
  3899. int delay, pending, cpu;
  3900. u32 val;
  3901. mvpp2_write(port->priv, MVPP2_TXQ_NUM_REG, txq->id);
  3902. val = mvpp2_read(port->priv, MVPP2_TXQ_PREF_BUF_REG);
  3903. val |= MVPP2_TXQ_DRAIN_EN_MASK;
  3904. mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG, val);
  3905. /* The napi queue has been stopped so wait for all packets
  3906. * to be transmitted.
  3907. */
  3908. delay = 0;
  3909. do {
  3910. if (delay >= MVPP2_TX_PENDING_TIMEOUT_MSEC) {
  3911. netdev_warn(port->dev,
  3912. "port %d: cleaning queue %d timed out\n",
  3913. port->id, txq->log_id);
  3914. break;
  3915. }
  3916. mdelay(1);
  3917. delay++;
  3918. pending = mvpp2_txq_pend_desc_num_get(port, txq);
  3919. } while (pending);
  3920. val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
  3921. mvpp2_write(port->priv, MVPP2_TXQ_PREF_BUF_REG, val);
  3922. for_each_present_cpu(cpu) {
  3923. txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
  3924. /* Release all packets */
  3925. mvpp2_txq_bufs_free(port, txq, txq_pcpu, txq_pcpu->count);
  3926. /* Reset queue */
  3927. txq_pcpu->count = 0;
  3928. txq_pcpu->txq_put_index = 0;
  3929. txq_pcpu->txq_get_index = 0;
  3930. }
  3931. }
  3932. /* Cleanup all Tx queues */
  3933. static void mvpp2_cleanup_txqs(struct mvpp2_port *port)
  3934. {
  3935. struct mvpp2_tx_queue *txq;
  3936. int queue;
  3937. u32 val;
  3938. val = mvpp2_read(port->priv, MVPP2_TX_PORT_FLUSH_REG);
  3939. /* Reset Tx ports and delete Tx queues */
  3940. val |= MVPP2_TX_PORT_FLUSH_MASK(port->id);
  3941. mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
  3942. for (queue = 0; queue < txq_number; queue++) {
  3943. txq = port->txqs[queue];
  3944. mvpp2_txq_clean(port, txq);
  3945. mvpp2_txq_deinit(port, txq);
  3946. }
  3947. on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
  3948. val &= ~MVPP2_TX_PORT_FLUSH_MASK(port->id);
  3949. mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
  3950. }
  3951. /* Cleanup all Rx queues */
  3952. static void mvpp2_cleanup_rxqs(struct mvpp2_port *port)
  3953. {
  3954. int queue;
  3955. for (queue = 0; queue < rxq_number; queue++)
  3956. mvpp2_rxq_deinit(port, port->rxqs[queue]);
  3957. }
  3958. /* Init all Rx queues for port */
  3959. static int mvpp2_setup_rxqs(struct mvpp2_port *port)
  3960. {
  3961. int queue, err;
  3962. for (queue = 0; queue < rxq_number; queue++) {
  3963. err = mvpp2_rxq_init(port, port->rxqs[queue]);
  3964. if (err)
  3965. goto err_cleanup;
  3966. }
  3967. return 0;
  3968. err_cleanup:
  3969. mvpp2_cleanup_rxqs(port);
  3970. return err;
  3971. }
  3972. /* Init all tx queues for port */
  3973. static int mvpp2_setup_txqs(struct mvpp2_port *port)
  3974. {
  3975. struct mvpp2_tx_queue *txq;
  3976. int queue, err;
  3977. for (queue = 0; queue < txq_number; queue++) {
  3978. txq = port->txqs[queue];
  3979. err = mvpp2_txq_init(port, txq);
  3980. if (err)
  3981. goto err_cleanup;
  3982. }
  3983. on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
  3984. return 0;
  3985. err_cleanup:
  3986. mvpp2_cleanup_txqs(port);
  3987. return err;
  3988. }
  3989. /* The callback for per-port interrupt */
  3990. static irqreturn_t mvpp2_isr(int irq, void *dev_id)
  3991. {
  3992. struct mvpp2_port *port = (struct mvpp2_port *)dev_id;
  3993. mvpp2_interrupts_disable(port);
  3994. napi_schedule(&port->napi);
  3995. return IRQ_HANDLED;
  3996. }
  3997. /* Adjust link */
  3998. static void mvpp2_link_event(struct net_device *dev)
  3999. {
  4000. struct mvpp2_port *port = netdev_priv(dev);
  4001. struct phy_device *phydev = dev->phydev;
  4002. int status_change = 0;
  4003. u32 val;
  4004. if (phydev->link) {
  4005. if ((port->speed != phydev->speed) ||
  4006. (port->duplex != phydev->duplex)) {
  4007. u32 val;
  4008. val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  4009. val &= ~(MVPP2_GMAC_CONFIG_MII_SPEED |
  4010. MVPP2_GMAC_CONFIG_GMII_SPEED |
  4011. MVPP2_GMAC_CONFIG_FULL_DUPLEX |
  4012. MVPP2_GMAC_AN_SPEED_EN |
  4013. MVPP2_GMAC_AN_DUPLEX_EN);
  4014. if (phydev->duplex)
  4015. val |= MVPP2_GMAC_CONFIG_FULL_DUPLEX;
  4016. if (phydev->speed == SPEED_1000)
  4017. val |= MVPP2_GMAC_CONFIG_GMII_SPEED;
  4018. else if (phydev->speed == SPEED_100)
  4019. val |= MVPP2_GMAC_CONFIG_MII_SPEED;
  4020. writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  4021. port->duplex = phydev->duplex;
  4022. port->speed = phydev->speed;
  4023. }
  4024. }
  4025. if (phydev->link != port->link) {
  4026. if (!phydev->link) {
  4027. port->duplex = -1;
  4028. port->speed = 0;
  4029. }
  4030. port->link = phydev->link;
  4031. status_change = 1;
  4032. }
  4033. if (status_change) {
  4034. if (phydev->link) {
  4035. val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  4036. val |= (MVPP2_GMAC_FORCE_LINK_PASS |
  4037. MVPP2_GMAC_FORCE_LINK_DOWN);
  4038. writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  4039. mvpp2_egress_enable(port);
  4040. mvpp2_ingress_enable(port);
  4041. } else {
  4042. mvpp2_ingress_disable(port);
  4043. mvpp2_egress_disable(port);
  4044. }
  4045. phy_print_status(phydev);
  4046. }
  4047. }
  4048. static void mvpp2_timer_set(struct mvpp2_port_pcpu *port_pcpu)
  4049. {
  4050. ktime_t interval;
  4051. if (!port_pcpu->timer_scheduled) {
  4052. port_pcpu->timer_scheduled = true;
  4053. interval = MVPP2_TXDONE_HRTIMER_PERIOD_NS;
  4054. hrtimer_start(&port_pcpu->tx_done_timer, interval,
  4055. HRTIMER_MODE_REL_PINNED);
  4056. }
  4057. }
  4058. static void mvpp2_tx_proc_cb(unsigned long data)
  4059. {
  4060. struct net_device *dev = (struct net_device *)data;
  4061. struct mvpp2_port *port = netdev_priv(dev);
  4062. struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
  4063. unsigned int tx_todo, cause;
  4064. if (!netif_running(dev))
  4065. return;
  4066. port_pcpu->timer_scheduled = false;
  4067. /* Process all the Tx queues */
  4068. cause = (1 << txq_number) - 1;
  4069. tx_todo = mvpp2_tx_done(port, cause);
  4070. /* Set the timer in case not all the packets were processed */
  4071. if (tx_todo)
  4072. mvpp2_timer_set(port_pcpu);
  4073. }
  4074. static enum hrtimer_restart mvpp2_hr_timer_cb(struct hrtimer *timer)
  4075. {
  4076. struct mvpp2_port_pcpu *port_pcpu = container_of(timer,
  4077. struct mvpp2_port_pcpu,
  4078. tx_done_timer);
  4079. tasklet_schedule(&port_pcpu->tx_done_tasklet);
  4080. return HRTIMER_NORESTART;
  4081. }
  4082. /* Main RX/TX processing routines */
  4083. /* Display more error info */
  4084. static void mvpp2_rx_error(struct mvpp2_port *port,
  4085. struct mvpp2_rx_desc *rx_desc)
  4086. {
  4087. u32 status = rx_desc->status;
  4088. switch (status & MVPP2_RXD_ERR_CODE_MASK) {
  4089. case MVPP2_RXD_ERR_CRC:
  4090. netdev_err(port->dev, "bad rx status %08x (crc error), size=%d\n",
  4091. status, rx_desc->data_size);
  4092. break;
  4093. case MVPP2_RXD_ERR_OVERRUN:
  4094. netdev_err(port->dev, "bad rx status %08x (overrun error), size=%d\n",
  4095. status, rx_desc->data_size);
  4096. break;
  4097. case MVPP2_RXD_ERR_RESOURCE:
  4098. netdev_err(port->dev, "bad rx status %08x (resource error), size=%d\n",
  4099. status, rx_desc->data_size);
  4100. break;
  4101. }
  4102. }
  4103. /* Handle RX checksum offload */
  4104. static void mvpp2_rx_csum(struct mvpp2_port *port, u32 status,
  4105. struct sk_buff *skb)
  4106. {
  4107. if (((status & MVPP2_RXD_L3_IP4) &&
  4108. !(status & MVPP2_RXD_IP4_HEADER_ERR)) ||
  4109. (status & MVPP2_RXD_L3_IP6))
  4110. if (((status & MVPP2_RXD_L4_UDP) ||
  4111. (status & MVPP2_RXD_L4_TCP)) &&
  4112. (status & MVPP2_RXD_L4_CSUM_OK)) {
  4113. skb->csum = 0;
  4114. skb->ip_summed = CHECKSUM_UNNECESSARY;
  4115. return;
  4116. }
  4117. skb->ip_summed = CHECKSUM_NONE;
  4118. }
  4119. /* Reuse skb if possible, or allocate a new skb and add it to BM pool */
  4120. static int mvpp2_rx_refill(struct mvpp2_port *port,
  4121. struct mvpp2_bm_pool *bm_pool, u32 bm)
  4122. {
  4123. dma_addr_t phys_addr;
  4124. void *buf;
  4125. /* No recycle or too many buffers are in use, so allocate a new skb */
  4126. buf = mvpp2_buf_alloc(port, bm_pool, &phys_addr, GFP_ATOMIC);
  4127. if (!buf)
  4128. return -ENOMEM;
  4129. mvpp2_pool_refill(port, bm, phys_addr, (unsigned long)buf);
  4130. return 0;
  4131. }
  4132. /* Handle tx checksum */
  4133. static u32 mvpp2_skb_tx_csum(struct mvpp2_port *port, struct sk_buff *skb)
  4134. {
  4135. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  4136. int ip_hdr_len = 0;
  4137. u8 l4_proto;
  4138. if (skb->protocol == htons(ETH_P_IP)) {
  4139. struct iphdr *ip4h = ip_hdr(skb);
  4140. /* Calculate IPv4 checksum and L4 checksum */
  4141. ip_hdr_len = ip4h->ihl;
  4142. l4_proto = ip4h->protocol;
  4143. } else if (skb->protocol == htons(ETH_P_IPV6)) {
  4144. struct ipv6hdr *ip6h = ipv6_hdr(skb);
  4145. /* Read l4_protocol from one of IPv6 extra headers */
  4146. if (skb_network_header_len(skb) > 0)
  4147. ip_hdr_len = (skb_network_header_len(skb) >> 2);
  4148. l4_proto = ip6h->nexthdr;
  4149. } else {
  4150. return MVPP2_TXD_L4_CSUM_NOT;
  4151. }
  4152. return mvpp2_txq_desc_csum(skb_network_offset(skb),
  4153. skb->protocol, ip_hdr_len, l4_proto);
  4154. }
  4155. return MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE;
  4156. }
  4157. static void mvpp2_buff_hdr_rx(struct mvpp2_port *port,
  4158. struct mvpp2_rx_desc *rx_desc)
  4159. {
  4160. struct mvpp2_buff_hdr *buff_hdr;
  4161. struct sk_buff *skb;
  4162. u32 rx_status = rx_desc->status;
  4163. dma_addr_t buff_phys_addr;
  4164. unsigned long buff_virt_addr;
  4165. dma_addr_t buff_phys_addr_next;
  4166. unsigned long buff_virt_addr_next;
  4167. int mc_id;
  4168. int pool_id;
  4169. pool_id = (rx_status & MVPP2_RXD_BM_POOL_ID_MASK) >>
  4170. MVPP2_RXD_BM_POOL_ID_OFFS;
  4171. buff_phys_addr = rx_desc->buf_phys_addr;
  4172. buff_virt_addr = rx_desc->buf_cookie;
  4173. do {
  4174. skb = (struct sk_buff *)buff_virt_addr;
  4175. buff_hdr = (struct mvpp2_buff_hdr *)skb->head;
  4176. mc_id = MVPP2_B_HDR_INFO_MC_ID(buff_hdr->info);
  4177. buff_phys_addr_next = buff_hdr->next_buff_phys_addr;
  4178. buff_virt_addr_next = buff_hdr->next_buff_virt_addr;
  4179. /* Release buffer */
  4180. mvpp2_bm_pool_mc_put(port, pool_id, buff_phys_addr,
  4181. buff_virt_addr, mc_id);
  4182. buff_phys_addr = buff_phys_addr_next;
  4183. buff_virt_addr = buff_virt_addr_next;
  4184. } while (!MVPP2_B_HDR_INFO_IS_LAST(buff_hdr->info));
  4185. }
  4186. /* Main rx processing */
  4187. static int mvpp2_rx(struct mvpp2_port *port, int rx_todo,
  4188. struct mvpp2_rx_queue *rxq)
  4189. {
  4190. struct net_device *dev = port->dev;
  4191. int rx_received;
  4192. int rx_done = 0;
  4193. u32 rcvd_pkts = 0;
  4194. u32 rcvd_bytes = 0;
  4195. /* Get number of received packets and clamp the to-do */
  4196. rx_received = mvpp2_rxq_received(port, rxq->id);
  4197. if (rx_todo > rx_received)
  4198. rx_todo = rx_received;
  4199. while (rx_done < rx_todo) {
  4200. struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
  4201. struct mvpp2_bm_pool *bm_pool;
  4202. struct sk_buff *skb;
  4203. unsigned int frag_size;
  4204. dma_addr_t phys_addr;
  4205. u32 bm, rx_status;
  4206. int pool, rx_bytes, err;
  4207. void *data;
  4208. rx_done++;
  4209. rx_status = rx_desc->status;
  4210. rx_bytes = rx_desc->data_size - MVPP2_MH_SIZE;
  4211. phys_addr = rx_desc->buf_phys_addr;
  4212. data = (void *)(uintptr_t)rx_desc->buf_cookie;
  4213. bm = mvpp2_bm_cookie_build(rx_desc);
  4214. pool = mvpp2_bm_cookie_pool_get(bm);
  4215. bm_pool = &port->priv->bm_pools[pool];
  4216. /* Check if buffer header is used */
  4217. if (rx_status & MVPP2_RXD_BUF_HDR) {
  4218. mvpp2_buff_hdr_rx(port, rx_desc);
  4219. continue;
  4220. }
  4221. /* In case of an error, release the requested buffer pointer
  4222. * to the Buffer Manager. This request process is controlled
  4223. * by the hardware, and the information about the buffer is
  4224. * comprised by the RX descriptor.
  4225. */
  4226. if (rx_status & MVPP2_RXD_ERR_SUMMARY) {
  4227. err_drop_frame:
  4228. dev->stats.rx_errors++;
  4229. mvpp2_rx_error(port, rx_desc);
  4230. /* Return the buffer to the pool */
  4231. mvpp2_pool_refill(port, bm, rx_desc->buf_phys_addr,
  4232. rx_desc->buf_cookie);
  4233. continue;
  4234. }
  4235. if (bm_pool->frag_size > PAGE_SIZE)
  4236. frag_size = 0;
  4237. else
  4238. frag_size = bm_pool->frag_size;
  4239. skb = build_skb(data, frag_size);
  4240. if (!skb) {
  4241. netdev_warn(port->dev, "skb build failed\n");
  4242. goto err_drop_frame;
  4243. }
  4244. err = mvpp2_rx_refill(port, bm_pool, bm);
  4245. if (err) {
  4246. netdev_err(port->dev, "failed to refill BM pools\n");
  4247. goto err_drop_frame;
  4248. }
  4249. dma_unmap_single(dev->dev.parent, phys_addr,
  4250. bm_pool->buf_size, DMA_FROM_DEVICE);
  4251. rcvd_pkts++;
  4252. rcvd_bytes += rx_bytes;
  4253. skb_reserve(skb, MVPP2_MH_SIZE + NET_SKB_PAD);
  4254. skb_put(skb, rx_bytes);
  4255. skb->protocol = eth_type_trans(skb, dev);
  4256. mvpp2_rx_csum(port, rx_status, skb);
  4257. napi_gro_receive(&port->napi, skb);
  4258. }
  4259. if (rcvd_pkts) {
  4260. struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
  4261. u64_stats_update_begin(&stats->syncp);
  4262. stats->rx_packets += rcvd_pkts;
  4263. stats->rx_bytes += rcvd_bytes;
  4264. u64_stats_update_end(&stats->syncp);
  4265. }
  4266. /* Update Rx queue management counters */
  4267. wmb();
  4268. mvpp2_rxq_status_update(port, rxq->id, rx_done, rx_done);
  4269. return rx_todo;
  4270. }
  4271. static inline void
  4272. tx_desc_unmap_put(struct device *dev, struct mvpp2_tx_queue *txq,
  4273. struct mvpp2_tx_desc *desc)
  4274. {
  4275. dma_unmap_single(dev, desc->buf_phys_addr,
  4276. desc->data_size, DMA_TO_DEVICE);
  4277. mvpp2_txq_desc_put(txq);
  4278. }
  4279. /* Handle tx fragmentation processing */
  4280. static int mvpp2_tx_frag_process(struct mvpp2_port *port, struct sk_buff *skb,
  4281. struct mvpp2_tx_queue *aggr_txq,
  4282. struct mvpp2_tx_queue *txq)
  4283. {
  4284. struct mvpp2_txq_pcpu *txq_pcpu = this_cpu_ptr(txq->pcpu);
  4285. struct mvpp2_tx_desc *tx_desc;
  4286. int i;
  4287. dma_addr_t buf_phys_addr;
  4288. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  4289. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  4290. void *addr = page_address(frag->page.p) + frag->page_offset;
  4291. tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
  4292. tx_desc->phys_txq = txq->id;
  4293. tx_desc->data_size = frag->size;
  4294. buf_phys_addr = dma_map_single(port->dev->dev.parent, addr,
  4295. tx_desc->data_size,
  4296. DMA_TO_DEVICE);
  4297. if (dma_mapping_error(port->dev->dev.parent, buf_phys_addr)) {
  4298. mvpp2_txq_desc_put(txq);
  4299. goto error;
  4300. }
  4301. tx_desc->packet_offset = buf_phys_addr & MVPP2_TX_DESC_ALIGN;
  4302. tx_desc->buf_phys_addr = buf_phys_addr & (~MVPP2_TX_DESC_ALIGN);
  4303. if (i == (skb_shinfo(skb)->nr_frags - 1)) {
  4304. /* Last descriptor */
  4305. tx_desc->command = MVPP2_TXD_L_DESC;
  4306. mvpp2_txq_inc_put(txq_pcpu, skb, tx_desc);
  4307. } else {
  4308. /* Descriptor in the middle: Not First, Not Last */
  4309. tx_desc->command = 0;
  4310. mvpp2_txq_inc_put(txq_pcpu, NULL, tx_desc);
  4311. }
  4312. }
  4313. return 0;
  4314. error:
  4315. /* Release all descriptors that were used to map fragments of
  4316. * this packet, as well as the corresponding DMA mappings
  4317. */
  4318. for (i = i - 1; i >= 0; i--) {
  4319. tx_desc = txq->descs + i;
  4320. tx_desc_unmap_put(port->dev->dev.parent, txq, tx_desc);
  4321. }
  4322. return -ENOMEM;
  4323. }
  4324. /* Main tx processing */
  4325. static int mvpp2_tx(struct sk_buff *skb, struct net_device *dev)
  4326. {
  4327. struct mvpp2_port *port = netdev_priv(dev);
  4328. struct mvpp2_tx_queue *txq, *aggr_txq;
  4329. struct mvpp2_txq_pcpu *txq_pcpu;
  4330. struct mvpp2_tx_desc *tx_desc;
  4331. dma_addr_t buf_phys_addr;
  4332. int frags = 0;
  4333. u16 txq_id;
  4334. u32 tx_cmd;
  4335. txq_id = skb_get_queue_mapping(skb);
  4336. txq = port->txqs[txq_id];
  4337. txq_pcpu = this_cpu_ptr(txq->pcpu);
  4338. aggr_txq = &port->priv->aggr_txqs[smp_processor_id()];
  4339. frags = skb_shinfo(skb)->nr_frags + 1;
  4340. /* Check number of available descriptors */
  4341. if (mvpp2_aggr_desc_num_check(port->priv, aggr_txq, frags) ||
  4342. mvpp2_txq_reserved_desc_num_proc(port->priv, txq,
  4343. txq_pcpu, frags)) {
  4344. frags = 0;
  4345. goto out;
  4346. }
  4347. /* Get a descriptor for the first part of the packet */
  4348. tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
  4349. tx_desc->phys_txq = txq->id;
  4350. tx_desc->data_size = skb_headlen(skb);
  4351. buf_phys_addr = dma_map_single(dev->dev.parent, skb->data,
  4352. tx_desc->data_size, DMA_TO_DEVICE);
  4353. if (unlikely(dma_mapping_error(dev->dev.parent, buf_phys_addr))) {
  4354. mvpp2_txq_desc_put(txq);
  4355. frags = 0;
  4356. goto out;
  4357. }
  4358. tx_desc->packet_offset = buf_phys_addr & MVPP2_TX_DESC_ALIGN;
  4359. tx_desc->buf_phys_addr = buf_phys_addr & ~MVPP2_TX_DESC_ALIGN;
  4360. tx_cmd = mvpp2_skb_tx_csum(port, skb);
  4361. if (frags == 1) {
  4362. /* First and Last descriptor */
  4363. tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC;
  4364. tx_desc->command = tx_cmd;
  4365. mvpp2_txq_inc_put(txq_pcpu, skb, tx_desc);
  4366. } else {
  4367. /* First but not Last */
  4368. tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_PADDING_DISABLE;
  4369. tx_desc->command = tx_cmd;
  4370. mvpp2_txq_inc_put(txq_pcpu, NULL, tx_desc);
  4371. /* Continue with other skb fragments */
  4372. if (mvpp2_tx_frag_process(port, skb, aggr_txq, txq)) {
  4373. tx_desc_unmap_put(port->dev->dev.parent, txq, tx_desc);
  4374. frags = 0;
  4375. goto out;
  4376. }
  4377. }
  4378. txq_pcpu->reserved_num -= frags;
  4379. txq_pcpu->count += frags;
  4380. aggr_txq->count += frags;
  4381. /* Enable transmit */
  4382. wmb();
  4383. mvpp2_aggr_txq_pend_desc_add(port, frags);
  4384. if (txq_pcpu->size - txq_pcpu->count < MAX_SKB_FRAGS + 1) {
  4385. struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
  4386. netif_tx_stop_queue(nq);
  4387. }
  4388. out:
  4389. if (frags > 0) {
  4390. struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
  4391. u64_stats_update_begin(&stats->syncp);
  4392. stats->tx_packets++;
  4393. stats->tx_bytes += skb->len;
  4394. u64_stats_update_end(&stats->syncp);
  4395. } else {
  4396. dev->stats.tx_dropped++;
  4397. dev_kfree_skb_any(skb);
  4398. }
  4399. /* Finalize TX processing */
  4400. if (txq_pcpu->count >= txq->done_pkts_coal)
  4401. mvpp2_txq_done(port, txq, txq_pcpu);
  4402. /* Set the timer in case not all frags were processed */
  4403. if (txq_pcpu->count <= frags && txq_pcpu->count > 0) {
  4404. struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
  4405. mvpp2_timer_set(port_pcpu);
  4406. }
  4407. return NETDEV_TX_OK;
  4408. }
  4409. static inline void mvpp2_cause_error(struct net_device *dev, int cause)
  4410. {
  4411. if (cause & MVPP2_CAUSE_FCS_ERR_MASK)
  4412. netdev_err(dev, "FCS error\n");
  4413. if (cause & MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK)
  4414. netdev_err(dev, "rx fifo overrun error\n");
  4415. if (cause & MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK)
  4416. netdev_err(dev, "tx fifo underrun error\n");
  4417. }
  4418. static int mvpp2_poll(struct napi_struct *napi, int budget)
  4419. {
  4420. u32 cause_rx_tx, cause_rx, cause_misc;
  4421. int rx_done = 0;
  4422. struct mvpp2_port *port = netdev_priv(napi->dev);
  4423. /* Rx/Tx cause register
  4424. *
  4425. * Bits 0-15: each bit indicates received packets on the Rx queue
  4426. * (bit 0 is for Rx queue 0).
  4427. *
  4428. * Bits 16-23: each bit indicates transmitted packets on the Tx queue
  4429. * (bit 16 is for Tx queue 0).
  4430. *
  4431. * Each CPU has its own Rx/Tx cause register
  4432. */
  4433. cause_rx_tx = mvpp2_read(port->priv,
  4434. MVPP2_ISR_RX_TX_CAUSE_REG(port->id));
  4435. cause_rx_tx &= ~MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
  4436. cause_misc = cause_rx_tx & MVPP2_CAUSE_MISC_SUM_MASK;
  4437. if (cause_misc) {
  4438. mvpp2_cause_error(port->dev, cause_misc);
  4439. /* Clear the cause register */
  4440. mvpp2_write(port->priv, MVPP2_ISR_MISC_CAUSE_REG, 0);
  4441. mvpp2_write(port->priv, MVPP2_ISR_RX_TX_CAUSE_REG(port->id),
  4442. cause_rx_tx & ~MVPP2_CAUSE_MISC_SUM_MASK);
  4443. }
  4444. cause_rx = cause_rx_tx & MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
  4445. /* Process RX packets */
  4446. cause_rx |= port->pending_cause_rx;
  4447. while (cause_rx && budget > 0) {
  4448. int count;
  4449. struct mvpp2_rx_queue *rxq;
  4450. rxq = mvpp2_get_rx_queue(port, cause_rx);
  4451. if (!rxq)
  4452. break;
  4453. count = mvpp2_rx(port, budget, rxq);
  4454. rx_done += count;
  4455. budget -= count;
  4456. if (budget > 0) {
  4457. /* Clear the bit associated to this Rx queue
  4458. * so that next iteration will continue from
  4459. * the next Rx queue.
  4460. */
  4461. cause_rx &= ~(1 << rxq->logic_rxq);
  4462. }
  4463. }
  4464. if (budget > 0) {
  4465. cause_rx = 0;
  4466. napi_complete_done(napi, rx_done);
  4467. mvpp2_interrupts_enable(port);
  4468. }
  4469. port->pending_cause_rx = cause_rx;
  4470. return rx_done;
  4471. }
  4472. /* Set hw internals when starting port */
  4473. static void mvpp2_start_dev(struct mvpp2_port *port)
  4474. {
  4475. struct net_device *ndev = port->dev;
  4476. mvpp2_gmac_max_rx_size_set(port);
  4477. mvpp2_txp_max_tx_size_set(port);
  4478. napi_enable(&port->napi);
  4479. /* Enable interrupts on all CPUs */
  4480. mvpp2_interrupts_enable(port);
  4481. mvpp2_port_enable(port);
  4482. phy_start(ndev->phydev);
  4483. netif_tx_start_all_queues(port->dev);
  4484. }
  4485. /* Set hw internals when stopping port */
  4486. static void mvpp2_stop_dev(struct mvpp2_port *port)
  4487. {
  4488. struct net_device *ndev = port->dev;
  4489. /* Stop new packets from arriving to RXQs */
  4490. mvpp2_ingress_disable(port);
  4491. mdelay(10);
  4492. /* Disable interrupts on all CPUs */
  4493. mvpp2_interrupts_disable(port);
  4494. napi_disable(&port->napi);
  4495. netif_carrier_off(port->dev);
  4496. netif_tx_stop_all_queues(port->dev);
  4497. mvpp2_egress_disable(port);
  4498. mvpp2_port_disable(port);
  4499. phy_stop(ndev->phydev);
  4500. }
  4501. static int mvpp2_check_ringparam_valid(struct net_device *dev,
  4502. struct ethtool_ringparam *ring)
  4503. {
  4504. u16 new_rx_pending = ring->rx_pending;
  4505. u16 new_tx_pending = ring->tx_pending;
  4506. if (ring->rx_pending == 0 || ring->tx_pending == 0)
  4507. return -EINVAL;
  4508. if (ring->rx_pending > MVPP2_MAX_RXD)
  4509. new_rx_pending = MVPP2_MAX_RXD;
  4510. else if (!IS_ALIGNED(ring->rx_pending, 16))
  4511. new_rx_pending = ALIGN(ring->rx_pending, 16);
  4512. if (ring->tx_pending > MVPP2_MAX_TXD)
  4513. new_tx_pending = MVPP2_MAX_TXD;
  4514. else if (!IS_ALIGNED(ring->tx_pending, 32))
  4515. new_tx_pending = ALIGN(ring->tx_pending, 32);
  4516. if (ring->rx_pending != new_rx_pending) {
  4517. netdev_info(dev, "illegal Rx ring size value %d, round to %d\n",
  4518. ring->rx_pending, new_rx_pending);
  4519. ring->rx_pending = new_rx_pending;
  4520. }
  4521. if (ring->tx_pending != new_tx_pending) {
  4522. netdev_info(dev, "illegal Tx ring size value %d, round to %d\n",
  4523. ring->tx_pending, new_tx_pending);
  4524. ring->tx_pending = new_tx_pending;
  4525. }
  4526. return 0;
  4527. }
  4528. static void mvpp2_get_mac_address(struct mvpp2_port *port, unsigned char *addr)
  4529. {
  4530. u32 mac_addr_l, mac_addr_m, mac_addr_h;
  4531. mac_addr_l = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
  4532. mac_addr_m = readl(port->priv->lms_base + MVPP2_SRC_ADDR_MIDDLE);
  4533. mac_addr_h = readl(port->priv->lms_base + MVPP2_SRC_ADDR_HIGH);
  4534. addr[0] = (mac_addr_h >> 24) & 0xFF;
  4535. addr[1] = (mac_addr_h >> 16) & 0xFF;
  4536. addr[2] = (mac_addr_h >> 8) & 0xFF;
  4537. addr[3] = mac_addr_h & 0xFF;
  4538. addr[4] = mac_addr_m & 0xFF;
  4539. addr[5] = (mac_addr_l >> MVPP2_GMAC_SA_LOW_OFFS) & 0xFF;
  4540. }
  4541. static int mvpp2_phy_connect(struct mvpp2_port *port)
  4542. {
  4543. struct phy_device *phy_dev;
  4544. phy_dev = of_phy_connect(port->dev, port->phy_node, mvpp2_link_event, 0,
  4545. port->phy_interface);
  4546. if (!phy_dev) {
  4547. netdev_err(port->dev, "cannot connect to phy\n");
  4548. return -ENODEV;
  4549. }
  4550. phy_dev->supported &= PHY_GBIT_FEATURES;
  4551. phy_dev->advertising = phy_dev->supported;
  4552. port->link = 0;
  4553. port->duplex = 0;
  4554. port->speed = 0;
  4555. return 0;
  4556. }
  4557. static void mvpp2_phy_disconnect(struct mvpp2_port *port)
  4558. {
  4559. struct net_device *ndev = port->dev;
  4560. phy_disconnect(ndev->phydev);
  4561. }
  4562. static int mvpp2_open(struct net_device *dev)
  4563. {
  4564. struct mvpp2_port *port = netdev_priv(dev);
  4565. unsigned char mac_bcast[ETH_ALEN] = {
  4566. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  4567. int err;
  4568. err = mvpp2_prs_mac_da_accept(port->priv, port->id, mac_bcast, true);
  4569. if (err) {
  4570. netdev_err(dev, "mvpp2_prs_mac_da_accept BC failed\n");
  4571. return err;
  4572. }
  4573. err = mvpp2_prs_mac_da_accept(port->priv, port->id,
  4574. dev->dev_addr, true);
  4575. if (err) {
  4576. netdev_err(dev, "mvpp2_prs_mac_da_accept MC failed\n");
  4577. return err;
  4578. }
  4579. err = mvpp2_prs_tag_mode_set(port->priv, port->id, MVPP2_TAG_TYPE_MH);
  4580. if (err) {
  4581. netdev_err(dev, "mvpp2_prs_tag_mode_set failed\n");
  4582. return err;
  4583. }
  4584. err = mvpp2_prs_def_flow(port);
  4585. if (err) {
  4586. netdev_err(dev, "mvpp2_prs_def_flow failed\n");
  4587. return err;
  4588. }
  4589. /* Allocate the Rx/Tx queues */
  4590. err = mvpp2_setup_rxqs(port);
  4591. if (err) {
  4592. netdev_err(port->dev, "cannot allocate Rx queues\n");
  4593. return err;
  4594. }
  4595. err = mvpp2_setup_txqs(port);
  4596. if (err) {
  4597. netdev_err(port->dev, "cannot allocate Tx queues\n");
  4598. goto err_cleanup_rxqs;
  4599. }
  4600. err = request_irq(port->irq, mvpp2_isr, 0, dev->name, port);
  4601. if (err) {
  4602. netdev_err(port->dev, "cannot request IRQ %d\n", port->irq);
  4603. goto err_cleanup_txqs;
  4604. }
  4605. /* In default link is down */
  4606. netif_carrier_off(port->dev);
  4607. err = mvpp2_phy_connect(port);
  4608. if (err < 0)
  4609. goto err_free_irq;
  4610. /* Unmask interrupts on all CPUs */
  4611. on_each_cpu(mvpp2_interrupts_unmask, port, 1);
  4612. mvpp2_start_dev(port);
  4613. return 0;
  4614. err_free_irq:
  4615. free_irq(port->irq, port);
  4616. err_cleanup_txqs:
  4617. mvpp2_cleanup_txqs(port);
  4618. err_cleanup_rxqs:
  4619. mvpp2_cleanup_rxqs(port);
  4620. return err;
  4621. }
  4622. static int mvpp2_stop(struct net_device *dev)
  4623. {
  4624. struct mvpp2_port *port = netdev_priv(dev);
  4625. struct mvpp2_port_pcpu *port_pcpu;
  4626. int cpu;
  4627. mvpp2_stop_dev(port);
  4628. mvpp2_phy_disconnect(port);
  4629. /* Mask interrupts on all CPUs */
  4630. on_each_cpu(mvpp2_interrupts_mask, port, 1);
  4631. free_irq(port->irq, port);
  4632. for_each_present_cpu(cpu) {
  4633. port_pcpu = per_cpu_ptr(port->pcpu, cpu);
  4634. hrtimer_cancel(&port_pcpu->tx_done_timer);
  4635. port_pcpu->timer_scheduled = false;
  4636. tasklet_kill(&port_pcpu->tx_done_tasklet);
  4637. }
  4638. mvpp2_cleanup_rxqs(port);
  4639. mvpp2_cleanup_txqs(port);
  4640. return 0;
  4641. }
  4642. static void mvpp2_set_rx_mode(struct net_device *dev)
  4643. {
  4644. struct mvpp2_port *port = netdev_priv(dev);
  4645. struct mvpp2 *priv = port->priv;
  4646. struct netdev_hw_addr *ha;
  4647. int id = port->id;
  4648. bool allmulti = dev->flags & IFF_ALLMULTI;
  4649. mvpp2_prs_mac_promisc_set(priv, id, dev->flags & IFF_PROMISC);
  4650. mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_ALL, allmulti);
  4651. mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_IP6, allmulti);
  4652. /* Remove all port->id's mcast enries */
  4653. mvpp2_prs_mcast_del_all(priv, id);
  4654. if (allmulti && !netdev_mc_empty(dev)) {
  4655. netdev_for_each_mc_addr(ha, dev)
  4656. mvpp2_prs_mac_da_accept(priv, id, ha->addr, true);
  4657. }
  4658. }
  4659. static int mvpp2_set_mac_address(struct net_device *dev, void *p)
  4660. {
  4661. struct mvpp2_port *port = netdev_priv(dev);
  4662. const struct sockaddr *addr = p;
  4663. int err;
  4664. if (!is_valid_ether_addr(addr->sa_data)) {
  4665. err = -EADDRNOTAVAIL;
  4666. goto error;
  4667. }
  4668. if (!netif_running(dev)) {
  4669. err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
  4670. if (!err)
  4671. return 0;
  4672. /* Reconfigure parser to accept the original MAC address */
  4673. err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
  4674. if (err)
  4675. goto error;
  4676. }
  4677. mvpp2_stop_dev(port);
  4678. err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
  4679. if (!err)
  4680. goto out_start;
  4681. /* Reconfigure parser accept the original MAC address */
  4682. err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
  4683. if (err)
  4684. goto error;
  4685. out_start:
  4686. mvpp2_start_dev(port);
  4687. mvpp2_egress_enable(port);
  4688. mvpp2_ingress_enable(port);
  4689. return 0;
  4690. error:
  4691. netdev_err(dev, "fail to change MAC address\n");
  4692. return err;
  4693. }
  4694. static int mvpp2_change_mtu(struct net_device *dev, int mtu)
  4695. {
  4696. struct mvpp2_port *port = netdev_priv(dev);
  4697. int err;
  4698. if (!IS_ALIGNED(MVPP2_RX_PKT_SIZE(mtu), 8)) {
  4699. netdev_info(dev, "illegal MTU value %d, round to %d\n", mtu,
  4700. ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8));
  4701. mtu = ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8);
  4702. }
  4703. if (!netif_running(dev)) {
  4704. err = mvpp2_bm_update_mtu(dev, mtu);
  4705. if (!err) {
  4706. port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
  4707. return 0;
  4708. }
  4709. /* Reconfigure BM to the original MTU */
  4710. err = mvpp2_bm_update_mtu(dev, dev->mtu);
  4711. if (err)
  4712. goto error;
  4713. }
  4714. mvpp2_stop_dev(port);
  4715. err = mvpp2_bm_update_mtu(dev, mtu);
  4716. if (!err) {
  4717. port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
  4718. goto out_start;
  4719. }
  4720. /* Reconfigure BM to the original MTU */
  4721. err = mvpp2_bm_update_mtu(dev, dev->mtu);
  4722. if (err)
  4723. goto error;
  4724. out_start:
  4725. mvpp2_start_dev(port);
  4726. mvpp2_egress_enable(port);
  4727. mvpp2_ingress_enable(port);
  4728. return 0;
  4729. error:
  4730. netdev_err(dev, "fail to change MTU\n");
  4731. return err;
  4732. }
  4733. static void
  4734. mvpp2_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  4735. {
  4736. struct mvpp2_port *port = netdev_priv(dev);
  4737. unsigned int start;
  4738. int cpu;
  4739. for_each_possible_cpu(cpu) {
  4740. struct mvpp2_pcpu_stats *cpu_stats;
  4741. u64 rx_packets;
  4742. u64 rx_bytes;
  4743. u64 tx_packets;
  4744. u64 tx_bytes;
  4745. cpu_stats = per_cpu_ptr(port->stats, cpu);
  4746. do {
  4747. start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
  4748. rx_packets = cpu_stats->rx_packets;
  4749. rx_bytes = cpu_stats->rx_bytes;
  4750. tx_packets = cpu_stats->tx_packets;
  4751. tx_bytes = cpu_stats->tx_bytes;
  4752. } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
  4753. stats->rx_packets += rx_packets;
  4754. stats->rx_bytes += rx_bytes;
  4755. stats->tx_packets += tx_packets;
  4756. stats->tx_bytes += tx_bytes;
  4757. }
  4758. stats->rx_errors = dev->stats.rx_errors;
  4759. stats->rx_dropped = dev->stats.rx_dropped;
  4760. stats->tx_dropped = dev->stats.tx_dropped;
  4761. }
  4762. static int mvpp2_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  4763. {
  4764. int ret;
  4765. if (!dev->phydev)
  4766. return -ENOTSUPP;
  4767. ret = phy_mii_ioctl(dev->phydev, ifr, cmd);
  4768. if (!ret)
  4769. mvpp2_link_event(dev);
  4770. return ret;
  4771. }
  4772. /* Ethtool methods */
  4773. /* Set interrupt coalescing for ethtools */
  4774. static int mvpp2_ethtool_set_coalesce(struct net_device *dev,
  4775. struct ethtool_coalesce *c)
  4776. {
  4777. struct mvpp2_port *port = netdev_priv(dev);
  4778. int queue;
  4779. for (queue = 0; queue < rxq_number; queue++) {
  4780. struct mvpp2_rx_queue *rxq = port->rxqs[queue];
  4781. rxq->time_coal = c->rx_coalesce_usecs;
  4782. rxq->pkts_coal = c->rx_max_coalesced_frames;
  4783. mvpp2_rx_pkts_coal_set(port, rxq);
  4784. mvpp2_rx_time_coal_set(port, rxq);
  4785. }
  4786. for (queue = 0; queue < txq_number; queue++) {
  4787. struct mvpp2_tx_queue *txq = port->txqs[queue];
  4788. txq->done_pkts_coal = c->tx_max_coalesced_frames;
  4789. }
  4790. return 0;
  4791. }
  4792. /* get coalescing for ethtools */
  4793. static int mvpp2_ethtool_get_coalesce(struct net_device *dev,
  4794. struct ethtool_coalesce *c)
  4795. {
  4796. struct mvpp2_port *port = netdev_priv(dev);
  4797. c->rx_coalesce_usecs = port->rxqs[0]->time_coal;
  4798. c->rx_max_coalesced_frames = port->rxqs[0]->pkts_coal;
  4799. c->tx_max_coalesced_frames = port->txqs[0]->done_pkts_coal;
  4800. return 0;
  4801. }
  4802. static void mvpp2_ethtool_get_drvinfo(struct net_device *dev,
  4803. struct ethtool_drvinfo *drvinfo)
  4804. {
  4805. strlcpy(drvinfo->driver, MVPP2_DRIVER_NAME,
  4806. sizeof(drvinfo->driver));
  4807. strlcpy(drvinfo->version, MVPP2_DRIVER_VERSION,
  4808. sizeof(drvinfo->version));
  4809. strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
  4810. sizeof(drvinfo->bus_info));
  4811. }
  4812. static void mvpp2_ethtool_get_ringparam(struct net_device *dev,
  4813. struct ethtool_ringparam *ring)
  4814. {
  4815. struct mvpp2_port *port = netdev_priv(dev);
  4816. ring->rx_max_pending = MVPP2_MAX_RXD;
  4817. ring->tx_max_pending = MVPP2_MAX_TXD;
  4818. ring->rx_pending = port->rx_ring_size;
  4819. ring->tx_pending = port->tx_ring_size;
  4820. }
  4821. static int mvpp2_ethtool_set_ringparam(struct net_device *dev,
  4822. struct ethtool_ringparam *ring)
  4823. {
  4824. struct mvpp2_port *port = netdev_priv(dev);
  4825. u16 prev_rx_ring_size = port->rx_ring_size;
  4826. u16 prev_tx_ring_size = port->tx_ring_size;
  4827. int err;
  4828. err = mvpp2_check_ringparam_valid(dev, ring);
  4829. if (err)
  4830. return err;
  4831. if (!netif_running(dev)) {
  4832. port->rx_ring_size = ring->rx_pending;
  4833. port->tx_ring_size = ring->tx_pending;
  4834. return 0;
  4835. }
  4836. /* The interface is running, so we have to force a
  4837. * reallocation of the queues
  4838. */
  4839. mvpp2_stop_dev(port);
  4840. mvpp2_cleanup_rxqs(port);
  4841. mvpp2_cleanup_txqs(port);
  4842. port->rx_ring_size = ring->rx_pending;
  4843. port->tx_ring_size = ring->tx_pending;
  4844. err = mvpp2_setup_rxqs(port);
  4845. if (err) {
  4846. /* Reallocate Rx queues with the original ring size */
  4847. port->rx_ring_size = prev_rx_ring_size;
  4848. ring->rx_pending = prev_rx_ring_size;
  4849. err = mvpp2_setup_rxqs(port);
  4850. if (err)
  4851. goto err_out;
  4852. }
  4853. err = mvpp2_setup_txqs(port);
  4854. if (err) {
  4855. /* Reallocate Tx queues with the original ring size */
  4856. port->tx_ring_size = prev_tx_ring_size;
  4857. ring->tx_pending = prev_tx_ring_size;
  4858. err = mvpp2_setup_txqs(port);
  4859. if (err)
  4860. goto err_clean_rxqs;
  4861. }
  4862. mvpp2_start_dev(port);
  4863. mvpp2_egress_enable(port);
  4864. mvpp2_ingress_enable(port);
  4865. return 0;
  4866. err_clean_rxqs:
  4867. mvpp2_cleanup_rxqs(port);
  4868. err_out:
  4869. netdev_err(dev, "fail to change ring parameters");
  4870. return err;
  4871. }
  4872. /* Device ops */
  4873. static const struct net_device_ops mvpp2_netdev_ops = {
  4874. .ndo_open = mvpp2_open,
  4875. .ndo_stop = mvpp2_stop,
  4876. .ndo_start_xmit = mvpp2_tx,
  4877. .ndo_set_rx_mode = mvpp2_set_rx_mode,
  4878. .ndo_set_mac_address = mvpp2_set_mac_address,
  4879. .ndo_change_mtu = mvpp2_change_mtu,
  4880. .ndo_get_stats64 = mvpp2_get_stats64,
  4881. .ndo_do_ioctl = mvpp2_ioctl,
  4882. };
  4883. static const struct ethtool_ops mvpp2_eth_tool_ops = {
  4884. .nway_reset = phy_ethtool_nway_reset,
  4885. .get_link = ethtool_op_get_link,
  4886. .set_coalesce = mvpp2_ethtool_set_coalesce,
  4887. .get_coalesce = mvpp2_ethtool_get_coalesce,
  4888. .get_drvinfo = mvpp2_ethtool_get_drvinfo,
  4889. .get_ringparam = mvpp2_ethtool_get_ringparam,
  4890. .set_ringparam = mvpp2_ethtool_set_ringparam,
  4891. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  4892. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  4893. };
  4894. /* Driver initialization */
  4895. static void mvpp2_port_power_up(struct mvpp2_port *port)
  4896. {
  4897. mvpp2_port_mii_set(port);
  4898. mvpp2_port_periodic_xon_disable(port);
  4899. mvpp2_port_fc_adv_enable(port);
  4900. mvpp2_port_reset(port);
  4901. }
  4902. /* Initialize port HW */
  4903. static int mvpp2_port_init(struct mvpp2_port *port)
  4904. {
  4905. struct device *dev = port->dev->dev.parent;
  4906. struct mvpp2 *priv = port->priv;
  4907. struct mvpp2_txq_pcpu *txq_pcpu;
  4908. int queue, cpu, err;
  4909. if (port->first_rxq + rxq_number > MVPP2_RXQ_TOTAL_NUM)
  4910. return -EINVAL;
  4911. /* Disable port */
  4912. mvpp2_egress_disable(port);
  4913. mvpp2_port_disable(port);
  4914. port->txqs = devm_kcalloc(dev, txq_number, sizeof(*port->txqs),
  4915. GFP_KERNEL);
  4916. if (!port->txqs)
  4917. return -ENOMEM;
  4918. /* Associate physical Tx queues to this port and initialize.
  4919. * The mapping is predefined.
  4920. */
  4921. for (queue = 0; queue < txq_number; queue++) {
  4922. int queue_phy_id = mvpp2_txq_phys(port->id, queue);
  4923. struct mvpp2_tx_queue *txq;
  4924. txq = devm_kzalloc(dev, sizeof(*txq), GFP_KERNEL);
  4925. if (!txq) {
  4926. err = -ENOMEM;
  4927. goto err_free_percpu;
  4928. }
  4929. txq->pcpu = alloc_percpu(struct mvpp2_txq_pcpu);
  4930. if (!txq->pcpu) {
  4931. err = -ENOMEM;
  4932. goto err_free_percpu;
  4933. }
  4934. txq->id = queue_phy_id;
  4935. txq->log_id = queue;
  4936. txq->done_pkts_coal = MVPP2_TXDONE_COAL_PKTS_THRESH;
  4937. for_each_present_cpu(cpu) {
  4938. txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
  4939. txq_pcpu->cpu = cpu;
  4940. }
  4941. port->txqs[queue] = txq;
  4942. }
  4943. port->rxqs = devm_kcalloc(dev, rxq_number, sizeof(*port->rxqs),
  4944. GFP_KERNEL);
  4945. if (!port->rxqs) {
  4946. err = -ENOMEM;
  4947. goto err_free_percpu;
  4948. }
  4949. /* Allocate and initialize Rx queue for this port */
  4950. for (queue = 0; queue < rxq_number; queue++) {
  4951. struct mvpp2_rx_queue *rxq;
  4952. /* Map physical Rx queue to port's logical Rx queue */
  4953. rxq = devm_kzalloc(dev, sizeof(*rxq), GFP_KERNEL);
  4954. if (!rxq) {
  4955. err = -ENOMEM;
  4956. goto err_free_percpu;
  4957. }
  4958. /* Map this Rx queue to a physical queue */
  4959. rxq->id = port->first_rxq + queue;
  4960. rxq->port = port->id;
  4961. rxq->logic_rxq = queue;
  4962. port->rxqs[queue] = rxq;
  4963. }
  4964. /* Configure Rx queue group interrupt for this port */
  4965. mvpp2_write(priv, MVPP2_ISR_RXQ_GROUP_REG(port->id), rxq_number);
  4966. /* Create Rx descriptor rings */
  4967. for (queue = 0; queue < rxq_number; queue++) {
  4968. struct mvpp2_rx_queue *rxq = port->rxqs[queue];
  4969. rxq->size = port->rx_ring_size;
  4970. rxq->pkts_coal = MVPP2_RX_COAL_PKTS;
  4971. rxq->time_coal = MVPP2_RX_COAL_USEC;
  4972. }
  4973. mvpp2_ingress_disable(port);
  4974. /* Port default configuration */
  4975. mvpp2_defaults_set(port);
  4976. /* Port's classifier configuration */
  4977. mvpp2_cls_oversize_rxq_set(port);
  4978. mvpp2_cls_port_config(port);
  4979. /* Provide an initial Rx packet size */
  4980. port->pkt_size = MVPP2_RX_PKT_SIZE(port->dev->mtu);
  4981. /* Initialize pools for swf */
  4982. err = mvpp2_swf_bm_pool_init(port);
  4983. if (err)
  4984. goto err_free_percpu;
  4985. return 0;
  4986. err_free_percpu:
  4987. for (queue = 0; queue < txq_number; queue++) {
  4988. if (!port->txqs[queue])
  4989. continue;
  4990. free_percpu(port->txqs[queue]->pcpu);
  4991. }
  4992. return err;
  4993. }
  4994. /* Ports initialization */
  4995. static int mvpp2_port_probe(struct platform_device *pdev,
  4996. struct device_node *port_node,
  4997. struct mvpp2 *priv,
  4998. int *next_first_rxq)
  4999. {
  5000. struct device_node *phy_node;
  5001. struct mvpp2_port *port;
  5002. struct mvpp2_port_pcpu *port_pcpu;
  5003. struct net_device *dev;
  5004. struct resource *res;
  5005. const char *dt_mac_addr;
  5006. const char *mac_from;
  5007. char hw_mac_addr[ETH_ALEN];
  5008. u32 id;
  5009. int features;
  5010. int phy_mode;
  5011. int priv_common_regs_num = 2;
  5012. int err, i, cpu;
  5013. dev = alloc_etherdev_mqs(sizeof(struct mvpp2_port), txq_number,
  5014. rxq_number);
  5015. if (!dev)
  5016. return -ENOMEM;
  5017. phy_node = of_parse_phandle(port_node, "phy", 0);
  5018. if (!phy_node) {
  5019. dev_err(&pdev->dev, "missing phy\n");
  5020. err = -ENODEV;
  5021. goto err_free_netdev;
  5022. }
  5023. phy_mode = of_get_phy_mode(port_node);
  5024. if (phy_mode < 0) {
  5025. dev_err(&pdev->dev, "incorrect phy mode\n");
  5026. err = phy_mode;
  5027. goto err_free_netdev;
  5028. }
  5029. if (of_property_read_u32(port_node, "port-id", &id)) {
  5030. err = -EINVAL;
  5031. dev_err(&pdev->dev, "missing port-id value\n");
  5032. goto err_free_netdev;
  5033. }
  5034. dev->tx_queue_len = MVPP2_MAX_TXD;
  5035. dev->watchdog_timeo = 5 * HZ;
  5036. dev->netdev_ops = &mvpp2_netdev_ops;
  5037. dev->ethtool_ops = &mvpp2_eth_tool_ops;
  5038. port = netdev_priv(dev);
  5039. port->irq = irq_of_parse_and_map(port_node, 0);
  5040. if (port->irq <= 0) {
  5041. err = -EINVAL;
  5042. goto err_free_netdev;
  5043. }
  5044. if (of_property_read_bool(port_node, "marvell,loopback"))
  5045. port->flags |= MVPP2_F_LOOPBACK;
  5046. port->priv = priv;
  5047. port->id = id;
  5048. port->first_rxq = *next_first_rxq;
  5049. port->phy_node = phy_node;
  5050. port->phy_interface = phy_mode;
  5051. res = platform_get_resource(pdev, IORESOURCE_MEM,
  5052. priv_common_regs_num + id);
  5053. port->base = devm_ioremap_resource(&pdev->dev, res);
  5054. if (IS_ERR(port->base)) {
  5055. err = PTR_ERR(port->base);
  5056. goto err_free_irq;
  5057. }
  5058. /* Alloc per-cpu stats */
  5059. port->stats = netdev_alloc_pcpu_stats(struct mvpp2_pcpu_stats);
  5060. if (!port->stats) {
  5061. err = -ENOMEM;
  5062. goto err_free_irq;
  5063. }
  5064. dt_mac_addr = of_get_mac_address(port_node);
  5065. if (dt_mac_addr && is_valid_ether_addr(dt_mac_addr)) {
  5066. mac_from = "device tree";
  5067. ether_addr_copy(dev->dev_addr, dt_mac_addr);
  5068. } else {
  5069. mvpp2_get_mac_address(port, hw_mac_addr);
  5070. if (is_valid_ether_addr(hw_mac_addr)) {
  5071. mac_from = "hardware";
  5072. ether_addr_copy(dev->dev_addr, hw_mac_addr);
  5073. } else {
  5074. mac_from = "random";
  5075. eth_hw_addr_random(dev);
  5076. }
  5077. }
  5078. port->tx_ring_size = MVPP2_MAX_TXD;
  5079. port->rx_ring_size = MVPP2_MAX_RXD;
  5080. port->dev = dev;
  5081. SET_NETDEV_DEV(dev, &pdev->dev);
  5082. err = mvpp2_port_init(port);
  5083. if (err < 0) {
  5084. dev_err(&pdev->dev, "failed to init port %d\n", id);
  5085. goto err_free_stats;
  5086. }
  5087. mvpp2_port_power_up(port);
  5088. port->pcpu = alloc_percpu(struct mvpp2_port_pcpu);
  5089. if (!port->pcpu) {
  5090. err = -ENOMEM;
  5091. goto err_free_txq_pcpu;
  5092. }
  5093. for_each_present_cpu(cpu) {
  5094. port_pcpu = per_cpu_ptr(port->pcpu, cpu);
  5095. hrtimer_init(&port_pcpu->tx_done_timer, CLOCK_MONOTONIC,
  5096. HRTIMER_MODE_REL_PINNED);
  5097. port_pcpu->tx_done_timer.function = mvpp2_hr_timer_cb;
  5098. port_pcpu->timer_scheduled = false;
  5099. tasklet_init(&port_pcpu->tx_done_tasklet, mvpp2_tx_proc_cb,
  5100. (unsigned long)dev);
  5101. }
  5102. netif_napi_add(dev, &port->napi, mvpp2_poll, NAPI_POLL_WEIGHT);
  5103. features = NETIF_F_SG | NETIF_F_IP_CSUM;
  5104. dev->features = features | NETIF_F_RXCSUM;
  5105. dev->hw_features |= features | NETIF_F_RXCSUM | NETIF_F_GRO;
  5106. dev->vlan_features |= features;
  5107. /* MTU range: 68 - 9676 */
  5108. dev->min_mtu = ETH_MIN_MTU;
  5109. /* 9676 == 9700 - 20 and rounding to 8 */
  5110. dev->max_mtu = 9676;
  5111. err = register_netdev(dev);
  5112. if (err < 0) {
  5113. dev_err(&pdev->dev, "failed to register netdev\n");
  5114. goto err_free_port_pcpu;
  5115. }
  5116. netdev_info(dev, "Using %s mac address %pM\n", mac_from, dev->dev_addr);
  5117. /* Increment the first Rx queue number to be used by the next port */
  5118. *next_first_rxq += rxq_number;
  5119. priv->port_list[id] = port;
  5120. return 0;
  5121. err_free_port_pcpu:
  5122. free_percpu(port->pcpu);
  5123. err_free_txq_pcpu:
  5124. for (i = 0; i < txq_number; i++)
  5125. free_percpu(port->txqs[i]->pcpu);
  5126. err_free_stats:
  5127. free_percpu(port->stats);
  5128. err_free_irq:
  5129. irq_dispose_mapping(port->irq);
  5130. err_free_netdev:
  5131. of_node_put(phy_node);
  5132. free_netdev(dev);
  5133. return err;
  5134. }
  5135. /* Ports removal routine */
  5136. static void mvpp2_port_remove(struct mvpp2_port *port)
  5137. {
  5138. int i;
  5139. unregister_netdev(port->dev);
  5140. of_node_put(port->phy_node);
  5141. free_percpu(port->pcpu);
  5142. free_percpu(port->stats);
  5143. for (i = 0; i < txq_number; i++)
  5144. free_percpu(port->txqs[i]->pcpu);
  5145. irq_dispose_mapping(port->irq);
  5146. free_netdev(port->dev);
  5147. }
  5148. /* Initialize decoding windows */
  5149. static void mvpp2_conf_mbus_windows(const struct mbus_dram_target_info *dram,
  5150. struct mvpp2 *priv)
  5151. {
  5152. u32 win_enable;
  5153. int i;
  5154. for (i = 0; i < 6; i++) {
  5155. mvpp2_write(priv, MVPP2_WIN_BASE(i), 0);
  5156. mvpp2_write(priv, MVPP2_WIN_SIZE(i), 0);
  5157. if (i < 4)
  5158. mvpp2_write(priv, MVPP2_WIN_REMAP(i), 0);
  5159. }
  5160. win_enable = 0;
  5161. for (i = 0; i < dram->num_cs; i++) {
  5162. const struct mbus_dram_window *cs = dram->cs + i;
  5163. mvpp2_write(priv, MVPP2_WIN_BASE(i),
  5164. (cs->base & 0xffff0000) | (cs->mbus_attr << 8) |
  5165. dram->mbus_dram_target_id);
  5166. mvpp2_write(priv, MVPP2_WIN_SIZE(i),
  5167. (cs->size - 1) & 0xffff0000);
  5168. win_enable |= (1 << i);
  5169. }
  5170. mvpp2_write(priv, MVPP2_BASE_ADDR_ENABLE, win_enable);
  5171. }
  5172. /* Initialize Rx FIFO's */
  5173. static void mvpp2_rx_fifo_init(struct mvpp2 *priv)
  5174. {
  5175. int port;
  5176. for (port = 0; port < MVPP2_MAX_PORTS; port++) {
  5177. mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
  5178. MVPP2_RX_FIFO_PORT_DATA_SIZE);
  5179. mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
  5180. MVPP2_RX_FIFO_PORT_ATTR_SIZE);
  5181. }
  5182. mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
  5183. MVPP2_RX_FIFO_PORT_MIN_PKT);
  5184. mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
  5185. }
  5186. /* Initialize network controller common part HW */
  5187. static int mvpp2_init(struct platform_device *pdev, struct mvpp2 *priv)
  5188. {
  5189. const struct mbus_dram_target_info *dram_target_info;
  5190. int err, i;
  5191. u32 val;
  5192. /* Checks for hardware constraints */
  5193. if (rxq_number % 4 || (rxq_number > MVPP2_MAX_RXQ) ||
  5194. (txq_number > MVPP2_MAX_TXQ)) {
  5195. dev_err(&pdev->dev, "invalid queue size parameter\n");
  5196. return -EINVAL;
  5197. }
  5198. /* MBUS windows configuration */
  5199. dram_target_info = mv_mbus_dram_info();
  5200. if (dram_target_info)
  5201. mvpp2_conf_mbus_windows(dram_target_info, priv);
  5202. /* Disable HW PHY polling */
  5203. val = readl(priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
  5204. val |= MVPP2_PHY_AN_STOP_SMI0_MASK;
  5205. writel(val, priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
  5206. /* Allocate and initialize aggregated TXQs */
  5207. priv->aggr_txqs = devm_kcalloc(&pdev->dev, num_present_cpus(),
  5208. sizeof(struct mvpp2_tx_queue),
  5209. GFP_KERNEL);
  5210. if (!priv->aggr_txqs)
  5211. return -ENOMEM;
  5212. for_each_present_cpu(i) {
  5213. priv->aggr_txqs[i].id = i;
  5214. priv->aggr_txqs[i].size = MVPP2_AGGR_TXQ_SIZE;
  5215. err = mvpp2_aggr_txq_init(pdev, &priv->aggr_txqs[i],
  5216. MVPP2_AGGR_TXQ_SIZE, i, priv);
  5217. if (err < 0)
  5218. return err;
  5219. }
  5220. /* Rx Fifo Init */
  5221. mvpp2_rx_fifo_init(priv);
  5222. /* Reset Rx queue group interrupt configuration */
  5223. for (i = 0; i < MVPP2_MAX_PORTS; i++)
  5224. mvpp2_write(priv, MVPP2_ISR_RXQ_GROUP_REG(i), rxq_number);
  5225. writel(MVPP2_EXT_GLOBAL_CTRL_DEFAULT,
  5226. priv->lms_base + MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG);
  5227. /* Allow cache snoop when transmiting packets */
  5228. mvpp2_write(priv, MVPP2_TX_SNOOP_REG, 0x1);
  5229. /* Buffer Manager initialization */
  5230. err = mvpp2_bm_init(pdev, priv);
  5231. if (err < 0)
  5232. return err;
  5233. /* Parser default initialization */
  5234. err = mvpp2_prs_default_init(pdev, priv);
  5235. if (err < 0)
  5236. return err;
  5237. /* Classifier default initialization */
  5238. mvpp2_cls_init(priv);
  5239. return 0;
  5240. }
  5241. static int mvpp2_probe(struct platform_device *pdev)
  5242. {
  5243. struct device_node *dn = pdev->dev.of_node;
  5244. struct device_node *port_node;
  5245. struct mvpp2 *priv;
  5246. struct resource *res;
  5247. int port_count, first_rxq;
  5248. int err;
  5249. priv = devm_kzalloc(&pdev->dev, sizeof(struct mvpp2), GFP_KERNEL);
  5250. if (!priv)
  5251. return -ENOMEM;
  5252. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  5253. priv->base = devm_ioremap_resource(&pdev->dev, res);
  5254. if (IS_ERR(priv->base))
  5255. return PTR_ERR(priv->base);
  5256. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  5257. priv->lms_base = devm_ioremap_resource(&pdev->dev, res);
  5258. if (IS_ERR(priv->lms_base))
  5259. return PTR_ERR(priv->lms_base);
  5260. priv->pp_clk = devm_clk_get(&pdev->dev, "pp_clk");
  5261. if (IS_ERR(priv->pp_clk))
  5262. return PTR_ERR(priv->pp_clk);
  5263. err = clk_prepare_enable(priv->pp_clk);
  5264. if (err < 0)
  5265. return err;
  5266. priv->gop_clk = devm_clk_get(&pdev->dev, "gop_clk");
  5267. if (IS_ERR(priv->gop_clk)) {
  5268. err = PTR_ERR(priv->gop_clk);
  5269. goto err_pp_clk;
  5270. }
  5271. err = clk_prepare_enable(priv->gop_clk);
  5272. if (err < 0)
  5273. goto err_pp_clk;
  5274. /* Get system's tclk rate */
  5275. priv->tclk = clk_get_rate(priv->pp_clk);
  5276. /* Initialize network controller */
  5277. err = mvpp2_init(pdev, priv);
  5278. if (err < 0) {
  5279. dev_err(&pdev->dev, "failed to initialize controller\n");
  5280. goto err_gop_clk;
  5281. }
  5282. port_count = of_get_available_child_count(dn);
  5283. if (port_count == 0) {
  5284. dev_err(&pdev->dev, "no ports enabled\n");
  5285. err = -ENODEV;
  5286. goto err_gop_clk;
  5287. }
  5288. priv->port_list = devm_kcalloc(&pdev->dev, port_count,
  5289. sizeof(struct mvpp2_port *),
  5290. GFP_KERNEL);
  5291. if (!priv->port_list) {
  5292. err = -ENOMEM;
  5293. goto err_gop_clk;
  5294. }
  5295. /* Initialize ports */
  5296. first_rxq = 0;
  5297. for_each_available_child_of_node(dn, port_node) {
  5298. err = mvpp2_port_probe(pdev, port_node, priv, &first_rxq);
  5299. if (err < 0)
  5300. goto err_gop_clk;
  5301. }
  5302. platform_set_drvdata(pdev, priv);
  5303. return 0;
  5304. err_gop_clk:
  5305. clk_disable_unprepare(priv->gop_clk);
  5306. err_pp_clk:
  5307. clk_disable_unprepare(priv->pp_clk);
  5308. return err;
  5309. }
  5310. static int mvpp2_remove(struct platform_device *pdev)
  5311. {
  5312. struct mvpp2 *priv = platform_get_drvdata(pdev);
  5313. struct device_node *dn = pdev->dev.of_node;
  5314. struct device_node *port_node;
  5315. int i = 0;
  5316. for_each_available_child_of_node(dn, port_node) {
  5317. if (priv->port_list[i])
  5318. mvpp2_port_remove(priv->port_list[i]);
  5319. i++;
  5320. }
  5321. for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
  5322. struct mvpp2_bm_pool *bm_pool = &priv->bm_pools[i];
  5323. mvpp2_bm_pool_destroy(pdev, priv, bm_pool);
  5324. }
  5325. for_each_present_cpu(i) {
  5326. struct mvpp2_tx_queue *aggr_txq = &priv->aggr_txqs[i];
  5327. dma_free_coherent(&pdev->dev,
  5328. MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
  5329. aggr_txq->descs,
  5330. aggr_txq->descs_phys);
  5331. }
  5332. clk_disable_unprepare(priv->pp_clk);
  5333. clk_disable_unprepare(priv->gop_clk);
  5334. return 0;
  5335. }
  5336. static const struct of_device_id mvpp2_match[] = {
  5337. { .compatible = "marvell,armada-375-pp2" },
  5338. { }
  5339. };
  5340. MODULE_DEVICE_TABLE(of, mvpp2_match);
  5341. static struct platform_driver mvpp2_driver = {
  5342. .probe = mvpp2_probe,
  5343. .remove = mvpp2_remove,
  5344. .driver = {
  5345. .name = MVPP2_DRIVER_NAME,
  5346. .of_match_table = mvpp2_match,
  5347. },
  5348. };
  5349. module_platform_driver(mvpp2_driver);
  5350. MODULE_DESCRIPTION("Marvell PPv2 Ethernet Driver - www.marvell.com");
  5351. MODULE_AUTHOR("Marcin Wojtas <mw@semihalf.com>");
  5352. MODULE_LICENSE("GPL v2");