mvpp2.c 227 KB

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