mvneta.c 119 KB

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