mvneta.c 160 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972
  1. /*
  2. * Driver for Marvell NETA network card for Armada XP and Armada 370 SoCs.
  3. *
  4. * Copyright (C) 2012 Marvell
  5. *
  6. * Rami Rosen <rosenr@marvell.com>
  7. * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
  8. *
  9. * This file is licensed under the terms of the GNU General Public
  10. * License version 2. This program is licensed "as is" without any
  11. * warranty of any kind, whether express or implied.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/cpu.h>
  15. #include <linux/etherdevice.h>
  16. #include <linux/if_vlan.h>
  17. #include <linux/inetdevice.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/io.h>
  20. #include <linux/kernel.h>
  21. #include <linux/mbus.h>
  22. #include <linux/module.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/of.h>
  25. #include <linux/of_address.h>
  26. #include <linux/of_irq.h>
  27. #include <linux/of_mdio.h>
  28. #include <linux/of_net.h>
  29. #include <linux/phy/phy.h>
  30. #include <linux/phy.h>
  31. #include <linux/phylink.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/skbuff.h>
  34. #include <net/hwbm.h>
  35. #include "mvneta_bm.h"
  36. #include <net/ip.h>
  37. #include <net/ipv6.h>
  38. #include <net/tso.h>
  39. #include <net/page_pool/helpers.h>
  40. #include <net/pkt_sched.h>
  41. #include <linux/bpf_trace.h>
  42. /* Registers */
  43. #define MVNETA_RXQ_CONFIG_REG(q) (0x1400 + ((q) << 2))
  44. #define MVNETA_RXQ_HW_BUF_ALLOC BIT(0)
  45. #define MVNETA_RXQ_SHORT_POOL_ID_SHIFT 4
  46. #define MVNETA_RXQ_SHORT_POOL_ID_MASK 0x30
  47. #define MVNETA_RXQ_LONG_POOL_ID_SHIFT 6
  48. #define MVNETA_RXQ_LONG_POOL_ID_MASK 0xc0
  49. #define MVNETA_RXQ_PKT_OFFSET_ALL_MASK (0xf << 8)
  50. #define MVNETA_RXQ_PKT_OFFSET_MASK(offs) ((offs) << 8)
  51. #define MVNETA_RXQ_THRESHOLD_REG(q) (0x14c0 + ((q) << 2))
  52. #define MVNETA_RXQ_NON_OCCUPIED(v) ((v) << 16)
  53. #define MVNETA_RXQ_BASE_ADDR_REG(q) (0x1480 + ((q) << 2))
  54. #define MVNETA_RXQ_SIZE_REG(q) (0x14a0 + ((q) << 2))
  55. #define MVNETA_RXQ_BUF_SIZE_SHIFT 19
  56. #define MVNETA_RXQ_BUF_SIZE_MASK (0x1fff << 19)
  57. #define MVNETA_RXQ_STATUS_REG(q) (0x14e0 + ((q) << 2))
  58. #define MVNETA_RXQ_OCCUPIED_ALL_MASK 0x3fff
  59. #define MVNETA_RXQ_STATUS_UPDATE_REG(q) (0x1500 + ((q) << 2))
  60. #define MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT 16
  61. #define MVNETA_RXQ_ADD_NON_OCCUPIED_MAX 255
  62. #define MVNETA_PORT_POOL_BUFFER_SZ_REG(pool) (0x1700 + ((pool) << 2))
  63. #define MVNETA_PORT_POOL_BUFFER_SZ_SHIFT 3
  64. #define MVNETA_PORT_POOL_BUFFER_SZ_MASK 0xfff8
  65. #define MVNETA_PORT_RX_RESET 0x1cc0
  66. #define MVNETA_PORT_RX_DMA_RESET BIT(0)
  67. #define MVNETA_PHY_ADDR 0x2000
  68. #define MVNETA_PHY_ADDR_MASK 0x1f
  69. #define MVNETA_MBUS_RETRY 0x2010
  70. #define MVNETA_UNIT_INTR_CAUSE 0x2080
  71. #define MVNETA_UNIT_CONTROL 0x20B0
  72. #define MVNETA_PHY_POLLING_ENABLE BIT(1)
  73. #define MVNETA_WIN_BASE(w) (0x2200 + ((w) << 3))
  74. #define MVNETA_WIN_SIZE(w) (0x2204 + ((w) << 3))
  75. #define MVNETA_WIN_REMAP(w) (0x2280 + ((w) << 2))
  76. #define MVNETA_BASE_ADDR_ENABLE 0x2290
  77. #define MVNETA_AC5_CNM_DDR_TARGET 0x2
  78. #define MVNETA_AC5_CNM_DDR_ATTR 0xb
  79. #define MVNETA_ACCESS_PROTECT_ENABLE 0x2294
  80. #define MVNETA_PORT_CONFIG 0x2400
  81. #define MVNETA_UNI_PROMISC_MODE BIT(0)
  82. #define MVNETA_DEF_RXQ(q) ((q) << 1)
  83. #define MVNETA_DEF_RXQ_ARP(q) ((q) << 4)
  84. #define MVNETA_TX_UNSET_ERR_SUM BIT(12)
  85. #define MVNETA_DEF_RXQ_TCP(q) ((q) << 16)
  86. #define MVNETA_DEF_RXQ_UDP(q) ((q) << 19)
  87. #define MVNETA_DEF_RXQ_BPDU(q) ((q) << 22)
  88. #define MVNETA_RX_CSUM_WITH_PSEUDO_HDR BIT(25)
  89. #define MVNETA_PORT_CONFIG_DEFL_VALUE(q) (MVNETA_DEF_RXQ(q) | \
  90. MVNETA_DEF_RXQ_ARP(q) | \
  91. MVNETA_DEF_RXQ_TCP(q) | \
  92. MVNETA_DEF_RXQ_UDP(q) | \
  93. MVNETA_DEF_RXQ_BPDU(q) | \
  94. MVNETA_TX_UNSET_ERR_SUM | \
  95. MVNETA_RX_CSUM_WITH_PSEUDO_HDR)
  96. #define MVNETA_PORT_CONFIG_EXTEND 0x2404
  97. #define MVNETA_MAC_ADDR_LOW 0x2414
  98. #define MVNETA_MAC_ADDR_HIGH 0x2418
  99. #define MVNETA_SDMA_CONFIG 0x241c
  100. #define MVNETA_SDMA_BRST_SIZE_16 4
  101. #define MVNETA_RX_BRST_SZ_MASK(burst) ((burst) << 1)
  102. #define MVNETA_RX_NO_DATA_SWAP BIT(4)
  103. #define MVNETA_TX_NO_DATA_SWAP BIT(5)
  104. #define MVNETA_DESC_SWAP BIT(6)
  105. #define MVNETA_TX_BRST_SZ_MASK(burst) ((burst) << 22)
  106. #define MVNETA_VLAN_PRIO_TO_RXQ 0x2440
  107. #define MVNETA_VLAN_PRIO_RXQ_MAP(prio, rxq) ((rxq) << ((prio) * 3))
  108. #define MVNETA_PORT_STATUS 0x2444
  109. #define MVNETA_TX_IN_PRGRS BIT(0)
  110. #define MVNETA_TX_FIFO_EMPTY BIT(8)
  111. #define MVNETA_RX_MIN_FRAME_SIZE 0x247c
  112. /* Only exists on Armada XP and Armada 370 */
  113. #define MVNETA_SERDES_CFG 0x24A0
  114. #define MVNETA_SGMII_SERDES_PROTO 0x0cc7
  115. #define MVNETA_QSGMII_SERDES_PROTO 0x0667
  116. #define MVNETA_HSGMII_SERDES_PROTO 0x1107
  117. #define MVNETA_TYPE_PRIO 0x24bc
  118. #define MVNETA_FORCE_UNI BIT(21)
  119. #define MVNETA_TXQ_CMD_1 0x24e4
  120. #define MVNETA_TXQ_CMD 0x2448
  121. #define MVNETA_TXQ_DISABLE_SHIFT 8
  122. #define MVNETA_TXQ_ENABLE_MASK 0x000000ff
  123. #define MVNETA_RX_DISCARD_FRAME_COUNT 0x2484
  124. #define MVNETA_OVERRUN_FRAME_COUNT 0x2488
  125. #define MVNETA_GMAC_CLOCK_DIVIDER 0x24f4
  126. #define MVNETA_GMAC_1MS_CLOCK_ENABLE BIT(31)
  127. #define MVNETA_ACC_MODE 0x2500
  128. #define MVNETA_BM_ADDRESS 0x2504
  129. #define MVNETA_CPU_MAP(cpu) (0x2540 + ((cpu) << 2))
  130. #define MVNETA_CPU_RXQ_ACCESS_ALL_MASK 0x000000ff
  131. #define MVNETA_CPU_TXQ_ACCESS_ALL_MASK 0x0000ff00
  132. #define MVNETA_CPU_RXQ_ACCESS(rxq) BIT(rxq)
  133. #define MVNETA_CPU_TXQ_ACCESS(txq) BIT(txq + 8)
  134. #define MVNETA_RXQ_TIME_COAL_REG(q) (0x2580 + ((q) << 2))
  135. /* Exception Interrupt Port/Queue Cause register
  136. *
  137. * Their behavior depend of the mapping done using the PCPX2Q
  138. * registers. For a given CPU if the bit associated to a queue is not
  139. * set, then for the register a read from this CPU will always return
  140. * 0 and a write won't do anything
  141. */
  142. #define MVNETA_INTR_NEW_CAUSE 0x25a0
  143. #define MVNETA_INTR_NEW_MASK 0x25a4
  144. /* bits 0..7 = TXQ SENT, one bit per queue.
  145. * bits 8..15 = RXQ OCCUP, one bit per queue.
  146. * bits 16..23 = RXQ FREE, one bit per queue.
  147. * bit 29 = OLD_REG_SUM, see old reg ?
  148. * bit 30 = TX_ERR_SUM, one bit for 4 ports
  149. * bit 31 = MISC_SUM, one bit for 4 ports
  150. */
  151. #define MVNETA_TX_INTR_MASK(nr_txqs) (((1 << nr_txqs) - 1) << 0)
  152. #define MVNETA_TX_INTR_MASK_ALL (0xff << 0)
  153. #define MVNETA_RX_INTR_MASK(nr_rxqs) (((1 << nr_rxqs) - 1) << 8)
  154. #define MVNETA_RX_INTR_MASK_ALL (0xff << 8)
  155. #define MVNETA_MISCINTR_INTR_MASK BIT(31)
  156. #define MVNETA_INTR_OLD_CAUSE 0x25a8
  157. #define MVNETA_INTR_OLD_MASK 0x25ac
  158. /* Data Path Port/Queue Cause Register */
  159. #define MVNETA_INTR_MISC_CAUSE 0x25b0
  160. #define MVNETA_INTR_MISC_MASK 0x25b4
  161. #define MVNETA_CAUSE_PHY_STATUS_CHANGE BIT(0)
  162. #define MVNETA_CAUSE_LINK_CHANGE BIT(1)
  163. #define MVNETA_CAUSE_PTP BIT(4)
  164. #define MVNETA_CAUSE_INTERNAL_ADDR_ERR BIT(7)
  165. #define MVNETA_CAUSE_RX_OVERRUN BIT(8)
  166. #define MVNETA_CAUSE_RX_CRC_ERROR BIT(9)
  167. #define MVNETA_CAUSE_RX_LARGE_PKT BIT(10)
  168. #define MVNETA_CAUSE_TX_UNDERUN BIT(11)
  169. #define MVNETA_CAUSE_PRBS_ERR BIT(12)
  170. #define MVNETA_CAUSE_PSC_SYNC_CHANGE BIT(13)
  171. #define MVNETA_CAUSE_SERDES_SYNC_ERR BIT(14)
  172. #define MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT 16
  173. #define MVNETA_CAUSE_BMU_ALLOC_ERR_ALL_MASK (0xF << MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT)
  174. #define MVNETA_CAUSE_BMU_ALLOC_ERR_MASK(pool) (1 << (MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT + (pool)))
  175. #define MVNETA_CAUSE_TXQ_ERROR_SHIFT 24
  176. #define MVNETA_CAUSE_TXQ_ERROR_ALL_MASK (0xFF << MVNETA_CAUSE_TXQ_ERROR_SHIFT)
  177. #define MVNETA_CAUSE_TXQ_ERROR_MASK(q) (1 << (MVNETA_CAUSE_TXQ_ERROR_SHIFT + (q)))
  178. #define MVNETA_INTR_ENABLE 0x25b8
  179. #define MVNETA_TXQ_INTR_ENABLE_ALL_MASK 0x0000ff00
  180. #define MVNETA_RXQ_INTR_ENABLE_ALL_MASK 0x000000ff
  181. #define MVNETA_RXQ_CMD 0x2680
  182. #define MVNETA_RXQ_DISABLE_SHIFT 8
  183. #define MVNETA_RXQ_ENABLE_MASK 0x000000ff
  184. #define MVETH_TXQ_TOKEN_COUNT_REG(q) (0x2700 + ((q) << 4))
  185. #define MVETH_TXQ_TOKEN_CFG_REG(q) (0x2704 + ((q) << 4))
  186. #define MVNETA_GMAC_CTRL_0 0x2c00
  187. #define MVNETA_GMAC_MAX_RX_SIZE_SHIFT 2
  188. #define MVNETA_GMAC_MAX_RX_SIZE_MASK 0x7ffc
  189. #define MVNETA_GMAC0_PORT_1000BASE_X BIT(1)
  190. #define MVNETA_GMAC0_PORT_ENABLE BIT(0)
  191. #define MVNETA_GMAC_CTRL_2 0x2c08
  192. #define MVNETA_GMAC2_INBAND_AN_ENABLE BIT(0)
  193. #define MVNETA_GMAC2_PCS_ENABLE BIT(3)
  194. #define MVNETA_GMAC2_PORT_RGMII BIT(4)
  195. #define MVNETA_GMAC2_PORT_RESET BIT(6)
  196. #define MVNETA_GMAC_STATUS 0x2c10
  197. #define MVNETA_GMAC_LINK_UP BIT(0)
  198. #define MVNETA_GMAC_SPEED_1000 BIT(1)
  199. #define MVNETA_GMAC_SPEED_100 BIT(2)
  200. #define MVNETA_GMAC_FULL_DUPLEX BIT(3)
  201. #define MVNETA_GMAC_RX_FLOW_CTRL_ENABLE BIT(4)
  202. #define MVNETA_GMAC_TX_FLOW_CTRL_ENABLE BIT(5)
  203. #define MVNETA_GMAC_RX_FLOW_CTRL_ACTIVE BIT(6)
  204. #define MVNETA_GMAC_TX_FLOW_CTRL_ACTIVE BIT(7)
  205. #define MVNETA_GMAC_AN_COMPLETE BIT(11)
  206. #define MVNETA_GMAC_SYNC_OK BIT(14)
  207. #define MVNETA_GMAC_AUTONEG_CONFIG 0x2c0c
  208. #define MVNETA_GMAC_FORCE_LINK_DOWN BIT(0)
  209. #define MVNETA_GMAC_FORCE_LINK_PASS BIT(1)
  210. #define MVNETA_GMAC_INBAND_AN_ENABLE BIT(2)
  211. #define MVNETA_GMAC_AN_BYPASS_ENABLE BIT(3)
  212. #define MVNETA_GMAC_INBAND_RESTART_AN BIT(4)
  213. #define MVNETA_GMAC_CONFIG_MII_SPEED BIT(5)
  214. #define MVNETA_GMAC_CONFIG_GMII_SPEED BIT(6)
  215. #define MVNETA_GMAC_AN_SPEED_EN BIT(7)
  216. #define MVNETA_GMAC_CONFIG_FLOW_CTRL BIT(8)
  217. #define MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL BIT(9)
  218. #define MVNETA_GMAC_AN_FLOW_CTRL_EN BIT(11)
  219. #define MVNETA_GMAC_CONFIG_FULL_DUPLEX BIT(12)
  220. #define MVNETA_GMAC_AN_DUPLEX_EN BIT(13)
  221. #define MVNETA_GMAC_CTRL_4 0x2c90
  222. #define MVNETA_GMAC4_SHORT_PREAMBLE_ENABLE BIT(1)
  223. #define MVNETA_MIB_COUNTERS_BASE 0x3000
  224. #define MVNETA_MIB_LATE_COLLISION 0x7c
  225. #define MVNETA_DA_FILT_SPEC_MCAST 0x3400
  226. #define MVNETA_DA_FILT_OTH_MCAST 0x3500
  227. #define MVNETA_DA_FILT_UCAST_BASE 0x3600
  228. #define MVNETA_TXQ_BASE_ADDR_REG(q) (0x3c00 + ((q) << 2))
  229. #define MVNETA_TXQ_SIZE_REG(q) (0x3c20 + ((q) << 2))
  230. #define MVNETA_TXQ_SENT_THRESH_ALL_MASK 0x3fff0000
  231. #define MVNETA_TXQ_SENT_THRESH_MASK(coal) ((coal) << 16)
  232. #define MVNETA_TXQ_UPDATE_REG(q) (0x3c60 + ((q) << 2))
  233. #define MVNETA_TXQ_DEC_SENT_SHIFT 16
  234. #define MVNETA_TXQ_DEC_SENT_MASK 0xff
  235. #define MVNETA_TXQ_STATUS_REG(q) (0x3c40 + ((q) << 2))
  236. #define MVNETA_TXQ_SENT_DESC_SHIFT 16
  237. #define MVNETA_TXQ_SENT_DESC_MASK 0x3fff0000
  238. #define MVNETA_PORT_TX_RESET 0x3cf0
  239. #define MVNETA_PORT_TX_DMA_RESET BIT(0)
  240. #define MVNETA_TXQ_CMD1_REG 0x3e00
  241. #define MVNETA_TXQ_CMD1_BW_LIM_SEL_V1 BIT(3)
  242. #define MVNETA_TXQ_CMD1_BW_LIM_EN BIT(0)
  243. #define MVNETA_REFILL_NUM_CLK_REG 0x3e08
  244. #define MVNETA_REFILL_MAX_NUM_CLK 0x0000ffff
  245. #define MVNETA_TX_MTU 0x3e0c
  246. #define MVNETA_TX_TOKEN_SIZE 0x3e14
  247. #define MVNETA_TX_TOKEN_SIZE_MAX 0xffffffff
  248. #define MVNETA_TXQ_BUCKET_REFILL_REG(q) (0x3e20 + ((q) << 2))
  249. #define MVNETA_TXQ_BUCKET_REFILL_PERIOD_MASK 0x3ff00000
  250. #define MVNETA_TXQ_BUCKET_REFILL_PERIOD_SHIFT 20
  251. #define MVNETA_TXQ_BUCKET_REFILL_VALUE_MAX 0x0007ffff
  252. #define MVNETA_TXQ_TOKEN_SIZE_REG(q) (0x3e40 + ((q) << 2))
  253. #define MVNETA_TXQ_TOKEN_SIZE_MAX 0x7fffffff
  254. /* The values of the bucket refill base period and refill period are taken from
  255. * the reference manual, and adds up to a base resolution of 10Kbps. This allows
  256. * to cover all rate-limit values from 10Kbps up to 5Gbps
  257. */
  258. /* Base period for the rate limit algorithm */
  259. #define MVNETA_TXQ_BUCKET_REFILL_BASE_PERIOD_NS 100
  260. /* Number of Base Period to wait between each bucket refill */
  261. #define MVNETA_TXQ_BUCKET_REFILL_PERIOD 1000
  262. /* The base resolution for rate limiting, in bps. Any max_rate value should be
  263. * a multiple of that value.
  264. */
  265. #define MVNETA_TXQ_RATE_LIMIT_RESOLUTION (NSEC_PER_SEC / \
  266. (MVNETA_TXQ_BUCKET_REFILL_BASE_PERIOD_NS * \
  267. MVNETA_TXQ_BUCKET_REFILL_PERIOD))
  268. #define MVNETA_LPI_CTRL_0 0x2cc0
  269. #define MVNETA_LPI_CTRL_0_TS (0xff << 8)
  270. #define MVNETA_LPI_CTRL_1 0x2cc4
  271. #define MVNETA_LPI_CTRL_1_REQUEST_ENABLE BIT(0)
  272. #define MVNETA_LPI_CTRL_1_REQUEST_FORCE BIT(1)
  273. #define MVNETA_LPI_CTRL_1_MANUAL_MODE BIT(2)
  274. #define MVNETA_LPI_CTRL_1_TW (0xfff << 4)
  275. #define MVNETA_LPI_CTRL_2 0x2cc8
  276. #define MVNETA_LPI_STATUS 0x2ccc
  277. #define MVNETA_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff
  278. /* Descriptor ring Macros */
  279. #define MVNETA_QUEUE_NEXT_DESC(q, index) \
  280. (((index) < (q)->last_desc) ? ((index) + 1) : 0)
  281. /* Various constants */
  282. /* Coalescing */
  283. #define MVNETA_TXDONE_COAL_PKTS 0 /* interrupt per packet */
  284. #define MVNETA_RX_COAL_PKTS 32
  285. #define MVNETA_RX_COAL_USEC 100
  286. /* The two bytes Marvell header. Either contains a special value used
  287. * by Marvell switches when a specific hardware mode is enabled (not
  288. * supported by this driver) or is filled automatically by zeroes on
  289. * the RX side. Those two bytes being at the front of the Ethernet
  290. * header, they allow to have the IP header aligned on a 4 bytes
  291. * boundary automatically: the hardware skips those two bytes on its
  292. * own.
  293. */
  294. #define MVNETA_MH_SIZE 2
  295. #define MVNETA_VLAN_TAG_LEN 4
  296. #define MVNETA_TX_CSUM_DEF_SIZE 1600
  297. #define MVNETA_TX_CSUM_MAX_SIZE 9800
  298. #define MVNETA_ACC_MODE_EXT1 1
  299. #define MVNETA_ACC_MODE_EXT2 2
  300. #define MVNETA_MAX_DECODE_WIN 6
  301. /* Timeout constants */
  302. #define MVNETA_TX_DISABLE_TIMEOUT_MSEC 1000
  303. #define MVNETA_RX_DISABLE_TIMEOUT_MSEC 1000
  304. #define MVNETA_TX_FIFO_EMPTY_TIMEOUT 10000
  305. #define MVNETA_TX_MTU_MAX 0x3ffff
  306. /* The RSS lookup table actually has 256 entries but we do not use
  307. * them yet
  308. */
  309. #define MVNETA_RSS_LU_TABLE_SIZE 1
  310. /* Max number of Rx descriptors */
  311. #define MVNETA_MAX_RXD 512
  312. /* Max number of Tx descriptors */
  313. #define MVNETA_MAX_TXD 1024
  314. /* Max number of allowed TCP segments for software TSO */
  315. #define MVNETA_MAX_TSO_SEGS 100
  316. #define MVNETA_MAX_SKB_DESCS (MVNETA_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
  317. /* The size of a TSO header page */
  318. #define MVNETA_TSO_PAGE_SIZE (2 * PAGE_SIZE)
  319. /* Number of TSO headers per page. This should be a power of 2 */
  320. #define MVNETA_TSO_PER_PAGE (MVNETA_TSO_PAGE_SIZE / TSO_HEADER_SIZE)
  321. /* Maximum number of TSO header pages */
  322. #define MVNETA_MAX_TSO_PAGES (MVNETA_MAX_TXD / MVNETA_TSO_PER_PAGE)
  323. /* descriptor aligned size */
  324. #define MVNETA_DESC_ALIGNED_SIZE 32
  325. /* Number of bytes to be taken into account by HW when putting incoming data
  326. * to the buffers. It is needed in case NET_SKB_PAD exceeds maximum packet
  327. * offset supported in MVNETA_RXQ_CONFIG_REG(q) registers.
  328. */
  329. #define MVNETA_RX_PKT_OFFSET_CORRECTION 64
  330. #define MVNETA_RX_PKT_SIZE(mtu) \
  331. ALIGN((mtu) + MVNETA_MH_SIZE + MVNETA_VLAN_TAG_LEN + \
  332. ETH_HLEN + ETH_FCS_LEN, \
  333. cache_line_size())
  334. /* Driver assumes that the last 3 bits are 0 */
  335. #define MVNETA_SKB_HEADROOM ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8)
  336. #define MVNETA_SKB_PAD (SKB_DATA_ALIGN(sizeof(struct skb_shared_info) + \
  337. MVNETA_SKB_HEADROOM))
  338. #define MVNETA_MAX_RX_BUF_SIZE (PAGE_SIZE - MVNETA_SKB_PAD)
  339. #define MVNETA_RX_GET_BM_POOL_ID(rxd) \
  340. (((rxd)->status & MVNETA_RXD_BM_POOL_MASK) >> MVNETA_RXD_BM_POOL_SHIFT)
  341. enum {
  342. ETHTOOL_STAT_EEE_WAKEUP,
  343. ETHTOOL_STAT_SKB_ALLOC_ERR,
  344. ETHTOOL_STAT_REFILL_ERR,
  345. ETHTOOL_XDP_REDIRECT,
  346. ETHTOOL_XDP_PASS,
  347. ETHTOOL_XDP_DROP,
  348. ETHTOOL_XDP_TX,
  349. ETHTOOL_XDP_TX_ERR,
  350. ETHTOOL_XDP_XMIT,
  351. ETHTOOL_XDP_XMIT_ERR,
  352. ETHTOOL_MAX_STATS,
  353. };
  354. struct mvneta_statistic {
  355. unsigned short offset;
  356. unsigned short type;
  357. const char name[ETH_GSTRING_LEN];
  358. };
  359. #define T_REG_32 32
  360. #define T_REG_64 64
  361. #define T_SW 1
  362. #define MVNETA_XDP_PASS 0
  363. #define MVNETA_XDP_DROPPED BIT(0)
  364. #define MVNETA_XDP_TX BIT(1)
  365. #define MVNETA_XDP_REDIR BIT(2)
  366. static const struct mvneta_statistic mvneta_statistics[] = {
  367. { 0x3000, T_REG_64, "good_octets_received", },
  368. { 0x3010, T_REG_32, "good_frames_received", },
  369. { 0x3008, T_REG_32, "bad_octets_received", },
  370. { 0x3014, T_REG_32, "bad_frames_received", },
  371. { 0x3018, T_REG_32, "broadcast_frames_received", },
  372. { 0x301c, T_REG_32, "multicast_frames_received", },
  373. { 0x3050, T_REG_32, "unrec_mac_control_received", },
  374. { 0x3058, T_REG_32, "good_fc_received", },
  375. { 0x305c, T_REG_32, "bad_fc_received", },
  376. { 0x3060, T_REG_32, "undersize_received", },
  377. { 0x3064, T_REG_32, "fragments_received", },
  378. { 0x3068, T_REG_32, "oversize_received", },
  379. { 0x306c, T_REG_32, "jabber_received", },
  380. { 0x3070, T_REG_32, "mac_receive_error", },
  381. { 0x3074, T_REG_32, "bad_crc_event", },
  382. { 0x3078, T_REG_32, "collision", },
  383. { 0x307c, T_REG_32, "late_collision", },
  384. { 0x2484, T_REG_32, "rx_discard", },
  385. { 0x2488, T_REG_32, "rx_overrun", },
  386. { 0x3020, T_REG_32, "frames_64_octets", },
  387. { 0x3024, T_REG_32, "frames_65_to_127_octets", },
  388. { 0x3028, T_REG_32, "frames_128_to_255_octets", },
  389. { 0x302c, T_REG_32, "frames_256_to_511_octets", },
  390. { 0x3030, T_REG_32, "frames_512_to_1023_octets", },
  391. { 0x3034, T_REG_32, "frames_1024_to_max_octets", },
  392. { 0x3038, T_REG_64, "good_octets_sent", },
  393. { 0x3040, T_REG_32, "good_frames_sent", },
  394. { 0x3044, T_REG_32, "excessive_collision", },
  395. { 0x3048, T_REG_32, "multicast_frames_sent", },
  396. { 0x304c, T_REG_32, "broadcast_frames_sent", },
  397. { 0x3054, T_REG_32, "fc_sent", },
  398. { 0x300c, T_REG_32, "internal_mac_transmit_err", },
  399. { ETHTOOL_STAT_EEE_WAKEUP, T_SW, "eee_wakeup_errors", },
  400. { ETHTOOL_STAT_SKB_ALLOC_ERR, T_SW, "skb_alloc_errors", },
  401. { ETHTOOL_STAT_REFILL_ERR, T_SW, "refill_errors", },
  402. { ETHTOOL_XDP_REDIRECT, T_SW, "rx_xdp_redirect", },
  403. { ETHTOOL_XDP_PASS, T_SW, "rx_xdp_pass", },
  404. { ETHTOOL_XDP_DROP, T_SW, "rx_xdp_drop", },
  405. { ETHTOOL_XDP_TX, T_SW, "rx_xdp_tx", },
  406. { ETHTOOL_XDP_TX_ERR, T_SW, "rx_xdp_tx_errors", },
  407. { ETHTOOL_XDP_XMIT, T_SW, "tx_xdp_xmit", },
  408. { ETHTOOL_XDP_XMIT_ERR, T_SW, "tx_xdp_xmit_errors", },
  409. };
  410. struct mvneta_stats {
  411. u64 rx_packets;
  412. u64 rx_bytes;
  413. u64 tx_packets;
  414. u64 tx_bytes;
  415. /* xdp */
  416. u64 xdp_redirect;
  417. u64 xdp_pass;
  418. u64 xdp_drop;
  419. u64 xdp_xmit;
  420. u64 xdp_xmit_err;
  421. u64 xdp_tx;
  422. u64 xdp_tx_err;
  423. };
  424. struct mvneta_ethtool_stats {
  425. struct mvneta_stats ps;
  426. u64 skb_alloc_error;
  427. u64 refill_error;
  428. };
  429. struct mvneta_pcpu_stats {
  430. struct u64_stats_sync syncp;
  431. struct mvneta_ethtool_stats es;
  432. u64 rx_dropped;
  433. u64 rx_errors;
  434. };
  435. struct mvneta_pcpu_port {
  436. /* Pointer to the shared port */
  437. struct mvneta_port *pp;
  438. /* Pointer to the CPU-local NAPI struct */
  439. struct napi_struct napi;
  440. /* Cause of the previous interrupt */
  441. u32 cause_rx_tx;
  442. };
  443. enum {
  444. __MVNETA_DOWN,
  445. };
  446. struct mvneta_port {
  447. u8 id;
  448. struct mvneta_pcpu_port __percpu *ports;
  449. struct mvneta_pcpu_stats __percpu *stats;
  450. unsigned long state;
  451. int pkt_size;
  452. void __iomem *base;
  453. struct mvneta_rx_queue *rxqs;
  454. struct mvneta_tx_queue *txqs;
  455. struct net_device *dev;
  456. struct hlist_node node_online;
  457. struct hlist_node node_dead;
  458. int rxq_def;
  459. /* Protect the access to the percpu interrupt registers,
  460. * ensuring that the configuration remains coherent.
  461. */
  462. spinlock_t lock;
  463. bool is_stopped;
  464. u32 cause_rx_tx;
  465. struct napi_struct napi;
  466. struct bpf_prog *xdp_prog;
  467. /* Core clock */
  468. struct clk *clk;
  469. /* AXI clock */
  470. struct clk *clk_bus;
  471. u8 mcast_count[256];
  472. u16 tx_ring_size;
  473. u16 rx_ring_size;
  474. phy_interface_t phy_interface;
  475. struct device_node *dn;
  476. unsigned int tx_csum_limit;
  477. struct phylink *phylink;
  478. struct phylink_config phylink_config;
  479. struct phylink_pcs phylink_pcs;
  480. struct phy *comphy;
  481. struct mvneta_bm *bm_priv;
  482. struct mvneta_bm_pool *pool_long;
  483. struct mvneta_bm_pool *pool_short;
  484. int bm_win_id;
  485. u64 ethtool_stats[ARRAY_SIZE(mvneta_statistics)];
  486. u32 indir[MVNETA_RSS_LU_TABLE_SIZE];
  487. /* Flags for special SoC configurations */
  488. bool neta_armada3700;
  489. bool neta_ac5;
  490. u16 rx_offset_correction;
  491. const struct mbus_dram_target_info *dram_target_info;
  492. };
  493. /* The mvneta_tx_desc and mvneta_rx_desc structures describe the
  494. * layout of the transmit and reception DMA descriptors, and their
  495. * layout is therefore defined by the hardware design
  496. */
  497. #define MVNETA_TX_L3_OFF_SHIFT 0
  498. #define MVNETA_TX_IP_HLEN_SHIFT 8
  499. #define MVNETA_TX_L4_UDP BIT(16)
  500. #define MVNETA_TX_L3_IP6 BIT(17)
  501. #define MVNETA_TXD_IP_CSUM BIT(18)
  502. #define MVNETA_TXD_Z_PAD BIT(19)
  503. #define MVNETA_TXD_L_DESC BIT(20)
  504. #define MVNETA_TXD_F_DESC BIT(21)
  505. #define MVNETA_TXD_FLZ_DESC (MVNETA_TXD_Z_PAD | \
  506. MVNETA_TXD_L_DESC | \
  507. MVNETA_TXD_F_DESC)
  508. #define MVNETA_TX_L4_CSUM_FULL BIT(30)
  509. #define MVNETA_TX_L4_CSUM_NOT BIT(31)
  510. #define MVNETA_RXD_ERR_CRC 0x0
  511. #define MVNETA_RXD_BM_POOL_SHIFT 13
  512. #define MVNETA_RXD_BM_POOL_MASK (BIT(13) | BIT(14))
  513. #define MVNETA_RXD_ERR_SUMMARY BIT(16)
  514. #define MVNETA_RXD_ERR_OVERRUN BIT(17)
  515. #define MVNETA_RXD_ERR_LEN BIT(18)
  516. #define MVNETA_RXD_ERR_RESOURCE (BIT(17) | BIT(18))
  517. #define MVNETA_RXD_ERR_CODE_MASK (BIT(17) | BIT(18))
  518. #define MVNETA_RXD_L3_IP4 BIT(25)
  519. #define MVNETA_RXD_LAST_DESC BIT(26)
  520. #define MVNETA_RXD_FIRST_DESC BIT(27)
  521. #define MVNETA_RXD_FIRST_LAST_DESC (MVNETA_RXD_FIRST_DESC | \
  522. MVNETA_RXD_LAST_DESC)
  523. #define MVNETA_RXD_L4_CSUM_OK BIT(30)
  524. #if defined(__LITTLE_ENDIAN)
  525. struct mvneta_tx_desc {
  526. u32 command; /* Options used by HW for packet transmitting.*/
  527. u16 reserved1; /* csum_l4 (for future use) */
  528. u16 data_size; /* Data size of transmitted packet in bytes */
  529. u32 buf_phys_addr; /* Physical addr of transmitted buffer */
  530. u32 reserved2; /* hw_cmd - (for future use, PMT) */
  531. u32 reserved3[4]; /* Reserved - (for future use) */
  532. };
  533. struct mvneta_rx_desc {
  534. u32 status; /* Info about received packet */
  535. u16 reserved1; /* pnc_info - (for future use, PnC) */
  536. u16 data_size; /* Size of received packet in bytes */
  537. u32 buf_phys_addr; /* Physical address of the buffer */
  538. u32 reserved2; /* pnc_flow_id (for future use, PnC) */
  539. u32 buf_cookie; /* cookie for access to RX buffer in rx path */
  540. u16 reserved3; /* prefetch_cmd, for future use */
  541. u16 reserved4; /* csum_l4 - (for future use, PnC) */
  542. u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
  543. u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
  544. };
  545. #else
  546. struct mvneta_tx_desc {
  547. u16 data_size; /* Data size of transmitted packet in bytes */
  548. u16 reserved1; /* csum_l4 (for future use) */
  549. u32 command; /* Options used by HW for packet transmitting.*/
  550. u32 reserved2; /* hw_cmd - (for future use, PMT) */
  551. u32 buf_phys_addr; /* Physical addr of transmitted buffer */
  552. u32 reserved3[4]; /* Reserved - (for future use) */
  553. };
  554. struct mvneta_rx_desc {
  555. u16 data_size; /* Size of received packet in bytes */
  556. u16 reserved1; /* pnc_info - (for future use, PnC) */
  557. u32 status; /* Info about received packet */
  558. u32 reserved2; /* pnc_flow_id (for future use, PnC) */
  559. u32 buf_phys_addr; /* Physical address of the buffer */
  560. u16 reserved4; /* csum_l4 - (for future use, PnC) */
  561. u16 reserved3; /* prefetch_cmd, for future use */
  562. u32 buf_cookie; /* cookie for access to RX buffer in rx path */
  563. u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
  564. u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
  565. };
  566. #endif
  567. enum mvneta_tx_buf_type {
  568. MVNETA_TYPE_TSO,
  569. MVNETA_TYPE_SKB,
  570. MVNETA_TYPE_XDP_TX,
  571. MVNETA_TYPE_XDP_NDO,
  572. };
  573. struct mvneta_tx_buf {
  574. enum mvneta_tx_buf_type type;
  575. union {
  576. struct xdp_frame *xdpf;
  577. struct sk_buff *skb;
  578. };
  579. };
  580. struct mvneta_tx_queue {
  581. /* Number of this TX queue, in the range 0-7 */
  582. u8 id;
  583. /* Number of TX DMA descriptors in the descriptor ring */
  584. int size;
  585. /* Number of currently used TX DMA descriptor in the
  586. * descriptor ring
  587. */
  588. int count;
  589. int pending;
  590. int tx_stop_threshold;
  591. int tx_wake_threshold;
  592. /* Array of transmitted buffers */
  593. struct mvneta_tx_buf *buf;
  594. /* Index of last TX DMA descriptor that was inserted */
  595. int txq_put_index;
  596. /* Index of the TX DMA descriptor to be cleaned up */
  597. int txq_get_index;
  598. u32 done_pkts_coal;
  599. /* Virtual address of the TX DMA descriptors array */
  600. struct mvneta_tx_desc *descs;
  601. /* DMA address of the TX DMA descriptors array */
  602. dma_addr_t descs_phys;
  603. /* Index of the last TX DMA descriptor */
  604. int last_desc;
  605. /* Index of the next TX DMA descriptor to process */
  606. int next_desc_to_proc;
  607. /* DMA buffers for TSO headers */
  608. char *tso_hdrs[MVNETA_MAX_TSO_PAGES];
  609. /* DMA address of TSO headers */
  610. dma_addr_t tso_hdrs_phys[MVNETA_MAX_TSO_PAGES];
  611. /* Affinity mask for CPUs*/
  612. cpumask_t affinity_mask;
  613. };
  614. struct mvneta_rx_queue {
  615. /* rx queue number, in the range 0-7 */
  616. u8 id;
  617. /* num of rx descriptors in the rx descriptor ring */
  618. int size;
  619. u32 pkts_coal;
  620. u32 time_coal;
  621. /* page_pool */
  622. struct page_pool *page_pool;
  623. struct xdp_rxq_info xdp_rxq;
  624. /* Virtual address of the RX buffer */
  625. void **buf_virt_addr;
  626. /* Virtual address of the RX DMA descriptors array */
  627. struct mvneta_rx_desc *descs;
  628. /* DMA address of the RX DMA descriptors array */
  629. dma_addr_t descs_phys;
  630. /* Index of the last RX DMA descriptor */
  631. int last_desc;
  632. /* Index of the next RX DMA descriptor to process */
  633. int next_desc_to_proc;
  634. /* Index of first RX DMA descriptor to refill */
  635. int first_to_refill;
  636. u32 refill_num;
  637. };
  638. static enum cpuhp_state online_hpstate;
  639. /* The hardware supports eight (8) rx queues, but we are only allowing
  640. * the first one to be used. Therefore, let's just allocate one queue.
  641. */
  642. static int rxq_number = 8;
  643. static int txq_number = 8;
  644. static int rxq_def;
  645. static int rx_copybreak __read_mostly = 256;
  646. /* HW BM need that each port be identify by a unique ID */
  647. static int global_port_id;
  648. #define MVNETA_DRIVER_NAME "mvneta"
  649. #define MVNETA_DRIVER_VERSION "1.0"
  650. /* Utility/helper methods */
  651. /* Write helper method */
  652. static void mvreg_write(struct mvneta_port *pp, u32 offset, u32 data)
  653. {
  654. writel(data, pp->base + offset);
  655. }
  656. /* Read helper method */
  657. static u32 mvreg_read(struct mvneta_port *pp, u32 offset)
  658. {
  659. return readl(pp->base + offset);
  660. }
  661. /* Increment txq get counter */
  662. static void mvneta_txq_inc_get(struct mvneta_tx_queue *txq)
  663. {
  664. txq->txq_get_index++;
  665. if (txq->txq_get_index == txq->size)
  666. txq->txq_get_index = 0;
  667. }
  668. /* Increment txq put counter */
  669. static void mvneta_txq_inc_put(struct mvneta_tx_queue *txq)
  670. {
  671. txq->txq_put_index++;
  672. if (txq->txq_put_index == txq->size)
  673. txq->txq_put_index = 0;
  674. }
  675. /* Clear all MIB counters */
  676. static void mvneta_mib_counters_clear(struct mvneta_port *pp)
  677. {
  678. int i;
  679. /* Perform dummy reads from MIB counters */
  680. for (i = 0; i < MVNETA_MIB_LATE_COLLISION; i += 4)
  681. mvreg_read(pp, (MVNETA_MIB_COUNTERS_BASE + i));
  682. mvreg_read(pp, MVNETA_RX_DISCARD_FRAME_COUNT);
  683. mvreg_read(pp, MVNETA_OVERRUN_FRAME_COUNT);
  684. }
  685. /* Get System Network Statistics */
  686. static void
  687. mvneta_get_stats64(struct net_device *dev,
  688. struct rtnl_link_stats64 *stats)
  689. {
  690. struct mvneta_port *pp = netdev_priv(dev);
  691. unsigned int start;
  692. int cpu;
  693. for_each_possible_cpu(cpu) {
  694. struct mvneta_pcpu_stats *cpu_stats;
  695. u64 rx_packets;
  696. u64 rx_bytes;
  697. u64 rx_dropped;
  698. u64 rx_errors;
  699. u64 tx_packets;
  700. u64 tx_bytes;
  701. cpu_stats = per_cpu_ptr(pp->stats, cpu);
  702. do {
  703. start = u64_stats_fetch_begin(&cpu_stats->syncp);
  704. rx_packets = cpu_stats->es.ps.rx_packets;
  705. rx_bytes = cpu_stats->es.ps.rx_bytes;
  706. rx_dropped = cpu_stats->rx_dropped;
  707. rx_errors = cpu_stats->rx_errors;
  708. tx_packets = cpu_stats->es.ps.tx_packets;
  709. tx_bytes = cpu_stats->es.ps.tx_bytes;
  710. } while (u64_stats_fetch_retry(&cpu_stats->syncp, start));
  711. stats->rx_packets += rx_packets;
  712. stats->rx_bytes += rx_bytes;
  713. stats->rx_dropped += rx_dropped;
  714. stats->rx_errors += rx_errors;
  715. stats->tx_packets += tx_packets;
  716. stats->tx_bytes += tx_bytes;
  717. }
  718. stats->tx_dropped = dev->stats.tx_dropped;
  719. }
  720. /* Rx descriptors helper methods */
  721. /* Checks whether the RX descriptor having this status is both the first
  722. * and the last descriptor for the RX packet. Each RX packet is currently
  723. * received through a single RX descriptor, so not having each RX
  724. * descriptor with its first and last bits set is an error
  725. */
  726. static int mvneta_rxq_desc_is_first_last(u32 status)
  727. {
  728. return (status & MVNETA_RXD_FIRST_LAST_DESC) ==
  729. MVNETA_RXD_FIRST_LAST_DESC;
  730. }
  731. /* Add number of descriptors ready to receive new packets */
  732. static void mvneta_rxq_non_occup_desc_add(struct mvneta_port *pp,
  733. struct mvneta_rx_queue *rxq,
  734. int ndescs)
  735. {
  736. /* Only MVNETA_RXQ_ADD_NON_OCCUPIED_MAX (255) descriptors can
  737. * be added at once
  738. */
  739. while (ndescs > MVNETA_RXQ_ADD_NON_OCCUPIED_MAX) {
  740. mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
  741. (MVNETA_RXQ_ADD_NON_OCCUPIED_MAX <<
  742. MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
  743. ndescs -= MVNETA_RXQ_ADD_NON_OCCUPIED_MAX;
  744. }
  745. mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
  746. (ndescs << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
  747. }
  748. /* Get number of RX descriptors occupied by received packets */
  749. static int mvneta_rxq_busy_desc_num_get(struct mvneta_port *pp,
  750. struct mvneta_rx_queue *rxq)
  751. {
  752. u32 val;
  753. val = mvreg_read(pp, MVNETA_RXQ_STATUS_REG(rxq->id));
  754. return val & MVNETA_RXQ_OCCUPIED_ALL_MASK;
  755. }
  756. /* Update num of rx desc called upon return from rx path or
  757. * from mvneta_rxq_drop_pkts().
  758. */
  759. static void mvneta_rxq_desc_num_update(struct mvneta_port *pp,
  760. struct mvneta_rx_queue *rxq,
  761. int rx_done, int rx_filled)
  762. {
  763. u32 val;
  764. if ((rx_done <= 0xff) && (rx_filled <= 0xff)) {
  765. val = rx_done |
  766. (rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT);
  767. mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
  768. return;
  769. }
  770. /* Only 255 descriptors can be added at once */
  771. while ((rx_done > 0) || (rx_filled > 0)) {
  772. if (rx_done <= 0xff) {
  773. val = rx_done;
  774. rx_done = 0;
  775. } else {
  776. val = 0xff;
  777. rx_done -= 0xff;
  778. }
  779. if (rx_filled <= 0xff) {
  780. val |= rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
  781. rx_filled = 0;
  782. } else {
  783. val |= 0xff << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
  784. rx_filled -= 0xff;
  785. }
  786. mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
  787. }
  788. }
  789. /* Get pointer to next RX descriptor to be processed by SW */
  790. static struct mvneta_rx_desc *
  791. mvneta_rxq_next_desc_get(struct mvneta_rx_queue *rxq)
  792. {
  793. int rx_desc = rxq->next_desc_to_proc;
  794. rxq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(rxq, rx_desc);
  795. prefetch(rxq->descs + rxq->next_desc_to_proc);
  796. return rxq->descs + rx_desc;
  797. }
  798. /* Change maximum receive size of the port. */
  799. static void mvneta_max_rx_size_set(struct mvneta_port *pp, int max_rx_size)
  800. {
  801. u32 val;
  802. val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
  803. val &= ~MVNETA_GMAC_MAX_RX_SIZE_MASK;
  804. val |= ((max_rx_size - MVNETA_MH_SIZE) / 2) <<
  805. MVNETA_GMAC_MAX_RX_SIZE_SHIFT;
  806. mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
  807. }
  808. /* Set rx queue offset */
  809. static void mvneta_rxq_offset_set(struct mvneta_port *pp,
  810. struct mvneta_rx_queue *rxq,
  811. int offset)
  812. {
  813. u32 val;
  814. val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
  815. val &= ~MVNETA_RXQ_PKT_OFFSET_ALL_MASK;
  816. /* Offset is in */
  817. val |= MVNETA_RXQ_PKT_OFFSET_MASK(offset >> 3);
  818. mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
  819. }
  820. /* Tx descriptors helper methods */
  821. /* Update HW with number of TX descriptors to be sent */
  822. static void mvneta_txq_pend_desc_add(struct mvneta_port *pp,
  823. struct mvneta_tx_queue *txq,
  824. int pend_desc)
  825. {
  826. u32 val;
  827. pend_desc += txq->pending;
  828. /* Only 255 Tx descriptors can be added at once */
  829. do {
  830. val = min(pend_desc, 255);
  831. mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
  832. pend_desc -= val;
  833. } while (pend_desc > 0);
  834. txq->pending = 0;
  835. }
  836. /* Get pointer to next TX descriptor to be processed (send) by HW */
  837. static struct mvneta_tx_desc *
  838. mvneta_txq_next_desc_get(struct mvneta_tx_queue *txq)
  839. {
  840. int tx_desc = txq->next_desc_to_proc;
  841. txq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(txq, tx_desc);
  842. return txq->descs + tx_desc;
  843. }
  844. /* Release the last allocated TX descriptor. Useful to handle DMA
  845. * mapping failures in the TX path.
  846. */
  847. static void mvneta_txq_desc_put(struct mvneta_tx_queue *txq)
  848. {
  849. if (txq->next_desc_to_proc == 0)
  850. txq->next_desc_to_proc = txq->last_desc - 1;
  851. else
  852. txq->next_desc_to_proc--;
  853. }
  854. /* Set rxq buf size */
  855. static void mvneta_rxq_buf_size_set(struct mvneta_port *pp,
  856. struct mvneta_rx_queue *rxq,
  857. int buf_size)
  858. {
  859. u32 val;
  860. val = mvreg_read(pp, MVNETA_RXQ_SIZE_REG(rxq->id));
  861. val &= ~MVNETA_RXQ_BUF_SIZE_MASK;
  862. val |= ((buf_size >> 3) << MVNETA_RXQ_BUF_SIZE_SHIFT);
  863. mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), val);
  864. }
  865. /* Disable buffer management (BM) */
  866. static void mvneta_rxq_bm_disable(struct mvneta_port *pp,
  867. struct mvneta_rx_queue *rxq)
  868. {
  869. u32 val;
  870. val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
  871. val &= ~MVNETA_RXQ_HW_BUF_ALLOC;
  872. mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
  873. }
  874. /* Enable buffer management (BM) */
  875. static void mvneta_rxq_bm_enable(struct mvneta_port *pp,
  876. struct mvneta_rx_queue *rxq)
  877. {
  878. u32 val;
  879. val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
  880. val |= MVNETA_RXQ_HW_BUF_ALLOC;
  881. mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
  882. }
  883. /* Notify HW about port's assignment of pool for bigger packets */
  884. static void mvneta_rxq_long_pool_set(struct mvneta_port *pp,
  885. struct mvneta_rx_queue *rxq)
  886. {
  887. u32 val;
  888. val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
  889. val &= ~MVNETA_RXQ_LONG_POOL_ID_MASK;
  890. val |= (pp->pool_long->id << MVNETA_RXQ_LONG_POOL_ID_SHIFT);
  891. mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
  892. }
  893. /* Notify HW about port's assignment of pool for smaller packets */
  894. static void mvneta_rxq_short_pool_set(struct mvneta_port *pp,
  895. struct mvneta_rx_queue *rxq)
  896. {
  897. u32 val;
  898. val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
  899. val &= ~MVNETA_RXQ_SHORT_POOL_ID_MASK;
  900. val |= (pp->pool_short->id << MVNETA_RXQ_SHORT_POOL_ID_SHIFT);
  901. mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
  902. }
  903. /* Set port's receive buffer size for assigned BM pool */
  904. static inline void mvneta_bm_pool_bufsize_set(struct mvneta_port *pp,
  905. int buf_size,
  906. u8 pool_id)
  907. {
  908. u32 val;
  909. if (!IS_ALIGNED(buf_size, 8)) {
  910. dev_warn(pp->dev->dev.parent,
  911. "illegal buf_size value %d, round to %d\n",
  912. buf_size, ALIGN(buf_size, 8));
  913. buf_size = ALIGN(buf_size, 8);
  914. }
  915. val = mvreg_read(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id));
  916. val |= buf_size & MVNETA_PORT_POOL_BUFFER_SZ_MASK;
  917. mvreg_write(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id), val);
  918. }
  919. /* Configure MBUS window in order to enable access BM internal SRAM */
  920. static int mvneta_mbus_io_win_set(struct mvneta_port *pp, u32 base, u32 wsize,
  921. u8 target, u8 attr)
  922. {
  923. u32 win_enable, win_protect;
  924. int i;
  925. win_enable = mvreg_read(pp, MVNETA_BASE_ADDR_ENABLE);
  926. if (pp->bm_win_id < 0) {
  927. /* Find first not occupied window */
  928. for (i = 0; i < MVNETA_MAX_DECODE_WIN; i++) {
  929. if (win_enable & (1 << i)) {
  930. pp->bm_win_id = i;
  931. break;
  932. }
  933. }
  934. if (i == MVNETA_MAX_DECODE_WIN)
  935. return -ENOMEM;
  936. } else {
  937. i = pp->bm_win_id;
  938. }
  939. mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
  940. mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
  941. if (i < 4)
  942. mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
  943. mvreg_write(pp, MVNETA_WIN_BASE(i), (base & 0xffff0000) |
  944. (attr << 8) | target);
  945. mvreg_write(pp, MVNETA_WIN_SIZE(i), (wsize - 1) & 0xffff0000);
  946. win_protect = mvreg_read(pp, MVNETA_ACCESS_PROTECT_ENABLE);
  947. win_protect |= 3 << (2 * i);
  948. mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect);
  949. win_enable &= ~(1 << i);
  950. mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
  951. return 0;
  952. }
  953. static int mvneta_bm_port_mbus_init(struct mvneta_port *pp)
  954. {
  955. u32 wsize;
  956. u8 target, attr;
  957. int err;
  958. /* Get BM window information */
  959. err = mvebu_mbus_get_io_win_info(pp->bm_priv->bppi_phys_addr, &wsize,
  960. &target, &attr);
  961. if (err < 0)
  962. return err;
  963. pp->bm_win_id = -1;
  964. /* Open NETA -> BM window */
  965. err = mvneta_mbus_io_win_set(pp, pp->bm_priv->bppi_phys_addr, wsize,
  966. target, attr);
  967. if (err < 0) {
  968. netdev_info(pp->dev, "fail to configure mbus window to BM\n");
  969. return err;
  970. }
  971. return 0;
  972. }
  973. /* Assign and initialize pools for port. In case of fail
  974. * buffer manager will remain disabled for current port.
  975. */
  976. static int mvneta_bm_port_init(struct platform_device *pdev,
  977. struct mvneta_port *pp)
  978. {
  979. struct device_node *dn = pdev->dev.of_node;
  980. u32 long_pool_id, short_pool_id;
  981. if (!pp->neta_armada3700) {
  982. int ret;
  983. ret = mvneta_bm_port_mbus_init(pp);
  984. if (ret)
  985. return ret;
  986. }
  987. if (of_property_read_u32(dn, "bm,pool-long", &long_pool_id)) {
  988. netdev_info(pp->dev, "missing long pool id\n");
  989. return -EINVAL;
  990. }
  991. /* Create port's long pool depending on mtu */
  992. pp->pool_long = mvneta_bm_pool_use(pp->bm_priv, long_pool_id,
  993. MVNETA_BM_LONG, pp->id,
  994. MVNETA_RX_PKT_SIZE(pp->dev->mtu));
  995. if (!pp->pool_long) {
  996. netdev_info(pp->dev, "fail to obtain long pool for port\n");
  997. return -ENOMEM;
  998. }
  999. pp->pool_long->port_map |= 1 << pp->id;
  1000. mvneta_bm_pool_bufsize_set(pp, pp->pool_long->buf_size,
  1001. pp->pool_long->id);
  1002. /* If short pool id is not defined, assume using single pool */
  1003. if (of_property_read_u32(dn, "bm,pool-short", &short_pool_id))
  1004. short_pool_id = long_pool_id;
  1005. /* Create port's short pool */
  1006. pp->pool_short = mvneta_bm_pool_use(pp->bm_priv, short_pool_id,
  1007. MVNETA_BM_SHORT, pp->id,
  1008. MVNETA_BM_SHORT_PKT_SIZE);
  1009. if (!pp->pool_short) {
  1010. netdev_info(pp->dev, "fail to obtain short pool for port\n");
  1011. mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
  1012. return -ENOMEM;
  1013. }
  1014. if (short_pool_id != long_pool_id) {
  1015. pp->pool_short->port_map |= 1 << pp->id;
  1016. mvneta_bm_pool_bufsize_set(pp, pp->pool_short->buf_size,
  1017. pp->pool_short->id);
  1018. }
  1019. return 0;
  1020. }
  1021. /* Update settings of a pool for bigger packets */
  1022. static void mvneta_bm_update_mtu(struct mvneta_port *pp, int mtu)
  1023. {
  1024. struct mvneta_bm_pool *bm_pool = pp->pool_long;
  1025. struct hwbm_pool *hwbm_pool = &bm_pool->hwbm_pool;
  1026. int num;
  1027. /* Release all buffers from long pool */
  1028. mvneta_bm_bufs_free(pp->bm_priv, bm_pool, 1 << pp->id);
  1029. if (hwbm_pool->buf_num) {
  1030. WARN(1, "cannot free all buffers in pool %d\n",
  1031. bm_pool->id);
  1032. goto bm_mtu_err;
  1033. }
  1034. bm_pool->pkt_size = MVNETA_RX_PKT_SIZE(mtu);
  1035. bm_pool->buf_size = MVNETA_RX_BUF_SIZE(bm_pool->pkt_size);
  1036. hwbm_pool->frag_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
  1037. SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(bm_pool->pkt_size));
  1038. /* Fill entire long pool */
  1039. num = hwbm_pool_add(hwbm_pool, hwbm_pool->size);
  1040. if (num != hwbm_pool->size) {
  1041. WARN(1, "pool %d: %d of %d allocated\n",
  1042. bm_pool->id, num, hwbm_pool->size);
  1043. goto bm_mtu_err;
  1044. }
  1045. mvneta_bm_pool_bufsize_set(pp, bm_pool->buf_size, bm_pool->id);
  1046. return;
  1047. bm_mtu_err:
  1048. mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
  1049. mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short, 1 << pp->id);
  1050. pp->bm_priv = NULL;
  1051. pp->rx_offset_correction = MVNETA_SKB_HEADROOM;
  1052. mvreg_write(pp, MVNETA_ACC_MODE, MVNETA_ACC_MODE_EXT1);
  1053. netdev_info(pp->dev, "fail to update MTU, fall back to software BM\n");
  1054. }
  1055. /* Start the Ethernet port RX and TX activity */
  1056. static void mvneta_port_up(struct mvneta_port *pp)
  1057. {
  1058. int queue;
  1059. u32 q_map;
  1060. /* Enable all initialized TXs. */
  1061. q_map = 0;
  1062. for (queue = 0; queue < txq_number; queue++) {
  1063. struct mvneta_tx_queue *txq = &pp->txqs[queue];
  1064. if (txq->descs)
  1065. q_map |= (1 << queue);
  1066. }
  1067. mvreg_write(pp, MVNETA_TXQ_CMD, q_map);
  1068. q_map = 0;
  1069. /* Enable all initialized RXQs. */
  1070. for (queue = 0; queue < rxq_number; queue++) {
  1071. struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
  1072. if (rxq->descs)
  1073. q_map |= (1 << queue);
  1074. }
  1075. mvreg_write(pp, MVNETA_RXQ_CMD, q_map);
  1076. }
  1077. /* Stop the Ethernet port activity */
  1078. static void mvneta_port_down(struct mvneta_port *pp)
  1079. {
  1080. u32 val;
  1081. int count;
  1082. /* Stop Rx port activity. Check port Rx activity. */
  1083. val = mvreg_read(pp, MVNETA_RXQ_CMD) & MVNETA_RXQ_ENABLE_MASK;
  1084. /* Issue stop command for active channels only */
  1085. if (val != 0)
  1086. mvreg_write(pp, MVNETA_RXQ_CMD,
  1087. val << MVNETA_RXQ_DISABLE_SHIFT);
  1088. /* Wait for all Rx activity to terminate. */
  1089. count = 0;
  1090. do {
  1091. if (count++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC) {
  1092. netdev_warn(pp->dev,
  1093. "TIMEOUT for RX stopped ! rx_queue_cmd: 0x%08x\n",
  1094. val);
  1095. break;
  1096. }
  1097. mdelay(1);
  1098. val = mvreg_read(pp, MVNETA_RXQ_CMD);
  1099. } while (val & MVNETA_RXQ_ENABLE_MASK);
  1100. /* Stop Tx port activity. Check port Tx activity. Issue stop
  1101. * command for active channels only
  1102. */
  1103. val = (mvreg_read(pp, MVNETA_TXQ_CMD)) & MVNETA_TXQ_ENABLE_MASK;
  1104. if (val != 0)
  1105. mvreg_write(pp, MVNETA_TXQ_CMD,
  1106. (val << MVNETA_TXQ_DISABLE_SHIFT));
  1107. /* Wait for all Tx activity to terminate. */
  1108. count = 0;
  1109. do {
  1110. if (count++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC) {
  1111. netdev_warn(pp->dev,
  1112. "TIMEOUT for TX stopped status=0x%08x\n",
  1113. val);
  1114. break;
  1115. }
  1116. mdelay(1);
  1117. /* Check TX Command reg that all Txqs are stopped */
  1118. val = mvreg_read(pp, MVNETA_TXQ_CMD);
  1119. } while (val & MVNETA_TXQ_ENABLE_MASK);
  1120. /* Double check to verify that TX FIFO is empty */
  1121. count = 0;
  1122. do {
  1123. if (count++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT) {
  1124. netdev_warn(pp->dev,
  1125. "TX FIFO empty timeout status=0x%08x\n",
  1126. val);
  1127. break;
  1128. }
  1129. mdelay(1);
  1130. val = mvreg_read(pp, MVNETA_PORT_STATUS);
  1131. } while (!(val & MVNETA_TX_FIFO_EMPTY) &&
  1132. (val & MVNETA_TX_IN_PRGRS));
  1133. udelay(200);
  1134. }
  1135. /* Enable the port by setting the port enable bit of the MAC control register */
  1136. static void mvneta_port_enable(struct mvneta_port *pp)
  1137. {
  1138. u32 val;
  1139. /* Enable port */
  1140. val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
  1141. val |= MVNETA_GMAC0_PORT_ENABLE;
  1142. mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
  1143. }
  1144. /* Disable the port and wait for about 200 usec before retuning */
  1145. static void mvneta_port_disable(struct mvneta_port *pp)
  1146. {
  1147. u32 val;
  1148. /* Reset the Enable bit in the Serial Control Register */
  1149. val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
  1150. val &= ~MVNETA_GMAC0_PORT_ENABLE;
  1151. mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
  1152. udelay(200);
  1153. }
  1154. /* Multicast tables methods */
  1155. /* Set all entries in Unicast MAC Table; queue==-1 means reject all */
  1156. static void mvneta_set_ucast_table(struct mvneta_port *pp, int queue)
  1157. {
  1158. int offset;
  1159. u32 val;
  1160. if (queue == -1) {
  1161. val = 0;
  1162. } else {
  1163. val = 0x1 | (queue << 1);
  1164. val |= (val << 24) | (val << 16) | (val << 8);
  1165. }
  1166. for (offset = 0; offset <= 0xc; offset += 4)
  1167. mvreg_write(pp, MVNETA_DA_FILT_UCAST_BASE + offset, val);
  1168. }
  1169. /* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */
  1170. static void mvneta_set_special_mcast_table(struct mvneta_port *pp, int queue)
  1171. {
  1172. int offset;
  1173. u32 val;
  1174. if (queue == -1) {
  1175. val = 0;
  1176. } else {
  1177. val = 0x1 | (queue << 1);
  1178. val |= (val << 24) | (val << 16) | (val << 8);
  1179. }
  1180. for (offset = 0; offset <= 0xfc; offset += 4)
  1181. mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + offset, val);
  1182. }
  1183. /* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */
  1184. static void mvneta_set_other_mcast_table(struct mvneta_port *pp, int queue)
  1185. {
  1186. int offset;
  1187. u32 val;
  1188. if (queue == -1) {
  1189. memset(pp->mcast_count, 0, sizeof(pp->mcast_count));
  1190. val = 0;
  1191. } else {
  1192. memset(pp->mcast_count, 1, sizeof(pp->mcast_count));
  1193. val = 0x1 | (queue << 1);
  1194. val |= (val << 24) | (val << 16) | (val << 8);
  1195. }
  1196. for (offset = 0; offset <= 0xfc; offset += 4)
  1197. mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val);
  1198. }
  1199. static void mvneta_percpu_unmask_interrupt(void *arg)
  1200. {
  1201. struct mvneta_port *pp = arg;
  1202. /* All the queue are unmasked, but actually only the ones
  1203. * mapped to this CPU will be unmasked
  1204. */
  1205. mvreg_write(pp, MVNETA_INTR_NEW_MASK,
  1206. MVNETA_RX_INTR_MASK_ALL |
  1207. MVNETA_TX_INTR_MASK_ALL |
  1208. MVNETA_MISCINTR_INTR_MASK);
  1209. }
  1210. static void mvneta_percpu_mask_interrupt(void *arg)
  1211. {
  1212. struct mvneta_port *pp = arg;
  1213. /* All the queue are masked, but actually only the ones
  1214. * mapped to this CPU will be masked
  1215. */
  1216. mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
  1217. mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
  1218. mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
  1219. }
  1220. static void mvneta_percpu_clear_intr_cause(void *arg)
  1221. {
  1222. struct mvneta_port *pp = arg;
  1223. /* All the queue are cleared, but actually only the ones
  1224. * mapped to this CPU will be cleared
  1225. */
  1226. mvreg_write(pp, MVNETA_INTR_NEW_CAUSE, 0);
  1227. mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
  1228. mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
  1229. }
  1230. /* This method sets defaults to the NETA port:
  1231. * Clears interrupt Cause and Mask registers.
  1232. * Clears all MAC tables.
  1233. * Sets defaults to all registers.
  1234. * Resets RX and TX descriptor rings.
  1235. * Resets PHY.
  1236. * This method can be called after mvneta_port_down() to return the port
  1237. * settings to defaults.
  1238. */
  1239. static void mvneta_defaults_set(struct mvneta_port *pp)
  1240. {
  1241. int cpu;
  1242. int queue;
  1243. u32 val;
  1244. int max_cpu = num_present_cpus();
  1245. /* Clear all Cause registers */
  1246. on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
  1247. /* Mask all interrupts */
  1248. on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
  1249. mvreg_write(pp, MVNETA_INTR_ENABLE, 0);
  1250. /* Enable MBUS Retry bit16 */
  1251. mvreg_write(pp, MVNETA_MBUS_RETRY, 0x20);
  1252. /* Set CPU queue access map. CPUs are assigned to the RX and
  1253. * TX queues modulo their number. If there is only one TX
  1254. * queue then it is assigned to the CPU associated to the
  1255. * default RX queue.
  1256. */
  1257. for_each_present_cpu(cpu) {
  1258. int rxq_map = 0, txq_map = 0;
  1259. int rxq, txq;
  1260. if (!pp->neta_armada3700) {
  1261. for (rxq = 0; rxq < rxq_number; rxq++)
  1262. if ((rxq % max_cpu) == cpu)
  1263. rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
  1264. for (txq = 0; txq < txq_number; txq++)
  1265. if ((txq % max_cpu) == cpu)
  1266. txq_map |= MVNETA_CPU_TXQ_ACCESS(txq);
  1267. /* With only one TX queue we configure a special case
  1268. * which will allow to get all the irq on a single
  1269. * CPU
  1270. */
  1271. if (txq_number == 1)
  1272. txq_map = (cpu == pp->rxq_def) ?
  1273. MVNETA_CPU_TXQ_ACCESS(0) : 0;
  1274. } else {
  1275. txq_map = MVNETA_CPU_TXQ_ACCESS_ALL_MASK;
  1276. rxq_map = MVNETA_CPU_RXQ_ACCESS_ALL_MASK;
  1277. }
  1278. mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
  1279. }
  1280. /* Reset RX and TX DMAs */
  1281. mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
  1282. mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
  1283. /* Disable Legacy WRR, Disable EJP, Release from reset */
  1284. mvreg_write(pp, MVNETA_TXQ_CMD_1, 0);
  1285. for (queue = 0; queue < txq_number; queue++) {
  1286. mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(queue), 0);
  1287. mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(queue), 0);
  1288. }
  1289. mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
  1290. mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
  1291. /* Set Port Acceleration Mode */
  1292. if (pp->bm_priv)
  1293. /* HW buffer management + legacy parser */
  1294. val = MVNETA_ACC_MODE_EXT2;
  1295. else
  1296. /* SW buffer management + legacy parser */
  1297. val = MVNETA_ACC_MODE_EXT1;
  1298. mvreg_write(pp, MVNETA_ACC_MODE, val);
  1299. if (pp->bm_priv)
  1300. mvreg_write(pp, MVNETA_BM_ADDRESS, pp->bm_priv->bppi_phys_addr);
  1301. /* Update val of portCfg register accordingly with all RxQueue types */
  1302. val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
  1303. mvreg_write(pp, MVNETA_PORT_CONFIG, val);
  1304. val = 0;
  1305. mvreg_write(pp, MVNETA_PORT_CONFIG_EXTEND, val);
  1306. mvreg_write(pp, MVNETA_RX_MIN_FRAME_SIZE, 64);
  1307. /* Build PORT_SDMA_CONFIG_REG */
  1308. val = 0;
  1309. /* Default burst size */
  1310. val |= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
  1311. val |= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
  1312. val |= MVNETA_RX_NO_DATA_SWAP | MVNETA_TX_NO_DATA_SWAP;
  1313. #if defined(__BIG_ENDIAN)
  1314. val |= MVNETA_DESC_SWAP;
  1315. #endif
  1316. /* Assign port SDMA configuration */
  1317. mvreg_write(pp, MVNETA_SDMA_CONFIG, val);
  1318. /* Disable PHY polling in hardware, since we're using the
  1319. * kernel phylib to do this.
  1320. */
  1321. val = mvreg_read(pp, MVNETA_UNIT_CONTROL);
  1322. val &= ~MVNETA_PHY_POLLING_ENABLE;
  1323. mvreg_write(pp, MVNETA_UNIT_CONTROL, val);
  1324. mvneta_set_ucast_table(pp, -1);
  1325. mvneta_set_special_mcast_table(pp, -1);
  1326. mvneta_set_other_mcast_table(pp, -1);
  1327. /* Set port interrupt enable register - default enable all */
  1328. mvreg_write(pp, MVNETA_INTR_ENABLE,
  1329. (MVNETA_RXQ_INTR_ENABLE_ALL_MASK
  1330. | MVNETA_TXQ_INTR_ENABLE_ALL_MASK));
  1331. mvneta_mib_counters_clear(pp);
  1332. }
  1333. /* Set max sizes for tx queues */
  1334. static void mvneta_txq_max_tx_size_set(struct mvneta_port *pp, int max_tx_size)
  1335. {
  1336. u32 val, size, mtu;
  1337. int queue;
  1338. mtu = max_tx_size * 8;
  1339. if (mtu > MVNETA_TX_MTU_MAX)
  1340. mtu = MVNETA_TX_MTU_MAX;
  1341. /* Set MTU */
  1342. val = mvreg_read(pp, MVNETA_TX_MTU);
  1343. val &= ~MVNETA_TX_MTU_MAX;
  1344. val |= mtu;
  1345. mvreg_write(pp, MVNETA_TX_MTU, val);
  1346. /* TX token size and all TXQs token size must be larger that MTU */
  1347. val = mvreg_read(pp, MVNETA_TX_TOKEN_SIZE);
  1348. size = val & MVNETA_TX_TOKEN_SIZE_MAX;
  1349. if (size < mtu) {
  1350. size = mtu;
  1351. val &= ~MVNETA_TX_TOKEN_SIZE_MAX;
  1352. val |= size;
  1353. mvreg_write(pp, MVNETA_TX_TOKEN_SIZE, val);
  1354. }
  1355. for (queue = 0; queue < txq_number; queue++) {
  1356. val = mvreg_read(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue));
  1357. size = val & MVNETA_TXQ_TOKEN_SIZE_MAX;
  1358. if (size < mtu) {
  1359. size = mtu;
  1360. val &= ~MVNETA_TXQ_TOKEN_SIZE_MAX;
  1361. val |= size;
  1362. mvreg_write(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue), val);
  1363. }
  1364. }
  1365. }
  1366. /* Set unicast address */
  1367. static void mvneta_set_ucast_addr(struct mvneta_port *pp, u8 last_nibble,
  1368. int queue)
  1369. {
  1370. unsigned int unicast_reg;
  1371. unsigned int tbl_offset;
  1372. unsigned int reg_offset;
  1373. /* Locate the Unicast table entry */
  1374. last_nibble = (0xf & last_nibble);
  1375. /* offset from unicast tbl base */
  1376. tbl_offset = (last_nibble / 4) * 4;
  1377. /* offset within the above reg */
  1378. reg_offset = last_nibble % 4;
  1379. unicast_reg = mvreg_read(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset));
  1380. if (queue == -1) {
  1381. /* Clear accepts frame bit at specified unicast DA tbl entry */
  1382. unicast_reg &= ~(0xff << (8 * reg_offset));
  1383. } else {
  1384. unicast_reg &= ~(0xff << (8 * reg_offset));
  1385. unicast_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
  1386. }
  1387. mvreg_write(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset), unicast_reg);
  1388. }
  1389. /* Set mac address */
  1390. static void mvneta_mac_addr_set(struct mvneta_port *pp,
  1391. const unsigned char *addr, int queue)
  1392. {
  1393. unsigned int mac_h;
  1394. unsigned int mac_l;
  1395. if (queue != -1) {
  1396. mac_l = (addr[4] << 8) | (addr[5]);
  1397. mac_h = (addr[0] << 24) | (addr[1] << 16) |
  1398. (addr[2] << 8) | (addr[3] << 0);
  1399. mvreg_write(pp, MVNETA_MAC_ADDR_LOW, mac_l);
  1400. mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, mac_h);
  1401. }
  1402. /* Accept frames of this address */
  1403. mvneta_set_ucast_addr(pp, addr[5], queue);
  1404. }
  1405. /* Set the number of packets that will be received before RX interrupt
  1406. * will be generated by HW.
  1407. */
  1408. static void mvneta_rx_pkts_coal_set(struct mvneta_port *pp,
  1409. struct mvneta_rx_queue *rxq, u32 value)
  1410. {
  1411. mvreg_write(pp, MVNETA_RXQ_THRESHOLD_REG(rxq->id),
  1412. value | MVNETA_RXQ_NON_OCCUPIED(0));
  1413. }
  1414. /* Set the time delay in usec before RX interrupt will be generated by
  1415. * HW.
  1416. */
  1417. static void mvneta_rx_time_coal_set(struct mvneta_port *pp,
  1418. struct mvneta_rx_queue *rxq, u32 value)
  1419. {
  1420. u32 val;
  1421. unsigned long clk_rate;
  1422. clk_rate = clk_get_rate(pp->clk);
  1423. val = (clk_rate / 1000000) * value;
  1424. mvreg_write(pp, MVNETA_RXQ_TIME_COAL_REG(rxq->id), val);
  1425. }
  1426. /* Set threshold for TX_DONE pkts coalescing */
  1427. static void mvneta_tx_done_pkts_coal_set(struct mvneta_port *pp,
  1428. struct mvneta_tx_queue *txq, u32 value)
  1429. {
  1430. u32 val;
  1431. val = mvreg_read(pp, MVNETA_TXQ_SIZE_REG(txq->id));
  1432. val &= ~MVNETA_TXQ_SENT_THRESH_ALL_MASK;
  1433. val |= MVNETA_TXQ_SENT_THRESH_MASK(value);
  1434. mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), val);
  1435. }
  1436. /* Handle rx descriptor fill by setting buf_cookie and buf_phys_addr */
  1437. static void mvneta_rx_desc_fill(struct mvneta_rx_desc *rx_desc,
  1438. u32 phys_addr, void *virt_addr,
  1439. struct mvneta_rx_queue *rxq)
  1440. {
  1441. int i;
  1442. rx_desc->buf_phys_addr = phys_addr;
  1443. i = rx_desc - rxq->descs;
  1444. rxq->buf_virt_addr[i] = virt_addr;
  1445. }
  1446. /* Decrement sent descriptors counter */
  1447. static void mvneta_txq_sent_desc_dec(struct mvneta_port *pp,
  1448. struct mvneta_tx_queue *txq,
  1449. int sent_desc)
  1450. {
  1451. u32 val;
  1452. /* Only 255 TX descriptors can be updated at once */
  1453. while (sent_desc > 0xff) {
  1454. val = 0xff << MVNETA_TXQ_DEC_SENT_SHIFT;
  1455. mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
  1456. sent_desc = sent_desc - 0xff;
  1457. }
  1458. val = sent_desc << MVNETA_TXQ_DEC_SENT_SHIFT;
  1459. mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
  1460. }
  1461. /* Get number of TX descriptors already sent by HW */
  1462. static int mvneta_txq_sent_desc_num_get(struct mvneta_port *pp,
  1463. struct mvneta_tx_queue *txq)
  1464. {
  1465. u32 val;
  1466. int sent_desc;
  1467. val = mvreg_read(pp, MVNETA_TXQ_STATUS_REG(txq->id));
  1468. sent_desc = (val & MVNETA_TXQ_SENT_DESC_MASK) >>
  1469. MVNETA_TXQ_SENT_DESC_SHIFT;
  1470. return sent_desc;
  1471. }
  1472. /* Get number of sent descriptors and decrement counter.
  1473. * The number of sent descriptors is returned.
  1474. */
  1475. static int mvneta_txq_sent_desc_proc(struct mvneta_port *pp,
  1476. struct mvneta_tx_queue *txq)
  1477. {
  1478. int sent_desc;
  1479. /* Get number of sent descriptors */
  1480. sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
  1481. /* Decrement sent descriptors counter */
  1482. if (sent_desc)
  1483. mvneta_txq_sent_desc_dec(pp, txq, sent_desc);
  1484. return sent_desc;
  1485. }
  1486. /* Set TXQ descriptors fields relevant for CSUM calculation */
  1487. static u32 mvneta_txq_desc_csum(int l3_offs, __be16 l3_proto,
  1488. int ip_hdr_len, int l4_proto)
  1489. {
  1490. u32 command;
  1491. /* Fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
  1492. * G_L4_chk, L4_type; required only for checksum
  1493. * calculation
  1494. */
  1495. command = l3_offs << MVNETA_TX_L3_OFF_SHIFT;
  1496. command |= ip_hdr_len << MVNETA_TX_IP_HLEN_SHIFT;
  1497. if (l3_proto == htons(ETH_P_IP))
  1498. command |= MVNETA_TXD_IP_CSUM;
  1499. else
  1500. command |= MVNETA_TX_L3_IP6;
  1501. if (l4_proto == IPPROTO_TCP)
  1502. command |= MVNETA_TX_L4_CSUM_FULL;
  1503. else if (l4_proto == IPPROTO_UDP)
  1504. command |= MVNETA_TX_L4_UDP | MVNETA_TX_L4_CSUM_FULL;
  1505. else
  1506. command |= MVNETA_TX_L4_CSUM_NOT;
  1507. return command;
  1508. }
  1509. /* Display more error info */
  1510. static void mvneta_rx_error(struct mvneta_port *pp,
  1511. struct mvneta_rx_desc *rx_desc)
  1512. {
  1513. struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
  1514. u32 status = rx_desc->status;
  1515. /* update per-cpu counter */
  1516. u64_stats_update_begin(&stats->syncp);
  1517. stats->rx_errors++;
  1518. u64_stats_update_end(&stats->syncp);
  1519. switch (status & MVNETA_RXD_ERR_CODE_MASK) {
  1520. case MVNETA_RXD_ERR_CRC:
  1521. netdev_err(pp->dev, "bad rx status %08x (crc error), size=%d\n",
  1522. status, rx_desc->data_size);
  1523. break;
  1524. case MVNETA_RXD_ERR_OVERRUN:
  1525. netdev_err(pp->dev, "bad rx status %08x (overrun error), size=%d\n",
  1526. status, rx_desc->data_size);
  1527. break;
  1528. case MVNETA_RXD_ERR_LEN:
  1529. netdev_err(pp->dev, "bad rx status %08x (max frame length error), size=%d\n",
  1530. status, rx_desc->data_size);
  1531. break;
  1532. case MVNETA_RXD_ERR_RESOURCE:
  1533. netdev_err(pp->dev, "bad rx status %08x (resource error), size=%d\n",
  1534. status, rx_desc->data_size);
  1535. break;
  1536. }
  1537. }
  1538. /* Handle RX checksum offload based on the descriptor's status */
  1539. static int mvneta_rx_csum(struct mvneta_port *pp, u32 status)
  1540. {
  1541. if ((pp->dev->features & NETIF_F_RXCSUM) &&
  1542. (status & MVNETA_RXD_L3_IP4) &&
  1543. (status & MVNETA_RXD_L4_CSUM_OK))
  1544. return CHECKSUM_UNNECESSARY;
  1545. return CHECKSUM_NONE;
  1546. }
  1547. /* Return tx queue pointer (find last set bit) according to <cause> returned
  1548. * form tx_done reg. <cause> must not be null. The return value is always a
  1549. * valid queue for matching the first one found in <cause>.
  1550. */
  1551. static struct mvneta_tx_queue *mvneta_tx_done_policy(struct mvneta_port *pp,
  1552. u32 cause)
  1553. {
  1554. int queue = fls(cause) - 1;
  1555. return &pp->txqs[queue];
  1556. }
  1557. /* Free tx queue skbuffs */
  1558. static void mvneta_txq_bufs_free(struct mvneta_port *pp,
  1559. struct mvneta_tx_queue *txq, int num,
  1560. struct netdev_queue *nq, bool napi)
  1561. {
  1562. unsigned int bytes_compl = 0, pkts_compl = 0;
  1563. struct xdp_frame_bulk bq;
  1564. int i;
  1565. xdp_frame_bulk_init(&bq);
  1566. rcu_read_lock(); /* need for xdp_return_frame_bulk */
  1567. for (i = 0; i < num; i++) {
  1568. struct mvneta_tx_buf *buf = &txq->buf[txq->txq_get_index];
  1569. struct mvneta_tx_desc *tx_desc = txq->descs +
  1570. txq->txq_get_index;
  1571. mvneta_txq_inc_get(txq);
  1572. if (buf->type == MVNETA_TYPE_XDP_NDO ||
  1573. buf->type == MVNETA_TYPE_SKB)
  1574. dma_unmap_single(pp->dev->dev.parent,
  1575. tx_desc->buf_phys_addr,
  1576. tx_desc->data_size, DMA_TO_DEVICE);
  1577. if ((buf->type == MVNETA_TYPE_TSO ||
  1578. buf->type == MVNETA_TYPE_SKB) && buf->skb) {
  1579. bytes_compl += buf->skb->len;
  1580. pkts_compl++;
  1581. dev_kfree_skb_any(buf->skb);
  1582. } else if ((buf->type == MVNETA_TYPE_XDP_TX ||
  1583. buf->type == MVNETA_TYPE_XDP_NDO) && buf->xdpf) {
  1584. if (napi && buf->type == MVNETA_TYPE_XDP_TX)
  1585. xdp_return_frame_rx_napi(buf->xdpf);
  1586. else
  1587. xdp_return_frame_bulk(buf->xdpf, &bq);
  1588. }
  1589. }
  1590. xdp_flush_frame_bulk(&bq);
  1591. rcu_read_unlock();
  1592. netdev_tx_completed_queue(nq, pkts_compl, bytes_compl);
  1593. }
  1594. /* Handle end of transmission */
  1595. static void mvneta_txq_done(struct mvneta_port *pp,
  1596. struct mvneta_tx_queue *txq)
  1597. {
  1598. struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
  1599. int tx_done;
  1600. tx_done = mvneta_txq_sent_desc_proc(pp, txq);
  1601. if (!tx_done)
  1602. return;
  1603. mvneta_txq_bufs_free(pp, txq, tx_done, nq, true);
  1604. txq->count -= tx_done;
  1605. if (netif_tx_queue_stopped(nq)) {
  1606. if (txq->count <= txq->tx_wake_threshold)
  1607. netif_tx_wake_queue(nq);
  1608. }
  1609. }
  1610. /* Refill processing for SW buffer management */
  1611. /* Allocate page per descriptor */
  1612. static int mvneta_rx_refill(struct mvneta_port *pp,
  1613. struct mvneta_rx_desc *rx_desc,
  1614. struct mvneta_rx_queue *rxq,
  1615. gfp_t gfp_mask)
  1616. {
  1617. dma_addr_t phys_addr;
  1618. struct page *page;
  1619. page = page_pool_alloc_pages(rxq->page_pool,
  1620. gfp_mask | __GFP_NOWARN);
  1621. if (!page)
  1622. return -ENOMEM;
  1623. phys_addr = page_pool_get_dma_addr(page) + pp->rx_offset_correction;
  1624. mvneta_rx_desc_fill(rx_desc, phys_addr, page, rxq);
  1625. return 0;
  1626. }
  1627. /* Handle tx checksum */
  1628. static u32 mvneta_skb_tx_csum(struct sk_buff *skb)
  1629. {
  1630. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1631. int ip_hdr_len = 0;
  1632. __be16 l3_proto = vlan_get_protocol(skb);
  1633. u8 l4_proto;
  1634. if (l3_proto == htons(ETH_P_IP)) {
  1635. struct iphdr *ip4h = ip_hdr(skb);
  1636. /* Calculate IPv4 checksum and L4 checksum */
  1637. ip_hdr_len = ip4h->ihl;
  1638. l4_proto = ip4h->protocol;
  1639. } else if (l3_proto == htons(ETH_P_IPV6)) {
  1640. struct ipv6hdr *ip6h = ipv6_hdr(skb);
  1641. /* Read l4_protocol from one of IPv6 extra headers */
  1642. if (skb_network_header_len(skb) > 0)
  1643. ip_hdr_len = (skb_network_header_len(skb) >> 2);
  1644. l4_proto = ip6h->nexthdr;
  1645. } else
  1646. return MVNETA_TX_L4_CSUM_NOT;
  1647. return mvneta_txq_desc_csum(skb_network_offset(skb),
  1648. l3_proto, ip_hdr_len, l4_proto);
  1649. }
  1650. return MVNETA_TX_L4_CSUM_NOT;
  1651. }
  1652. /* Drop packets received by the RXQ and free buffers */
  1653. static void mvneta_rxq_drop_pkts(struct mvneta_port *pp,
  1654. struct mvneta_rx_queue *rxq)
  1655. {
  1656. int rx_done, i;
  1657. rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
  1658. if (rx_done)
  1659. mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
  1660. if (pp->bm_priv) {
  1661. for (i = 0; i < rx_done; i++) {
  1662. struct mvneta_rx_desc *rx_desc =
  1663. mvneta_rxq_next_desc_get(rxq);
  1664. u8 pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc);
  1665. struct mvneta_bm_pool *bm_pool;
  1666. bm_pool = &pp->bm_priv->bm_pools[pool_id];
  1667. /* Return dropped buffer to the pool */
  1668. mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
  1669. rx_desc->buf_phys_addr);
  1670. }
  1671. return;
  1672. }
  1673. for (i = 0; i < rxq->size; i++) {
  1674. struct mvneta_rx_desc *rx_desc = rxq->descs + i;
  1675. void *data = rxq->buf_virt_addr[i];
  1676. if (!data || !(rx_desc->buf_phys_addr))
  1677. continue;
  1678. page_pool_put_full_page(rxq->page_pool, data, false);
  1679. }
  1680. if (xdp_rxq_info_is_reg(&rxq->xdp_rxq))
  1681. xdp_rxq_info_unreg(&rxq->xdp_rxq);
  1682. page_pool_destroy(rxq->page_pool);
  1683. rxq->page_pool = NULL;
  1684. }
  1685. static void
  1686. mvneta_update_stats(struct mvneta_port *pp,
  1687. struct mvneta_stats *ps)
  1688. {
  1689. struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
  1690. u64_stats_update_begin(&stats->syncp);
  1691. stats->es.ps.rx_packets += ps->rx_packets;
  1692. stats->es.ps.rx_bytes += ps->rx_bytes;
  1693. /* xdp */
  1694. stats->es.ps.xdp_redirect += ps->xdp_redirect;
  1695. stats->es.ps.xdp_pass += ps->xdp_pass;
  1696. stats->es.ps.xdp_drop += ps->xdp_drop;
  1697. u64_stats_update_end(&stats->syncp);
  1698. }
  1699. static inline
  1700. int mvneta_rx_refill_queue(struct mvneta_port *pp, struct mvneta_rx_queue *rxq)
  1701. {
  1702. struct mvneta_rx_desc *rx_desc;
  1703. int curr_desc = rxq->first_to_refill;
  1704. int i;
  1705. for (i = 0; (i < rxq->refill_num) && (i < 64); i++) {
  1706. rx_desc = rxq->descs + curr_desc;
  1707. if (!(rx_desc->buf_phys_addr)) {
  1708. if (mvneta_rx_refill(pp, rx_desc, rxq, GFP_ATOMIC)) {
  1709. struct mvneta_pcpu_stats *stats;
  1710. pr_err("Can't refill queue %d. Done %d from %d\n",
  1711. rxq->id, i, rxq->refill_num);
  1712. stats = this_cpu_ptr(pp->stats);
  1713. u64_stats_update_begin(&stats->syncp);
  1714. stats->es.refill_error++;
  1715. u64_stats_update_end(&stats->syncp);
  1716. break;
  1717. }
  1718. }
  1719. curr_desc = MVNETA_QUEUE_NEXT_DESC(rxq, curr_desc);
  1720. }
  1721. rxq->refill_num -= i;
  1722. rxq->first_to_refill = curr_desc;
  1723. return i;
  1724. }
  1725. static void
  1726. mvneta_xdp_put_buff(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
  1727. struct xdp_buff *xdp, int sync_len)
  1728. {
  1729. struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
  1730. int i;
  1731. if (likely(!xdp_buff_has_frags(xdp)))
  1732. goto out;
  1733. for (i = 0; i < sinfo->nr_frags; i++)
  1734. page_pool_put_full_page(rxq->page_pool,
  1735. skb_frag_page(&sinfo->frags[i]), true);
  1736. out:
  1737. page_pool_put_page(rxq->page_pool, virt_to_head_page(xdp->data),
  1738. sync_len, true);
  1739. }
  1740. static int
  1741. mvneta_xdp_submit_frame(struct mvneta_port *pp, struct mvneta_tx_queue *txq,
  1742. struct xdp_frame *xdpf, int *nxmit_byte, bool dma_map)
  1743. {
  1744. struct skb_shared_info *sinfo = xdp_get_shared_info_from_frame(xdpf);
  1745. struct device *dev = pp->dev->dev.parent;
  1746. struct mvneta_tx_desc *tx_desc;
  1747. int i, num_frames = 1;
  1748. struct page *page;
  1749. if (unlikely(xdp_frame_has_frags(xdpf)))
  1750. num_frames += sinfo->nr_frags;
  1751. if (txq->count + num_frames >= txq->size)
  1752. return MVNETA_XDP_DROPPED;
  1753. for (i = 0; i < num_frames; i++) {
  1754. struct mvneta_tx_buf *buf = &txq->buf[txq->txq_put_index];
  1755. skb_frag_t *frag = NULL;
  1756. int len = xdpf->len;
  1757. dma_addr_t dma_addr;
  1758. if (unlikely(i)) { /* paged area */
  1759. frag = &sinfo->frags[i - 1];
  1760. len = skb_frag_size(frag);
  1761. }
  1762. tx_desc = mvneta_txq_next_desc_get(txq);
  1763. if (dma_map) {
  1764. /* ndo_xdp_xmit */
  1765. void *data;
  1766. data = unlikely(frag) ? skb_frag_address(frag)
  1767. : xdpf->data;
  1768. dma_addr = dma_map_single(dev, data, len,
  1769. DMA_TO_DEVICE);
  1770. if (dma_mapping_error(dev, dma_addr)) {
  1771. mvneta_txq_desc_put(txq);
  1772. goto unmap;
  1773. }
  1774. buf->type = MVNETA_TYPE_XDP_NDO;
  1775. } else {
  1776. page = unlikely(frag) ? skb_frag_page(frag)
  1777. : virt_to_page(xdpf->data);
  1778. dma_addr = page_pool_get_dma_addr(page);
  1779. if (unlikely(frag))
  1780. dma_addr += skb_frag_off(frag);
  1781. else
  1782. dma_addr += sizeof(*xdpf) + xdpf->headroom;
  1783. dma_sync_single_for_device(dev, dma_addr, len,
  1784. DMA_BIDIRECTIONAL);
  1785. buf->type = MVNETA_TYPE_XDP_TX;
  1786. }
  1787. buf->xdpf = unlikely(i) ? NULL : xdpf;
  1788. tx_desc->command = unlikely(i) ? 0 : MVNETA_TXD_F_DESC;
  1789. tx_desc->buf_phys_addr = dma_addr;
  1790. tx_desc->data_size = len;
  1791. *nxmit_byte += len;
  1792. mvneta_txq_inc_put(txq);
  1793. }
  1794. /*last descriptor */
  1795. tx_desc->command |= MVNETA_TXD_L_DESC | MVNETA_TXD_Z_PAD;
  1796. txq->pending += num_frames;
  1797. txq->count += num_frames;
  1798. return MVNETA_XDP_TX;
  1799. unmap:
  1800. for (i--; i >= 0; i--) {
  1801. mvneta_txq_desc_put(txq);
  1802. tx_desc = txq->descs + txq->next_desc_to_proc;
  1803. dma_unmap_single(dev, tx_desc->buf_phys_addr,
  1804. tx_desc->data_size,
  1805. DMA_TO_DEVICE);
  1806. }
  1807. return MVNETA_XDP_DROPPED;
  1808. }
  1809. static int
  1810. mvneta_xdp_xmit_back(struct mvneta_port *pp, struct xdp_buff *xdp)
  1811. {
  1812. struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
  1813. struct mvneta_tx_queue *txq;
  1814. struct netdev_queue *nq;
  1815. int cpu, nxmit_byte = 0;
  1816. struct xdp_frame *xdpf;
  1817. u32 ret;
  1818. xdpf = xdp_convert_buff_to_frame(xdp);
  1819. if (unlikely(!xdpf))
  1820. return MVNETA_XDP_DROPPED;
  1821. cpu = smp_processor_id();
  1822. txq = &pp->txqs[cpu % txq_number];
  1823. nq = netdev_get_tx_queue(pp->dev, txq->id);
  1824. __netif_tx_lock(nq, cpu);
  1825. ret = mvneta_xdp_submit_frame(pp, txq, xdpf, &nxmit_byte, false);
  1826. if (ret == MVNETA_XDP_TX) {
  1827. u64_stats_update_begin(&stats->syncp);
  1828. stats->es.ps.tx_bytes += nxmit_byte;
  1829. stats->es.ps.tx_packets++;
  1830. stats->es.ps.xdp_tx++;
  1831. u64_stats_update_end(&stats->syncp);
  1832. mvneta_txq_pend_desc_add(pp, txq, 0);
  1833. } else {
  1834. u64_stats_update_begin(&stats->syncp);
  1835. stats->es.ps.xdp_tx_err++;
  1836. u64_stats_update_end(&stats->syncp);
  1837. }
  1838. __netif_tx_unlock(nq);
  1839. return ret;
  1840. }
  1841. static int
  1842. mvneta_xdp_xmit(struct net_device *dev, int num_frame,
  1843. struct xdp_frame **frames, u32 flags)
  1844. {
  1845. struct mvneta_port *pp = netdev_priv(dev);
  1846. struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
  1847. int i, nxmit_byte = 0, nxmit = 0;
  1848. int cpu = smp_processor_id();
  1849. struct mvneta_tx_queue *txq;
  1850. struct netdev_queue *nq;
  1851. u32 ret;
  1852. if (unlikely(test_bit(__MVNETA_DOWN, &pp->state)))
  1853. return -ENETDOWN;
  1854. if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
  1855. return -EINVAL;
  1856. txq = &pp->txqs[cpu % txq_number];
  1857. nq = netdev_get_tx_queue(pp->dev, txq->id);
  1858. __netif_tx_lock(nq, cpu);
  1859. for (i = 0; i < num_frame; i++) {
  1860. ret = mvneta_xdp_submit_frame(pp, txq, frames[i], &nxmit_byte,
  1861. true);
  1862. if (ret != MVNETA_XDP_TX)
  1863. break;
  1864. nxmit++;
  1865. }
  1866. if (unlikely(flags & XDP_XMIT_FLUSH))
  1867. mvneta_txq_pend_desc_add(pp, txq, 0);
  1868. __netif_tx_unlock(nq);
  1869. u64_stats_update_begin(&stats->syncp);
  1870. stats->es.ps.tx_bytes += nxmit_byte;
  1871. stats->es.ps.tx_packets += nxmit;
  1872. stats->es.ps.xdp_xmit += nxmit;
  1873. stats->es.ps.xdp_xmit_err += num_frame - nxmit;
  1874. u64_stats_update_end(&stats->syncp);
  1875. return nxmit;
  1876. }
  1877. static int
  1878. mvneta_run_xdp(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
  1879. struct bpf_prog *prog, struct xdp_buff *xdp,
  1880. u32 frame_sz, struct mvneta_stats *stats)
  1881. {
  1882. unsigned int len, data_len, sync;
  1883. u32 ret, act;
  1884. len = xdp->data_end - xdp->data_hard_start - pp->rx_offset_correction;
  1885. data_len = xdp->data_end - xdp->data;
  1886. act = bpf_prog_run_xdp(prog, xdp);
  1887. /* Due xdp_adjust_tail: DMA sync for_device cover max len CPU touch */
  1888. sync = xdp->data_end - xdp->data_hard_start - pp->rx_offset_correction;
  1889. sync = max(sync, len);
  1890. switch (act) {
  1891. case XDP_PASS:
  1892. stats->xdp_pass++;
  1893. return MVNETA_XDP_PASS;
  1894. case XDP_REDIRECT: {
  1895. int err;
  1896. err = xdp_do_redirect(pp->dev, xdp, prog);
  1897. if (unlikely(err)) {
  1898. mvneta_xdp_put_buff(pp, rxq, xdp, sync);
  1899. ret = MVNETA_XDP_DROPPED;
  1900. } else {
  1901. ret = MVNETA_XDP_REDIR;
  1902. stats->xdp_redirect++;
  1903. }
  1904. break;
  1905. }
  1906. case XDP_TX:
  1907. ret = mvneta_xdp_xmit_back(pp, xdp);
  1908. if (ret != MVNETA_XDP_TX)
  1909. mvneta_xdp_put_buff(pp, rxq, xdp, sync);
  1910. break;
  1911. default:
  1912. bpf_warn_invalid_xdp_action(pp->dev, prog, act);
  1913. fallthrough;
  1914. case XDP_ABORTED:
  1915. trace_xdp_exception(pp->dev, prog, act);
  1916. fallthrough;
  1917. case XDP_DROP:
  1918. mvneta_xdp_put_buff(pp, rxq, xdp, sync);
  1919. ret = MVNETA_XDP_DROPPED;
  1920. stats->xdp_drop++;
  1921. break;
  1922. }
  1923. stats->rx_bytes += frame_sz + xdp->data_end - xdp->data - data_len;
  1924. stats->rx_packets++;
  1925. return ret;
  1926. }
  1927. static void
  1928. mvneta_swbm_rx_frame(struct mvneta_port *pp,
  1929. struct mvneta_rx_desc *rx_desc,
  1930. struct mvneta_rx_queue *rxq,
  1931. struct xdp_buff *xdp, int *size,
  1932. struct page *page)
  1933. {
  1934. unsigned char *data = page_address(page);
  1935. int data_len = -MVNETA_MH_SIZE, len;
  1936. struct net_device *dev = pp->dev;
  1937. enum dma_data_direction dma_dir;
  1938. if (*size > MVNETA_MAX_RX_BUF_SIZE) {
  1939. len = MVNETA_MAX_RX_BUF_SIZE;
  1940. data_len += len;
  1941. } else {
  1942. len = *size;
  1943. data_len += len - ETH_FCS_LEN;
  1944. }
  1945. *size = *size - len;
  1946. dma_dir = page_pool_get_dma_dir(rxq->page_pool);
  1947. dma_sync_single_for_cpu(dev->dev.parent,
  1948. rx_desc->buf_phys_addr,
  1949. len, dma_dir);
  1950. rx_desc->buf_phys_addr = 0;
  1951. /* Prefetch header */
  1952. prefetch(data);
  1953. xdp_buff_clear_frags_flag(xdp);
  1954. xdp_prepare_buff(xdp, data, pp->rx_offset_correction + MVNETA_MH_SIZE,
  1955. data_len, true);
  1956. }
  1957. static void
  1958. mvneta_swbm_add_rx_fragment(struct mvneta_port *pp,
  1959. struct mvneta_rx_desc *rx_desc,
  1960. struct mvneta_rx_queue *rxq,
  1961. struct xdp_buff *xdp, int *size,
  1962. struct page *page)
  1963. {
  1964. struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
  1965. struct net_device *dev = pp->dev;
  1966. enum dma_data_direction dma_dir;
  1967. int data_len, len;
  1968. if (*size > MVNETA_MAX_RX_BUF_SIZE) {
  1969. len = MVNETA_MAX_RX_BUF_SIZE;
  1970. data_len = len;
  1971. } else {
  1972. len = *size;
  1973. data_len = len - ETH_FCS_LEN;
  1974. }
  1975. dma_dir = page_pool_get_dma_dir(rxq->page_pool);
  1976. dma_sync_single_for_cpu(dev->dev.parent,
  1977. rx_desc->buf_phys_addr,
  1978. len, dma_dir);
  1979. rx_desc->buf_phys_addr = 0;
  1980. if (!xdp_buff_has_frags(xdp))
  1981. sinfo->nr_frags = 0;
  1982. if (data_len > 0 && sinfo->nr_frags < MAX_SKB_FRAGS) {
  1983. skb_frag_t *frag = &sinfo->frags[sinfo->nr_frags++];
  1984. skb_frag_fill_page_desc(frag, page,
  1985. pp->rx_offset_correction, data_len);
  1986. if (!xdp_buff_has_frags(xdp)) {
  1987. sinfo->xdp_frags_size = *size;
  1988. xdp_buff_set_frags_flag(xdp);
  1989. }
  1990. if (page_is_pfmemalloc(page))
  1991. xdp_buff_set_frag_pfmemalloc(xdp);
  1992. } else {
  1993. page_pool_put_full_page(rxq->page_pool, page, true);
  1994. }
  1995. *size -= len;
  1996. }
  1997. static struct sk_buff *
  1998. mvneta_swbm_build_skb(struct mvneta_port *pp, struct page_pool *pool,
  1999. struct xdp_buff *xdp, u32 desc_status)
  2000. {
  2001. struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
  2002. u32 metasize = xdp->data - xdp->data_meta;
  2003. struct sk_buff *skb;
  2004. u8 num_frags;
  2005. if (unlikely(xdp_buff_has_frags(xdp)))
  2006. num_frags = sinfo->nr_frags;
  2007. skb = build_skb(xdp->data_hard_start, PAGE_SIZE);
  2008. if (!skb)
  2009. return ERR_PTR(-ENOMEM);
  2010. skb_mark_for_recycle(skb);
  2011. skb_reserve(skb, xdp->data - xdp->data_hard_start);
  2012. skb_put(skb, xdp->data_end - xdp->data);
  2013. if (metasize)
  2014. skb_metadata_set(skb, metasize);
  2015. skb->ip_summed = mvneta_rx_csum(pp, desc_status);
  2016. if (unlikely(xdp_buff_has_frags(xdp)))
  2017. xdp_update_skb_frags_info(skb, num_frags, sinfo->xdp_frags_size,
  2018. num_frags * xdp->frame_sz,
  2019. xdp_buff_get_skb_flags(xdp));
  2020. return skb;
  2021. }
  2022. /* Main rx processing when using software buffer management */
  2023. static int mvneta_rx_swbm(struct napi_struct *napi,
  2024. struct mvneta_port *pp, int budget,
  2025. struct mvneta_rx_queue *rxq)
  2026. {
  2027. int rx_proc = 0, rx_todo, refill, size = 0;
  2028. struct net_device *dev = pp->dev;
  2029. struct mvneta_stats ps = {};
  2030. struct bpf_prog *xdp_prog;
  2031. u32 desc_status, frame_sz;
  2032. struct xdp_buff xdp_buf;
  2033. xdp_init_buff(&xdp_buf, PAGE_SIZE, &rxq->xdp_rxq);
  2034. xdp_buf.data_hard_start = NULL;
  2035. /* Get number of received packets */
  2036. rx_todo = mvneta_rxq_busy_desc_num_get(pp, rxq);
  2037. xdp_prog = READ_ONCE(pp->xdp_prog);
  2038. /* Fairness NAPI loop */
  2039. while (rx_proc < budget && rx_proc < rx_todo) {
  2040. struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
  2041. u32 rx_status, index;
  2042. struct sk_buff *skb;
  2043. struct page *page;
  2044. index = rx_desc - rxq->descs;
  2045. page = (struct page *)rxq->buf_virt_addr[index];
  2046. rx_status = rx_desc->status;
  2047. rx_proc++;
  2048. rxq->refill_num++;
  2049. if (rx_status & MVNETA_RXD_FIRST_DESC) {
  2050. /* Check errors only for FIRST descriptor */
  2051. if (rx_status & MVNETA_RXD_ERR_SUMMARY) {
  2052. mvneta_rx_error(pp, rx_desc);
  2053. goto next;
  2054. }
  2055. size = rx_desc->data_size;
  2056. frame_sz = size - ETH_FCS_LEN;
  2057. desc_status = rx_status;
  2058. mvneta_swbm_rx_frame(pp, rx_desc, rxq, &xdp_buf,
  2059. &size, page);
  2060. } else {
  2061. if (unlikely(!xdp_buf.data_hard_start)) {
  2062. rx_desc->buf_phys_addr = 0;
  2063. page_pool_put_full_page(rxq->page_pool, page,
  2064. true);
  2065. goto next;
  2066. }
  2067. mvneta_swbm_add_rx_fragment(pp, rx_desc, rxq, &xdp_buf,
  2068. &size, page);
  2069. } /* Middle or Last descriptor */
  2070. if (!(rx_status & MVNETA_RXD_LAST_DESC))
  2071. /* no last descriptor this time */
  2072. continue;
  2073. if (size) {
  2074. mvneta_xdp_put_buff(pp, rxq, &xdp_buf, -1);
  2075. goto next;
  2076. }
  2077. if (xdp_prog &&
  2078. mvneta_run_xdp(pp, rxq, xdp_prog, &xdp_buf, frame_sz, &ps))
  2079. goto next;
  2080. skb = mvneta_swbm_build_skb(pp, rxq->page_pool, &xdp_buf, desc_status);
  2081. if (IS_ERR(skb)) {
  2082. struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
  2083. mvneta_xdp_put_buff(pp, rxq, &xdp_buf, -1);
  2084. u64_stats_update_begin(&stats->syncp);
  2085. stats->es.skb_alloc_error++;
  2086. stats->rx_dropped++;
  2087. u64_stats_update_end(&stats->syncp);
  2088. goto next;
  2089. }
  2090. ps.rx_bytes += skb->len;
  2091. ps.rx_packets++;
  2092. skb->protocol = eth_type_trans(skb, dev);
  2093. napi_gro_receive(napi, skb);
  2094. next:
  2095. xdp_buf.data_hard_start = NULL;
  2096. }
  2097. if (xdp_buf.data_hard_start)
  2098. mvneta_xdp_put_buff(pp, rxq, &xdp_buf, -1);
  2099. if (ps.xdp_redirect)
  2100. xdp_do_flush();
  2101. if (ps.rx_packets)
  2102. mvneta_update_stats(pp, &ps);
  2103. /* return some buffers to hardware queue, one at a time is too slow */
  2104. refill = mvneta_rx_refill_queue(pp, rxq);
  2105. /* Update rxq management counters */
  2106. mvneta_rxq_desc_num_update(pp, rxq, rx_proc, refill);
  2107. return ps.rx_packets;
  2108. }
  2109. /* Main rx processing when using hardware buffer management */
  2110. static int mvneta_rx_hwbm(struct napi_struct *napi,
  2111. struct mvneta_port *pp, int rx_todo,
  2112. struct mvneta_rx_queue *rxq)
  2113. {
  2114. struct net_device *dev = pp->dev;
  2115. int rx_done;
  2116. u32 rcvd_pkts = 0;
  2117. u32 rcvd_bytes = 0;
  2118. /* Get number of received packets */
  2119. rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
  2120. if (rx_todo > rx_done)
  2121. rx_todo = rx_done;
  2122. rx_done = 0;
  2123. /* Fairness NAPI loop */
  2124. while (rx_done < rx_todo) {
  2125. struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
  2126. struct mvneta_bm_pool *bm_pool = NULL;
  2127. struct sk_buff *skb;
  2128. unsigned char *data;
  2129. dma_addr_t phys_addr;
  2130. u32 rx_status, frag_size;
  2131. int rx_bytes, err;
  2132. u8 pool_id;
  2133. rx_done++;
  2134. rx_status = rx_desc->status;
  2135. rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE);
  2136. data = (u8 *)(uintptr_t)rx_desc->buf_cookie;
  2137. phys_addr = rx_desc->buf_phys_addr;
  2138. pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc);
  2139. bm_pool = &pp->bm_priv->bm_pools[pool_id];
  2140. if (!mvneta_rxq_desc_is_first_last(rx_status) ||
  2141. (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
  2142. err_drop_frame_ret_pool:
  2143. /* Return the buffer to the pool */
  2144. mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
  2145. rx_desc->buf_phys_addr);
  2146. err_drop_frame:
  2147. mvneta_rx_error(pp, rx_desc);
  2148. /* leave the descriptor untouched */
  2149. continue;
  2150. }
  2151. if (rx_bytes <= rx_copybreak) {
  2152. /* better copy a small frame and not unmap the DMA region */
  2153. skb = netdev_alloc_skb_ip_align(dev, rx_bytes);
  2154. if (unlikely(!skb))
  2155. goto err_drop_frame_ret_pool;
  2156. dma_sync_single_range_for_cpu(&pp->bm_priv->pdev->dev,
  2157. rx_desc->buf_phys_addr,
  2158. MVNETA_MH_SIZE + NET_SKB_PAD,
  2159. rx_bytes,
  2160. DMA_FROM_DEVICE);
  2161. skb_put_data(skb, data + MVNETA_MH_SIZE + NET_SKB_PAD,
  2162. rx_bytes);
  2163. skb->protocol = eth_type_trans(skb, dev);
  2164. skb->ip_summed = mvneta_rx_csum(pp, rx_status);
  2165. napi_gro_receive(napi, skb);
  2166. rcvd_pkts++;
  2167. rcvd_bytes += rx_bytes;
  2168. /* Return the buffer to the pool */
  2169. mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
  2170. rx_desc->buf_phys_addr);
  2171. /* leave the descriptor and buffer untouched */
  2172. continue;
  2173. }
  2174. /* Refill processing */
  2175. err = hwbm_pool_refill(&bm_pool->hwbm_pool, GFP_ATOMIC);
  2176. if (err) {
  2177. struct mvneta_pcpu_stats *stats;
  2178. netdev_err(dev, "Linux processing - Can't refill\n");
  2179. stats = this_cpu_ptr(pp->stats);
  2180. u64_stats_update_begin(&stats->syncp);
  2181. stats->es.refill_error++;
  2182. u64_stats_update_end(&stats->syncp);
  2183. goto err_drop_frame_ret_pool;
  2184. }
  2185. frag_size = bm_pool->hwbm_pool.frag_size;
  2186. skb = build_skb(data, frag_size > PAGE_SIZE ? 0 : frag_size);
  2187. /* After refill old buffer has to be unmapped regardless
  2188. * the skb is successfully built or not.
  2189. */
  2190. dma_unmap_single(&pp->bm_priv->pdev->dev, phys_addr,
  2191. bm_pool->buf_size, DMA_FROM_DEVICE);
  2192. if (!skb)
  2193. goto err_drop_frame;
  2194. rcvd_pkts++;
  2195. rcvd_bytes += rx_bytes;
  2196. /* Linux processing */
  2197. skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD);
  2198. skb_put(skb, rx_bytes);
  2199. skb->protocol = eth_type_trans(skb, dev);
  2200. skb->ip_summed = mvneta_rx_csum(pp, rx_status);
  2201. napi_gro_receive(napi, skb);
  2202. }
  2203. if (rcvd_pkts) {
  2204. struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
  2205. u64_stats_update_begin(&stats->syncp);
  2206. stats->es.ps.rx_packets += rcvd_pkts;
  2207. stats->es.ps.rx_bytes += rcvd_bytes;
  2208. u64_stats_update_end(&stats->syncp);
  2209. }
  2210. /* Update rxq management counters */
  2211. mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
  2212. return rx_done;
  2213. }
  2214. static void mvneta_free_tso_hdrs(struct mvneta_port *pp,
  2215. struct mvneta_tx_queue *txq)
  2216. {
  2217. struct device *dev = pp->dev->dev.parent;
  2218. int i;
  2219. for (i = 0; i < MVNETA_MAX_TSO_PAGES; i++) {
  2220. if (txq->tso_hdrs[i]) {
  2221. dma_free_coherent(dev, MVNETA_TSO_PAGE_SIZE,
  2222. txq->tso_hdrs[i],
  2223. txq->tso_hdrs_phys[i]);
  2224. txq->tso_hdrs[i] = NULL;
  2225. }
  2226. }
  2227. }
  2228. static int mvneta_alloc_tso_hdrs(struct mvneta_port *pp,
  2229. struct mvneta_tx_queue *txq)
  2230. {
  2231. struct device *dev = pp->dev->dev.parent;
  2232. int i, num;
  2233. num = DIV_ROUND_UP(txq->size, MVNETA_TSO_PER_PAGE);
  2234. for (i = 0; i < num; i++) {
  2235. txq->tso_hdrs[i] = dma_alloc_coherent(dev, MVNETA_TSO_PAGE_SIZE,
  2236. &txq->tso_hdrs_phys[i],
  2237. GFP_KERNEL);
  2238. if (!txq->tso_hdrs[i]) {
  2239. mvneta_free_tso_hdrs(pp, txq);
  2240. return -ENOMEM;
  2241. }
  2242. }
  2243. return 0;
  2244. }
  2245. static char *mvneta_get_tso_hdr(struct mvneta_tx_queue *txq, dma_addr_t *dma)
  2246. {
  2247. int index, offset;
  2248. index = txq->txq_put_index / MVNETA_TSO_PER_PAGE;
  2249. offset = (txq->txq_put_index % MVNETA_TSO_PER_PAGE) * TSO_HEADER_SIZE;
  2250. *dma = txq->tso_hdrs_phys[index] + offset;
  2251. return txq->tso_hdrs[index] + offset;
  2252. }
  2253. static void mvneta_tso_put_hdr(struct sk_buff *skb, struct mvneta_tx_queue *txq,
  2254. struct tso_t *tso, int size, bool is_last)
  2255. {
  2256. struct mvneta_tx_buf *buf = &txq->buf[txq->txq_put_index];
  2257. int hdr_len = skb_tcp_all_headers(skb);
  2258. struct mvneta_tx_desc *tx_desc;
  2259. dma_addr_t hdr_phys;
  2260. char *hdr;
  2261. hdr = mvneta_get_tso_hdr(txq, &hdr_phys);
  2262. tso_build_hdr(skb, hdr, tso, size, is_last);
  2263. tx_desc = mvneta_txq_next_desc_get(txq);
  2264. tx_desc->data_size = hdr_len;
  2265. tx_desc->command = mvneta_skb_tx_csum(skb);
  2266. tx_desc->command |= MVNETA_TXD_F_DESC;
  2267. tx_desc->buf_phys_addr = hdr_phys;
  2268. buf->type = MVNETA_TYPE_TSO;
  2269. buf->skb = NULL;
  2270. mvneta_txq_inc_put(txq);
  2271. }
  2272. static inline int
  2273. mvneta_tso_put_data(struct net_device *dev, struct mvneta_tx_queue *txq,
  2274. struct sk_buff *skb, char *data, int size,
  2275. bool last_tcp, bool is_last)
  2276. {
  2277. struct mvneta_tx_buf *buf = &txq->buf[txq->txq_put_index];
  2278. struct mvneta_tx_desc *tx_desc;
  2279. tx_desc = mvneta_txq_next_desc_get(txq);
  2280. tx_desc->data_size = size;
  2281. tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, data,
  2282. size, DMA_TO_DEVICE);
  2283. if (unlikely(dma_mapping_error(dev->dev.parent,
  2284. tx_desc->buf_phys_addr))) {
  2285. mvneta_txq_desc_put(txq);
  2286. return -ENOMEM;
  2287. }
  2288. tx_desc->command = 0;
  2289. buf->type = MVNETA_TYPE_SKB;
  2290. buf->skb = NULL;
  2291. if (last_tcp) {
  2292. /* last descriptor in the TCP packet */
  2293. tx_desc->command = MVNETA_TXD_L_DESC;
  2294. /* last descriptor in SKB */
  2295. if (is_last)
  2296. buf->skb = skb;
  2297. }
  2298. mvneta_txq_inc_put(txq);
  2299. return 0;
  2300. }
  2301. static void mvneta_release_descs(struct mvneta_port *pp,
  2302. struct mvneta_tx_queue *txq,
  2303. int first, int num)
  2304. {
  2305. int desc_idx, i;
  2306. desc_idx = first + num;
  2307. if (desc_idx >= txq->size)
  2308. desc_idx -= txq->size;
  2309. for (i = num; i >= 0; i--) {
  2310. struct mvneta_tx_desc *tx_desc = txq->descs + desc_idx;
  2311. struct mvneta_tx_buf *buf = &txq->buf[desc_idx];
  2312. if (buf->type == MVNETA_TYPE_SKB)
  2313. dma_unmap_single(pp->dev->dev.parent,
  2314. tx_desc->buf_phys_addr,
  2315. tx_desc->data_size,
  2316. DMA_TO_DEVICE);
  2317. mvneta_txq_desc_put(txq);
  2318. if (desc_idx == 0)
  2319. desc_idx = txq->size;
  2320. desc_idx -= 1;
  2321. }
  2322. }
  2323. static int mvneta_tx_tso(struct sk_buff *skb, struct net_device *dev,
  2324. struct mvneta_tx_queue *txq)
  2325. {
  2326. int hdr_len, total_len, data_left;
  2327. int first_desc, desc_count = 0;
  2328. struct mvneta_port *pp = netdev_priv(dev);
  2329. struct tso_t tso;
  2330. /* Count needed descriptors */
  2331. if ((txq->count + tso_count_descs(skb)) >= txq->size)
  2332. return 0;
  2333. if (skb_headlen(skb) < skb_tcp_all_headers(skb)) {
  2334. pr_info("*** Is this even possible?\n");
  2335. return 0;
  2336. }
  2337. first_desc = txq->txq_put_index;
  2338. /* Initialize the TSO handler, and prepare the first payload */
  2339. hdr_len = tso_start(skb, &tso);
  2340. total_len = skb->len - hdr_len;
  2341. while (total_len > 0) {
  2342. data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
  2343. total_len -= data_left;
  2344. desc_count++;
  2345. /* prepare packet headers: MAC + IP + TCP */
  2346. mvneta_tso_put_hdr(skb, txq, &tso, data_left, total_len == 0);
  2347. while (data_left > 0) {
  2348. int size;
  2349. desc_count++;
  2350. size = min_t(int, tso.size, data_left);
  2351. if (mvneta_tso_put_data(dev, txq, skb,
  2352. tso.data, size,
  2353. size == data_left,
  2354. total_len == 0))
  2355. goto err_release;
  2356. data_left -= size;
  2357. tso_build_data(skb, &tso, size);
  2358. }
  2359. }
  2360. return desc_count;
  2361. err_release:
  2362. /* Release all used data descriptors; header descriptors must not
  2363. * be DMA-unmapped.
  2364. */
  2365. mvneta_release_descs(pp, txq, first_desc, desc_count - 1);
  2366. return 0;
  2367. }
  2368. /* Handle tx fragmentation processing */
  2369. static int mvneta_tx_frag_process(struct mvneta_port *pp, struct sk_buff *skb,
  2370. struct mvneta_tx_queue *txq)
  2371. {
  2372. struct mvneta_tx_desc *tx_desc;
  2373. int i, nr_frags = skb_shinfo(skb)->nr_frags;
  2374. int first_desc = txq->txq_put_index;
  2375. for (i = 0; i < nr_frags; i++) {
  2376. struct mvneta_tx_buf *buf = &txq->buf[txq->txq_put_index];
  2377. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  2378. void *addr = skb_frag_address(frag);
  2379. tx_desc = mvneta_txq_next_desc_get(txq);
  2380. tx_desc->data_size = skb_frag_size(frag);
  2381. tx_desc->buf_phys_addr =
  2382. dma_map_single(pp->dev->dev.parent, addr,
  2383. tx_desc->data_size, DMA_TO_DEVICE);
  2384. if (dma_mapping_error(pp->dev->dev.parent,
  2385. tx_desc->buf_phys_addr)) {
  2386. mvneta_txq_desc_put(txq);
  2387. goto error;
  2388. }
  2389. if (i == nr_frags - 1) {
  2390. /* Last descriptor */
  2391. tx_desc->command = MVNETA_TXD_L_DESC | MVNETA_TXD_Z_PAD;
  2392. buf->skb = skb;
  2393. } else {
  2394. /* Descriptor in the middle: Not First, Not Last */
  2395. tx_desc->command = 0;
  2396. buf->skb = NULL;
  2397. }
  2398. buf->type = MVNETA_TYPE_SKB;
  2399. mvneta_txq_inc_put(txq);
  2400. }
  2401. return 0;
  2402. error:
  2403. /* Release all descriptors that were used to map fragments of
  2404. * this packet, as well as the corresponding DMA mappings
  2405. */
  2406. mvneta_release_descs(pp, txq, first_desc, i - 1);
  2407. return -ENOMEM;
  2408. }
  2409. /* Main tx processing */
  2410. static netdev_tx_t mvneta_tx(struct sk_buff *skb, struct net_device *dev)
  2411. {
  2412. struct mvneta_port *pp = netdev_priv(dev);
  2413. u16 txq_id = skb_get_queue_mapping(skb);
  2414. struct mvneta_tx_queue *txq = &pp->txqs[txq_id];
  2415. struct mvneta_tx_buf *buf = &txq->buf[txq->txq_put_index];
  2416. struct mvneta_tx_desc *tx_desc;
  2417. int len = skb->len;
  2418. int frags = 0;
  2419. u32 tx_cmd;
  2420. if (!netif_running(dev))
  2421. goto out;
  2422. if (skb_is_gso(skb)) {
  2423. frags = mvneta_tx_tso(skb, dev, txq);
  2424. goto out;
  2425. }
  2426. frags = skb_shinfo(skb)->nr_frags + 1;
  2427. /* Get a descriptor for the first part of the packet */
  2428. tx_desc = mvneta_txq_next_desc_get(txq);
  2429. tx_cmd = mvneta_skb_tx_csum(skb);
  2430. tx_desc->data_size = skb_headlen(skb);
  2431. tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, skb->data,
  2432. tx_desc->data_size,
  2433. DMA_TO_DEVICE);
  2434. if (unlikely(dma_mapping_error(dev->dev.parent,
  2435. tx_desc->buf_phys_addr))) {
  2436. mvneta_txq_desc_put(txq);
  2437. frags = 0;
  2438. goto out;
  2439. }
  2440. buf->type = MVNETA_TYPE_SKB;
  2441. if (frags == 1) {
  2442. /* First and Last descriptor */
  2443. tx_cmd |= MVNETA_TXD_FLZ_DESC;
  2444. tx_desc->command = tx_cmd;
  2445. buf->skb = skb;
  2446. mvneta_txq_inc_put(txq);
  2447. } else {
  2448. /* First but not Last */
  2449. tx_cmd |= MVNETA_TXD_F_DESC;
  2450. buf->skb = NULL;
  2451. mvneta_txq_inc_put(txq);
  2452. tx_desc->command = tx_cmd;
  2453. /* Continue with other skb fragments */
  2454. if (mvneta_tx_frag_process(pp, skb, txq)) {
  2455. dma_unmap_single(dev->dev.parent,
  2456. tx_desc->buf_phys_addr,
  2457. tx_desc->data_size,
  2458. DMA_TO_DEVICE);
  2459. mvneta_txq_desc_put(txq);
  2460. frags = 0;
  2461. goto out;
  2462. }
  2463. }
  2464. out:
  2465. if (frags > 0) {
  2466. struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
  2467. struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
  2468. netdev_tx_sent_queue(nq, len);
  2469. txq->count += frags;
  2470. if (txq->count >= txq->tx_stop_threshold)
  2471. netif_tx_stop_queue(nq);
  2472. /* This is not really the true transmit point, since we batch
  2473. * up several before hitting the hardware, but is the best we
  2474. * can do without more complexity to walk the packets in the
  2475. * pending section of the transmit queue.
  2476. */
  2477. skb_tx_timestamp(skb);
  2478. if (!netdev_xmit_more() || netif_xmit_stopped(nq) ||
  2479. txq->pending + frags > MVNETA_TXQ_DEC_SENT_MASK)
  2480. mvneta_txq_pend_desc_add(pp, txq, frags);
  2481. else
  2482. txq->pending += frags;
  2483. u64_stats_update_begin(&stats->syncp);
  2484. stats->es.ps.tx_bytes += len;
  2485. stats->es.ps.tx_packets++;
  2486. u64_stats_update_end(&stats->syncp);
  2487. } else {
  2488. dev->stats.tx_dropped++;
  2489. dev_kfree_skb_any(skb);
  2490. }
  2491. return NETDEV_TX_OK;
  2492. }
  2493. /* Free tx resources, when resetting a port */
  2494. static void mvneta_txq_done_force(struct mvneta_port *pp,
  2495. struct mvneta_tx_queue *txq)
  2496. {
  2497. struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
  2498. int tx_done = txq->count;
  2499. mvneta_txq_bufs_free(pp, txq, tx_done, nq, false);
  2500. /* reset txq */
  2501. txq->count = 0;
  2502. txq->txq_put_index = 0;
  2503. txq->txq_get_index = 0;
  2504. }
  2505. /* Handle tx done - called in softirq context. The <cause_tx_done> argument
  2506. * must be a valid cause according to MVNETA_TXQ_INTR_MASK_ALL.
  2507. */
  2508. static void mvneta_tx_done_gbe(struct mvneta_port *pp, u32 cause_tx_done)
  2509. {
  2510. struct mvneta_tx_queue *txq;
  2511. struct netdev_queue *nq;
  2512. int cpu = smp_processor_id();
  2513. while (cause_tx_done) {
  2514. txq = mvneta_tx_done_policy(pp, cause_tx_done);
  2515. nq = netdev_get_tx_queue(pp->dev, txq->id);
  2516. __netif_tx_lock(nq, cpu);
  2517. if (txq->count)
  2518. mvneta_txq_done(pp, txq);
  2519. __netif_tx_unlock(nq);
  2520. cause_tx_done &= ~((1 << txq->id));
  2521. }
  2522. }
  2523. /* Compute crc8 of the specified address, using a unique algorithm ,
  2524. * according to hw spec, different than generic crc8 algorithm
  2525. */
  2526. static int mvneta_addr_crc(unsigned char *addr)
  2527. {
  2528. int crc = 0;
  2529. int i;
  2530. for (i = 0; i < ETH_ALEN; i++) {
  2531. int j;
  2532. crc = (crc ^ addr[i]) << 8;
  2533. for (j = 7; j >= 0; j--) {
  2534. if (crc & (0x100 << j))
  2535. crc ^= 0x107 << j;
  2536. }
  2537. }
  2538. return crc;
  2539. }
  2540. /* This method controls the net device special MAC multicast support.
  2541. * The Special Multicast Table for MAC addresses supports MAC of the form
  2542. * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
  2543. * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
  2544. * Table entries in the DA-Filter table. This method set the Special
  2545. * Multicast Table appropriate entry.
  2546. */
  2547. static void mvneta_set_special_mcast_addr(struct mvneta_port *pp,
  2548. unsigned char last_byte,
  2549. int queue)
  2550. {
  2551. unsigned int smc_table_reg;
  2552. unsigned int tbl_offset;
  2553. unsigned int reg_offset;
  2554. /* Register offset from SMC table base */
  2555. tbl_offset = (last_byte / 4);
  2556. /* Entry offset within the above reg */
  2557. reg_offset = last_byte % 4;
  2558. smc_table_reg = mvreg_read(pp, (MVNETA_DA_FILT_SPEC_MCAST
  2559. + tbl_offset * 4));
  2560. if (queue == -1)
  2561. smc_table_reg &= ~(0xff << (8 * reg_offset));
  2562. else {
  2563. smc_table_reg &= ~(0xff << (8 * reg_offset));
  2564. smc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
  2565. }
  2566. mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + tbl_offset * 4,
  2567. smc_table_reg);
  2568. }
  2569. /* This method controls the network device Other MAC multicast support.
  2570. * The Other Multicast Table is used for multicast of another type.
  2571. * A CRC-8 is used as an index to the Other Multicast Table entries
  2572. * in the DA-Filter table.
  2573. * The method gets the CRC-8 value from the calling routine and
  2574. * sets the Other Multicast Table appropriate entry according to the
  2575. * specified CRC-8 .
  2576. */
  2577. static void mvneta_set_other_mcast_addr(struct mvneta_port *pp,
  2578. unsigned char crc8,
  2579. int queue)
  2580. {
  2581. unsigned int omc_table_reg;
  2582. unsigned int tbl_offset;
  2583. unsigned int reg_offset;
  2584. tbl_offset = (crc8 / 4) * 4; /* Register offset from OMC table base */
  2585. reg_offset = crc8 % 4; /* Entry offset within the above reg */
  2586. omc_table_reg = mvreg_read(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset);
  2587. if (queue == -1) {
  2588. /* Clear accepts frame bit at specified Other DA table entry */
  2589. omc_table_reg &= ~(0xff << (8 * reg_offset));
  2590. } else {
  2591. omc_table_reg &= ~(0xff << (8 * reg_offset));
  2592. omc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
  2593. }
  2594. mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset, omc_table_reg);
  2595. }
  2596. /* The network device supports multicast using two tables:
  2597. * 1) Special Multicast Table for MAC addresses of the form
  2598. * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
  2599. * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
  2600. * Table entries in the DA-Filter table.
  2601. * 2) Other Multicast Table for multicast of another type. A CRC-8 value
  2602. * is used as an index to the Other Multicast Table entries in the
  2603. * DA-Filter table.
  2604. */
  2605. static int mvneta_mcast_addr_set(struct mvneta_port *pp, unsigned char *p_addr,
  2606. int queue)
  2607. {
  2608. unsigned char crc_result = 0;
  2609. if (memcmp(p_addr, "\x01\x00\x5e\x00\x00", 5) == 0) {
  2610. mvneta_set_special_mcast_addr(pp, p_addr[5], queue);
  2611. return 0;
  2612. }
  2613. crc_result = mvneta_addr_crc(p_addr);
  2614. if (queue == -1) {
  2615. if (pp->mcast_count[crc_result] == 0) {
  2616. netdev_info(pp->dev, "No valid Mcast for crc8=0x%02x\n",
  2617. crc_result);
  2618. return -EINVAL;
  2619. }
  2620. pp->mcast_count[crc_result]--;
  2621. if (pp->mcast_count[crc_result] != 0) {
  2622. netdev_info(pp->dev,
  2623. "After delete there are %d valid Mcast for crc8=0x%02x\n",
  2624. pp->mcast_count[crc_result], crc_result);
  2625. return -EINVAL;
  2626. }
  2627. } else
  2628. pp->mcast_count[crc_result]++;
  2629. mvneta_set_other_mcast_addr(pp, crc_result, queue);
  2630. return 0;
  2631. }
  2632. /* Configure Fitering mode of Ethernet port */
  2633. static void mvneta_rx_unicast_promisc_set(struct mvneta_port *pp,
  2634. int is_promisc)
  2635. {
  2636. u32 port_cfg_reg, val;
  2637. port_cfg_reg = mvreg_read(pp, MVNETA_PORT_CONFIG);
  2638. val = mvreg_read(pp, MVNETA_TYPE_PRIO);
  2639. /* Set / Clear UPM bit in port configuration register */
  2640. if (is_promisc) {
  2641. /* Accept all Unicast addresses */
  2642. port_cfg_reg |= MVNETA_UNI_PROMISC_MODE;
  2643. val |= MVNETA_FORCE_UNI;
  2644. mvreg_write(pp, MVNETA_MAC_ADDR_LOW, 0xffff);
  2645. mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, 0xffffffff);
  2646. } else {
  2647. /* Reject all Unicast addresses */
  2648. port_cfg_reg &= ~MVNETA_UNI_PROMISC_MODE;
  2649. val &= ~MVNETA_FORCE_UNI;
  2650. }
  2651. mvreg_write(pp, MVNETA_PORT_CONFIG, port_cfg_reg);
  2652. mvreg_write(pp, MVNETA_TYPE_PRIO, val);
  2653. }
  2654. /* register unicast and multicast addresses */
  2655. static void mvneta_set_rx_mode(struct net_device *dev)
  2656. {
  2657. struct mvneta_port *pp = netdev_priv(dev);
  2658. struct netdev_hw_addr *ha;
  2659. if (dev->flags & IFF_PROMISC) {
  2660. /* Accept all: Multicast + Unicast */
  2661. mvneta_rx_unicast_promisc_set(pp, 1);
  2662. mvneta_set_ucast_table(pp, pp->rxq_def);
  2663. mvneta_set_special_mcast_table(pp, pp->rxq_def);
  2664. mvneta_set_other_mcast_table(pp, pp->rxq_def);
  2665. } else {
  2666. /* Accept single Unicast */
  2667. mvneta_rx_unicast_promisc_set(pp, 0);
  2668. mvneta_set_ucast_table(pp, -1);
  2669. mvneta_mac_addr_set(pp, dev->dev_addr, pp->rxq_def);
  2670. if (dev->flags & IFF_ALLMULTI) {
  2671. /* Accept all multicast */
  2672. mvneta_set_special_mcast_table(pp, pp->rxq_def);
  2673. mvneta_set_other_mcast_table(pp, pp->rxq_def);
  2674. } else {
  2675. /* Accept only initialized multicast */
  2676. mvneta_set_special_mcast_table(pp, -1);
  2677. mvneta_set_other_mcast_table(pp, -1);
  2678. if (!netdev_mc_empty(dev)) {
  2679. netdev_for_each_mc_addr(ha, dev) {
  2680. mvneta_mcast_addr_set(pp, ha->addr,
  2681. pp->rxq_def);
  2682. }
  2683. }
  2684. }
  2685. }
  2686. }
  2687. /* Interrupt handling - the callback for request_irq() */
  2688. static irqreturn_t mvneta_isr(int irq, void *dev_id)
  2689. {
  2690. struct mvneta_port *pp = (struct mvneta_port *)dev_id;
  2691. mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
  2692. napi_schedule(&pp->napi);
  2693. return IRQ_HANDLED;
  2694. }
  2695. /* Interrupt handling - the callback for request_percpu_irq() */
  2696. static irqreturn_t mvneta_percpu_isr(int irq, void *dev_id)
  2697. {
  2698. struct mvneta_pcpu_port *port = (struct mvneta_pcpu_port *)dev_id;
  2699. disable_percpu_irq(port->pp->dev->irq);
  2700. napi_schedule(&port->napi);
  2701. return IRQ_HANDLED;
  2702. }
  2703. static void mvneta_link_change(struct mvneta_port *pp)
  2704. {
  2705. u32 gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS);
  2706. phylink_pcs_change(&pp->phylink_pcs,
  2707. !!(gmac_stat & MVNETA_GMAC_LINK_UP));
  2708. }
  2709. /* NAPI handler
  2710. * Bits 0 - 7 of the causeRxTx register indicate that are transmitted
  2711. * packets on the corresponding TXQ (Bit 0 is for TX queue 1).
  2712. * Bits 8 -15 of the cause Rx Tx register indicate that are received
  2713. * packets on the corresponding RXQ (Bit 8 is for RX queue 0).
  2714. * Each CPU has its own causeRxTx register
  2715. */
  2716. static int mvneta_poll(struct napi_struct *napi, int budget)
  2717. {
  2718. int rx_done = 0;
  2719. u32 cause_rx_tx;
  2720. int rx_queue;
  2721. struct mvneta_port *pp = netdev_priv(napi->dev);
  2722. struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
  2723. if (!netif_running(pp->dev)) {
  2724. napi_complete(napi);
  2725. return rx_done;
  2726. }
  2727. /* Read cause register */
  2728. cause_rx_tx = mvreg_read(pp, MVNETA_INTR_NEW_CAUSE);
  2729. if (cause_rx_tx & MVNETA_MISCINTR_INTR_MASK) {
  2730. u32 cause_misc = mvreg_read(pp, MVNETA_INTR_MISC_CAUSE);
  2731. mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
  2732. if (cause_misc & (MVNETA_CAUSE_PHY_STATUS_CHANGE |
  2733. MVNETA_CAUSE_LINK_CHANGE))
  2734. mvneta_link_change(pp);
  2735. }
  2736. /* Release Tx descriptors */
  2737. if (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL) {
  2738. mvneta_tx_done_gbe(pp, (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL));
  2739. cause_rx_tx &= ~MVNETA_TX_INTR_MASK_ALL;
  2740. }
  2741. /* For the case where the last mvneta_poll did not process all
  2742. * RX packets
  2743. */
  2744. cause_rx_tx |= pp->neta_armada3700 ? pp->cause_rx_tx :
  2745. port->cause_rx_tx;
  2746. rx_queue = fls(((cause_rx_tx >> 8) & 0xff));
  2747. if (rx_queue) {
  2748. rx_queue = rx_queue - 1;
  2749. if (pp->bm_priv)
  2750. rx_done = mvneta_rx_hwbm(napi, pp, budget,
  2751. &pp->rxqs[rx_queue]);
  2752. else
  2753. rx_done = mvneta_rx_swbm(napi, pp, budget,
  2754. &pp->rxqs[rx_queue]);
  2755. }
  2756. if (rx_done < budget) {
  2757. cause_rx_tx = 0;
  2758. napi_complete_done(napi, rx_done);
  2759. if (pp->neta_armada3700) {
  2760. unsigned long flags;
  2761. local_irq_save(flags);
  2762. mvreg_write(pp, MVNETA_INTR_NEW_MASK,
  2763. MVNETA_RX_INTR_MASK(rxq_number) |
  2764. MVNETA_TX_INTR_MASK(txq_number) |
  2765. MVNETA_MISCINTR_INTR_MASK);
  2766. local_irq_restore(flags);
  2767. } else {
  2768. enable_percpu_irq(pp->dev->irq, 0);
  2769. }
  2770. }
  2771. if (pp->neta_armada3700)
  2772. pp->cause_rx_tx = cause_rx_tx;
  2773. else
  2774. port->cause_rx_tx = cause_rx_tx;
  2775. return rx_done;
  2776. }
  2777. static int mvneta_create_page_pool(struct mvneta_port *pp,
  2778. struct mvneta_rx_queue *rxq, int size)
  2779. {
  2780. struct bpf_prog *xdp_prog = READ_ONCE(pp->xdp_prog);
  2781. struct page_pool_params pp_params = {
  2782. .order = 0,
  2783. .flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV,
  2784. .pool_size = size,
  2785. .nid = NUMA_NO_NODE,
  2786. .dev = pp->dev->dev.parent,
  2787. .dma_dir = xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE,
  2788. .offset = pp->rx_offset_correction,
  2789. .max_len = MVNETA_MAX_RX_BUF_SIZE,
  2790. };
  2791. int err;
  2792. rxq->page_pool = page_pool_create(&pp_params);
  2793. if (IS_ERR(rxq->page_pool)) {
  2794. err = PTR_ERR(rxq->page_pool);
  2795. rxq->page_pool = NULL;
  2796. return err;
  2797. }
  2798. err = __xdp_rxq_info_reg(&rxq->xdp_rxq, pp->dev, rxq->id, 0,
  2799. PAGE_SIZE);
  2800. if (err < 0)
  2801. goto err_free_pp;
  2802. err = xdp_rxq_info_reg_mem_model(&rxq->xdp_rxq, MEM_TYPE_PAGE_POOL,
  2803. rxq->page_pool);
  2804. if (err)
  2805. goto err_unregister_rxq;
  2806. return 0;
  2807. err_unregister_rxq:
  2808. xdp_rxq_info_unreg(&rxq->xdp_rxq);
  2809. err_free_pp:
  2810. page_pool_destroy(rxq->page_pool);
  2811. rxq->page_pool = NULL;
  2812. return err;
  2813. }
  2814. /* Handle rxq fill: allocates rxq skbs; called when initializing a port */
  2815. static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
  2816. int num)
  2817. {
  2818. int i, err;
  2819. err = mvneta_create_page_pool(pp, rxq, num);
  2820. if (err < 0)
  2821. return err;
  2822. for (i = 0; i < num; i++) {
  2823. memset(rxq->descs + i, 0, sizeof(struct mvneta_rx_desc));
  2824. if (mvneta_rx_refill(pp, rxq->descs + i, rxq,
  2825. GFP_KERNEL) != 0) {
  2826. netdev_err(pp->dev,
  2827. "%s:rxq %d, %d of %d buffs filled\n",
  2828. __func__, rxq->id, i, num);
  2829. break;
  2830. }
  2831. }
  2832. /* Add this number of RX descriptors as non occupied (ready to
  2833. * get packets)
  2834. */
  2835. mvneta_rxq_non_occup_desc_add(pp, rxq, i);
  2836. return i;
  2837. }
  2838. /* Free all packets pending transmit from all TXQs and reset TX port */
  2839. static void mvneta_tx_reset(struct mvneta_port *pp)
  2840. {
  2841. int queue;
  2842. /* free the skb's in the tx ring */
  2843. for (queue = 0; queue < txq_number; queue++)
  2844. mvneta_txq_done_force(pp, &pp->txqs[queue]);
  2845. mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
  2846. mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
  2847. }
  2848. static void mvneta_rx_reset(struct mvneta_port *pp)
  2849. {
  2850. mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
  2851. mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
  2852. }
  2853. /* Rx/Tx queue initialization/cleanup methods */
  2854. static int mvneta_rxq_sw_init(struct mvneta_port *pp,
  2855. struct mvneta_rx_queue *rxq)
  2856. {
  2857. rxq->size = pp->rx_ring_size;
  2858. /* Allocate memory for RX descriptors */
  2859. rxq->descs = dma_alloc_coherent(pp->dev->dev.parent,
  2860. rxq->size * MVNETA_DESC_ALIGNED_SIZE,
  2861. &rxq->descs_phys, GFP_KERNEL);
  2862. if (!rxq->descs)
  2863. return -ENOMEM;
  2864. rxq->last_desc = rxq->size - 1;
  2865. return 0;
  2866. }
  2867. static void mvneta_rxq_hw_init(struct mvneta_port *pp,
  2868. struct mvneta_rx_queue *rxq)
  2869. {
  2870. /* Set Rx descriptors queue starting address */
  2871. mvreg_write(pp, MVNETA_RXQ_BASE_ADDR_REG(rxq->id), rxq->descs_phys);
  2872. mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), rxq->size);
  2873. /* Set coalescing pkts and time */
  2874. mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
  2875. mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
  2876. if (!pp->bm_priv) {
  2877. /* Set Offset */
  2878. mvneta_rxq_offset_set(pp, rxq, 0);
  2879. mvneta_rxq_buf_size_set(pp, rxq, PAGE_SIZE < SZ_64K ?
  2880. MVNETA_MAX_RX_BUF_SIZE :
  2881. MVNETA_RX_BUF_SIZE(pp->pkt_size));
  2882. mvneta_rxq_bm_disable(pp, rxq);
  2883. mvneta_rxq_fill(pp, rxq, rxq->size);
  2884. } else {
  2885. /* Set Offset */
  2886. mvneta_rxq_offset_set(pp, rxq,
  2887. NET_SKB_PAD - pp->rx_offset_correction);
  2888. mvneta_rxq_bm_enable(pp, rxq);
  2889. /* Fill RXQ with buffers from RX pool */
  2890. mvneta_rxq_long_pool_set(pp, rxq);
  2891. mvneta_rxq_short_pool_set(pp, rxq);
  2892. mvneta_rxq_non_occup_desc_add(pp, rxq, rxq->size);
  2893. }
  2894. }
  2895. /* Create a specified RX queue */
  2896. static int mvneta_rxq_init(struct mvneta_port *pp,
  2897. struct mvneta_rx_queue *rxq)
  2898. {
  2899. int ret;
  2900. ret = mvneta_rxq_sw_init(pp, rxq);
  2901. if (ret < 0)
  2902. return ret;
  2903. mvneta_rxq_hw_init(pp, rxq);
  2904. return 0;
  2905. }
  2906. /* Cleanup Rx queue */
  2907. static void mvneta_rxq_deinit(struct mvneta_port *pp,
  2908. struct mvneta_rx_queue *rxq)
  2909. {
  2910. mvneta_rxq_drop_pkts(pp, rxq);
  2911. if (rxq->descs)
  2912. dma_free_coherent(pp->dev->dev.parent,
  2913. rxq->size * MVNETA_DESC_ALIGNED_SIZE,
  2914. rxq->descs,
  2915. rxq->descs_phys);
  2916. rxq->descs = NULL;
  2917. rxq->last_desc = 0;
  2918. rxq->next_desc_to_proc = 0;
  2919. rxq->descs_phys = 0;
  2920. rxq->first_to_refill = 0;
  2921. rxq->refill_num = 0;
  2922. }
  2923. static int mvneta_txq_sw_init(struct mvneta_port *pp,
  2924. struct mvneta_tx_queue *txq)
  2925. {
  2926. int cpu, err;
  2927. txq->size = pp->tx_ring_size;
  2928. /* A queue must always have room for at least one skb.
  2929. * Therefore, stop the queue when the free entries reaches
  2930. * the maximum number of descriptors per skb.
  2931. */
  2932. txq->tx_stop_threshold = txq->size - MVNETA_MAX_SKB_DESCS;
  2933. txq->tx_wake_threshold = txq->tx_stop_threshold / 2;
  2934. /* Allocate memory for TX descriptors */
  2935. txq->descs = dma_alloc_coherent(pp->dev->dev.parent,
  2936. txq->size * MVNETA_DESC_ALIGNED_SIZE,
  2937. &txq->descs_phys, GFP_KERNEL);
  2938. if (!txq->descs)
  2939. return -ENOMEM;
  2940. txq->last_desc = txq->size - 1;
  2941. txq->buf = kmalloc_objs(*txq->buf, txq->size);
  2942. if (!txq->buf)
  2943. return -ENOMEM;
  2944. /* Allocate DMA buffers for TSO MAC/IP/TCP headers */
  2945. err = mvneta_alloc_tso_hdrs(pp, txq);
  2946. if (err)
  2947. return err;
  2948. /* Setup XPS mapping */
  2949. if (pp->neta_armada3700)
  2950. cpu = 0;
  2951. else if (txq_number > 1)
  2952. cpu = txq->id % num_present_cpus();
  2953. else
  2954. cpu = pp->rxq_def % num_present_cpus();
  2955. cpumask_set_cpu(cpu, &txq->affinity_mask);
  2956. netif_set_xps_queue(pp->dev, &txq->affinity_mask, txq->id);
  2957. return 0;
  2958. }
  2959. static void mvneta_txq_hw_init(struct mvneta_port *pp,
  2960. struct mvneta_tx_queue *txq)
  2961. {
  2962. /* Set maximum bandwidth for enabled TXQs */
  2963. mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0x03ffffff);
  2964. mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0x3fffffff);
  2965. /* Set Tx descriptors queue starting address */
  2966. mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), txq->descs_phys);
  2967. mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), txq->size);
  2968. mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
  2969. }
  2970. /* Create and initialize a tx queue */
  2971. static int mvneta_txq_init(struct mvneta_port *pp,
  2972. struct mvneta_tx_queue *txq)
  2973. {
  2974. int ret;
  2975. ret = mvneta_txq_sw_init(pp, txq);
  2976. if (ret < 0)
  2977. return ret;
  2978. mvneta_txq_hw_init(pp, txq);
  2979. return 0;
  2980. }
  2981. /* Free allocated resources when mvneta_txq_init() fails to allocate memory*/
  2982. static void mvneta_txq_sw_deinit(struct mvneta_port *pp,
  2983. struct mvneta_tx_queue *txq)
  2984. {
  2985. struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
  2986. kfree(txq->buf);
  2987. mvneta_free_tso_hdrs(pp, txq);
  2988. if (txq->descs)
  2989. dma_free_coherent(pp->dev->dev.parent,
  2990. txq->size * MVNETA_DESC_ALIGNED_SIZE,
  2991. txq->descs, txq->descs_phys);
  2992. netdev_tx_reset_queue(nq);
  2993. txq->buf = NULL;
  2994. txq->descs = NULL;
  2995. txq->last_desc = 0;
  2996. txq->next_desc_to_proc = 0;
  2997. txq->descs_phys = 0;
  2998. }
  2999. static void mvneta_txq_hw_deinit(struct mvneta_port *pp,
  3000. struct mvneta_tx_queue *txq)
  3001. {
  3002. /* Set minimum bandwidth for disabled TXQs */
  3003. mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0);
  3004. mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0);
  3005. /* Set Tx descriptors queue starting address and size */
  3006. mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), 0);
  3007. mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), 0);
  3008. }
  3009. static void mvneta_txq_deinit(struct mvneta_port *pp,
  3010. struct mvneta_tx_queue *txq)
  3011. {
  3012. mvneta_txq_sw_deinit(pp, txq);
  3013. mvneta_txq_hw_deinit(pp, txq);
  3014. }
  3015. /* Cleanup all Tx queues */
  3016. static void mvneta_cleanup_txqs(struct mvneta_port *pp)
  3017. {
  3018. int queue;
  3019. for (queue = 0; queue < txq_number; queue++)
  3020. mvneta_txq_deinit(pp, &pp->txqs[queue]);
  3021. }
  3022. /* Cleanup all Rx queues */
  3023. static void mvneta_cleanup_rxqs(struct mvneta_port *pp)
  3024. {
  3025. int queue;
  3026. for (queue = 0; queue < rxq_number; queue++)
  3027. mvneta_rxq_deinit(pp, &pp->rxqs[queue]);
  3028. }
  3029. /* Init all Rx queues */
  3030. static int mvneta_setup_rxqs(struct mvneta_port *pp)
  3031. {
  3032. int queue;
  3033. for (queue = 0; queue < rxq_number; queue++) {
  3034. int err = mvneta_rxq_init(pp, &pp->rxqs[queue]);
  3035. if (err) {
  3036. netdev_err(pp->dev, "%s: can't create rxq=%d\n",
  3037. __func__, queue);
  3038. mvneta_cleanup_rxqs(pp);
  3039. return err;
  3040. }
  3041. }
  3042. return 0;
  3043. }
  3044. /* Init all tx queues */
  3045. static int mvneta_setup_txqs(struct mvneta_port *pp)
  3046. {
  3047. int queue;
  3048. for (queue = 0; queue < txq_number; queue++) {
  3049. int err = mvneta_txq_init(pp, &pp->txqs[queue]);
  3050. if (err) {
  3051. netdev_err(pp->dev, "%s: can't create txq=%d\n",
  3052. __func__, queue);
  3053. mvneta_cleanup_txqs(pp);
  3054. return err;
  3055. }
  3056. }
  3057. return 0;
  3058. }
  3059. static int mvneta_comphy_init(struct mvneta_port *pp, phy_interface_t interface)
  3060. {
  3061. int ret;
  3062. ret = phy_set_mode_ext(pp->comphy, PHY_MODE_ETHERNET, interface);
  3063. if (ret)
  3064. return ret;
  3065. return phy_power_on(pp->comphy);
  3066. }
  3067. static int mvneta_config_interface(struct mvneta_port *pp,
  3068. phy_interface_t interface)
  3069. {
  3070. int ret = 0;
  3071. if (pp->comphy) {
  3072. if (interface == PHY_INTERFACE_MODE_SGMII ||
  3073. interface == PHY_INTERFACE_MODE_1000BASEX ||
  3074. interface == PHY_INTERFACE_MODE_2500BASEX) {
  3075. ret = mvneta_comphy_init(pp, interface);
  3076. }
  3077. } else {
  3078. switch (interface) {
  3079. case PHY_INTERFACE_MODE_QSGMII:
  3080. mvreg_write(pp, MVNETA_SERDES_CFG,
  3081. MVNETA_QSGMII_SERDES_PROTO);
  3082. break;
  3083. case PHY_INTERFACE_MODE_SGMII:
  3084. case PHY_INTERFACE_MODE_1000BASEX:
  3085. mvreg_write(pp, MVNETA_SERDES_CFG,
  3086. MVNETA_SGMII_SERDES_PROTO);
  3087. break;
  3088. case PHY_INTERFACE_MODE_2500BASEX:
  3089. mvreg_write(pp, MVNETA_SERDES_CFG,
  3090. MVNETA_HSGMII_SERDES_PROTO);
  3091. break;
  3092. default:
  3093. break;
  3094. }
  3095. }
  3096. pp->phy_interface = interface;
  3097. return ret;
  3098. }
  3099. static void mvneta_start_dev(struct mvneta_port *pp)
  3100. {
  3101. int cpu;
  3102. WARN_ON(mvneta_config_interface(pp, pp->phy_interface));
  3103. mvneta_max_rx_size_set(pp, pp->pkt_size);
  3104. mvneta_txq_max_tx_size_set(pp, pp->pkt_size);
  3105. /* start the Rx/Tx activity */
  3106. mvneta_port_enable(pp);
  3107. if (!pp->neta_armada3700) {
  3108. /* Enable polling on the port */
  3109. for_each_online_cpu(cpu) {
  3110. struct mvneta_pcpu_port *port =
  3111. per_cpu_ptr(pp->ports, cpu);
  3112. napi_enable(&port->napi);
  3113. }
  3114. } else {
  3115. napi_enable(&pp->napi);
  3116. }
  3117. /* Unmask interrupts. It has to be done from each CPU */
  3118. on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
  3119. mvreg_write(pp, MVNETA_INTR_MISC_MASK,
  3120. MVNETA_CAUSE_PHY_STATUS_CHANGE |
  3121. MVNETA_CAUSE_LINK_CHANGE);
  3122. phylink_start(pp->phylink);
  3123. /* We may have called phylink_speed_down before */
  3124. phylink_speed_up(pp->phylink);
  3125. netif_tx_start_all_queues(pp->dev);
  3126. clear_bit(__MVNETA_DOWN, &pp->state);
  3127. }
  3128. static void mvneta_stop_dev(struct mvneta_port *pp)
  3129. {
  3130. unsigned int cpu;
  3131. set_bit(__MVNETA_DOWN, &pp->state);
  3132. if (device_may_wakeup(&pp->dev->dev))
  3133. phylink_speed_down(pp->phylink, false);
  3134. phylink_stop(pp->phylink);
  3135. if (!pp->neta_armada3700) {
  3136. for_each_online_cpu(cpu) {
  3137. struct mvneta_pcpu_port *port =
  3138. per_cpu_ptr(pp->ports, cpu);
  3139. napi_disable(&port->napi);
  3140. }
  3141. } else {
  3142. napi_disable(&pp->napi);
  3143. }
  3144. netif_carrier_off(pp->dev);
  3145. mvneta_port_down(pp);
  3146. netif_tx_stop_all_queues(pp->dev);
  3147. /* Stop the port activity */
  3148. mvneta_port_disable(pp);
  3149. /* Clear all ethernet port interrupts */
  3150. on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
  3151. /* Mask all ethernet port interrupts */
  3152. on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
  3153. mvneta_tx_reset(pp);
  3154. mvneta_rx_reset(pp);
  3155. WARN_ON(phy_power_off(pp->comphy));
  3156. }
  3157. static void mvneta_percpu_enable(void *arg)
  3158. {
  3159. struct mvneta_port *pp = arg;
  3160. enable_percpu_irq(pp->dev->irq, IRQ_TYPE_NONE);
  3161. }
  3162. static void mvneta_percpu_disable(void *arg)
  3163. {
  3164. struct mvneta_port *pp = arg;
  3165. disable_percpu_irq(pp->dev->irq);
  3166. }
  3167. /* Change the device mtu */
  3168. static int mvneta_change_mtu(struct net_device *dev, int mtu)
  3169. {
  3170. struct mvneta_port *pp = netdev_priv(dev);
  3171. struct bpf_prog *prog = pp->xdp_prog;
  3172. int ret;
  3173. if (!IS_ALIGNED(MVNETA_RX_PKT_SIZE(mtu), 8)) {
  3174. netdev_info(dev, "Illegal MTU value %d, rounding to %d\n",
  3175. mtu, ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8));
  3176. mtu = ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8);
  3177. }
  3178. if (prog && !prog->aux->xdp_has_frags &&
  3179. mtu > MVNETA_MAX_RX_BUF_SIZE) {
  3180. netdev_info(dev, "Illegal MTU %d for XDP prog without frags\n",
  3181. mtu);
  3182. return -EINVAL;
  3183. }
  3184. WRITE_ONCE(dev->mtu, mtu);
  3185. if (!netif_running(dev)) {
  3186. if (pp->bm_priv)
  3187. mvneta_bm_update_mtu(pp, mtu);
  3188. netdev_update_features(dev);
  3189. return 0;
  3190. }
  3191. /* The interface is running, so we have to force a
  3192. * reallocation of the queues
  3193. */
  3194. mvneta_stop_dev(pp);
  3195. on_each_cpu(mvneta_percpu_disable, pp, true);
  3196. mvneta_cleanup_txqs(pp);
  3197. mvneta_cleanup_rxqs(pp);
  3198. if (pp->bm_priv)
  3199. mvneta_bm_update_mtu(pp, mtu);
  3200. pp->pkt_size = MVNETA_RX_PKT_SIZE(dev->mtu);
  3201. ret = mvneta_setup_rxqs(pp);
  3202. if (ret) {
  3203. netdev_err(dev, "unable to setup rxqs after MTU change\n");
  3204. return ret;
  3205. }
  3206. ret = mvneta_setup_txqs(pp);
  3207. if (ret) {
  3208. netdev_err(dev, "unable to setup txqs after MTU change\n");
  3209. return ret;
  3210. }
  3211. on_each_cpu(mvneta_percpu_enable, pp, true);
  3212. mvneta_start_dev(pp);
  3213. netdev_update_features(dev);
  3214. return 0;
  3215. }
  3216. static netdev_features_t mvneta_fix_features(struct net_device *dev,
  3217. netdev_features_t features)
  3218. {
  3219. struct mvneta_port *pp = netdev_priv(dev);
  3220. if (pp->tx_csum_limit && dev->mtu > pp->tx_csum_limit) {
  3221. features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
  3222. netdev_info(dev,
  3223. "Disable IP checksum for MTU greater than %dB\n",
  3224. pp->tx_csum_limit);
  3225. }
  3226. return features;
  3227. }
  3228. /* Get mac address */
  3229. static void mvneta_get_mac_addr(struct mvneta_port *pp, unsigned char *addr)
  3230. {
  3231. u32 mac_addr_l, mac_addr_h;
  3232. mac_addr_l = mvreg_read(pp, MVNETA_MAC_ADDR_LOW);
  3233. mac_addr_h = mvreg_read(pp, MVNETA_MAC_ADDR_HIGH);
  3234. addr[0] = (mac_addr_h >> 24) & 0xFF;
  3235. addr[1] = (mac_addr_h >> 16) & 0xFF;
  3236. addr[2] = (mac_addr_h >> 8) & 0xFF;
  3237. addr[3] = mac_addr_h & 0xFF;
  3238. addr[4] = (mac_addr_l >> 8) & 0xFF;
  3239. addr[5] = mac_addr_l & 0xFF;
  3240. }
  3241. /* Handle setting mac address */
  3242. static int mvneta_set_mac_addr(struct net_device *dev, void *addr)
  3243. {
  3244. struct mvneta_port *pp = netdev_priv(dev);
  3245. struct sockaddr *sockaddr = addr;
  3246. int ret;
  3247. ret = eth_prepare_mac_addr_change(dev, addr);
  3248. if (ret < 0)
  3249. return ret;
  3250. /* Remove previous address table entry */
  3251. mvneta_mac_addr_set(pp, dev->dev_addr, -1);
  3252. /* Set new addr in hw */
  3253. mvneta_mac_addr_set(pp, sockaddr->sa_data, pp->rxq_def);
  3254. eth_commit_mac_addr_change(dev, addr);
  3255. return 0;
  3256. }
  3257. static struct mvneta_port *mvneta_pcs_to_port(struct phylink_pcs *pcs)
  3258. {
  3259. return container_of(pcs, struct mvneta_port, phylink_pcs);
  3260. }
  3261. static unsigned int mvneta_pcs_inband_caps(struct phylink_pcs *pcs,
  3262. phy_interface_t interface)
  3263. {
  3264. /* When operating in an 802.3z mode, we must have AN enabled:
  3265. * "Bit 2 Field InBandAnEn In-band Auto-Negotiation enable. ...
  3266. * When <PortType> = 1 (1000BASE-X) this field must be set to 1."
  3267. * Therefore, inband is "required".
  3268. */
  3269. if (phy_interface_mode_is_8023z(interface))
  3270. return LINK_INBAND_ENABLE;
  3271. /* QSGMII, SGMII and RGMII can be configured to use inband
  3272. * signalling of the AN result. Indicate these as "possible".
  3273. */
  3274. if (interface == PHY_INTERFACE_MODE_SGMII ||
  3275. interface == PHY_INTERFACE_MODE_QSGMII ||
  3276. phy_interface_mode_is_rgmii(interface))
  3277. return LINK_INBAND_DISABLE | LINK_INBAND_ENABLE;
  3278. /* For any other modes, indicate that inband is not supported. */
  3279. return LINK_INBAND_DISABLE;
  3280. }
  3281. static void mvneta_pcs_get_state(struct phylink_pcs *pcs, unsigned int neg_mode,
  3282. struct phylink_link_state *state)
  3283. {
  3284. struct mvneta_port *pp = mvneta_pcs_to_port(pcs);
  3285. u32 gmac_stat;
  3286. gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS);
  3287. if (gmac_stat & MVNETA_GMAC_SPEED_1000)
  3288. state->speed =
  3289. state->interface == PHY_INTERFACE_MODE_2500BASEX ?
  3290. SPEED_2500 : SPEED_1000;
  3291. else if (gmac_stat & MVNETA_GMAC_SPEED_100)
  3292. state->speed = SPEED_100;
  3293. else
  3294. state->speed = SPEED_10;
  3295. state->an_complete = !!(gmac_stat & MVNETA_GMAC_AN_COMPLETE);
  3296. state->link = !!(gmac_stat & MVNETA_GMAC_LINK_UP);
  3297. state->duplex = !!(gmac_stat & MVNETA_GMAC_FULL_DUPLEX);
  3298. if (gmac_stat & MVNETA_GMAC_RX_FLOW_CTRL_ENABLE)
  3299. state->pause |= MLO_PAUSE_RX;
  3300. if (gmac_stat & MVNETA_GMAC_TX_FLOW_CTRL_ENABLE)
  3301. state->pause |= MLO_PAUSE_TX;
  3302. }
  3303. static int mvneta_pcs_config(struct phylink_pcs *pcs, unsigned int neg_mode,
  3304. phy_interface_t interface,
  3305. const unsigned long *advertising,
  3306. bool permit_pause_to_mac)
  3307. {
  3308. struct mvneta_port *pp = mvneta_pcs_to_port(pcs);
  3309. u32 mask, val, an, old_an, changed;
  3310. mask = MVNETA_GMAC_INBAND_AN_ENABLE |
  3311. MVNETA_GMAC_INBAND_RESTART_AN |
  3312. MVNETA_GMAC_AN_SPEED_EN |
  3313. MVNETA_GMAC_AN_FLOW_CTRL_EN |
  3314. MVNETA_GMAC_AN_DUPLEX_EN;
  3315. if (neg_mode == PHYLINK_PCS_NEG_INBAND_ENABLED) {
  3316. mask |= MVNETA_GMAC_CONFIG_MII_SPEED |
  3317. MVNETA_GMAC_CONFIG_GMII_SPEED |
  3318. MVNETA_GMAC_CONFIG_FULL_DUPLEX;
  3319. val = MVNETA_GMAC_INBAND_AN_ENABLE;
  3320. if (interface == PHY_INTERFACE_MODE_SGMII) {
  3321. /* SGMII mode receives the speed and duplex from PHY */
  3322. val |= MVNETA_GMAC_AN_SPEED_EN |
  3323. MVNETA_GMAC_AN_DUPLEX_EN;
  3324. } else {
  3325. /* 802.3z mode has fixed speed and duplex */
  3326. val |= MVNETA_GMAC_CONFIG_GMII_SPEED |
  3327. MVNETA_GMAC_CONFIG_FULL_DUPLEX;
  3328. /* The FLOW_CTRL_EN bit selects either the hardware
  3329. * automatically or the CONFIG_FLOW_CTRL manually
  3330. * controls the GMAC pause mode.
  3331. */
  3332. if (permit_pause_to_mac)
  3333. val |= MVNETA_GMAC_AN_FLOW_CTRL_EN;
  3334. /* Update the advertisement bits */
  3335. mask |= MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL;
  3336. if (phylink_test(advertising, Pause))
  3337. val |= MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL;
  3338. }
  3339. } else {
  3340. /* Phy or fixed speed - disable in-band AN modes */
  3341. val = 0;
  3342. }
  3343. old_an = an = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
  3344. an = (an & ~mask) | val;
  3345. changed = old_an ^ an;
  3346. if (changed)
  3347. mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, an);
  3348. /* We are only interested in the advertisement bits changing */
  3349. return !!(changed & MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL);
  3350. }
  3351. static void mvneta_pcs_an_restart(struct phylink_pcs *pcs)
  3352. {
  3353. struct mvneta_port *pp = mvneta_pcs_to_port(pcs);
  3354. u32 gmac_an = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
  3355. mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
  3356. gmac_an | MVNETA_GMAC_INBAND_RESTART_AN);
  3357. mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
  3358. gmac_an & ~MVNETA_GMAC_INBAND_RESTART_AN);
  3359. }
  3360. static const struct phylink_pcs_ops mvneta_phylink_pcs_ops = {
  3361. .pcs_inband_caps = mvneta_pcs_inband_caps,
  3362. .pcs_get_state = mvneta_pcs_get_state,
  3363. .pcs_config = mvneta_pcs_config,
  3364. .pcs_an_restart = mvneta_pcs_an_restart,
  3365. };
  3366. static struct phylink_pcs *mvneta_mac_select_pcs(struct phylink_config *config,
  3367. phy_interface_t interface)
  3368. {
  3369. struct net_device *ndev = to_net_dev(config->dev);
  3370. struct mvneta_port *pp = netdev_priv(ndev);
  3371. return &pp->phylink_pcs;
  3372. }
  3373. static int mvneta_mac_prepare(struct phylink_config *config, unsigned int mode,
  3374. phy_interface_t interface)
  3375. {
  3376. struct net_device *ndev = to_net_dev(config->dev);
  3377. struct mvneta_port *pp = netdev_priv(ndev);
  3378. u32 val;
  3379. if (pp->phy_interface != interface ||
  3380. phylink_autoneg_inband(mode)) {
  3381. /* Force the link down when changing the interface or if in
  3382. * in-band mode. According to Armada 370 documentation, we
  3383. * can only change the port mode and in-band enable when the
  3384. * link is down.
  3385. */
  3386. val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
  3387. val &= ~MVNETA_GMAC_FORCE_LINK_PASS;
  3388. val |= MVNETA_GMAC_FORCE_LINK_DOWN;
  3389. mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
  3390. }
  3391. if (pp->phy_interface != interface)
  3392. WARN_ON(phy_power_off(pp->comphy));
  3393. /* Enable the 1ms clock */
  3394. if (phylink_autoneg_inband(mode)) {
  3395. unsigned long rate = clk_get_rate(pp->clk);
  3396. mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER,
  3397. MVNETA_GMAC_1MS_CLOCK_ENABLE | (rate / 1000));
  3398. }
  3399. return 0;
  3400. }
  3401. static void mvneta_mac_config(struct phylink_config *config, unsigned int mode,
  3402. const struct phylink_link_state *state)
  3403. {
  3404. struct net_device *ndev = to_net_dev(config->dev);
  3405. struct mvneta_port *pp = netdev_priv(ndev);
  3406. u32 new_ctrl0, gmac_ctrl0 = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
  3407. u32 new_ctrl2, gmac_ctrl2 = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
  3408. u32 new_ctrl4, gmac_ctrl4 = mvreg_read(pp, MVNETA_GMAC_CTRL_4);
  3409. new_ctrl0 = gmac_ctrl0 & ~MVNETA_GMAC0_PORT_1000BASE_X;
  3410. new_ctrl2 = gmac_ctrl2 & ~(MVNETA_GMAC2_INBAND_AN_ENABLE |
  3411. MVNETA_GMAC2_PORT_RESET);
  3412. new_ctrl4 = gmac_ctrl4 & ~(MVNETA_GMAC4_SHORT_PREAMBLE_ENABLE);
  3413. /* Even though it might look weird, when we're configured in
  3414. * SGMII or QSGMII mode, the RGMII bit needs to be set.
  3415. */
  3416. new_ctrl2 |= MVNETA_GMAC2_PORT_RGMII;
  3417. if (state->interface == PHY_INTERFACE_MODE_QSGMII ||
  3418. state->interface == PHY_INTERFACE_MODE_SGMII ||
  3419. phy_interface_mode_is_8023z(state->interface))
  3420. new_ctrl2 |= MVNETA_GMAC2_PCS_ENABLE;
  3421. if (!phylink_autoneg_inband(mode)) {
  3422. /* Phy or fixed speed - nothing to do, leave the
  3423. * configured speed, duplex and flow control as-is.
  3424. */
  3425. } else if (state->interface == PHY_INTERFACE_MODE_SGMII) {
  3426. /* SGMII mode receives the state from the PHY */
  3427. new_ctrl2 |= MVNETA_GMAC2_INBAND_AN_ENABLE;
  3428. } else {
  3429. /* 802.3z negotiation - only 1000base-X */
  3430. new_ctrl0 |= MVNETA_GMAC0_PORT_1000BASE_X;
  3431. }
  3432. /* When at 2.5G, the link partner can send frames with shortened
  3433. * preambles.
  3434. */
  3435. if (state->interface == PHY_INTERFACE_MODE_2500BASEX)
  3436. new_ctrl4 |= MVNETA_GMAC4_SHORT_PREAMBLE_ENABLE;
  3437. if (new_ctrl0 != gmac_ctrl0)
  3438. mvreg_write(pp, MVNETA_GMAC_CTRL_0, new_ctrl0);
  3439. if (new_ctrl2 != gmac_ctrl2)
  3440. mvreg_write(pp, MVNETA_GMAC_CTRL_2, new_ctrl2);
  3441. if (new_ctrl4 != gmac_ctrl4)
  3442. mvreg_write(pp, MVNETA_GMAC_CTRL_4, new_ctrl4);
  3443. if (gmac_ctrl2 & MVNETA_GMAC2_PORT_RESET) {
  3444. while ((mvreg_read(pp, MVNETA_GMAC_CTRL_2) &
  3445. MVNETA_GMAC2_PORT_RESET) != 0)
  3446. continue;
  3447. }
  3448. }
  3449. static int mvneta_mac_finish(struct phylink_config *config, unsigned int mode,
  3450. phy_interface_t interface)
  3451. {
  3452. struct net_device *ndev = to_net_dev(config->dev);
  3453. struct mvneta_port *pp = netdev_priv(ndev);
  3454. u32 val, clk;
  3455. /* Disable 1ms clock if not in in-band mode */
  3456. if (!phylink_autoneg_inband(mode)) {
  3457. clk = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER);
  3458. clk &= ~MVNETA_GMAC_1MS_CLOCK_ENABLE;
  3459. mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, clk);
  3460. }
  3461. if (pp->phy_interface != interface)
  3462. /* Enable the Serdes PHY */
  3463. WARN_ON(mvneta_config_interface(pp, interface));
  3464. /* Allow the link to come up if in in-band mode, otherwise the
  3465. * link is forced via mac_link_down()/mac_link_up()
  3466. */
  3467. if (phylink_autoneg_inband(mode)) {
  3468. val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
  3469. val &= ~MVNETA_GMAC_FORCE_LINK_DOWN;
  3470. mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
  3471. }
  3472. return 0;
  3473. }
  3474. static void mvneta_mac_link_down(struct phylink_config *config,
  3475. unsigned int mode, phy_interface_t interface)
  3476. {
  3477. struct net_device *ndev = to_net_dev(config->dev);
  3478. struct mvneta_port *pp = netdev_priv(ndev);
  3479. u32 val;
  3480. mvneta_port_down(pp);
  3481. if (!phylink_autoneg_inband(mode)) {
  3482. val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
  3483. val &= ~MVNETA_GMAC_FORCE_LINK_PASS;
  3484. val |= MVNETA_GMAC_FORCE_LINK_DOWN;
  3485. mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
  3486. }
  3487. }
  3488. static void mvneta_mac_link_up(struct phylink_config *config,
  3489. struct phy_device *phy,
  3490. unsigned int mode, phy_interface_t interface,
  3491. int speed, int duplex,
  3492. bool tx_pause, bool rx_pause)
  3493. {
  3494. struct net_device *ndev = to_net_dev(config->dev);
  3495. struct mvneta_port *pp = netdev_priv(ndev);
  3496. u32 val;
  3497. if (!phylink_autoneg_inband(mode)) {
  3498. val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
  3499. val &= ~(MVNETA_GMAC_FORCE_LINK_DOWN |
  3500. MVNETA_GMAC_CONFIG_MII_SPEED |
  3501. MVNETA_GMAC_CONFIG_GMII_SPEED |
  3502. MVNETA_GMAC_CONFIG_FLOW_CTRL |
  3503. MVNETA_GMAC_CONFIG_FULL_DUPLEX);
  3504. val |= MVNETA_GMAC_FORCE_LINK_PASS;
  3505. if (speed == SPEED_1000 || speed == SPEED_2500)
  3506. val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
  3507. else if (speed == SPEED_100)
  3508. val |= MVNETA_GMAC_CONFIG_MII_SPEED;
  3509. if (duplex == DUPLEX_FULL)
  3510. val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
  3511. if (tx_pause || rx_pause)
  3512. val |= MVNETA_GMAC_CONFIG_FLOW_CTRL;
  3513. mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
  3514. } else {
  3515. /* When inband doesn't cover flow control or flow control is
  3516. * disabled, we need to manually configure it. This bit will
  3517. * only have effect if MVNETA_GMAC_AN_FLOW_CTRL_EN is unset.
  3518. */
  3519. val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
  3520. val &= ~MVNETA_GMAC_CONFIG_FLOW_CTRL;
  3521. if (tx_pause || rx_pause)
  3522. val |= MVNETA_GMAC_CONFIG_FLOW_CTRL;
  3523. mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
  3524. }
  3525. mvneta_port_up(pp);
  3526. }
  3527. static void mvneta_mac_disable_tx_lpi(struct phylink_config *config)
  3528. {
  3529. struct mvneta_port *pp = netdev_priv(to_net_dev(config->dev));
  3530. u32 lpi1;
  3531. lpi1 = mvreg_read(pp, MVNETA_LPI_CTRL_1);
  3532. lpi1 &= ~(MVNETA_LPI_CTRL_1_REQUEST_ENABLE |
  3533. MVNETA_LPI_CTRL_1_REQUEST_FORCE |
  3534. MVNETA_LPI_CTRL_1_MANUAL_MODE);
  3535. mvreg_write(pp, MVNETA_LPI_CTRL_1, lpi1);
  3536. }
  3537. static int mvneta_mac_enable_tx_lpi(struct phylink_config *config, u32 timer,
  3538. bool tx_clk_stop)
  3539. {
  3540. struct mvneta_port *pp = netdev_priv(to_net_dev(config->dev));
  3541. u32 ts, tw, lpi0, lpi1, status;
  3542. status = mvreg_read(pp, MVNETA_GMAC_STATUS);
  3543. if (status & MVNETA_GMAC_SPEED_1000) {
  3544. /* At 1G speeds, the timer resolution are 1us, and
  3545. * 802.3 says tw is 16.5us. Round up to 17us.
  3546. */
  3547. tw = 17;
  3548. ts = timer;
  3549. } else {
  3550. /* At 100M speeds, the timer resolutions are 10us, and
  3551. * 802.3 says tw is 30us.
  3552. */
  3553. tw = 3;
  3554. ts = DIV_ROUND_UP(timer, 10);
  3555. }
  3556. if (ts > 255)
  3557. ts = 255;
  3558. /* Configure ts */
  3559. lpi0 = mvreg_read(pp, MVNETA_LPI_CTRL_0);
  3560. lpi0 = u32_replace_bits(lpi0, ts, MVNETA_LPI_CTRL_0_TS);
  3561. mvreg_write(pp, MVNETA_LPI_CTRL_0, lpi0);
  3562. /* Configure tw and enable LPI generation */
  3563. lpi1 = mvreg_read(pp, MVNETA_LPI_CTRL_1);
  3564. lpi1 = u32_replace_bits(lpi1, tw, MVNETA_LPI_CTRL_1_TW);
  3565. lpi1 |= MVNETA_LPI_CTRL_1_REQUEST_ENABLE;
  3566. mvreg_write(pp, MVNETA_LPI_CTRL_1, lpi1);
  3567. return 0;
  3568. }
  3569. static const struct phylink_mac_ops mvneta_phylink_ops = {
  3570. .mac_select_pcs = mvneta_mac_select_pcs,
  3571. .mac_prepare = mvneta_mac_prepare,
  3572. .mac_config = mvneta_mac_config,
  3573. .mac_finish = mvneta_mac_finish,
  3574. .mac_link_down = mvneta_mac_link_down,
  3575. .mac_link_up = mvneta_mac_link_up,
  3576. .mac_disable_tx_lpi = mvneta_mac_disable_tx_lpi,
  3577. .mac_enable_tx_lpi = mvneta_mac_enable_tx_lpi,
  3578. };
  3579. static int mvneta_mdio_probe(struct mvneta_port *pp)
  3580. {
  3581. struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
  3582. int err = phylink_of_phy_connect(pp->phylink, pp->dn, 0);
  3583. if (err)
  3584. netdev_err(pp->dev, "could not attach PHY: %d\n", err);
  3585. phylink_ethtool_get_wol(pp->phylink, &wol);
  3586. device_set_wakeup_capable(&pp->dev->dev, !!wol.supported);
  3587. /* PHY WoL may be enabled but device wakeup disabled */
  3588. if (wol.supported)
  3589. device_set_wakeup_enable(&pp->dev->dev, !!wol.wolopts);
  3590. return err;
  3591. }
  3592. static void mvneta_mdio_remove(struct mvneta_port *pp)
  3593. {
  3594. phylink_disconnect_phy(pp->phylink);
  3595. }
  3596. /* Electing a CPU must be done in an atomic way: it should be done
  3597. * after or before the removal/insertion of a CPU and this function is
  3598. * not reentrant.
  3599. */
  3600. static void mvneta_percpu_elect(struct mvneta_port *pp)
  3601. {
  3602. int elected_cpu = 0, max_cpu, cpu;
  3603. /* Use the cpu associated to the rxq when it is online, in all
  3604. * the other cases, use the cpu 0 which can't be offline.
  3605. */
  3606. if (pp->rxq_def < nr_cpu_ids && cpu_online(pp->rxq_def))
  3607. elected_cpu = pp->rxq_def;
  3608. max_cpu = num_present_cpus();
  3609. for_each_online_cpu(cpu) {
  3610. int rxq_map = 0, txq_map = 0;
  3611. int rxq;
  3612. for (rxq = 0; rxq < rxq_number; rxq++)
  3613. if ((rxq % max_cpu) == cpu)
  3614. rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
  3615. if (cpu == elected_cpu)
  3616. /* Map the default receive queue to the elected CPU */
  3617. rxq_map |= MVNETA_CPU_RXQ_ACCESS(pp->rxq_def);
  3618. /* We update the TX queue map only if we have one
  3619. * queue. In this case we associate the TX queue to
  3620. * the CPU bound to the default RX queue
  3621. */
  3622. if (txq_number == 1)
  3623. txq_map = (cpu == elected_cpu) ?
  3624. MVNETA_CPU_TXQ_ACCESS(0) : 0;
  3625. else
  3626. txq_map = mvreg_read(pp, MVNETA_CPU_MAP(cpu)) &
  3627. MVNETA_CPU_TXQ_ACCESS_ALL_MASK;
  3628. mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
  3629. /* Update the interrupt mask on each CPU according the
  3630. * new mapping
  3631. */
  3632. smp_call_function_single(cpu, mvneta_percpu_unmask_interrupt,
  3633. pp, true);
  3634. }
  3635. };
  3636. static int mvneta_cpu_online(unsigned int cpu, struct hlist_node *node)
  3637. {
  3638. int other_cpu;
  3639. struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
  3640. node_online);
  3641. struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
  3642. /* Armada 3700's per-cpu interrupt for mvneta is broken, all interrupts
  3643. * are routed to CPU 0, so we don't need all the cpu-hotplug support
  3644. */
  3645. if (pp->neta_armada3700)
  3646. return 0;
  3647. netdev_lock(port->napi.dev);
  3648. spin_lock(&pp->lock);
  3649. /*
  3650. * Configuring the driver for a new CPU while the driver is
  3651. * stopping is racy, so just avoid it.
  3652. */
  3653. if (pp->is_stopped) {
  3654. spin_unlock(&pp->lock);
  3655. netdev_unlock(port->napi.dev);
  3656. return 0;
  3657. }
  3658. netif_tx_stop_all_queues(pp->dev);
  3659. /*
  3660. * We have to synchronise on tha napi of each CPU except the one
  3661. * just being woken up
  3662. */
  3663. for_each_online_cpu(other_cpu) {
  3664. if (other_cpu != cpu) {
  3665. struct mvneta_pcpu_port *other_port =
  3666. per_cpu_ptr(pp->ports, other_cpu);
  3667. napi_synchronize(&other_port->napi);
  3668. }
  3669. }
  3670. /* Mask all ethernet port interrupts */
  3671. on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
  3672. napi_enable_locked(&port->napi);
  3673. /*
  3674. * Enable per-CPU interrupts on the CPU that is
  3675. * brought up.
  3676. */
  3677. mvneta_percpu_enable(pp);
  3678. /*
  3679. * Enable per-CPU interrupt on the one CPU we care
  3680. * about.
  3681. */
  3682. mvneta_percpu_elect(pp);
  3683. /* Unmask all ethernet port interrupts */
  3684. on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
  3685. mvreg_write(pp, MVNETA_INTR_MISC_MASK,
  3686. MVNETA_CAUSE_PHY_STATUS_CHANGE |
  3687. MVNETA_CAUSE_LINK_CHANGE);
  3688. netif_tx_start_all_queues(pp->dev);
  3689. spin_unlock(&pp->lock);
  3690. netdev_unlock(port->napi.dev);
  3691. return 0;
  3692. }
  3693. static int mvneta_cpu_down_prepare(unsigned int cpu, struct hlist_node *node)
  3694. {
  3695. struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
  3696. node_online);
  3697. struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
  3698. /*
  3699. * Thanks to this lock we are sure that any pending cpu election is
  3700. * done.
  3701. */
  3702. spin_lock(&pp->lock);
  3703. /* Mask all ethernet port interrupts */
  3704. on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
  3705. spin_unlock(&pp->lock);
  3706. napi_synchronize(&port->napi);
  3707. napi_disable(&port->napi);
  3708. /* Disable per-CPU interrupts on the CPU that is brought down. */
  3709. mvneta_percpu_disable(pp);
  3710. return 0;
  3711. }
  3712. static int mvneta_cpu_dead(unsigned int cpu, struct hlist_node *node)
  3713. {
  3714. struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
  3715. node_dead);
  3716. /* Check if a new CPU must be elected now this on is down */
  3717. spin_lock(&pp->lock);
  3718. mvneta_percpu_elect(pp);
  3719. spin_unlock(&pp->lock);
  3720. /* Unmask all ethernet port interrupts */
  3721. on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
  3722. mvreg_write(pp, MVNETA_INTR_MISC_MASK,
  3723. MVNETA_CAUSE_PHY_STATUS_CHANGE |
  3724. MVNETA_CAUSE_LINK_CHANGE);
  3725. netif_tx_start_all_queues(pp->dev);
  3726. return 0;
  3727. }
  3728. static int mvneta_open(struct net_device *dev)
  3729. {
  3730. struct mvneta_port *pp = netdev_priv(dev);
  3731. int ret;
  3732. pp->pkt_size = MVNETA_RX_PKT_SIZE(pp->dev->mtu);
  3733. ret = mvneta_setup_rxqs(pp);
  3734. if (ret)
  3735. return ret;
  3736. ret = mvneta_setup_txqs(pp);
  3737. if (ret)
  3738. goto err_cleanup_rxqs;
  3739. /* Connect to port interrupt line */
  3740. if (pp->neta_armada3700)
  3741. ret = request_irq(pp->dev->irq, mvneta_isr, 0,
  3742. dev->name, pp);
  3743. else
  3744. ret = request_percpu_irq(pp->dev->irq, mvneta_percpu_isr,
  3745. dev->name, pp->ports);
  3746. if (ret) {
  3747. netdev_err(pp->dev, "cannot request irq %d\n", pp->dev->irq);
  3748. goto err_cleanup_txqs;
  3749. }
  3750. if (!pp->neta_armada3700) {
  3751. /* Enable per-CPU interrupt on all the CPU to handle our RX
  3752. * queue interrupts
  3753. */
  3754. on_each_cpu(mvneta_percpu_enable, pp, true);
  3755. pp->is_stopped = false;
  3756. /* Register a CPU notifier to handle the case where our CPU
  3757. * might be taken offline.
  3758. */
  3759. ret = cpuhp_state_add_instance_nocalls(online_hpstate,
  3760. &pp->node_online);
  3761. if (ret)
  3762. goto err_free_irq;
  3763. ret = cpuhp_state_add_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
  3764. &pp->node_dead);
  3765. if (ret)
  3766. goto err_free_online_hp;
  3767. }
  3768. ret = mvneta_mdio_probe(pp);
  3769. if (ret < 0) {
  3770. netdev_err(dev, "cannot probe MDIO bus\n");
  3771. goto err_free_dead_hp;
  3772. }
  3773. mvneta_start_dev(pp);
  3774. return 0;
  3775. err_free_dead_hp:
  3776. if (!pp->neta_armada3700)
  3777. cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
  3778. &pp->node_dead);
  3779. err_free_online_hp:
  3780. if (!pp->neta_armada3700)
  3781. cpuhp_state_remove_instance_nocalls(online_hpstate,
  3782. &pp->node_online);
  3783. err_free_irq:
  3784. if (pp->neta_armada3700) {
  3785. free_irq(pp->dev->irq, pp);
  3786. } else {
  3787. on_each_cpu(mvneta_percpu_disable, pp, true);
  3788. free_percpu_irq(pp->dev->irq, pp->ports);
  3789. }
  3790. err_cleanup_txqs:
  3791. mvneta_cleanup_txqs(pp);
  3792. err_cleanup_rxqs:
  3793. mvneta_cleanup_rxqs(pp);
  3794. return ret;
  3795. }
  3796. /* Stop the port, free port interrupt line */
  3797. static int mvneta_stop(struct net_device *dev)
  3798. {
  3799. struct mvneta_port *pp = netdev_priv(dev);
  3800. if (!pp->neta_armada3700) {
  3801. /* Inform that we are stopping so we don't want to setup the
  3802. * driver for new CPUs in the notifiers. The code of the
  3803. * notifier for CPU online is protected by the same spinlock,
  3804. * so when we get the lock, the notifier work is done.
  3805. */
  3806. spin_lock(&pp->lock);
  3807. pp->is_stopped = true;
  3808. spin_unlock(&pp->lock);
  3809. mvneta_stop_dev(pp);
  3810. mvneta_mdio_remove(pp);
  3811. cpuhp_state_remove_instance_nocalls(online_hpstate,
  3812. &pp->node_online);
  3813. cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
  3814. &pp->node_dead);
  3815. on_each_cpu(mvneta_percpu_disable, pp, true);
  3816. free_percpu_irq(dev->irq, pp->ports);
  3817. } else {
  3818. mvneta_stop_dev(pp);
  3819. mvneta_mdio_remove(pp);
  3820. free_irq(dev->irq, pp);
  3821. }
  3822. mvneta_cleanup_rxqs(pp);
  3823. mvneta_cleanup_txqs(pp);
  3824. return 0;
  3825. }
  3826. static int mvneta_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  3827. {
  3828. struct mvneta_port *pp = netdev_priv(dev);
  3829. return phylink_mii_ioctl(pp->phylink, ifr, cmd);
  3830. }
  3831. static int mvneta_xdp_setup(struct net_device *dev, struct bpf_prog *prog,
  3832. struct netlink_ext_ack *extack)
  3833. {
  3834. bool need_update, running = netif_running(dev);
  3835. struct mvneta_port *pp = netdev_priv(dev);
  3836. struct bpf_prog *old_prog;
  3837. if (prog && !prog->aux->xdp_has_frags &&
  3838. dev->mtu > MVNETA_MAX_RX_BUF_SIZE) {
  3839. NL_SET_ERR_MSG_MOD(extack, "prog does not support XDP frags");
  3840. return -EOPNOTSUPP;
  3841. }
  3842. if (pp->bm_priv) {
  3843. NL_SET_ERR_MSG_MOD(extack,
  3844. "Hardware Buffer Management not supported on XDP");
  3845. return -EOPNOTSUPP;
  3846. }
  3847. need_update = !!pp->xdp_prog != !!prog;
  3848. if (running && need_update)
  3849. mvneta_stop(dev);
  3850. old_prog = xchg(&pp->xdp_prog, prog);
  3851. if (old_prog)
  3852. bpf_prog_put(old_prog);
  3853. if (running && need_update)
  3854. return mvneta_open(dev);
  3855. return 0;
  3856. }
  3857. static int mvneta_xdp(struct net_device *dev, struct netdev_bpf *xdp)
  3858. {
  3859. switch (xdp->command) {
  3860. case XDP_SETUP_PROG:
  3861. return mvneta_xdp_setup(dev, xdp->prog, xdp->extack);
  3862. default:
  3863. return -EINVAL;
  3864. }
  3865. }
  3866. /* Ethtool methods */
  3867. /* Set link ksettings (phy address, speed) for ethtools */
  3868. static int
  3869. mvneta_ethtool_set_link_ksettings(struct net_device *ndev,
  3870. const struct ethtool_link_ksettings *cmd)
  3871. {
  3872. struct mvneta_port *pp = netdev_priv(ndev);
  3873. return phylink_ethtool_ksettings_set(pp->phylink, cmd);
  3874. }
  3875. /* Get link ksettings for ethtools */
  3876. static int
  3877. mvneta_ethtool_get_link_ksettings(struct net_device *ndev,
  3878. struct ethtool_link_ksettings *cmd)
  3879. {
  3880. struct mvneta_port *pp = netdev_priv(ndev);
  3881. return phylink_ethtool_ksettings_get(pp->phylink, cmd);
  3882. }
  3883. static int mvneta_ethtool_nway_reset(struct net_device *dev)
  3884. {
  3885. struct mvneta_port *pp = netdev_priv(dev);
  3886. return phylink_ethtool_nway_reset(pp->phylink);
  3887. }
  3888. /* Set interrupt coalescing for ethtools */
  3889. static int
  3890. mvneta_ethtool_set_coalesce(struct net_device *dev,
  3891. struct ethtool_coalesce *c,
  3892. struct kernel_ethtool_coalesce *kernel_coal,
  3893. struct netlink_ext_ack *extack)
  3894. {
  3895. struct mvneta_port *pp = netdev_priv(dev);
  3896. int queue;
  3897. for (queue = 0; queue < rxq_number; queue++) {
  3898. struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
  3899. rxq->time_coal = c->rx_coalesce_usecs;
  3900. rxq->pkts_coal = c->rx_max_coalesced_frames;
  3901. mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
  3902. mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
  3903. }
  3904. for (queue = 0; queue < txq_number; queue++) {
  3905. struct mvneta_tx_queue *txq = &pp->txqs[queue];
  3906. txq->done_pkts_coal = c->tx_max_coalesced_frames;
  3907. mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
  3908. }
  3909. return 0;
  3910. }
  3911. /* get coalescing for ethtools */
  3912. static int
  3913. mvneta_ethtool_get_coalesce(struct net_device *dev,
  3914. struct ethtool_coalesce *c,
  3915. struct kernel_ethtool_coalesce *kernel_coal,
  3916. struct netlink_ext_ack *extack)
  3917. {
  3918. struct mvneta_port *pp = netdev_priv(dev);
  3919. c->rx_coalesce_usecs = pp->rxqs[0].time_coal;
  3920. c->rx_max_coalesced_frames = pp->rxqs[0].pkts_coal;
  3921. c->tx_max_coalesced_frames = pp->txqs[0].done_pkts_coal;
  3922. return 0;
  3923. }
  3924. static void mvneta_ethtool_get_drvinfo(struct net_device *dev,
  3925. struct ethtool_drvinfo *drvinfo)
  3926. {
  3927. strscpy(drvinfo->driver, MVNETA_DRIVER_NAME,
  3928. sizeof(drvinfo->driver));
  3929. strscpy(drvinfo->version, MVNETA_DRIVER_VERSION,
  3930. sizeof(drvinfo->version));
  3931. strscpy(drvinfo->bus_info, dev_name(&dev->dev),
  3932. sizeof(drvinfo->bus_info));
  3933. }
  3934. static void
  3935. mvneta_ethtool_get_ringparam(struct net_device *netdev,
  3936. struct ethtool_ringparam *ring,
  3937. struct kernel_ethtool_ringparam *kernel_ring,
  3938. struct netlink_ext_ack *extack)
  3939. {
  3940. struct mvneta_port *pp = netdev_priv(netdev);
  3941. ring->rx_max_pending = MVNETA_MAX_RXD;
  3942. ring->tx_max_pending = MVNETA_MAX_TXD;
  3943. ring->rx_pending = pp->rx_ring_size;
  3944. ring->tx_pending = pp->tx_ring_size;
  3945. }
  3946. static int
  3947. mvneta_ethtool_set_ringparam(struct net_device *dev,
  3948. struct ethtool_ringparam *ring,
  3949. struct kernel_ethtool_ringparam *kernel_ring,
  3950. struct netlink_ext_ack *extack)
  3951. {
  3952. struct mvneta_port *pp = netdev_priv(dev);
  3953. if ((ring->rx_pending == 0) || (ring->tx_pending == 0))
  3954. return -EINVAL;
  3955. pp->rx_ring_size = ring->rx_pending < MVNETA_MAX_RXD ?
  3956. ring->rx_pending : MVNETA_MAX_RXD;
  3957. pp->tx_ring_size = clamp_t(u16, ring->tx_pending,
  3958. MVNETA_MAX_SKB_DESCS * 2, MVNETA_MAX_TXD);
  3959. if (pp->tx_ring_size != ring->tx_pending)
  3960. netdev_warn(dev, "TX queue size set to %u (requested %u)\n",
  3961. pp->tx_ring_size, ring->tx_pending);
  3962. if (netif_running(dev)) {
  3963. mvneta_stop(dev);
  3964. if (mvneta_open(dev)) {
  3965. netdev_err(dev,
  3966. "error on opening device after ring param change\n");
  3967. return -ENOMEM;
  3968. }
  3969. }
  3970. return 0;
  3971. }
  3972. static void mvneta_ethtool_get_pauseparam(struct net_device *dev,
  3973. struct ethtool_pauseparam *pause)
  3974. {
  3975. struct mvneta_port *pp = netdev_priv(dev);
  3976. phylink_ethtool_get_pauseparam(pp->phylink, pause);
  3977. }
  3978. static int mvneta_ethtool_set_pauseparam(struct net_device *dev,
  3979. struct ethtool_pauseparam *pause)
  3980. {
  3981. struct mvneta_port *pp = netdev_priv(dev);
  3982. return phylink_ethtool_set_pauseparam(pp->phylink, pause);
  3983. }
  3984. static void mvneta_ethtool_get_strings(struct net_device *netdev, u32 sset,
  3985. u8 *data)
  3986. {
  3987. if (sset == ETH_SS_STATS) {
  3988. struct mvneta_port *pp = netdev_priv(netdev);
  3989. int i;
  3990. for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
  3991. ethtool_puts(&data, mvneta_statistics[i].name);
  3992. if (!pp->bm_priv) {
  3993. page_pool_ethtool_stats_get_strings(data);
  3994. }
  3995. }
  3996. }
  3997. static void
  3998. mvneta_ethtool_update_pcpu_stats(struct mvneta_port *pp,
  3999. struct mvneta_ethtool_stats *es)
  4000. {
  4001. unsigned int start;
  4002. int cpu;
  4003. for_each_possible_cpu(cpu) {
  4004. struct mvneta_pcpu_stats *stats;
  4005. u64 skb_alloc_error;
  4006. u64 refill_error;
  4007. u64 xdp_redirect;
  4008. u64 xdp_xmit_err;
  4009. u64 xdp_tx_err;
  4010. u64 xdp_pass;
  4011. u64 xdp_drop;
  4012. u64 xdp_xmit;
  4013. u64 xdp_tx;
  4014. stats = per_cpu_ptr(pp->stats, cpu);
  4015. do {
  4016. start = u64_stats_fetch_begin(&stats->syncp);
  4017. skb_alloc_error = stats->es.skb_alloc_error;
  4018. refill_error = stats->es.refill_error;
  4019. xdp_redirect = stats->es.ps.xdp_redirect;
  4020. xdp_pass = stats->es.ps.xdp_pass;
  4021. xdp_drop = stats->es.ps.xdp_drop;
  4022. xdp_xmit = stats->es.ps.xdp_xmit;
  4023. xdp_xmit_err = stats->es.ps.xdp_xmit_err;
  4024. xdp_tx = stats->es.ps.xdp_tx;
  4025. xdp_tx_err = stats->es.ps.xdp_tx_err;
  4026. } while (u64_stats_fetch_retry(&stats->syncp, start));
  4027. es->skb_alloc_error += skb_alloc_error;
  4028. es->refill_error += refill_error;
  4029. es->ps.xdp_redirect += xdp_redirect;
  4030. es->ps.xdp_pass += xdp_pass;
  4031. es->ps.xdp_drop += xdp_drop;
  4032. es->ps.xdp_xmit += xdp_xmit;
  4033. es->ps.xdp_xmit_err += xdp_xmit_err;
  4034. es->ps.xdp_tx += xdp_tx;
  4035. es->ps.xdp_tx_err += xdp_tx_err;
  4036. }
  4037. }
  4038. static void mvneta_ethtool_update_stats(struct mvneta_port *pp)
  4039. {
  4040. struct mvneta_ethtool_stats stats = {};
  4041. const struct mvneta_statistic *s;
  4042. void __iomem *base = pp->base;
  4043. u32 high, low;
  4044. u64 val;
  4045. int i;
  4046. mvneta_ethtool_update_pcpu_stats(pp, &stats);
  4047. for (i = 0, s = mvneta_statistics;
  4048. s < mvneta_statistics + ARRAY_SIZE(mvneta_statistics);
  4049. s++, i++) {
  4050. switch (s->type) {
  4051. case T_REG_32:
  4052. val = readl_relaxed(base + s->offset);
  4053. pp->ethtool_stats[i] += val;
  4054. break;
  4055. case T_REG_64:
  4056. /* Docs say to read low 32-bit then high */
  4057. low = readl_relaxed(base + s->offset);
  4058. high = readl_relaxed(base + s->offset + 4);
  4059. val = (u64)high << 32 | low;
  4060. pp->ethtool_stats[i] += val;
  4061. break;
  4062. case T_SW:
  4063. switch (s->offset) {
  4064. case ETHTOOL_STAT_EEE_WAKEUP:
  4065. val = phylink_get_eee_err(pp->phylink);
  4066. pp->ethtool_stats[i] += val;
  4067. break;
  4068. case ETHTOOL_STAT_SKB_ALLOC_ERR:
  4069. pp->ethtool_stats[i] = stats.skb_alloc_error;
  4070. break;
  4071. case ETHTOOL_STAT_REFILL_ERR:
  4072. pp->ethtool_stats[i] = stats.refill_error;
  4073. break;
  4074. case ETHTOOL_XDP_REDIRECT:
  4075. pp->ethtool_stats[i] = stats.ps.xdp_redirect;
  4076. break;
  4077. case ETHTOOL_XDP_PASS:
  4078. pp->ethtool_stats[i] = stats.ps.xdp_pass;
  4079. break;
  4080. case ETHTOOL_XDP_DROP:
  4081. pp->ethtool_stats[i] = stats.ps.xdp_drop;
  4082. break;
  4083. case ETHTOOL_XDP_TX:
  4084. pp->ethtool_stats[i] = stats.ps.xdp_tx;
  4085. break;
  4086. case ETHTOOL_XDP_TX_ERR:
  4087. pp->ethtool_stats[i] = stats.ps.xdp_tx_err;
  4088. break;
  4089. case ETHTOOL_XDP_XMIT:
  4090. pp->ethtool_stats[i] = stats.ps.xdp_xmit;
  4091. break;
  4092. case ETHTOOL_XDP_XMIT_ERR:
  4093. pp->ethtool_stats[i] = stats.ps.xdp_xmit_err;
  4094. break;
  4095. }
  4096. break;
  4097. }
  4098. }
  4099. }
  4100. static void mvneta_ethtool_pp_stats(struct mvneta_port *pp, u64 *data)
  4101. {
  4102. struct page_pool_stats stats = {};
  4103. int i;
  4104. for (i = 0; i < rxq_number; i++) {
  4105. if (pp->rxqs[i].page_pool)
  4106. page_pool_get_stats(pp->rxqs[i].page_pool, &stats);
  4107. }
  4108. page_pool_ethtool_stats_get(data, &stats);
  4109. }
  4110. static void mvneta_ethtool_get_stats(struct net_device *dev,
  4111. struct ethtool_stats *stats, u64 *data)
  4112. {
  4113. struct mvneta_port *pp = netdev_priv(dev);
  4114. int i;
  4115. mvneta_ethtool_update_stats(pp);
  4116. for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
  4117. *data++ = pp->ethtool_stats[i];
  4118. if (!pp->bm_priv)
  4119. mvneta_ethtool_pp_stats(pp, data);
  4120. }
  4121. static int mvneta_ethtool_get_sset_count(struct net_device *dev, int sset)
  4122. {
  4123. if (sset == ETH_SS_STATS) {
  4124. int count = ARRAY_SIZE(mvneta_statistics);
  4125. struct mvneta_port *pp = netdev_priv(dev);
  4126. if (!pp->bm_priv)
  4127. count += page_pool_ethtool_stats_get_count();
  4128. return count;
  4129. }
  4130. return -EOPNOTSUPP;
  4131. }
  4132. static u32 mvneta_ethtool_get_rxfh_indir_size(struct net_device *dev)
  4133. {
  4134. return MVNETA_RSS_LU_TABLE_SIZE;
  4135. }
  4136. static u32 mvneta_ethtool_get_rx_ring_count(struct net_device *dev)
  4137. {
  4138. return rxq_number;
  4139. }
  4140. static int mvneta_config_rss(struct mvneta_port *pp)
  4141. {
  4142. int cpu;
  4143. u32 val;
  4144. netif_tx_stop_all_queues(pp->dev);
  4145. on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
  4146. if (!pp->neta_armada3700) {
  4147. /* We have to synchronise on the napi of each CPU */
  4148. for_each_online_cpu(cpu) {
  4149. struct mvneta_pcpu_port *pcpu_port =
  4150. per_cpu_ptr(pp->ports, cpu);
  4151. napi_synchronize(&pcpu_port->napi);
  4152. napi_disable(&pcpu_port->napi);
  4153. }
  4154. } else {
  4155. napi_synchronize(&pp->napi);
  4156. napi_disable(&pp->napi);
  4157. }
  4158. pp->rxq_def = pp->indir[0];
  4159. /* Update unicast mapping */
  4160. mvneta_set_rx_mode(pp->dev);
  4161. /* Update val of portCfg register accordingly with all RxQueue types */
  4162. val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
  4163. mvreg_write(pp, MVNETA_PORT_CONFIG, val);
  4164. /* Update the elected CPU matching the new rxq_def */
  4165. spin_lock(&pp->lock);
  4166. mvneta_percpu_elect(pp);
  4167. spin_unlock(&pp->lock);
  4168. if (!pp->neta_armada3700) {
  4169. /* We have to synchronise on the napi of each CPU */
  4170. for_each_online_cpu(cpu) {
  4171. struct mvneta_pcpu_port *pcpu_port =
  4172. per_cpu_ptr(pp->ports, cpu);
  4173. napi_enable(&pcpu_port->napi);
  4174. }
  4175. } else {
  4176. napi_enable(&pp->napi);
  4177. }
  4178. netif_tx_start_all_queues(pp->dev);
  4179. return 0;
  4180. }
  4181. static int mvneta_ethtool_set_rxfh(struct net_device *dev,
  4182. struct ethtool_rxfh_param *rxfh,
  4183. struct netlink_ext_ack *extack)
  4184. {
  4185. struct mvneta_port *pp = netdev_priv(dev);
  4186. /* Current code for Armada 3700 doesn't support RSS features yet */
  4187. if (pp->neta_armada3700)
  4188. return -EOPNOTSUPP;
  4189. /* We require at least one supported parameter to be changed
  4190. * and no change in any of the unsupported parameters
  4191. */
  4192. if (rxfh->key ||
  4193. (rxfh->hfunc != ETH_RSS_HASH_NO_CHANGE &&
  4194. rxfh->hfunc != ETH_RSS_HASH_TOP))
  4195. return -EOPNOTSUPP;
  4196. if (!rxfh->indir)
  4197. return 0;
  4198. memcpy(pp->indir, rxfh->indir, MVNETA_RSS_LU_TABLE_SIZE);
  4199. return mvneta_config_rss(pp);
  4200. }
  4201. static int mvneta_ethtool_get_rxfh(struct net_device *dev,
  4202. struct ethtool_rxfh_param *rxfh)
  4203. {
  4204. struct mvneta_port *pp = netdev_priv(dev);
  4205. /* Current code for Armada 3700 doesn't support RSS features yet */
  4206. if (pp->neta_armada3700)
  4207. return -EOPNOTSUPP;
  4208. rxfh->hfunc = ETH_RSS_HASH_TOP;
  4209. if (!rxfh->indir)
  4210. return 0;
  4211. memcpy(rxfh->indir, pp->indir, MVNETA_RSS_LU_TABLE_SIZE);
  4212. return 0;
  4213. }
  4214. static void mvneta_ethtool_get_wol(struct net_device *dev,
  4215. struct ethtool_wolinfo *wol)
  4216. {
  4217. struct mvneta_port *pp = netdev_priv(dev);
  4218. phylink_ethtool_get_wol(pp->phylink, wol);
  4219. }
  4220. static int mvneta_ethtool_set_wol(struct net_device *dev,
  4221. struct ethtool_wolinfo *wol)
  4222. {
  4223. struct mvneta_port *pp = netdev_priv(dev);
  4224. int ret;
  4225. ret = phylink_ethtool_set_wol(pp->phylink, wol);
  4226. if (!ret)
  4227. device_set_wakeup_enable(&dev->dev, !!wol->wolopts);
  4228. return ret;
  4229. }
  4230. static int mvneta_ethtool_get_eee(struct net_device *dev,
  4231. struct ethtool_keee *eee)
  4232. {
  4233. struct mvneta_port *pp = netdev_priv(dev);
  4234. return phylink_ethtool_get_eee(pp->phylink, eee);
  4235. }
  4236. static int mvneta_ethtool_set_eee(struct net_device *dev,
  4237. struct ethtool_keee *eee)
  4238. {
  4239. struct mvneta_port *pp = netdev_priv(dev);
  4240. /* The Armada 37x documents do not give limits for this other than
  4241. * it being an 8-bit register.
  4242. */
  4243. if (eee->tx_lpi_enabled && eee->tx_lpi_timer > 255)
  4244. return -EINVAL;
  4245. return phylink_ethtool_set_eee(pp->phylink, eee);
  4246. }
  4247. static void mvneta_clear_rx_prio_map(struct mvneta_port *pp)
  4248. {
  4249. mvreg_write(pp, MVNETA_VLAN_PRIO_TO_RXQ, 0);
  4250. }
  4251. static void mvneta_map_vlan_prio_to_rxq(struct mvneta_port *pp, u8 pri, u8 rxq)
  4252. {
  4253. u32 val = mvreg_read(pp, MVNETA_VLAN_PRIO_TO_RXQ);
  4254. val &= ~MVNETA_VLAN_PRIO_RXQ_MAP(pri, 0x7);
  4255. val |= MVNETA_VLAN_PRIO_RXQ_MAP(pri, rxq);
  4256. mvreg_write(pp, MVNETA_VLAN_PRIO_TO_RXQ, val);
  4257. }
  4258. static int mvneta_enable_per_queue_rate_limit(struct mvneta_port *pp)
  4259. {
  4260. unsigned long core_clk_rate;
  4261. u32 refill_cycles;
  4262. u32 val;
  4263. core_clk_rate = clk_get_rate(pp->clk);
  4264. if (!core_clk_rate)
  4265. return -EINVAL;
  4266. refill_cycles = MVNETA_TXQ_BUCKET_REFILL_BASE_PERIOD_NS /
  4267. (NSEC_PER_SEC / core_clk_rate);
  4268. if (refill_cycles > MVNETA_REFILL_MAX_NUM_CLK)
  4269. return -EINVAL;
  4270. /* Enable bw limit algorithm version 3 */
  4271. val = mvreg_read(pp, MVNETA_TXQ_CMD1_REG);
  4272. val &= ~(MVNETA_TXQ_CMD1_BW_LIM_SEL_V1 | MVNETA_TXQ_CMD1_BW_LIM_EN);
  4273. mvreg_write(pp, MVNETA_TXQ_CMD1_REG, val);
  4274. /* Set the base refill rate */
  4275. mvreg_write(pp, MVNETA_REFILL_NUM_CLK_REG, refill_cycles);
  4276. return 0;
  4277. }
  4278. static void mvneta_disable_per_queue_rate_limit(struct mvneta_port *pp)
  4279. {
  4280. u32 val = mvreg_read(pp, MVNETA_TXQ_CMD1_REG);
  4281. val |= (MVNETA_TXQ_CMD1_BW_LIM_SEL_V1 | MVNETA_TXQ_CMD1_BW_LIM_EN);
  4282. mvreg_write(pp, MVNETA_TXQ_CMD1_REG, val);
  4283. }
  4284. static int mvneta_setup_queue_rates(struct mvneta_port *pp, int queue,
  4285. u64 min_rate, u64 max_rate)
  4286. {
  4287. u32 refill_val, rem;
  4288. u32 val = 0;
  4289. /* Convert to from Bps to bps */
  4290. max_rate *= 8;
  4291. if (min_rate)
  4292. return -EINVAL;
  4293. refill_val = div_u64_rem(max_rate, MVNETA_TXQ_RATE_LIMIT_RESOLUTION,
  4294. &rem);
  4295. if (rem || !refill_val ||
  4296. refill_val > MVNETA_TXQ_BUCKET_REFILL_VALUE_MAX)
  4297. return -EINVAL;
  4298. val = refill_val;
  4299. val |= (MVNETA_TXQ_BUCKET_REFILL_PERIOD <<
  4300. MVNETA_TXQ_BUCKET_REFILL_PERIOD_SHIFT);
  4301. mvreg_write(pp, MVNETA_TXQ_BUCKET_REFILL_REG(queue), val);
  4302. return 0;
  4303. }
  4304. static int mvneta_setup_mqprio(struct net_device *dev,
  4305. struct tc_mqprio_qopt_offload *mqprio)
  4306. {
  4307. struct mvneta_port *pp = netdev_priv(dev);
  4308. int rxq, txq, tc, ret;
  4309. u8 num_tc;
  4310. if (mqprio->qopt.hw != TC_MQPRIO_HW_OFFLOAD_TCS)
  4311. return 0;
  4312. num_tc = mqprio->qopt.num_tc;
  4313. if (num_tc > rxq_number)
  4314. return -EINVAL;
  4315. mvneta_clear_rx_prio_map(pp);
  4316. if (!num_tc) {
  4317. mvneta_disable_per_queue_rate_limit(pp);
  4318. netdev_reset_tc(dev);
  4319. return 0;
  4320. }
  4321. netdev_set_num_tc(dev, mqprio->qopt.num_tc);
  4322. for (tc = 0; tc < mqprio->qopt.num_tc; tc++) {
  4323. netdev_set_tc_queue(dev, tc, mqprio->qopt.count[tc],
  4324. mqprio->qopt.offset[tc]);
  4325. for (rxq = mqprio->qopt.offset[tc];
  4326. rxq < mqprio->qopt.count[tc] + mqprio->qopt.offset[tc];
  4327. rxq++) {
  4328. if (rxq >= rxq_number)
  4329. return -EINVAL;
  4330. mvneta_map_vlan_prio_to_rxq(pp, tc, rxq);
  4331. }
  4332. }
  4333. if (mqprio->shaper != TC_MQPRIO_SHAPER_BW_RATE) {
  4334. mvneta_disable_per_queue_rate_limit(pp);
  4335. return 0;
  4336. }
  4337. if (mqprio->qopt.num_tc > txq_number)
  4338. return -EINVAL;
  4339. ret = mvneta_enable_per_queue_rate_limit(pp);
  4340. if (ret)
  4341. return ret;
  4342. for (tc = 0; tc < mqprio->qopt.num_tc; tc++) {
  4343. for (txq = mqprio->qopt.offset[tc];
  4344. txq < mqprio->qopt.count[tc] + mqprio->qopt.offset[tc];
  4345. txq++) {
  4346. if (txq >= txq_number)
  4347. return -EINVAL;
  4348. ret = mvneta_setup_queue_rates(pp, txq,
  4349. mqprio->min_rate[tc],
  4350. mqprio->max_rate[tc]);
  4351. if (ret)
  4352. return ret;
  4353. }
  4354. }
  4355. return 0;
  4356. }
  4357. static int mvneta_setup_tc(struct net_device *dev, enum tc_setup_type type,
  4358. void *type_data)
  4359. {
  4360. switch (type) {
  4361. case TC_SETUP_QDISC_MQPRIO:
  4362. return mvneta_setup_mqprio(dev, type_data);
  4363. default:
  4364. return -EOPNOTSUPP;
  4365. }
  4366. }
  4367. static const struct net_device_ops mvneta_netdev_ops = {
  4368. .ndo_open = mvneta_open,
  4369. .ndo_stop = mvneta_stop,
  4370. .ndo_start_xmit = mvneta_tx,
  4371. .ndo_set_rx_mode = mvneta_set_rx_mode,
  4372. .ndo_set_mac_address = mvneta_set_mac_addr,
  4373. .ndo_change_mtu = mvneta_change_mtu,
  4374. .ndo_fix_features = mvneta_fix_features,
  4375. .ndo_get_stats64 = mvneta_get_stats64,
  4376. .ndo_eth_ioctl = mvneta_ioctl,
  4377. .ndo_bpf = mvneta_xdp,
  4378. .ndo_xdp_xmit = mvneta_xdp_xmit,
  4379. .ndo_setup_tc = mvneta_setup_tc,
  4380. };
  4381. static const struct ethtool_ops mvneta_eth_tool_ops = {
  4382. .supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS |
  4383. ETHTOOL_COALESCE_MAX_FRAMES,
  4384. .nway_reset = mvneta_ethtool_nway_reset,
  4385. .get_link = ethtool_op_get_link,
  4386. .set_coalesce = mvneta_ethtool_set_coalesce,
  4387. .get_coalesce = mvneta_ethtool_get_coalesce,
  4388. .get_drvinfo = mvneta_ethtool_get_drvinfo,
  4389. .get_ringparam = mvneta_ethtool_get_ringparam,
  4390. .set_ringparam = mvneta_ethtool_set_ringparam,
  4391. .get_pauseparam = mvneta_ethtool_get_pauseparam,
  4392. .set_pauseparam = mvneta_ethtool_set_pauseparam,
  4393. .get_strings = mvneta_ethtool_get_strings,
  4394. .get_ethtool_stats = mvneta_ethtool_get_stats,
  4395. .get_sset_count = mvneta_ethtool_get_sset_count,
  4396. .get_rxfh_indir_size = mvneta_ethtool_get_rxfh_indir_size,
  4397. .get_rx_ring_count = mvneta_ethtool_get_rx_ring_count,
  4398. .get_rxfh = mvneta_ethtool_get_rxfh,
  4399. .set_rxfh = mvneta_ethtool_set_rxfh,
  4400. .get_link_ksettings = mvneta_ethtool_get_link_ksettings,
  4401. .set_link_ksettings = mvneta_ethtool_set_link_ksettings,
  4402. .get_wol = mvneta_ethtool_get_wol,
  4403. .set_wol = mvneta_ethtool_set_wol,
  4404. .get_ts_info = ethtool_op_get_ts_info,
  4405. .get_eee = mvneta_ethtool_get_eee,
  4406. .set_eee = mvneta_ethtool_set_eee,
  4407. };
  4408. /* Initialize hw */
  4409. static int mvneta_init(struct device *dev, struct mvneta_port *pp)
  4410. {
  4411. int queue;
  4412. /* Disable port */
  4413. mvneta_port_disable(pp);
  4414. /* Set port default values */
  4415. mvneta_defaults_set(pp);
  4416. pp->txqs = devm_kcalloc(dev, txq_number, sizeof(*pp->txqs), GFP_KERNEL);
  4417. if (!pp->txqs)
  4418. return -ENOMEM;
  4419. /* Initialize TX descriptor rings */
  4420. for (queue = 0; queue < txq_number; queue++) {
  4421. struct mvneta_tx_queue *txq = &pp->txqs[queue];
  4422. txq->id = queue;
  4423. txq->size = pp->tx_ring_size;
  4424. txq->done_pkts_coal = MVNETA_TXDONE_COAL_PKTS;
  4425. }
  4426. pp->rxqs = devm_kcalloc(dev, rxq_number, sizeof(*pp->rxqs), GFP_KERNEL);
  4427. if (!pp->rxqs)
  4428. return -ENOMEM;
  4429. /* Create Rx descriptor rings */
  4430. for (queue = 0; queue < rxq_number; queue++) {
  4431. struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
  4432. rxq->id = queue;
  4433. rxq->size = pp->rx_ring_size;
  4434. rxq->pkts_coal = MVNETA_RX_COAL_PKTS;
  4435. rxq->time_coal = MVNETA_RX_COAL_USEC;
  4436. rxq->buf_virt_addr
  4437. = devm_kmalloc_array(pp->dev->dev.parent,
  4438. rxq->size,
  4439. sizeof(*rxq->buf_virt_addr),
  4440. GFP_KERNEL);
  4441. if (!rxq->buf_virt_addr)
  4442. return -ENOMEM;
  4443. }
  4444. return 0;
  4445. }
  4446. /* platform glue : initialize decoding windows */
  4447. static void mvneta_conf_mbus_windows(struct mvneta_port *pp,
  4448. const struct mbus_dram_target_info *dram)
  4449. {
  4450. u32 win_enable;
  4451. u32 win_protect;
  4452. int i;
  4453. for (i = 0; i < 6; i++) {
  4454. mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
  4455. mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
  4456. if (i < 4)
  4457. mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
  4458. }
  4459. win_enable = 0x3f;
  4460. win_protect = 0;
  4461. if (dram) {
  4462. for (i = 0; i < dram->num_cs; i++) {
  4463. const struct mbus_dram_window *cs = dram->cs + i;
  4464. mvreg_write(pp, MVNETA_WIN_BASE(i),
  4465. (cs->base & 0xffff0000) |
  4466. (cs->mbus_attr << 8) |
  4467. dram->mbus_dram_target_id);
  4468. mvreg_write(pp, MVNETA_WIN_SIZE(i),
  4469. (cs->size - 1) & 0xffff0000);
  4470. win_enable &= ~(1 << i);
  4471. win_protect |= 3 << (2 * i);
  4472. }
  4473. } else {
  4474. if (pp->neta_ac5)
  4475. mvreg_write(pp, MVNETA_WIN_BASE(0),
  4476. (MVNETA_AC5_CNM_DDR_ATTR << 8) |
  4477. MVNETA_AC5_CNM_DDR_TARGET);
  4478. /* For Armada3700 open default 4GB Mbus window, leaving
  4479. * arbitration of target/attribute to a different layer
  4480. * of configuration.
  4481. */
  4482. mvreg_write(pp, MVNETA_WIN_SIZE(0), 0xffff0000);
  4483. win_enable &= ~BIT(0);
  4484. win_protect = 3;
  4485. }
  4486. mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
  4487. mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect);
  4488. }
  4489. /* Power up the port */
  4490. static int mvneta_port_power_up(struct mvneta_port *pp, int phy_mode)
  4491. {
  4492. /* MAC Cause register should be cleared */
  4493. mvreg_write(pp, MVNETA_UNIT_INTR_CAUSE, 0);
  4494. if (phy_mode != PHY_INTERFACE_MODE_QSGMII &&
  4495. phy_mode != PHY_INTERFACE_MODE_SGMII &&
  4496. !phy_interface_mode_is_8023z(phy_mode) &&
  4497. !phy_interface_mode_is_rgmii(phy_mode))
  4498. return -EINVAL;
  4499. /* Ensure LPI is disabled */
  4500. mvneta_mac_disable_tx_lpi(&pp->phylink_config);
  4501. return 0;
  4502. }
  4503. /* Device initialization routine */
  4504. static int mvneta_probe(struct platform_device *pdev)
  4505. {
  4506. struct device_node *dn = pdev->dev.of_node;
  4507. struct device_node *bm_node;
  4508. struct mvneta_port *pp;
  4509. struct net_device *dev;
  4510. struct phylink *phylink;
  4511. struct phy *comphy;
  4512. char hw_mac_addr[ETH_ALEN];
  4513. phy_interface_t phy_mode;
  4514. const char *mac_from;
  4515. int tx_csum_limit;
  4516. int err;
  4517. int cpu;
  4518. dev = devm_alloc_etherdev_mqs(&pdev->dev, sizeof(struct mvneta_port),
  4519. txq_number, rxq_number);
  4520. if (!dev)
  4521. return -ENOMEM;
  4522. dev->tx_queue_len = MVNETA_MAX_TXD;
  4523. dev->watchdog_timeo = 5 * HZ;
  4524. dev->netdev_ops = &mvneta_netdev_ops;
  4525. dev->ethtool_ops = &mvneta_eth_tool_ops;
  4526. pp = netdev_priv(dev);
  4527. spin_lock_init(&pp->lock);
  4528. pp->dn = dn;
  4529. pp->rxq_def = rxq_def;
  4530. pp->indir[0] = rxq_def;
  4531. err = of_get_phy_mode(dn, &phy_mode);
  4532. if (err) {
  4533. dev_err(&pdev->dev, "incorrect phy-mode\n");
  4534. return err;
  4535. }
  4536. pp->phy_interface = phy_mode;
  4537. comphy = devm_of_phy_get(&pdev->dev, dn, NULL);
  4538. if (comphy == ERR_PTR(-EPROBE_DEFER))
  4539. return -EPROBE_DEFER;
  4540. if (IS_ERR(comphy))
  4541. comphy = NULL;
  4542. pp->comphy = comphy;
  4543. pp->base = devm_platform_ioremap_resource(pdev, 0);
  4544. if (IS_ERR(pp->base))
  4545. return PTR_ERR(pp->base);
  4546. /* Get special SoC configurations */
  4547. if (of_device_is_compatible(dn, "marvell,armada-3700-neta"))
  4548. pp->neta_armada3700 = true;
  4549. if (of_device_is_compatible(dn, "marvell,armada-ac5-neta")) {
  4550. pp->neta_armada3700 = true;
  4551. pp->neta_ac5 = true;
  4552. }
  4553. dev->irq = irq_of_parse_and_map(dn, 0);
  4554. if (dev->irq == 0)
  4555. return -EINVAL;
  4556. pp->clk = devm_clk_get(&pdev->dev, "core");
  4557. if (IS_ERR(pp->clk))
  4558. pp->clk = devm_clk_get(&pdev->dev, NULL);
  4559. if (IS_ERR(pp->clk)) {
  4560. err = PTR_ERR(pp->clk);
  4561. goto err_free_irq;
  4562. }
  4563. clk_prepare_enable(pp->clk);
  4564. pp->clk_bus = devm_clk_get(&pdev->dev, "bus");
  4565. if (!IS_ERR(pp->clk_bus))
  4566. clk_prepare_enable(pp->clk_bus);
  4567. pp->phylink_pcs.ops = &mvneta_phylink_pcs_ops;
  4568. pp->phylink_config.dev = &dev->dev;
  4569. pp->phylink_config.type = PHYLINK_NETDEV;
  4570. pp->phylink_config.mac_capabilities = MAC_SYM_PAUSE | MAC_10 |
  4571. MAC_100 | MAC_1000FD | MAC_2500FD;
  4572. /* Setup EEE. Choose 250us idle. Only supported in SGMII modes. */
  4573. __set_bit(PHY_INTERFACE_MODE_QSGMII, pp->phylink_config.lpi_interfaces);
  4574. __set_bit(PHY_INTERFACE_MODE_SGMII, pp->phylink_config.lpi_interfaces);
  4575. pp->phylink_config.lpi_capabilities = MAC_100FD | MAC_1000FD;
  4576. pp->phylink_config.lpi_timer_default = 250;
  4577. pp->phylink_config.eee_enabled_default = true;
  4578. phy_interface_set_rgmii(pp->phylink_config.supported_interfaces);
  4579. __set_bit(PHY_INTERFACE_MODE_QSGMII,
  4580. pp->phylink_config.supported_interfaces);
  4581. if (comphy) {
  4582. /* If a COMPHY is present, we can support any of the serdes
  4583. * modes and switch between them.
  4584. */
  4585. __set_bit(PHY_INTERFACE_MODE_SGMII,
  4586. pp->phylink_config.supported_interfaces);
  4587. __set_bit(PHY_INTERFACE_MODE_1000BASEX,
  4588. pp->phylink_config.supported_interfaces);
  4589. __set_bit(PHY_INTERFACE_MODE_2500BASEX,
  4590. pp->phylink_config.supported_interfaces);
  4591. } else if (phy_mode == PHY_INTERFACE_MODE_2500BASEX) {
  4592. /* No COMPHY, with only 2500BASE-X mode supported */
  4593. __set_bit(PHY_INTERFACE_MODE_2500BASEX,
  4594. pp->phylink_config.supported_interfaces);
  4595. } else if (phy_mode == PHY_INTERFACE_MODE_1000BASEX ||
  4596. phy_mode == PHY_INTERFACE_MODE_SGMII) {
  4597. /* No COMPHY, we can switch between 1000BASE-X and SGMII */
  4598. __set_bit(PHY_INTERFACE_MODE_1000BASEX,
  4599. pp->phylink_config.supported_interfaces);
  4600. __set_bit(PHY_INTERFACE_MODE_SGMII,
  4601. pp->phylink_config.supported_interfaces);
  4602. }
  4603. phylink = phylink_create(&pp->phylink_config, pdev->dev.fwnode,
  4604. phy_mode, &mvneta_phylink_ops);
  4605. if (IS_ERR(phylink)) {
  4606. err = PTR_ERR(phylink);
  4607. goto err_clk;
  4608. }
  4609. pp->phylink = phylink;
  4610. /* Alloc per-cpu port structure */
  4611. pp->ports = alloc_percpu(struct mvneta_pcpu_port);
  4612. if (!pp->ports) {
  4613. err = -ENOMEM;
  4614. goto err_free_phylink;
  4615. }
  4616. /* Alloc per-cpu stats */
  4617. pp->stats = netdev_alloc_pcpu_stats(struct mvneta_pcpu_stats);
  4618. if (!pp->stats) {
  4619. err = -ENOMEM;
  4620. goto err_free_ports;
  4621. }
  4622. err = of_get_ethdev_address(dn, dev);
  4623. if (!err) {
  4624. mac_from = "device tree";
  4625. } else {
  4626. mvneta_get_mac_addr(pp, hw_mac_addr);
  4627. if (is_valid_ether_addr(hw_mac_addr)) {
  4628. mac_from = "hardware";
  4629. eth_hw_addr_set(dev, hw_mac_addr);
  4630. } else {
  4631. mac_from = "random";
  4632. eth_hw_addr_random(dev);
  4633. }
  4634. }
  4635. if (!of_property_read_u32(dn, "tx-csum-limit", &tx_csum_limit)) {
  4636. if (tx_csum_limit < 0 ||
  4637. tx_csum_limit > MVNETA_TX_CSUM_MAX_SIZE) {
  4638. tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE;
  4639. dev_info(&pdev->dev,
  4640. "Wrong TX csum limit in DT, set to %dB\n",
  4641. MVNETA_TX_CSUM_DEF_SIZE);
  4642. }
  4643. } else if (of_device_is_compatible(dn, "marvell,armada-370-neta")) {
  4644. tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE;
  4645. } else {
  4646. tx_csum_limit = MVNETA_TX_CSUM_MAX_SIZE;
  4647. }
  4648. pp->tx_csum_limit = tx_csum_limit;
  4649. pp->dram_target_info = mv_mbus_dram_info();
  4650. /* Armada3700 requires setting default configuration of Mbus
  4651. * windows, however without using filled mbus_dram_target_info
  4652. * structure.
  4653. */
  4654. if (pp->dram_target_info || pp->neta_armada3700)
  4655. mvneta_conf_mbus_windows(pp, pp->dram_target_info);
  4656. pp->tx_ring_size = MVNETA_MAX_TXD;
  4657. pp->rx_ring_size = MVNETA_MAX_RXD;
  4658. pp->dev = dev;
  4659. SET_NETDEV_DEV(dev, &pdev->dev);
  4660. pp->id = global_port_id++;
  4661. /* Obtain access to BM resources if enabled and already initialized */
  4662. bm_node = of_parse_phandle(dn, "buffer-manager", 0);
  4663. if (bm_node) {
  4664. pp->bm_priv = mvneta_bm_get(bm_node);
  4665. if (pp->bm_priv) {
  4666. err = mvneta_bm_port_init(pdev, pp);
  4667. if (err < 0) {
  4668. dev_info(&pdev->dev,
  4669. "use SW buffer management\n");
  4670. mvneta_bm_put(pp->bm_priv);
  4671. pp->bm_priv = NULL;
  4672. }
  4673. }
  4674. /* Set RX packet offset correction for platforms, whose
  4675. * NET_SKB_PAD, exceeds 64B. It should be 64B for 64-bit
  4676. * platforms and 0B for 32-bit ones.
  4677. */
  4678. pp->rx_offset_correction = max(0,
  4679. NET_SKB_PAD -
  4680. MVNETA_RX_PKT_OFFSET_CORRECTION);
  4681. }
  4682. of_node_put(bm_node);
  4683. /* sw buffer management */
  4684. if (!pp->bm_priv)
  4685. pp->rx_offset_correction = MVNETA_SKB_HEADROOM;
  4686. err = mvneta_init(&pdev->dev, pp);
  4687. if (err < 0)
  4688. goto err_netdev;
  4689. err = mvneta_port_power_up(pp, pp->phy_interface);
  4690. if (err < 0) {
  4691. dev_err(&pdev->dev, "can't power up port\n");
  4692. goto err_netdev;
  4693. }
  4694. /* Armada3700 network controller does not support per-cpu
  4695. * operation, so only single NAPI should be initialized.
  4696. */
  4697. if (pp->neta_armada3700) {
  4698. netif_napi_add(dev, &pp->napi, mvneta_poll);
  4699. } else {
  4700. for_each_present_cpu(cpu) {
  4701. struct mvneta_pcpu_port *port =
  4702. per_cpu_ptr(pp->ports, cpu);
  4703. netif_napi_add(dev, &port->napi, mvneta_poll);
  4704. port->pp = pp;
  4705. }
  4706. }
  4707. dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  4708. NETIF_F_TSO | NETIF_F_RXCSUM;
  4709. dev->hw_features |= dev->features;
  4710. dev->vlan_features |= dev->features;
  4711. if (!pp->bm_priv)
  4712. dev->xdp_features = NETDEV_XDP_ACT_BASIC |
  4713. NETDEV_XDP_ACT_REDIRECT |
  4714. NETDEV_XDP_ACT_NDO_XMIT |
  4715. NETDEV_XDP_ACT_RX_SG |
  4716. NETDEV_XDP_ACT_NDO_XMIT_SG;
  4717. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  4718. netif_set_tso_max_segs(dev, MVNETA_MAX_TSO_SEGS);
  4719. /* MTU range: 68 - 9676 */
  4720. dev->min_mtu = ETH_MIN_MTU;
  4721. /* 9676 == 9700 - 20 and rounding to 8 */
  4722. dev->max_mtu = 9676;
  4723. err = register_netdev(dev);
  4724. if (err < 0) {
  4725. dev_err(&pdev->dev, "failed to register\n");
  4726. goto err_netdev;
  4727. }
  4728. netdev_info(dev, "Using %s mac address %pM\n", mac_from,
  4729. dev->dev_addr);
  4730. platform_set_drvdata(pdev, pp->dev);
  4731. return 0;
  4732. err_netdev:
  4733. if (pp->bm_priv) {
  4734. mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
  4735. mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short,
  4736. 1 << pp->id);
  4737. mvneta_bm_put(pp->bm_priv);
  4738. }
  4739. free_percpu(pp->stats);
  4740. err_free_ports:
  4741. free_percpu(pp->ports);
  4742. err_free_phylink:
  4743. if (pp->phylink)
  4744. phylink_destroy(pp->phylink);
  4745. err_clk:
  4746. clk_disable_unprepare(pp->clk_bus);
  4747. clk_disable_unprepare(pp->clk);
  4748. err_free_irq:
  4749. irq_dispose_mapping(dev->irq);
  4750. return err;
  4751. }
  4752. /* Device removal routine */
  4753. static void mvneta_remove(struct platform_device *pdev)
  4754. {
  4755. struct net_device *dev = platform_get_drvdata(pdev);
  4756. struct mvneta_port *pp = netdev_priv(dev);
  4757. unregister_netdev(dev);
  4758. clk_disable_unprepare(pp->clk_bus);
  4759. clk_disable_unprepare(pp->clk);
  4760. free_percpu(pp->ports);
  4761. free_percpu(pp->stats);
  4762. irq_dispose_mapping(dev->irq);
  4763. phylink_destroy(pp->phylink);
  4764. if (pp->bm_priv) {
  4765. mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
  4766. mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short,
  4767. 1 << pp->id);
  4768. mvneta_bm_put(pp->bm_priv);
  4769. }
  4770. }
  4771. #ifdef CONFIG_PM_SLEEP
  4772. static int mvneta_suspend(struct device *device)
  4773. {
  4774. int queue;
  4775. struct net_device *dev = dev_get_drvdata(device);
  4776. struct mvneta_port *pp = netdev_priv(dev);
  4777. if (!netif_running(dev))
  4778. goto clean_exit;
  4779. if (!pp->neta_armada3700) {
  4780. spin_lock(&pp->lock);
  4781. pp->is_stopped = true;
  4782. spin_unlock(&pp->lock);
  4783. cpuhp_state_remove_instance_nocalls(online_hpstate,
  4784. &pp->node_online);
  4785. cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
  4786. &pp->node_dead);
  4787. }
  4788. rtnl_lock();
  4789. mvneta_stop_dev(pp);
  4790. rtnl_unlock();
  4791. for (queue = 0; queue < rxq_number; queue++) {
  4792. struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
  4793. mvneta_rxq_drop_pkts(pp, rxq);
  4794. }
  4795. for (queue = 0; queue < txq_number; queue++) {
  4796. struct mvneta_tx_queue *txq = &pp->txqs[queue];
  4797. mvneta_txq_hw_deinit(pp, txq);
  4798. }
  4799. clean_exit:
  4800. netif_device_detach(dev);
  4801. clk_disable_unprepare(pp->clk_bus);
  4802. clk_disable_unprepare(pp->clk);
  4803. return 0;
  4804. }
  4805. static int mvneta_resume(struct device *device)
  4806. {
  4807. struct platform_device *pdev = to_platform_device(device);
  4808. struct net_device *dev = dev_get_drvdata(device);
  4809. struct mvneta_port *pp = netdev_priv(dev);
  4810. int err, queue;
  4811. clk_prepare_enable(pp->clk);
  4812. if (!IS_ERR(pp->clk_bus))
  4813. clk_prepare_enable(pp->clk_bus);
  4814. if (pp->dram_target_info || pp->neta_armada3700)
  4815. mvneta_conf_mbus_windows(pp, pp->dram_target_info);
  4816. if (pp->bm_priv) {
  4817. err = mvneta_bm_port_init(pdev, pp);
  4818. if (err < 0) {
  4819. dev_info(&pdev->dev, "use SW buffer management\n");
  4820. pp->rx_offset_correction = MVNETA_SKB_HEADROOM;
  4821. pp->bm_priv = NULL;
  4822. }
  4823. }
  4824. mvneta_defaults_set(pp);
  4825. err = mvneta_port_power_up(pp, pp->phy_interface);
  4826. if (err < 0) {
  4827. dev_err(device, "can't power up port\n");
  4828. return err;
  4829. }
  4830. netif_device_attach(dev);
  4831. if (!netif_running(dev))
  4832. return 0;
  4833. for (queue = 0; queue < rxq_number; queue++) {
  4834. struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
  4835. rxq->next_desc_to_proc = 0;
  4836. mvneta_rxq_hw_init(pp, rxq);
  4837. }
  4838. for (queue = 0; queue < txq_number; queue++) {
  4839. struct mvneta_tx_queue *txq = &pp->txqs[queue];
  4840. txq->next_desc_to_proc = 0;
  4841. mvneta_txq_hw_init(pp, txq);
  4842. }
  4843. if (!pp->neta_armada3700) {
  4844. spin_lock(&pp->lock);
  4845. pp->is_stopped = false;
  4846. spin_unlock(&pp->lock);
  4847. cpuhp_state_add_instance_nocalls(online_hpstate,
  4848. &pp->node_online);
  4849. cpuhp_state_add_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
  4850. &pp->node_dead);
  4851. }
  4852. rtnl_lock();
  4853. mvneta_start_dev(pp);
  4854. rtnl_unlock();
  4855. mvneta_set_rx_mode(dev);
  4856. return 0;
  4857. }
  4858. #endif
  4859. static SIMPLE_DEV_PM_OPS(mvneta_pm_ops, mvneta_suspend, mvneta_resume);
  4860. static const struct of_device_id mvneta_match[] = {
  4861. { .compatible = "marvell,armada-370-neta" },
  4862. { .compatible = "marvell,armada-xp-neta" },
  4863. { .compatible = "marvell,armada-3700-neta" },
  4864. { .compatible = "marvell,armada-ac5-neta" },
  4865. { }
  4866. };
  4867. MODULE_DEVICE_TABLE(of, mvneta_match);
  4868. static struct platform_driver mvneta_driver = {
  4869. .probe = mvneta_probe,
  4870. .remove = mvneta_remove,
  4871. .driver = {
  4872. .name = MVNETA_DRIVER_NAME,
  4873. .of_match_table = mvneta_match,
  4874. .pm = &mvneta_pm_ops,
  4875. },
  4876. };
  4877. static int __init mvneta_driver_init(void)
  4878. {
  4879. int ret;
  4880. BUILD_BUG_ON_NOT_POWER_OF_2(MVNETA_TSO_PER_PAGE);
  4881. ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "net/mvneta:online",
  4882. mvneta_cpu_online,
  4883. mvneta_cpu_down_prepare);
  4884. if (ret < 0)
  4885. goto out;
  4886. online_hpstate = ret;
  4887. ret = cpuhp_setup_state_multi(CPUHP_NET_MVNETA_DEAD, "net/mvneta:dead",
  4888. NULL, mvneta_cpu_dead);
  4889. if (ret)
  4890. goto err_dead;
  4891. ret = platform_driver_register(&mvneta_driver);
  4892. if (ret)
  4893. goto err;
  4894. return 0;
  4895. err:
  4896. cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD);
  4897. err_dead:
  4898. cpuhp_remove_multi_state(online_hpstate);
  4899. out:
  4900. return ret;
  4901. }
  4902. module_init(mvneta_driver_init);
  4903. static void __exit mvneta_driver_exit(void)
  4904. {
  4905. platform_driver_unregister(&mvneta_driver);
  4906. cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD);
  4907. cpuhp_remove_multi_state(online_hpstate);
  4908. }
  4909. module_exit(mvneta_driver_exit);
  4910. MODULE_DESCRIPTION("Marvell NETA Ethernet Driver - www.marvell.com");
  4911. MODULE_AUTHOR("Rami Rosen <rosenr@marvell.com>, Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
  4912. MODULE_LICENSE("GPL");
  4913. module_param(rxq_number, int, 0444);
  4914. module_param(txq_number, int, 0444);
  4915. module_param(rxq_def, int, 0444);
  4916. module_param(rx_copybreak, int, 0644);