mvpp2.c 224 KB

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