mvneta.c 115 KB

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