mvpp2.c 180 KB

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