mvneta.c 114 KB

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