mvpp2.c 250 KB

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