mvpp2.c 223 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112
  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/mfd/syscon.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/cpumask.h>
  23. #include <linux/of.h>
  24. #include <linux/of_irq.h>
  25. #include <linux/of_mdio.h>
  26. #include <linux/of_net.h>
  27. #include <linux/of_address.h>
  28. #include <linux/of_device.h>
  29. #include <linux/phy.h>
  30. #include <linux/phy/phy.h>
  31. #include <linux/clk.h>
  32. #include <linux/hrtimer.h>
  33. #include <linux/ktime.h>
  34. #include <linux/regmap.h>
  35. #include <uapi/linux/ppp_defs.h>
  36. #include <net/ip.h>
  37. #include <net/ipv6.h>
  38. #include <net/tso.h>
  39. /* RX Fifo Registers */
  40. #define MVPP2_RX_DATA_FIFO_SIZE_REG(port) (0x00 + 4 * (port))
  41. #define MVPP2_RX_ATTR_FIFO_SIZE_REG(port) (0x20 + 4 * (port))
  42. #define MVPP2_RX_MIN_PKT_SIZE_REG 0x60
  43. #define MVPP2_RX_FIFO_INIT_REG 0x64
  44. /* RX DMA Top Registers */
  45. #define MVPP2_RX_CTRL_REG(port) (0x140 + 4 * (port))
  46. #define MVPP2_RX_LOW_LATENCY_PKT_SIZE(s) (((s) & 0xfff) << 16)
  47. #define MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK BIT(31)
  48. #define MVPP2_POOL_BUF_SIZE_REG(pool) (0x180 + 4 * (pool))
  49. #define MVPP2_POOL_BUF_SIZE_OFFSET 5
  50. #define MVPP2_RXQ_CONFIG_REG(rxq) (0x800 + 4 * (rxq))
  51. #define MVPP2_SNOOP_PKT_SIZE_MASK 0x1ff
  52. #define MVPP2_SNOOP_BUF_HDR_MASK BIT(9)
  53. #define MVPP2_RXQ_POOL_SHORT_OFFS 20
  54. #define MVPP21_RXQ_POOL_SHORT_MASK 0x700000
  55. #define MVPP22_RXQ_POOL_SHORT_MASK 0xf00000
  56. #define MVPP2_RXQ_POOL_LONG_OFFS 24
  57. #define MVPP21_RXQ_POOL_LONG_MASK 0x7000000
  58. #define MVPP22_RXQ_POOL_LONG_MASK 0xf000000
  59. #define MVPP2_RXQ_PACKET_OFFSET_OFFS 28
  60. #define MVPP2_RXQ_PACKET_OFFSET_MASK 0x70000000
  61. #define MVPP2_RXQ_DISABLE_MASK BIT(31)
  62. /* Parser Registers */
  63. #define MVPP2_PRS_INIT_LOOKUP_REG 0x1000
  64. #define MVPP2_PRS_PORT_LU_MAX 0xf
  65. #define MVPP2_PRS_PORT_LU_MASK(port) (0xff << ((port) * 4))
  66. #define MVPP2_PRS_PORT_LU_VAL(port, val) ((val) << ((port) * 4))
  67. #define MVPP2_PRS_INIT_OFFS_REG(port) (0x1004 + ((port) & 4))
  68. #define MVPP2_PRS_INIT_OFF_MASK(port) (0x3f << (((port) % 4) * 8))
  69. #define MVPP2_PRS_INIT_OFF_VAL(port, val) ((val) << (((port) % 4) * 8))
  70. #define MVPP2_PRS_MAX_LOOP_REG(port) (0x100c + ((port) & 4))
  71. #define MVPP2_PRS_MAX_LOOP_MASK(port) (0xff << (((port) % 4) * 8))
  72. #define MVPP2_PRS_MAX_LOOP_VAL(port, val) ((val) << (((port) % 4) * 8))
  73. #define MVPP2_PRS_TCAM_IDX_REG 0x1100
  74. #define MVPP2_PRS_TCAM_DATA_REG(idx) (0x1104 + (idx) * 4)
  75. #define MVPP2_PRS_TCAM_INV_MASK BIT(31)
  76. #define MVPP2_PRS_SRAM_IDX_REG 0x1200
  77. #define MVPP2_PRS_SRAM_DATA_REG(idx) (0x1204 + (idx) * 4)
  78. #define MVPP2_PRS_TCAM_CTRL_REG 0x1230
  79. #define MVPP2_PRS_TCAM_EN_MASK BIT(0)
  80. /* Classifier Registers */
  81. #define MVPP2_CLS_MODE_REG 0x1800
  82. #define MVPP2_CLS_MODE_ACTIVE_MASK BIT(0)
  83. #define MVPP2_CLS_PORT_WAY_REG 0x1810
  84. #define MVPP2_CLS_PORT_WAY_MASK(port) (1 << (port))
  85. #define MVPP2_CLS_LKP_INDEX_REG 0x1814
  86. #define MVPP2_CLS_LKP_INDEX_WAY_OFFS 6
  87. #define MVPP2_CLS_LKP_TBL_REG 0x1818
  88. #define MVPP2_CLS_LKP_TBL_RXQ_MASK 0xff
  89. #define MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK BIT(25)
  90. #define MVPP2_CLS_FLOW_INDEX_REG 0x1820
  91. #define MVPP2_CLS_FLOW_TBL0_REG 0x1824
  92. #define MVPP2_CLS_FLOW_TBL1_REG 0x1828
  93. #define MVPP2_CLS_FLOW_TBL2_REG 0x182c
  94. #define MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port) (0x1980 + ((port) * 4))
  95. #define MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS 3
  96. #define MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK 0x7
  97. #define MVPP2_CLS_SWFWD_P2HQ_REG(port) (0x19b0 + ((port) * 4))
  98. #define MVPP2_CLS_SWFWD_PCTRL_REG 0x19d0
  99. #define MVPP2_CLS_SWFWD_PCTRL_MASK(port) (1 << (port))
  100. /* Descriptor Manager Top Registers */
  101. #define MVPP2_RXQ_NUM_REG 0x2040
  102. #define MVPP2_RXQ_DESC_ADDR_REG 0x2044
  103. #define MVPP22_DESC_ADDR_OFFS 8
  104. #define MVPP2_RXQ_DESC_SIZE_REG 0x2048
  105. #define MVPP2_RXQ_DESC_SIZE_MASK 0x3ff0
  106. #define MVPP2_RXQ_STATUS_UPDATE_REG(rxq) (0x3000 + 4 * (rxq))
  107. #define MVPP2_RXQ_NUM_PROCESSED_OFFSET 0
  108. #define MVPP2_RXQ_NUM_NEW_OFFSET 16
  109. #define MVPP2_RXQ_STATUS_REG(rxq) (0x3400 + 4 * (rxq))
  110. #define MVPP2_RXQ_OCCUPIED_MASK 0x3fff
  111. #define MVPP2_RXQ_NON_OCCUPIED_OFFSET 16
  112. #define MVPP2_RXQ_NON_OCCUPIED_MASK 0x3fff0000
  113. #define MVPP2_RXQ_THRESH_REG 0x204c
  114. #define MVPP2_OCCUPIED_THRESH_OFFSET 0
  115. #define MVPP2_OCCUPIED_THRESH_MASK 0x3fff
  116. #define MVPP2_RXQ_INDEX_REG 0x2050
  117. #define MVPP2_TXQ_NUM_REG 0x2080
  118. #define MVPP2_TXQ_DESC_ADDR_REG 0x2084
  119. #define MVPP2_TXQ_DESC_SIZE_REG 0x2088
  120. #define MVPP2_TXQ_DESC_SIZE_MASK 0x3ff0
  121. #define MVPP2_TXQ_THRESH_REG 0x2094
  122. #define MVPP2_TXQ_THRESH_OFFSET 16
  123. #define MVPP2_TXQ_THRESH_MASK 0x3fff
  124. #define MVPP2_AGGR_TXQ_UPDATE_REG 0x2090
  125. #define MVPP2_TXQ_INDEX_REG 0x2098
  126. #define MVPP2_TXQ_PREF_BUF_REG 0x209c
  127. #define MVPP2_PREF_BUF_PTR(desc) ((desc) & 0xfff)
  128. #define MVPP2_PREF_BUF_SIZE_4 (BIT(12) | BIT(13))
  129. #define MVPP2_PREF_BUF_SIZE_16 (BIT(12) | BIT(14))
  130. #define MVPP2_PREF_BUF_THRESH(val) ((val) << 17)
  131. #define MVPP2_TXQ_DRAIN_EN_MASK BIT(31)
  132. #define MVPP2_TXQ_PENDING_REG 0x20a0
  133. #define MVPP2_TXQ_PENDING_MASK 0x3fff
  134. #define MVPP2_TXQ_INT_STATUS_REG 0x20a4
  135. #define MVPP2_TXQ_SENT_REG(txq) (0x3c00 + 4 * (txq))
  136. #define MVPP2_TRANSMITTED_COUNT_OFFSET 16
  137. #define MVPP2_TRANSMITTED_COUNT_MASK 0x3fff0000
  138. #define MVPP2_TXQ_RSVD_REQ_REG 0x20b0
  139. #define MVPP2_TXQ_RSVD_REQ_Q_OFFSET 16
  140. #define MVPP2_TXQ_RSVD_RSLT_REG 0x20b4
  141. #define MVPP2_TXQ_RSVD_RSLT_MASK 0x3fff
  142. #define MVPP2_TXQ_RSVD_CLR_REG 0x20b8
  143. #define MVPP2_TXQ_RSVD_CLR_OFFSET 16
  144. #define MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu) (0x2100 + 4 * (cpu))
  145. #define MVPP22_AGGR_TXQ_DESC_ADDR_OFFS 8
  146. #define MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu) (0x2140 + 4 * (cpu))
  147. #define MVPP2_AGGR_TXQ_DESC_SIZE_MASK 0x3ff0
  148. #define MVPP2_AGGR_TXQ_STATUS_REG(cpu) (0x2180 + 4 * (cpu))
  149. #define MVPP2_AGGR_TXQ_PENDING_MASK 0x3fff
  150. #define MVPP2_AGGR_TXQ_INDEX_REG(cpu) (0x21c0 + 4 * (cpu))
  151. /* MBUS bridge registers */
  152. #define MVPP2_WIN_BASE(w) (0x4000 + ((w) << 2))
  153. #define MVPP2_WIN_SIZE(w) (0x4020 + ((w) << 2))
  154. #define MVPP2_WIN_REMAP(w) (0x4040 + ((w) << 2))
  155. #define MVPP2_BASE_ADDR_ENABLE 0x4060
  156. /* AXI Bridge Registers */
  157. #define MVPP22_AXI_BM_WR_ATTR_REG 0x4100
  158. #define MVPP22_AXI_BM_RD_ATTR_REG 0x4104
  159. #define MVPP22_AXI_AGGRQ_DESCR_RD_ATTR_REG 0x4110
  160. #define MVPP22_AXI_TXQ_DESCR_WR_ATTR_REG 0x4114
  161. #define MVPP22_AXI_TXQ_DESCR_RD_ATTR_REG 0x4118
  162. #define MVPP22_AXI_RXQ_DESCR_WR_ATTR_REG 0x411c
  163. #define MVPP22_AXI_RX_DATA_WR_ATTR_REG 0x4120
  164. #define MVPP22_AXI_TX_DATA_RD_ATTR_REG 0x4130
  165. #define MVPP22_AXI_RD_NORMAL_CODE_REG 0x4150
  166. #define MVPP22_AXI_RD_SNOOP_CODE_REG 0x4154
  167. #define MVPP22_AXI_WR_NORMAL_CODE_REG 0x4160
  168. #define MVPP22_AXI_WR_SNOOP_CODE_REG 0x4164
  169. /* Values for AXI Bridge registers */
  170. #define MVPP22_AXI_ATTR_CACHE_OFFS 0
  171. #define MVPP22_AXI_ATTR_DOMAIN_OFFS 12
  172. #define MVPP22_AXI_CODE_CACHE_OFFS 0
  173. #define MVPP22_AXI_CODE_DOMAIN_OFFS 4
  174. #define MVPP22_AXI_CODE_CACHE_NON_CACHE 0x3
  175. #define MVPP22_AXI_CODE_CACHE_WR_CACHE 0x7
  176. #define MVPP22_AXI_CODE_CACHE_RD_CACHE 0xb
  177. #define MVPP22_AXI_CODE_DOMAIN_OUTER_DOM 2
  178. #define MVPP22_AXI_CODE_DOMAIN_SYSTEM 3
  179. /* Interrupt Cause and Mask registers */
  180. #define MVPP2_ISR_TX_THRESHOLD_REG(port) (0x5140 + 4 * (port))
  181. #define MVPP2_MAX_ISR_TX_THRESHOLD 0xfffff0
  182. #define MVPP2_ISR_RX_THRESHOLD_REG(rxq) (0x5200 + 4 * (rxq))
  183. #define MVPP2_MAX_ISR_RX_THRESHOLD 0xfffff0
  184. #define MVPP21_ISR_RXQ_GROUP_REG(port) (0x5400 + 4 * (port))
  185. #define MVPP22_ISR_RXQ_GROUP_INDEX_REG 0x5400
  186. #define MVPP22_ISR_RXQ_GROUP_INDEX_SUBGROUP_MASK 0xf
  187. #define MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_MASK 0x380
  188. #define MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET 7
  189. #define MVPP22_ISR_RXQ_GROUP_INDEX_SUBGROUP_MASK 0xf
  190. #define MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_MASK 0x380
  191. #define MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG 0x5404
  192. #define MVPP22_ISR_RXQ_SUB_GROUP_STARTQ_MASK 0x1f
  193. #define MVPP22_ISR_RXQ_SUB_GROUP_SIZE_MASK 0xf00
  194. #define MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET 8
  195. #define MVPP2_ISR_ENABLE_REG(port) (0x5420 + 4 * (port))
  196. #define MVPP2_ISR_ENABLE_INTERRUPT(mask) ((mask) & 0xffff)
  197. #define MVPP2_ISR_DISABLE_INTERRUPT(mask) (((mask) << 16) & 0xffff0000)
  198. #define MVPP2_ISR_RX_TX_CAUSE_REG(port) (0x5480 + 4 * (port))
  199. #define MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK 0xffff
  200. #define MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK 0xff0000
  201. #define MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_OFFSET 16
  202. #define MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK BIT(24)
  203. #define MVPP2_CAUSE_FCS_ERR_MASK BIT(25)
  204. #define MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK BIT(26)
  205. #define MVPP2_CAUSE_TX_EXCEPTION_SUM_MASK BIT(29)
  206. #define MVPP2_CAUSE_RX_EXCEPTION_SUM_MASK BIT(30)
  207. #define MVPP2_CAUSE_MISC_SUM_MASK BIT(31)
  208. #define MVPP2_ISR_RX_TX_MASK_REG(port) (0x54a0 + 4 * (port))
  209. #define MVPP2_ISR_PON_RX_TX_MASK_REG 0x54bc
  210. #define MVPP2_PON_CAUSE_RXQ_OCCUP_DESC_ALL_MASK 0xffff
  211. #define MVPP2_PON_CAUSE_TXP_OCCUP_DESC_ALL_MASK 0x3fc00000
  212. #define MVPP2_PON_CAUSE_MISC_SUM_MASK BIT(31)
  213. #define MVPP2_ISR_MISC_CAUSE_REG 0x55b0
  214. /* Buffer Manager registers */
  215. #define MVPP2_BM_POOL_BASE_REG(pool) (0x6000 + ((pool) * 4))
  216. #define MVPP2_BM_POOL_BASE_ADDR_MASK 0xfffff80
  217. #define MVPP2_BM_POOL_SIZE_REG(pool) (0x6040 + ((pool) * 4))
  218. #define MVPP2_BM_POOL_SIZE_MASK 0xfff0
  219. #define MVPP2_BM_POOL_READ_PTR_REG(pool) (0x6080 + ((pool) * 4))
  220. #define MVPP2_BM_POOL_GET_READ_PTR_MASK 0xfff0
  221. #define MVPP2_BM_POOL_PTRS_NUM_REG(pool) (0x60c0 + ((pool) * 4))
  222. #define MVPP2_BM_POOL_PTRS_NUM_MASK 0xfff0
  223. #define MVPP2_BM_BPPI_READ_PTR_REG(pool) (0x6100 + ((pool) * 4))
  224. #define MVPP2_BM_BPPI_PTRS_NUM_REG(pool) (0x6140 + ((pool) * 4))
  225. #define MVPP2_BM_BPPI_PTR_NUM_MASK 0x7ff
  226. #define MVPP2_BM_BPPI_PREFETCH_FULL_MASK BIT(16)
  227. #define MVPP2_BM_POOL_CTRL_REG(pool) (0x6200 + ((pool) * 4))
  228. #define MVPP2_BM_START_MASK BIT(0)
  229. #define MVPP2_BM_STOP_MASK BIT(1)
  230. #define MVPP2_BM_STATE_MASK BIT(4)
  231. #define MVPP2_BM_LOW_THRESH_OFFS 8
  232. #define MVPP2_BM_LOW_THRESH_MASK 0x7f00
  233. #define MVPP2_BM_LOW_THRESH_VALUE(val) ((val) << \
  234. MVPP2_BM_LOW_THRESH_OFFS)
  235. #define MVPP2_BM_HIGH_THRESH_OFFS 16
  236. #define MVPP2_BM_HIGH_THRESH_MASK 0x7f0000
  237. #define MVPP2_BM_HIGH_THRESH_VALUE(val) ((val) << \
  238. MVPP2_BM_HIGH_THRESH_OFFS)
  239. #define MVPP2_BM_INTR_CAUSE_REG(pool) (0x6240 + ((pool) * 4))
  240. #define MVPP2_BM_RELEASED_DELAY_MASK BIT(0)
  241. #define MVPP2_BM_ALLOC_FAILED_MASK BIT(1)
  242. #define MVPP2_BM_BPPE_EMPTY_MASK BIT(2)
  243. #define MVPP2_BM_BPPE_FULL_MASK BIT(3)
  244. #define MVPP2_BM_AVAILABLE_BP_LOW_MASK BIT(4)
  245. #define MVPP2_BM_INTR_MASK_REG(pool) (0x6280 + ((pool) * 4))
  246. #define MVPP2_BM_PHY_ALLOC_REG(pool) (0x6400 + ((pool) * 4))
  247. #define MVPP2_BM_PHY_ALLOC_GRNTD_MASK BIT(0)
  248. #define MVPP2_BM_VIRT_ALLOC_REG 0x6440
  249. #define MVPP22_BM_ADDR_HIGH_ALLOC 0x6444
  250. #define MVPP22_BM_ADDR_HIGH_PHYS_MASK 0xff
  251. #define MVPP22_BM_ADDR_HIGH_VIRT_MASK 0xff00
  252. #define MVPP22_BM_ADDR_HIGH_VIRT_SHIFT 8
  253. #define MVPP2_BM_PHY_RLS_REG(pool) (0x6480 + ((pool) * 4))
  254. #define MVPP2_BM_PHY_RLS_MC_BUFF_MASK BIT(0)
  255. #define MVPP2_BM_PHY_RLS_PRIO_EN_MASK BIT(1)
  256. #define MVPP2_BM_PHY_RLS_GRNTD_MASK BIT(2)
  257. #define MVPP2_BM_VIRT_RLS_REG 0x64c0
  258. #define MVPP22_BM_ADDR_HIGH_RLS_REG 0x64c4
  259. #define MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK 0xff
  260. #define MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK 0xff00
  261. #define MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT 8
  262. /* TX Scheduler registers */
  263. #define MVPP2_TXP_SCHED_PORT_INDEX_REG 0x8000
  264. #define MVPP2_TXP_SCHED_Q_CMD_REG 0x8004
  265. #define MVPP2_TXP_SCHED_ENQ_MASK 0xff
  266. #define MVPP2_TXP_SCHED_DISQ_OFFSET 8
  267. #define MVPP2_TXP_SCHED_CMD_1_REG 0x8010
  268. #define MVPP2_TXP_SCHED_PERIOD_REG 0x8018
  269. #define MVPP2_TXP_SCHED_MTU_REG 0x801c
  270. #define MVPP2_TXP_MTU_MAX 0x7FFFF
  271. #define MVPP2_TXP_SCHED_REFILL_REG 0x8020
  272. #define MVPP2_TXP_REFILL_TOKENS_ALL_MASK 0x7ffff
  273. #define MVPP2_TXP_REFILL_PERIOD_ALL_MASK 0x3ff00000
  274. #define MVPP2_TXP_REFILL_PERIOD_MASK(v) ((v) << 20)
  275. #define MVPP2_TXP_SCHED_TOKEN_SIZE_REG 0x8024
  276. #define MVPP2_TXP_TOKEN_SIZE_MAX 0xffffffff
  277. #define MVPP2_TXQ_SCHED_REFILL_REG(q) (0x8040 + ((q) << 2))
  278. #define MVPP2_TXQ_REFILL_TOKENS_ALL_MASK 0x7ffff
  279. #define MVPP2_TXQ_REFILL_PERIOD_ALL_MASK 0x3ff00000
  280. #define MVPP2_TXQ_REFILL_PERIOD_MASK(v) ((v) << 20)
  281. #define MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(q) (0x8060 + ((q) << 2))
  282. #define MVPP2_TXQ_TOKEN_SIZE_MAX 0x7fffffff
  283. #define MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(q) (0x8080 + ((q) << 2))
  284. #define MVPP2_TXQ_TOKEN_CNTR_MAX 0xffffffff
  285. /* TX general registers */
  286. #define MVPP2_TX_SNOOP_REG 0x8800
  287. #define MVPP2_TX_PORT_FLUSH_REG 0x8810
  288. #define MVPP2_TX_PORT_FLUSH_MASK(port) (1 << (port))
  289. /* LMS registers */
  290. #define MVPP2_SRC_ADDR_MIDDLE 0x24
  291. #define MVPP2_SRC_ADDR_HIGH 0x28
  292. #define MVPP2_PHY_AN_CFG0_REG 0x34
  293. #define MVPP2_PHY_AN_STOP_SMI0_MASK BIT(7)
  294. #define MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG 0x305c
  295. #define MVPP2_EXT_GLOBAL_CTRL_DEFAULT 0x27
  296. /* Per-port registers */
  297. #define MVPP2_GMAC_CTRL_0_REG 0x0
  298. #define MVPP2_GMAC_PORT_EN_MASK BIT(0)
  299. #define MVPP2_GMAC_PORT_TYPE_MASK BIT(1)
  300. #define MVPP2_GMAC_MAX_RX_SIZE_OFFS 2
  301. #define MVPP2_GMAC_MAX_RX_SIZE_MASK 0x7ffc
  302. #define MVPP2_GMAC_MIB_CNTR_EN_MASK BIT(15)
  303. #define MVPP2_GMAC_CTRL_1_REG 0x4
  304. #define MVPP2_GMAC_PERIODIC_XON_EN_MASK BIT(1)
  305. #define MVPP2_GMAC_GMII_LB_EN_MASK BIT(5)
  306. #define MVPP2_GMAC_PCS_LB_EN_BIT 6
  307. #define MVPP2_GMAC_PCS_LB_EN_MASK BIT(6)
  308. #define MVPP2_GMAC_SA_LOW_OFFS 7
  309. #define MVPP2_GMAC_CTRL_2_REG 0x8
  310. #define MVPP2_GMAC_INBAND_AN_MASK BIT(0)
  311. #define MVPP2_GMAC_FLOW_CTRL_MASK GENMASK(2, 1)
  312. #define MVPP2_GMAC_PCS_ENABLE_MASK BIT(3)
  313. #define MVPP2_GMAC_INTERNAL_CLK_MASK BIT(4)
  314. #define MVPP2_GMAC_DISABLE_PADDING BIT(5)
  315. #define MVPP2_GMAC_PORT_RESET_MASK BIT(6)
  316. #define MVPP2_GMAC_AUTONEG_CONFIG 0xc
  317. #define MVPP2_GMAC_FORCE_LINK_DOWN BIT(0)
  318. #define MVPP2_GMAC_FORCE_LINK_PASS BIT(1)
  319. #define MVPP2_GMAC_IN_BAND_AUTONEG BIT(2)
  320. #define MVPP2_GMAC_IN_BAND_AUTONEG_BYPASS BIT(3)
  321. #define MVPP2_GMAC_CONFIG_MII_SPEED BIT(5)
  322. #define MVPP2_GMAC_CONFIG_GMII_SPEED BIT(6)
  323. #define MVPP2_GMAC_AN_SPEED_EN BIT(7)
  324. #define MVPP2_GMAC_FC_ADV_EN BIT(9)
  325. #define MVPP2_GMAC_FLOW_CTRL_AUTONEG BIT(11)
  326. #define MVPP2_GMAC_CONFIG_FULL_DUPLEX BIT(12)
  327. #define MVPP2_GMAC_AN_DUPLEX_EN BIT(13)
  328. #define MVPP2_GMAC_STATUS0 0x10
  329. #define MVPP2_GMAC_STATUS0_LINK_UP BIT(0)
  330. #define MVPP2_GMAC_PORT_FIFO_CFG_1_REG 0x1c
  331. #define MVPP2_GMAC_TX_FIFO_MIN_TH_OFFS 6
  332. #define MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK 0x1fc0
  333. #define MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(v) (((v) << 6) & \
  334. MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK)
  335. #define MVPP22_GMAC_INT_STAT 0x20
  336. #define MVPP22_GMAC_INT_STAT_LINK BIT(1)
  337. #define MVPP22_GMAC_INT_MASK 0x24
  338. #define MVPP22_GMAC_INT_MASK_LINK_STAT BIT(1)
  339. #define MVPP22_GMAC_CTRL_4_REG 0x90
  340. #define MVPP22_CTRL4_EXT_PIN_GMII_SEL BIT(0)
  341. #define MVPP22_CTRL4_DP_CLK_SEL BIT(5)
  342. #define MVPP22_CTRL4_SYNC_BYPASS_DIS BIT(6)
  343. #define MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE BIT(7)
  344. #define MVPP22_GMAC_INT_SUM_MASK 0xa4
  345. #define MVPP22_GMAC_INT_SUM_MASK_LINK_STAT BIT(1)
  346. /* Per-port XGMAC registers. PPv2.2 only, only for GOP port 0,
  347. * relative to port->base.
  348. */
  349. #define MVPP22_XLG_CTRL0_REG 0x100
  350. #define MVPP22_XLG_CTRL0_PORT_EN BIT(0)
  351. #define MVPP22_XLG_CTRL0_MAC_RESET_DIS BIT(1)
  352. #define MVPP22_XLG_CTRL0_RX_FLOW_CTRL_EN BIT(7)
  353. #define MVPP22_XLG_CTRL0_MIB_CNT_DIS BIT(14)
  354. #define MVPP22_XLG_CTRL1_REG 0x104
  355. #define MVPP22_XLG_CTRL1_FRAMESIZELIMIT_OFFS 0
  356. #define MVPP22_XLG_CTRL1_FRAMESIZELIMIT_MASK 0x1fff
  357. #define MVPP22_XLG_STATUS 0x10c
  358. #define MVPP22_XLG_STATUS_LINK_UP BIT(0)
  359. #define MVPP22_XLG_INT_STAT 0x114
  360. #define MVPP22_XLG_INT_STAT_LINK BIT(1)
  361. #define MVPP22_XLG_INT_MASK 0x118
  362. #define MVPP22_XLG_INT_MASK_LINK BIT(1)
  363. #define MVPP22_XLG_CTRL3_REG 0x11c
  364. #define MVPP22_XLG_CTRL3_MACMODESELECT_MASK (7 << 13)
  365. #define MVPP22_XLG_CTRL3_MACMODESELECT_GMAC (0 << 13)
  366. #define MVPP22_XLG_CTRL3_MACMODESELECT_10G (1 << 13)
  367. #define MVPP22_XLG_EXT_INT_MASK 0x15c
  368. #define MVPP22_XLG_EXT_INT_MASK_XLG BIT(1)
  369. #define MVPP22_XLG_EXT_INT_MASK_GIG BIT(2)
  370. #define MVPP22_XLG_CTRL4_REG 0x184
  371. #define MVPP22_XLG_CTRL4_FWD_FC BIT(5)
  372. #define MVPP22_XLG_CTRL4_FWD_PFC BIT(6)
  373. #define MVPP22_XLG_CTRL4_MACMODSELECT_GMAC BIT(12)
  374. /* SMI registers. PPv2.2 only, relative to priv->iface_base. */
  375. #define MVPP22_SMI_MISC_CFG_REG 0x1204
  376. #define MVPP22_SMI_POLLING_EN BIT(10)
  377. #define MVPP22_GMAC_BASE(port) (0x7000 + (port) * 0x1000 + 0xe00)
  378. #define MVPP2_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff
  379. /* Descriptor ring Macros */
  380. #define MVPP2_QUEUE_NEXT_DESC(q, index) \
  381. (((index) < (q)->last_desc) ? ((index) + 1) : 0)
  382. /* XPCS registers. PPv2.2 only */
  383. #define MVPP22_MPCS_BASE(port) (0x7000 + (port) * 0x1000)
  384. #define MVPP22_MPCS_CTRL 0x14
  385. #define MVPP22_MPCS_CTRL_FWD_ERR_CONN BIT(10)
  386. #define MVPP22_MPCS_CLK_RESET 0x14c
  387. #define MAC_CLK_RESET_SD_TX BIT(0)
  388. #define MAC_CLK_RESET_SD_RX BIT(1)
  389. #define MAC_CLK_RESET_MAC BIT(2)
  390. #define MVPP22_MPCS_CLK_RESET_DIV_RATIO(n) ((n) << 4)
  391. #define MVPP22_MPCS_CLK_RESET_DIV_SET BIT(11)
  392. /* XPCS registers. PPv2.2 only */
  393. #define MVPP22_XPCS_BASE(port) (0x7400 + (port) * 0x1000)
  394. #define MVPP22_XPCS_CFG0 0x0
  395. #define MVPP22_XPCS_CFG0_PCS_MODE(n) ((n) << 3)
  396. #define MVPP22_XPCS_CFG0_ACTIVE_LANE(n) ((n) << 5)
  397. /* System controller registers. Accessed through a regmap. */
  398. #define GENCONF_SOFT_RESET1 0x1108
  399. #define GENCONF_SOFT_RESET1_GOP BIT(6)
  400. #define GENCONF_PORT_CTRL0 0x1110
  401. #define GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT BIT(1)
  402. #define GENCONF_PORT_CTRL0_RX_DATA_SAMPLE BIT(29)
  403. #define GENCONF_PORT_CTRL0_CLK_DIV_PHASE_CLR BIT(31)
  404. #define GENCONF_PORT_CTRL1 0x1114
  405. #define GENCONF_PORT_CTRL1_EN(p) BIT(p)
  406. #define GENCONF_PORT_CTRL1_RESET(p) (BIT(p) << 28)
  407. #define GENCONF_CTRL0 0x1120
  408. #define GENCONF_CTRL0_PORT0_RGMII BIT(0)
  409. #define GENCONF_CTRL0_PORT1_RGMII_MII BIT(1)
  410. #define GENCONF_CTRL0_PORT1_RGMII BIT(2)
  411. /* Various constants */
  412. /* Coalescing */
  413. #define MVPP2_TXDONE_COAL_PKTS_THRESH 15
  414. #define MVPP2_TXDONE_HRTIMER_PERIOD_NS 1000000UL
  415. #define MVPP2_TXDONE_COAL_USEC 1000
  416. #define MVPP2_RX_COAL_PKTS 32
  417. #define MVPP2_RX_COAL_USEC 100
  418. /* The two bytes Marvell header. Either contains a special value used
  419. * by Marvell switches when a specific hardware mode is enabled (not
  420. * supported by this driver) or is filled automatically by zeroes on
  421. * the RX side. Those two bytes being at the front of the Ethernet
  422. * header, they allow to have the IP header aligned on a 4 bytes
  423. * boundary automatically: the hardware skips those two bytes on its
  424. * own.
  425. */
  426. #define MVPP2_MH_SIZE 2
  427. #define MVPP2_ETH_TYPE_LEN 2
  428. #define MVPP2_PPPOE_HDR_SIZE 8
  429. #define MVPP2_VLAN_TAG_LEN 4
  430. /* Lbtd 802.3 type */
  431. #define MVPP2_IP_LBDT_TYPE 0xfffa
  432. #define MVPP2_TX_CSUM_MAX_SIZE 9800
  433. /* Timeout constants */
  434. #define MVPP2_TX_DISABLE_TIMEOUT_MSEC 1000
  435. #define MVPP2_TX_PENDING_TIMEOUT_MSEC 1000
  436. #define MVPP2_TX_MTU_MAX 0x7ffff
  437. /* Maximum number of T-CONTs of PON port */
  438. #define MVPP2_MAX_TCONT 16
  439. /* Maximum number of supported ports */
  440. #define MVPP2_MAX_PORTS 4
  441. /* Maximum number of TXQs used by single port */
  442. #define MVPP2_MAX_TXQ 8
  443. /* Dfault number of RXQs in use */
  444. #define MVPP2_DEFAULT_RXQ 4
  445. /* Max number of Rx descriptors */
  446. #define MVPP2_MAX_RXD 128
  447. /* Max number of Tx descriptors */
  448. #define MVPP2_MAX_TXD 1024
  449. /* Amount of Tx descriptors that can be reserved at once by CPU */
  450. #define MVPP2_CPU_DESC_CHUNK 64
  451. /* Max number of Tx descriptors in each aggregated queue */
  452. #define MVPP2_AGGR_TXQ_SIZE 256
  453. /* Descriptor aligned size */
  454. #define MVPP2_DESC_ALIGNED_SIZE 32
  455. /* Descriptor alignment mask */
  456. #define MVPP2_TX_DESC_ALIGN (MVPP2_DESC_ALIGNED_SIZE - 1)
  457. /* RX FIFO constants */
  458. #define MVPP2_RX_FIFO_PORT_DATA_SIZE 0x2000
  459. #define MVPP2_RX_FIFO_PORT_ATTR_SIZE 0x80
  460. #define MVPP2_RX_FIFO_PORT_MIN_PKT 0x80
  461. /* RX buffer constants */
  462. #define MVPP2_SKB_SHINFO_SIZE \
  463. SKB_DATA_ALIGN(sizeof(struct skb_shared_info))
  464. #define MVPP2_RX_PKT_SIZE(mtu) \
  465. ALIGN((mtu) + MVPP2_MH_SIZE + MVPP2_VLAN_TAG_LEN + \
  466. ETH_HLEN + ETH_FCS_LEN, cache_line_size())
  467. #define MVPP2_RX_BUF_SIZE(pkt_size) ((pkt_size) + NET_SKB_PAD)
  468. #define MVPP2_RX_TOTAL_SIZE(buf_size) ((buf_size) + MVPP2_SKB_SHINFO_SIZE)
  469. #define MVPP2_RX_MAX_PKT_SIZE(total_size) \
  470. ((total_size) - NET_SKB_PAD - MVPP2_SKB_SHINFO_SIZE)
  471. #define MVPP2_BIT_TO_BYTE(bit) ((bit) / 8)
  472. /* IPv6 max L3 address size */
  473. #define MVPP2_MAX_L3_ADDR_SIZE 16
  474. /* Port flags */
  475. #define MVPP2_F_LOOPBACK BIT(0)
  476. /* Marvell tag types */
  477. enum mvpp2_tag_type {
  478. MVPP2_TAG_TYPE_NONE = 0,
  479. MVPP2_TAG_TYPE_MH = 1,
  480. MVPP2_TAG_TYPE_DSA = 2,
  481. MVPP2_TAG_TYPE_EDSA = 3,
  482. MVPP2_TAG_TYPE_VLAN = 4,
  483. MVPP2_TAG_TYPE_LAST = 5
  484. };
  485. /* Parser constants */
  486. #define MVPP2_PRS_TCAM_SRAM_SIZE 256
  487. #define MVPP2_PRS_TCAM_WORDS 6
  488. #define MVPP2_PRS_SRAM_WORDS 4
  489. #define MVPP2_PRS_FLOW_ID_SIZE 64
  490. #define MVPP2_PRS_FLOW_ID_MASK 0x3f
  491. #define MVPP2_PRS_TCAM_ENTRY_INVALID 1
  492. #define MVPP2_PRS_TCAM_DSA_TAGGED_BIT BIT(5)
  493. #define MVPP2_PRS_IPV4_HEAD 0x40
  494. #define MVPP2_PRS_IPV4_HEAD_MASK 0xf0
  495. #define MVPP2_PRS_IPV4_MC 0xe0
  496. #define MVPP2_PRS_IPV4_MC_MASK 0xf0
  497. #define MVPP2_PRS_IPV4_BC_MASK 0xff
  498. #define MVPP2_PRS_IPV4_IHL 0x5
  499. #define MVPP2_PRS_IPV4_IHL_MASK 0xf
  500. #define MVPP2_PRS_IPV6_MC 0xff
  501. #define MVPP2_PRS_IPV6_MC_MASK 0xff
  502. #define MVPP2_PRS_IPV6_HOP_MASK 0xff
  503. #define MVPP2_PRS_TCAM_PROTO_MASK 0xff
  504. #define MVPP2_PRS_TCAM_PROTO_MASK_L 0x3f
  505. #define MVPP2_PRS_DBL_VLANS_MAX 100
  506. /* Tcam structure:
  507. * - lookup ID - 4 bits
  508. * - port ID - 1 byte
  509. * - additional information - 1 byte
  510. * - header data - 8 bytes
  511. * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(5)->(0).
  512. */
  513. #define MVPP2_PRS_AI_BITS 8
  514. #define MVPP2_PRS_PORT_MASK 0xff
  515. #define MVPP2_PRS_LU_MASK 0xf
  516. #define MVPP2_PRS_TCAM_DATA_BYTE(offs) \
  517. (((offs) - ((offs) % 2)) * 2 + ((offs) % 2))
  518. #define MVPP2_PRS_TCAM_DATA_BYTE_EN(offs) \
  519. (((offs) * 2) - ((offs) % 2) + 2)
  520. #define MVPP2_PRS_TCAM_AI_BYTE 16
  521. #define MVPP2_PRS_TCAM_PORT_BYTE 17
  522. #define MVPP2_PRS_TCAM_LU_BYTE 20
  523. #define MVPP2_PRS_TCAM_EN_OFFS(offs) ((offs) + 2)
  524. #define MVPP2_PRS_TCAM_INV_WORD 5
  525. /* Tcam entries ID */
  526. #define MVPP2_PE_DROP_ALL 0
  527. #define MVPP2_PE_FIRST_FREE_TID 1
  528. #define MVPP2_PE_LAST_FREE_TID (MVPP2_PRS_TCAM_SRAM_SIZE - 31)
  529. #define MVPP2_PE_IP6_EXT_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 30)
  530. #define MVPP2_PE_MAC_MC_IP6 (MVPP2_PRS_TCAM_SRAM_SIZE - 29)
  531. #define MVPP2_PE_IP6_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 28)
  532. #define MVPP2_PE_IP4_ADDR_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 27)
  533. #define MVPP2_PE_LAST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 26)
  534. #define MVPP2_PE_FIRST_DEFAULT_FLOW (MVPP2_PRS_TCAM_SRAM_SIZE - 19)
  535. #define MVPP2_PE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 18)
  536. #define MVPP2_PE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 17)
  537. #define MVPP2_PE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 16)
  538. #define MVPP2_PE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 15)
  539. #define MVPP2_PE_ETYPE_EDSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 14)
  540. #define MVPP2_PE_ETYPE_EDSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 13)
  541. #define MVPP2_PE_ETYPE_DSA_TAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 12)
  542. #define MVPP2_PE_ETYPE_DSA_UNTAGGED (MVPP2_PRS_TCAM_SRAM_SIZE - 11)
  543. #define MVPP2_PE_MH_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 10)
  544. #define MVPP2_PE_DSA_DEFAULT (MVPP2_PRS_TCAM_SRAM_SIZE - 9)
  545. #define MVPP2_PE_IP6_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 8)
  546. #define MVPP2_PE_IP4_PROTO_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 7)
  547. #define MVPP2_PE_ETH_TYPE_UN (MVPP2_PRS_TCAM_SRAM_SIZE - 6)
  548. #define MVPP2_PE_VLAN_DBL (MVPP2_PRS_TCAM_SRAM_SIZE - 5)
  549. #define MVPP2_PE_VLAN_NONE (MVPP2_PRS_TCAM_SRAM_SIZE - 4)
  550. #define MVPP2_PE_MAC_MC_ALL (MVPP2_PRS_TCAM_SRAM_SIZE - 3)
  551. #define MVPP2_PE_MAC_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 2)
  552. #define MVPP2_PE_MAC_NON_PROMISCUOUS (MVPP2_PRS_TCAM_SRAM_SIZE - 1)
  553. /* Sram structure
  554. * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(3)->(0).
  555. */
  556. #define MVPP2_PRS_SRAM_RI_OFFS 0
  557. #define MVPP2_PRS_SRAM_RI_WORD 0
  558. #define MVPP2_PRS_SRAM_RI_CTRL_OFFS 32
  559. #define MVPP2_PRS_SRAM_RI_CTRL_WORD 1
  560. #define MVPP2_PRS_SRAM_RI_CTRL_BITS 32
  561. #define MVPP2_PRS_SRAM_SHIFT_OFFS 64
  562. #define MVPP2_PRS_SRAM_SHIFT_SIGN_BIT 72
  563. #define MVPP2_PRS_SRAM_UDF_OFFS 73
  564. #define MVPP2_PRS_SRAM_UDF_BITS 8
  565. #define MVPP2_PRS_SRAM_UDF_MASK 0xff
  566. #define MVPP2_PRS_SRAM_UDF_SIGN_BIT 81
  567. #define MVPP2_PRS_SRAM_UDF_TYPE_OFFS 82
  568. #define MVPP2_PRS_SRAM_UDF_TYPE_MASK 0x7
  569. #define MVPP2_PRS_SRAM_UDF_TYPE_L3 1
  570. #define MVPP2_PRS_SRAM_UDF_TYPE_L4 4
  571. #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS 85
  572. #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK 0x3
  573. #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD 1
  574. #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP4_ADD 2
  575. #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP6_ADD 3
  576. #define MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS 87
  577. #define MVPP2_PRS_SRAM_OP_SEL_UDF_BITS 2
  578. #define MVPP2_PRS_SRAM_OP_SEL_UDF_MASK 0x3
  579. #define MVPP2_PRS_SRAM_OP_SEL_UDF_ADD 0
  580. #define MVPP2_PRS_SRAM_OP_SEL_UDF_IP4_ADD 2
  581. #define MVPP2_PRS_SRAM_OP_SEL_UDF_IP6_ADD 3
  582. #define MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS 89
  583. #define MVPP2_PRS_SRAM_AI_OFFS 90
  584. #define MVPP2_PRS_SRAM_AI_CTRL_OFFS 98
  585. #define MVPP2_PRS_SRAM_AI_CTRL_BITS 8
  586. #define MVPP2_PRS_SRAM_AI_MASK 0xff
  587. #define MVPP2_PRS_SRAM_NEXT_LU_OFFS 106
  588. #define MVPP2_PRS_SRAM_NEXT_LU_MASK 0xf
  589. #define MVPP2_PRS_SRAM_LU_DONE_BIT 110
  590. #define MVPP2_PRS_SRAM_LU_GEN_BIT 111
  591. /* Sram result info bits assignment */
  592. #define MVPP2_PRS_RI_MAC_ME_MASK 0x1
  593. #define MVPP2_PRS_RI_DSA_MASK 0x2
  594. #define MVPP2_PRS_RI_VLAN_MASK (BIT(2) | BIT(3))
  595. #define MVPP2_PRS_RI_VLAN_NONE 0x0
  596. #define MVPP2_PRS_RI_VLAN_SINGLE BIT(2)
  597. #define MVPP2_PRS_RI_VLAN_DOUBLE BIT(3)
  598. #define MVPP2_PRS_RI_VLAN_TRIPLE (BIT(2) | BIT(3))
  599. #define MVPP2_PRS_RI_CPU_CODE_MASK 0x70
  600. #define MVPP2_PRS_RI_CPU_CODE_RX_SPEC BIT(4)
  601. #define MVPP2_PRS_RI_L2_CAST_MASK (BIT(9) | BIT(10))
  602. #define MVPP2_PRS_RI_L2_UCAST 0x0
  603. #define MVPP2_PRS_RI_L2_MCAST BIT(9)
  604. #define MVPP2_PRS_RI_L2_BCAST BIT(10)
  605. #define MVPP2_PRS_RI_PPPOE_MASK 0x800
  606. #define MVPP2_PRS_RI_L3_PROTO_MASK (BIT(12) | BIT(13) | BIT(14))
  607. #define MVPP2_PRS_RI_L3_UN 0x0
  608. #define MVPP2_PRS_RI_L3_IP4 BIT(12)
  609. #define MVPP2_PRS_RI_L3_IP4_OPT BIT(13)
  610. #define MVPP2_PRS_RI_L3_IP4_OTHER (BIT(12) | BIT(13))
  611. #define MVPP2_PRS_RI_L3_IP6 BIT(14)
  612. #define MVPP2_PRS_RI_L3_IP6_EXT (BIT(12) | BIT(14))
  613. #define MVPP2_PRS_RI_L3_ARP (BIT(13) | BIT(14))
  614. #define MVPP2_PRS_RI_L3_ADDR_MASK (BIT(15) | BIT(16))
  615. #define MVPP2_PRS_RI_L3_UCAST 0x0
  616. #define MVPP2_PRS_RI_L3_MCAST BIT(15)
  617. #define MVPP2_PRS_RI_L3_BCAST (BIT(15) | BIT(16))
  618. #define MVPP2_PRS_RI_IP_FRAG_MASK 0x20000
  619. #define MVPP2_PRS_RI_IP_FRAG_TRUE BIT(17)
  620. #define MVPP2_PRS_RI_UDF3_MASK 0x300000
  621. #define MVPP2_PRS_RI_UDF3_RX_SPECIAL BIT(21)
  622. #define MVPP2_PRS_RI_L4_PROTO_MASK 0x1c00000
  623. #define MVPP2_PRS_RI_L4_TCP BIT(22)
  624. #define MVPP2_PRS_RI_L4_UDP BIT(23)
  625. #define MVPP2_PRS_RI_L4_OTHER (BIT(22) | BIT(23))
  626. #define MVPP2_PRS_RI_UDF7_MASK 0x60000000
  627. #define MVPP2_PRS_RI_UDF7_IP6_LITE BIT(29)
  628. #define MVPP2_PRS_RI_DROP_MASK 0x80000000
  629. /* Sram additional info bits assignment */
  630. #define MVPP2_PRS_IPV4_DIP_AI_BIT BIT(0)
  631. #define MVPP2_PRS_IPV6_NO_EXT_AI_BIT BIT(0)
  632. #define MVPP2_PRS_IPV6_EXT_AI_BIT BIT(1)
  633. #define MVPP2_PRS_IPV6_EXT_AH_AI_BIT BIT(2)
  634. #define MVPP2_PRS_IPV6_EXT_AH_LEN_AI_BIT BIT(3)
  635. #define MVPP2_PRS_IPV6_EXT_AH_L4_AI_BIT BIT(4)
  636. #define MVPP2_PRS_SINGLE_VLAN_AI 0
  637. #define MVPP2_PRS_DBL_VLAN_AI_BIT BIT(7)
  638. /* DSA/EDSA type */
  639. #define MVPP2_PRS_TAGGED true
  640. #define MVPP2_PRS_UNTAGGED false
  641. #define MVPP2_PRS_EDSA true
  642. #define MVPP2_PRS_DSA false
  643. /* MAC entries, shadow udf */
  644. enum mvpp2_prs_udf {
  645. MVPP2_PRS_UDF_MAC_DEF,
  646. MVPP2_PRS_UDF_MAC_RANGE,
  647. MVPP2_PRS_UDF_L2_DEF,
  648. MVPP2_PRS_UDF_L2_DEF_COPY,
  649. MVPP2_PRS_UDF_L2_USER,
  650. };
  651. /* Lookup ID */
  652. enum mvpp2_prs_lookup {
  653. MVPP2_PRS_LU_MH,
  654. MVPP2_PRS_LU_MAC,
  655. MVPP2_PRS_LU_DSA,
  656. MVPP2_PRS_LU_VLAN,
  657. MVPP2_PRS_LU_L2,
  658. MVPP2_PRS_LU_PPPOE,
  659. MVPP2_PRS_LU_IP4,
  660. MVPP2_PRS_LU_IP6,
  661. MVPP2_PRS_LU_FLOWS,
  662. MVPP2_PRS_LU_LAST,
  663. };
  664. /* L3 cast enum */
  665. enum mvpp2_prs_l3_cast {
  666. MVPP2_PRS_L3_UNI_CAST,
  667. MVPP2_PRS_L3_MULTI_CAST,
  668. MVPP2_PRS_L3_BROAD_CAST
  669. };
  670. /* Classifier constants */
  671. #define MVPP2_CLS_FLOWS_TBL_SIZE 512
  672. #define MVPP2_CLS_FLOWS_TBL_DATA_WORDS 3
  673. #define MVPP2_CLS_LKP_TBL_SIZE 64
  674. /* BM constants */
  675. #define MVPP2_BM_POOLS_NUM 8
  676. #define MVPP2_BM_LONG_BUF_NUM 1024
  677. #define MVPP2_BM_SHORT_BUF_NUM 2048
  678. #define MVPP2_BM_POOL_SIZE_MAX (16*1024 - MVPP2_BM_POOL_PTR_ALIGN/4)
  679. #define MVPP2_BM_POOL_PTR_ALIGN 128
  680. #define MVPP2_BM_SWF_LONG_POOL(port) ((port > 2) ? 2 : port)
  681. #define MVPP2_BM_SWF_SHORT_POOL 3
  682. /* BM cookie (32 bits) definition */
  683. #define MVPP2_BM_COOKIE_POOL_OFFS 8
  684. #define MVPP2_BM_COOKIE_CPU_OFFS 24
  685. /* BM short pool packet size
  686. * These value assure that for SWF the total number
  687. * of bytes allocated for each buffer will be 512
  688. */
  689. #define MVPP2_BM_SHORT_PKT_SIZE MVPP2_RX_MAX_PKT_SIZE(512)
  690. #define MVPP21_ADDR_SPACE_SZ 0
  691. #define MVPP22_ADDR_SPACE_SZ SZ_64K
  692. #define MVPP2_MAX_THREADS 8
  693. #define MVPP2_MAX_QVECS MVPP2_MAX_THREADS
  694. enum mvpp2_bm_type {
  695. MVPP2_BM_FREE,
  696. MVPP2_BM_SWF_LONG,
  697. MVPP2_BM_SWF_SHORT
  698. };
  699. /* Definitions */
  700. /* Shared Packet Processor resources */
  701. struct mvpp2 {
  702. /* Shared registers' base addresses */
  703. void __iomem *lms_base;
  704. void __iomem *iface_base;
  705. /* On PPv2.2, each "software thread" can access the base
  706. * register through a separate address space, each 64 KB apart
  707. * from each other. Typically, such address spaces will be
  708. * used per CPU.
  709. */
  710. void __iomem *swth_base[MVPP2_MAX_THREADS];
  711. /* On PPv2.2, some port control registers are located into the system
  712. * controller space. These registers are accessible through a regmap.
  713. */
  714. struct regmap *sysctrl_base;
  715. /* Common clocks */
  716. struct clk *pp_clk;
  717. struct clk *gop_clk;
  718. struct clk *mg_clk;
  719. struct clk *axi_clk;
  720. /* List of pointers to port structures */
  721. struct mvpp2_port **port_list;
  722. /* Aggregated TXQs */
  723. struct mvpp2_tx_queue *aggr_txqs;
  724. /* BM pools */
  725. struct mvpp2_bm_pool *bm_pools;
  726. /* PRS shadow table */
  727. struct mvpp2_prs_shadow *prs_shadow;
  728. /* PRS auxiliary table for double vlan entries control */
  729. bool *prs_double_vlans;
  730. /* Tclk value */
  731. u32 tclk;
  732. /* HW version */
  733. enum { MVPP21, MVPP22 } hw_version;
  734. /* Maximum number of RXQs per port */
  735. unsigned int max_port_rxqs;
  736. };
  737. struct mvpp2_pcpu_stats {
  738. struct u64_stats_sync syncp;
  739. u64 rx_packets;
  740. u64 rx_bytes;
  741. u64 tx_packets;
  742. u64 tx_bytes;
  743. };
  744. /* Per-CPU port control */
  745. struct mvpp2_port_pcpu {
  746. struct hrtimer tx_done_timer;
  747. bool timer_scheduled;
  748. /* Tasklet for egress finalization */
  749. struct tasklet_struct tx_done_tasklet;
  750. };
  751. struct mvpp2_queue_vector {
  752. int irq;
  753. struct napi_struct napi;
  754. enum { MVPP2_QUEUE_VECTOR_SHARED, MVPP2_QUEUE_VECTOR_PRIVATE } type;
  755. int sw_thread_id;
  756. u16 sw_thread_mask;
  757. int first_rxq;
  758. int nrxqs;
  759. u32 pending_cause_rx;
  760. struct mvpp2_port *port;
  761. };
  762. struct mvpp2_port {
  763. u8 id;
  764. /* Index of the port from the "group of ports" complex point
  765. * of view
  766. */
  767. int gop_id;
  768. int link_irq;
  769. struct mvpp2 *priv;
  770. /* Per-port registers' base address */
  771. void __iomem *base;
  772. struct mvpp2_rx_queue **rxqs;
  773. unsigned int nrxqs;
  774. struct mvpp2_tx_queue **txqs;
  775. unsigned int ntxqs;
  776. struct net_device *dev;
  777. int pkt_size;
  778. /* Per-CPU port control */
  779. struct mvpp2_port_pcpu __percpu *pcpu;
  780. /* Flags */
  781. unsigned long flags;
  782. u16 tx_ring_size;
  783. u16 rx_ring_size;
  784. struct mvpp2_pcpu_stats __percpu *stats;
  785. phy_interface_t phy_interface;
  786. struct device_node *phy_node;
  787. struct phy *comphy;
  788. unsigned int link;
  789. unsigned int duplex;
  790. unsigned int speed;
  791. struct mvpp2_bm_pool *pool_long;
  792. struct mvpp2_bm_pool *pool_short;
  793. /* Index of first port's physical RXQ */
  794. u8 first_rxq;
  795. struct mvpp2_queue_vector qvecs[MVPP2_MAX_QVECS];
  796. unsigned int nqvecs;
  797. bool has_tx_irqs;
  798. u32 tx_time_coal;
  799. };
  800. /* The mvpp2_tx_desc and mvpp2_rx_desc structures describe the
  801. * layout of the transmit and reception DMA descriptors, and their
  802. * layout is therefore defined by the hardware design
  803. */
  804. #define MVPP2_TXD_L3_OFF_SHIFT 0
  805. #define MVPP2_TXD_IP_HLEN_SHIFT 8
  806. #define MVPP2_TXD_L4_CSUM_FRAG BIT(13)
  807. #define MVPP2_TXD_L4_CSUM_NOT BIT(14)
  808. #define MVPP2_TXD_IP_CSUM_DISABLE BIT(15)
  809. #define MVPP2_TXD_PADDING_DISABLE BIT(23)
  810. #define MVPP2_TXD_L4_UDP BIT(24)
  811. #define MVPP2_TXD_L3_IP6 BIT(26)
  812. #define MVPP2_TXD_L_DESC BIT(28)
  813. #define MVPP2_TXD_F_DESC BIT(29)
  814. #define MVPP2_RXD_ERR_SUMMARY BIT(15)
  815. #define MVPP2_RXD_ERR_CODE_MASK (BIT(13) | BIT(14))
  816. #define MVPP2_RXD_ERR_CRC 0x0
  817. #define MVPP2_RXD_ERR_OVERRUN BIT(13)
  818. #define MVPP2_RXD_ERR_RESOURCE (BIT(13) | BIT(14))
  819. #define MVPP2_RXD_BM_POOL_ID_OFFS 16
  820. #define MVPP2_RXD_BM_POOL_ID_MASK (BIT(16) | BIT(17) | BIT(18))
  821. #define MVPP2_RXD_HWF_SYNC BIT(21)
  822. #define MVPP2_RXD_L4_CSUM_OK BIT(22)
  823. #define MVPP2_RXD_IP4_HEADER_ERR BIT(24)
  824. #define MVPP2_RXD_L4_TCP BIT(25)
  825. #define MVPP2_RXD_L4_UDP BIT(26)
  826. #define MVPP2_RXD_L3_IP4 BIT(28)
  827. #define MVPP2_RXD_L3_IP6 BIT(30)
  828. #define MVPP2_RXD_BUF_HDR BIT(31)
  829. /* HW TX descriptor for PPv2.1 */
  830. struct mvpp21_tx_desc {
  831. u32 command; /* Options used by HW for packet transmitting.*/
  832. u8 packet_offset; /* the offset from the buffer beginning */
  833. u8 phys_txq; /* destination queue ID */
  834. u16 data_size; /* data size of transmitted packet in bytes */
  835. u32 buf_dma_addr; /* physical addr of transmitted buffer */
  836. u32 buf_cookie; /* cookie for access to TX buffer in tx path */
  837. u32 reserved1[3]; /* hw_cmd (for future use, BM, PON, PNC) */
  838. u32 reserved2; /* reserved (for future use) */
  839. };
  840. /* HW RX descriptor for PPv2.1 */
  841. struct mvpp21_rx_desc {
  842. u32 status; /* info about received packet */
  843. u16 reserved1; /* parser_info (for future use, PnC) */
  844. u16 data_size; /* size of received packet in bytes */
  845. u32 buf_dma_addr; /* physical address of the buffer */
  846. u32 buf_cookie; /* cookie for access to RX buffer in rx path */
  847. u16 reserved2; /* gem_port_id (for future use, PON) */
  848. u16 reserved3; /* csum_l4 (for future use, PnC) */
  849. u8 reserved4; /* bm_qset (for future use, BM) */
  850. u8 reserved5;
  851. u16 reserved6; /* classify_info (for future use, PnC) */
  852. u32 reserved7; /* flow_id (for future use, PnC) */
  853. u32 reserved8;
  854. };
  855. /* HW TX descriptor for PPv2.2 */
  856. struct mvpp22_tx_desc {
  857. u32 command;
  858. u8 packet_offset;
  859. u8 phys_txq;
  860. u16 data_size;
  861. u64 reserved1;
  862. u64 buf_dma_addr_ptp;
  863. u64 buf_cookie_misc;
  864. };
  865. /* HW RX descriptor for PPv2.2 */
  866. struct mvpp22_rx_desc {
  867. u32 status;
  868. u16 reserved1;
  869. u16 data_size;
  870. u32 reserved2;
  871. u32 reserved3;
  872. u64 buf_dma_addr_key_hash;
  873. u64 buf_cookie_misc;
  874. };
  875. /* Opaque type used by the driver to manipulate the HW TX and RX
  876. * descriptors
  877. */
  878. struct mvpp2_tx_desc {
  879. union {
  880. struct mvpp21_tx_desc pp21;
  881. struct mvpp22_tx_desc pp22;
  882. };
  883. };
  884. struct mvpp2_rx_desc {
  885. union {
  886. struct mvpp21_rx_desc pp21;
  887. struct mvpp22_rx_desc pp22;
  888. };
  889. };
  890. struct mvpp2_txq_pcpu_buf {
  891. /* Transmitted SKB */
  892. struct sk_buff *skb;
  893. /* Physical address of transmitted buffer */
  894. dma_addr_t dma;
  895. /* Size transmitted */
  896. size_t size;
  897. };
  898. /* Per-CPU Tx queue control */
  899. struct mvpp2_txq_pcpu {
  900. int cpu;
  901. /* Number of Tx DMA descriptors in the descriptor ring */
  902. int size;
  903. /* Number of currently used Tx DMA descriptor in the
  904. * descriptor ring
  905. */
  906. int count;
  907. /* Number of Tx DMA descriptors reserved for each CPU */
  908. int reserved_num;
  909. /* Infos about transmitted buffers */
  910. struct mvpp2_txq_pcpu_buf *buffs;
  911. /* Index of last TX DMA descriptor that was inserted */
  912. int txq_put_index;
  913. /* Index of the TX DMA descriptor to be cleaned up */
  914. int txq_get_index;
  915. /* DMA buffer for TSO headers */
  916. char *tso_headers;
  917. dma_addr_t tso_headers_dma;
  918. };
  919. struct mvpp2_tx_queue {
  920. /* Physical number of this Tx queue */
  921. u8 id;
  922. /* Logical number of this Tx queue */
  923. u8 log_id;
  924. /* Number of Tx DMA descriptors in the descriptor ring */
  925. int size;
  926. /* Number of currently used Tx DMA descriptor in the descriptor ring */
  927. int count;
  928. /* Per-CPU control of physical Tx queues */
  929. struct mvpp2_txq_pcpu __percpu *pcpu;
  930. u32 done_pkts_coal;
  931. /* Virtual address of thex Tx DMA descriptors array */
  932. struct mvpp2_tx_desc *descs;
  933. /* DMA address of the Tx DMA descriptors array */
  934. dma_addr_t descs_dma;
  935. /* Index of the last Tx DMA descriptor */
  936. int last_desc;
  937. /* Index of the next Tx DMA descriptor to process */
  938. int next_desc_to_proc;
  939. };
  940. struct mvpp2_rx_queue {
  941. /* RX queue number, in the range 0-31 for physical RXQs */
  942. u8 id;
  943. /* Num of rx descriptors in the rx descriptor ring */
  944. int size;
  945. u32 pkts_coal;
  946. u32 time_coal;
  947. /* Virtual address of the RX DMA descriptors array */
  948. struct mvpp2_rx_desc *descs;
  949. /* DMA address of the RX DMA descriptors array */
  950. dma_addr_t descs_dma;
  951. /* Index of the last RX DMA descriptor */
  952. int last_desc;
  953. /* Index of the next RX DMA descriptor to process */
  954. int next_desc_to_proc;
  955. /* ID of port to which physical RXQ is mapped */
  956. int port;
  957. /* Port's logic RXQ number to which physical RXQ is mapped */
  958. int logic_rxq;
  959. };
  960. union mvpp2_prs_tcam_entry {
  961. u32 word[MVPP2_PRS_TCAM_WORDS];
  962. u8 byte[MVPP2_PRS_TCAM_WORDS * 4];
  963. };
  964. union mvpp2_prs_sram_entry {
  965. u32 word[MVPP2_PRS_SRAM_WORDS];
  966. u8 byte[MVPP2_PRS_SRAM_WORDS * 4];
  967. };
  968. struct mvpp2_prs_entry {
  969. u32 index;
  970. union mvpp2_prs_tcam_entry tcam;
  971. union mvpp2_prs_sram_entry sram;
  972. };
  973. struct mvpp2_prs_shadow {
  974. bool valid;
  975. bool finish;
  976. /* Lookup ID */
  977. int lu;
  978. /* User defined offset */
  979. int udf;
  980. /* Result info */
  981. u32 ri;
  982. u32 ri_mask;
  983. };
  984. struct mvpp2_cls_flow_entry {
  985. u32 index;
  986. u32 data[MVPP2_CLS_FLOWS_TBL_DATA_WORDS];
  987. };
  988. struct mvpp2_cls_lookup_entry {
  989. u32 lkpid;
  990. u32 way;
  991. u32 data;
  992. };
  993. struct mvpp2_bm_pool {
  994. /* Pool number in the range 0-7 */
  995. int id;
  996. enum mvpp2_bm_type type;
  997. /* Buffer Pointers Pool External (BPPE) size */
  998. int size;
  999. /* BPPE size in bytes */
  1000. int size_bytes;
  1001. /* Number of buffers for this pool */
  1002. int buf_num;
  1003. /* Pool buffer size */
  1004. int buf_size;
  1005. /* Packet size */
  1006. int pkt_size;
  1007. int frag_size;
  1008. /* BPPE virtual base address */
  1009. u32 *virt_addr;
  1010. /* BPPE DMA base address */
  1011. dma_addr_t dma_addr;
  1012. /* Ports using BM pool */
  1013. u32 port_map;
  1014. };
  1015. /* Queue modes */
  1016. #define MVPP2_QDIST_SINGLE_MODE 0
  1017. #define MVPP2_QDIST_MULTI_MODE 1
  1018. static int queue_mode = MVPP2_QDIST_SINGLE_MODE;
  1019. module_param(queue_mode, int, 0444);
  1020. MODULE_PARM_DESC(queue_mode, "Set queue_mode (single=0, multi=1)");
  1021. #define MVPP2_DRIVER_NAME "mvpp2"
  1022. #define MVPP2_DRIVER_VERSION "1.0"
  1023. /* Utility/helper methods */
  1024. static void mvpp2_write(struct mvpp2 *priv, u32 offset, u32 data)
  1025. {
  1026. writel(data, priv->swth_base[0] + offset);
  1027. }
  1028. static u32 mvpp2_read(struct mvpp2 *priv, u32 offset)
  1029. {
  1030. return readl(priv->swth_base[0] + offset);
  1031. }
  1032. /* These accessors should be used to access:
  1033. *
  1034. * - per-CPU registers, where each CPU has its own copy of the
  1035. * register.
  1036. *
  1037. * MVPP2_BM_VIRT_ALLOC_REG
  1038. * MVPP2_BM_ADDR_HIGH_ALLOC
  1039. * MVPP22_BM_ADDR_HIGH_RLS_REG
  1040. * MVPP2_BM_VIRT_RLS_REG
  1041. * MVPP2_ISR_RX_TX_CAUSE_REG
  1042. * MVPP2_ISR_RX_TX_MASK_REG
  1043. * MVPP2_TXQ_NUM_REG
  1044. * MVPP2_AGGR_TXQ_UPDATE_REG
  1045. * MVPP2_TXQ_RSVD_REQ_REG
  1046. * MVPP2_TXQ_RSVD_RSLT_REG
  1047. * MVPP2_TXQ_SENT_REG
  1048. * MVPP2_RXQ_NUM_REG
  1049. *
  1050. * - global registers that must be accessed through a specific CPU
  1051. * window, because they are related to an access to a per-CPU
  1052. * register
  1053. *
  1054. * MVPP2_BM_PHY_ALLOC_REG (related to MVPP2_BM_VIRT_ALLOC_REG)
  1055. * MVPP2_BM_PHY_RLS_REG (related to MVPP2_BM_VIRT_RLS_REG)
  1056. * MVPP2_RXQ_THRESH_REG (related to MVPP2_RXQ_NUM_REG)
  1057. * MVPP2_RXQ_DESC_ADDR_REG (related to MVPP2_RXQ_NUM_REG)
  1058. * MVPP2_RXQ_DESC_SIZE_REG (related to MVPP2_RXQ_NUM_REG)
  1059. * MVPP2_RXQ_INDEX_REG (related to MVPP2_RXQ_NUM_REG)
  1060. * MVPP2_TXQ_PENDING_REG (related to MVPP2_TXQ_NUM_REG)
  1061. * MVPP2_TXQ_DESC_ADDR_REG (related to MVPP2_TXQ_NUM_REG)
  1062. * MVPP2_TXQ_DESC_SIZE_REG (related to MVPP2_TXQ_NUM_REG)
  1063. * MVPP2_TXQ_INDEX_REG (related to MVPP2_TXQ_NUM_REG)
  1064. * MVPP2_TXQ_PENDING_REG (related to MVPP2_TXQ_NUM_REG)
  1065. * MVPP2_TXQ_PREF_BUF_REG (related to MVPP2_TXQ_NUM_REG)
  1066. * MVPP2_TXQ_PREF_BUF_REG (related to MVPP2_TXQ_NUM_REG)
  1067. */
  1068. static void mvpp2_percpu_write(struct mvpp2 *priv, int cpu,
  1069. u32 offset, u32 data)
  1070. {
  1071. writel(data, priv->swth_base[cpu] + offset);
  1072. }
  1073. static u32 mvpp2_percpu_read(struct mvpp2 *priv, int cpu,
  1074. u32 offset)
  1075. {
  1076. return readl(priv->swth_base[cpu] + offset);
  1077. }
  1078. static dma_addr_t mvpp2_txdesc_dma_addr_get(struct mvpp2_port *port,
  1079. struct mvpp2_tx_desc *tx_desc)
  1080. {
  1081. if (port->priv->hw_version == MVPP21)
  1082. return tx_desc->pp21.buf_dma_addr;
  1083. else
  1084. return tx_desc->pp22.buf_dma_addr_ptp & GENMASK_ULL(40, 0);
  1085. }
  1086. static void mvpp2_txdesc_dma_addr_set(struct mvpp2_port *port,
  1087. struct mvpp2_tx_desc *tx_desc,
  1088. dma_addr_t dma_addr)
  1089. {
  1090. if (port->priv->hw_version == MVPP21) {
  1091. tx_desc->pp21.buf_dma_addr = dma_addr;
  1092. } else {
  1093. u64 val = (u64)dma_addr;
  1094. tx_desc->pp22.buf_dma_addr_ptp &= ~GENMASK_ULL(40, 0);
  1095. tx_desc->pp22.buf_dma_addr_ptp |= val;
  1096. }
  1097. }
  1098. static size_t mvpp2_txdesc_size_get(struct mvpp2_port *port,
  1099. struct mvpp2_tx_desc *tx_desc)
  1100. {
  1101. if (port->priv->hw_version == MVPP21)
  1102. return tx_desc->pp21.data_size;
  1103. else
  1104. return tx_desc->pp22.data_size;
  1105. }
  1106. static void mvpp2_txdesc_size_set(struct mvpp2_port *port,
  1107. struct mvpp2_tx_desc *tx_desc,
  1108. size_t size)
  1109. {
  1110. if (port->priv->hw_version == MVPP21)
  1111. tx_desc->pp21.data_size = size;
  1112. else
  1113. tx_desc->pp22.data_size = size;
  1114. }
  1115. static void mvpp2_txdesc_txq_set(struct mvpp2_port *port,
  1116. struct mvpp2_tx_desc *tx_desc,
  1117. unsigned int txq)
  1118. {
  1119. if (port->priv->hw_version == MVPP21)
  1120. tx_desc->pp21.phys_txq = txq;
  1121. else
  1122. tx_desc->pp22.phys_txq = txq;
  1123. }
  1124. static void mvpp2_txdesc_cmd_set(struct mvpp2_port *port,
  1125. struct mvpp2_tx_desc *tx_desc,
  1126. unsigned int command)
  1127. {
  1128. if (port->priv->hw_version == MVPP21)
  1129. tx_desc->pp21.command = command;
  1130. else
  1131. tx_desc->pp22.command = command;
  1132. }
  1133. static void mvpp2_txdesc_offset_set(struct mvpp2_port *port,
  1134. struct mvpp2_tx_desc *tx_desc,
  1135. unsigned int offset)
  1136. {
  1137. if (port->priv->hw_version == MVPP21)
  1138. tx_desc->pp21.packet_offset = offset;
  1139. else
  1140. tx_desc->pp22.packet_offset = offset;
  1141. }
  1142. static unsigned int mvpp2_txdesc_offset_get(struct mvpp2_port *port,
  1143. struct mvpp2_tx_desc *tx_desc)
  1144. {
  1145. if (port->priv->hw_version == MVPP21)
  1146. return tx_desc->pp21.packet_offset;
  1147. else
  1148. return tx_desc->pp22.packet_offset;
  1149. }
  1150. static dma_addr_t mvpp2_rxdesc_dma_addr_get(struct mvpp2_port *port,
  1151. struct mvpp2_rx_desc *rx_desc)
  1152. {
  1153. if (port->priv->hw_version == MVPP21)
  1154. return rx_desc->pp21.buf_dma_addr;
  1155. else
  1156. return rx_desc->pp22.buf_dma_addr_key_hash & GENMASK_ULL(40, 0);
  1157. }
  1158. static unsigned long mvpp2_rxdesc_cookie_get(struct mvpp2_port *port,
  1159. struct mvpp2_rx_desc *rx_desc)
  1160. {
  1161. if (port->priv->hw_version == MVPP21)
  1162. return rx_desc->pp21.buf_cookie;
  1163. else
  1164. return rx_desc->pp22.buf_cookie_misc & GENMASK_ULL(40, 0);
  1165. }
  1166. static size_t mvpp2_rxdesc_size_get(struct mvpp2_port *port,
  1167. struct mvpp2_rx_desc *rx_desc)
  1168. {
  1169. if (port->priv->hw_version == MVPP21)
  1170. return rx_desc->pp21.data_size;
  1171. else
  1172. return rx_desc->pp22.data_size;
  1173. }
  1174. static u32 mvpp2_rxdesc_status_get(struct mvpp2_port *port,
  1175. struct mvpp2_rx_desc *rx_desc)
  1176. {
  1177. if (port->priv->hw_version == MVPP21)
  1178. return rx_desc->pp21.status;
  1179. else
  1180. return rx_desc->pp22.status;
  1181. }
  1182. static void mvpp2_txq_inc_get(struct mvpp2_txq_pcpu *txq_pcpu)
  1183. {
  1184. txq_pcpu->txq_get_index++;
  1185. if (txq_pcpu->txq_get_index == txq_pcpu->size)
  1186. txq_pcpu->txq_get_index = 0;
  1187. }
  1188. static void mvpp2_txq_inc_put(struct mvpp2_port *port,
  1189. struct mvpp2_txq_pcpu *txq_pcpu,
  1190. struct sk_buff *skb,
  1191. struct mvpp2_tx_desc *tx_desc)
  1192. {
  1193. struct mvpp2_txq_pcpu_buf *tx_buf =
  1194. txq_pcpu->buffs + txq_pcpu->txq_put_index;
  1195. tx_buf->skb = skb;
  1196. tx_buf->size = mvpp2_txdesc_size_get(port, tx_desc);
  1197. tx_buf->dma = mvpp2_txdesc_dma_addr_get(port, tx_desc) +
  1198. mvpp2_txdesc_offset_get(port, tx_desc);
  1199. txq_pcpu->txq_put_index++;
  1200. if (txq_pcpu->txq_put_index == txq_pcpu->size)
  1201. txq_pcpu->txq_put_index = 0;
  1202. }
  1203. /* Get number of physical egress port */
  1204. static inline int mvpp2_egress_port(struct mvpp2_port *port)
  1205. {
  1206. return MVPP2_MAX_TCONT + port->id;
  1207. }
  1208. /* Get number of physical TXQ */
  1209. static inline int mvpp2_txq_phys(int port, int txq)
  1210. {
  1211. return (MVPP2_MAX_TCONT + port) * MVPP2_MAX_TXQ + txq;
  1212. }
  1213. /* Parser configuration routines */
  1214. /* Update parser tcam and sram hw entries */
  1215. static int mvpp2_prs_hw_write(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
  1216. {
  1217. int i;
  1218. if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
  1219. return -EINVAL;
  1220. /* Clear entry invalidation bit */
  1221. pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] &= ~MVPP2_PRS_TCAM_INV_MASK;
  1222. /* Write tcam index - indirect access */
  1223. mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
  1224. for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
  1225. mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), pe->tcam.word[i]);
  1226. /* Write sram index - indirect access */
  1227. mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
  1228. for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
  1229. mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), pe->sram.word[i]);
  1230. return 0;
  1231. }
  1232. /* Read tcam entry from hw */
  1233. static int mvpp2_prs_hw_read(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
  1234. {
  1235. int i;
  1236. if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
  1237. return -EINVAL;
  1238. /* Write tcam index - indirect access */
  1239. mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
  1240. pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] = mvpp2_read(priv,
  1241. MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD));
  1242. if (pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] & MVPP2_PRS_TCAM_INV_MASK)
  1243. return MVPP2_PRS_TCAM_ENTRY_INVALID;
  1244. for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
  1245. pe->tcam.word[i] = mvpp2_read(priv, MVPP2_PRS_TCAM_DATA_REG(i));
  1246. /* Write sram index - indirect access */
  1247. mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
  1248. for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
  1249. pe->sram.word[i] = mvpp2_read(priv, MVPP2_PRS_SRAM_DATA_REG(i));
  1250. return 0;
  1251. }
  1252. /* Invalidate tcam hw entry */
  1253. static void mvpp2_prs_hw_inv(struct mvpp2 *priv, int index)
  1254. {
  1255. /* Write index - indirect access */
  1256. mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
  1257. mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD),
  1258. MVPP2_PRS_TCAM_INV_MASK);
  1259. }
  1260. /* Enable shadow table entry and set its lookup ID */
  1261. static void mvpp2_prs_shadow_set(struct mvpp2 *priv, int index, int lu)
  1262. {
  1263. priv->prs_shadow[index].valid = true;
  1264. priv->prs_shadow[index].lu = lu;
  1265. }
  1266. /* Update ri fields in shadow table entry */
  1267. static void mvpp2_prs_shadow_ri_set(struct mvpp2 *priv, int index,
  1268. unsigned int ri, unsigned int ri_mask)
  1269. {
  1270. priv->prs_shadow[index].ri_mask = ri_mask;
  1271. priv->prs_shadow[index].ri = ri;
  1272. }
  1273. /* Update lookup field in tcam sw entry */
  1274. static void mvpp2_prs_tcam_lu_set(struct mvpp2_prs_entry *pe, unsigned int lu)
  1275. {
  1276. int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_LU_BYTE);
  1277. pe->tcam.byte[MVPP2_PRS_TCAM_LU_BYTE] = lu;
  1278. pe->tcam.byte[enable_off] = MVPP2_PRS_LU_MASK;
  1279. }
  1280. /* Update mask for single port in tcam sw entry */
  1281. static void mvpp2_prs_tcam_port_set(struct mvpp2_prs_entry *pe,
  1282. unsigned int port, bool add)
  1283. {
  1284. int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
  1285. if (add)
  1286. pe->tcam.byte[enable_off] &= ~(1 << port);
  1287. else
  1288. pe->tcam.byte[enable_off] |= 1 << port;
  1289. }
  1290. /* Update port map in tcam sw entry */
  1291. static void mvpp2_prs_tcam_port_map_set(struct mvpp2_prs_entry *pe,
  1292. unsigned int ports)
  1293. {
  1294. unsigned char port_mask = MVPP2_PRS_PORT_MASK;
  1295. int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
  1296. pe->tcam.byte[MVPP2_PRS_TCAM_PORT_BYTE] = 0;
  1297. pe->tcam.byte[enable_off] &= ~port_mask;
  1298. pe->tcam.byte[enable_off] |= ~ports & MVPP2_PRS_PORT_MASK;
  1299. }
  1300. /* Obtain port map from tcam sw entry */
  1301. static unsigned int mvpp2_prs_tcam_port_map_get(struct mvpp2_prs_entry *pe)
  1302. {
  1303. int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
  1304. return ~(pe->tcam.byte[enable_off]) & MVPP2_PRS_PORT_MASK;
  1305. }
  1306. /* Set byte of data and its enable bits in tcam sw entry */
  1307. static void mvpp2_prs_tcam_data_byte_set(struct mvpp2_prs_entry *pe,
  1308. unsigned int offs, unsigned char byte,
  1309. unsigned char enable)
  1310. {
  1311. pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)] = byte;
  1312. pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)] = enable;
  1313. }
  1314. /* Get byte of data and its enable bits from tcam sw entry */
  1315. static void mvpp2_prs_tcam_data_byte_get(struct mvpp2_prs_entry *pe,
  1316. unsigned int offs, unsigned char *byte,
  1317. unsigned char *enable)
  1318. {
  1319. *byte = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)];
  1320. *enable = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)];
  1321. }
  1322. /* Compare tcam data bytes with a pattern */
  1323. static bool mvpp2_prs_tcam_data_cmp(struct mvpp2_prs_entry *pe, int offs,
  1324. u16 data)
  1325. {
  1326. int off = MVPP2_PRS_TCAM_DATA_BYTE(offs);
  1327. u16 tcam_data;
  1328. tcam_data = (8 << pe->tcam.byte[off + 1]) | pe->tcam.byte[off];
  1329. if (tcam_data != data)
  1330. return false;
  1331. return true;
  1332. }
  1333. /* Update ai bits in tcam sw entry */
  1334. static void mvpp2_prs_tcam_ai_update(struct mvpp2_prs_entry *pe,
  1335. unsigned int bits, unsigned int enable)
  1336. {
  1337. int i, ai_idx = MVPP2_PRS_TCAM_AI_BYTE;
  1338. for (i = 0; i < MVPP2_PRS_AI_BITS; i++) {
  1339. if (!(enable & BIT(i)))
  1340. continue;
  1341. if (bits & BIT(i))
  1342. pe->tcam.byte[ai_idx] |= 1 << i;
  1343. else
  1344. pe->tcam.byte[ai_idx] &= ~(1 << i);
  1345. }
  1346. pe->tcam.byte[MVPP2_PRS_TCAM_EN_OFFS(ai_idx)] |= enable;
  1347. }
  1348. /* Get ai bits from tcam sw entry */
  1349. static int mvpp2_prs_tcam_ai_get(struct mvpp2_prs_entry *pe)
  1350. {
  1351. return pe->tcam.byte[MVPP2_PRS_TCAM_AI_BYTE];
  1352. }
  1353. /* Set ethertype in tcam sw entry */
  1354. static void mvpp2_prs_match_etype(struct mvpp2_prs_entry *pe, int offset,
  1355. unsigned short ethertype)
  1356. {
  1357. mvpp2_prs_tcam_data_byte_set(pe, offset + 0, ethertype >> 8, 0xff);
  1358. mvpp2_prs_tcam_data_byte_set(pe, offset + 1, ethertype & 0xff, 0xff);
  1359. }
  1360. /* Set bits in sram sw entry */
  1361. static void mvpp2_prs_sram_bits_set(struct mvpp2_prs_entry *pe, int bit_num,
  1362. int val)
  1363. {
  1364. pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] |= (val << (bit_num % 8));
  1365. }
  1366. /* Clear bits in sram sw entry */
  1367. static void mvpp2_prs_sram_bits_clear(struct mvpp2_prs_entry *pe, int bit_num,
  1368. int val)
  1369. {
  1370. pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] &= ~(val << (bit_num % 8));
  1371. }
  1372. /* Update ri bits in sram sw entry */
  1373. static void mvpp2_prs_sram_ri_update(struct mvpp2_prs_entry *pe,
  1374. unsigned int bits, unsigned int mask)
  1375. {
  1376. unsigned int i;
  1377. for (i = 0; i < MVPP2_PRS_SRAM_RI_CTRL_BITS; i++) {
  1378. int ri_off = MVPP2_PRS_SRAM_RI_OFFS;
  1379. if (!(mask & BIT(i)))
  1380. continue;
  1381. if (bits & BIT(i))
  1382. mvpp2_prs_sram_bits_set(pe, ri_off + i, 1);
  1383. else
  1384. mvpp2_prs_sram_bits_clear(pe, ri_off + i, 1);
  1385. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_RI_CTRL_OFFS + i, 1);
  1386. }
  1387. }
  1388. /* Obtain ri bits from sram sw entry */
  1389. static int mvpp2_prs_sram_ri_get(struct mvpp2_prs_entry *pe)
  1390. {
  1391. return pe->sram.word[MVPP2_PRS_SRAM_RI_WORD];
  1392. }
  1393. /* Update ai bits in sram sw entry */
  1394. static void mvpp2_prs_sram_ai_update(struct mvpp2_prs_entry *pe,
  1395. unsigned int bits, unsigned int mask)
  1396. {
  1397. unsigned int i;
  1398. int ai_off = MVPP2_PRS_SRAM_AI_OFFS;
  1399. for (i = 0; i < MVPP2_PRS_SRAM_AI_CTRL_BITS; i++) {
  1400. if (!(mask & BIT(i)))
  1401. continue;
  1402. if (bits & BIT(i))
  1403. mvpp2_prs_sram_bits_set(pe, ai_off + i, 1);
  1404. else
  1405. mvpp2_prs_sram_bits_clear(pe, ai_off + i, 1);
  1406. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_AI_CTRL_OFFS + i, 1);
  1407. }
  1408. }
  1409. /* Read ai bits from sram sw entry */
  1410. static int mvpp2_prs_sram_ai_get(struct mvpp2_prs_entry *pe)
  1411. {
  1412. u8 bits;
  1413. int ai_off = MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_AI_OFFS);
  1414. int ai_en_off = ai_off + 1;
  1415. int ai_shift = MVPP2_PRS_SRAM_AI_OFFS % 8;
  1416. bits = (pe->sram.byte[ai_off] >> ai_shift) |
  1417. (pe->sram.byte[ai_en_off] << (8 - ai_shift));
  1418. return bits;
  1419. }
  1420. /* In sram sw entry set lookup ID field of the tcam key to be used in the next
  1421. * lookup interation
  1422. */
  1423. static void mvpp2_prs_sram_next_lu_set(struct mvpp2_prs_entry *pe,
  1424. unsigned int lu)
  1425. {
  1426. int sram_next_off = MVPP2_PRS_SRAM_NEXT_LU_OFFS;
  1427. mvpp2_prs_sram_bits_clear(pe, sram_next_off,
  1428. MVPP2_PRS_SRAM_NEXT_LU_MASK);
  1429. mvpp2_prs_sram_bits_set(pe, sram_next_off, lu);
  1430. }
  1431. /* In the sram sw entry set sign and value of the next lookup offset
  1432. * and the offset value generated to the classifier
  1433. */
  1434. static void mvpp2_prs_sram_shift_set(struct mvpp2_prs_entry *pe, int shift,
  1435. unsigned int op)
  1436. {
  1437. /* Set sign */
  1438. if (shift < 0) {
  1439. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
  1440. shift = 0 - shift;
  1441. } else {
  1442. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
  1443. }
  1444. /* Set value */
  1445. pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_SHIFT_OFFS)] =
  1446. (unsigned char)shift;
  1447. /* Reset and set operation */
  1448. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS,
  1449. MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK);
  1450. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS, op);
  1451. /* Set base offset as current */
  1452. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
  1453. }
  1454. /* In the sram sw entry set sign and value of the user defined offset
  1455. * generated to the classifier
  1456. */
  1457. static void mvpp2_prs_sram_offset_set(struct mvpp2_prs_entry *pe,
  1458. unsigned int type, int offset,
  1459. unsigned int op)
  1460. {
  1461. /* Set sign */
  1462. if (offset < 0) {
  1463. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
  1464. offset = 0 - offset;
  1465. } else {
  1466. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
  1467. }
  1468. /* Set value */
  1469. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_OFFS,
  1470. MVPP2_PRS_SRAM_UDF_MASK);
  1471. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_OFFS, offset);
  1472. pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
  1473. MVPP2_PRS_SRAM_UDF_BITS)] &=
  1474. ~(MVPP2_PRS_SRAM_UDF_MASK >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
  1475. pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
  1476. MVPP2_PRS_SRAM_UDF_BITS)] |=
  1477. (offset >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
  1478. /* Set offset type */
  1479. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS,
  1480. MVPP2_PRS_SRAM_UDF_TYPE_MASK);
  1481. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS, type);
  1482. /* Set offset operation */
  1483. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS,
  1484. MVPP2_PRS_SRAM_OP_SEL_UDF_MASK);
  1485. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS, op);
  1486. pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
  1487. MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] &=
  1488. ~(MVPP2_PRS_SRAM_OP_SEL_UDF_MASK >>
  1489. (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
  1490. pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
  1491. MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] |=
  1492. (op >> (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
  1493. /* Set base offset as current */
  1494. mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
  1495. }
  1496. /* Find parser flow entry */
  1497. static struct mvpp2_prs_entry *mvpp2_prs_flow_find(struct mvpp2 *priv, int flow)
  1498. {
  1499. struct mvpp2_prs_entry *pe;
  1500. int tid;
  1501. pe = kzalloc(sizeof(*pe), GFP_KERNEL);
  1502. if (!pe)
  1503. return NULL;
  1504. mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
  1505. /* Go through the all entires with MVPP2_PRS_LU_FLOWS */
  1506. for (tid = MVPP2_PRS_TCAM_SRAM_SIZE - 1; tid >= 0; tid--) {
  1507. u8 bits;
  1508. if (!priv->prs_shadow[tid].valid ||
  1509. priv->prs_shadow[tid].lu != MVPP2_PRS_LU_FLOWS)
  1510. continue;
  1511. pe->index = tid;
  1512. mvpp2_prs_hw_read(priv, pe);
  1513. bits = mvpp2_prs_sram_ai_get(pe);
  1514. /* Sram store classification lookup ID in AI bits [5:0] */
  1515. if ((bits & MVPP2_PRS_FLOW_ID_MASK) == flow)
  1516. return pe;
  1517. }
  1518. kfree(pe);
  1519. return NULL;
  1520. }
  1521. /* Return first free tcam index, seeking from start to end */
  1522. static int mvpp2_prs_tcam_first_free(struct mvpp2 *priv, unsigned char start,
  1523. unsigned char end)
  1524. {
  1525. int tid;
  1526. if (start > end)
  1527. swap(start, end);
  1528. if (end >= MVPP2_PRS_TCAM_SRAM_SIZE)
  1529. end = MVPP2_PRS_TCAM_SRAM_SIZE - 1;
  1530. for (tid = start; tid <= end; tid++) {
  1531. if (!priv->prs_shadow[tid].valid)
  1532. return tid;
  1533. }
  1534. return -EINVAL;
  1535. }
  1536. /* Enable/disable dropping all mac da's */
  1537. static void mvpp2_prs_mac_drop_all_set(struct mvpp2 *priv, int port, bool add)
  1538. {
  1539. struct mvpp2_prs_entry pe;
  1540. if (priv->prs_shadow[MVPP2_PE_DROP_ALL].valid) {
  1541. /* Entry exist - update port only */
  1542. pe.index = MVPP2_PE_DROP_ALL;
  1543. mvpp2_prs_hw_read(priv, &pe);
  1544. } else {
  1545. /* Entry doesn't exist - create new */
  1546. memset(&pe, 0, sizeof(pe));
  1547. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
  1548. pe.index = MVPP2_PE_DROP_ALL;
  1549. /* Non-promiscuous mode for all ports - DROP unknown packets */
  1550. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
  1551. MVPP2_PRS_RI_DROP_MASK);
  1552. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  1553. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  1554. /* Update shadow table */
  1555. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
  1556. /* Mask all ports */
  1557. mvpp2_prs_tcam_port_map_set(&pe, 0);
  1558. }
  1559. /* Update port mask */
  1560. mvpp2_prs_tcam_port_set(&pe, port, add);
  1561. mvpp2_prs_hw_write(priv, &pe);
  1562. }
  1563. /* Set port to promiscuous mode */
  1564. static void mvpp2_prs_mac_promisc_set(struct mvpp2 *priv, int port, bool add)
  1565. {
  1566. struct mvpp2_prs_entry pe;
  1567. /* Promiscuous mode - Accept unknown packets */
  1568. if (priv->prs_shadow[MVPP2_PE_MAC_PROMISCUOUS].valid) {
  1569. /* Entry exist - update port only */
  1570. pe.index = MVPP2_PE_MAC_PROMISCUOUS;
  1571. mvpp2_prs_hw_read(priv, &pe);
  1572. } else {
  1573. /* Entry doesn't exist - create new */
  1574. memset(&pe, 0, sizeof(pe));
  1575. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
  1576. pe.index = MVPP2_PE_MAC_PROMISCUOUS;
  1577. /* Continue - set next lookup */
  1578. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
  1579. /* Set result info bits */
  1580. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_UCAST,
  1581. MVPP2_PRS_RI_L2_CAST_MASK);
  1582. /* Shift to ethertype */
  1583. mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
  1584. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1585. /* Mask all ports */
  1586. mvpp2_prs_tcam_port_map_set(&pe, 0);
  1587. /* Update shadow table */
  1588. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
  1589. }
  1590. /* Update port mask */
  1591. mvpp2_prs_tcam_port_set(&pe, port, add);
  1592. mvpp2_prs_hw_write(priv, &pe);
  1593. }
  1594. /* Accept multicast */
  1595. static void mvpp2_prs_mac_multi_set(struct mvpp2 *priv, int port, int index,
  1596. bool add)
  1597. {
  1598. struct mvpp2_prs_entry pe;
  1599. unsigned char da_mc;
  1600. /* Ethernet multicast address first byte is
  1601. * 0x01 for IPv4 and 0x33 for IPv6
  1602. */
  1603. da_mc = (index == MVPP2_PE_MAC_MC_ALL) ? 0x01 : 0x33;
  1604. if (priv->prs_shadow[index].valid) {
  1605. /* Entry exist - update port only */
  1606. pe.index = index;
  1607. mvpp2_prs_hw_read(priv, &pe);
  1608. } else {
  1609. /* Entry doesn't exist - create new */
  1610. memset(&pe, 0, sizeof(pe));
  1611. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
  1612. pe.index = index;
  1613. /* Continue - set next lookup */
  1614. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
  1615. /* Set result info bits */
  1616. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_MCAST,
  1617. MVPP2_PRS_RI_L2_CAST_MASK);
  1618. /* Update tcam entry data first byte */
  1619. mvpp2_prs_tcam_data_byte_set(&pe, 0, da_mc, 0xff);
  1620. /* Shift to ethertype */
  1621. mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
  1622. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1623. /* Mask all ports */
  1624. mvpp2_prs_tcam_port_map_set(&pe, 0);
  1625. /* Update shadow table */
  1626. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
  1627. }
  1628. /* Update port mask */
  1629. mvpp2_prs_tcam_port_set(&pe, port, add);
  1630. mvpp2_prs_hw_write(priv, &pe);
  1631. }
  1632. /* Set entry for dsa packets */
  1633. static void mvpp2_prs_dsa_tag_set(struct mvpp2 *priv, int port, bool add,
  1634. bool tagged, bool extend)
  1635. {
  1636. struct mvpp2_prs_entry pe;
  1637. int tid, shift;
  1638. if (extend) {
  1639. tid = tagged ? MVPP2_PE_EDSA_TAGGED : MVPP2_PE_EDSA_UNTAGGED;
  1640. shift = 8;
  1641. } else {
  1642. tid = tagged ? MVPP2_PE_DSA_TAGGED : MVPP2_PE_DSA_UNTAGGED;
  1643. shift = 4;
  1644. }
  1645. if (priv->prs_shadow[tid].valid) {
  1646. /* Entry exist - update port only */
  1647. pe.index = tid;
  1648. mvpp2_prs_hw_read(priv, &pe);
  1649. } else {
  1650. /* Entry doesn't exist - create new */
  1651. memset(&pe, 0, sizeof(pe));
  1652. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
  1653. pe.index = tid;
  1654. /* Shift 4 bytes if DSA tag or 8 bytes in case of EDSA tag*/
  1655. mvpp2_prs_sram_shift_set(&pe, shift,
  1656. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1657. /* Update shadow table */
  1658. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
  1659. if (tagged) {
  1660. /* Set tagged bit in DSA tag */
  1661. mvpp2_prs_tcam_data_byte_set(&pe, 0,
  1662. MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
  1663. MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
  1664. /* Clear all ai bits for next iteration */
  1665. mvpp2_prs_sram_ai_update(&pe, 0,
  1666. MVPP2_PRS_SRAM_AI_MASK);
  1667. /* If packet is tagged continue check vlans */
  1668. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
  1669. } else {
  1670. /* Set result info bits to 'no vlans' */
  1671. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
  1672. MVPP2_PRS_RI_VLAN_MASK);
  1673. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
  1674. }
  1675. /* Mask all ports */
  1676. mvpp2_prs_tcam_port_map_set(&pe, 0);
  1677. }
  1678. /* Update port mask */
  1679. mvpp2_prs_tcam_port_set(&pe, port, add);
  1680. mvpp2_prs_hw_write(priv, &pe);
  1681. }
  1682. /* Set entry for dsa ethertype */
  1683. static void mvpp2_prs_dsa_tag_ethertype_set(struct mvpp2 *priv, int port,
  1684. bool add, bool tagged, bool extend)
  1685. {
  1686. struct mvpp2_prs_entry pe;
  1687. int tid, shift, port_mask;
  1688. if (extend) {
  1689. tid = tagged ? MVPP2_PE_ETYPE_EDSA_TAGGED :
  1690. MVPP2_PE_ETYPE_EDSA_UNTAGGED;
  1691. port_mask = 0;
  1692. shift = 8;
  1693. } else {
  1694. tid = tagged ? MVPP2_PE_ETYPE_DSA_TAGGED :
  1695. MVPP2_PE_ETYPE_DSA_UNTAGGED;
  1696. port_mask = MVPP2_PRS_PORT_MASK;
  1697. shift = 4;
  1698. }
  1699. if (priv->prs_shadow[tid].valid) {
  1700. /* Entry exist - update port only */
  1701. pe.index = tid;
  1702. mvpp2_prs_hw_read(priv, &pe);
  1703. } else {
  1704. /* Entry doesn't exist - create new */
  1705. memset(&pe, 0, sizeof(pe));
  1706. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
  1707. pe.index = tid;
  1708. /* Set ethertype */
  1709. mvpp2_prs_match_etype(&pe, 0, ETH_P_EDSA);
  1710. mvpp2_prs_match_etype(&pe, 2, 0);
  1711. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DSA_MASK,
  1712. MVPP2_PRS_RI_DSA_MASK);
  1713. /* Shift ethertype + 2 byte reserved + tag*/
  1714. mvpp2_prs_sram_shift_set(&pe, 2 + MVPP2_ETH_TYPE_LEN + shift,
  1715. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1716. /* Update shadow table */
  1717. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
  1718. if (tagged) {
  1719. /* Set tagged bit in DSA tag */
  1720. mvpp2_prs_tcam_data_byte_set(&pe,
  1721. MVPP2_ETH_TYPE_LEN + 2 + 3,
  1722. MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
  1723. MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
  1724. /* Clear all ai bits for next iteration */
  1725. mvpp2_prs_sram_ai_update(&pe, 0,
  1726. MVPP2_PRS_SRAM_AI_MASK);
  1727. /* If packet is tagged continue check vlans */
  1728. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
  1729. } else {
  1730. /* Set result info bits to 'no vlans' */
  1731. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
  1732. MVPP2_PRS_RI_VLAN_MASK);
  1733. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
  1734. }
  1735. /* Mask/unmask all ports, depending on dsa type */
  1736. mvpp2_prs_tcam_port_map_set(&pe, port_mask);
  1737. }
  1738. /* Update port mask */
  1739. mvpp2_prs_tcam_port_set(&pe, port, add);
  1740. mvpp2_prs_hw_write(priv, &pe);
  1741. }
  1742. /* Search for existing single/triple vlan entry */
  1743. static struct mvpp2_prs_entry *mvpp2_prs_vlan_find(struct mvpp2 *priv,
  1744. unsigned short tpid, int ai)
  1745. {
  1746. struct mvpp2_prs_entry *pe;
  1747. int tid;
  1748. pe = kzalloc(sizeof(*pe), GFP_KERNEL);
  1749. if (!pe)
  1750. return NULL;
  1751. mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
  1752. /* Go through the all entries with MVPP2_PRS_LU_VLAN */
  1753. for (tid = MVPP2_PE_FIRST_FREE_TID;
  1754. tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
  1755. unsigned int ri_bits, ai_bits;
  1756. bool match;
  1757. if (!priv->prs_shadow[tid].valid ||
  1758. priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
  1759. continue;
  1760. pe->index = tid;
  1761. mvpp2_prs_hw_read(priv, pe);
  1762. match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid));
  1763. if (!match)
  1764. continue;
  1765. /* Get vlan type */
  1766. ri_bits = mvpp2_prs_sram_ri_get(pe);
  1767. ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
  1768. /* Get current ai value from tcam */
  1769. ai_bits = mvpp2_prs_tcam_ai_get(pe);
  1770. /* Clear double vlan bit */
  1771. ai_bits &= ~MVPP2_PRS_DBL_VLAN_AI_BIT;
  1772. if (ai != ai_bits)
  1773. continue;
  1774. if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
  1775. ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
  1776. return pe;
  1777. }
  1778. kfree(pe);
  1779. return NULL;
  1780. }
  1781. /* Add/update single/triple vlan entry */
  1782. static int mvpp2_prs_vlan_add(struct mvpp2 *priv, unsigned short tpid, int ai,
  1783. unsigned int port_map)
  1784. {
  1785. struct mvpp2_prs_entry *pe;
  1786. int tid_aux, tid;
  1787. int ret = 0;
  1788. pe = mvpp2_prs_vlan_find(priv, tpid, ai);
  1789. if (!pe) {
  1790. /* Create new tcam entry */
  1791. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_LAST_FREE_TID,
  1792. MVPP2_PE_FIRST_FREE_TID);
  1793. if (tid < 0)
  1794. return tid;
  1795. pe = kzalloc(sizeof(*pe), GFP_KERNEL);
  1796. if (!pe)
  1797. return -ENOMEM;
  1798. /* Get last double vlan tid */
  1799. for (tid_aux = MVPP2_PE_LAST_FREE_TID;
  1800. tid_aux >= MVPP2_PE_FIRST_FREE_TID; tid_aux--) {
  1801. unsigned int ri_bits;
  1802. if (!priv->prs_shadow[tid_aux].valid ||
  1803. priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
  1804. continue;
  1805. pe->index = tid_aux;
  1806. mvpp2_prs_hw_read(priv, pe);
  1807. ri_bits = mvpp2_prs_sram_ri_get(pe);
  1808. if ((ri_bits & MVPP2_PRS_RI_VLAN_MASK) ==
  1809. MVPP2_PRS_RI_VLAN_DOUBLE)
  1810. break;
  1811. }
  1812. if (tid <= tid_aux) {
  1813. ret = -EINVAL;
  1814. goto free_pe;
  1815. }
  1816. memset(pe, 0, sizeof(*pe));
  1817. mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
  1818. pe->index = tid;
  1819. mvpp2_prs_match_etype(pe, 0, tpid);
  1820. mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_L2);
  1821. /* Shift 4 bytes - skip 1 vlan tag */
  1822. mvpp2_prs_sram_shift_set(pe, MVPP2_VLAN_TAG_LEN,
  1823. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1824. /* Clear all ai bits for next iteration */
  1825. mvpp2_prs_sram_ai_update(pe, 0, MVPP2_PRS_SRAM_AI_MASK);
  1826. if (ai == MVPP2_PRS_SINGLE_VLAN_AI) {
  1827. mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_SINGLE,
  1828. MVPP2_PRS_RI_VLAN_MASK);
  1829. } else {
  1830. ai |= MVPP2_PRS_DBL_VLAN_AI_BIT;
  1831. mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_TRIPLE,
  1832. MVPP2_PRS_RI_VLAN_MASK);
  1833. }
  1834. mvpp2_prs_tcam_ai_update(pe, ai, MVPP2_PRS_SRAM_AI_MASK);
  1835. mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
  1836. }
  1837. /* Update ports' mask */
  1838. mvpp2_prs_tcam_port_map_set(pe, port_map);
  1839. mvpp2_prs_hw_write(priv, pe);
  1840. free_pe:
  1841. kfree(pe);
  1842. return ret;
  1843. }
  1844. /* Get first free double vlan ai number */
  1845. static int mvpp2_prs_double_vlan_ai_free_get(struct mvpp2 *priv)
  1846. {
  1847. int i;
  1848. for (i = 1; i < MVPP2_PRS_DBL_VLANS_MAX; i++) {
  1849. if (!priv->prs_double_vlans[i])
  1850. return i;
  1851. }
  1852. return -EINVAL;
  1853. }
  1854. /* Search for existing double vlan entry */
  1855. static struct mvpp2_prs_entry *mvpp2_prs_double_vlan_find(struct mvpp2 *priv,
  1856. unsigned short tpid1,
  1857. unsigned short tpid2)
  1858. {
  1859. struct mvpp2_prs_entry *pe;
  1860. int tid;
  1861. pe = kzalloc(sizeof(*pe), GFP_KERNEL);
  1862. if (!pe)
  1863. return NULL;
  1864. mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
  1865. /* Go through the all entries with MVPP2_PRS_LU_VLAN */
  1866. for (tid = MVPP2_PE_FIRST_FREE_TID;
  1867. tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
  1868. unsigned int ri_mask;
  1869. bool match;
  1870. if (!priv->prs_shadow[tid].valid ||
  1871. priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
  1872. continue;
  1873. pe->index = tid;
  1874. mvpp2_prs_hw_read(priv, pe);
  1875. match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid1))
  1876. && mvpp2_prs_tcam_data_cmp(pe, 4, swab16(tpid2));
  1877. if (!match)
  1878. continue;
  1879. ri_mask = mvpp2_prs_sram_ri_get(pe) & MVPP2_PRS_RI_VLAN_MASK;
  1880. if (ri_mask == MVPP2_PRS_RI_VLAN_DOUBLE)
  1881. return pe;
  1882. }
  1883. kfree(pe);
  1884. return NULL;
  1885. }
  1886. /* Add or update double vlan entry */
  1887. static int mvpp2_prs_double_vlan_add(struct mvpp2 *priv, unsigned short tpid1,
  1888. unsigned short tpid2,
  1889. unsigned int port_map)
  1890. {
  1891. struct mvpp2_prs_entry *pe;
  1892. int tid_aux, tid, ai, ret = 0;
  1893. pe = mvpp2_prs_double_vlan_find(priv, tpid1, tpid2);
  1894. if (!pe) {
  1895. /* Create new tcam entry */
  1896. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1897. MVPP2_PE_LAST_FREE_TID);
  1898. if (tid < 0)
  1899. return tid;
  1900. pe = kzalloc(sizeof(*pe), GFP_KERNEL);
  1901. if (!pe)
  1902. return -ENOMEM;
  1903. /* Set ai value for new double vlan entry */
  1904. ai = mvpp2_prs_double_vlan_ai_free_get(priv);
  1905. if (ai < 0) {
  1906. ret = ai;
  1907. goto free_pe;
  1908. }
  1909. /* Get first single/triple vlan tid */
  1910. for (tid_aux = MVPP2_PE_FIRST_FREE_TID;
  1911. tid_aux <= MVPP2_PE_LAST_FREE_TID; tid_aux++) {
  1912. unsigned int ri_bits;
  1913. if (!priv->prs_shadow[tid_aux].valid ||
  1914. priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
  1915. continue;
  1916. pe->index = tid_aux;
  1917. mvpp2_prs_hw_read(priv, pe);
  1918. ri_bits = mvpp2_prs_sram_ri_get(pe);
  1919. ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
  1920. if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
  1921. ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
  1922. break;
  1923. }
  1924. if (tid >= tid_aux) {
  1925. ret = -ERANGE;
  1926. goto free_pe;
  1927. }
  1928. memset(pe, 0, sizeof(*pe));
  1929. mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
  1930. pe->index = tid;
  1931. priv->prs_double_vlans[ai] = true;
  1932. mvpp2_prs_match_etype(pe, 0, tpid1);
  1933. mvpp2_prs_match_etype(pe, 4, tpid2);
  1934. mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_VLAN);
  1935. /* Shift 8 bytes - skip 2 vlan tags */
  1936. mvpp2_prs_sram_shift_set(pe, 2 * MVPP2_VLAN_TAG_LEN,
  1937. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1938. mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_DOUBLE,
  1939. MVPP2_PRS_RI_VLAN_MASK);
  1940. mvpp2_prs_sram_ai_update(pe, ai | MVPP2_PRS_DBL_VLAN_AI_BIT,
  1941. MVPP2_PRS_SRAM_AI_MASK);
  1942. mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
  1943. }
  1944. /* Update ports' mask */
  1945. mvpp2_prs_tcam_port_map_set(pe, port_map);
  1946. mvpp2_prs_hw_write(priv, pe);
  1947. free_pe:
  1948. kfree(pe);
  1949. return ret;
  1950. }
  1951. /* IPv4 header parsing for fragmentation and L4 offset */
  1952. static int mvpp2_prs_ip4_proto(struct mvpp2 *priv, unsigned short proto,
  1953. unsigned int ri, unsigned int ri_mask)
  1954. {
  1955. struct mvpp2_prs_entry pe;
  1956. int tid;
  1957. if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
  1958. (proto != IPPROTO_IGMP))
  1959. return -EINVAL;
  1960. /* Not fragmented packet */
  1961. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1962. MVPP2_PE_LAST_FREE_TID);
  1963. if (tid < 0)
  1964. return tid;
  1965. memset(&pe, 0, sizeof(pe));
  1966. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
  1967. pe.index = tid;
  1968. /* Set next lu to IPv4 */
  1969. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
  1970. mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  1971. /* Set L4 offset */
  1972. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
  1973. sizeof(struct iphdr) - 4,
  1974. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  1975. mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
  1976. MVPP2_PRS_IPV4_DIP_AI_BIT);
  1977. mvpp2_prs_sram_ri_update(&pe, ri, ri_mask | MVPP2_PRS_RI_IP_FRAG_MASK);
  1978. mvpp2_prs_tcam_data_byte_set(&pe, 2, 0x00,
  1979. MVPP2_PRS_TCAM_PROTO_MASK_L);
  1980. mvpp2_prs_tcam_data_byte_set(&pe, 3, 0x00,
  1981. MVPP2_PRS_TCAM_PROTO_MASK);
  1982. mvpp2_prs_tcam_data_byte_set(&pe, 5, proto, MVPP2_PRS_TCAM_PROTO_MASK);
  1983. mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
  1984. /* Unmask all ports */
  1985. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  1986. /* Update shadow table and hw entry */
  1987. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
  1988. mvpp2_prs_hw_write(priv, &pe);
  1989. /* Fragmented packet */
  1990. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  1991. MVPP2_PE_LAST_FREE_TID);
  1992. if (tid < 0)
  1993. return tid;
  1994. pe.index = tid;
  1995. /* Clear ri before updating */
  1996. pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
  1997. pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
  1998. mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
  1999. mvpp2_prs_sram_ri_update(&pe, ri | MVPP2_PRS_RI_IP_FRAG_TRUE,
  2000. ri_mask | MVPP2_PRS_RI_IP_FRAG_MASK);
  2001. mvpp2_prs_tcam_data_byte_set(&pe, 2, 0x00, 0x0);
  2002. mvpp2_prs_tcam_data_byte_set(&pe, 3, 0x00, 0x0);
  2003. /* Update shadow table and hw entry */
  2004. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
  2005. mvpp2_prs_hw_write(priv, &pe);
  2006. return 0;
  2007. }
  2008. /* IPv4 L3 multicast or broadcast */
  2009. static int mvpp2_prs_ip4_cast(struct mvpp2 *priv, unsigned short l3_cast)
  2010. {
  2011. struct mvpp2_prs_entry pe;
  2012. int mask, tid;
  2013. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  2014. MVPP2_PE_LAST_FREE_TID);
  2015. if (tid < 0)
  2016. return tid;
  2017. memset(&pe, 0, sizeof(pe));
  2018. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
  2019. pe.index = tid;
  2020. switch (l3_cast) {
  2021. case MVPP2_PRS_L3_MULTI_CAST:
  2022. mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV4_MC,
  2023. MVPP2_PRS_IPV4_MC_MASK);
  2024. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
  2025. MVPP2_PRS_RI_L3_ADDR_MASK);
  2026. break;
  2027. case MVPP2_PRS_L3_BROAD_CAST:
  2028. mask = MVPP2_PRS_IPV4_BC_MASK;
  2029. mvpp2_prs_tcam_data_byte_set(&pe, 0, mask, mask);
  2030. mvpp2_prs_tcam_data_byte_set(&pe, 1, mask, mask);
  2031. mvpp2_prs_tcam_data_byte_set(&pe, 2, mask, mask);
  2032. mvpp2_prs_tcam_data_byte_set(&pe, 3, mask, mask);
  2033. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_BCAST,
  2034. MVPP2_PRS_RI_L3_ADDR_MASK);
  2035. break;
  2036. default:
  2037. return -EINVAL;
  2038. }
  2039. /* Finished: go to flowid generation */
  2040. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  2041. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  2042. mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
  2043. MVPP2_PRS_IPV4_DIP_AI_BIT);
  2044. /* Unmask all ports */
  2045. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  2046. /* Update shadow table and hw entry */
  2047. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
  2048. mvpp2_prs_hw_write(priv, &pe);
  2049. return 0;
  2050. }
  2051. /* Set entries for protocols over IPv6 */
  2052. static int mvpp2_prs_ip6_proto(struct mvpp2 *priv, unsigned short proto,
  2053. unsigned int ri, unsigned int ri_mask)
  2054. {
  2055. struct mvpp2_prs_entry pe;
  2056. int tid;
  2057. if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
  2058. (proto != IPPROTO_ICMPV6) && (proto != IPPROTO_IPIP))
  2059. return -EINVAL;
  2060. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  2061. MVPP2_PE_LAST_FREE_TID);
  2062. if (tid < 0)
  2063. return tid;
  2064. memset(&pe, 0, sizeof(pe));
  2065. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
  2066. pe.index = tid;
  2067. /* Finished: go to flowid generation */
  2068. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  2069. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  2070. mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
  2071. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
  2072. sizeof(struct ipv6hdr) - 6,
  2073. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  2074. mvpp2_prs_tcam_data_byte_set(&pe, 0, proto, MVPP2_PRS_TCAM_PROTO_MASK);
  2075. mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
  2076. MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
  2077. /* Unmask all ports */
  2078. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  2079. /* Write HW */
  2080. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
  2081. mvpp2_prs_hw_write(priv, &pe);
  2082. return 0;
  2083. }
  2084. /* IPv6 L3 multicast entry */
  2085. static int mvpp2_prs_ip6_cast(struct mvpp2 *priv, unsigned short l3_cast)
  2086. {
  2087. struct mvpp2_prs_entry pe;
  2088. int tid;
  2089. if (l3_cast != MVPP2_PRS_L3_MULTI_CAST)
  2090. return -EINVAL;
  2091. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  2092. MVPP2_PE_LAST_FREE_TID);
  2093. if (tid < 0)
  2094. return tid;
  2095. memset(&pe, 0, sizeof(pe));
  2096. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
  2097. pe.index = tid;
  2098. /* Finished: go to flowid generation */
  2099. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
  2100. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
  2101. MVPP2_PRS_RI_L3_ADDR_MASK);
  2102. mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
  2103. MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
  2104. /* Shift back to IPv6 NH */
  2105. mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  2106. mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV6_MC,
  2107. MVPP2_PRS_IPV6_MC_MASK);
  2108. mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
  2109. /* Unmask all ports */
  2110. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  2111. /* Update shadow table and hw entry */
  2112. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
  2113. mvpp2_prs_hw_write(priv, &pe);
  2114. return 0;
  2115. }
  2116. /* Parser per-port initialization */
  2117. static void mvpp2_prs_hw_port_init(struct mvpp2 *priv, int port, int lu_first,
  2118. int lu_max, int offset)
  2119. {
  2120. u32 val;
  2121. /* Set lookup ID */
  2122. val = mvpp2_read(priv, MVPP2_PRS_INIT_LOOKUP_REG);
  2123. val &= ~MVPP2_PRS_PORT_LU_MASK(port);
  2124. val |= MVPP2_PRS_PORT_LU_VAL(port, lu_first);
  2125. mvpp2_write(priv, MVPP2_PRS_INIT_LOOKUP_REG, val);
  2126. /* Set maximum number of loops for packet received from port */
  2127. val = mvpp2_read(priv, MVPP2_PRS_MAX_LOOP_REG(port));
  2128. val &= ~MVPP2_PRS_MAX_LOOP_MASK(port);
  2129. val |= MVPP2_PRS_MAX_LOOP_VAL(port, lu_max);
  2130. mvpp2_write(priv, MVPP2_PRS_MAX_LOOP_REG(port), val);
  2131. /* Set initial offset for packet header extraction for the first
  2132. * searching loop
  2133. */
  2134. val = mvpp2_read(priv, MVPP2_PRS_INIT_OFFS_REG(port));
  2135. val &= ~MVPP2_PRS_INIT_OFF_MASK(port);
  2136. val |= MVPP2_PRS_INIT_OFF_VAL(port, offset);
  2137. mvpp2_write(priv, MVPP2_PRS_INIT_OFFS_REG(port), val);
  2138. }
  2139. /* Default flow entries initialization for all ports */
  2140. static void mvpp2_prs_def_flow_init(struct mvpp2 *priv)
  2141. {
  2142. struct mvpp2_prs_entry pe;
  2143. int port;
  2144. for (port = 0; port < MVPP2_MAX_PORTS; port++) {
  2145. memset(&pe, 0, sizeof(pe));
  2146. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  2147. pe.index = MVPP2_PE_FIRST_DEFAULT_FLOW - port;
  2148. /* Mask all ports */
  2149. mvpp2_prs_tcam_port_map_set(&pe, 0);
  2150. /* Set flow ID*/
  2151. mvpp2_prs_sram_ai_update(&pe, port, MVPP2_PRS_FLOW_ID_MASK);
  2152. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
  2153. /* Update shadow table and hw entry */
  2154. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_FLOWS);
  2155. mvpp2_prs_hw_write(priv, &pe);
  2156. }
  2157. }
  2158. /* Set default entry for Marvell Header field */
  2159. static void mvpp2_prs_mh_init(struct mvpp2 *priv)
  2160. {
  2161. struct mvpp2_prs_entry pe;
  2162. memset(&pe, 0, sizeof(pe));
  2163. pe.index = MVPP2_PE_MH_DEFAULT;
  2164. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MH);
  2165. mvpp2_prs_sram_shift_set(&pe, MVPP2_MH_SIZE,
  2166. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  2167. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_MAC);
  2168. /* Unmask all ports */
  2169. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  2170. /* Update shadow table and hw entry */
  2171. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MH);
  2172. mvpp2_prs_hw_write(priv, &pe);
  2173. }
  2174. /* Set default entires (place holder) for promiscuous, non-promiscuous and
  2175. * multicast MAC addresses
  2176. */
  2177. static void mvpp2_prs_mac_init(struct mvpp2 *priv)
  2178. {
  2179. struct mvpp2_prs_entry pe;
  2180. memset(&pe, 0, sizeof(pe));
  2181. /* Non-promiscuous mode for all ports - DROP unknown packets */
  2182. pe.index = MVPP2_PE_MAC_NON_PROMISCUOUS;
  2183. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
  2184. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
  2185. MVPP2_PRS_RI_DROP_MASK);
  2186. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  2187. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  2188. /* Unmask all ports */
  2189. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  2190. /* Update shadow table and hw entry */
  2191. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
  2192. mvpp2_prs_hw_write(priv, &pe);
  2193. /* place holders only - no ports */
  2194. mvpp2_prs_mac_drop_all_set(priv, 0, false);
  2195. mvpp2_prs_mac_promisc_set(priv, 0, false);
  2196. mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_ALL, 0, false);
  2197. mvpp2_prs_mac_multi_set(priv, MVPP2_PE_MAC_MC_IP6, 0, false);
  2198. }
  2199. /* Set default entries for various types of dsa packets */
  2200. static void mvpp2_prs_dsa_init(struct mvpp2 *priv)
  2201. {
  2202. struct mvpp2_prs_entry pe;
  2203. /* None tagged EDSA entry - place holder */
  2204. mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
  2205. MVPP2_PRS_EDSA);
  2206. /* Tagged EDSA entry - place holder */
  2207. mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
  2208. /* None tagged DSA entry - place holder */
  2209. mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
  2210. MVPP2_PRS_DSA);
  2211. /* Tagged DSA entry - place holder */
  2212. mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
  2213. /* None tagged EDSA ethertype entry - place holder*/
  2214. mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
  2215. MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
  2216. /* Tagged EDSA ethertype entry - place holder*/
  2217. mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
  2218. MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
  2219. /* None tagged DSA ethertype entry */
  2220. mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
  2221. MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
  2222. /* Tagged DSA ethertype entry */
  2223. mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
  2224. MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
  2225. /* Set default entry, in case DSA or EDSA tag not found */
  2226. memset(&pe, 0, sizeof(pe));
  2227. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
  2228. pe.index = MVPP2_PE_DSA_DEFAULT;
  2229. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
  2230. /* Shift 0 bytes */
  2231. mvpp2_prs_sram_shift_set(&pe, 0, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  2232. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
  2233. /* Clear all sram ai bits for next iteration */
  2234. mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
  2235. /* Unmask all ports */
  2236. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  2237. mvpp2_prs_hw_write(priv, &pe);
  2238. }
  2239. /* Match basic ethertypes */
  2240. static int mvpp2_prs_etype_init(struct mvpp2 *priv)
  2241. {
  2242. struct mvpp2_prs_entry pe;
  2243. int tid;
  2244. /* Ethertype: PPPoE */
  2245. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  2246. MVPP2_PE_LAST_FREE_TID);
  2247. if (tid < 0)
  2248. return tid;
  2249. memset(&pe, 0, sizeof(pe));
  2250. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  2251. pe.index = tid;
  2252. mvpp2_prs_match_etype(&pe, 0, ETH_P_PPP_SES);
  2253. mvpp2_prs_sram_shift_set(&pe, MVPP2_PPPOE_HDR_SIZE,
  2254. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  2255. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
  2256. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_PPPOE_MASK,
  2257. MVPP2_PRS_RI_PPPOE_MASK);
  2258. /* Update shadow table and hw entry */
  2259. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  2260. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  2261. priv->prs_shadow[pe.index].finish = false;
  2262. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_PPPOE_MASK,
  2263. MVPP2_PRS_RI_PPPOE_MASK);
  2264. mvpp2_prs_hw_write(priv, &pe);
  2265. /* Ethertype: ARP */
  2266. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  2267. MVPP2_PE_LAST_FREE_TID);
  2268. if (tid < 0)
  2269. return tid;
  2270. memset(&pe, 0, sizeof(pe));
  2271. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  2272. pe.index = tid;
  2273. mvpp2_prs_match_etype(&pe, 0, ETH_P_ARP);
  2274. /* Generate flow in the next iteration*/
  2275. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  2276. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  2277. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_ARP,
  2278. MVPP2_PRS_RI_L3_PROTO_MASK);
  2279. /* Set L3 offset */
  2280. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  2281. MVPP2_ETH_TYPE_LEN,
  2282. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  2283. /* Update shadow table and hw entry */
  2284. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  2285. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  2286. priv->prs_shadow[pe.index].finish = true;
  2287. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_ARP,
  2288. MVPP2_PRS_RI_L3_PROTO_MASK);
  2289. mvpp2_prs_hw_write(priv, &pe);
  2290. /* Ethertype: LBTD */
  2291. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  2292. MVPP2_PE_LAST_FREE_TID);
  2293. if (tid < 0)
  2294. return tid;
  2295. memset(&pe, 0, sizeof(pe));
  2296. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  2297. pe.index = tid;
  2298. mvpp2_prs_match_etype(&pe, 0, MVPP2_IP_LBDT_TYPE);
  2299. /* Generate flow in the next iteration*/
  2300. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  2301. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  2302. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
  2303. MVPP2_PRS_RI_UDF3_RX_SPECIAL,
  2304. MVPP2_PRS_RI_CPU_CODE_MASK |
  2305. MVPP2_PRS_RI_UDF3_MASK);
  2306. /* Set L3 offset */
  2307. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  2308. MVPP2_ETH_TYPE_LEN,
  2309. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  2310. /* Update shadow table and hw entry */
  2311. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  2312. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  2313. priv->prs_shadow[pe.index].finish = true;
  2314. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
  2315. MVPP2_PRS_RI_UDF3_RX_SPECIAL,
  2316. MVPP2_PRS_RI_CPU_CODE_MASK |
  2317. MVPP2_PRS_RI_UDF3_MASK);
  2318. mvpp2_prs_hw_write(priv, &pe);
  2319. /* Ethertype: IPv4 without options */
  2320. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  2321. MVPP2_PE_LAST_FREE_TID);
  2322. if (tid < 0)
  2323. return tid;
  2324. memset(&pe, 0, sizeof(pe));
  2325. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  2326. pe.index = tid;
  2327. mvpp2_prs_match_etype(&pe, 0, ETH_P_IP);
  2328. mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
  2329. MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
  2330. MVPP2_PRS_IPV4_HEAD_MASK |
  2331. MVPP2_PRS_IPV4_IHL_MASK);
  2332. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
  2333. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
  2334. MVPP2_PRS_RI_L3_PROTO_MASK);
  2335. /* Skip eth_type + 4 bytes of IP header */
  2336. mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
  2337. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  2338. /* Set L3 offset */
  2339. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  2340. MVPP2_ETH_TYPE_LEN,
  2341. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  2342. /* Update shadow table and hw entry */
  2343. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  2344. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  2345. priv->prs_shadow[pe.index].finish = false;
  2346. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4,
  2347. MVPP2_PRS_RI_L3_PROTO_MASK);
  2348. mvpp2_prs_hw_write(priv, &pe);
  2349. /* Ethertype: IPv4 with options */
  2350. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  2351. MVPP2_PE_LAST_FREE_TID);
  2352. if (tid < 0)
  2353. return tid;
  2354. pe.index = tid;
  2355. /* Clear tcam data before updating */
  2356. pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(MVPP2_ETH_TYPE_LEN)] = 0x0;
  2357. pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(MVPP2_ETH_TYPE_LEN)] = 0x0;
  2358. mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
  2359. MVPP2_PRS_IPV4_HEAD,
  2360. MVPP2_PRS_IPV4_HEAD_MASK);
  2361. /* Clear ri before updating */
  2362. pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
  2363. pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
  2364. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
  2365. MVPP2_PRS_RI_L3_PROTO_MASK);
  2366. /* Update shadow table and hw entry */
  2367. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  2368. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  2369. priv->prs_shadow[pe.index].finish = false;
  2370. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4_OPT,
  2371. MVPP2_PRS_RI_L3_PROTO_MASK);
  2372. mvpp2_prs_hw_write(priv, &pe);
  2373. /* Ethertype: IPv6 without options */
  2374. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  2375. MVPP2_PE_LAST_FREE_TID);
  2376. if (tid < 0)
  2377. return tid;
  2378. memset(&pe, 0, sizeof(pe));
  2379. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  2380. pe.index = tid;
  2381. mvpp2_prs_match_etype(&pe, 0, ETH_P_IPV6);
  2382. /* Skip DIP of IPV6 header */
  2383. mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 8 +
  2384. MVPP2_MAX_L3_ADDR_SIZE,
  2385. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  2386. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
  2387. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
  2388. MVPP2_PRS_RI_L3_PROTO_MASK);
  2389. /* Set L3 offset */
  2390. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  2391. MVPP2_ETH_TYPE_LEN,
  2392. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  2393. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  2394. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  2395. priv->prs_shadow[pe.index].finish = false;
  2396. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP6,
  2397. MVPP2_PRS_RI_L3_PROTO_MASK);
  2398. mvpp2_prs_hw_write(priv, &pe);
  2399. /* Default entry for MVPP2_PRS_LU_L2 - Unknown ethtype */
  2400. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  2401. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
  2402. pe.index = MVPP2_PE_ETH_TYPE_UN;
  2403. /* Unmask all ports */
  2404. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  2405. /* Generate flow in the next iteration*/
  2406. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  2407. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  2408. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
  2409. MVPP2_PRS_RI_L3_PROTO_MASK);
  2410. /* Set L3 offset even it's unknown L3 */
  2411. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  2412. MVPP2_ETH_TYPE_LEN,
  2413. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  2414. /* Update shadow table and hw entry */
  2415. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
  2416. priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
  2417. priv->prs_shadow[pe.index].finish = true;
  2418. mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_UN,
  2419. MVPP2_PRS_RI_L3_PROTO_MASK);
  2420. mvpp2_prs_hw_write(priv, &pe);
  2421. return 0;
  2422. }
  2423. /* Configure vlan entries and detect up to 2 successive VLAN tags.
  2424. * Possible options:
  2425. * 0x8100, 0x88A8
  2426. * 0x8100, 0x8100
  2427. * 0x8100
  2428. * 0x88A8
  2429. */
  2430. static int mvpp2_prs_vlan_init(struct platform_device *pdev, struct mvpp2 *priv)
  2431. {
  2432. struct mvpp2_prs_entry pe;
  2433. int err;
  2434. priv->prs_double_vlans = devm_kcalloc(&pdev->dev, sizeof(bool),
  2435. MVPP2_PRS_DBL_VLANS_MAX,
  2436. GFP_KERNEL);
  2437. if (!priv->prs_double_vlans)
  2438. return -ENOMEM;
  2439. /* Double VLAN: 0x8100, 0x88A8 */
  2440. err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021AD,
  2441. MVPP2_PRS_PORT_MASK);
  2442. if (err)
  2443. return err;
  2444. /* Double VLAN: 0x8100, 0x8100 */
  2445. err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021Q,
  2446. MVPP2_PRS_PORT_MASK);
  2447. if (err)
  2448. return err;
  2449. /* Single VLAN: 0x88a8 */
  2450. err = mvpp2_prs_vlan_add(priv, ETH_P_8021AD, MVPP2_PRS_SINGLE_VLAN_AI,
  2451. MVPP2_PRS_PORT_MASK);
  2452. if (err)
  2453. return err;
  2454. /* Single VLAN: 0x8100 */
  2455. err = mvpp2_prs_vlan_add(priv, ETH_P_8021Q, MVPP2_PRS_SINGLE_VLAN_AI,
  2456. MVPP2_PRS_PORT_MASK);
  2457. if (err)
  2458. return err;
  2459. /* Set default double vlan entry */
  2460. memset(&pe, 0, sizeof(pe));
  2461. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
  2462. pe.index = MVPP2_PE_VLAN_DBL;
  2463. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
  2464. /* Clear ai for next iterations */
  2465. mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
  2466. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_DOUBLE,
  2467. MVPP2_PRS_RI_VLAN_MASK);
  2468. mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_DBL_VLAN_AI_BIT,
  2469. MVPP2_PRS_DBL_VLAN_AI_BIT);
  2470. /* Unmask all ports */
  2471. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  2472. /* Update shadow table and hw entry */
  2473. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
  2474. mvpp2_prs_hw_write(priv, &pe);
  2475. /* Set default vlan none entry */
  2476. memset(&pe, 0, sizeof(pe));
  2477. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
  2478. pe.index = MVPP2_PE_VLAN_NONE;
  2479. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
  2480. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
  2481. MVPP2_PRS_RI_VLAN_MASK);
  2482. /* Unmask all ports */
  2483. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  2484. /* Update shadow table and hw entry */
  2485. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
  2486. mvpp2_prs_hw_write(priv, &pe);
  2487. return 0;
  2488. }
  2489. /* Set entries for PPPoE ethertype */
  2490. static int mvpp2_prs_pppoe_init(struct mvpp2 *priv)
  2491. {
  2492. struct mvpp2_prs_entry pe;
  2493. int tid;
  2494. /* IPv4 over PPPoE with options */
  2495. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  2496. MVPP2_PE_LAST_FREE_TID);
  2497. if (tid < 0)
  2498. return tid;
  2499. memset(&pe, 0, sizeof(pe));
  2500. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
  2501. pe.index = tid;
  2502. mvpp2_prs_match_etype(&pe, 0, PPP_IP);
  2503. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
  2504. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
  2505. MVPP2_PRS_RI_L3_PROTO_MASK);
  2506. /* Skip eth_type + 4 bytes of IP header */
  2507. mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
  2508. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  2509. /* Set L3 offset */
  2510. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  2511. MVPP2_ETH_TYPE_LEN,
  2512. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  2513. /* Update shadow table and hw entry */
  2514. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
  2515. mvpp2_prs_hw_write(priv, &pe);
  2516. /* IPv4 over PPPoE without options */
  2517. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  2518. MVPP2_PE_LAST_FREE_TID);
  2519. if (tid < 0)
  2520. return tid;
  2521. pe.index = tid;
  2522. mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
  2523. MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
  2524. MVPP2_PRS_IPV4_HEAD_MASK |
  2525. MVPP2_PRS_IPV4_IHL_MASK);
  2526. /* Clear ri before updating */
  2527. pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
  2528. pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
  2529. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
  2530. MVPP2_PRS_RI_L3_PROTO_MASK);
  2531. /* Update shadow table and hw entry */
  2532. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
  2533. mvpp2_prs_hw_write(priv, &pe);
  2534. /* IPv6 over PPPoE */
  2535. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  2536. MVPP2_PE_LAST_FREE_TID);
  2537. if (tid < 0)
  2538. return tid;
  2539. memset(&pe, 0, sizeof(pe));
  2540. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
  2541. pe.index = tid;
  2542. mvpp2_prs_match_etype(&pe, 0, PPP_IPV6);
  2543. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
  2544. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
  2545. MVPP2_PRS_RI_L3_PROTO_MASK);
  2546. /* Skip eth_type + 4 bytes of IPv6 header */
  2547. mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
  2548. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  2549. /* Set L3 offset */
  2550. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  2551. MVPP2_ETH_TYPE_LEN,
  2552. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  2553. /* Update shadow table and hw entry */
  2554. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
  2555. mvpp2_prs_hw_write(priv, &pe);
  2556. /* Non-IP over PPPoE */
  2557. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  2558. MVPP2_PE_LAST_FREE_TID);
  2559. if (tid < 0)
  2560. return tid;
  2561. memset(&pe, 0, sizeof(pe));
  2562. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
  2563. pe.index = tid;
  2564. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
  2565. MVPP2_PRS_RI_L3_PROTO_MASK);
  2566. /* Finished: go to flowid generation */
  2567. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  2568. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  2569. /* Set L3 offset even if it's unknown L3 */
  2570. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
  2571. MVPP2_ETH_TYPE_LEN,
  2572. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  2573. /* Update shadow table and hw entry */
  2574. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
  2575. mvpp2_prs_hw_write(priv, &pe);
  2576. return 0;
  2577. }
  2578. /* Initialize entries for IPv4 */
  2579. static int mvpp2_prs_ip4_init(struct mvpp2 *priv)
  2580. {
  2581. struct mvpp2_prs_entry pe;
  2582. int err;
  2583. /* Set entries for TCP, UDP and IGMP over IPv4 */
  2584. err = mvpp2_prs_ip4_proto(priv, IPPROTO_TCP, MVPP2_PRS_RI_L4_TCP,
  2585. MVPP2_PRS_RI_L4_PROTO_MASK);
  2586. if (err)
  2587. return err;
  2588. err = mvpp2_prs_ip4_proto(priv, IPPROTO_UDP, MVPP2_PRS_RI_L4_UDP,
  2589. MVPP2_PRS_RI_L4_PROTO_MASK);
  2590. if (err)
  2591. return err;
  2592. err = mvpp2_prs_ip4_proto(priv, IPPROTO_IGMP,
  2593. MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
  2594. MVPP2_PRS_RI_UDF3_RX_SPECIAL,
  2595. MVPP2_PRS_RI_CPU_CODE_MASK |
  2596. MVPP2_PRS_RI_UDF3_MASK);
  2597. if (err)
  2598. return err;
  2599. /* IPv4 Broadcast */
  2600. err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_BROAD_CAST);
  2601. if (err)
  2602. return err;
  2603. /* IPv4 Multicast */
  2604. err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
  2605. if (err)
  2606. return err;
  2607. /* Default IPv4 entry for unknown protocols */
  2608. memset(&pe, 0, sizeof(pe));
  2609. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
  2610. pe.index = MVPP2_PE_IP4_PROTO_UN;
  2611. /* Set next lu to IPv4 */
  2612. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
  2613. mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  2614. /* Set L4 offset */
  2615. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
  2616. sizeof(struct iphdr) - 4,
  2617. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  2618. mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
  2619. MVPP2_PRS_IPV4_DIP_AI_BIT);
  2620. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
  2621. MVPP2_PRS_RI_L4_PROTO_MASK);
  2622. mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
  2623. /* Unmask all ports */
  2624. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  2625. /* Update shadow table and hw entry */
  2626. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
  2627. mvpp2_prs_hw_write(priv, &pe);
  2628. /* Default IPv4 entry for unicast address */
  2629. memset(&pe, 0, sizeof(pe));
  2630. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
  2631. pe.index = MVPP2_PE_IP4_ADDR_UN;
  2632. /* Finished: go to flowid generation */
  2633. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  2634. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  2635. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
  2636. MVPP2_PRS_RI_L3_ADDR_MASK);
  2637. mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
  2638. MVPP2_PRS_IPV4_DIP_AI_BIT);
  2639. /* Unmask all ports */
  2640. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  2641. /* Update shadow table and hw entry */
  2642. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
  2643. mvpp2_prs_hw_write(priv, &pe);
  2644. return 0;
  2645. }
  2646. /* Initialize entries for IPv6 */
  2647. static int mvpp2_prs_ip6_init(struct mvpp2 *priv)
  2648. {
  2649. struct mvpp2_prs_entry pe;
  2650. int tid, err;
  2651. /* Set entries for TCP, UDP and ICMP over IPv6 */
  2652. err = mvpp2_prs_ip6_proto(priv, IPPROTO_TCP,
  2653. MVPP2_PRS_RI_L4_TCP,
  2654. MVPP2_PRS_RI_L4_PROTO_MASK);
  2655. if (err)
  2656. return err;
  2657. err = mvpp2_prs_ip6_proto(priv, IPPROTO_UDP,
  2658. MVPP2_PRS_RI_L4_UDP,
  2659. MVPP2_PRS_RI_L4_PROTO_MASK);
  2660. if (err)
  2661. return err;
  2662. err = mvpp2_prs_ip6_proto(priv, IPPROTO_ICMPV6,
  2663. MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
  2664. MVPP2_PRS_RI_UDF3_RX_SPECIAL,
  2665. MVPP2_PRS_RI_CPU_CODE_MASK |
  2666. MVPP2_PRS_RI_UDF3_MASK);
  2667. if (err)
  2668. return err;
  2669. /* IPv4 is the last header. This is similar case as 6-TCP or 17-UDP */
  2670. /* Result Info: UDF7=1, DS lite */
  2671. err = mvpp2_prs_ip6_proto(priv, IPPROTO_IPIP,
  2672. MVPP2_PRS_RI_UDF7_IP6_LITE,
  2673. MVPP2_PRS_RI_UDF7_MASK);
  2674. if (err)
  2675. return err;
  2676. /* IPv6 multicast */
  2677. err = mvpp2_prs_ip6_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
  2678. if (err)
  2679. return err;
  2680. /* Entry for checking hop limit */
  2681. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  2682. MVPP2_PE_LAST_FREE_TID);
  2683. if (tid < 0)
  2684. return tid;
  2685. memset(&pe, 0, sizeof(pe));
  2686. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
  2687. pe.index = tid;
  2688. /* Finished: go to flowid generation */
  2689. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  2690. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  2691. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN |
  2692. MVPP2_PRS_RI_DROP_MASK,
  2693. MVPP2_PRS_RI_L3_PROTO_MASK |
  2694. MVPP2_PRS_RI_DROP_MASK);
  2695. mvpp2_prs_tcam_data_byte_set(&pe, 1, 0x00, MVPP2_PRS_IPV6_HOP_MASK);
  2696. mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
  2697. MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
  2698. /* Update shadow table and hw entry */
  2699. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
  2700. mvpp2_prs_hw_write(priv, &pe);
  2701. /* Default IPv6 entry for unknown protocols */
  2702. memset(&pe, 0, sizeof(pe));
  2703. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
  2704. pe.index = MVPP2_PE_IP6_PROTO_UN;
  2705. /* Finished: go to flowid generation */
  2706. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  2707. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  2708. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
  2709. MVPP2_PRS_RI_L4_PROTO_MASK);
  2710. /* Set L4 offset relatively to our current place */
  2711. mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
  2712. sizeof(struct ipv6hdr) - 4,
  2713. MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
  2714. mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
  2715. MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
  2716. /* Unmask all ports */
  2717. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  2718. /* Update shadow table and hw entry */
  2719. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
  2720. mvpp2_prs_hw_write(priv, &pe);
  2721. /* Default IPv6 entry for unknown ext protocols */
  2722. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  2723. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
  2724. pe.index = MVPP2_PE_IP6_EXT_PROTO_UN;
  2725. /* Finished: go to flowid generation */
  2726. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
  2727. mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
  2728. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
  2729. MVPP2_PRS_RI_L4_PROTO_MASK);
  2730. mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_EXT_AI_BIT,
  2731. MVPP2_PRS_IPV6_EXT_AI_BIT);
  2732. /* Unmask all ports */
  2733. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  2734. /* Update shadow table and hw entry */
  2735. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
  2736. mvpp2_prs_hw_write(priv, &pe);
  2737. /* Default IPv6 entry for unicast address */
  2738. memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
  2739. mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
  2740. pe.index = MVPP2_PE_IP6_ADDR_UN;
  2741. /* Finished: go to IPv6 again */
  2742. mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
  2743. mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
  2744. MVPP2_PRS_RI_L3_ADDR_MASK);
  2745. mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
  2746. MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
  2747. /* Shift back to IPV6 NH */
  2748. mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  2749. mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
  2750. /* Unmask all ports */
  2751. mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
  2752. /* Update shadow table and hw entry */
  2753. mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
  2754. mvpp2_prs_hw_write(priv, &pe);
  2755. return 0;
  2756. }
  2757. /* Parser default initialization */
  2758. static int mvpp2_prs_default_init(struct platform_device *pdev,
  2759. struct mvpp2 *priv)
  2760. {
  2761. int err, index, i;
  2762. /* Enable tcam table */
  2763. mvpp2_write(priv, MVPP2_PRS_TCAM_CTRL_REG, MVPP2_PRS_TCAM_EN_MASK);
  2764. /* Clear all tcam and sram entries */
  2765. for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++) {
  2766. mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
  2767. for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
  2768. mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), 0);
  2769. mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, index);
  2770. for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
  2771. mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), 0);
  2772. }
  2773. /* Invalidate all tcam entries */
  2774. for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++)
  2775. mvpp2_prs_hw_inv(priv, index);
  2776. priv->prs_shadow = devm_kcalloc(&pdev->dev, MVPP2_PRS_TCAM_SRAM_SIZE,
  2777. sizeof(*priv->prs_shadow),
  2778. GFP_KERNEL);
  2779. if (!priv->prs_shadow)
  2780. return -ENOMEM;
  2781. /* Always start from lookup = 0 */
  2782. for (index = 0; index < MVPP2_MAX_PORTS; index++)
  2783. mvpp2_prs_hw_port_init(priv, index, MVPP2_PRS_LU_MH,
  2784. MVPP2_PRS_PORT_LU_MAX, 0);
  2785. mvpp2_prs_def_flow_init(priv);
  2786. mvpp2_prs_mh_init(priv);
  2787. mvpp2_prs_mac_init(priv);
  2788. mvpp2_prs_dsa_init(priv);
  2789. err = mvpp2_prs_etype_init(priv);
  2790. if (err)
  2791. return err;
  2792. err = mvpp2_prs_vlan_init(pdev, priv);
  2793. if (err)
  2794. return err;
  2795. err = mvpp2_prs_pppoe_init(priv);
  2796. if (err)
  2797. return err;
  2798. err = mvpp2_prs_ip6_init(priv);
  2799. if (err)
  2800. return err;
  2801. err = mvpp2_prs_ip4_init(priv);
  2802. if (err)
  2803. return err;
  2804. return 0;
  2805. }
  2806. /* Compare MAC DA with tcam entry data */
  2807. static bool mvpp2_prs_mac_range_equals(struct mvpp2_prs_entry *pe,
  2808. const u8 *da, unsigned char *mask)
  2809. {
  2810. unsigned char tcam_byte, tcam_mask;
  2811. int index;
  2812. for (index = 0; index < ETH_ALEN; index++) {
  2813. mvpp2_prs_tcam_data_byte_get(pe, index, &tcam_byte, &tcam_mask);
  2814. if (tcam_mask != mask[index])
  2815. return false;
  2816. if ((tcam_mask & tcam_byte) != (da[index] & mask[index]))
  2817. return false;
  2818. }
  2819. return true;
  2820. }
  2821. /* Find tcam entry with matched pair <MAC DA, port> */
  2822. static struct mvpp2_prs_entry *
  2823. mvpp2_prs_mac_da_range_find(struct mvpp2 *priv, int pmap, const u8 *da,
  2824. unsigned char *mask, int udf_type)
  2825. {
  2826. struct mvpp2_prs_entry *pe;
  2827. int tid;
  2828. pe = kzalloc(sizeof(*pe), GFP_KERNEL);
  2829. if (!pe)
  2830. return NULL;
  2831. mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
  2832. /* Go through the all entires with MVPP2_PRS_LU_MAC */
  2833. for (tid = MVPP2_PE_FIRST_FREE_TID;
  2834. tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
  2835. unsigned int entry_pmap;
  2836. if (!priv->prs_shadow[tid].valid ||
  2837. (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
  2838. (priv->prs_shadow[tid].udf != udf_type))
  2839. continue;
  2840. pe->index = tid;
  2841. mvpp2_prs_hw_read(priv, pe);
  2842. entry_pmap = mvpp2_prs_tcam_port_map_get(pe);
  2843. if (mvpp2_prs_mac_range_equals(pe, da, mask) &&
  2844. entry_pmap == pmap)
  2845. return pe;
  2846. }
  2847. kfree(pe);
  2848. return NULL;
  2849. }
  2850. /* Update parser's mac da entry */
  2851. static int mvpp2_prs_mac_da_accept(struct mvpp2 *priv, int port,
  2852. const u8 *da, bool add)
  2853. {
  2854. struct mvpp2_prs_entry *pe;
  2855. unsigned int pmap, len, ri;
  2856. unsigned char mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  2857. int tid;
  2858. /* Scan TCAM and see if entry with this <MAC DA, port> already exist */
  2859. pe = mvpp2_prs_mac_da_range_find(priv, (1 << port), da, mask,
  2860. MVPP2_PRS_UDF_MAC_DEF);
  2861. /* No such entry */
  2862. if (!pe) {
  2863. if (!add)
  2864. return 0;
  2865. /* Create new TCAM entry */
  2866. /* Find first range mac entry*/
  2867. for (tid = MVPP2_PE_FIRST_FREE_TID;
  2868. tid <= MVPP2_PE_LAST_FREE_TID; tid++)
  2869. if (priv->prs_shadow[tid].valid &&
  2870. (priv->prs_shadow[tid].lu == MVPP2_PRS_LU_MAC) &&
  2871. (priv->prs_shadow[tid].udf ==
  2872. MVPP2_PRS_UDF_MAC_RANGE))
  2873. break;
  2874. /* Go through the all entries from first to last */
  2875. tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
  2876. tid - 1);
  2877. if (tid < 0)
  2878. return tid;
  2879. pe = kzalloc(sizeof(*pe), GFP_KERNEL);
  2880. if (!pe)
  2881. return -ENOMEM;
  2882. mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
  2883. pe->index = tid;
  2884. /* Mask all ports */
  2885. mvpp2_prs_tcam_port_map_set(pe, 0);
  2886. }
  2887. /* Update port mask */
  2888. mvpp2_prs_tcam_port_set(pe, port, add);
  2889. /* Invalidate the entry if no ports are left enabled */
  2890. pmap = mvpp2_prs_tcam_port_map_get(pe);
  2891. if (pmap == 0) {
  2892. if (add) {
  2893. kfree(pe);
  2894. return -EINVAL;
  2895. }
  2896. mvpp2_prs_hw_inv(priv, pe->index);
  2897. priv->prs_shadow[pe->index].valid = false;
  2898. kfree(pe);
  2899. return 0;
  2900. }
  2901. /* Continue - set next lookup */
  2902. mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_DSA);
  2903. /* Set match on DA */
  2904. len = ETH_ALEN;
  2905. while (len--)
  2906. mvpp2_prs_tcam_data_byte_set(pe, len, da[len], 0xff);
  2907. /* Set result info bits */
  2908. if (is_broadcast_ether_addr(da))
  2909. ri = MVPP2_PRS_RI_L2_BCAST;
  2910. else if (is_multicast_ether_addr(da))
  2911. ri = MVPP2_PRS_RI_L2_MCAST;
  2912. else
  2913. ri = MVPP2_PRS_RI_L2_UCAST | MVPP2_PRS_RI_MAC_ME_MASK;
  2914. mvpp2_prs_sram_ri_update(pe, ri, MVPP2_PRS_RI_L2_CAST_MASK |
  2915. MVPP2_PRS_RI_MAC_ME_MASK);
  2916. mvpp2_prs_shadow_ri_set(priv, pe->index, ri, MVPP2_PRS_RI_L2_CAST_MASK |
  2917. MVPP2_PRS_RI_MAC_ME_MASK);
  2918. /* Shift to ethertype */
  2919. mvpp2_prs_sram_shift_set(pe, 2 * ETH_ALEN,
  2920. MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
  2921. /* Update shadow table and hw entry */
  2922. priv->prs_shadow[pe->index].udf = MVPP2_PRS_UDF_MAC_DEF;
  2923. mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_MAC);
  2924. mvpp2_prs_hw_write(priv, pe);
  2925. kfree(pe);
  2926. return 0;
  2927. }
  2928. static int mvpp2_prs_update_mac_da(struct net_device *dev, const u8 *da)
  2929. {
  2930. struct mvpp2_port *port = netdev_priv(dev);
  2931. int err;
  2932. /* Remove old parser entry */
  2933. err = mvpp2_prs_mac_da_accept(port->priv, port->id, dev->dev_addr,
  2934. false);
  2935. if (err)
  2936. return err;
  2937. /* Add new parser entry */
  2938. err = mvpp2_prs_mac_da_accept(port->priv, port->id, da, true);
  2939. if (err)
  2940. return err;
  2941. /* Set addr in the device */
  2942. ether_addr_copy(dev->dev_addr, da);
  2943. return 0;
  2944. }
  2945. /* Delete all port's multicast simple (not range) entries */
  2946. static void mvpp2_prs_mcast_del_all(struct mvpp2 *priv, int port)
  2947. {
  2948. struct mvpp2_prs_entry pe;
  2949. int index, tid;
  2950. for (tid = MVPP2_PE_FIRST_FREE_TID;
  2951. tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
  2952. unsigned char da[ETH_ALEN], da_mask[ETH_ALEN];
  2953. if (!priv->prs_shadow[tid].valid ||
  2954. (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
  2955. (priv->prs_shadow[tid].udf != MVPP2_PRS_UDF_MAC_DEF))
  2956. continue;
  2957. /* Only simple mac entries */
  2958. pe.index = tid;
  2959. mvpp2_prs_hw_read(priv, &pe);
  2960. /* Read mac addr from entry */
  2961. for (index = 0; index < ETH_ALEN; index++)
  2962. mvpp2_prs_tcam_data_byte_get(&pe, index, &da[index],
  2963. &da_mask[index]);
  2964. if (is_multicast_ether_addr(da) && !is_broadcast_ether_addr(da))
  2965. /* Delete this entry */
  2966. mvpp2_prs_mac_da_accept(priv, port, da, false);
  2967. }
  2968. }
  2969. static int mvpp2_prs_tag_mode_set(struct mvpp2 *priv, int port, int type)
  2970. {
  2971. switch (type) {
  2972. case MVPP2_TAG_TYPE_EDSA:
  2973. /* Add port to EDSA entries */
  2974. mvpp2_prs_dsa_tag_set(priv, port, true,
  2975. MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
  2976. mvpp2_prs_dsa_tag_set(priv, port, true,
  2977. MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
  2978. /* Remove port from DSA entries */
  2979. mvpp2_prs_dsa_tag_set(priv, port, false,
  2980. MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
  2981. mvpp2_prs_dsa_tag_set(priv, port, false,
  2982. MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
  2983. break;
  2984. case MVPP2_TAG_TYPE_DSA:
  2985. /* Add port to DSA entries */
  2986. mvpp2_prs_dsa_tag_set(priv, port, true,
  2987. MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
  2988. mvpp2_prs_dsa_tag_set(priv, port, true,
  2989. MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
  2990. /* Remove port from EDSA entries */
  2991. mvpp2_prs_dsa_tag_set(priv, port, false,
  2992. MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
  2993. mvpp2_prs_dsa_tag_set(priv, port, false,
  2994. MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
  2995. break;
  2996. case MVPP2_TAG_TYPE_MH:
  2997. case MVPP2_TAG_TYPE_NONE:
  2998. /* Remove port form EDSA and DSA entries */
  2999. mvpp2_prs_dsa_tag_set(priv, port, false,
  3000. MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
  3001. mvpp2_prs_dsa_tag_set(priv, port, false,
  3002. MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
  3003. mvpp2_prs_dsa_tag_set(priv, port, false,
  3004. MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
  3005. mvpp2_prs_dsa_tag_set(priv, port, false,
  3006. MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
  3007. break;
  3008. default:
  3009. if ((type < 0) || (type > MVPP2_TAG_TYPE_EDSA))
  3010. return -EINVAL;
  3011. }
  3012. return 0;
  3013. }
  3014. /* Set prs flow for the port */
  3015. static int mvpp2_prs_def_flow(struct mvpp2_port *port)
  3016. {
  3017. struct mvpp2_prs_entry *pe;
  3018. int tid;
  3019. pe = mvpp2_prs_flow_find(port->priv, port->id);
  3020. /* Such entry not exist */
  3021. if (!pe) {
  3022. /* Go through the all entires from last to first */
  3023. tid = mvpp2_prs_tcam_first_free(port->priv,
  3024. MVPP2_PE_LAST_FREE_TID,
  3025. MVPP2_PE_FIRST_FREE_TID);
  3026. if (tid < 0)
  3027. return tid;
  3028. pe = kzalloc(sizeof(*pe), GFP_KERNEL);
  3029. if (!pe)
  3030. return -ENOMEM;
  3031. mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
  3032. pe->index = tid;
  3033. /* Set flow ID*/
  3034. mvpp2_prs_sram_ai_update(pe, port->id, MVPP2_PRS_FLOW_ID_MASK);
  3035. mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
  3036. /* Update shadow table */
  3037. mvpp2_prs_shadow_set(port->priv, pe->index, MVPP2_PRS_LU_FLOWS);
  3038. }
  3039. mvpp2_prs_tcam_port_map_set(pe, (1 << port->id));
  3040. mvpp2_prs_hw_write(port->priv, pe);
  3041. kfree(pe);
  3042. return 0;
  3043. }
  3044. /* Classifier configuration routines */
  3045. /* Update classification flow table registers */
  3046. static void mvpp2_cls_flow_write(struct mvpp2 *priv,
  3047. struct mvpp2_cls_flow_entry *fe)
  3048. {
  3049. mvpp2_write(priv, MVPP2_CLS_FLOW_INDEX_REG, fe->index);
  3050. mvpp2_write(priv, MVPP2_CLS_FLOW_TBL0_REG, fe->data[0]);
  3051. mvpp2_write(priv, MVPP2_CLS_FLOW_TBL1_REG, fe->data[1]);
  3052. mvpp2_write(priv, MVPP2_CLS_FLOW_TBL2_REG, fe->data[2]);
  3053. }
  3054. /* Update classification lookup table register */
  3055. static void mvpp2_cls_lookup_write(struct mvpp2 *priv,
  3056. struct mvpp2_cls_lookup_entry *le)
  3057. {
  3058. u32 val;
  3059. val = (le->way << MVPP2_CLS_LKP_INDEX_WAY_OFFS) | le->lkpid;
  3060. mvpp2_write(priv, MVPP2_CLS_LKP_INDEX_REG, val);
  3061. mvpp2_write(priv, MVPP2_CLS_LKP_TBL_REG, le->data);
  3062. }
  3063. /* Classifier default initialization */
  3064. static void mvpp2_cls_init(struct mvpp2 *priv)
  3065. {
  3066. struct mvpp2_cls_lookup_entry le;
  3067. struct mvpp2_cls_flow_entry fe;
  3068. int index;
  3069. /* Enable classifier */
  3070. mvpp2_write(priv, MVPP2_CLS_MODE_REG, MVPP2_CLS_MODE_ACTIVE_MASK);
  3071. /* Clear classifier flow table */
  3072. memset(&fe.data, 0, sizeof(fe.data));
  3073. for (index = 0; index < MVPP2_CLS_FLOWS_TBL_SIZE; index++) {
  3074. fe.index = index;
  3075. mvpp2_cls_flow_write(priv, &fe);
  3076. }
  3077. /* Clear classifier lookup table */
  3078. le.data = 0;
  3079. for (index = 0; index < MVPP2_CLS_LKP_TBL_SIZE; index++) {
  3080. le.lkpid = index;
  3081. le.way = 0;
  3082. mvpp2_cls_lookup_write(priv, &le);
  3083. le.way = 1;
  3084. mvpp2_cls_lookup_write(priv, &le);
  3085. }
  3086. }
  3087. static void mvpp2_cls_port_config(struct mvpp2_port *port)
  3088. {
  3089. struct mvpp2_cls_lookup_entry le;
  3090. u32 val;
  3091. /* Set way for the port */
  3092. val = mvpp2_read(port->priv, MVPP2_CLS_PORT_WAY_REG);
  3093. val &= ~MVPP2_CLS_PORT_WAY_MASK(port->id);
  3094. mvpp2_write(port->priv, MVPP2_CLS_PORT_WAY_REG, val);
  3095. /* Pick the entry to be accessed in lookup ID decoding table
  3096. * according to the way and lkpid.
  3097. */
  3098. le.lkpid = port->id;
  3099. le.way = 0;
  3100. le.data = 0;
  3101. /* Set initial CPU queue for receiving packets */
  3102. le.data &= ~MVPP2_CLS_LKP_TBL_RXQ_MASK;
  3103. le.data |= port->first_rxq;
  3104. /* Disable classification engines */
  3105. le.data &= ~MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK;
  3106. /* Update lookup ID table entry */
  3107. mvpp2_cls_lookup_write(port->priv, &le);
  3108. }
  3109. /* Set CPU queue number for oversize packets */
  3110. static void mvpp2_cls_oversize_rxq_set(struct mvpp2_port *port)
  3111. {
  3112. u32 val;
  3113. mvpp2_write(port->priv, MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port->id),
  3114. port->first_rxq & MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK);
  3115. mvpp2_write(port->priv, MVPP2_CLS_SWFWD_P2HQ_REG(port->id),
  3116. (port->first_rxq >> MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS));
  3117. val = mvpp2_read(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG);
  3118. val |= MVPP2_CLS_SWFWD_PCTRL_MASK(port->id);
  3119. mvpp2_write(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG, val);
  3120. }
  3121. static void *mvpp2_frag_alloc(const struct mvpp2_bm_pool *pool)
  3122. {
  3123. if (likely(pool->frag_size <= PAGE_SIZE))
  3124. return netdev_alloc_frag(pool->frag_size);
  3125. else
  3126. return kmalloc(pool->frag_size, GFP_ATOMIC);
  3127. }
  3128. static void mvpp2_frag_free(const struct mvpp2_bm_pool *pool, void *data)
  3129. {
  3130. if (likely(pool->frag_size <= PAGE_SIZE))
  3131. skb_free_frag(data);
  3132. else
  3133. kfree(data);
  3134. }
  3135. /* Buffer Manager configuration routines */
  3136. /* Create pool */
  3137. static int mvpp2_bm_pool_create(struct platform_device *pdev,
  3138. struct mvpp2 *priv,
  3139. struct mvpp2_bm_pool *bm_pool, int size)
  3140. {
  3141. u32 val;
  3142. /* Number of buffer pointers must be a multiple of 16, as per
  3143. * hardware constraints
  3144. */
  3145. if (!IS_ALIGNED(size, 16))
  3146. return -EINVAL;
  3147. /* PPv2.1 needs 8 bytes per buffer pointer, PPv2.2 needs 16
  3148. * bytes per buffer pointer
  3149. */
  3150. if (priv->hw_version == MVPP21)
  3151. bm_pool->size_bytes = 2 * sizeof(u32) * size;
  3152. else
  3153. bm_pool->size_bytes = 2 * sizeof(u64) * size;
  3154. bm_pool->virt_addr = dma_alloc_coherent(&pdev->dev, bm_pool->size_bytes,
  3155. &bm_pool->dma_addr,
  3156. GFP_KERNEL);
  3157. if (!bm_pool->virt_addr)
  3158. return -ENOMEM;
  3159. if (!IS_ALIGNED((unsigned long)bm_pool->virt_addr,
  3160. MVPP2_BM_POOL_PTR_ALIGN)) {
  3161. dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
  3162. bm_pool->virt_addr, bm_pool->dma_addr);
  3163. dev_err(&pdev->dev, "BM pool %d is not %d bytes aligned\n",
  3164. bm_pool->id, MVPP2_BM_POOL_PTR_ALIGN);
  3165. return -ENOMEM;
  3166. }
  3167. mvpp2_write(priv, MVPP2_BM_POOL_BASE_REG(bm_pool->id),
  3168. lower_32_bits(bm_pool->dma_addr));
  3169. mvpp2_write(priv, MVPP2_BM_POOL_SIZE_REG(bm_pool->id), size);
  3170. val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
  3171. val |= MVPP2_BM_START_MASK;
  3172. mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
  3173. bm_pool->type = MVPP2_BM_FREE;
  3174. bm_pool->size = size;
  3175. bm_pool->pkt_size = 0;
  3176. bm_pool->buf_num = 0;
  3177. return 0;
  3178. }
  3179. /* Set pool buffer size */
  3180. static void mvpp2_bm_pool_bufsize_set(struct mvpp2 *priv,
  3181. struct mvpp2_bm_pool *bm_pool,
  3182. int buf_size)
  3183. {
  3184. u32 val;
  3185. bm_pool->buf_size = buf_size;
  3186. val = ALIGN(buf_size, 1 << MVPP2_POOL_BUF_SIZE_OFFSET);
  3187. mvpp2_write(priv, MVPP2_POOL_BUF_SIZE_REG(bm_pool->id), val);
  3188. }
  3189. static void mvpp2_bm_bufs_get_addrs(struct device *dev, struct mvpp2 *priv,
  3190. struct mvpp2_bm_pool *bm_pool,
  3191. dma_addr_t *dma_addr,
  3192. phys_addr_t *phys_addr)
  3193. {
  3194. int cpu = get_cpu();
  3195. *dma_addr = mvpp2_percpu_read(priv, cpu,
  3196. MVPP2_BM_PHY_ALLOC_REG(bm_pool->id));
  3197. *phys_addr = mvpp2_percpu_read(priv, cpu, MVPP2_BM_VIRT_ALLOC_REG);
  3198. if (priv->hw_version == MVPP22) {
  3199. u32 val;
  3200. u32 dma_addr_highbits, phys_addr_highbits;
  3201. val = mvpp2_percpu_read(priv, cpu, MVPP22_BM_ADDR_HIGH_ALLOC);
  3202. dma_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_PHYS_MASK);
  3203. phys_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_VIRT_MASK) >>
  3204. MVPP22_BM_ADDR_HIGH_VIRT_SHIFT;
  3205. if (sizeof(dma_addr_t) == 8)
  3206. *dma_addr |= (u64)dma_addr_highbits << 32;
  3207. if (sizeof(phys_addr_t) == 8)
  3208. *phys_addr |= (u64)phys_addr_highbits << 32;
  3209. }
  3210. put_cpu();
  3211. }
  3212. /* Free all buffers from the pool */
  3213. static void mvpp2_bm_bufs_free(struct device *dev, struct mvpp2 *priv,
  3214. struct mvpp2_bm_pool *bm_pool)
  3215. {
  3216. int i;
  3217. for (i = 0; i < bm_pool->buf_num; i++) {
  3218. dma_addr_t buf_dma_addr;
  3219. phys_addr_t buf_phys_addr;
  3220. void *data;
  3221. mvpp2_bm_bufs_get_addrs(dev, priv, bm_pool,
  3222. &buf_dma_addr, &buf_phys_addr);
  3223. dma_unmap_single(dev, buf_dma_addr,
  3224. bm_pool->buf_size, DMA_FROM_DEVICE);
  3225. data = (void *)phys_to_virt(buf_phys_addr);
  3226. if (!data)
  3227. break;
  3228. mvpp2_frag_free(bm_pool, data);
  3229. }
  3230. /* Update BM driver with number of buffers removed from pool */
  3231. bm_pool->buf_num -= i;
  3232. }
  3233. /* Cleanup pool */
  3234. static int mvpp2_bm_pool_destroy(struct platform_device *pdev,
  3235. struct mvpp2 *priv,
  3236. struct mvpp2_bm_pool *bm_pool)
  3237. {
  3238. u32 val;
  3239. mvpp2_bm_bufs_free(&pdev->dev, priv, bm_pool);
  3240. if (bm_pool->buf_num) {
  3241. WARN(1, "cannot free all buffers in pool %d\n", bm_pool->id);
  3242. return 0;
  3243. }
  3244. val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
  3245. val |= MVPP2_BM_STOP_MASK;
  3246. mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
  3247. dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
  3248. bm_pool->virt_addr,
  3249. bm_pool->dma_addr);
  3250. return 0;
  3251. }
  3252. static int mvpp2_bm_pools_init(struct platform_device *pdev,
  3253. struct mvpp2 *priv)
  3254. {
  3255. int i, err, size;
  3256. struct mvpp2_bm_pool *bm_pool;
  3257. /* Create all pools with maximum size */
  3258. size = MVPP2_BM_POOL_SIZE_MAX;
  3259. for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
  3260. bm_pool = &priv->bm_pools[i];
  3261. bm_pool->id = i;
  3262. err = mvpp2_bm_pool_create(pdev, priv, bm_pool, size);
  3263. if (err)
  3264. goto err_unroll_pools;
  3265. mvpp2_bm_pool_bufsize_set(priv, bm_pool, 0);
  3266. }
  3267. return 0;
  3268. err_unroll_pools:
  3269. dev_err(&pdev->dev, "failed to create BM pool %d, size %d\n", i, size);
  3270. for (i = i - 1; i >= 0; i--)
  3271. mvpp2_bm_pool_destroy(pdev, priv, &priv->bm_pools[i]);
  3272. return err;
  3273. }
  3274. static int mvpp2_bm_init(struct platform_device *pdev, struct mvpp2 *priv)
  3275. {
  3276. int i, err;
  3277. for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
  3278. /* Mask BM all interrupts */
  3279. mvpp2_write(priv, MVPP2_BM_INTR_MASK_REG(i), 0);
  3280. /* Clear BM cause register */
  3281. mvpp2_write(priv, MVPP2_BM_INTR_CAUSE_REG(i), 0);
  3282. }
  3283. /* Allocate and initialize BM pools */
  3284. priv->bm_pools = devm_kcalloc(&pdev->dev, MVPP2_BM_POOLS_NUM,
  3285. sizeof(*priv->bm_pools), GFP_KERNEL);
  3286. if (!priv->bm_pools)
  3287. return -ENOMEM;
  3288. err = mvpp2_bm_pools_init(pdev, priv);
  3289. if (err < 0)
  3290. return err;
  3291. return 0;
  3292. }
  3293. /* Attach long pool to rxq */
  3294. static void mvpp2_rxq_long_pool_set(struct mvpp2_port *port,
  3295. int lrxq, int long_pool)
  3296. {
  3297. u32 val, mask;
  3298. int prxq;
  3299. /* Get queue physical ID */
  3300. prxq = port->rxqs[lrxq]->id;
  3301. if (port->priv->hw_version == MVPP21)
  3302. mask = MVPP21_RXQ_POOL_LONG_MASK;
  3303. else
  3304. mask = MVPP22_RXQ_POOL_LONG_MASK;
  3305. val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
  3306. val &= ~mask;
  3307. val |= (long_pool << MVPP2_RXQ_POOL_LONG_OFFS) & mask;
  3308. mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
  3309. }
  3310. /* Attach short pool to rxq */
  3311. static void mvpp2_rxq_short_pool_set(struct mvpp2_port *port,
  3312. int lrxq, int short_pool)
  3313. {
  3314. u32 val, mask;
  3315. int prxq;
  3316. /* Get queue physical ID */
  3317. prxq = port->rxqs[lrxq]->id;
  3318. if (port->priv->hw_version == MVPP21)
  3319. mask = MVPP21_RXQ_POOL_SHORT_MASK;
  3320. else
  3321. mask = MVPP22_RXQ_POOL_SHORT_MASK;
  3322. val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
  3323. val &= ~mask;
  3324. val |= (short_pool << MVPP2_RXQ_POOL_SHORT_OFFS) & mask;
  3325. mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
  3326. }
  3327. static void *mvpp2_buf_alloc(struct mvpp2_port *port,
  3328. struct mvpp2_bm_pool *bm_pool,
  3329. dma_addr_t *buf_dma_addr,
  3330. phys_addr_t *buf_phys_addr,
  3331. gfp_t gfp_mask)
  3332. {
  3333. dma_addr_t dma_addr;
  3334. void *data;
  3335. data = mvpp2_frag_alloc(bm_pool);
  3336. if (!data)
  3337. return NULL;
  3338. dma_addr = dma_map_single(port->dev->dev.parent, data,
  3339. MVPP2_RX_BUF_SIZE(bm_pool->pkt_size),
  3340. DMA_FROM_DEVICE);
  3341. if (unlikely(dma_mapping_error(port->dev->dev.parent, dma_addr))) {
  3342. mvpp2_frag_free(bm_pool, data);
  3343. return NULL;
  3344. }
  3345. *buf_dma_addr = dma_addr;
  3346. *buf_phys_addr = virt_to_phys(data);
  3347. return data;
  3348. }
  3349. /* Release buffer to BM */
  3350. static inline void mvpp2_bm_pool_put(struct mvpp2_port *port, int pool,
  3351. dma_addr_t buf_dma_addr,
  3352. phys_addr_t buf_phys_addr)
  3353. {
  3354. int cpu = get_cpu();
  3355. if (port->priv->hw_version == MVPP22) {
  3356. u32 val = 0;
  3357. if (sizeof(dma_addr_t) == 8)
  3358. val |= upper_32_bits(buf_dma_addr) &
  3359. MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK;
  3360. if (sizeof(phys_addr_t) == 8)
  3361. val |= (upper_32_bits(buf_phys_addr)
  3362. << MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT) &
  3363. MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK;
  3364. mvpp2_percpu_write(port->priv, cpu,
  3365. MVPP22_BM_ADDR_HIGH_RLS_REG, val);
  3366. }
  3367. /* MVPP2_BM_VIRT_RLS_REG is not interpreted by HW, and simply
  3368. * returned in the "cookie" field of the RX
  3369. * descriptor. Instead of storing the virtual address, we
  3370. * store the physical address
  3371. */
  3372. mvpp2_percpu_write(port->priv, cpu,
  3373. MVPP2_BM_VIRT_RLS_REG, buf_phys_addr);
  3374. mvpp2_percpu_write(port->priv, cpu,
  3375. MVPP2_BM_PHY_RLS_REG(pool), buf_dma_addr);
  3376. put_cpu();
  3377. }
  3378. /* Allocate buffers for the pool */
  3379. static int mvpp2_bm_bufs_add(struct mvpp2_port *port,
  3380. struct mvpp2_bm_pool *bm_pool, int buf_num)
  3381. {
  3382. int i, buf_size, total_size;
  3383. dma_addr_t dma_addr;
  3384. phys_addr_t phys_addr;
  3385. void *buf;
  3386. buf_size = MVPP2_RX_BUF_SIZE(bm_pool->pkt_size);
  3387. total_size = MVPP2_RX_TOTAL_SIZE(buf_size);
  3388. if (buf_num < 0 ||
  3389. (buf_num + bm_pool->buf_num > bm_pool->size)) {
  3390. netdev_err(port->dev,
  3391. "cannot allocate %d buffers for pool %d\n",
  3392. buf_num, bm_pool->id);
  3393. return 0;
  3394. }
  3395. for (i = 0; i < buf_num; i++) {
  3396. buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr,
  3397. &phys_addr, GFP_KERNEL);
  3398. if (!buf)
  3399. break;
  3400. mvpp2_bm_pool_put(port, bm_pool->id, dma_addr,
  3401. phys_addr);
  3402. }
  3403. /* Update BM driver with number of buffers added to pool */
  3404. bm_pool->buf_num += i;
  3405. netdev_dbg(port->dev,
  3406. "%s pool %d: pkt_size=%4d, buf_size=%4d, total_size=%4d\n",
  3407. bm_pool->type == MVPP2_BM_SWF_SHORT ? "short" : " long",
  3408. bm_pool->id, bm_pool->pkt_size, buf_size, total_size);
  3409. netdev_dbg(port->dev,
  3410. "%s pool %d: %d of %d buffers added\n",
  3411. bm_pool->type == MVPP2_BM_SWF_SHORT ? "short" : " long",
  3412. bm_pool->id, i, buf_num);
  3413. return i;
  3414. }
  3415. /* Notify the driver that BM pool is being used as specific type and return the
  3416. * pool pointer on success
  3417. */
  3418. static struct mvpp2_bm_pool *
  3419. mvpp2_bm_pool_use(struct mvpp2_port *port, int pool, enum mvpp2_bm_type type,
  3420. int pkt_size)
  3421. {
  3422. struct mvpp2_bm_pool *new_pool = &port->priv->bm_pools[pool];
  3423. int num;
  3424. if (new_pool->type != MVPP2_BM_FREE && new_pool->type != type) {
  3425. netdev_err(port->dev, "mixing pool types is forbidden\n");
  3426. return NULL;
  3427. }
  3428. if (new_pool->type == MVPP2_BM_FREE)
  3429. new_pool->type = type;
  3430. /* Allocate buffers in case BM pool is used as long pool, but packet
  3431. * size doesn't match MTU or BM pool hasn't being used yet
  3432. */
  3433. if (((type == MVPP2_BM_SWF_LONG) && (pkt_size > new_pool->pkt_size)) ||
  3434. (new_pool->pkt_size == 0)) {
  3435. int pkts_num;
  3436. /* Set default buffer number or free all the buffers in case
  3437. * the pool is not empty
  3438. */
  3439. pkts_num = new_pool->buf_num;
  3440. if (pkts_num == 0)
  3441. pkts_num = type == MVPP2_BM_SWF_LONG ?
  3442. MVPP2_BM_LONG_BUF_NUM :
  3443. MVPP2_BM_SHORT_BUF_NUM;
  3444. else
  3445. mvpp2_bm_bufs_free(port->dev->dev.parent,
  3446. port->priv, new_pool);
  3447. new_pool->pkt_size = pkt_size;
  3448. new_pool->frag_size =
  3449. SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
  3450. MVPP2_SKB_SHINFO_SIZE;
  3451. /* Allocate buffers for this pool */
  3452. num = mvpp2_bm_bufs_add(port, new_pool, pkts_num);
  3453. if (num != pkts_num) {
  3454. WARN(1, "pool %d: %d of %d allocated\n",
  3455. new_pool->id, num, pkts_num);
  3456. return NULL;
  3457. }
  3458. }
  3459. mvpp2_bm_pool_bufsize_set(port->priv, new_pool,
  3460. MVPP2_RX_BUF_SIZE(new_pool->pkt_size));
  3461. return new_pool;
  3462. }
  3463. /* Initialize pools for swf */
  3464. static int mvpp2_swf_bm_pool_init(struct mvpp2_port *port)
  3465. {
  3466. int rxq;
  3467. if (!port->pool_long) {
  3468. port->pool_long =
  3469. mvpp2_bm_pool_use(port, MVPP2_BM_SWF_LONG_POOL(port->id),
  3470. MVPP2_BM_SWF_LONG,
  3471. port->pkt_size);
  3472. if (!port->pool_long)
  3473. return -ENOMEM;
  3474. port->pool_long->port_map |= (1 << port->id);
  3475. for (rxq = 0; rxq < port->nrxqs; rxq++)
  3476. mvpp2_rxq_long_pool_set(port, rxq, port->pool_long->id);
  3477. }
  3478. if (!port->pool_short) {
  3479. port->pool_short =
  3480. mvpp2_bm_pool_use(port, MVPP2_BM_SWF_SHORT_POOL,
  3481. MVPP2_BM_SWF_SHORT,
  3482. MVPP2_BM_SHORT_PKT_SIZE);
  3483. if (!port->pool_short)
  3484. return -ENOMEM;
  3485. port->pool_short->port_map |= (1 << port->id);
  3486. for (rxq = 0; rxq < port->nrxqs; rxq++)
  3487. mvpp2_rxq_short_pool_set(port, rxq,
  3488. port->pool_short->id);
  3489. }
  3490. return 0;
  3491. }
  3492. static int mvpp2_bm_update_mtu(struct net_device *dev, int mtu)
  3493. {
  3494. struct mvpp2_port *port = netdev_priv(dev);
  3495. struct mvpp2_bm_pool *port_pool = port->pool_long;
  3496. int num, pkts_num = port_pool->buf_num;
  3497. int pkt_size = MVPP2_RX_PKT_SIZE(mtu);
  3498. /* Update BM pool with new buffer size */
  3499. mvpp2_bm_bufs_free(dev->dev.parent, port->priv, port_pool);
  3500. if (port_pool->buf_num) {
  3501. WARN(1, "cannot free all buffers in pool %d\n", port_pool->id);
  3502. return -EIO;
  3503. }
  3504. port_pool->pkt_size = pkt_size;
  3505. port_pool->frag_size = SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
  3506. MVPP2_SKB_SHINFO_SIZE;
  3507. num = mvpp2_bm_bufs_add(port, port_pool, pkts_num);
  3508. if (num != pkts_num) {
  3509. WARN(1, "pool %d: %d of %d allocated\n",
  3510. port_pool->id, num, pkts_num);
  3511. return -EIO;
  3512. }
  3513. mvpp2_bm_pool_bufsize_set(port->priv, port_pool,
  3514. MVPP2_RX_BUF_SIZE(port_pool->pkt_size));
  3515. dev->mtu = mtu;
  3516. netdev_update_features(dev);
  3517. return 0;
  3518. }
  3519. static inline void mvpp2_interrupts_enable(struct mvpp2_port *port)
  3520. {
  3521. int i, sw_thread_mask = 0;
  3522. for (i = 0; i < port->nqvecs; i++)
  3523. sw_thread_mask |= port->qvecs[i].sw_thread_mask;
  3524. mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
  3525. MVPP2_ISR_ENABLE_INTERRUPT(sw_thread_mask));
  3526. }
  3527. static inline void mvpp2_interrupts_disable(struct mvpp2_port *port)
  3528. {
  3529. int i, sw_thread_mask = 0;
  3530. for (i = 0; i < port->nqvecs; i++)
  3531. sw_thread_mask |= port->qvecs[i].sw_thread_mask;
  3532. mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
  3533. MVPP2_ISR_DISABLE_INTERRUPT(sw_thread_mask));
  3534. }
  3535. static inline void mvpp2_qvec_interrupt_enable(struct mvpp2_queue_vector *qvec)
  3536. {
  3537. struct mvpp2_port *port = qvec->port;
  3538. mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
  3539. MVPP2_ISR_ENABLE_INTERRUPT(qvec->sw_thread_mask));
  3540. }
  3541. static inline void mvpp2_qvec_interrupt_disable(struct mvpp2_queue_vector *qvec)
  3542. {
  3543. struct mvpp2_port *port = qvec->port;
  3544. mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
  3545. MVPP2_ISR_DISABLE_INTERRUPT(qvec->sw_thread_mask));
  3546. }
  3547. /* Mask the current CPU's Rx/Tx interrupts
  3548. * Called by on_each_cpu(), guaranteed to run with migration disabled,
  3549. * using smp_processor_id() is OK.
  3550. */
  3551. static void mvpp2_interrupts_mask(void *arg)
  3552. {
  3553. struct mvpp2_port *port = arg;
  3554. mvpp2_percpu_write(port->priv, smp_processor_id(),
  3555. MVPP2_ISR_RX_TX_MASK_REG(port->id), 0);
  3556. }
  3557. /* Unmask the current CPU's Rx/Tx interrupts.
  3558. * Called by on_each_cpu(), guaranteed to run with migration disabled,
  3559. * using smp_processor_id() is OK.
  3560. */
  3561. static void mvpp2_interrupts_unmask(void *arg)
  3562. {
  3563. struct mvpp2_port *port = arg;
  3564. u32 val;
  3565. val = MVPP2_CAUSE_MISC_SUM_MASK |
  3566. MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
  3567. if (port->has_tx_irqs)
  3568. val |= MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
  3569. mvpp2_percpu_write(port->priv, smp_processor_id(),
  3570. MVPP2_ISR_RX_TX_MASK_REG(port->id), val);
  3571. }
  3572. static void
  3573. mvpp2_shared_interrupt_mask_unmask(struct mvpp2_port *port, bool mask)
  3574. {
  3575. u32 val;
  3576. int i;
  3577. if (port->priv->hw_version != MVPP22)
  3578. return;
  3579. if (mask)
  3580. val = 0;
  3581. else
  3582. val = MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
  3583. for (i = 0; i < port->nqvecs; i++) {
  3584. struct mvpp2_queue_vector *v = port->qvecs + i;
  3585. if (v->type != MVPP2_QUEUE_VECTOR_SHARED)
  3586. continue;
  3587. mvpp2_percpu_write(port->priv, v->sw_thread_id,
  3588. MVPP2_ISR_RX_TX_MASK_REG(port->id), val);
  3589. }
  3590. }
  3591. /* Port configuration routines */
  3592. static void mvpp22_gop_init_rgmii(struct mvpp2_port *port)
  3593. {
  3594. struct mvpp2 *priv = port->priv;
  3595. u32 val;
  3596. regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
  3597. val |= GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT;
  3598. regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
  3599. regmap_read(priv->sysctrl_base, GENCONF_CTRL0, &val);
  3600. if (port->gop_id == 2)
  3601. val |= GENCONF_CTRL0_PORT0_RGMII | GENCONF_CTRL0_PORT1_RGMII;
  3602. else if (port->gop_id == 3)
  3603. val |= GENCONF_CTRL0_PORT1_RGMII_MII;
  3604. regmap_write(priv->sysctrl_base, GENCONF_CTRL0, val);
  3605. }
  3606. static void mvpp22_gop_init_sgmii(struct mvpp2_port *port)
  3607. {
  3608. struct mvpp2 *priv = port->priv;
  3609. u32 val;
  3610. regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
  3611. val |= GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT |
  3612. GENCONF_PORT_CTRL0_RX_DATA_SAMPLE;
  3613. regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
  3614. if (port->gop_id > 1) {
  3615. regmap_read(priv->sysctrl_base, GENCONF_CTRL0, &val);
  3616. if (port->gop_id == 2)
  3617. val &= ~GENCONF_CTRL0_PORT0_RGMII;
  3618. else if (port->gop_id == 3)
  3619. val &= ~GENCONF_CTRL0_PORT1_RGMII_MII;
  3620. regmap_write(priv->sysctrl_base, GENCONF_CTRL0, val);
  3621. }
  3622. }
  3623. static void mvpp22_gop_init_10gkr(struct mvpp2_port *port)
  3624. {
  3625. struct mvpp2 *priv = port->priv;
  3626. void __iomem *mpcs = priv->iface_base + MVPP22_MPCS_BASE(port->gop_id);
  3627. void __iomem *xpcs = priv->iface_base + MVPP22_XPCS_BASE(port->gop_id);
  3628. u32 val;
  3629. /* XPCS */
  3630. val = readl(xpcs + MVPP22_XPCS_CFG0);
  3631. val &= ~(MVPP22_XPCS_CFG0_PCS_MODE(0x3) |
  3632. MVPP22_XPCS_CFG0_ACTIVE_LANE(0x3));
  3633. val |= MVPP22_XPCS_CFG0_ACTIVE_LANE(2);
  3634. writel(val, xpcs + MVPP22_XPCS_CFG0);
  3635. /* MPCS */
  3636. val = readl(mpcs + MVPP22_MPCS_CTRL);
  3637. val &= ~MVPP22_MPCS_CTRL_FWD_ERR_CONN;
  3638. writel(val, mpcs + MVPP22_MPCS_CTRL);
  3639. val = readl(mpcs + MVPP22_MPCS_CLK_RESET);
  3640. val &= ~(MVPP22_MPCS_CLK_RESET_DIV_RATIO(0x7) | MAC_CLK_RESET_MAC |
  3641. MAC_CLK_RESET_SD_RX | MAC_CLK_RESET_SD_TX);
  3642. val |= MVPP22_MPCS_CLK_RESET_DIV_RATIO(1);
  3643. writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
  3644. val &= ~MVPP22_MPCS_CLK_RESET_DIV_SET;
  3645. val |= MAC_CLK_RESET_MAC | MAC_CLK_RESET_SD_RX | MAC_CLK_RESET_SD_TX;
  3646. writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
  3647. }
  3648. static int mvpp22_gop_init(struct mvpp2_port *port)
  3649. {
  3650. struct mvpp2 *priv = port->priv;
  3651. u32 val;
  3652. if (!priv->sysctrl_base)
  3653. return 0;
  3654. switch (port->phy_interface) {
  3655. case PHY_INTERFACE_MODE_RGMII:
  3656. case PHY_INTERFACE_MODE_RGMII_ID:
  3657. case PHY_INTERFACE_MODE_RGMII_RXID:
  3658. case PHY_INTERFACE_MODE_RGMII_TXID:
  3659. if (port->gop_id == 0)
  3660. goto invalid_conf;
  3661. mvpp22_gop_init_rgmii(port);
  3662. break;
  3663. case PHY_INTERFACE_MODE_SGMII:
  3664. mvpp22_gop_init_sgmii(port);
  3665. break;
  3666. case PHY_INTERFACE_MODE_10GKR:
  3667. if (port->gop_id != 0)
  3668. goto invalid_conf;
  3669. mvpp22_gop_init_10gkr(port);
  3670. break;
  3671. default:
  3672. goto unsupported_conf;
  3673. }
  3674. regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL1, &val);
  3675. val |= GENCONF_PORT_CTRL1_RESET(port->gop_id) |
  3676. GENCONF_PORT_CTRL1_EN(port->gop_id);
  3677. regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL1, val);
  3678. regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
  3679. val |= GENCONF_PORT_CTRL0_CLK_DIV_PHASE_CLR;
  3680. regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
  3681. regmap_read(priv->sysctrl_base, GENCONF_SOFT_RESET1, &val);
  3682. val |= GENCONF_SOFT_RESET1_GOP;
  3683. regmap_write(priv->sysctrl_base, GENCONF_SOFT_RESET1, val);
  3684. unsupported_conf:
  3685. return 0;
  3686. invalid_conf:
  3687. netdev_err(port->dev, "Invalid port configuration\n");
  3688. return -EINVAL;
  3689. }
  3690. static void mvpp22_gop_unmask_irq(struct mvpp2_port *port)
  3691. {
  3692. u32 val;
  3693. if (phy_interface_mode_is_rgmii(port->phy_interface) ||
  3694. port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
  3695. /* Enable the GMAC link status irq for this port */
  3696. val = readl(port->base + MVPP22_GMAC_INT_SUM_MASK);
  3697. val |= MVPP22_GMAC_INT_SUM_MASK_LINK_STAT;
  3698. writel(val, port->base + MVPP22_GMAC_INT_SUM_MASK);
  3699. }
  3700. if (port->gop_id == 0) {
  3701. /* Enable the XLG/GIG irqs for this port */
  3702. val = readl(port->base + MVPP22_XLG_EXT_INT_MASK);
  3703. if (port->phy_interface == PHY_INTERFACE_MODE_10GKR)
  3704. val |= MVPP22_XLG_EXT_INT_MASK_XLG;
  3705. else
  3706. val |= MVPP22_XLG_EXT_INT_MASK_GIG;
  3707. writel(val, port->base + MVPP22_XLG_EXT_INT_MASK);
  3708. }
  3709. }
  3710. static void mvpp22_gop_mask_irq(struct mvpp2_port *port)
  3711. {
  3712. u32 val;
  3713. if (port->gop_id == 0) {
  3714. val = readl(port->base + MVPP22_XLG_EXT_INT_MASK);
  3715. val &= ~(MVPP22_XLG_EXT_INT_MASK_XLG |
  3716. MVPP22_XLG_EXT_INT_MASK_GIG);
  3717. writel(val, port->base + MVPP22_XLG_EXT_INT_MASK);
  3718. }
  3719. if (phy_interface_mode_is_rgmii(port->phy_interface) ||
  3720. port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
  3721. val = readl(port->base + MVPP22_GMAC_INT_SUM_MASK);
  3722. val &= ~MVPP22_GMAC_INT_SUM_MASK_LINK_STAT;
  3723. writel(val, port->base + MVPP22_GMAC_INT_SUM_MASK);
  3724. }
  3725. }
  3726. static void mvpp22_gop_setup_irq(struct mvpp2_port *port)
  3727. {
  3728. u32 val;
  3729. if (phy_interface_mode_is_rgmii(port->phy_interface) ||
  3730. port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
  3731. val = readl(port->base + MVPP22_GMAC_INT_MASK);
  3732. val |= MVPP22_GMAC_INT_MASK_LINK_STAT;
  3733. writel(val, port->base + MVPP22_GMAC_INT_MASK);
  3734. }
  3735. if (port->gop_id == 0) {
  3736. val = readl(port->base + MVPP22_XLG_INT_MASK);
  3737. val |= MVPP22_XLG_INT_MASK_LINK;
  3738. writel(val, port->base + MVPP22_XLG_INT_MASK);
  3739. }
  3740. mvpp22_gop_unmask_irq(port);
  3741. }
  3742. static int mvpp22_comphy_init(struct mvpp2_port *port)
  3743. {
  3744. enum phy_mode mode;
  3745. int ret;
  3746. if (!port->comphy)
  3747. return 0;
  3748. switch (port->phy_interface) {
  3749. case PHY_INTERFACE_MODE_SGMII:
  3750. mode = PHY_MODE_SGMII;
  3751. break;
  3752. case PHY_INTERFACE_MODE_10GKR:
  3753. mode = PHY_MODE_10GKR;
  3754. break;
  3755. default:
  3756. return -EINVAL;
  3757. }
  3758. ret = phy_set_mode(port->comphy, mode);
  3759. if (ret)
  3760. return ret;
  3761. return phy_power_on(port->comphy);
  3762. }
  3763. static void mvpp2_port_mii_gmac_configure_mode(struct mvpp2_port *port)
  3764. {
  3765. u32 val;
  3766. if (port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
  3767. val = readl(port->base + MVPP22_GMAC_CTRL_4_REG);
  3768. val |= MVPP22_CTRL4_SYNC_BYPASS_DIS | MVPP22_CTRL4_DP_CLK_SEL |
  3769. MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
  3770. val &= ~MVPP22_CTRL4_EXT_PIN_GMII_SEL;
  3771. writel(val, port->base + MVPP22_GMAC_CTRL_4_REG);
  3772. val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
  3773. val |= MVPP2_GMAC_DISABLE_PADDING;
  3774. val &= ~MVPP2_GMAC_FLOW_CTRL_MASK;
  3775. writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
  3776. } else if (phy_interface_mode_is_rgmii(port->phy_interface)) {
  3777. val = readl(port->base + MVPP22_GMAC_CTRL_4_REG);
  3778. val |= MVPP22_CTRL4_EXT_PIN_GMII_SEL |
  3779. MVPP22_CTRL4_SYNC_BYPASS_DIS |
  3780. MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
  3781. val &= ~MVPP22_CTRL4_DP_CLK_SEL;
  3782. writel(val, port->base + MVPP22_GMAC_CTRL_4_REG);
  3783. val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
  3784. val &= ~MVPP2_GMAC_DISABLE_PADDING;
  3785. writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
  3786. }
  3787. /* The port is connected to a copper PHY */
  3788. val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
  3789. val &= ~MVPP2_GMAC_PORT_TYPE_MASK;
  3790. writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
  3791. val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  3792. val |= MVPP2_GMAC_IN_BAND_AUTONEG_BYPASS |
  3793. MVPP2_GMAC_AN_SPEED_EN | MVPP2_GMAC_FLOW_CTRL_AUTONEG |
  3794. MVPP2_GMAC_AN_DUPLEX_EN;
  3795. if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
  3796. val |= MVPP2_GMAC_IN_BAND_AUTONEG;
  3797. writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  3798. }
  3799. static void mvpp2_port_mii_gmac_configure(struct mvpp2_port *port)
  3800. {
  3801. u32 val;
  3802. /* Force link down */
  3803. val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  3804. val &= ~MVPP2_GMAC_FORCE_LINK_PASS;
  3805. val |= MVPP2_GMAC_FORCE_LINK_DOWN;
  3806. writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  3807. /* Set the GMAC in a reset state */
  3808. val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
  3809. val |= MVPP2_GMAC_PORT_RESET_MASK;
  3810. writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
  3811. /* Configure the PCS and in-band AN */
  3812. val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
  3813. if (port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
  3814. val |= MVPP2_GMAC_INBAND_AN_MASK | MVPP2_GMAC_PCS_ENABLE_MASK;
  3815. } else if (phy_interface_mode_is_rgmii(port->phy_interface)) {
  3816. val &= ~MVPP2_GMAC_PCS_ENABLE_MASK;
  3817. }
  3818. writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
  3819. mvpp2_port_mii_gmac_configure_mode(port);
  3820. /* Unset the GMAC reset state */
  3821. val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
  3822. val &= ~MVPP2_GMAC_PORT_RESET_MASK;
  3823. writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
  3824. /* Stop forcing link down */
  3825. val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  3826. val &= ~MVPP2_GMAC_FORCE_LINK_DOWN;
  3827. writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  3828. }
  3829. static void mvpp2_port_mii_xlg_configure(struct mvpp2_port *port)
  3830. {
  3831. u32 val;
  3832. if (port->gop_id != 0)
  3833. return;
  3834. val = readl(port->base + MVPP22_XLG_CTRL0_REG);
  3835. val |= MVPP22_XLG_CTRL0_RX_FLOW_CTRL_EN;
  3836. writel(val, port->base + MVPP22_XLG_CTRL0_REG);
  3837. val = readl(port->base + MVPP22_XLG_CTRL4_REG);
  3838. val &= ~MVPP22_XLG_CTRL4_MACMODSELECT_GMAC;
  3839. val |= MVPP22_XLG_CTRL4_FWD_FC | MVPP22_XLG_CTRL4_FWD_PFC;
  3840. writel(val, port->base + MVPP22_XLG_CTRL4_REG);
  3841. }
  3842. static void mvpp22_port_mii_set(struct mvpp2_port *port)
  3843. {
  3844. u32 val;
  3845. /* Only GOP port 0 has an XLG MAC */
  3846. if (port->gop_id == 0) {
  3847. val = readl(port->base + MVPP22_XLG_CTRL3_REG);
  3848. val &= ~MVPP22_XLG_CTRL3_MACMODESELECT_MASK;
  3849. if (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
  3850. port->phy_interface == PHY_INTERFACE_MODE_10GKR)
  3851. val |= MVPP22_XLG_CTRL3_MACMODESELECT_10G;
  3852. else
  3853. val |= MVPP22_XLG_CTRL3_MACMODESELECT_GMAC;
  3854. writel(val, port->base + MVPP22_XLG_CTRL3_REG);
  3855. }
  3856. }
  3857. static void mvpp2_port_mii_set(struct mvpp2_port *port)
  3858. {
  3859. if (port->priv->hw_version == MVPP22)
  3860. mvpp22_port_mii_set(port);
  3861. if (phy_interface_mode_is_rgmii(port->phy_interface) ||
  3862. port->phy_interface == PHY_INTERFACE_MODE_SGMII)
  3863. mvpp2_port_mii_gmac_configure(port);
  3864. else if (port->phy_interface == PHY_INTERFACE_MODE_10GKR)
  3865. mvpp2_port_mii_xlg_configure(port);
  3866. }
  3867. static void mvpp2_port_fc_adv_enable(struct mvpp2_port *port)
  3868. {
  3869. u32 val;
  3870. val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  3871. val |= MVPP2_GMAC_FC_ADV_EN;
  3872. writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  3873. }
  3874. static void mvpp2_port_enable(struct mvpp2_port *port)
  3875. {
  3876. u32 val;
  3877. /* Only GOP port 0 has an XLG MAC */
  3878. if (port->gop_id == 0 &&
  3879. (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
  3880. port->phy_interface == PHY_INTERFACE_MODE_10GKR)) {
  3881. val = readl(port->base + MVPP22_XLG_CTRL0_REG);
  3882. val |= MVPP22_XLG_CTRL0_PORT_EN |
  3883. MVPP22_XLG_CTRL0_MAC_RESET_DIS;
  3884. val &= ~MVPP22_XLG_CTRL0_MIB_CNT_DIS;
  3885. writel(val, port->base + MVPP22_XLG_CTRL0_REG);
  3886. } else {
  3887. val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
  3888. val |= MVPP2_GMAC_PORT_EN_MASK;
  3889. val |= MVPP2_GMAC_MIB_CNTR_EN_MASK;
  3890. writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
  3891. }
  3892. }
  3893. static void mvpp2_port_disable(struct mvpp2_port *port)
  3894. {
  3895. u32 val;
  3896. /* Only GOP port 0 has an XLG MAC */
  3897. if (port->gop_id == 0 &&
  3898. (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
  3899. port->phy_interface == PHY_INTERFACE_MODE_10GKR)) {
  3900. val = readl(port->base + MVPP22_XLG_CTRL0_REG);
  3901. val &= ~(MVPP22_XLG_CTRL0_PORT_EN |
  3902. MVPP22_XLG_CTRL0_MAC_RESET_DIS);
  3903. writel(val, port->base + MVPP22_XLG_CTRL0_REG);
  3904. } else {
  3905. val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
  3906. val &= ~(MVPP2_GMAC_PORT_EN_MASK);
  3907. writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
  3908. }
  3909. }
  3910. /* Set IEEE 802.3x Flow Control Xon Packet Transmission Mode */
  3911. static void mvpp2_port_periodic_xon_disable(struct mvpp2_port *port)
  3912. {
  3913. u32 val;
  3914. val = readl(port->base + MVPP2_GMAC_CTRL_1_REG) &
  3915. ~MVPP2_GMAC_PERIODIC_XON_EN_MASK;
  3916. writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
  3917. }
  3918. /* Configure loopback port */
  3919. static void mvpp2_port_loopback_set(struct mvpp2_port *port)
  3920. {
  3921. u32 val;
  3922. val = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
  3923. if (port->speed == 1000)
  3924. val |= MVPP2_GMAC_GMII_LB_EN_MASK;
  3925. else
  3926. val &= ~MVPP2_GMAC_GMII_LB_EN_MASK;
  3927. if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
  3928. val |= MVPP2_GMAC_PCS_LB_EN_MASK;
  3929. else
  3930. val &= ~MVPP2_GMAC_PCS_LB_EN_MASK;
  3931. writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
  3932. }
  3933. static void mvpp2_port_reset(struct mvpp2_port *port)
  3934. {
  3935. u32 val;
  3936. val = readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
  3937. ~MVPP2_GMAC_PORT_RESET_MASK;
  3938. writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
  3939. while (readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
  3940. MVPP2_GMAC_PORT_RESET_MASK)
  3941. continue;
  3942. }
  3943. /* Change maximum receive size of the port */
  3944. static inline void mvpp2_gmac_max_rx_size_set(struct mvpp2_port *port)
  3945. {
  3946. u32 val;
  3947. val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
  3948. val &= ~MVPP2_GMAC_MAX_RX_SIZE_MASK;
  3949. val |= (((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
  3950. MVPP2_GMAC_MAX_RX_SIZE_OFFS);
  3951. writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
  3952. }
  3953. /* Change maximum receive size of the port */
  3954. static inline void mvpp2_xlg_max_rx_size_set(struct mvpp2_port *port)
  3955. {
  3956. u32 val;
  3957. val = readl(port->base + MVPP22_XLG_CTRL1_REG);
  3958. val &= ~MVPP22_XLG_CTRL1_FRAMESIZELIMIT_MASK;
  3959. val |= ((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
  3960. MVPP22_XLG_CTRL1_FRAMESIZELIMIT_OFFS;
  3961. writel(val, port->base + MVPP22_XLG_CTRL1_REG);
  3962. }
  3963. /* Set defaults to the MVPP2 port */
  3964. static void mvpp2_defaults_set(struct mvpp2_port *port)
  3965. {
  3966. int tx_port_num, val, queue, ptxq, lrxq;
  3967. if (port->priv->hw_version == MVPP21) {
  3968. /* Configure port to loopback if needed */
  3969. if (port->flags & MVPP2_F_LOOPBACK)
  3970. mvpp2_port_loopback_set(port);
  3971. /* Update TX FIFO MIN Threshold */
  3972. val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
  3973. val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
  3974. /* Min. TX threshold must be less than minimal packet length */
  3975. val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(64 - 4 - 2);
  3976. writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
  3977. }
  3978. /* Disable Legacy WRR, Disable EJP, Release from reset */
  3979. tx_port_num = mvpp2_egress_port(port);
  3980. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG,
  3981. tx_port_num);
  3982. mvpp2_write(port->priv, MVPP2_TXP_SCHED_CMD_1_REG, 0);
  3983. /* Close bandwidth for all queues */
  3984. for (queue = 0; queue < MVPP2_MAX_TXQ; queue++) {
  3985. ptxq = mvpp2_txq_phys(port->id, queue);
  3986. mvpp2_write(port->priv,
  3987. MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(ptxq), 0);
  3988. }
  3989. /* Set refill period to 1 usec, refill tokens
  3990. * and bucket size to maximum
  3991. */
  3992. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PERIOD_REG,
  3993. port->priv->tclk / USEC_PER_SEC);
  3994. val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_REFILL_REG);
  3995. val &= ~MVPP2_TXP_REFILL_PERIOD_ALL_MASK;
  3996. val |= MVPP2_TXP_REFILL_PERIOD_MASK(1);
  3997. val |= MVPP2_TXP_REFILL_TOKENS_ALL_MASK;
  3998. mvpp2_write(port->priv, MVPP2_TXP_SCHED_REFILL_REG, val);
  3999. val = MVPP2_TXP_TOKEN_SIZE_MAX;
  4000. mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
  4001. /* Set MaximumLowLatencyPacketSize value to 256 */
  4002. mvpp2_write(port->priv, MVPP2_RX_CTRL_REG(port->id),
  4003. MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK |
  4004. MVPP2_RX_LOW_LATENCY_PKT_SIZE(256));
  4005. /* Enable Rx cache snoop */
  4006. for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
  4007. queue = port->rxqs[lrxq]->id;
  4008. val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
  4009. val |= MVPP2_SNOOP_PKT_SIZE_MASK |
  4010. MVPP2_SNOOP_BUF_HDR_MASK;
  4011. mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
  4012. }
  4013. /* At default, mask all interrupts to all present cpus */
  4014. mvpp2_interrupts_disable(port);
  4015. }
  4016. /* Enable/disable receiving packets */
  4017. static void mvpp2_ingress_enable(struct mvpp2_port *port)
  4018. {
  4019. u32 val;
  4020. int lrxq, queue;
  4021. for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
  4022. queue = port->rxqs[lrxq]->id;
  4023. val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
  4024. val &= ~MVPP2_RXQ_DISABLE_MASK;
  4025. mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
  4026. }
  4027. }
  4028. static void mvpp2_ingress_disable(struct mvpp2_port *port)
  4029. {
  4030. u32 val;
  4031. int lrxq, queue;
  4032. for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
  4033. queue = port->rxqs[lrxq]->id;
  4034. val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
  4035. val |= MVPP2_RXQ_DISABLE_MASK;
  4036. mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
  4037. }
  4038. }
  4039. /* Enable transmit via physical egress queue
  4040. * - HW starts take descriptors from DRAM
  4041. */
  4042. static void mvpp2_egress_enable(struct mvpp2_port *port)
  4043. {
  4044. u32 qmap;
  4045. int queue;
  4046. int tx_port_num = mvpp2_egress_port(port);
  4047. /* Enable all initialized TXs. */
  4048. qmap = 0;
  4049. for (queue = 0; queue < port->ntxqs; queue++) {
  4050. struct mvpp2_tx_queue *txq = port->txqs[queue];
  4051. if (txq->descs)
  4052. qmap |= (1 << queue);
  4053. }
  4054. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
  4055. mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG, qmap);
  4056. }
  4057. /* Disable transmit via physical egress queue
  4058. * - HW doesn't take descriptors from DRAM
  4059. */
  4060. static void mvpp2_egress_disable(struct mvpp2_port *port)
  4061. {
  4062. u32 reg_data;
  4063. int delay;
  4064. int tx_port_num = mvpp2_egress_port(port);
  4065. /* Issue stop command for active channels only */
  4066. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
  4067. reg_data = (mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG)) &
  4068. MVPP2_TXP_SCHED_ENQ_MASK;
  4069. if (reg_data != 0)
  4070. mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG,
  4071. (reg_data << MVPP2_TXP_SCHED_DISQ_OFFSET));
  4072. /* Wait for all Tx activity to terminate. */
  4073. delay = 0;
  4074. do {
  4075. if (delay >= MVPP2_TX_DISABLE_TIMEOUT_MSEC) {
  4076. netdev_warn(port->dev,
  4077. "Tx stop timed out, status=0x%08x\n",
  4078. reg_data);
  4079. break;
  4080. }
  4081. mdelay(1);
  4082. delay++;
  4083. /* Check port TX Command register that all
  4084. * Tx queues are stopped
  4085. */
  4086. reg_data = mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG);
  4087. } while (reg_data & MVPP2_TXP_SCHED_ENQ_MASK);
  4088. }
  4089. /* Rx descriptors helper methods */
  4090. /* Get number of Rx descriptors occupied by received packets */
  4091. static inline int
  4092. mvpp2_rxq_received(struct mvpp2_port *port, int rxq_id)
  4093. {
  4094. u32 val = mvpp2_read(port->priv, MVPP2_RXQ_STATUS_REG(rxq_id));
  4095. return val & MVPP2_RXQ_OCCUPIED_MASK;
  4096. }
  4097. /* Update Rx queue status with the number of occupied and available
  4098. * Rx descriptor slots.
  4099. */
  4100. static inline void
  4101. mvpp2_rxq_status_update(struct mvpp2_port *port, int rxq_id,
  4102. int used_count, int free_count)
  4103. {
  4104. /* Decrement the number of used descriptors and increment count
  4105. * increment the number of free descriptors.
  4106. */
  4107. u32 val = used_count | (free_count << MVPP2_RXQ_NUM_NEW_OFFSET);
  4108. mvpp2_write(port->priv, MVPP2_RXQ_STATUS_UPDATE_REG(rxq_id), val);
  4109. }
  4110. /* Get pointer to next RX descriptor to be processed by SW */
  4111. static inline struct mvpp2_rx_desc *
  4112. mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue *rxq)
  4113. {
  4114. int rx_desc = rxq->next_desc_to_proc;
  4115. rxq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(rxq, rx_desc);
  4116. prefetch(rxq->descs + rxq->next_desc_to_proc);
  4117. return rxq->descs + rx_desc;
  4118. }
  4119. /* Set rx queue offset */
  4120. static void mvpp2_rxq_offset_set(struct mvpp2_port *port,
  4121. int prxq, int offset)
  4122. {
  4123. u32 val;
  4124. /* Convert offset from bytes to units of 32 bytes */
  4125. offset = offset >> 5;
  4126. val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
  4127. val &= ~MVPP2_RXQ_PACKET_OFFSET_MASK;
  4128. /* Offset is in */
  4129. val |= ((offset << MVPP2_RXQ_PACKET_OFFSET_OFFS) &
  4130. MVPP2_RXQ_PACKET_OFFSET_MASK);
  4131. mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
  4132. }
  4133. /* Tx descriptors helper methods */
  4134. /* Get pointer to next Tx descriptor to be processed (send) by HW */
  4135. static struct mvpp2_tx_desc *
  4136. mvpp2_txq_next_desc_get(struct mvpp2_tx_queue *txq)
  4137. {
  4138. int tx_desc = txq->next_desc_to_proc;
  4139. txq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(txq, tx_desc);
  4140. return txq->descs + tx_desc;
  4141. }
  4142. /* Update HW with number of aggregated Tx descriptors to be sent
  4143. *
  4144. * Called only from mvpp2_tx(), so migration is disabled, using
  4145. * smp_processor_id() is OK.
  4146. */
  4147. static void mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port *port, int pending)
  4148. {
  4149. /* aggregated access - relevant TXQ number is written in TX desc */
  4150. mvpp2_percpu_write(port->priv, smp_processor_id(),
  4151. MVPP2_AGGR_TXQ_UPDATE_REG, pending);
  4152. }
  4153. /* Check if there are enough free descriptors in aggregated txq.
  4154. * If not, update the number of occupied descriptors and repeat the check.
  4155. *
  4156. * Called only from mvpp2_tx(), so migration is disabled, using
  4157. * smp_processor_id() is OK.
  4158. */
  4159. static int mvpp2_aggr_desc_num_check(struct mvpp2 *priv,
  4160. struct mvpp2_tx_queue *aggr_txq, int num)
  4161. {
  4162. if ((aggr_txq->count + num) > aggr_txq->size) {
  4163. /* Update number of occupied aggregated Tx descriptors */
  4164. int cpu = smp_processor_id();
  4165. u32 val = mvpp2_read(priv, MVPP2_AGGR_TXQ_STATUS_REG(cpu));
  4166. aggr_txq->count = val & MVPP2_AGGR_TXQ_PENDING_MASK;
  4167. }
  4168. if ((aggr_txq->count + num) > aggr_txq->size)
  4169. return -ENOMEM;
  4170. return 0;
  4171. }
  4172. /* Reserved Tx descriptors allocation request
  4173. *
  4174. * Called only from mvpp2_txq_reserved_desc_num_proc(), itself called
  4175. * only by mvpp2_tx(), so migration is disabled, using
  4176. * smp_processor_id() is OK.
  4177. */
  4178. static int mvpp2_txq_alloc_reserved_desc(struct mvpp2 *priv,
  4179. struct mvpp2_tx_queue *txq, int num)
  4180. {
  4181. u32 val;
  4182. int cpu = smp_processor_id();
  4183. val = (txq->id << MVPP2_TXQ_RSVD_REQ_Q_OFFSET) | num;
  4184. mvpp2_percpu_write(priv, cpu, MVPP2_TXQ_RSVD_REQ_REG, val);
  4185. val = mvpp2_percpu_read(priv, cpu, MVPP2_TXQ_RSVD_RSLT_REG);
  4186. return val & MVPP2_TXQ_RSVD_RSLT_MASK;
  4187. }
  4188. /* Check if there are enough reserved descriptors for transmission.
  4189. * If not, request chunk of reserved descriptors and check again.
  4190. */
  4191. static int mvpp2_txq_reserved_desc_num_proc(struct mvpp2 *priv,
  4192. struct mvpp2_tx_queue *txq,
  4193. struct mvpp2_txq_pcpu *txq_pcpu,
  4194. int num)
  4195. {
  4196. int req, cpu, desc_count;
  4197. if (txq_pcpu->reserved_num >= num)
  4198. return 0;
  4199. /* Not enough descriptors reserved! Update the reserved descriptor
  4200. * count and check again.
  4201. */
  4202. desc_count = 0;
  4203. /* Compute total of used descriptors */
  4204. for_each_present_cpu(cpu) {
  4205. struct mvpp2_txq_pcpu *txq_pcpu_aux;
  4206. txq_pcpu_aux = per_cpu_ptr(txq->pcpu, cpu);
  4207. desc_count += txq_pcpu_aux->count;
  4208. desc_count += txq_pcpu_aux->reserved_num;
  4209. }
  4210. req = max(MVPP2_CPU_DESC_CHUNK, num - txq_pcpu->reserved_num);
  4211. desc_count += req;
  4212. if (desc_count >
  4213. (txq->size - (num_present_cpus() * MVPP2_CPU_DESC_CHUNK)))
  4214. return -ENOMEM;
  4215. txq_pcpu->reserved_num += mvpp2_txq_alloc_reserved_desc(priv, txq, req);
  4216. /* OK, the descriptor cound has been updated: check again. */
  4217. if (txq_pcpu->reserved_num < num)
  4218. return -ENOMEM;
  4219. return 0;
  4220. }
  4221. /* Release the last allocated Tx descriptor. Useful to handle DMA
  4222. * mapping failures in the Tx path.
  4223. */
  4224. static void mvpp2_txq_desc_put(struct mvpp2_tx_queue *txq)
  4225. {
  4226. if (txq->next_desc_to_proc == 0)
  4227. txq->next_desc_to_proc = txq->last_desc - 1;
  4228. else
  4229. txq->next_desc_to_proc--;
  4230. }
  4231. /* Set Tx descriptors fields relevant for CSUM calculation */
  4232. static u32 mvpp2_txq_desc_csum(int l3_offs, int l3_proto,
  4233. int ip_hdr_len, int l4_proto)
  4234. {
  4235. u32 command;
  4236. /* fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
  4237. * G_L4_chk, L4_type required only for checksum calculation
  4238. */
  4239. command = (l3_offs << MVPP2_TXD_L3_OFF_SHIFT);
  4240. command |= (ip_hdr_len << MVPP2_TXD_IP_HLEN_SHIFT);
  4241. command |= MVPP2_TXD_IP_CSUM_DISABLE;
  4242. if (l3_proto == swab16(ETH_P_IP)) {
  4243. command &= ~MVPP2_TXD_IP_CSUM_DISABLE; /* enable IPv4 csum */
  4244. command &= ~MVPP2_TXD_L3_IP6; /* enable IPv4 */
  4245. } else {
  4246. command |= MVPP2_TXD_L3_IP6; /* enable IPv6 */
  4247. }
  4248. if (l4_proto == IPPROTO_TCP) {
  4249. command &= ~MVPP2_TXD_L4_UDP; /* enable TCP */
  4250. command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
  4251. } else if (l4_proto == IPPROTO_UDP) {
  4252. command |= MVPP2_TXD_L4_UDP; /* enable UDP */
  4253. command &= ~MVPP2_TXD_L4_CSUM_FRAG; /* generate L4 csum */
  4254. } else {
  4255. command |= MVPP2_TXD_L4_CSUM_NOT;
  4256. }
  4257. return command;
  4258. }
  4259. /* Get number of sent descriptors and decrement counter.
  4260. * The number of sent descriptors is returned.
  4261. * Per-CPU access
  4262. *
  4263. * Called only from mvpp2_txq_done(), called from mvpp2_tx()
  4264. * (migration disabled) and from the TX completion tasklet (migration
  4265. * disabled) so using smp_processor_id() is OK.
  4266. */
  4267. static inline int mvpp2_txq_sent_desc_proc(struct mvpp2_port *port,
  4268. struct mvpp2_tx_queue *txq)
  4269. {
  4270. u32 val;
  4271. /* Reading status reg resets transmitted descriptor counter */
  4272. val = mvpp2_percpu_read(port->priv, smp_processor_id(),
  4273. MVPP2_TXQ_SENT_REG(txq->id));
  4274. return (val & MVPP2_TRANSMITTED_COUNT_MASK) >>
  4275. MVPP2_TRANSMITTED_COUNT_OFFSET;
  4276. }
  4277. /* Called through on_each_cpu(), so runs on all CPUs, with migration
  4278. * disabled, therefore using smp_processor_id() is OK.
  4279. */
  4280. static void mvpp2_txq_sent_counter_clear(void *arg)
  4281. {
  4282. struct mvpp2_port *port = arg;
  4283. int queue;
  4284. for (queue = 0; queue < port->ntxqs; queue++) {
  4285. int id = port->txqs[queue]->id;
  4286. mvpp2_percpu_read(port->priv, smp_processor_id(),
  4287. MVPP2_TXQ_SENT_REG(id));
  4288. }
  4289. }
  4290. /* Set max sizes for Tx queues */
  4291. static void mvpp2_txp_max_tx_size_set(struct mvpp2_port *port)
  4292. {
  4293. u32 val, size, mtu;
  4294. int txq, tx_port_num;
  4295. mtu = port->pkt_size * 8;
  4296. if (mtu > MVPP2_TXP_MTU_MAX)
  4297. mtu = MVPP2_TXP_MTU_MAX;
  4298. /* WA for wrong Token bucket update: Set MTU value = 3*real MTU value */
  4299. mtu = 3 * mtu;
  4300. /* Indirect access to registers */
  4301. tx_port_num = mvpp2_egress_port(port);
  4302. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
  4303. /* Set MTU */
  4304. val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_MTU_REG);
  4305. val &= ~MVPP2_TXP_MTU_MAX;
  4306. val |= mtu;
  4307. mvpp2_write(port->priv, MVPP2_TXP_SCHED_MTU_REG, val);
  4308. /* TXP token size and all TXQs token size must be larger that MTU */
  4309. val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG);
  4310. size = val & MVPP2_TXP_TOKEN_SIZE_MAX;
  4311. if (size < mtu) {
  4312. size = mtu;
  4313. val &= ~MVPP2_TXP_TOKEN_SIZE_MAX;
  4314. val |= size;
  4315. mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
  4316. }
  4317. for (txq = 0; txq < port->ntxqs; txq++) {
  4318. val = mvpp2_read(port->priv,
  4319. MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq));
  4320. size = val & MVPP2_TXQ_TOKEN_SIZE_MAX;
  4321. if (size < mtu) {
  4322. size = mtu;
  4323. val &= ~MVPP2_TXQ_TOKEN_SIZE_MAX;
  4324. val |= size;
  4325. mvpp2_write(port->priv,
  4326. MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq),
  4327. val);
  4328. }
  4329. }
  4330. }
  4331. /* Set the number of packets that will be received before Rx interrupt
  4332. * will be generated by HW.
  4333. */
  4334. static void mvpp2_rx_pkts_coal_set(struct mvpp2_port *port,
  4335. struct mvpp2_rx_queue *rxq)
  4336. {
  4337. int cpu = get_cpu();
  4338. if (rxq->pkts_coal > MVPP2_OCCUPIED_THRESH_MASK)
  4339. rxq->pkts_coal = MVPP2_OCCUPIED_THRESH_MASK;
  4340. mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
  4341. mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_THRESH_REG,
  4342. rxq->pkts_coal);
  4343. put_cpu();
  4344. }
  4345. /* For some reason in the LSP this is done on each CPU. Why ? */
  4346. static void mvpp2_tx_pkts_coal_set(struct mvpp2_port *port,
  4347. struct mvpp2_tx_queue *txq)
  4348. {
  4349. int cpu = get_cpu();
  4350. u32 val;
  4351. if (txq->done_pkts_coal > MVPP2_TXQ_THRESH_MASK)
  4352. txq->done_pkts_coal = MVPP2_TXQ_THRESH_MASK;
  4353. val = (txq->done_pkts_coal << MVPP2_TXQ_THRESH_OFFSET);
  4354. mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
  4355. mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_THRESH_REG, val);
  4356. put_cpu();
  4357. }
  4358. static u32 mvpp2_usec_to_cycles(u32 usec, unsigned long clk_hz)
  4359. {
  4360. u64 tmp = (u64)clk_hz * usec;
  4361. do_div(tmp, USEC_PER_SEC);
  4362. return tmp > U32_MAX ? U32_MAX : tmp;
  4363. }
  4364. static u32 mvpp2_cycles_to_usec(u32 cycles, unsigned long clk_hz)
  4365. {
  4366. u64 tmp = (u64)cycles * USEC_PER_SEC;
  4367. do_div(tmp, clk_hz);
  4368. return tmp > U32_MAX ? U32_MAX : tmp;
  4369. }
  4370. /* Set the time delay in usec before Rx interrupt */
  4371. static void mvpp2_rx_time_coal_set(struct mvpp2_port *port,
  4372. struct mvpp2_rx_queue *rxq)
  4373. {
  4374. unsigned long freq = port->priv->tclk;
  4375. u32 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
  4376. if (val > MVPP2_MAX_ISR_RX_THRESHOLD) {
  4377. rxq->time_coal =
  4378. mvpp2_cycles_to_usec(MVPP2_MAX_ISR_RX_THRESHOLD, freq);
  4379. /* re-evaluate to get actual register value */
  4380. val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
  4381. }
  4382. mvpp2_write(port->priv, MVPP2_ISR_RX_THRESHOLD_REG(rxq->id), val);
  4383. }
  4384. static void mvpp2_tx_time_coal_set(struct mvpp2_port *port)
  4385. {
  4386. unsigned long freq = port->priv->tclk;
  4387. u32 val = mvpp2_usec_to_cycles(port->tx_time_coal, freq);
  4388. if (val > MVPP2_MAX_ISR_TX_THRESHOLD) {
  4389. port->tx_time_coal =
  4390. mvpp2_cycles_to_usec(MVPP2_MAX_ISR_TX_THRESHOLD, freq);
  4391. /* re-evaluate to get actual register value */
  4392. val = mvpp2_usec_to_cycles(port->tx_time_coal, freq);
  4393. }
  4394. mvpp2_write(port->priv, MVPP2_ISR_TX_THRESHOLD_REG(port->id), val);
  4395. }
  4396. /* Free Tx queue skbuffs */
  4397. static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
  4398. struct mvpp2_tx_queue *txq,
  4399. struct mvpp2_txq_pcpu *txq_pcpu, int num)
  4400. {
  4401. int i;
  4402. for (i = 0; i < num; i++) {
  4403. struct mvpp2_txq_pcpu_buf *tx_buf =
  4404. txq_pcpu->buffs + txq_pcpu->txq_get_index;
  4405. dma_unmap_single(port->dev->dev.parent, tx_buf->dma,
  4406. tx_buf->size, DMA_TO_DEVICE);
  4407. if (tx_buf->skb)
  4408. dev_kfree_skb_any(tx_buf->skb);
  4409. mvpp2_txq_inc_get(txq_pcpu);
  4410. }
  4411. }
  4412. static inline struct mvpp2_rx_queue *mvpp2_get_rx_queue(struct mvpp2_port *port,
  4413. u32 cause)
  4414. {
  4415. int queue = fls(cause) - 1;
  4416. return port->rxqs[queue];
  4417. }
  4418. static inline struct mvpp2_tx_queue *mvpp2_get_tx_queue(struct mvpp2_port *port,
  4419. u32 cause)
  4420. {
  4421. int queue = fls(cause) - 1;
  4422. return port->txqs[queue];
  4423. }
  4424. /* Handle end of transmission */
  4425. static void mvpp2_txq_done(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
  4426. struct mvpp2_txq_pcpu *txq_pcpu)
  4427. {
  4428. struct netdev_queue *nq = netdev_get_tx_queue(port->dev, txq->log_id);
  4429. int tx_done;
  4430. if (txq_pcpu->cpu != smp_processor_id())
  4431. netdev_err(port->dev, "wrong cpu on the end of Tx processing\n");
  4432. tx_done = mvpp2_txq_sent_desc_proc(port, txq);
  4433. if (!tx_done)
  4434. return;
  4435. mvpp2_txq_bufs_free(port, txq, txq_pcpu, tx_done);
  4436. txq_pcpu->count -= tx_done;
  4437. if (netif_tx_queue_stopped(nq))
  4438. if (txq_pcpu->size - txq_pcpu->count >= MAX_SKB_FRAGS + 1)
  4439. netif_tx_wake_queue(nq);
  4440. }
  4441. static unsigned int mvpp2_tx_done(struct mvpp2_port *port, u32 cause,
  4442. int cpu)
  4443. {
  4444. struct mvpp2_tx_queue *txq;
  4445. struct mvpp2_txq_pcpu *txq_pcpu;
  4446. unsigned int tx_todo = 0;
  4447. while (cause) {
  4448. txq = mvpp2_get_tx_queue(port, cause);
  4449. if (!txq)
  4450. break;
  4451. txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
  4452. if (txq_pcpu->count) {
  4453. mvpp2_txq_done(port, txq, txq_pcpu);
  4454. tx_todo += txq_pcpu->count;
  4455. }
  4456. cause &= ~(1 << txq->log_id);
  4457. }
  4458. return tx_todo;
  4459. }
  4460. /* Rx/Tx queue initialization/cleanup methods */
  4461. /* Allocate and initialize descriptors for aggr TXQ */
  4462. static int mvpp2_aggr_txq_init(struct platform_device *pdev,
  4463. struct mvpp2_tx_queue *aggr_txq, int cpu,
  4464. struct mvpp2 *priv)
  4465. {
  4466. u32 txq_dma;
  4467. /* Allocate memory for TX descriptors */
  4468. aggr_txq->descs = dma_alloc_coherent(&pdev->dev,
  4469. MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
  4470. &aggr_txq->descs_dma, GFP_KERNEL);
  4471. if (!aggr_txq->descs)
  4472. return -ENOMEM;
  4473. aggr_txq->last_desc = aggr_txq->size - 1;
  4474. /* Aggr TXQ no reset WA */
  4475. aggr_txq->next_desc_to_proc = mvpp2_read(priv,
  4476. MVPP2_AGGR_TXQ_INDEX_REG(cpu));
  4477. /* Set Tx descriptors queue starting address indirect
  4478. * access
  4479. */
  4480. if (priv->hw_version == MVPP21)
  4481. txq_dma = aggr_txq->descs_dma;
  4482. else
  4483. txq_dma = aggr_txq->descs_dma >>
  4484. MVPP22_AGGR_TXQ_DESC_ADDR_OFFS;
  4485. mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu), txq_dma);
  4486. mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu),
  4487. MVPP2_AGGR_TXQ_SIZE);
  4488. return 0;
  4489. }
  4490. /* Create a specified Rx queue */
  4491. static int mvpp2_rxq_init(struct mvpp2_port *port,
  4492. struct mvpp2_rx_queue *rxq)
  4493. {
  4494. u32 rxq_dma;
  4495. int cpu;
  4496. rxq->size = port->rx_ring_size;
  4497. /* Allocate memory for RX descriptors */
  4498. rxq->descs = dma_alloc_coherent(port->dev->dev.parent,
  4499. rxq->size * MVPP2_DESC_ALIGNED_SIZE,
  4500. &rxq->descs_dma, GFP_KERNEL);
  4501. if (!rxq->descs)
  4502. return -ENOMEM;
  4503. rxq->last_desc = rxq->size - 1;
  4504. /* Zero occupied and non-occupied counters - direct access */
  4505. mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
  4506. /* Set Rx descriptors queue starting address - indirect access */
  4507. cpu = get_cpu();
  4508. mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
  4509. if (port->priv->hw_version == MVPP21)
  4510. rxq_dma = rxq->descs_dma;
  4511. else
  4512. rxq_dma = rxq->descs_dma >> MVPP22_DESC_ADDR_OFFS;
  4513. mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_ADDR_REG, rxq_dma);
  4514. mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_SIZE_REG, rxq->size);
  4515. mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_INDEX_REG, 0);
  4516. put_cpu();
  4517. /* Set Offset */
  4518. mvpp2_rxq_offset_set(port, rxq->id, NET_SKB_PAD);
  4519. /* Set coalescing pkts and time */
  4520. mvpp2_rx_pkts_coal_set(port, rxq);
  4521. mvpp2_rx_time_coal_set(port, rxq);
  4522. /* Add number of descriptors ready for receiving packets */
  4523. mvpp2_rxq_status_update(port, rxq->id, 0, rxq->size);
  4524. return 0;
  4525. }
  4526. /* Push packets received by the RXQ to BM pool */
  4527. static void mvpp2_rxq_drop_pkts(struct mvpp2_port *port,
  4528. struct mvpp2_rx_queue *rxq)
  4529. {
  4530. int rx_received, i;
  4531. rx_received = mvpp2_rxq_received(port, rxq->id);
  4532. if (!rx_received)
  4533. return;
  4534. for (i = 0; i < rx_received; i++) {
  4535. struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
  4536. u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
  4537. int pool;
  4538. pool = (status & MVPP2_RXD_BM_POOL_ID_MASK) >>
  4539. MVPP2_RXD_BM_POOL_ID_OFFS;
  4540. mvpp2_bm_pool_put(port, pool,
  4541. mvpp2_rxdesc_dma_addr_get(port, rx_desc),
  4542. mvpp2_rxdesc_cookie_get(port, rx_desc));
  4543. }
  4544. mvpp2_rxq_status_update(port, rxq->id, rx_received, rx_received);
  4545. }
  4546. /* Cleanup Rx queue */
  4547. static void mvpp2_rxq_deinit(struct mvpp2_port *port,
  4548. struct mvpp2_rx_queue *rxq)
  4549. {
  4550. int cpu;
  4551. mvpp2_rxq_drop_pkts(port, rxq);
  4552. if (rxq->descs)
  4553. dma_free_coherent(port->dev->dev.parent,
  4554. rxq->size * MVPP2_DESC_ALIGNED_SIZE,
  4555. rxq->descs,
  4556. rxq->descs_dma);
  4557. rxq->descs = NULL;
  4558. rxq->last_desc = 0;
  4559. rxq->next_desc_to_proc = 0;
  4560. rxq->descs_dma = 0;
  4561. /* Clear Rx descriptors queue starting address and size;
  4562. * free descriptor number
  4563. */
  4564. mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
  4565. cpu = get_cpu();
  4566. mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
  4567. mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_ADDR_REG, 0);
  4568. mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_SIZE_REG, 0);
  4569. put_cpu();
  4570. }
  4571. /* Create and initialize a Tx queue */
  4572. static int mvpp2_txq_init(struct mvpp2_port *port,
  4573. struct mvpp2_tx_queue *txq)
  4574. {
  4575. u32 val;
  4576. int cpu, desc, desc_per_txq, tx_port_num;
  4577. struct mvpp2_txq_pcpu *txq_pcpu;
  4578. txq->size = port->tx_ring_size;
  4579. /* Allocate memory for Tx descriptors */
  4580. txq->descs = dma_alloc_coherent(port->dev->dev.parent,
  4581. txq->size * MVPP2_DESC_ALIGNED_SIZE,
  4582. &txq->descs_dma, GFP_KERNEL);
  4583. if (!txq->descs)
  4584. return -ENOMEM;
  4585. txq->last_desc = txq->size - 1;
  4586. /* Set Tx descriptors queue starting address - indirect access */
  4587. cpu = get_cpu();
  4588. mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
  4589. mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_ADDR_REG,
  4590. txq->descs_dma);
  4591. mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_SIZE_REG,
  4592. txq->size & MVPP2_TXQ_DESC_SIZE_MASK);
  4593. mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_INDEX_REG, 0);
  4594. mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_RSVD_CLR_REG,
  4595. txq->id << MVPP2_TXQ_RSVD_CLR_OFFSET);
  4596. val = mvpp2_percpu_read(port->priv, cpu, MVPP2_TXQ_PENDING_REG);
  4597. val &= ~MVPP2_TXQ_PENDING_MASK;
  4598. mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PENDING_REG, val);
  4599. /* Calculate base address in prefetch buffer. We reserve 16 descriptors
  4600. * for each existing TXQ.
  4601. * TCONTS for PON port must be continuous from 0 to MVPP2_MAX_TCONT
  4602. * GBE ports assumed to be continious from 0 to MVPP2_MAX_PORTS
  4603. */
  4604. desc_per_txq = 16;
  4605. desc = (port->id * MVPP2_MAX_TXQ * desc_per_txq) +
  4606. (txq->log_id * desc_per_txq);
  4607. mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG,
  4608. MVPP2_PREF_BUF_PTR(desc) | MVPP2_PREF_BUF_SIZE_16 |
  4609. MVPP2_PREF_BUF_THRESH(desc_per_txq / 2));
  4610. put_cpu();
  4611. /* WRR / EJP configuration - indirect access */
  4612. tx_port_num = mvpp2_egress_port(port);
  4613. mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
  4614. val = mvpp2_read(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id));
  4615. val &= ~MVPP2_TXQ_REFILL_PERIOD_ALL_MASK;
  4616. val |= MVPP2_TXQ_REFILL_PERIOD_MASK(1);
  4617. val |= MVPP2_TXQ_REFILL_TOKENS_ALL_MASK;
  4618. mvpp2_write(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id), val);
  4619. val = MVPP2_TXQ_TOKEN_SIZE_MAX;
  4620. mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq->log_id),
  4621. val);
  4622. for_each_present_cpu(cpu) {
  4623. txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
  4624. txq_pcpu->size = txq->size;
  4625. txq_pcpu->buffs = kmalloc_array(txq_pcpu->size,
  4626. sizeof(*txq_pcpu->buffs),
  4627. GFP_KERNEL);
  4628. if (!txq_pcpu->buffs)
  4629. goto cleanup;
  4630. txq_pcpu->count = 0;
  4631. txq_pcpu->reserved_num = 0;
  4632. txq_pcpu->txq_put_index = 0;
  4633. txq_pcpu->txq_get_index = 0;
  4634. txq_pcpu->tso_headers =
  4635. dma_alloc_coherent(port->dev->dev.parent,
  4636. MVPP2_AGGR_TXQ_SIZE * TSO_HEADER_SIZE,
  4637. &txq_pcpu->tso_headers_dma,
  4638. GFP_KERNEL);
  4639. if (!txq_pcpu->tso_headers)
  4640. goto cleanup;
  4641. }
  4642. return 0;
  4643. cleanup:
  4644. for_each_present_cpu(cpu) {
  4645. txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
  4646. kfree(txq_pcpu->buffs);
  4647. dma_free_coherent(port->dev->dev.parent,
  4648. MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
  4649. txq_pcpu->tso_headers,
  4650. txq_pcpu->tso_headers_dma);
  4651. }
  4652. dma_free_coherent(port->dev->dev.parent,
  4653. txq->size * MVPP2_DESC_ALIGNED_SIZE,
  4654. txq->descs, txq->descs_dma);
  4655. return -ENOMEM;
  4656. }
  4657. /* Free allocated TXQ resources */
  4658. static void mvpp2_txq_deinit(struct mvpp2_port *port,
  4659. struct mvpp2_tx_queue *txq)
  4660. {
  4661. struct mvpp2_txq_pcpu *txq_pcpu;
  4662. int cpu;
  4663. for_each_present_cpu(cpu) {
  4664. txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
  4665. kfree(txq_pcpu->buffs);
  4666. dma_free_coherent(port->dev->dev.parent,
  4667. MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
  4668. txq_pcpu->tso_headers,
  4669. txq_pcpu->tso_headers_dma);
  4670. }
  4671. if (txq->descs)
  4672. dma_free_coherent(port->dev->dev.parent,
  4673. txq->size * MVPP2_DESC_ALIGNED_SIZE,
  4674. txq->descs, txq->descs_dma);
  4675. txq->descs = NULL;
  4676. txq->last_desc = 0;
  4677. txq->next_desc_to_proc = 0;
  4678. txq->descs_dma = 0;
  4679. /* Set minimum bandwidth for disabled TXQs */
  4680. mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(txq->id), 0);
  4681. /* Set Tx descriptors queue starting address and size */
  4682. cpu = get_cpu();
  4683. mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
  4684. mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_ADDR_REG, 0);
  4685. mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_SIZE_REG, 0);
  4686. put_cpu();
  4687. }
  4688. /* Cleanup Tx ports */
  4689. static void mvpp2_txq_clean(struct mvpp2_port *port, struct mvpp2_tx_queue *txq)
  4690. {
  4691. struct mvpp2_txq_pcpu *txq_pcpu;
  4692. int delay, pending, cpu;
  4693. u32 val;
  4694. cpu = get_cpu();
  4695. mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
  4696. val = mvpp2_percpu_read(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG);
  4697. val |= MVPP2_TXQ_DRAIN_EN_MASK;
  4698. mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG, val);
  4699. /* The napi queue has been stopped so wait for all packets
  4700. * to be transmitted.
  4701. */
  4702. delay = 0;
  4703. do {
  4704. if (delay >= MVPP2_TX_PENDING_TIMEOUT_MSEC) {
  4705. netdev_warn(port->dev,
  4706. "port %d: cleaning queue %d timed out\n",
  4707. port->id, txq->log_id);
  4708. break;
  4709. }
  4710. mdelay(1);
  4711. delay++;
  4712. pending = mvpp2_percpu_read(port->priv, cpu,
  4713. MVPP2_TXQ_PENDING_REG);
  4714. pending &= MVPP2_TXQ_PENDING_MASK;
  4715. } while (pending);
  4716. val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
  4717. mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG, val);
  4718. put_cpu();
  4719. for_each_present_cpu(cpu) {
  4720. txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
  4721. /* Release all packets */
  4722. mvpp2_txq_bufs_free(port, txq, txq_pcpu, txq_pcpu->count);
  4723. /* Reset queue */
  4724. txq_pcpu->count = 0;
  4725. txq_pcpu->txq_put_index = 0;
  4726. txq_pcpu->txq_get_index = 0;
  4727. }
  4728. }
  4729. /* Cleanup all Tx queues */
  4730. static void mvpp2_cleanup_txqs(struct mvpp2_port *port)
  4731. {
  4732. struct mvpp2_tx_queue *txq;
  4733. int queue;
  4734. u32 val;
  4735. val = mvpp2_read(port->priv, MVPP2_TX_PORT_FLUSH_REG);
  4736. /* Reset Tx ports and delete Tx queues */
  4737. val |= MVPP2_TX_PORT_FLUSH_MASK(port->id);
  4738. mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
  4739. for (queue = 0; queue < port->ntxqs; queue++) {
  4740. txq = port->txqs[queue];
  4741. mvpp2_txq_clean(port, txq);
  4742. mvpp2_txq_deinit(port, txq);
  4743. }
  4744. on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
  4745. val &= ~MVPP2_TX_PORT_FLUSH_MASK(port->id);
  4746. mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
  4747. }
  4748. /* Cleanup all Rx queues */
  4749. static void mvpp2_cleanup_rxqs(struct mvpp2_port *port)
  4750. {
  4751. int queue;
  4752. for (queue = 0; queue < port->nrxqs; queue++)
  4753. mvpp2_rxq_deinit(port, port->rxqs[queue]);
  4754. }
  4755. /* Init all Rx queues for port */
  4756. static int mvpp2_setup_rxqs(struct mvpp2_port *port)
  4757. {
  4758. int queue, err;
  4759. for (queue = 0; queue < port->nrxqs; queue++) {
  4760. err = mvpp2_rxq_init(port, port->rxqs[queue]);
  4761. if (err)
  4762. goto err_cleanup;
  4763. }
  4764. return 0;
  4765. err_cleanup:
  4766. mvpp2_cleanup_rxqs(port);
  4767. return err;
  4768. }
  4769. /* Init all tx queues for port */
  4770. static int mvpp2_setup_txqs(struct mvpp2_port *port)
  4771. {
  4772. struct mvpp2_tx_queue *txq;
  4773. int queue, err;
  4774. for (queue = 0; queue < port->ntxqs; queue++) {
  4775. txq = port->txqs[queue];
  4776. err = mvpp2_txq_init(port, txq);
  4777. if (err)
  4778. goto err_cleanup;
  4779. }
  4780. if (port->has_tx_irqs) {
  4781. mvpp2_tx_time_coal_set(port);
  4782. for (queue = 0; queue < port->ntxqs; queue++) {
  4783. txq = port->txqs[queue];
  4784. mvpp2_tx_pkts_coal_set(port, txq);
  4785. }
  4786. }
  4787. on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
  4788. return 0;
  4789. err_cleanup:
  4790. mvpp2_cleanup_txqs(port);
  4791. return err;
  4792. }
  4793. /* The callback for per-port interrupt */
  4794. static irqreturn_t mvpp2_isr(int irq, void *dev_id)
  4795. {
  4796. struct mvpp2_queue_vector *qv = dev_id;
  4797. mvpp2_qvec_interrupt_disable(qv);
  4798. napi_schedule(&qv->napi);
  4799. return IRQ_HANDLED;
  4800. }
  4801. /* Per-port interrupt for link status changes */
  4802. static irqreturn_t mvpp2_link_status_isr(int irq, void *dev_id)
  4803. {
  4804. struct mvpp2_port *port = (struct mvpp2_port *)dev_id;
  4805. struct net_device *dev = port->dev;
  4806. bool event = false, link = false;
  4807. u32 val;
  4808. mvpp22_gop_mask_irq(port);
  4809. if (port->gop_id == 0 &&
  4810. port->phy_interface == PHY_INTERFACE_MODE_10GKR) {
  4811. val = readl(port->base + MVPP22_XLG_INT_STAT);
  4812. if (val & MVPP22_XLG_INT_STAT_LINK) {
  4813. event = true;
  4814. val = readl(port->base + MVPP22_XLG_STATUS);
  4815. if (val & MVPP22_XLG_STATUS_LINK_UP)
  4816. link = true;
  4817. }
  4818. } else if (phy_interface_mode_is_rgmii(port->phy_interface) ||
  4819. port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
  4820. val = readl(port->base + MVPP22_GMAC_INT_STAT);
  4821. if (val & MVPP22_GMAC_INT_STAT_LINK) {
  4822. event = true;
  4823. val = readl(port->base + MVPP2_GMAC_STATUS0);
  4824. if (val & MVPP2_GMAC_STATUS0_LINK_UP)
  4825. link = true;
  4826. }
  4827. }
  4828. if (!netif_running(dev) || !event)
  4829. goto handled;
  4830. if (link) {
  4831. mvpp2_interrupts_enable(port);
  4832. mvpp2_egress_enable(port);
  4833. mvpp2_ingress_enable(port);
  4834. netif_carrier_on(dev);
  4835. netif_tx_wake_all_queues(dev);
  4836. } else {
  4837. netif_tx_stop_all_queues(dev);
  4838. netif_carrier_off(dev);
  4839. mvpp2_ingress_disable(port);
  4840. mvpp2_egress_disable(port);
  4841. mvpp2_interrupts_disable(port);
  4842. }
  4843. handled:
  4844. mvpp22_gop_unmask_irq(port);
  4845. return IRQ_HANDLED;
  4846. }
  4847. static void mvpp2_gmac_set_autoneg(struct mvpp2_port *port,
  4848. struct phy_device *phydev)
  4849. {
  4850. u32 val;
  4851. if (port->phy_interface != PHY_INTERFACE_MODE_RGMII &&
  4852. port->phy_interface != PHY_INTERFACE_MODE_RGMII_ID &&
  4853. port->phy_interface != PHY_INTERFACE_MODE_RGMII_RXID &&
  4854. port->phy_interface != PHY_INTERFACE_MODE_RGMII_TXID &&
  4855. port->phy_interface != PHY_INTERFACE_MODE_SGMII)
  4856. return;
  4857. val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  4858. val &= ~(MVPP2_GMAC_CONFIG_MII_SPEED |
  4859. MVPP2_GMAC_CONFIG_GMII_SPEED |
  4860. MVPP2_GMAC_CONFIG_FULL_DUPLEX |
  4861. MVPP2_GMAC_AN_SPEED_EN |
  4862. MVPP2_GMAC_AN_DUPLEX_EN);
  4863. if (phydev->duplex)
  4864. val |= MVPP2_GMAC_CONFIG_FULL_DUPLEX;
  4865. if (phydev->speed == SPEED_1000)
  4866. val |= MVPP2_GMAC_CONFIG_GMII_SPEED;
  4867. else if (phydev->speed == SPEED_100)
  4868. val |= MVPP2_GMAC_CONFIG_MII_SPEED;
  4869. writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  4870. }
  4871. /* Adjust link */
  4872. static void mvpp2_link_event(struct net_device *dev)
  4873. {
  4874. struct mvpp2_port *port = netdev_priv(dev);
  4875. struct phy_device *phydev = dev->phydev;
  4876. bool link_reconfigured = false;
  4877. u32 val;
  4878. if (phydev->link) {
  4879. if (port->phy_interface != phydev->interface && port->comphy) {
  4880. /* disable current port for reconfiguration */
  4881. mvpp2_interrupts_disable(port);
  4882. netif_carrier_off(port->dev);
  4883. mvpp2_port_disable(port);
  4884. phy_power_off(port->comphy);
  4885. /* comphy reconfiguration */
  4886. port->phy_interface = phydev->interface;
  4887. mvpp22_comphy_init(port);
  4888. /* gop/mac reconfiguration */
  4889. mvpp22_gop_init(port);
  4890. mvpp2_port_mii_set(port);
  4891. link_reconfigured = true;
  4892. }
  4893. if ((port->speed != phydev->speed) ||
  4894. (port->duplex != phydev->duplex)) {
  4895. mvpp2_gmac_set_autoneg(port, phydev);
  4896. port->duplex = phydev->duplex;
  4897. port->speed = phydev->speed;
  4898. }
  4899. }
  4900. if (phydev->link != port->link || link_reconfigured) {
  4901. port->link = phydev->link;
  4902. if (phydev->link) {
  4903. if (port->phy_interface == PHY_INTERFACE_MODE_RGMII ||
  4904. port->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
  4905. port->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
  4906. port->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID ||
  4907. port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
  4908. val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  4909. val |= (MVPP2_GMAC_FORCE_LINK_PASS |
  4910. MVPP2_GMAC_FORCE_LINK_DOWN);
  4911. writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
  4912. }
  4913. mvpp2_interrupts_enable(port);
  4914. mvpp2_port_enable(port);
  4915. mvpp2_egress_enable(port);
  4916. mvpp2_ingress_enable(port);
  4917. netif_carrier_on(dev);
  4918. netif_tx_wake_all_queues(dev);
  4919. } else {
  4920. port->duplex = -1;
  4921. port->speed = 0;
  4922. netif_tx_stop_all_queues(dev);
  4923. netif_carrier_off(dev);
  4924. mvpp2_ingress_disable(port);
  4925. mvpp2_egress_disable(port);
  4926. mvpp2_port_disable(port);
  4927. mvpp2_interrupts_disable(port);
  4928. }
  4929. phy_print_status(phydev);
  4930. }
  4931. }
  4932. static void mvpp2_timer_set(struct mvpp2_port_pcpu *port_pcpu)
  4933. {
  4934. ktime_t interval;
  4935. if (!port_pcpu->timer_scheduled) {
  4936. port_pcpu->timer_scheduled = true;
  4937. interval = MVPP2_TXDONE_HRTIMER_PERIOD_NS;
  4938. hrtimer_start(&port_pcpu->tx_done_timer, interval,
  4939. HRTIMER_MODE_REL_PINNED);
  4940. }
  4941. }
  4942. static void mvpp2_tx_proc_cb(unsigned long data)
  4943. {
  4944. struct net_device *dev = (struct net_device *)data;
  4945. struct mvpp2_port *port = netdev_priv(dev);
  4946. struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
  4947. unsigned int tx_todo, cause;
  4948. if (!netif_running(dev))
  4949. return;
  4950. port_pcpu->timer_scheduled = false;
  4951. /* Process all the Tx queues */
  4952. cause = (1 << port->ntxqs) - 1;
  4953. tx_todo = mvpp2_tx_done(port, cause, smp_processor_id());
  4954. /* Set the timer in case not all the packets were processed */
  4955. if (tx_todo)
  4956. mvpp2_timer_set(port_pcpu);
  4957. }
  4958. static enum hrtimer_restart mvpp2_hr_timer_cb(struct hrtimer *timer)
  4959. {
  4960. struct mvpp2_port_pcpu *port_pcpu = container_of(timer,
  4961. struct mvpp2_port_pcpu,
  4962. tx_done_timer);
  4963. tasklet_schedule(&port_pcpu->tx_done_tasklet);
  4964. return HRTIMER_NORESTART;
  4965. }
  4966. /* Main RX/TX processing routines */
  4967. /* Display more error info */
  4968. static void mvpp2_rx_error(struct mvpp2_port *port,
  4969. struct mvpp2_rx_desc *rx_desc)
  4970. {
  4971. u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
  4972. size_t sz = mvpp2_rxdesc_size_get(port, rx_desc);
  4973. switch (status & MVPP2_RXD_ERR_CODE_MASK) {
  4974. case MVPP2_RXD_ERR_CRC:
  4975. netdev_err(port->dev, "bad rx status %08x (crc error), size=%zu\n",
  4976. status, sz);
  4977. break;
  4978. case MVPP2_RXD_ERR_OVERRUN:
  4979. netdev_err(port->dev, "bad rx status %08x (overrun error), size=%zu\n",
  4980. status, sz);
  4981. break;
  4982. case MVPP2_RXD_ERR_RESOURCE:
  4983. netdev_err(port->dev, "bad rx status %08x (resource error), size=%zu\n",
  4984. status, sz);
  4985. break;
  4986. }
  4987. }
  4988. /* Handle RX checksum offload */
  4989. static void mvpp2_rx_csum(struct mvpp2_port *port, u32 status,
  4990. struct sk_buff *skb)
  4991. {
  4992. if (((status & MVPP2_RXD_L3_IP4) &&
  4993. !(status & MVPP2_RXD_IP4_HEADER_ERR)) ||
  4994. (status & MVPP2_RXD_L3_IP6))
  4995. if (((status & MVPP2_RXD_L4_UDP) ||
  4996. (status & MVPP2_RXD_L4_TCP)) &&
  4997. (status & MVPP2_RXD_L4_CSUM_OK)) {
  4998. skb->csum = 0;
  4999. skb->ip_summed = CHECKSUM_UNNECESSARY;
  5000. return;
  5001. }
  5002. skb->ip_summed = CHECKSUM_NONE;
  5003. }
  5004. /* Reuse skb if possible, or allocate a new skb and add it to BM pool */
  5005. static int mvpp2_rx_refill(struct mvpp2_port *port,
  5006. struct mvpp2_bm_pool *bm_pool, int pool)
  5007. {
  5008. dma_addr_t dma_addr;
  5009. phys_addr_t phys_addr;
  5010. void *buf;
  5011. /* No recycle or too many buffers are in use, so allocate a new skb */
  5012. buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr, &phys_addr,
  5013. GFP_ATOMIC);
  5014. if (!buf)
  5015. return -ENOMEM;
  5016. mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
  5017. return 0;
  5018. }
  5019. /* Handle tx checksum */
  5020. static u32 mvpp2_skb_tx_csum(struct mvpp2_port *port, struct sk_buff *skb)
  5021. {
  5022. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  5023. int ip_hdr_len = 0;
  5024. u8 l4_proto;
  5025. if (skb->protocol == htons(ETH_P_IP)) {
  5026. struct iphdr *ip4h = ip_hdr(skb);
  5027. /* Calculate IPv4 checksum and L4 checksum */
  5028. ip_hdr_len = ip4h->ihl;
  5029. l4_proto = ip4h->protocol;
  5030. } else if (skb->protocol == htons(ETH_P_IPV6)) {
  5031. struct ipv6hdr *ip6h = ipv6_hdr(skb);
  5032. /* Read l4_protocol from one of IPv6 extra headers */
  5033. if (skb_network_header_len(skb) > 0)
  5034. ip_hdr_len = (skb_network_header_len(skb) >> 2);
  5035. l4_proto = ip6h->nexthdr;
  5036. } else {
  5037. return MVPP2_TXD_L4_CSUM_NOT;
  5038. }
  5039. return mvpp2_txq_desc_csum(skb_network_offset(skb),
  5040. skb->protocol, ip_hdr_len, l4_proto);
  5041. }
  5042. return MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE;
  5043. }
  5044. /* Main rx processing */
  5045. static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,
  5046. int rx_todo, struct mvpp2_rx_queue *rxq)
  5047. {
  5048. struct net_device *dev = port->dev;
  5049. int rx_received;
  5050. int rx_done = 0;
  5051. u32 rcvd_pkts = 0;
  5052. u32 rcvd_bytes = 0;
  5053. /* Get number of received packets and clamp the to-do */
  5054. rx_received = mvpp2_rxq_received(port, rxq->id);
  5055. if (rx_todo > rx_received)
  5056. rx_todo = rx_received;
  5057. while (rx_done < rx_todo) {
  5058. struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
  5059. struct mvpp2_bm_pool *bm_pool;
  5060. struct sk_buff *skb;
  5061. unsigned int frag_size;
  5062. dma_addr_t dma_addr;
  5063. phys_addr_t phys_addr;
  5064. u32 rx_status;
  5065. int pool, rx_bytes, err;
  5066. void *data;
  5067. rx_done++;
  5068. rx_status = mvpp2_rxdesc_status_get(port, rx_desc);
  5069. rx_bytes = mvpp2_rxdesc_size_get(port, rx_desc);
  5070. rx_bytes -= MVPP2_MH_SIZE;
  5071. dma_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
  5072. phys_addr = mvpp2_rxdesc_cookie_get(port, rx_desc);
  5073. data = (void *)phys_to_virt(phys_addr);
  5074. pool = (rx_status & MVPP2_RXD_BM_POOL_ID_MASK) >>
  5075. MVPP2_RXD_BM_POOL_ID_OFFS;
  5076. bm_pool = &port->priv->bm_pools[pool];
  5077. /* In case of an error, release the requested buffer pointer
  5078. * to the Buffer Manager. This request process is controlled
  5079. * by the hardware, and the information about the buffer is
  5080. * comprised by the RX descriptor.
  5081. */
  5082. if (rx_status & MVPP2_RXD_ERR_SUMMARY) {
  5083. err_drop_frame:
  5084. dev->stats.rx_errors++;
  5085. mvpp2_rx_error(port, rx_desc);
  5086. /* Return the buffer to the pool */
  5087. mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
  5088. continue;
  5089. }
  5090. if (bm_pool->frag_size > PAGE_SIZE)
  5091. frag_size = 0;
  5092. else
  5093. frag_size = bm_pool->frag_size;
  5094. skb = build_skb(data, frag_size);
  5095. if (!skb) {
  5096. netdev_warn(port->dev, "skb build failed\n");
  5097. goto err_drop_frame;
  5098. }
  5099. err = mvpp2_rx_refill(port, bm_pool, pool);
  5100. if (err) {
  5101. netdev_err(port->dev, "failed to refill BM pools\n");
  5102. goto err_drop_frame;
  5103. }
  5104. dma_unmap_single(dev->dev.parent, dma_addr,
  5105. bm_pool->buf_size, DMA_FROM_DEVICE);
  5106. rcvd_pkts++;
  5107. rcvd_bytes += rx_bytes;
  5108. skb_reserve(skb, MVPP2_MH_SIZE + NET_SKB_PAD);
  5109. skb_put(skb, rx_bytes);
  5110. skb->protocol = eth_type_trans(skb, dev);
  5111. mvpp2_rx_csum(port, rx_status, skb);
  5112. napi_gro_receive(napi, skb);
  5113. }
  5114. if (rcvd_pkts) {
  5115. struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
  5116. u64_stats_update_begin(&stats->syncp);
  5117. stats->rx_packets += rcvd_pkts;
  5118. stats->rx_bytes += rcvd_bytes;
  5119. u64_stats_update_end(&stats->syncp);
  5120. }
  5121. /* Update Rx queue management counters */
  5122. wmb();
  5123. mvpp2_rxq_status_update(port, rxq->id, rx_done, rx_done);
  5124. return rx_todo;
  5125. }
  5126. static inline void
  5127. tx_desc_unmap_put(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
  5128. struct mvpp2_tx_desc *desc)
  5129. {
  5130. dma_addr_t buf_dma_addr =
  5131. mvpp2_txdesc_dma_addr_get(port, desc);
  5132. size_t buf_sz =
  5133. mvpp2_txdesc_size_get(port, desc);
  5134. dma_unmap_single(port->dev->dev.parent, buf_dma_addr,
  5135. buf_sz, DMA_TO_DEVICE);
  5136. mvpp2_txq_desc_put(txq);
  5137. }
  5138. /* Handle tx fragmentation processing */
  5139. static int mvpp2_tx_frag_process(struct mvpp2_port *port, struct sk_buff *skb,
  5140. struct mvpp2_tx_queue *aggr_txq,
  5141. struct mvpp2_tx_queue *txq)
  5142. {
  5143. struct mvpp2_txq_pcpu *txq_pcpu = this_cpu_ptr(txq->pcpu);
  5144. struct mvpp2_tx_desc *tx_desc;
  5145. int i;
  5146. dma_addr_t buf_dma_addr;
  5147. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  5148. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  5149. void *addr = page_address(frag->page.p) + frag->page_offset;
  5150. tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
  5151. mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
  5152. mvpp2_txdesc_size_set(port, tx_desc, frag->size);
  5153. buf_dma_addr = dma_map_single(port->dev->dev.parent, addr,
  5154. frag->size,
  5155. DMA_TO_DEVICE);
  5156. if (dma_mapping_error(port->dev->dev.parent, buf_dma_addr)) {
  5157. mvpp2_txq_desc_put(txq);
  5158. goto cleanup;
  5159. }
  5160. mvpp2_txdesc_offset_set(port, tx_desc,
  5161. buf_dma_addr & MVPP2_TX_DESC_ALIGN);
  5162. mvpp2_txdesc_dma_addr_set(port, tx_desc,
  5163. buf_dma_addr & ~MVPP2_TX_DESC_ALIGN);
  5164. if (i == (skb_shinfo(skb)->nr_frags - 1)) {
  5165. /* Last descriptor */
  5166. mvpp2_txdesc_cmd_set(port, tx_desc,
  5167. MVPP2_TXD_L_DESC);
  5168. mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
  5169. } else {
  5170. /* Descriptor in the middle: Not First, Not Last */
  5171. mvpp2_txdesc_cmd_set(port, tx_desc, 0);
  5172. mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
  5173. }
  5174. }
  5175. return 0;
  5176. cleanup:
  5177. /* Release all descriptors that were used to map fragments of
  5178. * this packet, as well as the corresponding DMA mappings
  5179. */
  5180. for (i = i - 1; i >= 0; i--) {
  5181. tx_desc = txq->descs + i;
  5182. tx_desc_unmap_put(port, txq, tx_desc);
  5183. }
  5184. return -ENOMEM;
  5185. }
  5186. static inline void mvpp2_tso_put_hdr(struct sk_buff *skb,
  5187. struct net_device *dev,
  5188. struct mvpp2_tx_queue *txq,
  5189. struct mvpp2_tx_queue *aggr_txq,
  5190. struct mvpp2_txq_pcpu *txq_pcpu,
  5191. int hdr_sz)
  5192. {
  5193. struct mvpp2_port *port = netdev_priv(dev);
  5194. struct mvpp2_tx_desc *tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
  5195. dma_addr_t addr;
  5196. mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
  5197. mvpp2_txdesc_size_set(port, tx_desc, hdr_sz);
  5198. addr = txq_pcpu->tso_headers_dma +
  5199. txq_pcpu->txq_put_index * TSO_HEADER_SIZE;
  5200. mvpp2_txdesc_offset_set(port, tx_desc, addr & MVPP2_TX_DESC_ALIGN);
  5201. mvpp2_txdesc_dma_addr_set(port, tx_desc, addr & ~MVPP2_TX_DESC_ALIGN);
  5202. mvpp2_txdesc_cmd_set(port, tx_desc, mvpp2_skb_tx_csum(port, skb) |
  5203. MVPP2_TXD_F_DESC |
  5204. MVPP2_TXD_PADDING_DISABLE);
  5205. mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
  5206. }
  5207. static inline int mvpp2_tso_put_data(struct sk_buff *skb,
  5208. struct net_device *dev, struct tso_t *tso,
  5209. struct mvpp2_tx_queue *txq,
  5210. struct mvpp2_tx_queue *aggr_txq,
  5211. struct mvpp2_txq_pcpu *txq_pcpu,
  5212. int sz, bool left, bool last)
  5213. {
  5214. struct mvpp2_port *port = netdev_priv(dev);
  5215. struct mvpp2_tx_desc *tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
  5216. dma_addr_t buf_dma_addr;
  5217. mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
  5218. mvpp2_txdesc_size_set(port, tx_desc, sz);
  5219. buf_dma_addr = dma_map_single(dev->dev.parent, tso->data, sz,
  5220. DMA_TO_DEVICE);
  5221. if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
  5222. mvpp2_txq_desc_put(txq);
  5223. return -ENOMEM;
  5224. }
  5225. mvpp2_txdesc_offset_set(port, tx_desc,
  5226. buf_dma_addr & MVPP2_TX_DESC_ALIGN);
  5227. mvpp2_txdesc_dma_addr_set(port, tx_desc,
  5228. buf_dma_addr & ~MVPP2_TX_DESC_ALIGN);
  5229. if (!left) {
  5230. mvpp2_txdesc_cmd_set(port, tx_desc, MVPP2_TXD_L_DESC);
  5231. if (last) {
  5232. mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
  5233. return 0;
  5234. }
  5235. } else {
  5236. mvpp2_txdesc_cmd_set(port, tx_desc, 0);
  5237. }
  5238. mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
  5239. return 0;
  5240. }
  5241. static int mvpp2_tx_tso(struct sk_buff *skb, struct net_device *dev,
  5242. struct mvpp2_tx_queue *txq,
  5243. struct mvpp2_tx_queue *aggr_txq,
  5244. struct mvpp2_txq_pcpu *txq_pcpu)
  5245. {
  5246. struct mvpp2_port *port = netdev_priv(dev);
  5247. struct tso_t tso;
  5248. int hdr_sz = skb_transport_offset(skb) + tcp_hdrlen(skb);
  5249. int i, len, descs = 0;
  5250. /* Check number of available descriptors */
  5251. if (mvpp2_aggr_desc_num_check(port->priv, aggr_txq,
  5252. tso_count_descs(skb)) ||
  5253. mvpp2_txq_reserved_desc_num_proc(port->priv, txq, txq_pcpu,
  5254. tso_count_descs(skb)))
  5255. return 0;
  5256. tso_start(skb, &tso);
  5257. len = skb->len - hdr_sz;
  5258. while (len > 0) {
  5259. int left = min_t(int, skb_shinfo(skb)->gso_size, len);
  5260. char *hdr = txq_pcpu->tso_headers +
  5261. txq_pcpu->txq_put_index * TSO_HEADER_SIZE;
  5262. len -= left;
  5263. descs++;
  5264. tso_build_hdr(skb, hdr, &tso, left, len == 0);
  5265. mvpp2_tso_put_hdr(skb, dev, txq, aggr_txq, txq_pcpu, hdr_sz);
  5266. while (left > 0) {
  5267. int sz = min_t(int, tso.size, left);
  5268. left -= sz;
  5269. descs++;
  5270. if (mvpp2_tso_put_data(skb, dev, &tso, txq, aggr_txq,
  5271. txq_pcpu, sz, left, len == 0))
  5272. goto release;
  5273. tso_build_data(skb, &tso, sz);
  5274. }
  5275. }
  5276. return descs;
  5277. release:
  5278. for (i = descs - 1; i >= 0; i--) {
  5279. struct mvpp2_tx_desc *tx_desc = txq->descs + i;
  5280. tx_desc_unmap_put(port, txq, tx_desc);
  5281. }
  5282. return 0;
  5283. }
  5284. /* Main tx processing */
  5285. static int mvpp2_tx(struct sk_buff *skb, struct net_device *dev)
  5286. {
  5287. struct mvpp2_port *port = netdev_priv(dev);
  5288. struct mvpp2_tx_queue *txq, *aggr_txq;
  5289. struct mvpp2_txq_pcpu *txq_pcpu;
  5290. struct mvpp2_tx_desc *tx_desc;
  5291. dma_addr_t buf_dma_addr;
  5292. int frags = 0;
  5293. u16 txq_id;
  5294. u32 tx_cmd;
  5295. txq_id = skb_get_queue_mapping(skb);
  5296. txq = port->txqs[txq_id];
  5297. txq_pcpu = this_cpu_ptr(txq->pcpu);
  5298. aggr_txq = &port->priv->aggr_txqs[smp_processor_id()];
  5299. if (skb_is_gso(skb)) {
  5300. frags = mvpp2_tx_tso(skb, dev, txq, aggr_txq, txq_pcpu);
  5301. goto out;
  5302. }
  5303. frags = skb_shinfo(skb)->nr_frags + 1;
  5304. /* Check number of available descriptors */
  5305. if (mvpp2_aggr_desc_num_check(port->priv, aggr_txq, frags) ||
  5306. mvpp2_txq_reserved_desc_num_proc(port->priv, txq,
  5307. txq_pcpu, frags)) {
  5308. frags = 0;
  5309. goto out;
  5310. }
  5311. /* Get a descriptor for the first part of the packet */
  5312. tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
  5313. mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
  5314. mvpp2_txdesc_size_set(port, tx_desc, skb_headlen(skb));
  5315. buf_dma_addr = dma_map_single(dev->dev.parent, skb->data,
  5316. skb_headlen(skb), DMA_TO_DEVICE);
  5317. if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
  5318. mvpp2_txq_desc_put(txq);
  5319. frags = 0;
  5320. goto out;
  5321. }
  5322. mvpp2_txdesc_offset_set(port, tx_desc,
  5323. buf_dma_addr & MVPP2_TX_DESC_ALIGN);
  5324. mvpp2_txdesc_dma_addr_set(port, tx_desc,
  5325. buf_dma_addr & ~MVPP2_TX_DESC_ALIGN);
  5326. tx_cmd = mvpp2_skb_tx_csum(port, skb);
  5327. if (frags == 1) {
  5328. /* First and Last descriptor */
  5329. tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC;
  5330. mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
  5331. mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
  5332. } else {
  5333. /* First but not Last */
  5334. tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_PADDING_DISABLE;
  5335. mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
  5336. mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
  5337. /* Continue with other skb fragments */
  5338. if (mvpp2_tx_frag_process(port, skb, aggr_txq, txq)) {
  5339. tx_desc_unmap_put(port, txq, tx_desc);
  5340. frags = 0;
  5341. goto out;
  5342. }
  5343. }
  5344. out:
  5345. if (frags > 0) {
  5346. struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
  5347. struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
  5348. txq_pcpu->reserved_num -= frags;
  5349. txq_pcpu->count += frags;
  5350. aggr_txq->count += frags;
  5351. /* Enable transmit */
  5352. wmb();
  5353. mvpp2_aggr_txq_pend_desc_add(port, frags);
  5354. if (txq_pcpu->size - txq_pcpu->count < MAX_SKB_FRAGS + 1)
  5355. netif_tx_stop_queue(nq);
  5356. u64_stats_update_begin(&stats->syncp);
  5357. stats->tx_packets++;
  5358. stats->tx_bytes += skb->len;
  5359. u64_stats_update_end(&stats->syncp);
  5360. } else {
  5361. dev->stats.tx_dropped++;
  5362. dev_kfree_skb_any(skb);
  5363. }
  5364. /* Finalize TX processing */
  5365. if (txq_pcpu->count >= txq->done_pkts_coal)
  5366. mvpp2_txq_done(port, txq, txq_pcpu);
  5367. /* Set the timer in case not all frags were processed */
  5368. if (!port->has_tx_irqs && txq_pcpu->count <= frags &&
  5369. txq_pcpu->count > 0) {
  5370. struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
  5371. mvpp2_timer_set(port_pcpu);
  5372. }
  5373. return NETDEV_TX_OK;
  5374. }
  5375. static inline void mvpp2_cause_error(struct net_device *dev, int cause)
  5376. {
  5377. if (cause & MVPP2_CAUSE_FCS_ERR_MASK)
  5378. netdev_err(dev, "FCS error\n");
  5379. if (cause & MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK)
  5380. netdev_err(dev, "rx fifo overrun error\n");
  5381. if (cause & MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK)
  5382. netdev_err(dev, "tx fifo underrun error\n");
  5383. }
  5384. static int mvpp2_poll(struct napi_struct *napi, int budget)
  5385. {
  5386. u32 cause_rx_tx, cause_rx, cause_tx, cause_misc;
  5387. int rx_done = 0;
  5388. struct mvpp2_port *port = netdev_priv(napi->dev);
  5389. struct mvpp2_queue_vector *qv;
  5390. int cpu = smp_processor_id();
  5391. qv = container_of(napi, struct mvpp2_queue_vector, napi);
  5392. /* Rx/Tx cause register
  5393. *
  5394. * Bits 0-15: each bit indicates received packets on the Rx queue
  5395. * (bit 0 is for Rx queue 0).
  5396. *
  5397. * Bits 16-23: each bit indicates transmitted packets on the Tx queue
  5398. * (bit 16 is for Tx queue 0).
  5399. *
  5400. * Each CPU has its own Rx/Tx cause register
  5401. */
  5402. cause_rx_tx = mvpp2_percpu_read(port->priv, qv->sw_thread_id,
  5403. MVPP2_ISR_RX_TX_CAUSE_REG(port->id));
  5404. cause_misc = cause_rx_tx & MVPP2_CAUSE_MISC_SUM_MASK;
  5405. if (cause_misc) {
  5406. mvpp2_cause_error(port->dev, cause_misc);
  5407. /* Clear the cause register */
  5408. mvpp2_write(port->priv, MVPP2_ISR_MISC_CAUSE_REG, 0);
  5409. mvpp2_percpu_write(port->priv, cpu,
  5410. MVPP2_ISR_RX_TX_CAUSE_REG(port->id),
  5411. cause_rx_tx & ~MVPP2_CAUSE_MISC_SUM_MASK);
  5412. }
  5413. cause_tx = cause_rx_tx & MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
  5414. if (cause_tx) {
  5415. cause_tx >>= MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_OFFSET;
  5416. mvpp2_tx_done(port, cause_tx, qv->sw_thread_id);
  5417. }
  5418. /* Process RX packets */
  5419. cause_rx = cause_rx_tx & MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
  5420. cause_rx <<= qv->first_rxq;
  5421. cause_rx |= qv->pending_cause_rx;
  5422. while (cause_rx && budget > 0) {
  5423. int count;
  5424. struct mvpp2_rx_queue *rxq;
  5425. rxq = mvpp2_get_rx_queue(port, cause_rx);
  5426. if (!rxq)
  5427. break;
  5428. count = mvpp2_rx(port, napi, budget, rxq);
  5429. rx_done += count;
  5430. budget -= count;
  5431. if (budget > 0) {
  5432. /* Clear the bit associated to this Rx queue
  5433. * so that next iteration will continue from
  5434. * the next Rx queue.
  5435. */
  5436. cause_rx &= ~(1 << rxq->logic_rxq);
  5437. }
  5438. }
  5439. if (budget > 0) {
  5440. cause_rx = 0;
  5441. napi_complete_done(napi, rx_done);
  5442. mvpp2_qvec_interrupt_enable(qv);
  5443. }
  5444. qv->pending_cause_rx = cause_rx;
  5445. return rx_done;
  5446. }
  5447. /* Set hw internals when starting port */
  5448. static void mvpp2_start_dev(struct mvpp2_port *port)
  5449. {
  5450. struct net_device *ndev = port->dev;
  5451. int i;
  5452. if (port->gop_id == 0 &&
  5453. (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
  5454. port->phy_interface == PHY_INTERFACE_MODE_10GKR))
  5455. mvpp2_xlg_max_rx_size_set(port);
  5456. else
  5457. mvpp2_gmac_max_rx_size_set(port);
  5458. mvpp2_txp_max_tx_size_set(port);
  5459. for (i = 0; i < port->nqvecs; i++)
  5460. napi_enable(&port->qvecs[i].napi);
  5461. /* Enable interrupts on all CPUs */
  5462. mvpp2_interrupts_enable(port);
  5463. if (port->priv->hw_version == MVPP22) {
  5464. mvpp22_comphy_init(port);
  5465. mvpp22_gop_init(port);
  5466. }
  5467. mvpp2_port_mii_set(port);
  5468. mvpp2_port_enable(port);
  5469. if (ndev->phydev)
  5470. phy_start(ndev->phydev);
  5471. netif_tx_start_all_queues(port->dev);
  5472. }
  5473. /* Set hw internals when stopping port */
  5474. static void mvpp2_stop_dev(struct mvpp2_port *port)
  5475. {
  5476. struct net_device *ndev = port->dev;
  5477. int i;
  5478. /* Stop new packets from arriving to RXQs */
  5479. mvpp2_ingress_disable(port);
  5480. mdelay(10);
  5481. /* Disable interrupts on all CPUs */
  5482. mvpp2_interrupts_disable(port);
  5483. for (i = 0; i < port->nqvecs; i++)
  5484. napi_disable(&port->qvecs[i].napi);
  5485. netif_carrier_off(port->dev);
  5486. netif_tx_stop_all_queues(port->dev);
  5487. mvpp2_egress_disable(port);
  5488. mvpp2_port_disable(port);
  5489. if (ndev->phydev)
  5490. phy_stop(ndev->phydev);
  5491. phy_power_off(port->comphy);
  5492. }
  5493. static int mvpp2_check_ringparam_valid(struct net_device *dev,
  5494. struct ethtool_ringparam *ring)
  5495. {
  5496. u16 new_rx_pending = ring->rx_pending;
  5497. u16 new_tx_pending = ring->tx_pending;
  5498. if (ring->rx_pending == 0 || ring->tx_pending == 0)
  5499. return -EINVAL;
  5500. if (ring->rx_pending > MVPP2_MAX_RXD)
  5501. new_rx_pending = MVPP2_MAX_RXD;
  5502. else if (!IS_ALIGNED(ring->rx_pending, 16))
  5503. new_rx_pending = ALIGN(ring->rx_pending, 16);
  5504. if (ring->tx_pending > MVPP2_MAX_TXD)
  5505. new_tx_pending = MVPP2_MAX_TXD;
  5506. else if (!IS_ALIGNED(ring->tx_pending, 32))
  5507. new_tx_pending = ALIGN(ring->tx_pending, 32);
  5508. if (ring->rx_pending != new_rx_pending) {
  5509. netdev_info(dev, "illegal Rx ring size value %d, round to %d\n",
  5510. ring->rx_pending, new_rx_pending);
  5511. ring->rx_pending = new_rx_pending;
  5512. }
  5513. if (ring->tx_pending != new_tx_pending) {
  5514. netdev_info(dev, "illegal Tx ring size value %d, round to %d\n",
  5515. ring->tx_pending, new_tx_pending);
  5516. ring->tx_pending = new_tx_pending;
  5517. }
  5518. return 0;
  5519. }
  5520. static void mvpp21_get_mac_address(struct mvpp2_port *port, unsigned char *addr)
  5521. {
  5522. u32 mac_addr_l, mac_addr_m, mac_addr_h;
  5523. mac_addr_l = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
  5524. mac_addr_m = readl(port->priv->lms_base + MVPP2_SRC_ADDR_MIDDLE);
  5525. mac_addr_h = readl(port->priv->lms_base + MVPP2_SRC_ADDR_HIGH);
  5526. addr[0] = (mac_addr_h >> 24) & 0xFF;
  5527. addr[1] = (mac_addr_h >> 16) & 0xFF;
  5528. addr[2] = (mac_addr_h >> 8) & 0xFF;
  5529. addr[3] = mac_addr_h & 0xFF;
  5530. addr[4] = mac_addr_m & 0xFF;
  5531. addr[5] = (mac_addr_l >> MVPP2_GMAC_SA_LOW_OFFS) & 0xFF;
  5532. }
  5533. static int mvpp2_phy_connect(struct mvpp2_port *port)
  5534. {
  5535. struct phy_device *phy_dev;
  5536. /* No PHY is attached */
  5537. if (!port->phy_node)
  5538. return 0;
  5539. phy_dev = of_phy_connect(port->dev, port->phy_node, mvpp2_link_event, 0,
  5540. port->phy_interface);
  5541. if (!phy_dev) {
  5542. netdev_err(port->dev, "cannot connect to phy\n");
  5543. return -ENODEV;
  5544. }
  5545. phy_dev->supported &= PHY_GBIT_FEATURES;
  5546. phy_dev->advertising = phy_dev->supported;
  5547. port->link = 0;
  5548. port->duplex = 0;
  5549. port->speed = 0;
  5550. return 0;
  5551. }
  5552. static void mvpp2_phy_disconnect(struct mvpp2_port *port)
  5553. {
  5554. struct net_device *ndev = port->dev;
  5555. if (!ndev->phydev)
  5556. return;
  5557. phy_disconnect(ndev->phydev);
  5558. }
  5559. static int mvpp2_irqs_init(struct mvpp2_port *port)
  5560. {
  5561. int err, i;
  5562. for (i = 0; i < port->nqvecs; i++) {
  5563. struct mvpp2_queue_vector *qv = port->qvecs + i;
  5564. err = request_irq(qv->irq, mvpp2_isr, 0, port->dev->name, qv);
  5565. if (err)
  5566. goto err;
  5567. if (qv->type == MVPP2_QUEUE_VECTOR_PRIVATE)
  5568. irq_set_affinity_hint(qv->irq,
  5569. cpumask_of(qv->sw_thread_id));
  5570. }
  5571. return 0;
  5572. err:
  5573. for (i = 0; i < port->nqvecs; i++) {
  5574. struct mvpp2_queue_vector *qv = port->qvecs + i;
  5575. irq_set_affinity_hint(qv->irq, NULL);
  5576. free_irq(qv->irq, qv);
  5577. }
  5578. return err;
  5579. }
  5580. static void mvpp2_irqs_deinit(struct mvpp2_port *port)
  5581. {
  5582. int i;
  5583. for (i = 0; i < port->nqvecs; i++) {
  5584. struct mvpp2_queue_vector *qv = port->qvecs + i;
  5585. irq_set_affinity_hint(qv->irq, NULL);
  5586. free_irq(qv->irq, qv);
  5587. }
  5588. }
  5589. static int mvpp2_open(struct net_device *dev)
  5590. {
  5591. struct mvpp2_port *port = netdev_priv(dev);
  5592. struct mvpp2 *priv = port->priv;
  5593. unsigned char mac_bcast[ETH_ALEN] = {
  5594. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  5595. int err;
  5596. err = mvpp2_prs_mac_da_accept(port->priv, port->id, mac_bcast, true);
  5597. if (err) {
  5598. netdev_err(dev, "mvpp2_prs_mac_da_accept BC failed\n");
  5599. return err;
  5600. }
  5601. err = mvpp2_prs_mac_da_accept(port->priv, port->id,
  5602. dev->dev_addr, true);
  5603. if (err) {
  5604. netdev_err(dev, "mvpp2_prs_mac_da_accept MC failed\n");
  5605. return err;
  5606. }
  5607. err = mvpp2_prs_tag_mode_set(port->priv, port->id, MVPP2_TAG_TYPE_MH);
  5608. if (err) {
  5609. netdev_err(dev, "mvpp2_prs_tag_mode_set failed\n");
  5610. return err;
  5611. }
  5612. err = mvpp2_prs_def_flow(port);
  5613. if (err) {
  5614. netdev_err(dev, "mvpp2_prs_def_flow failed\n");
  5615. return err;
  5616. }
  5617. /* Allocate the Rx/Tx queues */
  5618. err = mvpp2_setup_rxqs(port);
  5619. if (err) {
  5620. netdev_err(port->dev, "cannot allocate Rx queues\n");
  5621. return err;
  5622. }
  5623. err = mvpp2_setup_txqs(port);
  5624. if (err) {
  5625. netdev_err(port->dev, "cannot allocate Tx queues\n");
  5626. goto err_cleanup_rxqs;
  5627. }
  5628. err = mvpp2_irqs_init(port);
  5629. if (err) {
  5630. netdev_err(port->dev, "cannot init IRQs\n");
  5631. goto err_cleanup_txqs;
  5632. }
  5633. if (priv->hw_version == MVPP22 && !port->phy_node && port->link_irq) {
  5634. err = request_irq(port->link_irq, mvpp2_link_status_isr, 0,
  5635. dev->name, port);
  5636. if (err) {
  5637. netdev_err(port->dev, "cannot request link IRQ %d\n",
  5638. port->link_irq);
  5639. goto err_free_irq;
  5640. }
  5641. mvpp22_gop_setup_irq(port);
  5642. }
  5643. /* In default link is down */
  5644. netif_carrier_off(port->dev);
  5645. err = mvpp2_phy_connect(port);
  5646. if (err < 0)
  5647. goto err_free_link_irq;
  5648. /* Unmask interrupts on all CPUs */
  5649. on_each_cpu(mvpp2_interrupts_unmask, port, 1);
  5650. mvpp2_shared_interrupt_mask_unmask(port, false);
  5651. mvpp2_start_dev(port);
  5652. return 0;
  5653. err_free_link_irq:
  5654. if (priv->hw_version == MVPP22 && !port->phy_node && port->link_irq)
  5655. free_irq(port->link_irq, port);
  5656. err_free_irq:
  5657. mvpp2_irqs_deinit(port);
  5658. err_cleanup_txqs:
  5659. mvpp2_cleanup_txqs(port);
  5660. err_cleanup_rxqs:
  5661. mvpp2_cleanup_rxqs(port);
  5662. return err;
  5663. }
  5664. static int mvpp2_stop(struct net_device *dev)
  5665. {
  5666. struct mvpp2_port *port = netdev_priv(dev);
  5667. struct mvpp2_port_pcpu *port_pcpu;
  5668. struct mvpp2 *priv = port->priv;
  5669. int cpu;
  5670. mvpp2_stop_dev(port);
  5671. mvpp2_phy_disconnect(port);
  5672. /* Mask interrupts on all CPUs */
  5673. on_each_cpu(mvpp2_interrupts_mask, port, 1);
  5674. mvpp2_shared_interrupt_mask_unmask(port, true);
  5675. if (priv->hw_version == MVPP22 && !port->phy_node && port->link_irq)
  5676. free_irq(port->link_irq, port);
  5677. mvpp2_irqs_deinit(port);
  5678. if (!port->has_tx_irqs) {
  5679. for_each_present_cpu(cpu) {
  5680. port_pcpu = per_cpu_ptr(port->pcpu, cpu);
  5681. hrtimer_cancel(&port_pcpu->tx_done_timer);
  5682. port_pcpu->timer_scheduled = false;
  5683. tasklet_kill(&port_pcpu->tx_done_tasklet);
  5684. }
  5685. }
  5686. mvpp2_cleanup_rxqs(port);
  5687. mvpp2_cleanup_txqs(port);
  5688. return 0;
  5689. }
  5690. static void mvpp2_set_rx_mode(struct net_device *dev)
  5691. {
  5692. struct mvpp2_port *port = netdev_priv(dev);
  5693. struct mvpp2 *priv = port->priv;
  5694. struct netdev_hw_addr *ha;
  5695. int id = port->id;
  5696. bool allmulti = dev->flags & IFF_ALLMULTI;
  5697. mvpp2_prs_mac_promisc_set(priv, id, dev->flags & IFF_PROMISC);
  5698. mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_ALL, allmulti);
  5699. mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_IP6, allmulti);
  5700. /* Remove all port->id's mcast enries */
  5701. mvpp2_prs_mcast_del_all(priv, id);
  5702. if (allmulti && !netdev_mc_empty(dev)) {
  5703. netdev_for_each_mc_addr(ha, dev)
  5704. mvpp2_prs_mac_da_accept(priv, id, ha->addr, true);
  5705. }
  5706. }
  5707. static int mvpp2_set_mac_address(struct net_device *dev, void *p)
  5708. {
  5709. struct mvpp2_port *port = netdev_priv(dev);
  5710. const struct sockaddr *addr = p;
  5711. int err;
  5712. if (!is_valid_ether_addr(addr->sa_data)) {
  5713. err = -EADDRNOTAVAIL;
  5714. goto log_error;
  5715. }
  5716. if (!netif_running(dev)) {
  5717. err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
  5718. if (!err)
  5719. return 0;
  5720. /* Reconfigure parser to accept the original MAC address */
  5721. err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
  5722. if (err)
  5723. goto log_error;
  5724. }
  5725. mvpp2_stop_dev(port);
  5726. err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
  5727. if (!err)
  5728. goto out_start;
  5729. /* Reconfigure parser accept the original MAC address */
  5730. err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
  5731. if (err)
  5732. goto log_error;
  5733. out_start:
  5734. mvpp2_start_dev(port);
  5735. mvpp2_egress_enable(port);
  5736. mvpp2_ingress_enable(port);
  5737. return 0;
  5738. log_error:
  5739. netdev_err(dev, "failed to change MAC address\n");
  5740. return err;
  5741. }
  5742. static int mvpp2_change_mtu(struct net_device *dev, int mtu)
  5743. {
  5744. struct mvpp2_port *port = netdev_priv(dev);
  5745. int err;
  5746. if (!IS_ALIGNED(MVPP2_RX_PKT_SIZE(mtu), 8)) {
  5747. netdev_info(dev, "illegal MTU value %d, round to %d\n", mtu,
  5748. ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8));
  5749. mtu = ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8);
  5750. }
  5751. if (!netif_running(dev)) {
  5752. err = mvpp2_bm_update_mtu(dev, mtu);
  5753. if (!err) {
  5754. port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
  5755. return 0;
  5756. }
  5757. /* Reconfigure BM to the original MTU */
  5758. err = mvpp2_bm_update_mtu(dev, dev->mtu);
  5759. if (err)
  5760. goto log_error;
  5761. }
  5762. mvpp2_stop_dev(port);
  5763. err = mvpp2_bm_update_mtu(dev, mtu);
  5764. if (!err) {
  5765. port->pkt_size = MVPP2_RX_PKT_SIZE(mtu);
  5766. goto out_start;
  5767. }
  5768. /* Reconfigure BM to the original MTU */
  5769. err = mvpp2_bm_update_mtu(dev, dev->mtu);
  5770. if (err)
  5771. goto log_error;
  5772. out_start:
  5773. mvpp2_start_dev(port);
  5774. mvpp2_egress_enable(port);
  5775. mvpp2_ingress_enable(port);
  5776. return 0;
  5777. log_error:
  5778. netdev_err(dev, "failed to change MTU\n");
  5779. return err;
  5780. }
  5781. static void
  5782. mvpp2_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  5783. {
  5784. struct mvpp2_port *port = netdev_priv(dev);
  5785. unsigned int start;
  5786. int cpu;
  5787. for_each_possible_cpu(cpu) {
  5788. struct mvpp2_pcpu_stats *cpu_stats;
  5789. u64 rx_packets;
  5790. u64 rx_bytes;
  5791. u64 tx_packets;
  5792. u64 tx_bytes;
  5793. cpu_stats = per_cpu_ptr(port->stats, cpu);
  5794. do {
  5795. start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
  5796. rx_packets = cpu_stats->rx_packets;
  5797. rx_bytes = cpu_stats->rx_bytes;
  5798. tx_packets = cpu_stats->tx_packets;
  5799. tx_bytes = cpu_stats->tx_bytes;
  5800. } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
  5801. stats->rx_packets += rx_packets;
  5802. stats->rx_bytes += rx_bytes;
  5803. stats->tx_packets += tx_packets;
  5804. stats->tx_bytes += tx_bytes;
  5805. }
  5806. stats->rx_errors = dev->stats.rx_errors;
  5807. stats->rx_dropped = dev->stats.rx_dropped;
  5808. stats->tx_dropped = dev->stats.tx_dropped;
  5809. }
  5810. static int mvpp2_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  5811. {
  5812. int ret;
  5813. if (!dev->phydev)
  5814. return -ENOTSUPP;
  5815. ret = phy_mii_ioctl(dev->phydev, ifr, cmd);
  5816. if (!ret)
  5817. mvpp2_link_event(dev);
  5818. return ret;
  5819. }
  5820. /* Ethtool methods */
  5821. /* Set interrupt coalescing for ethtools */
  5822. static int mvpp2_ethtool_set_coalesce(struct net_device *dev,
  5823. struct ethtool_coalesce *c)
  5824. {
  5825. struct mvpp2_port *port = netdev_priv(dev);
  5826. int queue;
  5827. for (queue = 0; queue < port->nrxqs; queue++) {
  5828. struct mvpp2_rx_queue *rxq = port->rxqs[queue];
  5829. rxq->time_coal = c->rx_coalesce_usecs;
  5830. rxq->pkts_coal = c->rx_max_coalesced_frames;
  5831. mvpp2_rx_pkts_coal_set(port, rxq);
  5832. mvpp2_rx_time_coal_set(port, rxq);
  5833. }
  5834. if (port->has_tx_irqs) {
  5835. port->tx_time_coal = c->tx_coalesce_usecs;
  5836. mvpp2_tx_time_coal_set(port);
  5837. }
  5838. for (queue = 0; queue < port->ntxqs; queue++) {
  5839. struct mvpp2_tx_queue *txq = port->txqs[queue];
  5840. txq->done_pkts_coal = c->tx_max_coalesced_frames;
  5841. if (port->has_tx_irqs)
  5842. mvpp2_tx_pkts_coal_set(port, txq);
  5843. }
  5844. return 0;
  5845. }
  5846. /* get coalescing for ethtools */
  5847. static int mvpp2_ethtool_get_coalesce(struct net_device *dev,
  5848. struct ethtool_coalesce *c)
  5849. {
  5850. struct mvpp2_port *port = netdev_priv(dev);
  5851. c->rx_coalesce_usecs = port->rxqs[0]->time_coal;
  5852. c->rx_max_coalesced_frames = port->rxqs[0]->pkts_coal;
  5853. c->tx_max_coalesced_frames = port->txqs[0]->done_pkts_coal;
  5854. return 0;
  5855. }
  5856. static void mvpp2_ethtool_get_drvinfo(struct net_device *dev,
  5857. struct ethtool_drvinfo *drvinfo)
  5858. {
  5859. strlcpy(drvinfo->driver, MVPP2_DRIVER_NAME,
  5860. sizeof(drvinfo->driver));
  5861. strlcpy(drvinfo->version, MVPP2_DRIVER_VERSION,
  5862. sizeof(drvinfo->version));
  5863. strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
  5864. sizeof(drvinfo->bus_info));
  5865. }
  5866. static void mvpp2_ethtool_get_ringparam(struct net_device *dev,
  5867. struct ethtool_ringparam *ring)
  5868. {
  5869. struct mvpp2_port *port = netdev_priv(dev);
  5870. ring->rx_max_pending = MVPP2_MAX_RXD;
  5871. ring->tx_max_pending = MVPP2_MAX_TXD;
  5872. ring->rx_pending = port->rx_ring_size;
  5873. ring->tx_pending = port->tx_ring_size;
  5874. }
  5875. static int mvpp2_ethtool_set_ringparam(struct net_device *dev,
  5876. struct ethtool_ringparam *ring)
  5877. {
  5878. struct mvpp2_port *port = netdev_priv(dev);
  5879. u16 prev_rx_ring_size = port->rx_ring_size;
  5880. u16 prev_tx_ring_size = port->tx_ring_size;
  5881. int err;
  5882. err = mvpp2_check_ringparam_valid(dev, ring);
  5883. if (err)
  5884. return err;
  5885. if (!netif_running(dev)) {
  5886. port->rx_ring_size = ring->rx_pending;
  5887. port->tx_ring_size = ring->tx_pending;
  5888. return 0;
  5889. }
  5890. /* The interface is running, so we have to force a
  5891. * reallocation of the queues
  5892. */
  5893. mvpp2_stop_dev(port);
  5894. mvpp2_cleanup_rxqs(port);
  5895. mvpp2_cleanup_txqs(port);
  5896. port->rx_ring_size = ring->rx_pending;
  5897. port->tx_ring_size = ring->tx_pending;
  5898. err = mvpp2_setup_rxqs(port);
  5899. if (err) {
  5900. /* Reallocate Rx queues with the original ring size */
  5901. port->rx_ring_size = prev_rx_ring_size;
  5902. ring->rx_pending = prev_rx_ring_size;
  5903. err = mvpp2_setup_rxqs(port);
  5904. if (err)
  5905. goto err_out;
  5906. }
  5907. err = mvpp2_setup_txqs(port);
  5908. if (err) {
  5909. /* Reallocate Tx queues with the original ring size */
  5910. port->tx_ring_size = prev_tx_ring_size;
  5911. ring->tx_pending = prev_tx_ring_size;
  5912. err = mvpp2_setup_txqs(port);
  5913. if (err)
  5914. goto err_clean_rxqs;
  5915. }
  5916. mvpp2_start_dev(port);
  5917. mvpp2_egress_enable(port);
  5918. mvpp2_ingress_enable(port);
  5919. return 0;
  5920. err_clean_rxqs:
  5921. mvpp2_cleanup_rxqs(port);
  5922. err_out:
  5923. netdev_err(dev, "failed to change ring parameters");
  5924. return err;
  5925. }
  5926. /* Device ops */
  5927. static const struct net_device_ops mvpp2_netdev_ops = {
  5928. .ndo_open = mvpp2_open,
  5929. .ndo_stop = mvpp2_stop,
  5930. .ndo_start_xmit = mvpp2_tx,
  5931. .ndo_set_rx_mode = mvpp2_set_rx_mode,
  5932. .ndo_set_mac_address = mvpp2_set_mac_address,
  5933. .ndo_change_mtu = mvpp2_change_mtu,
  5934. .ndo_get_stats64 = mvpp2_get_stats64,
  5935. .ndo_do_ioctl = mvpp2_ioctl,
  5936. };
  5937. static const struct ethtool_ops mvpp2_eth_tool_ops = {
  5938. .nway_reset = phy_ethtool_nway_reset,
  5939. .get_link = ethtool_op_get_link,
  5940. .set_coalesce = mvpp2_ethtool_set_coalesce,
  5941. .get_coalesce = mvpp2_ethtool_get_coalesce,
  5942. .get_drvinfo = mvpp2_ethtool_get_drvinfo,
  5943. .get_ringparam = mvpp2_ethtool_get_ringparam,
  5944. .set_ringparam = mvpp2_ethtool_set_ringparam,
  5945. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  5946. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  5947. };
  5948. /* Used for PPv2.1, or PPv2.2 with the old Device Tree binding that
  5949. * had a single IRQ defined per-port.
  5950. */
  5951. static int mvpp2_simple_queue_vectors_init(struct mvpp2_port *port,
  5952. struct device_node *port_node)
  5953. {
  5954. struct mvpp2_queue_vector *v = &port->qvecs[0];
  5955. v->first_rxq = 0;
  5956. v->nrxqs = port->nrxqs;
  5957. v->type = MVPP2_QUEUE_VECTOR_SHARED;
  5958. v->sw_thread_id = 0;
  5959. v->sw_thread_mask = *cpumask_bits(cpu_online_mask);
  5960. v->port = port;
  5961. v->irq = irq_of_parse_and_map(port_node, 0);
  5962. if (v->irq <= 0)
  5963. return -EINVAL;
  5964. netif_napi_add(port->dev, &v->napi, mvpp2_poll,
  5965. NAPI_POLL_WEIGHT);
  5966. port->nqvecs = 1;
  5967. return 0;
  5968. }
  5969. static int mvpp2_multi_queue_vectors_init(struct mvpp2_port *port,
  5970. struct device_node *port_node)
  5971. {
  5972. struct mvpp2_queue_vector *v;
  5973. int i, ret;
  5974. port->nqvecs = num_possible_cpus();
  5975. if (queue_mode == MVPP2_QDIST_SINGLE_MODE)
  5976. port->nqvecs += 1;
  5977. for (i = 0; i < port->nqvecs; i++) {
  5978. char irqname[16];
  5979. v = port->qvecs + i;
  5980. v->port = port;
  5981. v->type = MVPP2_QUEUE_VECTOR_PRIVATE;
  5982. v->sw_thread_id = i;
  5983. v->sw_thread_mask = BIT(i);
  5984. snprintf(irqname, sizeof(irqname), "tx-cpu%d", i);
  5985. if (queue_mode == MVPP2_QDIST_MULTI_MODE) {
  5986. v->first_rxq = i * MVPP2_DEFAULT_RXQ;
  5987. v->nrxqs = MVPP2_DEFAULT_RXQ;
  5988. } else if (queue_mode == MVPP2_QDIST_SINGLE_MODE &&
  5989. i == (port->nqvecs - 1)) {
  5990. v->first_rxq = 0;
  5991. v->nrxqs = port->nrxqs;
  5992. v->type = MVPP2_QUEUE_VECTOR_SHARED;
  5993. strncpy(irqname, "rx-shared", sizeof(irqname));
  5994. }
  5995. v->irq = of_irq_get_byname(port_node, irqname);
  5996. if (v->irq <= 0) {
  5997. ret = -EINVAL;
  5998. goto err;
  5999. }
  6000. netif_napi_add(port->dev, &v->napi, mvpp2_poll,
  6001. NAPI_POLL_WEIGHT);
  6002. }
  6003. return 0;
  6004. err:
  6005. for (i = 0; i < port->nqvecs; i++)
  6006. irq_dispose_mapping(port->qvecs[i].irq);
  6007. return ret;
  6008. }
  6009. static int mvpp2_queue_vectors_init(struct mvpp2_port *port,
  6010. struct device_node *port_node)
  6011. {
  6012. if (port->has_tx_irqs)
  6013. return mvpp2_multi_queue_vectors_init(port, port_node);
  6014. else
  6015. return mvpp2_simple_queue_vectors_init(port, port_node);
  6016. }
  6017. static void mvpp2_queue_vectors_deinit(struct mvpp2_port *port)
  6018. {
  6019. int i;
  6020. for (i = 0; i < port->nqvecs; i++)
  6021. irq_dispose_mapping(port->qvecs[i].irq);
  6022. }
  6023. /* Configure Rx queue group interrupt for this port */
  6024. static void mvpp2_rx_irqs_setup(struct mvpp2_port *port)
  6025. {
  6026. struct mvpp2 *priv = port->priv;
  6027. u32 val;
  6028. int i;
  6029. if (priv->hw_version == MVPP21) {
  6030. mvpp2_write(priv, MVPP21_ISR_RXQ_GROUP_REG(port->id),
  6031. port->nrxqs);
  6032. return;
  6033. }
  6034. /* Handle the more complicated PPv2.2 case */
  6035. for (i = 0; i < port->nqvecs; i++) {
  6036. struct mvpp2_queue_vector *qv = port->qvecs + i;
  6037. if (!qv->nrxqs)
  6038. continue;
  6039. val = qv->sw_thread_id;
  6040. val |= port->id << MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET;
  6041. mvpp2_write(priv, MVPP22_ISR_RXQ_GROUP_INDEX_REG, val);
  6042. val = qv->first_rxq;
  6043. val |= qv->nrxqs << MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET;
  6044. mvpp2_write(priv, MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG, val);
  6045. }
  6046. }
  6047. /* Initialize port HW */
  6048. static int mvpp2_port_init(struct mvpp2_port *port)
  6049. {
  6050. struct device *dev = port->dev->dev.parent;
  6051. struct mvpp2 *priv = port->priv;
  6052. struct mvpp2_txq_pcpu *txq_pcpu;
  6053. int queue, cpu, err;
  6054. /* Checks for hardware constraints */
  6055. if (port->first_rxq + port->nrxqs >
  6056. MVPP2_MAX_PORTS * priv->max_port_rxqs)
  6057. return -EINVAL;
  6058. if (port->nrxqs % 4 || (port->nrxqs > priv->max_port_rxqs) ||
  6059. (port->ntxqs > MVPP2_MAX_TXQ))
  6060. return -EINVAL;
  6061. /* Disable port */
  6062. mvpp2_egress_disable(port);
  6063. mvpp2_port_disable(port);
  6064. port->tx_time_coal = MVPP2_TXDONE_COAL_USEC;
  6065. port->txqs = devm_kcalloc(dev, port->ntxqs, sizeof(*port->txqs),
  6066. GFP_KERNEL);
  6067. if (!port->txqs)
  6068. return -ENOMEM;
  6069. /* Associate physical Tx queues to this port and initialize.
  6070. * The mapping is predefined.
  6071. */
  6072. for (queue = 0; queue < port->ntxqs; queue++) {
  6073. int queue_phy_id = mvpp2_txq_phys(port->id, queue);
  6074. struct mvpp2_tx_queue *txq;
  6075. txq = devm_kzalloc(dev, sizeof(*txq), GFP_KERNEL);
  6076. if (!txq) {
  6077. err = -ENOMEM;
  6078. goto err_free_percpu;
  6079. }
  6080. txq->pcpu = alloc_percpu(struct mvpp2_txq_pcpu);
  6081. if (!txq->pcpu) {
  6082. err = -ENOMEM;
  6083. goto err_free_percpu;
  6084. }
  6085. txq->id = queue_phy_id;
  6086. txq->log_id = queue;
  6087. txq->done_pkts_coal = MVPP2_TXDONE_COAL_PKTS_THRESH;
  6088. for_each_present_cpu(cpu) {
  6089. txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
  6090. txq_pcpu->cpu = cpu;
  6091. }
  6092. port->txqs[queue] = txq;
  6093. }
  6094. port->rxqs = devm_kcalloc(dev, port->nrxqs, sizeof(*port->rxqs),
  6095. GFP_KERNEL);
  6096. if (!port->rxqs) {
  6097. err = -ENOMEM;
  6098. goto err_free_percpu;
  6099. }
  6100. /* Allocate and initialize Rx queue for this port */
  6101. for (queue = 0; queue < port->nrxqs; queue++) {
  6102. struct mvpp2_rx_queue *rxq;
  6103. /* Map physical Rx queue to port's logical Rx queue */
  6104. rxq = devm_kzalloc(dev, sizeof(*rxq), GFP_KERNEL);
  6105. if (!rxq) {
  6106. err = -ENOMEM;
  6107. goto err_free_percpu;
  6108. }
  6109. /* Map this Rx queue to a physical queue */
  6110. rxq->id = port->first_rxq + queue;
  6111. rxq->port = port->id;
  6112. rxq->logic_rxq = queue;
  6113. port->rxqs[queue] = rxq;
  6114. }
  6115. mvpp2_rx_irqs_setup(port);
  6116. /* Create Rx descriptor rings */
  6117. for (queue = 0; queue < port->nrxqs; queue++) {
  6118. struct mvpp2_rx_queue *rxq = port->rxqs[queue];
  6119. rxq->size = port->rx_ring_size;
  6120. rxq->pkts_coal = MVPP2_RX_COAL_PKTS;
  6121. rxq->time_coal = MVPP2_RX_COAL_USEC;
  6122. }
  6123. mvpp2_ingress_disable(port);
  6124. /* Port default configuration */
  6125. mvpp2_defaults_set(port);
  6126. /* Port's classifier configuration */
  6127. mvpp2_cls_oversize_rxq_set(port);
  6128. mvpp2_cls_port_config(port);
  6129. /* Provide an initial Rx packet size */
  6130. port->pkt_size = MVPP2_RX_PKT_SIZE(port->dev->mtu);
  6131. /* Initialize pools for swf */
  6132. err = mvpp2_swf_bm_pool_init(port);
  6133. if (err)
  6134. goto err_free_percpu;
  6135. return 0;
  6136. err_free_percpu:
  6137. for (queue = 0; queue < port->ntxqs; queue++) {
  6138. if (!port->txqs[queue])
  6139. continue;
  6140. free_percpu(port->txqs[queue]->pcpu);
  6141. }
  6142. return err;
  6143. }
  6144. /* Checks if the port DT description has the TX interrupts
  6145. * described. On PPv2.1, there are no such interrupts. On PPv2.2,
  6146. * there are available, but we need to keep support for old DTs.
  6147. */
  6148. static bool mvpp2_port_has_tx_irqs(struct mvpp2 *priv,
  6149. struct device_node *port_node)
  6150. {
  6151. char *irqs[5] = { "rx-shared", "tx-cpu0", "tx-cpu1",
  6152. "tx-cpu2", "tx-cpu3" };
  6153. int ret, i;
  6154. if (priv->hw_version == MVPP21)
  6155. return false;
  6156. for (i = 0; i < 5; i++) {
  6157. ret = of_property_match_string(port_node, "interrupt-names",
  6158. irqs[i]);
  6159. if (ret < 0)
  6160. return false;
  6161. }
  6162. return true;
  6163. }
  6164. static void mvpp2_port_copy_mac_addr(struct net_device *dev, struct mvpp2 *priv,
  6165. struct device_node *port_node,
  6166. char **mac_from)
  6167. {
  6168. struct mvpp2_port *port = netdev_priv(dev);
  6169. char hw_mac_addr[ETH_ALEN] = {0};
  6170. const char *dt_mac_addr;
  6171. dt_mac_addr = of_get_mac_address(port_node);
  6172. if (dt_mac_addr && is_valid_ether_addr(dt_mac_addr)) {
  6173. *mac_from = "device tree";
  6174. ether_addr_copy(dev->dev_addr, dt_mac_addr);
  6175. return;
  6176. }
  6177. if (priv->hw_version == MVPP21) {
  6178. mvpp21_get_mac_address(port, hw_mac_addr);
  6179. if (is_valid_ether_addr(hw_mac_addr)) {
  6180. *mac_from = "hardware";
  6181. ether_addr_copy(dev->dev_addr, hw_mac_addr);
  6182. return;
  6183. }
  6184. }
  6185. *mac_from = "random";
  6186. eth_hw_addr_random(dev);
  6187. }
  6188. /* Ports initialization */
  6189. static int mvpp2_port_probe(struct platform_device *pdev,
  6190. struct device_node *port_node,
  6191. struct mvpp2 *priv, int index)
  6192. {
  6193. struct device_node *phy_node;
  6194. struct phy *comphy;
  6195. struct mvpp2_port *port;
  6196. struct mvpp2_port_pcpu *port_pcpu;
  6197. struct net_device *dev;
  6198. struct resource *res;
  6199. char *mac_from = "";
  6200. unsigned int ntxqs, nrxqs;
  6201. bool has_tx_irqs;
  6202. u32 id;
  6203. int features;
  6204. int phy_mode;
  6205. int err, i, cpu;
  6206. has_tx_irqs = mvpp2_port_has_tx_irqs(priv, port_node);
  6207. if (!has_tx_irqs)
  6208. queue_mode = MVPP2_QDIST_SINGLE_MODE;
  6209. ntxqs = MVPP2_MAX_TXQ;
  6210. if (priv->hw_version == MVPP22 && queue_mode == MVPP2_QDIST_MULTI_MODE)
  6211. nrxqs = MVPP2_DEFAULT_RXQ * num_possible_cpus();
  6212. else
  6213. nrxqs = MVPP2_DEFAULT_RXQ;
  6214. dev = alloc_etherdev_mqs(sizeof(*port), ntxqs, nrxqs);
  6215. if (!dev)
  6216. return -ENOMEM;
  6217. phy_node = of_parse_phandle(port_node, "phy", 0);
  6218. phy_mode = of_get_phy_mode(port_node);
  6219. if (phy_mode < 0) {
  6220. dev_err(&pdev->dev, "incorrect phy mode\n");
  6221. err = phy_mode;
  6222. goto err_free_netdev;
  6223. }
  6224. comphy = devm_of_phy_get(&pdev->dev, port_node, NULL);
  6225. if (IS_ERR(comphy)) {
  6226. if (PTR_ERR(comphy) == -EPROBE_DEFER) {
  6227. err = -EPROBE_DEFER;
  6228. goto err_free_netdev;
  6229. }
  6230. comphy = NULL;
  6231. }
  6232. if (of_property_read_u32(port_node, "port-id", &id)) {
  6233. err = -EINVAL;
  6234. dev_err(&pdev->dev, "missing port-id value\n");
  6235. goto err_free_netdev;
  6236. }
  6237. dev->tx_queue_len = MVPP2_MAX_TXD;
  6238. dev->watchdog_timeo = 5 * HZ;
  6239. dev->netdev_ops = &mvpp2_netdev_ops;
  6240. dev->ethtool_ops = &mvpp2_eth_tool_ops;
  6241. port = netdev_priv(dev);
  6242. port->dev = dev;
  6243. port->ntxqs = ntxqs;
  6244. port->nrxqs = nrxqs;
  6245. port->priv = priv;
  6246. port->has_tx_irqs = has_tx_irqs;
  6247. err = mvpp2_queue_vectors_init(port, port_node);
  6248. if (err)
  6249. goto err_free_netdev;
  6250. port->link_irq = of_irq_get_byname(port_node, "link");
  6251. if (port->link_irq == -EPROBE_DEFER) {
  6252. err = -EPROBE_DEFER;
  6253. goto err_deinit_qvecs;
  6254. }
  6255. if (port->link_irq <= 0)
  6256. /* the link irq is optional */
  6257. port->link_irq = 0;
  6258. if (of_property_read_bool(port_node, "marvell,loopback"))
  6259. port->flags |= MVPP2_F_LOOPBACK;
  6260. port->id = id;
  6261. if (priv->hw_version == MVPP21)
  6262. port->first_rxq = port->id * port->nrxqs;
  6263. else
  6264. port->first_rxq = port->id * priv->max_port_rxqs;
  6265. port->phy_node = phy_node;
  6266. port->phy_interface = phy_mode;
  6267. port->comphy = comphy;
  6268. if (priv->hw_version == MVPP21) {
  6269. res = platform_get_resource(pdev, IORESOURCE_MEM, 2 + id);
  6270. port->base = devm_ioremap_resource(&pdev->dev, res);
  6271. if (IS_ERR(port->base)) {
  6272. err = PTR_ERR(port->base);
  6273. goto err_free_irq;
  6274. }
  6275. } else {
  6276. if (of_property_read_u32(port_node, "gop-port-id",
  6277. &port->gop_id)) {
  6278. err = -EINVAL;
  6279. dev_err(&pdev->dev, "missing gop-port-id value\n");
  6280. goto err_deinit_qvecs;
  6281. }
  6282. port->base = priv->iface_base + MVPP22_GMAC_BASE(port->gop_id);
  6283. }
  6284. /* Alloc per-cpu stats */
  6285. port->stats = netdev_alloc_pcpu_stats(struct mvpp2_pcpu_stats);
  6286. if (!port->stats) {
  6287. err = -ENOMEM;
  6288. goto err_free_irq;
  6289. }
  6290. mvpp2_port_copy_mac_addr(dev, priv, port_node, &mac_from);
  6291. port->tx_ring_size = MVPP2_MAX_TXD;
  6292. port->rx_ring_size = MVPP2_MAX_RXD;
  6293. SET_NETDEV_DEV(dev, &pdev->dev);
  6294. err = mvpp2_port_init(port);
  6295. if (err < 0) {
  6296. dev_err(&pdev->dev, "failed to init port %d\n", id);
  6297. goto err_free_stats;
  6298. }
  6299. mvpp2_port_periodic_xon_disable(port);
  6300. if (priv->hw_version == MVPP21)
  6301. mvpp2_port_fc_adv_enable(port);
  6302. mvpp2_port_reset(port);
  6303. port->pcpu = alloc_percpu(struct mvpp2_port_pcpu);
  6304. if (!port->pcpu) {
  6305. err = -ENOMEM;
  6306. goto err_free_txq_pcpu;
  6307. }
  6308. if (!port->has_tx_irqs) {
  6309. for_each_present_cpu(cpu) {
  6310. port_pcpu = per_cpu_ptr(port->pcpu, cpu);
  6311. hrtimer_init(&port_pcpu->tx_done_timer, CLOCK_MONOTONIC,
  6312. HRTIMER_MODE_REL_PINNED);
  6313. port_pcpu->tx_done_timer.function = mvpp2_hr_timer_cb;
  6314. port_pcpu->timer_scheduled = false;
  6315. tasklet_init(&port_pcpu->tx_done_tasklet,
  6316. mvpp2_tx_proc_cb,
  6317. (unsigned long)dev);
  6318. }
  6319. }
  6320. features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
  6321. dev->features = features | NETIF_F_RXCSUM;
  6322. dev->hw_features |= features | NETIF_F_RXCSUM | NETIF_F_GRO;
  6323. dev->vlan_features |= features;
  6324. /* MTU range: 68 - 9676 */
  6325. dev->min_mtu = ETH_MIN_MTU;
  6326. /* 9676 == 9700 - 20 and rounding to 8 */
  6327. dev->max_mtu = 9676;
  6328. err = register_netdev(dev);
  6329. if (err < 0) {
  6330. dev_err(&pdev->dev, "failed to register netdev\n");
  6331. goto err_free_port_pcpu;
  6332. }
  6333. netdev_info(dev, "Using %s mac address %pM\n", mac_from, dev->dev_addr);
  6334. priv->port_list[index] = port;
  6335. return 0;
  6336. err_free_port_pcpu:
  6337. free_percpu(port->pcpu);
  6338. err_free_txq_pcpu:
  6339. for (i = 0; i < port->ntxqs; i++)
  6340. free_percpu(port->txqs[i]->pcpu);
  6341. err_free_stats:
  6342. free_percpu(port->stats);
  6343. err_free_irq:
  6344. if (port->link_irq)
  6345. irq_dispose_mapping(port->link_irq);
  6346. err_deinit_qvecs:
  6347. mvpp2_queue_vectors_deinit(port);
  6348. err_free_netdev:
  6349. of_node_put(phy_node);
  6350. free_netdev(dev);
  6351. return err;
  6352. }
  6353. /* Ports removal routine */
  6354. static void mvpp2_port_remove(struct mvpp2_port *port)
  6355. {
  6356. int i;
  6357. unregister_netdev(port->dev);
  6358. of_node_put(port->phy_node);
  6359. free_percpu(port->pcpu);
  6360. free_percpu(port->stats);
  6361. for (i = 0; i < port->ntxqs; i++)
  6362. free_percpu(port->txqs[i]->pcpu);
  6363. mvpp2_queue_vectors_deinit(port);
  6364. if (port->link_irq)
  6365. irq_dispose_mapping(port->link_irq);
  6366. free_netdev(port->dev);
  6367. }
  6368. /* Initialize decoding windows */
  6369. static void mvpp2_conf_mbus_windows(const struct mbus_dram_target_info *dram,
  6370. struct mvpp2 *priv)
  6371. {
  6372. u32 win_enable;
  6373. int i;
  6374. for (i = 0; i < 6; i++) {
  6375. mvpp2_write(priv, MVPP2_WIN_BASE(i), 0);
  6376. mvpp2_write(priv, MVPP2_WIN_SIZE(i), 0);
  6377. if (i < 4)
  6378. mvpp2_write(priv, MVPP2_WIN_REMAP(i), 0);
  6379. }
  6380. win_enable = 0;
  6381. for (i = 0; i < dram->num_cs; i++) {
  6382. const struct mbus_dram_window *cs = dram->cs + i;
  6383. mvpp2_write(priv, MVPP2_WIN_BASE(i),
  6384. (cs->base & 0xffff0000) | (cs->mbus_attr << 8) |
  6385. dram->mbus_dram_target_id);
  6386. mvpp2_write(priv, MVPP2_WIN_SIZE(i),
  6387. (cs->size - 1) & 0xffff0000);
  6388. win_enable |= (1 << i);
  6389. }
  6390. mvpp2_write(priv, MVPP2_BASE_ADDR_ENABLE, win_enable);
  6391. }
  6392. /* Initialize Rx FIFO's */
  6393. static void mvpp2_rx_fifo_init(struct mvpp2 *priv)
  6394. {
  6395. int port;
  6396. for (port = 0; port < MVPP2_MAX_PORTS; port++) {
  6397. mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
  6398. MVPP2_RX_FIFO_PORT_DATA_SIZE);
  6399. mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
  6400. MVPP2_RX_FIFO_PORT_ATTR_SIZE);
  6401. }
  6402. mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
  6403. MVPP2_RX_FIFO_PORT_MIN_PKT);
  6404. mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
  6405. }
  6406. static void mvpp2_axi_init(struct mvpp2 *priv)
  6407. {
  6408. u32 val, rdval, wrval;
  6409. mvpp2_write(priv, MVPP22_BM_ADDR_HIGH_RLS_REG, 0x0);
  6410. /* AXI Bridge Configuration */
  6411. rdval = MVPP22_AXI_CODE_CACHE_RD_CACHE
  6412. << MVPP22_AXI_ATTR_CACHE_OFFS;
  6413. rdval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
  6414. << MVPP22_AXI_ATTR_DOMAIN_OFFS;
  6415. wrval = MVPP22_AXI_CODE_CACHE_WR_CACHE
  6416. << MVPP22_AXI_ATTR_CACHE_OFFS;
  6417. wrval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
  6418. << MVPP22_AXI_ATTR_DOMAIN_OFFS;
  6419. /* BM */
  6420. mvpp2_write(priv, MVPP22_AXI_BM_WR_ATTR_REG, wrval);
  6421. mvpp2_write(priv, MVPP22_AXI_BM_RD_ATTR_REG, rdval);
  6422. /* Descriptors */
  6423. mvpp2_write(priv, MVPP22_AXI_AGGRQ_DESCR_RD_ATTR_REG, rdval);
  6424. mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_WR_ATTR_REG, wrval);
  6425. mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_RD_ATTR_REG, rdval);
  6426. mvpp2_write(priv, MVPP22_AXI_RXQ_DESCR_WR_ATTR_REG, wrval);
  6427. /* Buffer Data */
  6428. mvpp2_write(priv, MVPP22_AXI_TX_DATA_RD_ATTR_REG, rdval);
  6429. mvpp2_write(priv, MVPP22_AXI_RX_DATA_WR_ATTR_REG, wrval);
  6430. val = MVPP22_AXI_CODE_CACHE_NON_CACHE
  6431. << MVPP22_AXI_CODE_CACHE_OFFS;
  6432. val |= MVPP22_AXI_CODE_DOMAIN_SYSTEM
  6433. << MVPP22_AXI_CODE_DOMAIN_OFFS;
  6434. mvpp2_write(priv, MVPP22_AXI_RD_NORMAL_CODE_REG, val);
  6435. mvpp2_write(priv, MVPP22_AXI_WR_NORMAL_CODE_REG, val);
  6436. val = MVPP22_AXI_CODE_CACHE_RD_CACHE
  6437. << MVPP22_AXI_CODE_CACHE_OFFS;
  6438. val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
  6439. << MVPP22_AXI_CODE_DOMAIN_OFFS;
  6440. mvpp2_write(priv, MVPP22_AXI_RD_SNOOP_CODE_REG, val);
  6441. val = MVPP22_AXI_CODE_CACHE_WR_CACHE
  6442. << MVPP22_AXI_CODE_CACHE_OFFS;
  6443. val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
  6444. << MVPP22_AXI_CODE_DOMAIN_OFFS;
  6445. mvpp2_write(priv, MVPP22_AXI_WR_SNOOP_CODE_REG, val);
  6446. }
  6447. /* Initialize network controller common part HW */
  6448. static int mvpp2_init(struct platform_device *pdev, struct mvpp2 *priv)
  6449. {
  6450. const struct mbus_dram_target_info *dram_target_info;
  6451. int err, i;
  6452. u32 val;
  6453. /* MBUS windows configuration */
  6454. dram_target_info = mv_mbus_dram_info();
  6455. if (dram_target_info)
  6456. mvpp2_conf_mbus_windows(dram_target_info, priv);
  6457. if (priv->hw_version == MVPP22)
  6458. mvpp2_axi_init(priv);
  6459. /* Disable HW PHY polling */
  6460. if (priv->hw_version == MVPP21) {
  6461. val = readl(priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
  6462. val |= MVPP2_PHY_AN_STOP_SMI0_MASK;
  6463. writel(val, priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
  6464. } else {
  6465. val = readl(priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
  6466. val &= ~MVPP22_SMI_POLLING_EN;
  6467. writel(val, priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
  6468. }
  6469. /* Allocate and initialize aggregated TXQs */
  6470. priv->aggr_txqs = devm_kcalloc(&pdev->dev, num_present_cpus(),
  6471. sizeof(*priv->aggr_txqs),
  6472. GFP_KERNEL);
  6473. if (!priv->aggr_txqs)
  6474. return -ENOMEM;
  6475. for_each_present_cpu(i) {
  6476. priv->aggr_txqs[i].id = i;
  6477. priv->aggr_txqs[i].size = MVPP2_AGGR_TXQ_SIZE;
  6478. err = mvpp2_aggr_txq_init(pdev, &priv->aggr_txqs[i], i, priv);
  6479. if (err < 0)
  6480. return err;
  6481. }
  6482. /* Rx Fifo Init */
  6483. mvpp2_rx_fifo_init(priv);
  6484. if (priv->hw_version == MVPP21)
  6485. writel(MVPP2_EXT_GLOBAL_CTRL_DEFAULT,
  6486. priv->lms_base + MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG);
  6487. /* Allow cache snoop when transmiting packets */
  6488. mvpp2_write(priv, MVPP2_TX_SNOOP_REG, 0x1);
  6489. /* Buffer Manager initialization */
  6490. err = mvpp2_bm_init(pdev, priv);
  6491. if (err < 0)
  6492. return err;
  6493. /* Parser default initialization */
  6494. err = mvpp2_prs_default_init(pdev, priv);
  6495. if (err < 0)
  6496. return err;
  6497. /* Classifier default initialization */
  6498. mvpp2_cls_init(priv);
  6499. return 0;
  6500. }
  6501. static int mvpp2_probe(struct platform_device *pdev)
  6502. {
  6503. struct device_node *dn = pdev->dev.of_node;
  6504. struct device_node *port_node;
  6505. struct mvpp2 *priv;
  6506. struct resource *res;
  6507. void __iomem *base;
  6508. int port_count, i;
  6509. int err;
  6510. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  6511. if (!priv)
  6512. return -ENOMEM;
  6513. priv->hw_version =
  6514. (unsigned long)of_device_get_match_data(&pdev->dev);
  6515. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  6516. base = devm_ioremap_resource(&pdev->dev, res);
  6517. if (IS_ERR(base))
  6518. return PTR_ERR(base);
  6519. if (priv->hw_version == MVPP21) {
  6520. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  6521. priv->lms_base = devm_ioremap_resource(&pdev->dev, res);
  6522. if (IS_ERR(priv->lms_base))
  6523. return PTR_ERR(priv->lms_base);
  6524. } else {
  6525. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  6526. priv->iface_base = devm_ioremap_resource(&pdev->dev, res);
  6527. if (IS_ERR(priv->iface_base))
  6528. return PTR_ERR(priv->iface_base);
  6529. priv->sysctrl_base =
  6530. syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
  6531. "marvell,system-controller");
  6532. if (IS_ERR(priv->sysctrl_base))
  6533. /* The system controller regmap is optional for dt
  6534. * compatibility reasons. When not provided, the
  6535. * configuration of the GoP relies on the
  6536. * firmware/bootloader.
  6537. */
  6538. priv->sysctrl_base = NULL;
  6539. }
  6540. for (i = 0; i < MVPP2_MAX_THREADS; i++) {
  6541. u32 addr_space_sz;
  6542. addr_space_sz = (priv->hw_version == MVPP21 ?
  6543. MVPP21_ADDR_SPACE_SZ : MVPP22_ADDR_SPACE_SZ);
  6544. priv->swth_base[i] = base + i * addr_space_sz;
  6545. }
  6546. if (priv->hw_version == MVPP21)
  6547. priv->max_port_rxqs = 8;
  6548. else
  6549. priv->max_port_rxqs = 32;
  6550. priv->pp_clk = devm_clk_get(&pdev->dev, "pp_clk");
  6551. if (IS_ERR(priv->pp_clk))
  6552. return PTR_ERR(priv->pp_clk);
  6553. err = clk_prepare_enable(priv->pp_clk);
  6554. if (err < 0)
  6555. return err;
  6556. priv->gop_clk = devm_clk_get(&pdev->dev, "gop_clk");
  6557. if (IS_ERR(priv->gop_clk)) {
  6558. err = PTR_ERR(priv->gop_clk);
  6559. goto err_pp_clk;
  6560. }
  6561. err = clk_prepare_enable(priv->gop_clk);
  6562. if (err < 0)
  6563. goto err_pp_clk;
  6564. if (priv->hw_version == MVPP22) {
  6565. priv->mg_clk = devm_clk_get(&pdev->dev, "mg_clk");
  6566. if (IS_ERR(priv->mg_clk)) {
  6567. err = PTR_ERR(priv->mg_clk);
  6568. goto err_gop_clk;
  6569. }
  6570. err = clk_prepare_enable(priv->mg_clk);
  6571. if (err < 0)
  6572. goto err_gop_clk;
  6573. priv->axi_clk = devm_clk_get(&pdev->dev, "axi_clk");
  6574. if (IS_ERR(priv->axi_clk)) {
  6575. err = PTR_ERR(priv->axi_clk);
  6576. if (err == -EPROBE_DEFER)
  6577. goto err_gop_clk;
  6578. priv->axi_clk = NULL;
  6579. } else {
  6580. err = clk_prepare_enable(priv->axi_clk);
  6581. if (err < 0)
  6582. goto err_gop_clk;
  6583. }
  6584. }
  6585. /* Get system's tclk rate */
  6586. priv->tclk = clk_get_rate(priv->pp_clk);
  6587. if (priv->hw_version == MVPP22) {
  6588. err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(40));
  6589. if (err)
  6590. goto err_mg_clk;
  6591. /* Sadly, the BM pools all share the same register to
  6592. * store the high 32 bits of their address. So they
  6593. * must all have the same high 32 bits, which forces
  6594. * us to restrict coherent memory to DMA_BIT_MASK(32).
  6595. */
  6596. err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  6597. if (err)
  6598. goto err_mg_clk;
  6599. }
  6600. /* Initialize network controller */
  6601. err = mvpp2_init(pdev, priv);
  6602. if (err < 0) {
  6603. dev_err(&pdev->dev, "failed to initialize controller\n");
  6604. goto err_mg_clk;
  6605. }
  6606. port_count = of_get_available_child_count(dn);
  6607. if (port_count == 0) {
  6608. dev_err(&pdev->dev, "no ports enabled\n");
  6609. err = -ENODEV;
  6610. goto err_mg_clk;
  6611. }
  6612. priv->port_list = devm_kcalloc(&pdev->dev, port_count,
  6613. sizeof(*priv->port_list),
  6614. GFP_KERNEL);
  6615. if (!priv->port_list) {
  6616. err = -ENOMEM;
  6617. goto err_mg_clk;
  6618. }
  6619. /* Initialize ports */
  6620. i = 0;
  6621. for_each_available_child_of_node(dn, port_node) {
  6622. err = mvpp2_port_probe(pdev, port_node, priv, i);
  6623. if (err < 0)
  6624. goto err_mg_clk;
  6625. i++;
  6626. }
  6627. platform_set_drvdata(pdev, priv);
  6628. return 0;
  6629. err_mg_clk:
  6630. clk_disable_unprepare(priv->axi_clk);
  6631. if (priv->hw_version == MVPP22)
  6632. clk_disable_unprepare(priv->mg_clk);
  6633. err_gop_clk:
  6634. clk_disable_unprepare(priv->gop_clk);
  6635. err_pp_clk:
  6636. clk_disable_unprepare(priv->pp_clk);
  6637. return err;
  6638. }
  6639. static int mvpp2_remove(struct platform_device *pdev)
  6640. {
  6641. struct mvpp2 *priv = platform_get_drvdata(pdev);
  6642. struct device_node *dn = pdev->dev.of_node;
  6643. struct device_node *port_node;
  6644. int i = 0;
  6645. for_each_available_child_of_node(dn, port_node) {
  6646. if (priv->port_list[i])
  6647. mvpp2_port_remove(priv->port_list[i]);
  6648. i++;
  6649. }
  6650. for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
  6651. struct mvpp2_bm_pool *bm_pool = &priv->bm_pools[i];
  6652. mvpp2_bm_pool_destroy(pdev, priv, bm_pool);
  6653. }
  6654. for_each_present_cpu(i) {
  6655. struct mvpp2_tx_queue *aggr_txq = &priv->aggr_txqs[i];
  6656. dma_free_coherent(&pdev->dev,
  6657. MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
  6658. aggr_txq->descs,
  6659. aggr_txq->descs_dma);
  6660. }
  6661. clk_disable_unprepare(priv->axi_clk);
  6662. clk_disable_unprepare(priv->mg_clk);
  6663. clk_disable_unprepare(priv->pp_clk);
  6664. clk_disable_unprepare(priv->gop_clk);
  6665. return 0;
  6666. }
  6667. static const struct of_device_id mvpp2_match[] = {
  6668. {
  6669. .compatible = "marvell,armada-375-pp2",
  6670. .data = (void *)MVPP21,
  6671. },
  6672. {
  6673. .compatible = "marvell,armada-7k-pp22",
  6674. .data = (void *)MVPP22,
  6675. },
  6676. { }
  6677. };
  6678. MODULE_DEVICE_TABLE(of, mvpp2_match);
  6679. static struct platform_driver mvpp2_driver = {
  6680. .probe = mvpp2_probe,
  6681. .remove = mvpp2_remove,
  6682. .driver = {
  6683. .name = MVPP2_DRIVER_NAME,
  6684. .of_match_table = mvpp2_match,
  6685. },
  6686. };
  6687. module_platform_driver(mvpp2_driver);
  6688. MODULE_DESCRIPTION("Marvell PPv2 Ethernet Driver - www.marvell.com");
  6689. MODULE_AUTHOR("Marcin Wojtas <mw@semihalf.com>");
  6690. MODULE_LICENSE("GPL v2");