mvneta.c 128 KB

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