mvneta.c 157 KB

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