mvpp2.c 178 KB

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