mvneta.c 159 KB

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