mvpp2.c 180 KB

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