mvneta.c 95 KB

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