mvneta.c 122 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539
  1. /*
  2. * Driver for Marvell NETA network card for Armada XP and Armada 370 SoCs.
  3. *
  4. * Copyright (C) 2012 Marvell
  5. *
  6. * Rami Rosen <rosenr@marvell.com>
  7. * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
  8. *
  9. * This file is licensed under the terms of the GNU General Public
  10. * License version 2. This program is licensed "as is" without any
  11. * warranty of any kind, whether express or implied.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/cpu.h>
  15. #include <linux/etherdevice.h>
  16. #include <linux/if_vlan.h>
  17. #include <linux/inetdevice.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/io.h>
  20. #include <linux/kernel.h>
  21. #include <linux/mbus.h>
  22. #include <linux/module.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/of.h>
  25. #include <linux/of_address.h>
  26. #include <linux/of_irq.h>
  27. #include <linux/of_mdio.h>
  28. #include <linux/of_net.h>
  29. #include <linux/phy.h>
  30. #include <linux/phy_fixed.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/skbuff.h>
  33. #include <net/hwbm.h>
  34. #include "mvneta_bm.h"
  35. #include <net/ip.h>
  36. #include <net/ipv6.h>
  37. #include <net/tso.h>
  38. /* Registers */
  39. #define MVNETA_RXQ_CONFIG_REG(q) (0x1400 + ((q) << 2))
  40. #define MVNETA_RXQ_HW_BUF_ALLOC BIT(0)
  41. #define MVNETA_RXQ_SHORT_POOL_ID_SHIFT 4
  42. #define MVNETA_RXQ_SHORT_POOL_ID_MASK 0x30
  43. #define MVNETA_RXQ_LONG_POOL_ID_SHIFT 6
  44. #define MVNETA_RXQ_LONG_POOL_ID_MASK 0xc0
  45. #define MVNETA_RXQ_PKT_OFFSET_ALL_MASK (0xf << 8)
  46. #define MVNETA_RXQ_PKT_OFFSET_MASK(offs) ((offs) << 8)
  47. #define MVNETA_RXQ_THRESHOLD_REG(q) (0x14c0 + ((q) << 2))
  48. #define MVNETA_RXQ_NON_OCCUPIED(v) ((v) << 16)
  49. #define MVNETA_RXQ_BASE_ADDR_REG(q) (0x1480 + ((q) << 2))
  50. #define MVNETA_RXQ_SIZE_REG(q) (0x14a0 + ((q) << 2))
  51. #define MVNETA_RXQ_BUF_SIZE_SHIFT 19
  52. #define MVNETA_RXQ_BUF_SIZE_MASK (0x1fff << 19)
  53. #define MVNETA_RXQ_STATUS_REG(q) (0x14e0 + ((q) << 2))
  54. #define MVNETA_RXQ_OCCUPIED_ALL_MASK 0x3fff
  55. #define MVNETA_RXQ_STATUS_UPDATE_REG(q) (0x1500 + ((q) << 2))
  56. #define MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT 16
  57. #define MVNETA_RXQ_ADD_NON_OCCUPIED_MAX 255
  58. #define MVNETA_PORT_POOL_BUFFER_SZ_REG(pool) (0x1700 + ((pool) << 2))
  59. #define MVNETA_PORT_POOL_BUFFER_SZ_SHIFT 3
  60. #define MVNETA_PORT_POOL_BUFFER_SZ_MASK 0xfff8
  61. #define MVNETA_PORT_RX_RESET 0x1cc0
  62. #define MVNETA_PORT_RX_DMA_RESET BIT(0)
  63. #define MVNETA_PHY_ADDR 0x2000
  64. #define MVNETA_PHY_ADDR_MASK 0x1f
  65. #define MVNETA_MBUS_RETRY 0x2010
  66. #define MVNETA_UNIT_INTR_CAUSE 0x2080
  67. #define MVNETA_UNIT_CONTROL 0x20B0
  68. #define MVNETA_PHY_POLLING_ENABLE BIT(1)
  69. #define MVNETA_WIN_BASE(w) (0x2200 + ((w) << 3))
  70. #define MVNETA_WIN_SIZE(w) (0x2204 + ((w) << 3))
  71. #define MVNETA_WIN_REMAP(w) (0x2280 + ((w) << 2))
  72. #define MVNETA_BASE_ADDR_ENABLE 0x2290
  73. #define MVNETA_ACCESS_PROTECT_ENABLE 0x2294
  74. #define MVNETA_PORT_CONFIG 0x2400
  75. #define MVNETA_UNI_PROMISC_MODE BIT(0)
  76. #define MVNETA_DEF_RXQ(q) ((q) << 1)
  77. #define MVNETA_DEF_RXQ_ARP(q) ((q) << 4)
  78. #define MVNETA_TX_UNSET_ERR_SUM BIT(12)
  79. #define MVNETA_DEF_RXQ_TCP(q) ((q) << 16)
  80. #define MVNETA_DEF_RXQ_UDP(q) ((q) << 19)
  81. #define MVNETA_DEF_RXQ_BPDU(q) ((q) << 22)
  82. #define MVNETA_RX_CSUM_WITH_PSEUDO_HDR BIT(25)
  83. #define MVNETA_PORT_CONFIG_DEFL_VALUE(q) (MVNETA_DEF_RXQ(q) | \
  84. MVNETA_DEF_RXQ_ARP(q) | \
  85. MVNETA_DEF_RXQ_TCP(q) | \
  86. MVNETA_DEF_RXQ_UDP(q) | \
  87. MVNETA_DEF_RXQ_BPDU(q) | \
  88. MVNETA_TX_UNSET_ERR_SUM | \
  89. MVNETA_RX_CSUM_WITH_PSEUDO_HDR)
  90. #define MVNETA_PORT_CONFIG_EXTEND 0x2404
  91. #define MVNETA_MAC_ADDR_LOW 0x2414
  92. #define MVNETA_MAC_ADDR_HIGH 0x2418
  93. #define MVNETA_SDMA_CONFIG 0x241c
  94. #define MVNETA_SDMA_BRST_SIZE_16 4
  95. #define MVNETA_RX_BRST_SZ_MASK(burst) ((burst) << 1)
  96. #define MVNETA_RX_NO_DATA_SWAP BIT(4)
  97. #define MVNETA_TX_NO_DATA_SWAP BIT(5)
  98. #define MVNETA_DESC_SWAP BIT(6)
  99. #define MVNETA_TX_BRST_SZ_MASK(burst) ((burst) << 22)
  100. #define MVNETA_PORT_STATUS 0x2444
  101. #define MVNETA_TX_IN_PRGRS BIT(1)
  102. #define MVNETA_TX_FIFO_EMPTY BIT(8)
  103. #define MVNETA_RX_MIN_FRAME_SIZE 0x247c
  104. #define MVNETA_SERDES_CFG 0x24A0
  105. #define MVNETA_SGMII_SERDES_PROTO 0x0cc7
  106. #define MVNETA_QSGMII_SERDES_PROTO 0x0667
  107. #define MVNETA_TYPE_PRIO 0x24bc
  108. #define MVNETA_FORCE_UNI BIT(21)
  109. #define MVNETA_TXQ_CMD_1 0x24e4
  110. #define MVNETA_TXQ_CMD 0x2448
  111. #define MVNETA_TXQ_DISABLE_SHIFT 8
  112. #define MVNETA_TXQ_ENABLE_MASK 0x000000ff
  113. #define MVNETA_RX_DISCARD_FRAME_COUNT 0x2484
  114. #define MVNETA_OVERRUN_FRAME_COUNT 0x2488
  115. #define MVNETA_GMAC_CLOCK_DIVIDER 0x24f4
  116. #define MVNETA_GMAC_1MS_CLOCK_ENABLE BIT(31)
  117. #define MVNETA_ACC_MODE 0x2500
  118. #define MVNETA_BM_ADDRESS 0x2504
  119. #define MVNETA_CPU_MAP(cpu) (0x2540 + ((cpu) << 2))
  120. #define MVNETA_CPU_RXQ_ACCESS_ALL_MASK 0x000000ff
  121. #define MVNETA_CPU_TXQ_ACCESS_ALL_MASK 0x0000ff00
  122. #define MVNETA_CPU_RXQ_ACCESS(rxq) BIT(rxq)
  123. #define MVNETA_CPU_TXQ_ACCESS(txq) BIT(txq + 8)
  124. #define MVNETA_RXQ_TIME_COAL_REG(q) (0x2580 + ((q) << 2))
  125. /* Exception Interrupt Port/Queue Cause register
  126. *
  127. * Their behavior depend of the mapping done using the PCPX2Q
  128. * registers. For a given CPU if the bit associated to a queue is not
  129. * set, then for the register a read from this CPU will always return
  130. * 0 and a write won't do anything
  131. */
  132. #define MVNETA_INTR_NEW_CAUSE 0x25a0
  133. #define MVNETA_INTR_NEW_MASK 0x25a4
  134. /* bits 0..7 = TXQ SENT, one bit per queue.
  135. * bits 8..15 = RXQ OCCUP, one bit per queue.
  136. * bits 16..23 = RXQ FREE, one bit per queue.
  137. * bit 29 = OLD_REG_SUM, see old reg ?
  138. * bit 30 = TX_ERR_SUM, one bit for 4 ports
  139. * bit 31 = MISC_SUM, one bit for 4 ports
  140. */
  141. #define MVNETA_TX_INTR_MASK(nr_txqs) (((1 << nr_txqs) - 1) << 0)
  142. #define MVNETA_TX_INTR_MASK_ALL (0xff << 0)
  143. #define MVNETA_RX_INTR_MASK(nr_rxqs) (((1 << nr_rxqs) - 1) << 8)
  144. #define MVNETA_RX_INTR_MASK_ALL (0xff << 8)
  145. #define MVNETA_MISCINTR_INTR_MASK BIT(31)
  146. #define MVNETA_INTR_OLD_CAUSE 0x25a8
  147. #define MVNETA_INTR_OLD_MASK 0x25ac
  148. /* Data Path Port/Queue Cause Register */
  149. #define MVNETA_INTR_MISC_CAUSE 0x25b0
  150. #define MVNETA_INTR_MISC_MASK 0x25b4
  151. #define MVNETA_CAUSE_PHY_STATUS_CHANGE BIT(0)
  152. #define MVNETA_CAUSE_LINK_CHANGE BIT(1)
  153. #define MVNETA_CAUSE_PTP BIT(4)
  154. #define MVNETA_CAUSE_INTERNAL_ADDR_ERR BIT(7)
  155. #define MVNETA_CAUSE_RX_OVERRUN BIT(8)
  156. #define MVNETA_CAUSE_RX_CRC_ERROR BIT(9)
  157. #define MVNETA_CAUSE_RX_LARGE_PKT BIT(10)
  158. #define MVNETA_CAUSE_TX_UNDERUN BIT(11)
  159. #define MVNETA_CAUSE_PRBS_ERR BIT(12)
  160. #define MVNETA_CAUSE_PSC_SYNC_CHANGE BIT(13)
  161. #define MVNETA_CAUSE_SERDES_SYNC_ERR BIT(14)
  162. #define MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT 16
  163. #define MVNETA_CAUSE_BMU_ALLOC_ERR_ALL_MASK (0xF << MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT)
  164. #define MVNETA_CAUSE_BMU_ALLOC_ERR_MASK(pool) (1 << (MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT + (pool)))
  165. #define MVNETA_CAUSE_TXQ_ERROR_SHIFT 24
  166. #define MVNETA_CAUSE_TXQ_ERROR_ALL_MASK (0xFF << MVNETA_CAUSE_TXQ_ERROR_SHIFT)
  167. #define MVNETA_CAUSE_TXQ_ERROR_MASK(q) (1 << (MVNETA_CAUSE_TXQ_ERROR_SHIFT + (q)))
  168. #define MVNETA_INTR_ENABLE 0x25b8
  169. #define MVNETA_TXQ_INTR_ENABLE_ALL_MASK 0x0000ff00
  170. #define MVNETA_RXQ_INTR_ENABLE_ALL_MASK 0x000000ff
  171. #define MVNETA_RXQ_CMD 0x2680
  172. #define MVNETA_RXQ_DISABLE_SHIFT 8
  173. #define MVNETA_RXQ_ENABLE_MASK 0x000000ff
  174. #define MVETH_TXQ_TOKEN_COUNT_REG(q) (0x2700 + ((q) << 4))
  175. #define MVETH_TXQ_TOKEN_CFG_REG(q) (0x2704 + ((q) << 4))
  176. #define MVNETA_GMAC_CTRL_0 0x2c00
  177. #define MVNETA_GMAC_MAX_RX_SIZE_SHIFT 2
  178. #define MVNETA_GMAC_MAX_RX_SIZE_MASK 0x7ffc
  179. #define MVNETA_GMAC0_PORT_ENABLE BIT(0)
  180. #define MVNETA_GMAC_CTRL_2 0x2c08
  181. #define MVNETA_GMAC2_INBAND_AN_ENABLE BIT(0)
  182. #define MVNETA_GMAC2_PCS_ENABLE BIT(3)
  183. #define MVNETA_GMAC2_PORT_RGMII BIT(4)
  184. #define MVNETA_GMAC2_PORT_RESET BIT(6)
  185. #define MVNETA_GMAC_STATUS 0x2c10
  186. #define MVNETA_GMAC_LINK_UP BIT(0)
  187. #define MVNETA_GMAC_SPEED_1000 BIT(1)
  188. #define MVNETA_GMAC_SPEED_100 BIT(2)
  189. #define MVNETA_GMAC_FULL_DUPLEX BIT(3)
  190. #define MVNETA_GMAC_RX_FLOW_CTRL_ENABLE BIT(4)
  191. #define MVNETA_GMAC_TX_FLOW_CTRL_ENABLE BIT(5)
  192. #define MVNETA_GMAC_RX_FLOW_CTRL_ACTIVE BIT(6)
  193. #define MVNETA_GMAC_TX_FLOW_CTRL_ACTIVE BIT(7)
  194. #define MVNETA_GMAC_AUTONEG_CONFIG 0x2c0c
  195. #define MVNETA_GMAC_FORCE_LINK_DOWN BIT(0)
  196. #define MVNETA_GMAC_FORCE_LINK_PASS BIT(1)
  197. #define MVNETA_GMAC_INBAND_AN_ENABLE BIT(2)
  198. #define MVNETA_GMAC_CONFIG_MII_SPEED BIT(5)
  199. #define MVNETA_GMAC_CONFIG_GMII_SPEED BIT(6)
  200. #define MVNETA_GMAC_AN_SPEED_EN BIT(7)
  201. #define MVNETA_GMAC_AN_FLOW_CTRL_EN BIT(11)
  202. #define MVNETA_GMAC_CONFIG_FULL_DUPLEX BIT(12)
  203. #define MVNETA_GMAC_AN_DUPLEX_EN BIT(13)
  204. #define MVNETA_MIB_COUNTERS_BASE 0x3000
  205. #define MVNETA_MIB_LATE_COLLISION 0x7c
  206. #define MVNETA_DA_FILT_SPEC_MCAST 0x3400
  207. #define MVNETA_DA_FILT_OTH_MCAST 0x3500
  208. #define MVNETA_DA_FILT_UCAST_BASE 0x3600
  209. #define MVNETA_TXQ_BASE_ADDR_REG(q) (0x3c00 + ((q) << 2))
  210. #define MVNETA_TXQ_SIZE_REG(q) (0x3c20 + ((q) << 2))
  211. #define MVNETA_TXQ_SENT_THRESH_ALL_MASK 0x3fff0000
  212. #define MVNETA_TXQ_SENT_THRESH_MASK(coal) ((coal) << 16)
  213. #define MVNETA_TXQ_UPDATE_REG(q) (0x3c60 + ((q) << 2))
  214. #define MVNETA_TXQ_DEC_SENT_SHIFT 16
  215. #define MVNETA_TXQ_DEC_SENT_MASK 0xff
  216. #define MVNETA_TXQ_STATUS_REG(q) (0x3c40 + ((q) << 2))
  217. #define MVNETA_TXQ_SENT_DESC_SHIFT 16
  218. #define MVNETA_TXQ_SENT_DESC_MASK 0x3fff0000
  219. #define MVNETA_PORT_TX_RESET 0x3cf0
  220. #define MVNETA_PORT_TX_DMA_RESET BIT(0)
  221. #define MVNETA_TX_MTU 0x3e0c
  222. #define MVNETA_TX_TOKEN_SIZE 0x3e14
  223. #define MVNETA_TX_TOKEN_SIZE_MAX 0xffffffff
  224. #define MVNETA_TXQ_TOKEN_SIZE_REG(q) (0x3e40 + ((q) << 2))
  225. #define MVNETA_TXQ_TOKEN_SIZE_MAX 0x7fffffff
  226. #define MVNETA_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff
  227. /* Descriptor ring Macros */
  228. #define MVNETA_QUEUE_NEXT_DESC(q, index) \
  229. (((index) < (q)->last_desc) ? ((index) + 1) : 0)
  230. /* Various constants */
  231. /* Coalescing */
  232. #define MVNETA_TXDONE_COAL_PKTS 0 /* interrupt per packet */
  233. #define MVNETA_RX_COAL_PKTS 32
  234. #define MVNETA_RX_COAL_USEC 100
  235. /* The two bytes Marvell header. Either contains a special value used
  236. * by Marvell switches when a specific hardware mode is enabled (not
  237. * supported by this driver) or is filled automatically by zeroes on
  238. * the RX side. Those two bytes being at the front of the Ethernet
  239. * header, they allow to have the IP header aligned on a 4 bytes
  240. * boundary automatically: the hardware skips those two bytes on its
  241. * own.
  242. */
  243. #define MVNETA_MH_SIZE 2
  244. #define MVNETA_VLAN_TAG_LEN 4
  245. #define MVNETA_TX_CSUM_DEF_SIZE 1600
  246. #define MVNETA_TX_CSUM_MAX_SIZE 9800
  247. #define MVNETA_ACC_MODE_EXT1 1
  248. #define MVNETA_ACC_MODE_EXT2 2
  249. #define MVNETA_MAX_DECODE_WIN 6
  250. /* Timeout constants */
  251. #define MVNETA_TX_DISABLE_TIMEOUT_MSEC 1000
  252. #define MVNETA_RX_DISABLE_TIMEOUT_MSEC 1000
  253. #define MVNETA_TX_FIFO_EMPTY_TIMEOUT 10000
  254. #define MVNETA_TX_MTU_MAX 0x3ffff
  255. /* The RSS lookup table actually has 256 entries but we do not use
  256. * them yet
  257. */
  258. #define MVNETA_RSS_LU_TABLE_SIZE 1
  259. /* TSO header size */
  260. #define TSO_HEADER_SIZE 128
  261. /* Max number of Rx descriptors */
  262. #define MVNETA_MAX_RXD 128
  263. /* Max number of Tx descriptors */
  264. #define MVNETA_MAX_TXD 532
  265. /* Max number of allowed TCP segments for software TSO */
  266. #define MVNETA_MAX_TSO_SEGS 100
  267. #define MVNETA_MAX_SKB_DESCS (MVNETA_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
  268. /* descriptor aligned size */
  269. #define MVNETA_DESC_ALIGNED_SIZE 32
  270. /* Number of bytes to be taken into account by HW when putting incoming data
  271. * to the buffers. It is needed in case NET_SKB_PAD exceeds maximum packet
  272. * offset supported in MVNETA_RXQ_CONFIG_REG(q) registers.
  273. */
  274. #define MVNETA_RX_PKT_OFFSET_CORRECTION 64
  275. #define MVNETA_RX_PKT_SIZE(mtu) \
  276. ALIGN((mtu) + MVNETA_MH_SIZE + MVNETA_VLAN_TAG_LEN + \
  277. ETH_HLEN + ETH_FCS_LEN, \
  278. cache_line_size())
  279. #define IS_TSO_HEADER(txq, addr) \
  280. ((addr >= txq->tso_hdrs_phys) && \
  281. (addr < txq->tso_hdrs_phys + txq->size * TSO_HEADER_SIZE))
  282. #define MVNETA_RX_GET_BM_POOL_ID(rxd) \
  283. (((rxd)->status & MVNETA_RXD_BM_POOL_MASK) >> MVNETA_RXD_BM_POOL_SHIFT)
  284. struct mvneta_statistic {
  285. unsigned short offset;
  286. unsigned short type;
  287. const char name[ETH_GSTRING_LEN];
  288. };
  289. #define T_REG_32 32
  290. #define T_REG_64 64
  291. static const struct mvneta_statistic mvneta_statistics[] = {
  292. { 0x3000, T_REG_64, "good_octets_received", },
  293. { 0x3010, T_REG_32, "good_frames_received", },
  294. { 0x3008, T_REG_32, "bad_octets_received", },
  295. { 0x3014, T_REG_32, "bad_frames_received", },
  296. { 0x3018, T_REG_32, "broadcast_frames_received", },
  297. { 0x301c, T_REG_32, "multicast_frames_received", },
  298. { 0x3050, T_REG_32, "unrec_mac_control_received", },
  299. { 0x3058, T_REG_32, "good_fc_received", },
  300. { 0x305c, T_REG_32, "bad_fc_received", },
  301. { 0x3060, T_REG_32, "undersize_received", },
  302. { 0x3064, T_REG_32, "fragments_received", },
  303. { 0x3068, T_REG_32, "oversize_received", },
  304. { 0x306c, T_REG_32, "jabber_received", },
  305. { 0x3070, T_REG_32, "mac_receive_error", },
  306. { 0x3074, T_REG_32, "bad_crc_event", },
  307. { 0x3078, T_REG_32, "collision", },
  308. { 0x307c, T_REG_32, "late_collision", },
  309. { 0x2484, T_REG_32, "rx_discard", },
  310. { 0x2488, T_REG_32, "rx_overrun", },
  311. { 0x3020, T_REG_32, "frames_64_octets", },
  312. { 0x3024, T_REG_32, "frames_65_to_127_octets", },
  313. { 0x3028, T_REG_32, "frames_128_to_255_octets", },
  314. { 0x302c, T_REG_32, "frames_256_to_511_octets", },
  315. { 0x3030, T_REG_32, "frames_512_to_1023_octets", },
  316. { 0x3034, T_REG_32, "frames_1024_to_max_octets", },
  317. { 0x3038, T_REG_64, "good_octets_sent", },
  318. { 0x3040, T_REG_32, "good_frames_sent", },
  319. { 0x3044, T_REG_32, "excessive_collision", },
  320. { 0x3048, T_REG_32, "multicast_frames_sent", },
  321. { 0x304c, T_REG_32, "broadcast_frames_sent", },
  322. { 0x3054, T_REG_32, "fc_sent", },
  323. { 0x300c, T_REG_32, "internal_mac_transmit_err", },
  324. };
  325. struct mvneta_pcpu_stats {
  326. struct u64_stats_sync syncp;
  327. u64 rx_packets;
  328. u64 rx_bytes;
  329. u64 tx_packets;
  330. u64 tx_bytes;
  331. };
  332. struct mvneta_pcpu_port {
  333. /* Pointer to the shared port */
  334. struct mvneta_port *pp;
  335. /* Pointer to the CPU-local NAPI struct */
  336. struct napi_struct napi;
  337. /* Cause of the previous interrupt */
  338. u32 cause_rx_tx;
  339. };
  340. struct mvneta_port {
  341. u8 id;
  342. struct mvneta_pcpu_port __percpu *ports;
  343. struct mvneta_pcpu_stats __percpu *stats;
  344. int pkt_size;
  345. unsigned int frag_size;
  346. void __iomem *base;
  347. struct mvneta_rx_queue *rxqs;
  348. struct mvneta_tx_queue *txqs;
  349. struct net_device *dev;
  350. struct hlist_node node_online;
  351. struct hlist_node node_dead;
  352. int rxq_def;
  353. /* Protect the access to the percpu interrupt registers,
  354. * ensuring that the configuration remains coherent.
  355. */
  356. spinlock_t lock;
  357. bool is_stopped;
  358. u32 cause_rx_tx;
  359. struct napi_struct napi;
  360. /* Core clock */
  361. struct clk *clk;
  362. /* AXI clock */
  363. struct clk *clk_bus;
  364. u8 mcast_count[256];
  365. u16 tx_ring_size;
  366. u16 rx_ring_size;
  367. struct mii_bus *mii_bus;
  368. phy_interface_t phy_interface;
  369. struct device_node *phy_node;
  370. unsigned int link;
  371. unsigned int duplex;
  372. unsigned int speed;
  373. unsigned int tx_csum_limit;
  374. unsigned int use_inband_status:1;
  375. struct mvneta_bm *bm_priv;
  376. struct mvneta_bm_pool *pool_long;
  377. struct mvneta_bm_pool *pool_short;
  378. int bm_win_id;
  379. u64 ethtool_stats[ARRAY_SIZE(mvneta_statistics)];
  380. u32 indir[MVNETA_RSS_LU_TABLE_SIZE];
  381. /* Flags for special SoC configurations */
  382. bool neta_armada3700;
  383. u16 rx_offset_correction;
  384. const struct mbus_dram_target_info *dram_target_info;
  385. };
  386. /* The mvneta_tx_desc and mvneta_rx_desc structures describe the
  387. * layout of the transmit and reception DMA descriptors, and their
  388. * layout is therefore defined by the hardware design
  389. */
  390. #define MVNETA_TX_L3_OFF_SHIFT 0
  391. #define MVNETA_TX_IP_HLEN_SHIFT 8
  392. #define MVNETA_TX_L4_UDP BIT(16)
  393. #define MVNETA_TX_L3_IP6 BIT(17)
  394. #define MVNETA_TXD_IP_CSUM BIT(18)
  395. #define MVNETA_TXD_Z_PAD BIT(19)
  396. #define MVNETA_TXD_L_DESC BIT(20)
  397. #define MVNETA_TXD_F_DESC BIT(21)
  398. #define MVNETA_TXD_FLZ_DESC (MVNETA_TXD_Z_PAD | \
  399. MVNETA_TXD_L_DESC | \
  400. MVNETA_TXD_F_DESC)
  401. #define MVNETA_TX_L4_CSUM_FULL BIT(30)
  402. #define MVNETA_TX_L4_CSUM_NOT BIT(31)
  403. #define MVNETA_RXD_ERR_CRC 0x0
  404. #define MVNETA_RXD_BM_POOL_SHIFT 13
  405. #define MVNETA_RXD_BM_POOL_MASK (BIT(13) | BIT(14))
  406. #define MVNETA_RXD_ERR_SUMMARY BIT(16)
  407. #define MVNETA_RXD_ERR_OVERRUN BIT(17)
  408. #define MVNETA_RXD_ERR_LEN BIT(18)
  409. #define MVNETA_RXD_ERR_RESOURCE (BIT(17) | BIT(18))
  410. #define MVNETA_RXD_ERR_CODE_MASK (BIT(17) | BIT(18))
  411. #define MVNETA_RXD_L3_IP4 BIT(25)
  412. #define MVNETA_RXD_FIRST_LAST_DESC (BIT(26) | BIT(27))
  413. #define MVNETA_RXD_L4_CSUM_OK BIT(30)
  414. #if defined(__LITTLE_ENDIAN)
  415. struct mvneta_tx_desc {
  416. u32 command; /* Options used by HW for packet transmitting.*/
  417. u16 reserverd1; /* csum_l4 (for future use) */
  418. u16 data_size; /* Data size of transmitted packet in bytes */
  419. u32 buf_phys_addr; /* Physical addr of transmitted buffer */
  420. u32 reserved2; /* hw_cmd - (for future use, PMT) */
  421. u32 reserved3[4]; /* Reserved - (for future use) */
  422. };
  423. struct mvneta_rx_desc {
  424. u32 status; /* Info about received packet */
  425. u16 reserved1; /* pnc_info - (for future use, PnC) */
  426. u16 data_size; /* Size of received packet in bytes */
  427. u32 buf_phys_addr; /* Physical address of the buffer */
  428. u32 reserved2; /* pnc_flow_id (for future use, PnC) */
  429. u32 buf_cookie; /* cookie for access to RX buffer in rx path */
  430. u16 reserved3; /* prefetch_cmd, for future use */
  431. u16 reserved4; /* csum_l4 - (for future use, PnC) */
  432. u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
  433. u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
  434. };
  435. #else
  436. struct mvneta_tx_desc {
  437. u16 data_size; /* Data size of transmitted packet in bytes */
  438. u16 reserverd1; /* csum_l4 (for future use) */
  439. u32 command; /* Options used by HW for packet transmitting.*/
  440. u32 reserved2; /* hw_cmd - (for future use, PMT) */
  441. u32 buf_phys_addr; /* Physical addr of transmitted buffer */
  442. u32 reserved3[4]; /* Reserved - (for future use) */
  443. };
  444. struct mvneta_rx_desc {
  445. u16 data_size; /* Size of received packet in bytes */
  446. u16 reserved1; /* pnc_info - (for future use, PnC) */
  447. u32 status; /* Info about received packet */
  448. u32 reserved2; /* pnc_flow_id (for future use, PnC) */
  449. u32 buf_phys_addr; /* Physical address of the buffer */
  450. u16 reserved4; /* csum_l4 - (for future use, PnC) */
  451. u16 reserved3; /* prefetch_cmd, for future use */
  452. u32 buf_cookie; /* cookie for access to RX buffer in rx path */
  453. u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
  454. u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
  455. };
  456. #endif
  457. struct mvneta_tx_queue {
  458. /* Number of this TX queue, in the range 0-7 */
  459. u8 id;
  460. /* Number of TX DMA descriptors in the descriptor ring */
  461. int size;
  462. /* Number of currently used TX DMA descriptor in the
  463. * descriptor ring
  464. */
  465. int count;
  466. int pending;
  467. int tx_stop_threshold;
  468. int tx_wake_threshold;
  469. /* Array of transmitted skb */
  470. struct sk_buff **tx_skb;
  471. /* Index of last TX DMA descriptor that was inserted */
  472. int txq_put_index;
  473. /* Index of the TX DMA descriptor to be cleaned up */
  474. int txq_get_index;
  475. u32 done_pkts_coal;
  476. /* Virtual address of the TX DMA descriptors array */
  477. struct mvneta_tx_desc *descs;
  478. /* DMA address of the TX DMA descriptors array */
  479. dma_addr_t descs_phys;
  480. /* Index of the last TX DMA descriptor */
  481. int last_desc;
  482. /* Index of the next TX DMA descriptor to process */
  483. int next_desc_to_proc;
  484. /* DMA buffers for TSO headers */
  485. char *tso_hdrs;
  486. /* DMA address of TSO headers */
  487. dma_addr_t tso_hdrs_phys;
  488. /* Affinity mask for CPUs*/
  489. cpumask_t affinity_mask;
  490. };
  491. struct mvneta_rx_queue {
  492. /* rx queue number, in the range 0-7 */
  493. u8 id;
  494. /* num of rx descriptors in the rx descriptor ring */
  495. int size;
  496. /* counter of times when mvneta_refill() failed */
  497. int missed;
  498. u32 pkts_coal;
  499. u32 time_coal;
  500. /* Virtual address of the RX buffer */
  501. void **buf_virt_addr;
  502. /* Virtual address of the RX DMA descriptors array */
  503. struct mvneta_rx_desc *descs;
  504. /* DMA address of the RX DMA descriptors array */
  505. dma_addr_t descs_phys;
  506. /* Index of the last RX DMA descriptor */
  507. int last_desc;
  508. /* Index of the next RX DMA descriptor to process */
  509. int next_desc_to_proc;
  510. };
  511. static enum cpuhp_state online_hpstate;
  512. /* The hardware supports eight (8) rx queues, but we are only allowing
  513. * the first one to be used. Therefore, let's just allocate one queue.
  514. */
  515. static int rxq_number = 8;
  516. static int txq_number = 8;
  517. static int rxq_def;
  518. static int rx_copybreak __read_mostly = 256;
  519. /* HW BM need that each port be identify by a unique ID */
  520. static int global_port_id;
  521. #define MVNETA_DRIVER_NAME "mvneta"
  522. #define MVNETA_DRIVER_VERSION "1.0"
  523. /* Utility/helper methods */
  524. /* Write helper method */
  525. static void mvreg_write(struct mvneta_port *pp, u32 offset, u32 data)
  526. {
  527. writel(data, pp->base + offset);
  528. }
  529. /* Read helper method */
  530. static u32 mvreg_read(struct mvneta_port *pp, u32 offset)
  531. {
  532. return readl(pp->base + offset);
  533. }
  534. /* Increment txq get counter */
  535. static void mvneta_txq_inc_get(struct mvneta_tx_queue *txq)
  536. {
  537. txq->txq_get_index++;
  538. if (txq->txq_get_index == txq->size)
  539. txq->txq_get_index = 0;
  540. }
  541. /* Increment txq put counter */
  542. static void mvneta_txq_inc_put(struct mvneta_tx_queue *txq)
  543. {
  544. txq->txq_put_index++;
  545. if (txq->txq_put_index == txq->size)
  546. txq->txq_put_index = 0;
  547. }
  548. /* Clear all MIB counters */
  549. static void mvneta_mib_counters_clear(struct mvneta_port *pp)
  550. {
  551. int i;
  552. u32 dummy;
  553. /* Perform dummy reads from MIB counters */
  554. for (i = 0; i < MVNETA_MIB_LATE_COLLISION; i += 4)
  555. dummy = mvreg_read(pp, (MVNETA_MIB_COUNTERS_BASE + i));
  556. dummy = mvreg_read(pp, MVNETA_RX_DISCARD_FRAME_COUNT);
  557. dummy = mvreg_read(pp, MVNETA_OVERRUN_FRAME_COUNT);
  558. }
  559. /* Get System Network Statistics */
  560. static void
  561. mvneta_get_stats64(struct net_device *dev,
  562. struct rtnl_link_stats64 *stats)
  563. {
  564. struct mvneta_port *pp = netdev_priv(dev);
  565. unsigned int start;
  566. int cpu;
  567. for_each_possible_cpu(cpu) {
  568. struct mvneta_pcpu_stats *cpu_stats;
  569. u64 rx_packets;
  570. u64 rx_bytes;
  571. u64 tx_packets;
  572. u64 tx_bytes;
  573. cpu_stats = per_cpu_ptr(pp->stats, cpu);
  574. do {
  575. start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
  576. rx_packets = cpu_stats->rx_packets;
  577. rx_bytes = cpu_stats->rx_bytes;
  578. tx_packets = cpu_stats->tx_packets;
  579. tx_bytes = cpu_stats->tx_bytes;
  580. } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
  581. stats->rx_packets += rx_packets;
  582. stats->rx_bytes += rx_bytes;
  583. stats->tx_packets += tx_packets;
  584. stats->tx_bytes += tx_bytes;
  585. }
  586. stats->rx_errors = dev->stats.rx_errors;
  587. stats->rx_dropped = dev->stats.rx_dropped;
  588. stats->tx_dropped = dev->stats.tx_dropped;
  589. }
  590. /* Rx descriptors helper methods */
  591. /* Checks whether the RX descriptor having this status is both the first
  592. * and the last descriptor for the RX packet. Each RX packet is currently
  593. * received through a single RX descriptor, so not having each RX
  594. * descriptor with its first and last bits set is an error
  595. */
  596. static int mvneta_rxq_desc_is_first_last(u32 status)
  597. {
  598. return (status & MVNETA_RXD_FIRST_LAST_DESC) ==
  599. MVNETA_RXD_FIRST_LAST_DESC;
  600. }
  601. /* Add number of descriptors ready to receive new packets */
  602. static void mvneta_rxq_non_occup_desc_add(struct mvneta_port *pp,
  603. struct mvneta_rx_queue *rxq,
  604. int ndescs)
  605. {
  606. /* Only MVNETA_RXQ_ADD_NON_OCCUPIED_MAX (255) descriptors can
  607. * be added at once
  608. */
  609. while (ndescs > MVNETA_RXQ_ADD_NON_OCCUPIED_MAX) {
  610. mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
  611. (MVNETA_RXQ_ADD_NON_OCCUPIED_MAX <<
  612. MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
  613. ndescs -= MVNETA_RXQ_ADD_NON_OCCUPIED_MAX;
  614. }
  615. mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
  616. (ndescs << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
  617. }
  618. /* Get number of RX descriptors occupied by received packets */
  619. static int mvneta_rxq_busy_desc_num_get(struct mvneta_port *pp,
  620. struct mvneta_rx_queue *rxq)
  621. {
  622. u32 val;
  623. val = mvreg_read(pp, MVNETA_RXQ_STATUS_REG(rxq->id));
  624. return val & MVNETA_RXQ_OCCUPIED_ALL_MASK;
  625. }
  626. /* Update num of rx desc called upon return from rx path or
  627. * from mvneta_rxq_drop_pkts().
  628. */
  629. static void mvneta_rxq_desc_num_update(struct mvneta_port *pp,
  630. struct mvneta_rx_queue *rxq,
  631. int rx_done, int rx_filled)
  632. {
  633. u32 val;
  634. if ((rx_done <= 0xff) && (rx_filled <= 0xff)) {
  635. val = rx_done |
  636. (rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT);
  637. mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
  638. return;
  639. }
  640. /* Only 255 descriptors can be added at once */
  641. while ((rx_done > 0) || (rx_filled > 0)) {
  642. if (rx_done <= 0xff) {
  643. val = rx_done;
  644. rx_done = 0;
  645. } else {
  646. val = 0xff;
  647. rx_done -= 0xff;
  648. }
  649. if (rx_filled <= 0xff) {
  650. val |= rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
  651. rx_filled = 0;
  652. } else {
  653. val |= 0xff << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
  654. rx_filled -= 0xff;
  655. }
  656. mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
  657. }
  658. }
  659. /* Get pointer to next RX descriptor to be processed by SW */
  660. static struct mvneta_rx_desc *
  661. mvneta_rxq_next_desc_get(struct mvneta_rx_queue *rxq)
  662. {
  663. int rx_desc = rxq->next_desc_to_proc;
  664. rxq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(rxq, rx_desc);
  665. prefetch(rxq->descs + rxq->next_desc_to_proc);
  666. return rxq->descs + rx_desc;
  667. }
  668. /* Change maximum receive size of the port. */
  669. static void mvneta_max_rx_size_set(struct mvneta_port *pp, int max_rx_size)
  670. {
  671. u32 val;
  672. val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
  673. val &= ~MVNETA_GMAC_MAX_RX_SIZE_MASK;
  674. val |= ((max_rx_size - MVNETA_MH_SIZE) / 2) <<
  675. MVNETA_GMAC_MAX_RX_SIZE_SHIFT;
  676. mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
  677. }
  678. /* Set rx queue offset */
  679. static void mvneta_rxq_offset_set(struct mvneta_port *pp,
  680. struct mvneta_rx_queue *rxq,
  681. int offset)
  682. {
  683. u32 val;
  684. val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
  685. val &= ~MVNETA_RXQ_PKT_OFFSET_ALL_MASK;
  686. /* Offset is in */
  687. val |= MVNETA_RXQ_PKT_OFFSET_MASK(offset >> 3);
  688. mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
  689. }
  690. /* Tx descriptors helper methods */
  691. /* Update HW with number of TX descriptors to be sent */
  692. static void mvneta_txq_pend_desc_add(struct mvneta_port *pp,
  693. struct mvneta_tx_queue *txq,
  694. int pend_desc)
  695. {
  696. u32 val;
  697. /* Only 255 descriptors can be added at once ; Assume caller
  698. * process TX desriptors in quanta less than 256
  699. */
  700. val = pend_desc + txq->pending;
  701. mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
  702. txq->pending = 0;
  703. }
  704. /* Get pointer to next TX descriptor to be processed (send) by HW */
  705. static struct mvneta_tx_desc *
  706. mvneta_txq_next_desc_get(struct mvneta_tx_queue *txq)
  707. {
  708. int tx_desc = txq->next_desc_to_proc;
  709. txq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(txq, tx_desc);
  710. return txq->descs + tx_desc;
  711. }
  712. /* Release the last allocated TX descriptor. Useful to handle DMA
  713. * mapping failures in the TX path.
  714. */
  715. static void mvneta_txq_desc_put(struct mvneta_tx_queue *txq)
  716. {
  717. if (txq->next_desc_to_proc == 0)
  718. txq->next_desc_to_proc = txq->last_desc - 1;
  719. else
  720. txq->next_desc_to_proc--;
  721. }
  722. /* Set rxq buf size */
  723. static void mvneta_rxq_buf_size_set(struct mvneta_port *pp,
  724. struct mvneta_rx_queue *rxq,
  725. int buf_size)
  726. {
  727. u32 val;
  728. val = mvreg_read(pp, MVNETA_RXQ_SIZE_REG(rxq->id));
  729. val &= ~MVNETA_RXQ_BUF_SIZE_MASK;
  730. val |= ((buf_size >> 3) << MVNETA_RXQ_BUF_SIZE_SHIFT);
  731. mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), val);
  732. }
  733. /* Disable buffer management (BM) */
  734. static void mvneta_rxq_bm_disable(struct mvneta_port *pp,
  735. struct mvneta_rx_queue *rxq)
  736. {
  737. u32 val;
  738. val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
  739. val &= ~MVNETA_RXQ_HW_BUF_ALLOC;
  740. mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
  741. }
  742. /* Enable buffer management (BM) */
  743. static void mvneta_rxq_bm_enable(struct mvneta_port *pp,
  744. struct mvneta_rx_queue *rxq)
  745. {
  746. u32 val;
  747. val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
  748. val |= MVNETA_RXQ_HW_BUF_ALLOC;
  749. mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
  750. }
  751. /* Notify HW about port's assignment of pool for bigger packets */
  752. static void mvneta_rxq_long_pool_set(struct mvneta_port *pp,
  753. struct mvneta_rx_queue *rxq)
  754. {
  755. u32 val;
  756. val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
  757. val &= ~MVNETA_RXQ_LONG_POOL_ID_MASK;
  758. val |= (pp->pool_long->id << MVNETA_RXQ_LONG_POOL_ID_SHIFT);
  759. mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
  760. }
  761. /* Notify HW about port's assignment of pool for smaller packets */
  762. static void mvneta_rxq_short_pool_set(struct mvneta_port *pp,
  763. struct mvneta_rx_queue *rxq)
  764. {
  765. u32 val;
  766. val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
  767. val &= ~MVNETA_RXQ_SHORT_POOL_ID_MASK;
  768. val |= (pp->pool_short->id << MVNETA_RXQ_SHORT_POOL_ID_SHIFT);
  769. mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
  770. }
  771. /* Set port's receive buffer size for assigned BM pool */
  772. static inline void mvneta_bm_pool_bufsize_set(struct mvneta_port *pp,
  773. int buf_size,
  774. u8 pool_id)
  775. {
  776. u32 val;
  777. if (!IS_ALIGNED(buf_size, 8)) {
  778. dev_warn(pp->dev->dev.parent,
  779. "illegal buf_size value %d, round to %d\n",
  780. buf_size, ALIGN(buf_size, 8));
  781. buf_size = ALIGN(buf_size, 8);
  782. }
  783. val = mvreg_read(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id));
  784. val |= buf_size & MVNETA_PORT_POOL_BUFFER_SZ_MASK;
  785. mvreg_write(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id), val);
  786. }
  787. /* Configure MBUS window in order to enable access BM internal SRAM */
  788. static int mvneta_mbus_io_win_set(struct mvneta_port *pp, u32 base, u32 wsize,
  789. u8 target, u8 attr)
  790. {
  791. u32 win_enable, win_protect;
  792. int i;
  793. win_enable = mvreg_read(pp, MVNETA_BASE_ADDR_ENABLE);
  794. if (pp->bm_win_id < 0) {
  795. /* Find first not occupied window */
  796. for (i = 0; i < MVNETA_MAX_DECODE_WIN; i++) {
  797. if (win_enable & (1 << i)) {
  798. pp->bm_win_id = i;
  799. break;
  800. }
  801. }
  802. if (i == MVNETA_MAX_DECODE_WIN)
  803. return -ENOMEM;
  804. } else {
  805. i = pp->bm_win_id;
  806. }
  807. mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
  808. mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
  809. if (i < 4)
  810. mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
  811. mvreg_write(pp, MVNETA_WIN_BASE(i), (base & 0xffff0000) |
  812. (attr << 8) | target);
  813. mvreg_write(pp, MVNETA_WIN_SIZE(i), (wsize - 1) & 0xffff0000);
  814. win_protect = mvreg_read(pp, MVNETA_ACCESS_PROTECT_ENABLE);
  815. win_protect |= 3 << (2 * i);
  816. mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect);
  817. win_enable &= ~(1 << i);
  818. mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
  819. return 0;
  820. }
  821. static int mvneta_bm_port_mbus_init(struct mvneta_port *pp)
  822. {
  823. u32 wsize;
  824. u8 target, attr;
  825. int err;
  826. /* Get BM window information */
  827. err = mvebu_mbus_get_io_win_info(pp->bm_priv->bppi_phys_addr, &wsize,
  828. &target, &attr);
  829. if (err < 0)
  830. return err;
  831. pp->bm_win_id = -1;
  832. /* Open NETA -> BM window */
  833. err = mvneta_mbus_io_win_set(pp, pp->bm_priv->bppi_phys_addr, wsize,
  834. target, attr);
  835. if (err < 0) {
  836. netdev_info(pp->dev, "fail to configure mbus window to BM\n");
  837. return err;
  838. }
  839. return 0;
  840. }
  841. /* Assign and initialize pools for port. In case of fail
  842. * buffer manager will remain disabled for current port.
  843. */
  844. static int mvneta_bm_port_init(struct platform_device *pdev,
  845. struct mvneta_port *pp)
  846. {
  847. struct device_node *dn = pdev->dev.of_node;
  848. u32 long_pool_id, short_pool_id;
  849. if (!pp->neta_armada3700) {
  850. int ret;
  851. ret = mvneta_bm_port_mbus_init(pp);
  852. if (ret)
  853. return ret;
  854. }
  855. if (of_property_read_u32(dn, "bm,pool-long", &long_pool_id)) {
  856. netdev_info(pp->dev, "missing long pool id\n");
  857. return -EINVAL;
  858. }
  859. /* Create port's long pool depending on mtu */
  860. pp->pool_long = mvneta_bm_pool_use(pp->bm_priv, long_pool_id,
  861. MVNETA_BM_LONG, pp->id,
  862. MVNETA_RX_PKT_SIZE(pp->dev->mtu));
  863. if (!pp->pool_long) {
  864. netdev_info(pp->dev, "fail to obtain long pool for port\n");
  865. return -ENOMEM;
  866. }
  867. pp->pool_long->port_map |= 1 << pp->id;
  868. mvneta_bm_pool_bufsize_set(pp, pp->pool_long->buf_size,
  869. pp->pool_long->id);
  870. /* If short pool id is not defined, assume using single pool */
  871. if (of_property_read_u32(dn, "bm,pool-short", &short_pool_id))
  872. short_pool_id = long_pool_id;
  873. /* Create port's short pool */
  874. pp->pool_short = mvneta_bm_pool_use(pp->bm_priv, short_pool_id,
  875. MVNETA_BM_SHORT, pp->id,
  876. MVNETA_BM_SHORT_PKT_SIZE);
  877. if (!pp->pool_short) {
  878. netdev_info(pp->dev, "fail to obtain short pool for port\n");
  879. mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
  880. return -ENOMEM;
  881. }
  882. if (short_pool_id != long_pool_id) {
  883. pp->pool_short->port_map |= 1 << pp->id;
  884. mvneta_bm_pool_bufsize_set(pp, pp->pool_short->buf_size,
  885. pp->pool_short->id);
  886. }
  887. return 0;
  888. }
  889. /* Update settings of a pool for bigger packets */
  890. static void mvneta_bm_update_mtu(struct mvneta_port *pp, int mtu)
  891. {
  892. struct mvneta_bm_pool *bm_pool = pp->pool_long;
  893. struct hwbm_pool *hwbm_pool = &bm_pool->hwbm_pool;
  894. int num;
  895. /* Release all buffers from long pool */
  896. mvneta_bm_bufs_free(pp->bm_priv, bm_pool, 1 << pp->id);
  897. if (hwbm_pool->buf_num) {
  898. WARN(1, "cannot free all buffers in pool %d\n",
  899. bm_pool->id);
  900. goto bm_mtu_err;
  901. }
  902. bm_pool->pkt_size = MVNETA_RX_PKT_SIZE(mtu);
  903. bm_pool->buf_size = MVNETA_RX_BUF_SIZE(bm_pool->pkt_size);
  904. hwbm_pool->frag_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
  905. SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(bm_pool->pkt_size));
  906. /* Fill entire long pool */
  907. num = hwbm_pool_add(hwbm_pool, hwbm_pool->size, GFP_ATOMIC);
  908. if (num != hwbm_pool->size) {
  909. WARN(1, "pool %d: %d of %d allocated\n",
  910. bm_pool->id, num, hwbm_pool->size);
  911. goto bm_mtu_err;
  912. }
  913. mvneta_bm_pool_bufsize_set(pp, bm_pool->buf_size, bm_pool->id);
  914. return;
  915. bm_mtu_err:
  916. mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
  917. mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short, 1 << pp->id);
  918. pp->bm_priv = NULL;
  919. mvreg_write(pp, MVNETA_ACC_MODE, MVNETA_ACC_MODE_EXT1);
  920. netdev_info(pp->dev, "fail to update MTU, fall back to software BM\n");
  921. }
  922. /* Start the Ethernet port RX and TX activity */
  923. static void mvneta_port_up(struct mvneta_port *pp)
  924. {
  925. int queue;
  926. u32 q_map;
  927. /* Enable all initialized TXs. */
  928. q_map = 0;
  929. for (queue = 0; queue < txq_number; queue++) {
  930. struct mvneta_tx_queue *txq = &pp->txqs[queue];
  931. if (txq->descs)
  932. q_map |= (1 << queue);
  933. }
  934. mvreg_write(pp, MVNETA_TXQ_CMD, q_map);
  935. /* Enable all initialized RXQs. */
  936. for (queue = 0; queue < rxq_number; queue++) {
  937. struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
  938. if (rxq->descs)
  939. q_map |= (1 << queue);
  940. }
  941. mvreg_write(pp, MVNETA_RXQ_CMD, q_map);
  942. }
  943. /* Stop the Ethernet port activity */
  944. static void mvneta_port_down(struct mvneta_port *pp)
  945. {
  946. u32 val;
  947. int count;
  948. /* Stop Rx port activity. Check port Rx activity. */
  949. val = mvreg_read(pp, MVNETA_RXQ_CMD) & MVNETA_RXQ_ENABLE_MASK;
  950. /* Issue stop command for active channels only */
  951. if (val != 0)
  952. mvreg_write(pp, MVNETA_RXQ_CMD,
  953. val << MVNETA_RXQ_DISABLE_SHIFT);
  954. /* Wait for all Rx activity to terminate. */
  955. count = 0;
  956. do {
  957. if (count++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC) {
  958. netdev_warn(pp->dev,
  959. "TIMEOUT for RX stopped ! rx_queue_cmd: 0x%08x\n",
  960. val);
  961. break;
  962. }
  963. mdelay(1);
  964. val = mvreg_read(pp, MVNETA_RXQ_CMD);
  965. } while (val & MVNETA_RXQ_ENABLE_MASK);
  966. /* Stop Tx port activity. Check port Tx activity. Issue stop
  967. * command for active channels only
  968. */
  969. val = (mvreg_read(pp, MVNETA_TXQ_CMD)) & MVNETA_TXQ_ENABLE_MASK;
  970. if (val != 0)
  971. mvreg_write(pp, MVNETA_TXQ_CMD,
  972. (val << MVNETA_TXQ_DISABLE_SHIFT));
  973. /* Wait for all Tx activity to terminate. */
  974. count = 0;
  975. do {
  976. if (count++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC) {
  977. netdev_warn(pp->dev,
  978. "TIMEOUT for TX stopped status=0x%08x\n",
  979. val);
  980. break;
  981. }
  982. mdelay(1);
  983. /* Check TX Command reg that all Txqs are stopped */
  984. val = mvreg_read(pp, MVNETA_TXQ_CMD);
  985. } while (val & MVNETA_TXQ_ENABLE_MASK);
  986. /* Double check to verify that TX FIFO is empty */
  987. count = 0;
  988. do {
  989. if (count++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT) {
  990. netdev_warn(pp->dev,
  991. "TX FIFO empty timeout status=0x%08x\n",
  992. val);
  993. break;
  994. }
  995. mdelay(1);
  996. val = mvreg_read(pp, MVNETA_PORT_STATUS);
  997. } while (!(val & MVNETA_TX_FIFO_EMPTY) &&
  998. (val & MVNETA_TX_IN_PRGRS));
  999. udelay(200);
  1000. }
  1001. /* Enable the port by setting the port enable bit of the MAC control register */
  1002. static void mvneta_port_enable(struct mvneta_port *pp)
  1003. {
  1004. u32 val;
  1005. /* Enable port */
  1006. val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
  1007. val |= MVNETA_GMAC0_PORT_ENABLE;
  1008. mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
  1009. }
  1010. /* Disable the port and wait for about 200 usec before retuning */
  1011. static void mvneta_port_disable(struct mvneta_port *pp)
  1012. {
  1013. u32 val;
  1014. /* Reset the Enable bit in the Serial Control Register */
  1015. val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
  1016. val &= ~MVNETA_GMAC0_PORT_ENABLE;
  1017. mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
  1018. udelay(200);
  1019. }
  1020. /* Multicast tables methods */
  1021. /* Set all entries in Unicast MAC Table; queue==-1 means reject all */
  1022. static void mvneta_set_ucast_table(struct mvneta_port *pp, int queue)
  1023. {
  1024. int offset;
  1025. u32 val;
  1026. if (queue == -1) {
  1027. val = 0;
  1028. } else {
  1029. val = 0x1 | (queue << 1);
  1030. val |= (val << 24) | (val << 16) | (val << 8);
  1031. }
  1032. for (offset = 0; offset <= 0xc; offset += 4)
  1033. mvreg_write(pp, MVNETA_DA_FILT_UCAST_BASE + offset, val);
  1034. }
  1035. /* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */
  1036. static void mvneta_set_special_mcast_table(struct mvneta_port *pp, int queue)
  1037. {
  1038. int offset;
  1039. u32 val;
  1040. if (queue == -1) {
  1041. val = 0;
  1042. } else {
  1043. val = 0x1 | (queue << 1);
  1044. val |= (val << 24) | (val << 16) | (val << 8);
  1045. }
  1046. for (offset = 0; offset <= 0xfc; offset += 4)
  1047. mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + offset, val);
  1048. }
  1049. /* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */
  1050. static void mvneta_set_other_mcast_table(struct mvneta_port *pp, int queue)
  1051. {
  1052. int offset;
  1053. u32 val;
  1054. if (queue == -1) {
  1055. memset(pp->mcast_count, 0, sizeof(pp->mcast_count));
  1056. val = 0;
  1057. } else {
  1058. memset(pp->mcast_count, 1, sizeof(pp->mcast_count));
  1059. val = 0x1 | (queue << 1);
  1060. val |= (val << 24) | (val << 16) | (val << 8);
  1061. }
  1062. for (offset = 0; offset <= 0xfc; offset += 4)
  1063. mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val);
  1064. }
  1065. static void mvneta_set_autoneg(struct mvneta_port *pp, int enable)
  1066. {
  1067. u32 val;
  1068. if (enable) {
  1069. val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
  1070. val &= ~(MVNETA_GMAC_FORCE_LINK_PASS |
  1071. MVNETA_GMAC_FORCE_LINK_DOWN |
  1072. MVNETA_GMAC_AN_FLOW_CTRL_EN);
  1073. val |= MVNETA_GMAC_INBAND_AN_ENABLE |
  1074. MVNETA_GMAC_AN_SPEED_EN |
  1075. MVNETA_GMAC_AN_DUPLEX_EN;
  1076. mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
  1077. val = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER);
  1078. val |= MVNETA_GMAC_1MS_CLOCK_ENABLE;
  1079. mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, val);
  1080. val = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
  1081. val |= MVNETA_GMAC2_INBAND_AN_ENABLE;
  1082. mvreg_write(pp, MVNETA_GMAC_CTRL_2, val);
  1083. } else {
  1084. val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
  1085. val &= ~(MVNETA_GMAC_INBAND_AN_ENABLE |
  1086. MVNETA_GMAC_AN_SPEED_EN |
  1087. MVNETA_GMAC_AN_DUPLEX_EN);
  1088. mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
  1089. val = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER);
  1090. val &= ~MVNETA_GMAC_1MS_CLOCK_ENABLE;
  1091. mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, val);
  1092. val = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
  1093. val &= ~MVNETA_GMAC2_INBAND_AN_ENABLE;
  1094. mvreg_write(pp, MVNETA_GMAC_CTRL_2, val);
  1095. }
  1096. }
  1097. static void mvneta_percpu_unmask_interrupt(void *arg)
  1098. {
  1099. struct mvneta_port *pp = arg;
  1100. /* All the queue are unmasked, but actually only the ones
  1101. * mapped to this CPU will be unmasked
  1102. */
  1103. mvreg_write(pp, MVNETA_INTR_NEW_MASK,
  1104. MVNETA_RX_INTR_MASK_ALL |
  1105. MVNETA_TX_INTR_MASK_ALL |
  1106. MVNETA_MISCINTR_INTR_MASK);
  1107. }
  1108. static void mvneta_percpu_mask_interrupt(void *arg)
  1109. {
  1110. struct mvneta_port *pp = arg;
  1111. /* All the queue are masked, but actually only the ones
  1112. * mapped to this CPU will be masked
  1113. */
  1114. mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
  1115. mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
  1116. mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
  1117. }
  1118. static void mvneta_percpu_clear_intr_cause(void *arg)
  1119. {
  1120. struct mvneta_port *pp = arg;
  1121. /* All the queue are cleared, but actually only the ones
  1122. * mapped to this CPU will be cleared
  1123. */
  1124. mvreg_write(pp, MVNETA_INTR_NEW_CAUSE, 0);
  1125. mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
  1126. mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
  1127. }
  1128. /* This method sets defaults to the NETA port:
  1129. * Clears interrupt Cause and Mask registers.
  1130. * Clears all MAC tables.
  1131. * Sets defaults to all registers.
  1132. * Resets RX and TX descriptor rings.
  1133. * Resets PHY.
  1134. * This method can be called after mvneta_port_down() to return the port
  1135. * settings to defaults.
  1136. */
  1137. static void mvneta_defaults_set(struct mvneta_port *pp)
  1138. {
  1139. int cpu;
  1140. int queue;
  1141. u32 val;
  1142. int max_cpu = num_present_cpus();
  1143. /* Clear all Cause registers */
  1144. on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
  1145. /* Mask all interrupts */
  1146. on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
  1147. mvreg_write(pp, MVNETA_INTR_ENABLE, 0);
  1148. /* Enable MBUS Retry bit16 */
  1149. mvreg_write(pp, MVNETA_MBUS_RETRY, 0x20);
  1150. /* Set CPU queue access map. CPUs are assigned to the RX and
  1151. * TX queues modulo their number. If there is only one TX
  1152. * queue then it is assigned to the CPU associated to the
  1153. * default RX queue.
  1154. */
  1155. for_each_present_cpu(cpu) {
  1156. int rxq_map = 0, txq_map = 0;
  1157. int rxq, txq;
  1158. if (!pp->neta_armada3700) {
  1159. for (rxq = 0; rxq < rxq_number; rxq++)
  1160. if ((rxq % max_cpu) == cpu)
  1161. rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
  1162. for (txq = 0; txq < txq_number; txq++)
  1163. if ((txq % max_cpu) == cpu)
  1164. txq_map |= MVNETA_CPU_TXQ_ACCESS(txq);
  1165. /* With only one TX queue we configure a special case
  1166. * which will allow to get all the irq on a single
  1167. * CPU
  1168. */
  1169. if (txq_number == 1)
  1170. txq_map = (cpu == pp->rxq_def) ?
  1171. MVNETA_CPU_TXQ_ACCESS(1) : 0;
  1172. } else {
  1173. txq_map = MVNETA_CPU_TXQ_ACCESS_ALL_MASK;
  1174. rxq_map = MVNETA_CPU_RXQ_ACCESS_ALL_MASK;
  1175. }
  1176. mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
  1177. }
  1178. /* Reset RX and TX DMAs */
  1179. mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
  1180. mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
  1181. /* Disable Legacy WRR, Disable EJP, Release from reset */
  1182. mvreg_write(pp, MVNETA_TXQ_CMD_1, 0);
  1183. for (queue = 0; queue < txq_number; queue++) {
  1184. mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(queue), 0);
  1185. mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(queue), 0);
  1186. }
  1187. mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
  1188. mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
  1189. /* Set Port Acceleration Mode */
  1190. if (pp->bm_priv)
  1191. /* HW buffer management + legacy parser */
  1192. val = MVNETA_ACC_MODE_EXT2;
  1193. else
  1194. /* SW buffer management + legacy parser */
  1195. val = MVNETA_ACC_MODE_EXT1;
  1196. mvreg_write(pp, MVNETA_ACC_MODE, val);
  1197. if (pp->bm_priv)
  1198. mvreg_write(pp, MVNETA_BM_ADDRESS, pp->bm_priv->bppi_phys_addr);
  1199. /* Update val of portCfg register accordingly with all RxQueue types */
  1200. val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
  1201. mvreg_write(pp, MVNETA_PORT_CONFIG, val);
  1202. val = 0;
  1203. mvreg_write(pp, MVNETA_PORT_CONFIG_EXTEND, val);
  1204. mvreg_write(pp, MVNETA_RX_MIN_FRAME_SIZE, 64);
  1205. /* Build PORT_SDMA_CONFIG_REG */
  1206. val = 0;
  1207. /* Default burst size */
  1208. val |= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
  1209. val |= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
  1210. val |= MVNETA_RX_NO_DATA_SWAP | MVNETA_TX_NO_DATA_SWAP;
  1211. #if defined(__BIG_ENDIAN)
  1212. val |= MVNETA_DESC_SWAP;
  1213. #endif
  1214. /* Assign port SDMA configuration */
  1215. mvreg_write(pp, MVNETA_SDMA_CONFIG, val);
  1216. /* Disable PHY polling in hardware, since we're using the
  1217. * kernel phylib to do this.
  1218. */
  1219. val = mvreg_read(pp, MVNETA_UNIT_CONTROL);
  1220. val &= ~MVNETA_PHY_POLLING_ENABLE;
  1221. mvreg_write(pp, MVNETA_UNIT_CONTROL, val);
  1222. mvneta_set_autoneg(pp, pp->use_inband_status);
  1223. mvneta_set_ucast_table(pp, -1);
  1224. mvneta_set_special_mcast_table(pp, -1);
  1225. mvneta_set_other_mcast_table(pp, -1);
  1226. /* Set port interrupt enable register - default enable all */
  1227. mvreg_write(pp, MVNETA_INTR_ENABLE,
  1228. (MVNETA_RXQ_INTR_ENABLE_ALL_MASK
  1229. | MVNETA_TXQ_INTR_ENABLE_ALL_MASK));
  1230. mvneta_mib_counters_clear(pp);
  1231. }
  1232. /* Set max sizes for tx queues */
  1233. static void mvneta_txq_max_tx_size_set(struct mvneta_port *pp, int max_tx_size)
  1234. {
  1235. u32 val, size, mtu;
  1236. int queue;
  1237. mtu = max_tx_size * 8;
  1238. if (mtu > MVNETA_TX_MTU_MAX)
  1239. mtu = MVNETA_TX_MTU_MAX;
  1240. /* Set MTU */
  1241. val = mvreg_read(pp, MVNETA_TX_MTU);
  1242. val &= ~MVNETA_TX_MTU_MAX;
  1243. val |= mtu;
  1244. mvreg_write(pp, MVNETA_TX_MTU, val);
  1245. /* TX token size and all TXQs token size must be larger that MTU */
  1246. val = mvreg_read(pp, MVNETA_TX_TOKEN_SIZE);
  1247. size = val & MVNETA_TX_TOKEN_SIZE_MAX;
  1248. if (size < mtu) {
  1249. size = mtu;
  1250. val &= ~MVNETA_TX_TOKEN_SIZE_MAX;
  1251. val |= size;
  1252. mvreg_write(pp, MVNETA_TX_TOKEN_SIZE, val);
  1253. }
  1254. for (queue = 0; queue < txq_number; queue++) {
  1255. val = mvreg_read(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue));
  1256. size = val & MVNETA_TXQ_TOKEN_SIZE_MAX;
  1257. if (size < mtu) {
  1258. size = mtu;
  1259. val &= ~MVNETA_TXQ_TOKEN_SIZE_MAX;
  1260. val |= size;
  1261. mvreg_write(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue), val);
  1262. }
  1263. }
  1264. }
  1265. /* Set unicast address */
  1266. static void mvneta_set_ucast_addr(struct mvneta_port *pp, u8 last_nibble,
  1267. int queue)
  1268. {
  1269. unsigned int unicast_reg;
  1270. unsigned int tbl_offset;
  1271. unsigned int reg_offset;
  1272. /* Locate the Unicast table entry */
  1273. last_nibble = (0xf & last_nibble);
  1274. /* offset from unicast tbl base */
  1275. tbl_offset = (last_nibble / 4) * 4;
  1276. /* offset within the above reg */
  1277. reg_offset = last_nibble % 4;
  1278. unicast_reg = mvreg_read(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset));
  1279. if (queue == -1) {
  1280. /* Clear accepts frame bit at specified unicast DA tbl entry */
  1281. unicast_reg &= ~(0xff << (8 * reg_offset));
  1282. } else {
  1283. unicast_reg &= ~(0xff << (8 * reg_offset));
  1284. unicast_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
  1285. }
  1286. mvreg_write(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset), unicast_reg);
  1287. }
  1288. /* Set mac address */
  1289. static void mvneta_mac_addr_set(struct mvneta_port *pp, unsigned char *addr,
  1290. int queue)
  1291. {
  1292. unsigned int mac_h;
  1293. unsigned int mac_l;
  1294. if (queue != -1) {
  1295. mac_l = (addr[4] << 8) | (addr[5]);
  1296. mac_h = (addr[0] << 24) | (addr[1] << 16) |
  1297. (addr[2] << 8) | (addr[3] << 0);
  1298. mvreg_write(pp, MVNETA_MAC_ADDR_LOW, mac_l);
  1299. mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, mac_h);
  1300. }
  1301. /* Accept frames of this address */
  1302. mvneta_set_ucast_addr(pp, addr[5], queue);
  1303. }
  1304. /* Set the number of packets that will be received before RX interrupt
  1305. * will be generated by HW.
  1306. */
  1307. static void mvneta_rx_pkts_coal_set(struct mvneta_port *pp,
  1308. struct mvneta_rx_queue *rxq, u32 value)
  1309. {
  1310. mvreg_write(pp, MVNETA_RXQ_THRESHOLD_REG(rxq->id),
  1311. value | MVNETA_RXQ_NON_OCCUPIED(0));
  1312. rxq->pkts_coal = value;
  1313. }
  1314. /* Set the time delay in usec before RX interrupt will be generated by
  1315. * HW.
  1316. */
  1317. static void mvneta_rx_time_coal_set(struct mvneta_port *pp,
  1318. struct mvneta_rx_queue *rxq, u32 value)
  1319. {
  1320. u32 val;
  1321. unsigned long clk_rate;
  1322. clk_rate = clk_get_rate(pp->clk);
  1323. val = (clk_rate / 1000000) * value;
  1324. mvreg_write(pp, MVNETA_RXQ_TIME_COAL_REG(rxq->id), val);
  1325. rxq->time_coal = value;
  1326. }
  1327. /* Set threshold for TX_DONE pkts coalescing */
  1328. static void mvneta_tx_done_pkts_coal_set(struct mvneta_port *pp,
  1329. struct mvneta_tx_queue *txq, u32 value)
  1330. {
  1331. u32 val;
  1332. val = mvreg_read(pp, MVNETA_TXQ_SIZE_REG(txq->id));
  1333. val &= ~MVNETA_TXQ_SENT_THRESH_ALL_MASK;
  1334. val |= MVNETA_TXQ_SENT_THRESH_MASK(value);
  1335. mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), val);
  1336. txq->done_pkts_coal = value;
  1337. }
  1338. /* Handle rx descriptor fill by setting buf_cookie and buf_phys_addr */
  1339. static void mvneta_rx_desc_fill(struct mvneta_rx_desc *rx_desc,
  1340. u32 phys_addr, void *virt_addr,
  1341. struct mvneta_rx_queue *rxq)
  1342. {
  1343. int i;
  1344. rx_desc->buf_phys_addr = phys_addr;
  1345. i = rx_desc - rxq->descs;
  1346. rxq->buf_virt_addr[i] = virt_addr;
  1347. }
  1348. /* Decrement sent descriptors counter */
  1349. static void mvneta_txq_sent_desc_dec(struct mvneta_port *pp,
  1350. struct mvneta_tx_queue *txq,
  1351. int sent_desc)
  1352. {
  1353. u32 val;
  1354. /* Only 255 TX descriptors can be updated at once */
  1355. while (sent_desc > 0xff) {
  1356. val = 0xff << MVNETA_TXQ_DEC_SENT_SHIFT;
  1357. mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
  1358. sent_desc = sent_desc - 0xff;
  1359. }
  1360. val = sent_desc << MVNETA_TXQ_DEC_SENT_SHIFT;
  1361. mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
  1362. }
  1363. /* Get number of TX descriptors already sent by HW */
  1364. static int mvneta_txq_sent_desc_num_get(struct mvneta_port *pp,
  1365. struct mvneta_tx_queue *txq)
  1366. {
  1367. u32 val;
  1368. int sent_desc;
  1369. val = mvreg_read(pp, MVNETA_TXQ_STATUS_REG(txq->id));
  1370. sent_desc = (val & MVNETA_TXQ_SENT_DESC_MASK) >>
  1371. MVNETA_TXQ_SENT_DESC_SHIFT;
  1372. return sent_desc;
  1373. }
  1374. /* Get number of sent descriptors and decrement counter.
  1375. * The number of sent descriptors is returned.
  1376. */
  1377. static int mvneta_txq_sent_desc_proc(struct mvneta_port *pp,
  1378. struct mvneta_tx_queue *txq)
  1379. {
  1380. int sent_desc;
  1381. /* Get number of sent descriptors */
  1382. sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
  1383. /* Decrement sent descriptors counter */
  1384. if (sent_desc)
  1385. mvneta_txq_sent_desc_dec(pp, txq, sent_desc);
  1386. return sent_desc;
  1387. }
  1388. /* Set TXQ descriptors fields relevant for CSUM calculation */
  1389. static u32 mvneta_txq_desc_csum(int l3_offs, int l3_proto,
  1390. int ip_hdr_len, int l4_proto)
  1391. {
  1392. u32 command;
  1393. /* Fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
  1394. * G_L4_chk, L4_type; required only for checksum
  1395. * calculation
  1396. */
  1397. command = l3_offs << MVNETA_TX_L3_OFF_SHIFT;
  1398. command |= ip_hdr_len << MVNETA_TX_IP_HLEN_SHIFT;
  1399. if (l3_proto == htons(ETH_P_IP))
  1400. command |= MVNETA_TXD_IP_CSUM;
  1401. else
  1402. command |= MVNETA_TX_L3_IP6;
  1403. if (l4_proto == IPPROTO_TCP)
  1404. command |= MVNETA_TX_L4_CSUM_FULL;
  1405. else if (l4_proto == IPPROTO_UDP)
  1406. command |= MVNETA_TX_L4_UDP | MVNETA_TX_L4_CSUM_FULL;
  1407. else
  1408. command |= MVNETA_TX_L4_CSUM_NOT;
  1409. return command;
  1410. }
  1411. /* Display more error info */
  1412. static void mvneta_rx_error(struct mvneta_port *pp,
  1413. struct mvneta_rx_desc *rx_desc)
  1414. {
  1415. u32 status = rx_desc->status;
  1416. if (!mvneta_rxq_desc_is_first_last(status)) {
  1417. netdev_err(pp->dev,
  1418. "bad rx status %08x (buffer oversize), size=%d\n",
  1419. status, rx_desc->data_size);
  1420. return;
  1421. }
  1422. switch (status & MVNETA_RXD_ERR_CODE_MASK) {
  1423. case MVNETA_RXD_ERR_CRC:
  1424. netdev_err(pp->dev, "bad rx status %08x (crc error), size=%d\n",
  1425. status, rx_desc->data_size);
  1426. break;
  1427. case MVNETA_RXD_ERR_OVERRUN:
  1428. netdev_err(pp->dev, "bad rx status %08x (overrun error), size=%d\n",
  1429. status, rx_desc->data_size);
  1430. break;
  1431. case MVNETA_RXD_ERR_LEN:
  1432. netdev_err(pp->dev, "bad rx status %08x (max frame length error), size=%d\n",
  1433. status, rx_desc->data_size);
  1434. break;
  1435. case MVNETA_RXD_ERR_RESOURCE:
  1436. netdev_err(pp->dev, "bad rx status %08x (resource error), size=%d\n",
  1437. status, rx_desc->data_size);
  1438. break;
  1439. }
  1440. }
  1441. /* Handle RX checksum offload based on the descriptor's status */
  1442. static void mvneta_rx_csum(struct mvneta_port *pp, u32 status,
  1443. struct sk_buff *skb)
  1444. {
  1445. if ((status & MVNETA_RXD_L3_IP4) &&
  1446. (status & MVNETA_RXD_L4_CSUM_OK)) {
  1447. skb->csum = 0;
  1448. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1449. return;
  1450. }
  1451. skb->ip_summed = CHECKSUM_NONE;
  1452. }
  1453. /* Return tx queue pointer (find last set bit) according to <cause> returned
  1454. * form tx_done reg. <cause> must not be null. The return value is always a
  1455. * valid queue for matching the first one found in <cause>.
  1456. */
  1457. static struct mvneta_tx_queue *mvneta_tx_done_policy(struct mvneta_port *pp,
  1458. u32 cause)
  1459. {
  1460. int queue = fls(cause) - 1;
  1461. return &pp->txqs[queue];
  1462. }
  1463. /* Free tx queue skbuffs */
  1464. static void mvneta_txq_bufs_free(struct mvneta_port *pp,
  1465. struct mvneta_tx_queue *txq, int num,
  1466. struct netdev_queue *nq)
  1467. {
  1468. unsigned int bytes_compl = 0, pkts_compl = 0;
  1469. int i;
  1470. for (i = 0; i < num; i++) {
  1471. struct mvneta_tx_desc *tx_desc = txq->descs +
  1472. txq->txq_get_index;
  1473. struct sk_buff *skb = txq->tx_skb[txq->txq_get_index];
  1474. if (skb) {
  1475. bytes_compl += skb->len;
  1476. pkts_compl++;
  1477. }
  1478. mvneta_txq_inc_get(txq);
  1479. if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr))
  1480. dma_unmap_single(pp->dev->dev.parent,
  1481. tx_desc->buf_phys_addr,
  1482. tx_desc->data_size, DMA_TO_DEVICE);
  1483. if (!skb)
  1484. continue;
  1485. dev_kfree_skb_any(skb);
  1486. }
  1487. netdev_tx_completed_queue(nq, pkts_compl, bytes_compl);
  1488. }
  1489. /* Handle end of transmission */
  1490. static void mvneta_txq_done(struct mvneta_port *pp,
  1491. struct mvneta_tx_queue *txq)
  1492. {
  1493. struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
  1494. int tx_done;
  1495. tx_done = mvneta_txq_sent_desc_proc(pp, txq);
  1496. if (!tx_done)
  1497. return;
  1498. mvneta_txq_bufs_free(pp, txq, tx_done, nq);
  1499. txq->count -= tx_done;
  1500. if (netif_tx_queue_stopped(nq)) {
  1501. if (txq->count <= txq->tx_wake_threshold)
  1502. netif_tx_wake_queue(nq);
  1503. }
  1504. }
  1505. void *mvneta_frag_alloc(unsigned int frag_size)
  1506. {
  1507. if (likely(frag_size <= PAGE_SIZE))
  1508. return netdev_alloc_frag(frag_size);
  1509. else
  1510. return kmalloc(frag_size, GFP_ATOMIC);
  1511. }
  1512. EXPORT_SYMBOL_GPL(mvneta_frag_alloc);
  1513. void mvneta_frag_free(unsigned int frag_size, void *data)
  1514. {
  1515. if (likely(frag_size <= PAGE_SIZE))
  1516. skb_free_frag(data);
  1517. else
  1518. kfree(data);
  1519. }
  1520. EXPORT_SYMBOL_GPL(mvneta_frag_free);
  1521. /* Refill processing for SW buffer management */
  1522. static int mvneta_rx_refill(struct mvneta_port *pp,
  1523. struct mvneta_rx_desc *rx_desc,
  1524. struct mvneta_rx_queue *rxq)
  1525. {
  1526. dma_addr_t phys_addr;
  1527. void *data;
  1528. data = mvneta_frag_alloc(pp->frag_size);
  1529. if (!data)
  1530. return -ENOMEM;
  1531. phys_addr = dma_map_single(pp->dev->dev.parent, data,
  1532. MVNETA_RX_BUF_SIZE(pp->pkt_size),
  1533. DMA_FROM_DEVICE);
  1534. if (unlikely(dma_mapping_error(pp->dev->dev.parent, phys_addr))) {
  1535. mvneta_frag_free(pp->frag_size, data);
  1536. return -ENOMEM;
  1537. }
  1538. phys_addr += pp->rx_offset_correction;
  1539. mvneta_rx_desc_fill(rx_desc, phys_addr, data, rxq);
  1540. return 0;
  1541. }
  1542. /* Handle tx checksum */
  1543. static u32 mvneta_skb_tx_csum(struct mvneta_port *pp, struct sk_buff *skb)
  1544. {
  1545. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1546. int ip_hdr_len = 0;
  1547. __be16 l3_proto = vlan_get_protocol(skb);
  1548. u8 l4_proto;
  1549. if (l3_proto == htons(ETH_P_IP)) {
  1550. struct iphdr *ip4h = ip_hdr(skb);
  1551. /* Calculate IPv4 checksum and L4 checksum */
  1552. ip_hdr_len = ip4h->ihl;
  1553. l4_proto = ip4h->protocol;
  1554. } else if (l3_proto == htons(ETH_P_IPV6)) {
  1555. struct ipv6hdr *ip6h = ipv6_hdr(skb);
  1556. /* Read l4_protocol from one of IPv6 extra headers */
  1557. if (skb_network_header_len(skb) > 0)
  1558. ip_hdr_len = (skb_network_header_len(skb) >> 2);
  1559. l4_proto = ip6h->nexthdr;
  1560. } else
  1561. return MVNETA_TX_L4_CSUM_NOT;
  1562. return mvneta_txq_desc_csum(skb_network_offset(skb),
  1563. l3_proto, ip_hdr_len, l4_proto);
  1564. }
  1565. return MVNETA_TX_L4_CSUM_NOT;
  1566. }
  1567. /* Drop packets received by the RXQ and free buffers */
  1568. static void mvneta_rxq_drop_pkts(struct mvneta_port *pp,
  1569. struct mvneta_rx_queue *rxq)
  1570. {
  1571. int rx_done, i;
  1572. rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
  1573. if (rx_done)
  1574. mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
  1575. if (pp->bm_priv) {
  1576. for (i = 0; i < rx_done; i++) {
  1577. struct mvneta_rx_desc *rx_desc =
  1578. mvneta_rxq_next_desc_get(rxq);
  1579. u8 pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc);
  1580. struct mvneta_bm_pool *bm_pool;
  1581. bm_pool = &pp->bm_priv->bm_pools[pool_id];
  1582. /* Return dropped buffer to the pool */
  1583. mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
  1584. rx_desc->buf_phys_addr);
  1585. }
  1586. return;
  1587. }
  1588. for (i = 0; i < rxq->size; i++) {
  1589. struct mvneta_rx_desc *rx_desc = rxq->descs + i;
  1590. void *data = rxq->buf_virt_addr[i];
  1591. dma_unmap_single(pp->dev->dev.parent, rx_desc->buf_phys_addr,
  1592. MVNETA_RX_BUF_SIZE(pp->pkt_size), DMA_FROM_DEVICE);
  1593. mvneta_frag_free(pp->frag_size, data);
  1594. }
  1595. }
  1596. /* Main rx processing when using software buffer management */
  1597. static int mvneta_rx_swbm(struct mvneta_port *pp, int rx_todo,
  1598. struct mvneta_rx_queue *rxq)
  1599. {
  1600. struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
  1601. struct net_device *dev = pp->dev;
  1602. int rx_done;
  1603. u32 rcvd_pkts = 0;
  1604. u32 rcvd_bytes = 0;
  1605. /* Get number of received packets */
  1606. rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
  1607. if (rx_todo > rx_done)
  1608. rx_todo = rx_done;
  1609. rx_done = 0;
  1610. /* Fairness NAPI loop */
  1611. while (rx_done < rx_todo) {
  1612. struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
  1613. struct sk_buff *skb;
  1614. unsigned char *data;
  1615. dma_addr_t phys_addr;
  1616. u32 rx_status, frag_size;
  1617. int rx_bytes, err, index;
  1618. rx_done++;
  1619. rx_status = rx_desc->status;
  1620. rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE);
  1621. index = rx_desc - rxq->descs;
  1622. data = rxq->buf_virt_addr[index];
  1623. phys_addr = rx_desc->buf_phys_addr;
  1624. if (!mvneta_rxq_desc_is_first_last(rx_status) ||
  1625. (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
  1626. err_drop_frame:
  1627. dev->stats.rx_errors++;
  1628. mvneta_rx_error(pp, rx_desc);
  1629. /* leave the descriptor untouched */
  1630. continue;
  1631. }
  1632. if (rx_bytes <= rx_copybreak) {
  1633. /* better copy a small frame and not unmap the DMA region */
  1634. skb = netdev_alloc_skb_ip_align(dev, rx_bytes);
  1635. if (unlikely(!skb))
  1636. goto err_drop_frame;
  1637. dma_sync_single_range_for_cpu(dev->dev.parent,
  1638. phys_addr,
  1639. MVNETA_MH_SIZE + NET_SKB_PAD,
  1640. rx_bytes,
  1641. DMA_FROM_DEVICE);
  1642. memcpy(skb_put(skb, rx_bytes),
  1643. data + MVNETA_MH_SIZE + NET_SKB_PAD,
  1644. rx_bytes);
  1645. skb->protocol = eth_type_trans(skb, dev);
  1646. mvneta_rx_csum(pp, rx_status, skb);
  1647. napi_gro_receive(&port->napi, skb);
  1648. rcvd_pkts++;
  1649. rcvd_bytes += rx_bytes;
  1650. /* leave the descriptor and buffer untouched */
  1651. continue;
  1652. }
  1653. /* Refill processing */
  1654. err = mvneta_rx_refill(pp, rx_desc, rxq);
  1655. if (err) {
  1656. netdev_err(dev, "Linux processing - Can't refill\n");
  1657. rxq->missed++;
  1658. goto err_drop_frame;
  1659. }
  1660. frag_size = pp->frag_size;
  1661. skb = build_skb(data, frag_size > PAGE_SIZE ? 0 : frag_size);
  1662. /* After refill old buffer has to be unmapped regardless
  1663. * the skb is successfully built or not.
  1664. */
  1665. dma_unmap_single(dev->dev.parent, phys_addr,
  1666. MVNETA_RX_BUF_SIZE(pp->pkt_size),
  1667. DMA_FROM_DEVICE);
  1668. if (!skb)
  1669. goto err_drop_frame;
  1670. rcvd_pkts++;
  1671. rcvd_bytes += rx_bytes;
  1672. /* Linux processing */
  1673. skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD);
  1674. skb_put(skb, rx_bytes);
  1675. skb->protocol = eth_type_trans(skb, dev);
  1676. mvneta_rx_csum(pp, rx_status, skb);
  1677. napi_gro_receive(&port->napi, skb);
  1678. }
  1679. if (rcvd_pkts) {
  1680. struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
  1681. u64_stats_update_begin(&stats->syncp);
  1682. stats->rx_packets += rcvd_pkts;
  1683. stats->rx_bytes += rcvd_bytes;
  1684. u64_stats_update_end(&stats->syncp);
  1685. }
  1686. /* Update rxq management counters */
  1687. mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
  1688. return rx_done;
  1689. }
  1690. /* Main rx processing when using hardware buffer management */
  1691. static int mvneta_rx_hwbm(struct mvneta_port *pp, int rx_todo,
  1692. struct mvneta_rx_queue *rxq)
  1693. {
  1694. struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
  1695. struct net_device *dev = pp->dev;
  1696. int rx_done;
  1697. u32 rcvd_pkts = 0;
  1698. u32 rcvd_bytes = 0;
  1699. /* Get number of received packets */
  1700. rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
  1701. if (rx_todo > rx_done)
  1702. rx_todo = rx_done;
  1703. rx_done = 0;
  1704. /* Fairness NAPI loop */
  1705. while (rx_done < rx_todo) {
  1706. struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
  1707. struct mvneta_bm_pool *bm_pool = NULL;
  1708. struct sk_buff *skb;
  1709. unsigned char *data;
  1710. dma_addr_t phys_addr;
  1711. u32 rx_status, frag_size;
  1712. int rx_bytes, err;
  1713. u8 pool_id;
  1714. rx_done++;
  1715. rx_status = rx_desc->status;
  1716. rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE);
  1717. data = (u8 *)(uintptr_t)rx_desc->buf_cookie;
  1718. phys_addr = rx_desc->buf_phys_addr;
  1719. pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc);
  1720. bm_pool = &pp->bm_priv->bm_pools[pool_id];
  1721. if (!mvneta_rxq_desc_is_first_last(rx_status) ||
  1722. (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
  1723. err_drop_frame_ret_pool:
  1724. /* Return the buffer to the pool */
  1725. mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
  1726. rx_desc->buf_phys_addr);
  1727. err_drop_frame:
  1728. dev->stats.rx_errors++;
  1729. mvneta_rx_error(pp, rx_desc);
  1730. /* leave the descriptor untouched */
  1731. continue;
  1732. }
  1733. if (rx_bytes <= rx_copybreak) {
  1734. /* better copy a small frame and not unmap the DMA region */
  1735. skb = netdev_alloc_skb_ip_align(dev, rx_bytes);
  1736. if (unlikely(!skb))
  1737. goto err_drop_frame_ret_pool;
  1738. dma_sync_single_range_for_cpu(dev->dev.parent,
  1739. rx_desc->buf_phys_addr,
  1740. MVNETA_MH_SIZE + NET_SKB_PAD,
  1741. rx_bytes,
  1742. DMA_FROM_DEVICE);
  1743. memcpy(skb_put(skb, rx_bytes),
  1744. data + MVNETA_MH_SIZE + NET_SKB_PAD,
  1745. rx_bytes);
  1746. skb->protocol = eth_type_trans(skb, dev);
  1747. mvneta_rx_csum(pp, rx_status, skb);
  1748. napi_gro_receive(&port->napi, skb);
  1749. rcvd_pkts++;
  1750. rcvd_bytes += rx_bytes;
  1751. /* Return the buffer to the pool */
  1752. mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
  1753. rx_desc->buf_phys_addr);
  1754. /* leave the descriptor and buffer untouched */
  1755. continue;
  1756. }
  1757. /* Refill processing */
  1758. err = hwbm_pool_refill(&bm_pool->hwbm_pool, GFP_ATOMIC);
  1759. if (err) {
  1760. netdev_err(dev, "Linux processing - Can't refill\n");
  1761. rxq->missed++;
  1762. goto err_drop_frame_ret_pool;
  1763. }
  1764. frag_size = bm_pool->hwbm_pool.frag_size;
  1765. skb = build_skb(data, frag_size > PAGE_SIZE ? 0 : frag_size);
  1766. /* After refill old buffer has to be unmapped regardless
  1767. * the skb is successfully built or not.
  1768. */
  1769. dma_unmap_single(&pp->bm_priv->pdev->dev, phys_addr,
  1770. bm_pool->buf_size, DMA_FROM_DEVICE);
  1771. if (!skb)
  1772. goto err_drop_frame;
  1773. rcvd_pkts++;
  1774. rcvd_bytes += rx_bytes;
  1775. /* Linux processing */
  1776. skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD);
  1777. skb_put(skb, rx_bytes);
  1778. skb->protocol = eth_type_trans(skb, dev);
  1779. mvneta_rx_csum(pp, rx_status, skb);
  1780. napi_gro_receive(&port->napi, skb);
  1781. }
  1782. if (rcvd_pkts) {
  1783. struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
  1784. u64_stats_update_begin(&stats->syncp);
  1785. stats->rx_packets += rcvd_pkts;
  1786. stats->rx_bytes += rcvd_bytes;
  1787. u64_stats_update_end(&stats->syncp);
  1788. }
  1789. /* Update rxq management counters */
  1790. mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
  1791. return rx_done;
  1792. }
  1793. static inline void
  1794. mvneta_tso_put_hdr(struct sk_buff *skb,
  1795. struct mvneta_port *pp, struct mvneta_tx_queue *txq)
  1796. {
  1797. struct mvneta_tx_desc *tx_desc;
  1798. int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  1799. txq->tx_skb[txq->txq_put_index] = NULL;
  1800. tx_desc = mvneta_txq_next_desc_get(txq);
  1801. tx_desc->data_size = hdr_len;
  1802. tx_desc->command = mvneta_skb_tx_csum(pp, skb);
  1803. tx_desc->command |= MVNETA_TXD_F_DESC;
  1804. tx_desc->buf_phys_addr = txq->tso_hdrs_phys +
  1805. txq->txq_put_index * TSO_HEADER_SIZE;
  1806. mvneta_txq_inc_put(txq);
  1807. }
  1808. static inline int
  1809. mvneta_tso_put_data(struct net_device *dev, struct mvneta_tx_queue *txq,
  1810. struct sk_buff *skb, char *data, int size,
  1811. bool last_tcp, bool is_last)
  1812. {
  1813. struct mvneta_tx_desc *tx_desc;
  1814. tx_desc = mvneta_txq_next_desc_get(txq);
  1815. tx_desc->data_size = size;
  1816. tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, data,
  1817. size, DMA_TO_DEVICE);
  1818. if (unlikely(dma_mapping_error(dev->dev.parent,
  1819. tx_desc->buf_phys_addr))) {
  1820. mvneta_txq_desc_put(txq);
  1821. return -ENOMEM;
  1822. }
  1823. tx_desc->command = 0;
  1824. txq->tx_skb[txq->txq_put_index] = NULL;
  1825. if (last_tcp) {
  1826. /* last descriptor in the TCP packet */
  1827. tx_desc->command = MVNETA_TXD_L_DESC;
  1828. /* last descriptor in SKB */
  1829. if (is_last)
  1830. txq->tx_skb[txq->txq_put_index] = skb;
  1831. }
  1832. mvneta_txq_inc_put(txq);
  1833. return 0;
  1834. }
  1835. static int mvneta_tx_tso(struct sk_buff *skb, struct net_device *dev,
  1836. struct mvneta_tx_queue *txq)
  1837. {
  1838. int total_len, data_left;
  1839. int desc_count = 0;
  1840. struct mvneta_port *pp = netdev_priv(dev);
  1841. struct tso_t tso;
  1842. int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  1843. int i;
  1844. /* Count needed descriptors */
  1845. if ((txq->count + tso_count_descs(skb)) >= txq->size)
  1846. return 0;
  1847. if (skb_headlen(skb) < (skb_transport_offset(skb) + tcp_hdrlen(skb))) {
  1848. pr_info("*** Is this even possible???!?!?\n");
  1849. return 0;
  1850. }
  1851. /* Initialize the TSO handler, and prepare the first payload */
  1852. tso_start(skb, &tso);
  1853. total_len = skb->len - hdr_len;
  1854. while (total_len > 0) {
  1855. char *hdr;
  1856. data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
  1857. total_len -= data_left;
  1858. desc_count++;
  1859. /* prepare packet headers: MAC + IP + TCP */
  1860. hdr = txq->tso_hdrs + txq->txq_put_index * TSO_HEADER_SIZE;
  1861. tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
  1862. mvneta_tso_put_hdr(skb, pp, txq);
  1863. while (data_left > 0) {
  1864. int size;
  1865. desc_count++;
  1866. size = min_t(int, tso.size, data_left);
  1867. if (mvneta_tso_put_data(dev, txq, skb,
  1868. tso.data, size,
  1869. size == data_left,
  1870. total_len == 0))
  1871. goto err_release;
  1872. data_left -= size;
  1873. tso_build_data(skb, &tso, size);
  1874. }
  1875. }
  1876. return desc_count;
  1877. err_release:
  1878. /* Release all used data descriptors; header descriptors must not
  1879. * be DMA-unmapped.
  1880. */
  1881. for (i = desc_count - 1; i >= 0; i--) {
  1882. struct mvneta_tx_desc *tx_desc = txq->descs + i;
  1883. if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr))
  1884. dma_unmap_single(pp->dev->dev.parent,
  1885. tx_desc->buf_phys_addr,
  1886. tx_desc->data_size,
  1887. DMA_TO_DEVICE);
  1888. mvneta_txq_desc_put(txq);
  1889. }
  1890. return 0;
  1891. }
  1892. /* Handle tx fragmentation processing */
  1893. static int mvneta_tx_frag_process(struct mvneta_port *pp, struct sk_buff *skb,
  1894. struct mvneta_tx_queue *txq)
  1895. {
  1896. struct mvneta_tx_desc *tx_desc;
  1897. int i, nr_frags = skb_shinfo(skb)->nr_frags;
  1898. for (i = 0; i < nr_frags; i++) {
  1899. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1900. void *addr = page_address(frag->page.p) + frag->page_offset;
  1901. tx_desc = mvneta_txq_next_desc_get(txq);
  1902. tx_desc->data_size = frag->size;
  1903. tx_desc->buf_phys_addr =
  1904. dma_map_single(pp->dev->dev.parent, addr,
  1905. tx_desc->data_size, DMA_TO_DEVICE);
  1906. if (dma_mapping_error(pp->dev->dev.parent,
  1907. tx_desc->buf_phys_addr)) {
  1908. mvneta_txq_desc_put(txq);
  1909. goto error;
  1910. }
  1911. if (i == nr_frags - 1) {
  1912. /* Last descriptor */
  1913. tx_desc->command = MVNETA_TXD_L_DESC | MVNETA_TXD_Z_PAD;
  1914. txq->tx_skb[txq->txq_put_index] = skb;
  1915. } else {
  1916. /* Descriptor in the middle: Not First, Not Last */
  1917. tx_desc->command = 0;
  1918. txq->tx_skb[txq->txq_put_index] = NULL;
  1919. }
  1920. mvneta_txq_inc_put(txq);
  1921. }
  1922. return 0;
  1923. error:
  1924. /* Release all descriptors that were used to map fragments of
  1925. * this packet, as well as the corresponding DMA mappings
  1926. */
  1927. for (i = i - 1; i >= 0; i--) {
  1928. tx_desc = txq->descs + i;
  1929. dma_unmap_single(pp->dev->dev.parent,
  1930. tx_desc->buf_phys_addr,
  1931. tx_desc->data_size,
  1932. DMA_TO_DEVICE);
  1933. mvneta_txq_desc_put(txq);
  1934. }
  1935. return -ENOMEM;
  1936. }
  1937. /* Main tx processing */
  1938. static int mvneta_tx(struct sk_buff *skb, struct net_device *dev)
  1939. {
  1940. struct mvneta_port *pp = netdev_priv(dev);
  1941. u16 txq_id = skb_get_queue_mapping(skb);
  1942. struct mvneta_tx_queue *txq = &pp->txqs[txq_id];
  1943. struct mvneta_tx_desc *tx_desc;
  1944. int len = skb->len;
  1945. int frags = 0;
  1946. u32 tx_cmd;
  1947. if (!netif_running(dev))
  1948. goto out;
  1949. if (skb_is_gso(skb)) {
  1950. frags = mvneta_tx_tso(skb, dev, txq);
  1951. goto out;
  1952. }
  1953. frags = skb_shinfo(skb)->nr_frags + 1;
  1954. /* Get a descriptor for the first part of the packet */
  1955. tx_desc = mvneta_txq_next_desc_get(txq);
  1956. tx_cmd = mvneta_skb_tx_csum(pp, skb);
  1957. tx_desc->data_size = skb_headlen(skb);
  1958. tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, skb->data,
  1959. tx_desc->data_size,
  1960. DMA_TO_DEVICE);
  1961. if (unlikely(dma_mapping_error(dev->dev.parent,
  1962. tx_desc->buf_phys_addr))) {
  1963. mvneta_txq_desc_put(txq);
  1964. frags = 0;
  1965. goto out;
  1966. }
  1967. if (frags == 1) {
  1968. /* First and Last descriptor */
  1969. tx_cmd |= MVNETA_TXD_FLZ_DESC;
  1970. tx_desc->command = tx_cmd;
  1971. txq->tx_skb[txq->txq_put_index] = skb;
  1972. mvneta_txq_inc_put(txq);
  1973. } else {
  1974. /* First but not Last */
  1975. tx_cmd |= MVNETA_TXD_F_DESC;
  1976. txq->tx_skb[txq->txq_put_index] = NULL;
  1977. mvneta_txq_inc_put(txq);
  1978. tx_desc->command = tx_cmd;
  1979. /* Continue with other skb fragments */
  1980. if (mvneta_tx_frag_process(pp, skb, txq)) {
  1981. dma_unmap_single(dev->dev.parent,
  1982. tx_desc->buf_phys_addr,
  1983. tx_desc->data_size,
  1984. DMA_TO_DEVICE);
  1985. mvneta_txq_desc_put(txq);
  1986. frags = 0;
  1987. goto out;
  1988. }
  1989. }
  1990. out:
  1991. if (frags > 0) {
  1992. struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
  1993. struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
  1994. netdev_tx_sent_queue(nq, len);
  1995. txq->count += frags;
  1996. if (txq->count >= txq->tx_stop_threshold)
  1997. netif_tx_stop_queue(nq);
  1998. if (!skb->xmit_more || netif_xmit_stopped(nq) ||
  1999. txq->pending + frags > MVNETA_TXQ_DEC_SENT_MASK)
  2000. mvneta_txq_pend_desc_add(pp, txq, frags);
  2001. else
  2002. txq->pending += frags;
  2003. u64_stats_update_begin(&stats->syncp);
  2004. stats->tx_packets++;
  2005. stats->tx_bytes += len;
  2006. u64_stats_update_end(&stats->syncp);
  2007. } else {
  2008. dev->stats.tx_dropped++;
  2009. dev_kfree_skb_any(skb);
  2010. }
  2011. return NETDEV_TX_OK;
  2012. }
  2013. /* Free tx resources, when resetting a port */
  2014. static void mvneta_txq_done_force(struct mvneta_port *pp,
  2015. struct mvneta_tx_queue *txq)
  2016. {
  2017. struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
  2018. int tx_done = txq->count;
  2019. mvneta_txq_bufs_free(pp, txq, tx_done, nq);
  2020. /* reset txq */
  2021. txq->count = 0;
  2022. txq->txq_put_index = 0;
  2023. txq->txq_get_index = 0;
  2024. }
  2025. /* Handle tx done - called in softirq context. The <cause_tx_done> argument
  2026. * must be a valid cause according to MVNETA_TXQ_INTR_MASK_ALL.
  2027. */
  2028. static void mvneta_tx_done_gbe(struct mvneta_port *pp, u32 cause_tx_done)
  2029. {
  2030. struct mvneta_tx_queue *txq;
  2031. struct netdev_queue *nq;
  2032. while (cause_tx_done) {
  2033. txq = mvneta_tx_done_policy(pp, cause_tx_done);
  2034. nq = netdev_get_tx_queue(pp->dev, txq->id);
  2035. __netif_tx_lock(nq, smp_processor_id());
  2036. if (txq->count)
  2037. mvneta_txq_done(pp, txq);
  2038. __netif_tx_unlock(nq);
  2039. cause_tx_done &= ~((1 << txq->id));
  2040. }
  2041. }
  2042. /* Compute crc8 of the specified address, using a unique algorithm ,
  2043. * according to hw spec, different than generic crc8 algorithm
  2044. */
  2045. static int mvneta_addr_crc(unsigned char *addr)
  2046. {
  2047. int crc = 0;
  2048. int i;
  2049. for (i = 0; i < ETH_ALEN; i++) {
  2050. int j;
  2051. crc = (crc ^ addr[i]) << 8;
  2052. for (j = 7; j >= 0; j--) {
  2053. if (crc & (0x100 << j))
  2054. crc ^= 0x107 << j;
  2055. }
  2056. }
  2057. return crc;
  2058. }
  2059. /* This method controls the net device special MAC multicast support.
  2060. * The Special Multicast Table for MAC addresses supports MAC of the form
  2061. * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
  2062. * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
  2063. * Table entries in the DA-Filter table. This method set the Special
  2064. * Multicast Table appropriate entry.
  2065. */
  2066. static void mvneta_set_special_mcast_addr(struct mvneta_port *pp,
  2067. unsigned char last_byte,
  2068. int queue)
  2069. {
  2070. unsigned int smc_table_reg;
  2071. unsigned int tbl_offset;
  2072. unsigned int reg_offset;
  2073. /* Register offset from SMC table base */
  2074. tbl_offset = (last_byte / 4);
  2075. /* Entry offset within the above reg */
  2076. reg_offset = last_byte % 4;
  2077. smc_table_reg = mvreg_read(pp, (MVNETA_DA_FILT_SPEC_MCAST
  2078. + tbl_offset * 4));
  2079. if (queue == -1)
  2080. smc_table_reg &= ~(0xff << (8 * reg_offset));
  2081. else {
  2082. smc_table_reg &= ~(0xff << (8 * reg_offset));
  2083. smc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
  2084. }
  2085. mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + tbl_offset * 4,
  2086. smc_table_reg);
  2087. }
  2088. /* This method controls the network device Other MAC multicast support.
  2089. * The Other Multicast Table is used for multicast of another type.
  2090. * A CRC-8 is used as an index to the Other Multicast Table entries
  2091. * in the DA-Filter table.
  2092. * The method gets the CRC-8 value from the calling routine and
  2093. * sets the Other Multicast Table appropriate entry according to the
  2094. * specified CRC-8 .
  2095. */
  2096. static void mvneta_set_other_mcast_addr(struct mvneta_port *pp,
  2097. unsigned char crc8,
  2098. int queue)
  2099. {
  2100. unsigned int omc_table_reg;
  2101. unsigned int tbl_offset;
  2102. unsigned int reg_offset;
  2103. tbl_offset = (crc8 / 4) * 4; /* Register offset from OMC table base */
  2104. reg_offset = crc8 % 4; /* Entry offset within the above reg */
  2105. omc_table_reg = mvreg_read(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset);
  2106. if (queue == -1) {
  2107. /* Clear accepts frame bit at specified Other DA table entry */
  2108. omc_table_reg &= ~(0xff << (8 * reg_offset));
  2109. } else {
  2110. omc_table_reg &= ~(0xff << (8 * reg_offset));
  2111. omc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
  2112. }
  2113. mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset, omc_table_reg);
  2114. }
  2115. /* The network device supports multicast using two tables:
  2116. * 1) Special Multicast Table for MAC addresses of the form
  2117. * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
  2118. * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
  2119. * Table entries in the DA-Filter table.
  2120. * 2) Other Multicast Table for multicast of another type. A CRC-8 value
  2121. * is used as an index to the Other Multicast Table entries in the
  2122. * DA-Filter table.
  2123. */
  2124. static int mvneta_mcast_addr_set(struct mvneta_port *pp, unsigned char *p_addr,
  2125. int queue)
  2126. {
  2127. unsigned char crc_result = 0;
  2128. if (memcmp(p_addr, "\x01\x00\x5e\x00\x00", 5) == 0) {
  2129. mvneta_set_special_mcast_addr(pp, p_addr[5], queue);
  2130. return 0;
  2131. }
  2132. crc_result = mvneta_addr_crc(p_addr);
  2133. if (queue == -1) {
  2134. if (pp->mcast_count[crc_result] == 0) {
  2135. netdev_info(pp->dev, "No valid Mcast for crc8=0x%02x\n",
  2136. crc_result);
  2137. return -EINVAL;
  2138. }
  2139. pp->mcast_count[crc_result]--;
  2140. if (pp->mcast_count[crc_result] != 0) {
  2141. netdev_info(pp->dev,
  2142. "After delete there are %d valid Mcast for crc8=0x%02x\n",
  2143. pp->mcast_count[crc_result], crc_result);
  2144. return -EINVAL;
  2145. }
  2146. } else
  2147. pp->mcast_count[crc_result]++;
  2148. mvneta_set_other_mcast_addr(pp, crc_result, queue);
  2149. return 0;
  2150. }
  2151. /* Configure Fitering mode of Ethernet port */
  2152. static void mvneta_rx_unicast_promisc_set(struct mvneta_port *pp,
  2153. int is_promisc)
  2154. {
  2155. u32 port_cfg_reg, val;
  2156. port_cfg_reg = mvreg_read(pp, MVNETA_PORT_CONFIG);
  2157. val = mvreg_read(pp, MVNETA_TYPE_PRIO);
  2158. /* Set / Clear UPM bit in port configuration register */
  2159. if (is_promisc) {
  2160. /* Accept all Unicast addresses */
  2161. port_cfg_reg |= MVNETA_UNI_PROMISC_MODE;
  2162. val |= MVNETA_FORCE_UNI;
  2163. mvreg_write(pp, MVNETA_MAC_ADDR_LOW, 0xffff);
  2164. mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, 0xffffffff);
  2165. } else {
  2166. /* Reject all Unicast addresses */
  2167. port_cfg_reg &= ~MVNETA_UNI_PROMISC_MODE;
  2168. val &= ~MVNETA_FORCE_UNI;
  2169. }
  2170. mvreg_write(pp, MVNETA_PORT_CONFIG, port_cfg_reg);
  2171. mvreg_write(pp, MVNETA_TYPE_PRIO, val);
  2172. }
  2173. /* register unicast and multicast addresses */
  2174. static void mvneta_set_rx_mode(struct net_device *dev)
  2175. {
  2176. struct mvneta_port *pp = netdev_priv(dev);
  2177. struct netdev_hw_addr *ha;
  2178. if (dev->flags & IFF_PROMISC) {
  2179. /* Accept all: Multicast + Unicast */
  2180. mvneta_rx_unicast_promisc_set(pp, 1);
  2181. mvneta_set_ucast_table(pp, pp->rxq_def);
  2182. mvneta_set_special_mcast_table(pp, pp->rxq_def);
  2183. mvneta_set_other_mcast_table(pp, pp->rxq_def);
  2184. } else {
  2185. /* Accept single Unicast */
  2186. mvneta_rx_unicast_promisc_set(pp, 0);
  2187. mvneta_set_ucast_table(pp, -1);
  2188. mvneta_mac_addr_set(pp, dev->dev_addr, pp->rxq_def);
  2189. if (dev->flags & IFF_ALLMULTI) {
  2190. /* Accept all multicast */
  2191. mvneta_set_special_mcast_table(pp, pp->rxq_def);
  2192. mvneta_set_other_mcast_table(pp, pp->rxq_def);
  2193. } else {
  2194. /* Accept only initialized multicast */
  2195. mvneta_set_special_mcast_table(pp, -1);
  2196. mvneta_set_other_mcast_table(pp, -1);
  2197. if (!netdev_mc_empty(dev)) {
  2198. netdev_for_each_mc_addr(ha, dev) {
  2199. mvneta_mcast_addr_set(pp, ha->addr,
  2200. pp->rxq_def);
  2201. }
  2202. }
  2203. }
  2204. }
  2205. }
  2206. /* Interrupt handling - the callback for request_irq() */
  2207. static irqreturn_t mvneta_isr(int irq, void *dev_id)
  2208. {
  2209. struct mvneta_port *pp = (struct mvneta_port *)dev_id;
  2210. mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
  2211. napi_schedule(&pp->napi);
  2212. return IRQ_HANDLED;
  2213. }
  2214. /* Interrupt handling - the callback for request_percpu_irq() */
  2215. static irqreturn_t mvneta_percpu_isr(int irq, void *dev_id)
  2216. {
  2217. struct mvneta_pcpu_port *port = (struct mvneta_pcpu_port *)dev_id;
  2218. disable_percpu_irq(port->pp->dev->irq);
  2219. napi_schedule(&port->napi);
  2220. return IRQ_HANDLED;
  2221. }
  2222. static int mvneta_fixed_link_update(struct mvneta_port *pp,
  2223. struct phy_device *phy)
  2224. {
  2225. struct fixed_phy_status status;
  2226. struct fixed_phy_status changed = {};
  2227. u32 gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS);
  2228. status.link = !!(gmac_stat & MVNETA_GMAC_LINK_UP);
  2229. if (gmac_stat & MVNETA_GMAC_SPEED_1000)
  2230. status.speed = SPEED_1000;
  2231. else if (gmac_stat & MVNETA_GMAC_SPEED_100)
  2232. status.speed = SPEED_100;
  2233. else
  2234. status.speed = SPEED_10;
  2235. status.duplex = !!(gmac_stat & MVNETA_GMAC_FULL_DUPLEX);
  2236. changed.link = 1;
  2237. changed.speed = 1;
  2238. changed.duplex = 1;
  2239. fixed_phy_update_state(phy, &status, &changed);
  2240. return 0;
  2241. }
  2242. /* NAPI handler
  2243. * Bits 0 - 7 of the causeRxTx register indicate that are transmitted
  2244. * packets on the corresponding TXQ (Bit 0 is for TX queue 1).
  2245. * Bits 8 -15 of the cause Rx Tx register indicate that are received
  2246. * packets on the corresponding RXQ (Bit 8 is for RX queue 0).
  2247. * Each CPU has its own causeRxTx register
  2248. */
  2249. static int mvneta_poll(struct napi_struct *napi, int budget)
  2250. {
  2251. int rx_done = 0;
  2252. u32 cause_rx_tx;
  2253. int rx_queue;
  2254. struct mvneta_port *pp = netdev_priv(napi->dev);
  2255. struct net_device *ndev = pp->dev;
  2256. struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
  2257. if (!netif_running(pp->dev)) {
  2258. napi_complete(napi);
  2259. return rx_done;
  2260. }
  2261. /* Read cause register */
  2262. cause_rx_tx = mvreg_read(pp, MVNETA_INTR_NEW_CAUSE);
  2263. if (cause_rx_tx & MVNETA_MISCINTR_INTR_MASK) {
  2264. u32 cause_misc = mvreg_read(pp, MVNETA_INTR_MISC_CAUSE);
  2265. mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
  2266. if (pp->use_inband_status && (cause_misc &
  2267. (MVNETA_CAUSE_PHY_STATUS_CHANGE |
  2268. MVNETA_CAUSE_LINK_CHANGE |
  2269. MVNETA_CAUSE_PSC_SYNC_CHANGE))) {
  2270. mvneta_fixed_link_update(pp, ndev->phydev);
  2271. }
  2272. }
  2273. /* Release Tx descriptors */
  2274. if (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL) {
  2275. mvneta_tx_done_gbe(pp, (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL));
  2276. cause_rx_tx &= ~MVNETA_TX_INTR_MASK_ALL;
  2277. }
  2278. /* For the case where the last mvneta_poll did not process all
  2279. * RX packets
  2280. */
  2281. rx_queue = fls(((cause_rx_tx >> 8) & 0xff));
  2282. cause_rx_tx |= pp->neta_armada3700 ? pp->cause_rx_tx :
  2283. port->cause_rx_tx;
  2284. if (rx_queue) {
  2285. rx_queue = rx_queue - 1;
  2286. if (pp->bm_priv)
  2287. rx_done = mvneta_rx_hwbm(pp, budget, &pp->rxqs[rx_queue]);
  2288. else
  2289. rx_done = mvneta_rx_swbm(pp, budget, &pp->rxqs[rx_queue]);
  2290. }
  2291. if (rx_done < budget) {
  2292. cause_rx_tx = 0;
  2293. napi_complete_done(napi, rx_done);
  2294. if (pp->neta_armada3700) {
  2295. unsigned long flags;
  2296. local_irq_save(flags);
  2297. mvreg_write(pp, MVNETA_INTR_NEW_MASK,
  2298. MVNETA_RX_INTR_MASK(rxq_number) |
  2299. MVNETA_TX_INTR_MASK(txq_number) |
  2300. MVNETA_MISCINTR_INTR_MASK);
  2301. local_irq_restore(flags);
  2302. } else {
  2303. enable_percpu_irq(pp->dev->irq, 0);
  2304. }
  2305. }
  2306. if (pp->neta_armada3700)
  2307. pp->cause_rx_tx = cause_rx_tx;
  2308. else
  2309. port->cause_rx_tx = cause_rx_tx;
  2310. return rx_done;
  2311. }
  2312. /* Handle rxq fill: allocates rxq skbs; called when initializing a port */
  2313. static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
  2314. int num)
  2315. {
  2316. int i;
  2317. for (i = 0; i < num; i++) {
  2318. memset(rxq->descs + i, 0, sizeof(struct mvneta_rx_desc));
  2319. if (mvneta_rx_refill(pp, rxq->descs + i, rxq) != 0) {
  2320. netdev_err(pp->dev, "%s:rxq %d, %d of %d buffs filled\n",
  2321. __func__, rxq->id, i, num);
  2322. break;
  2323. }
  2324. }
  2325. /* Add this number of RX descriptors as non occupied (ready to
  2326. * get packets)
  2327. */
  2328. mvneta_rxq_non_occup_desc_add(pp, rxq, i);
  2329. return i;
  2330. }
  2331. /* Free all packets pending transmit from all TXQs and reset TX port */
  2332. static void mvneta_tx_reset(struct mvneta_port *pp)
  2333. {
  2334. int queue;
  2335. /* free the skb's in the tx ring */
  2336. for (queue = 0; queue < txq_number; queue++)
  2337. mvneta_txq_done_force(pp, &pp->txqs[queue]);
  2338. mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
  2339. mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
  2340. }
  2341. static void mvneta_rx_reset(struct mvneta_port *pp)
  2342. {
  2343. mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
  2344. mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
  2345. }
  2346. /* Rx/Tx queue initialization/cleanup methods */
  2347. /* Create a specified RX queue */
  2348. static int mvneta_rxq_init(struct mvneta_port *pp,
  2349. struct mvneta_rx_queue *rxq)
  2350. {
  2351. rxq->size = pp->rx_ring_size;
  2352. /* Allocate memory for RX descriptors */
  2353. rxq->descs = dma_alloc_coherent(pp->dev->dev.parent,
  2354. rxq->size * MVNETA_DESC_ALIGNED_SIZE,
  2355. &rxq->descs_phys, GFP_KERNEL);
  2356. if (!rxq->descs)
  2357. return -ENOMEM;
  2358. rxq->last_desc = rxq->size - 1;
  2359. /* Set Rx descriptors queue starting address */
  2360. mvreg_write(pp, MVNETA_RXQ_BASE_ADDR_REG(rxq->id), rxq->descs_phys);
  2361. mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), rxq->size);
  2362. /* Set Offset */
  2363. mvneta_rxq_offset_set(pp, rxq, NET_SKB_PAD - pp->rx_offset_correction);
  2364. /* Set coalescing pkts and time */
  2365. mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
  2366. mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
  2367. if (!pp->bm_priv) {
  2368. /* Fill RXQ with buffers from RX pool */
  2369. mvneta_rxq_buf_size_set(pp, rxq,
  2370. MVNETA_RX_BUF_SIZE(pp->pkt_size));
  2371. mvneta_rxq_bm_disable(pp, rxq);
  2372. mvneta_rxq_fill(pp, rxq, rxq->size);
  2373. } else {
  2374. mvneta_rxq_bm_enable(pp, rxq);
  2375. mvneta_rxq_long_pool_set(pp, rxq);
  2376. mvneta_rxq_short_pool_set(pp, rxq);
  2377. mvneta_rxq_non_occup_desc_add(pp, rxq, rxq->size);
  2378. }
  2379. return 0;
  2380. }
  2381. /* Cleanup Rx queue */
  2382. static void mvneta_rxq_deinit(struct mvneta_port *pp,
  2383. struct mvneta_rx_queue *rxq)
  2384. {
  2385. mvneta_rxq_drop_pkts(pp, rxq);
  2386. if (rxq->descs)
  2387. dma_free_coherent(pp->dev->dev.parent,
  2388. rxq->size * MVNETA_DESC_ALIGNED_SIZE,
  2389. rxq->descs,
  2390. rxq->descs_phys);
  2391. rxq->descs = NULL;
  2392. rxq->last_desc = 0;
  2393. rxq->next_desc_to_proc = 0;
  2394. rxq->descs_phys = 0;
  2395. }
  2396. /* Create and initialize a tx queue */
  2397. static int mvneta_txq_init(struct mvneta_port *pp,
  2398. struct mvneta_tx_queue *txq)
  2399. {
  2400. int cpu;
  2401. txq->size = pp->tx_ring_size;
  2402. /* A queue must always have room for at least one skb.
  2403. * Therefore, stop the queue when the free entries reaches
  2404. * the maximum number of descriptors per skb.
  2405. */
  2406. txq->tx_stop_threshold = txq->size - MVNETA_MAX_SKB_DESCS;
  2407. txq->tx_wake_threshold = txq->tx_stop_threshold / 2;
  2408. /* Allocate memory for TX descriptors */
  2409. txq->descs = dma_alloc_coherent(pp->dev->dev.parent,
  2410. txq->size * MVNETA_DESC_ALIGNED_SIZE,
  2411. &txq->descs_phys, GFP_KERNEL);
  2412. if (!txq->descs)
  2413. return -ENOMEM;
  2414. txq->last_desc = txq->size - 1;
  2415. /* Set maximum bandwidth for enabled TXQs */
  2416. mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0x03ffffff);
  2417. mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0x3fffffff);
  2418. /* Set Tx descriptors queue starting address */
  2419. mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), txq->descs_phys);
  2420. mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), txq->size);
  2421. txq->tx_skb = kmalloc_array(txq->size, sizeof(*txq->tx_skb),
  2422. GFP_KERNEL);
  2423. if (!txq->tx_skb) {
  2424. dma_free_coherent(pp->dev->dev.parent,
  2425. txq->size * MVNETA_DESC_ALIGNED_SIZE,
  2426. txq->descs, txq->descs_phys);
  2427. return -ENOMEM;
  2428. }
  2429. /* Allocate DMA buffers for TSO MAC/IP/TCP headers */
  2430. txq->tso_hdrs = dma_alloc_coherent(pp->dev->dev.parent,
  2431. txq->size * TSO_HEADER_SIZE,
  2432. &txq->tso_hdrs_phys, GFP_KERNEL);
  2433. if (!txq->tso_hdrs) {
  2434. kfree(txq->tx_skb);
  2435. dma_free_coherent(pp->dev->dev.parent,
  2436. txq->size * MVNETA_DESC_ALIGNED_SIZE,
  2437. txq->descs, txq->descs_phys);
  2438. return -ENOMEM;
  2439. }
  2440. mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
  2441. /* Setup XPS mapping */
  2442. if (txq_number > 1)
  2443. cpu = txq->id % num_present_cpus();
  2444. else
  2445. cpu = pp->rxq_def % num_present_cpus();
  2446. cpumask_set_cpu(cpu, &txq->affinity_mask);
  2447. netif_set_xps_queue(pp->dev, &txq->affinity_mask, txq->id);
  2448. return 0;
  2449. }
  2450. /* Free allocated resources when mvneta_txq_init() fails to allocate memory*/
  2451. static void mvneta_txq_deinit(struct mvneta_port *pp,
  2452. struct mvneta_tx_queue *txq)
  2453. {
  2454. struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
  2455. kfree(txq->tx_skb);
  2456. if (txq->tso_hdrs)
  2457. dma_free_coherent(pp->dev->dev.parent,
  2458. txq->size * TSO_HEADER_SIZE,
  2459. txq->tso_hdrs, txq->tso_hdrs_phys);
  2460. if (txq->descs)
  2461. dma_free_coherent(pp->dev->dev.parent,
  2462. txq->size * MVNETA_DESC_ALIGNED_SIZE,
  2463. txq->descs, txq->descs_phys);
  2464. netdev_tx_reset_queue(nq);
  2465. txq->descs = NULL;
  2466. txq->last_desc = 0;
  2467. txq->next_desc_to_proc = 0;
  2468. txq->descs_phys = 0;
  2469. /* Set minimum bandwidth for disabled TXQs */
  2470. mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0);
  2471. mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0);
  2472. /* Set Tx descriptors queue starting address and size */
  2473. mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), 0);
  2474. mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), 0);
  2475. }
  2476. /* Cleanup all Tx queues */
  2477. static void mvneta_cleanup_txqs(struct mvneta_port *pp)
  2478. {
  2479. int queue;
  2480. for (queue = 0; queue < txq_number; queue++)
  2481. mvneta_txq_deinit(pp, &pp->txqs[queue]);
  2482. }
  2483. /* Cleanup all Rx queues */
  2484. static void mvneta_cleanup_rxqs(struct mvneta_port *pp)
  2485. {
  2486. int queue;
  2487. for (queue = 0; queue < txq_number; queue++)
  2488. mvneta_rxq_deinit(pp, &pp->rxqs[queue]);
  2489. }
  2490. /* Init all Rx queues */
  2491. static int mvneta_setup_rxqs(struct mvneta_port *pp)
  2492. {
  2493. int queue;
  2494. for (queue = 0; queue < rxq_number; queue++) {
  2495. int err = mvneta_rxq_init(pp, &pp->rxqs[queue]);
  2496. if (err) {
  2497. netdev_err(pp->dev, "%s: can't create rxq=%d\n",
  2498. __func__, queue);
  2499. mvneta_cleanup_rxqs(pp);
  2500. return err;
  2501. }
  2502. }
  2503. return 0;
  2504. }
  2505. /* Init all tx queues */
  2506. static int mvneta_setup_txqs(struct mvneta_port *pp)
  2507. {
  2508. int queue;
  2509. for (queue = 0; queue < txq_number; queue++) {
  2510. int err = mvneta_txq_init(pp, &pp->txqs[queue]);
  2511. if (err) {
  2512. netdev_err(pp->dev, "%s: can't create txq=%d\n",
  2513. __func__, queue);
  2514. mvneta_cleanup_txqs(pp);
  2515. return err;
  2516. }
  2517. }
  2518. return 0;
  2519. }
  2520. static void mvneta_start_dev(struct mvneta_port *pp)
  2521. {
  2522. int cpu;
  2523. struct net_device *ndev = pp->dev;
  2524. mvneta_max_rx_size_set(pp, pp->pkt_size);
  2525. mvneta_txq_max_tx_size_set(pp, pp->pkt_size);
  2526. /* start the Rx/Tx activity */
  2527. mvneta_port_enable(pp);
  2528. if (!pp->neta_armada3700) {
  2529. /* Enable polling on the port */
  2530. for_each_online_cpu(cpu) {
  2531. struct mvneta_pcpu_port *port =
  2532. per_cpu_ptr(pp->ports, cpu);
  2533. napi_enable(&port->napi);
  2534. }
  2535. } else {
  2536. napi_enable(&pp->napi);
  2537. }
  2538. /* Unmask interrupts. It has to be done from each CPU */
  2539. on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
  2540. mvreg_write(pp, MVNETA_INTR_MISC_MASK,
  2541. MVNETA_CAUSE_PHY_STATUS_CHANGE |
  2542. MVNETA_CAUSE_LINK_CHANGE |
  2543. MVNETA_CAUSE_PSC_SYNC_CHANGE);
  2544. phy_start(ndev->phydev);
  2545. netif_tx_start_all_queues(pp->dev);
  2546. }
  2547. static void mvneta_stop_dev(struct mvneta_port *pp)
  2548. {
  2549. unsigned int cpu;
  2550. struct net_device *ndev = pp->dev;
  2551. phy_stop(ndev->phydev);
  2552. if (!pp->neta_armada3700) {
  2553. for_each_online_cpu(cpu) {
  2554. struct mvneta_pcpu_port *port =
  2555. per_cpu_ptr(pp->ports, cpu);
  2556. napi_disable(&port->napi);
  2557. }
  2558. } else {
  2559. napi_disable(&pp->napi);
  2560. }
  2561. netif_carrier_off(pp->dev);
  2562. mvneta_port_down(pp);
  2563. netif_tx_stop_all_queues(pp->dev);
  2564. /* Stop the port activity */
  2565. mvneta_port_disable(pp);
  2566. /* Clear all ethernet port interrupts */
  2567. on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
  2568. /* Mask all ethernet port interrupts */
  2569. on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
  2570. mvneta_tx_reset(pp);
  2571. mvneta_rx_reset(pp);
  2572. }
  2573. static void mvneta_percpu_enable(void *arg)
  2574. {
  2575. struct mvneta_port *pp = arg;
  2576. enable_percpu_irq(pp->dev->irq, IRQ_TYPE_NONE);
  2577. }
  2578. static void mvneta_percpu_disable(void *arg)
  2579. {
  2580. struct mvneta_port *pp = arg;
  2581. disable_percpu_irq(pp->dev->irq);
  2582. }
  2583. /* Change the device mtu */
  2584. static int mvneta_change_mtu(struct net_device *dev, int mtu)
  2585. {
  2586. struct mvneta_port *pp = netdev_priv(dev);
  2587. int ret;
  2588. if (!IS_ALIGNED(MVNETA_RX_PKT_SIZE(mtu), 8)) {
  2589. netdev_info(dev, "Illegal MTU value %d, rounding to %d\n",
  2590. mtu, ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8));
  2591. mtu = ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8);
  2592. }
  2593. dev->mtu = mtu;
  2594. if (!netif_running(dev)) {
  2595. if (pp->bm_priv)
  2596. mvneta_bm_update_mtu(pp, mtu);
  2597. netdev_update_features(dev);
  2598. return 0;
  2599. }
  2600. /* The interface is running, so we have to force a
  2601. * reallocation of the queues
  2602. */
  2603. mvneta_stop_dev(pp);
  2604. on_each_cpu(mvneta_percpu_disable, pp, true);
  2605. mvneta_cleanup_txqs(pp);
  2606. mvneta_cleanup_rxqs(pp);
  2607. if (pp->bm_priv)
  2608. mvneta_bm_update_mtu(pp, mtu);
  2609. pp->pkt_size = MVNETA_RX_PKT_SIZE(dev->mtu);
  2610. pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) +
  2611. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  2612. ret = mvneta_setup_rxqs(pp);
  2613. if (ret) {
  2614. netdev_err(dev, "unable to setup rxqs after MTU change\n");
  2615. return ret;
  2616. }
  2617. ret = mvneta_setup_txqs(pp);
  2618. if (ret) {
  2619. netdev_err(dev, "unable to setup txqs after MTU change\n");
  2620. return ret;
  2621. }
  2622. on_each_cpu(mvneta_percpu_enable, pp, true);
  2623. mvneta_start_dev(pp);
  2624. mvneta_port_up(pp);
  2625. netdev_update_features(dev);
  2626. return 0;
  2627. }
  2628. static netdev_features_t mvneta_fix_features(struct net_device *dev,
  2629. netdev_features_t features)
  2630. {
  2631. struct mvneta_port *pp = netdev_priv(dev);
  2632. if (pp->tx_csum_limit && dev->mtu > pp->tx_csum_limit) {
  2633. features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
  2634. netdev_info(dev,
  2635. "Disable IP checksum for MTU greater than %dB\n",
  2636. pp->tx_csum_limit);
  2637. }
  2638. return features;
  2639. }
  2640. /* Get mac address */
  2641. static void mvneta_get_mac_addr(struct mvneta_port *pp, unsigned char *addr)
  2642. {
  2643. u32 mac_addr_l, mac_addr_h;
  2644. mac_addr_l = mvreg_read(pp, MVNETA_MAC_ADDR_LOW);
  2645. mac_addr_h = mvreg_read(pp, MVNETA_MAC_ADDR_HIGH);
  2646. addr[0] = (mac_addr_h >> 24) & 0xFF;
  2647. addr[1] = (mac_addr_h >> 16) & 0xFF;
  2648. addr[2] = (mac_addr_h >> 8) & 0xFF;
  2649. addr[3] = mac_addr_h & 0xFF;
  2650. addr[4] = (mac_addr_l >> 8) & 0xFF;
  2651. addr[5] = mac_addr_l & 0xFF;
  2652. }
  2653. /* Handle setting mac address */
  2654. static int mvneta_set_mac_addr(struct net_device *dev, void *addr)
  2655. {
  2656. struct mvneta_port *pp = netdev_priv(dev);
  2657. struct sockaddr *sockaddr = addr;
  2658. int ret;
  2659. ret = eth_prepare_mac_addr_change(dev, addr);
  2660. if (ret < 0)
  2661. return ret;
  2662. /* Remove previous address table entry */
  2663. mvneta_mac_addr_set(pp, dev->dev_addr, -1);
  2664. /* Set new addr in hw */
  2665. mvneta_mac_addr_set(pp, sockaddr->sa_data, pp->rxq_def);
  2666. eth_commit_mac_addr_change(dev, addr);
  2667. return 0;
  2668. }
  2669. static void mvneta_adjust_link(struct net_device *ndev)
  2670. {
  2671. struct mvneta_port *pp = netdev_priv(ndev);
  2672. struct phy_device *phydev = ndev->phydev;
  2673. int status_change = 0;
  2674. if (phydev->link) {
  2675. if ((pp->speed != phydev->speed) ||
  2676. (pp->duplex != phydev->duplex)) {
  2677. u32 val;
  2678. val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
  2679. val &= ~(MVNETA_GMAC_CONFIG_MII_SPEED |
  2680. MVNETA_GMAC_CONFIG_GMII_SPEED |
  2681. MVNETA_GMAC_CONFIG_FULL_DUPLEX);
  2682. if (phydev->duplex)
  2683. val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
  2684. if (phydev->speed == SPEED_1000)
  2685. val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
  2686. else if (phydev->speed == SPEED_100)
  2687. val |= MVNETA_GMAC_CONFIG_MII_SPEED;
  2688. mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
  2689. pp->duplex = phydev->duplex;
  2690. pp->speed = phydev->speed;
  2691. }
  2692. }
  2693. if (phydev->link != pp->link) {
  2694. if (!phydev->link) {
  2695. pp->duplex = -1;
  2696. pp->speed = 0;
  2697. }
  2698. pp->link = phydev->link;
  2699. status_change = 1;
  2700. }
  2701. if (status_change) {
  2702. if (phydev->link) {
  2703. if (!pp->use_inband_status) {
  2704. u32 val = mvreg_read(pp,
  2705. MVNETA_GMAC_AUTONEG_CONFIG);
  2706. val &= ~MVNETA_GMAC_FORCE_LINK_DOWN;
  2707. val |= MVNETA_GMAC_FORCE_LINK_PASS;
  2708. mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
  2709. val);
  2710. }
  2711. mvneta_port_up(pp);
  2712. } else {
  2713. if (!pp->use_inband_status) {
  2714. u32 val = mvreg_read(pp,
  2715. MVNETA_GMAC_AUTONEG_CONFIG);
  2716. val &= ~MVNETA_GMAC_FORCE_LINK_PASS;
  2717. val |= MVNETA_GMAC_FORCE_LINK_DOWN;
  2718. mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
  2719. val);
  2720. }
  2721. mvneta_port_down(pp);
  2722. }
  2723. phy_print_status(phydev);
  2724. }
  2725. }
  2726. static int mvneta_mdio_probe(struct mvneta_port *pp)
  2727. {
  2728. struct phy_device *phy_dev;
  2729. struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
  2730. phy_dev = of_phy_connect(pp->dev, pp->phy_node, mvneta_adjust_link, 0,
  2731. pp->phy_interface);
  2732. if (!phy_dev) {
  2733. netdev_err(pp->dev, "could not find the PHY\n");
  2734. return -ENODEV;
  2735. }
  2736. phy_ethtool_get_wol(phy_dev, &wol);
  2737. device_set_wakeup_capable(&pp->dev->dev, !!wol.supported);
  2738. phy_dev->supported &= PHY_GBIT_FEATURES;
  2739. phy_dev->advertising = phy_dev->supported;
  2740. pp->link = 0;
  2741. pp->duplex = 0;
  2742. pp->speed = 0;
  2743. return 0;
  2744. }
  2745. static void mvneta_mdio_remove(struct mvneta_port *pp)
  2746. {
  2747. struct net_device *ndev = pp->dev;
  2748. phy_disconnect(ndev->phydev);
  2749. }
  2750. /* Electing a CPU must be done in an atomic way: it should be done
  2751. * after or before the removal/insertion of a CPU and this function is
  2752. * not reentrant.
  2753. */
  2754. static void mvneta_percpu_elect(struct mvneta_port *pp)
  2755. {
  2756. int elected_cpu = 0, max_cpu, cpu, i = 0;
  2757. /* Use the cpu associated to the rxq when it is online, in all
  2758. * the other cases, use the cpu 0 which can't be offline.
  2759. */
  2760. if (cpu_online(pp->rxq_def))
  2761. elected_cpu = pp->rxq_def;
  2762. max_cpu = num_present_cpus();
  2763. for_each_online_cpu(cpu) {
  2764. int rxq_map = 0, txq_map = 0;
  2765. int rxq;
  2766. for (rxq = 0; rxq < rxq_number; rxq++)
  2767. if ((rxq % max_cpu) == cpu)
  2768. rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
  2769. if (cpu == elected_cpu)
  2770. /* Map the default receive queue queue to the
  2771. * elected CPU
  2772. */
  2773. rxq_map |= MVNETA_CPU_RXQ_ACCESS(pp->rxq_def);
  2774. /* We update the TX queue map only if we have one
  2775. * queue. In this case we associate the TX queue to
  2776. * the CPU bound to the default RX queue
  2777. */
  2778. if (txq_number == 1)
  2779. txq_map = (cpu == elected_cpu) ?
  2780. MVNETA_CPU_TXQ_ACCESS(1) : 0;
  2781. else
  2782. txq_map = mvreg_read(pp, MVNETA_CPU_MAP(cpu)) &
  2783. MVNETA_CPU_TXQ_ACCESS_ALL_MASK;
  2784. mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
  2785. /* Update the interrupt mask on each CPU according the
  2786. * new mapping
  2787. */
  2788. smp_call_function_single(cpu, mvneta_percpu_unmask_interrupt,
  2789. pp, true);
  2790. i++;
  2791. }
  2792. };
  2793. static int mvneta_cpu_online(unsigned int cpu, struct hlist_node *node)
  2794. {
  2795. int other_cpu;
  2796. struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
  2797. node_online);
  2798. struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
  2799. spin_lock(&pp->lock);
  2800. /*
  2801. * Configuring the driver for a new CPU while the driver is
  2802. * stopping is racy, so just avoid it.
  2803. */
  2804. if (pp->is_stopped) {
  2805. spin_unlock(&pp->lock);
  2806. return 0;
  2807. }
  2808. netif_tx_stop_all_queues(pp->dev);
  2809. /*
  2810. * We have to synchronise on tha napi of each CPU except the one
  2811. * just being woken up
  2812. */
  2813. for_each_online_cpu(other_cpu) {
  2814. if (other_cpu != cpu) {
  2815. struct mvneta_pcpu_port *other_port =
  2816. per_cpu_ptr(pp->ports, other_cpu);
  2817. napi_synchronize(&other_port->napi);
  2818. }
  2819. }
  2820. /* Mask all ethernet port interrupts */
  2821. on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
  2822. napi_enable(&port->napi);
  2823. /*
  2824. * Enable per-CPU interrupts on the CPU that is
  2825. * brought up.
  2826. */
  2827. mvneta_percpu_enable(pp);
  2828. /*
  2829. * Enable per-CPU interrupt on the one CPU we care
  2830. * about.
  2831. */
  2832. mvneta_percpu_elect(pp);
  2833. /* Unmask all ethernet port interrupts */
  2834. on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
  2835. mvreg_write(pp, MVNETA_INTR_MISC_MASK,
  2836. MVNETA_CAUSE_PHY_STATUS_CHANGE |
  2837. MVNETA_CAUSE_LINK_CHANGE |
  2838. MVNETA_CAUSE_PSC_SYNC_CHANGE);
  2839. netif_tx_start_all_queues(pp->dev);
  2840. spin_unlock(&pp->lock);
  2841. return 0;
  2842. }
  2843. static int mvneta_cpu_down_prepare(unsigned int cpu, struct hlist_node *node)
  2844. {
  2845. struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
  2846. node_online);
  2847. struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
  2848. /*
  2849. * Thanks to this lock we are sure that any pending cpu election is
  2850. * done.
  2851. */
  2852. spin_lock(&pp->lock);
  2853. /* Mask all ethernet port interrupts */
  2854. on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
  2855. spin_unlock(&pp->lock);
  2856. napi_synchronize(&port->napi);
  2857. napi_disable(&port->napi);
  2858. /* Disable per-CPU interrupts on the CPU that is brought down. */
  2859. mvneta_percpu_disable(pp);
  2860. return 0;
  2861. }
  2862. static int mvneta_cpu_dead(unsigned int cpu, struct hlist_node *node)
  2863. {
  2864. struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
  2865. node_dead);
  2866. /* Check if a new CPU must be elected now this on is down */
  2867. spin_lock(&pp->lock);
  2868. mvneta_percpu_elect(pp);
  2869. spin_unlock(&pp->lock);
  2870. /* Unmask all ethernet port interrupts */
  2871. on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
  2872. mvreg_write(pp, MVNETA_INTR_MISC_MASK,
  2873. MVNETA_CAUSE_PHY_STATUS_CHANGE |
  2874. MVNETA_CAUSE_LINK_CHANGE |
  2875. MVNETA_CAUSE_PSC_SYNC_CHANGE);
  2876. netif_tx_start_all_queues(pp->dev);
  2877. return 0;
  2878. }
  2879. static int mvneta_open(struct net_device *dev)
  2880. {
  2881. struct mvneta_port *pp = netdev_priv(dev);
  2882. int ret;
  2883. pp->pkt_size = MVNETA_RX_PKT_SIZE(pp->dev->mtu);
  2884. pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) +
  2885. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  2886. ret = mvneta_setup_rxqs(pp);
  2887. if (ret)
  2888. return ret;
  2889. ret = mvneta_setup_txqs(pp);
  2890. if (ret)
  2891. goto err_cleanup_rxqs;
  2892. /* Connect to port interrupt line */
  2893. if (pp->neta_armada3700)
  2894. ret = request_irq(pp->dev->irq, mvneta_isr, 0,
  2895. dev->name, pp);
  2896. else
  2897. ret = request_percpu_irq(pp->dev->irq, mvneta_percpu_isr,
  2898. dev->name, pp->ports);
  2899. if (ret) {
  2900. netdev_err(pp->dev, "cannot request irq %d\n", pp->dev->irq);
  2901. goto err_cleanup_txqs;
  2902. }
  2903. if (!pp->neta_armada3700) {
  2904. /* Enable per-CPU interrupt on all the CPU to handle our RX
  2905. * queue interrupts
  2906. */
  2907. on_each_cpu(mvneta_percpu_enable, pp, true);
  2908. pp->is_stopped = false;
  2909. /* Register a CPU notifier to handle the case where our CPU
  2910. * might be taken offline.
  2911. */
  2912. ret = cpuhp_state_add_instance_nocalls(online_hpstate,
  2913. &pp->node_online);
  2914. if (ret)
  2915. goto err_free_irq;
  2916. ret = cpuhp_state_add_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
  2917. &pp->node_dead);
  2918. if (ret)
  2919. goto err_free_online_hp;
  2920. }
  2921. /* In default link is down */
  2922. netif_carrier_off(pp->dev);
  2923. ret = mvneta_mdio_probe(pp);
  2924. if (ret < 0) {
  2925. netdev_err(dev, "cannot probe MDIO bus\n");
  2926. goto err_free_dead_hp;
  2927. }
  2928. mvneta_start_dev(pp);
  2929. return 0;
  2930. err_free_dead_hp:
  2931. if (!pp->neta_armada3700)
  2932. cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
  2933. &pp->node_dead);
  2934. err_free_online_hp:
  2935. if (!pp->neta_armada3700)
  2936. cpuhp_state_remove_instance_nocalls(online_hpstate,
  2937. &pp->node_online);
  2938. err_free_irq:
  2939. if (pp->neta_armada3700) {
  2940. free_irq(pp->dev->irq, pp);
  2941. } else {
  2942. on_each_cpu(mvneta_percpu_disable, pp, true);
  2943. free_percpu_irq(pp->dev->irq, pp->ports);
  2944. }
  2945. err_cleanup_txqs:
  2946. mvneta_cleanup_txqs(pp);
  2947. err_cleanup_rxqs:
  2948. mvneta_cleanup_rxqs(pp);
  2949. return ret;
  2950. }
  2951. /* Stop the port, free port interrupt line */
  2952. static int mvneta_stop(struct net_device *dev)
  2953. {
  2954. struct mvneta_port *pp = netdev_priv(dev);
  2955. if (!pp->neta_armada3700) {
  2956. /* Inform that we are stopping so we don't want to setup the
  2957. * driver for new CPUs in the notifiers. The code of the
  2958. * notifier for CPU online is protected by the same spinlock,
  2959. * so when we get the lock, the notifer work is done.
  2960. */
  2961. spin_lock(&pp->lock);
  2962. pp->is_stopped = true;
  2963. spin_unlock(&pp->lock);
  2964. mvneta_stop_dev(pp);
  2965. mvneta_mdio_remove(pp);
  2966. cpuhp_state_remove_instance_nocalls(online_hpstate,
  2967. &pp->node_online);
  2968. cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
  2969. &pp->node_dead);
  2970. on_each_cpu(mvneta_percpu_disable, pp, true);
  2971. free_percpu_irq(dev->irq, pp->ports);
  2972. } else {
  2973. mvneta_stop_dev(pp);
  2974. mvneta_mdio_remove(pp);
  2975. free_irq(dev->irq, pp);
  2976. }
  2977. mvneta_cleanup_rxqs(pp);
  2978. mvneta_cleanup_txqs(pp);
  2979. return 0;
  2980. }
  2981. static int mvneta_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  2982. {
  2983. if (!dev->phydev)
  2984. return -ENOTSUPP;
  2985. return phy_mii_ioctl(dev->phydev, ifr, cmd);
  2986. }
  2987. /* Ethtool methods */
  2988. /* Set link ksettings (phy address, speed) for ethtools */
  2989. static int
  2990. mvneta_ethtool_set_link_ksettings(struct net_device *ndev,
  2991. const struct ethtool_link_ksettings *cmd)
  2992. {
  2993. struct mvneta_port *pp = netdev_priv(ndev);
  2994. struct phy_device *phydev = ndev->phydev;
  2995. if (!phydev)
  2996. return -ENODEV;
  2997. if ((cmd->base.autoneg == AUTONEG_ENABLE) != pp->use_inband_status) {
  2998. u32 val;
  2999. mvneta_set_autoneg(pp, cmd->base.autoneg == AUTONEG_ENABLE);
  3000. if (cmd->base.autoneg == AUTONEG_DISABLE) {
  3001. val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
  3002. val &= ~(MVNETA_GMAC_CONFIG_MII_SPEED |
  3003. MVNETA_GMAC_CONFIG_GMII_SPEED |
  3004. MVNETA_GMAC_CONFIG_FULL_DUPLEX);
  3005. if (phydev->duplex)
  3006. val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
  3007. if (phydev->speed == SPEED_1000)
  3008. val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
  3009. else if (phydev->speed == SPEED_100)
  3010. val |= MVNETA_GMAC_CONFIG_MII_SPEED;
  3011. mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
  3012. }
  3013. pp->use_inband_status = (cmd->base.autoneg == AUTONEG_ENABLE);
  3014. netdev_info(pp->dev, "autoneg status set to %i\n",
  3015. pp->use_inband_status);
  3016. if (netif_running(ndev)) {
  3017. mvneta_port_down(pp);
  3018. mvneta_port_up(pp);
  3019. }
  3020. }
  3021. return phy_ethtool_ksettings_set(ndev->phydev, cmd);
  3022. }
  3023. /* Set interrupt coalescing for ethtools */
  3024. static int mvneta_ethtool_set_coalesce(struct net_device *dev,
  3025. struct ethtool_coalesce *c)
  3026. {
  3027. struct mvneta_port *pp = netdev_priv(dev);
  3028. int queue;
  3029. for (queue = 0; queue < rxq_number; queue++) {
  3030. struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
  3031. rxq->time_coal = c->rx_coalesce_usecs;
  3032. rxq->pkts_coal = c->rx_max_coalesced_frames;
  3033. mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
  3034. mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
  3035. }
  3036. for (queue = 0; queue < txq_number; queue++) {
  3037. struct mvneta_tx_queue *txq = &pp->txqs[queue];
  3038. txq->done_pkts_coal = c->tx_max_coalesced_frames;
  3039. mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
  3040. }
  3041. return 0;
  3042. }
  3043. /* get coalescing for ethtools */
  3044. static int mvneta_ethtool_get_coalesce(struct net_device *dev,
  3045. struct ethtool_coalesce *c)
  3046. {
  3047. struct mvneta_port *pp = netdev_priv(dev);
  3048. c->rx_coalesce_usecs = pp->rxqs[0].time_coal;
  3049. c->rx_max_coalesced_frames = pp->rxqs[0].pkts_coal;
  3050. c->tx_max_coalesced_frames = pp->txqs[0].done_pkts_coal;
  3051. return 0;
  3052. }
  3053. static void mvneta_ethtool_get_drvinfo(struct net_device *dev,
  3054. struct ethtool_drvinfo *drvinfo)
  3055. {
  3056. strlcpy(drvinfo->driver, MVNETA_DRIVER_NAME,
  3057. sizeof(drvinfo->driver));
  3058. strlcpy(drvinfo->version, MVNETA_DRIVER_VERSION,
  3059. sizeof(drvinfo->version));
  3060. strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
  3061. sizeof(drvinfo->bus_info));
  3062. }
  3063. static void mvneta_ethtool_get_ringparam(struct net_device *netdev,
  3064. struct ethtool_ringparam *ring)
  3065. {
  3066. struct mvneta_port *pp = netdev_priv(netdev);
  3067. ring->rx_max_pending = MVNETA_MAX_RXD;
  3068. ring->tx_max_pending = MVNETA_MAX_TXD;
  3069. ring->rx_pending = pp->rx_ring_size;
  3070. ring->tx_pending = pp->tx_ring_size;
  3071. }
  3072. static int mvneta_ethtool_set_ringparam(struct net_device *dev,
  3073. struct ethtool_ringparam *ring)
  3074. {
  3075. struct mvneta_port *pp = netdev_priv(dev);
  3076. if ((ring->rx_pending == 0) || (ring->tx_pending == 0))
  3077. return -EINVAL;
  3078. pp->rx_ring_size = ring->rx_pending < MVNETA_MAX_RXD ?
  3079. ring->rx_pending : MVNETA_MAX_RXD;
  3080. pp->tx_ring_size = clamp_t(u16, ring->tx_pending,
  3081. MVNETA_MAX_SKB_DESCS * 2, MVNETA_MAX_TXD);
  3082. if (pp->tx_ring_size != ring->tx_pending)
  3083. netdev_warn(dev, "TX queue size set to %u (requested %u)\n",
  3084. pp->tx_ring_size, ring->tx_pending);
  3085. if (netif_running(dev)) {
  3086. mvneta_stop(dev);
  3087. if (mvneta_open(dev)) {
  3088. netdev_err(dev,
  3089. "error on opening device after ring param change\n");
  3090. return -ENOMEM;
  3091. }
  3092. }
  3093. return 0;
  3094. }
  3095. static void mvneta_ethtool_get_strings(struct net_device *netdev, u32 sset,
  3096. u8 *data)
  3097. {
  3098. if (sset == ETH_SS_STATS) {
  3099. int i;
  3100. for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
  3101. memcpy(data + i * ETH_GSTRING_LEN,
  3102. mvneta_statistics[i].name, ETH_GSTRING_LEN);
  3103. }
  3104. }
  3105. static void mvneta_ethtool_update_stats(struct mvneta_port *pp)
  3106. {
  3107. const struct mvneta_statistic *s;
  3108. void __iomem *base = pp->base;
  3109. u32 high, low, val;
  3110. u64 val64;
  3111. int i;
  3112. for (i = 0, s = mvneta_statistics;
  3113. s < mvneta_statistics + ARRAY_SIZE(mvneta_statistics);
  3114. s++, i++) {
  3115. switch (s->type) {
  3116. case T_REG_32:
  3117. val = readl_relaxed(base + s->offset);
  3118. pp->ethtool_stats[i] += val;
  3119. break;
  3120. case T_REG_64:
  3121. /* Docs say to read low 32-bit then high */
  3122. low = readl_relaxed(base + s->offset);
  3123. high = readl_relaxed(base + s->offset + 4);
  3124. val64 = (u64)high << 32 | low;
  3125. pp->ethtool_stats[i] += val64;
  3126. break;
  3127. }
  3128. }
  3129. }
  3130. static void mvneta_ethtool_get_stats(struct net_device *dev,
  3131. struct ethtool_stats *stats, u64 *data)
  3132. {
  3133. struct mvneta_port *pp = netdev_priv(dev);
  3134. int i;
  3135. mvneta_ethtool_update_stats(pp);
  3136. for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
  3137. *data++ = pp->ethtool_stats[i];
  3138. }
  3139. static int mvneta_ethtool_get_sset_count(struct net_device *dev, int sset)
  3140. {
  3141. if (sset == ETH_SS_STATS)
  3142. return ARRAY_SIZE(mvneta_statistics);
  3143. return -EOPNOTSUPP;
  3144. }
  3145. static u32 mvneta_ethtool_get_rxfh_indir_size(struct net_device *dev)
  3146. {
  3147. return MVNETA_RSS_LU_TABLE_SIZE;
  3148. }
  3149. static int mvneta_ethtool_get_rxnfc(struct net_device *dev,
  3150. struct ethtool_rxnfc *info,
  3151. u32 *rules __always_unused)
  3152. {
  3153. switch (info->cmd) {
  3154. case ETHTOOL_GRXRINGS:
  3155. info->data = rxq_number;
  3156. return 0;
  3157. case ETHTOOL_GRXFH:
  3158. return -EOPNOTSUPP;
  3159. default:
  3160. return -EOPNOTSUPP;
  3161. }
  3162. }
  3163. static int mvneta_config_rss(struct mvneta_port *pp)
  3164. {
  3165. int cpu;
  3166. u32 val;
  3167. netif_tx_stop_all_queues(pp->dev);
  3168. on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
  3169. /* We have to synchronise on the napi of each CPU */
  3170. for_each_online_cpu(cpu) {
  3171. struct mvneta_pcpu_port *pcpu_port =
  3172. per_cpu_ptr(pp->ports, cpu);
  3173. napi_synchronize(&pcpu_port->napi);
  3174. napi_disable(&pcpu_port->napi);
  3175. }
  3176. pp->rxq_def = pp->indir[0];
  3177. /* Update unicast mapping */
  3178. mvneta_set_rx_mode(pp->dev);
  3179. /* Update val of portCfg register accordingly with all RxQueue types */
  3180. val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
  3181. mvreg_write(pp, MVNETA_PORT_CONFIG, val);
  3182. /* Update the elected CPU matching the new rxq_def */
  3183. spin_lock(&pp->lock);
  3184. mvneta_percpu_elect(pp);
  3185. spin_unlock(&pp->lock);
  3186. /* We have to synchronise on the napi of each CPU */
  3187. for_each_online_cpu(cpu) {
  3188. struct mvneta_pcpu_port *pcpu_port =
  3189. per_cpu_ptr(pp->ports, cpu);
  3190. napi_enable(&pcpu_port->napi);
  3191. }
  3192. netif_tx_start_all_queues(pp->dev);
  3193. return 0;
  3194. }
  3195. static int mvneta_ethtool_set_rxfh(struct net_device *dev, const u32 *indir,
  3196. const u8 *key, const u8 hfunc)
  3197. {
  3198. struct mvneta_port *pp = netdev_priv(dev);
  3199. /* Current code for Armada 3700 doesn't support RSS features yet */
  3200. if (pp->neta_armada3700)
  3201. return -EOPNOTSUPP;
  3202. /* We require at least one supported parameter to be changed
  3203. * and no change in any of the unsupported parameters
  3204. */
  3205. if (key ||
  3206. (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
  3207. return -EOPNOTSUPP;
  3208. if (!indir)
  3209. return 0;
  3210. memcpy(pp->indir, indir, MVNETA_RSS_LU_TABLE_SIZE);
  3211. return mvneta_config_rss(pp);
  3212. }
  3213. static int mvneta_ethtool_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
  3214. u8 *hfunc)
  3215. {
  3216. struct mvneta_port *pp = netdev_priv(dev);
  3217. /* Current code for Armada 3700 doesn't support RSS features yet */
  3218. if (pp->neta_armada3700)
  3219. return -EOPNOTSUPP;
  3220. if (hfunc)
  3221. *hfunc = ETH_RSS_HASH_TOP;
  3222. if (!indir)
  3223. return 0;
  3224. memcpy(indir, pp->indir, MVNETA_RSS_LU_TABLE_SIZE);
  3225. return 0;
  3226. }
  3227. static void mvneta_ethtool_get_wol(struct net_device *dev,
  3228. struct ethtool_wolinfo *wol)
  3229. {
  3230. wol->supported = 0;
  3231. wol->wolopts = 0;
  3232. if (dev->phydev)
  3233. phy_ethtool_get_wol(dev->phydev, wol);
  3234. }
  3235. static int mvneta_ethtool_set_wol(struct net_device *dev,
  3236. struct ethtool_wolinfo *wol)
  3237. {
  3238. int ret;
  3239. if (!dev->phydev)
  3240. return -EOPNOTSUPP;
  3241. ret = phy_ethtool_set_wol(dev->phydev, wol);
  3242. if (!ret)
  3243. device_set_wakeup_enable(&dev->dev, !!wol->wolopts);
  3244. return ret;
  3245. }
  3246. static const struct net_device_ops mvneta_netdev_ops = {
  3247. .ndo_open = mvneta_open,
  3248. .ndo_stop = mvneta_stop,
  3249. .ndo_start_xmit = mvneta_tx,
  3250. .ndo_set_rx_mode = mvneta_set_rx_mode,
  3251. .ndo_set_mac_address = mvneta_set_mac_addr,
  3252. .ndo_change_mtu = mvneta_change_mtu,
  3253. .ndo_fix_features = mvneta_fix_features,
  3254. .ndo_get_stats64 = mvneta_get_stats64,
  3255. .ndo_do_ioctl = mvneta_ioctl,
  3256. };
  3257. static const struct ethtool_ops mvneta_eth_tool_ops = {
  3258. .nway_reset = phy_ethtool_nway_reset,
  3259. .get_link = ethtool_op_get_link,
  3260. .set_coalesce = mvneta_ethtool_set_coalesce,
  3261. .get_coalesce = mvneta_ethtool_get_coalesce,
  3262. .get_drvinfo = mvneta_ethtool_get_drvinfo,
  3263. .get_ringparam = mvneta_ethtool_get_ringparam,
  3264. .set_ringparam = mvneta_ethtool_set_ringparam,
  3265. .get_strings = mvneta_ethtool_get_strings,
  3266. .get_ethtool_stats = mvneta_ethtool_get_stats,
  3267. .get_sset_count = mvneta_ethtool_get_sset_count,
  3268. .get_rxfh_indir_size = mvneta_ethtool_get_rxfh_indir_size,
  3269. .get_rxnfc = mvneta_ethtool_get_rxnfc,
  3270. .get_rxfh = mvneta_ethtool_get_rxfh,
  3271. .set_rxfh = mvneta_ethtool_set_rxfh,
  3272. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  3273. .set_link_ksettings = mvneta_ethtool_set_link_ksettings,
  3274. .get_wol = mvneta_ethtool_get_wol,
  3275. .set_wol = mvneta_ethtool_set_wol,
  3276. };
  3277. /* Initialize hw */
  3278. static int mvneta_init(struct device *dev, struct mvneta_port *pp)
  3279. {
  3280. int queue;
  3281. /* Disable port */
  3282. mvneta_port_disable(pp);
  3283. /* Set port default values */
  3284. mvneta_defaults_set(pp);
  3285. pp->txqs = devm_kcalloc(dev, txq_number, sizeof(*pp->txqs), GFP_KERNEL);
  3286. if (!pp->txqs)
  3287. return -ENOMEM;
  3288. /* Initialize TX descriptor rings */
  3289. for (queue = 0; queue < txq_number; queue++) {
  3290. struct mvneta_tx_queue *txq = &pp->txqs[queue];
  3291. txq->id = queue;
  3292. txq->size = pp->tx_ring_size;
  3293. txq->done_pkts_coal = MVNETA_TXDONE_COAL_PKTS;
  3294. }
  3295. pp->rxqs = devm_kcalloc(dev, rxq_number, sizeof(*pp->rxqs), GFP_KERNEL);
  3296. if (!pp->rxqs)
  3297. return -ENOMEM;
  3298. /* Create Rx descriptor rings */
  3299. for (queue = 0; queue < rxq_number; queue++) {
  3300. struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
  3301. rxq->id = queue;
  3302. rxq->size = pp->rx_ring_size;
  3303. rxq->pkts_coal = MVNETA_RX_COAL_PKTS;
  3304. rxq->time_coal = MVNETA_RX_COAL_USEC;
  3305. rxq->buf_virt_addr
  3306. = devm_kmalloc_array(pp->dev->dev.parent,
  3307. rxq->size,
  3308. sizeof(*rxq->buf_virt_addr),
  3309. GFP_KERNEL);
  3310. if (!rxq->buf_virt_addr)
  3311. return -ENOMEM;
  3312. }
  3313. return 0;
  3314. }
  3315. /* platform glue : initialize decoding windows */
  3316. static void mvneta_conf_mbus_windows(struct mvneta_port *pp,
  3317. const struct mbus_dram_target_info *dram)
  3318. {
  3319. u32 win_enable;
  3320. u32 win_protect;
  3321. int i;
  3322. for (i = 0; i < 6; i++) {
  3323. mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
  3324. mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
  3325. if (i < 4)
  3326. mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
  3327. }
  3328. win_enable = 0x3f;
  3329. win_protect = 0;
  3330. if (dram) {
  3331. for (i = 0; i < dram->num_cs; i++) {
  3332. const struct mbus_dram_window *cs = dram->cs + i;
  3333. mvreg_write(pp, MVNETA_WIN_BASE(i),
  3334. (cs->base & 0xffff0000) |
  3335. (cs->mbus_attr << 8) |
  3336. dram->mbus_dram_target_id);
  3337. mvreg_write(pp, MVNETA_WIN_SIZE(i),
  3338. (cs->size - 1) & 0xffff0000);
  3339. win_enable &= ~(1 << i);
  3340. win_protect |= 3 << (2 * i);
  3341. }
  3342. } else {
  3343. /* For Armada3700 open default 4GB Mbus window, leaving
  3344. * arbitration of target/attribute to a different layer
  3345. * of configuration.
  3346. */
  3347. mvreg_write(pp, MVNETA_WIN_SIZE(0), 0xffff0000);
  3348. win_enable &= ~BIT(0);
  3349. win_protect = 3;
  3350. }
  3351. mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
  3352. mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect);
  3353. }
  3354. /* Power up the port */
  3355. static int mvneta_port_power_up(struct mvneta_port *pp, int phy_mode)
  3356. {
  3357. u32 ctrl;
  3358. /* MAC Cause register should be cleared */
  3359. mvreg_write(pp, MVNETA_UNIT_INTR_CAUSE, 0);
  3360. ctrl = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
  3361. /* Even though it might look weird, when we're configured in
  3362. * SGMII or QSGMII mode, the RGMII bit needs to be set.
  3363. */
  3364. switch(phy_mode) {
  3365. case PHY_INTERFACE_MODE_QSGMII:
  3366. mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_QSGMII_SERDES_PROTO);
  3367. ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
  3368. break;
  3369. case PHY_INTERFACE_MODE_SGMII:
  3370. mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_SGMII_SERDES_PROTO);
  3371. ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
  3372. break;
  3373. case PHY_INTERFACE_MODE_RGMII:
  3374. case PHY_INTERFACE_MODE_RGMII_ID:
  3375. case PHY_INTERFACE_MODE_RGMII_RXID:
  3376. case PHY_INTERFACE_MODE_RGMII_TXID:
  3377. ctrl |= MVNETA_GMAC2_PORT_RGMII;
  3378. break;
  3379. default:
  3380. return -EINVAL;
  3381. }
  3382. /* Cancel Port Reset */
  3383. ctrl &= ~MVNETA_GMAC2_PORT_RESET;
  3384. mvreg_write(pp, MVNETA_GMAC_CTRL_2, ctrl);
  3385. while ((mvreg_read(pp, MVNETA_GMAC_CTRL_2) &
  3386. MVNETA_GMAC2_PORT_RESET) != 0)
  3387. continue;
  3388. return 0;
  3389. }
  3390. /* Device initialization routine */
  3391. static int mvneta_probe(struct platform_device *pdev)
  3392. {
  3393. struct resource *res;
  3394. struct device_node *dn = pdev->dev.of_node;
  3395. struct device_node *phy_node;
  3396. struct device_node *bm_node;
  3397. struct mvneta_port *pp;
  3398. struct net_device *dev;
  3399. const char *dt_mac_addr;
  3400. char hw_mac_addr[ETH_ALEN];
  3401. const char *mac_from;
  3402. const char *managed;
  3403. int tx_csum_limit;
  3404. int phy_mode;
  3405. int err;
  3406. int cpu;
  3407. dev = alloc_etherdev_mqs(sizeof(struct mvneta_port), txq_number, rxq_number);
  3408. if (!dev)
  3409. return -ENOMEM;
  3410. dev->irq = irq_of_parse_and_map(dn, 0);
  3411. if (dev->irq == 0) {
  3412. err = -EINVAL;
  3413. goto err_free_netdev;
  3414. }
  3415. phy_node = of_parse_phandle(dn, "phy", 0);
  3416. if (!phy_node) {
  3417. if (!of_phy_is_fixed_link(dn)) {
  3418. dev_err(&pdev->dev, "no PHY specified\n");
  3419. err = -ENODEV;
  3420. goto err_free_irq;
  3421. }
  3422. err = of_phy_register_fixed_link(dn);
  3423. if (err < 0) {
  3424. dev_err(&pdev->dev, "cannot register fixed PHY\n");
  3425. goto err_free_irq;
  3426. }
  3427. /* In the case of a fixed PHY, the DT node associated
  3428. * to the PHY is the Ethernet MAC DT node.
  3429. */
  3430. phy_node = of_node_get(dn);
  3431. }
  3432. phy_mode = of_get_phy_mode(dn);
  3433. if (phy_mode < 0) {
  3434. dev_err(&pdev->dev, "incorrect phy-mode\n");
  3435. err = -EINVAL;
  3436. goto err_put_phy_node;
  3437. }
  3438. dev->tx_queue_len = MVNETA_MAX_TXD;
  3439. dev->watchdog_timeo = 5 * HZ;
  3440. dev->netdev_ops = &mvneta_netdev_ops;
  3441. dev->ethtool_ops = &mvneta_eth_tool_ops;
  3442. pp = netdev_priv(dev);
  3443. spin_lock_init(&pp->lock);
  3444. pp->phy_node = phy_node;
  3445. pp->phy_interface = phy_mode;
  3446. err = of_property_read_string(dn, "managed", &managed);
  3447. pp->use_inband_status = (err == 0 &&
  3448. strcmp(managed, "in-band-status") == 0);
  3449. pp->rxq_def = rxq_def;
  3450. /* Set RX packet offset correction for platforms, whose
  3451. * NET_SKB_PAD, exceeds 64B. It should be 64B for 64-bit
  3452. * platforms and 0B for 32-bit ones.
  3453. */
  3454. pp->rx_offset_correction =
  3455. max(0, NET_SKB_PAD - MVNETA_RX_PKT_OFFSET_CORRECTION);
  3456. pp->indir[0] = rxq_def;
  3457. /* Get special SoC configurations */
  3458. if (of_device_is_compatible(dn, "marvell,armada-3700-neta"))
  3459. pp->neta_armada3700 = true;
  3460. pp->clk = devm_clk_get(&pdev->dev, "core");
  3461. if (IS_ERR(pp->clk))
  3462. pp->clk = devm_clk_get(&pdev->dev, NULL);
  3463. if (IS_ERR(pp->clk)) {
  3464. err = PTR_ERR(pp->clk);
  3465. goto err_put_phy_node;
  3466. }
  3467. clk_prepare_enable(pp->clk);
  3468. pp->clk_bus = devm_clk_get(&pdev->dev, "bus");
  3469. if (!IS_ERR(pp->clk_bus))
  3470. clk_prepare_enable(pp->clk_bus);
  3471. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  3472. pp->base = devm_ioremap_resource(&pdev->dev, res);
  3473. if (IS_ERR(pp->base)) {
  3474. err = PTR_ERR(pp->base);
  3475. goto err_clk;
  3476. }
  3477. /* Alloc per-cpu port structure */
  3478. pp->ports = alloc_percpu(struct mvneta_pcpu_port);
  3479. if (!pp->ports) {
  3480. err = -ENOMEM;
  3481. goto err_clk;
  3482. }
  3483. /* Alloc per-cpu stats */
  3484. pp->stats = netdev_alloc_pcpu_stats(struct mvneta_pcpu_stats);
  3485. if (!pp->stats) {
  3486. err = -ENOMEM;
  3487. goto err_free_ports;
  3488. }
  3489. dt_mac_addr = of_get_mac_address(dn);
  3490. if (dt_mac_addr) {
  3491. mac_from = "device tree";
  3492. memcpy(dev->dev_addr, dt_mac_addr, ETH_ALEN);
  3493. } else {
  3494. mvneta_get_mac_addr(pp, hw_mac_addr);
  3495. if (is_valid_ether_addr(hw_mac_addr)) {
  3496. mac_from = "hardware";
  3497. memcpy(dev->dev_addr, hw_mac_addr, ETH_ALEN);
  3498. } else {
  3499. mac_from = "random";
  3500. eth_hw_addr_random(dev);
  3501. }
  3502. }
  3503. if (!of_property_read_u32(dn, "tx-csum-limit", &tx_csum_limit)) {
  3504. if (tx_csum_limit < 0 ||
  3505. tx_csum_limit > MVNETA_TX_CSUM_MAX_SIZE) {
  3506. tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE;
  3507. dev_info(&pdev->dev,
  3508. "Wrong TX csum limit in DT, set to %dB\n",
  3509. MVNETA_TX_CSUM_DEF_SIZE);
  3510. }
  3511. } else if (of_device_is_compatible(dn, "marvell,armada-370-neta")) {
  3512. tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE;
  3513. } else {
  3514. tx_csum_limit = MVNETA_TX_CSUM_MAX_SIZE;
  3515. }
  3516. pp->tx_csum_limit = tx_csum_limit;
  3517. pp->dram_target_info = mv_mbus_dram_info();
  3518. /* Armada3700 requires setting default configuration of Mbus
  3519. * windows, however without using filled mbus_dram_target_info
  3520. * structure.
  3521. */
  3522. if (pp->dram_target_info || pp->neta_armada3700)
  3523. mvneta_conf_mbus_windows(pp, pp->dram_target_info);
  3524. pp->tx_ring_size = MVNETA_MAX_TXD;
  3525. pp->rx_ring_size = MVNETA_MAX_RXD;
  3526. pp->dev = dev;
  3527. SET_NETDEV_DEV(dev, &pdev->dev);
  3528. pp->id = global_port_id++;
  3529. /* Obtain access to BM resources if enabled and already initialized */
  3530. bm_node = of_parse_phandle(dn, "buffer-manager", 0);
  3531. if (bm_node && bm_node->data) {
  3532. pp->bm_priv = bm_node->data;
  3533. err = mvneta_bm_port_init(pdev, pp);
  3534. if (err < 0) {
  3535. dev_info(&pdev->dev, "use SW buffer management\n");
  3536. pp->bm_priv = NULL;
  3537. }
  3538. }
  3539. of_node_put(bm_node);
  3540. err = mvneta_init(&pdev->dev, pp);
  3541. if (err < 0)
  3542. goto err_netdev;
  3543. err = mvneta_port_power_up(pp, phy_mode);
  3544. if (err < 0) {
  3545. dev_err(&pdev->dev, "can't power up port\n");
  3546. goto err_netdev;
  3547. }
  3548. /* Armada3700 network controller does not support per-cpu
  3549. * operation, so only single NAPI should be initialized.
  3550. */
  3551. if (pp->neta_armada3700) {
  3552. netif_napi_add(dev, &pp->napi, mvneta_poll, NAPI_POLL_WEIGHT);
  3553. } else {
  3554. for_each_present_cpu(cpu) {
  3555. struct mvneta_pcpu_port *port =
  3556. per_cpu_ptr(pp->ports, cpu);
  3557. netif_napi_add(dev, &port->napi, mvneta_poll,
  3558. NAPI_POLL_WEIGHT);
  3559. port->pp = pp;
  3560. }
  3561. }
  3562. dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
  3563. dev->hw_features |= dev->features;
  3564. dev->vlan_features |= dev->features;
  3565. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  3566. dev->gso_max_segs = MVNETA_MAX_TSO_SEGS;
  3567. /* MTU range: 68 - 9676 */
  3568. dev->min_mtu = ETH_MIN_MTU;
  3569. /* 9676 == 9700 - 20 and rounding to 8 */
  3570. dev->max_mtu = 9676;
  3571. err = register_netdev(dev);
  3572. if (err < 0) {
  3573. dev_err(&pdev->dev, "failed to register\n");
  3574. goto err_free_stats;
  3575. }
  3576. netdev_info(dev, "Using %s mac address %pM\n", mac_from,
  3577. dev->dev_addr);
  3578. platform_set_drvdata(pdev, pp->dev);
  3579. if (pp->use_inband_status) {
  3580. struct phy_device *phy = of_phy_find_device(dn);
  3581. mvneta_fixed_link_update(pp, phy);
  3582. put_device(&phy->mdio.dev);
  3583. }
  3584. return 0;
  3585. err_netdev:
  3586. unregister_netdev(dev);
  3587. if (pp->bm_priv) {
  3588. mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
  3589. mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short,
  3590. 1 << pp->id);
  3591. }
  3592. err_free_stats:
  3593. free_percpu(pp->stats);
  3594. err_free_ports:
  3595. free_percpu(pp->ports);
  3596. err_clk:
  3597. clk_disable_unprepare(pp->clk_bus);
  3598. clk_disable_unprepare(pp->clk);
  3599. err_put_phy_node:
  3600. of_node_put(phy_node);
  3601. if (of_phy_is_fixed_link(dn))
  3602. of_phy_deregister_fixed_link(dn);
  3603. err_free_irq:
  3604. irq_dispose_mapping(dev->irq);
  3605. err_free_netdev:
  3606. free_netdev(dev);
  3607. return err;
  3608. }
  3609. /* Device removal routine */
  3610. static int mvneta_remove(struct platform_device *pdev)
  3611. {
  3612. struct net_device *dev = platform_get_drvdata(pdev);
  3613. struct device_node *dn = pdev->dev.of_node;
  3614. struct mvneta_port *pp = netdev_priv(dev);
  3615. unregister_netdev(dev);
  3616. clk_disable_unprepare(pp->clk_bus);
  3617. clk_disable_unprepare(pp->clk);
  3618. free_percpu(pp->ports);
  3619. free_percpu(pp->stats);
  3620. if (of_phy_is_fixed_link(dn))
  3621. of_phy_deregister_fixed_link(dn);
  3622. irq_dispose_mapping(dev->irq);
  3623. of_node_put(pp->phy_node);
  3624. free_netdev(dev);
  3625. if (pp->bm_priv) {
  3626. mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
  3627. mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short,
  3628. 1 << pp->id);
  3629. }
  3630. return 0;
  3631. }
  3632. #ifdef CONFIG_PM_SLEEP
  3633. static int mvneta_suspend(struct device *device)
  3634. {
  3635. struct net_device *dev = dev_get_drvdata(device);
  3636. struct mvneta_port *pp = netdev_priv(dev);
  3637. if (netif_running(dev))
  3638. mvneta_stop(dev);
  3639. netif_device_detach(dev);
  3640. clk_disable_unprepare(pp->clk_bus);
  3641. clk_disable_unprepare(pp->clk);
  3642. return 0;
  3643. }
  3644. static int mvneta_resume(struct device *device)
  3645. {
  3646. struct platform_device *pdev = to_platform_device(device);
  3647. struct net_device *dev = dev_get_drvdata(device);
  3648. struct mvneta_port *pp = netdev_priv(dev);
  3649. int err;
  3650. clk_prepare_enable(pp->clk);
  3651. if (!IS_ERR(pp->clk_bus))
  3652. clk_prepare_enable(pp->clk_bus);
  3653. if (pp->dram_target_info || pp->neta_armada3700)
  3654. mvneta_conf_mbus_windows(pp, pp->dram_target_info);
  3655. if (pp->bm_priv) {
  3656. err = mvneta_bm_port_init(pdev, pp);
  3657. if (err < 0) {
  3658. dev_info(&pdev->dev, "use SW buffer management\n");
  3659. pp->bm_priv = NULL;
  3660. }
  3661. }
  3662. mvneta_defaults_set(pp);
  3663. err = mvneta_port_power_up(pp, pp->phy_interface);
  3664. if (err < 0) {
  3665. dev_err(device, "can't power up port\n");
  3666. return err;
  3667. }
  3668. if (pp->use_inband_status)
  3669. mvneta_fixed_link_update(pp, dev->phydev);
  3670. netif_device_attach(dev);
  3671. if (netif_running(dev)) {
  3672. mvneta_open(dev);
  3673. mvneta_set_rx_mode(dev);
  3674. }
  3675. return 0;
  3676. }
  3677. #endif
  3678. static SIMPLE_DEV_PM_OPS(mvneta_pm_ops, mvneta_suspend, mvneta_resume);
  3679. static const struct of_device_id mvneta_match[] = {
  3680. { .compatible = "marvell,armada-370-neta" },
  3681. { .compatible = "marvell,armada-xp-neta" },
  3682. { .compatible = "marvell,armada-3700-neta" },
  3683. { }
  3684. };
  3685. MODULE_DEVICE_TABLE(of, mvneta_match);
  3686. static struct platform_driver mvneta_driver = {
  3687. .probe = mvneta_probe,
  3688. .remove = mvneta_remove,
  3689. .driver = {
  3690. .name = MVNETA_DRIVER_NAME,
  3691. .of_match_table = mvneta_match,
  3692. .pm = &mvneta_pm_ops,
  3693. },
  3694. };
  3695. static int __init mvneta_driver_init(void)
  3696. {
  3697. int ret;
  3698. ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "net/mvmeta:online",
  3699. mvneta_cpu_online,
  3700. mvneta_cpu_down_prepare);
  3701. if (ret < 0)
  3702. goto out;
  3703. online_hpstate = ret;
  3704. ret = cpuhp_setup_state_multi(CPUHP_NET_MVNETA_DEAD, "net/mvneta:dead",
  3705. NULL, mvneta_cpu_dead);
  3706. if (ret)
  3707. goto err_dead;
  3708. ret = platform_driver_register(&mvneta_driver);
  3709. if (ret)
  3710. goto err;
  3711. return 0;
  3712. err:
  3713. cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD);
  3714. err_dead:
  3715. cpuhp_remove_multi_state(online_hpstate);
  3716. out:
  3717. return ret;
  3718. }
  3719. module_init(mvneta_driver_init);
  3720. static void __exit mvneta_driver_exit(void)
  3721. {
  3722. platform_driver_unregister(&mvneta_driver);
  3723. cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD);
  3724. cpuhp_remove_multi_state(online_hpstate);
  3725. }
  3726. module_exit(mvneta_driver_exit);
  3727. MODULE_DESCRIPTION("Marvell NETA Ethernet Driver - www.marvell.com");
  3728. MODULE_AUTHOR("Rami Rosen <rosenr@marvell.com>, Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
  3729. MODULE_LICENSE("GPL");
  3730. module_param(rxq_number, int, S_IRUGO);
  3731. module_param(txq_number, int, S_IRUGO);
  3732. module_param(rxq_def, int, S_IRUGO);
  3733. module_param(rx_copybreak, int, S_IRUGO | S_IWUSR);